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