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