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