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