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