]> code.delx.au - gnu-emacs/blob - src/keyboard.c
(make_lispy_event) [!USE_X_TOOLKIT]: Dragging
[gnu-emacs] / src / keyboard.c
1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985,86,87,88,89,93,94,95 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /* Allow config.h to undefine symbols found here. */
21 #include <signal.h>
22
23 #include <config.h>
24 #include <stdio.h>
25 #include "termchar.h"
26 #include "termopts.h"
27 #include "lisp.h"
28 #include "termhooks.h"
29 #include "macros.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "commands.h"
33 #include "buffer.h"
34 #include "disptab.h"
35 #include "dispextern.h"
36 #include "keyboard.h"
37 #include "intervals.h"
38 #include "blockinput.h"
39 #include <setjmp.h>
40 #include <errno.h>
41
42 #ifdef MSDOS
43 #include "msdos.h"
44 #include <time.h>
45 #else /* not MSDOS */
46 #ifndef VMS
47 #include <sys/ioctl.h>
48 #endif
49 #endif /* not MSDOS */
50
51 #include "syssignal.h"
52 #include "systty.h"
53
54 /* This is to get the definitions of the XK_ symbols. */
55 #ifdef HAVE_X_WINDOWS
56 #include "xterm.h"
57 #endif
58
59 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
60 #include "systime.h"
61
62 extern int errno;
63
64 /* Variables for blockinput.h: */
65
66 /* Non-zero if interrupt input is blocked right now. */
67 int interrupt_input_blocked;
68
69 /* Nonzero means an input interrupt has arrived
70 during the current critical section. */
71 int interrupt_input_pending;
72
73
74 /* File descriptor to use for input. */
75 extern int input_fd;
76
77 #ifdef HAVE_X_WINDOWS
78 /* Make all keyboard buffers much bigger when using X windows. */
79 #define KBD_BUFFER_SIZE 4096
80 #else /* No X-windows, character input */
81 #define KBD_BUFFER_SIZE 256
82 #endif /* No X-windows */
83
84 /* Following definition copied from eval.c */
85
86 struct backtrace
87 {
88 struct backtrace *next;
89 Lisp_Object *function;
90 Lisp_Object *args; /* Points to vector of args. */
91 int nargs; /* length of vector. If nargs is UNEVALLED,
92 args points to slot holding list of
93 unevalled args */
94 char evalargs;
95 };
96
97 #ifdef MULTI_KBOARD
98 KBOARD *initial_kboard;
99 KBOARD *current_kboard;
100 KBOARD *all_kboards;
101 int single_kboard;
102 #else
103 KBOARD the_only_kboard;
104 #endif
105
106 /* Non-nil disable property on a command means
107 do not execute it; call disabled-command-hook's value instead. */
108 Lisp_Object Qdisabled, Qdisabled_command_hook;
109
110 #define NUM_RECENT_KEYS (100)
111 int recent_keys_index; /* Index for storing next element into recent_keys */
112 int total_keys; /* Total number of elements stored into recent_keys */
113 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
114
115 /* Vector holding the key sequence that invoked the current command.
116 It is reused for each command, and it may be longer than the current
117 sequence; this_command_key_count indicates how many elements
118 actually mean something.
119 It's easier to staticpro a single Lisp_Object than an array. */
120 Lisp_Object this_command_keys;
121 int this_command_key_count;
122
123 /* Record values of this_command_key_count and echo_length ()
124 before this command was read. */
125 static int before_command_key_count;
126 static int before_command_echo_length;
127 /* Values of before_command_key_count and before_command_echo_length
128 saved by reset-this-command-lengths. */
129 static int before_command_key_count_1;
130 static int before_command_echo_length_1;
131 /* Flag set by reset-this-command-lengths,
132 saying to reset the lengths when add_command_key is called. */
133 static int before_command_restore_flag;
134
135 extern int minbuf_level;
136
137 extern struct backtrace *backtrace_list;
138
139 /* Nonzero means do menu prompting. */
140 static int menu_prompting;
141
142 /* Character to see next line of menu prompt. */
143 static Lisp_Object menu_prompt_more_char;
144
145 /* For longjmp to where kbd input is being done. */
146 static jmp_buf getcjmp;
147
148 /* True while doing kbd input. */
149 int waiting_for_input;
150
151 /* True while displaying for echoing. Delays C-g throwing. */
152 static int echoing;
153
154 /* Nonzero means disregard local maps for the menu bar. */
155 static int inhibit_local_menu_bar_menus;
156
157 /* Nonzero means C-g should cause immediate error-signal. */
158 int immediate_quit;
159
160 /* Character to recognize as the help char. */
161 Lisp_Object Vhelp_char;
162
163 /* List of other event types to recognize as meaning "help". */
164 Lisp_Object Vhelp_event_list;
165
166 /* Form to execute when help char is typed. */
167 Lisp_Object Vhelp_form;
168
169 /* Command to run when the help character follows a prefix key. */
170 Lisp_Object Vprefix_help_command;
171
172 /* List of items that should move to the end of the menu bar. */
173 Lisp_Object Vmenu_bar_final_items;
174
175 /* Non-nil means show the equivalent key-binding for
176 any M-x command that has one.
177 The value can be a length of time to show the message for.
178 If the value is non-nil and not a number, we wait 2 seconds. */
179 Lisp_Object Vsuggest_key_bindings;
180
181 /* Character that causes a quit. Normally C-g.
182
183 If we are running on an ordinary terminal, this must be an ordinary
184 ASCII char, since we want to make it our interrupt character.
185
186 If we are not running on an ordinary terminal, it still needs to be
187 an ordinary ASCII char. This character needs to be recognized in
188 the input interrupt handler. At this point, the keystroke is
189 represented as a struct input_event, while the desired quit
190 character is specified as a lispy event. The mapping from struct
191 input_events to lispy events cannot run in an interrupt handler,
192 and the reverse mapping is difficult for anything but ASCII
193 keystrokes.
194
195 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
196 ASCII character. */
197 int quit_char;
198
199 extern Lisp_Object current_global_map;
200 extern int minibuf_level;
201
202 /* If non-nil, this is a map that overrides all other local maps. */
203 Lisp_Object Voverriding_local_map;
204
205 /* If non-nil, Voverriding_local_map applies to the menu bar. */
206 Lisp_Object Voverriding_local_map_menu_flag;
207
208 /* Keymap that defines special misc events that should
209 be processed immediately at a low level. */
210 Lisp_Object Vspecial_event_map;
211
212 /* Current depth in recursive edits. */
213 int command_loop_level;
214
215 /* Total number of times command_loop has read a key sequence. */
216 int num_input_keys;
217
218 /* Last input character read as a command. */
219 Lisp_Object last_command_char;
220
221 /* Last input character read as a command, not counting menus
222 reached by the mouse. */
223 Lisp_Object last_nonmenu_event;
224
225 /* Last input character read for any purpose. */
226 Lisp_Object last_input_char;
227
228 /* If not Qnil, a list of objects to be read as subsequent command input. */
229 Lisp_Object Vunread_command_events;
230
231 /* If not -1, an event to be read as subsequent command input. */
232 int unread_command_char;
233
234 /* If not Qnil, this is a switch-frame event which we decided to put
235 off until the end of a key sequence. This should be read as the
236 next command input, after any unread_command_events.
237
238 read_key_sequence uses this to delay switch-frame events until the
239 end of the key sequence; Fread_char uses it to put off switch-frame
240 events until a non-ASCII event is acceptable as input. */
241 Lisp_Object unread_switch_frame;
242
243 /* A mask of extra modifier bits to put into every keyboard char. */
244 int extra_keyboard_modifiers;
245
246 /* Char to use as prefix when a meta character is typed in.
247 This is bound on entry to minibuffer in case ESC is changed there. */
248
249 Lisp_Object meta_prefix_char;
250
251 /* Last size recorded for a current buffer which is not a minibuffer. */
252 static int last_non_minibuf_size;
253
254 /* Number of idle seconds before an auto-save and garbage collection. */
255 static Lisp_Object Vauto_save_timeout;
256
257 /* Total number of times read_char has returned. */
258 int num_input_chars;
259
260 /* Total number of times read_char has returned, outside of macros. */
261 int num_nonmacro_input_chars;
262
263 /* Auto-save automatically when this many characters have been typed
264 since the last time. */
265
266 static int auto_save_interval;
267
268 /* Value of num_nonmacro_input_chars as of last auto save. */
269
270 int last_auto_save;
271
272 /* The command being executed by the command loop.
273 Commands may set this, and the value set will be copied into
274 current_kboard->Vlast_command instead of the actual command. */
275 Lisp_Object this_command;
276
277 /* The value of point when the last command was executed. */
278 int last_point_position;
279
280 /* The buffer that was current when the last command was started. */
281 Lisp_Object last_point_position_buffer;
282
283 #ifdef MULTI_FRAME
284 /* The frame in which the last input event occurred, or Qmacro if the
285 last event came from a macro. We use this to determine when to
286 generate switch-frame events. This may be cleared by functions
287 like Fselect_frame, to make sure that a switch-frame event is
288 generated by the next character. */
289 Lisp_Object internal_last_event_frame;
290 #endif
291
292 /* A user-visible version of the above, intended to allow users to
293 figure out where the last event came from, if the event doesn't
294 carry that information itself (i.e. if it was a character). */
295 Lisp_Object Vlast_event_frame;
296
297 /* The timestamp of the last input event we received from the X server.
298 X Windows wants this for selection ownership. */
299 unsigned long last_event_timestamp;
300
301 Lisp_Object Qself_insert_command;
302 Lisp_Object Qforward_char;
303 Lisp_Object Qbackward_char;
304 Lisp_Object Qundefined;
305
306 /* read_key_sequence stores here the command definition of the
307 key sequence that it reads. */
308 Lisp_Object read_key_sequence_cmd;
309
310 /* Form to evaluate (if non-nil) when Emacs is started. */
311 Lisp_Object Vtop_level;
312
313 /* User-supplied string to translate input characters through. */
314 Lisp_Object Vkeyboard_translate_table;
315
316 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
317 extern Lisp_Object Vfunction_key_map;
318
319 /* Another keymap that maps key sequences into key sequences.
320 This one takes precedence over ordinary definitions. */
321 extern Lisp_Object Vkey_translation_map;
322
323 /* Non-nil means deactivate the mark at end of this command. */
324 Lisp_Object Vdeactivate_mark;
325
326 /* Menu bar specified in Lucid Emacs fashion. */
327
328 Lisp_Object Vlucid_menu_bar_dirty_flag;
329 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
330
331 /* Hooks to run before and after each command. */
332 Lisp_Object Qpre_command_hook, Vpre_command_hook;
333 Lisp_Object Qpost_command_hook, Vpost_command_hook;
334 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
335 /* Hook run after a command if there's no more input soon. */
336 Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook;
337
338 /* Delay time in microseconds before running post-command-idle-hook. */
339 int post_command_idle_delay;
340
341 /* List of deferred actions to be performed at a later time.
342 The precise format isn't relevant here; we just check whether it is nil. */
343 Lisp_Object Vdeferred_action_list;
344
345 /* Function to call to handle deferred actions, when there are any. */
346 Lisp_Object Vdeferred_action_function;
347 Lisp_Object Qdeferred_action_function;
348
349 /* File in which we write all commands we read. */
350 FILE *dribble;
351
352 /* Nonzero if input is available. */
353 int input_pending;
354
355 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
356 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
357
358 int meta_key;
359
360 extern char *pending_malloc_warning;
361
362 /* Circular buffer for pre-read keyboard input. */
363 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
364
365 /* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
366
367 The interrupt-level event handlers will never enqueue an event on a
368 frame which is not in Vframe_list, and once an event is dequeued,
369 internal_last_event_frame or the event itself points to the frame.
370 So that's all fine.
371
372 But while the event is sitting in the queue, it's completely
373 unprotected. Suppose the user types one command which will run for
374 a while and then delete a frame, and then types another event at
375 the frame that will be deleted, before the command gets around to
376 it. Suppose there are no references to this frame elsewhere in
377 Emacs, and a GC occurs before the second event is dequeued. Now we
378 have an event referring to a freed frame, which will crash Emacs
379 when it is dequeued.
380
381 Similar things happen when an event on a scroll bar is enqueued; the
382 window may be deleted while the event is in the queue.
383
384 So, we use this vector to protect the frame_or_window field in the
385 event queue. That way, they'll be dequeued as dead frames or
386 windows, but still valid lisp objects.
387
388 If kbd_buffer[i].kind != no_event, then
389 (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
390 == kbd_buffer[i].frame_or_window. */
391 static Lisp_Object kbd_buffer_frame_or_window;
392
393 /* Pointer to next available character in kbd_buffer.
394 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
395 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
396 next available char is in kbd_buffer[0]. */
397 static struct input_event *kbd_fetch_ptr;
398
399 /* Pointer to next place to store character in kbd_buffer. This
400 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
401 character should go in kbd_buffer[0]. */
402 static volatile struct input_event *kbd_store_ptr;
403
404 /* The above pair of variables forms a "queue empty" flag. When we
405 enqueue a non-hook event, we increment kbd_store_ptr. When we
406 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
407 there is input available iff the two pointers are not equal.
408
409 Why not just have a flag set and cleared by the enqueuing and
410 dequeuing functions? Such a flag could be screwed up by interrupts
411 at inopportune times. */
412
413 /* If this flag is non-nil, we check mouse_moved to see when the
414 mouse moves, and motion events will appear in the input stream.
415 Otherwise, mouse motion is ignored. */
416 static Lisp_Object do_mouse_tracking;
417
418 /* Symbols to head events. */
419 Lisp_Object Qmouse_movement;
420 Lisp_Object Qscroll_bar_movement;
421 Lisp_Object Qswitch_frame;
422 Lisp_Object Qdelete_frame;
423 Lisp_Object Qiconify_frame;
424 Lisp_Object Qmake_frame_visible;
425
426 /* Symbols to denote kinds of events. */
427 Lisp_Object Qfunction_key;
428 Lisp_Object Qmouse_click;
429 /* Lisp_Object Qmouse_movement; - also an event header */
430
431 /* Properties of event headers. */
432 Lisp_Object Qevent_kind;
433 Lisp_Object Qevent_symbol_elements;
434
435 Lisp_Object Qmenu_enable;
436
437 /* An event header symbol HEAD may have a property named
438 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
439 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
440 mask of modifiers applied to it. If present, this is used to help
441 speed up parse_modifiers. */
442 Lisp_Object Qevent_symbol_element_mask;
443
444 /* An unmodified event header BASE may have a property named
445 Qmodifier_cache, which is an alist mapping modifier masks onto
446 modified versions of BASE. If present, this helps speed up
447 apply_modifiers. */
448 Lisp_Object Qmodifier_cache;
449
450 /* Symbols to use for parts of windows. */
451 Lisp_Object Qmode_line;
452 Lisp_Object Qvertical_line;
453 Lisp_Object Qvertical_scroll_bar;
454 Lisp_Object Qmenu_bar;
455
456 extern Lisp_Object Qmenu_enable;
457
458 Lisp_Object recursive_edit_unwind (), command_loop ();
459 Lisp_Object Fthis_command_keys ();
460 Lisp_Object Qextended_command_history;
461
462 extern char *x_get_keysym_name ();
463
464 Lisp_Object Qpolling_period;
465
466 extern Lisp_Object Vprint_level, Vprint_length;
467
468 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
469 happens. */
470 EMACS_TIME *input_available_clear_time;
471
472 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
473 Default is 1 if INTERRUPT_INPUT is defined. */
474 int interrupt_input;
475
476 /* Nonzero while interrupts are temporarily deferred during redisplay. */
477 int interrupts_deferred;
478
479 /* nonzero means use ^S/^Q for flow control. */
480 int flow_control;
481
482 /* Allow m- file to inhibit use of FIONREAD. */
483 #ifdef BROKEN_FIONREAD
484 #undef FIONREAD
485 #endif
486
487 /* We are unable to use interrupts if FIONREAD is not available,
488 so flush SIGIO so we won't try. */
489 #ifndef FIONREAD
490 #ifdef SIGIO
491 #undef SIGIO
492 #endif
493 #endif
494
495 /* If we support X Windows, turn on the code to poll periodically
496 to detect C-g. It isn't actually used when doing interrupt input. */
497 #ifdef HAVE_X_WINDOWS
498 #define POLL_FOR_INPUT
499 #endif
500 \f
501 /* Global variable declarations. */
502
503 /* Function for init_keyboard to call with no args (if nonzero). */
504 void (*keyboard_init_hook) ();
505
506 static int read_avail_input ();
507 static void get_input_pending ();
508 static int readable_events ();
509 static Lisp_Object read_char_x_menu_prompt ();
510 static Lisp_Object read_char_minibuf_menu_prompt ();
511 static Lisp_Object make_lispy_event ();
512 #ifdef HAVE_MOUSE
513 static Lisp_Object make_lispy_movement ();
514 #endif
515 static Lisp_Object modify_event_symbol ();
516 static Lisp_Object make_lispy_switch_frame ();
517 static int parse_solitary_modifier ();
518
519 /* > 0 if we are to echo keystrokes. */
520 static int echo_keystrokes;
521
522 /* Nonzero means don't try to suspend even if the operating system seems
523 to support it. */
524 static int cannot_suspend;
525
526 #define min(a,b) ((a)<(b)?(a):(b))
527 #define max(a,b) ((a)>(b)?(a):(b))
528
529 /* Install the string STR as the beginning of the string of echoing,
530 so that it serves as a prompt for the next character.
531 Also start echoing. */
532
533 echo_prompt (str)
534 char *str;
535 {
536 int len = strlen (str);
537
538 if (len > ECHOBUFSIZE - 4)
539 len = ECHOBUFSIZE - 4;
540 bcopy (str, current_kboard->echobuf, len);
541 current_kboard->echoptr = current_kboard->echobuf + len;
542 *current_kboard->echoptr = '\0';
543
544 current_kboard->echo_after_prompt = len;
545
546 echo ();
547 }
548
549 /* Add C to the echo string, if echoing is going on.
550 C can be a character, which is printed prettily ("M-C-x" and all that
551 jazz), or a symbol, whose name is printed. */
552
553 echo_char (c)
554 Lisp_Object c;
555 {
556 extern char *push_key_description ();
557
558 if (current_kboard->immediate_echo)
559 {
560 char *ptr = current_kboard->echoptr;
561
562 if (ptr != current_kboard->echobuf)
563 *ptr++ = ' ';
564
565 /* If someone has passed us a composite event, use its head symbol. */
566 c = EVENT_HEAD (c);
567
568 if (INTEGERP (c))
569 {
570 if (ptr - current_kboard->echobuf > ECHOBUFSIZE - 6)
571 return;
572
573 ptr = push_key_description (XINT (c), ptr);
574 }
575 else if (SYMBOLP (c))
576 {
577 struct Lisp_String *name = XSYMBOL (c)->name;
578 if ((ptr - current_kboard->echobuf) + name->size + 4 > ECHOBUFSIZE)
579 return;
580 bcopy (name->data, ptr, name->size);
581 ptr += name->size;
582 }
583
584 if (current_kboard->echoptr == current_kboard->echobuf
585 && help_char_p (c))
586 {
587 strcpy (ptr, " (Type ? for further options)");
588 ptr += strlen (ptr);
589 }
590
591 *ptr = 0;
592 current_kboard->echoptr = ptr;
593
594 echo ();
595 }
596 }
597
598 /* Temporarily add a dash to the end of the echo string if it's not
599 empty, so that it serves as a mini-prompt for the very next character. */
600
601 echo_dash ()
602 {
603 if (!current_kboard->immediate_echo
604 && current_kboard->echoptr == current_kboard->echobuf)
605 return;
606 /* Do nothing if we just printed a prompt. */
607 if (current_kboard->echo_after_prompt
608 == current_kboard->echoptr - current_kboard->echobuf)
609 return;
610 /* Do nothing if not echoing at all. */
611 if (current_kboard->echoptr == 0)
612 return;
613
614 /* Put a dash at the end of the buffer temporarily,
615 but make it go away when the next character is added. */
616 current_kboard->echoptr[0] = '-';
617 current_kboard->echoptr[1] = 0;
618
619 echo ();
620 }
621
622 /* Display the current echo string, and begin echoing if not already
623 doing so. */
624
625 echo ()
626 {
627 if (!current_kboard->immediate_echo)
628 {
629 int i;
630 current_kboard->immediate_echo = 1;
631
632 for (i = 0; i < this_command_key_count; i++)
633 {
634 Lisp_Object c;
635 c = XVECTOR (this_command_keys)->contents[i];
636 if (! (EVENT_HAS_PARAMETERS (c)
637 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
638 echo_char (c);
639 }
640 echo_dash ();
641 }
642
643 echoing = 1;
644 message1_nolog (current_kboard->echobuf);
645 echoing = 0;
646
647 if (waiting_for_input && !NILP (Vquit_flag))
648 quit_throw_to_read_char ();
649 }
650
651 /* Turn off echoing, for the start of a new command. */
652
653 cancel_echoing ()
654 {
655 current_kboard->immediate_echo = 0;
656 current_kboard->echoptr = current_kboard->echobuf;
657 current_kboard->echo_after_prompt = -1;
658 }
659
660 /* Return the length of the current echo string. */
661
662 static int
663 echo_length ()
664 {
665 return current_kboard->echoptr - current_kboard->echobuf;
666 }
667
668 /* Truncate the current echo message to its first LEN chars.
669 This and echo_char get used by read_key_sequence when the user
670 switches frames while entering a key sequence. */
671
672 static void
673 echo_truncate (len)
674 int len;
675 {
676 current_kboard->echobuf[len] = '\0';
677 current_kboard->echoptr = current_kboard->echobuf + len;
678 truncate_echo_area (len);
679 }
680
681 \f
682 /* Functions for manipulating this_command_keys. */
683 static void
684 add_command_key (key)
685 Lisp_Object key;
686 {
687 int size = XVECTOR (this_command_keys)->size;
688
689 /* If reset-this-command-length was called recently, obey it now.
690 See the doc string of that function for an explanation of why. */
691 if (before_command_restore_flag)
692 {
693 this_command_key_count = before_command_key_count_1;
694 echo_truncate (before_command_echo_length_1);
695 before_command_restore_flag = 0;
696 }
697
698 if (this_command_key_count >= size)
699 {
700 Lisp_Object new_keys;
701
702 new_keys = Fmake_vector (make_number (size * 2), Qnil);
703 bcopy (XVECTOR (this_command_keys)->contents,
704 XVECTOR (new_keys)->contents,
705 size * sizeof (Lisp_Object));
706
707 this_command_keys = new_keys;
708 }
709
710 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
711 }
712 \f
713 Lisp_Object
714 recursive_edit_1 ()
715 {
716 int count = specpdl_ptr - specpdl;
717 Lisp_Object val;
718
719 if (command_loop_level > 0)
720 {
721 specbind (Qstandard_output, Qt);
722 specbind (Qstandard_input, Qt);
723 }
724
725 val = command_loop ();
726 if (EQ (val, Qt))
727 Fsignal (Qquit, Qnil);
728
729 return unbind_to (count, Qnil);
730 }
731
732 /* When an auto-save happens, record the "time", and don't do again soon. */
733
734 record_auto_save ()
735 {
736 last_auto_save = num_nonmacro_input_chars;
737 }
738
739 /* Make an auto save happen as soon as possible at command level. */
740
741 force_auto_save_soon ()
742 {
743 last_auto_save = - auto_save_interval - 1;
744
745 record_asynch_buffer_change ();
746 }
747 \f
748 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
749 "Invoke the editor command loop recursively.\n\
750 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
751 that tells this function to return.\n\
752 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
753 This function is called by the editor initialization to begin editing.")
754 ()
755 {
756 int count = specpdl_ptr - specpdl;
757 Lisp_Object val;
758
759 command_loop_level++;
760 update_mode_lines = 1;
761
762 record_unwind_protect (recursive_edit_unwind,
763 (command_loop_level
764 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
765 ? Fcurrent_buffer ()
766 : Qnil);
767 recursive_edit_1 ();
768 return unbind_to (count, Qnil);
769 }
770
771 Lisp_Object
772 recursive_edit_unwind (buffer)
773 Lisp_Object buffer;
774 {
775 if (!NILP (buffer))
776 Fset_buffer (buffer);
777
778 command_loop_level--;
779 update_mode_lines = 1;
780 return Qnil;
781 }
782 \f
783 static void
784 any_kboard_state ()
785 {
786 #ifdef MULTI_KBOARD
787 #if 0 /* Theory: if there's anything in Vunread_command_events,
788 it will right away be read by read_key_sequence,
789 and then if we do switch KBOARDS, it will go into the side
790 queue then. So we don't need to do anything special here -- rms. */
791 if (CONSP (Vunread_command_events))
792 {
793 current_kboard->kbd_queue
794 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
795 current_kboard->kbd_queue_has_data = 1;
796 }
797 Vunread_command_events = Qnil;
798 #endif
799 single_kboard = 0;
800 #endif
801 }
802
803 /* Switch to the single-kboard state, making current_kboard
804 the only KBOARD from which further input is accepted. */
805
806 void
807 single_kboard_state ()
808 {
809 #ifdef MULTI_KBOARD
810 single_kboard = 1;
811 #endif
812 }
813
814 /* Maintain a stack of kboards, so other parts of Emacs
815 can switch temporarily to the kboard of a given frame
816 and then revert to the previous status. */
817
818 struct kboard_stack
819 {
820 KBOARD *kboard;
821 struct kboard_stack *next;
822 };
823
824 static struct kboard_stack *kboard_stack;
825
826 void
827 push_frame_kboard (f)
828 FRAME_PTR f;
829 {
830 #ifdef MULTI_KBOARD
831 struct kboard_stack *p
832 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
833
834 p->next = kboard_stack;
835 p->kboard = current_kboard;
836 kboard_stack = p;
837
838 current_kboard = FRAME_KBOARD (f);
839 #endif
840 }
841
842 void
843 pop_frame_kboard ()
844 {
845 #ifdef MULTI_KBOARD
846 struct kboard_stack *p = kboard_stack;
847 current_kboard = p->kboard;
848 kboard_stack = p->next;
849 xfree (p);
850 #endif
851 }
852 \f
853 /* Handle errors that are not handled at inner levels
854 by printing an error message and returning to the editor command loop. */
855
856 Lisp_Object
857 cmd_error (data)
858 Lisp_Object data;
859 {
860 Lisp_Object old_level, old_length;
861
862 Vstandard_output = Qt;
863 Vstandard_input = Qt;
864 Vexecuting_macro = Qnil;
865 current_kboard->Vprefix_arg = Qnil;
866 cancel_echoing ();
867
868 /* Avoid unquittable loop if data contains a circular list. */
869 old_level = Vprint_level;
870 old_length = Vprint_length;
871 XSETFASTINT(Vprint_level, 10);
872 XSETFASTINT(Vprint_length, 10);
873 cmd_error_internal (data, NULL);
874 Vprint_level = old_level;
875 Vprint_length = old_length;
876
877 Vquit_flag = Qnil;
878
879 Vinhibit_quit = Qnil;
880 #ifdef MULTI_KBOARD
881 any_kboard_state ();
882 #endif
883
884 return make_number (0);
885 }
886
887 cmd_error_internal (data, context)
888 Lisp_Object data;
889 char *context;
890 {
891 Lisp_Object errmsg, tail, errname, file_error;
892 Lisp_Object stream;
893 struct gcpro gcpro1;
894 int i;
895
896 Vquit_flag = Qnil;
897 Vinhibit_quit = Qt;
898 echo_area_glyphs = 0;
899
900 /* If the window system or terminal frame hasn't been initialized
901 yet, or we're not interactive, it's best to dump this message out
902 to stderr and exit. */
903 if (! FRAME_MESSAGE_BUF (selected_frame)
904 || noninteractive)
905 stream = Qexternal_debugging_output;
906 else
907 {
908 Fdiscard_input ();
909 bitch_at_user ();
910 stream = Qt;
911 }
912
913 if (context != 0)
914 write_string_1 (context, -1, stream);
915
916 errname = Fcar (data);
917
918 if (EQ (errname, Qerror))
919 {
920 data = Fcdr (data);
921 if (!CONSP (data)) data = Qnil;
922 errmsg = Fcar (data);
923 file_error = Qnil;
924 }
925 else
926 {
927 errmsg = Fget (errname, Qerror_message);
928 file_error = Fmemq (Qfile_error,
929 Fget (errname, Qerror_conditions));
930 }
931
932 /* Print an error message including the data items.
933 This is done by printing it into a scratch buffer
934 and then making a copy of the text in the buffer. */
935
936 if (!CONSP (data)) data = Qnil;
937 tail = Fcdr (data);
938 GCPRO1 (tail);
939
940 /* For file-error, make error message by concatenating
941 all the data items. They are all strings. */
942 if (!NILP (file_error) && !NILP (tail))
943 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
944
945 if (STRINGP (errmsg))
946 Fprinc (errmsg, stream);
947 else
948 write_string_1 ("peculiar error", -1, stream);
949
950 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
951 {
952 write_string_1 (i ? ", " : ": ", 2, stream);
953 if (!NILP (file_error))
954 Fprinc (Fcar (tail), stream);
955 else
956 Fprin1 (Fcar (tail), stream);
957 }
958 UNGCPRO;
959
960 /* If the window system or terminal frame hasn't been initialized
961 yet, or we're in -batch mode, this error should cause Emacs to exit. */
962 if (! FRAME_MESSAGE_BUF (selected_frame)
963 || noninteractive)
964 {
965 Fterpri (stream);
966 Fkill_emacs (make_number (-1));
967 }
968 }
969 \f
970 Lisp_Object command_loop_1 ();
971 Lisp_Object command_loop_2 ();
972 Lisp_Object top_level_1 ();
973
974 /* Entry to editor-command-loop.
975 This level has the catches for exiting/returning to editor command loop.
976 It returns nil to exit recursive edit, t to abort it. */
977
978 Lisp_Object
979 command_loop ()
980 {
981 if (command_loop_level > 0 || minibuf_level > 0)
982 {
983 return internal_catch (Qexit, command_loop_2, Qnil);
984 }
985 else
986 while (1)
987 {
988 internal_catch (Qtop_level, top_level_1, Qnil);
989 internal_catch (Qtop_level, command_loop_2, Qnil);
990
991 /* End of file in -batch run causes exit here. */
992 if (noninteractive)
993 Fkill_emacs (Qt);
994 }
995 }
996
997 /* Here we catch errors in execution of commands within the
998 editing loop, and reenter the editing loop.
999 When there is an error, cmd_error runs and returns a non-nil
1000 value to us. A value of nil means that cmd_loop_1 itself
1001 returned due to end of file (or end of kbd macro). */
1002
1003 Lisp_Object
1004 command_loop_2 ()
1005 {
1006 register Lisp_Object val;
1007
1008 do
1009 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
1010 while (!NILP (val));
1011
1012 return Qnil;
1013 }
1014
1015 Lisp_Object
1016 top_level_2 ()
1017 {
1018 return Feval (Vtop_level);
1019 }
1020
1021 Lisp_Object
1022 top_level_1 ()
1023 {
1024 /* On entry to the outer level, run the startup file */
1025 if (!NILP (Vtop_level))
1026 internal_condition_case (top_level_2, Qerror, cmd_error);
1027 else if (!NILP (Vpurify_flag))
1028 message ("Bare impure Emacs (standard Lisp code not loaded)");
1029 else
1030 message ("Bare Emacs (standard Lisp code not loaded)");
1031 return Qnil;
1032 }
1033
1034 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1035 "Exit all recursive editing levels.")
1036 ()
1037 {
1038 Fthrow (Qtop_level, Qnil);
1039 }
1040
1041 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1042 "Exit from the innermost recursive edit or minibuffer.")
1043 ()
1044 {
1045 if (command_loop_level > 0 || minibuf_level > 0)
1046 Fthrow (Qexit, Qnil);
1047
1048 error ("No recursive edit is in progress");
1049 }
1050
1051 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1052 "Abort the command that requested this recursive edit or minibuffer input.")
1053 ()
1054 {
1055 if (command_loop_level > 0 || minibuf_level > 0)
1056 Fthrow (Qexit, Qt);
1057
1058 error ("No recursive edit is in progress");
1059 }
1060 \f
1061 /* This is the actual command reading loop,
1062 sans error-handling encapsulation. */
1063
1064 Lisp_Object Fcommand_execute ();
1065 static int read_key_sequence ();
1066 void safe_run_hooks ();
1067
1068 Lisp_Object
1069 command_loop_1 ()
1070 {
1071 Lisp_Object cmd, tem;
1072 int lose;
1073 int nonundocount;
1074 Lisp_Object keybuf[30];
1075 int i;
1076 int no_redisplay;
1077 int no_direct;
1078 int prev_modiff;
1079 struct buffer *prev_buffer;
1080 #ifdef MULTI_KBOARD
1081 int was_locked = single_kboard;
1082 #endif
1083
1084 current_kboard->Vprefix_arg = Qnil;
1085 Vdeactivate_mark = Qnil;
1086 waiting_for_input = 0;
1087 cancel_echoing ();
1088
1089 nonundocount = 0;
1090 no_redisplay = 0;
1091 this_command_key_count = 0;
1092
1093 /* Make sure this hook runs after commands that get errors and
1094 throw to top level. */
1095 /* Note that the value cell will never directly contain nil
1096 if the symbol is a local variable. */
1097 if (!NILP (XSYMBOL (Qpost_command_hook)->value) && !NILP (Vrun_hooks))
1098 safe_run_hooks (Qpost_command_hook);
1099
1100 if (!NILP (Vdeferred_action_list))
1101 call0 (Vdeferred_action_function);
1102
1103 if (!NILP (XSYMBOL (Qpost_command_idle_hook)->value) && !NILP (Vrun_hooks))
1104 {
1105 if (NILP (Vunread_command_events)
1106 && NILP (Vexecuting_macro)
1107 && !NILP (sit_for (0, post_command_idle_delay, 0, 1)))
1108 safe_run_hooks (Qpost_command_idle_hook);
1109 }
1110
1111 /* Do this after running Vpost_command_hook, for consistency. */
1112 current_kboard->Vlast_command = this_command;
1113
1114 while (1)
1115 {
1116 /* Make sure the current window's buffer is selected. */
1117 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1118 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1119
1120 /* Display any malloc warning that just came out. Use while because
1121 displaying one warning can cause another. */
1122
1123 while (pending_malloc_warning)
1124 display_malloc_warning ();
1125
1126 no_direct = 0;
1127
1128 Vdeactivate_mark = Qnil;
1129
1130 /* If minibuffer on and echo area in use,
1131 wait 2 sec and redraw minibuffer. */
1132
1133 if (minibuf_level && echo_area_glyphs
1134 && EQ (minibuf_window, echo_area_window))
1135 {
1136 /* Bind inhibit-quit to t so that C-g gets read in
1137 rather than quitting back to the minibuffer. */
1138 int count = specpdl_ptr - specpdl;
1139 specbind (Qinhibit_quit, Qt);
1140 Fsit_for (make_number (2), Qnil, Qnil);
1141 unbind_to (count, Qnil);
1142
1143 echo_area_glyphs = 0;
1144 no_direct = 1;
1145 if (!NILP (Vquit_flag))
1146 {
1147 Vquit_flag = Qnil;
1148 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1149 }
1150 }
1151
1152 #ifdef C_ALLOCA
1153 alloca (0); /* Cause a garbage collection now */
1154 /* Since we can free the most stuff here. */
1155 #endif /* C_ALLOCA */
1156
1157 #if 0
1158 #ifdef MULTI_FRAME
1159 /* Select the frame that the last event came from. Usually,
1160 switch-frame events will take care of this, but if some lisp
1161 code swallows a switch-frame event, we'll fix things up here.
1162 Is this a good idea? */
1163 if (FRAMEP (internal_last_event_frame)
1164 && XFRAME (internal_last_event_frame) != selected_frame)
1165 Fselect_frame (internal_last_event_frame, Qnil);
1166 #endif
1167 #endif
1168 /* If it has changed current-menubar from previous value,
1169 really recompute the menubar from the value. */
1170 if (! NILP (Vlucid_menu_bar_dirty_flag)
1171 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1172 call0 (Qrecompute_lucid_menubar);
1173
1174 before_command_key_count = this_command_key_count;
1175 before_command_echo_length = echo_length ();
1176
1177 this_command = Qnil;
1178
1179 /* Read next key sequence; i gets its length. */
1180 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1181 Qnil, 0, 1);
1182
1183 ++num_input_keys;
1184
1185 /* Now we have read a key sequence of length I,
1186 or else I is 0 and we found end of file. */
1187
1188 if (i == 0) /* End of file -- happens only in */
1189 return Qnil; /* a kbd macro, at the end. */
1190 /* -1 means read_key_sequence got a menu that was rejected.
1191 Just loop around and read another command. */
1192 if (i == -1)
1193 {
1194 cancel_echoing ();
1195 this_command_key_count = 0;
1196 goto finalize;
1197 }
1198
1199 last_command_char = keybuf[i - 1];
1200
1201 /* If the previous command tried to force a specific window-start,
1202 forget about that, in case this command moves point far away
1203 from that position. But also throw away beg_unchanged and
1204 end_unchanged information in that case, so that redisplay will
1205 update the whole window properly. */
1206 if (!NILP (XWINDOW (selected_window)->force_start))
1207 {
1208 XWINDOW (selected_window)->force_start = Qnil;
1209 beg_unchanged = end_unchanged = 0;
1210 }
1211
1212 cmd = read_key_sequence_cmd;
1213 if (!NILP (Vexecuting_macro))
1214 {
1215 if (!NILP (Vquit_flag))
1216 {
1217 Vexecuting_macro = Qt;
1218 QUIT; /* Make some noise. */
1219 /* Will return since macro now empty. */
1220 }
1221 }
1222
1223 /* Do redisplay processing after this command except in special
1224 cases identified below that set no_redisplay to 1.
1225 (actually, there's currently no way to prevent the redisplay,
1226 and no_redisplay is ignored.
1227 Perhaps someday we will really implement it.) */
1228 no_redisplay = 0;
1229
1230 prev_buffer = current_buffer;
1231 prev_modiff = MODIFF;
1232 last_point_position = PT;
1233 XSETBUFFER (last_point_position_buffer, prev_buffer);
1234
1235 /* Execute the command. */
1236
1237 this_command = cmd;
1238 /* Note that the value cell will never directly contain nil
1239 if the symbol is a local variable. */
1240 if (!NILP (XSYMBOL (Qpre_command_hook)->value) && !NILP (Vrun_hooks))
1241 safe_run_hooks (Qpre_command_hook);
1242
1243 if (NILP (this_command))
1244 {
1245 /* nil means key is undefined. */
1246 bitch_at_user ();
1247 current_kboard->defining_kbd_macro = Qnil;
1248 update_mode_lines = 1;
1249 current_kboard->Vprefix_arg = Qnil;
1250 }
1251 else
1252 {
1253 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1254 {
1255 /* Recognize some common commands in common situations and
1256 do them directly. */
1257 if (EQ (this_command, Qforward_char) && PT < ZV)
1258 {
1259 struct Lisp_Char_Table *dp
1260 = window_display_table (XWINDOW (selected_window));
1261 lose = FETCH_CHAR (PT);
1262 SET_PT (PT + 1);
1263 if ((dp
1264 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1265 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1266 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1267 && (lose >= 0x20 && lose < 0x7f)))
1268 : (lose >= 0x20 && lose < 0x7f))
1269 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1270 >= MODIFF)
1271 && (XFASTINT (XWINDOW (selected_window)->last_point)
1272 == PT - 1)
1273 && !windows_or_buffers_changed
1274 && EQ (current_buffer->selective_display, Qnil)
1275 && !detect_input_pending ()
1276 && NILP (Vexecuting_macro))
1277 no_redisplay = direct_output_forward_char (1);
1278 goto directly_done;
1279 }
1280 else if (EQ (this_command, Qbackward_char) && PT > BEGV)
1281 {
1282 struct Lisp_Char_Table *dp
1283 = window_display_table (XWINDOW (selected_window));
1284 SET_PT (PT - 1);
1285 lose = FETCH_CHAR (PT);
1286 if ((dp
1287 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1288 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1289 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1290 && (lose >= 0x20 && lose < 0x7f)))
1291 : (lose >= 0x20 && lose < 0x7f))
1292 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1293 >= MODIFF)
1294 && (XFASTINT (XWINDOW (selected_window)->last_point)
1295 == PT + 1)
1296 && !windows_or_buffers_changed
1297 && EQ (current_buffer->selective_display, Qnil)
1298 && !detect_input_pending ()
1299 && NILP (Vexecuting_macro))
1300 no_redisplay = direct_output_forward_char (-1);
1301 goto directly_done;
1302 }
1303 else if (EQ (this_command, Qself_insert_command)
1304 /* Try this optimization only on ascii keystrokes. */
1305 && INTEGERP (last_command_char))
1306 {
1307 unsigned char c = XINT (last_command_char);
1308 int value;
1309
1310 if (NILP (Vexecuting_macro)
1311 && !EQ (minibuf_window, selected_window))
1312 {
1313 if (!nonundocount || nonundocount >= 20)
1314 {
1315 Fundo_boundary ();
1316 nonundocount = 0;
1317 }
1318 nonundocount++;
1319 }
1320 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1321 < MODIFF)
1322 || (XFASTINT (XWINDOW (selected_window)->last_point)
1323 != PT)
1324 || MODIFF <= SAVE_MODIFF
1325 || windows_or_buffers_changed
1326 || !EQ (current_buffer->selective_display, Qnil)
1327 || detect_input_pending ()
1328 || !NILP (Vexecuting_macro));
1329 value = internal_self_insert (c, 0);
1330 if (value)
1331 lose = 1;
1332 if (value == 2)
1333 nonundocount = 0;
1334
1335 if (!lose
1336 && (PT == ZV || FETCH_CHAR (PT) == '\n'))
1337 {
1338 struct Lisp_Char_Table *dp
1339 = window_display_table (XWINDOW (selected_window));
1340 int lose = c;
1341
1342 if (dp)
1343 {
1344 Lisp_Object obj;
1345
1346 obj = DISP_CHAR_VECTOR (dp, lose);
1347 if (NILP (obj))
1348 {
1349 /* Do it only for char codes
1350 that by default display as themselves. */
1351 if (lose >= 0x20 && lose <= 0x7e)
1352 no_redisplay = direct_output_for_insert (lose);
1353 }
1354 else if (VECTORP (obj)
1355 && XVECTOR (obj)->size == 1
1356 && (obj = XVECTOR (obj)->contents[0],
1357 INTEGERP (obj))
1358 /* Insist face not specified in glyph. */
1359 && (XINT (obj) & ((-1) << 8)) == 0)
1360 no_redisplay
1361 = direct_output_for_insert (XINT (obj));
1362 }
1363 else
1364 {
1365 if (lose >= 0x20 && lose <= 0x7e)
1366 no_redisplay = direct_output_for_insert (lose);
1367 }
1368 }
1369 goto directly_done;
1370 }
1371 }
1372
1373 /* Here for a command that isn't executed directly */
1374
1375 nonundocount = 0;
1376 if (NILP (current_kboard->Vprefix_arg))
1377 Fundo_boundary ();
1378 Fcommand_execute (this_command, Qnil);
1379
1380 }
1381 directly_done: ;
1382
1383 /* Note that the value cell will never directly contain nil
1384 if the symbol is a local variable. */
1385 if (!NILP (XSYMBOL (Qpost_command_hook)->value) && !NILP (Vrun_hooks))
1386 safe_run_hooks (Qpost_command_hook);
1387
1388 if (!NILP (Vdeferred_action_list))
1389 safe_run_hooks (Qdeferred_action_function);
1390
1391 if (!NILP (XSYMBOL (Qpost_command_idle_hook)->value)
1392 && !NILP (Vrun_hooks))
1393 {
1394 if (NILP (Vunread_command_events)
1395 && NILP (Vexecuting_macro)
1396 && !NILP (sit_for (0, post_command_idle_delay, 0, 1)))
1397 safe_run_hooks (Qpost_command_idle_hook);
1398 }
1399
1400 /* If there is a prefix argument,
1401 1) We don't want Vlast_command to be ``universal-argument''
1402 (that would be dumb), so don't set Vlast_command,
1403 2) we want to leave echoing on so that the prefix will be
1404 echoed as part of this key sequence, so don't call
1405 cancel_echoing, and
1406 3) we want to leave this_command_key_count non-zero, so that
1407 read_char will realize that it is re-reading a character, and
1408 not echo it a second time.
1409
1410 If the command didn't actually create a prefix arg,
1411 but is merely a frame event that is transparent to prefix args,
1412 then the above doesn't apply. */
1413 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1414 {
1415 current_kboard->Vlast_command = this_command;
1416 cancel_echoing ();
1417 this_command_key_count = 0;
1418 }
1419
1420 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1421 {
1422 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1423 {
1424 current_buffer->mark_active = Qnil;
1425 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1426 }
1427 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1428 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1429 }
1430
1431 finalize:
1432 /* Install chars successfully executed in kbd macro. */
1433
1434 if (!NILP (current_kboard->defining_kbd_macro)
1435 && NILP (current_kboard->Vprefix_arg))
1436 finalize_kbd_macro_chars ();
1437
1438 #ifdef MULTI_KBOARD
1439 if (!was_locked)
1440 any_kboard_state ();
1441 #endif
1442 }
1443 }
1444
1445 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1446
1447 static Lisp_Object
1448 safe_run_hooks_1 (hook)
1449 Lisp_Object hook;
1450 {
1451 return call1 (Vrun_hooks, Vinhibit_quit);
1452 }
1453
1454 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1455
1456 static Lisp_Object
1457 safe_run_hooks_error (data)
1458 Lisp_Object data;
1459 {
1460 Fset (Vinhibit_quit, Qnil);
1461 }
1462
1463 /* If we get an error while running the hook, cause the hook variable
1464 to be nil. Also inhibit quits, so that C-g won't cause the hook
1465 to mysteriously evaporate. */
1466
1467 void
1468 safe_run_hooks (hook)
1469 Lisp_Object hook;
1470 {
1471 Lisp_Object value;
1472 int count = specpdl_ptr - specpdl;
1473 specbind (Qinhibit_quit, hook);
1474
1475 internal_condition_case (safe_run_hooks_1, Qerror, safe_run_hooks_error);
1476
1477 unbind_to (count, Qnil);
1478 }
1479 \f
1480 /* Number of seconds between polling for input. */
1481 int polling_period;
1482
1483 /* Nonzero means polling for input is temporarily suppressed. */
1484 int poll_suppress_count;
1485
1486 /* Nonzero if polling_for_input is actually being used. */
1487 int polling_for_input;
1488
1489 #ifdef POLL_FOR_INPUT
1490
1491 /* Handle an alarm once each second and read pending input
1492 so as to handle a C-g if it comces in. */
1493
1494 SIGTYPE
1495 input_poll_signal (signalnum) /* If we don't have an argument, */
1496 int signalnum; /* some compilers complain in signal calls. */
1497 {
1498 if (interrupt_input_blocked == 0
1499 && !waiting_for_input)
1500 read_avail_input (0);
1501 signal (SIGALRM, input_poll_signal);
1502 alarm (polling_period);
1503 }
1504
1505 #endif
1506
1507 /* Begin signals to poll for input, if they are appropriate.
1508 This function is called unconditionally from various places. */
1509
1510 start_polling ()
1511 {
1512 #ifdef POLL_FOR_INPUT
1513 if (read_socket_hook && !interrupt_input)
1514 {
1515 poll_suppress_count--;
1516 if (poll_suppress_count == 0)
1517 {
1518 signal (SIGALRM, input_poll_signal);
1519 polling_for_input = 1;
1520 alarm (polling_period);
1521 }
1522 }
1523 #endif
1524 }
1525
1526 /* Nonzero if we are using polling to handle input asynchronously. */
1527
1528 int
1529 input_polling_used ()
1530 {
1531 #ifdef POLL_FOR_INPUT
1532 return read_socket_hook && !interrupt_input;
1533 #else
1534 return 0;
1535 #endif
1536 }
1537
1538 /* Turn off polling. */
1539
1540 stop_polling ()
1541 {
1542 #ifdef POLL_FOR_INPUT
1543 if (read_socket_hook && !interrupt_input)
1544 {
1545 if (poll_suppress_count == 0)
1546 {
1547 polling_for_input = 0;
1548 alarm (0);
1549 }
1550 poll_suppress_count++;
1551 }
1552 #endif
1553 }
1554
1555 /* Set the value of poll_suppress_count to COUNT
1556 and start or stop polling accordingly. */
1557
1558 void
1559 set_poll_suppress_count (count)
1560 int count;
1561 {
1562 #ifdef POLL_FOR_INPUT
1563 if (count == 0 && poll_suppress_count != 0)
1564 {
1565 poll_suppress_count = 1;
1566 start_polling ();
1567 }
1568 else if (count != 0 && poll_suppress_count == 0)
1569 {
1570 stop_polling ();
1571 }
1572 poll_suppress_count = count;
1573 #endif
1574 }
1575
1576 /* Bind polling_period to a value at least N.
1577 But don't decrease it. */
1578
1579 bind_polling_period (n)
1580 int n;
1581 {
1582 #ifdef POLL_FOR_INPUT
1583 int new = polling_period;
1584
1585 if (n > new)
1586 new = n;
1587
1588 stop_polling ();
1589 specbind (Qpolling_period, make_number (new));
1590 /* Start a new alarm with the new period. */
1591 start_polling ();
1592 #endif
1593 }
1594 \f
1595 /* Apply the control modifier to CHARACTER. */
1596
1597 int
1598 make_ctrl_char (c)
1599 int c;
1600 {
1601 /* Save the upper bits here. */
1602 int upper = c & ~0177;
1603
1604 c &= 0177;
1605
1606 /* Everything in the columns containing the upper-case letters
1607 denotes a control character. */
1608 if (c >= 0100 && c < 0140)
1609 {
1610 int oc = c;
1611 c &= ~0140;
1612 /* Set the shift modifier for a control char
1613 made from a shifted letter. But only for letters! */
1614 if (oc >= 'A' && oc <= 'Z')
1615 c |= shift_modifier;
1616 }
1617
1618 /* The lower-case letters denote control characters too. */
1619 else if (c >= 'a' && c <= 'z')
1620 c &= ~0140;
1621
1622 /* Include the bits for control and shift
1623 only if the basic ASCII code can't indicate them. */
1624 else if (c >= ' ')
1625 c |= ctrl_modifier;
1626
1627 /* Replace the high bits. */
1628 c |= (upper & ~ctrl_modifier);
1629
1630 return c;
1631 }
1632
1633
1634 \f
1635 /* Input of single characters from keyboard */
1636
1637 Lisp_Object print_help ();
1638 static Lisp_Object kbd_buffer_get_event ();
1639 static void record_char ();
1640
1641 #ifdef MULTI_KBOARD
1642 static jmp_buf wrong_kboard_jmpbuf;
1643 #endif
1644
1645 /* read a character from the keyboard; call the redisplay if needed */
1646 /* commandflag 0 means do not do auto-saving, but do do redisplay.
1647 -1 means do not do redisplay, but do do autosaving.
1648 1 means do both. */
1649
1650 /* The arguments MAPS and NMAPS are for menu prompting.
1651 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1652
1653 PREV_EVENT is the previous input event, or nil if we are reading
1654 the first event of a key sequence.
1655
1656 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
1657 if we used a mouse menu to read the input, or zero otherwise. If
1658 USED_MOUSE_MENU is null, we don't dereference it.
1659
1660 Value is t if we showed a menu and the user rejected it. */
1661
1662 Lisp_Object
1663 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
1664 int commandflag;
1665 int nmaps;
1666 Lisp_Object *maps;
1667 Lisp_Object prev_event;
1668 int *used_mouse_menu;
1669 {
1670 register Lisp_Object c;
1671 int count;
1672 jmp_buf local_getcjmp;
1673 jmp_buf save_jump;
1674 int key_already_recorded = 0;
1675 Lisp_Object tem, save;
1676 Lisp_Object also_record;
1677 also_record = Qnil;
1678
1679 before_command_key_count = this_command_key_count;
1680 before_command_echo_length = echo_length ();
1681
1682 retry:
1683
1684 if (CONSP (Vunread_command_events))
1685 {
1686 c = XCONS (Vunread_command_events)->car;
1687 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
1688
1689 if (this_command_key_count == 0)
1690 goto reread_first;
1691 else
1692 goto reread;
1693 }
1694
1695 if (unread_command_char != -1)
1696 {
1697 XSETINT (c, unread_command_char);
1698 unread_command_char = -1;
1699
1700 if (this_command_key_count == 0)
1701 goto reread_first;
1702 else
1703 goto reread;
1704 }
1705
1706 /* If there is no function key translated before
1707 reset-this-command-lengths takes effect, forget about it. */
1708 before_command_restore_flag = 0;
1709
1710 if (!NILP (Vexecuting_macro))
1711 {
1712 #ifdef MULTI_FRAME
1713 /* We set this to Qmacro; since that's not a frame, nobody will
1714 try to switch frames on us, and the selected window will
1715 remain unchanged.
1716
1717 Since this event came from a macro, it would be misleading to
1718 leave internal_last_event_frame set to wherever the last
1719 real event came from. Normally, a switch-frame event selects
1720 internal_last_event_frame after each command is read, but
1721 events read from a macro should never cause a new frame to be
1722 selected. */
1723 Vlast_event_frame = internal_last_event_frame = Qmacro;
1724 #endif
1725
1726 /* Exit the macro if we are at the end.
1727 Also, some things replace the macro with t
1728 to force an early exit. */
1729 if (EQ (Vexecuting_macro, Qt)
1730 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
1731 {
1732 XSETINT (c, -1);
1733 return c;
1734 }
1735
1736 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
1737 if (STRINGP (Vexecuting_macro)
1738 && (XINT (c) & 0x80))
1739 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
1740
1741 executing_macro_index++;
1742
1743 goto from_macro;
1744 }
1745
1746 if (!NILP (unread_switch_frame))
1747 {
1748 c = unread_switch_frame;
1749 unread_switch_frame = Qnil;
1750
1751 /* This event should make it into this_command_keys, and get echoed
1752 again, so we go to reread_first, rather than reread. */
1753 goto reread_first;
1754 }
1755
1756 if (commandflag >= 0 && !input_pending && !detect_input_pending ())
1757 redisplay ();
1758
1759 /* Message turns off echoing unless more keystrokes turn it on again. */
1760 if (echo_area_glyphs && *echo_area_glyphs
1761 && echo_area_glyphs != current_kboard->echobuf)
1762 cancel_echoing ();
1763 else
1764 /* If already echoing, continue. */
1765 echo_dash ();
1766
1767 /* Try reading a character via menu prompting in the minibuf.
1768 Try this before the sit-for, because the sit-for
1769 would do the wrong thing if we are supposed to do
1770 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
1771 after a mouse event so don't try a minibuf menu. */
1772 c = Qnil;
1773 if (nmaps > 0 && INTERACTIVE
1774 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
1775 /* Don't bring up a menu if we already have another event. */
1776 && NILP (Vunread_command_events)
1777 && unread_command_char < 0
1778 && !detect_input_pending ())
1779 {
1780 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
1781 if (! NILP (c))
1782 {
1783 key_already_recorded = 1;
1784 goto non_reread_1;
1785 }
1786 }
1787
1788 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
1789 We will do that below, temporarily for short sections of code,
1790 when appropriate. local_getcjmp must be in effect
1791 around any call to sit_for or kbd_buffer_get_event;
1792 it *must not* be in effect when we call redisplay. */
1793
1794 if (_setjmp (local_getcjmp))
1795 {
1796 XSETINT (c, quit_char);
1797 #ifdef MULTI_FRAME
1798 XSETFRAME (internal_last_event_frame, selected_frame);
1799 Vlast_event_frame = internal_last_event_frame;
1800 #endif
1801 /* If we report the quit char as an event,
1802 don't do so more than once. */
1803 if (!NILP (Vinhibit_quit))
1804 Vquit_flag = Qnil;
1805
1806 #ifdef MULTI_KBOARD
1807 {
1808 KBOARD *kb = FRAME_KBOARD (selected_frame);
1809 if (kb != current_kboard)
1810 {
1811 Lisp_Object *tailp = &kb->kbd_queue;
1812 /* We shouldn't get here if we were in single-kboard mode! */
1813 if (single_kboard)
1814 abort ();
1815 while (CONSP (*tailp))
1816 tailp = &XCONS (*tailp)->cdr;
1817 if (!NILP (*tailp))
1818 abort ();
1819 *tailp = Fcons (c, Qnil);
1820 kb->kbd_queue_has_data = 1;
1821 current_kboard = kb;
1822 longjmp (wrong_kboard_jmpbuf, 1);
1823 }
1824 }
1825 #endif
1826 goto non_reread;
1827 }
1828
1829 /* If in middle of key sequence and minibuffer not active,
1830 start echoing if enough time elapses. */
1831
1832 if (minibuf_level == 0 && !current_kboard->immediate_echo
1833 && this_command_key_count > 0
1834 && ! noninteractive
1835 && echo_keystrokes > 0
1836 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0))
1837 {
1838 Lisp_Object tem0;
1839
1840 /* After a mouse event, start echoing right away.
1841 This is because we are probably about to display a menu,
1842 and we don't want to delay before doing so. */
1843 if (EVENT_HAS_PARAMETERS (prev_event))
1844 echo ();
1845 else
1846 {
1847 save_getcjmp (save_jump);
1848 restore_getcjmp (local_getcjmp);
1849 tem0 = sit_for (echo_keystrokes, 0, 1, 1);
1850 restore_getcjmp (save_jump);
1851 if (EQ (tem0, Qt))
1852 echo ();
1853 }
1854 }
1855
1856 /* Maybe auto save due to number of keystrokes. */
1857
1858 if (commandflag != 0
1859 && auto_save_interval > 0
1860 && num_nonmacro_input_chars - last_auto_save > max (auto_save_interval, 20)
1861 && !detect_input_pending ())
1862 {
1863 Fdo_auto_save (Qnil, Qnil);
1864 /* Hooks can actually change some buffers in auto save. */
1865 redisplay ();
1866 }
1867
1868 /* Try reading using an X menu.
1869 This is never confused with reading using the minibuf
1870 because the recursive call of read_char in read_char_minibuf_menu_prompt
1871 does not pass on any keymaps. */
1872
1873 if (nmaps > 0 && INTERACTIVE
1874 && !NILP (prev_event)
1875 && EVENT_HAS_PARAMETERS (prev_event)
1876 && !EQ (XCONS (prev_event)->car, Qmenu_bar)
1877 /* Don't bring up a menu if we already have another event. */
1878 && NILP (Vunread_command_events)
1879 && unread_command_char < 0)
1880 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
1881
1882 /* Maybe autosave and/or garbage collect due to idleness. */
1883
1884 if (INTERACTIVE && NILP (c))
1885 {
1886 int delay_level, buffer_size;
1887
1888 /* Slow down auto saves logarithmically in size of current buffer,
1889 and garbage collect while we're at it. */
1890 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
1891 last_non_minibuf_size = Z - BEG;
1892 buffer_size = (last_non_minibuf_size >> 8) + 1;
1893 delay_level = 0;
1894 while (buffer_size > 64)
1895 delay_level++, buffer_size -= buffer_size >> 2;
1896 if (delay_level < 4) delay_level = 4;
1897 /* delay_level is 4 for files under around 50k, 7 at 100k,
1898 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
1899
1900 /* Auto save if enough time goes by without input. */
1901 if (commandflag != 0
1902 && num_nonmacro_input_chars > last_auto_save
1903 && INTEGERP (Vauto_save_timeout)
1904 && XINT (Vauto_save_timeout) > 0)
1905 {
1906 Lisp_Object tem0;
1907 int delay = delay_level * XFASTINT (Vauto_save_timeout) / 4;
1908
1909 save_getcjmp (save_jump);
1910 restore_getcjmp (local_getcjmp);
1911 tem0 = sit_for (delay, 0, 1, 1);
1912 restore_getcjmp (save_jump);
1913
1914 if (EQ (tem0, Qt))
1915 {
1916 Fdo_auto_save (Qnil, Qnil);
1917
1918 /* If we have auto-saved and there is still no input
1919 available, garbage collect if there has been enough
1920 consing going on to make it worthwhile. */
1921 if (!detect_input_pending ()
1922 && consing_since_gc > gc_cons_threshold / 2)
1923 Fgarbage_collect ();
1924
1925 redisplay ();
1926 }
1927 }
1928 }
1929
1930 /* Read something from current KBOARD's side queue, if possible. */
1931
1932 if (NILP (c))
1933 {
1934 if (current_kboard->kbd_queue_has_data)
1935 {
1936 if (!CONSP (current_kboard->kbd_queue))
1937 abort ();
1938 c = XCONS (current_kboard->kbd_queue)->car;
1939 current_kboard->kbd_queue
1940 = XCONS (current_kboard->kbd_queue)->cdr;
1941 if (NILP (current_kboard->kbd_queue))
1942 current_kboard->kbd_queue_has_data = 0;
1943 input_pending = readable_events ();
1944 #ifdef MULTI_FRAME
1945 if (EVENT_HAS_PARAMETERS (c)
1946 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
1947 internal_last_event_frame = XCONS (XCONS (c)->cdr)->car;
1948 Vlast_event_frame = internal_last_event_frame;
1949 #endif
1950 }
1951 }
1952
1953 #ifdef MULTI_KBOARD
1954 /* If current_kboard's side queue is empty check the other kboards.
1955 If one of them has data that we have not yet seen here,
1956 switch to it and process the data waiting for it.
1957
1958 Note: if the events queued up for another kboard
1959 have already been seen here, and therefore are not a complete command,
1960 the kbd_queue_has_data field is 0, so we skip that kboard here.
1961 That's to avoid an infinite loop switching between kboards here. */
1962 if (NILP (c) && !single_kboard)
1963 {
1964 KBOARD *kb;
1965 for (kb = all_kboards; kb; kb = kb->next_kboard)
1966 if (kb->kbd_queue_has_data)
1967 {
1968 current_kboard = kb;
1969 longjmp (wrong_kboard_jmpbuf, 1);
1970 }
1971 }
1972 #endif
1973
1974 wrong_kboard:
1975
1976 stop_polling ();
1977
1978 /* Finally, we read from the main queue,
1979 and if that gives us something we can't use yet, we put it on the
1980 appropriate side queue and try again. */
1981
1982 if (NILP (c))
1983 {
1984 KBOARD *kb;
1985
1986 /* Actually read a character, waiting if necessary. */
1987 save_getcjmp (save_jump);
1988 restore_getcjmp (local_getcjmp);
1989 c = kbd_buffer_get_event (&kb, used_mouse_menu);
1990 restore_getcjmp (save_jump);
1991
1992 #ifdef MULTI_KBOARD
1993 if (! NILP (c) && (kb != current_kboard))
1994 {
1995 Lisp_Object *tailp = &kb->kbd_queue;
1996 while (CONSP (*tailp))
1997 tailp = &XCONS (*tailp)->cdr;
1998 if (!NILP (*tailp))
1999 abort ();
2000 *tailp = Fcons (c, Qnil);
2001 kb->kbd_queue_has_data = 1;
2002 c = Qnil;
2003 if (single_kboard)
2004 goto wrong_kboard;
2005 current_kboard = kb;
2006 longjmp (wrong_kboard_jmpbuf, 1);
2007 }
2008 #endif
2009 }
2010
2011 /* Terminate Emacs in batch mode if at eof. */
2012 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
2013 Fkill_emacs (make_number (1));
2014
2015 if (INTEGERP (c))
2016 {
2017 /* Add in any extra modifiers, where appropriate. */
2018 if ((extra_keyboard_modifiers & CHAR_CTL)
2019 || ((extra_keyboard_modifiers & 0177) < ' '
2020 && (extra_keyboard_modifiers & 0177) != 0))
2021 XSETINT (c, make_ctrl_char (XINT (c)));
2022
2023 /* Transfer any other modifier bits directly from
2024 extra_keyboard_modifiers to c. Ignore the actual character code
2025 in the low 16 bits of extra_keyboard_modifiers. */
2026 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
2027 }
2028
2029 non_reread:
2030
2031 start_polling ();
2032
2033 if (NILP (c))
2034 {
2035 if (commandflag >= 0
2036 && !input_pending && !detect_input_pending ())
2037 redisplay ();
2038
2039 goto wrong_kboard;
2040 }
2041
2042 non_reread_1:
2043
2044 /* Buffer switch events are only for internal wakeups
2045 so don't show them to the user. */
2046 if (BUFFERP (c))
2047 return c;
2048
2049 if (key_already_recorded)
2050 return c;
2051
2052 /* Process special events within read_char
2053 and loop around to read another event. */
2054 save = Vquit_flag;
2055 Vquit_flag = Qnil;
2056 tem = get_keyelt (access_keymap (get_keymap_1 (Vspecial_event_map, 0, 0),
2057 c, 0, 0), 1);
2058 Vquit_flag = save;
2059
2060 if (!NILP (tem))
2061 {
2062 last_input_char = c;
2063 Fcommand_execute (tem, Qnil);
2064 goto retry;
2065 }
2066
2067 /* Wipe the echo area. */
2068 echo_area_glyphs = 0;
2069
2070 /* Handle things that only apply to characters. */
2071 if (INTEGERP (c))
2072 {
2073 /* If kbd_buffer_get_event gave us an EOF, return that. */
2074 if (XINT (c) == -1)
2075 return c;
2076
2077 if (STRINGP (Vkeyboard_translate_table)
2078 && XSTRING (Vkeyboard_translate_table)->size > XFASTINT (c))
2079 XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[XFASTINT (c)]);
2080 }
2081
2082 /* If this event is a mouse click in the menu bar,
2083 return just menu-bar for now. Modify the mouse click event
2084 so we won't do this twice, then queue it up. */
2085 if (EVENT_HAS_PARAMETERS (c)
2086 && CONSP (XCONS (c)->cdr)
2087 && CONSP (EVENT_START (c))
2088 && CONSP (XCONS (EVENT_START (c))->cdr))
2089 {
2090 Lisp_Object posn;
2091
2092 posn = POSN_BUFFER_POSN (EVENT_START (c));
2093 /* Handle menu-bar events:
2094 insert the dummy prefix event `menu-bar'. */
2095 if (EQ (posn, Qmenu_bar))
2096 {
2097 /* Change menu-bar to (menu-bar) as the event "position". */
2098 POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
2099
2100 also_record = c;
2101 Vunread_command_events = Fcons (c, Vunread_command_events);
2102 c = posn;
2103 }
2104 }
2105
2106 record_char (c);
2107 if (! NILP (also_record))
2108 record_char (also_record);
2109
2110 from_macro:
2111 reread_first:
2112 before_command_key_count = this_command_key_count;
2113 before_command_echo_length = echo_length ();
2114
2115 /* Don't echo mouse motion events. */
2116 if (echo_keystrokes
2117 && ! (EVENT_HAS_PARAMETERS (c)
2118 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
2119 {
2120 echo_char (c);
2121 if (! NILP (also_record))
2122 echo_char (also_record);
2123 }
2124
2125 /* Record this character as part of the current key. */
2126 add_command_key (c);
2127 if (! NILP (also_record))
2128 add_command_key (also_record);
2129
2130 /* Re-reading in the middle of a command */
2131 reread:
2132 last_input_char = c;
2133 num_input_chars++;
2134
2135 /* Process the help character specially if enabled */
2136 if (!NILP (Vhelp_form) && help_char_p (c))
2137 {
2138 Lisp_Object tem0;
2139 count = specpdl_ptr - specpdl;
2140
2141 record_unwind_protect (Fset_window_configuration,
2142 Fcurrent_window_configuration (Qnil));
2143
2144 tem0 = Feval (Vhelp_form);
2145 if (STRINGP (tem0))
2146 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
2147
2148 cancel_echoing ();
2149 do
2150 c = read_char (0, 0, 0, Qnil, 0);
2151 while (BUFFERP (c));
2152 /* Remove the help from the frame */
2153 unbind_to (count, Qnil);
2154
2155 redisplay ();
2156 if (EQ (c, make_number (040)))
2157 {
2158 cancel_echoing ();
2159 do
2160 c = read_char (0, 0, 0, Qnil, 0);
2161 while (BUFFERP (c));
2162 }
2163 }
2164
2165 return c;
2166 }
2167
2168 /* Return 1 if should recognize C as "the help character". */
2169
2170 int
2171 help_char_p (c)
2172 Lisp_Object c;
2173 {
2174 Lisp_Object tail;
2175
2176 if (EQ (c, Vhelp_char))
2177 return 1;
2178 for (tail = Vhelp_event_list; CONSP (tail); tail = XCONS (tail)->cdr)
2179 if (EQ (c, XCONS (tail)->car))
2180 return 1;
2181 return 0;
2182 }
2183
2184 /* Record the input event C in various ways. */
2185
2186 static void
2187 record_char (c)
2188 Lisp_Object c;
2189 {
2190 total_keys++;
2191 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
2192 if (++recent_keys_index >= NUM_RECENT_KEYS)
2193 recent_keys_index = 0;
2194
2195 /* Write c to the dribble file. If c is a lispy event, write
2196 the event's symbol to the dribble file, in <brackets>. Bleaugh.
2197 If you, dear reader, have a better idea, you've got the source. :-) */
2198 if (dribble)
2199 {
2200 if (INTEGERP (c))
2201 {
2202 if (XUINT (c) < 0x100)
2203 putc (XINT (c), dribble);
2204 else
2205 fprintf (dribble, " 0x%x", (int) XUINT (c));
2206 }
2207 else
2208 {
2209 Lisp_Object dribblee;
2210
2211 /* If it's a structured event, take the event header. */
2212 dribblee = EVENT_HEAD (c);
2213
2214 if (SYMBOLP (dribblee))
2215 {
2216 putc ('<', dribble);
2217 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
2218 XSYMBOL (dribblee)->name->size,
2219 dribble);
2220 putc ('>', dribble);
2221 }
2222 }
2223
2224 fflush (dribble);
2225 }
2226
2227 store_kbd_macro_char (c);
2228
2229 num_nonmacro_input_chars++;
2230 }
2231
2232 Lisp_Object
2233 print_help (object)
2234 Lisp_Object object;
2235 {
2236 struct buffer *old = current_buffer;
2237 Fprinc (object, Qnil);
2238 set_buffer_internal (XBUFFER (Vstandard_output));
2239 call0 (intern ("help-mode"));
2240 set_buffer_internal (old);
2241 return Qnil;
2242 }
2243
2244 /* Copy out or in the info on where C-g should throw to.
2245 This is used when running Lisp code from within get_char,
2246 in case get_char is called recursively.
2247 See read_process_output. */
2248
2249 save_getcjmp (temp)
2250 jmp_buf temp;
2251 {
2252 bcopy (getcjmp, temp, sizeof getcjmp);
2253 }
2254
2255 restore_getcjmp (temp)
2256 jmp_buf temp;
2257 {
2258 bcopy (temp, getcjmp, sizeof getcjmp);
2259 }
2260 \f
2261 #ifdef HAVE_MOUSE
2262
2263 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
2264 of this function. */
2265
2266 static Lisp_Object
2267 tracking_off (old_value)
2268 Lisp_Object old_value;
2269 {
2270 do_mouse_tracking = old_value;
2271 if (NILP (old_value))
2272 {
2273 /* Redisplay may have been preempted because there was input
2274 available, and it assumes it will be called again after the
2275 input has been processed. If the only input available was
2276 the sort that we have just disabled, then we need to call
2277 redisplay. */
2278 if (!readable_events ())
2279 {
2280 redisplay_preserve_echo_area ();
2281 get_input_pending (&input_pending);
2282 }
2283 }
2284 }
2285
2286 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
2287 "Evaluate BODY with mouse movement events enabled.\n\
2288 Within a `track-mouse' form, mouse motion generates input events that\n\
2289 you can read with `read-event'.\n\
2290 Normally, mouse motion is ignored.")
2291 (args)
2292 Lisp_Object args;
2293 {
2294 int count = specpdl_ptr - specpdl;
2295 Lisp_Object val;
2296
2297 record_unwind_protect (tracking_off, do_mouse_tracking);
2298
2299 do_mouse_tracking = Qt;
2300
2301 val = Fprogn (args);
2302 return unbind_to (count, val);
2303 }
2304
2305 /* If mouse has moved on some frame, return one of those frames.
2306 Return 0 otherwise. */
2307
2308 static FRAME_PTR
2309 some_mouse_moved ()
2310 {
2311 Lisp_Object tail, frame;
2312
2313 FOR_EACH_FRAME (tail, frame)
2314 {
2315 if (XFRAME (frame)->mouse_moved)
2316 return XFRAME (frame);
2317 }
2318
2319 return 0;
2320 }
2321
2322 #endif /* HAVE_MOUSE */
2323 \f
2324 /* Low level keyboard/mouse input.
2325 kbd_buffer_store_event places events in kbd_buffer, and
2326 kbd_buffer_get_event retrieves them.
2327 mouse_moved indicates when the mouse has moved again, and
2328 *mouse_position_hook provides the mouse position. */
2329
2330 /* Return true iff there are any events in the queue that read-char
2331 would return. If this returns false, a read-char would block. */
2332 static int
2333 readable_events ()
2334 {
2335 if (kbd_fetch_ptr != kbd_store_ptr)
2336 return 1;
2337 #ifdef HAVE_MOUSE
2338 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2339 return 1;
2340 #endif
2341 if (single_kboard)
2342 {
2343 if (current_kboard->kbd_queue_has_data)
2344 return 1;
2345 }
2346 else
2347 {
2348 KBOARD *kb;
2349 for (kb = all_kboards; kb; kb = kb->next_kboard)
2350 if (kb->kbd_queue_has_data)
2351 return 1;
2352 }
2353 return 0;
2354 }
2355
2356 /* Set this for debugging, to have a way to get out */
2357 int stop_character;
2358
2359 #ifdef MULTI_KBOARD
2360 static KBOARD *
2361 event_to_kboard (event)
2362 struct input_event *event;
2363 {
2364 Lisp_Object frame;
2365 frame = event->frame_or_window;
2366 if (CONSP (frame))
2367 frame = XCONS (frame)->car;
2368 else if (WINDOWP (frame))
2369 frame = WINDOW_FRAME (XWINDOW (frame));
2370
2371 /* There are still some events that don't set this field.
2372 For now, just ignore the problem.
2373 Also ignore dead frames here. */
2374 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
2375 return 0;
2376 else
2377 return FRAME_KBOARD (XFRAME (frame));
2378 }
2379 #endif
2380
2381 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
2382
2383 void
2384 kbd_buffer_store_event (event)
2385 register struct input_event *event;
2386 {
2387 if (event->kind == no_event)
2388 abort ();
2389
2390 if (event->kind == ascii_keystroke)
2391 {
2392 register int c = event->code & 0377;
2393
2394 if (event->modifiers & ctrl_modifier)
2395 c = make_ctrl_char (c);
2396
2397 c |= (event->modifiers
2398 & (meta_modifier | alt_modifier
2399 | hyper_modifier | super_modifier));
2400
2401 if (c == quit_char)
2402 {
2403 extern SIGTYPE interrupt_signal ();
2404 #ifdef MULTI_KBOARD
2405 KBOARD *kb;
2406 struct input_event *sp;
2407
2408 if (single_kboard
2409 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
2410 kb != current_kboard))
2411 {
2412 kb->kbd_queue
2413 = Fcons (make_lispy_switch_frame (event->frame_or_window),
2414 Fcons (make_number (c), Qnil));
2415 kb->kbd_queue_has_data = 1;
2416 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
2417 {
2418 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
2419 sp = kbd_buffer;
2420
2421 if (event_to_kboard (sp) == kb)
2422 {
2423 sp->kind = no_event;
2424 sp->frame_or_window = Qnil;
2425 }
2426 }
2427 return;
2428 }
2429 #endif
2430
2431 #ifdef MULTI_FRAME
2432 /* If this results in a quit_char being returned to Emacs as
2433 input, set Vlast_event_frame properly. If this doesn't
2434 get returned to Emacs as an event, the next event read
2435 will set Vlast_event_frame again, so this is safe to do. */
2436 {
2437 Lisp_Object focus;
2438
2439 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
2440 if (NILP (focus))
2441 focus = event->frame_or_window;
2442 internal_last_event_frame = focus;
2443 Vlast_event_frame = focus;
2444 }
2445 #endif
2446
2447 last_event_timestamp = event->timestamp;
2448 interrupt_signal ();
2449 return;
2450 }
2451
2452 if (c && c == stop_character)
2453 {
2454 sys_suspend ();
2455 return;
2456 }
2457 }
2458 /* Don't insert two buffer_switch_event's in a row.
2459 Just ignore the second one. */
2460 else if (event->kind == buffer_switch_event
2461 && kbd_fetch_ptr != kbd_store_ptr
2462 && kbd_store_ptr->kind == buffer_switch_event)
2463 return;
2464
2465 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
2466 kbd_store_ptr = kbd_buffer;
2467
2468 /* Don't let the very last slot in the buffer become full,
2469 since that would make the two pointers equal,
2470 and that is indistinguishable from an empty buffer.
2471 Discard the event if it would fill the last slot. */
2472 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
2473 {
2474 volatile struct input_event *sp = kbd_store_ptr;
2475 sp->kind = event->kind;
2476 if (event->kind == selection_request_event)
2477 {
2478 /* We must not use the ordinary copying code for this case,
2479 since `part' is an enum and copying it might not copy enough
2480 in this case. */
2481 bcopy (event, (char *) sp, sizeof (*event));
2482 }
2483 else
2484 {
2485 sp->code = event->code;
2486 sp->part = event->part;
2487 sp->frame_or_window = event->frame_or_window;
2488 sp->modifiers = event->modifiers;
2489 sp->x = event->x;
2490 sp->y = event->y;
2491 sp->timestamp = event->timestamp;
2492 }
2493 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
2494 - kbd_buffer]
2495 = event->frame_or_window);
2496
2497 kbd_store_ptr++;
2498 }
2499 }
2500 \f
2501 /* Read one event from the event buffer, waiting if necessary.
2502 The value is a Lisp object representing the event.
2503 The value is nil for an event that should be ignored,
2504 or that was handled here.
2505 We always read and discard one event. */
2506
2507 static Lisp_Object
2508 kbd_buffer_get_event (kbp, used_mouse_menu)
2509 KBOARD **kbp;
2510 int *used_mouse_menu;
2511 {
2512 register int c;
2513 Lisp_Object obj;
2514
2515 if (noninteractive)
2516 {
2517 c = getchar ();
2518 XSETINT (obj, c);
2519 *kbp = current_kboard;
2520 return obj;
2521 }
2522
2523 /* Wait until there is input available. */
2524 for (;;)
2525 {
2526 if (kbd_fetch_ptr != kbd_store_ptr)
2527 break;
2528 #ifdef HAVE_MOUSE
2529 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2530 break;
2531 #endif
2532
2533 /* If the quit flag is set, then read_char will return
2534 quit_char, so that counts as "available input." */
2535 if (!NILP (Vquit_flag))
2536 quit_throw_to_read_char ();
2537
2538 /* One way or another, wait until input is available; then, if
2539 interrupt handlers have not read it, read it now. */
2540
2541 #ifdef OLDVMS
2542 wait_for_kbd_input ();
2543 #else
2544 /* Note SIGIO has been undef'd if FIONREAD is missing. */
2545 #ifdef SIGIO
2546 gobble_input (0);
2547 #endif /* SIGIO */
2548 if (kbd_fetch_ptr != kbd_store_ptr)
2549 break;
2550 #ifdef HAVE_MOUSE
2551 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2552 break;
2553 #endif
2554 {
2555 Lisp_Object minus_one;
2556
2557 XSETINT (minus_one, -1);
2558 wait_reading_process_input (0, 0, minus_one, 1);
2559
2560 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
2561 /* Pass 1 for EXPECT since we just waited to have input. */
2562 read_avail_input (1);
2563 }
2564 #endif /* not VMS */
2565 }
2566
2567 /* At this point, we know that there is a readable event available
2568 somewhere. If the event queue is empty, then there must be a
2569 mouse movement enabled and available. */
2570 if (kbd_fetch_ptr != kbd_store_ptr)
2571 {
2572 struct input_event *event;
2573
2574 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2575 ? kbd_fetch_ptr
2576 : kbd_buffer);
2577
2578 last_event_timestamp = event->timestamp;
2579
2580 #ifdef MULTI_KBOARD
2581 *kbp = event_to_kboard (event);
2582 if (*kbp == 0)
2583 *kbp = current_kboard; /* Better than returning null ptr? */
2584 #else
2585 *kbp = &the_only_kboard;
2586 #endif
2587
2588 obj = Qnil;
2589
2590 /* These two kinds of events get special handling
2591 and don't actually appear to the command loop.
2592 We return nil for them. */
2593 if (event->kind == selection_request_event)
2594 {
2595 #ifdef HAVE_X11
2596 struct input_event copy;
2597
2598 /* Remove it from the buffer before processing it,
2599 since otherwise swallow_events will see it
2600 and process it again. */
2601 copy = *event;
2602 kbd_fetch_ptr = event + 1;
2603 input_pending = readable_events ();
2604 x_handle_selection_request (&copy);
2605 #else
2606 /* We're getting selection request events, but we don't have
2607 a window system. */
2608 abort ();
2609 #endif
2610 }
2611
2612 else if (event->kind == selection_clear_event)
2613 {
2614 #ifdef HAVE_X11
2615 struct input_event copy;
2616
2617 /* Remove it from the buffer before processing it. */
2618 copy = *event;
2619 kbd_fetch_ptr = event + 1;
2620 input_pending = readable_events ();
2621 x_handle_selection_clear (&copy);
2622 #else
2623 /* We're getting selection request events, but we don't have
2624 a window system. */
2625 abort ();
2626 #endif
2627 }
2628 #ifdef HAVE_X11
2629 else if (event->kind == delete_window_event)
2630 {
2631 /* Make an event (delete-frame (FRAME)). */
2632 obj = Fcons (event->frame_or_window, Qnil);
2633 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
2634 kbd_fetch_ptr = event + 1;
2635 }
2636 else if (event->kind == iconify_event)
2637 {
2638 /* Make an event (iconify-frame (FRAME)). */
2639 obj = Fcons (event->frame_or_window, Qnil);
2640 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
2641 kbd_fetch_ptr = event + 1;
2642 }
2643 else if (event->kind == deiconify_event)
2644 {
2645 /* Make an event (make-frame-visible (FRAME)). */
2646 obj = Fcons (event->frame_or_window, Qnil);
2647 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
2648 kbd_fetch_ptr = event + 1;
2649 }
2650 #endif
2651 else if (event->kind == buffer_switch_event)
2652 {
2653 /* The value doesn't matter here; only the type is tested. */
2654 XSETBUFFER (obj, current_buffer);
2655 kbd_fetch_ptr = event + 1;
2656 }
2657 #ifdef USE_X_TOOLKIT
2658 else if (event->kind == menu_bar_activate_event)
2659 {
2660 kbd_fetch_ptr = event + 1;
2661 input_pending = readable_events ();
2662 x_activate_menubar (XFRAME (event->frame_or_window));
2663 }
2664 #endif
2665 /* Just discard these, by returning nil.
2666 With MULTI_KBOARD, these events are used as placeholders
2667 when we need to randomly delete events from the queue.
2668 (They shouldn't otherwise be found in the buffer,
2669 but on some machines it appears they do show up
2670 even without MULTI_KBOARD.) */
2671 else if (event->kind == no_event)
2672 kbd_fetch_ptr = event + 1;
2673
2674 /* If this event is on a different frame, return a switch-frame this
2675 time, and leave the event in the queue for next time. */
2676 else
2677 {
2678 #ifdef MULTI_FRAME
2679 Lisp_Object frame;
2680 Lisp_Object focus;
2681
2682 frame = event->frame_or_window;
2683 if (CONSP (frame))
2684 frame = XCONS (frame)->car;
2685 else if (WINDOWP (frame))
2686 frame = WINDOW_FRAME (XWINDOW (frame));
2687
2688 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
2689 if (! NILP (focus))
2690 frame = focus;
2691
2692 if (! EQ (frame, internal_last_event_frame)
2693 && XFRAME (frame) != selected_frame)
2694 obj = make_lispy_switch_frame (frame);
2695 internal_last_event_frame = frame;
2696 #endif /* MULTI_FRAME */
2697
2698 /* If we didn't decide to make a switch-frame event, go ahead
2699 and build a real event from the queue entry. */
2700
2701 if (NILP (obj))
2702 {
2703 obj = make_lispy_event (event);
2704 #ifdef USE_X_TOOLKIT
2705 /* If this was a menu selection, then set the flag to inhibit
2706 writing to last_nonmenu_event. Don't do this if the event
2707 we're returning is (menu-bar), though; that indicates the
2708 beginning of the menu sequence, and we might as well leave
2709 that as the `event with parameters' for this selection. */
2710 if (event->kind == menu_bar_event
2711 && !(CONSP (obj) && EQ (XCONS (obj)->car, Qmenu_bar))
2712 && used_mouse_menu)
2713 *used_mouse_menu = 1;
2714 #endif
2715
2716 /* Wipe out this event, to catch bugs. */
2717 event->kind = no_event;
2718 XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer] = Qnil;
2719
2720 kbd_fetch_ptr = event + 1;
2721 }
2722 }
2723 }
2724 #ifdef HAVE_MOUSE
2725 /* Try generating a mouse motion event. */
2726 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2727 {
2728 FRAME_PTR f = some_mouse_moved ();
2729 Lisp_Object bar_window;
2730 enum scroll_bar_part part;
2731 Lisp_Object x, y;
2732 unsigned long time;
2733
2734 *kbp = current_kboard;
2735 /* Note that this uses F to determine which display to look at.
2736 If there is no valid info, it does not store anything
2737 so x remains nil. */
2738 x = Qnil;
2739 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
2740
2741 obj = Qnil;
2742
2743 #ifdef MULTI_FRAME
2744 /* Decide if we should generate a switch-frame event. Don't
2745 generate switch-frame events for motion outside of all Emacs
2746 frames. */
2747 if (!NILP (x) && f)
2748 {
2749 Lisp_Object frame;
2750
2751 frame = FRAME_FOCUS_FRAME (f);
2752 if (NILP (frame))
2753 XSETFRAME (frame, f);
2754
2755 if (! EQ (frame, internal_last_event_frame)
2756 && XFRAME (frame) != selected_frame)
2757 obj = make_lispy_switch_frame (frame);
2758 internal_last_event_frame = frame;
2759 }
2760 #endif
2761
2762 /* If we didn't decide to make a switch-frame event, go ahead and
2763 return a mouse-motion event. */
2764 if (!NILP (x) && NILP (obj))
2765 obj = make_lispy_movement (f, bar_window, part, x, y, time);
2766 }
2767 #endif /* HAVE_MOUSE */
2768 else
2769 /* We were promised by the above while loop that there was
2770 something for us to read! */
2771 abort ();
2772
2773 input_pending = readable_events ();
2774
2775 #ifdef MULTI_FRAME
2776 Vlast_event_frame = internal_last_event_frame;
2777 #endif
2778
2779 return (obj);
2780 }
2781 \f
2782 /* Process any events that are not user-visible,
2783 then return, without reading any user-visible events. */
2784
2785 void
2786 swallow_events ()
2787 {
2788 while (kbd_fetch_ptr != kbd_store_ptr)
2789 {
2790 struct input_event *event;
2791
2792 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2793 ? kbd_fetch_ptr
2794 : kbd_buffer);
2795
2796 last_event_timestamp = event->timestamp;
2797
2798 /* These two kinds of events get special handling
2799 and don't actually appear to the command loop. */
2800 if (event->kind == selection_request_event)
2801 {
2802 #ifdef HAVE_X11
2803 struct input_event copy;
2804
2805 /* Remove it from the buffer before processing it,
2806 since otherwise swallow_events called recursively could see it
2807 and process it again. */
2808 copy = *event;
2809 kbd_fetch_ptr = event + 1;
2810 input_pending = readable_events ();
2811 x_handle_selection_request (&copy);
2812 #else
2813 /* We're getting selection request events, but we don't have
2814 a window system. */
2815 abort ();
2816 #endif
2817 }
2818
2819 else if (event->kind == selection_clear_event)
2820 {
2821 #ifdef HAVE_X11
2822 struct input_event copy;
2823
2824 /* Remove it from the buffer before processing it, */
2825 copy = *event;
2826
2827 kbd_fetch_ptr = event + 1;
2828 input_pending = readable_events ();
2829 x_handle_selection_clear (&copy);
2830 #else
2831 /* We're getting selection request events, but we don't have
2832 a window system. */
2833 abort ();
2834 #endif
2835 }
2836 else
2837 break;
2838 }
2839
2840 get_input_pending (&input_pending);
2841 }
2842 \f
2843 /* Caches for modify_event_symbol. */
2844 static Lisp_Object accent_key_syms;
2845 static Lisp_Object func_key_syms;
2846 static Lisp_Object mouse_syms;
2847
2848 /* This is a list of keysym codes for special "accent" characters.
2849 It parallels lispy_accent_keys. */
2850
2851 static int lispy_accent_codes[] =
2852 {
2853 #ifdef XK_dead_circumflex
2854 XK_dead_circumflex,
2855 #else
2856 0,
2857 #endif
2858 #ifdef XK_dead_grave
2859 XK_dead_grave,
2860 #else
2861 0,
2862 #endif
2863 #ifdef XK_dead_tilde
2864 XK_dead_tilde,
2865 #else
2866 0,
2867 #endif
2868 #ifdef XK_dead_diaeresis
2869 XK_dead_diaeresis,
2870 #else
2871 0,
2872 #endif
2873 #ifdef XK_dead_macron
2874 XK_dead_macron,
2875 #else
2876 0,
2877 #endif
2878 #ifdef XK_dead_degree
2879 XK_dead_degree,
2880 #else
2881 0,
2882 #endif
2883 #ifdef XK_dead_acute
2884 XK_dead_acute,
2885 #else
2886 0,
2887 #endif
2888 #ifdef XK_dead_cedilla
2889 XK_dead_cedilla,
2890 #else
2891 0,
2892 #endif
2893 #ifdef XK_dead_breve
2894 XK_dead_breve,
2895 #else
2896 0,
2897 #endif
2898 #ifdef XK_dead_ogonek
2899 XK_dead_ogonek,
2900 #else
2901 0,
2902 #endif
2903 #ifdef XK_dead_caron
2904 XK_dead_caron,
2905 #else
2906 0,
2907 #endif
2908 #ifdef XK_dead_doubleacute
2909 XK_dead_doubleacute,
2910 #else
2911 0,
2912 #endif
2913 #ifdef XK_dead_abovedot
2914 XK_dead_abovedot,
2915 #else
2916 0,
2917 #endif
2918 };
2919
2920 /* This is a list of Lisp names for special "accent" characters.
2921 It parallels lispy_accent_codes. */
2922
2923 static char *lispy_accent_keys[] =
2924 {
2925 "dead-circumflex",
2926 "dead-grave",
2927 "dead-tilde",
2928 "dead-diaeresis",
2929 "dead-macron",
2930 "dead-degree",
2931 "dead-acute",
2932 "dead-cedilla",
2933 "dead-breve",
2934 "dead-ogonek",
2935 "dead-caron",
2936 "dead-doubleacute",
2937 "dead-abovedot",
2938 };
2939
2940 /* You'll notice that this table is arranged to be conveniently
2941 indexed by X Windows keysym values. */
2942 static char *lispy_function_keys[] =
2943 {
2944 /* X Keysym value */
2945
2946 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00 */
2947 "backspace",
2948 "tab",
2949 "linefeed",
2950 "clear",
2951 0,
2952 "return",
2953 0, 0,
2954 0, 0, 0, /* 0xff10 */
2955 "pause",
2956 0, 0, 0, 0, 0, 0, 0,
2957 "escape",
2958 0, 0, 0, 0,
2959 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff20...2f */
2960 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff30...3f */
2961 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
2962
2963 "home", /* 0xff50 */ /* IsCursorKey */
2964 "left",
2965 "up",
2966 "right",
2967 "down",
2968 "prior",
2969 "next",
2970 "end",
2971 "begin",
2972 0, /* 0xff59 */
2973 0, 0, 0, 0, 0, 0,
2974 "select", /* 0xff60 */ /* IsMiscFunctionKey */
2975 "print",
2976 "execute",
2977 "insert",
2978 0, /* 0xff64 */
2979 "undo",
2980 "redo",
2981 "menu",
2982 "find",
2983 "cancel",
2984 "help",
2985 "break", /* 0xff6b */
2986
2987 0, 0, 0, 0, 0, 0, 0, 0, "backtab", 0,
2988 0, /* 0xff76 */
2989 0, 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff7f */
2990 "kp-space", /* 0xff80 */ /* IsKeypadKey */
2991 0, 0, 0, 0, 0, 0, 0, 0,
2992 "kp-tab", /* 0xff89 */
2993 0, 0, 0,
2994 "kp-enter", /* 0xff8d */
2995 0, 0, 0,
2996 "kp-f1", /* 0xff91 */
2997 "kp-f2",
2998 "kp-f3",
2999 "kp-f4",
3000 "kp-home", /* 0xff95 */
3001 "kp-left",
3002 "kp-up",
3003 "kp-right",
3004 "kp-down",
3005 "kp-prior", /* kp-page-up */
3006 "kp-next", /* kp-page-down */
3007 "kp-end",
3008 "kp-begin",
3009 "kp-insert",
3010 "kp-delete",
3011 0, /* 0xffa0 */
3012 0, 0, 0, 0, 0, 0, 0, 0, 0,
3013 "kp-multiply", /* 0xffaa */
3014 "kp-add",
3015 "kp-separator",
3016 "kp-subtract",
3017 "kp-decimal",
3018 "kp-divide", /* 0xffaf */
3019 "kp-0", /* 0xffb0 */
3020 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
3021 0, /* 0xffba */
3022 0, 0,
3023 "kp-equal", /* 0xffbd */
3024 "f1", /* 0xffbe */ /* IsFunctionKey */
3025 "f2",
3026 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
3027 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
3028 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
3029 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
3030 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
3031 0, 0, 0, 0, 0, 0, 0, 0,
3032 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
3033 0, 0, 0, 0, 0, 0, 0, "delete"
3034 };
3035
3036 static char *lispy_mouse_names[] =
3037 {
3038 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
3039 };
3040
3041 /* Scroll bar parts. */
3042 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
3043
3044 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
3045 Lisp_Object *scroll_bar_parts[] = {
3046 &Qabove_handle, &Qhandle, &Qbelow_handle
3047 };
3048
3049
3050 /* A vector, indexed by button number, giving the down-going location
3051 of currently depressed buttons, both scroll bar and non-scroll bar.
3052
3053 The elements have the form
3054 (BUTTON-NUMBER MODIFIER-MASK . REST)
3055 where REST is the cdr of a position as it would be reported in the event.
3056
3057 The make_lispy_event function stores positions here to tell the
3058 difference between click and drag events, and to store the starting
3059 location to be included in drag events. */
3060
3061 static Lisp_Object button_down_location;
3062
3063 /* Information about the most recent up-going button event: Which
3064 button, what location, and what time. */
3065
3066 static int last_mouse_button;
3067 static int last_mouse_x;
3068 static int last_mouse_y;
3069 static unsigned long button_down_time;
3070
3071 /* The maximum time between clicks to make a double-click,
3072 or Qnil to disable double-click detection,
3073 or Qt for no time limit. */
3074 Lisp_Object Vdouble_click_time;
3075
3076 /* The number of clicks in this multiple-click. */
3077
3078 int double_click_count;
3079
3080 /* Given a struct input_event, build the lisp event which represents
3081 it. If EVENT is 0, build a mouse movement event from the mouse
3082 movement buffer, which should have a movement event in it.
3083
3084 Note that events must be passed to this function in the order they
3085 are received; this function stores the location of button presses
3086 in order to build drag events when the button is released. */
3087
3088 static Lisp_Object
3089 make_lispy_event (event)
3090 struct input_event *event;
3091 {
3092 int i;
3093
3094 switch (SWITCH_ENUM_CAST (event->kind))
3095 {
3096 /* A simple keystroke. */
3097 case ascii_keystroke:
3098 {
3099 Lisp_Object lispy_c;
3100 int c = event->code & 0377;
3101 /* Turn ASCII characters into control characters
3102 when proper. */
3103 if (event->modifiers & ctrl_modifier)
3104 c = make_ctrl_char (c);
3105
3106 /* Add in the other modifier bits. We took care of ctrl_modifier
3107 just above, and the shift key was taken care of by the X code,
3108 and applied to control characters by make_ctrl_char. */
3109 c |= (event->modifiers
3110 & (meta_modifier | alt_modifier
3111 | hyper_modifier | super_modifier));
3112 button_down_time = 0;
3113 XSETFASTINT (lispy_c, c);
3114 return lispy_c;
3115 }
3116
3117 /* A function key. The symbol may need to have modifier prefixes
3118 tacked onto it. */
3119 case non_ascii_keystroke:
3120 button_down_time = 0;
3121
3122 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
3123 if (event->code == lispy_accent_codes[i])
3124 return modify_event_symbol (i,
3125 event->modifiers,
3126 Qfunction_key, Qnil,
3127 lispy_accent_keys, &accent_key_syms,
3128 (sizeof (lispy_accent_keys)
3129 / sizeof (lispy_accent_keys[0])));
3130
3131 /* Handle system-specific keysyms. */
3132 if (event->code & (1 << 28))
3133 {
3134 /* We need to use an alist rather than a vector as the cache
3135 since we can't make a vector long enuf. */
3136 if (NILP (current_kboard->system_key_syms))
3137 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
3138 return modify_event_symbol (event->code,
3139 event->modifiers,
3140 Qfunction_key,
3141 current_kboard->Vsystem_key_alist,
3142 0, &current_kboard->system_key_syms,
3143 (unsigned)-1);
3144 }
3145
3146 return modify_event_symbol (event->code - 0xff00,
3147 event->modifiers,
3148 Qfunction_key, Qnil,
3149 lispy_function_keys, &func_key_syms,
3150 (sizeof (lispy_function_keys)
3151 / sizeof (lispy_function_keys[0])));
3152 break;
3153
3154 #ifdef HAVE_MOUSE
3155 /* A mouse click. Figure out where it is, decide whether it's
3156 a press, click or drag, and build the appropriate structure. */
3157 case mouse_click:
3158 case scroll_bar_click:
3159 {
3160 int button = event->code;
3161 int is_double;
3162 Lisp_Object position;
3163 Lisp_Object *start_pos_ptr;
3164 Lisp_Object start_pos;
3165
3166 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
3167 abort ();
3168
3169 /* Build the position as appropriate for this mouse click. */
3170 if (event->kind == mouse_click)
3171 {
3172 int part;
3173 FRAME_PTR f = XFRAME (event->frame_or_window);
3174 Lisp_Object window;
3175 Lisp_Object posn;
3176 int row, column;
3177
3178 /* Ignore mouse events that were made on frame that
3179 have been deleted. */
3180 if (! FRAME_LIVE_P (f))
3181 return Qnil;
3182
3183 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
3184 &column, &row, NULL, 1);
3185
3186 #ifndef USE_X_TOOLKIT
3187 /* In the non-toolkit version, clicks on the menu bar
3188 are ordinary button events in the event buffer.
3189 Distinguish them, and invoke the menu.
3190
3191 (In the toolkit version, the toolkit handles the menu bar
3192 and Emacs doesn't know about it until after the user
3193 makes a selection.) */
3194 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
3195 && (event->modifiers & down_modifier))
3196 {
3197 Lisp_Object items, item;
3198 int hpos;
3199 int i;
3200
3201 #if 0
3202 /* Activate the menu bar on the down event. If the
3203 up event comes in before the menu code can deal with it,
3204 just ignore it. */
3205 if (! (event->modifiers & down_modifier))
3206 return Qnil;
3207 #endif
3208
3209 item = Qnil;
3210 items = FRAME_MENU_BAR_ITEMS (f);
3211 for (i = 0; i < XVECTOR (items)->size; i += 3)
3212 {
3213 Lisp_Object pos, string;
3214 string = XVECTOR (items)->contents[i + 1];
3215 pos = XVECTOR (items)->contents[i + 2];
3216 if (NILP (string))
3217 break;
3218 if (column >= XINT (pos)
3219 && column < XINT (pos) + XSTRING (string)->size)
3220 {
3221 item = XVECTOR (items)->contents[i];
3222 break;
3223 }
3224 }
3225
3226 position
3227 = Fcons (event->frame_or_window,
3228 Fcons (Qmenu_bar,
3229 Fcons (Fcons (event->x, event->y),
3230 Fcons (make_number (event->timestamp),
3231 Qnil))));
3232
3233 return Fcons (item, Fcons (position, Qnil));
3234 }
3235 #endif /* not USE_X_TOOLKIT */
3236
3237 window = window_from_coordinates (f, column, row, &part);
3238
3239 if (!WINDOWP (window))
3240 {
3241 window = event->frame_or_window;
3242 posn = Qnil;
3243 }
3244 else
3245 {
3246 int pixcolumn, pixrow;
3247 column -= XINT (XWINDOW (window)->left);
3248 row -= XINT (XWINDOW (window)->top);
3249 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
3250 XSETINT (event->x, pixcolumn);
3251 XSETINT (event->y, pixrow);
3252
3253 if (part == 1)
3254 posn = Qmode_line;
3255 else if (part == 2)
3256 posn = Qvertical_line;
3257 else
3258 XSETINT (posn,
3259 buffer_posn_from_coords (XWINDOW (window),
3260 column, row));
3261 }
3262
3263 position
3264 = Fcons (window,
3265 Fcons (posn,
3266 Fcons (Fcons (event->x, event->y),
3267 Fcons (make_number (event->timestamp),
3268 Qnil))));
3269 }
3270 else
3271 {
3272 Lisp_Object window;
3273 Lisp_Object portion_whole;
3274 Lisp_Object part;
3275
3276 window = event->frame_or_window;
3277 portion_whole = Fcons (event->x, event->y);
3278 part = *scroll_bar_parts[(int) event->part];
3279
3280 position =
3281 Fcons (window,
3282 Fcons (Qvertical_scroll_bar,
3283 Fcons (portion_whole,
3284 Fcons (make_number (event->timestamp),
3285 Fcons (part, Qnil)))));
3286 }
3287
3288 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
3289
3290 start_pos = *start_pos_ptr;
3291 *start_pos_ptr = Qnil;
3292
3293 is_double = (button == last_mouse_button
3294 && XINT (event->x) == last_mouse_x
3295 && XINT (event->y) == last_mouse_y
3296 && button_down_time != 0
3297 && (EQ (Vdouble_click_time, Qt)
3298 || (INTEGERP (Vdouble_click_time)
3299 && ((int)(event->timestamp - button_down_time)
3300 < XINT (Vdouble_click_time)))));
3301 last_mouse_button = button;
3302 last_mouse_x = XINT (event->x);
3303 last_mouse_y = XINT (event->y);
3304
3305 /* If this is a button press, squirrel away the location, so
3306 we can decide later whether it was a click or a drag. */
3307 if (event->modifiers & down_modifier)
3308 {
3309 if (is_double)
3310 {
3311 double_click_count++;
3312 event->modifiers |= ((double_click_count > 2)
3313 ? triple_modifier
3314 : double_modifier);
3315 }
3316 else
3317 double_click_count = 1;
3318 button_down_time = event->timestamp;
3319 *start_pos_ptr = Fcopy_alist (position);
3320 }
3321
3322 /* Now we're releasing a button - check the co-ordinates to
3323 see if this was a click or a drag. */
3324 else if (event->modifiers & up_modifier)
3325 {
3326 /* If we did not see a down before this up,
3327 ignore the up. Probably this happened because
3328 the down event chose a menu item.
3329 It would be an annoyance to treat the release
3330 of the button that chose the menu item
3331 as a separate event. */
3332
3333 if (!CONSP (start_pos))
3334 return Qnil;
3335
3336 event->modifiers &= ~up_modifier;
3337 #if 0 /* Formerly we treated an up with no down as a click event. */
3338 if (!CONSP (start_pos))
3339 event->modifiers |= click_modifier;
3340 else
3341 #endif
3342 {
3343 /* The third element of every position should be the (x,y)
3344 pair. */
3345 Lisp_Object down;
3346
3347 down = Fnth (make_number (2), start_pos);
3348 if (EQ (event->x, XCONS (down)->car)
3349 && EQ (event->y, XCONS (down)->cdr))
3350 {
3351 event->modifiers |= click_modifier;
3352 }
3353 else
3354 {
3355 button_down_time = 0;
3356 event->modifiers |= drag_modifier;
3357 }
3358 /* Don't check is_double; treat this as multiple
3359 if the down-event was multiple. */
3360 if (double_click_count > 1)
3361 event->modifiers |= ((double_click_count > 2)
3362 ? triple_modifier
3363 : double_modifier);
3364 }
3365 }
3366 else
3367 /* Every mouse event should either have the down_modifier or
3368 the up_modifier set. */
3369 abort ();
3370
3371 {
3372 /* Get the symbol we should use for the mouse click. */
3373 Lisp_Object head;
3374
3375 head = modify_event_symbol (button,
3376 event->modifiers,
3377 Qmouse_click, Qnil,
3378 lispy_mouse_names, &mouse_syms,
3379 (sizeof (lispy_mouse_names)
3380 / sizeof (lispy_mouse_names[0])));
3381 if (event->modifiers & drag_modifier)
3382 return Fcons (head,
3383 Fcons (start_pos,
3384 Fcons (position,
3385 Qnil)));
3386 else if (event->modifiers & (double_modifier | triple_modifier))
3387 return Fcons (head,
3388 Fcons (position,
3389 Fcons (make_number (double_click_count),
3390 Qnil)));
3391 else
3392 return Fcons (head,
3393 Fcons (position,
3394 Qnil));
3395 }
3396 }
3397 #endif /* HAVE_MOUSE */
3398
3399 #ifdef USE_X_TOOLKIT
3400 case menu_bar_event:
3401 /* The event value is in the cdr of the frame_or_window slot. */
3402 if (!CONSP (event->frame_or_window))
3403 abort ();
3404 return XCONS (event->frame_or_window)->cdr;
3405 #endif
3406
3407 /* The 'kind' field of the event is something we don't recognize. */
3408 default:
3409 abort ();
3410 }
3411 }
3412
3413 #ifdef HAVE_MOUSE
3414
3415 static Lisp_Object
3416 make_lispy_movement (frame, bar_window, part, x, y, time)
3417 FRAME_PTR frame;
3418 Lisp_Object bar_window;
3419 enum scroll_bar_part part;
3420 Lisp_Object x, y;
3421 unsigned long time;
3422 {
3423 #ifdef MULTI_FRAME
3424 /* Is it a scroll bar movement? */
3425 if (frame && ! NILP (bar_window))
3426 {
3427 Lisp_Object part_sym;
3428
3429 part_sym = *scroll_bar_parts[(int) part];
3430 return Fcons (Qscroll_bar_movement,
3431 (Fcons (Fcons (bar_window,
3432 Fcons (Qvertical_scroll_bar,
3433 Fcons (Fcons (x, y),
3434 Fcons (make_number (time),
3435 Fcons (part_sym,
3436 Qnil))))),
3437 Qnil)));
3438 }
3439
3440 /* Or is it an ordinary mouse movement? */
3441 else
3442 #endif /* MULTI_FRAME */
3443 {
3444 int area;
3445 Lisp_Object window;
3446 Lisp_Object posn;
3447 int column, row;
3448
3449 #ifdef MULTI_FRAME
3450 if (frame)
3451 #else
3452 if (1)
3453 #endif
3454 {
3455 /* It's in a frame; which window on that frame? */
3456 pixel_to_glyph_coords (frame, XINT (x), XINT (y), &column, &row,
3457 NULL, 1);
3458 window = window_from_coordinates (frame, column, row, &area);
3459 }
3460 else
3461 window = Qnil;
3462
3463 if (WINDOWP (window))
3464 {
3465 int pixcolumn, pixrow;
3466 column -= XINT (XWINDOW (window)->left);
3467 row -= XINT (XWINDOW (window)->top);
3468 glyph_to_pixel_coords (frame, column, row, &pixcolumn, &pixrow);
3469 XSETINT (x, pixcolumn);
3470 XSETINT (y, pixrow);
3471
3472 if (area == 1)
3473 posn = Qmode_line;
3474 else if (area == 2)
3475 posn = Qvertical_line;
3476 else
3477 XSETINT (posn,
3478 buffer_posn_from_coords (XWINDOW (window), column, row));
3479 }
3480 #ifdef MULTI_FRAME
3481 else if (frame != 0)
3482 {
3483 XSETFRAME (window, frame);
3484 posn = Qnil;
3485 }
3486 #endif
3487 else
3488 {
3489 window = Qnil;
3490 posn = Qnil;
3491 XSETFASTINT (x, 0);
3492 XSETFASTINT (y, 0);
3493 }
3494
3495 return Fcons (Qmouse_movement,
3496 Fcons (Fcons (window,
3497 Fcons (posn,
3498 Fcons (Fcons (x, y),
3499 Fcons (make_number (time),
3500 Qnil)))),
3501 Qnil));
3502 }
3503 }
3504
3505 #endif /* HAVE_MOUSE */
3506
3507 /* Construct a switch frame event. */
3508 static Lisp_Object
3509 make_lispy_switch_frame (frame)
3510 Lisp_Object frame;
3511 {
3512 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
3513 }
3514 \f
3515 /* Manipulating modifiers. */
3516
3517 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
3518
3519 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
3520 SYMBOL's name of the end of the modifiers; the string from this
3521 position is the unmodified symbol name.
3522
3523 This doesn't use any caches. */
3524
3525 static int
3526 parse_modifiers_uncached (symbol, modifier_end)
3527 Lisp_Object symbol;
3528 int *modifier_end;
3529 {
3530 struct Lisp_String *name;
3531 int i;
3532 int modifiers;
3533
3534 CHECK_SYMBOL (symbol, 1);
3535
3536 modifiers = 0;
3537 name = XSYMBOL (symbol)->name;
3538
3539 for (i = 0; i+2 <= name->size; )
3540 {
3541 int this_mod_end = 0;
3542 int this_mod = 0;
3543
3544 /* See if the name continues with a modifier word.
3545 Check that the word appears, but don't check what follows it.
3546 Set this_mod and this_mod_end to record what we find. */
3547
3548 switch (name->data[i])
3549 {
3550 #define SINGLE_LETTER_MOD(BIT) \
3551 (this_mod_end = i + 1, this_mod = BIT)
3552
3553 case 'A':
3554 SINGLE_LETTER_MOD (alt_modifier);
3555 break;
3556
3557 case 'C':
3558 SINGLE_LETTER_MOD (ctrl_modifier);
3559 break;
3560
3561 case 'H':
3562 SINGLE_LETTER_MOD (hyper_modifier);
3563 break;
3564
3565 case 'M':
3566 SINGLE_LETTER_MOD (meta_modifier);
3567 break;
3568
3569 case 'S':
3570 SINGLE_LETTER_MOD (shift_modifier);
3571 break;
3572
3573 case 's':
3574 SINGLE_LETTER_MOD (super_modifier);
3575 break;
3576
3577 #undef SINGLE_LETTER_MOD
3578 }
3579
3580 /* If we found no modifier, stop looking for them. */
3581 if (this_mod_end == 0)
3582 break;
3583
3584 /* Check there is a dash after the modifier, so that it
3585 really is a modifier. */
3586 if (this_mod_end >= name->size || name->data[this_mod_end] != '-')
3587 break;
3588
3589 /* This modifier is real; look for another. */
3590 modifiers |= this_mod;
3591 i = this_mod_end + 1;
3592 }
3593
3594 /* Should we include the `click' modifier? */
3595 if (! (modifiers & (down_modifier | drag_modifier
3596 | double_modifier | triple_modifier))
3597 && i + 7 == name->size
3598 && strncmp (name->data + i, "mouse-", 6) == 0
3599 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
3600 modifiers |= click_modifier;
3601
3602 if (modifier_end)
3603 *modifier_end = i;
3604
3605 return modifiers;
3606 }
3607
3608 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
3609 prepended to the string BASE[0..BASE_LEN-1].
3610 This doesn't use any caches. */
3611 static Lisp_Object
3612 apply_modifiers_uncached (modifiers, base, base_len)
3613 int modifiers;
3614 char *base;
3615 int base_len;
3616 {
3617 /* Since BASE could contain nulls, we can't use intern here; we have
3618 to use Fintern, which expects a genuine Lisp_String, and keeps a
3619 reference to it. */
3620 char *new_mods =
3621 (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
3622 int mod_len;
3623
3624 {
3625 char *p = new_mods;
3626
3627 /* Only the event queue may use the `up' modifier; it should always
3628 be turned into a click or drag event before presented to lisp code. */
3629 if (modifiers & up_modifier)
3630 abort ();
3631
3632 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
3633 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
3634 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
3635 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
3636 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
3637 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
3638 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
3639 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
3640 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
3641 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
3642 /* The click modifier is denoted by the absence of other modifiers. */
3643
3644 *p = '\0';
3645
3646 mod_len = p - new_mods;
3647 }
3648
3649 {
3650 Lisp_Object new_name;
3651
3652 new_name = make_uninit_string (mod_len + base_len);
3653 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
3654 bcopy (base, XSTRING (new_name)->data + mod_len, base_len);
3655
3656 return Fintern (new_name, Qnil);
3657 }
3658 }
3659
3660
3661 static char *modifier_names[] =
3662 {
3663 "up", "down", "drag", "click", "double", "triple", 0, 0,
3664 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3665 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
3666 };
3667 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
3668
3669 static Lisp_Object modifier_symbols;
3670
3671 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
3672 static Lisp_Object
3673 lispy_modifier_list (modifiers)
3674 int modifiers;
3675 {
3676 Lisp_Object modifier_list;
3677 int i;
3678
3679 modifier_list = Qnil;
3680 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
3681 if (modifiers & (1<<i))
3682 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
3683 modifier_list);
3684
3685 return modifier_list;
3686 }
3687
3688
3689 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
3690 where UNMODIFIED is the unmodified form of SYMBOL,
3691 MASK is the set of modifiers present in SYMBOL's name.
3692 This is similar to parse_modifiers_uncached, but uses the cache in
3693 SYMBOL's Qevent_symbol_element_mask property, and maintains the
3694 Qevent_symbol_elements property. */
3695
3696 static Lisp_Object
3697 parse_modifiers (symbol)
3698 Lisp_Object symbol;
3699 {
3700 Lisp_Object elements;
3701
3702 elements = Fget (symbol, Qevent_symbol_element_mask);
3703 if (CONSP (elements))
3704 return elements;
3705 else
3706 {
3707 int end;
3708 int modifiers = parse_modifiers_uncached (symbol, &end);
3709 Lisp_Object unmodified;
3710 Lisp_Object mask;
3711
3712 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
3713 XSYMBOL (symbol)->name->size - end),
3714 Qnil);
3715
3716 if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1))
3717 abort ();
3718 XSETFASTINT (mask, modifiers);
3719 elements = Fcons (unmodified, Fcons (mask, Qnil));
3720
3721 /* Cache the parsing results on SYMBOL. */
3722 Fput (symbol, Qevent_symbol_element_mask,
3723 elements);
3724 Fput (symbol, Qevent_symbol_elements,
3725 Fcons (unmodified, lispy_modifier_list (modifiers)));
3726
3727 /* Since we know that SYMBOL is modifiers applied to unmodified,
3728 it would be nice to put that in unmodified's cache.
3729 But we can't, since we're not sure that parse_modifiers is
3730 canonical. */
3731
3732 return elements;
3733 }
3734 }
3735
3736 /* Apply the modifiers MODIFIERS to the symbol BASE.
3737 BASE must be unmodified.
3738
3739 This is like apply_modifiers_uncached, but uses BASE's
3740 Qmodifier_cache property, if present. It also builds
3741 Qevent_symbol_elements properties, since it has that info anyway.
3742
3743 apply_modifiers copies the value of BASE's Qevent_kind property to
3744 the modified symbol. */
3745 static Lisp_Object
3746 apply_modifiers (modifiers, base)
3747 int modifiers;
3748 Lisp_Object base;
3749 {
3750 Lisp_Object cache, index, entry, new_symbol;
3751
3752 /* Mask out upper bits. We don't know where this value's been. */
3753 modifiers &= ((EMACS_INT)1 << VALBITS) - 1;
3754
3755 /* The click modifier never figures into cache indices. */
3756 cache = Fget (base, Qmodifier_cache);
3757 XSETFASTINT (index, (modifiers & ~click_modifier));
3758 entry = assq_no_quit (index, cache);
3759
3760 if (CONSP (entry))
3761 new_symbol = XCONS (entry)->cdr;
3762 else
3763 {
3764 /* We have to create the symbol ourselves. */
3765 new_symbol = apply_modifiers_uncached (modifiers,
3766 XSYMBOL (base)->name->data,
3767 XSYMBOL (base)->name->size);
3768
3769 /* Add the new symbol to the base's cache. */
3770 entry = Fcons (index, new_symbol);
3771 Fput (base, Qmodifier_cache, Fcons (entry, cache));
3772
3773 /* We have the parsing info now for free, so add it to the caches. */
3774 XSETFASTINT (index, modifiers);
3775 Fput (new_symbol, Qevent_symbol_element_mask,
3776 Fcons (base, Fcons (index, Qnil)));
3777 Fput (new_symbol, Qevent_symbol_elements,
3778 Fcons (base, lispy_modifier_list (modifiers)));
3779 }
3780
3781 /* Make sure this symbol is of the same kind as BASE.
3782
3783 You'd think we could just set this once and for all when we
3784 intern the symbol above, but reorder_modifiers may call us when
3785 BASE's property isn't set right; we can't assume that just
3786 because it has a Qmodifier_cache property it must have its
3787 Qevent_kind set right as well. */
3788 if (NILP (Fget (new_symbol, Qevent_kind)))
3789 {
3790 Lisp_Object kind;
3791
3792 kind = Fget (base, Qevent_kind);
3793 if (! NILP (kind))
3794 Fput (new_symbol, Qevent_kind, kind);
3795 }
3796
3797 return new_symbol;
3798 }
3799
3800
3801 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
3802 return a symbol with the modifiers placed in the canonical order.
3803 Canonical order is alphabetical, except for down and drag, which
3804 always come last. The 'click' modifier is never written out.
3805
3806 Fdefine_key calls this to make sure that (for example) C-M-foo
3807 and M-C-foo end up being equivalent in the keymap. */
3808
3809 Lisp_Object
3810 reorder_modifiers (symbol)
3811 Lisp_Object symbol;
3812 {
3813 /* It's hopefully okay to write the code this way, since everything
3814 will soon be in caches, and no consing will be done at all. */
3815 Lisp_Object parsed;
3816
3817 parsed = parse_modifiers (symbol);
3818 return apply_modifiers ((int) XINT (XCONS (XCONS (parsed)->cdr)->car),
3819 XCONS (parsed)->car);
3820 }
3821
3822
3823 /* For handling events, we often want to produce a symbol whose name
3824 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
3825 to some base, like the name of a function key or mouse button.
3826 modify_event_symbol produces symbols of this sort.
3827
3828 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
3829 is the name of the i'th symbol. TABLE_SIZE is the number of elements
3830 in the table.
3831
3832 Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
3833 NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
3834
3835 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
3836 persist between calls to modify_event_symbol that it can use to
3837 store a cache of the symbols it's generated for this NAME_TABLE
3838 before. The object stored there may be a vector or an alist.
3839
3840 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
3841
3842 MODIFIERS is a set of modifier bits (as given in struct input_events)
3843 whose prefixes should be applied to the symbol name.
3844
3845 SYMBOL_KIND is the value to be placed in the event_kind property of
3846 the returned symbol.
3847
3848 The symbols we create are supposed to have an
3849 `event-symbol-elements' property, which lists the modifiers present
3850 in the symbol's name. */
3851
3852 static Lisp_Object
3853 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
3854 name_table, symbol_table, table_size)
3855 int symbol_num;
3856 unsigned modifiers;
3857 Lisp_Object symbol_kind;
3858 Lisp_Object name_alist;
3859 char **name_table;
3860 Lisp_Object *symbol_table;
3861 unsigned int table_size;
3862 {
3863 Lisp_Object value;
3864 Lisp_Object symbol_int;
3865
3866 /* Get rid of the "vendor-specific" bit here. */
3867 XSETINT (symbol_int, symbol_num & 0xffffff);
3868
3869 /* Is this a request for a valid symbol? */
3870 if (symbol_num < 0 || symbol_num >= table_size)
3871 return Qnil;
3872
3873 if (CONSP (*symbol_table))
3874 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
3875
3876 /* If *symbol_table doesn't seem to be initialized properly, fix that.
3877 *symbol_table should be a lisp vector TABLE_SIZE elements long,
3878 where the Nth element is the symbol for NAME_TABLE[N], or nil if
3879 we've never used that symbol before. */
3880 else
3881 {
3882 if (! VECTORP (*symbol_table)
3883 || XVECTOR (*symbol_table)->size != table_size)
3884 {
3885 Lisp_Object size;
3886
3887 XSETFASTINT (size, table_size);
3888 *symbol_table = Fmake_vector (size, Qnil);
3889 }
3890
3891 value = XVECTOR (*symbol_table)->contents[symbol_num];
3892 }
3893
3894 /* Have we already used this symbol before? */
3895 if (NILP (value))
3896 {
3897 /* No; let's create it. */
3898 if (!NILP (name_alist))
3899 value = Fcdr_safe (Fassq (symbol_int, name_alist));
3900 else if (name_table[symbol_num])
3901 value = intern (name_table[symbol_num]);
3902
3903 #ifdef HAVE_X_WINDOWS
3904 if (NILP (value))
3905 {
3906 char *name = x_get_keysym_name (symbol_num);
3907 if (name)
3908 value = intern (name);
3909 }
3910 #endif
3911
3912 if (NILP (value))
3913 {
3914 char buf[20];
3915 sprintf (buf, "key-%d", symbol_num);
3916 value = intern (buf);
3917 }
3918
3919 if (CONSP (*symbol_table))
3920 *symbol_table = Fcons (value, *symbol_table);
3921 else
3922 XVECTOR (*symbol_table)->contents[symbol_num] = value;
3923
3924 /* Fill in the cache entries for this symbol; this also
3925 builds the Qevent_symbol_elements property, which the user
3926 cares about. */
3927 apply_modifiers (modifiers & click_modifier, value);
3928 Fput (value, Qevent_kind, symbol_kind);
3929 }
3930
3931 /* Apply modifiers to that symbol. */
3932 return apply_modifiers (modifiers, value);
3933 }
3934 \f
3935 /* Convert a list that represents an event type,
3936 such as (ctrl meta backspace), into the usual representation of that
3937 event type as a number or a symbol. */
3938
3939 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
3940 "Convert the event description LIST to an event type.\n\
3941 LIST should contain one base event type (a character or symbol)\n\
3942 and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\
3943 drag, down, double or triple).\n\
3944 The return value is an event type (a character or symbol) which\n\
3945 has the same base event type and all the specified modifiers.")
3946 (event)
3947 Lisp_Object event;
3948 {
3949 Lisp_Object base;
3950 int modifiers = 0;
3951 Lisp_Object rest;
3952
3953 base = Qnil;
3954 rest = event;
3955 while (CONSP (rest))
3956 {
3957 Lisp_Object elt;
3958 int this = 0;
3959
3960 elt = XCONS (rest)->car;
3961
3962 /* Given a symbol, see if it is a modifier name. */
3963 if (SYMBOLP (elt))
3964 this = parse_solitary_modifier (elt);
3965
3966 if (this != 0)
3967 modifiers |= this;
3968 else if (!NILP (base))
3969 error ("Two bases given in one event");
3970 else
3971 base = elt;
3972
3973 rest = XCONS (rest)->cdr;
3974 }
3975
3976 /* Let the symbol A refer to the character A. */
3977 if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1)
3978 XSETINT (base, XSYMBOL (base)->name->data[0]);
3979
3980 if (INTEGERP (base))
3981 {
3982 /* Turn (shift a) into A. */
3983 if ((modifiers & shift_modifier) != 0
3984 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
3985 {
3986 XSETINT (base, XINT (base) - ('a' - 'A'));
3987 modifiers &= ~shift_modifier;
3988 }
3989
3990 /* Turn (control a) into C-a. */
3991 if (modifiers & ctrl_modifier)
3992 return make_number ((modifiers & ~ctrl_modifier)
3993 | make_ctrl_char (XINT (base)));
3994 else
3995 return make_number (modifiers | XINT (base));
3996 }
3997 else if (SYMBOLP (base))
3998 return apply_modifiers (modifiers, base);
3999 else
4000 error ("Invalid base event");
4001 }
4002
4003 /* Try to recognize SYMBOL as a modifier name.
4004 Return the modifier flag bit, or 0 if not recognized. */
4005
4006 static int
4007 parse_solitary_modifier (symbol)
4008 Lisp_Object symbol;
4009 {
4010 struct Lisp_String *name = XSYMBOL (symbol)->name;
4011
4012 switch (name->data[0])
4013 {
4014 #define SINGLE_LETTER_MOD(BIT) \
4015 if (name->size == 1) \
4016 return BIT;
4017
4018 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
4019 if (LEN == name->size \
4020 && ! strncmp (name->data, NAME, LEN)) \
4021 return BIT;
4022
4023 case 'A':
4024 SINGLE_LETTER_MOD (alt_modifier);
4025 break;
4026
4027 case 'a':
4028 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
4029 break;
4030
4031 case 'C':
4032 SINGLE_LETTER_MOD (ctrl_modifier);
4033 break;
4034
4035 case 'c':
4036 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
4037 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
4038 break;
4039
4040 case 'H':
4041 SINGLE_LETTER_MOD (hyper_modifier);
4042 break;
4043
4044 case 'h':
4045 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
4046 break;
4047
4048 case 'M':
4049 SINGLE_LETTER_MOD (meta_modifier);
4050 break;
4051
4052 case 'm':
4053 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
4054 break;
4055
4056 case 'S':
4057 SINGLE_LETTER_MOD (shift_modifier);
4058 break;
4059
4060 case 's':
4061 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
4062 MULTI_LETTER_MOD (super_modifier, "super", 5);
4063 SINGLE_LETTER_MOD (super_modifier);
4064 break;
4065
4066 case 'd':
4067 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
4068 MULTI_LETTER_MOD (down_modifier, "down", 4);
4069 MULTI_LETTER_MOD (double_modifier, "double", 6);
4070 break;
4071
4072 case 't':
4073 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
4074 break;
4075
4076 #undef SINGLE_LETTER_MOD
4077 #undef MULTI_LETTER_MOD
4078 }
4079
4080 return 0;
4081 }
4082
4083 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
4084 Such a list is not valid as an event,
4085 but it can be a Lucid-style event type list. */
4086
4087 int
4088 lucid_event_type_list_p (object)
4089 Lisp_Object object;
4090 {
4091 Lisp_Object tail;
4092
4093 if (! CONSP (object))
4094 return 0;
4095
4096 for (tail = object; CONSP (tail); tail = XCONS (tail)->cdr)
4097 {
4098 Lisp_Object elt;
4099 elt = XCONS (tail)->car;
4100 if (! (INTEGERP (elt) || SYMBOLP (elt)))
4101 return 0;
4102 }
4103
4104 return NILP (tail);
4105 }
4106 \f
4107 /* Store into *addr a value nonzero if terminal input chars are available.
4108 Serves the purpose of ioctl (0, FIONREAD, addr)
4109 but works even if FIONREAD does not exist.
4110 (In fact, this may actually read some input.) */
4111
4112 static void
4113 get_input_pending (addr)
4114 int *addr;
4115 {
4116 /* First of all, have we already counted some input? */
4117 *addr = !NILP (Vquit_flag) || readable_events ();
4118
4119 /* If input is being read as it arrives, and we have none, there is none. */
4120 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
4121 return;
4122
4123 /* Try to read some input and see how much we get. */
4124 gobble_input (0);
4125 *addr = !NILP (Vquit_flag) || readable_events ();
4126 }
4127
4128 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
4129
4130 int
4131 gobble_input (expected)
4132 int expected;
4133 {
4134 #ifndef VMS
4135 #ifdef SIGIO
4136 if (interrupt_input)
4137 {
4138 SIGMASKTYPE mask;
4139 mask = sigblockx (SIGIO);
4140 read_avail_input (expected);
4141 sigsetmask (mask);
4142 }
4143 else
4144 #ifdef POLL_FOR_INPUT
4145 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
4146 {
4147 SIGMASKTYPE mask;
4148 mask = sigblockx (SIGALRM);
4149 read_avail_input (expected);
4150 sigsetmask (mask);
4151 }
4152 else
4153 #endif
4154 #endif
4155 read_avail_input (expected);
4156 #endif
4157 }
4158
4159 /* Put a buffer_switch_event in the buffer
4160 so that read_key_sequence will notice the new current buffer. */
4161
4162 record_asynch_buffer_change ()
4163 {
4164 struct input_event event;
4165 Lisp_Object tem;
4166
4167 event.kind = buffer_switch_event;
4168 event.frame_or_window = Qnil;
4169
4170 #ifdef subprocesses
4171 /* We don't need a buffer-switch event unless Emacs is waiting for input.
4172 The purpose of the event is to make read_key_sequence look up the
4173 keymaps again. If we aren't in read_key_sequence, we don't need one,
4174 and the event could cause trouble by messing up (input-pending-p). */
4175 tem = Fwaiting_for_user_input_p ();
4176 if (NILP (tem))
4177 return;
4178 #else
4179 /* We never need these events if we have no asynchronous subprocesses. */
4180 return;
4181 #endif
4182
4183 /* Make sure no interrupt happens while storing the event. */
4184 #ifdef SIGIO
4185 if (interrupt_input)
4186 {
4187 SIGMASKTYPE mask;
4188 mask = sigblockx (SIGIO);
4189 kbd_buffer_store_event (&event);
4190 sigsetmask (mask);
4191 }
4192 else
4193 #endif
4194 {
4195 stop_polling ();
4196 kbd_buffer_store_event (&event);
4197 start_polling ();
4198 }
4199 }
4200 \f
4201 #ifndef VMS
4202
4203 /* Read any terminal input already buffered up by the system
4204 into the kbd_buffer, but do not wait.
4205
4206 EXPECTED should be nonzero if the caller knows there is some input.
4207
4208 Except on VMS, all input is read by this function.
4209 If interrupt_input is nonzero, this function MUST be called
4210 only when SIGIO is blocked.
4211
4212 Returns the number of keyboard chars read, or -1 meaning
4213 this is a bad time to try to read input. */
4214
4215 static int
4216 read_avail_input (expected)
4217 int expected;
4218 {
4219 struct input_event buf[KBD_BUFFER_SIZE];
4220 register int i;
4221 int nread;
4222
4223 if (read_socket_hook)
4224 /* No need for FIONREAD or fcntl; just say don't wait. */
4225 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE,
4226 expected, expected);
4227 else
4228 {
4229 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
4230 the kbd_buffer can really hold. That may prevent loss
4231 of characters on some systems when input is stuffed at us. */
4232 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
4233 int n_to_read;
4234
4235 /* Determine how many characters we should *try* to read. */
4236 #ifdef WINDOWSNT
4237 return 0;
4238 #else /* not WINDOWSNT */
4239 #ifdef MSDOS
4240 n_to_read = dos_keysns ();
4241 if (n_to_read == 0)
4242 return 0;
4243 #else /* not MSDOS */
4244 #ifdef FIONREAD
4245 /* Find out how much input is available. */
4246 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
4247 /* Formerly simply reported no input, but that sometimes led to
4248 a failure of Emacs to terminate.
4249 SIGHUP seems appropriate if we can't reach the terminal. */
4250 /* ??? Is it really right to send the signal just to this process
4251 rather than to the whole process group?
4252 Perhaps on systems with FIONREAD Emacs is alone in its group. */
4253 kill (getpid (), SIGHUP);
4254 if (n_to_read == 0)
4255 return 0;
4256 if (n_to_read > sizeof cbuf)
4257 n_to_read = sizeof cbuf;
4258 #else /* no FIONREAD */
4259 #if defined(USG) || defined(DGUX)
4260 /* Read some input if available, but don't wait. */
4261 n_to_read = sizeof cbuf;
4262 fcntl (input_fd, F_SETFL, O_NDELAY);
4263 #else
4264 you lose;
4265 #endif
4266 #endif
4267 #endif /* not MSDOS */
4268 #endif /* not WINDOWSNT */
4269
4270 /* Now read; for one reason or another, this will not block.
4271 NREAD is set to the number of chars read. */
4272 do
4273 {
4274 #ifdef MSDOS
4275 cbuf[0] = dos_keyread();
4276 nread = 1;
4277 #else
4278 nread = read (input_fd, cbuf, n_to_read);
4279 #endif
4280 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
4281 /* The kernel sometimes fails to deliver SIGHUP for ptys.
4282 This looks incorrect, but it isn't, because _BSD causes
4283 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
4284 and that causes a value other than 0 when there is no input. */
4285 if (nread == 0)
4286 kill (0, SIGHUP);
4287 #endif
4288 }
4289 while (
4290 /* We used to retry the read if it was interrupted.
4291 But this does the wrong thing when O_NDELAY causes
4292 an EAGAIN error. Does anybody know of a situation
4293 where a retry is actually needed? */
4294 #if 0
4295 nread < 0 && (errno == EAGAIN
4296 #ifdef EFAULT
4297 || errno == EFAULT
4298 #endif
4299 #ifdef EBADSLT
4300 || errno == EBADSLT
4301 #endif
4302 )
4303 #else
4304 0
4305 #endif
4306 );
4307
4308 #ifndef FIONREAD
4309 #if defined (USG) || defined (DGUX)
4310 fcntl (input_fd, F_SETFL, 0);
4311 #endif /* USG or DGUX */
4312 #endif /* no FIONREAD */
4313 for (i = 0; i < nread; i++)
4314 {
4315 buf[i].kind = ascii_keystroke;
4316 buf[i].modifiers = 0;
4317 if (meta_key == 1 && (cbuf[i] & 0x80))
4318 buf[i].modifiers = meta_modifier;
4319 if (meta_key != 2)
4320 cbuf[i] &= ~0x80;
4321
4322 buf[i].code = cbuf[i];
4323 #ifdef MULTI_FRAME
4324 XSETFRAME (buf[i].frame_or_window, selected_frame);
4325 #else
4326 buf[i].frame_or_window = Qnil;
4327 #endif
4328 }
4329 }
4330
4331 /* Scan the chars for C-g and store them in kbd_buffer. */
4332 for (i = 0; i < nread; i++)
4333 {
4334 kbd_buffer_store_event (&buf[i]);
4335 /* Don't look at input that follows a C-g too closely.
4336 This reduces lossage due to autorepeat on C-g. */
4337 if (buf[i].kind == ascii_keystroke
4338 && buf[i].code == quit_char)
4339 break;
4340 }
4341
4342 return nread;
4343 }
4344 #endif /* not VMS */
4345 \f
4346 #ifdef SIGIO /* for entire page */
4347 /* Note SIGIO has been undef'd if FIONREAD is missing. */
4348
4349 SIGTYPE
4350 input_available_signal (signo)
4351 int signo;
4352 {
4353 /* Must preserve main program's value of errno. */
4354 int old_errno = errno;
4355 #ifdef BSD4_1
4356 extern int select_alarmed;
4357 #endif
4358
4359 #ifdef USG
4360 /* USG systems forget handlers when they are used;
4361 must reestablish each time */
4362 signal (signo, input_available_signal);
4363 #endif /* USG */
4364
4365 #ifdef BSD4_1
4366 sigisheld (SIGIO);
4367 #endif
4368
4369 if (input_available_clear_time)
4370 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
4371
4372 while (1)
4373 {
4374 int nread;
4375 nread = read_avail_input (1);
4376 /* -1 means it's not ok to read the input now.
4377 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
4378 0 means there was no keyboard input available. */
4379 if (nread <= 0)
4380 break;
4381
4382 #ifdef BSD4_1
4383 select_alarmed = 1; /* Force the select emulator back to life */
4384 #endif
4385 }
4386
4387 #ifdef BSD4_1
4388 sigfree ();
4389 #endif
4390 errno = old_errno;
4391 }
4392 #endif /* SIGIO */
4393
4394 /* Send ourselves a SIGIO.
4395
4396 This function exists so that the UNBLOCK_INPUT macro in
4397 blockinput.h can have some way to take care of input we put off
4398 dealing with, without assuming that every file which uses
4399 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
4400 void
4401 reinvoke_input_signal ()
4402 {
4403 #ifdef SIGIO
4404 kill (0, SIGIO);
4405 #endif
4406 }
4407
4408
4409 \f
4410 /* Return the prompt-string of a sparse keymap.
4411 This is the first element which is a string.
4412 Return nil if there is none. */
4413
4414 Lisp_Object
4415 map_prompt (map)
4416 Lisp_Object map;
4417 {
4418 while (CONSP (map))
4419 {
4420 register Lisp_Object tem;
4421 tem = Fcar (map);
4422 if (STRINGP (tem))
4423 return tem;
4424 map = Fcdr (map);
4425 }
4426 return Qnil;
4427 }
4428
4429 static void menu_bar_item ();
4430 static void menu_bar_one_keymap ();
4431
4432 /* These variables hold the vector under construction within
4433 menu_bar_items and its subroutines, and the current index
4434 for storing into that vector. */
4435 static Lisp_Object menu_bar_items_vector;
4436 static int menu_bar_items_index;
4437
4438 /* Return a vector of menu items for a menu bar, appropriate
4439 to the current buffer. Each item has three elements in the vector:
4440 KEY STRING MAPLIST.
4441
4442 OLD is an old vector we can optionally reuse, or nil. */
4443
4444 Lisp_Object
4445 menu_bar_items (old)
4446 Lisp_Object old;
4447 {
4448 /* The number of keymaps we're scanning right now, and the number of
4449 keymaps we have allocated space for. */
4450 int nmaps;
4451
4452 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
4453 in the current keymaps, or nil where it is not a prefix. */
4454 Lisp_Object *maps;
4455
4456 Lisp_Object def, tem, tail;
4457
4458 Lisp_Object result;
4459
4460 int mapno;
4461 Lisp_Object oquit;
4462
4463 int i;
4464
4465 struct gcpro gcpro1;
4466
4467 /* In order to build the menus, we need to call the keymap
4468 accessors. They all call QUIT. But this function is called
4469 during redisplay, during which a quit is fatal. So inhibit
4470 quitting while building the menus.
4471 We do this instead of specbind because (1) errors will clear it anyway
4472 and (2) this avoids risk of specpdl overflow. */
4473 oquit = Vinhibit_quit;
4474 Vinhibit_quit = Qt;
4475
4476 if (!NILP (old))
4477 menu_bar_items_vector = old;
4478 else
4479 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
4480 menu_bar_items_index = 0;
4481
4482 GCPRO1 (menu_bar_items_vector);
4483
4484 /* Build our list of keymaps.
4485 If we recognize a function key and replace its escape sequence in
4486 keybuf with its symbol, or if the sequence starts with a mouse
4487 click and we need to switch buffers, we jump back here to rebuild
4488 the initial keymaps from the current buffer. */
4489 {
4490 Lisp_Object *tmaps;
4491
4492 /* Should overriding-terminal-local-map and overriding-local-map apply? */
4493 if (!NILP (Voverriding_local_map_menu_flag))
4494 {
4495 /* Yes, use them (if non-nil) as well as the global map. */
4496 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
4497 nmaps = 0;
4498 if (!NILP (current_kboard->Voverriding_terminal_local_map))
4499 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
4500 if (!NILP (Voverriding_local_map))
4501 maps[nmaps++] = Voverriding_local_map;
4502 }
4503 else
4504 {
4505 /* No, so use major and minor mode keymaps. */
4506 nmaps = current_minor_maps (NULL, &tmaps);
4507 maps = (Lisp_Object *) alloca ((nmaps + 2) * sizeof (maps[0]));
4508 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
4509 #ifdef USE_TEXT_PROPERTIES
4510 maps[nmaps++] = get_local_map (PT, current_buffer);
4511 #else
4512 maps[nmaps++] = current_buffer->keymap;
4513 #endif
4514 }
4515 maps[nmaps++] = current_global_map;
4516 }
4517
4518 /* Look up in each map the dummy prefix key `menu-bar'. */
4519
4520 result = Qnil;
4521
4522 for (mapno = nmaps - 1; mapno >= 0; mapno--)
4523 {
4524 if (! NILP (maps[mapno]))
4525 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0));
4526 else
4527 def = Qnil;
4528
4529 tem = Fkeymapp (def);
4530 if (!NILP (tem))
4531 menu_bar_one_keymap (def);
4532 }
4533
4534 /* Move to the end those items that should be at the end. */
4535
4536 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCONS (tail)->cdr)
4537 {
4538 int i;
4539 int end = menu_bar_items_index;
4540
4541 for (i = 0; i < end; i += 3)
4542 if (EQ (XCONS (tail)->car, XVECTOR (menu_bar_items_vector)->contents[i]))
4543 {
4544 Lisp_Object tem0, tem1, tem2;
4545 /* Move the item at index I to the end,
4546 shifting all the others forward. */
4547 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
4548 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
4549 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
4550 if (end > i + 3)
4551 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 3],
4552 &XVECTOR (menu_bar_items_vector)->contents[i],
4553 (end - i - 3) * sizeof (Lisp_Object));
4554 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem0;
4555 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem1;
4556 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem2;
4557 break;
4558 }
4559 }
4560
4561 /* Add nil, nil, nil at the end. */
4562 i = menu_bar_items_index;
4563 if (i + 3 > XVECTOR (menu_bar_items_vector)->size)
4564 {
4565 Lisp_Object tem;
4566 int newsize = 2 * i;
4567 tem = Fmake_vector (make_number (2 * i), Qnil);
4568 bcopy (XVECTOR (menu_bar_items_vector)->contents,
4569 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
4570 menu_bar_items_vector = tem;
4571 }
4572 /* Add this item. */
4573 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
4574 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
4575 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
4576 menu_bar_items_index = i;
4577
4578 Vinhibit_quit = oquit;
4579 UNGCPRO;
4580 return menu_bar_items_vector;
4581 }
4582 \f
4583 /* Scan one map KEYMAP, accumulating any menu items it defines
4584 in menu_bar_items_vector. */
4585
4586 static void
4587 menu_bar_one_keymap (keymap)
4588 Lisp_Object keymap;
4589 {
4590 Lisp_Object tail, item, key, binding, item_string, table;
4591
4592 /* Loop over all keymap entries that have menu strings. */
4593 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
4594 {
4595 item = XCONS (tail)->car;
4596 if (CONSP (item))
4597 {
4598 key = XCONS (item)->car;
4599 binding = XCONS (item)->cdr;
4600 if (CONSP (binding))
4601 {
4602 item_string = XCONS (binding)->car;
4603 if (STRINGP (item_string))
4604 menu_bar_item (key, item_string, Fcdr (binding));
4605 }
4606 else if (EQ (binding, Qundefined))
4607 menu_bar_item (key, Qnil, binding);
4608 }
4609 else if (VECTORP (item))
4610 {
4611 /* Loop over the char values represented in the vector. */
4612 int len = XVECTOR (item)->size;
4613 int c;
4614 for (c = 0; c < len; c++)
4615 {
4616 Lisp_Object character;
4617 XSETFASTINT (character, c);
4618 binding = XVECTOR (item)->contents[c];
4619 if (CONSP (binding))
4620 {
4621 item_string = XCONS (binding)->car;
4622 if (STRINGP (item_string))
4623 menu_bar_item (key, item_string, Fcdr (binding));
4624 }
4625 else if (EQ (binding, Qundefined))
4626 menu_bar_item (key, Qnil, binding);
4627 }
4628 }
4629 }
4630 }
4631
4632 /* This is used as the handler when calling internal_condition_case_1. */
4633
4634 static Lisp_Object
4635 menu_bar_item_1 (arg)
4636 Lisp_Object arg;
4637 {
4638 return Qnil;
4639 }
4640
4641 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
4642 If there's already an item for KEY, add this DEF to it. */
4643
4644 static void
4645 menu_bar_item (key, item_string, def)
4646 Lisp_Object key, item_string, def;
4647 {
4648 Lisp_Object tem;
4649 Lisp_Object enabled;
4650 int i;
4651
4652 if (EQ (def, Qundefined))
4653 {
4654 /* If a map has an explicit `undefined' as definition,
4655 discard any previously made menu bar item. */
4656
4657 for (i = 0; i < menu_bar_items_index; i += 3)
4658 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
4659 {
4660 if (menu_bar_items_index > i + 3)
4661 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 3],
4662 &XVECTOR (menu_bar_items_vector)->contents[i],
4663 (menu_bar_items_index - i - 3) * sizeof (Lisp_Object));
4664 menu_bar_items_index -= 3;
4665 return;
4666 }
4667
4668 /* If there's no definition for this key yet,
4669 just ignore `undefined'. */
4670 return;
4671 }
4672
4673 /* See if this entry is enabled. */
4674 enabled = Qt;
4675
4676 if (SYMBOLP (def))
4677 {
4678 /* No property, or nil, means enable.
4679 Otherwise, enable if value is not nil. */
4680 tem = Fget (def, Qmenu_enable);
4681 if (!NILP (tem))
4682 /* (condition-case nil (eval tem)
4683 (error nil)) */
4684 enabled = internal_condition_case_1 (Feval, tem, Qerror,
4685 menu_bar_item_1);
4686 }
4687
4688 /* Ignore this item if it's not enabled. */
4689 if (NILP (enabled))
4690 return;
4691
4692 /* Find any existing item for this KEY. */
4693 for (i = 0; i < menu_bar_items_index; i += 3)
4694 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
4695 break;
4696
4697 /* If we did not find this KEY, add it at the end. */
4698 if (i == menu_bar_items_index)
4699 {
4700 /* If vector is too small, get a bigger one. */
4701 if (i + 3 > XVECTOR (menu_bar_items_vector)->size)
4702 {
4703 Lisp_Object tem;
4704 int newsize = 2 * i;
4705 tem = Fmake_vector (make_number (2 * i), Qnil);
4706 bcopy (XVECTOR (menu_bar_items_vector)->contents,
4707 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
4708 menu_bar_items_vector = tem;
4709 }
4710 /* Add this item. */
4711 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
4712 XVECTOR (menu_bar_items_vector)->contents[i++] = item_string;
4713 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (def, Qnil);
4714 menu_bar_items_index = i;
4715 }
4716 /* We did find an item for this KEY. Add DEF to its list of maps. */
4717 else
4718 {
4719 Lisp_Object old;
4720 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
4721 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (def, old);
4722 }
4723 }
4724 \f
4725 /* Read a character using menus based on maps in the array MAPS.
4726 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
4727 Return t if we displayed a menu but the user rejected it.
4728
4729 PREV_EVENT is the previous input event, or nil if we are reading
4730 the first event of a key sequence.
4731
4732 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
4733 if we used a mouse menu to read the input, or zero otherwise. If
4734 USED_MOUSE_MENU is null, we don't dereference it.
4735
4736 The prompting is done based on the prompt-string of the map
4737 and the strings associated with various map elements.
4738
4739 This can be done with X menus or with menus put in the minibuf.
4740 These are done in different ways, depending on how the input will be read.
4741 Menus using X are done after auto-saving in read-char, getting the input
4742 event from Fx_popup_menu; menus using the minibuf use read_char recursively
4743 and do auto-saving in the inner call of read_char. */
4744
4745 static Lisp_Object
4746 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
4747 int nmaps;
4748 Lisp_Object *maps;
4749 Lisp_Object prev_event;
4750 int *used_mouse_menu;
4751 {
4752 int mapno;
4753 register Lisp_Object name;
4754 Lisp_Object rest, vector;
4755
4756 if (used_mouse_menu)
4757 *used_mouse_menu = 0;
4758
4759 /* Use local over global Menu maps */
4760
4761 if (! menu_prompting)
4762 return Qnil;
4763
4764 /* Optionally disregard all but the global map. */
4765 if (inhibit_local_menu_bar_menus)
4766 {
4767 maps += (nmaps - 1);
4768 nmaps = 1;
4769 }
4770
4771 /* Get the menu name from the first map that has one (a prompt string). */
4772 for (mapno = 0; mapno < nmaps; mapno++)
4773 {
4774 name = map_prompt (maps[mapno]);
4775 if (!NILP (name))
4776 break;
4777 }
4778
4779 /* If we don't have any menus, just read a character normally. */
4780 if (mapno >= nmaps)
4781 return Qnil;
4782
4783 #if (defined (HAVE_X_WINDOWS) && defined (HAVE_X_MENU)) || defined (MSDOS)
4784 /* If we got to this point via a mouse click,
4785 use a real menu for mouse selection. */
4786 if (EVENT_HAS_PARAMETERS (prev_event)
4787 && !EQ (XCONS (prev_event)->car, Qmenu_bar))
4788 {
4789 /* Display the menu and get the selection. */
4790 Lisp_Object *realmaps
4791 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
4792 Lisp_Object value;
4793 int nmaps1 = 0;
4794
4795 /* Use the maps that are not nil. */
4796 for (mapno = 0; mapno < nmaps; mapno++)
4797 if (!NILP (maps[mapno]))
4798 realmaps[nmaps1++] = maps[mapno];
4799
4800 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
4801 if (CONSP (value))
4802 {
4803 Lisp_Object tem;
4804
4805 /* If we got multiple events, unread all but
4806 the first.
4807 There is no way to prevent those unread events
4808 from showing up later in last_nonmenu_event.
4809 So turn symbol and integer events into lists,
4810 to indicate that they came from a mouse menu,
4811 so that when present in last_nonmenu_event
4812 they won't confuse things. */
4813 for (tem = XCONS (value)->cdr; !NILP (tem);
4814 tem = XCONS (tem)->cdr)
4815 if (SYMBOLP (XCONS (tem)->car)
4816 || INTEGERP (XCONS (tem)->car))
4817 XCONS (tem)->car
4818 = Fcons (XCONS (tem)->car, Qnil);
4819
4820 /* If we got more than one event, put all but the first
4821 onto this list to be read later.
4822 Return just the first event now. */
4823 Vunread_command_events
4824 = nconc2 (XCONS (value)->cdr, Vunread_command_events);
4825 value = XCONS (value)->car;
4826 }
4827 else if (NILP (value))
4828 value = Qt;
4829 if (used_mouse_menu)
4830 *used_mouse_menu = 1;
4831 return value;
4832 }
4833 #endif /* (HAVE_X_WINDOWS && HAVE_X_MENU) || MSDOS */
4834 return Qnil ;
4835 }
4836
4837 static Lisp_Object
4838 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
4839 int commandflag ;
4840 int nmaps;
4841 Lisp_Object *maps;
4842 {
4843 int mapno;
4844 register Lisp_Object name;
4845 int nlength;
4846 int width = FRAME_WIDTH (selected_frame) - 4;
4847 char *menu = (char *) alloca (width + 4);
4848 int idx = -1;
4849 int nobindings = 1;
4850 Lisp_Object rest, vector;
4851
4852 if (! menu_prompting)
4853 return Qnil;
4854
4855 /* Get the menu name from the first map that has one (a prompt string). */
4856 for (mapno = 0; mapno < nmaps; mapno++)
4857 {
4858 name = map_prompt (maps[mapno]);
4859 if (!NILP (name))
4860 break;
4861 }
4862
4863 /* If we don't have any menus, just read a character normally. */
4864 if (mapno >= nmaps)
4865 return Qnil;
4866
4867 /* Prompt string always starts with map's prompt, and a space. */
4868 strcpy (menu, XSTRING (name)->data);
4869 nlength = XSTRING (name)->size;
4870 menu[nlength++] = ':';
4871 menu[nlength++] = ' ';
4872 menu[nlength] = 0;
4873
4874 /* Start prompting at start of first map. */
4875 mapno = 0;
4876 rest = maps[mapno];
4877
4878 /* Present the documented bindings, a line at a time. */
4879 while (1)
4880 {
4881 int notfirst = 0;
4882 int i = nlength;
4883 Lisp_Object obj;
4884 int ch;
4885 Lisp_Object orig_defn_macro;
4886
4887 /* Loop over elements of map. */
4888 while (i < width)
4889 {
4890 Lisp_Object s, elt;
4891
4892 /* If reached end of map, start at beginning of next map. */
4893 if (NILP (rest))
4894 {
4895 mapno++;
4896 /* At end of last map, wrap around to first map if just starting,
4897 or end this line if already have something on it. */
4898 if (mapno == nmaps)
4899 {
4900 mapno = 0;
4901 if (notfirst || nobindings) break;
4902 }
4903 rest = maps[mapno];
4904 }
4905
4906 /* Look at the next element of the map. */
4907 if (idx >= 0)
4908 elt = XVECTOR (vector)->contents[idx];
4909 else
4910 elt = Fcar_safe (rest);
4911
4912 if (idx < 0 && VECTORP (elt))
4913 {
4914 /* If we found a dense table in the keymap,
4915 advanced past it, but start scanning its contents. */
4916 rest = Fcdr_safe (rest);
4917 vector = elt;
4918 idx = 0;
4919 }
4920 else
4921 {
4922 /* An ordinary element. */
4923 Lisp_Object event;
4924
4925 if (idx < 0)
4926 {
4927 s = Fcar_safe (Fcdr_safe (elt)); /* alist */
4928 event = Fcar_safe (elt);
4929 }
4930 else
4931 {
4932 s = Fcar_safe (elt); /* vector */
4933 XSETINT (event, idx);
4934 }
4935
4936 /* Ignore the element if it has no prompt string. */
4937 if (STRINGP (s) && INTEGERP (event))
4938 {
4939 /* 1 if the char to type matches the string. */
4940 int char_matches;
4941 Lisp_Object upcased_event, downcased_event;
4942 Lisp_Object desc;
4943
4944 upcased_event = Fupcase (event);
4945 downcased_event = Fdowncase (event);
4946 char_matches = (XINT (upcased_event) == XSTRING (s)->data[0]
4947 || XINT (downcased_event) == XSTRING (s)->data[0]);
4948 if (! char_matches)
4949 desc = Fsingle_key_description (event);
4950
4951 /* If we have room for the prompt string, add it to this line.
4952 If this is the first on the line, always add it. */
4953 if ((XSTRING (s)->size + i + 2
4954 + (char_matches ? 0 : XSTRING (desc)->size + 3))
4955 < width
4956 || !notfirst)
4957 {
4958 int thiswidth;
4959
4960 /* Punctuate between strings. */
4961 if (notfirst)
4962 {
4963 strcpy (menu + i, ", ");
4964 i += 2;
4965 }
4966 notfirst = 1;
4967 nobindings = 0 ;
4968
4969 /* If the char to type doesn't match the string's
4970 first char, explicitly show what char to type. */
4971 if (! char_matches)
4972 {
4973 /* Add as much of string as fits. */
4974 thiswidth = XSTRING (desc)->size;
4975 if (thiswidth + i > width)
4976 thiswidth = width - i;
4977 bcopy (XSTRING (desc)->data, menu + i, thiswidth);
4978 i += thiswidth;
4979 strcpy (menu + i, " = ");
4980 i += 3;
4981 }
4982
4983 /* Add as much of string as fits. */
4984 thiswidth = XSTRING (s)->size;
4985 if (thiswidth + i > width)
4986 thiswidth = width - i;
4987 bcopy (XSTRING (s)->data, menu + i, thiswidth);
4988 i += thiswidth;
4989 menu[i] = 0;
4990 }
4991 else
4992 {
4993 /* If this element does not fit, end the line now,
4994 and save the element for the next line. */
4995 strcpy (menu + i, "...");
4996 break;
4997 }
4998 }
4999
5000 /* Move past this element. */
5001 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
5002 /* Handle reaching end of dense table. */
5003 idx = -1;
5004 if (idx >= 0)
5005 idx++;
5006 else
5007 rest = Fcdr_safe (rest);
5008 }
5009 }
5010
5011 /* Prompt with that and read response. */
5012 message1 (menu);
5013
5014 /* Make believe its not a keyboard macro in case the help char
5015 is pressed. Help characters are not recorded because menu prompting
5016 is not used on replay.
5017 */
5018 orig_defn_macro = current_kboard->defining_kbd_macro;
5019 current_kboard->defining_kbd_macro = Qnil;
5020 do
5021 obj = read_char (commandflag, 0, 0, Qnil, 0);
5022 while (BUFFERP (obj));
5023 current_kboard->defining_kbd_macro = orig_defn_macro;
5024
5025 if (!INTEGERP (obj))
5026 return obj;
5027 else
5028 ch = XINT (obj);
5029
5030 if (! EQ (obj, menu_prompt_more_char)
5031 && (!INTEGERP (menu_prompt_more_char)
5032 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
5033 {
5034 if (!NILP (current_kboard->defining_kbd_macro))
5035 store_kbd_macro_char (obj);
5036 return obj;
5037 }
5038 /* Help char - go round again */
5039 }
5040 }
5041 \f
5042 /* Reading key sequences. */
5043
5044 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
5045 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
5046 keymap, or nil otherwise. Return the index of the first keymap in
5047 which KEY has any binding, or NMAPS if no map has a binding.
5048
5049 If KEY is a meta ASCII character, treat it like meta-prefix-char
5050 followed by the corresponding non-meta character. Keymaps in
5051 CURRENT with non-prefix bindings for meta-prefix-char become nil in
5052 NEXT.
5053
5054 If KEY has no bindings in any of the CURRENT maps, NEXT is left
5055 unmodified.
5056
5057 NEXT may be the same array as CURRENT. */
5058
5059 static int
5060 follow_key (key, nmaps, current, defs, next)
5061 Lisp_Object key;
5062 Lisp_Object *current, *defs, *next;
5063 int nmaps;
5064 {
5065 int i, first_binding;
5066 int did_meta = 0;
5067
5068 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
5069 followed by the corresponding non-meta character.
5070 Put the results into DEFS, since we are going to alter that anyway.
5071 Do not alter CURRENT or NEXT. */
5072 if (INTEGERP (key) && (XINT (key) & CHAR_META))
5073 {
5074 for (i = 0; i < nmaps; i++)
5075 if (! NILP (current[i]))
5076 {
5077 Lisp_Object def;
5078 def = get_keyelt (access_keymap (current[i],
5079 meta_prefix_char, 1, 0));
5080
5081 /* Note that since we pass the resulting bindings through
5082 get_keymap_1, non-prefix bindings for meta-prefix-char
5083 disappear. */
5084 defs[i] = get_keymap_1 (def, 0, 1);
5085 }
5086 else
5087 defs[i] = Qnil;
5088
5089 did_meta = 1;
5090 XSETINT (key, XFASTINT (key) & ~CHAR_META);
5091 }
5092
5093 first_binding = nmaps;
5094 for (i = nmaps - 1; i >= 0; i--)
5095 {
5096 if (! NILP (current[i]))
5097 {
5098 Lisp_Object map;
5099 if (did_meta)
5100 map = defs[i];
5101 else
5102 map = current[i];
5103
5104 defs[i] = get_keyelt (access_keymap (map, key, 1, 0));
5105 if (! NILP (defs[i]))
5106 first_binding = i;
5107 }
5108 else
5109 defs[i] = Qnil;
5110 }
5111
5112 /* Given the set of bindings we've found, produce the next set of maps. */
5113 if (first_binding < nmaps)
5114 for (i = 0; i < nmaps; i++)
5115 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
5116
5117 return first_binding;
5118 }
5119
5120 /* Read a sequence of keys that ends with a non prefix character,
5121 storing it in KEYBUF, a buffer of size BUFSIZE.
5122 Prompt with PROMPT.
5123 Return the length of the key sequence stored.
5124 Return -1 if the user rejected a command menu.
5125
5126 Echo starting immediately unless `prompt' is 0.
5127
5128 Where a key sequence ends depends on the currently active keymaps.
5129 These include any minor mode keymaps active in the current buffer,
5130 the current buffer's local map, and the global map.
5131
5132 If a key sequence has no other bindings, we check Vfunction_key_map
5133 to see if some trailing subsequence might be the beginning of a
5134 function key's sequence. If so, we try to read the whole function
5135 key, and substitute its symbolic name into the key sequence.
5136
5137 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
5138 `double-' events into similar click events, if that would make them
5139 bound. We try to turn `triple-' events first into `double-' events,
5140 then into clicks.
5141
5142 If we get a mouse click in a mode line, vertical divider, or other
5143 non-text area, we treat the click as if it were prefixed by the
5144 symbol denoting that area - `mode-line', `vertical-line', or
5145 whatever.
5146
5147 If the sequence starts with a mouse click, we read the key sequence
5148 with respect to the buffer clicked on, not the current buffer.
5149
5150 If the user switches frames in the midst of a key sequence, we put
5151 off the switch-frame event until later; the next call to
5152 read_char will return it. */
5153
5154 static int
5155 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
5156 can_return_switch_frame)
5157 Lisp_Object *keybuf;
5158 int bufsize;
5159 Lisp_Object prompt;
5160 int dont_downcase_last;
5161 int can_return_switch_frame;
5162 {
5163 int count = specpdl_ptr - specpdl;
5164
5165 /* How many keys there are in the current key sequence. */
5166 int t;
5167
5168 /* The length of the echo buffer when we started reading, and
5169 the length of this_command_keys when we started reading. */
5170 int echo_start;
5171 int keys_start;
5172
5173 /* The number of keymaps we're scanning right now, and the number of
5174 keymaps we have allocated space for. */
5175 int nmaps;
5176 int nmaps_allocated = 0;
5177
5178 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
5179 the current keymaps. */
5180 Lisp_Object *defs;
5181
5182 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
5183 in the current keymaps, or nil where it is not a prefix. */
5184 Lisp_Object *submaps;
5185
5186 /* The local map to start out with at start of key sequence. */
5187 Lisp_Object orig_local_map;
5188
5189 /* 1 if we have already considered switching to the local-map property
5190 of the place where a mouse click occurred. */
5191 int localized_local_map = 0;
5192
5193 /* The index in defs[] of the first keymap that has a binding for
5194 this key sequence. In other words, the lowest i such that
5195 defs[i] is non-nil. */
5196 int first_binding;
5197
5198 /* If t < mock_input, then KEYBUF[t] should be read as the next
5199 input key.
5200
5201 We use this to recover after recognizing a function key. Once we
5202 realize that a suffix of the current key sequence is actually a
5203 function key's escape sequence, we replace the suffix with the
5204 function key's binding from Vfunction_key_map. Now keybuf
5205 contains a new and different key sequence, so the echo area,
5206 this_command_keys, and the submaps and defs arrays are wrong. In
5207 this situation, we set mock_input to t, set t to 0, and jump to
5208 restart_sequence; the loop will read keys from keybuf up until
5209 mock_input, thus rebuilding the state; and then it will resume
5210 reading characters from the keyboard. */
5211 int mock_input = 0;
5212
5213 /* If the sequence is unbound in submaps[], then
5214 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
5215 and fkey_map is its binding.
5216
5217 These might be > t, indicating that all function key scanning
5218 should hold off until t reaches them. We do this when we've just
5219 recognized a function key, to avoid searching for the function
5220 key's again in Vfunction_key_map. */
5221 int fkey_start = 0, fkey_end = 0;
5222 Lisp_Object fkey_map;
5223
5224 /* Likewise, for key_translation_map. */
5225 int keytran_start = 0, keytran_end = 0;
5226 Lisp_Object keytran_map;
5227
5228 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
5229 we put it off for later. While we're reading, we keep the event here. */
5230 Lisp_Object delayed_switch_frame;
5231
5232 /* See the comment below... */
5233 #if defined (GOBBLE_FIRST_EVENT)
5234 Lisp_Object first_event;
5235 #endif
5236
5237 Lisp_Object original_uppercase;
5238 int original_uppercase_position = -1;
5239
5240 /* Gets around Microsoft compiler limitations. */
5241 int dummyflag = 0;
5242
5243 struct buffer *starting_buffer;
5244
5245 /* Nonzero if we seem to have got the beginning of a binding
5246 in function_key_map. */
5247 int function_key_possible = 0;
5248 int key_translation_possible = 0;
5249
5250 /* Save the status of key translation before each step,
5251 so that we can restore this after downcasing. */
5252 Lisp_Object prev_fkey_map;
5253 Lisp_Object prev_fkey_start;
5254 Lisp_Object prev_fkey_end;
5255
5256 Lisp_Object prev_keytran_map;
5257 Lisp_Object prev_keytran_start;
5258 Lisp_Object prev_keytran_end;
5259
5260 int junk;
5261
5262 last_nonmenu_event = Qnil;
5263
5264 delayed_switch_frame = Qnil;
5265 fkey_map = Vfunction_key_map;
5266 keytran_map = Vkey_translation_map;
5267
5268 /* If there is no function-key-map, turn off function key scanning. */
5269 if (NILP (Fkeymapp (Vfunction_key_map)))
5270 fkey_start = fkey_end = bufsize + 1;
5271
5272 /* If there is no key-translation-map, turn off scanning. */
5273 if (NILP (Fkeymapp (Vkey_translation_map)))
5274 keytran_start = keytran_end = bufsize + 1;
5275
5276 if (INTERACTIVE)
5277 {
5278 if (!NILP (prompt))
5279 echo_prompt (XSTRING (prompt)->data);
5280 else if (cursor_in_echo_area && echo_keystrokes)
5281 /* This doesn't put in a dash if the echo buffer is empty, so
5282 you don't always see a dash hanging out in the minibuffer. */
5283 echo_dash ();
5284 }
5285
5286 /* Record the initial state of the echo area and this_command_keys;
5287 we will need to restore them if we replay a key sequence. */
5288 if (INTERACTIVE)
5289 echo_start = echo_length ();
5290 keys_start = this_command_key_count;
5291
5292 #if defined (GOBBLE_FIRST_EVENT)
5293 /* This doesn't quite work, because some of the things that read_char
5294 does cannot safely be bypassed. It seems too risky to try to make
5295 this work right. */
5296
5297 /* Read the first char of the sequence specially, before setting
5298 up any keymaps, in case a filter runs and switches buffers on us. */
5299 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
5300 &junk);
5301 #endif /* GOBBLE_FIRST_EVENT */
5302
5303 orig_local_map = get_local_map (PT, current_buffer);
5304
5305 /* We jump here when the key sequence has been thoroughly changed, and
5306 we need to rescan it starting from the beginning. When we jump here,
5307 keybuf[0..mock_input] holds the sequence we should reread. */
5308 replay_sequence:
5309
5310 starting_buffer = current_buffer;
5311 function_key_possible = 0;
5312 key_translation_possible = 0;
5313
5314 /* Build our list of keymaps.
5315 If we recognize a function key and replace its escape sequence in
5316 keybuf with its symbol, or if the sequence starts with a mouse
5317 click and we need to switch buffers, we jump back here to rebuild
5318 the initial keymaps from the current buffer. */
5319 {
5320 Lisp_Object *maps;
5321
5322 if (!NILP (current_kboard->Voverriding_terminal_local_map)
5323 || !NILP (Voverriding_local_map))
5324 {
5325 if (3 > nmaps_allocated)
5326 {
5327 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
5328 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
5329 nmaps_allocated = 3;
5330 }
5331 nmaps = 0;
5332 if (!NILP (current_kboard->Voverriding_terminal_local_map))
5333 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
5334 if (!NILP (Voverriding_local_map))
5335 submaps[nmaps++] = Voverriding_local_map;
5336 }
5337 else
5338 {
5339 nmaps = current_minor_maps (0, &maps);
5340 if (nmaps + 2 > nmaps_allocated)
5341 {
5342 submaps = (Lisp_Object *) alloca ((nmaps+2) * sizeof (submaps[0]));
5343 defs = (Lisp_Object *) alloca ((nmaps+2) * sizeof (defs[0]));
5344 nmaps_allocated = nmaps + 2;
5345 }
5346 bcopy (maps, submaps, nmaps * sizeof (submaps[0]));
5347 #ifdef USE_TEXT_PROPERTIES
5348 submaps[nmaps++] = orig_local_map;
5349 #else
5350 submaps[nmaps++] = current_buffer->keymap;
5351 #endif
5352 }
5353 submaps[nmaps++] = current_global_map;
5354 }
5355
5356 /* Find an accurate initial value for first_binding. */
5357 for (first_binding = 0; first_binding < nmaps; first_binding++)
5358 if (! NILP (submaps[first_binding]))
5359 break;
5360
5361 /* Start from the beginning in keybuf. */
5362 t = 0;
5363
5364 /* These are no-ops the first time through, but if we restart, they
5365 revert the echo area and this_command_keys to their original state. */
5366 this_command_key_count = keys_start;
5367 if (INTERACTIVE && t < mock_input)
5368 echo_truncate (echo_start);
5369
5370 /* If the best binding for the current key sequence is a keymap, or
5371 we may be looking at a function key's escape sequence, keep on
5372 reading. */
5373 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
5374 || (first_binding >= nmaps
5375 && fkey_start < t
5376 /* mock input is never part of a function key's sequence. */
5377 && mock_input <= fkey_start)
5378 || (first_binding >= nmaps
5379 && keytran_start < t && key_translation_possible)
5380 /* Don't return in the middle of a possible function key sequence,
5381 if the only bindings we found were via case conversion.
5382 Thus, if ESC O a has a function-key-map translation
5383 and ESC o has a binding, don't return after ESC O,
5384 so that we can translate ESC O plus the next character. */
5385 )
5386 {
5387 Lisp_Object key;
5388 int used_mouse_menu = 0;
5389
5390 /* Where the last real key started. If we need to throw away a
5391 key that has expanded into more than one element of keybuf
5392 (say, a mouse click on the mode line which is being treated
5393 as [mode-line (mouse-...)], then we backtrack to this point
5394 of keybuf. */
5395 int last_real_key_start;
5396
5397 /* These variables are analogous to echo_start and keys_start;
5398 while those allow us to restart the entire key sequence,
5399 echo_local_start and keys_local_start allow us to throw away
5400 just one key. */
5401 int echo_local_start, keys_local_start, local_first_binding;
5402
5403 if (t >= bufsize)
5404 error ("Key sequence too long");
5405
5406 if (INTERACTIVE)
5407 echo_local_start = echo_length ();
5408 keys_local_start = this_command_key_count;
5409 local_first_binding = first_binding;
5410
5411 replay_key:
5412 /* These are no-ops, unless we throw away a keystroke below and
5413 jumped back up to replay_key; in that case, these restore the
5414 variables to their original state, allowing us to replay the
5415 loop. */
5416 if (INTERACTIVE && t < mock_input)
5417 echo_truncate (echo_local_start);
5418 this_command_key_count = keys_local_start;
5419 first_binding = local_first_binding;
5420
5421 /* By default, assume each event is "real". */
5422 last_real_key_start = t;
5423
5424 /* Does mock_input indicate that we are re-reading a key sequence? */
5425 if (t < mock_input)
5426 {
5427 key = keybuf[t];
5428 add_command_key (key);
5429 if (echo_keystrokes)
5430 echo_char (key);
5431 }
5432
5433 /* If not, we should actually read a character. */
5434 else
5435 {
5436 struct buffer *buf = current_buffer;
5437
5438 {
5439 #ifdef MULTI_KBOARD
5440 KBOARD *interrupted_kboard = current_kboard;
5441 struct frame *interrupted_frame = selected_frame;
5442 if (setjmp (wrong_kboard_jmpbuf))
5443 {
5444 if (!NILP (delayed_switch_frame))
5445 {
5446 interrupted_kboard->kbd_queue
5447 = Fcons (delayed_switch_frame,
5448 interrupted_kboard->kbd_queue);
5449 delayed_switch_frame = Qnil;
5450 }
5451 while (t > 0)
5452 interrupted_kboard->kbd_queue
5453 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
5454
5455 /* If the side queue is non-empty, ensure it begins with a
5456 switch-frame, so we'll replay it in the right context. */
5457 if (CONSP (interrupted_kboard->kbd_queue)
5458 && (key = XCONS (interrupted_kboard->kbd_queue)->car,
5459 !(EVENT_HAS_PARAMETERS (key)
5460 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
5461 Qswitch_frame))))
5462 {
5463 Lisp_Object frame;
5464 XSETFRAME (frame, interrupted_frame);
5465 interrupted_kboard->kbd_queue
5466 = Fcons (make_lispy_switch_frame (frame),
5467 interrupted_kboard->kbd_queue);
5468 }
5469 mock_input = 0;
5470 orig_local_map = get_local_map (PT, current_buffer);
5471 goto replay_sequence;
5472 }
5473 #endif
5474 key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
5475 &used_mouse_menu);
5476 }
5477
5478 /* read_char returns t when it shows a menu and the user rejects it.
5479 Just return -1. */
5480 if (EQ (key, Qt))
5481 return -1;
5482
5483 /* read_char returns -1 at the end of a macro.
5484 Emacs 18 handles this by returning immediately with a
5485 zero, so that's what we'll do. */
5486 if (INTEGERP (key) && XINT (key) == -1)
5487 {
5488 t = 0;
5489 /* The Microsoft C compiler can't handle the goto that
5490 would go here. */
5491 dummyflag = 1;
5492 break;
5493 }
5494
5495 /* If the current buffer has been changed from under us, the
5496 keymap may have changed, so replay the sequence. */
5497 if (BUFFERP (key))
5498 {
5499 mock_input = t;
5500 orig_local_map = get_local_map (PT, current_buffer);
5501 goto replay_sequence;
5502 }
5503
5504 /* If we have a quit that was typed in another frame, and
5505 quit_throw_to_read_char switched buffers,
5506 replay to get the right keymap. */
5507 if (XINT (key) == quit_char && current_buffer != starting_buffer)
5508 {
5509 keybuf[t++] = key;
5510 mock_input = t;
5511 Vquit_flag = Qnil;
5512 orig_local_map = get_local_map (PT, current_buffer);
5513 goto replay_sequence;
5514 }
5515
5516 Vquit_flag = Qnil;
5517 }
5518
5519 /* Clicks in non-text areas get prefixed by the symbol
5520 in their CHAR-ADDRESS field. For example, a click on
5521 the mode line is prefixed by the symbol `mode-line'.
5522
5523 Furthermore, key sequences beginning with mouse clicks
5524 are read using the keymaps of the buffer clicked on, not
5525 the current buffer. So we may have to switch the buffer
5526 here.
5527
5528 When we turn one event into two events, we must make sure
5529 that neither of the two looks like the original--so that,
5530 if we replay the events, they won't be expanded again.
5531 If not for this, such reexpansion could happen either here
5532 or when user programs play with this-command-keys. */
5533 if (EVENT_HAS_PARAMETERS (key))
5534 {
5535 Lisp_Object kind;
5536
5537 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
5538 if (EQ (kind, Qmouse_click))
5539 {
5540 Lisp_Object window, posn;
5541
5542 window = POSN_WINDOW (EVENT_START (key));
5543 posn = POSN_BUFFER_POSN (EVENT_START (key));
5544 if (CONSP (posn))
5545 {
5546 /* We're looking at the second event of a
5547 sequence which we expanded before. Set
5548 last_real_key_start appropriately. */
5549 if (t > 0)
5550 last_real_key_start = t - 1;
5551 }
5552
5553 /* Key sequences beginning with mouse clicks are
5554 read using the keymaps in the buffer clicked on,
5555 not the current buffer. If we're at the
5556 beginning of a key sequence, switch buffers. */
5557 if (last_real_key_start == 0
5558 && WINDOWP (window)
5559 && BUFFERP (XWINDOW (window)->buffer)
5560 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
5561 {
5562 keybuf[t] = key;
5563 mock_input = t + 1;
5564
5565 /* Arrange to go back to the original buffer once we're
5566 done reading the key sequence. Note that we can't
5567 use save_excursion_{save,restore} here, because they
5568 save point as well as the current buffer; we don't
5569 want to save point, because redisplay may change it,
5570 to accommodate a Fset_window_start or something. We
5571 don't want to do this at the top of the function,
5572 because we may get input from a subprocess which
5573 wants to change the selected window and stuff (say,
5574 emacsclient). */
5575 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
5576
5577 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
5578 orig_local_map = get_local_map (PT, current_buffer);
5579 goto replay_sequence;
5580 }
5581 /* For a mouse click, get the local text-property keymap
5582 of the place clicked on, rather than point. */
5583 if (last_real_key_start == 0 && CONSP (XCONS (key)->cdr)
5584 && ! localized_local_map)
5585 {
5586 Lisp_Object map_here, start, pos;
5587
5588 localized_local_map = 1;
5589 start = EVENT_START (key);
5590 if (CONSP (start) && CONSP (XCONS (start)->cdr))
5591 {
5592 pos = POSN_BUFFER_POSN (start);
5593 if (INTEGERP (pos)
5594 && XINT (pos) >= BEG && XINT (pos) <= Z)
5595 {
5596 map_here = get_local_map (XINT (pos), current_buffer);
5597 if (!EQ (map_here, orig_local_map))
5598 {
5599 orig_local_map = map_here;
5600 keybuf[t] = key;
5601 mock_input = t + 1;
5602
5603 goto replay_sequence;
5604 }
5605 }
5606 }
5607 }
5608
5609 /* Expand mode-line and scroll-bar events into two events:
5610 use posn as a fake prefix key. */
5611 if (SYMBOLP (posn))
5612 {
5613 if (t + 1 >= bufsize)
5614 error ("Key sequence too long");
5615 keybuf[t] = posn;
5616 keybuf[t+1] = key;
5617 mock_input = t + 2;
5618
5619 /* Zap the position in key, so we know that we've
5620 expanded it, and don't try to do so again. */
5621 POSN_BUFFER_POSN (EVENT_START (key))
5622 = Fcons (posn, Qnil);
5623 goto replay_key;
5624 }
5625 }
5626 else if (EQ (kind, Qswitch_frame))
5627 {
5628 /* If we're at the beginning of a key sequence, and the caller
5629 says it's okay, go ahead and return this event. If we're
5630 in the midst of a key sequence, delay it until the end. */
5631 if (t > 0 || !can_return_switch_frame)
5632 {
5633 delayed_switch_frame = key;
5634 goto replay_key;
5635 }
5636 }
5637 else if (CONSP (XCONS (key)->cdr)
5638 && CONSP (EVENT_START (key))
5639 && CONSP (XCONS (EVENT_START (key))->cdr))
5640 {
5641 Lisp_Object posn;
5642
5643 posn = POSN_BUFFER_POSN (EVENT_START (key));
5644 /* Handle menu-bar events:
5645 insert the dummy prefix event `menu-bar'. */
5646 if (EQ (posn, Qmenu_bar))
5647 {
5648 if (t + 1 >= bufsize)
5649 error ("Key sequence too long");
5650 keybuf[t] = posn;
5651 keybuf[t+1] = key;
5652
5653 /* Zap the position in key, so we know that we've
5654 expanded it, and don't try to do so again. */
5655 POSN_BUFFER_POSN (EVENT_START (key))
5656 = Fcons (posn, Qnil);
5657
5658 mock_input = t + 2;
5659 goto replay_sequence;
5660 }
5661 else if (CONSP (posn))
5662 {
5663 /* We're looking at the second event of a
5664 sequence which we expanded before. Set
5665 last_real_key_start appropriately. */
5666 if (last_real_key_start == t && t > 0)
5667 last_real_key_start = t - 1;
5668 }
5669 }
5670 }
5671
5672 /* We have finally decided that KEY is something we might want
5673 to look up. */
5674 first_binding = (follow_key (key,
5675 nmaps - first_binding,
5676 submaps + first_binding,
5677 defs + first_binding,
5678 submaps + first_binding)
5679 + first_binding);
5680
5681 /* If KEY wasn't bound, we'll try some fallbacks. */
5682 if (first_binding >= nmaps)
5683 {
5684 Lisp_Object head;
5685
5686 head = EVENT_HEAD (key);
5687 if (help_char_p (head) && t > 0)
5688 {
5689 read_key_sequence_cmd = Vprefix_help_command;
5690 keybuf[t++] = key;
5691 last_nonmenu_event = key;
5692 /* The Microsoft C compiler can't handle the goto that
5693 would go here. */
5694 dummyflag = 1;
5695 break;
5696 }
5697
5698 if (SYMBOLP (head))
5699 {
5700 Lisp_Object breakdown;
5701 int modifiers;
5702
5703 breakdown = parse_modifiers (head);
5704 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
5705 /* Attempt to reduce an unbound mouse event to a simpler
5706 event that is bound:
5707 Drags reduce to clicks.
5708 Double-clicks reduce to clicks.
5709 Triple-clicks reduce to double-clicks, then to clicks.
5710 Down-clicks are eliminated.
5711 Double-downs reduce to downs, then are eliminated.
5712 Triple-downs reduce to double-downs, then to downs,
5713 then are eliminated. */
5714 if (modifiers & (down_modifier | drag_modifier
5715 | double_modifier | triple_modifier))
5716 {
5717 while (modifiers & (down_modifier | drag_modifier
5718 | double_modifier | triple_modifier))
5719 {
5720 Lisp_Object new_head, new_click;
5721 if (modifiers & triple_modifier)
5722 modifiers ^= (double_modifier | triple_modifier);
5723 else if (modifiers & double_modifier)
5724 modifiers &= ~double_modifier;
5725 else if (modifiers & drag_modifier)
5726 modifiers &= ~drag_modifier;
5727 else
5728 {
5729 /* Dispose of this `down' event by simply jumping
5730 back to replay_key, to get another event.
5731
5732 Note that if this event came from mock input,
5733 then just jumping back to replay_key will just
5734 hand it to us again. So we have to wipe out any
5735 mock input.
5736
5737 We could delete keybuf[t] and shift everything
5738 after that to the left by one spot, but we'd also
5739 have to fix up any variable that points into
5740 keybuf, and shifting isn't really necessary
5741 anyway.
5742
5743 Adding prefixes for non-textual mouse clicks
5744 creates two characters of mock input, and both
5745 must be thrown away. If we're only looking at
5746 the prefix now, we can just jump back to
5747 replay_key. On the other hand, if we've already
5748 processed the prefix, and now the actual click
5749 itself is giving us trouble, then we've lost the
5750 state of the keymaps we want to backtrack to, and
5751 we need to replay the whole sequence to rebuild
5752 it.
5753
5754 Beyond that, only function key expansion could
5755 create more than two keys, but that should never
5756 generate mouse events, so it's okay to zero
5757 mock_input in that case too.
5758
5759 Isn't this just the most wonderful code ever? */
5760 if (t == last_real_key_start)
5761 {
5762 mock_input = 0;
5763 goto replay_key;
5764 }
5765 else
5766 {
5767 mock_input = last_real_key_start;
5768 goto replay_sequence;
5769 }
5770 }
5771
5772 new_head
5773 = apply_modifiers (modifiers, XCONS (breakdown)->car);
5774 new_click
5775 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
5776
5777 /* Look for a binding for this new key. follow_key
5778 promises that it didn't munge submaps the
5779 last time we called it, since key was unbound. */
5780 first_binding
5781 = (follow_key (new_click,
5782 nmaps - local_first_binding,
5783 submaps + local_first_binding,
5784 defs + local_first_binding,
5785 submaps + local_first_binding)
5786 + local_first_binding);
5787
5788 /* If that click is bound, go for it. */
5789 if (first_binding < nmaps)
5790 {
5791 key = new_click;
5792 break;
5793 }
5794 /* Otherwise, we'll leave key set to the drag event. */
5795 }
5796 }
5797 }
5798 }
5799
5800 keybuf[t++] = key;
5801 /* Normally, last_nonmenu_event gets the previous key we read.
5802 But when a mouse popup menu is being used,
5803 we don't update last_nonmenu_event; it continues to hold the mouse
5804 event that preceded the first level of menu. */
5805 if (!used_mouse_menu)
5806 last_nonmenu_event = key;
5807
5808 prev_fkey_map = fkey_map;
5809 prev_fkey_start = fkey_start;
5810 prev_fkey_end = fkey_end;
5811
5812 prev_keytran_map = keytran_map;
5813 prev_keytran_start = keytran_start;
5814 prev_keytran_end = keytran_end;
5815
5816 /* If the sequence is unbound, see if we can hang a function key
5817 off the end of it. We only want to scan real keyboard input
5818 for function key sequences, so if mock_input says that we're
5819 re-reading old events, don't examine it. */
5820 if (first_binding >= nmaps
5821 && t >= mock_input)
5822 {
5823 Lisp_Object fkey_next;
5824
5825 /* Continue scan from fkey_end until we find a bound suffix.
5826 If we fail, increment fkey_start
5827 and start fkey_end from there. */
5828 while (fkey_end < t)
5829 {
5830 Lisp_Object key;
5831
5832 key = keybuf[fkey_end++];
5833 /* Look up meta-characters by prefixing them
5834 with meta_prefix_char. I hate this. */
5835 if (INTEGERP (key) && XINT (key) & meta_modifier)
5836 {
5837 fkey_next
5838 = get_keymap_1
5839 (get_keyelt
5840 (access_keymap (fkey_map, meta_prefix_char, 1, 0)),
5841 0, 1);
5842 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
5843 }
5844 else
5845 fkey_next = fkey_map;
5846
5847 fkey_next
5848 = get_keyelt (access_keymap (fkey_next, key, 1, 0));
5849
5850 #if 0 /* I didn't turn this on, because it might cause trouble
5851 for the mapping of return into C-m and tab into C-i. */
5852 /* Optionally don't map function keys into other things.
5853 This enables the user to redefine kp- keys easily. */
5854 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
5855 fkey_next = Qnil;
5856 #endif
5857
5858 /* If the function key map gives a function, not an
5859 array, then call the function with no args and use
5860 its value instead. */
5861 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
5862 && fkey_end == t)
5863 {
5864 struct gcpro gcpro1, gcpro2, gcpro3;
5865 Lisp_Object tem;
5866 tem = fkey_next;
5867
5868 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
5869 fkey_next = call1 (fkey_next, prompt);
5870 UNGCPRO;
5871 /* If the function returned something invalid,
5872 barf--don't ignore it.
5873 (To ignore it safely, we would need to gcpro a bunch of
5874 other variables.) */
5875 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
5876 error ("Function in function-key-map returns invalid key sequence");
5877 }
5878
5879 function_key_possible = ! NILP (fkey_next);
5880
5881 /* If keybuf[fkey_start..fkey_end] is bound in the
5882 function key map and it's a suffix of the current
5883 sequence (i.e. fkey_end == t), replace it with
5884 the binding and restart with fkey_start at the end. */
5885 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
5886 && fkey_end == t)
5887 {
5888 int len = XFASTINT (Flength (fkey_next));
5889
5890 t = fkey_start + len;
5891 if (t >= bufsize)
5892 error ("Key sequence too long");
5893
5894 if (VECTORP (fkey_next))
5895 bcopy (XVECTOR (fkey_next)->contents,
5896 keybuf + fkey_start,
5897 (t - fkey_start) * sizeof (keybuf[0]));
5898 else if (STRINGP (fkey_next))
5899 {
5900 int i;
5901
5902 for (i = 0; i < len; i++)
5903 XSETFASTINT (keybuf[fkey_start + i],
5904 XSTRING (fkey_next)->data[i]);
5905 }
5906
5907 mock_input = t;
5908 fkey_start = fkey_end = t;
5909 fkey_map = Vfunction_key_map;
5910
5911 /* Do pass the results through key-translation-map. */
5912 keytran_start = keytran_end = 0;
5913 keytran_map = Vkey_translation_map;
5914
5915 goto replay_sequence;
5916 }
5917
5918 fkey_map = get_keymap_1 (fkey_next, 0, 1);
5919
5920 /* If we no longer have a bound suffix, try a new positions for
5921 fkey_start. */
5922 if (NILP (fkey_map))
5923 {
5924 fkey_end = ++fkey_start;
5925 fkey_map = Vfunction_key_map;
5926 function_key_possible = 0;
5927 }
5928 }
5929 }
5930
5931 /* Look for this sequence in key-translation-map. */
5932 {
5933 Lisp_Object keytran_next;
5934
5935 /* Scan from keytran_end until we find a bound suffix. */
5936 while (keytran_end < t)
5937 {
5938 Lisp_Object key;
5939
5940 key = keybuf[keytran_end++];
5941 /* Look up meta-characters by prefixing them
5942 with meta_prefix_char. I hate this. */
5943 if (INTEGERP (key) && XINT (key) & meta_modifier)
5944 {
5945 keytran_next
5946 = get_keymap_1
5947 (get_keyelt
5948 (access_keymap (keytran_map, meta_prefix_char, 1, 0)),
5949 0, 1);
5950 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
5951 }
5952 else
5953 keytran_next = keytran_map;
5954
5955 keytran_next
5956 = get_keyelt (access_keymap (keytran_next, key, 1, 0));
5957
5958 /* If the key translation map gives a function, not an
5959 array, then call the function with no args and use
5960 its value instead. */
5961 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
5962 && keytran_end == t)
5963 {
5964 struct gcpro gcpro1, gcpro2, gcpro3;
5965 Lisp_Object tem;
5966 tem = keytran_next;
5967
5968 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
5969 keytran_next = call1 (keytran_next, prompt);
5970 UNGCPRO;
5971 /* If the function returned something invalid,
5972 barf--don't ignore it.
5973 (To ignore it safely, we would need to gcpro a bunch of
5974 other variables.) */
5975 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
5976 error ("Function in key-translation-map returns invalid key sequence");
5977 }
5978
5979 key_translation_possible = ! NILP (keytran_next);
5980
5981 /* If keybuf[keytran_start..keytran_end] is bound in the
5982 key translation map and it's a suffix of the current
5983 sequence (i.e. keytran_end == t), replace it with
5984 the binding and restart with keytran_start at the end. */
5985 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
5986 && keytran_end == t)
5987 {
5988 int len = XFASTINT (Flength (keytran_next));
5989
5990 t = keytran_start + len;
5991 if (t >= bufsize)
5992 error ("Key sequence too long");
5993
5994 if (VECTORP (keytran_next))
5995 bcopy (XVECTOR (keytran_next)->contents,
5996 keybuf + keytran_start,
5997 (t - keytran_start) * sizeof (keybuf[0]));
5998 else if (STRINGP (keytran_next))
5999 {
6000 int i;
6001
6002 for (i = 0; i < len; i++)
6003 XSETFASTINT (keybuf[keytran_start + i],
6004 XSTRING (keytran_next)->data[i]);
6005 }
6006
6007 mock_input = t;
6008 keytran_start = keytran_end = t;
6009 keytran_map = Vkey_translation_map;
6010
6011 /* Don't pass the results of key-translation-map
6012 through function-key-map. */
6013 fkey_start = fkey_end = t;
6014 fkey_map = Vkey_translation_map;
6015
6016 goto replay_sequence;
6017 }
6018
6019 keytran_map = get_keymap_1 (keytran_next, 0, 1);
6020
6021 /* If we no longer have a bound suffix, try a new positions for
6022 keytran_start. */
6023 if (NILP (keytran_map))
6024 {
6025 keytran_end = ++keytran_start;
6026 keytran_map = Vkey_translation_map;
6027 key_translation_possible = 0;
6028 }
6029 }
6030 }
6031
6032 /* If KEY is not defined in any of the keymaps,
6033 and cannot be part of a function key or translation,
6034 and is an upper case letter
6035 use the corresponding lower-case letter instead. */
6036 if (first_binding == nmaps && ! function_key_possible
6037 && ! key_translation_possible
6038 && INTEGERP (key)
6039 && ((((XINT (key) & 0x3ffff)
6040 < XSTRING (current_buffer->downcase_table)->size)
6041 && UPPERCASEP (XINT (key) & 0x3ffff))
6042 || (XINT (key) & shift_modifier)))
6043 {
6044 Lisp_Object new_key;
6045
6046 original_uppercase = key;
6047 original_uppercase_position = t - 1;
6048
6049 if (XINT (new_key) & shift_modifier)
6050 XSETINT (new_key, XINT (key) & ~shift_modifier);
6051 else
6052 XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
6053 | (XINT (key) & ~0x3ffff)));
6054
6055 /* We have to do this unconditionally, regardless of whether
6056 the lower-case char is defined in the keymaps, because they
6057 might get translated through function-key-map. */
6058 keybuf[t - 1] = new_key;
6059 mock_input = t;
6060
6061 fkey_map = prev_fkey_map;
6062 fkey_start = prev_fkey_start;
6063 fkey_end = prev_fkey_end;
6064
6065 keytran_map = prev_keytran_map;
6066 keytran_start = prev_keytran_start;
6067 keytran_end = prev_keytran_end;
6068
6069 goto replay_sequence;
6070 }
6071 /* If KEY is not defined in any of the keymaps,
6072 and cannot be part of a function key or translation,
6073 and is a shifted function key,
6074 use the corresponding unshifted function key instead. */
6075 if (first_binding == nmaps && ! function_key_possible
6076 && ! key_translation_possible
6077 && SYMBOLP (key))
6078 {
6079 Lisp_Object breakdown;
6080 int modifiers;
6081
6082 breakdown = parse_modifiers (key);
6083 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
6084 if (modifiers & shift_modifier)
6085 {
6086 Lisp_Object new_key;
6087
6088 original_uppercase = key;
6089 original_uppercase_position = t - 1;
6090
6091 modifiers &= ~shift_modifier;
6092 new_key = apply_modifiers (modifiers,
6093 XCONS (breakdown)->car);
6094
6095 keybuf[t - 1] = new_key;
6096 mock_input = t;
6097
6098 fkey_map = prev_fkey_map;
6099 fkey_start = prev_fkey_start;
6100 fkey_end = prev_fkey_end;
6101
6102 keytran_map = prev_keytran_map;
6103 keytran_start = prev_keytran_start;
6104 keytran_end = prev_keytran_end;
6105
6106 goto replay_sequence;
6107 }
6108 }
6109 }
6110
6111 if (!dummyflag)
6112 read_key_sequence_cmd = (first_binding < nmaps
6113 ? defs[first_binding]
6114 : Qnil);
6115
6116 unread_switch_frame = delayed_switch_frame;
6117 unbind_to (count, Qnil);
6118
6119 /* Don't downcase the last character if the caller says don't.
6120 Don't downcase it if the result is undefined, either. */
6121 if ((dont_downcase_last || first_binding >= nmaps)
6122 && t - 1 == original_uppercase_position)
6123 keybuf[t - 1] = original_uppercase;
6124
6125 /* Occasionally we fabricate events, perhaps by expanding something
6126 according to function-key-map, or by adding a prefix symbol to a
6127 mouse click in the scroll bar or modeline. In this cases, return
6128 the entire generated key sequence, even if we hit an unbound
6129 prefix or a definition before the end. This means that you will
6130 be able to push back the event properly, and also means that
6131 read-key-sequence will always return a logical unit.
6132
6133 Better ideas? */
6134 for (; t < mock_input; t++)
6135 {
6136 if (echo_keystrokes)
6137 echo_char (keybuf[t]);
6138 add_command_key (keybuf[t]);
6139 }
6140
6141 return t;
6142 }
6143
6144 #if 0 /* This doc string is too long for some compilers.
6145 This commented-out definition serves for DOC. */
6146 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
6147 "Read a sequence of keystrokes and return as a string or vector.\n\
6148 The sequence is sufficient to specify a non-prefix command in the\n\
6149 current local and global maps.\n\
6150 \n\
6151 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
6152 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
6153 as a continuation of the previous key.\n\
6154 \n\
6155 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
6156 convert the last event to lower case. (Normally any upper case event\n\
6157 is converted to lower case if the original event is undefined and the lower\n\
6158 case equivalent is defined.) A non-nil value is appropriate for reading\n\
6159 a key sequence to be defined.\n\
6160 \n\
6161 A C-g typed while in this function is treated like any other character,\n\
6162 and `quit-flag' is not set.\n\
6163 \n\
6164 If the key sequence starts with a mouse click, then the sequence is read\n\
6165 using the keymaps of the buffer of the window clicked in, not the buffer\n\
6166 of the selected window as normal.\n\
6167 ""\n\
6168 `read-key-sequence' drops unbound button-down events, since you normally\n\
6169 only care about the click or drag events which follow them. If a drag\n\
6170 or multi-click event is unbound, but the corresponding click event would\n\
6171 be bound, `read-key-sequence' turns the event into a click event at the\n\
6172 drag's starting position. This means that you don't have to distinguish\n\
6173 between click and drag, double, or triple events unless you want to.\n\
6174 \n\
6175 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
6176 lines separating windows, and scroll bars with imaginary keys\n\
6177 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
6178 \n\
6179 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
6180 function will process a switch-frame event if the user switches frames\n\
6181 before typing anything. If the user switches frames in the middle of a\n\
6182 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
6183 is nil, then the event will be put off until after the current key sequence.\n\
6184 \n\
6185 `read-key-sequence' checks `function-key-map' for function key\n\
6186 sequences, where they wouldn't conflict with ordinary bindings. See\n\
6187 `function-key-map' for more details.")
6188 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
6189 #endif
6190
6191 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
6192 0)
6193 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
6194 Lisp_Object prompt, continue_echo, dont_downcase_last;
6195 Lisp_Object can_return_switch_frame;
6196 {
6197 Lisp_Object keybuf[30];
6198 register int i;
6199 struct gcpro gcpro1, gcpro2;
6200
6201 if (!NILP (prompt))
6202 CHECK_STRING (prompt, 0);
6203 QUIT;
6204
6205 bzero (keybuf, sizeof keybuf);
6206 GCPRO1 (keybuf[0]);
6207 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
6208
6209 if (NILP (continue_echo))
6210 this_command_key_count = 0;
6211
6212 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
6213 prompt, ! NILP (dont_downcase_last),
6214 ! NILP (can_return_switch_frame));
6215
6216 if (i == -1)
6217 {
6218 Vquit_flag = Qt;
6219 QUIT;
6220 }
6221 UNGCPRO;
6222 return make_event_array (i, keybuf);
6223 }
6224 \f
6225 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
6226 "Execute CMD as an editor command.\n\
6227 CMD must be a symbol that satisfies the `commandp' predicate.\n\
6228 Optional second arg RECORD-FLAG non-nil\n\
6229 means unconditionally put this command in `command-history'.\n\
6230 Otherwise, that is done only if an arg is read using the minibuffer.")
6231 (cmd, record)
6232 Lisp_Object cmd, record;
6233 {
6234 register Lisp_Object final;
6235 register Lisp_Object tem;
6236 Lisp_Object prefixarg;
6237 struct backtrace backtrace;
6238 extern int debug_on_next_call;
6239
6240 prefixarg = current_kboard->Vprefix_arg;
6241 current_kboard->Vprefix_arg = Qnil;
6242 Vcurrent_prefix_arg = prefixarg;
6243 debug_on_next_call = 0;
6244
6245 if (SYMBOLP (cmd))
6246 {
6247 tem = Fget (cmd, Qdisabled);
6248 if (!NILP (tem) && !NILP (Vrun_hooks))
6249 {
6250 tem = Fsymbol_value (Qdisabled_command_hook);
6251 if (!NILP (tem))
6252 return call1 (Vrun_hooks, Qdisabled_command_hook);
6253 }
6254 }
6255
6256 while (1)
6257 {
6258 final = Findirect_function (cmd);
6259
6260 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
6261 do_autoload (final, cmd);
6262 else
6263 break;
6264 }
6265
6266 if (STRINGP (final) || VECTORP (final))
6267 {
6268 /* If requested, place the macro in the command history. For
6269 other sorts of commands, call-interactively takes care of
6270 this. */
6271 if (!NILP (record))
6272 Vcommand_history
6273 = Fcons (Fcons (Qexecute_kbd_macro,
6274 Fcons (final, Fcons (prefixarg, Qnil))),
6275 Vcommand_history);
6276
6277 return Fexecute_kbd_macro (final, prefixarg);
6278 }
6279 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
6280 {
6281 backtrace.next = backtrace_list;
6282 backtrace_list = &backtrace;
6283 backtrace.function = &Qcall_interactively;
6284 backtrace.args = &cmd;
6285 backtrace.nargs = 1;
6286 backtrace.evalargs = 0;
6287
6288 tem = Fcall_interactively (cmd, record);
6289
6290 backtrace_list = backtrace.next;
6291 return tem;
6292 }
6293 return Qnil;
6294 }
6295 \f
6296 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
6297 1, 1, "P",
6298 "Read function name, then read its arguments and call it.")
6299 (prefixarg)
6300 Lisp_Object prefixarg;
6301 {
6302 Lisp_Object function;
6303 char buf[40];
6304 Lisp_Object saved_keys;
6305 struct gcpro gcpro1;
6306
6307 saved_keys = Fvector (this_command_key_count,
6308 XVECTOR (this_command_keys)->contents);
6309 buf[0] = 0;
6310 GCPRO1 (saved_keys);
6311
6312 if (EQ (prefixarg, Qminus))
6313 strcpy (buf, "- ");
6314 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
6315 strcpy (buf, "C-u ");
6316 else if (CONSP (prefixarg) && INTEGERP (XCONS (prefixarg)->car))
6317 {
6318 if (sizeof (int) == sizeof (EMACS_INT))
6319 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
6320 else if (sizeof (long) == sizeof (EMACS_INT))
6321 sprintf (buf, "%ld ", XINT (XCONS (prefixarg)->car));
6322 else
6323 abort ();
6324 }
6325 else if (INTEGERP (prefixarg))
6326 {
6327 if (sizeof (int) == sizeof (EMACS_INT))
6328 sprintf (buf, "%d ", XINT (prefixarg));
6329 else if (sizeof (long) == sizeof (EMACS_INT))
6330 sprintf (buf, "%ld ", XINT (prefixarg));
6331 else
6332 abort ();
6333 }
6334
6335 /* This isn't strictly correct if execute-extended-command
6336 is bound to anything else. Perhaps it should use
6337 this_command_keys? */
6338 strcat (buf, "M-x ");
6339
6340 /* Prompt with buf, and then read a string, completing from and
6341 restricting to the set of all defined commands. Don't provide
6342 any initial input. Save the command read on the extended-command
6343 history list. */
6344 function = Fcompleting_read (build_string (buf),
6345 Vobarray, Qcommandp,
6346 Qt, Qnil, Qextended_command_history);
6347
6348 /* Set this_command_keys to the concatenation of saved_keys and
6349 function, followed by a RET. */
6350 {
6351 struct Lisp_String *str;
6352 Lisp_Object *keys;
6353 int i;
6354 Lisp_Object tem;
6355
6356 this_command_key_count = 0;
6357
6358 keys = XVECTOR (saved_keys)->contents;
6359 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
6360 add_command_key (keys[i]);
6361
6362 str = XSTRING (function);
6363 for (i = 0; i < str->size; i++)
6364 {
6365 XSETFASTINT (tem, str->data[i]);
6366 add_command_key (tem);
6367 }
6368
6369 XSETFASTINT (tem, '\015');
6370 add_command_key (tem);
6371 }
6372
6373 UNGCPRO;
6374
6375 function = Fintern (function, Qnil);
6376 current_kboard->Vprefix_arg = prefixarg;
6377 this_command = function;
6378
6379 /* If enabled, show which key runs this command. */
6380 if (!NILP (Vsuggest_key_bindings)
6381 && SYMBOLP (function))
6382 {
6383 Lisp_Object *maps, bindings;
6384 int nmaps, i;
6385
6386 bindings = Qnil;
6387 nmaps = current_active_maps (&maps);
6388
6389 for (i = 0; i < nmaps && NILP (bindings); i++)
6390 bindings = Fwhere_is_internal (function, maps[i], Qt, Qnil);
6391
6392 free (maps);
6393
6394 if (!NILP (bindings))
6395 {
6396 message ("You can run the command `%s' by typing %s",
6397 XSYMBOL (function)->name->data,
6398 XSTRING (Fkey_description (bindings))->data);
6399 Fsit_for ((NUMBERP (Vsuggest_key_bindings)
6400 ? Vsuggest_key_bindings : make_number (2)),
6401 Qnil, Qnil);
6402 }
6403 }
6404
6405 return Fcommand_execute (function, Qt);
6406 }
6407
6408 /* Find the set of keymaps now active.
6409 Store into *MAPS_P a vector holding the various maps
6410 and return the number of them. The vector was malloc'd
6411 and the caller should free it. */
6412
6413 int
6414 current_active_maps (maps_p)
6415 Lisp_Object **maps_p;
6416 {
6417 Lisp_Object *tmaps, *maps;
6418 int nmaps;
6419
6420 /* Should overriding-terminal-local-map and overriding-local-map apply? */
6421 if (!NILP (Voverriding_local_map_menu_flag))
6422 {
6423 /* Yes, use them (if non-nil) as well as the global map. */
6424 maps = (Lisp_Object *) xmalloc (3 * sizeof (maps[0]));
6425 nmaps = 0;
6426 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6427 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6428 if (!NILP (Voverriding_local_map))
6429 maps[nmaps++] = Voverriding_local_map;
6430 }
6431 else
6432 {
6433 /* No, so use major and minor mode keymaps. */
6434 nmaps = current_minor_maps (NULL, &tmaps);
6435 maps = (Lisp_Object *) xmalloc ((nmaps + 2) * sizeof (maps[0]));
6436 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
6437 #ifdef USE_TEXT_PROPERTIES
6438 maps[nmaps++] = get_local_map (PT, current_buffer);
6439 #else
6440 maps[nmaps++] = current_buffer->keymap;
6441 #endif
6442 }
6443 maps[nmaps++] = current_global_map;
6444
6445 *maps_p = maps;
6446 return nmaps;
6447 }
6448 \f
6449
6450 detect_input_pending ()
6451 {
6452 if (!input_pending)
6453 get_input_pending (&input_pending);
6454
6455 return input_pending;
6456 }
6457
6458 /* This is called in some cases before a possible quit.
6459 It cases the next call to detect_input_pending to recompute input_pending.
6460 So calling this function unnecessarily can't do any harm. */
6461 clear_input_pending ()
6462 {
6463 input_pending = 0;
6464 }
6465
6466 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
6467 "T if command input is currently available with no waiting.\n\
6468 Actually, the value is nil only if we can be sure that no input is available.")
6469 ()
6470 {
6471 if (!NILP (Vunread_command_events) || unread_command_char != -1)
6472 return (Qt);
6473
6474 return detect_input_pending () ? Qt : Qnil;
6475 }
6476
6477 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
6478 "Return vector of last 100 events, not counting those from keyboard macros.")
6479 ()
6480 {
6481 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
6482 Lisp_Object val;
6483
6484 if (total_keys < NUM_RECENT_KEYS)
6485 return Fvector (total_keys, keys);
6486 else
6487 {
6488 val = Fvector (NUM_RECENT_KEYS, keys);
6489 bcopy (keys + recent_keys_index,
6490 XVECTOR (val)->contents,
6491 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
6492 bcopy (keys,
6493 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
6494 recent_keys_index * sizeof (Lisp_Object));
6495 return val;
6496 }
6497 }
6498
6499 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
6500 "Return the key sequence that invoked this command.\n\
6501 The value is a string or a vector.")
6502 ()
6503 {
6504 return make_event_array (this_command_key_count,
6505 XVECTOR (this_command_keys)->contents);
6506 }
6507
6508 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
6509 Sreset_this_command_lengths, 0, 0, 0,
6510 "Used for complicated reasons in `universal-argument-other-key'.\n\
6511 \n\
6512 `universal-argument-other-key' rereads the event just typed.\n\
6513 It then gets translated through `function-key-map'.\n\
6514 The translated event gets included in the echo area and in\n\
6515 the value of `this-command-keys' in addition to the raw original event.\n\
6516 That is not right.\n\
6517 \n\
6518 Calling this function directs the translated event to replace\n\
6519 the original event, so that only one version of the event actually\n\
6520 appears in the echo area and in the value of `this-command-keys.'.")
6521 ()
6522 {
6523 before_command_restore_flag = 1;
6524 before_command_key_count_1 = before_command_key_count;
6525 before_command_echo_length_1 = before_command_echo_length;
6526 }
6527
6528 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
6529 "Return the current depth in recursive edits.")
6530 ()
6531 {
6532 Lisp_Object temp;
6533 XSETFASTINT (temp, command_loop_level + minibuf_level);
6534 return temp;
6535 }
6536
6537 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
6538 "FOpen dribble file: ",
6539 "Start writing all keyboard characters to a dribble file called FILE.\n\
6540 If FILE is nil, close any open dribble file.")
6541 (file)
6542 Lisp_Object file;
6543 {
6544 if (NILP (file))
6545 {
6546 if (dribble)
6547 {
6548 fclose (dribble);
6549 dribble = 0;
6550 }
6551 }
6552 else
6553 {
6554 file = Fexpand_file_name (file, Qnil);
6555 dribble = fopen (XSTRING (file)->data, "w");
6556 }
6557 return Qnil;
6558 }
6559
6560 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
6561 "Discard the contents of the terminal input buffer.\n\
6562 Also cancel any kbd macro being defined.")
6563 ()
6564 {
6565 current_kboard->defining_kbd_macro = Qnil;
6566 update_mode_lines++;
6567
6568 Vunread_command_events = Qnil;
6569 unread_command_char = -1;
6570
6571 discard_tty_input ();
6572
6573 /* Without the cast, GCC complains that this assignment loses the
6574 volatile qualifier of kbd_store_ptr. Is there anything wrong
6575 with that? */
6576 kbd_fetch_ptr = (struct input_event *) kbd_store_ptr;
6577 Ffillarray (kbd_buffer_frame_or_window, Qnil);
6578 input_pending = 0;
6579
6580 return Qnil;
6581 }
6582 \f
6583 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
6584 "Stop Emacs and return to superior process. You can resume later.\n\
6585 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
6586 control, run a subshell instead.\n\n\
6587 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
6588 to be read as terminal input by Emacs's parent, after suspension.\n\
6589 \n\
6590 Before suspending, run the normal hook `suspend-hook'.\n\
6591 After resumption run the normal hook `suspend-resume-hook'.\n\
6592 \n\
6593 Some operating systems cannot stop the Emacs process and resume it later.\n\
6594 On such systems, Emacs starts a subshell instead of suspending.")
6595 (stuffstring)
6596 Lisp_Object stuffstring;
6597 {
6598 Lisp_Object tem;
6599 int count = specpdl_ptr - specpdl;
6600 int old_height, old_width;
6601 int width, height;
6602 struct gcpro gcpro1, gcpro2;
6603 extern init_sys_modes ();
6604
6605 if (!NILP (stuffstring))
6606 CHECK_STRING (stuffstring, 0);
6607
6608 /* Run the functions in suspend-hook. */
6609 if (!NILP (Vrun_hooks))
6610 call1 (Vrun_hooks, intern ("suspend-hook"));
6611
6612 GCPRO1 (stuffstring);
6613 get_frame_size (&old_width, &old_height);
6614 reset_sys_modes ();
6615 /* sys_suspend can get an error if it tries to fork a subshell
6616 and the system resources aren't available for that. */
6617 record_unwind_protect (init_sys_modes, 0);
6618 stuff_buffered_input (stuffstring);
6619 if (cannot_suspend)
6620 sys_subshell ();
6621 else
6622 sys_suspend ();
6623 unbind_to (count, Qnil);
6624
6625 /* Check if terminal/window size has changed.
6626 Note that this is not useful when we are running directly
6627 with a window system; but suspend should be disabled in that case. */
6628 get_frame_size (&width, &height);
6629 if (width != old_width || height != old_height)
6630 change_frame_size (selected_frame, height, width, 0, 0);
6631
6632 /* Run suspend-resume-hook. */
6633 if (!NILP (Vrun_hooks))
6634 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
6635
6636 UNGCPRO;
6637 return Qnil;
6638 }
6639
6640 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
6641 Then in any case stuff anything Emacs has read ahead and not used. */
6642
6643 stuff_buffered_input (stuffstring)
6644 Lisp_Object stuffstring;
6645 {
6646 /* stuff_char works only in BSD, versions 4.2 and up. */
6647 #ifdef BSD
6648 #ifndef BSD4_1
6649 register unsigned char *p;
6650
6651 if (STRINGP (stuffstring))
6652 {
6653 register int count;
6654
6655 p = XSTRING (stuffstring)->data;
6656 count = XSTRING (stuffstring)->size;
6657 while (count-- > 0)
6658 stuff_char (*p++);
6659 stuff_char ('\n');
6660 }
6661 /* Anything we have read ahead, put back for the shell to read. */
6662 /* ?? What should this do when we have multiple keyboards??
6663 Should we ignore anything that was typed in at the "wrong" kboard? */
6664 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
6665 {
6666 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
6667 kbd_fetch_ptr = kbd_buffer;
6668 if (kbd_fetch_ptr->kind == ascii_keystroke)
6669 stuff_char (kbd_fetch_ptr->code);
6670 kbd_fetch_ptr->kind = no_event;
6671 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
6672 - kbd_buffer]
6673 = Qnil);
6674 }
6675 input_pending = 0;
6676 #endif
6677 #endif /* BSD and not BSD4_1 */
6678 }
6679 \f
6680 set_waiting_for_input (time_to_clear)
6681 EMACS_TIME *time_to_clear;
6682 {
6683 input_available_clear_time = time_to_clear;
6684
6685 /* Tell interrupt_signal to throw back to read_char, */
6686 waiting_for_input = 1;
6687
6688 /* If interrupt_signal was called before and buffered a C-g,
6689 make it run again now, to avoid timing error. */
6690 if (!NILP (Vquit_flag))
6691 quit_throw_to_read_char ();
6692 }
6693
6694 clear_waiting_for_input ()
6695 {
6696 /* Tell interrupt_signal not to throw back to read_char, */
6697 waiting_for_input = 0;
6698 input_available_clear_time = 0;
6699 }
6700
6701 /* This routine is called at interrupt level in response to C-G.
6702 If interrupt_input, this is the handler for SIGINT.
6703 Otherwise, it is called from kbd_buffer_store_event,
6704 in handling SIGIO or SIGTINT.
6705
6706 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
6707 immediately throw back to read_char.
6708
6709 Otherwise it sets the Lisp variable quit-flag not-nil.
6710 This causes eval to throw, when it gets a chance.
6711 If quit-flag is already non-nil, it stops the job right away. */
6712
6713 SIGTYPE
6714 interrupt_signal (signalnum) /* If we don't have an argument, */
6715 int signalnum; /* some compilers complain in signal calls. */
6716 {
6717 char c;
6718 /* Must preserve main program's value of errno. */
6719 int old_errno = errno;
6720
6721 #ifdef USG
6722 if (!read_socket_hook && NILP (Vwindow_system))
6723 {
6724 /* USG systems forget handlers when they are used;
6725 must reestablish each time */
6726 signal (SIGINT, interrupt_signal);
6727 signal (SIGQUIT, interrupt_signal);
6728 }
6729 #endif /* USG */
6730
6731 cancel_echoing ();
6732
6733 if (!NILP (Vquit_flag) && FRAME_TERMCAP_P (selected_frame))
6734 {
6735 fflush (stdout);
6736 reset_sys_modes ();
6737 sigfree ();
6738 #ifdef SIGTSTP /* Support possible in later USG versions */
6739 /*
6740 * On systems which can suspend the current process and return to the original
6741 * shell, this command causes the user to end up back at the shell.
6742 * The "Auto-save" and "Abort" questions are not asked until
6743 * the user elects to return to emacs, at which point he can save the current
6744 * job and either dump core or continue.
6745 */
6746 sys_suspend ();
6747 #else
6748 #ifdef VMS
6749 if (sys_suspend () == -1)
6750 {
6751 printf ("Not running as a subprocess;\n");
6752 printf ("you can continue or abort.\n");
6753 }
6754 #else /* not VMS */
6755 /* Perhaps should really fork an inferior shell?
6756 But that would not provide any way to get back
6757 to the original shell, ever. */
6758 printf ("No support for stopping a process on this operating system;\n");
6759 printf ("you can continue or abort.\n");
6760 #endif /* not VMS */
6761 #endif /* not SIGTSTP */
6762 #ifdef MSDOS
6763 /* We must remain inside the screen area when the internal terminal
6764 is used. Note that [Enter] is not echoed by dos. */
6765 cursor_to (0, 0);
6766 #endif
6767 printf ("Auto-save? (y or n) ");
6768 fflush (stdout);
6769 if (((c = getchar ()) & ~040) == 'Y')
6770 {
6771 Fdo_auto_save (Qt, Qnil);
6772 #ifdef MSDOS
6773 printf ("\r\nAuto-save done");
6774 #else /* not MSDOS */
6775 printf ("Auto-save done\n");
6776 #endif /* not MSDOS */
6777 }
6778 while (c != '\n') c = getchar ();
6779 #ifdef MSDOS
6780 printf ("\r\nAbort? (y or n) ");
6781 #else /* not MSDOS */
6782 #ifdef VMS
6783 printf ("Abort (and enter debugger)? (y or n) ");
6784 #else /* not VMS */
6785 printf ("Abort (and dump core)? (y or n) ");
6786 #endif /* not VMS */
6787 #endif /* not MSDOS */
6788 fflush (stdout);
6789 if (((c = getchar ()) & ~040) == 'Y')
6790 abort ();
6791 while (c != '\n') c = getchar ();
6792 #ifdef MSDOS
6793 printf ("\r\nContinuing...\r\n");
6794 #else /* not MSDOS */
6795 printf ("Continuing...\n");
6796 #endif /* not MSDOS */
6797 fflush (stdout);
6798 init_sys_modes ();
6799 }
6800 else
6801 {
6802 /* If executing a function that wants to be interrupted out of
6803 and the user has not deferred quitting by binding `inhibit-quit'
6804 then quit right away. */
6805 if (immediate_quit && NILP (Vinhibit_quit))
6806 {
6807 immediate_quit = 0;
6808 sigfree ();
6809 Fsignal (Qquit, Qnil);
6810 }
6811 else
6812 /* Else request quit when it's safe */
6813 Vquit_flag = Qt;
6814 }
6815
6816 if (waiting_for_input && !echoing)
6817 quit_throw_to_read_char ();
6818
6819 errno = old_errno;
6820 }
6821
6822 /* Handle a C-g by making read_char return C-g. */
6823
6824 quit_throw_to_read_char ()
6825 {
6826 quit_error_check ();
6827 sigfree ();
6828 /* Prevent another signal from doing this before we finish. */
6829 clear_waiting_for_input ();
6830 input_pending = 0;
6831
6832 Vunread_command_events = Qnil;
6833 unread_command_char = -1;
6834
6835 #if 0 /* Currently, sit_for is called from read_char without turning
6836 off polling. And that can call set_waiting_for_input.
6837 It seems to be harmless. */
6838 #ifdef POLL_FOR_INPUT
6839 /* May be > 1 if in recursive minibuffer. */
6840 if (poll_suppress_count == 0)
6841 abort ();
6842 #endif
6843 #endif
6844 #ifdef MULTI_FRAME
6845 if (FRAMEP (internal_last_event_frame)
6846 && XFRAME (internal_last_event_frame) != selected_frame)
6847 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
6848 Qnil, 0);
6849 #endif
6850
6851 _longjmp (getcjmp, 1);
6852 }
6853 \f
6854 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
6855 "Set mode of reading keyboard input.\n\
6856 First arg INTERRUPT non-nil means use input interrupts;\n\
6857 nil means use CBREAK mode.\n\
6858 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
6859 (no effect except in CBREAK mode).\n\
6860 Third arg META t means accept 8-bit input (for a Meta key).\n\
6861 META nil means ignore the top bit, on the assumption it is parity.\n\
6862 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
6863 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
6864 See also `current-input-mode'.")
6865 (interrupt, flow, meta, quit)
6866 Lisp_Object interrupt, flow, meta, quit;
6867 {
6868 if (!NILP (quit)
6869 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
6870 error ("set-input-mode: QUIT must be an ASCII character");
6871
6872 #ifdef POLL_FOR_INPUT
6873 stop_polling ();
6874 #endif
6875
6876 #ifndef MSDOS
6877 /* this causes startup screen to be restored and messes with the mouse */
6878 reset_sys_modes ();
6879 #endif
6880
6881 #ifdef SIGIO
6882 /* Note SIGIO has been undef'd if FIONREAD is missing. */
6883 if (read_socket_hook)
6884 {
6885 /* When using X, don't give the user a real choice,
6886 because we haven't implemented the mechanisms to support it. */
6887 #ifdef NO_SOCK_SIGIO
6888 interrupt_input = 0;
6889 #else /* not NO_SOCK_SIGIO */
6890 interrupt_input = 1;
6891 #endif /* NO_SOCK_SIGIO */
6892 }
6893 else
6894 interrupt_input = !NILP (interrupt);
6895 #else /* not SIGIO */
6896 interrupt_input = 0;
6897 #endif /* not SIGIO */
6898
6899 /* Our VMS input only works by interrupts, as of now. */
6900 #ifdef VMS
6901 interrupt_input = 1;
6902 #endif
6903
6904 flow_control = !NILP (flow);
6905 if (NILP (meta))
6906 meta_key = 0;
6907 else if (EQ (meta, Qt))
6908 meta_key = 1;
6909 else
6910 meta_key = 2;
6911 if (!NILP (quit))
6912 /* Don't let this value be out of range. */
6913 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
6914
6915 #ifndef MSDOS
6916 init_sys_modes ();
6917 #endif
6918
6919 #ifdef POLL_FOR_INPUT
6920 poll_suppress_count = 1;
6921 start_polling ();
6922 #endif
6923 return Qnil;
6924 }
6925
6926 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
6927 "Return information about the way Emacs currently reads keyboard input.\n\
6928 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
6929 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
6930 nil, Emacs is using CBREAK mode.\n\
6931 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
6932 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
6933 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
6934 META nil means ignoring the top bit, on the assumption it is parity.\n\
6935 META is neither t nor nil if accepting 8-bit input and using\n\
6936 all 8 bits as the character code.\n\
6937 QUIT is the character Emacs currently uses to quit.\n\
6938 The elements of this list correspond to the arguments of\n\
6939 `set-input-mode'.")
6940 ()
6941 {
6942 Lisp_Object val[4];
6943
6944 val[0] = interrupt_input ? Qt : Qnil;
6945 val[1] = flow_control ? Qt : Qnil;
6946 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
6947 XSETFASTINT (val[3], quit_char);
6948
6949 return Flist (sizeof (val) / sizeof (val[0]), val);
6950 }
6951
6952 \f
6953 /*
6954 * Set up a new kboard object with reasonable initial values.
6955 */
6956 void
6957 init_kboard (kb)
6958 KBOARD *kb;
6959 {
6960 kb->Voverriding_terminal_local_map = Qnil;
6961 kb->Vlast_command = Qnil;
6962 kb->Vprefix_arg = Qnil;
6963 kb->kbd_queue = Qnil;
6964 kb->kbd_queue_has_data = 0;
6965 kb->immediate_echo = 0;
6966 kb->echoptr = kb->echobuf;
6967 kb->echo_after_prompt = -1;
6968 kb->kbd_macro_buffer = 0;
6969 kb->kbd_macro_bufsize = 0;
6970 kb->defining_kbd_macro = Qnil;
6971 kb->Vlast_kbd_macro = Qnil;
6972 kb->reference_count = 0;
6973 kb->Vsystem_key_alist = Qnil;
6974 kb->system_key_syms = Qnil;
6975 kb->Vdefault_minibuffer_frame = Qnil;
6976 }
6977
6978 /*
6979 * Destroy the contents of a kboard object, but not the object itself.
6980 * We use this just before deleteing it, or if we're going to initialize
6981 * it a second time.
6982 */
6983 static void
6984 wipe_kboard (kb)
6985 KBOARD *kb;
6986 {
6987 if (kb->kbd_macro_buffer)
6988 xfree (kb->kbd_macro_buffer);
6989 }
6990
6991 #ifdef MULTI_KBOARD
6992 void
6993 delete_kboard (kb)
6994 KBOARD *kb;
6995 {
6996 KBOARD **kbp;
6997 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
6998 if (*kbp == NULL)
6999 abort ();
7000 *kbp = kb->next_kboard;
7001 wipe_kboard (kb);
7002 xfree (kb);
7003 }
7004 #endif
7005
7006 init_keyboard ()
7007 {
7008 /* This is correct before outermost invocation of the editor loop */
7009 command_loop_level = -1;
7010 immediate_quit = 0;
7011 quit_char = Ctl ('g');
7012 Vunread_command_events = Qnil;
7013 unread_command_char = -1;
7014 total_keys = 0;
7015 recent_keys_index = 0;
7016 kbd_fetch_ptr = kbd_buffer;
7017 kbd_store_ptr = kbd_buffer;
7018 kbd_buffer_frame_or_window
7019 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
7020 #ifdef HAVE_MOUSE
7021 do_mouse_tracking = Qnil;
7022 #endif
7023 input_pending = 0;
7024
7025 #ifdef MULTI_FRAME
7026 /* This means that command_loop_1 won't try to select anything the first
7027 time through. */
7028 internal_last_event_frame = Qnil;
7029 Vlast_event_frame = internal_last_event_frame;
7030 #endif
7031
7032 #ifdef MULTI_KBOARD
7033 current_kboard = initial_kboard;
7034 #endif
7035 wipe_kboard (current_kboard);
7036 init_kboard (current_kboard);
7037
7038 if (initialized)
7039 Ffillarray (kbd_buffer_frame_or_window, Qnil);
7040
7041 kbd_buffer_frame_or_window
7042 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
7043 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
7044 {
7045 signal (SIGINT, interrupt_signal);
7046 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
7047 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
7048 SIGQUIT and we can't tell which one it will give us. */
7049 signal (SIGQUIT, interrupt_signal);
7050 #endif /* HAVE_TERMIO */
7051 }
7052 /* Note SIGIO has been undef'd if FIONREAD is missing. */
7053 #ifdef SIGIO
7054 if (!noninteractive)
7055 signal (SIGIO, input_available_signal);
7056 #endif /* SIGIO */
7057
7058 /* Use interrupt input by default, if it works and noninterrupt input
7059 has deficiencies. */
7060
7061 #ifdef INTERRUPT_INPUT
7062 interrupt_input = 1;
7063 #else
7064 interrupt_input = 0;
7065 #endif
7066
7067 /* Our VMS input only works by interrupts, as of now. */
7068 #ifdef VMS
7069 interrupt_input = 1;
7070 #endif
7071
7072 sigfree ();
7073 dribble = 0;
7074
7075 if (keyboard_init_hook)
7076 (*keyboard_init_hook) ();
7077
7078 #ifdef POLL_FOR_INPUT
7079 poll_suppress_count = 1;
7080 start_polling ();
7081 #endif
7082 }
7083
7084 /* This type's only use is in syms_of_keyboard, to initialize the
7085 event header symbols and put properties on them. */
7086 struct event_head {
7087 Lisp_Object *var;
7088 char *name;
7089 Lisp_Object *kind;
7090 };
7091
7092 struct event_head head_table[] = {
7093 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
7094 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
7095 &Qswitch_frame, "switch-frame", &Qswitch_frame,
7096 &Qdelete_frame, "delete-frame", &Qdelete_frame,
7097 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
7098 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
7099 };
7100
7101 syms_of_keyboard ()
7102 {
7103 Qdisabled_command_hook = intern ("disabled-command-hook");
7104 staticpro (&Qdisabled_command_hook);
7105
7106 Qself_insert_command = intern ("self-insert-command");
7107 staticpro (&Qself_insert_command);
7108
7109 Qforward_char = intern ("forward-char");
7110 staticpro (&Qforward_char);
7111
7112 Qbackward_char = intern ("backward-char");
7113 staticpro (&Qbackward_char);
7114
7115 Qdisabled = intern ("disabled");
7116 staticpro (&Qdisabled);
7117
7118 Qundefined = intern ("undefined");
7119 staticpro (&Qundefined);
7120
7121 Qpre_command_hook = intern ("pre-command-hook");
7122 staticpro (&Qpre_command_hook);
7123
7124 Qpost_command_hook = intern ("post-command-hook");
7125 staticpro (&Qpost_command_hook);
7126
7127 Qpost_command_idle_hook = intern ("post-command-idle-hook");
7128 staticpro (&Qpost_command_idle_hook);
7129
7130 Qdeferred_action_function = intern ("deferred-action-function");
7131 staticpro (&Qdeferred_action_function);
7132
7133 Qcommand_hook_internal = intern ("command-hook-internal");
7134 staticpro (&Qcommand_hook_internal);
7135
7136 Qfunction_key = intern ("function-key");
7137 staticpro (&Qfunction_key);
7138 Qmouse_click = intern ("mouse-click");
7139 staticpro (&Qmouse_click);
7140
7141 Qmenu_enable = intern ("menu-enable");
7142 staticpro (&Qmenu_enable);
7143
7144 Qmode_line = intern ("mode-line");
7145 staticpro (&Qmode_line);
7146 Qvertical_line = intern ("vertical-line");
7147 staticpro (&Qvertical_line);
7148 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
7149 staticpro (&Qvertical_scroll_bar);
7150 Qmenu_bar = intern ("menu-bar");
7151 staticpro (&Qmenu_bar);
7152
7153 Qabove_handle = intern ("above-handle");
7154 staticpro (&Qabove_handle);
7155 Qhandle = intern ("handle");
7156 staticpro (&Qhandle);
7157 Qbelow_handle = intern ("below-handle");
7158 staticpro (&Qbelow_handle);
7159
7160 Qevent_kind = intern ("event-kind");
7161 staticpro (&Qevent_kind);
7162 Qevent_symbol_elements = intern ("event-symbol-elements");
7163 staticpro (&Qevent_symbol_elements);
7164 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
7165 staticpro (&Qevent_symbol_element_mask);
7166 Qmodifier_cache = intern ("modifier-cache");
7167 staticpro (&Qmodifier_cache);
7168
7169 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
7170 staticpro (&Qrecompute_lucid_menubar);
7171 Qactivate_menubar_hook = intern ("activate-menubar-hook");
7172 staticpro (&Qactivate_menubar_hook);
7173
7174 Qpolling_period = intern ("polling-period");
7175 staticpro (&Qpolling_period);
7176
7177 {
7178 struct event_head *p;
7179
7180 for (p = head_table;
7181 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
7182 p++)
7183 {
7184 *p->var = intern (p->name);
7185 staticpro (p->var);
7186 Fput (*p->var, Qevent_kind, *p->kind);
7187 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
7188 }
7189 }
7190
7191 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
7192 staticpro (&button_down_location);
7193
7194 {
7195 int i;
7196 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
7197
7198 modifier_symbols = Fmake_vector (make_number (len), Qnil);
7199 for (i = 0; i < len; i++)
7200 if (modifier_names[i])
7201 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
7202 staticpro (&modifier_symbols);
7203 }
7204
7205 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
7206 staticpro (&recent_keys);
7207
7208 this_command_keys = Fmake_vector (make_number (40), Qnil);
7209 staticpro (&this_command_keys);
7210
7211 Qextended_command_history = intern ("extended-command-history");
7212 Fset (Qextended_command_history, Qnil);
7213 staticpro (&Qextended_command_history);
7214
7215 kbd_buffer_frame_or_window
7216 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
7217 staticpro (&kbd_buffer_frame_or_window);
7218
7219 accent_key_syms = Qnil;
7220 staticpro (&accent_key_syms);
7221
7222 func_key_syms = Qnil;
7223 staticpro (&func_key_syms);
7224
7225 mouse_syms = Qnil;
7226 staticpro (&mouse_syms);
7227
7228 unread_switch_frame = Qnil;
7229 staticpro (&unread_switch_frame);
7230
7231 defsubr (&Sevent_convert_list);
7232 defsubr (&Sread_key_sequence);
7233 defsubr (&Srecursive_edit);
7234 #ifdef HAVE_MOUSE
7235 defsubr (&Strack_mouse);
7236 #endif
7237 defsubr (&Sinput_pending_p);
7238 defsubr (&Scommand_execute);
7239 defsubr (&Srecent_keys);
7240 defsubr (&Sthis_command_keys);
7241 defsubr (&Sreset_this_command_lengths);
7242 defsubr (&Ssuspend_emacs);
7243 defsubr (&Sabort_recursive_edit);
7244 defsubr (&Sexit_recursive_edit);
7245 defsubr (&Srecursion_depth);
7246 defsubr (&Stop_level);
7247 defsubr (&Sdiscard_input);
7248 defsubr (&Sopen_dribble_file);
7249 defsubr (&Sset_input_mode);
7250 defsubr (&Scurrent_input_mode);
7251 defsubr (&Sexecute_extended_command);
7252
7253 DEFVAR_LISP ("last-command-char", &last_command_char,
7254 "Last input event that was part of a command.");
7255
7256 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
7257 "Last input event that was part of a command.");
7258
7259 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
7260 "Last input event in a command, except for mouse menu events.\n\
7261 Mouse menus give back keys that don't look like mouse events;\n\
7262 this variable holds the actual mouse event that led to the menu,\n\
7263 so that you can determine whether the command was run by mouse or not.");
7264
7265 DEFVAR_LISP ("last-input-char", &last_input_char,
7266 "Last input event.");
7267
7268 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
7269 "Last input event.");
7270
7271 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
7272 "List of objects to be read as next command input events.");
7273
7274 DEFVAR_INT ("unread-command-char", &unread_command_char,
7275 "If not -1, an object to be read as next command input event.");
7276
7277 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
7278 "Meta-prefix character code. Meta-foo as command input\n\
7279 turns into this character followed by foo.");
7280 XSETINT (meta_prefix_char, 033);
7281
7282 DEFVAR_KBOARD ("last-command", Vlast_command,
7283 "The last command executed. Normally a symbol with a function definition,\n\
7284 but can be whatever was found in the keymap, or whatever the variable\n\
7285 `this-command' was set to by that command.\n\
7286 \n\
7287 The value `mode-exit' is special; it means that the previous command\n\
7288 read an event that told it to exit, and it did so and unread that event.\n\
7289 In other words, the present command is the event that made the previous\n\
7290 command exit.\n\
7291 \n\
7292 The value `kill-region' is special; it means that the previous command\n\
7293 was a kill command.");
7294
7295 DEFVAR_LISP ("this-command", &this_command,
7296 "The command now being executed.\n\
7297 The command can set this variable; whatever is put here\n\
7298 will be in `last-command' during the following command.");
7299 this_command = Qnil;
7300
7301 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
7302 "*Number of keyboard input characters between auto-saves.\n\
7303 Zero means disable autosaving due to number of characters typed.");
7304 auto_save_interval = 300;
7305
7306 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
7307 "*Number of seconds idle time before auto-save.\n\
7308 Zero or nil means disable auto-saving due to idleness.\n\
7309 After auto-saving due to this many seconds of idle time,\n\
7310 Emacs also does a garbage collection if that seems to be warranted.");
7311 XSETFASTINT (Vauto_save_timeout, 30);
7312
7313 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
7314 "*Nonzero means echo unfinished commands after this many seconds of pause.");
7315 echo_keystrokes = 1;
7316
7317 DEFVAR_INT ("polling-period", &polling_period,
7318 "*Interval between polling for input during Lisp execution.\n\
7319 The reason for polling is to make C-g work to stop a running program.\n\
7320 Polling is needed only when using X windows and SIGIO does not work.\n\
7321 Polling is automatically disabled in all other cases.");
7322 polling_period = 2;
7323
7324 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
7325 "*Maximum time between mouse clicks to make a double-click.\n\
7326 Measured in milliseconds. nil means disable double-click recognition;\n\
7327 t means double-clicks have no time limit and are detected\n\
7328 by position only.");
7329 Vdouble_click_time = make_number (500);
7330
7331 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
7332 "*Non-nil means inhibit local map menu bar menus.");
7333 inhibit_local_menu_bar_menus = 0;
7334
7335 DEFVAR_INT ("num-input-keys", &num_input_keys,
7336 "Number of complete keys read from the keyboard so far.");
7337 num_input_keys = 0;
7338
7339 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
7340 "The frame in which the most recently read event occurred.\n\
7341 If the last event came from a keyboard macro, this is set to `macro'.");
7342 Vlast_event_frame = Qnil;
7343
7344 DEFVAR_LISP ("help-char", &Vhelp_char,
7345 "Character to recognize as meaning Help.\n\
7346 When it is read, do `(eval help-form)', and display result if it's a string.\n\
7347 If the value of `help-form' is nil, this char can be read normally.");
7348 XSETINT (Vhelp_char, Ctl ('H'));
7349
7350 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
7351 "List of input events to recognize as meaning Help.\n\
7352 These work just like the value of `help-char' (see that).");
7353 Vhelp_event_list = Qnil;
7354
7355 DEFVAR_LISP ("help-form", &Vhelp_form,
7356 "Form to execute when character `help-char' is read.\n\
7357 If the form returns a string, that string is displayed.\n\
7358 If `help-form' is nil, the help char is not recognized.");
7359 Vhelp_form = Qnil;
7360
7361 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
7362 "Command to run when `help-char' character follows a prefix key.\n\
7363 This command is used only when there is no actual binding\n\
7364 for that character after that prefix key.");
7365 Vprefix_help_command = Qnil;
7366
7367 DEFVAR_LISP ("top-level", &Vtop_level,
7368 "Form to evaluate when Emacs starts up.\n\
7369 Useful to set before you dump a modified Emacs.");
7370 Vtop_level = Qnil;
7371
7372 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
7373 "String used as translate table for keyboard input, or nil.\n\
7374 Each character is looked up in this string and the contents used instead.\n\
7375 If string is of length N, character codes N and up are untranslated.");
7376 Vkeyboard_translate_table = Qnil;
7377
7378 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
7379 "Non-nil means to always spawn a subshell instead of suspending,\n\
7380 even if the operating system has support for stopping a process.");
7381 cannot_suspend = 0;
7382
7383 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
7384 "Non-nil means prompt with menus when appropriate.\n\
7385 This is done when reading from a keymap that has a prompt string,\n\
7386 for elements that have prompt strings.\n\
7387 The menu is displayed on the screen\n\
7388 if X menus were enabled at configuration\n\
7389 time and the previous event was a mouse click prefix key.\n\
7390 Otherwise, menu prompting uses the echo area.");
7391 menu_prompting = 1;
7392
7393 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
7394 "Character to see next line of menu prompt.\n\
7395 Type this character while in a menu prompt to rotate around the lines of it.");
7396 XSETINT (menu_prompt_more_char, ' ');
7397
7398 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
7399 "A mask of additional modifier keys to use with every keyboard character.\n\
7400 Emacs applies the modifiers of the character stored here to each keyboard\n\
7401 character it reads. For example, after evaluating the expression\n\
7402 (setq extra-keyboard-modifiers ?\\C-x)\n\
7403 all input characters will have the control modifier applied to them.\n\
7404 \n\
7405 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
7406 not count as a control character; rather, it counts as a character\n\
7407 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
7408 cancels any modification.");
7409 extra_keyboard_modifiers = 0;
7410
7411 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
7412 "If an editing command sets this to t, deactivate the mark afterward.\n\
7413 The command loop sets this to nil before each command,\n\
7414 and tests the value when the command returns.\n\
7415 Buffer modification stores t in this variable.");
7416 Vdeactivate_mark = Qnil;
7417
7418 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
7419 "Temporary storage of pre-command-hook or post-command-hook.");
7420 Vcommand_hook_internal = Qnil;
7421
7422 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
7423 "Normal hook run before each command is executed.\n\
7424 Errors running the hook are caught and ignored.");
7425 Vpre_command_hook = Qnil;
7426
7427 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
7428 "Normal hook run after each command is executed.\n\
7429 Errors running the hook are caught and ignored.");
7430 Vpost_command_hook = Qnil;
7431
7432 DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
7433 "Normal hook run after each command is executed, if idle.\n\
7434 Errors running the hook are caught and ignored.");
7435 Vpost_command_idle_hook = Qnil;
7436
7437 DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
7438 "Delay time before running `post-command-idle-delay'.\n\
7439 This is measured in microseconds.");
7440 post_command_idle_delay = 100000;
7441
7442 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
7443 "t means menu bar, specified Lucid style, needs to be recomputed.");
7444 Vlucid_menu_bar_dirty_flag = Qnil;
7445
7446 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
7447 "List of menu bar items to move to the end of the menu bar.\n\
7448 The elements of the list are event types that may have menu bar bindings.");
7449 Vmenu_bar_final_items = Qnil;
7450
7451 DEFVAR_KBOARD ("overriding-terminal-local-map",
7452 Voverriding_terminal_local_map,
7453 "Keymap that overrides all other local keymaps.\n\
7454 If this variable is non-nil, it is used as a keymap instead of the\n\
7455 buffer's local map, and the minor mode keymaps and text property keymaps.");
7456
7457 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
7458 "Keymap that overrides all other local keymaps.\n\
7459 If this variable is non-nil, it is used as a keymap instead of the\n\
7460 buffer's local map, and the minor mode keymaps and text property keymaps.");
7461 Voverriding_local_map = Qnil;
7462
7463 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
7464 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
7465 Otherwise, the menu bar continues to reflect the buffer's local map\n\
7466 and the minor mode maps regardless of `overriding-local-map'.");
7467 Voverriding_local_map_menu_flag = Qnil;
7468
7469 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
7470 "Keymap defining bindings for special events to execute at low level.");
7471 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
7472
7473 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
7474 "*Non-nil means generate motion events for mouse motion.");
7475
7476 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
7477 "Alist of system-specific X windows key symbols.\n\
7478 Each element should have the form (N . SYMBOL) where N is the\n\
7479 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
7480 and SYMBOL is its name.");
7481
7482 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
7483 "List of deferred actions to be performed at a later time.\n\
7484 The precise format isn't relevant here; we just check whether it is nil.");
7485 Vdeferred_action_list = Qnil;
7486
7487 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
7488 "Function to call to handle deferred actions, after each command.\n\
7489 This function is called with no arguments after each command\n\
7490 whenever `deferred-action-list' is non-nil.");
7491 Vdeferred_action_function = Qnil;
7492
7493 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
7494 "Non-nil means show the equivalent key-binding when M-x command has one.\n\
7495 The value can be a length of time to show the message for.\n\
7496 If the value is non-nil and not a number, we wait 2 seconds.");
7497 Vsuggest_key_bindings = Qt;
7498 }
7499
7500 keys_of_keyboard ()
7501 {
7502 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
7503 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
7504 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
7505 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
7506 initial_define_key (meta_map, 'x', "execute-extended-command");
7507
7508 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
7509 "handle-delete-frame");
7510 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
7511 "ignore-event");
7512 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
7513 "ignore-event");
7514 }