(open-line): Treat all cases of enabled undo alike.
[bpt/emacs.git] / src / keyboard.c
CommitLineData
284f4730 1/* Keyboard and mouse input; editor command loop.
86e5706b 2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993 Free Software Foundation, Inc.
284f4730
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
7b4aedb9 8the Free Software Foundation; either version 2, or (at your option)
284f4730
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20/* Allow config.h to undefine symbols found here. */
21#include <signal.h>
22
18160b98 23#include <config.h>
284f4730
JB
24#include <stdio.h>
25#undef NULL
26#include "termchar.h"
27#include "termopts.h"
28#include "lisp.h"
29#include "termhooks.h"
30#include "macros.h"
ff11dfa1 31#include "frame.h"
284f4730
JB
32#include "window.h"
33#include "commands.h"
34#include "buffer.h"
35#include "disptab.h"
f4255cd1 36#include "dispextern.h"
284f4730 37#include "keyboard.h"
497ba7a1 38#include "intervals.h"
9ac0d9e0 39#include "blockinput.h"
284f4730
JB
40#include <setjmp.h>
41#include <errno.h>
42
80e4aa30
RS
43#ifdef MSDOS
44#include "msdos.h"
45#include <time.h>
46#else /* not MSDOS */
284f4730
JB
47#ifndef VMS
48#include <sys/ioctl.h>
284f4730 49#endif
80e4aa30 50#endif /* not MSDOS */
284f4730 51
52baf19e 52#include "syssignal.h"
6ef5b54f 53#include "systty.h"
ffd56f97 54#include "systime.h"
52baf19e
JB
55
56extern int errno;
57
9ac0d9e0
JB
58/* Variables for blockinput.h: */
59
60/* Non-zero if interrupt input is blocked right now. */
63927c41 61int interrupt_input_blocked;
9ac0d9e0
JB
62
63/* Nonzero means an input interrupt has arrived
64 during the current critical section. */
63927c41 65int interrupt_input_pending;
9ac0d9e0
JB
66
67
284f4730
JB
68#ifdef HAVE_X_WINDOWS
69extern Lisp_Object Vmouse_grabbed;
70
71/* Make all keyboard buffers much bigger when using X windows. */
72#define KBD_BUFFER_SIZE 4096
73#else /* No X-windows, character input */
74#define KBD_BUFFER_SIZE 256
75#endif /* No X-windows */
76
77/* Following definition copied from eval.c */
78
79struct backtrace
80 {
81 struct backtrace *next;
82 Lisp_Object *function;
83 Lisp_Object *args; /* Points to vector of args. */
84 int nargs; /* length of vector. If nargs is UNEVALLED,
85 args points to slot holding list of
86 unevalled args */
87 char evalargs;
88 };
89
90/* Non-nil disable property on a command means
91 do not execute it; call disabled-command-hook's value instead. */
2e894dab 92Lisp_Object Qdisabled, Qdisabled_command_hook;
284f4730
JB
93
94#define NUM_RECENT_KEYS (100)
95int recent_keys_index; /* Index for storing next element into recent_keys */
96int total_keys; /* Total number of elements stored into recent_keys */
5160df46 97Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
284f4730 98
6569cc8d
JB
99/* Vector holding the key sequence that invoked the current command.
100 It is reused for each command, and it may be longer than the current
101 sequence; this_command_key_count indicates how many elements
102 actually mean something.
103 It's easier to staticpro a single Lisp_Object than an array. */
104Lisp_Object this_command_keys;
105int this_command_key_count;
284f4730
JB
106
107extern int minbuf_level;
108
109extern struct backtrace *backtrace_list;
110
111/* Nonzero means do menu prompting. */
112static int menu_prompting;
113
114/* Character to see next line of menu prompt. */
115static Lisp_Object menu_prompt_more_char;
116
117/* For longjmp to where kbd input is being done. */
118static jmp_buf getcjmp;
119
120/* True while doing kbd input. */
121int waiting_for_input;
122
123/* True while displaying for echoing. Delays C-g throwing. */
124static int echoing;
125
80e4aa30 126/* Nonzero means C-g should cause immediate error-signal. */
284f4730
JB
127int immediate_quit;
128
129/* Character to recognize as the help char. */
7e85b935 130Lisp_Object Vhelp_char;
284f4730
JB
131
132/* Form to execute when help char is typed. */
133Lisp_Object Vhelp_form;
134
7e85b935
RS
135/* Command to run when the help character follows a prefix key. */
136Lisp_Object Vprefix_help_command;
137
9f9c0e27
RS
138/* List of items that should move to the end of the menu bar. */
139Lisp_Object Vmenu_bar_final_items;
a73c5e29 140
284f4730
JB
141/* Character that causes a quit. Normally C-g.
142
143 If we are running on an ordinary terminal, this must be an ordinary
144 ASCII char, since we want to make it our interrupt character.
145
146 If we are not running on an ordinary terminal, it still needs to be
147 an ordinary ASCII char. This character needs to be recognized in
148 the input interrupt handler. At this point, the keystroke is
149 represented as a struct input_event, while the desired quit
150 character is specified as a lispy event. The mapping from struct
151 input_events to lispy events cannot run in an interrupt handler,
152 and the reverse mapping is difficult for anything but ASCII
153 keystrokes.
154
155 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
156 ASCII character. */
157int quit_char;
158
159extern Lisp_Object current_global_map;
160extern int minibuf_level;
161
162/* Current depth in recursive edits. */
163int command_loop_level;
164
165/* Total number of times command_loop has read a key sequence. */
166int num_input_keys;
167
168/* Last input character read as a command. */
169Lisp_Object last_command_char;
170
7d6de002
RS
171/* Last input character read as a command, not counting menus
172 reached by the mouse. */
173Lisp_Object last_nonmenu_event;
174
284f4730
JB
175/* Last input character read for any purpose. */
176Lisp_Object last_input_char;
177
dbc4e1c1
JB
178/* If not Qnil, a list of objects to be read as subsequent command input. */
179Lisp_Object unread_command_events;
284f4730 180
86e5706b
RS
181/* If not -1, an event to be read as subsequent command input. */
182int unread_command_char;
183
cd21b839
JB
184/* If not Qnil, this is a switch-frame event which we decided to put
185 off until the end of a key sequence. This should be read as the
dbc4e1c1 186 next command input, after any unread_command_events.
8f805655
JB
187
188 read_key_sequence uses this to delay switch-frame events until the
189 end of the key sequence; Fread_char uses it to put off switch-frame
190 events until a non-ASCII event is acceptable as input. */
191Lisp_Object unread_switch_frame;
cd21b839 192
9fa4395d
RS
193/* A mask of extra modifier bits to put into every keyboard char. */
194int extra_keyboard_modifiers;
195
284f4730
JB
196/* Char to use as prefix when a meta character is typed in.
197 This is bound on entry to minibuffer in case ESC is changed there. */
198
199Lisp_Object meta_prefix_char;
200
201/* Last size recorded for a current buffer which is not a minibuffer. */
202static int last_non_minibuf_size;
203
06ef7355 204/* Number of idle seconds before an auto-save and garbage collection. */
284f4730
JB
205static Lisp_Object Vauto_save_timeout;
206
207/* Total number of times read_char has returned. */
208int num_input_chars;
209
51172b6d
RS
210/* Total number of times read_char has returned, outside of macros. */
211int num_nonmacro_input_chars;
212
284f4730
JB
213/* Auto-save automatically when this many characters have been typed
214 since the last time. */
215
216static int auto_save_interval;
217
51172b6d 218/* Value of num_nonmacro_input_chars as of last auto save. */
284f4730
JB
219
220int last_auto_save;
221
222/* Last command executed by the editor command loop, not counting
223 commands that set the prefix argument. */
224
225Lisp_Object last_command;
226
227/* The command being executed by the command loop.
228 Commands may set this, and the value set will be copied into last_command
229 instead of the actual command. */
230Lisp_Object this_command;
231
07d2b8de 232#ifdef MULTI_FRAME
fce33686 233/* The frame in which the last input event occurred, or Qmacro if the
3c370943
JB
234 last event came from a macro. We use this to determine when to
235 generate switch-frame events. This may be cleared by functions
236 like Fselect_frame, to make sure that a switch-frame event is
237 generated by the next character. */
238Lisp_Object internal_last_event_frame;
239
240/* A user-visible version of the above, intended to allow users to
241 figure out where the last event came from, if the event doesn't
242 carry that information itself (i.e. if it was a character). */
ff11dfa1 243Lisp_Object Vlast_event_frame;
07d2b8de 244#endif
284f4730 245
1113d9db
JB
246/* The timestamp of the last input event we received from the X server.
247 X Windows wants this for selection ownership. */
284f4730
JB
248unsigned long last_event_timestamp;
249
250Lisp_Object Qself_insert_command;
251Lisp_Object Qforward_char;
252Lisp_Object Qbackward_char;
e58aa385 253Lisp_Object Qundefined;
284f4730
JB
254
255/* read_key_sequence stores here the command definition of the
256 key sequence that it reads. */
257Lisp_Object read_key_sequence_cmd;
258
259/* Form to evaluate (if non-nil) when Emacs is started. */
260Lisp_Object Vtop_level;
261
262/* User-supplied string to translate input characters through. */
263Lisp_Object Vkeyboard_translate_table;
264
265/* Keymap mapping ASCII function key sequences onto their preferred forms. */
266extern Lisp_Object Vfunction_key_map;
267
a612e298
RS
268/* Keymap mapping ASCII function key sequences onto their preferred forms. */
269Lisp_Object Vkey_translation_map;
270
86e5706b
RS
271/* Non-nil means deactivate the mark at end of this command. */
272Lisp_Object Vdeactivate_mark;
273
48e416d4
RS
274/* Menu bar specified in Lucid Emacs fashion. */
275
276Lisp_Object Vlucid_menu_bar_dirty_flag;
277Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
278
86e5706b
RS
279/* Hooks to run before and after each command. */
280Lisp_Object Qpre_command_hook, Qpost_command_hook;
281Lisp_Object Vpre_command_hook, Vpost_command_hook;
282
284f4730
JB
283/* File in which we write all commands we read. */
284FILE *dribble;
285
286/* Nonzero if input is available. */
287int input_pending;
288
b04904fb
RS
289/* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
290 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
291
284f4730
JB
292int meta_key;
293
294extern char *pending_malloc_warning;
295
296/* Circular buffer for pre-read keyboard input. */
297static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
298
7b4aedb9 299/* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
4bb994d1 300
7b4aedb9
JB
301 The interrupt-level event handlers will never enqueue an event on a
302 frame which is not in Vframe_list, and once an event is dequeued,
3c370943
JB
303 internal_last_event_frame or the event itself points to the frame.
304 So that's all fine.
4bb994d1
JB
305
306 But while the event is sitting in the queue, it's completely
307 unprotected. Suppose the user types one command which will run for
308 a while and then delete a frame, and then types another event at
309 the frame that will be deleted, before the command gets around to
310 it. Suppose there are no references to this frame elsewhere in
311 Emacs, and a GC occurs before the second event is dequeued. Now we
312 have an event referring to a freed frame, which will crash Emacs
313 when it is dequeued.
314
3c370943 315 Similar things happen when an event on a scroll bar is enqueued; the
7b4aedb9 316 window may be deleted while the event is in the queue.
4bb994d1 317
7b4aedb9
JB
318 So, we use this vector to protect the frame_or_window field in the
319 event queue. That way, they'll be dequeued as dead frames or
320 windows, but still valid lisp objects.
321
322 If kbd_buffer[i].kind != no_event, then
323 (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
324 == kbd_buffer[i].frame_or_window. */
325static Lisp_Object kbd_buffer_frame_or_window;
4bb994d1 326
284f4730
JB
327/* Pointer to next available character in kbd_buffer.
328 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
329 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
330 next available char is in kbd_buffer[0]. */
331static struct input_event *kbd_fetch_ptr;
332
333/* Pointer to next place to store character in kbd_buffer. This
334 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
335 character should go in kbd_buffer[0]. */
ff0b5f4c
JB
336#ifdef __STDC__
337volatile
338#endif
284f4730
JB
339static struct input_event *kbd_store_ptr;
340
341/* The above pair of variables forms a "queue empty" flag. When we
342 enqueue a non-hook event, we increment kbd_write_count. When we
343 dequeue a non-hook event, we increment kbd_read_count. We say that
4bb994d1 344 there is input available iff the two counters are not equal.
284f4730
JB
345
346 Why not just have a flag set and cleared by the enqueuing and
347 dequeuing functions? Such a flag could be screwed up by interrupts
348 at inopportune times. */
349
663258f2 350/* If this flag is non-zero, we check mouse_moved to see when the
4bb994d1 351 mouse moves, and motion events will appear in the input stream. If
663258f2
JB
352 it is zero, mouse motion is ignored. */
353static int do_mouse_tracking;
284f4730
JB
354
355/* The window system handling code should set this if the mouse has
356 moved since the last call to the mouse_position_hook. Calling that
357 hook should clear this. Code assumes that if this is set, it can
358 call mouse_position_hook to get the promised position, so don't set
359 it unless you're prepared to substantiate the claim! */
360int mouse_moved;
361
362/* True iff there is an event in kbd_buffer, or if mouse tracking is
363 enabled and there is a new mouse position in the mouse movement
364 buffer. Note that if this is false, that doesn't mean that there
365 is readable input; all the events in the queue might be button-up
366 events, and do_mouse_tracking might be off. */
367#define EVENT_QUEUES_EMPTY \
368 ((kbd_fetch_ptr == kbd_store_ptr) && (!do_mouse_tracking || !mouse_moved))
369
370
371/* Symbols to head events. */
372Lisp_Object Qmouse_movement;
3c370943 373Lisp_Object Qscroll_bar_movement;
cd21b839
JB
374Lisp_Object Qswitch_frame;
375
284f4730
JB
376/* Symbols to denote kinds of events. */
377Lisp_Object Qfunction_key;
378Lisp_Object Qmouse_click;
379/* Lisp_Object Qmouse_movement; - also an event header */
284f4730
JB
380
381/* Properties of event headers. */
382Lisp_Object Qevent_kind;
88cb0656 383Lisp_Object Qevent_symbol_elements;
284f4730 384
598a9fa7
JB
385Lisp_Object Qmenu_enable;
386
0a7f1fc0
JB
387/* An event header symbol HEAD may have a property named
388 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
389 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
390 mask of modifiers applied to it. If present, this is used to help
391 speed up parse_modifiers. */
392Lisp_Object Qevent_symbol_element_mask;
393
394/* An unmodified event header BASE may have a property named
395 Qmodifier_cache, which is an alist mapping modifier masks onto
396 modified versions of BASE. If present, this helps speed up
397 apply_modifiers. */
398Lisp_Object Qmodifier_cache;
399
5ec75a55 400/* Symbols to use for parts of windows. */
284f4730 401Lisp_Object Qmode_line;
e5d77022 402Lisp_Object Qvertical_line;
3c370943 403Lisp_Object Qvertical_scroll_bar;
5ec75a55
RS
404Lisp_Object Qmenu_bar;
405
406extern Lisp_Object Qmenu_enable;
284f4730 407
f4255cd1
JB
408Lisp_Object recursive_edit_unwind (), command_loop ();
409Lisp_Object Fthis_command_keys ();
03b4122a 410Lisp_Object Qextended_command_history;
284f4730 411
ffd56f97
JB
412/* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
413 happens. */
414EMACS_TIME *input_available_clear_time;
284f4730
JB
415
416/* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
417 Default is 1 if INTERRUPT_INPUT is defined. */
418int interrupt_input;
419
420/* Nonzero while interrupts are temporarily deferred during redisplay. */
421int interrupts_deferred;
422
423/* nonzero means use ^S/^Q for flow control. */
424int flow_control;
425
284f4730
JB
426/* Allow m- file to inhibit use of FIONREAD. */
427#ifdef BROKEN_FIONREAD
428#undef FIONREAD
429#endif
430
431/* We are unable to use interrupts if FIONREAD is not available,
432 so flush SIGIO so we won't try. */
433#ifndef FIONREAD
434#ifdef SIGIO
435#undef SIGIO
436#endif
437#endif
438
34f04431
RS
439/* If we support X Windows, turn on the code to poll periodically
440 to detect C-g. It isn't actually used when doing interrupt input. */
284f4730 441#ifdef HAVE_X_WINDOWS
284f4730
JB
442#define POLL_FOR_INPUT
443#endif
284f4730
JB
444\f
445/* Global variable declarations. */
446
447/* Function for init_keyboard to call with no args (if nonzero). */
448void (*keyboard_init_hook) ();
449
450static int read_avail_input ();
451static void get_input_pending ();
9fd7d808 452static int readable_events ();
7617111f 453static Lisp_Object read_char_menu_prompt ();
a612e298
RS
454static Lisp_Object make_lispy_event ();
455static Lisp_Object make_lispy_movement ();
456static Lisp_Object modify_event_symbol ();
457static Lisp_Object make_lispy_switch_frame ();
284f4730
JB
458
459/* > 0 if we are to echo keystrokes. */
460static int echo_keystrokes;
461
462/* Nonzero means echo each character as typed. */
463static int immediate_echo;
464
465/* The text we're echoing in the modeline - partial key sequences,
f4255cd1
JB
466 usually. '\0'-terminated. This really shouldn't have a fixed size. */
467static char echobuf[300];
284f4730
JB
468
469/* Where to append more text to echobuf if we want to. */
470static char *echoptr;
471
472#define min(a,b) ((a)<(b)?(a):(b))
473#define max(a,b) ((a)>(b)?(a):(b))
474
475/* Install the string STR as the beginning of the string of echoing,
476 so that it serves as a prompt for the next character.
477 Also start echoing. */
478
479echo_prompt (str)
480 char *str;
481{
482 int len = strlen (str);
483 if (len > sizeof echobuf - 4)
484 len = sizeof echobuf - 4;
0a7f1fc0 485 bcopy (str, echobuf, len);
284f4730 486 echoptr = echobuf + len;
0a7f1fc0 487 *echoptr = '\0';
284f4730
JB
488
489 echo ();
490}
491
492/* Add C to the echo string, if echoing is going on.
493 C can be a character, which is printed prettily ("M-C-x" and all that
494 jazz), or a symbol, whose name is printed. */
495
496echo_char (c)
497 Lisp_Object c;
498{
499 extern char *push_key_description ();
500
501 if (immediate_echo)
502 {
503 char *ptr = echoptr;
504
505 if (ptr != echobuf)
506 *ptr++ = ' ';
507
508 /* If someone has passed us a composite event, use its head symbol. */
88cb0656 509 c = EVENT_HEAD (c);
284f4730
JB
510
511 if (XTYPE (c) == Lisp_Int)
512 {
513 if (ptr - echobuf > sizeof echobuf - 6)
514 return;
515
cb5df6ae 516 ptr = push_key_description (XINT (c), ptr);
284f4730
JB
517 }
518 else if (XTYPE (c) == Lisp_Symbol)
519 {
520 struct Lisp_String *name = XSYMBOL (c)->name;
521 if (((ptr - echobuf) + name->size + 4) > sizeof echobuf)
522 return;
523 bcopy (name->data, ptr, name->size);
524 ptr += name->size;
525 }
526
7e85b935 527 if (echoptr == echobuf && EQ (c, Vhelp_char))
284f4730
JB
528 {
529 strcpy (ptr, " (Type ? for further options)");
530 ptr += strlen (ptr);
531 }
532
533 *ptr = 0;
534 echoptr = ptr;
535
536 echo ();
537 }
538}
539
540/* Temporarily add a dash to the end of the echo string if it's not
541 empty, so that it serves as a mini-prompt for the very next character. */
542
543echo_dash ()
544{
545 if (!immediate_echo && echoptr == echobuf)
546 return;
4bafa972
JB
547 /* Do nothing if not echoing at all. */
548 if (echoptr == 0)
549 return;
284f4730
JB
550
551 /* Put a dash at the end of the buffer temporarily,
552 but make it go away when the next character is added. */
553 echoptr[0] = '-';
554 echoptr[1] = 0;
555
556 echo ();
557}
558
559/* Display the current echo string, and begin echoing if not already
560 doing so. */
561
562echo ()
563{
564 if (!immediate_echo)
565 {
566 int i;
567 immediate_echo = 1;
568
569 for (i = 0; i < this_command_key_count; i++)
6569cc8d 570 echo_char (XVECTOR (this_command_keys)->contents[i]);
284f4730
JB
571 echo_dash ();
572 }
573
574 echoing = 1;
575 message1 (echobuf);
576 echoing = 0;
577
578 if (waiting_for_input && !NILP (Vquit_flag))
579 quit_throw_to_read_char ();
580}
581
582/* Turn off echoing, for the start of a new command. */
583
584cancel_echoing ()
585{
586 immediate_echo = 0;
587 echoptr = echobuf;
588}
589
590/* Return the length of the current echo string. */
591
592static int
593echo_length ()
594{
595 return echoptr - echobuf;
596}
597
598/* Truncate the current echo message to its first LEN chars.
599 This and echo_char get used by read_key_sequence when the user
ff11dfa1 600 switches frames while entering a key sequence. */
284f4730
JB
601
602static void
603echo_truncate (len)
604 int len;
605{
606 echobuf[len] = '\0';
0a7f1fc0 607 echoptr = echobuf + len;
284f4730
JB
608}
609
610\f
611/* Functions for manipulating this_command_keys. */
612static void
613add_command_key (key)
614 Lisp_Object key;
615{
6569cc8d
JB
616 int size = XVECTOR (this_command_keys)->size;
617
618 if (this_command_key_count >= size)
284f4730 619 {
6569cc8d
JB
620 Lisp_Object new_keys = Fmake_vector (make_number (size * 2), Qnil);
621
622 bcopy (XVECTOR (this_command_keys)->contents,
623 XVECTOR (new_keys)->contents,
8f805655 624 size * sizeof (Lisp_Object));
6569cc8d
JB
625
626 this_command_keys = new_keys;
284f4730 627 }
6569cc8d
JB
628
629 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
284f4730
JB
630}
631\f
632Lisp_Object
633recursive_edit_1 ()
634{
635 int count = specpdl_ptr - specpdl;
636 Lisp_Object val;
637
638 if (command_loop_level > 0)
639 {
640 specbind (Qstandard_output, Qt);
641 specbind (Qstandard_input, Qt);
642 }
643
644 val = command_loop ();
645 if (EQ (val, Qt))
646 Fsignal (Qquit, Qnil);
647
cb5df6ae 648 return unbind_to (count, Qnil);
284f4730
JB
649}
650
651/* When an auto-save happens, record the "time", and don't do again soon. */
652record_auto_save ()
653{
51172b6d 654 last_auto_save = num_nonmacro_input_chars;
284f4730
JB
655}
656\f
284f4730
JB
657DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
658 "Invoke the editor command loop recursively.\n\
659To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
660that tells this function to return.\n\
661Alternately, `(throw 'exit t)' makes this function signal an error.\n\
662This function is called by the editor initialization to begin editing.")
663 ()
664{
665 int count = specpdl_ptr - specpdl;
666 Lisp_Object val;
667
668 command_loop_level++;
669 update_mode_lines = 1;
670
671 record_unwind_protect (recursive_edit_unwind,
672 (command_loop_level
673 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
674 ? Fcurrent_buffer ()
675 : Qnil);
676 recursive_edit_1 ();
677 return unbind_to (count, Qnil);
678}
679
680Lisp_Object
681recursive_edit_unwind (buffer)
682 Lisp_Object buffer;
683{
684 if (!NILP (buffer))
685 Fset_buffer (buffer);
686
687 command_loop_level--;
688 update_mode_lines = 1;
689 return Qnil;
690}
691\f
692Lisp_Object
693cmd_error (data)
694 Lisp_Object data;
695{
696 Lisp_Object errmsg, tail, errname, file_error;
697 Lisp_Object stream;
698 struct gcpro gcpro1;
699 int i;
700
701 Vquit_flag = Qnil;
702 Vinhibit_quit = Qt;
703 Vstandard_output = Qt;
704 Vstandard_input = Qt;
705 Vexecuting_macro = Qnil;
706 echo_area_glyphs = 0;
707
ff11dfa1 708 /* If the window system or terminal frame hasn't been initialized
284f4730
JB
709 yet, or we're not interactive, it's best to dump this message out
710 to stderr and exit. */
ff11dfa1 711 if (! FRAME_MESSAGE_BUF (selected_frame)
284f4730
JB
712 || noninteractive)
713 stream = Qexternal_debugging_output;
714 else
715 {
716 Fdiscard_input ();
717 bitch_at_user ();
718 stream = Qt;
719 }
720
721 errname = Fcar (data);
722
723 if (EQ (errname, Qerror))
724 {
725 data = Fcdr (data);
726 if (!CONSP (data)) data = Qnil;
727 errmsg = Fcar (data);
728 file_error = Qnil;
729 }
730 else
731 {
732 errmsg = Fget (errname, Qerror_message);
733 file_error = Fmemq (Qfile_error,
734 Fget (errname, Qerror_conditions));
735 }
736
737 /* Print an error message including the data items.
738 This is done by printing it into a scratch buffer
739 and then making a copy of the text in the buffer. */
740
741 if (!CONSP (data)) data = Qnil;
742 tail = Fcdr (data);
743 GCPRO1 (tail);
744
745 /* For file-error, make error message by concatenating
746 all the data items. They are all strings. */
747 if (!NILP (file_error) && !NILP (tail))
748 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
749
750 if (XTYPE (errmsg) == Lisp_String)
751 Fprinc (errmsg, stream);
752 else
753 write_string_1 ("peculiar error", -1, stream);
754
755 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
756 {
757 write_string_1 (i ? ", " : ": ", 2, stream);
758 if (!NILP (file_error))
759 Fprinc (Fcar (tail), stream);
760 else
761 Fprin1 (Fcar (tail), stream);
762 }
763 UNGCPRO;
764
ff11dfa1 765 /* If the window system or terminal frame hasn't been initialized
284f4730 766 yet, or we're in -batch mode, this error should cause Emacs to exit. */
ff11dfa1 767 if (! FRAME_MESSAGE_BUF (selected_frame)
284f4730
JB
768 || noninteractive)
769 {
770 Fterpri (stream);
771 Fkill_emacs (make_number (-1));
772 }
773
774 Vquit_flag = Qnil;
775
776 Vinhibit_quit = Qnil;
777 return make_number (0);
778}
779\f
780Lisp_Object command_loop_1 ();
781Lisp_Object command_loop_2 ();
782Lisp_Object top_level_1 ();
783
784/* Entry to editor-command-loop.
785 This level has the catches for exiting/returning to editor command loop.
786 It returns nil to exit recursive edit, t to abort it. */
787
788Lisp_Object
789command_loop ()
790{
791 if (command_loop_level > 0 || minibuf_level > 0)
792 {
793 return internal_catch (Qexit, command_loop_2, Qnil);
794 }
795 else
796 while (1)
797 {
798 internal_catch (Qtop_level, top_level_1, Qnil);
799 internal_catch (Qtop_level, command_loop_2, Qnil);
800
801 /* End of file in -batch run causes exit here. */
802 if (noninteractive)
803 Fkill_emacs (Qt);
804 }
805}
806
807/* Here we catch errors in execution of commands within the
808 editing loop, and reenter the editing loop.
809 When there is an error, cmd_error runs and returns a non-nil
810 value to us. A value of nil means that cmd_loop_1 itself
811 returned due to end of file (or end of kbd macro). */
812
813Lisp_Object
814command_loop_2 ()
815{
816 register Lisp_Object val;
817
818 do
819 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
820 while (!NILP (val));
821
822 return Qnil;
823}
824
825Lisp_Object
826top_level_2 ()
827{
828 return Feval (Vtop_level);
829}
830
831Lisp_Object
832top_level_1 ()
833{
834 /* On entry to the outer level, run the startup file */
835 if (!NILP (Vtop_level))
836 internal_condition_case (top_level_2, Qerror, cmd_error);
837 else if (!NILP (Vpurify_flag))
838 message ("Bare impure Emacs (standard Lisp code not loaded)");
839 else
840 message ("Bare Emacs (standard Lisp code not loaded)");
841 return Qnil;
842}
843
844DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
845 "Exit all recursive editing levels.")
846 ()
847{
848 Fthrow (Qtop_level, Qnil);
849}
850
851DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
852 "Exit from the innermost recursive edit or minibuffer.")
853 ()
854{
855 if (command_loop_level > 0 || minibuf_level > 0)
856 Fthrow (Qexit, Qnil);
857
858 error ("No recursive edit is in progress");
859}
860
861DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
862 "Abort the command that requested this recursive edit or minibuffer input.")
863 ()
864{
865 if (command_loop_level > 0 || minibuf_level > 0)
866 Fthrow (Qexit, Qt);
867
868 error ("No recursive edit is in progress");
869}
870\f
871/* This is the actual command reading loop,
872 sans error-handling encapsulation. */
873
874Lisp_Object Fcommand_execute ();
875static int read_key_sequence ();
876
877Lisp_Object
878command_loop_1 ()
879{
48e416d4 880 Lisp_Object cmd, tem;
284f4730
JB
881 int lose;
882 int nonundocount;
883 Lisp_Object keybuf[30];
884 int i;
885 int no_redisplay;
886 int no_direct;
86e5706b
RS
887 int prev_modiff;
888 struct buffer *prev_buffer;
284f4730
JB
889
890 Vprefix_arg = Qnil;
86e5706b 891 Vdeactivate_mark = Qnil;
284f4730
JB
892 waiting_for_input = 0;
893 cancel_echoing ();
894
895 /* Don't clear out last_command at the beginning of a macro. */
896 if (XTYPE (Vexecuting_macro) != Lisp_String)
897 last_command = Qt;
898
899 nonundocount = 0;
900 no_redisplay = 0;
901 this_command_key_count = 0;
902
a612e298
RS
903 /* Make sure this hook runs after commands that get errors and
904 throw to top level. */
905 if (!NILP (Vpost_command_hook))
906 call1 (Vrun_hooks, Qpost_command_hook);
907
284f4730
JB
908 while (1)
909 {
910 /* Install chars successfully executed in kbd macro. */
911
912 if (defining_kbd_macro && NILP (Vprefix_arg))
913 finalize_kbd_macro_chars ();
914
915 /* Make sure the current window's buffer is selected. */
916 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
917 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
918
919 /* Display any malloc warning that just came out. Use while because
920 displaying one warning can cause another. */
921
922 while (pending_malloc_warning)
923 display_malloc_warning ();
924
925 no_direct = 0;
926
86e5706b
RS
927 Vdeactivate_mark = Qnil;
928
284f4730 929 /* If minibuffer on and echo area in use,
eb8c3be9 930 wait 2 sec and redraw minibuffer. */
284f4730
JB
931
932 if (minibuf_level && echo_area_glyphs)
933 {
f1bed6d8
RS
934 /* Bind inhibit-quit to t so that C-g gets read in
935 rather than quitting back to the minibuffer. */
936 int count = specpdl_ptr - specpdl;
937 specbind (Qinhibit_quit, Qt);
284f4730 938 Fsit_for (make_number (2), Qnil, Qnil);
cb5df6ae 939 unbind_to (count, Qnil);
f1bed6d8 940
284f4730
JB
941 echo_area_glyphs = 0;
942 no_direct = 1;
943 if (!NILP (Vquit_flag))
944 {
945 Vquit_flag = Qnil;
dbc4e1c1 946 unread_command_events = Fcons (make_number (quit_char), Qnil);
284f4730
JB
947 }
948 }
949
950#ifdef C_ALLOCA
951 alloca (0); /* Cause a garbage collection now */
952 /* Since we can free the most stuff here. */
953#endif /* C_ALLOCA */
954
8f805655 955#if 0
ff11dfa1 956#ifdef MULTI_FRAME
8f805655
JB
957 /* Select the frame that the last event came from. Usually,
958 switch-frame events will take care of this, but if some lisp
959 code swallows a switch-frame event, we'll fix things up here.
960 Is this a good idea? */
3c370943
JB
961 if (XTYPE (internal_last_event_frame) == Lisp_Frame
962 && XFRAME (internal_last_event_frame) != selected_frame)
963 Fselect_frame (internal_last_event_frame, Qnil);
cd21b839 964#endif
284f4730 965#endif
48e416d4
RS
966 /* If it has changed current-menubar from previous value,
967 really recompute the menubar from the value. */
a646e520
RS
968 if (! NILP (Vlucid_menu_bar_dirty_flag)
969 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
48e416d4
RS
970 call0 (Qrecompute_lucid_menubar);
971
0e9d6dd3 972#if 0 /* This is done in xdisp.c now. */
48e416d4
RS
973#ifdef MULTI_FRAME
974 for (tem = Vframe_list; CONSP (tem); tem = XCONS (tem)->cdr)
975 {
976 struct frame *f = XFRAME (XCONS (tem)->car);
977 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
598a9fa7
JB
978
979 /* If the user has switched buffers or windows, we need to
980 recompute to reflect the new bindings. But we'll
981 recompute when update_mode_lines is set too; that means
982 that people can use force-mode-line-update to request
983 that the menu bar be recomputed. The adverse effect on
984 the rest of the redisplay algorithm is about the same as
985 windows_or_buffers_changed anyway. */
48e416d4 986 if (windows_or_buffers_changed
598a9fa7 987 || update_mode_lines
48e416d4
RS
988 || (XFASTINT (w->last_modified) < MODIFF
989 && (XFASTINT (w->last_modified)
990 <= XBUFFER (w->buffer)->save_modified)))
991 {
992 struct buffer *prev = current_buffer;
993 current_buffer = XBUFFER (w->buffer);
994 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items ();
995 current_buffer = prev;
996 }
997 }
998#endif /* MULTI_FRAME */
0e9d6dd3 999#endif /* 0 */
284f4730 1000
8f805655 1001 /* Read next key sequence; i gets its length. */
84d91fda 1002 i = read_key_sequence (keybuf, (sizeof keybuf / sizeof (keybuf[0])), Qnil);
8f805655
JB
1003
1004 ++num_input_keys;
1005
284f4730
JB
1006 /* Now we have read a key sequence of length I,
1007 or else I is 0 and we found end of file. */
1008
1009 if (i == 0) /* End of file -- happens only in */
1010 return Qnil; /* a kbd macro, at the end. */
dcc408a0
RS
1011 /* -1 means read_key_sequence got a menu that was rejected.
1012 Just loop around and read another command. */
1013 if (i == -1)
1014 {
1015 cancel_echoing ();
1016 this_command_key_count = 0;
1017 continue;
1018 }
284f4730 1019
284f4730
JB
1020 last_command_char = keybuf[i - 1];
1021
75c0b143
RS
1022 /* If the previous command tried to force a specific window-start,
1023 forget about that, in case this command moves point far away
1024 from that position. */
1025 XWINDOW (selected_window)->force_start = Qnil;
1026
284f4730
JB
1027 cmd = read_key_sequence_cmd;
1028 if (!NILP (Vexecuting_macro))
1029 {
1030 if (!NILP (Vquit_flag))
1031 {
1032 Vexecuting_macro = Qt;
1033 QUIT; /* Make some noise. */
1034 /* Will return since macro now empty. */
1035 }
1036 }
1037
1038 /* Do redisplay processing after this command except in special
1039 cases identified below that set no_redisplay to 1. */
1040 no_redisplay = 0;
1041
86e5706b
RS
1042 prev_buffer = current_buffer;
1043 prev_modiff = MODIFF;
1044
284f4730
JB
1045 /* Execute the command. */
1046
86e5706b
RS
1047 this_command = cmd;
1048 if (!NILP (Vpre_command_hook))
1049 call1 (Vrun_hooks, Qpre_command_hook);
1050
258bf746 1051 if (NILP (this_command))
284f4730
JB
1052 {
1053 /* nil means key is undefined. */
1054 bitch_at_user ();
1055 defining_kbd_macro = 0;
1056 update_mode_lines = 1;
1057 Vprefix_arg = Qnil;
86e5706b 1058
284f4730
JB
1059 }
1060 else
1061 {
284f4730
JB
1062 if (NILP (Vprefix_arg) && ! no_direct)
1063 {
1064 /* Recognize some common commands in common situations and
1065 do them directly. */
258bf746 1066 if (EQ (this_command, Qforward_char) && point < ZV)
284f4730
JB
1067 {
1068 struct Lisp_Vector *dp
1069 = window_display_table (XWINDOW (selected_window));
1070 lose = FETCH_CHAR (point);
1071 SET_PT (point + 1);
0f7a8fee 1072 if ((dp
82ba47d7 1073 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
06ddfb24 1074 && XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1)
0f7a8fee 1075 : (lose >= 0x20 && lose < 0x7f))
284f4730
JB
1076 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1077 >= MODIFF)
1078 && (XFASTINT (XWINDOW (selected_window)->last_point)
1079 == point - 1)
1080 && !windows_or_buffers_changed
1081 && EQ (current_buffer->selective_display, Qnil)
1082 && !detect_input_pending ()
1083 && NILP (Vexecuting_macro))
1084 no_redisplay = direct_output_forward_char (1);
1085 goto directly_done;
1086 }
258bf746 1087 else if (EQ (this_command, Qbackward_char) && point > BEGV)
284f4730
JB
1088 {
1089 struct Lisp_Vector *dp
1090 = window_display_table (XWINDOW (selected_window));
1091 SET_PT (point - 1);
1092 lose = FETCH_CHAR (point);
0f7a8fee
JB
1093 if ((dp
1094 ? (XTYPE (DISP_CHAR_VECTOR (dp, lose)) != Lisp_Vector
06ddfb24 1095 && XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1)
0f7a8fee 1096 : (lose >= 0x20 && lose < 0x7f))
284f4730
JB
1097 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1098 >= MODIFF)
1099 && (XFASTINT (XWINDOW (selected_window)->last_point)
1100 == point + 1)
1101 && !windows_or_buffers_changed
1102 && EQ (current_buffer->selective_display, Qnil)
1103 && !detect_input_pending ()
1104 && NILP (Vexecuting_macro))
1105 no_redisplay = direct_output_forward_char (-1);
1106 goto directly_done;
1107 }
258bf746 1108 else if (EQ (this_command, Qself_insert_command)
284f4730
JB
1109 /* Try this optimization only on ascii keystrokes. */
1110 && XTYPE (last_command_char) == Lisp_Int)
1111 {
1112 unsigned char c = XINT (last_command_char);
1113
1114 if (NILP (Vexecuting_macro) &&
1115 !EQ (minibuf_window, selected_window))
1116 {
1117 if (!nonundocount || nonundocount >= 20)
1118 {
1119 Fundo_boundary ();
1120 nonundocount = 0;
1121 }
1122 nonundocount++;
1123 }
1124 lose = (XFASTINT (XWINDOW (selected_window)->last_modified)
1125 < MODIFF)
1126 || (XFASTINT (XWINDOW (selected_window)->last_point)
1127 != point)
1128 || MODIFF <= current_buffer->save_modified
1129 || windows_or_buffers_changed
1130 || !EQ (current_buffer->selective_display, Qnil)
1131 || detect_input_pending ()
1132 || !NILP (Vexecuting_macro);
b8d9050d 1133 if (internal_self_insert (c, 0))
284f4730
JB
1134 {
1135 lose = 1;
1136 nonundocount = 0;
1137 }
1138 if (!lose &&
1139 (point == ZV || FETCH_CHAR (point) == '\n'))
1140 {
1141 struct Lisp_Vector *dp
1142 = window_display_table (XWINDOW (selected_window));
b8d9050d 1143 int lose = c;
284f4730 1144
0f7a8fee
JB
1145 if (dp)
1146 {
1147 Lisp_Object obj = DISP_CHAR_VECTOR (dp, lose);
1148
1149 if (XTYPE (obj) == Lisp_Vector
1150 && XVECTOR (obj)->size == 1
1151 && (XTYPE (obj = XVECTOR (obj)->contents[0])
1152 == Lisp_Int))
1153 no_redisplay =
1154 direct_output_for_insert (XINT (obj));
1155 }
1156 else
1157 {
1158 if (lose >= 0x20 && lose <= 0x7e)
1159 no_redisplay = direct_output_for_insert (lose);
1160 }
284f4730
JB
1161 }
1162 goto directly_done;
1163 }
1164 }
1165
1166 /* Here for a command that isn't executed directly */
1167
1168 nonundocount = 0;
1169 if (NILP (Vprefix_arg))
1170 Fundo_boundary ();
258bf746 1171 Fcommand_execute (this_command, Qnil);
284f4730 1172
284f4730 1173 }
a764a753 1174 directly_done: ;
284f4730 1175
86e5706b
RS
1176 if (!NILP (Vpost_command_hook))
1177 call1 (Vrun_hooks, Qpost_command_hook);
1178
284f4730
JB
1179 /* If there is a prefix argument,
1180 1) We don't want last_command to be ``universal-argument''
1181 (that would be dumb), so don't set last_command,
1182 2) we want to leave echoing on so that the prefix will be
1183 echoed as part of this key sequence, so don't call
1184 cancel_echoing, and
1185 3) we want to leave this_command_key_count non-zero, so that
1186 read_char will realize that it is re-reading a character, and
1187 not echo it a second time. */
1188 if (NILP (Vprefix_arg))
1189 {
1190 last_command = this_command;
1191 cancel_echoing ();
1192 this_command_key_count = 0;
1193 }
86e5706b
RS
1194
1195 if (!NILP (current_buffer->mark_active))
1196 {
1197 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1198 {
1199 current_buffer->mark_active = Qnil;
1200 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1201 }
1202 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1203 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1204 }
284f4730
JB
1205 }
1206}
1207\f
1208/* Number of seconds between polling for input. */
1209int polling_period;
1210
eb8c3be9 1211/* Nonzero means polling for input is temporarily suppressed. */
284f4730
JB
1212int poll_suppress_count;
1213
1214#ifdef POLL_FOR_INPUT
1215int polling_for_input;
1216
1217/* Handle an alarm once each second and read pending input
1218 so as to handle a C-g if it comces in. */
1219
1220SIGTYPE
1221input_poll_signal ()
1222{
9ac0d9e0
JB
1223 if (interrupt_input_blocked == 0
1224 && !waiting_for_input)
1225 read_avail_input (0);
284f4730
JB
1226 signal (SIGALRM, input_poll_signal);
1227 alarm (polling_period);
1228}
1229
1230#endif
1231
1232/* Begin signals to poll for input, if they are appropriate.
1233 This function is called unconditionally from various places. */
1234
1235start_polling ()
1236{
1237#ifdef POLL_FOR_INPUT
34f04431 1238 if (read_socket_hook && !interrupt_input)
284f4730
JB
1239 {
1240 poll_suppress_count--;
1241 if (poll_suppress_count == 0)
1242 {
1243 signal (SIGALRM, input_poll_signal);
1244 polling_for_input = 1;
1245 alarm (polling_period);
1246 }
1247 }
1248#endif
1249}
1250
1251/* Turn off polling. */
1252
1253stop_polling ()
1254{
1255#ifdef POLL_FOR_INPUT
34f04431 1256 if (read_socket_hook && !interrupt_input)
284f4730
JB
1257 {
1258 if (poll_suppress_count == 0)
1259 {
1260 polling_for_input = 0;
1261 alarm (0);
1262 }
1263 poll_suppress_count++;
1264 }
1265#endif
1266}
fe8aeef3
RS
1267
1268/* Set the value of poll_suppress_count to COUNT
1269 and start or stop polling accordingly. */
1270
1271void
1272set_poll_suppress_count (count)
1273 int count;
1274{
1275#ifdef POLL_FOR_INPUT
1276 if (count == 0 && poll_suppress_count != 0)
1277 {
1278 poll_suppress_count = 1;
1279 start_polling ();
1280 }
1281 else if (count != 0 && poll_suppress_count == 0)
1282 {
1283 stop_polling ();
1284 }
1285 poll_suppress_count = count;
1286#endif
1287}
284f4730 1288\f
faf5e407
JB
1289/* Applying the control modifier to CHARACTER. */
1290int
1291make_ctrl_char (c)
1292 int c;
1293{
d205953b
JB
1294 /* Save the upper bits here. */
1295 int upper = c & ~0177;
1296
1297 c &= 0177;
1298
1299 /* Everything in the columns containing the upper-case letters
1300 denotes a control character. */
1301 if (c >= 0100 && c < 0140)
1302 {
1303 int oc = c;
1304 c &= ~0140;
1305 /* Set the shift modifier for a control char
1306 made from a shifted letter. But only for letters! */
1307 if (oc >= 'A' && oc <= 'Z')
1308 c |= shift_modifier;
1309 }
1310
1311 /* The lower-case letters denote control characters too. */
1312 else if (c >= 'a' && c <= 'z')
1313 c &= ~0140;
1314
1315 /* Include the bits for control and shift
1316 only if the basic ASCII code can't indicate them. */
1317 else if (c >= ' ')
1318 c |= ctrl_modifier;
1319
1320 /* Replace the high bits. */
1321 c |= (upper & ~ctrl_modifier);
faf5e407
JB
1322
1323 return c;
1324}
1325
1326
1327\f
284f4730
JB
1328/* Input of single characters from keyboard */
1329
1330Lisp_Object print_help ();
1331static Lisp_Object kbd_buffer_get_event ();
1332
1333/* read a character from the keyboard; call the redisplay if needed */
1334/* commandflag 0 means do not do auto-saving, but do do redisplay.
1335 -1 means do not do redisplay, but do do autosaving.
1336 1 means do both. */
1337
7d6de002
RS
1338/* The arguments MAPS and NMAPS are for menu prompting.
1339 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1340
1341 PREV_EVENT is the previous input event, or nil if we are reading
1342 the first event of a key sequence.
1343
6569cc8d
JB
1344 If USED_MOUSE_MENU is non-zero, then we set *USED_MOUSE_MENU to 1
1345 if we used a mouse menu to read the input, or zero otherwise. If
dcc408a0
RS
1346 USED_MOUSE_MENU is zero, *USED_MOUSE_MENU is left alone.
1347
1348 Value is t if we showed a menu and the user rejected it. */
7d6de002 1349
284f4730 1350Lisp_Object
7d6de002 1351read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
284f4730 1352 int commandflag;
7d6de002
RS
1353 int nmaps;
1354 Lisp_Object *maps;
1355 Lisp_Object prev_event;
1356 int *used_mouse_menu;
284f4730
JB
1357{
1358 register Lisp_Object c;
1359 int count;
1360 jmp_buf save_jump;
1361
dbc4e1c1 1362 if (CONSP (unread_command_events))
284f4730 1363 {
dbc4e1c1
JB
1364 c = XCONS (unread_command_events)->car;
1365 unread_command_events = XCONS (unread_command_events)->cdr;
284f4730 1366
284f4730
JB
1367 if (this_command_key_count == 0)
1368 goto reread_first;
1369 else
1370 goto reread;
1371 }
1372
86e5706b
RS
1373 if (unread_command_char != -1)
1374 {
1375 XSET (c, Lisp_Int, unread_command_char);
1376 unread_command_char = -1;
1377
1378 if (this_command_key_count == 0)
1379 goto reread_first;
1380 else
1381 goto reread;
1382 }
1383
284f4730
JB
1384 if (!NILP (Vexecuting_macro))
1385 {
07d2b8de 1386#ifdef MULTI_FRAME
fce33686
JB
1387 /* We set this to Qmacro; since that's not a frame, nobody will
1388 try to switch frames on us, and the selected window will
1389 remain unchanged.
1390
1391 Since this event came from a macro, it would be misleading to
eb8c3be9 1392 leave internal_last_event_frame set to wherever the last
3c370943
JB
1393 real event came from. Normally, a switch-frame event selects
1394 internal_last_event_frame after each command is read, but
1395 events read from a macro should never cause a new frame to be
1396 selected. */
1397 Vlast_event_frame = internal_last_event_frame = Qmacro;
07d2b8de 1398#endif
fce33686 1399
663258f2
JB
1400 /* Exit the macro if we are at the end.
1401 Also, some things replace the macro with t
1402 to force an early exit. */
1403 if (EQ (Vexecuting_macro, Qt)
1404 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
284f4730
JB
1405 {
1406 XSET (c, Lisp_Int, -1);
1407 return c;
1408 }
1409
1410 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
86e5706b
RS
1411 if (XTYPE (Vexecuting_macro) == Lisp_String
1412 && (XINT (c) & 0x80))
1413 XFASTINT (c) = CHAR_META | (XINT (c) & ~0x80);
1414
284f4730
JB
1415 executing_macro_index++;
1416
1417 goto from_macro;
1418 }
1419
cd21b839
JB
1420 if (!NILP (unread_switch_frame))
1421 {
1422 c = unread_switch_frame;
1423 unread_switch_frame = Qnil;
1424
1425 /* This event should make it into this_command_keys, and get echoed
f4255cd1
JB
1426 again, so we go to reread_first, rather than reread. */
1427 goto reread_first;
cd21b839
JB
1428 }
1429
e9bf89a0
RS
1430 if (commandflag >= 0 && !input_pending && !detect_input_pending ())
1431 prepare_menu_bars ();
1432
284f4730 1433 /* Save outer setjmp data, in case called recursively. */
f76475ad 1434 save_getcjmp (save_jump);
284f4730
JB
1435
1436 stop_polling ();
1437
1438 if (commandflag >= 0 && !input_pending && !detect_input_pending ())
1439 redisplay ();
1440
1441 if (_setjmp (getcjmp))
1442 {
1443 XSET (c, Lisp_Int, quit_char);
07d2b8de 1444#ifdef MULTI_FRAME
3c370943
JB
1445 XSET (internal_last_event_frame, Lisp_Frame, selected_frame);
1446 Vlast_event_frame = internal_last_event_frame;
07d2b8de 1447#endif
04904c29
RS
1448 /* If we report the quit char as an event,
1449 don't do so more than once. */
1450 if (!NILP (Vinhibit_quit))
1451 Vquit_flag = Qnil;
284f4730 1452
284f4730
JB
1453 goto non_reread;
1454 }
1455
1456 /* Message turns off echoing unless more keystrokes turn it on again. */
1457 if (echo_area_glyphs && *echo_area_glyphs && echo_area_glyphs != echobuf)
1458 cancel_echoing ();
1459 else
1460 /* If already echoing, continue. */
1461 echo_dash ();
1462
1463 /* If in middle of key sequence and minibuffer not active,
1464 start echoing if enough time elapses. */
1465 if (minibuf_level == 0 && !immediate_echo && this_command_key_count > 0
27203ead 1466 && ! noninteractive
284f4730
JB
1467 && echo_keystrokes > 0
1468 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0))
1469 {
1470 Lisp_Object tem0;
1471
7d6de002
RS
1472 /* After a mouse event, start echoing right away.
1473 This is because we are probably about to display a menu,
1474 and we don't want to delay before doing so. */
dbc4e1c1 1475 if (EVENT_HAS_PARAMETERS (prev_event))
284f4730 1476 echo ();
7d6de002
RS
1477 else
1478 {
1479 tem0 = sit_for (echo_keystrokes, 0, 1, 1);
1480 if (EQ (tem0, Qt))
1481 echo ();
1482 }
284f4730
JB
1483 }
1484
1485 /* Maybe auto save due to number of keystrokes or idle time. */
1486
1487 if (commandflag != 0
1488 && auto_save_interval > 0
51172b6d 1489 && num_nonmacro_input_chars - last_auto_save > max (auto_save_interval, 20)
284f4730
JB
1490 && !detect_input_pending ())
1491 {
1492 jmp_buf temp;
1493 save_getcjmp (temp);
1494 Fdo_auto_save (Qnil, Qnil);
1495 restore_getcjmp (temp);
1496 }
1497
7d6de002
RS
1498 /* Try reading a character via menu prompting.
1499 Try this before the sit-for, because the sit-for
1500 would do the wrong thing if we are supposed to do
1501 menu prompting. */
1502 c = Qnil;
1503 if (INTERACTIVE && !NILP (prev_event))
1504 c = read_char_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
1505
284f4730
JB
1506 /* Slow down auto saves logarithmically in size of current buffer,
1507 and garbage collect while we're at it. */
26c1639e 1508 if (INTERACTIVE && NILP (c))
7d6de002
RS
1509 {
1510 int delay_level, buffer_size;
1511
1512 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
1513 last_non_minibuf_size = Z - BEG;
1514 buffer_size = (last_non_minibuf_size >> 8) + 1;
1515 delay_level = 0;
1516 while (buffer_size > 64)
1517 delay_level++, buffer_size -= buffer_size >> 2;
1518 if (delay_level < 4) delay_level = 4;
1519 /* delay_level is 4 for files under around 50k, 7 at 100k,
1520 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
1521
1522 /* Auto save if enough time goes by without input. */
1523 if (commandflag != 0
51172b6d 1524 && num_nonmacro_input_chars > last_auto_save
7d6de002
RS
1525 && XTYPE (Vauto_save_timeout) == Lisp_Int
1526 && XINT (Vauto_save_timeout) > 0)
1527 {
1528 Lisp_Object tem0;
1529 int delay = delay_level * XFASTINT (Vauto_save_timeout) / 4;
1530 tem0 = sit_for (delay, 0, 1, 1);
1531 if (EQ (tem0, Qt))
1532 {
1533 jmp_buf temp;
1534 save_getcjmp (temp);
1535 Fdo_auto_save (Qnil, Qnil);
1536 restore_getcjmp (temp);
1537
1538 /* If we have auto-saved and there is still no input
1539 available, garbage collect if there has been enough
1540 consing going on to make it worthwhile. */
1541 if (!detect_input_pending ()
1542 && consing_since_gc > gc_cons_threshold / 2)
7e85b935
RS
1543 {
1544 Fgarbage_collect ();
e9bf89a0
RS
1545 /* prepare_menu_bars isn't safe here, but it should
1546 also be unnecessary. */
7e85b935
RS
1547 redisplay ();
1548 }
7d6de002
RS
1549 }
1550 }
1551 }
284f4730
JB
1552
1553 /* Actually read a character, waiting if necessary. */
4eb4f926 1554 while (NILP (c))
1e12dd87
RS
1555 {
1556 c = kbd_buffer_get_event ();
1557 if (!NILP (c))
1558 break;
1559 if (commandflag >= 0 && !input_pending && !detect_input_pending ())
1560 redisplay ();
1561 }
284f4730 1562
284f4730 1563 /* Terminate Emacs in batch mode if at eof. */
7d6de002 1564 if (noninteractive && XTYPE (c) == Lisp_Int && XINT (c) < 0)
284f4730
JB
1565 Fkill_emacs (make_number (1));
1566
80645119
JB
1567 if (XTYPE (c) == Lisp_Int)
1568 {
1569 /* Add in any extra modifiers, where appropriate. */
1570 if ((extra_keyboard_modifiers & CHAR_CTL)
1571 || ((extra_keyboard_modifiers & 0177) < ' '
1572 && (extra_keyboard_modifiers & 0177) != 0))
faf5e407 1573 XSETINT (c, make_ctrl_char (XINT (c)));
80645119
JB
1574
1575 /* Transfer any other modifier bits directly from
1576 extra_keyboard_modifiers to c. Ignore the actual character code
1577 in the low 16 bits of extra_keyboard_modifiers. */
b8d9050d 1578 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
80645119 1579 }
9fa4395d 1580
284f4730
JB
1581 non_reread:
1582
f76475ad 1583 restore_getcjmp (save_jump);
284f4730
JB
1584
1585 start_polling ();
1586
1587 echo_area_glyphs = 0;
1588
1589 /* Handle things that only apply to characters. */
1590 if (XTYPE (c) == Lisp_Int)
1591 {
1592 /* If kbd_buffer_get_event gave us an EOF, return that. */
86e5706b 1593 if (XINT (c) == -1)
284f4730
JB
1594 return c;
1595
284f4730 1596 if (XTYPE (Vkeyboard_translate_table) == Lisp_String
f4255cd1
JB
1597 && XSTRING (Vkeyboard_translate_table)->size > XFASTINT (c))
1598 XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[XFASTINT (c)]);
284f4730
JB
1599 }
1600
1601 total_keys++;
5160df46
JB
1602 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
1603 if (++recent_keys_index >= NUM_RECENT_KEYS)
284f4730
JB
1604 recent_keys_index = 0;
1605
1606 /* Write c to the dribble file. If c is a lispy event, write
1607 the event's symbol to the dribble file, in <brackets>. Bleaugh.
1608 If you, dear reader, have a better idea, you've got the source. :-) */
1609 if (dribble)
1610 {
1611 if (XTYPE (c) == Lisp_Int)
f4255cd1 1612 putc (XINT (c), dribble);
284f4730
JB
1613 else
1614 {
1615 Lisp_Object dribblee = c;
1616
1617 /* If it's a structured event, take the event header. */
88cb0656 1618 dribblee = EVENT_HEAD (dribblee);
284f4730 1619
8f805655 1620 if (XTYPE (dribblee) == Lisp_Symbol)
284f4730
JB
1621 {
1622 putc ('<', dribble);
8f805655
JB
1623 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
1624 XSYMBOL (dribblee)->name->size,
284f4730
JB
1625 dribble);
1626 putc ('>', dribble);
1627 }
1628 }
1629
1630 fflush (dribble);
1631 }
1632
1633 store_kbd_macro_char (c);
1634
51172b6d
RS
1635 num_nonmacro_input_chars++;
1636
284f4730
JB
1637 from_macro:
1638 reread_first:
284f4730 1639
4bb994d1
JB
1640 /* Record this character as part of the current key.
1641 Don't record mouse motion; it should never matter. */
1642 if (! (EVENT_HAS_PARAMETERS (c)
1643 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
7b4aedb9
JB
1644 {
1645 echo_char (c);
1646 add_command_key (c);
1647 }
284f4730
JB
1648
1649 /* Re-reading in the middle of a command */
1650 reread:
1651 last_input_char = c;
1652 num_input_chars++;
1653
1654 /* Process the help character specially if enabled */
7e85b935 1655 if (EQ (c, Vhelp_char) && !NILP (Vhelp_form))
284f4730
JB
1656 {
1657 Lisp_Object tem0;
1658 count = specpdl_ptr - specpdl;
1659
1660 record_unwind_protect (Fset_window_configuration,
1661 Fcurrent_window_configuration (Qnil));
1662
1663 tem0 = Feval (Vhelp_form);
1664 if (XTYPE (tem0) == Lisp_String)
1665 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
1666
1667 cancel_echoing ();
6569cc8d 1668 c = read_char (0, 0, 0, Qnil, 0);
ff11dfa1 1669 /* Remove the help from the frame */
284f4730 1670 unbind_to (count, Qnil);
e9bf89a0 1671 prepare_menu_bars ();
284f4730
JB
1672 redisplay ();
1673 if (EQ (c, make_number (040)))
1674 {
1675 cancel_echoing ();
7d6de002 1676 c = read_char (0, 0, 0, Qnil, 0);
284f4730
JB
1677 }
1678 }
1679
1680 return c;
1681}
1682
1683Lisp_Object
1684print_help (object)
1685 Lisp_Object object;
1686{
1687 Fprinc (object, Qnil);
1688 return Qnil;
1689}
1690
1691/* Copy out or in the info on where C-g should throw to.
1692 This is used when running Lisp code from within get_char,
1693 in case get_char is called recursively.
1694 See read_process_output. */
1695
1696save_getcjmp (temp)
1697 jmp_buf temp;
1698{
1699 bcopy (getcjmp, temp, sizeof getcjmp);
1700}
1701
1702restore_getcjmp (temp)
1703 jmp_buf temp;
1704{
1705 bcopy (temp, getcjmp, sizeof getcjmp);
1706}
1707
1708\f
284f4730
JB
1709/* Restore mouse tracking enablement. See Ftrack_mouse for the only use
1710 of this function. */
1711static Lisp_Object
1712tracking_off (old_value)
1713 Lisp_Object old_value;
1714{
1715 if (! XFASTINT (old_value))
1716 {
1717 do_mouse_tracking = 0;
1718
1719 /* Redisplay may have been preempted because there was input
1720 available, and it assumes it will be called again after the
1721 input has been processed. If the only input available was
1722 the sort that we have just disabled, then we need to call
1723 redisplay. */
1724 if (!readable_events ())
1725 {
e9bf89a0 1726 prepare_menu_bars ();
284f4730
JB
1727 redisplay_preserve_echo_area ();
1728 get_input_pending (&input_pending);
1729 }
1730 }
1731}
1732
1733DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
4bb994d1
JB
1734 "Evaluate BODY with mouse movement events enabled.\n\
1735Within a `track-mouse' form, mouse motion generates input events that\n\
1736you can read with `read-event'.\n\
1737Normally, mouse motion is ignored.")
284f4730
JB
1738 (args)
1739 Lisp_Object args;
1740{
1741 int count = specpdl_ptr - specpdl;
1742 Lisp_Object val;
1743
1744 XSET (val, Lisp_Int, do_mouse_tracking);
1745 record_unwind_protect (tracking_off, val);
1746
1747 do_mouse_tracking = 1;
1748
1749 val = Fprogn (args);
1750 return unbind_to (count, val);
1751}
a612e298
RS
1752\f
1753/* Low level keyboard/mouse input.
1754 kbd_buffer_store_event places events in kbd_buffer, and
1755 kbd_buffer_get_event retrieves them.
1756 mouse_moved indicates when the mouse has moved again, and
1757 *mouse_position_hook provides the mouse position. */
1758
1759/* Return true iff there are any events in the queue that read-char
1760 would return. If this returns false, a read-char would block. */
1761static int
1762readable_events ()
1763{
1764 return ! EVENT_QUEUES_EMPTY;
1765}
1766
1767/* Set this for debugging, to have a way to get out */
1768int stop_character;
284f4730
JB
1769
1770/* Store an event obtained at interrupt level into kbd_buffer, fifo */
1771
1772void
1773kbd_buffer_store_event (event)
1774 register struct input_event *event;
1775{
1776 if (event->kind == no_event)
1777 abort ();
1778
1779 if (event->kind == ascii_keystroke)
1780 {
e9bf89a0 1781 register int c = event->code & 0377;
284f4730 1782
faf5e407
JB
1783 if (event->modifiers & ctrl_modifier)
1784 c = make_ctrl_char (c);
1785
9fd7d808
RS
1786 c |= (event->modifiers
1787 & (meta_modifier | alt_modifier
1788 | hyper_modifier | super_modifier));
1789
86e5706b 1790 if (c == quit_char)
284f4730 1791 {
3e51c7b7
JB
1792 extern SIGTYPE interrupt_signal ();
1793
07d2b8de 1794#ifdef MULTI_FRAME
284f4730 1795 /* If this results in a quit_char being returned to Emacs as
3c370943 1796 input, set Vlast_event_frame properly. If this doesn't
284f4730 1797 get returned to Emacs as an event, the next event read
ff11dfa1 1798 will set Vlast_event_frame again, so this is safe to do. */
4bb994d1 1799 {
27203ead
RS
1800 Lisp_Object focus
1801 = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
4bb994d1
JB
1802
1803 if (NILP (focus))
3c370943 1804 internal_last_event_frame = event->frame_or_window;
4bb994d1 1805 else
3c370943
JB
1806 internal_last_event_frame = focus;
1807 Vlast_event_frame = internal_last_event_frame;
4bb994d1 1808 }
07d2b8de 1809#endif
3e51c7b7 1810
ffd56f97 1811 last_event_timestamp = event->timestamp;
284f4730
JB
1812 interrupt_signal ();
1813 return;
1814 }
1815
1816 if (c && c == stop_character)
1817 {
1818 sys_suspend ();
1819 return;
1820 }
284f4730
JB
1821 }
1822
1823 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
1824 kbd_store_ptr = kbd_buffer;
1825
1826 /* Don't let the very last slot in the buffer become full,
1827 since that would make the two pointers equal,
1828 and that is indistinguishable from an empty buffer.
1829 Discard the event if it would fill the last slot. */
1830 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
1831 {
1832 kbd_store_ptr->kind = event->kind;
27203ead
RS
1833 if (event->kind == selection_request_event)
1834 {
1835 /* We must not use the ordinary copying code for this case,
1836 since `part' is an enum and copying it might not copy enough
1837 in this case. */
1838 bcopy (event, kbd_store_ptr, sizeof (*event));
1839 }
1840 else
1841 {
1842 kbd_store_ptr->code = event->code;
1843 kbd_store_ptr->part = event->part;
1844 kbd_store_ptr->frame_or_window = event->frame_or_window;
1845 kbd_store_ptr->modifiers = event->modifiers;
1846 kbd_store_ptr->x = event->x;
1847 kbd_store_ptr->y = event->y;
1848 kbd_store_ptr->timestamp = event->timestamp;
1849 }
7b4aedb9
JB
1850 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
1851 - kbd_buffer]
1852 = event->frame_or_window);
284f4730
JB
1853
1854 kbd_store_ptr++;
1855 }
1856}
a612e298
RS
1857\f
1858/* Read one event from the event buffer, waiting if necessary.
1859 The value is a Lisp object representing the event.
1860 The value is nil for an event that should be ignored,
1861 or that was handled here.
1862 We always read and discard one event. */
284f4730
JB
1863
1864static Lisp_Object
1865kbd_buffer_get_event ()
1866{
1867 register int c;
1868 Lisp_Object obj;
1869
1870 if (noninteractive)
1871 {
1872 c = getchar ();
1873 XSET (obj, Lisp_Int, c);
1874 return obj;
1875 }
1876
1877 /* Wait until there is input available. */
1878 for (;;)
1879 {
284f4730
JB
1880 if (!EVENT_QUEUES_EMPTY)
1881 break;
1882
1883 /* If the quit flag is set, then read_char will return
1884 quit_char, so that counts as "available input." */
1885 if (!NILP (Vquit_flag))
1886 quit_throw_to_read_char ();
1887
1888 /* One way or another, wait until input is available; then, if
1889 interrupt handlers have not read it, read it now. */
1890
1891#ifdef OLDVMS
1892 wait_for_kbd_input ();
1893#else
1894/* Note SIGIO has been undef'd if FIONREAD is missing. */
1895#ifdef SIGIO
1896 gobble_input (0);
1897#endif /* SIGIO */
1898 if (EVENT_QUEUES_EMPTY)
1899 {
f76475ad
JB
1900 Lisp_Object minus_one;
1901
1902 XSET (minus_one, Lisp_Int, -1);
1903 wait_reading_process_input (0, 0, minus_one, 1);
284f4730
JB
1904
1905 if (!interrupt_input && EVENT_QUEUES_EMPTY)
1906 {
1907 read_avail_input (0);
1908 }
1909 }
1910#endif /* not VMS */
1911 }
1912
1913 /* At this point, we know that there is a readable event available
1914 somewhere. If the event queue is empty, then there must be a
1915 mouse movement enabled and available. */
1916 if (kbd_fetch_ptr != kbd_store_ptr)
1917 {
cd21b839 1918 struct input_event *event;
3e51c7b7 1919
856bf263 1920 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
cd21b839
JB
1921 ? kbd_fetch_ptr
1922 : kbd_buffer);
3e51c7b7 1923
cd21b839 1924 last_event_timestamp = event->timestamp;
cd21b839 1925
4bb994d1
JB
1926 obj = Qnil;
1927
48e416d4 1928 /* These two kinds of events get special handling
a612e298
RS
1929 and don't actually appear to the command loop.
1930 We return nil for them. */
48e416d4
RS
1931 if (event->kind == selection_request_event)
1932 {
598a9fa7 1933#ifdef HAVE_X11
48e416d4
RS
1934 x_handle_selection_request (event);
1935 kbd_fetch_ptr = event + 1;
598a9fa7
JB
1936#else
1937 /* We're getting selection request events, but we don't have
1938 a window system. */
1939 abort ();
1940#endif
48e416d4
RS
1941 }
1942
1e12dd87 1943 else if (event->kind == selection_clear_event)
48e416d4 1944 {
598a9fa7 1945#ifdef HAVE_X11
48e416d4
RS
1946 x_handle_selection_clear (event);
1947 kbd_fetch_ptr = event + 1;
598a9fa7
JB
1948#else
1949 /* We're getting selection request events, but we don't have
1950 a window system. */
1951 abort ();
1952#endif
48e416d4 1953 }
990acea3
RS
1954#ifdef HAVE_X11
1955 else if (event->kind == delete_window_event)
1956 {
58f6c8ab
RS
1957 Lisp_Object tail, frame;
1958 struct frame *f;
1959
1960 /* If the user destroys the only frame, Emacs should exit.
1961 Count visible frames and iconified frames. */
1962 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
1963 {
1964 frame = XCONS (tail)->car;
1965 if (XTYPE (frame) != Lisp_Frame || EQ (frame, event->frame_or_window))
1966 continue;
1967 f = XFRAME (frame);
1968 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1969 break;
1970 }
990acea3 1971
58f6c8ab 1972 if (! CONSP (tail))
80e4aa30 1973 Fkill_emacs (Qnil);
854f3a54
RS
1974
1975 Fdelete_frame (event->frame_or_window, Qt);
1976 kbd_fetch_ptr = event + 1;
990acea3
RS
1977 }
1978#endif
a612e298
RS
1979 /* Just discard these, by returning nil.
1980 (They shouldn't be found in the buffer,
1981 but on some machines it appears they do show up.) */
1982 else if (event->kind == no_event)
1983 kbd_fetch_ptr = event + 1;
48e416d4 1984
4bb994d1
JB
1985 /* If this event is on a different frame, return a switch-frame this
1986 time, and leave the event in the queue for next time. */
1e12dd87
RS
1987 else
1988 {
a6d53864 1989#ifdef MULTI_FRAME
1e12dd87
RS
1990 Lisp_Object frame = event->frame_or_window;
1991 Lisp_Object focus;
7b4aedb9 1992
1e12dd87
RS
1993 if (XTYPE (frame) == Lisp_Window)
1994 frame = WINDOW_FRAME (XWINDOW (frame));
4bb994d1 1995
1e12dd87
RS
1996 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
1997 if (! NILP (focus))
1998 frame = focus;
07d2b8de 1999
1e12dd87
RS
2000 if (! EQ (frame, internal_last_event_frame)
2001 && XFRAME (frame) != selected_frame)
2002 obj = make_lispy_switch_frame (frame);
2003 internal_last_event_frame = frame;
a6d53864 2004#endif /* MULTI_FRAME */
4bb994d1 2005
1e12dd87
RS
2006 /* If we didn't decide to make a switch-frame event, go ahead
2007 and build a real event from the queue entry. */
cd21b839 2008
1e12dd87
RS
2009 if (NILP (obj))
2010 {
2011 obj = make_lispy_event (event);
2012
2013 /* Wipe out this event, to catch bugs. */
2014 event->kind = no_event;
2015 (XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer]
2016 = Qnil);
2017
2018 kbd_fetch_ptr = event + 1;
2019 }
4bb994d1 2020 }
284f4730 2021 }
a612e298 2022 /* Try generating a mouse motion event. */
284f4730
JB
2023 else if (do_mouse_tracking && mouse_moved)
2024 {
7e85b935 2025 FRAME_PTR f = 0;
7b4aedb9 2026 Lisp_Object bar_window;
3c370943 2027 enum scroll_bar_part part;
e5d77022
JB
2028 Lisp_Object x, y;
2029 unsigned long time;
284f4730 2030
7b4aedb9 2031 (*mouse_position_hook) (&f, &bar_window, &part, &x, &y, &time);
4bb994d1
JB
2032
2033 obj = Qnil;
284f4730 2034
07d2b8de 2035#ifdef MULTI_FRAME
4bb994d1
JB
2036 /* Decide if we should generate a switch-frame event. Don't
2037 generate switch-frame events for motion outside of all Emacs
2038 frames. */
2039 if (f)
cd21b839 2040 {
4bb994d1
JB
2041 Lisp_Object frame = FRAME_FOCUS_FRAME (f);
2042
2043 if (NILP (frame))
2044 XSET (frame, Lisp_Frame, f);
2045
80645119
JB
2046 if (! EQ (frame, internal_last_event_frame)
2047 && XFRAME (frame) != selected_frame)
764cb3f9 2048 obj = make_lispy_switch_frame (frame);
80645119 2049 internal_last_event_frame = frame;
cd21b839 2050 }
07d2b8de 2051#endif
4bb994d1
JB
2052
2053 /* If we didn't decide to make a switch-frame event, go ahead and
2054 return a mouse-motion event. */
2055 if (NILP (obj))
7b4aedb9 2056 obj = make_lispy_movement (f, bar_window, part, x, y, time);
4bb994d1 2057 }
284f4730
JB
2058 else
2059 /* We were promised by the above while loop that there was
2060 something for us to read! */
2061 abort ();
2062
2063 input_pending = readable_events ();
2064
3c370943
JB
2065#ifdef MULTI_FRAME
2066 Vlast_event_frame = internal_last_event_frame;
2067#endif
2068
284f4730
JB
2069 return (obj);
2070}
a612e298
RS
2071\f
2072/* Process any events that are not user-visible,
2073 then return, without reading any user-visible events. */
3a3b9632
RS
2074
2075void
2076swallow_events ()
2077{
2078 while (kbd_fetch_ptr != kbd_store_ptr)
2079 {
2080 struct input_event *event;
2081
2082 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2083 ? kbd_fetch_ptr
2084 : kbd_buffer);
2085
2086 last_event_timestamp = event->timestamp;
2087
2088 /* These two kinds of events get special handling
2089 and don't actually appear to the command loop. */
2090 if (event->kind == selection_request_event)
2091 {
2092#ifdef HAVE_X11
2093 x_handle_selection_request (event);
2094 kbd_fetch_ptr = event + 1;
2095#else
2096 /* We're getting selection request events, but we don't have
2097 a window system. */
2098 abort ();
2099#endif
2100 }
2101
2102 else if (event->kind == selection_clear_event)
2103 {
2104#ifdef HAVE_X11
2105 x_handle_selection_clear (event);
2106 kbd_fetch_ptr = event + 1;
2107#else
2108 /* We're getting selection request events, but we don't have
2109 a window system. */
2110 abort ();
2111#endif
2112 }
2113 else
2114 break;
2115 }
2116
2117 get_input_pending (&input_pending);
2118}
a612e298 2119\f
284f4730 2120/* Caches for modify_event_symbol. */
e9bf89a0 2121static Lisp_Object accent_key_syms;
80e4aa30 2122static Lisp_Object vendor_key_syms;
284f4730
JB
2123static Lisp_Object func_key_syms;
2124static Lisp_Object mouse_syms;
2125
80e4aa30
RS
2126Lisp_Object Vvendor_key_alist;
2127
e9bf89a0
RS
2128/* This is a list of keysym codes for special "accent" characters.
2129 It parallels lispy_accent_keys. */
2130
2131static int lispy_accent_codes[] =
2132{
79a7046c 2133#ifdef XK_dead_circumflex
e9bf89a0 2134 XK_dead_circumflex,
79a7046c
RS
2135#else
2136 0,
2137#endif
2138#ifdef XK_dead_grave
e9bf89a0 2139 XK_dead_grave,
79a7046c
RS
2140#else
2141 0,
2142#endif
2143#ifdef XK_dead_tilde
e9bf89a0 2144 XK_dead_tilde,
79a7046c
RS
2145#else
2146 0,
2147#endif
2148#ifdef XK_dead_diaeresis
e9bf89a0 2149 XK_dead_diaeresis,
79a7046c
RS
2150#else
2151 0,
2152#endif
2153#ifdef XK_dead_macron
e9bf89a0 2154 XK_dead_macron,
79a7046c
RS
2155#else
2156 0,
2157#endif
2158#ifdef XK_dead_degree
e9bf89a0 2159 XK_dead_degree,
79a7046c
RS
2160#else
2161 0,
2162#endif
2163#ifdef XK_dead_acute
e9bf89a0 2164 XK_dead_acute,
79a7046c
RS
2165#else
2166 0,
2167#endif
2168#ifdef XK_dead_cedilla
e9bf89a0 2169 XK_dead_cedilla,
79a7046c
RS
2170#else
2171 0,
2172#endif
2173#ifdef XK_dead_breve
e9bf89a0 2174 XK_dead_breve,
79a7046c
RS
2175#else
2176 0,
2177#endif
2178#ifdef XK_dead_ogonek
e9bf89a0 2179 XK_dead_ogonek,
79a7046c
RS
2180#else
2181 0,
2182#endif
2183#ifdef XK_dead_caron
e9bf89a0 2184 XK_dead_caron,
79a7046c
RS
2185#else
2186 0,
2187#endif
2188#ifdef XK_dead_doubleacute
e9bf89a0 2189 XK_dead_doubleacute,
79a7046c
RS
2190#else
2191 0,
2192#endif
2193#ifdef XK_dead_abovedot
e9bf89a0 2194 XK_dead_abovedot,
79a7046c
RS
2195#else
2196 0,
2197#endif
e9bf89a0
RS
2198};
2199
2200/* This is a list of Lisp names for special "accent" characters.
2201 It parallels lispy_accent_codes. */
2202
2203static char *lispy_accent_keys[] =
2204{
2205 "dead-circumflex",
2206 "dead-grave",
2207 "dead-tilde",
2208 "dead-diaeresis",
2209 "dead-macron",
2210 "dead-degree",
2211 "dead-acute",
2212 "dead-cedilla",
2213 "dead-breve",
2214 "dead-ogonek",
2215 "dead-caron",
2216 "dead-doubleacute",
2217 "dead-abovedot",
2218};
2219
284f4730
JB
2220/* You'll notice that this table is arranged to be conveniently
2221 indexed by X Windows keysym values. */
2222static char *lispy_function_keys[] =
2223 {
2224 /* X Keysym value */
2225
80e4aa30 2226 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00 */
86e5706b
RS
2227 "backspace",
2228 "tab",
2229 "linefeed",
2230 "clear",
2231 0,
2232 "return",
2233 0, 0,
2234 0, 0, 0, /* 0xff10 */
2235 "pause",
2236 0, 0, 0, 0, 0, 0, 0,
2237 "escape",
2238 0, 0, 0, 0,
2239 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff20...2f */
2240 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff30...3f */
2241 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
2242
284f4730
JB
2243 "home", /* 0xff50 */ /* IsCursorKey */
2244 "left",
2245 "up",
2246 "right",
2247 "down",
2248 "prior",
2249 "next",
2250 "end",
2251 "begin",
2252 0, /* 0xff59 */
2253 0, 0, 0, 0, 0, 0,
2254 "select", /* 0xff60 */ /* IsMiscFunctionKey */
2255 "print",
2256 "execute",
2257 "insert",
2258 0, /* 0xff64 */
2259 "undo",
2260 "redo",
2261 "menu",
2262 "find",
2263 "cancel",
2264 "help",
2265 "break", /* 0xff6b */
2266
80e4aa30 2267 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
284f4730 2268 0, /* 0xff76 */
36ae397e 2269 0, 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff7f */
284f4730
JB
2270 "kp-space", /* 0xff80 */ /* IsKeypadKey */
2271 0, 0, 0, 0, 0, 0, 0, 0,
2272 "kp-tab", /* 0xff89 */
2273 0, 0, 0,
2274 "kp-enter", /* 0xff8d */
2275 0, 0, 0,
2276 "kp-f1", /* 0xff91 */
2277 "kp-f2",
2278 "kp-f3",
2279 "kp-f4",
2280 0, /* 0xff95 */
2281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2282 "kp-multiply", /* 0xffaa */
2283 "kp-add",
2284 "kp-separator",
2285 "kp-subtract",
2286 "kp-decimal",
2287 "kp-divide", /* 0xffaf */
2288 "kp-0", /* 0xffb0 */
2289 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
2290 0, /* 0xffba */
2291 0, 0,
2292 "kp-equal", /* 0xffbd */
2293 "f1", /* 0xffbe */ /* IsFunctionKey */
86e5706b
RS
2294 "f2",
2295 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
2296 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
2297 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
2298 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
2299 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
2300 0, 0, 0, 0, 0, 0, 0, 0,
2301 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
2302 0, 0, 0, 0, 0, 0, 0, "delete"
284f4730
JB
2303 };
2304
2305static char *lispy_mouse_names[] =
2306{
2307 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
2308};
2309
3c370943 2310/* Scroll bar parts. */
4bb994d1
JB
2311Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
2312
3c370943
JB
2313/* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
2314Lisp_Object *scroll_bar_parts[] = {
4bb994d1
JB
2315 &Qabove_handle, &Qhandle, &Qbelow_handle
2316};
2317
2318
7b4aedb9 2319/* A vector, indexed by button number, giving the down-going location
3c370943 2320 of currently depressed buttons, both scroll bar and non-scroll bar.
7b4aedb9
JB
2321
2322 The elements have the form
2323 (BUTTON-NUMBER MODIFIER-MASK . REST)
2324 where REST is the cdr of a position as it would be reported in the event.
2325
2326 The make_lispy_event function stores positions here to tell the
2327 difference between click and drag events, and to store the starting
2328 location to be included in drag events. */
2329
2330static Lisp_Object button_down_location;
88cb0656 2331
fbcd35bd
JB
2332/* Information about the most recent up-going button event: Which
2333 button, what location, and what time. */
2334
559f9d04
RS
2335static int last_mouse_button;
2336static int last_mouse_x;
2337static int last_mouse_y;
2338static unsigned long button_down_time;
fbcd35bd 2339
564dc952
JB
2340/* The maximum time between clicks to make a double-click,
2341 or Qnil to disable double-click detection,
2342 or Qt for no time limit. */
2343Lisp_Object Vdouble_click_time;
fbcd35bd
JB
2344
2345/* The number of clicks in this multiple-click. */
2346
2347int double_click_count;
2348
284f4730
JB
2349/* Given a struct input_event, build the lisp event which represents
2350 it. If EVENT is 0, build a mouse movement event from the mouse
88cb0656
JB
2351 movement buffer, which should have a movement event in it.
2352
2353 Note that events must be passed to this function in the order they
2354 are received; this function stores the location of button presses
2355 in order to build drag events when the button is released. */
284f4730
JB
2356
2357static Lisp_Object
2358make_lispy_event (event)
2359 struct input_event *event;
2360{
79a7046c
RS
2361 int i;
2362
284f4730
JB
2363#ifdef SWITCH_ENUM_BUG
2364 switch ((int) event->kind)
2365#else
2366 switch (event->kind)
2367#endif
2368 {
284f4730
JB
2369 /* A simple keystroke. */
2370 case ascii_keystroke:
86e5706b 2371 {
e9bf89a0 2372 int c = event->code & 0377;
5a1c6df8
JB
2373 /* Turn ASCII characters into control characters
2374 when proper. */
2375 if (event->modifiers & ctrl_modifier)
d205953b
JB
2376 c = make_ctrl_char (c);
2377
2378 /* Add in the other modifier bits. We took care of ctrl_modifier
2379 just above, and the shift key was taken care of by the X code,
2380 and applied to control characters by make_ctrl_char. */
86e5706b
RS
2381 c |= (event->modifiers
2382 & (meta_modifier | alt_modifier
2383 | hyper_modifier | super_modifier));
559f9d04 2384 button_down_time = 0;
86e5706b
RS
2385 return c;
2386 }
284f4730
JB
2387
2388 /* A function key. The symbol may need to have modifier prefixes
2389 tacked onto it. */
2390 case non_ascii_keystroke:
559f9d04 2391 button_down_time = 0;
e9bf89a0
RS
2392
2393 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
2394 if (event->code == lispy_accent_codes[i])
2395 return modify_event_symbol (i,
2396 event->modifiers,
80e4aa30 2397 Qfunction_key, Qnil,
e9bf89a0
RS
2398 lispy_accent_keys, &accent_key_syms,
2399 (sizeof (lispy_accent_keys)
2400 / sizeof (lispy_accent_keys[0])));
2401
80e4aa30
RS
2402 /* Handle vendor-specific keysyms. */
2403 if (event->code & (1 << 28))
2404 {
2405 /* We need to use an alist rather than a vector as the cache
2406 since we can't make a vector long enuf. */
2407 if (NILP (vendor_key_syms))
2408 vendor_key_syms = Fcons (Qnil, Qnil);
2409 return modify_event_symbol (event->code & 0xffffff,
2410 event->modifiers,
2411 Qfunction_key, Vvendor_key_alist,
2412 0, &vendor_key_syms, 0xffffff);
2413 }
2414
e9bf89a0
RS
2415 return modify_event_symbol (event->code - 0xff00,
2416 event->modifiers,
80e4aa30 2417 Qfunction_key, Qnil,
284f4730
JB
2418 lispy_function_keys, &func_key_syms,
2419 (sizeof (lispy_function_keys)
2420 / sizeof (lispy_function_keys[0])));
2421 break;
2422
88cb0656
JB
2423 /* A mouse click. Figure out where it is, decide whether it's
2424 a press, click or drag, and build the appropriate structure. */
284f4730 2425 case mouse_click:
3c370943 2426 case scroll_bar_click:
284f4730 2427 {
e9bf89a0 2428 int button = event->code;
559f9d04 2429 int is_double;
7b4aedb9 2430 Lisp_Object position;
dbc4e1c1
JB
2431 Lisp_Object *start_pos_ptr;
2432 Lisp_Object start_pos;
284f4730 2433
7b4aedb9 2434 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
88cb0656
JB
2435 abort ();
2436
7b4aedb9
JB
2437 /* Build the position as appropriate for this mouse click. */
2438 if (event->kind == mouse_click)
284f4730 2439 {
7b4aedb9 2440 int part;
598a9fa7 2441 FRAME_PTR f = XFRAME (event->frame_or_window);
5ec75a55
RS
2442 Lisp_Object window
2443 = window_from_coordinates (f, XINT (event->x), XINT (event->y),
2444 &part);
7b4aedb9
JB
2445 Lisp_Object posn;
2446
5ec75a55
RS
2447 if (XINT (event->y) < FRAME_MENU_BAR_LINES (f))
2448 {
2449 int hpos;
2450 Lisp_Object items;
2451 items = FRAME_MENU_BAR_ITEMS (f);
2452 for (; CONSP (items); items = XCONS (items)->cdr)
2453 {
2454 Lisp_Object pos, string;
2455 pos = Fcdr (Fcdr (Fcar (items)));
2456 string = Fcar (Fcdr (Fcar (items)));
0a9b0670
RS
2457 if (XINT (event->x) >= XINT (pos)
2458 && XINT (event->x) < XINT (pos) + XSTRING (string)->size)
5ec75a55
RS
2459 break;
2460 }
2461 position
2462 = Fcons (event->frame_or_window,
2463 Fcons (Qmenu_bar,
2464 Fcons (Fcons (event->x, event->y),
2465 Fcons (make_number (event->timestamp),
2466 Qnil))));
2467
2468 if (CONSP (items))
2469 return Fcons (Fcar (Fcar (items)),
2470 Fcons (position, Qnil));
2471 else
2472 return Fcons (Qnil, Fcons (position, Qnil));
2473 }
2474 else if (XTYPE (window) != Lisp_Window)
7b4aedb9 2475 posn = Qnil;
284f4730 2476 else
7b4aedb9 2477 {
dbc4e1c1
JB
2478 XSETINT (event->x,
2479 (XINT (event->x) - XINT (XWINDOW (window)->left)));
2480 XSETINT (event->y,
2481 (XINT (event->y) - XINT (XWINDOW (window)->top)));
2482
7b4aedb9
JB
2483 if (part == 1)
2484 posn = Qmode_line;
2485 else if (part == 2)
2486 posn = Qvertical_line;
2487 else
dbc4e1c1
JB
2488 XSET (posn, Lisp_Int,
2489 buffer_posn_from_coords (XWINDOW (window),
2490 XINT (event->x),
2491 XINT (event->y)));
7b4aedb9
JB
2492 }
2493
5ec75a55
RS
2494 position
2495 = Fcons (window,
2496 Fcons (posn,
2497 Fcons (Fcons (event->x, event->y),
2498 Fcons (make_number (event->timestamp),
2499 Qnil))));
284f4730 2500 }
7b4aedb9 2501 else
88cb0656 2502 {
7b4aedb9
JB
2503 Lisp_Object window = event->frame_or_window;
2504 Lisp_Object portion_whole = Fcons (event->x, event->y);
3c370943 2505 Lisp_Object part = *scroll_bar_parts[(int) event->part];
7b4aedb9
JB
2506
2507 position =
2508 Fcons (window,
3c370943 2509 Fcons (Qvertical_scroll_bar,
7b4aedb9
JB
2510 Fcons (portion_whole,
2511 Fcons (make_number (event->timestamp),
2512 Fcons (part,
2513 Qnil)))));
88cb0656
JB
2514 }
2515
dbc4e1c1
JB
2516 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
2517
2518 start_pos = *start_pos_ptr;
2519 *start_pos_ptr = Qnil;
7b4aedb9 2520
559f9d04
RS
2521 is_double = (button == last_mouse_button
2522 && XINT (event->x) == last_mouse_x
2523 && XINT (event->y) == last_mouse_y
2524 && button_down_time != 0
2525 && (EQ (Vdouble_click_time, Qt)
2526 || (INTEGERP (Vdouble_click_time)
2527 && ((int)(event->timestamp - button_down_time)
2528 < XINT (Vdouble_click_time)))));
2529 last_mouse_button = button;
2530 last_mouse_x = XINT (event->x);
2531 last_mouse_y = XINT (event->y);
2532
7b4aedb9
JB
2533 /* If this is a button press, squirrel away the location, so
2534 we can decide later whether it was a click or a drag. */
2535 if (event->modifiers & down_modifier)
559f9d04
RS
2536 {
2537 if (is_double)
2538 {
2539 double_click_count++;
2540 event->modifiers |= ((double_click_count > 2)
2541 ? triple_modifier
2542 : double_modifier);
2543 }
2544 else
2545 double_click_count = 1;
2546 button_down_time = event->timestamp;
2547 *start_pos_ptr = Fcopy_alist (position);
2548 }
7b4aedb9 2549
88cb0656 2550 /* Now we're releasing a button - check the co-ordinates to
7b4aedb9 2551 see if this was a click or a drag. */
88cb0656
JB
2552 else if (event->modifiers & up_modifier)
2553 {
48e416d4
RS
2554 /* If we did not see a down before this up,
2555 ignore the up. Probably this happened because
2556 the down event chose a menu item.
2557 It would be an annoyance to treat the release
2558 of the button that chose the menu item
2559 as a separate event. */
2560
2561 if (XTYPE (start_pos) != Lisp_Cons)
2562 return Qnil;
2563
88cb0656 2564 event->modifiers &= ~up_modifier;
48e416d4 2565#if 0 /* Formerly we treated an up with no down as a click event. */
dbc4e1c1
JB
2566 if (XTYPE (start_pos) != Lisp_Cons)
2567 event->modifiers |= click_modifier;
2568 else
48e416d4 2569#endif
dbc4e1c1
JB
2570 {
2571 /* The third element of every position should be the (x,y)
2572 pair. */
2573 Lisp_Object down = Fnth (make_number (2), start_pos);
2574
fbcd35bd
JB
2575 if (EQ (event->x, XCONS (down)->car)
2576 && EQ (event->y, XCONS (down)->cdr))
2577 {
559f9d04
RS
2578 if (is_double && double_click_count > 1)
2579 event->modifiers |= ((double_click_count > 2)
2580 ? triple_modifier
2581 : double_modifier);
fbcd35bd 2582 else
559f9d04 2583 event->modifiers |= click_modifier;
fbcd35bd
JB
2584 }
2585 else
2586 {
559f9d04 2587 button_down_time = 0;
fbcd35bd
JB
2588 event->modifiers |= drag_modifier;
2589 }
dbc4e1c1 2590 }
88cb0656
JB
2591 }
2592 else
2593 /* Every mouse event should either have the down_modifier or
7b4aedb9 2594 the up_modifier set. */
88cb0656
JB
2595 abort ();
2596
88cb0656 2597 {
7b4aedb9 2598 /* Get the symbol we should use for the mouse click. */
5ec75a55
RS
2599 Lisp_Object head
2600 = modify_event_symbol (button,
2601 event->modifiers,
80e4aa30 2602 Qmouse_click, Qnil,
5ec75a55
RS
2603 lispy_mouse_names, &mouse_syms,
2604 (sizeof (lispy_mouse_names)
2605 / sizeof (lispy_mouse_names[0])));
7b4aedb9 2606
88cb0656 2607 if (event->modifiers & drag_modifier)
dbc4e1c1
JB
2608 return Fcons (head,
2609 Fcons (start_pos,
2610 Fcons (position,
2611 Qnil)));
fbcd35bd
JB
2612 else if (event->modifiers & (double_modifier | triple_modifier))
2613 return Fcons (head,
2614 Fcons (position,
2615 Fcons (make_number (double_click_count),
2616 Qnil)));
88cb0656
JB
2617 else
2618 return Fcons (head,
7b4aedb9 2619 Fcons (position,
88cb0656
JB
2620 Qnil));
2621 }
284f4730
JB
2622 }
2623
284f4730
JB
2624 /* The 'kind' field of the event is something we don't recognize. */
2625 default:
48e416d4 2626 abort ();
284f4730
JB
2627 }
2628}
2629
2630static Lisp_Object
7b4aedb9 2631make_lispy_movement (frame, bar_window, part, x, y, time)
ff11dfa1 2632 FRAME_PTR frame;
7b4aedb9 2633 Lisp_Object bar_window;
3c370943 2634 enum scroll_bar_part part;
284f4730 2635 Lisp_Object x, y;
e5d77022 2636 unsigned long time;
284f4730 2637{
3c370943 2638 /* Is it a scroll bar movement? */
7b4aedb9 2639 if (frame && ! NILP (bar_window))
4bb994d1 2640 {
3c370943 2641 Lisp_Object part_sym = *scroll_bar_parts[(int) part];
4bb994d1 2642
3c370943 2643 return Fcons (Qscroll_bar_movement,
7b4aedb9 2644 (Fcons (Fcons (bar_window,
3c370943 2645 Fcons (Qvertical_scroll_bar,
4bb994d1
JB
2646 Fcons (Fcons (x, y),
2647 Fcons (make_number (time),
cb5df6ae 2648 Fcons (part_sym,
4bb994d1
JB
2649 Qnil))))),
2650 Qnil)));
2651 }
2652
2653 /* Or is it an ordinary mouse movement? */
284f4730
JB
2654 else
2655 {
4bb994d1
JB
2656 int area;
2657 Lisp_Object window =
2658 (frame
2659 ? window_from_coordinates (frame, XINT (x), XINT (y), &area)
2660 : Qnil);
2661 Lisp_Object posn;
2662
2663 if (XTYPE (window) == Lisp_Window)
2664 {
2665 XSETINT (x, XINT (x) - XINT (XWINDOW (window)->left));
2666 XSETINT (y, XINT (y) - XINT (XWINDOW (window)->top));
2667
2668 if (area == 1)
2669 posn = Qmode_line;
2670 else if (area == 2)
2671 posn = Qvertical_line;
2672 else
2673 XSET (posn, Lisp_Int,
2674 buffer_posn_from_coords (XWINDOW (window),
2675 XINT (x), XINT (y)));
2676 }
e9bf89a0
RS
2677 else if (frame != 0)
2678 {
2679 XSET (window, Lisp_Frame, frame);
2680 posn = Qnil;
2681 }
284f4730 2682 else
4bb994d1
JB
2683 {
2684 window = Qnil;
2685 posn = Qnil;
f76b81d6
JB
2686 XFASTINT (x) = 0;
2687 XFASTINT (y) = 0;
4bb994d1 2688 }
284f4730 2689
4bb994d1
JB
2690 return Fcons (Qmouse_movement,
2691 Fcons (Fcons (window,
2692 Fcons (posn,
2693 Fcons (Fcons (x, y),
2694 Fcons (make_number (time),
2695 Qnil)))),
2696 Qnil));
2697 }
284f4730
JB
2698}
2699
cd21b839
JB
2700/* Construct a switch frame event. */
2701static Lisp_Object
2702make_lispy_switch_frame (frame)
2703 Lisp_Object frame;
2704{
2705 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
2706}
0a7f1fc0
JB
2707\f
2708/* Manipulating modifiers. */
284f4730 2709
0a7f1fc0 2710/* Parse the name of SYMBOL, and return the set of modifiers it contains.
284f4730 2711
0a7f1fc0
JB
2712 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
2713 SYMBOL's name of the end of the modifiers; the string from this
2714 position is the unmodified symbol name.
284f4730 2715
0a7f1fc0
JB
2716 This doesn't use any caches. */
2717static int
2718parse_modifiers_uncached (symbol, modifier_end)
284f4730 2719 Lisp_Object symbol;
0a7f1fc0 2720 int *modifier_end;
284f4730
JB
2721{
2722 struct Lisp_String *name;
2723 int i;
2724 int modifiers;
284f4730
JB
2725
2726 CHECK_SYMBOL (symbol, 1);
2727
2728 modifiers = 0;
2729 name = XSYMBOL (symbol)->name;
2730
284f4730 2731
0a7f1fc0 2732 for (i = 0; i+2 <= name->size; )
284f4730
JB
2733 switch (name->data[i])
2734 {
0a7f1fc0
JB
2735#define SINGLE_LETTER_MOD(bit) \
2736 if (name->data[i+1] != '-') \
2737 goto no_more_modifiers; \
2738 modifiers |= bit; \
fce33686 2739 i += 2;
0a7f1fc0
JB
2740
2741 case 'A':
2742 SINGLE_LETTER_MOD (alt_modifier);
284f4730
JB
2743 break;
2744
2745 case 'C':
0a7f1fc0 2746 SINGLE_LETTER_MOD (ctrl_modifier);
fce33686
JB
2747 break;
2748
2749 case 'H':
0a7f1fc0 2750 SINGLE_LETTER_MOD (hyper_modifier);
fce33686
JB
2751 break;
2752
2753 case 'M':
0a7f1fc0 2754 SINGLE_LETTER_MOD (meta_modifier);
284f4730
JB
2755 break;
2756
2757 case 'S':
0a7f1fc0 2758 SINGLE_LETTER_MOD (shift_modifier);
fce33686
JB
2759 break;
2760
2761 case 's':
86e5706b 2762 SINGLE_LETTER_MOD (super_modifier);
284f4730
JB
2763 break;
2764
fce33686
JB
2765 case 'd':
2766 if (i + 5 > name->size)
2767 goto no_more_modifiers;
2768 if (! strncmp (name->data + i, "drag-", 5))
2769 {
fce33686
JB
2770 modifiers |= drag_modifier;
2771 i += 5;
2772 }
2773 else if (! strncmp (name->data + i, "down-", 5))
2774 {
fce33686
JB
2775 modifiers |= down_modifier;
2776 i += 5;
2777 }
fbcd35bd
JB
2778 else if (i + 7 <= name->size
2779 && ! strncmp (name->data + i, "double-", 7))
2780 {
2781 modifiers |= double_modifier;
2782 i += 7;
2783 }
2784 else
2785 goto no_more_modifiers;
2786 break;
2787
2788 case 't':
2789 if (i + 7 > name->size)
2790 goto no_more_modifiers;
2791 if (! strncmp (name->data + i, "triple-", 7))
2792 {
2793 modifiers |= triple_modifier;
2794 i += 7;
2795 }
fce33686
JB
2796 else
2797 goto no_more_modifiers;
284f4730
JB
2798 break;
2799
2800 default:
2801 goto no_more_modifiers;
0a7f1fc0
JB
2802
2803#undef SINGLE_LETTER_MOD
284f4730
JB
2804 }
2805 no_more_modifiers:
2806
0a7f1fc0 2807 /* Should we include the `click' modifier? */
fbcd35bd
JB
2808 if (! (modifiers & (down_modifier | drag_modifier
2809 | double_modifier | triple_modifier))
0a7f1fc0 2810 && i + 7 == name->size
4bb994d1 2811 && strncmp (name->data + i, "mouse-", 6) == 0
6569cc8d 2812 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
0a7f1fc0
JB
2813 modifiers |= click_modifier;
2814
2815 if (modifier_end)
2816 *modifier_end = i;
2817
2818 return modifiers;
2819}
2820
2821
2822/* Return a symbol whose name is the modifier prefixes for MODIFIERS
2823 prepended to the string BASE[0..BASE_LEN-1].
2824 This doesn't use any caches. */
2825static Lisp_Object
2826apply_modifiers_uncached (modifiers, base, base_len)
2827 int modifiers;
2828 char *base;
2829 int base_len;
2830{
2831 /* Since BASE could contain nulls, we can't use intern here; we have
2832 to use Fintern, which expects a genuine Lisp_String, and keeps a
2833 reference to it. */
2834 char *new_mods =
fbcd35bd 2835 (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
0a7f1fc0 2836 int mod_len;
284f4730 2837
284f4730 2838 {
0a7f1fc0
JB
2839 char *p = new_mods;
2840
2841 /* Only the event queue may use the `up' modifier; it should always
2842 be turned into a click or drag event before presented to lisp code. */
2843 if (modifiers & up_modifier)
2844 abort ();
2845
2846 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
2847 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
2848 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
2849 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
2850 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
86e5706b 2851 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
fbcd35bd
JB
2852 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
2853 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
559f9d04
RS
2854 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
2855 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
0a7f1fc0
JB
2856 /* The click modifier is denoted by the absence of other modifiers. */
2857
2858 *p = '\0';
2859
2860 mod_len = p - new_mods;
2861 }
284f4730 2862
0a7f1fc0
JB
2863 {
2864 Lisp_Object new_name = make_uninit_string (mod_len + base_len);
2865
2866 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
2867 bcopy (base, XSTRING (new_name)->data + mod_len, base_len);
284f4730
JB
2868
2869 return Fintern (new_name, Qnil);
2870 }
2871}
2872
2873
0a7f1fc0
JB
2874static char *modifier_names[] =
2875{
fbcd35bd 2876 "up", "down", "drag", "click", "double", "triple", 0, 0,
1dfdf9e2 2877 0, 0, 0, 0, 0, 0, 0, 0,
86e5706b 2878 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
0a7f1fc0 2879};
80645119 2880#define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
0a7f1fc0
JB
2881
2882static Lisp_Object modifier_symbols;
2883
2884/* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
2885static Lisp_Object
2886lispy_modifier_list (modifiers)
2887 int modifiers;
2888{
2889 Lisp_Object modifier_list;
2890 int i;
2891
2892 modifier_list = Qnil;
80645119 2893 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
0a7f1fc0 2894 if (modifiers & (1<<i))
80645119
JB
2895 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
2896 modifier_list);
0a7f1fc0
JB
2897
2898 return modifier_list;
2899}
2900
2901
2902/* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
2903 where UNMODIFIED is the unmodified form of SYMBOL,
2904 MASK is the set of modifiers present in SYMBOL's name.
2905 This is similar to parse_modifiers_uncached, but uses the cache in
2906 SYMBOL's Qevent_symbol_element_mask property, and maintains the
2907 Qevent_symbol_elements property. */
2908static Lisp_Object
2909parse_modifiers (symbol)
2910 Lisp_Object symbol;
2911{
2912 Lisp_Object elements = Fget (symbol, Qevent_symbol_element_mask);
2913
2914 if (CONSP (elements))
2915 return elements;
2916 else
2917 {
2918 int end;
2919 int modifiers = parse_modifiers_uncached (symbol, &end);
2920 Lisp_Object unmodified
2921 = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
2922 XSYMBOL (symbol)->name->size - end),
2923 Qnil);
2924 Lisp_Object mask;
2925
734fef94
JB
2926 if (modifiers & ~((1<<VALBITS) - 1))
2927 abort ();
0a7f1fc0
JB
2928 XFASTINT (mask) = modifiers;
2929 elements = Fcons (unmodified, Fcons (mask, Qnil));
2930
2931 /* Cache the parsing results on SYMBOL. */
2932 Fput (symbol, Qevent_symbol_element_mask,
2933 elements);
2934 Fput (symbol, Qevent_symbol_elements,
2935 Fcons (unmodified, lispy_modifier_list (modifiers)));
2936
2937 /* Since we know that SYMBOL is modifiers applied to unmodified,
2938 it would be nice to put that in unmodified's cache.
2939 But we can't, since we're not sure that parse_modifiers is
2940 canonical. */
2941
2942 return elements;
2943 }
2944}
2945
2946/* Apply the modifiers MODIFIERS to the symbol BASE.
2947 BASE must be unmodified.
2948
2949 This is like apply_modifiers_uncached, but uses BASE's
2950 Qmodifier_cache property, if present. It also builds
cd21b839
JB
2951 Qevent_symbol_elements properties, since it has that info anyway.
2952
2953 apply_modifiers copies the value of BASE's Qevent_kind property to
2954 the modified symbol. */
0a7f1fc0
JB
2955static Lisp_Object
2956apply_modifiers (modifiers, base)
2957 int modifiers;
2958 Lisp_Object base;
2959{
7b4aedb9 2960 Lisp_Object cache, index, entry, new_symbol;
0a7f1fc0 2961
80645119
JB
2962 /* Mask out upper bits. We don't know where this value's been. */
2963 modifiers &= (1<<VALBITS) - 1;
2964
0a7f1fc0 2965 /* The click modifier never figures into cache indices. */
0a7f1fc0 2966 cache = Fget (base, Qmodifier_cache);
cd21b839 2967 XFASTINT (index) = (modifiers & ~click_modifier);
0a7f1fc0
JB
2968 entry = Fassq (index, cache);
2969
2970 if (CONSP (entry))
7b4aedb9
JB
2971 new_symbol = XCONS (entry)->cdr;
2972 else
2973 {
2974 /* We have to create the symbol ourselves. */
2975 new_symbol = apply_modifiers_uncached (modifiers,
2976 XSYMBOL (base)->name->data,
2977 XSYMBOL (base)->name->size);
2978
2979 /* Add the new symbol to the base's cache. */
2980 entry = Fcons (index, new_symbol);
2981 Fput (base, Qmodifier_cache, Fcons (entry, cache));
2982
2983 /* We have the parsing info now for free, so add it to the caches. */
2984 XFASTINT (index) = modifiers;
2985 Fput (new_symbol, Qevent_symbol_element_mask,
2986 Fcons (base, Fcons (index, Qnil)));
2987 Fput (new_symbol, Qevent_symbol_elements,
2988 Fcons (base, lispy_modifier_list (modifiers)));
2989 }
0a7f1fc0 2990
7b4aedb9
JB
2991 /* Make sure this symbol is of the same kind as BASE.
2992
2993 You'd think we could just set this once and for all when we
2994 intern the symbol above, but reorder_modifiers may call us when
2995 BASE's property isn't set right; we can't assume that just
80645119
JB
2996 because it has a Qmodifier_cache property it must have its
2997 Qevent_kind set right as well. */
7b4aedb9
JB
2998 if (NILP (Fget (new_symbol, Qevent_kind)))
2999 {
3000 Lisp_Object kind = Fget (base, Qevent_kind);
3001
3002 if (! NILP (kind))
3003 Fput (new_symbol, Qevent_kind, kind);
3004 }
3005
3006 return new_symbol;
0a7f1fc0
JB
3007}
3008
3009
3010/* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
3011 return a symbol with the modifiers placed in the canonical order.
3012 Canonical order is alphabetical, except for down and drag, which
3013 always come last. The 'click' modifier is never written out.
3014
3015 Fdefine_key calls this to make sure that (for example) C-M-foo
3016 and M-C-foo end up being equivalent in the keymap. */
3017
3018Lisp_Object
3019reorder_modifiers (symbol)
3020 Lisp_Object symbol;
3021{
3022 /* It's hopefully okay to write the code this way, since everything
3023 will soon be in caches, and no consing will be done at all. */
3024 Lisp_Object parsed = parse_modifiers (symbol);
3025
3026 return apply_modifiers (XCONS (XCONS (parsed)->cdr)->car,
3027 XCONS (parsed)->car);
3028}
3029
3030
284f4730
JB
3031/* For handling events, we often want to produce a symbol whose name
3032 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
3033 to some base, like the name of a function key or mouse button.
3034 modify_event_symbol produces symbols of this sort.
3035
3036 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
3037 is the name of the i'th symbol. TABLE_SIZE is the number of elements
3038 in the table.
3039
80e4aa30
RS
3040 Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
3041 NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
3042
284f4730
JB
3043 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
3044 persist between calls to modify_event_symbol that it can use to
3045 store a cache of the symbols it's generated for this NAME_TABLE
80e4aa30 3046 before. The object stored there may be a vector or an alist.
284f4730
JB
3047
3048 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
3049
3050 MODIFIERS is a set of modifier bits (as given in struct input_events)
3051 whose prefixes should be applied to the symbol name.
3052
3053 SYMBOL_KIND is the value to be placed in the event_kind property of
88cb0656
JB
3054 the returned symbol.
3055
3056 The symbols we create are supposed to have an
eb8c3be9 3057 `event-symbol-elements' property, which lists the modifiers present
88cb0656
JB
3058 in the symbol's name. */
3059
284f4730 3060static Lisp_Object
80e4aa30
RS
3061modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
3062 name_table, symbol_table, table_size)
284f4730
JB
3063 int symbol_num;
3064 unsigned modifiers;
3065 Lisp_Object symbol_kind;
80e4aa30 3066 Lisp_Object name_alist;
284f4730
JB
3067 char **name_table;
3068 Lisp_Object *symbol_table;
3069 int table_size;
3070{
80e4aa30
RS
3071 Lisp_Object value;
3072 Lisp_Object symbol_int;
3073
3074 XSET (symbol_int, Lisp_Int, symbol_num);
284f4730
JB
3075
3076 /* Is this a request for a valid symbol? */
88cb0656 3077 if (symbol_num < 0 || symbol_num >= table_size)
284f4730
JB
3078 abort ();
3079
80e4aa30
RS
3080 if (CONSP (*symbol_table))
3081 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
3082
0a7f1fc0 3083 /* If *symbol_table doesn't seem to be initialized properly, fix that.
88cb0656 3084 *symbol_table should be a lisp vector TABLE_SIZE elements long,
4bb994d1
JB
3085 where the Nth element is the symbol for NAME_TABLE[N], or nil if
3086 we've never used that symbol before. */
80e4aa30 3087 else
88cb0656 3088 {
80e4aa30
RS
3089 if (! VECTORP (*symbol_table)
3090 || XVECTOR (*symbol_table)->size != table_size)
3091 {
3092 Lisp_Object size;
0a7f1fc0 3093
80e4aa30
RS
3094 XFASTINT (size) = table_size;
3095 *symbol_table = Fmake_vector (size, Qnil);
3096 }
284f4730 3097
80e4aa30
RS
3098 value = XVECTOR (*symbol_table)->contents[symbol_num];
3099 }
284f4730 3100
0a7f1fc0 3101 /* Have we already used this symbol before? */
80e4aa30 3102 if (NILP (value))
284f4730 3103 {
0a7f1fc0 3104 /* No; let's create it. */
80e4aa30
RS
3105 if (!NILP (name_alist))
3106 value = Fassq (symbol_int, name_alist);
3107 else if (name_table[symbol_num])
3108 value = intern (name_table[symbol_num]);
d1f50460
RS
3109 else
3110 {
3111 char buf[20];
3112 sprintf (buf, "key-%d", symbol_num);
80e4aa30 3113 value = intern (buf);
d1f50460 3114 }
0a7f1fc0 3115
80e4aa30
RS
3116 if (CONSP (*symbol_table))
3117 *symbol_table = Fcons (value, *symbol_table);
3118 else
3119 XVECTOR (*symbol_table)->contents[symbol_num] = value;
3120
0a7f1fc0
JB
3121 /* Fill in the cache entries for this symbol; this also
3122 builds the Qevent_symbol_elements property, which the user
3123 cares about. */
80e4aa30
RS
3124 apply_modifiers (modifiers & click_modifier, value);
3125 Fput (value, Qevent_kind, symbol_kind);
284f4730 3126 }
88cb0656 3127
0a7f1fc0 3128 /* Apply modifiers to that symbol. */
80e4aa30 3129 return apply_modifiers (modifiers, value);
284f4730 3130}
0a7f1fc0 3131
284f4730 3132\f
284f4730
JB
3133/* Store into *addr a value nonzero if terminal input chars are available.
3134 Serves the purpose of ioctl (0, FIONREAD, addr)
3135 but works even if FIONREAD does not exist.
3136 (In fact, this may actually read some input.) */
3137
3138static void
3139get_input_pending (addr)
3140 int *addr;
3141{
3142 /* First of all, have we already counted some input? */
3143 *addr = !NILP (Vquit_flag) || readable_events ();
3144
3145 /* If input is being read as it arrives, and we have none, there is none. */
3146 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
3147 return;
3148
3149 /* Try to read some input and see how much we get. */
3150 gobble_input (0);
3151 *addr = !NILP (Vquit_flag) || readable_events ();
3152}
3153
3154/* Interface to read_avail_input, blocking SIGIO if necessary. */
3155
3156int
3157gobble_input (expected)
3158 int expected;
3159{
3160#ifndef VMS
3161#ifdef SIGIO
3162 if (interrupt_input)
3163 {
32676c08 3164 SIGMASKTYPE mask;
e065a56e 3165 mask = sigblockx (SIGIO);
284f4730 3166 read_avail_input (expected);
e065a56e 3167 sigsetmask (mask);
284f4730
JB
3168 }
3169 else
3170#endif
3171 read_avail_input (expected);
3172#endif
3173}
3174\f
3175#ifndef VMS
3176
3177/* Read any terminal input already buffered up by the system
3178 into the kbd_buffer, but do not wait.
3179
3180 EXPECTED should be nonzero if the caller knows there is some input.
3181
3182 Except on VMS, all input is read by this function.
3183 If interrupt_input is nonzero, this function MUST be called
3184 only when SIGIO is blocked.
3185
3186 Returns the number of keyboard chars read, or -1 meaning
3187 this is a bad time to try to read input. */
3188
3189static int
3190read_avail_input (expected)
3191 int expected;
3192{
3193 struct input_event buf[KBD_BUFFER_SIZE];
3194 register int i;
3195 int nread;
3196
3197 if (read_socket_hook)
3198 /* No need for FIONREAD or fcntl; just say don't wait. */
3199 nread = (*read_socket_hook) (0, buf, KBD_BUFFER_SIZE, expected, expected);
3200 else
3201 {
17270835
RS
3202 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
3203 the kbd_buffer can really hold. That may prevent loss
3204 of characters on some systems when input is stuffed at us. */
3205 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
284f4730 3206
80e4aa30
RS
3207#ifdef MSDOS
3208 nread = dos_keysns ();
3209 if (nread == 0) return 0;
3210#else */ not MSDOS */
284f4730
JB
3211#ifdef FIONREAD
3212 /* Find out how much input is available. */
3213 if (ioctl (0, FIONREAD, &nread) < 0)
3214 /* Formerly simply reported no input, but that sometimes led to
3215 a failure of Emacs to terminate.
3216 SIGHUP seems appropriate if we can't reach the terminal. */
e4535288
RS
3217 /* ??? Is it really right to send the signal just to this process
3218 rather than to the whole process group?
3219 Perhaps on systems with FIONREAD Emacs is alone in its group. */
284f4730
JB
3220 kill (getpid (), SIGHUP);
3221 if (nread == 0)
3222 return 0;
3223 if (nread > sizeof cbuf)
3224 nread = sizeof cbuf;
3225#else /* no FIONREAD */
482952ef 3226#if defined(USG) || defined(DGUX)
284f4730
JB
3227 /* Read some input if available, but don't wait. */
3228 nread = sizeof cbuf;
3229 fcntl (fileno (stdin), F_SETFL, O_NDELAY);
3230#else
3231 you lose;
3232#endif
3233#endif
80e4aa30 3234#endif /* not MSDOS */
284f4730
JB
3235
3236 /* Now read; for one reason or another, this will not block. */
3237 while (1)
3238 {
80e4aa30
RS
3239#ifdef MSDOS
3240 cbuf[0] = dos_keyread();
3241 nread = 1;
3242#else
854f3a54 3243 nread = read (fileno (stdin), cbuf, nread);
80e4aa30 3244#endif
284f4730
JB
3245#ifdef AIX
3246 /* The kernel sometimes fails to deliver SIGHUP for ptys.
3247 This looks incorrect, but it isn't, because _BSD causes
3248 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
3249 and that causes a value other than 0 when there is no input. */
854f3a54 3250 if (nread == 0)
80e4aa30 3251 kill (0, SIGHUP);
284f4730 3252#endif
854f3a54
RS
3253 /* This code is wrong, but at least it gets the right results.
3254 Fix it for 19.23. */
284f4730 3255 /* Retry the read if it is interrupted. */
854f3a54 3256 if (nread >= 0
80e4aa30 3257#ifdef EFAULT
284f4730 3258 || ! (errno == EAGAIN || errno == EFAULT
80e4aa30
RS
3259#else
3260 || ! (errno == EAGAIN
3261#endif
284f4730
JB
3262#ifdef EBADSLT
3263 || errno == EBADSLT
3264#endif
3265 ))
854f3a54 3266 break;
284f4730
JB
3267 }
3268
3269#ifndef FIONREAD
02c2c53f 3270#if defined (USG) || defined (DGUX)
284f4730 3271 fcntl (fileno (stdin), F_SETFL, 0);
02c2c53f 3272#endif /* USG or DGUX */
284f4730
JB
3273#endif /* no FIONREAD */
3274 for (i = 0; i < nread; i++)
3275 {
3276 buf[i].kind = ascii_keystroke;
86e5706b 3277 buf[i].modifiers = 0;
b04904fb 3278 if (meta_key == 1 && (cbuf[i] & 0x80))
86e5706b 3279 buf[i].modifiers = meta_modifier;
b04904fb
RS
3280 if (meta_key != 2)
3281 cbuf[i] &= ~0x80;
86e5706b 3282
7b4aedb9
JB
3283 XSET (buf[i].code, Lisp_Int, cbuf[i]);
3284#ifdef MULTI_FRAME
3285 XSET (buf[i].frame_or_window, Lisp_Frame, selected_frame);
3286#else
3287 buf[i].frame_or_window = Qnil;
3288#endif
284f4730
JB
3289 }
3290 }
3291
3292 /* Scan the chars for C-g and store them in kbd_buffer. */
3293 for (i = 0; i < nread; i++)
3294 {
3295 kbd_buffer_store_event (&buf[i]);
3296 /* Don't look at input that follows a C-g too closely.
3297 This reduces lossage due to autorepeat on C-g. */
3298 if (buf[i].kind == ascii_keystroke
3299 && XINT(buf[i].code) == quit_char)
3300 break;
3301 }
3302
3303 return nread;
3304}
3305#endif /* not VMS */
3306\f
3307#ifdef SIGIO /* for entire page */
3308/* Note SIGIO has been undef'd if FIONREAD is missing. */
3309
2ce30ea2 3310SIGTYPE
284f4730
JB
3311input_available_signal (signo)
3312 int signo;
3313{
3314 /* Must preserve main program's value of errno. */
3315 int old_errno = errno;
3316#ifdef BSD4_1
3317 extern int select_alarmed;
3318#endif
3319
3320#ifdef USG
3321 /* USG systems forget handlers when they are used;
3322 must reestablish each time */
3323 signal (signo, input_available_signal);
3324#endif /* USG */
3325
3326#ifdef BSD4_1
3327 sigisheld (SIGIO);
3328#endif
3329
ffd56f97
JB
3330 if (input_available_clear_time)
3331 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
284f4730
JB
3332
3333 while (1)
3334 {
3335 int nread;
3336 nread = read_avail_input (1);
3337 /* -1 means it's not ok to read the input now.
3338 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
3339 0 means there was no keyboard input available. */
3340 if (nread <= 0)
3341 break;
3342
3343#ifdef BSD4_1
3344 select_alarmed = 1; /* Force the select emulator back to life */
3345#endif
3346 }
3347
3348#ifdef BSD4_1
3349 sigfree ();
3350#endif
3351 errno = old_errno;
3352}
3353#endif /* SIGIO */
ad163903
JB
3354
3355/* Send ourselves a SIGIO.
3356
3357 This function exists so that the UNBLOCK_INPUT macro in
3358 blockinput.h can have some way to take care of input we put off
3359 dealing with, without assuming that every file which uses
3360 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
3361void
3362reinvoke_input_signal ()
3363{
3364#ifdef SIGIO
3365 kill (0, SIGIO);
3366#endif
3367}
3368
3369
284f4730
JB
3370\f
3371/* Return the prompt-string of a sparse keymap.
3372 This is the first element which is a string.
3373 Return nil if there is none. */
3374
3375Lisp_Object
3376map_prompt (map)
3377 Lisp_Object map;
3378{
3379 while (CONSP (map))
3380 {
3381 register Lisp_Object tem;
3382 tem = Fcar (map);
3383 if (XTYPE (tem) == Lisp_String)
3384 return tem;
3385 map = Fcdr (map);
3386 }
3387 return Qnil;
3388}
3389
5ec75a55
RS
3390static Lisp_Object menu_bar_item ();
3391static Lisp_Object menu_bar_one_keymap ();
3392
3393/* Return a list of menu items for a menu bar, appropriate
3394 to the current buffer.
3395 The elements have the form (KEY STRING . nil). */
3396
3397Lisp_Object
3398menu_bar_items ()
3399{
3400 /* The number of keymaps we're scanning right now, and the number of
3401 keymaps we have allocated space for. */
3402 int nmaps;
3403
3404 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
3405 in the current keymaps, or nil where it is not a prefix. */
3406 Lisp_Object *maps;
3407
9f9c0e27 3408 Lisp_Object def, tem, tail;
5ec75a55
RS
3409
3410 Lisp_Object result;
3411
3412 int mapno;
47d319aa 3413 Lisp_Object oquit;
5ec75a55 3414
db60d856
JB
3415 /* In order to build the menus, we need to call the keymap
3416 accessors. They all call QUIT. But this function is called
3417 during redisplay, during which a quit is fatal. So inhibit
47d319aa
RS
3418 quitting while building the menus.
3419 We do this instead of specbind because (1) errors will clear it anyway
3420 and (2) this avoids risk of specpdl overflow. */
3421 oquit = Vinhibit_quit;
3422 Vinhibit_quit = Qt;
db60d856 3423
5ec75a55
RS
3424 /* Build our list of keymaps.
3425 If we recognize a function key and replace its escape sequence in
3426 keybuf with its symbol, or if the sequence starts with a mouse
3427 click and we need to switch buffers, we jump back here to rebuild
3428 the initial keymaps from the current buffer. */
3429 {
3430 Lisp_Object *tmaps;
3431
3432 nmaps = current_minor_maps (0, &tmaps) + 2;
3433 maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
3434 bcopy (tmaps, maps, (nmaps - 2) * sizeof (maps[0]));
3435#ifdef USE_TEXT_PROPERTIES
3436 maps[nmaps-2] = get_local_map (PT, current_buffer);
3437#else
a6d53864 3438 maps[nmaps-2] = current_buffer->keymap;
5ec75a55 3439#endif
7e6992e0 3440 maps[nmaps-1] = current_global_map;
5ec75a55
RS
3441 }
3442
3443 /* Look up in each map the dummy prefix key `menu-bar'. */
3444
3445 result = Qnil;
3446
e58aa385 3447 for (mapno = nmaps - 1; mapno >= 0; mapno--)
5ec75a55
RS
3448 {
3449 if (! NILP (maps[mapno]))
e74fbc70 3450 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0));
5ec75a55
RS
3451 else
3452 def = Qnil;
3453
3454 tem = Fkeymapp (def);
3455 if (!NILP (tem))
3456 result = menu_bar_one_keymap (def, result);
3457 }
3458
9f9c0e27
RS
3459 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCONS (tail)->cdr)
3460 {
3461 Lisp_Object elt;
3462
3463 elt = Fassq (XCONS (tail)->car, result);
3464 if (!NILP (elt))
3465 result = Fcons (elt, Fdelq (elt, result));
3466 }
a73c5e29 3467
85e28fa2 3468 result = Fnreverse (result);
47d319aa 3469 Vinhibit_quit = oquit;
85e28fa2 3470 return result;
5ec75a55
RS
3471}
3472\f
3473/* Scan one map KEYMAP, accumulating any menu items it defines
3474 that have not yet been seen in RESULT. Return the updated RESULT. */
3475
3476static Lisp_Object
3477menu_bar_one_keymap (keymap, result)
3478 Lisp_Object keymap, result;
3479{
3480 Lisp_Object tail, item, key, binding, item_string, table;
3481
3482 /* Loop over all keymap entries that have menu strings. */
3483 for (tail = keymap; XTYPE (tail) == Lisp_Cons; tail = XCONS (tail)->cdr)
3484 {
3485 item = XCONS (tail)->car;
3486 if (XTYPE (item) == Lisp_Cons)
3487 {
3488 key = XCONS (item)->car;
3489 binding = XCONS (item)->cdr;
3490 if (XTYPE (binding) == Lisp_Cons)
3491 {
3492 item_string = XCONS (binding)->car;
3493 if (XTYPE (item_string) == Lisp_String)
3494 result = menu_bar_item (key, item_string,
3495 Fcdr (binding), result);
3496 }
e58aa385
RS
3497 else if (EQ (binding, Qundefined))
3498 result = menu_bar_item (key, item_string,
ae450a95 3499 binding, result);
5ec75a55
RS
3500 }
3501 else if (XTYPE (item) == Lisp_Vector)
3502 {
3503 /* Loop over the char values represented in the vector. */
3504 int len = XVECTOR (item)->size;
3505 int c;
3506 for (c = 0; c < len; c++)
3507 {
3508 Lisp_Object character;
3509 XFASTINT (character) = c;
3510 binding = XVECTOR (item)->contents[c];
3511 if (XTYPE (binding) == Lisp_Cons)
3512 {
3513 item_string = XCONS (binding)->car;
3514 if (XTYPE (item_string) == Lisp_String)
3515 result = menu_bar_item (key, item_string,
3516 Fcdr (binding), result);
3517 }
e58aa385
RS
3518 else if (EQ (binding, Qundefined))
3519 result = menu_bar_item (key, item_string,
ae450a95 3520 binding, result);
5ec75a55
RS
3521 }
3522 }
3523 }
3524
3525 return result;
3526}
3527
3528static Lisp_Object
3529menu_bar_item (key, item_string, def, result)
3530 Lisp_Object key, item_string, def, result;
3531{
e58aa385 3532 Lisp_Object tem;
5ec75a55
RS
3533 Lisp_Object enabled;
3534
e58aa385
RS
3535 if (EQ (def, Qundefined))
3536 {
3537 /* If a map has an explicit nil as definition,
3538 discard any previously made menu bar item. */
3539 tem = Fassq (key, result);
3540 return Fdelq (tem, result);
3541 }
3542
5ec75a55
RS
3543 /* See if this entry is enabled. */
3544 enabled = Qt;
3545
3546 if (XTYPE (def) == Lisp_Symbol)
3547 {
3548 /* No property, or nil, means enable.
3549 Otherwise, enable if value is not nil. */
3550 tem = Fget (def, Qmenu_enable);
3551 if (!NILP (tem))
3552 enabled = Feval (tem);
3553 }
3554
3555 /* Add an entry for this key and string
3556 if there is none yet. */
e58aa385
RS
3557 tem = Fassq (key, result);
3558 if (!NILP (enabled) && NILP (tem))
5ec75a55
RS
3559 result = Fcons (Fcons (key, Fcons (item_string, Qnil)), result);
3560
3561 return result;
3562}
3563\f
dcc408a0
RS
3564/* Read a character using menus based on maps in the array MAPS.
3565 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
3566 Return t if we displayed a menu but the user rejected it.
7d6de002
RS
3567
3568 PREV_EVENT is the previous input event, or nil if we are reading
3569 the first event of a key sequence.
3570
6569cc8d
JB
3571 If USED_MOUSE_MENU is non-zero, then we set *USED_MOUSE_MENU to 1
3572 if we used a mouse menu to read the input, or zero otherwise. If
3573 USED_MOUSE_MENU is zero, *USED_MOUSE_MENU is left alone.
284f4730
JB
3574
3575 The prompting is done based on the prompt-string of the map
3576 and the strings associated with various map elements. */
3577
7617111f 3578static Lisp_Object
7d6de002
RS
3579read_char_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
3580 int nmaps;
3581 Lisp_Object *maps;
3582 Lisp_Object prev_event;
3583 int *used_mouse_menu;
284f4730 3584{
7d6de002
RS
3585 int mapno;
3586 register Lisp_Object name;
284f4730 3587 int nlength;
ff11dfa1 3588 int width = FRAME_WIDTH (selected_frame) - 4;
7d6de002
RS
3589 char *menu = (char *) alloca (width + 4);
3590 int idx = -1;
3591 Lisp_Object rest, vector;
3592
6569cc8d
JB
3593 if (used_mouse_menu)
3594 *used_mouse_menu = 0;
284f4730
JB
3595
3596 /* Use local over global Menu maps */
3597
7d6de002
RS
3598 if (! menu_prompting)
3599 return Qnil;
3600
3601 /* Get the menu name from the first map that has one (a prompt string). */
3602 for (mapno = 0; mapno < nmaps; mapno++)
3603 {
3604 name = map_prompt (maps[mapno]);
3605 if (!NILP (name))
3606 break;
3607 }
284f4730 3608
7d6de002 3609 /* If we don't have any menus, just read a character normally. */
dbc4e1c1 3610 if (mapno >= nmaps)
7d6de002
RS
3611 return Qnil;
3612
dbc4e1c1
JB
3613#ifdef HAVE_X_WINDOWS
3614#ifdef HAVE_X_MENU
7d6de002
RS
3615 /* If we got to this point via a mouse click,
3616 use a real menu for mouse selection. */
dbc4e1c1 3617 if (EVENT_HAS_PARAMETERS (prev_event))
7d6de002
RS
3618 {
3619 /* Display the menu and get the selection. */
3620 Lisp_Object *realmaps
3621 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
3622 Lisp_Object value;
3623 int nmaps1 = 0;
3624
3625 /* Use the maps that are not nil. */
3626 for (mapno = 0; mapno < nmaps; mapno++)
3627 if (!NILP (maps[mapno]))
3628 realmaps[nmaps1++] = maps[mapno];
3629
3630 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
663258f2
JB
3631 if (CONSP (value))
3632 {
3633 /* If we got more than one event, put all but the first
3634 onto this list to be read later.
3635 Return just the first event now. */
3636 unread_command_events
3637 = nconc2 (XCONS (value)->cdr, unread_command_events);
3638 value = XCONS (value)->car;
3639 }
7d6de002 3640 if (NILP (value))
dcc408a0 3641 value = Qt;
6569cc8d
JB
3642 if (used_mouse_menu)
3643 *used_mouse_menu = 1;
7d6de002
RS
3644 return value;
3645 }
dbc4e1c1
JB
3646#endif /* HAVE_X_MENU */
3647#endif /* HAVE_X_WINDOWS */
284f4730
JB
3648
3649 /* Prompt string always starts with map's prompt, and a space. */
3650 strcpy (menu, XSTRING (name)->data);
3651 nlength = XSTRING (name)->size;
7d6de002 3652 menu[nlength++] = ':';
284f4730
JB
3653 menu[nlength++] = ' ';
3654 menu[nlength] = 0;
3655
7d6de002
RS
3656 /* Start prompting at start of first map. */
3657 mapno = 0;
3658 rest = maps[mapno];
284f4730
JB
3659
3660 /* Present the documented bindings, a line at a time. */
3661 while (1)
3662 {
3663 int notfirst = 0;
3664 int i = nlength;
3665 Lisp_Object obj;
3666 int ch;
3667
284f4730 3668 /* Loop over elements of map. */
7d6de002 3669 while (i < width)
284f4730 3670 {
7d6de002 3671 Lisp_Object s, elt;
284f4730 3672
7d6de002
RS
3673 /* If reached end of map, start at beginning of next map. */
3674 if (NILP (rest))
3675 {
3676 mapno++;
3677 /* At end of last map, wrap around to first map if just starting,
3678 or end this line if already have something on it. */
3679 if (mapno == nmaps)
284f4730 3680 {
7d6de002
RS
3681 if (notfirst)
3682 break;
3683 else
3684 mapno = 0;
284f4730 3685 }
7d6de002 3686 rest = maps[mapno];
284f4730 3687 }
7d6de002
RS
3688
3689 /* Look at the next element of the map. */
3690 if (idx >= 0)
3691 elt = XVECTOR (vector)->contents[idx];
284f4730 3692 else
7d6de002
RS
3693 elt = Fcar_safe (rest);
3694
3695 if (idx < 0 && XTYPE (elt) == Lisp_Vector)
284f4730 3696 {
7d6de002
RS
3697 /* If we found a dense table in the keymap,
3698 advanced past it, but start scanning its contents. */
3699 rest = Fcdr_safe (rest);
3700 vector = elt;
3701 idx = 0;
284f4730 3702 }
7d6de002
RS
3703 else
3704 {
3705 /* An ordinary element. */
3706 s = Fcar_safe (Fcdr_safe (elt));
3707 if (XTYPE (s) != Lisp_String)
3708 /* Ignore the element if it has no prompt string. */
3709 ;
3710 /* If we have room for the prompt string, add it to this line.
3711 If this is the first on the line, always add it. */
3712 else if (XSTRING (s)->size + i < width
3713 || !notfirst)
3714 {
3715 int thiswidth;
284f4730 3716
7d6de002
RS
3717 /* Punctuate between strings. */
3718 if (notfirst)
3719 {
3720 strcpy (menu + i, ", ");
3721 i += 2;
3722 }
3723 notfirst = 1;
3724
3725 /* Add as much of string as fits. */
3726 thiswidth = XSTRING (s)->size;
3727 if (thiswidth + i > width)
3728 thiswidth = width - i;
3729 bcopy (XSTRING (s)->data, menu + i, thiswidth);
3730 i += thiswidth;
3731 }
3732 else
3733 {
3734 /* If this element does not fit, end the line now,
3735 and save the element for the next line. */
3736 strcpy (menu + i, "...");
3737 break;
3738 }
3739
3740 /* Move past this element. */
3741 if (idx >= 0 && idx + 1 >= XVECTOR (rest)->size)
3742 /* Handle reaching end of dense table. */
3743 idx = -1;
3744 if (idx >= 0)
3745 idx++;
3746 else
3747 rest = Fcdr_safe (rest);
3748 }
284f4730
JB
3749 }
3750
3751 /* Prompt with that and read response. */
3752 message1 (menu);
7d6de002 3753 obj = read_char (1, 0, 0, Qnil, 0);
284f4730
JB
3754
3755 if (XTYPE (obj) != Lisp_Int)
3756 return obj;
3757 else
3758 ch = XINT (obj);
3759
f4255cd1 3760 if (! EQ (obj, menu_prompt_more_char)
284f4730 3761 && (XTYPE (menu_prompt_more_char) != Lisp_Int
f4255cd1 3762 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
284f4730
JB
3763 return obj;
3764 }
3765}
284f4730
JB
3766\f
3767/* Reading key sequences. */
3768
3769/* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
3770 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
3771 keymap, or nil otherwise. Return the index of the first keymap in
3772 which KEY has any binding, or NMAPS if no map has a binding.
3773
3774 If KEY is a meta ASCII character, treat it like meta-prefix-char
3775 followed by the corresponding non-meta character. Keymaps in
3776 CURRENT with non-prefix bindings for meta-prefix-char become nil in
3777 NEXT.
3778
3779 When KEY is not defined in any of the keymaps, if it is an upper
3780 case letter and there are bindings for the corresponding lower-case
3781 letter, return the bindings for the lower-case letter.
e9bf89a0
RS
3782 We store 1 in *CASE_CONVERTED in this case.
3783 Otherwise, we don't change *CASE_CONVERTED.
284f4730 3784
88cb0656
JB
3785 If KEY has no bindings in any of the CURRENT maps, NEXT is left
3786 unmodified.
3787
284f4730
JB
3788 NEXT may == CURRENT. */
3789
3790static int
e9bf89a0 3791follow_key (key, nmaps, current, defs, next, case_converted)
284f4730
JB
3792 Lisp_Object key;
3793 Lisp_Object *current, *defs, *next;
3794 int nmaps;
e9bf89a0 3795 int *case_converted;
284f4730
JB
3796{
3797 int i, first_binding;
3798
3799 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
3800 followed by the corresponding non-meta character. */
86e5706b 3801 if (XTYPE (key) == Lisp_Int && (XINT (key) & CHAR_META))
284f4730
JB
3802 {
3803 for (i = 0; i < nmaps; i++)
3804 if (! NILP (current[i]))
3805 {
cd21b839 3806 next[i] =
e74fbc70 3807 get_keyelt (access_keymap (current[i], meta_prefix_char, 1, 0));
284f4730
JB
3808
3809 /* Note that since we pass the resulting bindings through
3810 get_keymap_1, non-prefix bindings for meta-prefix-char
3811 disappear. */
f4255cd1 3812 next[i] = get_keymap_1 (next[i], 0, 1);
284f4730
JB
3813 }
3814 else
3815 next[i] = Qnil;
3816
3817 current = next;
86e5706b 3818 XSET (key, Lisp_Int, XFASTINT (key) & ~CHAR_META);
284f4730
JB
3819 }
3820
3821 first_binding = nmaps;
3822 for (i = nmaps - 1; i >= 0; i--)
3823 {
3824 if (! NILP (current[i]))
3825 {
e74fbc70 3826 defs[i] = get_keyelt (access_keymap (current[i], key, 1, 0));
284f4730
JB
3827 if (! NILP (defs[i]))
3828 first_binding = i;
3829 }
3830 else
3831 defs[i] = Qnil;
3832 }
3833
3834 /* When KEY is not defined in any of the keymaps, if it is an upper
3835 case letter and there are bindings for the corresponding
3836 lower-case letter, return the bindings for the lower-case letter. */
3837 if (first_binding == nmaps
3838 && XTYPE (key) == Lisp_Int
bb1ef421
RS
3839 && ((((XINT (key) & 0x3ffff)
3840 < XSTRING (current_buffer->downcase_table)->size)
3841 && UPPERCASEP (XINT (key) & 0x3ffff))
86e5706b 3842 || (XINT (key) & shift_modifier)))
284f4730 3843 {
86e5706b
RS
3844 if (XINT (key) & shift_modifier)
3845 XSETINT (key, XINT (key) & ~shift_modifier);
3846 else
398b666f
RS
3847 XSETINT (key, (DOWNCASE (XINT (key) & 0x3ffff)
3848 | (XINT (key) & ~0x3ffff)));
284f4730
JB
3849
3850 first_binding = nmaps;
3851 for (i = nmaps - 1; i >= 0; i--)
3852 {
3853 if (! NILP (current[i]))
3854 {
e74fbc70 3855 defs[i] = get_keyelt (access_keymap (current[i], key, 1, 0));
284f4730
JB
3856 if (! NILP (defs[i]))
3857 first_binding = i;
3858 }
3859 else
3860 defs[i] = Qnil;
3861 }
e9bf89a0
RS
3862 if (first_binding != nmaps)
3863 *case_converted = 1;
284f4730 3864 }
86e5706b 3865
284f4730 3866 /* Given the set of bindings we've found, produce the next set of maps. */
0a7f1fc0
JB
3867 if (first_binding < nmaps)
3868 for (i = 0; i < nmaps; i++)
f4255cd1 3869 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
284f4730
JB
3870
3871 return first_binding;
3872}
3873
f4255cd1
JB
3874/* Read a sequence of keys that ends with a non prefix character,
3875 storing it in KEYBUF, a buffer of size BUFSIZE.
3876 Prompt with PROMPT.
284f4730 3877 Return the length of the key sequence stored.
dcc408a0 3878 Return -1 if the user rejected a command menu.
284f4730 3879
f4255cd1
JB
3880 Echo starting immediately unless `prompt' is 0.
3881
3882 Where a key sequence ends depends on the currently active keymaps.
3883 These include any minor mode keymaps active in the current buffer,
3884 the current buffer's local map, and the global map.
3885
3886 If a key sequence has no other bindings, we check Vfunction_key_map
3887 to see if some trailing subsequence might be the beginning of a
3888 function key's sequence. If so, we try to read the whole function
3889 key, and substitute its symbolic name into the key sequence.
3890
fbcd35bd
JB
3891 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
3892 `double-' events into similar click events, if that would make them
3893 bound. We try to turn `triple-' events first into `double-' events,
3894 then into clicks.
f4255cd1
JB
3895
3896 If we get a mouse click in a mode line, vertical divider, or other
3897 non-text area, we treat the click as if it were prefixed by the
3898 symbol denoting that area - `mode-line', `vertical-line', or
3899 whatever.
3900
3901 If the sequence starts with a mouse click, we read the key sequence
3902 with respect to the buffer clicked on, not the current buffer.
284f4730 3903
f4255cd1
JB
3904 If the user switches frames in the midst of a key sequence, we put
3905 off the switch-frame event until later; the next call to
3906 read_char will return it. */
48e416d4 3907
284f4730
JB
3908static int
3909read_key_sequence (keybuf, bufsize, prompt)
3910 Lisp_Object *keybuf;
3911 int bufsize;
84d91fda 3912 Lisp_Object prompt;
284f4730 3913{
f4255cd1
JB
3914 int count = specpdl_ptr - specpdl;
3915
284f4730
JB
3916 /* How many keys there are in the current key sequence. */
3917 int t;
3918
284f4730
JB
3919 /* The length of the echo buffer when we started reading, and
3920 the length of this_command_keys when we started reading. */
3921 int echo_start;
f4255cd1 3922 int keys_start;
284f4730
JB
3923
3924 /* The number of keymaps we're scanning right now, and the number of
3925 keymaps we have allocated space for. */
3926 int nmaps;
3927 int nmaps_allocated = 0;
3928
284f4730
JB
3929 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
3930 the current keymaps. */
3931 Lisp_Object *defs;
3932
f4255cd1
JB
3933 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
3934 in the current keymaps, or nil where it is not a prefix. */
3935 Lisp_Object *submaps;
3936
3937 /* The index in defs[] of the first keymap that has a binding for
3938 this key sequence. In other words, the lowest i such that
3939 defs[i] is non-nil. */
284f4730
JB
3940 int first_binding;
3941
f4255cd1 3942 /* If t < mock_input, then KEYBUF[t] should be read as the next
253598e4
JB
3943 input key.
3944
3945 We use this to recover after recognizing a function key. Once we
3946 realize that a suffix of the current key sequence is actually a
3947 function key's escape sequence, we replace the suffix with the
3948 function key's binding from Vfunction_key_map. Now keybuf
f4255cd1
JB
3949 contains a new and different key sequence, so the echo area,
3950 this_command_keys, and the submaps and defs arrays are wrong. In
3951 this situation, we set mock_input to t, set t to 0, and jump to
3952 restart_sequence; the loop will read keys from keybuf up until
3953 mock_input, thus rebuilding the state; and then it will resume
3954 reading characters from the keyboard. */
284f4730
JB
3955 int mock_input = 0;
3956
253598e4 3957 /* If the sequence is unbound in submaps[], then
f4255cd1
JB
3958 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
3959 and fkey_map is its binding.
253598e4 3960
f4255cd1
JB
3961 These might be > t, indicating that all function key scanning
3962 should hold off until t reaches them. We do this when we've just
3963 recognized a function key, to avoid searching for the function
3964 key's again in Vfunction_key_map. */
284f4730 3965 int fkey_start = 0, fkey_end = 0;
4efda7dd 3966 Lisp_Object fkey_map;
284f4730 3967
a612e298
RS
3968 /* Likewise, for key_translation_map. */
3969 int keytran_start = 0, keytran_end = 0;
3970 Lisp_Object keytran_map;
3971
cd21b839
JB
3972 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
3973 we put it off for later. While we're reading, we keep the event here. */
4efda7dd 3974 Lisp_Object delayed_switch_frame;
cd21b839 3975
51763820
BF
3976 /* See the comment below... */
3977#if defined (GOBBLE_FIRST_EVENT)
4efda7dd 3978 Lisp_Object first_event;
51763820 3979#endif
4efda7dd 3980
3b9189f8
RS
3981 struct buffer *starting_buffer;
3982
e9bf89a0
RS
3983 /* Nonzero if we found the binding for one of the chars
3984 in this key sequence by downcasing it. */
3985 int case_converted = 0;
3986
3987 /* Nonzero if we seem to have got the beginning of a binding
3988 in function_key_map. */
3989 int function_key_possible = 0;
3990
4efda7dd
RS
3991 int junk;
3992
3993 last_nonmenu_event = Qnil;
3994
3995 delayed_switch_frame = Qnil;
3996 fkey_map = Vfunction_key_map;
a612e298 3997 keytran_map = Vkey_translation_map;
f4255cd1 3998
a612e298 3999 /* If there is no function-key-map, turn off function key scanning. */
f4255cd1
JB
4000 if (NILP (Fkeymapp (Vfunction_key_map)))
4001 fkey_start = fkey_end = bufsize + 1;
4002
a612e298
RS
4003 /* If there is no key-translation-map, turn off scanning. */
4004 if (NILP (Fkeymapp (Vkey_translation_map)))
4005 keytran_start = keytran_end = bufsize + 1;
4006
284f4730
JB
4007 if (INTERACTIVE)
4008 {
84d91fda
RS
4009 if (!NILP (prompt))
4010 echo_prompt (XSTRING (prompt)->data);
284f4730
JB
4011 else if (cursor_in_echo_area)
4012 /* This doesn't put in a dash if the echo buffer is empty, so
4013 you don't always see a dash hanging out in the minibuffer. */
4014 echo_dash ();
284f4730
JB
4015 }
4016
f4255cd1
JB
4017 /* Record the initial state of the echo area and this_command_keys;
4018 we will need to restore them if we replay a key sequence. */
0a7f1fc0 4019 if (INTERACTIVE)
f4255cd1
JB
4020 echo_start = echo_length ();
4021 keys_start = this_command_key_count;
0a7f1fc0 4022
51763820
BF
4023#if defined (GOBBLE_FIRST_EVENT)
4024 /* This doesn't quite work, because some of the things that read_char
4025 does cannot safely be bypassed. It seems too risky to try to make
4026 this work right. */
4027
4efda7dd
RS
4028 /* Read the first char of the sequence specially, before setting
4029 up any keymaps, in case a filter runs and switches buffers on us. */
84d91fda 4030 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
4efda7dd 4031 &junk);
51763820 4032#endif /* GOBBLE_FIRST_EVENT */
4efda7dd 4033
7b4aedb9
JB
4034 /* We jump here when the key sequence has been thoroughly changed, and
4035 we need to rescan it starting from the beginning. When we jump here,
4036 keybuf[0..mock_input] holds the sequence we should reread. */
07d2b8de 4037 replay_sequence:
7b4aedb9 4038
3b9189f8 4039 starting_buffer = current_buffer;
e9bf89a0
RS
4040 case_converted = 0;
4041 function_key_possible = 0;
3b9189f8 4042
f4255cd1 4043 /* Build our list of keymaps.
07d2b8de
JB
4044 If we recognize a function key and replace its escape sequence in
4045 keybuf with its symbol, or if the sequence starts with a mouse
4046 click and we need to switch buffers, we jump back here to rebuild
4047 the initial keymaps from the current buffer. */
284f4730
JB
4048 {
4049 Lisp_Object *maps;
4050
4051 nmaps = current_minor_maps (0, &maps) + 2;
4052 if (nmaps > nmaps_allocated)
4053 {
253598e4 4054 submaps = (Lisp_Object *) alloca (nmaps * sizeof (submaps[0]));
284f4730
JB
4055 defs = (Lisp_Object *) alloca (nmaps * sizeof (defs[0]));
4056 nmaps_allocated = nmaps;
4057 }
253598e4 4058 bcopy (maps, submaps, (nmaps - 2) * sizeof (submaps[0]));
497ba7a1
RS
4059#ifdef USE_TEXT_PROPERTIES
4060 submaps[nmaps-2] = get_local_map (PT, current_buffer);
4061#else
a6d53864 4062 submaps[nmaps-2] = current_buffer->keymap;
497ba7a1 4063#endif
7e6992e0 4064 submaps[nmaps-1] = current_global_map;
284f4730
JB
4065 }
4066
4067 /* Find an accurate initial value for first_binding. */
4068 for (first_binding = 0; first_binding < nmaps; first_binding++)
253598e4 4069 if (! NILP (submaps[first_binding]))
284f4730
JB
4070 break;
4071
3b9189f8 4072 /* Start from the beginning in keybuf. */
f4255cd1
JB
4073 t = 0;
4074
4075 /* These are no-ops the first time through, but if we restart, they
4076 revert the echo area and this_command_keys to their original state. */
4077 this_command_key_count = keys_start;
4078 if (INTERACTIVE)
4079 echo_truncate (echo_start);
4080
cca310da
JB
4081 /* If the best binding for the current key sequence is a keymap, or
4082 we may be looking at a function key's escape sequence, keep on
4083 reading. */
253598e4 4084 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
cca310da
JB
4085 || (first_binding >= nmaps
4086 && fkey_start < t
4087 /* mock input is never part of a function key's sequence. */
a612e298
RS
4088 && mock_input <= fkey_start)
4089 || (first_binding >= nmaps
4090 && keytran_start < t
4091 /* mock input is never part of a function key's sequence. */
e9bf89a0
RS
4092 && mock_input <= keytran_start)
4093 /* Don't return in the middle of a possible function key sequence,
4094 if the only bindings we found were via case conversion.
4095 Thus, if ESC O a has a function-key-map translation
4096 and ESC o has a binding, don't return after ESC O,
4097 so that we can translate ESC O plus the next character. */
4098 || (function_key_possible && case_converted))
284f4730
JB
4099 {
4100 Lisp_Object key;
7d6de002 4101 int used_mouse_menu = 0;
284f4730 4102
7b4aedb9
JB
4103 /* Where the last real key started. If we need to throw away a
4104 key that has expanded into more than one element of keybuf
4105 (say, a mouse click on the mode line which is being treated
4106 as [mode-line (mouse-...)], then we backtrack to this point
4107 of keybuf. */
4108 int last_real_key_start;
4109
0a7f1fc0
JB
4110 /* These variables are analogous to echo_start and keys_start;
4111 while those allow us to restart the entire key sequence,
4112 echo_local_start and keys_local_start allow us to throw away
4113 just one key. */
f4255cd1
JB
4114 int echo_local_start, keys_local_start, local_first_binding;
4115
284f4730
JB
4116 if (t >= bufsize)
4117 error ("key sequence too long");
4118
f4255cd1
JB
4119 if (INTERACTIVE)
4120 echo_local_start = echo_length ();
4121 keys_local_start = this_command_key_count;
4122 local_first_binding = first_binding;
4123
4124 replay_key:
0a7f1fc0 4125 /* These are no-ops, unless we throw away a keystroke below and
f4255cd1
JB
4126 jumped back up to replay_key; in that case, these restore the
4127 variables to their original state, allowing us to replay the
0a7f1fc0 4128 loop. */
f4255cd1
JB
4129 if (INTERACTIVE)
4130 echo_truncate (echo_local_start);
0a7f1fc0
JB
4131 this_command_key_count = keys_local_start;
4132 first_binding = local_first_binding;
4133
7e85b935
RS
4134 /* By default, assume each event is "real". */
4135 last_real_key_start = t;
4136
f4255cd1 4137 /* Does mock_input indicate that we are re-reading a key sequence? */
284f4730
JB
4138 if (t < mock_input)
4139 {
4140 key = keybuf[t];
4141 add_command_key (key);
4142 echo_char (key);
4143 }
253598e4
JB
4144
4145 /* If not, we should actually read a character. */
284f4730
JB
4146 else
4147 {
a6d53864
RS
4148 struct buffer *buf = current_buffer;
4149
84d91fda 4150 key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
a6d53864 4151 &used_mouse_menu);
284f4730 4152
dcc408a0
RS
4153 /* read_char returns t when it shows a menu and the user rejects it.
4154 Just return -1. */
4155 if (EQ (key, Qt))
4156 return -1;
4157
f4255cd1 4158 /* read_char returns -1 at the end of a macro.
284f4730
JB
4159 Emacs 18 handles this by returning immediately with a
4160 zero, so that's what we'll do. */
86e5706b 4161 if (XTYPE (key) == Lisp_Int && XINT (key) == -1)
cd21b839 4162 {
f4255cd1
JB
4163 t = 0;
4164 goto done;
cd21b839 4165 }
284f4730 4166
3b9189f8
RS
4167 /* If we have a quit that was typed in another frame, and
4168 quit_throw_to_read_char switched buffers,
4169 replay to get the right keymap. */
4170 if (EQ (key, quit_char) && current_buffer != starting_buffer)
4171 {
4172 keybuf[t++] = key;
4173 mock_input = t;
4174 Vquit_flag = Qnil;
4175 goto replay_sequence;
4176 }
4177
284f4730 4178 Vquit_flag = Qnil;
7e85b935 4179 }
284f4730 4180
7e85b935
RS
4181 /* Clicks in non-text areas get prefixed by the symbol
4182 in their CHAR-ADDRESS field. For example, a click on
4183 the mode line is prefixed by the symbol `mode-line'.
4184
4185 Furthermore, key sequences beginning with mouse clicks
4186 are read using the keymaps of the buffer clicked on, not
4187 the current buffer. So we may have to switch the buffer
4188 here.
4189
4190 When we turn one event into two events, we must make sure
4191 that neither of the two looks like the original--so that,
4192 if we replay the events, they won't be expanded again.
4193 If not for this, such reexpansion could happen either here
4194 or when user programs play with this-command-keys. */
4195 if (EVENT_HAS_PARAMETERS (key))
4196 {
4197 Lisp_Object kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
cca310da 4198
7e85b935 4199 if (EQ (kind, Qmouse_click))
0a7f1fc0 4200 {
7e85b935
RS
4201 Lisp_Object window = POSN_WINDOW (EVENT_START (key));
4202 Lisp_Object posn = POSN_BUFFER_POSN (EVENT_START (key));
f4255cd1 4203
7e85b935 4204 if (XTYPE (posn) == Lisp_Cons)
0a7f1fc0 4205 {
7e85b935
RS
4206 /* We're looking at the second event of a
4207 sequence which we expanded before. Set
4208 last_real_key_start appropriately. */
4209 if (t > 0)
4210 last_real_key_start = t - 1;
cd21b839 4211 }
7e85b935
RS
4212
4213 /* Key sequences beginning with mouse clicks are
4214 read using the keymaps in the buffer clicked on,
4215 not the current buffer. If we're at the
4216 beginning of a key sequence, switch buffers. */
4217 if (last_real_key_start == 0
4218 && XTYPE (window) == Lisp_Window
4219 && XTYPE (XWINDOW (window)->buffer) == Lisp_Buffer
4220 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
cd21b839 4221 {
7e85b935
RS
4222 keybuf[t] = key;
4223 mock_input = t + 1;
4224
4225 /* Arrange to go back to the original buffer once we're
4226 done reading the key sequence. Note that we can't
4227 use save_excursion_{save,restore} here, because they
4228 save point as well as the current buffer; we don't
4229 want to save point, because redisplay may change it,
4230 to accommodate a Fset_window_start or something. We
4231 don't want to do this at the top of the function,
4232 because we may get input from a subprocess which
4233 wants to change the selected window and stuff (say,
4234 emacsclient). */
4235 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4236
4237 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
4238 goto replay_sequence;
0a7f1fc0 4239 }
7e85b935 4240 else if (XTYPE (posn) == Lisp_Symbol)
5ec75a55 4241 {
7e85b935
RS
4242 /* Expand mode-line and scroll-bar events into two events:
4243 use posn as a fake prefix key. */
5ec75a55 4244
7e85b935
RS
4245 if (t + 1 >= bufsize)
4246 error ("key sequence too long");
4247 keybuf[t] = posn;
4248 keybuf[t+1] = key;
4249 mock_input = t + 2;
4250
4251 /* Zap the position in key, so we know that we've
4252 expanded it, and don't try to do so again. */
4253 POSN_BUFFER_POSN (EVENT_START (key))
4254 = Fcons (posn, Qnil);
4255 goto replay_key;
5ec75a55 4256 }
0a7f1fc0 4257 }
7e85b935 4258 else if (EQ (kind, Qswitch_frame))
a6d53864 4259 {
7e85b935
RS
4260 /* If we're at the beginning of a key sequence, go
4261 ahead and return this event. If we're in the
4262 midst of a key sequence, delay it until the end. */
4263 if (t > 0)
4264 {
4265 delayed_switch_frame = key;
4266 goto replay_key;
4267 }
4268 }
4269 else
4270 {
4271 Lisp_Object posn = POSN_BUFFER_POSN (EVENT_START (key));
4272
4273 /* Handle menu-bar events:
4274 insert the dummy prefix event `menu-bar'. */
4275 if (EQ (posn, Qmenu_bar))
4276 {
4277 if (t + 1 >= bufsize)
4278 error ("key sequence too long");
4279 /* Run the Lucid hook. */
4280 call1 (Vrun_hooks, Qactivate_menubar_hook);
4281 /* If it has changed current-menubar from previous value,
4282 really recompute the menubar from the value. */
4283 if (! NILP (Vlucid_menu_bar_dirty_flag))
4284 call0 (Qrecompute_lucid_menubar);
4285 keybuf[t] = posn;
4286 keybuf[t+1] = key;
4287
4288 /* Zap the position in key, so we know that we've
4289 expanded it, and don't try to do so again. */
4290 POSN_BUFFER_POSN (EVENT_START (key))
4291 = Fcons (posn, Qnil);
4292
4293 mock_input = t + 2;
4294 goto replay_sequence;
4295 }
4296 else if (XTYPE (posn) == Lisp_Cons)
4297 {
4298 /* We're looking at the second event of a
4299 sequence which we expanded before. Set
4300 last_real_key_start appropriately. */
4301 if (last_real_key_start == t && t > 0)
4302 last_real_key_start = t - 1;
4303 }
a6d53864 4304 }
284f4730 4305 }
f4255cd1
JB
4306
4307 /* We have finally decided that KEY is something we might want
4308 to look up. */
284f4730
JB
4309 first_binding = (follow_key (key,
4310 nmaps - first_binding,
253598e4 4311 submaps + first_binding,
284f4730 4312 defs + first_binding,
e9bf89a0
RS
4313 submaps + first_binding,
4314 &case_converted)
284f4730 4315 + first_binding);
0a7f1fc0 4316
f4255cd1 4317 /* If KEY wasn't bound, we'll try some fallbacks. */
0a7f1fc0
JB
4318 if (first_binding >= nmaps)
4319 {
4320 Lisp_Object head = EVENT_HEAD (key);
4321
7e85b935
RS
4322 if (EQ (head, Vhelp_char))
4323 {
4324 read_key_sequence_cmd = Vprefix_help_command;
4325 keybuf[t++] = key;
4326 last_nonmenu_event = key;
4327 goto done;
4328 }
4329
0a7f1fc0
JB
4330 if (XTYPE (head) == Lisp_Symbol)
4331 {
4332 Lisp_Object breakdown = parse_modifiers (head);
80645119 4333 int modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
0a7f1fc0 4334
559f9d04
RS
4335 /* Attempt to reduce an unbound mouse event to a simpler
4336 event that is bound:
4337 Drags reduce to clicks.
4338 Double-clicks reduce to clicks.
4339 Triple-clicks reduce to double-clicks, then to clicks.
4340 Down-clicks are eliminated.
4341 Double-downs reduce to downs, then are eliminated.
4342 Triple-downs reduce to double-downs, then to downs,
4343 then are eliminated. */
4344 if (modifiers & (down_modifier | drag_modifier
4345 | double_modifier | triple_modifier))
0a7f1fc0 4346 {
559f9d04
RS
4347 while (modifiers & (down_modifier | drag_modifier
4348 | double_modifier | triple_modifier))
fbcd35bd
JB
4349 {
4350 Lisp_Object new_head, new_click;
4351 if (modifiers & triple_modifier)
4352 modifiers ^= (double_modifier | triple_modifier);
559f9d04 4353 else if (modifiers & (drag_modifier | double_modifier))
fbcd35bd 4354 modifiers &= ~(drag_modifier | double_modifier);
559f9d04
RS
4355 else
4356 {
4357 /* Dispose of this `down' event by simply jumping
4358 back to replay_key, to get another event.
4359
4360 Note that if this event came from mock input,
4361 then just jumping back to replay_key will just
4362 hand it to us again. So we have to wipe out any
4363 mock input.
4364
4365 We could delete keybuf[t] and shift everything
4366 after that to the left by one spot, but we'd also
4367 have to fix up any variable that points into
4368 keybuf, and shifting isn't really necessary
4369 anyway.
4370
4371 Adding prefixes for non-textual mouse clicks
4372 creates two characters of mock input, and both
4373 must be thrown away. If we're only looking at
4374 the prefix now, we can just jump back to
4375 replay_key. On the other hand, if we've already
4376 processed the prefix, and now the actual click
4377 itself is giving us trouble, then we've lost the
4378 state of the keymaps we want to backtrack to, and
4379 we need to replay the whole sequence to rebuild
4380 it.
4381
4382 Beyond that, only function key expansion could
4383 create more than two keys, but that should never
4384 generate mouse events, so it's okay to zero
4385 mock_input in that case too.
4386
4387 Isn't this just the most wonderful code ever? */
4388 if (t == last_real_key_start)
4389 {
4390 mock_input = 0;
4391 goto replay_key;
4392 }
4393 else
4394 {
4395 mock_input = last_real_key_start;
4396 goto replay_sequence;
4397 }
4398 }
4399
27203ead
RS
4400 new_head
4401 = apply_modifiers (modifiers, XCONS (breakdown)->car);
4402 new_click
4403 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
fbcd35bd
JB
4404
4405 /* Look for a binding for this new key. follow_key
4406 promises that it didn't munge submaps the
4407 last time we called it, since key was unbound. */
27203ead
RS
4408 first_binding
4409 = (follow_key (new_click,
4410 nmaps - local_first_binding,
4411 submaps + local_first_binding,
4412 defs + local_first_binding,
e9bf89a0
RS
4413 submaps + local_first_binding,
4414 &case_converted)
27203ead 4415 + local_first_binding);
fbcd35bd
JB
4416
4417 /* If that click is bound, go for it. */
4418 if (first_binding < nmaps)
4419 {
4420 key = new_click;
4421 break;
4422 }
4423 /* Otherwise, we'll leave key set to the drag event. */
4424 }
0a7f1fc0
JB
4425 }
4426 }
4427 }
4428
284f4730 4429 keybuf[t++] = key;
7d6de002
RS
4430 /* Normally, last_nonmenu_event gets the previous key we read.
4431 But when a mouse popup menu is being used,
4432 we don't update last_nonmenu_event; it continues to hold the mouse
4433 event that preceded the first level of menu. */
4434 if (!used_mouse_menu)
4435 last_nonmenu_event = key;
284f4730
JB
4436
4437 /* If the sequence is unbound, see if we can hang a function key
253598e4
JB
4438 off the end of it. We only want to scan real keyboard input
4439 for function key sequences, so if mock_input says that we're
f4255cd1 4440 re-reading old events, don't examine it. */
e9bf89a0 4441 if ((first_binding >= nmaps || case_converted)
253598e4 4442 && t >= mock_input)
284f4730
JB
4443 {
4444 Lisp_Object fkey_next;
4445
e9bf89a0
RS
4446 /* Continue scan from fkey_end until we find a bound suffix.
4447 If we fail, increment fkey_start
4448 and start fkey_end from there. */
284f4730
JB
4449 while (fkey_end < t)
4450 {
f4255cd1
JB
4451 Lisp_Object key;
4452
4453 key = keybuf[fkey_end++];
067ffa38
JB
4454 /* Look up meta-characters by prefixing them
4455 with meta_prefix_char. I hate this. */
d3cc13fa 4456 if (XTYPE (key) == Lisp_Int && XINT (key) & meta_modifier)
f4255cd1 4457 {
e74fbc70
RS
4458 fkey_next
4459 = get_keymap_1
f4255cd1 4460 (get_keyelt
e74fbc70 4461 (access_keymap (fkey_map, meta_prefix_char, 1, 0)),
f4255cd1 4462 0, 1);
d3cc13fa 4463 XFASTINT (key) = XFASTINT (key) & ~meta_modifier;
f4255cd1 4464 }
067ffa38
JB
4465 else
4466 fkey_next = fkey_map;
4467
e74fbc70
RS
4468 fkey_next
4469 = get_keyelt (access_keymap (fkey_next, key, 1, 0));
067ffa38 4470
1abe6abe
RS
4471 /* If the function key map gives a function, not an
4472 array, then call the function with no args and use
4473 its value instead. */
4474 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
4475 && fkey_end == t)
4476 {
4477 struct gcpro gcpro1, gcpro2, gcpro3;
4478 Lisp_Object tem;
4479 tem = fkey_next;
4480
4481 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
84d91fda 4482 fkey_next = call1 (fkey_next, prompt);
1abe6abe
RS
4483 UNGCPRO;
4484 /* If the function returned something invalid,
4485 barf--don't ignore it.
4486 (To ignore it safely, we would need to gcpro a bunch of
4487 other variables.) */
4488 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
4489 error ("Function in function-key-map returns invalid key sequence");
4490 }
4491
e9bf89a0
RS
4492 function_key_possible = ! NILP (fkey_next);
4493
85bc5181 4494 /* If keybuf[fkey_start..fkey_end] is bound in the
a764a753 4495 function key map and it's a suffix of the current
85bc5181 4496 sequence (i.e. fkey_end == t), replace it with
a764a753 4497 the binding and restart with fkey_start at the end. */
f5ea6163 4498 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
284f4730
JB
4499 && fkey_end == t)
4500 {
2e864a76 4501 int len = XFASTINT (Flength (fkey_next));
f5ea6163
JB
4502
4503 t = fkey_start + len;
284f4730
JB
4504 if (t >= bufsize)
4505 error ("key sequence too long");
4506
f5ea6163
JB
4507 if (VECTORP (fkey_next))
4508 bcopy (XVECTOR (fkey_next)->contents,
4509 keybuf + fkey_start,
4510 (t - fkey_start) * sizeof (keybuf[0]));
4511 else if (STRINGP (fkey_next))
4512 {
4513 int i;
4514
4515 for (i = 0; i < len; i++)
1abe6abe
RS
4516 XFASTINT (keybuf[fkey_start + i])
4517 = XSTRING (fkey_next)->data[i];
f5ea6163 4518 }
284f4730
JB
4519
4520 mock_input = t;
4521 fkey_start = fkey_end = t;
32e6d806 4522 fkey_map = Vfunction_key_map;
284f4730 4523
f4255cd1 4524 goto replay_sequence;
284f4730
JB
4525 }
4526
f4255cd1 4527 fkey_map = get_keymap_1 (fkey_next, 0, 1);
284f4730 4528
a764a753
JB
4529 /* If we no longer have a bound suffix, try a new positions for
4530 fkey_start. */
284f4730
JB
4531 if (NILP (fkey_map))
4532 {
4533 fkey_end = ++fkey_start;
4534 fkey_map = Vfunction_key_map;
e9bf89a0 4535 function_key_possible = 0;
284f4730
JB
4536 }
4537 }
4538 }
a612e298
RS
4539
4540 /* Look for this sequence in key-translation-map. */
4541 {
4542 Lisp_Object keytran_next;
4543
4544 /* Scan from keytran_end until we find a bound suffix. */
4545 while (keytran_end < t)
4546 {
4547 Lisp_Object key;
4548
4549 key = keybuf[keytran_end++];
4550 /* Look up meta-characters by prefixing them
4551 with meta_prefix_char. I hate this. */
4552 if (XTYPE (key) == Lisp_Int && XINT (key) & meta_modifier)
4553 {
4554 keytran_next
4555 = get_keymap_1
4556 (get_keyelt
4557 (access_keymap (keytran_map, meta_prefix_char, 1, 0)),
4558 0, 1);
4559 XFASTINT (key) = XFASTINT (key) & ~meta_modifier;
4560 }
4561 else
4562 keytran_next = keytran_map;
4563
4564 keytran_next
4565 = get_keyelt (access_keymap (keytran_next, key, 1, 0));
4566
1abe6abe
RS
4567 /* If the key translation map gives a function, not an
4568 array, then call the function with no args and use
4569 its value instead. */
4570 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
4571 && keytran_end == t)
4572 {
4573 struct gcpro gcpro1, gcpro2, gcpro3;
4574 Lisp_Object tem;
4575 tem = keytran_next;
4576
4577 GCPRO3 (keytran_map, keytran_map, delayed_switch_frame);
84d91fda 4578 keytran_next = call1 (keytran_next, prompt);
1abe6abe
RS
4579 UNGCPRO;
4580 /* If the function returned something invalid,
4581 barf--don't ignore it.
4582 (To ignore it safely, we would need to gcpro a bunch of
4583 other variables.) */
4584 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
4585 error ("Function in function-key-map returns invalid key sequence");
4586 }
4587
a612e298 4588 /* If keybuf[keytran_start..keytran_end] is bound in the
1abe6abe 4589 key translation map and it's a suffix of the current
a612e298
RS
4590 sequence (i.e. keytran_end == t), replace it with
4591 the binding and restart with keytran_start at the end. */
4592 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
4593 && keytran_end == t)
4594 {
2e864a76 4595 int len = XFASTINT (Flength (keytran_next));
a612e298
RS
4596
4597 t = keytran_start + len;
4598 if (t >= bufsize)
4599 error ("key sequence too long");
4600
4601 if (VECTORP (keytran_next))
4602 bcopy (XVECTOR (keytran_next)->contents,
4603 keybuf + keytran_start,
4604 (t - keytran_start) * sizeof (keybuf[0]));
4605 else if (STRINGP (keytran_next))
4606 {
4607 int i;
4608
4609 for (i = 0; i < len; i++)
4610 XFASTINT (keybuf[keytran_start + i])
4611 = XSTRING (keytran_next)->data[i];
4612 }
4613
4614 mock_input = t;
4615 keytran_start = keytran_end = t;
4616 keytran_map = Vkey_translation_map;
4617
4618 goto replay_sequence;
4619 }
4620
4621 keytran_map = get_keymap_1 (keytran_next, 0, 1);
4622
4623 /* If we no longer have a bound suffix, try a new positions for
4624 keytran_start. */
4625 if (NILP (keytran_map))
4626 {
4627 keytran_end = ++keytran_start;
4628 keytran_map = Vkey_translation_map;
4629 }
4630 }
4631 }
284f4730
JB
4632 }
4633
4634 read_key_sequence_cmd = (first_binding < nmaps
4635 ? defs[first_binding]
4636 : Qnil);
4637
f4255cd1 4638 done:
cd21b839 4639 unread_switch_frame = delayed_switch_frame;
f4255cd1 4640 unbind_to (count, Qnil);
07f76a14
JB
4641
4642 /* Occasionally we fabricate events, perhaps by expanding something
4643 according to function-key-map, or by adding a prefix symbol to a
4644 mouse click in the scroll bar or modeline. In this cases, return
4645 the entire generated key sequence, even if we hit an unbound
4646 prefix or a definition before the end. This means that you will
4647 be able to push back the event properly, and also means that
4648 read-key-sequence will always return a logical unit.
4649
4650 Better ideas? */
cca310da
JB
4651 for (; t < mock_input; t++)
4652 {
4653 echo_char (keybuf[t]);
4654 add_command_key (keybuf[t]);
4655 }
07f76a14 4656
284f4730
JB
4657 return t;
4658}
4659
a612e298
RS
4660#if 0 /* This doc string is too long for some compilers.
4661 This commented-out definition serves for DOC. */
c0a58692 4662DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 2, 0,
284f4730
JB
4663 "Read a sequence of keystrokes and return as a string or vector.\n\
4664The sequence is sufficient to specify a non-prefix command in the\n\
4665current local and global maps.\n\
4666\n\
c0a58692
RS
4667First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
4668Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
4669as a continuation of the previous key.\n\
284f4730 4670\n\
cb5df6ae
JB
4671A C-g typed while in this function is treated like any other character,\n\
4672and `quit-flag' is not set.\n\
4673\n\
4674If the key sequence starts with a mouse click, then the sequence is read\n\
4675using the keymaps of the buffer of the window clicked in, not the buffer\n\
4676of the selected window as normal.\n\
ede41463 4677""\n\
cb5df6ae
JB
4678`read-key-sequence' drops unbound button-down events, since you normally\n\
4679only care about the click or drag events which follow them. If a drag\n\
fbcd35bd
JB
4680or multi-click event is unbound, but the corresponding click event would\n\
4681be bound, `read-key-sequence' turns the event into a click event at the\n\
cb5df6ae 4682drag's starting position. This means that you don't have to distinguish\n\
fbcd35bd 4683between click and drag, double, or triple events unless you want to.\n\
cb5df6ae
JB
4684\n\
4685`read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
3c370943
JB
4686lines separating windows, and scroll bars with imaginary keys\n\
4687`mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
cb5df6ae
JB
4688\n\
4689If the user switches frames in the middle of a key sequence, the\n\
4690frame-switch event is put off until after the current key sequence.\n\
4691\n\
4692`read-key-sequence' checks `function-key-map' for function key\n\
4693sequences, where they wouldn't conflict with ordinary bindings. See\n\
4bb994d1 4694`function-key-map' for more details.")
a612e298
RS
4695 (prompt, continue_echo)
4696#endif
4697
4698DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 2, 0,
4699 0)
c0a58692
RS
4700 (prompt, continue_echo)
4701 Lisp_Object prompt, continue_echo;
284f4730
JB
4702{
4703 Lisp_Object keybuf[30];
4704 register int i;
4705 struct gcpro gcpro1, gcpro2;
4706
4707 if (!NILP (prompt))
4708 CHECK_STRING (prompt, 0);
4709 QUIT;
4710
4711 bzero (keybuf, sizeof keybuf);
4712 GCPRO1 (keybuf[0]);
4713 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
4714
daa37602 4715 if (NILP (continue_echo))
c0a58692
RS
4716 this_command_key_count = 0;
4717
84d91fda 4718 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])), prompt);
284f4730 4719
dcc408a0
RS
4720 if (i == -1)
4721 {
4722 Vquit_flag = Qt;
4723 QUIT;
4724 }
284f4730 4725 UNGCPRO;
86e5706b 4726 return make_event_array (i, keybuf);
284f4730
JB
4727}
4728\f
4729DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
4730 "Execute CMD as an editor command.\n\
4731CMD must be a symbol that satisfies the `commandp' predicate.\n\
4732Optional second arg RECORD-FLAG non-nil\n\
4733means unconditionally put this command in `command-history'.\n\
4734Otherwise, that is done only if an arg is read using the minibuffer.")
4735 (cmd, record)
4736 Lisp_Object cmd, record;
4737{
4738 register Lisp_Object final;
4739 register Lisp_Object tem;
4740 Lisp_Object prefixarg;
4741 struct backtrace backtrace;
4742 extern int debug_on_next_call;
4743
4744 prefixarg = Vprefix_arg, Vprefix_arg = Qnil;
4745 Vcurrent_prefix_arg = prefixarg;
4746 debug_on_next_call = 0;
4747
4748 if (XTYPE (cmd) == Lisp_Symbol)
4749 {
4750 tem = Fget (cmd, Qdisabled);
4751 if (!NILP (tem))
2e894dab 4752 return call1 (Vrun_hooks, Qdisabled_command_hook);
284f4730
JB
4753 }
4754
4755 while (1)
4756 {
ffd56f97 4757 final = Findirect_function (cmd);
284f4730
JB
4758
4759 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
4760 do_autoload (final, cmd);
4761 else
4762 break;
4763 }
4764
4765 if (XTYPE (final) == Lisp_String
4766 || XTYPE (final) == Lisp_Vector)
4767 {
4768 /* If requested, place the macro in the command history. For
4769 other sorts of commands, call-interactively takes care of
4770 this. */
4771 if (!NILP (record))
4772 Vcommand_history
4773 = Fcons (Fcons (Qexecute_kbd_macro,
4774 Fcons (final, Fcons (prefixarg, Qnil))),
4775 Vcommand_history);
4776
4777 return Fexecute_kbd_macro (final, prefixarg);
4778 }
4779 if (CONSP (final) || XTYPE (final) == Lisp_Subr
4780 || XTYPE (final) == Lisp_Compiled)
4781 {
4782 backtrace.next = backtrace_list;
4783 backtrace_list = &backtrace;
4784 backtrace.function = &Qcall_interactively;
4785 backtrace.args = &cmd;
4786 backtrace.nargs = 1;
4787 backtrace.evalargs = 0;
4788
4789 tem = Fcall_interactively (cmd, record);
4790
4791 backtrace_list = backtrace.next;
4792 return tem;
4793 }
4794 return Qnil;
4795}
4796\f
284f4730
JB
4797DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
4798 1, 1, "P",
4799 "Read function name, then read its arguments and call it.")
4800 (prefixarg)
4801 Lisp_Object prefixarg;
4802{
4803 Lisp_Object function;
4804 char buf[40];
4805 Lisp_Object saved_keys;
4806 struct gcpro gcpro1;
4807
4808 saved_keys = Fthis_command_keys ();
4809 buf[0] = 0;
4810 GCPRO1 (saved_keys);
4811
4812 if (EQ (prefixarg, Qminus))
4813 strcpy (buf, "- ");
4814 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
4815 strcpy (buf, "C-u ");
4816 else if (CONSP (prefixarg) && XTYPE (XCONS (prefixarg)->car) == Lisp_Int)
4817 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
4818 else if (XTYPE (prefixarg) == Lisp_Int)
4819 sprintf (buf, "%d ", XINT (prefixarg));
4820
4821 /* This isn't strictly correct if execute-extended-command
4822 is bound to anything else. Perhaps it should use
4823 this_command_keys? */
4824 strcat (buf, "M-x ");
4825
4826 /* Prompt with buf, and then read a string, completing from and
4827 restricting to the set of all defined commands. Don't provide
51763820 4828 any initial input. Save the command read on the extended-command
03b4122a 4829 history list. */
284f4730
JB
4830 function = Fcompleting_read (build_string (buf),
4831 Vobarray, Qcommandp,
03b4122a 4832 Qt, Qnil, Qextended_command_history);
284f4730 4833
1113d9db
JB
4834 /* Set this_command_keys to the concatenation of saved_keys and
4835 function, followed by a RET. */
284f4730 4836 {
1113d9db 4837 struct Lisp_String *str;
284f4730
JB
4838 int i;
4839 Lisp_Object tem;
4840
1113d9db
JB
4841 this_command_key_count = 0;
4842
4843 str = XSTRING (saved_keys);
4844 for (i = 0; i < str->size; i++)
284f4730 4845 {
1113d9db 4846 XFASTINT (tem) = str->data[i];
284f4730
JB
4847 add_command_key (tem);
4848 }
1113d9db
JB
4849
4850 str = XSTRING (function);
4851 for (i = 0; i < str->size; i++)
4852 {
4853 XFASTINT (tem) = str->data[i];
4854 add_command_key (tem);
4855 }
4856
4857 XFASTINT (tem) = '\015';
4858 add_command_key (tem);
284f4730
JB
4859 }
4860
4861 UNGCPRO;
4862
0a7f1fc0 4863 function = Fintern (function, Qnil);
284f4730
JB
4864 Vprefix_arg = prefixarg;
4865 this_command = function;
4866
4867 return Fcommand_execute (function, Qt);
4868}
4869\f
4870
4871detect_input_pending ()
4872{
4873 if (!input_pending)
4874 get_input_pending (&input_pending);
4875
4876 return input_pending;
4877}
4878
ffd56f97
JB
4879/* This is called in some cases before a possible quit.
4880 It cases the next call to detect_input_pending to recompute input_pending.
4881 So calling this function unnecessarily can't do any harm. */
4882clear_input_pending ()
4883{
4884 input_pending = 0;
4885}
4886
284f4730
JB
4887DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
4888 "T if command input is currently available with no waiting.\n\
4889Actually, the value is nil only if we can be sure that no input is available.")
4890 ()
4891{
86e5706b 4892 if (!NILP (unread_command_events) || unread_command_char != -1)
284f4730
JB
4893 return (Qt);
4894
4895 return detect_input_pending () ? Qt : Qnil;
4896}
4897
4898DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
22d7cb89 4899 "Return vector of last 100 events, not counting those from keyboard macros.")
284f4730
JB
4900 ()
4901{
5160df46 4902 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
284f4730
JB
4903 Lisp_Object val;
4904
4905 if (total_keys < NUM_RECENT_KEYS)
5160df46 4906 return Fvector (total_keys, keys);
284f4730
JB
4907 else
4908 {
5160df46
JB
4909 val = Fvector (NUM_RECENT_KEYS, keys);
4910 bcopy (keys + recent_keys_index,
284f4730
JB
4911 XVECTOR (val)->contents,
4912 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
5160df46 4913 bcopy (keys,
284f4730
JB
4914 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
4915 recent_keys_index * sizeof (Lisp_Object));
4916 return val;
4917 }
4918}
4919
4920DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
e5f920d7
RS
4921 "Return the key sequence that invoked this command.\n\
4922The value is a string or a vector.")
284f4730
JB
4923 ()
4924{
86e5706b
RS
4925 return make_event_array (this_command_key_count,
4926 XVECTOR (this_command_keys)->contents);
284f4730
JB
4927}
4928
4929DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
4930 "Return the current depth in recursive edits.")
4931 ()
4932{
4933 Lisp_Object temp;
4934 XFASTINT (temp) = command_loop_level + minibuf_level;
4935 return temp;
4936}
4937
4938DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
4939 "FOpen dribble file: ",
9b2471df
RS
4940 "Start writing all keyboard characters to a dribble file called FILE.\n\
4941If FILE is nil, close any open dribble file.")
284f4730
JB
4942 (file)
4943 Lisp_Object file;
4944{
4945 if (NILP (file))
4946 {
4947 fclose (dribble);
4948 dribble = 0;
4949 }
4950 else
4951 {
4952 file = Fexpand_file_name (file, Qnil);
4953 dribble = fopen (XSTRING (file)->data, "w");
4954 }
4955 return Qnil;
4956}
4957
4958DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
4959 "Discard the contents of the terminal input buffer.\n\
4960Also cancel any kbd macro being defined.")
4961 ()
4962{
4963 defining_kbd_macro = 0;
4964 update_mode_lines++;
4965
dbc4e1c1 4966 unread_command_events = Qnil;
86e5706b 4967 unread_command_char = -1;
284f4730
JB
4968
4969 discard_tty_input ();
4970
ff0b5f4c
JB
4971 /* Without the cast, GCC complains that this assignment loses the
4972 volatile qualifier of kbd_store_ptr. Is there anything wrong
4973 with that? */
4974 kbd_fetch_ptr = (struct input_event *) kbd_store_ptr;
7b4aedb9 4975 Ffillarray (kbd_buffer_frame_or_window, Qnil);
284f4730
JB
4976 input_pending = 0;
4977
4978 return Qnil;
4979}
4980\f
4981DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
4982 "Stop Emacs and return to superior process. You can resume later.\n\
4983On systems that don't have job control, run a subshell instead.\n\n\
4984If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
b7d2ebbf
RS
4985to be read as terminal input by Emacs's parent, after suspension.\n\
4986\n\
7df30614 4987Before suspending, call the functions in `suspend-hook' with no args.\n\
b7d2ebbf
RS
4988If any of them returns nil, don't call the rest and don't suspend.\n\
4989Otherwise, suspend normally and after resumption run the normal hook\n\
284f4730
JB
4990`suspend-resume-hook' if that is bound and non-nil.\n\
4991\n\
4992Some operating systems cannot stop the Emacs process and resume it later.\n\
b7d2ebbf 4993On such systems, Emacs starts a subshell instead of suspending.")
284f4730
JB
4994 (stuffstring)
4995 Lisp_Object stuffstring;
4996{
3a69360c 4997 Lisp_Object tem;
284f4730
JB
4998 int count = specpdl_ptr - specpdl;
4999 int old_height, old_width;
5000 int width, height;
b7d2ebbf 5001 struct gcpro gcpro1, gcpro2;
284f4730
JB
5002 extern init_sys_modes ();
5003
5004 if (!NILP (stuffstring))
5005 CHECK_STRING (stuffstring, 0);
284f4730 5006
1e95ed28
JB
5007 /* Run the functions in suspend-hook. */
5008 if (!NILP (Vrun_hooks))
5009 call1 (Vrun_hooks, intern ("suspend-hook"));
284f4730 5010
b7d2ebbf 5011 GCPRO1 (stuffstring);
ff11dfa1 5012 get_frame_size (&old_width, &old_height);
284f4730
JB
5013 reset_sys_modes ();
5014 /* sys_suspend can get an error if it tries to fork a subshell
5015 and the system resources aren't available for that. */
5016 record_unwind_protect (init_sys_modes, 0);
5017 stuff_buffered_input (stuffstring);
5018 sys_suspend ();
5019 unbind_to (count, Qnil);
5020
5021 /* Check if terminal/window size has changed.
5022 Note that this is not useful when we are running directly
5023 with a window system; but suspend should be disabled in that case. */
ff11dfa1 5024 get_frame_size (&width, &height);
284f4730 5025 if (width != old_width || height != old_height)
f5ea6163 5026 change_frame_size (selected_frame, height, width, 0, 0);
284f4730 5027
1e95ed28 5028 /* Run suspend-resume-hook. */
284f4730
JB
5029 if (!NILP (Vrun_hooks))
5030 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
5031
5032 UNGCPRO;
5033 return Qnil;
5034}
5035
5036/* If STUFFSTRING is a string, stuff its contents as pending terminal input.
eb8c3be9 5037 Then in any case stuff anything Emacs has read ahead and not used. */
284f4730
JB
5038
5039stuff_buffered_input (stuffstring)
5040 Lisp_Object stuffstring;
5041{
5042 register unsigned char *p;
5043
5044/* stuff_char works only in BSD, versions 4.2 and up. */
5045#ifdef BSD
5046#ifndef BSD4_1
5047 if (XTYPE (stuffstring) == Lisp_String)
5048 {
5049 register int count;
5050
5051 p = XSTRING (stuffstring)->data;
5052 count = XSTRING (stuffstring)->size;
5053 while (count-- > 0)
5054 stuff_char (*p++);
5055 stuff_char ('\n');
5056 }
5057 /* Anything we have read ahead, put back for the shell to read. */
5058 while (kbd_fetch_ptr != kbd_store_ptr)
5059 {
5060 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
5061 kbd_fetch_ptr = kbd_buffer;
5062 if (kbd_fetch_ptr->kind == ascii_keystroke)
e9bf89a0 5063 stuff_char (kbd_fetch_ptr->code);
4bb994d1 5064 kbd_fetch_ptr->kind = no_event;
7b4aedb9
JB
5065 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
5066 - kbd_buffer]
5067 = Qnil);
284f4730
JB
5068 kbd_fetch_ptr++;
5069 }
5070 input_pending = 0;
5071#endif
5072#endif /* BSD and not BSD4_1 */
5073}
5074\f
ffd56f97
JB
5075set_waiting_for_input (time_to_clear)
5076 EMACS_TIME *time_to_clear;
284f4730 5077{
ffd56f97 5078 input_available_clear_time = time_to_clear;
284f4730
JB
5079
5080 /* Tell interrupt_signal to throw back to read_char, */
5081 waiting_for_input = 1;
5082
5083 /* If interrupt_signal was called before and buffered a C-g,
5084 make it run again now, to avoid timing error. */
5085 if (!NILP (Vquit_flag))
5086 quit_throw_to_read_char ();
284f4730
JB
5087}
5088
5089clear_waiting_for_input ()
5090{
5091 /* Tell interrupt_signal not to throw back to read_char, */
5092 waiting_for_input = 0;
ffd56f97 5093 input_available_clear_time = 0;
284f4730
JB
5094}
5095
5096/* This routine is called at interrupt level in response to C-G.
5097 If interrupt_input, this is the handler for SIGINT.
5098 Otherwise, it is called from kbd_buffer_store_event,
5099 in handling SIGIO or SIGTINT.
5100
5101 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
5102 immediately throw back to read_char.
5103
5104 Otherwise it sets the Lisp variable quit-flag not-nil.
5105 This causes eval to throw, when it gets a chance.
5106 If quit-flag is already non-nil, it stops the job right away. */
5107
5108SIGTYPE
5109interrupt_signal ()
5110{
5111 char c;
5112 /* Must preserve main program's value of errno. */
5113 int old_errno = errno;
284f4730
JB
5114
5115#ifdef USG
5116 /* USG systems forget handlers when they are used;
5117 must reestablish each time */
5118 signal (SIGINT, interrupt_signal);
5119 signal (SIGQUIT, interrupt_signal);
5120#endif /* USG */
5121
5122 cancel_echoing ();
5123
d5045cf9 5124 if (!NILP (Vquit_flag) && FRAME_TERMCAP_P (selected_frame))
284f4730
JB
5125 {
5126 fflush (stdout);
5127 reset_sys_modes ();
5128 sigfree ();
5129#ifdef SIGTSTP /* Support possible in later USG versions */
5130/*
5131 * On systems which can suspend the current process and return to the original
5132 * shell, this command causes the user to end up back at the shell.
5133 * The "Auto-save" and "Abort" questions are not asked until
5134 * the user elects to return to emacs, at which point he can save the current
5135 * job and either dump core or continue.
5136 */
5137 sys_suspend ();
5138#else
5139#ifdef VMS
5140 if (sys_suspend () == -1)
5141 {
5142 printf ("Not running as a subprocess;\n");
5143 printf ("you can continue or abort.\n");
5144 }
5145#else /* not VMS */
5146 /* Perhaps should really fork an inferior shell?
5147 But that would not provide any way to get back
5148 to the original shell, ever. */
5149 printf ("No support for stopping a process on this operating system;\n");
5150 printf ("you can continue or abort.\n");
5151#endif /* not VMS */
5152#endif /* not SIGTSTP */
80e4aa30
RS
5153#ifdef MSDOS
5154 /* We must remain inside the screen area when the internal terminal
5155 is used. Note that [Enter] is not echoed by dos. */
5156 cursor_to (0, 0);
5157#endif
284f4730
JB
5158 printf ("Auto-save? (y or n) ");
5159 fflush (stdout);
5160 if (((c = getchar ()) & ~040) == 'Y')
9fd7d808
RS
5161 {
5162 Fdo_auto_save (Qt, Qnil);
80e4aa30
RS
5163#ifdef MSDOS
5164 printf ("\r\nAuto-save done");
5165#else /* not MSDOS */
9fd7d808 5166 printf ("Auto-save done\n");
80e4aa30 5167#endif /* not MSDOS */
9fd7d808 5168 }
284f4730 5169 while (c != '\n') c = getchar ();
80e4aa30
RS
5170#ifdef MSDOS
5171 printf ("\r\nAbort? (y or n) ");
5172#else /* not MSDOS */
284f4730
JB
5173#ifdef VMS
5174 printf ("Abort (and enter debugger)? (y or n) ");
5175#else /* not VMS */
5176 printf ("Abort (and dump core)? (y or n) ");
5177#endif /* not VMS */
80e4aa30 5178#endif /* not MSDOS */
284f4730
JB
5179 fflush (stdout);
5180 if (((c = getchar ()) & ~040) == 'Y')
5181 abort ();
5182 while (c != '\n') c = getchar ();
80e4aa30
RS
5183#ifdef MSDOS
5184 printf ("\r\nContinuing...\r\n");
5185#else /* not MSDOS */
284f4730 5186 printf ("Continuing...\n");
80e4aa30 5187#endif /* not MSDOS */
284f4730
JB
5188 fflush (stdout);
5189 init_sys_modes ();
5190 }
5191 else
5192 {
5193 /* If executing a function that wants to be interrupted out of
5194 and the user has not deferred quitting by binding `inhibit-quit'
5195 then quit right away. */
5196 if (immediate_quit && NILP (Vinhibit_quit))
5197 {
5198 immediate_quit = 0;
5199 sigfree ();
5200 Fsignal (Qquit, Qnil);
5201 }
5202 else
5203 /* Else request quit when it's safe */
5204 Vquit_flag = Qt;
5205 }
5206
5207 if (waiting_for_input && !echoing)
5208 quit_throw_to_read_char ();
5209
5210 errno = old_errno;
5211}
5212
5213/* Handle a C-g by making read_char return C-g. */
5214
5215quit_throw_to_read_char ()
5216{
5217 quit_error_check ();
5218 sigfree ();
5219 /* Prevent another signal from doing this before we finish. */
f76475ad 5220 clear_waiting_for_input ();
284f4730
JB
5221 input_pending = 0;
5222
dbc4e1c1 5223 unread_command_events = Qnil;
86e5706b 5224 unread_command_char = -1;
284f4730 5225
e6b01c14
JB
5226#ifdef POLL_FOR_INPUT
5227 /* May be > 1 if in recursive minibuffer. */
5228 if (poll_suppress_count == 0)
5229 abort ();
5230#endif
0dfcc832 5231#ifdef MULTI_FRAME
e9bf89a0
RS
5232 if (XTYPE (internal_last_event_frame) == Lisp_Frame
5233 && XFRAME (internal_last_event_frame) != selected_frame)
3b9189f8 5234 Fhandle_switch_frame (make_lispy_switch_frame (internal_last_event_frame));
0dfcc832 5235#endif
e6b01c14 5236
284f4730
JB
5237 _longjmp (getcjmp, 1);
5238}
5239\f
5240DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
5241 "Set mode of reading keyboard input.\n\
464f8898
RS
5242First arg INTERRUPT non-nil means use input interrupts;\n\
5243 nil means use CBREAK mode.\n\
5244Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
284f4730 5245 (no effect except in CBREAK mode).\n\
b04904fb
RS
5246Third arg META t means accept 8-bit input (for a Meta key).\n\
5247 META nil means ignore the top bit, on the assumption it is parity.\n\
5248 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
a8ee7ef9
RS
5249Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
5250See also `current-input-mode'.")
284f4730
JB
5251 (interrupt, flow, meta, quit)
5252 Lisp_Object interrupt, flow, meta, quit;
5253{
5254 if (!NILP (quit)
5255 && (XTYPE (quit) != Lisp_Int
5256 || XINT (quit) < 0 || XINT (quit) > 0400))
34f04431
RS
5257 error ("set-input-mode: QUIT must be an ASCII character");
5258
5259#ifdef POLL_FOR_INPUT
5260 stop_polling ();
5261#endif
284f4730
JB
5262
5263 reset_sys_modes ();
5264#ifdef SIGIO
5265/* Note SIGIO has been undef'd if FIONREAD is missing. */
5266#ifdef NO_SOCK_SIGIO
5267 if (read_socket_hook)
5268 interrupt_input = 0; /* No interrupts if reading from a socket. */
5269 else
5270#endif /* NO_SOCK_SIGIO */
5271 interrupt_input = !NILP (interrupt);
5272#else /* not SIGIO */
5273 interrupt_input = 0;
5274#endif /* not SIGIO */
5275/* Our VMS input only works by interrupts, as of now. */
5276#ifdef VMS
5277 interrupt_input = 1;
5278#endif
5279 flow_control = !NILP (flow);
b04904fb
RS
5280 if (NILP (meta))
5281 meta_key = 0;
5282 else if (EQ (meta, Qt))
5283 meta_key = 1;
5284 else
5285 meta_key = 2;
284f4730
JB
5286 if (!NILP (quit))
5287 /* Don't let this value be out of range. */
5288 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
5289
5290 init_sys_modes ();
34f04431
RS
5291
5292#ifdef POLL_FOR_INPUT
5293 poll_suppress_count = 1;
5294 start_polling ();
5295#endif
284f4730
JB
5296 return Qnil;
5297}
80645119
JB
5298
5299DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
5300 "Return information about the way Emacs currently reads keyboard input.\n\
5301The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
5302 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
5303 nil, Emacs is using CBREAK mode.\n\
5304 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
5305 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
a8ee7ef9
RS
5306 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
5307 META nil means ignoring the top bit, on the assumption it is parity.\n\
5308 META is neither t nor nil if accepting 8-bit input and using\n\
5309 all 8 bits as the character code.\n\
80645119
JB
5310 QUIT is the character Emacs currently uses to quit.\n\
5311The elements of this list correspond to the arguments of\n\
a8ee7ef9 5312`set-input-mode'.")
80645119
JB
5313 ()
5314{
5315 Lisp_Object val[4];
5316
5317 val[0] = interrupt_input ? Qt : Qnil;
5318 val[1] = flow_control ? Qt : Qnil;
a8ee7ef9 5319 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
7b50ece7 5320 XFASTINT (val[3]) = quit_char;
80645119 5321
bf673a7a 5322 return Flist (sizeof (val) / sizeof (val[0]), val);
80645119
JB
5323}
5324
284f4730
JB
5325\f
5326init_keyboard ()
5327{
284f4730
JB
5328 /* This is correct before outermost invocation of the editor loop */
5329 command_loop_level = -1;
5330 immediate_quit = 0;
5331 quit_char = Ctl ('g');
dbc4e1c1 5332 unread_command_events = Qnil;
86e5706b 5333 unread_command_char = -1;
284f4730 5334 total_keys = 0;
9deb415a 5335 recent_keys_index = 0;
284f4730
JB
5336 kbd_fetch_ptr = kbd_buffer;
5337 kbd_store_ptr = kbd_buffer;
5338 do_mouse_tracking = 0;
5339 input_pending = 0;
5340
07d2b8de 5341#ifdef MULTI_FRAME
8f805655
JB
5342 /* This means that command_loop_1 won't try to select anything the first
5343 time through. */
3c370943
JB
5344 internal_last_event_frame = Qnil;
5345 Vlast_event_frame = internal_last_event_frame;
7b4aedb9
JB
5346#endif
5347
5348 /* If we're running a dumped Emacs, we need to clear out
5349 kbd_buffer_frame_or_window, in case some events got into it
5350 before we dumped.
4bb994d1 5351
7b4aedb9
JB
5352 If we're running an undumped Emacs, it hasn't been initialized by
5353 syms_of_keyboard yet. */
4bb994d1 5354 if (initialized)
7b4aedb9 5355 Ffillarray (kbd_buffer_frame_or_window, Qnil);
07d2b8de 5356
284f4730
JB
5357 if (!noninteractive)
5358 {
5359 signal (SIGINT, interrupt_signal);
cb5df6ae 5360#if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
284f4730
JB
5361 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
5362 SIGQUIT and we can't tell which one it will give us. */
5363 signal (SIGQUIT, interrupt_signal);
5364#endif /* HAVE_TERMIO */
5365/* Note SIGIO has been undef'd if FIONREAD is missing. */
5366#ifdef SIGIO
5367 signal (SIGIO, input_available_signal);
8ea0a720 5368#endif /* SIGIO */
284f4730
JB
5369 }
5370
5371/* Use interrupt input by default, if it works and noninterrupt input
5372 has deficiencies. */
5373
5374#ifdef INTERRUPT_INPUT
5375 interrupt_input = 1;
5376#else
5377 interrupt_input = 0;
5378#endif
5379
5380/* Our VMS input only works by interrupts, as of now. */
5381#ifdef VMS
5382 interrupt_input = 1;
5383#endif
5384
5385 sigfree ();
5386 dribble = 0;
5387
5388 if (keyboard_init_hook)
5389 (*keyboard_init_hook) ();
5390
5391#ifdef POLL_FOR_INPUT
5392 poll_suppress_count = 1;
5393 start_polling ();
5394#endif
5395}
5396
5397/* This type's only use is in syms_of_keyboard, to initialize the
5398 event header symbols and put properties on them. */
5399struct event_head {
5400 Lisp_Object *var;
5401 char *name;
5402 Lisp_Object *kind;
5403};
5404
5405struct event_head head_table[] = {
7b4aedb9 5406 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
3c370943 5407 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
7b4aedb9 5408 &Qswitch_frame, "switch-frame", &Qswitch_frame,
284f4730
JB
5409};
5410
5411syms_of_keyboard ()
5412{
2e894dab
RS
5413 Qdisabled_command_hook = intern ("disabled-command-hook");
5414 staticpro (&Qdisabled_command_hook);
5415
284f4730
JB
5416 Qself_insert_command = intern ("self-insert-command");
5417 staticpro (&Qself_insert_command);
5418
5419 Qforward_char = intern ("forward-char");
5420 staticpro (&Qforward_char);
5421
5422 Qbackward_char = intern ("backward-char");
5423 staticpro (&Qbackward_char);
5424
5425 Qdisabled = intern ("disabled");
5426 staticpro (&Qdisabled);
5427
e58aa385
RS
5428 Qundefined = intern ("undefined");
5429 staticpro (&Qundefined);
5430
86e5706b
RS
5431 Qpre_command_hook = intern ("pre-command-hook");
5432 staticpro (&Qpre_command_hook);
5433
5434 Qpost_command_hook = intern ("post-command-hook");
5435 staticpro (&Qpost_command_hook);
5436
284f4730
JB
5437 Qfunction_key = intern ("function-key");
5438 staticpro (&Qfunction_key);
13b5e56c 5439 Qmouse_click = intern ("mouse-click");
284f4730 5440 staticpro (&Qmouse_click);
284f4730 5441
598a9fa7
JB
5442 Qmenu_enable = intern ("menu-enable");
5443 staticpro (&Qmenu_enable);
5444
284f4730
JB
5445 Qmode_line = intern ("mode-line");
5446 staticpro (&Qmode_line);
e5d77022
JB
5447 Qvertical_line = intern ("vertical-line");
5448 staticpro (&Qvertical_line);
3c370943
JB
5449 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
5450 staticpro (&Qvertical_scroll_bar);
5ec75a55
RS
5451 Qmenu_bar = intern ("menu-bar");
5452 staticpro (&Qmenu_bar);
4bb994d1
JB
5453
5454 Qabove_handle = intern ("above-handle");
5455 staticpro (&Qabove_handle);
5456 Qhandle = intern ("handle");
5457 staticpro (&Qhandle);
5458 Qbelow_handle = intern ("below-handle");
5459 staticpro (&Qbelow_handle);
284f4730 5460
cd21b839 5461 Qevent_kind = intern ("event-kind");
284f4730 5462 staticpro (&Qevent_kind);
88cb0656
JB
5463 Qevent_symbol_elements = intern ("event-symbol-elements");
5464 staticpro (&Qevent_symbol_elements);
0a7f1fc0
JB
5465 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
5466 staticpro (&Qevent_symbol_element_mask);
5467 Qmodifier_cache = intern ("modifier-cache");
5468 staticpro (&Qmodifier_cache);
284f4730 5469
48e416d4
RS
5470 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
5471 staticpro (&Qrecompute_lucid_menubar);
5472 Qactivate_menubar_hook = intern ("activate-menubar-hook");
5473 staticpro (&Qactivate_menubar_hook);
5474
284f4730
JB
5475 {
5476 struct event_head *p;
5477
5478 for (p = head_table;
5479 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
5480 p++)
5481 {
5482 *p->var = intern (p->name);
5483 staticpro (p->var);
5484 Fput (*p->var, Qevent_kind, *p->kind);
88cb0656 5485 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
284f4730
JB
5486 }
5487 }
5488
7b4aedb9
JB
5489 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
5490 staticpro (&button_down_location);
88cb0656
JB
5491
5492 {
5493 int i;
5494 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
5495
5496 modifier_symbols = Fmake_vector (make_number (len), Qnil);
5497 for (i = 0; i < len; i++)
86e5706b
RS
5498 if (modifier_names[i])
5499 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
88cb0656
JB
5500 staticpro (&modifier_symbols);
5501 }
5502
9deb415a
JB
5503 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
5504 staticpro (&recent_keys);
5505
6569cc8d 5506 this_command_keys = Fmake_vector (make_number (40), Qnil);
715d9345 5507 staticpro (&this_command_keys);
6569cc8d 5508
03b4122a
BF
5509 Qextended_command_history = intern ("extended-command-history");
5510 Fset (Qextended_command_history, Qnil);
5511 staticpro (&Qextended_command_history);
5512
7b4aedb9
JB
5513 kbd_buffer_frame_or_window
5514 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
5515 staticpro (&kbd_buffer_frame_or_window);
4bb994d1 5516
284f4730
JB
5517 func_key_syms = Qnil;
5518 staticpro (&func_key_syms);
5519
5520 mouse_syms = Qnil;
5521 staticpro (&mouse_syms);
5522
cd21b839
JB
5523 unread_switch_frame = Qnil;
5524 staticpro (&unread_switch_frame);
5525
284f4730
JB
5526 defsubr (&Sread_key_sequence);
5527 defsubr (&Srecursive_edit);
5528 defsubr (&Strack_mouse);
284f4730
JB
5529 defsubr (&Sinput_pending_p);
5530 defsubr (&Scommand_execute);
5531 defsubr (&Srecent_keys);
5532 defsubr (&Sthis_command_keys);
5533 defsubr (&Ssuspend_emacs);
5534 defsubr (&Sabort_recursive_edit);
5535 defsubr (&Sexit_recursive_edit);
5536 defsubr (&Srecursion_depth);
5537 defsubr (&Stop_level);
5538 defsubr (&Sdiscard_input);
5539 defsubr (&Sopen_dribble_file);
5540 defsubr (&Sset_input_mode);
80645119 5541 defsubr (&Scurrent_input_mode);
284f4730
JB
5542 defsubr (&Sexecute_extended_command);
5543
284f4730 5544 DEFVAR_LISP ("last-command-char", &last_command_char,
86e5706b
RS
5545 "Last input event that was part of a command.");
5546
5547 DEFVAR_LISP ("last-command-event", &last_command_char,
5548 "Last input event that was part of a command.");
284f4730 5549
7d6de002 5550 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
86e5706b 5551 "Last input event in a command, except for mouse menu events.\n\
7d6de002
RS
5552Mouse menus give back keys that don't look like mouse events;\n\
5553this variable holds the actual mouse event that led to the menu,\n\
5554so that you can determine whether the command was run by mouse or not.");
5555
284f4730 5556 DEFVAR_LISP ("last-input-char", &last_input_char,
86e5706b
RS
5557 "Last input event.");
5558
5559 DEFVAR_LISP ("last-input-event", &last_input_char,
5560 "Last input event.");
284f4730 5561
dbc4e1c1 5562 DEFVAR_LISP ("unread-command-events", &unread_command_events,
1c07d0a6 5563 "List of objects to be read as next command input events.");
284f4730 5564
86e5706b
RS
5565 DEFVAR_INT ("unread-command-char", &unread_command_char,
5566 "If not -1, an object to be read as next command input event.");
5567
284f4730
JB
5568 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
5569 "Meta-prefix character code. Meta-foo as command input\n\
5570turns into this character followed by foo.");
5571 XSET (meta_prefix_char, Lisp_Int, 033);
5572
5573 DEFVAR_LISP ("last-command", &last_command,
5574 "The last command executed. Normally a symbol with a function definition,\n\
5575but can be whatever was found in the keymap, or whatever the variable\n\
5576`this-command' was set to by that command.");
5577 last_command = Qnil;
5578
5579 DEFVAR_LISP ("this-command", &this_command,
5580 "The command now being executed.\n\
5581The command can set this variable; whatever is put here\n\
5582will be in `last-command' during the following command.");
5583 this_command = Qnil;
5584
5585 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
5586 "*Number of keyboard input characters between auto-saves.\n\
5587Zero means disable autosaving due to number of characters typed.");
5588 auto_save_interval = 300;
5589
5590 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
5591 "*Number of seconds idle time before auto-save.\n\
06ef7355
RS
5592Zero or nil means disable auto-saving due to idleness.\n\
5593After auto-saving due to this many seconds of idle time,\n\
84447c71 5594Emacs also does a garbage collection if that seems to be warranted.");
284f4730
JB
5595 XFASTINT (Vauto_save_timeout) = 30;
5596
5597 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
5598 "*Nonzero means echo unfinished commands after this many seconds of pause.");
5599 echo_keystrokes = 1;
5600
5601 DEFVAR_INT ("polling-period", &polling_period,
5602 "*Interval between polling for input during Lisp execution.\n\
5603The reason for polling is to make C-g work to stop a running program.\n\
5604Polling is needed only when using X windows and SIGIO does not work.\n\
5605Polling is automatically disabled in all other cases.");
5606 polling_period = 2;
5607
564dc952 5608 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
fbcd35bd 5609 "*Maximum time between mouse clicks to make a double-click.\n\
564dc952
JB
5610Measured in milliseconds. nil means disable double-click recognition;\n\
5611t means double-clicks have no time limit and are detected\n\
fbcd35bd 5612by position only.");
aab06933 5613 Vdouble_click_time = make_number (500);
fbcd35bd 5614
284f4730
JB
5615 DEFVAR_INT ("num-input-keys", &num_input_keys,
5616 "*Number of complete keys read from the keyboard so far.");
5617 num_input_keys = 0;
5618
07d2b8de 5619#ifdef MULTI_FRAME
ff11dfa1 5620 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
fce33686
JB
5621 "*The frame in which the most recently read event occurred.\n\
5622If the last event came from a keyboard macro, this is set to `macro'.");
ff11dfa1 5623 Vlast_event_frame = Qnil;
07d2b8de 5624#endif
284f4730 5625
7e85b935 5626 DEFVAR_LISP ("help-char", &Vhelp_char,
284f4730
JB
5627 "Character to recognize as meaning Help.\n\
5628When it is read, do `(eval help-form)', and display result if it's a string.\n\
5629If the value of `help-form' is nil, this char can be read normally.");
7e85b935 5630 XSET (Vhelp_char, Lisp_Int, Ctl ('H'));
284f4730
JB
5631
5632 DEFVAR_LISP ("help-form", &Vhelp_form,
7e85b935 5633 "Form to execute when character `help-char' is read.\n\
284f4730
JB
5634If the form returns a string, that string is displayed.\n\
5635If `help-form' is nil, the help char is not recognized.");
5636 Vhelp_form = Qnil;
5637
7e85b935
RS
5638 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
5639 "Command to run when `help-char' character follows a prefix key.\n\
5640This command is used only when there is no actual binding\n\
5641for that character after that prefix key.");
5642 Vprefix_help_command = Qnil;
5643
284f4730
JB
5644 DEFVAR_LISP ("top-level", &Vtop_level,
5645 "Form to evaluate when Emacs starts up.\n\
5646Useful to set before you dump a modified Emacs.");
5647 Vtop_level = Qnil;
5648
5649 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
5650 "String used as translate table for keyboard input, or nil.\n\
5651Each character is looked up in this string and the contents used instead.\n\
5652If string is of length N, character codes N and up are untranslated.");
5653 Vkeyboard_translate_table = Qnil;
5654
a612e298
RS
5655 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
5656 "Keymap of key translations that can override keymaps.\n\
5657This keymap works like `function-key-map', but comes after that,\n\
5658and applies even for keys that have ordinary bindings.");
5659 Vkey_translation_map = Qnil;
5660
284f4730 5661 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
7d6de002 5662 "Non-nil means prompt with menus when appropriate.\n\
284f4730 5663This is done when reading from a keymap that has a prompt string,\n\
7d6de002
RS
5664for elements that have prompt strings.\n\
5665The menu is displayed on the screen\n\
5666if X menus were enabled at configuration\n\
5667time and the previous event was a mouse click prefix key.\n\
5668Otherwise, menu prompting uses the echo area.");
284f4730
JB
5669 menu_prompting = 1;
5670
5671 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
5672 "Character to see next line of menu prompt.\n\
5673Type this character while in a menu prompt to rotate around the lines of it.");
5674 XSET (menu_prompt_more_char, Lisp_Int, ' ');
9fa4395d
RS
5675
5676 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
5677 "A mask of additional modifier keys to use with every keyboard character.\n\
ad163903
JB
5678Emacs applies the modifiers of the character stored here to each keyboard\n\
5679character it reads. For example, after evaluating the expression\n\
80645119
JB
5680 (setq extra-keyboard-modifiers ?\C-x)\n\
5681all input characters will have the control modifier applied to them.\n\
5682\n\
5683Note that the character ?\C-@, equivalent to the integer zero, does\n\
5684not count as a control character; rather, it counts as a character\n\
27203ead 5685with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
80645119 5686cancels any modification.");
9fa4395d 5687 extra_keyboard_modifiers = 0;
86e5706b
RS
5688
5689 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
5690 "If an editing command sets this to t, deactivate the mark afterward.\n\
5691The command loop sets this to nil before each command,\n\
5692and tests the value when the command returns.\n\
5693Buffer modification stores t in this variable.");
5694 Vdeactivate_mark = Qnil;
5695
5696 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
5697 "Normal hook run before each command is executed.");
5698 Vpre_command_hook = Qnil;
5699
5700 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
7e85b935 5701 "Normal hook run after each command is executed.");
86e5706b 5702 Vpost_command_hook = Qnil;
48e416d4
RS
5703
5704 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
5705 "t means menu bar, specified Lucid style, needs to be recomputed.");
5706 Vlucid_menu_bar_dirty_flag = Qnil;
a73c5e29 5707
9f9c0e27
RS
5708 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
5709 "List of menu bar items to move to the end of the menu bar.\n\
a612e298 5710The elements of the list are event types that may have menu bar bindings.");
9f9c0e27 5711 Vmenu_bar_final_items = Qnil;
e9bf89a0
RS
5712
5713 DEFVAR_BOOL ("track-mouse", &do_mouse_tracking,
5714 "*Non-nil means generate motion events for mouse motion.");
80e4aa30
RS
5715
5716 DEFVAR_LISP ("vendor-key-alist", &Vvendor_key_alist,
5717 "Alist of vendor-specific X windows key symbols.\n\
5718Each element should have the form (N . SYMBOL) where N is the\n\
5719numeric keysym code (sans the \"vendor-specific\" bit 1<<28)\n\
5720and SYMBOL is its name.");
5721 Vmenu_bar_final_items = Qnil;
284f4730
JB
5722}
5723
5724keys_of_keyboard ()
5725{
5726 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
5727 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
5728 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
5729 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
5730 initial_define_key (meta_map, 'x', "execute-extended-command");
5731}