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