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