(PENTIUM:CPunix:4.0*:*): New case.
[emacs/old-mirror.git] / src / keyboard.c
blob625e14b92474b4f50587a35d3d58dc7ec7db886b
1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985,86,87,88,89,93,94,95,96 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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. */
21 /* Allow config.h to undefine symbols found here. */
22 #include <signal.h>
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 "disptab.h"
36 #include "dispextern.h"
37 #include "keyboard.h"
38 #include "intervals.h"
39 #include "blockinput.h"
40 #include <setjmp.h>
41 #include <errno.h>
43 #ifdef MSDOS
44 #include "msdos.h"
45 #include <time.h>
46 #else /* not MSDOS */
47 #ifndef VMS
48 #include <sys/ioctl.h>
49 #endif
50 #endif /* not MSDOS */
52 #include "syssignal.h"
53 #include "systty.h"
55 /* This is to get the definitions of the XK_ symbols. */
56 #ifdef HAVE_X_WINDOWS
57 #include "xterm.h"
58 #endif
60 #ifdef HAVE_NTGUI
61 #include "w32term.h"
62 #endif /* HAVE_NTGUI */
64 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
65 #include "systime.h"
67 extern int errno;
69 /* Variables for blockinput.h: */
71 /* Non-zero if interrupt input is blocked right now. */
72 int interrupt_input_blocked;
74 /* Nonzero means an input interrupt has arrived
75 during the current critical section. */
76 int interrupt_input_pending;
79 /* File descriptor to use for input. */
80 extern int input_fd;
82 #ifdef HAVE_WINDOW_SYSTEM
83 /* Make all keyboard buffers much bigger when using X windows. */
84 #define KBD_BUFFER_SIZE 4096
85 #else /* No X-windows, character input */
86 #define KBD_BUFFER_SIZE 256
87 #endif /* No X-windows */
89 /* Following definition copied from eval.c */
91 struct backtrace
93 struct backtrace *next;
94 Lisp_Object *function;
95 Lisp_Object *args; /* Points to vector of args. */
96 int nargs; /* length of vector. If nargs is UNEVALLED,
97 args points to slot holding list of
98 unevalled args */
99 char evalargs;
102 #ifdef MULTI_KBOARD
103 KBOARD *initial_kboard;
104 KBOARD *current_kboard;
105 KBOARD *all_kboards;
106 int single_kboard;
107 #else
108 KBOARD the_only_kboard;
109 #endif
111 /* Non-nil disable property on a command means
112 do not execute it; call disabled-command-hook's value instead. */
113 Lisp_Object Qdisabled, Qdisabled_command_hook;
115 #define NUM_RECENT_KEYS (100)
116 int recent_keys_index; /* Index for storing next element into recent_keys */
117 int total_keys; /* Total number of elements stored into recent_keys */
118 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
120 /* Vector holding the key sequence that invoked the current command.
121 It is reused for each command, and it may be longer than the current
122 sequence; this_command_key_count indicates how many elements
123 actually mean something.
124 It's easier to staticpro a single Lisp_Object than an array. */
125 Lisp_Object this_command_keys;
126 int this_command_key_count;
128 /* Number of elements of this_command_keys
129 that precede this key sequence. */
130 int this_single_command_key_start;
132 /* Record values of this_command_key_count and echo_length ()
133 before this command was read. */
134 static int before_command_key_count;
135 static int before_command_echo_length;
136 /* Values of before_command_key_count and before_command_echo_length
137 saved by reset-this-command-lengths. */
138 static int before_command_key_count_1;
139 static int before_command_echo_length_1;
140 /* Flag set by reset-this-command-lengths,
141 saying to reset the lengths when add_command_key is called. */
142 static int before_command_restore_flag;
144 extern int minbuf_level;
146 extern struct backtrace *backtrace_list;
148 /* Nonzero means do menu prompting. */
149 static int menu_prompting;
151 /* Character to see next line of menu prompt. */
152 static Lisp_Object menu_prompt_more_char;
154 /* For longjmp to where kbd input is being done. */
155 static jmp_buf getcjmp;
157 /* True while doing kbd input. */
158 int waiting_for_input;
160 /* True while displaying for echoing. Delays C-g throwing. */
161 static int echoing;
163 /* True means we can start echoing at the next input pause
164 even though there is something in the echo area. */
165 static char *ok_to_echo_at_next_pause;
167 /* Nonzero means disregard local maps for the menu bar. */
168 static int inhibit_local_menu_bar_menus;
170 /* Nonzero means C-g should cause immediate error-signal. */
171 int immediate_quit;
173 /* Character to recognize as the help char. */
174 Lisp_Object Vhelp_char;
176 /* List of other event types to recognize as meaning "help". */
177 Lisp_Object Vhelp_event_list;
179 /* Form to execute when help char is typed. */
180 Lisp_Object Vhelp_form;
182 /* Command to run when the help character follows a prefix key. */
183 Lisp_Object Vprefix_help_command;
185 /* List of items that should move to the end of the menu bar. */
186 Lisp_Object Vmenu_bar_final_items;
188 /* Non-nil means show the equivalent key-binding for
189 any M-x command that has one.
190 The value can be a length of time to show the message for.
191 If the value is non-nil and not a number, we wait 2 seconds. */
192 Lisp_Object Vsuggest_key_bindings;
194 /* Character that causes a quit. Normally C-g.
196 If we are running on an ordinary terminal, this must be an ordinary
197 ASCII char, since we want to make it our interrupt character.
199 If we are not running on an ordinary terminal, it still needs to be
200 an ordinary ASCII char. This character needs to be recognized in
201 the input interrupt handler. At this point, the keystroke is
202 represented as a struct input_event, while the desired quit
203 character is specified as a lispy event. The mapping from struct
204 input_events to lispy events cannot run in an interrupt handler,
205 and the reverse mapping is difficult for anything but ASCII
206 keystrokes.
208 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
209 ASCII character. */
210 int quit_char;
212 extern Lisp_Object current_global_map;
213 extern int minibuf_level;
215 /* If non-nil, this is a map that overrides all other local maps. */
216 Lisp_Object Voverriding_local_map;
218 /* If non-nil, Voverriding_local_map applies to the menu bar. */
219 Lisp_Object Voverriding_local_map_menu_flag;
221 /* Keymap that defines special misc events that should
222 be processed immediately at a low level. */
223 Lisp_Object Vspecial_event_map;
225 /* Current depth in recursive edits. */
226 int command_loop_level;
228 /* Total number of times command_loop has read a key sequence. */
229 int num_input_keys;
231 /* Last input character read as a command. */
232 Lisp_Object last_command_char;
234 /* Last input character read as a command, not counting menus
235 reached by the mouse. */
236 Lisp_Object last_nonmenu_event;
238 /* Last input character read for any purpose. */
239 Lisp_Object last_input_char;
241 /* If not Qnil, a list of objects to be read as subsequent command input. */
242 Lisp_Object Vunread_command_events;
244 /* If not -1, an event to be read as subsequent command input. */
245 int unread_command_char;
247 /* If not Qnil, this is a switch-frame event which we decided to put
248 off until the end of a key sequence. This should be read as the
249 next command input, after any unread_command_events.
251 read_key_sequence uses this to delay switch-frame events until the
252 end of the key sequence; Fread_char uses it to put off switch-frame
253 events until a non-ASCII event is acceptable as input. */
254 Lisp_Object unread_switch_frame;
256 /* A mask of extra modifier bits to put into every keyboard char. */
257 int extra_keyboard_modifiers;
259 /* Char to use as prefix when a meta character is typed in.
260 This is bound on entry to minibuffer in case ESC is changed there. */
262 Lisp_Object meta_prefix_char;
264 /* Last size recorded for a current buffer which is not a minibuffer. */
265 static int last_non_minibuf_size;
267 /* Number of idle seconds before an auto-save and garbage collection. */
268 static Lisp_Object Vauto_save_timeout;
270 /* Total number of times read_char has returned. */
271 int num_input_chars;
273 /* Total number of times read_char has returned, outside of macros. */
274 int num_nonmacro_input_chars;
276 /* Auto-save automatically when this many characters have been typed
277 since the last time. */
279 static int auto_save_interval;
281 /* Value of num_nonmacro_input_chars as of last auto save. */
283 int last_auto_save;
285 /* The command being executed by the command loop.
286 Commands may set this, and the value set will be copied into
287 current_kboard->Vlast_command instead of the actual command. */
288 Lisp_Object this_command;
290 /* The value of point when the last command was executed. */
291 int last_point_position;
293 /* The buffer that was current when the last command was started. */
294 Lisp_Object last_point_position_buffer;
296 /* The frame in which the last input event occurred, or Qmacro if the
297 last event came from a macro. We use this to determine when to
298 generate switch-frame events. This may be cleared by functions
299 like Fselect_frame, to make sure that a switch-frame event is
300 generated by the next character. */
301 Lisp_Object internal_last_event_frame;
303 /* A user-visible version of the above, intended to allow users to
304 figure out where the last event came from, if the event doesn't
305 carry that information itself (i.e. if it was a character). */
306 Lisp_Object Vlast_event_frame;
308 /* The timestamp of the last input event we received from the X server.
309 X Windows wants this for selection ownership. */
310 unsigned long last_event_timestamp;
312 Lisp_Object Qself_insert_command;
313 Lisp_Object Qforward_char;
314 Lisp_Object Qbackward_char;
315 Lisp_Object Qundefined;
317 /* read_key_sequence stores here the command definition of the
318 key sequence that it reads. */
319 Lisp_Object read_key_sequence_cmd;
321 /* Form to evaluate (if non-nil) when Emacs is started. */
322 Lisp_Object Vtop_level;
324 /* User-supplied string to translate input characters through. */
325 Lisp_Object Vkeyboard_translate_table;
327 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
328 extern Lisp_Object Vfunction_key_map;
330 /* Another keymap that maps key sequences into key sequences.
331 This one takes precedence over ordinary definitions. */
332 extern Lisp_Object Vkey_translation_map;
334 /* Non-nil means deactivate the mark at end of this command. */
335 Lisp_Object Vdeactivate_mark;
337 /* Menu bar specified in Lucid Emacs fashion. */
339 Lisp_Object Vlucid_menu_bar_dirty_flag;
340 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
342 /* Hooks to run before and after each command. */
343 Lisp_Object Qpre_command_hook, Vpre_command_hook;
344 Lisp_Object Qpost_command_hook, Vpost_command_hook;
345 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
346 /* Hook run after a command if there's no more input soon. */
347 Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook;
349 /* Delay time in microseconds before running post-command-idle-hook. */
350 int post_command_idle_delay;
352 /* List of deferred actions to be performed at a later time.
353 The precise format isn't relevant here; we just check whether it is nil. */
354 Lisp_Object Vdeferred_action_list;
356 /* Function to call to handle deferred actions, when there are any. */
357 Lisp_Object Vdeferred_action_function;
358 Lisp_Object Qdeferred_action_function;
360 /* File in which we write all commands we read. */
361 FILE *dribble;
363 /* Nonzero if input is available. */
364 int input_pending;
366 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
367 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
369 int meta_key;
371 extern char *pending_malloc_warning;
373 /* Circular buffer for pre-read keyboard input. */
374 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
376 /* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
378 The interrupt-level event handlers will never enqueue an event on a
379 frame which is not in Vframe_list, and once an event is dequeued,
380 internal_last_event_frame or the event itself points to the frame.
381 So that's all fine.
383 But while the event is sitting in the queue, it's completely
384 unprotected. Suppose the user types one command which will run for
385 a while and then delete a frame, and then types another event at
386 the frame that will be deleted, before the command gets around to
387 it. Suppose there are no references to this frame elsewhere in
388 Emacs, and a GC occurs before the second event is dequeued. Now we
389 have an event referring to a freed frame, which will crash Emacs
390 when it is dequeued.
392 Similar things happen when an event on a scroll bar is enqueued; the
393 window may be deleted while the event is in the queue.
395 So, we use this vector to protect the frame_or_window field in the
396 event queue. That way, they'll be dequeued as dead frames or
397 windows, but still valid lisp objects.
399 If kbd_buffer[i].kind != no_event, then
400 (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
401 == kbd_buffer[i].frame_or_window. */
402 static Lisp_Object kbd_buffer_frame_or_window;
404 /* Pointer to next available character in kbd_buffer.
405 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
406 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
407 next available char is in kbd_buffer[0]. */
408 static struct input_event *kbd_fetch_ptr;
410 /* Pointer to next place to store character in kbd_buffer. This
411 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
412 character should go in kbd_buffer[0]. */
413 static volatile struct input_event *kbd_store_ptr;
415 /* The above pair of variables forms a "queue empty" flag. When we
416 enqueue a non-hook event, we increment kbd_store_ptr. When we
417 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
418 there is input available iff the two pointers are not equal.
420 Why not just have a flag set and cleared by the enqueuing and
421 dequeuing functions? Such a flag could be screwed up by interrupts
422 at inopportune times. */
424 /* If this flag is non-nil, we check mouse_moved to see when the
425 mouse moves, and motion events will appear in the input stream.
426 Otherwise, mouse motion is ignored. */
427 static Lisp_Object do_mouse_tracking;
429 /* Symbols to head events. */
430 Lisp_Object Qmouse_movement;
431 Lisp_Object Qscroll_bar_movement;
432 Lisp_Object Qswitch_frame;
433 Lisp_Object Qdelete_frame;
434 Lisp_Object Qiconify_frame;
435 Lisp_Object Qmake_frame_visible;
437 /* Symbols to denote kinds of events. */
438 Lisp_Object Qfunction_key;
439 Lisp_Object Qmouse_click;
440 Lisp_Object Qtimer_event;
441 /* Lisp_Object Qmouse_movement; - also an event header */
443 /* Properties of event headers. */
444 Lisp_Object Qevent_kind;
445 Lisp_Object Qevent_symbol_elements;
447 Lisp_Object Qmenu_enable;
449 /* An event header symbol HEAD may have a property named
450 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
451 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
452 mask of modifiers applied to it. If present, this is used to help
453 speed up parse_modifiers. */
454 Lisp_Object Qevent_symbol_element_mask;
456 /* An unmodified event header BASE may have a property named
457 Qmodifier_cache, which is an alist mapping modifier masks onto
458 modified versions of BASE. If present, this helps speed up
459 apply_modifiers. */
460 Lisp_Object Qmodifier_cache;
462 /* Symbols to use for parts of windows. */
463 Lisp_Object Qmode_line;
464 Lisp_Object Qvertical_line;
465 Lisp_Object Qvertical_scroll_bar;
466 Lisp_Object Qmenu_bar;
468 extern Lisp_Object Qmenu_enable;
470 Lisp_Object recursive_edit_unwind (), command_loop ();
471 Lisp_Object Fthis_command_keys ();
472 Lisp_Object Qextended_command_history;
473 EMACS_TIME timer_check ();
475 extern char *x_get_keysym_name ();
477 Lisp_Object Qpolling_period;
479 /* List of absolute timers. Appears in order of next scheduled event. */
480 Lisp_Object Vtimer_list;
482 /* List of idle time timers. Appears in order of next scheduled event. */
483 Lisp_Object Vtimer_idle_list;
485 /* Incremented whenever a timer is run. */
486 int timers_run;
488 extern Lisp_Object Vprint_level, Vprint_length;
490 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
491 happens. */
492 EMACS_TIME *input_available_clear_time;
494 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
495 Default is 1 if INTERRUPT_INPUT is defined. */
496 int interrupt_input;
498 /* Nonzero while interrupts are temporarily deferred during redisplay. */
499 int interrupts_deferred;
501 /* Nonzero means use ^S/^Q for flow control. */
502 int flow_control;
504 /* Allow m- file to inhibit use of FIONREAD. */
505 #ifdef BROKEN_FIONREAD
506 #undef FIONREAD
507 #endif
509 /* We are unable to use interrupts if FIONREAD is not available,
510 so flush SIGIO so we won't try. */
511 #ifndef FIONREAD
512 #ifdef SIGIO
513 #undef SIGIO
514 #endif
515 #endif
517 /* If we support a window system, turn on the code to poll periodically
518 to detect C-g. It isn't actually used when doing interrupt input. */
519 #ifdef HAVE_WINDOW_SYSTEM
520 #define POLL_FOR_INPUT
521 #endif
523 /* Global variable declarations. */
525 /* Function for init_keyboard to call with no args (if nonzero). */
526 void (*keyboard_init_hook) ();
528 static int read_avail_input ();
529 static void get_input_pending ();
530 static int readable_events ();
531 static Lisp_Object read_char_x_menu_prompt ();
532 static Lisp_Object read_char_minibuf_menu_prompt ();
533 static Lisp_Object make_lispy_event ();
534 #ifdef HAVE_MOUSE
535 static Lisp_Object make_lispy_movement ();
536 #endif
537 static Lisp_Object modify_event_symbol ();
538 static Lisp_Object make_lispy_switch_frame ();
539 static int parse_solitary_modifier ();
541 /* > 0 if we are to echo keystrokes. */
542 static int echo_keystrokes;
544 /* Nonzero means don't try to suspend even if the operating system seems
545 to support it. */
546 static int cannot_suspend;
548 #define min(a,b) ((a)<(b)?(a):(b))
549 #define max(a,b) ((a)>(b)?(a):(b))
551 /* Install the string STR as the beginning of the string of echoing,
552 so that it serves as a prompt for the next character.
553 Also start echoing. */
555 echo_prompt (str)
556 char *str;
558 int len = strlen (str);
560 if (len > ECHOBUFSIZE - 4)
561 len = ECHOBUFSIZE - 4;
562 bcopy (str, current_kboard->echobuf, len);
563 current_kboard->echoptr = current_kboard->echobuf + len;
564 *current_kboard->echoptr = '\0';
566 current_kboard->echo_after_prompt = len;
568 echo_now ();
571 /* Add C to the echo string, if echoing is going on.
572 C can be a character, which is printed prettily ("M-C-x" and all that
573 jazz), or a symbol, whose name is printed. */
575 echo_char (c)
576 Lisp_Object c;
578 extern char *push_key_description ();
580 if (current_kboard->immediate_echo)
582 char *ptr = current_kboard->echoptr;
584 if (ptr != current_kboard->echobuf)
585 *ptr++ = ' ';
587 /* If someone has passed us a composite event, use its head symbol. */
588 c = EVENT_HEAD (c);
590 if (INTEGERP (c))
592 if (ptr - current_kboard->echobuf > ECHOBUFSIZE - 6)
593 return;
595 ptr = push_key_description (XINT (c), ptr);
597 else if (SYMBOLP (c))
599 struct Lisp_String *name = XSYMBOL (c)->name;
600 if ((ptr - current_kboard->echobuf) + name->size + 4 > ECHOBUFSIZE)
601 return;
602 bcopy (name->data, ptr, name->size);
603 ptr += name->size;
606 if (current_kboard->echoptr == current_kboard->echobuf
607 && help_char_p (c))
609 strcpy (ptr, " (Type ? for further options)");
610 ptr += strlen (ptr);
613 *ptr = 0;
614 current_kboard->echoptr = ptr;
616 echo_now ();
620 /* Temporarily add a dash to the end of the echo string if it's not
621 empty, so that it serves as a mini-prompt for the very next character. */
623 echo_dash ()
625 if (!current_kboard->immediate_echo
626 && current_kboard->echoptr == current_kboard->echobuf)
627 return;
628 /* Do nothing if we just printed a prompt. */
629 if (current_kboard->echo_after_prompt
630 == current_kboard->echoptr - current_kboard->echobuf)
631 return;
632 /* Do nothing if not echoing at all. */
633 if (current_kboard->echoptr == 0)
634 return;
636 /* Put a dash at the end of the buffer temporarily,
637 but make it go away when the next character is added. */
638 current_kboard->echoptr[0] = '-';
639 current_kboard->echoptr[1] = 0;
641 echo_now ();
644 /* Display the current echo string, and begin echoing if not already
645 doing so. */
647 echo_now ()
649 if (!current_kboard->immediate_echo)
651 int i;
652 current_kboard->immediate_echo = 1;
654 for (i = 0; i < this_command_key_count; i++)
656 Lisp_Object c;
657 c = XVECTOR (this_command_keys)->contents[i];
658 if (! (EVENT_HAS_PARAMETERS (c)
659 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
660 echo_char (c);
662 echo_dash ();
665 echoing = 1;
666 message1_nolog (current_kboard->echobuf);
667 echoing = 0;
669 if (waiting_for_input && !NILP (Vquit_flag))
670 quit_throw_to_read_char ();
673 /* Turn off echoing, for the start of a new command. */
675 cancel_echoing ()
677 current_kboard->immediate_echo = 0;
678 current_kboard->echoptr = current_kboard->echobuf;
679 current_kboard->echo_after_prompt = -1;
680 ok_to_echo_at_next_pause = 0;
683 /* Return the length of the current echo string. */
685 static int
686 echo_length ()
688 return current_kboard->echoptr - current_kboard->echobuf;
691 /* Truncate the current echo message to its first LEN chars.
692 This and echo_char get used by read_key_sequence when the user
693 switches frames while entering a key sequence. */
695 static void
696 echo_truncate (len)
697 int len;
699 current_kboard->echobuf[len] = '\0';
700 current_kboard->echoptr = current_kboard->echobuf + len;
701 truncate_echo_area (len);
705 /* Functions for manipulating this_command_keys. */
706 static void
707 add_command_key (key)
708 Lisp_Object key;
710 int size = XVECTOR (this_command_keys)->size;
712 /* If reset-this-command-length was called recently, obey it now.
713 See the doc string of that function for an explanation of why. */
714 if (before_command_restore_flag)
716 this_command_key_count = before_command_key_count_1;
717 if (this_command_key_count < this_single_command_key_start)
718 this_single_command_key_start = this_command_key_count;
719 echo_truncate (before_command_echo_length_1);
720 before_command_restore_flag = 0;
723 if (this_command_key_count >= size)
725 Lisp_Object new_keys;
727 new_keys = Fmake_vector (make_number (size * 2), Qnil);
728 bcopy (XVECTOR (this_command_keys)->contents,
729 XVECTOR (new_keys)->contents,
730 size * sizeof (Lisp_Object));
732 this_command_keys = new_keys;
735 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
738 Lisp_Object
739 recursive_edit_1 ()
741 int count = specpdl_ptr - specpdl;
742 Lisp_Object val;
744 if (command_loop_level > 0)
746 specbind (Qstandard_output, Qt);
747 specbind (Qstandard_input, Qt);
750 val = command_loop ();
751 if (EQ (val, Qt))
752 Fsignal (Qquit, Qnil);
753 /* Handle throw from read_minibuf when using minibuffer
754 while it's active but we're in another window. */
755 if (STRINGP (val))
756 Fsignal (Qerror, Fcons (val, Qnil));
758 return unbind_to (count, Qnil);
761 /* When an auto-save happens, record the "time", and don't do again soon. */
763 record_auto_save ()
765 last_auto_save = num_nonmacro_input_chars;
768 /* Make an auto save happen as soon as possible at command level. */
770 force_auto_save_soon ()
772 last_auto_save = - auto_save_interval - 1;
774 record_asynch_buffer_change ();
777 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
778 "Invoke the editor command loop recursively.\n\
779 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
780 that tells this function to return.\n\
781 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
782 This function is called by the editor initialization to begin editing.")
785 int count = specpdl_ptr - specpdl;
786 Lisp_Object val;
788 command_loop_level++;
789 update_mode_lines = 1;
791 record_unwind_protect (recursive_edit_unwind,
792 (command_loop_level
793 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
794 ? Fcurrent_buffer ()
795 : Qnil);
796 recursive_edit_1 ();
797 return unbind_to (count, Qnil);
800 Lisp_Object
801 recursive_edit_unwind (buffer)
802 Lisp_Object buffer;
804 if (!NILP (buffer))
805 Fset_buffer (buffer);
807 command_loop_level--;
808 update_mode_lines = 1;
809 return Qnil;
812 static void
813 any_kboard_state ()
815 #ifdef MULTI_KBOARD
816 #if 0 /* Theory: if there's anything in Vunread_command_events,
817 it will right away be read by read_key_sequence,
818 and then if we do switch KBOARDS, it will go into the side
819 queue then. So we don't need to do anything special here -- rms. */
820 if (CONSP (Vunread_command_events))
822 current_kboard->kbd_queue
823 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
824 current_kboard->kbd_queue_has_data = 1;
826 Vunread_command_events = Qnil;
827 #endif
828 single_kboard = 0;
829 #endif
832 /* Switch to the single-kboard state, making current_kboard
833 the only KBOARD from which further input is accepted. */
835 void
836 single_kboard_state ()
838 #ifdef MULTI_KBOARD
839 single_kboard = 1;
840 #endif
843 /* Maintain a stack of kboards, so other parts of Emacs
844 can switch temporarily to the kboard of a given frame
845 and then revert to the previous status. */
847 struct kboard_stack
849 KBOARD *kboard;
850 struct kboard_stack *next;
853 static struct kboard_stack *kboard_stack;
855 void
856 push_frame_kboard (f)
857 FRAME_PTR f;
859 #ifdef MULTI_KBOARD
860 struct kboard_stack *p
861 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
863 p->next = kboard_stack;
864 p->kboard = current_kboard;
865 kboard_stack = p;
867 current_kboard = FRAME_KBOARD (f);
868 #endif
871 void
872 pop_frame_kboard ()
874 #ifdef MULTI_KBOARD
875 struct kboard_stack *p = kboard_stack;
876 current_kboard = p->kboard;
877 kboard_stack = p->next;
878 xfree (p);
879 #endif
882 /* Handle errors that are not handled at inner levels
883 by printing an error message and returning to the editor command loop. */
885 Lisp_Object
886 cmd_error (data)
887 Lisp_Object data;
889 Lisp_Object old_level, old_length;
890 char macroerror[50];
892 if (!NILP (executing_macro))
894 if (executing_macro_iterations == 1)
895 sprintf (macroerror, "After 1 kbd macro iteration: ");
896 else
897 sprintf (macroerror, "After %d kbd macro iterations: ",
898 executing_macro_iterations);
900 else
901 *macroerror = 0;
903 Vstandard_output = Qt;
904 Vstandard_input = Qt;
905 Vexecuting_macro = Qnil;
906 executing_macro = Qnil;
907 current_kboard->Vprefix_arg = Qnil;
908 cancel_echoing ();
910 /* Avoid unquittable loop if data contains a circular list. */
911 old_level = Vprint_level;
912 old_length = Vprint_length;
913 XSETFASTINT (Vprint_level, 10);
914 XSETFASTINT (Vprint_length, 10);
915 cmd_error_internal (data, macroerror);
916 Vprint_level = old_level;
917 Vprint_length = old_length;
919 Vquit_flag = Qnil;
921 Vinhibit_quit = Qnil;
922 #ifdef MULTI_KBOARD
923 any_kboard_state ();
924 #endif
926 return make_number (0);
929 cmd_error_internal (data, context)
930 Lisp_Object data;
931 char *context;
933 Lisp_Object stream;
935 Vquit_flag = Qnil;
936 Vinhibit_quit = Qt;
937 echo_area_glyphs = 0;
939 /* If the window system or terminal frame hasn't been initialized
940 yet, or we're not interactive, it's best to dump this message out
941 to stderr and exit. */
942 if (! FRAME_MESSAGE_BUF (selected_frame)
943 || noninteractive)
944 stream = Qexternal_debugging_output;
945 else
947 Fdiscard_input ();
948 bitch_at_user ();
949 stream = Qt;
952 if (context != 0)
953 write_string_1 (context, -1, stream);
955 print_error_message (data, stream);
957 /* If the window system or terminal frame hasn't been initialized
958 yet, or we're in -batch mode, this error should cause Emacs to exit. */
959 if (! FRAME_MESSAGE_BUF (selected_frame)
960 || noninteractive)
962 Fterpri (stream);
963 Fkill_emacs (make_number (-1));
967 Lisp_Object command_loop_1 ();
968 Lisp_Object command_loop_2 ();
969 Lisp_Object top_level_1 ();
971 /* Entry to editor-command-loop.
972 This level has the catches for exiting/returning to editor command loop.
973 It returns nil to exit recursive edit, t to abort it. */
975 Lisp_Object
976 command_loop ()
978 if (command_loop_level > 0 || minibuf_level > 0)
980 return internal_catch (Qexit, command_loop_2, Qnil);
982 else
983 while (1)
985 internal_catch (Qtop_level, top_level_1, Qnil);
986 internal_catch (Qtop_level, command_loop_2, Qnil);
988 /* End of file in -batch run causes exit here. */
989 if (noninteractive)
990 Fkill_emacs (Qt);
994 /* Here we catch errors in execution of commands within the
995 editing loop, and reenter the editing loop.
996 When there is an error, cmd_error runs and returns a non-nil
997 value to us. A value of nil means that cmd_loop_1 itself
998 returned due to end of file (or end of kbd macro). */
1000 Lisp_Object
1001 command_loop_2 ()
1003 register Lisp_Object val;
1006 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
1007 while (!NILP (val));
1009 return Qnil;
1012 Lisp_Object
1013 top_level_2 ()
1015 return Feval (Vtop_level);
1018 Lisp_Object
1019 top_level_1 ()
1021 /* On entry to the outer level, run the startup file */
1022 if (!NILP (Vtop_level))
1023 internal_condition_case (top_level_2, Qerror, cmd_error);
1024 else if (!NILP (Vpurify_flag))
1025 message ("Bare impure Emacs (standard Lisp code not loaded)");
1026 else
1027 message ("Bare Emacs (standard Lisp code not loaded)");
1028 return Qnil;
1031 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1032 "Exit all recursive editing levels.")
1035 Fthrow (Qtop_level, Qnil);
1038 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1039 "Exit from the innermost recursive edit or minibuffer.")
1042 if (command_loop_level > 0 || minibuf_level > 0)
1043 Fthrow (Qexit, Qnil);
1045 error ("No recursive edit is in progress");
1048 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1049 "Abort the command that requested this recursive edit or minibuffer input.")
1052 if (command_loop_level > 0 || minibuf_level > 0)
1053 Fthrow (Qexit, Qt);
1055 error ("No recursive edit is in progress");
1058 /* This is the actual command reading loop,
1059 sans error-handling encapsulation. */
1061 Lisp_Object Fcommand_execute ();
1062 static int read_key_sequence ();
1063 void safe_run_hooks ();
1065 Lisp_Object
1066 command_loop_1 ()
1068 Lisp_Object cmd, tem;
1069 int lose;
1070 int nonundocount;
1071 Lisp_Object keybuf[30];
1072 int i;
1073 int no_redisplay;
1074 int no_direct;
1075 int prev_modiff;
1076 struct buffer *prev_buffer;
1077 #ifdef MULTI_KBOARD
1078 int was_locked = single_kboard;
1079 #endif
1081 current_kboard->Vprefix_arg = Qnil;
1082 Vdeactivate_mark = Qnil;
1083 waiting_for_input = 0;
1084 cancel_echoing ();
1086 nonundocount = 0;
1087 no_redisplay = 0;
1088 this_command_key_count = 0;
1089 this_single_command_key_start = 0;
1091 /* Make sure this hook runs after commands that get errors and
1092 throw to top level. */
1093 /* Note that the value cell will never directly contain nil
1094 if the symbol is a local variable. */
1095 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1096 safe_run_hooks (Qpost_command_hook);
1098 if (!NILP (Vdeferred_action_list))
1099 call0 (Vdeferred_action_function);
1101 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1103 if (NILP (Vunread_command_events)
1104 && NILP (Vexecuting_macro)
1105 && !NILP (sit_for (0, post_command_idle_delay, 0, 1)))
1106 safe_run_hooks (Qpost_command_idle_hook);
1109 /* Do this after running Vpost_command_hook, for consistency. */
1110 current_kboard->Vlast_command = this_command;
1112 while (1)
1114 /* Make sure the current window's buffer is selected. */
1115 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1116 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1118 /* Display any malloc warning that just came out. Use while because
1119 displaying one warning can cause another. */
1121 while (pending_malloc_warning)
1122 display_malloc_warning ();
1124 no_direct = 0;
1126 Vdeactivate_mark = Qnil;
1128 /* If minibuffer on and echo area in use,
1129 wait 2 sec and redraw minibuffer. */
1131 if (minibuf_level && echo_area_glyphs
1132 && EQ (minibuf_window, echo_area_window))
1134 /* Bind inhibit-quit to t so that C-g gets read in
1135 rather than quitting back to the minibuffer. */
1136 int count = specpdl_ptr - specpdl;
1137 specbind (Qinhibit_quit, Qt);
1139 Fsit_for (make_number (2), Qnil, Qnil);
1140 /* Clear the echo area. */
1141 message2 (0);
1143 unbind_to (count, Qnil);
1145 /* If a C-g came in before, treat it as input now. */
1146 if (!NILP (Vquit_flag))
1148 Vquit_flag = Qnil;
1149 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1153 #ifdef C_ALLOCA
1154 alloca (0); /* Cause a garbage collection now */
1155 /* Since we can free the most stuff here. */
1156 #endif /* C_ALLOCA */
1158 #if 0
1159 /* Select the frame that the last event came from. Usually,
1160 switch-frame events will take care of this, but if some lisp
1161 code swallows a switch-frame event, we'll fix things up here.
1162 Is this a good idea? */
1163 if (FRAMEP (internal_last_event_frame)
1164 && XFRAME (internal_last_event_frame) != selected_frame)
1165 Fselect_frame (internal_last_event_frame, Qnil);
1166 #endif
1167 /* If it has changed current-menubar from previous value,
1168 really recompute the menubar from the value. */
1169 if (! NILP (Vlucid_menu_bar_dirty_flag)
1170 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1171 call0 (Qrecompute_lucid_menubar);
1173 before_command_key_count = this_command_key_count;
1174 before_command_echo_length = echo_length ();
1176 this_command = Qnil;
1178 /* Read next key sequence; i gets its length. */
1179 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1180 Qnil, 0, 1);
1182 /* A filter may have run while we were reading the input. */
1183 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1184 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1186 ++num_input_keys;
1188 /* Now we have read a key sequence of length I,
1189 or else I is 0 and we found end of file. */
1191 if (i == 0) /* End of file -- happens only in */
1192 return Qnil; /* a kbd macro, at the end. */
1193 /* -1 means read_key_sequence got a menu that was rejected.
1194 Just loop around and read another command. */
1195 if (i == -1)
1197 cancel_echoing ();
1198 this_command_key_count = 0;
1199 this_single_command_key_start = 0;
1200 goto finalize;
1203 last_command_char = keybuf[i - 1];
1205 /* If the previous command tried to force a specific window-start,
1206 forget about that, in case this command moves point far away
1207 from that position. But also throw away beg_unchanged and
1208 end_unchanged information in that case, so that redisplay will
1209 update the whole window properly. */
1210 if (!NILP (XWINDOW (selected_window)->force_start))
1212 XWINDOW (selected_window)->force_start = Qnil;
1213 beg_unchanged = end_unchanged = 0;
1216 cmd = read_key_sequence_cmd;
1217 if (!NILP (Vexecuting_macro))
1219 if (!NILP (Vquit_flag))
1221 Vexecuting_macro = Qt;
1222 QUIT; /* Make some noise. */
1223 /* Will return since macro now empty. */
1227 /* Do redisplay processing after this command except in special
1228 cases identified below that set no_redisplay to 1.
1229 (actually, there's currently no way to prevent the redisplay,
1230 and no_redisplay is ignored.
1231 Perhaps someday we will really implement it.) */
1232 no_redisplay = 0;
1234 prev_buffer = current_buffer;
1235 prev_modiff = MODIFF;
1236 last_point_position = PT;
1237 XSETBUFFER (last_point_position_buffer, prev_buffer);
1239 /* Execute the command. */
1241 this_command = cmd;
1242 /* Note that the value cell will never directly contain nil
1243 if the symbol is a local variable. */
1244 if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
1245 safe_run_hooks (Qpre_command_hook);
1247 if (NILP (this_command))
1249 /* nil means key is undefined. */
1250 bitch_at_user ();
1251 current_kboard->defining_kbd_macro = Qnil;
1252 update_mode_lines = 1;
1253 current_kboard->Vprefix_arg = Qnil;
1255 else
1257 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1259 /* Recognize some common commands in common situations and
1260 do them directly. */
1261 if (EQ (this_command, Qforward_char) && PT < ZV)
1263 struct Lisp_Char_Table *dp
1264 = window_display_table (XWINDOW (selected_window));
1265 lose = FETCH_CHAR (PT);
1266 SET_PT (PT + 1);
1267 if ((dp
1268 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1269 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1270 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1271 && (lose >= 0x20 && lose < 0x7f)))
1272 : (lose >= 0x20 && lose < 0x7f))
1273 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1274 >= MODIFF)
1275 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1276 >= OVERLAY_MODIFF)
1277 && (XFASTINT (XWINDOW (selected_window)->last_point)
1278 == PT - 1)
1279 && !windows_or_buffers_changed
1280 && EQ (current_buffer->selective_display, Qnil)
1281 && !detect_input_pending ()
1282 && NILP (XWINDOW (selected_window)->column_number_displayed)
1283 && NILP (Vexecuting_macro))
1284 no_redisplay = direct_output_forward_char (1);
1285 goto directly_done;
1287 else if (EQ (this_command, Qbackward_char) && PT > BEGV)
1289 struct Lisp_Char_Table *dp
1290 = window_display_table (XWINDOW (selected_window));
1291 SET_PT (PT - 1);
1292 lose = FETCH_CHAR (PT);
1293 if ((dp
1294 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1295 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1296 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1297 && (lose >= 0x20 && lose < 0x7f)))
1298 : (lose >= 0x20 && lose < 0x7f))
1299 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1300 >= MODIFF)
1301 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1302 >= OVERLAY_MODIFF)
1303 && (XFASTINT (XWINDOW (selected_window)->last_point)
1304 == PT + 1)
1305 && !windows_or_buffers_changed
1306 && EQ (current_buffer->selective_display, Qnil)
1307 && !detect_input_pending ()
1308 && NILP (XWINDOW (selected_window)->column_number_displayed)
1309 && NILP (Vexecuting_macro))
1310 no_redisplay = direct_output_forward_char (-1);
1311 goto directly_done;
1313 else if (EQ (this_command, Qself_insert_command)
1314 /* Try this optimization only on ascii keystrokes. */
1315 && INTEGERP (last_command_char))
1317 unsigned char c = XINT (last_command_char);
1318 int value;
1320 if (NILP (Vexecuting_macro)
1321 && !EQ (minibuf_window, selected_window))
1323 if (!nonundocount || nonundocount >= 20)
1325 Fundo_boundary ();
1326 nonundocount = 0;
1328 nonundocount++;
1330 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1331 < MODIFF)
1332 || (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1333 < OVERLAY_MODIFF)
1334 || (XFASTINT (XWINDOW (selected_window)->last_point)
1335 != PT)
1336 || MODIFF <= SAVE_MODIFF
1337 || windows_or_buffers_changed
1338 || !EQ (current_buffer->selective_display, Qnil)
1339 || detect_input_pending ()
1340 || !NILP (XWINDOW (selected_window)->column_number_displayed)
1341 || !NILP (Vexecuting_macro));
1342 value = internal_self_insert (c, 0);
1343 if (value)
1344 lose = 1;
1345 if (value == 2)
1346 nonundocount = 0;
1348 if (!lose
1349 && (PT == ZV || FETCH_CHAR (PT) == '\n'))
1351 struct Lisp_Char_Table *dp
1352 = window_display_table (XWINDOW (selected_window));
1353 int lose = c;
1355 if (dp)
1357 Lisp_Object obj;
1359 obj = DISP_CHAR_VECTOR (dp, lose);
1360 if (NILP (obj))
1362 /* Do it only for char codes
1363 that by default display as themselves. */
1364 if (lose >= 0x20 && lose <= 0x7e)
1365 no_redisplay = direct_output_for_insert (lose);
1367 else if (VECTORP (obj)
1368 && XVECTOR (obj)->size == 1
1369 && (obj = XVECTOR (obj)->contents[0],
1370 INTEGERP (obj))
1371 /* Insist face not specified in glyph. */
1372 && (XINT (obj) & ((-1) << 8)) == 0)
1373 no_redisplay
1374 = direct_output_for_insert (XINT (obj));
1376 else
1378 if (lose >= 0x20 && lose <= 0x7e)
1379 no_redisplay = direct_output_for_insert (lose);
1382 goto directly_done;
1386 /* Here for a command that isn't executed directly */
1388 nonundocount = 0;
1389 if (NILP (current_kboard->Vprefix_arg))
1390 Fundo_boundary ();
1391 Fcommand_execute (this_command, Qnil, Qnil, Qnil);
1394 directly_done: ;
1396 /* If there is a prefix argument,
1397 1) We don't want Vlast_command to be ``universal-argument''
1398 (that would be dumb), so don't set Vlast_command,
1399 2) we want to leave echoing on so that the prefix will be
1400 echoed as part of this key sequence, so don't call
1401 cancel_echoing, and
1402 3) we want to leave this_command_key_count non-zero, so that
1403 read_char will realize that it is re-reading a character, and
1404 not echo it a second time.
1406 If the command didn't actually create a prefix arg,
1407 but is merely a frame event that is transparent to prefix args,
1408 then the above doesn't apply. */
1409 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1411 current_kboard->Vlast_command = this_command;
1412 cancel_echoing ();
1413 this_command_key_count = 0;
1414 this_single_command_key_start = 0;
1417 /* Note that the value cell will never directly contain nil
1418 if the symbol is a local variable. */
1419 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1420 safe_run_hooks (Qpost_command_hook);
1422 if (!NILP (Vdeferred_action_list))
1423 safe_run_hooks (Qdeferred_action_function);
1425 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1427 if (NILP (Vunread_command_events)
1428 && NILP (Vexecuting_macro)
1429 && !NILP (sit_for (0, post_command_idle_delay, 0, 1)))
1430 safe_run_hooks (Qpost_command_idle_hook);
1433 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1435 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1437 current_buffer->mark_active = Qnil;
1438 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1440 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1441 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1444 finalize:
1445 /* Install chars successfully executed in kbd macro. */
1447 if (!NILP (current_kboard->defining_kbd_macro)
1448 && NILP (current_kboard->Vprefix_arg))
1449 finalize_kbd_macro_chars ();
1451 #ifdef MULTI_KBOARD
1452 if (!was_locked)
1453 any_kboard_state ();
1454 #endif
1458 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1460 static Lisp_Object
1461 safe_run_hooks_1 (hook)
1462 Lisp_Object hook;
1464 return call1 (Vrun_hooks, Vinhibit_quit);
1467 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1469 static Lisp_Object
1470 safe_run_hooks_error (data)
1471 Lisp_Object data;
1473 Fset (Vinhibit_quit, Qnil);
1476 /* If we get an error while running the hook, cause the hook variable
1477 to be nil. Also inhibit quits, so that C-g won't cause the hook
1478 to mysteriously evaporate. */
1480 void
1481 safe_run_hooks (hook)
1482 Lisp_Object hook;
1484 Lisp_Object value;
1485 int count = specpdl_ptr - specpdl;
1486 specbind (Qinhibit_quit, hook);
1488 internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
1490 unbind_to (count, Qnil);
1493 /* Number of seconds between polling for input. */
1494 int polling_period;
1496 /* Nonzero means polling for input is temporarily suppressed. */
1497 int poll_suppress_count;
1499 /* Nonzero if polling_for_input is actually being used. */
1500 int polling_for_input;
1502 #ifdef POLL_FOR_INPUT
1504 /* Handle an alarm once each second and read pending input
1505 so as to handle a C-g if it comces in. */
1507 SIGTYPE
1508 input_poll_signal (signalnum) /* If we don't have an argument, */
1509 int signalnum; /* some compilers complain in signal calls. */
1511 /* This causes the call to start_polling at the end
1512 to do its job. It also arranges for a quit or error
1513 from within read_avail_input to resume polling. */
1514 poll_suppress_count++;
1515 if (interrupt_input_blocked == 0
1516 && !waiting_for_input)
1517 read_avail_input (0);
1518 /* Turn on the SIGALRM handler and request another alarm. */
1519 start_polling ();
1522 #endif
1524 /* Begin signals to poll for input, if they are appropriate.
1525 This function is called unconditionally from various places. */
1527 start_polling ()
1529 #ifdef POLL_FOR_INPUT
1530 if (read_socket_hook && !interrupt_input)
1532 poll_suppress_count--;
1533 if (poll_suppress_count == 0)
1535 signal (SIGALRM, input_poll_signal);
1536 polling_for_input = 1;
1537 alarm (polling_period);
1540 #endif
1543 /* Nonzero if we are using polling to handle input asynchronously. */
1546 input_polling_used ()
1548 #ifdef POLL_FOR_INPUT
1549 return read_socket_hook && !interrupt_input;
1550 #else
1551 return 0;
1552 #endif
1555 /* Turn off polling. */
1557 stop_polling ()
1559 #ifdef POLL_FOR_INPUT
1560 if (read_socket_hook && !interrupt_input)
1562 if (poll_suppress_count == 0)
1564 polling_for_input = 0;
1565 alarm (0);
1567 poll_suppress_count++;
1569 #endif
1572 /* Set the value of poll_suppress_count to COUNT
1573 and start or stop polling accordingly. */
1575 void
1576 set_poll_suppress_count (count)
1577 int count;
1579 #ifdef POLL_FOR_INPUT
1580 if (count == 0 && poll_suppress_count != 0)
1582 poll_suppress_count = 1;
1583 start_polling ();
1585 else if (count != 0 && poll_suppress_count == 0)
1587 stop_polling ();
1589 poll_suppress_count = count;
1590 #endif
1593 /* Bind polling_period to a value at least N.
1594 But don't decrease it. */
1596 bind_polling_period (n)
1597 int n;
1599 #ifdef POLL_FOR_INPUT
1600 int new = polling_period;
1602 if (n > new)
1603 new = n;
1605 stop_polling ();
1606 specbind (Qpolling_period, make_number (new));
1607 /* Start a new alarm with the new period. */
1608 start_polling ();
1609 #endif
1612 /* Apply the control modifier to CHARACTER. */
1615 make_ctrl_char (c)
1616 int c;
1618 /* Save the upper bits here. */
1619 int upper = c & ~0177;
1621 c &= 0177;
1623 /* Everything in the columns containing the upper-case letters
1624 denotes a control character. */
1625 if (c >= 0100 && c < 0140)
1627 int oc = c;
1628 c &= ~0140;
1629 /* Set the shift modifier for a control char
1630 made from a shifted letter. But only for letters! */
1631 if (oc >= 'A' && oc <= 'Z')
1632 c |= shift_modifier;
1635 /* The lower-case letters denote control characters too. */
1636 else if (c >= 'a' && c <= 'z')
1637 c &= ~0140;
1639 /* Include the bits for control and shift
1640 only if the basic ASCII code can't indicate them. */
1641 else if (c >= ' ')
1642 c |= ctrl_modifier;
1644 /* Replace the high bits. */
1645 c |= (upper & ~ctrl_modifier);
1647 return c;
1652 /* Input of single characters from keyboard */
1654 Lisp_Object print_help ();
1655 static Lisp_Object kbd_buffer_get_event ();
1656 static void record_char ();
1658 #ifdef MULTI_KBOARD
1659 static jmp_buf wrong_kboard_jmpbuf;
1660 #endif
1662 /* read a character from the keyboard; call the redisplay if needed */
1663 /* commandflag 0 means do not do auto-saving, but do do redisplay.
1664 -1 means do not do redisplay, but do do autosaving.
1665 1 means do both. */
1667 /* The arguments MAPS and NMAPS are for menu prompting.
1668 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1670 PREV_EVENT is the previous input event, or nil if we are reading
1671 the first event of a key sequence.
1673 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
1674 if we used a mouse menu to read the input, or zero otherwise. If
1675 USED_MOUSE_MENU is null, we don't dereference it.
1677 Value is t if we showed a menu and the user rejected it. */
1679 Lisp_Object
1680 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
1681 int commandflag;
1682 int nmaps;
1683 Lisp_Object *maps;
1684 Lisp_Object prev_event;
1685 int *used_mouse_menu;
1687 register Lisp_Object c;
1688 int count;
1689 jmp_buf local_getcjmp;
1690 jmp_buf save_jump;
1691 int key_already_recorded = 0;
1692 Lisp_Object tem, save;
1693 Lisp_Object also_record;
1694 also_record = Qnil;
1696 before_command_key_count = this_command_key_count;
1697 before_command_echo_length = echo_length ();
1699 retry:
1701 if (CONSP (Vunread_command_events))
1703 c = XCONS (Vunread_command_events)->car;
1704 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
1706 /* Undo what read_char_x_menu_prompt did when it unread
1707 additional keys returned by Fx_popup_menu. */
1708 if (CONSP (c)
1709 && (SYMBOLP (XCONS (c)->car) || INTEGERP (XCONS (c)->car))
1710 && NILP (XCONS (c)->cdr))
1711 c = XCONS (c)->car;
1713 if (this_command_key_count == 0)
1714 goto reread_first;
1715 else
1716 goto reread;
1719 if (unread_command_char != -1)
1721 XSETINT (c, unread_command_char);
1722 unread_command_char = -1;
1724 if (this_command_key_count == 0)
1725 goto reread_first;
1726 else
1727 goto reread;
1730 /* If there is no function key translated before
1731 reset-this-command-lengths takes effect, forget about it. */
1732 before_command_restore_flag = 0;
1734 if (!NILP (Vexecuting_macro))
1736 /* We set this to Qmacro; since that's not a frame, nobody will
1737 try to switch frames on us, and the selected window will
1738 remain unchanged.
1740 Since this event came from a macro, it would be misleading to
1741 leave internal_last_event_frame set to wherever the last
1742 real event came from. Normally, a switch-frame event selects
1743 internal_last_event_frame after each command is read, but
1744 events read from a macro should never cause a new frame to be
1745 selected. */
1746 Vlast_event_frame = internal_last_event_frame = Qmacro;
1748 /* Exit the macro if we are at the end.
1749 Also, some things replace the macro with t
1750 to force an early exit. */
1751 if (EQ (Vexecuting_macro, Qt)
1752 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
1754 XSETINT (c, -1);
1755 return c;
1758 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
1759 if (STRINGP (Vexecuting_macro)
1760 && (XINT (c) & 0x80))
1761 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
1763 executing_macro_index++;
1765 goto from_macro;
1768 if (!NILP (unread_switch_frame))
1770 c = unread_switch_frame;
1771 unread_switch_frame = Qnil;
1773 /* This event should make it into this_command_keys, and get echoed
1774 again, so we go to reread_first, rather than reread. */
1775 goto reread_first;
1778 if (commandflag >= 0 && !input_pending
1779 && !detect_input_pending_run_timers (0))
1780 redisplay ();
1782 /* Message turns off echoing unless more keystrokes turn it on again. */
1783 if (echo_area_glyphs && *echo_area_glyphs
1784 && echo_area_glyphs != current_kboard->echobuf
1785 && ok_to_echo_at_next_pause != echo_area_glyphs)
1786 cancel_echoing ();
1787 else
1788 /* If already echoing, continue. */
1789 echo_dash ();
1791 /* Try reading a character via menu prompting in the minibuf.
1792 Try this before the sit-for, because the sit-for
1793 would do the wrong thing if we are supposed to do
1794 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
1795 after a mouse event so don't try a minibuf menu. */
1796 c = Qnil;
1797 if (nmaps > 0 && INTERACTIVE
1798 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
1799 /* Don't bring up a menu if we already have another event. */
1800 && NILP (Vunread_command_events)
1801 && unread_command_char < 0
1802 && !detect_input_pending_run_timers (0))
1804 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
1805 if (! NILP (c))
1807 key_already_recorded = 1;
1808 goto non_reread_1;
1812 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
1813 We will do that below, temporarily for short sections of code,
1814 when appropriate. local_getcjmp must be in effect
1815 around any call to sit_for or kbd_buffer_get_event;
1816 it *must not* be in effect when we call redisplay. */
1818 if (_setjmp (local_getcjmp))
1820 XSETINT (c, quit_char);
1821 XSETFRAME (internal_last_event_frame, selected_frame);
1822 Vlast_event_frame = internal_last_event_frame;
1823 /* If we report the quit char as an event,
1824 don't do so more than once. */
1825 if (!NILP (Vinhibit_quit))
1826 Vquit_flag = Qnil;
1828 #ifdef MULTI_KBOARD
1830 KBOARD *kb = FRAME_KBOARD (selected_frame);
1831 if (kb != current_kboard)
1833 Lisp_Object *tailp = &kb->kbd_queue;
1834 /* We shouldn't get here if we were in single-kboard mode! */
1835 if (single_kboard)
1836 abort ();
1837 while (CONSP (*tailp))
1838 tailp = &XCONS (*tailp)->cdr;
1839 if (!NILP (*tailp))
1840 abort ();
1841 *tailp = Fcons (c, Qnil);
1842 kb->kbd_queue_has_data = 1;
1843 current_kboard = kb;
1844 longjmp (wrong_kboard_jmpbuf, 1);
1847 #endif
1848 goto non_reread;
1851 timer_start_idle ();
1853 /* If in middle of key sequence and minibuffer not active,
1854 start echoing if enough time elapses. */
1856 if (minibuf_level == 0 && !current_kboard->immediate_echo
1857 && this_command_key_count > 0
1858 && ! noninteractive
1859 && echo_keystrokes > 0
1860 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0
1861 || ok_to_echo_at_next_pause == echo_area_glyphs))
1863 Lisp_Object tem0;
1865 /* After a mouse event, start echoing right away.
1866 This is because we are probably about to display a menu,
1867 and we don't want to delay before doing so. */
1868 if (EVENT_HAS_PARAMETERS (prev_event))
1869 echo_now ();
1870 else
1872 save_getcjmp (save_jump);
1873 restore_getcjmp (local_getcjmp);
1874 tem0 = sit_for (echo_keystrokes, 0, 1, 1);
1875 restore_getcjmp (save_jump);
1876 if (EQ (tem0, Qt))
1877 echo_now ();
1881 /* Maybe auto save due to number of keystrokes. */
1883 if (commandflag != 0
1884 && auto_save_interval > 0
1885 && num_nonmacro_input_chars - last_auto_save > max (auto_save_interval, 20)
1886 && !detect_input_pending_run_timers (0))
1888 Fdo_auto_save (Qnil, Qnil);
1889 /* Hooks can actually change some buffers in auto save. */
1890 redisplay ();
1893 /* Try reading using an X menu.
1894 This is never confused with reading using the minibuf
1895 because the recursive call of read_char in read_char_minibuf_menu_prompt
1896 does not pass on any keymaps. */
1898 if (nmaps > 0 && INTERACTIVE
1899 && !NILP (prev_event)
1900 && EVENT_HAS_PARAMETERS (prev_event)
1901 && !EQ (XCONS (prev_event)->car, Qmenu_bar)
1902 /* Don't bring up a menu if we already have another event. */
1903 && NILP (Vunread_command_events)
1904 && unread_command_char < 0)
1905 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
1907 /* Maybe autosave and/or garbage collect due to idleness. */
1909 if (INTERACTIVE && NILP (c))
1911 int delay_level, buffer_size;
1913 /* Slow down auto saves logarithmically in size of current buffer,
1914 and garbage collect while we're at it. */
1915 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
1916 last_non_minibuf_size = Z - BEG;
1917 buffer_size = (last_non_minibuf_size >> 8) + 1;
1918 delay_level = 0;
1919 while (buffer_size > 64)
1920 delay_level++, buffer_size -= buffer_size >> 2;
1921 if (delay_level < 4) delay_level = 4;
1922 /* delay_level is 4 for files under around 50k, 7 at 100k,
1923 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
1925 /* Auto save if enough time goes by without input. */
1926 if (commandflag != 0
1927 && num_nonmacro_input_chars > last_auto_save
1928 && INTEGERP (Vauto_save_timeout)
1929 && XINT (Vauto_save_timeout) > 0)
1931 Lisp_Object tem0;
1933 save_getcjmp (save_jump);
1934 restore_getcjmp (local_getcjmp);
1935 tem0 = sit_for (delay_level * XFASTINT (Vauto_save_timeout) / 4,
1936 0, 1, 1);
1937 restore_getcjmp (save_jump);
1939 if (EQ (tem0, Qt))
1941 Fdo_auto_save (Qnil, Qnil);
1943 /* If we have auto-saved and there is still no input
1944 available, garbage collect if there has been enough
1945 consing going on to make it worthwhile. */
1946 if (!detect_input_pending_run_timers (0)
1947 && consing_since_gc > gc_cons_threshold / 2)
1948 Fgarbage_collect ();
1950 redisplay ();
1955 /* Read something from current KBOARD's side queue, if possible. */
1957 if (NILP (c))
1959 if (current_kboard->kbd_queue_has_data)
1961 if (!CONSP (current_kboard->kbd_queue))
1962 abort ();
1963 c = XCONS (current_kboard->kbd_queue)->car;
1964 current_kboard->kbd_queue
1965 = XCONS (current_kboard->kbd_queue)->cdr;
1966 if (NILP (current_kboard->kbd_queue))
1967 current_kboard->kbd_queue_has_data = 0;
1968 input_pending = readable_events (0);
1969 if (EVENT_HAS_PARAMETERS (c)
1970 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
1971 internal_last_event_frame = XCONS (XCONS (c)->cdr)->car;
1972 Vlast_event_frame = internal_last_event_frame;
1976 #ifdef MULTI_KBOARD
1977 /* If current_kboard's side queue is empty check the other kboards.
1978 If one of them has data that we have not yet seen here,
1979 switch to it and process the data waiting for it.
1981 Note: if the events queued up for another kboard
1982 have already been seen here, and therefore are not a complete command,
1983 the kbd_queue_has_data field is 0, so we skip that kboard here.
1984 That's to avoid an infinite loop switching between kboards here. */
1985 if (NILP (c) && !single_kboard)
1987 KBOARD *kb;
1988 for (kb = all_kboards; kb; kb = kb->next_kboard)
1989 if (kb->kbd_queue_has_data)
1991 current_kboard = kb;
1992 longjmp (wrong_kboard_jmpbuf, 1);
1995 #endif
1997 wrong_kboard:
1999 stop_polling ();
2001 /* Finally, we read from the main queue,
2002 and if that gives us something we can't use yet, we put it on the
2003 appropriate side queue and try again. */
2005 if (NILP (c))
2007 KBOARD *kb;
2009 /* Actually read a character, waiting if necessary. */
2010 save_getcjmp (save_jump);
2011 restore_getcjmp (local_getcjmp);
2012 c = kbd_buffer_get_event (&kb, used_mouse_menu);
2013 restore_getcjmp (save_jump);
2015 #ifdef MULTI_KBOARD
2016 if (! NILP (c) && (kb != current_kboard))
2018 Lisp_Object *tailp = &kb->kbd_queue;
2019 while (CONSP (*tailp))
2020 tailp = &XCONS (*tailp)->cdr;
2021 if (!NILP (*tailp))
2022 abort ();
2023 *tailp = Fcons (c, Qnil);
2024 kb->kbd_queue_has_data = 1;
2025 c = Qnil;
2026 if (single_kboard)
2027 goto wrong_kboard;
2028 current_kboard = kb;
2029 longjmp (wrong_kboard_jmpbuf, 1);
2031 #endif
2034 /* Terminate Emacs in batch mode if at eof. */
2035 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
2036 Fkill_emacs (make_number (1));
2038 if (INTEGERP (c))
2040 /* Add in any extra modifiers, where appropriate. */
2041 if ((extra_keyboard_modifiers & CHAR_CTL)
2042 || ((extra_keyboard_modifiers & 0177) < ' '
2043 && (extra_keyboard_modifiers & 0177) != 0))
2044 XSETINT (c, make_ctrl_char (XINT (c)));
2046 /* Transfer any other modifier bits directly from
2047 extra_keyboard_modifiers to c. Ignore the actual character code
2048 in the low 16 bits of extra_keyboard_modifiers. */
2049 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
2052 non_reread:
2054 /* Now that we have read an event, Emacs is not idle--
2055 unless the event was a timer event (not used now). */
2056 if (! (CONSP (c) && EQ (XCONS (c)->car, Qtimer_event)))
2057 timer_stop_idle ();
2059 start_polling ();
2061 if (NILP (c))
2063 if (commandflag >= 0
2064 && !input_pending && !detect_input_pending_run_timers (0))
2065 redisplay ();
2067 goto wrong_kboard;
2070 non_reread_1:
2072 /* Buffer switch events are only for internal wakeups
2073 so don't show them to the user. */
2074 if (BUFFERP (c))
2075 return c;
2077 if (key_already_recorded)
2078 return c;
2080 /* Process special events within read_char
2081 and loop around to read another event. */
2082 save = Vquit_flag;
2083 Vquit_flag = Qnil;
2084 tem = get_keyelt (access_keymap (get_keymap_1 (Vspecial_event_map, 0, 0),
2085 c, 0, 0), 1);
2086 Vquit_flag = save;
2088 if (!NILP (tem))
2090 int was_locked = single_kboard;
2092 last_input_char = c;
2093 Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt);
2095 /* Resume allowing input from any kboard, if that was true before. */
2096 if (!was_locked)
2097 any_kboard_state ();
2099 goto retry;
2102 /* Wipe the echo area. */
2103 echo_area_glyphs = 0;
2105 /* Handle things that only apply to characters. */
2106 if (INTEGERP (c))
2108 /* If kbd_buffer_get_event gave us an EOF, return that. */
2109 if (XINT (c) == -1)
2110 return c;
2112 if (STRINGP (Vkeyboard_translate_table)
2113 && XSTRING (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2114 XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[XFASTINT (c)]);
2115 else if ((VECTORP (Vkeyboard_translate_table)
2116 && XVECTOR (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2117 || (CHAR_TABLE_P (Vkeyboard_translate_table)
2118 && CHAR_TABLE_ORDINARY_SLOTS > (unsigned) XFASTINT (c)))
2120 Lisp_Object d;
2121 d = Faref (Vkeyboard_translate_table, c);
2122 /* nil in keyboard-translate-table means no translation. */
2123 if (!NILP (d))
2124 c = d;
2128 /* If this event is a mouse click in the menu bar,
2129 return just menu-bar for now. Modify the mouse click event
2130 so we won't do this twice, then queue it up. */
2131 if (EVENT_HAS_PARAMETERS (c)
2132 && CONSP (XCONS (c)->cdr)
2133 && CONSP (EVENT_START (c))
2134 && CONSP (XCONS (EVENT_START (c))->cdr))
2136 Lisp_Object posn;
2138 posn = POSN_BUFFER_POSN (EVENT_START (c));
2139 /* Handle menu-bar events:
2140 insert the dummy prefix event `menu-bar'. */
2141 if (EQ (posn, Qmenu_bar))
2143 /* Change menu-bar to (menu-bar) as the event "position". */
2144 POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
2146 also_record = c;
2147 Vunread_command_events = Fcons (c, Vunread_command_events);
2148 c = posn;
2152 record_char (c);
2153 if (! NILP (also_record))
2154 record_char (also_record);
2156 from_macro:
2157 reread_first:
2158 before_command_key_count = this_command_key_count;
2159 before_command_echo_length = echo_length ();
2161 /* Don't echo mouse motion events. */
2162 if (echo_keystrokes
2163 && ! (EVENT_HAS_PARAMETERS (c)
2164 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
2166 echo_char (c);
2167 if (! NILP (also_record))
2168 echo_char (also_record);
2169 /* Once we reread a character, echoing can happen
2170 the next time we pause to read a new one. */
2171 ok_to_echo_at_next_pause = echo_area_glyphs;
2174 /* Record this character as part of the current key. */
2175 add_command_key (c);
2176 if (! NILP (also_record))
2177 add_command_key (also_record);
2179 /* Re-reading in the middle of a command */
2180 reread:
2181 last_input_char = c;
2182 num_input_chars++;
2184 /* Process the help character specially if enabled */
2185 if (!NILP (Vhelp_form) && help_char_p (c))
2187 Lisp_Object tem0;
2188 count = specpdl_ptr - specpdl;
2190 record_unwind_protect (Fset_window_configuration,
2191 Fcurrent_window_configuration (Qnil));
2193 tem0 = Feval (Vhelp_form);
2194 if (STRINGP (tem0))
2195 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
2197 cancel_echoing ();
2199 c = read_char (0, 0, 0, Qnil, 0);
2200 while (BUFFERP (c));
2201 /* Remove the help from the frame */
2202 unbind_to (count, Qnil);
2204 redisplay ();
2205 if (EQ (c, make_number (040)))
2207 cancel_echoing ();
2209 c = read_char (0, 0, 0, Qnil, 0);
2210 while (BUFFERP (c));
2214 return c;
2217 /* Return 1 if should recognize C as "the help character". */
2220 help_char_p (c)
2221 Lisp_Object c;
2223 Lisp_Object tail;
2225 if (EQ (c, Vhelp_char))
2226 return 1;
2227 for (tail = Vhelp_event_list; CONSP (tail); tail = XCONS (tail)->cdr)
2228 if (EQ (c, XCONS (tail)->car))
2229 return 1;
2230 return 0;
2233 /* Record the input event C in various ways. */
2235 static void
2236 record_char (c)
2237 Lisp_Object c;
2239 total_keys++;
2240 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
2241 if (++recent_keys_index >= NUM_RECENT_KEYS)
2242 recent_keys_index = 0;
2244 /* Write c to the dribble file. If c is a lispy event, write
2245 the event's symbol to the dribble file, in <brackets>. Bleaugh.
2246 If you, dear reader, have a better idea, you've got the source. :-) */
2247 if (dribble)
2249 if (INTEGERP (c))
2251 if (XUINT (c) < 0x100)
2252 putc (XINT (c), dribble);
2253 else
2254 fprintf (dribble, " 0x%x", (int) XUINT (c));
2256 else
2258 Lisp_Object dribblee;
2260 /* If it's a structured event, take the event header. */
2261 dribblee = EVENT_HEAD (c);
2263 if (SYMBOLP (dribblee))
2265 putc ('<', dribble);
2266 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
2267 XSYMBOL (dribblee)->name->size,
2268 dribble);
2269 putc ('>', dribble);
2273 fflush (dribble);
2276 store_kbd_macro_char (c);
2278 num_nonmacro_input_chars++;
2281 Lisp_Object
2282 print_help (object)
2283 Lisp_Object object;
2285 struct buffer *old = current_buffer;
2286 Fprinc (object, Qnil);
2287 set_buffer_internal (XBUFFER (Vstandard_output));
2288 call0 (intern ("help-mode"));
2289 set_buffer_internal (old);
2290 return Qnil;
2293 /* Copy out or in the info on where C-g should throw to.
2294 This is used when running Lisp code from within get_char,
2295 in case get_char is called recursively.
2296 See read_process_output. */
2298 save_getcjmp (temp)
2299 jmp_buf temp;
2301 bcopy (getcjmp, temp, sizeof getcjmp);
2304 restore_getcjmp (temp)
2305 jmp_buf temp;
2307 bcopy (temp, getcjmp, sizeof getcjmp);
2310 #ifdef HAVE_MOUSE
2312 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
2313 of this function. */
2315 static Lisp_Object
2316 tracking_off (old_value)
2317 Lisp_Object old_value;
2319 do_mouse_tracking = old_value;
2320 if (NILP (old_value))
2322 /* Redisplay may have been preempted because there was input
2323 available, and it assumes it will be called again after the
2324 input has been processed. If the only input available was
2325 the sort that we have just disabled, then we need to call
2326 redisplay. */
2327 if (!readable_events (1))
2329 redisplay_preserve_echo_area ();
2330 get_input_pending (&input_pending, 1);
2335 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
2336 "Evaluate BODY with mouse movement events enabled.\n\
2337 Within a `track-mouse' form, mouse motion generates input events that\n\
2338 you can read with `read-event'.\n\
2339 Normally, mouse motion is ignored.")
2340 (args)
2341 Lisp_Object args;
2343 int count = specpdl_ptr - specpdl;
2344 Lisp_Object val;
2346 record_unwind_protect (tracking_off, do_mouse_tracking);
2348 do_mouse_tracking = Qt;
2350 val = Fprogn (args);
2351 return unbind_to (count, val);
2354 /* If mouse has moved on some frame, return one of those frames.
2355 Return 0 otherwise. */
2357 static FRAME_PTR
2358 some_mouse_moved ()
2360 Lisp_Object tail, frame;
2362 FOR_EACH_FRAME (tail, frame)
2364 if (XFRAME (frame)->mouse_moved)
2365 return XFRAME (frame);
2368 return 0;
2371 #endif /* HAVE_MOUSE */
2373 /* Low level keyboard/mouse input.
2374 kbd_buffer_store_event places events in kbd_buffer, and
2375 kbd_buffer_get_event retrieves them. */
2377 /* Return true iff there are any events in the queue that read-char
2378 would return. If this returns false, a read-char would block. */
2379 static int
2380 readable_events (do_timers_now)
2381 int do_timers_now;
2383 if (do_timers_now)
2384 timer_check (do_timers_now);
2386 if (kbd_fetch_ptr != kbd_store_ptr)
2387 return 1;
2388 #ifdef HAVE_MOUSE
2389 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2390 return 1;
2391 #endif
2392 if (single_kboard)
2394 if (current_kboard->kbd_queue_has_data)
2395 return 1;
2397 else
2399 KBOARD *kb;
2400 for (kb = all_kboards; kb; kb = kb->next_kboard)
2401 if (kb->kbd_queue_has_data)
2402 return 1;
2404 return 0;
2407 /* Set this for debugging, to have a way to get out */
2408 int stop_character;
2410 #ifdef MULTI_KBOARD
2411 static KBOARD *
2412 event_to_kboard (event)
2413 struct input_event *event;
2415 Lisp_Object frame;
2416 frame = event->frame_or_window;
2417 if (CONSP (frame))
2418 frame = XCONS (frame)->car;
2419 else if (WINDOWP (frame))
2420 frame = WINDOW_FRAME (XWINDOW (frame));
2422 /* There are still some events that don't set this field.
2423 For now, just ignore the problem.
2424 Also ignore dead frames here. */
2425 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
2426 return 0;
2427 else
2428 return FRAME_KBOARD (XFRAME (frame));
2430 #endif
2432 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
2434 void
2435 kbd_buffer_store_event (event)
2436 register struct input_event *event;
2438 if (event->kind == no_event)
2439 abort ();
2441 if (event->kind == ascii_keystroke)
2443 register int c = event->code & 0377;
2445 if (event->modifiers & ctrl_modifier)
2446 c = make_ctrl_char (c);
2448 c |= (event->modifiers
2449 & (meta_modifier | alt_modifier
2450 | hyper_modifier | super_modifier));
2452 if (c == quit_char)
2454 extern SIGTYPE interrupt_signal ();
2455 #ifdef MULTI_KBOARD
2456 KBOARD *kb;
2457 struct input_event *sp;
2459 if (single_kboard
2460 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
2461 kb != current_kboard))
2463 kb->kbd_queue
2464 = Fcons (make_lispy_switch_frame (event->frame_or_window),
2465 Fcons (make_number (c), Qnil));
2466 kb->kbd_queue_has_data = 1;
2467 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
2469 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
2470 sp = kbd_buffer;
2472 if (event_to_kboard (sp) == kb)
2474 sp->kind = no_event;
2475 sp->frame_or_window = Qnil;
2478 return;
2480 #endif
2482 /* If this results in a quit_char being returned to Emacs as
2483 input, set Vlast_event_frame properly. If this doesn't
2484 get returned to Emacs as an event, the next event read
2485 will set Vlast_event_frame again, so this is safe to do. */
2487 Lisp_Object focus;
2489 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
2490 if (NILP (focus))
2491 focus = event->frame_or_window;
2492 internal_last_event_frame = focus;
2493 Vlast_event_frame = focus;
2496 last_event_timestamp = event->timestamp;
2497 interrupt_signal ();
2498 return;
2501 if (c && c == stop_character)
2503 sys_suspend ();
2504 return;
2507 /* Don't insert two buffer_switch_event's in a row.
2508 Just ignore the second one. */
2509 else if (event->kind == buffer_switch_event
2510 && kbd_fetch_ptr != kbd_store_ptr
2511 && kbd_store_ptr->kind == buffer_switch_event)
2512 return;
2514 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
2515 kbd_store_ptr = kbd_buffer;
2517 /* Don't let the very last slot in the buffer become full,
2518 since that would make the two pointers equal,
2519 and that is indistinguishable from an empty buffer.
2520 Discard the event if it would fill the last slot. */
2521 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
2523 volatile struct input_event *sp = kbd_store_ptr;
2524 sp->kind = event->kind;
2525 if (event->kind == selection_request_event)
2527 /* We must not use the ordinary copying code for this case,
2528 since `part' is an enum and copying it might not copy enough
2529 in this case. */
2530 bcopy (event, (char *) sp, sizeof (*event));
2532 else
2534 sp->code = event->code;
2535 sp->part = event->part;
2536 sp->frame_or_window = event->frame_or_window;
2537 sp->modifiers = event->modifiers;
2538 sp->x = event->x;
2539 sp->y = event->y;
2540 sp->timestamp = event->timestamp;
2542 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
2543 - kbd_buffer]
2544 = event->frame_or_window);
2546 kbd_store_ptr++;
2550 /* Read one event from the event buffer, waiting if necessary.
2551 The value is a Lisp object representing the event.
2552 The value is nil for an event that should be ignored,
2553 or that was handled here.
2554 We always read and discard one event. */
2556 static Lisp_Object
2557 kbd_buffer_get_event (kbp, used_mouse_menu)
2558 KBOARD **kbp;
2559 int *used_mouse_menu;
2561 register int c;
2562 Lisp_Object obj;
2563 EMACS_TIME next_timer_delay;
2565 if (noninteractive)
2567 c = getchar ();
2568 XSETINT (obj, c);
2569 *kbp = current_kboard;
2570 return obj;
2573 /* Wait until there is input available. */
2574 for (;;)
2576 if (kbd_fetch_ptr != kbd_store_ptr)
2577 break;
2578 #ifdef HAVE_MOUSE
2579 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2580 break;
2581 #endif
2583 /* If the quit flag is set, then read_char will return
2584 quit_char, so that counts as "available input." */
2585 if (!NILP (Vquit_flag))
2586 quit_throw_to_read_char ();
2588 /* One way or another, wait until input is available; then, if
2589 interrupt handlers have not read it, read it now. */
2591 #ifdef OLDVMS
2592 wait_for_kbd_input ();
2593 #else
2594 /* Note SIGIO has been undef'd if FIONREAD is missing. */
2595 #ifdef SIGIO
2596 gobble_input (0);
2597 #endif /* SIGIO */
2598 if (kbd_fetch_ptr != kbd_store_ptr)
2599 break;
2600 #ifdef HAVE_MOUSE
2601 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2602 break;
2603 #endif
2605 Lisp_Object minus_one;
2607 XSETINT (minus_one, -1);
2608 wait_reading_process_input (0, 0, minus_one, 1);
2610 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
2611 /* Pass 1 for EXPECT since we just waited to have input. */
2612 read_avail_input (1);
2614 #endif /* not VMS */
2617 /* At this point, we know that there is a readable event available
2618 somewhere. If the event queue is empty, then there must be a
2619 mouse movement enabled and available. */
2620 if (kbd_fetch_ptr != kbd_store_ptr)
2622 struct input_event *event;
2624 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2625 ? kbd_fetch_ptr
2626 : kbd_buffer);
2628 last_event_timestamp = event->timestamp;
2630 #ifdef MULTI_KBOARD
2631 *kbp = event_to_kboard (event);
2632 if (*kbp == 0)
2633 *kbp = current_kboard; /* Better than returning null ptr? */
2634 #else
2635 *kbp = &the_only_kboard;
2636 #endif
2638 obj = Qnil;
2640 /* These two kinds of events get special handling
2641 and don't actually appear to the command loop.
2642 We return nil for them. */
2643 if (event->kind == selection_request_event)
2645 #ifdef HAVE_X11
2646 struct input_event copy;
2648 /* Remove it from the buffer before processing it,
2649 since otherwise swallow_events will see it
2650 and process it again. */
2651 copy = *event;
2652 kbd_fetch_ptr = event + 1;
2653 input_pending = readable_events (0);
2654 x_handle_selection_request (&copy);
2655 #else
2656 /* We're getting selection request events, but we don't have
2657 a window system. */
2658 abort ();
2659 #endif
2662 else if (event->kind == selection_clear_event)
2664 #ifdef HAVE_X11
2665 struct input_event copy;
2667 /* Remove it from the buffer before processing it. */
2668 copy = *event;
2669 kbd_fetch_ptr = event + 1;
2670 input_pending = readable_events (0);
2671 x_handle_selection_clear (&copy);
2672 #else
2673 /* We're getting selection request events, but we don't have
2674 a window system. */
2675 abort ();
2676 #endif
2678 #if defined (HAVE_X11) || defined (HAVE_NTGUI)
2679 else if (event->kind == delete_window_event)
2681 /* Make an event (delete-frame (FRAME)). */
2682 obj = Fcons (event->frame_or_window, Qnil);
2683 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
2684 kbd_fetch_ptr = event + 1;
2686 else if (event->kind == iconify_event)
2688 /* Make an event (iconify-frame (FRAME)). */
2689 obj = Fcons (event->frame_or_window, Qnil);
2690 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
2691 kbd_fetch_ptr = event + 1;
2693 else if (event->kind == deiconify_event)
2695 /* Make an event (make-frame-visible (FRAME)). */
2696 obj = Fcons (event->frame_or_window, Qnil);
2697 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
2698 kbd_fetch_ptr = event + 1;
2700 #endif
2701 else if (event->kind == buffer_switch_event)
2703 /* The value doesn't matter here; only the type is tested. */
2704 XSETBUFFER (obj, current_buffer);
2705 kbd_fetch_ptr = event + 1;
2707 #ifdef USE_X_TOOLKIT
2708 else if (event->kind == menu_bar_activate_event)
2710 kbd_fetch_ptr = event + 1;
2711 input_pending = readable_events (0);
2712 if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
2713 x_activate_menubar (XFRAME (event->frame_or_window));
2715 #endif
2716 /* Just discard these, by returning nil.
2717 With MULTI_KBOARD, these events are used as placeholders
2718 when we need to randomly delete events from the queue.
2719 (They shouldn't otherwise be found in the buffer,
2720 but on some machines it appears they do show up
2721 even without MULTI_KBOARD.) */
2722 else if (event->kind == no_event)
2723 kbd_fetch_ptr = event + 1;
2725 /* If this event is on a different frame, return a switch-frame this
2726 time, and leave the event in the queue for next time. */
2727 else
2729 Lisp_Object frame;
2730 Lisp_Object focus;
2732 frame = event->frame_or_window;
2733 if (CONSP (frame))
2734 frame = XCONS (frame)->car;
2735 else if (WINDOWP (frame))
2736 frame = WINDOW_FRAME (XWINDOW (frame));
2738 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
2739 if (! NILP (focus))
2740 frame = focus;
2742 if (! EQ (frame, internal_last_event_frame)
2743 && XFRAME (frame) != selected_frame)
2744 obj = make_lispy_switch_frame (frame);
2745 internal_last_event_frame = frame;
2747 /* If we didn't decide to make a switch-frame event, go ahead
2748 and build a real event from the queue entry. */
2750 if (NILP (obj))
2752 obj = make_lispy_event (event);
2753 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
2754 /* If this was a menu selection, then set the flag to inhibit
2755 writing to last_nonmenu_event. Don't do this if the event
2756 we're returning is (menu-bar), though; that indicates the
2757 beginning of the menu sequence, and we might as well leave
2758 that as the `event with parameters' for this selection. */
2759 if (event->kind == menu_bar_event
2760 && !(CONSP (obj) && EQ (XCONS (obj)->car, Qmenu_bar))
2761 && used_mouse_menu)
2762 *used_mouse_menu = 1;
2763 #endif
2765 /* Wipe out this event, to catch bugs. */
2766 event->kind = no_event;
2767 XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer] = Qnil;
2769 kbd_fetch_ptr = event + 1;
2773 #ifdef HAVE_MOUSE
2774 /* Try generating a mouse motion event. */
2775 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2777 FRAME_PTR f = some_mouse_moved ();
2778 Lisp_Object bar_window;
2779 enum scroll_bar_part part;
2780 Lisp_Object x, y;
2781 unsigned long time;
2783 *kbp = current_kboard;
2784 /* Note that this uses F to determine which display to look at.
2785 If there is no valid info, it does not store anything
2786 so x remains nil. */
2787 x = Qnil;
2788 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
2790 obj = Qnil;
2792 /* Decide if we should generate a switch-frame event. Don't
2793 generate switch-frame events for motion outside of all Emacs
2794 frames. */
2795 if (!NILP (x) && f)
2797 Lisp_Object frame;
2799 frame = FRAME_FOCUS_FRAME (f);
2800 if (NILP (frame))
2801 XSETFRAME (frame, f);
2803 if (! EQ (frame, internal_last_event_frame)
2804 && XFRAME (frame) != selected_frame)
2805 obj = make_lispy_switch_frame (frame);
2806 internal_last_event_frame = frame;
2809 /* If we didn't decide to make a switch-frame event, go ahead and
2810 return a mouse-motion event. */
2811 if (!NILP (x) && NILP (obj))
2812 obj = make_lispy_movement (f, bar_window, part, x, y, time);
2814 #endif /* HAVE_MOUSE */
2815 else
2816 /* We were promised by the above while loop that there was
2817 something for us to read! */
2818 abort ();
2820 input_pending = readable_events (0);
2822 Vlast_event_frame = internal_last_event_frame;
2824 return (obj);
2827 /* Process any events that are not user-visible,
2828 then return, without reading any user-visible events. */
2830 void
2831 swallow_events (do_display)
2832 int do_display;
2834 int old_timers_run;
2836 while (kbd_fetch_ptr != kbd_store_ptr)
2838 struct input_event *event;
2840 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2841 ? kbd_fetch_ptr
2842 : kbd_buffer);
2844 last_event_timestamp = event->timestamp;
2846 /* These two kinds of events get special handling
2847 and don't actually appear to the command loop. */
2848 if (event->kind == selection_request_event)
2850 #ifdef HAVE_X11
2851 struct input_event copy;
2853 /* Remove it from the buffer before processing it,
2854 since otherwise swallow_events called recursively could see it
2855 and process it again. */
2856 copy = *event;
2857 kbd_fetch_ptr = event + 1;
2858 input_pending = readable_events (0);
2859 x_handle_selection_request (&copy);
2860 #else
2861 /* We're getting selection request events, but we don't have
2862 a window system. */
2863 abort ();
2864 #endif
2867 else if (event->kind == selection_clear_event)
2869 #ifdef HAVE_X11
2870 struct input_event copy;
2872 /* Remove it from the buffer before processing it, */
2873 copy = *event;
2875 kbd_fetch_ptr = event + 1;
2876 input_pending = readable_events (0);
2877 x_handle_selection_clear (&copy);
2878 #else
2879 /* We're getting selection request events, but we don't have
2880 a window system. */
2881 abort ();
2882 #endif
2884 /* Note that timer_event is currently never used. */
2885 else if (event->kind == timer_event)
2887 Lisp_Object tem, lisp_event;
2888 int was_locked = single_kboard;
2890 tem = get_keymap_1 (Vspecial_event_map, 0, 0);
2891 tem = get_keyelt (access_keymap (tem, Qtimer_event, 0, 0),
2893 lisp_event = Fcons (Qtimer_event,
2894 Fcons (Fcdr (event->frame_or_window), Qnil));
2895 kbd_fetch_ptr = event + 1;
2896 if (kbd_fetch_ptr == kbd_store_ptr)
2897 input_pending = 0;
2898 Fcommand_execute (tem, Qnil, Fvector (1, &lisp_event), Qt);
2899 timers_run++;
2900 if (do_display)
2901 redisplay_preserve_echo_area ();
2903 /* Resume allowing input from any kboard, if that was true before. */
2904 if (!was_locked)
2905 any_kboard_state ();
2907 else
2908 break;
2911 old_timers_run = timers_run;
2912 get_input_pending (&input_pending, 1);
2914 if (timers_run != old_timers_run && do_display)
2915 redisplay_preserve_echo_area ();
2918 static EMACS_TIME timer_idleness_start_time;
2920 /* Record the start of when Emacs is idle,
2921 for the sake of running idle-time timers. */
2923 timer_start_idle ()
2925 Lisp_Object timers;
2927 /* If we are already in the idle state, do nothing. */
2928 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
2929 return;
2931 EMACS_GET_TIME (timer_idleness_start_time);
2933 /* Mark all idle-time timers as once again candidates for running. */
2934 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCONS (timers)->cdr)
2936 Lisp_Object timer;
2938 timer = XCONS (timers)->car;
2940 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
2941 continue;
2942 XVECTOR (timer)->contents[0] = Qnil;
2946 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
2948 timer_stop_idle ()
2950 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
2953 /* This is only for debugging. */
2954 struct input_event last_timer_event;
2956 /* Check whether a timer has fired. To prevent larger problems we simply
2957 disregard elements that are not proper timers. Do not make a circular
2958 timer list for the time being.
2960 Returns the number of seconds to wait until the next timer fires. If a
2961 timer is triggering now, return zero seconds.
2962 If no timer is active, return -1 seconds.
2964 If a timer is ripe, we run it, with quitting turned off.
2966 DO_IT_NOW is now ignored. It used to mean that we should
2967 run the timer directly instead of queueing a timer-event.
2968 Now we always run timers directly. */
2970 EMACS_TIME
2971 timer_check (do_it_now)
2972 int do_it_now;
2974 EMACS_TIME nexttime;
2975 EMACS_TIME now, idleness_now;
2976 Lisp_Object timers, idle_timers, chosen_timer;
2977 /* Nonzero if we generate some events. */
2978 int events_generated = 0;
2979 struct gcpro gcpro1, gcpro2, gcpro3;
2981 EMACS_SET_SECS (nexttime, -1);
2982 EMACS_SET_USECS (nexttime, -1);
2984 /* Always consider the ordinary timers. */
2985 timers = Vtimer_list;
2986 /* Consider the idle timers only if Emacs is idle. */
2987 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
2988 idle_timers = Vtimer_idle_list;
2989 else
2990 idle_timers = Qnil;
2991 chosen_timer = Qnil;
2992 GCPRO3 (timers, idle_timers, chosen_timer);
2994 if (CONSP (timers) || CONSP (idle_timers))
2996 EMACS_GET_TIME (now);
2997 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
2998 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
3001 while (CONSP (timers) || CONSP (idle_timers))
3003 int triggertime = EMACS_SECS (now);
3004 Lisp_Object *vector;
3005 Lisp_Object timer, idle_timer;
3006 EMACS_TIME timer_time, idle_timer_time;
3007 EMACS_TIME difference, timer_difference, idle_timer_difference;
3009 /* Skip past invalid timers and timers already handled. */
3010 if (!NILP (timers))
3012 timer = XCONS (timers)->car;
3013 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3015 timers = XCONS (timers)->cdr;
3016 continue;
3018 vector = XVECTOR (timer)->contents;
3020 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3021 || !INTEGERP (vector[3])
3022 || ! NILP (vector[0]))
3024 timers = XCONS (timers)->cdr;
3025 continue;
3028 if (!NILP (idle_timers))
3030 timer = XCONS (idle_timers)->car;
3031 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3033 idle_timers = XCONS (idle_timers)->cdr;
3034 continue;
3036 vector = XVECTOR (timer)->contents;
3038 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3039 || !INTEGERP (vector[3])
3040 || ! NILP (vector[0]))
3042 idle_timers = XCONS (idle_timers)->cdr;
3043 continue;
3047 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
3048 based on the next ordinary timer.
3049 TIMER_DIFFERENCE is the distance in time from NOW to when
3050 this timer becomes ripe (negative if it's already ripe). */
3051 if (!NILP (timers))
3053 timer = XCONS (timers)->car;
3054 vector = XVECTOR (timer)->contents;
3055 EMACS_SET_SECS (timer_time,
3056 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3057 EMACS_SET_USECS (timer_time, XINT (vector[3]));
3058 EMACS_SUB_TIME (timer_difference, timer_time, now);
3061 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
3062 based on the next idle timer. */
3063 if (!NILP (idle_timers))
3065 idle_timer = XCONS (idle_timers)->car;
3066 vector = XVECTOR (idle_timer)->contents;
3067 EMACS_SET_SECS (idle_timer_time,
3068 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3069 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
3070 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
3073 /* Decide which timer is the next timer,
3074 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
3075 Also step down the list where we found that timer. */
3077 if (! NILP (timers) && ! NILP (idle_timers))
3079 EMACS_TIME temp;
3080 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
3081 if (EMACS_TIME_NEG_P (temp))
3083 chosen_timer = timer;
3084 timers = XCONS (timers)->cdr;
3085 difference = timer_difference;
3087 else
3089 chosen_timer = idle_timer;
3090 idle_timers = XCONS (idle_timers)->cdr;
3091 difference = idle_timer_difference;
3094 else if (! NILP (timers))
3096 chosen_timer = timer;
3097 timers = XCONS (timers)->cdr;
3098 difference = timer_difference;
3100 else
3102 chosen_timer = idle_timer;
3103 idle_timers = XCONS (idle_timers)->cdr;
3104 difference = idle_timer_difference;
3106 vector = XVECTOR (chosen_timer)->contents;
3108 /* If timer is rupe, run it if it hasn't been run. */
3109 if (EMACS_TIME_NEG_P (difference)
3110 || (EMACS_SECS (difference) == 0
3111 && EMACS_USECS (difference) == 0))
3113 if (NILP (vector[0]))
3115 /* Mark the timer as triggered to prevent problems if the lisp
3116 code fails to reschedule it right. */
3117 vector[0] = Qt;
3119 /* Run the timer or queue a timer event. */
3120 if (1)
3122 Lisp_Object tem, event;
3123 int was_locked = single_kboard;
3124 int count = specpdl_ptr - specpdl;
3126 specbind (Qinhibit_quit, Qt);
3128 tem = get_keymap_1 (Vspecial_event_map, 0, 0);
3129 tem = get_keyelt (access_keymap (tem, Qtimer_event, 0, 0),
3131 event = Fcons (Qtimer_event, Fcons (chosen_timer, Qnil));
3132 Fcommand_execute (tem, Qnil, Fvector (1, &event), Qt);
3133 timers_run++;
3135 unbind_to (count, Qnil);
3137 /* Resume allowing input from any kboard, if that was true before. */
3138 if (!was_locked)
3139 any_kboard_state ();
3141 /* Since we have handled the event,
3142 we don't need to tell the caller to wake up and do it. */
3144 #if 0
3145 else
3147 /* Generate a timer event so the caller will handle it. */
3148 struct input_event event;
3150 event.kind = timer_event;
3151 event.modifiers = 0;
3152 event.x = event.y = Qnil;
3153 event.timestamp = triggertime;
3154 /* Store the timer in the frame slot. */
3155 event.frame_or_window
3156 = Fcons (Fselected_frame (), chosen_timer);
3157 kbd_buffer_store_event (&event);
3159 last_timer_event = event;
3161 /* Tell caller to handle this event right away. */
3162 events_generated = 1;
3163 EMACS_SET_SECS (nexttime, 0);
3164 EMACS_SET_USECS (nexttime, 0);
3166 /* Don't queue more than one event at once.
3167 When Emacs is ready for another, it will
3168 queue the next one. */
3169 UNGCPRO;
3170 return nexttime;
3172 #endif /* 0 */
3175 else
3176 /* When we encounter a timer that is still waiting,
3177 return the amount of time to wait before it is ripe. */
3179 UNGCPRO;
3180 /* But if we generated an event,
3181 tell the caller to handle it now. */
3182 if (events_generated)
3183 return nexttime;
3184 return difference;
3188 /* No timers are pending in the future. */
3189 /* Return 0 if we generated an event, and -1 if not. */
3190 UNGCPRO;
3191 return nexttime;
3194 /* Caches for modify_event_symbol. */
3195 static Lisp_Object accent_key_syms;
3196 static Lisp_Object func_key_syms;
3197 static Lisp_Object mouse_syms;
3199 /* This is a list of keysym codes for special "accent" characters.
3200 It parallels lispy_accent_keys. */
3202 static int lispy_accent_codes[] =
3204 #ifdef XK_dead_circumflex
3205 XK_dead_circumflex,
3206 #else
3208 #endif
3209 #ifdef XK_dead_grave
3210 XK_dead_grave,
3211 #else
3213 #endif
3214 #ifdef XK_dead_tilde
3215 XK_dead_tilde,
3216 #else
3218 #endif
3219 #ifdef XK_dead_diaeresis
3220 XK_dead_diaeresis,
3221 #else
3223 #endif
3224 #ifdef XK_dead_macron
3225 XK_dead_macron,
3226 #else
3228 #endif
3229 #ifdef XK_dead_degree
3230 XK_dead_degree,
3231 #else
3233 #endif
3234 #ifdef XK_dead_acute
3235 XK_dead_acute,
3236 #else
3238 #endif
3239 #ifdef XK_dead_cedilla
3240 XK_dead_cedilla,
3241 #else
3243 #endif
3244 #ifdef XK_dead_breve
3245 XK_dead_breve,
3246 #else
3248 #endif
3249 #ifdef XK_dead_ogonek
3250 XK_dead_ogonek,
3251 #else
3253 #endif
3254 #ifdef XK_dead_caron
3255 XK_dead_caron,
3256 #else
3258 #endif
3259 #ifdef XK_dead_doubleacute
3260 XK_dead_doubleacute,
3261 #else
3263 #endif
3264 #ifdef XK_dead_abovedot
3265 XK_dead_abovedot,
3266 #else
3268 #endif
3271 /* This is a list of Lisp names for special "accent" characters.
3272 It parallels lispy_accent_codes. */
3274 static char *lispy_accent_keys[] =
3276 "dead-circumflex",
3277 "dead-grave",
3278 "dead-tilde",
3279 "dead-diaeresis",
3280 "dead-macron",
3281 "dead-degree",
3282 "dead-acute",
3283 "dead-cedilla",
3284 "dead-breve",
3285 "dead-ogonek",
3286 "dead-caron",
3287 "dead-doubleacute",
3288 "dead-abovedot",
3291 #ifdef HAVE_NTGUI
3292 #define FUNCTION_KEY_OFFSET 0x0
3294 char *lispy_function_keys[] =
3296 0, /* 0 */
3298 0, /* VK_LBUTTON 0x01 */
3299 0, /* VK_RBUTTON 0x02 */
3300 "cancel", /* VK_CANCEL 0x03 */
3301 0, /* VK_MBUTTON 0x04 */
3303 0, 0, 0, /* 0x05 .. 0x07 */
3305 "backspace", /* VK_BACK 0x08 */
3306 "tab", /* VK_TAB 0x09 */
3308 0, 0, /* 0x0A .. 0x0B */
3310 "clear", /* VK_CLEAR 0x0C */
3311 "return", /* VK_RETURN 0x0D */
3313 0, 0, /* 0x0E .. 0x0F */
3315 "shift", /* VK_SHIFT 0x10 */
3316 "control", /* VK_CONTROL 0x11 */
3317 "menu", /* VK_MENU 0x12 */
3318 "pause", /* VK_PAUSE 0x13 */
3319 "capital", /* VK_CAPITAL 0x14 */
3321 0, 0, 0, 0, 0, 0, /* 0x15 .. 0x1A */
3323 0, /* VK_ESCAPE 0x1B */
3325 0, 0, 0, 0, /* 0x1C .. 0x1F */
3327 0, /* VK_SPACE 0x20 */
3328 "prior", /* VK_PRIOR 0x21 */
3329 "next", /* VK_NEXT 0x22 */
3330 "end", /* VK_END 0x23 */
3331 "home", /* VK_HOME 0x24 */
3332 "left", /* VK_LEFT 0x25 */
3333 "up", /* VK_UP 0x26 */
3334 "right", /* VK_RIGHT 0x27 */
3335 "down", /* VK_DOWN 0x28 */
3336 "select", /* VK_SELECT 0x29 */
3337 "print", /* VK_PRINT 0x2A */
3338 "execute", /* VK_EXECUTE 0x2B */
3339 "snapshot", /* VK_SNAPSHOT 0x2C */
3340 "insert", /* VK_INSERT 0x2D */
3341 "delete", /* VK_DELETE 0x2E */
3342 "help", /* VK_HELP 0x2F */
3344 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
3346 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3348 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
3350 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
3352 0, 0, 0, 0, 0, 0, 0, 0, 0,
3353 0, 0, 0, 0, 0, 0, 0, 0, 0,
3354 0, 0, 0, 0, 0, 0, 0, 0,
3356 "lwindow", /* VK_LWIN 0x5B */
3357 "rwindow", /* VK_RWIN 0x5C */
3358 "apps", /* VK_APPS 0x5D */
3360 0, 0, /* 0x5E .. 0x5F */
3362 "kp-0", /* VK_NUMPAD0 0x60 */
3363 "kp-1", /* VK_NUMPAD1 0x61 */
3364 "kp-2", /* VK_NUMPAD2 0x62 */
3365 "kp-3", /* VK_NUMPAD3 0x63 */
3366 "kp-4", /* VK_NUMPAD4 0x64 */
3367 "kp-5", /* VK_NUMPAD5 0x65 */
3368 "kp-6", /* VK_NUMPAD6 0x66 */
3369 "kp-7", /* VK_NUMPAD7 0x67 */
3370 "kp-8", /* VK_NUMPAD8 0x68 */
3371 "kp-9", /* VK_NUMPAD9 0x69 */
3372 "kp-multiply", /* VK_MULTIPLY 0x6A */
3373 "kp-add", /* VK_ADD 0x6B */
3374 "kp-separator", /* VK_SEPARATOR 0x6C */
3375 "kp-subtract", /* VK_SUBTRACT 0x6D */
3376 "kp-decimal", /* VK_DECIMAL 0x6E */
3377 "kp-divide", /* VK_DIVIDE 0x6F */
3378 "f1", /* VK_F1 0x70 */
3379 "f2", /* VK_F2 0x71 */
3380 "f3", /* VK_F3 0x72 */
3381 "f4", /* VK_F4 0x73 */
3382 "f5", /* VK_F5 0x74 */
3383 "f6", /* VK_F6 0x75 */
3384 "f7", /* VK_F7 0x76 */
3385 "f8", /* VK_F8 0x77 */
3386 "f9", /* VK_F9 0x78 */
3387 "f10", /* VK_F10 0x79 */
3388 "f11", /* VK_F11 0x7A */
3389 "f12", /* VK_F12 0x7B */
3390 "f13", /* VK_F13 0x7C */
3391 "f14", /* VK_F14 0x7D */
3392 "f15", /* VK_F15 0x7E */
3393 "f16", /* VK_F16 0x7F */
3394 "f17", /* VK_F17 0x80 */
3395 "f18", /* VK_F18 0x81 */
3396 "f19", /* VK_F19 0x82 */
3397 "f20", /* VK_F20 0x83 */
3398 "f21", /* VK_F21 0x84 */
3399 "f22", /* VK_F22 0x85 */
3400 "f23", /* VK_F23 0x86 */
3401 "f24", /* VK_F24 0x87 */
3403 0, 0, 0, 0, /* 0x88 .. 0x8B */
3404 0, 0, 0, 0, /* 0x8C .. 0x8F */
3406 "kp-numlock", /* VK_NUMLOCK 0x90 */
3407 "scroll", /* VK_SCROLL 0x91 */
3409 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
3410 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
3411 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
3412 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
3413 "kp-end", /* VK_NUMPAD_END 0x96 */
3414 "kp-home", /* VK_NUMPAD_HOME 0x97 */
3415 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
3416 "kp-up", /* VK_NUMPAD_UP 0x99 */
3417 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
3418 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
3419 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
3420 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
3422 0, 0, /* 0x9E .. 0x9F */
3425 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
3426 * Used only as parameters to GetAsyncKeyState() and GetKeyState().
3427 * No other API or message will distinguish left and right keys this way.
3429 /* 0xA0 .. 0xEF */
3431 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3432 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3433 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3434 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3435 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3437 /* 0xF0 .. 0xF5 */
3439 0, 0, 0, 0, 0, 0,
3441 "attn", /* VK_ATTN 0xF6 */
3442 "crsel", /* VK_CRSEL 0xF7 */
3443 "exsel", /* VK_EXSEL 0xF8 */
3444 "ereof", /* VK_EREOF 0xF9 */
3445 "play", /* VK_PLAY 0xFA */
3446 "zoom", /* VK_ZOOM 0xFB */
3447 "noname", /* VK_NONAME 0xFC */
3448 "pa1", /* VK_PA1 0xFD */
3449 "oem_clear", /* VK_OEM_CLEAR 0xFE */
3452 #else
3454 #define FUNCTION_KEY_OFFSET 0xff00
3456 /* You'll notice that this table is arranged to be conveniently
3457 indexed by X Windows keysym values. */
3458 static char *lispy_function_keys[] =
3460 /* X Keysym value */
3462 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00 */
3463 "backspace",
3464 "tab",
3465 "linefeed",
3466 "clear",
3468 "return",
3469 0, 0,
3470 0, 0, 0, /* 0xff10 */
3471 "pause",
3472 0, 0, 0, 0, 0, 0, 0,
3473 "escape",
3474 0, 0, 0, 0,
3475 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff20...2f */
3476 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff30...3f */
3477 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
3479 "home", /* 0xff50 */ /* IsCursorKey */
3480 "left",
3481 "up",
3482 "right",
3483 "down",
3484 "prior",
3485 "next",
3486 "end",
3487 "begin",
3488 0, /* 0xff59 */
3489 0, 0, 0, 0, 0, 0,
3490 "select", /* 0xff60 */ /* IsMiscFunctionKey */
3491 "print",
3492 "execute",
3493 "insert",
3494 0, /* 0xff64 */
3495 "undo",
3496 "redo",
3497 "menu",
3498 "find",
3499 "cancel",
3500 "help",
3501 "break", /* 0xff6b */
3503 0, 0, 0, 0, 0, 0, 0, 0, "backtab", 0,
3504 0, /* 0xff76 */
3505 0, 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff7f */
3506 "kp-space", /* 0xff80 */ /* IsKeypadKey */
3507 0, 0, 0, 0, 0, 0, 0, 0,
3508 "kp-tab", /* 0xff89 */
3509 0, 0, 0,
3510 "kp-enter", /* 0xff8d */
3511 0, 0, 0,
3512 "kp-f1", /* 0xff91 */
3513 "kp-f2",
3514 "kp-f3",
3515 "kp-f4",
3516 "kp-home", /* 0xff95 */
3517 "kp-left",
3518 "kp-up",
3519 "kp-right",
3520 "kp-down",
3521 "kp-prior", /* kp-page-up */
3522 "kp-next", /* kp-page-down */
3523 "kp-end",
3524 "kp-begin",
3525 "kp-insert",
3526 "kp-delete",
3527 0, /* 0xffa0 */
3528 0, 0, 0, 0, 0, 0, 0, 0, 0,
3529 "kp-multiply", /* 0xffaa */
3530 "kp-add",
3531 "kp-separator",
3532 "kp-subtract",
3533 "kp-decimal",
3534 "kp-divide", /* 0xffaf */
3535 "kp-0", /* 0xffb0 */
3536 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
3537 0, /* 0xffba */
3538 0, 0,
3539 "kp-equal", /* 0xffbd */
3540 "f1", /* 0xffbe */ /* IsFunctionKey */
3541 "f2",
3542 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
3543 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
3544 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
3545 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
3546 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
3547 0, 0, 0, 0, 0, 0, 0, 0,
3548 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
3549 0, 0, 0, 0, 0, 0, 0, "delete"
3552 #endif /* HAVE_NTGUI */
3554 static char *lispy_mouse_names[] =
3556 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
3559 /* Scroll bar parts. */
3560 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
3561 Lisp_Object Qup, Qdown;
3563 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
3564 Lisp_Object *scroll_bar_parts[] = {
3565 &Qabove_handle, &Qhandle, &Qbelow_handle,
3566 &Qup, &Qdown,
3570 /* A vector, indexed by button number, giving the down-going location
3571 of currently depressed buttons, both scroll bar and non-scroll bar.
3573 The elements have the form
3574 (BUTTON-NUMBER MODIFIER-MASK . REST)
3575 where REST is the cdr of a position as it would be reported in the event.
3577 The make_lispy_event function stores positions here to tell the
3578 difference between click and drag events, and to store the starting
3579 location to be included in drag events. */
3581 static Lisp_Object button_down_location;
3583 /* Information about the most recent up-going button event: Which
3584 button, what location, and what time. */
3586 static int last_mouse_button;
3587 static int last_mouse_x;
3588 static int last_mouse_y;
3589 static unsigned long button_down_time;
3591 /* The maximum time between clicks to make a double-click,
3592 or Qnil to disable double-click detection,
3593 or Qt for no time limit. */
3594 Lisp_Object Vdouble_click_time;
3596 /* The number of clicks in this multiple-click. */
3598 int double_click_count;
3600 /* Given a struct input_event, build the lisp event which represents
3601 it. If EVENT is 0, build a mouse movement event from the mouse
3602 movement buffer, which should have a movement event in it.
3604 Note that events must be passed to this function in the order they
3605 are received; this function stores the location of button presses
3606 in order to build drag events when the button is released. */
3608 static Lisp_Object
3609 make_lispy_event (event)
3610 struct input_event *event;
3612 int i;
3614 switch (SWITCH_ENUM_CAST (event->kind))
3616 /* A simple keystroke. */
3617 case ascii_keystroke:
3619 Lisp_Object lispy_c;
3620 int c = event->code & 0377;
3621 /* Turn ASCII characters into control characters
3622 when proper. */
3623 if (event->modifiers & ctrl_modifier)
3624 c = make_ctrl_char (c);
3626 /* Add in the other modifier bits. We took care of ctrl_modifier
3627 just above, and the shift key was taken care of by the X code,
3628 and applied to control characters by make_ctrl_char. */
3629 c |= (event->modifiers
3630 & (meta_modifier | alt_modifier
3631 | hyper_modifier | super_modifier));
3632 button_down_time = 0;
3633 XSETFASTINT (lispy_c, c);
3634 return lispy_c;
3637 /* A function key. The symbol may need to have modifier prefixes
3638 tacked onto it. */
3639 case non_ascii_keystroke:
3640 button_down_time = 0;
3642 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
3643 if (event->code == lispy_accent_codes[i])
3644 return modify_event_symbol (i,
3645 event->modifiers,
3646 Qfunction_key, Qnil,
3647 lispy_accent_keys, &accent_key_syms,
3648 (sizeof (lispy_accent_keys)
3649 / sizeof (lispy_accent_keys[0])));
3651 /* Handle system-specific keysyms. */
3652 if (event->code & (1 << 28))
3654 /* We need to use an alist rather than a vector as the cache
3655 since we can't make a vector long enuf. */
3656 if (NILP (current_kboard->system_key_syms))
3657 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
3658 return modify_event_symbol (event->code,
3659 event->modifiers,
3660 Qfunction_key,
3661 current_kboard->Vsystem_key_alist,
3662 0, &current_kboard->system_key_syms,
3663 (unsigned)-1);
3666 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
3667 event->modifiers,
3668 Qfunction_key, Qnil,
3669 lispy_function_keys, &func_key_syms,
3670 (sizeof (lispy_function_keys)
3671 / sizeof (lispy_function_keys[0])));
3672 break;
3674 /* Note that timer_event is currently never used. */
3675 case timer_event:
3676 return Fcons (Qtimer_event, Fcons (Fcdr (event->frame_or_window), Qnil));
3678 #ifdef HAVE_MOUSE
3679 /* A mouse click. Figure out where it is, decide whether it's
3680 a press, click or drag, and build the appropriate structure. */
3681 case mouse_click:
3682 case scroll_bar_click:
3684 int button = event->code;
3685 int is_double;
3686 Lisp_Object position;
3687 Lisp_Object *start_pos_ptr;
3688 Lisp_Object start_pos;
3690 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
3691 abort ();
3693 /* Build the position as appropriate for this mouse click. */
3694 if (event->kind == mouse_click)
3696 int part;
3697 FRAME_PTR f = XFRAME (event->frame_or_window);
3698 Lisp_Object window;
3699 Lisp_Object posn;
3700 int row, column;
3702 /* Ignore mouse events that were made on frame that
3703 have been deleted. */
3704 if (! FRAME_LIVE_P (f))
3705 return Qnil;
3707 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
3708 &column, &row, NULL, 1);
3710 #ifndef USE_X_TOOLKIT
3711 /* In the non-toolkit version, clicks on the menu bar
3712 are ordinary button events in the event buffer.
3713 Distinguish them, and invoke the menu.
3715 (In the toolkit version, the toolkit handles the menu bar
3716 and Emacs doesn't know about it until after the user
3717 makes a selection.) */
3718 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
3719 && (event->modifiers & down_modifier))
3721 Lisp_Object items, item;
3722 int hpos;
3723 int i;
3725 #if 0
3726 /* Activate the menu bar on the down event. If the
3727 up event comes in before the menu code can deal with it,
3728 just ignore it. */
3729 if (! (event->modifiers & down_modifier))
3730 return Qnil;
3731 #endif
3733 item = Qnil;
3734 items = FRAME_MENU_BAR_ITEMS (f);
3735 for (i = 0; i < XVECTOR (items)->size; i += 4)
3737 Lisp_Object pos, string;
3738 string = XVECTOR (items)->contents[i + 1];
3739 pos = XVECTOR (items)->contents[i + 3];
3740 if (NILP (string))
3741 break;
3742 if (column >= XINT (pos)
3743 && column < XINT (pos) + XSTRING (string)->size)
3745 item = XVECTOR (items)->contents[i];
3746 break;
3750 position
3751 = Fcons (event->frame_or_window,
3752 Fcons (Qmenu_bar,
3753 Fcons (Fcons (event->x, event->y),
3754 Fcons (make_number (event->timestamp),
3755 Qnil))));
3757 return Fcons (item, Fcons (position, Qnil));
3759 #endif /* not USE_X_TOOLKIT */
3761 window = window_from_coordinates (f, column, row, &part);
3763 if (!WINDOWP (window))
3765 window = event->frame_or_window;
3766 posn = Qnil;
3768 else
3770 int pixcolumn, pixrow;
3771 column -= WINDOW_LEFT_MARGIN (XWINDOW (window));
3772 row -= XINT (XWINDOW (window)->top);
3773 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
3774 XSETINT (event->x, pixcolumn);
3775 XSETINT (event->y, pixrow);
3777 if (part == 1)
3778 posn = Qmode_line;
3779 else if (part == 2)
3780 posn = Qvertical_line;
3781 else
3782 XSETINT (posn,
3783 buffer_posn_from_coords (XWINDOW (window),
3784 column, row));
3787 position
3788 = Fcons (window,
3789 Fcons (posn,
3790 Fcons (Fcons (event->x, event->y),
3791 Fcons (make_number (event->timestamp),
3792 Qnil))));
3794 else
3796 Lisp_Object window;
3797 Lisp_Object portion_whole;
3798 Lisp_Object part;
3800 window = event->frame_or_window;
3801 portion_whole = Fcons (event->x, event->y);
3802 part = *scroll_bar_parts[(int) event->part];
3804 position
3805 = Fcons (window,
3806 Fcons (Qvertical_scroll_bar,
3807 Fcons (portion_whole,
3808 Fcons (make_number (event->timestamp),
3809 Fcons (part, Qnil)))));
3812 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
3814 start_pos = *start_pos_ptr;
3815 *start_pos_ptr = Qnil;
3817 is_double = (button == last_mouse_button
3818 && XINT (event->x) == last_mouse_x
3819 && XINT (event->y) == last_mouse_y
3820 && button_down_time != 0
3821 && (EQ (Vdouble_click_time, Qt)
3822 || (INTEGERP (Vdouble_click_time)
3823 && ((int)(event->timestamp - button_down_time)
3824 < XINT (Vdouble_click_time)))));
3825 last_mouse_button = button;
3826 last_mouse_x = XINT (event->x);
3827 last_mouse_y = XINT (event->y);
3829 /* If this is a button press, squirrel away the location, so
3830 we can decide later whether it was a click or a drag. */
3831 if (event->modifiers & down_modifier)
3833 if (is_double)
3835 double_click_count++;
3836 event->modifiers |= ((double_click_count > 2)
3837 ? triple_modifier
3838 : double_modifier);
3840 else
3841 double_click_count = 1;
3842 button_down_time = event->timestamp;
3843 *start_pos_ptr = Fcopy_alist (position);
3846 /* Now we're releasing a button - check the co-ordinates to
3847 see if this was a click or a drag. */
3848 else if (event->modifiers & up_modifier)
3850 /* If we did not see a down before this up,
3851 ignore the up. Probably this happened because
3852 the down event chose a menu item.
3853 It would be an annoyance to treat the release
3854 of the button that chose the menu item
3855 as a separate event. */
3857 if (!CONSP (start_pos))
3858 return Qnil;
3860 event->modifiers &= ~up_modifier;
3861 #if 0 /* Formerly we treated an up with no down as a click event. */
3862 if (!CONSP (start_pos))
3863 event->modifiers |= click_modifier;
3864 else
3865 #endif
3867 /* The third element of every position should be the (x,y)
3868 pair. */
3869 Lisp_Object down;
3871 down = Fnth (make_number (2), start_pos);
3872 if (EQ (event->x, XCONS (down)->car)
3873 && EQ (event->y, XCONS (down)->cdr))
3875 event->modifiers |= click_modifier;
3877 else
3879 button_down_time = 0;
3880 event->modifiers |= drag_modifier;
3882 /* Don't check is_double; treat this as multiple
3883 if the down-event was multiple. */
3884 if (double_click_count > 1)
3885 event->modifiers |= ((double_click_count > 2)
3886 ? triple_modifier
3887 : double_modifier);
3890 else
3891 /* Every mouse event should either have the down_modifier or
3892 the up_modifier set. */
3893 abort ();
3896 /* Get the symbol we should use for the mouse click. */
3897 Lisp_Object head;
3899 head = modify_event_symbol (button,
3900 event->modifiers,
3901 Qmouse_click, Qnil,
3902 lispy_mouse_names, &mouse_syms,
3903 (sizeof (lispy_mouse_names)
3904 / sizeof (lispy_mouse_names[0])));
3905 if (event->modifiers & drag_modifier)
3906 return Fcons (head,
3907 Fcons (start_pos,
3908 Fcons (position,
3909 Qnil)));
3910 else if (event->modifiers & (double_modifier | triple_modifier))
3911 return Fcons (head,
3912 Fcons (position,
3913 Fcons (make_number (double_click_count),
3914 Qnil)));
3915 else
3916 return Fcons (head,
3917 Fcons (position,
3918 Qnil));
3922 #ifdef WINDOWSNT
3923 case w32_scroll_bar_click:
3925 int button = event->code;
3926 int is_double;
3927 Lisp_Object position;
3928 Lisp_Object *start_pos_ptr;
3929 Lisp_Object start_pos;
3931 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
3932 abort ();
3935 Lisp_Object window;
3936 Lisp_Object portion_whole;
3937 Lisp_Object part;
3939 window = event->frame_or_window;
3940 portion_whole = Fcons (event->x, event->y);
3941 part = *scroll_bar_parts[(int) event->part];
3943 position =
3944 Fcons (window,
3945 Fcons (Qvertical_scroll_bar,
3946 Fcons (portion_whole,
3947 Fcons (make_number (event->timestamp),
3948 Fcons (part, Qnil)))));
3951 /* Always treat W32 scroll bar events as clicks. */
3952 event->modifiers |= click_modifier;
3955 /* Get the symbol we should use for the mouse click. */
3956 Lisp_Object head;
3958 head = modify_event_symbol (button,
3959 event->modifiers,
3960 Qmouse_click, Qnil,
3961 lispy_mouse_names, &mouse_syms,
3962 (sizeof (lispy_mouse_names)
3963 / sizeof (lispy_mouse_names[0])));
3964 return Fcons (head,
3965 Fcons (position,
3966 Qnil));
3969 #endif
3971 #endif /* HAVE_MOUSE */
3973 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
3974 case menu_bar_event:
3975 /* The event value is in the cdr of the frame_or_window slot. */
3976 if (!CONSP (event->frame_or_window))
3977 abort ();
3978 return XCONS (event->frame_or_window)->cdr;
3979 #endif
3981 /* The 'kind' field of the event is something we don't recognize. */
3982 default:
3983 abort ();
3987 #ifdef HAVE_MOUSE
3989 static Lisp_Object
3990 make_lispy_movement (frame, bar_window, part, x, y, time)
3991 FRAME_PTR frame;
3992 Lisp_Object bar_window;
3993 enum scroll_bar_part part;
3994 Lisp_Object x, y;
3995 unsigned long time;
3997 /* Is it a scroll bar movement? */
3998 if (frame && ! NILP (bar_window))
4000 Lisp_Object part_sym;
4002 part_sym = *scroll_bar_parts[(int) part];
4003 return Fcons (Qscroll_bar_movement,
4004 (Fcons (Fcons (bar_window,
4005 Fcons (Qvertical_scroll_bar,
4006 Fcons (Fcons (x, y),
4007 Fcons (make_number (time),
4008 Fcons (part_sym,
4009 Qnil))))),
4010 Qnil)));
4013 /* Or is it an ordinary mouse movement? */
4014 else
4016 int area;
4017 Lisp_Object window;
4018 Lisp_Object posn;
4019 int column, row;
4021 if (frame)
4023 /* It's in a frame; which window on that frame? */
4024 pixel_to_glyph_coords (frame, XINT (x), XINT (y), &column, &row,
4025 NULL, 1);
4026 window = window_from_coordinates (frame, column, row, &area);
4028 else
4029 window = Qnil;
4031 if (WINDOWP (window))
4033 int pixcolumn, pixrow;
4034 column -= WINDOW_LEFT_MARGIN (XWINDOW (window));
4035 row -= XINT (XWINDOW (window)->top);
4036 glyph_to_pixel_coords (frame, column, row, &pixcolumn, &pixrow);
4037 XSETINT (x, pixcolumn);
4038 XSETINT (y, pixrow);
4040 if (area == 1)
4041 posn = Qmode_line;
4042 else if (area == 2)
4043 posn = Qvertical_line;
4044 else
4045 XSETINT (posn,
4046 buffer_posn_from_coords (XWINDOW (window), column, row));
4048 else if (frame != 0)
4050 XSETFRAME (window, frame);
4051 posn = Qnil;
4053 else
4055 window = Qnil;
4056 posn = Qnil;
4057 XSETFASTINT (x, 0);
4058 XSETFASTINT (y, 0);
4061 return Fcons (Qmouse_movement,
4062 Fcons (Fcons (window,
4063 Fcons (posn,
4064 Fcons (Fcons (x, y),
4065 Fcons (make_number (time),
4066 Qnil)))),
4067 Qnil));
4071 #endif /* HAVE_MOUSE */
4073 /* Construct a switch frame event. */
4074 static Lisp_Object
4075 make_lispy_switch_frame (frame)
4076 Lisp_Object frame;
4078 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
4081 /* Manipulating modifiers. */
4083 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
4085 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
4086 SYMBOL's name of the end of the modifiers; the string from this
4087 position is the unmodified symbol name.
4089 This doesn't use any caches. */
4091 static int
4092 parse_modifiers_uncached (symbol, modifier_end)
4093 Lisp_Object symbol;
4094 int *modifier_end;
4096 struct Lisp_String *name;
4097 int i;
4098 int modifiers;
4100 CHECK_SYMBOL (symbol, 1);
4102 modifiers = 0;
4103 name = XSYMBOL (symbol)->name;
4105 for (i = 0; i+2 <= name->size; )
4107 int this_mod_end = 0;
4108 int this_mod = 0;
4110 /* See if the name continues with a modifier word.
4111 Check that the word appears, but don't check what follows it.
4112 Set this_mod and this_mod_end to record what we find. */
4114 switch (name->data[i])
4116 #define SINGLE_LETTER_MOD(BIT) \
4117 (this_mod_end = i + 1, this_mod = BIT)
4119 case 'A':
4120 SINGLE_LETTER_MOD (alt_modifier);
4121 break;
4123 case 'C':
4124 SINGLE_LETTER_MOD (ctrl_modifier);
4125 break;
4127 case 'H':
4128 SINGLE_LETTER_MOD (hyper_modifier);
4129 break;
4131 case 'M':
4132 SINGLE_LETTER_MOD (meta_modifier);
4133 break;
4135 case 'S':
4136 SINGLE_LETTER_MOD (shift_modifier);
4137 break;
4139 case 's':
4140 SINGLE_LETTER_MOD (super_modifier);
4141 break;
4143 #undef SINGLE_LETTER_MOD
4146 /* If we found no modifier, stop looking for them. */
4147 if (this_mod_end == 0)
4148 break;
4150 /* Check there is a dash after the modifier, so that it
4151 really is a modifier. */
4152 if (this_mod_end >= name->size || name->data[this_mod_end] != '-')
4153 break;
4155 /* This modifier is real; look for another. */
4156 modifiers |= this_mod;
4157 i = this_mod_end + 1;
4160 /* Should we include the `click' modifier? */
4161 if (! (modifiers & (down_modifier | drag_modifier
4162 | double_modifier | triple_modifier))
4163 && i + 7 == name->size
4164 && strncmp (name->data + i, "mouse-", 6) == 0
4165 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
4166 modifiers |= click_modifier;
4168 if (modifier_end)
4169 *modifier_end = i;
4171 return modifiers;
4174 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
4175 prepended to the string BASE[0..BASE_LEN-1].
4176 This doesn't use any caches. */
4177 static Lisp_Object
4178 apply_modifiers_uncached (modifiers, base, base_len)
4179 int modifiers;
4180 char *base;
4181 int base_len;
4183 /* Since BASE could contain nulls, we can't use intern here; we have
4184 to use Fintern, which expects a genuine Lisp_String, and keeps a
4185 reference to it. */
4186 char *new_mods =
4187 (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
4188 int mod_len;
4191 char *p = new_mods;
4193 /* Only the event queue may use the `up' modifier; it should always
4194 be turned into a click or drag event before presented to lisp code. */
4195 if (modifiers & up_modifier)
4196 abort ();
4198 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
4199 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
4200 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
4201 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
4202 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
4203 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
4204 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
4205 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
4206 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
4207 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
4208 /* The click modifier is denoted by the absence of other modifiers. */
4210 *p = '\0';
4212 mod_len = p - new_mods;
4216 Lisp_Object new_name;
4218 new_name = make_uninit_string (mod_len + base_len);
4219 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
4220 bcopy (base, XSTRING (new_name)->data + mod_len, base_len);
4222 return Fintern (new_name, Qnil);
4227 static char *modifier_names[] =
4229 "up", "down", "drag", "click", "double", "triple", 0, 0,
4230 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4231 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
4233 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
4235 static Lisp_Object modifier_symbols;
4237 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
4238 static Lisp_Object
4239 lispy_modifier_list (modifiers)
4240 int modifiers;
4242 Lisp_Object modifier_list;
4243 int i;
4245 modifier_list = Qnil;
4246 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
4247 if (modifiers & (1<<i))
4248 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
4249 modifier_list);
4251 return modifier_list;
4255 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
4256 where UNMODIFIED is the unmodified form of SYMBOL,
4257 MASK is the set of modifiers present in SYMBOL's name.
4258 This is similar to parse_modifiers_uncached, but uses the cache in
4259 SYMBOL's Qevent_symbol_element_mask property, and maintains the
4260 Qevent_symbol_elements property. */
4262 static Lisp_Object
4263 parse_modifiers (symbol)
4264 Lisp_Object symbol;
4266 Lisp_Object elements;
4268 elements = Fget (symbol, Qevent_symbol_element_mask);
4269 if (CONSP (elements))
4270 return elements;
4271 else
4273 int end;
4274 int modifiers = parse_modifiers_uncached (symbol, &end);
4275 Lisp_Object unmodified;
4276 Lisp_Object mask;
4278 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
4279 XSYMBOL (symbol)->name->size - end),
4280 Qnil);
4282 if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1))
4283 abort ();
4284 XSETFASTINT (mask, modifiers);
4285 elements = Fcons (unmodified, Fcons (mask, Qnil));
4287 /* Cache the parsing results on SYMBOL. */
4288 Fput (symbol, Qevent_symbol_element_mask,
4289 elements);
4290 Fput (symbol, Qevent_symbol_elements,
4291 Fcons (unmodified, lispy_modifier_list (modifiers)));
4293 /* Since we know that SYMBOL is modifiers applied to unmodified,
4294 it would be nice to put that in unmodified's cache.
4295 But we can't, since we're not sure that parse_modifiers is
4296 canonical. */
4298 return elements;
4302 /* Apply the modifiers MODIFIERS to the symbol BASE.
4303 BASE must be unmodified.
4305 This is like apply_modifiers_uncached, but uses BASE's
4306 Qmodifier_cache property, if present. It also builds
4307 Qevent_symbol_elements properties, since it has that info anyway.
4309 apply_modifiers copies the value of BASE's Qevent_kind property to
4310 the modified symbol. */
4311 static Lisp_Object
4312 apply_modifiers (modifiers, base)
4313 int modifiers;
4314 Lisp_Object base;
4316 Lisp_Object cache, index, entry, new_symbol;
4318 /* Mask out upper bits. We don't know where this value's been. */
4319 modifiers &= ((EMACS_INT)1 << VALBITS) - 1;
4321 /* The click modifier never figures into cache indices. */
4322 cache = Fget (base, Qmodifier_cache);
4323 XSETFASTINT (index, (modifiers & ~click_modifier));
4324 entry = assq_no_quit (index, cache);
4326 if (CONSP (entry))
4327 new_symbol = XCONS (entry)->cdr;
4328 else
4330 /* We have to create the symbol ourselves. */
4331 new_symbol = apply_modifiers_uncached (modifiers,
4332 XSYMBOL (base)->name->data,
4333 XSYMBOL (base)->name->size);
4335 /* Add the new symbol to the base's cache. */
4336 entry = Fcons (index, new_symbol);
4337 Fput (base, Qmodifier_cache, Fcons (entry, cache));
4339 /* We have the parsing info now for free, so add it to the caches. */
4340 XSETFASTINT (index, modifiers);
4341 Fput (new_symbol, Qevent_symbol_element_mask,
4342 Fcons (base, Fcons (index, Qnil)));
4343 Fput (new_symbol, Qevent_symbol_elements,
4344 Fcons (base, lispy_modifier_list (modifiers)));
4347 /* Make sure this symbol is of the same kind as BASE.
4349 You'd think we could just set this once and for all when we
4350 intern the symbol above, but reorder_modifiers may call us when
4351 BASE's property isn't set right; we can't assume that just
4352 because it has a Qmodifier_cache property it must have its
4353 Qevent_kind set right as well. */
4354 if (NILP (Fget (new_symbol, Qevent_kind)))
4356 Lisp_Object kind;
4358 kind = Fget (base, Qevent_kind);
4359 if (! NILP (kind))
4360 Fput (new_symbol, Qevent_kind, kind);
4363 return new_symbol;
4367 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
4368 return a symbol with the modifiers placed in the canonical order.
4369 Canonical order is alphabetical, except for down and drag, which
4370 always come last. The 'click' modifier is never written out.
4372 Fdefine_key calls this to make sure that (for example) C-M-foo
4373 and M-C-foo end up being equivalent in the keymap. */
4375 Lisp_Object
4376 reorder_modifiers (symbol)
4377 Lisp_Object symbol;
4379 /* It's hopefully okay to write the code this way, since everything
4380 will soon be in caches, and no consing will be done at all. */
4381 Lisp_Object parsed;
4383 parsed = parse_modifiers (symbol);
4384 return apply_modifiers ((int) XINT (XCONS (XCONS (parsed)->cdr)->car),
4385 XCONS (parsed)->car);
4389 /* For handling events, we often want to produce a symbol whose name
4390 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
4391 to some base, like the name of a function key or mouse button.
4392 modify_event_symbol produces symbols of this sort.
4394 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
4395 is the name of the i'th symbol. TABLE_SIZE is the number of elements
4396 in the table.
4398 Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
4399 NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
4401 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
4402 persist between calls to modify_event_symbol that it can use to
4403 store a cache of the symbols it's generated for this NAME_TABLE
4404 before. The object stored there may be a vector or an alist.
4406 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
4408 MODIFIERS is a set of modifier bits (as given in struct input_events)
4409 whose prefixes should be applied to the symbol name.
4411 SYMBOL_KIND is the value to be placed in the event_kind property of
4412 the returned symbol.
4414 The symbols we create are supposed to have an
4415 `event-symbol-elements' property, which lists the modifiers present
4416 in the symbol's name. */
4418 static Lisp_Object
4419 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
4420 name_table, symbol_table, table_size)
4421 int symbol_num;
4422 unsigned modifiers;
4423 Lisp_Object symbol_kind;
4424 Lisp_Object name_alist;
4425 char **name_table;
4426 Lisp_Object *symbol_table;
4427 unsigned int table_size;
4429 Lisp_Object value;
4430 Lisp_Object symbol_int;
4432 /* Get rid of the "vendor-specific" bit here. */
4433 XSETINT (symbol_int, symbol_num & 0xffffff);
4435 /* Is this a request for a valid symbol? */
4436 if (symbol_num < 0 || symbol_num >= table_size)
4437 return Qnil;
4439 if (CONSP (*symbol_table))
4440 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
4442 /* If *symbol_table doesn't seem to be initialized properly, fix that.
4443 *symbol_table should be a lisp vector TABLE_SIZE elements long,
4444 where the Nth element is the symbol for NAME_TABLE[N], or nil if
4445 we've never used that symbol before. */
4446 else
4448 if (! VECTORP (*symbol_table)
4449 || XVECTOR (*symbol_table)->size != table_size)
4451 Lisp_Object size;
4453 XSETFASTINT (size, table_size);
4454 *symbol_table = Fmake_vector (size, Qnil);
4457 value = XVECTOR (*symbol_table)->contents[symbol_num];
4460 /* Have we already used this symbol before? */
4461 if (NILP (value))
4463 /* No; let's create it. */
4464 if (!NILP (name_alist))
4465 value = Fcdr_safe (Fassq (symbol_int, name_alist));
4466 else if (name_table[symbol_num])
4467 value = intern (name_table[symbol_num]);
4469 #ifdef HAVE_WINDOW_SYSTEM
4470 if (NILP (value))
4472 char *name = x_get_keysym_name (symbol_num);
4473 if (name)
4474 value = intern (name);
4476 #endif
4478 if (NILP (value))
4480 char buf[20];
4481 sprintf (buf, "key-%d", symbol_num);
4482 value = intern (buf);
4485 if (CONSP (*symbol_table))
4486 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
4487 else
4488 XVECTOR (*symbol_table)->contents[symbol_num] = value;
4490 /* Fill in the cache entries for this symbol; this also
4491 builds the Qevent_symbol_elements property, which the user
4492 cares about. */
4493 apply_modifiers (modifiers & click_modifier, value);
4494 Fput (value, Qevent_kind, symbol_kind);
4497 /* Apply modifiers to that symbol. */
4498 return apply_modifiers (modifiers, value);
4501 /* Convert a list that represents an event type,
4502 such as (ctrl meta backspace), into the usual representation of that
4503 event type as a number or a symbol. */
4505 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
4506 "Convert the event description list EVENT-DESC to an event type.\n\
4507 EVENT-DESC should contain one base event type (a character or symbol)\n\
4508 and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\
4509 drag, down, double or triple). The base must be last.\n\
4510 The return value is an event type (a character or symbol) which\n\
4511 has the same base event type and all the specified modifiers.")
4512 (event_desc)
4513 Lisp_Object event_desc;
4515 Lisp_Object base;
4516 int modifiers = 0;
4517 Lisp_Object rest;
4519 base = Qnil;
4520 rest = event_desc;
4521 while (CONSP (rest))
4523 Lisp_Object elt;
4524 int this = 0;
4526 elt = XCONS (rest)->car;
4527 rest = XCONS (rest)->cdr;
4529 /* Given a symbol, see if it is a modifier name. */
4530 if (SYMBOLP (elt) && CONSP (rest))
4531 this = parse_solitary_modifier (elt);
4533 if (this != 0)
4534 modifiers |= this;
4535 else if (!NILP (base))
4536 error ("Two bases given in one event");
4537 else
4538 base = elt;
4542 /* Let the symbol A refer to the character A. */
4543 if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1)
4544 XSETINT (base, XSYMBOL (base)->name->data[0]);
4546 if (INTEGERP (base))
4548 /* Turn (shift a) into A. */
4549 if ((modifiers & shift_modifier) != 0
4550 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
4552 XSETINT (base, XINT (base) - ('a' - 'A'));
4553 modifiers &= ~shift_modifier;
4556 /* Turn (control a) into C-a. */
4557 if (modifiers & ctrl_modifier)
4558 return make_number ((modifiers & ~ctrl_modifier)
4559 | make_ctrl_char (XINT (base)));
4560 else
4561 return make_number (modifiers | XINT (base));
4563 else if (SYMBOLP (base))
4564 return apply_modifiers (modifiers, base);
4565 else
4566 error ("Invalid base event");
4569 /* Try to recognize SYMBOL as a modifier name.
4570 Return the modifier flag bit, or 0 if not recognized. */
4572 static int
4573 parse_solitary_modifier (symbol)
4574 Lisp_Object symbol;
4576 struct Lisp_String *name = XSYMBOL (symbol)->name;
4578 switch (name->data[0])
4580 #define SINGLE_LETTER_MOD(BIT) \
4581 if (name->size == 1) \
4582 return BIT;
4584 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
4585 if (LEN == name->size \
4586 && ! strncmp (name->data, NAME, LEN)) \
4587 return BIT;
4589 case 'A':
4590 SINGLE_LETTER_MOD (alt_modifier);
4591 break;
4593 case 'a':
4594 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
4595 break;
4597 case 'C':
4598 SINGLE_LETTER_MOD (ctrl_modifier);
4599 break;
4601 case 'c':
4602 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
4603 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
4604 break;
4606 case 'H':
4607 SINGLE_LETTER_MOD (hyper_modifier);
4608 break;
4610 case 'h':
4611 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
4612 break;
4614 case 'M':
4615 SINGLE_LETTER_MOD (meta_modifier);
4616 break;
4618 case 'm':
4619 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
4620 break;
4622 case 'S':
4623 SINGLE_LETTER_MOD (shift_modifier);
4624 break;
4626 case 's':
4627 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
4628 MULTI_LETTER_MOD (super_modifier, "super", 5);
4629 SINGLE_LETTER_MOD (super_modifier);
4630 break;
4632 case 'd':
4633 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
4634 MULTI_LETTER_MOD (down_modifier, "down", 4);
4635 MULTI_LETTER_MOD (double_modifier, "double", 6);
4636 break;
4638 case 't':
4639 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
4640 break;
4642 #undef SINGLE_LETTER_MOD
4643 #undef MULTI_LETTER_MOD
4646 return 0;
4649 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
4650 Such a list is not valid as an event,
4651 but it can be a Lucid-style event type list. */
4654 lucid_event_type_list_p (object)
4655 Lisp_Object object;
4657 Lisp_Object tail;
4659 if (! CONSP (object))
4660 return 0;
4662 for (tail = object; CONSP (tail); tail = XCONS (tail)->cdr)
4664 Lisp_Object elt;
4665 elt = XCONS (tail)->car;
4666 if (! (INTEGERP (elt) || SYMBOLP (elt)))
4667 return 0;
4670 return NILP (tail);
4673 /* Store into *addr a value nonzero if terminal input chars are available.
4674 Serves the purpose of ioctl (0, FIONREAD, addr)
4675 but works even if FIONREAD does not exist.
4676 (In fact, this may actually read some input.)
4678 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe. */
4680 static void
4681 get_input_pending (addr, do_timers_now)
4682 int *addr;
4683 int do_timers_now;
4685 /* First of all, have we already counted some input? */
4686 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
4688 /* If input is being read as it arrives, and we have none, there is none. */
4689 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
4690 return;
4692 /* Try to read some input and see how much we get. */
4693 gobble_input (0);
4694 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
4697 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
4700 gobble_input (expected)
4701 int expected;
4703 #ifndef VMS
4704 #ifdef SIGIO
4705 if (interrupt_input)
4707 SIGMASKTYPE mask;
4708 mask = sigblock (sigmask (SIGIO));
4709 read_avail_input (expected);
4710 sigsetmask (mask);
4712 else
4713 #ifdef POLL_FOR_INPUT
4714 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
4716 SIGMASKTYPE mask;
4717 mask = sigblock (sigmask (SIGALRM));
4718 read_avail_input (expected);
4719 sigsetmask (mask);
4721 else
4722 #endif
4723 #endif
4724 read_avail_input (expected);
4725 #endif
4728 /* Put a buffer_switch_event in the buffer
4729 so that read_key_sequence will notice the new current buffer. */
4731 record_asynch_buffer_change ()
4733 struct input_event event;
4734 Lisp_Object tem;
4736 event.kind = buffer_switch_event;
4737 event.frame_or_window = Qnil;
4739 #ifdef subprocesses
4740 /* We don't need a buffer-switch event unless Emacs is waiting for input.
4741 The purpose of the event is to make read_key_sequence look up the
4742 keymaps again. If we aren't in read_key_sequence, we don't need one,
4743 and the event could cause trouble by messing up (input-pending-p). */
4744 tem = Fwaiting_for_user_input_p ();
4745 if (NILP (tem))
4746 return;
4747 #else
4748 /* We never need these events if we have no asynchronous subprocesses. */
4749 return;
4750 #endif
4752 /* Make sure no interrupt happens while storing the event. */
4753 #ifdef SIGIO
4754 if (interrupt_input)
4756 SIGMASKTYPE mask;
4757 mask = sigblock (sigmask (SIGIO));
4758 kbd_buffer_store_event (&event);
4759 sigsetmask (mask);
4761 else
4762 #endif
4764 stop_polling ();
4765 kbd_buffer_store_event (&event);
4766 start_polling ();
4770 #ifndef VMS
4772 /* Read any terminal input already buffered up by the system
4773 into the kbd_buffer, but do not wait.
4775 EXPECTED should be nonzero if the caller knows there is some input.
4777 Except on VMS, all input is read by this function.
4778 If interrupt_input is nonzero, this function MUST be called
4779 only when SIGIO is blocked.
4781 Returns the number of keyboard chars read, or -1 meaning
4782 this is a bad time to try to read input. */
4784 static int
4785 read_avail_input (expected)
4786 int expected;
4788 struct input_event buf[KBD_BUFFER_SIZE];
4789 register int i;
4790 int nread;
4792 if (read_socket_hook)
4793 /* No need for FIONREAD or fcntl; just say don't wait. */
4794 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE, expected);
4795 else
4797 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
4798 the kbd_buffer can really hold. That may prevent loss
4799 of characters on some systems when input is stuffed at us. */
4800 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
4801 int n_to_read;
4803 /* Determine how many characters we should *try* to read. */
4804 #ifdef WINDOWSNT
4805 return 0;
4806 #else /* not WINDOWSNT */
4807 #ifdef MSDOS
4808 n_to_read = dos_keysns ();
4809 if (n_to_read == 0)
4810 return 0;
4811 #else /* not MSDOS */
4812 #ifdef FIONREAD
4813 /* Find out how much input is available. */
4814 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
4815 /* Formerly simply reported no input, but that sometimes led to
4816 a failure of Emacs to terminate.
4817 SIGHUP seems appropriate if we can't reach the terminal. */
4818 /* ??? Is it really right to send the signal just to this process
4819 rather than to the whole process group?
4820 Perhaps on systems with FIONREAD Emacs is alone in its group. */
4821 kill (getpid (), SIGHUP);
4822 if (n_to_read == 0)
4823 return 0;
4824 if (n_to_read > sizeof cbuf)
4825 n_to_read = sizeof cbuf;
4826 #else /* no FIONREAD */
4827 #if defined (USG) || defined (DGUX)
4828 /* Read some input if available, but don't wait. */
4829 n_to_read = sizeof cbuf;
4830 fcntl (input_fd, F_SETFL, O_NDELAY);
4831 #else
4832 you lose;
4833 #endif
4834 #endif
4835 #endif /* not MSDOS */
4836 #endif /* not WINDOWSNT */
4838 /* Now read; for one reason or another, this will not block.
4839 NREAD is set to the number of chars read. */
4842 #ifdef MSDOS
4843 cbuf[0] = dos_keyread ();
4844 nread = 1;
4845 #else
4846 nread = read (input_fd, cbuf, n_to_read);
4847 #endif
4848 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
4849 /* The kernel sometimes fails to deliver SIGHUP for ptys.
4850 This looks incorrect, but it isn't, because _BSD causes
4851 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
4852 and that causes a value other than 0 when there is no input. */
4853 if (nread == 0)
4854 kill (0, SIGHUP);
4855 #endif
4857 while (
4858 /* We used to retry the read if it was interrupted.
4859 But this does the wrong thing when O_NDELAY causes
4860 an EAGAIN error. Does anybody know of a situation
4861 where a retry is actually needed? */
4862 #if 0
4863 nread < 0 && (errno == EAGAIN
4864 #ifdef EFAULT
4865 || errno == EFAULT
4866 #endif
4867 #ifdef EBADSLT
4868 || errno == EBADSLT
4869 #endif
4871 #else
4873 #endif
4876 #ifndef FIONREAD
4877 #if defined (USG) || defined (DGUX)
4878 fcntl (input_fd, F_SETFL, 0);
4879 #endif /* USG or DGUX */
4880 #endif /* no FIONREAD */
4881 for (i = 0; i < nread; i++)
4883 buf[i].kind = ascii_keystroke;
4884 buf[i].modifiers = 0;
4885 if (meta_key == 1 && (cbuf[i] & 0x80))
4886 buf[i].modifiers = meta_modifier;
4887 if (meta_key != 2)
4888 cbuf[i] &= ~0x80;
4890 buf[i].code = cbuf[i];
4891 XSETFRAME (buf[i].frame_or_window, selected_frame);
4895 /* Scan the chars for C-g and store them in kbd_buffer. */
4896 for (i = 0; i < nread; i++)
4898 kbd_buffer_store_event (&buf[i]);
4899 /* Don't look at input that follows a C-g too closely.
4900 This reduces lossage due to autorepeat on C-g. */
4901 if (buf[i].kind == ascii_keystroke
4902 && buf[i].code == quit_char)
4903 break;
4906 return nread;
4908 #endif /* not VMS */
4910 #ifdef SIGIO /* for entire page */
4911 /* Note SIGIO has been undef'd if FIONREAD is missing. */
4913 SIGTYPE
4914 input_available_signal (signo)
4915 int signo;
4917 /* Must preserve main program's value of errno. */
4918 int old_errno = errno;
4919 #ifdef BSD4_1
4920 extern int select_alarmed;
4921 #endif
4923 #if defined (USG) && !defined (POSIX_SIGNALS)
4924 /* USG systems forget handlers when they are used;
4925 must reestablish each time */
4926 signal (signo, input_available_signal);
4927 #endif /* USG */
4929 #ifdef BSD4_1
4930 sigisheld (SIGIO);
4931 #endif
4933 if (input_available_clear_time)
4934 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
4936 while (1)
4938 int nread;
4939 nread = read_avail_input (1);
4940 /* -1 means it's not ok to read the input now.
4941 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
4942 0 means there was no keyboard input available. */
4943 if (nread <= 0)
4944 break;
4946 #ifdef BSD4_1
4947 select_alarmed = 1; /* Force the select emulator back to life */
4948 #endif
4951 #ifdef BSD4_1
4952 sigfree ();
4953 #endif
4954 errno = old_errno;
4956 #endif /* SIGIO */
4958 /* Send ourselves a SIGIO.
4960 This function exists so that the UNBLOCK_INPUT macro in
4961 blockinput.h can have some way to take care of input we put off
4962 dealing with, without assuming that every file which uses
4963 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
4964 void
4965 reinvoke_input_signal ()
4967 #ifdef SIGIO
4968 kill (getpid (), SIGIO);
4969 #endif
4974 /* Return the prompt-string of a sparse keymap.
4975 This is the first element which is a string.
4976 Return nil if there is none. */
4978 Lisp_Object
4979 map_prompt (map)
4980 Lisp_Object map;
4982 while (CONSP (map))
4984 register Lisp_Object tem;
4985 tem = Fcar (map);
4986 if (STRINGP (tem))
4987 return tem;
4988 map = Fcdr (map);
4990 return Qnil;
4993 static void menu_bar_item ();
4994 static void menu_bar_one_keymap ();
4996 /* These variables hold the vector under construction within
4997 menu_bar_items and its subroutines, and the current index
4998 for storing into that vector. */
4999 static Lisp_Object menu_bar_items_vector;
5000 static int menu_bar_items_index;
5002 /* Return a vector of menu items for a menu bar, appropriate
5003 to the current buffer. Each item has three elements in the vector:
5004 KEY STRING MAPLIST.
5006 OLD is an old vector we can optionally reuse, or nil. */
5008 Lisp_Object
5009 menu_bar_items (old)
5010 Lisp_Object old;
5012 /* The number of keymaps we're scanning right now, and the number of
5013 keymaps we have allocated space for. */
5014 int nmaps;
5016 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
5017 in the current keymaps, or nil where it is not a prefix. */
5018 Lisp_Object *maps;
5020 Lisp_Object def, tem, tail;
5022 Lisp_Object result;
5024 int mapno;
5025 Lisp_Object oquit;
5027 int i;
5029 struct gcpro gcpro1;
5031 /* In order to build the menus, we need to call the keymap
5032 accessors. They all call QUIT. But this function is called
5033 during redisplay, during which a quit is fatal. So inhibit
5034 quitting while building the menus.
5035 We do this instead of specbind because (1) errors will clear it anyway
5036 and (2) this avoids risk of specpdl overflow. */
5037 oquit = Vinhibit_quit;
5038 Vinhibit_quit = Qt;
5040 if (!NILP (old))
5041 menu_bar_items_vector = old;
5042 else
5043 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
5044 menu_bar_items_index = 0;
5046 GCPRO1 (menu_bar_items_vector);
5048 /* Build our list of keymaps.
5049 If we recognize a function key and replace its escape sequence in
5050 keybuf with its symbol, or if the sequence starts with a mouse
5051 click and we need to switch buffers, we jump back here to rebuild
5052 the initial keymaps from the current buffer. */
5054 Lisp_Object *tmaps;
5056 /* Should overriding-terminal-local-map and overriding-local-map apply? */
5057 if (!NILP (Voverriding_local_map_menu_flag))
5059 /* Yes, use them (if non-nil) as well as the global map. */
5060 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
5061 nmaps = 0;
5062 if (!NILP (current_kboard->Voverriding_terminal_local_map))
5063 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
5064 if (!NILP (Voverriding_local_map))
5065 maps[nmaps++] = Voverriding_local_map;
5067 else
5069 /* No, so use major and minor mode keymaps. */
5070 nmaps = current_minor_maps (NULL, &tmaps);
5071 maps = (Lisp_Object *) alloca ((nmaps + 2) * sizeof (maps[0]));
5072 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
5073 #ifdef USE_TEXT_PROPERTIES
5074 maps[nmaps++] = get_local_map (PT, current_buffer);
5075 #else
5076 maps[nmaps++] = current_buffer->keymap;
5077 #endif
5079 maps[nmaps++] = current_global_map;
5082 /* Look up in each map the dummy prefix key `menu-bar'. */
5084 result = Qnil;
5086 for (mapno = nmaps - 1; mapno >= 0; mapno--)
5088 if (! NILP (maps[mapno]))
5089 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0));
5090 else
5091 def = Qnil;
5093 tem = Fkeymapp (def);
5094 if (!NILP (tem))
5095 menu_bar_one_keymap (def);
5098 /* Move to the end those items that should be at the end. */
5100 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCONS (tail)->cdr)
5102 int i;
5103 int end = menu_bar_items_index;
5105 for (i = 0; i < end; i += 4)
5106 if (EQ (XCONS (tail)->car, XVECTOR (menu_bar_items_vector)->contents[i]))
5108 Lisp_Object tem0, tem1, tem2, tem3;
5109 /* Move the item at index I to the end,
5110 shifting all the others forward. */
5111 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
5112 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
5113 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
5114 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
5115 if (end > i + 4)
5116 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
5117 &XVECTOR (menu_bar_items_vector)->contents[i],
5118 (end - i - 4) * sizeof (Lisp_Object));
5119 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
5120 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
5121 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
5122 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
5123 break;
5127 /* Add nil, nil, nil, nil at the end. */
5128 i = menu_bar_items_index;
5129 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
5131 Lisp_Object tem;
5132 int newsize = 2 * i;
5133 tem = Fmake_vector (make_number (2 * i), Qnil);
5134 bcopy (XVECTOR (menu_bar_items_vector)->contents,
5135 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
5136 menu_bar_items_vector = tem;
5138 /* Add this item. */
5139 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5140 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5141 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5142 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5143 menu_bar_items_index = i;
5145 Vinhibit_quit = oquit;
5146 UNGCPRO;
5147 return menu_bar_items_vector;
5150 /* Scan one map KEYMAP, accumulating any menu items it defines
5151 in menu_bar_items_vector. */
5153 static void
5154 menu_bar_one_keymap (keymap)
5155 Lisp_Object keymap;
5157 Lisp_Object tail, item, key, binding, item_string, table;
5159 /* Loop over all keymap entries that have menu strings. */
5160 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
5162 item = XCONS (tail)->car;
5163 if (CONSP (item))
5165 key = XCONS (item)->car;
5166 binding = XCONS (item)->cdr;
5167 if (CONSP (binding))
5169 item_string = XCONS (binding)->car;
5170 if (STRINGP (item_string))
5171 menu_bar_item (key, item_string, Fcdr (binding));
5173 else if (EQ (binding, Qundefined))
5174 menu_bar_item (key, Qnil, binding);
5176 else if (VECTORP (item))
5178 /* Loop over the char values represented in the vector. */
5179 int len = XVECTOR (item)->size;
5180 int c;
5181 for (c = 0; c < len; c++)
5183 Lisp_Object character;
5184 XSETFASTINT (character, c);
5185 binding = XVECTOR (item)->contents[c];
5186 if (CONSP (binding))
5188 item_string = XCONS (binding)->car;
5189 if (STRINGP (item_string))
5190 menu_bar_item (key, item_string, Fcdr (binding));
5192 else if (EQ (binding, Qundefined))
5193 menu_bar_item (key, Qnil, binding);
5199 /* This is used as the handler when calling internal_condition_case_1. */
5201 static Lisp_Object
5202 menu_bar_item_1 (arg)
5203 Lisp_Object arg;
5205 return Qnil;
5208 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
5209 If there's already an item for KEY, add this DEF to it. */
5211 static void
5212 menu_bar_item (key, item_string, def)
5213 Lisp_Object key, item_string, def;
5215 Lisp_Object tem;
5216 Lisp_Object enabled;
5217 int i;
5219 /* Skip menu-bar equiv keys data. */
5220 if (CONSP (def) && CONSP (XCONS (def)->car))
5221 def = XCONS (def)->cdr;
5223 if (EQ (def, Qundefined))
5225 /* If a map has an explicit `undefined' as definition,
5226 discard any previously made menu bar item. */
5228 for (i = 0; i < menu_bar_items_index; i += 4)
5229 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
5231 if (menu_bar_items_index > i + 4)
5232 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
5233 &XVECTOR (menu_bar_items_vector)->contents[i],
5234 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
5235 menu_bar_items_index -= 4;
5236 return;
5239 /* If there's no definition for this key yet,
5240 just ignore `undefined'. */
5241 return;
5244 /* See if this entry is enabled. */
5245 enabled = Qt;
5247 if (SYMBOLP (def))
5249 /* No property, or nil, means enable.
5250 Otherwise, enable if value is not nil. */
5251 tem = Fget (def, Qmenu_enable);
5252 if (!NILP (tem))
5253 /* (condition-case nil (eval tem)
5254 (error nil)) */
5255 enabled = internal_condition_case_1 (Feval, tem, Qerror,
5256 menu_bar_item_1);
5259 /* Ignore this item if it's not enabled. */
5260 if (NILP (enabled))
5261 return;
5263 /* Find any existing item for this KEY. */
5264 for (i = 0; i < menu_bar_items_index; i += 4)
5265 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
5266 break;
5268 /* If we did not find this KEY, add it at the end. */
5269 if (i == menu_bar_items_index)
5271 /* If vector is too small, get a bigger one. */
5272 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
5274 Lisp_Object tem;
5275 int newsize = 2 * i;
5276 tem = Fmake_vector (make_number (2 * i), Qnil);
5277 bcopy (XVECTOR (menu_bar_items_vector)->contents,
5278 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
5279 menu_bar_items_vector = tem;
5281 /* Add this item. */
5282 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
5283 XVECTOR (menu_bar_items_vector)->contents[i++] = item_string;
5284 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (def, Qnil);
5285 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
5286 menu_bar_items_index = i;
5288 /* We did find an item for this KEY. Add DEF to its list of maps. */
5289 else
5291 Lisp_Object old;
5292 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
5293 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (def, old);
5297 /* Read a character using menus based on maps in the array MAPS.
5298 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
5299 Return t if we displayed a menu but the user rejected it.
5301 PREV_EVENT is the previous input event, or nil if we are reading
5302 the first event of a key sequence.
5304 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
5305 if we used a mouse menu to read the input, or zero otherwise. If
5306 USED_MOUSE_MENU is null, we don't dereference it.
5308 The prompting is done based on the prompt-string of the map
5309 and the strings associated with various map elements.
5311 This can be done with X menus or with menus put in the minibuf.
5312 These are done in different ways, depending on how the input will be read.
5313 Menus using X are done after auto-saving in read-char, getting the input
5314 event from Fx_popup_menu; menus using the minibuf use read_char recursively
5315 and do auto-saving in the inner call of read_char. */
5317 static Lisp_Object
5318 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
5319 int nmaps;
5320 Lisp_Object *maps;
5321 Lisp_Object prev_event;
5322 int *used_mouse_menu;
5324 int mapno;
5325 register Lisp_Object name;
5326 Lisp_Object rest, vector;
5328 if (used_mouse_menu)
5329 *used_mouse_menu = 0;
5331 /* Use local over global Menu maps */
5333 if (! menu_prompting)
5334 return Qnil;
5336 /* Optionally disregard all but the global map. */
5337 if (inhibit_local_menu_bar_menus)
5339 maps += (nmaps - 1);
5340 nmaps = 1;
5343 /* Get the menu name from the first map that has one (a prompt string). */
5344 for (mapno = 0; mapno < nmaps; mapno++)
5346 name = map_prompt (maps[mapno]);
5347 if (!NILP (name))
5348 break;
5351 /* If we don't have any menus, just read a character normally. */
5352 if (mapno >= nmaps)
5353 return Qnil;
5355 #ifdef HAVE_MENUS
5356 /* If we got to this point via a mouse click,
5357 use a real menu for mouse selection. */
5358 if (EVENT_HAS_PARAMETERS (prev_event)
5359 && !EQ (XCONS (prev_event)->car, Qmenu_bar))
5361 /* Display the menu and get the selection. */
5362 Lisp_Object *realmaps
5363 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
5364 Lisp_Object value;
5365 int nmaps1 = 0;
5367 /* Use the maps that are not nil. */
5368 for (mapno = 0; mapno < nmaps; mapno++)
5369 if (!NILP (maps[mapno]))
5370 realmaps[nmaps1++] = maps[mapno];
5372 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
5373 if (CONSP (value))
5375 Lisp_Object tem;
5377 /* If we got multiple events, unread all but
5378 the first.
5379 There is no way to prevent those unread events
5380 from showing up later in last_nonmenu_event.
5381 So turn symbol and integer events into lists,
5382 to indicate that they came from a mouse menu,
5383 so that when present in last_nonmenu_event
5384 they won't confuse things. */
5385 for (tem = XCONS (value)->cdr; !NILP (tem);
5386 tem = XCONS (tem)->cdr)
5387 if (SYMBOLP (XCONS (tem)->car)
5388 || INTEGERP (XCONS (tem)->car))
5389 XCONS (tem)->car
5390 = Fcons (XCONS (tem)->car, Qnil);
5392 /* If we got more than one event, put all but the first
5393 onto this list to be read later.
5394 Return just the first event now. */
5395 Vunread_command_events
5396 = nconc2 (XCONS (value)->cdr, Vunread_command_events);
5397 value = XCONS (value)->car;
5399 else if (NILP (value))
5400 value = Qt;
5401 if (used_mouse_menu)
5402 *used_mouse_menu = 1;
5403 return value;
5405 #endif /* HAVE_MENUS */
5406 return Qnil ;
5409 /* Buffer in use so far for the minibuf prompts for menu keymaps.
5410 We make this bigger when necessary, and never free it. */
5411 static char *read_char_minibuf_menu_text;
5412 /* Size of that buffer. */
5413 static int read_char_minibuf_menu_width;
5415 static Lisp_Object
5416 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
5417 int commandflag ;
5418 int nmaps;
5419 Lisp_Object *maps;
5421 int mapno;
5422 register Lisp_Object name;
5423 int nlength;
5424 int width = FRAME_WIDTH (selected_frame) - 4;
5425 int idx = -1;
5426 int nobindings = 1;
5427 Lisp_Object rest, vector;
5428 char *menu;
5430 if (! menu_prompting)
5431 return Qnil;
5433 /* Make sure we have a big enough buffer for the menu text. */
5434 if (read_char_minibuf_menu_text == 0)
5436 read_char_minibuf_menu_width = width + 4;
5437 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
5439 else if (width + 4 > read_char_minibuf_menu_width)
5441 read_char_minibuf_menu_width = width + 4;
5442 read_char_minibuf_menu_text
5443 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
5445 menu = read_char_minibuf_menu_text;
5447 /* Get the menu name from the first map that has one (a prompt string). */
5448 for (mapno = 0; mapno < nmaps; mapno++)
5450 name = map_prompt (maps[mapno]);
5451 if (!NILP (name))
5452 break;
5455 /* If we don't have any menus, just read a character normally. */
5456 if (mapno >= nmaps)
5457 return Qnil;
5459 /* Prompt string always starts with map's prompt, and a space. */
5460 strcpy (menu, XSTRING (name)->data);
5461 nlength = XSTRING (name)->size;
5462 menu[nlength++] = ':';
5463 menu[nlength++] = ' ';
5464 menu[nlength] = 0;
5466 /* Start prompting at start of first map. */
5467 mapno = 0;
5468 rest = maps[mapno];
5470 /* Present the documented bindings, a line at a time. */
5471 while (1)
5473 int notfirst = 0;
5474 int i = nlength;
5475 Lisp_Object obj;
5476 int ch;
5477 Lisp_Object orig_defn_macro;
5479 /* Loop over elements of map. */
5480 while (i < width)
5482 Lisp_Object s, elt;
5484 /* If reached end of map, start at beginning of next map. */
5485 if (NILP (rest))
5487 mapno++;
5488 /* At end of last map, wrap around to first map if just starting,
5489 or end this line if already have something on it. */
5490 if (mapno == nmaps)
5492 mapno = 0;
5493 if (notfirst || nobindings) break;
5495 rest = maps[mapno];
5498 /* Look at the next element of the map. */
5499 if (idx >= 0)
5500 elt = XVECTOR (vector)->contents[idx];
5501 else
5502 elt = Fcar_safe (rest);
5504 if (idx < 0 && VECTORP (elt))
5506 /* If we found a dense table in the keymap,
5507 advanced past it, but start scanning its contents. */
5508 rest = Fcdr_safe (rest);
5509 vector = elt;
5510 idx = 0;
5512 else
5514 /* An ordinary element. */
5515 Lisp_Object event;
5517 if (idx < 0)
5519 s = Fcar_safe (Fcdr_safe (elt)); /* alist */
5520 event = Fcar_safe (elt);
5522 else
5524 s = Fcar_safe (elt); /* vector */
5525 XSETINT (event, idx);
5528 /* Ignore the element if it has no prompt string. */
5529 if (STRINGP (s) && INTEGERP (event))
5531 /* 1 if the char to type matches the string. */
5532 int char_matches;
5533 Lisp_Object upcased_event, downcased_event;
5534 Lisp_Object desc;
5536 upcased_event = Fupcase (event);
5537 downcased_event = Fdowncase (event);
5538 char_matches = (XINT (upcased_event) == XSTRING (s)->data[0]
5539 || XINT (downcased_event) == XSTRING (s)->data[0]);
5540 if (! char_matches)
5541 desc = Fsingle_key_description (event);
5543 /* If we have room for the prompt string, add it to this line.
5544 If this is the first on the line, always add it. */
5545 if ((XSTRING (s)->size + i + 2
5546 + (char_matches ? 0 : XSTRING (desc)->size + 3))
5547 < width
5548 || !notfirst)
5550 int thiswidth;
5552 /* Punctuate between strings. */
5553 if (notfirst)
5555 strcpy (menu + i, ", ");
5556 i += 2;
5558 notfirst = 1;
5559 nobindings = 0 ;
5561 /* If the char to type doesn't match the string's
5562 first char, explicitly show what char to type. */
5563 if (! char_matches)
5565 /* Add as much of string as fits. */
5566 thiswidth = XSTRING (desc)->size;
5567 if (thiswidth + i > width)
5568 thiswidth = width - i;
5569 bcopy (XSTRING (desc)->data, menu + i, thiswidth);
5570 i += thiswidth;
5571 strcpy (menu + i, " = ");
5572 i += 3;
5575 /* Add as much of string as fits. */
5576 thiswidth = XSTRING (s)->size;
5577 if (thiswidth + i > width)
5578 thiswidth = width - i;
5579 bcopy (XSTRING (s)->data, menu + i, thiswidth);
5580 i += thiswidth;
5581 menu[i] = 0;
5583 else
5585 /* If this element does not fit, end the line now,
5586 and save the element for the next line. */
5587 strcpy (menu + i, "...");
5588 break;
5592 /* Move past this element. */
5593 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
5594 /* Handle reaching end of dense table. */
5595 idx = -1;
5596 if (idx >= 0)
5597 idx++;
5598 else
5599 rest = Fcdr_safe (rest);
5603 /* Prompt with that and read response. */
5604 message1 (menu);
5606 /* Make believe its not a keyboard macro in case the help char
5607 is pressed. Help characters are not recorded because menu prompting
5608 is not used on replay.
5610 orig_defn_macro = current_kboard->defining_kbd_macro;
5611 current_kboard->defining_kbd_macro = Qnil;
5613 obj = read_char (commandflag, 0, 0, Qnil, 0);
5614 while (BUFFERP (obj));
5615 current_kboard->defining_kbd_macro = orig_defn_macro;
5617 if (!INTEGERP (obj))
5618 return obj;
5619 else
5620 ch = XINT (obj);
5622 if (! EQ (obj, menu_prompt_more_char)
5623 && (!INTEGERP (menu_prompt_more_char)
5624 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
5626 if (!NILP (current_kboard->defining_kbd_macro))
5627 store_kbd_macro_char (obj);
5628 return obj;
5630 /* Help char - go round again */
5634 /* Reading key sequences. */
5636 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
5637 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
5638 keymap, or nil otherwise. Return the index of the first keymap in
5639 which KEY has any binding, or NMAPS if no map has a binding.
5641 If KEY is a meta ASCII character, treat it like meta-prefix-char
5642 followed by the corresponding non-meta character. Keymaps in
5643 CURRENT with non-prefix bindings for meta-prefix-char become nil in
5644 NEXT.
5646 If KEY has no bindings in any of the CURRENT maps, NEXT is left
5647 unmodified.
5649 NEXT may be the same array as CURRENT. */
5651 static int
5652 follow_key (key, nmaps, current, defs, next)
5653 Lisp_Object key;
5654 Lisp_Object *current, *defs, *next;
5655 int nmaps;
5657 int i, first_binding;
5658 int did_meta = 0;
5660 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
5661 followed by the corresponding non-meta character.
5662 Put the results into DEFS, since we are going to alter that anyway.
5663 Do not alter CURRENT or NEXT. */
5664 if (INTEGERP (key) && (XINT (key) & CHAR_META))
5666 for (i = 0; i < nmaps; i++)
5667 if (! NILP (current[i]))
5669 Lisp_Object def;
5670 def = get_keyelt (access_keymap (current[i],
5671 meta_prefix_char, 1, 0));
5673 /* Note that since we pass the resulting bindings through
5674 get_keymap_1, non-prefix bindings for meta-prefix-char
5675 disappear. */
5676 defs[i] = get_keymap_1 (def, 0, 1);
5678 else
5679 defs[i] = Qnil;
5681 did_meta = 1;
5682 XSETINT (key, XFASTINT (key) & ~CHAR_META);
5685 first_binding = nmaps;
5686 for (i = nmaps - 1; i >= 0; i--)
5688 if (! NILP (current[i]))
5690 Lisp_Object map;
5691 if (did_meta)
5692 map = defs[i];
5693 else
5694 map = current[i];
5696 defs[i] = get_keyelt (access_keymap (map, key, 1, 0));
5697 if (! NILP (defs[i]))
5698 first_binding = i;
5700 else
5701 defs[i] = Qnil;
5704 /* Given the set of bindings we've found, produce the next set of maps. */
5705 if (first_binding < nmaps)
5706 for (i = 0; i < nmaps; i++)
5707 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
5709 return first_binding;
5712 /* Read a sequence of keys that ends with a non prefix character,
5713 storing it in KEYBUF, a buffer of size BUFSIZE.
5714 Prompt with PROMPT.
5715 Return the length of the key sequence stored.
5716 Return -1 if the user rejected a command menu.
5718 Echo starting immediately unless `prompt' is 0.
5720 Where a key sequence ends depends on the currently active keymaps.
5721 These include any minor mode keymaps active in the current buffer,
5722 the current buffer's local map, and the global map.
5724 If a key sequence has no other bindings, we check Vfunction_key_map
5725 to see if some trailing subsequence might be the beginning of a
5726 function key's sequence. If so, we try to read the whole function
5727 key, and substitute its symbolic name into the key sequence.
5729 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
5730 `double-' events into similar click events, if that would make them
5731 bound. We try to turn `triple-' events first into `double-' events,
5732 then into clicks.
5734 If we get a mouse click in a mode line, vertical divider, or other
5735 non-text area, we treat the click as if it were prefixed by the
5736 symbol denoting that area - `mode-line', `vertical-line', or
5737 whatever.
5739 If the sequence starts with a mouse click, we read the key sequence
5740 with respect to the buffer clicked on, not the current buffer.
5742 If the user switches frames in the midst of a key sequence, we put
5743 off the switch-frame event until later; the next call to
5744 read_char will return it. */
5746 static int
5747 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
5748 can_return_switch_frame)
5749 Lisp_Object *keybuf;
5750 int bufsize;
5751 Lisp_Object prompt;
5752 int dont_downcase_last;
5753 int can_return_switch_frame;
5755 int count = specpdl_ptr - specpdl;
5757 /* How many keys there are in the current key sequence. */
5758 int t;
5760 /* The length of the echo buffer when we started reading, and
5761 the length of this_command_keys when we started reading. */
5762 int echo_start;
5763 int keys_start;
5765 /* The number of keymaps we're scanning right now, and the number of
5766 keymaps we have allocated space for. */
5767 int nmaps;
5768 int nmaps_allocated = 0;
5770 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
5771 the current keymaps. */
5772 Lisp_Object *defs;
5774 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
5775 in the current keymaps, or nil where it is not a prefix. */
5776 Lisp_Object *submaps;
5778 /* The local map to start out with at start of key sequence. */
5779 Lisp_Object orig_local_map;
5781 /* 1 if we have already considered switching to the local-map property
5782 of the place where a mouse click occurred. */
5783 int localized_local_map = 0;
5785 /* The index in defs[] of the first keymap that has a binding for
5786 this key sequence. In other words, the lowest i such that
5787 defs[i] is non-nil. */
5788 int first_binding;
5790 /* If t < mock_input, then KEYBUF[t] should be read as the next
5791 input key.
5793 We use this to recover after recognizing a function key. Once we
5794 realize that a suffix of the current key sequence is actually a
5795 function key's escape sequence, we replace the suffix with the
5796 function key's binding from Vfunction_key_map. Now keybuf
5797 contains a new and different key sequence, so the echo area,
5798 this_command_keys, and the submaps and defs arrays are wrong. In
5799 this situation, we set mock_input to t, set t to 0, and jump to
5800 restart_sequence; the loop will read keys from keybuf up until
5801 mock_input, thus rebuilding the state; and then it will resume
5802 reading characters from the keyboard. */
5803 int mock_input = 0;
5805 /* If the sequence is unbound in submaps[], then
5806 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
5807 and fkey_map is its binding.
5809 These might be > t, indicating that all function key scanning
5810 should hold off until t reaches them. We do this when we've just
5811 recognized a function key, to avoid searching for the function
5812 key's again in Vfunction_key_map. */
5813 int fkey_start = 0, fkey_end = 0;
5814 Lisp_Object fkey_map;
5816 /* Likewise, for key_translation_map. */
5817 int keytran_start = 0, keytran_end = 0;
5818 Lisp_Object keytran_map;
5820 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
5821 we put it off for later. While we're reading, we keep the event here. */
5822 Lisp_Object delayed_switch_frame;
5824 /* See the comment below... */
5825 #if defined (GOBBLE_FIRST_EVENT)
5826 Lisp_Object first_event;
5827 #endif
5829 Lisp_Object original_uppercase;
5830 int original_uppercase_position = -1;
5832 /* Gets around Microsoft compiler limitations. */
5833 int dummyflag = 0;
5835 struct buffer *starting_buffer;
5837 /* Nonzero if we seem to have got the beginning of a binding
5838 in function_key_map. */
5839 int function_key_possible = 0;
5840 int key_translation_possible = 0;
5842 /* Save the status of key translation before each step,
5843 so that we can restore this after downcasing. */
5844 Lisp_Object prev_fkey_map;
5845 Lisp_Object prev_fkey_start;
5846 Lisp_Object prev_fkey_end;
5848 Lisp_Object prev_keytran_map;
5849 Lisp_Object prev_keytran_start;
5850 Lisp_Object prev_keytran_end;
5852 int junk;
5854 last_nonmenu_event = Qnil;
5856 delayed_switch_frame = Qnil;
5857 fkey_map = Vfunction_key_map;
5858 keytran_map = Vkey_translation_map;
5860 /* If there is no function-key-map, turn off function key scanning. */
5861 if (NILP (Fkeymapp (Vfunction_key_map)))
5862 fkey_start = fkey_end = bufsize + 1;
5864 /* If there is no key-translation-map, turn off scanning. */
5865 if (NILP (Fkeymapp (Vkey_translation_map)))
5866 keytran_start = keytran_end = bufsize + 1;
5868 if (INTERACTIVE)
5870 if (!NILP (prompt))
5871 echo_prompt (XSTRING (prompt)->data);
5872 else if (cursor_in_echo_area && echo_keystrokes)
5873 /* This doesn't put in a dash if the echo buffer is empty, so
5874 you don't always see a dash hanging out in the minibuffer. */
5875 echo_dash ();
5878 /* Record the initial state of the echo area and this_command_keys;
5879 we will need to restore them if we replay a key sequence. */
5880 if (INTERACTIVE)
5881 echo_start = echo_length ();
5882 keys_start = this_command_key_count;
5883 this_single_command_key_start = keys_start;
5885 #if defined (GOBBLE_FIRST_EVENT)
5886 /* This doesn't quite work, because some of the things that read_char
5887 does cannot safely be bypassed. It seems too risky to try to make
5888 this work right. */
5890 /* Read the first char of the sequence specially, before setting
5891 up any keymaps, in case a filter runs and switches buffers on us. */
5892 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
5893 &junk);
5894 #endif /* GOBBLE_FIRST_EVENT */
5896 orig_local_map = get_local_map (PT, current_buffer);
5898 /* We jump here when the key sequence has been thoroughly changed, and
5899 we need to rescan it starting from the beginning. When we jump here,
5900 keybuf[0..mock_input] holds the sequence we should reread. */
5901 replay_sequence:
5903 starting_buffer = current_buffer;
5904 function_key_possible = 0;
5905 key_translation_possible = 0;
5907 /* Build our list of keymaps.
5908 If we recognize a function key and replace its escape sequence in
5909 keybuf with its symbol, or if the sequence starts with a mouse
5910 click and we need to switch buffers, we jump back here to rebuild
5911 the initial keymaps from the current buffer. */
5913 Lisp_Object *maps;
5915 if (!NILP (current_kboard->Voverriding_terminal_local_map)
5916 || !NILP (Voverriding_local_map))
5918 if (3 > nmaps_allocated)
5920 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
5921 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
5922 nmaps_allocated = 3;
5924 nmaps = 0;
5925 if (!NILP (current_kboard->Voverriding_terminal_local_map))
5926 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
5927 if (!NILP (Voverriding_local_map))
5928 submaps[nmaps++] = Voverriding_local_map;
5930 else
5932 nmaps = current_minor_maps (0, &maps);
5933 if (nmaps + 2 > nmaps_allocated)
5935 submaps = (Lisp_Object *) alloca ((nmaps+2) * sizeof (submaps[0]));
5936 defs = (Lisp_Object *) alloca ((nmaps+2) * sizeof (defs[0]));
5937 nmaps_allocated = nmaps + 2;
5939 bcopy (maps, submaps, nmaps * sizeof (submaps[0]));
5940 #ifdef USE_TEXT_PROPERTIES
5941 submaps[nmaps++] = orig_local_map;
5942 #else
5943 submaps[nmaps++] = current_buffer->keymap;
5944 #endif
5946 submaps[nmaps++] = current_global_map;
5949 /* Find an accurate initial value for first_binding. */
5950 for (first_binding = 0; first_binding < nmaps; first_binding++)
5951 if (! NILP (submaps[first_binding]))
5952 break;
5954 /* Start from the beginning in keybuf. */
5955 t = 0;
5957 /* These are no-ops the first time through, but if we restart, they
5958 revert the echo area and this_command_keys to their original state. */
5959 this_command_key_count = keys_start;
5960 if (INTERACTIVE && t < mock_input)
5961 echo_truncate (echo_start);
5963 /* If the best binding for the current key sequence is a keymap, or
5964 we may be looking at a function key's escape sequence, keep on
5965 reading. */
5966 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
5967 || (first_binding >= nmaps
5968 && fkey_start < t
5969 /* mock input is never part of a function key's sequence. */
5970 && mock_input <= fkey_start)
5971 || (first_binding >= nmaps
5972 && keytran_start < t && key_translation_possible)
5973 /* Don't return in the middle of a possible function key sequence,
5974 if the only bindings we found were via case conversion.
5975 Thus, if ESC O a has a function-key-map translation
5976 and ESC o has a binding, don't return after ESC O,
5977 so that we can translate ESC O plus the next character. */
5980 Lisp_Object key;
5981 int used_mouse_menu = 0;
5983 /* Where the last real key started. If we need to throw away a
5984 key that has expanded into more than one element of keybuf
5985 (say, a mouse click on the mode line which is being treated
5986 as [mode-line (mouse-...)], then we backtrack to this point
5987 of keybuf. */
5988 int last_real_key_start;
5990 /* These variables are analogous to echo_start and keys_start;
5991 while those allow us to restart the entire key sequence,
5992 echo_local_start and keys_local_start allow us to throw away
5993 just one key. */
5994 int echo_local_start, keys_local_start, local_first_binding;
5996 if (t >= bufsize)
5997 error ("Key sequence too long");
5999 if (INTERACTIVE)
6000 echo_local_start = echo_length ();
6001 keys_local_start = this_command_key_count;
6002 local_first_binding = first_binding;
6004 replay_key:
6005 /* These are no-ops, unless we throw away a keystroke below and
6006 jumped back up to replay_key; in that case, these restore the
6007 variables to their original state, allowing us to replay the
6008 loop. */
6009 if (INTERACTIVE && t < mock_input)
6010 echo_truncate (echo_local_start);
6011 this_command_key_count = keys_local_start;
6012 first_binding = local_first_binding;
6014 /* By default, assume each event is "real". */
6015 last_real_key_start = t;
6017 /* Does mock_input indicate that we are re-reading a key sequence? */
6018 if (t < mock_input)
6020 key = keybuf[t];
6021 add_command_key (key);
6022 if (echo_keystrokes)
6023 echo_char (key);
6026 /* If not, we should actually read a character. */
6027 else
6029 struct buffer *buf = current_buffer;
6032 #ifdef MULTI_KBOARD
6033 KBOARD *interrupted_kboard = current_kboard;
6034 struct frame *interrupted_frame = selected_frame;
6035 if (setjmp (wrong_kboard_jmpbuf))
6037 if (!NILP (delayed_switch_frame))
6039 interrupted_kboard->kbd_queue
6040 = Fcons (delayed_switch_frame,
6041 interrupted_kboard->kbd_queue);
6042 delayed_switch_frame = Qnil;
6044 while (t > 0)
6045 interrupted_kboard->kbd_queue
6046 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
6048 /* If the side queue is non-empty, ensure it begins with a
6049 switch-frame, so we'll replay it in the right context. */
6050 if (CONSP (interrupted_kboard->kbd_queue)
6051 && (key = XCONS (interrupted_kboard->kbd_queue)->car,
6052 !(EVENT_HAS_PARAMETERS (key)
6053 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
6054 Qswitch_frame))))
6056 Lisp_Object frame;
6057 XSETFRAME (frame, interrupted_frame);
6058 interrupted_kboard->kbd_queue
6059 = Fcons (make_lispy_switch_frame (frame),
6060 interrupted_kboard->kbd_queue);
6062 mock_input = 0;
6063 orig_local_map = get_local_map (PT, current_buffer);
6064 goto replay_sequence;
6066 #endif
6067 key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
6068 &used_mouse_menu);
6071 /* read_char returns t when it shows a menu and the user rejects it.
6072 Just return -1. */
6073 if (EQ (key, Qt))
6074 return -1;
6076 /* read_char returns -1 at the end of a macro.
6077 Emacs 18 handles this by returning immediately with a
6078 zero, so that's what we'll do. */
6079 if (INTEGERP (key) && XINT (key) == -1)
6081 t = 0;
6082 /* The Microsoft C compiler can't handle the goto that
6083 would go here. */
6084 dummyflag = 1;
6085 break;
6088 /* If the current buffer has been changed from under us, the
6089 keymap may have changed, so replay the sequence. */
6090 if (BUFFERP (key))
6092 mock_input = t;
6093 orig_local_map = get_local_map (PT, current_buffer);
6094 goto replay_sequence;
6097 /* If we have a quit that was typed in another frame, and
6098 quit_throw_to_read_char switched buffers,
6099 replay to get the right keymap. */
6100 if (XINT (key) == quit_char && current_buffer != starting_buffer)
6102 keybuf[t++] = key;
6103 mock_input = t;
6104 Vquit_flag = Qnil;
6105 orig_local_map = get_local_map (PT, current_buffer);
6106 goto replay_sequence;
6109 Vquit_flag = Qnil;
6112 /* Clicks in non-text areas get prefixed by the symbol
6113 in their CHAR-ADDRESS field. For example, a click on
6114 the mode line is prefixed by the symbol `mode-line'.
6116 Furthermore, key sequences beginning with mouse clicks
6117 are read using the keymaps of the buffer clicked on, not
6118 the current buffer. So we may have to switch the buffer
6119 here.
6121 When we turn one event into two events, we must make sure
6122 that neither of the two looks like the original--so that,
6123 if we replay the events, they won't be expanded again.
6124 If not for this, such reexpansion could happen either here
6125 or when user programs play with this-command-keys. */
6126 if (EVENT_HAS_PARAMETERS (key))
6128 Lisp_Object kind;
6130 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
6131 if (EQ (kind, Qmouse_click))
6133 Lisp_Object window, posn;
6135 window = POSN_WINDOW (EVENT_START (key));
6136 posn = POSN_BUFFER_POSN (EVENT_START (key));
6137 if (CONSP (posn))
6139 /* We're looking at the second event of a
6140 sequence which we expanded before. Set
6141 last_real_key_start appropriately. */
6142 if (t > 0)
6143 last_real_key_start = t - 1;
6146 /* Key sequences beginning with mouse clicks are
6147 read using the keymaps in the buffer clicked on,
6148 not the current buffer. If we're at the
6149 beginning of a key sequence, switch buffers. */
6150 if (last_real_key_start == 0
6151 && WINDOWP (window)
6152 && BUFFERP (XWINDOW (window)->buffer)
6153 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
6155 keybuf[t] = key;
6156 mock_input = t + 1;
6158 /* Arrange to go back to the original buffer once we're
6159 done reading the key sequence. Note that we can't
6160 use save_excursion_{save,restore} here, because they
6161 save point as well as the current buffer; we don't
6162 want to save point, because redisplay may change it,
6163 to accommodate a Fset_window_start or something. We
6164 don't want to do this at the top of the function,
6165 because we may get input from a subprocess which
6166 wants to change the selected window and stuff (say,
6167 emacsclient). */
6168 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
6170 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
6171 orig_local_map = get_local_map (PT, current_buffer);
6172 goto replay_sequence;
6174 /* For a mouse click, get the local text-property keymap
6175 of the place clicked on, rather than point. */
6176 if (last_real_key_start == 0 && CONSP (XCONS (key)->cdr)
6177 && ! localized_local_map)
6179 Lisp_Object map_here, start, pos;
6181 localized_local_map = 1;
6182 start = EVENT_START (key);
6183 if (CONSP (start) && CONSP (XCONS (start)->cdr))
6185 pos = POSN_BUFFER_POSN (start);
6186 if (INTEGERP (pos)
6187 && XINT (pos) >= BEG && XINT (pos) <= Z)
6189 map_here = get_local_map (XINT (pos), current_buffer);
6190 if (!EQ (map_here, orig_local_map))
6192 orig_local_map = map_here;
6193 keybuf[t] = key;
6194 mock_input = t + 1;
6196 goto replay_sequence;
6202 /* Expand mode-line and scroll-bar events into two events:
6203 use posn as a fake prefix key. */
6204 if (SYMBOLP (posn))
6206 if (t + 1 >= bufsize)
6207 error ("Key sequence too long");
6208 keybuf[t] = posn;
6209 keybuf[t+1] = key;
6210 mock_input = t + 2;
6212 /* Zap the position in key, so we know that we've
6213 expanded it, and don't try to do so again. */
6214 POSN_BUFFER_POSN (EVENT_START (key))
6215 = Fcons (posn, Qnil);
6216 goto replay_key;
6219 else if (EQ (kind, Qswitch_frame))
6221 /* If we're at the beginning of a key sequence, and the caller
6222 says it's okay, go ahead and return this event. If we're
6223 in the midst of a key sequence, delay it until the end. */
6224 if (t > 0 || !can_return_switch_frame)
6226 delayed_switch_frame = key;
6227 goto replay_key;
6230 else if (CONSP (XCONS (key)->cdr)
6231 && CONSP (EVENT_START (key))
6232 && CONSP (XCONS (EVENT_START (key))->cdr))
6234 Lisp_Object posn;
6236 posn = POSN_BUFFER_POSN (EVENT_START (key));
6237 /* Handle menu-bar events:
6238 insert the dummy prefix event `menu-bar'. */
6239 if (EQ (posn, Qmenu_bar))
6241 if (t + 1 >= bufsize)
6242 error ("Key sequence too long");
6243 keybuf[t] = posn;
6244 keybuf[t+1] = key;
6246 /* Zap the position in key, so we know that we've
6247 expanded it, and don't try to do so again. */
6248 POSN_BUFFER_POSN (EVENT_START (key))
6249 = Fcons (posn, Qnil);
6251 mock_input = t + 2;
6252 goto replay_sequence;
6254 else if (CONSP (posn))
6256 /* We're looking at the second event of a
6257 sequence which we expanded before. Set
6258 last_real_key_start appropriately. */
6259 if (last_real_key_start == t && t > 0)
6260 last_real_key_start = t - 1;
6265 /* We have finally decided that KEY is something we might want
6266 to look up. */
6267 first_binding = (follow_key (key,
6268 nmaps - first_binding,
6269 submaps + first_binding,
6270 defs + first_binding,
6271 submaps + first_binding)
6272 + first_binding);
6274 /* If KEY wasn't bound, we'll try some fallbacks. */
6275 if (first_binding >= nmaps)
6277 Lisp_Object head;
6279 head = EVENT_HEAD (key);
6280 if (help_char_p (head) && t > 0)
6282 read_key_sequence_cmd = Vprefix_help_command;
6283 keybuf[t++] = key;
6284 last_nonmenu_event = key;
6285 /* The Microsoft C compiler can't handle the goto that
6286 would go here. */
6287 dummyflag = 1;
6288 break;
6291 if (SYMBOLP (head))
6293 Lisp_Object breakdown;
6294 int modifiers;
6296 breakdown = parse_modifiers (head);
6297 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
6298 /* Attempt to reduce an unbound mouse event to a simpler
6299 event that is bound:
6300 Drags reduce to clicks.
6301 Double-clicks reduce to clicks.
6302 Triple-clicks reduce to double-clicks, then to clicks.
6303 Down-clicks are eliminated.
6304 Double-downs reduce to downs, then are eliminated.
6305 Triple-downs reduce to double-downs, then to downs,
6306 then are eliminated. */
6307 if (modifiers & (down_modifier | drag_modifier
6308 | double_modifier | triple_modifier))
6310 while (modifiers & (down_modifier | drag_modifier
6311 | double_modifier | triple_modifier))
6313 Lisp_Object new_head, new_click;
6314 if (modifiers & triple_modifier)
6315 modifiers ^= (double_modifier | triple_modifier);
6316 else if (modifiers & double_modifier)
6317 modifiers &= ~double_modifier;
6318 else if (modifiers & drag_modifier)
6319 modifiers &= ~drag_modifier;
6320 else
6322 /* Dispose of this `down' event by simply jumping
6323 back to replay_key, to get another event.
6325 Note that if this event came from mock input,
6326 then just jumping back to replay_key will just
6327 hand it to us again. So we have to wipe out any
6328 mock input.
6330 We could delete keybuf[t] and shift everything
6331 after that to the left by one spot, but we'd also
6332 have to fix up any variable that points into
6333 keybuf, and shifting isn't really necessary
6334 anyway.
6336 Adding prefixes for non-textual mouse clicks
6337 creates two characters of mock input, and both
6338 must be thrown away. If we're only looking at
6339 the prefix now, we can just jump back to
6340 replay_key. On the other hand, if we've already
6341 processed the prefix, and now the actual click
6342 itself is giving us trouble, then we've lost the
6343 state of the keymaps we want to backtrack to, and
6344 we need to replay the whole sequence to rebuild
6347 Beyond that, only function key expansion could
6348 create more than two keys, but that should never
6349 generate mouse events, so it's okay to zero
6350 mock_input in that case too.
6352 Isn't this just the most wonderful code ever? */
6353 if (t == last_real_key_start)
6355 mock_input = 0;
6356 goto replay_key;
6358 else
6360 mock_input = last_real_key_start;
6361 goto replay_sequence;
6365 new_head
6366 = apply_modifiers (modifiers, XCONS (breakdown)->car);
6367 new_click
6368 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
6370 /* Look for a binding for this new key. follow_key
6371 promises that it didn't munge submaps the
6372 last time we called it, since key was unbound. */
6373 first_binding
6374 = (follow_key (new_click,
6375 nmaps - local_first_binding,
6376 submaps + local_first_binding,
6377 defs + local_first_binding,
6378 submaps + local_first_binding)
6379 + local_first_binding);
6381 /* If that click is bound, go for it. */
6382 if (first_binding < nmaps)
6384 key = new_click;
6385 break;
6387 /* Otherwise, we'll leave key set to the drag event. */
6393 keybuf[t++] = key;
6394 /* Normally, last_nonmenu_event gets the previous key we read.
6395 But when a mouse popup menu is being used,
6396 we don't update last_nonmenu_event; it continues to hold the mouse
6397 event that preceded the first level of menu. */
6398 if (!used_mouse_menu)
6399 last_nonmenu_event = key;
6401 /* Record what part of this_command_keys is the current key sequence. */
6402 this_single_command_key_start = this_command_key_count - t;
6404 prev_fkey_map = fkey_map;
6405 prev_fkey_start = fkey_start;
6406 prev_fkey_end = fkey_end;
6408 prev_keytran_map = keytran_map;
6409 prev_keytran_start = keytran_start;
6410 prev_keytran_end = keytran_end;
6412 /* If the sequence is unbound, see if we can hang a function key
6413 off the end of it. We only want to scan real keyboard input
6414 for function key sequences, so if mock_input says that we're
6415 re-reading old events, don't examine it. */
6416 if (first_binding >= nmaps
6417 && t >= mock_input)
6419 Lisp_Object fkey_next;
6421 /* Continue scan from fkey_end until we find a bound suffix.
6422 If we fail, increment fkey_start
6423 and start fkey_end from there. */
6424 while (fkey_end < t)
6426 Lisp_Object key;
6428 key = keybuf[fkey_end++];
6429 /* Look up meta-characters by prefixing them
6430 with meta_prefix_char. I hate this. */
6431 if (INTEGERP (key) && XINT (key) & meta_modifier)
6433 fkey_next
6434 = get_keymap_1
6435 (get_keyelt
6436 (access_keymap (fkey_map, meta_prefix_char, 1, 0)),
6437 0, 1);
6438 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
6440 else
6441 fkey_next = fkey_map;
6443 fkey_next
6444 = get_keyelt (access_keymap (fkey_next, key, 1, 0));
6446 #if 0 /* I didn't turn this on, because it might cause trouble
6447 for the mapping of return into C-m and tab into C-i. */
6448 /* Optionally don't map function keys into other things.
6449 This enables the user to redefine kp- keys easily. */
6450 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
6451 fkey_next = Qnil;
6452 #endif
6454 /* If the function key map gives a function, not an
6455 array, then call the function with no args and use
6456 its value instead. */
6457 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
6458 && fkey_end == t)
6460 struct gcpro gcpro1, gcpro2, gcpro3;
6461 Lisp_Object tem;
6462 tem = fkey_next;
6464 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
6465 fkey_next = call1 (fkey_next, prompt);
6466 UNGCPRO;
6467 /* If the function returned something invalid,
6468 barf--don't ignore it.
6469 (To ignore it safely, we would need to gcpro a bunch of
6470 other variables.) */
6471 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
6472 error ("Function in key-translation-map returns invalid key sequence");
6475 function_key_possible = ! NILP (fkey_next);
6477 /* If keybuf[fkey_start..fkey_end] is bound in the
6478 function key map and it's a suffix of the current
6479 sequence (i.e. fkey_end == t), replace it with
6480 the binding and restart with fkey_start at the end. */
6481 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
6482 && fkey_end == t)
6484 int len = XFASTINT (Flength (fkey_next));
6486 t = fkey_start + len;
6487 if (t >= bufsize)
6488 error ("Key sequence too long");
6490 if (VECTORP (fkey_next))
6491 bcopy (XVECTOR (fkey_next)->contents,
6492 keybuf + fkey_start,
6493 (t - fkey_start) * sizeof (keybuf[0]));
6494 else if (STRINGP (fkey_next))
6496 int i;
6498 for (i = 0; i < len; i++)
6499 XSETFASTINT (keybuf[fkey_start + i],
6500 XSTRING (fkey_next)->data[i]);
6503 mock_input = t;
6504 fkey_start = fkey_end = t;
6505 fkey_map = Vfunction_key_map;
6507 /* Do pass the results through key-translation-map. */
6508 keytran_start = keytran_end = 0;
6509 keytran_map = Vkey_translation_map;
6511 goto replay_sequence;
6514 fkey_map = get_keymap_1 (fkey_next, 0, 1);
6516 /* If we no longer have a bound suffix, try a new positions for
6517 fkey_start. */
6518 if (NILP (fkey_map))
6520 fkey_end = ++fkey_start;
6521 fkey_map = Vfunction_key_map;
6522 function_key_possible = 0;
6527 /* Look for this sequence in key-translation-map. */
6529 Lisp_Object keytran_next;
6531 /* Scan from keytran_end until we find a bound suffix. */
6532 while (keytran_end < t)
6534 Lisp_Object key;
6536 key = keybuf[keytran_end++];
6537 /* Look up meta-characters by prefixing them
6538 with meta_prefix_char. I hate this. */
6539 if (INTEGERP (key) && XINT (key) & meta_modifier)
6541 keytran_next
6542 = get_keymap_1
6543 (get_keyelt
6544 (access_keymap (keytran_map, meta_prefix_char, 1, 0)),
6545 0, 1);
6546 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
6548 else
6549 keytran_next = keytran_map;
6551 keytran_next
6552 = get_keyelt (access_keymap (keytran_next, key, 1, 0));
6554 /* If the key translation map gives a function, not an
6555 array, then call the function with no args and use
6556 its value instead. */
6557 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
6558 && keytran_end == t)
6560 struct gcpro gcpro1, gcpro2, gcpro3;
6561 Lisp_Object tem;
6562 tem = keytran_next;
6564 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
6565 keytran_next = call1 (keytran_next, prompt);
6566 UNGCPRO;
6567 /* If the function returned something invalid,
6568 barf--don't ignore it.
6569 (To ignore it safely, we would need to gcpro a bunch of
6570 other variables.) */
6571 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
6572 error ("Function in key-translation-map returns invalid key sequence");
6575 key_translation_possible = ! NILP (keytran_next);
6577 /* If keybuf[keytran_start..keytran_end] is bound in the
6578 key translation map and it's a suffix of the current
6579 sequence (i.e. keytran_end == t), replace it with
6580 the binding and restart with keytran_start at the end. */
6581 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
6582 && keytran_end == t)
6584 int len = XFASTINT (Flength (keytran_next));
6586 t = keytran_start + len;
6587 if (t >= bufsize)
6588 error ("Key sequence too long");
6590 if (VECTORP (keytran_next))
6591 bcopy (XVECTOR (keytran_next)->contents,
6592 keybuf + keytran_start,
6593 (t - keytran_start) * sizeof (keybuf[0]));
6594 else if (STRINGP (keytran_next))
6596 int i;
6598 for (i = 0; i < len; i++)
6599 XSETFASTINT (keybuf[keytran_start + i],
6600 XSTRING (keytran_next)->data[i]);
6603 mock_input = t;
6604 keytran_start = keytran_end = t;
6605 keytran_map = Vkey_translation_map;
6607 /* Don't pass the results of key-translation-map
6608 through function-key-map. */
6609 fkey_start = fkey_end = t;
6610 fkey_map = Vkey_translation_map;
6612 goto replay_sequence;
6615 keytran_map = get_keymap_1 (keytran_next, 0, 1);
6617 /* If we no longer have a bound suffix, try a new positions for
6618 keytran_start. */
6619 if (NILP (keytran_map))
6621 keytran_end = ++keytran_start;
6622 keytran_map = Vkey_translation_map;
6623 key_translation_possible = 0;
6628 /* If KEY is not defined in any of the keymaps,
6629 and cannot be part of a function key or translation,
6630 and is an upper case letter
6631 use the corresponding lower-case letter instead. */
6632 if (first_binding == nmaps && ! function_key_possible
6633 && ! key_translation_possible
6634 && INTEGERP (key)
6635 && ((((XINT (key) & 0x3ffff)
6636 < XSTRING (current_buffer->downcase_table)->size)
6637 && UPPERCASEP (XINT (key) & 0x3ffff))
6638 || (XINT (key) & shift_modifier)))
6640 Lisp_Object new_key;
6642 original_uppercase = key;
6643 original_uppercase_position = t - 1;
6645 if (XINT (key) & shift_modifier)
6646 XSETINT (new_key, XINT (key) & ~shift_modifier);
6647 else
6648 XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
6649 | (XINT (key) & ~0x3ffff)));
6651 /* We have to do this unconditionally, regardless of whether
6652 the lower-case char is defined in the keymaps, because they
6653 might get translated through function-key-map. */
6654 keybuf[t - 1] = new_key;
6655 mock_input = t;
6657 fkey_map = prev_fkey_map;
6658 fkey_start = prev_fkey_start;
6659 fkey_end = prev_fkey_end;
6661 keytran_map = prev_keytran_map;
6662 keytran_start = prev_keytran_start;
6663 keytran_end = prev_keytran_end;
6665 goto replay_sequence;
6667 /* If KEY is not defined in any of the keymaps,
6668 and cannot be part of a function key or translation,
6669 and is a shifted function key,
6670 use the corresponding unshifted function key instead. */
6671 if (first_binding == nmaps && ! function_key_possible
6672 && ! key_translation_possible
6673 && SYMBOLP (key))
6675 Lisp_Object breakdown;
6676 int modifiers;
6678 breakdown = parse_modifiers (key);
6679 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
6680 if (modifiers & shift_modifier)
6682 Lisp_Object new_key;
6684 original_uppercase = key;
6685 original_uppercase_position = t - 1;
6687 modifiers &= ~shift_modifier;
6688 new_key = apply_modifiers (modifiers,
6689 XCONS (breakdown)->car);
6691 keybuf[t - 1] = new_key;
6692 mock_input = t;
6694 fkey_map = prev_fkey_map;
6695 fkey_start = prev_fkey_start;
6696 fkey_end = prev_fkey_end;
6698 keytran_map = prev_keytran_map;
6699 keytran_start = prev_keytran_start;
6700 keytran_end = prev_keytran_end;
6702 goto replay_sequence;
6707 if (!dummyflag)
6708 read_key_sequence_cmd = (first_binding < nmaps
6709 ? defs[first_binding]
6710 : Qnil);
6712 unread_switch_frame = delayed_switch_frame;
6713 unbind_to (count, Qnil);
6715 /* Don't downcase the last character if the caller says don't.
6716 Don't downcase it if the result is undefined, either. */
6717 if ((dont_downcase_last || first_binding >= nmaps)
6718 && t - 1 == original_uppercase_position)
6719 keybuf[t - 1] = original_uppercase;
6721 /* Occasionally we fabricate events, perhaps by expanding something
6722 according to function-key-map, or by adding a prefix symbol to a
6723 mouse click in the scroll bar or modeline. In this cases, return
6724 the entire generated key sequence, even if we hit an unbound
6725 prefix or a definition before the end. This means that you will
6726 be able to push back the event properly, and also means that
6727 read-key-sequence will always return a logical unit.
6729 Better ideas? */
6730 for (; t < mock_input; t++)
6732 if (echo_keystrokes)
6733 echo_char (keybuf[t]);
6734 add_command_key (keybuf[t]);
6737 return t;
6740 #if 0 /* This doc string is too long for some compilers.
6741 This commented-out definition serves for DOC. */
6742 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
6743 "Read a sequence of keystrokes and return as a string or vector.\n\
6744 The sequence is sufficient to specify a non-prefix command in the\n\
6745 current local and global maps.\n\
6747 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
6748 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
6749 as a continuation of the previous key.\n\
6751 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
6752 convert the last event to lower case. (Normally any upper case event\n\
6753 is converted to lower case if the original event is undefined and the lower\n\
6754 case equivalent is defined.) A non-nil value is appropriate for reading\n\
6755 a key sequence to be defined.\n\
6757 A C-g typed while in this function is treated like any other character,\n\
6758 and `quit-flag' is not set.\n\
6760 If the key sequence starts with a mouse click, then the sequence is read\n\
6761 using the keymaps of the buffer of the window clicked in, not the buffer\n\
6762 of the selected window as normal.\n\
6763 ""\n\
6764 `read-key-sequence' drops unbound button-down events, since you normally\n\
6765 only care about the click or drag events which follow them. If a drag\n\
6766 or multi-click event is unbound, but the corresponding click event would\n\
6767 be bound, `read-key-sequence' turns the event into a click event at the\n\
6768 drag's starting position. This means that you don't have to distinguish\n\
6769 between click and drag, double, or triple events unless you want to.\n\
6771 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
6772 lines separating windows, and scroll bars with imaginary keys\n\
6773 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
6775 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
6776 function will process a switch-frame event if the user switches frames\n\
6777 before typing anything. If the user switches frames in the middle of a\n\
6778 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
6779 is nil, then the event will be put off until after the current key sequence.\n\
6781 `read-key-sequence' checks `function-key-map' for function key\n\
6782 sequences, where they wouldn't conflict with ordinary bindings. See\n\
6783 `function-key-map' for more details.")
6784 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
6785 #endif
6787 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
6789 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
6790 Lisp_Object prompt, continue_echo, dont_downcase_last;
6791 Lisp_Object can_return_switch_frame;
6793 Lisp_Object keybuf[30];
6794 register int i;
6795 struct gcpro gcpro1, gcpro2;
6797 if (!NILP (prompt))
6798 CHECK_STRING (prompt, 0);
6799 QUIT;
6801 bzero (keybuf, sizeof keybuf);
6802 GCPRO1 (keybuf[0]);
6803 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
6805 if (NILP (continue_echo))
6807 this_command_key_count = 0;
6808 this_single_command_key_start = 0;
6811 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
6812 prompt, ! NILP (dont_downcase_last),
6813 ! NILP (can_return_switch_frame));
6815 if (i == -1)
6817 Vquit_flag = Qt;
6818 QUIT;
6820 UNGCPRO;
6821 return make_event_array (i, keybuf);
6824 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
6825 "Execute CMD as an editor command.\n\
6826 CMD must be a symbol that satisfies the `commandp' predicate.\n\
6827 Optional second arg RECORD-FLAG non-nil\n\
6828 means unconditionally put this command in `command-history'.\n\
6829 Otherwise, that is done only if an arg is read using the minibuffer.\n\
6830 The argument KEYS specifies the value to use instead of (this-command-keys)\n\
6831 when reading the arguments; if it is nil, (this-command-keys) is used.\n\
6832 The argument SPECIAL, if non-nil, means that this command is executing\n\
6833 a special event, so ignore the prefix argument and don't clear it.")
6834 (cmd, record_flag, keys, special)
6835 Lisp_Object cmd, record_flag, keys, special;
6837 register Lisp_Object final;
6838 register Lisp_Object tem;
6839 Lisp_Object prefixarg;
6840 struct backtrace backtrace;
6841 extern int debug_on_next_call;
6843 debug_on_next_call = 0;
6845 if (NILP (special))
6847 prefixarg = current_kboard->Vprefix_arg;
6848 Vcurrent_prefix_arg = prefixarg;
6849 current_kboard->Vprefix_arg = Qnil;
6851 else
6852 prefixarg = Qnil;
6854 if (SYMBOLP (cmd))
6856 tem = Fget (cmd, Qdisabled);
6857 if (!NILP (tem) && !NILP (Vrun_hooks))
6859 tem = Fsymbol_value (Qdisabled_command_hook);
6860 if (!NILP (tem))
6861 return call1 (Vrun_hooks, Qdisabled_command_hook);
6865 while (1)
6867 final = Findirect_function (cmd);
6869 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
6871 struct gcpro gcpro1, gcpro2;
6873 GCPRO2 (cmd, prefixarg);
6874 do_autoload (final, cmd);
6875 UNGCPRO;
6877 else
6878 break;
6881 if (STRINGP (final) || VECTORP (final))
6883 /* If requested, place the macro in the command history. For
6884 other sorts of commands, call-interactively takes care of
6885 this. */
6886 if (!NILP (record_flag))
6887 Vcommand_history
6888 = Fcons (Fcons (Qexecute_kbd_macro,
6889 Fcons (final, Fcons (prefixarg, Qnil))),
6890 Vcommand_history);
6892 return Fexecute_kbd_macro (final, prefixarg);
6894 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
6896 backtrace.next = backtrace_list;
6897 backtrace_list = &backtrace;
6898 backtrace.function = &Qcall_interactively;
6899 backtrace.args = &cmd;
6900 backtrace.nargs = 1;
6901 backtrace.evalargs = 0;
6903 tem = Fcall_interactively (cmd, record_flag, keys);
6905 backtrace_list = backtrace.next;
6906 return tem;
6908 return Qnil;
6911 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
6912 1, 1, "P",
6913 "Read function name, then read its arguments and call it.")
6914 (prefixarg)
6915 Lisp_Object prefixarg;
6917 Lisp_Object function;
6918 char buf[40];
6919 Lisp_Object saved_keys;
6920 Lisp_Object bindings, value;
6921 struct gcpro gcpro1, gcpro2;
6923 saved_keys = Fvector (this_command_key_count,
6924 XVECTOR (this_command_keys)->contents);
6925 buf[0] = 0;
6926 GCPRO2 (saved_keys, prefixarg);
6928 if (EQ (prefixarg, Qminus))
6929 strcpy (buf, "- ");
6930 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
6931 strcpy (buf, "C-u ");
6932 else if (CONSP (prefixarg) && INTEGERP (XCONS (prefixarg)->car))
6934 if (sizeof (int) == sizeof (EMACS_INT))
6935 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
6936 else if (sizeof (long) == sizeof (EMACS_INT))
6937 sprintf (buf, "%ld ", XINT (XCONS (prefixarg)->car));
6938 else
6939 abort ();
6941 else if (INTEGERP (prefixarg))
6943 if (sizeof (int) == sizeof (EMACS_INT))
6944 sprintf (buf, "%d ", XINT (prefixarg));
6945 else if (sizeof (long) == sizeof (EMACS_INT))
6946 sprintf (buf, "%ld ", XINT (prefixarg));
6947 else
6948 abort ();
6951 /* This isn't strictly correct if execute-extended-command
6952 is bound to anything else. Perhaps it should use
6953 this_command_keys? */
6954 strcat (buf, "M-x ");
6956 /* Prompt with buf, and then read a string, completing from and
6957 restricting to the set of all defined commands. Don't provide
6958 any initial input. Save the command read on the extended-command
6959 history list. */
6960 function = Fcompleting_read (build_string (buf),
6961 Vobarray, Qcommandp,
6962 Qt, Qnil, Qextended_command_history);
6964 if (STRINGP (function) && XSTRING (function)->size == 0)
6965 error ("No command name given");
6967 /* Set this_command_keys to the concatenation of saved_keys and
6968 function, followed by a RET. */
6970 struct Lisp_String *str;
6971 Lisp_Object *keys;
6972 int i;
6973 Lisp_Object tem;
6975 this_command_key_count = 0;
6976 this_single_command_key_start = 0;
6978 keys = XVECTOR (saved_keys)->contents;
6979 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
6980 add_command_key (keys[i]);
6982 str = XSTRING (function);
6983 for (i = 0; i < str->size; i++)
6985 XSETFASTINT (tem, str->data[i]);
6986 add_command_key (tem);
6989 XSETFASTINT (tem, '\015');
6990 add_command_key (tem);
6993 UNGCPRO;
6995 function = Fintern (function, Qnil);
6996 current_kboard->Vprefix_arg = prefixarg;
6997 this_command = function;
6999 /* If enabled, show which key runs this command. */
7000 if (!NILP (Vsuggest_key_bindings)
7001 && NILP (Vexecuting_macro)
7002 && SYMBOLP (function))
7003 bindings = Fwhere_is_internal (function, Voverriding_local_map,
7004 Qt, Qnil);
7005 else
7006 bindings = Qnil;
7008 value = Qnil;
7009 GCPRO2 (bindings, value);
7010 value = Fcommand_execute (function, Qt, Qnil, Qnil);
7012 /* If the command has a key binding, print it now. */
7013 if (!NILP (bindings))
7015 /* But first wait, and skip the message if there is input. */
7016 if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
7017 ? Vsuggest_key_bindings : make_number (2)),
7018 Qnil, Qnil)))
7020 Lisp_Object binding;
7021 char *newmessage;
7022 char *oldmessage = echo_area_glyphs;
7023 int oldmessage_len = echo_area_glyphs_length;
7025 binding = Fkey_description (bindings);
7027 newmessage
7028 = (char *) alloca (XSYMBOL (function)->name->size
7029 + XSTRING (binding)->size
7030 + 100);
7031 sprintf (newmessage, "You can run the command `%s' by typing %s",
7032 XSYMBOL (function)->name->data,
7033 XSTRING (binding)->data);
7034 message1_nolog (newmessage);
7035 if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
7036 ? Vsuggest_key_bindings : make_number (2)),
7037 Qnil, Qnil)))
7038 message2_nolog (oldmessage, oldmessage_len);
7042 RETURN_UNGCPRO (value);
7045 /* Find the set of keymaps now active.
7046 Store into *MAPS_P a vector holding the various maps
7047 and return the number of them. The vector was malloc'd
7048 and the caller should free it. */
7051 current_active_maps (maps_p)
7052 Lisp_Object **maps_p;
7054 Lisp_Object *tmaps, *maps;
7055 int nmaps;
7057 /* Should overriding-terminal-local-map and overriding-local-map apply? */
7058 if (!NILP (Voverriding_local_map_menu_flag))
7060 /* Yes, use them (if non-nil) as well as the global map. */
7061 maps = (Lisp_Object *) xmalloc (3 * sizeof (maps[0]));
7062 nmaps = 0;
7063 if (!NILP (current_kboard->Voverriding_terminal_local_map))
7064 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
7065 if (!NILP (Voverriding_local_map))
7066 maps[nmaps++] = Voverriding_local_map;
7068 else
7070 /* No, so use major and minor mode keymaps. */
7071 nmaps = current_minor_maps (NULL, &tmaps);
7072 maps = (Lisp_Object *) xmalloc ((nmaps + 2) * sizeof (maps[0]));
7073 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
7074 #ifdef USE_TEXT_PROPERTIES
7075 maps[nmaps++] = get_local_map (PT, current_buffer);
7076 #else
7077 maps[nmaps++] = current_buffer->keymap;
7078 #endif
7080 maps[nmaps++] = current_global_map;
7082 *maps_p = maps;
7083 return nmaps;
7086 /* Return nonzero if input events are pending. */
7088 detect_input_pending ()
7090 if (!input_pending)
7091 get_input_pending (&input_pending, 0);
7093 return input_pending;
7096 /* Return nonzero if input events are pending, and run any pending timers. */
7098 detect_input_pending_run_timers (do_display)
7099 int do_display;
7101 int old_timers_run = timers_run;
7103 if (!input_pending)
7104 get_input_pending (&input_pending, 1);
7106 if (old_timers_run != timers_run && do_display)
7107 redisplay_preserve_echo_area ();
7109 return input_pending;
7112 /* This is called in some cases before a possible quit.
7113 It cases the next call to detect_input_pending to recompute input_pending.
7114 So calling this function unnecessarily can't do any harm. */
7115 clear_input_pending ()
7117 input_pending = 0;
7120 /* Return nonzero if there are pending requeued events.
7121 This isn't used yet. The hope is to make wait_reading_process_input
7122 call it, and return return if it runs Lisp code that unreads something.
7123 The problem is, kbd_buffer_get_event needs to be fixed to know what
7124 to do in that case. It isn't trivial. */
7126 requeued_events_pending_p ()
7128 return (!NILP (Vunread_command_events) || unread_command_char != -1);
7132 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
7133 "T if command input is currently available with no waiting.\n\
7134 Actually, the value is nil only if we can be sure that no input is available.")
7137 if (!NILP (Vunread_command_events) || unread_command_char != -1)
7138 return (Qt);
7140 get_input_pending (&input_pending, 1);
7141 return input_pending > 0 ? Qt : Qnil;
7144 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
7145 "Return vector of last 100 events, not counting those from keyboard macros.")
7148 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
7149 Lisp_Object val;
7151 if (total_keys < NUM_RECENT_KEYS)
7152 return Fvector (total_keys, keys);
7153 else
7155 val = Fvector (NUM_RECENT_KEYS, keys);
7156 bcopy (keys + recent_keys_index,
7157 XVECTOR (val)->contents,
7158 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
7159 bcopy (keys,
7160 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
7161 recent_keys_index * sizeof (Lisp_Object));
7162 return val;
7166 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
7167 "Return the key sequence that invoked this command.\n\
7168 The value is a string or a vector.")
7171 return make_event_array (this_command_key_count,
7172 XVECTOR (this_command_keys)->contents);
7175 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
7176 Sthis_single_command_keys, 0, 0, 0,
7177 "Return the key sequence that invoked this command.\n\
7178 Unlike `this-command-keys', this function's value\n\
7179 does not include prefix arguments.\n\
7180 The value is a string or a vector.")
7183 return make_event_array (this_command_key_count
7184 - this_single_command_key_start,
7185 (XVECTOR (this_command_keys)->contents
7186 + this_single_command_key_start));
7189 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
7190 Sreset_this_command_lengths, 0, 0, 0,
7191 "Used for complicated reasons in `universal-argument-other-key'.\n\
7193 `universal-argument-other-key' rereads the event just typed.\n\
7194 It then gets translated through `function-key-map'.\n\
7195 The translated event gets included in the echo area and in\n\
7196 the value of `this-command-keys' in addition to the raw original event.\n\
7197 That is not right.\n\
7199 Calling this function directs the translated event to replace\n\
7200 the original event, so that only one version of the event actually\n\
7201 appears in the echo area and in the value of `this-command-keys.'.")
7204 before_command_restore_flag = 1;
7205 before_command_key_count_1 = before_command_key_count;
7206 before_command_echo_length_1 = before_command_echo_length;
7209 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
7210 "Return the current depth in recursive edits.")
7213 Lisp_Object temp;
7214 XSETFASTINT (temp, command_loop_level + minibuf_level);
7215 return temp;
7218 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
7219 "FOpen dribble file: ",
7220 "Start writing all keyboard characters to a dribble file called FILE.\n\
7221 If FILE is nil, close any open dribble file.")
7222 (file)
7223 Lisp_Object file;
7225 if (dribble)
7227 fclose (dribble);
7228 dribble = 0;
7230 if (!NILP (file))
7232 file = Fexpand_file_name (file, Qnil);
7233 dribble = fopen (XSTRING (file)->data, "w");
7234 if (dribble == 0)
7235 report_file_error ("Opening dribble", Fcons (file, Qnil));
7237 return Qnil;
7240 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
7241 "Discard the contents of the terminal input buffer.\n\
7242 Also cancel any kbd macro being defined.")
7245 current_kboard->defining_kbd_macro = Qnil;
7246 update_mode_lines++;
7248 Vunread_command_events = Qnil;
7249 unread_command_char = -1;
7251 discard_tty_input ();
7253 /* Without the cast, GCC complains that this assignment loses the
7254 volatile qualifier of kbd_store_ptr. Is there anything wrong
7255 with that? */
7256 kbd_fetch_ptr = (struct input_event *) kbd_store_ptr;
7257 Ffillarray (kbd_buffer_frame_or_window, Qnil);
7258 input_pending = 0;
7260 return Qnil;
7263 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
7264 "Stop Emacs and return to superior process. You can resume later.\n\
7265 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
7266 control, run a subshell instead.\n\n\
7267 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
7268 to be read as terminal input by Emacs's parent, after suspension.\n\
7270 Before suspending, run the normal hook `suspend-hook'.\n\
7271 After resumption run the normal hook `suspend-resume-hook'.\n\
7273 Some operating systems cannot stop the Emacs process and resume it later.\n\
7274 On such systems, Emacs starts a subshell instead of suspending.")
7275 (stuffstring)
7276 Lisp_Object stuffstring;
7278 Lisp_Object tem;
7279 int count = specpdl_ptr - specpdl;
7280 int old_height, old_width;
7281 int width, height;
7282 struct gcpro gcpro1, gcpro2;
7283 extern init_sys_modes ();
7285 if (!NILP (stuffstring))
7286 CHECK_STRING (stuffstring, 0);
7288 /* Run the functions in suspend-hook. */
7289 if (!NILP (Vrun_hooks))
7290 call1 (Vrun_hooks, intern ("suspend-hook"));
7292 GCPRO1 (stuffstring);
7293 get_frame_size (&old_width, &old_height);
7294 reset_sys_modes ();
7295 /* sys_suspend can get an error if it tries to fork a subshell
7296 and the system resources aren't available for that. */
7297 record_unwind_protect (init_sys_modes, 0);
7298 stuff_buffered_input (stuffstring);
7299 if (cannot_suspend)
7300 sys_subshell ();
7301 else
7302 sys_suspend ();
7303 unbind_to (count, Qnil);
7305 /* Check if terminal/window size has changed.
7306 Note that this is not useful when we are running directly
7307 with a window system; but suspend should be disabled in that case. */
7308 get_frame_size (&width, &height);
7309 if (width != old_width || height != old_height)
7310 change_frame_size (selected_frame, height, width, 0, 0);
7312 /* Run suspend-resume-hook. */
7313 if (!NILP (Vrun_hooks))
7314 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
7316 UNGCPRO;
7317 return Qnil;
7320 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
7321 Then in any case stuff anything Emacs has read ahead and not used. */
7323 stuff_buffered_input (stuffstring)
7324 Lisp_Object stuffstring;
7326 /* stuff_char works only in BSD, versions 4.2 and up. */
7327 #ifdef BSD_SYSTEM
7328 #ifndef BSD4_1
7329 register unsigned char *p;
7331 if (STRINGP (stuffstring))
7333 register int count;
7335 p = XSTRING (stuffstring)->data;
7336 count = XSTRING (stuffstring)->size;
7337 while (count-- > 0)
7338 stuff_char (*p++);
7339 stuff_char ('\n');
7341 /* Anything we have read ahead, put back for the shell to read. */
7342 /* ?? What should this do when we have multiple keyboards??
7343 Should we ignore anything that was typed in at the "wrong" kboard? */
7344 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
7346 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
7347 kbd_fetch_ptr = kbd_buffer;
7348 if (kbd_fetch_ptr->kind == ascii_keystroke)
7349 stuff_char (kbd_fetch_ptr->code);
7350 kbd_fetch_ptr->kind = no_event;
7351 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
7352 - kbd_buffer]
7353 = Qnil);
7355 input_pending = 0;
7356 #endif
7357 #endif /* BSD_SYSTEM and not BSD4_1 */
7360 set_waiting_for_input (time_to_clear)
7361 EMACS_TIME *time_to_clear;
7363 input_available_clear_time = time_to_clear;
7365 /* Tell interrupt_signal to throw back to read_char, */
7366 waiting_for_input = 1;
7368 /* If interrupt_signal was called before and buffered a C-g,
7369 make it run again now, to avoid timing error. */
7370 if (!NILP (Vquit_flag))
7371 quit_throw_to_read_char ();
7374 clear_waiting_for_input ()
7376 /* Tell interrupt_signal not to throw back to read_char, */
7377 waiting_for_input = 0;
7378 input_available_clear_time = 0;
7381 /* This routine is called at interrupt level in response to C-G.
7382 If interrupt_input, this is the handler for SIGINT.
7383 Otherwise, it is called from kbd_buffer_store_event,
7384 in handling SIGIO or SIGTINT.
7386 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
7387 immediately throw back to read_char.
7389 Otherwise it sets the Lisp variable quit-flag not-nil.
7390 This causes eval to throw, when it gets a chance.
7391 If quit-flag is already non-nil, it stops the job right away. */
7393 SIGTYPE
7394 interrupt_signal (signalnum) /* If we don't have an argument, */
7395 int signalnum; /* some compilers complain in signal calls. */
7397 char c;
7398 /* Must preserve main program's value of errno. */
7399 int old_errno = errno;
7401 #if defined (USG) && !defined (POSIX_SIGNALS)
7402 if (!read_socket_hook && NILP (Vwindow_system))
7404 /* USG systems forget handlers when they are used;
7405 must reestablish each time */
7406 signal (SIGINT, interrupt_signal);
7407 signal (SIGQUIT, interrupt_signal);
7409 #endif /* USG */
7411 cancel_echoing ();
7413 if (!NILP (Vquit_flag) && FRAME_TERMCAP_P (selected_frame))
7415 fflush (stdout);
7416 reset_sys_modes ();
7417 sigfree ();
7418 #ifdef SIGTSTP /* Support possible in later USG versions */
7420 * On systems which can suspend the current process and return to the original
7421 * shell, this command causes the user to end up back at the shell.
7422 * The "Auto-save" and "Abort" questions are not asked until
7423 * the user elects to return to emacs, at which point he can save the current
7424 * job and either dump core or continue.
7426 sys_suspend ();
7427 #else
7428 #ifdef VMS
7429 if (sys_suspend () == -1)
7431 printf ("Not running as a subprocess;\n");
7432 printf ("you can continue or abort.\n");
7434 #else /* not VMS */
7435 /* Perhaps should really fork an inferior shell?
7436 But that would not provide any way to get back
7437 to the original shell, ever. */
7438 printf ("No support for stopping a process on this operating system;\n");
7439 printf ("you can continue or abort.\n");
7440 #endif /* not VMS */
7441 #endif /* not SIGTSTP */
7442 #ifdef MSDOS
7443 /* We must remain inside the screen area when the internal terminal
7444 is used. Note that [Enter] is not echoed by dos. */
7445 cursor_to (0, 0);
7446 #endif
7447 /* It doesn't work to autosave while GC is in progress;
7448 the code used for auto-saving doesn't cope with the mark bit. */
7449 if (!gc_in_progress)
7451 printf ("Auto-save? (y or n) ");
7452 fflush (stdout);
7453 if (((c = getchar ()) & ~040) == 'Y')
7455 Fdo_auto_save (Qt, Qnil);
7456 #ifdef MSDOS
7457 printf ("\r\nAuto-save done");
7458 #else /* not MSDOS */
7459 printf ("Auto-save done\n");
7460 #endif /* not MSDOS */
7462 while (c != '\n') c = getchar ();
7464 else
7466 /* During GC, it must be safe to reenable quitting again. */
7467 Vinhibit_quit = Qnil;
7468 #ifdef MSDOS
7469 printf ("\r\n");
7470 #endif /* not MSDOS */
7471 printf ("Garbage collection in progress; cannot auto-save now\r\n");
7472 printf ("but will instead do a real quit after garbage collection ends\r\n");
7473 fflush (stdout);
7476 #ifdef MSDOS
7477 printf ("\r\nAbort? (y or n) ");
7478 #else /* not MSDOS */
7479 #ifdef VMS
7480 printf ("Abort (and enter debugger)? (y or n) ");
7481 #else /* not VMS */
7482 printf ("Abort (and dump core)? (y or n) ");
7483 #endif /* not VMS */
7484 #endif /* not MSDOS */
7485 fflush (stdout);
7486 if (((c = getchar ()) & ~040) == 'Y')
7487 abort ();
7488 while (c != '\n') c = getchar ();
7489 #ifdef MSDOS
7490 printf ("\r\nContinuing...\r\n");
7491 #else /* not MSDOS */
7492 printf ("Continuing...\n");
7493 #endif /* not MSDOS */
7494 fflush (stdout);
7495 init_sys_modes ();
7497 else
7499 /* If executing a function that wants to be interrupted out of
7500 and the user has not deferred quitting by binding `inhibit-quit'
7501 then quit right away. */
7502 if (immediate_quit && NILP (Vinhibit_quit))
7504 immediate_quit = 0;
7505 sigfree ();
7506 Fsignal (Qquit, Qnil);
7508 else
7509 /* Else request quit when it's safe */
7510 Vquit_flag = Qt;
7513 if (waiting_for_input && !echoing)
7514 quit_throw_to_read_char ();
7516 errno = old_errno;
7519 /* Handle a C-g by making read_char return C-g. */
7521 quit_throw_to_read_char ()
7523 quit_error_check ();
7524 sigfree ();
7525 /* Prevent another signal from doing this before we finish. */
7526 clear_waiting_for_input ();
7527 input_pending = 0;
7529 Vunread_command_events = Qnil;
7530 unread_command_char = -1;
7532 #if 0 /* Currently, sit_for is called from read_char without turning
7533 off polling. And that can call set_waiting_for_input.
7534 It seems to be harmless. */
7535 #ifdef POLL_FOR_INPUT
7536 /* May be > 1 if in recursive minibuffer. */
7537 if (poll_suppress_count == 0)
7538 abort ();
7539 #endif
7540 #endif
7541 if (FRAMEP (internal_last_event_frame)
7542 && XFRAME (internal_last_event_frame) != selected_frame)
7543 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
7544 Qnil, 0);
7546 _longjmp (getcjmp, 1);
7549 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
7550 "Set mode of reading keyboard input.\n\
7551 First arg INTERRUPT non-nil means use input interrupts;\n\
7552 nil means use CBREAK mode.\n\
7553 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
7554 (no effect except in CBREAK mode).\n\
7555 Third arg META t means accept 8-bit input (for a Meta key).\n\
7556 META nil means ignore the top bit, on the assumption it is parity.\n\
7557 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
7558 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
7559 See also `current-input-mode'.")
7560 (interrupt, flow, meta, quit)
7561 Lisp_Object interrupt, flow, meta, quit;
7563 if (!NILP (quit)
7564 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
7565 error ("set-input-mode: QUIT must be an ASCII character");
7567 #ifdef POLL_FOR_INPUT
7568 stop_polling ();
7569 #endif
7571 #ifndef MSDOS
7572 /* this causes startup screen to be restored and messes with the mouse */
7573 reset_sys_modes ();
7574 #endif
7576 #ifdef SIGIO
7577 /* Note SIGIO has been undef'd if FIONREAD is missing. */
7578 if (read_socket_hook)
7580 /* When using X, don't give the user a real choice,
7581 because we haven't implemented the mechanisms to support it. */
7582 #ifdef NO_SOCK_SIGIO
7583 interrupt_input = 0;
7584 #else /* not NO_SOCK_SIGIO */
7585 interrupt_input = 1;
7586 #endif /* NO_SOCK_SIGIO */
7588 else
7589 interrupt_input = !NILP (interrupt);
7590 #else /* not SIGIO */
7591 interrupt_input = 0;
7592 #endif /* not SIGIO */
7594 /* Our VMS input only works by interrupts, as of now. */
7595 #ifdef VMS
7596 interrupt_input = 1;
7597 #endif
7599 flow_control = !NILP (flow);
7600 if (NILP (meta))
7601 meta_key = 0;
7602 else if (EQ (meta, Qt))
7603 meta_key = 1;
7604 else
7605 meta_key = 2;
7606 if (!NILP (quit))
7607 /* Don't let this value be out of range. */
7608 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
7610 #ifndef MSDOS
7611 init_sys_modes ();
7612 #endif
7614 #ifdef POLL_FOR_INPUT
7615 poll_suppress_count = 1;
7616 start_polling ();
7617 #endif
7618 return Qnil;
7621 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
7622 "Return information about the way Emacs currently reads keyboard input.\n\
7623 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
7624 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
7625 nil, Emacs is using CBREAK mode.\n\
7626 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
7627 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
7628 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
7629 META nil means ignoring the top bit, on the assumption it is parity.\n\
7630 META is neither t nor nil if accepting 8-bit input and using\n\
7631 all 8 bits as the character code.\n\
7632 QUIT is the character Emacs currently uses to quit.\n\
7633 The elements of this list correspond to the arguments of\n\
7634 `set-input-mode'.")
7637 Lisp_Object val[4];
7639 val[0] = interrupt_input ? Qt : Qnil;
7640 val[1] = flow_control ? Qt : Qnil;
7641 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
7642 XSETFASTINT (val[3], quit_char);
7644 return Flist (sizeof (val) / sizeof (val[0]), val);
7649 * Set up a new kboard object with reasonable initial values.
7651 void
7652 init_kboard (kb)
7653 KBOARD *kb;
7655 kb->Voverriding_terminal_local_map = Qnil;
7656 kb->Vlast_command = Qnil;
7657 kb->Vprefix_arg = Qnil;
7658 kb->kbd_queue = Qnil;
7659 kb->kbd_queue_has_data = 0;
7660 kb->immediate_echo = 0;
7661 kb->echoptr = kb->echobuf;
7662 kb->echo_after_prompt = -1;
7663 kb->kbd_macro_buffer = 0;
7664 kb->kbd_macro_bufsize = 0;
7665 kb->defining_kbd_macro = Qnil;
7666 kb->Vlast_kbd_macro = Qnil;
7667 kb->reference_count = 0;
7668 kb->Vsystem_key_alist = Qnil;
7669 kb->system_key_syms = Qnil;
7670 kb->Vdefault_minibuffer_frame = Qnil;
7674 * Destroy the contents of a kboard object, but not the object itself.
7675 * We use this just before deleting it, or if we're going to initialize
7676 * it a second time.
7678 static void
7679 wipe_kboard (kb)
7680 KBOARD *kb;
7682 if (kb->kbd_macro_buffer)
7683 xfree (kb->kbd_macro_buffer);
7686 #ifdef MULTI_KBOARD
7687 void
7688 delete_kboard (kb)
7689 KBOARD *kb;
7691 KBOARD **kbp;
7692 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
7693 if (*kbp == NULL)
7694 abort ();
7695 *kbp = kb->next_kboard;
7696 wipe_kboard (kb);
7697 xfree (kb);
7699 #endif
7701 init_keyboard ()
7703 /* This is correct before outermost invocation of the editor loop */
7704 command_loop_level = -1;
7705 immediate_quit = 0;
7706 quit_char = Ctl ('g');
7707 Vunread_command_events = Qnil;
7708 unread_command_char = -1;
7709 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
7710 total_keys = 0;
7711 recent_keys_index = 0;
7712 kbd_fetch_ptr = kbd_buffer;
7713 kbd_store_ptr = kbd_buffer;
7714 kbd_buffer_frame_or_window
7715 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
7716 #ifdef HAVE_MOUSE
7717 do_mouse_tracking = Qnil;
7718 #endif
7719 input_pending = 0;
7721 /* This means that command_loop_1 won't try to select anything the first
7722 time through. */
7723 internal_last_event_frame = Qnil;
7724 Vlast_event_frame = internal_last_event_frame;
7726 #ifdef MULTI_KBOARD
7727 current_kboard = initial_kboard;
7728 #endif
7729 wipe_kboard (current_kboard);
7730 init_kboard (current_kboard);
7732 if (initialized)
7733 Ffillarray (kbd_buffer_frame_or_window, Qnil);
7735 kbd_buffer_frame_or_window
7736 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
7737 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
7739 signal (SIGINT, interrupt_signal);
7740 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
7741 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
7742 SIGQUIT and we can't tell which one it will give us. */
7743 signal (SIGQUIT, interrupt_signal);
7744 #endif /* HAVE_TERMIO */
7746 /* Note SIGIO has been undef'd if FIONREAD is missing. */
7747 #ifdef SIGIO
7748 if (!noninteractive)
7749 signal (SIGIO, input_available_signal);
7750 #endif /* SIGIO */
7752 /* Use interrupt input by default, if it works and noninterrupt input
7753 has deficiencies. */
7755 #ifdef INTERRUPT_INPUT
7756 interrupt_input = 1;
7757 #else
7758 interrupt_input = 0;
7759 #endif
7761 /* Our VMS input only works by interrupts, as of now. */
7762 #ifdef VMS
7763 interrupt_input = 1;
7764 #endif
7766 sigfree ();
7767 dribble = 0;
7769 if (keyboard_init_hook)
7770 (*keyboard_init_hook) ();
7772 #ifdef POLL_FOR_INPUT
7773 poll_suppress_count = 1;
7774 start_polling ();
7775 #endif
7778 /* This type's only use is in syms_of_keyboard, to initialize the
7779 event header symbols and put properties on them. */
7780 struct event_head {
7781 Lisp_Object *var;
7782 char *name;
7783 Lisp_Object *kind;
7786 struct event_head head_table[] = {
7787 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
7788 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
7789 &Qswitch_frame, "switch-frame", &Qswitch_frame,
7790 &Qdelete_frame, "delete-frame", &Qdelete_frame,
7791 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
7792 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
7795 syms_of_keyboard ()
7797 Qdisabled_command_hook = intern ("disabled-command-hook");
7798 staticpro (&Qdisabled_command_hook);
7800 Qself_insert_command = intern ("self-insert-command");
7801 staticpro (&Qself_insert_command);
7803 Qforward_char = intern ("forward-char");
7804 staticpro (&Qforward_char);
7806 Qbackward_char = intern ("backward-char");
7807 staticpro (&Qbackward_char);
7809 Qdisabled = intern ("disabled");
7810 staticpro (&Qdisabled);
7812 Qundefined = intern ("undefined");
7813 staticpro (&Qundefined);
7815 Qpre_command_hook = intern ("pre-command-hook");
7816 staticpro (&Qpre_command_hook);
7818 Qpost_command_hook = intern ("post-command-hook");
7819 staticpro (&Qpost_command_hook);
7821 Qpost_command_idle_hook = intern ("post-command-idle-hook");
7822 staticpro (&Qpost_command_idle_hook);
7824 Qdeferred_action_function = intern ("deferred-action-function");
7825 staticpro (&Qdeferred_action_function);
7827 Qcommand_hook_internal = intern ("command-hook-internal");
7828 staticpro (&Qcommand_hook_internal);
7830 Qfunction_key = intern ("function-key");
7831 staticpro (&Qfunction_key);
7832 Qmouse_click = intern ("mouse-click");
7833 staticpro (&Qmouse_click);
7834 Qtimer_event = intern ("timer-event");
7835 staticpro (&Qtimer_event);
7837 Qmenu_enable = intern ("menu-enable");
7838 staticpro (&Qmenu_enable);
7840 Qmode_line = intern ("mode-line");
7841 staticpro (&Qmode_line);
7842 Qvertical_line = intern ("vertical-line");
7843 staticpro (&Qvertical_line);
7844 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
7845 staticpro (&Qvertical_scroll_bar);
7846 Qmenu_bar = intern ("menu-bar");
7847 staticpro (&Qmenu_bar);
7849 Qabove_handle = intern ("above-handle");
7850 staticpro (&Qabove_handle);
7851 Qhandle = intern ("handle");
7852 staticpro (&Qhandle);
7853 Qbelow_handle = intern ("below-handle");
7854 staticpro (&Qbelow_handle);
7855 Qup = intern ("up");
7856 staticpro (&Qup);
7857 Qdown = intern ("down");
7858 staticpro (&Qdown);
7860 Qevent_kind = intern ("event-kind");
7861 staticpro (&Qevent_kind);
7862 Qevent_symbol_elements = intern ("event-symbol-elements");
7863 staticpro (&Qevent_symbol_elements);
7864 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
7865 staticpro (&Qevent_symbol_element_mask);
7866 Qmodifier_cache = intern ("modifier-cache");
7867 staticpro (&Qmodifier_cache);
7869 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
7870 staticpro (&Qrecompute_lucid_menubar);
7871 Qactivate_menubar_hook = intern ("activate-menubar-hook");
7872 staticpro (&Qactivate_menubar_hook);
7874 Qpolling_period = intern ("polling-period");
7875 staticpro (&Qpolling_period);
7878 struct event_head *p;
7880 for (p = head_table;
7881 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
7882 p++)
7884 *p->var = intern (p->name);
7885 staticpro (p->var);
7886 Fput (*p->var, Qevent_kind, *p->kind);
7887 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
7891 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
7892 staticpro (&button_down_location);
7895 int i;
7896 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
7898 modifier_symbols = Fmake_vector (make_number (len), Qnil);
7899 for (i = 0; i < len; i++)
7900 if (modifier_names[i])
7901 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
7902 staticpro (&modifier_symbols);
7905 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
7906 staticpro (&recent_keys);
7908 this_command_keys = Fmake_vector (make_number (40), Qnil);
7909 staticpro (&this_command_keys);
7911 Qextended_command_history = intern ("extended-command-history");
7912 Fset (Qextended_command_history, Qnil);
7913 staticpro (&Qextended_command_history);
7915 kbd_buffer_frame_or_window
7916 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
7917 staticpro (&kbd_buffer_frame_or_window);
7919 accent_key_syms = Qnil;
7920 staticpro (&accent_key_syms);
7922 func_key_syms = Qnil;
7923 staticpro (&func_key_syms);
7925 mouse_syms = Qnil;
7926 staticpro (&mouse_syms);
7928 unread_switch_frame = Qnil;
7929 staticpro (&unread_switch_frame);
7931 internal_last_event_frame = Qnil;
7932 staticpro (&internal_last_event_frame);
7934 read_key_sequence_cmd = Qnil;
7935 staticpro (&read_key_sequence_cmd);
7937 defsubr (&Sevent_convert_list);
7938 defsubr (&Sread_key_sequence);
7939 defsubr (&Srecursive_edit);
7940 #ifdef HAVE_MOUSE
7941 defsubr (&Strack_mouse);
7942 #endif
7943 defsubr (&Sinput_pending_p);
7944 defsubr (&Scommand_execute);
7945 defsubr (&Srecent_keys);
7946 defsubr (&Sthis_command_keys);
7947 defsubr (&Sthis_single_command_keys);
7948 defsubr (&Sreset_this_command_lengths);
7949 defsubr (&Ssuspend_emacs);
7950 defsubr (&Sabort_recursive_edit);
7951 defsubr (&Sexit_recursive_edit);
7952 defsubr (&Srecursion_depth);
7953 defsubr (&Stop_level);
7954 defsubr (&Sdiscard_input);
7955 defsubr (&Sopen_dribble_file);
7956 defsubr (&Sset_input_mode);
7957 defsubr (&Scurrent_input_mode);
7958 defsubr (&Sexecute_extended_command);
7960 DEFVAR_LISP ("last-command-char", &last_command_char,
7961 "Last input event that was part of a command.");
7963 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
7964 "Last input event that was part of a command.");
7966 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
7967 "Last input event in a command, except for mouse menu events.\n\
7968 Mouse menus give back keys that don't look like mouse events;\n\
7969 this variable holds the actual mouse event that led to the menu,\n\
7970 so that you can determine whether the command was run by mouse or not.");
7972 DEFVAR_LISP ("last-input-char", &last_input_char,
7973 "Last input event.");
7975 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
7976 "Last input event.");
7978 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
7979 "List of objects to be read as next command input events.");
7981 DEFVAR_INT ("unread-command-char", &unread_command_char,
7982 "If not -1, an object to be read as next command input event.");
7984 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
7985 "Meta-prefix character code. Meta-foo as command input\n\
7986 turns into this character followed by foo.");
7987 XSETINT (meta_prefix_char, 033);
7989 DEFVAR_KBOARD ("last-command", Vlast_command,
7990 "The last command executed. Normally a symbol with a function definition,\n\
7991 but can be whatever was found in the keymap, or whatever the variable\n\
7992 `this-command' was set to by that command.\n\
7994 The value `mode-exit' is special; it means that the previous command\n\
7995 read an event that told it to exit, and it did so and unread that event.\n\
7996 In other words, the present command is the event that made the previous\n\
7997 command exit.\n\
7999 The value `kill-region' is special; it means that the previous command\n\
8000 was a kill command.");
8002 DEFVAR_LISP ("this-command", &this_command,
8003 "The command now being executed.\n\
8004 The command can set this variable; whatever is put here\n\
8005 will be in `last-command' during the following command.");
8006 this_command = Qnil;
8008 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
8009 "*Number of keyboard input characters between auto-saves.\n\
8010 Zero means disable autosaving due to number of characters typed.");
8011 auto_save_interval = 300;
8013 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
8014 "*Number of seconds idle time before auto-save.\n\
8015 Zero or nil means disable auto-saving due to idleness.\n\
8016 After auto-saving due to this many seconds of idle time,\n\
8017 Emacs also does a garbage collection if that seems to be warranted.");
8018 XSETFASTINT (Vauto_save_timeout, 30);
8020 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
8021 "*Nonzero means echo unfinished commands after this many seconds of pause.");
8022 echo_keystrokes = 1;
8024 DEFVAR_INT ("polling-period", &polling_period,
8025 "*Interval between polling for input during Lisp execution.\n\
8026 The reason for polling is to make C-g work to stop a running program.\n\
8027 Polling is needed only when using X windows and SIGIO does not work.\n\
8028 Polling is automatically disabled in all other cases.");
8029 polling_period = 2;
8031 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
8032 "*Maximum time between mouse clicks to make a double-click.\n\
8033 Measured in milliseconds. nil means disable double-click recognition;\n\
8034 t means double-clicks have no time limit and are detected\n\
8035 by position only.");
8036 Vdouble_click_time = make_number (500);
8038 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
8039 "*Non-nil means inhibit local map menu bar menus.");
8040 inhibit_local_menu_bar_menus = 0;
8042 DEFVAR_INT ("num-input-keys", &num_input_keys,
8043 "Number of complete key sequences read from the keyboard so far.\n\
8044 This includes key sequences read from keyboard macros.\n\
8045 The number is effectively the number of interactive command invocations.");
8046 num_input_keys = 0;
8048 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
8049 "The frame in which the most recently read event occurred.\n\
8050 If the last event came from a keyboard macro, this is set to `macro'.");
8051 Vlast_event_frame = Qnil;
8053 DEFVAR_LISP ("help-char", &Vhelp_char,
8054 "Character to recognize as meaning Help.\n\
8055 When it is read, do `(eval help-form)', and display result if it's a string.\n\
8056 If the value of `help-form' is nil, this char can be read normally.");
8057 XSETINT (Vhelp_char, Ctl ('H'));
8059 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
8060 "List of input events to recognize as meaning Help.\n\
8061 These work just like the value of `help-char' (see that).");
8062 Vhelp_event_list = Qnil;
8064 DEFVAR_LISP ("help-form", &Vhelp_form,
8065 "Form to execute when character `help-char' is read.\n\
8066 If the form returns a string, that string is displayed.\n\
8067 If `help-form' is nil, the help char is not recognized.");
8068 Vhelp_form = Qnil;
8070 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
8071 "Command to run when `help-char' character follows a prefix key.\n\
8072 This command is used only when there is no actual binding\n\
8073 for that character after that prefix key.");
8074 Vprefix_help_command = Qnil;
8076 DEFVAR_LISP ("top-level", &Vtop_level,
8077 "Form to evaluate when Emacs starts up.\n\
8078 Useful to set before you dump a modified Emacs.");
8079 Vtop_level = Qnil;
8081 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
8082 "Translate table for keyboard input, or nil.\n\
8083 Each character is looked up in this string and the contents used instead.\n\
8084 The value may be a string, a vector, or a char-table.\n\
8085 If it is a string or vector of length N,\n\
8086 character codes N and up are untranslated.\n\
8087 In a vector or a char-table, an element which is nil means \"no translation\".");
8088 Vkeyboard_translate_table = Qnil;
8090 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
8091 "Non-nil means to always spawn a subshell instead of suspending,\n\
8092 even if the operating system has support for stopping a process.");
8093 cannot_suspend = 0;
8095 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
8096 "Non-nil means prompt with menus when appropriate.\n\
8097 This is done when reading from a keymap that has a prompt string,\n\
8098 for elements that have prompt strings.\n\
8099 The menu is displayed on the screen\n\
8100 if X menus were enabled at configuration\n\
8101 time and the previous event was a mouse click prefix key.\n\
8102 Otherwise, menu prompting uses the echo area.");
8103 menu_prompting = 1;
8105 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
8106 "Character to see next line of menu prompt.\n\
8107 Type this character while in a menu prompt to rotate around the lines of it.");
8108 XSETINT (menu_prompt_more_char, ' ');
8110 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
8111 "A mask of additional modifier keys to use with every keyboard character.\n\
8112 Emacs applies the modifiers of the character stored here to each keyboard\n\
8113 character it reads. For example, after evaluating the expression\n\
8114 (setq extra-keyboard-modifiers ?\\C-x)\n\
8115 all input characters will have the control modifier applied to them.\n\
8117 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
8118 not count as a control character; rather, it counts as a character\n\
8119 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
8120 cancels any modification.");
8121 extra_keyboard_modifiers = 0;
8123 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
8124 "If an editing command sets this to t, deactivate the mark afterward.\n\
8125 The command loop sets this to nil before each command,\n\
8126 and tests the value when the command returns.\n\
8127 Buffer modification stores t in this variable.");
8128 Vdeactivate_mark = Qnil;
8130 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
8131 "Temporary storage of pre-command-hook or post-command-hook.");
8132 Vcommand_hook_internal = Qnil;
8134 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
8135 "Normal hook run before each command is executed.\n\
8136 Errors running the hook are caught and ignored.");
8137 Vpre_command_hook = Qnil;
8139 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
8140 "Normal hook run after each command is executed.\n\
8141 Errors running the hook are caught and ignored.");
8142 Vpost_command_hook = Qnil;
8144 DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
8145 "Normal hook run after each command is executed, if idle.\n\
8146 Errors running the hook are caught and ignored.\n\
8147 This feature is obsolete; use idle timers instead. See `etc/NEWS'.");
8148 Vpost_command_idle_hook = Qnil;
8150 DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
8151 "Delay time before running `post-command-idle-hook'.\n\
8152 This is measured in microseconds.");
8153 post_command_idle_delay = 100000;
8155 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
8156 "t means menu bar, specified Lucid style, needs to be recomputed.");
8157 Vlucid_menu_bar_dirty_flag = Qnil;
8159 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
8160 "List of menu bar items to move to the end of the menu bar.\n\
8161 The elements of the list are event types that may have menu bar bindings.");
8162 Vmenu_bar_final_items = Qnil;
8164 DEFVAR_KBOARD ("overriding-terminal-local-map",
8165 Voverriding_terminal_local_map,
8166 "Keymap that overrides all other local keymaps.\n\
8167 If this variable is non-nil, it is used as a keymap instead of the\n\
8168 buffer's local map, and the minor mode keymaps and text property keymaps.");
8170 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
8171 "Keymap that overrides all other local keymaps.\n\
8172 If this variable is non-nil, it is used as a keymap instead of the\n\
8173 buffer's local map, and the minor mode keymaps and text property keymaps.");
8174 Voverriding_local_map = Qnil;
8176 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
8177 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
8178 Otherwise, the menu bar continues to reflect the buffer's local map\n\
8179 and the minor mode maps regardless of `overriding-local-map'.");
8180 Voverriding_local_map_menu_flag = Qnil;
8182 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
8183 "Keymap defining bindings for special events to execute at low level.");
8184 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
8186 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
8187 "*Non-nil means generate motion events for mouse motion.");
8189 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
8190 "Alist of system-specific X windows key symbols.\n\
8191 Each element should have the form (N . SYMBOL) where N is the\n\
8192 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
8193 and SYMBOL is its name.");
8195 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
8196 "List of deferred actions to be performed at a later time.\n\
8197 The precise format isn't relevant here; we just check whether it is nil.");
8198 Vdeferred_action_list = Qnil;
8200 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
8201 "Function to call to handle deferred actions, after each command.\n\
8202 This function is called with no arguments after each command\n\
8203 whenever `deferred-action-list' is non-nil.");
8204 Vdeferred_action_function = Qnil;
8206 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
8207 "Non-nil means show the equivalent key-binding when M-x command has one.\n\
8208 The value can be a length of time to show the message for.\n\
8209 If the value is non-nil and not a number, we wait 2 seconds.");
8210 Vsuggest_key_bindings = Qt;
8212 DEFVAR_LISP ("timer-list", &Vtimer_list,
8213 "List of active absolute time timers in order of increasing time");
8214 Vtimer_list = Qnil;
8216 DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
8217 "List of active idle-time timers in order of increasing time");
8218 Vtimer_idle_list = Qnil;
8221 keys_of_keyboard ()
8223 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
8224 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
8225 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
8226 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
8227 initial_define_key (meta_map, 'x', "execute-extended-command");
8229 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
8230 "handle-delete-frame");
8231 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
8232 "ignore-event");
8233 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
8234 "ignore-event");