(window_change_signal): Add ignored argument.
[bpt/emacs.git] / src / keyboard.c
CommitLineData
284f4730 1/* Keyboard and mouse input; editor command loop.
0d882d52 2 Copyright (C) 1985,86,87,88,89,93,94,95 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
18160b98 23#include <config.h>
284f4730
JB
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
80e4aa30
RS
43#ifdef MSDOS
44#include "msdos.h"
45#include <time.h>
46#else /* not MSDOS */
284f4730
JB
47#ifndef VMS
48#include <sys/ioctl.h>
284f4730 49#endif
80e4aa30 50#endif /* not MSDOS */
284f4730 51
52baf19e 52#include "syssignal.h"
6ef5b54f 53#include "systty.h"
52baf19e 54
c5e3b6c5
RS
55/* This is to get the definitions of the XK_ symbols. */
56#ifdef HAVE_X_WINDOWS
57#include "xterm.h"
58#endif
59
0c2611c5
RS
60/* Include systime.h after xterm.h to avoid double inclusion of time.h. */
61#include "systime.h"
62
52baf19e
JB
63extern int errno;
64
9ac0d9e0
JB
65/* Variables for blockinput.h: */
66
67/* Non-zero if interrupt input is blocked right now. */
63927c41 68int interrupt_input_blocked;
9ac0d9e0
JB
69
70/* Nonzero means an input interrupt has arrived
71 during the current critical section. */
63927c41 72int interrupt_input_pending;
9ac0d9e0
JB
73
74
437f6112
RS
75/* File descriptor to use for input. */
76extern int input_fd;
284f4730 77
bcce3e02 78#ifdef HAVE_X_WINDOWS
284f4730
JB
79/* Make all keyboard buffers much bigger when using X windows. */
80#define KBD_BUFFER_SIZE 4096
81#else /* No X-windows, character input */
82#define KBD_BUFFER_SIZE 256
83#endif /* No X-windows */
84
85/* Following definition copied from eval.c */
86
87struct backtrace
88 {
89 struct backtrace *next;
90 Lisp_Object *function;
91 Lisp_Object *args; /* Points to vector of args. */
92 int nargs; /* length of vector. If nargs is UNEVALLED,
93 args points to slot holding list of
94 unevalled args */
95 char evalargs;
96 };
97
6c6083a9
KH
98#ifdef MULTI_PERDISPLAY
99PERDISPLAY *current_perdisplay;
a2b20856 100PERDISPLAY *all_perdisplays;
6c6083a9 101#else
ba72822c 102PERDISPLAY the_only_perdisplay;
6c6083a9 103#endif
612b78ef 104
284f4730
JB
105/* Non-nil disable property on a command means
106 do not execute it; call disabled-command-hook's value instead. */
2e894dab 107Lisp_Object Qdisabled, Qdisabled_command_hook;
284f4730
JB
108
109#define NUM_RECENT_KEYS (100)
110int recent_keys_index; /* Index for storing next element into recent_keys */
111int total_keys; /* Total number of elements stored into recent_keys */
5160df46 112Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
284f4730 113
6569cc8d
JB
114/* Vector holding the key sequence that invoked the current command.
115 It is reused for each command, and it may be longer than the current
116 sequence; this_command_key_count indicates how many elements
117 actually mean something.
118 It's easier to staticpro a single Lisp_Object than an array. */
119Lisp_Object this_command_keys;
120int this_command_key_count;
284f4730
JB
121
122extern int minbuf_level;
123
124extern struct backtrace *backtrace_list;
125
126/* Nonzero means do menu prompting. */
127static int menu_prompting;
128
129/* Character to see next line of menu prompt. */
130static Lisp_Object menu_prompt_more_char;
131
132/* For longjmp to where kbd input is being done. */
133static jmp_buf getcjmp;
134
135/* True while doing kbd input. */
136int waiting_for_input;
137
138/* True while displaying for echoing. Delays C-g throwing. */
139static int echoing;
140
03361bcc
RS
141/* Nonzero means disregard local maps for the menu bar. */
142static int inhibit_local_menu_bar_menus;
143
80e4aa30 144/* Nonzero means C-g should cause immediate error-signal. */
284f4730
JB
145int immediate_quit;
146
147/* Character to recognize as the help char. */
7e85b935 148Lisp_Object Vhelp_char;
284f4730
JB
149
150/* Form to execute when help char is typed. */
151Lisp_Object Vhelp_form;
152
7e85b935
RS
153/* Command to run when the help character follows a prefix key. */
154Lisp_Object Vprefix_help_command;
155
9f9c0e27
RS
156/* List of items that should move to the end of the menu bar. */
157Lisp_Object Vmenu_bar_final_items;
a73c5e29 158
284f4730
JB
159/* Character that causes a quit. Normally C-g.
160
161 If we are running on an ordinary terminal, this must be an ordinary
162 ASCII char, since we want to make it our interrupt character.
163
164 If we are not running on an ordinary terminal, it still needs to be
165 an ordinary ASCII char. This character needs to be recognized in
166 the input interrupt handler. At this point, the keystroke is
167 represented as a struct input_event, while the desired quit
168 character is specified as a lispy event. The mapping from struct
169 input_events to lispy events cannot run in an interrupt handler,
170 and the reverse mapping is difficult for anything but ASCII
171 keystrokes.
172
173 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
174 ASCII character. */
175int quit_char;
176
177extern Lisp_Object current_global_map;
178extern int minibuf_level;
179
9dd3131c
RS
180/* If non-nil, this is a map that overrides all other local maps. */
181Lisp_Object Voverriding_local_map;
182
d0a49716
RS
183/* If non-nil, Voverriding_local_map applies to the menu bar. */
184Lisp_Object Voverriding_local_map_menu_flag;
185
284f4730
JB
186/* Current depth in recursive edits. */
187int command_loop_level;
188
189/* Total number of times command_loop has read a key sequence. */
190int num_input_keys;
191
192/* Last input character read as a command. */
193Lisp_Object last_command_char;
194
7d6de002
RS
195/* Last input character read as a command, not counting menus
196 reached by the mouse. */
197Lisp_Object last_nonmenu_event;
198
284f4730
JB
199/* Last input character read for any purpose. */
200Lisp_Object last_input_char;
201
dbc4e1c1 202/* If not Qnil, a list of objects to be read as subsequent command input. */
24597608 203Lisp_Object Vunread_command_events;
284f4730 204
86e5706b
RS
205/* If not -1, an event to be read as subsequent command input. */
206int unread_command_char;
207
cd21b839
JB
208/* If not Qnil, this is a switch-frame event which we decided to put
209 off until the end of a key sequence. This should be read as the
dbc4e1c1 210 next command input, after any unread_command_events.
8f805655
JB
211
212 read_key_sequence uses this to delay switch-frame events until the
213 end of the key sequence; Fread_char uses it to put off switch-frame
214 events until a non-ASCII event is acceptable as input. */
215Lisp_Object unread_switch_frame;
cd21b839 216
9fa4395d
RS
217/* A mask of extra modifier bits to put into every keyboard char. */
218int extra_keyboard_modifiers;
219
284f4730
JB
220/* Char to use as prefix when a meta character is typed in.
221 This is bound on entry to minibuffer in case ESC is changed there. */
222
223Lisp_Object meta_prefix_char;
224
225/* Last size recorded for a current buffer which is not a minibuffer. */
226static int last_non_minibuf_size;
227
06ef7355 228/* Number of idle seconds before an auto-save and garbage collection. */
284f4730
JB
229static Lisp_Object Vauto_save_timeout;
230
231/* Total number of times read_char has returned. */
232int num_input_chars;
233
51172b6d
RS
234/* Total number of times read_char has returned, outside of macros. */
235int num_nonmacro_input_chars;
236
284f4730
JB
237/* Auto-save automatically when this many characters have been typed
238 since the last time. */
239
240static int auto_save_interval;
241
51172b6d 242/* Value of num_nonmacro_input_chars as of last auto save. */
284f4730
JB
243
244int last_auto_save;
245
246/* Last command executed by the editor command loop, not counting
247 commands that set the prefix argument. */
248
249Lisp_Object last_command;
250
251/* The command being executed by the command loop.
252 Commands may set this, and the value set will be copied into last_command
253 instead of the actual command. */
254Lisp_Object this_command;
255
b453f72e
KH
256/* The value of point when the last command was executed. */
257int last_point_position;
258
047688cb
RS
259/* The buffer that was current when the last command was started. */
260Lisp_Object last_point_position_buffer;
261
1113d9db
JB
262/* The timestamp of the last input event we received from the X server.
263 X Windows wants this for selection ownership. */
284f4730
JB
264unsigned long last_event_timestamp;
265
266Lisp_Object Qself_insert_command;
267Lisp_Object Qforward_char;
268Lisp_Object Qbackward_char;
e58aa385 269Lisp_Object Qundefined;
284f4730
JB
270
271/* read_key_sequence stores here the command definition of the
272 key sequence that it reads. */
273Lisp_Object read_key_sequence_cmd;
274
275/* Form to evaluate (if non-nil) when Emacs is started. */
276Lisp_Object Vtop_level;
277
278/* User-supplied string to translate input characters through. */
279Lisp_Object Vkeyboard_translate_table;
280
281/* Keymap mapping ASCII function key sequences onto their preferred forms. */
282extern Lisp_Object Vfunction_key_map;
283
a612e298
RS
284/* Keymap mapping ASCII function key sequences onto their preferred forms. */
285Lisp_Object Vkey_translation_map;
286
86e5706b
RS
287/* Non-nil means deactivate the mark at end of this command. */
288Lisp_Object Vdeactivate_mark;
289
48e416d4
RS
290/* Menu bar specified in Lucid Emacs fashion. */
291
292Lisp_Object Vlucid_menu_bar_dirty_flag;
293Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
294
86e5706b
RS
295/* Hooks to run before and after each command. */
296Lisp_Object Qpre_command_hook, Qpost_command_hook;
297Lisp_Object Vpre_command_hook, Vpost_command_hook;
40932d1a 298Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
86e5706b 299
8a792f3a
RS
300/* List of deferred actions to be performed at a later time.
301 The precise format isn't relevant here; we just check whether it is nil. */
302Lisp_Object Vdeferred_action_list;
303
304/* Function to call to handle deferred actions, when there are any. */
305Lisp_Object Vdeferred_action_function;
3ef14e46 306Lisp_Object Qdeferred_action_function;
8a792f3a 307
284f4730
JB
308/* File in which we write all commands we read. */
309FILE *dribble;
310
311/* Nonzero if input is available. */
312int input_pending;
313
b04904fb
RS
314/* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
315 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
316
284f4730
JB
317int meta_key;
318
319extern char *pending_malloc_warning;
320
2eb6bfbe 321#ifdef HAVE_MOUSE
a9d77f1f
RS
322/* If this flag is a frame, we check mouse_moved to see when the
323 mouse moves, and motion events will appear in the input stream.
324 Otherwise, mouse motion is ignored. */
325static Lisp_Object do_mouse_tracking;
284f4730
JB
326
327/* The window system handling code should set this if the mouse has
328 moved since the last call to the mouse_position_hook. Calling that
329 hook should clear this. Code assumes that if this is set, it can
330 call mouse_position_hook to get the promised position, so don't set
331 it unless you're prepared to substantiate the claim! */
332int mouse_moved;
2eb6bfbe 333#endif /* HAVE_MOUSE. */
284f4730
JB
334
335/* Symbols to head events. */
336Lisp_Object Qmouse_movement;
3c370943 337Lisp_Object Qscroll_bar_movement;
cd21b839 338Lisp_Object Qswitch_frame;
bbdc2092 339Lisp_Object Qdelete_frame;
af17bd2b
KH
340Lisp_Object Qiconify_frame;
341Lisp_Object Qmake_frame_visible;
cd21b839 342
284f4730
JB
343/* Symbols to denote kinds of events. */
344Lisp_Object Qfunction_key;
345Lisp_Object Qmouse_click;
346/* Lisp_Object Qmouse_movement; - also an event header */
284f4730
JB
347
348/* Properties of event headers. */
349Lisp_Object Qevent_kind;
88cb0656 350Lisp_Object Qevent_symbol_elements;
284f4730 351
598a9fa7
JB
352Lisp_Object Qmenu_enable;
353
0a7f1fc0
JB
354/* An event header symbol HEAD may have a property named
355 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
356 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
357 mask of modifiers applied to it. If present, this is used to help
358 speed up parse_modifiers. */
359Lisp_Object Qevent_symbol_element_mask;
360
361/* An unmodified event header BASE may have a property named
362 Qmodifier_cache, which is an alist mapping modifier masks onto
363 modified versions of BASE. If present, this helps speed up
364 apply_modifiers. */
365Lisp_Object Qmodifier_cache;
366
5ec75a55 367/* Symbols to use for parts of windows. */
284f4730 368Lisp_Object Qmode_line;
e5d77022 369Lisp_Object Qvertical_line;
3c370943 370Lisp_Object Qvertical_scroll_bar;
5ec75a55
RS
371Lisp_Object Qmenu_bar;
372
373extern Lisp_Object Qmenu_enable;
284f4730 374
f4255cd1
JB
375Lisp_Object recursive_edit_unwind (), command_loop ();
376Lisp_Object Fthis_command_keys ();
03b4122a 377Lisp_Object Qextended_command_history;
284f4730 378
f4eef8b4
RS
379Lisp_Object Qpolling_period;
380
ffd56f97
JB
381/* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
382 happens. */
383EMACS_TIME *input_available_clear_time;
284f4730
JB
384
385/* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
386 Default is 1 if INTERRUPT_INPUT is defined. */
387int interrupt_input;
388
389/* Nonzero while interrupts are temporarily deferred during redisplay. */
390int interrupts_deferred;
391
392/* nonzero means use ^S/^Q for flow control. */
393int flow_control;
394
284f4730
JB
395/* Allow m- file to inhibit use of FIONREAD. */
396#ifdef BROKEN_FIONREAD
397#undef FIONREAD
398#endif
399
400/* We are unable to use interrupts if FIONREAD is not available,
401 so flush SIGIO so we won't try. */
402#ifndef FIONREAD
403#ifdef SIGIO
404#undef SIGIO
405#endif
406#endif
407
34f04431
RS
408/* If we support X Windows, turn on the code to poll periodically
409 to detect C-g. It isn't actually used when doing interrupt input. */
284f4730 410#ifdef HAVE_X_WINDOWS
284f4730
JB
411#define POLL_FOR_INPUT
412#endif
284f4730
JB
413\f
414/* Global variable declarations. */
415
416/* Function for init_keyboard to call with no args (if nonzero). */
417void (*keyboard_init_hook) ();
418
419static int read_avail_input ();
420static void get_input_pending ();
9fd7d808 421static int readable_events ();
8150596a
RS
422static Lisp_Object read_char_x_menu_prompt ();
423static Lisp_Object read_char_minibuf_menu_prompt ();
a612e298
RS
424static Lisp_Object make_lispy_event ();
425static Lisp_Object make_lispy_movement ();
426static Lisp_Object modify_event_symbol ();
427static Lisp_Object make_lispy_switch_frame ();
284f4730
JB
428
429/* > 0 if we are to echo keystrokes. */
430static int echo_keystrokes;
431
8026024c
KH
432/* Nonzero means don't try to suspend even if the operating system seems
433 to support it. */
434static int cannot_suspend;
435
284f4730
JB
436#define min(a,b) ((a)<(b)?(a):(b))
437#define max(a,b) ((a)>(b)?(a):(b))
438
439/* Install the string STR as the beginning of the string of echoing,
440 so that it serves as a prompt for the next character.
441 Also start echoing. */
442
443echo_prompt (str)
444 char *str;
445{
446 int len = strlen (str);
7a80a6f6 447
ba72822c
KH
448 if (len > ECHOBUFSIZE - 4)
449 len = ECHOBUFSIZE - 4;
6c6083a9
KH
450 bcopy (str, current_perdisplay->echobuf, len);
451 current_perdisplay->echoptr = current_perdisplay->echobuf + len;
452 *current_perdisplay->echoptr = '\0';
284f4730 453
6c6083a9 454 current_perdisplay->echo_after_prompt = len;
7a80a6f6 455
284f4730
JB
456 echo ();
457}
458
459/* Add C to the echo string, if echoing is going on.
460 C can be a character, which is printed prettily ("M-C-x" and all that
461 jazz), or a symbol, whose name is printed. */
462
463echo_char (c)
464 Lisp_Object c;
465{
466 extern char *push_key_description ();
467
6c6083a9 468 if (current_perdisplay->immediate_echo)
284f4730 469 {
6c6083a9 470 char *ptr = current_perdisplay->echoptr;
284f4730 471
6c6083a9 472 if (ptr != current_perdisplay->echobuf)
284f4730
JB
473 *ptr++ = ' ';
474
475 /* If someone has passed us a composite event, use its head symbol. */
88cb0656 476 c = EVENT_HEAD (c);
284f4730 477
8c18cbfb 478 if (INTEGERP (c))
284f4730 479 {
6c6083a9 480 if (ptr - current_perdisplay->echobuf > ECHOBUFSIZE - 6)
284f4730
JB
481 return;
482
cb5df6ae 483 ptr = push_key_description (XINT (c), ptr);
284f4730 484 }
8c18cbfb 485 else if (SYMBOLP (c))
284f4730
JB
486 {
487 struct Lisp_String *name = XSYMBOL (c)->name;
6c6083a9
KH
488 if (((ptr - current_perdisplay->echobuf) + name->size + 4)
489 > ECHOBUFSIZE)
284f4730
JB
490 return;
491 bcopy (name->data, ptr, name->size);
492 ptr += name->size;
493 }
494
6c6083a9
KH
495 if (current_perdisplay->echoptr == current_perdisplay->echobuf
496 && EQ (c, Vhelp_char))
284f4730
JB
497 {
498 strcpy (ptr, " (Type ? for further options)");
499 ptr += strlen (ptr);
500 }
501
502 *ptr = 0;
6c6083a9 503 current_perdisplay->echoptr = ptr;
284f4730
JB
504
505 echo ();
506 }
507}
508
509/* Temporarily add a dash to the end of the echo string if it's not
510 empty, so that it serves as a mini-prompt for the very next character. */
511
512echo_dash ()
513{
6c6083a9
KH
514 if (!current_perdisplay->immediate_echo
515 && current_perdisplay->echoptr == current_perdisplay->echobuf)
284f4730 516 return;
7a80a6f6 517 /* Do nothing if we just printed a prompt. */
6c6083a9
KH
518 if (current_perdisplay->echo_after_prompt
519 == current_perdisplay->echoptr - current_perdisplay->echobuf)
7a80a6f6 520 return;
4bafa972 521 /* Do nothing if not echoing at all. */
6c6083a9 522 if (current_perdisplay->echoptr == 0)
4bafa972 523 return;
284f4730
JB
524
525 /* Put a dash at the end of the buffer temporarily,
526 but make it go away when the next character is added. */
6c6083a9
KH
527 current_perdisplay->echoptr[0] = '-';
528 current_perdisplay->echoptr[1] = 0;
284f4730
JB
529
530 echo ();
531}
532
533/* Display the current echo string, and begin echoing if not already
534 doing so. */
535
536echo ()
537{
6c6083a9 538 if (!current_perdisplay->immediate_echo)
284f4730
JB
539 {
540 int i;
6c6083a9 541 current_perdisplay->immediate_echo = 1;
284f4730
JB
542
543 for (i = 0; i < this_command_key_count; i++)
d0a57728
RS
544 {
545 Lisp_Object c;
546 c = XVECTOR (this_command_keys)->contents[i];
547 if (! (EVENT_HAS_PARAMETERS (c)
548 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
549 echo_char (c);
550 }
284f4730
JB
551 echo_dash ();
552 }
553
554 echoing = 1;
6c6083a9 555 message1_nolog (current_perdisplay->echobuf);
284f4730
JB
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{
6c6083a9
KH
566 current_perdisplay->immediate_echo = 0;
567 current_perdisplay->echoptr = current_perdisplay->echobuf;
568 current_perdisplay->echo_after_prompt = -1;
284f4730
JB
569}
570
571/* Return the length of the current echo string. */
572
573static int
574echo_length ()
575{
6c6083a9 576 return current_perdisplay->echoptr - current_perdisplay->echobuf;
284f4730
JB
577}
578
579/* Truncate the current echo message to its first LEN chars.
580 This and echo_char get used by read_key_sequence when the user
ff11dfa1 581 switches frames while entering a key sequence. */
284f4730
JB
582
583static void
584echo_truncate (len)
585 int len;
586{
6c6083a9
KH
587 current_perdisplay->echobuf[len] = '\0';
588 current_perdisplay->echoptr = current_perdisplay->echobuf + len;
40932d1a 589 truncate_echo_area (len);
284f4730
JB
590}
591
592\f
593/* Functions for manipulating this_command_keys. */
594static void
595add_command_key (key)
596 Lisp_Object key;
597{
6569cc8d
JB
598 int size = XVECTOR (this_command_keys)->size;
599
600 if (this_command_key_count >= size)
284f4730 601 {
9b8eb840 602 Lisp_Object new_keys;
6569cc8d 603
9b8eb840 604 new_keys = Fmake_vector (make_number (size * 2), Qnil);
6569cc8d
JB
605 bcopy (XVECTOR (this_command_keys)->contents,
606 XVECTOR (new_keys)->contents,
8f805655 607 size * sizeof (Lisp_Object));
6569cc8d
JB
608
609 this_command_keys = new_keys;
284f4730 610 }
6569cc8d
JB
611
612 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
284f4730
JB
613}
614\f
615Lisp_Object
616recursive_edit_1 ()
617{
618 int count = specpdl_ptr - specpdl;
619 Lisp_Object val;
620
621 if (command_loop_level > 0)
622 {
623 specbind (Qstandard_output, Qt);
624 specbind (Qstandard_input, Qt);
625 }
626
627 val = command_loop ();
628 if (EQ (val, Qt))
629 Fsignal (Qquit, Qnil);
630
cb5df6ae 631 return unbind_to (count, Qnil);
284f4730
JB
632}
633
634/* When an auto-save happens, record the "time", and don't do again soon. */
5846638c 635
284f4730
JB
636record_auto_save ()
637{
51172b6d 638 last_auto_save = num_nonmacro_input_chars;
284f4730 639}
5846638c
RS
640
641/* Make an auto save happen as soon as possible at command level. */
642
643force_auto_save_soon ()
644{
645 last_auto_save = - auto_save_interval - 1;
241ceaf7
RS
646
647 record_asynch_buffer_change ();
5846638c 648}
284f4730 649\f
284f4730
JB
650DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
651 "Invoke the editor command loop recursively.\n\
652To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
653that tells this function to return.\n\
654Alternately, `(throw 'exit t)' makes this function signal an error.\n\
655This function is called by the editor initialization to begin editing.")
656 ()
657{
658 int count = specpdl_ptr - specpdl;
659 Lisp_Object val;
660
661 command_loop_level++;
662 update_mode_lines = 1;
663
664 record_unwind_protect (recursive_edit_unwind,
665 (command_loop_level
666 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
667 ? Fcurrent_buffer ()
668 : Qnil);
669 recursive_edit_1 ();
670 return unbind_to (count, Qnil);
671}
672
673Lisp_Object
674recursive_edit_unwind (buffer)
675 Lisp_Object buffer;
676{
677 if (!NILP (buffer))
678 Fset_buffer (buffer);
679
680 command_loop_level--;
681 update_mode_lines = 1;
682 return Qnil;
683}
684\f
685Lisp_Object
686cmd_error (data)
687 Lisp_Object data;
a1341f75
RS
688{
689 Vstandard_output = Qt;
690 Vstandard_input = Qt;
691 Vexecuting_macro = Qnil;
ff4b06d3
KH
692 if (!current_perdisplay)
693 abort ();
694 current_perdisplay->Vprefix_arg = Qnil;
143e20ce 695 cancel_echoing ();
a1341f75
RS
696 cmd_error_internal (data, 0);
697
698 Vquit_flag = Qnil;
699
700 Vinhibit_quit = Qnil;
ff4b06d3
KH
701#ifdef MULTI_PERDISPLAY
702 current_perdisplay = 0;
703#endif
a1341f75
RS
704
705 return make_number (0);
706}
707
708cmd_error_internal (data, context)
709 Lisp_Object data;
710 char *context;
284f4730
JB
711{
712 Lisp_Object errmsg, tail, errname, file_error;
713 Lisp_Object stream;
714 struct gcpro gcpro1;
715 int i;
716
717 Vquit_flag = Qnil;
718 Vinhibit_quit = Qt;
284f4730
JB
719 echo_area_glyphs = 0;
720
ff11dfa1 721 /* If the window system or terminal frame hasn't been initialized
284f4730
JB
722 yet, or we're not interactive, it's best to dump this message out
723 to stderr and exit. */
ff11dfa1 724 if (! FRAME_MESSAGE_BUF (selected_frame)
284f4730
JB
725 || noninteractive)
726 stream = Qexternal_debugging_output;
727 else
728 {
729 Fdiscard_input ();
730 bitch_at_user ();
731 stream = Qt;
732 }
733
a1341f75
RS
734 if (context != 0)
735 write_string_1 (context, -1, stream);
736
284f4730
JB
737 errname = Fcar (data);
738
739 if (EQ (errname, Qerror))
740 {
741 data = Fcdr (data);
742 if (!CONSP (data)) data = Qnil;
743 errmsg = Fcar (data);
744 file_error = Qnil;
745 }
746 else
747 {
748 errmsg = Fget (errname, Qerror_message);
749 file_error = Fmemq (Qfile_error,
750 Fget (errname, Qerror_conditions));
751 }
752
753 /* Print an error message including the data items.
754 This is done by printing it into a scratch buffer
755 and then making a copy of the text in the buffer. */
756
757 if (!CONSP (data)) data = Qnil;
758 tail = Fcdr (data);
759 GCPRO1 (tail);
760
761 /* For file-error, make error message by concatenating
762 all the data items. They are all strings. */
763 if (!NILP (file_error) && !NILP (tail))
764 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
765
8c18cbfb 766 if (STRINGP (errmsg))
284f4730
JB
767 Fprinc (errmsg, stream);
768 else
769 write_string_1 ("peculiar error", -1, stream);
770
771 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
772 {
773 write_string_1 (i ? ", " : ": ", 2, stream);
774 if (!NILP (file_error))
775 Fprinc (Fcar (tail), stream);
776 else
777 Fprin1 (Fcar (tail), stream);
778 }
779 UNGCPRO;
780
ff11dfa1 781 /* If the window system or terminal frame hasn't been initialized
284f4730 782 yet, or we're in -batch mode, this error should cause Emacs to exit. */
ff11dfa1 783 if (! FRAME_MESSAGE_BUF (selected_frame)
284f4730
JB
784 || noninteractive)
785 {
786 Fterpri (stream);
787 Fkill_emacs (make_number (-1));
788 }
284f4730
JB
789}
790\f
791Lisp_Object command_loop_1 ();
792Lisp_Object command_loop_2 ();
793Lisp_Object top_level_1 ();
794
795/* Entry to editor-command-loop.
796 This level has the catches for exiting/returning to editor command loop.
797 It returns nil to exit recursive edit, t to abort it. */
798
799Lisp_Object
800command_loop ()
801{
802 if (command_loop_level > 0 || minibuf_level > 0)
803 {
804 return internal_catch (Qexit, command_loop_2, Qnil);
805 }
806 else
807 while (1)
808 {
809 internal_catch (Qtop_level, top_level_1, Qnil);
810 internal_catch (Qtop_level, command_loop_2, Qnil);
811
812 /* End of file in -batch run causes exit here. */
813 if (noninteractive)
814 Fkill_emacs (Qt);
815 }
816}
817
818/* Here we catch errors in execution of commands within the
819 editing loop, and reenter the editing loop.
820 When there is an error, cmd_error runs and returns a non-nil
821 value to us. A value of nil means that cmd_loop_1 itself
822 returned due to end of file (or end of kbd macro). */
823
824Lisp_Object
825command_loop_2 ()
826{
827 register Lisp_Object val;
828
829 do
830 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
831 while (!NILP (val));
832
833 return Qnil;
834}
835
836Lisp_Object
837top_level_2 ()
838{
839 return Feval (Vtop_level);
840}
841
842Lisp_Object
843top_level_1 ()
844{
845 /* On entry to the outer level, run the startup file */
846 if (!NILP (Vtop_level))
847 internal_condition_case (top_level_2, Qerror, cmd_error);
848 else if (!NILP (Vpurify_flag))
849 message ("Bare impure Emacs (standard Lisp code not loaded)");
850 else
851 message ("Bare Emacs (standard Lisp code not loaded)");
852 return Qnil;
853}
854
855DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
856 "Exit all recursive editing levels.")
857 ()
858{
859 Fthrow (Qtop_level, Qnil);
860}
861
862DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
863 "Exit from the innermost recursive edit or minibuffer.")
864 ()
865{
866 if (command_loop_level > 0 || minibuf_level > 0)
867 Fthrow (Qexit, Qnil);
868
869 error ("No recursive edit is in progress");
870}
871
872DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
873 "Abort the command that requested this recursive edit or minibuffer input.")
874 ()
875{
876 if (command_loop_level > 0 || minibuf_level > 0)
877 Fthrow (Qexit, Qt);
878
879 error ("No recursive edit is in progress");
880}
881\f
882/* This is the actual command reading loop,
883 sans error-handling encapsulation. */
884
885Lisp_Object Fcommand_execute ();
886static int read_key_sequence ();
1c9784c9 887static void safe_run_hooks ();
284f4730
JB
888
889Lisp_Object
890command_loop_1 ()
891{
48e416d4 892 Lisp_Object cmd, tem;
284f4730
JB
893 int lose;
894 int nonundocount;
895 Lisp_Object keybuf[30];
896 int i;
897 int no_redisplay;
898 int no_direct;
86e5706b
RS
899 int prev_modiff;
900 struct buffer *prev_buffer;
ff4b06d3 901 PERDISPLAY *global_perdisplay = current_perdisplay;
284f4730 902
86e5706b 903 Vdeactivate_mark = Qnil;
284f4730 904 waiting_for_input = 0;
267de7a1
KH
905 if (current_perdisplay)
906 cancel_echoing ();
284f4730 907
284f4730
JB
908 nonundocount = 0;
909 no_redisplay = 0;
910 this_command_key_count = 0;
911
a612e298
RS
912 /* Make sure this hook runs after commands that get errors and
913 throw to top level. */
a98ea3f9
RS
914 /* Note that the value cell will never directly contain nil
915 if the symbol is a local variable. */
916 if (!NILP (XSYMBOL (Qpost_command_hook)->value) && !NILP (Vrun_hooks))
917 safe_run_hooks (Qpost_command_hook);
a612e298 918
8a792f3a
RS
919 if (!NILP (Vdeferred_action_list))
920 call0 (Vdeferred_action_function);
921
51d5a2c9
RS
922 /* Do this after running Vpost_command_hook, for consistency. */
923 last_command = this_command;
924
284f4730
JB
925 while (1)
926 {
284f4730
JB
927 /* Make sure the current window's buffer is selected. */
928 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
929 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
930
931 /* Display any malloc warning that just came out. Use while because
932 displaying one warning can cause another. */
933
934 while (pending_malloc_warning)
935 display_malloc_warning ();
936
937 no_direct = 0;
938
86e5706b
RS
939 Vdeactivate_mark = Qnil;
940
284f4730 941 /* If minibuffer on and echo area in use,
eb8c3be9 942 wait 2 sec and redraw minibuffer. */
284f4730
JB
943
944 if (minibuf_level && echo_area_glyphs)
945 {
f1bed6d8
RS
946 /* Bind inhibit-quit to t so that C-g gets read in
947 rather than quitting back to the minibuffer. */
948 int count = specpdl_ptr - specpdl;
949 specbind (Qinhibit_quit, Qt);
284f4730 950 Fsit_for (make_number (2), Qnil, Qnil);
cb5df6ae 951 unbind_to (count, Qnil);
f1bed6d8 952
284f4730
JB
953 echo_area_glyphs = 0;
954 no_direct = 1;
955 if (!NILP (Vquit_flag))
956 {
957 Vquit_flag = Qnil;
24597608 958 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
284f4730
JB
959 }
960 }
961
962#ifdef C_ALLOCA
ff4b06d3 963 alloca (0); /* Cause a garbage collection now */
284f4730
JB
964 /* Since we can free the most stuff here. */
965#endif /* C_ALLOCA */
966
8f805655 967#if 0
ff11dfa1 968#ifdef MULTI_FRAME
8f805655
JB
969 /* Select the frame that the last event came from. Usually,
970 switch-frame events will take care of this, but if some lisp
971 code swallows a switch-frame event, we'll fix things up here.
972 Is this a good idea? */
8c18cbfb 973 if (FRAMEP (internal_last_event_frame)
3c370943
JB
974 && XFRAME (internal_last_event_frame) != selected_frame)
975 Fselect_frame (internal_last_event_frame, Qnil);
cd21b839 976#endif
284f4730 977#endif
48e416d4
RS
978 /* If it has changed current-menubar from previous value,
979 really recompute the menubar from the value. */
a646e520
RS
980 if (! NILP (Vlucid_menu_bar_dirty_flag)
981 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
48e416d4
RS
982 call0 (Qrecompute_lucid_menubar);
983
8f805655 984 /* Read next key sequence; i gets its length. */
ff4b06d3 985 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0], Qnil, 0);
8f805655
JB
986
987 ++num_input_keys;
988
284f4730
JB
989 /* Now we have read a key sequence of length I,
990 or else I is 0 and we found end of file. */
991
992 if (i == 0) /* End of file -- happens only in */
993 return Qnil; /* a kbd macro, at the end. */
dcc408a0
RS
994 /* -1 means read_key_sequence got a menu that was rejected.
995 Just loop around and read another command. */
996 if (i == -1)
997 {
998 cancel_echoing ();
999 this_command_key_count = 0;
ff4b06d3 1000 goto finalize;
dcc408a0 1001 }
284f4730 1002
284f4730
JB
1003 last_command_char = keybuf[i - 1];
1004
75c0b143
RS
1005 /* If the previous command tried to force a specific window-start,
1006 forget about that, in case this command moves point far away
1007 from that position. */
1008 XWINDOW (selected_window)->force_start = Qnil;
1009
284f4730
JB
1010 cmd = read_key_sequence_cmd;
1011 if (!NILP (Vexecuting_macro))
1012 {
1013 if (!NILP (Vquit_flag))
1014 {
1015 Vexecuting_macro = Qt;
1016 QUIT; /* Make some noise. */
1017 /* Will return since macro now empty. */
1018 }
1019 }
1020
1021 /* Do redisplay processing after this command except in special
40932d1a
RS
1022 cases identified below that set no_redisplay to 1.
1023 (actually, there's currently no way to prevent the redisplay,
1024 and no_redisplay is ignored.
1025 Perhaps someday we will really implement it. */
284f4730
JB
1026 no_redisplay = 0;
1027
86e5706b
RS
1028 prev_buffer = current_buffer;
1029 prev_modiff = MODIFF;
8746da95 1030 last_point_position = PT;
18cd2eeb 1031 XSETBUFFER (last_point_position_buffer, prev_buffer);
86e5706b 1032
284f4730
JB
1033 /* Execute the command. */
1034
86e5706b 1035 this_command = cmd;
a98ea3f9
RS
1036 /* Note that the value cell will never directly contain nil
1037 if the symbol is a local variable. */
1038 if (!NILP (XSYMBOL (Qpre_command_hook)->value) && !NILP (Vrun_hooks))
1039 safe_run_hooks (Qpre_command_hook);
86e5706b 1040
258bf746 1041 if (NILP (this_command))
284f4730
JB
1042 {
1043 /* nil means key is undefined. */
1044 bitch_at_user ();
1045 defining_kbd_macro = 0;
1046 update_mode_lines = 1;
ff4b06d3 1047 current_perdisplay->Vprefix_arg = Qnil;
86e5706b 1048
284f4730
JB
1049 }
1050 else
1051 {
ff4b06d3 1052 if (NILP (current_perdisplay->Vprefix_arg) && ! no_direct)
284f4730
JB
1053 {
1054 /* Recognize some common commands in common situations and
1055 do them directly. */
8001d352 1056 if (EQ (this_command, Qforward_char) && PT < ZV)
284f4730
JB
1057 {
1058 struct Lisp_Vector *dp
1059 = window_display_table (XWINDOW (selected_window));
8001d352
KH
1060 lose = FETCH_CHAR (PT);
1061 SET_PT (PT + 1);
0f7a8fee 1062 if ((dp
82ba47d7 1063 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
9a5540db
RS
1064 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1065 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1066 && (lose >= 0x20 && lose < 0x7f)))
0f7a8fee 1067 : (lose >= 0x20 && lose < 0x7f))
284f4730
JB
1068 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1069 >= MODIFF)
1070 && (XFASTINT (XWINDOW (selected_window)->last_point)
8001d352 1071 == PT - 1)
284f4730
JB
1072 && !windows_or_buffers_changed
1073 && EQ (current_buffer->selective_display, Qnil)
1074 && !detect_input_pending ()
1075 && NILP (Vexecuting_macro))
1076 no_redisplay = direct_output_forward_char (1);
1077 goto directly_done;
1078 }
8001d352 1079 else if (EQ (this_command, Qbackward_char) && PT > BEGV)
284f4730
JB
1080 {
1081 struct Lisp_Vector *dp
1082 = window_display_table (XWINDOW (selected_window));
8001d352
KH
1083 SET_PT (PT - 1);
1084 lose = FETCH_CHAR (PT);
0f7a8fee 1085 if ((dp
ca873d73 1086 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
9a5540db
RS
1087 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1088 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1089 && (lose >= 0x20 && lose < 0x7f)))
0f7a8fee 1090 : (lose >= 0x20 && lose < 0x7f))
284f4730
JB
1091 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1092 >= MODIFF)
1093 && (XFASTINT (XWINDOW (selected_window)->last_point)
8001d352 1094 == PT + 1)
284f4730
JB
1095 && !windows_or_buffers_changed
1096 && EQ (current_buffer->selective_display, Qnil)
1097 && !detect_input_pending ()
1098 && NILP (Vexecuting_macro))
1099 no_redisplay = direct_output_forward_char (-1);
1100 goto directly_done;
1101 }
258bf746 1102 else if (EQ (this_command, Qself_insert_command)
284f4730 1103 /* Try this optimization only on ascii keystrokes. */
8c18cbfb 1104 && INTEGERP (last_command_char))
284f4730
JB
1105 {
1106 unsigned char c = XINT (last_command_char);
fc9cce4e 1107 int value;
284f4730 1108
fc9cce4e
RS
1109 if (NILP (Vexecuting_macro)
1110 && !EQ (minibuf_window, selected_window))
284f4730
JB
1111 {
1112 if (!nonundocount || nonundocount >= 20)
1113 {
1114 Fundo_boundary ();
1115 nonundocount = 0;
1116 }
1117 nonundocount++;
1118 }
fc9cce4e
RS
1119 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1120 < MODIFF)
1121 || (XFASTINT (XWINDOW (selected_window)->last_point)
1122 != PT)
4c61f38e 1123 || MODIFF <= SAVE_MODIFF
fc9cce4e
RS
1124 || windows_or_buffers_changed
1125 || !EQ (current_buffer->selective_display, Qnil)
1126 || detect_input_pending ()
1127 || !NILP (Vexecuting_macro));
1128 value = internal_self_insert (c, 0);
1129 if (value)
1130 lose = 1;
1131 if (value == 2)
1132 nonundocount = 0;
1133
1134 if (!lose
1135 && (PT == ZV || FETCH_CHAR (PT) == '\n'))
284f4730
JB
1136 {
1137 struct Lisp_Vector *dp
1138 = window_display_table (XWINDOW (selected_window));
b8d9050d 1139 int lose = c;
284f4730 1140
0f7a8fee
JB
1141 if (dp)
1142 {
9b8eb840 1143 Lisp_Object obj;
0f7a8fee 1144
9b8eb840 1145 obj = DISP_CHAR_VECTOR (dp, lose);
054c8675 1146 if (NILP (obj))
8e91f441
RS
1147 {
1148 /* Do it only for char codes
1149 that by default display as themselves. */
1150 if (lose >= 0x20 && lose <= 0x7e)
1151 no_redisplay = direct_output_for_insert (lose);
1152 }
8c18cbfb 1153 else if (VECTORP (obj)
054c8675 1154 && XVECTOR (obj)->size == 1
8c18cbfb
KH
1155 && (obj = XVECTOR (obj)->contents[0],
1156 INTEGERP (obj))
054c8675
RS
1157 /* Insist face not specified in glyph. */
1158 && (XINT (obj) & ((-1) << 8)) == 0)
bd48a052
RS
1159 no_redisplay
1160 = direct_output_for_insert (XINT (obj));
0f7a8fee
JB
1161 }
1162 else
1163 {
1164 if (lose >= 0x20 && lose <= 0x7e)
1165 no_redisplay = direct_output_for_insert (lose);
1166 }
284f4730
JB
1167 }
1168 goto directly_done;
1169 }
1170 }
1171
1172 /* Here for a command that isn't executed directly */
1173
1174 nonundocount = 0;
ff4b06d3 1175 if (NILP (current_perdisplay->Vprefix_arg))
284f4730 1176 Fundo_boundary ();
258bf746 1177 Fcommand_execute (this_command, Qnil);
284f4730 1178
284f4730 1179 }
a764a753 1180 directly_done: ;
284f4730 1181
a98ea3f9
RS
1182 /* Note that the value cell will never directly contain nil
1183 if the symbol is a local variable. */
1184 if (!NILP (XSYMBOL (Qpost_command_hook)->value) && !NILP (Vrun_hooks))
1185 safe_run_hooks (Qpost_command_hook);
86e5706b 1186
8a792f3a 1187 if (!NILP (Vdeferred_action_list))
3ef14e46 1188 safe_run_hooks (Qdeferred_action_function);
8a792f3a 1189
284f4730
JB
1190 /* If there is a prefix argument,
1191 1) We don't want last_command to be ``universal-argument''
1192 (that would be dumb), so don't set last_command,
1193 2) we want to leave echoing on so that the prefix will be
1194 echoed as part of this key sequence, so don't call
1195 cancel_echoing, and
1196 3) we want to leave this_command_key_count non-zero, so that
1197 read_char will realize that it is re-reading a character, and
1198 not echo it a second time. */
ff4b06d3 1199 if (NILP (current_perdisplay->Vprefix_arg))
284f4730
JB
1200 {
1201 last_command = this_command;
1202 cancel_echoing ();
1203 this_command_key_count = 0;
1204 }
86e5706b 1205
88ce066e 1206 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
86e5706b
RS
1207 {
1208 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1209 {
1210 current_buffer->mark_active = Qnil;
1211 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1212 }
1213 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1214 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1215 }
ff4b06d3
KH
1216
1217 finalize:
1218 /* Install chars successfully executed in kbd macro. */
1219
1220 if (defining_kbd_macro && NILP (current_perdisplay->Vprefix_arg))
1221 finalize_kbd_macro_chars ();
1222
1223#ifdef MULTI_PERDISPLAY
1224 current_perdisplay = global_perdisplay;
1225#endif
284f4730
JB
1226 }
1227}
1c9784c9
KH
1228
1229/* If we get an error while running the hook, cause the hook variable
1230 to be nil. Also inhibit quits, so that C-g won't cause the hook
1231 to mysteriously evaporate. */
1232static void
1233safe_run_hooks (hook)
a98ea3f9 1234 Lisp_Object hook;
1c9784c9 1235{
68553292 1236 Lisp_Object value;
1c9784c9
KH
1237 int count = specpdl_ptr - specpdl;
1238 specbind (Qinhibit_quit, Qt);
1239
a98ea3f9
RS
1240 /* We read and set the variable with functions,
1241 in case it's buffer-local. */
68553292 1242 value = Vcommand_hook_internal = Fsymbol_value (hook);
a98ea3f9 1243 Fset (hook, Qnil);
1c9784c9 1244 call1 (Vrun_hooks, Qcommand_hook_internal);
68553292 1245 Fset (hook, value);
1c9784c9
KH
1246
1247 unbind_to (count, Qnil);
1248}
284f4730
JB
1249\f
1250/* Number of seconds between polling for input. */
1251int polling_period;
1252
eb8c3be9 1253/* Nonzero means polling for input is temporarily suppressed. */
284f4730
JB
1254int poll_suppress_count;
1255
36922b18 1256/* Nonzero if polling_for_input is actually being used. */
284f4730
JB
1257int polling_for_input;
1258
36922b18
RS
1259#ifdef POLL_FOR_INPUT
1260
284f4730
JB
1261/* Handle an alarm once each second and read pending input
1262 so as to handle a C-g if it comces in. */
1263
1264SIGTYPE
1265input_poll_signal ()
1266{
9ac0d9e0
JB
1267 if (interrupt_input_blocked == 0
1268 && !waiting_for_input)
1269 read_avail_input (0);
284f4730
JB
1270 signal (SIGALRM, input_poll_signal);
1271 alarm (polling_period);
1272}
1273
1274#endif
1275
1276/* Begin signals to poll for input, if they are appropriate.
1277 This function is called unconditionally from various places. */
1278
1279start_polling ()
1280{
1281#ifdef POLL_FOR_INPUT
34f04431 1282 if (read_socket_hook && !interrupt_input)
284f4730
JB
1283 {
1284 poll_suppress_count--;
1285 if (poll_suppress_count == 0)
1286 {
1287 signal (SIGALRM, input_poll_signal);
1288 polling_for_input = 1;
1289 alarm (polling_period);
1290 }
1291 }
1292#endif
1293}
1294
1d3195db
RS
1295/* Nonzero if we are using polling to handle input asynchronously. */
1296
1297int
1298input_polling_used ()
1299{
1300#ifdef POLL_FOR_INPUT
1301 return read_socket_hook && !interrupt_input;
1302#else
1303 return 0;
1304#endif
1305}
1306
284f4730
JB
1307/* Turn off polling. */
1308
1309stop_polling ()
1310{
1311#ifdef POLL_FOR_INPUT
34f04431 1312 if (read_socket_hook && !interrupt_input)
284f4730
JB
1313 {
1314 if (poll_suppress_count == 0)
1315 {
1316 polling_for_input = 0;
1317 alarm (0);
1318 }
1319 poll_suppress_count++;
1320 }
1321#endif
1322}
fe8aeef3
RS
1323
1324/* Set the value of poll_suppress_count to COUNT
1325 and start or stop polling accordingly. */
1326
1327void
1328set_poll_suppress_count (count)
1329 int count;
1330{
1331#ifdef POLL_FOR_INPUT
1332 if (count == 0 && poll_suppress_count != 0)
1333 {
1334 poll_suppress_count = 1;
1335 start_polling ();
1336 }
1337 else if (count != 0 && poll_suppress_count == 0)
1338 {
1339 stop_polling ();
1340 }
1341 poll_suppress_count = count;
1342#endif
1343}
f4eef8b4 1344
d0a57728
RS
1345/* Bind polling_period to a value at least N.
1346 But don't decrease it. */
1347
f4eef8b4
RS
1348bind_polling_period (n)
1349 int n;
1350{
1351#ifdef POLL_FOR_INPUT
d0a57728
RS
1352 int new = polling_period;
1353
1354 if (n > new)
1355 new = n;
1356
f4eef8b4 1357 stop_polling ();
d0a57728
RS
1358 specbind (Qpolling_period, make_number (new));
1359 /* Start a new alarm with the new period. */
f4eef8b4
RS
1360 start_polling ();
1361#endif
1362}
284f4730 1363\f
faf5e407
JB
1364/* Applying the control modifier to CHARACTER. */
1365int
1366make_ctrl_char (c)
1367 int c;
1368{
d205953b
JB
1369 /* Save the upper bits here. */
1370 int upper = c & ~0177;
1371
1372 c &= 0177;
1373
1374 /* Everything in the columns containing the upper-case letters
1375 denotes a control character. */
1376 if (c >= 0100 && c < 0140)
1377 {
1378 int oc = c;
1379 c &= ~0140;
1380 /* Set the shift modifier for a control char
1381 made from a shifted letter. But only for letters! */
1382 if (oc >= 'A' && oc <= 'Z')
1383 c |= shift_modifier;
1384 }
1385
1386 /* The lower-case letters denote control characters too. */
1387 else if (c >= 'a' && c <= 'z')
1388 c &= ~0140;
1389
1390 /* Include the bits for control and shift
1391 only if the basic ASCII code can't indicate them. */
1392 else if (c >= ' ')
1393 c |= ctrl_modifier;
1394
1395 /* Replace the high bits. */
1396 c |= (upper & ~ctrl_modifier);
faf5e407
JB
1397
1398 return c;
1399}
1400
1401
1402\f
284f4730
JB
1403/* Input of single characters from keyboard */
1404
1405Lisp_Object print_help ();
1406static Lisp_Object kbd_buffer_get_event ();
e4fe371d 1407static void record_char ();
284f4730
JB
1408
1409/* read a character from the keyboard; call the redisplay if needed */
1410/* commandflag 0 means do not do auto-saving, but do do redisplay.
1411 -1 means do not do redisplay, but do do autosaving.
1412 1 means do both. */
1413
7d6de002
RS
1414/* The arguments MAPS and NMAPS are for menu prompting.
1415 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1416
1417 PREV_EVENT is the previous input event, or nil if we are reading
1418 the first event of a key sequence.
1419
6569cc8d
JB
1420 If USED_MOUSE_MENU is non-zero, then we set *USED_MOUSE_MENU to 1
1421 if we used a mouse menu to read the input, or zero otherwise. If
dcc408a0
RS
1422 USED_MOUSE_MENU is zero, *USED_MOUSE_MENU is left alone.
1423
1424 Value is t if we showed a menu and the user rejected it. */
7d6de002 1425
284f4730 1426Lisp_Object
7d6de002 1427read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
284f4730 1428 int commandflag;
7d6de002
RS
1429 int nmaps;
1430 Lisp_Object *maps;
1431 Lisp_Object prev_event;
1432 int *used_mouse_menu;
284f4730
JB
1433{
1434 register Lisp_Object c;
1435 int count;
1436 jmp_buf save_jump;
a1341f75 1437 int key_already_recorded = 0;
e4fe371d
RS
1438 Lisp_Object also_record;
1439 also_record = Qnil;
284f4730 1440
24597608 1441 if (CONSP (Vunread_command_events))
284f4730 1442 {
24597608
RS
1443 c = XCONS (Vunread_command_events)->car;
1444 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
284f4730 1445
284f4730
JB
1446 if (this_command_key_count == 0)
1447 goto reread_first;
1448 else
1449 goto reread;
1450 }
1451
86e5706b
RS
1452 if (unread_command_char != -1)
1453 {
18cd2eeb 1454 XSETINT (c, unread_command_char);
86e5706b
RS
1455 unread_command_char = -1;
1456
1457 if (this_command_key_count == 0)
1458 goto reread_first;
1459 else
1460 goto reread;
1461 }
1462
284f4730
JB
1463 if (!NILP (Vexecuting_macro))
1464 {
07d2b8de 1465#ifdef MULTI_FRAME
fce33686
JB
1466 /* We set this to Qmacro; since that's not a frame, nobody will
1467 try to switch frames on us, and the selected window will
1468 remain unchanged.
1469
1470 Since this event came from a macro, it would be misleading to
eb8c3be9 1471 leave internal_last_event_frame set to wherever the last
3c370943
JB
1472 real event came from. Normally, a switch-frame event selects
1473 internal_last_event_frame after each command is read, but
1474 events read from a macro should never cause a new frame to be
1475 selected. */
40f239ee
KH
1476 if (!current_perdisplay)
1477 abort ();
1478 current_perdisplay->internal_last_event_frame = Qmacro;
1479 current_perdisplay->Vlast_event_frame = Qmacro;
07d2b8de 1480#endif
fce33686 1481
663258f2
JB
1482 /* Exit the macro if we are at the end.
1483 Also, some things replace the macro with t
1484 to force an early exit. */
1485 if (EQ (Vexecuting_macro, Qt)
1486 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
284f4730 1487 {
18cd2eeb 1488 XSETINT (c, -1);
284f4730
JB
1489 return c;
1490 }
1491
1492 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
8c18cbfb 1493 if (STRINGP (Vexecuting_macro)
86e5706b 1494 && (XINT (c) & 0x80))
bb9e9bed 1495 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
86e5706b 1496
284f4730
JB
1497 executing_macro_index++;
1498
1499 goto from_macro;
1500 }
1501
cd21b839
JB
1502 if (!NILP (unread_switch_frame))
1503 {
1504 c = unread_switch_frame;
1505 unread_switch_frame = Qnil;
1506
1507 /* This event should make it into this_command_keys, and get echoed
f4255cd1
JB
1508 again, so we go to reread_first, rather than reread. */
1509 goto reread_first;
cd21b839
JB
1510 }
1511
3f9929bf
RS
1512 /* Don't bother updating menu bars while doing mouse tracking.
1513 We get events very rapidly then, and the menu bar won't be changing.
1514 We do update the menu bar once on entry to Ftrack_mouse. */
b8556aee 1515 if (commandflag > 0 && !input_pending && !detect_input_pending ())
e9bf89a0
RS
1516 prepare_menu_bars ();
1517
284f4730 1518 /* Save outer setjmp data, in case called recursively. */
f76475ad 1519 save_getcjmp (save_jump);
284f4730
JB
1520
1521 stop_polling ();
1522
1523 if (commandflag >= 0 && !input_pending && !detect_input_pending ())
1524 redisplay ();
1525
1526 if (_setjmp (getcjmp))
1527 {
18cd2eeb 1528 XSETINT (c, quit_char);
07d2b8de 1529#ifdef MULTI_FRAME
40f239ee
KH
1530 XSETFRAME (current_perdisplay->internal_last_event_frame,
1531 selected_frame);
1532 current_perdisplay->Vlast_event_frame
1533 = current_perdisplay->internal_last_event_frame;
07d2b8de 1534#endif
04904c29
RS
1535 /* If we report the quit char as an event,
1536 don't do so more than once. */
1537 if (!NILP (Vinhibit_quit))
1538 Vquit_flag = Qnil;
284f4730 1539
284f4730
JB
1540 goto non_reread;
1541 }
1542
1543 /* Message turns off echoing unless more keystrokes turn it on again. */
6c6083a9
KH
1544 if (echo_area_glyphs && *echo_area_glyphs
1545 && echo_area_glyphs != current_perdisplay->echobuf)
284f4730
JB
1546 cancel_echoing ();
1547 else
1548 /* If already echoing, continue. */
1549 echo_dash ();
1550
8150596a
RS
1551 /* Try reading a character via menu prompting in the minibuf.
1552 Try this before the sit-for, because the sit-for
1553 would do the wrong thing if we are supposed to do
1554 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
1555 after a mouse event so don't try a minibuf menu. */
1556 c = Qnil;
24597608
RS
1557 if (nmaps > 0 && INTERACTIVE
1558 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
1559 /* Don't bring up a menu if we already have another event. */
1560 && NILP (Vunread_command_events)
1561 && unread_command_char < 0
b8556aee 1562 && !detect_input_pending ())
8150596a
RS
1563 {
1564 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
24597608 1565 if (! NILP (c))
a1341f75
RS
1566 {
1567 key_already_recorded = 1;
1568 goto non_reread;
1569 }
8150596a
RS
1570 }
1571
284f4730
JB
1572 /* If in middle of key sequence and minibuffer not active,
1573 start echoing if enough time elapses. */
6c6083a9
KH
1574 if (minibuf_level == 0 && !current_perdisplay->immediate_echo
1575 && this_command_key_count > 0
27203ead 1576 && ! noninteractive
284f4730
JB
1577 && echo_keystrokes > 0
1578 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0))
1579 {
1580 Lisp_Object tem0;
1581
7d6de002
RS
1582 /* After a mouse event, start echoing right away.
1583 This is because we are probably about to display a menu,
1584 and we don't want to delay before doing so. */
dbc4e1c1 1585 if (EVENT_HAS_PARAMETERS (prev_event))
284f4730 1586 echo ();
7d6de002
RS
1587 else
1588 {
1589 tem0 = sit_for (echo_keystrokes, 0, 1, 1);
1590 if (EQ (tem0, Qt))
1591 echo ();
1592 }
284f4730
JB
1593 }
1594
1595 /* Maybe auto save due to number of keystrokes or idle time. */
1596
1597 if (commandflag != 0
1598 && auto_save_interval > 0
51172b6d 1599 && num_nonmacro_input_chars - last_auto_save > max (auto_save_interval, 20)
284f4730
JB
1600 && !detect_input_pending ())
1601 {
1602 jmp_buf temp;
1603 save_getcjmp (temp);
1604 Fdo_auto_save (Qnil, Qnil);
ef8fd672
RS
1605 /* Hooks can actually change some buffers in auto save. */
1606 redisplay ();
284f4730
JB
1607 restore_getcjmp (temp);
1608 }
1609
8150596a 1610 /* Try reading using an X menu.
24597608
RS
1611 This is never confused with reading using the minibuf
1612 because the recursive call of read_char in read_char_minibuf_menu_prompt
1613 does not pass on any keymaps. */
1614 if (nmaps > 0 && INTERACTIVE
1615 && !NILP (prev_event) && EVENT_HAS_PARAMETERS (prev_event)
1616 /* Don't bring up a menu if we already have another event. */
1617 && NILP (Vunread_command_events)
b8556aee 1618 && unread_command_char < 0)
8150596a 1619 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
7d6de002 1620
284f4730
JB
1621 /* Slow down auto saves logarithmically in size of current buffer,
1622 and garbage collect while we're at it. */
26c1639e 1623 if (INTERACTIVE && NILP (c))
7d6de002
RS
1624 {
1625 int delay_level, buffer_size;
1626
1627 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
1628 last_non_minibuf_size = Z - BEG;
1629 buffer_size = (last_non_minibuf_size >> 8) + 1;
1630 delay_level = 0;
1631 while (buffer_size > 64)
1632 delay_level++, buffer_size -= buffer_size >> 2;
1633 if (delay_level < 4) delay_level = 4;
1634 /* delay_level is 4 for files under around 50k, 7 at 100k,
1635 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
1636
1637 /* Auto save if enough time goes by without input. */
1638 if (commandflag != 0
51172b6d 1639 && num_nonmacro_input_chars > last_auto_save
8c18cbfb 1640 && INTEGERP (Vauto_save_timeout)
7d6de002
RS
1641 && XINT (Vauto_save_timeout) > 0)
1642 {
1643 Lisp_Object tem0;
1644 int delay = delay_level * XFASTINT (Vauto_save_timeout) / 4;
1645 tem0 = sit_for (delay, 0, 1, 1);
1646 if (EQ (tem0, Qt))
1647 {
1648 jmp_buf temp;
1649 save_getcjmp (temp);
1650 Fdo_auto_save (Qnil, Qnil);
1651 restore_getcjmp (temp);
1652
1653 /* If we have auto-saved and there is still no input
1654 available, garbage collect if there has been enough
1655 consing going on to make it worthwhile. */
1656 if (!detect_input_pending ()
1657 && consing_since_gc > gc_cons_threshold / 2)
ef8fd672
RS
1658 Fgarbage_collect ();
1659 /* prepare_menu_bars isn't safe here, but it should
1660 also be unnecessary. */
1661 redisplay ();
7d6de002
RS
1662 }
1663 }
1664 }
284f4730
JB
1665
1666 /* Actually read a character, waiting if necessary. */
4eb4f926 1667 while (NILP (c))
1e12dd87
RS
1668 {
1669 c = kbd_buffer_get_event ();
1670 if (!NILP (c))
1671 break;
1672 if (commandflag >= 0 && !input_pending && !detect_input_pending ())
cd72760c
RS
1673 {
1674 prepare_menu_bars ();
1675 redisplay ();
1676 }
1e12dd87 1677 }
284f4730 1678
284f4730 1679 /* Terminate Emacs in batch mode if at eof. */
8c18cbfb 1680 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
284f4730
JB
1681 Fkill_emacs (make_number (1));
1682
8c18cbfb 1683 if (INTEGERP (c))
80645119
JB
1684 {
1685 /* Add in any extra modifiers, where appropriate. */
1686 if ((extra_keyboard_modifiers & CHAR_CTL)
1687 || ((extra_keyboard_modifiers & 0177) < ' '
1688 && (extra_keyboard_modifiers & 0177) != 0))
faf5e407 1689 XSETINT (c, make_ctrl_char (XINT (c)));
80645119
JB
1690
1691 /* Transfer any other modifier bits directly from
1692 extra_keyboard_modifiers to c. Ignore the actual character code
1693 in the low 16 bits of extra_keyboard_modifiers. */
b8d9050d 1694 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
80645119 1695 }
9fa4395d 1696
284f4730
JB
1697 non_reread:
1698
f76475ad 1699 restore_getcjmp (save_jump);
284f4730
JB
1700
1701 start_polling ();
1702
dfd11da7
RS
1703 /* Buffer switch events are only for internal wakeups
1704 so don't show them to the user. */
8c18cbfb 1705 if (BUFFERP (c))
dfd11da7
RS
1706 return c;
1707
a1341f75
RS
1708 if (key_already_recorded)
1709 return c;
1710
dfd11da7
RS
1711 /* Wipe the echo area. */
1712 echo_area_glyphs = 0;
284f4730
JB
1713
1714 /* Handle things that only apply to characters. */
8c18cbfb 1715 if (INTEGERP (c))
284f4730
JB
1716 {
1717 /* If kbd_buffer_get_event gave us an EOF, return that. */
86e5706b 1718 if (XINT (c) == -1)
284f4730
JB
1719 return c;
1720
8c18cbfb 1721 if (STRINGP (Vkeyboard_translate_table)
f4255cd1
JB
1722 && XSTRING (Vkeyboard_translate_table)->size > XFASTINT (c))
1723 XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[XFASTINT (c)]);
284f4730
JB
1724 }
1725
e4fe371d
RS
1726 /* If this event is a mouse click in the menu bar,
1727 return just menu-bar for now. Modify the mouse click event
1728 so we won't do this twice, then queue it up. */
1729 if (EVENT_HAS_PARAMETERS (c)
1730 && CONSP (XCONS (c)->cdr)
1731 && CONSP (EVENT_START (c))
1732 && CONSP (XCONS (EVENT_START (c))->cdr))
284f4730 1733 {
e4fe371d 1734 Lisp_Object posn;
284f4730 1735
e4fe371d
RS
1736 posn = POSN_BUFFER_POSN (EVENT_START (c));
1737 /* Handle menu-bar events:
1738 insert the dummy prefix event `menu-bar'. */
1739 if (EQ (posn, Qmenu_bar))
1740 {
1741 /* Change menu-bar to (menu-bar) as the event "position". */
1742 POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
284f4730 1743
e4fe371d
RS
1744 also_record = c;
1745 Vunread_command_events = Fcons (c, Vunread_command_events);
1746 c = posn;
284f4730 1747 }
284f4730
JB
1748 }
1749
e4fe371d
RS
1750 record_char (c);
1751 if (! NILP (also_record))
1752 record_char (also_record);
51172b6d 1753
284f4730
JB
1754 from_macro:
1755 reread_first:
284f4730 1756
b8556aee 1757 /* Don't echo mouse motion events. */
8ea231fc
RS
1758 if (echo_keystrokes
1759 && ! (EVENT_HAS_PARAMETERS (c)
1760 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
e4fe371d
RS
1761 {
1762 echo_char (c);
1763 if (! NILP (also_record))
1764 echo_char (also_record);
1765 }
b8556aee 1766
db8c1663 1767 /* Record this character as part of the current key. */
b8556aee 1768 add_command_key (c);
e4fe371d
RS
1769 if (! NILP (also_record))
1770 add_command_key (also_record);
284f4730
JB
1771
1772 /* Re-reading in the middle of a command */
1773 reread:
1774 last_input_char = c;
1775 num_input_chars++;
1776
1777 /* Process the help character specially if enabled */
7e85b935 1778 if (EQ (c, Vhelp_char) && !NILP (Vhelp_form))
284f4730
JB
1779 {
1780 Lisp_Object tem0;
1781 count = specpdl_ptr - specpdl;
1782
1783 record_unwind_protect (Fset_window_configuration,
1784 Fcurrent_window_configuration (Qnil));
1785
1786 tem0 = Feval (Vhelp_form);
8c18cbfb 1787 if (STRINGP (tem0))
284f4730
JB
1788 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
1789
1790 cancel_echoing ();
3cb81011
KH
1791 do
1792 c = read_char (0, 0, 0, Qnil, 0);
8c18cbfb 1793 while (BUFFERP (c));
ff11dfa1 1794 /* Remove the help from the frame */
284f4730 1795 unbind_to (count, Qnil);
e9bf89a0 1796 prepare_menu_bars ();
284f4730
JB
1797 redisplay ();
1798 if (EQ (c, make_number (040)))
1799 {
1800 cancel_echoing ();
3cb81011
KH
1801 do
1802 c = read_char (0, 0, 0, Qnil, 0);
8c18cbfb 1803 while (BUFFERP (c));
284f4730
JB
1804 }
1805 }
1806
1807 return c;
1808}
1809
e4fe371d
RS
1810/* Record the input event C in various ways. */
1811
1812static void
1813record_char (c)
1814 Lisp_Object c;
1815{
1816 total_keys++;
1817 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
1818 if (++recent_keys_index >= NUM_RECENT_KEYS)
1819 recent_keys_index = 0;
1820
1821 /* Write c to the dribble file. If c is a lispy event, write
1822 the event's symbol to the dribble file, in <brackets>. Bleaugh.
1823 If you, dear reader, have a better idea, you've got the source. :-) */
1824 if (dribble)
1825 {
1826 if (INTEGERP (c))
1827 {
1828 if (XUINT (c) < 0x100)
1829 putc (XINT (c), dribble);
1830 else
1831 fprintf (dribble, " 0x%x", XUINT (c));
1832 }
1833 else
1834 {
1835 Lisp_Object dribblee;
1836
1837 /* If it's a structured event, take the event header. */
1838 dribblee = EVENT_HEAD (c);
1839
1840 if (SYMBOLP (dribblee))
1841 {
1842 putc ('<', dribble);
1843 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
1844 XSYMBOL (dribblee)->name->size,
1845 dribble);
1846 putc ('>', dribble);
1847 }
1848 }
1849
1850 fflush (dribble);
1851 }
1852
1853 store_kbd_macro_char (c);
1854
1855 num_nonmacro_input_chars++;
1856}
1857
284f4730
JB
1858Lisp_Object
1859print_help (object)
1860 Lisp_Object object;
1861{
622de3e9 1862 struct buffer *old = current_buffer;
284f4730 1863 Fprinc (object, Qnil);
622de3e9
KH
1864 set_buffer_internal (XBUFFER (Vstandard_output));
1865 call0 (intern ("help-mode"));
1866 set_buffer_internal (old);
284f4730
JB
1867 return Qnil;
1868}
1869
1870/* Copy out or in the info on where C-g should throw to.
1871 This is used when running Lisp code from within get_char,
1872 in case get_char is called recursively.
1873 See read_process_output. */
1874
1875save_getcjmp (temp)
1876 jmp_buf temp;
1877{
1878 bcopy (getcjmp, temp, sizeof getcjmp);
1879}
1880
1881restore_getcjmp (temp)
1882 jmp_buf temp;
1883{
1884 bcopy (temp, getcjmp, sizeof getcjmp);
1885}
1886
1887\f
2eb6bfbe
RM
1888#ifdef HAVE_MOUSE
1889
284f4730
JB
1890/* Restore mouse tracking enablement. See Ftrack_mouse for the only use
1891 of this function. */
a9d77f1f 1892
284f4730
JB
1893static Lisp_Object
1894tracking_off (old_value)
1895 Lisp_Object old_value;
1896{
71edead1
RS
1897 do_mouse_tracking = old_value;
1898 if (NILP (old_value))
284f4730 1899 {
284f4730
JB
1900 /* Redisplay may have been preempted because there was input
1901 available, and it assumes it will be called again after the
1902 input has been processed. If the only input available was
1903 the sort that we have just disabled, then we need to call
1904 redisplay. */
1905 if (!readable_events ())
1906 {
e9bf89a0 1907 prepare_menu_bars ();
284f4730
JB
1908 redisplay_preserve_echo_area ();
1909 get_input_pending (&input_pending);
1910 }
1911 }
1912}
1913
1914DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
4bb994d1
JB
1915 "Evaluate BODY with mouse movement events enabled.\n\
1916Within a `track-mouse' form, mouse motion generates input events that\n\
1917you can read with `read-event'.\n\
1918Normally, mouse motion is ignored.")
284f4730
JB
1919 (args)
1920 Lisp_Object args;
1921{
1922 int count = specpdl_ptr - specpdl;
1923 Lisp_Object val;
1924
a9d77f1f 1925 record_unwind_protect (tracking_off, do_mouse_tracking);
284f4730 1926
3f9929bf
RS
1927 if (!input_pending && !detect_input_pending ())
1928 prepare_menu_bars ();
1929
a9d77f1f 1930 XSETFRAME (do_mouse_tracking, selected_frame);
284f4730
JB
1931
1932 val = Fprogn (args);
1933 return unbind_to (count, val);
1934}
2eb6bfbe
RM
1935
1936#endif /* HAVE_MOUSE */
a612e298
RS
1937\f
1938/* Low level keyboard/mouse input.
1939 kbd_buffer_store_event places events in kbd_buffer, and
1940 kbd_buffer_get_event retrieves them.
1941 mouse_moved indicates when the mouse has moved again, and
1942 *mouse_position_hook provides the mouse position. */
1943
ba72822c 1944static PERDISPLAY *
40f239ee
KH
1945find_active_event_queue (check_mouse)
1946 int check_mouse;
612b78ef 1947{
ba72822c 1948 PERDISPLAY *perd;
6c6083a9
KH
1949
1950 for (perd = all_perdisplays; perd; perd = perd->next_perdisplay)
612b78ef
KH
1951 {
1952 if (perd->kbd_fetch_ptr != perd->kbd_store_ptr)
1953 return perd;
40f239ee
KH
1954#ifdef HAVE_MOUSE
1955 if (check_mouse && FRAMEP (do_mouse_tracking) && mouse_moved)
1956 return perd;
1957#endif
612b78ef
KH
1958 }
1959 return 0;
1960}
1961
a612e298
RS
1962/* Return true iff there are any events in the queue that read-char
1963 would return. If this returns false, a read-char would block. */
1964static int
1965readable_events ()
1966{
40f239ee 1967 return find_active_event_queue (1) != NULL;
a612e298
RS
1968}
1969
1970/* Set this for debugging, to have a way to get out */
1971int stop_character;
284f4730
JB
1972
1973/* Store an event obtained at interrupt level into kbd_buffer, fifo */
1974
1975void
1976kbd_buffer_store_event (event)
1977 register struct input_event *event;
1978{
ba72822c 1979 PERDISPLAY *perd = get_perdisplay (XFRAME (event->frame_or_window));
612b78ef 1980
284f4730
JB
1981 if (event->kind == no_event)
1982 abort ();
1983
1984 if (event->kind == ascii_keystroke)
1985 {
e9bf89a0 1986 register int c = event->code & 0377;
284f4730 1987
faf5e407
JB
1988 if (event->modifiers & ctrl_modifier)
1989 c = make_ctrl_char (c);
1990
9fd7d808
RS
1991 c |= (event->modifiers
1992 & (meta_modifier | alt_modifier
1993 | hyper_modifier | super_modifier));
1994
86e5706b 1995 if (c == quit_char)
284f4730 1996 {
3e51c7b7
JB
1997 extern SIGTYPE interrupt_signal ();
1998
07d2b8de 1999#ifdef MULTI_FRAME
284f4730 2000 /* If this results in a quit_char being returned to Emacs as
3c370943 2001 input, set Vlast_event_frame properly. If this doesn't
284f4730 2002 get returned to Emacs as an event, the next event read
ff11dfa1 2003 will set Vlast_event_frame again, so this is safe to do. */
4bb994d1 2004 {
9b8eb840 2005 Lisp_Object focus;
4bb994d1 2006
9b8eb840 2007 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
4bb994d1 2008 if (NILP (focus))
40f239ee 2009 perd->internal_last_event_frame = event->frame_or_window;
4bb994d1 2010 else
40f239ee
KH
2011 perd->internal_last_event_frame = focus;
2012 perd->Vlast_event_frame = perd->internal_last_event_frame;
4bb994d1 2013 }
07d2b8de 2014#endif
3e51c7b7 2015
ffd56f97 2016 last_event_timestamp = event->timestamp;
284f4730
JB
2017 interrupt_signal ();
2018 return;
2019 }
2020
2021 if (c && c == stop_character)
2022 {
2023 sys_suspend ();
2024 return;
2025 }
284f4730
JB
2026 }
2027
612b78ef
KH
2028 if (perd->kbd_store_ptr - perd->kbd_buffer == KBD_BUFFER_SIZE)
2029 perd->kbd_store_ptr = perd->kbd_buffer;
284f4730
JB
2030
2031 /* Don't let the very last slot in the buffer become full,
2032 since that would make the two pointers equal,
2033 and that is indistinguishable from an empty buffer.
2034 Discard the event if it would fill the last slot. */
612b78ef 2035 if (perd->kbd_fetch_ptr - 1 != perd->kbd_store_ptr)
284f4730 2036 {
612b78ef
KH
2037 volatile struct input_event *sp = perd->kbd_store_ptr;
2038 sp->kind = event->kind;
27203ead
RS
2039 if (event->kind == selection_request_event)
2040 {
2041 /* We must not use the ordinary copying code for this case,
2042 since `part' is an enum and copying it might not copy enough
2043 in this case. */
612b78ef 2044 bcopy (event, (char *) sp, sizeof (*event));
27203ead
RS
2045 }
2046 else
2047 {
612b78ef
KH
2048 sp->code = event->code;
2049 sp->part = event->part;
2050 sp->frame_or_window = event->frame_or_window;
2051 sp->modifiers = event->modifiers;
2052 sp->x = event->x;
2053 sp->y = event->y;
2054 sp->timestamp = event->timestamp;
27203ead 2055 }
ba72822c
KH
2056 (XVECTOR (perd->kbd_buffer_frame_or_window)->contents[perd->kbd_store_ptr
2057 - perd->kbd_buffer]
7b4aedb9 2058 = event->frame_or_window);
284f4730 2059
612b78ef 2060 perd->kbd_store_ptr++;
284f4730
JB
2061 }
2062}
a612e298
RS
2063\f
2064/* Read one event from the event buffer, waiting if necessary.
2065 The value is a Lisp object representing the event.
2066 The value is nil for an event that should be ignored,
2067 or that was handled here.
2068 We always read and discard one event. */
284f4730
JB
2069
2070static Lisp_Object
2071kbd_buffer_get_event ()
2072{
ba72822c 2073 PERDISPLAY *perd;
284f4730
JB
2074 register int c;
2075 Lisp_Object obj;
2076
2077 if (noninteractive)
2078 {
2079 c = getchar ();
18cd2eeb 2080 XSETINT (obj, c);
284f4730
JB
2081 return obj;
2082 }
2083
2084 /* Wait until there is input available. */
2085 for (;;)
2086 {
40f239ee
KH
2087 perd = find_active_event_queue (1);
2088 if (perd)
284f4730
JB
2089 break;
2090
2091 /* If the quit flag is set, then read_char will return
2092 quit_char, so that counts as "available input." */
2093 if (!NILP (Vquit_flag))
2094 quit_throw_to_read_char ();
2095
2096 /* One way or another, wait until input is available; then, if
2097 interrupt handlers have not read it, read it now. */
2098
2099#ifdef OLDVMS
2100 wait_for_kbd_input ();
2101#else
2102/* Note SIGIO has been undef'd if FIONREAD is missing. */
2103#ifdef SIGIO
2104 gobble_input (0);
2105#endif /* SIGIO */
40f239ee
KH
2106 perd = find_active_event_queue (1);
2107 if (!perd)
284f4730 2108 {
f76475ad
JB
2109 Lisp_Object minus_one;
2110
18cd2eeb 2111 XSETINT (minus_one, -1);
f76475ad 2112 wait_reading_process_input (0, 0, minus_one, 1);
284f4730 2113
40f239ee 2114 if (!interrupt_input && find_active_event_queue (0) == NULL)
bedae5a5
RS
2115 /* Pass 1 for EXPECT since we just waited to have input. */
2116 read_avail_input (1);
284f4730
JB
2117 }
2118#endif /* not VMS */
2119 }
2120
2121 /* At this point, we know that there is a readable event available
2122 somewhere. If the event queue is empty, then there must be a
2123 mouse movement enabled and available. */
40f239ee 2124 if (perd->kbd_fetch_ptr != perd->kbd_store_ptr)
284f4730 2125 {
cd21b839 2126 struct input_event *event;
3e51c7b7 2127
612b78ef
KH
2128 event = ((perd->kbd_fetch_ptr < perd->kbd_buffer + KBD_BUFFER_SIZE)
2129 ? perd->kbd_fetch_ptr
2130 : perd->kbd_buffer);
3e51c7b7 2131
cd21b839 2132 last_event_timestamp = event->timestamp;
cd21b839 2133
4bb994d1
JB
2134 obj = Qnil;
2135
48e416d4 2136 /* These two kinds of events get special handling
a612e298
RS
2137 and don't actually appear to the command loop.
2138 We return nil for them. */
48e416d4
RS
2139 if (event->kind == selection_request_event)
2140 {
598a9fa7 2141#ifdef HAVE_X11
4581e928
RS
2142 struct input_event copy = *event;
2143 /* Remove it from the buffer before processing it,
2144 since otherwise swallow_events will see it
2145 and process it again. */
612b78ef 2146 perd->kbd_fetch_ptr = event + 1;
4581e928 2147 x_handle_selection_request (&copy);
598a9fa7
JB
2148#else
2149 /* We're getting selection request events, but we don't have
2150 a window system. */
2151 abort ();
2152#endif
48e416d4
RS
2153 }
2154
1e12dd87 2155 else if (event->kind == selection_clear_event)
48e416d4 2156 {
598a9fa7 2157#ifdef HAVE_X11
48e416d4 2158 x_handle_selection_clear (event);
612b78ef 2159 perd->kbd_fetch_ptr = event + 1;
598a9fa7
JB
2160#else
2161 /* We're getting selection request events, but we don't have
2162 a window system. */
2163 abort ();
2164#endif
48e416d4 2165 }
990acea3
RS
2166#ifdef HAVE_X11
2167 else if (event->kind == delete_window_event)
2168 {
bbdc2092
RS
2169 /* Make an event (delete-frame (FRAME)). */
2170 obj = Fcons (event->frame_or_window, Qnil);
af17bd2b 2171 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
612b78ef 2172 perd->kbd_fetch_ptr = event + 1;
af17bd2b
KH
2173 }
2174 else if (event->kind == iconify_event)
2175 {
2176 /* Make an event (iconify-frame (FRAME)). */
2177 obj = Fcons (event->frame_or_window, Qnil);
2178 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
612b78ef 2179 perd->kbd_fetch_ptr = event + 1;
af17bd2b
KH
2180 }
2181 else if (event->kind == deiconify_event)
2182 {
2183 /* Make an event (make-frame-visible (FRAME)). */
2184 obj = Fcons (event->frame_or_window, Qnil);
2185 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
612b78ef 2186 perd->kbd_fetch_ptr = event + 1;
990acea3
RS
2187 }
2188#endif
f5e09c8b
RS
2189 else if (event->kind == menu_bar_event)
2190 {
2191 /* The event value is in the frame_or_window slot. */
2192 obj = event->frame_or_window;
612b78ef 2193 perd->kbd_fetch_ptr = event + 1;
f5e09c8b 2194 }
a8015ab5
KH
2195 else if (event->kind == buffer_switch_event)
2196 {
2197 /* The value doesn't matter here; only the type is tested. */
18cd2eeb 2198 XSETBUFFER (obj, current_buffer);
612b78ef 2199 perd->kbd_fetch_ptr = event + 1;
a8015ab5 2200 }
a612e298
RS
2201 /* Just discard these, by returning nil.
2202 (They shouldn't be found in the buffer,
2203 but on some machines it appears they do show up.) */
2204 else if (event->kind == no_event)
612b78ef 2205 perd->kbd_fetch_ptr = event + 1;
48e416d4 2206
4bb994d1
JB
2207 /* If this event is on a different frame, return a switch-frame this
2208 time, and leave the event in the queue for next time. */
1e12dd87
RS
2209 else
2210 {
a6d53864 2211#ifdef MULTI_FRAME
9b8eb840 2212 Lisp_Object frame;
1e12dd87 2213 Lisp_Object focus;
7b4aedb9 2214
9b8eb840 2215 frame = event->frame_or_window;
8c18cbfb 2216 if (WINDOWP (frame))
1e12dd87 2217 frame = WINDOW_FRAME (XWINDOW (frame));
4bb994d1 2218
1e12dd87
RS
2219 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
2220 if (! NILP (focus))
2221 frame = focus;
07d2b8de 2222
40f239ee 2223 if (! EQ (frame, perd->internal_last_event_frame)
1e12dd87
RS
2224 && XFRAME (frame) != selected_frame)
2225 obj = make_lispy_switch_frame (frame);
40f239ee 2226 perd->internal_last_event_frame = frame;
a6d53864 2227#endif /* MULTI_FRAME */
4bb994d1 2228
1e12dd87
RS
2229 /* If we didn't decide to make a switch-frame event, go ahead
2230 and build a real event from the queue entry. */
cd21b839 2231
1e12dd87
RS
2232 if (NILP (obj))
2233 {
2234 obj = make_lispy_event (event);
2235
2236 /* Wipe out this event, to catch bugs. */
2237 event->kind = no_event;
ba72822c 2238 XVECTOR (perd->kbd_buffer_frame_or_window)->contents[event - perd->kbd_buffer] = Qnil;
1e12dd87 2239
612b78ef 2240 perd->kbd_fetch_ptr = event + 1;
1e12dd87 2241 }
4bb994d1 2242 }
284f4730 2243 }
2eb6bfbe 2244#ifdef HAVE_MOUSE
a612e298 2245 /* Try generating a mouse motion event. */
a9d77f1f 2246 else if (FRAMEP (do_mouse_tracking) && mouse_moved)
284f4730 2247 {
a9d77f1f 2248 FRAME_PTR f = XFRAME (do_mouse_tracking);
7b4aedb9 2249 Lisp_Object bar_window;
3c370943 2250 enum scroll_bar_part part;
e5d77022
JB
2251 Lisp_Object x, y;
2252 unsigned long time;
284f4730 2253
e177ac3a
RS
2254 /* Note that this uses F to determine which display to look at.
2255 If there is no valid info, it does not store anything
2256 so x remains nil. */
2257 x = Qnil;
7b4aedb9 2258 (*mouse_position_hook) (&f, &bar_window, &part, &x, &y, &time);
4bb994d1
JB
2259
2260 obj = Qnil;
284f4730 2261
07d2b8de 2262#ifdef MULTI_FRAME
4bb994d1
JB
2263 /* Decide if we should generate a switch-frame event. Don't
2264 generate switch-frame events for motion outside of all Emacs
2265 frames. */
e177ac3a 2266 if (!NILP (x) && f)
cd21b839 2267 {
9b8eb840 2268 Lisp_Object frame;
4bb994d1 2269
9b8eb840 2270 frame = FRAME_FOCUS_FRAME (f);
4bb994d1 2271 if (NILP (frame))
18cd2eeb 2272 XSETFRAME (frame, f);
4bb994d1 2273
40f239ee 2274 if (! EQ (frame, perd->internal_last_event_frame)
80645119 2275 && XFRAME (frame) != selected_frame)
764cb3f9 2276 obj = make_lispy_switch_frame (frame);
40f239ee 2277 perd->internal_last_event_frame = frame;
cd21b839 2278 }
68553292 2279#endif
4bb994d1
JB
2280
2281 /* If we didn't decide to make a switch-frame event, go ahead and
2282 return a mouse-motion event. */
e177ac3a 2283 if (!NILP (x) && NILP (obj))
7b4aedb9 2284 obj = make_lispy_movement (f, bar_window, part, x, y, time);
6cbff1cb 2285 }
2eb6bfbe 2286#endif /* HAVE_MOUSE */
284f4730
JB
2287 else
2288 /* We were promised by the above while loop that there was
2289 something for us to read! */
2290 abort ();
2291
2292 input_pending = readable_events ();
2293
3c370943 2294#ifdef MULTI_FRAME
40f239ee 2295 perd->Vlast_event_frame = perd->internal_last_event_frame;
3c370943
JB
2296#endif
2297
284f4730
JB
2298 return (obj);
2299}
a612e298
RS
2300\f
2301/* Process any events that are not user-visible,
2302 then return, without reading any user-visible events. */
3a3b9632
RS
2303
2304void
2305swallow_events ()
2306{
ba72822c 2307 PERDISPLAY *perd;
40f239ee 2308 while ((perd = find_active_event_queue (0)) != NULL)
3a3b9632
RS
2309 {
2310 struct input_event *event;
2311
612b78ef
KH
2312 event = ((perd->kbd_fetch_ptr < perd->kbd_buffer + KBD_BUFFER_SIZE)
2313 ? perd->kbd_fetch_ptr
2314 : perd->kbd_buffer);
3a3b9632
RS
2315
2316 last_event_timestamp = event->timestamp;
2317
2318 /* These two kinds of events get special handling
2319 and don't actually appear to the command loop. */
2320 if (event->kind == selection_request_event)
2321 {
2322#ifdef HAVE_X11
4581e928
RS
2323 struct input_event copy;
2324 copy = *event;
612b78ef 2325 perd->kbd_fetch_ptr = event + 1;
4581e928 2326 x_handle_selection_request (&copy);
3a3b9632
RS
2327#else
2328 /* We're getting selection request events, but we don't have
2329 a window system. */
2330 abort ();
2331#endif
2332 }
2333
2334 else if (event->kind == selection_clear_event)
2335 {
2336#ifdef HAVE_X11
2337 x_handle_selection_clear (event);
612b78ef 2338 perd->kbd_fetch_ptr = event + 1;
3a3b9632
RS
2339#else
2340 /* We're getting selection request events, but we don't have
2341 a window system. */
2342 abort ();
2343#endif
2344 }
2345 else
2346 break;
2347 }
2348
2349 get_input_pending (&input_pending);
2350}
a612e298 2351\f
284f4730 2352/* Caches for modify_event_symbol. */
e9bf89a0 2353static Lisp_Object accent_key_syms;
270a208f 2354static Lisp_Object system_key_syms;
284f4730
JB
2355static Lisp_Object func_key_syms;
2356static Lisp_Object mouse_syms;
2357
270a208f 2358Lisp_Object Vsystem_key_alist;
80e4aa30 2359
e9bf89a0
RS
2360/* This is a list of keysym codes for special "accent" characters.
2361 It parallels lispy_accent_keys. */
2362
2363static int lispy_accent_codes[] =
2364{
79a7046c 2365#ifdef XK_dead_circumflex
e9bf89a0 2366 XK_dead_circumflex,
79a7046c
RS
2367#else
2368 0,
2369#endif
2370#ifdef XK_dead_grave
e9bf89a0 2371 XK_dead_grave,
79a7046c
RS
2372#else
2373 0,
2374#endif
2375#ifdef XK_dead_tilde
e9bf89a0 2376 XK_dead_tilde,
79a7046c
RS
2377#else
2378 0,
2379#endif
2380#ifdef XK_dead_diaeresis
e9bf89a0 2381 XK_dead_diaeresis,
79a7046c
RS
2382#else
2383 0,
2384#endif
2385#ifdef XK_dead_macron
e9bf89a0 2386 XK_dead_macron,
79a7046c
RS
2387#else
2388 0,
2389#endif
2390#ifdef XK_dead_degree
e9bf89a0 2391 XK_dead_degree,
79a7046c
RS
2392#else
2393 0,
2394#endif
2395#ifdef XK_dead_acute
e9bf89a0 2396 XK_dead_acute,
79a7046c
RS
2397#else
2398 0,
2399#endif
2400#ifdef XK_dead_cedilla
e9bf89a0 2401 XK_dead_cedilla,
79a7046c
RS
2402#else
2403 0,
2404#endif
2405#ifdef XK_dead_breve
e9bf89a0 2406 XK_dead_breve,
79a7046c
RS
2407#else
2408 0,
2409#endif
2410#ifdef XK_dead_ogonek
e9bf89a0 2411 XK_dead_ogonek,
79a7046c
RS
2412#else
2413 0,
2414#endif
2415#ifdef XK_dead_caron
e9bf89a0 2416 XK_dead_caron,
79a7046c
RS
2417#else
2418 0,
2419#endif
2420#ifdef XK_dead_doubleacute
e9bf89a0 2421 XK_dead_doubleacute,
79a7046c
RS
2422#else
2423 0,
2424#endif
2425#ifdef XK_dead_abovedot
e9bf89a0 2426 XK_dead_abovedot,
79a7046c
RS
2427#else
2428 0,
2429#endif
e9bf89a0
RS
2430};
2431
2432/* This is a list of Lisp names for special "accent" characters.
2433 It parallels lispy_accent_codes. */
2434
2435static char *lispy_accent_keys[] =
2436{
2437 "dead-circumflex",
2438 "dead-grave",
2439 "dead-tilde",
2440 "dead-diaeresis",
2441 "dead-macron",
2442 "dead-degree",
2443 "dead-acute",
2444 "dead-cedilla",
2445 "dead-breve",
2446 "dead-ogonek",
2447 "dead-caron",
2448 "dead-doubleacute",
2449 "dead-abovedot",
2450};
2451
284f4730
JB
2452/* You'll notice that this table is arranged to be conveniently
2453 indexed by X Windows keysym values. */
2454static char *lispy_function_keys[] =
2455 {
2456 /* X Keysym value */
2457
80e4aa30 2458 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00 */
86e5706b
RS
2459 "backspace",
2460 "tab",
2461 "linefeed",
2462 "clear",
2463 0,
2464 "return",
2465 0, 0,
2466 0, 0, 0, /* 0xff10 */
2467 "pause",
2468 0, 0, 0, 0, 0, 0, 0,
2469 "escape",
2470 0, 0, 0, 0,
2471 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff20...2f */
2472 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff30...3f */
2473 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
2474
284f4730
JB
2475 "home", /* 0xff50 */ /* IsCursorKey */
2476 "left",
2477 "up",
2478 "right",
2479 "down",
2480 "prior",
2481 "next",
2482 "end",
2483 "begin",
2484 0, /* 0xff59 */
2485 0, 0, 0, 0, 0, 0,
2486 "select", /* 0xff60 */ /* IsMiscFunctionKey */
2487 "print",
2488 "execute",
2489 "insert",
2490 0, /* 0xff64 */
2491 "undo",
2492 "redo",
2493 "menu",
2494 "find",
2495 "cancel",
2496 "help",
2497 "break", /* 0xff6b */
2498
9fdbfdf8 2499 0, 0, 0, 0, 0, 0, 0, 0, "backtab", 0,
284f4730 2500 0, /* 0xff76 */
36ae397e 2501 0, 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff7f */
284f4730
JB
2502 "kp-space", /* 0xff80 */ /* IsKeypadKey */
2503 0, 0, 0, 0, 0, 0, 0, 0,
2504 "kp-tab", /* 0xff89 */
2505 0, 0, 0,
2506 "kp-enter", /* 0xff8d */
2507 0, 0, 0,
2508 "kp-f1", /* 0xff91 */
2509 "kp-f2",
2510 "kp-f3",
2511 "kp-f4",
872157e7
RS
2512 "kp-home", /* 0xff95 */
2513 "kp-left",
2514 "kp-up",
2515 "kp-right",
2516 "kp-down",
2517 "kp-prior", /* kp-page-up */
2518 "kp-next", /* kp-page-down */
2519 "kp-end",
2520 "kp-begin",
2521 "kp-insert",
2522 "kp-delete",
2523 0, /* 0xffa0 */
2524 0, 0, 0, 0, 0, 0, 0, 0, 0,
284f4730
JB
2525 "kp-multiply", /* 0xffaa */
2526 "kp-add",
2527 "kp-separator",
2528 "kp-subtract",
2529 "kp-decimal",
2530 "kp-divide", /* 0xffaf */
2531 "kp-0", /* 0xffb0 */
2532 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
2533 0, /* 0xffba */
2534 0, 0,
2535 "kp-equal", /* 0xffbd */
2536 "f1", /* 0xffbe */ /* IsFunctionKey */
86e5706b
RS
2537 "f2",
2538 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
2539 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
2540 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
2541 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
2542 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
2543 0, 0, 0, 0, 0, 0, 0, 0,
2544 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
2545 0, 0, 0, 0, 0, 0, 0, "delete"
284f4730
JB
2546 };
2547
2548static char *lispy_mouse_names[] =
2549{
2550 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
2551};
2552
3c370943 2553/* Scroll bar parts. */
4bb994d1
JB
2554Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
2555
3c370943
JB
2556/* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
2557Lisp_Object *scroll_bar_parts[] = {
4bb994d1
JB
2558 &Qabove_handle, &Qhandle, &Qbelow_handle
2559};
2560
2561
7b4aedb9 2562/* A vector, indexed by button number, giving the down-going location
3c370943 2563 of currently depressed buttons, both scroll bar and non-scroll bar.
7b4aedb9
JB
2564
2565 The elements have the form
2566 (BUTTON-NUMBER MODIFIER-MASK . REST)
2567 where REST is the cdr of a position as it would be reported in the event.
2568
2569 The make_lispy_event function stores positions here to tell the
2570 difference between click and drag events, and to store the starting
2571 location to be included in drag events. */
2572
2573static Lisp_Object button_down_location;
88cb0656 2574
fbcd35bd
JB
2575/* Information about the most recent up-going button event: Which
2576 button, what location, and what time. */
2577
559f9d04
RS
2578static int last_mouse_button;
2579static int last_mouse_x;
2580static int last_mouse_y;
2581static unsigned long button_down_time;
fbcd35bd 2582
564dc952
JB
2583/* The maximum time between clicks to make a double-click,
2584 or Qnil to disable double-click detection,
2585 or Qt for no time limit. */
2586Lisp_Object Vdouble_click_time;
fbcd35bd
JB
2587
2588/* The number of clicks in this multiple-click. */
2589
2590int double_click_count;
2591
bb936752
FP
2592#ifdef USE_X_TOOLKIT
2593extern Lisp_Object map_event_to_object ();
2594#endif /* USE_X_TOOLKIT */
2595
284f4730
JB
2596/* Given a struct input_event, build the lisp event which represents
2597 it. If EVENT is 0, build a mouse movement event from the mouse
88cb0656
JB
2598 movement buffer, which should have a movement event in it.
2599
2600 Note that events must be passed to this function in the order they
2601 are received; this function stores the location of button presses
2602 in order to build drag events when the button is released. */
284f4730
JB
2603
2604static Lisp_Object
2605make_lispy_event (event)
2606 struct input_event *event;
2607{
79a7046c
RS
2608 int i;
2609
0220c518 2610 switch (SWITCH_ENUM_CAST (event->kind))
284f4730 2611 {
284f4730
JB
2612 /* A simple keystroke. */
2613 case ascii_keystroke:
86e5706b 2614 {
9343ab07 2615 Lisp_Object lispy_c;
e9bf89a0 2616 int c = event->code & 0377;
5a1c6df8
JB
2617 /* Turn ASCII characters into control characters
2618 when proper. */
2619 if (event->modifiers & ctrl_modifier)
d205953b
JB
2620 c = make_ctrl_char (c);
2621
2622 /* Add in the other modifier bits. We took care of ctrl_modifier
2623 just above, and the shift key was taken care of by the X code,
2624 and applied to control characters by make_ctrl_char. */
86e5706b
RS
2625 c |= (event->modifiers
2626 & (meta_modifier | alt_modifier
2627 | hyper_modifier | super_modifier));
559f9d04 2628 button_down_time = 0;
bb9e9bed 2629 XSETFASTINT (lispy_c, c);
9343ab07 2630 return lispy_c;
86e5706b 2631 }
284f4730
JB
2632
2633 /* A function key. The symbol may need to have modifier prefixes
2634 tacked onto it. */
2635 case non_ascii_keystroke:
559f9d04 2636 button_down_time = 0;
e9bf89a0
RS
2637
2638 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
2639 if (event->code == lispy_accent_codes[i])
2640 return modify_event_symbol (i,
2641 event->modifiers,
80e4aa30 2642 Qfunction_key, Qnil,
e9bf89a0
RS
2643 lispy_accent_keys, &accent_key_syms,
2644 (sizeof (lispy_accent_keys)
2645 / sizeof (lispy_accent_keys[0])));
2646
270a208f 2647 /* Handle system-specific keysyms. */
80e4aa30
RS
2648 if (event->code & (1 << 28))
2649 {
2650 /* We need to use an alist rather than a vector as the cache
2651 since we can't make a vector long enuf. */
270a208f
RS
2652 if (NILP (system_key_syms))
2653 system_key_syms = Fcons (Qnil, Qnil);
80e4aa30
RS
2654 return modify_event_symbol (event->code & 0xffffff,
2655 event->modifiers,
270a208f
RS
2656 Qfunction_key, Vsystem_key_alist,
2657 0, &system_key_syms, 0xffffff);
80e4aa30
RS
2658 }
2659
e9bf89a0
RS
2660 return modify_event_symbol (event->code - 0xff00,
2661 event->modifiers,
80e4aa30 2662 Qfunction_key, Qnil,
284f4730
JB
2663 lispy_function_keys, &func_key_syms,
2664 (sizeof (lispy_function_keys)
2665 / sizeof (lispy_function_keys[0])));
2666 break;
2667
87485d6f 2668#if defined (MULTI_FRAME) || defined (HAVE_MOUSE)
88cb0656
JB
2669 /* A mouse click. Figure out where it is, decide whether it's
2670 a press, click or drag, and build the appropriate structure. */
284f4730 2671 case mouse_click:
3c370943 2672 case scroll_bar_click:
284f4730 2673 {
e9bf89a0 2674 int button = event->code;
559f9d04 2675 int is_double;
7b4aedb9 2676 Lisp_Object position;
dbc4e1c1
JB
2677 Lisp_Object *start_pos_ptr;
2678 Lisp_Object start_pos;
284f4730 2679
7b4aedb9 2680 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
88cb0656
JB
2681 abort ();
2682
7b4aedb9
JB
2683 /* Build the position as appropriate for this mouse click. */
2684 if (event->kind == mouse_click)
284f4730 2685 {
7b4aedb9 2686 int part;
598a9fa7 2687 FRAME_PTR f = XFRAME (event->frame_or_window);
0aafc975 2688 Lisp_Object window;
7b4aedb9 2689 Lisp_Object posn;
9e20143a
RS
2690 int row, column;
2691
5da3133a
RS
2692 /* Ignore mouse events that were made on frame that
2693 have been deleted. */
2694 if (! FRAME_LIVE_P (f))
2695 return Qnil;
2696
9e20143a 2697 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
eef045bf 2698 &column, &row, 0, 1);
7b4aedb9 2699
eef045bf
RS
2700#ifndef USE_X_TOOLKIT
2701 /* In the non-toolkit version, clicks on the menu bar
2702 are ordinary button events in the event buffer.
2703 Distinguish them, and invoke the menu.
2704
2705 (In the toolkit version, the toolkit handles the menu bar
2706 and Emacs doesn't know about it until after the user
2707 makes a selection.) */
2708 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f))
bb936752 2709 {
b7c49376 2710 Lisp_Object items, item;
0a0e8fe6
RS
2711 int hpos;
2712 int i;
2713
2714 /* Activate the menu bar on the down event. If the
2715 up event comes in before the menu code can deal with it,
2716 just ignore it. */
2717 if (! (event->modifiers & down_modifier))
2718 return Qnil;
0aafc975 2719
f2ae6b3f 2720 item = Qnil;
5ec75a55 2721 items = FRAME_MENU_BAR_ITEMS (f);
b7c49376 2722 for (i = 0; i < XVECTOR (items)->size; i += 3)
5ec75a55
RS
2723 {
2724 Lisp_Object pos, string;
b7c49376
RS
2725 string = XVECTOR (items)->contents[i + 1];
2726 pos = XVECTOR (items)->contents[i + 2];
2727 if (NILP (string))
2728 break;
9e20143a
RS
2729 if (column >= XINT (pos)
2730 && column < XINT (pos) + XSTRING (string)->size)
b7c49376
RS
2731 {
2732 item = XVECTOR (items)->contents[i];
2733 break;
2734 }
5ec75a55 2735 }
9e20143a 2736
5ec75a55
RS
2737 position
2738 = Fcons (event->frame_or_window,
2739 Fcons (Qmenu_bar,
2740 Fcons (Fcons (event->x, event->y),
2741 Fcons (make_number (event->timestamp),
2742 Qnil))));
2743
b7c49376 2744 return Fcons (item, Fcons (position, Qnil));
5ec75a55 2745 }
eef045bf 2746#endif /* not USE_X_TOOLKIT */
0aafc975 2747
9e20143a 2748 window = window_from_coordinates (f, column, row, &part);
0aafc975 2749
8c18cbfb 2750 if (!WINDOWP (window))
78ced549
RS
2751 {
2752 window = event->frame_or_window;
2753 posn = Qnil;
2754 }
284f4730 2755 else
7b4aedb9 2756 {
9e20143a
RS
2757 int pixcolumn, pixrow;
2758 column -= XINT (XWINDOW (window)->left);
2759 row -= XINT (XWINDOW (window)->top);
2760 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
2761 XSETINT (event->x, pixcolumn);
2762 XSETINT (event->y, pixrow);
dbc4e1c1 2763
7b4aedb9
JB
2764 if (part == 1)
2765 posn = Qmode_line;
2766 else if (part == 2)
2767 posn = Qvertical_line;
2768 else
18cd2eeb
KH
2769 XSETINT (posn,
2770 buffer_posn_from_coords (XWINDOW (window),
2771 column, row));
7b4aedb9
JB
2772 }
2773
5ec75a55
RS
2774 position
2775 = Fcons (window,
2776 Fcons (posn,
2777 Fcons (Fcons (event->x, event->y),
2778 Fcons (make_number (event->timestamp),
2779 Qnil))));
284f4730 2780 }
7b4aedb9 2781 else
88cb0656 2782 {
9e20143a
RS
2783 Lisp_Object window;
2784 Lisp_Object portion_whole;
2785 Lisp_Object part;
2786
2787 window = event->frame_or_window;
2788 portion_whole = Fcons (event->x, event->y);
2789 part = *scroll_bar_parts[(int) event->part];
7b4aedb9
JB
2790
2791 position =
2792 Fcons (window,
3c370943 2793 Fcons (Qvertical_scroll_bar,
7b4aedb9
JB
2794 Fcons (portion_whole,
2795 Fcons (make_number (event->timestamp),
9e20143a 2796 Fcons (part, Qnil)))));
88cb0656
JB
2797 }
2798
dbc4e1c1
JB
2799 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
2800
2801 start_pos = *start_pos_ptr;
2802 *start_pos_ptr = Qnil;
7b4aedb9 2803
559f9d04
RS
2804 is_double = (button == last_mouse_button
2805 && XINT (event->x) == last_mouse_x
2806 && XINT (event->y) == last_mouse_y
2807 && button_down_time != 0
2808 && (EQ (Vdouble_click_time, Qt)
2809 || (INTEGERP (Vdouble_click_time)
2810 && ((int)(event->timestamp - button_down_time)
2811 < XINT (Vdouble_click_time)))));
2812 last_mouse_button = button;
2813 last_mouse_x = XINT (event->x);
2814 last_mouse_y = XINT (event->y);
2815
7b4aedb9
JB
2816 /* If this is a button press, squirrel away the location, so
2817 we can decide later whether it was a click or a drag. */
2818 if (event->modifiers & down_modifier)
559f9d04
RS
2819 {
2820 if (is_double)
2821 {
2822 double_click_count++;
2823 event->modifiers |= ((double_click_count > 2)
2824 ? triple_modifier
2825 : double_modifier);
2826 }
2827 else
2828 double_click_count = 1;
2829 button_down_time = event->timestamp;
2830 *start_pos_ptr = Fcopy_alist (position);
2831 }
7b4aedb9 2832
88cb0656 2833 /* Now we're releasing a button - check the co-ordinates to
7b4aedb9 2834 see if this was a click or a drag. */
88cb0656
JB
2835 else if (event->modifiers & up_modifier)
2836 {
48e416d4
RS
2837 /* If we did not see a down before this up,
2838 ignore the up. Probably this happened because
2839 the down event chose a menu item.
2840 It would be an annoyance to treat the release
2841 of the button that chose the menu item
2842 as a separate event. */
2843
8c18cbfb 2844 if (!CONSP (start_pos))
48e416d4
RS
2845 return Qnil;
2846
88cb0656 2847 event->modifiers &= ~up_modifier;
48e416d4 2848#if 0 /* Formerly we treated an up with no down as a click event. */
8c18cbfb 2849 if (!CONSP (start_pos))
dbc4e1c1
JB
2850 event->modifiers |= click_modifier;
2851 else
48e416d4 2852#endif
dbc4e1c1
JB
2853 {
2854 /* The third element of every position should be the (x,y)
2855 pair. */
9b8eb840 2856 Lisp_Object down;
dbc4e1c1 2857
9b8eb840 2858 down = Fnth (make_number (2), start_pos);
fbcd35bd
JB
2859 if (EQ (event->x, XCONS (down)->car)
2860 && EQ (event->y, XCONS (down)->cdr))
2861 {
bc536d84 2862 event->modifiers |= click_modifier;
fbcd35bd
JB
2863 }
2864 else
2865 {
559f9d04 2866 button_down_time = 0;
fbcd35bd
JB
2867 event->modifiers |= drag_modifier;
2868 }
bc536d84
RS
2869 /* Don't check is_double; treat this as multiple
2870 if the down-event was multiple. */
2871 if (double_click_count > 1)
2872 event->modifiers |= ((double_click_count > 2)
2873 ? triple_modifier
2874 : double_modifier);
dbc4e1c1 2875 }
88cb0656
JB
2876 }
2877 else
2878 /* Every mouse event should either have the down_modifier or
7b4aedb9 2879 the up_modifier set. */
88cb0656
JB
2880 abort ();
2881
88cb0656 2882 {
7b4aedb9 2883 /* Get the symbol we should use for the mouse click. */
9b8eb840
KH
2884 Lisp_Object head;
2885
2886 head = modify_event_symbol (button,
2887 event->modifiers,
2888 Qmouse_click, Qnil,
2889 lispy_mouse_names, &mouse_syms,
2890 (sizeof (lispy_mouse_names)
2891 / sizeof (lispy_mouse_names[0])));
88cb0656 2892 if (event->modifiers & drag_modifier)
dbc4e1c1
JB
2893 return Fcons (head,
2894 Fcons (start_pos,
2895 Fcons (position,
2896 Qnil)));
fbcd35bd
JB
2897 else if (event->modifiers & (double_modifier | triple_modifier))
2898 return Fcons (head,
2899 Fcons (position,
2900 Fcons (make_number (double_click_count),
2901 Qnil)));
88cb0656
JB
2902 else
2903 return Fcons (head,
7b4aedb9 2904 Fcons (position,
88cb0656
JB
2905 Qnil));
2906 }
284f4730 2907 }
5846638c 2908#endif /* MULTI_FRAME or HAVE_MOUSE */
284f4730 2909
284f4730
JB
2910 /* The 'kind' field of the event is something we don't recognize. */
2911 default:
48e416d4 2912 abort ();
284f4730
JB
2913 }
2914}
2915
87485d6f 2916#if defined (MULTI_FRAME) || defined (HAVE_MOUSE)
6cbff1cb 2917
284f4730 2918static Lisp_Object
7b4aedb9 2919make_lispy_movement (frame, bar_window, part, x, y, time)
ff11dfa1 2920 FRAME_PTR frame;
7b4aedb9 2921 Lisp_Object bar_window;
3c370943 2922 enum scroll_bar_part part;
284f4730 2923 Lisp_Object x, y;
e5d77022 2924 unsigned long time;
284f4730 2925{
68553292 2926#ifdef MULTI_FRAME
3c370943 2927 /* Is it a scroll bar movement? */
7b4aedb9 2928 if (frame && ! NILP (bar_window))
4bb994d1 2929 {
9b8eb840 2930 Lisp_Object part_sym;
4bb994d1 2931
9b8eb840 2932 part_sym = *scroll_bar_parts[(int) part];
3c370943 2933 return Fcons (Qscroll_bar_movement,
7b4aedb9 2934 (Fcons (Fcons (bar_window,
3c370943 2935 Fcons (Qvertical_scroll_bar,
4bb994d1
JB
2936 Fcons (Fcons (x, y),
2937 Fcons (make_number (time),
cb5df6ae 2938 Fcons (part_sym,
4bb994d1
JB
2939 Qnil))))),
2940 Qnil)));
2941 }
2942
2943 /* Or is it an ordinary mouse movement? */
284f4730 2944 else
68553292 2945#endif /* MULTI_FRAME */
284f4730 2946 {
4bb994d1 2947 int area;
9e20143a 2948 Lisp_Object window;
4bb994d1 2949 Lisp_Object posn;
9e20143a
RS
2950 int column, row;
2951
68553292 2952#ifdef MULTI_FRAME
9e20143a 2953 if (frame)
68553292
RS
2954#else
2955 if (1)
2956#endif
047688cb
RS
2957 {
2958 /* It's in a frame; which window on that frame? */
2959 pixel_to_glyph_coords (frame, XINT (x), XINT (y), &column, &row, 0, 1);
2960 window = window_from_coordinates (frame, column, row, &area);
2961 }
9e20143a
RS
2962 else
2963 window = Qnil;
4bb994d1 2964
8c18cbfb 2965 if (WINDOWP (window))
4bb994d1 2966 {
9e20143a
RS
2967 int pixcolumn, pixrow;
2968 column -= XINT (XWINDOW (window)->left);
2969 row -= XINT (XWINDOW (window)->top);
2970 glyph_to_pixel_coords (frame, column, row, &pixcolumn, &pixrow);
2971 XSETINT (x, pixcolumn);
2972 XSETINT (y, pixrow);
4bb994d1
JB
2973
2974 if (area == 1)
2975 posn = Qmode_line;
2976 else if (area == 2)
2977 posn = Qvertical_line;
2978 else
18cd2eeb
KH
2979 XSETINT (posn,
2980 buffer_posn_from_coords (XWINDOW (window), column, row));
4bb994d1 2981 }
68553292 2982#ifdef MULTI_FRAME
e9bf89a0
RS
2983 else if (frame != 0)
2984 {
18cd2eeb 2985 XSETFRAME (window, frame);
e9bf89a0
RS
2986 posn = Qnil;
2987 }
68553292 2988#endif
284f4730 2989 else
4bb994d1
JB
2990 {
2991 window = Qnil;
2992 posn = Qnil;
bb9e9bed
KH
2993 XSETFASTINT (x, 0);
2994 XSETFASTINT (y, 0);
4bb994d1 2995 }
284f4730 2996
4bb994d1
JB
2997 return Fcons (Qmouse_movement,
2998 Fcons (Fcons (window,
2999 Fcons (posn,
3000 Fcons (Fcons (x, y),
3001 Fcons (make_number (time),
3002 Qnil)))),
3003 Qnil));
3004 }
284f4730
JB
3005}
3006
68553292 3007#endif /* neither MULTI_FRAME nor HAVE_MOUSE */
6cbff1cb 3008
cd21b839
JB
3009/* Construct a switch frame event. */
3010static Lisp_Object
3011make_lispy_switch_frame (frame)
3012 Lisp_Object frame;
3013{
3014 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
3015}
0a7f1fc0
JB
3016\f
3017/* Manipulating modifiers. */
284f4730 3018
0a7f1fc0 3019/* Parse the name of SYMBOL, and return the set of modifiers it contains.
284f4730 3020
0a7f1fc0
JB
3021 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
3022 SYMBOL's name of the end of the modifiers; the string from this
3023 position is the unmodified symbol name.
284f4730 3024
0a7f1fc0
JB
3025 This doesn't use any caches. */
3026static int
3027parse_modifiers_uncached (symbol, modifier_end)
284f4730 3028 Lisp_Object symbol;
0a7f1fc0 3029 int *modifier_end;
284f4730
JB
3030{
3031 struct Lisp_String *name;
3032 int i;
3033 int modifiers;
284f4730
JB
3034
3035 CHECK_SYMBOL (symbol, 1);
3036
3037 modifiers = 0;
3038 name = XSYMBOL (symbol)->name;
3039
284f4730 3040
0a7f1fc0 3041 for (i = 0; i+2 <= name->size; )
284f4730
JB
3042 switch (name->data[i])
3043 {
0a7f1fc0
JB
3044#define SINGLE_LETTER_MOD(bit) \
3045 if (name->data[i+1] != '-') \
3046 goto no_more_modifiers; \
3047 modifiers |= bit; \
fce33686 3048 i += 2;
0a7f1fc0
JB
3049
3050 case 'A':
3051 SINGLE_LETTER_MOD (alt_modifier);
284f4730
JB
3052 break;
3053
3054 case 'C':
0a7f1fc0 3055 SINGLE_LETTER_MOD (ctrl_modifier);
fce33686
JB
3056 break;
3057
3058 case 'H':
0a7f1fc0 3059 SINGLE_LETTER_MOD (hyper_modifier);
fce33686
JB
3060 break;
3061
3062 case 'M':
0a7f1fc0 3063 SINGLE_LETTER_MOD (meta_modifier);
284f4730
JB
3064 break;
3065
3066 case 'S':
0a7f1fc0 3067 SINGLE_LETTER_MOD (shift_modifier);
fce33686
JB
3068 break;
3069
3070 case 's':
86e5706b 3071 SINGLE_LETTER_MOD (super_modifier);
284f4730
JB
3072 break;
3073
fce33686
JB
3074 case 'd':
3075 if (i + 5 > name->size)
3076 goto no_more_modifiers;
3077 if (! strncmp (name->data + i, "drag-", 5))
3078 {
fce33686
JB
3079 modifiers |= drag_modifier;
3080 i += 5;
3081 }
3082 else if (! strncmp (name->data + i, "down-", 5))
3083 {
fce33686
JB
3084 modifiers |= down_modifier;
3085 i += 5;
3086 }
fbcd35bd
JB
3087 else if (i + 7 <= name->size
3088 && ! strncmp (name->data + i, "double-", 7))
3089 {
3090 modifiers |= double_modifier;
3091 i += 7;
3092 }
3093 else
3094 goto no_more_modifiers;
3095 break;
3096
3097 case 't':
3098 if (i + 7 > name->size)
3099 goto no_more_modifiers;
3100 if (! strncmp (name->data + i, "triple-", 7))
3101 {
3102 modifiers |= triple_modifier;
3103 i += 7;
3104 }
fce33686
JB
3105 else
3106 goto no_more_modifiers;
284f4730
JB
3107 break;
3108
3109 default:
3110 goto no_more_modifiers;
0a7f1fc0
JB
3111
3112#undef SINGLE_LETTER_MOD
284f4730
JB
3113 }
3114 no_more_modifiers:
3115
0a7f1fc0 3116 /* Should we include the `click' modifier? */
fbcd35bd
JB
3117 if (! (modifiers & (down_modifier | drag_modifier
3118 | double_modifier | triple_modifier))
0a7f1fc0 3119 && i + 7 == name->size
4bb994d1 3120 && strncmp (name->data + i, "mouse-", 6) == 0
6569cc8d 3121 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
0a7f1fc0
JB
3122 modifiers |= click_modifier;
3123
3124 if (modifier_end)
3125 *modifier_end = i;
3126
3127 return modifiers;
3128}
3129
3130
3131/* Return a symbol whose name is the modifier prefixes for MODIFIERS
3132 prepended to the string BASE[0..BASE_LEN-1].
3133 This doesn't use any caches. */
3134static Lisp_Object
3135apply_modifiers_uncached (modifiers, base, base_len)
3136 int modifiers;
3137 char *base;
3138 int base_len;
3139{
3140 /* Since BASE could contain nulls, we can't use intern here; we have
3141 to use Fintern, which expects a genuine Lisp_String, and keeps a
3142 reference to it. */
3143 char *new_mods =
fbcd35bd 3144 (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
0a7f1fc0 3145 int mod_len;
284f4730 3146
284f4730 3147 {
0a7f1fc0
JB
3148 char *p = new_mods;
3149
3150 /* Only the event queue may use the `up' modifier; it should always
3151 be turned into a click or drag event before presented to lisp code. */
3152 if (modifiers & up_modifier)
3153 abort ();
3154
3155 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
3156 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
3157 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
3158 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
3159 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
86e5706b 3160 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
fbcd35bd
JB
3161 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
3162 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
559f9d04
RS
3163 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
3164 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
0a7f1fc0
JB
3165 /* The click modifier is denoted by the absence of other modifiers. */
3166
3167 *p = '\0';
3168
3169 mod_len = p - new_mods;
3170 }
284f4730 3171
0a7f1fc0 3172 {
9b8eb840 3173 Lisp_Object new_name;
0a7f1fc0 3174
9b8eb840 3175 new_name = make_uninit_string (mod_len + base_len);
0a7f1fc0
JB
3176 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
3177 bcopy (base, XSTRING (new_name)->data + mod_len, base_len);
284f4730
JB
3178
3179 return Fintern (new_name, Qnil);
3180 }
3181}
3182
3183
0a7f1fc0
JB
3184static char *modifier_names[] =
3185{
fbcd35bd 3186 "up", "down", "drag", "click", "double", "triple", 0, 0,
1dfdf9e2 3187 0, 0, 0, 0, 0, 0, 0, 0,
86e5706b 3188 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
0a7f1fc0 3189};
80645119 3190#define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
0a7f1fc0
JB
3191
3192static Lisp_Object modifier_symbols;
3193
3194/* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
3195static Lisp_Object
3196lispy_modifier_list (modifiers)
3197 int modifiers;
3198{
3199 Lisp_Object modifier_list;
3200 int i;
3201
3202 modifier_list = Qnil;
80645119 3203 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
0a7f1fc0 3204 if (modifiers & (1<<i))
80645119
JB
3205 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
3206 modifier_list);
0a7f1fc0
JB
3207
3208 return modifier_list;
3209}
3210
3211
3212/* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
3213 where UNMODIFIED is the unmodified form of SYMBOL,
3214 MASK is the set of modifiers present in SYMBOL's name.
3215 This is similar to parse_modifiers_uncached, but uses the cache in
3216 SYMBOL's Qevent_symbol_element_mask property, and maintains the
3217 Qevent_symbol_elements property. */
3218static Lisp_Object
3219parse_modifiers (symbol)
3220 Lisp_Object symbol;
3221{
9b8eb840 3222 Lisp_Object elements;
0a7f1fc0 3223
9b8eb840 3224 elements = Fget (symbol, Qevent_symbol_element_mask);
0a7f1fc0
JB
3225 if (CONSP (elements))
3226 return elements;
3227 else
3228 {
3229 int end;
3230 int modifiers = parse_modifiers_uncached (symbol, &end);
9b8eb840 3231 Lisp_Object unmodified;
0a7f1fc0
JB
3232 Lisp_Object mask;
3233
9b8eb840
KH
3234 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
3235 XSYMBOL (symbol)->name->size - end),
3236 Qnil);
3237
734fef94
JB
3238 if (modifiers & ~((1<<VALBITS) - 1))
3239 abort ();
bb9e9bed 3240 XSETFASTINT (mask, modifiers);
0a7f1fc0
JB
3241 elements = Fcons (unmodified, Fcons (mask, Qnil));
3242
3243 /* Cache the parsing results on SYMBOL. */
3244 Fput (symbol, Qevent_symbol_element_mask,
3245 elements);
3246 Fput (symbol, Qevent_symbol_elements,
3247 Fcons (unmodified, lispy_modifier_list (modifiers)));
3248
3249 /* Since we know that SYMBOL is modifiers applied to unmodified,
3250 it would be nice to put that in unmodified's cache.
3251 But we can't, since we're not sure that parse_modifiers is
3252 canonical. */
3253
3254 return elements;
3255 }
3256}
3257
3258/* Apply the modifiers MODIFIERS to the symbol BASE.
3259 BASE must be unmodified.
3260
3261 This is like apply_modifiers_uncached, but uses BASE's
3262 Qmodifier_cache property, if present. It also builds
cd21b839
JB
3263 Qevent_symbol_elements properties, since it has that info anyway.
3264
3265 apply_modifiers copies the value of BASE's Qevent_kind property to
3266 the modified symbol. */
0a7f1fc0
JB
3267static Lisp_Object
3268apply_modifiers (modifiers, base)
3269 int modifiers;
3270 Lisp_Object base;
3271{
7b4aedb9 3272 Lisp_Object cache, index, entry, new_symbol;
0a7f1fc0 3273
80645119
JB
3274 /* Mask out upper bits. We don't know where this value's been. */
3275 modifiers &= (1<<VALBITS) - 1;
3276
0a7f1fc0 3277 /* The click modifier never figures into cache indices. */
0a7f1fc0 3278 cache = Fget (base, Qmodifier_cache);
bb9e9bed 3279 XSETFASTINT (index, (modifiers & ~click_modifier));
697e4895 3280 entry = assq_no_quit (index, cache);
0a7f1fc0
JB
3281
3282 if (CONSP (entry))
7b4aedb9
JB
3283 new_symbol = XCONS (entry)->cdr;
3284 else
3285 {
3286 /* We have to create the symbol ourselves. */
3287 new_symbol = apply_modifiers_uncached (modifiers,
3288 XSYMBOL (base)->name->data,
3289 XSYMBOL (base)->name->size);
3290
3291 /* Add the new symbol to the base's cache. */
3292 entry = Fcons (index, new_symbol);
3293 Fput (base, Qmodifier_cache, Fcons (entry, cache));
3294
3295 /* We have the parsing info now for free, so add it to the caches. */
bb9e9bed 3296 XSETFASTINT (index, modifiers);
7b4aedb9
JB
3297 Fput (new_symbol, Qevent_symbol_element_mask,
3298 Fcons (base, Fcons (index, Qnil)));
3299 Fput (new_symbol, Qevent_symbol_elements,
3300 Fcons (base, lispy_modifier_list (modifiers)));
3301 }
0a7f1fc0 3302
7b4aedb9
JB
3303 /* Make sure this symbol is of the same kind as BASE.
3304
3305 You'd think we could just set this once and for all when we
3306 intern the symbol above, but reorder_modifiers may call us when
3307 BASE's property isn't set right; we can't assume that just
80645119
JB
3308 because it has a Qmodifier_cache property it must have its
3309 Qevent_kind set right as well. */
7b4aedb9
JB
3310 if (NILP (Fget (new_symbol, Qevent_kind)))
3311 {
9b8eb840 3312 Lisp_Object kind;
7b4aedb9 3313
9b8eb840 3314 kind = Fget (base, Qevent_kind);
7b4aedb9
JB
3315 if (! NILP (kind))
3316 Fput (new_symbol, Qevent_kind, kind);
3317 }
3318
3319 return new_symbol;
0a7f1fc0
JB
3320}
3321
3322
3323/* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
3324 return a symbol with the modifiers placed in the canonical order.
3325 Canonical order is alphabetical, except for down and drag, which
3326 always come last. The 'click' modifier is never written out.
3327
3328 Fdefine_key calls this to make sure that (for example) C-M-foo
3329 and M-C-foo end up being equivalent in the keymap. */
3330
3331Lisp_Object
3332reorder_modifiers (symbol)
3333 Lisp_Object symbol;
3334{
3335 /* It's hopefully okay to write the code this way, since everything
3336 will soon be in caches, and no consing will be done at all. */
9b8eb840 3337 Lisp_Object parsed;
0a7f1fc0 3338
9b8eb840 3339 parsed = parse_modifiers (symbol);
0a7f1fc0
JB
3340 return apply_modifiers (XCONS (XCONS (parsed)->cdr)->car,
3341 XCONS (parsed)->car);
3342}
3343
3344
284f4730
JB
3345/* For handling events, we often want to produce a symbol whose name
3346 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
3347 to some base, like the name of a function key or mouse button.
3348 modify_event_symbol produces symbols of this sort.
3349
3350 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
3351 is the name of the i'th symbol. TABLE_SIZE is the number of elements
3352 in the table.
3353
80e4aa30
RS
3354 Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
3355 NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
3356
284f4730
JB
3357 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
3358 persist between calls to modify_event_symbol that it can use to
3359 store a cache of the symbols it's generated for this NAME_TABLE
80e4aa30 3360 before. The object stored there may be a vector or an alist.
284f4730
JB
3361
3362 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
3363
3364 MODIFIERS is a set of modifier bits (as given in struct input_events)
3365 whose prefixes should be applied to the symbol name.
3366
3367 SYMBOL_KIND is the value to be placed in the event_kind property of
88cb0656
JB
3368 the returned symbol.
3369
3370 The symbols we create are supposed to have an
eb8c3be9 3371 `event-symbol-elements' property, which lists the modifiers present
88cb0656
JB
3372 in the symbol's name. */
3373
284f4730 3374static Lisp_Object
80e4aa30
RS
3375modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
3376 name_table, symbol_table, table_size)
284f4730
JB
3377 int symbol_num;
3378 unsigned modifiers;
3379 Lisp_Object symbol_kind;
80e4aa30 3380 Lisp_Object name_alist;
284f4730
JB
3381 char **name_table;
3382 Lisp_Object *symbol_table;
3383 int table_size;
3384{
80e4aa30
RS
3385 Lisp_Object value;
3386 Lisp_Object symbol_int;
3387
18cd2eeb 3388 XSETINT (symbol_int, symbol_num);
284f4730
JB
3389
3390 /* Is this a request for a valid symbol? */
88cb0656 3391 if (symbol_num < 0 || symbol_num >= table_size)
0c2611c5 3392 return Qnil;
284f4730 3393
80e4aa30
RS
3394 if (CONSP (*symbol_table))
3395 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
3396
0a7f1fc0 3397 /* If *symbol_table doesn't seem to be initialized properly, fix that.
88cb0656 3398 *symbol_table should be a lisp vector TABLE_SIZE elements long,
4bb994d1
JB
3399 where the Nth element is the symbol for NAME_TABLE[N], or nil if
3400 we've never used that symbol before. */
80e4aa30 3401 else
88cb0656 3402 {
80e4aa30
RS
3403 if (! VECTORP (*symbol_table)
3404 || XVECTOR (*symbol_table)->size != table_size)
3405 {
3406 Lisp_Object size;
0a7f1fc0 3407
bb9e9bed 3408 XSETFASTINT (size, table_size);
80e4aa30
RS
3409 *symbol_table = Fmake_vector (size, Qnil);
3410 }
284f4730 3411
80e4aa30
RS
3412 value = XVECTOR (*symbol_table)->contents[symbol_num];
3413 }
284f4730 3414
0a7f1fc0 3415 /* Have we already used this symbol before? */
80e4aa30 3416 if (NILP (value))
284f4730 3417 {
0a7f1fc0 3418 /* No; let's create it. */
80e4aa30 3419 if (!NILP (name_alist))
b64b4075 3420 value = Fcdr_safe (Fassq (symbol_int, name_alist));
80e4aa30
RS
3421 else if (name_table[symbol_num])
3422 value = intern (name_table[symbol_num]);
b64b4075
RS
3423
3424 if (NILP (value))
d1f50460
RS
3425 {
3426 char buf[20];
3427 sprintf (buf, "key-%d", symbol_num);
80e4aa30 3428 value = intern (buf);
d1f50460 3429 }
0a7f1fc0 3430
80e4aa30
RS
3431 if (CONSP (*symbol_table))
3432 *symbol_table = Fcons (value, *symbol_table);
3433 else
3434 XVECTOR (*symbol_table)->contents[symbol_num] = value;
3435
0a7f1fc0
JB
3436 /* Fill in the cache entries for this symbol; this also
3437 builds the Qevent_symbol_elements property, which the user
3438 cares about. */
80e4aa30
RS
3439 apply_modifiers (modifiers & click_modifier, value);
3440 Fput (value, Qevent_kind, symbol_kind);
284f4730 3441 }
88cb0656 3442
0a7f1fc0 3443 /* Apply modifiers to that symbol. */
80e4aa30 3444 return apply_modifiers (modifiers, value);
284f4730 3445}
0a7f1fc0 3446
284f4730 3447\f
284f4730
JB
3448/* Store into *addr a value nonzero if terminal input chars are available.
3449 Serves the purpose of ioctl (0, FIONREAD, addr)
3450 but works even if FIONREAD does not exist.
3451 (In fact, this may actually read some input.) */
3452
3453static void
3454get_input_pending (addr)
3455 int *addr;
3456{
3457 /* First of all, have we already counted some input? */
3458 *addr = !NILP (Vquit_flag) || readable_events ();
3459
3460 /* If input is being read as it arrives, and we have none, there is none. */
3461 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
3462 return;
3463
3464 /* Try to read some input and see how much we get. */
3465 gobble_input (0);
3466 *addr = !NILP (Vquit_flag) || readable_events ();
3467}
3468
81931ba1 3469/* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
284f4730
JB
3470
3471int
3472gobble_input (expected)
3473 int expected;
3474{
3475#ifndef VMS
3476#ifdef SIGIO
3477 if (interrupt_input)
3478 {
32676c08 3479 SIGMASKTYPE mask;
e065a56e 3480 mask = sigblockx (SIGIO);
284f4730 3481 read_avail_input (expected);
e065a56e 3482 sigsetmask (mask);
284f4730
JB
3483 }
3484 else
81931ba1
RS
3485#ifdef POLL_FOR_INPUT
3486 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
3487 {
3488 SIGMASKTYPE mask;
3489 mask = sigblockx (SIGALRM);
3490 read_avail_input (expected);
3491 sigsetmask (mask);
3492 }
3493 else
87485d6f 3494#endif
284f4730
JB
3495#endif
3496 read_avail_input (expected);
3497#endif
3498}
a8015ab5 3499
241ceaf7
RS
3500/* Put a buffer_switch_event in the buffer
3501 so that read_key_sequence will notice the new current buffer. */
3502
a8015ab5
KH
3503record_asynch_buffer_change ()
3504{
3505 struct input_event event;
a30f0615
RS
3506 Lisp_Object tem;
3507
a8015ab5
KH
3508 event.kind = buffer_switch_event;
3509 event.frame_or_window = Qnil;
241ceaf7 3510
f65e6f7d 3511#ifdef subprocesses
a30f0615
RS
3512 /* We don't need a buffer-switch event unless Emacs is waiting for input.
3513 The purpose of the event is to make read_key_sequence look up the
3514 keymaps again. If we aren't in read_key_sequence, we don't need one,
3515 and the event could cause trouble by messing up (input-pending-p). */
3516 tem = Fwaiting_for_user_input_p ();
3517 if (NILP (tem))
3518 return;
f65e6f7d
RS
3519#else
3520 /* We never need these events if we have no asynchronous subprocesses. */
3521 return;
3522#endif
a30f0615 3523
241ceaf7
RS
3524 /* Make sure no interrupt happens while storing the event. */
3525#ifdef SIGIO
3526 if (interrupt_input)
3527 {
3528 SIGMASKTYPE mask;
3529 mask = sigblockx (SIGIO);
3530 kbd_buffer_store_event (&event);
3531 sigsetmask (mask);
3532 }
3533 else
3534#endif
3535 {
3536 stop_polling ();
3537 kbd_buffer_store_event (&event);
3538 start_polling ();
3539 }
a8015ab5 3540}
284f4730
JB
3541\f
3542#ifndef VMS
3543
3544/* Read any terminal input already buffered up by the system
3545 into the kbd_buffer, but do not wait.
3546
3547 EXPECTED should be nonzero if the caller knows there is some input.
3548
3549 Except on VMS, all input is read by this function.
3550 If interrupt_input is nonzero, this function MUST be called
3551 only when SIGIO is blocked.
3552
3553 Returns the number of keyboard chars read, or -1 meaning
3554 this is a bad time to try to read input. */
3555
3556static int
3557read_avail_input (expected)
3558 int expected;
3559{
3560 struct input_event buf[KBD_BUFFER_SIZE];
3561 register int i;
3562 int nread;
3563
3564 if (read_socket_hook)
3565 /* No need for FIONREAD or fcntl; just say don't wait. */
437f6112
RS
3566 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE,
3567 expected, expected);
284f4730
JB
3568 else
3569 {
17270835
RS
3570 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
3571 the kbd_buffer can really hold. That may prevent loss
3572 of characters on some systems when input is stuffed at us. */
3573 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
58788063 3574 int n_to_read;
284f4730 3575
58788063 3576 /* Determine how many characters we should *try* to read. */
bc536d84
RS
3577#ifdef WINDOWSNT
3578 return 0;
3579#else /* not WINDOWSNT */
80e4aa30 3580#ifdef MSDOS
58788063
RS
3581 n_to_read = dos_keysns ();
3582 if (n_to_read == 0)
3583 return 0;
c3a2738c 3584#else /* not MSDOS */
284f4730
JB
3585#ifdef FIONREAD
3586 /* Find out how much input is available. */
437f6112 3587 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
284f4730
JB
3588 /* Formerly simply reported no input, but that sometimes led to
3589 a failure of Emacs to terminate.
3590 SIGHUP seems appropriate if we can't reach the terminal. */
e4535288
RS
3591 /* ??? Is it really right to send the signal just to this process
3592 rather than to the whole process group?
3593 Perhaps on systems with FIONREAD Emacs is alone in its group. */
284f4730 3594 kill (getpid (), SIGHUP);
58788063 3595 if (n_to_read == 0)
284f4730 3596 return 0;
58788063
RS
3597 if (n_to_read > sizeof cbuf)
3598 n_to_read = sizeof cbuf;
284f4730 3599#else /* no FIONREAD */
482952ef 3600#if defined(USG) || defined(DGUX)
284f4730 3601 /* Read some input if available, but don't wait. */
58788063 3602 n_to_read = sizeof cbuf;
437f6112 3603 fcntl (input_fd, F_SETFL, O_NDELAY);
284f4730
JB
3604#else
3605 you lose;
3606#endif
3607#endif
80e4aa30 3608#endif /* not MSDOS */
bc536d84 3609#endif /* not WINDOWSNT */
284f4730 3610
58788063
RS
3611 /* Now read; for one reason or another, this will not block.
3612 NREAD is set to the number of chars read. */
9134775b 3613 do
284f4730 3614 {
80e4aa30
RS
3615#ifdef MSDOS
3616 cbuf[0] = dos_keyread();
3617 nread = 1;
3618#else
437f6112 3619 nread = read (input_fd, cbuf, n_to_read);
80e4aa30 3620#endif
762f2b92 3621#if defined (AIX) && (! defined (aix386) && defined (_BSD))
284f4730
JB
3622 /* The kernel sometimes fails to deliver SIGHUP for ptys.
3623 This looks incorrect, but it isn't, because _BSD causes
3624 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
3625 and that causes a value other than 0 when there is no input. */
854f3a54 3626 if (nread == 0)
80e4aa30 3627 kill (0, SIGHUP);
284f4730 3628#endif
9134775b 3629 }
791587ee
KH
3630 while (
3631 /* We used to retry the read if it was interrupted.
3632 But this does the wrong thing when O_NDELAY causes
3633 an EAGAIN error. Does anybody know of a situation
3634 where a retry is actually needed? */
3635#if 0
3636 nread < 0 && (errno == EAGAIN
6aec06f5 3637#ifdef EFAULT
9134775b 3638 || errno == EFAULT
80e4aa30 3639#endif
284f4730 3640#ifdef EBADSLT
9134775b 3641 || errno == EBADSLT
284f4730 3642#endif
791587ee
KH
3643 )
3644#else
3645 0
3646#endif
3647 );
284f4730
JB
3648
3649#ifndef FIONREAD
02c2c53f 3650#if defined (USG) || defined (DGUX)
437f6112 3651 fcntl (input_fd, F_SETFL, 0);
02c2c53f 3652#endif /* USG or DGUX */
284f4730
JB
3653#endif /* no FIONREAD */
3654 for (i = 0; i < nread; i++)
3655 {
3656 buf[i].kind = ascii_keystroke;
86e5706b 3657 buf[i].modifiers = 0;
b04904fb 3658 if (meta_key == 1 && (cbuf[i] & 0x80))
86e5706b 3659 buf[i].modifiers = meta_modifier;
b04904fb
RS
3660 if (meta_key != 2)
3661 cbuf[i] &= ~0x80;
f3e59d5e
KH
3662
3663 buf[i].code = cbuf[i];
7b4aedb9 3664#ifdef MULTI_FRAME
18cd2eeb 3665 XSETFRAME (buf[i].frame_or_window, selected_frame);
7b4aedb9
JB
3666#else
3667 buf[i].frame_or_window = Qnil;
3668#endif
284f4730
JB
3669 }
3670 }
3671
3672 /* Scan the chars for C-g and store them in kbd_buffer. */
3673 for (i = 0; i < nread; i++)
3674 {
3675 kbd_buffer_store_event (&buf[i]);
3676 /* Don't look at input that follows a C-g too closely.
3677 This reduces lossage due to autorepeat on C-g. */
3678 if (buf[i].kind == ascii_keystroke
9343ab07 3679 && buf[i].code == quit_char)
284f4730
JB
3680 break;
3681 }
3682
3683 return nread;
3684}
3685#endif /* not VMS */
3686\f
3687#ifdef SIGIO /* for entire page */
3688/* Note SIGIO has been undef'd if FIONREAD is missing. */
3689
2ce30ea2 3690SIGTYPE
284f4730
JB
3691input_available_signal (signo)
3692 int signo;
3693{
3694 /* Must preserve main program's value of errno. */
3695 int old_errno = errno;
3696#ifdef BSD4_1
3697 extern int select_alarmed;
3698#endif
3699
3700#ifdef USG
3701 /* USG systems forget handlers when they are used;
3702 must reestablish each time */
3703 signal (signo, input_available_signal);
3704#endif /* USG */
3705
3706#ifdef BSD4_1
3707 sigisheld (SIGIO);
3708#endif
3709
ffd56f97
JB
3710 if (input_available_clear_time)
3711 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
284f4730
JB
3712
3713 while (1)
3714 {
3715 int nread;
3716 nread = read_avail_input (1);
3717 /* -1 means it's not ok to read the input now.
3718 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
3719 0 means there was no keyboard input available. */
3720 if (nread <= 0)
3721 break;
3722
3723#ifdef BSD4_1
3724 select_alarmed = 1; /* Force the select emulator back to life */
3725#endif
3726 }
3727
3728#ifdef BSD4_1
3729 sigfree ();
3730#endif
3731 errno = old_errno;
3732}
3733#endif /* SIGIO */
ad163903
JB
3734
3735/* Send ourselves a SIGIO.
3736
3737 This function exists so that the UNBLOCK_INPUT macro in
3738 blockinput.h can have some way to take care of input we put off
3739 dealing with, without assuming that every file which uses
3740 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
3741void
3742reinvoke_input_signal ()
3743{
3744#ifdef SIGIO
3745 kill (0, SIGIO);
3746#endif
3747}
3748
3749
284f4730
JB
3750\f
3751/* Return the prompt-string of a sparse keymap.
3752 This is the first element which is a string.
3753 Return nil if there is none. */
3754
3755Lisp_Object
3756map_prompt (map)
3757 Lisp_Object map;
3758{
3759 while (CONSP (map))
3760 {
3761 register Lisp_Object tem;
3762 tem = Fcar (map);
8c18cbfb 3763 if (STRINGP (tem))
284f4730
JB
3764 return tem;
3765 map = Fcdr (map);
3766 }
3767 return Qnil;
3768}
3769
b7c49376
RS
3770static void menu_bar_item ();
3771static void menu_bar_one_keymap ();
3772
3773/* These variables hold the vector under construction within
3774 menu_bar_items and its subroutines, and the current index
3775 for storing into that vector. */
3776static Lisp_Object menu_bar_items_vector;
9343ab07 3777static int menu_bar_items_index;
5ec75a55 3778
b7c49376
RS
3779/* Return a vector of menu items for a menu bar, appropriate
3780 to the current buffer. Each item has three elements in the vector:
f5e09c8b 3781 KEY STRING MAPLIST.
b7c49376
RS
3782
3783 OLD is an old vector we can optionally reuse, or nil. */
5ec75a55
RS
3784
3785Lisp_Object
b7c49376
RS
3786menu_bar_items (old)
3787 Lisp_Object old;
5ec75a55
RS
3788{
3789 /* The number of keymaps we're scanning right now, and the number of
3790 keymaps we have allocated space for. */
3791 int nmaps;
3792
3793 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
3794 in the current keymaps, or nil where it is not a prefix. */
3795 Lisp_Object *maps;
3796
9f9c0e27 3797 Lisp_Object def, tem, tail;
5ec75a55
RS
3798
3799 Lisp_Object result;
3800
3801 int mapno;
47d319aa 3802 Lisp_Object oquit;
5ec75a55 3803
b7c49376
RS
3804 int i;
3805
3806 struct gcpro gcpro1;
3807
db60d856
JB
3808 /* In order to build the menus, we need to call the keymap
3809 accessors. They all call QUIT. But this function is called
3810 during redisplay, during which a quit is fatal. So inhibit
47d319aa
RS
3811 quitting while building the menus.
3812 We do this instead of specbind because (1) errors will clear it anyway
3813 and (2) this avoids risk of specpdl overflow. */
3814 oquit = Vinhibit_quit;
3815 Vinhibit_quit = Qt;
db60d856 3816
b7c49376
RS
3817 if (!NILP (old))
3818 menu_bar_items_vector = old;
3819 else
3820 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
3821 menu_bar_items_index = 0;
3822
3823 GCPRO1 (menu_bar_items_vector);
3824
5ec75a55
RS
3825 /* Build our list of keymaps.
3826 If we recognize a function key and replace its escape sequence in
3827 keybuf with its symbol, or if the sequence starts with a mouse
3828 click and we need to switch buffers, we jump back here to rebuild
3829 the initial keymaps from the current buffer. */
3830 {
3831 Lisp_Object *tmaps;
3832
d0a49716
RS
3833 /* Should overriding-local-map apply, here? */
3834 if (!NILP (Voverriding_local_map_menu_flag))
9dd3131c 3835 {
d0a49716
RS
3836 if (NILP (Voverriding_local_map))
3837 {
3838 /* Yes, and it is nil. Use just global map. */
3839 nmaps = 1;
3840 maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
3841 }
3842 else
3843 {
3844 /* Yes, and it is non-nil. Use it and the global map. */
3845 nmaps = 2;
3846 maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
3847 maps[0] = Voverriding_local_map;
3848 }
9dd3131c
RS
3849 }
3850 else
3851 {
d0a49716 3852 /* No, so use major and minor mode keymaps. */
9dd3131c
RS
3853 nmaps = current_minor_maps (0, &tmaps) + 2;
3854 maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
3855 bcopy (tmaps, maps, (nmaps - 2) * sizeof (maps[0]));
5ec75a55 3856#ifdef USE_TEXT_PROPERTIES
9dd3131c 3857 maps[nmaps-2] = get_local_map (PT, current_buffer);
5ec75a55 3858#else
9dd3131c 3859 maps[nmaps-2] = current_buffer->keymap;
5ec75a55 3860#endif
9dd3131c 3861 }
7e6992e0 3862 maps[nmaps-1] = current_global_map;
5ec75a55
RS
3863 }
3864
3865 /* Look up in each map the dummy prefix key `menu-bar'. */
3866
3867 result = Qnil;
3868
e58aa385 3869 for (mapno = nmaps - 1; mapno >= 0; mapno--)
5ec75a55
RS
3870 {
3871 if (! NILP (maps[mapno]))
e74fbc70 3872 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0));
5ec75a55
RS
3873 else
3874 def = Qnil;
3875
3876 tem = Fkeymapp (def);
3877 if (!NILP (tem))
b7c49376 3878 menu_bar_one_keymap (def);
5ec75a55
RS
3879 }
3880
b7c49376
RS
3881 /* Move to the end those items that should be at the end. */
3882
9f9c0e27
RS
3883 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCONS (tail)->cdr)
3884 {
b7c49376
RS
3885 int i;
3886 int end = menu_bar_items_index;
3887
3888 for (i = 0; i < end; i += 3)
3889 if (EQ (XCONS (tail)->car, XVECTOR (menu_bar_items_vector)->contents[i]))
3890 {
0301268e
RS
3891 Lisp_Object tem0, tem1, tem2;
3892 /* Move the item at index I to the end,
3893 shifting all the others forward. */
3894 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
3895 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
3896 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
3897 if (end > i + 3)
3898 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 3],
3899 &XVECTOR (menu_bar_items_vector)->contents[i],
3900 (end - i - 3) * sizeof (Lisp_Object));
3901 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem0;
3902 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem1;
3903 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem2;
3904 break;
b7c49376
RS
3905 }
3906 }
9f9c0e27 3907
b7c49376
RS
3908 /* Add nil, nil, nil at the end. */
3909 i = menu_bar_items_index;
3910 if (i + 3 > XVECTOR (menu_bar_items_vector)->size)
3911 {
3912 Lisp_Object tem;
3913 int newsize = 2 * i;
3914 tem = Fmake_vector (make_number (2 * i), Qnil);
3915 bcopy (XVECTOR (menu_bar_items_vector)->contents,
3916 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
3917 menu_bar_items_vector = tem;
9f9c0e27 3918 }
b7c49376
RS
3919 /* Add this item. */
3920 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
3921 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
3922 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
3923 menu_bar_items_index = i;
a73c5e29 3924
47d319aa 3925 Vinhibit_quit = oquit;
b7c49376
RS
3926 UNGCPRO;
3927 return menu_bar_items_vector;
5ec75a55
RS
3928}
3929\f
3930/* Scan one map KEYMAP, accumulating any menu items it defines
f5e09c8b 3931 in menu_bar_items_vector. */
5ec75a55 3932
b7c49376
RS
3933static void
3934menu_bar_one_keymap (keymap)
3935 Lisp_Object keymap;
5ec75a55
RS
3936{
3937 Lisp_Object tail, item, key, binding, item_string, table;
3938
3939 /* Loop over all keymap entries that have menu strings. */
8c18cbfb 3940 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
5ec75a55
RS
3941 {
3942 item = XCONS (tail)->car;
8c18cbfb 3943 if (CONSP (item))
5ec75a55
RS
3944 {
3945 key = XCONS (item)->car;
3946 binding = XCONS (item)->cdr;
8c18cbfb 3947 if (CONSP (binding))
5ec75a55
RS
3948 {
3949 item_string = XCONS (binding)->car;
8c18cbfb 3950 if (STRINGP (item_string))
b7c49376 3951 menu_bar_item (key, item_string, Fcdr (binding));
5ec75a55 3952 }
e58aa385 3953 else if (EQ (binding, Qundefined))
8aa034e1 3954 menu_bar_item (key, Qnil, binding);
5ec75a55 3955 }
8c18cbfb 3956 else if (VECTORP (item))
5ec75a55
RS
3957 {
3958 /* Loop over the char values represented in the vector. */
3959 int len = XVECTOR (item)->size;
3960 int c;
3961 for (c = 0; c < len; c++)
3962 {
3963 Lisp_Object character;
bb9e9bed 3964 XSETFASTINT (character, c);
5ec75a55 3965 binding = XVECTOR (item)->contents[c];
8c18cbfb 3966 if (CONSP (binding))
5ec75a55
RS
3967 {
3968 item_string = XCONS (binding)->car;
8c18cbfb 3969 if (STRINGP (item_string))
b7c49376 3970 menu_bar_item (key, item_string, Fcdr (binding));
5ec75a55 3971 }
e58aa385 3972 else if (EQ (binding, Qundefined))
8aa034e1 3973 menu_bar_item (key, Qnil, binding);
5ec75a55
RS
3974 }
3975 }
3976 }
5ec75a55
RS
3977}
3978
047a8ea7
RS
3979/* This is used as the handler when calling internal_condition_case_1. */
3980
3981static Lisp_Object
3982menu_bar_item_1 (arg)
3983 Lisp_Object arg;
3984{
3985 return Qnil;
3986}
3987
f5e09c8b
RS
3988/* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
3989 If there's already an item for KEY, add this DEF to it. */
3990
b7c49376
RS
3991static void
3992menu_bar_item (key, item_string, def)
3993 Lisp_Object key, item_string, def;
5ec75a55 3994{
e58aa385 3995 Lisp_Object tem;
5ec75a55 3996 Lisp_Object enabled;
b7c49376 3997 int i;
5ec75a55 3998
e58aa385
RS
3999 if (EQ (def, Qundefined))
4000 {
f5e09c8b 4001 /* If a map has an explicit `undefined' as definition,
e58aa385 4002 discard any previously made menu bar item. */
b7c49376
RS
4003
4004 for (i = 0; i < menu_bar_items_index; i += 3)
4005 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
4006 {
4007 if (menu_bar_items_index > i + 3)
4008 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 3],
4009 &XVECTOR (menu_bar_items_vector)->contents[i],
4010 (menu_bar_items_index - i - 3) * sizeof (Lisp_Object));
4011 menu_bar_items_index -= 3;
4012 return;
4013 }
8aa034e1
RS
4014
4015 /* If there's no definition for this key yet,
4016 just ignore `undefined'. */
4017 return;
e58aa385
RS
4018 }
4019
5ec75a55
RS
4020 /* See if this entry is enabled. */
4021 enabled = Qt;
4022
8c18cbfb 4023 if (SYMBOLP (def))
5ec75a55
RS
4024 {
4025 /* No property, or nil, means enable.
4026 Otherwise, enable if value is not nil. */
4027 tem = Fget (def, Qmenu_enable);
4028 if (!NILP (tem))
047a8ea7
RS
4029 /* (condition-case nil (eval tem)
4030 (error nil)) */
4031 enabled = internal_condition_case_1 (Feval, tem, Qerror,
4032 menu_bar_item_1);
5ec75a55
RS
4033 }
4034
b7c49376
RS
4035 /* Ignore this item if it's not enabled. */
4036 if (NILP (enabled))
4037 return;
5ec75a55 4038
f5e09c8b 4039 /* Find any existing item for this KEY. */
b7c49376
RS
4040 for (i = 0; i < menu_bar_items_index; i += 3)
4041 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
4042 break;
4043
f5e09c8b 4044 /* If we did not find this KEY, add it at the end. */
b7c49376
RS
4045 if (i == menu_bar_items_index)
4046 {
4047 /* If vector is too small, get a bigger one. */
4048 if (i + 3 > XVECTOR (menu_bar_items_vector)->size)
4049 {
4050 Lisp_Object tem;
4051 int newsize = 2 * i;
4052 tem = Fmake_vector (make_number (2 * i), Qnil);
4053 bcopy (XVECTOR (menu_bar_items_vector)->contents,
4054 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
4055 menu_bar_items_vector = tem;
4056 }
4057 /* Add this item. */
4058 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
4059 XVECTOR (menu_bar_items_vector)->contents[i++] = item_string;
f5e09c8b 4060 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (def, Qnil);
b7c49376
RS
4061 menu_bar_items_index = i;
4062 }
f5e09c8b
RS
4063 /* We did find an item for this KEY. Add DEF to its list of maps. */
4064 else
4065 {
4066 Lisp_Object old;
4067 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
4068 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (def, old);
4069 }
5ec75a55
RS
4070}
4071\f
dcc408a0
RS
4072/* Read a character using menus based on maps in the array MAPS.
4073 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
4074 Return t if we displayed a menu but the user rejected it.
7d6de002
RS
4075
4076 PREV_EVENT is the previous input event, or nil if we are reading
4077 the first event of a key sequence.
4078
6569cc8d
JB
4079 If USED_MOUSE_MENU is non-zero, then we set *USED_MOUSE_MENU to 1
4080 if we used a mouse menu to read the input, or zero otherwise. If
4081 USED_MOUSE_MENU is zero, *USED_MOUSE_MENU is left alone.
284f4730
JB
4082
4083 The prompting is done based on the prompt-string of the map
8150596a
RS
4084 and the strings associated with various map elements.
4085
4086 This can be done with X menus or with menus put in the minibuf.
4087 These are done in different ways, depending on how the input will be read.
4088 Menus using X are done after auto-saving in read-char, getting the input
4089 event from Fx_popup_menu; menus using the minibuf use read_char recursively
4090 and do auto-saving in the inner call of read_char. */
284f4730 4091
7617111f 4092static Lisp_Object
8150596a 4093read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
7d6de002
RS
4094 int nmaps;
4095 Lisp_Object *maps;
4096 Lisp_Object prev_event;
4097 int *used_mouse_menu;
284f4730 4098{
7d6de002
RS
4099 int mapno;
4100 register Lisp_Object name;
7d6de002
RS
4101 Lisp_Object rest, vector;
4102
6569cc8d
JB
4103 if (used_mouse_menu)
4104 *used_mouse_menu = 0;
284f4730
JB
4105
4106 /* Use local over global Menu maps */
4107
7d6de002
RS
4108 if (! menu_prompting)
4109 return Qnil;
4110
03361bcc
RS
4111 /* Optionally disregard all but the global map. */
4112 if (inhibit_local_menu_bar_menus)
4113 {
4114 maps += (nmaps - 1);
4115 nmaps = 1;
4116 }
4117
7d6de002
RS
4118 /* Get the menu name from the first map that has one (a prompt string). */
4119 for (mapno = 0; mapno < nmaps; mapno++)
4120 {
4121 name = map_prompt (maps[mapno]);
4122 if (!NILP (name))
4123 break;
4124 }
284f4730 4125
7d6de002 4126 /* If we don't have any menus, just read a character normally. */
dbc4e1c1 4127 if (mapno >= nmaps)
7d6de002
RS
4128 return Qnil;
4129
87485d6f 4130#if (defined (HAVE_X_WINDOWS) && defined (HAVE_X_MENU)) || defined (MSDOS)
7d6de002
RS
4131 /* If we got to this point via a mouse click,
4132 use a real menu for mouse selection. */
dbc4e1c1 4133 if (EVENT_HAS_PARAMETERS (prev_event))
7d6de002
RS
4134 {
4135 /* Display the menu and get the selection. */
4136 Lisp_Object *realmaps
4137 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
4138 Lisp_Object value;
4139 int nmaps1 = 0;
4140
4141 /* Use the maps that are not nil. */
4142 for (mapno = 0; mapno < nmaps; mapno++)
4143 if (!NILP (maps[mapno]))
4144 realmaps[nmaps1++] = maps[mapno];
4145
4146 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
663258f2
JB
4147 if (CONSP (value))
4148 {
4149 /* If we got more than one event, put all but the first
4150 onto this list to be read later.
4151 Return just the first event now. */
24597608
RS
4152 Vunread_command_events
4153 = nconc2 (XCONS (value)->cdr, Vunread_command_events);
663258f2
JB
4154 value = XCONS (value)->car;
4155 }
1c90c381 4156 else if (NILP (value))
dcc408a0 4157 value = Qt;
6569cc8d
JB
4158 if (used_mouse_menu)
4159 *used_mouse_menu = 1;
7d6de002
RS
4160 return value;
4161 }
87485d6f 4162#endif /* (HAVE_X_WINDOWS && HAVE_X_MENU) || MSDOS */
8150596a
RS
4163 return Qnil ;
4164}
4165
4166static Lisp_Object
24597608 4167read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
8150596a
RS
4168 int commandflag ;
4169 int nmaps;
4170 Lisp_Object *maps;
4171{
4172 int mapno;
4173 register Lisp_Object name;
4174 int nlength;
4175 int width = FRAME_WIDTH (selected_frame) - 4;
4176 char *menu = (char *) alloca (width + 4);
4177 int idx = -1;
9fdbfdf8 4178 int nobindings = 1;
8150596a
RS
4179 Lisp_Object rest, vector;
4180
4181 if (! menu_prompting)
4182 return Qnil;
4183
4184 /* Get the menu name from the first map that has one (a prompt string). */
4185 for (mapno = 0; mapno < nmaps; mapno++)
4186 {
4187 name = map_prompt (maps[mapno]);
4188 if (!NILP (name))
4189 break;
4190 }
4191
4192 /* If we don't have any menus, just read a character normally. */
4193 if (mapno >= nmaps)
4194 return Qnil;
284f4730
JB
4195
4196 /* Prompt string always starts with map's prompt, and a space. */
4197 strcpy (menu, XSTRING (name)->data);
4198 nlength = XSTRING (name)->size;
7d6de002 4199 menu[nlength++] = ':';
284f4730
JB
4200 menu[nlength++] = ' ';
4201 menu[nlength] = 0;
4202
7d6de002
RS
4203 /* Start prompting at start of first map. */
4204 mapno = 0;
4205 rest = maps[mapno];
284f4730
JB
4206
4207 /* Present the documented bindings, a line at a time. */
4208 while (1)
4209 {
4210 int notfirst = 0;
4211 int i = nlength;
4212 Lisp_Object obj;
4213 int ch;
8150596a 4214 int orig_defn_macro ;
284f4730 4215
284f4730 4216 /* Loop over elements of map. */
7d6de002 4217 while (i < width)
284f4730 4218 {
7d6de002 4219 Lisp_Object s, elt;
284f4730 4220
7d6de002
RS
4221 /* If reached end of map, start at beginning of next map. */
4222 if (NILP (rest))
4223 {
4224 mapno++;
4225 /* At end of last map, wrap around to first map if just starting,
4226 or end this line if already have something on it. */
4227 if (mapno == nmaps)
284f4730 4228 {
8150596a 4229 mapno = 0;
40932d1a 4230 if (notfirst || nobindings) break;
284f4730 4231 }
7d6de002 4232 rest = maps[mapno];
284f4730 4233 }
7d6de002
RS
4234
4235 /* Look at the next element of the map. */
4236 if (idx >= 0)
4237 elt = XVECTOR (vector)->contents[idx];
284f4730 4238 else
7d6de002
RS
4239 elt = Fcar_safe (rest);
4240
8c18cbfb 4241 if (idx < 0 && VECTORP (elt))
284f4730 4242 {
7d6de002
RS
4243 /* If we found a dense table in the keymap,
4244 advanced past it, but start scanning its contents. */
4245 rest = Fcdr_safe (rest);
4246 vector = elt;
4247 idx = 0;
284f4730 4248 }
7d6de002
RS
4249 else
4250 {
4251 /* An ordinary element. */
8150596a
RS
4252 if ( idx < 0 )
4253 s = Fcar_safe (Fcdr_safe (elt)); /* alist */
4254 else
4255 s = Fcar_safe(elt); /* vector */
8c18cbfb 4256 if (!STRINGP (s))
7d6de002
RS
4257 /* Ignore the element if it has no prompt string. */
4258 ;
4259 /* If we have room for the prompt string, add it to this line.
4260 If this is the first on the line, always add it. */
8150596a 4261 else if (XSTRING (s)->size + i + 2 < width
7d6de002
RS
4262 || !notfirst)
4263 {
4264 int thiswidth;
284f4730 4265
7d6de002
RS
4266 /* Punctuate between strings. */
4267 if (notfirst)
4268 {
4269 strcpy (menu + i, ", ");
4270 i += 2;
4271 }
4272 notfirst = 1;
8150596a 4273 nobindings = 0 ;
7d6de002
RS
4274
4275 /* Add as much of string as fits. */
4276 thiswidth = XSTRING (s)->size;
4277 if (thiswidth + i > width)
4278 thiswidth = width - i;
4279 bcopy (XSTRING (s)->data, menu + i, thiswidth);
4280 i += thiswidth;
8150596a 4281 menu[i] = 0;
7d6de002
RS
4282 }
4283 else
4284 {
4285 /* If this element does not fit, end the line now,
4286 and save the element for the next line. */
4287 strcpy (menu + i, "...");
4288 break;
4289 }
4290
4291 /* Move past this element. */
8150596a 4292 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
7d6de002
RS
4293 /* Handle reaching end of dense table. */
4294 idx = -1;
4295 if (idx >= 0)
4296 idx++;
4297 else
4298 rest = Fcdr_safe (rest);
4299 }
284f4730
JB
4300 }
4301
4302 /* Prompt with that and read response. */
4303 message1 (menu);
8150596a
RS
4304
4305 /* Make believe its not a keyboard macro in case the help char
4306 is pressed. Help characters are not recorded because menu prompting
4307 is not used on replay.
4308 */
4309 orig_defn_macro = defining_kbd_macro ;
4310 defining_kbd_macro = 0 ;
3cb81011
KH
4311 do
4312 obj = read_char (commandflag, 0, 0, Qnil, 0);
8c18cbfb 4313 while (BUFFERP (obj));
8150596a 4314 defining_kbd_macro = orig_defn_macro ;
284f4730 4315
8c18cbfb 4316 if (!INTEGERP (obj))
284f4730
JB
4317 return obj;
4318 else
4319 ch = XINT (obj);
4320
f4255cd1 4321 if (! EQ (obj, menu_prompt_more_char)
8c18cbfb 4322 && (!INTEGERP (menu_prompt_more_char)
f4255cd1 4323 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
8150596a
RS
4324 {
4325 if ( defining_kbd_macro )
4326 store_kbd_macro_char(obj) ;
4327 return obj;
4328 }
4329 /* Help char - go round again */
284f4730
JB
4330 }
4331}
284f4730
JB
4332\f
4333/* Reading key sequences. */
4334
4335/* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
4336 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
4337 keymap, or nil otherwise. Return the index of the first keymap in
4338 which KEY has any binding, or NMAPS if no map has a binding.
4339
4340 If KEY is a meta ASCII character, treat it like meta-prefix-char
4341 followed by the corresponding non-meta character. Keymaps in
4342 CURRENT with non-prefix bindings for meta-prefix-char become nil in
4343 NEXT.
4344
88cb0656
JB
4345 If KEY has no bindings in any of the CURRENT maps, NEXT is left
4346 unmodified.
4347
284f4730
JB
4348 NEXT may == CURRENT. */
4349
4350static int
4e50f26a 4351follow_key (key, nmaps, current, defs, next)
284f4730
JB
4352 Lisp_Object key;
4353 Lisp_Object *current, *defs, *next;
4354 int nmaps;
4355{
4356 int i, first_binding;
4357
4358 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
4359 followed by the corresponding non-meta character. */
8c18cbfb 4360 if (INTEGERP (key) && (XINT (key) & CHAR_META))
284f4730
JB
4361 {
4362 for (i = 0; i < nmaps; i++)
4363 if (! NILP (current[i]))
4364 {
cd21b839 4365 next[i] =
e74fbc70 4366 get_keyelt (access_keymap (current[i], meta_prefix_char, 1, 0));
284f4730
JB
4367
4368 /* Note that since we pass the resulting bindings through
4369 get_keymap_1, non-prefix bindings for meta-prefix-char
4370 disappear. */
f4255cd1 4371 next[i] = get_keymap_1 (next[i], 0, 1);
284f4730
JB
4372 }
4373 else
4374 next[i] = Qnil;
4375
4376 current = next;
18cd2eeb 4377 XSETINT (key, XFASTINT (key) & ~CHAR_META);
284f4730
JB
4378 }
4379
4380 first_binding = nmaps;
4381 for (i = nmaps - 1; i >= 0; i--)
4382 {
4383 if (! NILP (current[i]))
4384 {
e74fbc70 4385 defs[i] = get_keyelt (access_keymap (current[i], key, 1, 0));
284f4730
JB
4386 if (! NILP (defs[i]))
4387 first_binding = i;
4388 }
4389 else
4390 defs[i] = Qnil;
4391 }
4392
284f4730 4393 /* Given the set of bindings we've found, produce the next set of maps. */
0a7f1fc0
JB
4394 if (first_binding < nmaps)
4395 for (i = 0; i < nmaps; i++)
f4255cd1 4396 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
284f4730
JB
4397
4398 return first_binding;
4399}
4400
f4255cd1
JB
4401/* Read a sequence of keys that ends with a non prefix character,
4402 storing it in KEYBUF, a buffer of size BUFSIZE.
4403 Prompt with PROMPT.
284f4730 4404 Return the length of the key sequence stored.
dcc408a0 4405 Return -1 if the user rejected a command menu.
284f4730 4406
f4255cd1
JB
4407 Echo starting immediately unless `prompt' is 0.
4408
4409 Where a key sequence ends depends on the currently active keymaps.
4410 These include any minor mode keymaps active in the current buffer,
4411 the current buffer's local map, and the global map.
4412
4413 If a key sequence has no other bindings, we check Vfunction_key_map
4414 to see if some trailing subsequence might be the beginning of a
4415 function key's sequence. If so, we try to read the whole function
4416 key, and substitute its symbolic name into the key sequence.
4417
fbcd35bd
JB
4418 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
4419 `double-' events into similar click events, if that would make them
4420 bound. We try to turn `triple-' events first into `double-' events,
4421 then into clicks.
f4255cd1
JB
4422
4423 If we get a mouse click in a mode line, vertical divider, or other
4424 non-text area, we treat the click as if it were prefixed by the
4425 symbol denoting that area - `mode-line', `vertical-line', or
4426 whatever.
4427
4428 If the sequence starts with a mouse click, we read the key sequence
4429 with respect to the buffer clicked on, not the current buffer.
284f4730 4430
f4255cd1
JB
4431 If the user switches frames in the midst of a key sequence, we put
4432 off the switch-frame event until later; the next call to
4433 read_char will return it. */
48e416d4 4434
284f4730 4435static int
309b0fc8 4436read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last)
284f4730
JB
4437 Lisp_Object *keybuf;
4438 int bufsize;
84d91fda 4439 Lisp_Object prompt;
309b0fc8 4440 int dont_downcase_last;
284f4730 4441{
f4255cd1
JB
4442 int count = specpdl_ptr - specpdl;
4443
284f4730
JB
4444 /* How many keys there are in the current key sequence. */
4445 int t;
4446
284f4730
JB
4447 /* The length of the echo buffer when we started reading, and
4448 the length of this_command_keys when we started reading. */
4449 int echo_start;
f4255cd1 4450 int keys_start;
284f4730
JB
4451
4452 /* The number of keymaps we're scanning right now, and the number of
4453 keymaps we have allocated space for. */
4454 int nmaps;
4455 int nmaps_allocated = 0;
4456
284f4730
JB
4457 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
4458 the current keymaps. */
4459 Lisp_Object *defs;
4460
f4255cd1
JB
4461 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
4462 in the current keymaps, or nil where it is not a prefix. */
4463 Lisp_Object *submaps;
4464
e0dff5f6
RS
4465 /* The local map to start out with at start of key sequence. */
4466 Lisp_Object orig_local_map;
4467
4468 /* 1 if we have already considered switching to the local-map property
4469 of the place where a mouse click occurred. */
4470 int localized_local_map = 0;
4471
f4255cd1
JB
4472 /* The index in defs[] of the first keymap that has a binding for
4473 this key sequence. In other words, the lowest i such that
4474 defs[i] is non-nil. */
284f4730
JB
4475 int first_binding;
4476
f4255cd1 4477 /* If t < mock_input, then KEYBUF[t] should be read as the next
253598e4
JB
4478 input key.
4479
4480 We use this to recover after recognizing a function key. Once we
4481 realize that a suffix of the current key sequence is actually a
4482 function key's escape sequence, we replace the suffix with the
4483 function key's binding from Vfunction_key_map. Now keybuf
f4255cd1
JB
4484 contains a new and different key sequence, so the echo area,
4485 this_command_keys, and the submaps and defs arrays are wrong. In
4486 this situation, we set mock_input to t, set t to 0, and jump to
4487 restart_sequence; the loop will read keys from keybuf up until
4488 mock_input, thus rebuilding the state; and then it will resume
4489 reading characters from the keyboard. */
284f4730
JB
4490 int mock_input = 0;
4491
253598e4 4492 /* If the sequence is unbound in submaps[], then
f4255cd1
JB
4493 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
4494 and fkey_map is its binding.
253598e4 4495
f4255cd1
JB
4496 These might be > t, indicating that all function key scanning
4497 should hold off until t reaches them. We do this when we've just
4498 recognized a function key, to avoid searching for the function
4499 key's again in Vfunction_key_map. */
284f4730 4500 int fkey_start = 0, fkey_end = 0;
4efda7dd 4501 Lisp_Object fkey_map;
284f4730 4502
a612e298
RS
4503 /* Likewise, for key_translation_map. */
4504 int keytran_start = 0, keytran_end = 0;
4505 Lisp_Object keytran_map;
4506
cd21b839
JB
4507 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
4508 we put it off for later. While we're reading, we keep the event here. */
4efda7dd 4509 Lisp_Object delayed_switch_frame;
cd21b839 4510
51763820
BF
4511 /* See the comment below... */
4512#if defined (GOBBLE_FIRST_EVENT)
4efda7dd 4513 Lisp_Object first_event;
51763820 4514#endif
4efda7dd 4515
309b0fc8
RS
4516 Lisp_Object original_uppercase;
4517 int original_uppercase_position = -1;
4518
bc536d84 4519 /* Gets around Microsoft compiler limitations. */
309b0fc8 4520 int dummyflag = 0;
bc536d84 4521
3b9189f8
RS
4522 struct buffer *starting_buffer;
4523
e9bf89a0
RS
4524 /* Nonzero if we seem to have got the beginning of a binding
4525 in function_key_map. */
4526 int function_key_possible = 0;
00a78037 4527 int key_translation_possible = 0;
e9bf89a0 4528
4efda7dd
RS
4529 int junk;
4530
4531 last_nonmenu_event = Qnil;
4532
4533 delayed_switch_frame = Qnil;
4534 fkey_map = Vfunction_key_map;
a612e298 4535 keytran_map = Vkey_translation_map;
f4255cd1 4536
a612e298 4537 /* If there is no function-key-map, turn off function key scanning. */
f4255cd1
JB
4538 if (NILP (Fkeymapp (Vfunction_key_map)))
4539 fkey_start = fkey_end = bufsize + 1;
4540
a612e298
RS
4541 /* If there is no key-translation-map, turn off scanning. */
4542 if (NILP (Fkeymapp (Vkey_translation_map)))
4543 keytran_start = keytran_end = bufsize + 1;
4544
284f4730
JB
4545 if (INTERACTIVE)
4546 {
84d91fda
RS
4547 if (!NILP (prompt))
4548 echo_prompt (XSTRING (prompt)->data);
a98ea3f9 4549 else if (cursor_in_echo_area && echo_keystrokes)
284f4730
JB
4550 /* This doesn't put in a dash if the echo buffer is empty, so
4551 you don't always see a dash hanging out in the minibuffer. */
4552 echo_dash ();
284f4730
JB
4553 }
4554
f4255cd1
JB
4555 /* Record the initial state of the echo area and this_command_keys;
4556 we will need to restore them if we replay a key sequence. */
0a7f1fc0 4557 if (INTERACTIVE)
f4255cd1
JB
4558 echo_start = echo_length ();
4559 keys_start = this_command_key_count;
0a7f1fc0 4560
51763820
BF
4561#if defined (GOBBLE_FIRST_EVENT)
4562 /* This doesn't quite work, because some of the things that read_char
4563 does cannot safely be bypassed. It seems too risky to try to make
4564 this work right. */
4565
4efda7dd
RS
4566 /* Read the first char of the sequence specially, before setting
4567 up any keymaps, in case a filter runs and switches buffers on us. */
84d91fda 4568 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
4efda7dd 4569 &junk);
51763820 4570#endif /* GOBBLE_FIRST_EVENT */
4efda7dd 4571
e0dff5f6
RS
4572 orig_local_map = get_local_map (PT, current_buffer);
4573
7b4aedb9
JB
4574 /* We jump here when the key sequence has been thoroughly changed, and
4575 we need to rescan it starting from the beginning. When we jump here,
4576 keybuf[0..mock_input] holds the sequence we should reread. */
07d2b8de 4577 replay_sequence:
7b4aedb9 4578
3b9189f8 4579 starting_buffer = current_buffer;
e9bf89a0 4580 function_key_possible = 0;
00a78037 4581 key_translation_possible = 0;
3b9189f8 4582
f4255cd1 4583 /* Build our list of keymaps.
07d2b8de
JB
4584 If we recognize a function key and replace its escape sequence in
4585 keybuf with its symbol, or if the sequence starts with a mouse
4586 click and we need to switch buffers, we jump back here to rebuild
4587 the initial keymaps from the current buffer. */
284f4730
JB
4588 {
4589 Lisp_Object *maps;
4590
9dd3131c 4591 if (!NILP (Voverriding_local_map))
284f4730 4592 {
9dd3131c
RS
4593 nmaps = 2;
4594 if (nmaps > nmaps_allocated)
4595 {
4596 submaps = (Lisp_Object *) alloca (nmaps * sizeof (submaps[0]));
4597 defs = (Lisp_Object *) alloca (nmaps * sizeof (defs[0]));
4598 nmaps_allocated = nmaps;
4599 }
4600 submaps[0] = Voverriding_local_map;
284f4730 4601 }
9dd3131c
RS
4602 else
4603 {
4604 nmaps = current_minor_maps (0, &maps) + 2;
4605 if (nmaps > nmaps_allocated)
4606 {
4607 submaps = (Lisp_Object *) alloca (nmaps * sizeof (submaps[0]));
4608 defs = (Lisp_Object *) alloca (nmaps * sizeof (defs[0]));
4609 nmaps_allocated = nmaps;
4610 }
4611 bcopy (maps, submaps, (nmaps - 2) * sizeof (submaps[0]));
497ba7a1 4612#ifdef USE_TEXT_PROPERTIES
e0dff5f6 4613 submaps[nmaps-2] = orig_local_map;
497ba7a1 4614#else
9dd3131c 4615 submaps[nmaps-2] = current_buffer->keymap;
497ba7a1 4616#endif
9dd3131c 4617 }
7e6992e0 4618 submaps[nmaps-1] = current_global_map;
284f4730
JB
4619 }
4620
4621 /* Find an accurate initial value for first_binding. */
4622 for (first_binding = 0; first_binding < nmaps; first_binding++)
253598e4 4623 if (! NILP (submaps[first_binding]))
284f4730
JB
4624 break;
4625
3b9189f8 4626 /* Start from the beginning in keybuf. */
f4255cd1
JB
4627 t = 0;
4628
4629 /* These are no-ops the first time through, but if we restart, they
4630 revert the echo area and this_command_keys to their original state. */
4631 this_command_key_count = keys_start;
40932d1a 4632 if (INTERACTIVE && t < mock_input)
f4255cd1
JB
4633 echo_truncate (echo_start);
4634
cca310da
JB
4635 /* If the best binding for the current key sequence is a keymap, or
4636 we may be looking at a function key's escape sequence, keep on
4637 reading. */
253598e4 4638 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
cca310da
JB
4639 || (first_binding >= nmaps
4640 && fkey_start < t
4641 /* mock input is never part of a function key's sequence. */
a612e298 4642 && mock_input <= fkey_start)
0d882d52
KH
4643 || (first_binding >= nmaps
4644 && keytran_start < t && key_translation_possible)
e9bf89a0
RS
4645 /* Don't return in the middle of a possible function key sequence,
4646 if the only bindings we found were via case conversion.
4647 Thus, if ESC O a has a function-key-map translation
4648 and ESC o has a binding, don't return after ESC O,
4649 so that we can translate ESC O plus the next character. */
4e50f26a 4650 )
284f4730
JB
4651 {
4652 Lisp_Object key;
7d6de002 4653 int used_mouse_menu = 0;
284f4730 4654
7b4aedb9
JB
4655 /* Where the last real key started. If we need to throw away a
4656 key that has expanded into more than one element of keybuf
4657 (say, a mouse click on the mode line which is being treated
4658 as [mode-line (mouse-...)], then we backtrack to this point
4659 of keybuf. */
4660 int last_real_key_start;
4661
0a7f1fc0
JB
4662 /* These variables are analogous to echo_start and keys_start;
4663 while those allow us to restart the entire key sequence,
4664 echo_local_start and keys_local_start allow us to throw away
4665 just one key. */
f4255cd1
JB
4666 int echo_local_start, keys_local_start, local_first_binding;
4667
284f4730
JB
4668 if (t >= bufsize)
4669 error ("key sequence too long");
4670
f4255cd1
JB
4671 if (INTERACTIVE)
4672 echo_local_start = echo_length ();
4673 keys_local_start = this_command_key_count;
4674 local_first_binding = first_binding;
4675
4676 replay_key:
0a7f1fc0 4677 /* These are no-ops, unless we throw away a keystroke below and
f4255cd1
JB
4678 jumped back up to replay_key; in that case, these restore the
4679 variables to their original state, allowing us to replay the
0a7f1fc0 4680 loop. */
40932d1a 4681 if (INTERACTIVE && t < mock_input)
f4255cd1 4682 echo_truncate (echo_local_start);
0a7f1fc0
JB
4683 this_command_key_count = keys_local_start;
4684 first_binding = local_first_binding;
4685
7e85b935
RS
4686 /* By default, assume each event is "real". */
4687 last_real_key_start = t;
4688
f4255cd1 4689 /* Does mock_input indicate that we are re-reading a key sequence? */
284f4730
JB
4690 if (t < mock_input)
4691 {
4692 key = keybuf[t];
4693 add_command_key (key);
a98ea3f9
RS
4694 if (echo_keystrokes)
4695 echo_char (key);
284f4730 4696 }
253598e4
JB
4697
4698 /* If not, we should actually read a character. */
284f4730
JB
4699 else
4700 {
a6d53864
RS
4701 struct buffer *buf = current_buffer;
4702
84d91fda 4703 key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
a6d53864 4704 &used_mouse_menu);
284f4730 4705
dcc408a0
RS
4706 /* read_char returns t when it shows a menu and the user rejects it.
4707 Just return -1. */
4708 if (EQ (key, Qt))
4709 return -1;
4710
f4255cd1 4711 /* read_char returns -1 at the end of a macro.
284f4730
JB
4712 Emacs 18 handles this by returning immediately with a
4713 zero, so that's what we'll do. */
8c18cbfb 4714 if (INTEGERP (key) && XINT (key) == -1)
cd21b839 4715 {
f4255cd1 4716 t = 0;
bc536d84
RS
4717 /* The Microsoft C compiler can't handle the goto that
4718 would go here. */
309b0fc8 4719 dummyflag = 1;
bc536d84 4720 break;
cd21b839 4721 }
284f4730 4722
3cb81011
KH
4723 /* If the current buffer has been changed from under us, the
4724 keymap may have changed, so replay the sequence. */
8c18cbfb 4725 if (BUFFERP (key))
3cb81011
KH
4726 {
4727 mock_input = t;
4728 goto replay_sequence;
4729 }
4730
3b9189f8
RS
4731 /* If we have a quit that was typed in another frame, and
4732 quit_throw_to_read_char switched buffers,
4733 replay to get the right keymap. */
9343ab07 4734 if (XINT (key) == quit_char && current_buffer != starting_buffer)
3b9189f8
RS
4735 {
4736 keybuf[t++] = key;
4737 mock_input = t;
4738 Vquit_flag = Qnil;
4739 goto replay_sequence;
4740 }
3cb81011 4741
284f4730 4742 Vquit_flag = Qnil;
7e85b935 4743 }
284f4730 4744
7e85b935
RS
4745 /* Clicks in non-text areas get prefixed by the symbol
4746 in their CHAR-ADDRESS field. For example, a click on
4747 the mode line is prefixed by the symbol `mode-line'.
4748
4749 Furthermore, key sequences beginning with mouse clicks
4750 are read using the keymaps of the buffer clicked on, not
4751 the current buffer. So we may have to switch the buffer
4752 here.
4753
4754 When we turn one event into two events, we must make sure
4755 that neither of the two looks like the original--so that,
4756 if we replay the events, they won't be expanded again.
4757 If not for this, such reexpansion could happen either here
4758 or when user programs play with this-command-keys. */
4759 if (EVENT_HAS_PARAMETERS (key))
4760 {
9b8eb840 4761 Lisp_Object kind;
cca310da 4762
9b8eb840 4763 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
7e85b935 4764 if (EQ (kind, Qmouse_click))
0a7f1fc0 4765 {
9b8eb840 4766 Lisp_Object window, posn;
f4255cd1 4767
9b8eb840
KH
4768 window = POSN_WINDOW (EVENT_START (key));
4769 posn = POSN_BUFFER_POSN (EVENT_START (key));
8c18cbfb 4770 if (CONSP (posn))
0a7f1fc0 4771 {
7e85b935
RS
4772 /* We're looking at the second event of a
4773 sequence which we expanded before. Set
4774 last_real_key_start appropriately. */
4775 if (t > 0)
4776 last_real_key_start = t - 1;
cd21b839 4777 }
7e85b935
RS
4778
4779 /* Key sequences beginning with mouse clicks are
4780 read using the keymaps in the buffer clicked on,
4781 not the current buffer. If we're at the
4782 beginning of a key sequence, switch buffers. */
4783 if (last_real_key_start == 0
8c18cbfb
KH
4784 && WINDOWP (window)
4785 && BUFFERP (XWINDOW (window)->buffer)
7e85b935 4786 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
cd21b839 4787 {
7e85b935
RS
4788 keybuf[t] = key;
4789 mock_input = t + 1;
4790
4791 /* Arrange to go back to the original buffer once we're
4792 done reading the key sequence. Note that we can't
4793 use save_excursion_{save,restore} here, because they
4794 save point as well as the current buffer; we don't
4795 want to save point, because redisplay may change it,
4796 to accommodate a Fset_window_start or something. We
4797 don't want to do this at the top of the function,
4798 because we may get input from a subprocess which
4799 wants to change the selected window and stuff (say,
4800 emacsclient). */
4801 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4802
4803 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
e0dff5f6 4804 orig_local_map = get_local_map (PT, current_buffer);
7e85b935 4805 goto replay_sequence;
0a7f1fc0 4806 }
e0dff5f6
RS
4807 /* For a mouse click, get the local text-property keymap
4808 of the place clicked on, rather than point. */
4809 if (last_real_key_start == 0 && CONSP (XCONS (key)->cdr)
4810 && ! localized_local_map)
5ec75a55 4811 {
e0dff5f6
RS
4812 Lisp_Object map_here, start, pos;
4813
4814 localized_local_map = 1;
4815 start = EVENT_START (key);
4816 if (CONSP (start) && CONSP (XCONS (start)->cdr))
4817 {
4818 pos = POSN_BUFFER_POSN (start);
4819 if (INTEGERP (pos))
4820 {
4821 map_here = get_local_map (XINT (pos), current_buffer);
4822 if (!EQ (map_here, orig_local_map))
4823 {
4824 orig_local_map = map_here;
4825 keybuf[t] = key;
4826 mock_input = t + 1;
5ec75a55 4827
e0dff5f6
RS
4828 goto replay_sequence;
4829 }
4830 }
4831 }
4832 }
4833
4834 /* Expand mode-line and scroll-bar events into two events:
4835 use posn as a fake prefix key. */
4836 if (SYMBOLP (posn))
4837 {
7e85b935
RS
4838 if (t + 1 >= bufsize)
4839 error ("key sequence too long");
4840 keybuf[t] = posn;
4841 keybuf[t+1] = key;
4842 mock_input = t + 2;
4843
4844 /* Zap the position in key, so we know that we've
4845 expanded it, and don't try to do so again. */
4846 POSN_BUFFER_POSN (EVENT_START (key))
4847 = Fcons (posn, Qnil);
4848 goto replay_key;
5ec75a55 4849 }
0a7f1fc0 4850 }
7e85b935 4851 else if (EQ (kind, Qswitch_frame))
a6d53864 4852 {
7e85b935
RS
4853 /* If we're at the beginning of a key sequence, go
4854 ahead and return this event. If we're in the
4855 midst of a key sequence, delay it until the end. */
4856 if (t > 0)
4857 {
4858 delayed_switch_frame = key;
4859 goto replay_key;
4860 }
4861 }
7a80a6f6
RS
4862 else if (CONSP (XCONS (key)->cdr)
4863 && CONSP (EVENT_START (key))
4864 && CONSP (XCONS (EVENT_START (key))->cdr))
7e85b935 4865 {
9b8eb840 4866 Lisp_Object posn;
7e85b935 4867
9b8eb840 4868 posn = POSN_BUFFER_POSN (EVENT_START (key));
7e85b935
RS
4869 /* Handle menu-bar events:
4870 insert the dummy prefix event `menu-bar'. */
4871 if (EQ (posn, Qmenu_bar))
4872 {
4873 if (t + 1 >= bufsize)
4874 error ("key sequence too long");
4875 /* Run the Lucid hook. */
88ce066e
RS
4876 if (!NILP (Vrun_hooks))
4877 call1 (Vrun_hooks, Qactivate_menubar_hook);
7e85b935
RS
4878 /* If it has changed current-menubar from previous value,
4879 really recompute the menubar from the value. */
4880 if (! NILP (Vlucid_menu_bar_dirty_flag))
4881 call0 (Qrecompute_lucid_menubar);
4882 keybuf[t] = posn;
4883 keybuf[t+1] = key;
4884
4885 /* Zap the position in key, so we know that we've
4886 expanded it, and don't try to do so again. */
4887 POSN_BUFFER_POSN (EVENT_START (key))
4888 = Fcons (posn, Qnil);
4889
4890 mock_input = t + 2;
4891 goto replay_sequence;
4892 }
8c18cbfb 4893 else if (CONSP (posn))
7e85b935
RS
4894 {
4895 /* We're looking at the second event of a
4896 sequence which we expanded before. Set
4897 last_real_key_start appropriately. */
4898 if (last_real_key_start == t && t > 0)
4899 last_real_key_start = t - 1;
4900 }
a6d53864 4901 }
284f4730 4902 }
f4255cd1
JB
4903
4904 /* We have finally decided that KEY is something we might want
4905 to look up. */
284f4730
JB
4906 first_binding = (follow_key (key,
4907 nmaps - first_binding,
253598e4 4908 submaps + first_binding,
284f4730 4909 defs + first_binding,
4e50f26a 4910 submaps + first_binding)
284f4730 4911 + first_binding);
0a7f1fc0 4912
f4255cd1 4913 /* If KEY wasn't bound, we'll try some fallbacks. */
0a7f1fc0
JB
4914 if (first_binding >= nmaps)
4915 {
9b8eb840 4916 Lisp_Object head;
0a7f1fc0 4917
9b8eb840 4918 head = EVENT_HEAD (key);
7e85b935
RS
4919 if (EQ (head, Vhelp_char))
4920 {
4921 read_key_sequence_cmd = Vprefix_help_command;
4922 keybuf[t++] = key;
4923 last_nonmenu_event = key;
bc536d84
RS
4924 /* The Microsoft C compiler can't handle the goto that
4925 would go here. */
309b0fc8 4926 dummyflag = 1;
0d882d52 4927 break;
7e85b935
RS
4928 }
4929
8c18cbfb 4930 if (SYMBOLP (head))
0a7f1fc0 4931 {
9b8eb840
KH
4932 Lisp_Object breakdown;
4933 int modifiers;
0a7f1fc0 4934
9b8eb840
KH
4935 breakdown = parse_modifiers (head);
4936 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
559f9d04
RS
4937 /* Attempt to reduce an unbound mouse event to a simpler
4938 event that is bound:
4939 Drags reduce to clicks.
4940 Double-clicks reduce to clicks.
4941 Triple-clicks reduce to double-clicks, then to clicks.
4942 Down-clicks are eliminated.
4943 Double-downs reduce to downs, then are eliminated.
4944 Triple-downs reduce to double-downs, then to downs,
4945 then are eliminated. */
4946 if (modifiers & (down_modifier | drag_modifier
4947 | double_modifier | triple_modifier))
0a7f1fc0 4948 {
559f9d04
RS
4949 while (modifiers & (down_modifier | drag_modifier
4950 | double_modifier | triple_modifier))
fbcd35bd
JB
4951 {
4952 Lisp_Object new_head, new_click;
4953 if (modifiers & triple_modifier)
4954 modifiers ^= (double_modifier | triple_modifier);
bc536d84
RS
4955 else if (modifiers & double_modifier)
4956 modifiers &= ~double_modifier;
4957 else if (modifiers & drag_modifier)
4958 modifiers &= ~drag_modifier;
559f9d04
RS
4959 else
4960 {
4961 /* Dispose of this `down' event by simply jumping
4962 back to replay_key, to get another event.
4963
4964 Note that if this event came from mock input,
4965 then just jumping back to replay_key will just
4966 hand it to us again. So we have to wipe out any
4967 mock input.
4968
4969 We could delete keybuf[t] and shift everything
4970 after that to the left by one spot, but we'd also
4971 have to fix up any variable that points into
4972 keybuf, and shifting isn't really necessary
4973 anyway.
4974
4975 Adding prefixes for non-textual mouse clicks
4976 creates two characters of mock input, and both
4977 must be thrown away. If we're only looking at
4978 the prefix now, we can just jump back to
4979 replay_key. On the other hand, if we've already
4980 processed the prefix, and now the actual click
4981 itself is giving us trouble, then we've lost the
4982 state of the keymaps we want to backtrack to, and
4983 we need to replay the whole sequence to rebuild
4984 it.
4985
4986 Beyond that, only function key expansion could
4987 create more than two keys, but that should never
4988 generate mouse events, so it's okay to zero
4989 mock_input in that case too.
4990
4991 Isn't this just the most wonderful code ever? */
4992 if (t == last_real_key_start)
4993 {
4994 mock_input = 0;
4995 goto replay_key;
4996 }
4997 else
4998 {
4999 mock_input = last_real_key_start;
5000 goto replay_sequence;
5001 }
5002 }
5003
27203ead
RS
5004 new_head
5005 = apply_modifiers (modifiers, XCONS (breakdown)->car);
5006 new_click
5007 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
fbcd35bd
JB
5008
5009 /* Look for a binding for this new key. follow_key
5010 promises that it didn't munge submaps the
5011 last time we called it, since key was unbound. */
27203ead
RS
5012 first_binding
5013 = (follow_key (new_click,
5014 nmaps - local_first_binding,
5015 submaps + local_first_binding,
5016 defs + local_first_binding,
4e50f26a 5017 submaps + local_first_binding)
27203ead 5018 + local_first_binding);
fbcd35bd
JB
5019
5020 /* If that click is bound, go for it. */
5021 if (first_binding < nmaps)
5022 {
5023 key = new_click;
5024 break;
5025 }
5026 /* Otherwise, we'll leave key set to the drag event. */
5027 }
0a7f1fc0
JB
5028 }
5029 }
5030 }
5031
284f4730 5032 keybuf[t++] = key;
7d6de002
RS
5033 /* Normally, last_nonmenu_event gets the previous key we read.
5034 But when a mouse popup menu is being used,
5035 we don't update last_nonmenu_event; it continues to hold the mouse
5036 event that preceded the first level of menu. */
5037 if (!used_mouse_menu)
5038 last_nonmenu_event = key;
284f4730
JB
5039
5040 /* If the sequence is unbound, see if we can hang a function key
253598e4
JB
5041 off the end of it. We only want to scan real keyboard input
5042 for function key sequences, so if mock_input says that we're
f4255cd1 5043 re-reading old events, don't examine it. */
4e50f26a 5044 if (first_binding >= nmaps
253598e4 5045 && t >= mock_input)
284f4730
JB
5046 {
5047 Lisp_Object fkey_next;
5048
e9bf89a0
RS
5049 /* Continue scan from fkey_end until we find a bound suffix.
5050 If we fail, increment fkey_start
5051 and start fkey_end from there. */
284f4730
JB
5052 while (fkey_end < t)
5053 {
f4255cd1
JB
5054 Lisp_Object key;
5055
5056 key = keybuf[fkey_end++];
067ffa38
JB
5057 /* Look up meta-characters by prefixing them
5058 with meta_prefix_char. I hate this. */
8c18cbfb 5059 if (INTEGERP (key) && XINT (key) & meta_modifier)
f4255cd1 5060 {
e74fbc70
RS
5061 fkey_next
5062 = get_keymap_1
f4255cd1 5063 (get_keyelt
e74fbc70 5064 (access_keymap (fkey_map, meta_prefix_char, 1, 0)),
f4255cd1 5065 0, 1);
bb9e9bed 5066 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
f4255cd1 5067 }
067ffa38
JB
5068 else
5069 fkey_next = fkey_map;
5070
e74fbc70
RS
5071 fkey_next
5072 = get_keyelt (access_keymap (fkey_next, key, 1, 0));
067ffa38 5073
7a80a6f6
RS
5074#if 0 /* I didn't turn this on, because it might cause trouble
5075 for the mapping of return into C-m and tab into C-i. */
5076 /* Optionally don't map function keys into other things.
5077 This enables the user to redefine kp- keys easily. */
5078 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
5079 fkey_next = Qnil;
5080#endif
5081
1abe6abe
RS
5082 /* If the function key map gives a function, not an
5083 array, then call the function with no args and use
5084 its value instead. */
5085 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
5086 && fkey_end == t)
5087 {
5088 struct gcpro gcpro1, gcpro2, gcpro3;
5089 Lisp_Object tem;
5090 tem = fkey_next;
5091
5092 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
84d91fda 5093 fkey_next = call1 (fkey_next, prompt);
1abe6abe
RS
5094 UNGCPRO;
5095 /* If the function returned something invalid,
5096 barf--don't ignore it.
5097 (To ignore it safely, we would need to gcpro a bunch of
5098 other variables.) */
5099 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
5100 error ("Function in function-key-map returns invalid key sequence");
5101 }
5102
e9bf89a0
RS
5103 function_key_possible = ! NILP (fkey_next);
5104
85bc5181 5105 /* If keybuf[fkey_start..fkey_end] is bound in the
a764a753 5106 function key map and it's a suffix of the current
85bc5181 5107 sequence (i.e. fkey_end == t), replace it with
a764a753 5108 the binding and restart with fkey_start at the end. */
f5ea6163 5109 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
284f4730
JB
5110 && fkey_end == t)
5111 {
2e864a76 5112 int len = XFASTINT (Flength (fkey_next));
f5ea6163
JB
5113
5114 t = fkey_start + len;
284f4730
JB
5115 if (t >= bufsize)
5116 error ("key sequence too long");
5117
f5ea6163
JB
5118 if (VECTORP (fkey_next))
5119 bcopy (XVECTOR (fkey_next)->contents,
5120 keybuf + fkey_start,
5121 (t - fkey_start) * sizeof (keybuf[0]));
5122 else if (STRINGP (fkey_next))
5123 {
5124 int i;
5125
5126 for (i = 0; i < len; i++)
bb9e9bed
KH
5127 XSETFASTINT (keybuf[fkey_start + i],
5128 XSTRING (fkey_next)->data[i]);
f5ea6163 5129 }
284f4730
JB
5130
5131 mock_input = t;
5132 fkey_start = fkey_end = t;
32e6d806 5133 fkey_map = Vfunction_key_map;
284f4730 5134
00a78037
RS
5135 /* Do pass the results through key-translation-map. */
5136 keytran_start = keytran_end = 0;
5137 keytran_map = Vkey_translation_map;
5138
f4255cd1 5139 goto replay_sequence;
284f4730
JB
5140 }
5141
f4255cd1 5142 fkey_map = get_keymap_1 (fkey_next, 0, 1);
284f4730 5143
a764a753
JB
5144 /* If we no longer have a bound suffix, try a new positions for
5145 fkey_start. */
284f4730
JB
5146 if (NILP (fkey_map))
5147 {
5148 fkey_end = ++fkey_start;
5149 fkey_map = Vfunction_key_map;
e9bf89a0 5150 function_key_possible = 0;
284f4730
JB
5151 }
5152 }
5153 }
a612e298
RS
5154
5155 /* Look for this sequence in key-translation-map. */
5156 {
5157 Lisp_Object keytran_next;
5158
5159 /* Scan from keytran_end until we find a bound suffix. */
5160 while (keytran_end < t)
5161 {
5162 Lisp_Object key;
5163
5164 key = keybuf[keytran_end++];
5165 /* Look up meta-characters by prefixing them
5166 with meta_prefix_char. I hate this. */
8c18cbfb 5167 if (INTEGERP (key) && XINT (key) & meta_modifier)
a612e298
RS
5168 {
5169 keytran_next
5170 = get_keymap_1
5171 (get_keyelt
5172 (access_keymap (keytran_map, meta_prefix_char, 1, 0)),
5173 0, 1);
bb9e9bed 5174 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
a612e298
RS
5175 }
5176 else
5177 keytran_next = keytran_map;
5178
5179 keytran_next
5180 = get_keyelt (access_keymap (keytran_next, key, 1, 0));
5181
1abe6abe
RS
5182 /* If the key translation map gives a function, not an
5183 array, then call the function with no args and use
5184 its value instead. */
5185 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
5186 && keytran_end == t)
5187 {
5188 struct gcpro gcpro1, gcpro2, gcpro3;
5189 Lisp_Object tem;
5190 tem = keytran_next;
5191
40932d1a 5192 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
84d91fda 5193 keytran_next = call1 (keytran_next, prompt);
1abe6abe
RS
5194 UNGCPRO;
5195 /* If the function returned something invalid,
5196 barf--don't ignore it.
5197 (To ignore it safely, we would need to gcpro a bunch of
5198 other variables.) */
5199 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
40932d1a 5200 error ("Function in key-translation-map returns invalid key sequence");
1abe6abe
RS
5201 }
5202
00a78037
RS
5203 key_translation_possible = ! NILP (keytran_next);
5204
a612e298 5205 /* If keybuf[keytran_start..keytran_end] is bound in the
1abe6abe 5206 key translation map and it's a suffix of the current
a612e298
RS
5207 sequence (i.e. keytran_end == t), replace it with
5208 the binding and restart with keytran_start at the end. */
5209 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
5210 && keytran_end == t)
5211 {
2e864a76 5212 int len = XFASTINT (Flength (keytran_next));
a612e298
RS
5213
5214 t = keytran_start + len;
5215 if (t >= bufsize)
5216 error ("key sequence too long");
5217
5218 if (VECTORP (keytran_next))
5219 bcopy (XVECTOR (keytran_next)->contents,
5220 keybuf + keytran_start,
5221 (t - keytran_start) * sizeof (keybuf[0]));
5222 else if (STRINGP (keytran_next))
5223 {
5224 int i;
5225
5226 for (i = 0; i < len; i++)
bb9e9bed
KH
5227 XSETFASTINT (keybuf[keytran_start + i],
5228 XSTRING (keytran_next)->data[i]);
a612e298
RS
5229 }
5230
5231 mock_input = t;
5232 keytran_start = keytran_end = t;
5233 keytran_map = Vkey_translation_map;
5234
00a78037
RS
5235 /* Don't pass the results of key-translation-map
5236 through function-key-map. */
5237 fkey_start = fkey_end = t;
5238 fkey_map = Vkey_translation_map;
5239
a612e298
RS
5240 goto replay_sequence;
5241 }
5242
5243 keytran_map = get_keymap_1 (keytran_next, 0, 1);
5244
5245 /* If we no longer have a bound suffix, try a new positions for
5246 keytran_start. */
5247 if (NILP (keytran_map))
5248 {
5249 keytran_end = ++keytran_start;
5250 keytran_map = Vkey_translation_map;
00a78037 5251 key_translation_possible = 0;
a612e298
RS
5252 }
5253 }
5254 }
4e50f26a
RS
5255
5256 /* If KEY is not defined in any of the keymaps,
5257 and cannot be part of a function key or translation,
5258 and is an upper case letter
5259 use the corresponding lower-case letter instead. */
5260 if (first_binding == nmaps && ! function_key_possible
00a78037 5261 && ! key_translation_possible
8c18cbfb 5262 && INTEGERP (key)
4e50f26a
RS
5263 && ((((XINT (key) & 0x3ffff)
5264 < XSTRING (current_buffer->downcase_table)->size)
5265 && UPPERCASEP (XINT (key) & 0x3ffff))
5266 || (XINT (key) & shift_modifier)))
5267 {
309b0fc8
RS
5268 original_uppercase = key;
5269 original_uppercase_position = t - 1;
5270
4e50f26a
RS
5271 if (XINT (key) & shift_modifier)
5272 XSETINT (key, XINT (key) & ~shift_modifier);
5273 else
5274 XSETINT (key, (DOWNCASE (XINT (key) & 0x3ffff)
5275 | (XINT (key) & ~0x3ffff)));
5276
5277 keybuf[t - 1] = key;
5278 mock_input = t;
5279 goto replay_sequence;
5280 }
ef8fd672
RS
5281 /* If KEY is not defined in any of the keymaps,
5282 and cannot be part of a function key or translation,
5283 and is a shifted function key,
5284 use the corresponding unshifted function key instead. */
5285 if (first_binding == nmaps && ! function_key_possible
5286 && ! key_translation_possible
5287 && SYMBOLP (key))
5288 {
5289 Lisp_Object breakdown;
5290 int modifiers;
5291
309b0fc8
RS
5292 original_uppercase = key;
5293 original_uppercase_position = t - 1;
5294
ef8fd672
RS
5295 breakdown = parse_modifiers (key);
5296 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
5297 if (modifiers & shift_modifier)
5298 {
5299 modifiers &= ~shift_modifier;
5300 key = apply_modifiers (make_number (modifiers),
5301 XCONS (breakdown)->car);
5302
5303 keybuf[t - 1] = key;
5304 mock_input = t;
5305 goto replay_sequence;
5306 }
5307 }
284f4730
JB
5308 }
5309
309b0fc8 5310 if (!dummyflag)
bc536d84
RS
5311 read_key_sequence_cmd = (first_binding < nmaps
5312 ? defs[first_binding]
5313 : Qnil);
284f4730 5314
cd21b839 5315 unread_switch_frame = delayed_switch_frame;
f4255cd1 5316 unbind_to (count, Qnil);
07f76a14 5317
309b0fc8
RS
5318 if (dont_downcase_last && t - 1 == original_uppercase_position)
5319 keybuf[t - 1] = original_uppercase;
5320
07f76a14
JB
5321 /* Occasionally we fabricate events, perhaps by expanding something
5322 according to function-key-map, or by adding a prefix symbol to a
5323 mouse click in the scroll bar or modeline. In this cases, return
5324 the entire generated key sequence, even if we hit an unbound
5325 prefix or a definition before the end. This means that you will
5326 be able to push back the event properly, and also means that
5327 read-key-sequence will always return a logical unit.
5328
5329 Better ideas? */
cca310da
JB
5330 for (; t < mock_input; t++)
5331 {
a98ea3f9
RS
5332 if (echo_keystrokes)
5333 echo_char (keybuf[t]);
cca310da
JB
5334 add_command_key (keybuf[t]);
5335 }
07f76a14 5336
284f4730
JB
5337 return t;
5338}
5339
a612e298
RS
5340#if 0 /* This doc string is too long for some compilers.
5341 This commented-out definition serves for DOC. */
c0a58692 5342DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 2, 0,
284f4730
JB
5343 "Read a sequence of keystrokes and return as a string or vector.\n\
5344The sequence is sufficient to specify a non-prefix command in the\n\
5345current local and global maps.\n\
5346\n\
c0a58692
RS
5347First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
5348Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
5349as a continuation of the previous key.\n\
284f4730 5350\n\
309b0fc8
RS
5351The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
5352convert the last event to lower case. (Normally any upper case event\n\
5353is converted to lower case if the original event is undefined and the lower\n\
5354case equivalent is defined.) A non-nil value is appropriate for reading\n\
5355a key sequence to be defined.\n\
5356\n\
cb5df6ae
JB
5357A C-g typed while in this function is treated like any other character,\n\
5358and `quit-flag' is not set.\n\
5359\n\
5360If the key sequence starts with a mouse click, then the sequence is read\n\
5361using the keymaps of the buffer of the window clicked in, not the buffer\n\
5362of the selected window as normal.\n\
ede41463 5363""\n\
cb5df6ae
JB
5364`read-key-sequence' drops unbound button-down events, since you normally\n\
5365only care about the click or drag events which follow them. If a drag\n\
fbcd35bd
JB
5366or multi-click event is unbound, but the corresponding click event would\n\
5367be bound, `read-key-sequence' turns the event into a click event at the\n\
cb5df6ae 5368drag's starting position. This means that you don't have to distinguish\n\
fbcd35bd 5369between click and drag, double, or triple events unless you want to.\n\
cb5df6ae
JB
5370\n\
5371`read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
3c370943
JB
5372lines separating windows, and scroll bars with imaginary keys\n\
5373`mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
cb5df6ae
JB
5374\n\
5375If the user switches frames in the middle of a key sequence, the\n\
5376frame-switch event is put off until after the current key sequence.\n\
5377\n\
5378`read-key-sequence' checks `function-key-map' for function key\n\
5379sequences, where they wouldn't conflict with ordinary bindings. See\n\
4bb994d1 5380`function-key-map' for more details.")
a612e298
RS
5381 (prompt, continue_echo)
5382#endif
5383
309b0fc8 5384DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 3, 0,
a612e298 5385 0)
309b0fc8
RS
5386 (prompt, continue_echo, dont_downcase_last)
5387 Lisp_Object prompt, continue_echo, dont_downcase_last;
284f4730
JB
5388{
5389 Lisp_Object keybuf[30];
5390 register int i;
5391 struct gcpro gcpro1, gcpro2;
5392
5393 if (!NILP (prompt))
5394 CHECK_STRING (prompt, 0);
5395 QUIT;
5396
5397 bzero (keybuf, sizeof keybuf);
5398 GCPRO1 (keybuf[0]);
5399 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
5400
daa37602 5401 if (NILP (continue_echo))
c0a58692
RS
5402 this_command_key_count = 0;
5403
309b0fc8
RS
5404 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
5405 prompt, ! NILP (dont_downcase_last));
284f4730 5406
dcc408a0
RS
5407 if (i == -1)
5408 {
5409 Vquit_flag = Qt;
5410 QUIT;
5411 }
284f4730 5412 UNGCPRO;
86e5706b 5413 return make_event_array (i, keybuf);
284f4730
JB
5414}
5415\f
5416DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
5417 "Execute CMD as an editor command.\n\
5418CMD must be a symbol that satisfies the `commandp' predicate.\n\
5419Optional second arg RECORD-FLAG non-nil\n\
5420means unconditionally put this command in `command-history'.\n\
5421Otherwise, that is done only if an arg is read using the minibuffer.")
5422 (cmd, record)
5423 Lisp_Object cmd, record;
5424{
5425 register Lisp_Object final;
5426 register Lisp_Object tem;
5427 Lisp_Object prefixarg;
5428 struct backtrace backtrace;
5429 extern int debug_on_next_call;
5430
ff4b06d3
KH
5431 prefixarg = current_perdisplay->Vprefix_arg;
5432 current_perdisplay->Vprefix_arg = Qnil;
5433 current_perdisplay->Vcurrent_prefix_arg = prefixarg;
284f4730
JB
5434 debug_on_next_call = 0;
5435
8c18cbfb 5436 if (SYMBOLP (cmd))
284f4730
JB
5437 {
5438 tem = Fget (cmd, Qdisabled);
88ce066e 5439 if (!NILP (tem) && !NILP (Vrun_hooks))
2e894dab 5440 return call1 (Vrun_hooks, Qdisabled_command_hook);
284f4730
JB
5441 }
5442
5443 while (1)
5444 {
ffd56f97 5445 final = Findirect_function (cmd);
284f4730
JB
5446
5447 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
5448 do_autoload (final, cmd);
5449 else
5450 break;
5451 }
5452
8c18cbfb 5453 if (STRINGP (final) || VECTORP (final))
284f4730
JB
5454 {
5455 /* If requested, place the macro in the command history. For
5456 other sorts of commands, call-interactively takes care of
5457 this. */
5458 if (!NILP (record))
5459 Vcommand_history
5460 = Fcons (Fcons (Qexecute_kbd_macro,
5461 Fcons (final, Fcons (prefixarg, Qnil))),
5462 Vcommand_history);
5463
5464 return Fexecute_kbd_macro (final, prefixarg);
5465 }
8c18cbfb 5466 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
284f4730
JB
5467 {
5468 backtrace.next = backtrace_list;
5469 backtrace_list = &backtrace;
5470 backtrace.function = &Qcall_interactively;
5471 backtrace.args = &cmd;
5472 backtrace.nargs = 1;
5473 backtrace.evalargs = 0;
5474
5475 tem = Fcall_interactively (cmd, record);
5476
5477 backtrace_list = backtrace.next;
5478 return tem;
5479 }
5480 return Qnil;
5481}
5482\f
284f4730
JB
5483DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
5484 1, 1, "P",
5485 "Read function name, then read its arguments and call it.")
5486 (prefixarg)
5487 Lisp_Object prefixarg;
5488{
5489 Lisp_Object function;
5490 char buf[40];
5491 Lisp_Object saved_keys;
5492 struct gcpro gcpro1;
5493
b0f2a7bf
KH
5494 saved_keys = Fvector (this_command_key_count,
5495 XVECTOR (this_command_keys)->contents);
284f4730
JB
5496 buf[0] = 0;
5497 GCPRO1 (saved_keys);
5498
5499 if (EQ (prefixarg, Qminus))
5500 strcpy (buf, "- ");
5501 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
5502 strcpy (buf, "C-u ");
8c18cbfb 5503 else if (CONSP (prefixarg) && INTEGERP (XCONS (prefixarg)->car))
284f4730 5504 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
8c18cbfb 5505 else if (INTEGERP (prefixarg))
284f4730
JB
5506 sprintf (buf, "%d ", XINT (prefixarg));
5507
5508 /* This isn't strictly correct if execute-extended-command
5509 is bound to anything else. Perhaps it should use
5510 this_command_keys? */
5511 strcat (buf, "M-x ");
5512
5513 /* Prompt with buf, and then read a string, completing from and
5514 restricting to the set of all defined commands. Don't provide
51763820 5515 any initial input. Save the command read on the extended-command
03b4122a 5516 history list. */
284f4730
JB
5517 function = Fcompleting_read (build_string (buf),
5518 Vobarray, Qcommandp,
03b4122a 5519 Qt, Qnil, Qextended_command_history);
284f4730 5520
1113d9db
JB
5521 /* Set this_command_keys to the concatenation of saved_keys and
5522 function, followed by a RET. */
284f4730 5523 {
1113d9db 5524 struct Lisp_String *str;
b0f2a7bf 5525 Lisp_Object *keys;
284f4730
JB
5526 int i;
5527 Lisp_Object tem;
5528
1113d9db
JB
5529 this_command_key_count = 0;
5530
b0f2a7bf
KH
5531 keys = XVECTOR (saved_keys)->contents;
5532 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
5533 add_command_key (keys[i]);
1113d9db
JB
5534
5535 str = XSTRING (function);
5536 for (i = 0; i < str->size; i++)
5537 {
bb9e9bed 5538 XSETFASTINT (tem, str->data[i]);
1113d9db
JB
5539 add_command_key (tem);
5540 }
5541
bb9e9bed 5542 XSETFASTINT (tem, '\015');
1113d9db 5543 add_command_key (tem);
284f4730
JB
5544 }
5545
5546 UNGCPRO;
5547
0a7f1fc0 5548 function = Fintern (function, Qnil);
ff4b06d3 5549 current_perdisplay->Vprefix_arg = prefixarg;
284f4730
JB
5550 this_command = function;
5551
5552 return Fcommand_execute (function, Qt);
5553}
5554\f
5555
5556detect_input_pending ()
5557{
5558 if (!input_pending)
5559 get_input_pending (&input_pending);
5560
5561 return input_pending;
5562}
5563
ffd56f97
JB
5564/* This is called in some cases before a possible quit.
5565 It cases the next call to detect_input_pending to recompute input_pending.
5566 So calling this function unnecessarily can't do any harm. */
5567clear_input_pending ()
5568{
5569 input_pending = 0;
5570}
5571
284f4730
JB
5572DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
5573 "T if command input is currently available with no waiting.\n\
5574Actually, the value is nil only if we can be sure that no input is available.")
5575 ()
5576{
24597608 5577 if (!NILP (Vunread_command_events) || unread_command_char != -1)
284f4730
JB
5578 return (Qt);
5579
5580 return detect_input_pending () ? Qt : Qnil;
5581}
5582
5583DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
22d7cb89 5584 "Return vector of last 100 events, not counting those from keyboard macros.")
284f4730
JB
5585 ()
5586{
5160df46 5587 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
284f4730
JB
5588 Lisp_Object val;
5589
5590 if (total_keys < NUM_RECENT_KEYS)
5160df46 5591 return Fvector (total_keys, keys);
284f4730
JB
5592 else
5593 {
5160df46
JB
5594 val = Fvector (NUM_RECENT_KEYS, keys);
5595 bcopy (keys + recent_keys_index,
284f4730
JB
5596 XVECTOR (val)->contents,
5597 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
5160df46 5598 bcopy (keys,
284f4730
JB
5599 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
5600 recent_keys_index * sizeof (Lisp_Object));
5601 return val;
5602 }
5603}
5604
5605DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
e5f920d7
RS
5606 "Return the key sequence that invoked this command.\n\
5607The value is a string or a vector.")
284f4730
JB
5608 ()
5609{
86e5706b
RS
5610 return make_event_array (this_command_key_count,
5611 XVECTOR (this_command_keys)->contents);
284f4730
JB
5612}
5613
5614DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
5615 "Return the current depth in recursive edits.")
5616 ()
5617{
5618 Lisp_Object temp;
bb9e9bed 5619 XSETFASTINT (temp, command_loop_level + minibuf_level);
284f4730
JB
5620 return temp;
5621}
5622
5623DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
5624 "FOpen dribble file: ",
9b2471df
RS
5625 "Start writing all keyboard characters to a dribble file called FILE.\n\
5626If FILE is nil, close any open dribble file.")
284f4730
JB
5627 (file)
5628 Lisp_Object file;
5629{
5630 if (NILP (file))
5631 {
d0011c11
RS
5632 if (dribble)
5633 {
5634 fclose (dribble);
5635 dribble = 0;
5636 }
284f4730
JB
5637 }
5638 else
5639 {
5640 file = Fexpand_file_name (file, Qnil);
5641 dribble = fopen (XSTRING (file)->data, "w");
5642 }
5643 return Qnil;
5644}
5645
5646DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
5647 "Discard the contents of the terminal input buffer.\n\
5648Also cancel any kbd macro being defined.")
5649 ()
5650{
5651 defining_kbd_macro = 0;
5652 update_mode_lines++;
5653
24597608 5654 Vunread_command_events = Qnil;
86e5706b 5655 unread_command_char = -1;
284f4730
JB
5656
5657 discard_tty_input ();
5658
ff0b5f4c
JB
5659 /* Without the cast, GCC complains that this assignment loses the
5660 volatile qualifier of kbd_store_ptr. Is there anything wrong
5661 with that? */
6c6083a9
KH
5662 current_perdisplay->kbd_fetch_ptr
5663 = (struct input_event *) current_perdisplay->kbd_store_ptr;
5664 Ffillarray (current_perdisplay->kbd_buffer_frame_or_window, Qnil);
284f4730
JB
5665 input_pending = 0;
5666
5667 return Qnil;
5668}
5669\f
5670DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
5671 "Stop Emacs and return to superior process. You can resume later.\n\
8026024c
KH
5672If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
5673control, run a subshell instead.\n\n\
284f4730 5674If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
b7d2ebbf
RS
5675to be read as terminal input by Emacs's parent, after suspension.\n\
5676\n\
bbdc2092
RS
5677Before suspending, run the normal hook `suspend-hook'.\n\
5678After resumption run the normal hook `suspend-resume-hook'.\n\
284f4730
JB
5679\n\
5680Some operating systems cannot stop the Emacs process and resume it later.\n\
b7d2ebbf 5681On such systems, Emacs starts a subshell instead of suspending.")
284f4730
JB
5682 (stuffstring)
5683 Lisp_Object stuffstring;
5684{
3a69360c 5685 Lisp_Object tem;
284f4730
JB
5686 int count = specpdl_ptr - specpdl;
5687 int old_height, old_width;
5688 int width, height;
b7d2ebbf 5689 struct gcpro gcpro1, gcpro2;
284f4730
JB
5690 extern init_sys_modes ();
5691
5692 if (!NILP (stuffstring))
5693 CHECK_STRING (stuffstring, 0);
284f4730 5694
1e95ed28
JB
5695 /* Run the functions in suspend-hook. */
5696 if (!NILP (Vrun_hooks))
5697 call1 (Vrun_hooks, intern ("suspend-hook"));
284f4730 5698
b7d2ebbf 5699 GCPRO1 (stuffstring);
ff11dfa1 5700 get_frame_size (&old_width, &old_height);
284f4730
JB
5701 reset_sys_modes ();
5702 /* sys_suspend can get an error if it tries to fork a subshell
5703 and the system resources aren't available for that. */
5704 record_unwind_protect (init_sys_modes, 0);
5705 stuff_buffered_input (stuffstring);
8026024c
KH
5706 if (cannot_suspend)
5707 sys_subshell ();
5708 else
5709 sys_suspend ();
284f4730
JB
5710 unbind_to (count, Qnil);
5711
5712 /* Check if terminal/window size has changed.
5713 Note that this is not useful when we are running directly
5714 with a window system; but suspend should be disabled in that case. */
ff11dfa1 5715 get_frame_size (&width, &height);
284f4730 5716 if (width != old_width || height != old_height)
f5ea6163 5717 change_frame_size (selected_frame, height, width, 0, 0);
284f4730 5718
1e95ed28 5719 /* Run suspend-resume-hook. */
284f4730
JB
5720 if (!NILP (Vrun_hooks))
5721 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
5722
5723 UNGCPRO;
5724 return Qnil;
5725}
5726
5727/* If STUFFSTRING is a string, stuff its contents as pending terminal input.
eb8c3be9 5728 Then in any case stuff anything Emacs has read ahead and not used. */
284f4730
JB
5729
5730stuff_buffered_input (stuffstring)
5731 Lisp_Object stuffstring;
5732{
284f4730
JB
5733/* stuff_char works only in BSD, versions 4.2 and up. */
5734#ifdef BSD
5735#ifndef BSD4_1
612b78ef 5736 register unsigned char *p;
6c6083a9 5737 PERDISPLAY *perd;
612b78ef 5738
8c18cbfb 5739 if (STRINGP (stuffstring))
284f4730
JB
5740 {
5741 register int count;
5742
5743 p = XSTRING (stuffstring)->data;
5744 count = XSTRING (stuffstring)->size;
5745 while (count-- > 0)
5746 stuff_char (*p++);
5747 stuff_char ('\n');
5748 }
5749 /* Anything we have read ahead, put back for the shell to read. */
6c6083a9
KH
5750#ifndef MULTI_PERDISPLAY
5751 perd = &the_only_perdisplay;
5752#else
5753 /* ?? What should this do when we have multiple keyboards?? */
5754 perd = current_perdisplay;
5755 if (!perd)
5756 return;
5757#endif
612b78ef 5758 while (perd->kbd_fetch_ptr != perd->kbd_store_ptr)
284f4730 5759 {
612b78ef
KH
5760 if (perd->kbd_fetch_ptr == perd->kbd_buffer + KBD_BUFFER_SIZE)
5761 perd->kbd_fetch_ptr = perd->kbd_buffer;
5762 if (perd->kbd_fetch_ptr->kind == ascii_keystroke)
5763 stuff_char (perd->kbd_fetch_ptr->code);
5764 perd->kbd_fetch_ptr->kind = no_event;
ba72822c
KH
5765 (XVECTOR (perd->kbd_buffer_frame_or_window)->contents[perd->kbd_fetch_ptr
5766 - perd->kbd_buffer]
7b4aedb9 5767 = Qnil);
612b78ef 5768 perd->kbd_fetch_ptr++;
284f4730
JB
5769 }
5770 input_pending = 0;
5771#endif
5772#endif /* BSD and not BSD4_1 */
5773}
5774\f
ffd56f97
JB
5775set_waiting_for_input (time_to_clear)
5776 EMACS_TIME *time_to_clear;
284f4730 5777{
ffd56f97 5778 input_available_clear_time = time_to_clear;
284f4730
JB
5779
5780 /* Tell interrupt_signal to throw back to read_char, */
5781 waiting_for_input = 1;
5782
5783 /* If interrupt_signal was called before and buffered a C-g,
5784 make it run again now, to avoid timing error. */
5785 if (!NILP (Vquit_flag))
5786 quit_throw_to_read_char ();
284f4730
JB
5787}
5788
5789clear_waiting_for_input ()
5790{
5791 /* Tell interrupt_signal not to throw back to read_char, */
5792 waiting_for_input = 0;
ffd56f97 5793 input_available_clear_time = 0;
284f4730
JB
5794}
5795
5796/* This routine is called at interrupt level in response to C-G.
5797 If interrupt_input, this is the handler for SIGINT.
5798 Otherwise, it is called from kbd_buffer_store_event,
5799 in handling SIGIO or SIGTINT.
5800
5801 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
5802 immediately throw back to read_char.
5803
5804 Otherwise it sets the Lisp variable quit-flag not-nil.
5805 This causes eval to throw, when it gets a chance.
5806 If quit-flag is already non-nil, it stops the job right away. */
5807
5808SIGTYPE
5809interrupt_signal ()
5810{
5811 char c;
5812 /* Must preserve main program's value of errno. */
5813 int old_errno = errno;
284f4730
JB
5814
5815#ifdef USG
7a80a6f6
RS
5816 if (!read_socket_hook && NILP (Vwindow_system))
5817 {
5818 /* USG systems forget handlers when they are used;
5819 must reestablish each time */
5820 signal (SIGINT, interrupt_signal);
5821 signal (SIGQUIT, interrupt_signal);
5822 }
284f4730
JB
5823#endif /* USG */
5824
5825 cancel_echoing ();
5826
d5045cf9 5827 if (!NILP (Vquit_flag) && FRAME_TERMCAP_P (selected_frame))
284f4730
JB
5828 {
5829 fflush (stdout);
5830 reset_sys_modes ();
5831 sigfree ();
5832#ifdef SIGTSTP /* Support possible in later USG versions */
5833/*
5834 * On systems which can suspend the current process and return to the original
5835 * shell, this command causes the user to end up back at the shell.
5836 * The "Auto-save" and "Abort" questions are not asked until
5837 * the user elects to return to emacs, at which point he can save the current
5838 * job and either dump core or continue.
5839 */
5840 sys_suspend ();
5841#else
5842#ifdef VMS
5843 if (sys_suspend () == -1)
5844 {
5845 printf ("Not running as a subprocess;\n");
5846 printf ("you can continue or abort.\n");
5847 }
5848#else /* not VMS */
5849 /* Perhaps should really fork an inferior shell?
5850 But that would not provide any way to get back
5851 to the original shell, ever. */
5852 printf ("No support for stopping a process on this operating system;\n");
5853 printf ("you can continue or abort.\n");
5854#endif /* not VMS */
5855#endif /* not SIGTSTP */
80e4aa30
RS
5856#ifdef MSDOS
5857 /* We must remain inside the screen area when the internal terminal
5858 is used. Note that [Enter] is not echoed by dos. */
5859 cursor_to (0, 0);
5860#endif
284f4730
JB
5861 printf ("Auto-save? (y or n) ");
5862 fflush (stdout);
5863 if (((c = getchar ()) & ~040) == 'Y')
9fd7d808
RS
5864 {
5865 Fdo_auto_save (Qt, Qnil);
80e4aa30
RS
5866#ifdef MSDOS
5867 printf ("\r\nAuto-save done");
5868#else /* not MSDOS */
9fd7d808 5869 printf ("Auto-save done\n");
80e4aa30 5870#endif /* not MSDOS */
9fd7d808 5871 }
284f4730 5872 while (c != '\n') c = getchar ();
80e4aa30
RS
5873#ifdef MSDOS
5874 printf ("\r\nAbort? (y or n) ");
5875#else /* not MSDOS */
284f4730
JB
5876#ifdef VMS
5877 printf ("Abort (and enter debugger)? (y or n) ");
5878#else /* not VMS */
5879 printf ("Abort (and dump core)? (y or n) ");
5880#endif /* not VMS */
80e4aa30 5881#endif /* not MSDOS */
284f4730
JB
5882 fflush (stdout);
5883 if (((c = getchar ()) & ~040) == 'Y')
5884 abort ();
5885 while (c != '\n') c = getchar ();
80e4aa30
RS
5886#ifdef MSDOS
5887 printf ("\r\nContinuing...\r\n");
5888#else /* not MSDOS */
284f4730 5889 printf ("Continuing...\n");
80e4aa30 5890#endif /* not MSDOS */
284f4730
JB
5891 fflush (stdout);
5892 init_sys_modes ();
5893 }
5894 else
5895 {
5896 /* If executing a function that wants to be interrupted out of
5897 and the user has not deferred quitting by binding `inhibit-quit'
5898 then quit right away. */
5899 if (immediate_quit && NILP (Vinhibit_quit))
5900 {
5901 immediate_quit = 0;
5902 sigfree ();
5903 Fsignal (Qquit, Qnil);
5904 }
5905 else
5906 /* Else request quit when it's safe */
5907 Vquit_flag = Qt;
5908 }
5909
5910 if (waiting_for_input && !echoing)
5911 quit_throw_to_read_char ();
5912
5913 errno = old_errno;
5914}
5915
5916/* Handle a C-g by making read_char return C-g. */
5917
5918quit_throw_to_read_char ()
5919{
5920 quit_error_check ();
5921 sigfree ();
5922 /* Prevent another signal from doing this before we finish. */
f76475ad 5923 clear_waiting_for_input ();
284f4730
JB
5924 input_pending = 0;
5925
24597608 5926 Vunread_command_events = Qnil;
86e5706b 5927 unread_command_char = -1;
284f4730 5928
e6b01c14
JB
5929#ifdef POLL_FOR_INPUT
5930 /* May be > 1 if in recursive minibuffer. */
5931 if (poll_suppress_count == 0)
5932 abort ();
5933#endif
0dfcc832 5934#ifdef MULTI_FRAME
40f239ee
KH
5935 {
5936 Lisp_Object frame;
5937
5938 if (!current_perdisplay)
5939 abort ();
5940 frame = current_perdisplay->internal_last_event_frame;
5941 if (FRAMEP (frame) && XFRAME (frame) != selected_frame)
5942 Fhandle_switch_frame (make_lispy_switch_frame (frame));
5943 }
0dfcc832 5944#endif
e6b01c14 5945
284f4730
JB
5946 _longjmp (getcjmp, 1);
5947}
5948\f
5949DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
5950 "Set mode of reading keyboard input.\n\
464f8898
RS
5951First arg INTERRUPT non-nil means use input interrupts;\n\
5952 nil means use CBREAK mode.\n\
5953Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
284f4730 5954 (no effect except in CBREAK mode).\n\
b04904fb
RS
5955Third arg META t means accept 8-bit input (for a Meta key).\n\
5956 META nil means ignore the top bit, on the assumption it is parity.\n\
5957 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
a8ee7ef9
RS
5958Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
5959See also `current-input-mode'.")
284f4730
JB
5960 (interrupt, flow, meta, quit)
5961 Lisp_Object interrupt, flow, meta, quit;
5962{
5963 if (!NILP (quit)
8c18cbfb 5964 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
34f04431
RS
5965 error ("set-input-mode: QUIT must be an ASCII character");
5966
5967#ifdef POLL_FOR_INPUT
5968 stop_polling ();
5969#endif
284f4730
JB
5970
5971 reset_sys_modes ();
5972#ifdef SIGIO
5973/* Note SIGIO has been undef'd if FIONREAD is missing. */
5974#ifdef NO_SOCK_SIGIO
5975 if (read_socket_hook)
5976 interrupt_input = 0; /* No interrupts if reading from a socket. */
5977 else
5978#endif /* NO_SOCK_SIGIO */
5979 interrupt_input = !NILP (interrupt);
5980#else /* not SIGIO */
5981 interrupt_input = 0;
5982#endif /* not SIGIO */
5983/* Our VMS input only works by interrupts, as of now. */
5984#ifdef VMS
5985 interrupt_input = 1;
5986#endif
5987 flow_control = !NILP (flow);
b04904fb
RS
5988 if (NILP (meta))
5989 meta_key = 0;
5990 else if (EQ (meta, Qt))
5991 meta_key = 1;
5992 else
5993 meta_key = 2;
284f4730
JB
5994 if (!NILP (quit))
5995 /* Don't let this value be out of range. */
5996 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
5997
5998 init_sys_modes ();
34f04431
RS
5999
6000#ifdef POLL_FOR_INPUT
6001 poll_suppress_count = 1;
6002 start_polling ();
6003#endif
284f4730
JB
6004 return Qnil;
6005}
80645119
JB
6006
6007DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
6008 "Return information about the way Emacs currently reads keyboard input.\n\
6009The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
6010 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
6011 nil, Emacs is using CBREAK mode.\n\
6012 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
6013 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
a8ee7ef9
RS
6014 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
6015 META nil means ignoring the top bit, on the assumption it is parity.\n\
6016 META is neither t nor nil if accepting 8-bit input and using\n\
6017 all 8 bits as the character code.\n\
80645119
JB
6018 QUIT is the character Emacs currently uses to quit.\n\
6019The elements of this list correspond to the arguments of\n\
a8ee7ef9 6020`set-input-mode'.")
80645119
JB
6021 ()
6022{
6023 Lisp_Object val[4];
6024
6025 val[0] = interrupt_input ? Qt : Qnil;
6026 val[1] = flow_control ? Qt : Qnil;
a8ee7ef9 6027 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
bb9e9bed 6028 XSETFASTINT (val[3], quit_char);
80645119 6029
bf673a7a 6030 return Flist (sizeof (val) / sizeof (val[0]), val);
80645119
JB
6031}
6032
284f4730 6033\f
6c6083a9
KH
6034/*
6035 * Set up a perdisplay object with reasonable initial values.
6036 */
6037void
6038init_perdisplay (perd)
6039 PERDISPLAY *perd;
6040{
6041 perd->Vprefix_arg = Qnil;
6042 perd->Vcurrent_prefix_arg = Qnil;
6043 perd->kbd_buffer
6044 = (struct input_event *)xmalloc (KBD_BUFFER_SIZE
6045 * sizeof (struct input_event));
6046 perd->kbd_fetch_ptr = perd->kbd_buffer;
6047 perd->kbd_store_ptr = perd->kbd_buffer;
6048 perd->kbd_buffer_frame_or_window
6049 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
40f239ee
KH
6050#ifdef MULTI_FRAME
6051 /* This means that command_loop_1 won't try to select anything the first
6052 time through. */
6053 perd->internal_last_event_frame = Qnil;
6054#endif
6055 perd->Vlast_event_frame = Qnil;
267de7a1
KH
6056 perd->immediate_echo = 0;
6057 perd->echoptr = perd->echobuf;
6058 perd->echo_after_prompt = -1;
6c6083a9
KH
6059}
6060
6061/*
6062 * Destroy the contents of a perdisplay object, but not the object itself.
6063 * We use this just before deleteing it, or if we're going to initialize
6064 * it a second time.
6065 */
6066void
6067wipe_perdisplay (perd)
6068 PERDISPLAY *perd;
6069{
6070 xfree (perd->kbd_buffer);
6071}
6072
284f4730
JB
6073init_keyboard ()
6074{
284f4730
JB
6075 /* This is correct before outermost invocation of the editor loop */
6076 command_loop_level = -1;
6077 immediate_quit = 0;
6078 quit_char = Ctl ('g');
24597608 6079 Vunread_command_events = Qnil;
86e5706b 6080 unread_command_char = -1;
284f4730 6081 total_keys = 0;
9deb415a 6082 recent_keys_index = 0;
2eb6bfbe 6083#ifdef HAVE_MOUSE
a9d77f1f 6084 do_mouse_tracking = Qnil;
2eb6bfbe 6085#endif
284f4730
JB
6086 input_pending = 0;
6087
6c6083a9 6088#ifndef MULTI_PERDISPLAY
4bb994d1 6089 if (initialized)
6c6083a9
KH
6090 wipe_perdisplay (&the_only_perdisplay);
6091 init_perdisplay (&the_only_perdisplay);
6092#endif
07d2b8de 6093
7a80a6f6 6094 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
284f4730
JB
6095 {
6096 signal (SIGINT, interrupt_signal);
cb5df6ae 6097#if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
284f4730
JB
6098 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
6099 SIGQUIT and we can't tell which one it will give us. */
6100 signal (SIGQUIT, interrupt_signal);
6101#endif /* HAVE_TERMIO */
7a80a6f6 6102 }
284f4730
JB
6103/* Note SIGIO has been undef'd if FIONREAD is missing. */
6104#ifdef SIGIO
7a80a6f6
RS
6105 if (!noninteractive)
6106 signal (SIGIO, input_available_signal);
8ea0a720 6107#endif /* SIGIO */
284f4730
JB
6108
6109/* Use interrupt input by default, if it works and noninterrupt input
6110 has deficiencies. */
6111
6112#ifdef INTERRUPT_INPUT
6113 interrupt_input = 1;
6114#else
6115 interrupt_input = 0;
6116#endif
6117
6118/* Our VMS input only works by interrupts, as of now. */
6119#ifdef VMS
6120 interrupt_input = 1;
6121#endif
6122
6123 sigfree ();
6124 dribble = 0;
6125
6126 if (keyboard_init_hook)
6127 (*keyboard_init_hook) ();
6128
6129#ifdef POLL_FOR_INPUT
6130 poll_suppress_count = 1;
6131 start_polling ();
6132#endif
6133}
6134
6135/* This type's only use is in syms_of_keyboard, to initialize the
6136 event header symbols and put properties on them. */
6137struct event_head {
6138 Lisp_Object *var;
6139 char *name;
6140 Lisp_Object *kind;
6141};
6142
6143struct event_head head_table[] = {
7b4aedb9 6144 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
3c370943 6145 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
7b4aedb9 6146 &Qswitch_frame, "switch-frame", &Qswitch_frame,
bbdc2092 6147 &Qdelete_frame, "delete-frame", &Qdelete_frame,
af17bd2b
KH
6148 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
6149 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
284f4730
JB
6150};
6151
6152syms_of_keyboard ()
6153{
2e894dab
RS
6154 Qdisabled_command_hook = intern ("disabled-command-hook");
6155 staticpro (&Qdisabled_command_hook);
6156
284f4730
JB
6157 Qself_insert_command = intern ("self-insert-command");
6158 staticpro (&Qself_insert_command);
6159
6160 Qforward_char = intern ("forward-char");
6161 staticpro (&Qforward_char);
6162
6163 Qbackward_char = intern ("backward-char");
6164 staticpro (&Qbackward_char);
6165
6166 Qdisabled = intern ("disabled");
6167 staticpro (&Qdisabled);
6168
e58aa385
RS
6169 Qundefined = intern ("undefined");
6170 staticpro (&Qundefined);
6171
86e5706b
RS
6172 Qpre_command_hook = intern ("pre-command-hook");
6173 staticpro (&Qpre_command_hook);
6174
6175 Qpost_command_hook = intern ("post-command-hook");
6176 staticpro (&Qpost_command_hook);
6177
3ef14e46
RS
6178 Qdeferred_action_function = intern ("deferred-action-function");
6179 staticpro (&Qdeferred_action_function);
6180
40932d1a
RS
6181 Qcommand_hook_internal = intern ("command-hook-internal");
6182 staticpro (&Qcommand_hook_internal);
6183
284f4730
JB
6184 Qfunction_key = intern ("function-key");
6185 staticpro (&Qfunction_key);
13b5e56c 6186 Qmouse_click = intern ("mouse-click");
284f4730 6187 staticpro (&Qmouse_click);
284f4730 6188
598a9fa7
JB
6189 Qmenu_enable = intern ("menu-enable");
6190 staticpro (&Qmenu_enable);
6191
284f4730
JB
6192 Qmode_line = intern ("mode-line");
6193 staticpro (&Qmode_line);
e5d77022
JB
6194 Qvertical_line = intern ("vertical-line");
6195 staticpro (&Qvertical_line);
3c370943
JB
6196 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
6197 staticpro (&Qvertical_scroll_bar);
5ec75a55
RS
6198 Qmenu_bar = intern ("menu-bar");
6199 staticpro (&Qmenu_bar);
4bb994d1
JB
6200
6201 Qabove_handle = intern ("above-handle");
6202 staticpro (&Qabove_handle);
6203 Qhandle = intern ("handle");
6204 staticpro (&Qhandle);
6205 Qbelow_handle = intern ("below-handle");
6206 staticpro (&Qbelow_handle);
284f4730 6207
cd21b839 6208 Qevent_kind = intern ("event-kind");
284f4730 6209 staticpro (&Qevent_kind);
88cb0656
JB
6210 Qevent_symbol_elements = intern ("event-symbol-elements");
6211 staticpro (&Qevent_symbol_elements);
0a7f1fc0
JB
6212 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
6213 staticpro (&Qevent_symbol_element_mask);
6214 Qmodifier_cache = intern ("modifier-cache");
6215 staticpro (&Qmodifier_cache);
284f4730 6216
48e416d4
RS
6217 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
6218 staticpro (&Qrecompute_lucid_menubar);
6219 Qactivate_menubar_hook = intern ("activate-menubar-hook");
6220 staticpro (&Qactivate_menubar_hook);
6221
f4eef8b4
RS
6222 Qpolling_period = intern ("polling-period");
6223 staticpro (&Qpolling_period);
6224
284f4730
JB
6225 {
6226 struct event_head *p;
6227
6228 for (p = head_table;
6229 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
6230 p++)
6231 {
6232 *p->var = intern (p->name);
6233 staticpro (p->var);
6234 Fput (*p->var, Qevent_kind, *p->kind);
88cb0656 6235 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
284f4730
JB
6236 }
6237 }
6238
7b4aedb9
JB
6239 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
6240 staticpro (&button_down_location);
88cb0656
JB
6241
6242 {
6243 int i;
6244 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
6245
6246 modifier_symbols = Fmake_vector (make_number (len), Qnil);
6247 for (i = 0; i < len; i++)
86e5706b
RS
6248 if (modifier_names[i])
6249 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
88cb0656
JB
6250 staticpro (&modifier_symbols);
6251 }
6252
9deb415a
JB
6253 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
6254 staticpro (&recent_keys);
6255
6569cc8d 6256 this_command_keys = Fmake_vector (make_number (40), Qnil);
715d9345 6257 staticpro (&this_command_keys);
6569cc8d 6258
03b4122a
BF
6259 Qextended_command_history = intern ("extended-command-history");
6260 Fset (Qextended_command_history, Qnil);
6261 staticpro (&Qextended_command_history);
6262
24597608
RS
6263 accent_key_syms = Qnil;
6264 staticpro (&accent_key_syms);
6265
284f4730
JB
6266 func_key_syms = Qnil;
6267 staticpro (&func_key_syms);
6268
4e0e7d8e
RS
6269 system_key_syms = Qnil;
6270 staticpro (&system_key_syms);
6271
284f4730
JB
6272 mouse_syms = Qnil;
6273 staticpro (&mouse_syms);
6274
cd21b839
JB
6275 unread_switch_frame = Qnil;
6276 staticpro (&unread_switch_frame);
6277
284f4730
JB
6278 defsubr (&Sread_key_sequence);
6279 defsubr (&Srecursive_edit);
2eb6bfbe 6280#ifdef HAVE_MOUSE
284f4730 6281 defsubr (&Strack_mouse);
2eb6bfbe 6282#endif
284f4730
JB
6283 defsubr (&Sinput_pending_p);
6284 defsubr (&Scommand_execute);
6285 defsubr (&Srecent_keys);
6286 defsubr (&Sthis_command_keys);
6287 defsubr (&Ssuspend_emacs);
6288 defsubr (&Sabort_recursive_edit);
6289 defsubr (&Sexit_recursive_edit);
6290 defsubr (&Srecursion_depth);
6291 defsubr (&Stop_level);
6292 defsubr (&Sdiscard_input);
6293 defsubr (&Sopen_dribble_file);
6294 defsubr (&Sset_input_mode);
80645119 6295 defsubr (&Scurrent_input_mode);
284f4730
JB
6296 defsubr (&Sexecute_extended_command);
6297
284f4730 6298 DEFVAR_LISP ("last-command-char", &last_command_char,
86e5706b
RS
6299 "Last input event that was part of a command.");
6300
186cf719 6301 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
86e5706b 6302 "Last input event that was part of a command.");
284f4730 6303
7d6de002 6304 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
86e5706b 6305 "Last input event in a command, except for mouse menu events.\n\
7d6de002
RS
6306Mouse menus give back keys that don't look like mouse events;\n\
6307this variable holds the actual mouse event that led to the menu,\n\
6308so that you can determine whether the command was run by mouse or not.");
6309
284f4730 6310 DEFVAR_LISP ("last-input-char", &last_input_char,
86e5706b
RS
6311 "Last input event.");
6312
186cf719 6313 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
86e5706b 6314 "Last input event.");
284f4730 6315
24597608 6316 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
1c07d0a6 6317 "List of objects to be read as next command input events.");
284f4730 6318
86e5706b
RS
6319 DEFVAR_INT ("unread-command-char", &unread_command_char,
6320 "If not -1, an object to be read as next command input event.");
6321
284f4730
JB
6322 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
6323 "Meta-prefix character code. Meta-foo as command input\n\
6324turns into this character followed by foo.");
18cd2eeb 6325 XSETINT (meta_prefix_char, 033);
284f4730
JB
6326
6327 DEFVAR_LISP ("last-command", &last_command,
6328 "The last command executed. Normally a symbol with a function definition,\n\
6329but can be whatever was found in the keymap, or whatever the variable\n\
18f29056
RS
6330`this-command' was set to by that command.\n\
6331\n\
6332The value `mode-exit' is special; it means that the previous command\n\
6333read an event that told it to exit, and it did so and unread that event.\n\
6334In other words, the present command is the event that made the previous\n\
6335command exit.\n\
6336\n\
6337The value `kill-region' is special; it means that the previous command\n\
6338was a kill command.");
284f4730
JB
6339 last_command = Qnil;
6340
6341 DEFVAR_LISP ("this-command", &this_command,
6342 "The command now being executed.\n\
6343The command can set this variable; whatever is put here\n\
6344will be in `last-command' during the following command.");
6345 this_command = Qnil;
6346
6347 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
6348 "*Number of keyboard input characters between auto-saves.\n\
6349Zero means disable autosaving due to number of characters typed.");
6350 auto_save_interval = 300;
6351
6352 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
6353 "*Number of seconds idle time before auto-save.\n\
06ef7355
RS
6354Zero or nil means disable auto-saving due to idleness.\n\
6355After auto-saving due to this many seconds of idle time,\n\
84447c71 6356Emacs also does a garbage collection if that seems to be warranted.");
bb9e9bed 6357 XSETFASTINT (Vauto_save_timeout, 30);
284f4730
JB
6358
6359 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
6360 "*Nonzero means echo unfinished commands after this many seconds of pause.");
6361 echo_keystrokes = 1;
6362
6363 DEFVAR_INT ("polling-period", &polling_period,
6364 "*Interval between polling for input during Lisp execution.\n\
6365The reason for polling is to make C-g work to stop a running program.\n\
6366Polling is needed only when using X windows and SIGIO does not work.\n\
6367Polling is automatically disabled in all other cases.");
6368 polling_period = 2;
6369
564dc952 6370 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
fbcd35bd 6371 "*Maximum time between mouse clicks to make a double-click.\n\
564dc952
JB
6372Measured in milliseconds. nil means disable double-click recognition;\n\
6373t means double-clicks have no time limit and are detected\n\
fbcd35bd 6374by position only.");
aab06933 6375 Vdouble_click_time = make_number (500);
fbcd35bd 6376
03361bcc
RS
6377 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
6378 "*Non-nil means inhibit local map menu bar menus.");
6379 inhibit_local_menu_bar_menus = 0;
6380
284f4730 6381 DEFVAR_INT ("num-input-keys", &num_input_keys,
e0dff5f6 6382 "Number of complete keys read from the keyboard so far.");
284f4730
JB
6383 num_input_keys = 0;
6384
7e85b935 6385 DEFVAR_LISP ("help-char", &Vhelp_char,
284f4730
JB
6386 "Character to recognize as meaning Help.\n\
6387When it is read, do `(eval help-form)', and display result if it's a string.\n\
6388If the value of `help-form' is nil, this char can be read normally.");
18cd2eeb 6389 XSETINT (Vhelp_char, Ctl ('H'));
284f4730
JB
6390
6391 DEFVAR_LISP ("help-form", &Vhelp_form,
7e85b935 6392 "Form to execute when character `help-char' is read.\n\
284f4730
JB
6393If the form returns a string, that string is displayed.\n\
6394If `help-form' is nil, the help char is not recognized.");
6395 Vhelp_form = Qnil;
6396
7e85b935
RS
6397 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
6398 "Command to run when `help-char' character follows a prefix key.\n\
6399This command is used only when there is no actual binding\n\
6400for that character after that prefix key.");
6401 Vprefix_help_command = Qnil;
6402
284f4730
JB
6403 DEFVAR_LISP ("top-level", &Vtop_level,
6404 "Form to evaluate when Emacs starts up.\n\
6405Useful to set before you dump a modified Emacs.");
6406 Vtop_level = Qnil;
6407
6408 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
6409 "String used as translate table for keyboard input, or nil.\n\
6410Each character is looked up in this string and the contents used instead.\n\
6411If string is of length N, character codes N and up are untranslated.");
6412 Vkeyboard_translate_table = Qnil;
6413
a612e298
RS
6414 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
6415 "Keymap of key translations that can override keymaps.\n\
6416This keymap works like `function-key-map', but comes after that,\n\
6417and applies even for keys that have ordinary bindings.");
6418 Vkey_translation_map = Qnil;
6419
8026024c
KH
6420 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
6421 "Non-nil means to always spawn a subshell instead of suspending,\n\
6422even if the operating system has support for stopping a process.");
6423 cannot_suspend = 0;
6424
284f4730 6425 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
7d6de002 6426 "Non-nil means prompt with menus when appropriate.\n\
284f4730 6427This is done when reading from a keymap that has a prompt string,\n\
7d6de002
RS
6428for elements that have prompt strings.\n\
6429The menu is displayed on the screen\n\
6430if X menus were enabled at configuration\n\
6431time and the previous event was a mouse click prefix key.\n\
6432Otherwise, menu prompting uses the echo area.");
284f4730
JB
6433 menu_prompting = 1;
6434
6435 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
6436 "Character to see next line of menu prompt.\n\
6437Type this character while in a menu prompt to rotate around the lines of it.");
18cd2eeb 6438 XSETINT (menu_prompt_more_char, ' ');
9fa4395d
RS
6439
6440 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
6441 "A mask of additional modifier keys to use with every keyboard character.\n\
ad163903
JB
6442Emacs applies the modifiers of the character stored here to each keyboard\n\
6443character it reads. For example, after evaluating the expression\n\
9d9f56dd 6444 (setq extra-keyboard-modifiers ?\\C-x)\n\
80645119
JB
6445all input characters will have the control modifier applied to them.\n\
6446\n\
9d9f56dd 6447Note that the character ?\\C-@, equivalent to the integer zero, does\n\
80645119 6448not count as a control character; rather, it counts as a character\n\
27203ead 6449with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
80645119 6450cancels any modification.");
9fa4395d 6451 extra_keyboard_modifiers = 0;
86e5706b
RS
6452
6453 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
6454 "If an editing command sets this to t, deactivate the mark afterward.\n\
6455The command loop sets this to nil before each command,\n\
6456and tests the value when the command returns.\n\
6457Buffer modification stores t in this variable.");
6458 Vdeactivate_mark = Qnil;
6459
b0f2a7bf
KH
6460 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
6461 "Temporary storage of pre-command-hook or post-command-hook.");
6462 Vcommand_hook_internal = Qnil;
6463
86e5706b 6464 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
a1fd42c0
RS
6465 "Normal hook run before each command is executed.\n\
6466While the hook is run, its value is temporarily set to nil\n\
6467to avoid an unbreakable infinite loop if a hook function gets an error.\n\
6468As a result, a hook function cannot straightforwardly alter the value of\n\
6469`pre-command-hook'. See the Emacs Lisp manual for a way of\n\
6470implementing hook functions that alter the set of hook functions.");
86e5706b
RS
6471 Vpre_command_hook = Qnil;
6472
6473 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
a1fd42c0
RS
6474 "Normal hook run after each command is executed.\n\
6475While the hook is run, its value is temporarily set to nil\n\
6476to avoid an unbreakable infinite loop if a hook function gets an error.\n\
6477As a result, a hook function cannot straightforwardly alter the value of\n\
6478`post-command-hook'. See the Emacs Lisp manual for a way of\n\
6479implementing hook functions that alter the set of hook functions.");
86e5706b 6480 Vpost_command_hook = Qnil;
48e416d4
RS
6481
6482 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
6483 "t means menu bar, specified Lucid style, needs to be recomputed.");
6484 Vlucid_menu_bar_dirty_flag = Qnil;
a73c5e29 6485
9f9c0e27
RS
6486 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
6487 "List of menu bar items to move to the end of the menu bar.\n\
a612e298 6488The elements of the list are event types that may have menu bar bindings.");
9f9c0e27 6489 Vmenu_bar_final_items = Qnil;
e9bf89a0 6490
9dd3131c
RS
6491 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
6492 "Keymap that overrides all other local keymaps.\n\
6493If this variable is non-nil, it is used as a keymap instead of the\n\
6494buffer's local map, and the minor mode keymaps and text property keymaps.");
6495 Voverriding_local_map = Qnil;
6496
d0a49716
RS
6497 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
6498 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
6499Otherwise, the menu bar continues to reflect the buffer's local map\n\
6500and the minor mode maps regardless of `overriding-local-map'.");
6501 Voverriding_local_map_menu_flag = Qnil;
6502
2eb6bfbe 6503#ifdef HAVE_MOUSE
71edead1 6504 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
e9bf89a0 6505 "*Non-nil means generate motion events for mouse motion.");
2eb6bfbe 6506#endif
80e4aa30 6507
270a208f
RS
6508 DEFVAR_LISP ("system-key-alist", &Vsystem_key_alist,
6509 "Alist of system-specific X windows key symbols.\n\
80e4aa30 6510Each element should have the form (N . SYMBOL) where N is the\n\
270a208f 6511numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
80e4aa30 6512and SYMBOL is its name.");
a69a8ca2 6513 Vsystem_key_alist = Qnil;
8a792f3a
RS
6514
6515 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
6516 "List of deferred actions to be performed at a later time.\n\
6517The precise format isn't relevant here; we just check whether it is nil.");
6518 Vdeferred_action_list = Qnil;
6519
6520 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
6521 "Function to call to handle deferred actions, after each command.\n\
6522This function is called with no arguments after each command\n\
6523whenever `deferred-action-list' is non-nil.");
6524 Vdeferred_action_function = Qnil;
6c6083a9
KH
6525
6526 DEFVAR_DISPLAY ("prefix-arg", Vprefix_arg,
6527 "The value of the prefix argument for the next editing command.\n\
6528It may be a number, or the symbol `-' for just a minus sign as arg,\n\
6529or a list whose car is a number for just one or more C-U's\n\
6530or nil if no argument has been specified.\n\
6531\n\
6532You cannot examine this variable to find the argument for this command\n\
6533since it has been set to nil by the time you can look.\n\
6534Instead, you should use the variable `current-prefix-arg', although\n\
6535normally commands can get this prefix argument with (interactive \"P\").");
6536
6537 DEFVAR_DISPLAY ("current-prefix-arg", Vcurrent_prefix_arg,
6538 "The value of the prefix argument for this editing command.\n\
6539It may be a number, or the symbol `-' for just a minus sign as arg,\n\
6540or a list whose car is a number for just one or more C-U's\n\
6541or nil if no argument has been specified.\n\
6542This is what `(interactive \"P\")' returns.");
40f239ee
KH
6543
6544 DEFVAR_DISPLAY ("last-event-frame", Vlast_event_frame,
6545 "The frame in which the most recently read event occurred.\n\
6546If the last event came from a keyboard macro, this is set to `macro'.");
284f4730
JB
6547}
6548
6549keys_of_keyboard ()
6550{
6551 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
6552 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
6553 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
6554 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
6555 initial_define_key (meta_map, 'x', "execute-extended-command");
6556}