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