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