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