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