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