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