(syms_of_buffer): Make erase-buffer a disabled command.
[bpt/emacs.git] / src / keyboard.c
CommitLineData
284f4730 1/* Keyboard and mouse input; editor command loop.
86e5706b 2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993 Free Software Foundation, Inc.
284f4730
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
7b4aedb9 8the Free Software Foundation; either version 2, or (at your option)
284f4730
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20/* Allow config.h to undefine symbols found here. */
21#include <signal.h>
22
23#include "config.h"
24#include <stdio.h>
25#undef NULL
26#include "termchar.h"
27#include "termopts.h"
28#include "lisp.h"
29#include "termhooks.h"
30#include "macros.h"
ff11dfa1 31#include "frame.h"
284f4730
JB
32#include "window.h"
33#include "commands.h"
34#include "buffer.h"
35#include "disptab.h"
f4255cd1 36#include "dispextern.h"
284f4730
JB
37#include "keyboard.h"
38#include <setjmp.h>
39#include <errno.h>
40
284f4730
JB
41#ifndef VMS
42#include <sys/ioctl.h>
284f4730
JB
43#endif
44
52baf19e 45#include "syssignal.h"
6ef5b54f 46#include "systty.h"
ffd56f97 47#include "systime.h"
52baf19e
JB
48
49extern int errno;
50
284f4730
JB
51#ifdef HAVE_X_WINDOWS
52extern Lisp_Object Vmouse_grabbed;
53
54/* Make all keyboard buffers much bigger when using X windows. */
55#define KBD_BUFFER_SIZE 4096
56#else /* No X-windows, character input */
57#define KBD_BUFFER_SIZE 256
58#endif /* No X-windows */
59
60/* Following definition copied from eval.c */
61
62struct backtrace
63 {
64 struct backtrace *next;
65 Lisp_Object *function;
66 Lisp_Object *args; /* Points to vector of args. */
67 int nargs; /* length of vector. If nargs is UNEVALLED,
68 args points to slot holding list of
69 unevalled args */
70 char evalargs;
71 };
72
73/* Non-nil disable property on a command means
74 do not execute it; call disabled-command-hook's value instead. */
75Lisp_Object Qdisabled, Vdisabled_command_hook;
76
77#define NUM_RECENT_KEYS (100)
78int recent_keys_index; /* Index for storing next element into recent_keys */
79int total_keys; /* Total number of elements stored into recent_keys */
5160df46 80Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
284f4730 81
6569cc8d
JB
82/* Vector holding the key sequence that invoked the current command.
83 It is reused for each command, and it may be longer than the current
84 sequence; this_command_key_count indicates how many elements
85 actually mean something.
86 It's easier to staticpro a single Lisp_Object than an array. */
87Lisp_Object this_command_keys;
88int this_command_key_count;
284f4730
JB
89
90extern int minbuf_level;
91
92extern struct backtrace *backtrace_list;
93
94/* Nonzero means do menu prompting. */
95static int menu_prompting;
96
97/* Character to see next line of menu prompt. */
98static Lisp_Object menu_prompt_more_char;
99
100/* For longjmp to where kbd input is being done. */
101static jmp_buf getcjmp;
102
103/* True while doing kbd input. */
104int waiting_for_input;
105
106/* True while displaying for echoing. Delays C-g throwing. */
107static int echoing;
108
109/* Nonzero means C-G should cause immediate error-signal. */
110int immediate_quit;
111
112/* Character to recognize as the help char. */
113Lisp_Object help_char;
114
115/* Form to execute when help char is typed. */
116Lisp_Object Vhelp_form;
117
118/* Character that causes a quit. Normally C-g.
119
120 If we are running on an ordinary terminal, this must be an ordinary
121 ASCII char, since we want to make it our interrupt character.
122
123 If we are not running on an ordinary terminal, it still needs to be
124 an ordinary ASCII char. This character needs to be recognized in
125 the input interrupt handler. At this point, the keystroke is
126 represented as a struct input_event, while the desired quit
127 character is specified as a lispy event. The mapping from struct
128 input_events to lispy events cannot run in an interrupt handler,
129 and the reverse mapping is difficult for anything but ASCII
130 keystrokes.
131
132 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
133 ASCII character. */
134int quit_char;
135
136extern Lisp_Object current_global_map;
137extern int minibuf_level;
138
139/* Current depth in recursive edits. */
140int command_loop_level;
141
142/* Total number of times command_loop has read a key sequence. */
143int num_input_keys;
144
145/* Last input character read as a command. */
146Lisp_Object last_command_char;
147
7d6de002
RS
148/* Last input character read as a command, not counting menus
149 reached by the mouse. */
150Lisp_Object last_nonmenu_event;
151
284f4730
JB
152/* Last input character read for any purpose. */
153Lisp_Object last_input_char;
154
dbc4e1c1
JB
155/* If not Qnil, a list of objects to be read as subsequent command input. */
156Lisp_Object unread_command_events;
284f4730 157
86e5706b
RS
158/* If not -1, an event to be read as subsequent command input. */
159int unread_command_char;
160
cd21b839
JB
161/* If not Qnil, this is a switch-frame event which we decided to put
162 off until the end of a key sequence. This should be read as the
dbc4e1c1 163 next command input, after any unread_command_events.
8f805655
JB
164
165 read_key_sequence uses this to delay switch-frame events until the
166 end of the key sequence; Fread_char uses it to put off switch-frame
167 events until a non-ASCII event is acceptable as input. */
168Lisp_Object unread_switch_frame;
cd21b839 169
9fa4395d
RS
170/* A mask of extra modifier bits to put into every keyboard char. */
171int extra_keyboard_modifiers;
172
284f4730
JB
173/* Char to use as prefix when a meta character is typed in.
174 This is bound on entry to minibuffer in case ESC is changed there. */
175
176Lisp_Object meta_prefix_char;
177
178/* Last size recorded for a current buffer which is not a minibuffer. */
179static int last_non_minibuf_size;
180
06ef7355 181/* Number of idle seconds before an auto-save and garbage collection. */
284f4730
JB
182static Lisp_Object Vauto_save_timeout;
183
184/* Total number of times read_char has returned. */
185int num_input_chars;
186
51172b6d
RS
187/* Total number of times read_char has returned, outside of macros. */
188int num_nonmacro_input_chars;
189
284f4730
JB
190/* Auto-save automatically when this many characters have been typed
191 since the last time. */
192
193static int auto_save_interval;
194
51172b6d 195/* Value of num_nonmacro_input_chars as of last auto save. */
284f4730
JB
196
197int last_auto_save;
198
199/* Last command executed by the editor command loop, not counting
200 commands that set the prefix argument. */
201
202Lisp_Object last_command;
203
204/* The command being executed by the command loop.
205 Commands may set this, and the value set will be copied into last_command
206 instead of the actual command. */
207Lisp_Object this_command;
208
07d2b8de 209#ifdef MULTI_FRAME
fce33686 210/* The frame in which the last input event occurred, or Qmacro if the
3c370943
JB
211 last event came from a macro. We use this to determine when to
212 generate switch-frame events. This may be cleared by functions
213 like Fselect_frame, to make sure that a switch-frame event is
214 generated by the next character. */
215Lisp_Object internal_last_event_frame;
216
217/* A user-visible version of the above, intended to allow users to
218 figure out where the last event came from, if the event doesn't
219 carry that information itself (i.e. if it was a character). */
ff11dfa1 220Lisp_Object Vlast_event_frame;
07d2b8de 221#endif
284f4730 222
1113d9db
JB
223/* The timestamp of the last input event we received from the X server.
224 X Windows wants this for selection ownership. */
284f4730
JB
225unsigned long last_event_timestamp;
226
227Lisp_Object Qself_insert_command;
228Lisp_Object Qforward_char;
229Lisp_Object Qbackward_char;
230
231/* read_key_sequence stores here the command definition of the
232 key sequence that it reads. */
233Lisp_Object read_key_sequence_cmd;
234
235/* Form to evaluate (if non-nil) when Emacs is started. */
236Lisp_Object Vtop_level;
237
238/* User-supplied string to translate input characters through. */
239Lisp_Object Vkeyboard_translate_table;
240
241/* Keymap mapping ASCII function key sequences onto their preferred forms. */
242extern Lisp_Object Vfunction_key_map;
243
86e5706b
RS
244/* Non-nil means deactivate the mark at end of this command. */
245Lisp_Object Vdeactivate_mark;
246
247/* Hooks to run before and after each command. */
248Lisp_Object Qpre_command_hook, Qpost_command_hook;
249Lisp_Object Vpre_command_hook, Vpost_command_hook;
250
284f4730
JB
251/* File in which we write all commands we read. */
252FILE *dribble;
253
254/* Nonzero if input is available. */
255int input_pending;
256
257/* Nonzero if should obey 0200 bit in input chars as "Meta". */
258int meta_key;
259
260extern char *pending_malloc_warning;
261
262/* Circular buffer for pre-read keyboard input. */
263static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
264
7b4aedb9 265/* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
4bb994d1 266
7b4aedb9
JB
267 The interrupt-level event handlers will never enqueue an event on a
268 frame which is not in Vframe_list, and once an event is dequeued,
3c370943
JB
269 internal_last_event_frame or the event itself points to the frame.
270 So that's all fine.
4bb994d1
JB
271
272 But while the event is sitting in the queue, it's completely
273 unprotected. Suppose the user types one command which will run for
274 a while and then delete a frame, and then types another event at
275 the frame that will be deleted, before the command gets around to
276 it. Suppose there are no references to this frame elsewhere in
277 Emacs, and a GC occurs before the second event is dequeued. Now we
278 have an event referring to a freed frame, which will crash Emacs
279 when it is dequeued.
280
3c370943 281 Similar things happen when an event on a scroll bar is enqueued; the
7b4aedb9 282 window may be deleted while the event is in the queue.
4bb994d1 283
7b4aedb9
JB
284 So, we use this vector to protect the frame_or_window field in the
285 event queue. That way, they'll be dequeued as dead frames or
286 windows, but still valid lisp objects.
287
288 If kbd_buffer[i].kind != no_event, then
289 (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
290 == kbd_buffer[i].frame_or_window. */
291static Lisp_Object kbd_buffer_frame_or_window;
4bb994d1 292
284f4730
JB
293/* Pointer to next available character in kbd_buffer.
294 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
295 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
296 next available char is in kbd_buffer[0]. */
297static struct input_event *kbd_fetch_ptr;
298
299/* Pointer to next place to store character in kbd_buffer. This
300 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
301 character should go in kbd_buffer[0]. */
ff0b5f4c
JB
302#ifdef __STDC__
303volatile
304#endif
284f4730
JB
305static struct input_event *kbd_store_ptr;
306
307/* The above pair of variables forms a "queue empty" flag. When we
308 enqueue a non-hook event, we increment kbd_write_count. When we
309 dequeue a non-hook event, we increment kbd_read_count. We say that
4bb994d1 310 there is input available iff the two counters are not equal.
284f4730
JB
311
312 Why not just have a flag set and cleared by the enqueuing and
313 dequeuing functions? Such a flag could be screwed up by interrupts
314 at inopportune times. */
315
4bb994d1
JB
316/* If this flag is non-zero, we will check mouse_moved to see when the
317 mouse moves, and motion events will appear in the input stream. If
318 it is zero, mouse motion will be ignored. */
284f4730
JB
319int do_mouse_tracking;
320
321/* The window system handling code should set this if the mouse has
322 moved since the last call to the mouse_position_hook. Calling that
323 hook should clear this. Code assumes that if this is set, it can
324 call mouse_position_hook to get the promised position, so don't set
325 it unless you're prepared to substantiate the claim! */
326int mouse_moved;
327
328/* True iff there is an event in kbd_buffer, or if mouse tracking is
329 enabled and there is a new mouse position in the mouse movement
330 buffer. Note that if this is false, that doesn't mean that there
331 is readable input; all the events in the queue might be button-up
332 events, and do_mouse_tracking might be off. */
333#define EVENT_QUEUES_EMPTY \
334 ((kbd_fetch_ptr == kbd_store_ptr) && (!do_mouse_tracking || !mouse_moved))
335
336
337/* Symbols to head events. */
338Lisp_Object Qmouse_movement;
3c370943 339Lisp_Object Qscroll_bar_movement;
284f4730 340
cd21b839
JB
341Lisp_Object Qswitch_frame;
342
284f4730
JB
343/* Symbols to denote kinds of events. */
344Lisp_Object Qfunction_key;
345Lisp_Object Qmouse_click;
346/* Lisp_Object Qmouse_movement; - also an event header */
284f4730
JB
347
348/* Properties of event headers. */
349Lisp_Object Qevent_kind;
88cb0656 350Lisp_Object Qevent_symbol_elements;
284f4730 351
0a7f1fc0
JB
352/* An event header symbol HEAD may have a property named
353 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
354 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
355 mask of modifiers applied to it. If present, this is used to help
356 speed up parse_modifiers. */
357Lisp_Object Qevent_symbol_element_mask;
358
359/* An unmodified event header BASE may have a property named
360 Qmodifier_cache, which is an alist mapping modifier masks onto
361 modified versions of BASE. If present, this helps speed up
362 apply_modifiers. */
363Lisp_Object Qmodifier_cache;
364
284f4730
JB
365/* Symbols to use for non-text mouse positions. */
366Lisp_Object Qmode_line;
e5d77022 367Lisp_Object Qvertical_line;
3c370943 368Lisp_Object Qvertical_scroll_bar;
284f4730 369
f4255cd1
JB
370Lisp_Object recursive_edit_unwind (), command_loop ();
371Lisp_Object Fthis_command_keys ();
284f4730 372
ffd56f97
JB
373/* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
374 happens. */
375EMACS_TIME *input_available_clear_time;
284f4730
JB
376
377/* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
378 Default is 1 if INTERRUPT_INPUT is defined. */
379int interrupt_input;
380
381/* Nonzero while interrupts are temporarily deferred during redisplay. */
382int interrupts_deferred;
383
384/* nonzero means use ^S/^Q for flow control. */
385int flow_control;
386
284f4730
JB
387/* Allow m- file to inhibit use of FIONREAD. */
388#ifdef BROKEN_FIONREAD
389#undef FIONREAD
390#endif
391
392/* We are unable to use interrupts if FIONREAD is not available,
393 so flush SIGIO so we won't try. */
394#ifndef FIONREAD
395#ifdef SIGIO
396#undef SIGIO
397#endif
398#endif
399
400/* If we support X Windows, and won't get an interrupt when input
401 arrives from the server, poll periodically so we can detect C-g. */
402#ifdef HAVE_X_WINDOWS
403#ifndef SIGIO
404#define POLL_FOR_INPUT
405#endif
406#endif
407\f
408/* Global variable declarations. */
409
410/* Function for init_keyboard to call with no args (if nonzero). */
411void (*keyboard_init_hook) ();
412
413static int read_avail_input ();
414static void get_input_pending ();
415
416/* > 0 if we are to echo keystrokes. */
417static int echo_keystrokes;
418
419/* Nonzero means echo each character as typed. */
420static int immediate_echo;
421
422/* The text we're echoing in the modeline - partial key sequences,
f4255cd1
JB
423 usually. '\0'-terminated. This really shouldn't have a fixed size. */
424static char echobuf[300];
284f4730
JB
425
426/* Where to append more text to echobuf if we want to. */
427static char *echoptr;
428
429#define min(a,b) ((a)<(b)?(a):(b))
430#define max(a,b) ((a)>(b)?(a):(b))
431
432/* Install the string STR as the beginning of the string of echoing,
433 so that it serves as a prompt for the next character.
434 Also start echoing. */
435
436echo_prompt (str)
437 char *str;
438{
439 int len = strlen (str);
440 if (len > sizeof echobuf - 4)
441 len = sizeof echobuf - 4;
0a7f1fc0 442 bcopy (str, echobuf, len);
284f4730 443 echoptr = echobuf + len;
0a7f1fc0 444 *echoptr = '\0';
284f4730
JB
445
446 echo ();
447}
448
449/* Add C to the echo string, if echoing is going on.
450 C can be a character, which is printed prettily ("M-C-x" and all that
451 jazz), or a symbol, whose name is printed. */
452
453echo_char (c)
454 Lisp_Object c;
455{
456 extern char *push_key_description ();
457
458 if (immediate_echo)
459 {
460 char *ptr = echoptr;
461
462 if (ptr != echobuf)
463 *ptr++ = ' ';
464
465 /* If someone has passed us a composite event, use its head symbol. */
88cb0656 466 c = EVENT_HEAD (c);
284f4730
JB
467
468 if (XTYPE (c) == Lisp_Int)
469 {
470 if (ptr - echobuf > sizeof echobuf - 6)
471 return;
472
cb5df6ae 473 ptr = push_key_description (XINT (c), ptr);
284f4730
JB
474 }
475 else if (XTYPE (c) == Lisp_Symbol)
476 {
477 struct Lisp_String *name = XSYMBOL (c)->name;
478 if (((ptr - echobuf) + name->size + 4) > sizeof echobuf)
479 return;
480 bcopy (name->data, ptr, name->size);
481 ptr += name->size;
482 }
483
f4255cd1 484 if (echoptr == echobuf && EQ (c, help_char))
284f4730
JB
485 {
486 strcpy (ptr, " (Type ? for further options)");
487 ptr += strlen (ptr);
488 }
489
490 *ptr = 0;
491 echoptr = ptr;
492
493 echo ();
494 }
495}
496
497/* Temporarily add a dash to the end of the echo string if it's not
498 empty, so that it serves as a mini-prompt for the very next character. */
499
500echo_dash ()
501{
502 if (!immediate_echo && echoptr == echobuf)
503 return;
504
505 /* Put a dash at the end of the buffer temporarily,
506 but make it go away when the next character is added. */
507 echoptr[0] = '-';
508 echoptr[1] = 0;
509
510 echo ();
511}
512
513/* Display the current echo string, and begin echoing if not already
514 doing so. */
515
516echo ()
517{
518 if (!immediate_echo)
519 {
520 int i;
521 immediate_echo = 1;
522
523 for (i = 0; i < this_command_key_count; i++)
6569cc8d 524 echo_char (XVECTOR (this_command_keys)->contents[i]);
284f4730
JB
525 echo_dash ();
526 }
527
528 echoing = 1;
529 message1 (echobuf);
530 echoing = 0;
531
532 if (waiting_for_input && !NILP (Vquit_flag))
533 quit_throw_to_read_char ();
534}
535
536/* Turn off echoing, for the start of a new command. */
537
538cancel_echoing ()
539{
540 immediate_echo = 0;
541 echoptr = echobuf;
542}
543
544/* Return the length of the current echo string. */
545
546static int
547echo_length ()
548{
549 return echoptr - echobuf;
550}
551
552/* Truncate the current echo message to its first LEN chars.
553 This and echo_char get used by read_key_sequence when the user
ff11dfa1 554 switches frames while entering a key sequence. */
284f4730
JB
555
556static void
557echo_truncate (len)
558 int len;
559{
560 echobuf[len] = '\0';
0a7f1fc0 561 echoptr = echobuf + len;
284f4730
JB
562}
563
564\f
565/* Functions for manipulating this_command_keys. */
566static void
567add_command_key (key)
568 Lisp_Object key;
569{
6569cc8d
JB
570 int size = XVECTOR (this_command_keys)->size;
571
572 if (this_command_key_count >= size)
284f4730 573 {
6569cc8d
JB
574 Lisp_Object new_keys = Fmake_vector (make_number (size * 2), Qnil);
575
576 bcopy (XVECTOR (this_command_keys)->contents,
577 XVECTOR (new_keys)->contents,
8f805655 578 size * sizeof (Lisp_Object));
6569cc8d
JB
579
580 this_command_keys = new_keys;
284f4730 581 }
6569cc8d
JB
582
583 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
284f4730
JB
584}
585\f
586Lisp_Object
587recursive_edit_1 ()
588{
589 int count = specpdl_ptr - specpdl;
590 Lisp_Object val;
591
592 if (command_loop_level > 0)
593 {
594 specbind (Qstandard_output, Qt);
595 specbind (Qstandard_input, Qt);
596 }
597
598 val = command_loop ();
599 if (EQ (val, Qt))
600 Fsignal (Qquit, Qnil);
601
cb5df6ae 602 return unbind_to (count, Qnil);
284f4730
JB
603}
604
605/* When an auto-save happens, record the "time", and don't do again soon. */
606record_auto_save ()
607{
51172b6d 608 last_auto_save = num_nonmacro_input_chars;
284f4730
JB
609}
610\f
284f4730
JB
611DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
612 "Invoke the editor command loop recursively.\n\
613To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
614that tells this function to return.\n\
615Alternately, `(throw 'exit t)' makes this function signal an error.\n\
616This function is called by the editor initialization to begin editing.")
617 ()
618{
619 int count = specpdl_ptr - specpdl;
620 Lisp_Object val;
621
622 command_loop_level++;
623 update_mode_lines = 1;
624
625 record_unwind_protect (recursive_edit_unwind,
626 (command_loop_level
627 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
628 ? Fcurrent_buffer ()
629 : Qnil);
630 recursive_edit_1 ();
631 return unbind_to (count, Qnil);
632}
633
634Lisp_Object
635recursive_edit_unwind (buffer)
636 Lisp_Object buffer;
637{
638 if (!NILP (buffer))
639 Fset_buffer (buffer);
640
641 command_loop_level--;
642 update_mode_lines = 1;
643 return Qnil;
644}
645\f
646Lisp_Object
647cmd_error (data)
648 Lisp_Object data;
649{
650 Lisp_Object errmsg, tail, errname, file_error;
651 Lisp_Object stream;
652 struct gcpro gcpro1;
653 int i;
654
655 Vquit_flag = Qnil;
656 Vinhibit_quit = Qt;
657 Vstandard_output = Qt;
658 Vstandard_input = Qt;
659 Vexecuting_macro = Qnil;
660 echo_area_glyphs = 0;
661
ff11dfa1 662 /* If the window system or terminal frame hasn't been initialized
284f4730
JB
663 yet, or we're not interactive, it's best to dump this message out
664 to stderr and exit. */
ff11dfa1 665 if (! FRAME_MESSAGE_BUF (selected_frame)
284f4730
JB
666 || noninteractive)
667 stream = Qexternal_debugging_output;
668 else
669 {
670 Fdiscard_input ();
671 bitch_at_user ();
672 stream = Qt;
673 }
674
675 errname = Fcar (data);
676
677 if (EQ (errname, Qerror))
678 {
679 data = Fcdr (data);
680 if (!CONSP (data)) data = Qnil;
681 errmsg = Fcar (data);
682 file_error = Qnil;
683 }
684 else
685 {
686 errmsg = Fget (errname, Qerror_message);
687 file_error = Fmemq (Qfile_error,
688 Fget (errname, Qerror_conditions));
689 }
690
691 /* Print an error message including the data items.
692 This is done by printing it into a scratch buffer
693 and then making a copy of the text in the buffer. */
694
695 if (!CONSP (data)) data = Qnil;
696 tail = Fcdr (data);
697 GCPRO1 (tail);
698
699 /* For file-error, make error message by concatenating
700 all the data items. They are all strings. */
701 if (!NILP (file_error) && !NILP (tail))
702 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
703
704 if (XTYPE (errmsg) == Lisp_String)
705 Fprinc (errmsg, stream);
706 else
707 write_string_1 ("peculiar error", -1, stream);
708
709 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
710 {
711 write_string_1 (i ? ", " : ": ", 2, stream);
712 if (!NILP (file_error))
713 Fprinc (Fcar (tail), stream);
714 else
715 Fprin1 (Fcar (tail), stream);
716 }
717 UNGCPRO;
718
ff11dfa1 719 /* If the window system or terminal frame hasn't been initialized
284f4730 720 yet, or we're in -batch mode, this error should cause Emacs to exit. */
ff11dfa1 721 if (! FRAME_MESSAGE_BUF (selected_frame)
284f4730
JB
722 || noninteractive)
723 {
724 Fterpri (stream);
725 Fkill_emacs (make_number (-1));
726 }
727
728 Vquit_flag = Qnil;
729
730 Vinhibit_quit = Qnil;
731 return make_number (0);
732}
733\f
734Lisp_Object command_loop_1 ();
735Lisp_Object command_loop_2 ();
736Lisp_Object top_level_1 ();
737
738/* Entry to editor-command-loop.
739 This level has the catches for exiting/returning to editor command loop.
740 It returns nil to exit recursive edit, t to abort it. */
741
742Lisp_Object
743command_loop ()
744{
745 if (command_loop_level > 0 || minibuf_level > 0)
746 {
747 return internal_catch (Qexit, command_loop_2, Qnil);
748 }
749 else
750 while (1)
751 {
752 internal_catch (Qtop_level, top_level_1, Qnil);
753 internal_catch (Qtop_level, command_loop_2, Qnil);
754
755 /* End of file in -batch run causes exit here. */
756 if (noninteractive)
757 Fkill_emacs (Qt);
758 }
759}
760
761/* Here we catch errors in execution of commands within the
762 editing loop, and reenter the editing loop.
763 When there is an error, cmd_error runs and returns a non-nil
764 value to us. A value of nil means that cmd_loop_1 itself
765 returned due to end of file (or end of kbd macro). */
766
767Lisp_Object
768command_loop_2 ()
769{
770 register Lisp_Object val;
771
772 do
773 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
774 while (!NILP (val));
775
776 return Qnil;
777}
778
779Lisp_Object
780top_level_2 ()
781{
782 return Feval (Vtop_level);
783}
784
785Lisp_Object
786top_level_1 ()
787{
788 /* On entry to the outer level, run the startup file */
789 if (!NILP (Vtop_level))
790 internal_condition_case (top_level_2, Qerror, cmd_error);
791 else if (!NILP (Vpurify_flag))
792 message ("Bare impure Emacs (standard Lisp code not loaded)");
793 else
794 message ("Bare Emacs (standard Lisp code not loaded)");
795 return Qnil;
796}
797
798DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
799 "Exit all recursive editing levels.")
800 ()
801{
802 Fthrow (Qtop_level, Qnil);
803}
804
805DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
806 "Exit from the innermost recursive edit or minibuffer.")
807 ()
808{
809 if (command_loop_level > 0 || minibuf_level > 0)
810 Fthrow (Qexit, Qnil);
811
812 error ("No recursive edit is in progress");
813}
814
815DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
816 "Abort the command that requested this recursive edit or minibuffer input.")
817 ()
818{
819 if (command_loop_level > 0 || minibuf_level > 0)
820 Fthrow (Qexit, Qt);
821
822 error ("No recursive edit is in progress");
823}
824\f
825/* This is the actual command reading loop,
826 sans error-handling encapsulation. */
827
828Lisp_Object Fcommand_execute ();
829static int read_key_sequence ();
830
831Lisp_Object
832command_loop_1 ()
833{
834 Lisp_Object cmd;
835 int lose;
836 int nonundocount;
837 Lisp_Object keybuf[30];
838 int i;
839 int no_redisplay;
840 int no_direct;
86e5706b
RS
841 int prev_modiff;
842 struct buffer *prev_buffer;
284f4730
JB
843
844 Vprefix_arg = Qnil;
86e5706b 845 Vdeactivate_mark = Qnil;
284f4730
JB
846 waiting_for_input = 0;
847 cancel_echoing ();
848
849 /* Don't clear out last_command at the beginning of a macro. */
850 if (XTYPE (Vexecuting_macro) != Lisp_String)
851 last_command = Qt;
852
853 nonundocount = 0;
854 no_redisplay = 0;
855 this_command_key_count = 0;
856
857 while (1)
858 {
859 /* Install chars successfully executed in kbd macro. */
860
861 if (defining_kbd_macro && NILP (Vprefix_arg))
862 finalize_kbd_macro_chars ();
863
864 /* Make sure the current window's buffer is selected. */
865 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
866 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
867
868 /* Display any malloc warning that just came out. Use while because
869 displaying one warning can cause another. */
870
871 while (pending_malloc_warning)
872 display_malloc_warning ();
873
874 no_direct = 0;
875
86e5706b
RS
876 Vdeactivate_mark = Qnil;
877
284f4730
JB
878 /* If minibuffer on and echo area in use,
879 wait 2 sec and redraw minibufer. */
880
881 if (minibuf_level && echo_area_glyphs)
882 {
f1bed6d8
RS
883 /* Bind inhibit-quit to t so that C-g gets read in
884 rather than quitting back to the minibuffer. */
885 int count = specpdl_ptr - specpdl;
886 specbind (Qinhibit_quit, Qt);
284f4730 887 Fsit_for (make_number (2), Qnil, Qnil);
cb5df6ae 888 unbind_to (count, Qnil);
f1bed6d8 889
284f4730
JB
890 echo_area_glyphs = 0;
891 no_direct = 1;
892 if (!NILP (Vquit_flag))
893 {
894 Vquit_flag = Qnil;
dbc4e1c1 895 unread_command_events = Fcons (make_number (quit_char), Qnil);
284f4730
JB
896 }
897 }
898
899#ifdef C_ALLOCA
900 alloca (0); /* Cause a garbage collection now */
901 /* Since we can free the most stuff here. */
902#endif /* C_ALLOCA */
903
8f805655 904#if 0
ff11dfa1 905#ifdef MULTI_FRAME
8f805655
JB
906 /* Select the frame that the last event came from. Usually,
907 switch-frame events will take care of this, but if some lisp
908 code swallows a switch-frame event, we'll fix things up here.
909 Is this a good idea? */
3c370943
JB
910 if (XTYPE (internal_last_event_frame) == Lisp_Frame
911 && XFRAME (internal_last_event_frame) != selected_frame)
912 Fselect_frame (internal_last_event_frame, Qnil);
cd21b839 913#endif
284f4730
JB
914#endif
915
8f805655
JB
916 /* Read next key sequence; i gets its length. */
917 i = read_key_sequence (keybuf, (sizeof keybuf / sizeof (keybuf[0])), 0);
918
919 ++num_input_keys;
920
284f4730
JB
921 /* Now we have read a key sequence of length I,
922 or else I is 0 and we found end of file. */
923
924 if (i == 0) /* End of file -- happens only in */
925 return Qnil; /* a kbd macro, at the end. */
926
284f4730
JB
927 last_command_char = keybuf[i - 1];
928
929 cmd = read_key_sequence_cmd;
930 if (!NILP (Vexecuting_macro))
931 {
932 if (!NILP (Vquit_flag))
933 {
934 Vexecuting_macro = Qt;
935 QUIT; /* Make some noise. */
936 /* Will return since macro now empty. */
937 }
938 }
939
940 /* Do redisplay processing after this command except in special
941 cases identified below that set no_redisplay to 1. */
942 no_redisplay = 0;
943
86e5706b
RS
944 prev_buffer = current_buffer;
945 prev_modiff = MODIFF;
946
284f4730
JB
947 /* Execute the command. */
948
86e5706b
RS
949 this_command = cmd;
950 if (!NILP (Vpre_command_hook))
951 call1 (Vrun_hooks, Qpre_command_hook);
952
284f4730
JB
953 if (NILP (cmd))
954 {
955 /* nil means key is undefined. */
956 bitch_at_user ();
957 defining_kbd_macro = 0;
958 update_mode_lines = 1;
959 Vprefix_arg = Qnil;
86e5706b 960
284f4730
JB
961 }
962 else
963 {
284f4730
JB
964 if (NILP (Vprefix_arg) && ! no_direct)
965 {
966 /* Recognize some common commands in common situations and
967 do them directly. */
968 if (EQ (cmd, Qforward_char) && point < ZV)
969 {
970 struct Lisp_Vector *dp
971 = window_display_table (XWINDOW (selected_window));
972 lose = FETCH_CHAR (point);
973 SET_PT (point + 1);
974 if (((dp == 0 && lose >= 040 && lose < 0177)
975 ||
976 (dp && (XTYPE (dp->contents[lose]) != Lisp_String
977 || XSTRING (dp->contents[lose])->size == sizeof (GLYPH))))
284f4730
JB
978 && (XFASTINT (XWINDOW (selected_window)->last_modified)
979 >= MODIFF)
980 && (XFASTINT (XWINDOW (selected_window)->last_point)
981 == point - 1)
982 && !windows_or_buffers_changed
983 && EQ (current_buffer->selective_display, Qnil)
984 && !detect_input_pending ()
985 && NILP (Vexecuting_macro))
986 no_redisplay = direct_output_forward_char (1);
987 goto directly_done;
988 }
989 else if (EQ (cmd, Qbackward_char) && point > BEGV)
990 {
991 struct Lisp_Vector *dp
992 = window_display_table (XWINDOW (selected_window));
993 SET_PT (point - 1);
994 lose = FETCH_CHAR (point);
995 if (((dp == 0 && lose >= 040 && lose < 0177)
996 ||
997 (dp && (XTYPE (dp->contents[lose]) != Lisp_String
998 || XSTRING (dp->contents[lose])->size == sizeof (GLYPH))))
284f4730
JB
999 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1000 >= MODIFF)
1001 && (XFASTINT (XWINDOW (selected_window)->last_point)
1002 == point + 1)
1003 && !windows_or_buffers_changed
1004 && EQ (current_buffer->selective_display, Qnil)
1005 && !detect_input_pending ()
1006 && NILP (Vexecuting_macro))
1007 no_redisplay = direct_output_forward_char (-1);
1008 goto directly_done;
1009 }
1010 else if (EQ (cmd, Qself_insert_command)
1011 /* Try this optimization only on ascii keystrokes. */
1012 && XTYPE (last_command_char) == Lisp_Int)
1013 {
1014 unsigned char c = XINT (last_command_char);
1015
1016 if (NILP (Vexecuting_macro) &&
1017 !EQ (minibuf_window, selected_window))
1018 {
1019 if (!nonundocount || nonundocount >= 20)
1020 {
1021 Fundo_boundary ();
1022 nonundocount = 0;
1023 }
1024 nonundocount++;
1025 }
1026 lose = (XFASTINT (XWINDOW (selected_window)->last_modified)
1027 < MODIFF)
1028 || (XFASTINT (XWINDOW (selected_window)->last_point)
1029 != point)
1030 || MODIFF <= current_buffer->save_modified
1031 || windows_or_buffers_changed
1032 || !EQ (current_buffer->selective_display, Qnil)
1033 || detect_input_pending ()
1034 || !NILP (Vexecuting_macro);
cb5df6ae 1035 if (internal_self_insert (XINT (c), 0))
284f4730
JB
1036 {
1037 lose = 1;
1038 nonundocount = 0;
1039 }
1040 if (!lose &&
1041 (point == ZV || FETCH_CHAR (point) == '\n'))
1042 {
1043 struct Lisp_Vector *dp
1044 = window_display_table (XWINDOW (selected_window));
1045
1046 if (dp == 0 || XTYPE (dp->contents[c]) != Lisp_String)
cb5df6ae 1047 no_redisplay = direct_output_for_insert (XINT (c));
284f4730
JB
1048 else if (XSTRING (dp->contents[c])->size
1049 == sizeof (GLYPH))
1050 no_redisplay =
1051 direct_output_for_insert (*(GLYPH *)XSTRING (dp->contents[c])->data);
1052 }
1053 goto directly_done;
1054 }
1055 }
1056
1057 /* Here for a command that isn't executed directly */
1058
1059 nonundocount = 0;
1060 if (NILP (Vprefix_arg))
1061 Fundo_boundary ();
1062 Fcommand_execute (cmd, Qnil);
1063
284f4730 1064 }
a764a753 1065 directly_done: ;
284f4730 1066
86e5706b
RS
1067 if (!NILP (Vpost_command_hook))
1068 call1 (Vrun_hooks, Qpost_command_hook);
1069
284f4730
JB
1070 /* If there is a prefix argument,
1071 1) We don't want last_command to be ``universal-argument''
1072 (that would be dumb), so don't set last_command,
1073 2) we want to leave echoing on so that the prefix will be
1074 echoed as part of this key sequence, so don't call
1075 cancel_echoing, and
1076 3) we want to leave this_command_key_count non-zero, so that
1077 read_char will realize that it is re-reading a character, and
1078 not echo it a second time. */
1079 if (NILP (Vprefix_arg))
1080 {
1081 last_command = this_command;
1082 cancel_echoing ();
1083 this_command_key_count = 0;
1084 }
86e5706b
RS
1085
1086 if (!NILP (current_buffer->mark_active))
1087 {
1088 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1089 {
1090 current_buffer->mark_active = Qnil;
1091 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1092 }
1093 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1094 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1095 }
284f4730
JB
1096 }
1097}
1098\f
1099/* Number of seconds between polling for input. */
1100int polling_period;
1101
1102/* Nonzero means polling for input is temporarily suppresed. */
1103int poll_suppress_count;
1104
1105#ifdef POLL_FOR_INPUT
1106int polling_for_input;
1107
1108/* Handle an alarm once each second and read pending input
1109 so as to handle a C-g if it comces in. */
1110
1111SIGTYPE
1112input_poll_signal ()
1113{
1114#ifdef HAVE_X_WINDOWS
1115 extern int x_input_blocked;
1116 if (x_input_blocked == 0)
1117#endif
1118 if (!waiting_for_input)
1119 read_avail_input (0);
1120 signal (SIGALRM, input_poll_signal);
1121 alarm (polling_period);
1122}
1123
1124#endif
1125
1126/* Begin signals to poll for input, if they are appropriate.
1127 This function is called unconditionally from various places. */
1128
1129start_polling ()
1130{
1131#ifdef POLL_FOR_INPUT
1132 if (read_socket_hook)
1133 {
1134 poll_suppress_count--;
1135 if (poll_suppress_count == 0)
1136 {
1137 signal (SIGALRM, input_poll_signal);
1138 polling_for_input = 1;
1139 alarm (polling_period);
1140 }
1141 }
1142#endif
1143}
1144
1145/* Turn off polling. */
1146
1147stop_polling ()
1148{
1149#ifdef POLL_FOR_INPUT
1150 if (read_socket_hook)
1151 {
1152 if (poll_suppress_count == 0)
1153 {
1154 polling_for_input = 0;
1155 alarm (0);
1156 }
1157 poll_suppress_count++;
1158 }
1159#endif
1160}
1161\f
1162/* Input of single characters from keyboard */
1163
1164Lisp_Object print_help ();
1165static Lisp_Object kbd_buffer_get_event ();
1166
1167/* read a character from the keyboard; call the redisplay if needed */
1168/* commandflag 0 means do not do auto-saving, but do do redisplay.
1169 -1 means do not do redisplay, but do do autosaving.
1170 1 means do both. */
1171
7d6de002
RS
1172/* The arguments MAPS and NMAPS are for menu prompting.
1173 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1174
1175 PREV_EVENT is the previous input event, or nil if we are reading
1176 the first event of a key sequence.
1177
6569cc8d
JB
1178 If USED_MOUSE_MENU is non-zero, then we set *USED_MOUSE_MENU to 1
1179 if we used a mouse menu to read the input, or zero otherwise. If
1180 USED_MOUSE_MENU is zero, *USED_MOUSE_MENU is left alone. */
7d6de002 1181
284f4730 1182Lisp_Object
7d6de002 1183read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
284f4730 1184 int commandflag;
7d6de002
RS
1185 int nmaps;
1186 Lisp_Object *maps;
1187 Lisp_Object prev_event;
1188 int *used_mouse_menu;
284f4730
JB
1189{
1190 register Lisp_Object c;
1191 int count;
1192 jmp_buf save_jump;
1193
dbc4e1c1 1194 if (CONSP (unread_command_events))
284f4730 1195 {
dbc4e1c1
JB
1196 c = XCONS (unread_command_events)->car;
1197 unread_command_events = XCONS (unread_command_events)->cdr;
284f4730 1198
284f4730
JB
1199 if (this_command_key_count == 0)
1200 goto reread_first;
1201 else
1202 goto reread;
1203 }
1204
86e5706b
RS
1205 if (unread_command_char != -1)
1206 {
1207 XSET (c, Lisp_Int, unread_command_char);
1208 unread_command_char = -1;
1209
1210 if (this_command_key_count == 0)
1211 goto reread_first;
1212 else
1213 goto reread;
1214 }
1215
284f4730
JB
1216 if (!NILP (Vexecuting_macro))
1217 {
07d2b8de 1218#ifdef MULTI_FRAME
fce33686
JB
1219 /* We set this to Qmacro; since that's not a frame, nobody will
1220 try to switch frames on us, and the selected window will
1221 remain unchanged.
1222
1223 Since this event came from a macro, it would be misleading to
3c370943
JB
1224 leave internal_last_event_frame set to whereever the last
1225 real event came from. Normally, a switch-frame event selects
1226 internal_last_event_frame after each command is read, but
1227 events read from a macro should never cause a new frame to be
1228 selected. */
1229 Vlast_event_frame = internal_last_event_frame = Qmacro;
07d2b8de 1230#endif
fce33686 1231
f4255cd1 1232 if (executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
284f4730
JB
1233 {
1234 XSET (c, Lisp_Int, -1);
1235 return c;
1236 }
1237
1238 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
86e5706b
RS
1239 if (XTYPE (Vexecuting_macro) == Lisp_String
1240 && (XINT (c) & 0x80))
1241 XFASTINT (c) = CHAR_META | (XINT (c) & ~0x80);
1242
284f4730
JB
1243 executing_macro_index++;
1244
1245 goto from_macro;
1246 }
1247
cd21b839
JB
1248 if (!NILP (unread_switch_frame))
1249 {
1250 c = unread_switch_frame;
1251 unread_switch_frame = Qnil;
1252
1253 /* This event should make it into this_command_keys, and get echoed
f4255cd1
JB
1254 again, so we go to reread_first, rather than reread. */
1255 goto reread_first;
cd21b839
JB
1256 }
1257
284f4730 1258 /* Save outer setjmp data, in case called recursively. */
f76475ad 1259 save_getcjmp (save_jump);
284f4730
JB
1260
1261 stop_polling ();
1262
1263 if (commandflag >= 0 && !input_pending && !detect_input_pending ())
1264 redisplay ();
1265
1266 if (_setjmp (getcjmp))
1267 {
1268 XSET (c, Lisp_Int, quit_char);
07d2b8de 1269#ifdef MULTI_FRAME
3c370943
JB
1270 XSET (internal_last_event_frame, Lisp_Frame, selected_frame);
1271 Vlast_event_frame = internal_last_event_frame;
07d2b8de 1272#endif
284f4730 1273
284f4730
JB
1274 goto non_reread;
1275 }
1276
1277 /* Message turns off echoing unless more keystrokes turn it on again. */
1278 if (echo_area_glyphs && *echo_area_glyphs && echo_area_glyphs != echobuf)
1279 cancel_echoing ();
1280 else
1281 /* If already echoing, continue. */
1282 echo_dash ();
1283
1284 /* If in middle of key sequence and minibuffer not active,
1285 start echoing if enough time elapses. */
1286 if (minibuf_level == 0 && !immediate_echo && this_command_key_count > 0
1287 && echo_keystrokes > 0
1288 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0))
1289 {
1290 Lisp_Object tem0;
1291
7d6de002
RS
1292 /* After a mouse event, start echoing right away.
1293 This is because we are probably about to display a menu,
1294 and we don't want to delay before doing so. */
dbc4e1c1 1295 if (EVENT_HAS_PARAMETERS (prev_event))
284f4730 1296 echo ();
7d6de002
RS
1297 else
1298 {
1299 tem0 = sit_for (echo_keystrokes, 0, 1, 1);
1300 if (EQ (tem0, Qt))
1301 echo ();
1302 }
284f4730
JB
1303 }
1304
1305 /* Maybe auto save due to number of keystrokes or idle time. */
1306
1307 if (commandflag != 0
1308 && auto_save_interval > 0
51172b6d 1309 && num_nonmacro_input_chars - last_auto_save > max (auto_save_interval, 20)
284f4730
JB
1310 && !detect_input_pending ())
1311 {
1312 jmp_buf temp;
1313 save_getcjmp (temp);
1314 Fdo_auto_save (Qnil, Qnil);
1315 restore_getcjmp (temp);
1316 }
1317
7d6de002
RS
1318 /* Try reading a character via menu prompting.
1319 Try this before the sit-for, because the sit-for
1320 would do the wrong thing if we are supposed to do
1321 menu prompting. */
1322 c = Qnil;
1323 if (INTERACTIVE && !NILP (prev_event))
1324 c = read_char_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
1325
284f4730
JB
1326 /* Slow down auto saves logarithmically in size of current buffer,
1327 and garbage collect while we're at it. */
7d6de002
RS
1328 if (NILP (c))
1329 {
1330 int delay_level, buffer_size;
1331
1332 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
1333 last_non_minibuf_size = Z - BEG;
1334 buffer_size = (last_non_minibuf_size >> 8) + 1;
1335 delay_level = 0;
1336 while (buffer_size > 64)
1337 delay_level++, buffer_size -= buffer_size >> 2;
1338 if (delay_level < 4) delay_level = 4;
1339 /* delay_level is 4 for files under around 50k, 7 at 100k,
1340 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
1341
1342 /* Auto save if enough time goes by without input. */
1343 if (commandflag != 0
51172b6d 1344 && num_nonmacro_input_chars > last_auto_save
7d6de002
RS
1345 && XTYPE (Vauto_save_timeout) == Lisp_Int
1346 && XINT (Vauto_save_timeout) > 0)
1347 {
1348 Lisp_Object tem0;
1349 int delay = delay_level * XFASTINT (Vauto_save_timeout) / 4;
1350 tem0 = sit_for (delay, 0, 1, 1);
1351 if (EQ (tem0, Qt))
1352 {
1353 jmp_buf temp;
1354 save_getcjmp (temp);
1355 Fdo_auto_save (Qnil, Qnil);
1356 restore_getcjmp (temp);
1357
1358 /* If we have auto-saved and there is still no input
1359 available, garbage collect if there has been enough
1360 consing going on to make it worthwhile. */
1361 if (!detect_input_pending ()
1362 && consing_since_gc > gc_cons_threshold / 2)
1363 Fgarbage_collect ();
1364 }
1365 }
1366 }
284f4730
JB
1367
1368 /* Actually read a character, waiting if necessary. */
7d6de002
RS
1369 if (NILP (c))
1370 c = kbd_buffer_get_event ();
284f4730
JB
1371
1372 if (NILP (c))
1373 abort (); /* Don't think this can happen. */
1374
284f4730 1375 /* Terminate Emacs in batch mode if at eof. */
7d6de002 1376 if (noninteractive && XTYPE (c) == Lisp_Int && XINT (c) < 0)
284f4730
JB
1377 Fkill_emacs (make_number (1));
1378
9fa4395d
RS
1379 /* Test for ControlMask and Mod1Mask. */
1380 if (extra_keyboard_modifiers & 4)
1381 c &= ~0140;
1382 if (extra_keyboard_modifiers & 8)
1383 c |= 0200;
1384
284f4730
JB
1385 non_reread:
1386
f76475ad 1387 restore_getcjmp (save_jump);
284f4730
JB
1388
1389 start_polling ();
1390
1391 echo_area_glyphs = 0;
1392
1393 /* Handle things that only apply to characters. */
1394 if (XTYPE (c) == Lisp_Int)
1395 {
1396 /* If kbd_buffer_get_event gave us an EOF, return that. */
86e5706b 1397 if (XINT (c) == -1)
284f4730
JB
1398 return c;
1399
284f4730 1400 if (XTYPE (Vkeyboard_translate_table) == Lisp_String
f4255cd1
JB
1401 && XSTRING (Vkeyboard_translate_table)->size > XFASTINT (c))
1402 XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[XFASTINT (c)]);
284f4730
JB
1403 }
1404
1405 total_keys++;
5160df46
JB
1406 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
1407 if (++recent_keys_index >= NUM_RECENT_KEYS)
284f4730
JB
1408 recent_keys_index = 0;
1409
1410 /* Write c to the dribble file. If c is a lispy event, write
1411 the event's symbol to the dribble file, in <brackets>. Bleaugh.
1412 If you, dear reader, have a better idea, you've got the source. :-) */
1413 if (dribble)
1414 {
1415 if (XTYPE (c) == Lisp_Int)
f4255cd1 1416 putc (XINT (c), dribble);
284f4730
JB
1417 else
1418 {
1419 Lisp_Object dribblee = c;
1420
1421 /* If it's a structured event, take the event header. */
88cb0656 1422 dribblee = EVENT_HEAD (dribblee);
284f4730 1423
8f805655 1424 if (XTYPE (dribblee) == Lisp_Symbol)
284f4730
JB
1425 {
1426 putc ('<', dribble);
8f805655
JB
1427 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
1428 XSYMBOL (dribblee)->name->size,
284f4730
JB
1429 dribble);
1430 putc ('>', dribble);
1431 }
1432 }
1433
1434 fflush (dribble);
1435 }
1436
1437 store_kbd_macro_char (c);
1438
51172b6d
RS
1439 num_nonmacro_input_chars++;
1440
284f4730
JB
1441 from_macro:
1442 reread_first:
284f4730 1443
4bb994d1
JB
1444 /* Record this character as part of the current key.
1445 Don't record mouse motion; it should never matter. */
1446 if (! (EVENT_HAS_PARAMETERS (c)
1447 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
7b4aedb9
JB
1448 {
1449 echo_char (c);
1450 add_command_key (c);
1451 }
284f4730
JB
1452
1453 /* Re-reading in the middle of a command */
1454 reread:
1455 last_input_char = c;
1456 num_input_chars++;
1457
1458 /* Process the help character specially if enabled */
1459 if (EQ (c, help_char) && !NILP (Vhelp_form))
1460 {
1461 Lisp_Object tem0;
1462 count = specpdl_ptr - specpdl;
1463
1464 record_unwind_protect (Fset_window_configuration,
1465 Fcurrent_window_configuration (Qnil));
1466
1467 tem0 = Feval (Vhelp_form);
1468 if (XTYPE (tem0) == Lisp_String)
1469 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
1470
1471 cancel_echoing ();
6569cc8d 1472 c = read_char (0, 0, 0, Qnil, 0);
ff11dfa1 1473 /* Remove the help from the frame */
284f4730
JB
1474 unbind_to (count, Qnil);
1475 redisplay ();
1476 if (EQ (c, make_number (040)))
1477 {
1478 cancel_echoing ();
7d6de002 1479 c = read_char (0, 0, 0, Qnil, 0);
284f4730
JB
1480 }
1481 }
1482
1483 return c;
1484}
1485
1486Lisp_Object
1487print_help (object)
1488 Lisp_Object object;
1489{
1490 Fprinc (object, Qnil);
1491 return Qnil;
1492}
1493
1494/* Copy out or in the info on where C-g should throw to.
1495 This is used when running Lisp code from within get_char,
1496 in case get_char is called recursively.
1497 See read_process_output. */
1498
1499save_getcjmp (temp)
1500 jmp_buf temp;
1501{
1502 bcopy (getcjmp, temp, sizeof getcjmp);
1503}
1504
1505restore_getcjmp (temp)
1506 jmp_buf temp;
1507{
1508 bcopy (temp, getcjmp, sizeof getcjmp);
1509}
1510
1511\f
1512/* Low level keyboard/mouse input.
1513 kbd_buffer_store_event places events in kbd_buffer, and
1514 kbd_buffer_get_event retrieves them.
1515 mouse_moved indicates when the mouse has moved again, and
1516 *mouse_position_hook provides the mouse position. */
1517
1518/* Set this for debugging, to have a way to get out */
1519int stop_character;
1520
ff11dfa1 1521extern int frame_garbaged;
284f4730
JB
1522
1523/* Return true iff there are any events in the queue that read-char
1524 would return. If this returns false, a read-char would block. */
1525static int
1526readable_events ()
1527{
88cb0656 1528 return ! EVENT_QUEUES_EMPTY;
284f4730
JB
1529}
1530
1531
1532/* Restore mouse tracking enablement. See Ftrack_mouse for the only use
1533 of this function. */
1534static Lisp_Object
1535tracking_off (old_value)
1536 Lisp_Object old_value;
1537{
1538 if (! XFASTINT (old_value))
1539 {
1540 do_mouse_tracking = 0;
1541
1542 /* Redisplay may have been preempted because there was input
1543 available, and it assumes it will be called again after the
1544 input has been processed. If the only input available was
1545 the sort that we have just disabled, then we need to call
1546 redisplay. */
1547 if (!readable_events ())
1548 {
1549 redisplay_preserve_echo_area ();
1550 get_input_pending (&input_pending);
1551 }
1552 }
1553}
1554
1555DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
4bb994d1
JB
1556 "Evaluate BODY with mouse movement events enabled.\n\
1557Within a `track-mouse' form, mouse motion generates input events that\n\
1558you can read with `read-event'.\n\
1559Normally, mouse motion is ignored.")
284f4730
JB
1560 (args)
1561 Lisp_Object args;
1562{
1563 int count = specpdl_ptr - specpdl;
1564 Lisp_Object val;
1565
1566 XSET (val, Lisp_Int, do_mouse_tracking);
1567 record_unwind_protect (tracking_off, val);
1568
1569 do_mouse_tracking = 1;
1570
1571 val = Fprogn (args);
1572 return unbind_to (count, val);
1573}
1574
1575/* Store an event obtained at interrupt level into kbd_buffer, fifo */
1576
1577void
1578kbd_buffer_store_event (event)
1579 register struct input_event *event;
1580{
1581 if (event->kind == no_event)
1582 abort ();
1583
1584 if (event->kind == ascii_keystroke)
1585 {
1586 register int c = XFASTINT (event->code) & 0377;
1587
86e5706b 1588 if (c == quit_char)
284f4730 1589 {
3e51c7b7
JB
1590 extern SIGTYPE interrupt_signal ();
1591
07d2b8de 1592#ifdef MULTI_FRAME
284f4730 1593 /* If this results in a quit_char being returned to Emacs as
3c370943 1594 input, set Vlast_event_frame properly. If this doesn't
284f4730 1595 get returned to Emacs as an event, the next event read
ff11dfa1 1596 will set Vlast_event_frame again, so this is safe to do. */
4bb994d1 1597 {
7b4aedb9
JB
1598 Lisp_Object focus =
1599 FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
4bb994d1
JB
1600
1601 if (NILP (focus))
3c370943 1602 internal_last_event_frame = event->frame_or_window;
4bb994d1 1603 else
3c370943
JB
1604 internal_last_event_frame = focus;
1605 Vlast_event_frame = internal_last_event_frame;
4bb994d1 1606 }
07d2b8de 1607#endif
3e51c7b7 1608
ffd56f97 1609 last_event_timestamp = event->timestamp;
284f4730
JB
1610 interrupt_signal ();
1611 return;
1612 }
1613
1614 if (c && c == stop_character)
1615 {
1616 sys_suspend ();
1617 return;
1618 }
1619
1620 XSET (event->code, Lisp_Int, c);
1621 }
1622
1623 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
1624 kbd_store_ptr = kbd_buffer;
1625
1626 /* Don't let the very last slot in the buffer become full,
1627 since that would make the two pointers equal,
1628 and that is indistinguishable from an empty buffer.
1629 Discard the event if it would fill the last slot. */
1630 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
1631 {
1632 kbd_store_ptr->kind = event->kind;
1633 kbd_store_ptr->code = event->code;
1634 kbd_store_ptr->part = event->part;
7b4aedb9 1635 kbd_store_ptr->frame_or_window = event->frame_or_window;
284f4730
JB
1636 kbd_store_ptr->modifiers = event->modifiers;
1637 kbd_store_ptr->x = event->x;
1638 kbd_store_ptr->y = event->y;
1639 kbd_store_ptr->timestamp = event->timestamp;
7b4aedb9
JB
1640 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
1641 - kbd_buffer]
1642 = event->frame_or_window);
284f4730
JB
1643
1644 kbd_store_ptr++;
1645 }
1646}
1647
1648static Lisp_Object make_lispy_event ();
1649static Lisp_Object make_lispy_movement ();
1650static Lisp_Object modify_event_symbol ();
cd21b839 1651static Lisp_Object make_lispy_switch_frame ();
284f4730
JB
1652
1653static Lisp_Object
1654kbd_buffer_get_event ()
1655{
1656 register int c;
1657 Lisp_Object obj;
1658
1659 if (noninteractive)
1660 {
1661 c = getchar ();
1662 XSET (obj, Lisp_Int, c);
1663 return obj;
1664 }
1665
1666 /* Wait until there is input available. */
1667 for (;;)
1668 {
284f4730
JB
1669 if (!EVENT_QUEUES_EMPTY)
1670 break;
1671
1672 /* If the quit flag is set, then read_char will return
1673 quit_char, so that counts as "available input." */
1674 if (!NILP (Vquit_flag))
1675 quit_throw_to_read_char ();
1676
1677 /* One way or another, wait until input is available; then, if
1678 interrupt handlers have not read it, read it now. */
1679
1680#ifdef OLDVMS
1681 wait_for_kbd_input ();
1682#else
1683/* Note SIGIO has been undef'd if FIONREAD is missing. */
1684#ifdef SIGIO
1685 gobble_input (0);
1686#endif /* SIGIO */
1687 if (EVENT_QUEUES_EMPTY)
1688 {
f76475ad
JB
1689 Lisp_Object minus_one;
1690
1691 XSET (minus_one, Lisp_Int, -1);
1692 wait_reading_process_input (0, 0, minus_one, 1);
284f4730
JB
1693
1694 if (!interrupt_input && EVENT_QUEUES_EMPTY)
1695 {
1696 read_avail_input (0);
1697 }
1698 }
1699#endif /* not VMS */
1700 }
1701
1702 /* At this point, we know that there is a readable event available
1703 somewhere. If the event queue is empty, then there must be a
1704 mouse movement enabled and available. */
1705 if (kbd_fetch_ptr != kbd_store_ptr)
1706 {
cd21b839 1707 struct input_event *event;
3e51c7b7 1708
856bf263 1709 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
cd21b839
JB
1710 ? kbd_fetch_ptr
1711 : kbd_buffer);
3e51c7b7 1712
cd21b839 1713 last_event_timestamp = event->timestamp;
cd21b839 1714
4bb994d1
JB
1715 obj = Qnil;
1716
07d2b8de 1717#ifdef MULTI_FRAME
4bb994d1
JB
1718 /* If this event is on a different frame, return a switch-frame this
1719 time, and leave the event in the queue for next time. */
1720 {
7b4aedb9
JB
1721 Lisp_Object frame = event->frame_or_window;
1722 Lisp_Object focus;
1723
1724 if (XTYPE (frame) == Lisp_Window)
1725 frame = WINDOW_FRAME (XWINDOW (frame));
4bb994d1 1726
7b4aedb9
JB
1727 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
1728 if (! NILP (focus))
1729 frame = focus;
07d2b8de 1730
3c370943 1731 if (! EQ (frame, internal_last_event_frame))
07d2b8de 1732 {
3c370943 1733 internal_last_event_frame = frame;
07d2b8de
JB
1734 obj = make_lispy_switch_frame (frame);
1735 }
4bb994d1 1736 }
07d2b8de 1737#endif
4bb994d1
JB
1738
1739 /* If we didn't decide to make a switch-frame event, go ahead
1740 and build a real event from the queue entry. */
1741 if (NILP (obj))
1742 {
1743 obj = make_lispy_event (event);
cd21b839 1744
4bb994d1
JB
1745 /* Wipe out this event, to catch bugs. */
1746 event->kind = no_event;
7b4aedb9
JB
1747 (XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer]
1748 = Qnil);
cd21b839 1749
4bb994d1
JB
1750 kbd_fetch_ptr = event + 1;
1751 }
284f4730
JB
1752 }
1753 else if (do_mouse_tracking && mouse_moved)
1754 {
4bb994d1 1755 FRAME_PTR f;
7b4aedb9 1756 Lisp_Object bar_window;
3c370943 1757 enum scroll_bar_part part;
e5d77022
JB
1758 Lisp_Object x, y;
1759 unsigned long time;
284f4730 1760
7b4aedb9 1761 (*mouse_position_hook) (&f, &bar_window, &part, &x, &y, &time);
4bb994d1
JB
1762
1763 obj = Qnil;
284f4730 1764
07d2b8de 1765#ifdef MULTI_FRAME
4bb994d1
JB
1766 /* Decide if we should generate a switch-frame event. Don't
1767 generate switch-frame events for motion outside of all Emacs
1768 frames. */
1769 if (f)
cd21b839 1770 {
4bb994d1
JB
1771 Lisp_Object frame = FRAME_FOCUS_FRAME (f);
1772
1773 if (NILP (frame))
1774 XSET (frame, Lisp_Frame, f);
1775
3c370943 1776 if (! EQ (frame, internal_last_event_frame))
4bb994d1 1777 {
3c370943
JB
1778 XSET (internal_last_event_frame, Lisp_Frame, frame);
1779 obj = make_lispy_switch_frame (internal_last_event_frame);
4bb994d1 1780 }
cd21b839 1781 }
07d2b8de 1782#endif
4bb994d1
JB
1783
1784 /* If we didn't decide to make a switch-frame event, go ahead and
1785 return a mouse-motion event. */
1786 if (NILP (obj))
7b4aedb9 1787 obj = make_lispy_movement (f, bar_window, part, x, y, time);
4bb994d1 1788 }
284f4730
JB
1789 else
1790 /* We were promised by the above while loop that there was
1791 something for us to read! */
1792 abort ();
1793
1794 input_pending = readable_events ();
1795
3c370943
JB
1796#ifdef MULTI_FRAME
1797 Vlast_event_frame = internal_last_event_frame;
1798#endif
1799
284f4730
JB
1800 return (obj);
1801}
1802
4bb994d1 1803
284f4730
JB
1804/* Caches for modify_event_symbol. */
1805static Lisp_Object func_key_syms;
1806static Lisp_Object mouse_syms;
1807
1808/* You'll notice that this table is arranged to be conveniently
1809 indexed by X Windows keysym values. */
1810static char *lispy_function_keys[] =
1811 {
1812 /* X Keysym value */
1813
86e5706b
RS
1814 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00 */
1815 "backspace",
1816 "tab",
1817 "linefeed",
1818 "clear",
1819 0,
1820 "return",
1821 0, 0,
1822 0, 0, 0, /* 0xff10 */
1823 "pause",
1824 0, 0, 0, 0, 0, 0, 0,
1825 "escape",
1826 0, 0, 0, 0,
1827 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff20...2f */
1828 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff30...3f */
1829 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
1830
284f4730
JB
1831 "home", /* 0xff50 */ /* IsCursorKey */
1832 "left",
1833 "up",
1834 "right",
1835 "down",
1836 "prior",
1837 "next",
1838 "end",
1839 "begin",
1840 0, /* 0xff59 */
1841 0, 0, 0, 0, 0, 0,
1842 "select", /* 0xff60 */ /* IsMiscFunctionKey */
1843 "print",
1844 "execute",
1845 "insert",
1846 0, /* 0xff64 */
1847 "undo",
1848 "redo",
1849 "menu",
1850 "find",
1851 "cancel",
1852 "help",
1853 "break", /* 0xff6b */
1854
1855 /* Here are some keys found mostly on HP keyboards. The X event
1856 handling code will strip bit 29, which flags vendor-specific
1857 keysyms. */
1858 "reset", /* 0x1000ff6c */
1859 "system",
1860 "user",
1861 "clearline",
1862 "insertline",
1863 "deleteline",
1864 "insertchar",
1865 "deletechar",
1866 "backtab",
1867 "kp_backtab", /* 0x1000ff75 */
1868 0, /* 0xff76 */
1869 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff7f */
1870 "kp-space", /* 0xff80 */ /* IsKeypadKey */
1871 0, 0, 0, 0, 0, 0, 0, 0,
1872 "kp-tab", /* 0xff89 */
1873 0, 0, 0,
1874 "kp-enter", /* 0xff8d */
1875 0, 0, 0,
1876 "kp-f1", /* 0xff91 */
1877 "kp-f2",
1878 "kp-f3",
1879 "kp-f4",
1880 0, /* 0xff95 */
1881 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1882 "kp-multiply", /* 0xffaa */
1883 "kp-add",
1884 "kp-separator",
1885 "kp-subtract",
1886 "kp-decimal",
1887 "kp-divide", /* 0xffaf */
1888 "kp-0", /* 0xffb0 */
1889 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
1890 0, /* 0xffba */
1891 0, 0,
1892 "kp-equal", /* 0xffbd */
1893 "f1", /* 0xffbe */ /* IsFunctionKey */
86e5706b
RS
1894 "f2",
1895 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
1896 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
1897 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
1898 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
1899 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
1900 0, 0, 0, 0, 0, 0, 0, 0,
1901 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
1902 0, 0, 0, 0, 0, 0, 0, "delete"
284f4730
JB
1903 };
1904
1905static char *lispy_mouse_names[] =
1906{
1907 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
1908};
1909
3c370943 1910/* Scroll bar parts. */
4bb994d1
JB
1911Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
1912
3c370943
JB
1913/* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
1914Lisp_Object *scroll_bar_parts[] = {
4bb994d1
JB
1915 &Qabove_handle, &Qhandle, &Qbelow_handle
1916};
1917
1918
7b4aedb9 1919/* A vector, indexed by button number, giving the down-going location
3c370943 1920 of currently depressed buttons, both scroll bar and non-scroll bar.
7b4aedb9
JB
1921
1922 The elements have the form
1923 (BUTTON-NUMBER MODIFIER-MASK . REST)
1924 where REST is the cdr of a position as it would be reported in the event.
1925
1926 The make_lispy_event function stores positions here to tell the
1927 difference between click and drag events, and to store the starting
1928 location to be included in drag events. */
1929
1930static Lisp_Object button_down_location;
88cb0656 1931
284f4730
JB
1932/* Given a struct input_event, build the lisp event which represents
1933 it. If EVENT is 0, build a mouse movement event from the mouse
88cb0656
JB
1934 movement buffer, which should have a movement event in it.
1935
1936 Note that events must be passed to this function in the order they
1937 are received; this function stores the location of button presses
1938 in order to build drag events when the button is released. */
284f4730
JB
1939
1940static Lisp_Object
1941make_lispy_event (event)
1942 struct input_event *event;
1943{
1944#ifdef SWITCH_ENUM_BUG
1945 switch ((int) event->kind)
1946#else
1947 switch (event->kind)
1948#endif
1949 {
284f4730
JB
1950 /* A simple keystroke. */
1951 case ascii_keystroke:
86e5706b
RS
1952 {
1953 int c = XFASTINT (event->code);
1954 /* Include the bits for control and shift
1955 only if the basic ASCII code can't indicate them. */
1956 if ((event->modifiers & ctrl_modifier)
1957 && c >= 040)
1958 c |= ctrl_modifier;
1959 if (XFASTINT (event->code) < 040
1960 && (event->modifiers & shift_modifier))
1961 c |= shift_modifier;
1962 c |= (event->modifiers
1963 & (meta_modifier | alt_modifier
1964 | hyper_modifier | super_modifier));
1965 return c;
1966 }
284f4730
JB
1967
1968 /* A function key. The symbol may need to have modifier prefixes
1969 tacked onto it. */
1970 case non_ascii_keystroke:
1971 return modify_event_symbol (XFASTINT (event->code), event->modifiers,
1972 Qfunction_key,
1973 lispy_function_keys, &func_key_syms,
1974 (sizeof (lispy_function_keys)
1975 / sizeof (lispy_function_keys[0])));
1976 break;
1977
88cb0656
JB
1978 /* A mouse click. Figure out where it is, decide whether it's
1979 a press, click or drag, and build the appropriate structure. */
284f4730 1980 case mouse_click:
3c370943 1981 case scroll_bar_click:
284f4730 1982 {
0a7f1fc0 1983 int button = XFASTINT (event->code);
7b4aedb9 1984 Lisp_Object position;
dbc4e1c1
JB
1985 Lisp_Object *start_pos_ptr;
1986 Lisp_Object start_pos;
284f4730 1987
7b4aedb9 1988 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
88cb0656
JB
1989 abort ();
1990
7b4aedb9
JB
1991 /* Build the position as appropriate for this mouse click. */
1992 if (event->kind == mouse_click)
284f4730 1993 {
7b4aedb9
JB
1994 int part;
1995 Lisp_Object window =
1996 window_from_coordinates (XFRAME (event->frame_or_window),
1997 XINT (event->x), XINT (event->y),
1998 &part);
1999 Lisp_Object posn;
2000
2001 if (XTYPE (window) != Lisp_Window)
2002 posn = Qnil;
284f4730 2003 else
7b4aedb9 2004 {
dbc4e1c1
JB
2005 XSETINT (event->x,
2006 (XINT (event->x) - XINT (XWINDOW (window)->left)));
2007 XSETINT (event->y,
2008 (XINT (event->y) - XINT (XWINDOW (window)->top)));
2009
7b4aedb9
JB
2010 if (part == 1)
2011 posn = Qmode_line;
2012 else if (part == 2)
2013 posn = Qvertical_line;
2014 else
dbc4e1c1
JB
2015 XSET (posn, Lisp_Int,
2016 buffer_posn_from_coords (XWINDOW (window),
2017 XINT (event->x),
2018 XINT (event->y)));
7b4aedb9
JB
2019 }
2020
2021 position =
2022 Fcons (window,
2023 Fcons (posn,
2024 Fcons (Fcons (event->x, event->y),
2025 Fcons (make_number (event->timestamp),
2026 Qnil))));
284f4730 2027 }
7b4aedb9 2028 else
88cb0656 2029 {
7b4aedb9
JB
2030 Lisp_Object window = event->frame_or_window;
2031 Lisp_Object portion_whole = Fcons (event->x, event->y);
3c370943 2032 Lisp_Object part = *scroll_bar_parts[(int) event->part];
7b4aedb9
JB
2033
2034 position =
2035 Fcons (window,
3c370943 2036 Fcons (Qvertical_scroll_bar,
7b4aedb9
JB
2037 Fcons (portion_whole,
2038 Fcons (make_number (event->timestamp),
2039 Fcons (part,
2040 Qnil)))));
88cb0656
JB
2041 }
2042
dbc4e1c1
JB
2043 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
2044
2045 start_pos = *start_pos_ptr;
2046 *start_pos_ptr = Qnil;
7b4aedb9
JB
2047
2048 /* If this is a button press, squirrel away the location, so
2049 we can decide later whether it was a click or a drag. */
2050 if (event->modifiers & down_modifier)
dbc4e1c1 2051 *start_pos_ptr = Fcopy_alist (position);
7b4aedb9 2052
88cb0656 2053 /* Now we're releasing a button - check the co-ordinates to
7b4aedb9 2054 see if this was a click or a drag. */
88cb0656
JB
2055 else if (event->modifiers & up_modifier)
2056 {
dbc4e1c1
JB
2057 /* Is there a start position stored at all for this
2058 button?
2059
2060 It would be nice if we could assume that if we're
2061 getting a button release, we must therefore have gotten
2062 a button press. Unfortunately, the X menu code thwarts
2063 this assumption, so we'll have to be more robust. We
2064 treat a button release with no stored start position as
2065 a click. */
88cb0656 2066 event->modifiers &= ~up_modifier;
dbc4e1c1
JB
2067 if (XTYPE (start_pos) != Lisp_Cons)
2068 event->modifiers |= click_modifier;
2069 else
2070 {
2071 /* The third element of every position should be the (x,y)
2072 pair. */
2073 Lisp_Object down = Fnth (make_number (2), start_pos);
2074
2075 event->modifiers |= ((EQ (event->x, XCONS (down)->car)
2076 && EQ (event->y, XCONS (down)->cdr))
2077 ? click_modifier
2078 : drag_modifier);
2079 }
88cb0656
JB
2080 }
2081 else
2082 /* Every mouse event should either have the down_modifier or
7b4aedb9 2083 the up_modifier set. */
88cb0656
JB
2084 abort ();
2085
88cb0656 2086 {
7b4aedb9
JB
2087 /* Get the symbol we should use for the mouse click. */
2088 Lisp_Object head =
2089 modify_event_symbol (button,
2090 event->modifiers,
2091 Qmouse_click,
2092 lispy_mouse_names, &mouse_syms,
2093 (sizeof (lispy_mouse_names)
2094 / sizeof (lispy_mouse_names[0])));
2095
88cb0656 2096 if (event->modifiers & drag_modifier)
dbc4e1c1
JB
2097 return Fcons (head,
2098 Fcons (start_pos,
2099 Fcons (position,
2100 Qnil)));
88cb0656
JB
2101 else
2102 return Fcons (head,
7b4aedb9 2103 Fcons (position,
88cb0656
JB
2104 Qnil));
2105 }
284f4730
JB
2106 }
2107
284f4730
JB
2108 /* The 'kind' field of the event is something we don't recognize. */
2109 default:
2110 abort();
2111 }
2112}
2113
2114static Lisp_Object
7b4aedb9 2115make_lispy_movement (frame, bar_window, part, x, y, time)
ff11dfa1 2116 FRAME_PTR frame;
7b4aedb9 2117 Lisp_Object bar_window;
3c370943 2118 enum scroll_bar_part part;
284f4730 2119 Lisp_Object x, y;
e5d77022 2120 unsigned long time;
284f4730 2121{
3c370943 2122 /* Is it a scroll bar movement? */
7b4aedb9 2123 if (frame && ! NILP (bar_window))
4bb994d1 2124 {
3c370943 2125 Lisp_Object part_sym = *scroll_bar_parts[(int) part];
4bb994d1 2126
3c370943 2127 return Fcons (Qscroll_bar_movement,
7b4aedb9 2128 (Fcons (Fcons (bar_window,
3c370943 2129 Fcons (Qvertical_scroll_bar,
4bb994d1
JB
2130 Fcons (Fcons (x, y),
2131 Fcons (make_number (time),
cb5df6ae 2132 Fcons (part_sym,
4bb994d1
JB
2133 Qnil))))),
2134 Qnil)));
2135 }
2136
2137 /* Or is it an ordinary mouse movement? */
284f4730
JB
2138 else
2139 {
4bb994d1
JB
2140 int area;
2141 Lisp_Object window =
2142 (frame
2143 ? window_from_coordinates (frame, XINT (x), XINT (y), &area)
2144 : Qnil);
2145 Lisp_Object posn;
2146
2147 if (XTYPE (window) == Lisp_Window)
2148 {
2149 XSETINT (x, XINT (x) - XINT (XWINDOW (window)->left));
2150 XSETINT (y, XINT (y) - XINT (XWINDOW (window)->top));
2151
2152 if (area == 1)
2153 posn = Qmode_line;
2154 else if (area == 2)
2155 posn = Qvertical_line;
2156 else
2157 XSET (posn, Lisp_Int,
2158 buffer_posn_from_coords (XWINDOW (window),
2159 XINT (x), XINT (y)));
2160 }
284f4730 2161 else
4bb994d1
JB
2162 {
2163 window = Qnil;
2164 posn = Qnil;
2165 }
284f4730 2166
4bb994d1
JB
2167 return Fcons (Qmouse_movement,
2168 Fcons (Fcons (window,
2169 Fcons (posn,
2170 Fcons (Fcons (x, y),
2171 Fcons (make_number (time),
2172 Qnil)))),
2173 Qnil));
2174 }
284f4730
JB
2175}
2176
2177
cd21b839
JB
2178/* Construct a switch frame event. */
2179static Lisp_Object
2180make_lispy_switch_frame (frame)
2181 Lisp_Object frame;
2182{
2183 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
2184}
2185
0a7f1fc0
JB
2186\f
2187/* Manipulating modifiers. */
284f4730 2188
0a7f1fc0 2189/* Parse the name of SYMBOL, and return the set of modifiers it contains.
284f4730 2190
0a7f1fc0
JB
2191 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
2192 SYMBOL's name of the end of the modifiers; the string from this
2193 position is the unmodified symbol name.
284f4730 2194
0a7f1fc0
JB
2195 This doesn't use any caches. */
2196static int
2197parse_modifiers_uncached (symbol, modifier_end)
284f4730 2198 Lisp_Object symbol;
0a7f1fc0 2199 int *modifier_end;
284f4730
JB
2200{
2201 struct Lisp_String *name;
2202 int i;
2203 int modifiers;
284f4730
JB
2204
2205 CHECK_SYMBOL (symbol, 1);
2206
2207 modifiers = 0;
2208 name = XSYMBOL (symbol)->name;
2209
284f4730 2210
0a7f1fc0 2211 for (i = 0; i+2 <= name->size; )
284f4730
JB
2212 switch (name->data[i])
2213 {
0a7f1fc0
JB
2214#define SINGLE_LETTER_MOD(bit) \
2215 if (name->data[i+1] != '-') \
2216 goto no_more_modifiers; \
2217 modifiers |= bit; \
fce33686 2218 i += 2;
0a7f1fc0
JB
2219
2220 case 'A':
2221 SINGLE_LETTER_MOD (alt_modifier);
284f4730
JB
2222 break;
2223
2224 case 'C':
0a7f1fc0 2225 SINGLE_LETTER_MOD (ctrl_modifier);
fce33686
JB
2226 break;
2227
2228 case 'H':
0a7f1fc0 2229 SINGLE_LETTER_MOD (hyper_modifier);
fce33686
JB
2230 break;
2231
2232 case 'M':
0a7f1fc0 2233 SINGLE_LETTER_MOD (meta_modifier);
284f4730
JB
2234 break;
2235
2236 case 'S':
0a7f1fc0 2237 SINGLE_LETTER_MOD (shift_modifier);
fce33686
JB
2238 break;
2239
2240 case 's':
86e5706b 2241 SINGLE_LETTER_MOD (super_modifier);
284f4730
JB
2242 break;
2243
fce33686
JB
2244 case 'd':
2245 if (i + 5 > name->size)
2246 goto no_more_modifiers;
2247 if (! strncmp (name->data + i, "drag-", 5))
2248 {
fce33686
JB
2249 modifiers |= drag_modifier;
2250 i += 5;
2251 }
2252 else if (! strncmp (name->data + i, "down-", 5))
2253 {
fce33686
JB
2254 modifiers |= down_modifier;
2255 i += 5;
2256 }
2257 else
2258 goto no_more_modifiers;
284f4730
JB
2259 break;
2260
2261 default:
2262 goto no_more_modifiers;
0a7f1fc0
JB
2263
2264#undef SINGLE_LETTER_MOD
284f4730
JB
2265 }
2266 no_more_modifiers:
2267
0a7f1fc0
JB
2268 /* Should we include the `click' modifier? */
2269 if (! (modifiers & (down_modifier | drag_modifier))
2270 && i + 7 == name->size
4bb994d1 2271 && strncmp (name->data + i, "mouse-", 6) == 0
6569cc8d 2272 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
0a7f1fc0
JB
2273 modifiers |= click_modifier;
2274
2275 if (modifier_end)
2276 *modifier_end = i;
2277
2278 return modifiers;
2279}
2280
2281
2282/* Return a symbol whose name is the modifier prefixes for MODIFIERS
2283 prepended to the string BASE[0..BASE_LEN-1].
2284 This doesn't use any caches. */
2285static Lisp_Object
2286apply_modifiers_uncached (modifiers, base, base_len)
2287 int modifiers;
2288 char *base;
2289 int base_len;
2290{
2291 /* Since BASE could contain nulls, we can't use intern here; we have
2292 to use Fintern, which expects a genuine Lisp_String, and keeps a
2293 reference to it. */
2294 char *new_mods =
86e5706b 2295 (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-"));
0a7f1fc0 2296 int mod_len;
284f4730 2297
284f4730 2298 {
0a7f1fc0
JB
2299 char *p = new_mods;
2300
2301 /* Only the event queue may use the `up' modifier; it should always
2302 be turned into a click or drag event before presented to lisp code. */
2303 if (modifiers & up_modifier)
2304 abort ();
2305
2306 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
2307 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
2308 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
2309 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
2310 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
86e5706b 2311 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
0a7f1fc0
JB
2312 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
2313 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
2314 /* The click modifier is denoted by the absence of other modifiers. */
2315
2316 *p = '\0';
2317
2318 mod_len = p - new_mods;
2319 }
284f4730 2320
0a7f1fc0
JB
2321 {
2322 Lisp_Object new_name = make_uninit_string (mod_len + base_len);
2323
2324 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
2325 bcopy (base, XSTRING (new_name)->data + mod_len, base_len);
284f4730
JB
2326
2327 return Fintern (new_name, Qnil);
2328 }
2329}
2330
2331
0a7f1fc0
JB
2332static char *modifier_names[] =
2333{
86e5706b
RS
2334 "up", 0, 0, 0, 0, 0, 0, "down",
2335 "drag", "click", 0, 0, 0, 0, 0, 0,
2336 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
0a7f1fc0
JB
2337};
2338
2339static Lisp_Object modifier_symbols;
2340
2341/* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
2342static Lisp_Object
2343lispy_modifier_list (modifiers)
2344 int modifiers;
2345{
2346 Lisp_Object modifier_list;
2347 int i;
2348
2349 modifier_list = Qnil;
2350 for (i = 0; (1<<i) <= modifiers; i++)
2351 if (modifiers & (1<<i))
8f805655
JB
2352 {
2353 if (i >= XVECTOR (modifier_symbols)->size)
2354 abort ();
2355 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
2356 modifier_list);
2357 }
0a7f1fc0
JB
2358
2359 return modifier_list;
2360}
2361
2362
2363/* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
2364 where UNMODIFIED is the unmodified form of SYMBOL,
2365 MASK is the set of modifiers present in SYMBOL's name.
2366 This is similar to parse_modifiers_uncached, but uses the cache in
2367 SYMBOL's Qevent_symbol_element_mask property, and maintains the
2368 Qevent_symbol_elements property. */
2369static Lisp_Object
2370parse_modifiers (symbol)
2371 Lisp_Object symbol;
2372{
2373 Lisp_Object elements = Fget (symbol, Qevent_symbol_element_mask);
2374
2375 if (CONSP (elements))
2376 return elements;
2377 else
2378 {
2379 int end;
2380 int modifiers = parse_modifiers_uncached (symbol, &end);
2381 Lisp_Object unmodified
2382 = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
2383 XSYMBOL (symbol)->name->size - end),
2384 Qnil);
2385 Lisp_Object mask;
2386
2387 XFASTINT (mask) = modifiers;
2388 elements = Fcons (unmodified, Fcons (mask, Qnil));
2389
2390 /* Cache the parsing results on SYMBOL. */
2391 Fput (symbol, Qevent_symbol_element_mask,
2392 elements);
2393 Fput (symbol, Qevent_symbol_elements,
2394 Fcons (unmodified, lispy_modifier_list (modifiers)));
2395
2396 /* Since we know that SYMBOL is modifiers applied to unmodified,
2397 it would be nice to put that in unmodified's cache.
2398 But we can't, since we're not sure that parse_modifiers is
2399 canonical. */
2400
2401 return elements;
2402 }
2403}
2404
2405/* Apply the modifiers MODIFIERS to the symbol BASE.
2406 BASE must be unmodified.
2407
2408 This is like apply_modifiers_uncached, but uses BASE's
2409 Qmodifier_cache property, if present. It also builds
cd21b839
JB
2410 Qevent_symbol_elements properties, since it has that info anyway.
2411
2412 apply_modifiers copies the value of BASE's Qevent_kind property to
2413 the modified symbol. */
0a7f1fc0
JB
2414static Lisp_Object
2415apply_modifiers (modifiers, base)
2416 int modifiers;
2417 Lisp_Object base;
2418{
7b4aedb9 2419 Lisp_Object cache, index, entry, new_symbol;
0a7f1fc0
JB
2420
2421 /* The click modifier never figures into cache indices. */
0a7f1fc0 2422 cache = Fget (base, Qmodifier_cache);
cd21b839 2423 XFASTINT (index) = (modifiers & ~click_modifier);
0a7f1fc0
JB
2424 entry = Fassq (index, cache);
2425
2426 if (CONSP (entry))
7b4aedb9
JB
2427 new_symbol = XCONS (entry)->cdr;
2428 else
2429 {
2430 /* We have to create the symbol ourselves. */
2431 new_symbol = apply_modifiers_uncached (modifiers,
2432 XSYMBOL (base)->name->data,
2433 XSYMBOL (base)->name->size);
2434
2435 /* Add the new symbol to the base's cache. */
2436 entry = Fcons (index, new_symbol);
2437 Fput (base, Qmodifier_cache, Fcons (entry, cache));
2438
2439 /* We have the parsing info now for free, so add it to the caches. */
2440 XFASTINT (index) = modifiers;
2441 Fput (new_symbol, Qevent_symbol_element_mask,
2442 Fcons (base, Fcons (index, Qnil)));
2443 Fput (new_symbol, Qevent_symbol_elements,
2444 Fcons (base, lispy_modifier_list (modifiers)));
2445 }
0a7f1fc0 2446
7b4aedb9
JB
2447 /* Make sure this symbol is of the same kind as BASE.
2448
2449 You'd think we could just set this once and for all when we
2450 intern the symbol above, but reorder_modifiers may call us when
2451 BASE's property isn't set right; we can't assume that just
2452 because we found something in the cache it must have its kind set
2453 right. */
2454 if (NILP (Fget (new_symbol, Qevent_kind)))
2455 {
2456 Lisp_Object kind = Fget (base, Qevent_kind);
2457
2458 if (! NILP (kind))
2459 Fput (new_symbol, Qevent_kind, kind);
2460 }
2461
2462 return new_symbol;
0a7f1fc0
JB
2463}
2464
2465
2466/* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
2467 return a symbol with the modifiers placed in the canonical order.
2468 Canonical order is alphabetical, except for down and drag, which
2469 always come last. The 'click' modifier is never written out.
2470
2471 Fdefine_key calls this to make sure that (for example) C-M-foo
2472 and M-C-foo end up being equivalent in the keymap. */
2473
2474Lisp_Object
2475reorder_modifiers (symbol)
2476 Lisp_Object symbol;
2477{
2478 /* It's hopefully okay to write the code this way, since everything
2479 will soon be in caches, and no consing will be done at all. */
2480 Lisp_Object parsed = parse_modifiers (symbol);
2481
2482 return apply_modifiers (XCONS (XCONS (parsed)->cdr)->car,
2483 XCONS (parsed)->car);
2484}
2485
2486
284f4730
JB
2487/* For handling events, we often want to produce a symbol whose name
2488 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
2489 to some base, like the name of a function key or mouse button.
2490 modify_event_symbol produces symbols of this sort.
2491
2492 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
2493 is the name of the i'th symbol. TABLE_SIZE is the number of elements
2494 in the table.
2495
2496 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
2497 persist between calls to modify_event_symbol that it can use to
2498 store a cache of the symbols it's generated for this NAME_TABLE
2499 before.
2500
2501 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
2502
2503 MODIFIERS is a set of modifier bits (as given in struct input_events)
2504 whose prefixes should be applied to the symbol name.
2505
2506 SYMBOL_KIND is the value to be placed in the event_kind property of
88cb0656
JB
2507 the returned symbol.
2508
2509 The symbols we create are supposed to have an
2510 `event-symbol-elements' propery, which lists the modifiers present
2511 in the symbol's name. */
2512
284f4730
JB
2513static Lisp_Object
2514modify_event_symbol (symbol_num, modifiers, symbol_kind, name_table,
2515 symbol_table, table_size)
2516 int symbol_num;
2517 unsigned modifiers;
2518 Lisp_Object symbol_kind;
2519 char **name_table;
2520 Lisp_Object *symbol_table;
2521 int table_size;
2522{
88cb0656 2523 Lisp_Object *slot;
284f4730
JB
2524
2525 /* Is this a request for a valid symbol? */
88cb0656 2526 if (symbol_num < 0 || symbol_num >= table_size)
284f4730
JB
2527 abort ();
2528
0a7f1fc0 2529 /* If *symbol_table doesn't seem to be initialized properly, fix that.
88cb0656 2530 *symbol_table should be a lisp vector TABLE_SIZE elements long,
4bb994d1
JB
2531 where the Nth element is the symbol for NAME_TABLE[N], or nil if
2532 we've never used that symbol before. */
284f4730
JB
2533 if (XTYPE (*symbol_table) != Lisp_Vector
2534 || XVECTOR (*symbol_table)->size != table_size)
88cb0656 2535 {
0a7f1fc0
JB
2536 Lisp_Object size;
2537
2538 XFASTINT (size) = table_size;
2539 *symbol_table = Fmake_vector (size, Qnil);
88cb0656 2540 }
284f4730 2541
88cb0656 2542 slot = & XVECTOR (*symbol_table)->contents[symbol_num];
284f4730 2543
0a7f1fc0
JB
2544 /* Have we already used this symbol before? */
2545 if (NILP (*slot))
284f4730 2546 {
0a7f1fc0
JB
2547 /* No; let's create it. */
2548 *slot = intern (name_table[symbol_num]);
2549
2550 /* Fill in the cache entries for this symbol; this also
2551 builds the Qevent_symbol_elements property, which the user
2552 cares about. */
6569cc8d 2553 apply_modifiers (modifiers & click_modifier, *slot);
0a7f1fc0 2554 Fput (*slot, Qevent_kind, symbol_kind);
284f4730 2555 }
88cb0656 2556
0a7f1fc0
JB
2557 /* Apply modifiers to that symbol. */
2558 return apply_modifiers (modifiers, *slot);
284f4730 2559}
0a7f1fc0 2560
284f4730 2561\f
284f4730
JB
2562/* Store into *addr a value nonzero if terminal input chars are available.
2563 Serves the purpose of ioctl (0, FIONREAD, addr)
2564 but works even if FIONREAD does not exist.
2565 (In fact, this may actually read some input.) */
2566
2567static void
2568get_input_pending (addr)
2569 int *addr;
2570{
2571 /* First of all, have we already counted some input? */
2572 *addr = !NILP (Vquit_flag) || readable_events ();
2573
2574 /* If input is being read as it arrives, and we have none, there is none. */
2575 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
2576 return;
2577
2578 /* Try to read some input and see how much we get. */
2579 gobble_input (0);
2580 *addr = !NILP (Vquit_flag) || readable_events ();
2581}
2582
2583/* Interface to read_avail_input, blocking SIGIO if necessary. */
2584
2585int
2586gobble_input (expected)
2587 int expected;
2588{
2589#ifndef VMS
2590#ifdef SIGIO
2591 if (interrupt_input)
2592 {
32676c08 2593 SIGMASKTYPE mask;
e065a56e 2594 mask = sigblockx (SIGIO);
284f4730 2595 read_avail_input (expected);
e065a56e 2596 sigsetmask (mask);
284f4730
JB
2597 }
2598 else
2599#endif
2600 read_avail_input (expected);
2601#endif
2602}
2603\f
2604#ifndef VMS
2605
2606/* Read any terminal input already buffered up by the system
2607 into the kbd_buffer, but do not wait.
2608
2609 EXPECTED should be nonzero if the caller knows there is some input.
2610
2611 Except on VMS, all input is read by this function.
2612 If interrupt_input is nonzero, this function MUST be called
2613 only when SIGIO is blocked.
2614
2615 Returns the number of keyboard chars read, or -1 meaning
2616 this is a bad time to try to read input. */
2617
2618static int
2619read_avail_input (expected)
2620 int expected;
2621{
2622 struct input_event buf[KBD_BUFFER_SIZE];
2623 register int i;
2624 int nread;
2625
2626 if (read_socket_hook)
2627 /* No need for FIONREAD or fcntl; just say don't wait. */
2628 nread = (*read_socket_hook) (0, buf, KBD_BUFFER_SIZE, expected, expected);
2629 else
2630 {
2631 unsigned char cbuf[KBD_BUFFER_SIZE];
2632
2633#ifdef FIONREAD
2634 /* Find out how much input is available. */
2635 if (ioctl (0, FIONREAD, &nread) < 0)
2636 /* Formerly simply reported no input, but that sometimes led to
2637 a failure of Emacs to terminate.
2638 SIGHUP seems appropriate if we can't reach the terminal. */
e4535288
RS
2639 /* ??? Is it really right to send the signal just to this process
2640 rather than to the whole process group?
2641 Perhaps on systems with FIONREAD Emacs is alone in its group. */
284f4730
JB
2642 kill (getpid (), SIGHUP);
2643 if (nread == 0)
2644 return 0;
2645 if (nread > sizeof cbuf)
2646 nread = sizeof cbuf;
2647#else /* no FIONREAD */
2648#ifdef USG
2649 /* Read some input if available, but don't wait. */
2650 nread = sizeof cbuf;
2651 fcntl (fileno (stdin), F_SETFL, O_NDELAY);
2652#else
2653 you lose;
2654#endif
2655#endif
2656
2657 /* Now read; for one reason or another, this will not block. */
2658 while (1)
2659 {
2660 nread = read (fileno (stdin), cbuf, nread);
2661#ifdef AIX
2662 /* The kernel sometimes fails to deliver SIGHUP for ptys.
2663 This looks incorrect, but it isn't, because _BSD causes
2664 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
2665 and that causes a value other than 0 when there is no input. */
2666 if (nread == 0)
2667 kill (SIGHUP, 0);
2668#endif
2669 /* Retry the read if it is interrupted. */
2670 if (nread >= 0
2671 || ! (errno == EAGAIN || errno == EFAULT
2672#ifdef EBADSLT
2673 || errno == EBADSLT
2674#endif
2675 ))
2676 break;
2677 }
2678
2679#ifndef FIONREAD
2680#ifdef USG
2681 fcntl (fileno (stdin), F_SETFL, 0);
2682#endif /* USG */
2683#endif /* no FIONREAD */
2684 for (i = 0; i < nread; i++)
2685 {
2686 buf[i].kind = ascii_keystroke;
86e5706b
RS
2687 buf[i].modifiers = 0;
2688 if (meta_key && (cbuf[i] & 0x80))
2689 buf[i].modifiers = meta_modifier;
2690 cbuf[i] &= ~0x80;
2691
7b4aedb9
JB
2692 XSET (buf[i].code, Lisp_Int, cbuf[i]);
2693#ifdef MULTI_FRAME
2694 XSET (buf[i].frame_or_window, Lisp_Frame, selected_frame);
2695#else
2696 buf[i].frame_or_window = Qnil;
2697#endif
284f4730
JB
2698 }
2699 }
2700
2701 /* Scan the chars for C-g and store them in kbd_buffer. */
2702 for (i = 0; i < nread; i++)
2703 {
2704 kbd_buffer_store_event (&buf[i]);
2705 /* Don't look at input that follows a C-g too closely.
2706 This reduces lossage due to autorepeat on C-g. */
2707 if (buf[i].kind == ascii_keystroke
2708 && XINT(buf[i].code) == quit_char)
2709 break;
2710 }
2711
2712 return nread;
2713}
2714#endif /* not VMS */
2715\f
2716#ifdef SIGIO /* for entire page */
2717/* Note SIGIO has been undef'd if FIONREAD is missing. */
2718
2ce30ea2 2719SIGTYPE
284f4730
JB
2720input_available_signal (signo)
2721 int signo;
2722{
2723 /* Must preserve main program's value of errno. */
2724 int old_errno = errno;
2725#ifdef BSD4_1
2726 extern int select_alarmed;
2727#endif
2728
2729#ifdef USG
2730 /* USG systems forget handlers when they are used;
2731 must reestablish each time */
2732 signal (signo, input_available_signal);
2733#endif /* USG */
2734
2735#ifdef BSD4_1
2736 sigisheld (SIGIO);
2737#endif
2738
ffd56f97
JB
2739 if (input_available_clear_time)
2740 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
284f4730
JB
2741
2742 while (1)
2743 {
2744 int nread;
2745 nread = read_avail_input (1);
2746 /* -1 means it's not ok to read the input now.
2747 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
2748 0 means there was no keyboard input available. */
2749 if (nread <= 0)
2750 break;
2751
2752#ifdef BSD4_1
2753 select_alarmed = 1; /* Force the select emulator back to life */
2754#endif
2755 }
2756
2757#ifdef BSD4_1
2758 sigfree ();
2759#endif
2760 errno = old_errno;
2761}
2762#endif /* SIGIO */
2763\f
2764/* Return the prompt-string of a sparse keymap.
2765 This is the first element which is a string.
2766 Return nil if there is none. */
2767
2768Lisp_Object
2769map_prompt (map)
2770 Lisp_Object map;
2771{
2772 while (CONSP (map))
2773 {
2774 register Lisp_Object tem;
2775 tem = Fcar (map);
2776 if (XTYPE (tem) == Lisp_String)
2777 return tem;
2778 map = Fcdr (map);
2779 }
2780 return Qnil;
2781}
2782
2783static int echo_flag;
2784static int echo_now;
2785
2786/* Read a character like read_char but optionally prompt based on maps
dbc4e1c1
JB
2787 in the array MAPS. NMAPS is the length of MAPS. Return nil if we
2788 decided not to read a character, because there are no menu items in
2789 MAPS.
7d6de002
RS
2790
2791 PREV_EVENT is the previous input event, or nil if we are reading
2792 the first event of a key sequence.
2793
6569cc8d
JB
2794 If USED_MOUSE_MENU is non-zero, then we set *USED_MOUSE_MENU to 1
2795 if we used a mouse menu to read the input, or zero otherwise. If
2796 USED_MOUSE_MENU is zero, *USED_MOUSE_MENU is left alone.
284f4730
JB
2797
2798 The prompting is done based on the prompt-string of the map
2799 and the strings associated with various map elements. */
2800
2801Lisp_Object
7d6de002
RS
2802read_char_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
2803 int nmaps;
2804 Lisp_Object *maps;
2805 Lisp_Object prev_event;
2806 int *used_mouse_menu;
284f4730 2807{
7d6de002
RS
2808 int mapno;
2809 register Lisp_Object name;
284f4730 2810 int nlength;
ff11dfa1 2811 int width = FRAME_WIDTH (selected_frame) - 4;
7d6de002
RS
2812 char *menu = (char *) alloca (width + 4);
2813 int idx = -1;
2814 Lisp_Object rest, vector;
2815
6569cc8d
JB
2816 if (used_mouse_menu)
2817 *used_mouse_menu = 0;
284f4730
JB
2818
2819 /* Use local over global Menu maps */
2820
7d6de002
RS
2821 if (! menu_prompting)
2822 return Qnil;
2823
2824 /* Get the menu name from the first map that has one (a prompt string). */
2825 for (mapno = 0; mapno < nmaps; mapno++)
2826 {
2827 name = map_prompt (maps[mapno]);
2828 if (!NILP (name))
2829 break;
2830 }
284f4730 2831
7d6de002 2832 /* If we don't have any menus, just read a character normally. */
dbc4e1c1 2833 if (mapno >= nmaps)
7d6de002
RS
2834 return Qnil;
2835
dbc4e1c1
JB
2836#ifdef HAVE_X_WINDOWS
2837#ifdef HAVE_X_MENU
7d6de002
RS
2838 /* If we got to this point via a mouse click,
2839 use a real menu for mouse selection. */
dbc4e1c1 2840 if (EVENT_HAS_PARAMETERS (prev_event))
7d6de002
RS
2841 {
2842 /* Display the menu and get the selection. */
2843 Lisp_Object *realmaps
2844 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
2845 Lisp_Object value;
2846 int nmaps1 = 0;
2847
2848 /* Use the maps that are not nil. */
2849 for (mapno = 0; mapno < nmaps; mapno++)
2850 if (!NILP (maps[mapno]))
2851 realmaps[nmaps1++] = maps[mapno];
2852
2853 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
2854 if (NILP (value))
2855 XSET (value, Lisp_Int, quit_char);
6569cc8d
JB
2856 if (used_mouse_menu)
2857 *used_mouse_menu = 1;
7d6de002
RS
2858 return value;
2859 }
dbc4e1c1
JB
2860#endif /* HAVE_X_MENU */
2861#endif /* HAVE_X_WINDOWS */
284f4730
JB
2862
2863 /* Prompt string always starts with map's prompt, and a space. */
2864 strcpy (menu, XSTRING (name)->data);
2865 nlength = XSTRING (name)->size;
7d6de002 2866 menu[nlength++] = ':';
284f4730
JB
2867 menu[nlength++] = ' ';
2868 menu[nlength] = 0;
2869
7d6de002
RS
2870 /* Start prompting at start of first map. */
2871 mapno = 0;
2872 rest = maps[mapno];
284f4730
JB
2873
2874 /* Present the documented bindings, a line at a time. */
2875 while (1)
2876 {
2877 int notfirst = 0;
2878 int i = nlength;
2879 Lisp_Object obj;
2880 int ch;
2881
284f4730 2882 /* Loop over elements of map. */
7d6de002 2883 while (i < width)
284f4730 2884 {
7d6de002 2885 Lisp_Object s, elt;
284f4730 2886
7d6de002
RS
2887 /* If reached end of map, start at beginning of next map. */
2888 if (NILP (rest))
2889 {
2890 mapno++;
2891 /* At end of last map, wrap around to first map if just starting,
2892 or end this line if already have something on it. */
2893 if (mapno == nmaps)
284f4730 2894 {
7d6de002
RS
2895 if (notfirst)
2896 break;
2897 else
2898 mapno = 0;
284f4730 2899 }
7d6de002 2900 rest = maps[mapno];
284f4730 2901 }
7d6de002
RS
2902
2903 /* Look at the next element of the map. */
2904 if (idx >= 0)
2905 elt = XVECTOR (vector)->contents[idx];
284f4730 2906 else
7d6de002
RS
2907 elt = Fcar_safe (rest);
2908
2909 if (idx < 0 && XTYPE (elt) == Lisp_Vector)
284f4730 2910 {
7d6de002
RS
2911 /* If we found a dense table in the keymap,
2912 advanced past it, but start scanning its contents. */
2913 rest = Fcdr_safe (rest);
2914 vector = elt;
2915 idx = 0;
284f4730 2916 }
7d6de002
RS
2917 else
2918 {
2919 /* An ordinary element. */
2920 s = Fcar_safe (Fcdr_safe (elt));
2921 if (XTYPE (s) != Lisp_String)
2922 /* Ignore the element if it has no prompt string. */
2923 ;
2924 /* If we have room for the prompt string, add it to this line.
2925 If this is the first on the line, always add it. */
2926 else if (XSTRING (s)->size + i < width
2927 || !notfirst)
2928 {
2929 int thiswidth;
284f4730 2930
7d6de002
RS
2931 /* Punctuate between strings. */
2932 if (notfirst)
2933 {
2934 strcpy (menu + i, ", ");
2935 i += 2;
2936 }
2937 notfirst = 1;
2938
2939 /* Add as much of string as fits. */
2940 thiswidth = XSTRING (s)->size;
2941 if (thiswidth + i > width)
2942 thiswidth = width - i;
2943 bcopy (XSTRING (s)->data, menu + i, thiswidth);
2944 i += thiswidth;
2945 }
2946 else
2947 {
2948 /* If this element does not fit, end the line now,
2949 and save the element for the next line. */
2950 strcpy (menu + i, "...");
2951 break;
2952 }
2953
2954 /* Move past this element. */
2955 if (idx >= 0 && idx + 1 >= XVECTOR (rest)->size)
2956 /* Handle reaching end of dense table. */
2957 idx = -1;
2958 if (idx >= 0)
2959 idx++;
2960 else
2961 rest = Fcdr_safe (rest);
2962 }
284f4730
JB
2963 }
2964
2965 /* Prompt with that and read response. */
2966 message1 (menu);
7d6de002 2967 obj = read_char (1, 0, 0, Qnil, 0);
284f4730
JB
2968
2969 if (XTYPE (obj) != Lisp_Int)
2970 return obj;
2971 else
2972 ch = XINT (obj);
2973
f4255cd1 2974 if (! EQ (obj, menu_prompt_more_char)
284f4730 2975 && (XTYPE (menu_prompt_more_char) != Lisp_Int
f4255cd1 2976 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
284f4730
JB
2977 return obj;
2978 }
2979}
284f4730
JB
2980\f
2981/* Reading key sequences. */
2982
2983/* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
2984 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
2985 keymap, or nil otherwise. Return the index of the first keymap in
2986 which KEY has any binding, or NMAPS if no map has a binding.
2987
2988 If KEY is a meta ASCII character, treat it like meta-prefix-char
2989 followed by the corresponding non-meta character. Keymaps in
2990 CURRENT with non-prefix bindings for meta-prefix-char become nil in
2991 NEXT.
2992
2993 When KEY is not defined in any of the keymaps, if it is an upper
2994 case letter and there are bindings for the corresponding lower-case
2995 letter, return the bindings for the lower-case letter.
2996
88cb0656
JB
2997 If KEY has no bindings in any of the CURRENT maps, NEXT is left
2998 unmodified.
2999
284f4730
JB
3000 NEXT may == CURRENT. */
3001
3002static int
3003follow_key (key, nmaps, current, defs, next)
3004 Lisp_Object key;
3005 Lisp_Object *current, *defs, *next;
3006 int nmaps;
3007{
3008 int i, first_binding;
3009
3010 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
3011 followed by the corresponding non-meta character. */
86e5706b 3012 if (XTYPE (key) == Lisp_Int && (XINT (key) & CHAR_META))
284f4730
JB
3013 {
3014 for (i = 0; i < nmaps; i++)
3015 if (! NILP (current[i]))
3016 {
cd21b839
JB
3017 next[i] =
3018 get_keyelt (access_keymap (current[i], meta_prefix_char, 1));
284f4730
JB
3019
3020 /* Note that since we pass the resulting bindings through
3021 get_keymap_1, non-prefix bindings for meta-prefix-char
3022 disappear. */
f4255cd1 3023 next[i] = get_keymap_1 (next[i], 0, 1);
284f4730
JB
3024 }
3025 else
3026 next[i] = Qnil;
3027
3028 current = next;
86e5706b 3029 XSET (key, Lisp_Int, XFASTINT (key) & ~CHAR_META);
284f4730
JB
3030 }
3031
3032 first_binding = nmaps;
3033 for (i = nmaps - 1; i >= 0; i--)
3034 {
3035 if (! NILP (current[i]))
3036 {
cd21b839 3037 defs[i] = get_keyelt (access_keymap (current[i], key, 1));
284f4730
JB
3038 if (! NILP (defs[i]))
3039 first_binding = i;
3040 }
3041 else
3042 defs[i] = Qnil;
3043 }
3044
3045 /* When KEY is not defined in any of the keymaps, if it is an upper
3046 case letter and there are bindings for the corresponding
3047 lower-case letter, return the bindings for the lower-case letter. */
3048 if (first_binding == nmaps
3049 && XTYPE (key) == Lisp_Int
86e5706b
RS
3050 && (UPPERCASEP (XINT (key) & 0x3ffff)
3051 || (XINT (key) & shift_modifier)))
284f4730 3052 {
86e5706b
RS
3053 if (XINT (key) & shift_modifier)
3054 XSETINT (key, XINT (key) & ~shift_modifier);
3055 else
3056 XSETINT (key, DOWNCASE (XINT (key)));
284f4730
JB
3057
3058 first_binding = nmaps;
3059 for (i = nmaps - 1; i >= 0; i--)
3060 {
3061 if (! NILP (current[i]))
3062 {
cd21b839 3063 defs[i] = get_keyelt (access_keymap (current[i], key, 1));
284f4730
JB
3064 if (! NILP (defs[i]))
3065 first_binding = i;
3066 }
3067 else
3068 defs[i] = Qnil;
3069 }
3070 }
86e5706b 3071
284f4730 3072 /* Given the set of bindings we've found, produce the next set of maps. */
0a7f1fc0
JB
3073 if (first_binding < nmaps)
3074 for (i = 0; i < nmaps; i++)
f4255cd1 3075 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
284f4730
JB
3076
3077 return first_binding;
3078}
3079
f4255cd1
JB
3080/* Read a sequence of keys that ends with a non prefix character,
3081 storing it in KEYBUF, a buffer of size BUFSIZE.
3082 Prompt with PROMPT.
284f4730
JB
3083 Return the length of the key sequence stored.
3084
f4255cd1
JB
3085 Echo starting immediately unless `prompt' is 0.
3086
3087 Where a key sequence ends depends on the currently active keymaps.
3088 These include any minor mode keymaps active in the current buffer,
3089 the current buffer's local map, and the global map.
3090
3091 If a key sequence has no other bindings, we check Vfunction_key_map
3092 to see if some trailing subsequence might be the beginning of a
3093 function key's sequence. If so, we try to read the whole function
3094 key, and substitute its symbolic name into the key sequence.
3095
3096 We ignore unbound `down-' mouse clicks. We turn unbound `drag-'
3097 events into similar click events, if that would make them bound.
3098
3099 If we get a mouse click in a mode line, vertical divider, or other
3100 non-text area, we treat the click as if it were prefixed by the
3101 symbol denoting that area - `mode-line', `vertical-line', or
3102 whatever.
3103
3104 If the sequence starts with a mouse click, we read the key sequence
3105 with respect to the buffer clicked on, not the current buffer.
284f4730 3106
f4255cd1
JB
3107 If the user switches frames in the midst of a key sequence, we put
3108 off the switch-frame event until later; the next call to
3109 read_char will return it. */
284f4730
JB
3110static int
3111read_key_sequence (keybuf, bufsize, prompt)
3112 Lisp_Object *keybuf;
3113 int bufsize;
f4255cd1 3114 char *prompt;
284f4730 3115{
f4255cd1
JB
3116 int count = specpdl_ptr - specpdl;
3117
284f4730
JB
3118 /* How many keys there are in the current key sequence. */
3119 int t;
3120
284f4730
JB
3121 /* The length of the echo buffer when we started reading, and
3122 the length of this_command_keys when we started reading. */
3123 int echo_start;
f4255cd1 3124 int keys_start;
284f4730
JB
3125
3126 /* The number of keymaps we're scanning right now, and the number of
3127 keymaps we have allocated space for. */
3128 int nmaps;
3129 int nmaps_allocated = 0;
3130
284f4730
JB
3131 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
3132 the current keymaps. */
3133 Lisp_Object *defs;
3134
f4255cd1
JB
3135 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
3136 in the current keymaps, or nil where it is not a prefix. */
3137 Lisp_Object *submaps;
3138
3139 /* The index in defs[] of the first keymap that has a binding for
3140 this key sequence. In other words, the lowest i such that
3141 defs[i] is non-nil. */
284f4730
JB
3142 int first_binding;
3143
f4255cd1 3144 /* If t < mock_input, then KEYBUF[t] should be read as the next
253598e4
JB
3145 input key.
3146
3147 We use this to recover after recognizing a function key. Once we
3148 realize that a suffix of the current key sequence is actually a
3149 function key's escape sequence, we replace the suffix with the
3150 function key's binding from Vfunction_key_map. Now keybuf
f4255cd1
JB
3151 contains a new and different key sequence, so the echo area,
3152 this_command_keys, and the submaps and defs arrays are wrong. In
3153 this situation, we set mock_input to t, set t to 0, and jump to
3154 restart_sequence; the loop will read keys from keybuf up until
3155 mock_input, thus rebuilding the state; and then it will resume
3156 reading characters from the keyboard. */
284f4730
JB
3157 int mock_input = 0;
3158
253598e4 3159 /* If the sequence is unbound in submaps[], then
f4255cd1
JB
3160 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
3161 and fkey_map is its binding.
253598e4 3162
f4255cd1
JB
3163 These might be > t, indicating that all function key scanning
3164 should hold off until t reaches them. We do this when we've just
3165 recognized a function key, to avoid searching for the function
3166 key's again in Vfunction_key_map. */
284f4730
JB
3167 int fkey_start = 0, fkey_end = 0;
3168 Lisp_Object fkey_map = Vfunction_key_map;
3169
cd21b839
JB
3170 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
3171 we put it off for later. While we're reading, we keep the event here. */
3172 Lisp_Object delayed_switch_frame = Qnil;
3173
f4255cd1
JB
3174
3175 /* If there is no function key map, turn off function key scanning. */
3176 if (NILP (Fkeymapp (Vfunction_key_map)))
3177 fkey_start = fkey_end = bufsize + 1;
3178
3179 /* We need to save the current buffer in case we switch buffers to
8f805655
JB
3180 find the right binding for a mouse click. Note that we can't use
3181 save_excursion_{save,restore} here, because they save point as
3182 well as the current buffer; we don't want to save point, because
3183 redisplay may change it, to accomodate a Fset_window_start or
3184 something. */
3185 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
f4255cd1 3186
7d6de002
RS
3187 last_nonmenu_event = Qnil;
3188
284f4730
JB
3189 if (INTERACTIVE)
3190 {
3191 if (prompt)
3192 echo_prompt (prompt);
3193 else if (cursor_in_echo_area)
3194 /* This doesn't put in a dash if the echo buffer is empty, so
3195 you don't always see a dash hanging out in the minibuffer. */
3196 echo_dash ();
284f4730
JB
3197 }
3198
f4255cd1
JB
3199 /* Record the initial state of the echo area and this_command_keys;
3200 we will need to restore them if we replay a key sequence. */
0a7f1fc0 3201 if (INTERACTIVE)
f4255cd1
JB
3202 echo_start = echo_length ();
3203 keys_start = this_command_key_count;
0a7f1fc0 3204
7b4aedb9
JB
3205 /* We jump here when the key sequence has been thoroughly changed, and
3206 we need to rescan it starting from the beginning. When we jump here,
3207 keybuf[0..mock_input] holds the sequence we should reread. */
07d2b8de 3208 replay_sequence:
7b4aedb9 3209
f4255cd1 3210 /* Build our list of keymaps.
07d2b8de
JB
3211 If we recognize a function key and replace its escape sequence in
3212 keybuf with its symbol, or if the sequence starts with a mouse
3213 click and we need to switch buffers, we jump back here to rebuild
3214 the initial keymaps from the current buffer. */
284f4730
JB
3215 {
3216 Lisp_Object *maps;
3217
3218 nmaps = current_minor_maps (0, &maps) + 2;
3219 if (nmaps > nmaps_allocated)
3220 {
253598e4 3221 submaps = (Lisp_Object *) alloca (nmaps * sizeof (submaps[0]));
284f4730
JB
3222 defs = (Lisp_Object *) alloca (nmaps * sizeof (defs[0]));
3223 nmaps_allocated = nmaps;
3224 }
253598e4 3225 bcopy (maps, submaps, (nmaps - 2) * sizeof (submaps[0]));
f4255cd1 3226 submaps[nmaps-2] = current_buffer->keymap;
253598e4 3227 submaps[nmaps-1] = global_map;
284f4730
JB
3228 }
3229
3230 /* Find an accurate initial value for first_binding. */
3231 for (first_binding = 0; first_binding < nmaps; first_binding++)
253598e4 3232 if (! NILP (submaps[first_binding]))
284f4730
JB
3233 break;
3234
f4255cd1
JB
3235 /* We jump here when a function key substitution has forced us to
3236 reprocess the current key sequence. keybuf[0..mock_input] is the
3237 sequence we want to reread. */
3238 t = 0;
3239
3240 /* These are no-ops the first time through, but if we restart, they
3241 revert the echo area and this_command_keys to their original state. */
3242 this_command_key_count = keys_start;
3243 if (INTERACTIVE)
3244 echo_truncate (echo_start);
3245
3246 /* If the best binding for the current key sequence is a keymap,
3247 or we may be looking at a function key's escape sequence, keep
3248 on reading. */
253598e4 3249 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
284f4730
JB
3250 || (first_binding >= nmaps && fkey_start < t))
3251 {
3252 Lisp_Object key;
7d6de002 3253 int used_mouse_menu = 0;
284f4730 3254
7b4aedb9
JB
3255 /* Where the last real key started. If we need to throw away a
3256 key that has expanded into more than one element of keybuf
3257 (say, a mouse click on the mode line which is being treated
3258 as [mode-line (mouse-...)], then we backtrack to this point
3259 of keybuf. */
3260 int last_real_key_start;
3261
0a7f1fc0
JB
3262 /* These variables are analogous to echo_start and keys_start;
3263 while those allow us to restart the entire key sequence,
3264 echo_local_start and keys_local_start allow us to throw away
3265 just one key. */
f4255cd1
JB
3266 int echo_local_start, keys_local_start, local_first_binding;
3267
284f4730
JB
3268 if (t >= bufsize)
3269 error ("key sequence too long");
3270
f4255cd1
JB
3271 if (INTERACTIVE)
3272 echo_local_start = echo_length ();
3273 keys_local_start = this_command_key_count;
3274 local_first_binding = first_binding;
3275
3276 replay_key:
0a7f1fc0 3277 /* These are no-ops, unless we throw away a keystroke below and
f4255cd1
JB
3278 jumped back up to replay_key; in that case, these restore the
3279 variables to their original state, allowing us to replay the
0a7f1fc0 3280 loop. */
f4255cd1
JB
3281 if (INTERACTIVE)
3282 echo_truncate (echo_local_start);
0a7f1fc0
JB
3283 this_command_key_count = keys_local_start;
3284 first_binding = local_first_binding;
3285
f4255cd1 3286 /* Does mock_input indicate that we are re-reading a key sequence? */
284f4730
JB
3287 if (t < mock_input)
3288 {
3289 key = keybuf[t];
3290 add_command_key (key);
3291 echo_char (key);
3292 }
253598e4
JB
3293
3294 /* If not, we should actually read a character. */
284f4730
JB
3295 else
3296 {
7b4aedb9
JB
3297 last_real_key_start = t;
3298
7d6de002
RS
3299 key = read_char (!prompt, nmaps, submaps, last_nonmenu_event,
3300 &used_mouse_menu);
284f4730 3301
f4255cd1 3302 /* read_char returns -1 at the end of a macro.
284f4730
JB
3303 Emacs 18 handles this by returning immediately with a
3304 zero, so that's what we'll do. */
86e5706b 3305 if (XTYPE (key) == Lisp_Int && XINT (key) == -1)
cd21b839 3306 {
f4255cd1
JB
3307 t = 0;
3308 goto done;
cd21b839 3309 }
284f4730
JB
3310
3311 Vquit_flag = Qnil;
3312
0a7f1fc0
JB
3313 /* Clicks in non-text areas get prefixed by the symbol
3314 in their CHAR-ADDRESS field. For example, a click on
f4255cd1
JB
3315 the mode line is prefixed by the symbol `mode-line'.
3316
3317 Furthermore, key sequences beginning with mouse clicks
3318 are read using the keymaps of the buffer clicked on, not
3319 the current buffer. So we may have to switch the buffer
3320 here. */
cd21b839 3321 if (EVENT_HAS_PARAMETERS (key))
0a7f1fc0 3322 {
cd21b839 3323 Lisp_Object kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
f4255cd1 3324
cd21b839 3325 if (EQ (kind, Qmouse_click))
0a7f1fc0 3326 {
f4255cd1
JB
3327 Lisp_Object window = POSN_WINDOW (EVENT_START (key));
3328 Lisp_Object posn = POSN_BUFFER_POSN (EVENT_START (key));
3329
3330 /* Key sequences beginning with mouse clicks are
3331 read using the keymaps in the buffer clicked on,
3332 not the current buffer. If we're at the
3333 beginning of a key sequence, switch buffers. */
3334 if (t == 0
3335 && XTYPE (window) == Lisp_Window
3336 && XTYPE (XWINDOW (window)->buffer) == Lisp_Buffer
3337 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
3338 {
3339 if (XTYPE (posn) == Lisp_Symbol)
3340 {
3341 if (t + 1 >= bufsize)
3342 error ("key sequence too long");
3343 keybuf[t] = posn;
3344 keybuf[t+1] = key;
3345 mock_input = t + 2;
3346 }
3347 else
3348 {
3349 keybuf[t] = key;
3350 mock_input = t + 1;
3351 }
0a7f1fc0 3352
f4255cd1 3353 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
07d2b8de 3354 goto replay_sequence;
f4255cd1
JB
3355 }
3356 else if (XTYPE (posn) == Lisp_Symbol)
cd21b839
JB
3357 {
3358 if (t + 1 >= bufsize)
3359 error ("key sequence too long");
3360 keybuf[t] = posn;
3361 keybuf[t+1] = key;
3362 mock_input = t + 2;
3363
f4255cd1 3364 goto replay_key;
cd21b839
JB
3365 }
3366 }
3367 else if (EQ (kind, Qswitch_frame))
3368 {
3369 /* If we're at the beginning of a key sequence, go
3370 ahead and return this event. If we're in the
3371 midst of a key sequence, delay it until the end. */
3372 if (t > 0)
3373 {
3374 delayed_switch_frame = key;
f4255cd1 3375 goto replay_key;
cd21b839 3376 }
0a7f1fc0
JB
3377 }
3378 }
284f4730 3379 }
f4255cd1
JB
3380
3381 /* We have finally decided that KEY is something we might want
3382 to look up. */
284f4730
JB
3383 first_binding = (follow_key (key,
3384 nmaps - first_binding,
253598e4 3385 submaps + first_binding,
284f4730 3386 defs + first_binding,
253598e4 3387 submaps + first_binding)
284f4730 3388 + first_binding);
0a7f1fc0 3389
f4255cd1 3390 /* If KEY wasn't bound, we'll try some fallbacks. */
0a7f1fc0
JB
3391 if (first_binding >= nmaps)
3392 {
3393 Lisp_Object head = EVENT_HEAD (key);
3394
3395 if (XTYPE (head) == Lisp_Symbol)
3396 {
3397 Lisp_Object breakdown = parse_modifiers (head);
3398 Lisp_Object modifiers =
3399 XINT (XCONS (XCONS (breakdown)->cdr)->car);
3400
3401 /* We drop unbound `down-' events altogether. */
3402 if (modifiers & down_modifier)
3403 {
7b4aedb9
JB
3404 /* Dispose of this event by simply jumping back to
3405 replay_key, to get another event.
3406
3407 Note that if this event came from mock input,
3408 then just jumping back to replay_key will just
3409 hand it to us again. So we have to wipe out any
3410 mock input.
3411
3412 We could delete keybuf[t] and shift everything
3413 after that to the left by one spot, but we'd also
3414 have to fix up any variable that points into
3415 keybuf, and shifting isn't really necessary
3416 anyway.
3417
3418 Adding prefixes for non-textual mouse clicks
3419 creates two characters of mock input, and both
3420 must be thrown away. If we're only looking at
3421 the prefix now, we can just jump back to
3422 replay_key. On the other hand, if we've already
3423 processed the prefix, and now the actual click
3424 itself is giving us trouble, then we've lost the
3425 state of the keymaps we want to backtrack to, and
3426 we need to replay the whole sequence to rebuild
3427 it.
3428
f4255cd1
JB
3429 Beyond that, only function key expansion could
3430 create more than two keys, but that should never
3431 generate mouse events, so it's okay to zero
3432 mock_input in that case too.
7b4aedb9 3433
0a7f1fc0 3434 Isn't this just the most wonderful code ever? */
7b4aedb9
JB
3435 if (t == last_real_key_start)
3436 {
3437 mock_input = 0;
3438 goto replay_key;
3439 }
3440 else
3441 {
3442 mock_input = last_real_key_start;
3443 goto replay_sequence;
3444 }
0a7f1fc0
JB
3445 }
3446
3447 /* We turn unbound `drag-' events into `click-'
3448 events, if the click would be bound. */
3449 else if (modifiers & drag_modifier)
3450 {
3451 Lisp_Object new_head =
3452 apply_modifiers (modifiers & ~drag_modifier,
3453 XCONS (breakdown)->car);
3454 Lisp_Object new_click =
3455 Fcons (new_head, Fcons (EVENT_START (key), Qnil));
3456
3457 /* Look for a binding for this new key. follow_key
3458 promises that it didn't munge submaps the
3459 last time we called it, since key was unbound. */
3460 first_binding =
3461 (follow_key (new_click,
3462 nmaps - local_first_binding,
3463 submaps + local_first_binding,
3464 defs + local_first_binding,
3465 submaps + local_first_binding)
3466 + local_first_binding);
3467
3468 /* If that click is bound, go for it. */
3469 if (first_binding < nmaps)
3470 key = new_click;
3471 /* Otherwise, we'll leave key set to the drag event. */
3472 }
3473 }
3474 }
3475
284f4730 3476 keybuf[t++] = key;
7d6de002
RS
3477 /* Normally, last_nonmenu_event gets the previous key we read.
3478 But when a mouse popup menu is being used,
3479 we don't update last_nonmenu_event; it continues to hold the mouse
3480 event that preceded the first level of menu. */
3481 if (!used_mouse_menu)
3482 last_nonmenu_event = key;
284f4730
JB
3483
3484 /* If the sequence is unbound, see if we can hang a function key
253598e4
JB
3485 off the end of it. We only want to scan real keyboard input
3486 for function key sequences, so if mock_input says that we're
f4255cd1 3487 re-reading old events, don't examine it. */
284f4730 3488 if (first_binding >= nmaps
253598e4 3489 && t >= mock_input)
284f4730
JB
3490 {
3491 Lisp_Object fkey_next;
3492
3493 /* Scan from fkey_end until we find a bound suffix. */
3494 while (fkey_end < t)
3495 {
f4255cd1
JB
3496 Lisp_Object key;
3497
3498 key = keybuf[fkey_end++];
067ffa38
JB
3499 /* Look up meta-characters by prefixing them
3500 with meta_prefix_char. I hate this. */
d3cc13fa 3501 if (XTYPE (key) == Lisp_Int && XINT (key) & meta_modifier)
f4255cd1
JB
3502 {
3503 fkey_next =
3504 get_keymap_1
3505 (get_keyelt
3506 (access_keymap
3507 (fkey_map, meta_prefix_char, 1)),
3508 0, 1);
d3cc13fa 3509 XFASTINT (key) = XFASTINT (key) & ~meta_modifier;
f4255cd1 3510 }
067ffa38
JB
3511 else
3512 fkey_next = fkey_map;
3513
284f4730 3514 fkey_next =
f4255cd1 3515 get_keyelt (access_keymap (fkey_next, key, 1));
067ffa38 3516
85bc5181 3517 /* If keybuf[fkey_start..fkey_end] is bound in the
a764a753 3518 function key map and it's a suffix of the current
85bc5181 3519 sequence (i.e. fkey_end == t), replace it with
a764a753 3520 the binding and restart with fkey_start at the end. */
284f4730
JB
3521 if (XTYPE (fkey_next) == Lisp_Vector
3522 && fkey_end == t)
3523 {
3524 t = fkey_start + XVECTOR (fkey_next)->size;
3525 if (t >= bufsize)
3526 error ("key sequence too long");
3527
3528 bcopy (XVECTOR (fkey_next)->contents,
3529 keybuf + fkey_start,
3530 (t - fkey_start) * sizeof (keybuf[0]));
3531
3532 mock_input = t;
3533 fkey_start = fkey_end = t;
3534
f4255cd1 3535 goto replay_sequence;
284f4730
JB
3536 }
3537
f4255cd1 3538 fkey_map = get_keymap_1 (fkey_next, 0, 1);
284f4730 3539
a764a753
JB
3540 /* If we no longer have a bound suffix, try a new positions for
3541 fkey_start. */
284f4730
JB
3542 if (NILP (fkey_map))
3543 {
3544 fkey_end = ++fkey_start;
3545 fkey_map = Vfunction_key_map;
3546 }
3547 }
3548 }
3549 }
3550
3551 read_key_sequence_cmd = (first_binding < nmaps
3552 ? defs[first_binding]
3553 : Qnil);
3554
f4255cd1 3555 done:
cd21b839 3556 unread_switch_frame = delayed_switch_frame;
f4255cd1 3557 unbind_to (count, Qnil);
284f4730
JB
3558 return t;
3559}
3560
c0a58692 3561DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 2, 0,
284f4730
JB
3562 "Read a sequence of keystrokes and return as a string or vector.\n\
3563The sequence is sufficient to specify a non-prefix command in the\n\
3564current local and global maps.\n\
3565\n\
c0a58692
RS
3566First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
3567Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
3568as a continuation of the previous key.\n\
284f4730 3569\n\
cb5df6ae
JB
3570A C-g typed while in this function is treated like any other character,\n\
3571and `quit-flag' is not set.\n\
3572\n\
3573If the key sequence starts with a mouse click, then the sequence is read\n\
3574using the keymaps of the buffer of the window clicked in, not the buffer\n\
3575of the selected window as normal.\n\
3576\n\
3577`read-key-sequence' drops unbound button-down events, since you normally\n\
3578only care about the click or drag events which follow them. If a drag\n\
3579event is unbound, but the corresponding click event would be bound,\n\
3580`read-key-sequence' turns the drag event into a click event at the\n\
3581drag's starting position. This means that you don't have to distinguish\n\
3582between click and drag events unless you want to.\n\
3583\n\
3584`read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
3c370943
JB
3585lines separating windows, and scroll bars with imaginary keys\n\
3586`mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
cb5df6ae
JB
3587\n\
3588If the user switches frames in the middle of a key sequence, the\n\
3589frame-switch event is put off until after the current key sequence.\n\
3590\n\
3591`read-key-sequence' checks `function-key-map' for function key\n\
3592sequences, where they wouldn't conflict with ordinary bindings. See\n\
4bb994d1 3593`function-key-map' for more details.")
c0a58692
RS
3594 (prompt, continue_echo)
3595 Lisp_Object prompt, continue_echo;
284f4730
JB
3596{
3597 Lisp_Object keybuf[30];
3598 register int i;
3599 struct gcpro gcpro1, gcpro2;
3600
3601 if (!NILP (prompt))
3602 CHECK_STRING (prompt, 0);
3603 QUIT;
3604
3605 bzero (keybuf, sizeof keybuf);
3606 GCPRO1 (keybuf[0]);
3607 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
3608
daa37602 3609 if (NILP (continue_echo))
c0a58692
RS
3610 this_command_key_count = 0;
3611
284f4730 3612 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
f4255cd1 3613 NILP (prompt) ? 0 : XSTRING (prompt)->data);
284f4730
JB
3614
3615 UNGCPRO;
86e5706b 3616 return make_event_array (i, keybuf);
284f4730
JB
3617}
3618\f
3619DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
3620 "Execute CMD as an editor command.\n\
3621CMD must be a symbol that satisfies the `commandp' predicate.\n\
3622Optional second arg RECORD-FLAG non-nil\n\
3623means unconditionally put this command in `command-history'.\n\
3624Otherwise, that is done only if an arg is read using the minibuffer.")
3625 (cmd, record)
3626 Lisp_Object cmd, record;
3627{
3628 register Lisp_Object final;
3629 register Lisp_Object tem;
3630 Lisp_Object prefixarg;
3631 struct backtrace backtrace;
3632 extern int debug_on_next_call;
3633
3634 prefixarg = Vprefix_arg, Vprefix_arg = Qnil;
3635 Vcurrent_prefix_arg = prefixarg;
3636 debug_on_next_call = 0;
3637
3638 if (XTYPE (cmd) == Lisp_Symbol)
3639 {
3640 tem = Fget (cmd, Qdisabled);
3641 if (!NILP (tem))
3642 return call1 (Vrun_hooks, Vdisabled_command_hook);
3643 }
3644
3645 while (1)
3646 {
ffd56f97 3647 final = Findirect_function (cmd);
284f4730
JB
3648
3649 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
3650 do_autoload (final, cmd);
3651 else
3652 break;
3653 }
3654
3655 if (XTYPE (final) == Lisp_String
3656 || XTYPE (final) == Lisp_Vector)
3657 {
3658 /* If requested, place the macro in the command history. For
3659 other sorts of commands, call-interactively takes care of
3660 this. */
3661 if (!NILP (record))
3662 Vcommand_history
3663 = Fcons (Fcons (Qexecute_kbd_macro,
3664 Fcons (final, Fcons (prefixarg, Qnil))),
3665 Vcommand_history);
3666
3667 return Fexecute_kbd_macro (final, prefixarg);
3668 }
3669 if (CONSP (final) || XTYPE (final) == Lisp_Subr
3670 || XTYPE (final) == Lisp_Compiled)
3671 {
3672 backtrace.next = backtrace_list;
3673 backtrace_list = &backtrace;
3674 backtrace.function = &Qcall_interactively;
3675 backtrace.args = &cmd;
3676 backtrace.nargs = 1;
3677 backtrace.evalargs = 0;
3678
3679 tem = Fcall_interactively (cmd, record);
3680
3681 backtrace_list = backtrace.next;
3682 return tem;
3683 }
3684 return Qnil;
3685}
3686\f
284f4730
JB
3687DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
3688 1, 1, "P",
3689 "Read function name, then read its arguments and call it.")
3690 (prefixarg)
3691 Lisp_Object prefixarg;
3692{
3693 Lisp_Object function;
3694 char buf[40];
3695 Lisp_Object saved_keys;
3696 struct gcpro gcpro1;
3697
3698 saved_keys = Fthis_command_keys ();
3699 buf[0] = 0;
3700 GCPRO1 (saved_keys);
3701
3702 if (EQ (prefixarg, Qminus))
3703 strcpy (buf, "- ");
3704 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
3705 strcpy (buf, "C-u ");
3706 else if (CONSP (prefixarg) && XTYPE (XCONS (prefixarg)->car) == Lisp_Int)
3707 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
3708 else if (XTYPE (prefixarg) == Lisp_Int)
3709 sprintf (buf, "%d ", XINT (prefixarg));
3710
3711 /* This isn't strictly correct if execute-extended-command
3712 is bound to anything else. Perhaps it should use
3713 this_command_keys? */
3714 strcat (buf, "M-x ");
3715
3716 /* Prompt with buf, and then read a string, completing from and
3717 restricting to the set of all defined commands. Don't provide
3718 any initial input. The last Qnil says not to perform a
3719 peculiar hack on the initial input. */
3720 function = Fcompleting_read (build_string (buf),
3721 Vobarray, Qcommandp,
3722 Qt, Qnil, Qnil);
3723
1113d9db
JB
3724 /* Set this_command_keys to the concatenation of saved_keys and
3725 function, followed by a RET. */
284f4730 3726 {
1113d9db 3727 struct Lisp_String *str;
284f4730
JB
3728 int i;
3729 Lisp_Object tem;
3730
1113d9db
JB
3731 this_command_key_count = 0;
3732
3733 str = XSTRING (saved_keys);
3734 for (i = 0; i < str->size; i++)
284f4730 3735 {
1113d9db 3736 XFASTINT (tem) = str->data[i];
284f4730
JB
3737 add_command_key (tem);
3738 }
1113d9db
JB
3739
3740 str = XSTRING (function);
3741 for (i = 0; i < str->size; i++)
3742 {
3743 XFASTINT (tem) = str->data[i];
3744 add_command_key (tem);
3745 }
3746
3747 XFASTINT (tem) = '\015';
3748 add_command_key (tem);
284f4730
JB
3749 }
3750
3751 UNGCPRO;
3752
0a7f1fc0 3753 function = Fintern (function, Qnil);
284f4730
JB
3754 Vprefix_arg = prefixarg;
3755 this_command = function;
3756
3757 return Fcommand_execute (function, Qt);
3758}
3759\f
3760
3761detect_input_pending ()
3762{
3763 if (!input_pending)
3764 get_input_pending (&input_pending);
3765
3766 return input_pending;
3767}
3768
ffd56f97
JB
3769/* This is called in some cases before a possible quit.
3770 It cases the next call to detect_input_pending to recompute input_pending.
3771 So calling this function unnecessarily can't do any harm. */
3772clear_input_pending ()
3773{
3774 input_pending = 0;
3775}
3776
284f4730
JB
3777DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
3778 "T if command input is currently available with no waiting.\n\
3779Actually, the value is nil only if we can be sure that no input is available.")
3780 ()
3781{
86e5706b 3782 if (!NILP (unread_command_events) || unread_command_char != -1)
284f4730
JB
3783 return (Qt);
3784
3785 return detect_input_pending () ? Qt : Qnil;
3786}
3787
3788DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
22d7cb89 3789 "Return vector of last 100 events, not counting those from keyboard macros.")
284f4730
JB
3790 ()
3791{
5160df46 3792 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
284f4730
JB
3793 Lisp_Object val;
3794
3795 if (total_keys < NUM_RECENT_KEYS)
5160df46 3796 return Fvector (total_keys, keys);
284f4730
JB
3797 else
3798 {
5160df46
JB
3799 val = Fvector (NUM_RECENT_KEYS, keys);
3800 bcopy (keys + recent_keys_index,
284f4730
JB
3801 XVECTOR (val)->contents,
3802 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
5160df46 3803 bcopy (keys,
284f4730
JB
3804 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
3805 recent_keys_index * sizeof (Lisp_Object));
3806 return val;
3807 }
3808}
3809
3810DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
3811 "Return string of the keystrokes that invoked this command.")
3812 ()
3813{
86e5706b
RS
3814 return make_event_array (this_command_key_count,
3815 XVECTOR (this_command_keys)->contents);
284f4730
JB
3816}
3817
3818DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
3819 "Return the current depth in recursive edits.")
3820 ()
3821{
3822 Lisp_Object temp;
3823 XFASTINT (temp) = command_loop_level + minibuf_level;
3824 return temp;
3825}
3826
3827DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
3828 "FOpen dribble file: ",
9b2471df
RS
3829 "Start writing all keyboard characters to a dribble file called FILE.\n\
3830If FILE is nil, close any open dribble file.")
284f4730
JB
3831 (file)
3832 Lisp_Object file;
3833{
3834 if (NILP (file))
3835 {
3836 fclose (dribble);
3837 dribble = 0;
3838 }
3839 else
3840 {
3841 file = Fexpand_file_name (file, Qnil);
3842 dribble = fopen (XSTRING (file)->data, "w");
3843 }
3844 return Qnil;
3845}
3846
3847DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
3848 "Discard the contents of the terminal input buffer.\n\
3849Also cancel any kbd macro being defined.")
3850 ()
3851{
3852 defining_kbd_macro = 0;
3853 update_mode_lines++;
3854
dbc4e1c1 3855 unread_command_events = Qnil;
86e5706b 3856 unread_command_char = -1;
284f4730
JB
3857
3858 discard_tty_input ();
3859
ff0b5f4c
JB
3860 /* Without the cast, GCC complains that this assignment loses the
3861 volatile qualifier of kbd_store_ptr. Is there anything wrong
3862 with that? */
3863 kbd_fetch_ptr = (struct input_event *) kbd_store_ptr;
7b4aedb9 3864 Ffillarray (kbd_buffer_frame_or_window, Qnil);
284f4730
JB
3865 input_pending = 0;
3866
3867 return Qnil;
3868}
3869\f
3870DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
3871 "Stop Emacs and return to superior process. You can resume later.\n\
3872On systems that don't have job control, run a subshell instead.\n\n\
3873If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
b7d2ebbf
RS
3874to be read as terminal input by Emacs's parent, after suspension.\n\
3875\n\
3876Before suspending, call the functions in `suspend-hooks' with no args.\n\
3877If any of them returns nil, don't call the rest and don't suspend.\n\
3878Otherwise, suspend normally and after resumption run the normal hook\n\
284f4730
JB
3879`suspend-resume-hook' if that is bound and non-nil.\n\
3880\n\
3881Some operating systems cannot stop the Emacs process and resume it later.\n\
b7d2ebbf 3882On such systems, Emacs starts a subshell instead of suspending.")
284f4730
JB
3883 (stuffstring)
3884 Lisp_Object stuffstring;
3885{
3a69360c 3886 Lisp_Object tem;
284f4730
JB
3887 int count = specpdl_ptr - specpdl;
3888 int old_height, old_width;
3889 int width, height;
b7d2ebbf 3890 struct gcpro gcpro1, gcpro2;
284f4730
JB
3891 extern init_sys_modes ();
3892
3893 if (!NILP (stuffstring))
3894 CHECK_STRING (stuffstring, 0);
284f4730 3895
b7d2ebbf
RS
3896 /* Run the functions in suspend-hooks. */
3897 tem = Fsymbol_value (intern ("suspend-hooks"));
3898 while (CONSP (tem))
284f4730 3899 {
b7d2ebbf
RS
3900 Lisp_Object val;
3901 GCPRO2 (stuffstring, tem);
3902 val = call0 (Fcar (tem));
3903 UNGCPRO;
3904 tem = Fcdr (tem);
3905 if (!EQ (val, Qnil)) return Qnil;
284f4730
JB
3906 }
3907
b7d2ebbf 3908 GCPRO1 (stuffstring);
ff11dfa1 3909 get_frame_size (&old_width, &old_height);
284f4730
JB
3910 reset_sys_modes ();
3911 /* sys_suspend can get an error if it tries to fork a subshell
3912 and the system resources aren't available for that. */
3913 record_unwind_protect (init_sys_modes, 0);
3914 stuff_buffered_input (stuffstring);
3915 sys_suspend ();
3916 unbind_to (count, Qnil);
3917
3918 /* Check if terminal/window size has changed.
3919 Note that this is not useful when we are running directly
3920 with a window system; but suspend should be disabled in that case. */
ff11dfa1 3921 get_frame_size (&width, &height);
284f4730 3922 if (width != old_width || height != old_height)
d5045cf9 3923 change_frame_size (0, height, width, 0, 0);
284f4730
JB
3924
3925 /* Call value of suspend-resume-hook
3926 if it is bound and value is non-nil. */
3927 if (!NILP (Vrun_hooks))
3928 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
3929
3930 UNGCPRO;
3931 return Qnil;
3932}
3933
3934/* If STUFFSTRING is a string, stuff its contents as pending terminal input.
3935 Then in any case stuff anthing Emacs has read ahead and not used. */
3936
3937stuff_buffered_input (stuffstring)
3938 Lisp_Object stuffstring;
3939{
3940 register unsigned char *p;
3941
3942/* stuff_char works only in BSD, versions 4.2 and up. */
3943#ifdef BSD
3944#ifndef BSD4_1
3945 if (XTYPE (stuffstring) == Lisp_String)
3946 {
3947 register int count;
3948
3949 p = XSTRING (stuffstring)->data;
3950 count = XSTRING (stuffstring)->size;
3951 while (count-- > 0)
3952 stuff_char (*p++);
3953 stuff_char ('\n');
3954 }
3955 /* Anything we have read ahead, put back for the shell to read. */
3956 while (kbd_fetch_ptr != kbd_store_ptr)
3957 {
3958 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
3959 kbd_fetch_ptr = kbd_buffer;
3960 if (kbd_fetch_ptr->kind == ascii_keystroke)
3961 stuff_char (XINT (kbd_fetch_ptr->code));
4bb994d1 3962 kbd_fetch_ptr->kind = no_event;
7b4aedb9
JB
3963 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
3964 - kbd_buffer]
3965 = Qnil);
284f4730
JB
3966 kbd_fetch_ptr++;
3967 }
3968 input_pending = 0;
3969#endif
3970#endif /* BSD and not BSD4_1 */
3971}
3972\f
ffd56f97
JB
3973set_waiting_for_input (time_to_clear)
3974 EMACS_TIME *time_to_clear;
284f4730 3975{
ffd56f97 3976 input_available_clear_time = time_to_clear;
284f4730
JB
3977
3978 /* Tell interrupt_signal to throw back to read_char, */
3979 waiting_for_input = 1;
3980
3981 /* If interrupt_signal was called before and buffered a C-g,
3982 make it run again now, to avoid timing error. */
3983 if (!NILP (Vquit_flag))
3984 quit_throw_to_read_char ();
3985
3986 /* If alarm has gone off already, echo now. */
3987 if (echo_flag)
3988 {
3989 echo ();
3990 echo_flag = 0;
3991 }
3992}
3993
3994clear_waiting_for_input ()
3995{
3996 /* Tell interrupt_signal not to throw back to read_char, */
3997 waiting_for_input = 0;
ffd56f97 3998 input_available_clear_time = 0;
284f4730
JB
3999}
4000
4001/* This routine is called at interrupt level in response to C-G.
4002 If interrupt_input, this is the handler for SIGINT.
4003 Otherwise, it is called from kbd_buffer_store_event,
4004 in handling SIGIO or SIGTINT.
4005
4006 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
4007 immediately throw back to read_char.
4008
4009 Otherwise it sets the Lisp variable quit-flag not-nil.
4010 This causes eval to throw, when it gets a chance.
4011 If quit-flag is already non-nil, it stops the job right away. */
4012
4013SIGTYPE
4014interrupt_signal ()
4015{
4016 char c;
4017 /* Must preserve main program's value of errno. */
4018 int old_errno = errno;
4019 extern Lisp_Object Vwindow_system;
4020
4021#ifdef USG
4022 /* USG systems forget handlers when they are used;
4023 must reestablish each time */
4024 signal (SIGINT, interrupt_signal);
4025 signal (SIGQUIT, interrupt_signal);
4026#endif /* USG */
4027
4028 cancel_echoing ();
4029
d5045cf9 4030 if (!NILP (Vquit_flag) && FRAME_TERMCAP_P (selected_frame))
284f4730
JB
4031 {
4032 fflush (stdout);
4033 reset_sys_modes ();
4034 sigfree ();
4035#ifdef SIGTSTP /* Support possible in later USG versions */
4036/*
4037 * On systems which can suspend the current process and return to the original
4038 * shell, this command causes the user to end up back at the shell.
4039 * The "Auto-save" and "Abort" questions are not asked until
4040 * the user elects to return to emacs, at which point he can save the current
4041 * job and either dump core or continue.
4042 */
4043 sys_suspend ();
4044#else
4045#ifdef VMS
4046 if (sys_suspend () == -1)
4047 {
4048 printf ("Not running as a subprocess;\n");
4049 printf ("you can continue or abort.\n");
4050 }
4051#else /* not VMS */
4052 /* Perhaps should really fork an inferior shell?
4053 But that would not provide any way to get back
4054 to the original shell, ever. */
4055 printf ("No support for stopping a process on this operating system;\n");
4056 printf ("you can continue or abort.\n");
4057#endif /* not VMS */
4058#endif /* not SIGTSTP */
4059 printf ("Auto-save? (y or n) ");
4060 fflush (stdout);
4061 if (((c = getchar ()) & ~040) == 'Y')
4062 Fdo_auto_save (Qnil, Qnil);
4063 while (c != '\n') c = getchar ();
4064#ifdef VMS
4065 printf ("Abort (and enter debugger)? (y or n) ");
4066#else /* not VMS */
4067 printf ("Abort (and dump core)? (y or n) ");
4068#endif /* not VMS */
4069 fflush (stdout);
4070 if (((c = getchar ()) & ~040) == 'Y')
4071 abort ();
4072 while (c != '\n') c = getchar ();
4073 printf ("Continuing...\n");
4074 fflush (stdout);
4075 init_sys_modes ();
4076 }
4077 else
4078 {
4079 /* If executing a function that wants to be interrupted out of
4080 and the user has not deferred quitting by binding `inhibit-quit'
4081 then quit right away. */
4082 if (immediate_quit && NILP (Vinhibit_quit))
4083 {
4084 immediate_quit = 0;
4085 sigfree ();
4086 Fsignal (Qquit, Qnil);
4087 }
4088 else
4089 /* Else request quit when it's safe */
4090 Vquit_flag = Qt;
4091 }
4092
4093 if (waiting_for_input && !echoing)
4094 quit_throw_to_read_char ();
4095
4096 errno = old_errno;
4097}
4098
4099/* Handle a C-g by making read_char return C-g. */
4100
4101quit_throw_to_read_char ()
4102{
4103 quit_error_check ();
4104 sigfree ();
4105 /* Prevent another signal from doing this before we finish. */
f76475ad 4106 clear_waiting_for_input ();
284f4730
JB
4107 input_pending = 0;
4108
dbc4e1c1 4109 unread_command_events = Qnil;
86e5706b 4110 unread_command_char = -1;
284f4730
JB
4111
4112 _longjmp (getcjmp, 1);
4113}
4114\f
4115DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
4116 "Set mode of reading keyboard input.\n\
464f8898
RS
4117First arg INTERRUPT non-nil means use input interrupts;\n\
4118 nil means use CBREAK mode.\n\
4119Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
284f4730 4120 (no effect except in CBREAK mode).\n\
464f8898 4121Third arg META non-nil means accept 8-bit input (for a Meta key).\n\
284f4730 4122 Otherwise, the top bit is ignored, on the assumption it is parity.\n\
464f8898 4123Optional fourth arg QUIT if non-nil specifies character to use for quitting.")
284f4730
JB
4124 (interrupt, flow, meta, quit)
4125 Lisp_Object interrupt, flow, meta, quit;
4126{
4127 if (!NILP (quit)
4128 && (XTYPE (quit) != Lisp_Int
4129 || XINT (quit) < 0 || XINT (quit) > 0400))
4130 error ("set-input-mode: QUIT must be an ASCII character.");
4131
4132 reset_sys_modes ();
4133#ifdef SIGIO
4134/* Note SIGIO has been undef'd if FIONREAD is missing. */
4135#ifdef NO_SOCK_SIGIO
4136 if (read_socket_hook)
4137 interrupt_input = 0; /* No interrupts if reading from a socket. */
4138 else
4139#endif /* NO_SOCK_SIGIO */
4140 interrupt_input = !NILP (interrupt);
4141#else /* not SIGIO */
4142 interrupt_input = 0;
4143#endif /* not SIGIO */
4144/* Our VMS input only works by interrupts, as of now. */
4145#ifdef VMS
4146 interrupt_input = 1;
4147#endif
4148 flow_control = !NILP (flow);
4149 meta_key = !NILP (meta);
4150 if (!NILP (quit))
4151 /* Don't let this value be out of range. */
4152 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
4153
4154 init_sys_modes ();
4155 return Qnil;
4156}
4157\f
4158init_keyboard ()
4159{
284f4730
JB
4160 /* This is correct before outermost invocation of the editor loop */
4161 command_loop_level = -1;
4162 immediate_quit = 0;
4163 quit_char = Ctl ('g');
dbc4e1c1 4164 unread_command_events = Qnil;
86e5706b 4165 unread_command_char = -1;
284f4730 4166 total_keys = 0;
9deb415a 4167 recent_keys_index = 0;
284f4730
JB
4168 kbd_fetch_ptr = kbd_buffer;
4169 kbd_store_ptr = kbd_buffer;
4170 do_mouse_tracking = 0;
4171 input_pending = 0;
4172
07d2b8de 4173#ifdef MULTI_FRAME
8f805655
JB
4174 /* This means that command_loop_1 won't try to select anything the first
4175 time through. */
3c370943
JB
4176 internal_last_event_frame = Qnil;
4177 Vlast_event_frame = internal_last_event_frame;
7b4aedb9
JB
4178#endif
4179
4180 /* If we're running a dumped Emacs, we need to clear out
4181 kbd_buffer_frame_or_window, in case some events got into it
4182 before we dumped.
4bb994d1 4183
7b4aedb9
JB
4184 If we're running an undumped Emacs, it hasn't been initialized by
4185 syms_of_keyboard yet. */
4bb994d1 4186 if (initialized)
7b4aedb9 4187 Ffillarray (kbd_buffer_frame_or_window, Qnil);
07d2b8de 4188
284f4730
JB
4189 if (!noninteractive)
4190 {
4191 signal (SIGINT, interrupt_signal);
cb5df6ae 4192#if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
284f4730
JB
4193 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
4194 SIGQUIT and we can't tell which one it will give us. */
4195 signal (SIGQUIT, interrupt_signal);
4196#endif /* HAVE_TERMIO */
4197/* Note SIGIO has been undef'd if FIONREAD is missing. */
4198#ifdef SIGIO
4199 signal (SIGIO, input_available_signal);
8ea0a720 4200#endif /* SIGIO */
284f4730
JB
4201 }
4202
4203/* Use interrupt input by default, if it works and noninterrupt input
4204 has deficiencies. */
4205
4206#ifdef INTERRUPT_INPUT
4207 interrupt_input = 1;
4208#else
4209 interrupt_input = 0;
4210#endif
4211
4212/* Our VMS input only works by interrupts, as of now. */
4213#ifdef VMS
4214 interrupt_input = 1;
4215#endif
4216
4217 sigfree ();
4218 dribble = 0;
4219
4220 if (keyboard_init_hook)
4221 (*keyboard_init_hook) ();
4222
4223#ifdef POLL_FOR_INPUT
4224 poll_suppress_count = 1;
4225 start_polling ();
4226#endif
4227}
4228
4229/* This type's only use is in syms_of_keyboard, to initialize the
4230 event header symbols and put properties on them. */
4231struct event_head {
4232 Lisp_Object *var;
4233 char *name;
4234 Lisp_Object *kind;
4235};
4236
4237struct event_head head_table[] = {
7b4aedb9 4238 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
3c370943 4239 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
7b4aedb9 4240 &Qswitch_frame, "switch-frame", &Qswitch_frame,
284f4730
JB
4241};
4242
4243syms_of_keyboard ()
4244{
4245 Qself_insert_command = intern ("self-insert-command");
4246 staticpro (&Qself_insert_command);
4247
4248 Qforward_char = intern ("forward-char");
4249 staticpro (&Qforward_char);
4250
4251 Qbackward_char = intern ("backward-char");
4252 staticpro (&Qbackward_char);
4253
4254 Qdisabled = intern ("disabled");
4255 staticpro (&Qdisabled);
4256
86e5706b
RS
4257 Qpre_command_hook = intern ("pre-command-hook");
4258 staticpro (&Qpre_command_hook);
4259
4260 Qpost_command_hook = intern ("post-command-hook");
4261 staticpro (&Qpost_command_hook);
4262
284f4730
JB
4263 Qfunction_key = intern ("function-key");
4264 staticpro (&Qfunction_key);
13b5e56c 4265 Qmouse_click = intern ("mouse-click");
284f4730 4266 staticpro (&Qmouse_click);
284f4730
JB
4267
4268 Qmode_line = intern ("mode-line");
4269 staticpro (&Qmode_line);
e5d77022
JB
4270 Qvertical_line = intern ("vertical-line");
4271 staticpro (&Qvertical_line);
3c370943
JB
4272 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
4273 staticpro (&Qvertical_scroll_bar);
4bb994d1
JB
4274
4275 Qabove_handle = intern ("above-handle");
4276 staticpro (&Qabove_handle);
4277 Qhandle = intern ("handle");
4278 staticpro (&Qhandle);
4279 Qbelow_handle = intern ("below-handle");
4280 staticpro (&Qbelow_handle);
284f4730 4281
cd21b839 4282 Qevent_kind = intern ("event-kind");
284f4730 4283 staticpro (&Qevent_kind);
88cb0656
JB
4284 Qevent_symbol_elements = intern ("event-symbol-elements");
4285 staticpro (&Qevent_symbol_elements);
0a7f1fc0
JB
4286 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
4287 staticpro (&Qevent_symbol_element_mask);
4288 Qmodifier_cache = intern ("modifier-cache");
4289 staticpro (&Qmodifier_cache);
284f4730
JB
4290
4291 {
4292 struct event_head *p;
4293
4294 for (p = head_table;
4295 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
4296 p++)
4297 {
4298 *p->var = intern (p->name);
4299 staticpro (p->var);
4300 Fput (*p->var, Qevent_kind, *p->kind);
88cb0656 4301 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
284f4730
JB
4302 }
4303 }
4304
7b4aedb9
JB
4305 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
4306 staticpro (&button_down_location);
88cb0656
JB
4307
4308 {
4309 int i;
4310 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
4311
4312 modifier_symbols = Fmake_vector (make_number (len), Qnil);
4313 for (i = 0; i < len; i++)
86e5706b
RS
4314 if (modifier_names[i])
4315 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
88cb0656
JB
4316 staticpro (&modifier_symbols);
4317 }
4318
9deb415a
JB
4319 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
4320 staticpro (&recent_keys);
4321
6569cc8d 4322 this_command_keys = Fmake_vector (make_number (40), Qnil);
715d9345 4323 staticpro (&this_command_keys);
6569cc8d 4324
7b4aedb9
JB
4325 kbd_buffer_frame_or_window
4326 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
4327 staticpro (&kbd_buffer_frame_or_window);
4bb994d1 4328
284f4730
JB
4329 func_key_syms = Qnil;
4330 staticpro (&func_key_syms);
4331
4332 mouse_syms = Qnil;
4333 staticpro (&mouse_syms);
4334
cd21b839
JB
4335 unread_switch_frame = Qnil;
4336 staticpro (&unread_switch_frame);
4337
284f4730
JB
4338 defsubr (&Sread_key_sequence);
4339 defsubr (&Srecursive_edit);
4340 defsubr (&Strack_mouse);
284f4730
JB
4341 defsubr (&Sinput_pending_p);
4342 defsubr (&Scommand_execute);
4343 defsubr (&Srecent_keys);
4344 defsubr (&Sthis_command_keys);
4345 defsubr (&Ssuspend_emacs);
4346 defsubr (&Sabort_recursive_edit);
4347 defsubr (&Sexit_recursive_edit);
4348 defsubr (&Srecursion_depth);
4349 defsubr (&Stop_level);
4350 defsubr (&Sdiscard_input);
4351 defsubr (&Sopen_dribble_file);
4352 defsubr (&Sset_input_mode);
4353 defsubr (&Sexecute_extended_command);
4354
4355 DEFVAR_LISP ("disabled-command-hook", &Vdisabled_command_hook,
4356 "Value is called instead of any command that is disabled\n\
7d6de002 4357\(has a non-nil `disabled' property).");
284f4730
JB
4358
4359 DEFVAR_LISP ("last-command-char", &last_command_char,
86e5706b
RS
4360 "Last input event that was part of a command.");
4361
4362 DEFVAR_LISP ("last-command-event", &last_command_char,
4363 "Last input event that was part of a command.");
284f4730 4364
7d6de002 4365 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
86e5706b 4366 "Last input event in a command, except for mouse menu events.\n\
7d6de002
RS
4367Mouse menus give back keys that don't look like mouse events;\n\
4368this variable holds the actual mouse event that led to the menu,\n\
4369so that you can determine whether the command was run by mouse or not.");
4370
284f4730 4371 DEFVAR_LISP ("last-input-char", &last_input_char,
86e5706b
RS
4372 "Last input event.");
4373
4374 DEFVAR_LISP ("last-input-event", &last_input_char,
4375 "Last input event.");
284f4730 4376
dbc4e1c1 4377 DEFVAR_LISP ("unread-command-events", &unread_command_events,
1c07d0a6 4378 "List of objects to be read as next command input events.");
284f4730 4379
86e5706b
RS
4380 DEFVAR_INT ("unread-command-char", &unread_command_char,
4381 "If not -1, an object to be read as next command input event.");
4382
284f4730
JB
4383 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
4384 "Meta-prefix character code. Meta-foo as command input\n\
4385turns into this character followed by foo.");
4386 XSET (meta_prefix_char, Lisp_Int, 033);
4387
4388 DEFVAR_LISP ("last-command", &last_command,
4389 "The last command executed. Normally a symbol with a function definition,\n\
4390but can be whatever was found in the keymap, or whatever the variable\n\
4391`this-command' was set to by that command.");
4392 last_command = Qnil;
4393
4394 DEFVAR_LISP ("this-command", &this_command,
4395 "The command now being executed.\n\
4396The command can set this variable; whatever is put here\n\
4397will be in `last-command' during the following command.");
4398 this_command = Qnil;
4399
4400 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
4401 "*Number of keyboard input characters between auto-saves.\n\
4402Zero means disable autosaving due to number of characters typed.");
4403 auto_save_interval = 300;
4404
4405 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
4406 "*Number of seconds idle time before auto-save.\n\
06ef7355
RS
4407Zero or nil means disable auto-saving due to idleness.\n\
4408After auto-saving due to this many seconds of idle time,\n\
84447c71 4409Emacs also does a garbage collection if that seems to be warranted.");
284f4730
JB
4410 XFASTINT (Vauto_save_timeout) = 30;
4411
4412 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
4413 "*Nonzero means echo unfinished commands after this many seconds of pause.");
4414 echo_keystrokes = 1;
4415
4416 DEFVAR_INT ("polling-period", &polling_period,
4417 "*Interval between polling for input during Lisp execution.\n\
4418The reason for polling is to make C-g work to stop a running program.\n\
4419Polling is needed only when using X windows and SIGIO does not work.\n\
4420Polling is automatically disabled in all other cases.");
4421 polling_period = 2;
4422
4423 DEFVAR_INT ("num-input-keys", &num_input_keys,
4424 "*Number of complete keys read from the keyboard so far.");
4425 num_input_keys = 0;
4426
07d2b8de 4427#ifdef MULTI_FRAME
ff11dfa1 4428 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
fce33686
JB
4429 "*The frame in which the most recently read event occurred.\n\
4430If the last event came from a keyboard macro, this is set to `macro'.");
ff11dfa1 4431 Vlast_event_frame = Qnil;
07d2b8de 4432#endif
284f4730
JB
4433
4434 DEFVAR_LISP ("help-char", &help_char,
4435 "Character to recognize as meaning Help.\n\
4436When it is read, do `(eval help-form)', and display result if it's a string.\n\
4437If the value of `help-form' is nil, this char can be read normally.");
4438 XSET (help_char, Lisp_Int, Ctl ('H'));
4439
4440 DEFVAR_LISP ("help-form", &Vhelp_form,
4441 "Form to execute when character help-char is read.\n\
4442If the form returns a string, that string is displayed.\n\
4443If `help-form' is nil, the help char is not recognized.");
4444 Vhelp_form = Qnil;
4445
4446 DEFVAR_LISP ("top-level", &Vtop_level,
4447 "Form to evaluate when Emacs starts up.\n\
4448Useful to set before you dump a modified Emacs.");
4449 Vtop_level = Qnil;
4450
4451 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
4452 "String used as translate table for keyboard input, or nil.\n\
4453Each character is looked up in this string and the contents used instead.\n\
4454If string is of length N, character codes N and up are untranslated.");
4455 Vkeyboard_translate_table = Qnil;
4456
284f4730 4457 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
7d6de002 4458 "Non-nil means prompt with menus when appropriate.\n\
284f4730 4459This is done when reading from a keymap that has a prompt string,\n\
7d6de002
RS
4460for elements that have prompt strings.\n\
4461The menu is displayed on the screen\n\
4462if X menus were enabled at configuration\n\
4463time and the previous event was a mouse click prefix key.\n\
4464Otherwise, menu prompting uses the echo area.");
284f4730
JB
4465 menu_prompting = 1;
4466
4467 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
4468 "Character to see next line of menu prompt.\n\
4469Type this character while in a menu prompt to rotate around the lines of it.");
4470 XSET (menu_prompt_more_char, Lisp_Int, ' ');
9fa4395d
RS
4471
4472 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
4473 "A mask of additional modifier keys to use with every keyboard character.\n\
4474These bits follow the convention for X windows,\n\
4475but the control and meta bits work even when you are not using X:\n\
4476 1 -- shift bit 2 -- lock bit\n\
4477 4 -- control bit 8 -- meta bit.");
4478 extra_keyboard_modifiers = 0;
86e5706b
RS
4479
4480 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
4481 "If an editing command sets this to t, deactivate the mark afterward.\n\
4482The command loop sets this to nil before each command,\n\
4483and tests the value when the command returns.\n\
4484Buffer modification stores t in this variable.");
4485 Vdeactivate_mark = Qnil;
4486
4487 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
4488 "Normal hook run before each command is executed.");
4489 Vpre_command_hook = Qnil;
4490
4491 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
4492 "Normal hook run before each command is executed.");
4493 Vpost_command_hook = Qnil;
284f4730
JB
4494}
4495
4496keys_of_keyboard ()
4497{
4498 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
4499 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
4500 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
4501 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
4502 initial_define_key (meta_map, 'x', "execute-extended-command");
4503}