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