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] = '-';
|
76
|
1330 *keysym = Fintern_soft(make_string((Bufbyte *)temp,
|
|
1331 strlen(temp)),
|
|
1332 Qnil);
|
74
|
1333 } else if (EQ (*keysym, QLFD))
|
70
|
1334 *keysym = QKlinefeed;
|
|
1335 else if (EQ (*keysym, QTAB))
|
|
1336 *keysym = QKtab;
|
|
1337 else if (EQ (*keysym, QRET))
|
|
1338 *keysym = QKreturn;
|
|
1339 else if (EQ (*keysym, QESC))
|
|
1340 *keysym = QKescape;
|
|
1341 else if (EQ (*keysym, QDEL))
|
|
1342 *keysym = QKdelete;
|
|
1343 else if (EQ (*keysym, QBS))
|
|
1344 *keysym = QKbackspace;
|
0
|
1345 }
|
|
1346 }
|
|
1347
|
70
|
1348
|
0
|
1349 /* Given any kind of key-specifier, return a keysym and modifier mask.
|
70
|
1350 Proper canonicalization is performed:
|
|
1351
|
|
1352 -- integers are converted into the equivalent characters.
|
|
1353 -- one-character strings are converted into the equivalent characters.
|
0
|
1354 */
|
70
|
1355
|
0
|
1356 static void
|
|
1357 define_key_parser (Lisp_Object spec, struct key_data *returned_value)
|
|
1358 {
|
70
|
1359 if (CHAR_OR_CHAR_INTP (spec))
|
0
|
1360 {
|
|
1361 struct Lisp_Event event;
|
|
1362 event.event_type = empty_event;
|
70
|
1363 character_to_event (XCHAR_OR_CHAR_INT (spec), &event,
|
0
|
1364 XCONSOLE (Vselected_console), 0);
|
|
1365 returned_value->keysym = event.event.key.keysym;
|
|
1366 returned_value->modifiers = event.event.key.modifiers;
|
|
1367 }
|
|
1368 else if (EVENTP (spec))
|
|
1369 {
|
|
1370 switch (XEVENT (spec)->event_type)
|
|
1371 {
|
|
1372 case key_press_event:
|
|
1373 {
|
|
1374 returned_value->keysym = XEVENT (spec)->event.key.keysym;
|
|
1375 returned_value->modifiers = XEVENT (spec)->event.key.modifiers;
|
|
1376 break;
|
|
1377 }
|
|
1378 case button_press_event:
|
|
1379 case button_release_event:
|
|
1380 {
|
|
1381 int down = (XEVENT (spec)->event_type == button_press_event);
|
|
1382 switch (XEVENT (spec)->event.button.button)
|
|
1383 {
|
|
1384 case 1:
|
|
1385 returned_value->keysym = (down ? Qbutton1 : Qbutton1up); break;
|
|
1386 case 2:
|
|
1387 returned_value->keysym = (down ? Qbutton2 : Qbutton2up); break;
|
|
1388 case 3:
|
|
1389 returned_value->keysym = (down ? Qbutton3 : Qbutton3up); break;
|
|
1390 case 4:
|
|
1391 returned_value->keysym = (down ? Qbutton4 : Qbutton4up); break;
|
|
1392 case 5:
|
|
1393 returned_value->keysym = (down ? Qbutton5 : Qbutton5up); break;
|
|
1394 case 6:
|
|
1395 returned_value->keysym = (down ? Qbutton6 : Qbutton6up); break;
|
|
1396 case 7:
|
|
1397 returned_value->keysym = (down ? Qbutton7 : Qbutton7up); break;
|
|
1398 default:
|
|
1399 returned_value->keysym =(down ? Qbutton0 : Qbutton0up); break;
|
|
1400 }
|
|
1401 returned_value->modifiers = XEVENT (spec)->event.button.modifiers;
|
|
1402 break;
|
|
1403 }
|
|
1404 default:
|
|
1405 signal_error (Qwrong_type_argument,
|
|
1406 list2 (build_translated_string
|
|
1407 ("unable to bind this type of event"),
|
|
1408 spec));
|
|
1409 }
|
|
1410 }
|
|
1411 else if (SYMBOLP (spec))
|
|
1412 {
|
|
1413 /* Be nice, allow = to mean (=) */
|
|
1414 if (bucky_sym_to_bucky_bit (spec) != 0)
|
|
1415 signal_simple_error ("Key is a modifier name", spec);
|
70
|
1416 define_key_check_and_coerce_keysym (spec, &spec, 0);
|
0
|
1417 returned_value->keysym = spec;
|
|
1418 returned_value->modifiers = 0;
|
|
1419 }
|
|
1420 else if (CONSP (spec))
|
|
1421 {
|
|
1422 unsigned int modifiers = 0;
|
|
1423 Lisp_Object keysym = Qnil;
|
|
1424 Lisp_Object rest = spec;
|
|
1425
|
|
1426 /* First, parse out the leading modifier symbols. */
|
|
1427 while (CONSP (rest))
|
|
1428 {
|
|
1429 unsigned int modifier;
|
|
1430
|
|
1431 keysym = XCAR (rest);
|
|
1432 modifier = bucky_sym_to_bucky_bit (keysym);
|
|
1433 modifiers |= modifier;
|
|
1434 if (!NILP (XCDR (rest)))
|
|
1435 {
|
|
1436 if (! modifier)
|
|
1437 signal_simple_error ("unknown modifier", keysym);
|
|
1438 }
|
|
1439 else
|
|
1440 {
|
|
1441 if (modifier)
|
|
1442 signal_simple_error ("nothing but modifiers here",
|
|
1443 spec);
|
|
1444 }
|
|
1445 rest = XCDR (rest);
|
|
1446 QUIT;
|
|
1447 }
|
|
1448 if (!NILP (rest))
|
|
1449 signal_simple_error ("dotted list", spec);
|
|
1450
|
70
|
1451 define_key_check_and_coerce_keysym (spec, &keysym, modifiers);
|
0
|
1452 returned_value->keysym = keysym;
|
|
1453 returned_value->modifiers = modifiers;
|
|
1454 }
|
|
1455 else
|
|
1456 {
|
|
1457 signal_simple_error ("unknown key-sequence specifier",
|
|
1458 spec);
|
|
1459 }
|
|
1460 }
|
|
1461
|
|
1462 /* Used by character-to-event */
|
|
1463 void
|
|
1464 key_desc_list_to_event (Lisp_Object list, Lisp_Object event,
|
|
1465 int allow_menu_events)
|
|
1466 {
|
|
1467 struct key_data raw_key;
|
|
1468
|
|
1469 if (allow_menu_events &&
|
|
1470 CONSP (list) &&
|
|
1471 /* #### where the hell does this come from? */
|
|
1472 EQ (XCAR (list), Qmenu_selection))
|
|
1473 {
|
|
1474 Lisp_Object fn, arg;
|
|
1475 if (! NILP (Fcdr (Fcdr (list))))
|
|
1476 signal_simple_error ("invalid menu event desc", list);
|
|
1477 arg = Fcar (Fcdr (list));
|
|
1478 if (SYMBOLP (arg))
|
|
1479 fn = Qcall_interactively;
|
|
1480 else
|
|
1481 fn = Qeval;
|
|
1482 XSETFRAME (XEVENT (event)->channel, selected_frame ());
|
|
1483 XEVENT (event)->event_type = misc_user_event;
|
|
1484 XEVENT (event)->event.eval.function = fn;
|
|
1485 XEVENT (event)->event.eval.object = arg;
|
|
1486 return;
|
|
1487 }
|
|
1488
|
|
1489 define_key_parser (list, &raw_key);
|
|
1490
|
|
1491 if (EQ (raw_key.keysym, Qbutton0) || EQ (raw_key.keysym, Qbutton0up) ||
|
|
1492 EQ (raw_key.keysym, Qbutton1) || EQ (raw_key.keysym, Qbutton1up) ||
|
|
1493 EQ (raw_key.keysym, Qbutton2) || EQ (raw_key.keysym, Qbutton2up) ||
|
|
1494 EQ (raw_key.keysym, Qbutton3) || EQ (raw_key.keysym, Qbutton3up) ||
|
|
1495 EQ (raw_key.keysym, Qbutton4) || EQ (raw_key.keysym, Qbutton4up) ||
|
|
1496 EQ (raw_key.keysym, Qbutton5) || EQ (raw_key.keysym, Qbutton5up) ||
|
|
1497 EQ (raw_key.keysym, Qbutton6) || EQ (raw_key.keysym, Qbutton6up) ||
|
|
1498 EQ (raw_key.keysym, Qbutton7) || EQ (raw_key.keysym, Qbutton7up))
|
|
1499 error ("Mouse-clicks can't appear in saved keyboard macros.");
|
|
1500
|
|
1501 XEVENT (event)->channel = Vselected_console;
|
|
1502 XEVENT (event)->event_type = key_press_event;
|
|
1503 XEVENT (event)->event.key.keysym = raw_key.keysym;
|
|
1504 XEVENT (event)->event.key.modifiers = raw_key.modifiers;
|
|
1505 }
|
|
1506
|
|
1507
|
|
1508 int
|
|
1509 event_matches_key_specifier_p (struct Lisp_Event *event,
|
|
1510 Lisp_Object key_specifier)
|
|
1511 {
|
|
1512 Lisp_Object event2;
|
|
1513 int retval;
|
|
1514 struct gcpro gcpro1;
|
|
1515
|
|
1516 if (event->event_type != key_press_event || NILP (key_specifier) ||
|
70
|
1517 (INTP (key_specifier) && !CHAR_INTP (key_specifier)))
|
0
|
1518 return 0;
|
|
1519
|
|
1520 /* if the specifier is an integer such as 27, then it should match
|
|
1521 both of the events 'escape' and 'control ['. Calling
|
|
1522 Fcharacter_to_event() will only match 'escape'. */
|
70
|
1523 if (CHAR_OR_CHAR_INTP (key_specifier))
|
|
1524 return (XCHAR_OR_CHAR_INT (key_specifier)
|
|
1525 == event_to_character (event, 0, 0, 0));
|
0
|
1526
|
|
1527 /* Otherwise, we cannot call event_to_character() because we may
|
|
1528 be dealing with non-ASCII keystrokes. In any case, if I ask
|
|
1529 for 'control [' then I should get exactly that, and not
|
|
1530 'escape'.
|
|
1531
|
|
1532 However, we have to behave differently on TTY's, where 'control ['
|
|
1533 is silently converted into 'escape' by the keyboard driver.
|
|
1534 In this case, ASCII is the only thing we know about, so we have
|
|
1535 to compare the ASCII values. */
|
|
1536
|
|
1537 GCPRO1 (event2);
|
|
1538 event2 = Fmake_event ();
|
|
1539 Fcharacter_to_event (key_specifier, event2, Qnil, Qnil);
|
|
1540 if (XEVENT (event2)->event_type != key_press_event)
|
|
1541 retval = 0;
|
|
1542 else if (CONSOLE_TTY_P (XCONSOLE (EVENT_CHANNEL (event))))
|
|
1543 {
|
|
1544 int ch1, ch2;
|
|
1545
|
|
1546 ch1 = event_to_character (event, 0, 0, 0);
|
|
1547 ch2 = event_to_character (XEVENT (event2), 0, 0, 0);
|
|
1548 retval = (ch1 >= 0 && ch2 >= 0 && ch1 == ch2);
|
|
1549 }
|
|
1550 else if (EQ (event->event.key.keysym, XEVENT (event2)->event.key.keysym) &&
|
|
1551 event->event.key.modifiers == XEVENT (event2)->event.key.modifiers)
|
|
1552 retval = 1;
|
|
1553 else
|
|
1554 retval = 0;
|
|
1555 Fdeallocate_event (event2);
|
|
1556 UNGCPRO;
|
|
1557 return retval;
|
|
1558 }
|
|
1559
|
|
1560 static int
|
|
1561 meta_prefix_char_p (CONST struct key_data *key)
|
|
1562 {
|
|
1563 struct Lisp_Event event;
|
|
1564
|
|
1565 event.event_type = key_press_event;
|
|
1566 event.channel = Vselected_console;
|
|
1567 event.event.key.keysym = key->keysym;
|
|
1568 event.event.key.modifiers = key->modifiers;
|
|
1569 return event_matches_key_specifier_p (&event, Vmeta_prefix_char);
|
|
1570 }
|
|
1571
|
20
|
1572 DEFUN ("event-matches-key-specifier-p", Fevent_matches_key_specifier_p, 2, 2, 0, /*
|
0
|
1573 Return non-nil if EVENT matches KEY-SPECIFIER.
|
|
1574 This can be useful, e.g., to determine if the user pressed `help-char' or
|
|
1575 `quit-char'.
|
20
|
1576 */
|
|
1577 (event, key_specifier))
|
0
|
1578 {
|
|
1579 CHECK_LIVE_EVENT (event);
|
|
1580 return (event_matches_key_specifier_p (XEVENT (event), key_specifier)
|
|
1581 ? Qt : Qnil);
|
|
1582 }
|
|
1583
|
|
1584 /* ASCII grunge.
|
|
1585 Given a keysym, return another keysym/modifier pair which could be
|
|
1586 considered the same key in an ASCII world. Backspace returns ^H, for
|
|
1587 example.
|
|
1588 */
|
|
1589 static void
|
|
1590 define_key_alternate_name (struct key_data *key,
|
|
1591 struct key_data *returned_value)
|
|
1592 {
|
|
1593 Lisp_Object keysym = key->keysym;
|
|
1594 unsigned int modifiers = key->modifiers;
|
|
1595 unsigned int modifiers_sans_control = (modifiers & (~MOD_CONTROL));
|
|
1596 unsigned int modifiers_sans_meta = (modifiers & (~MOD_META));
|
|
1597 returned_value->keysym = Qnil; /* By default, no "alternate" key */
|
|
1598 returned_value->modifiers = 0;
|
|
1599 #define MACROLET(k,m) do { returned_value->keysym = (k); \
|
|
1600 returned_value->modifiers = (m); \
|
|
1601 RETURN__; } while (0)
|
|
1602 if (modifiers_sans_meta == MOD_CONTROL)
|
|
1603 {
|
|
1604 if EQ (keysym, QKspace)
|
|
1605 MACROLET (make_char ('@'), modifiers);
|
|
1606 else if (!CHARP (keysym))
|
|
1607 return;
|
|
1608 else switch (XCHAR (keysym))
|
|
1609 {
|
|
1610 case '@': /* c-@ => c-space */
|
|
1611 MACROLET (QKspace, modifiers);
|
|
1612 case 'h': /* c-h => backspace */
|
|
1613 MACROLET (QKbackspace, modifiers_sans_control);
|
|
1614 case 'i': /* c-i => tab */
|
|
1615 MACROLET (QKtab, modifiers_sans_control);
|
|
1616 case 'j': /* c-j => linefeed */
|
|
1617 MACROLET (QKlinefeed, modifiers_sans_control);
|
|
1618 case 'm': /* c-m => return */
|
|
1619 MACROLET (QKreturn, modifiers_sans_control);
|
|
1620 case '[': /* c-[ => escape */
|
|
1621 MACROLET (QKescape, modifiers_sans_control);
|
|
1622 default:
|
|
1623 return;
|
|
1624 }
|
|
1625 }
|
|
1626 else if (modifiers_sans_meta != 0)
|
|
1627 return;
|
|
1628 else if (EQ (keysym, QKbackspace)) /* backspace => c-h */
|
|
1629 MACROLET (make_char ('h'), (modifiers | MOD_CONTROL));
|
|
1630 else if (EQ (keysym, QKtab)) /* tab => c-i */
|
|
1631 MACROLET (make_char ('i'), (modifiers | MOD_CONTROL));
|
|
1632 else if (EQ (keysym, QKlinefeed)) /* linefeed => c-j */
|
|
1633 MACROLET (make_char ('j'), (modifiers | MOD_CONTROL));
|
|
1634 else if (EQ (keysym, QKreturn)) /* return => c-m */
|
|
1635 MACROLET (make_char ('m'), (modifiers | MOD_CONTROL));
|
|
1636 else if (EQ (keysym, QKescape)) /* escape => c-[ */
|
|
1637 MACROLET (make_char ('['), (modifiers | MOD_CONTROL));
|
|
1638 else
|
|
1639 return;
|
|
1640 #undef MACROLET
|
|
1641 }
|
|
1642
|
|
1643
|
|
1644 static void
|
|
1645 ensure_meta_prefix_char_keymapp (Lisp_Object keys, int indx,
|
|
1646 Lisp_Object keymap)
|
|
1647 {
|
|
1648 /* This function can GC */
|
|
1649 char buf [255];
|
|
1650 Lisp_Object new_keys;
|
|
1651 int i;
|
|
1652 Lisp_Object mpc_binding;
|
|
1653 struct key_data meta_key;
|
|
1654
|
|
1655 if (NILP (Vmeta_prefix_char) ||
|
70
|
1656 (INTP (Vmeta_prefix_char) && !CHAR_INTP (Vmeta_prefix_char)))
|
0
|
1657 return;
|
|
1658
|
|
1659 define_key_parser (Vmeta_prefix_char, &meta_key);
|
|
1660 mpc_binding = keymap_lookup_1 (keymap, &meta_key, 0);
|
|
1661 if (NILP (mpc_binding) || !NILP (Fkeymapp (mpc_binding)))
|
|
1662 return;
|
|
1663
|
|
1664 if (indx == 0)
|
|
1665 new_keys = keys;
|
|
1666 else if (STRINGP (keys))
|
|
1667 new_keys = Fsubstring (keys, Qzero, make_int (indx));
|
|
1668 else if (VECTORP (keys))
|
|
1669 {
|
|
1670 new_keys = make_vector (indx, Qnil);
|
|
1671 for (i = 0; i < indx; i++)
|
|
1672 vector_data (XVECTOR (new_keys)) [i] =
|
|
1673 vector_data (XVECTOR (keys)) [i];
|
|
1674 }
|
|
1675 else
|
|
1676 abort ();
|
|
1677 if (EQ (keys, new_keys))
|
|
1678 sprintf (buf, GETTEXT ("can't bind %s: %s has a non-keymap binding"),
|
70
|
1679 (char *) XSTRING_DATA (Fkey_description (keys)),
|
|
1680 (char *) XSTRING_DATA (Fsingle_key_description
|
|
1681 (Vmeta_prefix_char)));
|
0
|
1682 else
|
|
1683 sprintf (buf, GETTEXT ("can't bind %s: %s %s has a non-keymap binding"),
|
70
|
1684 (char *) XSTRING_DATA (Fkey_description (keys)),
|
|
1685 (char *) XSTRING_DATA (Fkey_description (new_keys)),
|
|
1686 (char *) XSTRING_DATA (Fsingle_key_description
|
|
1687 (Vmeta_prefix_char)));
|
0
|
1688 signal_simple_error (buf, mpc_binding);
|
|
1689 }
|
|
1690
|
20
|
1691 DEFUN ("define-key", Fdefine_key, 3, 3, 0, /*
|
0
|
1692 Define key sequence KEYS, in KEYMAP, as DEF.
|
|
1693 KEYMAP is a keymap object.
|
|
1694 KEYS is the sequence of keystrokes to bind, described below.
|
|
1695 DEF is anything that can be a key's definition:
|
|
1696 nil (means key is undefined in this keymap);
|
|
1697 a command (a Lisp function suitable for interactive calling);
|
|
1698 a string or key sequence vector (treated as a keyboard macro);
|
|
1699 a keymap (to define a prefix key);
|
|
1700 a symbol; when the key is looked up, the symbol will stand for its
|
|
1701 function definition, that should at that time be one of the above,
|
|
1702 or another symbol whose function definition is used, and so on.
|
|
1703 a cons (STRING . DEFN), meaning that DEFN is the definition
|
|
1704 (DEFN should be a valid definition in its own right);
|
|
1705 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.
|
|
1706
|
|
1707 Contrary to popular belief, the world is not ASCII. When running under a
|
|
1708 window manager, XEmacs can tell the difference between, for example, the
|
|
1709 keystrokes control-h, control-shift-h, and backspace. You can, in fact,
|
|
1710 bind different commands to each of these.
|
|
1711
|
|
1712 A `key sequence' is a set of keystrokes. A `keystroke' is a keysym and some
|
|
1713 set of modifiers (such as control and meta). A `keysym' is what is printed
|
|
1714 on the keys on your keyboard.
|
|
1715
|
|
1716 A keysym may be represented by a symbol, or (if and only if it is equivalent
|
|
1717 to an ASCII character in the range 32 - 255) by a character or its equivalent
|
|
1718 ASCII code. The `A' key may be represented by the symbol `A', the character
|
|
1719 `?A', or by the number 65. The `break' key may be represented only by the
|
|
1720 symbol `break'.
|
|
1721
|
|
1722 A keystroke may be represented by a list: the last element of the list
|
|
1723 is the key (a symbol, character, or number, as above) and the
|
|
1724 preceding elements are the symbolic names of modifier keys (control,
|
|
1725 meta, super, hyper, alt, and shift). Thus, the sequence control-b is
|
|
1726 represented by the forms `(control b)', `(control ?b)', and `(control
|
|
1727 98)'. A keystroke may also be represented by an event object, as
|
|
1728 returned by the `next-command-event' and `read-key-sequence'
|
|
1729 functions.
|
|
1730
|
|
1731 Note that in this context, the keystroke `control-b' is *not* represented
|
|
1732 by the number 2 (the ASCII code for ^B) or the character `?\^B'. See below.
|
|
1733
|
|
1734 The `shift' modifier is somewhat of a special case. You should not (and
|
|
1735 cannot) use `(meta shift a)' to mean `(meta A)', since for characters that
|
|
1736 have ASCII equivalents, the state of the shift key is implicit in the
|
|
1737 keysym (a vs. A). You also cannot say `(shift =)' to mean `+', as that
|
|
1738 sort of thing varies from keyboard to keyboard. The shift modifier is for
|
|
1739 use only with characters that do not have a second keysym on the same key,
|
|
1740 such as `backspace' and `tab'.
|
|
1741
|
|
1742 A key sequence is a vector of keystrokes. As a degenerate case, elements
|
|
1743 of this vector may also be keysyms if they have no modifiers. That is,
|
|
1744 the `A' keystroke is represented by all of these forms:
|
|
1745 A ?A 65 (A) (?A) (65)
|
|
1746 [A] [?A] [65] [(A)] [(?A)] [(65)]
|
|
1747
|
|
1748 the `control-a' keystroke is represented by these forms:
|
|
1749 (control A) (control ?A) (control 65)
|
|
1750 [(control A)] [(control ?A)] [(control 65)]
|
|
1751 the key sequence `control-c control-a' is represented by these forms:
|
|
1752 [(control c) (control a)] [(control ?c) (control ?a)]
|
|
1753 [(control 99) (control 65)] etc.
|
|
1754
|
|
1755 Mouse button clicks work just like keypresses: (control button1) means
|
|
1756 pressing the left mouse button while holding down the control key.
|
|
1757 \[(control c) (shift button3)] means control-c, hold shift, click right.
|
|
1758
|
|
1759 Commands may be bound to the mouse-button up-stroke rather than the down-
|
|
1760 stroke as well. `button1' means the down-stroke, and `button1up' means the
|
|
1761 up-stroke. Different commands may be bound to the up and down strokes,
|
|
1762 though that is probably not what you want, so be careful.
|
|
1763
|
|
1764 For backward compatibility, a key sequence may also be represented by a
|
|
1765 string. In this case, it represents the key sequence(s) that would
|
|
1766 produce that sequence of ASCII characters in a purely ASCII world. For
|
|
1767 example, a string containing the ASCII backspace character, \"\\^H\", would
|
|
1768 represent two key sequences: `(control h)' and `backspace'. Binding a
|
|
1769 command to this will actually bind both of those key sequences. Likewise
|
|
1770 for the following pairs:
|
|
1771
|
|
1772 control h backspace
|
|
1773 control i tab
|
|
1774 control m return
|
|
1775 control j linefeed
|
|
1776 control [ escape
|
|
1777 control @ control space
|
|
1778
|
|
1779 After binding a command to two key sequences with a form like
|
|
1780
|
|
1781 (define-key global-map \"\\^X\\^I\" \'command-1)
|
|
1782
|
|
1783 it is possible to redefine only one of those sequences like so:
|
|
1784
|
|
1785 (define-key global-map [(control x) (control i)] \'command-2)
|
|
1786 (define-key global-map [(control x) tab] \'command-3)
|
|
1787
|
|
1788 Of course, all of this applies only when running under a window system. If
|
|
1789 you're talking to XEmacs through a TTY connection, you don't get any of
|
|
1790 these features.
|
20
|
1791 */
|
|
1792 (keymap, keys, def))
|
0
|
1793 {
|
|
1794 /* This function can GC */
|
|
1795 int idx;
|
|
1796 int metized = 0;
|
|
1797 int size;
|
|
1798 int ascii_hack;
|
|
1799 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
1800
|
|
1801 if (VECTORP (keys))
|
|
1802 size = vector_length (XVECTOR (keys));
|
|
1803 else if (STRINGP (keys))
|
70
|
1804 size = string_char_length (XSTRING (keys));
|
|
1805 else if (CHAR_OR_CHAR_INTP (keys) || SYMBOLP (keys) || CONSP (keys))
|
0
|
1806 {
|
|
1807 if (!CONSP (keys)) keys = list1 (keys);
|
|
1808 size = 1;
|
|
1809 keys = make_vector (1, keys); /* this is kinda sleazy. */
|
|
1810 }
|
|
1811 else
|
|
1812 {
|
|
1813 keys = wrong_type_argument (Qsequencep, keys);
|
|
1814 size = XINT (Flength (keys));
|
|
1815 }
|
|
1816 if (size == 0)
|
|
1817 return (Qnil);
|
|
1818
|
|
1819 GCPRO3 (keymap, keys, def);
|
|
1820
|
|
1821 /* ASCII grunge.
|
|
1822 When the user defines a key which, in a strictly ASCII world, would be
|
|
1823 produced by two different keys (^J and linefeed, or ^H and backspace,
|
|
1824 for example) then the binding will be made for both keysyms.
|
|
1825
|
|
1826 This is done if the user binds a command to a string, as in
|
|
1827 (define-key map "\^H" 'something), but not when using one of the new
|
|
1828 syntaxes, like (define-key map '(control h) 'something).
|
|
1829 */
|
|
1830 ascii_hack = (STRINGP (keys));
|
|
1831
|
|
1832 keymap = get_keymap (keymap, 1, 1);
|
|
1833
|
|
1834 idx = 0;
|
|
1835 while (1)
|
|
1836 {
|
|
1837 Lisp_Object c;
|
|
1838 struct key_data raw_key1;
|
|
1839 struct key_data raw_key2;
|
|
1840
|
|
1841 if (STRINGP (keys))
|
|
1842 c = make_char (string_char (XSTRING (keys), idx));
|
|
1843 else
|
70
|
1844 c = vector_data (XVECTOR (keys)) [idx];
|
0
|
1845
|
|
1846 define_key_parser (c, &raw_key1);
|
|
1847
|
|
1848 if (!metized && ascii_hack && meta_prefix_char_p (&raw_key1))
|
|
1849 {
|
|
1850 if (idx == (size - 1))
|
|
1851 {
|
|
1852 /* This is a hack to prevent a binding for the meta-prefix-char
|
|
1853 from being made in a map which already has a non-empty "meta"
|
|
1854 submap. That is, we can't let both "escape" and "meta" have
|
|
1855 a binding in the same keymap. This implies that the idiom
|
|
1856 (define-key my-map "\e" my-escape-map)
|
|
1857 (define-key my-escape-map "a" 'my-command)
|
|
1858 no longer works. That's ok. Instead the luser should do
|
|
1859 (define-key my-map "\ea" 'my-command)
|
|
1860 or, more correctly
|
|
1861 (define-key my-map "\M-a" 'my-command)
|
|
1862 and then perhaps
|
|
1863 (defvar my-escape-map (lookup-key my-map "\e"))
|
|
1864 if the luser really wants the map in a variable.
|
|
1865 */
|
|
1866 Lisp_Object mmap;
|
|
1867 struct gcpro ngcpro1;
|
|
1868
|
|
1869 NGCPRO1 (c);
|
|
1870 mmap = Fgethash (MAKE_MODIFIER_HASH_KEY (MOD_META),
|
|
1871 XKEYMAP (keymap)->table, Qnil);
|
|
1872 if (!NILP (mmap)
|
|
1873 && keymap_fullness (mmap) != 0)
|
|
1874 {
|
|
1875 Lisp_Object desc
|
|
1876 = Fsingle_key_description (Vmeta_prefix_char);
|
|
1877 signal_simple_error_2
|
|
1878 ("Map contains meta-bindings, can't bind", desc, keymap);
|
|
1879 }
|
|
1880 NUNGCPRO;
|
|
1881 }
|
|
1882 else
|
|
1883 {
|
|
1884 metized = 1;
|
|
1885 idx++;
|
|
1886 continue;
|
|
1887 }
|
|
1888 }
|
|
1889
|
|
1890 if (ascii_hack)
|
|
1891 define_key_alternate_name (&raw_key1, &raw_key2);
|
|
1892 else
|
|
1893 {
|
|
1894 raw_key2.keysym = Qnil;
|
|
1895 raw_key2.modifiers = 0;
|
|
1896 }
|
|
1897
|
|
1898 if (metized)
|
|
1899 {
|
|
1900 raw_key1.modifiers |= MOD_META;
|
|
1901 raw_key2.modifiers |= MOD_META;
|
|
1902 metized = 0;
|
|
1903 }
|
|
1904
|
|
1905 /* This crap is to make sure that someone doesn't bind something like
|
|
1906 "C-x M-a" while "C-x ESC" has a non-keymap binding. */
|
|
1907 if (raw_key1.modifiers & MOD_META)
|
|
1908 ensure_meta_prefix_char_keymapp (keys, idx, keymap);
|
|
1909
|
|
1910 if (++idx == size)
|
|
1911 {
|
|
1912 keymap_store (keymap, &raw_key1, def);
|
|
1913 if (ascii_hack && !NILP (raw_key2.keysym))
|
|
1914 keymap_store (keymap, &raw_key2, def);
|
|
1915 UNGCPRO;
|
|
1916 return def;
|
|
1917 }
|
|
1918
|
|
1919 {
|
|
1920 Lisp_Object cmd;
|
|
1921 struct gcpro ngcpro1;
|
|
1922 NGCPRO1 (c);
|
|
1923
|
|
1924 cmd = keymap_lookup_1 (keymap, &raw_key1, 0);
|
|
1925 if (NILP (cmd))
|
|
1926 {
|
|
1927 cmd = Fmake_sparse_keymap (Qnil);
|
|
1928 XKEYMAP (cmd)->name /* for debugging */
|
|
1929 = list2 (make_key_description (&raw_key1, 1), keymap);
|
|
1930 keymap_store (keymap, &raw_key1, cmd);
|
|
1931 }
|
|
1932 if (NILP (Fkeymapp (cmd)))
|
|
1933 signal_simple_error_2 ("invalid prefix keys in sequence",
|
|
1934 c, keys);
|
|
1935
|
|
1936 if (ascii_hack && !NILP (raw_key2.keysym) &&
|
|
1937 NILP (keymap_lookup_1 (keymap, &raw_key2, 0)))
|
|
1938 keymap_store (keymap, &raw_key2, cmd);
|
|
1939
|
|
1940 keymap = get_keymap (cmd, 1, 1);
|
|
1941 NUNGCPRO;
|
|
1942 }
|
|
1943 }
|
|
1944 }
|
|
1945
|
|
1946
|
|
1947 /************************************************************************/
|
|
1948 /* Looking up keys in keymaps */
|
|
1949 /************************************************************************/
|
|
1950
|
|
1951 /* We need a very fast (i.e., non-consing) version of lookup-key in order
|
2
|
1952 to make where-is-internal really fly. */
|
0
|
1953
|
|
1954 struct raw_lookup_key_mapper_closure
|
2
|
1955 {
|
|
1956 int remaining;
|
|
1957 CONST struct key_data *raw_keys;
|
|
1958 int raw_keys_count;
|
|
1959 int keys_so_far;
|
|
1960 int accept_default;
|
|
1961 };
|
0
|
1962
|
|
1963 static Lisp_Object raw_lookup_key_mapper (Lisp_Object k, void *);
|
|
1964
|
|
1965 /* Caller should gc-protect args (keymaps may autoload) */
|
|
1966 static Lisp_Object
|
|
1967 raw_lookup_key (Lisp_Object keymap,
|
|
1968 CONST struct key_data *raw_keys, int raw_keys_count,
|
|
1969 int keys_so_far, int accept_default)
|
|
1970 {
|
|
1971 /* This function can GC */
|
|
1972 struct raw_lookup_key_mapper_closure c;
|
|
1973 c.remaining = raw_keys_count - 1;
|
|
1974 c.raw_keys = raw_keys;
|
|
1975 c.raw_keys_count = raw_keys_count;
|
|
1976 c.keys_so_far = keys_so_far;
|
|
1977 c.accept_default = accept_default;
|
|
1978
|
|
1979 return (traverse_keymaps (keymap, Qnil, raw_lookup_key_mapper,
|
|
1980 &c));
|
|
1981 }
|
|
1982
|
|
1983 static Lisp_Object
|
|
1984 raw_lookup_key_mapper (Lisp_Object k, void *arg)
|
|
1985 {
|
|
1986 /* This function can GC */
|
|
1987 struct raw_lookup_key_mapper_closure *c = arg;
|
|
1988 int accept_default = c->accept_default;
|
|
1989 int remaining = c->remaining;
|
|
1990 int keys_so_far = c->keys_so_far;
|
|
1991 CONST struct key_data *raw_keys = c->raw_keys;
|
|
1992 Lisp_Object cmd;
|
|
1993
|
|
1994 if (! meta_prefix_char_p (&(raw_keys[0])))
|
|
1995 {
|
|
1996 /* Normal case: every case except the meta-hack (see below). */
|
|
1997 cmd = keymap_lookup_1 (k, &(raw_keys[0]), accept_default);
|
|
1998
|
|
1999 if (remaining == 0)
|
|
2000 /* Return whatever we found if we're out of keys */
|
|
2001 ;
|
|
2002 else if (NILP (cmd))
|
|
2003 /* Found nothing (though perhaps parent map may have binding) */
|
|
2004 ;
|
|
2005 else if (NILP (Fkeymapp (cmd)))
|
|
2006 /* Didn't find a keymap, and we have more keys.
|
|
2007 * Return a fixnum to indicate that keys were too long.
|
|
2008 */
|
|
2009 cmd = make_int (keys_so_far + 1);
|
|
2010 else
|
|
2011 cmd = raw_lookup_key (cmd, raw_keys + 1, remaining,
|
|
2012 keys_so_far + 1, accept_default);
|
|
2013 }
|
|
2014 else
|
|
2015 {
|
|
2016 /* This is a hack so that looking up a key-sequence whose last
|
|
2017 * element is the meta-prefix-char will return the keymap that
|
|
2018 * the "meta" keys are stored in, if there is no binding for
|
|
2019 * the meta-prefix-char (and if this map has a "meta" submap).
|
|
2020 * If this map doesnt have a "meta" submap, then the
|
|
2021 * meta-prefix-char is looked up just like any other key.
|
|
2022 */
|
|
2023 if (remaining == 0)
|
|
2024 {
|
|
2025 /* First look for the prefix-char directly */
|
|
2026 cmd = keymap_lookup_1 (k, &(raw_keys[0]), accept_default);
|
|
2027 if (NILP (cmd))
|
|
2028 {
|
|
2029 /* Do kludgy return of the meta-map */
|
|
2030 cmd = Fgethash (MAKE_MODIFIER_HASH_KEY (MOD_META),
|
|
2031 XKEYMAP (k)->table, Qnil);
|
|
2032 }
|
|
2033 }
|
|
2034 else
|
|
2035 {
|
|
2036 /* Search for the prefix-char-prefixed sequence directly */
|
|
2037 cmd = keymap_lookup_1 (k, &(raw_keys[0]), accept_default);
|
|
2038 cmd = get_keymap (cmd, 0, 1);
|
|
2039 if (!NILP (cmd))
|
|
2040 cmd = raw_lookup_key (cmd, raw_keys + 1, remaining,
|
|
2041 keys_so_far + 1, accept_default);
|
|
2042 else if ((raw_keys[1].modifiers & MOD_META) == 0)
|
|
2043 {
|
|
2044 struct key_data metified;
|
|
2045 metified.keysym = raw_keys[1].keysym;
|
|
2046 metified.modifiers = raw_keys[1].modifiers | MOD_META;
|
|
2047
|
|
2048 /* Search for meta-next-char sequence directly */
|
|
2049 cmd = keymap_lookup_1 (k, &metified, accept_default);
|
|
2050 if (remaining == 1)
|
|
2051 ;
|
|
2052 else
|
|
2053 {
|
|
2054 cmd = get_keymap (cmd, 0, 1);
|
|
2055 if (!NILP (cmd))
|
|
2056 cmd = raw_lookup_key (cmd, raw_keys + 2, remaining - 1,
|
|
2057 keys_so_far + 2,
|
|
2058 accept_default);
|
|
2059 }
|
|
2060 }
|
|
2061 }
|
|
2062 }
|
|
2063 if (accept_default && NILP (cmd))
|
|
2064 cmd = XKEYMAP (k)->default_binding;
|
|
2065 return (cmd);
|
|
2066 }
|
|
2067
|
|
2068 /* Value is number if `keys' is too long; NIL if valid but has no definition.*/
|
|
2069 /* Caller should gc-protect arguments */
|
|
2070 static Lisp_Object
|
|
2071 lookup_keys (Lisp_Object keymap, int nkeys, Lisp_Object *keys,
|
|
2072 int accept_default)
|
|
2073 {
|
|
2074 /* This function can GC */
|
|
2075 struct key_data kkk[20];
|
|
2076 struct key_data *raw_keys;
|
|
2077 int i;
|
|
2078
|
|
2079 if (nkeys == 0)
|
|
2080 return Qnil;
|
|
2081
|
70
|
2082 if (nkeys > (countof (kkk)))
|
0
|
2083 raw_keys = kkk;
|
|
2084 else
|
|
2085 raw_keys = (struct key_data *) alloca (sizeof (struct key_data) * nkeys);
|
|
2086
|
|
2087 for (i = 0; i < nkeys; i++)
|
|
2088 {
|
|
2089 define_key_parser (keys[i], &(raw_keys[i]));
|
|
2090 }
|
|
2091 return (raw_lookup_key (keymap, raw_keys, nkeys, 0,
|
|
2092 accept_default));
|
|
2093 }
|
|
2094
|
|
2095 static Lisp_Object
|
|
2096 lookup_events (Lisp_Object event_head, int nmaps, Lisp_Object keymaps[],
|
|
2097 int accept_default)
|
|
2098 {
|
|
2099 /* This function can GC */
|
|
2100 struct key_data kkk[20];
|
|
2101 Lisp_Object event;
|
|
2102
|
|
2103 int nkeys;
|
|
2104 struct key_data *raw_keys;
|
|
2105 Lisp_Object tem = Qnil;
|
|
2106 struct gcpro gcpro1, gcpro2;
|
|
2107 int iii;
|
|
2108
|
|
2109 CHECK_LIVE_EVENT (event_head);
|
|
2110
|
|
2111 nkeys = event_chain_count (event_head);
|
|
2112
|
|
2113 if (nkeys < (countof (kkk)))
|
|
2114 raw_keys = kkk;
|
|
2115 else
|
|
2116 raw_keys = (struct key_data *) alloca (sizeof (struct key_data) * nkeys);
|
|
2117
|
|
2118 nkeys = 0;
|
|
2119 EVENT_CHAIN_LOOP (event, event_head)
|
|
2120 define_key_parser (event, &(raw_keys[nkeys++]));
|
|
2121 GCPRO2 (keymaps[0], event_head);
|
|
2122 gcpro1.nvars = nmaps;
|
|
2123 /* ####raw_keys[].keysym slots aren't gc-protected. We rely (but shouldn't)
|
|
2124 * on somebody else somewhere (obarray) having a pointer to all keysyms. */
|
|
2125 for (iii = 0; iii < nmaps; iii++)
|
|
2126 {
|
|
2127 tem = raw_lookup_key (keymaps[iii], raw_keys, nkeys, 0,
|
|
2128 accept_default);
|
|
2129 if (INTP (tem))
|
|
2130 {
|
|
2131 /* Too long in some local map means don't look at global map */
|
|
2132 tem = Qnil;
|
|
2133 break;
|
|
2134 }
|
|
2135 else if (!NILP (tem))
|
|
2136 break;
|
|
2137 }
|
|
2138 UNGCPRO;
|
|
2139 return (tem);
|
|
2140 }
|
|
2141
|
20
|
2142 DEFUN ("lookup-key", Flookup_key, 2, 3, 0, /*
|
0
|
2143 In keymap KEYMAP, look up key-sequence KEYS. Return the definition.
|
|
2144 Nil is returned if KEYS is unbound. See documentation of `define-key'
|
|
2145 for valid key definitions and key-sequence specifications.
|
|
2146 A number is returned if KEYS is \"too long\"; that is, the leading
|
|
2147 characters fail to be a valid sequence of prefix characters in KEYMAP.
|
|
2148 The number is how many characters at the front of KEYS
|
|
2149 it takes to reach a non-prefix command.
|
20
|
2150 */
|
|
2151 (keymap, keys, accept_default))
|
0
|
2152 {
|
|
2153 /* This function can GC */
|
|
2154 if (VECTORP (keys))
|
|
2155 {
|
|
2156 return lookup_keys (keymap,
|
|
2157 vector_length (XVECTOR (keys)),
|
|
2158 vector_data (XVECTOR (keys)),
|
|
2159 !NILP (accept_default));
|
|
2160 }
|
70
|
2161 else if (SYMBOLP (keys) || CHAR_OR_CHAR_INTP (keys) || CONSP (keys))
|
0
|
2162 {
|
|
2163 return lookup_keys (keymap, 1, &keys,
|
|
2164 !NILP (accept_default));
|
|
2165 }
|
|
2166 else if (!STRINGP (keys))
|
|
2167 {
|
|
2168 keys = wrong_type_argument (Qsequencep, keys);
|
|
2169 return Flookup_key (keymap, keys, accept_default);
|
|
2170 }
|
2
|
2171 else /* STRINGP (keys) */
|
0
|
2172 {
|
|
2173 int length = string_char_length (XSTRING (keys));
|
|
2174 int i;
|
|
2175 struct key_data *raw_keys
|
|
2176 = (struct key_data *) alloca (sizeof (struct key_data) * length);
|
|
2177 if (length == 0)
|
|
2178 return Qnil;
|
|
2179
|
|
2180 for (i = 0; i < length; i++)
|
|
2181 {
|
|
2182 Emchar n = string_char (XSTRING (keys), i);
|
|
2183 define_key_parser (make_char (n), &(raw_keys[i]));
|
|
2184 }
|
|
2185 return (raw_lookup_key (keymap, raw_keys, length, 0,
|
|
2186 !NILP (accept_default)));
|
|
2187 }
|
|
2188 }
|
|
2189
|
|
2190 /* Given a key sequence, returns a list of keymaps to search for bindings.
|
|
2191 Does all manner of semi-hairy heuristics, like looking in the current
|
|
2192 buffer's map before looking in the global map and looking in the local
|
|
2193 map of the buffer in which the mouse was clicked in event0 is a click.
|
|
2194
|
|
2195 It would be kind of nice if this were in Lisp so that this semi-hairy
|
|
2196 semi-heuristic command-lookup behaviour could be readily understood and
|
|
2197 customised. However, this needs to be pretty fast, or performance of
|
|
2198 keyboard macros goes to shit; putting this in lisp slows macros down
|
|
2199 2-3x. And they're already slower than v18 by 5-6x.
|
|
2200 */
|
|
2201
|
|
2202 struct relevant_maps
|
|
2203 {
|
|
2204 int nmaps;
|
|
2205 unsigned int max_maps;
|
|
2206 Lisp_Object *maps;
|
|
2207 struct gcpro *gcpro;
|
|
2208 };
|
|
2209
|
|
2210 static void get_relevant_extent_keymaps (Lisp_Object pos,
|
|
2211 Lisp_Object buffer_or_string,
|
|
2212 Lisp_Object glyph,
|
|
2213 struct relevant_maps *closure);
|
|
2214 static void get_relevant_minor_maps (Lisp_Object buffer,
|
|
2215 struct relevant_maps *closure);
|
|
2216
|
|
2217 static void
|
|
2218 relevant_map_push (Lisp_Object map, struct relevant_maps *closure)
|
|
2219 {
|
|
2220 unsigned int nmaps = closure->nmaps;
|
|
2221
|
|
2222 if (!KEYMAPP (map))
|
|
2223 return;
|
|
2224 closure->nmaps = nmaps + 1;
|
|
2225 if (nmaps < closure->max_maps)
|
|
2226 {
|
|
2227 closure->maps[nmaps] = map;
|
|
2228 closure->gcpro->nvars = nmaps;
|
|
2229 }
|
|
2230 }
|
|
2231
|
|
2232 static int
|
|
2233 get_relevant_keymaps (Lisp_Object keys,
|
|
2234 int max_maps, Lisp_Object maps[])
|
|
2235 {
|
|
2236 /* This function can GC */
|
|
2237 Lisp_Object terminal = Qnil;
|
|
2238 struct gcpro gcpro1;
|
|
2239 struct relevant_maps closure;
|
|
2240 struct console *con;
|
|
2241
|
|
2242 GCPRO1 (*maps);
|
|
2243 gcpro1.nvars = 0;
|
|
2244 closure.nmaps = 0;
|
|
2245 closure.max_maps = max_maps;
|
|
2246 closure.maps = maps;
|
|
2247 closure.gcpro = &gcpro1;
|
|
2248
|
|
2249 if (EVENTP (keys))
|
|
2250 terminal = event_chain_tail (keys);
|
|
2251 else if (VECTORP (keys))
|
|
2252 {
|
|
2253 int len = vector_length (XVECTOR (keys));
|
|
2254 if (len > 0)
|
|
2255 terminal = vector_data (XVECTOR (keys))[len - 1];
|
|
2256 }
|
|
2257
|
|
2258 if (EVENTP (terminal))
|
|
2259 {
|
|
2260 CHECK_LIVE_EVENT (terminal);
|
|
2261 con = event_console_or_selected (terminal);
|
|
2262 }
|
|
2263 else
|
|
2264 con = XCONSOLE (Vselected_console);
|
|
2265
|
|
2266 if (KEYMAPP (con->overriding_terminal_local_map)
|
|
2267 || KEYMAPP (Voverriding_local_map))
|
|
2268 {
|
|
2269 if (KEYMAPP (con->overriding_terminal_local_map))
|
|
2270 relevant_map_push (con->overriding_terminal_local_map, &closure);
|
|
2271 if (KEYMAPP (Voverriding_local_map))
|
|
2272 relevant_map_push (Voverriding_local_map, &closure);
|
|
2273 }
|
|
2274 else if (!EVENTP (terminal)
|
|
2275 || (XEVENT (terminal)->event_type != button_press_event
|
|
2276 && XEVENT (terminal)->event_type != button_release_event))
|
|
2277 {
|
|
2278 Lisp_Object tem;
|
|
2279 XSETBUFFER (tem, current_buffer);
|
|
2280 /* It's not a mouse event; order of keymaps searched is:
|
|
2281 o keymap of any/all extents under the mouse
|
|
2282 o minor-mode maps
|
|
2283 o local-map of current-buffer
|
|
2284 o global-map
|
|
2285 */
|
|
2286 /* The terminal element of the lookup may be nil or a keysym.
|
|
2287 In those cases we don't want to check for an extent
|
|
2288 keymap. */
|
|
2289 if (EVENTP (terminal))
|
|
2290 {
|
|
2291 get_relevant_extent_keymaps (make_int (BUF_PT (current_buffer)),
|
|
2292 tem, Qnil, &closure);
|
|
2293 }
|
|
2294 get_relevant_minor_maps (tem, &closure);
|
|
2295
|
|
2296 tem = current_buffer->keymap;
|
|
2297 if (!NILP (tem))
|
|
2298 relevant_map_push (tem, &closure);
|
|
2299 }
|
|
2300 #ifdef HAVE_WINDOW_SYSTEM
|
|
2301 else
|
|
2302 {
|
|
2303 /* It's a mouse event; order of keymaps searched is:
|
|
2304 o local-map of mouse-grabbed-buffer
|
|
2305 o keymap of any/all extents under the mouse
|
|
2306 if the mouse is over a modeline:
|
|
2307 o modeline-map of buffer corresponding to that modeline
|
|
2308 o else, local-map of buffer under the mouse
|
|
2309 o minor-mode maps
|
|
2310 o local-map of current-buffer
|
|
2311 o global-map
|
|
2312 */
|
|
2313 Lisp_Object window = Fevent_window (terminal);
|
|
2314
|
|
2315 if (BUFFERP (Vmouse_grabbed_buffer))
|
|
2316 {
|
|
2317 Lisp_Object map = XBUFFER (Vmouse_grabbed_buffer)->keymap;
|
|
2318
|
|
2319 get_relevant_minor_maps (Vmouse_grabbed_buffer, &closure);
|
|
2320 if (!NILP (map))
|
|
2321 relevant_map_push (map, &closure);
|
|
2322 }
|
|
2323
|
|
2324 if (!NILP (window))
|
|
2325 {
|
|
2326 Lisp_Object buffer = Fwindow_buffer (window);
|
|
2327
|
|
2328 if (!NILP (buffer))
|
|
2329 {
|
|
2330 if (!NILP (Fevent_over_modeline_p (terminal)))
|
|
2331 {
|
|
2332 Lisp_Object map = symbol_value_in_buffer (Qmodeline_map,
|
|
2333 buffer);
|
|
2334
|
|
2335 get_relevant_extent_keymaps
|
|
2336 (Fevent_modeline_position (terminal),
|
|
2337 XBUFFER (buffer)->generated_modeline_string,
|
|
2338 /* #### third arg should maybe be a glyph. */
|
|
2339 Qnil, &closure);
|
|
2340
|
|
2341 if (!UNBOUNDP (map) && !NILP (map))
|
|
2342 relevant_map_push (get_keymap (map, 1, 1), &closure);
|
|
2343 }
|
|
2344 else
|
|
2345 {
|
|
2346 get_relevant_extent_keymaps (Fevent_point (terminal), buffer,
|
|
2347 Fevent_glyph_extent (terminal),
|
|
2348 &closure);
|
|
2349 }
|
|
2350
|
|
2351 if (!EQ (buffer, Vmouse_grabbed_buffer)) /* already pushed */
|
|
2352 {
|
|
2353 get_relevant_minor_maps (buffer, &closure);
|
70
|
2354 relevant_map_push (XBUFFER (buffer)->keymap, &closure);
|
0
|
2355 }
|
|
2356 }
|
|
2357 }
|
|
2358 else if (!NILP (Fevent_over_toolbar_p (terminal)))
|
|
2359 {
|
|
2360 Lisp_Object map = Fsymbol_value (Qtoolbar_map);
|
|
2361
|
|
2362 if (!UNBOUNDP (map) && !NILP (map))
|
|
2363 relevant_map_push (map, &closure);
|
|
2364 }
|
|
2365 }
|
|
2366 #endif /* HAVE_WINDOW_SYSTEM */
|
|
2367
|
|
2368 {
|
|
2369 int nmaps = closure.nmaps;
|
|
2370 /* Silently truncate at 100 keymaps to prevent infinite losssage */
|
|
2371 if (nmaps >= max_maps && max_maps > 0)
|
|
2372 maps[max_maps - 1] = Vcurrent_global_map;
|
|
2373 else
|
|
2374 maps[nmaps] = Vcurrent_global_map;
|
|
2375 UNGCPRO;
|
|
2376 return (nmaps + 1);
|
|
2377 }
|
|
2378 }
|
|
2379
|
|
2380 /* Returns a set of keymaps extracted from the extents at POS in
|
|
2381 BUFFER_OR_STRING. The GLYPH arg, if specified, is one more extent
|
|
2382 to look for a keymap in, and if it has one, its keymap will be the
|
|
2383 first element in the list returned. This is so we can correctly
|
|
2384 search the keymaps associated with glyphs which may be physically
|
|
2385 disjoint from their extents: for example, if a glyph is out in the
|
|
2386 margin, we should still consult the kemyap of that glyph's extent,
|
|
2387 which may not itself be under the mouse.
|
|
2388 */
|
|
2389 static void
|
|
2390 get_relevant_extent_keymaps (Lisp_Object pos, Lisp_Object buffer_or_string,
|
|
2391 Lisp_Object glyph,
|
|
2392 struct relevant_maps *closure)
|
|
2393 {
|
|
2394 /* This function can GC */
|
|
2395 /* the glyph keymap, if any, comes first.
|
|
2396 (Processing it twice is no big deal: noop.) */
|
|
2397 if (!NILP (glyph))
|
|
2398 {
|
|
2399 Lisp_Object keymap = Fextent_property (glyph, Qkeymap, Qnil);
|
|
2400 if (!NILP (keymap))
|
|
2401 relevant_map_push (get_keymap (keymap, 1, 1), closure);
|
|
2402 }
|
|
2403
|
|
2404 /* Next check the extents at the text position, if any */
|
|
2405 if (!NILP (pos))
|
|
2406 {
|
|
2407 Lisp_Object extent;
|
|
2408 for (extent = Fextent_at (pos, buffer_or_string, Qkeymap, Qnil, Qnil);
|
|
2409 !NILP (extent);
|
|
2410 extent = Fextent_at (pos, buffer_or_string, Qkeymap, extent, Qnil))
|
|
2411 {
|
|
2412 Lisp_Object keymap = Fextent_property (extent, Qkeymap, Qnil);
|
|
2413 if (!NILP (keymap))
|
|
2414 relevant_map_push (get_keymap (keymap, 1, 1), closure);
|
|
2415 QUIT;
|
|
2416 }
|
|
2417 }
|
|
2418 }
|
|
2419
|
|
2420 static Lisp_Object
|
|
2421 minor_mode_keymap_predicate (Lisp_Object assoc, Lisp_Object buffer)
|
|
2422 {
|
|
2423 /* This function can GC */
|
|
2424 if (CONSP (assoc))
|
|
2425 {
|
|
2426 Lisp_Object sym = XCAR (assoc);
|
|
2427 if (SYMBOLP (sym))
|
|
2428 {
|
|
2429 Lisp_Object val = symbol_value_in_buffer (sym, buffer);
|
|
2430 if (!NILP (val) && !UNBOUNDP (val))
|
|
2431 {
|
|
2432 Lisp_Object map = get_keymap (XCDR (assoc), 0, 1);
|
|
2433 return (map);
|
|
2434 }
|
|
2435 }
|
|
2436 }
|
|
2437 return (Qnil);
|
|
2438 }
|
|
2439
|
|
2440 static void
|
|
2441 get_relevant_minor_maps (Lisp_Object buffer, struct relevant_maps *closure)
|
|
2442 {
|
|
2443 /* This function can GC */
|
|
2444 Lisp_Object alist;
|
|
2445
|
|
2446 /* Will you ever lose badly if you make this circular! */
|
|
2447 for (alist = symbol_value_in_buffer (Qminor_mode_map_alist, buffer);
|
|
2448 CONSP (alist);
|
|
2449 alist = XCDR (alist))
|
|
2450 {
|
|
2451 Lisp_Object m = minor_mode_keymap_predicate (XCAR (alist),
|
|
2452 buffer);
|
|
2453 if (!NILP (m)) relevant_map_push (m, closure);
|
|
2454 QUIT;
|
|
2455 }
|
|
2456 }
|
|
2457
|
|
2458 /* #### Would map-current-keymaps be a better thing?? */
|
20
|
2459 DEFUN ("current-keymaps", Fcurrent_keymaps, 0, 1, 0, /*
|
0
|
2460 Return a list of the current keymaps that will be searched for bindings.
|
|
2461 This lists keymaps such as the current local map and the minor-mode maps,
|
|
2462 but does not list the parents of those keymaps.
|
|
2463 EVENT-OR-KEYS controls which keymaps will be listed.
|
|
2464 If EVENT-OR-KEYS is a mouse event (or a vector whose last element is a
|
|
2465 mouse event), the keymaps for that mouse event will be listed (see
|
|
2466 `key-binding'). Otherwise, the keymaps for key presses will be listed.
|
20
|
2467 */
|
|
2468 (event_or_keys))
|
0
|
2469 {
|
|
2470 /* This function can GC */
|
|
2471 struct gcpro gcpro1;
|
|
2472 Lisp_Object maps[100];
|
|
2473 Lisp_Object *gubbish = maps;
|
|
2474 int nmaps;
|
|
2475
|
|
2476 GCPRO1 (event_or_keys);
|
|
2477 nmaps = get_relevant_keymaps (event_or_keys, countof (maps),
|
|
2478 gubbish);
|
|
2479 if (nmaps > countof (maps))
|
|
2480 {
|
|
2481 gubbish = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
|
|
2482 nmaps = get_relevant_keymaps (event_or_keys, nmaps, gubbish);
|
|
2483 }
|
|
2484 UNGCPRO;
|
|
2485 return (Flist (nmaps, gubbish));
|
|
2486 }
|
|
2487
|
20
|
2488 DEFUN ("key-binding", Fkey_binding, 1, 2, 0, /*
|
0
|
2489 Return the binding for command KEYS in current keymaps.
|
|
2490 KEYS is a string, a vector of events, or a vector of key-description lists
|
|
2491 as described in the documentation for the `define-key' function.
|
|
2492 The binding is probably a symbol with a function definition; see
|
|
2493 the documentation for `lookup-key' for more information.
|
|
2494
|
|
2495 For key-presses, the order of keymaps searched is:
|
|
2496 - the `keymap' property of any extent(s) at point;
|
|
2497 - any applicable minor-mode maps;
|
|
2498 - the current-local-map of the current-buffer;
|
|
2499 - the current global map.
|
|
2500
|
|
2501 For mouse-clicks, the order of keymaps searched is:
|
|
2502 - the current-local-map of the `mouse-grabbed-buffer' if any;
|
|
2503 - the `keymap' property of any extent(s) at the position of the click
|
|
2504 (this includes modeline extents);
|
|
2505 - the modeline-map of the buffer corresponding to the modeline under
|
|
2506 the mouse (if the click happened over a modeline);
|
|
2507 - the value of toolbar-map in the current-buffer (if the click
|
|
2508 happened over a toolbar);
|
|
2509 - the current-local-map of the buffer under the mouse (does not
|
|
2510 apply to toolbar clicks);
|
|
2511 - any applicable minor-mode maps;
|
|
2512 - the current global map.
|
|
2513
|
|
2514 Note that if `overriding-local-map' or `overriding-terminal-local-map'
|
|
2515 is non-nil, *only* those two maps and the current global map are searched.
|
20
|
2516 */
|
|
2517 (keys, accept_default))
|
0
|
2518 {
|
|
2519 /* This function can GC */
|
|
2520 int i;
|
|
2521 Lisp_Object maps[100];
|
|
2522 int nmaps;
|
|
2523 struct gcpro gcpro1, gcpro2;
|
|
2524 GCPRO2 (keys, accept_default); /* get_relevant_keymaps may autoload */
|
|
2525
|
|
2526 nmaps = get_relevant_keymaps (keys, countof (maps), maps);
|
|
2527
|
|
2528 UNGCPRO;
|
|
2529
|
|
2530 if (EVENTP (keys)) /* unadvertised "feature" for the future */
|
|
2531 return (lookup_events (keys, nmaps, maps,
|
|
2532 !NILP (accept_default)));
|
|
2533
|
|
2534 for (i = 0; i < nmaps; i++)
|
|
2535 {
|
|
2536 Lisp_Object tem = Flookup_key (maps[i], keys,
|
|
2537 accept_default);
|
|
2538 if (INTP (tem))
|
|
2539 {
|
|
2540 /* Too long in some local map means don't look at global map */
|
|
2541 return (Qnil);
|
|
2542 }
|
|
2543 else if (!NILP (tem))
|
|
2544 return (tem);
|
|
2545 }
|
|
2546 return (Qnil);
|
|
2547 }
|
|
2548
|
|
2549 static Lisp_Object
|
|
2550 process_event_binding_result (Lisp_Object result)
|
|
2551 {
|
|
2552 if (EQ (result, Qundefined))
|
|
2553 /* The suppress-keymap function binds keys to 'undefined - special-case
|
|
2554 that here, so that being bound to that has the same error-behavior as
|
|
2555 not being defined at all.
|
|
2556 */
|
|
2557 result = Qnil;
|
|
2558 if (!NILP (result))
|
|
2559 {
|
|
2560 Lisp_Object map;
|
|
2561 /* Snap out possible keymap indirections */
|
|
2562 map = get_keymap (result, 0, 1);
|
|
2563 if (!NILP (map))
|
|
2564 result = map;
|
|
2565 }
|
|
2566
|
|
2567 return result;
|
|
2568 }
|
|
2569
|
|
2570 /* Attempts to find a command corresponding to the event-sequence
|
|
2571 whose head is event0 (sequence is threaded though event_next).
|
|
2572
|
|
2573 The return value will be
|
|
2574
|
|
2575 -- nil (there is no binding; this will also be returned
|
|
2576 whenever the event chain is "too long", i.e. there
|
|
2577 is a non-nil, non-keymap binding for a prefix of
|
|
2578 the event chain)
|
|
2579 -- a keymap (part of a command has been specified)
|
|
2580 -- a command (anything that satisfies `commandp'; this includes
|
|
2581 some symbols, lists, subrs, strings, vectors, and
|
|
2582 compiled-function objects) */
|
|
2583 Lisp_Object
|
|
2584 event_binding (Lisp_Object event0, int accept_default)
|
|
2585 {
|
|
2586 /* This function can GC */
|
|
2587 Lisp_Object maps[100];
|
|
2588 int nmaps;
|
|
2589
|
|
2590 assert (EVENTP (event0));
|
|
2591
|
|
2592 nmaps = get_relevant_keymaps (event0, countof (maps), maps);
|
|
2593 return (process_event_binding_result
|
|
2594 (lookup_events (event0, nmaps, maps, accept_default)));
|
|
2595 }
|
|
2596
|
|
2597 /* Attempts to find a function key mapping corresponding to the
|
|
2598 event-sequence whose head is event0 (sequence is threaded through
|
|
2599 event_next). The return value will be the same as for event_binding(). */
|
|
2600 Lisp_Object
|
|
2601 munging_key_map_event_binding (Lisp_Object event0,
|
|
2602 enum munge_me_out_the_door munge)
|
|
2603 {
|
|
2604 Lisp_Object the_map;
|
|
2605 Lisp_Object maps[1];
|
|
2606
|
|
2607 if (munge == MUNGE_ME_FUNCTION_KEY)
|
|
2608 {
|
|
2609 struct console *c = event_console_or_selected (event0);
|
|
2610
|
|
2611 the_map = CONSOLE_FUNCTION_KEY_MAP (c);
|
|
2612 }
|
|
2613 else
|
|
2614 the_map = Vkey_translation_map;
|
|
2615
|
|
2616 if (NILP (the_map))
|
|
2617 return Qnil;
|
|
2618
|
|
2619 maps[0] = the_map;
|
|
2620 return process_event_binding_result (lookup_events (event0, 1, maps, 1));
|
|
2621 }
|
|
2622
|
|
2623
|
|
2624 /************************************************************************/
|
|
2625 /* Setting/querying the global and local maps */
|
|
2626 /************************************************************************/
|
|
2627
|
20
|
2628 DEFUN ("use-global-map", Fuse_global_map, 1, 1, 0, /*
|
0
|
2629 Select KEYMAP as the global keymap.
|
20
|
2630 */
|
|
2631 (keymap))
|
0
|
2632 {
|
|
2633 /* This function can GC */
|
|
2634 keymap = get_keymap (keymap, 1, 1);
|
|
2635 Vcurrent_global_map = keymap;
|
|
2636 return Qnil;
|
|
2637 }
|
|
2638
|
20
|
2639 DEFUN ("use-local-map", Fuse_local_map, 1, 2, 0, /*
|
0
|
2640 Select KEYMAP as the local keymap in BUFFER.
|
|
2641 If KEYMAP is nil, that means no local keymap.
|
|
2642 If BUFFER is nil, the current buffer is assumed.
|
20
|
2643 */
|
|
2644 (keymap, buffer))
|
0
|
2645 {
|
|
2646 /* This function can GC */
|
|
2647 struct buffer *b = decode_buffer (buffer, 0);
|
|
2648 if (!NILP (keymap))
|
|
2649 keymap = get_keymap (keymap, 1, 1);
|
|
2650
|
|
2651 b->keymap = keymap;
|
|
2652
|
|
2653 return Qnil;
|
|
2654 }
|
|
2655
|
20
|
2656 DEFUN ("current-local-map", Fcurrent_local_map, 0, 1, 0, /*
|
0
|
2657 Return BUFFER's local keymap, or nil if it has none.
|
|
2658 If BUFFER is nil, the current buffer is assumed.
|
20
|
2659 */
|
|
2660 (buffer))
|
0
|
2661 {
|
|
2662 struct buffer *b = decode_buffer (buffer, 0);
|
|
2663 return b->keymap;
|
|
2664 }
|
|
2665
|
20
|
2666 DEFUN ("current-global-map", Fcurrent_global_map, 0, 0, 0, /*
|
0
|
2667 Return the current global keymap.
|
20
|
2668 */
|
|
2669 ())
|
0
|
2670 {
|
|
2671 return (Vcurrent_global_map);
|
|
2672 }
|
|
2673
|
|
2674
|
|
2675 /************************************************************************/
|
|
2676 /* Mapping over keymap elements */
|
|
2677 /************************************************************************/
|
|
2678
|
|
2679 /* Since keymaps are arranged in a hierarchy, one keymap per bucky bit or
|
|
2680 prefix key, it's not entirely objvious what map-keymap should do, but
|
|
2681 what it does is: map over all keys in this map; then recursively map
|
|
2682 over all submaps of this map that are "bucky" submaps. This means that,
|
|
2683 when mapping over a keymap, it appears that "x" and "C-x" are in the
|
|
2684 same map, although "C-x" is really in the "control" submap of this one.
|
|
2685 However, since we don't recursively descend the submaps that are bound
|
|
2686 to prefix keys (like C-x, C-h, etc) the caller will have to recurse on
|
|
2687 those explicitly, if that's what they want.
|
|
2688
|
|
2689 So the end result of this is that the bucky keymaps (the ones indexed
|
|
2690 under the large integers returned from MAKE_MODIFIER_HASH_KEY()) are
|
|
2691 invisible from elisp. They're just an implementation detail that code
|
|
2692 outside of this file doesn't need to know about.
|
|
2693 */
|
|
2694
|
|
2695 struct map_keymap_unsorted_closure
|
|
2696 {
|
|
2697 void (*fn) (CONST struct key_data *, Lisp_Object binding, void *arg);
|
|
2698 void *arg;
|
|
2699 unsigned int modifiers;
|
|
2700 };
|
|
2701
|
|
2702 /* used by map_keymap() */
|
|
2703 static void
|
|
2704 map_keymap_unsorted_mapper (CONST void *hash_key, void *hash_contents,
|
|
2705 void *map_keymap_unsorted_closure)
|
|
2706 {
|
|
2707 /* This function can GC */
|
|
2708 Lisp_Object keysym;
|
|
2709 Lisp_Object contents;
|
|
2710 struct map_keymap_unsorted_closure *closure = map_keymap_unsorted_closure;
|
|
2711 unsigned int modifiers = closure->modifiers;
|
|
2712 unsigned int mod_bit;
|
|
2713 CVOID_TO_LISP (keysym, hash_key);
|
|
2714 VOID_TO_LISP (contents, hash_contents);
|
|
2715 mod_bit = MODIFIER_HASH_KEY_BITS (keysym);
|
|
2716 if (mod_bit != 0)
|
|
2717 {
|
|
2718 int omod = modifiers;
|
|
2719 closure->modifiers = (modifiers | mod_bit);
|
|
2720 contents = get_keymap (contents, 1, 1);
|
|
2721 elisp_maphash (map_keymap_unsorted_mapper,
|
|
2722 XKEYMAP (contents)->table,
|
|
2723 map_keymap_unsorted_closure);
|
|
2724 closure->modifiers = omod;
|
|
2725 }
|
|
2726 else
|
|
2727 {
|
|
2728 struct key_data key;
|
|
2729 key.keysym = keysym;
|
|
2730 key.modifiers = modifiers;
|
|
2731 ((*closure->fn) (&key, contents, closure->arg));
|
|
2732 }
|
|
2733 }
|
|
2734
|
|
2735
|
|
2736 struct map_keymap_sorted_closure
|
|
2737 {
|
|
2738 Lisp_Object *result_locative;
|
|
2739 };
|
|
2740
|
|
2741 /* used by map_keymap_sorted() */
|
|
2742 static void
|
|
2743 map_keymap_sorted_mapper (CONST void *hash_key, void *hash_contents,
|
|
2744 void *map_keymap_sorted_closure)
|
|
2745 {
|
|
2746 struct map_keymap_sorted_closure *cl = map_keymap_sorted_closure;
|
|
2747 Lisp_Object key, contents;
|
|
2748 Lisp_Object *list = cl->result_locative;
|
|
2749 CVOID_TO_LISP (key, hash_key);
|
|
2750 VOID_TO_LISP (contents, hash_contents);
|
|
2751 *list = Fcons (Fcons (key, contents), *list);
|
|
2752 }
|
|
2753
|
|
2754
|
|
2755 /* used by map_keymap_sorted(), describe_map_sort_predicate(),
|
|
2756 and keymap_submaps().
|
|
2757 */
|
|
2758 static int
|
|
2759 map_keymap_sort_predicate (Lisp_Object obj1, Lisp_Object obj2,
|
|
2760 Lisp_Object pred)
|
|
2761 {
|
|
2762 /* obj1 and obj2 are conses with keysyms in their cars. Cdrs are ignored.
|
|
2763 */
|
|
2764 unsigned int bit1, bit2;
|
|
2765 int sym1_p = 0;
|
|
2766 int sym2_p = 0;
|
|
2767 obj1 = XCAR (obj1);
|
|
2768 obj2 = XCAR (obj2);
|
|
2769
|
|
2770 if (EQ (obj1, obj2))
|
|
2771 return -1;
|
|
2772 bit1 = MODIFIER_HASH_KEY_BITS (obj1);
|
|
2773 bit2 = MODIFIER_HASH_KEY_BITS (obj2);
|
|
2774
|
|
2775 /* If either is a symbol with a character-set-property, then sort it by
|
|
2776 that code instead of alphabetically.
|
|
2777 */
|
|
2778 if (! bit1 && SYMBOLP (obj1))
|
|
2779 {
|
|
2780 Lisp_Object code = Fget (obj1, Vcharacter_set_property, Qnil);
|
70
|
2781 if (CHAR_OR_CHAR_INTP (code))
|
|
2782 {
|
|
2783 obj1 = code;
|
|
2784 CHECK_CHAR_COERCE_INT (obj1);
|
|
2785 sym1_p = 1;
|
|
2786 }
|
0
|
2787 }
|
|
2788 if (! bit2 && SYMBOLP (obj2))
|
|
2789 {
|
|
2790 Lisp_Object code = Fget (obj2, Vcharacter_set_property, Qnil);
|
70
|
2791 if (CHAR_OR_CHAR_INTP (code))
|
|
2792 {
|
|
2793 obj2 = code;
|
|
2794 CHECK_CHAR_COERCE_INT (obj2);
|
|
2795 sym2_p = 1;
|
|
2796 }
|
0
|
2797 }
|
|
2798
|
|
2799 /* all symbols (non-ASCIIs) come after characters (ASCIIs) */
|
|
2800 if (XTYPE (obj1) != XTYPE (obj2))
|
|
2801 return (SYMBOLP (obj2) ? 1 : -1);
|
|
2802
|
|
2803 if (! bit1 && CHARP (obj1)) /* they're both ASCII */
|
|
2804 {
|
|
2805 int o1 = XCHAR (obj1);
|
|
2806 int o2 = XCHAR (obj2);
|
|
2807 if (o1 == o2 && /* If one started out as a symbol and the */
|
|
2808 sym1_p != sym2_p) /* other didn't, the symbol comes last. */
|
|
2809 return (sym2_p ? 1 : -1);
|
|
2810
|
|
2811 return ((o1 < o2) ? 1 : -1); /* else just compare them */
|
|
2812 }
|
|
2813
|
|
2814 /* else they're both symbols. If they're both buckys, then order them. */
|
|
2815 if (bit1 && bit2)
|
|
2816 return ((bit1 < bit2) ? 1 : -1);
|
|
2817
|
|
2818 /* if only one is a bucky, then it comes later */
|
|
2819 if (bit1 || bit2)
|
|
2820 return (bit2 ? 1 : -1);
|
|
2821
|
|
2822 /* otherwise, string-sort them. */
|
|
2823 {
|
|
2824 char *s1 = (char *) string_data (XSYMBOL (obj1)->name);
|
|
2825 char *s2 = (char *) string_data (XSYMBOL (obj2)->name);
|
|
2826 return (
|
|
2827 #ifdef I18N2
|
|
2828 (0 > strcoll (s1, s2))
|
|
2829 #else
|
|
2830 (0 > strcmp (s1, s2))
|
|
2831 #endif
|
|
2832 ? 1 : -1);
|
|
2833 }
|
|
2834 }
|
|
2835
|
|
2836
|
|
2837 /* used by map_keymap() */
|
|
2838 static void
|
|
2839 map_keymap_sorted (Lisp_Object keymap_table,
|
|
2840 unsigned int modifiers,
|
|
2841 void (*function) (CONST struct key_data *key,
|
|
2842 Lisp_Object binding,
|
|
2843 void *map_keymap_sorted_closure),
|
|
2844 void *map_keymap_sorted_closure)
|
|
2845 {
|
|
2846 /* This function can GC */
|
|
2847 struct gcpro gcpro1;
|
|
2848 Lisp_Object contents = Qnil;
|
|
2849
|
|
2850 if (XINT (Fhashtable_fullness (keymap_table)) == 0)
|
|
2851 return;
|
|
2852
|
|
2853 GCPRO1 (contents);
|
|
2854
|
|
2855 {
|
|
2856 struct map_keymap_sorted_closure c1;
|
|
2857 c1.result_locative = &contents;
|
|
2858 elisp_maphash (map_keymap_sorted_mapper, keymap_table, &c1);
|
|
2859 }
|
|
2860 contents = list_sort (contents, Qnil, map_keymap_sort_predicate);
|
|
2861 for (; !NILP (contents); contents = XCDR (contents))
|
|
2862 {
|
|
2863 Lisp_Object keysym = XCAR (XCAR (contents));
|
|
2864 Lisp_Object binding = XCDR (XCAR (contents));
|
|
2865 unsigned int sub_bits = MODIFIER_HASH_KEY_BITS (keysym);
|
|
2866 if (sub_bits != 0)
|
|
2867 map_keymap_sorted (XKEYMAP (get_keymap (binding,
|
|
2868 1, 1))->table,
|
|
2869 (modifiers | sub_bits),
|
|
2870 function,
|
|
2871 map_keymap_sorted_closure);
|
|
2872 else
|
|
2873 {
|
|
2874 struct key_data k;
|
|
2875 k.keysym = keysym;
|
|
2876 k.modifiers = modifiers;
|
|
2877 ((*function) (&k, binding, map_keymap_sorted_closure));
|
|
2878 }
|
|
2879 }
|
|
2880 UNGCPRO;
|
|
2881 }
|
|
2882
|
|
2883
|
|
2884 /* used by Fmap_keymap() */
|
|
2885 static void
|
|
2886 map_keymap_mapper (CONST struct key_data *key,
|
|
2887 Lisp_Object binding,
|
|
2888 void *function)
|
|
2889 {
|
|
2890 /* This function can GC */
|
|
2891 Lisp_Object fn;
|
|
2892 VOID_TO_LISP (fn, function);
|
|
2893 call2 (fn, make_key_description (key, 1), binding);
|
|
2894 }
|
|
2895
|
|
2896
|
|
2897 static void
|
|
2898 map_keymap (Lisp_Object keymap_table, int sort_first,
|
|
2899 void (*function) (CONST struct key_data *key,
|
|
2900 Lisp_Object binding,
|
|
2901 void *fn_arg),
|
|
2902 void *fn_arg)
|
|
2903 {
|
|
2904 /* This function can GC */
|
|
2905 if (sort_first)
|
|
2906 map_keymap_sorted (keymap_table, 0, function, fn_arg);
|
|
2907 else
|
|
2908 {
|
|
2909 struct map_keymap_unsorted_closure map_keymap_unsorted_closure;
|
|
2910 map_keymap_unsorted_closure.fn = function;
|
|
2911 map_keymap_unsorted_closure.arg = fn_arg;
|
|
2912 map_keymap_unsorted_closure.modifiers = 0;
|
|
2913 elisp_maphash (map_keymap_unsorted_mapper, keymap_table,
|
|
2914 &map_keymap_unsorted_closure);
|
|
2915 }
|
|
2916 }
|
|
2917
|
20
|
2918 DEFUN ("map-keymap", Fmap_keymap, 2, 3, 0, /*
|
0
|
2919 Apply FUNCTION to each element of KEYMAP.
|
|
2920 FUNCTION will be called with two arguments: a key-description list, and
|
|
2921 the binding. The order in which the elements of the keymap are passed to
|
|
2922 the function is unspecified. If the function inserts new elements into
|
|
2923 the keymap, it may or may not be called with them later. No element of
|
|
2924 the keymap will ever be passed to the function more than once.
|
|
2925
|
|
2926 The function will not be called on elements of this keymap's parents
|
|
2927 (see the function `keymap-parents') or upon keymaps which are contained
|
|
2928 within this keymap (multi-character definitions).
|
|
2929 It will be called on \"meta\" characters since they are not really
|
|
2930 two-character sequences.
|
|
2931
|
|
2932 If the optional third argument SORT-FIRST is non-nil, then the elements of
|
|
2933 the keymap will be passed to the mapper function in a canonical order.
|
|
2934 Otherwise, they will be passed in hash (that is, random) order, which is
|
|
2935 faster.
|
20
|
2936 */
|
70
|
2937 (function, keymap, sort_first))
|
0
|
2938 {
|
|
2939 /* This function can GC */
|
|
2940 struct gcpro gcpro1, gcpro2;
|
|
2941
|
|
2942 /* tolerate obviously transposed args */
|
|
2943 if (!NILP (Fkeymapp (function)))
|
|
2944 {
|
|
2945 Lisp_Object tmp = function;
|
|
2946 function = keymap;
|
|
2947 keymap = tmp;
|
|
2948 }
|
|
2949 GCPRO2 (function, keymap);
|
|
2950 keymap = get_keymap (keymap, 1, 1);
|
|
2951 map_keymap (XKEYMAP (keymap)->table, !NILP (sort_first),
|
|
2952 map_keymap_mapper, LISP_TO_VOID (function));
|
|
2953 UNGCPRO;
|
|
2954 return Qnil;
|
|
2955 }
|
|
2956
|
|
2957
|
|
2958
|
|
2959 /************************************************************************/
|
|
2960 /* Accessible keymaps */
|
|
2961 /************************************************************************/
|
|
2962
|
|
2963 struct accessible_keymaps_closure
|
|
2964 {
|
|
2965 Lisp_Object tail;
|
|
2966 };
|
|
2967
|
|
2968
|
|
2969 static void
|
|
2970 accessible_keymaps_mapper_1 (Lisp_Object keysym, Lisp_Object contents,
|
|
2971 unsigned int modifiers,
|
|
2972 struct accessible_keymaps_closure *closure)
|
|
2973 {
|
|
2974 /* This function can GC */
|
|
2975 unsigned int subbits = MODIFIER_HASH_KEY_BITS (keysym);
|
|
2976
|
|
2977 if (subbits != 0)
|
|
2978 {
|
|
2979 Lisp_Object submaps;
|
|
2980
|
|
2981 contents = get_keymap (contents, 1, 1);
|
|
2982 submaps = keymap_submaps (contents);
|
|
2983 for (; !NILP (submaps); submaps = XCDR (submaps))
|
|
2984 {
|
|
2985 accessible_keymaps_mapper_1 (XCAR (XCAR (submaps)),
|
|
2986 XCDR (XCAR (submaps)),
|
|
2987 (subbits | modifiers),
|
|
2988 closure);
|
|
2989 }
|
|
2990 }
|
|
2991 else
|
|
2992 {
|
|
2993 Lisp_Object thisseq = Fcar (Fcar (closure->tail));
|
|
2994 Lisp_Object cmd = get_keyelt (contents, 1);
|
|
2995 Lisp_Object vec;
|
|
2996 int j;
|
|
2997 struct key_data key;
|
|
2998 key.keysym = keysym;
|
|
2999 key.modifiers = modifiers;
|
|
3000
|
|
3001 if (NILP (cmd))
|
|
3002 abort ();
|
|
3003 cmd = get_keymap (cmd, 0, 1);
|
|
3004 if (!KEYMAPP (cmd))
|
|
3005 abort ();
|
|
3006
|
|
3007 vec = make_vector (vector_length (XVECTOR (thisseq)) + 1, Qnil);
|
|
3008 for (j = 0; j < vector_length (XVECTOR (thisseq)); j++)
|
|
3009 vector_data (XVECTOR (vec)) [j] = vector_data (XVECTOR (thisseq)) [j];
|
|
3010 vector_data (XVECTOR (vec)) [j] = make_key_description (&key, 1);
|
|
3011
|
|
3012 nconc2 (closure->tail, list1 (Fcons (vec, cmd)));
|
|
3013 }
|
|
3014 }
|
|
3015
|
|
3016
|
|
3017 static Lisp_Object
|
|
3018 accessible_keymaps_keymap_mapper (Lisp_Object thismap, void *arg)
|
|
3019 {
|
|
3020 /* This function can GC */
|
|
3021 struct accessible_keymaps_closure *closure = arg;
|
|
3022 Lisp_Object submaps = keymap_submaps (thismap);
|
|
3023
|
|
3024 for (; !NILP (submaps); submaps = XCDR (submaps))
|
|
3025 {
|
|
3026 accessible_keymaps_mapper_1 (XCAR (XCAR (submaps)),
|
|
3027 XCDR (XCAR (submaps)),
|
|
3028 0,
|
|
3029 closure);
|
|
3030 }
|
|
3031 return (Qnil);
|
|
3032 }
|
|
3033
|
|
3034
|
20
|
3035 DEFUN ("accessible-keymaps", Faccessible_keymaps, 1, 2, 0, /*
|
0
|
3036 Find all keymaps accessible via prefix characters from STARTMAP.
|
|
3037 Returns a list of elements of the form (KEYS . MAP), where the sequence
|
|
3038 KEYS starting from STARTMAP gets you to MAP. These elements are ordered
|
|
3039 so that the KEYS increase in length. The first element is ([] . STARTMAP).
|
|
3040 An optional argument PREFIX, if non-nil, should be a key sequence;
|
|
3041 then the value includes only maps for prefixes that start with PREFIX.
|
20
|
3042 */
|
|
3043 (startmap, prefix))
|
0
|
3044 {
|
|
3045 /* This function can GC */
|
|
3046 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
|
3047 Lisp_Object accessible_keymaps = Qnil;
|
|
3048 struct accessible_keymaps_closure c;
|
|
3049 c.tail = Qnil;
|
|
3050 GCPRO4 (accessible_keymaps, c.tail, prefix, startmap);
|
|
3051
|
|
3052 retry:
|
|
3053 startmap = get_keymap (startmap, 1, 1);
|
|
3054 if (NILP (prefix))
|
|
3055 prefix = make_vector (0, Qnil);
|
|
3056 else if (!VECTORP (prefix) || STRINGP (prefix))
|
|
3057 {
|
|
3058 prefix = wrong_type_argument (Qarrayp, prefix);
|
|
3059 goto retry;
|
|
3060 }
|
|
3061 else
|
|
3062 {
|
|
3063 int len = XINT (Flength (prefix));
|
|
3064 Lisp_Object def = Flookup_key (startmap, prefix, Qnil);
|
|
3065 Lisp_Object p;
|
|
3066 int iii;
|
|
3067 struct gcpro ngcpro1;
|
|
3068
|
|
3069 def = get_keymap (def, 0, 1);
|
|
3070 if (!KEYMAPP (def))
|
|
3071 goto RETURN;
|
|
3072
|
|
3073 startmap = def;
|
|
3074 p = make_vector (len, Qnil);
|
|
3075 NGCPRO1 (p);
|
|
3076 for (iii = 0; iii < len; iii++)
|
|
3077 {
|
|
3078 struct key_data key;
|
|
3079 define_key_parser (Faref (prefix, make_int (iii)), &key);
|
|
3080 vector_data (XVECTOR (p))[iii] = make_key_description (&key, 1);
|
|
3081 }
|
|
3082 NUNGCPRO;
|
|
3083 prefix = p;
|
|
3084 }
|
|
3085
|
|
3086 accessible_keymaps = list1 (Fcons (prefix, startmap));
|
|
3087
|
|
3088 /* For each map in the list maps,
|
|
3089 look at any other maps it points to
|
|
3090 and stick them at the end if they are not already in the list */
|
|
3091
|
|
3092 for (c.tail = accessible_keymaps;
|
|
3093 !NILP (c.tail);
|
|
3094 c.tail = XCDR (c.tail))
|
|
3095 {
|
|
3096 Lisp_Object thismap = Fcdr (Fcar (c.tail));
|
|
3097 CHECK_KEYMAP (thismap);
|
|
3098 traverse_keymaps (thismap, Qnil,
|
|
3099 accessible_keymaps_keymap_mapper, &c);
|
|
3100 }
|
|
3101 RETURN:
|
|
3102 UNGCPRO;
|
|
3103 return (accessible_keymaps);
|
|
3104 }
|
|
3105
|
|
3106
|
|
3107
|
|
3108 /************************************************************************/
|
|
3109 /* Pretty descriptions of key sequences */
|
|
3110 /************************************************************************/
|
|
3111
|
20
|
3112 DEFUN ("key-description", Fkey_description, 1, 1, 0, /*
|
0
|
3113 Return a pretty description of key-sequence KEYS.
|
|
3114 Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"
|
|
3115 spaces are put between sequence elements, etc.
|
20
|
3116 */
|
|
3117 (keys))
|
0
|
3118 {
|
70
|
3119 if (CHAR_OR_CHAR_INTP (keys) || CONSP (keys) || SYMBOLP (keys)
|
|
3120 || EVENTP (keys))
|
0
|
3121 {
|
|
3122 return Fsingle_key_description (keys);
|
|
3123 }
|
|
3124 else if (VECTORP (keys) ||
|
|
3125 STRINGP (keys))
|
|
3126 {
|
|
3127 Lisp_Object string = Qnil;
|
|
3128 /* Lisp_Object sep = Qnil; */
|
|
3129 int size = XINT (Flength (keys));
|
|
3130 int i;
|
|
3131
|
|
3132 for (i = 0; i < size; i++)
|
|
3133 {
|
|
3134 Lisp_Object s2 = Fsingle_key_description
|
|
3135 (((STRINGP (keys))
|
|
3136 ? make_char (string_char (XSTRING (keys), i))
|
|
3137 : vector_data (XVECTOR (keys))[i]));
|
|
3138
|
|
3139 if (i == 0)
|
|
3140 string = s2;
|
|
3141 else
|
|
3142 {
|
|
3143 /* if (NILP (sep)) Lisp_Object sep = build_string (" ") */;
|
|
3144 string = concat2 (string, concat2 (Vsingle_space_string, s2));
|
|
3145 }
|
|
3146 }
|
|
3147 return (string);
|
|
3148 }
|
|
3149 return Fkey_description (wrong_type_argument (Qsequencep, keys));
|
|
3150 }
|
|
3151
|
20
|
3152 DEFUN ("single-key-description", Fsingle_key_description, 1, 1, 0, /*
|
0
|
3153 Return a pretty description of command character KEY.
|
|
3154 Control characters turn into C-whatever, etc.
|
|
3155 This differs from `text-char-description' in that it returns a description
|
|
3156 of a key read from the user rather than a character from a buffer.
|
20
|
3157 */
|
|
3158 (key))
|
0
|
3159 {
|
|
3160 if (SYMBOLP (key))
|
|
3161 key = Fcons (key, Qnil); /* sleaze sleaze */
|
|
3162
|
70
|
3163 if (EVENTP (key) || CHAR_OR_CHAR_INTP (key))
|
0
|
3164 {
|
|
3165 char buf [255];
|
|
3166 if (!EVENTP (key))
|
|
3167 {
|
|
3168 struct Lisp_Event event;
|
|
3169 event.event_type = empty_event;
|
|
3170 CHECK_CHAR_COERCE_INT (key);
|
|
3171 character_to_event (XCHAR (key), &event,
|
|
3172 XCONSOLE (Vselected_console), 0);
|
|
3173 format_event_object (buf, &event, 1);
|
|
3174 }
|
|
3175 else
|
|
3176 format_event_object (buf, XEVENT (key), 1);
|
|
3177 return (build_string (buf));
|
|
3178 }
|
|
3179
|
|
3180 if (CONSP (key))
|
|
3181 {
|
|
3182 char buf[255];
|
|
3183 char *bufp = buf;
|
|
3184 Lisp_Object rest;
|
|
3185 buf[0] = 0;
|
|
3186 LIST_LOOP (rest, key)
|
|
3187 {
|
|
3188 Lisp_Object keysym = XCAR (rest);
|
|
3189 if (EQ (keysym, Qcontrol)) strcpy (bufp, "C-"), bufp += 2;
|
|
3190 else if (EQ (keysym, Qctrl)) strcpy (bufp, "C-"), bufp += 2;
|
|
3191 else if (EQ (keysym, Qmeta)) strcpy (bufp, "M-"), bufp += 2;
|
|
3192 else if (EQ (keysym, Qsuper)) strcpy (bufp, "S-"), bufp += 2;
|
|
3193 else if (EQ (keysym, Qhyper)) strcpy (bufp, "H-"), bufp += 2;
|
|
3194 else if (EQ (keysym, Qalt)) strcpy (bufp, "A-"), bufp += 2;
|
|
3195 else if (EQ (keysym, Qshift)) strcpy (bufp, "Sh-"), bufp += 3;
|
70
|
3196 else if (CHAR_OR_CHAR_INTP (keysym))
|
|
3197 {
|
|
3198 bufp += set_charptr_emchar ((Bufbyte *) bufp,
|
|
3199 XCHAR_OR_CHAR_INT (keysym));
|
|
3200 *bufp = 0;
|
|
3201 }
|
0
|
3202 else
|
|
3203 {
|
|
3204 CHECK_SYMBOL (keysym);
|
|
3205 #if 0 /* This is bogus */
|
|
3206 if (EQ (keysym, QKlinefeed)) strcpy (bufp, "LFD");
|
|
3207 else if (EQ (keysym, QKtab)) strcpy (bufp, "TAB");
|
|
3208 else if (EQ (keysym, QKreturn)) strcpy (bufp, "RET");
|
|
3209 else if (EQ (keysym, QKescape)) strcpy (bufp, "ESC");
|
|
3210 else if (EQ (keysym, QKdelete)) strcpy (bufp, "DEL");
|
|
3211 else if (EQ (keysym, QKspace)) strcpy (bufp, "SPC");
|
|
3212 else if (EQ (keysym, QKbackspace)) strcpy (bufp, "BS");
|
|
3213 else
|
|
3214 #endif
|
|
3215 strcpy (bufp, (char *) string_data (XSYMBOL (keysym)->name));
|
|
3216 if (!NILP (XCDR (rest)))
|
|
3217 signal_simple_error ("invalid key description",
|
|
3218 key);
|
|
3219 }
|
|
3220 }
|
|
3221 return build_string (buf);
|
|
3222 }
|
|
3223 return Fsingle_key_description
|
|
3224 (wrong_type_argument (intern ("char-or-event-p"), key));
|
|
3225 }
|
|
3226
|
20
|
3227 DEFUN ("text-char-description", Ftext_char_description, 1, 1, 0, /*
|
0
|
3228 Return a pretty description of file-character CHR.
|
|
3229 Unprintable characters turn into \"^char\" or \\NNN, depending on the value
|
|
3230 of the `ctl-arrow' variable.
|
|
3231 This differs from `single-key-description' in that it returns a description
|
|
3232 of a character from a buffer rather than a key read from the user.
|
20
|
3233 */
|
|
3234 (chr))
|
0
|
3235 {
|
|
3236 Bufbyte buf[200];
|
|
3237 Bufbyte *p;
|
|
3238 unsigned int c;
|
|
3239 Lisp_Object ctl_arrow = current_buffer->ctl_arrow;
|
|
3240 int ctl_p = !NILP (ctl_arrow);
|
70
|
3241 Emchar printable_min = (CHAR_OR_CHAR_INTP (ctl_arrow)
|
|
3242 ? XCHAR_OR_CHAR_INT (ctl_arrow)
|
|
3243 : ((EQ (ctl_arrow, Qt) || NILP (ctl_arrow))
|
|
3244 ? 256 : 160));
|
0
|
3245
|
|
3246 if (EVENTP (chr))
|
|
3247 {
|
|
3248 Lisp_Object ch = Fevent_to_character (chr, Qnil, Qnil, Qt);
|
|
3249 if (NILP (ch))
|
|
3250 return
|
|
3251 signal_simple_continuable_error
|
|
3252 ("character has no ASCII equivalent", Fcopy_event (chr, Qnil));
|
|
3253 chr = ch;
|
|
3254 }
|
|
3255
|
|
3256 CHECK_CHAR_COERCE_INT (chr);
|
|
3257
|
|
3258 c = XCHAR (chr);
|
|
3259 p = buf;
|
|
3260
|
|
3261 if (c >= printable_min)
|
|
3262 {
|
|
3263 p += set_charptr_emchar (p, c);
|
|
3264 }
|
|
3265 else if (c < 040 && ctl_p)
|
|
3266 {
|
|
3267 *p++ = '^';
|
|
3268 *p++ = c + 64; /* 'A' - 1 */
|
|
3269 }
|
|
3270 else if (c == 0177)
|
|
3271 {
|
|
3272 *p++ = '^';
|
|
3273 *p++ = '?';
|
|
3274 }
|
|
3275 else if (c >= 0200 || c < 040)
|
|
3276 {
|
|
3277 *p++ = '\\';
|
70
|
3278 #ifdef MULE
|
|
3279 /* !!#### This syntax is not readable. It will
|
|
3280 be interpreted as a 3-digit octal number rather
|
|
3281 than a 7-digit octal number. */
|
|
3282 if (c >= 0400)
|
|
3283 {
|
|
3284 *p++ = '0' + ((c & 07000000) >> 18);
|
|
3285 *p++ = '0' + ((c & 0700000) >> 15);
|
|
3286 *p++ = '0' + ((c & 070000) >> 12);
|
|
3287 *p++ = '0' + ((c & 07000) >> 9);
|
|
3288 }
|
|
3289 #endif
|
0
|
3290 *p++ = '0' + ((c & 0700) >> 6);
|
|
3291 *p++ = '0' + ((c & 0070) >> 3);
|
|
3292 *p++ = '0' + ((c & 0007));
|
|
3293 }
|
|
3294 else
|
|
3295 {
|
|
3296 p += set_charptr_emchar (p, c);
|
|
3297 }
|
|
3298
|
|
3299 *p = 0;
|
|
3300 return build_string ((char *) buf);
|
|
3301 }
|
|
3302
|
|
3303
|
|
3304 /************************************************************************/
|
|
3305 /* where-is (mapping bindings to keys) */
|
|
3306 /************************************************************************/
|
|
3307
|
|
3308 static Lisp_Object
|
|
3309 where_is_internal (Lisp_Object definition, Lisp_Object *maps, int nmaps,
|
|
3310 Lisp_Object firstonly, char *target_buffer);
|
|
3311
|
20
|
3312 DEFUN ("where-is-internal", Fwhere_is_internal, 1, 5, 0, /*
|
0
|
3313 Return list of keys that invoke DEFINITION in KEYMAPS.
|
|
3314 KEYMAPS can be either a keymap (meaning search in that keymap and the
|
|
3315 current global keymap) or a list of keymaps (meaning search in exactly
|
|
3316 those keymaps and no others). If KEYMAPS is nil, search in the currently
|
|
3317 applicable maps for EVENT-OR-KEYS (this is equivalent to specifying
|
|
3318 `(current-keymaps EVENT-OR-KEYS)' as the argument to KEYMAPS).
|
|
3319
|
|
3320 If optional 3rd arg FIRSTONLY is non-nil, return a vector representing
|
|
3321 the first key sequence found, rather than a list of all possible key
|
|
3322 sequences.
|
|
3323
|
|
3324 If optional 4th arg NOINDIRECT is non-nil, don't follow indirections
|
|
3325 to other keymaps or slots. This makes it possible to search for an
|
|
3326 indirect definition itself.
|
20
|
3327 */
|
|
3328 (definition, keymaps, firstonly, noindirect, event_or_keys))
|
0
|
3329 {
|
|
3330 /* This function can GC */
|
|
3331 Lisp_Object maps[100];
|
|
3332 Lisp_Object *gubbish = maps;
|
|
3333 int nmaps;
|
|
3334
|
|
3335 /* Get keymaps as an array */
|
|
3336 if (NILP (keymaps))
|
|
3337 {
|
|
3338 nmaps = get_relevant_keymaps (event_or_keys, countof (maps),
|
|
3339 gubbish);
|
|
3340 if (nmaps > countof (maps))
|
|
3341 {
|
|
3342 gubbish = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
|
|
3343 nmaps = get_relevant_keymaps (event_or_keys, nmaps, gubbish);
|
|
3344 }
|
|
3345 }
|
|
3346 else if (CONSP (keymaps))
|
|
3347 {
|
|
3348 Lisp_Object rest;
|
|
3349 int i;
|
|
3350
|
|
3351 nmaps = XINT (Flength (keymaps));
|
|
3352 if (nmaps > countof (maps))
|
|
3353 {
|
|
3354 gubbish = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
|
|
3355 }
|
|
3356 for (rest = keymaps, i = 0; !NILP (rest);
|
|
3357 rest = XCDR (keymaps), i++)
|
|
3358 {
|
|
3359 gubbish[i] = get_keymap (XCAR (keymaps), 1, 1);
|
|
3360 }
|
|
3361 }
|
|
3362 else
|
|
3363 {
|
|
3364 nmaps = 1;
|
|
3365 gubbish[0] = get_keymap (keymaps, 1, 1);
|
|
3366 if (!EQ (gubbish[0], Vcurrent_global_map))
|
|
3367 {
|
|
3368 gubbish[1] = Vcurrent_global_map;
|
|
3369 nmaps++;
|
|
3370 }
|
|
3371 }
|
|
3372
|
|
3373 return where_is_internal (definition, gubbish, nmaps, firstonly, 0);
|
|
3374 }
|
|
3375
|
|
3376 /* This function is like
|
|
3377 (key-description (where-is-internal definition nil t))
|
|
3378 except that it writes its output into a (char *) buffer that you
|
|
3379 provide; it doesn't cons (or allocate memory) at all, so it's
|
|
3380 very fast. This is used by menubar.c.
|
|
3381 */
|
|
3382 void
|
|
3383 where_is_to_char (Lisp_Object definition, char *buffer)
|
|
3384 {
|
|
3385 /* This function can GC */
|
|
3386 Lisp_Object maps[100];
|
|
3387 Lisp_Object *gubbish = maps;
|
|
3388 int nmaps;
|
|
3389
|
|
3390 /* Get keymaps as an array */
|
|
3391 nmaps = get_relevant_keymaps (Qnil, countof (maps), gubbish);
|
|
3392 if (nmaps > countof (maps))
|
|
3393 {
|
|
3394 gubbish = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
|
|
3395 nmaps = get_relevant_keymaps (Qnil, nmaps, gubbish);
|
|
3396 }
|
|
3397
|
|
3398 buffer[0] = 0;
|
|
3399 where_is_internal (definition, maps, nmaps, Qt, buffer);
|
|
3400 }
|
|
3401
|
|
3402
|
|
3403 static Lisp_Object
|
|
3404 raw_keys_to_keys (struct key_data *keys, int count)
|
|
3405 {
|
|
3406 Lisp_Object result = make_vector (count, Qnil);
|
|
3407 while (count--)
|
|
3408 vector_data (XVECTOR (result)) [count] =
|
|
3409 make_key_description (&(keys[count]), 1);
|
|
3410 return (result);
|
|
3411 }
|
|
3412
|
|
3413
|
|
3414 static void
|
|
3415 format_raw_keys (struct key_data *keys, int count, char *buf)
|
|
3416 {
|
|
3417 int i;
|
|
3418 struct Lisp_Event event;
|
|
3419 event.event_type = key_press_event;
|
|
3420 event.channel = Vselected_console;
|
|
3421 for (i = 0; i < count; i++)
|
|
3422 {
|
|
3423 event.event.key.keysym = keys[i].keysym;
|
|
3424 event.event.key.modifiers = keys[i].modifiers;
|
|
3425 format_event_object (buf, &event, 1);
|
|
3426 buf += strlen (buf);
|
|
3427 if (i < count-1)
|
|
3428 buf[0] = ' ', buf++;
|
|
3429 }
|
|
3430 }
|
|
3431
|
|
3432
|
|
3433 /* definition is the thing to look for.
|
|
3434 map is a keymap.
|
|
3435 shadow is an array of shadow_count keymaps; if there is a different
|
|
3436 binding in any of the keymaps of a key that we are considering
|
|
3437 returning, then we reconsider.
|
|
3438 firstonly means give up after finding the first match;
|
|
3439 keys_so_far and modifiers_so_far describe which map we're looking in;
|
|
3440 If we're in the "meta" submap of the map that "C-x 4" is bound to,
|
|
3441 then keys_so_far will be {(control x), \4}, and modifiers_so_far
|
|
3442 will be MOD_META. That is, keys_so_far is the chain of keys that we
|
|
3443 have followed, and modifiers_so_far_so_far is the bits (partial keys)
|
|
3444 beyond that.
|
|
3445
|
|
3446 (keys_so_far is a global buffer and the keys_count arg says how much
|
|
3447 of it we're currently interested in.)
|
|
3448
|
|
3449 If target_buffer is provided, then we write a key-description into it,
|
|
3450 to avoid consing a string. This only works with firstonly on.
|
|
3451 */
|
|
3452
|
|
3453 struct where_is_closure
|
|
3454 {
|
|
3455 Lisp_Object definition;
|
|
3456 Lisp_Object *shadow;
|
|
3457 int shadow_count;
|
|
3458 int firstonly;
|
|
3459 int keys_count;
|
|
3460 unsigned int modifiers_so_far;
|
|
3461 char *target_buffer;
|
|
3462 struct key_data *keys_so_far;
|
|
3463 int keys_so_far_total_size;
|
|
3464 int keys_so_far_malloced;
|
|
3465 };
|
|
3466
|
|
3467 static Lisp_Object where_is_recursive_mapper (Lisp_Object map, void *arg);
|
|
3468
|
|
3469 static Lisp_Object
|
|
3470 where_is_recursive_mapper (Lisp_Object map, void *arg)
|
|
3471 {
|
|
3472 /* This function can GC */
|
|
3473 struct where_is_closure *c = arg;
|
|
3474 Lisp_Object definition = c->definition;
|
|
3475 CONST int firstonly = c->firstonly;
|
|
3476 CONST unsigned int keys_count = c->keys_count;
|
|
3477 CONST unsigned int modifiers_so_far = c->modifiers_so_far;
|
|
3478 char *target_buffer = c->target_buffer;
|
|
3479 Lisp_Object keys = Fgethash (definition,
|
|
3480 XKEYMAP (map)->inverse_table,
|
|
3481 Qnil);
|
|
3482 Lisp_Object submaps;
|
|
3483 Lisp_Object result = Qnil;
|
|
3484
|
|
3485 if (!NILP (keys))
|
|
3486 {
|
|
3487 /* One or more keys in this map match the definition we're looking
|
|
3488 for. Verify that these bindings aren't shadowed by other bindings
|
|
3489 in the shadow maps. Either nil or number as value from
|
|
3490 raw_lookup_key() means undefined.
|
|
3491 */
|
|
3492 struct key_data *so_far = c->keys_so_far;
|
|
3493
|
|
3494 for (;;) /* loop over all keys that match */
|
|
3495 {
|
|
3496 Lisp_Object k = ((CONSP (keys)) ? XCAR (keys) : keys);
|
|
3497 int i;
|
|
3498
|
|
3499 so_far [keys_count].keysym = k;
|
|
3500 so_far [keys_count].modifiers = modifiers_so_far;
|
|
3501
|
|
3502 /* now loop over all shadow maps */
|
|
3503 for (i = 0; i < c->shadow_count; i++)
|
|
3504 {
|
|
3505 Lisp_Object shadowed = raw_lookup_key (c->shadow[i],
|
|
3506 so_far,
|
|
3507 keys_count + 1,
|
|
3508 0, 1);
|
|
3509
|
|
3510 if (NILP (shadowed) || CHARP (shadowed) ||
|
|
3511 EQ (shadowed, definition))
|
|
3512 continue; /* we passed this test; it's not shadowed here. */
|
|
3513 else
|
|
3514 /* ignore this key binding, since it actually has a
|
|
3515 different binding in a shadowing map */
|
|
3516 goto c_doesnt_have_proper_loop_exit_statements;
|
|
3517 }
|
|
3518
|
|
3519 /* OK, the key is for real */
|
|
3520 if (target_buffer)
|
|
3521 {
|
|
3522 if (!firstonly) abort ();
|
|
3523 format_raw_keys (so_far, keys_count + 1, target_buffer);
|
|
3524 return (make_int (1));
|
|
3525 }
|
|
3526 else if (firstonly)
|
|
3527 return raw_keys_to_keys (so_far, keys_count + 1);
|
|
3528 else
|
|
3529 result = Fcons (raw_keys_to_keys (so_far, keys_count + 1),
|
|
3530 result);
|
|
3531
|
|
3532 c_doesnt_have_proper_loop_exit_statements:
|
|
3533 /* now on to the next matching key ... */
|
|
3534 if (!CONSP (keys)) break;
|
|
3535 keys = XCDR (keys);
|
|
3536 }
|
|
3537 }
|
|
3538
|
|
3539 /* Now search the sub-keymaps of this map.
|
|
3540 If we're in "firstonly" mode and have already found one, this
|
|
3541 point is not reached. If we get one from lower down, either
|
|
3542 return it immediately (in firstonly mode) or tack it onto the
|
|
3543 end of the ones we've gotten so far.
|
|
3544 */
|
|
3545 for (submaps = keymap_submaps (map);
|
|
3546 !NILP (submaps);
|
|
3547 submaps = XCDR (submaps))
|
|
3548 {
|
|
3549 Lisp_Object key = XCAR (XCAR (submaps));
|
|
3550 Lisp_Object submap = XCDR (XCAR (submaps));
|
|
3551 unsigned int lower_modifiers;
|
|
3552 int lower_keys_count = keys_count;
|
|
3553 unsigned int bucky;
|
|
3554
|
|
3555 submap = get_keymap (submap, 0, 1);
|
|
3556
|
|
3557 if (EQ (submap, map))
|
|
3558 /* Arrgh! Some loser has introduced a loop... */
|
|
3559 continue;
|
|
3560
|
|
3561 /* If this is not a keymap, then that's probably because someone
|
|
3562 did an `fset' of a symbol that used to point to a map such that
|
|
3563 it no longer does. Sigh. Ignore this, and invalidate the cache
|
|
3564 so that it doesn't happen to us next time too.
|
|
3565 */
|
|
3566 if (NILP (submap))
|
|
3567 {
|
|
3568 XKEYMAP (map)->sub_maps_cache = Qt;
|
|
3569 continue;
|
|
3570 }
|
|
3571
|
|
3572 /* If the map is a "bucky" map, then add a bit to the
|
|
3573 modifiers_so_far list.
|
|
3574 Otherwise, add a new raw_key onto the end of keys_so_far.
|
|
3575 */
|
|
3576 bucky = MODIFIER_HASH_KEY_BITS (key);
|
|
3577 if (bucky != 0)
|
|
3578 lower_modifiers = (modifiers_so_far | bucky);
|
|
3579 else
|
|
3580 {
|
|
3581 struct key_data *so_far = c->keys_so_far;
|
|
3582 lower_modifiers = 0;
|
|
3583 so_far [lower_keys_count].keysym = key;
|
|
3584 so_far [lower_keys_count].modifiers = modifiers_so_far;
|
|
3585 lower_keys_count++;
|
|
3586 }
|
|
3587
|
|
3588 if (lower_keys_count >= c->keys_so_far_total_size)
|
|
3589 {
|
|
3590 int size = lower_keys_count + 50;
|
|
3591 if (! c->keys_so_far_malloced)
|
|
3592 {
|
|
3593 struct key_data *new = xmalloc (size * sizeof (struct key_data));
|
|
3594 memcpy ((void *)new, (const void *)c->keys_so_far,
|
|
3595 c->keys_so_far_total_size * sizeof (struct key_data));
|
|
3596 }
|
|
3597 else
|
|
3598 c->keys_so_far = xrealloc (c->keys_so_far,
|
|
3599 size * sizeof (struct key_data));
|
|
3600
|
|
3601 c->keys_so_far_total_size = size;
|
|
3602 c->keys_so_far_malloced = 1;
|
|
3603 }
|
|
3604
|
|
3605 {
|
|
3606 Lisp_Object lower;
|
|
3607
|
|
3608 c->keys_count = lower_keys_count;
|
|
3609 c->modifiers_so_far = lower_modifiers;
|
|
3610
|
|
3611 lower = traverse_keymaps (submap, Qnil, where_is_recursive_mapper,
|
|
3612 c);
|
|
3613 c->keys_count = keys_count;
|
|
3614 c->modifiers_so_far = modifiers_so_far;
|
|
3615
|
|
3616 if (!firstonly)
|
|
3617 result = nconc2 (lower, result);
|
|
3618 else if (!NILP (lower))
|
|
3619 return (lower);
|
|
3620 }
|
|
3621 }
|
|
3622 return (result);
|
|
3623 }
|
|
3624
|
|
3625
|
|
3626 static Lisp_Object
|
|
3627 where_is_internal (Lisp_Object definition, Lisp_Object *maps, int nmaps,
|
|
3628 Lisp_Object firstonly, char *target_buffer)
|
|
3629 {
|
|
3630 /* This function can GC */
|
|
3631 Lisp_Object result = Qnil;
|
|
3632 int i;
|
|
3633 struct key_data raw[20];
|
|
3634 struct where_is_closure c;
|
|
3635
|
|
3636 c.definition = definition;
|
|
3637 c.shadow = maps;
|
|
3638 c.firstonly = !NILP (firstonly);
|
|
3639 c.target_buffer = target_buffer;
|
|
3640 c.keys_so_far = raw;
|
|
3641 c.keys_so_far_total_size = countof (raw);
|
|
3642 c.keys_so_far_malloced = 0;
|
|
3643
|
|
3644 /* Loop over each of the maps, accumulating the keys found.
|
|
3645 For each map searched, all previous maps shadow this one
|
|
3646 so that bogus keys aren't listed. */
|
|
3647 for (i = 0; i < nmaps; i++)
|
|
3648 {
|
|
3649 Lisp_Object this_result;
|
|
3650 c.shadow_count = i;
|
|
3651 /* Reset the things set in each iteration */
|
|
3652 c.keys_count = 0;
|
|
3653 c.modifiers_so_far = 0;
|
|
3654
|
|
3655 this_result = traverse_keymaps (maps[i], Qnil, where_is_recursive_mapper,
|
|
3656 &c);
|
|
3657 if (!NILP (firstonly))
|
|
3658 {
|
|
3659 result = this_result;
|
|
3660 if (!NILP (result))
|
|
3661 break;
|
|
3662 }
|
|
3663 else
|
|
3664 result = nconc2 (this_result, result);
|
|
3665 }
|
|
3666
|
|
3667 if (NILP (firstonly))
|
|
3668 result = Fnreverse (result);
|
|
3669
|
|
3670 if (c.keys_so_far_malloced)
|
|
3671 xfree (c.keys_so_far);
|
|
3672 return (result);
|
|
3673 }
|
|
3674
|
|
3675
|
|
3676 /************************************************************************/
|
|
3677 /* Describing keymaps */
|
|
3678 /************************************************************************/
|
|
3679
|
20
|
3680 DEFUN ("describe-bindings-internal", Fdescribe_bindings_internal, 1, 5, 0, /*
|
0
|
3681 Insert a list of all defined keys and their definitions in MAP.
|
|
3682 Optional second argument ALL says whether to include even \"uninteresting\"
|
|
3683 definitions (ie symbols with a non-nil `suppress-keymap' property.
|
|
3684 Third argument SHADOW is a list of keymaps whose bindings shadow those
|
|
3685 of map; if a binding is present in any shadowing map, it is not printed.
|
|
3686 Fourth argument PREFIX, if non-nil, should be a key sequence;
|
|
3687 only bindings which start with that key sequence will be printed.
|
|
3688 Fifth argument MOUSE-ONLY-P says to only print bindings for mouse clicks.
|
20
|
3689 */
|
|
3690 (map, all, shadow, prefix, mouse_only_p))
|
0
|
3691 {
|
|
3692 /* This function can GC */
|
|
3693 describe_map_tree (map, NILP (all), shadow, prefix,
|
|
3694 !NILP (mouse_only_p));
|
|
3695 return (Qnil);
|
|
3696 }
|
|
3697
|
|
3698
|
|
3699 /* Insert a desription of the key bindings in STARTMAP,
|
|
3700 followed by those of all maps reachable through STARTMAP.
|
|
3701 If PARTIAL is nonzero, omit certain "uninteresting" commands
|
|
3702 (such as `undefined').
|
|
3703 If SHADOW is non-nil, it is a list of other maps;
|
|
3704 don't mention keys which would be shadowed by any of them
|
|
3705 If PREFIX is non-nil, only list bindings which start with those keys
|
|
3706 */
|
|
3707
|
|
3708 void
|
|
3709 describe_map_tree (Lisp_Object startmap, int partial, Lisp_Object shadow,
|
|
3710 Lisp_Object prefix, int mice_only_p)
|
|
3711 {
|
|
3712 /* This function can GC */
|
|
3713 Lisp_Object maps = Qnil;
|
|
3714 struct gcpro gcpro1, gcpro2; /* get_keymap may autoload */
|
|
3715 GCPRO2 (maps, shadow);
|
|
3716
|
|
3717 maps = Faccessible_keymaps (startmap, prefix);
|
|
3718
|
|
3719 for (; !NILP (maps); maps = Fcdr (maps))
|
|
3720 {
|
|
3721 Lisp_Object sub_shadow = Qnil;
|
|
3722 Lisp_Object elt = Fcar (maps);
|
|
3723 Lisp_Object tail = shadow;
|
|
3724 int no_prefix = (VECTORP (Fcar (elt))
|
|
3725 && XINT (Flength (Fcar (elt))) == 0);
|
|
3726 struct gcpro ngcpro1, ngcpro2, ngcpro3;
|
|
3727 NGCPRO3 (sub_shadow, elt, tail);
|
|
3728
|
|
3729 for (; CONSP (tail); tail = XCDR (tail))
|
|
3730 {
|
|
3731 Lisp_Object sh = XCAR (tail);
|
|
3732
|
|
3733 /* If the sequence by which we reach this keymap is zero-length,
|
|
3734 then the shadow maps for this keymap are just SHADOW. */
|
|
3735 if (no_prefix)
|
|
3736 ;
|
|
3737 /* If the sequence by which we reach this keymap actually has
|
|
3738 some elements, then the sequence's definition in SHADOW is
|
|
3739 what we should use. */
|
|
3740 else
|
|
3741 {
|
|
3742 sh = Flookup_key (sh, Fcar (elt), Qt);
|
|
3743 if (CHARP (sh))
|
|
3744 sh = Qnil;
|
|
3745 }
|
|
3746
|
|
3747 if (!NILP (sh))
|
|
3748 {
|
|
3749 Lisp_Object shm = get_keymap (sh, 0, 1);
|
|
3750 if (!KEYMAPP (shm))
|
|
3751 /* If sh is not nil and not a keymap, it completely shadows
|
|
3752 this map, so don't describe this map at all. */
|
|
3753 goto SKIP;
|
|
3754 sub_shadow = Fcons (shm, sub_shadow);
|
|
3755 }
|
|
3756 }
|
|
3757
|
|
3758 {
|
|
3759 /* Describe the contents of map MAP, assuming that this map
|
|
3760 itself is reached by the sequence of prefix keys KEYS (a vector).
|
|
3761 PARTIAL and SHADOW are as in `describe_map_tree'. */
|
|
3762 Lisp_Object keysdesc
|
|
3763 = ((!no_prefix)
|
|
3764 ? concat2 (Fkey_description (Fcar (elt)), Vsingle_space_string)
|
|
3765 : Qnil);
|
|
3766 describe_map (Fcdr (elt), keysdesc,
|
|
3767 describe_command,
|
|
3768 partial,
|
|
3769 sub_shadow,
|
|
3770 mice_only_p);
|
|
3771 }
|
|
3772 SKIP:
|
|
3773 NUNGCPRO;
|
|
3774 }
|
|
3775 UNGCPRO;
|
|
3776 }
|
|
3777
|
|
3778
|
|
3779 static void
|
|
3780 describe_command (Lisp_Object definition)
|
|
3781 {
|
|
3782 /* This function can GC */
|
|
3783 Lisp_Object buffer;
|
|
3784 int keymapp = !NILP (Fkeymapp (definition));
|
|
3785 struct gcpro gcpro1, gcpro2;
|
|
3786 GCPRO2 (definition, buffer);
|
|
3787
|
|
3788 XSETBUFFER (buffer, current_buffer);
|
|
3789 Findent_to (make_int (16), make_int (3), buffer);
|
|
3790 if (keymapp)
|
|
3791 buffer_insert_c_string (XBUFFER (buffer), "<< ");
|
|
3792
|
|
3793 if (SYMBOLP (definition))
|
|
3794 {
|
|
3795 buffer_insert1 (XBUFFER (buffer), Fsymbol_name (definition));
|
|
3796 }
|
|
3797 else if (STRINGP (definition) || VECTORP (definition))
|
|
3798 {
|
|
3799 buffer_insert_c_string (XBUFFER (buffer), "Kbd Macro: ");
|
|
3800 buffer_insert1 (XBUFFER (buffer), Fkey_description (definition));
|
|
3801 }
|
|
3802 else if (COMPILED_FUNCTIONP (definition))
|
|
3803 buffer_insert_c_string (XBUFFER (buffer), "Anonymous Compiled Function");
|
|
3804 else if (CONSP (definition) && EQ (XCAR (definition), Qlambda))
|
|
3805 buffer_insert_c_string (XBUFFER (buffer), "Anonymous Lambda");
|
|
3806 else if (KEYMAPP (definition))
|
|
3807 {
|
|
3808 Lisp_Object name = XKEYMAP (definition)->name;
|
|
3809 if (STRINGP (name) || (SYMBOLP (name) && !NILP (name)))
|
|
3810 {
|
|
3811 buffer_insert_c_string (XBUFFER (buffer), "Prefix command ");
|
|
3812 if (SYMBOLP (name)
|
|
3813 && EQ (find_symbol_value (name), definition))
|
|
3814 buffer_insert1 (XBUFFER (buffer), Fsymbol_name (name));
|
|
3815 else
|
|
3816 {
|
|
3817 buffer_insert1 (XBUFFER (buffer), Fprin1_to_string (name, Qnil));
|
|
3818 }
|
|
3819 }
|
|
3820 else
|
|
3821 buffer_insert_c_string (XBUFFER (buffer), "Prefix Command");
|
|
3822 }
|
|
3823 else
|
|
3824 buffer_insert_c_string (XBUFFER (buffer), "??");
|
|
3825
|
|
3826 if (keymapp)
|
|
3827 buffer_insert_c_string (XBUFFER (buffer), " >>");
|
|
3828 buffer_insert_c_string (XBUFFER (buffer), "\n");
|
|
3829 UNGCPRO;
|
|
3830 }
|
|
3831
|
|
3832 struct describe_map_closure
|
|
3833 {
|
|
3834 Lisp_Object *list; /* pointer to the list to update */
|
|
3835 Lisp_Object partial; /* whether to ignore suppressed commands */
|
|
3836 Lisp_Object shadow; /* list of maps shadowing this one */
|
|
3837 Lisp_Object self; /* this map */
|
|
3838 Lisp_Object self_root; /* this map, or some map that has this map as
|
|
3839 a parent. this is the base of the tree */
|
|
3840 int mice_only_p; /* whether we are to display only button bindings */
|
|
3841 };
|
|
3842
|
|
3843 struct describe_map_shadow_closure
|
|
3844 {
|
|
3845 CONST struct key_data *raw_key;
|
|
3846 Lisp_Object self;
|
|
3847 };
|
|
3848
|
|
3849 static Lisp_Object
|
|
3850 describe_map_mapper_shadow_search (Lisp_Object map, void *arg)
|
|
3851 {
|
|
3852 struct describe_map_shadow_closure *c = arg;
|
|
3853
|
|
3854 if (EQ (map, c->self))
|
|
3855 return (Qzero); /* Not shadowed; terminate search */
|
|
3856 else if (!NILP (keymap_lookup_directly (map,
|
|
3857 c->raw_key->keysym,
|
|
3858 c->raw_key->modifiers)))
|
|
3859 return (Qt);
|
|
3860 else
|
|
3861 return (Qnil);
|
|
3862 }
|
|
3863
|
|
3864
|
|
3865 static Lisp_Object
|
|
3866 keymap_lookup_inherited_mapper (Lisp_Object km, void *arg)
|
|
3867 {
|
|
3868 struct key_data *k = arg;
|
|
3869 return (keymap_lookup_directly (km, k->keysym, k->modifiers));
|
|
3870 }
|
|
3871
|
|
3872
|
|
3873 static void
|
|
3874 describe_map_mapper (CONST struct key_data *key,
|
|
3875 Lisp_Object binding,
|
|
3876 void *describe_map_closure)
|
|
3877 {
|
|
3878 /* This function can GC */
|
|
3879 struct describe_map_closure *closure = describe_map_closure;
|
|
3880 Lisp_Object keysym = key->keysym;
|
|
3881 unsigned int modifiers = key->modifiers;
|
|
3882
|
|
3883 /* Dont mention suppressed commands. */
|
|
3884 if (SYMBOLP (binding)
|
|
3885 && !NILP (closure->partial)
|
|
3886 && !NILP (Fget (binding, closure->partial, Qnil)))
|
|
3887 return;
|
|
3888
|
|
3889 /* If we're only supposed to display mouse bindings and this isn't one,
|
|
3890 then bug out. */
|
|
3891 if (closure->mice_only_p &&
|
|
3892 (! (EQ (keysym, Qbutton0) || EQ (keysym, Qbutton1)
|
|
3893 || EQ (keysym, Qbutton2) || EQ (keysym, Qbutton3)
|
|
3894 || EQ (keysym, Qbutton4) || EQ (keysym, Qbutton5)
|
|
3895 || EQ (keysym, Qbutton6) || EQ (keysym, Qbutton7))))
|
|
3896 return;
|
|
3897
|
|
3898 /* If this command in this map is shadowed by some other map, ignore it. */
|
|
3899 {
|
|
3900 Lisp_Object tail;
|
|
3901
|
|
3902 for (tail = closure->shadow; CONSP (tail); tail = XCDR (tail))
|
|
3903 {
|
|
3904 QUIT;
|
|
3905 if (!NILP (traverse_keymaps (XCAR (tail), Qnil,
|
|
3906 keymap_lookup_inherited_mapper,
|
|
3907 /* Cast to discard `const' */
|
|
3908 (void *)key)))
|
|
3909 return;
|
|
3910 }
|
|
3911 }
|
|
3912
|
|
3913 /* If this key is in some map of which this map is a parent, then ignore
|
|
3914 it (in that case, it has been shadowed).
|
|
3915 */
|
|
3916 {
|
|
3917 Lisp_Object sh;
|
|
3918 struct describe_map_shadow_closure c;
|
|
3919 c.raw_key = key;
|
|
3920 c.self = closure->self;
|
|
3921
|
|
3922 sh = traverse_keymaps (closure->self_root, Qnil,
|
|
3923 describe_map_mapper_shadow_search, &c);
|
|
3924 if (!NILP (sh) && !ZEROP (sh))
|
|
3925 return;
|
|
3926 }
|
|
3927
|
|
3928 /* Otherwise add it to the list to be sorted. */
|
|
3929 *(closure->list) = Fcons (Fcons (Fcons (keysym, make_int (modifiers)),
|
|
3930 binding),
|
|
3931 *(closure->list));
|
|
3932 }
|
|
3933
|
|
3934
|
|
3935 static int
|
|
3936 describe_map_sort_predicate (Lisp_Object obj1, Lisp_Object obj2,
|
|
3937 Lisp_Object pred)
|
|
3938 {
|
|
3939 /* obj1 and obj2 are conses of the form
|
|
3940 ( ( <keysym> . <modifiers> ) . <binding> )
|
|
3941 keysym and modifiers are used, binding is ignored.
|
|
3942 */
|
|
3943 unsigned int bit1, bit2;
|
|
3944 obj1 = XCAR (obj1);
|
|
3945 obj2 = XCAR (obj2);
|
|
3946 bit1 = XINT (XCDR (obj1));
|
|
3947 bit2 = XINT (XCDR (obj2));
|
|
3948 if (bit1 != bit2)
|
|
3949 return ((bit1 < bit2) ? 1 : -1);
|
|
3950 else
|
|
3951 return map_keymap_sort_predicate (obj1, obj2, pred);
|
|
3952 }
|
|
3953
|
|
3954 /* Elide 2 or more consecutive numeric keysyms bound to the same thing,
|
|
3955 or 2 or more symbolic keysyms that are bound to the same thing and
|
|
3956 have consecutive character-set-properties.
|
|
3957 */
|
|
3958 static int
|
|
3959 elide_next_two_p (Lisp_Object list)
|
|
3960 {
|
|
3961 Lisp_Object s1, s2;
|
|
3962
|
|
3963 if (NILP (XCDR (list)))
|
|
3964 return 0;
|
|
3965
|
|
3966 /* next two bindings differ */
|
|
3967 if (!EQ (XCDR (XCAR (list)),
|
|
3968 XCDR (XCAR (XCDR (list)))))
|
|
3969 return 0;
|
|
3970
|
|
3971 /* next two modifier-sets differ */
|
|
3972 if (!EQ (XCDR (XCAR (XCAR (list))),
|
|
3973 XCDR (XCAR (XCAR (XCDR (list))))))
|
|
3974 return 0;
|
|
3975
|
|
3976 s1 = XCAR (XCAR (XCAR (list)));
|
|
3977 s2 = XCAR (XCAR (XCAR (XCDR (list))));
|
|
3978
|
|
3979 if (SYMBOLP (s1))
|
|
3980 {
|
|
3981 Lisp_Object code = Fget (s1, Vcharacter_set_property, Qnil);
|
70
|
3982 if (CHAR_OR_CHAR_INTP (code))
|
|
3983 {
|
|
3984 s1 = code;
|
|
3985 CHECK_CHAR_COERCE_INT (s1);
|
|
3986 }
|
0
|
3987 else return 0;
|
|
3988 }
|
|
3989 if (SYMBOLP (s2))
|
|
3990 {
|
|
3991 Lisp_Object code = Fget (s2, Vcharacter_set_property, Qnil);
|
70
|
3992 if (CHAR_OR_CHAR_INTP (code))
|
|
3993 {
|
|
3994 s2 = code;
|
|
3995 CHECK_CHAR_COERCE_INT (s2);
|
|
3996 }
|
0
|
3997 else return 0;
|
|
3998 }
|
|
3999
|
|
4000 if (XCHAR (s1) == XCHAR (s2) ||
|
|
4001 XCHAR (s1) + 1 == XCHAR (s2))
|
|
4002 return 1;
|
|
4003 return 0;
|
|
4004 }
|
|
4005
|
|
4006
|
|
4007 static Lisp_Object
|
|
4008 describe_map_parent_mapper (Lisp_Object keymap, void *arg)
|
|
4009 {
|
|
4010 /* This function can GC */
|
|
4011 struct describe_map_closure *describe_map_closure = arg;
|
|
4012 describe_map_closure->self = keymap;
|
|
4013 map_keymap (XKEYMAP (keymap)->table,
|
|
4014 0, /* don't sort: we'll do it later */
|
|
4015 describe_map_mapper, describe_map_closure);
|
|
4016 return (Qnil);
|
|
4017 }
|
|
4018
|
|
4019
|
|
4020 static void
|
|
4021 describe_map (Lisp_Object keymap, Lisp_Object elt_prefix,
|
|
4022 void (*elt_describer) (Lisp_Object),
|
|
4023 int partial,
|
|
4024 Lisp_Object shadow,
|
|
4025 int mice_only_p)
|
|
4026 {
|
|
4027 /* This function can GC */
|
|
4028 struct describe_map_closure describe_map_closure;
|
|
4029 Lisp_Object list = Qnil;
|
|
4030 struct buffer *buf = current_buffer;
|
|
4031 Emchar printable_min = (CHAR_OR_CHAR_INTP (buf->ctl_arrow)
|
|
4032 ? XCHAR_OR_CHAR_INT (buf->ctl_arrow)
|
|
4033 : ((EQ (buf->ctl_arrow, Qt)
|
|
4034 || EQ (buf->ctl_arrow, Qnil))
|
|
4035 ? 256 : 160));
|
|
4036 int elided = 0;
|
|
4037 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
|
4038
|
|
4039 keymap = get_keymap (keymap, 1, 1);
|
|
4040 describe_map_closure.partial = (partial ? Qsuppress_keymap : Qnil);
|
|
4041 describe_map_closure.shadow = shadow;
|
|
4042 describe_map_closure.list = &list;
|
|
4043 describe_map_closure.self_root = keymap;
|
|
4044 describe_map_closure.mice_only_p = mice_only_p;
|
|
4045
|
|
4046 GCPRO4 (keymap, elt_prefix, shadow, list);
|
|
4047
|
|
4048 traverse_keymaps (keymap, Qnil,
|
|
4049 describe_map_parent_mapper, &describe_map_closure);
|
|
4050
|
|
4051 if (!NILP (list))
|
|
4052 {
|
|
4053 list = list_sort (list, Qnil, describe_map_sort_predicate);
|
|
4054 buffer_insert_c_string (buf, "\n");
|
|
4055 while (!NILP (list))
|
|
4056 {
|
|
4057 Lisp_Object elt = XCAR (XCAR (list));
|
|
4058 Lisp_Object keysym = XCAR (elt);
|
|
4059 unsigned int modifiers = XINT (XCDR (elt));
|
|
4060
|
|
4061 if (!NILP (elt_prefix))
|
|
4062 buffer_insert_lisp_string (buf, elt_prefix);
|
|
4063
|
|
4064 if (modifiers & MOD_META) buffer_insert_c_string (buf, "M-");
|
|
4065 if (modifiers & MOD_CONTROL) buffer_insert_c_string (buf, "C-");
|
|
4066 if (modifiers & MOD_SUPER) buffer_insert_c_string (buf, "S-");
|
|
4067 if (modifiers & MOD_HYPER) buffer_insert_c_string (buf, "H-");
|
|
4068 if (modifiers & MOD_ALT) buffer_insert_c_string (buf, "Alt-");
|
|
4069 if (modifiers & MOD_SHIFT) buffer_insert_c_string (buf, "Sh-");
|
|
4070 if (SYMBOLP (keysym))
|
|
4071 {
|
|
4072 Lisp_Object code = Fget (keysym, Vcharacter_set_property, Qnil);
|
|
4073 Emchar c = (CHAR_OR_CHAR_INTP (code)
|
|
4074 ? XCHAR_OR_CHAR_INT (code) : -1);
|
|
4075 /* Calling Fsingle_key_description() would cons more */
|
|
4076 #if 0 /* This is bogus */
|
|
4077 if (EQ (keysym, QKlinefeed))
|
|
4078 buffer_insert_c_string (buf, "LFD");
|
|
4079 else if (EQ (keysym, QKtab))
|
|
4080 buffer_insert_c_string (buf, "TAB");
|
|
4081 else if (EQ (keysym, QKreturn))
|
|
4082 buffer_insert_c_string (buf, "RET");
|
|
4083 else if (EQ (keysym, QKescape))
|
|
4084 buffer_insert_c_string (buf, "ESC");
|
|
4085 else if (EQ (keysym, QKdelete))
|
|
4086 buffer_insert_c_string (buf, "DEL");
|
|
4087 else if (EQ (keysym, QKspace))
|
|
4088 buffer_insert_c_string (buf, "SPC");
|
|
4089 else if (EQ (keysym, QKbackspace))
|
|
4090 buffer_insert_c_string (buf, "BS");
|
|
4091 else
|
|
4092 #endif
|
|
4093 if (c >= printable_min)
|
|
4094 buffer_insert_emacs_char (buf, c);
|
|
4095 else buffer_insert1 (buf, Fsymbol_name (keysym));
|
|
4096 }
|
|
4097 else if (CHARP (keysym))
|
|
4098 buffer_insert_emacs_char (buf, XCHAR (keysym));
|
|
4099 else
|
|
4100 buffer_insert_c_string (buf, "---bad keysym---");
|
|
4101
|
|
4102 if (elided)
|
|
4103 elided = 0;
|
|
4104 else
|
|
4105 {
|
|
4106 int k = 0;
|
|
4107
|
|
4108 while (elide_next_two_p (list))
|
|
4109 {
|
|
4110 k++;
|
|
4111 list = XCDR (list);
|
|
4112 }
|
|
4113 if (k != 0)
|
|
4114 {
|
|
4115 if (k == 1)
|
|
4116 buffer_insert_c_string (buf, ", ");
|
|
4117 else
|
|
4118 buffer_insert_c_string (buf, " .. ");
|
|
4119 elided = 1;
|
|
4120 continue;
|
|
4121 }
|
|
4122 }
|
|
4123
|
|
4124 /* Print a description of the definition of this character. */
|
|
4125 (*elt_describer) (XCDR (XCAR (list)));
|
|
4126 list = XCDR (list);
|
|
4127 }
|
|
4128 }
|
|
4129 UNGCPRO;
|
|
4130 }
|
|
4131
|
|
4132
|
|
4133 void
|
|
4134 syms_of_keymap (void)
|
|
4135 {
|
|
4136 defsymbol (&Qminor_mode_map_alist, "minor-mode-map-alist");
|
|
4137
|
|
4138 defsymbol (&Qkeymapp, "keymapp");
|
|
4139
|
|
4140 defsymbol (&Qsuppress_keymap, "suppress-keymap");
|
|
4141
|
|
4142 defsymbol (&Qmodeline_map, "modeline-map");
|
|
4143 defsymbol (&Qtoolbar_map, "toolbar-map");
|
|
4144
|
20
|
4145 DEFSUBR (Fkeymap_parents);
|
|
4146 DEFSUBR (Fset_keymap_parents);
|
|
4147 DEFSUBR (Fkeymap_name);
|
|
4148 DEFSUBR (Fset_keymap_name);
|
|
4149 DEFSUBR (Fkeymap_prompt);
|
|
4150 DEFSUBR (Fset_keymap_prompt);
|
|
4151 DEFSUBR (Fkeymap_default_binding);
|
|
4152 DEFSUBR (Fset_keymap_default_binding);
|
|
4153
|
|
4154 DEFSUBR (Fkeymapp);
|
|
4155 DEFSUBR (Fmake_keymap);
|
|
4156 DEFSUBR (Fmake_sparse_keymap);
|
|
4157
|
|
4158 DEFSUBR (Fcopy_keymap);
|
|
4159 DEFSUBR (Fkeymap_fullness);
|
|
4160 DEFSUBR (Fmap_keymap);
|
|
4161 DEFSUBR (Fevent_matches_key_specifier_p);
|
|
4162 DEFSUBR (Fdefine_key);
|
|
4163 DEFSUBR (Flookup_key);
|
|
4164 DEFSUBR (Fkey_binding);
|
|
4165 DEFSUBR (Fuse_global_map);
|
|
4166 DEFSUBR (Fuse_local_map);
|
|
4167 DEFSUBR (Fcurrent_local_map);
|
|
4168 DEFSUBR (Fcurrent_global_map);
|
|
4169 DEFSUBR (Fcurrent_keymaps);
|
|
4170 DEFSUBR (Faccessible_keymaps);
|
|
4171 DEFSUBR (Fkey_description);
|
|
4172 DEFSUBR (Fsingle_key_description);
|
|
4173 DEFSUBR (Fwhere_is_internal);
|
|
4174 DEFSUBR (Fdescribe_bindings_internal);
|
|
4175
|
|
4176 DEFSUBR (Ftext_char_description);
|
0
|
4177
|
|
4178 defsymbol (&Qcontrol, "control");
|
|
4179 defsymbol (&Qctrl, "ctrl");
|
|
4180 defsymbol (&Qmeta, "meta");
|
|
4181 defsymbol (&Qsuper, "super");
|
|
4182 defsymbol (&Qhyper, "hyper");
|
|
4183 defsymbol (&Qalt, "alt");
|
|
4184 defsymbol (&Qshift, "shift");
|
|
4185 defsymbol (&Qbutton0, "button0");
|
|
4186 defsymbol (&Qbutton1, "button1");
|
|
4187 defsymbol (&Qbutton2, "button2");
|
|
4188 defsymbol (&Qbutton3, "button3");
|
|
4189 defsymbol (&Qbutton4, "button4");
|
|
4190 defsymbol (&Qbutton5, "button5");
|
|
4191 defsymbol (&Qbutton6, "button6");
|
|
4192 defsymbol (&Qbutton7, "button7");
|
|
4193 defsymbol (&Qbutton0up, "button0up");
|
|
4194 defsymbol (&Qbutton1up, "button1up");
|
|
4195 defsymbol (&Qbutton2up, "button2up");
|
|
4196 defsymbol (&Qbutton3up, "button3up");
|
|
4197 defsymbol (&Qbutton4up, "button4up");
|
|
4198 defsymbol (&Qbutton5up, "button5up");
|
|
4199 defsymbol (&Qbutton6up, "button6up");
|
|
4200 defsymbol (&Qbutton7up, "button7up");
|
|
4201 defsymbol (&Qmenu_selection, "menu-selection");
|
|
4202 defsymbol (&QLFD, "LFD");
|
|
4203 defsymbol (&QTAB, "TAB");
|
|
4204 defsymbol (&QRET, "RET");
|
|
4205 defsymbol (&QESC, "ESC");
|
|
4206 defsymbol (&QDEL, "DEL");
|
|
4207 defsymbol (&QBS, "BS");
|
|
4208 }
|
|
4209
|
|
4210 void
|
|
4211 vars_of_keymap (void)
|
|
4212 {
|
|
4213 DEFVAR_LISP ("meta-prefix-char", &Vmeta_prefix_char /*
|
|
4214 Meta-prefix character.
|
|
4215 This character followed by some character `foo' turns into `Meta-foo'.
|
|
4216 This can be any form recognized as a single key specifier.
|
|
4217 To disable the meta-prefix-char, set it to a negative number.
|
|
4218 */ );
|
|
4219 Vmeta_prefix_char = make_char (033);
|
|
4220
|
|
4221 DEFVAR_LISP ("mouse-grabbed-buffer", &Vmouse_grabbed_buffer /*
|
|
4222 A buffer which should be consulted first for all mouse activity.
|
|
4223 When a mouse-click is processed, it will first be looked up in the
|
|
4224 local-map of this buffer, and then through the normal mechanism if there
|
|
4225 is no binding for that click. This buffer's value of `mode-motion-hook'
|
|
4226 will be consulted instead of the `mode-motion-hook' of the buffer of the
|
|
4227 window under the mouse. You should *bind* this, not set it.
|
|
4228 */ );
|
|
4229 Vmouse_grabbed_buffer = Qnil;
|
|
4230
|
|
4231 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map /*
|
|
4232 Keymap that overrides all other local keymaps.
|
|
4233 If this variable is non-nil, it is used as a keymap instead of the
|
|
4234 buffer's local map, and the minor mode keymaps and extent-local keymaps.
|
|
4235 You should *bind* this, not set it.
|
|
4236 */ );
|
|
4237 Voverriding_local_map = Qnil;
|
|
4238
|
|
4239 Fset (Qminor_mode_map_alist, Qnil);
|
|
4240
|
|
4241 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map /*
|
|
4242 Keymap of key translations that can override keymaps.
|
|
4243 This keymap works like `function-key-map', but comes after that,
|
|
4244 and applies even for keys that have ordinary bindings.
|
|
4245 */ );
|
|
4246
|
|
4247 DEFVAR_INT ("keymap-tick", &keymap_tick /*
|
|
4248 Incremented for each change to any keymap.
|
|
4249 */ );
|
|
4250 keymap_tick = 0;
|
|
4251
|
|
4252 staticpro (&Vcurrent_global_map);
|
|
4253
|
|
4254 Vsingle_space_string = make_pure_string ((CONST Bufbyte *) " ", 1, Qnil, 1);
|
|
4255 staticpro (&Vsingle_space_string);
|
|
4256 }
|
|
4257
|
|
4258 void
|
|
4259 complex_vars_of_keymap (void)
|
|
4260 {
|
|
4261 /* This function can GC */
|
|
4262 Lisp_Object ESC_prefix = intern ("ESC-prefix");
|
|
4263 Lisp_Object meta_disgustitute;
|
|
4264
|
|
4265 Vcurrent_global_map = Fmake_keymap (Qnil);
|
|
4266
|
|
4267 meta_disgustitute = Fmake_keymap (Qnil);
|
|
4268 Ffset (ESC_prefix, meta_disgustitute);
|
|
4269 /* no need to protect meta_disgustitute, though */
|
|
4270 keymap_store_internal (MAKE_MODIFIER_HASH_KEY (MOD_META),
|
|
4271 XKEYMAP (Vcurrent_global_map),
|
|
4272 meta_disgustitute);
|
|
4273 XKEYMAP (Vcurrent_global_map)->sub_maps_cache = Qt;
|
|
4274
|
|
4275 Vkey_translation_map = Fmake_sparse_keymap (intern ("key-translation-map"));
|
|
4276 }
|