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