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