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