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