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