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