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