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