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