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