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