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