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