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