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