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