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