(Finsert_file_contents): Do call signal_after_change
[emacs.git] / src / keyboard.c
blobb16cb1a1f4ab12ef57c94a03a2e1c427cf6266e6
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 "charset.h"
36 #include "disptab.h"
37 #include "dispextern.h"
38 #include "keyboard.h"
39 #include "intervals.h"
40 #include "blockinput.h"
41 #include <setjmp.h>
42 #include <errno.h>
44 #ifdef MSDOS
45 #include "msdos.h"
46 #include <time.h>
47 #else /* not MSDOS */
48 #ifndef VMS
49 #include <sys/ioctl.h>
50 #endif
51 #endif /* not MSDOS */
53 #include "syssignal.h"
54 #include "systty.h"
56 /* This is to get the definitions of the XK_ symbols. */
57 #ifdef HAVE_X_WINDOWS
58 #include "xterm.h"
59 #endif
61 #ifdef HAVE_NTGUI
62 #include "w32term.h"
63 #endif /* HAVE_NTGUI */
65 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
66 #include "systime.h"
68 extern int errno;
70 /* Variables for blockinput.h: */
72 /* Non-zero if interrupt input is blocked right now. */
73 int interrupt_input_blocked;
75 /* Nonzero means an input interrupt has arrived
76 during the current critical section. */
77 int interrupt_input_pending;
80 /* File descriptor to use for input. */
81 extern int input_fd;
83 #ifdef HAVE_WINDOW_SYSTEM
84 /* Make all keyboard buffers much bigger when using X windows. */
85 #define KBD_BUFFER_SIZE 4096
86 #else /* No X-windows, character input */
87 #define KBD_BUFFER_SIZE 256
88 #endif /* No X-windows */
90 /* Following definition copied from eval.c */
92 struct backtrace
94 struct backtrace *next;
95 Lisp_Object *function;
96 Lisp_Object *args; /* Points to vector of args. */
97 int nargs; /* length of vector. If nargs is UNEVALLED,
98 args points to slot holding list of
99 unevalled args */
100 char evalargs;
103 #ifdef MULTI_KBOARD
104 KBOARD *initial_kboard;
105 KBOARD *current_kboard;
106 KBOARD *all_kboards;
107 int single_kboard;
108 #else
109 KBOARD the_only_kboard;
110 #endif
112 /* Non-nil disable property on a command means
113 do not execute it; call disabled-command-hook's value instead. */
114 Lisp_Object Qdisabled, Qdisabled_command_hook;
116 #define NUM_RECENT_KEYS (100)
117 int recent_keys_index; /* Index for storing next element into recent_keys */
118 int total_keys; /* Total number of elements stored into recent_keys */
119 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
121 /* Vector holding the key sequence that invoked the current command.
122 It is reused for each command, and it may be longer than the current
123 sequence; this_command_key_count indicates how many elements
124 actually mean something.
125 It's easier to staticpro a single Lisp_Object than an array. */
126 Lisp_Object this_command_keys;
127 int this_command_key_count;
129 /* Number of elements of this_command_keys
130 that precede this key sequence. */
131 int this_single_command_key_start;
133 /* Record values of this_command_key_count and echo_length ()
134 before this command was read. */
135 static int before_command_key_count;
136 static int before_command_echo_length;
137 /* Values of before_command_key_count and before_command_echo_length
138 saved by reset-this-command-lengths. */
139 static int before_command_key_count_1;
140 static int before_command_echo_length_1;
141 /* Flag set by reset-this-command-lengths,
142 saying to reset the lengths when add_command_key is called. */
143 static int before_command_restore_flag;
145 extern int minbuf_level;
147 extern struct backtrace *backtrace_list;
149 /* Nonzero means do menu prompting. */
150 static int menu_prompting;
152 /* Character to see next line of menu prompt. */
153 static Lisp_Object menu_prompt_more_char;
155 /* For longjmp to where kbd input is being done. */
156 static jmp_buf getcjmp;
158 /* True while doing kbd input. */
159 int waiting_for_input;
161 /* True while displaying for echoing. Delays C-g throwing. */
162 static int echoing;
164 /* True means we can start echoing at the next input pause
165 even though there is something in the echo area. */
166 static char *ok_to_echo_at_next_pause;
168 /* Nonzero means disregard local maps for the menu bar. */
169 static int inhibit_local_menu_bar_menus;
171 /* Nonzero means C-g should cause immediate error-signal. */
172 int immediate_quit;
174 /* Character to recognize as the help char. */
175 Lisp_Object Vhelp_char;
177 /* List of other event types to recognize as meaning "help". */
178 Lisp_Object Vhelp_event_list;
180 /* Form to execute when help char is typed. */
181 Lisp_Object Vhelp_form;
183 /* Command to run when the help character follows a prefix key. */
184 Lisp_Object Vprefix_help_command;
186 /* List of items that should move to the end of the menu bar. */
187 Lisp_Object Vmenu_bar_final_items;
189 /* Non-nil means show the equivalent key-binding for
190 any M-x command that has one.
191 The value can be a length of time to show the message for.
192 If the value is non-nil and not a number, we wait 2 seconds. */
193 Lisp_Object Vsuggest_key_bindings;
195 /* Character that causes a quit. Normally C-g.
197 If we are running on an ordinary terminal, this must be an ordinary
198 ASCII char, since we want to make it our interrupt character.
200 If we are not running on an ordinary terminal, it still needs to be
201 an ordinary ASCII char. This character needs to be recognized in
202 the input interrupt handler. At this point, the keystroke is
203 represented as a struct input_event, while the desired quit
204 character is specified as a lispy event. The mapping from struct
205 input_events to lispy events cannot run in an interrupt handler,
206 and the reverse mapping is difficult for anything but ASCII
207 keystrokes.
209 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
210 ASCII character. */
211 int quit_char;
213 extern Lisp_Object current_global_map;
214 extern int minibuf_level;
216 /* If non-nil, this is a map that overrides all other local maps. */
217 Lisp_Object Voverriding_local_map;
219 /* If non-nil, Voverriding_local_map applies to the menu bar. */
220 Lisp_Object Voverriding_local_map_menu_flag;
222 /* Keymap that defines special misc events that should
223 be processed immediately at a low level. */
224 Lisp_Object Vspecial_event_map;
226 /* Current depth in recursive edits. */
227 int command_loop_level;
229 /* Total number of times command_loop has read a key sequence. */
230 int num_input_keys;
232 /* Last input character read as a command. */
233 Lisp_Object last_command_char;
235 /* Last input character read as a command, not counting menus
236 reached by the mouse. */
237 Lisp_Object last_nonmenu_event;
239 /* Last input character read for any purpose. */
240 Lisp_Object last_input_char;
242 /* If not Qnil, a list of objects to be read as subsequent command input. */
243 Lisp_Object Vunread_command_events;
245 /* If not -1, an event to be read as subsequent command input. */
246 int unread_command_char;
248 /* If not Qnil, this is a switch-frame event which we decided to put
249 off until the end of a key sequence. This should be read as the
250 next command input, after any unread_command_events.
252 read_key_sequence uses this to delay switch-frame events until the
253 end of the key sequence; Fread_char uses it to put off switch-frame
254 events until a non-ASCII event is acceptable as input. */
255 Lisp_Object unread_switch_frame;
257 /* A mask of extra modifier bits to put into every keyboard char. */
258 int extra_keyboard_modifiers;
260 /* Char to use as prefix when a meta character is typed in.
261 This is bound on entry to minibuffer in case ESC is changed there. */
263 Lisp_Object meta_prefix_char;
265 /* Last size recorded for a current buffer which is not a minibuffer. */
266 static int last_non_minibuf_size;
268 /* Number of idle seconds before an auto-save and garbage collection. */
269 static Lisp_Object Vauto_save_timeout;
271 /* Total number of times read_char has returned. */
272 int num_input_chars;
274 /* Total number of times read_char has returned, outside of macros. */
275 int num_nonmacro_input_chars;
277 /* Auto-save automatically when this many characters have been typed
278 since the last time. */
280 static int auto_save_interval;
282 /* Value of num_nonmacro_input_chars as of last auto save. */
284 int last_auto_save;
286 /* The command being executed by the command loop.
287 Commands may set this, and the value set will be copied into
288 current_kboard->Vlast_command instead of the actual command. */
289 Lisp_Object this_command;
291 /* The value of point when the last command was executed. */
292 int last_point_position;
294 /* The buffer that was current when the last command was started. */
295 Lisp_Object last_point_position_buffer;
297 /* The frame in which the last input event occurred, or Qmacro if the
298 last event came from a macro. We use this to determine when to
299 generate switch-frame events. This may be cleared by functions
300 like Fselect_frame, to make sure that a switch-frame event is
301 generated by the next character. */
302 Lisp_Object internal_last_event_frame;
304 /* A user-visible version of the above, intended to allow users to
305 figure out where the last event came from, if the event doesn't
306 carry that information itself (i.e. if it was a character). */
307 Lisp_Object Vlast_event_frame;
309 /* The timestamp of the last input event we received from the X server.
310 X Windows wants this for selection ownership. */
311 unsigned long last_event_timestamp;
313 Lisp_Object Qself_insert_command;
314 Lisp_Object Qforward_char;
315 Lisp_Object Qbackward_char;
316 Lisp_Object Qundefined;
318 /* read_key_sequence stores here the command definition of the
319 key sequence that it reads. */
320 Lisp_Object read_key_sequence_cmd;
322 /* Form to evaluate (if non-nil) when Emacs is started. */
323 Lisp_Object Vtop_level;
325 /* User-supplied string to translate input characters through. */
326 Lisp_Object Vkeyboard_translate_table;
328 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
329 extern Lisp_Object Vfunction_key_map;
331 /* Another keymap that maps key sequences into key sequences.
332 This one takes precedence over ordinary definitions. */
333 extern Lisp_Object Vkey_translation_map;
335 /* Non-nil means deactivate the mark at end of this command. */
336 Lisp_Object Vdeactivate_mark;
338 /* Menu bar specified in Lucid Emacs fashion. */
340 Lisp_Object Vlucid_menu_bar_dirty_flag;
341 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
343 Lisp_Object Qecho_area_clear_hook;
345 /* Hooks to run before and after each command. */
346 Lisp_Object Qpre_command_hook, Vpre_command_hook;
347 Lisp_Object Qpost_command_hook, Vpost_command_hook;
348 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
349 /* Hook run after a command if there's no more input soon. */
350 Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook;
352 /* Delay time in microseconds before running post-command-idle-hook. */
353 int post_command_idle_delay;
355 /* List of deferred actions to be performed at a later time.
356 The precise format isn't relevant here; we just check whether it is nil. */
357 Lisp_Object Vdeferred_action_list;
359 /* Function to call to handle deferred actions, when there are any. */
360 Lisp_Object Vdeferred_action_function;
361 Lisp_Object Qdeferred_action_function;
363 /* File in which we write all commands we read. */
364 FILE *dribble;
366 /* Nonzero if input is available. */
367 int input_pending;
369 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
370 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
372 int meta_key;
374 extern char *pending_malloc_warning;
376 /* Circular buffer for pre-read keyboard input. */
377 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
379 /* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
381 The interrupt-level event handlers will never enqueue an event on a
382 frame which is not in Vframe_list, and once an event is dequeued,
383 internal_last_event_frame or the event itself points to the frame.
384 So that's all fine.
386 But while the event is sitting in the queue, it's completely
387 unprotected. Suppose the user types one command which will run for
388 a while and then delete a frame, and then types another event at
389 the frame that will be deleted, before the command gets around to
390 it. Suppose there are no references to this frame elsewhere in
391 Emacs, and a GC occurs before the second event is dequeued. Now we
392 have an event referring to a freed frame, which will crash Emacs
393 when it is dequeued.
395 Similar things happen when an event on a scroll bar is enqueued; the
396 window may be deleted while the event is in the queue.
398 So, we use this vector to protect the frame_or_window field in the
399 event queue. That way, they'll be dequeued as dead frames or
400 windows, but still valid lisp objects.
402 If kbd_buffer[i].kind != no_event, then
403 (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
404 == kbd_buffer[i].frame_or_window. */
405 static Lisp_Object kbd_buffer_frame_or_window;
407 /* Pointer to next available character in kbd_buffer.
408 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
409 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
410 next available char is in kbd_buffer[0]. */
411 static struct input_event *kbd_fetch_ptr;
413 /* Pointer to next place to store character in kbd_buffer. This
414 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
415 character should go in kbd_buffer[0]. */
416 static volatile struct input_event *kbd_store_ptr;
418 /* The above pair of variables forms a "queue empty" flag. When we
419 enqueue a non-hook event, we increment kbd_store_ptr. When we
420 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
421 there is input available iff the two pointers are not equal.
423 Why not just have a flag set and cleared by the enqueuing and
424 dequeuing functions? Such a flag could be screwed up by interrupts
425 at inopportune times. */
427 /* If this flag is non-nil, we check mouse_moved to see when the
428 mouse moves, and motion events will appear in the input stream.
429 Otherwise, mouse motion is ignored. */
430 static Lisp_Object do_mouse_tracking;
432 /* Symbols to head events. */
433 Lisp_Object Qmouse_movement;
434 Lisp_Object Qscroll_bar_movement;
435 Lisp_Object Qswitch_frame;
436 Lisp_Object Qdelete_frame;
437 Lisp_Object Qiconify_frame;
438 Lisp_Object Qmake_frame_visible;
440 /* Symbols to denote kinds of events. */
441 Lisp_Object Qfunction_key;
442 Lisp_Object Qmouse_click;
443 Lisp_Object Qtimer_event;
444 /* Lisp_Object Qmouse_movement; - also an event header */
446 /* Properties of event headers. */
447 Lisp_Object Qevent_kind;
448 Lisp_Object Qevent_symbol_elements;
450 Lisp_Object Qmenu_enable;
452 /* An event header symbol HEAD may have a property named
453 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
454 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
455 mask of modifiers applied to it. If present, this is used to help
456 speed up parse_modifiers. */
457 Lisp_Object Qevent_symbol_element_mask;
459 /* An unmodified event header BASE may have a property named
460 Qmodifier_cache, which is an alist mapping modifier masks onto
461 modified versions of BASE. If present, this helps speed up
462 apply_modifiers. */
463 Lisp_Object Qmodifier_cache;
465 /* Symbols to use for parts of windows. */
466 Lisp_Object Qmode_line;
467 Lisp_Object Qvertical_line;
468 Lisp_Object Qvertical_scroll_bar;
469 Lisp_Object Qmenu_bar;
471 extern Lisp_Object Qmenu_enable;
473 Lisp_Object recursive_edit_unwind (), command_loop ();
474 Lisp_Object Fthis_command_keys ();
475 Lisp_Object Qextended_command_history;
476 EMACS_TIME timer_check ();
478 extern char *x_get_keysym_name ();
480 static void record_menu_key ();
482 Lisp_Object Qpolling_period;
484 /* List of absolute timers. Appears in order of next scheduled event. */
485 Lisp_Object Vtimer_list;
487 /* List of idle time timers. Appears in order of next scheduled event. */
488 Lisp_Object Vtimer_idle_list;
490 /* Incremented whenever a timer is run. */
491 int timers_run;
493 extern Lisp_Object Vprint_level, Vprint_length;
495 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
496 happens. */
497 EMACS_TIME *input_available_clear_time;
499 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
500 Default is 1 if INTERRUPT_INPUT is defined. */
501 int interrupt_input;
503 /* Nonzero while interrupts are temporarily deferred during redisplay. */
504 int interrupts_deferred;
506 /* Nonzero means use ^S/^Q for flow control. */
507 int flow_control;
509 /* Allow m- file to inhibit use of FIONREAD. */
510 #ifdef BROKEN_FIONREAD
511 #undef FIONREAD
512 #endif
514 /* We are unable to use interrupts if FIONREAD is not available,
515 so flush SIGIO so we won't try. */
516 #ifndef FIONREAD
517 #ifdef SIGIO
518 #undef SIGIO
519 #endif
520 #endif
522 /* If we support a window system, turn on the code to poll periodically
523 to detect C-g. It isn't actually used when doing interrupt input. */
524 #ifdef HAVE_WINDOW_SYSTEM
525 #define POLL_FOR_INPUT
526 #endif
528 /* Global variable declarations. */
530 /* Function for init_keyboard to call with no args (if nonzero). */
531 void (*keyboard_init_hook) ();
533 static int read_avail_input ();
534 static void get_input_pending ();
535 static int readable_events ();
536 static Lisp_Object read_char_x_menu_prompt ();
537 static Lisp_Object read_char_minibuf_menu_prompt ();
538 static Lisp_Object make_lispy_event ();
539 #ifdef HAVE_MOUSE
540 static Lisp_Object make_lispy_movement ();
541 #endif
542 static Lisp_Object modify_event_symbol ();
543 static Lisp_Object make_lispy_switch_frame ();
544 static int parse_solitary_modifier ();
546 /* > 0 if we are to echo keystrokes. */
547 static int echo_keystrokes;
549 /* Nonzero means don't try to suspend even if the operating system seems
550 to support it. */
551 static int cannot_suspend;
553 #define min(a,b) ((a)<(b)?(a):(b))
554 #define max(a,b) ((a)>(b)?(a):(b))
556 /* Install the string STR as the beginning of the string of echoing,
557 so that it serves as a prompt for the next character.
558 Also start echoing. */
560 echo_prompt (str)
561 char *str;
563 int len = strlen (str);
565 if (len > ECHOBUFSIZE - 4)
566 len = ECHOBUFSIZE - 4;
567 bcopy (str, current_kboard->echobuf, len);
568 current_kboard->echoptr = current_kboard->echobuf + len;
569 *current_kboard->echoptr = '\0';
571 current_kboard->echo_after_prompt = len;
573 echo_now ();
576 /* Add C to the echo string, if echoing is going on.
577 C can be a character, which is printed prettily ("M-C-x" and all that
578 jazz), or a symbol, whose name is printed. */
580 echo_char (c)
581 Lisp_Object c;
583 extern char *push_key_description ();
585 if (current_kboard->immediate_echo)
587 char *ptr = current_kboard->echoptr;
589 if (ptr != current_kboard->echobuf)
590 *ptr++ = ' ';
592 /* If someone has passed us a composite event, use its head symbol. */
593 c = EVENT_HEAD (c);
595 if (INTEGERP (c))
597 if (ptr - current_kboard->echobuf > ECHOBUFSIZE - 6)
598 return;
600 ptr = push_key_description (XINT (c), ptr);
602 else if (SYMBOLP (c))
604 struct Lisp_String *name = XSYMBOL (c)->name;
605 if ((ptr - current_kboard->echobuf) + name->size + 4 > ECHOBUFSIZE)
606 return;
607 bcopy (name->data, ptr, name->size);
608 ptr += name->size;
611 if (current_kboard->echoptr == current_kboard->echobuf
612 && help_char_p (c))
614 strcpy (ptr, " (Type ? for further options)");
615 ptr += strlen (ptr);
618 *ptr = 0;
619 current_kboard->echoptr = ptr;
621 echo_now ();
625 /* Temporarily add a dash to the end of the echo string if it's not
626 empty, so that it serves as a mini-prompt for the very next character. */
628 echo_dash ()
630 if (!current_kboard->immediate_echo
631 && current_kboard->echoptr == current_kboard->echobuf)
632 return;
633 /* Do nothing if we just printed a prompt. */
634 if (current_kboard->echo_after_prompt
635 == current_kboard->echoptr - current_kboard->echobuf)
636 return;
637 /* Do nothing if not echoing at all. */
638 if (current_kboard->echoptr == 0)
639 return;
641 /* Put a dash at the end of the buffer temporarily,
642 but make it go away when the next character is added. */
643 current_kboard->echoptr[0] = '-';
644 current_kboard->echoptr[1] = 0;
646 echo_now ();
649 /* Display the current echo string, and begin echoing if not already
650 doing so. */
652 echo_now ()
654 if (!current_kboard->immediate_echo)
656 int i;
657 current_kboard->immediate_echo = 1;
659 for (i = 0; i < this_command_key_count; i++)
661 Lisp_Object c;
662 c = XVECTOR (this_command_keys)->contents[i];
663 if (! (EVENT_HAS_PARAMETERS (c)
664 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
665 echo_char (c);
667 echo_dash ();
670 echoing = 1;
671 message1_nolog (current_kboard->echobuf);
672 echoing = 0;
674 if (waiting_for_input && !NILP (Vquit_flag))
675 quit_throw_to_read_char ();
678 /* Turn off echoing, for the start of a new command. */
680 cancel_echoing ()
682 current_kboard->immediate_echo = 0;
683 current_kboard->echoptr = current_kboard->echobuf;
684 current_kboard->echo_after_prompt = -1;
685 ok_to_echo_at_next_pause = 0;
688 /* Return the length of the current echo string. */
690 static int
691 echo_length ()
693 return current_kboard->echoptr - current_kboard->echobuf;
696 /* Truncate the current echo message to its first LEN chars.
697 This and echo_char get used by read_key_sequence when the user
698 switches frames while entering a key sequence. */
700 static void
701 echo_truncate (len)
702 int len;
704 current_kboard->echobuf[len] = '\0';
705 current_kboard->echoptr = current_kboard->echobuf + len;
706 truncate_echo_area (len);
710 /* Functions for manipulating this_command_keys. */
711 static void
712 add_command_key (key)
713 Lisp_Object key;
715 int size = XVECTOR (this_command_keys)->size;
717 /* If reset-this-command-length was called recently, obey it now.
718 See the doc string of that function for an explanation of why. */
719 if (before_command_restore_flag)
721 this_command_key_count = before_command_key_count_1;
722 if (this_command_key_count < this_single_command_key_start)
723 this_single_command_key_start = this_command_key_count;
724 echo_truncate (before_command_echo_length_1);
725 before_command_restore_flag = 0;
728 if (this_command_key_count >= size)
730 Lisp_Object new_keys;
732 new_keys = Fmake_vector (make_number (size * 2), Qnil);
733 bcopy (XVECTOR (this_command_keys)->contents,
734 XVECTOR (new_keys)->contents,
735 size * sizeof (Lisp_Object));
737 this_command_keys = new_keys;
740 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
743 Lisp_Object
744 recursive_edit_1 ()
746 int count = specpdl_ptr - specpdl;
747 Lisp_Object val;
749 if (command_loop_level > 0)
751 specbind (Qstandard_output, Qt);
752 specbind (Qstandard_input, Qt);
755 val = command_loop ();
756 if (EQ (val, Qt))
757 Fsignal (Qquit, Qnil);
758 /* Handle throw from read_minibuf when using minibuffer
759 while it's active but we're in another window. */
760 if (STRINGP (val))
761 Fsignal (Qerror, Fcons (val, Qnil));
763 return unbind_to (count, Qnil);
766 /* When an auto-save happens, record the "time", and don't do again soon. */
768 record_auto_save ()
770 last_auto_save = num_nonmacro_input_chars;
773 /* Make an auto save happen as soon as possible at command level. */
775 force_auto_save_soon ()
777 last_auto_save = - auto_save_interval - 1;
779 record_asynch_buffer_change ();
782 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
783 "Invoke the editor command loop recursively.\n\
784 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
785 that tells this function to return.\n\
786 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
787 This function is called by the editor initialization to begin editing.")
790 int count = specpdl_ptr - specpdl;
791 Lisp_Object val;
793 command_loop_level++;
794 update_mode_lines = 1;
796 record_unwind_protect (recursive_edit_unwind,
797 (command_loop_level
798 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
799 ? Fcurrent_buffer ()
800 : Qnil);
801 recursive_edit_1 ();
802 return unbind_to (count, Qnil);
805 Lisp_Object
806 recursive_edit_unwind (buffer)
807 Lisp_Object buffer;
809 if (!NILP (buffer))
810 Fset_buffer (buffer);
812 command_loop_level--;
813 update_mode_lines = 1;
814 return Qnil;
817 static void
818 any_kboard_state ()
820 #ifdef MULTI_KBOARD
821 #if 0 /* Theory: if there's anything in Vunread_command_events,
822 it will right away be read by read_key_sequence,
823 and then if we do switch KBOARDS, it will go into the side
824 queue then. So we don't need to do anything special here -- rms. */
825 if (CONSP (Vunread_command_events))
827 current_kboard->kbd_queue
828 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
829 current_kboard->kbd_queue_has_data = 1;
831 Vunread_command_events = Qnil;
832 #endif
833 single_kboard = 0;
834 #endif
837 /* Switch to the single-kboard state, making current_kboard
838 the only KBOARD from which further input is accepted. */
840 void
841 single_kboard_state ()
843 #ifdef MULTI_KBOARD
844 single_kboard = 1;
845 #endif
848 /* Maintain a stack of kboards, so other parts of Emacs
849 can switch temporarily to the kboard of a given frame
850 and then revert to the previous status. */
852 struct kboard_stack
854 KBOARD *kboard;
855 struct kboard_stack *next;
858 static struct kboard_stack *kboard_stack;
860 void
861 push_frame_kboard (f)
862 FRAME_PTR f;
864 #ifdef MULTI_KBOARD
865 struct kboard_stack *p
866 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
868 p->next = kboard_stack;
869 p->kboard = current_kboard;
870 kboard_stack = p;
872 current_kboard = FRAME_KBOARD (f);
873 #endif
876 void
877 pop_frame_kboard ()
879 #ifdef MULTI_KBOARD
880 struct kboard_stack *p = kboard_stack;
881 current_kboard = p->kboard;
882 kboard_stack = p->next;
883 xfree (p);
884 #endif
887 /* Handle errors that are not handled at inner levels
888 by printing an error message and returning to the editor command loop. */
890 Lisp_Object
891 cmd_error (data)
892 Lisp_Object data;
894 Lisp_Object old_level, old_length;
895 char macroerror[50];
897 if (!NILP (executing_macro))
899 if (executing_macro_iterations == 1)
900 sprintf (macroerror, "After 1 kbd macro iteration: ");
901 else
902 sprintf (macroerror, "After %d kbd macro iterations: ",
903 executing_macro_iterations);
905 else
906 *macroerror = 0;
908 Vstandard_output = Qt;
909 Vstandard_input = Qt;
910 Vexecuting_macro = Qnil;
911 executing_macro = Qnil;
912 current_kboard->Vprefix_arg = Qnil;
913 cancel_echoing ();
915 /* Avoid unquittable loop if data contains a circular list. */
916 old_level = Vprint_level;
917 old_length = Vprint_length;
918 XSETFASTINT (Vprint_level, 10);
919 XSETFASTINT (Vprint_length, 10);
920 cmd_error_internal (data, macroerror);
921 Vprint_level = old_level;
922 Vprint_length = old_length;
924 Vquit_flag = Qnil;
926 Vinhibit_quit = Qnil;
927 #ifdef MULTI_KBOARD
928 any_kboard_state ();
929 #endif
931 return make_number (0);
934 cmd_error_internal (data, context)
935 Lisp_Object data;
936 char *context;
938 Lisp_Object stream;
940 Vquit_flag = Qnil;
941 Vinhibit_quit = Qt;
942 echo_area_glyphs = 0;
944 /* If the window system or terminal frame hasn't been initialized
945 yet, or we're not interactive, it's best to dump this message out
946 to stderr and exit. */
947 if (! FRAME_MESSAGE_BUF (selected_frame)
948 || noninteractive)
949 stream = Qexternal_debugging_output;
950 else
952 Fdiscard_input ();
953 bitch_at_user ();
954 stream = Qt;
957 if (context != 0)
958 write_string_1 (context, -1, stream);
960 print_error_message (data, stream);
962 /* If the window system or terminal frame hasn't been initialized
963 yet, or we're in -batch mode, this error should cause Emacs to exit. */
964 if (! FRAME_MESSAGE_BUF (selected_frame)
965 || noninteractive)
967 Fterpri (stream);
968 Fkill_emacs (make_number (-1));
972 Lisp_Object command_loop_1 ();
973 Lisp_Object command_loop_2 ();
974 Lisp_Object top_level_1 ();
976 /* Entry to editor-command-loop.
977 This level has the catches for exiting/returning to editor command loop.
978 It returns nil to exit recursive edit, t to abort it. */
980 Lisp_Object
981 command_loop ()
983 if (command_loop_level > 0 || minibuf_level > 0)
985 return internal_catch (Qexit, command_loop_2, Qnil);
987 else
988 while (1)
990 internal_catch (Qtop_level, top_level_1, Qnil);
991 internal_catch (Qtop_level, command_loop_2, Qnil);
993 /* End of file in -batch run causes exit here. */
994 if (noninteractive)
995 Fkill_emacs (Qt);
999 /* Here we catch errors in execution of commands within the
1000 editing loop, and reenter the editing loop.
1001 When there is an error, cmd_error runs and returns a non-nil
1002 value to us. A value of nil means that cmd_loop_1 itself
1003 returned due to end of file (or end of kbd macro). */
1005 Lisp_Object
1006 command_loop_2 ()
1008 register Lisp_Object val;
1011 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
1012 while (!NILP (val));
1014 return Qnil;
1017 Lisp_Object
1018 top_level_2 ()
1020 return Feval (Vtop_level);
1023 Lisp_Object
1024 top_level_1 ()
1026 /* On entry to the outer level, run the startup file */
1027 if (!NILP (Vtop_level))
1028 internal_condition_case (top_level_2, Qerror, cmd_error);
1029 else if (!NILP (Vpurify_flag))
1030 message ("Bare impure Emacs (standard Lisp code not loaded)");
1031 else
1032 message ("Bare Emacs (standard Lisp code not loaded)");
1033 return Qnil;
1036 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1037 "Exit all recursive editing levels.")
1040 Fthrow (Qtop_level, Qnil);
1043 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1044 "Exit from the innermost recursive edit or minibuffer.")
1047 if (command_loop_level > 0 || minibuf_level > 0)
1048 Fthrow (Qexit, Qnil);
1050 error ("No recursive edit is in progress");
1053 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1054 "Abort the command that requested this recursive edit or minibuffer input.")
1057 if (command_loop_level > 0 || minibuf_level > 0)
1058 Fthrow (Qexit, Qt);
1060 error ("No recursive edit is in progress");
1063 /* This is the actual command reading loop,
1064 sans error-handling encapsulation. */
1066 Lisp_Object Fcommand_execute ();
1067 static int read_key_sequence ();
1068 void safe_run_hooks ();
1070 Lisp_Object
1071 command_loop_1 ()
1073 Lisp_Object cmd, tem;
1074 int lose, lose2;
1075 int nonundocount;
1076 Lisp_Object keybuf[30];
1077 int i;
1078 int no_redisplay;
1079 int no_direct;
1080 int prev_modiff;
1081 struct buffer *prev_buffer;
1082 #ifdef MULTI_KBOARD
1083 int was_locked = single_kboard;
1084 #endif
1086 current_kboard->Vprefix_arg = Qnil;
1087 Vdeactivate_mark = Qnil;
1088 waiting_for_input = 0;
1089 cancel_echoing ();
1091 nonundocount = 0;
1092 no_redisplay = 0;
1093 this_command_key_count = 0;
1094 this_single_command_key_start = 0;
1096 /* Make sure this hook runs after commands that get errors and
1097 throw to top level. */
1098 /* Note that the value cell will never directly contain nil
1099 if the symbol is a local variable. */
1100 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1101 safe_run_hooks (Qpost_command_hook);
1103 if (!NILP (Vdeferred_action_list))
1104 call0 (Vdeferred_action_function);
1106 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1108 if (NILP (Vunread_command_events)
1109 && NILP (Vexecuting_macro)
1110 && !NILP (sit_for (0, post_command_idle_delay, 0, 1)))
1111 safe_run_hooks (Qpost_command_idle_hook);
1114 /* Do this after running Vpost_command_hook, for consistency. */
1115 current_kboard->Vlast_command = this_command;
1117 while (1)
1119 /* Make sure the current window's buffer is selected. */
1120 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1121 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1123 /* Display any malloc warning that just came out. Use while because
1124 displaying one warning can cause another. */
1126 while (pending_malloc_warning)
1127 display_malloc_warning ();
1129 no_direct = 0;
1131 Vdeactivate_mark = Qnil;
1133 /* If minibuffer on and echo area in use,
1134 wait 2 sec and redraw minibuffer. */
1136 if (minibuf_level && echo_area_glyphs
1137 && EQ (minibuf_window, echo_area_window))
1139 /* Bind inhibit-quit to t so that C-g gets read in
1140 rather than quitting back to the minibuffer. */
1141 int count = specpdl_ptr - specpdl;
1142 specbind (Qinhibit_quit, Qt);
1144 Fsit_for (make_number (2), Qnil, Qnil);
1145 /* Clear the echo area. */
1146 message2 (0);
1147 safe_run_hooks (Qecho_area_clear_hook);
1149 unbind_to (count, Qnil);
1151 /* If a C-g came in before, treat it as input now. */
1152 if (!NILP (Vquit_flag))
1154 Vquit_flag = Qnil;
1155 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1159 #ifdef C_ALLOCA
1160 alloca (0); /* Cause a garbage collection now */
1161 /* Since we can free the most stuff here. */
1162 #endif /* C_ALLOCA */
1164 #if 0
1165 /* Select the frame that the last event came from. Usually,
1166 switch-frame events will take care of this, but if some lisp
1167 code swallows a switch-frame event, we'll fix things up here.
1168 Is this a good idea? */
1169 if (FRAMEP (internal_last_event_frame)
1170 && XFRAME (internal_last_event_frame) != selected_frame)
1171 Fselect_frame (internal_last_event_frame, Qnil);
1172 #endif
1173 /* If it has changed current-menubar from previous value,
1174 really recompute the menubar from the value. */
1175 if (! NILP (Vlucid_menu_bar_dirty_flag)
1176 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1177 call0 (Qrecompute_lucid_menubar);
1179 before_command_key_count = this_command_key_count;
1180 before_command_echo_length = echo_length ();
1182 this_command = Qnil;
1184 /* Read next key sequence; i gets its length. */
1185 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1186 Qnil, 0, 1, 1);
1188 /* A filter may have run while we were reading the input. */
1189 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1190 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1192 ++num_input_keys;
1194 /* Now we have read a key sequence of length I,
1195 or else I is 0 and we found end of file. */
1197 if (i == 0) /* End of file -- happens only in */
1198 return Qnil; /* a kbd macro, at the end. */
1199 /* -1 means read_key_sequence got a menu that was rejected.
1200 Just loop around and read another command. */
1201 if (i == -1)
1203 cancel_echoing ();
1204 this_command_key_count = 0;
1205 this_single_command_key_start = 0;
1206 goto finalize;
1209 last_command_char = keybuf[i - 1];
1211 /* If the previous command tried to force a specific window-start,
1212 forget about that, in case this command moves point far away
1213 from that position. But also throw away beg_unchanged and
1214 end_unchanged information in that case, so that redisplay will
1215 update the whole window properly. */
1216 if (!NILP (XWINDOW (selected_window)->force_start))
1218 XWINDOW (selected_window)->force_start = Qnil;
1219 beg_unchanged = end_unchanged = 0;
1222 cmd = read_key_sequence_cmd;
1223 if (!NILP (Vexecuting_macro))
1225 if (!NILP (Vquit_flag))
1227 Vexecuting_macro = Qt;
1228 QUIT; /* Make some noise. */
1229 /* Will return since macro now empty. */
1233 /* Do redisplay processing after this command except in special
1234 cases identified below that set no_redisplay to 1.
1235 (actually, there's currently no way to prevent the redisplay,
1236 and no_redisplay is ignored.
1237 Perhaps someday we will really implement it.) */
1238 no_redisplay = 0;
1240 prev_buffer = current_buffer;
1241 prev_modiff = MODIFF;
1242 last_point_position = PT;
1243 XSETBUFFER (last_point_position_buffer, prev_buffer);
1245 /* Execute the command. */
1247 this_command = cmd;
1248 /* Note that the value cell will never directly contain nil
1249 if the symbol is a local variable. */
1250 if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
1251 safe_run_hooks (Qpre_command_hook);
1253 if (NILP (this_command))
1255 /* nil means key is undefined. */
1256 bitch_at_user ();
1257 current_kboard->defining_kbd_macro = Qnil;
1258 update_mode_lines = 1;
1259 current_kboard->Vprefix_arg = Qnil;
1261 else
1263 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1265 /* Recognize some common commands in common situations and
1266 do them directly. */
1267 if (EQ (this_command, Qforward_char) && PT < ZV)
1269 struct Lisp_Char_Table *dp
1270 = window_display_table (XWINDOW (selected_window));
1271 lose = FETCH_BYTE (PT);
1272 SET_PT (forward_point (1));
1273 if ((dp
1274 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1275 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1276 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1277 && (lose >= 0x20 && lose < 0x7f)))
1278 : (lose >= 0x20 && lose < 0x7f))
1279 /* To extract the case of continuation on
1280 wide-column characters. */
1281 && (WIDTH_BY_CHAR_HEAD (FETCH_BYTE (PT)) == 1)
1282 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1283 >= MODIFF)
1284 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1285 >= OVERLAY_MODIFF)
1286 && (XFASTINT (XWINDOW (selected_window)->last_point)
1287 == PT - 1)
1288 && !windows_or_buffers_changed
1289 && EQ (current_buffer->selective_display, Qnil)
1290 && !detect_input_pending ()
1291 && NILP (XWINDOW (selected_window)->column_number_displayed)
1292 && NILP (Vexecuting_macro))
1293 no_redisplay = direct_output_forward_char (1);
1294 goto directly_done;
1296 else if (EQ (this_command, Qbackward_char) && PT > BEGV)
1298 struct Lisp_Char_Table *dp
1299 = window_display_table (XWINDOW (selected_window));
1300 SET_PT (forward_point (-1));
1301 lose = FETCH_BYTE (PT);
1302 if ((dp
1303 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1304 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1305 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1306 && (lose >= 0x20 && lose < 0x7f)))
1307 : (lose >= 0x20 && lose < 0x7f))
1308 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1309 >= MODIFF)
1310 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1311 >= OVERLAY_MODIFF)
1312 && (XFASTINT (XWINDOW (selected_window)->last_point)
1313 == PT + 1)
1314 && !windows_or_buffers_changed
1315 && EQ (current_buffer->selective_display, Qnil)
1316 && !detect_input_pending ()
1317 && NILP (XWINDOW (selected_window)->column_number_displayed)
1318 && NILP (Vexecuting_macro))
1319 no_redisplay = direct_output_forward_char (-1);
1320 goto directly_done;
1322 else if (EQ (this_command, Qself_insert_command)
1323 /* Try this optimization only on ascii keystrokes. */
1324 && INTEGERP (last_command_char))
1326 unsigned char c = XINT (last_command_char);
1327 int value;
1329 if (NILP (Vexecuting_macro)
1330 && !EQ (minibuf_window, selected_window))
1332 if (!nonundocount || nonundocount >= 20)
1334 Fundo_boundary ();
1335 nonundocount = 0;
1337 nonundocount++;
1339 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1340 < MODIFF)
1341 || (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1342 < OVERLAY_MODIFF)
1343 || (XFASTINT (XWINDOW (selected_window)->last_point)
1344 != PT)
1345 || MODIFF <= SAVE_MODIFF
1346 || windows_or_buffers_changed
1347 || !EQ (current_buffer->selective_display, Qnil)
1348 || detect_input_pending ()
1349 || !NILP (XWINDOW (selected_window)->column_number_displayed)
1350 || !NILP (Vexecuting_macro));
1351 value = internal_self_insert (c, 0);
1352 if (value)
1353 lose = 1;
1354 if (value == 2)
1355 nonundocount = 0;
1357 if (!lose
1358 && (PT == ZV || FETCH_BYTE (PT) == '\n'))
1360 struct Lisp_Char_Table *dp
1361 = window_display_table (XWINDOW (selected_window));
1362 int lose = c;
1364 if (dp)
1366 Lisp_Object obj;
1368 obj = DISP_CHAR_VECTOR (dp, lose);
1369 if (NILP (obj))
1371 /* Do it only for char codes
1372 that by default display as themselves. */
1373 if (lose >= 0x20 && lose <= 0x7e)
1374 no_redisplay = direct_output_for_insert (lose);
1376 else if (VECTORP (obj)
1377 && XVECTOR (obj)->size == 1
1378 && (obj = XVECTOR (obj)->contents[0],
1379 INTEGERP (obj))
1380 /* Insist face not specified in glyph. */
1381 && (XINT (obj) & ((-1) << 8)) == 0)
1382 no_redisplay
1383 = direct_output_for_insert (XINT (obj));
1385 else
1387 if (lose >= 0x20 && lose <= 0x7e)
1388 no_redisplay = direct_output_for_insert (lose);
1391 goto directly_done;
1395 /* Here for a command that isn't executed directly */
1397 nonundocount = 0;
1398 if (NILP (current_kboard->Vprefix_arg))
1399 Fundo_boundary ();
1400 Fcommand_execute (this_command, Qnil, Qnil, Qnil);
1403 directly_done: ;
1405 /* If there is a prefix argument,
1406 1) We don't want Vlast_command to be ``universal-argument''
1407 (that would be dumb), so don't set Vlast_command,
1408 2) we want to leave echoing on so that the prefix will be
1409 echoed as part of this key sequence, so don't call
1410 cancel_echoing, and
1411 3) we want to leave this_command_key_count non-zero, so that
1412 read_char will realize that it is re-reading a character, and
1413 not echo it a second time.
1415 If the command didn't actually create a prefix arg,
1416 but is merely a frame event that is transparent to prefix args,
1417 then the above doesn't apply. */
1418 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1420 current_kboard->Vlast_command = this_command;
1421 cancel_echoing ();
1422 this_command_key_count = 0;
1423 this_single_command_key_start = 0;
1426 /* Note that the value cell will never directly contain nil
1427 if the symbol is a local variable. */
1428 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1429 safe_run_hooks (Qpost_command_hook);
1431 if (!NILP (Vdeferred_action_list))
1432 safe_run_hooks (Qdeferred_action_function);
1434 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1436 if (NILP (Vunread_command_events)
1437 && NILP (Vexecuting_macro)
1438 && !NILP (sit_for (0, post_command_idle_delay, 0, 1)))
1439 safe_run_hooks (Qpost_command_idle_hook);
1442 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1444 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1446 current_buffer->mark_active = Qnil;
1447 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1449 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1450 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1453 finalize:
1454 /* Install chars successfully executed in kbd macro. */
1456 if (!NILP (current_kboard->defining_kbd_macro)
1457 && NILP (current_kboard->Vprefix_arg))
1458 finalize_kbd_macro_chars ();
1460 #ifdef MULTI_KBOARD
1461 if (!was_locked)
1462 any_kboard_state ();
1463 #endif
1467 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1469 static Lisp_Object
1470 safe_run_hooks_1 (hook)
1471 Lisp_Object hook;
1473 return call1 (Vrun_hooks, Vinhibit_quit);
1476 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1478 static Lisp_Object
1479 safe_run_hooks_error (data)
1480 Lisp_Object data;
1482 Fset (Vinhibit_quit, Qnil);
1485 /* If we get an error while running the hook, cause the hook variable
1486 to be nil. Also inhibit quits, so that C-g won't cause the hook
1487 to mysteriously evaporate. */
1489 void
1490 safe_run_hooks (hook)
1491 Lisp_Object hook;
1493 Lisp_Object value;
1494 int count = specpdl_ptr - specpdl;
1495 specbind (Qinhibit_quit, hook);
1497 internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
1499 unbind_to (count, Qnil);
1502 /* Number of seconds between polling for input. */
1503 int polling_period;
1505 /* Nonzero means polling for input is temporarily suppressed. */
1506 int poll_suppress_count;
1508 /* Nonzero if polling_for_input is actually being used. */
1509 int polling_for_input;
1511 #ifdef POLL_FOR_INPUT
1513 /* Handle an alarm once each second and read pending input
1514 so as to handle a C-g if it comces in. */
1516 SIGTYPE
1517 input_poll_signal (signalnum) /* If we don't have an argument, */
1518 int signalnum; /* some compilers complain in signal calls. */
1520 /* This causes the call to start_polling at the end
1521 to do its job. It also arranges for a quit or error
1522 from within read_avail_input to resume polling. */
1523 poll_suppress_count++;
1524 if (interrupt_input_blocked == 0
1525 && !waiting_for_input)
1526 read_avail_input (0);
1527 /* Turn on the SIGALRM handler and request another alarm. */
1528 start_polling ();
1531 #endif
1533 /* Begin signals to poll for input, if they are appropriate.
1534 This function is called unconditionally from various places. */
1536 start_polling ()
1538 #ifdef POLL_FOR_INPUT
1539 if (read_socket_hook && !interrupt_input)
1541 poll_suppress_count--;
1542 if (poll_suppress_count == 0)
1544 signal (SIGALRM, input_poll_signal);
1545 polling_for_input = 1;
1546 alarm (polling_period);
1549 #endif
1552 /* Nonzero if we are using polling to handle input asynchronously. */
1555 input_polling_used ()
1557 #ifdef POLL_FOR_INPUT
1558 return read_socket_hook && !interrupt_input;
1559 #else
1560 return 0;
1561 #endif
1564 /* Turn off polling. */
1566 stop_polling ()
1568 #ifdef POLL_FOR_INPUT
1569 if (read_socket_hook && !interrupt_input)
1571 if (poll_suppress_count == 0)
1573 polling_for_input = 0;
1574 alarm (0);
1576 poll_suppress_count++;
1578 #endif
1581 /* Set the value of poll_suppress_count to COUNT
1582 and start or stop polling accordingly. */
1584 void
1585 set_poll_suppress_count (count)
1586 int count;
1588 #ifdef POLL_FOR_INPUT
1589 if (count == 0 && poll_suppress_count != 0)
1591 poll_suppress_count = 1;
1592 start_polling ();
1594 else if (count != 0 && poll_suppress_count == 0)
1596 stop_polling ();
1598 poll_suppress_count = count;
1599 #endif
1602 /* Bind polling_period to a value at least N.
1603 But don't decrease it. */
1605 bind_polling_period (n)
1606 int n;
1608 #ifdef POLL_FOR_INPUT
1609 int new = polling_period;
1611 if (n > new)
1612 new = n;
1614 stop_polling ();
1615 specbind (Qpolling_period, make_number (new));
1616 /* Start a new alarm with the new period. */
1617 start_polling ();
1618 #endif
1621 /* Apply the control modifier to CHARACTER. */
1624 make_ctrl_char (c)
1625 int c;
1627 /* Save the upper bits here. */
1628 int upper = c & ~0177;
1630 c &= 0177;
1632 /* Everything in the columns containing the upper-case letters
1633 denotes a control character. */
1634 if (c >= 0100 && c < 0140)
1636 int oc = c;
1637 c &= ~0140;
1638 /* Set the shift modifier for a control char
1639 made from a shifted letter. But only for letters! */
1640 if (oc >= 'A' && oc <= 'Z')
1641 c |= shift_modifier;
1644 /* The lower-case letters denote control characters too. */
1645 else if (c >= 'a' && c <= 'z')
1646 c &= ~0140;
1648 /* Include the bits for control and shift
1649 only if the basic ASCII code can't indicate them. */
1650 else if (c >= ' ')
1651 c |= ctrl_modifier;
1653 /* Replace the high bits. */
1654 c |= (upper & ~ctrl_modifier);
1656 return c;
1661 /* Input of single characters from keyboard */
1663 Lisp_Object print_help ();
1664 static Lisp_Object kbd_buffer_get_event ();
1665 static void record_char ();
1667 #ifdef MULTI_KBOARD
1668 static jmp_buf wrong_kboard_jmpbuf;
1669 #endif
1671 /* read a character from the keyboard; call the redisplay if needed */
1672 /* commandflag 0 means do not do auto-saving, but do do redisplay.
1673 -1 means do not do redisplay, but do do autosaving.
1674 1 means do both. */
1676 /* The arguments MAPS and NMAPS are for menu prompting.
1677 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1679 PREV_EVENT is the previous input event, or nil if we are reading
1680 the first event of a key sequence.
1682 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
1683 if we used a mouse menu to read the input, or zero otherwise. If
1684 USED_MOUSE_MENU is null, we don't dereference it.
1686 Value is t if we showed a menu and the user rejected it. */
1688 Lisp_Object
1689 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
1690 int commandflag;
1691 int nmaps;
1692 Lisp_Object *maps;
1693 Lisp_Object prev_event;
1694 int *used_mouse_menu;
1696 register Lisp_Object c;
1697 int count;
1698 jmp_buf local_getcjmp;
1699 jmp_buf save_jump;
1700 int key_already_recorded = 0;
1701 Lisp_Object tem, save;
1702 Lisp_Object also_record;
1703 also_record = Qnil;
1705 before_command_key_count = this_command_key_count;
1706 before_command_echo_length = echo_length ();
1708 retry:
1710 if (CONSP (Vunread_command_events))
1712 c = XCONS (Vunread_command_events)->car;
1713 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
1715 /* Undo what read_char_x_menu_prompt did when it unread
1716 additional keys returned by Fx_popup_menu. */
1717 if (CONSP (c)
1718 && (SYMBOLP (XCONS (c)->car) || INTEGERP (XCONS (c)->car))
1719 && NILP (XCONS (c)->cdr))
1720 c = XCONS (c)->car;
1722 if (this_command_key_count == 0)
1723 goto reread_first;
1724 else
1725 goto reread;
1728 if (unread_command_char != -1)
1730 XSETINT (c, unread_command_char);
1731 unread_command_char = -1;
1733 if (this_command_key_count == 0)
1734 goto reread_first;
1735 else
1736 goto reread;
1739 /* If there is no function key translated before
1740 reset-this-command-lengths takes effect, forget about it. */
1741 before_command_restore_flag = 0;
1743 if (!NILP (Vexecuting_macro))
1745 /* We set this to Qmacro; since that's not a frame, nobody will
1746 try to switch frames on us, and the selected window will
1747 remain unchanged.
1749 Since this event came from a macro, it would be misleading to
1750 leave internal_last_event_frame set to wherever the last
1751 real event came from. Normally, a switch-frame event selects
1752 internal_last_event_frame after each command is read, but
1753 events read from a macro should never cause a new frame to be
1754 selected. */
1755 Vlast_event_frame = internal_last_event_frame = Qmacro;
1757 /* Exit the macro if we are at the end.
1758 Also, some things replace the macro with t
1759 to force an early exit. */
1760 if (EQ (Vexecuting_macro, Qt)
1761 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
1763 XSETINT (c, -1);
1764 return c;
1767 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
1768 if (STRINGP (Vexecuting_macro)
1769 && (XINT (c) & 0x80))
1770 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
1772 executing_macro_index++;
1774 goto from_macro;
1777 if (!NILP (unread_switch_frame))
1779 c = unread_switch_frame;
1780 unread_switch_frame = Qnil;
1782 /* This event should make it into this_command_keys, and get echoed
1783 again, so we go to reread_first, rather than reread. */
1784 goto reread_first;
1787 if (commandflag >= 0 && !input_pending
1788 && !detect_input_pending_run_timers (0))
1789 redisplay ();
1791 /* Message turns off echoing unless more keystrokes turn it on again. */
1792 if (echo_area_glyphs && *echo_area_glyphs
1793 && echo_area_glyphs != current_kboard->echobuf
1794 && ok_to_echo_at_next_pause != echo_area_glyphs)
1795 cancel_echoing ();
1796 else
1797 /* If already echoing, continue. */
1798 echo_dash ();
1800 /* Try reading a character via menu prompting in the minibuf.
1801 Try this before the sit-for, because the sit-for
1802 would do the wrong thing if we are supposed to do
1803 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
1804 after a mouse event so don't try a minibuf menu. */
1805 c = Qnil;
1806 if (nmaps > 0 && INTERACTIVE
1807 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
1808 /* Don't bring up a menu if we already have another event. */
1809 && NILP (Vunread_command_events)
1810 && unread_command_char < 0
1811 && !detect_input_pending_run_timers (0))
1813 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
1814 if (! NILP (c))
1816 key_already_recorded = 1;
1817 goto non_reread_1;
1821 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
1822 We will do that below, temporarily for short sections of code,
1823 when appropriate. local_getcjmp must be in effect
1824 around any call to sit_for or kbd_buffer_get_event;
1825 it *must not* be in effect when we call redisplay. */
1827 if (_setjmp (local_getcjmp))
1829 XSETINT (c, quit_char);
1830 XSETFRAME (internal_last_event_frame, selected_frame);
1831 Vlast_event_frame = internal_last_event_frame;
1832 /* If we report the quit char as an event,
1833 don't do so more than once. */
1834 if (!NILP (Vinhibit_quit))
1835 Vquit_flag = Qnil;
1837 #ifdef MULTI_KBOARD
1839 KBOARD *kb = FRAME_KBOARD (selected_frame);
1840 if (kb != current_kboard)
1842 Lisp_Object *tailp = &kb->kbd_queue;
1843 /* We shouldn't get here if we were in single-kboard mode! */
1844 if (single_kboard)
1845 abort ();
1846 while (CONSP (*tailp))
1847 tailp = &XCONS (*tailp)->cdr;
1848 if (!NILP (*tailp))
1849 abort ();
1850 *tailp = Fcons (c, Qnil);
1851 kb->kbd_queue_has_data = 1;
1852 current_kboard = kb;
1853 longjmp (wrong_kboard_jmpbuf, 1);
1856 #endif
1857 goto non_reread;
1860 timer_start_idle ();
1862 /* If in middle of key sequence and minibuffer not active,
1863 start echoing if enough time elapses. */
1865 if (minibuf_level == 0 && !current_kboard->immediate_echo
1866 && this_command_key_count > 0
1867 && ! noninteractive
1868 && echo_keystrokes > 0
1869 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0
1870 || ok_to_echo_at_next_pause == echo_area_glyphs))
1872 Lisp_Object tem0;
1874 /* After a mouse event, start echoing right away.
1875 This is because we are probably about to display a menu,
1876 and we don't want to delay before doing so. */
1877 if (EVENT_HAS_PARAMETERS (prev_event))
1878 echo_now ();
1879 else
1881 save_getcjmp (save_jump);
1882 restore_getcjmp (local_getcjmp);
1883 tem0 = sit_for (echo_keystrokes, 0, 1, 1);
1884 restore_getcjmp (save_jump);
1885 if (EQ (tem0, Qt))
1886 echo_now ();
1890 /* Maybe auto save due to number of keystrokes. */
1892 if (commandflag != 0
1893 && auto_save_interval > 0
1894 && num_nonmacro_input_chars - last_auto_save > max (auto_save_interval, 20)
1895 && !detect_input_pending_run_timers (0))
1897 Fdo_auto_save (Qnil, Qnil);
1898 /* Hooks can actually change some buffers in auto save. */
1899 redisplay ();
1902 /* Try reading using an X menu.
1903 This is never confused with reading using the minibuf
1904 because the recursive call of read_char in read_char_minibuf_menu_prompt
1905 does not pass on any keymaps. */
1907 if (nmaps > 0 && INTERACTIVE
1908 && !NILP (prev_event)
1909 && EVENT_HAS_PARAMETERS (prev_event)
1910 && !EQ (XCONS (prev_event)->car, Qmenu_bar)
1911 /* Don't bring up a menu if we already have another event. */
1912 && NILP (Vunread_command_events)
1913 && unread_command_char < 0)
1915 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
1917 /* Now that we have read an event, Emacs is not idle. */
1918 timer_stop_idle ();
1920 return c;
1923 /* Maybe autosave and/or garbage collect due to idleness. */
1925 if (INTERACTIVE && NILP (c))
1927 int delay_level, buffer_size;
1929 /* Slow down auto saves logarithmically in size of current buffer,
1930 and garbage collect while we're at it. */
1931 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
1932 last_non_minibuf_size = Z - BEG;
1933 buffer_size = (last_non_minibuf_size >> 8) + 1;
1934 delay_level = 0;
1935 while (buffer_size > 64)
1936 delay_level++, buffer_size -= buffer_size >> 2;
1937 if (delay_level < 4) delay_level = 4;
1938 /* delay_level is 4 for files under around 50k, 7 at 100k,
1939 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
1941 /* Auto save if enough time goes by without input. */
1942 if (commandflag != 0
1943 && num_nonmacro_input_chars > last_auto_save
1944 && INTEGERP (Vauto_save_timeout)
1945 && XINT (Vauto_save_timeout) > 0)
1947 Lisp_Object tem0;
1949 save_getcjmp (save_jump);
1950 restore_getcjmp (local_getcjmp);
1951 tem0 = sit_for (delay_level * XFASTINT (Vauto_save_timeout) / 4,
1952 0, 1, 1);
1953 restore_getcjmp (save_jump);
1955 if (EQ (tem0, Qt))
1957 Fdo_auto_save (Qnil, Qnil);
1959 /* If we have auto-saved and there is still no input
1960 available, garbage collect if there has been enough
1961 consing going on to make it worthwhile. */
1962 if (!detect_input_pending_run_timers (0)
1963 && consing_since_gc > gc_cons_threshold / 2)
1964 Fgarbage_collect ();
1966 redisplay ();
1971 /* Read something from current KBOARD's side queue, if possible. */
1973 if (NILP (c))
1975 if (current_kboard->kbd_queue_has_data)
1977 if (!CONSP (current_kboard->kbd_queue))
1978 abort ();
1979 c = XCONS (current_kboard->kbd_queue)->car;
1980 current_kboard->kbd_queue
1981 = XCONS (current_kboard->kbd_queue)->cdr;
1982 if (NILP (current_kboard->kbd_queue))
1983 current_kboard->kbd_queue_has_data = 0;
1984 input_pending = readable_events (0);
1985 if (EVENT_HAS_PARAMETERS (c)
1986 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
1987 internal_last_event_frame = XCONS (XCONS (c)->cdr)->car;
1988 Vlast_event_frame = internal_last_event_frame;
1992 #ifdef MULTI_KBOARD
1993 /* If current_kboard's side queue is empty check the other kboards.
1994 If one of them has data that we have not yet seen here,
1995 switch to it and process the data waiting for it.
1997 Note: if the events queued up for another kboard
1998 have already been seen here, and therefore are not a complete command,
1999 the kbd_queue_has_data field is 0, so we skip that kboard here.
2000 That's to avoid an infinite loop switching between kboards here. */
2001 if (NILP (c) && !single_kboard)
2003 KBOARD *kb;
2004 for (kb = all_kboards; kb; kb = kb->next_kboard)
2005 if (kb->kbd_queue_has_data)
2007 current_kboard = kb;
2008 longjmp (wrong_kboard_jmpbuf, 1);
2011 #endif
2013 wrong_kboard:
2015 stop_polling ();
2017 /* Finally, we read from the main queue,
2018 and if that gives us something we can't use yet, we put it on the
2019 appropriate side queue and try again. */
2021 if (NILP (c))
2023 KBOARD *kb;
2025 /* Actually read a character, waiting if necessary. */
2026 save_getcjmp (save_jump);
2027 restore_getcjmp (local_getcjmp);
2028 c = kbd_buffer_get_event (&kb, used_mouse_menu);
2029 restore_getcjmp (save_jump);
2031 #ifdef MULTI_KBOARD
2032 if (! NILP (c) && (kb != current_kboard))
2034 Lisp_Object *tailp = &kb->kbd_queue;
2035 while (CONSP (*tailp))
2036 tailp = &XCONS (*tailp)->cdr;
2037 if (!NILP (*tailp))
2038 abort ();
2039 *tailp = Fcons (c, Qnil);
2040 kb->kbd_queue_has_data = 1;
2041 c = Qnil;
2042 if (single_kboard)
2043 goto wrong_kboard;
2044 current_kboard = kb;
2045 longjmp (wrong_kboard_jmpbuf, 1);
2047 #endif
2050 /* Terminate Emacs in batch mode if at eof. */
2051 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
2052 Fkill_emacs (make_number (1));
2054 if (INTEGERP (c))
2056 /* Add in any extra modifiers, where appropriate. */
2057 if ((extra_keyboard_modifiers & CHAR_CTL)
2058 || ((extra_keyboard_modifiers & 0177) < ' '
2059 && (extra_keyboard_modifiers & 0177) != 0))
2060 XSETINT (c, make_ctrl_char (XINT (c)));
2062 /* Transfer any other modifier bits directly from
2063 extra_keyboard_modifiers to c. Ignore the actual character code
2064 in the low 16 bits of extra_keyboard_modifiers. */
2065 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
2068 non_reread:
2070 /* Now that we have read an event, Emacs is not idle--
2071 unless the event was a timer event (not used now). */
2072 if (! (CONSP (c) && EQ (XCONS (c)->car, Qtimer_event)))
2073 timer_stop_idle ();
2075 start_polling ();
2077 if (NILP (c))
2079 if (commandflag >= 0
2080 && !input_pending && !detect_input_pending_run_timers (0))
2081 redisplay ();
2083 goto wrong_kboard;
2086 non_reread_1:
2088 /* Buffer switch events are only for internal wakeups
2089 so don't show them to the user. */
2090 if (BUFFERP (c))
2091 return c;
2093 if (key_already_recorded)
2094 return c;
2096 /* Process special events within read_char
2097 and loop around to read another event. */
2098 save = Vquit_flag;
2099 Vquit_flag = Qnil;
2100 tem = get_keyelt (access_keymap (get_keymap_1 (Vspecial_event_map, 0, 0),
2101 c, 0, 0), 1);
2102 Vquit_flag = save;
2104 if (!NILP (tem))
2106 int was_locked = single_kboard;
2108 last_input_char = c;
2109 Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt);
2111 /* Resume allowing input from any kboard, if that was true before. */
2112 if (!was_locked)
2113 any_kboard_state ();
2115 goto retry;
2118 /* Wipe the echo area. */
2119 if (echo_area_glyphs)
2120 safe_run_hooks (Qecho_area_clear_hook);
2121 echo_area_glyphs = 0;
2123 /* Handle things that only apply to characters. */
2124 if (INTEGERP (c))
2126 /* If kbd_buffer_get_event gave us an EOF, return that. */
2127 if (XINT (c) == -1)
2128 return c;
2130 if (STRINGP (Vkeyboard_translate_table)
2131 && XSTRING (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2132 XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[XFASTINT (c)]);
2133 else if ((VECTORP (Vkeyboard_translate_table)
2134 && XVECTOR (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2135 || (CHAR_TABLE_P (Vkeyboard_translate_table)
2136 && CHAR_TABLE_ORDINARY_SLOTS > (unsigned) XFASTINT (c)))
2138 Lisp_Object d;
2139 d = Faref (Vkeyboard_translate_table, c);
2140 /* nil in keyboard-translate-table means no translation. */
2141 if (!NILP (d))
2142 c = d;
2146 /* If this event is a mouse click in the menu bar,
2147 return just menu-bar for now. Modify the mouse click event
2148 so we won't do this twice, then queue it up. */
2149 if (EVENT_HAS_PARAMETERS (c)
2150 && CONSP (XCONS (c)->cdr)
2151 && CONSP (EVENT_START (c))
2152 && CONSP (XCONS (EVENT_START (c))->cdr))
2154 Lisp_Object posn;
2156 posn = POSN_BUFFER_POSN (EVENT_START (c));
2157 /* Handle menu-bar events:
2158 insert the dummy prefix event `menu-bar'. */
2159 if (EQ (posn, Qmenu_bar))
2161 /* Change menu-bar to (menu-bar) as the event "position". */
2162 POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
2164 also_record = c;
2165 Vunread_command_events = Fcons (c, Vunread_command_events);
2166 c = posn;
2170 record_char (c);
2171 if (! NILP (also_record))
2172 record_char (also_record);
2174 from_macro:
2175 reread_first:
2176 before_command_key_count = this_command_key_count;
2177 before_command_echo_length = echo_length ();
2179 /* Don't echo mouse motion events. */
2180 if (echo_keystrokes
2181 && ! (EVENT_HAS_PARAMETERS (c)
2182 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
2184 echo_char (c);
2185 if (! NILP (also_record))
2186 echo_char (also_record);
2187 /* Once we reread a character, echoing can happen
2188 the next time we pause to read a new one. */
2189 ok_to_echo_at_next_pause = echo_area_glyphs;
2192 /* Record this character as part of the current key. */
2193 add_command_key (c);
2194 if (! NILP (also_record))
2195 add_command_key (also_record);
2197 /* Re-reading in the middle of a command */
2198 reread:
2199 last_input_char = c;
2200 num_input_chars++;
2202 /* Process the help character specially if enabled */
2203 if (!NILP (Vhelp_form) && help_char_p (c))
2205 Lisp_Object tem0;
2206 count = specpdl_ptr - specpdl;
2208 record_unwind_protect (Fset_window_configuration,
2209 Fcurrent_window_configuration (Qnil));
2211 tem0 = Feval (Vhelp_form);
2212 if (STRINGP (tem0))
2213 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
2215 cancel_echoing ();
2217 c = read_char (0, 0, 0, Qnil, 0);
2218 while (BUFFERP (c));
2219 /* Remove the help from the frame */
2220 unbind_to (count, Qnil);
2222 redisplay ();
2223 if (EQ (c, make_number (040)))
2225 cancel_echoing ();
2227 c = read_char (0, 0, 0, Qnil, 0);
2228 while (BUFFERP (c));
2232 return c;
2235 /* Record a key that came from a mouse menu.
2236 Record it for echoing, for this-command-keys, and so on. */
2238 static void
2239 record_menu_key (c)
2240 Lisp_Object c;
2242 /* Wipe the echo area. */
2243 echo_area_glyphs = 0;
2245 record_char (c);
2247 before_command_key_count = this_command_key_count;
2248 before_command_echo_length = echo_length ();
2250 /* Don't echo mouse motion events. */
2251 if (echo_keystrokes)
2253 echo_char (c);
2255 /* Once we reread a character, echoing can happen
2256 the next time we pause to read a new one. */
2257 ok_to_echo_at_next_pause = 0;
2260 /* Record this character as part of the current key. */
2261 add_command_key (c);
2263 /* Re-reading in the middle of a command */
2264 last_input_char = c;
2265 num_input_chars++;
2268 /* Return 1 if should recognize C as "the help character". */
2271 help_char_p (c)
2272 Lisp_Object c;
2274 Lisp_Object tail;
2276 if (EQ (c, Vhelp_char))
2277 return 1;
2278 for (tail = Vhelp_event_list; CONSP (tail); tail = XCONS (tail)->cdr)
2279 if (EQ (c, XCONS (tail)->car))
2280 return 1;
2281 return 0;
2284 /* Record the input event C in various ways. */
2286 static void
2287 record_char (c)
2288 Lisp_Object c;
2290 total_keys++;
2291 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
2292 if (++recent_keys_index >= NUM_RECENT_KEYS)
2293 recent_keys_index = 0;
2295 /* Write c to the dribble file. If c is a lispy event, write
2296 the event's symbol to the dribble file, in <brackets>. Bleaugh.
2297 If you, dear reader, have a better idea, you've got the source. :-) */
2298 if (dribble)
2300 if (INTEGERP (c))
2302 if (XUINT (c) < 0x100)
2303 putc (XINT (c), dribble);
2304 else
2305 fprintf (dribble, " 0x%x", (int) XUINT (c));
2307 else
2309 Lisp_Object dribblee;
2311 /* If it's a structured event, take the event header. */
2312 dribblee = EVENT_HEAD (c);
2314 if (SYMBOLP (dribblee))
2316 putc ('<', dribble);
2317 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
2318 XSYMBOL (dribblee)->name->size,
2319 dribble);
2320 putc ('>', dribble);
2324 fflush (dribble);
2327 store_kbd_macro_char (c);
2329 num_nonmacro_input_chars++;
2332 Lisp_Object
2333 print_help (object)
2334 Lisp_Object object;
2336 struct buffer *old = current_buffer;
2337 Fprinc (object, Qnil);
2338 set_buffer_internal (XBUFFER (Vstandard_output));
2339 call0 (intern ("help-mode"));
2340 set_buffer_internal (old);
2341 return Qnil;
2344 /* Copy out or in the info on where C-g should throw to.
2345 This is used when running Lisp code from within get_char,
2346 in case get_char is called recursively.
2347 See read_process_output. */
2349 save_getcjmp (temp)
2350 jmp_buf temp;
2352 bcopy (getcjmp, temp, sizeof getcjmp);
2355 restore_getcjmp (temp)
2356 jmp_buf temp;
2358 bcopy (temp, getcjmp, sizeof getcjmp);
2361 #ifdef HAVE_MOUSE
2363 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
2364 of this function. */
2366 static Lisp_Object
2367 tracking_off (old_value)
2368 Lisp_Object old_value;
2370 do_mouse_tracking = old_value;
2371 if (NILP (old_value))
2373 /* Redisplay may have been preempted because there was input
2374 available, and it assumes it will be called again after the
2375 input has been processed. If the only input available was
2376 the sort that we have just disabled, then we need to call
2377 redisplay. */
2378 if (!readable_events (1))
2380 redisplay_preserve_echo_area ();
2381 get_input_pending (&input_pending, 1);
2386 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
2387 "Evaluate BODY with mouse movement events enabled.\n\
2388 Within a `track-mouse' form, mouse motion generates input events that\n\
2389 you can read with `read-event'.\n\
2390 Normally, mouse motion is ignored.")
2391 (args)
2392 Lisp_Object args;
2394 int count = specpdl_ptr - specpdl;
2395 Lisp_Object val;
2397 record_unwind_protect (tracking_off, do_mouse_tracking);
2399 do_mouse_tracking = Qt;
2401 val = Fprogn (args);
2402 return unbind_to (count, val);
2405 /* If mouse has moved on some frame, return one of those frames.
2406 Return 0 otherwise. */
2408 static FRAME_PTR
2409 some_mouse_moved ()
2411 Lisp_Object tail, frame;
2413 FOR_EACH_FRAME (tail, frame)
2415 if (XFRAME (frame)->mouse_moved)
2416 return XFRAME (frame);
2419 return 0;
2422 #endif /* HAVE_MOUSE */
2424 /* Low level keyboard/mouse input.
2425 kbd_buffer_store_event places events in kbd_buffer, and
2426 kbd_buffer_get_event retrieves them. */
2428 /* Return true iff there are any events in the queue that read-char
2429 would return. If this returns false, a read-char would block. */
2430 static int
2431 readable_events (do_timers_now)
2432 int do_timers_now;
2434 if (do_timers_now)
2435 timer_check (do_timers_now);
2437 if (kbd_fetch_ptr != kbd_store_ptr)
2438 return 1;
2439 #ifdef HAVE_MOUSE
2440 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2441 return 1;
2442 #endif
2443 if (single_kboard)
2445 if (current_kboard->kbd_queue_has_data)
2446 return 1;
2448 else
2450 KBOARD *kb;
2451 for (kb = all_kboards; kb; kb = kb->next_kboard)
2452 if (kb->kbd_queue_has_data)
2453 return 1;
2455 return 0;
2458 /* Set this for debugging, to have a way to get out */
2459 int stop_character;
2461 #ifdef MULTI_KBOARD
2462 static KBOARD *
2463 event_to_kboard (event)
2464 struct input_event *event;
2466 Lisp_Object frame;
2467 frame = event->frame_or_window;
2468 if (CONSP (frame))
2469 frame = XCONS (frame)->car;
2470 else if (WINDOWP (frame))
2471 frame = WINDOW_FRAME (XWINDOW (frame));
2473 /* There are still some events that don't set this field.
2474 For now, just ignore the problem.
2475 Also ignore dead frames here. */
2476 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
2477 return 0;
2478 else
2479 return FRAME_KBOARD (XFRAME (frame));
2481 #endif
2483 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
2485 void
2486 kbd_buffer_store_event (event)
2487 register struct input_event *event;
2489 if (event->kind == no_event)
2490 abort ();
2492 if (event->kind == ascii_keystroke)
2494 register int c = event->code & 0377;
2496 if (event->modifiers & ctrl_modifier)
2497 c = make_ctrl_char (c);
2499 c |= (event->modifiers
2500 & (meta_modifier | alt_modifier
2501 | hyper_modifier | super_modifier));
2503 if (c == quit_char)
2505 extern SIGTYPE interrupt_signal ();
2506 #ifdef MULTI_KBOARD
2507 KBOARD *kb;
2508 struct input_event *sp;
2510 if (single_kboard
2511 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
2512 kb != current_kboard))
2514 kb->kbd_queue
2515 = Fcons (make_lispy_switch_frame (event->frame_or_window),
2516 Fcons (make_number (c), Qnil));
2517 kb->kbd_queue_has_data = 1;
2518 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
2520 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
2521 sp = kbd_buffer;
2523 if (event_to_kboard (sp) == kb)
2525 sp->kind = no_event;
2526 sp->frame_or_window = Qnil;
2529 return;
2531 #endif
2533 /* If this results in a quit_char being returned to Emacs as
2534 input, set Vlast_event_frame properly. If this doesn't
2535 get returned to Emacs as an event, the next event read
2536 will set Vlast_event_frame again, so this is safe to do. */
2538 Lisp_Object focus;
2540 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
2541 if (NILP (focus))
2542 focus = event->frame_or_window;
2543 internal_last_event_frame = focus;
2544 Vlast_event_frame = focus;
2547 last_event_timestamp = event->timestamp;
2548 interrupt_signal ();
2549 return;
2552 if (c && c == stop_character)
2554 sys_suspend ();
2555 return;
2558 /* Don't insert two buffer_switch_event's in a row.
2559 Just ignore the second one. */
2560 else if (event->kind == buffer_switch_event
2561 && kbd_fetch_ptr != kbd_store_ptr
2562 && kbd_store_ptr->kind == buffer_switch_event)
2563 return;
2565 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
2566 kbd_store_ptr = kbd_buffer;
2568 /* Don't let the very last slot in the buffer become full,
2569 since that would make the two pointers equal,
2570 and that is indistinguishable from an empty buffer.
2571 Discard the event if it would fill the last slot. */
2572 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
2574 volatile struct input_event *sp = kbd_store_ptr;
2575 sp->kind = event->kind;
2576 if (event->kind == selection_request_event)
2578 /* We must not use the ordinary copying code for this case,
2579 since `part' is an enum and copying it might not copy enough
2580 in this case. */
2581 bcopy (event, (char *) sp, sizeof (*event));
2583 else
2585 sp->code = event->code;
2586 sp->part = event->part;
2587 sp->frame_or_window = event->frame_or_window;
2588 sp->modifiers = event->modifiers;
2589 sp->x = event->x;
2590 sp->y = event->y;
2591 sp->timestamp = event->timestamp;
2593 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
2594 - kbd_buffer]
2595 = event->frame_or_window);
2597 kbd_store_ptr++;
2601 /* Read one event from the event buffer, waiting if necessary.
2602 The value is a Lisp object representing the event.
2603 The value is nil for an event that should be ignored,
2604 or that was handled here.
2605 We always read and discard one event. */
2607 static Lisp_Object
2608 kbd_buffer_get_event (kbp, used_mouse_menu)
2609 KBOARD **kbp;
2610 int *used_mouse_menu;
2612 register int c;
2613 Lisp_Object obj;
2614 EMACS_TIME next_timer_delay;
2616 if (noninteractive)
2618 c = getchar ();
2619 XSETINT (obj, c);
2620 *kbp = current_kboard;
2621 return obj;
2624 /* Wait until there is input available. */
2625 for (;;)
2627 if (kbd_fetch_ptr != kbd_store_ptr)
2628 break;
2629 #ifdef HAVE_MOUSE
2630 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2631 break;
2632 #endif
2634 /* If the quit flag is set, then read_char will return
2635 quit_char, so that counts as "available input." */
2636 if (!NILP (Vquit_flag))
2637 quit_throw_to_read_char ();
2639 /* One way or another, wait until input is available; then, if
2640 interrupt handlers have not read it, read it now. */
2642 #ifdef OLDVMS
2643 wait_for_kbd_input ();
2644 #else
2645 /* Note SIGIO has been undef'd if FIONREAD is missing. */
2646 #ifdef SIGIO
2647 gobble_input (0);
2648 #endif /* SIGIO */
2649 if (kbd_fetch_ptr != kbd_store_ptr)
2650 break;
2651 #ifdef HAVE_MOUSE
2652 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2653 break;
2654 #endif
2656 Lisp_Object minus_one;
2658 XSETINT (minus_one, -1);
2659 wait_reading_process_input (0, 0, minus_one, 1);
2661 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
2662 /* Pass 1 for EXPECT since we just waited to have input. */
2663 read_avail_input (1);
2665 #endif /* not VMS */
2668 /* At this point, we know that there is a readable event available
2669 somewhere. If the event queue is empty, then there must be a
2670 mouse movement enabled and available. */
2671 if (kbd_fetch_ptr != kbd_store_ptr)
2673 struct input_event *event;
2675 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2676 ? kbd_fetch_ptr
2677 : kbd_buffer);
2679 last_event_timestamp = event->timestamp;
2681 #ifdef MULTI_KBOARD
2682 *kbp = event_to_kboard (event);
2683 if (*kbp == 0)
2684 *kbp = current_kboard; /* Better than returning null ptr? */
2685 #else
2686 *kbp = &the_only_kboard;
2687 #endif
2689 obj = Qnil;
2691 /* These two kinds of events get special handling
2692 and don't actually appear to the command loop.
2693 We return nil for them. */
2694 if (event->kind == selection_request_event)
2696 #ifdef HAVE_X11
2697 struct input_event copy;
2699 /* Remove it from the buffer before processing it,
2700 since otherwise swallow_events will see it
2701 and process it again. */
2702 copy = *event;
2703 kbd_fetch_ptr = event + 1;
2704 input_pending = readable_events (0);
2705 x_handle_selection_request (&copy);
2706 #else
2707 /* We're getting selection request events, but we don't have
2708 a window system. */
2709 abort ();
2710 #endif
2713 else if (event->kind == selection_clear_event)
2715 #ifdef HAVE_X11
2716 struct input_event copy;
2718 /* Remove it from the buffer before processing it. */
2719 copy = *event;
2720 kbd_fetch_ptr = event + 1;
2721 input_pending = readable_events (0);
2722 x_handle_selection_clear (&copy);
2723 #else
2724 /* We're getting selection request events, but we don't have
2725 a window system. */
2726 abort ();
2727 #endif
2729 #if defined (HAVE_X11) || defined (HAVE_NTGUI)
2730 else if (event->kind == delete_window_event)
2732 /* Make an event (delete-frame (FRAME)). */
2733 obj = Fcons (event->frame_or_window, Qnil);
2734 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
2735 kbd_fetch_ptr = event + 1;
2737 else if (event->kind == iconify_event)
2739 /* Make an event (iconify-frame (FRAME)). */
2740 obj = Fcons (event->frame_or_window, Qnil);
2741 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
2742 kbd_fetch_ptr = event + 1;
2744 else if (event->kind == deiconify_event)
2746 /* Make an event (make-frame-visible (FRAME)). */
2747 obj = Fcons (event->frame_or_window, Qnil);
2748 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
2749 kbd_fetch_ptr = event + 1;
2751 #endif
2752 else if (event->kind == buffer_switch_event)
2754 /* The value doesn't matter here; only the type is tested. */
2755 XSETBUFFER (obj, current_buffer);
2756 kbd_fetch_ptr = event + 1;
2758 #ifdef USE_X_TOOLKIT
2759 else if (event->kind == menu_bar_activate_event)
2761 kbd_fetch_ptr = event + 1;
2762 input_pending = readable_events (0);
2763 if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
2764 x_activate_menubar (XFRAME (event->frame_or_window));
2766 #endif
2767 /* Just discard these, by returning nil.
2768 With MULTI_KBOARD, these events are used as placeholders
2769 when we need to randomly delete events from the queue.
2770 (They shouldn't otherwise be found in the buffer,
2771 but on some machines it appears they do show up
2772 even without MULTI_KBOARD.) */
2773 else if (event->kind == no_event)
2774 kbd_fetch_ptr = event + 1;
2776 /* If this event is on a different frame, return a switch-frame this
2777 time, and leave the event in the queue for next time. */
2778 else
2780 Lisp_Object frame;
2781 Lisp_Object focus;
2783 frame = event->frame_or_window;
2784 if (CONSP (frame))
2785 frame = XCONS (frame)->car;
2786 else if (WINDOWP (frame))
2787 frame = WINDOW_FRAME (XWINDOW (frame));
2789 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
2790 if (! NILP (focus))
2791 frame = focus;
2793 if (! EQ (frame, internal_last_event_frame)
2794 && XFRAME (frame) != selected_frame)
2795 obj = make_lispy_switch_frame (frame);
2796 internal_last_event_frame = frame;
2798 /* If we didn't decide to make a switch-frame event, go ahead
2799 and build a real event from the queue entry. */
2801 if (NILP (obj))
2803 obj = make_lispy_event (event);
2804 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
2805 /* If this was a menu selection, then set the flag to inhibit
2806 writing to last_nonmenu_event. Don't do this if the event
2807 we're returning is (menu-bar), though; that indicates the
2808 beginning of the menu sequence, and we might as well leave
2809 that as the `event with parameters' for this selection. */
2810 if (event->kind == menu_bar_event
2811 && !(CONSP (obj) && EQ (XCONS (obj)->car, Qmenu_bar))
2812 && used_mouse_menu)
2813 *used_mouse_menu = 1;
2814 #endif
2816 /* Wipe out this event, to catch bugs. */
2817 event->kind = no_event;
2818 XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer] = Qnil;
2820 kbd_fetch_ptr = event + 1;
2824 #ifdef HAVE_MOUSE
2825 /* Try generating a mouse motion event. */
2826 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2828 FRAME_PTR f = some_mouse_moved ();
2829 Lisp_Object bar_window;
2830 enum scroll_bar_part part;
2831 Lisp_Object x, y;
2832 unsigned long time;
2834 *kbp = current_kboard;
2835 /* Note that this uses F to determine which display to look at.
2836 If there is no valid info, it does not store anything
2837 so x remains nil. */
2838 x = Qnil;
2839 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
2841 obj = Qnil;
2843 /* Decide if we should generate a switch-frame event. Don't
2844 generate switch-frame events for motion outside of all Emacs
2845 frames. */
2846 if (!NILP (x) && f)
2848 Lisp_Object frame;
2850 frame = FRAME_FOCUS_FRAME (f);
2851 if (NILP (frame))
2852 XSETFRAME (frame, f);
2854 if (! EQ (frame, internal_last_event_frame)
2855 && XFRAME (frame) != selected_frame)
2856 obj = make_lispy_switch_frame (frame);
2857 internal_last_event_frame = frame;
2860 /* If we didn't decide to make a switch-frame event, go ahead and
2861 return a mouse-motion event. */
2862 if (!NILP (x) && NILP (obj))
2863 obj = make_lispy_movement (f, bar_window, part, x, y, time);
2865 #endif /* HAVE_MOUSE */
2866 else
2867 /* We were promised by the above while loop that there was
2868 something for us to read! */
2869 abort ();
2871 input_pending = readable_events (0);
2873 Vlast_event_frame = internal_last_event_frame;
2875 return (obj);
2878 /* Process any events that are not user-visible,
2879 then return, without reading any user-visible events. */
2881 void
2882 swallow_events (do_display)
2883 int do_display;
2885 int old_timers_run;
2887 while (kbd_fetch_ptr != kbd_store_ptr)
2889 struct input_event *event;
2891 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2892 ? kbd_fetch_ptr
2893 : kbd_buffer);
2895 last_event_timestamp = event->timestamp;
2897 /* These two kinds of events get special handling
2898 and don't actually appear to the command loop. */
2899 if (event->kind == selection_request_event)
2901 #ifdef HAVE_X11
2902 struct input_event copy;
2904 /* Remove it from the buffer before processing it,
2905 since otherwise swallow_events called recursively could see it
2906 and process it again. */
2907 copy = *event;
2908 kbd_fetch_ptr = event + 1;
2909 input_pending = readable_events (0);
2910 x_handle_selection_request (&copy);
2911 #else
2912 /* We're getting selection request events, but we don't have
2913 a window system. */
2914 abort ();
2915 #endif
2918 else if (event->kind == selection_clear_event)
2920 #ifdef HAVE_X11
2921 struct input_event copy;
2923 /* Remove it from the buffer before processing it, */
2924 copy = *event;
2926 kbd_fetch_ptr = event + 1;
2927 input_pending = readable_events (0);
2928 x_handle_selection_clear (&copy);
2929 #else
2930 /* We're getting selection request events, but we don't have
2931 a window system. */
2932 abort ();
2933 #endif
2935 /* Note that timer_event is currently never used. */
2936 else if (event->kind == timer_event)
2938 Lisp_Object tem, lisp_event;
2939 int was_locked = single_kboard;
2941 tem = get_keymap_1 (Vspecial_event_map, 0, 0);
2942 tem = get_keyelt (access_keymap (tem, Qtimer_event, 0, 0),
2944 lisp_event = Fcons (Qtimer_event,
2945 Fcons (Fcdr (event->frame_or_window), Qnil));
2946 kbd_fetch_ptr = event + 1;
2947 if (kbd_fetch_ptr == kbd_store_ptr)
2948 input_pending = 0;
2949 Fcommand_execute (tem, Qnil, Fvector (1, &lisp_event), Qt);
2950 timers_run++;
2951 if (do_display)
2952 redisplay_preserve_echo_area ();
2954 /* Resume allowing input from any kboard, if that was true before. */
2955 if (!was_locked)
2956 any_kboard_state ();
2958 else
2959 break;
2962 old_timers_run = timers_run;
2963 get_input_pending (&input_pending, 1);
2965 if (timers_run != old_timers_run && do_display)
2966 redisplay_preserve_echo_area ();
2969 static EMACS_TIME timer_idleness_start_time;
2971 /* Record the start of when Emacs is idle,
2972 for the sake of running idle-time timers. */
2974 timer_start_idle ()
2976 Lisp_Object timers;
2978 /* If we are already in the idle state, do nothing. */
2979 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
2980 return;
2982 EMACS_GET_TIME (timer_idleness_start_time);
2984 /* Mark all idle-time timers as once again candidates for running. */
2985 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCONS (timers)->cdr)
2987 Lisp_Object timer;
2989 timer = XCONS (timers)->car;
2991 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
2992 continue;
2993 XVECTOR (timer)->contents[0] = Qnil;
2997 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
2999 timer_stop_idle ()
3001 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
3004 /* This is only for debugging. */
3005 struct input_event last_timer_event;
3007 /* Check whether a timer has fired. To prevent larger problems we simply
3008 disregard elements that are not proper timers. Do not make a circular
3009 timer list for the time being.
3011 Returns the number of seconds to wait until the next timer fires. If a
3012 timer is triggering now, return zero seconds.
3013 If no timer is active, return -1 seconds.
3015 If a timer is ripe, we run it, with quitting turned off.
3017 DO_IT_NOW is now ignored. It used to mean that we should
3018 run the timer directly instead of queueing a timer-event.
3019 Now we always run timers directly. */
3021 EMACS_TIME
3022 timer_check (do_it_now)
3023 int do_it_now;
3025 EMACS_TIME nexttime;
3026 EMACS_TIME now, idleness_now;
3027 Lisp_Object timers, idle_timers, chosen_timer;
3028 /* Nonzero if we generate some events. */
3029 int events_generated = 0;
3030 struct gcpro gcpro1, gcpro2, gcpro3;
3032 EMACS_SET_SECS (nexttime, -1);
3033 EMACS_SET_USECS (nexttime, -1);
3035 /* Always consider the ordinary timers. */
3036 timers = Vtimer_list;
3037 /* Consider the idle timers only if Emacs is idle. */
3038 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3039 idle_timers = Vtimer_idle_list;
3040 else
3041 idle_timers = Qnil;
3042 chosen_timer = Qnil;
3043 GCPRO3 (timers, idle_timers, chosen_timer);
3045 if (CONSP (timers) || CONSP (idle_timers))
3047 EMACS_GET_TIME (now);
3048 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3049 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
3052 while (CONSP (timers) || CONSP (idle_timers))
3054 int triggertime = EMACS_SECS (now);
3055 Lisp_Object *vector;
3056 Lisp_Object timer, idle_timer;
3057 EMACS_TIME timer_time, idle_timer_time;
3058 EMACS_TIME difference, timer_difference, idle_timer_difference;
3060 /* Skip past invalid timers and timers already handled. */
3061 if (!NILP (timers))
3063 timer = XCONS (timers)->car;
3064 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3066 timers = XCONS (timers)->cdr;
3067 continue;
3069 vector = XVECTOR (timer)->contents;
3071 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3072 || !INTEGERP (vector[3])
3073 || ! NILP (vector[0]))
3075 timers = XCONS (timers)->cdr;
3076 continue;
3079 if (!NILP (idle_timers))
3081 timer = XCONS (idle_timers)->car;
3082 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3084 idle_timers = XCONS (idle_timers)->cdr;
3085 continue;
3087 vector = XVECTOR (timer)->contents;
3089 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3090 || !INTEGERP (vector[3])
3091 || ! NILP (vector[0]))
3093 idle_timers = XCONS (idle_timers)->cdr;
3094 continue;
3098 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
3099 based on the next ordinary timer.
3100 TIMER_DIFFERENCE is the distance in time from NOW to when
3101 this timer becomes ripe (negative if it's already ripe). */
3102 if (!NILP (timers))
3104 timer = XCONS (timers)->car;
3105 vector = XVECTOR (timer)->contents;
3106 EMACS_SET_SECS (timer_time,
3107 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3108 EMACS_SET_USECS (timer_time, XINT (vector[3]));
3109 EMACS_SUB_TIME (timer_difference, timer_time, now);
3112 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
3113 based on the next idle timer. */
3114 if (!NILP (idle_timers))
3116 idle_timer = XCONS (idle_timers)->car;
3117 vector = XVECTOR (idle_timer)->contents;
3118 EMACS_SET_SECS (idle_timer_time,
3119 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3120 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
3121 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
3124 /* Decide which timer is the next timer,
3125 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
3126 Also step down the list where we found that timer. */
3128 if (! NILP (timers) && ! NILP (idle_timers))
3130 EMACS_TIME temp;
3131 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
3132 if (EMACS_TIME_NEG_P (temp))
3134 chosen_timer = timer;
3135 timers = XCONS (timers)->cdr;
3136 difference = timer_difference;
3138 else
3140 chosen_timer = idle_timer;
3141 idle_timers = XCONS (idle_timers)->cdr;
3142 difference = idle_timer_difference;
3145 else if (! NILP (timers))
3147 chosen_timer = timer;
3148 timers = XCONS (timers)->cdr;
3149 difference = timer_difference;
3151 else
3153 chosen_timer = idle_timer;
3154 idle_timers = XCONS (idle_timers)->cdr;
3155 difference = idle_timer_difference;
3157 vector = XVECTOR (chosen_timer)->contents;
3159 /* If timer is rupe, run it if it hasn't been run. */
3160 if (EMACS_TIME_NEG_P (difference)
3161 || (EMACS_SECS (difference) == 0
3162 && EMACS_USECS (difference) == 0))
3164 if (NILP (vector[0]))
3166 /* Mark the timer as triggered to prevent problems if the lisp
3167 code fails to reschedule it right. */
3168 vector[0] = Qt;
3170 /* Run the timer or queue a timer event. */
3171 if (1)
3173 Lisp_Object tem, event;
3174 int was_locked = single_kboard;
3175 int count = specpdl_ptr - specpdl;
3177 specbind (Qinhibit_quit, Qt);
3179 tem = get_keymap_1 (Vspecial_event_map, 0, 0);
3180 tem = get_keyelt (access_keymap (tem, Qtimer_event, 0, 0),
3182 event = Fcons (Qtimer_event, Fcons (chosen_timer, Qnil));
3183 Fcommand_execute (tem, Qnil, Fvector (1, &event), Qt);
3184 timers_run++;
3186 unbind_to (count, Qnil);
3188 /* Resume allowing input from any kboard, if that was true before. */
3189 if (!was_locked)
3190 any_kboard_state ();
3192 /* Since we have handled the event,
3193 we don't need to tell the caller to wake up and do it. */
3195 #if 0
3196 else
3198 /* Generate a timer event so the caller will handle it. */
3199 struct input_event event;
3201 event.kind = timer_event;
3202 event.modifiers = 0;
3203 event.x = event.y = Qnil;
3204 event.timestamp = triggertime;
3205 /* Store the timer in the frame slot. */
3206 event.frame_or_window
3207 = Fcons (Fselected_frame (), chosen_timer);
3208 kbd_buffer_store_event (&event);
3210 last_timer_event = event;
3212 /* Tell caller to handle this event right away. */
3213 events_generated = 1;
3214 EMACS_SET_SECS (nexttime, 0);
3215 EMACS_SET_USECS (nexttime, 0);
3217 /* Don't queue more than one event at once.
3218 When Emacs is ready for another, it will
3219 queue the next one. */
3220 UNGCPRO;
3221 return nexttime;
3223 #endif /* 0 */
3226 else
3227 /* When we encounter a timer that is still waiting,
3228 return the amount of time to wait before it is ripe. */
3230 UNGCPRO;
3231 /* But if we generated an event,
3232 tell the caller to handle it now. */
3233 if (events_generated)
3234 return nexttime;
3235 return difference;
3239 /* No timers are pending in the future. */
3240 /* Return 0 if we generated an event, and -1 if not. */
3241 UNGCPRO;
3242 return nexttime;
3245 /* Caches for modify_event_symbol. */
3246 static Lisp_Object accent_key_syms;
3247 static Lisp_Object func_key_syms;
3248 static Lisp_Object mouse_syms;
3250 /* This is a list of keysym codes for special "accent" characters.
3251 It parallels lispy_accent_keys. */
3253 static int lispy_accent_codes[] =
3255 #ifdef XK_dead_circumflex
3256 XK_dead_circumflex,
3257 #else
3259 #endif
3260 #ifdef XK_dead_grave
3261 XK_dead_grave,
3262 #else
3264 #endif
3265 #ifdef XK_dead_tilde
3266 XK_dead_tilde,
3267 #else
3269 #endif
3270 #ifdef XK_dead_diaeresis
3271 XK_dead_diaeresis,
3272 #else
3274 #endif
3275 #ifdef XK_dead_macron
3276 XK_dead_macron,
3277 #else
3279 #endif
3280 #ifdef XK_dead_degree
3281 XK_dead_degree,
3282 #else
3284 #endif
3285 #ifdef XK_dead_acute
3286 XK_dead_acute,
3287 #else
3289 #endif
3290 #ifdef XK_dead_cedilla
3291 XK_dead_cedilla,
3292 #else
3294 #endif
3295 #ifdef XK_dead_breve
3296 XK_dead_breve,
3297 #else
3299 #endif
3300 #ifdef XK_dead_ogonek
3301 XK_dead_ogonek,
3302 #else
3304 #endif
3305 #ifdef XK_dead_caron
3306 XK_dead_caron,
3307 #else
3309 #endif
3310 #ifdef XK_dead_doubleacute
3311 XK_dead_doubleacute,
3312 #else
3314 #endif
3315 #ifdef XK_dead_abovedot
3316 XK_dead_abovedot,
3317 #else
3319 #endif
3322 /* This is a list of Lisp names for special "accent" characters.
3323 It parallels lispy_accent_codes. */
3325 static char *lispy_accent_keys[] =
3327 "dead-circumflex",
3328 "dead-grave",
3329 "dead-tilde",
3330 "dead-diaeresis",
3331 "dead-macron",
3332 "dead-degree",
3333 "dead-acute",
3334 "dead-cedilla",
3335 "dead-breve",
3336 "dead-ogonek",
3337 "dead-caron",
3338 "dead-doubleacute",
3339 "dead-abovedot",
3342 #ifdef HAVE_NTGUI
3343 #define FUNCTION_KEY_OFFSET 0x0
3345 char *lispy_function_keys[] =
3347 0, /* 0 */
3349 0, /* VK_LBUTTON 0x01 */
3350 0, /* VK_RBUTTON 0x02 */
3351 "cancel", /* VK_CANCEL 0x03 */
3352 0, /* VK_MBUTTON 0x04 */
3354 0, 0, 0, /* 0x05 .. 0x07 */
3356 "backspace", /* VK_BACK 0x08 */
3357 "tab", /* VK_TAB 0x09 */
3359 0, 0, /* 0x0A .. 0x0B */
3361 "clear", /* VK_CLEAR 0x0C */
3362 "return", /* VK_RETURN 0x0D */
3364 0, 0, /* 0x0E .. 0x0F */
3366 "shift", /* VK_SHIFT 0x10 */
3367 "control", /* VK_CONTROL 0x11 */
3368 "menu", /* VK_MENU 0x12 */
3369 "pause", /* VK_PAUSE 0x13 */
3370 "capital", /* VK_CAPITAL 0x14 */
3372 0, 0, 0, 0, 0, 0, /* 0x15 .. 0x1A */
3374 0, /* VK_ESCAPE 0x1B */
3376 0, 0, 0, 0, /* 0x1C .. 0x1F */
3378 0, /* VK_SPACE 0x20 */
3379 "prior", /* VK_PRIOR 0x21 */
3380 "next", /* VK_NEXT 0x22 */
3381 "end", /* VK_END 0x23 */
3382 "home", /* VK_HOME 0x24 */
3383 "left", /* VK_LEFT 0x25 */
3384 "up", /* VK_UP 0x26 */
3385 "right", /* VK_RIGHT 0x27 */
3386 "down", /* VK_DOWN 0x28 */
3387 "select", /* VK_SELECT 0x29 */
3388 "print", /* VK_PRINT 0x2A */
3389 "execute", /* VK_EXECUTE 0x2B */
3390 "snapshot", /* VK_SNAPSHOT 0x2C */
3391 "insert", /* VK_INSERT 0x2D */
3392 "delete", /* VK_DELETE 0x2E */
3393 "help", /* VK_HELP 0x2F */
3395 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
3397 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3399 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
3401 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
3403 0, 0, 0, 0, 0, 0, 0, 0, 0,
3404 0, 0, 0, 0, 0, 0, 0, 0, 0,
3405 0, 0, 0, 0, 0, 0, 0, 0,
3407 "lwindow", /* VK_LWIN 0x5B */
3408 "rwindow", /* VK_RWIN 0x5C */
3409 "apps", /* VK_APPS 0x5D */
3411 0, 0, /* 0x5E .. 0x5F */
3413 "kp-0", /* VK_NUMPAD0 0x60 */
3414 "kp-1", /* VK_NUMPAD1 0x61 */
3415 "kp-2", /* VK_NUMPAD2 0x62 */
3416 "kp-3", /* VK_NUMPAD3 0x63 */
3417 "kp-4", /* VK_NUMPAD4 0x64 */
3418 "kp-5", /* VK_NUMPAD5 0x65 */
3419 "kp-6", /* VK_NUMPAD6 0x66 */
3420 "kp-7", /* VK_NUMPAD7 0x67 */
3421 "kp-8", /* VK_NUMPAD8 0x68 */
3422 "kp-9", /* VK_NUMPAD9 0x69 */
3423 "kp-multiply", /* VK_MULTIPLY 0x6A */
3424 "kp-add", /* VK_ADD 0x6B */
3425 "kp-separator", /* VK_SEPARATOR 0x6C */
3426 "kp-subtract", /* VK_SUBTRACT 0x6D */
3427 "kp-decimal", /* VK_DECIMAL 0x6E */
3428 "kp-divide", /* VK_DIVIDE 0x6F */
3429 "f1", /* VK_F1 0x70 */
3430 "f2", /* VK_F2 0x71 */
3431 "f3", /* VK_F3 0x72 */
3432 "f4", /* VK_F4 0x73 */
3433 "f5", /* VK_F5 0x74 */
3434 "f6", /* VK_F6 0x75 */
3435 "f7", /* VK_F7 0x76 */
3436 "f8", /* VK_F8 0x77 */
3437 "f9", /* VK_F9 0x78 */
3438 "f10", /* VK_F10 0x79 */
3439 "f11", /* VK_F11 0x7A */
3440 "f12", /* VK_F12 0x7B */
3441 "f13", /* VK_F13 0x7C */
3442 "f14", /* VK_F14 0x7D */
3443 "f15", /* VK_F15 0x7E */
3444 "f16", /* VK_F16 0x7F */
3445 "f17", /* VK_F17 0x80 */
3446 "f18", /* VK_F18 0x81 */
3447 "f19", /* VK_F19 0x82 */
3448 "f20", /* VK_F20 0x83 */
3449 "f21", /* VK_F21 0x84 */
3450 "f22", /* VK_F22 0x85 */
3451 "f23", /* VK_F23 0x86 */
3452 "f24", /* VK_F24 0x87 */
3454 0, 0, 0, 0, /* 0x88 .. 0x8B */
3455 0, 0, 0, 0, /* 0x8C .. 0x8F */
3457 "kp-numlock", /* VK_NUMLOCK 0x90 */
3458 "scroll", /* VK_SCROLL 0x91 */
3460 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
3461 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
3462 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
3463 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
3464 "kp-end", /* VK_NUMPAD_END 0x96 */
3465 "kp-home", /* VK_NUMPAD_HOME 0x97 */
3466 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
3467 "kp-up", /* VK_NUMPAD_UP 0x99 */
3468 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
3469 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
3470 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
3471 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
3473 0, 0, /* 0x9E .. 0x9F */
3476 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
3477 * Used only as parameters to GetAsyncKeyState() and GetKeyState().
3478 * No other API or message will distinguish left and right keys this way.
3480 /* 0xA0 .. 0xEF */
3482 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3483 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3484 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3485 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3486 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3488 /* 0xF0 .. 0xF5 */
3490 0, 0, 0, 0, 0, 0,
3492 "attn", /* VK_ATTN 0xF6 */
3493 "crsel", /* VK_CRSEL 0xF7 */
3494 "exsel", /* VK_EXSEL 0xF8 */
3495 "ereof", /* VK_EREOF 0xF9 */
3496 "play", /* VK_PLAY 0xFA */
3497 "zoom", /* VK_ZOOM 0xFB */
3498 "noname", /* VK_NONAME 0xFC */
3499 "pa1", /* VK_PA1 0xFD */
3500 "oem_clear", /* VK_OEM_CLEAR 0xFE */
3503 #else /* not HAVE_NTGUI */
3505 #ifdef XK_kana_A
3506 static char *lispy_kana_keys[] =
3508 /* X Keysym value */
3509 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x400 .. 0x40f */
3510 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x410 .. 0x41f */
3511 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x420 .. 0x42f */
3512 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x430 .. 0x43f */
3513 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x440 .. 0x44f */
3514 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x450 .. 0x45f */
3515 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x460 .. 0x46f */
3516 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0,
3517 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x480 .. 0x48f */
3518 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x490 .. 0x49f */
3519 0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket",
3520 "kana-comma", "kana-conjunctive", "kana-WO", "kana-a",
3521 "kana-i", "kana-u", "kana-e", "kana-o",
3522 "kana-ya", "kana-yu", "kana-yo", "kana-tsu",
3523 "prolongedsound", "kana-A", "kana-I", "kana-U",
3524 "kana-E", "kana-O", "kana-KA", "kana-KI",
3525 "kana-KU", "kana-KE", "kana-KO", "kana-SA",
3526 "kana-SHI", "kana-SU", "kana-SE", "kana-SO",
3527 "kana-TA", "kana-CHI", "kana-TSU", "kana-TE",
3528 "kana-TO", "kana-NA", "kana-NI", "kana-NU",
3529 "kana-NE", "kana-NO", "kana-HA", "kana-HI",
3530 "kana-FU", "kana-HE", "kana-HO", "kana-MA",
3531 "kana-MI", "kana-MU", "kana-ME", "kana-MO",
3532 "kana-YA", "kana-YU", "kana-YO", "kana-RA",
3533 "kana-RI", "kana-RU", "kana-RE", "kana-RO",
3534 "kana-WA", "kana-N", "voicedsound", "semivoicedsound",
3535 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4e0 .. 0x4ef */
3536 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f0 .. 0x4ff */
3538 #endif /* XK_kana_A */
3540 #define FUNCTION_KEY_OFFSET 0xff00
3542 /* You'll notice that this table is arranged to be conveniently
3543 indexed by X Windows keysym values. */
3544 static char *lispy_function_keys[] =
3546 /* X Keysym value */
3548 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00 */
3549 "backspace",
3550 "tab",
3551 "linefeed",
3552 "clear",
3554 "return",
3555 0, 0,
3556 0, 0, 0, /* 0xff10 */
3557 "pause",
3558 0, 0, 0, 0, 0, 0, 0,
3559 "escape",
3560 0, 0, 0, 0,
3561 0, "kanji", "muhenkan",
3562 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff20...2f */
3563 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff30...3f */
3564 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
3566 "home", /* 0xff50 */ /* IsCursorKey */
3567 "left",
3568 "up",
3569 "right",
3570 "down",
3571 "prior",
3572 "next",
3573 "end",
3574 "begin",
3575 0, /* 0xff59 */
3576 0, 0, 0, 0, 0, 0,
3577 "select", /* 0xff60 */ /* IsMiscFunctionKey */
3578 "print",
3579 "execute",
3580 "insert",
3581 0, /* 0xff64 */
3582 "undo",
3583 "redo",
3584 "menu",
3585 "find",
3586 "cancel",
3587 "help",
3588 "break", /* 0xff6b */
3590 0, 0, 0, 0, 0, 0, 0, 0, "backtab", 0,
3591 0, /* 0xff76 */
3592 0, 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff7f */
3593 "kp-space", /* 0xff80 */ /* IsKeypadKey */
3594 0, 0, 0, 0, 0, 0, 0, 0,
3595 "kp-tab", /* 0xff89 */
3596 0, 0, 0,
3597 "kp-enter", /* 0xff8d */
3598 0, 0, 0,
3599 "kp-f1", /* 0xff91 */
3600 "kp-f2",
3601 "kp-f3",
3602 "kp-f4",
3603 "kp-home", /* 0xff95 */
3604 "kp-left",
3605 "kp-up",
3606 "kp-right",
3607 "kp-down",
3608 "kp-prior", /* kp-page-up */
3609 "kp-next", /* kp-page-down */
3610 "kp-end",
3611 "kp-begin",
3612 "kp-insert",
3613 "kp-delete",
3614 0, /* 0xffa0 */
3615 0, 0, 0, 0, 0, 0, 0, 0, 0,
3616 "kp-multiply", /* 0xffaa */
3617 "kp-add",
3618 "kp-separator",
3619 "kp-subtract",
3620 "kp-decimal",
3621 "kp-divide", /* 0xffaf */
3622 "kp-0", /* 0xffb0 */
3623 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
3624 0, /* 0xffba */
3625 0, 0,
3626 "kp-equal", /* 0xffbd */
3627 "f1", /* 0xffbe */ /* IsFunctionKey */
3628 "f2",
3629 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
3630 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
3631 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
3632 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
3633 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
3634 0, 0, 0, 0, 0, 0, 0, 0,
3635 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
3636 0, 0, 0, 0, 0, 0, 0, "delete"
3639 /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE. */
3640 #define ISO_FUNCTION_KEY_OFFSET 0xfe00
3642 static char *iso_lispy_function_keys[] =
3644 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe00 */
3645 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe08 */
3646 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe10 */
3647 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe18 */
3648 "iso-lefttab", /* 0xfe20 */
3649 "iso-move-line-up", "iso-move-line-down",
3650 "iso-partial-line-up", "iso-partial-line-down",
3651 "iso-partial-space-left", "iso-partial-space-right",
3652 "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */
3653 "iso-release-margin-left", "iso-release-margin-right",
3654 "iso-release-both-margins",
3655 "iso-fast-cursor-left", "iso-fast-cursor-right",
3656 "iso-fast-cursor-up", "iso-fast-cursor-down",
3657 "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */
3658 "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */
3661 #endif /* not HAVE_NTGUI */
3663 static char *lispy_mouse_names[] =
3665 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
3668 /* Scroll bar parts. */
3669 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
3670 Lisp_Object Qup, Qdown;
3672 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
3673 Lisp_Object *scroll_bar_parts[] = {
3674 &Qabove_handle, &Qhandle, &Qbelow_handle,
3675 &Qup, &Qdown,
3679 /* A vector, indexed by button number, giving the down-going location
3680 of currently depressed buttons, both scroll bar and non-scroll bar.
3682 The elements have the form
3683 (BUTTON-NUMBER MODIFIER-MASK . REST)
3684 where REST is the cdr of a position as it would be reported in the event.
3686 The make_lispy_event function stores positions here to tell the
3687 difference between click and drag events, and to store the starting
3688 location to be included in drag events. */
3690 static Lisp_Object button_down_location;
3692 /* Information about the most recent up-going button event: Which
3693 button, what location, and what time. */
3695 static int last_mouse_button;
3696 static int last_mouse_x;
3697 static int last_mouse_y;
3698 static unsigned long button_down_time;
3700 /* The maximum time between clicks to make a double-click,
3701 or Qnil to disable double-click detection,
3702 or Qt for no time limit. */
3703 Lisp_Object Vdouble_click_time;
3705 /* The number of clicks in this multiple-click. */
3707 int double_click_count;
3709 /* Given a struct input_event, build the lisp event which represents
3710 it. If EVENT is 0, build a mouse movement event from the mouse
3711 movement buffer, which should have a movement event in it.
3713 Note that events must be passed to this function in the order they
3714 are received; this function stores the location of button presses
3715 in order to build drag events when the button is released. */
3717 static Lisp_Object
3718 make_lispy_event (event)
3719 struct input_event *event;
3721 int i;
3723 switch (SWITCH_ENUM_CAST (event->kind))
3725 /* A simple keystroke. */
3726 case ascii_keystroke:
3728 Lisp_Object lispy_c;
3729 int c = event->code & 0377;
3730 /* Turn ASCII characters into control characters
3731 when proper. */
3732 if (event->modifiers & ctrl_modifier)
3733 c = make_ctrl_char (c);
3735 /* Add in the other modifier bits. We took care of ctrl_modifier
3736 just above, and the shift key was taken care of by the X code,
3737 and applied to control characters by make_ctrl_char. */
3738 c |= (event->modifiers
3739 & (meta_modifier | alt_modifier
3740 | hyper_modifier | super_modifier));
3741 button_down_time = 0;
3742 XSETFASTINT (lispy_c, c);
3743 return lispy_c;
3746 /* A function key. The symbol may need to have modifier prefixes
3747 tacked onto it. */
3748 case non_ascii_keystroke:
3749 button_down_time = 0;
3751 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
3752 if (event->code == lispy_accent_codes[i])
3753 return modify_event_symbol (i,
3754 event->modifiers,
3755 Qfunction_key, Qnil,
3756 lispy_accent_keys, &accent_key_syms,
3757 (sizeof (lispy_accent_keys)
3758 / sizeof (lispy_accent_keys[0])));
3760 /* Handle system-specific keysyms. */
3761 if (event->code & (1 << 28))
3763 /* We need to use an alist rather than a vector as the cache
3764 since we can't make a vector long enuf. */
3765 if (NILP (current_kboard->system_key_syms))
3766 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
3767 return modify_event_symbol (event->code,
3768 event->modifiers,
3769 Qfunction_key,
3770 current_kboard->Vsystem_key_alist,
3771 0, &current_kboard->system_key_syms,
3772 (unsigned)-1);
3775 #ifdef XK_kana_A
3776 if (event->code >= 0x400 && event->code < 0x500)
3777 return modify_event_symbol (event->code - 0x400,
3778 event->modifiers & ~shift_modifier,
3779 Qfunction_key, Qnil,
3780 lispy_kana_keys, &func_key_syms,
3781 (sizeof (lispy_kana_keys)
3782 / sizeof (lispy_kana_keys[0])));
3783 #endif /* XK_kana_A */
3785 if (event->code < FUNCTION_KEY_OFFSET
3786 && event->code >= ISO_FUNCTION_KEY_OFFSET)
3787 return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET,
3788 event->modifiers,
3789 Qfunction_key, Qnil,
3790 iso_lispy_function_keys, &func_key_syms,
3791 (sizeof (iso_lispy_function_keys)
3792 / sizeof (iso_lispy_function_keys[0])));
3793 else
3794 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
3795 event->modifiers,
3796 Qfunction_key, Qnil,
3797 lispy_function_keys, &func_key_syms,
3798 (sizeof (lispy_function_keys)
3799 / sizeof (lispy_function_keys[0])));
3801 /* Note that timer_event is currently never used. */
3802 case timer_event:
3803 return Fcons (Qtimer_event, Fcons (Fcdr (event->frame_or_window), Qnil));
3805 #ifdef HAVE_MOUSE
3806 /* A mouse click. Figure out where it is, decide whether it's
3807 a press, click or drag, and build the appropriate structure. */
3808 case mouse_click:
3809 case scroll_bar_click:
3811 int button = event->code;
3812 int is_double;
3813 Lisp_Object position;
3814 Lisp_Object *start_pos_ptr;
3815 Lisp_Object start_pos;
3817 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
3818 abort ();
3820 /* Build the position as appropriate for this mouse click. */
3821 if (event->kind == mouse_click)
3823 int part;
3824 FRAME_PTR f = XFRAME (event->frame_or_window);
3825 Lisp_Object window;
3826 Lisp_Object posn;
3827 int row, column;
3829 /* Ignore mouse events that were made on frame that
3830 have been deleted. */
3831 if (! FRAME_LIVE_P (f))
3832 return Qnil;
3834 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
3835 &column, &row, NULL, 1);
3837 #ifndef USE_X_TOOLKIT
3838 /* In the non-toolkit version, clicks on the menu bar
3839 are ordinary button events in the event buffer.
3840 Distinguish them, and invoke the menu.
3842 (In the toolkit version, the toolkit handles the menu bar
3843 and Emacs doesn't know about it until after the user
3844 makes a selection.) */
3845 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
3846 && (event->modifiers & down_modifier))
3848 Lisp_Object items, item;
3849 int hpos;
3850 int i;
3852 #if 0
3853 /* Activate the menu bar on the down event. If the
3854 up event comes in before the menu code can deal with it,
3855 just ignore it. */
3856 if (! (event->modifiers & down_modifier))
3857 return Qnil;
3858 #endif
3860 item = Qnil;
3861 items = FRAME_MENU_BAR_ITEMS (f);
3862 for (i = 0; i < XVECTOR (items)->size; i += 4)
3864 Lisp_Object pos, string;
3865 string = XVECTOR (items)->contents[i + 1];
3866 pos = XVECTOR (items)->contents[i + 3];
3867 if (NILP (string))
3868 break;
3869 if (column >= XINT (pos)
3870 && column < XINT (pos) + XSTRING (string)->size)
3872 item = XVECTOR (items)->contents[i];
3873 break;
3877 position
3878 = Fcons (event->frame_or_window,
3879 Fcons (Qmenu_bar,
3880 Fcons (Fcons (event->x, event->y),
3881 Fcons (make_number (event->timestamp),
3882 Qnil))));
3884 return Fcons (item, Fcons (position, Qnil));
3886 #endif /* not USE_X_TOOLKIT */
3888 window = window_from_coordinates (f, column, row, &part);
3890 if (!WINDOWP (window))
3892 window = event->frame_or_window;
3893 posn = Qnil;
3895 else
3897 int pixcolumn, pixrow;
3898 column -= WINDOW_LEFT_MARGIN (XWINDOW (window));
3899 row -= XINT (XWINDOW (window)->top);
3900 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
3901 XSETINT (event->x, pixcolumn);
3902 XSETINT (event->y, pixrow);
3904 if (part == 1)
3905 posn = Qmode_line;
3906 else if (part == 2)
3907 posn = Qvertical_line;
3908 else
3909 XSETINT (posn,
3910 buffer_posn_from_coords (XWINDOW (window),
3911 column, row));
3914 position
3915 = Fcons (window,
3916 Fcons (posn,
3917 Fcons (Fcons (event->x, event->y),
3918 Fcons (make_number (event->timestamp),
3919 Qnil))));
3921 else
3923 Lisp_Object window;
3924 Lisp_Object portion_whole;
3925 Lisp_Object part;
3927 window = event->frame_or_window;
3928 portion_whole = Fcons (event->x, event->y);
3929 part = *scroll_bar_parts[(int) event->part];
3931 position
3932 = Fcons (window,
3933 Fcons (Qvertical_scroll_bar,
3934 Fcons (portion_whole,
3935 Fcons (make_number (event->timestamp),
3936 Fcons (part, Qnil)))));
3939 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
3941 start_pos = *start_pos_ptr;
3942 *start_pos_ptr = Qnil;
3944 is_double = (button == last_mouse_button
3945 && XINT (event->x) == last_mouse_x
3946 && XINT (event->y) == last_mouse_y
3947 && button_down_time != 0
3948 && (EQ (Vdouble_click_time, Qt)
3949 || (INTEGERP (Vdouble_click_time)
3950 && ((int)(event->timestamp - button_down_time)
3951 < XINT (Vdouble_click_time)))));
3952 last_mouse_button = button;
3953 last_mouse_x = XINT (event->x);
3954 last_mouse_y = XINT (event->y);
3956 /* If this is a button press, squirrel away the location, so
3957 we can decide later whether it was a click or a drag. */
3958 if (event->modifiers & down_modifier)
3960 if (is_double)
3962 double_click_count++;
3963 event->modifiers |= ((double_click_count > 2)
3964 ? triple_modifier
3965 : double_modifier);
3967 else
3968 double_click_count = 1;
3969 button_down_time = event->timestamp;
3970 *start_pos_ptr = Fcopy_alist (position);
3973 /* Now we're releasing a button - check the co-ordinates to
3974 see if this was a click or a drag. */
3975 else if (event->modifiers & up_modifier)
3977 /* If we did not see a down before this up,
3978 ignore the up. Probably this happened because
3979 the down event chose a menu item.
3980 It would be an annoyance to treat the release
3981 of the button that chose the menu item
3982 as a separate event. */
3984 if (!CONSP (start_pos))
3985 return Qnil;
3987 event->modifiers &= ~up_modifier;
3988 #if 0 /* Formerly we treated an up with no down as a click event. */
3989 if (!CONSP (start_pos))
3990 event->modifiers |= click_modifier;
3991 else
3992 #endif
3994 /* The third element of every position should be the (x,y)
3995 pair. */
3996 Lisp_Object down;
3998 down = Fnth (make_number (2), start_pos);
3999 if (EQ (event->x, XCONS (down)->car)
4000 && EQ (event->y, XCONS (down)->cdr))
4002 event->modifiers |= click_modifier;
4004 else
4006 button_down_time = 0;
4007 event->modifiers |= drag_modifier;
4009 /* Don't check is_double; treat this as multiple
4010 if the down-event was multiple. */
4011 if (double_click_count > 1)
4012 event->modifiers |= ((double_click_count > 2)
4013 ? triple_modifier
4014 : double_modifier);
4017 else
4018 /* Every mouse event should either have the down_modifier or
4019 the up_modifier set. */
4020 abort ();
4023 /* Get the symbol we should use for the mouse click. */
4024 Lisp_Object head;
4026 head = modify_event_symbol (button,
4027 event->modifiers,
4028 Qmouse_click, Qnil,
4029 lispy_mouse_names, &mouse_syms,
4030 (sizeof (lispy_mouse_names)
4031 / sizeof (lispy_mouse_names[0])));
4032 if (event->modifiers & drag_modifier)
4033 return Fcons (head,
4034 Fcons (start_pos,
4035 Fcons (position,
4036 Qnil)));
4037 else if (event->modifiers & (double_modifier | triple_modifier))
4038 return Fcons (head,
4039 Fcons (position,
4040 Fcons (make_number (double_click_count),
4041 Qnil)));
4042 else
4043 return Fcons (head,
4044 Fcons (position,
4045 Qnil));
4049 #ifdef WINDOWSNT
4050 case w32_scroll_bar_click:
4052 int button = event->code;
4053 int is_double;
4054 Lisp_Object position;
4055 Lisp_Object *start_pos_ptr;
4056 Lisp_Object start_pos;
4058 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
4059 abort ();
4062 Lisp_Object window;
4063 Lisp_Object portion_whole;
4064 Lisp_Object part;
4066 window = event->frame_or_window;
4067 portion_whole = Fcons (event->x, event->y);
4068 part = *scroll_bar_parts[(int) event->part];
4070 position =
4071 Fcons (window,
4072 Fcons (Qvertical_scroll_bar,
4073 Fcons (portion_whole,
4074 Fcons (make_number (event->timestamp),
4075 Fcons (part, Qnil)))));
4078 /* Always treat W32 scroll bar events as clicks. */
4079 event->modifiers |= click_modifier;
4082 /* Get the symbol we should use for the mouse click. */
4083 Lisp_Object head;
4085 head = modify_event_symbol (button,
4086 event->modifiers,
4087 Qmouse_click, Qnil,
4088 lispy_mouse_names, &mouse_syms,
4089 (sizeof (lispy_mouse_names)
4090 / sizeof (lispy_mouse_names[0])));
4091 return Fcons (head,
4092 Fcons (position,
4093 Qnil));
4096 #endif
4098 #endif /* HAVE_MOUSE */
4100 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
4101 case menu_bar_event:
4102 /* The event value is in the cdr of the frame_or_window slot. */
4103 if (!CONSP (event->frame_or_window))
4104 abort ();
4105 return XCONS (event->frame_or_window)->cdr;
4106 #endif
4108 /* The 'kind' field of the event is something we don't recognize. */
4109 default:
4110 abort ();
4114 #ifdef HAVE_MOUSE
4116 static Lisp_Object
4117 make_lispy_movement (frame, bar_window, part, x, y, time)
4118 FRAME_PTR frame;
4119 Lisp_Object bar_window;
4120 enum scroll_bar_part part;
4121 Lisp_Object x, y;
4122 unsigned long time;
4124 /* Is it a scroll bar movement? */
4125 if (frame && ! NILP (bar_window))
4127 Lisp_Object part_sym;
4129 part_sym = *scroll_bar_parts[(int) part];
4130 return Fcons (Qscroll_bar_movement,
4131 (Fcons (Fcons (bar_window,
4132 Fcons (Qvertical_scroll_bar,
4133 Fcons (Fcons (x, y),
4134 Fcons (make_number (time),
4135 Fcons (part_sym,
4136 Qnil))))),
4137 Qnil)));
4140 /* Or is it an ordinary mouse movement? */
4141 else
4143 int area;
4144 Lisp_Object window;
4145 Lisp_Object posn;
4146 int column, row;
4148 if (frame)
4150 /* It's in a frame; which window on that frame? */
4151 pixel_to_glyph_coords (frame, XINT (x), XINT (y), &column, &row,
4152 NULL, 1);
4153 window = window_from_coordinates (frame, column, row, &area);
4155 else
4156 window = Qnil;
4158 if (WINDOWP (window))
4160 int pixcolumn, pixrow;
4161 column -= WINDOW_LEFT_MARGIN (XWINDOW (window));
4162 row -= XINT (XWINDOW (window)->top);
4163 glyph_to_pixel_coords (frame, column, row, &pixcolumn, &pixrow);
4164 XSETINT (x, pixcolumn);
4165 XSETINT (y, pixrow);
4167 if (area == 1)
4168 posn = Qmode_line;
4169 else if (area == 2)
4170 posn = Qvertical_line;
4171 else
4172 XSETINT (posn,
4173 buffer_posn_from_coords (XWINDOW (window), column, row));
4175 else if (frame != 0)
4177 XSETFRAME (window, frame);
4178 posn = Qnil;
4180 else
4182 window = Qnil;
4183 posn = Qnil;
4184 XSETFASTINT (x, 0);
4185 XSETFASTINT (y, 0);
4188 return Fcons (Qmouse_movement,
4189 Fcons (Fcons (window,
4190 Fcons (posn,
4191 Fcons (Fcons (x, y),
4192 Fcons (make_number (time),
4193 Qnil)))),
4194 Qnil));
4198 #endif /* HAVE_MOUSE */
4200 /* Construct a switch frame event. */
4201 static Lisp_Object
4202 make_lispy_switch_frame (frame)
4203 Lisp_Object frame;
4205 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
4208 /* Manipulating modifiers. */
4210 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
4212 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
4213 SYMBOL's name of the end of the modifiers; the string from this
4214 position is the unmodified symbol name.
4216 This doesn't use any caches. */
4218 static int
4219 parse_modifiers_uncached (symbol, modifier_end)
4220 Lisp_Object symbol;
4221 int *modifier_end;
4223 struct Lisp_String *name;
4224 int i;
4225 int modifiers;
4227 CHECK_SYMBOL (symbol, 1);
4229 modifiers = 0;
4230 name = XSYMBOL (symbol)->name;
4232 for (i = 0; i+2 <= name->size; )
4234 int this_mod_end = 0;
4235 int this_mod = 0;
4237 /* See if the name continues with a modifier word.
4238 Check that the word appears, but don't check what follows it.
4239 Set this_mod and this_mod_end to record what we find. */
4241 switch (name->data[i])
4243 #define SINGLE_LETTER_MOD(BIT) \
4244 (this_mod_end = i + 1, this_mod = BIT)
4246 case 'A':
4247 SINGLE_LETTER_MOD (alt_modifier);
4248 break;
4250 case 'C':
4251 SINGLE_LETTER_MOD (ctrl_modifier);
4252 break;
4254 case 'H':
4255 SINGLE_LETTER_MOD (hyper_modifier);
4256 break;
4258 case 'M':
4259 SINGLE_LETTER_MOD (meta_modifier);
4260 break;
4262 case 'S':
4263 SINGLE_LETTER_MOD (shift_modifier);
4264 break;
4266 case 's':
4267 SINGLE_LETTER_MOD (super_modifier);
4268 break;
4270 #undef SINGLE_LETTER_MOD
4273 /* If we found no modifier, stop looking for them. */
4274 if (this_mod_end == 0)
4275 break;
4277 /* Check there is a dash after the modifier, so that it
4278 really is a modifier. */
4279 if (this_mod_end >= name->size || name->data[this_mod_end] != '-')
4280 break;
4282 /* This modifier is real; look for another. */
4283 modifiers |= this_mod;
4284 i = this_mod_end + 1;
4287 /* Should we include the `click' modifier? */
4288 if (! (modifiers & (down_modifier | drag_modifier
4289 | double_modifier | triple_modifier))
4290 && i + 7 == name->size
4291 && strncmp (name->data + i, "mouse-", 6) == 0
4292 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
4293 modifiers |= click_modifier;
4295 if (modifier_end)
4296 *modifier_end = i;
4298 return modifiers;
4301 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
4302 prepended to the string BASE[0..BASE_LEN-1].
4303 This doesn't use any caches. */
4304 static Lisp_Object
4305 apply_modifiers_uncached (modifiers, base, base_len)
4306 int modifiers;
4307 char *base;
4308 int base_len;
4310 /* Since BASE could contain nulls, we can't use intern here; we have
4311 to use Fintern, which expects a genuine Lisp_String, and keeps a
4312 reference to it. */
4313 char *new_mods =
4314 (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
4315 int mod_len;
4318 char *p = new_mods;
4320 /* Only the event queue may use the `up' modifier; it should always
4321 be turned into a click or drag event before presented to lisp code. */
4322 if (modifiers & up_modifier)
4323 abort ();
4325 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
4326 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
4327 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
4328 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
4329 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
4330 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
4331 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
4332 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
4333 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
4334 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
4335 /* The click modifier is denoted by the absence of other modifiers. */
4337 *p = '\0';
4339 mod_len = p - new_mods;
4343 Lisp_Object new_name;
4345 new_name = make_uninit_string (mod_len + base_len);
4346 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
4347 bcopy (base, XSTRING (new_name)->data + mod_len, base_len);
4349 return Fintern (new_name, Qnil);
4354 static char *modifier_names[] =
4356 "up", "down", "drag", "click", "double", "triple", 0, 0,
4357 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4358 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
4360 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
4362 static Lisp_Object modifier_symbols;
4364 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
4365 static Lisp_Object
4366 lispy_modifier_list (modifiers)
4367 int modifiers;
4369 Lisp_Object modifier_list;
4370 int i;
4372 modifier_list = Qnil;
4373 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
4374 if (modifiers & (1<<i))
4375 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
4376 modifier_list);
4378 return modifier_list;
4382 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
4383 where UNMODIFIED is the unmodified form of SYMBOL,
4384 MASK is the set of modifiers present in SYMBOL's name.
4385 This is similar to parse_modifiers_uncached, but uses the cache in
4386 SYMBOL's Qevent_symbol_element_mask property, and maintains the
4387 Qevent_symbol_elements property. */
4389 static Lisp_Object
4390 parse_modifiers (symbol)
4391 Lisp_Object symbol;
4393 Lisp_Object elements;
4395 elements = Fget (symbol, Qevent_symbol_element_mask);
4396 if (CONSP (elements))
4397 return elements;
4398 else
4400 int end;
4401 int modifiers = parse_modifiers_uncached (symbol, &end);
4402 Lisp_Object unmodified;
4403 Lisp_Object mask;
4405 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
4406 XSYMBOL (symbol)->name->size - end),
4407 Qnil);
4409 if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1))
4410 abort ();
4411 XSETFASTINT (mask, modifiers);
4412 elements = Fcons (unmodified, Fcons (mask, Qnil));
4414 /* Cache the parsing results on SYMBOL. */
4415 Fput (symbol, Qevent_symbol_element_mask,
4416 elements);
4417 Fput (symbol, Qevent_symbol_elements,
4418 Fcons (unmodified, lispy_modifier_list (modifiers)));
4420 /* Since we know that SYMBOL is modifiers applied to unmodified,
4421 it would be nice to put that in unmodified's cache.
4422 But we can't, since we're not sure that parse_modifiers is
4423 canonical. */
4425 return elements;
4429 /* Apply the modifiers MODIFIERS to the symbol BASE.
4430 BASE must be unmodified.
4432 This is like apply_modifiers_uncached, but uses BASE's
4433 Qmodifier_cache property, if present. It also builds
4434 Qevent_symbol_elements properties, since it has that info anyway.
4436 apply_modifiers copies the value of BASE's Qevent_kind property to
4437 the modified symbol. */
4438 static Lisp_Object
4439 apply_modifiers (modifiers, base)
4440 int modifiers;
4441 Lisp_Object base;
4443 Lisp_Object cache, index, entry, new_symbol;
4445 /* Mask out upper bits. We don't know where this value's been. */
4446 modifiers &= ((EMACS_INT)1 << VALBITS) - 1;
4448 /* The click modifier never figures into cache indices. */
4449 cache = Fget (base, Qmodifier_cache);
4450 XSETFASTINT (index, (modifiers & ~click_modifier));
4451 entry = assq_no_quit (index, cache);
4453 if (CONSP (entry))
4454 new_symbol = XCONS (entry)->cdr;
4455 else
4457 /* We have to create the symbol ourselves. */
4458 new_symbol = apply_modifiers_uncached (modifiers,
4459 XSYMBOL (base)->name->data,
4460 XSYMBOL (base)->name->size);
4462 /* Add the new symbol to the base's cache. */
4463 entry = Fcons (index, new_symbol);
4464 Fput (base, Qmodifier_cache, Fcons (entry, cache));
4466 /* We have the parsing info now for free, so add it to the caches. */
4467 XSETFASTINT (index, modifiers);
4468 Fput (new_symbol, Qevent_symbol_element_mask,
4469 Fcons (base, Fcons (index, Qnil)));
4470 Fput (new_symbol, Qevent_symbol_elements,
4471 Fcons (base, lispy_modifier_list (modifiers)));
4474 /* Make sure this symbol is of the same kind as BASE.
4476 You'd think we could just set this once and for all when we
4477 intern the symbol above, but reorder_modifiers may call us when
4478 BASE's property isn't set right; we can't assume that just
4479 because it has a Qmodifier_cache property it must have its
4480 Qevent_kind set right as well. */
4481 if (NILP (Fget (new_symbol, Qevent_kind)))
4483 Lisp_Object kind;
4485 kind = Fget (base, Qevent_kind);
4486 if (! NILP (kind))
4487 Fput (new_symbol, Qevent_kind, kind);
4490 return new_symbol;
4494 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
4495 return a symbol with the modifiers placed in the canonical order.
4496 Canonical order is alphabetical, except for down and drag, which
4497 always come last. The 'click' modifier is never written out.
4499 Fdefine_key calls this to make sure that (for example) C-M-foo
4500 and M-C-foo end up being equivalent in the keymap. */
4502 Lisp_Object
4503 reorder_modifiers (symbol)
4504 Lisp_Object symbol;
4506 /* It's hopefully okay to write the code this way, since everything
4507 will soon be in caches, and no consing will be done at all. */
4508 Lisp_Object parsed;
4510 parsed = parse_modifiers (symbol);
4511 return apply_modifiers ((int) XINT (XCONS (XCONS (parsed)->cdr)->car),
4512 XCONS (parsed)->car);
4516 /* For handling events, we often want to produce a symbol whose name
4517 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
4518 to some base, like the name of a function key or mouse button.
4519 modify_event_symbol produces symbols of this sort.
4521 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
4522 is the name of the i'th symbol. TABLE_SIZE is the number of elements
4523 in the table.
4525 Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
4526 NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
4528 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
4529 persist between calls to modify_event_symbol that it can use to
4530 store a cache of the symbols it's generated for this NAME_TABLE
4531 before. The object stored there may be a vector or an alist.
4533 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
4535 MODIFIERS is a set of modifier bits (as given in struct input_events)
4536 whose prefixes should be applied to the symbol name.
4538 SYMBOL_KIND is the value to be placed in the event_kind property of
4539 the returned symbol.
4541 The symbols we create are supposed to have an
4542 `event-symbol-elements' property, which lists the modifiers present
4543 in the symbol's name. */
4545 static Lisp_Object
4546 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
4547 name_table, symbol_table, table_size)
4548 int symbol_num;
4549 unsigned modifiers;
4550 Lisp_Object symbol_kind;
4551 Lisp_Object name_alist;
4552 char **name_table;
4553 Lisp_Object *symbol_table;
4554 unsigned int table_size;
4556 Lisp_Object value;
4557 Lisp_Object symbol_int;
4559 /* Get rid of the "vendor-specific" bit here. */
4560 XSETINT (symbol_int, symbol_num & 0xffffff);
4562 /* Is this a request for a valid symbol? */
4563 if (symbol_num < 0 || symbol_num >= table_size)
4564 return Qnil;
4566 if (CONSP (*symbol_table))
4567 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
4569 /* If *symbol_table doesn't seem to be initialized properly, fix that.
4570 *symbol_table should be a lisp vector TABLE_SIZE elements long,
4571 where the Nth element is the symbol for NAME_TABLE[N], or nil if
4572 we've never used that symbol before. */
4573 else
4575 if (! VECTORP (*symbol_table)
4576 || XVECTOR (*symbol_table)->size != table_size)
4578 Lisp_Object size;
4580 XSETFASTINT (size, table_size);
4581 *symbol_table = Fmake_vector (size, Qnil);
4584 value = XVECTOR (*symbol_table)->contents[symbol_num];
4587 /* Have we already used this symbol before? */
4588 if (NILP (value))
4590 /* No; let's create it. */
4591 if (!NILP (name_alist))
4592 value = Fcdr_safe (Fassq (symbol_int, name_alist));
4593 else if (name_table != 0 && name_table[symbol_num])
4594 value = intern (name_table[symbol_num]);
4596 #ifdef HAVE_WINDOW_SYSTEM
4597 if (NILP (value))
4599 char *name = x_get_keysym_name (symbol_num);
4600 if (name)
4601 value = intern (name);
4603 #endif
4605 if (NILP (value))
4607 char buf[20];
4608 sprintf (buf, "key-%d", symbol_num);
4609 value = intern (buf);
4612 if (CONSP (*symbol_table))
4613 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
4614 else
4615 XVECTOR (*symbol_table)->contents[symbol_num] = value;
4617 /* Fill in the cache entries for this symbol; this also
4618 builds the Qevent_symbol_elements property, which the user
4619 cares about. */
4620 apply_modifiers (modifiers & click_modifier, value);
4621 Fput (value, Qevent_kind, symbol_kind);
4624 /* Apply modifiers to that symbol. */
4625 return apply_modifiers (modifiers, value);
4628 /* Convert a list that represents an event type,
4629 such as (ctrl meta backspace), into the usual representation of that
4630 event type as a number or a symbol. */
4632 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
4633 "Convert the event description list EVENT-DESC to an event type.\n\
4634 EVENT-DESC should contain one base event type (a character or symbol)\n\
4635 and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\
4636 drag, down, double or triple). The base must be last.\n\
4637 The return value is an event type (a character or symbol) which\n\
4638 has the same base event type and all the specified modifiers.")
4639 (event_desc)
4640 Lisp_Object event_desc;
4642 Lisp_Object base;
4643 int modifiers = 0;
4644 Lisp_Object rest;
4646 base = Qnil;
4647 rest = event_desc;
4648 while (CONSP (rest))
4650 Lisp_Object elt;
4651 int this = 0;
4653 elt = XCONS (rest)->car;
4654 rest = XCONS (rest)->cdr;
4656 /* Given a symbol, see if it is a modifier name. */
4657 if (SYMBOLP (elt) && CONSP (rest))
4658 this = parse_solitary_modifier (elt);
4660 if (this != 0)
4661 modifiers |= this;
4662 else if (!NILP (base))
4663 error ("Two bases given in one event");
4664 else
4665 base = elt;
4669 /* Let the symbol A refer to the character A. */
4670 if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1)
4671 XSETINT (base, XSYMBOL (base)->name->data[0]);
4673 if (INTEGERP (base))
4675 /* Turn (shift a) into A. */
4676 if ((modifiers & shift_modifier) != 0
4677 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
4679 XSETINT (base, XINT (base) - ('a' - 'A'));
4680 modifiers &= ~shift_modifier;
4683 /* Turn (control a) into C-a. */
4684 if (modifiers & ctrl_modifier)
4685 return make_number ((modifiers & ~ctrl_modifier)
4686 | make_ctrl_char (XINT (base)));
4687 else
4688 return make_number (modifiers | XINT (base));
4690 else if (SYMBOLP (base))
4691 return apply_modifiers (modifiers, base);
4692 else
4693 error ("Invalid base event");
4696 /* Try to recognize SYMBOL as a modifier name.
4697 Return the modifier flag bit, or 0 if not recognized. */
4699 static int
4700 parse_solitary_modifier (symbol)
4701 Lisp_Object symbol;
4703 struct Lisp_String *name = XSYMBOL (symbol)->name;
4705 switch (name->data[0])
4707 #define SINGLE_LETTER_MOD(BIT) \
4708 if (name->size == 1) \
4709 return BIT;
4711 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
4712 if (LEN == name->size \
4713 && ! strncmp (name->data, NAME, LEN)) \
4714 return BIT;
4716 case 'A':
4717 SINGLE_LETTER_MOD (alt_modifier);
4718 break;
4720 case 'a':
4721 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
4722 break;
4724 case 'C':
4725 SINGLE_LETTER_MOD (ctrl_modifier);
4726 break;
4728 case 'c':
4729 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
4730 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
4731 break;
4733 case 'H':
4734 SINGLE_LETTER_MOD (hyper_modifier);
4735 break;
4737 case 'h':
4738 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
4739 break;
4741 case 'M':
4742 SINGLE_LETTER_MOD (meta_modifier);
4743 break;
4745 case 'm':
4746 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
4747 break;
4749 case 'S':
4750 SINGLE_LETTER_MOD (shift_modifier);
4751 break;
4753 case 's':
4754 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
4755 MULTI_LETTER_MOD (super_modifier, "super", 5);
4756 SINGLE_LETTER_MOD (super_modifier);
4757 break;
4759 case 'd':
4760 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
4761 MULTI_LETTER_MOD (down_modifier, "down", 4);
4762 MULTI_LETTER_MOD (double_modifier, "double", 6);
4763 break;
4765 case 't':
4766 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
4767 break;
4769 #undef SINGLE_LETTER_MOD
4770 #undef MULTI_LETTER_MOD
4773 return 0;
4776 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
4777 Such a list is not valid as an event,
4778 but it can be a Lucid-style event type list. */
4781 lucid_event_type_list_p (object)
4782 Lisp_Object object;
4784 Lisp_Object tail;
4786 if (! CONSP (object))
4787 return 0;
4789 for (tail = object; CONSP (tail); tail = XCONS (tail)->cdr)
4791 Lisp_Object elt;
4792 elt = XCONS (tail)->car;
4793 if (! (INTEGERP (elt) || SYMBOLP (elt)))
4794 return 0;
4797 return NILP (tail);
4800 /* Store into *addr a value nonzero if terminal input chars are available.
4801 Serves the purpose of ioctl (0, FIONREAD, addr)
4802 but works even if FIONREAD does not exist.
4803 (In fact, this may actually read some input.)
4805 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe. */
4807 static void
4808 get_input_pending (addr, do_timers_now)
4809 int *addr;
4810 int do_timers_now;
4812 /* First of all, have we already counted some input? */
4813 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
4815 /* If input is being read as it arrives, and we have none, there is none. */
4816 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
4817 return;
4819 /* Try to read some input and see how much we get. */
4820 gobble_input (0);
4821 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
4824 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
4827 gobble_input (expected)
4828 int expected;
4830 #ifndef VMS
4831 #ifdef SIGIO
4832 if (interrupt_input)
4834 SIGMASKTYPE mask;
4835 mask = sigblock (sigmask (SIGIO));
4836 read_avail_input (expected);
4837 sigsetmask (mask);
4839 else
4840 #ifdef POLL_FOR_INPUT
4841 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
4843 SIGMASKTYPE mask;
4844 mask = sigblock (sigmask (SIGALRM));
4845 read_avail_input (expected);
4846 sigsetmask (mask);
4848 else
4849 #endif
4850 #endif
4851 read_avail_input (expected);
4852 #endif
4855 /* Put a buffer_switch_event in the buffer
4856 so that read_key_sequence will notice the new current buffer. */
4858 record_asynch_buffer_change ()
4860 struct input_event event;
4861 Lisp_Object tem;
4863 event.kind = buffer_switch_event;
4864 event.frame_or_window = Qnil;
4866 #ifdef subprocesses
4867 /* We don't need a buffer-switch event unless Emacs is waiting for input.
4868 The purpose of the event is to make read_key_sequence look up the
4869 keymaps again. If we aren't in read_key_sequence, we don't need one,
4870 and the event could cause trouble by messing up (input-pending-p). */
4871 tem = Fwaiting_for_user_input_p ();
4872 if (NILP (tem))
4873 return;
4874 #else
4875 /* We never need these events if we have no asynchronous subprocesses. */
4876 return;
4877 #endif
4879 /* Make sure no interrupt happens while storing the event. */
4880 #ifdef SIGIO
4881 if (interrupt_input)
4883 SIGMASKTYPE mask;
4884 mask = sigblock (sigmask (SIGIO));
4885 kbd_buffer_store_event (&event);
4886 sigsetmask (mask);
4888 else
4889 #endif
4891 stop_polling ();
4892 kbd_buffer_store_event (&event);
4893 start_polling ();
4897 #ifndef VMS
4899 /* Read any terminal input already buffered up by the system
4900 into the kbd_buffer, but do not wait.
4902 EXPECTED should be nonzero if the caller knows there is some input.
4904 Except on VMS, all input is read by this function.
4905 If interrupt_input is nonzero, this function MUST be called
4906 only when SIGIO is blocked.
4908 Returns the number of keyboard chars read, or -1 meaning
4909 this is a bad time to try to read input. */
4911 static int
4912 read_avail_input (expected)
4913 int expected;
4915 struct input_event buf[KBD_BUFFER_SIZE];
4916 register int i;
4917 int nread;
4919 if (read_socket_hook)
4920 /* No need for FIONREAD or fcntl; just say don't wait. */
4921 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE, expected);
4922 else
4924 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
4925 the kbd_buffer can really hold. That may prevent loss
4926 of characters on some systems when input is stuffed at us. */
4927 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
4928 int n_to_read;
4930 /* Determine how many characters we should *try* to read. */
4931 #ifdef WINDOWSNT
4932 return 0;
4933 #else /* not WINDOWSNT */
4934 #ifdef MSDOS
4935 n_to_read = dos_keysns ();
4936 if (n_to_read == 0)
4937 return 0;
4938 #else /* not MSDOS */
4939 #ifdef FIONREAD
4940 /* Find out how much input is available. */
4941 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
4942 /* Formerly simply reported no input, but that sometimes led to
4943 a failure of Emacs to terminate.
4944 SIGHUP seems appropriate if we can't reach the terminal. */
4945 /* ??? Is it really right to send the signal just to this process
4946 rather than to the whole process group?
4947 Perhaps on systems with FIONREAD Emacs is alone in its group. */
4948 kill (getpid (), SIGHUP);
4949 if (n_to_read == 0)
4950 return 0;
4951 if (n_to_read > sizeof cbuf)
4952 n_to_read = sizeof cbuf;
4953 #else /* no FIONREAD */
4954 #if defined (USG) || defined (DGUX)
4955 /* Read some input if available, but don't wait. */
4956 n_to_read = sizeof cbuf;
4957 fcntl (input_fd, F_SETFL, O_NDELAY);
4958 #else
4959 you lose;
4960 #endif
4961 #endif
4962 #endif /* not MSDOS */
4963 #endif /* not WINDOWSNT */
4965 /* Now read; for one reason or another, this will not block.
4966 NREAD is set to the number of chars read. */
4969 #ifdef MSDOS
4970 cbuf[0] = dos_keyread ();
4971 nread = 1;
4972 #else
4973 nread = read (input_fd, cbuf, n_to_read);
4974 #endif
4975 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
4976 /* The kernel sometimes fails to deliver SIGHUP for ptys.
4977 This looks incorrect, but it isn't, because _BSD causes
4978 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
4979 and that causes a value other than 0 when there is no input. */
4980 if (nread == 0)
4981 kill (0, SIGHUP);
4982 #endif
4984 while (
4985 /* We used to retry the read if it was interrupted.
4986 But this does the wrong thing when O_NDELAY causes
4987 an EAGAIN error. Does anybody know of a situation
4988 where a retry is actually needed? */
4989 #if 0
4990 nread < 0 && (errno == EAGAIN
4991 #ifdef EFAULT
4992 || errno == EFAULT
4993 #endif
4994 #ifdef EBADSLT
4995 || errno == EBADSLT
4996 #endif
4998 #else
5000 #endif
5003 #ifndef FIONREAD
5004 #if defined (USG) || defined (DGUX)
5005 fcntl (input_fd, F_SETFL, 0);
5006 #endif /* USG or DGUX */
5007 #endif /* no FIONREAD */
5008 for (i = 0; i < nread; i++)
5010 buf[i].kind = ascii_keystroke;
5011 buf[i].modifiers = 0;
5012 if (meta_key == 1 && (cbuf[i] & 0x80))
5013 buf[i].modifiers = meta_modifier;
5014 if (meta_key != 2)
5015 cbuf[i] &= ~0x80;
5017 buf[i].code = cbuf[i];
5018 XSETFRAME (buf[i].frame_or_window, selected_frame);
5022 /* Scan the chars for C-g and store them in kbd_buffer. */
5023 for (i = 0; i < nread; i++)
5025 kbd_buffer_store_event (&buf[i]);
5026 /* Don't look at input that follows a C-g too closely.
5027 This reduces lossage due to autorepeat on C-g. */
5028 if (buf[i].kind == ascii_keystroke
5029 && buf[i].code == quit_char)
5030 break;
5033 return nread;
5035 #endif /* not VMS */
5037 #ifdef SIGIO /* for entire page */
5038 /* Note SIGIO has been undef'd if FIONREAD is missing. */
5040 SIGTYPE
5041 input_available_signal (signo)
5042 int signo;
5044 /* Must preserve main program's value of errno. */
5045 int old_errno = errno;
5046 #ifdef BSD4_1
5047 extern int select_alarmed;
5048 #endif
5050 #if defined (USG) && !defined (POSIX_SIGNALS)
5051 /* USG systems forget handlers when they are used;
5052 must reestablish each time */
5053 signal (signo, input_available_signal);
5054 #endif /* USG */
5056 #ifdef BSD4_1
5057 sigisheld (SIGIO);
5058 #endif
5060 if (input_available_clear_time)
5061 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
5063 while (1)
5065 int nread;
5066 nread = read_avail_input (1);
5067 /* -1 means it's not ok to read the input now.
5068 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
5069 0 means there was no keyboard input available. */
5070 if (nread <= 0)
5071 break;
5073 #ifdef BSD4_1
5074 select_alarmed = 1; /* Force the select emulator back to life */
5075 #endif
5078 #ifdef BSD4_1
5079 sigfree ();
5080 #endif
5081 errno = old_errno;
5083 #endif /* SIGIO */
5085 /* Send ourselves a SIGIO.
5087 This function exists so that the UNBLOCK_INPUT macro in
5088 blockinput.h can have some way to take care of input we put off
5089 dealing with, without assuming that every file which uses
5090 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
5091 void
5092 reinvoke_input_signal ()
5094 #ifdef SIGIO
5095 kill (getpid (), SIGIO);
5096 #endif
5101 /* Return the prompt-string of a sparse keymap.
5102 This is the first element which is a string.
5103 Return nil if there is none. */
5105 Lisp_Object
5106 map_prompt (map)
5107 Lisp_Object map;
5109 while (CONSP (map))
5111 register Lisp_Object tem;
5112 tem = Fcar (map);
5113 if (STRINGP (tem))
5114 return tem;
5115 map = Fcdr (map);
5117 return Qnil;
5120 static void menu_bar_item ();
5121 static void menu_bar_one_keymap ();
5123 /* These variables hold the vector under construction within
5124 menu_bar_items and its subroutines, and the current index
5125 for storing into that vector. */
5126 static Lisp_Object menu_bar_items_vector;
5127 static int menu_bar_items_index;
5129 /* Return a vector of menu items for a menu bar, appropriate
5130 to the current buffer. Each item has three elements in the vector:
5131 KEY STRING MAPLIST.
5133 OLD is an old vector we can optionally reuse, or nil. */
5135 Lisp_Object
5136 menu_bar_items (old)
5137 Lisp_Object old;
5139 /* The number of keymaps we're scanning right now, and the number of
5140 keymaps we have allocated space for. */
5141 int nmaps;
5143 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
5144 in the current keymaps, or nil where it is not a prefix. */
5145 Lisp_Object *maps;
5147 Lisp_Object def, tem, tail;
5149 Lisp_Object result;
5151 int mapno;
5152 Lisp_Object oquit;
5154 int i;
5156 struct gcpro gcpro1;
5158 /* In order to build the menus, we need to call the keymap
5159 accessors. They all call QUIT. But this function is called
5160 during redisplay, during which a quit is fatal. So inhibit
5161 quitting while building the menus.
5162 We do this instead of specbind because (1) errors will clear it anyway
5163 and (2) this avoids risk of specpdl overflow. */
5164 oquit = Vinhibit_quit;
5165 Vinhibit_quit = Qt;
5167 if (!NILP (old))
5168 menu_bar_items_vector = old;
5169 else
5170 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
5171 menu_bar_items_index = 0;
5173 GCPRO1 (menu_bar_items_vector);
5175 /* Build our list of keymaps.
5176 If we recognize a function key and replace its escape sequence in
5177 keybuf with its symbol, or if the sequence starts with a mouse
5178 click and we need to switch buffers, we jump back here to rebuild
5179 the initial keymaps from the current buffer. */
5181 Lisp_Object *tmaps;
5183 /* Should overriding-terminal-local-map and overriding-local-map apply? */
5184 if (!NILP (Voverriding_local_map_menu_flag))
5186 /* Yes, use them (if non-nil) as well as the global map. */
5187 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
5188 nmaps = 0;
5189 if (!NILP (current_kboard->Voverriding_terminal_local_map))
5190 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
5191 if (!NILP (Voverriding_local_map))
5192 maps[nmaps++] = Voverriding_local_map;
5194 else
5196 /* No, so use major and minor mode keymaps. */
5197 nmaps = current_minor_maps (NULL, &tmaps);
5198 maps = (Lisp_Object *) alloca ((nmaps + 2) * sizeof (maps[0]));
5199 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
5200 #ifdef USE_TEXT_PROPERTIES
5201 maps[nmaps++] = get_local_map (PT, current_buffer);
5202 #else
5203 maps[nmaps++] = current_buffer->keymap;
5204 #endif
5206 maps[nmaps++] = current_global_map;
5209 /* Look up in each map the dummy prefix key `menu-bar'. */
5211 result = Qnil;
5213 for (mapno = nmaps - 1; mapno >= 0; mapno--)
5215 if (! NILP (maps[mapno]))
5216 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0));
5217 else
5218 def = Qnil;
5220 tem = Fkeymapp (def);
5221 if (!NILP (tem))
5222 menu_bar_one_keymap (def);
5225 /* Move to the end those items that should be at the end. */
5227 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCONS (tail)->cdr)
5229 int i;
5230 int end = menu_bar_items_index;
5232 for (i = 0; i < end; i += 4)
5233 if (EQ (XCONS (tail)->car, XVECTOR (menu_bar_items_vector)->contents[i]))
5235 Lisp_Object tem0, tem1, tem2, tem3;
5236 /* Move the item at index I to the end,
5237 shifting all the others forward. */
5238 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
5239 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
5240 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
5241 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
5242 if (end > i + 4)
5243 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
5244 &XVECTOR (menu_bar_items_vector)->contents[i],
5245 (end - i - 4) * sizeof (Lisp_Object));
5246 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
5247 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
5248 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
5249 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
5250 break;
5254 /* Add nil, nil, nil, nil at the end. */
5255 i = menu_bar_items_index;
5256 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
5258 Lisp_Object tem;
5259 int newsize = 2 * i;
5260 tem = Fmake_vector (make_number (2 * i), Qnil);
5261 bcopy (XVECTOR (menu_bar_items_vector)->contents,
5262 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
5263 menu_bar_items_vector = tem;
5265 /* Add this item. */
5266 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5267 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5268 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5269 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5270 menu_bar_items_index = i;
5272 Vinhibit_quit = oquit;
5273 UNGCPRO;
5274 return menu_bar_items_vector;
5277 /* Scan one map KEYMAP, accumulating any menu items it defines
5278 in menu_bar_items_vector. */
5280 static void
5281 menu_bar_one_keymap (keymap)
5282 Lisp_Object keymap;
5284 Lisp_Object tail, item, key, binding, item_string, table;
5286 /* Loop over all keymap entries that have menu strings. */
5287 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
5289 item = XCONS (tail)->car;
5290 if (CONSP (item))
5292 key = XCONS (item)->car;
5293 binding = XCONS (item)->cdr;
5294 if (CONSP (binding))
5296 item_string = XCONS (binding)->car;
5297 if (STRINGP (item_string))
5298 menu_bar_item (key, item_string, Fcdr (binding));
5300 else if (EQ (binding, Qundefined))
5301 menu_bar_item (key, Qnil, binding);
5303 else if (VECTORP (item))
5305 /* Loop over the char values represented in the vector. */
5306 int len = XVECTOR (item)->size;
5307 int c;
5308 for (c = 0; c < len; c++)
5310 Lisp_Object character;
5311 XSETFASTINT (character, c);
5312 binding = XVECTOR (item)->contents[c];
5313 if (CONSP (binding))
5315 item_string = XCONS (binding)->car;
5316 if (STRINGP (item_string))
5317 menu_bar_item (key, item_string, Fcdr (binding));
5319 else if (EQ (binding, Qundefined))
5320 menu_bar_item (key, Qnil, binding);
5326 /* This is used as the handler when calling internal_condition_case_1. */
5328 static Lisp_Object
5329 menu_bar_item_1 (arg)
5330 Lisp_Object arg;
5332 return Qnil;
5335 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
5336 If there's already an item for KEY, add this DEF to it. */
5338 static void
5339 menu_bar_item (key, item_string, def)
5340 Lisp_Object key, item_string, def;
5342 Lisp_Object tem;
5343 Lisp_Object enabled;
5344 int i;
5346 /* Skip menu-bar equiv keys data. */
5347 if (CONSP (def) && CONSP (XCONS (def)->car))
5348 def = XCONS (def)->cdr;
5350 if (EQ (def, Qundefined))
5352 /* If a map has an explicit `undefined' as definition,
5353 discard any previously made menu bar item. */
5355 for (i = 0; i < menu_bar_items_index; i += 4)
5356 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
5358 if (menu_bar_items_index > i + 4)
5359 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
5360 &XVECTOR (menu_bar_items_vector)->contents[i],
5361 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
5362 menu_bar_items_index -= 4;
5363 return;
5366 /* If there's no definition for this key yet,
5367 just ignore `undefined'. */
5368 return;
5371 /* See if this entry is enabled. */
5372 enabled = Qt;
5374 if (SYMBOLP (def))
5376 /* No property, or nil, means enable.
5377 Otherwise, enable if value is not nil. */
5378 tem = Fget (def, Qmenu_enable);
5379 if (!NILP (tem))
5380 /* (condition-case nil (eval tem)
5381 (error nil)) */
5382 enabled = internal_condition_case_1 (Feval, tem, Qerror,
5383 menu_bar_item_1);
5386 /* Ignore this item if it's not enabled. */
5387 if (NILP (enabled))
5388 return;
5390 /* Find any existing item for this KEY. */
5391 for (i = 0; i < menu_bar_items_index; i += 4)
5392 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
5393 break;
5395 /* If we did not find this KEY, add it at the end. */
5396 if (i == menu_bar_items_index)
5398 /* If vector is too small, get a bigger one. */
5399 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
5401 Lisp_Object tem;
5402 int newsize = 2 * i;
5403 tem = Fmake_vector (make_number (2 * i), Qnil);
5404 bcopy (XVECTOR (menu_bar_items_vector)->contents,
5405 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
5406 menu_bar_items_vector = tem;
5408 /* Add this item. */
5409 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
5410 XVECTOR (menu_bar_items_vector)->contents[i++] = item_string;
5411 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (def, Qnil);
5412 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
5413 menu_bar_items_index = i;
5415 /* We did find an item for this KEY. Add DEF to its list of maps. */
5416 else
5418 Lisp_Object old;
5419 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
5420 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (def, old);
5424 /* Read a character using menus based on maps in the array MAPS.
5425 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
5426 Return t if we displayed a menu but the user rejected it.
5428 PREV_EVENT is the previous input event, or nil if we are reading
5429 the first event of a key sequence.
5431 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
5432 if we used a mouse menu to read the input, or zero otherwise. If
5433 USED_MOUSE_MENU is null, we don't dereference it.
5435 The prompting is done based on the prompt-string of the map
5436 and the strings associated with various map elements.
5438 This can be done with X menus or with menus put in the minibuf.
5439 These are done in different ways, depending on how the input will be read.
5440 Menus using X are done after auto-saving in read-char, getting the input
5441 event from Fx_popup_menu; menus using the minibuf use read_char recursively
5442 and do auto-saving in the inner call of read_char. */
5444 static Lisp_Object
5445 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
5446 int nmaps;
5447 Lisp_Object *maps;
5448 Lisp_Object prev_event;
5449 int *used_mouse_menu;
5451 int mapno;
5452 register Lisp_Object name;
5453 Lisp_Object rest, vector;
5455 if (used_mouse_menu)
5456 *used_mouse_menu = 0;
5458 /* Use local over global Menu maps */
5460 if (! menu_prompting)
5461 return Qnil;
5463 /* Optionally disregard all but the global map. */
5464 if (inhibit_local_menu_bar_menus)
5466 maps += (nmaps - 1);
5467 nmaps = 1;
5470 /* Get the menu name from the first map that has one (a prompt string). */
5471 for (mapno = 0; mapno < nmaps; mapno++)
5473 name = map_prompt (maps[mapno]);
5474 if (!NILP (name))
5475 break;
5478 /* If we don't have any menus, just read a character normally. */
5479 if (mapno >= nmaps)
5480 return Qnil;
5482 #ifdef HAVE_MENUS
5483 /* If we got to this point via a mouse click,
5484 use a real menu for mouse selection. */
5485 if (EVENT_HAS_PARAMETERS (prev_event)
5486 && !EQ (XCONS (prev_event)->car, Qmenu_bar))
5488 /* Display the menu and get the selection. */
5489 Lisp_Object *realmaps
5490 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
5491 Lisp_Object value;
5492 int nmaps1 = 0;
5494 /* Use the maps that are not nil. */
5495 for (mapno = 0; mapno < nmaps; mapno++)
5496 if (!NILP (maps[mapno]))
5497 realmaps[nmaps1++] = maps[mapno];
5499 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
5500 if (CONSP (value))
5502 Lisp_Object tem;
5504 record_menu_key (XCONS (value)->car);
5506 /* If we got multiple events, unread all but
5507 the first.
5508 There is no way to prevent those unread events
5509 from showing up later in last_nonmenu_event.
5510 So turn symbol and integer events into lists,
5511 to indicate that they came from a mouse menu,
5512 so that when present in last_nonmenu_event
5513 they won't confuse things. */
5514 for (tem = XCONS (value)->cdr; !NILP (tem);
5515 tem = XCONS (tem)->cdr)
5517 record_menu_key (XCONS (tem)->car);
5518 if (SYMBOLP (XCONS (tem)->car)
5519 || INTEGERP (XCONS (tem)->car))
5520 XCONS (tem)->car
5521 = Fcons (XCONS (tem)->car, Qnil);
5524 /* If we got more than one event, put all but the first
5525 onto this list to be read later.
5526 Return just the first event now. */
5527 Vunread_command_events
5528 = nconc2 (XCONS (value)->cdr, Vunread_command_events);
5529 value = XCONS (value)->car;
5531 else if (NILP (value))
5532 value = Qt;
5533 if (used_mouse_menu)
5534 *used_mouse_menu = 1;
5535 return value;
5537 #endif /* HAVE_MENUS */
5538 return Qnil ;
5541 /* Buffer in use so far for the minibuf prompts for menu keymaps.
5542 We make this bigger when necessary, and never free it. */
5543 static char *read_char_minibuf_menu_text;
5544 /* Size of that buffer. */
5545 static int read_char_minibuf_menu_width;
5547 static Lisp_Object
5548 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
5549 int commandflag ;
5550 int nmaps;
5551 Lisp_Object *maps;
5553 int mapno;
5554 register Lisp_Object name;
5555 int nlength;
5556 int width = FRAME_WIDTH (selected_frame) - 4;
5557 int idx = -1;
5558 int nobindings = 1;
5559 Lisp_Object rest, vector;
5560 char *menu;
5562 if (! menu_prompting)
5563 return Qnil;
5565 /* Make sure we have a big enough buffer for the menu text. */
5566 if (read_char_minibuf_menu_text == 0)
5568 read_char_minibuf_menu_width = width + 4;
5569 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
5571 else if (width + 4 > read_char_minibuf_menu_width)
5573 read_char_minibuf_menu_width = width + 4;
5574 read_char_minibuf_menu_text
5575 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
5577 menu = read_char_minibuf_menu_text;
5579 /* Get the menu name from the first map that has one (a prompt string). */
5580 for (mapno = 0; mapno < nmaps; mapno++)
5582 name = map_prompt (maps[mapno]);
5583 if (!NILP (name))
5584 break;
5587 /* If we don't have any menus, just read a character normally. */
5588 if (mapno >= nmaps)
5589 return Qnil;
5591 /* Prompt string always starts with map's prompt, and a space. */
5592 strcpy (menu, XSTRING (name)->data);
5593 nlength = XSTRING (name)->size;
5594 menu[nlength++] = ':';
5595 menu[nlength++] = ' ';
5596 menu[nlength] = 0;
5598 /* Start prompting at start of first map. */
5599 mapno = 0;
5600 rest = maps[mapno];
5602 /* Present the documented bindings, a line at a time. */
5603 while (1)
5605 int notfirst = 0;
5606 int i = nlength;
5607 Lisp_Object obj;
5608 int ch;
5609 Lisp_Object orig_defn_macro;
5611 /* Loop over elements of map. */
5612 while (i < width)
5614 Lisp_Object s, elt;
5616 /* If reached end of map, start at beginning of next map. */
5617 if (NILP (rest))
5619 mapno++;
5620 /* At end of last map, wrap around to first map if just starting,
5621 or end this line if already have something on it. */
5622 if (mapno == nmaps)
5624 mapno = 0;
5625 if (notfirst || nobindings) break;
5627 rest = maps[mapno];
5630 /* Look at the next element of the map. */
5631 if (idx >= 0)
5632 elt = XVECTOR (vector)->contents[idx];
5633 else
5634 elt = Fcar_safe (rest);
5636 if (idx < 0 && VECTORP (elt))
5638 /* If we found a dense table in the keymap,
5639 advanced past it, but start scanning its contents. */
5640 rest = Fcdr_safe (rest);
5641 vector = elt;
5642 idx = 0;
5644 else
5646 /* An ordinary element. */
5647 Lisp_Object event;
5649 if (idx < 0)
5651 s = Fcar_safe (Fcdr_safe (elt)); /* alist */
5652 event = Fcar_safe (elt);
5654 else
5656 s = Fcar_safe (elt); /* vector */
5657 XSETINT (event, idx);
5660 /* Ignore the element if it has no prompt string. */
5661 if (STRINGP (s) && INTEGERP (event))
5663 /* 1 if the char to type matches the string. */
5664 int char_matches;
5665 Lisp_Object upcased_event, downcased_event;
5666 Lisp_Object desc;
5668 upcased_event = Fupcase (event);
5669 downcased_event = Fdowncase (event);
5670 char_matches = (XINT (upcased_event) == XSTRING (s)->data[0]
5671 || XINT (downcased_event) == XSTRING (s)->data[0]);
5672 if (! char_matches)
5673 desc = Fsingle_key_description (event);
5675 /* If we have room for the prompt string, add it to this line.
5676 If this is the first on the line, always add it. */
5677 if ((XSTRING (s)->size + i + 2
5678 + (char_matches ? 0 : XSTRING (desc)->size + 3))
5679 < width
5680 || !notfirst)
5682 int thiswidth;
5684 /* Punctuate between strings. */
5685 if (notfirst)
5687 strcpy (menu + i, ", ");
5688 i += 2;
5690 notfirst = 1;
5691 nobindings = 0 ;
5693 /* If the char to type doesn't match the string's
5694 first char, explicitly show what char to type. */
5695 if (! char_matches)
5697 /* Add as much of string as fits. */
5698 thiswidth = XSTRING (desc)->size;
5699 if (thiswidth + i > width)
5700 thiswidth = width - i;
5701 bcopy (XSTRING (desc)->data, menu + i, thiswidth);
5702 i += thiswidth;
5703 strcpy (menu + i, " = ");
5704 i += 3;
5707 /* Add as much of string as fits. */
5708 thiswidth = XSTRING (s)->size;
5709 if (thiswidth + i > width)
5710 thiswidth = width - i;
5711 bcopy (XSTRING (s)->data, menu + i, thiswidth);
5712 i += thiswidth;
5713 menu[i] = 0;
5715 else
5717 /* If this element does not fit, end the line now,
5718 and save the element for the next line. */
5719 strcpy (menu + i, "...");
5720 break;
5724 /* Move past this element. */
5725 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
5726 /* Handle reaching end of dense table. */
5727 idx = -1;
5728 if (idx >= 0)
5729 idx++;
5730 else
5731 rest = Fcdr_safe (rest);
5735 /* Prompt with that and read response. */
5736 message1 (menu);
5738 /* Make believe its not a keyboard macro in case the help char
5739 is pressed. Help characters are not recorded because menu prompting
5740 is not used on replay.
5742 orig_defn_macro = current_kboard->defining_kbd_macro;
5743 current_kboard->defining_kbd_macro = Qnil;
5745 obj = read_char (commandflag, 0, 0, Qnil, 0);
5746 while (BUFFERP (obj));
5747 current_kboard->defining_kbd_macro = orig_defn_macro;
5749 if (!INTEGERP (obj))
5750 return obj;
5751 else
5752 ch = XINT (obj);
5754 if (! EQ (obj, menu_prompt_more_char)
5755 && (!INTEGERP (menu_prompt_more_char)
5756 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
5758 if (!NILP (current_kboard->defining_kbd_macro))
5759 store_kbd_macro_char (obj);
5760 return obj;
5762 /* Help char - go round again */
5766 /* Reading key sequences. */
5768 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
5769 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
5770 keymap, or nil otherwise. Return the index of the first keymap in
5771 which KEY has any binding, or NMAPS if no map has a binding.
5773 If KEY is a meta ASCII character, treat it like meta-prefix-char
5774 followed by the corresponding non-meta character. Keymaps in
5775 CURRENT with non-prefix bindings for meta-prefix-char become nil in
5776 NEXT.
5778 If KEY has no bindings in any of the CURRENT maps, NEXT is left
5779 unmodified.
5781 NEXT may be the same array as CURRENT. */
5783 static int
5784 follow_key (key, nmaps, current, defs, next)
5785 Lisp_Object key;
5786 Lisp_Object *current, *defs, *next;
5787 int nmaps;
5789 int i, first_binding;
5790 int did_meta = 0;
5792 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
5793 followed by the corresponding non-meta character.
5794 Put the results into DEFS, since we are going to alter that anyway.
5795 Do not alter CURRENT or NEXT. */
5796 if (INTEGERP (key) && (XINT (key) & CHAR_META))
5798 for (i = 0; i < nmaps; i++)
5799 if (! NILP (current[i]))
5801 Lisp_Object def;
5802 def = get_keyelt (access_keymap (current[i],
5803 meta_prefix_char, 1, 0));
5805 /* Note that since we pass the resulting bindings through
5806 get_keymap_1, non-prefix bindings for meta-prefix-char
5807 disappear. */
5808 defs[i] = get_keymap_1 (def, 0, 1);
5810 else
5811 defs[i] = Qnil;
5813 did_meta = 1;
5814 XSETINT (key, XFASTINT (key) & ~CHAR_META);
5817 first_binding = nmaps;
5818 for (i = nmaps - 1; i >= 0; i--)
5820 if (! NILP (current[i]))
5822 Lisp_Object map;
5823 if (did_meta)
5824 map = defs[i];
5825 else
5826 map = current[i];
5828 defs[i] = get_keyelt (access_keymap (map, key, 1, 0));
5829 if (! NILP (defs[i]))
5830 first_binding = i;
5832 else
5833 defs[i] = Qnil;
5836 /* Given the set of bindings we've found, produce the next set of maps. */
5837 if (first_binding < nmaps)
5838 for (i = 0; i < nmaps; i++)
5839 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
5841 return first_binding;
5844 /* Read a sequence of keys that ends with a non prefix character,
5845 storing it in KEYBUF, a buffer of size BUFSIZE.
5846 Prompt with PROMPT.
5847 Return the length of the key sequence stored.
5848 Return -1 if the user rejected a command menu.
5850 Echo starting immediately unless `prompt' is 0.
5852 Where a key sequence ends depends on the currently active keymaps.
5853 These include any minor mode keymaps active in the current buffer,
5854 the current buffer's local map, and the global map.
5856 If a key sequence has no other bindings, we check Vfunction_key_map
5857 to see if some trailing subsequence might be the beginning of a
5858 function key's sequence. If so, we try to read the whole function
5859 key, and substitute its symbolic name into the key sequence.
5861 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
5862 `double-' events into similar click events, if that would make them
5863 bound. We try to turn `triple-' events first into `double-' events,
5864 then into clicks.
5866 If we get a mouse click in a mode line, vertical divider, or other
5867 non-text area, we treat the click as if it were prefixed by the
5868 symbol denoting that area - `mode-line', `vertical-line', or
5869 whatever.
5871 If the sequence starts with a mouse click, we read the key sequence
5872 with respect to the buffer clicked on, not the current buffer.
5874 If the user switches frames in the midst of a key sequence, we put
5875 off the switch-frame event until later; the next call to
5876 read_char will return it.
5878 If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
5879 from the selected window's buffer. */
5881 static int
5882 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
5883 can_return_switch_frame, fix_current_buffer)
5884 Lisp_Object *keybuf;
5885 int bufsize;
5886 Lisp_Object prompt;
5887 int dont_downcase_last;
5888 int can_return_switch_frame;
5889 int fix_current_buffer;
5891 int count = specpdl_ptr - specpdl;
5893 /* How many keys there are in the current key sequence. */
5894 int t;
5896 /* The length of the echo buffer when we started reading, and
5897 the length of this_command_keys when we started reading. */
5898 int echo_start;
5899 int keys_start;
5901 /* The number of keymaps we're scanning right now, and the number of
5902 keymaps we have allocated space for. */
5903 int nmaps;
5904 int nmaps_allocated = 0;
5906 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
5907 the current keymaps. */
5908 Lisp_Object *defs;
5910 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
5911 in the current keymaps, or nil where it is not a prefix. */
5912 Lisp_Object *submaps;
5914 /* The local map to start out with at start of key sequence. */
5915 Lisp_Object orig_local_map;
5917 /* 1 if we have already considered switching to the local-map property
5918 of the place where a mouse click occurred. */
5919 int localized_local_map = 0;
5921 /* The index in defs[] of the first keymap that has a binding for
5922 this key sequence. In other words, the lowest i such that
5923 defs[i] is non-nil. */
5924 int first_binding;
5926 /* If t < mock_input, then KEYBUF[t] should be read as the next
5927 input key.
5929 We use this to recover after recognizing a function key. Once we
5930 realize that a suffix of the current key sequence is actually a
5931 function key's escape sequence, we replace the suffix with the
5932 function key's binding from Vfunction_key_map. Now keybuf
5933 contains a new and different key sequence, so the echo area,
5934 this_command_keys, and the submaps and defs arrays are wrong. In
5935 this situation, we set mock_input to t, set t to 0, and jump to
5936 restart_sequence; the loop will read keys from keybuf up until
5937 mock_input, thus rebuilding the state; and then it will resume
5938 reading characters from the keyboard. */
5939 int mock_input = 0;
5941 /* If the sequence is unbound in submaps[], then
5942 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
5943 and fkey_map is its binding.
5945 These might be > t, indicating that all function key scanning
5946 should hold off until t reaches them. We do this when we've just
5947 recognized a function key, to avoid searching for the function
5948 key's again in Vfunction_key_map. */
5949 int fkey_start = 0, fkey_end = 0;
5950 Lisp_Object fkey_map;
5952 /* Likewise, for key_translation_map. */
5953 int keytran_start = 0, keytran_end = 0;
5954 Lisp_Object keytran_map;
5956 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
5957 we put it off for later. While we're reading, we keep the event here. */
5958 Lisp_Object delayed_switch_frame;
5960 /* See the comment below... */
5961 #if defined (GOBBLE_FIRST_EVENT)
5962 Lisp_Object first_event;
5963 #endif
5965 Lisp_Object original_uppercase;
5966 int original_uppercase_position = -1;
5968 /* Gets around Microsoft compiler limitations. */
5969 int dummyflag = 0;
5971 struct buffer *starting_buffer;
5973 /* Nonzero if we seem to have got the beginning of a binding
5974 in function_key_map. */
5975 int function_key_possible = 0;
5976 int key_translation_possible = 0;
5978 /* Save the status of key translation before each step,
5979 so that we can restore this after downcasing. */
5980 Lisp_Object prev_fkey_map;
5981 Lisp_Object prev_fkey_start;
5982 Lisp_Object prev_fkey_end;
5984 Lisp_Object prev_keytran_map;
5985 Lisp_Object prev_keytran_start;
5986 Lisp_Object prev_keytran_end;
5988 int junk;
5990 last_nonmenu_event = Qnil;
5992 delayed_switch_frame = Qnil;
5993 fkey_map = Vfunction_key_map;
5994 keytran_map = Vkey_translation_map;
5996 /* If there is no function-key-map, turn off function key scanning. */
5997 if (NILP (Fkeymapp (Vfunction_key_map)))
5998 fkey_start = fkey_end = bufsize + 1;
6000 /* If there is no key-translation-map, turn off scanning. */
6001 if (NILP (Fkeymapp (Vkey_translation_map)))
6002 keytran_start = keytran_end = bufsize + 1;
6004 if (INTERACTIVE)
6006 if (!NILP (prompt))
6007 echo_prompt (XSTRING (prompt)->data);
6008 else if (cursor_in_echo_area && echo_keystrokes)
6009 /* This doesn't put in a dash if the echo buffer is empty, so
6010 you don't always see a dash hanging out in the minibuffer. */
6011 echo_dash ();
6014 /* Record the initial state of the echo area and this_command_keys;
6015 we will need to restore them if we replay a key sequence. */
6016 if (INTERACTIVE)
6017 echo_start = echo_length ();
6018 keys_start = this_command_key_count;
6019 this_single_command_key_start = keys_start;
6021 #if defined (GOBBLE_FIRST_EVENT)
6022 /* This doesn't quite work, because some of the things that read_char
6023 does cannot safely be bypassed. It seems too risky to try to make
6024 this work right. */
6026 /* Read the first char of the sequence specially, before setting
6027 up any keymaps, in case a filter runs and switches buffers on us. */
6028 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
6029 &junk);
6030 #endif /* GOBBLE_FIRST_EVENT */
6032 orig_local_map = get_local_map (PT, current_buffer);
6034 /* We jump here when the key sequence has been thoroughly changed, and
6035 we need to rescan it starting from the beginning. When we jump here,
6036 keybuf[0..mock_input] holds the sequence we should reread. */
6037 replay_sequence:
6039 starting_buffer = current_buffer;
6040 function_key_possible = 0;
6041 key_translation_possible = 0;
6043 /* Build our list of keymaps.
6044 If we recognize a function key and replace its escape sequence in
6045 keybuf with its symbol, or if the sequence starts with a mouse
6046 click and we need to switch buffers, we jump back here to rebuild
6047 the initial keymaps from the current buffer. */
6049 Lisp_Object *maps;
6051 if (!NILP (current_kboard->Voverriding_terminal_local_map)
6052 || !NILP (Voverriding_local_map))
6054 if (3 > nmaps_allocated)
6056 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
6057 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
6058 nmaps_allocated = 3;
6060 nmaps = 0;
6061 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6062 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6063 if (!NILP (Voverriding_local_map))
6064 submaps[nmaps++] = Voverriding_local_map;
6066 else
6068 nmaps = current_minor_maps (0, &maps);
6069 if (nmaps + 2 > nmaps_allocated)
6071 submaps = (Lisp_Object *) alloca ((nmaps+2) * sizeof (submaps[0]));
6072 defs = (Lisp_Object *) alloca ((nmaps+2) * sizeof (defs[0]));
6073 nmaps_allocated = nmaps + 2;
6075 bcopy (maps, submaps, nmaps * sizeof (submaps[0]));
6076 #ifdef USE_TEXT_PROPERTIES
6077 submaps[nmaps++] = orig_local_map;
6078 #else
6079 submaps[nmaps++] = current_buffer->keymap;
6080 #endif
6082 submaps[nmaps++] = current_global_map;
6085 /* Find an accurate initial value for first_binding. */
6086 for (first_binding = 0; first_binding < nmaps; first_binding++)
6087 if (! NILP (submaps[first_binding]))
6088 break;
6090 /* Start from the beginning in keybuf. */
6091 t = 0;
6093 /* These are no-ops the first time through, but if we restart, they
6094 revert the echo area and this_command_keys to their original state. */
6095 this_command_key_count = keys_start;
6096 if (INTERACTIVE && t < mock_input)
6097 echo_truncate (echo_start);
6099 /* If the best binding for the current key sequence is a keymap, or
6100 we may be looking at a function key's escape sequence, keep on
6101 reading. */
6102 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
6103 || (first_binding >= nmaps
6104 && fkey_start < t
6105 /* mock input is never part of a function key's sequence. */
6106 && mock_input <= fkey_start)
6107 || (first_binding >= nmaps
6108 && keytran_start < t && key_translation_possible)
6109 /* Don't return in the middle of a possible function key sequence,
6110 if the only bindings we found were via case conversion.
6111 Thus, if ESC O a has a function-key-map translation
6112 and ESC o has a binding, don't return after ESC O,
6113 so that we can translate ESC O plus the next character. */
6116 Lisp_Object key;
6117 int used_mouse_menu = 0;
6119 /* Where the last real key started. If we need to throw away a
6120 key that has expanded into more than one element of keybuf
6121 (say, a mouse click on the mode line which is being treated
6122 as [mode-line (mouse-...)], then we backtrack to this point
6123 of keybuf. */
6124 int last_real_key_start;
6126 /* These variables are analogous to echo_start and keys_start;
6127 while those allow us to restart the entire key sequence,
6128 echo_local_start and keys_local_start allow us to throw away
6129 just one key. */
6130 int echo_local_start, keys_local_start, local_first_binding;
6132 if (t >= bufsize)
6133 error ("Key sequence too long");
6135 if (INTERACTIVE)
6136 echo_local_start = echo_length ();
6137 keys_local_start = this_command_key_count;
6138 local_first_binding = first_binding;
6140 replay_key:
6141 /* These are no-ops, unless we throw away a keystroke below and
6142 jumped back up to replay_key; in that case, these restore the
6143 variables to their original state, allowing us to replay the
6144 loop. */
6145 if (INTERACTIVE && t < mock_input)
6146 echo_truncate (echo_local_start);
6147 this_command_key_count = keys_local_start;
6148 first_binding = local_first_binding;
6150 /* By default, assume each event is "real". */
6151 last_real_key_start = t;
6153 /* Does mock_input indicate that we are re-reading a key sequence? */
6154 if (t < mock_input)
6156 key = keybuf[t];
6157 add_command_key (key);
6158 if (echo_keystrokes)
6159 echo_char (key);
6162 /* If not, we should actually read a character. */
6163 else
6165 struct buffer *buf = current_buffer;
6168 #ifdef MULTI_KBOARD
6169 KBOARD *interrupted_kboard = current_kboard;
6170 struct frame *interrupted_frame = selected_frame;
6171 if (setjmp (wrong_kboard_jmpbuf))
6173 if (!NILP (delayed_switch_frame))
6175 interrupted_kboard->kbd_queue
6176 = Fcons (delayed_switch_frame,
6177 interrupted_kboard->kbd_queue);
6178 delayed_switch_frame = Qnil;
6180 while (t > 0)
6181 interrupted_kboard->kbd_queue
6182 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
6184 /* If the side queue is non-empty, ensure it begins with a
6185 switch-frame, so we'll replay it in the right context. */
6186 if (CONSP (interrupted_kboard->kbd_queue)
6187 && (key = XCONS (interrupted_kboard->kbd_queue)->car,
6188 !(EVENT_HAS_PARAMETERS (key)
6189 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
6190 Qswitch_frame))))
6192 Lisp_Object frame;
6193 XSETFRAME (frame, interrupted_frame);
6194 interrupted_kboard->kbd_queue
6195 = Fcons (make_lispy_switch_frame (frame),
6196 interrupted_kboard->kbd_queue);
6198 mock_input = 0;
6199 orig_local_map = get_local_map (PT, current_buffer);
6200 goto replay_sequence;
6202 #endif
6203 key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
6204 &used_mouse_menu);
6207 /* read_char returns t when it shows a menu and the user rejects it.
6208 Just return -1. */
6209 if (EQ (key, Qt))
6210 return -1;
6212 /* read_char returns -1 at the end of a macro.
6213 Emacs 18 handles this by returning immediately with a
6214 zero, so that's what we'll do. */
6215 if (INTEGERP (key) && XINT (key) == -1)
6217 t = 0;
6218 /* The Microsoft C compiler can't handle the goto that
6219 would go here. */
6220 dummyflag = 1;
6221 break;
6224 /* If the current buffer has been changed from under us, the
6225 keymap may have changed, so replay the sequence. */
6226 if (BUFFERP (key))
6228 mock_input = t;
6229 /* Reset the current buffer from the selected window
6230 in case something changed the former and not the latter.
6231 This is to be more consistent with the behavior
6232 of the command_loop_1. */
6233 if (fix_current_buffer)
6234 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
6235 Fset_buffer (XWINDOW (selected_window)->buffer);
6237 orig_local_map = get_local_map (PT, current_buffer);
6238 goto replay_sequence;
6241 /* If we have a quit that was typed in another frame, and
6242 quit_throw_to_read_char switched buffers,
6243 replay to get the right keymap. */
6244 if (XINT (key) == quit_char && current_buffer != starting_buffer)
6246 keybuf[t++] = key;
6247 mock_input = t;
6248 Vquit_flag = Qnil;
6249 orig_local_map = get_local_map (PT, current_buffer);
6250 goto replay_sequence;
6253 Vquit_flag = Qnil;
6256 /* Clicks in non-text areas get prefixed by the symbol
6257 in their CHAR-ADDRESS field. For example, a click on
6258 the mode line is prefixed by the symbol `mode-line'.
6260 Furthermore, key sequences beginning with mouse clicks
6261 are read using the keymaps of the buffer clicked on, not
6262 the current buffer. So we may have to switch the buffer
6263 here.
6265 When we turn one event into two events, we must make sure
6266 that neither of the two looks like the original--so that,
6267 if we replay the events, they won't be expanded again.
6268 If not for this, such reexpansion could happen either here
6269 or when user programs play with this-command-keys. */
6270 if (EVENT_HAS_PARAMETERS (key))
6272 Lisp_Object kind;
6274 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
6275 if (EQ (kind, Qmouse_click))
6277 Lisp_Object window, posn;
6279 window = POSN_WINDOW (EVENT_START (key));
6280 posn = POSN_BUFFER_POSN (EVENT_START (key));
6281 if (CONSP (posn))
6283 /* We're looking at the second event of a
6284 sequence which we expanded before. Set
6285 last_real_key_start appropriately. */
6286 if (t > 0)
6287 last_real_key_start = t - 1;
6290 /* Key sequences beginning with mouse clicks are
6291 read using the keymaps in the buffer clicked on,
6292 not the current buffer. If we're at the
6293 beginning of a key sequence, switch buffers. */
6294 if (last_real_key_start == 0
6295 && WINDOWP (window)
6296 && BUFFERP (XWINDOW (window)->buffer)
6297 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
6299 keybuf[t] = key;
6300 mock_input = t + 1;
6302 /* Arrange to go back to the original buffer once we're
6303 done reading the key sequence. Note that we can't
6304 use save_excursion_{save,restore} here, because they
6305 save point as well as the current buffer; we don't
6306 want to save point, because redisplay may change it,
6307 to accommodate a Fset_window_start or something. We
6308 don't want to do this at the top of the function,
6309 because we may get input from a subprocess which
6310 wants to change the selected window and stuff (say,
6311 emacsclient). */
6312 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
6314 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
6315 orig_local_map = get_local_map (PT, current_buffer);
6316 goto replay_sequence;
6318 /* For a mouse click, get the local text-property keymap
6319 of the place clicked on, rather than point. */
6320 if (last_real_key_start == 0 && CONSP (XCONS (key)->cdr)
6321 && ! localized_local_map)
6323 Lisp_Object map_here, start, pos;
6325 localized_local_map = 1;
6326 start = EVENT_START (key);
6327 if (CONSP (start) && CONSP (XCONS (start)->cdr))
6329 pos = POSN_BUFFER_POSN (start);
6330 if (INTEGERP (pos)
6331 && XINT (pos) >= BEG && XINT (pos) <= Z)
6333 map_here = get_local_map (XINT (pos), current_buffer);
6334 if (!EQ (map_here, orig_local_map))
6336 orig_local_map = map_here;
6337 keybuf[t] = key;
6338 mock_input = t + 1;
6340 goto replay_sequence;
6346 /* Expand mode-line and scroll-bar events into two events:
6347 use posn as a fake prefix key. */
6348 if (SYMBOLP (posn))
6350 if (t + 1 >= bufsize)
6351 error ("Key sequence too long");
6352 keybuf[t] = posn;
6353 keybuf[t+1] = key;
6354 mock_input = t + 2;
6356 /* Zap the position in key, so we know that we've
6357 expanded it, and don't try to do so again. */
6358 POSN_BUFFER_POSN (EVENT_START (key))
6359 = Fcons (posn, Qnil);
6360 goto replay_key;
6363 else if (EQ (kind, Qswitch_frame))
6365 /* If we're at the beginning of a key sequence, and the caller
6366 says it's okay, go ahead and return this event. If we're
6367 in the midst of a key sequence, delay it until the end. */
6368 if (t > 0 || !can_return_switch_frame)
6370 delayed_switch_frame = key;
6371 goto replay_key;
6374 else if (CONSP (XCONS (key)->cdr)
6375 && CONSP (EVENT_START (key))
6376 && CONSP (XCONS (EVENT_START (key))->cdr))
6378 Lisp_Object posn;
6380 posn = POSN_BUFFER_POSN (EVENT_START (key));
6381 /* Handle menu-bar events:
6382 insert the dummy prefix event `menu-bar'. */
6383 if (EQ (posn, Qmenu_bar))
6385 if (t + 1 >= bufsize)
6386 error ("Key sequence too long");
6387 keybuf[t] = posn;
6388 keybuf[t+1] = key;
6390 /* Zap the position in key, so we know that we've
6391 expanded it, and don't try to do so again. */
6392 POSN_BUFFER_POSN (EVENT_START (key))
6393 = Fcons (posn, Qnil);
6395 mock_input = t + 2;
6396 goto replay_sequence;
6398 else if (CONSP (posn))
6400 /* We're looking at the second event of a
6401 sequence which we expanded before. Set
6402 last_real_key_start appropriately. */
6403 if (last_real_key_start == t && t > 0)
6404 last_real_key_start = t - 1;
6409 /* We have finally decided that KEY is something we might want
6410 to look up. */
6411 first_binding = (follow_key (key,
6412 nmaps - first_binding,
6413 submaps + first_binding,
6414 defs + first_binding,
6415 submaps + first_binding)
6416 + first_binding);
6418 /* If KEY wasn't bound, we'll try some fallbacks. */
6419 if (first_binding >= nmaps)
6421 Lisp_Object head;
6423 head = EVENT_HEAD (key);
6424 if (help_char_p (head) && t > 0)
6426 read_key_sequence_cmd = Vprefix_help_command;
6427 keybuf[t++] = key;
6428 last_nonmenu_event = key;
6429 /* The Microsoft C compiler can't handle the goto that
6430 would go here. */
6431 dummyflag = 1;
6432 break;
6435 if (SYMBOLP (head))
6437 Lisp_Object breakdown;
6438 int modifiers;
6440 breakdown = parse_modifiers (head);
6441 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
6442 /* Attempt to reduce an unbound mouse event to a simpler
6443 event that is bound:
6444 Drags reduce to clicks.
6445 Double-clicks reduce to clicks.
6446 Triple-clicks reduce to double-clicks, then to clicks.
6447 Down-clicks are eliminated.
6448 Double-downs reduce to downs, then are eliminated.
6449 Triple-downs reduce to double-downs, then to downs,
6450 then are eliminated. */
6451 if (modifiers & (down_modifier | drag_modifier
6452 | double_modifier | triple_modifier))
6454 while (modifiers & (down_modifier | drag_modifier
6455 | double_modifier | triple_modifier))
6457 Lisp_Object new_head, new_click;
6458 if (modifiers & triple_modifier)
6459 modifiers ^= (double_modifier | triple_modifier);
6460 else if (modifiers & double_modifier)
6461 modifiers &= ~double_modifier;
6462 else if (modifiers & drag_modifier)
6463 modifiers &= ~drag_modifier;
6464 else
6466 /* Dispose of this `down' event by simply jumping
6467 back to replay_key, to get another event.
6469 Note that if this event came from mock input,
6470 then just jumping back to replay_key will just
6471 hand it to us again. So we have to wipe out any
6472 mock input.
6474 We could delete keybuf[t] and shift everything
6475 after that to the left by one spot, but we'd also
6476 have to fix up any variable that points into
6477 keybuf, and shifting isn't really necessary
6478 anyway.
6480 Adding prefixes for non-textual mouse clicks
6481 creates two characters of mock input, and both
6482 must be thrown away. If we're only looking at
6483 the prefix now, we can just jump back to
6484 replay_key. On the other hand, if we've already
6485 processed the prefix, and now the actual click
6486 itself is giving us trouble, then we've lost the
6487 state of the keymaps we want to backtrack to, and
6488 we need to replay the whole sequence to rebuild
6491 Beyond that, only function key expansion could
6492 create more than two keys, but that should never
6493 generate mouse events, so it's okay to zero
6494 mock_input in that case too.
6496 Isn't this just the most wonderful code ever? */
6497 if (t == last_real_key_start)
6499 mock_input = 0;
6500 goto replay_key;
6502 else
6504 mock_input = last_real_key_start;
6505 goto replay_sequence;
6509 new_head
6510 = apply_modifiers (modifiers, XCONS (breakdown)->car);
6511 new_click
6512 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
6514 /* Look for a binding for this new key. follow_key
6515 promises that it didn't munge submaps the
6516 last time we called it, since key was unbound. */
6517 first_binding
6518 = (follow_key (new_click,
6519 nmaps - local_first_binding,
6520 submaps + local_first_binding,
6521 defs + local_first_binding,
6522 submaps + local_first_binding)
6523 + local_first_binding);
6525 /* If that click is bound, go for it. */
6526 if (first_binding < nmaps)
6528 key = new_click;
6529 break;
6531 /* Otherwise, we'll leave key set to the drag event. */
6537 keybuf[t++] = key;
6538 /* Normally, last_nonmenu_event gets the previous key we read.
6539 But when a mouse popup menu is being used,
6540 we don't update last_nonmenu_event; it continues to hold the mouse
6541 event that preceded the first level of menu. */
6542 if (!used_mouse_menu)
6543 last_nonmenu_event = key;
6545 /* Record what part of this_command_keys is the current key sequence. */
6546 this_single_command_key_start = this_command_key_count - t;
6548 prev_fkey_map = fkey_map;
6549 prev_fkey_start = fkey_start;
6550 prev_fkey_end = fkey_end;
6552 prev_keytran_map = keytran_map;
6553 prev_keytran_start = keytran_start;
6554 prev_keytran_end = keytran_end;
6556 /* If the sequence is unbound, see if we can hang a function key
6557 off the end of it. We only want to scan real keyboard input
6558 for function key sequences, so if mock_input says that we're
6559 re-reading old events, don't examine it. */
6560 if (first_binding >= nmaps
6561 && t >= mock_input)
6563 Lisp_Object fkey_next;
6565 /* Continue scan from fkey_end until we find a bound suffix.
6566 If we fail, increment fkey_start
6567 and start fkey_end from there. */
6568 while (fkey_end < t)
6570 Lisp_Object key;
6572 key = keybuf[fkey_end++];
6573 /* Look up meta-characters by prefixing them
6574 with meta_prefix_char. I hate this. */
6575 if (INTEGERP (key) && XINT (key) & meta_modifier)
6577 fkey_next
6578 = get_keymap_1
6579 (get_keyelt
6580 (access_keymap (fkey_map, meta_prefix_char, 1, 0)),
6581 0, 1);
6582 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
6584 else
6585 fkey_next = fkey_map;
6587 fkey_next
6588 = get_keyelt (access_keymap (fkey_next, key, 1, 0));
6590 #if 0 /* I didn't turn this on, because it might cause trouble
6591 for the mapping of return into C-m and tab into C-i. */
6592 /* Optionally don't map function keys into other things.
6593 This enables the user to redefine kp- keys easily. */
6594 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
6595 fkey_next = Qnil;
6596 #endif
6598 /* If the function key map gives a function, not an
6599 array, then call the function with no args and use
6600 its value instead. */
6601 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
6602 && fkey_end == t)
6604 struct gcpro gcpro1, gcpro2, gcpro3;
6605 Lisp_Object tem;
6606 tem = fkey_next;
6608 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
6609 fkey_next = call1 (fkey_next, prompt);
6610 UNGCPRO;
6611 /* If the function returned something invalid,
6612 barf--don't ignore it.
6613 (To ignore it safely, we would need to gcpro a bunch of
6614 other variables.) */
6615 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
6616 error ("Function in key-translation-map returns invalid key sequence");
6619 function_key_possible = ! NILP (fkey_next);
6621 /* If keybuf[fkey_start..fkey_end] is bound in the
6622 function key map and it's a suffix of the current
6623 sequence (i.e. fkey_end == t), replace it with
6624 the binding and restart with fkey_start at the end. */
6625 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
6626 && fkey_end == t)
6628 int len = XFASTINT (Flength (fkey_next));
6630 t = fkey_start + len;
6631 if (t >= bufsize)
6632 error ("Key sequence too long");
6634 if (VECTORP (fkey_next))
6635 bcopy (XVECTOR (fkey_next)->contents,
6636 keybuf + fkey_start,
6637 (t - fkey_start) * sizeof (keybuf[0]));
6638 else if (STRINGP (fkey_next))
6640 int i;
6642 for (i = 0; i < len; i++)
6643 XSETFASTINT (keybuf[fkey_start + i],
6644 XSTRING (fkey_next)->data[i]);
6647 mock_input = t;
6648 fkey_start = fkey_end = t;
6649 fkey_map = Vfunction_key_map;
6651 /* Do pass the results through key-translation-map. */
6652 keytran_start = keytran_end = 0;
6653 keytran_map = Vkey_translation_map;
6655 goto replay_sequence;
6658 fkey_map = get_keymap_1 (fkey_next, 0, 1);
6660 /* If we no longer have a bound suffix, try a new positions for
6661 fkey_start. */
6662 if (NILP (fkey_map))
6664 fkey_end = ++fkey_start;
6665 fkey_map = Vfunction_key_map;
6666 function_key_possible = 0;
6671 /* Look for this sequence in key-translation-map. */
6673 Lisp_Object keytran_next;
6675 /* Scan from keytran_end until we find a bound suffix. */
6676 while (keytran_end < t)
6678 Lisp_Object key;
6680 key = keybuf[keytran_end++];
6681 /* Look up meta-characters by prefixing them
6682 with meta_prefix_char. I hate this. */
6683 if (INTEGERP (key) && XINT (key) & meta_modifier)
6685 keytran_next
6686 = get_keymap_1
6687 (get_keyelt
6688 (access_keymap (keytran_map, meta_prefix_char, 1, 0)),
6689 0, 1);
6690 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
6692 else
6693 keytran_next = keytran_map;
6695 keytran_next
6696 = get_keyelt (access_keymap (keytran_next, key, 1, 0));
6698 /* If the key translation map gives a function, not an
6699 array, then call the function with no args and use
6700 its value instead. */
6701 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
6702 && keytran_end == t)
6704 struct gcpro gcpro1, gcpro2, gcpro3;
6705 Lisp_Object tem;
6706 tem = keytran_next;
6708 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
6709 keytran_next = call1 (keytran_next, prompt);
6710 UNGCPRO;
6711 /* If the function returned something invalid,
6712 barf--don't ignore it.
6713 (To ignore it safely, we would need to gcpro a bunch of
6714 other variables.) */
6715 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
6716 error ("Function in key-translation-map returns invalid key sequence");
6719 key_translation_possible = ! NILP (keytran_next);
6721 /* If keybuf[keytran_start..keytran_end] is bound in the
6722 key translation map and it's a suffix of the current
6723 sequence (i.e. keytran_end == t), replace it with
6724 the binding and restart with keytran_start at the end. */
6725 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
6726 && keytran_end == t)
6728 int len = XFASTINT (Flength (keytran_next));
6730 t = keytran_start + len;
6731 if (t >= bufsize)
6732 error ("Key sequence too long");
6734 if (VECTORP (keytran_next))
6735 bcopy (XVECTOR (keytran_next)->contents,
6736 keybuf + keytran_start,
6737 (t - keytran_start) * sizeof (keybuf[0]));
6738 else if (STRINGP (keytran_next))
6740 int i;
6742 for (i = 0; i < len; i++)
6743 XSETFASTINT (keybuf[keytran_start + i],
6744 XSTRING (keytran_next)->data[i]);
6747 mock_input = t;
6748 keytran_start = keytran_end = t;
6749 keytran_map = Vkey_translation_map;
6751 /* Don't pass the results of key-translation-map
6752 through function-key-map. */
6753 fkey_start = fkey_end = t;
6754 fkey_map = Vkey_translation_map;
6756 goto replay_sequence;
6759 keytran_map = get_keymap_1 (keytran_next, 0, 1);
6761 /* If we no longer have a bound suffix, try a new positions for
6762 keytran_start. */
6763 if (NILP (keytran_map))
6765 keytran_end = ++keytran_start;
6766 keytran_map = Vkey_translation_map;
6767 key_translation_possible = 0;
6772 /* If KEY is not defined in any of the keymaps,
6773 and cannot be part of a function key or translation,
6774 and is an upper case letter
6775 use the corresponding lower-case letter instead. */
6776 if (first_binding == nmaps && ! function_key_possible
6777 && ! key_translation_possible
6778 && INTEGERP (key)
6779 && ((((XINT (key) & 0x3ffff)
6780 < XSTRING (current_buffer->downcase_table)->size)
6781 && UPPERCASEP (XINT (key) & 0x3ffff))
6782 || (XINT (key) & shift_modifier)))
6784 Lisp_Object new_key;
6786 original_uppercase = key;
6787 original_uppercase_position = t - 1;
6789 if (XINT (key) & shift_modifier)
6790 XSETINT (new_key, XINT (key) & ~shift_modifier);
6791 else
6792 XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
6793 | (XINT (key) & ~0x3ffff)));
6795 /* We have to do this unconditionally, regardless of whether
6796 the lower-case char is defined in the keymaps, because they
6797 might get translated through function-key-map. */
6798 keybuf[t - 1] = new_key;
6799 mock_input = t;
6801 fkey_map = prev_fkey_map;
6802 fkey_start = prev_fkey_start;
6803 fkey_end = prev_fkey_end;
6805 keytran_map = prev_keytran_map;
6806 keytran_start = prev_keytran_start;
6807 keytran_end = prev_keytran_end;
6809 goto replay_sequence;
6811 /* If KEY is not defined in any of the keymaps,
6812 and cannot be part of a function key or translation,
6813 and is a shifted function key,
6814 use the corresponding unshifted function key instead. */
6815 if (first_binding == nmaps && ! function_key_possible
6816 && ! key_translation_possible
6817 && SYMBOLP (key))
6819 Lisp_Object breakdown;
6820 int modifiers;
6822 breakdown = parse_modifiers (key);
6823 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
6824 if (modifiers & shift_modifier)
6826 Lisp_Object new_key;
6828 original_uppercase = key;
6829 original_uppercase_position = t - 1;
6831 modifiers &= ~shift_modifier;
6832 new_key = apply_modifiers (modifiers,
6833 XCONS (breakdown)->car);
6835 keybuf[t - 1] = new_key;
6836 mock_input = t;
6838 fkey_map = prev_fkey_map;
6839 fkey_start = prev_fkey_start;
6840 fkey_end = prev_fkey_end;
6842 keytran_map = prev_keytran_map;
6843 keytran_start = prev_keytran_start;
6844 keytran_end = prev_keytran_end;
6846 goto replay_sequence;
6851 if (!dummyflag)
6852 read_key_sequence_cmd = (first_binding < nmaps
6853 ? defs[first_binding]
6854 : Qnil);
6856 unread_switch_frame = delayed_switch_frame;
6857 unbind_to (count, Qnil);
6859 /* Don't downcase the last character if the caller says don't.
6860 Don't downcase it if the result is undefined, either. */
6861 if ((dont_downcase_last || first_binding >= nmaps)
6862 && t - 1 == original_uppercase_position)
6863 keybuf[t - 1] = original_uppercase;
6865 /* Occasionally we fabricate events, perhaps by expanding something
6866 according to function-key-map, or by adding a prefix symbol to a
6867 mouse click in the scroll bar or modeline. In this cases, return
6868 the entire generated key sequence, even if we hit an unbound
6869 prefix or a definition before the end. This means that you will
6870 be able to push back the event properly, and also means that
6871 read-key-sequence will always return a logical unit.
6873 Better ideas? */
6874 for (; t < mock_input; t++)
6876 if (echo_keystrokes)
6877 echo_char (keybuf[t]);
6878 add_command_key (keybuf[t]);
6881 return t;
6884 #if 0 /* This doc string is too long for some compilers.
6885 This commented-out definition serves for DOC. */
6886 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
6887 "Read a sequence of keystrokes and return as a string or vector.\n\
6888 The sequence is sufficient to specify a non-prefix command in the\n\
6889 current local and global maps.\n\
6891 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
6892 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
6893 as a continuation of the previous key.\n\
6895 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
6896 convert the last event to lower case. (Normally any upper case event\n\
6897 is converted to lower case if the original event is undefined and the lower\n\
6898 case equivalent is defined.) A non-nil value is appropriate for reading\n\
6899 a key sequence to be defined.\n\
6901 A C-g typed while in this function is treated like any other character,\n\
6902 and `quit-flag' is not set.\n\
6904 If the key sequence starts with a mouse click, then the sequence is read\n\
6905 using the keymaps of the buffer of the window clicked in, not the buffer\n\
6906 of the selected window as normal.\n\
6907 ""\n\
6908 `read-key-sequence' drops unbound button-down events, since you normally\n\
6909 only care about the click or drag events which follow them. If a drag\n\
6910 or multi-click event is unbound, but the corresponding click event would\n\
6911 be bound, `read-key-sequence' turns the event into a click event at the\n\
6912 drag's starting position. This means that you don't have to distinguish\n\
6913 between click and drag, double, or triple events unless you want to.\n\
6915 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
6916 lines separating windows, and scroll bars with imaginary keys\n\
6917 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
6919 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
6920 function will process a switch-frame event if the user switches frames\n\
6921 before typing anything. If the user switches frames in the middle of a\n\
6922 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
6923 is nil, then the event will be put off until after the current key sequence.\n\
6925 `read-key-sequence' checks `function-key-map' for function key\n\
6926 sequences, where they wouldn't conflict with ordinary bindings. See\n\
6927 `function-key-map' for more details.")
6928 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
6929 #endif
6931 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
6933 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
6934 Lisp_Object prompt, continue_echo, dont_downcase_last;
6935 Lisp_Object can_return_switch_frame;
6937 Lisp_Object keybuf[30];
6938 register int i;
6939 struct gcpro gcpro1, gcpro2;
6941 if (!NILP (prompt))
6942 CHECK_STRING (prompt, 0);
6943 QUIT;
6945 bzero (keybuf, sizeof keybuf);
6946 GCPRO1 (keybuf[0]);
6947 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
6949 if (NILP (continue_echo))
6951 this_command_key_count = 0;
6952 this_single_command_key_start = 0;
6955 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
6956 prompt, ! NILP (dont_downcase_last),
6957 ! NILP (can_return_switch_frame), 0);
6959 if (i == -1)
6961 Vquit_flag = Qt;
6962 QUIT;
6964 UNGCPRO;
6965 return make_event_array (i, keybuf);
6968 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
6969 "Execute CMD as an editor command.\n\
6970 CMD must be a symbol that satisfies the `commandp' predicate.\n\
6971 Optional second arg RECORD-FLAG non-nil\n\
6972 means unconditionally put this command in `command-history'.\n\
6973 Otherwise, that is done only if an arg is read using the minibuffer.\n\
6974 The argument KEYS specifies the value to use instead of (this-command-keys)\n\
6975 when reading the arguments; if it is nil, (this-command-keys) is used.\n\
6976 The argument SPECIAL, if non-nil, means that this command is executing\n\
6977 a special event, so ignore the prefix argument and don't clear it.")
6978 (cmd, record_flag, keys, special)
6979 Lisp_Object cmd, record_flag, keys, special;
6981 register Lisp_Object final;
6982 register Lisp_Object tem;
6983 Lisp_Object prefixarg;
6984 struct backtrace backtrace;
6985 extern int debug_on_next_call;
6987 debug_on_next_call = 0;
6989 if (NILP (special))
6991 prefixarg = current_kboard->Vprefix_arg;
6992 Vcurrent_prefix_arg = prefixarg;
6993 current_kboard->Vprefix_arg = Qnil;
6995 else
6996 prefixarg = Qnil;
6998 if (SYMBOLP (cmd))
7000 tem = Fget (cmd, Qdisabled);
7001 if (!NILP (tem) && !NILP (Vrun_hooks))
7003 tem = Fsymbol_value (Qdisabled_command_hook);
7004 if (!NILP (tem))
7005 return call1 (Vrun_hooks, Qdisabled_command_hook);
7009 while (1)
7011 final = Findirect_function (cmd);
7013 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
7015 struct gcpro gcpro1, gcpro2;
7017 GCPRO2 (cmd, prefixarg);
7018 do_autoload (final, cmd);
7019 UNGCPRO;
7021 else
7022 break;
7025 if (STRINGP (final) || VECTORP (final))
7027 /* If requested, place the macro in the command history. For
7028 other sorts of commands, call-interactively takes care of
7029 this. */
7030 if (!NILP (record_flag))
7031 Vcommand_history
7032 = Fcons (Fcons (Qexecute_kbd_macro,
7033 Fcons (final, Fcons (prefixarg, Qnil))),
7034 Vcommand_history);
7036 return Fexecute_kbd_macro (final, prefixarg);
7038 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
7040 backtrace.next = backtrace_list;
7041 backtrace_list = &backtrace;
7042 backtrace.function = &Qcall_interactively;
7043 backtrace.args = &cmd;
7044 backtrace.nargs = 1;
7045 backtrace.evalargs = 0;
7047 tem = Fcall_interactively (cmd, record_flag, keys);
7049 backtrace_list = backtrace.next;
7050 return tem;
7052 return Qnil;
7055 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
7056 1, 1, "P",
7057 "Read function name, then read its arguments and call it.")
7058 (prefixarg)
7059 Lisp_Object prefixarg;
7061 Lisp_Object function;
7062 char buf[40];
7063 Lisp_Object saved_keys;
7064 Lisp_Object bindings, value;
7065 struct gcpro gcpro1, gcpro2;
7067 saved_keys = Fvector (this_command_key_count,
7068 XVECTOR (this_command_keys)->contents);
7069 buf[0] = 0;
7070 GCPRO2 (saved_keys, prefixarg);
7072 if (EQ (prefixarg, Qminus))
7073 strcpy (buf, "- ");
7074 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
7075 strcpy (buf, "C-u ");
7076 else if (CONSP (prefixarg) && INTEGERP (XCONS (prefixarg)->car))
7078 if (sizeof (int) == sizeof (EMACS_INT))
7079 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
7080 else if (sizeof (long) == sizeof (EMACS_INT))
7081 sprintf (buf, "%ld ", XINT (XCONS (prefixarg)->car));
7082 else
7083 abort ();
7085 else if (INTEGERP (prefixarg))
7087 if (sizeof (int) == sizeof (EMACS_INT))
7088 sprintf (buf, "%d ", XINT (prefixarg));
7089 else if (sizeof (long) == sizeof (EMACS_INT))
7090 sprintf (buf, "%ld ", XINT (prefixarg));
7091 else
7092 abort ();
7095 /* This isn't strictly correct if execute-extended-command
7096 is bound to anything else. Perhaps it should use
7097 this_command_keys? */
7098 strcat (buf, "M-x ");
7100 /* Prompt with buf, and then read a string, completing from and
7101 restricting to the set of all defined commands. Don't provide
7102 any initial input. Save the command read on the extended-command
7103 history list. */
7104 function = Fcompleting_read (build_string (buf),
7105 Vobarray, Qcommandp,
7106 Qt, Qnil, Qextended_command_history);
7108 if (STRINGP (function) && XSTRING (function)->size == 0)
7109 error ("No command name given");
7111 /* Set this_command_keys to the concatenation of saved_keys and
7112 function, followed by a RET. */
7114 struct Lisp_String *str;
7115 Lisp_Object *keys;
7116 int i;
7117 Lisp_Object tem;
7119 this_command_key_count = 0;
7120 this_single_command_key_start = 0;
7122 keys = XVECTOR (saved_keys)->contents;
7123 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
7124 add_command_key (keys[i]);
7126 str = XSTRING (function);
7127 for (i = 0; i < str->size; i++)
7129 XSETFASTINT (tem, str->data[i]);
7130 add_command_key (tem);
7133 XSETFASTINT (tem, '\015');
7134 add_command_key (tem);
7137 UNGCPRO;
7139 function = Fintern (function, Qnil);
7140 current_kboard->Vprefix_arg = prefixarg;
7141 this_command = function;
7143 /* If enabled, show which key runs this command. */
7144 if (!NILP (Vsuggest_key_bindings)
7145 && NILP (Vexecuting_macro)
7146 && SYMBOLP (function))
7147 bindings = Fwhere_is_internal (function, Voverriding_local_map,
7148 Qt, Qnil);
7149 else
7150 bindings = Qnil;
7152 value = Qnil;
7153 GCPRO2 (bindings, value);
7154 value = Fcommand_execute (function, Qt, Qnil, Qnil);
7156 /* If the command has a key binding, print it now. */
7157 if (!NILP (bindings))
7159 /* But first wait, and skip the message if there is input. */
7160 if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
7161 ? Vsuggest_key_bindings : make_number (2)),
7162 Qnil, Qnil)))
7164 Lisp_Object binding;
7165 char *newmessage;
7166 char *oldmessage = echo_area_glyphs;
7167 int oldmessage_len = echo_area_glyphs_length;
7169 binding = Fkey_description (bindings);
7171 newmessage
7172 = (char *) alloca (XSYMBOL (function)->name->size
7173 + XSTRING (binding)->size
7174 + 100);
7175 sprintf (newmessage, "You can run the command `%s' by typing %s",
7176 XSYMBOL (function)->name->data,
7177 XSTRING (binding)->data);
7178 message1_nolog (newmessage);
7179 if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
7180 ? Vsuggest_key_bindings : make_number (2)),
7181 Qnil, Qnil)))
7182 message2_nolog (oldmessage, oldmessage_len);
7186 RETURN_UNGCPRO (value);
7189 /* Find the set of keymaps now active.
7190 Store into *MAPS_P a vector holding the various maps
7191 and return the number of them. The vector was malloc'd
7192 and the caller should free it. */
7195 current_active_maps (maps_p)
7196 Lisp_Object **maps_p;
7198 Lisp_Object *tmaps, *maps;
7199 int nmaps;
7201 /* Should overriding-terminal-local-map and overriding-local-map apply? */
7202 if (!NILP (Voverriding_local_map_menu_flag))
7204 /* Yes, use them (if non-nil) as well as the global map. */
7205 maps = (Lisp_Object *) xmalloc (3 * sizeof (maps[0]));
7206 nmaps = 0;
7207 if (!NILP (current_kboard->Voverriding_terminal_local_map))
7208 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
7209 if (!NILP (Voverriding_local_map))
7210 maps[nmaps++] = Voverriding_local_map;
7212 else
7214 /* No, so use major and minor mode keymaps. */
7215 nmaps = current_minor_maps (NULL, &tmaps);
7216 maps = (Lisp_Object *) xmalloc ((nmaps + 2) * sizeof (maps[0]));
7217 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
7218 #ifdef USE_TEXT_PROPERTIES
7219 maps[nmaps++] = get_local_map (PT, current_buffer);
7220 #else
7221 maps[nmaps++] = current_buffer->keymap;
7222 #endif
7224 maps[nmaps++] = current_global_map;
7226 *maps_p = maps;
7227 return nmaps;
7230 /* Return nonzero if input events are pending. */
7232 detect_input_pending ()
7234 if (!input_pending)
7235 get_input_pending (&input_pending, 0);
7237 return input_pending;
7240 /* Return nonzero if input events are pending, and run any pending timers. */
7242 detect_input_pending_run_timers (do_display)
7243 int do_display;
7245 int old_timers_run = timers_run;
7247 if (!input_pending)
7248 get_input_pending (&input_pending, 1);
7250 if (old_timers_run != timers_run && do_display)
7251 redisplay_preserve_echo_area ();
7253 return input_pending;
7256 /* This is called in some cases before a possible quit.
7257 It cases the next call to detect_input_pending to recompute input_pending.
7258 So calling this function unnecessarily can't do any harm. */
7259 clear_input_pending ()
7261 input_pending = 0;
7264 /* Return nonzero if there are pending requeued events.
7265 This isn't used yet. The hope is to make wait_reading_process_input
7266 call it, and return return if it runs Lisp code that unreads something.
7267 The problem is, kbd_buffer_get_event needs to be fixed to know what
7268 to do in that case. It isn't trivial. */
7270 requeued_events_pending_p ()
7272 return (!NILP (Vunread_command_events) || unread_command_char != -1);
7276 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
7277 "T if command input is currently available with no waiting.\n\
7278 Actually, the value is nil only if we can be sure that no input is available.")
7281 if (!NILP (Vunread_command_events) || unread_command_char != -1)
7282 return (Qt);
7284 get_input_pending (&input_pending, 1);
7285 return input_pending > 0 ? Qt : Qnil;
7288 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
7289 "Return vector of last 100 events, not counting those from keyboard macros.")
7292 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
7293 Lisp_Object val;
7295 if (total_keys < NUM_RECENT_KEYS)
7296 return Fvector (total_keys, keys);
7297 else
7299 val = Fvector (NUM_RECENT_KEYS, keys);
7300 bcopy (keys + recent_keys_index,
7301 XVECTOR (val)->contents,
7302 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
7303 bcopy (keys,
7304 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
7305 recent_keys_index * sizeof (Lisp_Object));
7306 return val;
7310 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
7311 "Return the key sequence that invoked this command.\n\
7312 The value is a string or a vector.")
7315 return make_event_array (this_command_key_count,
7316 XVECTOR (this_command_keys)->contents);
7319 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
7320 Sthis_single_command_keys, 0, 0, 0,
7321 "Return the key sequence that invoked this command.\n\
7322 Unlike `this-command-keys', this function's value\n\
7323 does not include prefix arguments.\n\
7324 The value is a string or a vector.")
7327 return make_event_array (this_command_key_count
7328 - this_single_command_key_start,
7329 (XVECTOR (this_command_keys)->contents
7330 + this_single_command_key_start));
7333 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
7334 Sreset_this_command_lengths, 0, 0, 0,
7335 "Used for complicated reasons in `universal-argument-other-key'.\n\
7337 `universal-argument-other-key' rereads the event just typed.\n\
7338 It then gets translated through `function-key-map'.\n\
7339 The translated event gets included in the echo area and in\n\
7340 the value of `this-command-keys' in addition to the raw original event.\n\
7341 That is not right.\n\
7343 Calling this function directs the translated event to replace\n\
7344 the original event, so that only one version of the event actually\n\
7345 appears in the echo area and in the value of `this-command-keys.'.")
7348 before_command_restore_flag = 1;
7349 before_command_key_count_1 = before_command_key_count;
7350 before_command_echo_length_1 = before_command_echo_length;
7353 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
7354 "Return the current depth in recursive edits.")
7357 Lisp_Object temp;
7358 XSETFASTINT (temp, command_loop_level + minibuf_level);
7359 return temp;
7362 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
7363 "FOpen dribble file: ",
7364 "Start writing all keyboard characters to a dribble file called FILE.\n\
7365 If FILE is nil, close any open dribble file.")
7366 (file)
7367 Lisp_Object file;
7369 if (dribble)
7371 fclose (dribble);
7372 dribble = 0;
7374 if (!NILP (file))
7376 file = Fexpand_file_name (file, Qnil);
7377 dribble = fopen (XSTRING (file)->data, "w");
7378 if (dribble == 0)
7379 report_file_error ("Opening dribble", Fcons (file, Qnil));
7381 return Qnil;
7384 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
7385 "Discard the contents of the terminal input buffer.\n\
7386 Also cancel any kbd macro being defined.")
7389 current_kboard->defining_kbd_macro = Qnil;
7390 update_mode_lines++;
7392 Vunread_command_events = Qnil;
7393 unread_command_char = -1;
7395 discard_tty_input ();
7397 /* Without the cast, GCC complains that this assignment loses the
7398 volatile qualifier of kbd_store_ptr. Is there anything wrong
7399 with that? */
7400 kbd_fetch_ptr = (struct input_event *) kbd_store_ptr;
7401 Ffillarray (kbd_buffer_frame_or_window, Qnil);
7402 input_pending = 0;
7404 return Qnil;
7407 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
7408 "Stop Emacs and return to superior process. You can resume later.\n\
7409 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
7410 control, run a subshell instead.\n\n\
7411 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
7412 to be read as terminal input by Emacs's parent, after suspension.\n\
7414 Before suspending, run the normal hook `suspend-hook'.\n\
7415 After resumption run the normal hook `suspend-resume-hook'.\n\
7417 Some operating systems cannot stop the Emacs process and resume it later.\n\
7418 On such systems, Emacs starts a subshell instead of suspending.")
7419 (stuffstring)
7420 Lisp_Object stuffstring;
7422 Lisp_Object tem;
7423 int count = specpdl_ptr - specpdl;
7424 int old_height, old_width;
7425 int width, height;
7426 struct gcpro gcpro1, gcpro2;
7427 extern init_sys_modes ();
7429 if (!NILP (stuffstring))
7430 CHECK_STRING (stuffstring, 0);
7432 /* Run the functions in suspend-hook. */
7433 if (!NILP (Vrun_hooks))
7434 call1 (Vrun_hooks, intern ("suspend-hook"));
7436 GCPRO1 (stuffstring);
7437 get_frame_size (&old_width, &old_height);
7438 reset_sys_modes ();
7439 /* sys_suspend can get an error if it tries to fork a subshell
7440 and the system resources aren't available for that. */
7441 record_unwind_protect (init_sys_modes, 0);
7442 stuff_buffered_input (stuffstring);
7443 if (cannot_suspend)
7444 sys_subshell ();
7445 else
7446 sys_suspend ();
7447 unbind_to (count, Qnil);
7449 /* Check if terminal/window size has changed.
7450 Note that this is not useful when we are running directly
7451 with a window system; but suspend should be disabled in that case. */
7452 get_frame_size (&width, &height);
7453 if (width != old_width || height != old_height)
7454 change_frame_size (selected_frame, height, width, 0, 0);
7456 /* Run suspend-resume-hook. */
7457 if (!NILP (Vrun_hooks))
7458 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
7460 UNGCPRO;
7461 return Qnil;
7464 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
7465 Then in any case stuff anything Emacs has read ahead and not used. */
7467 stuff_buffered_input (stuffstring)
7468 Lisp_Object stuffstring;
7470 /* stuff_char works only in BSD, versions 4.2 and up. */
7471 #ifdef BSD_SYSTEM
7472 #ifndef BSD4_1
7473 register unsigned char *p;
7475 if (STRINGP (stuffstring))
7477 register int count;
7479 p = XSTRING (stuffstring)->data;
7480 count = XSTRING (stuffstring)->size;
7481 while (count-- > 0)
7482 stuff_char (*p++);
7483 stuff_char ('\n');
7485 /* Anything we have read ahead, put back for the shell to read. */
7486 /* ?? What should this do when we have multiple keyboards??
7487 Should we ignore anything that was typed in at the "wrong" kboard? */
7488 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
7490 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
7491 kbd_fetch_ptr = kbd_buffer;
7492 if (kbd_fetch_ptr->kind == ascii_keystroke)
7493 stuff_char (kbd_fetch_ptr->code);
7494 kbd_fetch_ptr->kind = no_event;
7495 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
7496 - kbd_buffer]
7497 = Qnil);
7499 input_pending = 0;
7500 #endif
7501 #endif /* BSD_SYSTEM and not BSD4_1 */
7504 set_waiting_for_input (time_to_clear)
7505 EMACS_TIME *time_to_clear;
7507 input_available_clear_time = time_to_clear;
7509 /* Tell interrupt_signal to throw back to read_char, */
7510 waiting_for_input = 1;
7512 /* If interrupt_signal was called before and buffered a C-g,
7513 make it run again now, to avoid timing error. */
7514 if (!NILP (Vquit_flag))
7515 quit_throw_to_read_char ();
7518 clear_waiting_for_input ()
7520 /* Tell interrupt_signal not to throw back to read_char, */
7521 waiting_for_input = 0;
7522 input_available_clear_time = 0;
7525 /* This routine is called at interrupt level in response to C-G.
7526 If interrupt_input, this is the handler for SIGINT.
7527 Otherwise, it is called from kbd_buffer_store_event,
7528 in handling SIGIO or SIGTINT.
7530 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
7531 immediately throw back to read_char.
7533 Otherwise it sets the Lisp variable quit-flag not-nil.
7534 This causes eval to throw, when it gets a chance.
7535 If quit-flag is already non-nil, it stops the job right away. */
7537 SIGTYPE
7538 interrupt_signal (signalnum) /* If we don't have an argument, */
7539 int signalnum; /* some compilers complain in signal calls. */
7541 char c;
7542 /* Must preserve main program's value of errno. */
7543 int old_errno = errno;
7545 #if defined (USG) && !defined (POSIX_SIGNALS)
7546 if (!read_socket_hook && NILP (Vwindow_system))
7548 /* USG systems forget handlers when they are used;
7549 must reestablish each time */
7550 signal (SIGINT, interrupt_signal);
7551 signal (SIGQUIT, interrupt_signal);
7553 #endif /* USG */
7555 cancel_echoing ();
7557 if (!NILP (Vquit_flag)
7558 && (FRAME_TERMCAP_P (selected_frame) || FRAME_MSDOS_P (selected_frame)))
7560 /* If SIGINT isn't blocked, don't let us be interrupted by
7561 another SIGINT, it might be harmful due to non-reentrancy
7562 in I/O functions. */
7563 sigblock (sigmask (SIGINT));
7565 fflush (stdout);
7566 reset_sys_modes ();
7568 #ifdef SIGTSTP /* Support possible in later USG versions */
7570 * On systems which can suspend the current process and return to the original
7571 * shell, this command causes the user to end up back at the shell.
7572 * The "Auto-save" and "Abort" questions are not asked until
7573 * the user elects to return to emacs, at which point he can save the current
7574 * job and either dump core or continue.
7576 sys_suspend ();
7577 #else
7578 #ifdef VMS
7579 if (sys_suspend () == -1)
7581 printf ("Not running as a subprocess;\n");
7582 printf ("you can continue or abort.\n");
7584 #else /* not VMS */
7585 /* Perhaps should really fork an inferior shell?
7586 But that would not provide any way to get back
7587 to the original shell, ever. */
7588 printf ("No support for stopping a process on this operating system;\n");
7589 printf ("you can continue or abort.\n");
7590 #endif /* not VMS */
7591 #endif /* not SIGTSTP */
7592 #ifdef MSDOS
7593 /* We must remain inside the screen area when the internal terminal
7594 is used. Note that [Enter] is not echoed by dos. */
7595 cursor_to (0, 0);
7596 #endif
7597 /* It doesn't work to autosave while GC is in progress;
7598 the code used for auto-saving doesn't cope with the mark bit. */
7599 if (!gc_in_progress)
7601 printf ("Auto-save? (y or n) ");
7602 fflush (stdout);
7603 if (((c = getchar ()) & ~040) == 'Y')
7605 Fdo_auto_save (Qt, Qnil);
7606 #ifdef MSDOS
7607 printf ("\r\nAuto-save done");
7608 #else /* not MSDOS */
7609 printf ("Auto-save done\n");
7610 #endif /* not MSDOS */
7612 while (c != '\n') c = getchar ();
7614 else
7616 /* During GC, it must be safe to reenable quitting again. */
7617 Vinhibit_quit = Qnil;
7618 #ifdef MSDOS
7619 printf ("\r\n");
7620 #endif /* not MSDOS */
7621 printf ("Garbage collection in progress; cannot auto-save now\r\n");
7622 printf ("but will instead do a real quit after garbage collection ends\r\n");
7623 fflush (stdout);
7626 #ifdef MSDOS
7627 printf ("\r\nAbort? (y or n) ");
7628 #else /* not MSDOS */
7629 #ifdef VMS
7630 printf ("Abort (and enter debugger)? (y or n) ");
7631 #else /* not VMS */
7632 printf ("Abort (and dump core)? (y or n) ");
7633 #endif /* not VMS */
7634 #endif /* not MSDOS */
7635 fflush (stdout);
7636 if (((c = getchar ()) & ~040) == 'Y')
7637 abort ();
7638 while (c != '\n') c = getchar ();
7639 #ifdef MSDOS
7640 printf ("\r\nContinuing...\r\n");
7641 #else /* not MSDOS */
7642 printf ("Continuing...\n");
7643 #endif /* not MSDOS */
7644 fflush (stdout);
7645 init_sys_modes ();
7646 sigfree ();
7648 else
7650 /* If executing a function that wants to be interrupted out of
7651 and the user has not deferred quitting by binding `inhibit-quit'
7652 then quit right away. */
7653 if (immediate_quit && NILP (Vinhibit_quit))
7655 immediate_quit = 0;
7656 sigfree ();
7657 Fsignal (Qquit, Qnil);
7659 else
7660 /* Else request quit when it's safe */
7661 Vquit_flag = Qt;
7664 if (waiting_for_input && !echoing)
7665 quit_throw_to_read_char ();
7667 errno = old_errno;
7670 /* Handle a C-g by making read_char return C-g. */
7672 quit_throw_to_read_char ()
7674 quit_error_check ();
7675 sigfree ();
7676 /* Prevent another signal from doing this before we finish. */
7677 clear_waiting_for_input ();
7678 input_pending = 0;
7680 Vunread_command_events = Qnil;
7681 unread_command_char = -1;
7683 #if 0 /* Currently, sit_for is called from read_char without turning
7684 off polling. And that can call set_waiting_for_input.
7685 It seems to be harmless. */
7686 #ifdef POLL_FOR_INPUT
7687 /* May be > 1 if in recursive minibuffer. */
7688 if (poll_suppress_count == 0)
7689 abort ();
7690 #endif
7691 #endif
7692 if (FRAMEP (internal_last_event_frame)
7693 && XFRAME (internal_last_event_frame) != selected_frame)
7694 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
7695 Qnil, 0);
7697 _longjmp (getcjmp, 1);
7700 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
7701 "Set mode of reading keyboard input.\n\
7702 First arg INTERRUPT non-nil means use input interrupts;\n\
7703 nil means use CBREAK mode.\n\
7704 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
7705 (no effect except in CBREAK mode).\n\
7706 Third arg META t means accept 8-bit input (for a Meta key).\n\
7707 META nil means ignore the top bit, on the assumption it is parity.\n\
7708 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
7709 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
7710 See also `current-input-mode'.")
7711 (interrupt, flow, meta, quit)
7712 Lisp_Object interrupt, flow, meta, quit;
7714 if (!NILP (quit)
7715 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
7716 error ("set-input-mode: QUIT must be an ASCII character");
7718 #ifdef POLL_FOR_INPUT
7719 stop_polling ();
7720 #endif
7722 #ifndef MSDOS
7723 /* this causes startup screen to be restored and messes with the mouse */
7724 reset_sys_modes ();
7725 #endif
7727 #ifdef SIGIO
7728 /* Note SIGIO has been undef'd if FIONREAD is missing. */
7729 if (read_socket_hook)
7731 /* When using X, don't give the user a real choice,
7732 because we haven't implemented the mechanisms to support it. */
7733 #ifdef NO_SOCK_SIGIO
7734 interrupt_input = 0;
7735 #else /* not NO_SOCK_SIGIO */
7736 interrupt_input = 1;
7737 #endif /* NO_SOCK_SIGIO */
7739 else
7740 interrupt_input = !NILP (interrupt);
7741 #else /* not SIGIO */
7742 interrupt_input = 0;
7743 #endif /* not SIGIO */
7745 /* Our VMS input only works by interrupts, as of now. */
7746 #ifdef VMS
7747 interrupt_input = 1;
7748 #endif
7750 flow_control = !NILP (flow);
7751 if (NILP (meta))
7752 meta_key = 0;
7753 else if (EQ (meta, Qt))
7754 meta_key = 1;
7755 else
7756 meta_key = 2;
7757 if (!NILP (quit))
7758 /* Don't let this value be out of range. */
7759 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
7761 #ifndef MSDOS
7762 init_sys_modes ();
7763 #endif
7765 #ifdef POLL_FOR_INPUT
7766 poll_suppress_count = 1;
7767 start_polling ();
7768 #endif
7769 return Qnil;
7772 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
7773 "Return information about the way Emacs currently reads keyboard input.\n\
7774 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
7775 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
7776 nil, Emacs is using CBREAK mode.\n\
7777 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
7778 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
7779 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
7780 META nil means ignoring the top bit, on the assumption it is parity.\n\
7781 META is neither t nor nil if accepting 8-bit input and using\n\
7782 all 8 bits as the character code.\n\
7783 QUIT is the character Emacs currently uses to quit.\n\
7784 The elements of this list correspond to the arguments of\n\
7785 `set-input-mode'.")
7788 Lisp_Object val[4];
7790 val[0] = interrupt_input ? Qt : Qnil;
7791 val[1] = flow_control ? Qt : Qnil;
7792 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
7793 XSETFASTINT (val[3], quit_char);
7795 return Flist (sizeof (val) / sizeof (val[0]), val);
7800 * Set up a new kboard object with reasonable initial values.
7802 void
7803 init_kboard (kb)
7804 KBOARD *kb;
7806 kb->Voverriding_terminal_local_map = Qnil;
7807 kb->Vlast_command = Qnil;
7808 kb->Vprefix_arg = Qnil;
7809 kb->kbd_queue = Qnil;
7810 kb->kbd_queue_has_data = 0;
7811 kb->immediate_echo = 0;
7812 kb->echoptr = kb->echobuf;
7813 kb->echo_after_prompt = -1;
7814 kb->kbd_macro_buffer = 0;
7815 kb->kbd_macro_bufsize = 0;
7816 kb->defining_kbd_macro = Qnil;
7817 kb->Vlast_kbd_macro = Qnil;
7818 kb->reference_count = 0;
7819 kb->Vsystem_key_alist = Qnil;
7820 kb->system_key_syms = Qnil;
7821 kb->Vdefault_minibuffer_frame = Qnil;
7825 * Destroy the contents of a kboard object, but not the object itself.
7826 * We use this just before deleting it, or if we're going to initialize
7827 * it a second time.
7829 static void
7830 wipe_kboard (kb)
7831 KBOARD *kb;
7833 if (kb->kbd_macro_buffer)
7834 xfree (kb->kbd_macro_buffer);
7837 #ifdef MULTI_KBOARD
7838 void
7839 delete_kboard (kb)
7840 KBOARD *kb;
7842 KBOARD **kbp;
7843 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
7844 if (*kbp == NULL)
7845 abort ();
7846 *kbp = kb->next_kboard;
7847 wipe_kboard (kb);
7848 xfree (kb);
7850 #endif
7852 init_keyboard ()
7854 /* This is correct before outermost invocation of the editor loop */
7855 command_loop_level = -1;
7856 immediate_quit = 0;
7857 quit_char = Ctl ('g');
7858 Vunread_command_events = Qnil;
7859 unread_command_char = -1;
7860 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
7861 total_keys = 0;
7862 recent_keys_index = 0;
7863 kbd_fetch_ptr = kbd_buffer;
7864 kbd_store_ptr = kbd_buffer;
7865 kbd_buffer_frame_or_window
7866 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
7867 #ifdef HAVE_MOUSE
7868 do_mouse_tracking = Qnil;
7869 #endif
7870 input_pending = 0;
7872 /* This means that command_loop_1 won't try to select anything the first
7873 time through. */
7874 internal_last_event_frame = Qnil;
7875 Vlast_event_frame = internal_last_event_frame;
7877 #ifdef MULTI_KBOARD
7878 current_kboard = initial_kboard;
7879 #endif
7880 wipe_kboard (current_kboard);
7881 init_kboard (current_kboard);
7883 if (initialized)
7884 Ffillarray (kbd_buffer_frame_or_window, Qnil);
7886 kbd_buffer_frame_or_window
7887 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
7888 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
7890 signal (SIGINT, interrupt_signal);
7891 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
7892 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
7893 SIGQUIT and we can't tell which one it will give us. */
7894 signal (SIGQUIT, interrupt_signal);
7895 #endif /* HAVE_TERMIO */
7897 /* Note SIGIO has been undef'd if FIONREAD is missing. */
7898 #ifdef SIGIO
7899 if (!noninteractive)
7900 signal (SIGIO, input_available_signal);
7901 #endif /* SIGIO */
7903 /* Use interrupt input by default, if it works and noninterrupt input
7904 has deficiencies. */
7906 #ifdef INTERRUPT_INPUT
7907 interrupt_input = 1;
7908 #else
7909 interrupt_input = 0;
7910 #endif
7912 /* Our VMS input only works by interrupts, as of now. */
7913 #ifdef VMS
7914 interrupt_input = 1;
7915 #endif
7917 sigfree ();
7918 dribble = 0;
7920 if (keyboard_init_hook)
7921 (*keyboard_init_hook) ();
7923 #ifdef POLL_FOR_INPUT
7924 poll_suppress_count = 1;
7925 start_polling ();
7926 #endif
7929 /* This type's only use is in syms_of_keyboard, to initialize the
7930 event header symbols and put properties on them. */
7931 struct event_head {
7932 Lisp_Object *var;
7933 char *name;
7934 Lisp_Object *kind;
7937 struct event_head head_table[] = {
7938 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
7939 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
7940 &Qswitch_frame, "switch-frame", &Qswitch_frame,
7941 &Qdelete_frame, "delete-frame", &Qdelete_frame,
7942 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
7943 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
7946 syms_of_keyboard ()
7948 Qdisabled_command_hook = intern ("disabled-command-hook");
7949 staticpro (&Qdisabled_command_hook);
7951 Qself_insert_command = intern ("self-insert-command");
7952 staticpro (&Qself_insert_command);
7954 Qforward_char = intern ("forward-char");
7955 staticpro (&Qforward_char);
7957 Qbackward_char = intern ("backward-char");
7958 staticpro (&Qbackward_char);
7960 Qdisabled = intern ("disabled");
7961 staticpro (&Qdisabled);
7963 Qundefined = intern ("undefined");
7964 staticpro (&Qundefined);
7966 Qpre_command_hook = intern ("pre-command-hook");
7967 staticpro (&Qpre_command_hook);
7969 Qpost_command_hook = intern ("post-command-hook");
7970 staticpro (&Qpost_command_hook);
7972 Qpost_command_idle_hook = intern ("post-command-idle-hook");
7973 staticpro (&Qpost_command_idle_hook);
7975 Qdeferred_action_function = intern ("deferred-action-function");
7976 staticpro (&Qdeferred_action_function);
7978 Qcommand_hook_internal = intern ("command-hook-internal");
7979 staticpro (&Qcommand_hook_internal);
7981 Qfunction_key = intern ("function-key");
7982 staticpro (&Qfunction_key);
7983 Qmouse_click = intern ("mouse-click");
7984 staticpro (&Qmouse_click);
7985 Qtimer_event = intern ("timer-event");
7986 staticpro (&Qtimer_event);
7988 Qmenu_enable = intern ("menu-enable");
7989 staticpro (&Qmenu_enable);
7991 Qmode_line = intern ("mode-line");
7992 staticpro (&Qmode_line);
7993 Qvertical_line = intern ("vertical-line");
7994 staticpro (&Qvertical_line);
7995 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
7996 staticpro (&Qvertical_scroll_bar);
7997 Qmenu_bar = intern ("menu-bar");
7998 staticpro (&Qmenu_bar);
8000 Qabove_handle = intern ("above-handle");
8001 staticpro (&Qabove_handle);
8002 Qhandle = intern ("handle");
8003 staticpro (&Qhandle);
8004 Qbelow_handle = intern ("below-handle");
8005 staticpro (&Qbelow_handle);
8006 Qup = intern ("up");
8007 staticpro (&Qup);
8008 Qdown = intern ("down");
8009 staticpro (&Qdown);
8011 Qevent_kind = intern ("event-kind");
8012 staticpro (&Qevent_kind);
8013 Qevent_symbol_elements = intern ("event-symbol-elements");
8014 staticpro (&Qevent_symbol_elements);
8015 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
8016 staticpro (&Qevent_symbol_element_mask);
8017 Qmodifier_cache = intern ("modifier-cache");
8018 staticpro (&Qmodifier_cache);
8020 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
8021 staticpro (&Qrecompute_lucid_menubar);
8022 Qactivate_menubar_hook = intern ("activate-menubar-hook");
8023 staticpro (&Qactivate_menubar_hook);
8025 Qpolling_period = intern ("polling-period");
8026 staticpro (&Qpolling_period);
8029 struct event_head *p;
8031 for (p = head_table;
8032 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
8033 p++)
8035 *p->var = intern (p->name);
8036 staticpro (p->var);
8037 Fput (*p->var, Qevent_kind, *p->kind);
8038 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
8042 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
8043 staticpro (&button_down_location);
8046 int i;
8047 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
8049 modifier_symbols = Fmake_vector (make_number (len), Qnil);
8050 for (i = 0; i < len; i++)
8051 if (modifier_names[i])
8052 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
8053 staticpro (&modifier_symbols);
8056 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
8057 staticpro (&recent_keys);
8059 this_command_keys = Fmake_vector (make_number (40), Qnil);
8060 staticpro (&this_command_keys);
8062 Qextended_command_history = intern ("extended-command-history");
8063 Fset (Qextended_command_history, Qnil);
8064 staticpro (&Qextended_command_history);
8066 kbd_buffer_frame_or_window
8067 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
8068 staticpro (&kbd_buffer_frame_or_window);
8070 accent_key_syms = Qnil;
8071 staticpro (&accent_key_syms);
8073 func_key_syms = Qnil;
8074 staticpro (&func_key_syms);
8076 mouse_syms = Qnil;
8077 staticpro (&mouse_syms);
8079 unread_switch_frame = Qnil;
8080 staticpro (&unread_switch_frame);
8082 internal_last_event_frame = Qnil;
8083 staticpro (&internal_last_event_frame);
8085 read_key_sequence_cmd = Qnil;
8086 staticpro (&read_key_sequence_cmd);
8088 defsubr (&Sevent_convert_list);
8089 defsubr (&Sread_key_sequence);
8090 defsubr (&Srecursive_edit);
8091 #ifdef HAVE_MOUSE
8092 defsubr (&Strack_mouse);
8093 #endif
8094 defsubr (&Sinput_pending_p);
8095 defsubr (&Scommand_execute);
8096 defsubr (&Srecent_keys);
8097 defsubr (&Sthis_command_keys);
8098 defsubr (&Sthis_single_command_keys);
8099 defsubr (&Sreset_this_command_lengths);
8100 defsubr (&Ssuspend_emacs);
8101 defsubr (&Sabort_recursive_edit);
8102 defsubr (&Sexit_recursive_edit);
8103 defsubr (&Srecursion_depth);
8104 defsubr (&Stop_level);
8105 defsubr (&Sdiscard_input);
8106 defsubr (&Sopen_dribble_file);
8107 defsubr (&Sset_input_mode);
8108 defsubr (&Scurrent_input_mode);
8109 defsubr (&Sexecute_extended_command);
8111 DEFVAR_LISP ("last-command-char", &last_command_char,
8112 "Last input event that was part of a command.");
8114 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
8115 "Last input event that was part of a command.");
8117 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
8118 "Last input event in a command, except for mouse menu events.\n\
8119 Mouse menus give back keys that don't look like mouse events;\n\
8120 this variable holds the actual mouse event that led to the menu,\n\
8121 so that you can determine whether the command was run by mouse or not.");
8123 DEFVAR_LISP ("last-input-char", &last_input_char,
8124 "Last input event.");
8126 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
8127 "Last input event.");
8129 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
8130 "List of objects to be read as next command input events.");
8132 DEFVAR_INT ("unread-command-char", &unread_command_char,
8133 "If not -1, an object to be read as next command input event.");
8135 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
8136 "Meta-prefix character code. Meta-foo as command input\n\
8137 turns into this character followed by foo.");
8138 XSETINT (meta_prefix_char, 033);
8140 DEFVAR_KBOARD ("last-command", Vlast_command,
8141 "The last command executed. Normally a symbol with a function definition,\n\
8142 but can be whatever was found in the keymap, or whatever the variable\n\
8143 `this-command' was set to by that command.\n\
8145 The value `mode-exit' is special; it means that the previous command\n\
8146 read an event that told it to exit, and it did so and unread that event.\n\
8147 In other words, the present command is the event that made the previous\n\
8148 command exit.\n\
8150 The value `kill-region' is special; it means that the previous command\n\
8151 was a kill command.");
8153 DEFVAR_LISP ("this-command", &this_command,
8154 "The command now being executed.\n\
8155 The command can set this variable; whatever is put here\n\
8156 will be in `last-command' during the following command.");
8157 this_command = Qnil;
8159 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
8160 "*Number of keyboard input characters between auto-saves.\n\
8161 Zero means disable autosaving due to number of characters typed.");
8162 auto_save_interval = 300;
8164 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
8165 "*Number of seconds idle time before auto-save.\n\
8166 Zero or nil means disable auto-saving due to idleness.\n\
8167 After auto-saving due to this many seconds of idle time,\n\
8168 Emacs also does a garbage collection if that seems to be warranted.");
8169 XSETFASTINT (Vauto_save_timeout, 30);
8171 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
8172 "*Nonzero means echo unfinished commands after this many seconds of pause.");
8173 echo_keystrokes = 1;
8175 DEFVAR_INT ("polling-period", &polling_period,
8176 "*Interval between polling for input during Lisp execution.\n\
8177 The reason for polling is to make C-g work to stop a running program.\n\
8178 Polling is needed only when using X windows and SIGIO does not work.\n\
8179 Polling is automatically disabled in all other cases.");
8180 polling_period = 2;
8182 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
8183 "*Maximum time between mouse clicks to make a double-click.\n\
8184 Measured in milliseconds. nil means disable double-click recognition;\n\
8185 t means double-clicks have no time limit and are detected\n\
8186 by position only.");
8187 Vdouble_click_time = make_number (500);
8189 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
8190 "*Non-nil means inhibit local map menu bar menus.");
8191 inhibit_local_menu_bar_menus = 0;
8193 DEFVAR_INT ("num-input-keys", &num_input_keys,
8194 "Number of complete key sequences read from the keyboard so far.\n\
8195 This includes key sequences read from keyboard macros.\n\
8196 The number is effectively the number of interactive command invocations.");
8197 num_input_keys = 0;
8199 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
8200 "The frame in which the most recently read event occurred.\n\
8201 If the last event came from a keyboard macro, this is set to `macro'.");
8202 Vlast_event_frame = Qnil;
8204 DEFVAR_LISP ("help-char", &Vhelp_char,
8205 "Character to recognize as meaning Help.\n\
8206 When it is read, do `(eval help-form)', and display result if it's a string.\n\
8207 If the value of `help-form' is nil, this char can be read normally.");
8208 XSETINT (Vhelp_char, Ctl ('H'));
8210 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
8211 "List of input events to recognize as meaning Help.\n\
8212 These work just like the value of `help-char' (see that).");
8213 Vhelp_event_list = Qnil;
8215 DEFVAR_LISP ("help-form", &Vhelp_form,
8216 "Form to execute when character `help-char' is read.\n\
8217 If the form returns a string, that string is displayed.\n\
8218 If `help-form' is nil, the help char is not recognized.");
8219 Vhelp_form = Qnil;
8221 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
8222 "Command to run when `help-char' character follows a prefix key.\n\
8223 This command is used only when there is no actual binding\n\
8224 for that character after that prefix key.");
8225 Vprefix_help_command = Qnil;
8227 DEFVAR_LISP ("top-level", &Vtop_level,
8228 "Form to evaluate when Emacs starts up.\n\
8229 Useful to set before you dump a modified Emacs.");
8230 Vtop_level = Qnil;
8232 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
8233 "Translate table for keyboard input, or nil.\n\
8234 Each character is looked up in this string and the contents used instead.\n\
8235 The value may be a string, a vector, or a char-table.\n\
8236 If it is a string or vector of length N,\n\
8237 character codes N and up are untranslated.\n\
8238 In a vector or a char-table, an element which is nil means \"no translation\".");
8239 Vkeyboard_translate_table = Qnil;
8241 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
8242 "Non-nil means to always spawn a subshell instead of suspending,\n\
8243 even if the operating system has support for stopping a process.");
8244 cannot_suspend = 0;
8246 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
8247 "Non-nil means prompt with menus when appropriate.\n\
8248 This is done when reading from a keymap that has a prompt string,\n\
8249 for elements that have prompt strings.\n\
8250 The menu is displayed on the screen\n\
8251 if X menus were enabled at configuration\n\
8252 time and the previous event was a mouse click prefix key.\n\
8253 Otherwise, menu prompting uses the echo area.");
8254 menu_prompting = 1;
8256 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
8257 "Character to see next line of menu prompt.\n\
8258 Type this character while in a menu prompt to rotate around the lines of it.");
8259 XSETINT (menu_prompt_more_char, ' ');
8261 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
8262 "A mask of additional modifier keys to use with every keyboard character.\n\
8263 Emacs applies the modifiers of the character stored here to each keyboard\n\
8264 character it reads. For example, after evaluating the expression\n\
8265 (setq extra-keyboard-modifiers ?\\C-x)\n\
8266 all input characters will have the control modifier applied to them.\n\
8268 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
8269 not count as a control character; rather, it counts as a character\n\
8270 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
8271 cancels any modification.");
8272 extra_keyboard_modifiers = 0;
8274 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
8275 "If an editing command sets this to t, deactivate the mark afterward.\n\
8276 The command loop sets this to nil before each command,\n\
8277 and tests the value when the command returns.\n\
8278 Buffer modification stores t in this variable.");
8279 Vdeactivate_mark = Qnil;
8281 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
8282 "Temporary storage of pre-command-hook or post-command-hook.");
8283 Vcommand_hook_internal = Qnil;
8285 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
8286 "Normal hook run before each command is executed.\n\
8287 Errors running the hook are caught and ignored.");
8288 Vpre_command_hook = Qnil;
8290 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
8291 "Normal hook run after each command is executed.\n\
8292 Errors running the hook are caught and ignored.");
8293 Vpost_command_hook = Qnil;
8295 DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
8296 "Normal hook run after each command is executed, if idle.\n\
8297 Errors running the hook are caught and ignored.\n\
8298 This feature is obsolete; use idle timers instead. See `etc/NEWS'.");
8299 Vpost_command_idle_hook = Qnil;
8301 DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
8302 "Delay time before running `post-command-idle-hook'.\n\
8303 This is measured in microseconds.");
8304 post_command_idle_delay = 100000;
8306 #if 0
8307 DEFVAR_LISP ("echo-area-clear-hook", ...,
8308 "Normal hook run when clearing the echo area.");
8309 #endif
8310 Qecho_area_clear_hook = intern ("echo-area-clear-hook");
8311 XSYMBOL (Qecho_area_clear_hook)->value = Qnil;
8313 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
8314 "t means menu bar, specified Lucid style, needs to be recomputed.");
8315 Vlucid_menu_bar_dirty_flag = Qnil;
8317 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
8318 "List of menu bar items to move to the end of the menu bar.\n\
8319 The elements of the list are event types that may have menu bar bindings.");
8320 Vmenu_bar_final_items = Qnil;
8322 DEFVAR_KBOARD ("overriding-terminal-local-map",
8323 Voverriding_terminal_local_map,
8324 "Keymap that overrides all other local keymaps.\n\
8325 If this variable is non-nil, it is used as a keymap instead of the\n\
8326 buffer's local map, and the minor mode keymaps and text property keymaps.");
8328 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
8329 "Keymap that overrides all other local keymaps.\n\
8330 If this variable is non-nil, it is used as a keymap instead of the\n\
8331 buffer's local map, and the minor mode keymaps and text property keymaps.");
8332 Voverriding_local_map = Qnil;
8334 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
8335 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
8336 Otherwise, the menu bar continues to reflect the buffer's local map\n\
8337 and the minor mode maps regardless of `overriding-local-map'.");
8338 Voverriding_local_map_menu_flag = Qnil;
8340 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
8341 "Keymap defining bindings for special events to execute at low level.");
8342 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
8344 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
8345 "*Non-nil means generate motion events for mouse motion.");
8347 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
8348 "Alist of system-specific X windows key symbols.\n\
8349 Each element should have the form (N . SYMBOL) where N is the\n\
8350 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
8351 and SYMBOL is its name.");
8353 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
8354 "List of deferred actions to be performed at a later time.\n\
8355 The precise format isn't relevant here; we just check whether it is nil.");
8356 Vdeferred_action_list = Qnil;
8358 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
8359 "Function to call to handle deferred actions, after each command.\n\
8360 This function is called with no arguments after each command\n\
8361 whenever `deferred-action-list' is non-nil.");
8362 Vdeferred_action_function = Qnil;
8364 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
8365 "Non-nil means show the equivalent key-binding when M-x command has one.\n\
8366 The value can be a length of time to show the message for.\n\
8367 If the value is non-nil and not a number, we wait 2 seconds.");
8368 Vsuggest_key_bindings = Qt;
8370 DEFVAR_LISP ("timer-list", &Vtimer_list,
8371 "List of active absolute time timers in order of increasing time");
8372 Vtimer_list = Qnil;
8374 DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
8375 "List of active idle-time timers in order of increasing time");
8376 Vtimer_idle_list = Qnil;
8379 keys_of_keyboard ()
8381 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
8382 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
8383 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
8384 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
8385 initial_define_key (meta_map, 'x', "execute-extended-command");
8387 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
8388 "handle-delete-frame");
8389 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
8390 "ignore-event");
8391 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
8392 "ignore-event");