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