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