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