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