(do-auto-fill): Check enable-kinsoku and enable-multibyte-characters.
[emacs.git] / src / keyboard.c
blobf299bd01e2db36f3cee75500fa8528284b9ba9bf
1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985,86,87,88,89,93,94,95,96,97 Free Software Foundation, Inc.
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;
317 Lisp_Object Qtimer_event_handler;
319 /* read_key_sequence stores here the command definition of the
320 key sequence that it reads. */
321 Lisp_Object read_key_sequence_cmd;
323 /* Form to evaluate (if non-nil) when Emacs is started. */
324 Lisp_Object Vtop_level;
326 /* User-supplied string to translate input characters through. */
327 Lisp_Object Vkeyboard_translate_table;
329 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
330 extern Lisp_Object Vfunction_key_map;
332 /* Another keymap that maps key sequences into key sequences.
333 This one takes precedence over ordinary definitions. */
334 extern Lisp_Object Vkey_translation_map;
336 /* Non-nil means deactivate the mark at end of this command. */
337 Lisp_Object Vdeactivate_mark;
339 /* Menu bar specified in Lucid Emacs fashion. */
341 Lisp_Object Vlucid_menu_bar_dirty_flag;
342 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
344 Lisp_Object Qecho_area_clear_hook;
346 /* Hooks to run before and after each command. */
347 Lisp_Object Qpre_command_hook, Vpre_command_hook;
348 Lisp_Object Qpost_command_hook, Vpost_command_hook;
349 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
350 /* Hook run after a command if there's no more input soon. */
351 Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook;
353 /* Delay time in microseconds before running post-command-idle-hook. */
354 int post_command_idle_delay;
356 /* List of deferred actions to be performed at a later time.
357 The precise format isn't relevant here; we just check whether it is nil. */
358 Lisp_Object Vdeferred_action_list;
360 /* Function to call to handle deferred actions, when there are any. */
361 Lisp_Object Vdeferred_action_function;
362 Lisp_Object Qdeferred_action_function;
364 /* File in which we write all commands we read. */
365 FILE *dribble;
367 /* Nonzero if input is available. */
368 int input_pending;
370 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
371 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
373 int meta_key;
375 extern char *pending_malloc_warning;
377 /* Circular buffer for pre-read keyboard input. */
378 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
380 /* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
382 The interrupt-level event handlers will never enqueue an event on a
383 frame which is not in Vframe_list, and once an event is dequeued,
384 internal_last_event_frame or the event itself points to the frame.
385 So that's all fine.
387 But while the event is sitting in the queue, it's completely
388 unprotected. Suppose the user types one command which will run for
389 a while and then delete a frame, and then types another event at
390 the frame that will be deleted, before the command gets around to
391 it. Suppose there are no references to this frame elsewhere in
392 Emacs, and a GC occurs before the second event is dequeued. Now we
393 have an event referring to a freed frame, which will crash Emacs
394 when it is dequeued.
396 Similar things happen when an event on a scroll bar is enqueued; the
397 window may be deleted while the event is in the queue.
399 So, we use this vector to protect the frame_or_window field in the
400 event queue. That way, they'll be dequeued as dead frames or
401 windows, but still valid lisp objects.
403 If kbd_buffer[i].kind != no_event, then
404 (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
405 == kbd_buffer[i].frame_or_window. */
406 static Lisp_Object kbd_buffer_frame_or_window;
408 /* Pointer to next available character in kbd_buffer.
409 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
410 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
411 next available char is in kbd_buffer[0]. */
412 static struct input_event *kbd_fetch_ptr;
414 /* Pointer to next place to store character in kbd_buffer. This
415 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
416 character should go in kbd_buffer[0]. */
417 static volatile struct input_event *kbd_store_ptr;
419 /* The above pair of variables forms a "queue empty" flag. When we
420 enqueue a non-hook event, we increment kbd_store_ptr. When we
421 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
422 there is input available iff the two pointers are not equal.
424 Why not just have a flag set and cleared by the enqueuing and
425 dequeuing functions? Such a flag could be screwed up by interrupts
426 at inopportune times. */
428 /* If this flag is non-nil, we check mouse_moved to see when the
429 mouse moves, and motion events will appear in the input stream.
430 Otherwise, mouse motion is ignored. */
431 static Lisp_Object do_mouse_tracking;
433 /* Symbols to head events. */
434 Lisp_Object Qmouse_movement;
435 Lisp_Object Qscroll_bar_movement;
436 Lisp_Object Qswitch_frame;
437 Lisp_Object Qdelete_frame;
438 Lisp_Object Qiconify_frame;
439 Lisp_Object Qmake_frame_visible;
441 /* Symbols to denote kinds of events. */
442 Lisp_Object Qfunction_key;
443 Lisp_Object Qmouse_click;
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 int 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))
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 else
2936 break;
2939 old_timers_run = timers_run;
2940 get_input_pending (&input_pending, 1);
2942 if (timers_run != old_timers_run && do_display)
2943 redisplay_preserve_echo_area ();
2946 static EMACS_TIME timer_idleness_start_time;
2948 /* Record the start of when Emacs is idle,
2949 for the sake of running idle-time timers. */
2951 timer_start_idle ()
2953 Lisp_Object timers;
2955 /* If we are already in the idle state, do nothing. */
2956 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
2957 return;
2959 EMACS_GET_TIME (timer_idleness_start_time);
2961 /* Mark all idle-time timers as once again candidates for running. */
2962 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCONS (timers)->cdr)
2964 Lisp_Object timer;
2966 timer = XCONS (timers)->car;
2968 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
2969 continue;
2970 XVECTOR (timer)->contents[0] = Qnil;
2974 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
2976 timer_stop_idle ()
2978 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
2981 /* This is only for debugging. */
2982 struct input_event last_timer_event;
2984 /* Check whether a timer has fired. To prevent larger problems we simply
2985 disregard elements that are not proper timers. Do not make a circular
2986 timer list for the time being.
2988 Returns the number of seconds to wait until the next timer fires. If a
2989 timer is triggering now, return zero seconds.
2990 If no timer is active, return -1 seconds.
2992 If a timer is ripe, we run it, with quitting turned off.
2994 DO_IT_NOW is now ignored. It used to mean that we should
2995 run the timer directly instead of queueing a timer-event.
2996 Now we always run timers directly. */
2998 EMACS_TIME
2999 timer_check (do_it_now)
3000 int do_it_now;
3002 EMACS_TIME nexttime;
3003 EMACS_TIME now, idleness_now;
3004 Lisp_Object timers, idle_timers, chosen_timer;
3005 struct gcpro gcpro1, gcpro2, gcpro3;
3007 EMACS_SET_SECS (nexttime, -1);
3008 EMACS_SET_USECS (nexttime, -1);
3010 /* Always consider the ordinary timers. */
3011 timers = Vtimer_list;
3012 /* Consider the idle timers only if Emacs is idle. */
3013 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3014 idle_timers = Vtimer_idle_list;
3015 else
3016 idle_timers = Qnil;
3017 chosen_timer = Qnil;
3018 GCPRO3 (timers, idle_timers, chosen_timer);
3020 if (CONSP (timers) || CONSP (idle_timers))
3022 EMACS_GET_TIME (now);
3023 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3024 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
3027 while (CONSP (timers) || CONSP (idle_timers))
3029 int triggertime = EMACS_SECS (now);
3030 Lisp_Object *vector;
3031 Lisp_Object timer, idle_timer;
3032 EMACS_TIME timer_time, idle_timer_time;
3033 EMACS_TIME difference, timer_difference, idle_timer_difference;
3035 /* Skip past invalid timers and timers already handled. */
3036 if (!NILP (timers))
3038 timer = XCONS (timers)->car;
3039 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3041 timers = XCONS (timers)->cdr;
3042 continue;
3044 vector = XVECTOR (timer)->contents;
3046 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3047 || !INTEGERP (vector[3])
3048 || ! NILP (vector[0]))
3050 timers = XCONS (timers)->cdr;
3051 continue;
3054 if (!NILP (idle_timers))
3056 timer = XCONS (idle_timers)->car;
3057 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3059 idle_timers = XCONS (idle_timers)->cdr;
3060 continue;
3062 vector = XVECTOR (timer)->contents;
3064 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3065 || !INTEGERP (vector[3])
3066 || ! NILP (vector[0]))
3068 idle_timers = XCONS (idle_timers)->cdr;
3069 continue;
3073 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
3074 based on the next ordinary timer.
3075 TIMER_DIFFERENCE is the distance in time from NOW to when
3076 this timer becomes ripe (negative if it's already ripe). */
3077 if (!NILP (timers))
3079 timer = XCONS (timers)->car;
3080 vector = XVECTOR (timer)->contents;
3081 EMACS_SET_SECS (timer_time,
3082 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3083 EMACS_SET_USECS (timer_time, XINT (vector[3]));
3084 EMACS_SUB_TIME (timer_difference, timer_time, now);
3087 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
3088 based on the next idle timer. */
3089 if (!NILP (idle_timers))
3091 idle_timer = XCONS (idle_timers)->car;
3092 vector = XVECTOR (idle_timer)->contents;
3093 EMACS_SET_SECS (idle_timer_time,
3094 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3095 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
3096 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
3099 /* Decide which timer is the next timer,
3100 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
3101 Also step down the list where we found that timer. */
3103 if (! NILP (timers) && ! NILP (idle_timers))
3105 EMACS_TIME temp;
3106 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
3107 if (EMACS_TIME_NEG_P (temp))
3109 chosen_timer = timer;
3110 timers = XCONS (timers)->cdr;
3111 difference = timer_difference;
3113 else
3115 chosen_timer = idle_timer;
3116 idle_timers = XCONS (idle_timers)->cdr;
3117 difference = idle_timer_difference;
3120 else if (! NILP (timers))
3122 chosen_timer = timer;
3123 timers = XCONS (timers)->cdr;
3124 difference = timer_difference;
3126 else
3128 chosen_timer = idle_timer;
3129 idle_timers = XCONS (idle_timers)->cdr;
3130 difference = idle_timer_difference;
3132 vector = XVECTOR (chosen_timer)->contents;
3134 /* If timer is rupe, run it if it hasn't been run. */
3135 if (EMACS_TIME_NEG_P (difference)
3136 || (EMACS_SECS (difference) == 0
3137 && EMACS_USECS (difference) == 0))
3139 if (NILP (vector[0]))
3141 Lisp_Object tem;
3142 int was_locked = single_kboard;
3143 int count = specpdl_ptr - specpdl;
3145 /* Mark the timer as triggered to prevent problems if the lisp
3146 code fails to reschedule it right. */
3147 vector[0] = Qt;
3149 specbind (Qinhibit_quit, Qt);
3151 call1 (Qtimer_event_handler, chosen_timer);
3152 timers_run++;
3154 unbind_to (count, Qnil);
3156 /* Resume allowing input from any kboard, if that was true before. */
3157 if (!was_locked)
3158 any_kboard_state ();
3160 /* Since we have handled the event,
3161 we don't need to tell the caller to wake up and do it. */
3164 else
3165 /* When we encounter a timer that is still waiting,
3166 return the amount of time to wait before it is ripe. */
3168 UNGCPRO;
3169 return difference;
3173 /* No timers are pending in the future. */
3174 /* Return 0 if we generated an event, and -1 if not. */
3175 UNGCPRO;
3176 return nexttime;
3179 /* Caches for modify_event_symbol. */
3180 static Lisp_Object accent_key_syms;
3181 static Lisp_Object func_key_syms;
3182 static Lisp_Object mouse_syms;
3184 /* This is a list of keysym codes for special "accent" characters.
3185 It parallels lispy_accent_keys. */
3187 static int lispy_accent_codes[] =
3189 #ifdef XK_dead_circumflex
3190 XK_dead_circumflex,
3191 #else
3193 #endif
3194 #ifdef XK_dead_grave
3195 XK_dead_grave,
3196 #else
3198 #endif
3199 #ifdef XK_dead_tilde
3200 XK_dead_tilde,
3201 #else
3203 #endif
3204 #ifdef XK_dead_diaeresis
3205 XK_dead_diaeresis,
3206 #else
3208 #endif
3209 #ifdef XK_dead_macron
3210 XK_dead_macron,
3211 #else
3213 #endif
3214 #ifdef XK_dead_degree
3215 XK_dead_degree,
3216 #else
3218 #endif
3219 #ifdef XK_dead_acute
3220 XK_dead_acute,
3221 #else
3223 #endif
3224 #ifdef XK_dead_cedilla
3225 XK_dead_cedilla,
3226 #else
3228 #endif
3229 #ifdef XK_dead_breve
3230 XK_dead_breve,
3231 #else
3233 #endif
3234 #ifdef XK_dead_ogonek
3235 XK_dead_ogonek,
3236 #else
3238 #endif
3239 #ifdef XK_dead_caron
3240 XK_dead_caron,
3241 #else
3243 #endif
3244 #ifdef XK_dead_doubleacute
3245 XK_dead_doubleacute,
3246 #else
3248 #endif
3249 #ifdef XK_dead_abovedot
3250 XK_dead_abovedot,
3251 #else
3253 #endif
3256 /* This is a list of Lisp names for special "accent" characters.
3257 It parallels lispy_accent_codes. */
3259 static char *lispy_accent_keys[] =
3261 "dead-circumflex",
3262 "dead-grave",
3263 "dead-tilde",
3264 "dead-diaeresis",
3265 "dead-macron",
3266 "dead-degree",
3267 "dead-acute",
3268 "dead-cedilla",
3269 "dead-breve",
3270 "dead-ogonek",
3271 "dead-caron",
3272 "dead-doubleacute",
3273 "dead-abovedot",
3276 #ifdef HAVE_NTGUI
3277 #define FUNCTION_KEY_OFFSET 0x0
3279 char *lispy_function_keys[] =
3281 0, /* 0 */
3283 0, /* VK_LBUTTON 0x01 */
3284 0, /* VK_RBUTTON 0x02 */
3285 "cancel", /* VK_CANCEL 0x03 */
3286 0, /* VK_MBUTTON 0x04 */
3288 0, 0, 0, /* 0x05 .. 0x07 */
3290 "backspace", /* VK_BACK 0x08 */
3291 "tab", /* VK_TAB 0x09 */
3293 0, 0, /* 0x0A .. 0x0B */
3295 "clear", /* VK_CLEAR 0x0C */
3296 "return", /* VK_RETURN 0x0D */
3298 0, 0, /* 0x0E .. 0x0F */
3300 "shift", /* VK_SHIFT 0x10 */
3301 "control", /* VK_CONTROL 0x11 */
3302 "menu", /* VK_MENU 0x12 */
3303 "pause", /* VK_PAUSE 0x13 */
3304 "capital", /* VK_CAPITAL 0x14 */
3306 0, 0, 0, 0, 0, 0, /* 0x15 .. 0x1A */
3308 0, /* VK_ESCAPE 0x1B */
3310 0, 0, 0, 0, /* 0x1C .. 0x1F */
3312 0, /* VK_SPACE 0x20 */
3313 "prior", /* VK_PRIOR 0x21 */
3314 "next", /* VK_NEXT 0x22 */
3315 "end", /* VK_END 0x23 */
3316 "home", /* VK_HOME 0x24 */
3317 "left", /* VK_LEFT 0x25 */
3318 "up", /* VK_UP 0x26 */
3319 "right", /* VK_RIGHT 0x27 */
3320 "down", /* VK_DOWN 0x28 */
3321 "select", /* VK_SELECT 0x29 */
3322 "print", /* VK_PRINT 0x2A */
3323 "execute", /* VK_EXECUTE 0x2B */
3324 "snapshot", /* VK_SNAPSHOT 0x2C */
3325 "insert", /* VK_INSERT 0x2D */
3326 "delete", /* VK_DELETE 0x2E */
3327 "help", /* VK_HELP 0x2F */
3329 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
3331 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3333 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
3335 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
3337 0, 0, 0, 0, 0, 0, 0, 0, 0,
3338 0, 0, 0, 0, 0, 0, 0, 0, 0,
3339 0, 0, 0, 0, 0, 0, 0, 0,
3341 "lwindow", /* VK_LWIN 0x5B */
3342 "rwindow", /* VK_RWIN 0x5C */
3343 "apps", /* VK_APPS 0x5D */
3345 0, 0, /* 0x5E .. 0x5F */
3347 "kp-0", /* VK_NUMPAD0 0x60 */
3348 "kp-1", /* VK_NUMPAD1 0x61 */
3349 "kp-2", /* VK_NUMPAD2 0x62 */
3350 "kp-3", /* VK_NUMPAD3 0x63 */
3351 "kp-4", /* VK_NUMPAD4 0x64 */
3352 "kp-5", /* VK_NUMPAD5 0x65 */
3353 "kp-6", /* VK_NUMPAD6 0x66 */
3354 "kp-7", /* VK_NUMPAD7 0x67 */
3355 "kp-8", /* VK_NUMPAD8 0x68 */
3356 "kp-9", /* VK_NUMPAD9 0x69 */
3357 "kp-multiply", /* VK_MULTIPLY 0x6A */
3358 "kp-add", /* VK_ADD 0x6B */
3359 "kp-separator", /* VK_SEPARATOR 0x6C */
3360 "kp-subtract", /* VK_SUBTRACT 0x6D */
3361 "kp-decimal", /* VK_DECIMAL 0x6E */
3362 "kp-divide", /* VK_DIVIDE 0x6F */
3363 "f1", /* VK_F1 0x70 */
3364 "f2", /* VK_F2 0x71 */
3365 "f3", /* VK_F3 0x72 */
3366 "f4", /* VK_F4 0x73 */
3367 "f5", /* VK_F5 0x74 */
3368 "f6", /* VK_F6 0x75 */
3369 "f7", /* VK_F7 0x76 */
3370 "f8", /* VK_F8 0x77 */
3371 "f9", /* VK_F9 0x78 */
3372 "f10", /* VK_F10 0x79 */
3373 "f11", /* VK_F11 0x7A */
3374 "f12", /* VK_F12 0x7B */
3375 "f13", /* VK_F13 0x7C */
3376 "f14", /* VK_F14 0x7D */
3377 "f15", /* VK_F15 0x7E */
3378 "f16", /* VK_F16 0x7F */
3379 "f17", /* VK_F17 0x80 */
3380 "f18", /* VK_F18 0x81 */
3381 "f19", /* VK_F19 0x82 */
3382 "f20", /* VK_F20 0x83 */
3383 "f21", /* VK_F21 0x84 */
3384 "f22", /* VK_F22 0x85 */
3385 "f23", /* VK_F23 0x86 */
3386 "f24", /* VK_F24 0x87 */
3388 0, 0, 0, 0, /* 0x88 .. 0x8B */
3389 0, 0, 0, 0, /* 0x8C .. 0x8F */
3391 "kp-numlock", /* VK_NUMLOCK 0x90 */
3392 "scroll", /* VK_SCROLL 0x91 */
3394 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
3395 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
3396 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
3397 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
3398 "kp-end", /* VK_NUMPAD_END 0x96 */
3399 "kp-home", /* VK_NUMPAD_HOME 0x97 */
3400 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
3401 "kp-up", /* VK_NUMPAD_UP 0x99 */
3402 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
3403 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
3404 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
3405 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
3407 0, 0, /* 0x9E .. 0x9F */
3410 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
3411 * Used only as parameters to GetAsyncKeyState() and GetKeyState().
3412 * No other API or message will distinguish left and right keys this way.
3414 /* 0xA0 .. 0xEF */
3416 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3417 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3418 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3419 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3420 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3422 /* 0xF0 .. 0xF5 */
3424 0, 0, 0, 0, 0, 0,
3426 "attn", /* VK_ATTN 0xF6 */
3427 "crsel", /* VK_CRSEL 0xF7 */
3428 "exsel", /* VK_EXSEL 0xF8 */
3429 "ereof", /* VK_EREOF 0xF9 */
3430 "play", /* VK_PLAY 0xFA */
3431 "zoom", /* VK_ZOOM 0xFB */
3432 "noname", /* VK_NONAME 0xFC */
3433 "pa1", /* VK_PA1 0xFD */
3434 "oem_clear", /* VK_OEM_CLEAR 0xFE */
3437 #else /* not HAVE_NTGUI */
3439 #ifdef XK_kana_A
3440 static char *lispy_kana_keys[] =
3442 /* X Keysym value */
3443 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x400 .. 0x40f */
3444 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x410 .. 0x41f */
3445 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x420 .. 0x42f */
3446 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x430 .. 0x43f */
3447 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x440 .. 0x44f */
3448 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x450 .. 0x45f */
3449 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x460 .. 0x46f */
3450 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0,
3451 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x480 .. 0x48f */
3452 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x490 .. 0x49f */
3453 0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket",
3454 "kana-comma", "kana-conjunctive", "kana-WO", "kana-a",
3455 "kana-i", "kana-u", "kana-e", "kana-o",
3456 "kana-ya", "kana-yu", "kana-yo", "kana-tsu",
3457 "prolongedsound", "kana-A", "kana-I", "kana-U",
3458 "kana-E", "kana-O", "kana-KA", "kana-KI",
3459 "kana-KU", "kana-KE", "kana-KO", "kana-SA",
3460 "kana-SHI", "kana-SU", "kana-SE", "kana-SO",
3461 "kana-TA", "kana-CHI", "kana-TSU", "kana-TE",
3462 "kana-TO", "kana-NA", "kana-NI", "kana-NU",
3463 "kana-NE", "kana-NO", "kana-HA", "kana-HI",
3464 "kana-FU", "kana-HE", "kana-HO", "kana-MA",
3465 "kana-MI", "kana-MU", "kana-ME", "kana-MO",
3466 "kana-YA", "kana-YU", "kana-YO", "kana-RA",
3467 "kana-RI", "kana-RU", "kana-RE", "kana-RO",
3468 "kana-WA", "kana-N", "voicedsound", "semivoicedsound",
3469 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4e0 .. 0x4ef */
3470 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f0 .. 0x4ff */
3472 #endif /* XK_kana_A */
3474 #define FUNCTION_KEY_OFFSET 0xff00
3476 /* You'll notice that this table is arranged to be conveniently
3477 indexed by X Windows keysym values. */
3478 static char *lispy_function_keys[] =
3480 /* X Keysym value */
3482 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00 */
3483 "backspace",
3484 "tab",
3485 "linefeed",
3486 "clear",
3488 "return",
3489 0, 0,
3490 0, 0, 0, /* 0xff10 */
3491 "pause",
3492 0, 0, 0, 0, 0, 0, 0,
3493 "escape",
3494 0, 0, 0, 0,
3495 0, "kanji", "muhenkan",
3496 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff20...2f */
3497 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff30...3f */
3498 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
3500 "home", /* 0xff50 */ /* IsCursorKey */
3501 "left",
3502 "up",
3503 "right",
3504 "down",
3505 "prior",
3506 "next",
3507 "end",
3508 "begin",
3509 0, /* 0xff59 */
3510 0, 0, 0, 0, 0, 0,
3511 "select", /* 0xff60 */ /* IsMiscFunctionKey */
3512 "print",
3513 "execute",
3514 "insert",
3515 0, /* 0xff64 */
3516 "undo",
3517 "redo",
3518 "menu",
3519 "find",
3520 "cancel",
3521 "help",
3522 "break", /* 0xff6b */
3524 0, 0, 0, 0, 0, 0, 0, 0, "backtab", 0,
3525 0, /* 0xff76 */
3526 0, 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff7f */
3527 "kp-space", /* 0xff80 */ /* IsKeypadKey */
3528 0, 0, 0, 0, 0, 0, 0, 0,
3529 "kp-tab", /* 0xff89 */
3530 0, 0, 0,
3531 "kp-enter", /* 0xff8d */
3532 0, 0, 0,
3533 "kp-f1", /* 0xff91 */
3534 "kp-f2",
3535 "kp-f3",
3536 "kp-f4",
3537 "kp-home", /* 0xff95 */
3538 "kp-left",
3539 "kp-up",
3540 "kp-right",
3541 "kp-down",
3542 "kp-prior", /* kp-page-up */
3543 "kp-next", /* kp-page-down */
3544 "kp-end",
3545 "kp-begin",
3546 "kp-insert",
3547 "kp-delete",
3548 0, /* 0xffa0 */
3549 0, 0, 0, 0, 0, 0, 0, 0, 0,
3550 "kp-multiply", /* 0xffaa */
3551 "kp-add",
3552 "kp-separator",
3553 "kp-subtract",
3554 "kp-decimal",
3555 "kp-divide", /* 0xffaf */
3556 "kp-0", /* 0xffb0 */
3557 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
3558 0, /* 0xffba */
3559 0, 0,
3560 "kp-equal", /* 0xffbd */
3561 "f1", /* 0xffbe */ /* IsFunctionKey */
3562 "f2",
3563 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
3564 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
3565 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
3566 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
3567 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
3568 0, 0, 0, 0, 0, 0, 0, 0,
3569 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
3570 0, 0, 0, 0, 0, 0, 0, "delete"
3573 /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE. */
3574 #define ISO_FUNCTION_KEY_OFFSET 0xfe00
3576 static char *iso_lispy_function_keys[] =
3578 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe00 */
3579 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe08 */
3580 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe10 */
3581 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe18 */
3582 "iso-lefttab", /* 0xfe20 */
3583 "iso-move-line-up", "iso-move-line-down",
3584 "iso-partial-line-up", "iso-partial-line-down",
3585 "iso-partial-space-left", "iso-partial-space-right",
3586 "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */
3587 "iso-release-margin-left", "iso-release-margin-right",
3588 "iso-release-both-margins",
3589 "iso-fast-cursor-left", "iso-fast-cursor-right",
3590 "iso-fast-cursor-up", "iso-fast-cursor-down",
3591 "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */
3592 "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */
3595 #endif /* not HAVE_NTGUI */
3597 static char *lispy_mouse_names[] =
3599 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
3602 /* Scroll bar parts. */
3603 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
3604 Lisp_Object Qup, Qdown;
3606 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
3607 Lisp_Object *scroll_bar_parts[] = {
3608 &Qabove_handle, &Qhandle, &Qbelow_handle,
3609 &Qup, &Qdown,
3613 /* A vector, indexed by button number, giving the down-going location
3614 of currently depressed buttons, both scroll bar and non-scroll bar.
3616 The elements have the form
3617 (BUTTON-NUMBER MODIFIER-MASK . REST)
3618 where REST is the cdr of a position as it would be reported in the event.
3620 The make_lispy_event function stores positions here to tell the
3621 difference between click and drag events, and to store the starting
3622 location to be included in drag events. */
3624 static Lisp_Object button_down_location;
3626 /* Information about the most recent up-going button event: Which
3627 button, what location, and what time. */
3629 static int last_mouse_button;
3630 static int last_mouse_x;
3631 static int last_mouse_y;
3632 static unsigned long button_down_time;
3634 /* The maximum time between clicks to make a double-click,
3635 or Qnil to disable double-click detection,
3636 or Qt for no time limit. */
3637 Lisp_Object Vdouble_click_time;
3639 /* The number of clicks in this multiple-click. */
3641 int double_click_count;
3643 /* Given a struct input_event, build the lisp event which represents
3644 it. If EVENT is 0, build a mouse movement event from the mouse
3645 movement buffer, which should have a movement event in it.
3647 Note that events must be passed to this function in the order they
3648 are received; this function stores the location of button presses
3649 in order to build drag events when the button is released. */
3651 static Lisp_Object
3652 make_lispy_event (event)
3653 struct input_event *event;
3655 int i;
3657 switch (SWITCH_ENUM_CAST (event->kind))
3659 /* A simple keystroke. */
3660 case ascii_keystroke:
3662 Lisp_Object lispy_c;
3663 int c = event->code & 0377;
3664 /* Turn ASCII characters into control characters
3665 when proper. */
3666 if (event->modifiers & ctrl_modifier)
3667 c = make_ctrl_char (c);
3669 /* Add in the other modifier bits. We took care of ctrl_modifier
3670 just above, and the shift key was taken care of by the X code,
3671 and applied to control characters by make_ctrl_char. */
3672 c |= (event->modifiers
3673 & (meta_modifier | alt_modifier
3674 | hyper_modifier | super_modifier));
3675 button_down_time = 0;
3676 XSETFASTINT (lispy_c, c);
3677 return lispy_c;
3680 /* A function key. The symbol may need to have modifier prefixes
3681 tacked onto it. */
3682 case non_ascii_keystroke:
3683 button_down_time = 0;
3685 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
3686 if (event->code == lispy_accent_codes[i])
3687 return modify_event_symbol (i,
3688 event->modifiers,
3689 Qfunction_key, Qnil,
3690 lispy_accent_keys, &accent_key_syms,
3691 (sizeof (lispy_accent_keys)
3692 / sizeof (lispy_accent_keys[0])));
3694 /* Handle system-specific keysyms. */
3695 if (event->code & (1 << 28))
3697 /* We need to use an alist rather than a vector as the cache
3698 since we can't make a vector long enuf. */
3699 if (NILP (current_kboard->system_key_syms))
3700 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
3701 return modify_event_symbol (event->code,
3702 event->modifiers,
3703 Qfunction_key,
3704 current_kboard->Vsystem_key_alist,
3705 0, &current_kboard->system_key_syms,
3706 (unsigned)-1);
3709 #ifdef XK_kana_A
3710 if (event->code >= 0x400 && event->code < 0x500)
3711 return modify_event_symbol (event->code - 0x400,
3712 event->modifiers & ~shift_modifier,
3713 Qfunction_key, Qnil,
3714 lispy_kana_keys, &func_key_syms,
3715 (sizeof (lispy_kana_keys)
3716 / sizeof (lispy_kana_keys[0])));
3717 #endif /* XK_kana_A */
3719 if (event->code < FUNCTION_KEY_OFFSET
3720 && event->code >= ISO_FUNCTION_KEY_OFFSET)
3721 return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET,
3722 event->modifiers,
3723 Qfunction_key, Qnil,
3724 iso_lispy_function_keys, &func_key_syms,
3725 (sizeof (iso_lispy_function_keys)
3726 / sizeof (iso_lispy_function_keys[0])));
3727 else
3728 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
3729 event->modifiers,
3730 Qfunction_key, Qnil,
3731 lispy_function_keys, &func_key_syms,
3732 (sizeof (lispy_function_keys)
3733 / sizeof (lispy_function_keys[0])));
3735 #ifdef HAVE_MOUSE
3736 /* A mouse click. Figure out where it is, decide whether it's
3737 a press, click or drag, and build the appropriate structure. */
3738 case mouse_click:
3739 case scroll_bar_click:
3741 int button = event->code;
3742 int is_double;
3743 Lisp_Object position;
3744 Lisp_Object *start_pos_ptr;
3745 Lisp_Object start_pos;
3747 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
3748 abort ();
3750 /* Build the position as appropriate for this mouse click. */
3751 if (event->kind == mouse_click)
3753 int part;
3754 FRAME_PTR f = XFRAME (event->frame_or_window);
3755 Lisp_Object window;
3756 Lisp_Object posn;
3757 int row, column;
3759 /* Ignore mouse events that were made on frame that
3760 have been deleted. */
3761 if (! FRAME_LIVE_P (f))
3762 return Qnil;
3764 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
3765 &column, &row, NULL, 1);
3767 #ifndef USE_X_TOOLKIT
3768 /* In the non-toolkit version, clicks on the menu bar
3769 are ordinary button events in the event buffer.
3770 Distinguish them, and invoke the menu.
3772 (In the toolkit version, the toolkit handles the menu bar
3773 and Emacs doesn't know about it until after the user
3774 makes a selection.) */
3775 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
3776 && (event->modifiers & down_modifier))
3778 Lisp_Object items, item;
3779 int hpos;
3780 int i;
3782 #if 0
3783 /* Activate the menu bar on the down event. If the
3784 up event comes in before the menu code can deal with it,
3785 just ignore it. */
3786 if (! (event->modifiers & down_modifier))
3787 return Qnil;
3788 #endif
3790 item = Qnil;
3791 items = FRAME_MENU_BAR_ITEMS (f);
3792 for (i = 0; i < XVECTOR (items)->size; i += 4)
3794 Lisp_Object pos, string;
3795 string = XVECTOR (items)->contents[i + 1];
3796 pos = XVECTOR (items)->contents[i + 3];
3797 if (NILP (string))
3798 break;
3799 if (column >= XINT (pos)
3800 && column < XINT (pos) + XSTRING (string)->size)
3802 item = XVECTOR (items)->contents[i];
3803 break;
3807 position
3808 = Fcons (event->frame_or_window,
3809 Fcons (Qmenu_bar,
3810 Fcons (Fcons (event->x, event->y),
3811 Fcons (make_number (event->timestamp),
3812 Qnil))));
3814 return Fcons (item, Fcons (position, Qnil));
3816 #endif /* not USE_X_TOOLKIT */
3818 window = window_from_coordinates (f, column, row, &part);
3820 if (!WINDOWP (window))
3822 window = event->frame_or_window;
3823 posn = Qnil;
3825 else
3827 int pixcolumn, pixrow;
3828 column -= WINDOW_LEFT_MARGIN (XWINDOW (window));
3829 row -= XINT (XWINDOW (window)->top);
3830 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
3831 XSETINT (event->x, pixcolumn);
3832 XSETINT (event->y, pixrow);
3834 if (part == 1)
3835 posn = Qmode_line;
3836 else if (part == 2)
3837 posn = Qvertical_line;
3838 else
3839 XSETINT (posn,
3840 buffer_posn_from_coords (XWINDOW (window),
3841 column, row));
3844 position
3845 = Fcons (window,
3846 Fcons (posn,
3847 Fcons (Fcons (event->x, event->y),
3848 Fcons (make_number (event->timestamp),
3849 Qnil))));
3851 else
3853 Lisp_Object window;
3854 Lisp_Object portion_whole;
3855 Lisp_Object part;
3857 window = event->frame_or_window;
3858 portion_whole = Fcons (event->x, event->y);
3859 part = *scroll_bar_parts[(int) event->part];
3861 position
3862 = Fcons (window,
3863 Fcons (Qvertical_scroll_bar,
3864 Fcons (portion_whole,
3865 Fcons (make_number (event->timestamp),
3866 Fcons (part, Qnil)))));
3869 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
3871 start_pos = *start_pos_ptr;
3872 *start_pos_ptr = Qnil;
3874 is_double = (button == last_mouse_button
3875 && XINT (event->x) == last_mouse_x
3876 && XINT (event->y) == last_mouse_y
3877 && button_down_time != 0
3878 && (EQ (Vdouble_click_time, Qt)
3879 || (INTEGERP (Vdouble_click_time)
3880 && ((int)(event->timestamp - button_down_time)
3881 < XINT (Vdouble_click_time)))));
3882 last_mouse_button = button;
3883 last_mouse_x = XINT (event->x);
3884 last_mouse_y = XINT (event->y);
3886 /* If this is a button press, squirrel away the location, so
3887 we can decide later whether it was a click or a drag. */
3888 if (event->modifiers & down_modifier)
3890 if (is_double)
3892 double_click_count++;
3893 event->modifiers |= ((double_click_count > 2)
3894 ? triple_modifier
3895 : double_modifier);
3897 else
3898 double_click_count = 1;
3899 button_down_time = event->timestamp;
3900 *start_pos_ptr = Fcopy_alist (position);
3903 /* Now we're releasing a button - check the co-ordinates to
3904 see if this was a click or a drag. */
3905 else if (event->modifiers & up_modifier)
3907 /* If we did not see a down before this up,
3908 ignore the up. Probably this happened because
3909 the down event chose a menu item.
3910 It would be an annoyance to treat the release
3911 of the button that chose the menu item
3912 as a separate event. */
3914 if (!CONSP (start_pos))
3915 return Qnil;
3917 event->modifiers &= ~up_modifier;
3918 #if 0 /* Formerly we treated an up with no down as a click event. */
3919 if (!CONSP (start_pos))
3920 event->modifiers |= click_modifier;
3921 else
3922 #endif
3924 /* The third element of every position should be the (x,y)
3925 pair. */
3926 Lisp_Object down;
3928 down = Fnth (make_number (2), start_pos);
3929 if (EQ (event->x, XCONS (down)->car)
3930 && EQ (event->y, XCONS (down)->cdr))
3932 event->modifiers |= click_modifier;
3934 else
3936 button_down_time = 0;
3937 event->modifiers |= drag_modifier;
3939 /* Don't check is_double; treat this as multiple
3940 if the down-event was multiple. */
3941 if (double_click_count > 1)
3942 event->modifiers |= ((double_click_count > 2)
3943 ? triple_modifier
3944 : double_modifier);
3947 else
3948 /* Every mouse event should either have the down_modifier or
3949 the up_modifier set. */
3950 abort ();
3953 /* Get the symbol we should use for the mouse click. */
3954 Lisp_Object head;
3956 head = modify_event_symbol (button,
3957 event->modifiers,
3958 Qmouse_click, Qnil,
3959 lispy_mouse_names, &mouse_syms,
3960 (sizeof (lispy_mouse_names)
3961 / sizeof (lispy_mouse_names[0])));
3962 if (event->modifiers & drag_modifier)
3963 return Fcons (head,
3964 Fcons (start_pos,
3965 Fcons (position,
3966 Qnil)));
3967 else if (event->modifiers & (double_modifier | triple_modifier))
3968 return Fcons (head,
3969 Fcons (position,
3970 Fcons (make_number (double_click_count),
3971 Qnil)));
3972 else
3973 return Fcons (head,
3974 Fcons (position,
3975 Qnil));
3979 #ifdef WINDOWSNT
3980 case w32_scroll_bar_click:
3982 int button = event->code;
3983 int is_double;
3984 Lisp_Object position;
3985 Lisp_Object *start_pos_ptr;
3986 Lisp_Object start_pos;
3988 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
3989 abort ();
3992 Lisp_Object window;
3993 Lisp_Object portion_whole;
3994 Lisp_Object part;
3996 window = event->frame_or_window;
3997 portion_whole = Fcons (event->x, event->y);
3998 part = *scroll_bar_parts[(int) event->part];
4000 position =
4001 Fcons (window,
4002 Fcons (Qvertical_scroll_bar,
4003 Fcons (portion_whole,
4004 Fcons (make_number (event->timestamp),
4005 Fcons (part, Qnil)))));
4008 /* Always treat W32 scroll bar events as clicks. */
4009 event->modifiers |= click_modifier;
4012 /* Get the symbol we should use for the mouse click. */
4013 Lisp_Object head;
4015 head = modify_event_symbol (button,
4016 event->modifiers,
4017 Qmouse_click, Qnil,
4018 lispy_mouse_names, &mouse_syms,
4019 (sizeof (lispy_mouse_names)
4020 / sizeof (lispy_mouse_names[0])));
4021 return Fcons (head,
4022 Fcons (position,
4023 Qnil));
4026 #endif
4028 #endif /* HAVE_MOUSE */
4030 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
4031 case menu_bar_event:
4032 /* The event value is in the cdr of the frame_or_window slot. */
4033 if (!CONSP (event->frame_or_window))
4034 abort ();
4035 return XCONS (event->frame_or_window)->cdr;
4036 #endif
4038 /* The 'kind' field of the event is something we don't recognize. */
4039 default:
4040 abort ();
4044 #ifdef HAVE_MOUSE
4046 static Lisp_Object
4047 make_lispy_movement (frame, bar_window, part, x, y, time)
4048 FRAME_PTR frame;
4049 Lisp_Object bar_window;
4050 enum scroll_bar_part part;
4051 Lisp_Object x, y;
4052 unsigned long time;
4054 /* Is it a scroll bar movement? */
4055 if (frame && ! NILP (bar_window))
4057 Lisp_Object part_sym;
4059 part_sym = *scroll_bar_parts[(int) part];
4060 return Fcons (Qscroll_bar_movement,
4061 (Fcons (Fcons (bar_window,
4062 Fcons (Qvertical_scroll_bar,
4063 Fcons (Fcons (x, y),
4064 Fcons (make_number (time),
4065 Fcons (part_sym,
4066 Qnil))))),
4067 Qnil)));
4070 /* Or is it an ordinary mouse movement? */
4071 else
4073 int area;
4074 Lisp_Object window;
4075 Lisp_Object posn;
4076 int column, row;
4078 if (frame)
4080 /* It's in a frame; which window on that frame? */
4081 pixel_to_glyph_coords (frame, XINT (x), XINT (y), &column, &row,
4082 NULL, 1);
4083 window = window_from_coordinates (frame, column, row, &area);
4085 else
4086 window = Qnil;
4088 if (WINDOWP (window))
4090 int pixcolumn, pixrow;
4091 column -= WINDOW_LEFT_MARGIN (XWINDOW (window));
4092 row -= XINT (XWINDOW (window)->top);
4093 glyph_to_pixel_coords (frame, column, row, &pixcolumn, &pixrow);
4094 XSETINT (x, pixcolumn);
4095 XSETINT (y, pixrow);
4097 if (area == 1)
4098 posn = Qmode_line;
4099 else if (area == 2)
4100 posn = Qvertical_line;
4101 else
4102 XSETINT (posn,
4103 buffer_posn_from_coords (XWINDOW (window), column, row));
4105 else if (frame != 0)
4107 XSETFRAME (window, frame);
4108 posn = Qnil;
4110 else
4112 window = Qnil;
4113 posn = Qnil;
4114 XSETFASTINT (x, 0);
4115 XSETFASTINT (y, 0);
4118 return Fcons (Qmouse_movement,
4119 Fcons (Fcons (window,
4120 Fcons (posn,
4121 Fcons (Fcons (x, y),
4122 Fcons (make_number (time),
4123 Qnil)))),
4124 Qnil));
4128 #endif /* HAVE_MOUSE */
4130 /* Construct a switch frame event. */
4131 static Lisp_Object
4132 make_lispy_switch_frame (frame)
4133 Lisp_Object frame;
4135 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
4138 /* Manipulating modifiers. */
4140 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
4142 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
4143 SYMBOL's name of the end of the modifiers; the string from this
4144 position is the unmodified symbol name.
4146 This doesn't use any caches. */
4148 static int
4149 parse_modifiers_uncached (symbol, modifier_end)
4150 Lisp_Object symbol;
4151 int *modifier_end;
4153 struct Lisp_String *name;
4154 int i;
4155 int modifiers;
4157 CHECK_SYMBOL (symbol, 1);
4159 modifiers = 0;
4160 name = XSYMBOL (symbol)->name;
4162 for (i = 0; i+2 <= name->size; )
4164 int this_mod_end = 0;
4165 int this_mod = 0;
4167 /* See if the name continues with a modifier word.
4168 Check that the word appears, but don't check what follows it.
4169 Set this_mod and this_mod_end to record what we find. */
4171 switch (name->data[i])
4173 #define SINGLE_LETTER_MOD(BIT) \
4174 (this_mod_end = i + 1, this_mod = BIT)
4176 case 'A':
4177 SINGLE_LETTER_MOD (alt_modifier);
4178 break;
4180 case 'C':
4181 SINGLE_LETTER_MOD (ctrl_modifier);
4182 break;
4184 case 'H':
4185 SINGLE_LETTER_MOD (hyper_modifier);
4186 break;
4188 case 'M':
4189 SINGLE_LETTER_MOD (meta_modifier);
4190 break;
4192 case 'S':
4193 SINGLE_LETTER_MOD (shift_modifier);
4194 break;
4196 case 's':
4197 SINGLE_LETTER_MOD (super_modifier);
4198 break;
4200 #undef SINGLE_LETTER_MOD
4203 /* If we found no modifier, stop looking for them. */
4204 if (this_mod_end == 0)
4205 break;
4207 /* Check there is a dash after the modifier, so that it
4208 really is a modifier. */
4209 if (this_mod_end >= name->size || name->data[this_mod_end] != '-')
4210 break;
4212 /* This modifier is real; look for another. */
4213 modifiers |= this_mod;
4214 i = this_mod_end + 1;
4217 /* Should we include the `click' modifier? */
4218 if (! (modifiers & (down_modifier | drag_modifier
4219 | double_modifier | triple_modifier))
4220 && i + 7 == name->size
4221 && strncmp (name->data + i, "mouse-", 6) == 0
4222 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
4223 modifiers |= click_modifier;
4225 if (modifier_end)
4226 *modifier_end = i;
4228 return modifiers;
4231 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
4232 prepended to the string BASE[0..BASE_LEN-1].
4233 This doesn't use any caches. */
4234 static Lisp_Object
4235 apply_modifiers_uncached (modifiers, base, base_len)
4236 int modifiers;
4237 char *base;
4238 int base_len;
4240 /* Since BASE could contain nulls, we can't use intern here; we have
4241 to use Fintern, which expects a genuine Lisp_String, and keeps a
4242 reference to it. */
4243 char *new_mods =
4244 (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
4245 int mod_len;
4248 char *p = new_mods;
4250 /* Only the event queue may use the `up' modifier; it should always
4251 be turned into a click or drag event before presented to lisp code. */
4252 if (modifiers & up_modifier)
4253 abort ();
4255 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
4256 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
4257 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
4258 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
4259 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
4260 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
4261 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
4262 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
4263 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
4264 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
4265 /* The click modifier is denoted by the absence of other modifiers. */
4267 *p = '\0';
4269 mod_len = p - new_mods;
4273 Lisp_Object new_name;
4275 new_name = make_uninit_string (mod_len + base_len);
4276 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
4277 bcopy (base, XSTRING (new_name)->data + mod_len, base_len);
4279 return Fintern (new_name, Qnil);
4284 static char *modifier_names[] =
4286 "up", "down", "drag", "click", "double", "triple", 0, 0,
4287 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4288 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
4290 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
4292 static Lisp_Object modifier_symbols;
4294 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
4295 static Lisp_Object
4296 lispy_modifier_list (modifiers)
4297 int modifiers;
4299 Lisp_Object modifier_list;
4300 int i;
4302 modifier_list = Qnil;
4303 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
4304 if (modifiers & (1<<i))
4305 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
4306 modifier_list);
4308 return modifier_list;
4312 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
4313 where UNMODIFIED is the unmodified form of SYMBOL,
4314 MASK is the set of modifiers present in SYMBOL's name.
4315 This is similar to parse_modifiers_uncached, but uses the cache in
4316 SYMBOL's Qevent_symbol_element_mask property, and maintains the
4317 Qevent_symbol_elements property. */
4319 static Lisp_Object
4320 parse_modifiers (symbol)
4321 Lisp_Object symbol;
4323 Lisp_Object elements;
4325 elements = Fget (symbol, Qevent_symbol_element_mask);
4326 if (CONSP (elements))
4327 return elements;
4328 else
4330 int end;
4331 int modifiers = parse_modifiers_uncached (symbol, &end);
4332 Lisp_Object unmodified;
4333 Lisp_Object mask;
4335 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
4336 XSYMBOL (symbol)->name->size - end),
4337 Qnil);
4339 if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1))
4340 abort ();
4341 XSETFASTINT (mask, modifiers);
4342 elements = Fcons (unmodified, Fcons (mask, Qnil));
4344 /* Cache the parsing results on SYMBOL. */
4345 Fput (symbol, Qevent_symbol_element_mask,
4346 elements);
4347 Fput (symbol, Qevent_symbol_elements,
4348 Fcons (unmodified, lispy_modifier_list (modifiers)));
4350 /* Since we know that SYMBOL is modifiers applied to unmodified,
4351 it would be nice to put that in unmodified's cache.
4352 But we can't, since we're not sure that parse_modifiers is
4353 canonical. */
4355 return elements;
4359 /* Apply the modifiers MODIFIERS to the symbol BASE.
4360 BASE must be unmodified.
4362 This is like apply_modifiers_uncached, but uses BASE's
4363 Qmodifier_cache property, if present. It also builds
4364 Qevent_symbol_elements properties, since it has that info anyway.
4366 apply_modifiers copies the value of BASE's Qevent_kind property to
4367 the modified symbol. */
4368 static Lisp_Object
4369 apply_modifiers (modifiers, base)
4370 int modifiers;
4371 Lisp_Object base;
4373 Lisp_Object cache, index, entry, new_symbol;
4375 /* Mask out upper bits. We don't know where this value's been. */
4376 modifiers &= ((EMACS_INT)1 << VALBITS) - 1;
4378 /* The click modifier never figures into cache indices. */
4379 cache = Fget (base, Qmodifier_cache);
4380 XSETFASTINT (index, (modifiers & ~click_modifier));
4381 entry = assq_no_quit (index, cache);
4383 if (CONSP (entry))
4384 new_symbol = XCONS (entry)->cdr;
4385 else
4387 /* We have to create the symbol ourselves. */
4388 new_symbol = apply_modifiers_uncached (modifiers,
4389 XSYMBOL (base)->name->data,
4390 XSYMBOL (base)->name->size);
4392 /* Add the new symbol to the base's cache. */
4393 entry = Fcons (index, new_symbol);
4394 Fput (base, Qmodifier_cache, Fcons (entry, cache));
4396 /* We have the parsing info now for free, so add it to the caches. */
4397 XSETFASTINT (index, modifiers);
4398 Fput (new_symbol, Qevent_symbol_element_mask,
4399 Fcons (base, Fcons (index, Qnil)));
4400 Fput (new_symbol, Qevent_symbol_elements,
4401 Fcons (base, lispy_modifier_list (modifiers)));
4404 /* Make sure this symbol is of the same kind as BASE.
4406 You'd think we could just set this once and for all when we
4407 intern the symbol above, but reorder_modifiers may call us when
4408 BASE's property isn't set right; we can't assume that just
4409 because it has a Qmodifier_cache property it must have its
4410 Qevent_kind set right as well. */
4411 if (NILP (Fget (new_symbol, Qevent_kind)))
4413 Lisp_Object kind;
4415 kind = Fget (base, Qevent_kind);
4416 if (! NILP (kind))
4417 Fput (new_symbol, Qevent_kind, kind);
4420 return new_symbol;
4424 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
4425 return a symbol with the modifiers placed in the canonical order.
4426 Canonical order is alphabetical, except for down and drag, which
4427 always come last. The 'click' modifier is never written out.
4429 Fdefine_key calls this to make sure that (for example) C-M-foo
4430 and M-C-foo end up being equivalent in the keymap. */
4432 Lisp_Object
4433 reorder_modifiers (symbol)
4434 Lisp_Object symbol;
4436 /* It's hopefully okay to write the code this way, since everything
4437 will soon be in caches, and no consing will be done at all. */
4438 Lisp_Object parsed;
4440 parsed = parse_modifiers (symbol);
4441 return apply_modifiers ((int) XINT (XCONS (XCONS (parsed)->cdr)->car),
4442 XCONS (parsed)->car);
4446 /* For handling events, we often want to produce a symbol whose name
4447 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
4448 to some base, like the name of a function key or mouse button.
4449 modify_event_symbol produces symbols of this sort.
4451 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
4452 is the name of the i'th symbol. TABLE_SIZE is the number of elements
4453 in the table.
4455 Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
4456 NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
4458 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
4459 persist between calls to modify_event_symbol that it can use to
4460 store a cache of the symbols it's generated for this NAME_TABLE
4461 before. The object stored there may be a vector or an alist.
4463 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
4465 MODIFIERS is a set of modifier bits (as given in struct input_events)
4466 whose prefixes should be applied to the symbol name.
4468 SYMBOL_KIND is the value to be placed in the event_kind property of
4469 the returned symbol.
4471 The symbols we create are supposed to have an
4472 `event-symbol-elements' property, which lists the modifiers present
4473 in the symbol's name. */
4475 static Lisp_Object
4476 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
4477 name_table, symbol_table, table_size)
4478 int symbol_num;
4479 unsigned modifiers;
4480 Lisp_Object symbol_kind;
4481 Lisp_Object name_alist;
4482 char **name_table;
4483 Lisp_Object *symbol_table;
4484 unsigned int table_size;
4486 Lisp_Object value;
4487 Lisp_Object symbol_int;
4489 /* Get rid of the "vendor-specific" bit here. */
4490 XSETINT (symbol_int, symbol_num & 0xffffff);
4492 /* Is this a request for a valid symbol? */
4493 if (symbol_num < 0 || symbol_num >= table_size)
4494 return Qnil;
4496 if (CONSP (*symbol_table))
4497 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
4499 /* If *symbol_table doesn't seem to be initialized properly, fix that.
4500 *symbol_table should be a lisp vector TABLE_SIZE elements long,
4501 where the Nth element is the symbol for NAME_TABLE[N], or nil if
4502 we've never used that symbol before. */
4503 else
4505 if (! VECTORP (*symbol_table)
4506 || XVECTOR (*symbol_table)->size != table_size)
4508 Lisp_Object size;
4510 XSETFASTINT (size, table_size);
4511 *symbol_table = Fmake_vector (size, Qnil);
4514 value = XVECTOR (*symbol_table)->contents[symbol_num];
4517 /* Have we already used this symbol before? */
4518 if (NILP (value))
4520 /* No; let's create it. */
4521 if (!NILP (name_alist))
4522 value = Fcdr_safe (Fassq (symbol_int, name_alist));
4523 else if (name_table != 0 && name_table[symbol_num])
4524 value = intern (name_table[symbol_num]);
4526 #ifdef HAVE_WINDOW_SYSTEM
4527 if (NILP (value))
4529 char *name = x_get_keysym_name (symbol_num);
4530 if (name)
4531 value = intern (name);
4533 #endif
4535 if (NILP (value))
4537 char buf[20];
4538 sprintf (buf, "key-%d", symbol_num);
4539 value = intern (buf);
4542 if (CONSP (*symbol_table))
4543 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
4544 else
4545 XVECTOR (*symbol_table)->contents[symbol_num] = value;
4547 /* Fill in the cache entries for this symbol; this also
4548 builds the Qevent_symbol_elements property, which the user
4549 cares about. */
4550 apply_modifiers (modifiers & click_modifier, value);
4551 Fput (value, Qevent_kind, symbol_kind);
4554 /* Apply modifiers to that symbol. */
4555 return apply_modifiers (modifiers, value);
4558 /* Convert a list that represents an event type,
4559 such as (ctrl meta backspace), into the usual representation of that
4560 event type as a number or a symbol. */
4562 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
4563 "Convert the event description list EVENT-DESC to an event type.\n\
4564 EVENT-DESC should contain one base event type (a character or symbol)\n\
4565 and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\
4566 drag, down, double or triple). The base must be last.\n\
4567 The return value is an event type (a character or symbol) which\n\
4568 has the same base event type and all the specified modifiers.")
4569 (event_desc)
4570 Lisp_Object event_desc;
4572 Lisp_Object base;
4573 int modifiers = 0;
4574 Lisp_Object rest;
4576 base = Qnil;
4577 rest = event_desc;
4578 while (CONSP (rest))
4580 Lisp_Object elt;
4581 int this = 0;
4583 elt = XCONS (rest)->car;
4584 rest = XCONS (rest)->cdr;
4586 /* Given a symbol, see if it is a modifier name. */
4587 if (SYMBOLP (elt) && CONSP (rest))
4588 this = parse_solitary_modifier (elt);
4590 if (this != 0)
4591 modifiers |= this;
4592 else if (!NILP (base))
4593 error ("Two bases given in one event");
4594 else
4595 base = elt;
4599 /* Let the symbol A refer to the character A. */
4600 if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1)
4601 XSETINT (base, XSYMBOL (base)->name->data[0]);
4603 if (INTEGERP (base))
4605 /* Turn (shift a) into A. */
4606 if ((modifiers & shift_modifier) != 0
4607 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
4609 XSETINT (base, XINT (base) - ('a' - 'A'));
4610 modifiers &= ~shift_modifier;
4613 /* Turn (control a) into C-a. */
4614 if (modifiers & ctrl_modifier)
4615 return make_number ((modifiers & ~ctrl_modifier)
4616 | make_ctrl_char (XINT (base)));
4617 else
4618 return make_number (modifiers | XINT (base));
4620 else if (SYMBOLP (base))
4621 return apply_modifiers (modifiers, base);
4622 else
4623 error ("Invalid base event");
4626 /* Try to recognize SYMBOL as a modifier name.
4627 Return the modifier flag bit, or 0 if not recognized. */
4629 static int
4630 parse_solitary_modifier (symbol)
4631 Lisp_Object symbol;
4633 struct Lisp_String *name = XSYMBOL (symbol)->name;
4635 switch (name->data[0])
4637 #define SINGLE_LETTER_MOD(BIT) \
4638 if (name->size == 1) \
4639 return BIT;
4641 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
4642 if (LEN == name->size \
4643 && ! strncmp (name->data, NAME, LEN)) \
4644 return BIT;
4646 case 'A':
4647 SINGLE_LETTER_MOD (alt_modifier);
4648 break;
4650 case 'a':
4651 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
4652 break;
4654 case 'C':
4655 SINGLE_LETTER_MOD (ctrl_modifier);
4656 break;
4658 case 'c':
4659 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
4660 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
4661 break;
4663 case 'H':
4664 SINGLE_LETTER_MOD (hyper_modifier);
4665 break;
4667 case 'h':
4668 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
4669 break;
4671 case 'M':
4672 SINGLE_LETTER_MOD (meta_modifier);
4673 break;
4675 case 'm':
4676 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
4677 break;
4679 case 'S':
4680 SINGLE_LETTER_MOD (shift_modifier);
4681 break;
4683 case 's':
4684 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
4685 MULTI_LETTER_MOD (super_modifier, "super", 5);
4686 SINGLE_LETTER_MOD (super_modifier);
4687 break;
4689 case 'd':
4690 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
4691 MULTI_LETTER_MOD (down_modifier, "down", 4);
4692 MULTI_LETTER_MOD (double_modifier, "double", 6);
4693 break;
4695 case 't':
4696 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
4697 break;
4699 #undef SINGLE_LETTER_MOD
4700 #undef MULTI_LETTER_MOD
4703 return 0;
4706 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
4707 Such a list is not valid as an event,
4708 but it can be a Lucid-style event type list. */
4711 lucid_event_type_list_p (object)
4712 Lisp_Object object;
4714 Lisp_Object tail;
4716 if (! CONSP (object))
4717 return 0;
4719 for (tail = object; CONSP (tail); tail = XCONS (tail)->cdr)
4721 Lisp_Object elt;
4722 elt = XCONS (tail)->car;
4723 if (! (INTEGERP (elt) || SYMBOLP (elt)))
4724 return 0;
4727 return NILP (tail);
4730 /* Store into *addr a value nonzero if terminal input chars are available.
4731 Serves the purpose of ioctl (0, FIONREAD, addr)
4732 but works even if FIONREAD does not exist.
4733 (In fact, this may actually read some input.)
4735 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe. */
4737 static void
4738 get_input_pending (addr, do_timers_now)
4739 int *addr;
4740 int do_timers_now;
4742 /* First of all, have we already counted some input? */
4743 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
4745 /* If input is being read as it arrives, and we have none, there is none. */
4746 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
4747 return;
4749 /* Try to read some input and see how much we get. */
4750 gobble_input (0);
4751 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
4754 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
4757 gobble_input (expected)
4758 int expected;
4760 #ifndef VMS
4761 #ifdef SIGIO
4762 if (interrupt_input)
4764 SIGMASKTYPE mask;
4765 mask = sigblock (sigmask (SIGIO));
4766 read_avail_input (expected);
4767 sigsetmask (mask);
4769 else
4770 #ifdef POLL_FOR_INPUT
4771 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
4773 SIGMASKTYPE mask;
4774 mask = sigblock (sigmask (SIGALRM));
4775 read_avail_input (expected);
4776 sigsetmask (mask);
4778 else
4779 #endif
4780 #endif
4781 read_avail_input (expected);
4782 #endif
4785 /* Put a buffer_switch_event in the buffer
4786 so that read_key_sequence will notice the new current buffer. */
4788 record_asynch_buffer_change ()
4790 struct input_event event;
4791 Lisp_Object tem;
4793 event.kind = buffer_switch_event;
4794 event.frame_or_window = Qnil;
4796 #ifdef subprocesses
4797 /* We don't need a buffer-switch event unless Emacs is waiting for input.
4798 The purpose of the event is to make read_key_sequence look up the
4799 keymaps again. If we aren't in read_key_sequence, we don't need one,
4800 and the event could cause trouble by messing up (input-pending-p). */
4801 tem = Fwaiting_for_user_input_p ();
4802 if (NILP (tem))
4803 return;
4804 #else
4805 /* We never need these events if we have no asynchronous subprocesses. */
4806 return;
4807 #endif
4809 /* Make sure no interrupt happens while storing the event. */
4810 #ifdef SIGIO
4811 if (interrupt_input)
4813 SIGMASKTYPE mask;
4814 mask = sigblock (sigmask (SIGIO));
4815 kbd_buffer_store_event (&event);
4816 sigsetmask (mask);
4818 else
4819 #endif
4821 stop_polling ();
4822 kbd_buffer_store_event (&event);
4823 start_polling ();
4827 #ifndef VMS
4829 /* Read any terminal input already buffered up by the system
4830 into the kbd_buffer, but do not wait.
4832 EXPECTED should be nonzero if the caller knows there is some input.
4834 Except on VMS, all input is read by this function.
4835 If interrupt_input is nonzero, this function MUST be called
4836 only when SIGIO is blocked.
4838 Returns the number of keyboard chars read, or -1 meaning
4839 this is a bad time to try to read input. */
4841 static int
4842 read_avail_input (expected)
4843 int expected;
4845 struct input_event buf[KBD_BUFFER_SIZE];
4846 register int i;
4847 int nread;
4849 if (read_socket_hook)
4850 /* No need for FIONREAD or fcntl; just say don't wait. */
4851 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE, expected);
4852 else
4854 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
4855 the kbd_buffer can really hold. That may prevent loss
4856 of characters on some systems when input is stuffed at us. */
4857 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
4858 int n_to_read;
4860 /* Determine how many characters we should *try* to read. */
4861 #ifdef WINDOWSNT
4862 return 0;
4863 #else /* not WINDOWSNT */
4864 #ifdef MSDOS
4865 n_to_read = dos_keysns ();
4866 if (n_to_read == 0)
4867 return 0;
4868 #else /* not MSDOS */
4869 #ifdef FIONREAD
4870 /* Find out how much input is available. */
4871 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
4872 /* Formerly simply reported no input, but that sometimes led to
4873 a failure of Emacs to terminate.
4874 SIGHUP seems appropriate if we can't reach the terminal. */
4875 /* ??? Is it really right to send the signal just to this process
4876 rather than to the whole process group?
4877 Perhaps on systems with FIONREAD Emacs is alone in its group. */
4878 kill (getpid (), SIGHUP);
4879 if (n_to_read == 0)
4880 return 0;
4881 if (n_to_read > sizeof cbuf)
4882 n_to_read = sizeof cbuf;
4883 #else /* no FIONREAD */
4884 #if defined (USG) || defined (DGUX)
4885 /* Read some input if available, but don't wait. */
4886 n_to_read = sizeof cbuf;
4887 fcntl (input_fd, F_SETFL, O_NDELAY);
4888 #else
4889 you lose;
4890 #endif
4891 #endif
4892 #endif /* not MSDOS */
4893 #endif /* not WINDOWSNT */
4895 /* Now read; for one reason or another, this will not block.
4896 NREAD is set to the number of chars read. */
4899 #ifdef MSDOS
4900 cbuf[0] = dos_keyread ();
4901 nread = 1;
4902 #else
4903 nread = read (input_fd, cbuf, n_to_read);
4904 #endif
4905 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
4906 /* The kernel sometimes fails to deliver SIGHUP for ptys.
4907 This looks incorrect, but it isn't, because _BSD causes
4908 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
4909 and that causes a value other than 0 when there is no input. */
4910 if (nread == 0)
4911 kill (0, SIGHUP);
4912 #endif
4914 while (
4915 /* We used to retry the read if it was interrupted.
4916 But this does the wrong thing when O_NDELAY causes
4917 an EAGAIN error. Does anybody know of a situation
4918 where a retry is actually needed? */
4919 #if 0
4920 nread < 0 && (errno == EAGAIN
4921 #ifdef EFAULT
4922 || errno == EFAULT
4923 #endif
4924 #ifdef EBADSLT
4925 || errno == EBADSLT
4926 #endif
4928 #else
4930 #endif
4933 #ifndef FIONREAD
4934 #if defined (USG) || defined (DGUX)
4935 fcntl (input_fd, F_SETFL, 0);
4936 #endif /* USG or DGUX */
4937 #endif /* no FIONREAD */
4938 for (i = 0; i < nread; i++)
4940 buf[i].kind = ascii_keystroke;
4941 buf[i].modifiers = 0;
4942 if (meta_key == 1 && (cbuf[i] & 0x80))
4943 buf[i].modifiers = meta_modifier;
4944 if (meta_key != 2)
4945 cbuf[i] &= ~0x80;
4947 buf[i].code = cbuf[i];
4948 XSETFRAME (buf[i].frame_or_window, selected_frame);
4952 /* Scan the chars for C-g and store them in kbd_buffer. */
4953 for (i = 0; i < nread; i++)
4955 kbd_buffer_store_event (&buf[i]);
4956 /* Don't look at input that follows a C-g too closely.
4957 This reduces lossage due to autorepeat on C-g. */
4958 if (buf[i].kind == ascii_keystroke
4959 && buf[i].code == quit_char)
4960 break;
4963 return nread;
4965 #endif /* not VMS */
4967 #ifdef SIGIO /* for entire page */
4968 /* Note SIGIO has been undef'd if FIONREAD is missing. */
4970 SIGTYPE
4971 input_available_signal (signo)
4972 int signo;
4974 /* Must preserve main program's value of errno. */
4975 int old_errno = errno;
4976 #ifdef BSD4_1
4977 extern int select_alarmed;
4978 #endif
4980 #if defined (USG) && !defined (POSIX_SIGNALS)
4981 /* USG systems forget handlers when they are used;
4982 must reestablish each time */
4983 signal (signo, input_available_signal);
4984 #endif /* USG */
4986 #ifdef BSD4_1
4987 sigisheld (SIGIO);
4988 #endif
4990 if (input_available_clear_time)
4991 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
4993 while (1)
4995 int nread;
4996 nread = read_avail_input (1);
4997 /* -1 means it's not ok to read the input now.
4998 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
4999 0 means there was no keyboard input available. */
5000 if (nread <= 0)
5001 break;
5003 #ifdef BSD4_1
5004 select_alarmed = 1; /* Force the select emulator back to life */
5005 #endif
5008 #ifdef BSD4_1
5009 sigfree ();
5010 #endif
5011 errno = old_errno;
5013 #endif /* SIGIO */
5015 /* Send ourselves a SIGIO.
5017 This function exists so that the UNBLOCK_INPUT macro in
5018 blockinput.h can have some way to take care of input we put off
5019 dealing with, without assuming that every file which uses
5020 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
5021 void
5022 reinvoke_input_signal ()
5024 #ifdef SIGIO
5025 kill (getpid (), SIGIO);
5026 #endif
5031 /* Return the prompt-string of a sparse keymap.
5032 This is the first element which is a string.
5033 Return nil if there is none. */
5035 Lisp_Object
5036 map_prompt (map)
5037 Lisp_Object map;
5039 while (CONSP (map))
5041 register Lisp_Object tem;
5042 tem = Fcar (map);
5043 if (STRINGP (tem))
5044 return tem;
5045 map = Fcdr (map);
5047 return Qnil;
5050 static void menu_bar_item ();
5051 static void menu_bar_one_keymap ();
5053 /* These variables hold the vector under construction within
5054 menu_bar_items and its subroutines, and the current index
5055 for storing into that vector. */
5056 static Lisp_Object menu_bar_items_vector;
5057 static int menu_bar_items_index;
5059 /* Return a vector of menu items for a menu bar, appropriate
5060 to the current buffer. Each item has three elements in the vector:
5061 KEY STRING MAPLIST.
5063 OLD is an old vector we can optionally reuse, or nil. */
5065 Lisp_Object
5066 menu_bar_items (old)
5067 Lisp_Object old;
5069 /* The number of keymaps we're scanning right now, and the number of
5070 keymaps we have allocated space for. */
5071 int nmaps;
5073 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
5074 in the current keymaps, or nil where it is not a prefix. */
5075 Lisp_Object *maps;
5077 Lisp_Object def, tem, tail;
5079 Lisp_Object result;
5081 int mapno;
5082 Lisp_Object oquit;
5084 int i;
5086 struct gcpro gcpro1;
5088 /* In order to build the menus, we need to call the keymap
5089 accessors. They all call QUIT. But this function is called
5090 during redisplay, during which a quit is fatal. So inhibit
5091 quitting while building the menus.
5092 We do this instead of specbind because (1) errors will clear it anyway
5093 and (2) this avoids risk of specpdl overflow. */
5094 oquit = Vinhibit_quit;
5095 Vinhibit_quit = Qt;
5097 if (!NILP (old))
5098 menu_bar_items_vector = old;
5099 else
5100 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
5101 menu_bar_items_index = 0;
5103 GCPRO1 (menu_bar_items_vector);
5105 /* Build our list of keymaps.
5106 If we recognize a function key and replace its escape sequence in
5107 keybuf with its symbol, or if the sequence starts with a mouse
5108 click and we need to switch buffers, we jump back here to rebuild
5109 the initial keymaps from the current buffer. */
5111 Lisp_Object *tmaps;
5113 /* Should overriding-terminal-local-map and overriding-local-map apply? */
5114 if (!NILP (Voverriding_local_map_menu_flag))
5116 /* Yes, use them (if non-nil) as well as the global map. */
5117 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
5118 nmaps = 0;
5119 if (!NILP (current_kboard->Voverriding_terminal_local_map))
5120 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
5121 if (!NILP (Voverriding_local_map))
5122 maps[nmaps++] = Voverriding_local_map;
5124 else
5126 /* No, so use major and minor mode keymaps. */
5127 nmaps = current_minor_maps (NULL, &tmaps);
5128 maps = (Lisp_Object *) alloca ((nmaps + 2) * sizeof (maps[0]));
5129 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
5130 #ifdef USE_TEXT_PROPERTIES
5131 maps[nmaps++] = get_local_map (PT, current_buffer);
5132 #else
5133 maps[nmaps++] = current_buffer->keymap;
5134 #endif
5136 maps[nmaps++] = current_global_map;
5139 /* Look up in each map the dummy prefix key `menu-bar'. */
5141 result = Qnil;
5143 for (mapno = nmaps - 1; mapno >= 0; mapno--)
5145 if (! NILP (maps[mapno]))
5146 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0));
5147 else
5148 def = Qnil;
5150 tem = Fkeymapp (def);
5151 if (!NILP (tem))
5152 menu_bar_one_keymap (def);
5155 /* Move to the end those items that should be at the end. */
5157 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCONS (tail)->cdr)
5159 int i;
5160 int end = menu_bar_items_index;
5162 for (i = 0; i < end; i += 4)
5163 if (EQ (XCONS (tail)->car, XVECTOR (menu_bar_items_vector)->contents[i]))
5165 Lisp_Object tem0, tem1, tem2, tem3;
5166 /* Move the item at index I to the end,
5167 shifting all the others forward. */
5168 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
5169 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
5170 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
5171 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
5172 if (end > i + 4)
5173 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
5174 &XVECTOR (menu_bar_items_vector)->contents[i],
5175 (end - i - 4) * sizeof (Lisp_Object));
5176 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
5177 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
5178 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
5179 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
5180 break;
5184 /* Add nil, nil, nil, nil at the end. */
5185 i = menu_bar_items_index;
5186 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
5188 Lisp_Object tem;
5189 int newsize = 2 * i;
5190 tem = Fmake_vector (make_number (2 * i), Qnil);
5191 bcopy (XVECTOR (menu_bar_items_vector)->contents,
5192 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
5193 menu_bar_items_vector = tem;
5195 /* Add this item. */
5196 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5197 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5198 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5199 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5200 menu_bar_items_index = i;
5202 Vinhibit_quit = oquit;
5203 UNGCPRO;
5204 return menu_bar_items_vector;
5207 /* Scan one map KEYMAP, accumulating any menu items it defines
5208 in menu_bar_items_vector. */
5210 static void
5211 menu_bar_one_keymap (keymap)
5212 Lisp_Object keymap;
5214 Lisp_Object tail, item, key, binding, item_string, table;
5216 /* Loop over all keymap entries that have menu strings. */
5217 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
5219 item = XCONS (tail)->car;
5220 if (CONSP (item))
5222 key = XCONS (item)->car;
5223 binding = XCONS (item)->cdr;
5224 if (CONSP (binding))
5226 item_string = XCONS (binding)->car;
5227 if (STRINGP (item_string))
5228 menu_bar_item (key, item_string, Fcdr (binding));
5230 else if (EQ (binding, Qundefined))
5231 menu_bar_item (key, Qnil, binding);
5233 else if (VECTORP (item))
5235 /* Loop over the char values represented in the vector. */
5236 int len = XVECTOR (item)->size;
5237 int c;
5238 for (c = 0; c < len; c++)
5240 Lisp_Object character;
5241 XSETFASTINT (character, c);
5242 binding = XVECTOR (item)->contents[c];
5243 if (CONSP (binding))
5245 item_string = XCONS (binding)->car;
5246 if (STRINGP (item_string))
5247 menu_bar_item (key, item_string, Fcdr (binding));
5249 else if (EQ (binding, Qundefined))
5250 menu_bar_item (key, Qnil, binding);
5256 /* This is used as the handler when calling internal_condition_case_1. */
5258 static Lisp_Object
5259 menu_bar_item_1 (arg)
5260 Lisp_Object arg;
5262 return Qnil;
5265 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
5266 If there's already an item for KEY, add this DEF to it. */
5268 static void
5269 menu_bar_item (key, item_string, def)
5270 Lisp_Object key, item_string, def;
5272 Lisp_Object tem;
5273 Lisp_Object enabled;
5274 int i;
5276 /* Skip menu-bar equiv keys data. */
5277 if (CONSP (def) && CONSP (XCONS (def)->car))
5278 def = XCONS (def)->cdr;
5280 if (EQ (def, Qundefined))
5282 /* If a map has an explicit `undefined' as definition,
5283 discard any previously made menu bar item. */
5285 for (i = 0; i < menu_bar_items_index; i += 4)
5286 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
5288 if (menu_bar_items_index > i + 4)
5289 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
5290 &XVECTOR (menu_bar_items_vector)->contents[i],
5291 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
5292 menu_bar_items_index -= 4;
5293 return;
5296 /* If there's no definition for this key yet,
5297 just ignore `undefined'. */
5298 return;
5301 /* See if this entry is enabled. */
5302 enabled = Qt;
5304 if (SYMBOLP (def))
5306 /* No property, or nil, means enable.
5307 Otherwise, enable if value is not nil. */
5308 tem = Fget (def, Qmenu_enable);
5309 if (!NILP (tem))
5310 /* (condition-case nil (eval tem)
5311 (error nil)) */
5312 enabled = internal_condition_case_1 (Feval, tem, Qerror,
5313 menu_bar_item_1);
5316 /* Ignore this item if it's not enabled. */
5317 if (NILP (enabled))
5318 return;
5320 /* Find any existing item for this KEY. */
5321 for (i = 0; i < menu_bar_items_index; i += 4)
5322 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
5323 break;
5325 /* If we did not find this KEY, add it at the end. */
5326 if (i == menu_bar_items_index)
5328 /* If vector is too small, get a bigger one. */
5329 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
5331 Lisp_Object tem;
5332 int newsize = 2 * i;
5333 tem = Fmake_vector (make_number (2 * i), Qnil);
5334 bcopy (XVECTOR (menu_bar_items_vector)->contents,
5335 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
5336 menu_bar_items_vector = tem;
5338 /* Add this item. */
5339 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
5340 XVECTOR (menu_bar_items_vector)->contents[i++] = item_string;
5341 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (def, Qnil);
5342 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
5343 menu_bar_items_index = i;
5345 /* We did find an item for this KEY. Add DEF to its list of maps. */
5346 else
5348 Lisp_Object old;
5349 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
5350 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (def, old);
5354 /* Read a character using menus based on maps in the array MAPS.
5355 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
5356 Return t if we displayed a menu but the user rejected it.
5358 PREV_EVENT is the previous input event, or nil if we are reading
5359 the first event of a key sequence.
5361 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
5362 if we used a mouse menu to read the input, or zero otherwise. If
5363 USED_MOUSE_MENU is null, we don't dereference it.
5365 The prompting is done based on the prompt-string of the map
5366 and the strings associated with various map elements.
5368 This can be done with X menus or with menus put in the minibuf.
5369 These are done in different ways, depending on how the input will be read.
5370 Menus using X are done after auto-saving in read-char, getting the input
5371 event from Fx_popup_menu; menus using the minibuf use read_char recursively
5372 and do auto-saving in the inner call of read_char. */
5374 static Lisp_Object
5375 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
5376 int nmaps;
5377 Lisp_Object *maps;
5378 Lisp_Object prev_event;
5379 int *used_mouse_menu;
5381 int mapno;
5382 register Lisp_Object name;
5383 Lisp_Object rest, vector;
5385 if (used_mouse_menu)
5386 *used_mouse_menu = 0;
5388 /* Use local over global Menu maps */
5390 if (! menu_prompting)
5391 return Qnil;
5393 /* Optionally disregard all but the global map. */
5394 if (inhibit_local_menu_bar_menus)
5396 maps += (nmaps - 1);
5397 nmaps = 1;
5400 /* Get the menu name from the first map that has one (a prompt string). */
5401 for (mapno = 0; mapno < nmaps; mapno++)
5403 name = map_prompt (maps[mapno]);
5404 if (!NILP (name))
5405 break;
5408 /* If we don't have any menus, just read a character normally. */
5409 if (mapno >= nmaps)
5410 return Qnil;
5412 #ifdef HAVE_MENUS
5413 /* If we got to this point via a mouse click,
5414 use a real menu for mouse selection. */
5415 if (EVENT_HAS_PARAMETERS (prev_event)
5416 && !EQ (XCONS (prev_event)->car, Qmenu_bar))
5418 /* Display the menu and get the selection. */
5419 Lisp_Object *realmaps
5420 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
5421 Lisp_Object value;
5422 int nmaps1 = 0;
5424 /* Use the maps that are not nil. */
5425 for (mapno = 0; mapno < nmaps; mapno++)
5426 if (!NILP (maps[mapno]))
5427 realmaps[nmaps1++] = maps[mapno];
5429 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
5430 if (CONSP (value))
5432 Lisp_Object tem;
5434 record_menu_key (XCONS (value)->car);
5436 /* If we got multiple events, unread all but
5437 the first.
5438 There is no way to prevent those unread events
5439 from showing up later in last_nonmenu_event.
5440 So turn symbol and integer events into lists,
5441 to indicate that they came from a mouse menu,
5442 so that when present in last_nonmenu_event
5443 they won't confuse things. */
5444 for (tem = XCONS (value)->cdr; !NILP (tem);
5445 tem = XCONS (tem)->cdr)
5447 record_menu_key (XCONS (tem)->car);
5448 if (SYMBOLP (XCONS (tem)->car)
5449 || INTEGERP (XCONS (tem)->car))
5450 XCONS (tem)->car
5451 = Fcons (XCONS (tem)->car, Qnil);
5454 /* If we got more than one event, put all but the first
5455 onto this list to be read later.
5456 Return just the first event now. */
5457 Vunread_command_events
5458 = nconc2 (XCONS (value)->cdr, Vunread_command_events);
5459 value = XCONS (value)->car;
5461 else if (NILP (value))
5462 value = Qt;
5463 if (used_mouse_menu)
5464 *used_mouse_menu = 1;
5465 return value;
5467 #endif /* HAVE_MENUS */
5468 return Qnil ;
5471 /* Buffer in use so far for the minibuf prompts for menu keymaps.
5472 We make this bigger when necessary, and never free it. */
5473 static char *read_char_minibuf_menu_text;
5474 /* Size of that buffer. */
5475 static int read_char_minibuf_menu_width;
5477 static Lisp_Object
5478 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
5479 int commandflag ;
5480 int nmaps;
5481 Lisp_Object *maps;
5483 int mapno;
5484 register Lisp_Object name;
5485 int nlength;
5486 int width = FRAME_WIDTH (selected_frame) - 4;
5487 int idx = -1;
5488 int nobindings = 1;
5489 Lisp_Object rest, vector;
5490 char *menu;
5492 if (! menu_prompting)
5493 return Qnil;
5495 /* Make sure we have a big enough buffer for the menu text. */
5496 if (read_char_minibuf_menu_text == 0)
5498 read_char_minibuf_menu_width = width + 4;
5499 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
5501 else if (width + 4 > read_char_minibuf_menu_width)
5503 read_char_minibuf_menu_width = width + 4;
5504 read_char_minibuf_menu_text
5505 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
5507 menu = read_char_minibuf_menu_text;
5509 /* Get the menu name from the first map that has one (a prompt string). */
5510 for (mapno = 0; mapno < nmaps; mapno++)
5512 name = map_prompt (maps[mapno]);
5513 if (!NILP (name))
5514 break;
5517 /* If we don't have any menus, just read a character normally. */
5518 if (mapno >= nmaps)
5519 return Qnil;
5521 /* Prompt string always starts with map's prompt, and a space. */
5522 strcpy (menu, XSTRING (name)->data);
5523 nlength = XSTRING (name)->size;
5524 menu[nlength++] = ':';
5525 menu[nlength++] = ' ';
5526 menu[nlength] = 0;
5528 /* Start prompting at start of first map. */
5529 mapno = 0;
5530 rest = maps[mapno];
5532 /* Present the documented bindings, a line at a time. */
5533 while (1)
5535 int notfirst = 0;
5536 int i = nlength;
5537 Lisp_Object obj;
5538 int ch;
5539 Lisp_Object orig_defn_macro;
5541 /* Loop over elements of map. */
5542 while (i < width)
5544 Lisp_Object s, elt;
5546 /* If reached end of map, start at beginning of next map. */
5547 if (NILP (rest))
5549 mapno++;
5550 /* At end of last map, wrap around to first map if just starting,
5551 or end this line if already have something on it. */
5552 if (mapno == nmaps)
5554 mapno = 0;
5555 if (notfirst || nobindings) break;
5557 rest = maps[mapno];
5560 /* Look at the next element of the map. */
5561 if (idx >= 0)
5562 elt = XVECTOR (vector)->contents[idx];
5563 else
5564 elt = Fcar_safe (rest);
5566 if (idx < 0 && VECTORP (elt))
5568 /* If we found a dense table in the keymap,
5569 advanced past it, but start scanning its contents. */
5570 rest = Fcdr_safe (rest);
5571 vector = elt;
5572 idx = 0;
5574 else
5576 /* An ordinary element. */
5577 Lisp_Object event;
5579 if (idx < 0)
5581 s = Fcar_safe (Fcdr_safe (elt)); /* alist */
5582 event = Fcar_safe (elt);
5584 else
5586 s = Fcar_safe (elt); /* vector */
5587 XSETINT (event, idx);
5590 /* Ignore the element if it has no prompt string. */
5591 if (STRINGP (s) && INTEGERP (event))
5593 /* 1 if the char to type matches the string. */
5594 int char_matches;
5595 Lisp_Object upcased_event, downcased_event;
5596 Lisp_Object desc;
5598 upcased_event = Fupcase (event);
5599 downcased_event = Fdowncase (event);
5600 char_matches = (XINT (upcased_event) == XSTRING (s)->data[0]
5601 || XINT (downcased_event) == XSTRING (s)->data[0]);
5602 if (! char_matches)
5603 desc = Fsingle_key_description (event);
5605 /* If we have room for the prompt string, add it to this line.
5606 If this is the first on the line, always add it. */
5607 if ((XSTRING (s)->size + i + 2
5608 + (char_matches ? 0 : XSTRING (desc)->size + 3))
5609 < width
5610 || !notfirst)
5612 int thiswidth;
5614 /* Punctuate between strings. */
5615 if (notfirst)
5617 strcpy (menu + i, ", ");
5618 i += 2;
5620 notfirst = 1;
5621 nobindings = 0 ;
5623 /* If the char to type doesn't match the string's
5624 first char, explicitly show what char to type. */
5625 if (! char_matches)
5627 /* Add as much of string as fits. */
5628 thiswidth = XSTRING (desc)->size;
5629 if (thiswidth + i > width)
5630 thiswidth = width - i;
5631 bcopy (XSTRING (desc)->data, menu + i, thiswidth);
5632 i += thiswidth;
5633 strcpy (menu + i, " = ");
5634 i += 3;
5637 /* Add as much of string as fits. */
5638 thiswidth = XSTRING (s)->size;
5639 if (thiswidth + i > width)
5640 thiswidth = width - i;
5641 bcopy (XSTRING (s)->data, menu + i, thiswidth);
5642 i += thiswidth;
5643 menu[i] = 0;
5645 else
5647 /* If this element does not fit, end the line now,
5648 and save the element for the next line. */
5649 strcpy (menu + i, "...");
5650 break;
5654 /* Move past this element. */
5655 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
5656 /* Handle reaching end of dense table. */
5657 idx = -1;
5658 if (idx >= 0)
5659 idx++;
5660 else
5661 rest = Fcdr_safe (rest);
5665 /* Prompt with that and read response. */
5666 message1 (menu);
5668 /* Make believe its not a keyboard macro in case the help char
5669 is pressed. Help characters are not recorded because menu prompting
5670 is not used on replay.
5672 orig_defn_macro = current_kboard->defining_kbd_macro;
5673 current_kboard->defining_kbd_macro = Qnil;
5675 obj = read_char (commandflag, 0, 0, Qnil, 0);
5676 while (BUFFERP (obj));
5677 current_kboard->defining_kbd_macro = orig_defn_macro;
5679 if (!INTEGERP (obj))
5680 return obj;
5681 else
5682 ch = XINT (obj);
5684 if (! EQ (obj, menu_prompt_more_char)
5685 && (!INTEGERP (menu_prompt_more_char)
5686 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
5688 if (!NILP (current_kboard->defining_kbd_macro))
5689 store_kbd_macro_char (obj);
5690 return obj;
5692 /* Help char - go round again */
5696 /* Reading key sequences. */
5698 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
5699 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
5700 keymap, or nil otherwise. Return the index of the first keymap in
5701 which KEY has any binding, or NMAPS if no map has a binding.
5703 If KEY is a meta ASCII character, treat it like meta-prefix-char
5704 followed by the corresponding non-meta character. Keymaps in
5705 CURRENT with non-prefix bindings for meta-prefix-char become nil in
5706 NEXT.
5708 If KEY has no bindings in any of the CURRENT maps, NEXT is left
5709 unmodified.
5711 NEXT may be the same array as CURRENT. */
5713 static int
5714 follow_key (key, nmaps, current, defs, next)
5715 Lisp_Object key;
5716 Lisp_Object *current, *defs, *next;
5717 int nmaps;
5719 int i, first_binding;
5720 int did_meta = 0;
5722 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
5723 followed by the corresponding non-meta character.
5724 Put the results into DEFS, since we are going to alter that anyway.
5725 Do not alter CURRENT or NEXT. */
5726 if (INTEGERP (key) && (XINT (key) & CHAR_META))
5728 for (i = 0; i < nmaps; i++)
5729 if (! NILP (current[i]))
5731 Lisp_Object def;
5732 def = get_keyelt (access_keymap (current[i],
5733 meta_prefix_char, 1, 0));
5735 /* Note that since we pass the resulting bindings through
5736 get_keymap_1, non-prefix bindings for meta-prefix-char
5737 disappear. */
5738 defs[i] = get_keymap_1 (def, 0, 1);
5740 else
5741 defs[i] = Qnil;
5743 did_meta = 1;
5744 XSETINT (key, XFASTINT (key) & ~CHAR_META);
5747 first_binding = nmaps;
5748 for (i = nmaps - 1; i >= 0; i--)
5750 if (! NILP (current[i]))
5752 Lisp_Object map;
5753 if (did_meta)
5754 map = defs[i];
5755 else
5756 map = current[i];
5758 defs[i] = get_keyelt (access_keymap (map, key, 1, 0));
5759 if (! NILP (defs[i]))
5760 first_binding = i;
5762 else
5763 defs[i] = Qnil;
5766 /* Given the set of bindings we've found, produce the next set of maps. */
5767 if (first_binding < nmaps)
5768 for (i = 0; i < nmaps; i++)
5769 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
5771 return first_binding;
5774 /* Read a sequence of keys that ends with a non prefix character,
5775 storing it in KEYBUF, a buffer of size BUFSIZE.
5776 Prompt with PROMPT.
5777 Return the length of the key sequence stored.
5778 Return -1 if the user rejected a command menu.
5780 Echo starting immediately unless `prompt' is 0.
5782 Where a key sequence ends depends on the currently active keymaps.
5783 These include any minor mode keymaps active in the current buffer,
5784 the current buffer's local map, and the global map.
5786 If a key sequence has no other bindings, we check Vfunction_key_map
5787 to see if some trailing subsequence might be the beginning of a
5788 function key's sequence. If so, we try to read the whole function
5789 key, and substitute its symbolic name into the key sequence.
5791 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
5792 `double-' events into similar click events, if that would make them
5793 bound. We try to turn `triple-' events first into `double-' events,
5794 then into clicks.
5796 If we get a mouse click in a mode line, vertical divider, or other
5797 non-text area, we treat the click as if it were prefixed by the
5798 symbol denoting that area - `mode-line', `vertical-line', or
5799 whatever.
5801 If the sequence starts with a mouse click, we read the key sequence
5802 with respect to the buffer clicked on, not the current buffer.
5804 If the user switches frames in the midst of a key sequence, we put
5805 off the switch-frame event until later; the next call to
5806 read_char will return it.
5808 If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
5809 from the selected window's buffer. */
5811 static int
5812 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
5813 can_return_switch_frame, fix_current_buffer)
5814 Lisp_Object *keybuf;
5815 int bufsize;
5816 Lisp_Object prompt;
5817 int dont_downcase_last;
5818 int can_return_switch_frame;
5819 int fix_current_buffer;
5821 int count = specpdl_ptr - specpdl;
5823 /* How many keys there are in the current key sequence. */
5824 int t;
5826 /* The length of the echo buffer when we started reading, and
5827 the length of this_command_keys when we started reading. */
5828 int echo_start;
5829 int keys_start;
5831 /* The number of keymaps we're scanning right now, and the number of
5832 keymaps we have allocated space for. */
5833 int nmaps;
5834 int nmaps_allocated = 0;
5836 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
5837 the current keymaps. */
5838 Lisp_Object *defs;
5840 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
5841 in the current keymaps, or nil where it is not a prefix. */
5842 Lisp_Object *submaps;
5844 /* The local map to start out with at start of key sequence. */
5845 Lisp_Object orig_local_map;
5847 /* 1 if we have already considered switching to the local-map property
5848 of the place where a mouse click occurred. */
5849 int localized_local_map = 0;
5851 /* The index in defs[] of the first keymap that has a binding for
5852 this key sequence. In other words, the lowest i such that
5853 defs[i] is non-nil. */
5854 int first_binding;
5856 /* If t < mock_input, then KEYBUF[t] should be read as the next
5857 input key.
5859 We use this to recover after recognizing a function key. Once we
5860 realize that a suffix of the current key sequence is actually a
5861 function key's escape sequence, we replace the suffix with the
5862 function key's binding from Vfunction_key_map. Now keybuf
5863 contains a new and different key sequence, so the echo area,
5864 this_command_keys, and the submaps and defs arrays are wrong. In
5865 this situation, we set mock_input to t, set t to 0, and jump to
5866 restart_sequence; the loop will read keys from keybuf up until
5867 mock_input, thus rebuilding the state; and then it will resume
5868 reading characters from the keyboard. */
5869 int mock_input = 0;
5871 /* If the sequence is unbound in submaps[], then
5872 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
5873 and fkey_map is its binding.
5875 These might be > t, indicating that all function key scanning
5876 should hold off until t reaches them. We do this when we've just
5877 recognized a function key, to avoid searching for the function
5878 key's again in Vfunction_key_map. */
5879 int fkey_start = 0, fkey_end = 0;
5880 Lisp_Object fkey_map;
5882 /* Likewise, for key_translation_map. */
5883 int keytran_start = 0, keytran_end = 0;
5884 Lisp_Object keytran_map;
5886 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
5887 we put it off for later. While we're reading, we keep the event here. */
5888 Lisp_Object delayed_switch_frame;
5890 /* See the comment below... */
5891 #if defined (GOBBLE_FIRST_EVENT)
5892 Lisp_Object first_event;
5893 #endif
5895 Lisp_Object original_uppercase;
5896 int original_uppercase_position = -1;
5898 /* Gets around Microsoft compiler limitations. */
5899 int dummyflag = 0;
5901 struct buffer *starting_buffer;
5903 /* Nonzero if we seem to have got the beginning of a binding
5904 in function_key_map. */
5905 int function_key_possible = 0;
5906 int key_translation_possible = 0;
5908 /* Save the status of key translation before each step,
5909 so that we can restore this after downcasing. */
5910 Lisp_Object prev_fkey_map;
5911 Lisp_Object prev_fkey_start;
5912 Lisp_Object prev_fkey_end;
5914 Lisp_Object prev_keytran_map;
5915 Lisp_Object prev_keytran_start;
5916 Lisp_Object prev_keytran_end;
5918 int junk;
5920 last_nonmenu_event = Qnil;
5922 delayed_switch_frame = Qnil;
5923 fkey_map = Vfunction_key_map;
5924 keytran_map = Vkey_translation_map;
5926 /* If there is no function-key-map, turn off function key scanning. */
5927 if (NILP (Fkeymapp (Vfunction_key_map)))
5928 fkey_start = fkey_end = bufsize + 1;
5930 /* If there is no key-translation-map, turn off scanning. */
5931 if (NILP (Fkeymapp (Vkey_translation_map)))
5932 keytran_start = keytran_end = bufsize + 1;
5934 if (INTERACTIVE)
5936 if (!NILP (prompt))
5937 echo_prompt (XSTRING (prompt)->data);
5938 else if (cursor_in_echo_area && echo_keystrokes)
5939 /* This doesn't put in a dash if the echo buffer is empty, so
5940 you don't always see a dash hanging out in the minibuffer. */
5941 echo_dash ();
5944 /* Record the initial state of the echo area and this_command_keys;
5945 we will need to restore them if we replay a key sequence. */
5946 if (INTERACTIVE)
5947 echo_start = echo_length ();
5948 keys_start = this_command_key_count;
5949 this_single_command_key_start = keys_start;
5951 #if defined (GOBBLE_FIRST_EVENT)
5952 /* This doesn't quite work, because some of the things that read_char
5953 does cannot safely be bypassed. It seems too risky to try to make
5954 this work right. */
5956 /* Read the first char of the sequence specially, before setting
5957 up any keymaps, in case a filter runs and switches buffers on us. */
5958 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
5959 &junk);
5960 #endif /* GOBBLE_FIRST_EVENT */
5962 orig_local_map = get_local_map (PT, current_buffer);
5964 /* We jump here when the key sequence has been thoroughly changed, and
5965 we need to rescan it starting from the beginning. When we jump here,
5966 keybuf[0..mock_input] holds the sequence we should reread. */
5967 replay_sequence:
5969 starting_buffer = current_buffer;
5970 function_key_possible = 0;
5971 key_translation_possible = 0;
5973 /* Build our list of keymaps.
5974 If we recognize a function key and replace its escape sequence in
5975 keybuf with its symbol, or if the sequence starts with a mouse
5976 click and we need to switch buffers, we jump back here to rebuild
5977 the initial keymaps from the current buffer. */
5979 Lisp_Object *maps;
5981 if (!NILP (current_kboard->Voverriding_terminal_local_map)
5982 || !NILP (Voverriding_local_map))
5984 if (3 > nmaps_allocated)
5986 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
5987 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
5988 nmaps_allocated = 3;
5990 nmaps = 0;
5991 if (!NILP (current_kboard->Voverriding_terminal_local_map))
5992 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
5993 if (!NILP (Voverriding_local_map))
5994 submaps[nmaps++] = Voverriding_local_map;
5996 else
5998 nmaps = current_minor_maps (0, &maps);
5999 if (nmaps + 2 > nmaps_allocated)
6001 submaps = (Lisp_Object *) alloca ((nmaps+2) * sizeof (submaps[0]));
6002 defs = (Lisp_Object *) alloca ((nmaps+2) * sizeof (defs[0]));
6003 nmaps_allocated = nmaps + 2;
6005 bcopy (maps, submaps, nmaps * sizeof (submaps[0]));
6006 #ifdef USE_TEXT_PROPERTIES
6007 submaps[nmaps++] = orig_local_map;
6008 #else
6009 submaps[nmaps++] = current_buffer->keymap;
6010 #endif
6012 submaps[nmaps++] = current_global_map;
6015 /* Find an accurate initial value for first_binding. */
6016 for (first_binding = 0; first_binding < nmaps; first_binding++)
6017 if (! NILP (submaps[first_binding]))
6018 break;
6020 /* Start from the beginning in keybuf. */
6021 t = 0;
6023 /* These are no-ops the first time through, but if we restart, they
6024 revert the echo area and this_command_keys to their original state. */
6025 this_command_key_count = keys_start;
6026 if (INTERACTIVE && t < mock_input)
6027 echo_truncate (echo_start);
6029 /* If the best binding for the current key sequence is a keymap, or
6030 we may be looking at a function key's escape sequence, keep on
6031 reading. */
6032 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
6033 || (first_binding >= nmaps
6034 && fkey_start < t
6035 /* mock input is never part of a function key's sequence. */
6036 && mock_input <= fkey_start)
6037 || (first_binding >= nmaps
6038 && keytran_start < t && key_translation_possible)
6039 /* Don't return in the middle of a possible function key sequence,
6040 if the only bindings we found were via case conversion.
6041 Thus, if ESC O a has a function-key-map translation
6042 and ESC o has a binding, don't return after ESC O,
6043 so that we can translate ESC O plus the next character. */
6046 Lisp_Object key;
6047 int used_mouse_menu = 0;
6049 /* Where the last real key started. If we need to throw away a
6050 key that has expanded into more than one element of keybuf
6051 (say, a mouse click on the mode line which is being treated
6052 as [mode-line (mouse-...)], then we backtrack to this point
6053 of keybuf. */
6054 int last_real_key_start;
6056 /* These variables are analogous to echo_start and keys_start;
6057 while those allow us to restart the entire key sequence,
6058 echo_local_start and keys_local_start allow us to throw away
6059 just one key. */
6060 int echo_local_start, keys_local_start, local_first_binding;
6062 if (t >= bufsize)
6063 error ("Key sequence too long");
6065 if (INTERACTIVE)
6066 echo_local_start = echo_length ();
6067 keys_local_start = this_command_key_count;
6068 local_first_binding = first_binding;
6070 replay_key:
6071 /* These are no-ops, unless we throw away a keystroke below and
6072 jumped back up to replay_key; in that case, these restore the
6073 variables to their original state, allowing us to replay the
6074 loop. */
6075 if (INTERACTIVE && t < mock_input)
6076 echo_truncate (echo_local_start);
6077 this_command_key_count = keys_local_start;
6078 first_binding = local_first_binding;
6080 /* By default, assume each event is "real". */
6081 last_real_key_start = t;
6083 /* Does mock_input indicate that we are re-reading a key sequence? */
6084 if (t < mock_input)
6086 key = keybuf[t];
6087 add_command_key (key);
6088 if (echo_keystrokes)
6089 echo_char (key);
6092 /* If not, we should actually read a character. */
6093 else
6095 struct buffer *buf = current_buffer;
6098 #ifdef MULTI_KBOARD
6099 KBOARD *interrupted_kboard = current_kboard;
6100 struct frame *interrupted_frame = selected_frame;
6101 if (setjmp (wrong_kboard_jmpbuf))
6103 if (!NILP (delayed_switch_frame))
6105 interrupted_kboard->kbd_queue
6106 = Fcons (delayed_switch_frame,
6107 interrupted_kboard->kbd_queue);
6108 delayed_switch_frame = Qnil;
6110 while (t > 0)
6111 interrupted_kboard->kbd_queue
6112 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
6114 /* If the side queue is non-empty, ensure it begins with a
6115 switch-frame, so we'll replay it in the right context. */
6116 if (CONSP (interrupted_kboard->kbd_queue)
6117 && (key = XCONS (interrupted_kboard->kbd_queue)->car,
6118 !(EVENT_HAS_PARAMETERS (key)
6119 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
6120 Qswitch_frame))))
6122 Lisp_Object frame;
6123 XSETFRAME (frame, interrupted_frame);
6124 interrupted_kboard->kbd_queue
6125 = Fcons (make_lispy_switch_frame (frame),
6126 interrupted_kboard->kbd_queue);
6128 mock_input = 0;
6129 orig_local_map = get_local_map (PT, current_buffer);
6130 goto replay_sequence;
6132 #endif
6133 key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
6134 &used_mouse_menu);
6137 /* read_char returns t when it shows a menu and the user rejects it.
6138 Just return -1. */
6139 if (EQ (key, Qt))
6140 return -1;
6142 /* read_char returns -1 at the end of a macro.
6143 Emacs 18 handles this by returning immediately with a
6144 zero, so that's what we'll do. */
6145 if (INTEGERP (key) && XINT (key) == -1)
6147 t = 0;
6148 /* The Microsoft C compiler can't handle the goto that
6149 would go here. */
6150 dummyflag = 1;
6151 break;
6154 /* If the current buffer has been changed from under us, the
6155 keymap may have changed, so replay the sequence. */
6156 if (BUFFERP (key))
6158 mock_input = t;
6159 /* Reset the current buffer from the selected window
6160 in case something changed the former and not the latter.
6161 This is to be more consistent with the behavior
6162 of the command_loop_1. */
6163 if (fix_current_buffer)
6164 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
6165 Fset_buffer (XWINDOW (selected_window)->buffer);
6167 orig_local_map = get_local_map (PT, current_buffer);
6168 goto replay_sequence;
6171 /* If we have a quit that was typed in another frame, and
6172 quit_throw_to_read_char switched buffers,
6173 replay to get the right keymap. */
6174 if (XINT (key) == quit_char && current_buffer != starting_buffer)
6176 keybuf[t++] = key;
6177 mock_input = t;
6178 Vquit_flag = Qnil;
6179 orig_local_map = get_local_map (PT, current_buffer);
6180 goto replay_sequence;
6183 Vquit_flag = Qnil;
6186 /* Clicks in non-text areas get prefixed by the symbol
6187 in their CHAR-ADDRESS field. For example, a click on
6188 the mode line is prefixed by the symbol `mode-line'.
6190 Furthermore, key sequences beginning with mouse clicks
6191 are read using the keymaps of the buffer clicked on, not
6192 the current buffer. So we may have to switch the buffer
6193 here.
6195 When we turn one event into two events, we must make sure
6196 that neither of the two looks like the original--so that,
6197 if we replay the events, they won't be expanded again.
6198 If not for this, such reexpansion could happen either here
6199 or when user programs play with this-command-keys. */
6200 if (EVENT_HAS_PARAMETERS (key))
6202 Lisp_Object kind;
6204 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
6205 if (EQ (kind, Qmouse_click))
6207 Lisp_Object window, posn;
6209 window = POSN_WINDOW (EVENT_START (key));
6210 posn = POSN_BUFFER_POSN (EVENT_START (key));
6211 if (CONSP (posn))
6213 /* We're looking at the second event of a
6214 sequence which we expanded before. Set
6215 last_real_key_start appropriately. */
6216 if (t > 0)
6217 last_real_key_start = t - 1;
6220 /* Key sequences beginning with mouse clicks are
6221 read using the keymaps in the buffer clicked on,
6222 not the current buffer. If we're at the
6223 beginning of a key sequence, switch buffers. */
6224 if (last_real_key_start == 0
6225 && WINDOWP (window)
6226 && BUFFERP (XWINDOW (window)->buffer)
6227 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
6229 keybuf[t] = key;
6230 mock_input = t + 1;
6232 /* Arrange to go back to the original buffer once we're
6233 done reading the key sequence. Note that we can't
6234 use save_excursion_{save,restore} here, because they
6235 save point as well as the current buffer; we don't
6236 want to save point, because redisplay may change it,
6237 to accommodate a Fset_window_start or something. We
6238 don't want to do this at the top of the function,
6239 because we may get input from a subprocess which
6240 wants to change the selected window and stuff (say,
6241 emacsclient). */
6242 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
6244 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
6245 orig_local_map = get_local_map (PT, current_buffer);
6246 goto replay_sequence;
6248 /* For a mouse click, get the local text-property keymap
6249 of the place clicked on, rather than point. */
6250 if (last_real_key_start == 0 && CONSP (XCONS (key)->cdr)
6251 && ! localized_local_map)
6253 Lisp_Object map_here, start, pos;
6255 localized_local_map = 1;
6256 start = EVENT_START (key);
6257 if (CONSP (start) && CONSP (XCONS (start)->cdr))
6259 pos = POSN_BUFFER_POSN (start);
6260 if (INTEGERP (pos)
6261 && XINT (pos) >= BEG && XINT (pos) <= Z)
6263 map_here = get_local_map (XINT (pos), current_buffer);
6264 if (!EQ (map_here, orig_local_map))
6266 orig_local_map = map_here;
6267 keybuf[t] = key;
6268 mock_input = t + 1;
6270 goto replay_sequence;
6276 /* Expand mode-line and scroll-bar events into two events:
6277 use posn as a fake prefix key. */
6278 if (SYMBOLP (posn))
6280 if (t + 1 >= bufsize)
6281 error ("Key sequence too long");
6282 keybuf[t] = posn;
6283 keybuf[t+1] = key;
6284 mock_input = t + 2;
6286 /* Zap the position in key, so we know that we've
6287 expanded it, and don't try to do so again. */
6288 POSN_BUFFER_POSN (EVENT_START (key))
6289 = Fcons (posn, Qnil);
6290 goto replay_key;
6293 else if (EQ (kind, Qswitch_frame))
6295 /* If we're at the beginning of a key sequence, and the caller
6296 says it's okay, go ahead and return this event. If we're
6297 in the midst of a key sequence, delay it until the end. */
6298 if (t > 0 || !can_return_switch_frame)
6300 delayed_switch_frame = key;
6301 goto replay_key;
6304 else if (CONSP (XCONS (key)->cdr)
6305 && CONSP (EVENT_START (key))
6306 && CONSP (XCONS (EVENT_START (key))->cdr))
6308 Lisp_Object posn;
6310 posn = POSN_BUFFER_POSN (EVENT_START (key));
6311 /* Handle menu-bar events:
6312 insert the dummy prefix event `menu-bar'. */
6313 if (EQ (posn, Qmenu_bar))
6315 if (t + 1 >= bufsize)
6316 error ("Key sequence too long");
6317 keybuf[t] = posn;
6318 keybuf[t+1] = key;
6320 /* Zap the position in key, so we know that we've
6321 expanded it, and don't try to do so again. */
6322 POSN_BUFFER_POSN (EVENT_START (key))
6323 = Fcons (posn, Qnil);
6325 mock_input = t + 2;
6326 goto replay_sequence;
6328 else if (CONSP (posn))
6330 /* We're looking at the second event of a
6331 sequence which we expanded before. Set
6332 last_real_key_start appropriately. */
6333 if (last_real_key_start == t && t > 0)
6334 last_real_key_start = t - 1;
6339 /* We have finally decided that KEY is something we might want
6340 to look up. */
6341 first_binding = (follow_key (key,
6342 nmaps - first_binding,
6343 submaps + first_binding,
6344 defs + first_binding,
6345 submaps + first_binding)
6346 + first_binding);
6348 /* If KEY wasn't bound, we'll try some fallbacks. */
6349 if (first_binding >= nmaps)
6351 Lisp_Object head;
6353 head = EVENT_HEAD (key);
6354 if (help_char_p (head) && t > 0)
6356 read_key_sequence_cmd = Vprefix_help_command;
6357 keybuf[t++] = key;
6358 last_nonmenu_event = key;
6359 /* The Microsoft C compiler can't handle the goto that
6360 would go here. */
6361 dummyflag = 1;
6362 break;
6365 if (SYMBOLP (head))
6367 Lisp_Object breakdown;
6368 int modifiers;
6370 breakdown = parse_modifiers (head);
6371 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
6372 /* Attempt to reduce an unbound mouse event to a simpler
6373 event that is bound:
6374 Drags reduce to clicks.
6375 Double-clicks reduce to clicks.
6376 Triple-clicks reduce to double-clicks, then to clicks.
6377 Down-clicks are eliminated.
6378 Double-downs reduce to downs, then are eliminated.
6379 Triple-downs reduce to double-downs, then to downs,
6380 then are eliminated. */
6381 if (modifiers & (down_modifier | drag_modifier
6382 | double_modifier | triple_modifier))
6384 while (modifiers & (down_modifier | drag_modifier
6385 | double_modifier | triple_modifier))
6387 Lisp_Object new_head, new_click;
6388 if (modifiers & triple_modifier)
6389 modifiers ^= (double_modifier | triple_modifier);
6390 else if (modifiers & double_modifier)
6391 modifiers &= ~double_modifier;
6392 else if (modifiers & drag_modifier)
6393 modifiers &= ~drag_modifier;
6394 else
6396 /* Dispose of this `down' event by simply jumping
6397 back to replay_key, to get another event.
6399 Note that if this event came from mock input,
6400 then just jumping back to replay_key will just
6401 hand it to us again. So we have to wipe out any
6402 mock input.
6404 We could delete keybuf[t] and shift everything
6405 after that to the left by one spot, but we'd also
6406 have to fix up any variable that points into
6407 keybuf, and shifting isn't really necessary
6408 anyway.
6410 Adding prefixes for non-textual mouse clicks
6411 creates two characters of mock input, and both
6412 must be thrown away. If we're only looking at
6413 the prefix now, we can just jump back to
6414 replay_key. On the other hand, if we've already
6415 processed the prefix, and now the actual click
6416 itself is giving us trouble, then we've lost the
6417 state of the keymaps we want to backtrack to, and
6418 we need to replay the whole sequence to rebuild
6421 Beyond that, only function key expansion could
6422 create more than two keys, but that should never
6423 generate mouse events, so it's okay to zero
6424 mock_input in that case too.
6426 Isn't this just the most wonderful code ever? */
6427 if (t == last_real_key_start)
6429 mock_input = 0;
6430 goto replay_key;
6432 else
6434 mock_input = last_real_key_start;
6435 goto replay_sequence;
6439 new_head
6440 = apply_modifiers (modifiers, XCONS (breakdown)->car);
6441 new_click
6442 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
6444 /* Look for a binding for this new key. follow_key
6445 promises that it didn't munge submaps the
6446 last time we called it, since key was unbound. */
6447 first_binding
6448 = (follow_key (new_click,
6449 nmaps - local_first_binding,
6450 submaps + local_first_binding,
6451 defs + local_first_binding,
6452 submaps + local_first_binding)
6453 + local_first_binding);
6455 /* If that click is bound, go for it. */
6456 if (first_binding < nmaps)
6458 key = new_click;
6459 break;
6461 /* Otherwise, we'll leave key set to the drag event. */
6467 keybuf[t++] = key;
6468 /* Normally, last_nonmenu_event gets the previous key we read.
6469 But when a mouse popup menu is being used,
6470 we don't update last_nonmenu_event; it continues to hold the mouse
6471 event that preceded the first level of menu. */
6472 if (!used_mouse_menu)
6473 last_nonmenu_event = key;
6475 /* Record what part of this_command_keys is the current key sequence. */
6476 this_single_command_key_start = this_command_key_count - t;
6478 prev_fkey_map = fkey_map;
6479 prev_fkey_start = fkey_start;
6480 prev_fkey_end = fkey_end;
6482 prev_keytran_map = keytran_map;
6483 prev_keytran_start = keytran_start;
6484 prev_keytran_end = keytran_end;
6486 /* If the sequence is unbound, see if we can hang a function key
6487 off the end of it. We only want to scan real keyboard input
6488 for function key sequences, so if mock_input says that we're
6489 re-reading old events, don't examine it. */
6490 if (first_binding >= nmaps
6491 && t >= mock_input)
6493 Lisp_Object fkey_next;
6495 /* Continue scan from fkey_end until we find a bound suffix.
6496 If we fail, increment fkey_start
6497 and start fkey_end from there. */
6498 while (fkey_end < t)
6500 Lisp_Object key;
6502 key = keybuf[fkey_end++];
6503 /* Look up meta-characters by prefixing them
6504 with meta_prefix_char. I hate this. */
6505 if (INTEGERP (key) && XINT (key) & meta_modifier)
6507 fkey_next
6508 = get_keymap_1
6509 (get_keyelt
6510 (access_keymap (fkey_map, meta_prefix_char, 1, 0)),
6511 0, 1);
6512 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
6514 else
6515 fkey_next = fkey_map;
6517 fkey_next
6518 = get_keyelt (access_keymap (fkey_next, key, 1, 0));
6520 #if 0 /* I didn't turn this on, because it might cause trouble
6521 for the mapping of return into C-m and tab into C-i. */
6522 /* Optionally don't map function keys into other things.
6523 This enables the user to redefine kp- keys easily. */
6524 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
6525 fkey_next = Qnil;
6526 #endif
6528 /* If the function key map gives a function, not an
6529 array, then call the function with no args and use
6530 its value instead. */
6531 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
6532 && fkey_end == t)
6534 struct gcpro gcpro1, gcpro2, gcpro3;
6535 Lisp_Object tem;
6536 tem = fkey_next;
6538 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
6539 fkey_next = call1 (fkey_next, prompt);
6540 UNGCPRO;
6541 /* If the function returned something invalid,
6542 barf--don't ignore it.
6543 (To ignore it safely, we would need to gcpro a bunch of
6544 other variables.) */
6545 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
6546 error ("Function in key-translation-map returns invalid key sequence");
6549 function_key_possible = ! NILP (fkey_next);
6551 /* If keybuf[fkey_start..fkey_end] is bound in the
6552 function key map and it's a suffix of the current
6553 sequence (i.e. fkey_end == t), replace it with
6554 the binding and restart with fkey_start at the end. */
6555 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
6556 && fkey_end == t)
6558 int len = XFASTINT (Flength (fkey_next));
6560 t = fkey_start + len;
6561 if (t >= bufsize)
6562 error ("Key sequence too long");
6564 if (VECTORP (fkey_next))
6565 bcopy (XVECTOR (fkey_next)->contents,
6566 keybuf + fkey_start,
6567 (t - fkey_start) * sizeof (keybuf[0]));
6568 else if (STRINGP (fkey_next))
6570 int i;
6572 for (i = 0; i < len; i++)
6573 XSETFASTINT (keybuf[fkey_start + i],
6574 XSTRING (fkey_next)->data[i]);
6577 mock_input = t;
6578 fkey_start = fkey_end = t;
6579 fkey_map = Vfunction_key_map;
6581 /* Do pass the results through key-translation-map. */
6582 keytran_start = keytran_end = 0;
6583 keytran_map = Vkey_translation_map;
6585 goto replay_sequence;
6588 fkey_map = get_keymap_1 (fkey_next, 0, 1);
6590 /* If we no longer have a bound suffix, try a new positions for
6591 fkey_start. */
6592 if (NILP (fkey_map))
6594 fkey_end = ++fkey_start;
6595 fkey_map = Vfunction_key_map;
6596 function_key_possible = 0;
6601 /* Look for this sequence in key-translation-map. */
6603 Lisp_Object keytran_next;
6605 /* Scan from keytran_end until we find a bound suffix. */
6606 while (keytran_end < t)
6608 Lisp_Object key;
6610 key = keybuf[keytran_end++];
6611 /* Look up meta-characters by prefixing them
6612 with meta_prefix_char. I hate this. */
6613 if (INTEGERP (key) && XINT (key) & meta_modifier)
6615 keytran_next
6616 = get_keymap_1
6617 (get_keyelt
6618 (access_keymap (keytran_map, meta_prefix_char, 1, 0)),
6619 0, 1);
6620 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
6622 else
6623 keytran_next = keytran_map;
6625 keytran_next
6626 = get_keyelt (access_keymap (keytran_next, key, 1, 0));
6628 /* If the key translation map gives a function, not an
6629 array, then call the function with no args and use
6630 its value instead. */
6631 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
6632 && keytran_end == t)
6634 struct gcpro gcpro1, gcpro2, gcpro3;
6635 Lisp_Object tem;
6636 tem = keytran_next;
6638 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
6639 keytran_next = call1 (keytran_next, prompt);
6640 UNGCPRO;
6641 /* If the function returned something invalid,
6642 barf--don't ignore it.
6643 (To ignore it safely, we would need to gcpro a bunch of
6644 other variables.) */
6645 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
6646 error ("Function in key-translation-map returns invalid key sequence");
6649 key_translation_possible = ! NILP (keytran_next);
6651 /* If keybuf[keytran_start..keytran_end] is bound in the
6652 key translation map and it's a suffix of the current
6653 sequence (i.e. keytran_end == t), replace it with
6654 the binding and restart with keytran_start at the end. */
6655 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
6656 && keytran_end == t)
6658 int len = XFASTINT (Flength (keytran_next));
6660 t = keytran_start + len;
6661 if (t >= bufsize)
6662 error ("Key sequence too long");
6664 if (VECTORP (keytran_next))
6665 bcopy (XVECTOR (keytran_next)->contents,
6666 keybuf + keytran_start,
6667 (t - keytran_start) * sizeof (keybuf[0]));
6668 else if (STRINGP (keytran_next))
6670 int i;
6672 for (i = 0; i < len; i++)
6673 XSETFASTINT (keybuf[keytran_start + i],
6674 XSTRING (keytran_next)->data[i]);
6677 mock_input = t;
6678 keytran_start = keytran_end = t;
6679 keytran_map = Vkey_translation_map;
6681 /* Don't pass the results of key-translation-map
6682 through function-key-map. */
6683 fkey_start = fkey_end = t;
6684 fkey_map = Vkey_translation_map;
6686 goto replay_sequence;
6689 keytran_map = get_keymap_1 (keytran_next, 0, 1);
6691 /* If we no longer have a bound suffix, try a new positions for
6692 keytran_start. */
6693 if (NILP (keytran_map))
6695 keytran_end = ++keytran_start;
6696 keytran_map = Vkey_translation_map;
6697 key_translation_possible = 0;
6702 /* If KEY is not defined in any of the keymaps,
6703 and cannot be part of a function key or translation,
6704 and is an upper case letter
6705 use the corresponding lower-case letter instead. */
6706 if (first_binding == nmaps && ! function_key_possible
6707 && ! key_translation_possible
6708 && INTEGERP (key)
6709 && ((((XINT (key) & 0x3ffff)
6710 < XSTRING (current_buffer->downcase_table)->size)
6711 && UPPERCASEP (XINT (key) & 0x3ffff))
6712 || (XINT (key) & shift_modifier)))
6714 Lisp_Object new_key;
6716 original_uppercase = key;
6717 original_uppercase_position = t - 1;
6719 if (XINT (key) & shift_modifier)
6720 XSETINT (new_key, XINT (key) & ~shift_modifier);
6721 else
6722 XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
6723 | (XINT (key) & ~0x3ffff)));
6725 /* We have to do this unconditionally, regardless of whether
6726 the lower-case char is defined in the keymaps, because they
6727 might get translated through function-key-map. */
6728 keybuf[t - 1] = new_key;
6729 mock_input = t;
6731 fkey_map = prev_fkey_map;
6732 fkey_start = prev_fkey_start;
6733 fkey_end = prev_fkey_end;
6735 keytran_map = prev_keytran_map;
6736 keytran_start = prev_keytran_start;
6737 keytran_end = prev_keytran_end;
6739 goto replay_sequence;
6741 /* If KEY is not defined in any of the keymaps,
6742 and cannot be part of a function key or translation,
6743 and is a shifted function key,
6744 use the corresponding unshifted function key instead. */
6745 if (first_binding == nmaps && ! function_key_possible
6746 && ! key_translation_possible
6747 && SYMBOLP (key))
6749 Lisp_Object breakdown;
6750 int modifiers;
6752 breakdown = parse_modifiers (key);
6753 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
6754 if (modifiers & shift_modifier)
6756 Lisp_Object new_key;
6758 original_uppercase = key;
6759 original_uppercase_position = t - 1;
6761 modifiers &= ~shift_modifier;
6762 new_key = apply_modifiers (modifiers,
6763 XCONS (breakdown)->car);
6765 keybuf[t - 1] = new_key;
6766 mock_input = t;
6768 fkey_map = prev_fkey_map;
6769 fkey_start = prev_fkey_start;
6770 fkey_end = prev_fkey_end;
6772 keytran_map = prev_keytran_map;
6773 keytran_start = prev_keytran_start;
6774 keytran_end = prev_keytran_end;
6776 goto replay_sequence;
6781 if (!dummyflag)
6782 read_key_sequence_cmd = (first_binding < nmaps
6783 ? defs[first_binding]
6784 : Qnil);
6786 unread_switch_frame = delayed_switch_frame;
6787 unbind_to (count, Qnil);
6789 /* Don't downcase the last character if the caller says don't.
6790 Don't downcase it if the result is undefined, either. */
6791 if ((dont_downcase_last || first_binding >= nmaps)
6792 && t - 1 == original_uppercase_position)
6793 keybuf[t - 1] = original_uppercase;
6795 /* Occasionally we fabricate events, perhaps by expanding something
6796 according to function-key-map, or by adding a prefix symbol to a
6797 mouse click in the scroll bar or modeline. In this cases, return
6798 the entire generated key sequence, even if we hit an unbound
6799 prefix or a definition before the end. This means that you will
6800 be able to push back the event properly, and also means that
6801 read-key-sequence will always return a logical unit.
6803 Better ideas? */
6804 for (; t < mock_input; t++)
6806 if (echo_keystrokes)
6807 echo_char (keybuf[t]);
6808 add_command_key (keybuf[t]);
6811 return t;
6814 #if 0 /* This doc string is too long for some compilers.
6815 This commented-out definition serves for DOC. */
6816 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
6817 "Read a sequence of keystrokes and return as a string or vector.\n\
6818 The sequence is sufficient to specify a non-prefix command in the\n\
6819 current local and global maps.\n\
6821 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
6822 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
6823 as a continuation of the previous key.\n\
6825 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
6826 convert the last event to lower case. (Normally any upper case event\n\
6827 is converted to lower case if the original event is undefined and the lower\n\
6828 case equivalent is defined.) A non-nil value is appropriate for reading\n\
6829 a key sequence to be defined.\n\
6831 A C-g typed while in this function is treated like any other character,\n\
6832 and `quit-flag' is not set.\n\
6834 If the key sequence starts with a mouse click, then the sequence is read\n\
6835 using the keymaps of the buffer of the window clicked in, not the buffer\n\
6836 of the selected window as normal.\n\
6837 ""\n\
6838 `read-key-sequence' drops unbound button-down events, since you normally\n\
6839 only care about the click or drag events which follow them. If a drag\n\
6840 or multi-click event is unbound, but the corresponding click event would\n\
6841 be bound, `read-key-sequence' turns the event into a click event at the\n\
6842 drag's starting position. This means that you don't have to distinguish\n\
6843 between click and drag, double, or triple events unless you want to.\n\
6845 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
6846 lines separating windows, and scroll bars with imaginary keys\n\
6847 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
6849 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
6850 function will process a switch-frame event if the user switches frames\n\
6851 before typing anything. If the user switches frames in the middle of a\n\
6852 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
6853 is nil, then the event will be put off until after the current key sequence.\n\
6855 `read-key-sequence' checks `function-key-map' for function key\n\
6856 sequences, where they wouldn't conflict with ordinary bindings. See\n\
6857 `function-key-map' for more details.")
6858 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
6859 #endif
6861 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
6863 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
6864 Lisp_Object prompt, continue_echo, dont_downcase_last;
6865 Lisp_Object can_return_switch_frame;
6867 Lisp_Object keybuf[30];
6868 register int i;
6869 struct gcpro gcpro1, gcpro2;
6871 if (!NILP (prompt))
6872 CHECK_STRING (prompt, 0);
6873 QUIT;
6875 bzero (keybuf, sizeof keybuf);
6876 GCPRO1 (keybuf[0]);
6877 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
6879 if (NILP (continue_echo))
6881 this_command_key_count = 0;
6882 this_single_command_key_start = 0;
6885 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
6886 prompt, ! NILP (dont_downcase_last),
6887 ! NILP (can_return_switch_frame), 0);
6889 if (i == -1)
6891 Vquit_flag = Qt;
6892 QUIT;
6894 UNGCPRO;
6895 return make_event_array (i, keybuf);
6898 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
6899 "Execute CMD as an editor command.\n\
6900 CMD must be a symbol that satisfies the `commandp' predicate.\n\
6901 Optional second arg RECORD-FLAG non-nil\n\
6902 means unconditionally put this command in `command-history'.\n\
6903 Otherwise, that is done only if an arg is read using the minibuffer.\n\
6904 The argument KEYS specifies the value to use instead of (this-command-keys)\n\
6905 when reading the arguments; if it is nil, (this-command-keys) is used.\n\
6906 The argument SPECIAL, if non-nil, means that this command is executing\n\
6907 a special event, so ignore the prefix argument and don't clear it.")
6908 (cmd, record_flag, keys, special)
6909 Lisp_Object cmd, record_flag, keys, special;
6911 register Lisp_Object final;
6912 register Lisp_Object tem;
6913 Lisp_Object prefixarg;
6914 struct backtrace backtrace;
6915 extern int debug_on_next_call;
6917 debug_on_next_call = 0;
6919 if (NILP (special))
6921 prefixarg = current_kboard->Vprefix_arg;
6922 Vcurrent_prefix_arg = prefixarg;
6923 current_kboard->Vprefix_arg = Qnil;
6925 else
6926 prefixarg = Qnil;
6928 if (SYMBOLP (cmd))
6930 tem = Fget (cmd, Qdisabled);
6931 if (!NILP (tem) && !NILP (Vrun_hooks))
6933 tem = Fsymbol_value (Qdisabled_command_hook);
6934 if (!NILP (tem))
6935 return call1 (Vrun_hooks, Qdisabled_command_hook);
6939 while (1)
6941 final = Findirect_function (cmd);
6943 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
6945 struct gcpro gcpro1, gcpro2;
6947 GCPRO2 (cmd, prefixarg);
6948 do_autoload (final, cmd);
6949 UNGCPRO;
6951 else
6952 break;
6955 if (STRINGP (final) || VECTORP (final))
6957 /* If requested, place the macro in the command history. For
6958 other sorts of commands, call-interactively takes care of
6959 this. */
6960 if (!NILP (record_flag))
6961 Vcommand_history
6962 = Fcons (Fcons (Qexecute_kbd_macro,
6963 Fcons (final, Fcons (prefixarg, Qnil))),
6964 Vcommand_history);
6966 return Fexecute_kbd_macro (final, prefixarg);
6968 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
6970 backtrace.next = backtrace_list;
6971 backtrace_list = &backtrace;
6972 backtrace.function = &Qcall_interactively;
6973 backtrace.args = &cmd;
6974 backtrace.nargs = 1;
6975 backtrace.evalargs = 0;
6977 tem = Fcall_interactively (cmd, record_flag, keys);
6979 backtrace_list = backtrace.next;
6980 return tem;
6982 return Qnil;
6985 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
6986 1, 1, "P",
6987 "Read function name, then read its arguments and call it.")
6988 (prefixarg)
6989 Lisp_Object prefixarg;
6991 Lisp_Object function;
6992 char buf[40];
6993 Lisp_Object saved_keys;
6994 Lisp_Object bindings, value;
6995 struct gcpro gcpro1, gcpro2;
6997 saved_keys = Fvector (this_command_key_count,
6998 XVECTOR (this_command_keys)->contents);
6999 buf[0] = 0;
7000 GCPRO2 (saved_keys, prefixarg);
7002 if (EQ (prefixarg, Qminus))
7003 strcpy (buf, "- ");
7004 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
7005 strcpy (buf, "C-u ");
7006 else if (CONSP (prefixarg) && INTEGERP (XCONS (prefixarg)->car))
7008 if (sizeof (int) == sizeof (EMACS_INT))
7009 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
7010 else if (sizeof (long) == sizeof (EMACS_INT))
7011 sprintf (buf, "%ld ", XINT (XCONS (prefixarg)->car));
7012 else
7013 abort ();
7015 else if (INTEGERP (prefixarg))
7017 if (sizeof (int) == sizeof (EMACS_INT))
7018 sprintf (buf, "%d ", XINT (prefixarg));
7019 else if (sizeof (long) == sizeof (EMACS_INT))
7020 sprintf (buf, "%ld ", XINT (prefixarg));
7021 else
7022 abort ();
7025 /* This isn't strictly correct if execute-extended-command
7026 is bound to anything else. Perhaps it should use
7027 this_command_keys? */
7028 strcat (buf, "M-x ");
7030 /* Prompt with buf, and then read a string, completing from and
7031 restricting to the set of all defined commands. Don't provide
7032 any initial input. Save the command read on the extended-command
7033 history list. */
7034 function = Fcompleting_read (build_string (buf),
7035 Vobarray, Qcommandp,
7036 Qt, Qnil, Qextended_command_history, Qnil);
7038 if (STRINGP (function) && XSTRING (function)->size == 0)
7039 error ("No command name given");
7041 /* Set this_command_keys to the concatenation of saved_keys and
7042 function, followed by a RET. */
7044 struct Lisp_String *str;
7045 Lisp_Object *keys;
7046 int i;
7047 Lisp_Object tem;
7049 this_command_key_count = 0;
7050 this_single_command_key_start = 0;
7052 keys = XVECTOR (saved_keys)->contents;
7053 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
7054 add_command_key (keys[i]);
7056 str = XSTRING (function);
7057 for (i = 0; i < str->size; i++)
7059 XSETFASTINT (tem, str->data[i]);
7060 add_command_key (tem);
7063 XSETFASTINT (tem, '\015');
7064 add_command_key (tem);
7067 UNGCPRO;
7069 function = Fintern (function, Qnil);
7070 current_kboard->Vprefix_arg = prefixarg;
7071 this_command = function;
7073 /* If enabled, show which key runs this command. */
7074 if (!NILP (Vsuggest_key_bindings)
7075 && NILP (Vexecuting_macro)
7076 && SYMBOLP (function))
7077 bindings = Fwhere_is_internal (function, Voverriding_local_map,
7078 Qt, Qnil);
7079 else
7080 bindings = Qnil;
7082 value = Qnil;
7083 GCPRO2 (bindings, value);
7084 value = Fcommand_execute (function, Qt, Qnil, Qnil);
7086 /* If the command has a key binding, print it now. */
7087 if (!NILP (bindings))
7089 /* But first wait, and skip the message if there is input. */
7090 if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
7091 ? Vsuggest_key_bindings : make_number (2)),
7092 Qnil, Qnil)))
7094 Lisp_Object binding;
7095 char *newmessage;
7096 char *oldmessage = echo_area_glyphs;
7097 int oldmessage_len = echo_area_glyphs_length;
7099 binding = Fkey_description (bindings);
7101 newmessage
7102 = (char *) alloca (XSYMBOL (function)->name->size
7103 + XSTRING (binding)->size
7104 + 100);
7105 sprintf (newmessage, "You can run the command `%s' by typing %s",
7106 XSYMBOL (function)->name->data,
7107 XSTRING (binding)->data);
7108 message1_nolog (newmessage);
7109 if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
7110 ? Vsuggest_key_bindings : make_number (2)),
7111 Qnil, Qnil)))
7112 message2_nolog (oldmessage, oldmessage_len);
7116 RETURN_UNGCPRO (value);
7119 /* Find the set of keymaps now active.
7120 Store into *MAPS_P a vector holding the various maps
7121 and return the number of them. The vector was malloc'd
7122 and the caller should free it. */
7125 current_active_maps (maps_p)
7126 Lisp_Object **maps_p;
7128 Lisp_Object *tmaps, *maps;
7129 int nmaps;
7131 /* Should overriding-terminal-local-map and overriding-local-map apply? */
7132 if (!NILP (Voverriding_local_map_menu_flag))
7134 /* Yes, use them (if non-nil) as well as the global map. */
7135 maps = (Lisp_Object *) xmalloc (3 * sizeof (maps[0]));
7136 nmaps = 0;
7137 if (!NILP (current_kboard->Voverriding_terminal_local_map))
7138 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
7139 if (!NILP (Voverriding_local_map))
7140 maps[nmaps++] = Voverriding_local_map;
7142 else
7144 /* No, so use major and minor mode keymaps. */
7145 nmaps = current_minor_maps (NULL, &tmaps);
7146 maps = (Lisp_Object *) xmalloc ((nmaps + 2) * sizeof (maps[0]));
7147 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
7148 #ifdef USE_TEXT_PROPERTIES
7149 maps[nmaps++] = get_local_map (PT, current_buffer);
7150 #else
7151 maps[nmaps++] = current_buffer->keymap;
7152 #endif
7154 maps[nmaps++] = current_global_map;
7156 *maps_p = maps;
7157 return nmaps;
7160 /* Return nonzero if input events are pending. */
7162 detect_input_pending ()
7164 if (!input_pending)
7165 get_input_pending (&input_pending, 0);
7167 return input_pending;
7170 /* Return nonzero if input events are pending, and run any pending timers. */
7172 detect_input_pending_run_timers (do_display)
7173 int do_display;
7175 int old_timers_run = timers_run;
7177 if (!input_pending)
7178 get_input_pending (&input_pending, 1);
7180 if (old_timers_run != timers_run && do_display)
7181 redisplay_preserve_echo_area ();
7183 return input_pending;
7186 /* This is called in some cases before a possible quit.
7187 It cases the next call to detect_input_pending to recompute input_pending.
7188 So calling this function unnecessarily can't do any harm. */
7189 clear_input_pending ()
7191 input_pending = 0;
7194 /* Return nonzero if there are pending requeued events.
7195 This isn't used yet. The hope is to make wait_reading_process_input
7196 call it, and return return if it runs Lisp code that unreads something.
7197 The problem is, kbd_buffer_get_event needs to be fixed to know what
7198 to do in that case. It isn't trivial. */
7200 requeued_events_pending_p ()
7202 return (!NILP (Vunread_command_events) || unread_command_char != -1);
7206 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
7207 "T if command input is currently available with no waiting.\n\
7208 Actually, the value is nil only if we can be sure that no input is available.")
7211 if (!NILP (Vunread_command_events) || unread_command_char != -1)
7212 return (Qt);
7214 get_input_pending (&input_pending, 1);
7215 return input_pending > 0 ? Qt : Qnil;
7218 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
7219 "Return vector of last 100 events, not counting those from keyboard macros.")
7222 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
7223 Lisp_Object val;
7225 if (total_keys < NUM_RECENT_KEYS)
7226 return Fvector (total_keys, keys);
7227 else
7229 val = Fvector (NUM_RECENT_KEYS, keys);
7230 bcopy (keys + recent_keys_index,
7231 XVECTOR (val)->contents,
7232 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
7233 bcopy (keys,
7234 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
7235 recent_keys_index * sizeof (Lisp_Object));
7236 return val;
7240 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
7241 "Return the key sequence that invoked this command.\n\
7242 The value is a string or a vector.")
7245 return make_event_array (this_command_key_count,
7246 XVECTOR (this_command_keys)->contents);
7249 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
7250 Sthis_single_command_keys, 0, 0, 0,
7251 "Return the key sequence that invoked this command.\n\
7252 Unlike `this-command-keys', this function's value\n\
7253 does not include prefix arguments.\n\
7254 The value is a string or a vector.")
7257 return make_event_array (this_command_key_count
7258 - this_single_command_key_start,
7259 (XVECTOR (this_command_keys)->contents
7260 + this_single_command_key_start));
7263 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
7264 Sreset_this_command_lengths, 0, 0, 0,
7265 "Used for complicated reasons in `universal-argument-other-key'.\n\
7267 `universal-argument-other-key' rereads the event just typed.\n\
7268 It then gets translated through `function-key-map'.\n\
7269 The translated event gets included in the echo area and in\n\
7270 the value of `this-command-keys' in addition to the raw original event.\n\
7271 That is not right.\n\
7273 Calling this function directs the translated event to replace\n\
7274 the original event, so that only one version of the event actually\n\
7275 appears in the echo area and in the value of `this-command-keys.'.")
7278 before_command_restore_flag = 1;
7279 before_command_key_count_1 = before_command_key_count;
7280 before_command_echo_length_1 = before_command_echo_length;
7283 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
7284 "Return the current depth in recursive edits.")
7287 Lisp_Object temp;
7288 XSETFASTINT (temp, command_loop_level + minibuf_level);
7289 return temp;
7292 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
7293 "FOpen dribble file: ",
7294 "Start writing all keyboard characters to a dribble file called FILE.\n\
7295 If FILE is nil, close any open dribble file.")
7296 (file)
7297 Lisp_Object file;
7299 if (dribble)
7301 fclose (dribble);
7302 dribble = 0;
7304 if (!NILP (file))
7306 file = Fexpand_file_name (file, Qnil);
7307 dribble = fopen (XSTRING (file)->data, "w");
7308 if (dribble == 0)
7309 report_file_error ("Opening dribble", Fcons (file, Qnil));
7311 return Qnil;
7314 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
7315 "Discard the contents of the terminal input buffer.\n\
7316 Also cancel any kbd macro being defined.")
7319 current_kboard->defining_kbd_macro = Qnil;
7320 update_mode_lines++;
7322 Vunread_command_events = Qnil;
7323 unread_command_char = -1;
7325 discard_tty_input ();
7327 /* Without the cast, GCC complains that this assignment loses the
7328 volatile qualifier of kbd_store_ptr. Is there anything wrong
7329 with that? */
7330 kbd_fetch_ptr = (struct input_event *) kbd_store_ptr;
7331 Ffillarray (kbd_buffer_frame_or_window, Qnil);
7332 input_pending = 0;
7334 return Qnil;
7337 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
7338 "Stop Emacs and return to superior process. You can resume later.\n\
7339 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
7340 control, run a subshell instead.\n\n\
7341 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
7342 to be read as terminal input by Emacs's parent, after suspension.\n\
7344 Before suspending, run the normal hook `suspend-hook'.\n\
7345 After resumption run the normal hook `suspend-resume-hook'.\n\
7347 Some operating systems cannot stop the Emacs process and resume it later.\n\
7348 On such systems, Emacs starts a subshell instead of suspending.")
7349 (stuffstring)
7350 Lisp_Object stuffstring;
7352 Lisp_Object tem;
7353 int count = specpdl_ptr - specpdl;
7354 int old_height, old_width;
7355 int width, height;
7356 struct gcpro gcpro1, gcpro2;
7357 extern init_sys_modes ();
7359 if (!NILP (stuffstring))
7360 CHECK_STRING (stuffstring, 0);
7362 /* Run the functions in suspend-hook. */
7363 if (!NILP (Vrun_hooks))
7364 call1 (Vrun_hooks, intern ("suspend-hook"));
7366 GCPRO1 (stuffstring);
7367 get_frame_size (&old_width, &old_height);
7368 reset_sys_modes ();
7369 /* sys_suspend can get an error if it tries to fork a subshell
7370 and the system resources aren't available for that. */
7371 record_unwind_protect (init_sys_modes, 0);
7372 stuff_buffered_input (stuffstring);
7373 if (cannot_suspend)
7374 sys_subshell ();
7375 else
7376 sys_suspend ();
7377 unbind_to (count, Qnil);
7379 /* Check if terminal/window size has changed.
7380 Note that this is not useful when we are running directly
7381 with a window system; but suspend should be disabled in that case. */
7382 get_frame_size (&width, &height);
7383 if (width != old_width || height != old_height)
7384 change_frame_size (selected_frame, height, width, 0, 0);
7386 /* Run suspend-resume-hook. */
7387 if (!NILP (Vrun_hooks))
7388 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
7390 UNGCPRO;
7391 return Qnil;
7394 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
7395 Then in any case stuff anything Emacs has read ahead and not used. */
7397 stuff_buffered_input (stuffstring)
7398 Lisp_Object stuffstring;
7400 /* stuff_char works only in BSD, versions 4.2 and up. */
7401 #ifdef BSD_SYSTEM
7402 #ifndef BSD4_1
7403 register unsigned char *p;
7405 if (STRINGP (stuffstring))
7407 register int count;
7409 p = XSTRING (stuffstring)->data;
7410 count = XSTRING (stuffstring)->size;
7411 while (count-- > 0)
7412 stuff_char (*p++);
7413 stuff_char ('\n');
7415 /* Anything we have read ahead, put back for the shell to read. */
7416 /* ?? What should this do when we have multiple keyboards??
7417 Should we ignore anything that was typed in at the "wrong" kboard? */
7418 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
7420 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
7421 kbd_fetch_ptr = kbd_buffer;
7422 if (kbd_fetch_ptr->kind == ascii_keystroke)
7423 stuff_char (kbd_fetch_ptr->code);
7424 kbd_fetch_ptr->kind = no_event;
7425 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
7426 - kbd_buffer]
7427 = Qnil);
7429 input_pending = 0;
7430 #endif
7431 #endif /* BSD_SYSTEM and not BSD4_1 */
7434 set_waiting_for_input (time_to_clear)
7435 EMACS_TIME *time_to_clear;
7437 input_available_clear_time = time_to_clear;
7439 /* Tell interrupt_signal to throw back to read_char, */
7440 waiting_for_input = 1;
7442 /* If interrupt_signal was called before and buffered a C-g,
7443 make it run again now, to avoid timing error. */
7444 if (!NILP (Vquit_flag))
7445 quit_throw_to_read_char ();
7448 clear_waiting_for_input ()
7450 /* Tell interrupt_signal not to throw back to read_char, */
7451 waiting_for_input = 0;
7452 input_available_clear_time = 0;
7455 /* This routine is called at interrupt level in response to C-G.
7456 If interrupt_input, this is the handler for SIGINT.
7457 Otherwise, it is called from kbd_buffer_store_event,
7458 in handling SIGIO or SIGTINT.
7460 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
7461 immediately throw back to read_char.
7463 Otherwise it sets the Lisp variable quit-flag not-nil.
7464 This causes eval to throw, when it gets a chance.
7465 If quit-flag is already non-nil, it stops the job right away. */
7467 SIGTYPE
7468 interrupt_signal (signalnum) /* If we don't have an argument, */
7469 int signalnum; /* some compilers complain in signal calls. */
7471 char c;
7472 /* Must preserve main program's value of errno. */
7473 int old_errno = errno;
7475 #if defined (USG) && !defined (POSIX_SIGNALS)
7476 if (!read_socket_hook && NILP (Vwindow_system))
7478 /* USG systems forget handlers when they are used;
7479 must reestablish each time */
7480 signal (SIGINT, interrupt_signal);
7481 signal (SIGQUIT, interrupt_signal);
7483 #endif /* USG */
7485 cancel_echoing ();
7487 if (!NILP (Vquit_flag)
7488 && (FRAME_TERMCAP_P (selected_frame) || FRAME_MSDOS_P (selected_frame)))
7490 /* If SIGINT isn't blocked, don't let us be interrupted by
7491 another SIGINT, it might be harmful due to non-reentrancy
7492 in I/O functions. */
7493 sigblock (sigmask (SIGINT));
7495 fflush (stdout);
7496 reset_sys_modes ();
7498 #ifdef SIGTSTP /* Support possible in later USG versions */
7500 * On systems which can suspend the current process and return to the original
7501 * shell, this command causes the user to end up back at the shell.
7502 * The "Auto-save" and "Abort" questions are not asked until
7503 * the user elects to return to emacs, at which point he can save the current
7504 * job and either dump core or continue.
7506 sys_suspend ();
7507 #else
7508 #ifdef VMS
7509 if (sys_suspend () == -1)
7511 printf ("Not running as a subprocess;\n");
7512 printf ("you can continue or abort.\n");
7514 #else /* not VMS */
7515 /* Perhaps should really fork an inferior shell?
7516 But that would not provide any way to get back
7517 to the original shell, ever. */
7518 printf ("No support for stopping a process on this operating system;\n");
7519 printf ("you can continue or abort.\n");
7520 #endif /* not VMS */
7521 #endif /* not SIGTSTP */
7522 #ifdef MSDOS
7523 /* We must remain inside the screen area when the internal terminal
7524 is used. Note that [Enter] is not echoed by dos. */
7525 cursor_to (0, 0);
7526 #endif
7527 /* It doesn't work to autosave while GC is in progress;
7528 the code used for auto-saving doesn't cope with the mark bit. */
7529 if (!gc_in_progress)
7531 printf ("Auto-save? (y or n) ");
7532 fflush (stdout);
7533 if (((c = getchar ()) & ~040) == 'Y')
7535 Fdo_auto_save (Qt, Qnil);
7536 #ifdef MSDOS
7537 printf ("\r\nAuto-save done");
7538 #else /* not MSDOS */
7539 printf ("Auto-save done\n");
7540 #endif /* not MSDOS */
7542 while (c != '\n') c = getchar ();
7544 else
7546 /* During GC, it must be safe to reenable quitting again. */
7547 Vinhibit_quit = Qnil;
7548 #ifdef MSDOS
7549 printf ("\r\n");
7550 #endif /* not MSDOS */
7551 printf ("Garbage collection in progress; cannot auto-save now\r\n");
7552 printf ("but will instead do a real quit after garbage collection ends\r\n");
7553 fflush (stdout);
7556 #ifdef MSDOS
7557 printf ("\r\nAbort? (y or n) ");
7558 #else /* not MSDOS */
7559 #ifdef VMS
7560 printf ("Abort (and enter debugger)? (y or n) ");
7561 #else /* not VMS */
7562 printf ("Abort (and dump core)? (y or n) ");
7563 #endif /* not VMS */
7564 #endif /* not MSDOS */
7565 fflush (stdout);
7566 if (((c = getchar ()) & ~040) == 'Y')
7567 abort ();
7568 while (c != '\n') c = getchar ();
7569 #ifdef MSDOS
7570 printf ("\r\nContinuing...\r\n");
7571 #else /* not MSDOS */
7572 printf ("Continuing...\n");
7573 #endif /* not MSDOS */
7574 fflush (stdout);
7575 init_sys_modes ();
7576 sigfree ();
7578 else
7580 /* If executing a function that wants to be interrupted out of
7581 and the user has not deferred quitting by binding `inhibit-quit'
7582 then quit right away. */
7583 if (immediate_quit && NILP (Vinhibit_quit))
7585 immediate_quit = 0;
7586 sigfree ();
7587 Fsignal (Qquit, Qnil);
7589 else
7590 /* Else request quit when it's safe */
7591 Vquit_flag = Qt;
7594 if (waiting_for_input && !echoing)
7595 quit_throw_to_read_char ();
7597 errno = old_errno;
7600 /* Handle a C-g by making read_char return C-g. */
7602 quit_throw_to_read_char ()
7604 quit_error_check ();
7605 sigfree ();
7606 /* Prevent another signal from doing this before we finish. */
7607 clear_waiting_for_input ();
7608 input_pending = 0;
7610 Vunread_command_events = Qnil;
7611 unread_command_char = -1;
7613 #if 0 /* Currently, sit_for is called from read_char without turning
7614 off polling. And that can call set_waiting_for_input.
7615 It seems to be harmless. */
7616 #ifdef POLL_FOR_INPUT
7617 /* May be > 1 if in recursive minibuffer. */
7618 if (poll_suppress_count == 0)
7619 abort ();
7620 #endif
7621 #endif
7622 if (FRAMEP (internal_last_event_frame)
7623 && XFRAME (internal_last_event_frame) != selected_frame)
7624 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
7625 Qnil, 0);
7627 _longjmp (getcjmp, 1);
7630 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
7631 "Set mode of reading keyboard input.\n\
7632 First arg INTERRUPT non-nil means use input interrupts;\n\
7633 nil means use CBREAK mode.\n\
7634 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
7635 (no effect except in CBREAK mode).\n\
7636 Third arg META t means accept 8-bit input (for a Meta key).\n\
7637 META nil means ignore the top bit, on the assumption it is parity.\n\
7638 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
7639 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
7640 See also `current-input-mode'.")
7641 (interrupt, flow, meta, quit)
7642 Lisp_Object interrupt, flow, meta, quit;
7644 if (!NILP (quit)
7645 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
7646 error ("set-input-mode: QUIT must be an ASCII character");
7648 #ifdef POLL_FOR_INPUT
7649 stop_polling ();
7650 #endif
7652 #ifndef MSDOS
7653 /* this causes startup screen to be restored and messes with the mouse */
7654 reset_sys_modes ();
7655 #endif
7657 #ifdef SIGIO
7658 /* Note SIGIO has been undef'd if FIONREAD is missing. */
7659 if (read_socket_hook)
7661 /* When using X, don't give the user a real choice,
7662 because we haven't implemented the mechanisms to support it. */
7663 #ifdef NO_SOCK_SIGIO
7664 interrupt_input = 0;
7665 #else /* not NO_SOCK_SIGIO */
7666 interrupt_input = 1;
7667 #endif /* NO_SOCK_SIGIO */
7669 else
7670 interrupt_input = !NILP (interrupt);
7671 #else /* not SIGIO */
7672 interrupt_input = 0;
7673 #endif /* not SIGIO */
7675 /* Our VMS input only works by interrupts, as of now. */
7676 #ifdef VMS
7677 interrupt_input = 1;
7678 #endif
7680 flow_control = !NILP (flow);
7681 if (NILP (meta))
7682 meta_key = 0;
7683 else if (EQ (meta, Qt))
7684 meta_key = 1;
7685 else
7686 meta_key = 2;
7687 if (!NILP (quit))
7688 /* Don't let this value be out of range. */
7689 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
7691 #ifndef MSDOS
7692 init_sys_modes ();
7693 #endif
7695 #ifdef POLL_FOR_INPUT
7696 poll_suppress_count = 1;
7697 start_polling ();
7698 #endif
7699 return Qnil;
7702 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
7703 "Return information about the way Emacs currently reads keyboard input.\n\
7704 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
7705 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
7706 nil, Emacs is using CBREAK mode.\n\
7707 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
7708 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
7709 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
7710 META nil means ignoring the top bit, on the assumption it is parity.\n\
7711 META is neither t nor nil if accepting 8-bit input and using\n\
7712 all 8 bits as the character code.\n\
7713 QUIT is the character Emacs currently uses to quit.\n\
7714 The elements of this list correspond to the arguments of\n\
7715 `set-input-mode'.")
7718 Lisp_Object val[4];
7720 val[0] = interrupt_input ? Qt : Qnil;
7721 val[1] = flow_control ? Qt : Qnil;
7722 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
7723 XSETFASTINT (val[3], quit_char);
7725 return Flist (sizeof (val) / sizeof (val[0]), val);
7730 * Set up a new kboard object with reasonable initial values.
7732 void
7733 init_kboard (kb)
7734 KBOARD *kb;
7736 kb->Voverriding_terminal_local_map = Qnil;
7737 kb->Vlast_command = Qnil;
7738 kb->Vprefix_arg = Qnil;
7739 kb->kbd_queue = Qnil;
7740 kb->kbd_queue_has_data = 0;
7741 kb->immediate_echo = 0;
7742 kb->echoptr = kb->echobuf;
7743 kb->echo_after_prompt = -1;
7744 kb->kbd_macro_buffer = 0;
7745 kb->kbd_macro_bufsize = 0;
7746 kb->defining_kbd_macro = Qnil;
7747 kb->Vlast_kbd_macro = Qnil;
7748 kb->reference_count = 0;
7749 kb->Vsystem_key_alist = Qnil;
7750 kb->system_key_syms = Qnil;
7751 kb->Vdefault_minibuffer_frame = Qnil;
7755 * Destroy the contents of a kboard object, but not the object itself.
7756 * We use this just before deleting it, or if we're going to initialize
7757 * it a second time.
7759 static void
7760 wipe_kboard (kb)
7761 KBOARD *kb;
7763 if (kb->kbd_macro_buffer)
7764 xfree (kb->kbd_macro_buffer);
7767 #ifdef MULTI_KBOARD
7768 void
7769 delete_kboard (kb)
7770 KBOARD *kb;
7772 KBOARD **kbp;
7773 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
7774 if (*kbp == NULL)
7775 abort ();
7776 *kbp = kb->next_kboard;
7777 wipe_kboard (kb);
7778 xfree (kb);
7780 #endif
7782 init_keyboard ()
7784 /* This is correct before outermost invocation of the editor loop */
7785 command_loop_level = -1;
7786 immediate_quit = 0;
7787 quit_char = Ctl ('g');
7788 Vunread_command_events = Qnil;
7789 unread_command_char = -1;
7790 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
7791 total_keys = 0;
7792 recent_keys_index = 0;
7793 kbd_fetch_ptr = kbd_buffer;
7794 kbd_store_ptr = kbd_buffer;
7795 kbd_buffer_frame_or_window
7796 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
7797 #ifdef HAVE_MOUSE
7798 do_mouse_tracking = Qnil;
7799 #endif
7800 input_pending = 0;
7802 /* This means that command_loop_1 won't try to select anything the first
7803 time through. */
7804 internal_last_event_frame = Qnil;
7805 Vlast_event_frame = internal_last_event_frame;
7807 #ifdef MULTI_KBOARD
7808 current_kboard = initial_kboard;
7809 #endif
7810 wipe_kboard (current_kboard);
7811 init_kboard (current_kboard);
7813 if (initialized)
7814 Ffillarray (kbd_buffer_frame_or_window, Qnil);
7816 kbd_buffer_frame_or_window
7817 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
7818 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
7820 signal (SIGINT, interrupt_signal);
7821 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
7822 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
7823 SIGQUIT and we can't tell which one it will give us. */
7824 signal (SIGQUIT, interrupt_signal);
7825 #endif /* HAVE_TERMIO */
7827 /* Note SIGIO has been undef'd if FIONREAD is missing. */
7828 #ifdef SIGIO
7829 if (!noninteractive)
7830 signal (SIGIO, input_available_signal);
7831 #endif /* SIGIO */
7833 /* Use interrupt input by default, if it works and noninterrupt input
7834 has deficiencies. */
7836 #ifdef INTERRUPT_INPUT
7837 interrupt_input = 1;
7838 #else
7839 interrupt_input = 0;
7840 #endif
7842 /* Our VMS input only works by interrupts, as of now. */
7843 #ifdef VMS
7844 interrupt_input = 1;
7845 #endif
7847 sigfree ();
7848 dribble = 0;
7850 if (keyboard_init_hook)
7851 (*keyboard_init_hook) ();
7853 #ifdef POLL_FOR_INPUT
7854 poll_suppress_count = 1;
7855 start_polling ();
7856 #endif
7859 /* This type's only use is in syms_of_keyboard, to initialize the
7860 event header symbols and put properties on them. */
7861 struct event_head {
7862 Lisp_Object *var;
7863 char *name;
7864 Lisp_Object *kind;
7867 struct event_head head_table[] = {
7868 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
7869 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
7870 &Qswitch_frame, "switch-frame", &Qswitch_frame,
7871 &Qdelete_frame, "delete-frame", &Qdelete_frame,
7872 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
7873 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
7876 syms_of_keyboard ()
7878 Qtimer_event_handler = intern ("timer-event-handler");
7879 staticpro (&Qtimer_event_handler);
7881 Qdisabled_command_hook = intern ("disabled-command-hook");
7882 staticpro (&Qdisabled_command_hook);
7884 Qself_insert_command = intern ("self-insert-command");
7885 staticpro (&Qself_insert_command);
7887 Qforward_char = intern ("forward-char");
7888 staticpro (&Qforward_char);
7890 Qbackward_char = intern ("backward-char");
7891 staticpro (&Qbackward_char);
7893 Qdisabled = intern ("disabled");
7894 staticpro (&Qdisabled);
7896 Qundefined = intern ("undefined");
7897 staticpro (&Qundefined);
7899 Qpre_command_hook = intern ("pre-command-hook");
7900 staticpro (&Qpre_command_hook);
7902 Qpost_command_hook = intern ("post-command-hook");
7903 staticpro (&Qpost_command_hook);
7905 Qpost_command_idle_hook = intern ("post-command-idle-hook");
7906 staticpro (&Qpost_command_idle_hook);
7908 Qdeferred_action_function = intern ("deferred-action-function");
7909 staticpro (&Qdeferred_action_function);
7911 Qcommand_hook_internal = intern ("command-hook-internal");
7912 staticpro (&Qcommand_hook_internal);
7914 Qfunction_key = intern ("function-key");
7915 staticpro (&Qfunction_key);
7916 Qmouse_click = intern ("mouse-click");
7917 staticpro (&Qmouse_click);
7919 Qmenu_enable = intern ("menu-enable");
7920 staticpro (&Qmenu_enable);
7922 Qmode_line = intern ("mode-line");
7923 staticpro (&Qmode_line);
7924 Qvertical_line = intern ("vertical-line");
7925 staticpro (&Qvertical_line);
7926 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
7927 staticpro (&Qvertical_scroll_bar);
7928 Qmenu_bar = intern ("menu-bar");
7929 staticpro (&Qmenu_bar);
7931 Qabove_handle = intern ("above-handle");
7932 staticpro (&Qabove_handle);
7933 Qhandle = intern ("handle");
7934 staticpro (&Qhandle);
7935 Qbelow_handle = intern ("below-handle");
7936 staticpro (&Qbelow_handle);
7937 Qup = intern ("up");
7938 staticpro (&Qup);
7939 Qdown = intern ("down");
7940 staticpro (&Qdown);
7942 Qevent_kind = intern ("event-kind");
7943 staticpro (&Qevent_kind);
7944 Qevent_symbol_elements = intern ("event-symbol-elements");
7945 staticpro (&Qevent_symbol_elements);
7946 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
7947 staticpro (&Qevent_symbol_element_mask);
7948 Qmodifier_cache = intern ("modifier-cache");
7949 staticpro (&Qmodifier_cache);
7951 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
7952 staticpro (&Qrecompute_lucid_menubar);
7953 Qactivate_menubar_hook = intern ("activate-menubar-hook");
7954 staticpro (&Qactivate_menubar_hook);
7956 Qpolling_period = intern ("polling-period");
7957 staticpro (&Qpolling_period);
7960 struct event_head *p;
7962 for (p = head_table;
7963 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
7964 p++)
7966 *p->var = intern (p->name);
7967 staticpro (p->var);
7968 Fput (*p->var, Qevent_kind, *p->kind);
7969 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
7973 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
7974 staticpro (&button_down_location);
7977 int i;
7978 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
7980 modifier_symbols = Fmake_vector (make_number (len), Qnil);
7981 for (i = 0; i < len; i++)
7982 if (modifier_names[i])
7983 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
7984 staticpro (&modifier_symbols);
7987 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
7988 staticpro (&recent_keys);
7990 this_command_keys = Fmake_vector (make_number (40), Qnil);
7991 staticpro (&this_command_keys);
7993 Qextended_command_history = intern ("extended-command-history");
7994 Fset (Qextended_command_history, Qnil);
7995 staticpro (&Qextended_command_history);
7997 kbd_buffer_frame_or_window
7998 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
7999 staticpro (&kbd_buffer_frame_or_window);
8001 accent_key_syms = Qnil;
8002 staticpro (&accent_key_syms);
8004 func_key_syms = Qnil;
8005 staticpro (&func_key_syms);
8007 mouse_syms = Qnil;
8008 staticpro (&mouse_syms);
8010 unread_switch_frame = Qnil;
8011 staticpro (&unread_switch_frame);
8013 internal_last_event_frame = Qnil;
8014 staticpro (&internal_last_event_frame);
8016 read_key_sequence_cmd = Qnil;
8017 staticpro (&read_key_sequence_cmd);
8019 defsubr (&Sevent_convert_list);
8020 defsubr (&Sread_key_sequence);
8021 defsubr (&Srecursive_edit);
8022 #ifdef HAVE_MOUSE
8023 defsubr (&Strack_mouse);
8024 #endif
8025 defsubr (&Sinput_pending_p);
8026 defsubr (&Scommand_execute);
8027 defsubr (&Srecent_keys);
8028 defsubr (&Sthis_command_keys);
8029 defsubr (&Sthis_single_command_keys);
8030 defsubr (&Sreset_this_command_lengths);
8031 defsubr (&Ssuspend_emacs);
8032 defsubr (&Sabort_recursive_edit);
8033 defsubr (&Sexit_recursive_edit);
8034 defsubr (&Srecursion_depth);
8035 defsubr (&Stop_level);
8036 defsubr (&Sdiscard_input);
8037 defsubr (&Sopen_dribble_file);
8038 defsubr (&Sset_input_mode);
8039 defsubr (&Scurrent_input_mode);
8040 defsubr (&Sexecute_extended_command);
8042 DEFVAR_LISP ("last-command-char", &last_command_char,
8043 "Last input event that was part of a command.");
8045 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
8046 "Last input event that was part of a command.");
8048 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
8049 "Last input event in a command, except for mouse menu events.\n\
8050 Mouse menus give back keys that don't look like mouse events;\n\
8051 this variable holds the actual mouse event that led to the menu,\n\
8052 so that you can determine whether the command was run by mouse or not.");
8054 DEFVAR_LISP ("last-input-char", &last_input_char,
8055 "Last input event.");
8057 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
8058 "Last input event.");
8060 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
8061 "List of objects to be read as next command input events.");
8063 DEFVAR_INT ("unread-command-char", &unread_command_char,
8064 "If not -1, an object to be read as next command input event.");
8066 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
8067 "Meta-prefix character code. Meta-foo as command input\n\
8068 turns into this character followed by foo.");
8069 XSETINT (meta_prefix_char, 033);
8071 DEFVAR_KBOARD ("last-command", Vlast_command,
8072 "The last command executed. Normally a symbol with a function definition,\n\
8073 but can be whatever was found in the keymap, or whatever the variable\n\
8074 `this-command' was set to by that command.\n\
8076 The value `mode-exit' is special; it means that the previous command\n\
8077 read an event that told it to exit, and it did so and unread that event.\n\
8078 In other words, the present command is the event that made the previous\n\
8079 command exit.\n\
8081 The value `kill-region' is special; it means that the previous command\n\
8082 was a kill command.");
8084 DEFVAR_LISP ("this-command", &this_command,
8085 "The command now being executed.\n\
8086 The command can set this variable; whatever is put here\n\
8087 will be in `last-command' during the following command.");
8088 this_command = Qnil;
8090 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
8091 "*Number of keyboard input characters between auto-saves.\n\
8092 Zero means disable autosaving due to number of characters typed.");
8093 auto_save_interval = 300;
8095 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
8096 "*Number of seconds idle time before auto-save.\n\
8097 Zero or nil means disable auto-saving due to idleness.\n\
8098 After auto-saving due to this many seconds of idle time,\n\
8099 Emacs also does a garbage collection if that seems to be warranted.");
8100 XSETFASTINT (Vauto_save_timeout, 30);
8102 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
8103 "*Nonzero means echo unfinished commands after this many seconds of pause.");
8104 echo_keystrokes = 1;
8106 DEFVAR_INT ("polling-period", &polling_period,
8107 "*Interval between polling for input during Lisp execution.\n\
8108 The reason for polling is to make C-g work to stop a running program.\n\
8109 Polling is needed only when using X windows and SIGIO does not work.\n\
8110 Polling is automatically disabled in all other cases.");
8111 polling_period = 2;
8113 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
8114 "*Maximum time between mouse clicks to make a double-click.\n\
8115 Measured in milliseconds. nil means disable double-click recognition;\n\
8116 t means double-clicks have no time limit and are detected\n\
8117 by position only.");
8118 Vdouble_click_time = make_number (500);
8120 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
8121 "*Non-nil means inhibit local map menu bar menus.");
8122 inhibit_local_menu_bar_menus = 0;
8124 DEFVAR_INT ("num-input-keys", &num_input_keys,
8125 "Number of complete key sequences read from the keyboard so far.\n\
8126 This includes key sequences read from keyboard macros.\n\
8127 The number is effectively the number of interactive command invocations.");
8128 num_input_keys = 0;
8130 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
8131 "The frame in which the most recently read event occurred.\n\
8132 If the last event came from a keyboard macro, this is set to `macro'.");
8133 Vlast_event_frame = Qnil;
8135 DEFVAR_LISP ("help-char", &Vhelp_char,
8136 "Character to recognize as meaning Help.\n\
8137 When it is read, do `(eval help-form)', and display result if it's a string.\n\
8138 If the value of `help-form' is nil, this char can be read normally.");
8139 XSETINT (Vhelp_char, Ctl ('H'));
8141 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
8142 "List of input events to recognize as meaning Help.\n\
8143 These work just like the value of `help-char' (see that).");
8144 Vhelp_event_list = Qnil;
8146 DEFVAR_LISP ("help-form", &Vhelp_form,
8147 "Form to execute when character `help-char' is read.\n\
8148 If the form returns a string, that string is displayed.\n\
8149 If `help-form' is nil, the help char is not recognized.");
8150 Vhelp_form = Qnil;
8152 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
8153 "Command to run when `help-char' character follows a prefix key.\n\
8154 This command is used only when there is no actual binding\n\
8155 for that character after that prefix key.");
8156 Vprefix_help_command = Qnil;
8158 DEFVAR_LISP ("top-level", &Vtop_level,
8159 "Form to evaluate when Emacs starts up.\n\
8160 Useful to set before you dump a modified Emacs.");
8161 Vtop_level = Qnil;
8163 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
8164 "Translate table for keyboard input, or nil.\n\
8165 Each character is looked up in this string and the contents used instead.\n\
8166 The value may be a string, a vector, or a char-table.\n\
8167 If it is a string or vector of length N,\n\
8168 character codes N and up are untranslated.\n\
8169 In a vector or a char-table, an element which is nil means \"no translation\".");
8170 Vkeyboard_translate_table = Qnil;
8172 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
8173 "Non-nil means to always spawn a subshell instead of suspending,\n\
8174 even if the operating system has support for stopping a process.");
8175 cannot_suspend = 0;
8177 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
8178 "Non-nil means prompt with menus when appropriate.\n\
8179 This is done when reading from a keymap that has a prompt string,\n\
8180 for elements that have prompt strings.\n\
8181 The menu is displayed on the screen\n\
8182 if X menus were enabled at configuration\n\
8183 time and the previous event was a mouse click prefix key.\n\
8184 Otherwise, menu prompting uses the echo area.");
8185 menu_prompting = 1;
8187 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
8188 "Character to see next line of menu prompt.\n\
8189 Type this character while in a menu prompt to rotate around the lines of it.");
8190 XSETINT (menu_prompt_more_char, ' ');
8192 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
8193 "A mask of additional modifier keys to use with every keyboard character.\n\
8194 Emacs applies the modifiers of the character stored here to each keyboard\n\
8195 character it reads. For example, after evaluating the expression\n\
8196 (setq extra-keyboard-modifiers ?\\C-x)\n\
8197 all input characters will have the control modifier applied to them.\n\
8199 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
8200 not count as a control character; rather, it counts as a character\n\
8201 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
8202 cancels any modification.");
8203 extra_keyboard_modifiers = 0;
8205 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
8206 "If an editing command sets this to t, deactivate the mark afterward.\n\
8207 The command loop sets this to nil before each command,\n\
8208 and tests the value when the command returns.\n\
8209 Buffer modification stores t in this variable.");
8210 Vdeactivate_mark = Qnil;
8212 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
8213 "Temporary storage of pre-command-hook or post-command-hook.");
8214 Vcommand_hook_internal = Qnil;
8216 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
8217 "Normal hook run before each command is executed.\n\
8218 Errors running the hook are caught and ignored.");
8219 Vpre_command_hook = Qnil;
8221 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
8222 "Normal hook run after each command is executed.\n\
8223 Errors running the hook are caught and ignored.");
8224 Vpost_command_hook = Qnil;
8226 DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
8227 "Normal hook run after each command is executed, if idle.\n\
8228 Errors running the hook are caught and ignored.\n\
8229 This feature is obsolete; use idle timers instead. See `etc/NEWS'.");
8230 Vpost_command_idle_hook = Qnil;
8232 DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
8233 "Delay time before running `post-command-idle-hook'.\n\
8234 This is measured in microseconds.");
8235 post_command_idle_delay = 100000;
8237 #if 0
8238 DEFVAR_LISP ("echo-area-clear-hook", ...,
8239 "Normal hook run when clearing the echo area.");
8240 #endif
8241 Qecho_area_clear_hook = intern ("echo-area-clear-hook");
8242 XSYMBOL (Qecho_area_clear_hook)->value = Qnil;
8244 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
8245 "t means menu bar, specified Lucid style, needs to be recomputed.");
8246 Vlucid_menu_bar_dirty_flag = Qnil;
8248 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
8249 "List of menu bar items to move to the end of the menu bar.\n\
8250 The elements of the list are event types that may have menu bar bindings.");
8251 Vmenu_bar_final_items = Qnil;
8253 DEFVAR_KBOARD ("overriding-terminal-local-map",
8254 Voverriding_terminal_local_map,
8255 "Keymap that overrides all other local keymaps.\n\
8256 If this variable is non-nil, it is used as a keymap instead of the\n\
8257 buffer's local map, and the minor mode keymaps and text property keymaps.");
8259 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
8260 "Keymap that overrides all other local keymaps.\n\
8261 If this variable is non-nil, it is used as a keymap instead of the\n\
8262 buffer's local map, and the minor mode keymaps and text property keymaps.");
8263 Voverriding_local_map = Qnil;
8265 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
8266 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
8267 Otherwise, the menu bar continues to reflect the buffer's local map\n\
8268 and the minor mode maps regardless of `overriding-local-map'.");
8269 Voverriding_local_map_menu_flag = Qnil;
8271 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
8272 "Keymap defining bindings for special events to execute at low level.");
8273 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
8275 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
8276 "*Non-nil means generate motion events for mouse motion.");
8278 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
8279 "Alist of system-specific X windows key symbols.\n\
8280 Each element should have the form (N . SYMBOL) where N is the\n\
8281 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
8282 and SYMBOL is its name.");
8284 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
8285 "List of deferred actions to be performed at a later time.\n\
8286 The precise format isn't relevant here; we just check whether it is nil.");
8287 Vdeferred_action_list = Qnil;
8289 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
8290 "Function to call to handle deferred actions, after each command.\n\
8291 This function is called with no arguments after each command\n\
8292 whenever `deferred-action-list' is non-nil.");
8293 Vdeferred_action_function = Qnil;
8295 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
8296 "Non-nil means show the equivalent key-binding when M-x command has one.\n\
8297 The value can be a length of time to show the message for.\n\
8298 If the value is non-nil and not a number, we wait 2 seconds.");
8299 Vsuggest_key_bindings = Qt;
8301 DEFVAR_LISP ("timer-list", &Vtimer_list,
8302 "List of active absolute time timers in order of increasing time");
8303 Vtimer_list = Qnil;
8305 DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
8306 "List of active idle-time timers in order of increasing time");
8307 Vtimer_idle_list = Qnil;
8310 keys_of_keyboard ()
8312 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
8313 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
8314 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
8315 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
8316 initial_define_key (meta_map, 'x', "execute-extended-command");
8318 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
8319 "handle-delete-frame");
8320 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
8321 "ignore-event");
8322 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
8323 "ignore-event");