(custom-group-value-create): Insert some
[emacs.git] / src / keymap.c
blob7a95e75c84ee5acaa184bc488b555f80ab90fa78
1 /* Manipulation of keymaps
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <stdio.h>
24 #if HAVE_ALLOCA_H
25 # include <alloca.h>
26 #endif
27 #include "lisp.h"
28 #include "commands.h"
29 #include "buffer.h"
30 #include "character.h"
31 #include "charset.h"
32 #include "keyboard.h"
33 #include "frame.h"
34 #include "termhooks.h"
35 #include "blockinput.h"
36 #include "puresize.h"
37 #include "intervals.h"
38 #include "keymap.h"
39 #include "window.h"
41 /* The number of elements in keymap vectors. */
42 #define DENSE_TABLE_SIZE (0200)
44 /* Actually allocate storage for these variables */
46 Lisp_Object current_global_map; /* Current global keymap */
48 Lisp_Object global_map; /* default global key bindings */
50 Lisp_Object meta_map; /* The keymap used for globally bound
51 ESC-prefixed default commands */
53 Lisp_Object control_x_map; /* The keymap used for globally bound
54 C-x-prefixed default commands */
56 /* was MinibufLocalMap */
57 Lisp_Object Vminibuffer_local_map;
58 /* The keymap used by the minibuf for local
59 bindings when spaces are allowed in the
60 minibuf */
62 /* was MinibufLocalNSMap */
63 Lisp_Object Vminibuffer_local_ns_map;
64 /* The keymap used by the minibuf for local
65 bindings when spaces are not encouraged
66 in the minibuf */
68 /* keymap used for minibuffers when doing completion */
69 /* was MinibufLocalCompletionMap */
70 Lisp_Object Vminibuffer_local_completion_map;
72 /* keymap used for minibuffers when doing completion in filenames */
73 Lisp_Object Vminibuffer_local_filename_completion_map;
75 /* keymap used for minibuffers when doing completion in filenames
76 with require-match*/
77 Lisp_Object Vminibuffer_local_filename_must_match_map;
79 /* keymap used for minibuffers when doing completion and require a match */
80 /* was MinibufLocalMustMatchMap */
81 Lisp_Object Vminibuffer_local_must_match_map;
83 /* Alist of minor mode variables and keymaps. */
84 Lisp_Object Vminor_mode_map_alist;
86 /* Alist of major-mode-specific overrides for
87 minor mode variables and keymaps. */
88 Lisp_Object Vminor_mode_overriding_map_alist;
90 /* List of emulation mode keymap alists. */
91 Lisp_Object Vemulation_mode_map_alists;
93 /* A list of all commands given new bindings since a certain time
94 when nil was stored here.
95 This is used to speed up recomputation of menu key equivalents
96 when Emacs starts up. t means don't record anything here. */
97 Lisp_Object Vdefine_key_rebound_commands;
99 Lisp_Object Qkeymapp, Qkeymap, Qnon_ascii, Qmenu_item, Qremap;
101 /* Alist of elements like (DEL . "\d"). */
102 static Lisp_Object exclude_keys;
104 /* Pre-allocated 2-element vector for Fcommand_remapping to use. */
105 static Lisp_Object command_remapping_vector;
107 /* A char with the CHAR_META bit set in a vector or the 0200 bit set
108 in a string key sequence is equivalent to prefixing with this
109 character. */
110 extern Lisp_Object meta_prefix_char;
112 extern Lisp_Object Voverriding_local_map;
114 /* Hash table used to cache a reverse-map to speed up calls to where-is. */
115 static Lisp_Object where_is_cache;
116 /* Which keymaps are reverse-stored in the cache. */
117 static Lisp_Object where_is_cache_keymaps;
119 static Lisp_Object store_in_keymap P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
120 static void fix_submap_inheritance P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
122 static Lisp_Object define_as_prefix P_ ((Lisp_Object, Lisp_Object));
123 static void describe_command P_ ((Lisp_Object, Lisp_Object));
124 static void describe_translation P_ ((Lisp_Object, Lisp_Object));
125 static void describe_map P_ ((Lisp_Object, Lisp_Object,
126 void (*) P_ ((Lisp_Object, Lisp_Object)),
127 int, Lisp_Object, Lisp_Object*, int, int));
128 static void describe_vector P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
129 void (*) (Lisp_Object, Lisp_Object), int,
130 Lisp_Object, Lisp_Object, int *,
131 int, int, int));
132 static void silly_event_symbol_error P_ ((Lisp_Object));
134 /* Keymap object support - constructors and predicates. */
136 DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0,
137 doc: /* Construct and return a new keymap, of the form (keymap CHARTABLE . ALIST).
138 CHARTABLE is a char-table that holds the bindings for all characters
139 without modifiers. All entries in it are initially nil, meaning
140 "command undefined". ALIST is an assoc-list which holds bindings for
141 function keys, mouse events, and any other things that appear in the
142 input stream. Initially, ALIST is nil.
144 The optional arg STRING supplies a menu name for the keymap
145 in case you use it as a menu with `x-popup-menu'. */)
146 (string)
147 Lisp_Object string;
149 Lisp_Object tail;
150 if (!NILP (string))
151 tail = Fcons (string, Qnil);
152 else
153 tail = Qnil;
154 return Fcons (Qkeymap,
155 Fcons (Fmake_char_table (Qkeymap, Qnil), tail));
158 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0,
159 doc: /* Construct and return a new sparse keymap.
160 Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),
161 which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),
162 which binds the function key or mouse event SYMBOL to DEFINITION.
163 Initially the alist is nil.
165 The optional arg STRING supplies a menu name for the keymap
166 in case you use it as a menu with `x-popup-menu'. */)
167 (string)
168 Lisp_Object string;
170 if (!NILP (string))
171 return Fcons (Qkeymap, Fcons (string, Qnil));
172 return Fcons (Qkeymap, Qnil);
175 /* This function is used for installing the standard key bindings
176 at initialization time.
178 For example:
180 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */
182 void
183 initial_define_key (keymap, key, defname)
184 Lisp_Object keymap;
185 int key;
186 char *defname;
188 store_in_keymap (keymap, make_number (key), intern (defname));
191 void
192 initial_define_lispy_key (keymap, keyname, defname)
193 Lisp_Object keymap;
194 char *keyname;
195 char *defname;
197 store_in_keymap (keymap, intern (keyname), intern (defname));
200 DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0,
201 doc: /* Return t if OBJECT is a keymap.
203 A keymap is a list (keymap . ALIST),
204 or a symbol whose function definition is itself a keymap.
205 ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);
206 a vector of densely packed bindings for small character codes
207 is also allowed as an element. */)
208 (object)
209 Lisp_Object object;
211 return (KEYMAPP (object) ? Qt : Qnil);
214 DEFUN ("keymap-prompt", Fkeymap_prompt, Skeymap_prompt, 1, 1, 0,
215 doc: /* Return the prompt-string of a keymap MAP.
216 If non-nil, the prompt is shown in the echo-area
217 when reading a key-sequence to be looked-up in this keymap. */)
218 (map)
219 Lisp_Object map;
221 map = get_keymap (map, 0, 0);
222 while (CONSP (map))
224 Lisp_Object tem = XCAR (map);
225 if (STRINGP (tem))
226 return tem;
227 map = XCDR (map);
229 return Qnil;
232 /* Check that OBJECT is a keymap (after dereferencing through any
233 symbols). If it is, return it.
235 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
236 is an autoload form, do the autoload and try again.
237 If AUTOLOAD is nonzero, callers must assume GC is possible.
239 If the map needs to be autoloaded, but AUTOLOAD is zero (and ERROR
240 is zero as well), return Qt.
242 ERROR controls how we respond if OBJECT isn't a keymap.
243 If ERROR is non-zero, signal an error; otherwise, just return Qnil.
245 Note that most of the time, we don't want to pursue autoloads.
246 Functions like Faccessible_keymaps which scan entire keymap trees
247 shouldn't load every autoloaded keymap. I'm not sure about this,
248 but it seems to me that only read_key_sequence, Flookup_key, and
249 Fdefine_key should cause keymaps to be autoloaded.
251 This function can GC when AUTOLOAD is non-zero, because it calls
252 do_autoload which can GC. */
254 Lisp_Object
255 get_keymap (object, error, autoload)
256 Lisp_Object object;
257 int error, autoload;
259 Lisp_Object tem;
261 autoload_retry:
262 if (NILP (object))
263 goto end;
264 if (CONSP (object) && EQ (XCAR (object), Qkeymap))
265 return object;
267 tem = indirect_function (object);
268 if (CONSP (tem))
270 if (EQ (XCAR (tem), Qkeymap))
271 return tem;
273 /* Should we do an autoload? Autoload forms for keymaps have
274 Qkeymap as their fifth element. */
275 if ((autoload || !error) && EQ (XCAR (tem), Qautoload)
276 && SYMBOLP (object))
278 Lisp_Object tail;
280 tail = Fnth (make_number (4), tem);
281 if (EQ (tail, Qkeymap))
283 if (autoload)
285 struct gcpro gcpro1, gcpro2;
287 GCPRO2 (tem, object);
288 do_autoload (tem, object);
289 UNGCPRO;
291 goto autoload_retry;
293 else
294 return Qt;
299 end:
300 if (error)
301 wrong_type_argument (Qkeymapp, object);
302 return Qnil;
305 /* Return the parent map of KEYMAP, or nil if it has none.
306 We assume that KEYMAP is a valid keymap. */
308 Lisp_Object
309 keymap_parent (keymap, autoload)
310 Lisp_Object keymap;
311 int autoload;
313 Lisp_Object list;
315 keymap = get_keymap (keymap, 1, autoload);
317 /* Skip past the initial element `keymap'. */
318 list = XCDR (keymap);
319 for (; CONSP (list); list = XCDR (list))
321 /* See if there is another `keymap'. */
322 if (KEYMAPP (list))
323 return list;
326 return get_keymap (list, 0, autoload);
329 DEFUN ("keymap-parent", Fkeymap_parent, Skeymap_parent, 1, 1, 0,
330 doc: /* Return the parent keymap of KEYMAP. */)
331 (keymap)
332 Lisp_Object keymap;
334 return keymap_parent (keymap, 1);
337 /* Check whether MAP is one of MAPS parents. */
339 keymap_memberp (map, maps)
340 Lisp_Object map, maps;
342 if (NILP (map)) return 0;
343 while (KEYMAPP (maps) && !EQ (map, maps))
344 maps = keymap_parent (maps, 0);
345 return (EQ (map, maps));
348 /* Set the parent keymap of MAP to PARENT. */
350 DEFUN ("set-keymap-parent", Fset_keymap_parent, Sset_keymap_parent, 2, 2, 0,
351 doc: /* Modify KEYMAP to set its parent map to PARENT.
352 Return PARENT. PARENT should be nil or another keymap. */)
353 (keymap, parent)
354 Lisp_Object keymap, parent;
356 Lisp_Object list, prev;
357 struct gcpro gcpro1, gcpro2;
358 int i;
360 /* Force a keymap flush for the next call to where-is.
361 Since this can be called from within where-is, we don't set where_is_cache
362 directly but only where_is_cache_keymaps, since where_is_cache shouldn't
363 be changed during where-is, while where_is_cache_keymaps is only used at
364 the very beginning of where-is and can thus be changed here without any
365 adverse effect.
366 This is a very minor correctness (rather than safety) issue. */
367 where_is_cache_keymaps = Qt;
369 GCPRO2 (keymap, parent);
370 keymap = get_keymap (keymap, 1, 1);
372 if (!NILP (parent))
374 parent = get_keymap (parent, 1, 1);
376 /* Check for cycles. */
377 if (keymap_memberp (keymap, parent))
378 error ("Cyclic keymap inheritance");
381 /* Skip past the initial element `keymap'. */
382 prev = keymap;
383 while (1)
385 list = XCDR (prev);
386 /* If there is a parent keymap here, replace it.
387 If we came to the end, add the parent in PREV. */
388 if (!CONSP (list) || KEYMAPP (list))
390 /* If we already have the right parent, return now
391 so that we avoid the loops below. */
392 if (EQ (XCDR (prev), parent))
393 RETURN_UNGCPRO (parent);
395 CHECK_IMPURE (prev);
396 XSETCDR (prev, parent);
397 break;
399 prev = list;
402 /* Scan through for submaps, and set their parents too. */
404 for (list = XCDR (keymap); CONSP (list); list = XCDR (list))
406 /* Stop the scan when we come to the parent. */
407 if (EQ (XCAR (list), Qkeymap))
408 break;
410 /* If this element holds a prefix map, deal with it. */
411 if (CONSP (XCAR (list))
412 && CONSP (XCDR (XCAR (list))))
413 fix_submap_inheritance (keymap, XCAR (XCAR (list)),
414 XCDR (XCAR (list)));
416 if (VECTORP (XCAR (list)))
417 for (i = 0; i < XVECTOR (XCAR (list))->size; i++)
418 if (CONSP (XVECTOR (XCAR (list))->contents[i]))
419 fix_submap_inheritance (keymap, make_number (i),
420 XVECTOR (XCAR (list))->contents[i]);
422 if (CHAR_TABLE_P (XCAR (list)))
424 map_char_table (fix_submap_inheritance, Qnil, XCAR (list), keymap);
428 RETURN_UNGCPRO (parent);
431 /* EVENT is defined in MAP as a prefix, and SUBMAP is its definition.
432 if EVENT is also a prefix in MAP's parent,
433 make sure that SUBMAP inherits that definition as its own parent. */
435 static void
436 fix_submap_inheritance (map, event, submap)
437 Lisp_Object map, event, submap;
439 Lisp_Object map_parent, parent_entry;
441 /* SUBMAP is a cons that we found as a key binding.
442 Discard the other things found in a menu key binding. */
444 submap = get_keymap (get_keyelt (submap, 0), 0, 0);
446 /* If it isn't a keymap now, there's no work to do. */
447 if (!CONSP (submap))
448 return;
450 map_parent = keymap_parent (map, 0);
451 if (!NILP (map_parent))
452 parent_entry =
453 get_keymap (access_keymap (map_parent, event, 0, 0, 0), 0, 0);
454 else
455 parent_entry = Qnil;
457 /* If MAP's parent has something other than a keymap,
458 our own submap shadows it completely. */
459 if (!CONSP (parent_entry))
460 return;
462 if (! EQ (parent_entry, submap))
464 Lisp_Object submap_parent;
465 submap_parent = submap;
466 while (1)
468 Lisp_Object tem;
470 tem = keymap_parent (submap_parent, 0);
472 if (KEYMAPP (tem))
474 if (keymap_memberp (tem, parent_entry))
475 /* Fset_keymap_parent could create a cycle. */
476 return;
477 submap_parent = tem;
479 else
480 break;
482 Fset_keymap_parent (submap_parent, parent_entry);
486 /* Look up IDX in MAP. IDX may be any sort of event.
487 Note that this does only one level of lookup; IDX must be a single
488 event, not a sequence.
490 If T_OK is non-zero, bindings for Qt are treated as default
491 bindings; any key left unmentioned by other tables and bindings is
492 given the binding of Qt.
494 If T_OK is zero, bindings for Qt are not treated specially.
496 If NOINHERIT, don't accept a subkeymap found in an inherited keymap. */
498 Lisp_Object
499 access_keymap (map, idx, t_ok, noinherit, autoload)
500 Lisp_Object map;
501 Lisp_Object idx;
502 int t_ok;
503 int noinherit;
504 int autoload;
506 Lisp_Object val;
508 /* Qunbound in VAL means we have found no binding yet. */
509 val = Qunbound;
511 /* If idx is a list (some sort of mouse click, perhaps?),
512 the index we want to use is the car of the list, which
513 ought to be a symbol. */
514 idx = EVENT_HEAD (idx);
516 /* If idx is a symbol, it might have modifiers, which need to
517 be put in the canonical order. */
518 if (SYMBOLP (idx))
519 idx = reorder_modifiers (idx);
520 else if (INTEGERP (idx))
521 /* Clobber the high bits that can be present on a machine
522 with more than 24 bits of integer. */
523 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
525 /* Handle the special meta -> esc mapping. */
526 if (INTEGERP (idx) && XUINT (idx) & meta_modifier)
528 /* See if there is a meta-map. If there's none, there is
529 no binding for IDX, unless a default binding exists in MAP. */
530 struct gcpro gcpro1;
531 Lisp_Object meta_map;
532 GCPRO1 (map);
533 /* A strange value in which Meta is set would cause
534 infinite recursion. Protect against that. */
535 if (XINT (meta_prefix_char) & CHAR_META)
536 meta_prefix_char = make_number (27);
537 meta_map = get_keymap (access_keymap (map, meta_prefix_char,
538 t_ok, noinherit, autoload),
539 0, autoload);
540 UNGCPRO;
541 if (CONSP (meta_map))
543 map = meta_map;
544 idx = make_number (XUINT (idx) & ~meta_modifier);
546 else if (t_ok)
547 /* Set IDX to t, so that we only find a default binding. */
548 idx = Qt;
549 else
550 /* We know there is no binding. */
551 return Qnil;
554 /* t_binding is where we put a default binding that applies,
555 to use in case we do not find a binding specifically
556 for this key sequence. */
558 Lisp_Object tail;
559 Lisp_Object t_binding = Qnil;
560 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
562 GCPRO4 (map, tail, idx, t_binding);
564 for (tail = XCDR (map);
565 (CONSP (tail)
566 || (tail = get_keymap (tail, 0, autoload), CONSP (tail)));
567 tail = XCDR (tail))
569 Lisp_Object binding;
571 binding = XCAR (tail);
572 if (SYMBOLP (binding))
574 /* If NOINHERIT, stop finding prefix definitions
575 after we pass a second occurrence of the `keymap' symbol. */
576 if (noinherit && EQ (binding, Qkeymap))
577 RETURN_UNGCPRO (Qnil);
579 else if (CONSP (binding))
581 Lisp_Object key = XCAR (binding);
583 if (EQ (key, idx))
584 val = XCDR (binding);
585 else if (t_ok && EQ (key, Qt))
587 t_binding = XCDR (binding);
588 t_ok = 0;
591 else if (VECTORP (binding))
593 if (NATNUMP (idx) && XFASTINT (idx) < ASIZE (binding))
594 val = AREF (binding, XFASTINT (idx));
596 else if (CHAR_TABLE_P (binding))
598 /* Character codes with modifiers
599 are not included in a char-table.
600 All character codes without modifiers are included. */
601 if (NATNUMP (idx) && (XFASTINT (idx) & CHAR_MODIFIER_MASK) == 0)
603 val = Faref (binding, idx);
604 /* `nil' has a special meaning for char-tables, so
605 we use something else to record an explicitly
606 unbound entry. */
607 if (NILP (val))
608 val = Qunbound;
612 /* If we found a binding, clean it up and return it. */
613 if (!EQ (val, Qunbound))
615 if (EQ (val, Qt))
616 /* A Qt binding is just like an explicit nil binding
617 (i.e. it shadows any parent binding but not bindings in
618 keymaps of lower precedence). */
619 val = Qnil;
620 val = get_keyelt (val, autoload);
621 if (KEYMAPP (val))
622 fix_submap_inheritance (map, idx, val);
623 RETURN_UNGCPRO (val);
625 QUIT;
627 UNGCPRO;
628 return get_keyelt (t_binding, autoload);
632 static void
633 map_keymap_item (fun, args, key, val, data)
634 map_keymap_function_t fun;
635 Lisp_Object args, key, val;
636 void *data;
638 /* We should maybe try to detect bindings shadowed by previous
639 ones and things like that. */
640 if (EQ (val, Qt))
641 val = Qnil;
642 (*fun) (key, val, args, data);
645 static void
646 map_keymap_char_table_item (args, key, val)
647 Lisp_Object args, key, val;
649 if (!NILP (val))
651 map_keymap_function_t fun = XSAVE_VALUE (XCAR (args))->pointer;
652 args = XCDR (args);
653 /* If the key is a range, make a copy since map_char_table modifies
654 it in place. */
655 if (CONSP (key))
656 key = Fcons (XCAR (key), XCDR (key));
657 map_keymap_item (fun, XCDR (args), key, val,
658 XSAVE_VALUE (XCAR (args))->pointer);
662 /* Call FUN for every binding in MAP and stop at (and return) the parent.
663 FUN is called with 4 arguments: FUN (KEY, BINDING, ARGS, DATA). */
664 Lisp_Object
665 map_keymap_internal (Lisp_Object map,
666 map_keymap_function_t fun,
667 Lisp_Object args,
668 void *data)
670 struct gcpro gcpro1, gcpro2, gcpro3;
671 Lisp_Object tail
672 = (CONSP (map) && EQ (Qkeymap, XCAR (map))) ? XCDR (map) : map;
674 GCPRO3 (map, args, tail);
675 for (; CONSP (tail) && !EQ (Qkeymap, XCAR (tail)); tail = XCDR (tail))
677 Lisp_Object binding = XCAR (tail);
679 if (CONSP (binding))
680 map_keymap_item (fun, args, XCAR (binding), XCDR (binding), data);
681 else if (VECTORP (binding))
683 /* Loop over the char values represented in the vector. */
684 int len = ASIZE (binding);
685 int c;
686 for (c = 0; c < len; c++)
688 Lisp_Object character;
689 XSETFASTINT (character, c);
690 map_keymap_item (fun, args, character, AREF (binding, c), data);
693 else if (CHAR_TABLE_P (binding))
695 map_char_table (map_keymap_char_table_item, Qnil, binding,
696 Fcons (make_save_value (fun, 0),
697 Fcons (make_save_value (data, 0),
698 args)));
701 UNGCPRO;
702 return tail;
705 static void
706 map_keymap_call (key, val, fun, dummy)
707 Lisp_Object key, val, fun;
708 void *dummy;
710 call2 (fun, key, val);
713 /* Same as map_keymap_internal, but doesn't traverses parent keymaps as well.
714 A non-zero AUTOLOAD indicates that autoloaded keymaps should be loaded. */
715 void
716 map_keymap (map, fun, args, data, autoload)
717 map_keymap_function_t fun;
718 Lisp_Object map, args;
719 void *data;
720 int autoload;
722 struct gcpro gcpro1;
723 GCPRO1 (args);
724 map = get_keymap (map, 1, autoload);
725 while (CONSP (map))
727 map = map_keymap_internal (map, fun, args, data);
728 map = get_keymap (map, 0, autoload);
730 UNGCPRO;
733 Lisp_Object Qkeymap_canonicalize;
735 /* Same as map_keymap, but does it right, properly eliminating duplicate
736 bindings due to inheritance. */
737 void
738 map_keymap_canonical (map, fun, args, data)
739 map_keymap_function_t fun;
740 Lisp_Object map, args;
741 void *data;
743 struct gcpro gcpro1;
744 GCPRO1 (args);
745 /* map_keymap_canonical may be used from redisplay (e.g. when building menus)
746 so be careful to ignore errors and to inhibit redisplay. */
747 map = safe_call1 (Qkeymap_canonicalize, map);
748 /* No need to use `map_keymap' here because canonical map has no parent. */
749 map_keymap_internal (map, fun, args, data);
750 UNGCPRO;
753 DEFUN ("map-keymap-internal", Fmap_keymap_internal, Smap_keymap_internal, 2, 2, 0,
754 doc: /* Call FUNCTION once for each event binding in KEYMAP.
755 FUNCTION is called with two arguments: the event that is bound, and
756 the definition it is bound to. The event may be a character range.
757 If KEYMAP has a parent, this function returns it without processing it. */)
758 (function, keymap)
759 Lisp_Object function, keymap;
761 struct gcpro gcpro1;
762 GCPRO1 (function);
763 keymap = get_keymap (keymap, 1, 1);
764 keymap = map_keymap_internal (keymap, map_keymap_call, function, NULL);
765 UNGCPRO;
766 return keymap;
769 DEFUN ("map-keymap", Fmap_keymap, Smap_keymap, 2, 3, 0,
770 doc: /* Call FUNCTION once for each event binding in KEYMAP.
771 FUNCTION is called with two arguments: the event that is bound, and
772 the definition it is bound to. The event may be a character range.
774 If KEYMAP has a parent, the parent's bindings are included as well.
775 This works recursively: if the parent has itself a parent, then the
776 grandparent's bindings are also included and so on.
777 usage: (map-keymap FUNCTION KEYMAP) */)
778 (function, keymap, sort_first)
779 Lisp_Object function, keymap, sort_first;
781 if (! NILP (sort_first))
782 return call2 (intern ("map-keymap-sorted"), function, keymap);
784 map_keymap (keymap, map_keymap_call, function, NULL, 1);
785 return Qnil;
788 /* Given OBJECT which was found in a slot in a keymap,
789 trace indirect definitions to get the actual definition of that slot.
790 An indirect definition is a list of the form
791 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
792 and INDEX is the object to look up in KEYMAP to yield the definition.
794 Also if OBJECT has a menu string as the first element,
795 remove that. Also remove a menu help string as second element.
797 If AUTOLOAD is nonzero, load autoloadable keymaps
798 that are referred to with indirection.
800 This can GC because menu_item_eval_property calls Feval. */
802 Lisp_Object
803 get_keyelt (object, autoload)
804 Lisp_Object object;
805 int autoload;
807 while (1)
809 if (!(CONSP (object)))
810 /* This is really the value. */
811 return object;
813 /* If the keymap contents looks like (keymap ...) or (lambda ...)
814 then use itself. */
815 else if (EQ (XCAR (object), Qkeymap) || EQ (XCAR (object), Qlambda))
816 return object;
818 /* If the keymap contents looks like (menu-item name . DEFN)
819 or (menu-item name DEFN ...) then use DEFN.
820 This is a new format menu item. */
821 else if (EQ (XCAR (object), Qmenu_item))
823 if (CONSP (XCDR (object)))
825 Lisp_Object tem;
827 object = XCDR (XCDR (object));
828 tem = object;
829 if (CONSP (object))
830 object = XCAR (object);
832 /* If there's a `:filter FILTER', apply FILTER to the
833 menu-item's definition to get the real definition to
834 use. */
835 for (; CONSP (tem) && CONSP (XCDR (tem)); tem = XCDR (tem))
836 if (EQ (XCAR (tem), QCfilter) && autoload)
838 Lisp_Object filter;
839 filter = XCAR (XCDR (tem));
840 filter = list2 (filter, list2 (Qquote, object));
841 object = menu_item_eval_property (filter);
842 break;
845 else
846 /* Invalid keymap. */
847 return object;
850 /* If the keymap contents looks like (STRING . DEFN), use DEFN.
851 Keymap alist elements like (CHAR MENUSTRING . DEFN)
852 will be used by HierarKey menus. */
853 else if (STRINGP (XCAR (object)))
855 object = XCDR (object);
856 /* Also remove a menu help string, if any,
857 following the menu item name. */
858 if (CONSP (object) && STRINGP (XCAR (object)))
859 object = XCDR (object);
860 /* Also remove the sublist that caches key equivalences, if any. */
861 if (CONSP (object) && CONSP (XCAR (object)))
863 Lisp_Object carcar;
864 carcar = XCAR (XCAR (object));
865 if (NILP (carcar) || VECTORP (carcar))
866 object = XCDR (object);
870 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
871 else
873 struct gcpro gcpro1;
874 Lisp_Object map;
875 GCPRO1 (object);
876 map = get_keymap (Fcar_safe (object), 0, autoload);
877 UNGCPRO;
878 return (!CONSP (map) ? object /* Invalid keymap */
879 : access_keymap (map, Fcdr (object), 0, 0, autoload));
884 static Lisp_Object
885 store_in_keymap (keymap, idx, def)
886 Lisp_Object keymap;
887 register Lisp_Object idx;
888 Lisp_Object def;
890 /* Flush any reverse-map cache. */
891 where_is_cache = Qnil;
892 where_is_cache_keymaps = Qt;
894 /* If we are preparing to dump, and DEF is a menu element
895 with a menu item indicator, copy it to ensure it is not pure. */
896 if (CONSP (def) && PURE_P (def)
897 && (EQ (XCAR (def), Qmenu_item) || STRINGP (XCAR (def))))
898 def = Fcons (XCAR (def), XCDR (def));
900 if (!CONSP (keymap) || !EQ (XCAR (keymap), Qkeymap))
901 error ("attempt to define a key in a non-keymap");
903 /* If idx is a cons, and the car part is a character, idx must be of
904 the form (FROM-CHAR . TO-CHAR). */
905 if (CONSP (idx) && CHARACTERP (XCAR (idx)))
906 CHECK_CHARACTER_CDR (idx);
907 else
908 /* If idx is a list (some sort of mouse click, perhaps?),
909 the index we want to use is the car of the list, which
910 ought to be a symbol. */
911 idx = EVENT_HEAD (idx);
913 /* If idx is a symbol, it might have modifiers, which need to
914 be put in the canonical order. */
915 if (SYMBOLP (idx))
916 idx = reorder_modifiers (idx);
917 else if (INTEGERP (idx))
918 /* Clobber the high bits that can be present on a machine
919 with more than 24 bits of integer. */
920 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
922 /* Scan the keymap for a binding of idx. */
924 Lisp_Object tail;
926 /* The cons after which we should insert new bindings. If the
927 keymap has a table element, we record its position here, so new
928 bindings will go after it; this way, the table will stay
929 towards the front of the alist and character lookups in dense
930 keymaps will remain fast. Otherwise, this just points at the
931 front of the keymap. */
932 Lisp_Object insertion_point;
934 insertion_point = keymap;
935 for (tail = XCDR (keymap); CONSP (tail); tail = XCDR (tail))
937 Lisp_Object elt;
939 elt = XCAR (tail);
940 if (VECTORP (elt))
942 if (NATNUMP (idx) && XFASTINT (idx) < ASIZE (elt))
944 CHECK_IMPURE (elt);
945 ASET (elt, XFASTINT (idx), def);
946 return def;
948 else if (CONSP (idx) && CHARACTERP (XCAR (idx)))
950 int from = XFASTINT (XCAR (idx));
951 int to = XFASTINT (XCDR (idx));
953 if (to >= ASIZE (elt))
954 to = ASIZE (elt) - 1;
955 for (; from <= to; from++)
956 ASET (elt, from, def);
957 if (to == XFASTINT (XCDR (idx)))
958 /* We have defined all keys in IDX. */
959 return def;
961 insertion_point = tail;
963 else if (CHAR_TABLE_P (elt))
965 /* Character codes with modifiers
966 are not included in a char-table.
967 All character codes without modifiers are included. */
968 if (NATNUMP (idx) && !(XFASTINT (idx) & CHAR_MODIFIER_MASK))
970 Faset (elt, idx,
971 /* `nil' has a special meaning for char-tables, so
972 we use something else to record an explicitly
973 unbound entry. */
974 NILP (def) ? Qt : def);
975 return def;
977 else if (CONSP (idx) && CHARACTERP (XCAR (idx)))
979 Fset_char_table_range (elt, idx, NILP (def) ? Qt : def);
980 return def;
982 insertion_point = tail;
984 else if (CONSP (elt))
986 if (EQ (idx, XCAR (elt)))
988 CHECK_IMPURE (elt);
989 XSETCDR (elt, def);
990 return def;
992 else if (CONSP (idx) && CHARACTERP (XCAR (idx)))
994 int from = XFASTINT (XCAR (idx));
995 int to = XFASTINT (XCDR (idx));
997 if (from <= XFASTINT (XCAR (elt))
998 && to >= XFASTINT (XCAR (elt)))
1000 XSETCDR (elt, def);
1001 if (from == to)
1002 return def;
1006 else if (EQ (elt, Qkeymap))
1007 /* If we find a 'keymap' symbol in the spine of KEYMAP,
1008 then we must have found the start of a second keymap
1009 being used as the tail of KEYMAP, and a binding for IDX
1010 should be inserted before it. */
1011 goto keymap_end;
1013 QUIT;
1016 keymap_end:
1017 /* We have scanned the entire keymap, and not found a binding for
1018 IDX. Let's add one. */
1020 Lisp_Object elt;
1022 if (CONSP (idx) && CHARACTERP (XCAR (idx)))
1024 /* IDX specifies a range of characters, and not all of them
1025 were handled yet, which means this keymap doesn't have a
1026 char-table. So, we insert a char-table now. */
1027 elt = Fmake_char_table (Qkeymap, Qnil);
1028 Fset_char_table_range (elt, idx, NILP (def) ? Qt : def);
1030 else
1031 elt = Fcons (idx, def);
1032 CHECK_IMPURE (insertion_point);
1033 XSETCDR (insertion_point, Fcons (elt, XCDR (insertion_point)));
1037 return def;
1040 EXFUN (Fcopy_keymap, 1);
1042 Lisp_Object
1043 copy_keymap_item (elt)
1044 Lisp_Object elt;
1046 Lisp_Object res, tem;
1048 if (!CONSP (elt))
1049 return elt;
1051 res = tem = elt;
1053 /* Is this a new format menu item. */
1054 if (EQ (XCAR (tem), Qmenu_item))
1056 /* Copy cell with menu-item marker. */
1057 res = elt = Fcons (XCAR (tem), XCDR (tem));
1058 tem = XCDR (elt);
1059 if (CONSP (tem))
1061 /* Copy cell with menu-item name. */
1062 XSETCDR (elt, Fcons (XCAR (tem), XCDR (tem)));
1063 elt = XCDR (elt);
1064 tem = XCDR (elt);
1066 if (CONSP (tem))
1068 /* Copy cell with binding and if the binding is a keymap,
1069 copy that. */
1070 XSETCDR (elt, Fcons (XCAR (tem), XCDR (tem)));
1071 elt = XCDR (elt);
1072 tem = XCAR (elt);
1073 if (CONSP (tem) && EQ (XCAR (tem), Qkeymap))
1074 XSETCAR (elt, Fcopy_keymap (tem));
1075 tem = XCDR (elt);
1076 if (CONSP (tem) && CONSP (XCAR (tem)))
1077 /* Delete cache for key equivalences. */
1078 XSETCDR (elt, XCDR (tem));
1081 else
1083 /* It may be an old fomat menu item.
1084 Skip the optional menu string. */
1085 if (STRINGP (XCAR (tem)))
1087 /* Copy the cell, since copy-alist didn't go this deep. */
1088 res = elt = Fcons (XCAR (tem), XCDR (tem));
1089 tem = XCDR (elt);
1090 /* Also skip the optional menu help string. */
1091 if (CONSP (tem) && STRINGP (XCAR (tem)))
1093 XSETCDR (elt, Fcons (XCAR (tem), XCDR (tem)));
1094 elt = XCDR (elt);
1095 tem = XCDR (elt);
1097 /* There may also be a list that caches key equivalences.
1098 Just delete it for the new keymap. */
1099 if (CONSP (tem)
1100 && CONSP (XCAR (tem))
1101 && (NILP (XCAR (XCAR (tem)))
1102 || VECTORP (XCAR (XCAR (tem)))))
1104 XSETCDR (elt, XCDR (tem));
1105 tem = XCDR (tem);
1107 if (CONSP (tem) && EQ (XCAR (tem), Qkeymap))
1108 XSETCDR (elt, Fcopy_keymap (tem));
1110 else if (EQ (XCAR (tem), Qkeymap))
1111 res = Fcopy_keymap (elt);
1113 return res;
1116 static void
1117 copy_keymap_1 (chartable, idx, elt)
1118 Lisp_Object chartable, idx, elt;
1120 Fset_char_table_range (chartable, idx, copy_keymap_item (elt));
1123 DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0,
1124 doc: /* Return a copy of the keymap KEYMAP.
1125 The copy starts out with the same definitions of KEYMAP,
1126 but changing either the copy or KEYMAP does not affect the other.
1127 Any key definitions that are subkeymaps are recursively copied.
1128 However, a key definition which is a symbol whose definition is a keymap
1129 is not copied. */)
1130 (keymap)
1131 Lisp_Object keymap;
1133 register Lisp_Object copy, tail;
1134 keymap = get_keymap (keymap, 1, 0);
1135 copy = tail = Fcons (Qkeymap, Qnil);
1136 keymap = XCDR (keymap); /* Skip the `keymap' symbol. */
1138 while (CONSP (keymap) && !EQ (XCAR (keymap), Qkeymap))
1140 Lisp_Object elt = XCAR (keymap);
1141 if (CHAR_TABLE_P (elt))
1143 elt = Fcopy_sequence (elt);
1144 map_char_table (copy_keymap_1, Qnil, elt, elt);
1146 else if (VECTORP (elt))
1148 int i;
1149 elt = Fcopy_sequence (elt);
1150 for (i = 0; i < ASIZE (elt); i++)
1151 ASET (elt, i, copy_keymap_item (AREF (elt, i)));
1153 else if (CONSP (elt))
1154 elt = Fcons (XCAR (elt), copy_keymap_item (XCDR (elt)));
1155 XSETCDR (tail, Fcons (elt, Qnil));
1156 tail = XCDR (tail);
1157 keymap = XCDR (keymap);
1159 XSETCDR (tail, keymap);
1160 return copy;
1163 /* Simple Keymap mutators and accessors. */
1165 /* GC is possible in this function if it autoloads a keymap. */
1167 DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
1168 doc: /* In KEYMAP, define key sequence KEY as DEF.
1169 KEYMAP is a keymap.
1171 KEY is a string or a vector of symbols and characters meaning a
1172 sequence of keystrokes and events. Non-ASCII characters with codes
1173 above 127 (such as ISO Latin-1) can be included if you use a vector.
1174 Using [t] for KEY creates a default definition, which applies to any
1175 event type that has no other definition in this keymap.
1177 DEF is anything that can be a key's definition:
1178 nil (means key is undefined in this keymap),
1179 a command (a Lisp function suitable for interactive calling),
1180 a string (treated as a keyboard macro),
1181 a keymap (to define a prefix key),
1182 a symbol (when the key is looked up, the symbol will stand for its
1183 function definition, which should at that time be one of the above,
1184 or another symbol whose function definition is used, etc.),
1185 a cons (STRING . DEFN), meaning that DEFN is the definition
1186 (DEFN should be a valid definition in its own right),
1187 or a cons (MAP . CHAR), meaning use definition of CHAR in keymap MAP,
1188 or an extended menu item definition.
1189 (See info node `(elisp)Extended Menu Items'.)
1191 If KEYMAP is a sparse keymap with a binding for KEY, the existing
1192 binding is altered. If there is no binding for KEY, the new pair
1193 binding KEY to DEF is added at the front of KEYMAP. */)
1194 (keymap, key, def)
1195 Lisp_Object keymap;
1196 Lisp_Object key;
1197 Lisp_Object def;
1199 register int idx;
1200 register Lisp_Object c;
1201 register Lisp_Object cmd;
1202 int metized = 0;
1203 int meta_bit;
1204 int length;
1205 struct gcpro gcpro1, gcpro2, gcpro3;
1207 GCPRO3 (keymap, key, def);
1208 keymap = get_keymap (keymap, 1, 1);
1210 CHECK_VECTOR_OR_STRING (key);
1212 length = XFASTINT (Flength (key));
1213 if (length == 0)
1214 RETURN_UNGCPRO (Qnil);
1216 if (SYMBOLP (def) && !EQ (Vdefine_key_rebound_commands, Qt))
1217 Vdefine_key_rebound_commands = Fcons (def, Vdefine_key_rebound_commands);
1219 meta_bit = (VECTORP (key) || (STRINGP (key) && STRING_MULTIBYTE (key))
1220 ? meta_modifier : 0x80);
1222 if (VECTORP (def) && ASIZE (def) > 0 && CONSP (AREF (def, 0)))
1223 { /* DEF is apparently an XEmacs-style keyboard macro. */
1224 Lisp_Object tmp = Fmake_vector (make_number (ASIZE (def)), Qnil);
1225 int i = ASIZE (def);
1226 while (--i >= 0)
1228 Lisp_Object c = AREF (def, i);
1229 if (CONSP (c) && lucid_event_type_list_p (c))
1230 c = Fevent_convert_list (c);
1231 ASET (tmp, i, c);
1233 def = tmp;
1236 idx = 0;
1237 while (1)
1239 c = Faref (key, make_number (idx));
1241 if (CONSP (c))
1243 /* C may be a Lucid style event type list or a cons (FROM .
1244 TO) specifying a range of characters. */
1245 if (lucid_event_type_list_p (c))
1246 c = Fevent_convert_list (c);
1247 else if (CHARACTERP (XCAR (c)))
1248 CHECK_CHARACTER_CDR (c);
1251 if (SYMBOLP (c))
1252 silly_event_symbol_error (c);
1254 if (INTEGERP (c)
1255 && (XINT (c) & meta_bit)
1256 && !metized)
1258 c = meta_prefix_char;
1259 metized = 1;
1261 else
1263 if (INTEGERP (c))
1264 XSETINT (c, XINT (c) & ~meta_bit);
1266 metized = 0;
1267 idx++;
1270 if (!INTEGERP (c) && !SYMBOLP (c)
1271 && (!CONSP (c)
1272 /* If C is a range, it must be a leaf. */
1273 || (INTEGERP (XCAR (c)) && idx != length)))
1274 error ("Key sequence contains invalid event");
1276 if (idx == length)
1277 RETURN_UNGCPRO (store_in_keymap (keymap, c, def));
1279 cmd = access_keymap (keymap, c, 0, 1, 1);
1281 /* If this key is undefined, make it a prefix. */
1282 if (NILP (cmd))
1283 cmd = define_as_prefix (keymap, c);
1285 keymap = get_keymap (cmd, 0, 1);
1286 if (!CONSP (keymap))
1287 /* We must use Fkey_description rather than just passing key to
1288 error; key might be a vector, not a string. */
1289 error ("Key sequence %s starts with non-prefix key %s",
1290 SDATA (Fkey_description (key, Qnil)),
1291 SDATA (Fkey_description (Fsubstring (key, make_number (0),
1292 make_number (idx)),
1293 Qnil)));
1297 /* This function may GC (it calls Fkey_binding). */
1299 DEFUN ("command-remapping", Fcommand_remapping, Scommand_remapping, 1, 3, 0,
1300 doc: /* Return the remapping for command COMMAND.
1301 Returns nil if COMMAND is not remapped (or not a symbol).
1303 If the optional argument POSITION is non-nil, it specifies a mouse
1304 position as returned by `event-start' and `event-end', and the
1305 remapping occurs in the keymaps associated with it. It can also be a
1306 number or marker, in which case the keymap properties at the specified
1307 buffer position instead of point are used. The KEYMAPS argument is
1308 ignored if POSITION is non-nil.
1310 If the optional argument KEYMAPS is non-nil, it should be a list of
1311 keymaps to search for command remapping. Otherwise, search for the
1312 remapping in all currently active keymaps. */)
1313 (command, position, keymaps)
1314 Lisp_Object command, position, keymaps;
1316 if (!SYMBOLP (command))
1317 return Qnil;
1319 ASET (command_remapping_vector, 1, command);
1321 if (NILP (keymaps))
1322 return Fkey_binding (command_remapping_vector, Qnil, Qt, position);
1323 else
1325 Lisp_Object maps, binding;
1327 for (maps = keymaps; CONSP (maps); maps = XCDR (maps))
1329 binding = Flookup_key (XCAR (maps), command_remapping_vector, Qnil);
1330 if (!NILP (binding) && !INTEGERP (binding))
1331 return binding;
1333 return Qnil;
1337 /* Value is number if KEY is too long; nil if valid but has no definition. */
1338 /* GC is possible in this function if it autoloads a keymap. */
1340 DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0,
1341 doc: /* In keymap KEYMAP, look up key sequence KEY. Return the definition.
1342 A value of nil means undefined. See doc of `define-key'
1343 for kinds of definitions.
1345 A number as value means KEY is "too long";
1346 that is, characters or symbols in it except for the last one
1347 fail to be a valid sequence of prefix characters in KEYMAP.
1348 The number is how many characters at the front of KEY
1349 it takes to reach a non-prefix key.
1351 Normally, `lookup-key' ignores bindings for t, which act as default
1352 bindings, used when nothing else in the keymap applies; this makes it
1353 usable as a general function for probing keymaps. However, if the
1354 third optional argument ACCEPT-DEFAULT is non-nil, `lookup-key' will
1355 recognize the default bindings, just as `read-key-sequence' does. */)
1356 (keymap, key, accept_default)
1357 Lisp_Object keymap;
1358 Lisp_Object key;
1359 Lisp_Object accept_default;
1361 register int idx;
1362 register Lisp_Object cmd;
1363 register Lisp_Object c;
1364 int length;
1365 int t_ok = !NILP (accept_default);
1366 struct gcpro gcpro1, gcpro2;
1368 GCPRO2 (keymap, key);
1369 keymap = get_keymap (keymap, 1, 1);
1371 CHECK_VECTOR_OR_STRING (key);
1373 length = XFASTINT (Flength (key));
1374 if (length == 0)
1375 RETURN_UNGCPRO (keymap);
1377 idx = 0;
1378 while (1)
1380 c = Faref (key, make_number (idx++));
1382 if (CONSP (c) && lucid_event_type_list_p (c))
1383 c = Fevent_convert_list (c);
1385 /* Turn the 8th bit of string chars into a meta modifier. */
1386 if (STRINGP (key) && XINT (c) & 0x80 && !STRING_MULTIBYTE (key))
1387 XSETINT (c, (XINT (c) | meta_modifier) & ~0x80);
1389 /* Allow string since binding for `menu-bar-select-buffer'
1390 includes the buffer name in the key sequence. */
1391 if (!INTEGERP (c) && !SYMBOLP (c) && !CONSP (c) && !STRINGP (c))
1392 error ("Key sequence contains invalid event");
1394 cmd = access_keymap (keymap, c, t_ok, 0, 1);
1395 if (idx == length)
1396 RETURN_UNGCPRO (cmd);
1398 keymap = get_keymap (cmd, 0, 1);
1399 if (!CONSP (keymap))
1400 RETURN_UNGCPRO (make_number (idx));
1402 QUIT;
1406 /* Make KEYMAP define event C as a keymap (i.e., as a prefix).
1407 Assume that currently it does not define C at all.
1408 Return the keymap. */
1410 static Lisp_Object
1411 define_as_prefix (keymap, c)
1412 Lisp_Object keymap, c;
1414 Lisp_Object cmd;
1416 cmd = Fmake_sparse_keymap (Qnil);
1417 /* If this key is defined as a prefix in an inherited keymap,
1418 make it a prefix in this map, and make its definition
1419 inherit the other prefix definition. */
1420 cmd = nconc2 (cmd, access_keymap (keymap, c, 0, 0, 0));
1421 store_in_keymap (keymap, c, cmd);
1423 return cmd;
1426 /* Append a key to the end of a key sequence. We always make a vector. */
1428 Lisp_Object
1429 append_key (key_sequence, key)
1430 Lisp_Object key_sequence, key;
1432 Lisp_Object args[2];
1434 args[0] = key_sequence;
1436 args[1] = Fcons (key, Qnil);
1437 return Fvconcat (2, args);
1440 /* Given a event type C which is a symbol,
1441 signal an error if is a mistake such as RET or M-RET or C-DEL, etc. */
1443 static void
1444 silly_event_symbol_error (c)
1445 Lisp_Object c;
1447 Lisp_Object parsed, base, name, assoc;
1448 int modifiers;
1450 parsed = parse_modifiers (c);
1451 modifiers = (int) XUINT (XCAR (XCDR (parsed)));
1452 base = XCAR (parsed);
1453 name = Fsymbol_name (base);
1454 /* This alist includes elements such as ("RET" . "\\r"). */
1455 assoc = Fassoc (name, exclude_keys);
1457 if (! NILP (assoc))
1459 char new_mods[sizeof ("\\A-\\C-\\H-\\M-\\S-\\s-")];
1460 char *p = new_mods;
1461 Lisp_Object keystring;
1462 if (modifiers & alt_modifier)
1463 { *p++ = '\\'; *p++ = 'A'; *p++ = '-'; }
1464 if (modifiers & ctrl_modifier)
1465 { *p++ = '\\'; *p++ = 'C'; *p++ = '-'; }
1466 if (modifiers & hyper_modifier)
1467 { *p++ = '\\'; *p++ = 'H'; *p++ = '-'; }
1468 if (modifiers & meta_modifier)
1469 { *p++ = '\\'; *p++ = 'M'; *p++ = '-'; }
1470 if (modifiers & shift_modifier)
1471 { *p++ = '\\'; *p++ = 'S'; *p++ = '-'; }
1472 if (modifiers & super_modifier)
1473 { *p++ = '\\'; *p++ = 's'; *p++ = '-'; }
1474 *p = 0;
1476 c = reorder_modifiers (c);
1477 keystring = concat2 (build_string (new_mods), XCDR (assoc));
1479 error ((modifiers & ~meta_modifier
1480 ? "To bind the key %s, use [?%s], not [%s]"
1481 : "To bind the key %s, use \"%s\", not [%s]"),
1482 SDATA (SYMBOL_NAME (c)), SDATA (keystring),
1483 SDATA (SYMBOL_NAME (c)));
1487 /* Global, local, and minor mode keymap stuff. */
1489 /* We can't put these variables inside current_minor_maps, since under
1490 some systems, static gets macro-defined to be the empty string.
1491 Ickypoo. */
1492 static Lisp_Object *cmm_modes = NULL, *cmm_maps = NULL;
1493 static int cmm_size = 0;
1495 /* Store a pointer to an array of the currently active minor modes in
1496 *modeptr, a pointer to an array of the keymaps of the currently
1497 active minor modes in *mapptr, and return the number of maps
1498 *mapptr contains.
1500 This function always returns a pointer to the same buffer, and may
1501 free or reallocate it, so if you want to keep it for a long time or
1502 hand it out to lisp code, copy it. This procedure will be called
1503 for every key sequence read, so the nice lispy approach (return a
1504 new assoclist, list, what have you) for each invocation would
1505 result in a lot of consing over time.
1507 If we used xrealloc/xmalloc and ran out of memory, they would throw
1508 back to the command loop, which would try to read a key sequence,
1509 which would call this function again, resulting in an infinite
1510 loop. Instead, we'll use realloc/malloc and silently truncate the
1511 list, let the key sequence be read, and hope some other piece of
1512 code signals the error. */
1514 current_minor_maps (modeptr, mapptr)
1515 Lisp_Object **modeptr, **mapptr;
1517 int i = 0;
1518 int list_number = 0;
1519 Lisp_Object alist, assoc, var, val;
1520 Lisp_Object emulation_alists;
1521 Lisp_Object lists[2];
1523 emulation_alists = Vemulation_mode_map_alists;
1524 lists[0] = Vminor_mode_overriding_map_alist;
1525 lists[1] = Vminor_mode_map_alist;
1527 for (list_number = 0; list_number < 2; list_number++)
1529 if (CONSP (emulation_alists))
1531 alist = XCAR (emulation_alists);
1532 emulation_alists = XCDR (emulation_alists);
1533 if (SYMBOLP (alist))
1534 alist = find_symbol_value (alist);
1535 list_number = -1;
1537 else
1538 alist = lists[list_number];
1540 for ( ; CONSP (alist); alist = XCDR (alist))
1541 if ((assoc = XCAR (alist), CONSP (assoc))
1542 && (var = XCAR (assoc), SYMBOLP (var))
1543 && (val = find_symbol_value (var), !EQ (val, Qunbound))
1544 && !NILP (val))
1546 Lisp_Object temp;
1548 /* If a variable has an entry in Vminor_mode_overriding_map_alist,
1549 and also an entry in Vminor_mode_map_alist,
1550 ignore the latter. */
1551 if (list_number == 1)
1553 val = assq_no_quit (var, lists[0]);
1554 if (!NILP (val))
1555 continue;
1558 if (i >= cmm_size)
1560 int newsize, allocsize;
1561 Lisp_Object *newmodes, *newmaps;
1563 newsize = cmm_size == 0 ? 30 : cmm_size * 2;
1564 allocsize = newsize * sizeof *newmodes;
1566 /* Use malloc here. See the comment above this function.
1567 Avoid realloc here; it causes spurious traps on GNU/Linux [KFS] */
1568 BLOCK_INPUT;
1569 newmodes = (Lisp_Object *) malloc (allocsize);
1570 if (newmodes)
1572 if (cmm_modes)
1574 bcopy (cmm_modes, newmodes, cmm_size * sizeof cmm_modes[0]);
1575 free (cmm_modes);
1577 cmm_modes = newmodes;
1580 newmaps = (Lisp_Object *) malloc (allocsize);
1581 if (newmaps)
1583 if (cmm_maps)
1585 bcopy (cmm_maps, newmaps, cmm_size * sizeof cmm_maps[0]);
1586 free (cmm_maps);
1588 cmm_maps = newmaps;
1590 UNBLOCK_INPUT;
1592 if (newmodes == NULL || newmaps == NULL)
1593 break;
1594 cmm_size = newsize;
1597 /* Get the keymap definition--or nil if it is not defined. */
1598 temp = Findirect_function (XCDR (assoc), Qt);
1599 if (!NILP (temp))
1601 cmm_modes[i] = var;
1602 cmm_maps [i] = temp;
1603 i++;
1608 if (modeptr) *modeptr = cmm_modes;
1609 if (mapptr) *mapptr = cmm_maps;
1610 return i;
1613 DEFUN ("current-active-maps", Fcurrent_active_maps, Scurrent_active_maps,
1614 0, 2, 0,
1615 doc: /* Return a list of the currently active keymaps.
1616 OLP if non-nil indicates that we should obey `overriding-local-map' and
1617 `overriding-terminal-local-map'. POSITION can specify a click position
1618 like in the respective argument of `key-binding'. */)
1619 (olp, position)
1620 Lisp_Object olp, position;
1622 int count = SPECPDL_INDEX ();
1624 Lisp_Object keymaps;
1626 /* If a mouse click position is given, our variables are based on
1627 the buffer clicked on, not the current buffer. So we may have to
1628 switch the buffer here. */
1630 if (CONSP (position))
1632 Lisp_Object window;
1634 window = POSN_WINDOW (position);
1636 if (WINDOWP (window)
1637 && BUFFERP (XWINDOW (window)->buffer)
1638 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
1640 /* Arrange to go back to the original buffer once we're done
1641 processing the key sequence. We don't use
1642 save_excursion_{save,restore} here, in analogy to
1643 `read-key-sequence' to avoid saving point. Maybe this
1644 would not be a problem here, but it is easier to keep
1645 things the same.
1648 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
1650 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
1654 keymaps = Fcons (current_global_map, Qnil);
1656 if (!NILP (olp))
1658 if (!NILP (current_kboard->Voverriding_terminal_local_map))
1659 keymaps = Fcons (current_kboard->Voverriding_terminal_local_map, keymaps);
1660 /* The doc said that overriding-terminal-local-map should
1661 override overriding-local-map. The code used them both,
1662 but it seems clearer to use just one. rms, jan 2005. */
1663 else if (!NILP (Voverriding_local_map))
1664 keymaps = Fcons (Voverriding_local_map, keymaps);
1666 if (NILP (XCDR (keymaps)))
1668 Lisp_Object *maps;
1669 int nmaps, i;
1671 Lisp_Object keymap, local_map;
1672 EMACS_INT pt;
1674 pt = INTEGERP (position) ? XINT (position)
1675 : MARKERP (position) ? marker_position (position)
1676 : PT;
1678 /* Get the buffer local maps, possibly overriden by text or
1679 overlay properties */
1681 local_map = get_local_map (pt, current_buffer, Qlocal_map);
1682 keymap = get_local_map (pt, current_buffer, Qkeymap);
1684 if (CONSP (position))
1686 Lisp_Object string;
1688 /* For a mouse click, get the local text-property keymap
1689 of the place clicked on, rather than point. */
1691 if (POSN_INBUFFER_P (position))
1693 Lisp_Object pos;
1695 pos = POSN_BUFFER_POSN (position);
1696 if (INTEGERP (pos)
1697 && XINT (pos) >= BEG && XINT (pos) <= Z)
1699 local_map = get_local_map (XINT (pos),
1700 current_buffer, Qlocal_map);
1702 keymap = get_local_map (XINT (pos),
1703 current_buffer, Qkeymap);
1707 /* If on a mode line string with a local keymap,
1708 or for a click on a string, i.e. overlay string or a
1709 string displayed via the `display' property,
1710 consider `local-map' and `keymap' properties of
1711 that string. */
1713 if (string = POSN_STRING (position),
1714 (CONSP (string) && STRINGP (XCAR (string))))
1716 Lisp_Object pos, map;
1718 pos = XCDR (string);
1719 string = XCAR (string);
1720 if (INTEGERP (pos)
1721 && XINT (pos) >= 0
1722 && XINT (pos) < SCHARS (string))
1724 map = Fget_text_property (pos, Qlocal_map, string);
1725 if (!NILP (map))
1726 local_map = map;
1728 map = Fget_text_property (pos, Qkeymap, string);
1729 if (!NILP (map))
1730 keymap = map;
1736 if (!NILP (local_map))
1737 keymaps = Fcons (local_map, keymaps);
1739 /* Now put all the minor mode keymaps on the list. */
1740 nmaps = current_minor_maps (0, &maps);
1742 for (i = --nmaps; i >= 0; i--)
1743 if (!NILP (maps[i]))
1744 keymaps = Fcons (maps[i], keymaps);
1746 if (!NILP (keymap))
1747 keymaps = Fcons (keymap, keymaps);
1750 unbind_to (count, Qnil);
1752 return keymaps;
1755 /* GC is possible in this function if it autoloads a keymap. */
1757 DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 4, 0,
1758 doc: /* Return the binding for command KEY in current keymaps.
1759 KEY is a string or vector, a sequence of keystrokes.
1760 The binding is probably a symbol with a function definition.
1762 Normally, `key-binding' ignores bindings for t, which act as default
1763 bindings, used when nothing else in the keymap applies; this makes it
1764 usable as a general function for probing keymaps. However, if the
1765 optional second argument ACCEPT-DEFAULT is non-nil, `key-binding' does
1766 recognize the default bindings, just as `read-key-sequence' does.
1768 Like the normal command loop, `key-binding' will remap the command
1769 resulting from looking up KEY by looking up the command in the
1770 current keymaps. However, if the optional third argument NO-REMAP
1771 is non-nil, `key-binding' returns the unmapped command.
1773 If KEY is a key sequence initiated with the mouse, the used keymaps
1774 will depend on the clicked mouse position with regard to the buffer
1775 and possible local keymaps on strings.
1777 If the optional argument POSITION is non-nil, it specifies a mouse
1778 position as returned by `event-start' and `event-end', and the lookup
1779 occurs in the keymaps associated with it instead of KEY. It can also
1780 be a number or marker, in which case the keymap properties at the
1781 specified buffer position instead of point are used.
1783 (key, accept_default, no_remap, position)
1784 Lisp_Object key, accept_default, no_remap, position;
1786 Lisp_Object *maps, value;
1787 int nmaps, i;
1788 struct gcpro gcpro1, gcpro2;
1789 int count = SPECPDL_INDEX ();
1791 GCPRO2 (key, position);
1793 if (NILP (position) && VECTORP (key))
1795 Lisp_Object event
1796 /* mouse events may have a symbolic prefix indicating the
1797 scrollbar or mode line */
1798 = AREF (key, SYMBOLP (AREF (key, 0)) && ASIZE (key) > 1 ? 1 : 0);
1800 /* We are not interested in locations without event data */
1802 if (EVENT_HAS_PARAMETERS (event) && CONSP (XCDR (event)))
1804 Lisp_Object kind = EVENT_HEAD_KIND (EVENT_HEAD (event));
1805 if (EQ (kind, Qmouse_click))
1806 position = EVENT_START (event);
1810 /* Key sequences beginning with mouse clicks
1811 are read using the keymaps of the buffer clicked on, not
1812 the current buffer. So we may have to switch the buffer
1813 here. */
1815 if (CONSP (position))
1817 Lisp_Object window;
1819 window = POSN_WINDOW (position);
1821 if (WINDOWP (window)
1822 && BUFFERP (XWINDOW (window)->buffer)
1823 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
1825 /* Arrange to go back to the original buffer once we're done
1826 processing the key sequence. We don't use
1827 save_excursion_{save,restore} here, in analogy to
1828 `read-key-sequence' to avoid saving point. Maybe this
1829 would not be a problem here, but it is easier to keep
1830 things the same.
1833 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
1835 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
1839 if (! NILP (current_kboard->Voverriding_terminal_local_map))
1841 value = Flookup_key (current_kboard->Voverriding_terminal_local_map,
1842 key, accept_default);
1843 if (! NILP (value) && !INTEGERP (value))
1844 goto done;
1846 else if (! NILP (Voverriding_local_map))
1848 value = Flookup_key (Voverriding_local_map, key, accept_default);
1849 if (! NILP (value) && !INTEGERP (value))
1850 goto done;
1852 else
1854 Lisp_Object keymap, local_map;
1855 EMACS_INT pt;
1857 pt = INTEGERP (position) ? XINT (position)
1858 : MARKERP (position) ? marker_position (position)
1859 : PT;
1861 local_map = get_local_map (pt, current_buffer, Qlocal_map);
1862 keymap = get_local_map (pt, current_buffer, Qkeymap);
1864 if (CONSP (position))
1866 Lisp_Object string;
1868 /* For a mouse click, get the local text-property keymap
1869 of the place clicked on, rather than point. */
1871 if (POSN_INBUFFER_P (position))
1873 Lisp_Object pos;
1875 pos = POSN_BUFFER_POSN (position);
1876 if (INTEGERP (pos)
1877 && XINT (pos) >= BEG && XINT (pos) <= Z)
1879 local_map = get_local_map (XINT (pos),
1880 current_buffer, Qlocal_map);
1882 keymap = get_local_map (XINT (pos),
1883 current_buffer, Qkeymap);
1887 /* If on a mode line string with a local keymap,
1888 or for a click on a string, i.e. overlay string or a
1889 string displayed via the `display' property,
1890 consider `local-map' and `keymap' properties of
1891 that string. */
1893 if (string = POSN_STRING (position),
1894 (CONSP (string) && STRINGP (XCAR (string))))
1896 Lisp_Object pos, map;
1898 pos = XCDR (string);
1899 string = XCAR (string);
1900 if (INTEGERP (pos)
1901 && XINT (pos) >= 0
1902 && XINT (pos) < SCHARS (string))
1904 map = Fget_text_property (pos, Qlocal_map, string);
1905 if (!NILP (map))
1906 local_map = map;
1908 map = Fget_text_property (pos, Qkeymap, string);
1909 if (!NILP (map))
1910 keymap = map;
1916 if (! NILP (keymap))
1918 value = Flookup_key (keymap, key, accept_default);
1919 if (! NILP (value) && !INTEGERP (value))
1920 goto done;
1923 nmaps = current_minor_maps (0, &maps);
1924 /* Note that all these maps are GCPRO'd
1925 in the places where we found them. */
1927 for (i = 0; i < nmaps; i++)
1928 if (! NILP (maps[i]))
1930 value = Flookup_key (maps[i], key, accept_default);
1931 if (! NILP (value) && !INTEGERP (value))
1932 goto done;
1935 if (! NILP (local_map))
1937 value = Flookup_key (local_map, key, accept_default);
1938 if (! NILP (value) && !INTEGERP (value))
1939 goto done;
1943 value = Flookup_key (current_global_map, key, accept_default);
1945 done:
1946 unbind_to (count, Qnil);
1948 UNGCPRO;
1949 if (NILP (value) || INTEGERP (value))
1950 return Qnil;
1952 /* If the result of the ordinary keymap lookup is an interactive
1953 command, look for a key binding (ie. remapping) for that command. */
1955 if (NILP (no_remap) && SYMBOLP (value))
1957 Lisp_Object value1;
1958 if (value1 = Fcommand_remapping (value, position, Qnil), !NILP (value1))
1959 value = value1;
1962 return value;
1965 /* GC is possible in this function if it autoloads a keymap. */
1967 DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0,
1968 doc: /* Return the binding for command KEYS in current local keymap only.
1969 KEYS is a string or vector, a sequence of keystrokes.
1970 The binding is probably a symbol with a function definition.
1972 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1973 bindings; see the description of `lookup-key' for more details about this. */)
1974 (keys, accept_default)
1975 Lisp_Object keys, accept_default;
1977 register Lisp_Object map;
1978 map = current_buffer->keymap;
1979 if (NILP (map))
1980 return Qnil;
1981 return Flookup_key (map, keys, accept_default);
1984 /* GC is possible in this function if it autoloads a keymap. */
1986 DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0,
1987 doc: /* Return the binding for command KEYS in current global keymap only.
1988 KEYS is a string or vector, a sequence of keystrokes.
1989 The binding is probably a symbol with a function definition.
1990 This function's return values are the same as those of `lookup-key'
1991 \(which see).
1993 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1994 bindings; see the description of `lookup-key' for more details about this. */)
1995 (keys, accept_default)
1996 Lisp_Object keys, accept_default;
1998 return Flookup_key (current_global_map, keys, accept_default);
2001 /* GC is possible in this function if it autoloads a keymap. */
2003 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0,
2004 doc: /* Find the visible minor mode bindings of KEY.
2005 Return an alist of pairs (MODENAME . BINDING), where MODENAME is
2006 the symbol which names the minor mode binding KEY, and BINDING is
2007 KEY's definition in that mode. In particular, if KEY has no
2008 minor-mode bindings, return nil. If the first binding is a
2009 non-prefix, all subsequent bindings will be omitted, since they would
2010 be ignored. Similarly, the list doesn't include non-prefix bindings
2011 that come after prefix bindings.
2013 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
2014 bindings; see the description of `lookup-key' for more details about this. */)
2015 (key, accept_default)
2016 Lisp_Object key, accept_default;
2018 Lisp_Object *modes, *maps;
2019 int nmaps;
2020 Lisp_Object binding;
2021 int i, j;
2022 struct gcpro gcpro1, gcpro2;
2024 nmaps = current_minor_maps (&modes, &maps);
2025 /* Note that all these maps are GCPRO'd
2026 in the places where we found them. */
2028 binding = Qnil;
2029 GCPRO2 (key, binding);
2031 for (i = j = 0; i < nmaps; i++)
2032 if (!NILP (maps[i])
2033 && !NILP (binding = Flookup_key (maps[i], key, accept_default))
2034 && !INTEGERP (binding))
2036 if (KEYMAPP (binding))
2037 maps[j++] = Fcons (modes[i], binding);
2038 else if (j == 0)
2039 RETURN_UNGCPRO (Fcons (Fcons (modes[i], binding), Qnil));
2042 UNGCPRO;
2043 return Flist (j, maps);
2046 DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 3, 0,
2047 doc: /* Define COMMAND as a prefix command. COMMAND should be a symbol.
2048 A new sparse keymap is stored as COMMAND's function definition and its value.
2049 If a second optional argument MAPVAR is given, the map is stored as
2050 its value instead of as COMMAND's value; but COMMAND is still defined
2051 as a function.
2052 The third optional argument NAME, if given, supplies a menu name
2053 string for the map. This is required to use the keymap as a menu.
2054 This function returns COMMAND. */)
2055 (command, mapvar, name)
2056 Lisp_Object command, mapvar, name;
2058 Lisp_Object map;
2059 map = Fmake_sparse_keymap (name);
2060 Ffset (command, map);
2061 if (!NILP (mapvar))
2062 Fset (mapvar, map);
2063 else
2064 Fset (command, map);
2065 return command;
2068 DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0,
2069 doc: /* Select KEYMAP as the global keymap. */)
2070 (keymap)
2071 Lisp_Object keymap;
2073 keymap = get_keymap (keymap, 1, 1);
2074 current_global_map = keymap;
2076 return Qnil;
2079 DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0,
2080 doc: /* Select KEYMAP as the local keymap.
2081 If KEYMAP is nil, that means no local keymap. */)
2082 (keymap)
2083 Lisp_Object keymap;
2085 if (!NILP (keymap))
2086 keymap = get_keymap (keymap, 1, 1);
2088 current_buffer->keymap = keymap;
2090 return Qnil;
2093 DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0,
2094 doc: /* Return current buffer's local keymap, or nil if it has none.
2095 Normally the local keymap is set by the major mode with `use-local-map'. */)
2098 return current_buffer->keymap;
2101 DEFUN ("current-global-map", Fcurrent_global_map, Scurrent_global_map, 0, 0, 0,
2102 doc: /* Return the current global keymap. */)
2105 return current_global_map;
2108 DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_maps, 0, 0, 0,
2109 doc: /* Return a list of keymaps for the minor modes of the current buffer. */)
2112 Lisp_Object *maps;
2113 int nmaps = current_minor_maps (0, &maps);
2115 return Flist (nmaps, maps);
2118 /* Help functions for describing and documenting keymaps. */
2120 struct accessible_keymaps_data {
2121 Lisp_Object maps, tail, thisseq;
2122 /* Does the current sequence end in the meta-prefix-char? */
2123 int is_metized;
2126 static void
2127 accessible_keymaps_1 (key, cmd, args, data)
2128 Lisp_Object key, cmd, args;
2129 /* Use void* to be compatible with map_keymap_function_t. */
2130 void *data;
2132 struct accessible_keymaps_data *d = data; /* Cast! */
2133 Lisp_Object maps = d->maps;
2134 Lisp_Object tail = d->tail;
2135 Lisp_Object thisseq = d->thisseq;
2136 int is_metized = d->is_metized && INTEGERP (key);
2137 Lisp_Object tem;
2139 cmd = get_keymap (get_keyelt (cmd, 0), 0, 0);
2140 if (NILP (cmd))
2141 return;
2143 /* Look for and break cycles. */
2144 while (!NILP (tem = Frassq (cmd, maps)))
2146 Lisp_Object prefix = XCAR (tem);
2147 int lim = XINT (Flength (XCAR (tem)));
2148 if (lim <= XINT (Flength (thisseq)))
2149 { /* This keymap was already seen with a smaller prefix. */
2150 int i = 0;
2151 while (i < lim && EQ (Faref (prefix, make_number (i)),
2152 Faref (thisseq, make_number (i))))
2153 i++;
2154 if (i >= lim)
2155 /* `prefix' is a prefix of `thisseq' => there's a cycle. */
2156 return;
2158 /* This occurrence of `cmd' in `maps' does not correspond to a cycle,
2159 but maybe `cmd' occurs again further down in `maps', so keep
2160 looking. */
2161 maps = XCDR (Fmemq (tem, maps));
2164 /* If the last key in thisseq is meta-prefix-char,
2165 turn it into a meta-ized keystroke. We know
2166 that the event we're about to append is an
2167 ascii keystroke since we're processing a
2168 keymap table. */
2169 if (is_metized)
2171 int meta_bit = meta_modifier;
2172 Lisp_Object last = make_number (XINT (Flength (thisseq)) - 1);
2173 tem = Fcopy_sequence (thisseq);
2175 Faset (tem, last, make_number (XINT (key) | meta_bit));
2177 /* This new sequence is the same length as
2178 thisseq, so stick it in the list right
2179 after this one. */
2180 XSETCDR (tail,
2181 Fcons (Fcons (tem, cmd), XCDR (tail)));
2183 else
2185 tem = append_key (thisseq, key);
2186 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
2190 /* This function cannot GC. */
2192 DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps,
2193 1, 2, 0,
2194 doc: /* Find all keymaps accessible via prefix characters from KEYMAP.
2195 Returns a list of elements of the form (KEYS . MAP), where the sequence
2196 KEYS starting from KEYMAP gets you to MAP. These elements are ordered
2197 so that the KEYS increase in length. The first element is ([] . KEYMAP).
2198 An optional argument PREFIX, if non-nil, should be a key sequence;
2199 then the value includes only maps for prefixes that start with PREFIX. */)
2200 (keymap, prefix)
2201 Lisp_Object keymap, prefix;
2203 Lisp_Object maps, tail;
2204 int prefixlen = XINT (Flength (prefix));
2206 /* no need for gcpro because we don't autoload any keymaps. */
2208 if (!NILP (prefix))
2210 /* If a prefix was specified, start with the keymap (if any) for
2211 that prefix, so we don't waste time considering other prefixes. */
2212 Lisp_Object tem;
2213 tem = Flookup_key (keymap, prefix, Qt);
2214 /* Flookup_key may give us nil, or a number,
2215 if the prefix is not defined in this particular map.
2216 It might even give us a list that isn't a keymap. */
2217 tem = get_keymap (tem, 0, 0);
2218 /* If the keymap is autoloaded `tem' is not a cons-cell, but we still
2219 want to return it. */
2220 if (!NILP (tem))
2222 /* Convert PREFIX to a vector now, so that later on
2223 we don't have to deal with the possibility of a string. */
2224 if (STRINGP (prefix))
2226 int i, i_byte, c;
2227 Lisp_Object copy;
2229 copy = Fmake_vector (make_number (SCHARS (prefix)), Qnil);
2230 for (i = 0, i_byte = 0; i < SCHARS (prefix);)
2232 int i_before = i;
2234 FETCH_STRING_CHAR_ADVANCE (c, prefix, i, i_byte);
2235 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
2236 c ^= 0200 | meta_modifier;
2237 ASET (copy, i_before, make_number (c));
2239 prefix = copy;
2241 maps = Fcons (Fcons (prefix, tem), Qnil);
2243 else
2244 return Qnil;
2246 else
2247 maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil),
2248 get_keymap (keymap, 1, 0)),
2249 Qnil);
2251 /* For each map in the list maps,
2252 look at any other maps it points to,
2253 and stick them at the end if they are not already in the list.
2255 This is a breadth-first traversal, where tail is the queue of
2256 nodes, and maps accumulates a list of all nodes visited. */
2258 for (tail = maps; CONSP (tail); tail = XCDR (tail))
2260 struct accessible_keymaps_data data;
2261 register Lisp_Object thismap = Fcdr (XCAR (tail));
2262 Lisp_Object last;
2264 data.thisseq = Fcar (XCAR (tail));
2265 data.maps = maps;
2266 data.tail = tail;
2267 last = make_number (XINT (Flength (data.thisseq)) - 1);
2268 /* Does the current sequence end in the meta-prefix-char? */
2269 data.is_metized = (XINT (last) >= 0
2270 /* Don't metize the last char of PREFIX. */
2271 && XINT (last) >= prefixlen
2272 && EQ (Faref (data.thisseq, last), meta_prefix_char));
2274 /* Since we can't run lisp code, we can't scan autoloaded maps. */
2275 if (CONSP (thismap))
2276 map_keymap (thismap, accessible_keymaps_1, Qnil, &data, 0);
2278 return maps;
2280 Lisp_Object Qsingle_key_description, Qkey_description;
2282 /* This function cannot GC. */
2284 DEFUN ("key-description", Fkey_description, Skey_description, 1, 2, 0,
2285 doc: /* Return a pretty description of key-sequence KEYS.
2286 Optional arg PREFIX is the sequence of keys leading up to KEYS.
2287 Control characters turn into "C-foo" sequences, meta into "M-foo",
2288 spaces are put between sequence elements, etc. */)
2289 (keys, prefix)
2290 Lisp_Object keys, prefix;
2292 int len = 0;
2293 int i, i_byte;
2294 Lisp_Object *args;
2295 int size = XINT (Flength (keys));
2296 Lisp_Object list;
2297 Lisp_Object sep = build_string (" ");
2298 Lisp_Object key;
2299 int add_meta = 0;
2301 if (!NILP (prefix))
2302 size += XINT (Flength (prefix));
2304 /* This has one extra element at the end that we don't pass to Fconcat. */
2305 args = (Lisp_Object *) alloca (size * 4 * sizeof (Lisp_Object));
2307 /* In effect, this computes
2308 (mapconcat 'single-key-description keys " ")
2309 but we shouldn't use mapconcat because it can do GC. */
2311 next_list:
2312 if (!NILP (prefix))
2313 list = prefix, prefix = Qnil;
2314 else if (!NILP (keys))
2315 list = keys, keys = Qnil;
2316 else
2318 if (add_meta)
2320 args[len] = Fsingle_key_description (meta_prefix_char, Qnil);
2321 len += 2;
2323 else if (len == 0)
2324 return empty_unibyte_string;
2325 return Fconcat (len - 1, args);
2328 if (STRINGP (list))
2329 size = SCHARS (list);
2330 else if (VECTORP (list))
2331 size = XVECTOR (list)->size;
2332 else if (CONSP (list))
2333 size = XINT (Flength (list));
2334 else
2335 wrong_type_argument (Qarrayp, list);
2337 i = i_byte = 0;
2339 while (i < size)
2341 if (STRINGP (list))
2343 int c;
2344 FETCH_STRING_CHAR_ADVANCE (c, list, i, i_byte);
2345 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
2346 c ^= 0200 | meta_modifier;
2347 XSETFASTINT (key, c);
2349 else if (VECTORP (list))
2351 key = AREF (list, i); i++;
2353 else
2355 key = XCAR (list);
2356 list = XCDR (list);
2357 i++;
2360 if (add_meta)
2362 if (!INTEGERP (key)
2363 || EQ (key, meta_prefix_char)
2364 || (XINT (key) & meta_modifier))
2366 args[len++] = Fsingle_key_description (meta_prefix_char, Qnil);
2367 args[len++] = sep;
2368 if (EQ (key, meta_prefix_char))
2369 continue;
2371 else
2372 XSETINT (key, (XINT (key) | meta_modifier) & ~0x80);
2373 add_meta = 0;
2375 else if (EQ (key, meta_prefix_char))
2377 add_meta = 1;
2378 continue;
2380 args[len++] = Fsingle_key_description (key, Qnil);
2381 args[len++] = sep;
2383 goto next_list;
2387 char *
2388 push_key_description (c, p, force_multibyte)
2389 register unsigned int c;
2390 register char *p;
2391 int force_multibyte;
2393 unsigned c2;
2395 /* Clear all the meaningless bits above the meta bit. */
2396 c &= meta_modifier | ~ - meta_modifier;
2397 c2 = c & ~(alt_modifier | ctrl_modifier | hyper_modifier
2398 | meta_modifier | shift_modifier | super_modifier);
2400 if (! CHARACTERP (make_number (c2)))
2402 /* KEY_DESCRIPTION_SIZE is large enough for this. */
2403 p += sprintf (p, "[%d]", c);
2404 return p;
2407 if (c & alt_modifier)
2409 *p++ = 'A';
2410 *p++ = '-';
2411 c -= alt_modifier;
2413 if ((c & ctrl_modifier) != 0
2414 || (c2 < ' ' && c2 != 27 && c2 != '\t' && c2 != Ctl ('M')))
2416 *p++ = 'C';
2417 *p++ = '-';
2418 c &= ~ctrl_modifier;
2420 if (c & hyper_modifier)
2422 *p++ = 'H';
2423 *p++ = '-';
2424 c -= hyper_modifier;
2426 if (c & meta_modifier)
2428 *p++ = 'M';
2429 *p++ = '-';
2430 c -= meta_modifier;
2432 if (c & shift_modifier)
2434 *p++ = 'S';
2435 *p++ = '-';
2436 c -= shift_modifier;
2438 if (c & super_modifier)
2440 *p++ = 's';
2441 *p++ = '-';
2442 c -= super_modifier;
2444 if (c < 040)
2446 if (c == 033)
2448 *p++ = 'E';
2449 *p++ = 'S';
2450 *p++ = 'C';
2452 else if (c == '\t')
2454 *p++ = 'T';
2455 *p++ = 'A';
2456 *p++ = 'B';
2458 else if (c == Ctl ('M'))
2460 *p++ = 'R';
2461 *p++ = 'E';
2462 *p++ = 'T';
2464 else
2466 /* `C-' already added above. */
2467 if (c > 0 && c <= Ctl ('Z'))
2468 *p++ = c + 0140;
2469 else
2470 *p++ = c + 0100;
2473 else if (c == 0177)
2475 *p++ = 'D';
2476 *p++ = 'E';
2477 *p++ = 'L';
2479 else if (c == ' ')
2481 *p++ = 'S';
2482 *p++ = 'P';
2483 *p++ = 'C';
2485 else if (c < 128
2486 || (NILP (current_buffer->enable_multibyte_characters)
2487 && SINGLE_BYTE_CHAR_P (c)
2488 && !force_multibyte))
2490 *p++ = c;
2492 else
2494 /* Now we are sure that C is a valid character code. */
2495 if (NILP (current_buffer->enable_multibyte_characters)
2496 && ! force_multibyte)
2497 *p++ = multibyte_char_to_unibyte (c, Qnil);
2498 else
2499 p += CHAR_STRING (c, (unsigned char *) p);
2502 return p;
2505 /* This function cannot GC. */
2507 DEFUN ("single-key-description", Fsingle_key_description,
2508 Ssingle_key_description, 1, 2, 0,
2509 doc: /* Return a pretty description of command character KEY.
2510 Control characters turn into C-whatever, etc.
2511 Optional argument NO-ANGLES non-nil means don't put angle brackets
2512 around function keys and event symbols. */)
2513 (key, no_angles)
2514 Lisp_Object key, no_angles;
2516 if (CONSP (key) && lucid_event_type_list_p (key))
2517 key = Fevent_convert_list (key);
2519 key = EVENT_HEAD (key);
2521 if (INTEGERP (key)) /* Normal character */
2523 char tem[KEY_DESCRIPTION_SIZE];
2525 *push_key_description (XUINT (key), tem, 1) = 0;
2526 return build_string (tem);
2528 else if (SYMBOLP (key)) /* Function key or event-symbol */
2530 if (NILP (no_angles))
2532 char *buffer
2533 = (char *) alloca (SBYTES (SYMBOL_NAME (key)) + 5);
2534 sprintf (buffer, "<%s>", SDATA (SYMBOL_NAME (key)));
2535 return build_string (buffer);
2537 else
2538 return Fsymbol_name (key);
2540 else if (STRINGP (key)) /* Buffer names in the menubar. */
2541 return Fcopy_sequence (key);
2542 else
2543 error ("KEY must be an integer, cons, symbol, or string");
2544 return Qnil;
2547 char *
2548 push_text_char_description (c, p)
2549 register unsigned int c;
2550 register char *p;
2552 if (c >= 0200)
2554 *p++ = 'M';
2555 *p++ = '-';
2556 c -= 0200;
2558 if (c < 040)
2560 *p++ = '^';
2561 *p++ = c + 64; /* 'A' - 1 */
2563 else if (c == 0177)
2565 *p++ = '^';
2566 *p++ = '?';
2568 else
2569 *p++ = c;
2570 return p;
2573 /* This function cannot GC. */
2575 DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
2576 doc: /* Return a pretty description of file-character CHARACTER.
2577 Control characters turn into "^char", etc. This differs from
2578 `single-key-description' which turns them into "C-char".
2579 Also, this function recognizes the 2**7 bit as the Meta character,
2580 whereas `single-key-description' uses the 2**27 bit for Meta.
2581 See Info node `(elisp)Describing Characters' for examples. */)
2582 (character)
2583 Lisp_Object character;
2585 /* Currently MAX_MULTIBYTE_LENGTH is 4 (< 6). */
2586 unsigned char str[6];
2587 int c;
2589 CHECK_NUMBER (character);
2591 c = XINT (character);
2592 if (!ASCII_CHAR_P (c))
2594 int len = CHAR_STRING (c, str);
2596 return make_multibyte_string (str, 1, len);
2599 *push_text_char_description (c & 0377, str) = 0;
2601 return build_string (str);
2604 static int where_is_preferred_modifier;
2606 /* Return 0 if SEQ uses non-preferred modifiers or non-char events.
2607 Else, return 2 if SEQ uses the where_is_preferred_modifier,
2608 and 1 otherwise. */
2609 static int
2610 preferred_sequence_p (seq)
2611 Lisp_Object seq;
2613 int i;
2614 int len = XINT (Flength (seq));
2615 int result = 1;
2617 for (i = 0; i < len; i++)
2619 Lisp_Object ii, elt;
2621 XSETFASTINT (ii, i);
2622 elt = Faref (seq, ii);
2624 if (!INTEGERP (elt))
2625 return 0;
2626 else
2628 int modifiers = XUINT (elt) & (CHAR_MODIFIER_MASK & ~CHAR_META);
2629 if (modifiers == where_is_preferred_modifier)
2630 result = 2;
2631 else if (modifiers)
2632 return 0;
2636 return result;
2640 /* where-is - finding a command in a set of keymaps. */
2642 static Lisp_Object where_is_internal ();
2643 static void where_is_internal_1 P_ ((Lisp_Object key, Lisp_Object binding,
2644 Lisp_Object args, void *data));
2646 /* Like Flookup_key, but uses a list of keymaps SHADOW instead of a single map.
2647 Returns the first non-nil binding found in any of those maps. */
2649 static Lisp_Object
2650 shadow_lookup (shadow, key, flag)
2651 Lisp_Object shadow, key, flag;
2653 Lisp_Object tail, value;
2655 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2657 value = Flookup_key (XCAR (tail), key, flag);
2658 if (NATNUMP (value))
2660 value = Flookup_key (XCAR (tail),
2661 Fsubstring (key, make_number (0), value), flag);
2662 if (!NILP (value))
2663 return Qnil;
2665 else if (!NILP (value))
2666 return value;
2668 return Qnil;
2671 static Lisp_Object Vmouse_events;
2673 struct where_is_internal_data {
2674 Lisp_Object definition, noindirect, this, last;
2675 int last_is_meta;
2676 Lisp_Object sequences;
2679 /* This function can GC if Flookup_key autoloads any keymaps. */
2681 static Lisp_Object
2682 where_is_internal (definition, keymaps, firstonly, noindirect, no_remap)
2683 Lisp_Object definition, keymaps;
2684 Lisp_Object firstonly, noindirect, no_remap;
2686 Lisp_Object maps = Qnil;
2687 Lisp_Object found, sequences;
2688 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
2689 /* 1 means ignore all menu bindings entirely. */
2690 int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii);
2692 found = keymaps;
2693 while (CONSP (found))
2695 maps =
2696 nconc2 (maps,
2697 Faccessible_keymaps (get_keymap (XCAR (found), 1, 0), Qnil));
2698 found = XCDR (found);
2701 GCPRO5 (definition, keymaps, maps, found, sequences);
2702 found = Qnil;
2703 sequences = Qnil;
2705 /* If this command is remapped, then it has no key bindings
2706 of its own. */
2707 if (NILP (no_remap)
2708 && SYMBOLP (definition)
2709 && !NILP (Fcommand_remapping (definition, Qnil, keymaps)))
2710 RETURN_UNGCPRO (Qnil);
2712 for (; CONSP (maps); maps = XCDR (maps))
2714 /* Key sequence to reach map, and the map that it reaches */
2715 register Lisp_Object this, map, tem;
2716 struct where_is_internal_data data;
2718 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
2719 [M-CHAR] sequences, check if last character of the sequence
2720 is the meta-prefix char. */
2721 Lisp_Object last;
2722 int last_is_meta;
2724 this = Fcar (XCAR (maps));
2725 map = Fcdr (XCAR (maps));
2726 last = make_number (XINT (Flength (this)) - 1);
2727 last_is_meta = (XINT (last) >= 0
2728 && EQ (Faref (this, last), meta_prefix_char));
2730 /* if (nomenus && !preferred_sequence_p (this)) */
2731 if (nomenus && XINT (last) >= 0
2732 && SYMBOLP (tem = Faref (this, make_number (0)))
2733 && !NILP (Fmemq (XCAR (parse_modifiers (tem)), Vmouse_events)))
2734 /* If no menu entries should be returned, skip over the
2735 keymaps bound to `menu-bar' and `tool-bar' and other
2736 non-ascii prefixes like `C-down-mouse-2'. */
2737 continue;
2739 QUIT;
2741 data.definition = definition;
2742 data.noindirect = noindirect;
2743 data.this = this;
2744 data.last = last;
2745 data.last_is_meta = last_is_meta;
2746 data.sequences = Qnil;
2748 if (CONSP (map))
2749 map_keymap (map, where_is_internal_1, Qnil, &data, 0);
2751 sequences = data.sequences;
2753 while (CONSP (sequences))
2755 Lisp_Object sequence, remapped, function;
2757 sequence = XCAR (sequences);
2758 sequences = XCDR (sequences);
2760 /* If the current sequence is a command remapping with
2761 format [remap COMMAND], find the key sequences
2762 which run COMMAND, and use those sequences instead. */
2763 remapped = Qnil;
2764 if (NILP (no_remap)
2765 && VECTORP (sequence) && XVECTOR (sequence)->size == 2
2766 && EQ (AREF (sequence, 0), Qremap)
2767 && (function = AREF (sequence, 1), SYMBOLP (function)))
2769 Lisp_Object remapped1;
2771 remapped1 = where_is_internal (function, keymaps, firstonly, noindirect, Qt);
2772 if (CONSP (remapped1))
2774 /* Verify that this key binding actually maps to the
2775 remapped command (see below). */
2776 if (!EQ (shadow_lookup (keymaps, XCAR (remapped1), Qnil), function))
2777 continue;
2778 sequence = XCAR (remapped1);
2779 remapped = XCDR (remapped1);
2780 goto record_sequence;
2784 /* Verify that this key binding is not shadowed by another
2785 binding for the same key, before we say it exists.
2787 Mechanism: look for local definition of this key and if
2788 it is defined and does not match what we found then
2789 ignore this key.
2791 Either nil or number as value from Flookup_key
2792 means undefined. */
2793 if (!EQ (shadow_lookup (keymaps, sequence, Qnil), definition))
2794 continue;
2796 record_sequence:
2797 /* Don't annoy user with strings from a menu such as
2798 Select Paste. Change them all to "(any string)",
2799 so that there seems to be only one menu item
2800 to report. */
2801 if (! NILP (sequence))
2803 Lisp_Object tem;
2804 tem = Faref (sequence, make_number (XVECTOR (sequence)->size - 1));
2805 if (STRINGP (tem))
2806 Faset (sequence, make_number (XVECTOR (sequence)->size - 1),
2807 build_string ("(any string)"));
2810 /* It is a true unshadowed match. Record it, unless it's already
2811 been seen (as could happen when inheriting keymaps). */
2812 if (NILP (Fmember (sequence, found)))
2813 found = Fcons (sequence, found);
2815 /* If firstonly is Qnon_ascii, then we can return the first
2816 binding we find. If firstonly is not Qnon_ascii but not
2817 nil, then we should return the first ascii-only binding
2818 we find. */
2819 if (EQ (firstonly, Qnon_ascii))
2820 RETURN_UNGCPRO (sequence);
2821 else if (!NILP (firstonly) && 2 == preferred_sequence_p (sequence))
2822 RETURN_UNGCPRO (sequence);
2824 if (CONSP (remapped))
2826 sequence = XCAR (remapped);
2827 remapped = XCDR (remapped);
2828 goto record_sequence;
2833 UNGCPRO;
2835 found = Fnreverse (found);
2837 /* firstonly may have been t, but we may have gone all the way through
2838 the keymaps without finding an all-ASCII key sequence. So just
2839 return the best we could find. */
2840 if (NILP (firstonly))
2841 return found;
2842 else if (where_is_preferred_modifier == 0)
2843 return Fcar (found);
2844 else
2845 { /* Maybe we did not find a preferred_modifier binding, but we did find
2846 some ASCII binding. */
2847 Lisp_Object bindings = found;
2848 while (CONSP (bindings))
2849 if (preferred_sequence_p (XCAR (bindings)))
2850 return XCAR (bindings);
2851 else
2852 bindings = XCDR (bindings);
2853 return Fcar (found);
2857 static Lisp_Object Vwhere_is_preferred_modifier;
2859 DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 5, 0,
2860 doc: /* Return list of keys that invoke DEFINITION.
2861 If KEYMAP is a keymap, search only KEYMAP and the global keymap.
2862 If KEYMAP is nil, search all the currently active keymaps.
2863 If KEYMAP is a list of keymaps, search only those keymaps.
2865 If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found,
2866 rather than a list of all possible key sequences.
2867 If FIRSTONLY is the symbol `non-ascii', return the first binding found,
2868 no matter what it is.
2869 If FIRSTONLY has another non-nil value, prefer bindings
2870 that use the modifier key specified in `where-is-preferred-modifier'
2871 \(or their meta variants) and entirely reject menu bindings.
2873 If optional 4th arg NOINDIRECT is non-nil, don't follow indirections
2874 to other keymaps or slots. This makes it possible to search for an
2875 indirect definition itself.
2877 If optional 5th arg NO-REMAP is non-nil, don't search for key sequences
2878 that invoke a command which is remapped to DEFINITION, but include the
2879 remapped command in the returned list. */)
2880 (definition, keymap, firstonly, noindirect, no_remap)
2881 Lisp_Object definition, keymap;
2882 Lisp_Object firstonly, noindirect, no_remap;
2884 Lisp_Object sequences, keymaps;
2885 /* 1 means ignore all menu bindings entirely. */
2886 int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii);
2887 Lisp_Object result;
2889 /* Refresh the C version of the modifier preference. */
2890 where_is_preferred_modifier
2891 = parse_solitary_modifier (Vwhere_is_preferred_modifier);
2893 /* Find the relevant keymaps. */
2894 if (CONSP (keymap) && KEYMAPP (XCAR (keymap)))
2895 keymaps = keymap;
2896 else if (!NILP (keymap))
2897 keymaps = Fcons (keymap, Fcons (current_global_map, Qnil));
2898 else
2899 keymaps = Fcurrent_active_maps (Qnil, Qnil);
2901 /* Only use caching for the menubar (i.e. called with (def nil t nil).
2902 We don't really need to check `keymap'. */
2903 if (nomenus && NILP (noindirect) && NILP (keymap))
2905 Lisp_Object *defns;
2906 int i, n;
2907 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
2909 /* Check heuristic-consistency of the cache. */
2910 if (NILP (Fequal (keymaps, where_is_cache_keymaps)))
2911 where_is_cache = Qnil;
2913 if (NILP (where_is_cache))
2915 /* We need to create the cache. */
2916 Lisp_Object args[2];
2917 where_is_cache = Fmake_hash_table (0, args);
2918 where_is_cache_keymaps = Qt;
2920 /* Fill in the cache. */
2921 GCPRO5 (definition, keymaps, firstonly, noindirect, no_remap);
2922 where_is_internal (definition, keymaps, firstonly, noindirect, no_remap);
2923 UNGCPRO;
2925 where_is_cache_keymaps = keymaps;
2928 /* We want to process definitions from the last to the first.
2929 Instead of consing, copy definitions to a vector and step
2930 over that vector. */
2931 sequences = Fgethash (definition, where_is_cache, Qnil);
2932 n = XINT (Flength (sequences));
2933 defns = (Lisp_Object *) alloca (n * sizeof *defns);
2934 for (i = 0; CONSP (sequences); sequences = XCDR (sequences))
2935 defns[i++] = XCAR (sequences);
2937 /* Verify that the key bindings are not shadowed. Note that
2938 the following can GC. */
2939 GCPRO2 (definition, keymaps);
2940 result = Qnil;
2942 int best_pref = -1;
2943 int j = -1;
2944 for (i = n - 1; i >= 0; --i)
2946 int pref = preferred_sequence_p (defns[i]);
2947 if (pref > best_pref
2948 && EQ (shadow_lookup (keymaps, defns[i], Qnil), definition))
2950 j = i;
2951 best_pref = pref;
2952 if (best_pref == 2)
2953 break;
2956 result = j >= 0 ? defns[j] : Qnil;
2958 UNGCPRO;
2960 else
2962 /* Kill the cache so that where_is_internal_1 doesn't think
2963 we're filling it up. */
2964 where_is_cache = Qnil;
2965 result = where_is_internal (definition, keymaps, firstonly, noindirect, no_remap);
2968 return result;
2971 /* This function can GC because get_keyelt can. */
2973 static void
2974 where_is_internal_1 (key, binding, args, data)
2975 Lisp_Object key, binding, args;
2976 void *data;
2978 struct where_is_internal_data *d = data; /* Cast! */
2979 Lisp_Object definition = d->definition;
2980 Lisp_Object noindirect = d->noindirect;
2981 Lisp_Object this = d->this;
2982 Lisp_Object last = d->last;
2983 int last_is_meta = d->last_is_meta;
2984 Lisp_Object sequence;
2986 /* Search through indirections unless that's not wanted. */
2987 if (NILP (noindirect))
2988 binding = get_keyelt (binding, 0);
2990 /* End this iteration if this element does not match
2991 the target. */
2993 if (!(!NILP (where_is_cache) /* everything "matches" during cache-fill. */
2994 || EQ (binding, definition)
2995 || (CONSP (definition) && !NILP (Fequal (binding, definition)))))
2996 /* Doesn't match. */
2997 return;
2999 /* We have found a match. Construct the key sequence where we found it. */
3000 if (INTEGERP (key) && last_is_meta)
3002 sequence = Fcopy_sequence (this);
3003 Faset (sequence, last, make_number (XINT (key) | meta_modifier));
3005 else
3007 if (CONSP (key))
3008 key = Fcons (XCAR (key), XCDR (key));
3009 sequence = append_key (this, key);
3012 if (!NILP (where_is_cache))
3014 Lisp_Object sequences = Fgethash (binding, where_is_cache, Qnil);
3015 Fputhash (binding, Fcons (sequence, sequences), where_is_cache);
3017 else
3018 d->sequences = Fcons (sequence, d->sequences);
3021 /* describe-bindings - summarizing all the bindings in a set of keymaps. */
3023 DEFUN ("describe-buffer-bindings", Fdescribe_buffer_bindings, Sdescribe_buffer_bindings, 1, 3, 0,
3024 doc: /* Insert the list of all defined keys and their definitions.
3025 The list is inserted in the current buffer, while the bindings are
3026 looked up in BUFFER.
3027 The optional argument PREFIX, if non-nil, should be a key sequence;
3028 then we display only bindings that start with that prefix.
3029 The optional argument MENUS, if non-nil, says to mention menu bindings.
3030 \(Ordinarily these are omitted from the output.) */)
3031 (buffer, prefix, menus)
3032 Lisp_Object buffer, prefix, menus;
3034 Lisp_Object outbuf, shadow;
3035 int nomenu = NILP (menus);
3036 register Lisp_Object start1;
3037 struct gcpro gcpro1;
3039 char *alternate_heading
3040 = "\
3041 Keyboard translations:\n\n\
3042 You type Translation\n\
3043 -------- -----------\n";
3045 CHECK_BUFFER (buffer);
3047 shadow = Qnil;
3048 GCPRO1 (shadow);
3050 outbuf = Fcurrent_buffer ();
3052 /* Report on alternates for keys. */
3053 if (STRINGP (current_kboard->Vkeyboard_translate_table) && !NILP (prefix))
3055 int c;
3056 const unsigned char *translate = SDATA (current_kboard->Vkeyboard_translate_table);
3057 int translate_len = SCHARS (current_kboard->Vkeyboard_translate_table);
3059 for (c = 0; c < translate_len; c++)
3060 if (translate[c] != c)
3062 char buf[KEY_DESCRIPTION_SIZE];
3063 char *bufend;
3065 if (alternate_heading)
3067 insert_string (alternate_heading);
3068 alternate_heading = 0;
3071 bufend = push_key_description (translate[c], buf, 1);
3072 insert (buf, bufend - buf);
3073 Findent_to (make_number (16), make_number (1));
3074 bufend = push_key_description (c, buf, 1);
3075 insert (buf, bufend - buf);
3077 insert ("\n", 1);
3079 /* Insert calls signal_after_change which may GC. */
3080 translate = SDATA (current_kboard->Vkeyboard_translate_table);
3083 insert ("\n", 1);
3086 if (!NILP (Vkey_translation_map))
3087 describe_map_tree (Vkey_translation_map, 0, Qnil, prefix,
3088 "Key translations", nomenu, 1, 0, 0);
3091 /* Print the (major mode) local map. */
3092 start1 = Qnil;
3093 if (!NILP (current_kboard->Voverriding_terminal_local_map))
3094 start1 = current_kboard->Voverriding_terminal_local_map;
3095 else if (!NILP (Voverriding_local_map))
3096 start1 = Voverriding_local_map;
3098 if (!NILP (start1))
3100 describe_map_tree (start1, 1, shadow, prefix,
3101 "\f\nOverriding Bindings", nomenu, 0, 0, 0);
3102 shadow = Fcons (start1, shadow);
3104 else
3106 /* Print the minor mode and major mode keymaps. */
3107 int i, nmaps;
3108 Lisp_Object *modes, *maps;
3110 /* Temporarily switch to `buffer', so that we can get that buffer's
3111 minor modes correctly. */
3112 Fset_buffer (buffer);
3114 nmaps = current_minor_maps (&modes, &maps);
3115 Fset_buffer (outbuf);
3117 start1 = get_local_map (BUF_PT (XBUFFER (buffer)),
3118 XBUFFER (buffer), Qkeymap);
3119 if (!NILP (start1))
3121 describe_map_tree (start1, 1, shadow, prefix,
3122 "\f\n`keymap' Property Bindings", nomenu,
3123 0, 0, 0);
3124 shadow = Fcons (start1, shadow);
3127 /* Print the minor mode maps. */
3128 for (i = 0; i < nmaps; i++)
3130 /* The title for a minor mode keymap
3131 is constructed at run time.
3132 We let describe_map_tree do the actual insertion
3133 because it takes care of other features when doing so. */
3134 char *title, *p;
3136 if (!SYMBOLP (modes[i]))
3137 abort ();
3139 p = title = (char *) alloca (42 + SCHARS (SYMBOL_NAME (modes[i])));
3140 *p++ = '\f';
3141 *p++ = '\n';
3142 *p++ = '`';
3143 bcopy (SDATA (SYMBOL_NAME (modes[i])), p,
3144 SCHARS (SYMBOL_NAME (modes[i])));
3145 p += SCHARS (SYMBOL_NAME (modes[i]));
3146 *p++ = '\'';
3147 bcopy (" Minor Mode Bindings", p, sizeof (" Minor Mode Bindings") - 1);
3148 p += sizeof (" Minor Mode Bindings") - 1;
3149 *p = 0;
3151 describe_map_tree (maps[i], 1, shadow, prefix,
3152 title, nomenu, 0, 0, 0);
3153 shadow = Fcons (maps[i], shadow);
3156 start1 = get_local_map (BUF_PT (XBUFFER (buffer)),
3157 XBUFFER (buffer), Qlocal_map);
3158 if (!NILP (start1))
3160 if (EQ (start1, XBUFFER (buffer)->keymap))
3161 describe_map_tree (start1, 1, shadow, prefix,
3162 "\f\nMajor Mode Bindings", nomenu, 0, 0, 0);
3163 else
3164 describe_map_tree (start1, 1, shadow, prefix,
3165 "\f\n`local-map' Property Bindings",
3166 nomenu, 0, 0, 0);
3168 shadow = Fcons (start1, shadow);
3172 describe_map_tree (current_global_map, 1, shadow, prefix,
3173 "\f\nGlobal Bindings", nomenu, 0, 1, 0);
3175 /* Print the function-key-map translations under this prefix. */
3176 if (!NILP (current_kboard->Vlocal_function_key_map))
3177 describe_map_tree (current_kboard->Vlocal_function_key_map, 0, Qnil, prefix,
3178 "\f\nFunction key map translations", nomenu, 1, 0, 0);
3180 /* Print the input-decode-map translations under this prefix. */
3181 if (!NILP (current_kboard->Vinput_decode_map))
3182 describe_map_tree (current_kboard->Vinput_decode_map, 0, Qnil, prefix,
3183 "\f\nInput decoding map translations", nomenu, 1, 0, 0);
3185 UNGCPRO;
3186 return Qnil;
3189 /* Insert a description of the key bindings in STARTMAP,
3190 followed by those of all maps reachable through STARTMAP.
3191 If PARTIAL is nonzero, omit certain "uninteresting" commands
3192 (such as `undefined').
3193 If SHADOW is non-nil, it is a list of maps;
3194 don't mention keys which would be shadowed by any of them.
3195 PREFIX, if non-nil, says mention only keys that start with PREFIX.
3196 TITLE, if not 0, is a string to insert at the beginning.
3197 TITLE should not end with a colon or a newline; we supply that.
3198 If NOMENU is not 0, then omit menu-bar commands.
3200 If TRANSL is nonzero, the definitions are actually key translations
3201 so print strings and vectors differently.
3203 If ALWAYS_TITLE is nonzero, print the title even if there are no maps
3204 to look through.
3206 If MENTION_SHADOW is nonzero, then when something is shadowed by SHADOW,
3207 don't omit it; instead, mention it but say it is shadowed. */
3209 void
3210 describe_map_tree (startmap, partial, shadow, prefix, title, nomenu, transl,
3211 always_title, mention_shadow)
3212 Lisp_Object startmap, shadow, prefix;
3213 int partial;
3214 char *title;
3215 int nomenu;
3216 int transl;
3217 int always_title;
3218 int mention_shadow;
3220 Lisp_Object maps, orig_maps, seen, sub_shadows;
3221 struct gcpro gcpro1, gcpro2, gcpro3;
3222 int something = 0;
3223 char *key_heading
3224 = "\
3225 key binding\n\
3226 --- -------\n";
3228 orig_maps = maps = Faccessible_keymaps (startmap, prefix);
3229 seen = Qnil;
3230 sub_shadows = Qnil;
3231 GCPRO3 (maps, seen, sub_shadows);
3233 if (nomenu)
3235 Lisp_Object list;
3237 /* Delete from MAPS each element that is for the menu bar. */
3238 for (list = maps; CONSP (list); list = XCDR (list))
3240 Lisp_Object elt, prefix, tem;
3242 elt = XCAR (list);
3243 prefix = Fcar (elt);
3244 if (XVECTOR (prefix)->size >= 1)
3246 tem = Faref (prefix, make_number (0));
3247 if (EQ (tem, Qmenu_bar))
3248 maps = Fdelq (elt, maps);
3253 if (!NILP (maps) || always_title)
3255 if (title)
3257 insert_string (title);
3258 if (!NILP (prefix))
3260 insert_string (" Starting With ");
3261 insert1 (Fkey_description (prefix, Qnil));
3263 insert_string (":\n");
3265 insert_string (key_heading);
3266 something = 1;
3269 for (; CONSP (maps); maps = XCDR (maps))
3271 register Lisp_Object elt, prefix, tail;
3273 elt = XCAR (maps);
3274 prefix = Fcar (elt);
3276 sub_shadows = Qnil;
3278 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
3280 Lisp_Object shmap;
3282 shmap = XCAR (tail);
3284 /* If the sequence by which we reach this keymap is zero-length,
3285 then the shadow map for this keymap is just SHADOW. */
3286 if ((STRINGP (prefix) && SCHARS (prefix) == 0)
3287 || (VECTORP (prefix) && XVECTOR (prefix)->size == 0))
3289 /* If the sequence by which we reach this keymap actually has
3290 some elements, then the sequence's definition in SHADOW is
3291 what we should use. */
3292 else
3294 shmap = Flookup_key (shmap, Fcar (elt), Qt);
3295 if (INTEGERP (shmap))
3296 shmap = Qnil;
3299 /* If shmap is not nil and not a keymap,
3300 it completely shadows this map, so don't
3301 describe this map at all. */
3302 if (!NILP (shmap) && !KEYMAPP (shmap))
3303 goto skip;
3305 if (!NILP (shmap))
3306 sub_shadows = Fcons (shmap, sub_shadows);
3309 /* Maps we have already listed in this loop shadow this map. */
3310 for (tail = orig_maps; !EQ (tail, maps); tail = XCDR (tail))
3312 Lisp_Object tem;
3313 tem = Fequal (Fcar (XCAR (tail)), prefix);
3314 if (!NILP (tem))
3315 sub_shadows = Fcons (XCDR (XCAR (tail)), sub_shadows);
3318 describe_map (Fcdr (elt), prefix,
3319 transl ? describe_translation : describe_command,
3320 partial, sub_shadows, &seen, nomenu, mention_shadow);
3322 skip: ;
3325 if (something)
3326 insert_string ("\n");
3328 UNGCPRO;
3331 static int previous_description_column;
3333 static void
3334 describe_command (definition, args)
3335 Lisp_Object definition, args;
3337 register Lisp_Object tem1;
3338 int column = (int) current_column (); /* iftc */
3339 int description_column;
3341 /* If column 16 is no good, go to col 32;
3342 but don't push beyond that--go to next line instead. */
3343 if (column > 30)
3345 insert_char ('\n');
3346 description_column = 32;
3348 else if (column > 14 || (column > 10 && previous_description_column == 32))
3349 description_column = 32;
3350 else
3351 description_column = 16;
3353 Findent_to (make_number (description_column), make_number (1));
3354 previous_description_column = description_column;
3356 if (SYMBOLP (definition))
3358 tem1 = SYMBOL_NAME (definition);
3359 insert1 (tem1);
3360 insert_string ("\n");
3362 else if (STRINGP (definition) || VECTORP (definition))
3363 insert_string ("Keyboard Macro\n");
3364 else if (KEYMAPP (definition))
3365 insert_string ("Prefix Command\n");
3366 else
3367 insert_string ("??\n");
3370 static void
3371 describe_translation (definition, args)
3372 Lisp_Object definition, args;
3374 register Lisp_Object tem1;
3376 Findent_to (make_number (16), make_number (1));
3378 if (SYMBOLP (definition))
3380 tem1 = SYMBOL_NAME (definition);
3381 insert1 (tem1);
3382 insert_string ("\n");
3384 else if (STRINGP (definition) || VECTORP (definition))
3386 insert1 (Fkey_description (definition, Qnil));
3387 insert_string ("\n");
3389 else if (KEYMAPP (definition))
3390 insert_string ("Prefix Command\n");
3391 else
3392 insert_string ("??\n");
3395 /* describe_map puts all the usable elements of a sparse keymap
3396 into an array of `struct describe_map_elt',
3397 then sorts them by the events. */
3399 struct describe_map_elt { Lisp_Object event; Lisp_Object definition; int shadowed; };
3401 /* qsort comparison function for sorting `struct describe_map_elt' by
3402 the event field. */
3404 static int
3405 describe_map_compare (aa, bb)
3406 const void *aa, *bb;
3408 const struct describe_map_elt *a = aa, *b = bb;
3409 if (INTEGERP (a->event) && INTEGERP (b->event))
3410 return ((XINT (a->event) > XINT (b->event))
3411 - (XINT (a->event) < XINT (b->event)));
3412 if (!INTEGERP (a->event) && INTEGERP (b->event))
3413 return 1;
3414 if (INTEGERP (a->event) && !INTEGERP (b->event))
3415 return -1;
3416 if (SYMBOLP (a->event) && SYMBOLP (b->event))
3417 return (!NILP (Fstring_lessp (a->event, b->event)) ? -1
3418 : !NILP (Fstring_lessp (b->event, a->event)) ? 1
3419 : 0);
3420 return 0;
3423 /* Describe the contents of map MAP, assuming that this map itself is
3424 reached by the sequence of prefix keys PREFIX (a string or vector).
3425 PARTIAL, SHADOW, NOMENU are as in `describe_map_tree' above. */
3427 static void
3428 describe_map (map, prefix, elt_describer, partial, shadow,
3429 seen, nomenu, mention_shadow)
3430 register Lisp_Object map;
3431 Lisp_Object prefix;
3432 void (*elt_describer) P_ ((Lisp_Object, Lisp_Object));
3433 int partial;
3434 Lisp_Object shadow;
3435 Lisp_Object *seen;
3436 int nomenu;
3437 int mention_shadow;
3439 Lisp_Object tail, definition, event;
3440 Lisp_Object tem;
3441 Lisp_Object suppress;
3442 Lisp_Object kludge;
3443 int first = 1;
3444 struct gcpro gcpro1, gcpro2, gcpro3;
3446 /* These accumulate the values from sparse keymap bindings,
3447 so we can sort them and handle them in order. */
3448 int length_needed = 0;
3449 struct describe_map_elt *vect;
3450 int slots_used = 0;
3451 int i;
3453 suppress = Qnil;
3455 if (partial)
3456 suppress = intern ("suppress-keymap");
3458 /* This vector gets used to present single keys to Flookup_key. Since
3459 that is done once per keymap element, we don't want to cons up a
3460 fresh vector every time. */
3461 kludge = Fmake_vector (make_number (1), Qnil);
3462 definition = Qnil;
3464 GCPRO3 (prefix, definition, kludge);
3466 map = call1 (Qkeymap_canonicalize, map);
3468 for (tail = map; CONSP (tail); tail = XCDR (tail))
3469 length_needed++;
3471 vect = ((struct describe_map_elt *)
3472 alloca (sizeof (struct describe_map_elt) * length_needed));
3474 for (tail = map; CONSP (tail); tail = XCDR (tail))
3476 QUIT;
3478 if (VECTORP (XCAR (tail))
3479 || CHAR_TABLE_P (XCAR (tail)))
3480 describe_vector (XCAR (tail),
3481 prefix, Qnil, elt_describer, partial, shadow, map,
3482 (int *)0, 0, 1, mention_shadow);
3483 else if (CONSP (XCAR (tail)))
3485 int this_shadowed = 0;
3487 event = XCAR (XCAR (tail));
3489 /* Ignore bindings whose "prefix" are not really valid events.
3490 (We get these in the frames and buffers menu.) */
3491 if (!(SYMBOLP (event) || INTEGERP (event)))
3492 continue;
3494 if (nomenu && EQ (event, Qmenu_bar))
3495 continue;
3497 definition = get_keyelt (XCDR (XCAR (tail)), 0);
3499 /* Don't show undefined commands or suppressed commands. */
3500 if (NILP (definition)) continue;
3501 if (SYMBOLP (definition) && partial)
3503 tem = Fget (definition, suppress);
3504 if (!NILP (tem))
3505 continue;
3508 /* Don't show a command that isn't really visible
3509 because a local definition of the same key shadows it. */
3511 ASET (kludge, 0, event);
3512 if (!NILP (shadow))
3514 tem = shadow_lookup (shadow, kludge, Qt);
3515 if (!NILP (tem))
3517 /* If both bindings are keymaps, this key is a prefix key,
3518 so don't say it is shadowed. */
3519 if (KEYMAPP (definition) && KEYMAPP (tem))
3521 /* Avoid generating duplicate entries if the
3522 shadowed binding has the same definition. */
3523 else if (mention_shadow && !EQ (tem, definition))
3524 this_shadowed = 1;
3525 else
3526 continue;
3530 tem = Flookup_key (map, kludge, Qt);
3531 if (!EQ (tem, definition)) continue;
3533 vect[slots_used].event = event;
3534 vect[slots_used].definition = definition;
3535 vect[slots_used].shadowed = this_shadowed;
3536 slots_used++;
3538 else if (EQ (XCAR (tail), Qkeymap))
3540 /* The same keymap might be in the structure twice, if we're
3541 using an inherited keymap. So skip anything we've already
3542 encountered. */
3543 tem = Fassq (tail, *seen);
3544 if (CONSP (tem) && !NILP (Fequal (XCAR (tem), prefix)))
3545 break;
3546 *seen = Fcons (Fcons (tail, prefix), *seen);
3550 /* If we found some sparse map events, sort them. */
3552 qsort (vect, slots_used, sizeof (struct describe_map_elt),
3553 describe_map_compare);
3555 /* Now output them in sorted order. */
3557 for (i = 0; i < slots_used; i++)
3559 Lisp_Object start, end;
3561 if (first)
3563 previous_description_column = 0;
3564 insert ("\n", 1);
3565 first = 0;
3568 ASET (kludge, 0, vect[i].event);
3569 start = vect[i].event;
3570 end = start;
3572 definition = vect[i].definition;
3574 /* Find consecutive chars that are identically defined. */
3575 if (INTEGERP (vect[i].event))
3577 while (i + 1 < slots_used
3578 && EQ (vect[i+1].event, make_number (XINT (vect[i].event) + 1))
3579 && !NILP (Fequal (vect[i + 1].definition, definition))
3580 && vect[i].shadowed == vect[i + 1].shadowed)
3581 i++;
3582 end = vect[i].event;
3585 /* Now START .. END is the range to describe next. */
3587 /* Insert the string to describe the event START. */
3588 insert1 (Fkey_description (kludge, prefix));
3590 if (!EQ (start, end))
3592 insert (" .. ", 4);
3594 ASET (kludge, 0, end);
3595 /* Insert the string to describe the character END. */
3596 insert1 (Fkey_description (kludge, prefix));
3599 /* Print a description of the definition of this character.
3600 elt_describer will take care of spacing out far enough
3601 for alignment purposes. */
3602 (*elt_describer) (vect[i].definition, Qnil);
3604 if (vect[i].shadowed)
3606 SET_PT (PT - 1);
3607 insert_string ("\n (that binding is currently shadowed by another mode)");
3608 SET_PT (PT + 1);
3612 UNGCPRO;
3615 static void
3616 describe_vector_princ (elt, fun)
3617 Lisp_Object elt, fun;
3619 Findent_to (make_number (16), make_number (1));
3620 call1 (fun, elt);
3621 Fterpri (Qnil);
3624 DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 2, 0,
3625 doc: /* Insert a description of contents of VECTOR.
3626 This is text showing the elements of vector matched against indices.
3627 DESCRIBER is the output function used; nil means use `princ'. */)
3628 (vector, describer)
3629 Lisp_Object vector, describer;
3631 int count = SPECPDL_INDEX ();
3632 if (NILP (describer))
3633 describer = intern ("princ");
3634 specbind (Qstandard_output, Fcurrent_buffer ());
3635 CHECK_VECTOR_OR_CHAR_TABLE (vector);
3636 describe_vector (vector, Qnil, describer, describe_vector_princ, 0,
3637 Qnil, Qnil, (int *)0, 0, 0, 0);
3639 return unbind_to (count, Qnil);
3642 /* Insert in the current buffer a description of the contents of VECTOR.
3643 We call ELT_DESCRIBER to insert the description of one value found
3644 in VECTOR.
3646 ELT_PREFIX describes what "comes before" the keys or indices defined
3647 by this vector. This is a human-readable string whose size
3648 is not necessarily related to the situation.
3650 If the vector is in a keymap, ELT_PREFIX is a prefix key which
3651 leads to this keymap.
3653 If the vector is a chartable, ELT_PREFIX is the vector
3654 of bytes that lead to the character set or portion of a character
3655 set described by this chartable.
3657 If PARTIAL is nonzero, it means do not mention suppressed commands
3658 (that assumes the vector is in a keymap).
3660 SHADOW is a list of keymaps that shadow this map.
3661 If it is non-nil, then we look up the key in those maps
3662 and we don't mention it now if it is defined by any of them.
3664 ENTIRE_MAP is the keymap in which this vector appears.
3665 If the definition in effect in the whole map does not match
3666 the one in this vector, we ignore this one.
3668 ARGS is simply passed as the second argument to ELT_DESCRIBER.
3670 INDICES and CHAR_TABLE_DEPTH are ignored. They will be removed in
3671 the near future.
3673 KEYMAP_P is 1 if vector is known to be a keymap, so map ESC to M-.
3675 ARGS is simply passed as the second argument to ELT_DESCRIBER. */
3677 static void
3678 describe_vector (vector, prefix, args, elt_describer,
3679 partial, shadow, entire_map,
3680 indices, char_table_depth, keymap_p,
3681 mention_shadow)
3682 register Lisp_Object vector;
3683 Lisp_Object prefix, args;
3684 void (*elt_describer) P_ ((Lisp_Object, Lisp_Object));
3685 int partial;
3686 Lisp_Object shadow;
3687 Lisp_Object entire_map;
3688 int *indices;
3689 int char_table_depth;
3690 int keymap_p;
3691 int mention_shadow;
3693 Lisp_Object definition;
3694 Lisp_Object tem2;
3695 Lisp_Object elt_prefix = Qnil;
3696 int i;
3697 Lisp_Object suppress;
3698 Lisp_Object kludge;
3699 int first = 1;
3700 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
3701 /* Range of elements to be handled. */
3702 int from, to, stop;
3703 Lisp_Object character;
3704 int starting_i;
3706 suppress = Qnil;
3708 definition = Qnil;
3710 if (!keymap_p)
3712 /* Call Fkey_description first, to avoid GC bug for the other string. */
3713 if (!NILP (prefix) && XFASTINT (Flength (prefix)) > 0)
3715 Lisp_Object tem;
3716 tem = Fkey_description (prefix, Qnil);
3717 elt_prefix = concat2 (tem, build_string (" "));
3719 prefix = Qnil;
3722 /* This vector gets used to present single keys to Flookup_key. Since
3723 that is done once per vector element, we don't want to cons up a
3724 fresh vector every time. */
3725 kludge = Fmake_vector (make_number (1), Qnil);
3726 GCPRO4 (elt_prefix, prefix, definition, kludge);
3728 if (partial)
3729 suppress = intern ("suppress-keymap");
3731 from = 0;
3732 if (CHAR_TABLE_P (vector))
3733 stop = MAX_5_BYTE_CHAR + 1, to = MAX_CHAR + 1;
3734 else
3735 stop = to = XVECTOR (vector)->size;
3737 for (i = from; ; i++)
3739 int this_shadowed = 0;
3740 int range_beg, range_end;
3741 Lisp_Object val;
3743 QUIT;
3745 if (i == stop)
3747 if (i == to)
3748 break;
3749 stop = to;
3752 starting_i = i;
3754 if (CHAR_TABLE_P (vector))
3756 range_beg = i;
3757 i = stop - 1;
3758 val = char_table_ref_and_range (vector, range_beg, &range_beg, &i);
3760 else
3761 val = AREF (vector, i);
3762 definition = get_keyelt (val, 0);
3764 if (NILP (definition)) continue;
3766 /* Don't mention suppressed commands. */
3767 if (SYMBOLP (definition) && partial)
3769 Lisp_Object tem;
3771 tem = Fget (definition, suppress);
3773 if (!NILP (tem)) continue;
3776 character = make_number (starting_i);
3777 ASET (kludge, 0, character);
3779 /* If this binding is shadowed by some other map, ignore it. */
3780 if (!NILP (shadow))
3782 Lisp_Object tem;
3784 tem = shadow_lookup (shadow, kludge, Qt);
3786 if (!NILP (tem))
3788 if (mention_shadow)
3789 this_shadowed = 1;
3790 else
3791 continue;
3795 /* Ignore this definition if it is shadowed by an earlier
3796 one in the same keymap. */
3797 if (!NILP (entire_map))
3799 Lisp_Object tem;
3801 tem = Flookup_key (entire_map, kludge, Qt);
3803 if (!EQ (tem, definition))
3804 continue;
3807 if (first)
3809 insert ("\n", 1);
3810 first = 0;
3813 /* Output the prefix that applies to every entry in this map. */
3814 if (!NILP (elt_prefix))
3815 insert1 (elt_prefix);
3817 insert1 (Fkey_description (kludge, prefix));
3819 /* Find all consecutive characters or rows that have the same
3820 definition. But, VECTOR is a char-table, we had better put a
3821 boundary between normal characters (-#x3FFF7F) and 8-bit
3822 characters (#x3FFF80-). */
3823 if (CHAR_TABLE_P (vector))
3825 while (i + 1 < stop
3826 && (range_beg = i + 1, range_end = stop - 1,
3827 val = char_table_ref_and_range (vector, range_beg,
3828 &range_beg, &range_end),
3829 tem2 = get_keyelt (val, 0),
3830 !NILP (tem2))
3831 && !NILP (Fequal (tem2, definition)))
3832 i = range_end;
3834 else
3835 while (i + 1 < stop
3836 && (tem2 = get_keyelt (AREF (vector, i + 1), 0),
3837 !NILP (tem2))
3838 && !NILP (Fequal (tem2, definition)))
3839 i++;
3841 /* If we have a range of more than one character,
3842 print where the range reaches to. */
3844 if (i != starting_i)
3846 insert (" .. ", 4);
3848 ASET (kludge, 0, make_number (i));
3850 if (!NILP (elt_prefix))
3851 insert1 (elt_prefix);
3853 insert1 (Fkey_description (kludge, prefix));
3856 /* Print a description of the definition of this character.
3857 elt_describer will take care of spacing out far enough
3858 for alignment purposes. */
3859 (*elt_describer) (definition, args);
3861 if (this_shadowed)
3863 SET_PT (PT - 1);
3864 insert_string (" (binding currently shadowed)");
3865 SET_PT (PT + 1);
3869 if (CHAR_TABLE_P (vector) && ! NILP (XCHAR_TABLE (vector)->defalt))
3871 if (!NILP (elt_prefix))
3872 insert1 (elt_prefix);
3873 insert ("default", 7);
3874 (*elt_describer) (XCHAR_TABLE (vector)->defalt, args);
3877 UNGCPRO;
3880 /* Apropos - finding all symbols whose names match a regexp. */
3881 static Lisp_Object apropos_predicate;
3882 static Lisp_Object apropos_accumulate;
3884 static void
3885 apropos_accum (symbol, string)
3886 Lisp_Object symbol, string;
3888 register Lisp_Object tem;
3890 tem = Fstring_match (string, Fsymbol_name (symbol), Qnil);
3891 if (!NILP (tem) && !NILP (apropos_predicate))
3892 tem = call1 (apropos_predicate, symbol);
3893 if (!NILP (tem))
3894 apropos_accumulate = Fcons (symbol, apropos_accumulate);
3897 DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0,
3898 doc: /* Show all symbols whose names contain match for REGEXP.
3899 If optional 2nd arg PREDICATE is non-nil, (funcall PREDICATE SYMBOL) is done
3900 for each symbol and a symbol is mentioned only if that returns non-nil.
3901 Return list of symbols found. */)
3902 (regexp, predicate)
3903 Lisp_Object regexp, predicate;
3905 Lisp_Object tem;
3906 CHECK_STRING (regexp);
3907 apropos_predicate = predicate;
3908 apropos_accumulate = Qnil;
3909 map_obarray (Vobarray, apropos_accum, regexp);
3910 tem = Fsort (apropos_accumulate, Qstring_lessp);
3911 apropos_accumulate = Qnil;
3912 apropos_predicate = Qnil;
3913 return tem;
3916 void
3917 syms_of_keymap ()
3919 Qkeymap = intern ("keymap");
3920 staticpro (&Qkeymap);
3921 staticpro (&apropos_predicate);
3922 staticpro (&apropos_accumulate);
3923 apropos_predicate = Qnil;
3924 apropos_accumulate = Qnil;
3926 Qkeymap_canonicalize = intern ("keymap-canonicalize");
3927 staticpro (&Qkeymap_canonicalize);
3929 /* Now we are ready to set up this property, so we can
3930 create char tables. */
3931 Fput (Qkeymap, Qchar_table_extra_slots, make_number (0));
3933 /* Initialize the keymaps standardly used.
3934 Each one is the value of a Lisp variable, and is also
3935 pointed to by a C variable */
3937 global_map = Fmake_keymap (Qnil);
3938 Fset (intern ("global-map"), global_map);
3940 current_global_map = global_map;
3941 staticpro (&global_map);
3942 staticpro (&current_global_map);
3944 meta_map = Fmake_keymap (Qnil);
3945 Fset (intern ("esc-map"), meta_map);
3946 Ffset (intern ("ESC-prefix"), meta_map);
3948 control_x_map = Fmake_keymap (Qnil);
3949 Fset (intern ("ctl-x-map"), control_x_map);
3950 Ffset (intern ("Control-X-prefix"), control_x_map);
3952 exclude_keys
3953 = Fcons (Fcons (build_string ("DEL"), build_string ("\\d")),
3954 Fcons (Fcons (build_string ("TAB"), build_string ("\\t")),
3955 Fcons (Fcons (build_string ("RET"), build_string ("\\r")),
3956 Fcons (Fcons (build_string ("ESC"), build_string ("\\e")),
3957 Fcons (Fcons (build_string ("SPC"), build_string (" ")),
3958 Qnil)))));
3959 staticpro (&exclude_keys);
3961 DEFVAR_LISP ("define-key-rebound-commands", &Vdefine_key_rebound_commands,
3962 doc: /* List of commands given new key bindings recently.
3963 This is used for internal purposes during Emacs startup;
3964 don't alter it yourself. */);
3965 Vdefine_key_rebound_commands = Qt;
3967 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map,
3968 doc: /* Default keymap to use when reading from the minibuffer. */);
3969 Vminibuffer_local_map = Fmake_sparse_keymap (Qnil);
3971 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map,
3972 doc: /* Local keymap for the minibuffer when spaces are not allowed. */);
3973 Vminibuffer_local_ns_map = Fmake_sparse_keymap (Qnil);
3974 Fset_keymap_parent (Vminibuffer_local_ns_map, Vminibuffer_local_map);
3976 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map,
3977 doc: /* Local keymap for minibuffer input with completion. */);
3978 Vminibuffer_local_completion_map = Fmake_sparse_keymap (Qnil);
3979 Fset_keymap_parent (Vminibuffer_local_completion_map, Vminibuffer_local_map);
3981 DEFVAR_LISP ("minibuffer-local-filename-completion-map",
3982 &Vminibuffer_local_filename_completion_map,
3983 doc: /* Local keymap for minibuffer input with completion for filenames. */);
3984 Vminibuffer_local_filename_completion_map = Fmake_sparse_keymap (Qnil);
3985 Fset_keymap_parent (Vminibuffer_local_filename_completion_map,
3986 Vminibuffer_local_completion_map);
3989 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map,
3990 doc: /* Local keymap for minibuffer input with completion, for exact match. */);
3991 Vminibuffer_local_must_match_map = Fmake_sparse_keymap (Qnil);
3992 Fset_keymap_parent (Vminibuffer_local_must_match_map,
3993 Vminibuffer_local_completion_map);
3995 DEFVAR_LISP ("minibuffer-local-filename-must-match-map",
3996 &Vminibuffer_local_filename_must_match_map,
3997 doc: /* Local keymap for minibuffer input with completion for filenames with exact match. */);
3998 Vminibuffer_local_filename_must_match_map = Fmake_sparse_keymap (Qnil);
3999 Fset_keymap_parent (Vminibuffer_local_filename_must_match_map,
4000 Vminibuffer_local_must_match_map);
4002 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist,
4003 doc: /* Alist of keymaps to use for minor modes.
4004 Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read
4005 key sequences and look up bindings if VARIABLE's value is non-nil.
4006 If two active keymaps bind the same key, the keymap appearing earlier
4007 in the list takes precedence. */);
4008 Vminor_mode_map_alist = Qnil;
4010 DEFVAR_LISP ("minor-mode-overriding-map-alist", &Vminor_mode_overriding_map_alist,
4011 doc: /* Alist of keymaps to use for minor modes, in current major mode.
4012 This variable is an alist just like `minor-mode-map-alist', and it is
4013 used the same way (and before `minor-mode-map-alist'); however,
4014 it is provided for major modes to bind locally. */);
4015 Vminor_mode_overriding_map_alist = Qnil;
4017 DEFVAR_LISP ("emulation-mode-map-alists", &Vemulation_mode_map_alists,
4018 doc: /* List of keymap alists to use for emulations modes.
4019 It is intended for modes or packages using multiple minor-mode keymaps.
4020 Each element is a keymap alist just like `minor-mode-map-alist', or a
4021 symbol with a variable binding which is a keymap alist, and it is used
4022 the same way. The "active" keymaps in each alist are used before
4023 `minor-mode-map-alist' and `minor-mode-overriding-map-alist'. */);
4024 Vemulation_mode_map_alists = Qnil;
4026 DEFVAR_LISP ("where-is-preferred-modifier", &Vwhere_is_preferred_modifier,
4027 doc: /* Preferred modifier to use for `where-is'.
4028 When a single binding is requested, `where-is' will return one that
4029 uses this modifier if possible. If nil, or if no such binding exists,
4030 bindings using keys without modifiers (or only with meta) will be
4031 preferred. */);
4032 Vwhere_is_preferred_modifier = Qnil;
4033 where_is_preferred_modifier = 0;
4035 staticpro (&Vmouse_events);
4036 Vmouse_events = Fcons (intern ("menu-bar"),
4037 Fcons (intern ("tool-bar"),
4038 Fcons (intern ("header-line"),
4039 Fcons (intern ("mode-line"),
4040 Fcons (intern ("mouse-1"),
4041 Fcons (intern ("mouse-2"),
4042 Fcons (intern ("mouse-3"),
4043 Fcons (intern ("mouse-4"),
4044 Fcons (intern ("mouse-5"),
4045 Qnil)))))))));
4048 Qsingle_key_description = intern ("single-key-description");
4049 staticpro (&Qsingle_key_description);
4051 Qkey_description = intern ("key-description");
4052 staticpro (&Qkey_description);
4054 Qkeymapp = intern ("keymapp");
4055 staticpro (&Qkeymapp);
4057 Qnon_ascii = intern ("non-ascii");
4058 staticpro (&Qnon_ascii);
4060 Qmenu_item = intern ("menu-item");
4061 staticpro (&Qmenu_item);
4063 Qremap = intern ("remap");
4064 staticpro (&Qremap);
4066 command_remapping_vector = Fmake_vector (make_number (2), Qremap);
4067 staticpro (&command_remapping_vector);
4069 where_is_cache_keymaps = Qt;
4070 where_is_cache = Qnil;
4071 staticpro (&where_is_cache);
4072 staticpro (&where_is_cache_keymaps);
4074 defsubr (&Skeymapp);
4075 defsubr (&Skeymap_parent);
4076 defsubr (&Skeymap_prompt);
4077 defsubr (&Sset_keymap_parent);
4078 defsubr (&Smake_keymap);
4079 defsubr (&Smake_sparse_keymap);
4080 defsubr (&Smap_keymap_internal);
4081 defsubr (&Smap_keymap);
4082 defsubr (&Scopy_keymap);
4083 defsubr (&Scommand_remapping);
4084 defsubr (&Skey_binding);
4085 defsubr (&Slocal_key_binding);
4086 defsubr (&Sglobal_key_binding);
4087 defsubr (&Sminor_mode_key_binding);
4088 defsubr (&Sdefine_key);
4089 defsubr (&Slookup_key);
4090 defsubr (&Sdefine_prefix_command);
4091 defsubr (&Suse_global_map);
4092 defsubr (&Suse_local_map);
4093 defsubr (&Scurrent_local_map);
4094 defsubr (&Scurrent_global_map);
4095 defsubr (&Scurrent_minor_mode_maps);
4096 defsubr (&Scurrent_active_maps);
4097 defsubr (&Saccessible_keymaps);
4098 defsubr (&Skey_description);
4099 defsubr (&Sdescribe_vector);
4100 defsubr (&Ssingle_key_description);
4101 defsubr (&Stext_char_description);
4102 defsubr (&Swhere_is_internal);
4103 defsubr (&Sdescribe_buffer_bindings);
4104 defsubr (&Sapropos_internal);
4107 void
4108 keys_of_keymap ()
4110 initial_define_key (global_map, 033, "ESC-prefix");
4111 initial_define_key (global_map, Ctl ('X'), "Control-X-prefix");
4114 /* arch-tag: 6dd15c26-7cf1-41c4-b904-f42f7ddda463
4115 (do not change this comment) */