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