entered into RCS
[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
06ef7355 158/* Number of idle seconds before an auto-save and garbage collection. */
284f4730
JB
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. */
f76475ad 1151 save_getcjmp (save_jump);
284f4730
JB
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
284f4730
JB
1165 goto non_reread;
1166 }
1167
1168 /* Message turns off echoing unless more keystrokes turn it on again. */
1169 if (echo_area_glyphs && *echo_area_glyphs && echo_area_glyphs != echobuf)
1170 cancel_echoing ();
1171 else
1172 /* If already echoing, continue. */
1173 echo_dash ();
1174
1175 /* If in middle of key sequence and minibuffer not active,
1176 start echoing if enough time elapses. */
1177 if (minibuf_level == 0 && !immediate_echo && this_command_key_count > 0
1178 && echo_keystrokes > 0
1179 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0))
1180 {
1181 Lisp_Object tem0;
1182
f76475ad 1183 tem0 = sit_for (echo_keystrokes, 0, 1, 1);
284f4730
JB
1184 if (EQ (tem0, Qt))
1185 echo ();
1186 }
1187
1188 /* Maybe auto save due to number of keystrokes or idle time. */
1189
1190 if (commandflag != 0
1191 && auto_save_interval > 0
1192 && num_input_chars - last_auto_save > max (auto_save_interval, 20)
1193 && !detect_input_pending ())
1194 {
1195 jmp_buf temp;
1196 save_getcjmp (temp);
1197 Fdo_auto_save (Qnil, Qnil);
1198 restore_getcjmp (temp);
1199 }
1200
1201 /* Slow down auto saves logarithmically in size of current buffer,
1202 and garbage collect while we're at it. */
1203 {
1204 int delay_level, buffer_size;
1205
1206 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
1207 last_non_minibuf_size = Z - BEG;
1208 buffer_size = (last_non_minibuf_size >> 8) + 1;
1209 delay_level = 0;
1210 while (buffer_size > 64)
1211 delay_level++, buffer_size -= buffer_size >> 2;
1212 if (delay_level < 4) delay_level = 4;
1213 /* delay_level is 4 for files under around 50k, 7 at 100k,
1214 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
1215
1216 /* Auto save if enough time goes by without input. */
1217 if (commandflag != 0
1218 && num_input_chars > last_auto_save
1219 && XTYPE (Vauto_save_timeout) == Lisp_Int
1220 && XINT (Vauto_save_timeout) > 0)
1221 {
1222 Lisp_Object tem0;
1223 int delay = delay_level * XFASTINT (Vauto_save_timeout) / 4;
f76475ad 1224 tem0 = sit_for (delay, 0, 1, 1);
284f4730
JB
1225 if (EQ (tem0, Qt))
1226 {
1227 jmp_buf temp;
1228 save_getcjmp (temp);
1229 Fdo_auto_save (Qnil, Qnil);
1230 restore_getcjmp (temp);
1231
1232 /* If we have auto-saved and there is still no input
1233 available, garbage collect if there has been enough
1234 consing going on to make it worthwhile. */
1235 if (!detect_input_pending ()
1236 && consing_since_gc > gc_cons_threshold / 2)
1237 Fgarbage_collect ();
1238 }
1239 }
1240 }
1241
1242 /* Actually read a character, waiting if necessary. */
1243 c = kbd_buffer_get_event ();
1244
1245 if (NILP (c))
1246 abort (); /* Don't think this can happen. */
1247
1248#if 0 /* I think that all the different kinds of events should be
1249 handled together now... */
1250 if (XTYPE (c) != Lisp_Int)
1251 {
1252 start_polling ();
1253 return c;
1254 }
1255 c = XINT (obj);
1256#endif
1257
1258 /* Terminate Emacs in batch mode if at eof. */
1259 if (noninteractive && c < 0)
1260 Fkill_emacs (make_number (1));
1261
1262 non_reread:
1263
f76475ad 1264 restore_getcjmp (save_jump);
284f4730
JB
1265
1266 start_polling ();
1267
1268 echo_area_glyphs = 0;
1269
1270 /* Handle things that only apply to characters. */
1271 if (XTYPE (c) == Lisp_Int)
1272 {
1273 /* If kbd_buffer_get_event gave us an EOF, return that. */
1274 if (XINT (c) < 0)
1275 return c;
1276
1277 /* Strip the high bits, and maybe the meta bit too. */
1278 XSETINT (c, c & (meta_key ? 0377 : 0177));
1279
1280 if (XTYPE (Vkeyboard_translate_table) == Lisp_String
1281 && XSTRING (Vkeyboard_translate_table)->size > XINT (c))
1282 XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[c]);
1283 }
1284
1285 total_keys++;
1286 recent_keys[recent_keys_index] = c;
1287 if (++recent_keys_index >= (sizeof (recent_keys)/sizeof(recent_keys[0])))
1288 recent_keys_index = 0;
1289
1290 /* Write c to the dribble file. If c is a lispy event, write
1291 the event's symbol to the dribble file, in <brackets>. Bleaugh.
1292 If you, dear reader, have a better idea, you've got the source. :-) */
1293 if (dribble)
1294 {
1295 if (XTYPE (c) == Lisp_Int)
1296 putc (c, dribble);
1297 else
1298 {
1299 Lisp_Object dribblee = c;
1300
1301 /* If it's a structured event, take the event header. */
1302 if (EVENT_HAS_PARAMETERS (dribblee))
1303 dribblee = EVENT_HEAD (dribblee);
1304
1305 if (XTYPE (c) == Lisp_Symbol)
1306 {
1307 putc ('<', dribble);
1308 fwrite (XSYMBOL (c)->name->data, sizeof (char),
1309 XSYMBOL (c)->name->size,
1310 dribble);
1311 putc ('>', dribble);
1312 }
1313 }
1314
1315 fflush (dribble);
1316 }
1317
1318 store_kbd_macro_char (c);
1319
1320 from_macro:
1321 reread_first:
1322 echo_char (c);
1323
1324 /* Record this character as part of the current key. */
1325 add_command_key (c);
1326
1327 /* Re-reading in the middle of a command */
1328 reread:
1329 last_input_char = c;
1330 num_input_chars++;
1331
1332 /* Process the help character specially if enabled */
1333 if (EQ (c, help_char) && !NILP (Vhelp_form))
1334 {
1335 Lisp_Object tem0;
1336 count = specpdl_ptr - specpdl;
1337
1338 record_unwind_protect (Fset_window_configuration,
1339 Fcurrent_window_configuration (Qnil));
1340
1341 tem0 = Feval (Vhelp_form);
1342 if (XTYPE (tem0) == Lisp_String)
1343 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
1344
1345 cancel_echoing ();
1346 c = read_char (0);
1347 /* Remove the help from the screen */
1348 unbind_to (count, Qnil);
1349 redisplay ();
1350 if (EQ (c, make_number (040)))
1351 {
1352 cancel_echoing ();
1353 c = read_char (0);
1354 }
1355 }
1356
1357 return c;
1358}
1359
1360Lisp_Object
1361print_help (object)
1362 Lisp_Object object;
1363{
1364 Fprinc (object, Qnil);
1365 return Qnil;
1366}
1367
1368/* Copy out or in the info on where C-g should throw to.
1369 This is used when running Lisp code from within get_char,
1370 in case get_char is called recursively.
1371 See read_process_output. */
1372
1373save_getcjmp (temp)
1374 jmp_buf temp;
1375{
1376 bcopy (getcjmp, temp, sizeof getcjmp);
1377}
1378
1379restore_getcjmp (temp)
1380 jmp_buf temp;
1381{
1382 bcopy (temp, getcjmp, sizeof getcjmp);
1383}
1384
1385\f
1386/* Low level keyboard/mouse input.
1387 kbd_buffer_store_event places events in kbd_buffer, and
1388 kbd_buffer_get_event retrieves them.
1389 mouse_moved indicates when the mouse has moved again, and
1390 *mouse_position_hook provides the mouse position. */
1391
1392/* Set this for debugging, to have a way to get out */
1393int stop_character;
1394
1395extern int screen_garbaged;
1396
1397/* Return true iff there are any events in the queue that read-char
1398 would return. If this returns false, a read-char would block. */
1399static int
1400readable_events ()
1401{
1402 struct input_event *ep;
1403
1404 if (EVENT_QUEUES_EMPTY)
1405 return 0;
1406
1407 if (do_mouse_tracking)
1408 return 1;
1409
1410 /* Mouse tracking is disabled, so we need to actually scan the
1411 input queue to see if any events are currently readable. */
1412 for (ep = kbd_fetch_ptr; ep != kbd_store_ptr; ep++)
1413 {
1414 if (ep == kbd_buffer + KBD_BUFFER_SIZE)
1415 ep = kbd_buffer;
1416
1417 /* Skip button-up events. */
1418 if ((ep->kind == mouse_click || ep->kind == scrollbar_click)
1419 && (ep->modifiers & up_modifier))
1420 continue;
1421
1422 return 1;
1423 }
1424
1425 return 0;
1426}
1427
1428
1429/* Restore mouse tracking enablement. See Ftrack_mouse for the only use
1430 of this function. */
1431static Lisp_Object
1432tracking_off (old_value)
1433 Lisp_Object old_value;
1434{
1435 if (! XFASTINT (old_value))
1436 {
1437 do_mouse_tracking = 0;
1438
1439 /* Redisplay may have been preempted because there was input
1440 available, and it assumes it will be called again after the
1441 input has been processed. If the only input available was
1442 the sort that we have just disabled, then we need to call
1443 redisplay. */
1444 if (!readable_events ())
1445 {
1446 redisplay_preserve_echo_area ();
1447 get_input_pending (&input_pending);
1448 }
1449 }
1450}
1451
1452DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
1453 "Evaluate BODY with mouse movement and button release events enabled.\n\
3371bdfc
RS
1454Within a `track-mouse', mouse motion and button releases generate input\n\
1455events that you can read with `read-event'.\n\
1456Normally, these occurrences don't generate events.")
284f4730
JB
1457 (args)
1458 Lisp_Object args;
1459{
1460 int count = specpdl_ptr - specpdl;
1461 Lisp_Object val;
1462
1463 XSET (val, Lisp_Int, do_mouse_tracking);
1464 record_unwind_protect (tracking_off, val);
1465
1466 do_mouse_tracking = 1;
1467
1468 val = Fprogn (args);
1469 return unbind_to (count, val);
1470}
1471
1472/* Store an event obtained at interrupt level into kbd_buffer, fifo */
1473
1474void
1475kbd_buffer_store_event (event)
1476 register struct input_event *event;
1477{
1478 if (event->kind == no_event)
1479 abort ();
1480
1481 if (event->kind == ascii_keystroke)
1482 {
1483 register int c = XFASTINT (event->code) & 0377;
1484
1485 if (c == quit_char
1486 || ((c == (0200 | quit_char)) && !meta_key))
1487 {
1488 /* If this results in a quit_char being returned to Emacs as
1489 input, set last-event-screen properly. If this doesn't
1490 get returned to Emacs as an event, the next event read
1491 will set Vlast_event_screen again, so this is safe to do. */
1492 extern SIGTYPE interrupt_signal ();
1493 XSET (Vlast_event_screen, Lisp_Screen, event->screen);
ffd56f97 1494 last_event_timestamp = event->timestamp;
284f4730
JB
1495 interrupt_signal ();
1496 return;
1497 }
1498
1499 if (c && c == stop_character)
1500 {
1501 sys_suspend ();
1502 return;
1503 }
1504
1505 XSET (event->code, Lisp_Int, c);
1506 }
1507
1508 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
1509 kbd_store_ptr = kbd_buffer;
1510
1511 /* Don't let the very last slot in the buffer become full,
1512 since that would make the two pointers equal,
1513 and that is indistinguishable from an empty buffer.
1514 Discard the event if it would fill the last slot. */
1515 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
1516 {
1517 kbd_store_ptr->kind = event->kind;
1518 kbd_store_ptr->code = event->code;
1519 kbd_store_ptr->part = event->part;
1520 kbd_store_ptr->screen = event->screen;
1521 kbd_store_ptr->modifiers = event->modifiers;
1522 kbd_store_ptr->x = event->x;
1523 kbd_store_ptr->y = event->y;
1524 kbd_store_ptr->timestamp = event->timestamp;
1525
1526 kbd_store_ptr++;
1527 }
1528}
1529
1530static Lisp_Object make_lispy_event ();
1531static Lisp_Object make_lispy_movement ();
1532static Lisp_Object modify_event_symbol ();
1533
1534static Lisp_Object
1535kbd_buffer_get_event ()
1536{
1537 register int c;
1538 Lisp_Object obj;
1539
1540 if (noninteractive)
1541 {
1542 c = getchar ();
1543 XSET (obj, Lisp_Int, c);
1544 return obj;
1545 }
1546
1547 /* Wait until there is input available. */
1548 for (;;)
1549 {
1550
1551 /* Process or toss any events that we don't want to return as
1552 input. The fact that we remove undesirable events here
1553 allows us to use EVENT_QUEUES_EMPTY in the rest of this loop. */
1554 if (! do_mouse_tracking)
1555 while (kbd_fetch_ptr != kbd_store_ptr)
1556 {
1557 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
1558 kbd_fetch_ptr = kbd_buffer;
1559
1560 if (kbd_fetch_ptr->kind == mouse_click
1561 || kbd_fetch_ptr->kind == scrollbar_click)
1562 {
1563 if ((kbd_fetch_ptr->modifiers & up_modifier) == 0)
1564 break;
1565 }
1566 else
1567 break;
1568
1569 kbd_fetch_ptr++;
1570 }
1571
1572 if (!EVENT_QUEUES_EMPTY)
1573 break;
1574
1575 /* If the quit flag is set, then read_char will return
1576 quit_char, so that counts as "available input." */
1577 if (!NILP (Vquit_flag))
1578 quit_throw_to_read_char ();
1579
1580 /* One way or another, wait until input is available; then, if
1581 interrupt handlers have not read it, read it now. */
1582
1583#ifdef OLDVMS
1584 wait_for_kbd_input ();
1585#else
1586/* Note SIGIO has been undef'd if FIONREAD is missing. */
1587#ifdef SIGIO
1588 gobble_input (0);
1589#endif /* SIGIO */
1590 if (EVENT_QUEUES_EMPTY)
1591 {
f76475ad
JB
1592 Lisp_Object minus_one;
1593
1594 XSET (minus_one, Lisp_Int, -1);
1595 wait_reading_process_input (0, 0, minus_one, 1);
284f4730
JB
1596
1597 if (!interrupt_input && EVENT_QUEUES_EMPTY)
1598 {
1599 read_avail_input (0);
1600 }
1601 }
1602#endif /* not VMS */
1603 }
1604
1605 /* At this point, we know that there is a readable event available
1606 somewhere. If the event queue is empty, then there must be a
1607 mouse movement enabled and available. */
1608 if (kbd_fetch_ptr != kbd_store_ptr)
1609 {
1610 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
1611 kbd_fetch_ptr = kbd_buffer;
1612 XSET (Vlast_event_screen, Lisp_Screen, kbd_fetch_ptr->screen);
1613 last_event_timestamp = XINT (kbd_fetch_ptr->timestamp);
1614 obj = make_lispy_event (kbd_fetch_ptr);
1615 kbd_fetch_ptr->kind = no_event;
1616 kbd_fetch_ptr++;
1617 if (XTYPE (obj) == Lisp_Int)
1618 XSET (obj, Lisp_Int, XINT (obj) & (meta_key ? 0377 : 0177));
1619 }
1620 else if (do_mouse_tracking && mouse_moved)
1621 {
1622 SCREEN_PTR screen;
1623 Lisp_Object x, y, time;
1624
1625 (*mouse_position_hook) (&screen, &x, &y, &time);
1626 XSET (Vlast_event_screen, Lisp_Screen, screen);
1627
1628 obj = make_lispy_movement (screen, x, y, time);
1629 }
1630 else
1631 /* We were promised by the above while loop that there was
1632 something for us to read! */
1633 abort ();
1634
1635 input_pending = readable_events ();
1636
1637 return (obj);
1638}
1639
1640/* Caches for modify_event_symbol. */
1641static Lisp_Object func_key_syms;
1642static Lisp_Object mouse_syms;
1643
1644/* You'll notice that this table is arranged to be conveniently
1645 indexed by X Windows keysym values. */
1646static char *lispy_function_keys[] =
1647 {
1648 /* X Keysym value */
1649
1650 "home", /* 0xff50 */ /* IsCursorKey */
1651 "left",
1652 "up",
1653 "right",
1654 "down",
1655 "prior",
1656 "next",
1657 "end",
1658 "begin",
1659 0, /* 0xff59 */
1660 0, 0, 0, 0, 0, 0,
1661 "select", /* 0xff60 */ /* IsMiscFunctionKey */
1662 "print",
1663 "execute",
1664 "insert",
1665 0, /* 0xff64 */
1666 "undo",
1667 "redo",
1668 "menu",
1669 "find",
1670 "cancel",
1671 "help",
1672 "break", /* 0xff6b */
1673
1674 /* Here are some keys found mostly on HP keyboards. The X event
1675 handling code will strip bit 29, which flags vendor-specific
1676 keysyms. */
1677 "reset", /* 0x1000ff6c */
1678 "system",
1679 "user",
1680 "clearline",
1681 "insertline",
1682 "deleteline",
1683 "insertchar",
1684 "deletechar",
1685 "backtab",
1686 "kp_backtab", /* 0x1000ff75 */
1687 0, /* 0xff76 */
1688 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff7f */
1689 "kp-space", /* 0xff80 */ /* IsKeypadKey */
1690 0, 0, 0, 0, 0, 0, 0, 0,
1691 "kp-tab", /* 0xff89 */
1692 0, 0, 0,
1693 "kp-enter", /* 0xff8d */
1694 0, 0, 0,
1695 "kp-f1", /* 0xff91 */
1696 "kp-f2",
1697 "kp-f3",
1698 "kp-f4",
1699 0, /* 0xff95 */
1700 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1701 "kp-multiply", /* 0xffaa */
1702 "kp-add",
1703 "kp-separator",
1704 "kp-subtract",
1705 "kp-decimal",
1706 "kp-divide", /* 0xffaf */
1707 "kp-0", /* 0xffb0 */
1708 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
1709 0, /* 0xffba */
1710 0, 0,
1711 "kp-equal", /* 0xffbd */
1712 "f1", /* 0xffbe */ /* IsFunctionKey */
1713 "f2", "f3", "f4",
1714 "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12",
1715 "f13", "f14", "f15", "f16", "f17", "f18", "f19", "f20",
1716 "f21", "f22", "f23", "f24", "f25", "f26", "f27", "f28",
1717 "f29", "f30", "f31", "f32", "f33", "f34", "f35" /* 0xffe0 */
1718 };
1719
1720static char *lispy_mouse_names[] =
1721{
1722 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
1723};
1724
1725/* Given a struct input_event, build the lisp event which represents
1726 it. If EVENT is 0, build a mouse movement event from the mouse
1727 movement buffer, which should have a movement event in it. */
1728
1729static Lisp_Object
1730make_lispy_event (event)
1731 struct input_event *event;
1732{
1733#ifdef SWITCH_ENUM_BUG
1734 switch ((int) event->kind)
1735#else
1736 switch (event->kind)
1737#endif
1738 {
1739
1740 /* A simple keystroke. */
1741 case ascii_keystroke:
1742 return event->code;
1743 break;
1744
1745 /* A function key. The symbol may need to have modifier prefixes
1746 tacked onto it. */
1747 case non_ascii_keystroke:
1748 return modify_event_symbol (XFASTINT (event->code), event->modifiers,
1749 Qfunction_key,
1750 lispy_function_keys, &func_key_syms,
1751 (sizeof (lispy_function_keys)
1752 / sizeof (lispy_function_keys[0])));
1753 break;
1754
1755 /* A mouse click - build a list of the relevant information. */
1756 case mouse_click:
1757 {
1758 int part;
1759 Lisp_Object window =
1760 window_from_coordinates (event->screen,
1761 XINT (event->x), XINT (event->y),
1762 &part);
1763 Lisp_Object posn;
1764
1765 if (XTYPE (window) != Lisp_Window)
1766 posn = Qnil;
1767 else
1768 {
1769 XSETINT (event->x, (XINT (event->x)
1770 - XINT (XWINDOW (window)->left)));
1771 XSETINT (event->y, (XINT (event->y)
1772 - XINT (XWINDOW (window)->top)));
1773
1774 if (part == 1)
1775 posn = Qmode_line;
1776 else if (part == 2)
1777 posn = Qvertical_split;
1778 else
1779 XSET (posn, Lisp_Int,
1780 buffer_posn_from_coords (XWINDOW (window),
1781 XINT (event->x),
1782 XINT (event->y)));
1783 }
1784
1785 return Fcons (modify_event_symbol (XFASTINT (event->code) - 1,
1786 event->modifiers,
1787 Qmouse_click,
1788 lispy_mouse_names, &mouse_syms,
1789 (sizeof (lispy_mouse_names)
1790 / sizeof (lispy_mouse_names[0]))),
1791 Fcons (window,
1792 Fcons (posn,
1793 Fcons (Fcons (event->x, event->y),
1794 Fcons (event->timestamp,
1795 Qnil)))));
1796 }
1797
1798 /* A scrollbar click. Build a list containing the relevant
1799 information. */
1800 case scrollbar_click:
1801 {
1802 Lisp_Object button
1803 = modify_event_symbol (XFASTINT (event->code) - 1,
1804 event->modifiers,
1805 Qmouse_click,
1806 lispy_mouse_names, &mouse_syms,
1807 (sizeof (lispy_mouse_names)
1808 / sizeof (lispy_mouse_names[0])));
1809 return Fcons (event->part,
1810 Fcons (SCREEN_SELECTED_WINDOW (event->screen),
1811 Fcons (button,
1812 Fcons (Fcons (event->x, event->y),
1813 Fcons (event->timestamp,
1814 Qnil)))));
1815 }
1816
1817 /* The 'kind' field of the event is something we don't recognize. */
1818 default:
1819 abort();
1820 }
1821}
1822
1823static Lisp_Object
1824make_lispy_movement (screen, x, y, time)
1825 SCREEN_PTR screen;
1826 Lisp_Object x, y;
1827 Lisp_Object time;
1828{
1829 Lisp_Object window;
1830 int ix, iy;
1831 Lisp_Object posn;
1832 int part;
1833
1834 ix = XINT (x);
1835 iy = XINT (y);
1836 window = (screen
1837 ? window_from_coordinates (screen, ix, iy, &part)
1838 : Qnil);
1839 if (XTYPE (window) != Lisp_Window)
1840 posn = Qnil;
1841 else
1842 {
1843 ix -= XINT (XWINDOW (window)->left);
1844 iy -= XINT (XWINDOW (window)->top);
1845 if (part == 1)
1846 posn = Qmode_line;
1847 else if (part == 2)
1848 posn = Qvertical_split;
1849 else
1850 XSET (posn, Lisp_Int, buffer_posn_from_coords (XWINDOW (window),
1851 ix, iy));
1852 }
1853
1854 XSETINT (x, ix);
1855 XSETINT (y, iy);
1856 return Fcons (Qmouse_movement,
1857 Fcons (window,
1858 Fcons (posn,
1859 Fcons (Fcons (x, y),
1860 Fcons (time, Qnil)))));
1861}
1862
1863
1864
1865/* Place the written representation of MODIFIERS in BUF, '\0'-terminated,
1866 and return its length. */
1867
1868static int
1869format_modifiers (modifiers, buf)
1870 int modifiers;
1871 char *buf;
1872{
1873 char *p = buf;
1874
1875 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
1876 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
1877 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
1878 if (modifiers & up_modifier) { *p++ = 'U'; *p++ = '-'; }
1879 *p = '\0';
1880
1881 return p - buf;
1882}
1883
1884
1885/* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
1886 return a symbol with the modifiers placed in the canonical order.
1887
1888 Fdefine_key calls this to make sure that (for example) C-M-foo
1889 and M-C-foo end up being equivalent in the keymap. */
1890
1891Lisp_Object
1892reorder_modifiers (symbol)
1893 Lisp_Object symbol;
1894{
1895 struct Lisp_String *name;
1896 int i;
1897 int modifiers;
1898 int not_canonical;
1899
1900 CHECK_SYMBOL (symbol, 1);
1901
1902 modifiers = 0;
1903 name = XSYMBOL (symbol)->name;
1904
1905 /* Special case for things with only one modifier, which is
1906 (hopefully) the vast majority of cases. */
1907 if (! (name->size >= 4 && name->data[1] == '-' && name->data[3] == '-'))
1908 return symbol;
1909
1910 for (i = 0; i + 1 < name->size && name->data[i + 1] == '-'; i += 2)
1911 switch (name->data[i])
1912 {
1913 case 'M':
1914 not_canonical |= (modifiers & (meta_modifier|ctrl_modifier
1915 |shift_modifier|up_modifier));
1916 modifiers |= meta_modifier;
1917 break;
1918
1919 case 'C':
1920 not_canonical |= (modifiers &
1921 (ctrl_modifier|shift_modifier|up_modifier));
1922 modifiers |= ctrl_modifier;
1923 break;
1924
1925 case 'S':
1926 not_canonical |= (modifiers & (shift_modifier|up_modifier));
1927 modifiers |= shift_modifier;
1928 break;
1929
1930 case 'U':
1931 not_canonical |= (modifiers & (up_modifier));
1932 modifiers |= up_modifier;
1933 break;
1934
1935 default:
1936 goto no_more_modifiers;
1937 }
1938 no_more_modifiers:
1939
1940 if (!not_canonical)
1941 return symbol;
1942
1943 /* The modifiers were out of order, so find a new symbol with the
1944 mods in order. Since the symbol name could contain nulls, we can't
1945 use intern here; we have to use Fintern, which expects a genuine
1946 Lisp_String, and keeps a reference to it. */
1947 {
1948 char *new_mods = (char *) alloca (sizeof ("C-M-S-U-"));
1949 int len = format_modifiers (modifiers, new_mods);
1950 Lisp_Object new_name = make_uninit_string (len + name->size - i);
1951
1952 bcopy (new_mods, XSTRING (new_name)->data, len);
1953 bcopy (name->data + i, XSTRING (new_name)->data + len, name->size - i);
1954
1955 return Fintern (new_name, Qnil);
1956 }
1957}
1958
1959
1960/* For handling events, we often want to produce a symbol whose name
1961 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
1962 to some base, like the name of a function key or mouse button.
1963 modify_event_symbol produces symbols of this sort.
1964
1965 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
1966 is the name of the i'th symbol. TABLE_SIZE is the number of elements
1967 in the table.
1968
1969 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
1970 persist between calls to modify_event_symbol that it can use to
1971 store a cache of the symbols it's generated for this NAME_TABLE
1972 before.
1973
1974 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
1975
1976 MODIFIERS is a set of modifier bits (as given in struct input_events)
1977 whose prefixes should be applied to the symbol name.
1978
1979 SYMBOL_KIND is the value to be placed in the event_kind property of
1980 the returned symbol. */
1981
1982static Lisp_Object
1983modify_event_symbol (symbol_num, modifiers, symbol_kind, name_table,
1984 symbol_table, table_size)
1985 int symbol_num;
1986 unsigned modifiers;
1987 Lisp_Object symbol_kind;
1988 char **name_table;
1989 Lisp_Object *symbol_table;
1990 int table_size;
1991{
1992 Lisp_Object *slot, *unmodified_slot;
1993
1994 /* Is this a request for a valid symbol? */
1995 if (symbol_num < 0 || symbol_num >= table_size
1996 || modifiers >= NUM_MODIFIER_COMBOS)
1997 abort ();
1998
1999 /* If *symbol_table is not a vector of the appropriate size,
2000 set it to one. */
2001 if (XTYPE (*symbol_table) != Lisp_Vector
2002 || XVECTOR (*symbol_table)->size != table_size)
2003 *symbol_table = Fmake_vector (make_number (table_size), Qnil);
2004
2005 unmodified_slot = slot = & XVECTOR (*symbol_table)->contents[symbol_num];
2006
2007 /* If there are modifier keys, there had better be a vector in
2008 this symbol's position of the symbol_table. */
2009 if (modifiers != 0)
2010 {
2011 Lisp_Object slot_contents = *slot;
2012
2013 /* If there isn't the right sort of vector there, put one in. */
2014 if (XTYPE (slot_contents) != Lisp_Vector
2015 || XVECTOR (slot_contents)->size != NUM_MODIFIER_COMBOS)
2016 {
2017 *slot = Fmake_vector (make_number (NUM_MODIFIER_COMBOS), Qnil);
2018
2019 /* Make sure that the vector has an entry for the unmodified
2020 symbol, so we can put it on the event_unmodified property. */
2021 if (! NILP (slot_contents))
2022 XVECTOR (*slot)->contents[0] = slot_contents;
2023 else
2024 XVECTOR (*slot)->contents[0] = intern (name_table [symbol_num]);
2025 }
2026 }
2027
2028 /* If this entry has been filled in with a modified symbol vector,
2029 point to the appropriate slot within that. */
2030 if (XTYPE (*slot) == Lisp_Vector)
2031 {
2032 unmodified_slot = & XVECTOR (*slot)->contents[0];
2033 slot = & XVECTOR (*slot)->contents[modifiers];
2034 }
2035
2036 /* Make sure we have an unmodified version of the symbol in its
2037 proper place? */
2038 if (NILP (*unmodified_slot))
2039 {
2040 *unmodified_slot = intern (name_table [symbol_num]);
2041 Fput (*unmodified_slot, Qevent_kind, symbol_kind);
2042 Fput (*unmodified_slot, Qevent_unmodified, *unmodified_slot);
2043 }
2044
2045 /* Have we already created a symbol for this combination of modifiers? */
2046 if (NILP (*slot))
2047 {
2048 /* No, let's create one. */
2049 char *modified_name
2050 = (char *) alloca (sizeof ("C-M-S-U-")
2051 + strlen (name_table [symbol_num]));
2052
2053 strcpy (modified_name + format_modifiers (modifiers, modified_name),
2054 name_table [symbol_num]);
2055
2056 *slot = intern (modified_name);
2057 Fput (*slot, Qevent_kind, symbol_kind);
2058 Fput (*slot, Qevent_unmodified, *unmodified_slot);
2059 }
2060
2061 return *slot;
2062}
2063\f
2064DEFUN ("mouse-click-p", Fmouse_click_p, Smouse_click_p, 1, 1, 0,
2065 "Return non-nil iff OBJECT is a representation of a mouse event.\n\
2066A mouse event is a list of five elements whose car is a symbol of the\n\
2067form <MODIFIERS>mouse-<DIGIT>. I hope this is a temporary hack.")
2068 (object)
2069 Lisp_Object object;
2070{
2071 if (EVENT_HAS_PARAMETERS (object)
2072 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (object)),
2073 Qmouse_click))
2074 return Qt;
2075 else
2076 return Qnil;
2077}
2078\f
2079/* Store into *addr a value nonzero if terminal input chars are available.
2080 Serves the purpose of ioctl (0, FIONREAD, addr)
2081 but works even if FIONREAD does not exist.
2082 (In fact, this may actually read some input.) */
2083
2084static void
2085get_input_pending (addr)
2086 int *addr;
2087{
2088 /* First of all, have we already counted some input? */
2089 *addr = !NILP (Vquit_flag) || readable_events ();
2090
2091 /* If input is being read as it arrives, and we have none, there is none. */
2092 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
2093 return;
2094
2095 /* Try to read some input and see how much we get. */
2096 gobble_input (0);
2097 *addr = !NILP (Vquit_flag) || readable_events ();
2098}
2099
2100/* Interface to read_avail_input, blocking SIGIO if necessary. */
2101
2102int
2103gobble_input (expected)
2104 int expected;
2105{
2106#ifndef VMS
2107#ifdef SIGIO
2108 if (interrupt_input)
2109 {
32676c08 2110 SIGMASKTYPE mask;
e065a56e 2111 mask = sigblockx (SIGIO);
284f4730 2112 read_avail_input (expected);
e065a56e 2113 sigsetmask (mask);
284f4730
JB
2114 }
2115 else
2116#endif
2117 read_avail_input (expected);
2118#endif
2119}
2120\f
2121#ifndef VMS
2122
2123/* Read any terminal input already buffered up by the system
2124 into the kbd_buffer, but do not wait.
2125
2126 EXPECTED should be nonzero if the caller knows there is some input.
2127
2128 Except on VMS, all input is read by this function.
2129 If interrupt_input is nonzero, this function MUST be called
2130 only when SIGIO is blocked.
2131
2132 Returns the number of keyboard chars read, or -1 meaning
2133 this is a bad time to try to read input. */
2134
2135static int
2136read_avail_input (expected)
2137 int expected;
2138{
2139 struct input_event buf[KBD_BUFFER_SIZE];
2140 register int i;
2141 int nread;
2142
2143 if (read_socket_hook)
2144 /* No need for FIONREAD or fcntl; just say don't wait. */
2145 nread = (*read_socket_hook) (0, buf, KBD_BUFFER_SIZE, expected, expected);
2146 else
2147 {
2148 unsigned char cbuf[KBD_BUFFER_SIZE];
2149
2150#ifdef FIONREAD
2151 /* Find out how much input is available. */
2152 if (ioctl (0, FIONREAD, &nread) < 0)
2153 /* Formerly simply reported no input, but that sometimes led to
2154 a failure of Emacs to terminate.
2155 SIGHUP seems appropriate if we can't reach the terminal. */
2156 kill (getpid (), SIGHUP);
2157 if (nread == 0)
2158 return 0;
2159 if (nread > sizeof cbuf)
2160 nread = sizeof cbuf;
2161#else /* no FIONREAD */
2162#ifdef USG
2163 /* Read some input if available, but don't wait. */
2164 nread = sizeof cbuf;
2165 fcntl (fileno (stdin), F_SETFL, O_NDELAY);
2166#else
2167 you lose;
2168#endif
2169#endif
2170
2171 /* Now read; for one reason or another, this will not block. */
2172 while (1)
2173 {
2174 nread = read (fileno (stdin), cbuf, nread);
2175#ifdef AIX
2176 /* The kernel sometimes fails to deliver SIGHUP for ptys.
2177 This looks incorrect, but it isn't, because _BSD causes
2178 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
2179 and that causes a value other than 0 when there is no input. */
2180 if (nread == 0)
2181 kill (SIGHUP, 0);
2182#endif
2183 /* Retry the read if it is interrupted. */
2184 if (nread >= 0
2185 || ! (errno == EAGAIN || errno == EFAULT
2186#ifdef EBADSLT
2187 || errno == EBADSLT
2188#endif
2189 ))
2190 break;
2191 }
2192
2193#ifndef FIONREAD
2194#ifdef USG
2195 fcntl (fileno (stdin), F_SETFL, 0);
2196#endif /* USG */
2197#endif /* no FIONREAD */
2198 for (i = 0; i < nread; i++)
2199 {
2200 buf[i].kind = ascii_keystroke;
2201 XSET (buf[i].code, Lisp_Int, cbuf[i]);
2202 buf[i].screen = selected_screen;
2203 }
2204 }
2205
2206 /* Scan the chars for C-g and store them in kbd_buffer. */
2207 for (i = 0; i < nread; i++)
2208 {
2209 kbd_buffer_store_event (&buf[i]);
2210 /* Don't look at input that follows a C-g too closely.
2211 This reduces lossage due to autorepeat on C-g. */
2212 if (buf[i].kind == ascii_keystroke
2213 && XINT(buf[i].code) == quit_char)
2214 break;
2215 }
2216
2217 return nread;
2218}
2219#endif /* not VMS */
2220\f
2221#ifdef SIGIO /* for entire page */
2222/* Note SIGIO has been undef'd if FIONREAD is missing. */
2223
2224input_available_signal (signo)
2225 int signo;
2226{
2227 /* Must preserve main program's value of errno. */
2228 int old_errno = errno;
2229#ifdef BSD4_1
2230 extern int select_alarmed;
2231#endif
2232
2233#ifdef USG
2234 /* USG systems forget handlers when they are used;
2235 must reestablish each time */
2236 signal (signo, input_available_signal);
2237#endif /* USG */
2238
2239#ifdef BSD4_1
2240 sigisheld (SIGIO);
2241#endif
2242
ffd56f97
JB
2243 if (input_available_clear_time)
2244 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
284f4730
JB
2245
2246 while (1)
2247 {
2248 int nread;
2249 nread = read_avail_input (1);
2250 /* -1 means it's not ok to read the input now.
2251 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
2252 0 means there was no keyboard input available. */
2253 if (nread <= 0)
2254 break;
2255
2256#ifdef BSD4_1
2257 select_alarmed = 1; /* Force the select emulator back to life */
2258#endif
2259 }
2260
2261#ifdef BSD4_1
2262 sigfree ();
2263#endif
2264 errno = old_errno;
2265}
2266#endif /* SIGIO */
2267\f
2268/* Return the prompt-string of a sparse keymap.
2269 This is the first element which is a string.
2270 Return nil if there is none. */
2271
2272Lisp_Object
2273map_prompt (map)
2274 Lisp_Object map;
2275{
2276 while (CONSP (map))
2277 {
2278 register Lisp_Object tem;
2279 tem = Fcar (map);
2280 if (XTYPE (tem) == Lisp_String)
2281 return tem;
2282 map = Fcdr (map);
2283 }
2284 return Qnil;
2285}
2286
2287static int echo_flag;
2288static int echo_now;
2289
2290/* Read a character like read_char but optionally prompt based on maps
2291 LOCAL and GLOBAL.
2292
2293 The prompting is done based on the prompt-string of the map
2294 and the strings associated with various map elements. */
2295
2296Lisp_Object
2297read_char_menu_prompt (prompt, local, global)
2298 int prompt;
2299 Lisp_Object local, global;
2300{
2301 register Lisp_Object rest, name;
2302 Lisp_Object hmap;
2303 int nlength;
2304 int width = SCREEN_WIDTH (selected_screen) - 4;
2305 char *menu = (char *) alloca (width);
2306
2307 /* Use local over global Menu maps */
2308
2309 if (menu_prompting)
2310 return read_char (!prompt);
2311
2312 /* We can't get prompt strings from dense keymaps. */
2313 if (CONSP (local)
2314 && EQ (Fcar (local), Qkeymap)
2315 && !(CONSP (XCONS (local)->cdr)
2316 && XTYPE (XCONS (XCONS (local)->cdr)->car) == Lisp_Vector))
2317 hmap = local;
2318 else if (CONSP (global)
2319 && EQ (Fcar (global), Qkeymap)
2320 && !(CONSP (XCONS (global)->cdr)
2321 && XTYPE (XCONS (XCONS (global)->cdr)->car) == Lisp_Vector))
2322 hmap = global;
2323 else
2324 return read_char (!prompt);
2325
2326 /* Get the map's prompt string. */
2327 name = map_prompt (hmap);
2328 if (NILP (name))
2329 return read_char (!prompt);
2330
2331 /* Prompt string always starts with map's prompt, and a space. */
2332 strcpy (menu, XSTRING (name)->data);
2333 nlength = XSTRING (name)->size;
2334 menu[nlength++] = ' ';
2335 menu[nlength] = 0;
2336
2337 /* Start prompting at start of map. */
2338 rest = hmap; /* Current menu item */
2339
2340 /* Present the documented bindings, a line at a time. */
2341 while (1)
2342 {
2343 int notfirst = 0;
2344 int i = nlength;
2345 Lisp_Object obj;
2346 int ch;
2347
2348 /* If reached end of map, start at beginning. */
2349 if (NILP (Fcdr (rest))) rest = hmap;
2350
2351 /* Loop over elements of map. */
2352 while (!NILP (rest) && i < width)
2353 {
2354 Lisp_Object s;
2355
2356 /* Look for conses whose cadrs are strings. */
2357 s = Fcar_safe (Fcdr_safe (Fcar_safe (rest)));
2358 if (XTYPE (s) != Lisp_String)
2359 /* Ignore all other elements. */
2360 ;
2361 /* If first such element, or enough room, add string to prompt. */
2362 else if (XSTRING (s)->size + i < width
2363 || !notfirst)
2364 {
2365 int thiswidth;
2366
2367 /* Punctuate between strings. */
2368 if (notfirst)
2369 {
2370 strcpy (menu + i, ", ");
2371 i += 2;
2372 }
2373 notfirst = 1;
2374
2375 /* Add as much of string as fits. */
2376 thiswidth = XSTRING (s)->size;
2377 if (thiswidth + i > width)
2378 thiswidth = width - i;
2379 bcopy (XSTRING (s)->data, menu + i, thiswidth);
2380 i += thiswidth;
2381 }
2382 else
2383 {
2384 /* If some elts don't fit, show there are more. */
2385 strcpy (menu + i, "...");
2386 break;
2387 }
2388
2389 /* Move past this element. */
2390 rest = Fcdr_safe (rest);
2391 }
2392
2393 /* Prompt with that and read response. */
2394 message1 (menu);
2395 obj = read_char (1);
2396
2397 if (XTYPE (obj) != Lisp_Int)
2398 return obj;
2399 else
2400 ch = XINT (obj);
2401
2402 if (obj != menu_prompt_more_char
2403 && (XTYPE (menu_prompt_more_char) != Lisp_Int
2404 || obj != make_number (Ctl (XINT (menu_prompt_more_char)))))
2405 return obj;
2406 }
2407}
2408
2409\f
2410/* Reading key sequences. */
2411
2412/* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
2413 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
2414 keymap, or nil otherwise. Return the index of the first keymap in
2415 which KEY has any binding, or NMAPS if no map has a binding.
2416
2417 If KEY is a meta ASCII character, treat it like meta-prefix-char
2418 followed by the corresponding non-meta character. Keymaps in
2419 CURRENT with non-prefix bindings for meta-prefix-char become nil in
2420 NEXT.
2421
2422 When KEY is not defined in any of the keymaps, if it is an upper
2423 case letter and there are bindings for the corresponding lower-case
2424 letter, return the bindings for the lower-case letter.
2425
2426 NEXT may == CURRENT. */
2427
2428static int
2429follow_key (key, nmaps, current, defs, next)
2430 Lisp_Object key;
2431 Lisp_Object *current, *defs, *next;
2432 int nmaps;
2433{
2434 int i, first_binding;
2435
2436 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
2437 followed by the corresponding non-meta character. */
2438 if (XTYPE (key) == Lisp_Int
2439 && XINT (key) >= 0200)
2440 {
2441 for (i = 0; i < nmaps; i++)
2442 if (! NILP (current[i]))
2443 {
2444 next[i] = get_keyelt (access_keymap (current[i],
2445 meta_prefix_char));
2446
2447 /* Note that since we pass the resulting bindings through
2448 get_keymap_1, non-prefix bindings for meta-prefix-char
2449 disappear. */
2450 next[i] = get_keymap_1 (next[i], 0);
2451 }
2452 else
2453 next[i] = Qnil;
2454
2455 current = next;
2456 XSET (key, Lisp_Int, XFASTINT (key) & 0177);
2457 }
2458
2459 first_binding = nmaps;
2460 for (i = nmaps - 1; i >= 0; i--)
2461 {
2462 if (! NILP (current[i]))
2463 {
2464 defs[i] = get_keyelt (access_keymap (current[i], key));
2465 if (! NILP (defs[i]))
2466 first_binding = i;
2467 }
2468 else
2469 defs[i] = Qnil;
2470 }
2471
2472 /* When KEY is not defined in any of the keymaps, if it is an upper
2473 case letter and there are bindings for the corresponding
2474 lower-case letter, return the bindings for the lower-case letter. */
2475 if (first_binding == nmaps
2476 && XTYPE (key) == Lisp_Int
2477 && UPPERCASEP (XINT (key)))
2478 {
2479 XSETINT (key, DOWNCASE (XINT (key)));
2480
2481 first_binding = nmaps;
2482 for (i = nmaps - 1; i >= 0; i--)
2483 {
2484 if (! NILP (current[i]))
2485 {
2486 defs[i] = get_keyelt (access_keymap (current[i], key));
2487 if (! NILP (defs[i]))
2488 first_binding = i;
2489 }
2490 else
2491 defs[i] = Qnil;
2492 }
2493 }
2494
2495 /* Given the set of bindings we've found, produce the next set of maps. */
2496 for (i = 0; i < nmaps; i++)
2497 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0);
2498
2499 return first_binding;
2500}
2501
2502/* Read a sequence of keys that ends with a non prefix character
2503 according to the keymaps in KEYMAPS[0..nmaps-1]. Keymaps appearing
2504 earlier in KEYMAPS take precidence over those appearing later.
2505
2506 Store the sequence in KEYBUF, a buffer of size BUFSIZE. Prompt
2507 with PROMPT. Echo starting immediately unless `prompt' is 0.
2508 Return the length of the key sequence stored.
2509
2510 If the user switches screens in the midst of a key sequence, we
2511 throw away any prefix we have read so far, and start afresh. For
2512 mouse clicks, we look up the click in the keymap of the buffer
2513 clicked on, throwing away any prefix if it is not the same buffer
2514 we used to be reading from. */
2515
2516static int
2517read_key_sequence (keybuf, bufsize, prompt)
2518 Lisp_Object *keybuf;
2519 int bufsize;
2520 Lisp_Object prompt;
2521{
2522 /* How many keys there are in the current key sequence. */
2523 int t;
2524
2525 /* The buffer that the most recently read event was typed at. This
2526 helps us read mouse clicks according to the buffer clicked in,
2527 and notice when the mouse has moved from one screen to another. */
2528 struct buffer *last_event_buffer = current_buffer;
2529
2530 /* The length of the echo buffer when we started reading, and
2531 the length of this_command_keys when we started reading. */
2532 int echo_start;
2533 int keys_start = this_command_key_count;
2534
2535 /* The number of keymaps we're scanning right now, and the number of
2536 keymaps we have allocated space for. */
2537 int nmaps;
2538 int nmaps_allocated = 0;
2539
2540 /* current[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
2541 in the current keymaps, or nil where it is not a prefix. */
2542 Lisp_Object *current;
2543
2544 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
2545 the current keymaps. */
2546 Lisp_Object *defs;
2547
2548 /* The lowest i such that defs[i] is non-nil. */
2549 int first_binding;
2550
2551 /* If mock_input > t, then KEYBUF[t] should be read as the next
2552 input key. */
2553 int mock_input = 0;
2554
2555 /* If the sequence is unbound in current[], keymap[fkey_start..fkey_end-1]
2556 is a prefix in Vfunction_key_map, and fkey_map is its binding.
2557 These might be > t, indicating that all function key scanning should
2558 hold off until t reaches them. */
2559 int fkey_start = 0, fkey_end = 0;
2560 Lisp_Object fkey_map = Vfunction_key_map;
2561
2562 if (INTERACTIVE)
2563 {
2564 if (prompt)
2565 echo_prompt (prompt);
2566 else if (cursor_in_echo_area)
2567 /* This doesn't put in a dash if the echo buffer is empty, so
2568 you don't always see a dash hanging out in the minibuffer. */
2569 echo_dash ();
2570 echo_start = echo_length ();
2571 }
2572
2573 /* If there is no function key map, turn of function key scanning. */
2574 if (NILP (Fkeymapp (Vfunction_key_map)))
2575 fkey_start = fkey_end = bufsize + 1;
2576
2577 restart:
2578 t = 0;
2579 this_command_key_count = keys_start;
2580
2581 {
2582 Lisp_Object *maps;
2583
2584 nmaps = current_minor_maps (0, &maps) + 2;
2585 if (nmaps > nmaps_allocated)
2586 {
2587 current = (Lisp_Object *) alloca (nmaps * sizeof (current[0]));
2588 defs = (Lisp_Object *) alloca (nmaps * sizeof (defs[0]));
2589 nmaps_allocated = nmaps;
2590 }
2591 bcopy (maps, current, (nmaps - 2) * sizeof (current[0]));
2592 current[nmaps-2] = last_event_buffer->keymap;
2593 current[nmaps-1] = global_map;
2594 }
2595
2596 /* Find an accurate initial value for first_binding. */
2597 for (first_binding = 0; first_binding < nmaps; first_binding++)
2598 if (! NILP (current[first_binding]))
2599 break;
2600
2601 while ((first_binding < nmaps && ! NILP (current[first_binding]))
2602 || (first_binding >= nmaps && fkey_start < t))
2603 {
2604 Lisp_Object key;
2605
2606 if (t >= bufsize)
2607 error ("key sequence too long");
2608
2609 /* Are we reading keys stuffed into keybuf? */
2610 if (t < mock_input)
2611 {
2612 key = keybuf[t];
2613 add_command_key (key);
2614 echo_char (key);
2615 }
2616 /* Otherwise, we should actually read a character. */
2617 else
2618 {
2619 struct buffer *buf;
2620
2621 if (!prompt && INTERACTIVE)
2622 key = read_char_menu_prompt (prompt, Qnil, Qnil);
2623 else
2624 key = read_char (!prompt);
2625
2626 /* The above routines return -1 at the end of a macro.
2627 Emacs 18 handles this by returning immediately with a
2628 zero, so that's what we'll do. */
2629 if (XTYPE (key) == Lisp_Int && XINT (key) < 0)
2630 return 0;
2631
2632 Vquit_flag = Qnil;
2633
2634 /* What buffer was this event typed/moused at? */
2635 if (XTYPE (key) == Lisp_Int || XTYPE (key) == Lisp_Symbol)
2636 buf = (XBUFFER
2637 (XWINDOW
2638 (SCREEN_SELECTED_WINDOW
2639 (XSCREEN (Vlast_event_screen)))->buffer));
2640 else if (EVENT_HAS_PARAMETERS (key))
2641 {
2642 Lisp_Object window = EVENT_WINDOW (key);
2643
2644 if (NILP (window))
2645 abort ();
2646
2647 buf = XBUFFER (XWINDOW (window)->buffer);
2648 }
2649 else
2650 abort ();
2651
2652 /* If this event came to a different buffer than the one
2653 we're currently in, switch buffers and start a new key
2654 sequence, starting with key. */
2655 if (buf != last_event_buffer)
2656 {
2657 last_event_buffer = buf;
2658 Fselect_screen (Vlast_event_screen, Qnil);
2659
2660 /* Arrange to read key as the next event. */
2661 keybuf[0] = key;
2662 mock_input = 1;
2663
2664 /* Truncate the key sequence in the echo area. */
2665 if (INTERACTIVE)
2666 echo_truncate (echo_start);
2667
2668 goto restart;
2669 }
2670 }
2671
2672 first_binding = (follow_key (key,
2673 nmaps - first_binding,
2674 current + first_binding,
2675 defs + first_binding,
2676 current + first_binding)
2677 + first_binding);
2678 keybuf[t++] = key;
2679
2680 /* If the sequence is unbound, see if we can hang a function key
2681 off the end of it. Don't reread the expansion of a function key. */
2682 if (first_binding >= nmaps
2683 && t > mock_input)
2684 {
2685 Lisp_Object fkey_next;
2686
2687 /* Scan from fkey_end until we find a bound suffix. */
2688 while (fkey_end < t)
2689 {
2690 fkey_next =
2691 get_keyelt (access_keymap (fkey_map, keybuf[fkey_end++]));
284f4730 2692 /* If keybuf[fkey_start..fkey_next] is bound in the
a764a753
JB
2693 function key map and it's a suffix of the current
2694 sequence (i.e. fkey_next == t), replace it with
2695 the binding and restart with fkey_start at the end. */
284f4730
JB
2696 if (XTYPE (fkey_next) == Lisp_Vector
2697 && fkey_end == t)
2698 {
2699 t = fkey_start + XVECTOR (fkey_next)->size;
2700 if (t >= bufsize)
2701 error ("key sequence too long");
2702
2703 bcopy (XVECTOR (fkey_next)->contents,
2704 keybuf + fkey_start,
2705 (t - fkey_start) * sizeof (keybuf[0]));
2706
2707 mock_input = t;
2708 fkey_start = fkey_end = t;
2709
2710 /* Truncate the key sequence in the echo area. */
2711 if (INTERACTIVE)
2712 echo_truncate (echo_start);
2713
2714 goto restart;
2715 }
2716
2717 fkey_map = get_keymap_1 (fkey_next, 0);
2718
a764a753
JB
2719 /* If we no longer have a bound suffix, try a new positions for
2720 fkey_start. */
284f4730
JB
2721 if (NILP (fkey_map))
2722 {
2723 fkey_end = ++fkey_start;
2724 fkey_map = Vfunction_key_map;
2725 }
2726 }
2727 }
2728 }
2729
2730 read_key_sequence_cmd = (first_binding < nmaps
2731 ? defs[first_binding]
2732 : Qnil);
2733
2734 return t;
2735}
2736
2737DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 1, 0,
2738 "Read a sequence of keystrokes and return as a string or vector.\n\
2739The sequence is sufficient to specify a non-prefix command in the\n\
2740current local and global maps.\n\
2741\n\
2742Arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
2743\n\
2744If Emacs is running on multiple screens, switching between screens in\n\
2745the midst of a keystroke will toss any prefix typed so far. A C-g\n\
2746typed while in this function is treated like any other character, and\n\
2747`quit-flag' is not set.")
2748 (prompt)
2749 Lisp_Object prompt;
2750{
2751 Lisp_Object keybuf[30];
2752 register int i;
2753 struct gcpro gcpro1, gcpro2;
2754
2755 if (!NILP (prompt))
2756 CHECK_STRING (prompt, 0);
2757 QUIT;
2758
2759 bzero (keybuf, sizeof keybuf);
2760 GCPRO1 (keybuf[0]);
2761 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
2762
2763 this_command_key_count = 0;
2764 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
2765 NILP (prompt) ? 0 : XSTRING (prompt)->data);
2766
2767 UNGCPRO;
2768 return make_array (i, keybuf);
2769}
2770\f
2771DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
2772 "Execute CMD as an editor command.\n\
2773CMD must be a symbol that satisfies the `commandp' predicate.\n\
2774Optional second arg RECORD-FLAG non-nil\n\
2775means unconditionally put this command in `command-history'.\n\
2776Otherwise, that is done only if an arg is read using the minibuffer.")
2777 (cmd, record)
2778 Lisp_Object cmd, record;
2779{
2780 register Lisp_Object final;
2781 register Lisp_Object tem;
2782 Lisp_Object prefixarg;
2783 struct backtrace backtrace;
2784 extern int debug_on_next_call;
2785
2786 prefixarg = Vprefix_arg, Vprefix_arg = Qnil;
2787 Vcurrent_prefix_arg = prefixarg;
2788 debug_on_next_call = 0;
2789
2790 if (XTYPE (cmd) == Lisp_Symbol)
2791 {
2792 tem = Fget (cmd, Qdisabled);
2793 if (!NILP (tem))
2794 return call1 (Vrun_hooks, Vdisabled_command_hook);
2795 }
2796
2797 while (1)
2798 {
ffd56f97 2799 final = Findirect_function (cmd);
284f4730
JB
2800
2801 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
2802 do_autoload (final, cmd);
2803 else
2804 break;
2805 }
2806
2807 if (XTYPE (final) == Lisp_String
2808 || XTYPE (final) == Lisp_Vector)
2809 {
2810 /* If requested, place the macro in the command history. For
2811 other sorts of commands, call-interactively takes care of
2812 this. */
2813 if (!NILP (record))
2814 Vcommand_history
2815 = Fcons (Fcons (Qexecute_kbd_macro,
2816 Fcons (final, Fcons (prefixarg, Qnil))),
2817 Vcommand_history);
2818
2819 return Fexecute_kbd_macro (final, prefixarg);
2820 }
2821 if (CONSP (final) || XTYPE (final) == Lisp_Subr
2822 || XTYPE (final) == Lisp_Compiled)
2823 {
2824 backtrace.next = backtrace_list;
2825 backtrace_list = &backtrace;
2826 backtrace.function = &Qcall_interactively;
2827 backtrace.args = &cmd;
2828 backtrace.nargs = 1;
2829 backtrace.evalargs = 0;
2830
2831 tem = Fcall_interactively (cmd, record);
2832
2833 backtrace_list = backtrace.next;
2834 return tem;
2835 }
2836 return Qnil;
2837}
2838\f
2839#if 0
2840DEFUN ("execute-mouse-event", Fexecute_mouse_event, Sexecute_mouse_event,
2841 1, 1, 0,
2842 "Execute the definition of the mouse-click event EVENT.\n\
2843The handler function is found by looking the event's key sequence up\n\
2844in the buffer's local mouse map and in `global-mouse-map'.\n\
2845\n\
2846After running the handler, call the value of `mouse-event-function'\n\
2847with EVENT as arg.")
2848 (event)
2849 Lisp_Object event;
2850{
2851 Lisp_Object tem;
2852 Lisp_Object mouse_cmd;
2853 Lisp_Object keyseq, window, screen_part, pos, time;
2854
2855#ifndef HAVE_X11
2856 Vmouse_event = event;
2857#endif
2858
2859 if (EQ (event, Qnil))
2860 {
2861 bitch_at_user ();
2862 return Qnil;
2863 }
2864
2865 CHECK_CONS (event, 0);
2866 pos = Fcar (event);
2867 window = Fcar (Fcdr (event));
2868 screen_part = Fcar (Fcdr (Fcdr (event)));
2869 keyseq = Fcar (Fcdr (Fcdr (Fcdr (event))));
2870 time = Fcar (Fcdr (Fcdr (Fcdr (Fcdr (event)))));
2871 CHECK_STRING (keyseq, 0);
2872 CHECK_WINDOW (window, 0);
2873
2874 /* Look up KEYSEQ in the buffer's local mouse map, then in global one. */
2875
2876 mouse_cmd = Qnil;
2877
2878 if (!NILP (XWINDOW (window)->buffer))
2879 {
2880 Lisp_Object local_map;
2881
2882 local_map = XBUFFER (XWINDOW (window)->buffer)->mouse_map;
2883 tem = Fkeymapp (local_map);
2884 if (!NILP (tem))
2885 mouse_cmd = Flookup_key (local_map, keyseq);
2886 /* A number as value means the key is too long; treat as undefined. */
2887 if (XTYPE (mouse_cmd) == Lisp_Int)
2888 mouse_cmd = Qnil;
2889 }
2890
2891 tem = Fkeymapp (Vglobal_mouse_map);
2892 if (NILP (mouse_cmd) && !NILP (tem))
2893 mouse_cmd = Flookup_key (Vglobal_mouse_map, keyseq);
2894 if (XTYPE (mouse_cmd) == Lisp_Int)
2895 mouse_cmd = Qnil;
2896
2897 if (NILP (mouse_cmd))
2898 {
2899 /* This button/shift combination is not defined.
2900 If it is a button-down event, ring the bell. */
2901#ifdef HAVE_X11
2902 if (XSTRING (keyseq)->data[XSTRING (keyseq)->size - 1] & 0x18 == 0)
2903#else
2904 if (XSTRING (keyseq)->data[XSTRING (keyseq)->size - 1] & 4 == 0)
2905#endif
2906 bitch_at_user ();
2907 }
2908 else
2909 {
2910 SCREEN_PTR s = XSCREEN (WINDOW_SCREEN (XWINDOW (window)));
2911
2912#ifndef HAVE_X11
2913 Vmouse_window = s->selected_window;
2914#endif /* HAVE_X11 */
2915 /* It's defined; call the definition. */
2916 Vprefix_arg = Qnil;
2917 if (!NILP (screen_part))
2918 {
2919 /* For a scroll-bar click, set the prefix arg
2920 to the number of lines down from the top the click was.
2921 Many scroll commands want to scroll by this many lines. */
2922 Lisp_Object position;
2923 Lisp_Object length;
2924 Lisp_Object offset;
2925
2926 position = Fcar (pos);
2927 length = Fcar (Fcdr (pos));
2928 offset = Fcar (Fcdr (Fcdr (pos)));
2929
2930 if (XINT (length) != 0)
2931 XSET (Vprefix_arg, Lisp_Int,
2932 (SCREEN_HEIGHT (s) * (XINT (position) + XINT (offset))
2933 / (XINT (length) + 2 * XINT (offset))));
2934 }
2935 Fcommand_execute (mouse_cmd, Qnil);
2936 }
2937
2938 if (!NILP (Vmouse_event_function)) /* Not `event' so no need for GCPRO */
2939 call1 (Vmouse_event_function, Vmouse_event);
2940 return Qnil;
2941}
2942#endif
2943\f
2944DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
2945 1, 1, "P",
2946 "Read function name, then read its arguments and call it.")
2947 (prefixarg)
2948 Lisp_Object prefixarg;
2949{
2950 Lisp_Object function;
2951 char buf[40];
2952 Lisp_Object saved_keys;
2953 struct gcpro gcpro1;
2954
2955 saved_keys = Fthis_command_keys ();
2956 buf[0] = 0;
2957 GCPRO1 (saved_keys);
2958
2959 if (EQ (prefixarg, Qminus))
2960 strcpy (buf, "- ");
2961 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
2962 strcpy (buf, "C-u ");
2963 else if (CONSP (prefixarg) && XTYPE (XCONS (prefixarg)->car) == Lisp_Int)
2964 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
2965 else if (XTYPE (prefixarg) == Lisp_Int)
2966 sprintf (buf, "%d ", XINT (prefixarg));
2967
2968 /* This isn't strictly correct if execute-extended-command
2969 is bound to anything else. Perhaps it should use
2970 this_command_keys? */
2971 strcat (buf, "M-x ");
2972
2973 /* Prompt with buf, and then read a string, completing from and
2974 restricting to the set of all defined commands. Don't provide
2975 any initial input. The last Qnil says not to perform a
2976 peculiar hack on the initial input. */
2977 function = Fcompleting_read (build_string (buf),
2978 Vobarray, Qcommandp,
2979 Qt, Qnil, Qnil);
2980
2981 /* Add the text read to this_command_keys. */
2982 {
2983 struct Lisp_String *func_str = XSTRING (function);
2984 int i;
2985 Lisp_Object tem;
2986
2987 for (i = 0; i < func_str->size; i++)
2988 {
2989 XSET (tem, Lisp_Int, func_str->data[i]);
2990 add_command_key (tem);
2991 }
2992 }
2993
2994 UNGCPRO;
2995
2996 function = Fintern (function, Vobarray);
2997 Vprefix_arg = prefixarg;
2998 this_command = function;
2999
3000 return Fcommand_execute (function, Qt);
3001}
3002\f
3003
3004detect_input_pending ()
3005{
3006 if (!input_pending)
3007 get_input_pending (&input_pending);
3008
3009 return input_pending;
3010}
3011
ffd56f97
JB
3012/* This is called in some cases before a possible quit.
3013 It cases the next call to detect_input_pending to recompute input_pending.
3014 So calling this function unnecessarily can't do any harm. */
3015clear_input_pending ()
3016{
3017 input_pending = 0;
3018}
3019
284f4730
JB
3020DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
3021 "T if command input is currently available with no waiting.\n\
3022Actually, the value is nil only if we can be sure that no input is available.")
3023 ()
3024{
3025 if (!NILP (unread_command_char))
3026 return (Qt);
3027
3028 return detect_input_pending () ? Qt : Qnil;
3029}
3030
3031DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
3032 "Return vector of last 100 chars read from terminal.")
3033 ()
3034{
3035 Lisp_Object val;
3036
3037 if (total_keys < NUM_RECENT_KEYS)
3038 return Fvector (total_keys, recent_keys);
3039 else
3040 {
3041 val = Fvector (NUM_RECENT_KEYS, recent_keys);
3042 bcopy (recent_keys + recent_keys_index,
3043 XVECTOR (val)->contents,
3044 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
3045 bcopy (recent_keys,
3046 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
3047 recent_keys_index * sizeof (Lisp_Object));
3048 return val;
3049 }
3050}
3051
3052DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
3053 "Return string of the keystrokes that invoked this command.")
3054 ()
3055{
3056 return make_array (this_command_key_count, this_command_keys);
3057}
3058
3059DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
3060 "Return the current depth in recursive edits.")
3061 ()
3062{
3063 Lisp_Object temp;
3064 XFASTINT (temp) = command_loop_level + minibuf_level;
3065 return temp;
3066}
3067
3068DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
3069 "FOpen dribble file: ",
3070 "Start writing all keyboard characters to FILE.")
3071 (file)
3072 Lisp_Object file;
3073{
3074 if (NILP (file))
3075 {
3076 fclose (dribble);
3077 dribble = 0;
3078 }
3079 else
3080 {
3081 file = Fexpand_file_name (file, Qnil);
3082 dribble = fopen (XSTRING (file)->data, "w");
3083 }
3084 return Qnil;
3085}
3086
3087DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
3088 "Discard the contents of the terminal input buffer.\n\
3089Also cancel any kbd macro being defined.")
3090 ()
3091{
3092 defining_kbd_macro = 0;
3093 update_mode_lines++;
3094
3095#if 0
3096 unread_command_char = make_number (-1);
3097#endif
3098 unread_command_char = Qnil;
3099
3100 discard_tty_input ();
3101
3102 kbd_fetch_ptr = kbd_store_ptr;
3103 input_pending = 0;
3104
3105 return Qnil;
3106}
3107\f
3108DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
3109 "Stop Emacs and return to superior process. You can resume later.\n\
3110On systems that don't have job control, run a subshell instead.\n\n\
3111If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
3112to be read as terminal input by Emacs's superior shell.\n\
3113Before suspending, if `suspend-hook' is bound and value is non-nil\n\
3114call the value as a function of no args. Don't suspend if it returns non-nil.\n\
3115Otherwise, suspend normally and after resumption call\n\
3116`suspend-resume-hook' if that is bound and non-nil.\n\
3117\n\
3118Some operating systems cannot stop the Emacs process and resume it later.\n\
3119On such systems, Emacs will start a subshell and wait for it to exit.")
3120 (stuffstring)
3121 Lisp_Object stuffstring;
3122{
3123 register Lisp_Object tem;
3124 int count = specpdl_ptr - specpdl;
3125 int old_height, old_width;
3126 int width, height;
3127 struct gcpro gcpro1;
3128 extern init_sys_modes ();
3129
3130 if (!NILP (stuffstring))
3131 CHECK_STRING (stuffstring, 0);
3132 GCPRO1 (stuffstring);
3133
3134 /* Call value of suspend-hook
3135 if it is bound and value is non-nil. */
3136 if (!NILP (Vrun_hooks))
3137 {
3138 tem = call1 (Vrun_hooks, intern ("suspend-hook"));
3139 if (!EQ (tem, Qnil)) return Qnil;
3140 }
3141
3142 get_screen_size (&old_width, &old_height);
3143 reset_sys_modes ();
3144 /* sys_suspend can get an error if it tries to fork a subshell
3145 and the system resources aren't available for that. */
3146 record_unwind_protect (init_sys_modes, 0);
3147 stuff_buffered_input (stuffstring);
3148 sys_suspend ();
3149 unbind_to (count, Qnil);
3150
3151 /* Check if terminal/window size has changed.
3152 Note that this is not useful when we are running directly
3153 with a window system; but suspend should be disabled in that case. */
3154 get_screen_size (&width, &height);
3155 if (width != old_width || height != old_height)
3156 change_screen_size (height, width, 0);
3157
3158 /* Call value of suspend-resume-hook
3159 if it is bound and value is non-nil. */
3160 if (!NILP (Vrun_hooks))
3161 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
3162
3163 UNGCPRO;
3164 return Qnil;
3165}
3166
3167/* If STUFFSTRING is a string, stuff its contents as pending terminal input.
3168 Then in any case stuff anthing Emacs has read ahead and not used. */
3169
3170stuff_buffered_input (stuffstring)
3171 Lisp_Object stuffstring;
3172{
3173 register unsigned char *p;
3174
3175/* stuff_char works only in BSD, versions 4.2 and up. */
3176#ifdef BSD
3177#ifndef BSD4_1
3178 if (XTYPE (stuffstring) == Lisp_String)
3179 {
3180 register int count;
3181
3182 p = XSTRING (stuffstring)->data;
3183 count = XSTRING (stuffstring)->size;
3184 while (count-- > 0)
3185 stuff_char (*p++);
3186 stuff_char ('\n');
3187 }
3188 /* Anything we have read ahead, put back for the shell to read. */
3189 while (kbd_fetch_ptr != kbd_store_ptr)
3190 {
3191 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
3192 kbd_fetch_ptr = kbd_buffer;
3193 if (kbd_fetch_ptr->kind == ascii_keystroke)
3194 stuff_char (XINT (kbd_fetch_ptr->code));
3195 kbd_fetch_ptr++;
3196 }
3197 input_pending = 0;
3198#endif
3199#endif /* BSD and not BSD4_1 */
3200}
3201\f
ffd56f97
JB
3202set_waiting_for_input (time_to_clear)
3203 EMACS_TIME *time_to_clear;
284f4730 3204{
ffd56f97 3205 input_available_clear_time = time_to_clear;
284f4730
JB
3206
3207 /* Tell interrupt_signal to throw back to read_char, */
3208 waiting_for_input = 1;
3209
3210 /* If interrupt_signal was called before and buffered a C-g,
3211 make it run again now, to avoid timing error. */
3212 if (!NILP (Vquit_flag))
3213 quit_throw_to_read_char ();
3214
3215 /* If alarm has gone off already, echo now. */
3216 if (echo_flag)
3217 {
3218 echo ();
3219 echo_flag = 0;
3220 }
3221}
3222
3223clear_waiting_for_input ()
3224{
3225 /* Tell interrupt_signal not to throw back to read_char, */
3226 waiting_for_input = 0;
ffd56f97 3227 input_available_clear_time = 0;
284f4730
JB
3228}
3229
3230/* This routine is called at interrupt level in response to C-G.
3231 If interrupt_input, this is the handler for SIGINT.
3232 Otherwise, it is called from kbd_buffer_store_event,
3233 in handling SIGIO or SIGTINT.
3234
3235 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
3236 immediately throw back to read_char.
3237
3238 Otherwise it sets the Lisp variable quit-flag not-nil.
3239 This causes eval to throw, when it gets a chance.
3240 If quit-flag is already non-nil, it stops the job right away. */
3241
3242SIGTYPE
3243interrupt_signal ()
3244{
3245 char c;
3246 /* Must preserve main program's value of errno. */
3247 int old_errno = errno;
3248 extern Lisp_Object Vwindow_system;
3249
3250#ifdef USG
3251 /* USG systems forget handlers when they are used;
3252 must reestablish each time */
3253 signal (SIGINT, interrupt_signal);
3254 signal (SIGQUIT, interrupt_signal);
3255#endif /* USG */
3256
3257 cancel_echoing ();
3258
3259 if (!NILP (Vquit_flag) && SCREEN_IS_TERMCAP (selected_screen))
3260 {
3261 fflush (stdout);
3262 reset_sys_modes ();
3263 sigfree ();
3264#ifdef SIGTSTP /* Support possible in later USG versions */
3265/*
3266 * On systems which can suspend the current process and return to the original
3267 * shell, this command causes the user to end up back at the shell.
3268 * The "Auto-save" and "Abort" questions are not asked until
3269 * the user elects to return to emacs, at which point he can save the current
3270 * job and either dump core or continue.
3271 */
3272 sys_suspend ();
3273#else
3274#ifdef VMS
3275 if (sys_suspend () == -1)
3276 {
3277 printf ("Not running as a subprocess;\n");
3278 printf ("you can continue or abort.\n");
3279 }
3280#else /* not VMS */
3281 /* Perhaps should really fork an inferior shell?
3282 But that would not provide any way to get back
3283 to the original shell, ever. */
3284 printf ("No support for stopping a process on this operating system;\n");
3285 printf ("you can continue or abort.\n");
3286#endif /* not VMS */
3287#endif /* not SIGTSTP */
3288 printf ("Auto-save? (y or n) ");
3289 fflush (stdout);
3290 if (((c = getchar ()) & ~040) == 'Y')
3291 Fdo_auto_save (Qnil, Qnil);
3292 while (c != '\n') c = getchar ();
3293#ifdef VMS
3294 printf ("Abort (and enter debugger)? (y or n) ");
3295#else /* not VMS */
3296 printf ("Abort (and dump core)? (y or n) ");
3297#endif /* not VMS */
3298 fflush (stdout);
3299 if (((c = getchar ()) & ~040) == 'Y')
3300 abort ();
3301 while (c != '\n') c = getchar ();
3302 printf ("Continuing...\n");
3303 fflush (stdout);
3304 init_sys_modes ();
3305 }
3306 else
3307 {
3308 /* If executing a function that wants to be interrupted out of
3309 and the user has not deferred quitting by binding `inhibit-quit'
3310 then quit right away. */
3311 if (immediate_quit && NILP (Vinhibit_quit))
3312 {
3313 immediate_quit = 0;
3314 sigfree ();
3315 Fsignal (Qquit, Qnil);
3316 }
3317 else
3318 /* Else request quit when it's safe */
3319 Vquit_flag = Qt;
3320 }
3321
3322 if (waiting_for_input && !echoing)
3323 quit_throw_to_read_char ();
3324
3325 errno = old_errno;
3326}
3327
3328/* Handle a C-g by making read_char return C-g. */
3329
3330quit_throw_to_read_char ()
3331{
3332 quit_error_check ();
3333 sigfree ();
3334 /* Prevent another signal from doing this before we finish. */
f76475ad 3335 clear_waiting_for_input ();
284f4730
JB
3336 input_pending = 0;
3337
3338#if 0
3339 unread_command_char = make_number (-1);
3340#endif
3341 unread_command_char = Qnil;
3342
3343 _longjmp (getcjmp, 1);
3344}
3345\f
3346DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
3347 "Set mode of reading keyboard input.\n\
3348First arg non-nil means use input interrupts; nil means use CBREAK mode.\n\
3349Second arg non-nil means use ^S/^Q flow control for output to terminal\n\
3350 (no effect except in CBREAK mode).\n\
3351Third arg non-nil means accept 8-bit input (for a Meta key).\n\
3352 Otherwise, the top bit is ignored, on the assumption it is parity.\n\
3353Optional fourth arg non-nil specifies character to use for quitting.")
3354 (interrupt, flow, meta, quit)
3355 Lisp_Object interrupt, flow, meta, quit;
3356{
3357 if (!NILP (quit)
3358 && (XTYPE (quit) != Lisp_Int
3359 || XINT (quit) < 0 || XINT (quit) > 0400))
3360 error ("set-input-mode: QUIT must be an ASCII character.");
3361
3362 reset_sys_modes ();
3363#ifdef SIGIO
3364/* Note SIGIO has been undef'd if FIONREAD is missing. */
3365#ifdef NO_SOCK_SIGIO
3366 if (read_socket_hook)
3367 interrupt_input = 0; /* No interrupts if reading from a socket. */
3368 else
3369#endif /* NO_SOCK_SIGIO */
3370 interrupt_input = !NILP (interrupt);
3371#else /* not SIGIO */
3372 interrupt_input = 0;
3373#endif /* not SIGIO */
3374/* Our VMS input only works by interrupts, as of now. */
3375#ifdef VMS
3376 interrupt_input = 1;
3377#endif
3378 flow_control = !NILP (flow);
3379 meta_key = !NILP (meta);
3380 if (!NILP (quit))
3381 /* Don't let this value be out of range. */
3382 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
3383
3384 init_sys_modes ();
3385 return Qnil;
3386}
3387\f
3388init_keyboard ()
3389{
3390 this_command_keys_size = 40;
3391 this_command_keys =
3392 (Lisp_Object *) xmalloc (this_command_keys_size * sizeof (Lisp_Object));
3393
3394 /* This is correct before outermost invocation of the editor loop */
3395 command_loop_level = -1;
3396 immediate_quit = 0;
3397 quit_char = Ctl ('g');
3398 unread_command_char = Qnil;
3399 recent_keys_index = 0;
3400 total_keys = 0;
3401 kbd_fetch_ptr = kbd_buffer;
3402 kbd_store_ptr = kbd_buffer;
3403 do_mouse_tracking = 0;
3404 input_pending = 0;
3405
3406 if (!noninteractive)
3407 {
3408 signal (SIGINT, interrupt_signal);
3409#ifdef HAVE_TERMIO
3410 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
3411 SIGQUIT and we can't tell which one it will give us. */
3412 signal (SIGQUIT, interrupt_signal);
3413#endif /* HAVE_TERMIO */
3414/* Note SIGIO has been undef'd if FIONREAD is missing. */
3415#ifdef SIGIO
3416 signal (SIGIO, input_available_signal);
3417#endif SIGIO
3418 }
3419
3420/* Use interrupt input by default, if it works and noninterrupt input
3421 has deficiencies. */
3422
3423#ifdef INTERRUPT_INPUT
3424 interrupt_input = 1;
3425#else
3426 interrupt_input = 0;
3427#endif
3428
3429/* Our VMS input only works by interrupts, as of now. */
3430#ifdef VMS
3431 interrupt_input = 1;
3432#endif
3433
3434 sigfree ();
3435 dribble = 0;
3436
3437 if (keyboard_init_hook)
3438 (*keyboard_init_hook) ();
3439
3440#ifdef POLL_FOR_INPUT
3441 poll_suppress_count = 1;
3442 start_polling ();
3443#endif
3444}
3445
3446/* This type's only use is in syms_of_keyboard, to initialize the
3447 event header symbols and put properties on them. */
3448struct event_head {
3449 Lisp_Object *var;
3450 char *name;
3451 Lisp_Object *kind;
3452};
3453
3454struct event_head head_table[] = {
3455 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
3456 &Qvscrollbar_part, "vscrollbar-part", &Qscrollbar_click,
3457 &Qvslider_part, "vslider-part", &Qscrollbar_click,
3458 &Qvthumbup_part, "vthumbup-part", &Qscrollbar_click,
3459 &Qvthumbdown_part, "vthumbdown-part", &Qscrollbar_click,
3460 &Qhscrollbar_part, "hscrollbar-part", &Qscrollbar_click,
3461 &Qhslider_part, "hslider-part", &Qscrollbar_click,
3462 &Qhthumbleft_part, "hthumbleft-part", &Qscrollbar_click,
3463 &Qhthumbright_part,"hthumbright-part", &Qscrollbar_click
3464};
3465
3466syms_of_keyboard ()
3467{
3468 Qself_insert_command = intern ("self-insert-command");
3469 staticpro (&Qself_insert_command);
3470
3471 Qforward_char = intern ("forward-char");
3472 staticpro (&Qforward_char);
3473
3474 Qbackward_char = intern ("backward-char");
3475 staticpro (&Qbackward_char);
3476
3477 Qdisabled = intern ("disabled");
3478 staticpro (&Qdisabled);
3479
3480 Qfunction_key = intern ("function-key");
3481 staticpro (&Qfunction_key);
3482 Qmouse_movement = intern ("mouse-click");
3483 staticpro (&Qmouse_click);
3484 Qmouse_movement = intern ("scrollbar-click");
3485 staticpro (&Qmouse_movement);
3486
3487 Qmode_line = intern ("mode-line");
3488 staticpro (&Qmode_line);
3489 Qvertical_split = intern ("vertical-split");
3490 staticpro (&Qvertical_split);
3491
3492 Qevent_kind = intern ("event-type");
3493 staticpro (&Qevent_kind);
3494 Qevent_unmodified = intern ("event-unmodified");
3495 staticpro (&Qevent_unmodified);
3496
3497 {
3498 struct event_head *p;
3499
3500 for (p = head_table;
3501 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
3502 p++)
3503 {
3504 *p->var = intern (p->name);
3505 staticpro (p->var);
3506 Fput (*p->var, Qevent_kind, *p->kind);
3507 Fput (*p->var, Qevent_unmodified, *p->var);
3508 }
3509 }
3510
3511 func_key_syms = Qnil;
3512 staticpro (&func_key_syms);
3513
3514 mouse_syms = Qnil;
3515 staticpro (&mouse_syms);
3516
3517 defsubr (&Sread_key_sequence);
3518 defsubr (&Srecursive_edit);
3519 defsubr (&Strack_mouse);
3520 defsubr (&Smouse_click_p);
3521 defsubr (&Sinput_pending_p);
3522 defsubr (&Scommand_execute);
3523 defsubr (&Srecent_keys);
3524 defsubr (&Sthis_command_keys);
3525 defsubr (&Ssuspend_emacs);
3526 defsubr (&Sabort_recursive_edit);
3527 defsubr (&Sexit_recursive_edit);
3528 defsubr (&Srecursion_depth);
3529 defsubr (&Stop_level);
3530 defsubr (&Sdiscard_input);
3531 defsubr (&Sopen_dribble_file);
3532 defsubr (&Sset_input_mode);
3533 defsubr (&Sexecute_extended_command);
3534
3535 DEFVAR_LISP ("disabled-command-hook", &Vdisabled_command_hook,
3536 "Value is called instead of any command that is disabled\n\
3537(has a non-nil `disabled' property).");
3538
3539 DEFVAR_LISP ("last-command-char", &last_command_char,
3540 "Last terminal input key that was part of a command.");
3541
3542 DEFVAR_LISP ("last-input-char", &last_input_char,
3543 "Last terminal input key.");
3544
3545 DEFVAR_LISP ("unread-command-char", &unread_command_char,
3546 "Object to be read as next input from input stream, or nil if none.");
3547
3548 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
3549 "Meta-prefix character code. Meta-foo as command input\n\
3550turns into this character followed by foo.");
3551 XSET (meta_prefix_char, Lisp_Int, 033);
3552
3553 DEFVAR_LISP ("last-command", &last_command,
3554 "The last command executed. Normally a symbol with a function definition,\n\
3555but can be whatever was found in the keymap, or whatever the variable\n\
3556`this-command' was set to by that command.");
3557 last_command = Qnil;
3558
3559 DEFVAR_LISP ("this-command", &this_command,
3560 "The command now being executed.\n\
3561The command can set this variable; whatever is put here\n\
3562will be in `last-command' during the following command.");
3563 this_command = Qnil;
3564
3565 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
3566 "*Number of keyboard input characters between auto-saves.\n\
3567Zero means disable autosaving due to number of characters typed.");
3568 auto_save_interval = 300;
3569
3570 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
3571 "*Number of seconds idle time before auto-save.\n\
06ef7355
RS
3572Zero or nil means disable auto-saving due to idleness.\n\
3573After auto-saving due to this many seconds of idle time,\n\
3574Emacs also does a garbage collection if that seems to be warranted."
284f4730
JB
3575 XFASTINT (Vauto_save_timeout) = 30;
3576
3577 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
3578 "*Nonzero means echo unfinished commands after this many seconds of pause.");
3579 echo_keystrokes = 1;
3580
3581 DEFVAR_INT ("polling-period", &polling_period,
3582 "*Interval between polling for input during Lisp execution.\n\
3583The reason for polling is to make C-g work to stop a running program.\n\
3584Polling is needed only when using X windows and SIGIO does not work.\n\
3585Polling is automatically disabled in all other cases.");
3586 polling_period = 2;
3587
3588 DEFVAR_INT ("num-input-keys", &num_input_keys,
3589 "*Number of complete keys read from the keyboard so far.");
3590 num_input_keys = 0;
3591
3592 DEFVAR_LISP ("last-event-screen", &Vlast_event_screen,
3593 "*The screen in which the most recently read event occurred.");
3594 Vlast_event_screen = Qnil;
3595
3596 DEFVAR_LISP ("help-char", &help_char,
3597 "Character to recognize as meaning Help.\n\
3598When it is read, do `(eval help-form)', and display result if it's a string.\n\
3599If the value of `help-form' is nil, this char can be read normally.");
3600 XSET (help_char, Lisp_Int, Ctl ('H'));
3601
3602 DEFVAR_LISP ("help-form", &Vhelp_form,
3603 "Form to execute when character help-char is read.\n\
3604If the form returns a string, that string is displayed.\n\
3605If `help-form' is nil, the help char is not recognized.");
3606 Vhelp_form = Qnil;
3607
3608 DEFVAR_LISP ("top-level", &Vtop_level,
3609 "Form to evaluate when Emacs starts up.\n\
3610Useful to set before you dump a modified Emacs.");
3611 Vtop_level = Qnil;
3612
3613 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
3614 "String used as translate table for keyboard input, or nil.\n\
3615Each character is looked up in this string and the contents used instead.\n\
3616If string is of length N, character codes N and up are untranslated.");
3617 Vkeyboard_translate_table = Qnil;
3618
3619#ifdef HAVE_X_WINDOWS
3620 DEFVAR_LISP ("mouse-event-function", &Vmouse_event_function,
3621 "Function to call for each mouse event, after the event's definition.\n\
3622Called, if non-nil, with one argument, which is the event-list.\n\
3623See the variable `mouse-event' for the format of this list.");
3624 Vmouse_event_function = Qnil;
3625
3626 DEFVAR_LISP ("mouse-left-hook", &Vmouse_left_hook,
3627 "Function to call when mouse leaves window. No arguments.");
3628 Vmouse_left_hook = Qnil;
3629
3630 DEFVAR_LISP ("map-screen-hook", &Vmap_screen_hook,
3631 "Function to call when screen is mapped. No arguments.");
3632 Vmap_screen_hook = Qnil;
3633
3634 DEFVAR_LISP ("unmap-screen-hook", &Vunmap_screen_hook,
3635 "Function to call when screen is unmapped. No arguments.");
3636 Vunmap_screen_hook = Qnil;
3637
3638 DEFVAR_LISP ("mouse-motion-handler", &Vmouse_motion_handler,
3639 "Handler for motion events. No arguments.");
3640 Vmouse_motion_handler = Qnil;
3641#endif
3642
3643 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
3644 "Non-nil means prompt with menus in echo area when appropriate.\n\
3645This is done when reading from a keymap that has a prompt string,\n\
3646for elements that have prompt strings.");
3647 menu_prompting = 1;
3648
3649 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
3650 "Character to see next line of menu prompt.\n\
3651Type this character while in a menu prompt to rotate around the lines of it.");
3652 XSET (menu_prompt_more_char, Lisp_Int, ' ');
3653}
3654
3655keys_of_keyboard ()
3656{
3657 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
3658 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
3659 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
3660 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
3661 initial_define_key (meta_map, 'x', "execute-extended-command");
3662}