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