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