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