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