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