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