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