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