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