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