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