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