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