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