0
|
1 /* Manipulation of keymaps
|
|
2 Copyright (C) 1985, 1991-1995 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995 Board of Trustees, University of Illinois.
|
|
4 Copyright (C) 1995 Sun Microsystems, Inc.
|
|
5 Totally redesigned by jwz in 1991.
|
|
6
|
|
7 This file is part of XEmacs.
|
|
8
|
|
9 XEmacs is free software; you can redistribute it and/or modify it
|
|
10 under the terms of the GNU General Public License as published by the
|
|
11 Free Software Foundation; either version 2, or (at your option) any
|
|
12 later version.
|
|
13
|
|
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
17 for more details.
|
|
18
|
|
19 You should have received a copy of the GNU General Public License
|
|
20 along with XEmacs; see the file COPYING. If not, write to
|
|
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
22 Boston, MA 02111-1307, USA. */
|
|
23
|
70
|
24 /* Synched up with: Mule 2.0. Not synched with FSF. Substantially
|
|
25 different from FSF. */
|
|
26
|
0
|
27
|
|
28 #include <config.h>
|
|
29 #include "lisp.h"
|
|
30
|
|
31 #include "buffer.h"
|
|
32 #include "bytecode.h"
|
|
33 #include "commands.h"
|
|
34 #include "console.h"
|
|
35 #include "elhash.h"
|
|
36 #include "events.h"
|
|
37 #include "frame.h"
|
|
38 #include "insdel.h"
|
|
39 #include "keymap.h"
|
|
40 #include "window.h"
|
|
41
|
100
|
42 #ifdef WINDOWSNT
|
|
43 /* Hmm, under unix we want X modifiers, under NT we want X modifiers if
|
|
44 we are running X and Windows modifiers otherwise.
|
|
45 gak. This is a kludge until we support multiple native GUIs!
|
|
46 */
|
|
47 #undef MOD_ALT
|
|
48 #undef MOD_CONTROL
|
|
49 #undef MOD_SHIFT
|
|
50 #endif
|
|
51
|
157
|
52 #include "events-mod.h"
|
100
|
53
|
0
|
54
|
|
55 /* A keymap contains four slots:
|
|
56
|
|
57 parents Ordered list of keymaps to search after
|
|
58 this one if no match is found.
|
|
59 Keymaps can thus be arranged in a hierarchy.
|
|
60
|
|
61 table A hash table, hashing keysyms to their bindings.
|
70
|
62 It will be one of the following:
|
|
63
|
|
64 -- a symbol, e.g. 'home
|
|
65 -- a character, representing something printable
|
|
66 (not ?\C-c meaning C-c, for instance)
|
|
67 -- an integer representing a modifier combination
|
0
|
68
|
|
69 inverse_table A hash table, hashing bindings to the list of keysyms
|
|
70 in this keymap which are bound to them. This is to make
|
|
71 the Fwhere_is_internal() function be fast. It needs to be
|
|
72 fast because we want to be able to call it in realtime to
|
|
73 update the keyboard-equivalents on the pulldown menus.
|
|
74 Values of the table are either atoms (keysyms)
|
|
75 or a dotted list of keysyms.
|
|
76
|
|
77 sub_maps_cache An alist; for each entry in this keymap whose binding is
|
|
78 a keymap (that is, Fkeymapp()) this alist associates that
|
|
79 keysym with that binding. This is used to optimize both
|
|
80 Fwhere_is_internal() and Faccessible_keymaps(). This slot
|
|
81 gets set to the symbol `t' every time a change is made to
|
|
82 this keymap, causing it to be recomputed when next needed.
|
|
83
|
|
84 prompt See `set-keymap-prompt'.
|
|
85
|
|
86 default_binding See `set-keymap-default-binding'.
|
|
87
|
|
88 Sequences of keys are stored in the obvious way: if the sequence of keys
|
|
89 "abc" was bound to some command `foo', the hierarchy would look like
|
|
90
|
|
91 keymap-1: associates "a" with keymap-2
|
|
92 keymap-2: associates "b" with keymap-3
|
|
93 keymap-3: associates "c" with foo
|
|
94
|
|
95 However, bucky bits ("modifiers" to the X-minded) are represented in the
|
|
96 keymap hierarchy as well. (This lets us use EQable objects as hash keys.)
|
|
97 Each combination of modifiers (e.g. control-hyper) gets its own submap
|
|
98 off of the main map. The hash key for a modifier combination is
|
70
|
99 an integer, computed by MAKE_MODIFIER_HASH_KEY().
|
185
|
100
|
0
|
101 If the key `C-a' was bound to some command, the hierarchy would look like
|
|
102
|
70
|
103 keymap-1: associates the integer MOD_CONTROL with keymap-2
|
0
|
104 keymap-2: associates "a" with the command
|
|
105
|
|
106 Similarly, if the key `C-H-a' was bound to some command, the hierarchy
|
|
107 would look like
|
|
108
|
70
|
109 keymap-1: associates the integer (MOD_CONTROL | MOD_HYPER)
|
0
|
110 with keymap-2
|
|
111 keymap-2: associates "a" with the command
|
|
112
|
|
113 Note that a special exception is made for the meta modifier, in order
|
|
114 to deal with ESC/meta lossage. Any key combination containing the
|
|
115 meta modifier is first indexed off of the main map into the meta
|
70
|
116 submap (with hash key MOD_META) and then indexed off of the
|
0
|
117 meta submap with the meta modifier removed from the key combination.
|
|
118 For example, when associating a command with C-M-H-a, we'd have
|
|
119
|
70
|
120 keymap-1: associates the integer MOD_META with keymap-2
|
|
121 keymap-2: associates the integer (MOD_CONTROL | MOD_HYPER)
|
0
|
122 with keymap-3
|
|
123 keymap-3: associates "a" with the command
|
|
124
|
|
125 Note that keymap-2 might have normal bindings in it; these would be
|
|
126 for key combinations containing only the meta modifier, such as
|
|
127 M-y or meta-backspace.
|
|
128
|
|
129 If the command that "a" was bound to in keymap-3 was itself a keymap,
|
|
130 then that would make the key "C-M-H-a" be a prefix character.
|
|
131
|
|
132 Note that this new model of keymaps takes much of the magic away from
|
|
133 the Escape key: the value of the variable `esc-map' is no longer indexed
|
|
134 in the `global-map' under the ESC key. It's indexed under the integer
|
70
|
135 MOD_META. This is not user-visible, however; none of the "bucky"
|
0
|
136 maps are.
|
|
137
|
|
138 There is a hack in Flookup_key() that makes (lookup-key global-map "\^[")
|
|
139 and (define-key some-random-map "\^[" my-esc-map) work as before, for
|
|
140 compatibility.
|
|
141
|
|
142 Since keymaps are opaque, the only way to extract information from them
|
|
143 is with the functions lookup-key, key-binding, local-key-binding, and
|
|
144 global-key-binding, which work just as before, and the new function
|
185
|
145 map-keymap, which is roughly analagous to maphash.
|
0
|
146
|
|
147 Note that map-keymap perpetuates the illusion that the "bucky" submaps
|
|
148 don't exist: if you map over a keymap with bucky submaps, it will also
|
|
149 map over those submaps. It does not, however, map over other random
|
|
150 submaps of the keymap, just the bucky ones.
|
|
151
|
|
152 One implication of this is that when you map over `global-map', you will
|
|
153 also map over `esc-map'. It is merely for compatibility that the esc-map
|
|
154 is accessible at all; I think that's a bad thing, since it blurs the
|
|
155 distinction between ESC and "meta" even more. "M-x" is no more a two-
|
|
156 key sequence than "C-x" is.
|
|
157
|
|
158 */
|
|
159
|
|
160 struct keymap
|
|
161 {
|
|
162 struct lcrecord_header header;
|
|
163 Lisp_Object parents; /* Keymaps to be searched after this one
|
|
164 * An ordered list */
|
|
165 Lisp_Object prompt; /* Qnil or a string to print in the minibuffer
|
|
166 * when reading from this keymap */
|
185
|
167
|
0
|
168 Lisp_Object table; /* The contents of this keymap */
|
|
169 Lisp_Object inverse_table; /* The inverse mapping of the above */
|
|
170
|
|
171 Lisp_Object default_binding; /* Use this if no other binding is found
|
|
172 * (this overrides parent maps and the
|
|
173 * normal global-map lookup). */
|
|
174
|
|
175
|
|
176 Lisp_Object sub_maps_cache; /* Cache of directly inferior keymaps;
|
|
177 This holds an alist, of the key and the
|
|
178 maps, or the modifier bit and the map.
|
|
179 If this is the symbol t, then the cache
|
|
180 needs to be recomputed.
|
|
181 */
|
|
182 int fullness; /* How many entries there are in this table.
|
|
183 This should be the same as the fullness
|
|
184 of the `table', but hash.c is broken. */
|
|
185 Lisp_Object name; /* Just for debugging convenience */
|
|
186 };
|
|
187
|
|
188 #define XKEYMAP(x) XRECORD (x, keymap, struct keymap)
|
|
189 #define XSETKEYMAP(x, p) XSETRECORD (x, p, keymap)
|
|
190 #define KEYMAPP(x) RECORDP (x, keymap)
|
|
191 #define CHECK_KEYMAP(x) CHECK_RECORD (x, keymap)
|
|
192
|
70
|
193 #define MAKE_MODIFIER_HASH_KEY(modifier) make_int (modifier)
|
|
194 #define MODIFIER_HASH_KEY_BITS(x) (INTP (x) ? XINT (x) : 0)
|
0
|
195
|
|
196
|
|
197
|
|
198 /* Actually allocate storage for these variables */
|
|
199
|
|
200 static Lisp_Object Vcurrent_global_map; /* Always a keymap */
|
|
201
|
|
202 static Lisp_Object Vmouse_grabbed_buffer;
|
|
203
|
|
204 /* Alist of minor mode variables and keymaps. */
|
|
205 static Lisp_Object Qminor_mode_map_alist;
|
|
206
|
|
207 static Lisp_Object Voverriding_local_map;
|
|
208
|
|
209 static Lisp_Object Vkey_translation_map;
|
|
210
|
|
211 /* This is incremented whenever a change is made to a keymap. This is
|
|
212 so that things which care (such as the menubar code) can recompute
|
|
213 privately-cached data when the user has changed keybindings.
|
|
214 */
|
|
215 int keymap_tick;
|
|
216
|
|
217 /* Prefixing a key with this character is the same as sending a meta bit. */
|
|
218 Lisp_Object Vmeta_prefix_char;
|
|
219
|
|
220 Lisp_Object Qkeymapp;
|
|
221
|
|
222 Lisp_Object Vsingle_space_string;
|
|
223
|
|
224 Lisp_Object Qsuppress_keymap;
|
|
225
|
|
226 Lisp_Object Qmodeline_map;
|
|
227 Lisp_Object Qtoolbar_map;
|
|
228
|
|
229 static void describe_command (Lisp_Object definition);
|
|
230 static void describe_map (Lisp_Object keymap, Lisp_Object elt_prefix,
|
|
231 void (*elt_describer) (Lisp_Object),
|
185
|
232 int partial,
|
|
233 Lisp_Object shadow,
|
0
|
234 int mice_only_p);
|
|
235 Lisp_Object Qcontrol, Qctrl, Qmeta, Qsuper, Qhyper, Qalt, Qshift;
|
|
236 /* Lisp_Object Qsymbol; defined in general.c */
|
|
237 Lisp_Object Qbutton0, Qbutton1, Qbutton2, Qbutton3, Qbutton4, Qbutton5,
|
|
238 Qbutton6, Qbutton7;
|
|
239 Lisp_Object Qbutton0up, Qbutton1up, Qbutton2up, Qbutton3up, Qbutton4up,
|
|
240 Qbutton5up, Qbutton6up, Qbutton7up;
|
|
241 Lisp_Object Qmenu_selection;
|
98
|
242 /* Emacs compatibility */
|
|
243 Lisp_Object Qdown_mouse_1, Qdown_mouse_2, Qdown_mouse_3;
|
|
244 Lisp_Object Qmouse_1, Qmouse_2, Qmouse_3;
|
0
|
245
|
|
246 /* Kludge kludge kludge */
|
|
247 Lisp_Object QLFD, QTAB, QRET, QESC, QDEL, QSPC, QBS;
|
|
248
|
|
249
|
|
250 /************************************************************************/
|
|
251 /* The keymap Lisp object */
|
|
252 /************************************************************************/
|
|
253
|
|
254 static Lisp_Object mark_keymap (Lisp_Object, void (*) (Lisp_Object));
|
|
255 static void print_keymap (Lisp_Object, Lisp_Object, int);
|
|
256 /* No need for keymap_equal #### Why not? */
|
|
257 DEFINE_LRECORD_IMPLEMENTATION ("keymap", keymap,
|
|
258 mark_keymap, print_keymap, 0, 0, 0,
|
|
259 struct keymap);
|
|
260 static Lisp_Object
|
|
261 mark_keymap (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
262 {
|
|
263 struct keymap *keymap = XKEYMAP (obj);
|
|
264 ((markobj) (keymap->parents));
|
|
265 ((markobj) (keymap->prompt));
|
|
266 ((markobj) (keymap->inverse_table));
|
|
267 ((markobj) (keymap->sub_maps_cache));
|
|
268 ((markobj) (keymap->default_binding));
|
|
269 ((markobj) (keymap->name));
|
173
|
270 return keymap->table;
|
0
|
271 }
|
185
|
272
|
0
|
273 static void
|
|
274 print_keymap (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
275 {
|
|
276 /* This function can GC */
|
|
277 struct keymap *keymap = XKEYMAP (obj);
|
|
278 char buf[200];
|
|
279 int size = XINT (Fkeymap_fullness (obj));
|
|
280 if (print_readably)
|
|
281 error ("printing unreadable object #<keymap 0x%x>", keymap->header.uid);
|
|
282 write_c_string ("#<keymap ", printcharfun);
|
|
283 if (!NILP (keymap->name))
|
|
284 print_internal (keymap->name, printcharfun, 1);
|
|
285 sprintf (buf, "%s%d entr%s 0x%x>",
|
|
286 ((NILP (keymap->name)) ? "" : " "),
|
|
287 size,
|
|
288 ((size == 1) ? "y" : "ies"),
|
|
289 keymap->header.uid);
|
|
290 write_c_string (buf, printcharfun);
|
|
291 }
|
|
292
|
|
293
|
|
294 /************************************************************************/
|
|
295 /* Traversing keymaps and their parents */
|
|
296 /************************************************************************/
|
|
297
|
|
298 static Lisp_Object
|
|
299 traverse_keymaps (Lisp_Object start_keymap, Lisp_Object start_parents,
|
|
300 Lisp_Object (*mapper) (Lisp_Object keymap, void *mapper_arg),
|
|
301 void *mapper_arg)
|
|
302 {
|
|
303 /* This function can GC */
|
|
304 Lisp_Object keymap;
|
|
305 Lisp_Object tail = start_parents;
|
|
306 Lisp_Object malloc_sucks[10];
|
|
307 Lisp_Object malloc_bites = Qnil;
|
|
308 int stack_depth = 0;
|
|
309 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
|
310 GCPRO4 (*malloc_sucks, malloc_bites, start_keymap, tail);
|
|
311 gcpro1.nvars = 0;
|
|
312
|
|
313 start_keymap = get_keymap (start_keymap, 1, 1);
|
|
314 keymap = start_keymap;
|
|
315 /* Hack special-case parents at top-level */
|
|
316 tail = ((!NILP (tail)) ? tail : XKEYMAP (keymap)->parents);
|
|
317
|
|
318 for (;;)
|
|
319 {
|
|
320 Lisp_Object result;
|
|
321
|
|
322 QUIT;
|
|
323 result = ((mapper) (keymap, mapper_arg));
|
|
324 if (!NILP (result))
|
|
325 {
|
|
326 while (CONSP (malloc_bites))
|
|
327 {
|
|
328 struct Lisp_Cons *victim = XCONS (malloc_bites);
|
|
329 malloc_bites = victim->cdr;
|
|
330 free_cons (victim);
|
|
331 }
|
|
332 UNGCPRO;
|
173
|
333 return result;
|
0
|
334 }
|
|
335 if (NILP (tail))
|
|
336 {
|
|
337 if (stack_depth == 0)
|
|
338 {
|
|
339 UNGCPRO;
|
173
|
340 return Qnil; /* Nothing found */
|
0
|
341 }
|
|
342 stack_depth--;
|
|
343 if (CONSP (malloc_bites))
|
|
344 {
|
|
345 struct Lisp_Cons *victim = XCONS (malloc_bites);
|
|
346 tail = victim->car;
|
|
347 malloc_bites = victim->cdr;
|
|
348 free_cons (victim);
|
|
349 }
|
|
350 else
|
|
351 {
|
|
352 tail = malloc_sucks[stack_depth];
|
|
353 gcpro1.nvars = stack_depth;
|
|
354 }
|
|
355 keymap = XCAR (tail);
|
|
356 tail = XCDR (tail);
|
|
357 }
|
|
358 else
|
|
359 {
|
|
360 Lisp_Object parents;
|
|
361
|
|
362 keymap = XCAR (tail);
|
|
363 tail = XCDR (tail);
|
|
364 parents = XKEYMAP (keymap)->parents;
|
|
365 if (!CONSP (parents))
|
|
366 ;
|
|
367 else if (NILP (tail))
|
|
368 /* Tail-recurse */
|
|
369 tail = parents;
|
|
370 else
|
|
371 {
|
|
372 if (CONSP (malloc_bites))
|
|
373 malloc_bites = noseeum_cons (tail, malloc_bites);
|
|
374 else if (stack_depth < countof (malloc_sucks))
|
|
375 {
|
|
376 malloc_sucks[stack_depth++] = tail;
|
|
377 gcpro1.nvars = stack_depth;
|
|
378 }
|
|
379 else
|
|
380 {
|
|
381 /* *&@##[*&^$ C. @#[$*&@# Unix. Losers all. */
|
|
382 int i;
|
|
383 for (i = 0, malloc_bites = Qnil;
|
|
384 i < countof (malloc_sucks);
|
|
385 i++)
|
|
386 malloc_bites = noseeum_cons (malloc_sucks[i],
|
|
387 malloc_bites);
|
|
388 gcpro1.nvars = 0;
|
|
389 }
|
|
390 tail = parents;
|
|
391 }
|
|
392 }
|
|
393 keymap = get_keymap (keymap, 1, 1);
|
|
394 if (EQ (keymap, start_keymap))
|
|
395 {
|
|
396 signal_simple_error ("Cyclic keymap indirection",
|
|
397 start_keymap);
|
|
398 }
|
|
399 }
|
|
400 }
|
|
401
|
|
402
|
|
403 /************************************************************************/
|
|
404 /* Some low-level functions */
|
|
405 /************************************************************************/
|
|
406
|
|
407 static unsigned int
|
|
408 bucky_sym_to_bucky_bit (Lisp_Object sym)
|
|
409 {
|
185
|
410 if (EQ (sym, Qcontrol)) return MOD_CONTROL;
|
|
411 if (EQ (sym, Qmeta)) return MOD_META;
|
|
412 if (EQ (sym, Qsuper)) return MOD_SUPER;
|
|
413 if (EQ (sym, Qhyper)) return MOD_HYPER;
|
|
414 if (EQ (sym, Qalt)) return MOD_ALT;
|
|
415 if (EQ (sym, Qsymbol)) return MOD_ALT; /* #### - reverse compat */
|
|
416 if (EQ (sym, Qshift)) return MOD_SHIFT;
|
|
417
|
|
418 return 0;
|
0
|
419 }
|
|
420
|
|
421 static Lisp_Object
|
|
422 control_meta_superify (Lisp_Object frob, unsigned int modifiers)
|
|
423 {
|
|
424 if (modifiers == 0)
|
|
425 return frob;
|
|
426 frob = Fcons (frob, Qnil);
|
173
|
427 if (modifiers & MOD_SHIFT) frob = Fcons (Qshift, frob);
|
|
428 if (modifiers & MOD_ALT) frob = Fcons (Qalt, frob);
|
|
429 if (modifiers & MOD_HYPER) frob = Fcons (Qhyper, frob);
|
|
430 if (modifiers & MOD_SUPER) frob = Fcons (Qsuper, frob);
|
|
431 if (modifiers & MOD_CONTROL) frob = Fcons (Qcontrol, frob);
|
|
432 if (modifiers & MOD_META) frob = Fcons (Qmeta, frob);
|
|
433 return frob;
|
0
|
434 }
|
|
435
|
|
436 static Lisp_Object
|
|
437 make_key_description (CONST struct key_data *key, int prettify)
|
|
438 {
|
|
439 Lisp_Object keysym = key->keysym;
|
|
440 unsigned int modifiers = key->modifiers;
|
|
441
|
70
|
442 if (prettify && CHARP (keysym))
|
0
|
443 {
|
|
444 /* This is a little slow, but (control a) is prettier than (control 65).
|
|
445 It's now ok to do this for digit-chars too, since we've fixed the
|
|
446 bug where \9 read as the integer 9 instead of as the symbol with
|
|
447 "9" as its name.
|
|
448 */
|
|
449 /* !!#### I'm not sure how correct this is. */
|
|
450 Bufbyte str [1 + MAX_EMCHAR_LEN];
|
|
451 Bytecount count = set_charptr_emchar (str, XCHAR (keysym));
|
|
452 str[count] = 0;
|
|
453 keysym = intern ((char *) str);
|
|
454 }
|
173
|
455 return control_meta_superify (keysym, modifiers);
|
0
|
456 }
|
|
457
|
|
458
|
|
459 /************************************************************************/
|
|
460 /* Low-level keymap-store functions */
|
|
461 /************************************************************************/
|
|
462
|
|
463 static Lisp_Object
|
|
464 raw_lookup_key (Lisp_Object keymap,
|
|
465 CONST struct key_data *raw_keys, int raw_keys_count,
|
|
466 int keys_so_far, int accept_default);
|
|
467
|
|
468 /* Relies on caller to gc-protect args */
|
|
469 static Lisp_Object
|
|
470 keymap_lookup_directly (Lisp_Object keymap,
|
|
471 Lisp_Object keysym, unsigned int modifiers)
|
|
472 {
|
|
473 struct keymap *k;
|
|
474
|
|
475 if ((modifiers & ~(MOD_CONTROL | MOD_META | MOD_SUPER | MOD_HYPER
|
|
476 | MOD_ALT | MOD_SHIFT)) != 0)
|
|
477 abort ();
|
|
478
|
|
479 k = XKEYMAP (keymap);
|
|
480
|
|
481 /* If the keysym is a one-character symbol, use the char code instead. */
|
|
482 if (SYMBOLP (keysym) && string_char_length (XSYMBOL (keysym)->name) == 1)
|
|
483 {
|
|
484 Lisp_Object i_fart_on_gcc =
|
|
485 make_char (string_char (XSYMBOL (keysym)->name, 0));
|
|
486 keysym = i_fart_on_gcc;
|
|
487 }
|
|
488
|
|
489 if (modifiers & MOD_META) /* Utterly hateful ESC lossage */
|
2
|
490 {
|
|
491 Lisp_Object submap = Fgethash (MAKE_MODIFIER_HASH_KEY (MOD_META),
|
|
492 k->table, Qnil);
|
|
493 if (NILP (submap))
|
173
|
494 return Qnil;
|
2
|
495 k = XKEYMAP (submap);
|
|
496 modifiers &= ~MOD_META;
|
|
497 }
|
0
|
498
|
|
499 if (modifiers != 0)
|
2
|
500 {
|
|
501 Lisp_Object submap = Fgethash (MAKE_MODIFIER_HASH_KEY (modifiers),
|
|
502 k->table, Qnil);
|
|
503 if (NILP (submap))
|
173
|
504 return Qnil;
|
2
|
505 k = XKEYMAP (submap);
|
|
506 }
|
173
|
507 return Fgethash (keysym, k->table, Qnil);
|
0
|
508 }
|
|
509
|
|
510 static void
|
|
511 keymap_store_inverse_internal (Lisp_Object inverse_table,
|
|
512 Lisp_Object keysym,
|
|
513 Lisp_Object value)
|
|
514 {
|
|
515 Lisp_Object keys = Fgethash (value, inverse_table, Qunbound);
|
|
516
|
|
517 if (UNBOUNDP (keys))
|
|
518 {
|
|
519 keys = keysym;
|
|
520 /* Don't cons this unless necessary */
|
|
521 /* keys = Fcons (keysym, Qnil); */
|
|
522 Fputhash (value, keys, inverse_table);
|
|
523 }
|
|
524 else if (!CONSP (keys))
|
|
525 {
|
|
526 /* Now it's necessary to cons */
|
|
527 keys = Fcons (keys, keysym);
|
|
528 Fputhash (value, keys, inverse_table);
|
|
529 }
|
|
530 else
|
|
531 {
|
|
532 while (CONSP (Fcdr (keys)))
|
|
533 keys = XCDR (keys);
|
|
534 XCDR (keys) = Fcons (XCDR (keys), keysym);
|
|
535 /* No need to call puthash because we've destructively
|
|
536 modified the list tail in place */
|
|
537 }
|
|
538 }
|
|
539
|
|
540
|
|
541 static void
|
|
542 keymap_delete_inverse_internal (Lisp_Object inverse_table,
|
185
|
543 Lisp_Object keysym,
|
0
|
544 Lisp_Object value)
|
|
545 {
|
|
546 Lisp_Object keys = Fgethash (value, inverse_table, Qunbound);
|
|
547 Lisp_Object new_keys = keys;
|
|
548 Lisp_Object tail;
|
|
549 Lisp_Object *prev;
|
|
550
|
|
551 if (UNBOUNDP (keys))
|
|
552 abort ();
|
|
553
|
|
554 for (prev = &new_keys, tail = new_keys;
|
|
555 ;
|
|
556 prev = &(XCDR (tail)), tail = XCDR (tail))
|
|
557 {
|
|
558 if (EQ (tail, keysym))
|
|
559 {
|
|
560 *prev = Qnil;
|
|
561 break;
|
|
562 }
|
|
563 else if (EQ (keysym, XCAR (tail)))
|
|
564 {
|
|
565 *prev = XCDR (tail);
|
|
566 break;
|
|
567 }
|
|
568 }
|
|
569
|
|
570 if (NILP (new_keys))
|
|
571 Fremhash (value, inverse_table);
|
|
572 else if (!EQ (keys, new_keys))
|
|
573 /* Removed the first elt */
|
|
574 Fputhash (value, new_keys, inverse_table);
|
|
575 /* else the list's tail has been modified, so we don't need to
|
|
576 touch the hash table again (the pointer in there is ok).
|
|
577 */
|
|
578 }
|
|
579
|
|
580
|
|
581 static void
|
|
582 keymap_store_internal (Lisp_Object keysym, struct keymap *keymap,
|
|
583 Lisp_Object value)
|
|
584 {
|
|
585 Lisp_Object prev_value = Fgethash (keysym, keymap->table, Qnil);
|
|
586
|
|
587 if (EQ (prev_value, value))
|
|
588 return;
|
|
589 if (!NILP (prev_value))
|
185
|
590 keymap_delete_inverse_internal (keymap->inverse_table,
|
0
|
591 keysym, prev_value);
|
|
592 if (NILP (value))
|
|
593 {
|
|
594 keymap->fullness--;
|
|
595 if (keymap->fullness < 0) abort ();
|
|
596 Fremhash (keysym, keymap->table);
|
|
597 }
|
|
598 else
|
|
599 {
|
|
600 if (NILP (prev_value))
|
|
601 keymap->fullness++;
|
|
602 Fputhash (keysym, value, keymap->table);
|
185
|
603 keymap_store_inverse_internal (keymap->inverse_table,
|
0
|
604 keysym, value);
|
|
605 }
|
|
606 keymap_tick++;
|
|
607 }
|
|
608
|
|
609
|
|
610 static Lisp_Object
|
|
611 create_bucky_submap (struct keymap *k, unsigned int modifiers,
|
|
612 Lisp_Object parent_for_debugging_info)
|
|
613 {
|
|
614 Lisp_Object submap = Fmake_sparse_keymap (Qnil);
|
|
615 /* User won't see this, but it is nice for debugging Emacs */
|
|
616 XKEYMAP (submap)->name
|
|
617 = control_meta_superify (parent_for_debugging_info, modifiers);
|
|
618 /* Invalidate cache */
|
|
619 k->sub_maps_cache = Qt;
|
|
620 keymap_store_internal (MAKE_MODIFIER_HASH_KEY (modifiers), k, submap);
|
173
|
621 return submap;
|
0
|
622 }
|
|
623
|
|
624
|
|
625 /* Relies on caller to gc-protect keymap, keysym, value */
|
|
626 static void
|
|
627 keymap_store (Lisp_Object keymap, CONST struct key_data *key,
|
|
628 Lisp_Object value)
|
|
629 {
|
|
630 Lisp_Object keysym = key->keysym;
|
|
631 unsigned int modifiers = key->modifiers;
|
|
632 struct keymap *k;
|
|
633
|
|
634 if ((modifiers & ~(MOD_CONTROL | MOD_META | MOD_SUPER | MOD_HYPER
|
|
635 | MOD_ALT | MOD_SHIFT)) != 0)
|
|
636 abort ();
|
|
637
|
|
638 k = XKEYMAP (keymap);
|
|
639
|
|
640 /* If the keysym is a one-character symbol, use the char code instead. */
|
|
641 if (SYMBOLP (keysym) && string_char_length (XSYMBOL (keysym)->name) == 1)
|
|
642 {
|
|
643 Lisp_Object run_the_gcc_developers_over_with_a_steamroller =
|
|
644 make_char (string_char (XSYMBOL (keysym)->name, 0));
|
|
645 keysym = run_the_gcc_developers_over_with_a_steamroller;
|
|
646 }
|
|
647
|
|
648 if (modifiers & MOD_META) /* Utterly hateful ESC lossage */
|
|
649 {
|
|
650 Lisp_Object submap = Fgethash (MAKE_MODIFIER_HASH_KEY (MOD_META),
|
|
651 k->table, Qnil);
|
|
652 if (NILP (submap))
|
|
653 submap = create_bucky_submap (k, MOD_META, keymap);
|
|
654 k = XKEYMAP (submap);
|
|
655 modifiers &= ~MOD_META;
|
|
656 }
|
|
657
|
|
658 if (modifiers != 0)
|
|
659 {
|
|
660 Lisp_Object submap = Fgethash (MAKE_MODIFIER_HASH_KEY (modifiers),
|
|
661 k->table, Qnil);
|
|
662 if (NILP (submap))
|
|
663 submap = create_bucky_submap (k, modifiers, keymap);
|
|
664 k = XKEYMAP (submap);
|
|
665 }
|
|
666 k->sub_maps_cache = Qt; /* Invalidate cache */
|
|
667 keymap_store_internal (keysym, k, value);
|
|
668 }
|
|
669
|
|
670
|
|
671 /************************************************************************/
|
|
672 /* Listing the submaps of a keymap */
|
|
673 /************************************************************************/
|
|
674
|
|
675 struct keymap_submaps_closure
|
|
676 {
|
|
677 Lisp_Object *result_locative;
|
|
678 };
|
|
679
|
|
680 static void
|
185
|
681 keymap_submaps_mapper_0 (CONST void *hash_key, void *hash_contents,
|
0
|
682 void *keymap_submaps_closure)
|
|
683 {
|
|
684 /* This function can GC */
|
|
685 Lisp_Object contents;
|
|
686 VOID_TO_LISP (contents, hash_contents);
|
|
687 /* Perform any autoloads, etc */
|
183
|
688 Fkeymapp (contents);
|
0
|
689 }
|
|
690
|
|
691 static void
|
185
|
692 keymap_submaps_mapper (CONST void *hash_key, void *hash_contents,
|
0
|
693 void *keymap_submaps_closure)
|
|
694 {
|
|
695 /* This function can GC */
|
|
696 Lisp_Object key, contents;
|
|
697 Lisp_Object *result_locative;
|
185
|
698 struct keymap_submaps_closure *cl =
|
|
699 (struct keymap_submaps_closure *) keymap_submaps_closure;
|
0
|
700 CVOID_TO_LISP (key, hash_key);
|
|
701 VOID_TO_LISP (contents, hash_contents);
|
|
702 result_locative = cl->result_locative;
|
|
703
|
|
704 if (!NILP (Fkeymapp (contents)))
|
|
705 *result_locative = Fcons (Fcons (key, contents), *result_locative);
|
|
706 }
|
|
707
|
185
|
708 static int map_keymap_sort_predicate (Lisp_Object obj1, Lisp_Object obj2,
|
0
|
709 Lisp_Object pred);
|
|
710
|
|
711 static Lisp_Object
|
|
712 keymap_submaps (Lisp_Object keymap)
|
|
713 {
|
|
714 /* This function can GC */
|
|
715 struct keymap *k = XKEYMAP (keymap);
|
|
716
|
|
717 if (EQ (k->sub_maps_cache, Qt)) /* Unknown */
|
|
718 {
|
|
719 Lisp_Object result = Qnil;
|
|
720 struct gcpro gcpro1, gcpro2;
|
|
721 struct keymap_submaps_closure keymap_submaps_closure;
|
|
722
|
|
723 GCPRO2 (keymap, result);
|
|
724 keymap_submaps_closure.result_locative = &result;
|
|
725 /* Do this first pass to touch (and load) any autoloaded maps */
|
|
726 elisp_maphash (keymap_submaps_mapper_0, k->table,
|
|
727 &keymap_submaps_closure);
|
|
728 result = Qnil;
|
|
729 elisp_maphash (keymap_submaps_mapper, k->table,
|
|
730 &keymap_submaps_closure);
|
|
731 /* keep it sorted so that the result of accessible-keymaps is ordered */
|
185
|
732 k->sub_maps_cache = list_sort (result,
|
0
|
733 Qnil,
|
|
734 map_keymap_sort_predicate);
|
|
735 UNGCPRO;
|
|
736 }
|
173
|
737 return k->sub_maps_cache;
|
0
|
738 }
|
|
739
|
|
740
|
|
741 /************************************************************************/
|
|
742 /* Basic operations on keymaps */
|
|
743 /************************************************************************/
|
|
744
|
|
745 static Lisp_Object
|
|
746 make_keymap (int size)
|
|
747 {
|
|
748 Lisp_Object result = Qnil;
|
185
|
749 struct keymap *keymap = alloc_lcrecord_type (struct keymap, lrecord_keymap);
|
0
|
750
|
|
751 XSETKEYMAP (result, keymap);
|
|
752
|
|
753 keymap->parents = Qnil;
|
|
754 keymap->table = Qnil;
|
|
755 keymap->prompt = Qnil;
|
|
756 keymap->default_binding = Qnil;
|
|
757 keymap->inverse_table = Qnil;
|
|
758 keymap->sub_maps_cache = Qnil; /* No possible submaps */
|
|
759 keymap->fullness = 0;
|
|
760 if (size != 0) /* hack for copy-keymap */
|
|
761 {
|
|
762 keymap->table = Fmake_hashtable (make_int (size), Qnil);
|
|
763 /* Inverse table is often less dense because of duplicate key-bindings.
|
|
764 If not, it will grow anyway. */
|
|
765 keymap->inverse_table = Fmake_hashtable (make_int (size * 3 / 4), Qnil);
|
|
766 }
|
|
767 keymap->name = Qnil;
|
173
|
768 return result;
|
0
|
769 }
|
|
770
|
20
|
771 DEFUN ("make-keymap", Fmake_keymap, 0, 1, 0, /*
|
0
|
772 Construct and return a new keymap object.
|
185
|
773 All entries in it are nil, meaning "command undefined".
|
0
|
774
|
|
775 Optional argument NAME specifies a name to assign to the keymap,
|
|
776 as in `set-keymap-name'. This name is only a debugging convenience;
|
|
777 it is not used except when printing the keymap.
|
20
|
778 */
|
|
779 (name))
|
0
|
780 {
|
|
781 Lisp_Object keymap = make_keymap (60);
|
|
782 if (!NILP (name))
|
|
783 Fset_keymap_name (keymap, name);
|
|
784 return keymap;
|
|
785 }
|
|
786
|
20
|
787 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, 0, 1, 0, /*
|
0
|
788 Construct and return a new keymap object.
|
185
|
789 All entries in it are nil, meaning "command undefined". The only
|
0
|
790 difference between this function and make-keymap is that this function
|
185
|
791 returns a "smaller" keymap (one that is expected to contain fewer
|
0
|
792 entries). As keymaps dynamically resize, the distinction is not great.
|
|
793
|
|
794 Optional argument NAME specifies a name to assign to the keymap,
|
|
795 as in `set-keymap-name'. This name is only a debugging convenience;
|
|
796 it is not used except when printing the keymap.
|
20
|
797 */
|
|
798 (name))
|
0
|
799 {
|
|
800 Lisp_Object keymap = make_keymap (8);
|
|
801 if (!NILP (name))
|
|
802 Fset_keymap_name (keymap, name);
|
|
803 return keymap;
|
|
804 }
|
|
805
|
20
|
806 DEFUN ("keymap-parents", Fkeymap_parents, 1, 1, 0, /*
|
0
|
807 Return the `parent' keymaps of the given keymap, or nil.
|
|
808 The parents of a keymap are searched for keybindings when a key sequence
|
|
809 isn't bound in this one. `(current-global-map)' is the default parent
|
|
810 of all keymaps.
|
20
|
811 */
|
|
812 (keymap))
|
0
|
813 {
|
|
814 keymap = get_keymap (keymap, 1, 1);
|
173
|
815 return Fcopy_sequence (XKEYMAP (keymap)->parents);
|
0
|
816 }
|
|
817
|
185
|
818
|
|
819
|
0
|
820 static Lisp_Object
|
|
821 traverse_keymaps_noop (Lisp_Object keymap, void *arg)
|
|
822 {
|
173
|
823 return Qnil;
|
0
|
824 }
|
|
825
|
20
|
826 DEFUN ("set-keymap-parents", Fset_keymap_parents, 2, 2, 0, /*
|
0
|
827 Sets the `parent' keymaps of the given keymap.
|
|
828 The parents of a keymap are searched for keybindings when a key sequence
|
|
829 isn't bound in this one. `(current-global-map)' is the default parent
|
|
830 of all keymaps.
|
20
|
831 */
|
|
832 (keymap, parents))
|
0
|
833 {
|
|
834 /* This function can GC */
|
|
835 Lisp_Object k;
|
|
836 struct gcpro gcpro1, gcpro2;
|
|
837
|
|
838 GCPRO2 (keymap, parents);
|
|
839 keymap = get_keymap (keymap, 1, 1);
|
|
840
|
|
841 if (KEYMAPP (parents)) /* backwards-compatibility */
|
|
842 parents = list1 (parents);
|
|
843 if (!NILP (parents))
|
|
844 {
|
|
845 Lisp_Object tail = parents;
|
|
846 while (!NILP (tail))
|
|
847 {
|
|
848 QUIT;
|
|
849 CHECK_CONS (tail);
|
|
850 k = XCAR (tail);
|
|
851 /* Require that it be an actual keymap object, rather than a symbol
|
|
852 with a (crockish) symbol-function which is a keymap */
|
|
853 CHECK_KEYMAP (k); /* get_keymap (k, 1, 1); */
|
|
854 tail = XCDR (tail);
|
|
855 }
|
|
856 }
|
|
857
|
|
858 /* Check for circularities */
|
|
859 traverse_keymaps (keymap, parents, traverse_keymaps_noop, 0);
|
|
860 keymap_tick++;
|
|
861 XKEYMAP (keymap)->parents = Fcopy_sequence (parents);
|
|
862 UNGCPRO;
|
173
|
863 return parents;
|
0
|
864 }
|
|
865
|
20
|
866 DEFUN ("set-keymap-name", Fset_keymap_name, 2, 2, 0, /*
|
0
|
867 Set the `name' of the KEYMAP to NEW-NAME.
|
|
868 The name is only a debugging convenience; it is not used except
|
|
869 when printing the keymap.
|
20
|
870 */
|
|
871 (keymap, new_name))
|
0
|
872 {
|
|
873 keymap = get_keymap (keymap, 1, 1);
|
|
874
|
|
875 XKEYMAP (keymap)->name = new_name;
|
173
|
876 return new_name;
|
0
|
877 }
|
|
878
|
20
|
879 DEFUN ("keymap-name", Fkeymap_name, 1, 1, 0, /*
|
0
|
880 Return the `name' of KEYMAP.
|
|
881 The name is only a debugging convenience; it is not used except
|
|
882 when printing the keymap.
|
20
|
883 */
|
|
884 (keymap))
|
0
|
885 {
|
|
886 keymap = get_keymap (keymap, 1, 1);
|
|
887
|
173
|
888 return XKEYMAP (keymap)->name;
|
0
|
889 }
|
|
890
|
20
|
891 DEFUN ("set-keymap-prompt", Fset_keymap_prompt, 2, 2, 0, /*
|
0
|
892 Sets the `prompt' of KEYMAP to string NEW-PROMPT, or `nil'
|
|
893 if no prompt is desired. The prompt is shown in the echo-area
|
|
894 when reading a key-sequence to be looked-up in this keymap.
|
20
|
895 */
|
|
896 (keymap, new_prompt))
|
0
|
897 {
|
|
898 keymap = get_keymap (keymap, 1, 1);
|
185
|
899
|
0
|
900 if (!NILP (new_prompt))
|
|
901 CHECK_STRING (new_prompt);
|
|
902
|
|
903 XKEYMAP (keymap)->prompt = new_prompt;
|
173
|
904 return new_prompt;
|
0
|
905 }
|
|
906
|
|
907 static Lisp_Object
|
|
908 keymap_prompt_mapper (Lisp_Object keymap, void *arg)
|
|
909 {
|
173
|
910 return XKEYMAP (keymap)->prompt;
|
0
|
911 }
|
|
912
|
|
913
|
20
|
914 DEFUN ("keymap-prompt", Fkeymap_prompt, 1, 2, 0, /*
|
0
|
915 Return the `prompt' of the given keymap.
|
|
916 If non-nil, the prompt is shown in the echo-area
|
|
917 when reading a key-sequence to be looked-up in this keymap.
|
20
|
918 */
|
|
919 (keymap, use_inherited))
|
0
|
920 {
|
|
921 /* This function can GC */
|
|
922 Lisp_Object prompt;
|
|
923
|
|
924 keymap = get_keymap (keymap, 1, 1);
|
|
925 prompt = XKEYMAP (keymap)->prompt;
|
|
926 if (!NILP (prompt) || NILP (use_inherited))
|
173
|
927 return prompt;
|
0
|
928 else
|
173
|
929 return traverse_keymaps (keymap, Qnil, keymap_prompt_mapper, 0);
|
0
|
930 }
|
|
931
|
20
|
932 DEFUN ("set-keymap-default-binding", Fset_keymap_default_binding, 2, 2, 0, /*
|
0
|
933 Sets the default binding of KEYMAP to COMMAND, or `nil'
|
|
934 if no default is desired. The default-binding is returned when
|
|
935 no other binding for a key-sequence is found in the keymap.
|
|
936 If a keymap has a non-nil default-binding, neither the keymap's
|
|
937 parents nor the current global map are searched for key bindings.
|
20
|
938 */
|
|
939 (keymap, command))
|
0
|
940 {
|
|
941 /* This function can GC */
|
|
942 keymap = get_keymap (keymap, 1, 1);
|
185
|
943
|
0
|
944 XKEYMAP (keymap)->default_binding = command;
|
173
|
945 return command;
|
0
|
946 }
|
|
947
|
20
|
948 DEFUN ("keymap-default-binding", Fkeymap_default_binding, 1, 1, 0, /*
|
0
|
949 Return the default binding of KEYMAP, or `nil' if it has none.
|
|
950 The default-binding is returned when no other binding for a key-sequence
|
|
951 is found in the keymap.
|
|
952 If a keymap has a non-nil default-binding, neither the keymap's
|
|
953 parents nor the current global map are searched for key bindings.
|
20
|
954 */
|
|
955 (keymap))
|
0
|
956 {
|
|
957 /* This function can GC */
|
|
958 keymap = get_keymap (keymap, 1, 1);
|
173
|
959 return XKEYMAP (keymap)->default_binding;
|
0
|
960 }
|
|
961
|
20
|
962 DEFUN ("keymapp", Fkeymapp, 1, 1, 0, /*
|
0
|
963 Return t if ARG is a keymap object.
|
|
964 The keymap may be autoloaded first if necessary.
|
20
|
965 */
|
|
966 (object))
|
0
|
967 {
|
|
968 /* This function can GC */
|
185
|
969 return KEYMAPP (get_keymap (object, 0, 0)) ? Qt : Qnil;
|
0
|
970 }
|
|
971
|
|
972 /* Check that OBJECT is a keymap (after dereferencing through any
|
|
973 symbols). If it is, return it.
|
|
974
|
|
975 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
|
|
976 is an autoload form, do the autoload and try again.
|
185
|
977 If AUTOLOAD is nonzero, callers must assume GC is possible.
|
0
|
978
|
|
979 ERRORP controls how we respond if OBJECT isn't a keymap.
|
185
|
980 If ERRORP is non-zero, signal an error; otherwise, just return Qnil.
|
|
981
|
|
982 Note that most of the time, we don't want to pursue autoloads.
|
|
983 Functions like Faccessible_keymaps which scan entire keymap trees
|
|
984 shouldn't load every autoloaded keymap. I'm not sure about this,
|
|
985 but it seems to me that only read_key_sequence, Flookup_key, and
|
|
986 Fdefine_key should cause keymaps to be autoloaded. */
|
|
987
|
0
|
988 Lisp_Object
|
|
989 get_keymap (Lisp_Object object, int errorp, int autoload)
|
|
990 {
|
|
991 /* This function can GC */
|
|
992 while (1)
|
|
993 {
|
|
994 Lisp_Object tem = indirect_function (object, 0);
|
185
|
995
|
0
|
996 if (KEYMAPP (tem))
|
|
997 return tem;
|
|
998 /* Should we do an autoload? */
|
|
999 else if (autoload
|
|
1000 /* (autoload "filename" doc nil keymap) */
|
|
1001 && SYMBOLP (object)
|
|
1002 && CONSP (tem)
|
|
1003 && EQ (XCAR (tem), Qautoload)
|
|
1004 && EQ (Fcar (Fcdr (Fcdr (Fcdr (Fcdr (tem))))), Qkeymap))
|
|
1005 {
|
|
1006 struct gcpro gcpro1, gcpro2;
|
|
1007 GCPRO2 (tem, object);
|
|
1008 do_autoload (tem, object);
|
|
1009 UNGCPRO;
|
|
1010 }
|
|
1011 else if (errorp)
|
|
1012 object = wrong_type_argument (Qkeymapp, object);
|
|
1013 else
|
|
1014 return Qnil;
|
|
1015 }
|
|
1016 }
|
|
1017
|
|
1018 /* Given OBJECT which was found in a slot in a keymap,
|
|
1019 trace indirect definitions to get the actual definition of that slot.
|
|
1020 An indirect definition is a list of the form
|
|
1021 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
|
|
1022 and INDEX is an ASCII code, or a cons of (KEYSYM . MODIFIERS).
|
|
1023 */
|
|
1024 static Lisp_Object
|
|
1025 get_keyelt (Lisp_Object object, int accept_default)
|
|
1026 {
|
|
1027 /* This function can GC */
|
|
1028 Lisp_Object map;
|
|
1029
|
|
1030 tail_recurse:
|
|
1031 if (!CONSP (object))
|
173
|
1032 return object;
|
0
|
1033
|
|
1034 {
|
|
1035 struct gcpro gcpro1;
|
|
1036 GCPRO1 (object);
|
|
1037 map = XCAR (object);
|
|
1038 map = get_keymap (map, 0, 1);
|
|
1039 UNGCPRO;
|
|
1040 }
|
|
1041 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
|
|
1042 if (!NILP (map))
|
|
1043 {
|
|
1044 Lisp_Object idx = Fcdr (object);
|
|
1045 struct key_data indirection;
|
|
1046 if (CHARP (idx))
|
|
1047 {
|
|
1048 struct Lisp_Event event;
|
|
1049 event.event_type = empty_event;
|
|
1050 character_to_event (XCHAR (idx), &event,
|
|
1051 XCONSOLE (Vselected_console), 0);
|
|
1052 indirection = event.event.key;
|
|
1053 }
|
|
1054 else if (CONSP (idx))
|
|
1055 {
|
|
1056 if (!INTP (XCDR (idx)))
|
173
|
1057 return Qnil;
|
0
|
1058 indirection.keysym = XCAR (idx);
|
|
1059 indirection.modifiers = XINT (XCDR (idx));
|
|
1060 }
|
|
1061 else if (SYMBOLP (idx))
|
|
1062 {
|
|
1063 indirection.keysym = idx;
|
|
1064 indirection.modifiers = 0;
|
|
1065 }
|
|
1066 else
|
|
1067 {
|
|
1068 /* Random junk */
|
173
|
1069 return Qnil;
|
0
|
1070 }
|
173
|
1071 return raw_lookup_key (map, &indirection, 1, 0, accept_default);
|
0
|
1072 }
|
|
1073 else if (STRINGP (XCAR (object)))
|
|
1074 {
|
|
1075 /* If the keymap contents looks like (STRING . DEFN),
|
|
1076 use DEFN.
|
|
1077 Keymap alist elements like (CHAR MENUSTRING . DEFN)
|
|
1078 will be used by HierarKey menus. */
|
|
1079 object = XCDR (object);
|
|
1080 goto tail_recurse;
|
|
1081 }
|
|
1082 else
|
|
1083 {
|
|
1084 /* Anything else is really the value. */
|
173
|
1085 return object;
|
0
|
1086 }
|
|
1087 }
|
|
1088
|
|
1089 static Lisp_Object
|
|
1090 keymap_lookup_1 (Lisp_Object keymap, CONST struct key_data *key,
|
|
1091 int accept_default)
|
|
1092 {
|
|
1093 /* This function can GC */
|
173
|
1094 return get_keyelt (keymap_lookup_directly (keymap,
|
|
1095 key->keysym, key->modifiers),
|
|
1096 accept_default);
|
0
|
1097 }
|
|
1098
|
|
1099
|
|
1100 /************************************************************************/
|
|
1101 /* Copying keymaps */
|
|
1102 /************************************************************************/
|
|
1103
|
|
1104 struct copy_keymap_inverse_closure
|
|
1105 {
|
|
1106 Lisp_Object inverse_table;
|
|
1107 };
|
|
1108
|
|
1109 static void
|
185
|
1110 copy_keymap_inverse_mapper (CONST void *hash_key, void *hash_contents,
|
0
|
1111 void *copy_keymap_inverse_closure)
|
|
1112 {
|
|
1113 Lisp_Object key, inverse_table, inverse_contents;
|
185
|
1114 struct copy_keymap_inverse_closure *closure =
|
|
1115 (struct copy_keymap_inverse_closure *) copy_keymap_inverse_closure;
|
0
|
1116
|
|
1117 VOID_TO_LISP (inverse_table, closure);
|
|
1118 VOID_TO_LISP (inverse_contents, hash_contents);
|
|
1119 CVOID_TO_LISP (key, hash_key);
|
|
1120 /* copy-sequence deals with dotted lists. */
|
|
1121 if (CONSP (inverse_contents))
|
|
1122 inverse_contents = Fcopy_sequence (inverse_contents);
|
|
1123 Fputhash (key, inverse_contents, closure->inverse_table);
|
|
1124 }
|
|
1125
|
|
1126
|
|
1127 static Lisp_Object
|
|
1128 copy_keymap_internal (struct keymap *keymap)
|
|
1129 {
|
|
1130 Lisp_Object nkm = make_keymap (0);
|
|
1131 struct keymap *new_keymap = XKEYMAP (nkm);
|
|
1132 struct copy_keymap_inverse_closure copy_keymap_inverse_closure;
|
|
1133 copy_keymap_inverse_closure.inverse_table = keymap->inverse_table;
|
|
1134
|
|
1135 new_keymap->parents = Fcopy_sequence (keymap->parents);
|
|
1136 new_keymap->fullness = keymap->fullness;
|
|
1137 new_keymap->sub_maps_cache = Qnil; /* No submaps */
|
|
1138 new_keymap->table = Fcopy_hashtable (keymap->table);
|
|
1139 new_keymap->inverse_table = Fcopy_hashtable (keymap->inverse_table);
|
|
1140 /* After copying the inverse map, we need to copy the conses which
|
|
1141 are its values, lest they be shared by the copy, and mangled.
|
|
1142 */
|
|
1143 elisp_maphash (copy_keymap_inverse_mapper, keymap->inverse_table,
|
|
1144 ©_keymap_inverse_closure);
|
|
1145 return nkm;
|
|
1146 }
|
|
1147
|
|
1148
|
|
1149 static Lisp_Object copy_keymap (Lisp_Object keymap);
|
|
1150
|
|
1151 struct copy_keymap_closure
|
|
1152 {
|
|
1153 struct keymap *self;
|
|
1154 };
|
|
1155
|
|
1156 static void
|
185
|
1157 copy_keymap_mapper (CONST void *hash_key, void *hash_contents,
|
0
|
1158 void *copy_keymap_closure)
|
|
1159 {
|
|
1160 /* This function can GC */
|
|
1161 Lisp_Object key, contents;
|
185
|
1162 struct copy_keymap_closure *closure =
|
|
1163 (struct copy_keymap_closure *) copy_keymap_closure;
|
0
|
1164
|
|
1165 CVOID_TO_LISP (key, hash_key);
|
|
1166 VOID_TO_LISP (contents, hash_contents);
|
|
1167 /* When we encounter a keymap which is indirected through a
|
|
1168 symbol, we need to copy the sub-map. In v18, the form
|
|
1169 (lookup-key (copy-keymap global-map) "\C-x")
|
|
1170 returned a new keymap, not the symbol 'Control-X-prefix.
|
|
1171 */
|
|
1172 contents = get_keymap (contents,
|
|
1173 0, 1); /* #### autoload GC-safe here? */
|
|
1174 if (KEYMAPP (contents))
|
|
1175 keymap_store_internal (key, closure->self,
|
|
1176 copy_keymap (contents));
|
|
1177 }
|
|
1178
|
|
1179 static Lisp_Object
|
|
1180 copy_keymap (Lisp_Object keymap)
|
|
1181 {
|
|
1182 /* This function can GC */
|
|
1183 struct copy_keymap_closure copy_keymap_closure;
|
|
1184
|
|
1185 keymap = copy_keymap_internal (XKEYMAP (keymap));
|
|
1186 copy_keymap_closure.self = XKEYMAP (keymap);
|
|
1187 elisp_maphash (copy_keymap_mapper,
|
|
1188 XKEYMAP (keymap)->table,
|
|
1189 ©_keymap_closure);
|
|
1190 return keymap;
|
|
1191 }
|
|
1192
|
20
|
1193 DEFUN ("copy-keymap", Fcopy_keymap, 1, 1, 0, /*
|
0
|
1194 Return a copy of the keymap KEYMAP.
|
|
1195 The copy starts out with the same definitions of KEYMAP,
|
|
1196 but changing either the copy or KEYMAP does not affect the other.
|
|
1197 Any key definitions that are subkeymaps are recursively copied.
|
20
|
1198 */
|
|
1199 (keymap))
|
0
|
1200 {
|
|
1201 /* This function can GC */
|
|
1202 keymap = get_keymap (keymap, 1, 1);
|
|
1203 return copy_keymap (keymap);
|
|
1204 }
|
|
1205
|
|
1206
|
|
1207 static int
|
|
1208 keymap_fullness (Lisp_Object keymap)
|
|
1209 {
|
|
1210 /* This function can GC */
|
|
1211 int fullness;
|
|
1212 Lisp_Object sub_maps;
|
|
1213 struct gcpro gcpro1, gcpro2;
|
|
1214
|
|
1215 keymap = get_keymap (keymap, 1, 1);
|
|
1216 fullness = XKEYMAP (keymap)->fullness;
|
|
1217 sub_maps = keymap_submaps (keymap);
|
|
1218 GCPRO2 (keymap, sub_maps);
|
|
1219 for (; !NILP (sub_maps); sub_maps = XCDR (sub_maps))
|
|
1220 {
|
|
1221 if (MODIFIER_HASH_KEY_BITS (XCAR (XCAR (sub_maps))) != 0)
|
|
1222 {
|
|
1223 Lisp_Object sub_map = XCDR (XCAR (sub_maps));
|
|
1224 fullness--; /* don't count bucky maps */
|
|
1225 fullness += keymap_fullness (sub_map);
|
|
1226 }
|
|
1227 }
|
|
1228 UNGCPRO;
|
173
|
1229 return fullness;
|
0
|
1230 }
|
|
1231
|
20
|
1232 DEFUN ("keymap-fullness", Fkeymap_fullness, 1, 1, 0, /*
|
0
|
1233 Return the number of bindings in the keymap.
|
20
|
1234 */
|
|
1235 (keymap))
|
0
|
1236 {
|
|
1237 /* This function can GC */
|
173
|
1238 return make_int (keymap_fullness (get_keymap (keymap, 1, 1)));
|
0
|
1239 }
|
|
1240
|
|
1241
|
|
1242 /************************************************************************/
|
|
1243 /* Defining keys in keymaps */
|
|
1244 /************************************************************************/
|
|
1245
|
70
|
1246 /* Given a keysym (should be a symbol, int, char), make sure it's valid
|
|
1247 and perform any necessary canonicalization. */
|
|
1248
|
0
|
1249 static void
|
70
|
1250 define_key_check_and_coerce_keysym (Lisp_Object spec,
|
|
1251 Lisp_Object *keysym,
|
|
1252 unsigned int modifiers)
|
0
|
1253 {
|
|
1254 /* Now, check and massage the trailing keysym specifier. */
|
8
|
1255 if (SYMBOLP (*keysym))
|
0
|
1256 {
|
70
|
1257 if (string_char_length (XSYMBOL (*keysym)->name) == 1)
|
0
|
1258 {
|
70
|
1259 Lisp_Object ream_gcc_up_the_ass =
|
|
1260 make_char (string_char (XSYMBOL (*keysym)->name, 0));
|
|
1261 *keysym = ream_gcc_up_the_ass;
|
0
|
1262 goto fixnum_keysym;
|
|
1263 }
|
|
1264 }
|
70
|
1265 else if (CHAR_OR_CHAR_INTP (*keysym))
|
0
|
1266 {
|
70
|
1267 CHECK_CHAR_COERCE_INT (*keysym);
|
0
|
1268 fixnum_keysym:
|
70
|
1269 if (XCHAR (*keysym) < ' '
|
|
1270 /* || (XCHAR (*keysym) >= 128 && XCHAR (*keysym) < 160) */)
|
|
1271 /* yuck! Can't make the above restriction; too many compatibility
|
|
1272 problems ... */
|
|
1273 signal_simple_error ("keysym char must be printable", *keysym);
|
0
|
1274 /* #### This bites! I want to be able to write (control shift a) */
|
|
1275 if (modifiers & MOD_SHIFT)
|
70
|
1276 signal_simple_error
|
|
1277 ("the `shift' modifier may not be applied to ASCII keysyms",
|
|
1278 spec);
|
0
|
1279 }
|
|
1280 else
|
|
1281 {
|
|
1282 signal_simple_error ("unknown keysym specifier",
|
8
|
1283 *keysym);
|
|
1284 }
|
|
1285
|
|
1286 if (SYMBOLP (*keysym))
|
|
1287 {
|
|
1288 char *name = (char *)
|
|
1289 string_data (XSYMBOL (*keysym)->name);
|
|
1290
|
70
|
1291 /* FSFmacs uses symbols with the printed representation of keysyms in
|
|
1292 their names, like 'M-x, and we use the syntax '(meta x). So, to avoid
|
|
1293 confusion, notice the M-x syntax and signal an error - because
|
|
1294 otherwise it would be interpreted as a regular keysym, and would even
|
|
1295 show up in the list-buffers output, causing confusion to the naive.
|
|
1296
|
|
1297 We can get away with this because none of the X keysym names contain
|
|
1298 a hyphen (some contain underscore, however).
|
|
1299
|
|
1300 It might be useful to reject keysyms which are not x-valid-keysym-
|
|
1301 name-p, but that would interfere with various tricks we do to
|
|
1302 sanitize the Sun keyboards, and would make it trickier to
|
|
1303 conditionalize a .emacs file for multiple X servers.
|
|
1304 */
|
|
1305 if (((int) strlen (name) >= 2 && name[1] == '-')
|
|
1306 #if 1
|
|
1307 ||
|
|
1308 /* Ok, this is a bit more dubious - prevent people from doing things
|
|
1309 like (global-set-key 'RET 'something) because that will have the
|
|
1310 same problem as above. (Gag!) Maybe we should just silently
|
|
1311 accept these as aliases for the "real" names?
|
|
1312 */
|
185
|
1313 (string_length (XSYMBOL (*keysym)->name) <= 3 &&
|
70
|
1314 (!strcmp (name, "LFD") ||
|
|
1315 !strcmp (name, "TAB") ||
|
|
1316 !strcmp (name, "RET") ||
|
|
1317 !strcmp (name, "ESC") ||
|
|
1318 !strcmp (name, "DEL") ||
|
|
1319 !strcmp (name, "SPC") ||
|
|
1320 !strcmp (name, "BS")))
|
|
1321 #endif /* unused */
|
|
1322 )
|
|
1323 signal_simple_error
|
|
1324 ("Invalid (FSF Emacs) key format (see doc of define-key)",
|
|
1325 *keysym);
|
|
1326
|
|
1327 /* #### Ok, this is a bit more dubious - make people not lose if they
|
|
1328 do things like (global-set-key 'RET 'something) because that would
|
|
1329 otherwise have the same problem as above. (Gag!) We silently
|
|
1330 accept these as aliases for the "real" names.
|
|
1331 */
|
74
|
1332 else if (!strncmp(name, "kp_", 3)) {
|
|
1333 /* Likewise, the obsolete keysym binding of kp_.* should not lose. */
|
|
1334 char temp[50];
|
|
1335
|
|
1336 strncpy(temp, name, sizeof (temp));
|
|
1337 temp[sizeof (temp) - 1] = '\0';
|
|
1338 temp[2] = '-';
|
76
|
1339 *keysym = Fintern_soft(make_string((Bufbyte *)temp,
|
|
1340 strlen(temp)),
|
|
1341 Qnil);
|
74
|
1342 } else if (EQ (*keysym, QLFD))
|
70
|
1343 *keysym = QKlinefeed;
|
|
1344 else if (EQ (*keysym, QTAB))
|
|
1345 *keysym = QKtab;
|
|
1346 else if (EQ (*keysym, QRET))
|
|
1347 *keysym = QKreturn;
|
|
1348 else if (EQ (*keysym, QESC))
|
|
1349 *keysym = QKescape;
|
|
1350 else if (EQ (*keysym, QDEL))
|
|
1351 *keysym = QKdelete;
|
|
1352 else if (EQ (*keysym, QBS))
|
|
1353 *keysym = QKbackspace;
|
98
|
1354 /* Emacs compatibility */
|
|
1355 else if (EQ(*keysym, Qdown_mouse_1))
|
|
1356 *keysym = Qbutton1;
|
|
1357 else if (EQ(*keysym, Qdown_mouse_2))
|
|
1358 *keysym = Qbutton2;
|
|
1359 else if (EQ(*keysym, Qdown_mouse_3))
|
|
1360 *keysym = Qbutton3;
|
|
1361 else if (EQ(*keysym, Qmouse_1))
|
|
1362 *keysym = Qbutton1up;
|
|
1363 else if (EQ(*keysym, Qmouse_2))
|
|
1364 *keysym = Qbutton2up;
|
|
1365 else if (EQ(*keysym, Qmouse_3))
|
|
1366 *keysym = Qbutton3up;
|
0
|
1367 }
|
|
1368 }
|
|
1369
|
70
|
1370
|
0
|
1371 /* Given any kind of key-specifier, return a keysym and modifier mask.
|
70
|
1372 Proper canonicalization is performed:
|
|
1373
|
|
1374 -- integers are converted into the equivalent characters.
|
|
1375 -- one-character strings are converted into the equivalent characters.
|
0
|
1376 */
|
70
|
1377
|
0
|
1378 static void
|
|
1379 define_key_parser (Lisp_Object spec, struct key_data *returned_value)
|
|
1380 {
|
70
|
1381 if (CHAR_OR_CHAR_INTP (spec))
|
0
|
1382 {
|
|
1383 struct Lisp_Event event;
|
|
1384 event.event_type = empty_event;
|
70
|
1385 character_to_event (XCHAR_OR_CHAR_INT (spec), &event,
|
0
|
1386 XCONSOLE (Vselected_console), 0);
|
185
|
1387 returned_value->keysym = event.event.key.keysym;
|
0
|
1388 returned_value->modifiers = event.event.key.modifiers;
|
|
1389 }
|
|
1390 else if (EVENTP (spec))
|
|
1391 {
|
|
1392 switch (XEVENT (spec)->event_type)
|
|
1393 {
|
|
1394 case key_press_event:
|
|
1395 {
|
185
|
1396 returned_value->keysym = XEVENT (spec)->event.key.keysym;
|
0
|
1397 returned_value->modifiers = XEVENT (spec)->event.key.modifiers;
|
|
1398 break;
|
|
1399 }
|
|
1400 case button_press_event:
|
|
1401 case button_release_event:
|
|
1402 {
|
|
1403 int down = (XEVENT (spec)->event_type == button_press_event);
|
|
1404 switch (XEVENT (spec)->event.button.button)
|
|
1405 {
|
|
1406 case 1:
|
|
1407 returned_value->keysym = (down ? Qbutton1 : Qbutton1up); break;
|
|
1408 case 2:
|
|
1409 returned_value->keysym = (down ? Qbutton2 : Qbutton2up); break;
|
|
1410 case 3:
|
|
1411 returned_value->keysym = (down ? Qbutton3 : Qbutton3up); break;
|
|
1412 case 4:
|
|
1413 returned_value->keysym = (down ? Qbutton4 : Qbutton4up); break;
|
|
1414 case 5:
|
|
1415 returned_value->keysym = (down ? Qbutton5 : Qbutton5up); break;
|
|
1416 case 6:
|
|
1417 returned_value->keysym = (down ? Qbutton6 : Qbutton6up); break;
|
|
1418 case 7:
|
|
1419 returned_value->keysym = (down ? Qbutton7 : Qbutton7up); break;
|
|
1420 default:
|
185
|
1421 returned_value->keysym = (down ? Qbutton0 : Qbutton0up); break;
|
0
|
1422 }
|
|
1423 returned_value->modifiers = XEVENT (spec)->event.button.modifiers;
|
|
1424 break;
|
|
1425 }
|
|
1426 default:
|
|
1427 signal_error (Qwrong_type_argument,
|
|
1428 list2 (build_translated_string
|
|
1429 ("unable to bind this type of event"),
|
|
1430 spec));
|
|
1431 }
|
|
1432 }
|
|
1433 else if (SYMBOLP (spec))
|
|
1434 {
|
|
1435 /* Be nice, allow = to mean (=) */
|
|
1436 if (bucky_sym_to_bucky_bit (spec) != 0)
|
|
1437 signal_simple_error ("Key is a modifier name", spec);
|
70
|
1438 define_key_check_and_coerce_keysym (spec, &spec, 0);
|
0
|
1439 returned_value->keysym = spec;
|
|
1440 returned_value->modifiers = 0;
|
|
1441 }
|
|
1442 else if (CONSP (spec))
|
|
1443 {
|
|
1444 unsigned int modifiers = 0;
|
|
1445 Lisp_Object keysym = Qnil;
|
|
1446 Lisp_Object rest = spec;
|
|
1447
|
|
1448 /* First, parse out the leading modifier symbols. */
|
|
1449 while (CONSP (rest))
|
|
1450 {
|
|
1451 unsigned int modifier;
|
|
1452
|
|
1453 keysym = XCAR (rest);
|
|
1454 modifier = bucky_sym_to_bucky_bit (keysym);
|
|
1455 modifiers |= modifier;
|
|
1456 if (!NILP (XCDR (rest)))
|
|
1457 {
|
|
1458 if (! modifier)
|
|
1459 signal_simple_error ("unknown modifier", keysym);
|
|
1460 }
|
|
1461 else
|
|
1462 {
|
|
1463 if (modifier)
|
|
1464 signal_simple_error ("nothing but modifiers here",
|
|
1465 spec);
|
|
1466 }
|
|
1467 rest = XCDR (rest);
|
|
1468 QUIT;
|
|
1469 }
|
|
1470 if (!NILP (rest))
|
|
1471 signal_simple_error ("dotted list", spec);
|
|
1472
|
70
|
1473 define_key_check_and_coerce_keysym (spec, &keysym, modifiers);
|
0
|
1474 returned_value->keysym = keysym;
|
|
1475 returned_value->modifiers = modifiers;
|
|
1476 }
|
|
1477 else
|
|
1478 {
|
|
1479 signal_simple_error ("unknown key-sequence specifier",
|
|
1480 spec);
|
|
1481 }
|
|
1482 }
|
|
1483
|
|
1484 /* Used by character-to-event */
|
|
1485 void
|
|
1486 key_desc_list_to_event (Lisp_Object list, Lisp_Object event,
|
|
1487 int allow_menu_events)
|
|
1488 {
|
|
1489 struct key_data raw_key;
|
|
1490
|
|
1491 if (allow_menu_events &&
|
|
1492 CONSP (list) &&
|
|
1493 /* #### where the hell does this come from? */
|
|
1494 EQ (XCAR (list), Qmenu_selection))
|
|
1495 {
|
|
1496 Lisp_Object fn, arg;
|
|
1497 if (! NILP (Fcdr (Fcdr (list))))
|
|
1498 signal_simple_error ("invalid menu event desc", list);
|
|
1499 arg = Fcar (Fcdr (list));
|
|
1500 if (SYMBOLP (arg))
|
|
1501 fn = Qcall_interactively;
|
|
1502 else
|
|
1503 fn = Qeval;
|
|
1504 XSETFRAME (XEVENT (event)->channel, selected_frame ());
|
|
1505 XEVENT (event)->event_type = misc_user_event;
|
|
1506 XEVENT (event)->event.eval.function = fn;
|
|
1507 XEVENT (event)->event.eval.object = arg;
|
|
1508 return;
|
|
1509 }
|
|
1510
|
|
1511 define_key_parser (list, &raw_key);
|
|
1512
|
|
1513 if (EQ (raw_key.keysym, Qbutton0) || EQ (raw_key.keysym, Qbutton0up) ||
|
|
1514 EQ (raw_key.keysym, Qbutton1) || EQ (raw_key.keysym, Qbutton1up) ||
|
|
1515 EQ (raw_key.keysym, Qbutton2) || EQ (raw_key.keysym, Qbutton2up) ||
|
|
1516 EQ (raw_key.keysym, Qbutton3) || EQ (raw_key.keysym, Qbutton3up) ||
|
|
1517 EQ (raw_key.keysym, Qbutton4) || EQ (raw_key.keysym, Qbutton4up) ||
|
|
1518 EQ (raw_key.keysym, Qbutton5) || EQ (raw_key.keysym, Qbutton5up) ||
|
|
1519 EQ (raw_key.keysym, Qbutton6) || EQ (raw_key.keysym, Qbutton6up) ||
|
|
1520 EQ (raw_key.keysym, Qbutton7) || EQ (raw_key.keysym, Qbutton7up))
|
|
1521 error ("Mouse-clicks can't appear in saved keyboard macros.");
|
|
1522
|
|
1523 XEVENT (event)->channel = Vselected_console;
|
|
1524 XEVENT (event)->event_type = key_press_event;
|
|
1525 XEVENT (event)->event.key.keysym = raw_key.keysym;
|
|
1526 XEVENT (event)->event.key.modifiers = raw_key.modifiers;
|
|
1527 }
|
|
1528
|
|
1529
|
|
1530 int
|
|
1531 event_matches_key_specifier_p (struct Lisp_Event *event,
|
|
1532 Lisp_Object key_specifier)
|
|
1533 {
|
|
1534 Lisp_Object event2;
|
|
1535 int retval;
|
|
1536 struct gcpro gcpro1;
|
|
1537
|
|
1538 if (event->event_type != key_press_event || NILP (key_specifier) ||
|
70
|
1539 (INTP (key_specifier) && !CHAR_INTP (key_specifier)))
|
0
|
1540 return 0;
|
|
1541
|
|
1542 /* if the specifier is an integer such as 27, then it should match
|
|
1543 both of the events 'escape' and 'control ['. Calling
|
|
1544 Fcharacter_to_event() will only match 'escape'. */
|
70
|
1545 if (CHAR_OR_CHAR_INTP (key_specifier))
|
|
1546 return (XCHAR_OR_CHAR_INT (key_specifier)
|
|
1547 == event_to_character (event, 0, 0, 0));
|
0
|
1548
|
|
1549 /* Otherwise, we cannot call event_to_character() because we may
|
|
1550 be dealing with non-ASCII keystrokes. In any case, if I ask
|
|
1551 for 'control [' then I should get exactly that, and not
|
|
1552 'escape'.
|
|
1553
|
|
1554 However, we have to behave differently on TTY's, where 'control ['
|
|
1555 is silently converted into 'escape' by the keyboard driver.
|
|
1556 In this case, ASCII is the only thing we know about, so we have
|
|
1557 to compare the ASCII values. */
|
|
1558
|
|
1559 GCPRO1 (event2);
|
|
1560 event2 = Fmake_event ();
|
|
1561 Fcharacter_to_event (key_specifier, event2, Qnil, Qnil);
|
|
1562 if (XEVENT (event2)->event_type != key_press_event)
|
|
1563 retval = 0;
|
|
1564 else if (CONSOLE_TTY_P (XCONSOLE (EVENT_CHANNEL (event))))
|
|
1565 {
|
|
1566 int ch1, ch2;
|
|
1567
|
|
1568 ch1 = event_to_character (event, 0, 0, 0);
|
|
1569 ch2 = event_to_character (XEVENT (event2), 0, 0, 0);
|
|
1570 retval = (ch1 >= 0 && ch2 >= 0 && ch1 == ch2);
|
|
1571 }
|
|
1572 else if (EQ (event->event.key.keysym, XEVENT (event2)->event.key.keysym) &&
|
|
1573 event->event.key.modifiers == XEVENT (event2)->event.key.modifiers)
|
|
1574 retval = 1;
|
|
1575 else
|
|
1576 retval = 0;
|
|
1577 Fdeallocate_event (event2);
|
|
1578 UNGCPRO;
|
|
1579 return retval;
|
|
1580 }
|
|
1581
|
|
1582 static int
|
|
1583 meta_prefix_char_p (CONST struct key_data *key)
|
|
1584 {
|
|
1585 struct Lisp_Event event;
|
|
1586
|
|
1587 event.event_type = key_press_event;
|
|
1588 event.channel = Vselected_console;
|
|
1589 event.event.key.keysym = key->keysym;
|
|
1590 event.event.key.modifiers = key->modifiers;
|
|
1591 return event_matches_key_specifier_p (&event, Vmeta_prefix_char);
|
|
1592 }
|
|
1593
|
20
|
1594 DEFUN ("event-matches-key-specifier-p", Fevent_matches_key_specifier_p, 2, 2, 0, /*
|
0
|
1595 Return non-nil if EVENT matches KEY-SPECIFIER.
|
|
1596 This can be useful, e.g., to determine if the user pressed `help-char' or
|
|
1597 `quit-char'.
|
20
|
1598 */
|
|
1599 (event, key_specifier))
|
0
|
1600 {
|
|
1601 CHECK_LIVE_EVENT (event);
|
|
1602 return (event_matches_key_specifier_p (XEVENT (event), key_specifier)
|
|
1603 ? Qt : Qnil);
|
|
1604 }
|
|
1605
|
|
1606 /* ASCII grunge.
|
185
|
1607 Given a keysym, return another keysym/modifier pair which could be
|
|
1608 considered the same key in an ASCII world. Backspace returns ^H, for
|
0
|
1609 example.
|
|
1610 */
|
|
1611 static void
|
|
1612 define_key_alternate_name (struct key_data *key,
|
|
1613 struct key_data *returned_value)
|
|
1614 {
|
|
1615 Lisp_Object keysym = key->keysym;
|
|
1616 unsigned int modifiers = key->modifiers;
|
|
1617 unsigned int modifiers_sans_control = (modifiers & (~MOD_CONTROL));
|
|
1618 unsigned int modifiers_sans_meta = (modifiers & (~MOD_META));
|
|
1619 returned_value->keysym = Qnil; /* By default, no "alternate" key */
|
|
1620 returned_value->modifiers = 0;
|
|
1621 #define MACROLET(k,m) do { returned_value->keysym = (k); \
|
|
1622 returned_value->modifiers = (m); \
|
|
1623 RETURN__; } while (0)
|
|
1624 if (modifiers_sans_meta == MOD_CONTROL)
|
|
1625 {
|
|
1626 if EQ (keysym, QKspace)
|
|
1627 MACROLET (make_char ('@'), modifiers);
|
|
1628 else if (!CHARP (keysym))
|
|
1629 return;
|
|
1630 else switch (XCHAR (keysym))
|
|
1631 {
|
|
1632 case '@': /* c-@ => c-space */
|
|
1633 MACROLET (QKspace, modifiers);
|
|
1634 case 'h': /* c-h => backspace */
|
|
1635 MACROLET (QKbackspace, modifiers_sans_control);
|
|
1636 case 'i': /* c-i => tab */
|
|
1637 MACROLET (QKtab, modifiers_sans_control);
|
|
1638 case 'j': /* c-j => linefeed */
|
|
1639 MACROLET (QKlinefeed, modifiers_sans_control);
|
|
1640 case 'm': /* c-m => return */
|
|
1641 MACROLET (QKreturn, modifiers_sans_control);
|
|
1642 case '[': /* c-[ => escape */
|
|
1643 MACROLET (QKescape, modifiers_sans_control);
|
|
1644 default:
|
|
1645 return;
|
|
1646 }
|
|
1647 }
|
|
1648 else if (modifiers_sans_meta != 0)
|
|
1649 return;
|
|
1650 else if (EQ (keysym, QKbackspace)) /* backspace => c-h */
|
|
1651 MACROLET (make_char ('h'), (modifiers | MOD_CONTROL));
|
|
1652 else if (EQ (keysym, QKtab)) /* tab => c-i */
|
|
1653 MACROLET (make_char ('i'), (modifiers | MOD_CONTROL));
|
|
1654 else if (EQ (keysym, QKlinefeed)) /* linefeed => c-j */
|
|
1655 MACROLET (make_char ('j'), (modifiers | MOD_CONTROL));
|
|
1656 else if (EQ (keysym, QKreturn)) /* return => c-m */
|
|
1657 MACROLET (make_char ('m'), (modifiers | MOD_CONTROL));
|
|
1658 else if (EQ (keysym, QKescape)) /* escape => c-[ */
|
|
1659 MACROLET (make_char ('['), (modifiers | MOD_CONTROL));
|
|
1660 else
|
|
1661 return;
|
|
1662 #undef MACROLET
|
|
1663 }
|
|
1664
|
|
1665
|
|
1666 static void
|
|
1667 ensure_meta_prefix_char_keymapp (Lisp_Object keys, int indx,
|
|
1668 Lisp_Object keymap)
|
|
1669 {
|
|
1670 /* This function can GC */
|
|
1671 char buf [255];
|
|
1672 Lisp_Object new_keys;
|
|
1673 int i;
|
|
1674 Lisp_Object mpc_binding;
|
|
1675 struct key_data meta_key;
|
|
1676
|
|
1677 if (NILP (Vmeta_prefix_char) ||
|
70
|
1678 (INTP (Vmeta_prefix_char) && !CHAR_INTP (Vmeta_prefix_char)))
|
0
|
1679 return;
|
|
1680
|
|
1681 define_key_parser (Vmeta_prefix_char, &meta_key);
|
|
1682 mpc_binding = keymap_lookup_1 (keymap, &meta_key, 0);
|
|
1683 if (NILP (mpc_binding) || !NILP (Fkeymapp (mpc_binding)))
|
|
1684 return;
|
|
1685
|
|
1686 if (indx == 0)
|
|
1687 new_keys = keys;
|
|
1688 else if (STRINGP (keys))
|
|
1689 new_keys = Fsubstring (keys, Qzero, make_int (indx));
|
|
1690 else if (VECTORP (keys))
|
|
1691 {
|
|
1692 new_keys = make_vector (indx, Qnil);
|
|
1693 for (i = 0; i < indx; i++)
|
173
|
1694 XVECTOR_DATA (new_keys) [i] = XVECTOR_DATA (keys) [i];
|
0
|
1695 }
|
|
1696 else
|
|
1697 abort ();
|
|
1698 if (EQ (keys, new_keys))
|
|
1699 sprintf (buf, GETTEXT ("can't bind %s: %s has a non-keymap binding"),
|
70
|
1700 (char *) XSTRING_DATA (Fkey_description (keys)),
|
|
1701 (char *) XSTRING_DATA (Fsingle_key_description
|
|
1702 (Vmeta_prefix_char)));
|
0
|
1703 else
|
|
1704 sprintf (buf, GETTEXT ("can't bind %s: %s %s has a non-keymap binding"),
|
70
|
1705 (char *) XSTRING_DATA (Fkey_description (keys)),
|
|
1706 (char *) XSTRING_DATA (Fkey_description (new_keys)),
|
|
1707 (char *) XSTRING_DATA (Fsingle_key_description
|
|
1708 (Vmeta_prefix_char)));
|
0
|
1709 signal_simple_error (buf, mpc_binding);
|
|
1710 }
|
|
1711
|
20
|
1712 DEFUN ("define-key", Fdefine_key, 3, 3, 0, /*
|
0
|
1713 Define key sequence KEYS, in KEYMAP, as DEF.
|
|
1714 KEYMAP is a keymap object.
|
|
1715 KEYS is the sequence of keystrokes to bind, described below.
|
|
1716 DEF is anything that can be a key's definition:
|
|
1717 nil (means key is undefined in this keymap);
|
|
1718 a command (a Lisp function suitable for interactive calling);
|
|
1719 a string or key sequence vector (treated as a keyboard macro);
|
|
1720 a keymap (to define a prefix key);
|
|
1721 a symbol; when the key is looked up, the symbol will stand for its
|
|
1722 function definition, that should at that time be one of the above,
|
|
1723 or another symbol whose function definition is used, and so on.
|
|
1724 a cons (STRING . DEFN), meaning that DEFN is the definition
|
|
1725 (DEFN should be a valid definition in its own right);
|
|
1726 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.
|
|
1727
|
|
1728 Contrary to popular belief, the world is not ASCII. When running under a
|
|
1729 window manager, XEmacs can tell the difference between, for example, the
|
|
1730 keystrokes control-h, control-shift-h, and backspace. You can, in fact,
|
|
1731 bind different commands to each of these.
|
|
1732
|
|
1733 A `key sequence' is a set of keystrokes. A `keystroke' is a keysym and some
|
|
1734 set of modifiers (such as control and meta). A `keysym' is what is printed
|
|
1735 on the keys on your keyboard.
|
|
1736
|
|
1737 A keysym may be represented by a symbol, or (if and only if it is equivalent
|
|
1738 to an ASCII character in the range 32 - 255) by a character or its equivalent
|
|
1739 ASCII code. The `A' key may be represented by the symbol `A', the character
|
|
1740 `?A', or by the number 65. The `break' key may be represented only by the
|
|
1741 symbol `break'.
|
|
1742
|
|
1743 A keystroke may be represented by a list: the last element of the list
|
|
1744 is the key (a symbol, character, or number, as above) and the
|
|
1745 preceding elements are the symbolic names of modifier keys (control,
|
|
1746 meta, super, hyper, alt, and shift). Thus, the sequence control-b is
|
|
1747 represented by the forms `(control b)', `(control ?b)', and `(control
|
|
1748 98)'. A keystroke may also be represented by an event object, as
|
|
1749 returned by the `next-command-event' and `read-key-sequence'
|
|
1750 functions.
|
|
1751
|
|
1752 Note that in this context, the keystroke `control-b' is *not* represented
|
|
1753 by the number 2 (the ASCII code for ^B) or the character `?\^B'. See below.
|
|
1754
|
|
1755 The `shift' modifier is somewhat of a special case. You should not (and
|
|
1756 cannot) use `(meta shift a)' to mean `(meta A)', since for characters that
|
|
1757 have ASCII equivalents, the state of the shift key is implicit in the
|
|
1758 keysym (a vs. A). You also cannot say `(shift =)' to mean `+', as that
|
|
1759 sort of thing varies from keyboard to keyboard. The shift modifier is for
|
|
1760 use only with characters that do not have a second keysym on the same key,
|
|
1761 such as `backspace' and `tab'.
|
|
1762
|
|
1763 A key sequence is a vector of keystrokes. As a degenerate case, elements
|
|
1764 of this vector may also be keysyms if they have no modifiers. That is,
|
|
1765 the `A' keystroke is represented by all of these forms:
|
|
1766 A ?A 65 (A) (?A) (65)
|
|
1767 [A] [?A] [65] [(A)] [(?A)] [(65)]
|
185
|
1768
|
0
|
1769 the `control-a' keystroke is represented by these forms:
|
|
1770 (control A) (control ?A) (control 65)
|
|
1771 [(control A)] [(control ?A)] [(control 65)]
|
|
1772 the key sequence `control-c control-a' is represented by these forms:
|
|
1773 [(control c) (control a)] [(control ?c) (control ?a)]
|
|
1774 [(control 99) (control 65)] etc.
|
|
1775
|
|
1776 Mouse button clicks work just like keypresses: (control button1) means
|
|
1777 pressing the left mouse button while holding down the control key.
|
|
1778 \[(control c) (shift button3)] means control-c, hold shift, click right.
|
|
1779
|
|
1780 Commands may be bound to the mouse-button up-stroke rather than the down-
|
|
1781 stroke as well. `button1' means the down-stroke, and `button1up' means the
|
|
1782 up-stroke. Different commands may be bound to the up and down strokes,
|
|
1783 though that is probably not what you want, so be careful.
|
|
1784
|
|
1785 For backward compatibility, a key sequence may also be represented by a
|
|
1786 string. In this case, it represents the key sequence(s) that would
|
|
1787 produce that sequence of ASCII characters in a purely ASCII world. For
|
185
|
1788 example, a string containing the ASCII backspace character, "\\^H", would
|
0
|
1789 represent two key sequences: `(control h)' and `backspace'. Binding a
|
|
1790 command to this will actually bind both of those key sequences. Likewise
|
|
1791 for the following pairs:
|
|
1792
|
|
1793 control h backspace
|
|
1794 control i tab
|
|
1795 control m return
|
|
1796 control j linefeed
|
|
1797 control [ escape
|
|
1798 control @ control space
|
|
1799
|
|
1800 After binding a command to two key sequences with a form like
|
|
1801
|
185
|
1802 (define-key global-map "\\^X\\^I" \'command-1)
|
0
|
1803
|
|
1804 it is possible to redefine only one of those sequences like so:
|
|
1805
|
|
1806 (define-key global-map [(control x) (control i)] \'command-2)
|
|
1807 (define-key global-map [(control x) tab] \'command-3)
|
|
1808
|
|
1809 Of course, all of this applies only when running under a window system. If
|
|
1810 you're talking to XEmacs through a TTY connection, you don't get any of
|
|
1811 these features.
|
20
|
1812 */
|
|
1813 (keymap, keys, def))
|
0
|
1814 {
|
|
1815 /* This function can GC */
|
|
1816 int idx;
|
|
1817 int metized = 0;
|
173
|
1818 int len;
|
0
|
1819 int ascii_hack;
|
|
1820 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
1821
|
|
1822 if (VECTORP (keys))
|
173
|
1823 len = XVECTOR_LENGTH (keys);
|
0
|
1824 else if (STRINGP (keys))
|
173
|
1825 len = string_char_length (XSTRING (keys));
|
70
|
1826 else if (CHAR_OR_CHAR_INTP (keys) || SYMBOLP (keys) || CONSP (keys))
|
0
|
1827 {
|
|
1828 if (!CONSP (keys)) keys = list1 (keys);
|
173
|
1829 len = 1;
|
0
|
1830 keys = make_vector (1, keys); /* this is kinda sleazy. */
|
|
1831 }
|
|
1832 else
|
|
1833 {
|
|
1834 keys = wrong_type_argument (Qsequencep, keys);
|
173
|
1835 len = XINT (Flength (keys));
|
0
|
1836 }
|
173
|
1837 if (len == 0)
|
|
1838 return Qnil;
|
0
|
1839
|
|
1840 GCPRO3 (keymap, keys, def);
|
|
1841
|
|
1842 /* ASCII grunge.
|
|
1843 When the user defines a key which, in a strictly ASCII world, would be
|
|
1844 produced by two different keys (^J and linefeed, or ^H and backspace,
|
|
1845 for example) then the binding will be made for both keysyms.
|
|
1846
|
|
1847 This is done if the user binds a command to a string, as in
|
|
1848 (define-key map "\^H" 'something), but not when using one of the new
|
|
1849 syntaxes, like (define-key map '(control h) 'something).
|
|
1850 */
|
|
1851 ascii_hack = (STRINGP (keys));
|
|
1852
|
|
1853 keymap = get_keymap (keymap, 1, 1);
|
|
1854
|
|
1855 idx = 0;
|
|
1856 while (1)
|
|
1857 {
|
|
1858 Lisp_Object c;
|
|
1859 struct key_data raw_key1;
|
|
1860 struct key_data raw_key2;
|
|
1861
|
|
1862 if (STRINGP (keys))
|
|
1863 c = make_char (string_char (XSTRING (keys), idx));
|
|
1864 else
|
173
|
1865 c = XVECTOR_DATA (keys) [idx];
|
0
|
1866
|
|
1867 define_key_parser (c, &raw_key1);
|
|
1868
|
|
1869 if (!metized && ascii_hack && meta_prefix_char_p (&raw_key1))
|
|
1870 {
|
173
|
1871 if (idx == (len - 1))
|
0
|
1872 {
|
|
1873 /* This is a hack to prevent a binding for the meta-prefix-char
|
|
1874 from being made in a map which already has a non-empty "meta"
|
|
1875 submap. That is, we can't let both "escape" and "meta" have
|
|
1876 a binding in the same keymap. This implies that the idiom
|
|
1877 (define-key my-map "\e" my-escape-map)
|
|
1878 (define-key my-escape-map "a" 'my-command)
|
|
1879 no longer works. That's ok. Instead the luser should do
|
|
1880 (define-key my-map "\ea" 'my-command)
|
|
1881 or, more correctly
|
|
1882 (define-key my-map "\M-a" 'my-command)
|
|
1883 and then perhaps
|
|
1884 (defvar my-escape-map (lookup-key my-map "\e"))
|
|
1885 if the luser really wants the map in a variable.
|
|
1886 */
|
|
1887 Lisp_Object mmap;
|
|
1888 struct gcpro ngcpro1;
|
|
1889
|
|
1890 NGCPRO1 (c);
|
|
1891 mmap = Fgethash (MAKE_MODIFIER_HASH_KEY (MOD_META),
|
|
1892 XKEYMAP (keymap)->table, Qnil);
|
|
1893 if (!NILP (mmap)
|
|
1894 && keymap_fullness (mmap) != 0)
|
|
1895 {
|
|
1896 Lisp_Object desc
|
|
1897 = Fsingle_key_description (Vmeta_prefix_char);
|
|
1898 signal_simple_error_2
|
|
1899 ("Map contains meta-bindings, can't bind", desc, keymap);
|
|
1900 }
|
|
1901 NUNGCPRO;
|
|
1902 }
|
|
1903 else
|
|
1904 {
|
|
1905 metized = 1;
|
|
1906 idx++;
|
|
1907 continue;
|
|
1908 }
|
|
1909 }
|
|
1910
|
|
1911 if (ascii_hack)
|
|
1912 define_key_alternate_name (&raw_key1, &raw_key2);
|
|
1913 else
|
|
1914 {
|
|
1915 raw_key2.keysym = Qnil;
|
|
1916 raw_key2.modifiers = 0;
|
|
1917 }
|
185
|
1918
|
0
|
1919 if (metized)
|
|
1920 {
|
|
1921 raw_key1.modifiers |= MOD_META;
|
|
1922 raw_key2.modifiers |= MOD_META;
|
|
1923 metized = 0;
|
|
1924 }
|
|
1925
|
|
1926 /* This crap is to make sure that someone doesn't bind something like
|
|
1927 "C-x M-a" while "C-x ESC" has a non-keymap binding. */
|
|
1928 if (raw_key1.modifiers & MOD_META)
|
|
1929 ensure_meta_prefix_char_keymapp (keys, idx, keymap);
|
|
1930
|
173
|
1931 if (++idx == len)
|
0
|
1932 {
|
|
1933 keymap_store (keymap, &raw_key1, def);
|
|
1934 if (ascii_hack && !NILP (raw_key2.keysym))
|
|
1935 keymap_store (keymap, &raw_key2, def);
|
|
1936 UNGCPRO;
|
|
1937 return def;
|
|
1938 }
|
185
|
1939
|
0
|
1940 {
|
|
1941 Lisp_Object cmd;
|
|
1942 struct gcpro ngcpro1;
|
|
1943 NGCPRO1 (c);
|
|
1944
|
|
1945 cmd = keymap_lookup_1 (keymap, &raw_key1, 0);
|
|
1946 if (NILP (cmd))
|
|
1947 {
|
|
1948 cmd = Fmake_sparse_keymap (Qnil);
|
|
1949 XKEYMAP (cmd)->name /* for debugging */
|
|
1950 = list2 (make_key_description (&raw_key1, 1), keymap);
|
|
1951 keymap_store (keymap, &raw_key1, cmd);
|
|
1952 }
|
|
1953 if (NILP (Fkeymapp (cmd)))
|
|
1954 signal_simple_error_2 ("invalid prefix keys in sequence",
|
|
1955 c, keys);
|
|
1956
|
|
1957 if (ascii_hack && !NILP (raw_key2.keysym) &&
|
|
1958 NILP (keymap_lookup_1 (keymap, &raw_key2, 0)))
|
|
1959 keymap_store (keymap, &raw_key2, cmd);
|
|
1960
|
|
1961 keymap = get_keymap (cmd, 1, 1);
|
|
1962 NUNGCPRO;
|
|
1963 }
|
|
1964 }
|
|
1965 }
|
|
1966
|
|
1967
|
|
1968 /************************************************************************/
|
|
1969 /* Looking up keys in keymaps */
|
|
1970 /************************************************************************/
|
|
1971
|
185
|
1972 /* We need a very fast (i.e., non-consing) version of lookup-key in order
|
2
|
1973 to make where-is-internal really fly. */
|
0
|
1974
|
|
1975 struct raw_lookup_key_mapper_closure
|
2
|
1976 {
|
|
1977 int remaining;
|
|
1978 CONST struct key_data *raw_keys;
|
|
1979 int raw_keys_count;
|
|
1980 int keys_so_far;
|
|
1981 int accept_default;
|
|
1982 };
|
0
|
1983
|
|
1984 static Lisp_Object raw_lookup_key_mapper (Lisp_Object k, void *);
|
|
1985
|
|
1986 /* Caller should gc-protect args (keymaps may autoload) */
|
|
1987 static Lisp_Object
|
|
1988 raw_lookup_key (Lisp_Object keymap,
|
|
1989 CONST struct key_data *raw_keys, int raw_keys_count,
|
|
1990 int keys_so_far, int accept_default)
|
|
1991 {
|
|
1992 /* This function can GC */
|
|
1993 struct raw_lookup_key_mapper_closure c;
|
|
1994 c.remaining = raw_keys_count - 1;
|
|
1995 c.raw_keys = raw_keys;
|
|
1996 c.raw_keys_count = raw_keys_count;
|
|
1997 c.keys_so_far = keys_so_far;
|
|
1998 c.accept_default = accept_default;
|
|
1999
|
173
|
2000 return traverse_keymaps (keymap, Qnil, raw_lookup_key_mapper, &c);
|
0
|
2001 }
|
|
2002
|
|
2003 static Lisp_Object
|
|
2004 raw_lookup_key_mapper (Lisp_Object k, void *arg)
|
|
2005 {
|
|
2006 /* This function can GC */
|
185
|
2007 struct raw_lookup_key_mapper_closure *c =
|
|
2008 (struct raw_lookup_key_mapper_closure *) arg;
|
0
|
2009 int accept_default = c->accept_default;
|
|
2010 int remaining = c->remaining;
|
|
2011 int keys_so_far = c->keys_so_far;
|
|
2012 CONST struct key_data *raw_keys = c->raw_keys;
|
|
2013 Lisp_Object cmd;
|
185
|
2014
|
0
|
2015 if (! meta_prefix_char_p (&(raw_keys[0])))
|
|
2016 {
|
|
2017 /* Normal case: every case except the meta-hack (see below). */
|
|
2018 cmd = keymap_lookup_1 (k, &(raw_keys[0]), accept_default);
|
185
|
2019
|
0
|
2020 if (remaining == 0)
|
|
2021 /* Return whatever we found if we're out of keys */
|
|
2022 ;
|
|
2023 else if (NILP (cmd))
|
|
2024 /* Found nothing (though perhaps parent map may have binding) */
|
|
2025 ;
|
|
2026 else if (NILP (Fkeymapp (cmd)))
|
|
2027 /* Didn't find a keymap, and we have more keys.
|
|
2028 * Return a fixnum to indicate that keys were too long.
|
|
2029 */
|
|
2030 cmd = make_int (keys_so_far + 1);
|
|
2031 else
|
185
|
2032 cmd = raw_lookup_key (cmd, raw_keys + 1, remaining,
|
0
|
2033 keys_so_far + 1, accept_default);
|
|
2034 }
|
|
2035 else
|
|
2036 {
|
|
2037 /* This is a hack so that looking up a key-sequence whose last
|
|
2038 * element is the meta-prefix-char will return the keymap that
|
|
2039 * the "meta" keys are stored in, if there is no binding for
|
|
2040 * the meta-prefix-char (and if this map has a "meta" submap).
|
|
2041 * If this map doesnt have a "meta" submap, then the
|
|
2042 * meta-prefix-char is looked up just like any other key.
|
|
2043 */
|
|
2044 if (remaining == 0)
|
|
2045 {
|
|
2046 /* First look for the prefix-char directly */
|
|
2047 cmd = keymap_lookup_1 (k, &(raw_keys[0]), accept_default);
|
|
2048 if (NILP (cmd))
|
|
2049 {
|
185
|
2050 /* Do kludgy return of the meta-map */
|
0
|
2051 cmd = Fgethash (MAKE_MODIFIER_HASH_KEY (MOD_META),
|
|
2052 XKEYMAP (k)->table, Qnil);
|
|
2053 }
|
|
2054 }
|
|
2055 else
|
|
2056 {
|
|
2057 /* Search for the prefix-char-prefixed sequence directly */
|
|
2058 cmd = keymap_lookup_1 (k, &(raw_keys[0]), accept_default);
|
|
2059 cmd = get_keymap (cmd, 0, 1);
|
|
2060 if (!NILP (cmd))
|
185
|
2061 cmd = raw_lookup_key (cmd, raw_keys + 1, remaining,
|
0
|
2062 keys_so_far + 1, accept_default);
|
|
2063 else if ((raw_keys[1].modifiers & MOD_META) == 0)
|
|
2064 {
|
|
2065 struct key_data metified;
|
|
2066 metified.keysym = raw_keys[1].keysym;
|
|
2067 metified.modifiers = raw_keys[1].modifiers | MOD_META;
|
|
2068
|
|
2069 /* Search for meta-next-char sequence directly */
|
|
2070 cmd = keymap_lookup_1 (k, &metified, accept_default);
|
|
2071 if (remaining == 1)
|
|
2072 ;
|
|
2073 else
|
|
2074 {
|
|
2075 cmd = get_keymap (cmd, 0, 1);
|
|
2076 if (!NILP (cmd))
|
|
2077 cmd = raw_lookup_key (cmd, raw_keys + 2, remaining - 1,
|
|
2078 keys_so_far + 2,
|
|
2079 accept_default);
|
|
2080 }
|
|
2081 }
|
|
2082 }
|
|
2083 }
|
|
2084 if (accept_default && NILP (cmd))
|
|
2085 cmd = XKEYMAP (k)->default_binding;
|
173
|
2086 return cmd;
|
0
|
2087 }
|
|
2088
|
|
2089 /* Value is number if `keys' is too long; NIL if valid but has no definition.*/
|
|
2090 /* Caller should gc-protect arguments */
|
|
2091 static Lisp_Object
|
|
2092 lookup_keys (Lisp_Object keymap, int nkeys, Lisp_Object *keys,
|
|
2093 int accept_default)
|
|
2094 {
|
|
2095 /* This function can GC */
|
|
2096 struct key_data kkk[20];
|
|
2097 struct key_data *raw_keys;
|
|
2098 int i;
|
|
2099
|
|
2100 if (nkeys == 0)
|
|
2101 return Qnil;
|
|
2102
|
100
|
2103 if (nkeys < (countof (kkk)))
|
0
|
2104 raw_keys = kkk;
|
|
2105 else
|
185
|
2106 raw_keys = alloca_array (struct key_data, nkeys);
|
0
|
2107
|
|
2108 for (i = 0; i < nkeys; i++)
|
|
2109 {
|
|
2110 define_key_parser (keys[i], &(raw_keys[i]));
|
|
2111 }
|
173
|
2112 return raw_lookup_key (keymap, raw_keys, nkeys, 0, accept_default);
|
0
|
2113 }
|
|
2114
|
|
2115 static Lisp_Object
|
|
2116 lookup_events (Lisp_Object event_head, int nmaps, Lisp_Object keymaps[],
|
|
2117 int accept_default)
|
|
2118 {
|
|
2119 /* This function can GC */
|
|
2120 struct key_data kkk[20];
|
|
2121 Lisp_Object event;
|
|
2122
|
|
2123 int nkeys;
|
|
2124 struct key_data *raw_keys;
|
|
2125 Lisp_Object tem = Qnil;
|
|
2126 struct gcpro gcpro1, gcpro2;
|
|
2127 int iii;
|
|
2128
|
|
2129 CHECK_LIVE_EVENT (event_head);
|
|
2130
|
|
2131 nkeys = event_chain_count (event_head);
|
|
2132
|
|
2133 if (nkeys < (countof (kkk)))
|
|
2134 raw_keys = kkk;
|
|
2135 else
|
185
|
2136 raw_keys = alloca_array (struct key_data, nkeys);
|
0
|
2137
|
|
2138 nkeys = 0;
|
|
2139 EVENT_CHAIN_LOOP (event, event_head)
|
|
2140 define_key_parser (event, &(raw_keys[nkeys++]));
|
|
2141 GCPRO2 (keymaps[0], event_head);
|
|
2142 gcpro1.nvars = nmaps;
|
|
2143 /* ####raw_keys[].keysym slots aren't gc-protected. We rely (but shouldn't)
|
|
2144 * on somebody else somewhere (obarray) having a pointer to all keysyms. */
|
|
2145 for (iii = 0; iii < nmaps; iii++)
|
|
2146 {
|
|
2147 tem = raw_lookup_key (keymaps[iii], raw_keys, nkeys, 0,
|
|
2148 accept_default);
|
|
2149 if (INTP (tem))
|
|
2150 {
|
|
2151 /* Too long in some local map means don't look at global map */
|
|
2152 tem = Qnil;
|
|
2153 break;
|
|
2154 }
|
|
2155 else if (!NILP (tem))
|
|
2156 break;
|
|
2157 }
|
|
2158 UNGCPRO;
|
173
|
2159 return tem;
|
0
|
2160 }
|
|
2161
|
20
|
2162 DEFUN ("lookup-key", Flookup_key, 2, 3, 0, /*
|
0
|
2163 In keymap KEYMAP, look up key-sequence KEYS. Return the definition.
|
|
2164 Nil is returned if KEYS is unbound. See documentation of `define-key'
|
|
2165 for valid key definitions and key-sequence specifications.
|
185
|
2166 A number is returned if KEYS is "too long"; that is, the leading
|
0
|
2167 characters fail to be a valid sequence of prefix characters in KEYMAP.
|
|
2168 The number is how many characters at the front of KEYS
|
|
2169 it takes to reach a non-prefix command.
|
20
|
2170 */
|
|
2171 (keymap, keys, accept_default))
|
0
|
2172 {
|
|
2173 /* This function can GC */
|
|
2174 if (VECTORP (keys))
|
|
2175 {
|
|
2176 return lookup_keys (keymap,
|
173
|
2177 XVECTOR_LENGTH (keys),
|
|
2178 XVECTOR_DATA (keys),
|
0
|
2179 !NILP (accept_default));
|
|
2180 }
|
70
|
2181 else if (SYMBOLP (keys) || CHAR_OR_CHAR_INTP (keys) || CONSP (keys))
|
0
|
2182 {
|
|
2183 return lookup_keys (keymap, 1, &keys,
|
|
2184 !NILP (accept_default));
|
|
2185 }
|
|
2186 else if (!STRINGP (keys))
|
|
2187 {
|
|
2188 keys = wrong_type_argument (Qsequencep, keys);
|
|
2189 return Flookup_key (keymap, keys, accept_default);
|
|
2190 }
|
2
|
2191 else /* STRINGP (keys) */
|
0
|
2192 {
|
|
2193 int length = string_char_length (XSTRING (keys));
|
|
2194 int i;
|
185
|
2195 struct key_data *raw_keys = alloca_array (struct key_data, length);
|
0
|
2196 if (length == 0)
|
|
2197 return Qnil;
|
|
2198
|
|
2199 for (i = 0; i < length; i++)
|
|
2200 {
|
|
2201 Emchar n = string_char (XSTRING (keys), i);
|
|
2202 define_key_parser (make_char (n), &(raw_keys[i]));
|
|
2203 }
|
173
|
2204 return raw_lookup_key (keymap, raw_keys, length, 0,
|
|
2205 !NILP (accept_default));
|
0
|
2206 }
|
|
2207 }
|
|
2208
|
|
2209 /* Given a key sequence, returns a list of keymaps to search for bindings.
|
|
2210 Does all manner of semi-hairy heuristics, like looking in the current
|
|
2211 buffer's map before looking in the global map and looking in the local
|
|
2212 map of the buffer in which the mouse was clicked in event0 is a click.
|
|
2213
|
|
2214 It would be kind of nice if this were in Lisp so that this semi-hairy
|
|
2215 semi-heuristic command-lookup behaviour could be readily understood and
|
|
2216 customised. However, this needs to be pretty fast, or performance of
|
|
2217 keyboard macros goes to shit; putting this in lisp slows macros down
|
185
|
2218 2-3x. And they're already slower than v18 by 5-6x.
|
0
|
2219 */
|
|
2220
|
|
2221 struct relevant_maps
|
|
2222 {
|
|
2223 int nmaps;
|
|
2224 unsigned int max_maps;
|
|
2225 Lisp_Object *maps;
|
|
2226 struct gcpro *gcpro;
|
|
2227 };
|
|
2228
|
|
2229 static void get_relevant_extent_keymaps (Lisp_Object pos,
|
|
2230 Lisp_Object buffer_or_string,
|
|
2231 Lisp_Object glyph,
|
|
2232 struct relevant_maps *closure);
|
|
2233 static void get_relevant_minor_maps (Lisp_Object buffer,
|
|
2234 struct relevant_maps *closure);
|
|
2235
|
|
2236 static void
|
|
2237 relevant_map_push (Lisp_Object map, struct relevant_maps *closure)
|
185
|
2238 {
|
0
|
2239 unsigned int nmaps = closure->nmaps;
|
|
2240
|
|
2241 if (!KEYMAPP (map))
|
|
2242 return;
|
|
2243 closure->nmaps = nmaps + 1;
|
|
2244 if (nmaps < closure->max_maps)
|
|
2245 {
|
|
2246 closure->maps[nmaps] = map;
|
|
2247 closure->gcpro->nvars = nmaps;
|
|
2248 }
|
|
2249 }
|
|
2250
|
|
2251 static int
|
|
2252 get_relevant_keymaps (Lisp_Object keys,
|
|
2253 int max_maps, Lisp_Object maps[])
|
|
2254 {
|
|
2255 /* This function can GC */
|
|
2256 Lisp_Object terminal = Qnil;
|
|
2257 struct gcpro gcpro1;
|
|
2258 struct relevant_maps closure;
|
|
2259 struct console *con;
|
|
2260
|
|
2261 GCPRO1 (*maps);
|
|
2262 gcpro1.nvars = 0;
|
|
2263 closure.nmaps = 0;
|
|
2264 closure.max_maps = max_maps;
|
|
2265 closure.maps = maps;
|
|
2266 closure.gcpro = &gcpro1;
|
|
2267
|
|
2268 if (EVENTP (keys))
|
|
2269 terminal = event_chain_tail (keys);
|
|
2270 else if (VECTORP (keys))
|
|
2271 {
|
173
|
2272 int len = XVECTOR_LENGTH (keys);
|
0
|
2273 if (len > 0)
|
173
|
2274 terminal = XVECTOR_DATA (keys)[len - 1];
|
0
|
2275 }
|
|
2276
|
|
2277 if (EVENTP (terminal))
|
|
2278 {
|
|
2279 CHECK_LIVE_EVENT (terminal);
|
|
2280 con = event_console_or_selected (terminal);
|
|
2281 }
|
|
2282 else
|
|
2283 con = XCONSOLE (Vselected_console);
|
185
|
2284
|
0
|
2285 if (KEYMAPP (con->overriding_terminal_local_map)
|
|
2286 || KEYMAPP (Voverriding_local_map))
|
|
2287 {
|
|
2288 if (KEYMAPP (con->overriding_terminal_local_map))
|
|
2289 relevant_map_push (con->overriding_terminal_local_map, &closure);
|
|
2290 if (KEYMAPP (Voverriding_local_map))
|
|
2291 relevant_map_push (Voverriding_local_map, &closure);
|
|
2292 }
|
|
2293 else if (!EVENTP (terminal)
|
185
|
2294 || (XEVENT (terminal)->event_type != button_press_event
|
0
|
2295 && XEVENT (terminal)->event_type != button_release_event))
|
|
2296 {
|
|
2297 Lisp_Object tem;
|
|
2298 XSETBUFFER (tem, current_buffer);
|
|
2299 /* It's not a mouse event; order of keymaps searched is:
|
|
2300 o keymap of any/all extents under the mouse
|
|
2301 o minor-mode maps
|
|
2302 o local-map of current-buffer
|
|
2303 o global-map
|
|
2304 */
|
|
2305 /* The terminal element of the lookup may be nil or a keysym.
|
|
2306 In those cases we don't want to check for an extent
|
|
2307 keymap. */
|
|
2308 if (EVENTP (terminal))
|
|
2309 {
|
|
2310 get_relevant_extent_keymaps (make_int (BUF_PT (current_buffer)),
|
|
2311 tem, Qnil, &closure);
|
|
2312 }
|
|
2313 get_relevant_minor_maps (tem, &closure);
|
|
2314
|
|
2315 tem = current_buffer->keymap;
|
|
2316 if (!NILP (tem))
|
|
2317 relevant_map_push (tem, &closure);
|
|
2318 }
|
|
2319 #ifdef HAVE_WINDOW_SYSTEM
|
|
2320 else
|
|
2321 {
|
|
2322 /* It's a mouse event; order of keymaps searched is:
|
|
2323 o local-map of mouse-grabbed-buffer
|
|
2324 o keymap of any/all extents under the mouse
|
|
2325 if the mouse is over a modeline:
|
|
2326 o modeline-map of buffer corresponding to that modeline
|
|
2327 o else, local-map of buffer under the mouse
|
|
2328 o minor-mode maps
|
|
2329 o local-map of current-buffer
|
|
2330 o global-map
|
|
2331 */
|
|
2332 Lisp_Object window = Fevent_window (terminal);
|
|
2333
|
|
2334 if (BUFFERP (Vmouse_grabbed_buffer))
|
|
2335 {
|
|
2336 Lisp_Object map = XBUFFER (Vmouse_grabbed_buffer)->keymap;
|
|
2337
|
|
2338 get_relevant_minor_maps (Vmouse_grabbed_buffer, &closure);
|
|
2339 if (!NILP (map))
|
|
2340 relevant_map_push (map, &closure);
|
|
2341 }
|
|
2342
|
|
2343 if (!NILP (window))
|
|
2344 {
|
|
2345 Lisp_Object buffer = Fwindow_buffer (window);
|
|
2346
|
|
2347 if (!NILP (buffer))
|
|
2348 {
|
|
2349 if (!NILP (Fevent_over_modeline_p (terminal)))
|
|
2350 {
|
|
2351 Lisp_Object map = symbol_value_in_buffer (Qmodeline_map,
|
|
2352 buffer);
|
|
2353
|
|
2354 get_relevant_extent_keymaps
|
|
2355 (Fevent_modeline_position (terminal),
|
|
2356 XBUFFER (buffer)->generated_modeline_string,
|
|
2357 /* #### third arg should maybe be a glyph. */
|
|
2358 Qnil, &closure);
|
|
2359
|
|
2360 if (!UNBOUNDP (map) && !NILP (map))
|
|
2361 relevant_map_push (get_keymap (map, 1, 1), &closure);
|
|
2362 }
|
|
2363 else
|
|
2364 {
|
|
2365 get_relevant_extent_keymaps (Fevent_point (terminal), buffer,
|
|
2366 Fevent_glyph_extent (terminal),
|
|
2367 &closure);
|
|
2368 }
|
|
2369
|
|
2370 if (!EQ (buffer, Vmouse_grabbed_buffer)) /* already pushed */
|
|
2371 {
|
114
|
2372 Lisp_Object map = XBUFFER (buffer)->keymap;
|
|
2373
|
0
|
2374 get_relevant_minor_maps (buffer, &closure);
|
114
|
2375 if (!NILP(map))
|
|
2376 relevant_map_push (map, &closure);
|
0
|
2377 }
|
|
2378 }
|
|
2379 }
|
|
2380 else if (!NILP (Fevent_over_toolbar_p (terminal)))
|
|
2381 {
|
|
2382 Lisp_Object map = Fsymbol_value (Qtoolbar_map);
|
|
2383
|
|
2384 if (!UNBOUNDP (map) && !NILP (map))
|
|
2385 relevant_map_push (map, &closure);
|
|
2386 }
|
|
2387 }
|
|
2388 #endif /* HAVE_WINDOW_SYSTEM */
|
|
2389
|
|
2390 {
|
|
2391 int nmaps = closure.nmaps;
|
|
2392 /* Silently truncate at 100 keymaps to prevent infinite losssage */
|
|
2393 if (nmaps >= max_maps && max_maps > 0)
|
|
2394 maps[max_maps - 1] = Vcurrent_global_map;
|
|
2395 else
|
|
2396 maps[nmaps] = Vcurrent_global_map;
|
|
2397 UNGCPRO;
|
173
|
2398 return nmaps + 1;
|
0
|
2399 }
|
|
2400 }
|
|
2401
|
|
2402 /* Returns a set of keymaps extracted from the extents at POS in
|
|
2403 BUFFER_OR_STRING. The GLYPH arg, if specified, is one more extent
|
|
2404 to look for a keymap in, and if it has one, its keymap will be the
|
|
2405 first element in the list returned. This is so we can correctly
|
|
2406 search the keymaps associated with glyphs which may be physically
|
|
2407 disjoint from their extents: for example, if a glyph is out in the
|
|
2408 margin, we should still consult the kemyap of that glyph's extent,
|
|
2409 which may not itself be under the mouse.
|
|
2410 */
|
157
|
2411
|
0
|
2412 static void
|
|
2413 get_relevant_extent_keymaps (Lisp_Object pos, Lisp_Object buffer_or_string,
|
|
2414 Lisp_Object glyph,
|
|
2415 struct relevant_maps *closure)
|
|
2416 {
|
|
2417 /* This function can GC */
|
|
2418 /* the glyph keymap, if any, comes first.
|
|
2419 (Processing it twice is no big deal: noop.) */
|
|
2420 if (!NILP (glyph))
|
|
2421 {
|
|
2422 Lisp_Object keymap = Fextent_property (glyph, Qkeymap, Qnil);
|
|
2423 if (!NILP (keymap))
|
|
2424 relevant_map_push (get_keymap (keymap, 1, 1), closure);
|
|
2425 }
|
185
|
2426
|
0
|
2427 /* Next check the extents at the text position, if any */
|
|
2428 if (!NILP (pos))
|
|
2429 {
|
|
2430 Lisp_Object extent;
|
157
|
2431 for (extent = Fextent_at (pos, buffer_or_string, Qkeymap, Qnil, Qat);
|
0
|
2432 !NILP (extent);
|
157
|
2433 extent = Fextent_at (pos, buffer_or_string, Qkeymap, extent, Qat))
|
0
|
2434 {
|
157
|
2435 if (!NILP (Fextent_in_region_p (extent, pos, pos, Qnil)))
|
|
2436 {
|
|
2437 Lisp_Object keymap = Fextent_property (extent, Qkeymap, Qnil);
|
|
2438 if (!NILP (keymap))
|
|
2439 relevant_map_push (get_keymap (keymap, 1, 1), closure);
|
|
2440 QUIT;
|
|
2441 }
|
0
|
2442 }
|
|
2443 }
|
|
2444 }
|
|
2445
|
|
2446 static Lisp_Object
|
|
2447 minor_mode_keymap_predicate (Lisp_Object assoc, Lisp_Object buffer)
|
|
2448 {
|
|
2449 /* This function can GC */
|
|
2450 if (CONSP (assoc))
|
|
2451 {
|
|
2452 Lisp_Object sym = XCAR (assoc);
|
|
2453 if (SYMBOLP (sym))
|
|
2454 {
|
|
2455 Lisp_Object val = symbol_value_in_buffer (sym, buffer);
|
|
2456 if (!NILP (val) && !UNBOUNDP (val))
|
|
2457 {
|
|
2458 Lisp_Object map = get_keymap (XCDR (assoc), 0, 1);
|
173
|
2459 return map;
|
0
|
2460 }
|
|
2461 }
|
|
2462 }
|
173
|
2463 return Qnil;
|
0
|
2464 }
|
|
2465
|
|
2466 static void
|
|
2467 get_relevant_minor_maps (Lisp_Object buffer, struct relevant_maps *closure)
|
|
2468 {
|
|
2469 /* This function can GC */
|
|
2470 Lisp_Object alist;
|
|
2471
|
|
2472 /* Will you ever lose badly if you make this circular! */
|
|
2473 for (alist = symbol_value_in_buffer (Qminor_mode_map_alist, buffer);
|
|
2474 CONSP (alist);
|
|
2475 alist = XCDR (alist))
|
|
2476 {
|
|
2477 Lisp_Object m = minor_mode_keymap_predicate (XCAR (alist),
|
|
2478 buffer);
|
|
2479 if (!NILP (m)) relevant_map_push (m, closure);
|
|
2480 QUIT;
|
|
2481 }
|
|
2482 }
|
|
2483
|
|
2484 /* #### Would map-current-keymaps be a better thing?? */
|
20
|
2485 DEFUN ("current-keymaps", Fcurrent_keymaps, 0, 1, 0, /*
|
0
|
2486 Return a list of the current keymaps that will be searched for bindings.
|
|
2487 This lists keymaps such as the current local map and the minor-mode maps,
|
|
2488 but does not list the parents of those keymaps.
|
|
2489 EVENT-OR-KEYS controls which keymaps will be listed.
|
|
2490 If EVENT-OR-KEYS is a mouse event (or a vector whose last element is a
|
|
2491 mouse event), the keymaps for that mouse event will be listed (see
|
|
2492 `key-binding'). Otherwise, the keymaps for key presses will be listed.
|
20
|
2493 */
|
|
2494 (event_or_keys))
|
0
|
2495 {
|
|
2496 /* This function can GC */
|
|
2497 struct gcpro gcpro1;
|
|
2498 Lisp_Object maps[100];
|
|
2499 Lisp_Object *gubbish = maps;
|
|
2500 int nmaps;
|
|
2501
|
|
2502 GCPRO1 (event_or_keys);
|
|
2503 nmaps = get_relevant_keymaps (event_or_keys, countof (maps),
|
|
2504 gubbish);
|
|
2505 if (nmaps > countof (maps))
|
|
2506 {
|
185
|
2507 gubbish = alloca_array (Lisp_Object, nmaps);
|
0
|
2508 nmaps = get_relevant_keymaps (event_or_keys, nmaps, gubbish);
|
|
2509 }
|
|
2510 UNGCPRO;
|
173
|
2511 return Flist (nmaps, gubbish);
|
0
|
2512 }
|
|
2513
|
20
|
2514 DEFUN ("key-binding", Fkey_binding, 1, 2, 0, /*
|
0
|
2515 Return the binding for command KEYS in current keymaps.
|
|
2516 KEYS is a string, a vector of events, or a vector of key-description lists
|
|
2517 as described in the documentation for the `define-key' function.
|
|
2518 The binding is probably a symbol with a function definition; see
|
|
2519 the documentation for `lookup-key' for more information.
|
|
2520
|
|
2521 For key-presses, the order of keymaps searched is:
|
|
2522 - the `keymap' property of any extent(s) at point;
|
|
2523 - any applicable minor-mode maps;
|
|
2524 - the current-local-map of the current-buffer;
|
|
2525 - the current global map.
|
|
2526
|
|
2527 For mouse-clicks, the order of keymaps searched is:
|
|
2528 - the current-local-map of the `mouse-grabbed-buffer' if any;
|
|
2529 - the `keymap' property of any extent(s) at the position of the click
|
|
2530 (this includes modeline extents);
|
|
2531 - the modeline-map of the buffer corresponding to the modeline under
|
|
2532 the mouse (if the click happened over a modeline);
|
|
2533 - the value of toolbar-map in the current-buffer (if the click
|
|
2534 happened over a toolbar);
|
|
2535 - the current-local-map of the buffer under the mouse (does not
|
|
2536 apply to toolbar clicks);
|
|
2537 - any applicable minor-mode maps;
|
|
2538 - the current global map.
|
|
2539
|
|
2540 Note that if `overriding-local-map' or `overriding-terminal-local-map'
|
|
2541 is non-nil, *only* those two maps and the current global map are searched.
|
20
|
2542 */
|
|
2543 (keys, accept_default))
|
0
|
2544 {
|
|
2545 /* This function can GC */
|
|
2546 int i;
|
|
2547 Lisp_Object maps[100];
|
|
2548 int nmaps;
|
|
2549 struct gcpro gcpro1, gcpro2;
|
|
2550 GCPRO2 (keys, accept_default); /* get_relevant_keymaps may autoload */
|
|
2551
|
|
2552 nmaps = get_relevant_keymaps (keys, countof (maps), maps);
|
|
2553
|
|
2554 UNGCPRO;
|
|
2555
|
|
2556 if (EVENTP (keys)) /* unadvertised "feature" for the future */
|
173
|
2557 return lookup_events (keys, nmaps, maps, !NILP (accept_default));
|
0
|
2558
|
|
2559 for (i = 0; i < nmaps; i++)
|
|
2560 {
|
|
2561 Lisp_Object tem = Flookup_key (maps[i], keys,
|
|
2562 accept_default);
|
|
2563 if (INTP (tem))
|
|
2564 {
|
|
2565 /* Too long in some local map means don't look at global map */
|
173
|
2566 return Qnil;
|
0
|
2567 }
|
|
2568 else if (!NILP (tem))
|
173
|
2569 return tem;
|
0
|
2570 }
|
173
|
2571 return Qnil;
|
0
|
2572 }
|
|
2573
|
|
2574 static Lisp_Object
|
|
2575 process_event_binding_result (Lisp_Object result)
|
|
2576 {
|
|
2577 if (EQ (result, Qundefined))
|
|
2578 /* The suppress-keymap function binds keys to 'undefined - special-case
|
|
2579 that here, so that being bound to that has the same error-behavior as
|
|
2580 not being defined at all.
|
|
2581 */
|
|
2582 result = Qnil;
|
|
2583 if (!NILP (result))
|
|
2584 {
|
|
2585 Lisp_Object map;
|
|
2586 /* Snap out possible keymap indirections */
|
|
2587 map = get_keymap (result, 0, 1);
|
|
2588 if (!NILP (map))
|
|
2589 result = map;
|
|
2590 }
|
|
2591
|
|
2592 return result;
|
|
2593 }
|
|
2594
|
|
2595 /* Attempts to find a command corresponding to the event-sequence
|
|
2596 whose head is event0 (sequence is threaded though event_next).
|
|
2597
|
|
2598 The return value will be
|
|
2599
|
|
2600 -- nil (there is no binding; this will also be returned
|
|
2601 whenever the event chain is "too long", i.e. there
|
|
2602 is a non-nil, non-keymap binding for a prefix of
|
|
2603 the event chain)
|
|
2604 -- a keymap (part of a command has been specified)
|
|
2605 -- a command (anything that satisfies `commandp'; this includes
|
|
2606 some symbols, lists, subrs, strings, vectors, and
|
|
2607 compiled-function objects) */
|
|
2608 Lisp_Object
|
|
2609 event_binding (Lisp_Object event0, int accept_default)
|
|
2610 {
|
|
2611 /* This function can GC */
|
|
2612 Lisp_Object maps[100];
|
|
2613 int nmaps;
|
|
2614
|
|
2615 assert (EVENTP (event0));
|
|
2616
|
|
2617 nmaps = get_relevant_keymaps (event0, countof (maps), maps);
|
114
|
2618 if (nmaps > countof (maps))
|
|
2619 nmaps = countof (maps);
|
173
|
2620 return process_event_binding_result (lookup_events (event0, nmaps, maps,
|
|
2621 accept_default));
|
0
|
2622 }
|
|
2623
|
175
|
2624 /* like event_binding, but specify a keymap to search */
|
|
2625
|
|
2626 Lisp_Object
|
|
2627 event_binding_in (Lisp_Object event0, Lisp_Object keymap, int accept_default)
|
|
2628 {
|
|
2629 /* This function can GC */
|
|
2630 if (!KEYMAPP (keymap))
|
|
2631 return Qnil;
|
185
|
2632
|
|
2633 return process_event_binding_result (lookup_events (event0, 1, &keymap,
|
175
|
2634 accept_default));
|
|
2635 }
|
|
2636
|
0
|
2637 /* Attempts to find a function key mapping corresponding to the
|
|
2638 event-sequence whose head is event0 (sequence is threaded through
|
|
2639 event_next). The return value will be the same as for event_binding(). */
|
|
2640 Lisp_Object
|
|
2641 munging_key_map_event_binding (Lisp_Object event0,
|
|
2642 enum munge_me_out_the_door munge)
|
|
2643 {
|
185
|
2644 Lisp_Object keymap = (munge == MUNGE_ME_FUNCTION_KEY) ?
|
|
2645 CONSOLE_FUNCTION_KEY_MAP (event_console_or_selected (event0)) :
|
|
2646 Vkey_translation_map;
|
|
2647
|
|
2648 if (NILP (keymap))
|
0
|
2649 return Qnil;
|
|
2650
|
185
|
2651 return process_event_binding_result (lookup_events (event0, 1, &keymap, 1));
|
0
|
2652 }
|
|
2653
|
|
2654
|
|
2655 /************************************************************************/
|
|
2656 /* Setting/querying the global and local maps */
|
|
2657 /************************************************************************/
|
|
2658
|
20
|
2659 DEFUN ("use-global-map", Fuse_global_map, 1, 1, 0, /*
|
0
|
2660 Select KEYMAP as the global keymap.
|
20
|
2661 */
|
|
2662 (keymap))
|
0
|
2663 {
|
|
2664 /* This function can GC */
|
|
2665 keymap = get_keymap (keymap, 1, 1);
|
|
2666 Vcurrent_global_map = keymap;
|
|
2667 return Qnil;
|
|
2668 }
|
|
2669
|
20
|
2670 DEFUN ("use-local-map", Fuse_local_map, 1, 2, 0, /*
|
0
|
2671 Select KEYMAP as the local keymap in BUFFER.
|
|
2672 If KEYMAP is nil, that means no local keymap.
|
|
2673 If BUFFER is nil, the current buffer is assumed.
|
20
|
2674 */
|
|
2675 (keymap, buffer))
|
0
|
2676 {
|
|
2677 /* This function can GC */
|
|
2678 struct buffer *b = decode_buffer (buffer, 0);
|
|
2679 if (!NILP (keymap))
|
|
2680 keymap = get_keymap (keymap, 1, 1);
|
|
2681
|
|
2682 b->keymap = keymap;
|
|
2683
|
|
2684 return Qnil;
|
|
2685 }
|
|
2686
|
20
|
2687 DEFUN ("current-local-map", Fcurrent_local_map, 0, 1, 0, /*
|
0
|
2688 Return BUFFER's local keymap, or nil if it has none.
|
|
2689 If BUFFER is nil, the current buffer is assumed.
|
20
|
2690 */
|
|
2691 (buffer))
|
0
|
2692 {
|
|
2693 struct buffer *b = decode_buffer (buffer, 0);
|
|
2694 return b->keymap;
|
|
2695 }
|
|
2696
|
20
|
2697 DEFUN ("current-global-map", Fcurrent_global_map, 0, 0, 0, /*
|
0
|
2698 Return the current global keymap.
|
20
|
2699 */
|
|
2700 ())
|
0
|
2701 {
|
173
|
2702 return Vcurrent_global_map;
|
0
|
2703 }
|
|
2704
|
|
2705
|
|
2706 /************************************************************************/
|
|
2707 /* Mapping over keymap elements */
|
|
2708 /************************************************************************/
|
|
2709
|
|
2710 /* Since keymaps are arranged in a hierarchy, one keymap per bucky bit or
|
185
|
2711 prefix key, it's not entirely obvious what map-keymap should do, but
|
0
|
2712 what it does is: map over all keys in this map; then recursively map
|
|
2713 over all submaps of this map that are "bucky" submaps. This means that,
|
|
2714 when mapping over a keymap, it appears that "x" and "C-x" are in the
|
|
2715 same map, although "C-x" is really in the "control" submap of this one.
|
|
2716 However, since we don't recursively descend the submaps that are bound
|
|
2717 to prefix keys (like C-x, C-h, etc) the caller will have to recurse on
|
|
2718 those explicitly, if that's what they want.
|
|
2719
|
|
2720 So the end result of this is that the bucky keymaps (the ones indexed
|
|
2721 under the large integers returned from MAKE_MODIFIER_HASH_KEY()) are
|
|
2722 invisible from elisp. They're just an implementation detail that code
|
|
2723 outside of this file doesn't need to know about.
|
|
2724 */
|
|
2725
|
|
2726 struct map_keymap_unsorted_closure
|
|
2727 {
|
|
2728 void (*fn) (CONST struct key_data *, Lisp_Object binding, void *arg);
|
|
2729 void *arg;
|
|
2730 unsigned int modifiers;
|
|
2731 };
|
|
2732
|
|
2733 /* used by map_keymap() */
|
|
2734 static void
|
185
|
2735 map_keymap_unsorted_mapper (CONST void *hash_key, void *hash_contents,
|
0
|
2736 void *map_keymap_unsorted_closure)
|
|
2737 {
|
|
2738 /* This function can GC */
|
|
2739 Lisp_Object keysym;
|
|
2740 Lisp_Object contents;
|
185
|
2741 struct map_keymap_unsorted_closure *closure =
|
|
2742 (struct map_keymap_unsorted_closure *) map_keymap_unsorted_closure;
|
0
|
2743 unsigned int modifiers = closure->modifiers;
|
|
2744 unsigned int mod_bit;
|
|
2745 CVOID_TO_LISP (keysym, hash_key);
|
|
2746 VOID_TO_LISP (contents, hash_contents);
|
|
2747 mod_bit = MODIFIER_HASH_KEY_BITS (keysym);
|
|
2748 if (mod_bit != 0)
|
|
2749 {
|
|
2750 int omod = modifiers;
|
|
2751 closure->modifiers = (modifiers | mod_bit);
|
185
|
2752 contents = get_keymap (contents, 1, 0);
|
0
|
2753 elisp_maphash (map_keymap_unsorted_mapper,
|
|
2754 XKEYMAP (contents)->table,
|
|
2755 map_keymap_unsorted_closure);
|
|
2756 closure->modifiers = omod;
|
|
2757 }
|
|
2758 else
|
|
2759 {
|
|
2760 struct key_data key;
|
|
2761 key.keysym = keysym;
|
|
2762 key.modifiers = modifiers;
|
|
2763 ((*closure->fn) (&key, contents, closure->arg));
|
|
2764 }
|
|
2765 }
|
|
2766
|
|
2767
|
|
2768 struct map_keymap_sorted_closure
|
|
2769 {
|
|
2770 Lisp_Object *result_locative;
|
|
2771 };
|
|
2772
|
|
2773 /* used by map_keymap_sorted() */
|
|
2774 static void
|
185
|
2775 map_keymap_sorted_mapper (CONST void *hash_key, void *hash_contents,
|
0
|
2776 void *map_keymap_sorted_closure)
|
|
2777 {
|
185
|
2778 struct map_keymap_sorted_closure *cl =
|
|
2779 (struct map_keymap_sorted_closure *) map_keymap_sorted_closure;
|
0
|
2780 Lisp_Object key, contents;
|
|
2781 Lisp_Object *list = cl->result_locative;
|
|
2782 CVOID_TO_LISP (key, hash_key);
|
|
2783 VOID_TO_LISP (contents, hash_contents);
|
|
2784 *list = Fcons (Fcons (key, contents), *list);
|
|
2785 }
|
|
2786
|
|
2787
|
|
2788 /* used by map_keymap_sorted(), describe_map_sort_predicate(),
|
|
2789 and keymap_submaps().
|
|
2790 */
|
|
2791 static int
|
185
|
2792 map_keymap_sort_predicate (Lisp_Object obj1, Lisp_Object obj2,
|
0
|
2793 Lisp_Object pred)
|
|
2794 {
|
|
2795 /* obj1 and obj2 are conses with keysyms in their cars. Cdrs are ignored.
|
|
2796 */
|
|
2797 unsigned int bit1, bit2;
|
|
2798 int sym1_p = 0;
|
|
2799 int sym2_p = 0;
|
|
2800 obj1 = XCAR (obj1);
|
|
2801 obj2 = XCAR (obj2);
|
|
2802
|
|
2803 if (EQ (obj1, obj2))
|
|
2804 return -1;
|
|
2805 bit1 = MODIFIER_HASH_KEY_BITS (obj1);
|
|
2806 bit2 = MODIFIER_HASH_KEY_BITS (obj2);
|
185
|
2807
|
0
|
2808 /* If either is a symbol with a character-set-property, then sort it by
|
|
2809 that code instead of alphabetically.
|
|
2810 */
|
|
2811 if (! bit1 && SYMBOLP (obj1))
|
|
2812 {
|
|
2813 Lisp_Object code = Fget (obj1, Vcharacter_set_property, Qnil);
|
70
|
2814 if (CHAR_OR_CHAR_INTP (code))
|
|
2815 {
|
|
2816 obj1 = code;
|
|
2817 CHECK_CHAR_COERCE_INT (obj1);
|
|
2818 sym1_p = 1;
|
|
2819 }
|
0
|
2820 }
|
|
2821 if (! bit2 && SYMBOLP (obj2))
|
|
2822 {
|
|
2823 Lisp_Object code = Fget (obj2, Vcharacter_set_property, Qnil);
|
70
|
2824 if (CHAR_OR_CHAR_INTP (code))
|
|
2825 {
|
|
2826 obj2 = code;
|
|
2827 CHECK_CHAR_COERCE_INT (obj2);
|
|
2828 sym2_p = 1;
|
|
2829 }
|
0
|
2830 }
|
|
2831
|
|
2832 /* all symbols (non-ASCIIs) come after characters (ASCIIs) */
|
|
2833 if (XTYPE (obj1) != XTYPE (obj2))
|
173
|
2834 return SYMBOLP (obj2) ? 1 : -1;
|
0
|
2835
|
|
2836 if (! bit1 && CHARP (obj1)) /* they're both ASCII */
|
|
2837 {
|
|
2838 int o1 = XCHAR (obj1);
|
|
2839 int o2 = XCHAR (obj2);
|
|
2840 if (o1 == o2 && /* If one started out as a symbol and the */
|
|
2841 sym1_p != sym2_p) /* other didn't, the symbol comes last. */
|
173
|
2842 return sym2_p ? 1 : -1;
|
|
2843
|
|
2844 return o1 < o2 ? 1 : -1; /* else just compare them */
|
0
|
2845 }
|
|
2846
|
|
2847 /* else they're both symbols. If they're both buckys, then order them. */
|
|
2848 if (bit1 && bit2)
|
173
|
2849 return bit1 < bit2 ? 1 : -1;
|
185
|
2850
|
0
|
2851 /* if only one is a bucky, then it comes later */
|
|
2852 if (bit1 || bit2)
|
173
|
2853 return bit2 ? 1 : -1;
|
0
|
2854
|
|
2855 /* otherwise, string-sort them. */
|
|
2856 {
|
|
2857 char *s1 = (char *) string_data (XSYMBOL (obj1)->name);
|
|
2858 char *s2 = (char *) string_data (XSYMBOL (obj2)->name);
|
|
2859 #ifdef I18N2
|
173
|
2860 return 0 > strcoll (s1, s2) ? 1 : -1;
|
0
|
2861 #else
|
173
|
2862 return 0 > strcmp (s1, s2) ? 1 : -1;
|
0
|
2863 #endif
|
|
2864 }
|
|
2865 }
|
|
2866
|
|
2867
|
|
2868 /* used by map_keymap() */
|
|
2869 static void
|
|
2870 map_keymap_sorted (Lisp_Object keymap_table,
|
185
|
2871 unsigned int modifiers,
|
0
|
2872 void (*function) (CONST struct key_data *key,
|
185
|
2873 Lisp_Object binding,
|
0
|
2874 void *map_keymap_sorted_closure),
|
|
2875 void *map_keymap_sorted_closure)
|
|
2876 {
|
|
2877 /* This function can GC */
|
|
2878 struct gcpro gcpro1;
|
|
2879 Lisp_Object contents = Qnil;
|
|
2880
|
|
2881 if (XINT (Fhashtable_fullness (keymap_table)) == 0)
|
|
2882 return;
|
|
2883
|
|
2884 GCPRO1 (contents);
|
|
2885
|
|
2886 {
|
|
2887 struct map_keymap_sorted_closure c1;
|
|
2888 c1.result_locative = &contents;
|
|
2889 elisp_maphash (map_keymap_sorted_mapper, keymap_table, &c1);
|
|
2890 }
|
|
2891 contents = list_sort (contents, Qnil, map_keymap_sort_predicate);
|
|
2892 for (; !NILP (contents); contents = XCDR (contents))
|
|
2893 {
|
|
2894 Lisp_Object keysym = XCAR (XCAR (contents));
|
|
2895 Lisp_Object binding = XCDR (XCAR (contents));
|
|
2896 unsigned int sub_bits = MODIFIER_HASH_KEY_BITS (keysym);
|
|
2897 if (sub_bits != 0)
|
|
2898 map_keymap_sorted (XKEYMAP (get_keymap (binding,
|
|
2899 1, 1))->table,
|
|
2900 (modifiers | sub_bits),
|
|
2901 function,
|
|
2902 map_keymap_sorted_closure);
|
|
2903 else
|
|
2904 {
|
|
2905 struct key_data k;
|
|
2906 k.keysym = keysym;
|
|
2907 k.modifiers = modifiers;
|
|
2908 ((*function) (&k, binding, map_keymap_sorted_closure));
|
|
2909 }
|
|
2910 }
|
|
2911 UNGCPRO;
|
|
2912 }
|
|
2913
|
|
2914
|
|
2915 /* used by Fmap_keymap() */
|
|
2916 static void
|
|
2917 map_keymap_mapper (CONST struct key_data *key,
|
185
|
2918 Lisp_Object binding,
|
0
|
2919 void *function)
|
|
2920 {
|
|
2921 /* This function can GC */
|
|
2922 Lisp_Object fn;
|
|
2923 VOID_TO_LISP (fn, function);
|
|
2924 call2 (fn, make_key_description (key, 1), binding);
|
|
2925 }
|
|
2926
|
|
2927
|
|
2928 static void
|
|
2929 map_keymap (Lisp_Object keymap_table, int sort_first,
|
|
2930 void (*function) (CONST struct key_data *key,
|
|
2931 Lisp_Object binding,
|
|
2932 void *fn_arg),
|
|
2933 void *fn_arg)
|
|
2934 {
|
|
2935 /* This function can GC */
|
|
2936 if (sort_first)
|
|
2937 map_keymap_sorted (keymap_table, 0, function, fn_arg);
|
|
2938 else
|
|
2939 {
|
|
2940 struct map_keymap_unsorted_closure map_keymap_unsorted_closure;
|
|
2941 map_keymap_unsorted_closure.fn = function;
|
|
2942 map_keymap_unsorted_closure.arg = fn_arg;
|
|
2943 map_keymap_unsorted_closure.modifiers = 0;
|
|
2944 elisp_maphash (map_keymap_unsorted_mapper, keymap_table,
|
|
2945 &map_keymap_unsorted_closure);
|
|
2946 }
|
|
2947 }
|
|
2948
|
20
|
2949 DEFUN ("map-keymap", Fmap_keymap, 2, 3, 0, /*
|
0
|
2950 Apply FUNCTION to each element of KEYMAP.
|
|
2951 FUNCTION will be called with two arguments: a key-description list, and
|
|
2952 the binding. The order in which the elements of the keymap are passed to
|
|
2953 the function is unspecified. If the function inserts new elements into
|
|
2954 the keymap, it may or may not be called with them later. No element of
|
|
2955 the keymap will ever be passed to the function more than once.
|
|
2956
|
|
2957 The function will not be called on elements of this keymap's parents
|
|
2958 (see the function `keymap-parents') or upon keymaps which are contained
|
|
2959 within this keymap (multi-character definitions).
|
185
|
2960 It will be called on "meta" characters since they are not really
|
0
|
2961 two-character sequences.
|
|
2962
|
|
2963 If the optional third argument SORT-FIRST is non-nil, then the elements of
|
|
2964 the keymap will be passed to the mapper function in a canonical order.
|
|
2965 Otherwise, they will be passed in hash (that is, random) order, which is
|
|
2966 faster.
|
20
|
2967 */
|
70
|
2968 (function, keymap, sort_first))
|
0
|
2969 {
|
|
2970 /* This function can GC */
|
|
2971 struct gcpro gcpro1, gcpro2;
|
|
2972
|
|
2973 /* tolerate obviously transposed args */
|
|
2974 if (!NILP (Fkeymapp (function)))
|
|
2975 {
|
|
2976 Lisp_Object tmp = function;
|
|
2977 function = keymap;
|
|
2978 keymap = tmp;
|
|
2979 }
|
|
2980 GCPRO2 (function, keymap);
|
|
2981 keymap = get_keymap (keymap, 1, 1);
|
|
2982 map_keymap (XKEYMAP (keymap)->table, !NILP (sort_first),
|
|
2983 map_keymap_mapper, LISP_TO_VOID (function));
|
|
2984 UNGCPRO;
|
|
2985 return Qnil;
|
|
2986 }
|
|
2987
|
|
2988
|
|
2989
|
|
2990 /************************************************************************/
|
|
2991 /* Accessible keymaps */
|
|
2992 /************************************************************************/
|
|
2993
|
|
2994 struct accessible_keymaps_closure
|
|
2995 {
|
|
2996 Lisp_Object tail;
|
|
2997 };
|
|
2998
|
|
2999
|
|
3000 static void
|
|
3001 accessible_keymaps_mapper_1 (Lisp_Object keysym, Lisp_Object contents,
|
|
3002 unsigned int modifiers,
|
|
3003 struct accessible_keymaps_closure *closure)
|
|
3004 {
|
|
3005 /* This function can GC */
|
|
3006 unsigned int subbits = MODIFIER_HASH_KEY_BITS (keysym);
|
|
3007
|
|
3008 if (subbits != 0)
|
|
3009 {
|
|
3010 Lisp_Object submaps;
|
|
3011
|
|
3012 contents = get_keymap (contents, 1, 1);
|
|
3013 submaps = keymap_submaps (contents);
|
|
3014 for (; !NILP (submaps); submaps = XCDR (submaps))
|
|
3015 {
|
|
3016 accessible_keymaps_mapper_1 (XCAR (XCAR (submaps)),
|
|
3017 XCDR (XCAR (submaps)),
|
|
3018 (subbits | modifiers),
|
|
3019 closure);
|
|
3020 }
|
|
3021 }
|
|
3022 else
|
|
3023 {
|
|
3024 Lisp_Object thisseq = Fcar (Fcar (closure->tail));
|
|
3025 Lisp_Object cmd = get_keyelt (contents, 1);
|
|
3026 Lisp_Object vec;
|
|
3027 int j;
|
173
|
3028 int len;
|
0
|
3029 struct key_data key;
|
|
3030 key.keysym = keysym;
|
|
3031 key.modifiers = modifiers;
|
|
3032
|
|
3033 if (NILP (cmd))
|
|
3034 abort ();
|
|
3035 cmd = get_keymap (cmd, 0, 1);
|
|
3036 if (!KEYMAPP (cmd))
|
|
3037 abort ();
|
|
3038
|
173
|
3039 vec = make_vector (XVECTOR_LENGTH (thisseq) + 1, Qnil);
|
|
3040 len = XVECTOR_LENGTH (thisseq);
|
|
3041 for (j = 0; j < len; j++)
|
|
3042 XVECTOR_DATA (vec) [j] = XVECTOR_DATA (thisseq) [j];
|
|
3043 XVECTOR_DATA (vec) [j] = make_key_description (&key, 1);
|
0
|
3044
|
|
3045 nconc2 (closure->tail, list1 (Fcons (vec, cmd)));
|
|
3046 }
|
|
3047 }
|
|
3048
|
|
3049
|
|
3050 static Lisp_Object
|
|
3051 accessible_keymaps_keymap_mapper (Lisp_Object thismap, void *arg)
|
|
3052 {
|
|
3053 /* This function can GC */
|
185
|
3054 struct accessible_keymaps_closure *closure =
|
|
3055 (struct accessible_keymaps_closure *) arg;
|
0
|
3056 Lisp_Object submaps = keymap_submaps (thismap);
|
|
3057
|
|
3058 for (; !NILP (submaps); submaps = XCDR (submaps))
|
|
3059 {
|
|
3060 accessible_keymaps_mapper_1 (XCAR (XCAR (submaps)),
|
|
3061 XCDR (XCAR (submaps)),
|
|
3062 0,
|
|
3063 closure);
|
|
3064 }
|
173
|
3065 return Qnil;
|
0
|
3066 }
|
|
3067
|
|
3068
|
20
|
3069 DEFUN ("accessible-keymaps", Faccessible_keymaps, 1, 2, 0, /*
|
185
|
3070 Find all keymaps accessible via prefix characters from KEYMAP.
|
0
|
3071 Returns a list of elements of the form (KEYS . MAP), where the sequence
|
185
|
3072 KEYS starting from KEYMAP gets you to MAP. These elements are ordered
|
|
3073 so that the KEYS increase in length. The first element is ([] . KEYMAP).
|
0
|
3074 An optional argument PREFIX, if non-nil, should be a key sequence;
|
|
3075 then the value includes only maps for prefixes that start with PREFIX.
|
20
|
3076 */
|
185
|
3077 (keymap, prefix))
|
0
|
3078 {
|
|
3079 /* This function can GC */
|
|
3080 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
|
3081 Lisp_Object accessible_keymaps = Qnil;
|
|
3082 struct accessible_keymaps_closure c;
|
|
3083 c.tail = Qnil;
|
185
|
3084 GCPRO4 (accessible_keymaps, c.tail, prefix, keymap);
|
0
|
3085
|
|
3086 retry:
|
185
|
3087 keymap = get_keymap (keymap, 1, 1);
|
0
|
3088 if (NILP (prefix))
|
|
3089 prefix = make_vector (0, Qnil);
|
|
3090 else if (!VECTORP (prefix) || STRINGP (prefix))
|
|
3091 {
|
|
3092 prefix = wrong_type_argument (Qarrayp, prefix);
|
|
3093 goto retry;
|
|
3094 }
|
|
3095 else
|
|
3096 {
|
|
3097 int len = XINT (Flength (prefix));
|
185
|
3098 Lisp_Object def = Flookup_key (keymap, prefix, Qnil);
|
0
|
3099 Lisp_Object p;
|
|
3100 int iii;
|
|
3101 struct gcpro ngcpro1;
|
|
3102
|
|
3103 def = get_keymap (def, 0, 1);
|
|
3104 if (!KEYMAPP (def))
|
|
3105 goto RETURN;
|
|
3106
|
185
|
3107 keymap = def;
|
0
|
3108 p = make_vector (len, Qnil);
|
|
3109 NGCPRO1 (p);
|
|
3110 for (iii = 0; iii < len; iii++)
|
|
3111 {
|
|
3112 struct key_data key;
|
|
3113 define_key_parser (Faref (prefix, make_int (iii)), &key);
|
173
|
3114 XVECTOR_DATA (p)[iii] = make_key_description (&key, 1);
|
0
|
3115 }
|
|
3116 NUNGCPRO;
|
|
3117 prefix = p;
|
|
3118 }
|
185
|
3119
|
|
3120 accessible_keymaps = list1 (Fcons (prefix, keymap));
|
0
|
3121
|
|
3122 /* For each map in the list maps,
|
|
3123 look at any other maps it points to
|
|
3124 and stick them at the end if they are not already in the list */
|
|
3125
|
|
3126 for (c.tail = accessible_keymaps;
|
|
3127 !NILP (c.tail);
|
|
3128 c.tail = XCDR (c.tail))
|
|
3129 {
|
|
3130 Lisp_Object thismap = Fcdr (Fcar (c.tail));
|
|
3131 CHECK_KEYMAP (thismap);
|
|
3132 traverse_keymaps (thismap, Qnil,
|
|
3133 accessible_keymaps_keymap_mapper, &c);
|
|
3134 }
|
|
3135 RETURN:
|
|
3136 UNGCPRO;
|
173
|
3137 return accessible_keymaps;
|
0
|
3138 }
|
|
3139
|
|
3140
|
|
3141
|
|
3142 /************************************************************************/
|
|
3143 /* Pretty descriptions of key sequences */
|
|
3144 /************************************************************************/
|
|
3145
|
20
|
3146 DEFUN ("key-description", Fkey_description, 1, 1, 0, /*
|
0
|
3147 Return a pretty description of key-sequence KEYS.
|
185
|
3148 Control characters turn into "C-foo" sequences, meta into "M-foo",
|
|
3149 spaces are put between sequence elements, etc...
|
20
|
3150 */
|
|
3151 (keys))
|
0
|
3152 {
|
70
|
3153 if (CHAR_OR_CHAR_INTP (keys) || CONSP (keys) || SYMBOLP (keys)
|
|
3154 || EVENTP (keys))
|
0
|
3155 {
|
|
3156 return Fsingle_key_description (keys);
|
|
3157 }
|
|
3158 else if (VECTORP (keys) ||
|
|
3159 STRINGP (keys))
|
|
3160 {
|
|
3161 Lisp_Object string = Qnil;
|
|
3162 /* Lisp_Object sep = Qnil; */
|
|
3163 int size = XINT (Flength (keys));
|
|
3164 int i;
|
|
3165
|
|
3166 for (i = 0; i < size; i++)
|
|
3167 {
|
|
3168 Lisp_Object s2 = Fsingle_key_description
|
|
3169 (((STRINGP (keys))
|
|
3170 ? make_char (string_char (XSTRING (keys), i))
|
173
|
3171 : XVECTOR_DATA (keys)[i]));
|
0
|
3172
|
|
3173 if (i == 0)
|
|
3174 string = s2;
|
|
3175 else
|
|
3176 {
|
|
3177 /* if (NILP (sep)) Lisp_Object sep = build_string (" ") */;
|
|
3178 string = concat2 (string, concat2 (Vsingle_space_string, s2));
|
|
3179 }
|
|
3180 }
|
173
|
3181 return string;
|
0
|
3182 }
|
|
3183 return Fkey_description (wrong_type_argument (Qsequencep, keys));
|
|
3184 }
|
|
3185
|
20
|
3186 DEFUN ("single-key-description", Fsingle_key_description, 1, 1, 0, /*
|
0
|
3187 Return a pretty description of command character KEY.
|
|
3188 Control characters turn into C-whatever, etc.
|
|
3189 This differs from `text-char-description' in that it returns a description
|
|
3190 of a key read from the user rather than a character from a buffer.
|
20
|
3191 */
|
|
3192 (key))
|
0
|
3193 {
|
|
3194 if (SYMBOLP (key))
|
|
3195 key = Fcons (key, Qnil); /* sleaze sleaze */
|
|
3196
|
70
|
3197 if (EVENTP (key) || CHAR_OR_CHAR_INTP (key))
|
0
|
3198 {
|
|
3199 char buf [255];
|
|
3200 if (!EVENTP (key))
|
|
3201 {
|
|
3202 struct Lisp_Event event;
|
|
3203 event.event_type = empty_event;
|
|
3204 CHECK_CHAR_COERCE_INT (key);
|
|
3205 character_to_event (XCHAR (key), &event,
|
|
3206 XCONSOLE (Vselected_console), 0);
|
|
3207 format_event_object (buf, &event, 1);
|
|
3208 }
|
|
3209 else
|
|
3210 format_event_object (buf, XEVENT (key), 1);
|
173
|
3211 return build_string (buf);
|
0
|
3212 }
|
|
3213
|
|
3214 if (CONSP (key))
|
|
3215 {
|
|
3216 char buf[255];
|
|
3217 char *bufp = buf;
|
|
3218 Lisp_Object rest;
|
|
3219 buf[0] = 0;
|
|
3220 LIST_LOOP (rest, key)
|
|
3221 {
|
|
3222 Lisp_Object keysym = XCAR (rest);
|
|
3223 if (EQ (keysym, Qcontrol)) strcpy (bufp, "C-"), bufp += 2;
|
|
3224 else if (EQ (keysym, Qctrl)) strcpy (bufp, "C-"), bufp += 2;
|
|
3225 else if (EQ (keysym, Qmeta)) strcpy (bufp, "M-"), bufp += 2;
|
|
3226 else if (EQ (keysym, Qsuper)) strcpy (bufp, "S-"), bufp += 2;
|
|
3227 else if (EQ (keysym, Qhyper)) strcpy (bufp, "H-"), bufp += 2;
|
|
3228 else if (EQ (keysym, Qalt)) strcpy (bufp, "A-"), bufp += 2;
|
|
3229 else if (EQ (keysym, Qshift)) strcpy (bufp, "Sh-"), bufp += 3;
|
70
|
3230 else if (CHAR_OR_CHAR_INTP (keysym))
|
|
3231 {
|
|
3232 bufp += set_charptr_emchar ((Bufbyte *) bufp,
|
|
3233 XCHAR_OR_CHAR_INT (keysym));
|
|
3234 *bufp = 0;
|
|
3235 }
|
0
|
3236 else
|
|
3237 {
|
|
3238 CHECK_SYMBOL (keysym);
|
|
3239 #if 0 /* This is bogus */
|
185
|
3240 if (EQ (keysym, QKlinefeed)) strcpy (bufp, "LFD");
|
|
3241 else if (EQ (keysym, QKtab)) strcpy (bufp, "TAB");
|
|
3242 else if (EQ (keysym, QKreturn)) strcpy (bufp, "RET");
|
|
3243 else if (EQ (keysym, QKescape)) strcpy (bufp, "ESC");
|
|
3244 else if (EQ (keysym, QKdelete)) strcpy (bufp, "DEL");
|
|
3245 else if (EQ (keysym, QKspace)) strcpy (bufp, "SPC");
|
|
3246 else if (EQ (keysym, QKbackspace)) strcpy (bufp, "BS");
|
0
|
3247 else
|
|
3248 #endif
|
|
3249 strcpy (bufp, (char *) string_data (XSYMBOL (keysym)->name));
|
|
3250 if (!NILP (XCDR (rest)))
|
|
3251 signal_simple_error ("invalid key description",
|
|
3252 key);
|
|
3253 }
|
|
3254 }
|
|
3255 return build_string (buf);
|
|
3256 }
|
|
3257 return Fsingle_key_description
|
|
3258 (wrong_type_argument (intern ("char-or-event-p"), key));
|
|
3259 }
|
|
3260
|
20
|
3261 DEFUN ("text-char-description", Ftext_char_description, 1, 1, 0, /*
|
0
|
3262 Return a pretty description of file-character CHR.
|
185
|
3263 Unprintable characters turn into "^char" or \\NNN, depending on the value
|
0
|
3264 of the `ctl-arrow' variable.
|
|
3265 This differs from `single-key-description' in that it returns a description
|
|
3266 of a character from a buffer rather than a key read from the user.
|
20
|
3267 */
|
|
3268 (chr))
|
0
|
3269 {
|
|
3270 Bufbyte buf[200];
|
|
3271 Bufbyte *p;
|
|
3272 unsigned int c;
|
|
3273 Lisp_Object ctl_arrow = current_buffer->ctl_arrow;
|
|
3274 int ctl_p = !NILP (ctl_arrow);
|
70
|
3275 Emchar printable_min = (CHAR_OR_CHAR_INTP (ctl_arrow)
|
|
3276 ? XCHAR_OR_CHAR_INT (ctl_arrow)
|
|
3277 : ((EQ (ctl_arrow, Qt) || NILP (ctl_arrow))
|
|
3278 ? 256 : 160));
|
0
|
3279
|
|
3280 if (EVENTP (chr))
|
|
3281 {
|
|
3282 Lisp_Object ch = Fevent_to_character (chr, Qnil, Qnil, Qt);
|
|
3283 if (NILP (ch))
|
|
3284 return
|
|
3285 signal_simple_continuable_error
|
|
3286 ("character has no ASCII equivalent", Fcopy_event (chr, Qnil));
|
|
3287 chr = ch;
|
|
3288 }
|
|
3289
|
|
3290 CHECK_CHAR_COERCE_INT (chr);
|
|
3291
|
|
3292 c = XCHAR (chr);
|
|
3293 p = buf;
|
|
3294
|
|
3295 if (c >= printable_min)
|
|
3296 {
|
|
3297 p += set_charptr_emchar (p, c);
|
|
3298 }
|
|
3299 else if (c < 040 && ctl_p)
|
|
3300 {
|
|
3301 *p++ = '^';
|
|
3302 *p++ = c + 64; /* 'A' - 1 */
|
|
3303 }
|
|
3304 else if (c == 0177)
|
|
3305 {
|
|
3306 *p++ = '^';
|
|
3307 *p++ = '?';
|
|
3308 }
|
|
3309 else if (c >= 0200 || c < 040)
|
|
3310 {
|
|
3311 *p++ = '\\';
|
70
|
3312 #ifdef MULE
|
|
3313 /* !!#### This syntax is not readable. It will
|
|
3314 be interpreted as a 3-digit octal number rather
|
|
3315 than a 7-digit octal number. */
|
|
3316 if (c >= 0400)
|
|
3317 {
|
|
3318 *p++ = '0' + ((c & 07000000) >> 18);
|
|
3319 *p++ = '0' + ((c & 0700000) >> 15);
|
|
3320 *p++ = '0' + ((c & 070000) >> 12);
|
|
3321 *p++ = '0' + ((c & 07000) >> 9);
|
|
3322 }
|
|
3323 #endif
|
0
|
3324 *p++ = '0' + ((c & 0700) >> 6);
|
|
3325 *p++ = '0' + ((c & 0070) >> 3);
|
|
3326 *p++ = '0' + ((c & 0007));
|
|
3327 }
|
|
3328 else
|
|
3329 {
|
|
3330 p += set_charptr_emchar (p, c);
|
|
3331 }
|
|
3332
|
|
3333 *p = 0;
|
|
3334 return build_string ((char *) buf);
|
|
3335 }
|
|
3336
|
|
3337
|
|
3338 /************************************************************************/
|
|
3339 /* where-is (mapping bindings to keys) */
|
|
3340 /************************************************************************/
|
|
3341
|
|
3342 static Lisp_Object
|
|
3343 where_is_internal (Lisp_Object definition, Lisp_Object *maps, int nmaps,
|
|
3344 Lisp_Object firstonly, char *target_buffer);
|
|
3345
|
20
|
3346 DEFUN ("where-is-internal", Fwhere_is_internal, 1, 5, 0, /*
|
0
|
3347 Return list of keys that invoke DEFINITION in KEYMAPS.
|
|
3348 KEYMAPS can be either a keymap (meaning search in that keymap and the
|
|
3349 current global keymap) or a list of keymaps (meaning search in exactly
|
|
3350 those keymaps and no others). If KEYMAPS is nil, search in the currently
|
|
3351 applicable maps for EVENT-OR-KEYS (this is equivalent to specifying
|
|
3352 `(current-keymaps EVENT-OR-KEYS)' as the argument to KEYMAPS).
|
|
3353
|
|
3354 If optional 3rd arg FIRSTONLY is non-nil, return a vector representing
|
|
3355 the first key sequence found, rather than a list of all possible key
|
|
3356 sequences.
|
|
3357
|
|
3358 If optional 4th arg NOINDIRECT is non-nil, don't follow indirections
|
|
3359 to other keymaps or slots. This makes it possible to search for an
|
|
3360 indirect definition itself.
|
20
|
3361 */
|
|
3362 (definition, keymaps, firstonly, noindirect, event_or_keys))
|
0
|
3363 {
|
|
3364 /* This function can GC */
|
|
3365 Lisp_Object maps[100];
|
|
3366 Lisp_Object *gubbish = maps;
|
|
3367 int nmaps;
|
|
3368
|
|
3369 /* Get keymaps as an array */
|
|
3370 if (NILP (keymaps))
|
|
3371 {
|
|
3372 nmaps = get_relevant_keymaps (event_or_keys, countof (maps),
|
|
3373 gubbish);
|
|
3374 if (nmaps > countof (maps))
|
|
3375 {
|
185
|
3376 gubbish = alloca_array (Lisp_Object, nmaps);
|
0
|
3377 nmaps = get_relevant_keymaps (event_or_keys, nmaps, gubbish);
|
|
3378 }
|
|
3379 }
|
|
3380 else if (CONSP (keymaps))
|
|
3381 {
|
|
3382 Lisp_Object rest;
|
|
3383 int i;
|
|
3384
|
|
3385 nmaps = XINT (Flength (keymaps));
|
|
3386 if (nmaps > countof (maps))
|
|
3387 {
|
185
|
3388 gubbish = alloca_array (Lisp_Object, nmaps);
|
0
|
3389 }
|
|
3390 for (rest = keymaps, i = 0; !NILP (rest);
|
|
3391 rest = XCDR (keymaps), i++)
|
|
3392 {
|
|
3393 gubbish[i] = get_keymap (XCAR (keymaps), 1, 1);
|
|
3394 }
|
|
3395 }
|
|
3396 else
|
|
3397 {
|
|
3398 nmaps = 1;
|
|
3399 gubbish[0] = get_keymap (keymaps, 1, 1);
|
|
3400 if (!EQ (gubbish[0], Vcurrent_global_map))
|
|
3401 {
|
|
3402 gubbish[1] = Vcurrent_global_map;
|
|
3403 nmaps++;
|
|
3404 }
|
|
3405 }
|
185
|
3406
|
0
|
3407 return where_is_internal (definition, gubbish, nmaps, firstonly, 0);
|
|
3408 }
|
|
3409
|
|
3410 /* This function is like
|
|
3411 (key-description (where-is-internal definition nil t))
|
185
|
3412 except that it writes its output into a (char *) buffer that you
|
0
|
3413 provide; it doesn't cons (or allocate memory) at all, so it's
|
|
3414 very fast. This is used by menubar.c.
|
|
3415 */
|
|
3416 void
|
|
3417 where_is_to_char (Lisp_Object definition, char *buffer)
|
|
3418 {
|
|
3419 /* This function can GC */
|
|
3420 Lisp_Object maps[100];
|
|
3421 Lisp_Object *gubbish = maps;
|
|
3422 int nmaps;
|
|
3423
|
|
3424 /* Get keymaps as an array */
|
|
3425 nmaps = get_relevant_keymaps (Qnil, countof (maps), gubbish);
|
|
3426 if (nmaps > countof (maps))
|
|
3427 {
|
185
|
3428 gubbish = alloca_array (Lisp_Object, nmaps);
|
0
|
3429 nmaps = get_relevant_keymaps (Qnil, nmaps, gubbish);
|
|
3430 }
|
|
3431
|
|
3432 buffer[0] = 0;
|
|
3433 where_is_internal (definition, maps, nmaps, Qt, buffer);
|
|
3434 }
|
|
3435
|
|
3436
|
185
|
3437 static Lisp_Object
|
0
|
3438 raw_keys_to_keys (struct key_data *keys, int count)
|
|
3439 {
|
|
3440 Lisp_Object result = make_vector (count, Qnil);
|
|
3441 while (count--)
|
173
|
3442 XVECTOR_DATA (result) [count] = make_key_description (&(keys[count]), 1);
|
|
3443 return result;
|
0
|
3444 }
|
|
3445
|
|
3446
|
|
3447 static void
|
|
3448 format_raw_keys (struct key_data *keys, int count, char *buf)
|
|
3449 {
|
|
3450 int i;
|
|
3451 struct Lisp_Event event;
|
|
3452 event.event_type = key_press_event;
|
|
3453 event.channel = Vselected_console;
|
|
3454 for (i = 0; i < count; i++)
|
|
3455 {
|
|
3456 event.event.key.keysym = keys[i].keysym;
|
|
3457 event.event.key.modifiers = keys[i].modifiers;
|
|
3458 format_event_object (buf, &event, 1);
|
|
3459 buf += strlen (buf);
|
|
3460 if (i < count-1)
|
|
3461 buf[0] = ' ', buf++;
|
|
3462 }
|
|
3463 }
|
|
3464
|
|
3465
|
|
3466 /* definition is the thing to look for.
|
|
3467 map is a keymap.
|
|
3468 shadow is an array of shadow_count keymaps; if there is a different
|
|
3469 binding in any of the keymaps of a key that we are considering
|
|
3470 returning, then we reconsider.
|
|
3471 firstonly means give up after finding the first match;
|
|
3472 keys_so_far and modifiers_so_far describe which map we're looking in;
|
|
3473 If we're in the "meta" submap of the map that "C-x 4" is bound to,
|
|
3474 then keys_so_far will be {(control x), \4}, and modifiers_so_far
|
|
3475 will be MOD_META. That is, keys_so_far is the chain of keys that we
|
|
3476 have followed, and modifiers_so_far_so_far is the bits (partial keys)
|
|
3477 beyond that.
|
185
|
3478
|
0
|
3479 (keys_so_far is a global buffer and the keys_count arg says how much
|
|
3480 of it we're currently interested in.)
|
185
|
3481
|
0
|
3482 If target_buffer is provided, then we write a key-description into it,
|
|
3483 to avoid consing a string. This only works with firstonly on.
|
|
3484 */
|
|
3485
|
|
3486 struct where_is_closure
|
|
3487 {
|
|
3488 Lisp_Object definition;
|
|
3489 Lisp_Object *shadow;
|
|
3490 int shadow_count;
|
|
3491 int firstonly;
|
|
3492 int keys_count;
|
|
3493 unsigned int modifiers_so_far;
|
|
3494 char *target_buffer;
|
|
3495 struct key_data *keys_so_far;
|
|
3496 int keys_so_far_total_size;
|
|
3497 int keys_so_far_malloced;
|
|
3498 };
|
|
3499
|
|
3500 static Lisp_Object where_is_recursive_mapper (Lisp_Object map, void *arg);
|
|
3501
|
|
3502 static Lisp_Object
|
|
3503 where_is_recursive_mapper (Lisp_Object map, void *arg)
|
|
3504 {
|
|
3505 /* This function can GC */
|
185
|
3506 struct where_is_closure *c = (struct where_is_closure *) arg;
|
0
|
3507 Lisp_Object definition = c->definition;
|
|
3508 CONST int firstonly = c->firstonly;
|
|
3509 CONST unsigned int keys_count = c->keys_count;
|
|
3510 CONST unsigned int modifiers_so_far = c->modifiers_so_far;
|
|
3511 char *target_buffer = c->target_buffer;
|
|
3512 Lisp_Object keys = Fgethash (definition,
|
|
3513 XKEYMAP (map)->inverse_table,
|
|
3514 Qnil);
|
|
3515 Lisp_Object submaps;
|
|
3516 Lisp_Object result = Qnil;
|
|
3517
|
|
3518 if (!NILP (keys))
|
|
3519 {
|
185
|
3520 /* One or more keys in this map match the definition we're looking for.
|
|
3521 Verify that these bindings aren't shadowed by other bindings
|
0
|
3522 in the shadow maps. Either nil or number as value from
|
185
|
3523 raw_lookup_key() means undefined. */
|
0
|
3524 struct key_data *so_far = c->keys_so_far;
|
|
3525
|
|
3526 for (;;) /* loop over all keys that match */
|
|
3527 {
|
|
3528 Lisp_Object k = ((CONSP (keys)) ? XCAR (keys) : keys);
|
|
3529 int i;
|
185
|
3530
|
0
|
3531 so_far [keys_count].keysym = k;
|
|
3532 so_far [keys_count].modifiers = modifiers_so_far;
|
|
3533
|
|
3534 /* now loop over all shadow maps */
|
|
3535 for (i = 0; i < c->shadow_count; i++)
|
|
3536 {
|
|
3537 Lisp_Object shadowed = raw_lookup_key (c->shadow[i],
|
|
3538 so_far,
|
185
|
3539 keys_count + 1,
|
0
|
3540 0, 1);
|
|
3541
|
|
3542 if (NILP (shadowed) || CHARP (shadowed) ||
|
|
3543 EQ (shadowed, definition))
|
|
3544 continue; /* we passed this test; it's not shadowed here. */
|
|
3545 else
|
|
3546 /* ignore this key binding, since it actually has a
|
|
3547 different binding in a shadowing map */
|
|
3548 goto c_doesnt_have_proper_loop_exit_statements;
|
|
3549 }
|
|
3550
|
|
3551 /* OK, the key is for real */
|
|
3552 if (target_buffer)
|
|
3553 {
|
|
3554 if (!firstonly) abort ();
|
|
3555 format_raw_keys (so_far, keys_count + 1, target_buffer);
|
173
|
3556 return make_int (1);
|
0
|
3557 }
|
|
3558 else if (firstonly)
|
|
3559 return raw_keys_to_keys (so_far, keys_count + 1);
|
|
3560 else
|
|
3561 result = Fcons (raw_keys_to_keys (so_far, keys_count + 1),
|
|
3562 result);
|
|
3563
|
|
3564 c_doesnt_have_proper_loop_exit_statements:
|
|
3565 /* now on to the next matching key ... */
|
|
3566 if (!CONSP (keys)) break;
|
|
3567 keys = XCDR (keys);
|
|
3568 }
|
|
3569 }
|
|
3570
|
|
3571 /* Now search the sub-keymaps of this map.
|
185
|
3572 If we're in "firstonly" mode and have already found one, this
|
0
|
3573 point is not reached. If we get one from lower down, either
|
|
3574 return it immediately (in firstonly mode) or tack it onto the
|
|
3575 end of the ones we've gotten so far.
|
|
3576 */
|
|
3577 for (submaps = keymap_submaps (map);
|
|
3578 !NILP (submaps);
|
|
3579 submaps = XCDR (submaps))
|
|
3580 {
|
|
3581 Lisp_Object key = XCAR (XCAR (submaps));
|
|
3582 Lisp_Object submap = XCDR (XCAR (submaps));
|
|
3583 unsigned int lower_modifiers;
|
|
3584 int lower_keys_count = keys_count;
|
|
3585 unsigned int bucky;
|
|
3586
|
185
|
3587 submap = get_keymap (submap, 0, 0);
|
0
|
3588
|
|
3589 if (EQ (submap, map))
|
|
3590 /* Arrgh! Some loser has introduced a loop... */
|
|
3591 continue;
|
|
3592
|
|
3593 /* If this is not a keymap, then that's probably because someone
|
|
3594 did an `fset' of a symbol that used to point to a map such that
|
|
3595 it no longer does. Sigh. Ignore this, and invalidate the cache
|
|
3596 so that it doesn't happen to us next time too.
|
|
3597 */
|
|
3598 if (NILP (submap))
|
|
3599 {
|
|
3600 XKEYMAP (map)->sub_maps_cache = Qt;
|
|
3601 continue;
|
|
3602 }
|
|
3603
|
|
3604 /* If the map is a "bucky" map, then add a bit to the
|
|
3605 modifiers_so_far list.
|
|
3606 Otherwise, add a new raw_key onto the end of keys_so_far.
|
|
3607 */
|
|
3608 bucky = MODIFIER_HASH_KEY_BITS (key);
|
|
3609 if (bucky != 0)
|
|
3610 lower_modifiers = (modifiers_so_far | bucky);
|
|
3611 else
|
|
3612 {
|
|
3613 struct key_data *so_far = c->keys_so_far;
|
|
3614 lower_modifiers = 0;
|
|
3615 so_far [lower_keys_count].keysym = key;
|
|
3616 so_far [lower_keys_count].modifiers = modifiers_so_far;
|
|
3617 lower_keys_count++;
|
|
3618 }
|
|
3619
|
|
3620 if (lower_keys_count >= c->keys_so_far_total_size)
|
|
3621 {
|
|
3622 int size = lower_keys_count + 50;
|
|
3623 if (! c->keys_so_far_malloced)
|
|
3624 {
|
185
|
3625 struct key_data *new = xnew_array (struct key_data, size);
|
0
|
3626 memcpy ((void *)new, (const void *)c->keys_so_far,
|
|
3627 c->keys_so_far_total_size * sizeof (struct key_data));
|
|
3628 }
|
|
3629 else
|
185
|
3630 XREALLOC_ARRAY (c->keys_so_far, struct key_data, size);
|
0
|
3631
|
|
3632 c->keys_so_far_total_size = size;
|
|
3633 c->keys_so_far_malloced = 1;
|
|
3634 }
|
|
3635
|
|
3636 {
|
|
3637 Lisp_Object lower;
|
|
3638
|
|
3639 c->keys_count = lower_keys_count;
|
|
3640 c->modifiers_so_far = lower_modifiers;
|
|
3641
|
185
|
3642 lower = traverse_keymaps (submap, Qnil, where_is_recursive_mapper, c);
|
|
3643
|
0
|
3644 c->keys_count = keys_count;
|
|
3645 c->modifiers_so_far = modifiers_so_far;
|
|
3646
|
|
3647 if (!firstonly)
|
|
3648 result = nconc2 (lower, result);
|
|
3649 else if (!NILP (lower))
|
173
|
3650 return lower;
|
0
|
3651 }
|
|
3652 }
|
173
|
3653 return result;
|
0
|
3654 }
|
|
3655
|
|
3656
|
|
3657 static Lisp_Object
|
|
3658 where_is_internal (Lisp_Object definition, Lisp_Object *maps, int nmaps,
|
|
3659 Lisp_Object firstonly, char *target_buffer)
|
|
3660 {
|
|
3661 /* This function can GC */
|
|
3662 Lisp_Object result = Qnil;
|
|
3663 int i;
|
|
3664 struct key_data raw[20];
|
|
3665 struct where_is_closure c;
|
|
3666
|
|
3667 c.definition = definition;
|
|
3668 c.shadow = maps;
|
|
3669 c.firstonly = !NILP (firstonly);
|
|
3670 c.target_buffer = target_buffer;
|
|
3671 c.keys_so_far = raw;
|
|
3672 c.keys_so_far_total_size = countof (raw);
|
|
3673 c.keys_so_far_malloced = 0;
|
|
3674
|
|
3675 /* Loop over each of the maps, accumulating the keys found.
|
|
3676 For each map searched, all previous maps shadow this one
|
|
3677 so that bogus keys aren't listed. */
|
|
3678 for (i = 0; i < nmaps; i++)
|
|
3679 {
|
|
3680 Lisp_Object this_result;
|
|
3681 c.shadow_count = i;
|
|
3682 /* Reset the things set in each iteration */
|
|
3683 c.keys_count = 0;
|
|
3684 c.modifiers_so_far = 0;
|
|
3685
|
|
3686 this_result = traverse_keymaps (maps[i], Qnil, where_is_recursive_mapper,
|
|
3687 &c);
|
|
3688 if (!NILP (firstonly))
|
|
3689 {
|
|
3690 result = this_result;
|
|
3691 if (!NILP (result))
|
|
3692 break;
|
|
3693 }
|
|
3694 else
|
|
3695 result = nconc2 (this_result, result);
|
|
3696 }
|
|
3697
|
|
3698 if (NILP (firstonly))
|
|
3699 result = Fnreverse (result);
|
|
3700
|
|
3701 if (c.keys_so_far_malloced)
|
|
3702 xfree (c.keys_so_far);
|
173
|
3703 return result;
|
0
|
3704 }
|
|
3705
|
|
3706
|
|
3707 /************************************************************************/
|
|
3708 /* Describing keymaps */
|
|
3709 /************************************************************************/
|
|
3710
|
20
|
3711 DEFUN ("describe-bindings-internal", Fdescribe_bindings_internal, 1, 5, 0, /*
|
0
|
3712 Insert a list of all defined keys and their definitions in MAP.
|
185
|
3713 Optional second argument ALL says whether to include even "uninteresting"
|
0
|
3714 definitions (ie symbols with a non-nil `suppress-keymap' property.
|
|
3715 Third argument SHADOW is a list of keymaps whose bindings shadow those
|
|
3716 of map; if a binding is present in any shadowing map, it is not printed.
|
|
3717 Fourth argument PREFIX, if non-nil, should be a key sequence;
|
|
3718 only bindings which start with that key sequence will be printed.
|
|
3719 Fifth argument MOUSE-ONLY-P says to only print bindings for mouse clicks.
|
20
|
3720 */
|
|
3721 (map, all, shadow, prefix, mouse_only_p))
|
0
|
3722 {
|
|
3723 /* This function can GC */
|
|
3724 describe_map_tree (map, NILP (all), shadow, prefix,
|
|
3725 !NILP (mouse_only_p));
|
173
|
3726 return Qnil;
|
0
|
3727 }
|
|
3728
|
|
3729
|
|
3730 /* Insert a desription of the key bindings in STARTMAP,
|
|
3731 followed by those of all maps reachable through STARTMAP.
|
|
3732 If PARTIAL is nonzero, omit certain "uninteresting" commands
|
|
3733 (such as `undefined').
|
|
3734 If SHADOW is non-nil, it is a list of other maps;
|
|
3735 don't mention keys which would be shadowed by any of them
|
185
|
3736 If PREFIX is non-nil, only list bindings which start with those keys.
|
0
|
3737 */
|
|
3738
|
|
3739 void
|
|
3740 describe_map_tree (Lisp_Object startmap, int partial, Lisp_Object shadow,
|
|
3741 Lisp_Object prefix, int mice_only_p)
|
|
3742 {
|
|
3743 /* This function can GC */
|
|
3744 Lisp_Object maps = Qnil;
|
|
3745 struct gcpro gcpro1, gcpro2; /* get_keymap may autoload */
|
|
3746 GCPRO2 (maps, shadow);
|
|
3747
|
|
3748 maps = Faccessible_keymaps (startmap, prefix);
|
|
3749
|
|
3750 for (; !NILP (maps); maps = Fcdr (maps))
|
|
3751 {
|
|
3752 Lisp_Object sub_shadow = Qnil;
|
|
3753 Lisp_Object elt = Fcar (maps);
|
185
|
3754 Lisp_Object tail;
|
0
|
3755 int no_prefix = (VECTORP (Fcar (elt))
|
|
3756 && XINT (Flength (Fcar (elt))) == 0);
|
|
3757 struct gcpro ngcpro1, ngcpro2, ngcpro3;
|
|
3758 NGCPRO3 (sub_shadow, elt, tail);
|
|
3759
|
185
|
3760 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
|
0
|
3761 {
|
185
|
3762 Lisp_Object shmap = XCAR (tail);
|
0
|
3763
|
|
3764 /* If the sequence by which we reach this keymap is zero-length,
|
|
3765 then the shadow maps for this keymap are just SHADOW. */
|
|
3766 if (no_prefix)
|
|
3767 ;
|
|
3768 /* If the sequence by which we reach this keymap actually has
|
|
3769 some elements, then the sequence's definition in SHADOW is
|
|
3770 what we should use. */
|
|
3771 else
|
|
3772 {
|
185
|
3773 shmap = Flookup_key (shmap, Fcar (elt), Qt);
|
|
3774 if (CHARP (shmap))
|
|
3775 shmap = Qnil;
|
0
|
3776 }
|
|
3777
|
185
|
3778 if (!NILP (shmap))
|
0
|
3779 {
|
185
|
3780 Lisp_Object shm = get_keymap (shmap, 0, 1);
|
|
3781 /* If shmap is not nil and not a keymap, it completely
|
|
3782 shadows this map, so don't describe this map at all. */
|
0
|
3783 if (!KEYMAPP (shm))
|
|
3784 goto SKIP;
|
|
3785 sub_shadow = Fcons (shm, sub_shadow);
|
|
3786 }
|
|
3787 }
|
|
3788
|
|
3789 {
|
|
3790 /* Describe the contents of map MAP, assuming that this map
|
|
3791 itself is reached by the sequence of prefix keys KEYS (a vector).
|
|
3792 PARTIAL and SHADOW are as in `describe_map_tree'. */
|
|
3793 Lisp_Object keysdesc
|
|
3794 = ((!no_prefix)
|
|
3795 ? concat2 (Fkey_description (Fcar (elt)), Vsingle_space_string)
|
|
3796 : Qnil);
|
|
3797 describe_map (Fcdr (elt), keysdesc,
|
|
3798 describe_command,
|
|
3799 partial,
|
|
3800 sub_shadow,
|
|
3801 mice_only_p);
|
|
3802 }
|
|
3803 SKIP:
|
|
3804 NUNGCPRO;
|
|
3805 }
|
|
3806 UNGCPRO;
|
|
3807 }
|
|
3808
|
|
3809
|
|
3810 static void
|
|
3811 describe_command (Lisp_Object definition)
|
|
3812 {
|
|
3813 /* This function can GC */
|
|
3814 Lisp_Object buffer;
|
|
3815 int keymapp = !NILP (Fkeymapp (definition));
|
|
3816 struct gcpro gcpro1, gcpro2;
|
|
3817 GCPRO2 (definition, buffer);
|
|
3818
|
|
3819 XSETBUFFER (buffer, current_buffer);
|
|
3820 Findent_to (make_int (16), make_int (3), buffer);
|
|
3821 if (keymapp)
|
|
3822 buffer_insert_c_string (XBUFFER (buffer), "<< ");
|
|
3823
|
|
3824 if (SYMBOLP (definition))
|
|
3825 {
|
|
3826 buffer_insert1 (XBUFFER (buffer), Fsymbol_name (definition));
|
|
3827 }
|
|
3828 else if (STRINGP (definition) || VECTORP (definition))
|
|
3829 {
|
|
3830 buffer_insert_c_string (XBUFFER (buffer), "Kbd Macro: ");
|
|
3831 buffer_insert1 (XBUFFER (buffer), Fkey_description (definition));
|
|
3832 }
|
|
3833 else if (COMPILED_FUNCTIONP (definition))
|
|
3834 buffer_insert_c_string (XBUFFER (buffer), "Anonymous Compiled Function");
|
|
3835 else if (CONSP (definition) && EQ (XCAR (definition), Qlambda))
|
|
3836 buffer_insert_c_string (XBUFFER (buffer), "Anonymous Lambda");
|
|
3837 else if (KEYMAPP (definition))
|
|
3838 {
|
|
3839 Lisp_Object name = XKEYMAP (definition)->name;
|
|
3840 if (STRINGP (name) || (SYMBOLP (name) && !NILP (name)))
|
|
3841 {
|
|
3842 buffer_insert_c_string (XBUFFER (buffer), "Prefix command ");
|
185
|
3843 if (SYMBOLP (name)
|
0
|
3844 && EQ (find_symbol_value (name), definition))
|
|
3845 buffer_insert1 (XBUFFER (buffer), Fsymbol_name (name));
|
|
3846 else
|
|
3847 {
|
|
3848 buffer_insert1 (XBUFFER (buffer), Fprin1_to_string (name, Qnil));
|
|
3849 }
|
|
3850 }
|
|
3851 else
|
|
3852 buffer_insert_c_string (XBUFFER (buffer), "Prefix Command");
|
|
3853 }
|
|
3854 else
|
|
3855 buffer_insert_c_string (XBUFFER (buffer), "??");
|
|
3856
|
|
3857 if (keymapp)
|
|
3858 buffer_insert_c_string (XBUFFER (buffer), " >>");
|
|
3859 buffer_insert_c_string (XBUFFER (buffer), "\n");
|
|
3860 UNGCPRO;
|
|
3861 }
|
|
3862
|
|
3863 struct describe_map_closure
|
|
3864 {
|
|
3865 Lisp_Object *list; /* pointer to the list to update */
|
|
3866 Lisp_Object partial; /* whether to ignore suppressed commands */
|
|
3867 Lisp_Object shadow; /* list of maps shadowing this one */
|
|
3868 Lisp_Object self; /* this map */
|
|
3869 Lisp_Object self_root; /* this map, or some map that has this map as
|
|
3870 a parent. this is the base of the tree */
|
|
3871 int mice_only_p; /* whether we are to display only button bindings */
|
|
3872 };
|
|
3873
|
|
3874 struct describe_map_shadow_closure
|
|
3875 {
|
|
3876 CONST struct key_data *raw_key;
|
|
3877 Lisp_Object self;
|
|
3878 };
|
|
3879
|
|
3880 static Lisp_Object
|
|
3881 describe_map_mapper_shadow_search (Lisp_Object map, void *arg)
|
|
3882 {
|
185
|
3883 struct describe_map_shadow_closure *c =
|
|
3884 (struct describe_map_shadow_closure *) arg;
|
0
|
3885
|
|
3886 if (EQ (map, c->self))
|
173
|
3887 return Qzero; /* Not shadowed; terminate search */
|
185
|
3888
|
|
3889 return (!NILP (keymap_lookup_directly (map,
|
|
3890 c->raw_key->keysym,
|
|
3891 c->raw_key->modifiers)))
|
|
3892 ? Qt : Qnil;
|
0
|
3893 }
|
185
|
3894
|
0
|
3895
|
|
3896 static Lisp_Object
|
|
3897 keymap_lookup_inherited_mapper (Lisp_Object km, void *arg)
|
|
3898 {
|
185
|
3899 struct key_data *k = (struct key_data *) arg;
|
173
|
3900 return keymap_lookup_directly (km, k->keysym, k->modifiers);
|
0
|
3901 }
|
|
3902
|
|
3903
|
|
3904 static void
|
|
3905 describe_map_mapper (CONST struct key_data *key,
|
|
3906 Lisp_Object binding,
|
|
3907 void *describe_map_closure)
|
|
3908 {
|
|
3909 /* This function can GC */
|
185
|
3910 struct describe_map_closure *closure =
|
|
3911 (struct describe_map_closure *) describe_map_closure;
|
0
|
3912 Lisp_Object keysym = key->keysym;
|
|
3913 unsigned int modifiers = key->modifiers;
|
|
3914
|
|
3915 /* Dont mention suppressed commands. */
|
|
3916 if (SYMBOLP (binding)
|
|
3917 && !NILP (closure->partial)
|
|
3918 && !NILP (Fget (binding, closure->partial, Qnil)))
|
|
3919 return;
|
185
|
3920
|
0
|
3921 /* If we're only supposed to display mouse bindings and this isn't one,
|
|
3922 then bug out. */
|
|
3923 if (closure->mice_only_p &&
|
185
|
3924 (! (EQ (keysym, Qbutton0) ||
|
|
3925 EQ (keysym, Qbutton1) ||
|
|
3926 EQ (keysym, Qbutton2) ||
|
|
3927 EQ (keysym, Qbutton3) ||
|
|
3928 EQ (keysym, Qbutton4) ||
|
|
3929 EQ (keysym, Qbutton5) ||
|
|
3930 EQ (keysym, Qbutton6) ||
|
|
3931 EQ (keysym, Qbutton7))))
|
0
|
3932 return;
|
|
3933
|
|
3934 /* If this command in this map is shadowed by some other map, ignore it. */
|
|
3935 {
|
|
3936 Lisp_Object tail;
|
|
3937
|
|
3938 for (tail = closure->shadow; CONSP (tail); tail = XCDR (tail))
|
|
3939 {
|
|
3940 QUIT;
|
|
3941 if (!NILP (traverse_keymaps (XCAR (tail), Qnil,
|
|
3942 keymap_lookup_inherited_mapper,
|
|
3943 /* Cast to discard `const' */
|
|
3944 (void *)key)))
|
|
3945 return;
|
|
3946 }
|
|
3947 }
|
|
3948
|
|
3949 /* If this key is in some map of which this map is a parent, then ignore
|
|
3950 it (in that case, it has been shadowed).
|
|
3951 */
|
|
3952 {
|
|
3953 Lisp_Object sh;
|
|
3954 struct describe_map_shadow_closure c;
|
|
3955 c.raw_key = key;
|
|
3956 c.self = closure->self;
|
|
3957
|
|
3958 sh = traverse_keymaps (closure->self_root, Qnil,
|
|
3959 describe_map_mapper_shadow_search, &c);
|
|
3960 if (!NILP (sh) && !ZEROP (sh))
|
|
3961 return;
|
|
3962 }
|
|
3963
|
|
3964 /* Otherwise add it to the list to be sorted. */
|
|
3965 *(closure->list) = Fcons (Fcons (Fcons (keysym, make_int (modifiers)),
|
|
3966 binding),
|
|
3967 *(closure->list));
|
|
3968 }
|
|
3969
|
|
3970
|
|
3971 static int
|
185
|
3972 describe_map_sort_predicate (Lisp_Object obj1, Lisp_Object obj2,
|
0
|
3973 Lisp_Object pred)
|
|
3974 {
|
|
3975 /* obj1 and obj2 are conses of the form
|
|
3976 ( ( <keysym> . <modifiers> ) . <binding> )
|
|
3977 keysym and modifiers are used, binding is ignored.
|
|
3978 */
|
|
3979 unsigned int bit1, bit2;
|
|
3980 obj1 = XCAR (obj1);
|
|
3981 obj2 = XCAR (obj2);
|
|
3982 bit1 = XINT (XCDR (obj1));
|
|
3983 bit2 = XINT (XCDR (obj2));
|
|
3984 if (bit1 != bit2)
|
173
|
3985 return bit1 < bit2 ? 1 : -1;
|
0
|
3986 else
|
|
3987 return map_keymap_sort_predicate (obj1, obj2, pred);
|
|
3988 }
|
|
3989
|
|
3990 /* Elide 2 or more consecutive numeric keysyms bound to the same thing,
|
|
3991 or 2 or more symbolic keysyms that are bound to the same thing and
|
|
3992 have consecutive character-set-properties.
|
|
3993 */
|
|
3994 static int
|
|
3995 elide_next_two_p (Lisp_Object list)
|
|
3996 {
|
|
3997 Lisp_Object s1, s2;
|
|
3998
|
|
3999 if (NILP (XCDR (list)))
|
|
4000 return 0;
|
|
4001
|
|
4002 /* next two bindings differ */
|
|
4003 if (!EQ (XCDR (XCAR (list)),
|
|
4004 XCDR (XCAR (XCDR (list)))))
|
|
4005 return 0;
|
|
4006
|
|
4007 /* next two modifier-sets differ */
|
|
4008 if (!EQ (XCDR (XCAR (XCAR (list))),
|
|
4009 XCDR (XCAR (XCAR (XCDR (list))))))
|
|
4010 return 0;
|
|
4011
|
|
4012 s1 = XCAR (XCAR (XCAR (list)));
|
|
4013 s2 = XCAR (XCAR (XCAR (XCDR (list))));
|
|
4014
|
|
4015 if (SYMBOLP (s1))
|
|
4016 {
|
|
4017 Lisp_Object code = Fget (s1, Vcharacter_set_property, Qnil);
|
70
|
4018 if (CHAR_OR_CHAR_INTP (code))
|
|
4019 {
|
|
4020 s1 = code;
|
|
4021 CHECK_CHAR_COERCE_INT (s1);
|
|
4022 }
|
0
|
4023 else return 0;
|
|
4024 }
|
|
4025 if (SYMBOLP (s2))
|
|
4026 {
|
|
4027 Lisp_Object code = Fget (s2, Vcharacter_set_property, Qnil);
|
70
|
4028 if (CHAR_OR_CHAR_INTP (code))
|
|
4029 {
|
|
4030 s2 = code;
|
|
4031 CHECK_CHAR_COERCE_INT (s2);
|
|
4032 }
|
0
|
4033 else return 0;
|
|
4034 }
|
|
4035
|
185
|
4036 return (XCHAR (s1) == XCHAR (s2) ||
|
|
4037 XCHAR (s1) + 1 == XCHAR (s2));
|
0
|
4038 }
|
|
4039
|
|
4040
|
|
4041 static Lisp_Object
|
|
4042 describe_map_parent_mapper (Lisp_Object keymap, void *arg)
|
|
4043 {
|
|
4044 /* This function can GC */
|
185
|
4045 struct describe_map_closure *describe_map_closure =
|
|
4046 (struct describe_map_closure *) arg;
|
0
|
4047 describe_map_closure->self = keymap;
|
|
4048 map_keymap (XKEYMAP (keymap)->table,
|
|
4049 0, /* don't sort: we'll do it later */
|
|
4050 describe_map_mapper, describe_map_closure);
|
173
|
4051 return Qnil;
|
0
|
4052 }
|
|
4053
|
|
4054
|
185
|
4055 /* Describe the contents of map MAP, assuming that this map itself is
|
|
4056 reached by the sequence of prefix keys KEYS (a string or vector).
|
|
4057 PARTIAL, SHADOW, NOMENU are as in `describe_map_tree' above. */
|
|
4058
|
0
|
4059 static void
|
|
4060 describe_map (Lisp_Object keymap, Lisp_Object elt_prefix,
|
|
4061 void (*elt_describer) (Lisp_Object),
|
185
|
4062 int partial,
|
0
|
4063 Lisp_Object shadow,
|
|
4064 int mice_only_p)
|
|
4065 {
|
|
4066 /* This function can GC */
|
|
4067 struct describe_map_closure describe_map_closure;
|
|
4068 Lisp_Object list = Qnil;
|
|
4069 struct buffer *buf = current_buffer;
|
|
4070 Emchar printable_min = (CHAR_OR_CHAR_INTP (buf->ctl_arrow)
|
|
4071 ? XCHAR_OR_CHAR_INT (buf->ctl_arrow)
|
|
4072 : ((EQ (buf->ctl_arrow, Qt)
|
|
4073 || EQ (buf->ctl_arrow, Qnil))
|
|
4074 ? 256 : 160));
|
|
4075 int elided = 0;
|
|
4076 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
|
4077
|
|
4078 keymap = get_keymap (keymap, 1, 1);
|
|
4079 describe_map_closure.partial = (partial ? Qsuppress_keymap : Qnil);
|
|
4080 describe_map_closure.shadow = shadow;
|
|
4081 describe_map_closure.list = &list;
|
|
4082 describe_map_closure.self_root = keymap;
|
|
4083 describe_map_closure.mice_only_p = mice_only_p;
|
|
4084
|
|
4085 GCPRO4 (keymap, elt_prefix, shadow, list);
|
|
4086
|
|
4087 traverse_keymaps (keymap, Qnil,
|
|
4088 describe_map_parent_mapper, &describe_map_closure);
|
|
4089
|
|
4090 if (!NILP (list))
|
|
4091 {
|
|
4092 list = list_sort (list, Qnil, describe_map_sort_predicate);
|
|
4093 buffer_insert_c_string (buf, "\n");
|
|
4094 while (!NILP (list))
|
|
4095 {
|
|
4096 Lisp_Object elt = XCAR (XCAR (list));
|
|
4097 Lisp_Object keysym = XCAR (elt);
|
|
4098 unsigned int modifiers = XINT (XCDR (elt));
|
|
4099
|
|
4100 if (!NILP (elt_prefix))
|
|
4101 buffer_insert_lisp_string (buf, elt_prefix);
|
|
4102
|
|
4103 if (modifiers & MOD_META) buffer_insert_c_string (buf, "M-");
|
|
4104 if (modifiers & MOD_CONTROL) buffer_insert_c_string (buf, "C-");
|
|
4105 if (modifiers & MOD_SUPER) buffer_insert_c_string (buf, "S-");
|
|
4106 if (modifiers & MOD_HYPER) buffer_insert_c_string (buf, "H-");
|
|
4107 if (modifiers & MOD_ALT) buffer_insert_c_string (buf, "Alt-");
|
|
4108 if (modifiers & MOD_SHIFT) buffer_insert_c_string (buf, "Sh-");
|
|
4109 if (SYMBOLP (keysym))
|
|
4110 {
|
|
4111 Lisp_Object code = Fget (keysym, Vcharacter_set_property, Qnil);
|
|
4112 Emchar c = (CHAR_OR_CHAR_INTP (code)
|
|
4113 ? XCHAR_OR_CHAR_INT (code) : -1);
|
|
4114 /* Calling Fsingle_key_description() would cons more */
|
|
4115 #if 0 /* This is bogus */
|
|
4116 if (EQ (keysym, QKlinefeed))
|
|
4117 buffer_insert_c_string (buf, "LFD");
|
|
4118 else if (EQ (keysym, QKtab))
|
|
4119 buffer_insert_c_string (buf, "TAB");
|
|
4120 else if (EQ (keysym, QKreturn))
|
|
4121 buffer_insert_c_string (buf, "RET");
|
|
4122 else if (EQ (keysym, QKescape))
|
|
4123 buffer_insert_c_string (buf, "ESC");
|
|
4124 else if (EQ (keysym, QKdelete))
|
|
4125 buffer_insert_c_string (buf, "DEL");
|
|
4126 else if (EQ (keysym, QKspace))
|
|
4127 buffer_insert_c_string (buf, "SPC");
|
|
4128 else if (EQ (keysym, QKbackspace))
|
|
4129 buffer_insert_c_string (buf, "BS");
|
185
|
4130 else
|
0
|
4131 #endif
|
|
4132 if (c >= printable_min)
|
|
4133 buffer_insert_emacs_char (buf, c);
|
|
4134 else buffer_insert1 (buf, Fsymbol_name (keysym));
|
|
4135 }
|
|
4136 else if (CHARP (keysym))
|
|
4137 buffer_insert_emacs_char (buf, XCHAR (keysym));
|
|
4138 else
|
|
4139 buffer_insert_c_string (buf, "---bad keysym---");
|
|
4140
|
|
4141 if (elided)
|
|
4142 elided = 0;
|
|
4143 else
|
|
4144 {
|
|
4145 int k = 0;
|
|
4146
|
|
4147 while (elide_next_two_p (list))
|
|
4148 {
|
|
4149 k++;
|
|
4150 list = XCDR (list);
|
|
4151 }
|
|
4152 if (k != 0)
|
|
4153 {
|
|
4154 if (k == 1)
|
|
4155 buffer_insert_c_string (buf, ", ");
|
|
4156 else
|
|
4157 buffer_insert_c_string (buf, " .. ");
|
|
4158 elided = 1;
|
|
4159 continue;
|
|
4160 }
|
|
4161 }
|
|
4162
|
|
4163 /* Print a description of the definition of this character. */
|
|
4164 (*elt_describer) (XCDR (XCAR (list)));
|
|
4165 list = XCDR (list);
|
|
4166 }
|
|
4167 }
|
|
4168 UNGCPRO;
|
|
4169 }
|
|
4170
|
|
4171
|
|
4172 void
|
|
4173 syms_of_keymap (void)
|
|
4174 {
|
|
4175 defsymbol (&Qminor_mode_map_alist, "minor-mode-map-alist");
|
|
4176
|
|
4177 defsymbol (&Qkeymapp, "keymapp");
|
|
4178
|
|
4179 defsymbol (&Qsuppress_keymap, "suppress-keymap");
|
|
4180
|
|
4181 defsymbol (&Qmodeline_map, "modeline-map");
|
|
4182 defsymbol (&Qtoolbar_map, "toolbar-map");
|
|
4183
|
20
|
4184 DEFSUBR (Fkeymap_parents);
|
|
4185 DEFSUBR (Fset_keymap_parents);
|
|
4186 DEFSUBR (Fkeymap_name);
|
|
4187 DEFSUBR (Fset_keymap_name);
|
|
4188 DEFSUBR (Fkeymap_prompt);
|
|
4189 DEFSUBR (Fset_keymap_prompt);
|
|
4190 DEFSUBR (Fkeymap_default_binding);
|
|
4191 DEFSUBR (Fset_keymap_default_binding);
|
|
4192
|
|
4193 DEFSUBR (Fkeymapp);
|
|
4194 DEFSUBR (Fmake_keymap);
|
|
4195 DEFSUBR (Fmake_sparse_keymap);
|
|
4196
|
|
4197 DEFSUBR (Fcopy_keymap);
|
|
4198 DEFSUBR (Fkeymap_fullness);
|
|
4199 DEFSUBR (Fmap_keymap);
|
|
4200 DEFSUBR (Fevent_matches_key_specifier_p);
|
|
4201 DEFSUBR (Fdefine_key);
|
|
4202 DEFSUBR (Flookup_key);
|
|
4203 DEFSUBR (Fkey_binding);
|
|
4204 DEFSUBR (Fuse_global_map);
|
|
4205 DEFSUBR (Fuse_local_map);
|
|
4206 DEFSUBR (Fcurrent_local_map);
|
|
4207 DEFSUBR (Fcurrent_global_map);
|
|
4208 DEFSUBR (Fcurrent_keymaps);
|
|
4209 DEFSUBR (Faccessible_keymaps);
|
|
4210 DEFSUBR (Fkey_description);
|
|
4211 DEFSUBR (Fsingle_key_description);
|
|
4212 DEFSUBR (Fwhere_is_internal);
|
|
4213 DEFSUBR (Fdescribe_bindings_internal);
|
|
4214
|
|
4215 DEFSUBR (Ftext_char_description);
|
0
|
4216
|
|
4217 defsymbol (&Qcontrol, "control");
|
|
4218 defsymbol (&Qctrl, "ctrl");
|
185
|
4219 defsymbol (&Qmeta, "meta");
|
|
4220 defsymbol (&Qsuper, "super");
|
|
4221 defsymbol (&Qhyper, "hyper");
|
0
|
4222 defsymbol (&Qalt, "alt");
|
|
4223 defsymbol (&Qshift, "shift");
|
|
4224 defsymbol (&Qbutton0, "button0");
|
|
4225 defsymbol (&Qbutton1, "button1");
|
|
4226 defsymbol (&Qbutton2, "button2");
|
|
4227 defsymbol (&Qbutton3, "button3");
|
|
4228 defsymbol (&Qbutton4, "button4");
|
|
4229 defsymbol (&Qbutton5, "button5");
|
|
4230 defsymbol (&Qbutton6, "button6");
|
|
4231 defsymbol (&Qbutton7, "button7");
|
|
4232 defsymbol (&Qbutton0up, "button0up");
|
|
4233 defsymbol (&Qbutton1up, "button1up");
|
|
4234 defsymbol (&Qbutton2up, "button2up");
|
|
4235 defsymbol (&Qbutton3up, "button3up");
|
|
4236 defsymbol (&Qbutton4up, "button4up");
|
|
4237 defsymbol (&Qbutton5up, "button5up");
|
|
4238 defsymbol (&Qbutton6up, "button6up");
|
|
4239 defsymbol (&Qbutton7up, "button7up");
|
98
|
4240 defsymbol (&Qmouse_1, "mouse-1");
|
|
4241 defsymbol (&Qmouse_2, "mouse-2");
|
|
4242 defsymbol (&Qmouse_3, "mouse-3");
|
|
4243 defsymbol (&Qdown_mouse_1, "down-mouse-1");
|
|
4244 defsymbol (&Qdown_mouse_2, "down-mouse-2");
|
|
4245 defsymbol (&Qdown_mouse_3, "down-mouse-3");
|
0
|
4246 defsymbol (&Qmenu_selection, "menu-selection");
|
|
4247 defsymbol (&QLFD, "LFD");
|
|
4248 defsymbol (&QTAB, "TAB");
|
|
4249 defsymbol (&QRET, "RET");
|
|
4250 defsymbol (&QESC, "ESC");
|
|
4251 defsymbol (&QDEL, "DEL");
|
|
4252 defsymbol (&QBS, "BS");
|
|
4253 }
|
|
4254
|
|
4255 void
|
|
4256 vars_of_keymap (void)
|
|
4257 {
|
|
4258 DEFVAR_LISP ("meta-prefix-char", &Vmeta_prefix_char /*
|
|
4259 Meta-prefix character.
|
|
4260 This character followed by some character `foo' turns into `Meta-foo'.
|
|
4261 This can be any form recognized as a single key specifier.
|
|
4262 To disable the meta-prefix-char, set it to a negative number.
|
|
4263 */ );
|
|
4264 Vmeta_prefix_char = make_char (033);
|
|
4265
|
|
4266 DEFVAR_LISP ("mouse-grabbed-buffer", &Vmouse_grabbed_buffer /*
|
|
4267 A buffer which should be consulted first for all mouse activity.
|
|
4268 When a mouse-click is processed, it will first be looked up in the
|
|
4269 local-map of this buffer, and then through the normal mechanism if there
|
|
4270 is no binding for that click. This buffer's value of `mode-motion-hook'
|
|
4271 will be consulted instead of the `mode-motion-hook' of the buffer of the
|
|
4272 window under the mouse. You should *bind* this, not set it.
|
|
4273 */ );
|
|
4274 Vmouse_grabbed_buffer = Qnil;
|
|
4275
|
|
4276 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map /*
|
|
4277 Keymap that overrides all other local keymaps.
|
|
4278 If this variable is non-nil, it is used as a keymap instead of the
|
|
4279 buffer's local map, and the minor mode keymaps and extent-local keymaps.
|
|
4280 You should *bind* this, not set it.
|
|
4281 */ );
|
|
4282 Voverriding_local_map = Qnil;
|
|
4283
|
|
4284 Fset (Qminor_mode_map_alist, Qnil);
|
|
4285
|
|
4286 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map /*
|
|
4287 Keymap of key translations that can override keymaps.
|
|
4288 This keymap works like `function-key-map', but comes after that,
|
|
4289 and applies even for keys that have ordinary bindings.
|
|
4290 */ );
|
|
4291
|
|
4292 DEFVAR_INT ("keymap-tick", &keymap_tick /*
|
|
4293 Incremented for each change to any keymap.
|
|
4294 */ );
|
|
4295 keymap_tick = 0;
|
|
4296
|
|
4297 staticpro (&Vcurrent_global_map);
|
|
4298
|
|
4299 Vsingle_space_string = make_pure_string ((CONST Bufbyte *) " ", 1, Qnil, 1);
|
|
4300 staticpro (&Vsingle_space_string);
|
|
4301 }
|
|
4302
|
|
4303 void
|
|
4304 complex_vars_of_keymap (void)
|
|
4305 {
|
|
4306 /* This function can GC */
|
|
4307 Lisp_Object ESC_prefix = intern ("ESC-prefix");
|
|
4308 Lisp_Object meta_disgustitute;
|
|
4309
|
|
4310 Vcurrent_global_map = Fmake_keymap (Qnil);
|
|
4311
|
|
4312 meta_disgustitute = Fmake_keymap (Qnil);
|
|
4313 Ffset (ESC_prefix, meta_disgustitute);
|
|
4314 /* no need to protect meta_disgustitute, though */
|
|
4315 keymap_store_internal (MAKE_MODIFIER_HASH_KEY (MOD_META),
|
|
4316 XKEYMAP (Vcurrent_global_map),
|
|
4317 meta_disgustitute);
|
|
4318 XKEYMAP (Vcurrent_global_map)->sub_maps_cache = Qt;
|
|
4319
|
|
4320 Vkey_translation_map = Fmake_sparse_keymap (intern ("key-translation-map"));
|
|
4321 }
|