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