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