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