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