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