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