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