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