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