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