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