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