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