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