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