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