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