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