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