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