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