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