*** empty log message ***
[bpt/emacs.git] / src / keyboard.c
CommitLineData
284f4730 1/* Keyboard and mouse input; editor command loop.
519a3b65 2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
284f4730
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the 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 "screen.h"
32#include "window.h"
33#include "commands.h"
34#include "buffer.h"
35#include "disptab.h"
36#include "keyboard.h"
37#include <setjmp.h>
38#include <errno.h>
39
284f4730
JB
40#ifndef VMS
41#include <sys/ioctl.h>
284f4730
JB
42#endif
43
52baf19e
JB
44#include "syssignal.h"
45#include "systerm.h"
ffd56f97 46#include "systime.h"
52baf19e
JB
47
48extern int errno;
49
284f4730
JB
50#ifdef HAVE_X_WINDOWS
51extern Lisp_Object Vmouse_grabbed;
52
53/* Make all keyboard buffers much bigger when using X windows. */
54#define KBD_BUFFER_SIZE 4096
55#else /* No X-windows, character input */
56#define KBD_BUFFER_SIZE 256
57#endif /* No X-windows */
58
59/* Following definition copied from eval.c */
60
61struct backtrace
62 {
63 struct backtrace *next;
64 Lisp_Object *function;
65 Lisp_Object *args; /* Points to vector of args. */
66 int nargs; /* length of vector. If nargs is UNEVALLED,
67 args points to slot holding list of
68 unevalled args */
69 char evalargs;
70 };
71
72/* Non-nil disable property on a command means
73 do not execute it; call disabled-command-hook's value instead. */
74Lisp_Object Qdisabled, Vdisabled_command_hook;
75
76#define NUM_RECENT_KEYS (100)
77int recent_keys_index; /* Index for storing next element into recent_keys */
78int total_keys; /* Total number of elements stored into recent_keys */
79Lisp_Object recent_keys[NUM_RECENT_KEYS]; /* Holds last 100 keystrokes */
80
81/* Buffer holding the key that invoked the current command. */
82Lisp_Object *this_command_keys;
83int this_command_key_count; /* Size in use. */
84int this_command_keys_size; /* Size allocated. */
85
86extern int minbuf_level;
87
88extern struct backtrace *backtrace_list;
89
90/* Nonzero means do menu prompting. */
91static int menu_prompting;
92
93/* Character to see next line of menu prompt. */
94static Lisp_Object menu_prompt_more_char;
95
96/* For longjmp to where kbd input is being done. */
97static jmp_buf getcjmp;
98
99/* True while doing kbd input. */
100int waiting_for_input;
101
102/* True while displaying for echoing. Delays C-g throwing. */
103static int echoing;
104
105/* Nonzero means C-G should cause immediate error-signal. */
106int immediate_quit;
107
108/* Character to recognize as the help char. */
109Lisp_Object help_char;
110
111/* Form to execute when help char is typed. */
112Lisp_Object Vhelp_form;
113
114/* Character that causes a quit. Normally C-g.
115
116 If we are running on an ordinary terminal, this must be an ordinary
117 ASCII char, since we want to make it our interrupt character.
118
119 If we are not running on an ordinary terminal, it still needs to be
120 an ordinary ASCII char. This character needs to be recognized in
121 the input interrupt handler. At this point, the keystroke is
122 represented as a struct input_event, while the desired quit
123 character is specified as a lispy event. The mapping from struct
124 input_events to lispy events cannot run in an interrupt handler,
125 and the reverse mapping is difficult for anything but ASCII
126 keystrokes.
127
128 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
129 ASCII character. */
130int quit_char;
131
132extern Lisp_Object current_global_map;
133extern int minibuf_level;
134
135/* Current depth in recursive edits. */
136int command_loop_level;
137
138/* Total number of times command_loop has read a key sequence. */
139int num_input_keys;
140
141/* Last input character read as a command. */
142Lisp_Object last_command_char;
143
144/* Last input character read for any purpose. */
145Lisp_Object last_input_char;
146
147/* If not Qnil, an object to be read as the next command input. */
148Lisp_Object unread_command_char;
149
150/* Char to use as prefix when a meta character is typed in.
151 This is bound on entry to minibuffer in case ESC is changed there. */
152
153Lisp_Object meta_prefix_char;
154
155/* Last size recorded for a current buffer which is not a minibuffer. */
156static int last_non_minibuf_size;
157
158/* Number of idle seconds before an auto-save. */
159static Lisp_Object Vauto_save_timeout;
160
161/* Total number of times read_char has returned. */
162int num_input_chars;
163
164/* Auto-save automatically when this many characters have been typed
165 since the last time. */
166
167static int auto_save_interval;
168
169/* Value of num_input_chars as of last auto save. */
170
171int last_auto_save;
172
173/* Last command executed by the editor command loop, not counting
174 commands that set the prefix argument. */
175
176Lisp_Object last_command;
177
178/* The command being executed by the command loop.
179 Commands may set this, and the value set will be copied into last_command
180 instead of the actual command. */
181Lisp_Object this_command;
182
183#ifndef HAVE_X11
184/* Window of last mouse click. */
185extern Lisp_Object Vmouse_window;
186
187/* List containing details of last mouse click. */
188extern Lisp_Object Vmouse_event;
189#endif /* defined HAVE_X11 */
190
191/* Hook to call on each mouse event after running its definition. */
192Lisp_Object Vmouse_event_function;
193
194/* Hook to call when mouse leaves screen. */
195Lisp_Object Vmouse_left_hook;
196
197/* Hook to call when a screen is mapped. */
198Lisp_Object Vmap_screen_hook;
199
200/* Hook to call when a screen is unmapped. */
201Lisp_Object Vunmap_screen_hook;
202
203/* Handler for non-grabbed (no keys depressed) mouse motion. */
204Lisp_Object Vmouse_motion_handler;
205
206/* The screen in which the last input event occurred.
207 command_loop_1 will select this screen before running the
208 command bound to an event sequence, and read_key_sequence will
209 toss the existing prefix if the user starts typing at a
210 new screen. */
211Lisp_Object Vlast_event_screen;
212
213/* X Windows wants this for selection ownership. */
214unsigned long last_event_timestamp;
215
216Lisp_Object Qself_insert_command;
217Lisp_Object Qforward_char;
218Lisp_Object Qbackward_char;
219
220/* read_key_sequence stores here the command definition of the
221 key sequence that it reads. */
222Lisp_Object read_key_sequence_cmd;
223
224/* Form to evaluate (if non-nil) when Emacs is started. */
225Lisp_Object Vtop_level;
226
227/* User-supplied string to translate input characters through. */
228Lisp_Object Vkeyboard_translate_table;
229
230/* Keymap mapping ASCII function key sequences onto their preferred forms. */
231extern Lisp_Object Vfunction_key_map;
232
233/* File in which we write all commands we read. */
234FILE *dribble;
235
236/* Nonzero if input is available. */
237int input_pending;
238
239/* Nonzero if should obey 0200 bit in input chars as "Meta". */
240int meta_key;
241
242extern char *pending_malloc_warning;
243
244/* Circular buffer for pre-read keyboard input. */
245static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
246
247/* Pointer to next available character in kbd_buffer.
248 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
249 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
250 next available char is in kbd_buffer[0]. */
251static struct input_event *kbd_fetch_ptr;
252
253/* Pointer to next place to store character in kbd_buffer. This
254 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
255 character should go in kbd_buffer[0]. */
256static struct input_event *kbd_store_ptr;
257
258/* The above pair of variables forms a "queue empty" flag. When we
259 enqueue a non-hook event, we increment kbd_write_count. When we
260 dequeue a non-hook event, we increment kbd_read_count. We say that
261 there is input available iff the two counters are equal.
262
263 Why not just have a flag set and cleared by the enqueuing and
264 dequeuing functions? Such a flag could be screwed up by interrupts
265 at inopportune times. */
266
267/* If this flag is non-zero, mouse movement events will appear in the
268 input stream. If is zero, mouse movement will be ignored. */
269int do_mouse_tracking;
270
271/* The window system handling code should set this if the mouse has
272 moved since the last call to the mouse_position_hook. Calling that
273 hook should clear this. Code assumes that if this is set, it can
274 call mouse_position_hook to get the promised position, so don't set
275 it unless you're prepared to substantiate the claim! */
276int mouse_moved;
277
278/* True iff there is an event in kbd_buffer, or if mouse tracking is
279 enabled and there is a new mouse position in the mouse movement
280 buffer. Note that if this is false, that doesn't mean that there
281 is readable input; all the events in the queue might be button-up
282 events, and do_mouse_tracking might be off. */
283#define EVENT_QUEUES_EMPTY \
284 ((kbd_fetch_ptr == kbd_store_ptr) && (!do_mouse_tracking || !mouse_moved))
285
286
287/* Symbols to head events. */
288Lisp_Object Qmouse_movement;
289
290Lisp_Object Qvscrollbar_part;
291Lisp_Object Qvslider_part;
292Lisp_Object Qvthumbup_part;
293Lisp_Object Qvthumbdown_part;
294
295Lisp_Object Qhscrollbar_part;
296Lisp_Object Qhslider_part;
297Lisp_Object Qhthumbleft_part;
298Lisp_Object Qhthumbright_part;
299
300/* Symbols to denote kinds of events. */
301Lisp_Object Qfunction_key;
302Lisp_Object Qmouse_click;
303/* Lisp_Object Qmouse_movement; - also an event header */
304Lisp_Object Qscrollbar_click;
305
306/* Properties of event headers. */
307Lisp_Object Qevent_kind;
308Lisp_Object Qevent_unmodified;
309
310/* Symbols to use for non-text mouse positions. */
311Lisp_Object Qmode_line;
312Lisp_Object Qvertical_split;
313
314
ffd56f97
JB
315/* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
316 happens. */
317EMACS_TIME *input_available_clear_time;
284f4730
JB
318
319/* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
320 Default is 1 if INTERRUPT_INPUT is defined. */
321int interrupt_input;
322
323/* Nonzero while interrupts are temporarily deferred during redisplay. */
324int interrupts_deferred;
325
326/* nonzero means use ^S/^Q for flow control. */
327int flow_control;
328
284f4730
JB
329/* Allow m- file to inhibit use of FIONREAD. */
330#ifdef BROKEN_FIONREAD
331#undef FIONREAD
332#endif
333
334/* We are unable to use interrupts if FIONREAD is not available,
335 so flush SIGIO so we won't try. */
336#ifndef FIONREAD
337#ifdef SIGIO
338#undef SIGIO
339#endif
340#endif
341
342/* If we support X Windows, and won't get an interrupt when input
343 arrives from the server, poll periodically so we can detect C-g. */
344#ifdef HAVE_X_WINDOWS
345#ifndef SIGIO
346#define POLL_FOR_INPUT
347#endif
348#endif
349\f
350/* Global variable declarations. */
351
352/* Function for init_keyboard to call with no args (if nonzero). */
353void (*keyboard_init_hook) ();
354
355static int read_avail_input ();
356static void get_input_pending ();
357
358/* > 0 if we are to echo keystrokes. */
359static int echo_keystrokes;
360
361/* Nonzero means echo each character as typed. */
362static int immediate_echo;
363
364/* The text we're echoing in the modeline - partial key sequences,
365 usually. '\0'-terminated. */
366static char echobuf[100];
367
368/* Where to append more text to echobuf if we want to. */
369static char *echoptr;
370
371#define min(a,b) ((a)<(b)?(a):(b))
372#define max(a,b) ((a)>(b)?(a):(b))
373
374/* Install the string STR as the beginning of the string of echoing,
375 so that it serves as a prompt for the next character.
376 Also start echoing. */
377
378echo_prompt (str)
379 char *str;
380{
381 int len = strlen (str);
382 if (len > sizeof echobuf - 4)
383 len = sizeof echobuf - 4;
384 bcopy (str, echobuf, len + 1);
385 echoptr = echobuf + len;
386
387 echo ();
388}
389
390/* Add C to the echo string, if echoing is going on.
391 C can be a character, which is printed prettily ("M-C-x" and all that
392 jazz), or a symbol, whose name is printed. */
393
394echo_char (c)
395 Lisp_Object c;
396{
397 extern char *push_key_description ();
398
399 if (immediate_echo)
400 {
401 char *ptr = echoptr;
402
403 if (ptr != echobuf)
404 *ptr++ = ' ';
405
406 /* If someone has passed us a composite event, use its head symbol. */
407 if (EVENT_HAS_PARAMETERS (c))
408 c = EVENT_HEAD (c);
409
410 if (XTYPE (c) == Lisp_Int)
411 {
412 if (ptr - echobuf > sizeof echobuf - 6)
413 return;
414
415 ptr = push_key_description (c, ptr);
416 }
417 else if (XTYPE (c) == Lisp_Symbol)
418 {
419 struct Lisp_String *name = XSYMBOL (c)->name;
420 if (((ptr - echobuf) + name->size + 4) > sizeof echobuf)
421 return;
422 bcopy (name->data, ptr, name->size);
423 ptr += name->size;
424 }
425
426 if (echoptr == echobuf && c == help_char)
427 {
428 strcpy (ptr, " (Type ? for further options)");
429 ptr += strlen (ptr);
430 }
431
432 *ptr = 0;
433 echoptr = ptr;
434
435 echo ();
436 }
437}
438
439/* Temporarily add a dash to the end of the echo string if it's not
440 empty, so that it serves as a mini-prompt for the very next character. */
441
442echo_dash ()
443{
444 if (!immediate_echo && echoptr == echobuf)
445 return;
446
447 /* Put a dash at the end of the buffer temporarily,
448 but make it go away when the next character is added. */
449 echoptr[0] = '-';
450 echoptr[1] = 0;
451
452 echo ();
453}
454
455/* Display the current echo string, and begin echoing if not already
456 doing so. */
457
458echo ()
459{
460 if (!immediate_echo)
461 {
462 int i;
463 immediate_echo = 1;
464
465 for (i = 0; i < this_command_key_count; i++)
466 echo_char (this_command_keys[i]);
467 echo_dash ();
468 }
469
470 echoing = 1;
471 message1 (echobuf);
472 echoing = 0;
473
474 if (waiting_for_input && !NILP (Vquit_flag))
475 quit_throw_to_read_char ();
476}
477
478/* Turn off echoing, for the start of a new command. */
479
480cancel_echoing ()
481{
482 immediate_echo = 0;
483 echoptr = echobuf;
484}
485
486/* Return the length of the current echo string. */
487
488static int
489echo_length ()
490{
491 return echoptr - echobuf;
492}
493
494/* Truncate the current echo message to its first LEN chars.
495 This and echo_char get used by read_key_sequence when the user
496 switches screens while entering a key sequence. */
497
498static void
499echo_truncate (len)
500 int len;
501{
502 echobuf[len] = '\0';
503 echoptr = echobuf + strlen (echobuf);
504}
505
506\f
507/* Functions for manipulating this_command_keys. */
508static void
509add_command_key (key)
510 Lisp_Object key;
511{
512 if (this_command_key_count == this_command_keys_size)
513 {
514 this_command_keys_size *= 2;
515 this_command_keys
516 = (Lisp_Object *) xrealloc (this_command_keys,
517 (this_command_keys_size
518 * sizeof (Lisp_Object)));
519 }
520 this_command_keys[this_command_key_count++] = key;
521}
522\f
523Lisp_Object
524recursive_edit_1 ()
525{
526 int count = specpdl_ptr - specpdl;
527 Lisp_Object val;
528
529 if (command_loop_level > 0)
530 {
531 specbind (Qstandard_output, Qt);
532 specbind (Qstandard_input, Qt);
533 }
534
535 val = command_loop ();
536 if (EQ (val, Qt))
537 Fsignal (Qquit, Qnil);
538
539 unbind_to (count);
540 return Qnil;
541}
542
543/* When an auto-save happens, record the "time", and don't do again soon. */
544record_auto_save ()
545{
546 last_auto_save = num_input_chars;
547}
548\f
549Lisp_Object recursive_edit_unwind (), command_loop ();
550
551DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
552 "Invoke the editor command loop recursively.\n\
553To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
554that tells this function to return.\n\
555Alternately, `(throw 'exit t)' makes this function signal an error.\n\
556This function is called by the editor initialization to begin editing.")
557 ()
558{
559 int count = specpdl_ptr - specpdl;
560 Lisp_Object val;
561
562 command_loop_level++;
563 update_mode_lines = 1;
564
565 record_unwind_protect (recursive_edit_unwind,
566 (command_loop_level
567 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
568 ? Fcurrent_buffer ()
569 : Qnil);
570 recursive_edit_1 ();
571 return unbind_to (count, Qnil);
572}
573
574Lisp_Object
575recursive_edit_unwind (buffer)
576 Lisp_Object buffer;
577{
578 if (!NILP (buffer))
579 Fset_buffer (buffer);
580
581 command_loop_level--;
582 update_mode_lines = 1;
583 return Qnil;
584}
585\f
586Lisp_Object
587cmd_error (data)
588 Lisp_Object data;
589{
590 Lisp_Object errmsg, tail, errname, file_error;
591 Lisp_Object stream;
592 struct gcpro gcpro1;
593 int i;
594
595 Vquit_flag = Qnil;
596 Vinhibit_quit = Qt;
597 Vstandard_output = Qt;
598 Vstandard_input = Qt;
599 Vexecuting_macro = Qnil;
600 echo_area_glyphs = 0;
601
602 /* If the window system or terminal screen hasn't been initialized
603 yet, or we're not interactive, it's best to dump this message out
604 to stderr and exit. */
605 if (! SCREEN_MESSAGE_BUF (selected_screen)
606 || noninteractive)
607 stream = Qexternal_debugging_output;
608 else
609 {
610 Fdiscard_input ();
611 bitch_at_user ();
612 stream = Qt;
613 }
614
615 errname = Fcar (data);
616
617 if (EQ (errname, Qerror))
618 {
619 data = Fcdr (data);
620 if (!CONSP (data)) data = Qnil;
621 errmsg = Fcar (data);
622 file_error = Qnil;
623 }
624 else
625 {
626 errmsg = Fget (errname, Qerror_message);
627 file_error = Fmemq (Qfile_error,
628 Fget (errname, Qerror_conditions));
629 }
630
631 /* Print an error message including the data items.
632 This is done by printing it into a scratch buffer
633 and then making a copy of the text in the buffer. */
634
635 if (!CONSP (data)) data = Qnil;
636 tail = Fcdr (data);
637 GCPRO1 (tail);
638
639 /* For file-error, make error message by concatenating
640 all the data items. They are all strings. */
641 if (!NILP (file_error) && !NILP (tail))
642 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
643
644 if (XTYPE (errmsg) == Lisp_String)
645 Fprinc (errmsg, stream);
646 else
647 write_string_1 ("peculiar error", -1, stream);
648
649 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
650 {
651 write_string_1 (i ? ", " : ": ", 2, stream);
652 if (!NILP (file_error))
653 Fprinc (Fcar (tail), stream);
654 else
655 Fprin1 (Fcar (tail), stream);
656 }
657 UNGCPRO;
658
659 /* If the window system or terminal screen hasn't been initialized
660 yet, or we're in -batch mode, this error should cause Emacs to exit. */
661 if (! SCREEN_MESSAGE_BUF (selected_screen)
662 || noninteractive)
663 {
664 Fterpri (stream);
665 Fkill_emacs (make_number (-1));
666 }
667
668 Vquit_flag = Qnil;
669
670 Vinhibit_quit = Qnil;
671 return make_number (0);
672}
673\f
674Lisp_Object command_loop_1 ();
675Lisp_Object command_loop_2 ();
676Lisp_Object top_level_1 ();
677
678/* Entry to editor-command-loop.
679 This level has the catches for exiting/returning to editor command loop.
680 It returns nil to exit recursive edit, t to abort it. */
681
682Lisp_Object
683command_loop ()
684{
685 if (command_loop_level > 0 || minibuf_level > 0)
686 {
687 return internal_catch (Qexit, command_loop_2, Qnil);
688 }
689 else
690 while (1)
691 {
692 internal_catch (Qtop_level, top_level_1, Qnil);
693 internal_catch (Qtop_level, command_loop_2, Qnil);
694
695 /* End of file in -batch run causes exit here. */
696 if (noninteractive)
697 Fkill_emacs (Qt);
698 }
699}
700
701/* Here we catch errors in execution of commands within the
702 editing loop, and reenter the editing loop.
703 When there is an error, cmd_error runs and returns a non-nil
704 value to us. A value of nil means that cmd_loop_1 itself
705 returned due to end of file (or end of kbd macro). */
706
707Lisp_Object
708command_loop_2 ()
709{
710 register Lisp_Object val;
711
712 do
713 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
714 while (!NILP (val));
715
716 return Qnil;
717}
718
719Lisp_Object
720top_level_2 ()
721{
722 return Feval (Vtop_level);
723}
724
725Lisp_Object
726top_level_1 ()
727{
728 /* On entry to the outer level, run the startup file */
729 if (!NILP (Vtop_level))
730 internal_condition_case (top_level_2, Qerror, cmd_error);
731 else if (!NILP (Vpurify_flag))
732 message ("Bare impure Emacs (standard Lisp code not loaded)");
733 else
734 message ("Bare Emacs (standard Lisp code not loaded)");
735 return Qnil;
736}
737
738DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
739 "Exit all recursive editing levels.")
740 ()
741{
742 Fthrow (Qtop_level, Qnil);
743}
744
745DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
746 "Exit from the innermost recursive edit or minibuffer.")
747 ()
748{
749 if (command_loop_level > 0 || minibuf_level > 0)
750 Fthrow (Qexit, Qnil);
751
752 error ("No recursive edit is in progress");
753}
754
755DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
756 "Abort the command that requested this recursive edit or minibuffer input.")
757 ()
758{
759 if (command_loop_level > 0 || minibuf_level > 0)
760 Fthrow (Qexit, Qt);
761
762 error ("No recursive edit is in progress");
763}
764\f
765/* This is the actual command reading loop,
766 sans error-handling encapsulation. */
767
768Lisp_Object Fcommand_execute ();
769static int read_key_sequence ();
770
771Lisp_Object
772command_loop_1 ()
773{
774 Lisp_Object cmd;
775 int lose;
776 int nonundocount;
777 Lisp_Object keybuf[30];
778 int i;
779 int no_redisplay;
780 int no_direct;
781
782 Vprefix_arg = Qnil;
783 waiting_for_input = 0;
784 cancel_echoing ();
785
786 /* Don't clear out last_command at the beginning of a macro. */
787 if (XTYPE (Vexecuting_macro) != Lisp_String)
788 last_command = Qt;
789
790 nonundocount = 0;
791 no_redisplay = 0;
792 this_command_key_count = 0;
793
794 while (1)
795 {
796 /* Install chars successfully executed in kbd macro. */
797
798 if (defining_kbd_macro && NILP (Vprefix_arg))
799 finalize_kbd_macro_chars ();
800
801 /* Make sure the current window's buffer is selected. */
802 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
803 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
804
805 /* Display any malloc warning that just came out. Use while because
806 displaying one warning can cause another. */
807
808 while (pending_malloc_warning)
809 display_malloc_warning ();
810
811 no_direct = 0;
812
813 /* If minibuffer on and echo area in use,
814 wait 2 sec and redraw minibufer. */
815
816 if (minibuf_level && echo_area_glyphs)
817 {
818 Fsit_for (make_number (2), Qnil, Qnil);
819 echo_area_glyphs = 0;
820 no_direct = 1;
821 if (!NILP (Vquit_flag))
822 {
823 Vquit_flag = Qnil;
824 unread_command_char = make_number (quit_char);
825 }
826 }
827
828#ifdef C_ALLOCA
829 alloca (0); /* Cause a garbage collection now */
830 /* Since we can free the most stuff here. */
831#endif /* C_ALLOCA */
832
833 /* Read next key sequence; i gets its length. */
834 i = read_key_sequence (keybuf, (sizeof keybuf / sizeof (keybuf[0])), 0);
835
836 ++num_input_keys;
837
838#ifdef MULTI_SCREEN
839 /* Select the screen that the key sequence came from. */
840 if (XTYPE (Vlast_event_screen) == Lisp_Screen
841 && XSCREEN (Vlast_event_screen) != selected_screen)
842 Fselect_screen (Vlast_event_screen, Qnil);
843#endif
844
845 /* Now we have read a key sequence of length I,
846 or else I is 0 and we found end of file. */
847
848 if (i == 0) /* End of file -- happens only in */
849 return Qnil; /* a kbd macro, at the end. */
850
851#if 0
852#ifdef HAVE_X_WINDOWS
853 if (SCREEN_IS_X (selected_screen))
854 {
855 if (i == -1) /* Mouse event */
856 {
857 nonundocount = 0;
858 if (NILP (Vprefix_arg) && NILP (Vexecuting_macro) &&
859 !EQ (minibuf_window, selected_window))
860 Fundo_boundary ();
861
862 if (defining_kbd_macro)
863 {
864 /* Be nice if this worked... */
865 }
866 Fexecute_mouse_event (read_key_sequence_cmd);
867 no_redisplay = 0;
868 goto directly_done;
869 }
870
871 if (i == -2) /* Lisp Symbol */
872 {
873 nonundocount = 0;
874 if (NILP (Vprefix_arg) && NILP (Vexecuting_macro) &&
875 !EQ (minibuf_window, selected_window))
876 Fundo_boundary ();
877
878 goto directly_done;
879 }
880 }
881#endif /* HAVE_X_WINDOWS */
882#endif
883
884 last_command_char = keybuf[i - 1];
885
886 cmd = read_key_sequence_cmd;
887 if (!NILP (Vexecuting_macro))
888 {
889 if (!NILP (Vquit_flag))
890 {
891 Vexecuting_macro = Qt;
892 QUIT; /* Make some noise. */
893 /* Will return since macro now empty. */
894 }
895 }
896
897 /* Do redisplay processing after this command except in special
898 cases identified below that set no_redisplay to 1. */
899 no_redisplay = 0;
900
901 /* Execute the command. */
902
903 if (NILP (cmd))
904 {
905 /* nil means key is undefined. */
906 bitch_at_user ();
907 defining_kbd_macro = 0;
908 update_mode_lines = 1;
909 Vprefix_arg = Qnil;
910 }
911 else
912 {
913 this_command = cmd;
914 if (NILP (Vprefix_arg) && ! no_direct)
915 {
916 /* Recognize some common commands in common situations and
917 do them directly. */
918 if (EQ (cmd, Qforward_char) && point < ZV)
919 {
920 struct Lisp_Vector *dp
921 = window_display_table (XWINDOW (selected_window));
922 lose = FETCH_CHAR (point);
923 SET_PT (point + 1);
924 if (((dp == 0 && lose >= 040 && lose < 0177)
925 ||
926 (dp && (XTYPE (dp->contents[lose]) != Lisp_String
927 || XSTRING (dp->contents[lose])->size == sizeof (GLYPH))))
284f4730
JB
928 && (XFASTINT (XWINDOW (selected_window)->last_modified)
929 >= MODIFF)
930 && (XFASTINT (XWINDOW (selected_window)->last_point)
931 == point - 1)
932 && !windows_or_buffers_changed
933 && EQ (current_buffer->selective_display, Qnil)
934 && !detect_input_pending ()
935 && NILP (Vexecuting_macro))
936 no_redisplay = direct_output_forward_char (1);
937 goto directly_done;
938 }
939 else if (EQ (cmd, Qbackward_char) && point > BEGV)
940 {
941 struct Lisp_Vector *dp
942 = window_display_table (XWINDOW (selected_window));
943 SET_PT (point - 1);
944 lose = FETCH_CHAR (point);
945 if (((dp == 0 && lose >= 040 && lose < 0177)
946 ||
947 (dp && (XTYPE (dp->contents[lose]) != Lisp_String
948 || XSTRING (dp->contents[lose])->size == sizeof (GLYPH))))
284f4730
JB
949 && (XFASTINT (XWINDOW (selected_window)->last_modified)
950 >= MODIFF)
951 && (XFASTINT (XWINDOW (selected_window)->last_point)
952 == point + 1)
953 && !windows_or_buffers_changed
954 && EQ (current_buffer->selective_display, Qnil)
955 && !detect_input_pending ()
956 && NILP (Vexecuting_macro))
957 no_redisplay = direct_output_forward_char (-1);
958 goto directly_done;
959 }
960 else if (EQ (cmd, Qself_insert_command)
961 /* Try this optimization only on ascii keystrokes. */
962 && XTYPE (last_command_char) == Lisp_Int)
963 {
964 unsigned char c = XINT (last_command_char);
965
966 if (NILP (Vexecuting_macro) &&
967 !EQ (minibuf_window, selected_window))
968 {
969 if (!nonundocount || nonundocount >= 20)
970 {
971 Fundo_boundary ();
972 nonundocount = 0;
973 }
974 nonundocount++;
975 }
976 lose = (XFASTINT (XWINDOW (selected_window)->last_modified)
977 < MODIFF)
978 || (XFASTINT (XWINDOW (selected_window)->last_point)
979 != point)
980 || MODIFF <= current_buffer->save_modified
981 || windows_or_buffers_changed
982 || !EQ (current_buffer->selective_display, Qnil)
983 || detect_input_pending ()
984 || !NILP (Vexecuting_macro);
985 if (internal_self_insert (c, 0))
986 {
987 lose = 1;
988 nonundocount = 0;
989 }
990 if (!lose &&
991 (point == ZV || FETCH_CHAR (point) == '\n'))
992 {
993 struct Lisp_Vector *dp
994 = window_display_table (XWINDOW (selected_window));
995
996 if (dp == 0 || XTYPE (dp->contents[c]) != Lisp_String)
997 no_redisplay = direct_output_for_insert (c);
998 else if (XSTRING (dp->contents[c])->size
999 == sizeof (GLYPH))
1000 no_redisplay =
1001 direct_output_for_insert (*(GLYPH *)XSTRING (dp->contents[c])->data);
1002 }
1003 goto directly_done;
1004 }
1005 }
1006
1007 /* Here for a command that isn't executed directly */
1008
1009 nonundocount = 0;
1010 if (NILP (Vprefix_arg))
1011 Fundo_boundary ();
1012 Fcommand_execute (cmd, Qnil);
1013
284f4730 1014 }
a764a753 1015 directly_done: ;
284f4730
JB
1016
1017 /* If there is a prefix argument,
1018 1) We don't want last_command to be ``universal-argument''
1019 (that would be dumb), so don't set last_command,
1020 2) we want to leave echoing on so that the prefix will be
1021 echoed as part of this key sequence, so don't call
1022 cancel_echoing, and
1023 3) we want to leave this_command_key_count non-zero, so that
1024 read_char will realize that it is re-reading a character, and
1025 not echo it a second time. */
1026 if (NILP (Vprefix_arg))
1027 {
1028 last_command = this_command;
1029 cancel_echoing ();
1030 this_command_key_count = 0;
1031 }
1032 }
1033}
1034\f
1035/* Number of seconds between polling for input. */
1036int polling_period;
1037
1038/* Nonzero means polling for input is temporarily suppresed. */
1039int poll_suppress_count;
1040
1041#ifdef POLL_FOR_INPUT
1042int polling_for_input;
1043
1044/* Handle an alarm once each second and read pending input
1045 so as to handle a C-g if it comces in. */
1046
1047SIGTYPE
1048input_poll_signal ()
1049{
1050#ifdef HAVE_X_WINDOWS
1051 extern int x_input_blocked;
1052 if (x_input_blocked == 0)
1053#endif
1054 if (!waiting_for_input)
1055 read_avail_input (0);
1056 signal (SIGALRM, input_poll_signal);
1057 alarm (polling_period);
1058}
1059
1060#endif
1061
1062/* Begin signals to poll for input, if they are appropriate.
1063 This function is called unconditionally from various places. */
1064
1065start_polling ()
1066{
1067#ifdef POLL_FOR_INPUT
1068 if (read_socket_hook)
1069 {
1070 poll_suppress_count--;
1071 if (poll_suppress_count == 0)
1072 {
1073 signal (SIGALRM, input_poll_signal);
1074 polling_for_input = 1;
1075 alarm (polling_period);
1076 }
1077 }
1078#endif
1079}
1080
1081/* Turn off polling. */
1082
1083stop_polling ()
1084{
1085#ifdef POLL_FOR_INPUT
1086 if (read_socket_hook)
1087 {
1088 if (poll_suppress_count == 0)
1089 {
1090 polling_for_input = 0;
1091 alarm (0);
1092 }
1093 poll_suppress_count++;
1094 }
1095#endif
1096}
1097\f
1098/* Input of single characters from keyboard */
1099
1100Lisp_Object print_help ();
1101static Lisp_Object kbd_buffer_get_event ();
1102
1103/* read a character from the keyboard; call the redisplay if needed */
1104/* commandflag 0 means do not do auto-saving, but do do redisplay.
1105 -1 means do not do redisplay, but do do autosaving.
1106 1 means do both. */
1107
1108Lisp_Object
1109read_char (commandflag)
1110 int commandflag;
1111{
1112 register Lisp_Object c;
1113 int count;
1114 jmp_buf save_jump;
1115
1116 if (!NILP (unread_command_char))
1117 {
1118 c = unread_command_char;
1119 unread_command_char = Qnil;
1120
1121#if 0 /* We're not handling mouse keys specially anymore. */
1122 if (!EQ (XTYPE (obj), Lisp_Int)) /* Mouse thing */
1123 {
1124 num_input_chars++;
1125 last_input_char = 0;
1126 return obj;
1127 }
1128#endif
1129
1130 if (this_command_key_count == 0)
1131 goto reread_first;
1132 else
1133 goto reread;
1134 }
1135
1136 if (!NILP (Vexecuting_macro))
1137 {
1138 if (executing_macro_index >= Flength (Vexecuting_macro))
1139 {
1140 XSET (c, Lisp_Int, -1);
1141 return c;
1142 }
1143
1144 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
1145 executing_macro_index++;
1146
1147 goto from_macro;
1148 }
1149
1150 /* Save outer setjmp data, in case called recursively. */
1151 bcopy (getcjmp, save_jump, sizeof getcjmp);
1152
1153 stop_polling ();
1154
1155 if (commandflag >= 0 && !input_pending && !detect_input_pending ())
1156 redisplay ();
1157
1158 if (_setjmp (getcjmp))
1159 {
1160 XSET (c, Lisp_Int, quit_char);
519a3b65
JB
1161#ifdef MULTI_SCREEN
1162 XSET (Vlast_event_screen, Lisp_Screen, selected_screen);
1163#endif
284f4730 1164
ffd56f97 1165 clear_waiting_for_input ();
284f4730
JB
1166
1167 goto non_reread;
1168 }
1169
1170 /* Message turns off echoing unless more keystrokes turn it on again. */
1171 if (echo_area_glyphs && *echo_area_glyphs && echo_area_glyphs != echobuf)
1172 cancel_echoing ();
1173 else
1174 /* If already echoing, continue. */
1175 echo_dash ();
1176
1177 /* If in middle of key sequence and minibuffer not active,
1178 start echoing if enough time elapses. */
1179 if (minibuf_level == 0 && !immediate_echo && this_command_key_count > 0
1180 && echo_keystrokes > 0
1181 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0))
1182 {
1183 Lisp_Object tem0;
1184
1185 tem0 = Fsit_for (make_number (echo_keystrokes), Qnil, Qt);
1186 if (EQ (tem0, Qt))
1187 echo ();
1188 }
1189
1190 /* Maybe auto save due to number of keystrokes or idle time. */
1191
1192 if (commandflag != 0
1193 && auto_save_interval > 0
1194 && num_input_chars - last_auto_save > max (auto_save_interval, 20)
1195 && !detect_input_pending ())
1196 {
1197 jmp_buf temp;
1198 save_getcjmp (temp);
1199 Fdo_auto_save (Qnil, Qnil);
1200 restore_getcjmp (temp);
1201 }
1202
1203 /* Slow down auto saves logarithmically in size of current buffer,
1204 and garbage collect while we're at it. */
1205 {
1206 int delay_level, buffer_size;
1207
1208 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
1209 last_non_minibuf_size = Z - BEG;
1210 buffer_size = (last_non_minibuf_size >> 8) + 1;
1211 delay_level = 0;
1212 while (buffer_size > 64)
1213 delay_level++, buffer_size -= buffer_size >> 2;
1214 if (delay_level < 4) delay_level = 4;
1215 /* delay_level is 4 for files under around 50k, 7 at 100k,
1216 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
1217
1218 /* Auto save if enough time goes by without input. */
1219 if (commandflag != 0
1220 && num_input_chars > last_auto_save
1221 && XTYPE (Vauto_save_timeout) == Lisp_Int
1222 && XINT (Vauto_save_timeout) > 0)
1223 {
1224 Lisp_Object tem0;
1225 int delay = delay_level * XFASTINT (Vauto_save_timeout) / 4;
1226 tem0 = Fsit_for (make_number (delay), Qnil, Qt);
1227 if (EQ (tem0, Qt))
1228 {
1229 jmp_buf temp;
1230 save_getcjmp (temp);
1231 Fdo_auto_save (Qnil, Qnil);
1232 restore_getcjmp (temp);
1233
1234 /* If we have auto-saved and there is still no input
1235 available, garbage collect if there has been enough
1236 consing going on to make it worthwhile. */
1237 if (!detect_input_pending ()
1238 && consing_since_gc > gc_cons_threshold / 2)
1239 Fgarbage_collect ();
1240 }
1241 }
1242 }
1243
1244 /* Actually read a character, waiting if necessary. */
1245 c = kbd_buffer_get_event ();
1246
1247 if (NILP (c))
1248 abort (); /* Don't think this can happen. */
1249
1250#if 0 /* I think that all the different kinds of events should be
1251 handled together now... */
1252 if (XTYPE (c) != Lisp_Int)
1253 {
1254 start_polling ();
1255 return c;
1256 }
1257 c = XINT (obj);
1258#endif
1259
1260 /* Terminate Emacs in batch mode if at eof. */
1261 if (noninteractive && c < 0)
1262 Fkill_emacs (make_number (1));
1263
1264 non_reread:
1265
1266 bcopy (save_jump, getcjmp, sizeof getcjmp);
1267
1268 start_polling ();
1269
1270 echo_area_glyphs = 0;
1271
1272 /* Handle things that only apply to characters. */
1273 if (XTYPE (c) == Lisp_Int)
1274 {
1275 /* If kbd_buffer_get_event gave us an EOF, return that. */
1276 if (XINT (c) < 0)
1277 return c;
1278
1279 /* Strip the high bits, and maybe the meta bit too. */
1280 XSETINT (c, c & (meta_key ? 0377 : 0177));
1281
1282 if (XTYPE (Vkeyboard_translate_table) == Lisp_String
1283 && XSTRING (Vkeyboard_translate_table)->size > XINT (c))
1284 XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[c]);
1285 }
1286
1287 total_keys++;
1288 recent_keys[recent_keys_index] = c;
1289 if (++recent_keys_index >= (sizeof (recent_keys)/sizeof(recent_keys[0])))
1290 recent_keys_index = 0;
1291
1292 /* Write c to the dribble file. If c is a lispy event, write
1293 the event's symbol to the dribble file, in <brackets>. Bleaugh.
1294 If you, dear reader, have a better idea, you've got the source. :-) */
1295 if (dribble)
1296 {
1297 if (XTYPE (c) == Lisp_Int)
1298 putc (c, dribble);
1299 else
1300 {
1301 Lisp_Object dribblee = c;
1302
1303 /* If it's a structured event, take the event header. */
1304 if (EVENT_HAS_PARAMETERS (dribblee))
1305 dribblee = EVENT_HEAD (dribblee);
1306
1307 if (XTYPE (c) == Lisp_Symbol)
1308 {
1309 putc ('<', dribble);
1310 fwrite (XSYMBOL (c)->name->data, sizeof (char),
1311 XSYMBOL (c)->name->size,
1312 dribble);
1313 putc ('>', dribble);
1314 }
1315 }
1316
1317 fflush (dribble);
1318 }
1319
1320 store_kbd_macro_char (c);
1321
1322 from_macro:
1323 reread_first:
1324 echo_char (c);
1325
1326 /* Record this character as part of the current key. */
1327 add_command_key (c);
1328
1329 /* Re-reading in the middle of a command */
1330 reread:
1331 last_input_char = c;
1332 num_input_chars++;
1333
1334 /* Process the help character specially if enabled */
1335 if (EQ (c, help_char) && !NILP (Vhelp_form))
1336 {
1337 Lisp_Object tem0;
1338 count = specpdl_ptr - specpdl;
1339
1340 record_unwind_protect (Fset_window_configuration,
1341 Fcurrent_window_configuration (Qnil));
1342
1343 tem0 = Feval (Vhelp_form);
1344 if (XTYPE (tem0) == Lisp_String)
1345 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
1346
1347 cancel_echoing ();
1348 c = read_char (0);
1349 /* Remove the help from the screen */
1350 unbind_to (count, Qnil);
1351 redisplay ();
1352 if (EQ (c, make_number (040)))
1353 {
1354 cancel_echoing ();
1355 c = read_char (0);
1356 }
1357 }
1358
1359 return c;
1360}
1361
1362Lisp_Object
1363print_help (object)
1364 Lisp_Object object;
1365{
1366 Fprinc (object, Qnil);
1367 return Qnil;
1368}
1369
1370/* Copy out or in the info on where C-g should throw to.
1371 This is used when running Lisp code from within get_char,
1372 in case get_char is called recursively.
1373 See read_process_output. */
1374
1375save_getcjmp (temp)
1376 jmp_buf temp;
1377{
1378 bcopy (getcjmp, temp, sizeof getcjmp);
1379}
1380
1381restore_getcjmp (temp)
1382 jmp_buf temp;
1383{
1384 bcopy (temp, getcjmp, sizeof getcjmp);
1385}
1386
1387\f
1388/* Low level keyboard/mouse input.
1389 kbd_buffer_store_event places events in kbd_buffer, and
1390 kbd_buffer_get_event retrieves them.
1391 mouse_moved indicates when the mouse has moved again, and
1392 *mouse_position_hook provides the mouse position. */
1393
1394/* Set this for debugging, to have a way to get out */
1395int stop_character;
1396
1397extern int screen_garbaged;
1398
1399/* Return true iff there are any events in the queue that read-char
1400 would return. If this returns false, a read-char would block. */
1401static int
1402readable_events ()
1403{
1404 struct input_event *ep;
1405
1406 if (EVENT_QUEUES_EMPTY)
1407 return 0;
1408
1409 if (do_mouse_tracking)
1410 return 1;
1411
1412 /* Mouse tracking is disabled, so we need to actually scan the
1413 input queue to see if any events are currently readable. */
1414 for (ep = kbd_fetch_ptr; ep != kbd_store_ptr; ep++)
1415 {
1416 if (ep == kbd_buffer + KBD_BUFFER_SIZE)
1417 ep = kbd_buffer;
1418
1419 /* Skip button-up events. */
1420 if ((ep->kind == mouse_click || ep->kind == scrollbar_click)
1421 && (ep->modifiers & up_modifier))
1422 continue;
1423
1424 return 1;
1425 }
1426
1427 return 0;
1428}
1429
1430
1431/* Restore mouse tracking enablement. See Ftrack_mouse for the only use
1432 of this function. */
1433static Lisp_Object
1434tracking_off (old_value)
1435 Lisp_Object old_value;
1436{
1437 if (! XFASTINT (old_value))
1438 {
1439 do_mouse_tracking = 0;
1440
1441 /* Redisplay may have been preempted because there was input
1442 available, and it assumes it will be called again after the
1443 input has been processed. If the only input available was
1444 the sort that we have just disabled, then we need to call
1445 redisplay. */
1446 if (!readable_events ())
1447 {
1448 redisplay_preserve_echo_area ();
1449 get_input_pending (&input_pending);
1450 }
1451 }
1452}
1453
1454DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
1455 "Evaluate BODY with mouse movement and button release events enabled.\n\
1456Within a track-mouse, read-event reports mouse movement and button releases;\n\
1457otherwise, they are ignored.")
1458 (args)
1459 Lisp_Object args;
1460{
1461 int count = specpdl_ptr - specpdl;
1462 Lisp_Object val;
1463
1464 XSET (val, Lisp_Int, do_mouse_tracking);
1465 record_unwind_protect (tracking_off, val);
1466
1467 do_mouse_tracking = 1;
1468
1469 val = Fprogn (args);
1470 return unbind_to (count, val);
1471}
1472
1473/* Store an event obtained at interrupt level into kbd_buffer, fifo */
1474
1475void
1476kbd_buffer_store_event (event)
1477 register struct input_event *event;
1478{
1479 if (event->kind == no_event)
1480 abort ();
1481
1482 if (event->kind == ascii_keystroke)
1483 {
1484 register int c = XFASTINT (event->code) & 0377;
1485
1486 if (c == quit_char
1487 || ((c == (0200 | quit_char)) && !meta_key))
1488 {
1489 /* If this results in a quit_char being returned to Emacs as
1490 input, set last-event-screen properly. If this doesn't
1491 get returned to Emacs as an event, the next event read
1492 will set Vlast_event_screen again, so this is safe to do. */
1493 extern SIGTYPE interrupt_signal ();
1494 XSET (Vlast_event_screen, Lisp_Screen, event->screen);
ffd56f97 1495 last_event_timestamp = event->timestamp;
284f4730
JB
1496 interrupt_signal ();
1497 return;
1498 }
1499
1500 if (c && c == stop_character)
1501 {
1502 sys_suspend ();
1503 return;
1504 }
1505
1506 XSET (event->code, Lisp_Int, c);
1507 }
1508
1509 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
1510 kbd_store_ptr = kbd_buffer;
1511
1512 /* Don't let the very last slot in the buffer become full,
1513 since that would make the two pointers equal,
1514 and that is indistinguishable from an empty buffer.
1515 Discard the event if it would fill the last slot. */
1516 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
1517 {
1518 kbd_store_ptr->kind = event->kind;
1519 kbd_store_ptr->code = event->code;
1520 kbd_store_ptr->part = event->part;
1521 kbd_store_ptr->screen = event->screen;
1522 kbd_store_ptr->modifiers = event->modifiers;
1523 kbd_store_ptr->x = event->x;
1524 kbd_store_ptr->y = event->y;
1525 kbd_store_ptr->timestamp = event->timestamp;
1526
1527 kbd_store_ptr++;
1528 }
1529}
1530
1531static Lisp_Object make_lispy_event ();
1532static Lisp_Object make_lispy_movement ();
1533static Lisp_Object modify_event_symbol ();
1534
1535static Lisp_Object
1536kbd_buffer_get_event ()
1537{
1538 register int c;
1539 Lisp_Object obj;
1540
1541 if (noninteractive)
1542 {
1543 c = getchar ();
1544 XSET (obj, Lisp_Int, c);
1545 return obj;
1546 }
1547
1548 /* Wait until there is input available. */
1549 for (;;)
1550 {
1551
1552 /* Process or toss any events that we don't want to return as
1553 input. The fact that we remove undesirable events here
1554 allows us to use EVENT_QUEUES_EMPTY in the rest of this loop. */
1555 if (! do_mouse_tracking)
1556 while (kbd_fetch_ptr != kbd_store_ptr)
1557 {
1558 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
1559 kbd_fetch_ptr = kbd_buffer;
1560
1561 if (kbd_fetch_ptr->kind == mouse_click
1562 || kbd_fetch_ptr->kind == scrollbar_click)
1563 {
1564 if ((kbd_fetch_ptr->modifiers & up_modifier) == 0)
1565 break;
1566 }
1567 else
1568 break;
1569
1570 kbd_fetch_ptr++;
1571 }
1572
1573 if (!EVENT_QUEUES_EMPTY)
1574 break;
1575
1576 /* If the quit flag is set, then read_char will return
1577 quit_char, so that counts as "available input." */
1578 if (!NILP (Vquit_flag))
1579 quit_throw_to_read_char ();
1580
1581 /* One way or another, wait until input is available; then, if
1582 interrupt handlers have not read it, read it now. */
1583
1584#ifdef OLDVMS
1585 wait_for_kbd_input ();
1586#else
1587/* Note SIGIO has been undef'd if FIONREAD is missing. */
1588#ifdef SIGIO
1589 gobble_input (0);
1590#endif /* SIGIO */
1591 if (EVENT_QUEUES_EMPTY)
1592 {
284f4730 1593 wait_reading_process_input (0, 0, -1, 1);
284f4730
JB
1594
1595 if (!interrupt_input && EVENT_QUEUES_EMPTY)
1596 {
1597 read_avail_input (0);
1598 }
1599 }
1600#endif /* not VMS */
1601 }
1602
1603 /* At this point, we know that there is a readable event available
1604 somewhere. If the event queue is empty, then there must be a
1605 mouse movement enabled and available. */
1606 if (kbd_fetch_ptr != kbd_store_ptr)
1607 {
1608 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
1609 kbd_fetch_ptr = kbd_buffer;
1610 XSET (Vlast_event_screen, Lisp_Screen, kbd_fetch_ptr->screen);
1611 last_event_timestamp = XINT (kbd_fetch_ptr->timestamp);
1612 obj = make_lispy_event (kbd_fetch_ptr);
1613 kbd_fetch_ptr->kind = no_event;
1614 kbd_fetch_ptr++;
1615 if (XTYPE (obj) == Lisp_Int)
1616 XSET (obj, Lisp_Int, XINT (obj) & (meta_key ? 0377 : 0177));
1617 }
1618 else if (do_mouse_tracking && mouse_moved)
1619 {
1620 SCREEN_PTR screen;
1621 Lisp_Object x, y, time;
1622
1623 (*mouse_position_hook) (&screen, &x, &y, &time);
1624 XSET (Vlast_event_screen, Lisp_Screen, screen);
1625
1626 obj = make_lispy_movement (screen, x, y, time);
1627 }
1628 else
1629 /* We were promised by the above while loop that there was
1630 something for us to read! */
1631 abort ();
1632
1633 input_pending = readable_events ();
1634
1635 return (obj);
1636}
1637
1638/* Caches for modify_event_symbol. */
1639static Lisp_Object func_key_syms;
1640static Lisp_Object mouse_syms;
1641
1642/* You'll notice that this table is arranged to be conveniently
1643 indexed by X Windows keysym values. */
1644static char *lispy_function_keys[] =
1645 {
1646 /* X Keysym value */
1647
1648 "home", /* 0xff50 */ /* IsCursorKey */
1649 "left",
1650 "up",
1651 "right",
1652 "down",
1653 "prior",
1654 "next",
1655 "end",
1656 "begin",
1657 0, /* 0xff59 */
1658 0, 0, 0, 0, 0, 0,
1659 "select", /* 0xff60 */ /* IsMiscFunctionKey */
1660 "print",
1661 "execute",
1662 "insert",
1663 0, /* 0xff64 */
1664 "undo",
1665 "redo",
1666 "menu",
1667 "find",
1668 "cancel",
1669 "help",
1670 "break", /* 0xff6b */
1671
1672 /* Here are some keys found mostly on HP keyboards. The X event
1673 handling code will strip bit 29, which flags vendor-specific
1674 keysyms. */
1675 "reset", /* 0x1000ff6c */
1676 "system",
1677 "user",
1678 "clearline",
1679 "insertline",
1680 "deleteline",
1681 "insertchar",
1682 "deletechar",
1683 "backtab",
1684 "kp_backtab", /* 0x1000ff75 */
1685 0, /* 0xff76 */
1686 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff7f */
1687 "kp-space", /* 0xff80 */ /* IsKeypadKey */
1688 0, 0, 0, 0, 0, 0, 0, 0,
1689 "kp-tab", /* 0xff89 */
1690 0, 0, 0,
1691 "kp-enter", /* 0xff8d */
1692 0, 0, 0,
1693 "kp-f1", /* 0xff91 */
1694 "kp-f2",
1695 "kp-f3",
1696 "kp-f4",
1697 0, /* 0xff95 */
1698 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1699 "kp-multiply", /* 0xffaa */
1700 "kp-add",
1701 "kp-separator",
1702 "kp-subtract",
1703 "kp-decimal",
1704 "kp-divide", /* 0xffaf */
1705 "kp-0", /* 0xffb0 */
1706 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
1707 0, /* 0xffba */
1708 0, 0,
1709 "kp-equal", /* 0xffbd */
1710 "f1", /* 0xffbe */ /* IsFunctionKey */
1711 "f2", "f3", "f4",
1712 "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12",
1713 "f13", "f14", "f15", "f16", "f17", "f18", "f19", "f20",
1714 "f21", "f22", "f23", "f24", "f25", "f26", "f27", "f28",
1715 "f29", "f30", "f31", "f32", "f33", "f34", "f35" /* 0xffe0 */
1716 };
1717
1718static char *lispy_mouse_names[] =
1719{
1720 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
1721};
1722
1723/* Given a struct input_event, build the lisp event which represents
1724 it. If EVENT is 0, build a mouse movement event from the mouse
1725 movement buffer, which should have a movement event in it. */
1726
1727static Lisp_Object
1728make_lispy_event (event)
1729 struct input_event *event;
1730{
1731#ifdef SWITCH_ENUM_BUG
1732 switch ((int) event->kind)
1733#else
1734 switch (event->kind)
1735#endif
1736 {
1737
1738 /* A simple keystroke. */
1739 case ascii_keystroke:
1740 return event->code;
1741 break;
1742
1743 /* A function key. The symbol may need to have modifier prefixes
1744 tacked onto it. */
1745 case non_ascii_keystroke:
1746 return modify_event_symbol (XFASTINT (event->code), event->modifiers,
1747 Qfunction_key,
1748 lispy_function_keys, &func_key_syms,
1749 (sizeof (lispy_function_keys)
1750 / sizeof (lispy_function_keys[0])));
1751 break;
1752
1753 /* A mouse click - build a list of the relevant information. */
1754 case mouse_click:
1755 {
1756 int part;
1757 Lisp_Object window =
1758 window_from_coordinates (event->screen,
1759 XINT (event->x), XINT (event->y),
1760 &part);
1761 Lisp_Object posn;
1762
1763 if (XTYPE (window) != Lisp_Window)
1764 posn = Qnil;
1765 else
1766 {
1767 XSETINT (event->x, (XINT (event->x)
1768 - XINT (XWINDOW (window)->left)));
1769 XSETINT (event->y, (XINT (event->y)
1770 - XINT (XWINDOW (window)->top)));
1771
1772 if (part == 1)
1773 posn = Qmode_line;
1774 else if (part == 2)
1775 posn = Qvertical_split;
1776 else
1777 XSET (posn, Lisp_Int,
1778 buffer_posn_from_coords (XWINDOW (window),
1779 XINT (event->x),
1780 XINT (event->y)));
1781 }
1782
1783 return Fcons (modify_event_symbol (XFASTINT (event->code) - 1,
1784 event->modifiers,
1785 Qmouse_click,
1786 lispy_mouse_names, &mouse_syms,
1787 (sizeof (lispy_mouse_names)
1788 / sizeof (lispy_mouse_names[0]))),
1789 Fcons (window,
1790 Fcons (posn,
1791 Fcons (Fcons (event->x, event->y),
1792 Fcons (event->timestamp,
1793 Qnil)))));
1794 }
1795
1796 /* A scrollbar click. Build a list containing the relevant
1797 information. */
1798 case scrollbar_click:
1799 {
1800 Lisp_Object button
1801 = modify_event_symbol (XFASTINT (event->code) - 1,
1802 event->modifiers,
1803 Qmouse_click,
1804 lispy_mouse_names, &mouse_syms,
1805 (sizeof (lispy_mouse_names)
1806 / sizeof (lispy_mouse_names[0])));
1807 return Fcons (event->part,
1808 Fcons (SCREEN_SELECTED_WINDOW (event->screen),
1809 Fcons (button,
1810 Fcons (Fcons (event->x, event->y),
1811 Fcons (event->timestamp,
1812 Qnil)))));
1813 }
1814
1815 /* The 'kind' field of the event is something we don't recognize. */
1816 default:
1817 abort();
1818 }
1819}
1820
1821static Lisp_Object
1822make_lispy_movement (screen, x, y, time)
1823 SCREEN_PTR screen;
1824 Lisp_Object x, y;
1825 Lisp_Object time;
1826{
1827 Lisp_Object window;
1828 int ix, iy;
1829 Lisp_Object posn;
1830 int part;
1831
1832 ix = XINT (x);
1833 iy = XINT (y);
1834 window = (screen
1835 ? window_from_coordinates (screen, ix, iy, &part)
1836 : Qnil);
1837 if (XTYPE (window) != Lisp_Window)
1838 posn = Qnil;
1839 else
1840 {
1841 ix -= XINT (XWINDOW (window)->left);
1842 iy -= XINT (XWINDOW (window)->top);
1843 if (part == 1)
1844 posn = Qmode_line;
1845 else if (part == 2)
1846 posn = Qvertical_split;
1847 else
1848 XSET (posn, Lisp_Int, buffer_posn_from_coords (XWINDOW (window),
1849 ix, iy));
1850 }
1851
1852 XSETINT (x, ix);
1853 XSETINT (y, iy);
1854 return Fcons (Qmouse_movement,
1855 Fcons (window,
1856 Fcons (posn,
1857 Fcons (Fcons (x, y),
1858 Fcons (time, Qnil)))));
1859}
1860
1861
1862
1863/* Place the written representation of MODIFIERS in BUF, '\0'-terminated,
1864 and return its length. */
1865
1866static int
1867format_modifiers (modifiers, buf)
1868 int modifiers;
1869 char *buf;
1870{
1871 char *p = buf;
1872
1873 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
1874 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
1875 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
1876 if (modifiers & up_modifier) { *p++ = 'U'; *p++ = '-'; }
1877 *p = '\0';
1878
1879 return p - buf;
1880}
1881
1882
1883/* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
1884 return a symbol with the modifiers placed in the canonical order.
1885
1886 Fdefine_key calls this to make sure that (for example) C-M-foo
1887 and M-C-foo end up being equivalent in the keymap. */
1888
1889Lisp_Object
1890reorder_modifiers (symbol)
1891 Lisp_Object symbol;
1892{
1893 struct Lisp_String *name;
1894 int i;
1895 int modifiers;
1896 int not_canonical;
1897
1898 CHECK_SYMBOL (symbol, 1);
1899
1900 modifiers = 0;
1901 name = XSYMBOL (symbol)->name;
1902
1903 /* Special case for things with only one modifier, which is
1904 (hopefully) the vast majority of cases. */
1905 if (! (name->size >= 4 && name->data[1] == '-' && name->data[3] == '-'))
1906 return symbol;
1907
1908 for (i = 0; i + 1 < name->size && name->data[i + 1] == '-'; i += 2)
1909 switch (name->data[i])
1910 {
1911 case 'M':
1912 not_canonical |= (modifiers & (meta_modifier|ctrl_modifier
1913 |shift_modifier|up_modifier));
1914 modifiers |= meta_modifier;
1915 break;
1916
1917 case 'C':
1918 not_canonical |= (modifiers &
1919 (ctrl_modifier|shift_modifier|up_modifier));
1920 modifiers |= ctrl_modifier;
1921 break;
1922
1923 case 'S':
1924 not_canonical |= (modifiers & (shift_modifier|up_modifier));
1925 modifiers |= shift_modifier;
1926 break;
1927
1928 case 'U':
1929 not_canonical |= (modifiers & (up_modifier));
1930 modifiers |= up_modifier;
1931 break;
1932
1933 default:
1934 goto no_more_modifiers;
1935 }
1936 no_more_modifiers:
1937
1938 if (!not_canonical)
1939 return symbol;
1940
1941 /* The modifiers were out of order, so find a new symbol with the
1942 mods in order. Since the symbol name could contain nulls, we can't
1943 use intern here; we have to use Fintern, which expects a genuine
1944 Lisp_String, and keeps a reference to it. */
1945 {
1946 char *new_mods = (char *) alloca (sizeof ("C-M-S-U-"));
1947 int len = format_modifiers (modifiers, new_mods);
1948 Lisp_Object new_name = make_uninit_string (len + name->size - i);
1949
1950 bcopy (new_mods, XSTRING (new_name)->data, len);
1951 bcopy (name->data + i, XSTRING (new_name)->data + len, name->size - i);
1952
1953 return Fintern (new_name, Qnil);
1954 }
1955}
1956
1957
1958/* For handling events, we often want to produce a symbol whose name
1959 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
1960 to some base, like the name of a function key or mouse button.
1961 modify_event_symbol produces symbols of this sort.
1962
1963 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
1964 is the name of the i'th symbol. TABLE_SIZE is the number of elements
1965 in the table.
1966
1967 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
1968 persist between calls to modify_event_symbol that it can use to
1969 store a cache of the symbols it's generated for this NAME_TABLE
1970 before.
1971
1972 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
1973
1974 MODIFIERS is a set of modifier bits (as given in struct input_events)
1975 whose prefixes should be applied to the symbol name.
1976
1977 SYMBOL_KIND is the value to be placed in the event_kind property of
1978 the returned symbol. */
1979
1980static Lisp_Object
1981modify_event_symbol (symbol_num, modifiers, symbol_kind, name_table,
1982 symbol_table, table_size)
1983 int symbol_num;
1984 unsigned modifiers;
1985 Lisp_Object symbol_kind;
1986 char **name_table;
1987 Lisp_Object *symbol_table;
1988 int table_size;
1989{
1990 Lisp_Object *slot, *unmodified_slot;
1991
1992 /* Is this a request for a valid symbol? */
1993 if (symbol_num < 0 || symbol_num >= table_size
1994 || modifiers >= NUM_MODIFIER_COMBOS)
1995 abort ();
1996
1997 /* If *symbol_table is not a vector of the appropriate size,
1998 set it to one. */
1999 if (XTYPE (*symbol_table) != Lisp_Vector
2000 || XVECTOR (*symbol_table)->size != table_size)
2001 *symbol_table = Fmake_vector (make_number (table_size), Qnil);
2002
2003 unmodified_slot = slot = & XVECTOR (*symbol_table)->contents[symbol_num];
2004
2005 /* If there are modifier keys, there had better be a vector in
2006 this symbol's position of the symbol_table. */
2007 if (modifiers != 0)
2008 {
2009 Lisp_Object slot_contents = *slot;
2010
2011 /* If there isn't the right sort of vector there, put one in. */
2012 if (XTYPE (slot_contents) != Lisp_Vector
2013 || XVECTOR (slot_contents)->size != NUM_MODIFIER_COMBOS)
2014 {
2015 *slot = Fmake_vector (make_number (NUM_MODIFIER_COMBOS), Qnil);
2016
2017 /* Make sure that the vector has an entry for the unmodified
2018 symbol, so we can put it on the event_unmodified property. */
2019 if (! NILP (slot_contents))
2020 XVECTOR (*slot)->contents[0] = slot_contents;
2021 else
2022 XVECTOR (*slot)->contents[0] = intern (name_table [symbol_num]);
2023 }
2024 }
2025
2026 /* If this entry has been filled in with a modified symbol vector,
2027 point to the appropriate slot within that. */
2028 if (XTYPE (*slot) == Lisp_Vector)
2029 {
2030 unmodified_slot = & XVECTOR (*slot)->contents[0];
2031 slot = & XVECTOR (*slot)->contents[modifiers];
2032 }
2033
2034 /* Make sure we have an unmodified version of the symbol in its
2035 proper place? */
2036 if (NILP (*unmodified_slot))
2037 {
2038 *unmodified_slot = intern (name_table [symbol_num]);
2039 Fput (*unmodified_slot, Qevent_kind, symbol_kind);
2040 Fput (*unmodified_slot, Qevent_unmodified, *unmodified_slot);
2041 }
2042
2043 /* Have we already created a symbol for this combination of modifiers? */
2044 if (NILP (*slot))
2045 {
2046 /* No, let's create one. */
2047 char *modified_name
2048 = (char *) alloca (sizeof ("C-M-S-U-")
2049 + strlen (name_table [symbol_num]));
2050
2051 strcpy (modified_name + format_modifiers (modifiers, modified_name),
2052 name_table [symbol_num]);
2053
2054 *slot = intern (modified_name);
2055 Fput (*slot, Qevent_kind, symbol_kind);
2056 Fput (*slot, Qevent_unmodified, *unmodified_slot);
2057 }
2058
2059 return *slot;
2060}
2061\f
2062DEFUN ("mouse-click-p", Fmouse_click_p, Smouse_click_p, 1, 1, 0,
2063 "Return non-nil iff OBJECT is a representation of a mouse event.\n\
2064A mouse event is a list of five elements whose car is a symbol of the\n\
2065form <MODIFIERS>mouse-<DIGIT>. I hope this is a temporary hack.")
2066 (object)
2067 Lisp_Object object;
2068{
2069 if (EVENT_HAS_PARAMETERS (object)
2070 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (object)),
2071 Qmouse_click))
2072 return Qt;
2073 else
2074 return Qnil;
2075}
2076\f
2077/* Store into *addr a value nonzero if terminal input chars are available.
2078 Serves the purpose of ioctl (0, FIONREAD, addr)
2079 but works even if FIONREAD does not exist.
2080 (In fact, this may actually read some input.) */
2081
2082static void
2083get_input_pending (addr)
2084 int *addr;
2085{
2086 /* First of all, have we already counted some input? */
2087 *addr = !NILP (Vquit_flag) || readable_events ();
2088
2089 /* If input is being read as it arrives, and we have none, there is none. */
2090 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
2091 return;
2092
2093 /* Try to read some input and see how much we get. */
2094 gobble_input (0);
2095 *addr = !NILP (Vquit_flag) || readable_events ();
2096}
2097
2098/* Interface to read_avail_input, blocking SIGIO if necessary. */
2099
2100int
2101gobble_input (expected)
2102 int expected;
2103{
2104#ifndef VMS
2105#ifdef SIGIO
2106 if (interrupt_input)
2107 {
32676c08 2108 SIGMASKTYPE mask;
e065a56e 2109 mask = sigblockx (SIGIO);
284f4730 2110 read_avail_input (expected);
e065a56e 2111 sigsetmask (mask);
284f4730
JB
2112 }
2113 else
2114#endif
2115 read_avail_input (expected);
2116#endif
2117}
2118\f
2119#ifndef VMS
2120
2121/* Read any terminal input already buffered up by the system
2122 into the kbd_buffer, but do not wait.
2123
2124 EXPECTED should be nonzero if the caller knows there is some input.
2125
2126 Except on VMS, all input is read by this function.
2127 If interrupt_input is nonzero, this function MUST be called
2128 only when SIGIO is blocked.
2129
2130 Returns the number of keyboard chars read, or -1 meaning
2131 this is a bad time to try to read input. */
2132
2133static int
2134read_avail_input (expected)
2135 int expected;
2136{
2137 struct input_event buf[KBD_BUFFER_SIZE];
2138 register int i;
2139 int nread;
2140
2141 if (read_socket_hook)
2142 /* No need for FIONREAD or fcntl; just say don't wait. */
2143 nread = (*read_socket_hook) (0, buf, KBD_BUFFER_SIZE, expected, expected);
2144 else
2145 {
2146 unsigned char cbuf[KBD_BUFFER_SIZE];
2147
2148#ifdef FIONREAD
2149 /* Find out how much input is available. */
2150 if (ioctl (0, FIONREAD, &nread) < 0)
2151 /* Formerly simply reported no input, but that sometimes led to
2152 a failure of Emacs to terminate.
2153 SIGHUP seems appropriate if we can't reach the terminal. */
2154 kill (getpid (), SIGHUP);
2155 if (nread == 0)
2156 return 0;
2157 if (nread > sizeof cbuf)
2158 nread = sizeof cbuf;
2159#else /* no FIONREAD */
2160#ifdef USG
2161 /* Read some input if available, but don't wait. */
2162 nread = sizeof cbuf;
2163 fcntl (fileno (stdin), F_SETFL, O_NDELAY);
2164#else
2165 you lose;
2166#endif
2167#endif
2168
2169 /* Now read; for one reason or another, this will not block. */
2170 while (1)
2171 {
2172 nread = read (fileno (stdin), cbuf, nread);
2173#ifdef AIX
2174 /* The kernel sometimes fails to deliver SIGHUP for ptys.
2175 This looks incorrect, but it isn't, because _BSD causes
2176 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
2177 and that causes a value other than 0 when there is no input. */
2178 if (nread == 0)
2179 kill (SIGHUP, 0);
2180#endif
2181 /* Retry the read if it is interrupted. */
2182 if (nread >= 0
2183 || ! (errno == EAGAIN || errno == EFAULT
2184#ifdef EBADSLT
2185 || errno == EBADSLT
2186#endif
2187 ))
2188 break;
2189 }
2190
2191#ifndef FIONREAD
2192#ifdef USG
2193 fcntl (fileno (stdin), F_SETFL, 0);
2194#endif /* USG */
2195#endif /* no FIONREAD */
2196 for (i = 0; i < nread; i++)
2197 {
2198 buf[i].kind = ascii_keystroke;
2199 XSET (buf[i].code, Lisp_Int, cbuf[i]);
2200 buf[i].screen = selected_screen;
2201 }
2202 }
2203
2204 /* Scan the chars for C-g and store them in kbd_buffer. */
2205 for (i = 0; i < nread; i++)
2206 {
2207 kbd_buffer_store_event (&buf[i]);
2208 /* Don't look at input that follows a C-g too closely.
2209 This reduces lossage due to autorepeat on C-g. */
2210 if (buf[i].kind == ascii_keystroke
2211 && XINT(buf[i].code) == quit_char)
2212 break;
2213 }
2214
2215 return nread;
2216}
2217#endif /* not VMS */
2218\f
2219#ifdef SIGIO /* for entire page */
2220/* Note SIGIO has been undef'd if FIONREAD is missing. */
2221
2222input_available_signal (signo)
2223 int signo;
2224{
2225 /* Must preserve main program's value of errno. */
2226 int old_errno = errno;
2227#ifdef BSD4_1
2228 extern int select_alarmed;
2229#endif
2230
2231#ifdef USG
2232 /* USG systems forget handlers when they are used;
2233 must reestablish each time */
2234 signal (signo, input_available_signal);
2235#endif /* USG */
2236
2237#ifdef BSD4_1
2238 sigisheld (SIGIO);
2239#endif
2240
ffd56f97
JB
2241 if (input_available_clear_time)
2242 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
284f4730
JB
2243
2244 while (1)
2245 {
2246 int nread;
2247 nread = read_avail_input (1);
2248 /* -1 means it's not ok to read the input now.
2249 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
2250 0 means there was no keyboard input available. */
2251 if (nread <= 0)
2252 break;
2253
2254#ifdef BSD4_1
2255 select_alarmed = 1; /* Force the select emulator back to life */
2256#endif
2257 }
2258
2259#ifdef BSD4_1
2260 sigfree ();
2261#endif
2262 errno = old_errno;
2263}
2264#endif /* SIGIO */
2265\f
2266/* Return the prompt-string of a sparse keymap.
2267 This is the first element which is a string.
2268 Return nil if there is none. */
2269
2270Lisp_Object
2271map_prompt (map)
2272 Lisp_Object map;
2273{
2274 while (CONSP (map))
2275 {
2276 register Lisp_Object tem;
2277 tem = Fcar (map);
2278 if (XTYPE (tem) == Lisp_String)
2279 return tem;
2280 map = Fcdr (map);
2281 }
2282 return Qnil;
2283}
2284
2285static int echo_flag;
2286static int echo_now;
2287
2288/* Read a character like read_char but optionally prompt based on maps
2289 LOCAL and GLOBAL.
2290
2291 The prompting is done based on the prompt-string of the map
2292 and the strings associated with various map elements. */
2293
2294Lisp_Object
2295read_char_menu_prompt (prompt, local, global)
2296 int prompt;
2297 Lisp_Object local, global;
2298{
2299 register Lisp_Object rest, name;
2300 Lisp_Object hmap;
2301 int nlength;
2302 int width = SCREEN_WIDTH (selected_screen) - 4;
2303 char *menu = (char *) alloca (width);
2304
2305 /* Use local over global Menu maps */
2306
2307 if (menu_prompting)
2308 return read_char (!prompt);
2309
2310 /* We can't get prompt strings from dense keymaps. */
2311 if (CONSP (local)
2312 && EQ (Fcar (local), Qkeymap)
2313 && !(CONSP (XCONS (local)->cdr)
2314 && XTYPE (XCONS (XCONS (local)->cdr)->car) == Lisp_Vector))
2315 hmap = local;
2316 else if (CONSP (global)
2317 && EQ (Fcar (global), Qkeymap)
2318 && !(CONSP (XCONS (global)->cdr)
2319 && XTYPE (XCONS (XCONS (global)->cdr)->car) == Lisp_Vector))
2320 hmap = global;
2321 else
2322 return read_char (!prompt);
2323
2324 /* Get the map's prompt string. */
2325 name = map_prompt (hmap);
2326 if (NILP (name))
2327 return read_char (!prompt);
2328
2329 /* Prompt string always starts with map's prompt, and a space. */
2330 strcpy (menu, XSTRING (name)->data);
2331 nlength = XSTRING (name)->size;
2332 menu[nlength++] = ' ';
2333 menu[nlength] = 0;
2334
2335 /* Start prompting at start of map. */
2336 rest = hmap; /* Current menu item */
2337
2338 /* Present the documented bindings, a line at a time. */
2339 while (1)
2340 {
2341 int notfirst = 0;
2342 int i = nlength;
2343 Lisp_Object obj;
2344 int ch;
2345
2346 /* If reached end of map, start at beginning. */
2347 if (NILP (Fcdr (rest))) rest = hmap;
2348
2349 /* Loop over elements of map. */
2350 while (!NILP (rest) && i < width)
2351 {
2352 Lisp_Object s;
2353
2354 /* Look for conses whose cadrs are strings. */
2355 s = Fcar_safe (Fcdr_safe (Fcar_safe (rest)));
2356 if (XTYPE (s) != Lisp_String)
2357 /* Ignore all other elements. */
2358 ;
2359 /* If first such element, or enough room, add string to prompt. */
2360 else if (XSTRING (s)->size + i < width
2361 || !notfirst)
2362 {
2363 int thiswidth;
2364
2365 /* Punctuate between strings. */
2366 if (notfirst)
2367 {
2368 strcpy (menu + i, ", ");
2369 i += 2;
2370 }
2371 notfirst = 1;
2372
2373 /* Add as much of string as fits. */
2374 thiswidth = XSTRING (s)->size;
2375 if (thiswidth + i > width)
2376 thiswidth = width - i;
2377 bcopy (XSTRING (s)->data, menu + i, thiswidth);
2378 i += thiswidth;
2379 }
2380 else
2381 {
2382 /* If some elts don't fit, show there are more. */
2383 strcpy (menu + i, "...");
2384 break;
2385 }
2386
2387 /* Move past this element. */
2388 rest = Fcdr_safe (rest);
2389 }
2390
2391 /* Prompt with that and read response. */
2392 message1 (menu);
2393 obj = read_char (1);
2394
2395 if (XTYPE (obj) != Lisp_Int)
2396 return obj;
2397 else
2398 ch = XINT (obj);
2399
2400 if (obj != menu_prompt_more_char
2401 && (XTYPE (menu_prompt_more_char) != Lisp_Int
2402 || obj != make_number (Ctl (XINT (menu_prompt_more_char)))))
2403 return obj;
2404 }
2405}
2406
2407\f
2408/* Reading key sequences. */
2409
2410/* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
2411 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
2412 keymap, or nil otherwise. Return the index of the first keymap in
2413 which KEY has any binding, or NMAPS if no map has a binding.
2414
2415 If KEY is a meta ASCII character, treat it like meta-prefix-char
2416 followed by the corresponding non-meta character. Keymaps in
2417 CURRENT with non-prefix bindings for meta-prefix-char become nil in
2418 NEXT.
2419
2420 When KEY is not defined in any of the keymaps, if it is an upper
2421 case letter and there are bindings for the corresponding lower-case
2422 letter, return the bindings for the lower-case letter.
2423
2424 NEXT may == CURRENT. */
2425
2426static int
2427follow_key (key, nmaps, current, defs, next)
2428 Lisp_Object key;
2429 Lisp_Object *current, *defs, *next;
2430 int nmaps;
2431{
2432 int i, first_binding;
2433
2434 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
2435 followed by the corresponding non-meta character. */
2436 if (XTYPE (key) == Lisp_Int
2437 && XINT (key) >= 0200)
2438 {
2439 for (i = 0; i < nmaps; i++)
2440 if (! NILP (current[i]))
2441 {
2442 next[i] = get_keyelt (access_keymap (current[i],
2443 meta_prefix_char));
2444
2445 /* Note that since we pass the resulting bindings through
2446 get_keymap_1, non-prefix bindings for meta-prefix-char
2447 disappear. */
2448 next[i] = get_keymap_1 (next[i], 0);
2449 }
2450 else
2451 next[i] = Qnil;
2452
2453 current = next;
2454 XSET (key, Lisp_Int, XFASTINT (key) & 0177);
2455 }
2456
2457 first_binding = nmaps;
2458 for (i = nmaps - 1; i >= 0; i--)
2459 {
2460 if (! NILP (current[i]))
2461 {
2462 defs[i] = get_keyelt (access_keymap (current[i], key));
2463 if (! NILP (defs[i]))
2464 first_binding = i;
2465 }
2466 else
2467 defs[i] = Qnil;
2468 }
2469
2470 /* When KEY is not defined in any of the keymaps, if it is an upper
2471 case letter and there are bindings for the corresponding
2472 lower-case letter, return the bindings for the lower-case letter. */
2473 if (first_binding == nmaps
2474 && XTYPE (key) == Lisp_Int
2475 && UPPERCASEP (XINT (key)))
2476 {
2477 XSETINT (key, DOWNCASE (XINT (key)));
2478
2479 first_binding = nmaps;
2480 for (i = nmaps - 1; i >= 0; i--)
2481 {
2482 if (! NILP (current[i]))
2483 {
2484 defs[i] = get_keyelt (access_keymap (current[i], key));
2485 if (! NILP (defs[i]))
2486 first_binding = i;
2487 }
2488 else
2489 defs[i] = Qnil;
2490 }
2491 }
2492
2493 /* Given the set of bindings we've found, produce the next set of maps. */
2494 for (i = 0; i < nmaps; i++)
2495 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0);
2496
2497 return first_binding;
2498}
2499
2500/* Read a sequence of keys that ends with a non prefix character
2501 according to the keymaps in KEYMAPS[0..nmaps-1]. Keymaps appearing
2502 earlier in KEYMAPS take precidence over those appearing later.
2503
2504 Store the sequence in KEYBUF, a buffer of size BUFSIZE. Prompt
2505 with PROMPT. Echo starting immediately unless `prompt' is 0.
2506 Return the length of the key sequence stored.
2507
2508 If the user switches screens in the midst of a key sequence, we
2509 throw away any prefix we have read so far, and start afresh. For
2510 mouse clicks, we look up the click in the keymap of the buffer
2511 clicked on, throwing away any prefix if it is not the same buffer
2512 we used to be reading from. */
2513
2514static int
2515read_key_sequence (keybuf, bufsize, prompt)
2516 Lisp_Object *keybuf;
2517 int bufsize;
2518 Lisp_Object prompt;
2519{
2520 /* How many keys there are in the current key sequence. */
2521 int t;
2522
2523 /* The buffer that the most recently read event was typed at. This
2524 helps us read mouse clicks according to the buffer clicked in,
2525 and notice when the mouse has moved from one screen to another. */
2526 struct buffer *last_event_buffer = current_buffer;
2527
2528 /* The length of the echo buffer when we started reading, and
2529 the length of this_command_keys when we started reading. */
2530 int echo_start;
2531 int keys_start = this_command_key_count;
2532
2533 /* The number of keymaps we're scanning right now, and the number of
2534 keymaps we have allocated space for. */
2535 int nmaps;
2536 int nmaps_allocated = 0;
2537
2538 /* current[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
2539 in the current keymaps, or nil where it is not a prefix. */
2540 Lisp_Object *current;
2541
2542 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
2543 the current keymaps. */
2544 Lisp_Object *defs;
2545
2546 /* The lowest i such that defs[i] is non-nil. */
2547 int first_binding;
2548
2549 /* If mock_input > t, then KEYBUF[t] should be read as the next
2550 input key. */
2551 int mock_input = 0;
2552
2553 /* If the sequence is unbound in current[], keymap[fkey_start..fkey_end-1]
2554 is a prefix in Vfunction_key_map, and fkey_map is its binding.
2555 These might be > t, indicating that all function key scanning should
2556 hold off until t reaches them. */
2557 int fkey_start = 0, fkey_end = 0;
2558 Lisp_Object fkey_map = Vfunction_key_map;
2559
2560 if (INTERACTIVE)
2561 {
2562 if (prompt)
2563 echo_prompt (prompt);
2564 else if (cursor_in_echo_area)
2565 /* This doesn't put in a dash if the echo buffer is empty, so
2566 you don't always see a dash hanging out in the minibuffer. */
2567 echo_dash ();
2568 echo_start = echo_length ();
2569 }
2570
2571 /* If there is no function key map, turn of function key scanning. */
2572 if (NILP (Fkeymapp (Vfunction_key_map)))
2573 fkey_start = fkey_end = bufsize + 1;
2574
2575 restart:
2576 t = 0;
2577 this_command_key_count = keys_start;
2578
2579 {
2580 Lisp_Object *maps;
2581
2582 nmaps = current_minor_maps (0, &maps) + 2;
2583 if (nmaps > nmaps_allocated)
2584 {
2585 current = (Lisp_Object *) alloca (nmaps * sizeof (current[0]));
2586 defs = (Lisp_Object *) alloca (nmaps * sizeof (defs[0]));
2587 nmaps_allocated = nmaps;
2588 }
2589 bcopy (maps, current, (nmaps - 2) * sizeof (current[0]));
2590 current[nmaps-2] = last_event_buffer->keymap;
2591 current[nmaps-1] = global_map;
2592 }
2593
2594 /* Find an accurate initial value for first_binding. */
2595 for (first_binding = 0; first_binding < nmaps; first_binding++)
2596 if (! NILP (current[first_binding]))
2597 break;
2598
2599 while ((first_binding < nmaps && ! NILP (current[first_binding]))
2600 || (first_binding >= nmaps && fkey_start < t))
2601 {
2602 Lisp_Object key;
2603
2604 if (t >= bufsize)
2605 error ("key sequence too long");
2606
2607 /* Are we reading keys stuffed into keybuf? */
2608 if (t < mock_input)
2609 {
2610 key = keybuf[t];
2611 add_command_key (key);
2612 echo_char (key);
2613 }
2614 /* Otherwise, we should actually read a character. */
2615 else
2616 {
2617 struct buffer *buf;
2618
2619 if (!prompt && INTERACTIVE)
2620 key = read_char_menu_prompt (prompt, Qnil, Qnil);
2621 else
2622 key = read_char (!prompt);
2623
2624 /* The above routines return -1 at the end of a macro.
2625 Emacs 18 handles this by returning immediately with a
2626 zero, so that's what we'll do. */
2627 if (XTYPE (key) == Lisp_Int && XINT (key) < 0)
2628 return 0;
2629
2630 Vquit_flag = Qnil;
2631
2632 /* What buffer was this event typed/moused at? */
2633 if (XTYPE (key) == Lisp_Int || XTYPE (key) == Lisp_Symbol)
2634 buf = (XBUFFER
2635 (XWINDOW
2636 (SCREEN_SELECTED_WINDOW
2637 (XSCREEN (Vlast_event_screen)))->buffer));
2638 else if (EVENT_HAS_PARAMETERS (key))
2639 {
2640 Lisp_Object window = EVENT_WINDOW (key);
2641
2642 if (NILP (window))
2643 abort ();
2644
2645 buf = XBUFFER (XWINDOW (window)->buffer);
2646 }
2647 else
2648 abort ();
2649
2650 /* If this event came to a different buffer than the one
2651 we're currently in, switch buffers and start a new key
2652 sequence, starting with key. */
2653 if (buf != last_event_buffer)
2654 {
2655 last_event_buffer = buf;
2656 Fselect_screen (Vlast_event_screen, Qnil);
2657
2658 /* Arrange to read key as the next event. */
2659 keybuf[0] = key;
2660 mock_input = 1;
2661
2662 /* Truncate the key sequence in the echo area. */
2663 if (INTERACTIVE)
2664 echo_truncate (echo_start);
2665
2666 goto restart;
2667 }
2668 }
2669
2670 first_binding = (follow_key (key,
2671 nmaps - first_binding,
2672 current + first_binding,
2673 defs + first_binding,
2674 current + first_binding)
2675 + first_binding);
2676 keybuf[t++] = key;
2677
2678 /* If the sequence is unbound, see if we can hang a function key
2679 off the end of it. Don't reread the expansion of a function key. */
2680 if (first_binding >= nmaps
2681 && t > mock_input)
2682 {
2683 Lisp_Object fkey_next;
2684
2685 /* Scan from fkey_end until we find a bound suffix. */
2686 while (fkey_end < t)
2687 {
2688 fkey_next =
2689 get_keyelt (access_keymap (fkey_map, keybuf[fkey_end++]));
284f4730 2690 /* If keybuf[fkey_start..fkey_next] is bound in the
a764a753
JB
2691 function key map and it's a suffix of the current
2692 sequence (i.e. fkey_next == t), replace it with
2693 the binding and restart with fkey_start at the end. */
284f4730
JB
2694 if (XTYPE (fkey_next) == Lisp_Vector
2695 && fkey_end == t)
2696 {
2697 t = fkey_start + XVECTOR (fkey_next)->size;
2698 if (t >= bufsize)
2699 error ("key sequence too long");
2700
2701 bcopy (XVECTOR (fkey_next)->contents,
2702 keybuf + fkey_start,
2703 (t - fkey_start) * sizeof (keybuf[0]));
2704
2705 mock_input = t;
2706 fkey_start = fkey_end = t;
2707
2708 /* Truncate the key sequence in the echo area. */
2709 if (INTERACTIVE)
2710 echo_truncate (echo_start);
2711
2712 goto restart;
2713 }
2714
2715 fkey_map = get_keymap_1 (fkey_next, 0);
2716
a764a753
JB
2717 /* If we no longer have a bound suffix, try a new positions for
2718 fkey_start. */
284f4730
JB
2719 if (NILP (fkey_map))
2720 {
2721 fkey_end = ++fkey_start;
2722 fkey_map = Vfunction_key_map;
2723 }
2724 }
2725 }
2726 }
2727
2728 read_key_sequence_cmd = (first_binding < nmaps
2729 ? defs[first_binding]
2730 : Qnil);
2731
2732 return t;
2733}
2734
2735DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 1, 0,
2736 "Read a sequence of keystrokes and return as a string or vector.\n\
2737The sequence is sufficient to specify a non-prefix command in the\n\
2738current local and global maps.\n\
2739\n\
2740Arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
2741\n\
2742If Emacs is running on multiple screens, switching between screens in\n\
2743the midst of a keystroke will toss any prefix typed so far. A C-g\n\
2744typed while in this function is treated like any other character, and\n\
2745`quit-flag' is not set.")
2746 (prompt)
2747 Lisp_Object prompt;
2748{
2749 Lisp_Object keybuf[30];
2750 register int i;
2751 struct gcpro gcpro1, gcpro2;
2752
2753 if (!NILP (prompt))
2754 CHECK_STRING (prompt, 0);
2755 QUIT;
2756
2757 bzero (keybuf, sizeof keybuf);
2758 GCPRO1 (keybuf[0]);
2759 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
2760
2761 this_command_key_count = 0;
2762 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
2763 NILP (prompt) ? 0 : XSTRING (prompt)->data);
2764
2765 UNGCPRO;
2766 return make_array (i, keybuf);
2767}
2768\f
2769DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
2770 "Execute CMD as an editor command.\n\
2771CMD must be a symbol that satisfies the `commandp' predicate.\n\
2772Optional second arg RECORD-FLAG non-nil\n\
2773means unconditionally put this command in `command-history'.\n\
2774Otherwise, that is done only if an arg is read using the minibuffer.")
2775 (cmd, record)
2776 Lisp_Object cmd, record;
2777{
2778 register Lisp_Object final;
2779 register Lisp_Object tem;
2780 Lisp_Object prefixarg;
2781 struct backtrace backtrace;
2782 extern int debug_on_next_call;
2783
2784 prefixarg = Vprefix_arg, Vprefix_arg = Qnil;
2785 Vcurrent_prefix_arg = prefixarg;
2786 debug_on_next_call = 0;
2787
2788 if (XTYPE (cmd) == Lisp_Symbol)
2789 {
2790 tem = Fget (cmd, Qdisabled);
2791 if (!NILP (tem))
2792 return call1 (Vrun_hooks, Vdisabled_command_hook);
2793 }
2794
2795 while (1)
2796 {
ffd56f97 2797 final = Findirect_function (cmd);
284f4730
JB
2798
2799 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
2800 do_autoload (final, cmd);
2801 else
2802 break;
2803 }
2804
2805 if (XTYPE (final) == Lisp_String
2806 || XTYPE (final) == Lisp_Vector)
2807 {
2808 /* If requested, place the macro in the command history. For
2809 other sorts of commands, call-interactively takes care of
2810 this. */
2811 if (!NILP (record))
2812 Vcommand_history
2813 = Fcons (Fcons (Qexecute_kbd_macro,
2814 Fcons (final, Fcons (prefixarg, Qnil))),
2815 Vcommand_history);
2816
2817 return Fexecute_kbd_macro (final, prefixarg);
2818 }
2819 if (CONSP (final) || XTYPE (final) == Lisp_Subr
2820 || XTYPE (final) == Lisp_Compiled)
2821 {
2822 backtrace.next = backtrace_list;
2823 backtrace_list = &backtrace;
2824 backtrace.function = &Qcall_interactively;
2825 backtrace.args = &cmd;
2826 backtrace.nargs = 1;
2827 backtrace.evalargs = 0;
2828
2829 tem = Fcall_interactively (cmd, record);
2830
2831 backtrace_list = backtrace.next;
2832 return tem;
2833 }
2834 return Qnil;
2835}
2836\f
2837#if 0
2838DEFUN ("execute-mouse-event", Fexecute_mouse_event, Sexecute_mouse_event,
2839 1, 1, 0,
2840 "Execute the definition of the mouse-click event EVENT.\n\
2841The handler function is found by looking the event's key sequence up\n\
2842in the buffer's local mouse map and in `global-mouse-map'.\n\
2843\n\
2844After running the handler, call the value of `mouse-event-function'\n\
2845with EVENT as arg.")
2846 (event)
2847 Lisp_Object event;
2848{
2849 Lisp_Object tem;
2850 Lisp_Object mouse_cmd;
2851 Lisp_Object keyseq, window, screen_part, pos, time;
2852
2853#ifndef HAVE_X11
2854 Vmouse_event = event;
2855#endif
2856
2857 if (EQ (event, Qnil))
2858 {
2859 bitch_at_user ();
2860 return Qnil;
2861 }
2862
2863 CHECK_CONS (event, 0);
2864 pos = Fcar (event);
2865 window = Fcar (Fcdr (event));
2866 screen_part = Fcar (Fcdr (Fcdr (event)));
2867 keyseq = Fcar (Fcdr (Fcdr (Fcdr (event))));
2868 time = Fcar (Fcdr (Fcdr (Fcdr (Fcdr (event)))));
2869 CHECK_STRING (keyseq, 0);
2870 CHECK_WINDOW (window, 0);
2871
2872 /* Look up KEYSEQ in the buffer's local mouse map, then in global one. */
2873
2874 mouse_cmd = Qnil;
2875
2876 if (!NILP (XWINDOW (window)->buffer))
2877 {
2878 Lisp_Object local_map;
2879
2880 local_map = XBUFFER (XWINDOW (window)->buffer)->mouse_map;
2881 tem = Fkeymapp (local_map);
2882 if (!NILP (tem))
2883 mouse_cmd = Flookup_key (local_map, keyseq);
2884 /* A number as value means the key is too long; treat as undefined. */
2885 if (XTYPE (mouse_cmd) == Lisp_Int)
2886 mouse_cmd = Qnil;
2887 }
2888
2889 tem = Fkeymapp (Vglobal_mouse_map);
2890 if (NILP (mouse_cmd) && !NILP (tem))
2891 mouse_cmd = Flookup_key (Vglobal_mouse_map, keyseq);
2892 if (XTYPE (mouse_cmd) == Lisp_Int)
2893 mouse_cmd = Qnil;
2894
2895 if (NILP (mouse_cmd))
2896 {
2897 /* This button/shift combination is not defined.
2898 If it is a button-down event, ring the bell. */
2899#ifdef HAVE_X11
2900 if (XSTRING (keyseq)->data[XSTRING (keyseq)->size - 1] & 0x18 == 0)
2901#else
2902 if (XSTRING (keyseq)->data[XSTRING (keyseq)->size - 1] & 4 == 0)
2903#endif
2904 bitch_at_user ();
2905 }
2906 else
2907 {
2908 SCREEN_PTR s = XSCREEN (WINDOW_SCREEN (XWINDOW (window)));
2909
2910#ifndef HAVE_X11
2911 Vmouse_window = s->selected_window;
2912#endif /* HAVE_X11 */
2913 /* It's defined; call the definition. */
2914 Vprefix_arg = Qnil;
2915 if (!NILP (screen_part))
2916 {
2917 /* For a scroll-bar click, set the prefix arg
2918 to the number of lines down from the top the click was.
2919 Many scroll commands want to scroll by this many lines. */
2920 Lisp_Object position;
2921 Lisp_Object length;
2922 Lisp_Object offset;
2923
2924 position = Fcar (pos);
2925 length = Fcar (Fcdr (pos));
2926 offset = Fcar (Fcdr (Fcdr (pos)));
2927
2928 if (XINT (length) != 0)
2929 XSET (Vprefix_arg, Lisp_Int,
2930 (SCREEN_HEIGHT (s) * (XINT (position) + XINT (offset))
2931 / (XINT (length) + 2 * XINT (offset))));
2932 }
2933 Fcommand_execute (mouse_cmd, Qnil);
2934 }
2935
2936 if (!NILP (Vmouse_event_function)) /* Not `event' so no need for GCPRO */
2937 call1 (Vmouse_event_function, Vmouse_event);
2938 return Qnil;
2939}
2940#endif
2941\f
2942DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
2943 1, 1, "P",
2944 "Read function name, then read its arguments and call it.")
2945 (prefixarg)
2946 Lisp_Object prefixarg;
2947{
2948 Lisp_Object function;
2949 char buf[40];
2950 Lisp_Object saved_keys;
2951 struct gcpro gcpro1;
2952
2953 saved_keys = Fthis_command_keys ();
2954 buf[0] = 0;
2955 GCPRO1 (saved_keys);
2956
2957 if (EQ (prefixarg, Qminus))
2958 strcpy (buf, "- ");
2959 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
2960 strcpy (buf, "C-u ");
2961 else if (CONSP (prefixarg) && XTYPE (XCONS (prefixarg)->car) == Lisp_Int)
2962 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
2963 else if (XTYPE (prefixarg) == Lisp_Int)
2964 sprintf (buf, "%d ", XINT (prefixarg));
2965
2966 /* This isn't strictly correct if execute-extended-command
2967 is bound to anything else. Perhaps it should use
2968 this_command_keys? */
2969 strcat (buf, "M-x ");
2970
2971 /* Prompt with buf, and then read a string, completing from and
2972 restricting to the set of all defined commands. Don't provide
2973 any initial input. The last Qnil says not to perform a
2974 peculiar hack on the initial input. */
2975 function = Fcompleting_read (build_string (buf),
2976 Vobarray, Qcommandp,
2977 Qt, Qnil, Qnil);
2978
2979 /* Add the text read to this_command_keys. */
2980 {
2981 struct Lisp_String *func_str = XSTRING (function);
2982 int i;
2983 Lisp_Object tem;
2984
2985 for (i = 0; i < func_str->size; i++)
2986 {
2987 XSET (tem, Lisp_Int, func_str->data[i]);
2988 add_command_key (tem);
2989 }
2990 }
2991
2992 UNGCPRO;
2993
2994 function = Fintern (function, Vobarray);
2995 Vprefix_arg = prefixarg;
2996 this_command = function;
2997
2998 return Fcommand_execute (function, Qt);
2999}
3000\f
3001
3002detect_input_pending ()
3003{
3004 if (!input_pending)
3005 get_input_pending (&input_pending);
3006
3007 return input_pending;
3008}
3009
ffd56f97
JB
3010/* This is called in some cases before a possible quit.
3011 It cases the next call to detect_input_pending to recompute input_pending.
3012 So calling this function unnecessarily can't do any harm. */
3013clear_input_pending ()
3014{
3015 input_pending = 0;
3016}
3017
284f4730
JB
3018DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
3019 "T if command input is currently available with no waiting.\n\
3020Actually, the value is nil only if we can be sure that no input is available.")
3021 ()
3022{
3023 if (!NILP (unread_command_char))
3024 return (Qt);
3025
3026 return detect_input_pending () ? Qt : Qnil;
3027}
3028
3029DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
3030 "Return vector of last 100 chars read from terminal.")
3031 ()
3032{
3033 Lisp_Object val;
3034
3035 if (total_keys < NUM_RECENT_KEYS)
3036 return Fvector (total_keys, recent_keys);
3037 else
3038 {
3039 val = Fvector (NUM_RECENT_KEYS, recent_keys);
3040 bcopy (recent_keys + recent_keys_index,
3041 XVECTOR (val)->contents,
3042 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
3043 bcopy (recent_keys,
3044 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
3045 recent_keys_index * sizeof (Lisp_Object));
3046 return val;
3047 }
3048}
3049
3050DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
3051 "Return string of the keystrokes that invoked this command.")
3052 ()
3053{
3054 return make_array (this_command_key_count, this_command_keys);
3055}
3056
3057DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
3058 "Return the current depth in recursive edits.")
3059 ()
3060{
3061 Lisp_Object temp;
3062 XFASTINT (temp) = command_loop_level + minibuf_level;
3063 return temp;
3064}
3065
3066DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
3067 "FOpen dribble file: ",
3068 "Start writing all keyboard characters to FILE.")
3069 (file)
3070 Lisp_Object file;
3071{
3072 if (NILP (file))
3073 {
3074 fclose (dribble);
3075 dribble = 0;
3076 }
3077 else
3078 {
3079 file = Fexpand_file_name (file, Qnil);
3080 dribble = fopen (XSTRING (file)->data, "w");
3081 }
3082 return Qnil;
3083}
3084
3085DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
3086 "Discard the contents of the terminal input buffer.\n\
3087Also cancel any kbd macro being defined.")
3088 ()
3089{
3090 defining_kbd_macro = 0;
3091 update_mode_lines++;
3092
3093#if 0
3094 unread_command_char = make_number (-1);
3095#endif
3096 unread_command_char = Qnil;
3097
3098 discard_tty_input ();
3099
3100 kbd_fetch_ptr = kbd_store_ptr;
3101 input_pending = 0;
3102
3103 return Qnil;
3104}
3105\f
3106DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
3107 "Stop Emacs and return to superior process. You can resume later.\n\
3108On systems that don't have job control, run a subshell instead.\n\n\
3109If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
3110to be read as terminal input by Emacs's superior shell.\n\
3111Before suspending, if `suspend-hook' is bound and value is non-nil\n\
3112call the value as a function of no args. Don't suspend if it returns non-nil.\n\
3113Otherwise, suspend normally and after resumption call\n\
3114`suspend-resume-hook' if that is bound and non-nil.\n\
3115\n\
3116Some operating systems cannot stop the Emacs process and resume it later.\n\
3117On such systems, Emacs will start a subshell and wait for it to exit.")
3118 (stuffstring)
3119 Lisp_Object stuffstring;
3120{
3121 register Lisp_Object tem;
3122 int count = specpdl_ptr - specpdl;
3123 int old_height, old_width;
3124 int width, height;
3125 struct gcpro gcpro1;
3126 extern init_sys_modes ();
3127
3128 if (!NILP (stuffstring))
3129 CHECK_STRING (stuffstring, 0);
3130 GCPRO1 (stuffstring);
3131
3132 /* Call value of suspend-hook
3133 if it is bound and value is non-nil. */
3134 if (!NILP (Vrun_hooks))
3135 {
3136 tem = call1 (Vrun_hooks, intern ("suspend-hook"));
3137 if (!EQ (tem, Qnil)) return Qnil;
3138 }
3139
3140 get_screen_size (&old_width, &old_height);
3141 reset_sys_modes ();
3142 /* sys_suspend can get an error if it tries to fork a subshell
3143 and the system resources aren't available for that. */
3144 record_unwind_protect (init_sys_modes, 0);
3145 stuff_buffered_input (stuffstring);
3146 sys_suspend ();
3147 unbind_to (count, Qnil);
3148
3149 /* Check if terminal/window size has changed.
3150 Note that this is not useful when we are running directly
3151 with a window system; but suspend should be disabled in that case. */
3152 get_screen_size (&width, &height);
3153 if (width != old_width || height != old_height)
3154 change_screen_size (height, width, 0);
3155
3156 /* Call value of suspend-resume-hook
3157 if it is bound and value is non-nil. */
3158 if (!NILP (Vrun_hooks))
3159 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
3160
3161 UNGCPRO;
3162 return Qnil;
3163}
3164
3165/* If STUFFSTRING is a string, stuff its contents as pending terminal input.
3166 Then in any case stuff anthing Emacs has read ahead and not used. */
3167
3168stuff_buffered_input (stuffstring)
3169 Lisp_Object stuffstring;
3170{
3171 register unsigned char *p;
3172
3173/* stuff_char works only in BSD, versions 4.2 and up. */
3174#ifdef BSD
3175#ifndef BSD4_1
3176 if (XTYPE (stuffstring) == Lisp_String)
3177 {
3178 register int count;
3179
3180 p = XSTRING (stuffstring)->data;
3181 count = XSTRING (stuffstring)->size;
3182 while (count-- > 0)
3183 stuff_char (*p++);
3184 stuff_char ('\n');
3185 }
3186 /* Anything we have read ahead, put back for the shell to read. */
3187 while (kbd_fetch_ptr != kbd_store_ptr)
3188 {
3189 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
3190 kbd_fetch_ptr = kbd_buffer;
3191 if (kbd_fetch_ptr->kind == ascii_keystroke)
3192 stuff_char (XINT (kbd_fetch_ptr->code));
3193 kbd_fetch_ptr++;
3194 }
3195 input_pending = 0;
3196#endif
3197#endif /* BSD and not BSD4_1 */
3198}
3199\f
ffd56f97
JB
3200set_waiting_for_input (time_to_clear)
3201 EMACS_TIME *time_to_clear;
284f4730 3202{
ffd56f97 3203 input_available_clear_time = time_to_clear;
284f4730
JB
3204
3205 /* Tell interrupt_signal to throw back to read_char, */
3206 waiting_for_input = 1;
3207
3208 /* If interrupt_signal was called before and buffered a C-g,
3209 make it run again now, to avoid timing error. */
3210 if (!NILP (Vquit_flag))
3211 quit_throw_to_read_char ();
3212
3213 /* If alarm has gone off already, echo now. */
3214 if (echo_flag)
3215 {
3216 echo ();
3217 echo_flag = 0;
3218 }
3219}
3220
3221clear_waiting_for_input ()
3222{
3223 /* Tell interrupt_signal not to throw back to read_char, */
3224 waiting_for_input = 0;
ffd56f97 3225 input_available_clear_time = 0;
284f4730
JB
3226}
3227
3228/* This routine is called at interrupt level in response to C-G.
3229 If interrupt_input, this is the handler for SIGINT.
3230 Otherwise, it is called from kbd_buffer_store_event,
3231 in handling SIGIO or SIGTINT.
3232
3233 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
3234 immediately throw back to read_char.
3235
3236 Otherwise it sets the Lisp variable quit-flag not-nil.
3237 This causes eval to throw, when it gets a chance.
3238 If quit-flag is already non-nil, it stops the job right away. */
3239
3240SIGTYPE
3241interrupt_signal ()
3242{
3243 char c;
3244 /* Must preserve main program's value of errno. */
3245 int old_errno = errno;
3246 extern Lisp_Object Vwindow_system;
3247
3248#ifdef USG
3249 /* USG systems forget handlers when they are used;
3250 must reestablish each time */
3251 signal (SIGINT, interrupt_signal);
3252 signal (SIGQUIT, interrupt_signal);
3253#endif /* USG */
3254
3255 cancel_echoing ();
3256
3257 if (!NILP (Vquit_flag) && SCREEN_IS_TERMCAP (selected_screen))
3258 {
3259 fflush (stdout);
3260 reset_sys_modes ();
3261 sigfree ();
3262#ifdef SIGTSTP /* Support possible in later USG versions */
3263/*
3264 * On systems which can suspend the current process and return to the original
3265 * shell, this command causes the user to end up back at the shell.
3266 * The "Auto-save" and "Abort" questions are not asked until
3267 * the user elects to return to emacs, at which point he can save the current
3268 * job and either dump core or continue.
3269 */
3270 sys_suspend ();
3271#else
3272#ifdef VMS
3273 if (sys_suspend () == -1)
3274 {
3275 printf ("Not running as a subprocess;\n");
3276 printf ("you can continue or abort.\n");
3277 }
3278#else /* not VMS */
3279 /* Perhaps should really fork an inferior shell?
3280 But that would not provide any way to get back
3281 to the original shell, ever. */
3282 printf ("No support for stopping a process on this operating system;\n");
3283 printf ("you can continue or abort.\n");
3284#endif /* not VMS */
3285#endif /* not SIGTSTP */
3286 printf ("Auto-save? (y or n) ");
3287 fflush (stdout);
3288 if (((c = getchar ()) & ~040) == 'Y')
3289 Fdo_auto_save (Qnil, Qnil);
3290 while (c != '\n') c = getchar ();
3291#ifdef VMS
3292 printf ("Abort (and enter debugger)? (y or n) ");
3293#else /* not VMS */
3294 printf ("Abort (and dump core)? (y or n) ");
3295#endif /* not VMS */
3296 fflush (stdout);
3297 if (((c = getchar ()) & ~040) == 'Y')
3298 abort ();
3299 while (c != '\n') c = getchar ();
3300 printf ("Continuing...\n");
3301 fflush (stdout);
3302 init_sys_modes ();
3303 }
3304 else
3305 {
3306 /* If executing a function that wants to be interrupted out of
3307 and the user has not deferred quitting by binding `inhibit-quit'
3308 then quit right away. */
3309 if (immediate_quit && NILP (Vinhibit_quit))
3310 {
3311 immediate_quit = 0;
3312 sigfree ();
3313 Fsignal (Qquit, Qnil);
3314 }
3315 else
3316 /* Else request quit when it's safe */
3317 Vquit_flag = Qt;
3318 }
3319
3320 if (waiting_for_input && !echoing)
3321 quit_throw_to_read_char ();
3322
3323 errno = old_errno;
3324}
3325
3326/* Handle a C-g by making read_char return C-g. */
3327
3328quit_throw_to_read_char ()
3329{
3330 quit_error_check ();
3331 sigfree ();
3332 /* Prevent another signal from doing this before we finish. */
3333 waiting_for_input = 0;
3334 input_pending = 0;
3335
3336#if 0
3337 unread_command_char = make_number (-1);
3338#endif
3339 unread_command_char = Qnil;
3340
3341 _longjmp (getcjmp, 1);
3342}
3343\f
3344DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
3345 "Set mode of reading keyboard input.\n\
3346First arg non-nil means use input interrupts; nil means use CBREAK mode.\n\
3347Second arg non-nil means use ^S/^Q flow control for output to terminal\n\
3348 (no effect except in CBREAK mode).\n\
3349Third arg non-nil means accept 8-bit input (for a Meta key).\n\
3350 Otherwise, the top bit is ignored, on the assumption it is parity.\n\
3351Optional fourth arg non-nil specifies character to use for quitting.")
3352 (interrupt, flow, meta, quit)
3353 Lisp_Object interrupt, flow, meta, quit;
3354{
3355 if (!NILP (quit)
3356 && (XTYPE (quit) != Lisp_Int
3357 || XINT (quit) < 0 || XINT (quit) > 0400))
3358 error ("set-input-mode: QUIT must be an ASCII character.");
3359
3360 reset_sys_modes ();
3361#ifdef SIGIO
3362/* Note SIGIO has been undef'd if FIONREAD is missing. */
3363#ifdef NO_SOCK_SIGIO
3364 if (read_socket_hook)
3365 interrupt_input = 0; /* No interrupts if reading from a socket. */
3366 else
3367#endif /* NO_SOCK_SIGIO */
3368 interrupt_input = !NILP (interrupt);
3369#else /* not SIGIO */
3370 interrupt_input = 0;
3371#endif /* not SIGIO */
3372/* Our VMS input only works by interrupts, as of now. */
3373#ifdef VMS
3374 interrupt_input = 1;
3375#endif
3376 flow_control = !NILP (flow);
3377 meta_key = !NILP (meta);
3378 if (!NILP (quit))
3379 /* Don't let this value be out of range. */
3380 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
3381
3382 init_sys_modes ();
3383 return Qnil;
3384}
3385\f
3386init_keyboard ()
3387{
3388 this_command_keys_size = 40;
3389 this_command_keys =
3390 (Lisp_Object *) xmalloc (this_command_keys_size * sizeof (Lisp_Object));
3391
3392 /* This is correct before outermost invocation of the editor loop */
3393 command_loop_level = -1;
3394 immediate_quit = 0;
3395 quit_char = Ctl ('g');
3396 unread_command_char = Qnil;
3397 recent_keys_index = 0;
3398 total_keys = 0;
3399 kbd_fetch_ptr = kbd_buffer;
3400 kbd_store_ptr = kbd_buffer;
3401 do_mouse_tracking = 0;
3402 input_pending = 0;
3403
3404 if (!noninteractive)
3405 {
3406 signal (SIGINT, interrupt_signal);
3407#ifdef HAVE_TERMIO
3408 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
3409 SIGQUIT and we can't tell which one it will give us. */
3410 signal (SIGQUIT, interrupt_signal);
3411#endif /* HAVE_TERMIO */
3412/* Note SIGIO has been undef'd if FIONREAD is missing. */
3413#ifdef SIGIO
3414 signal (SIGIO, input_available_signal);
3415#endif SIGIO
3416 }
3417
3418/* Use interrupt input by default, if it works and noninterrupt input
3419 has deficiencies. */
3420
3421#ifdef INTERRUPT_INPUT
3422 interrupt_input = 1;
3423#else
3424 interrupt_input = 0;
3425#endif
3426
3427/* Our VMS input only works by interrupts, as of now. */
3428#ifdef VMS
3429 interrupt_input = 1;
3430#endif
3431
3432 sigfree ();
3433 dribble = 0;
3434
3435 if (keyboard_init_hook)
3436 (*keyboard_init_hook) ();
3437
3438#ifdef POLL_FOR_INPUT
3439 poll_suppress_count = 1;
3440 start_polling ();
3441#endif
3442}
3443
3444/* This type's only use is in syms_of_keyboard, to initialize the
3445 event header symbols and put properties on them. */
3446struct event_head {
3447 Lisp_Object *var;
3448 char *name;
3449 Lisp_Object *kind;
3450};
3451
3452struct event_head head_table[] = {
3453 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
3454 &Qvscrollbar_part, "vscrollbar-part", &Qscrollbar_click,
3455 &Qvslider_part, "vslider-part", &Qscrollbar_click,
3456 &Qvthumbup_part, "vthumbup-part", &Qscrollbar_click,
3457 &Qvthumbdown_part, "vthumbdown-part", &Qscrollbar_click,
3458 &Qhscrollbar_part, "hscrollbar-part", &Qscrollbar_click,
3459 &Qhslider_part, "hslider-part", &Qscrollbar_click,
3460 &Qhthumbleft_part, "hthumbleft-part", &Qscrollbar_click,
3461 &Qhthumbright_part,"hthumbright-part", &Qscrollbar_click
3462};
3463
3464syms_of_keyboard ()
3465{
3466 Qself_insert_command = intern ("self-insert-command");
3467 staticpro (&Qself_insert_command);
3468
3469 Qforward_char = intern ("forward-char");
3470 staticpro (&Qforward_char);
3471
3472 Qbackward_char = intern ("backward-char");
3473 staticpro (&Qbackward_char);
3474
3475 Qdisabled = intern ("disabled");
3476 staticpro (&Qdisabled);
3477
3478 Qfunction_key = intern ("function-key");
3479 staticpro (&Qfunction_key);
3480 Qmouse_movement = intern ("mouse-click");
3481 staticpro (&Qmouse_click);
3482 Qmouse_movement = intern ("scrollbar-click");
3483 staticpro (&Qmouse_movement);
3484
3485 Qmode_line = intern ("mode-line");
3486 staticpro (&Qmode_line);
3487 Qvertical_split = intern ("vertical-split");
3488 staticpro (&Qvertical_split);
3489
3490 Qevent_kind = intern ("event-type");
3491 staticpro (&Qevent_kind);
3492 Qevent_unmodified = intern ("event-unmodified");
3493 staticpro (&Qevent_unmodified);
3494
3495 {
3496 struct event_head *p;
3497
3498 for (p = head_table;
3499 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
3500 p++)
3501 {
3502 *p->var = intern (p->name);
3503 staticpro (p->var);
3504 Fput (*p->var, Qevent_kind, *p->kind);
3505 Fput (*p->var, Qevent_unmodified, *p->var);
3506 }
3507 }
3508
3509 func_key_syms = Qnil;
3510 staticpro (&func_key_syms);
3511
3512 mouse_syms = Qnil;
3513 staticpro (&mouse_syms);
3514
3515 defsubr (&Sread_key_sequence);
3516 defsubr (&Srecursive_edit);
3517 defsubr (&Strack_mouse);
3518 defsubr (&Smouse_click_p);
3519 defsubr (&Sinput_pending_p);
3520 defsubr (&Scommand_execute);
3521 defsubr (&Srecent_keys);
3522 defsubr (&Sthis_command_keys);
3523 defsubr (&Ssuspend_emacs);
3524 defsubr (&Sabort_recursive_edit);
3525 defsubr (&Sexit_recursive_edit);
3526 defsubr (&Srecursion_depth);
3527 defsubr (&Stop_level);
3528 defsubr (&Sdiscard_input);
3529 defsubr (&Sopen_dribble_file);
3530 defsubr (&Sset_input_mode);
3531 defsubr (&Sexecute_extended_command);
3532
3533 DEFVAR_LISP ("disabled-command-hook", &Vdisabled_command_hook,
3534 "Value is called instead of any command that is disabled\n\
3535(has a non-nil `disabled' property).");
3536
3537 DEFVAR_LISP ("last-command-char", &last_command_char,
3538 "Last terminal input key that was part of a command.");
3539
3540 DEFVAR_LISP ("last-input-char", &last_input_char,
3541 "Last terminal input key.");
3542
3543 DEFVAR_LISP ("unread-command-char", &unread_command_char,
3544 "Object to be read as next input from input stream, or nil if none.");
3545
3546 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
3547 "Meta-prefix character code. Meta-foo as command input\n\
3548turns into this character followed by foo.");
3549 XSET (meta_prefix_char, Lisp_Int, 033);
3550
3551 DEFVAR_LISP ("last-command", &last_command,
3552 "The last command executed. Normally a symbol with a function definition,\n\
3553but can be whatever was found in the keymap, or whatever the variable\n\
3554`this-command' was set to by that command.");
3555 last_command = Qnil;
3556
3557 DEFVAR_LISP ("this-command", &this_command,
3558 "The command now being executed.\n\
3559The command can set this variable; whatever is put here\n\
3560will be in `last-command' during the following command.");
3561 this_command = Qnil;
3562
3563 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
3564 "*Number of keyboard input characters between auto-saves.\n\
3565Zero means disable autosaving due to number of characters typed.");
3566 auto_save_interval = 300;
3567
3568 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
3569 "*Number of seconds idle time before auto-save.\n\
3570Zero or nil means disable auto-saving due to idleness.");
3571 XFASTINT (Vauto_save_timeout) = 30;
3572
3573 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
3574 "*Nonzero means echo unfinished commands after this many seconds of pause.");
3575 echo_keystrokes = 1;
3576
3577 DEFVAR_INT ("polling-period", &polling_period,
3578 "*Interval between polling for input during Lisp execution.\n\
3579The reason for polling is to make C-g work to stop a running program.\n\
3580Polling is needed only when using X windows and SIGIO does not work.\n\
3581Polling is automatically disabled in all other cases.");
3582 polling_period = 2;
3583
3584 DEFVAR_INT ("num-input-keys", &num_input_keys,
3585 "*Number of complete keys read from the keyboard so far.");
3586 num_input_keys = 0;
3587
3588 DEFVAR_LISP ("last-event-screen", &Vlast_event_screen,
3589 "*The screen in which the most recently read event occurred.");
3590 Vlast_event_screen = Qnil;
3591
3592 DEFVAR_LISP ("help-char", &help_char,
3593 "Character to recognize as meaning Help.\n\
3594When it is read, do `(eval help-form)', and display result if it's a string.\n\
3595If the value of `help-form' is nil, this char can be read normally.");
3596 XSET (help_char, Lisp_Int, Ctl ('H'));
3597
3598 DEFVAR_LISP ("help-form", &Vhelp_form,
3599 "Form to execute when character help-char is read.\n\
3600If the form returns a string, that string is displayed.\n\
3601If `help-form' is nil, the help char is not recognized.");
3602 Vhelp_form = Qnil;
3603
3604 DEFVAR_LISP ("top-level", &Vtop_level,
3605 "Form to evaluate when Emacs starts up.\n\
3606Useful to set before you dump a modified Emacs.");
3607 Vtop_level = Qnil;
3608
3609 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
3610 "String used as translate table for keyboard input, or nil.\n\
3611Each character is looked up in this string and the contents used instead.\n\
3612If string is of length N, character codes N and up are untranslated.");
3613 Vkeyboard_translate_table = Qnil;
3614
3615#ifdef HAVE_X_WINDOWS
3616 DEFVAR_LISP ("mouse-event-function", &Vmouse_event_function,
3617 "Function to call for each mouse event, after the event's definition.\n\
3618Called, if non-nil, with one argument, which is the event-list.\n\
3619See the variable `mouse-event' for the format of this list.");
3620 Vmouse_event_function = Qnil;
3621
3622 DEFVAR_LISP ("mouse-left-hook", &Vmouse_left_hook,
3623 "Function to call when mouse leaves window. No arguments.");
3624 Vmouse_left_hook = Qnil;
3625
3626 DEFVAR_LISP ("map-screen-hook", &Vmap_screen_hook,
3627 "Function to call when screen is mapped. No arguments.");
3628 Vmap_screen_hook = Qnil;
3629
3630 DEFVAR_LISP ("unmap-screen-hook", &Vunmap_screen_hook,
3631 "Function to call when screen is unmapped. No arguments.");
3632 Vunmap_screen_hook = Qnil;
3633
3634 DEFVAR_LISP ("mouse-motion-handler", &Vmouse_motion_handler,
3635 "Handler for motion events. No arguments.");
3636 Vmouse_motion_handler = Qnil;
3637#endif
3638
3639 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
3640 "Non-nil means prompt with menus in echo area when appropriate.\n\
3641This is done when reading from a keymap that has a prompt string,\n\
3642for elements that have prompt strings.");
3643 menu_prompting = 1;
3644
3645 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
3646 "Character to see next line of menu prompt.\n\
3647Type this character while in a menu prompt to rotate around the lines of it.");
3648 XSET (menu_prompt_more_char, Lisp_Int, ' ');
3649}
3650
3651keys_of_keyboard ()
3652{
3653 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
3654 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
3655 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
3656 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
3657 initial_define_key (meta_map, 'x', "execute-extended-command");
3658}