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