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