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