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