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