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