(display-time): Call display-time-mode.
[emacs.git] / src / keyboard.c
bloba343e1b59b55f9c750717d16ebf86b691e7c645d
1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985,86,87,88,89,93,94,95,96 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Allow config.h to undefine symbols found here. */
22 #include <signal.h>
24 #include <config.h>
25 #include <stdio.h>
26 #include "termchar.h"
27 #include "termopts.h"
28 #include "lisp.h"
29 #include "termhooks.h"
30 #include "macros.h"
31 #include "frame.h"
32 #include "window.h"
33 #include "commands.h"
34 #include "buffer.h"
35 #include "disptab.h"
36 #include "dispextern.h"
37 #include "keyboard.h"
38 #include "intervals.h"
39 #include "blockinput.h"
40 #include <setjmp.h>
41 #include <errno.h>
43 #ifdef MSDOS
44 #include "msdos.h"
45 #include <time.h>
46 #else /* not MSDOS */
47 #ifndef VMS
48 #include <sys/ioctl.h>
49 #endif
50 #endif /* not MSDOS */
52 #include "syssignal.h"
53 #include "systty.h"
55 /* This is to get the definitions of the XK_ symbols. */
56 #ifdef HAVE_X_WINDOWS
57 #include "xterm.h"
58 #endif
60 #ifdef HAVE_NTGUI
61 #include "w32term.h"
62 #endif /* HAVE_NTGUI */
64 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
65 #include "systime.h"
67 extern int errno;
69 /* Variables for blockinput.h: */
71 /* Non-zero if interrupt input is blocked right now. */
72 int interrupt_input_blocked;
74 /* Nonzero means an input interrupt has arrived
75 during the current critical section. */
76 int interrupt_input_pending;
79 /* File descriptor to use for input. */
80 extern int input_fd;
82 #ifdef HAVE_WINDOW_SYSTEM
83 /* Make all keyboard buffers much bigger when using X windows. */
84 #define KBD_BUFFER_SIZE 4096
85 #else /* No X-windows, character input */
86 #define KBD_BUFFER_SIZE 256
87 #endif /* No X-windows */
89 /* Following definition copied from eval.c */
91 struct backtrace
93 struct backtrace *next;
94 Lisp_Object *function;
95 Lisp_Object *args; /* Points to vector of args. */
96 int nargs; /* length of vector. If nargs is UNEVALLED,
97 args points to slot holding list of
98 unevalled args */
99 char evalargs;
102 #ifdef MULTI_KBOARD
103 KBOARD *initial_kboard;
104 KBOARD *current_kboard;
105 KBOARD *all_kboards;
106 int single_kboard;
107 #else
108 KBOARD the_only_kboard;
109 #endif
111 /* Non-nil disable property on a command means
112 do not execute it; call disabled-command-hook's value instead. */
113 Lisp_Object Qdisabled, Qdisabled_command_hook;
115 #define NUM_RECENT_KEYS (100)
116 int recent_keys_index; /* Index for storing next element into recent_keys */
117 int total_keys; /* Total number of elements stored into recent_keys */
118 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
120 /* Vector holding the key sequence that invoked the current command.
121 It is reused for each command, and it may be longer than the current
122 sequence; this_command_key_count indicates how many elements
123 actually mean something.
124 It's easier to staticpro a single Lisp_Object than an array. */
125 Lisp_Object this_command_keys;
126 int this_command_key_count;
128 /* Number of elements of this_command_keys
129 that precede this key sequence. */
130 int this_single_command_key_start;
132 /* Record values of this_command_key_count and echo_length ()
133 before this command was read. */
134 static int before_command_key_count;
135 static int before_command_echo_length;
136 /* Values of before_command_key_count and before_command_echo_length
137 saved by reset-this-command-lengths. */
138 static int before_command_key_count_1;
139 static int before_command_echo_length_1;
140 /* Flag set by reset-this-command-lengths,
141 saying to reset the lengths when add_command_key is called. */
142 static int before_command_restore_flag;
144 extern int minbuf_level;
146 extern struct backtrace *backtrace_list;
148 /* Nonzero means do menu prompting. */
149 static int menu_prompting;
151 /* Character to see next line of menu prompt. */
152 static Lisp_Object menu_prompt_more_char;
154 /* For longjmp to where kbd input is being done. */
155 static jmp_buf getcjmp;
157 /* True while doing kbd input. */
158 int waiting_for_input;
160 /* True while displaying for echoing. Delays C-g throwing. */
161 static int echoing;
163 /* True means we can start echoing at the next input pause
164 even though there is something in the echo area. */
165 static char *ok_to_echo_at_next_pause;
167 /* Nonzero means disregard local maps for the menu bar. */
168 static int inhibit_local_menu_bar_menus;
170 /* Nonzero means C-g should cause immediate error-signal. */
171 int immediate_quit;
173 /* Character to recognize as the help char. */
174 Lisp_Object Vhelp_char;
176 /* List of other event types to recognize as meaning "help". */
177 Lisp_Object Vhelp_event_list;
179 /* Form to execute when help char is typed. */
180 Lisp_Object Vhelp_form;
182 /* Command to run when the help character follows a prefix key. */
183 Lisp_Object Vprefix_help_command;
185 /* List of items that should move to the end of the menu bar. */
186 Lisp_Object Vmenu_bar_final_items;
188 /* Non-nil means show the equivalent key-binding for
189 any M-x command that has one.
190 The value can be a length of time to show the message for.
191 If the value is non-nil and not a number, we wait 2 seconds. */
192 Lisp_Object Vsuggest_key_bindings;
194 /* Character that causes a quit. Normally C-g.
196 If we are running on an ordinary terminal, this must be an ordinary
197 ASCII char, since we want to make it our interrupt character.
199 If we are not running on an ordinary terminal, it still needs to be
200 an ordinary ASCII char. This character needs to be recognized in
201 the input interrupt handler. At this point, the keystroke is
202 represented as a struct input_event, while the desired quit
203 character is specified as a lispy event. The mapping from struct
204 input_events to lispy events cannot run in an interrupt handler,
205 and the reverse mapping is difficult for anything but ASCII
206 keystrokes.
208 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
209 ASCII character. */
210 int quit_char;
212 extern Lisp_Object current_global_map;
213 extern int minibuf_level;
215 /* If non-nil, this is a map that overrides all other local maps. */
216 Lisp_Object Voverriding_local_map;
218 /* If non-nil, Voverriding_local_map applies to the menu bar. */
219 Lisp_Object Voverriding_local_map_menu_flag;
221 /* Keymap that defines special misc events that should
222 be processed immediately at a low level. */
223 Lisp_Object Vspecial_event_map;
225 /* Current depth in recursive edits. */
226 int command_loop_level;
228 /* Total number of times command_loop has read a key sequence. */
229 int num_input_keys;
231 /* Last input character read as a command. */
232 Lisp_Object last_command_char;
234 /* Last input character read as a command, not counting menus
235 reached by the mouse. */
236 Lisp_Object last_nonmenu_event;
238 /* Last input character read for any purpose. */
239 Lisp_Object last_input_char;
241 /* If not Qnil, a list of objects to be read as subsequent command input. */
242 Lisp_Object Vunread_command_events;
244 /* If not -1, an event to be read as subsequent command input. */
245 int unread_command_char;
247 /* If not Qnil, this is a switch-frame event which we decided to put
248 off until the end of a key sequence. This should be read as the
249 next command input, after any unread_command_events.
251 read_key_sequence uses this to delay switch-frame events until the
252 end of the key sequence; Fread_char uses it to put off switch-frame
253 events until a non-ASCII event is acceptable as input. */
254 Lisp_Object unread_switch_frame;
256 /* A mask of extra modifier bits to put into every keyboard char. */
257 int extra_keyboard_modifiers;
259 /* Char to use as prefix when a meta character is typed in.
260 This is bound on entry to minibuffer in case ESC is changed there. */
262 Lisp_Object meta_prefix_char;
264 /* Last size recorded for a current buffer which is not a minibuffer. */
265 static int last_non_minibuf_size;
267 /* Number of idle seconds before an auto-save and garbage collection. */
268 static Lisp_Object Vauto_save_timeout;
270 /* Total number of times read_char has returned. */
271 int num_input_chars;
273 /* Total number of times read_char has returned, outside of macros. */
274 int num_nonmacro_input_chars;
276 /* Auto-save automatically when this many characters have been typed
277 since the last time. */
279 static int auto_save_interval;
281 /* Value of num_nonmacro_input_chars as of last auto save. */
283 int last_auto_save;
285 /* The command being executed by the command loop.
286 Commands may set this, and the value set will be copied into
287 current_kboard->Vlast_command instead of the actual command. */
288 Lisp_Object this_command;
290 /* The value of point when the last command was executed. */
291 int last_point_position;
293 /* The buffer that was current when the last command was started. */
294 Lisp_Object last_point_position_buffer;
296 #ifdef MULTI_FRAME
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;
303 #endif
305 /* A user-visible version of the above, intended to allow users to
306 figure out where the last event came from, if the event doesn't
307 carry that information itself (i.e. if it was a character). */
308 Lisp_Object Vlast_event_frame;
310 /* The timestamp of the last input event we received from the X server.
311 X Windows wants this for selection ownership. */
312 unsigned long last_event_timestamp;
314 Lisp_Object Qself_insert_command;
315 Lisp_Object Qforward_char;
316 Lisp_Object Qbackward_char;
317 Lisp_Object Qundefined;
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 /* Hooks to run before and after each command. */
345 Lisp_Object Qpre_command_hook, Vpre_command_hook;
346 Lisp_Object Qpost_command_hook, Vpost_command_hook;
347 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
348 /* Hook run after a command if there's no more input soon. */
349 Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook;
351 /* Delay time in microseconds before running post-command-idle-hook. */
352 int post_command_idle_delay;
354 /* List of deferred actions to be performed at a later time.
355 The precise format isn't relevant here; we just check whether it is nil. */
356 Lisp_Object Vdeferred_action_list;
358 /* Function to call to handle deferred actions, when there are any. */
359 Lisp_Object Vdeferred_action_function;
360 Lisp_Object Qdeferred_action_function;
362 /* File in which we write all commands we read. */
363 FILE *dribble;
365 /* Nonzero if input is available. */
366 int input_pending;
368 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
369 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
371 int meta_key;
373 extern char *pending_malloc_warning;
375 /* Circular buffer for pre-read keyboard input. */
376 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
378 /* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
380 The interrupt-level event handlers will never enqueue an event on a
381 frame which is not in Vframe_list, and once an event is dequeued,
382 internal_last_event_frame or the event itself points to the frame.
383 So that's all fine.
385 But while the event is sitting in the queue, it's completely
386 unprotected. Suppose the user types one command which will run for
387 a while and then delete a frame, and then types another event at
388 the frame that will be deleted, before the command gets around to
389 it. Suppose there are no references to this frame elsewhere in
390 Emacs, and a GC occurs before the second event is dequeued. Now we
391 have an event referring to a freed frame, which will crash Emacs
392 when it is dequeued.
394 Similar things happen when an event on a scroll bar is enqueued; the
395 window may be deleted while the event is in the queue.
397 So, we use this vector to protect the frame_or_window field in the
398 event queue. That way, they'll be dequeued as dead frames or
399 windows, but still valid lisp objects.
401 If kbd_buffer[i].kind != no_event, then
402 (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
403 == kbd_buffer[i].frame_or_window. */
404 static Lisp_Object kbd_buffer_frame_or_window;
406 /* Pointer to next available character in kbd_buffer.
407 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
408 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
409 next available char is in kbd_buffer[0]. */
410 static struct input_event *kbd_fetch_ptr;
412 /* Pointer to next place to store character in kbd_buffer. This
413 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
414 character should go in kbd_buffer[0]. */
415 static volatile struct input_event *kbd_store_ptr;
417 /* The above pair of variables forms a "queue empty" flag. When we
418 enqueue a non-hook event, we increment kbd_store_ptr. When we
419 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
420 there is input available iff the two pointers are not equal.
422 Why not just have a flag set and cleared by the enqueuing and
423 dequeuing functions? Such a flag could be screwed up by interrupts
424 at inopportune times. */
426 /* If this flag is non-nil, we check mouse_moved to see when the
427 mouse moves, and motion events will appear in the input stream.
428 Otherwise, mouse motion is ignored. */
429 static Lisp_Object do_mouse_tracking;
431 /* Symbols to head events. */
432 Lisp_Object Qmouse_movement;
433 Lisp_Object Qscroll_bar_movement;
434 Lisp_Object Qswitch_frame;
435 Lisp_Object Qdelete_frame;
436 Lisp_Object Qiconify_frame;
437 Lisp_Object Qmake_frame_visible;
439 /* Symbols to denote kinds of events. */
440 Lisp_Object Qfunction_key;
441 Lisp_Object Qmouse_click;
442 Lisp_Object Qtimer_event;
443 /* Lisp_Object Qmouse_movement; - also an event header */
445 /* Properties of event headers. */
446 Lisp_Object Qevent_kind;
447 Lisp_Object Qevent_symbol_elements;
449 Lisp_Object Qmenu_enable;
451 /* An event header symbol HEAD may have a property named
452 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
453 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
454 mask of modifiers applied to it. If present, this is used to help
455 speed up parse_modifiers. */
456 Lisp_Object Qevent_symbol_element_mask;
458 /* An unmodified event header BASE may have a property named
459 Qmodifier_cache, which is an alist mapping modifier masks onto
460 modified versions of BASE. If present, this helps speed up
461 apply_modifiers. */
462 Lisp_Object Qmodifier_cache;
464 /* Symbols to use for parts of windows. */
465 Lisp_Object Qmode_line;
466 Lisp_Object Qvertical_line;
467 Lisp_Object Qvertical_scroll_bar;
468 Lisp_Object Qmenu_bar;
470 extern Lisp_Object Qmenu_enable;
472 Lisp_Object recursive_edit_unwind (), command_loop ();
473 Lisp_Object Fthis_command_keys ();
474 Lisp_Object Qextended_command_history;
475 EMACS_TIME timer_check ();
477 extern char *x_get_keysym_name ();
479 Lisp_Object Qpolling_period;
481 /* List of absolute timers. Appears in order of next scheduled event. */
482 Lisp_Object Vtimer_list;
484 /* List of idle time timers. Appears in order of next scheduled event. */
485 Lisp_Object Vtimer_idle_list;
487 /* Incremented whenever a timer is run. */
488 int timers_run;
490 extern Lisp_Object Vprint_level, Vprint_length;
492 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
493 happens. */
494 EMACS_TIME *input_available_clear_time;
496 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
497 Default is 1 if INTERRUPT_INPUT is defined. */
498 int interrupt_input;
500 /* Nonzero while interrupts are temporarily deferred during redisplay. */
501 int interrupts_deferred;
503 /* Nonzero means use ^S/^Q for flow control. */
504 int flow_control;
506 /* Allow m- file to inhibit use of FIONREAD. */
507 #ifdef BROKEN_FIONREAD
508 #undef FIONREAD
509 #endif
511 /* We are unable to use interrupts if FIONREAD is not available,
512 so flush SIGIO so we won't try. */
513 #ifndef FIONREAD
514 #ifdef SIGIO
515 #undef SIGIO
516 #endif
517 #endif
519 /* If we support a window system, turn on the code to poll periodically
520 to detect C-g. It isn't actually used when doing interrupt input. */
521 #ifdef HAVE_WINDOW_SYSTEM
522 #define POLL_FOR_INPUT
523 #endif
525 /* Global variable declarations. */
527 /* Function for init_keyboard to call with no args (if nonzero). */
528 void (*keyboard_init_hook) ();
530 static int read_avail_input ();
531 static void get_input_pending ();
532 static int readable_events ();
533 static Lisp_Object read_char_x_menu_prompt ();
534 static Lisp_Object read_char_minibuf_menu_prompt ();
535 static Lisp_Object make_lispy_event ();
536 #ifdef HAVE_MOUSE
537 static Lisp_Object make_lispy_movement ();
538 #endif
539 static Lisp_Object modify_event_symbol ();
540 static Lisp_Object make_lispy_switch_frame ();
541 static int parse_solitary_modifier ();
543 /* > 0 if we are to echo keystrokes. */
544 static int echo_keystrokes;
546 /* Nonzero means don't try to suspend even if the operating system seems
547 to support it. */
548 static int cannot_suspend;
550 #define min(a,b) ((a)<(b)?(a):(b))
551 #define max(a,b) ((a)>(b)?(a):(b))
553 /* Install the string STR as the beginning of the string of echoing,
554 so that it serves as a prompt for the next character.
555 Also start echoing. */
557 echo_prompt (str)
558 char *str;
560 int len = strlen (str);
562 if (len > ECHOBUFSIZE - 4)
563 len = ECHOBUFSIZE - 4;
564 bcopy (str, current_kboard->echobuf, len);
565 current_kboard->echoptr = current_kboard->echobuf + len;
566 *current_kboard->echoptr = '\0';
568 current_kboard->echo_after_prompt = len;
570 echo_now ();
573 /* Add C to the echo string, if echoing is going on.
574 C can be a character, which is printed prettily ("M-C-x" and all that
575 jazz), or a symbol, whose name is printed. */
577 echo_char (c)
578 Lisp_Object c;
580 extern char *push_key_description ();
582 if (current_kboard->immediate_echo)
584 char *ptr = current_kboard->echoptr;
586 if (ptr != current_kboard->echobuf)
587 *ptr++ = ' ';
589 /* If someone has passed us a composite event, use its head symbol. */
590 c = EVENT_HEAD (c);
592 if (INTEGERP (c))
594 if (ptr - current_kboard->echobuf > ECHOBUFSIZE - 6)
595 return;
597 ptr = push_key_description (XINT (c), ptr);
599 else if (SYMBOLP (c))
601 struct Lisp_String *name = XSYMBOL (c)->name;
602 if ((ptr - current_kboard->echobuf) + name->size + 4 > ECHOBUFSIZE)
603 return;
604 bcopy (name->data, ptr, name->size);
605 ptr += name->size;
608 if (current_kboard->echoptr == current_kboard->echobuf
609 && help_char_p (c))
611 strcpy (ptr, " (Type ? for further options)");
612 ptr += strlen (ptr);
615 *ptr = 0;
616 current_kboard->echoptr = ptr;
618 echo_now ();
622 /* Temporarily add a dash to the end of the echo string if it's not
623 empty, so that it serves as a mini-prompt for the very next character. */
625 echo_dash ()
627 if (!current_kboard->immediate_echo
628 && current_kboard->echoptr == current_kboard->echobuf)
629 return;
630 /* Do nothing if we just printed a prompt. */
631 if (current_kboard->echo_after_prompt
632 == current_kboard->echoptr - current_kboard->echobuf)
633 return;
634 /* Do nothing if not echoing at all. */
635 if (current_kboard->echoptr == 0)
636 return;
638 /* Put a dash at the end of the buffer temporarily,
639 but make it go away when the next character is added. */
640 current_kboard->echoptr[0] = '-';
641 current_kboard->echoptr[1] = 0;
643 echo_now ();
646 /* Display the current echo string, and begin echoing if not already
647 doing so. */
649 echo_now ()
651 if (!current_kboard->immediate_echo)
653 int i;
654 current_kboard->immediate_echo = 1;
656 for (i = 0; i < this_command_key_count; i++)
658 Lisp_Object c;
659 c = XVECTOR (this_command_keys)->contents[i];
660 if (! (EVENT_HAS_PARAMETERS (c)
661 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
662 echo_char (c);
664 echo_dash ();
667 echoing = 1;
668 message1_nolog (current_kboard->echobuf);
669 echoing = 0;
671 if (waiting_for_input && !NILP (Vquit_flag))
672 quit_throw_to_read_char ();
675 /* Turn off echoing, for the start of a new command. */
677 cancel_echoing ()
679 current_kboard->immediate_echo = 0;
680 current_kboard->echoptr = current_kboard->echobuf;
681 current_kboard->echo_after_prompt = -1;
682 ok_to_echo_at_next_pause = 0;
685 /* Return the length of the current echo string. */
687 static int
688 echo_length ()
690 return current_kboard->echoptr - current_kboard->echobuf;
693 /* Truncate the current echo message to its first LEN chars.
694 This and echo_char get used by read_key_sequence when the user
695 switches frames while entering a key sequence. */
697 static void
698 echo_truncate (len)
699 int len;
701 current_kboard->echobuf[len] = '\0';
702 current_kboard->echoptr = current_kboard->echobuf + len;
703 truncate_echo_area (len);
707 /* Functions for manipulating this_command_keys. */
708 static void
709 add_command_key (key)
710 Lisp_Object key;
712 int size = XVECTOR (this_command_keys)->size;
714 /* If reset-this-command-length was called recently, obey it now.
715 See the doc string of that function for an explanation of why. */
716 if (before_command_restore_flag)
718 this_command_key_count = before_command_key_count_1;
719 if (this_command_key_count < this_single_command_key_start)
720 this_single_command_key_start = this_command_key_count;
721 echo_truncate (before_command_echo_length_1);
722 before_command_restore_flag = 0;
725 if (this_command_key_count >= size)
727 Lisp_Object new_keys;
729 new_keys = Fmake_vector (make_number (size * 2), Qnil);
730 bcopy (XVECTOR (this_command_keys)->contents,
731 XVECTOR (new_keys)->contents,
732 size * sizeof (Lisp_Object));
734 this_command_keys = new_keys;
737 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
740 Lisp_Object
741 recursive_edit_1 ()
743 int count = specpdl_ptr - specpdl;
744 Lisp_Object val;
746 if (command_loop_level > 0)
748 specbind (Qstandard_output, Qt);
749 specbind (Qstandard_input, Qt);
752 val = command_loop ();
753 if (EQ (val, Qt))
754 Fsignal (Qquit, Qnil);
756 return unbind_to (count, Qnil);
759 /* When an auto-save happens, record the "time", and don't do again soon. */
761 record_auto_save ()
763 last_auto_save = num_nonmacro_input_chars;
766 /* Make an auto save happen as soon as possible at command level. */
768 force_auto_save_soon ()
770 last_auto_save = - auto_save_interval - 1;
772 record_asynch_buffer_change ();
775 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
776 "Invoke the editor command loop recursively.\n\
777 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
778 that tells this function to return.\n\
779 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
780 This function is called by the editor initialization to begin editing.")
783 int count = specpdl_ptr - specpdl;
784 Lisp_Object val;
786 command_loop_level++;
787 update_mode_lines = 1;
789 record_unwind_protect (recursive_edit_unwind,
790 (command_loop_level
791 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
792 ? Fcurrent_buffer ()
793 : Qnil);
794 recursive_edit_1 ();
795 return unbind_to (count, Qnil);
798 Lisp_Object
799 recursive_edit_unwind (buffer)
800 Lisp_Object buffer;
802 if (!NILP (buffer))
803 Fset_buffer (buffer);
805 command_loop_level--;
806 update_mode_lines = 1;
807 return Qnil;
810 static void
811 any_kboard_state ()
813 #ifdef MULTI_KBOARD
814 #if 0 /* Theory: if there's anything in Vunread_command_events,
815 it will right away be read by read_key_sequence,
816 and then if we do switch KBOARDS, it will go into the side
817 queue then. So we don't need to do anything special here -- rms. */
818 if (CONSP (Vunread_command_events))
820 current_kboard->kbd_queue
821 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
822 current_kboard->kbd_queue_has_data = 1;
824 Vunread_command_events = Qnil;
825 #endif
826 single_kboard = 0;
827 #endif
830 /* Switch to the single-kboard state, making current_kboard
831 the only KBOARD from which further input is accepted. */
833 void
834 single_kboard_state ()
836 #ifdef MULTI_KBOARD
837 single_kboard = 1;
838 #endif
841 /* Maintain a stack of kboards, so other parts of Emacs
842 can switch temporarily to the kboard of a given frame
843 and then revert to the previous status. */
845 struct kboard_stack
847 KBOARD *kboard;
848 struct kboard_stack *next;
851 static struct kboard_stack *kboard_stack;
853 void
854 push_frame_kboard (f)
855 FRAME_PTR f;
857 #ifdef MULTI_KBOARD
858 struct kboard_stack *p
859 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
861 p->next = kboard_stack;
862 p->kboard = current_kboard;
863 kboard_stack = p;
865 current_kboard = FRAME_KBOARD (f);
866 #endif
869 void
870 pop_frame_kboard ()
872 #ifdef MULTI_KBOARD
873 struct kboard_stack *p = kboard_stack;
874 current_kboard = p->kboard;
875 kboard_stack = p->next;
876 xfree (p);
877 #endif
880 /* Handle errors that are not handled at inner levels
881 by printing an error message and returning to the editor command loop. */
883 Lisp_Object
884 cmd_error (data)
885 Lisp_Object data;
887 Lisp_Object old_level, old_length;
889 Vstandard_output = Qt;
890 Vstandard_input = Qt;
891 Vexecuting_macro = Qnil;
892 current_kboard->Vprefix_arg = Qnil;
893 cancel_echoing ();
895 /* Avoid unquittable loop if data contains a circular list. */
896 old_level = Vprint_level;
897 old_length = Vprint_length;
898 XSETFASTINT (Vprint_level, 10);
899 XSETFASTINT (Vprint_length, 10);
900 cmd_error_internal (data, NULL);
901 Vprint_level = old_level;
902 Vprint_length = old_length;
904 Vquit_flag = Qnil;
906 Vinhibit_quit = Qnil;
907 #ifdef MULTI_KBOARD
908 any_kboard_state ();
909 #endif
911 return make_number (0);
914 cmd_error_internal (data, context)
915 Lisp_Object data;
916 char *context;
918 Lisp_Object stream;
920 Vquit_flag = Qnil;
921 Vinhibit_quit = Qt;
922 echo_area_glyphs = 0;
924 /* If the window system or terminal frame hasn't been initialized
925 yet, or we're not interactive, it's best to dump this message out
926 to stderr and exit. */
927 if (! FRAME_MESSAGE_BUF (selected_frame)
928 || noninteractive)
929 stream = Qexternal_debugging_output;
930 else
932 Fdiscard_input ();
933 bitch_at_user ();
934 stream = Qt;
937 if (context != 0)
938 write_string_1 (context, -1, stream);
940 print_error_message (data, stream);
942 /* If the window system or terminal frame hasn't been initialized
943 yet, or we're in -batch mode, this error should cause Emacs to exit. */
944 if (! FRAME_MESSAGE_BUF (selected_frame)
945 || noninteractive)
947 Fterpri (stream);
948 Fkill_emacs (make_number (-1));
952 Lisp_Object command_loop_1 ();
953 Lisp_Object command_loop_2 ();
954 Lisp_Object top_level_1 ();
956 /* Entry to editor-command-loop.
957 This level has the catches for exiting/returning to editor command loop.
958 It returns nil to exit recursive edit, t to abort it. */
960 Lisp_Object
961 command_loop ()
963 if (command_loop_level > 0 || minibuf_level > 0)
965 return internal_catch (Qexit, command_loop_2, Qnil);
967 else
968 while (1)
970 internal_catch (Qtop_level, top_level_1, Qnil);
971 internal_catch (Qtop_level, command_loop_2, Qnil);
973 /* End of file in -batch run causes exit here. */
974 if (noninteractive)
975 Fkill_emacs (Qt);
979 /* Here we catch errors in execution of commands within the
980 editing loop, and reenter the editing loop.
981 When there is an error, cmd_error runs and returns a non-nil
982 value to us. A value of nil means that cmd_loop_1 itself
983 returned due to end of file (or end of kbd macro). */
985 Lisp_Object
986 command_loop_2 ()
988 register Lisp_Object val;
991 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
992 while (!NILP (val));
994 return Qnil;
997 Lisp_Object
998 top_level_2 ()
1000 return Feval (Vtop_level);
1003 Lisp_Object
1004 top_level_1 ()
1006 /* On entry to the outer level, run the startup file */
1007 if (!NILP (Vtop_level))
1008 internal_condition_case (top_level_2, Qerror, cmd_error);
1009 else if (!NILP (Vpurify_flag))
1010 message ("Bare impure Emacs (standard Lisp code not loaded)");
1011 else
1012 message ("Bare Emacs (standard Lisp code not loaded)");
1013 return Qnil;
1016 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1017 "Exit all recursive editing levels.")
1020 Fthrow (Qtop_level, Qnil);
1023 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1024 "Exit from the innermost recursive edit or minibuffer.")
1027 if (command_loop_level > 0 || minibuf_level > 0)
1028 Fthrow (Qexit, Qnil);
1030 error ("No recursive edit is in progress");
1033 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1034 "Abort the command that requested this recursive edit or minibuffer input.")
1037 if (command_loop_level > 0 || minibuf_level > 0)
1038 Fthrow (Qexit, Qt);
1040 error ("No recursive edit is in progress");
1043 /* This is the actual command reading loop,
1044 sans error-handling encapsulation. */
1046 Lisp_Object Fcommand_execute ();
1047 static int read_key_sequence ();
1048 void safe_run_hooks ();
1050 Lisp_Object
1051 command_loop_1 ()
1053 Lisp_Object cmd, tem;
1054 int lose;
1055 int nonundocount;
1056 Lisp_Object keybuf[30];
1057 int i;
1058 int no_redisplay;
1059 int no_direct;
1060 int prev_modiff;
1061 struct buffer *prev_buffer;
1062 #ifdef MULTI_KBOARD
1063 int was_locked = single_kboard;
1064 #endif
1066 current_kboard->Vprefix_arg = Qnil;
1067 Vdeactivate_mark = Qnil;
1068 waiting_for_input = 0;
1069 cancel_echoing ();
1071 nonundocount = 0;
1072 no_redisplay = 0;
1073 this_command_key_count = 0;
1074 this_single_command_key_start = 0;
1076 /* Make sure this hook runs after commands that get errors and
1077 throw to top level. */
1078 /* Note that the value cell will never directly contain nil
1079 if the symbol is a local variable. */
1080 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1081 safe_run_hooks (Qpost_command_hook);
1083 if (!NILP (Vdeferred_action_list))
1084 call0 (Vdeferred_action_function);
1086 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1088 if (NILP (Vunread_command_events)
1089 && NILP (Vexecuting_macro)
1090 && !NILP (sit_for (0, post_command_idle_delay, 0, 1)))
1091 safe_run_hooks (Qpost_command_idle_hook);
1094 /* Do this after running Vpost_command_hook, for consistency. */
1095 current_kboard->Vlast_command = this_command;
1097 while (1)
1099 /* Make sure the current window's buffer is selected. */
1100 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1101 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1103 /* Display any malloc warning that just came out. Use while because
1104 displaying one warning can cause another. */
1106 while (pending_malloc_warning)
1107 display_malloc_warning ();
1109 no_direct = 0;
1111 Vdeactivate_mark = Qnil;
1113 /* If minibuffer on and echo area in use,
1114 wait 2 sec and redraw minibuffer. */
1116 if (minibuf_level && echo_area_glyphs
1117 && EQ (minibuf_window, echo_area_window))
1119 /* Bind inhibit-quit to t so that C-g gets read in
1120 rather than quitting back to the minibuffer. */
1121 int count = specpdl_ptr - specpdl;
1122 specbind (Qinhibit_quit, Qt);
1124 Fsit_for (make_number (2), Qnil, Qnil);
1125 /* Clear the echo area. */
1126 message2 (0);
1128 unbind_to (count, Qnil);
1130 /* If a C-g came in before, treat it as input now. */
1131 if (!NILP (Vquit_flag))
1133 Vquit_flag = Qnil;
1134 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1138 #ifdef C_ALLOCA
1139 alloca (0); /* Cause a garbage collection now */
1140 /* Since we can free the most stuff here. */
1141 #endif /* C_ALLOCA */
1143 #if 0
1144 #ifdef MULTI_FRAME
1145 /* Select the frame that the last event came from. Usually,
1146 switch-frame events will take care of this, but if some lisp
1147 code swallows a switch-frame event, we'll fix things up here.
1148 Is this a good idea? */
1149 if (FRAMEP (internal_last_event_frame)
1150 && XFRAME (internal_last_event_frame) != selected_frame)
1151 Fselect_frame (internal_last_event_frame, Qnil);
1152 #endif
1153 #endif
1154 /* If it has changed current-menubar from previous value,
1155 really recompute the menubar from the value. */
1156 if (! NILP (Vlucid_menu_bar_dirty_flag)
1157 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1158 call0 (Qrecompute_lucid_menubar);
1160 before_command_key_count = this_command_key_count;
1161 before_command_echo_length = echo_length ();
1163 this_command = Qnil;
1165 /* Read next key sequence; i gets its length. */
1166 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1167 Qnil, 0, 1);
1169 ++num_input_keys;
1171 /* Now we have read a key sequence of length I,
1172 or else I is 0 and we found end of file. */
1174 if (i == 0) /* End of file -- happens only in */
1175 return Qnil; /* a kbd macro, at the end. */
1176 /* -1 means read_key_sequence got a menu that was rejected.
1177 Just loop around and read another command. */
1178 if (i == -1)
1180 cancel_echoing ();
1181 this_command_key_count = 0;
1182 this_single_command_key_start = 0;
1183 goto finalize;
1186 last_command_char = keybuf[i - 1];
1188 /* If the previous command tried to force a specific window-start,
1189 forget about that, in case this command moves point far away
1190 from that position. But also throw away beg_unchanged and
1191 end_unchanged information in that case, so that redisplay will
1192 update the whole window properly. */
1193 if (!NILP (XWINDOW (selected_window)->force_start))
1195 XWINDOW (selected_window)->force_start = Qnil;
1196 beg_unchanged = end_unchanged = 0;
1199 cmd = read_key_sequence_cmd;
1200 if (!NILP (Vexecuting_macro))
1202 if (!NILP (Vquit_flag))
1204 Vexecuting_macro = Qt;
1205 QUIT; /* Make some noise. */
1206 /* Will return since macro now empty. */
1210 /* Do redisplay processing after this command except in special
1211 cases identified below that set no_redisplay to 1.
1212 (actually, there's currently no way to prevent the redisplay,
1213 and no_redisplay is ignored.
1214 Perhaps someday we will really implement it.) */
1215 no_redisplay = 0;
1217 prev_buffer = current_buffer;
1218 prev_modiff = MODIFF;
1219 last_point_position = PT;
1220 XSETBUFFER (last_point_position_buffer, prev_buffer);
1222 /* Execute the command. */
1224 this_command = cmd;
1225 /* Note that the value cell will never directly contain nil
1226 if the symbol is a local variable. */
1227 if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
1228 safe_run_hooks (Qpre_command_hook);
1230 if (NILP (this_command))
1232 /* nil means key is undefined. */
1233 bitch_at_user ();
1234 current_kboard->defining_kbd_macro = Qnil;
1235 update_mode_lines = 1;
1236 current_kboard->Vprefix_arg = Qnil;
1238 else
1240 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1242 /* Recognize some common commands in common situations and
1243 do them directly. */
1244 if (EQ (this_command, Qforward_char) && PT < ZV)
1246 struct Lisp_Char_Table *dp
1247 = window_display_table (XWINDOW (selected_window));
1248 lose = FETCH_CHAR (PT);
1249 SET_PT (PT + 1);
1250 if ((dp
1251 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1252 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1253 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1254 && (lose >= 0x20 && lose < 0x7f)))
1255 : (lose >= 0x20 && lose < 0x7f))
1256 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1257 >= MODIFF)
1258 && (XFASTINT (XWINDOW (selected_window)->last_point)
1259 == PT - 1)
1260 && !windows_or_buffers_changed
1261 && EQ (current_buffer->selective_display, Qnil)
1262 && !detect_input_pending ()
1263 && NILP (XWINDOW (selected_window)->column_number_displayed)
1264 && NILP (Vexecuting_macro))
1265 no_redisplay = direct_output_forward_char (1);
1266 goto directly_done;
1268 else if (EQ (this_command, Qbackward_char) && PT > BEGV)
1270 struct Lisp_Char_Table *dp
1271 = window_display_table (XWINDOW (selected_window));
1272 SET_PT (PT - 1);
1273 lose = FETCH_CHAR (PT);
1274 if ((dp
1275 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1276 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1277 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1278 && (lose >= 0x20 && lose < 0x7f)))
1279 : (lose >= 0x20 && lose < 0x7f))
1280 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1281 >= MODIFF)
1282 && (XFASTINT (XWINDOW (selected_window)->last_point)
1283 == PT + 1)
1284 && !windows_or_buffers_changed
1285 && EQ (current_buffer->selective_display, Qnil)
1286 && !detect_input_pending ()
1287 && NILP (XWINDOW (selected_window)->column_number_displayed)
1288 && NILP (Vexecuting_macro))
1289 no_redisplay = direct_output_forward_char (-1);
1290 goto directly_done;
1292 else if (EQ (this_command, Qself_insert_command)
1293 /* Try this optimization only on ascii keystrokes. */
1294 && INTEGERP (last_command_char))
1296 unsigned char c = XINT (last_command_char);
1297 int value;
1299 if (NILP (Vexecuting_macro)
1300 && !EQ (minibuf_window, selected_window))
1302 if (!nonundocount || nonundocount >= 20)
1304 Fundo_boundary ();
1305 nonundocount = 0;
1307 nonundocount++;
1309 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1310 < MODIFF)
1311 || (XFASTINT (XWINDOW (selected_window)->last_point)
1312 != PT)
1313 || MODIFF <= SAVE_MODIFF
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 value = internal_self_insert (c, 0);
1320 if (value)
1321 lose = 1;
1322 if (value == 2)
1323 nonundocount = 0;
1325 if (!lose
1326 && (PT == ZV || FETCH_CHAR (PT) == '\n'))
1328 struct Lisp_Char_Table *dp
1329 = window_display_table (XWINDOW (selected_window));
1330 int lose = c;
1332 if (dp)
1334 Lisp_Object obj;
1336 obj = DISP_CHAR_VECTOR (dp, lose);
1337 if (NILP (obj))
1339 /* Do it only for char codes
1340 that by default display as themselves. */
1341 if (lose >= 0x20 && lose <= 0x7e)
1342 no_redisplay = direct_output_for_insert (lose);
1344 else if (VECTORP (obj)
1345 && XVECTOR (obj)->size == 1
1346 && (obj = XVECTOR (obj)->contents[0],
1347 INTEGERP (obj))
1348 /* Insist face not specified in glyph. */
1349 && (XINT (obj) & ((-1) << 8)) == 0)
1350 no_redisplay
1351 = direct_output_for_insert (XINT (obj));
1353 else
1355 if (lose >= 0x20 && lose <= 0x7e)
1356 no_redisplay = direct_output_for_insert (lose);
1359 goto directly_done;
1363 /* Here for a command that isn't executed directly */
1365 nonundocount = 0;
1366 if (NILP (current_kboard->Vprefix_arg))
1367 Fundo_boundary ();
1368 Fcommand_execute (this_command, Qnil, Qnil, Qnil);
1371 directly_done: ;
1373 /* If there is a prefix argument,
1374 1) We don't want Vlast_command to be ``universal-argument''
1375 (that would be dumb), so don't set Vlast_command,
1376 2) we want to leave echoing on so that the prefix will be
1377 echoed as part of this key sequence, so don't call
1378 cancel_echoing, and
1379 3) we want to leave this_command_key_count non-zero, so that
1380 read_char will realize that it is re-reading a character, and
1381 not echo it a second time.
1383 If the command didn't actually create a prefix arg,
1384 but is merely a frame event that is transparent to prefix args,
1385 then the above doesn't apply. */
1386 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1388 current_kboard->Vlast_command = this_command;
1389 cancel_echoing ();
1390 this_command_key_count = 0;
1391 this_single_command_key_start = 0;
1394 /* Note that the value cell will never directly contain nil
1395 if the symbol is a local variable. */
1396 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1397 safe_run_hooks (Qpost_command_hook);
1399 if (!NILP (Vdeferred_action_list))
1400 safe_run_hooks (Qdeferred_action_function);
1402 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1404 if (NILP (Vunread_command_events)
1405 && NILP (Vexecuting_macro)
1406 && !NILP (sit_for (0, post_command_idle_delay, 0, 1)))
1407 safe_run_hooks (Qpost_command_idle_hook);
1410 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1412 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1414 current_buffer->mark_active = Qnil;
1415 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1417 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1418 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1421 finalize:
1422 /* Install chars successfully executed in kbd macro. */
1424 if (!NILP (current_kboard->defining_kbd_macro)
1425 && NILP (current_kboard->Vprefix_arg))
1426 finalize_kbd_macro_chars ();
1428 #ifdef MULTI_KBOARD
1429 if (!was_locked)
1430 any_kboard_state ();
1431 #endif
1435 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1437 static Lisp_Object
1438 safe_run_hooks_1 (hook)
1439 Lisp_Object hook;
1441 return call1 (Vrun_hooks, Vinhibit_quit);
1444 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1446 static Lisp_Object
1447 safe_run_hooks_error (data)
1448 Lisp_Object data;
1450 Fset (Vinhibit_quit, Qnil);
1453 /* If we get an error while running the hook, cause the hook variable
1454 to be nil. Also inhibit quits, so that C-g won't cause the hook
1455 to mysteriously evaporate. */
1457 void
1458 safe_run_hooks (hook)
1459 Lisp_Object hook;
1461 Lisp_Object value;
1462 int count = specpdl_ptr - specpdl;
1463 specbind (Qinhibit_quit, hook);
1465 internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
1467 unbind_to (count, Qnil);
1470 /* Number of seconds between polling for input. */
1471 int polling_period;
1473 /* Nonzero means polling for input is temporarily suppressed. */
1474 int poll_suppress_count;
1476 /* Nonzero if polling_for_input is actually being used. */
1477 int polling_for_input;
1479 #ifdef POLL_FOR_INPUT
1481 /* Handle an alarm once each second and read pending input
1482 so as to handle a C-g if it comces in. */
1484 SIGTYPE
1485 input_poll_signal (signalnum) /* If we don't have an argument, */
1486 int signalnum; /* some compilers complain in signal calls. */
1488 /* This causes the call to start_polling at the end
1489 to do its job. It also arranges for a quit or error
1490 from within read_avail_input to resume polling. */
1491 poll_suppress_count++;
1492 if (interrupt_input_blocked == 0
1493 && !waiting_for_input)
1494 read_avail_input (0);
1495 /* Turn on the SIGALRM handler and request another alarm. */
1496 start_polling ();
1499 #endif
1501 /* Begin signals to poll for input, if they are appropriate.
1502 This function is called unconditionally from various places. */
1504 start_polling ()
1506 #ifdef POLL_FOR_INPUT
1507 if (read_socket_hook && !interrupt_input)
1509 poll_suppress_count--;
1510 if (poll_suppress_count == 0)
1512 signal (SIGALRM, input_poll_signal);
1513 polling_for_input = 1;
1514 alarm (polling_period);
1517 #endif
1520 /* Nonzero if we are using polling to handle input asynchronously. */
1523 input_polling_used ()
1525 #ifdef POLL_FOR_INPUT
1526 return read_socket_hook && !interrupt_input;
1527 #else
1528 return 0;
1529 #endif
1532 /* Turn off polling. */
1534 stop_polling ()
1536 #ifdef POLL_FOR_INPUT
1537 if (read_socket_hook && !interrupt_input)
1539 if (poll_suppress_count == 0)
1541 polling_for_input = 0;
1542 alarm (0);
1544 poll_suppress_count++;
1546 #endif
1549 /* Set the value of poll_suppress_count to COUNT
1550 and start or stop polling accordingly. */
1552 void
1553 set_poll_suppress_count (count)
1554 int count;
1556 #ifdef POLL_FOR_INPUT
1557 if (count == 0 && poll_suppress_count != 0)
1559 poll_suppress_count = 1;
1560 start_polling ();
1562 else if (count != 0 && poll_suppress_count == 0)
1564 stop_polling ();
1566 poll_suppress_count = count;
1567 #endif
1570 /* Bind polling_period to a value at least N.
1571 But don't decrease it. */
1573 bind_polling_period (n)
1574 int n;
1576 #ifdef POLL_FOR_INPUT
1577 int new = polling_period;
1579 if (n > new)
1580 new = n;
1582 stop_polling ();
1583 specbind (Qpolling_period, make_number (new));
1584 /* Start a new alarm with the new period. */
1585 start_polling ();
1586 #endif
1589 /* Apply the control modifier to CHARACTER. */
1592 make_ctrl_char (c)
1593 int c;
1595 /* Save the upper bits here. */
1596 int upper = c & ~0177;
1598 c &= 0177;
1600 /* Everything in the columns containing the upper-case letters
1601 denotes a control character. */
1602 if (c >= 0100 && c < 0140)
1604 int oc = c;
1605 c &= ~0140;
1606 /* Set the shift modifier for a control char
1607 made from a shifted letter. But only for letters! */
1608 if (oc >= 'A' && oc <= 'Z')
1609 c |= shift_modifier;
1612 /* The lower-case letters denote control characters too. */
1613 else if (c >= 'a' && c <= 'z')
1614 c &= ~0140;
1616 /* Include the bits for control and shift
1617 only if the basic ASCII code can't indicate them. */
1618 else if (c >= ' ')
1619 c |= ctrl_modifier;
1621 /* Replace the high bits. */
1622 c |= (upper & ~ctrl_modifier);
1624 return c;
1629 /* Input of single characters from keyboard */
1631 Lisp_Object print_help ();
1632 static Lisp_Object kbd_buffer_get_event ();
1633 static void record_char ();
1635 #ifdef MULTI_KBOARD
1636 static jmp_buf wrong_kboard_jmpbuf;
1637 #endif
1639 /* read a character from the keyboard; call the redisplay if needed */
1640 /* commandflag 0 means do not do auto-saving, but do do redisplay.
1641 -1 means do not do redisplay, but do do autosaving.
1642 1 means do both. */
1644 /* The arguments MAPS and NMAPS are for menu prompting.
1645 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1647 PREV_EVENT is the previous input event, or nil if we are reading
1648 the first event of a key sequence.
1650 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
1651 if we used a mouse menu to read the input, or zero otherwise. If
1652 USED_MOUSE_MENU is null, we don't dereference it.
1654 Value is t if we showed a menu and the user rejected it. */
1656 Lisp_Object
1657 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
1658 int commandflag;
1659 int nmaps;
1660 Lisp_Object *maps;
1661 Lisp_Object prev_event;
1662 int *used_mouse_menu;
1664 register Lisp_Object c;
1665 int count;
1666 jmp_buf local_getcjmp;
1667 jmp_buf save_jump;
1668 int key_already_recorded = 0;
1669 Lisp_Object tem, save;
1670 Lisp_Object also_record;
1671 also_record = Qnil;
1673 before_command_key_count = this_command_key_count;
1674 before_command_echo_length = echo_length ();
1676 retry:
1678 if (CONSP (Vunread_command_events))
1680 c = XCONS (Vunread_command_events)->car;
1681 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
1683 /* Undo what read_char_x_menu_prompt did when it unread
1684 additional keys returned by Fx_popup_menu. */
1685 if (CONSP (c)
1686 && (SYMBOLP (XCONS (c)->car) || INTEGERP (XCONS (c)->car))
1687 && NILP (XCONS (c)->cdr))
1688 c = XCONS (c)->car;
1690 if (this_command_key_count == 0)
1691 goto reread_first;
1692 else
1693 goto reread;
1696 if (unread_command_char != -1)
1698 XSETINT (c, unread_command_char);
1699 unread_command_char = -1;
1701 if (this_command_key_count == 0)
1702 goto reread_first;
1703 else
1704 goto reread;
1707 /* If there is no function key translated before
1708 reset-this-command-lengths takes effect, forget about it. */
1709 before_command_restore_flag = 0;
1711 if (!NILP (Vexecuting_macro))
1713 #ifdef MULTI_FRAME
1714 /* We set this to Qmacro; since that's not a frame, nobody will
1715 try to switch frames on us, and the selected window will
1716 remain unchanged.
1718 Since this event came from a macro, it would be misleading to
1719 leave internal_last_event_frame set to wherever the last
1720 real event came from. Normally, a switch-frame event selects
1721 internal_last_event_frame after each command is read, but
1722 events read from a macro should never cause a new frame to be
1723 selected. */
1724 Vlast_event_frame = internal_last_event_frame = Qmacro;
1725 #endif
1727 /* Exit the macro if we are at the end.
1728 Also, some things replace the macro with t
1729 to force an early exit. */
1730 if (EQ (Vexecuting_macro, Qt)
1731 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
1733 XSETINT (c, -1);
1734 return c;
1737 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
1738 if (STRINGP (Vexecuting_macro)
1739 && (XINT (c) & 0x80))
1740 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
1742 executing_macro_index++;
1744 goto from_macro;
1747 if (!NILP (unread_switch_frame))
1749 c = unread_switch_frame;
1750 unread_switch_frame = Qnil;
1752 /* This event should make it into this_command_keys, and get echoed
1753 again, so we go to reread_first, rather than reread. */
1754 goto reread_first;
1757 if (commandflag >= 0 && !input_pending
1758 && !detect_input_pending_run_timers (0))
1759 redisplay ();
1761 /* Message turns off echoing unless more keystrokes turn it on again. */
1762 if (echo_area_glyphs && *echo_area_glyphs
1763 && echo_area_glyphs != current_kboard->echobuf
1764 && ok_to_echo_at_next_pause != echo_area_glyphs)
1765 cancel_echoing ();
1766 else
1767 /* If already echoing, continue. */
1768 echo_dash ();
1770 /* Try reading a character via menu prompting in the minibuf.
1771 Try this before the sit-for, because the sit-for
1772 would do the wrong thing if we are supposed to do
1773 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
1774 after a mouse event so don't try a minibuf menu. */
1775 c = Qnil;
1776 if (nmaps > 0 && INTERACTIVE
1777 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
1778 /* Don't bring up a menu if we already have another event. */
1779 && NILP (Vunread_command_events)
1780 && unread_command_char < 0
1781 && !detect_input_pending_run_timers (0))
1783 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
1784 if (! NILP (c))
1786 key_already_recorded = 1;
1787 goto non_reread_1;
1791 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
1792 We will do that below, temporarily for short sections of code,
1793 when appropriate. local_getcjmp must be in effect
1794 around any call to sit_for or kbd_buffer_get_event;
1795 it *must not* be in effect when we call redisplay. */
1797 if (_setjmp (local_getcjmp))
1799 XSETINT (c, quit_char);
1800 #ifdef MULTI_FRAME
1801 XSETFRAME (internal_last_event_frame, selected_frame);
1802 Vlast_event_frame = internal_last_event_frame;
1803 #endif
1804 /* If we report the quit char as an event,
1805 don't do so more than once. */
1806 if (!NILP (Vinhibit_quit))
1807 Vquit_flag = Qnil;
1809 #ifdef MULTI_KBOARD
1811 KBOARD *kb = FRAME_KBOARD (selected_frame);
1812 if (kb != current_kboard)
1814 Lisp_Object *tailp = &kb->kbd_queue;
1815 /* We shouldn't get here if we were in single-kboard mode! */
1816 if (single_kboard)
1817 abort ();
1818 while (CONSP (*tailp))
1819 tailp = &XCONS (*tailp)->cdr;
1820 if (!NILP (*tailp))
1821 abort ();
1822 *tailp = Fcons (c, Qnil);
1823 kb->kbd_queue_has_data = 1;
1824 current_kboard = kb;
1825 longjmp (wrong_kboard_jmpbuf, 1);
1828 #endif
1829 goto non_reread;
1832 timer_start_idle ();
1834 /* If in middle of key sequence and minibuffer not active,
1835 start echoing if enough time elapses. */
1837 if (minibuf_level == 0 && !current_kboard->immediate_echo
1838 && this_command_key_count > 0
1839 && ! noninteractive
1840 && echo_keystrokes > 0
1841 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0
1842 || ok_to_echo_at_next_pause == echo_area_glyphs))
1844 Lisp_Object tem0;
1846 /* After a mouse event, start echoing right away.
1847 This is because we are probably about to display a menu,
1848 and we don't want to delay before doing so. */
1849 if (EVENT_HAS_PARAMETERS (prev_event))
1850 echo_now ();
1851 else
1853 save_getcjmp (save_jump);
1854 restore_getcjmp (local_getcjmp);
1855 tem0 = sit_for (echo_keystrokes, 0, 1, 1);
1856 restore_getcjmp (save_jump);
1857 if (EQ (tem0, Qt))
1858 echo_now ();
1862 /* Maybe auto save due to number of keystrokes. */
1864 if (commandflag != 0
1865 && auto_save_interval > 0
1866 && num_nonmacro_input_chars - last_auto_save > max (auto_save_interval, 20)
1867 && !detect_input_pending_run_timers (0))
1869 Fdo_auto_save (Qnil, Qnil);
1870 /* Hooks can actually change some buffers in auto save. */
1871 redisplay ();
1874 /* Try reading using an X menu.
1875 This is never confused with reading using the minibuf
1876 because the recursive call of read_char in read_char_minibuf_menu_prompt
1877 does not pass on any keymaps. */
1879 if (nmaps > 0 && INTERACTIVE
1880 && !NILP (prev_event)
1881 && EVENT_HAS_PARAMETERS (prev_event)
1882 && !EQ (XCONS (prev_event)->car, Qmenu_bar)
1883 /* Don't bring up a menu if we already have another event. */
1884 && NILP (Vunread_command_events)
1885 && unread_command_char < 0)
1886 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
1888 /* Maybe autosave and/or garbage collect due to idleness. */
1890 if (INTERACTIVE && NILP (c))
1892 int delay_level, buffer_size;
1894 /* Slow down auto saves logarithmically in size of current buffer,
1895 and garbage collect while we're at it. */
1896 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
1897 last_non_minibuf_size = Z - BEG;
1898 buffer_size = (last_non_minibuf_size >> 8) + 1;
1899 delay_level = 0;
1900 while (buffer_size > 64)
1901 delay_level++, buffer_size -= buffer_size >> 2;
1902 if (delay_level < 4) delay_level = 4;
1903 /* delay_level is 4 for files under around 50k, 7 at 100k,
1904 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
1906 /* Auto save if enough time goes by without input. */
1907 if (commandflag != 0
1908 && num_nonmacro_input_chars > last_auto_save
1909 && INTEGERP (Vauto_save_timeout)
1910 && XINT (Vauto_save_timeout) > 0)
1912 Lisp_Object tem0;
1914 save_getcjmp (save_jump);
1915 restore_getcjmp (local_getcjmp);
1916 tem0 = sit_for (delay_level * XFASTINT (Vauto_save_timeout) / 4,
1917 0, 1, 1);
1918 restore_getcjmp (save_jump);
1920 if (EQ (tem0, Qt))
1922 Fdo_auto_save (Qnil, Qnil);
1924 /* If we have auto-saved and there is still no input
1925 available, garbage collect if there has been enough
1926 consing going on to make it worthwhile. */
1927 if (!detect_input_pending_run_timers (0)
1928 && consing_since_gc > gc_cons_threshold / 2)
1929 Fgarbage_collect ();
1931 redisplay ();
1936 /* Read something from current KBOARD's side queue, if possible. */
1938 if (NILP (c))
1940 if (current_kboard->kbd_queue_has_data)
1942 if (!CONSP (current_kboard->kbd_queue))
1943 abort ();
1944 c = XCONS (current_kboard->kbd_queue)->car;
1945 current_kboard->kbd_queue
1946 = XCONS (current_kboard->kbd_queue)->cdr;
1947 if (NILP (current_kboard->kbd_queue))
1948 current_kboard->kbd_queue_has_data = 0;
1949 input_pending = readable_events (0);
1950 #ifdef MULTI_FRAME
1951 if (EVENT_HAS_PARAMETERS (c)
1952 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
1953 internal_last_event_frame = XCONS (XCONS (c)->cdr)->car;
1954 Vlast_event_frame = internal_last_event_frame;
1955 #endif
1959 #ifdef MULTI_KBOARD
1960 /* If current_kboard's side queue is empty check the other kboards.
1961 If one of them has data that we have not yet seen here,
1962 switch to it and process the data waiting for it.
1964 Note: if the events queued up for another kboard
1965 have already been seen here, and therefore are not a complete command,
1966 the kbd_queue_has_data field is 0, so we skip that kboard here.
1967 That's to avoid an infinite loop switching between kboards here. */
1968 if (NILP (c) && !single_kboard)
1970 KBOARD *kb;
1971 for (kb = all_kboards; kb; kb = kb->next_kboard)
1972 if (kb->kbd_queue_has_data)
1974 current_kboard = kb;
1975 longjmp (wrong_kboard_jmpbuf, 1);
1978 #endif
1980 wrong_kboard:
1982 stop_polling ();
1984 /* Finally, we read from the main queue,
1985 and if that gives us something we can't use yet, we put it on the
1986 appropriate side queue and try again. */
1988 if (NILP (c))
1990 KBOARD *kb;
1992 /* Actually read a character, waiting if necessary. */
1993 save_getcjmp (save_jump);
1994 restore_getcjmp (local_getcjmp);
1995 c = kbd_buffer_get_event (&kb, used_mouse_menu);
1996 restore_getcjmp (save_jump);
1998 #ifdef MULTI_KBOARD
1999 if (! NILP (c) && (kb != current_kboard))
2001 Lisp_Object *tailp = &kb->kbd_queue;
2002 while (CONSP (*tailp))
2003 tailp = &XCONS (*tailp)->cdr;
2004 if (!NILP (*tailp))
2005 abort ();
2006 *tailp = Fcons (c, Qnil);
2007 kb->kbd_queue_has_data = 1;
2008 c = Qnil;
2009 if (single_kboard)
2010 goto wrong_kboard;
2011 current_kboard = kb;
2012 longjmp (wrong_kboard_jmpbuf, 1);
2014 #endif
2017 /* Terminate Emacs in batch mode if at eof. */
2018 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
2019 Fkill_emacs (make_number (1));
2021 if (INTEGERP (c))
2023 /* Add in any extra modifiers, where appropriate. */
2024 if ((extra_keyboard_modifiers & CHAR_CTL)
2025 || ((extra_keyboard_modifiers & 0177) < ' '
2026 && (extra_keyboard_modifiers & 0177) != 0))
2027 XSETINT (c, make_ctrl_char (XINT (c)));
2029 /* Transfer any other modifier bits directly from
2030 extra_keyboard_modifiers to c. Ignore the actual character code
2031 in the low 16 bits of extra_keyboard_modifiers. */
2032 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
2035 non_reread:
2037 /* Now that we have read an event, Emacs is not idle--
2038 unless the event was a timer event (not used now). */
2039 if (! (CONSP (c) && EQ (XCONS (c)->car, Qtimer_event)))
2040 timer_stop_idle ();
2042 start_polling ();
2044 if (NILP (c))
2046 if (commandflag >= 0
2047 && !input_pending && !detect_input_pending_run_timers (0))
2048 redisplay ();
2050 goto wrong_kboard;
2053 non_reread_1:
2055 /* Buffer switch events are only for internal wakeups
2056 so don't show them to the user. */
2057 if (BUFFERP (c))
2058 return c;
2060 if (key_already_recorded)
2061 return c;
2063 /* Process special events within read_char
2064 and loop around to read another event. */
2065 save = Vquit_flag;
2066 Vquit_flag = Qnil;
2067 tem = get_keyelt (access_keymap (get_keymap_1 (Vspecial_event_map, 0, 0),
2068 c, 0, 0), 1);
2069 Vquit_flag = save;
2071 if (!NILP (tem))
2073 int was_locked = single_kboard;
2075 last_input_char = c;
2076 Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt);
2078 /* Resume allowing input from any kboard, if that was true before. */
2079 if (!was_locked)
2080 any_kboard_state ();
2082 goto retry;
2085 /* Wipe the echo area. */
2086 echo_area_glyphs = 0;
2088 /* Handle things that only apply to characters. */
2089 if (INTEGERP (c))
2091 /* If kbd_buffer_get_event gave us an EOF, return that. */
2092 if (XINT (c) == -1)
2093 return c;
2095 if (STRINGP (Vkeyboard_translate_table)
2096 && XSTRING (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2097 XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[XFASTINT (c)]);
2098 else if ((VECTORP (Vkeyboard_translate_table)
2099 && XVECTOR (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2100 || (CHAR_TABLE_P (Vkeyboard_translate_table)
2101 && CHAR_TABLE_ORDINARY_SLOTS > (unsigned) XFASTINT (c)))
2103 Lisp_Object d;
2104 d = Faref (Vkeyboard_translate_table, c);
2105 /* nil in keyboard-translate-table means no translation. */
2106 if (!NILP (d))
2107 c = d;
2111 /* If this event is a mouse click in the menu bar,
2112 return just menu-bar for now. Modify the mouse click event
2113 so we won't do this twice, then queue it up. */
2114 if (EVENT_HAS_PARAMETERS (c)
2115 && CONSP (XCONS (c)->cdr)
2116 && CONSP (EVENT_START (c))
2117 && CONSP (XCONS (EVENT_START (c))->cdr))
2119 Lisp_Object posn;
2121 posn = POSN_BUFFER_POSN (EVENT_START (c));
2122 /* Handle menu-bar events:
2123 insert the dummy prefix event `menu-bar'. */
2124 if (EQ (posn, Qmenu_bar))
2126 /* Change menu-bar to (menu-bar) as the event "position". */
2127 POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
2129 also_record = c;
2130 Vunread_command_events = Fcons (c, Vunread_command_events);
2131 c = posn;
2135 record_char (c);
2136 if (! NILP (also_record))
2137 record_char (also_record);
2139 from_macro:
2140 reread_first:
2141 before_command_key_count = this_command_key_count;
2142 before_command_echo_length = echo_length ();
2144 /* Don't echo mouse motion events. */
2145 if (echo_keystrokes
2146 && ! (EVENT_HAS_PARAMETERS (c)
2147 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
2149 echo_char (c);
2150 if (! NILP (also_record))
2151 echo_char (also_record);
2152 /* Once we reread a character, echoing can happen
2153 the next time we pause to read a new one. */
2154 ok_to_echo_at_next_pause = echo_area_glyphs;
2157 /* Record this character as part of the current key. */
2158 add_command_key (c);
2159 if (! NILP (also_record))
2160 add_command_key (also_record);
2162 /* Re-reading in the middle of a command */
2163 reread:
2164 last_input_char = c;
2165 num_input_chars++;
2167 /* Process the help character specially if enabled */
2168 if (!NILP (Vhelp_form) && help_char_p (c))
2170 Lisp_Object tem0;
2171 count = specpdl_ptr - specpdl;
2173 record_unwind_protect (Fset_window_configuration,
2174 Fcurrent_window_configuration (Qnil));
2176 tem0 = Feval (Vhelp_form);
2177 if (STRINGP (tem0))
2178 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
2180 cancel_echoing ();
2182 c = read_char (0, 0, 0, Qnil, 0);
2183 while (BUFFERP (c));
2184 /* Remove the help from the frame */
2185 unbind_to (count, Qnil);
2187 redisplay ();
2188 if (EQ (c, make_number (040)))
2190 cancel_echoing ();
2192 c = read_char (0, 0, 0, Qnil, 0);
2193 while (BUFFERP (c));
2197 return c;
2200 /* Return 1 if should recognize C as "the help character". */
2203 help_char_p (c)
2204 Lisp_Object c;
2206 Lisp_Object tail;
2208 if (EQ (c, Vhelp_char))
2209 return 1;
2210 for (tail = Vhelp_event_list; CONSP (tail); tail = XCONS (tail)->cdr)
2211 if (EQ (c, XCONS (tail)->car))
2212 return 1;
2213 return 0;
2216 /* Record the input event C in various ways. */
2218 static void
2219 record_char (c)
2220 Lisp_Object c;
2222 total_keys++;
2223 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
2224 if (++recent_keys_index >= NUM_RECENT_KEYS)
2225 recent_keys_index = 0;
2227 /* Write c to the dribble file. If c is a lispy event, write
2228 the event's symbol to the dribble file, in <brackets>. Bleaugh.
2229 If you, dear reader, have a better idea, you've got the source. :-) */
2230 if (dribble)
2232 if (INTEGERP (c))
2234 if (XUINT (c) < 0x100)
2235 putc (XINT (c), dribble);
2236 else
2237 fprintf (dribble, " 0x%x", (int) XUINT (c));
2239 else
2241 Lisp_Object dribblee;
2243 /* If it's a structured event, take the event header. */
2244 dribblee = EVENT_HEAD (c);
2246 if (SYMBOLP (dribblee))
2248 putc ('<', dribble);
2249 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
2250 XSYMBOL (dribblee)->name->size,
2251 dribble);
2252 putc ('>', dribble);
2256 fflush (dribble);
2259 store_kbd_macro_char (c);
2261 num_nonmacro_input_chars++;
2264 Lisp_Object
2265 print_help (object)
2266 Lisp_Object object;
2268 struct buffer *old = current_buffer;
2269 Fprinc (object, Qnil);
2270 set_buffer_internal (XBUFFER (Vstandard_output));
2271 call0 (intern ("help-mode"));
2272 set_buffer_internal (old);
2273 return Qnil;
2276 /* Copy out or in the info on where C-g should throw to.
2277 This is used when running Lisp code from within get_char,
2278 in case get_char is called recursively.
2279 See read_process_output. */
2281 save_getcjmp (temp)
2282 jmp_buf temp;
2284 bcopy (getcjmp, temp, sizeof getcjmp);
2287 restore_getcjmp (temp)
2288 jmp_buf temp;
2290 bcopy (temp, getcjmp, sizeof getcjmp);
2293 #ifdef HAVE_MOUSE
2295 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
2296 of this function. */
2298 static Lisp_Object
2299 tracking_off (old_value)
2300 Lisp_Object old_value;
2302 do_mouse_tracking = old_value;
2303 if (NILP (old_value))
2305 /* Redisplay may have been preempted because there was input
2306 available, and it assumes it will be called again after the
2307 input has been processed. If the only input available was
2308 the sort that we have just disabled, then we need to call
2309 redisplay. */
2310 if (!readable_events (1))
2312 redisplay_preserve_echo_area ();
2313 get_input_pending (&input_pending, 1);
2318 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
2319 "Evaluate BODY with mouse movement events enabled.\n\
2320 Within a `track-mouse' form, mouse motion generates input events that\n\
2321 you can read with `read-event'.\n\
2322 Normally, mouse motion is ignored.")
2323 (args)
2324 Lisp_Object args;
2326 int count = specpdl_ptr - specpdl;
2327 Lisp_Object val;
2329 record_unwind_protect (tracking_off, do_mouse_tracking);
2331 do_mouse_tracking = Qt;
2333 val = Fprogn (args);
2334 return unbind_to (count, val);
2337 /* If mouse has moved on some frame, return one of those frames.
2338 Return 0 otherwise. */
2340 static FRAME_PTR
2341 some_mouse_moved ()
2343 Lisp_Object tail, frame;
2345 FOR_EACH_FRAME (tail, frame)
2347 if (XFRAME (frame)->mouse_moved)
2348 return XFRAME (frame);
2351 return 0;
2354 #endif /* HAVE_MOUSE */
2356 /* Low level keyboard/mouse input.
2357 kbd_buffer_store_event places events in kbd_buffer, and
2358 kbd_buffer_get_event retrieves them. */
2360 /* Return true iff there are any events in the queue that read-char
2361 would return. If this returns false, a read-char would block. */
2362 static int
2363 readable_events (do_timers_now)
2364 int do_timers_now;
2366 if (do_timers_now)
2367 timer_check (do_timers_now);
2369 if (kbd_fetch_ptr != kbd_store_ptr)
2370 return 1;
2371 #ifdef HAVE_MOUSE
2372 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2373 return 1;
2374 #endif
2375 if (single_kboard)
2377 if (current_kboard->kbd_queue_has_data)
2378 return 1;
2380 else
2382 KBOARD *kb;
2383 for (kb = all_kboards; kb; kb = kb->next_kboard)
2384 if (kb->kbd_queue_has_data)
2385 return 1;
2387 return 0;
2390 /* Set this for debugging, to have a way to get out */
2391 int stop_character;
2393 #ifdef MULTI_KBOARD
2394 static KBOARD *
2395 event_to_kboard (event)
2396 struct input_event *event;
2398 Lisp_Object frame;
2399 frame = event->frame_or_window;
2400 if (CONSP (frame))
2401 frame = XCONS (frame)->car;
2402 else if (WINDOWP (frame))
2403 frame = WINDOW_FRAME (XWINDOW (frame));
2405 /* There are still some events that don't set this field.
2406 For now, just ignore the problem.
2407 Also ignore dead frames here. */
2408 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
2409 return 0;
2410 else
2411 return FRAME_KBOARD (XFRAME (frame));
2413 #endif
2415 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
2417 void
2418 kbd_buffer_store_event (event)
2419 register struct input_event *event;
2421 if (event->kind == no_event)
2422 abort ();
2424 if (event->kind == ascii_keystroke)
2426 register int c = event->code & 0377;
2428 if (event->modifiers & ctrl_modifier)
2429 c = make_ctrl_char (c);
2431 c |= (event->modifiers
2432 & (meta_modifier | alt_modifier
2433 | hyper_modifier | super_modifier));
2435 if (c == quit_char)
2437 extern SIGTYPE interrupt_signal ();
2438 #ifdef MULTI_KBOARD
2439 KBOARD *kb;
2440 struct input_event *sp;
2442 if (single_kboard
2443 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
2444 kb != current_kboard))
2446 kb->kbd_queue
2447 = Fcons (make_lispy_switch_frame (event->frame_or_window),
2448 Fcons (make_number (c), Qnil));
2449 kb->kbd_queue_has_data = 1;
2450 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
2452 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
2453 sp = kbd_buffer;
2455 if (event_to_kboard (sp) == kb)
2457 sp->kind = no_event;
2458 sp->frame_or_window = Qnil;
2461 return;
2463 #endif
2465 #ifdef MULTI_FRAME
2466 /* If this results in a quit_char being returned to Emacs as
2467 input, set Vlast_event_frame properly. If this doesn't
2468 get returned to Emacs as an event, the next event read
2469 will set Vlast_event_frame again, so this is safe to do. */
2471 Lisp_Object focus;
2473 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
2474 if (NILP (focus))
2475 focus = event->frame_or_window;
2476 internal_last_event_frame = focus;
2477 Vlast_event_frame = focus;
2479 #endif
2481 last_event_timestamp = event->timestamp;
2482 interrupt_signal ();
2483 return;
2486 if (c && c == stop_character)
2488 sys_suspend ();
2489 return;
2492 /* Don't insert two buffer_switch_event's in a row.
2493 Just ignore the second one. */
2494 else if (event->kind == buffer_switch_event
2495 && kbd_fetch_ptr != kbd_store_ptr
2496 && kbd_store_ptr->kind == buffer_switch_event)
2497 return;
2499 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
2500 kbd_store_ptr = kbd_buffer;
2502 /* Don't let the very last slot in the buffer become full,
2503 since that would make the two pointers equal,
2504 and that is indistinguishable from an empty buffer.
2505 Discard the event if it would fill the last slot. */
2506 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
2508 volatile struct input_event *sp = kbd_store_ptr;
2509 sp->kind = event->kind;
2510 if (event->kind == selection_request_event)
2512 /* We must not use the ordinary copying code for this case,
2513 since `part' is an enum and copying it might not copy enough
2514 in this case. */
2515 bcopy (event, (char *) sp, sizeof (*event));
2517 else
2519 sp->code = event->code;
2520 sp->part = event->part;
2521 sp->frame_or_window = event->frame_or_window;
2522 sp->modifiers = event->modifiers;
2523 sp->x = event->x;
2524 sp->y = event->y;
2525 sp->timestamp = event->timestamp;
2527 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
2528 - kbd_buffer]
2529 = event->frame_or_window);
2531 kbd_store_ptr++;
2535 /* Read one event from the event buffer, waiting if necessary.
2536 The value is a Lisp object representing the event.
2537 The value is nil for an event that should be ignored,
2538 or that was handled here.
2539 We always read and discard one event. */
2541 static Lisp_Object
2542 kbd_buffer_get_event (kbp, used_mouse_menu)
2543 KBOARD **kbp;
2544 int *used_mouse_menu;
2546 register int c;
2547 Lisp_Object obj;
2548 EMACS_TIME next_timer_delay;
2550 if (noninteractive)
2552 c = getchar ();
2553 XSETINT (obj, c);
2554 *kbp = current_kboard;
2555 return obj;
2558 /* Wait until there is input available. */
2559 for (;;)
2561 if (kbd_fetch_ptr != kbd_store_ptr)
2562 break;
2563 #ifdef HAVE_MOUSE
2564 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2565 break;
2566 #endif
2568 /* If the quit flag is set, then read_char will return
2569 quit_char, so that counts as "available input." */
2570 if (!NILP (Vquit_flag))
2571 quit_throw_to_read_char ();
2573 /* One way or another, wait until input is available; then, if
2574 interrupt handlers have not read it, read it now. */
2576 #ifdef OLDVMS
2577 wait_for_kbd_input ();
2578 #else
2579 /* Note SIGIO has been undef'd if FIONREAD is missing. */
2580 #ifdef SIGIO
2581 gobble_input (0);
2582 #endif /* SIGIO */
2583 if (kbd_fetch_ptr != kbd_store_ptr)
2584 break;
2585 #ifdef HAVE_MOUSE
2586 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2587 break;
2588 #endif
2590 Lisp_Object minus_one;
2592 XSETINT (minus_one, -1);
2593 wait_reading_process_input (0, 0, minus_one, 1);
2595 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
2596 /* Pass 1 for EXPECT since we just waited to have input. */
2597 read_avail_input (1);
2599 #endif /* not VMS */
2602 /* At this point, we know that there is a readable event available
2603 somewhere. If the event queue is empty, then there must be a
2604 mouse movement enabled and available. */
2605 if (kbd_fetch_ptr != kbd_store_ptr)
2607 struct input_event *event;
2609 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2610 ? kbd_fetch_ptr
2611 : kbd_buffer);
2613 last_event_timestamp = event->timestamp;
2615 #ifdef MULTI_KBOARD
2616 *kbp = event_to_kboard (event);
2617 if (*kbp == 0)
2618 *kbp = current_kboard; /* Better than returning null ptr? */
2619 #else
2620 *kbp = &the_only_kboard;
2621 #endif
2623 obj = Qnil;
2625 /* These two kinds of events get special handling
2626 and don't actually appear to the command loop.
2627 We return nil for them. */
2628 if (event->kind == selection_request_event)
2630 #ifdef HAVE_X11
2631 struct input_event copy;
2633 /* Remove it from the buffer before processing it,
2634 since otherwise swallow_events will see it
2635 and process it again. */
2636 copy = *event;
2637 kbd_fetch_ptr = event + 1;
2638 input_pending = readable_events (0);
2639 x_handle_selection_request (&copy);
2640 #else
2641 /* We're getting selection request events, but we don't have
2642 a window system. */
2643 abort ();
2644 #endif
2647 else if (event->kind == selection_clear_event)
2649 #ifdef HAVE_X11
2650 struct input_event copy;
2652 /* Remove it from the buffer before processing it. */
2653 copy = *event;
2654 kbd_fetch_ptr = event + 1;
2655 input_pending = readable_events (0);
2656 x_handle_selection_clear (&copy);
2657 #else
2658 /* We're getting selection request events, but we don't have
2659 a window system. */
2660 abort ();
2661 #endif
2663 #if defined (HAVE_X11) || defined (HAVE_NTGUI)
2664 else if (event->kind == delete_window_event)
2666 /* Make an event (delete-frame (FRAME)). */
2667 obj = Fcons (event->frame_or_window, Qnil);
2668 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
2669 kbd_fetch_ptr = event + 1;
2671 else if (event->kind == iconify_event)
2673 /* Make an event (iconify-frame (FRAME)). */
2674 obj = Fcons (event->frame_or_window, Qnil);
2675 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
2676 kbd_fetch_ptr = event + 1;
2678 else if (event->kind == deiconify_event)
2680 /* Make an event (make-frame-visible (FRAME)). */
2681 obj = Fcons (event->frame_or_window, Qnil);
2682 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
2683 kbd_fetch_ptr = event + 1;
2685 #endif
2686 else if (event->kind == buffer_switch_event)
2688 /* The value doesn't matter here; only the type is tested. */
2689 XSETBUFFER (obj, current_buffer);
2690 kbd_fetch_ptr = event + 1;
2692 #ifdef USE_X_TOOLKIT
2693 else if (event->kind == menu_bar_activate_event)
2695 kbd_fetch_ptr = event + 1;
2696 input_pending = readable_events (0);
2697 if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
2698 x_activate_menubar (XFRAME (event->frame_or_window));
2700 #endif
2701 /* Just discard these, by returning nil.
2702 With MULTI_KBOARD, these events are used as placeholders
2703 when we need to randomly delete events from the queue.
2704 (They shouldn't otherwise be found in the buffer,
2705 but on some machines it appears they do show up
2706 even without MULTI_KBOARD.) */
2707 else if (event->kind == no_event)
2708 kbd_fetch_ptr = event + 1;
2710 /* If this event is on a different frame, return a switch-frame this
2711 time, and leave the event in the queue for next time. */
2712 else
2714 #ifdef MULTI_FRAME
2715 Lisp_Object frame;
2716 Lisp_Object focus;
2718 frame = event->frame_or_window;
2719 if (CONSP (frame))
2720 frame = XCONS (frame)->car;
2721 else if (WINDOWP (frame))
2722 frame = WINDOW_FRAME (XWINDOW (frame));
2724 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
2725 if (! NILP (focus))
2726 frame = focus;
2728 if (! EQ (frame, internal_last_event_frame)
2729 && XFRAME (frame) != selected_frame)
2730 obj = make_lispy_switch_frame (frame);
2731 internal_last_event_frame = frame;
2732 #endif /* MULTI_FRAME */
2734 /* If we didn't decide to make a switch-frame event, go ahead
2735 and build a real event from the queue entry. */
2737 if (NILP (obj))
2739 obj = make_lispy_event (event);
2740 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
2741 /* If this was a menu selection, then set the flag to inhibit
2742 writing to last_nonmenu_event. Don't do this if the event
2743 we're returning is (menu-bar), though; that indicates the
2744 beginning of the menu sequence, and we might as well leave
2745 that as the `event with parameters' for this selection. */
2746 if (event->kind == menu_bar_event
2747 && !(CONSP (obj) && EQ (XCONS (obj)->car, Qmenu_bar))
2748 && used_mouse_menu)
2749 *used_mouse_menu = 1;
2750 #endif
2752 /* Wipe out this event, to catch bugs. */
2753 event->kind = no_event;
2754 XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer] = Qnil;
2756 kbd_fetch_ptr = event + 1;
2760 #ifdef HAVE_MOUSE
2761 /* Try generating a mouse motion event. */
2762 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2764 FRAME_PTR f = some_mouse_moved ();
2765 Lisp_Object bar_window;
2766 enum scroll_bar_part part;
2767 Lisp_Object x, y;
2768 unsigned long time;
2770 *kbp = current_kboard;
2771 /* Note that this uses F to determine which display to look at.
2772 If there is no valid info, it does not store anything
2773 so x remains nil. */
2774 x = Qnil;
2775 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
2777 obj = Qnil;
2779 #ifdef MULTI_FRAME
2780 /* Decide if we should generate a switch-frame event. Don't
2781 generate switch-frame events for motion outside of all Emacs
2782 frames. */
2783 if (!NILP (x) && f)
2785 Lisp_Object frame;
2787 frame = FRAME_FOCUS_FRAME (f);
2788 if (NILP (frame))
2789 XSETFRAME (frame, f);
2791 if (! EQ (frame, internal_last_event_frame)
2792 && XFRAME (frame) != selected_frame)
2793 obj = make_lispy_switch_frame (frame);
2794 internal_last_event_frame = frame;
2796 #endif
2798 /* If we didn't decide to make a switch-frame event, go ahead and
2799 return a mouse-motion event. */
2800 if (!NILP (x) && NILP (obj))
2801 obj = make_lispy_movement (f, bar_window, part, x, y, time);
2803 #endif /* HAVE_MOUSE */
2804 else
2805 /* We were promised by the above while loop that there was
2806 something for us to read! */
2807 abort ();
2809 input_pending = readable_events (0);
2811 #ifdef MULTI_FRAME
2812 Vlast_event_frame = internal_last_event_frame;
2813 #endif
2815 return (obj);
2818 /* Process any events that are not user-visible,
2819 then return, without reading any user-visible events. */
2821 void
2822 swallow_events (do_display)
2823 int do_display;
2825 int old_timers_run;
2827 while (kbd_fetch_ptr != kbd_store_ptr)
2829 struct input_event *event;
2831 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2832 ? kbd_fetch_ptr
2833 : kbd_buffer);
2835 last_event_timestamp = event->timestamp;
2837 /* These two kinds of events get special handling
2838 and don't actually appear to the command loop. */
2839 if (event->kind == selection_request_event)
2841 #ifdef HAVE_X11
2842 struct input_event copy;
2844 /* Remove it from the buffer before processing it,
2845 since otherwise swallow_events called recursively could see it
2846 and process it again. */
2847 copy = *event;
2848 kbd_fetch_ptr = event + 1;
2849 input_pending = readable_events (0);
2850 x_handle_selection_request (&copy);
2851 #else
2852 /* We're getting selection request events, but we don't have
2853 a window system. */
2854 abort ();
2855 #endif
2858 else if (event->kind == selection_clear_event)
2860 #ifdef HAVE_X11
2861 struct input_event copy;
2863 /* Remove it from the buffer before processing it, */
2864 copy = *event;
2866 kbd_fetch_ptr = event + 1;
2867 input_pending = readable_events (0);
2868 x_handle_selection_clear (&copy);
2869 #else
2870 /* We're getting selection request events, but we don't have
2871 a window system. */
2872 abort ();
2873 #endif
2875 /* Note that timer_event is currently never used. */
2876 else if (event->kind == timer_event)
2878 Lisp_Object tem, lisp_event;
2879 int was_locked = single_kboard;
2881 tem = get_keymap_1 (Vspecial_event_map, 0, 0);
2882 tem = get_keyelt (access_keymap (tem, Qtimer_event, 0, 0),
2884 lisp_event = Fcons (Qtimer_event,
2885 Fcons (Fcdr (event->frame_or_window), Qnil));
2886 kbd_fetch_ptr = event + 1;
2887 if (kbd_fetch_ptr == kbd_store_ptr)
2888 input_pending = 0;
2889 Fcommand_execute (tem, Qnil, Fvector (1, &lisp_event), Qt);
2890 timers_run++;
2891 if (do_display)
2892 redisplay_preserve_echo_area ();
2894 /* Resume allowing input from any kboard, if that was true before. */
2895 if (!was_locked)
2896 any_kboard_state ();
2898 else
2899 break;
2902 old_timers_run = timers_run;
2903 get_input_pending (&input_pending, 1);
2905 if (timers_run != old_timers_run && do_display)
2906 redisplay_preserve_echo_area ();
2909 static EMACS_TIME timer_idleness_start_time;
2911 /* Record the start of when Emacs is idle,
2912 for the sake of running idle-time timers. */
2914 timer_start_idle ()
2916 Lisp_Object timers;
2918 /* If we are already in the idle state, do nothing. */
2919 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
2920 return;
2922 EMACS_GET_TIME (timer_idleness_start_time);
2924 /* Mark all idle-time timers as once again candidates for running. */
2925 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCONS (timers)->cdr)
2927 Lisp_Object timer;
2929 timer = XCONS (timers)->car;
2931 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
2932 continue;
2933 XVECTOR (timer)->contents[0] = Qnil;
2937 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
2939 timer_stop_idle ()
2941 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
2944 /* This is only for debugging. */
2945 struct input_event last_timer_event;
2947 /* Check whether a timer has fired. To prevent larger problems we simply
2948 disregard elements that are not proper timers. Do not make a circular
2949 timer list for the time being.
2951 Returns the number of seconds to wait until the next timer fires. If a
2952 timer is triggering now, return zero seconds.
2953 If no timer is active, return -1 seconds.
2955 If a timer is ripe, we run it, with quitting turned off.
2957 DO_IT_NOW is now ignored. It used to mean that we should
2958 run the timer directly instead of queueing a timer-event.
2959 Now we always run timers directly. */
2961 EMACS_TIME
2962 timer_check (do_it_now)
2963 int do_it_now;
2965 EMACS_TIME nexttime;
2966 EMACS_TIME now, idleness_now;
2967 Lisp_Object timers, idle_timers, chosen_timer;
2968 /* Nonzero if we generate some events. */
2969 int events_generated = 0;
2970 struct gcpro gcpro1, gcpro2, gcpro3;
2972 EMACS_SET_SECS (nexttime, -1);
2973 EMACS_SET_USECS (nexttime, -1);
2975 /* Always consider the ordinary timers. */
2976 timers = Vtimer_list;
2977 /* Consider the idle timers only if Emacs is idle. */
2978 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
2979 idle_timers = Vtimer_idle_list;
2980 else
2981 idle_timers = Qnil;
2982 chosen_timer = Qnil;
2983 GCPRO3 (timers, idle_timers, chosen_timer);
2985 if (CONSP (timers) || CONSP (idle_timers))
2987 EMACS_GET_TIME (now);
2988 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
2989 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
2992 while (CONSP (timers) || CONSP (idle_timers))
2994 int triggertime = EMACS_SECS (now);
2995 Lisp_Object *vector;
2996 Lisp_Object timer, idle_timer;
2997 EMACS_TIME timer_time, idle_timer_time;
2998 EMACS_TIME difference, timer_difference, idle_timer_difference;
3000 /* Skip past invalid timers and timers already handled. */
3001 if (!NILP (timers))
3003 timer = XCONS (timers)->car;
3004 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3006 timers = XCONS (timers)->cdr;
3007 continue;
3009 vector = XVECTOR (timer)->contents;
3011 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3012 || !INTEGERP (vector[3])
3013 || ! NILP (vector[0]))
3015 timers = XCONS (timers)->cdr;
3016 continue;
3019 if (!NILP (idle_timers))
3021 timer = XCONS (idle_timers)->car;
3022 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3024 idle_timers = XCONS (idle_timers)->cdr;
3025 continue;
3027 vector = XVECTOR (timer)->contents;
3029 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3030 || !INTEGERP (vector[3])
3031 || ! NILP (vector[0]))
3033 idle_timers = XCONS (idle_timers)->cdr;
3034 continue;
3038 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
3039 based on the next ordinary timer.
3040 TIMER_DIFFERENCE is the distance in time from NOW to when
3041 this timer becomes ripe (negative if it's already ripe). */
3042 if (!NILP (timers))
3044 timer = XCONS (timers)->car;
3045 vector = XVECTOR (timer)->contents;
3046 EMACS_SET_SECS (timer_time,
3047 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3048 EMACS_SET_USECS (timer_time, XINT (vector[3]));
3049 EMACS_SUB_TIME (timer_difference, timer_time, now);
3052 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
3053 based on the next idle timer. */
3054 if (!NILP (idle_timers))
3056 idle_timer = XCONS (idle_timers)->car;
3057 vector = XVECTOR (idle_timer)->contents;
3058 EMACS_SET_SECS (idle_timer_time,
3059 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3060 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
3061 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
3064 /* Decide which timer is the next timer,
3065 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
3066 Also step down the list where we found that timer. */
3068 if (! NILP (timers) && ! NILP (idle_timers))
3070 EMACS_TIME temp;
3071 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
3072 if (EMACS_TIME_NEG_P (temp))
3074 chosen_timer = timer;
3075 timers = XCONS (timers)->cdr;
3076 difference = timer_difference;
3078 else
3080 chosen_timer = idle_timer;
3081 idle_timers = XCONS (idle_timers)->cdr;
3082 difference = idle_timer_difference;
3085 else if (! NILP (timers))
3087 chosen_timer = timer;
3088 timers = XCONS (timers)->cdr;
3089 difference = timer_difference;
3091 else
3093 chosen_timer = idle_timer;
3094 idle_timers = XCONS (idle_timers)->cdr;
3095 difference = idle_timer_difference;
3097 vector = XVECTOR (chosen_timer)->contents;
3099 /* If timer is rupe, run it if it hasn't been run. */
3100 if (EMACS_TIME_NEG_P (difference)
3101 || (EMACS_SECS (difference) == 0
3102 && EMACS_USECS (difference) == 0))
3104 if (NILP (vector[0]))
3106 /* Mark the timer as triggered to prevent problems if the lisp
3107 code fails to reschedule it right. */
3108 vector[0] = Qt;
3110 /* Run the timer or queue a timer event. */
3111 if (1)
3113 Lisp_Object tem, event;
3114 int was_locked = single_kboard;
3115 int count = specpdl_ptr - specpdl;
3117 specbind (Qinhibit_quit, Qt);
3119 tem = get_keymap_1 (Vspecial_event_map, 0, 0);
3120 tem = get_keyelt (access_keymap (tem, Qtimer_event, 0, 0),
3122 event = Fcons (Qtimer_event, Fcons (chosen_timer, Qnil));
3123 Fcommand_execute (tem, Qnil, Fvector (1, &event), Qt);
3124 timers_run++;
3126 unbind_to (count, Qnil);
3128 /* Resume allowing input from any kboard, if that was true before. */
3129 if (!was_locked)
3130 any_kboard_state ();
3132 /* Since we have handled the event,
3133 we don't need to tell the caller to wake up and do it. */
3135 #if 0
3136 else
3138 /* Generate a timer event so the caller will handle it. */
3139 struct input_event event;
3141 event.kind = timer_event;
3142 event.modifiers = 0;
3143 event.x = event.y = Qnil;
3144 event.timestamp = triggertime;
3145 /* Store the timer in the frame slot. */
3146 event.frame_or_window
3147 = Fcons (Fselected_frame (), chosen_timer);
3148 kbd_buffer_store_event (&event);
3150 last_timer_event = event;
3152 /* Tell caller to handle this event right away. */
3153 events_generated = 1;
3154 EMACS_SET_SECS (nexttime, 0);
3155 EMACS_SET_USECS (nexttime, 0);
3157 /* Don't queue more than one event at once.
3158 When Emacs is ready for another, it will
3159 queue the next one. */
3160 UNGCPRO;
3161 return nexttime;
3163 #endif /* 0 */
3166 else
3167 /* When we encounter a timer that is still waiting,
3168 return the amount of time to wait before it is ripe. */
3170 UNGCPRO;
3171 /* But if we generated an event,
3172 tell the caller to handle it now. */
3173 if (events_generated)
3174 return nexttime;
3175 return difference;
3179 /* No timers are pending in the future. */
3180 /* Return 0 if we generated an event, and -1 if not. */
3181 UNGCPRO;
3182 return nexttime;
3185 /* Caches for modify_event_symbol. */
3186 static Lisp_Object accent_key_syms;
3187 static Lisp_Object func_key_syms;
3188 static Lisp_Object mouse_syms;
3190 /* This is a list of keysym codes for special "accent" characters.
3191 It parallels lispy_accent_keys. */
3193 static int lispy_accent_codes[] =
3195 #ifdef XK_dead_circumflex
3196 XK_dead_circumflex,
3197 #else
3199 #endif
3200 #ifdef XK_dead_grave
3201 XK_dead_grave,
3202 #else
3204 #endif
3205 #ifdef XK_dead_tilde
3206 XK_dead_tilde,
3207 #else
3209 #endif
3210 #ifdef XK_dead_diaeresis
3211 XK_dead_diaeresis,
3212 #else
3214 #endif
3215 #ifdef XK_dead_macron
3216 XK_dead_macron,
3217 #else
3219 #endif
3220 #ifdef XK_dead_degree
3221 XK_dead_degree,
3222 #else
3224 #endif
3225 #ifdef XK_dead_acute
3226 XK_dead_acute,
3227 #else
3229 #endif
3230 #ifdef XK_dead_cedilla
3231 XK_dead_cedilla,
3232 #else
3234 #endif
3235 #ifdef XK_dead_breve
3236 XK_dead_breve,
3237 #else
3239 #endif
3240 #ifdef XK_dead_ogonek
3241 XK_dead_ogonek,
3242 #else
3244 #endif
3245 #ifdef XK_dead_caron
3246 XK_dead_caron,
3247 #else
3249 #endif
3250 #ifdef XK_dead_doubleacute
3251 XK_dead_doubleacute,
3252 #else
3254 #endif
3255 #ifdef XK_dead_abovedot
3256 XK_dead_abovedot,
3257 #else
3259 #endif
3262 /* This is a list of Lisp names for special "accent" characters.
3263 It parallels lispy_accent_codes. */
3265 static char *lispy_accent_keys[] =
3267 "dead-circumflex",
3268 "dead-grave",
3269 "dead-tilde",
3270 "dead-diaeresis",
3271 "dead-macron",
3272 "dead-degree",
3273 "dead-acute",
3274 "dead-cedilla",
3275 "dead-breve",
3276 "dead-ogonek",
3277 "dead-caron",
3278 "dead-doubleacute",
3279 "dead-abovedot",
3282 #ifdef HAVE_NTGUI
3283 #define FUNCTION_KEY_OFFSET 0x0
3285 char *lispy_function_keys[] =
3287 0, /* 0 */
3289 0, /* VK_LBUTTON 0x01 */
3290 0, /* VK_RBUTTON 0x02 */
3291 "cancel", /* VK_CANCEL 0x03 */
3292 0, /* VK_MBUTTON 0x04 */
3294 0, 0, 0, /* 0x05 .. 0x07 */
3296 "backspace", /* VK_BACK 0x08 */
3297 "tab", /* VK_TAB 0x09 */
3299 0, 0, /* 0x0A .. 0x0B */
3301 "clear", /* VK_CLEAR 0x0C */
3302 "return", /* VK_RETURN 0x0D */
3304 0, 0, /* 0x0E .. 0x0F */
3306 "shift", /* VK_SHIFT 0x10 */
3307 "control", /* VK_CONTROL 0x11 */
3308 "menu", /* VK_MENU 0x12 */
3309 "pause", /* VK_PAUSE 0x13 */
3310 "capital", /* VK_CAPITAL 0x14 */
3312 0, 0, 0, 0, 0, 0, /* 0x15 .. 0x1A */
3314 0, /* VK_ESCAPE 0x1B */
3316 0, 0, 0, 0, /* 0x1C .. 0x1F */
3318 0, /* VK_SPACE 0x20 */
3319 "prior", /* VK_PRIOR 0x21 */
3320 "next", /* VK_NEXT 0x22 */
3321 "end", /* VK_END 0x23 */
3322 "home", /* VK_HOME 0x24 */
3323 "left", /* VK_LEFT 0x25 */
3324 "up", /* VK_UP 0x26 */
3325 "right", /* VK_RIGHT 0x27 */
3326 "down", /* VK_DOWN 0x28 */
3327 "select", /* VK_SELECT 0x29 */
3328 "print", /* VK_PRINT 0x2A */
3329 "execute", /* VK_EXECUTE 0x2B */
3330 "snapshot", /* VK_SNAPSHOT 0x2C */
3331 "insert", /* VK_INSERT 0x2D */
3332 "delete", /* VK_DELETE 0x2E */
3333 "help", /* VK_HELP 0x2F */
3335 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
3337 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3339 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
3341 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
3343 0, 0, 0, 0, 0, 0, 0, 0, 0,
3344 0, 0, 0, 0, 0, 0, 0, 0, 0,
3345 0, 0, 0, 0, 0, 0, 0, 0,
3347 "lwindow", /* VK_LWIN 0x5B */
3348 "rwindow", /* VK_RWIN 0x5C */
3349 "apps", /* VK_APPS 0x5D */
3351 0, 0, /* 0x5E .. 0x5F */
3353 "kp-0", /* VK_NUMPAD0 0x60 */
3354 "kp-1", /* VK_NUMPAD1 0x61 */
3355 "kp-2", /* VK_NUMPAD2 0x62 */
3356 "kp-3", /* VK_NUMPAD3 0x63 */
3357 "kp-4", /* VK_NUMPAD4 0x64 */
3358 "kp-5", /* VK_NUMPAD5 0x65 */
3359 "kp-6", /* VK_NUMPAD6 0x66 */
3360 "kp-7", /* VK_NUMPAD7 0x67 */
3361 "kp-8", /* VK_NUMPAD8 0x68 */
3362 "kp-9", /* VK_NUMPAD9 0x69 */
3363 "kp-multiply", /* VK_MULTIPLY 0x6A */
3364 "kp-add", /* VK_ADD 0x6B */
3365 "kp-separator", /* VK_SEPARATOR 0x6C */
3366 "kp-subtract", /* VK_SUBTRACT 0x6D */
3367 "kp-decimal", /* VK_DECIMAL 0x6E */
3368 "kp-divide", /* VK_DIVIDE 0x6F */
3369 "f1", /* VK_F1 0x70 */
3370 "f2", /* VK_F2 0x71 */
3371 "f3", /* VK_F3 0x72 */
3372 "f4", /* VK_F4 0x73 */
3373 "f5", /* VK_F5 0x74 */
3374 "f6", /* VK_F6 0x75 */
3375 "f7", /* VK_F7 0x76 */
3376 "f8", /* VK_F8 0x77 */
3377 "f9", /* VK_F9 0x78 */
3378 "f10", /* VK_F10 0x79 */
3379 "f11", /* VK_F11 0x7A */
3380 "f12", /* VK_F12 0x7B */
3381 "f13", /* VK_F13 0x7C */
3382 "f14", /* VK_F14 0x7D */
3383 "f15", /* VK_F15 0x7E */
3384 "f16", /* VK_F16 0x7F */
3385 "f17", /* VK_F17 0x80 */
3386 "f18", /* VK_F18 0x81 */
3387 "f19", /* VK_F19 0x82 */
3388 "f20", /* VK_F20 0x83 */
3389 "f21", /* VK_F21 0x84 */
3390 "f22", /* VK_F22 0x85 */
3391 "f23", /* VK_F23 0x86 */
3392 "f24", /* VK_F24 0x87 */
3394 0, 0, 0, 0, /* 0x88 .. 0x8B */
3395 0, 0, 0, 0, /* 0x8C .. 0x8F */
3397 "kp-numlock", /* VK_NUMLOCK 0x90 */
3398 "scroll", /* VK_SCROLL 0x91 */
3400 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
3401 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
3402 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
3403 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
3404 "kp-end", /* VK_NUMPAD_END 0x96 */
3405 "kp-home", /* VK_NUMPAD_HOME 0x97 */
3406 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
3407 "kp-up", /* VK_NUMPAD_UP 0x99 */
3408 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
3409 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
3410 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
3411 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
3413 0, 0, /* 0x9E .. 0x9F */
3416 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
3417 * Used only as parameters to GetAsyncKeyState() and GetKeyState().
3418 * No other API or message will distinguish left and right keys this way.
3420 /* 0xA0 .. 0xEF */
3422 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3423 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3424 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3425 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3426 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3428 /* 0xF0 .. 0xF5 */
3430 0, 0, 0, 0, 0, 0,
3432 "attn", /* VK_ATTN 0xF6 */
3433 "crsel", /* VK_CRSEL 0xF7 */
3434 "exsel", /* VK_EXSEL 0xF8 */
3435 "ereof", /* VK_EREOF 0xF9 */
3436 "play", /* VK_PLAY 0xFA */
3437 "zoom", /* VK_ZOOM 0xFB */
3438 "noname", /* VK_NONAME 0xFC */
3439 "pa1", /* VK_PA1 0xFD */
3440 "oem_clear", /* VK_OEM_CLEAR 0xFE */
3443 #else
3445 #define FUNCTION_KEY_OFFSET 0xff00
3447 /* You'll notice that this table is arranged to be conveniently
3448 indexed by X Windows keysym values. */
3449 static char *lispy_function_keys[] =
3451 /* X Keysym value */
3453 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00 */
3454 "backspace",
3455 "tab",
3456 "linefeed",
3457 "clear",
3459 "return",
3460 0, 0,
3461 0, 0, 0, /* 0xff10 */
3462 "pause",
3463 0, 0, 0, 0, 0, 0, 0,
3464 "escape",
3465 0, 0, 0, 0,
3466 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff20...2f */
3467 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff30...3f */
3468 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
3470 "home", /* 0xff50 */ /* IsCursorKey */
3471 "left",
3472 "up",
3473 "right",
3474 "down",
3475 "prior",
3476 "next",
3477 "end",
3478 "begin",
3479 0, /* 0xff59 */
3480 0, 0, 0, 0, 0, 0,
3481 "select", /* 0xff60 */ /* IsMiscFunctionKey */
3482 "print",
3483 "execute",
3484 "insert",
3485 0, /* 0xff64 */
3486 "undo",
3487 "redo",
3488 "menu",
3489 "find",
3490 "cancel",
3491 "help",
3492 "break", /* 0xff6b */
3494 0, 0, 0, 0, 0, 0, 0, 0, "backtab", 0,
3495 0, /* 0xff76 */
3496 0, 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff7f */
3497 "kp-space", /* 0xff80 */ /* IsKeypadKey */
3498 0, 0, 0, 0, 0, 0, 0, 0,
3499 "kp-tab", /* 0xff89 */
3500 0, 0, 0,
3501 "kp-enter", /* 0xff8d */
3502 0, 0, 0,
3503 "kp-f1", /* 0xff91 */
3504 "kp-f2",
3505 "kp-f3",
3506 "kp-f4",
3507 "kp-home", /* 0xff95 */
3508 "kp-left",
3509 "kp-up",
3510 "kp-right",
3511 "kp-down",
3512 "kp-prior", /* kp-page-up */
3513 "kp-next", /* kp-page-down */
3514 "kp-end",
3515 "kp-begin",
3516 "kp-insert",
3517 "kp-delete",
3518 0, /* 0xffa0 */
3519 0, 0, 0, 0, 0, 0, 0, 0, 0,
3520 "kp-multiply", /* 0xffaa */
3521 "kp-add",
3522 "kp-separator",
3523 "kp-subtract",
3524 "kp-decimal",
3525 "kp-divide", /* 0xffaf */
3526 "kp-0", /* 0xffb0 */
3527 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
3528 0, /* 0xffba */
3529 0, 0,
3530 "kp-equal", /* 0xffbd */
3531 "f1", /* 0xffbe */ /* IsFunctionKey */
3532 "f2",
3533 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
3534 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
3535 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
3536 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
3537 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
3538 0, 0, 0, 0, 0, 0, 0, 0,
3539 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
3540 0, 0, 0, 0, 0, 0, 0, "delete"
3543 #endif /* HAVE_NTGUI */
3545 static char *lispy_mouse_names[] =
3547 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
3550 /* Scroll bar parts. */
3551 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
3552 Lisp_Object Qup, Qdown;
3554 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
3555 Lisp_Object *scroll_bar_parts[] = {
3556 &Qabove_handle, &Qhandle, &Qbelow_handle,
3557 &Qup, &Qdown,
3561 /* A vector, indexed by button number, giving the down-going location
3562 of currently depressed buttons, both scroll bar and non-scroll bar.
3564 The elements have the form
3565 (BUTTON-NUMBER MODIFIER-MASK . REST)
3566 where REST is the cdr of a position as it would be reported in the event.
3568 The make_lispy_event function stores positions here to tell the
3569 difference between click and drag events, and to store the starting
3570 location to be included in drag events. */
3572 static Lisp_Object button_down_location;
3574 /* Information about the most recent up-going button event: Which
3575 button, what location, and what time. */
3577 static int last_mouse_button;
3578 static int last_mouse_x;
3579 static int last_mouse_y;
3580 static unsigned long button_down_time;
3582 /* The maximum time between clicks to make a double-click,
3583 or Qnil to disable double-click detection,
3584 or Qt for no time limit. */
3585 Lisp_Object Vdouble_click_time;
3587 /* The number of clicks in this multiple-click. */
3589 int double_click_count;
3591 /* Given a struct input_event, build the lisp event which represents
3592 it. If EVENT is 0, build a mouse movement event from the mouse
3593 movement buffer, which should have a movement event in it.
3595 Note that events must be passed to this function in the order they
3596 are received; this function stores the location of button presses
3597 in order to build drag events when the button is released. */
3599 static Lisp_Object
3600 make_lispy_event (event)
3601 struct input_event *event;
3603 int i;
3605 switch (SWITCH_ENUM_CAST (event->kind))
3607 /* A simple keystroke. */
3608 case ascii_keystroke:
3610 Lisp_Object lispy_c;
3611 int c = event->code & 0377;
3612 /* Turn ASCII characters into control characters
3613 when proper. */
3614 if (event->modifiers & ctrl_modifier)
3615 c = make_ctrl_char (c);
3617 /* Add in the other modifier bits. We took care of ctrl_modifier
3618 just above, and the shift key was taken care of by the X code,
3619 and applied to control characters by make_ctrl_char. */
3620 c |= (event->modifiers
3621 & (meta_modifier | alt_modifier
3622 | hyper_modifier | super_modifier));
3623 button_down_time = 0;
3624 XSETFASTINT (lispy_c, c);
3625 return lispy_c;
3628 /* A function key. The symbol may need to have modifier prefixes
3629 tacked onto it. */
3630 case non_ascii_keystroke:
3631 button_down_time = 0;
3633 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
3634 if (event->code == lispy_accent_codes[i])
3635 return modify_event_symbol (i,
3636 event->modifiers,
3637 Qfunction_key, Qnil,
3638 lispy_accent_keys, &accent_key_syms,
3639 (sizeof (lispy_accent_keys)
3640 / sizeof (lispy_accent_keys[0])));
3642 /* Handle system-specific keysyms. */
3643 if (event->code & (1 << 28))
3645 /* We need to use an alist rather than a vector as the cache
3646 since we can't make a vector long enuf. */
3647 if (NILP (current_kboard->system_key_syms))
3648 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
3649 return modify_event_symbol (event->code,
3650 event->modifiers,
3651 Qfunction_key,
3652 current_kboard->Vsystem_key_alist,
3653 0, &current_kboard->system_key_syms,
3654 (unsigned)-1);
3657 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
3658 event->modifiers,
3659 Qfunction_key, Qnil,
3660 lispy_function_keys, &func_key_syms,
3661 (sizeof (lispy_function_keys)
3662 / sizeof (lispy_function_keys[0])));
3663 break;
3665 /* Note that timer_event is currently never used. */
3666 case timer_event:
3667 return Fcons (Qtimer_event, Fcons (Fcdr (event->frame_or_window), Qnil));
3669 #ifdef HAVE_MOUSE
3670 /* A mouse click. Figure out where it is, decide whether it's
3671 a press, click or drag, and build the appropriate structure. */
3672 case mouse_click:
3673 case scroll_bar_click:
3675 int button = event->code;
3676 int is_double;
3677 Lisp_Object position;
3678 Lisp_Object *start_pos_ptr;
3679 Lisp_Object start_pos;
3681 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
3682 abort ();
3684 /* Build the position as appropriate for this mouse click. */
3685 if (event->kind == mouse_click)
3687 int part;
3688 FRAME_PTR f = XFRAME (event->frame_or_window);
3689 Lisp_Object window;
3690 Lisp_Object posn;
3691 int row, column;
3693 /* Ignore mouse events that were made on frame that
3694 have been deleted. */
3695 if (! FRAME_LIVE_P (f))
3696 return Qnil;
3698 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
3699 &column, &row, NULL, 1);
3701 #ifndef USE_X_TOOLKIT
3702 /* In the non-toolkit version, clicks on the menu bar
3703 are ordinary button events in the event buffer.
3704 Distinguish them, and invoke the menu.
3706 (In the toolkit version, the toolkit handles the menu bar
3707 and Emacs doesn't know about it until after the user
3708 makes a selection.) */
3709 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
3710 && (event->modifiers & down_modifier))
3712 Lisp_Object items, item;
3713 int hpos;
3714 int i;
3716 #if 0
3717 /* Activate the menu bar on the down event. If the
3718 up event comes in before the menu code can deal with it,
3719 just ignore it. */
3720 if (! (event->modifiers & down_modifier))
3721 return Qnil;
3722 #endif
3724 item = Qnil;
3725 items = FRAME_MENU_BAR_ITEMS (f);
3726 for (i = 0; i < XVECTOR (items)->size; i += 4)
3728 Lisp_Object pos, string;
3729 string = XVECTOR (items)->contents[i + 1];
3730 pos = XVECTOR (items)->contents[i + 3];
3731 if (NILP (string))
3732 break;
3733 if (column >= XINT (pos)
3734 && column < XINT (pos) + XSTRING (string)->size)
3736 item = XVECTOR (items)->contents[i];
3737 break;
3741 position
3742 = Fcons (event->frame_or_window,
3743 Fcons (Qmenu_bar,
3744 Fcons (Fcons (event->x, event->y),
3745 Fcons (make_number (event->timestamp),
3746 Qnil))));
3748 return Fcons (item, Fcons (position, Qnil));
3750 #endif /* not USE_X_TOOLKIT */
3752 window = window_from_coordinates (f, column, row, &part);
3754 if (!WINDOWP (window))
3756 window = event->frame_or_window;
3757 posn = Qnil;
3759 else
3761 int pixcolumn, pixrow;
3762 column -= XINT (XWINDOW (window)->left);
3763 row -= XINT (XWINDOW (window)->top);
3764 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
3765 XSETINT (event->x, pixcolumn);
3766 XSETINT (event->y, pixrow);
3768 if (part == 1)
3769 posn = Qmode_line;
3770 else if (part == 2)
3771 posn = Qvertical_line;
3772 else
3773 XSETINT (posn,
3774 buffer_posn_from_coords (XWINDOW (window),
3775 column, row));
3778 position
3779 = Fcons (window,
3780 Fcons (posn,
3781 Fcons (Fcons (event->x, event->y),
3782 Fcons (make_number (event->timestamp),
3783 Qnil))));
3785 else
3787 Lisp_Object window;
3788 Lisp_Object portion_whole;
3789 Lisp_Object part;
3791 window = event->frame_or_window;
3792 portion_whole = Fcons (event->x, event->y);
3793 part = *scroll_bar_parts[(int) event->part];
3795 position
3796 = Fcons (window,
3797 Fcons (Qvertical_scroll_bar,
3798 Fcons (portion_whole,
3799 Fcons (make_number (event->timestamp),
3800 Fcons (part, Qnil)))));
3803 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
3805 start_pos = *start_pos_ptr;
3806 *start_pos_ptr = Qnil;
3808 is_double = (button == last_mouse_button
3809 && XINT (event->x) == last_mouse_x
3810 && XINT (event->y) == last_mouse_y
3811 && button_down_time != 0
3812 && (EQ (Vdouble_click_time, Qt)
3813 || (INTEGERP (Vdouble_click_time)
3814 && ((int)(event->timestamp - button_down_time)
3815 < XINT (Vdouble_click_time)))));
3816 last_mouse_button = button;
3817 last_mouse_x = XINT (event->x);
3818 last_mouse_y = XINT (event->y);
3820 /* If this is a button press, squirrel away the location, so
3821 we can decide later whether it was a click or a drag. */
3822 if (event->modifiers & down_modifier)
3824 if (is_double)
3826 double_click_count++;
3827 event->modifiers |= ((double_click_count > 2)
3828 ? triple_modifier
3829 : double_modifier);
3831 else
3832 double_click_count = 1;
3833 button_down_time = event->timestamp;
3834 *start_pos_ptr = Fcopy_alist (position);
3837 /* Now we're releasing a button - check the co-ordinates to
3838 see if this was a click or a drag. */
3839 else if (event->modifiers & up_modifier)
3841 /* If we did not see a down before this up,
3842 ignore the up. Probably this happened because
3843 the down event chose a menu item.
3844 It would be an annoyance to treat the release
3845 of the button that chose the menu item
3846 as a separate event. */
3848 if (!CONSP (start_pos))
3849 return Qnil;
3851 event->modifiers &= ~up_modifier;
3852 #if 0 /* Formerly we treated an up with no down as a click event. */
3853 if (!CONSP (start_pos))
3854 event->modifiers |= click_modifier;
3855 else
3856 #endif
3858 /* The third element of every position should be the (x,y)
3859 pair. */
3860 Lisp_Object down;
3862 down = Fnth (make_number (2), start_pos);
3863 if (EQ (event->x, XCONS (down)->car)
3864 && EQ (event->y, XCONS (down)->cdr))
3866 event->modifiers |= click_modifier;
3868 else
3870 button_down_time = 0;
3871 event->modifiers |= drag_modifier;
3873 /* Don't check is_double; treat this as multiple
3874 if the down-event was multiple. */
3875 if (double_click_count > 1)
3876 event->modifiers |= ((double_click_count > 2)
3877 ? triple_modifier
3878 : double_modifier);
3881 else
3882 /* Every mouse event should either have the down_modifier or
3883 the up_modifier set. */
3884 abort ();
3887 /* Get the symbol we should use for the mouse click. */
3888 Lisp_Object head;
3890 head = modify_event_symbol (button,
3891 event->modifiers,
3892 Qmouse_click, Qnil,
3893 lispy_mouse_names, &mouse_syms,
3894 (sizeof (lispy_mouse_names)
3895 / sizeof (lispy_mouse_names[0])));
3896 if (event->modifiers & drag_modifier)
3897 return Fcons (head,
3898 Fcons (start_pos,
3899 Fcons (position,
3900 Qnil)));
3901 else if (event->modifiers & (double_modifier | triple_modifier))
3902 return Fcons (head,
3903 Fcons (position,
3904 Fcons (make_number (double_click_count),
3905 Qnil)));
3906 else
3907 return Fcons (head,
3908 Fcons (position,
3909 Qnil));
3913 #ifdef WINDOWSNT
3914 case win32_scroll_bar_click:
3916 int button = event->code;
3917 int is_double;
3918 Lisp_Object position;
3919 Lisp_Object *start_pos_ptr;
3920 Lisp_Object start_pos;
3922 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
3923 abort ();
3926 Lisp_Object window;
3927 Lisp_Object portion_whole;
3928 Lisp_Object part;
3930 window = event->frame_or_window;
3931 portion_whole = Fcons (event->x, event->y);
3932 part = *scroll_bar_parts[(int) event->part];
3934 position =
3935 Fcons (window,
3936 Fcons (Qvertical_scroll_bar,
3937 Fcons (portion_whole,
3938 Fcons (make_number (event->timestamp),
3939 Fcons (part, Qnil)))));
3942 /* Always treat Win32 scroll bar events as clicks. */
3943 event->modifiers |= click_modifier;
3946 /* Get the symbol we should use for the mouse click. */
3947 Lisp_Object head;
3949 head = modify_event_symbol (button,
3950 event->modifiers,
3951 Qmouse_click, Qnil,
3952 lispy_mouse_names, &mouse_syms,
3953 (sizeof (lispy_mouse_names)
3954 / sizeof (lispy_mouse_names[0])));
3955 return Fcons (head,
3956 Fcons (position,
3957 Qnil));
3960 #endif
3962 #endif /* HAVE_MOUSE */
3964 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
3965 case menu_bar_event:
3966 /* The event value is in the cdr of the frame_or_window slot. */
3967 if (!CONSP (event->frame_or_window))
3968 abort ();
3969 return XCONS (event->frame_or_window)->cdr;
3970 #endif
3972 /* The 'kind' field of the event is something we don't recognize. */
3973 default:
3974 abort ();
3978 #ifdef HAVE_MOUSE
3980 static Lisp_Object
3981 make_lispy_movement (frame, bar_window, part, x, y, time)
3982 FRAME_PTR frame;
3983 Lisp_Object bar_window;
3984 enum scroll_bar_part part;
3985 Lisp_Object x, y;
3986 unsigned long time;
3988 #ifdef MULTI_FRAME
3989 /* Is it a scroll bar movement? */
3990 if (frame && ! NILP (bar_window))
3992 Lisp_Object part_sym;
3994 part_sym = *scroll_bar_parts[(int) part];
3995 return Fcons (Qscroll_bar_movement,
3996 (Fcons (Fcons (bar_window,
3997 Fcons (Qvertical_scroll_bar,
3998 Fcons (Fcons (x, y),
3999 Fcons (make_number (time),
4000 Fcons (part_sym,
4001 Qnil))))),
4002 Qnil)));
4005 /* Or is it an ordinary mouse movement? */
4006 else
4007 #endif /* MULTI_FRAME */
4009 int area;
4010 Lisp_Object window;
4011 Lisp_Object posn;
4012 int column, row;
4014 #ifdef MULTI_FRAME
4015 if (frame)
4016 #else
4017 if (1)
4018 #endif
4020 /* It's in a frame; which window on that frame? */
4021 pixel_to_glyph_coords (frame, XINT (x), XINT (y), &column, &row,
4022 NULL, 1);
4023 window = window_from_coordinates (frame, column, row, &area);
4025 else
4026 window = Qnil;
4028 if (WINDOWP (window))
4030 int pixcolumn, pixrow;
4031 column -= XINT (XWINDOW (window)->left);
4032 row -= XINT (XWINDOW (window)->top);
4033 glyph_to_pixel_coords (frame, column, row, &pixcolumn, &pixrow);
4034 XSETINT (x, pixcolumn);
4035 XSETINT (y, pixrow);
4037 if (area == 1)
4038 posn = Qmode_line;
4039 else if (area == 2)
4040 posn = Qvertical_line;
4041 else
4042 XSETINT (posn,
4043 buffer_posn_from_coords (XWINDOW (window), column, row));
4045 #ifdef MULTI_FRAME
4046 else if (frame != 0)
4048 XSETFRAME (window, frame);
4049 posn = Qnil;
4051 #endif
4052 else
4054 window = Qnil;
4055 posn = Qnil;
4056 XSETFASTINT (x, 0);
4057 XSETFASTINT (y, 0);
4060 return Fcons (Qmouse_movement,
4061 Fcons (Fcons (window,
4062 Fcons (posn,
4063 Fcons (Fcons (x, y),
4064 Fcons (make_number (time),
4065 Qnil)))),
4066 Qnil));
4070 #endif /* HAVE_MOUSE */
4072 /* Construct a switch frame event. */
4073 static Lisp_Object
4074 make_lispy_switch_frame (frame)
4075 Lisp_Object frame;
4077 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
4080 /* Manipulating modifiers. */
4082 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
4084 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
4085 SYMBOL's name of the end of the modifiers; the string from this
4086 position is the unmodified symbol name.
4088 This doesn't use any caches. */
4090 static int
4091 parse_modifiers_uncached (symbol, modifier_end)
4092 Lisp_Object symbol;
4093 int *modifier_end;
4095 struct Lisp_String *name;
4096 int i;
4097 int modifiers;
4099 CHECK_SYMBOL (symbol, 1);
4101 modifiers = 0;
4102 name = XSYMBOL (symbol)->name;
4104 for (i = 0; i+2 <= name->size; )
4106 int this_mod_end = 0;
4107 int this_mod = 0;
4109 /* See if the name continues with a modifier word.
4110 Check that the word appears, but don't check what follows it.
4111 Set this_mod and this_mod_end to record what we find. */
4113 switch (name->data[i])
4115 #define SINGLE_LETTER_MOD(BIT) \
4116 (this_mod_end = i + 1, this_mod = BIT)
4118 case 'A':
4119 SINGLE_LETTER_MOD (alt_modifier);
4120 break;
4122 case 'C':
4123 SINGLE_LETTER_MOD (ctrl_modifier);
4124 break;
4126 case 'H':
4127 SINGLE_LETTER_MOD (hyper_modifier);
4128 break;
4130 case 'M':
4131 SINGLE_LETTER_MOD (meta_modifier);
4132 break;
4134 case 'S':
4135 SINGLE_LETTER_MOD (shift_modifier);
4136 break;
4138 case 's':
4139 SINGLE_LETTER_MOD (super_modifier);
4140 break;
4142 #undef SINGLE_LETTER_MOD
4145 /* If we found no modifier, stop looking for them. */
4146 if (this_mod_end == 0)
4147 break;
4149 /* Check there is a dash after the modifier, so that it
4150 really is a modifier. */
4151 if (this_mod_end >= name->size || name->data[this_mod_end] != '-')
4152 break;
4154 /* This modifier is real; look for another. */
4155 modifiers |= this_mod;
4156 i = this_mod_end + 1;
4159 /* Should we include the `click' modifier? */
4160 if (! (modifiers & (down_modifier | drag_modifier
4161 | double_modifier | triple_modifier))
4162 && i + 7 == name->size
4163 && strncmp (name->data + i, "mouse-", 6) == 0
4164 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
4165 modifiers |= click_modifier;
4167 if (modifier_end)
4168 *modifier_end = i;
4170 return modifiers;
4173 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
4174 prepended to the string BASE[0..BASE_LEN-1].
4175 This doesn't use any caches. */
4176 static Lisp_Object
4177 apply_modifiers_uncached (modifiers, base, base_len)
4178 int modifiers;
4179 char *base;
4180 int base_len;
4182 /* Since BASE could contain nulls, we can't use intern here; we have
4183 to use Fintern, which expects a genuine Lisp_String, and keeps a
4184 reference to it. */
4185 char *new_mods =
4186 (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
4187 int mod_len;
4190 char *p = new_mods;
4192 /* Only the event queue may use the `up' modifier; it should always
4193 be turned into a click or drag event before presented to lisp code. */
4194 if (modifiers & up_modifier)
4195 abort ();
4197 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
4198 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
4199 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
4200 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
4201 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
4202 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
4203 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
4204 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
4205 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
4206 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
4207 /* The click modifier is denoted by the absence of other modifiers. */
4209 *p = '\0';
4211 mod_len = p - new_mods;
4215 Lisp_Object new_name;
4217 new_name = make_uninit_string (mod_len + base_len);
4218 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
4219 bcopy (base, XSTRING (new_name)->data + mod_len, base_len);
4221 return Fintern (new_name, Qnil);
4226 static char *modifier_names[] =
4228 "up", "down", "drag", "click", "double", "triple", 0, 0,
4229 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4230 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
4232 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
4234 static Lisp_Object modifier_symbols;
4236 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
4237 static Lisp_Object
4238 lispy_modifier_list (modifiers)
4239 int modifiers;
4241 Lisp_Object modifier_list;
4242 int i;
4244 modifier_list = Qnil;
4245 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
4246 if (modifiers & (1<<i))
4247 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
4248 modifier_list);
4250 return modifier_list;
4254 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
4255 where UNMODIFIED is the unmodified form of SYMBOL,
4256 MASK is the set of modifiers present in SYMBOL's name.
4257 This is similar to parse_modifiers_uncached, but uses the cache in
4258 SYMBOL's Qevent_symbol_element_mask property, and maintains the
4259 Qevent_symbol_elements property. */
4261 static Lisp_Object
4262 parse_modifiers (symbol)
4263 Lisp_Object symbol;
4265 Lisp_Object elements;
4267 elements = Fget (symbol, Qevent_symbol_element_mask);
4268 if (CONSP (elements))
4269 return elements;
4270 else
4272 int end;
4273 int modifiers = parse_modifiers_uncached (symbol, &end);
4274 Lisp_Object unmodified;
4275 Lisp_Object mask;
4277 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
4278 XSYMBOL (symbol)->name->size - end),
4279 Qnil);
4281 if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1))
4282 abort ();
4283 XSETFASTINT (mask, modifiers);
4284 elements = Fcons (unmodified, Fcons (mask, Qnil));
4286 /* Cache the parsing results on SYMBOL. */
4287 Fput (symbol, Qevent_symbol_element_mask,
4288 elements);
4289 Fput (symbol, Qevent_symbol_elements,
4290 Fcons (unmodified, lispy_modifier_list (modifiers)));
4292 /* Since we know that SYMBOL is modifiers applied to unmodified,
4293 it would be nice to put that in unmodified's cache.
4294 But we can't, since we're not sure that parse_modifiers is
4295 canonical. */
4297 return elements;
4301 /* Apply the modifiers MODIFIERS to the symbol BASE.
4302 BASE must be unmodified.
4304 This is like apply_modifiers_uncached, but uses BASE's
4305 Qmodifier_cache property, if present. It also builds
4306 Qevent_symbol_elements properties, since it has that info anyway.
4308 apply_modifiers copies the value of BASE's Qevent_kind property to
4309 the modified symbol. */
4310 static Lisp_Object
4311 apply_modifiers (modifiers, base)
4312 int modifiers;
4313 Lisp_Object base;
4315 Lisp_Object cache, index, entry, new_symbol;
4317 /* Mask out upper bits. We don't know where this value's been. */
4318 modifiers &= ((EMACS_INT)1 << VALBITS) - 1;
4320 /* The click modifier never figures into cache indices. */
4321 cache = Fget (base, Qmodifier_cache);
4322 XSETFASTINT (index, (modifiers & ~click_modifier));
4323 entry = assq_no_quit (index, cache);
4325 if (CONSP (entry))
4326 new_symbol = XCONS (entry)->cdr;
4327 else
4329 /* We have to create the symbol ourselves. */
4330 new_symbol = apply_modifiers_uncached (modifiers,
4331 XSYMBOL (base)->name->data,
4332 XSYMBOL (base)->name->size);
4334 /* Add the new symbol to the base's cache. */
4335 entry = Fcons (index, new_symbol);
4336 Fput (base, Qmodifier_cache, Fcons (entry, cache));
4338 /* We have the parsing info now for free, so add it to the caches. */
4339 XSETFASTINT (index, modifiers);
4340 Fput (new_symbol, Qevent_symbol_element_mask,
4341 Fcons (base, Fcons (index, Qnil)));
4342 Fput (new_symbol, Qevent_symbol_elements,
4343 Fcons (base, lispy_modifier_list (modifiers)));
4346 /* Make sure this symbol is of the same kind as BASE.
4348 You'd think we could just set this once and for all when we
4349 intern the symbol above, but reorder_modifiers may call us when
4350 BASE's property isn't set right; we can't assume that just
4351 because it has a Qmodifier_cache property it must have its
4352 Qevent_kind set right as well. */
4353 if (NILP (Fget (new_symbol, Qevent_kind)))
4355 Lisp_Object kind;
4357 kind = Fget (base, Qevent_kind);
4358 if (! NILP (kind))
4359 Fput (new_symbol, Qevent_kind, kind);
4362 return new_symbol;
4366 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
4367 return a symbol with the modifiers placed in the canonical order.
4368 Canonical order is alphabetical, except for down and drag, which
4369 always come last. The 'click' modifier is never written out.
4371 Fdefine_key calls this to make sure that (for example) C-M-foo
4372 and M-C-foo end up being equivalent in the keymap. */
4374 Lisp_Object
4375 reorder_modifiers (symbol)
4376 Lisp_Object symbol;
4378 /* It's hopefully okay to write the code this way, since everything
4379 will soon be in caches, and no consing will be done at all. */
4380 Lisp_Object parsed;
4382 parsed = parse_modifiers (symbol);
4383 return apply_modifiers ((int) XINT (XCONS (XCONS (parsed)->cdr)->car),
4384 XCONS (parsed)->car);
4388 /* For handling events, we often want to produce a symbol whose name
4389 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
4390 to some base, like the name of a function key or mouse button.
4391 modify_event_symbol produces symbols of this sort.
4393 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
4394 is the name of the i'th symbol. TABLE_SIZE is the number of elements
4395 in the table.
4397 Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
4398 NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
4400 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
4401 persist between calls to modify_event_symbol that it can use to
4402 store a cache of the symbols it's generated for this NAME_TABLE
4403 before. The object stored there may be a vector or an alist.
4405 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
4407 MODIFIERS is a set of modifier bits (as given in struct input_events)
4408 whose prefixes should be applied to the symbol name.
4410 SYMBOL_KIND is the value to be placed in the event_kind property of
4411 the returned symbol.
4413 The symbols we create are supposed to have an
4414 `event-symbol-elements' property, which lists the modifiers present
4415 in the symbol's name. */
4417 static Lisp_Object
4418 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
4419 name_table, symbol_table, table_size)
4420 int symbol_num;
4421 unsigned modifiers;
4422 Lisp_Object symbol_kind;
4423 Lisp_Object name_alist;
4424 char **name_table;
4425 Lisp_Object *symbol_table;
4426 unsigned int table_size;
4428 Lisp_Object value;
4429 Lisp_Object symbol_int;
4431 /* Get rid of the "vendor-specific" bit here. */
4432 XSETINT (symbol_int, symbol_num & 0xffffff);
4434 /* Is this a request for a valid symbol? */
4435 if (symbol_num < 0 || symbol_num >= table_size)
4436 return Qnil;
4438 if (CONSP (*symbol_table))
4439 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
4441 /* If *symbol_table doesn't seem to be initialized properly, fix that.
4442 *symbol_table should be a lisp vector TABLE_SIZE elements long,
4443 where the Nth element is the symbol for NAME_TABLE[N], or nil if
4444 we've never used that symbol before. */
4445 else
4447 if (! VECTORP (*symbol_table)
4448 || XVECTOR (*symbol_table)->size != table_size)
4450 Lisp_Object size;
4452 XSETFASTINT (size, table_size);
4453 *symbol_table = Fmake_vector (size, Qnil);
4456 value = XVECTOR (*symbol_table)->contents[symbol_num];
4459 /* Have we already used this symbol before? */
4460 if (NILP (value))
4462 /* No; let's create it. */
4463 if (!NILP (name_alist))
4464 value = Fcdr_safe (Fassq (symbol_int, name_alist));
4465 else if (name_table[symbol_num])
4466 value = intern (name_table[symbol_num]);
4468 #ifdef HAVE_WINDOW_SYSTEM
4469 if (NILP (value))
4471 char *name = x_get_keysym_name (symbol_num);
4472 if (name)
4473 value = intern (name);
4475 #endif
4477 if (NILP (value))
4479 char buf[20];
4480 sprintf (buf, "key-%d", symbol_num);
4481 value = intern (buf);
4484 if (CONSP (*symbol_table))
4485 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
4486 else
4487 XVECTOR (*symbol_table)->contents[symbol_num] = value;
4489 /* Fill in the cache entries for this symbol; this also
4490 builds the Qevent_symbol_elements property, which the user
4491 cares about. */
4492 apply_modifiers (modifiers & click_modifier, value);
4493 Fput (value, Qevent_kind, symbol_kind);
4496 /* Apply modifiers to that symbol. */
4497 return apply_modifiers (modifiers, value);
4500 /* Convert a list that represents an event type,
4501 such as (ctrl meta backspace), into the usual representation of that
4502 event type as a number or a symbol. */
4504 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
4505 "Convert the event description list EVENT-DESC to an event type.\n\
4506 EVENT-DESC should contain one base event type (a character or symbol)\n\
4507 and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\
4508 drag, down, double or triple). The base must be last.\n\
4509 The return value is an event type (a character or symbol) which\n\
4510 has the same base event type and all the specified modifiers.")
4511 (event_desc)
4512 Lisp_Object event_desc;
4514 Lisp_Object base;
4515 int modifiers = 0;
4516 Lisp_Object rest;
4518 base = Qnil;
4519 rest = event_desc;
4520 while (CONSP (rest))
4522 Lisp_Object elt;
4523 int this = 0;
4525 elt = XCONS (rest)->car;
4526 rest = XCONS (rest)->cdr;
4528 /* Given a symbol, see if it is a modifier name. */
4529 if (SYMBOLP (elt) && CONSP (rest))
4530 this = parse_solitary_modifier (elt);
4532 if (this != 0)
4533 modifiers |= this;
4534 else if (!NILP (base))
4535 error ("Two bases given in one event");
4536 else
4537 base = elt;
4541 /* Let the symbol A refer to the character A. */
4542 if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1)
4543 XSETINT (base, XSYMBOL (base)->name->data[0]);
4545 if (INTEGERP (base))
4547 /* Turn (shift a) into A. */
4548 if ((modifiers & shift_modifier) != 0
4549 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
4551 XSETINT (base, XINT (base) - ('a' - 'A'));
4552 modifiers &= ~shift_modifier;
4555 /* Turn (control a) into C-a. */
4556 if (modifiers & ctrl_modifier)
4557 return make_number ((modifiers & ~ctrl_modifier)
4558 | make_ctrl_char (XINT (base)));
4559 else
4560 return make_number (modifiers | XINT (base));
4562 else if (SYMBOLP (base))
4563 return apply_modifiers (modifiers, base);
4564 else
4565 error ("Invalid base event");
4568 /* Try to recognize SYMBOL as a modifier name.
4569 Return the modifier flag bit, or 0 if not recognized. */
4571 static int
4572 parse_solitary_modifier (symbol)
4573 Lisp_Object symbol;
4575 struct Lisp_String *name = XSYMBOL (symbol)->name;
4577 switch (name->data[0])
4579 #define SINGLE_LETTER_MOD(BIT) \
4580 if (name->size == 1) \
4581 return BIT;
4583 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
4584 if (LEN == name->size \
4585 && ! strncmp (name->data, NAME, LEN)) \
4586 return BIT;
4588 case 'A':
4589 SINGLE_LETTER_MOD (alt_modifier);
4590 break;
4592 case 'a':
4593 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
4594 break;
4596 case 'C':
4597 SINGLE_LETTER_MOD (ctrl_modifier);
4598 break;
4600 case 'c':
4601 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
4602 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
4603 break;
4605 case 'H':
4606 SINGLE_LETTER_MOD (hyper_modifier);
4607 break;
4609 case 'h':
4610 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
4611 break;
4613 case 'M':
4614 SINGLE_LETTER_MOD (meta_modifier);
4615 break;
4617 case 'm':
4618 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
4619 break;
4621 case 'S':
4622 SINGLE_LETTER_MOD (shift_modifier);
4623 break;
4625 case 's':
4626 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
4627 MULTI_LETTER_MOD (super_modifier, "super", 5);
4628 SINGLE_LETTER_MOD (super_modifier);
4629 break;
4631 case 'd':
4632 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
4633 MULTI_LETTER_MOD (down_modifier, "down", 4);
4634 MULTI_LETTER_MOD (double_modifier, "double", 6);
4635 break;
4637 case 't':
4638 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
4639 break;
4641 #undef SINGLE_LETTER_MOD
4642 #undef MULTI_LETTER_MOD
4645 return 0;
4648 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
4649 Such a list is not valid as an event,
4650 but it can be a Lucid-style event type list. */
4653 lucid_event_type_list_p (object)
4654 Lisp_Object object;
4656 Lisp_Object tail;
4658 if (! CONSP (object))
4659 return 0;
4661 for (tail = object; CONSP (tail); tail = XCONS (tail)->cdr)
4663 Lisp_Object elt;
4664 elt = XCONS (tail)->car;
4665 if (! (INTEGERP (elt) || SYMBOLP (elt)))
4666 return 0;
4669 return NILP (tail);
4672 /* Store into *addr a value nonzero if terminal input chars are available.
4673 Serves the purpose of ioctl (0, FIONREAD, addr)
4674 but works even if FIONREAD does not exist.
4675 (In fact, this may actually read some input.)
4677 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe. */
4679 static void
4680 get_input_pending (addr, do_timers_now)
4681 int *addr;
4682 int do_timers_now;
4684 /* First of all, have we already counted some input? */
4685 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
4687 /* If input is being read as it arrives, and we have none, there is none. */
4688 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
4689 return;
4691 /* Try to read some input and see how much we get. */
4692 gobble_input (0);
4693 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
4696 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
4699 gobble_input (expected)
4700 int expected;
4702 #ifndef VMS
4703 #ifdef SIGIO
4704 if (interrupt_input)
4706 SIGMASKTYPE mask;
4707 mask = sigblockx (SIGIO);
4708 read_avail_input (expected);
4709 sigsetmask (mask);
4711 else
4712 #ifdef POLL_FOR_INPUT
4713 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
4715 SIGMASKTYPE mask;
4716 mask = sigblockx (SIGALRM);
4717 read_avail_input (expected);
4718 sigsetmask (mask);
4720 else
4721 #endif
4722 #endif
4723 read_avail_input (expected);
4724 #endif
4727 /* Put a buffer_switch_event in the buffer
4728 so that read_key_sequence will notice the new current buffer. */
4730 record_asynch_buffer_change ()
4732 struct input_event event;
4733 Lisp_Object tem;
4735 event.kind = buffer_switch_event;
4736 event.frame_or_window = Qnil;
4738 #ifdef subprocesses
4739 /* We don't need a buffer-switch event unless Emacs is waiting for input.
4740 The purpose of the event is to make read_key_sequence look up the
4741 keymaps again. If we aren't in read_key_sequence, we don't need one,
4742 and the event could cause trouble by messing up (input-pending-p). */
4743 tem = Fwaiting_for_user_input_p ();
4744 if (NILP (tem))
4745 return;
4746 #else
4747 /* We never need these events if we have no asynchronous subprocesses. */
4748 return;
4749 #endif
4751 /* Make sure no interrupt happens while storing the event. */
4752 #ifdef SIGIO
4753 if (interrupt_input)
4755 SIGMASKTYPE mask;
4756 mask = sigblockx (SIGIO);
4757 kbd_buffer_store_event (&event);
4758 sigsetmask (mask);
4760 else
4761 #endif
4763 stop_polling ();
4764 kbd_buffer_store_event (&event);
4765 start_polling ();
4769 #ifndef VMS
4771 /* Read any terminal input already buffered up by the system
4772 into the kbd_buffer, but do not wait.
4774 EXPECTED should be nonzero if the caller knows there is some input.
4776 Except on VMS, all input is read by this function.
4777 If interrupt_input is nonzero, this function MUST be called
4778 only when SIGIO is blocked.
4780 Returns the number of keyboard chars read, or -1 meaning
4781 this is a bad time to try to read input. */
4783 static int
4784 read_avail_input (expected)
4785 int expected;
4787 struct input_event buf[KBD_BUFFER_SIZE];
4788 register int i;
4789 int nread;
4791 if (read_socket_hook)
4792 /* No need for FIONREAD or fcntl; just say don't wait. */
4793 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE,
4794 expected, expected);
4795 else
4797 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
4798 the kbd_buffer can really hold. That may prevent loss
4799 of characters on some systems when input is stuffed at us. */
4800 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
4801 int n_to_read;
4803 /* Determine how many characters we should *try* to read. */
4804 #ifdef WINDOWSNT
4805 return 0;
4806 #else /* not WINDOWSNT */
4807 #ifdef MSDOS
4808 n_to_read = dos_keysns ();
4809 if (n_to_read == 0)
4810 return 0;
4811 #else /* not MSDOS */
4812 #ifdef FIONREAD
4813 /* Find out how much input is available. */
4814 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
4815 /* Formerly simply reported no input, but that sometimes led to
4816 a failure of Emacs to terminate.
4817 SIGHUP seems appropriate if we can't reach the terminal. */
4818 /* ??? Is it really right to send the signal just to this process
4819 rather than to the whole process group?
4820 Perhaps on systems with FIONREAD Emacs is alone in its group. */
4821 kill (getpid (), SIGHUP);
4822 if (n_to_read == 0)
4823 return 0;
4824 if (n_to_read > sizeof cbuf)
4825 n_to_read = sizeof cbuf;
4826 #else /* no FIONREAD */
4827 #if defined (USG) || defined (DGUX)
4828 /* Read some input if available, but don't wait. */
4829 n_to_read = sizeof cbuf;
4830 fcntl (input_fd, F_SETFL, O_NDELAY);
4831 #else
4832 you lose;
4833 #endif
4834 #endif
4835 #endif /* not MSDOS */
4836 #endif /* not WINDOWSNT */
4838 /* Now read; for one reason or another, this will not block.
4839 NREAD is set to the number of chars read. */
4842 #ifdef MSDOS
4843 cbuf[0] = dos_keyread ();
4844 nread = 1;
4845 #else
4846 nread = read (input_fd, cbuf, n_to_read);
4847 #endif
4848 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
4849 /* The kernel sometimes fails to deliver SIGHUP for ptys.
4850 This looks incorrect, but it isn't, because _BSD causes
4851 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
4852 and that causes a value other than 0 when there is no input. */
4853 if (nread == 0)
4854 kill (0, SIGHUP);
4855 #endif
4857 while (
4858 /* We used to retry the read if it was interrupted.
4859 But this does the wrong thing when O_NDELAY causes
4860 an EAGAIN error. Does anybody know of a situation
4861 where a retry is actually needed? */
4862 #if 0
4863 nread < 0 && (errno == EAGAIN
4864 #ifdef EFAULT
4865 || errno == EFAULT
4866 #endif
4867 #ifdef EBADSLT
4868 || errno == EBADSLT
4869 #endif
4871 #else
4873 #endif
4876 #ifndef FIONREAD
4877 #if defined (USG) || defined (DGUX)
4878 fcntl (input_fd, F_SETFL, 0);
4879 #endif /* USG or DGUX */
4880 #endif /* no FIONREAD */
4881 for (i = 0; i < nread; i++)
4883 buf[i].kind = ascii_keystroke;
4884 buf[i].modifiers = 0;
4885 if (meta_key == 1 && (cbuf[i] & 0x80))
4886 buf[i].modifiers = meta_modifier;
4887 if (meta_key != 2)
4888 cbuf[i] &= ~0x80;
4890 buf[i].code = cbuf[i];
4891 #ifdef MULTI_FRAME
4892 XSETFRAME (buf[i].frame_or_window, selected_frame);
4893 #else
4894 buf[i].frame_or_window = Qnil;
4895 #endif
4899 /* Scan the chars for C-g and store them in kbd_buffer. */
4900 for (i = 0; i < nread; i++)
4902 kbd_buffer_store_event (&buf[i]);
4903 /* Don't look at input that follows a C-g too closely.
4904 This reduces lossage due to autorepeat on C-g. */
4905 if (buf[i].kind == ascii_keystroke
4906 && buf[i].code == quit_char)
4907 break;
4910 return nread;
4912 #endif /* not VMS */
4914 #ifdef SIGIO /* for entire page */
4915 /* Note SIGIO has been undef'd if FIONREAD is missing. */
4917 SIGTYPE
4918 input_available_signal (signo)
4919 int signo;
4921 /* Must preserve main program's value of errno. */
4922 int old_errno = errno;
4923 #ifdef BSD4_1
4924 extern int select_alarmed;
4925 #endif
4927 #ifdef USG
4928 /* USG systems forget handlers when they are used;
4929 must reestablish each time */
4930 signal (signo, input_available_signal);
4931 #endif /* USG */
4933 #ifdef BSD4_1
4934 sigisheld (SIGIO);
4935 #endif
4937 if (input_available_clear_time)
4938 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
4940 while (1)
4942 int nread;
4943 nread = read_avail_input (1);
4944 /* -1 means it's not ok to read the input now.
4945 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
4946 0 means there was no keyboard input available. */
4947 if (nread <= 0)
4948 break;
4950 #ifdef BSD4_1
4951 select_alarmed = 1; /* Force the select emulator back to life */
4952 #endif
4955 #ifdef BSD4_1
4956 sigfree ();
4957 #endif
4958 errno = old_errno;
4960 #endif /* SIGIO */
4962 /* Send ourselves a SIGIO.
4964 This function exists so that the UNBLOCK_INPUT macro in
4965 blockinput.h can have some way to take care of input we put off
4966 dealing with, without assuming that every file which uses
4967 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
4968 void
4969 reinvoke_input_signal ()
4971 #ifdef SIGIO
4972 kill (getpid (), SIGIO);
4973 #endif
4978 /* Return the prompt-string of a sparse keymap.
4979 This is the first element which is a string.
4980 Return nil if there is none. */
4982 Lisp_Object
4983 map_prompt (map)
4984 Lisp_Object map;
4986 while (CONSP (map))
4988 register Lisp_Object tem;
4989 tem = Fcar (map);
4990 if (STRINGP (tem))
4991 return tem;
4992 map = Fcdr (map);
4994 return Qnil;
4997 static void menu_bar_item ();
4998 static void menu_bar_one_keymap ();
5000 /* These variables hold the vector under construction within
5001 menu_bar_items and its subroutines, and the current index
5002 for storing into that vector. */
5003 static Lisp_Object menu_bar_items_vector;
5004 static int menu_bar_items_index;
5006 /* Return a vector of menu items for a menu bar, appropriate
5007 to the current buffer. Each item has three elements in the vector:
5008 KEY STRING MAPLIST.
5010 OLD is an old vector we can optionally reuse, or nil. */
5012 Lisp_Object
5013 menu_bar_items (old)
5014 Lisp_Object old;
5016 /* The number of keymaps we're scanning right now, and the number of
5017 keymaps we have allocated space for. */
5018 int nmaps;
5020 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
5021 in the current keymaps, or nil where it is not a prefix. */
5022 Lisp_Object *maps;
5024 Lisp_Object def, tem, tail;
5026 Lisp_Object result;
5028 int mapno;
5029 Lisp_Object oquit;
5031 int i;
5033 struct gcpro gcpro1;
5035 /* In order to build the menus, we need to call the keymap
5036 accessors. They all call QUIT. But this function is called
5037 during redisplay, during which a quit is fatal. So inhibit
5038 quitting while building the menus.
5039 We do this instead of specbind because (1) errors will clear it anyway
5040 and (2) this avoids risk of specpdl overflow. */
5041 oquit = Vinhibit_quit;
5042 Vinhibit_quit = Qt;
5044 if (!NILP (old))
5045 menu_bar_items_vector = old;
5046 else
5047 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
5048 menu_bar_items_index = 0;
5050 GCPRO1 (menu_bar_items_vector);
5052 /* Build our list of keymaps.
5053 If we recognize a function key and replace its escape sequence in
5054 keybuf with its symbol, or if the sequence starts with a mouse
5055 click and we need to switch buffers, we jump back here to rebuild
5056 the initial keymaps from the current buffer. */
5058 Lisp_Object *tmaps;
5060 /* Should overriding-terminal-local-map and overriding-local-map apply? */
5061 if (!NILP (Voverriding_local_map_menu_flag))
5063 /* Yes, use them (if non-nil) as well as the global map. */
5064 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
5065 nmaps = 0;
5066 if (!NILP (current_kboard->Voverriding_terminal_local_map))
5067 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
5068 if (!NILP (Voverriding_local_map))
5069 maps[nmaps++] = Voverriding_local_map;
5071 else
5073 /* No, so use major and minor mode keymaps. */
5074 nmaps = current_minor_maps (NULL, &tmaps);
5075 maps = (Lisp_Object *) alloca ((nmaps + 2) * sizeof (maps[0]));
5076 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
5077 #ifdef USE_TEXT_PROPERTIES
5078 maps[nmaps++] = get_local_map (PT, current_buffer);
5079 #else
5080 maps[nmaps++] = current_buffer->keymap;
5081 #endif
5083 maps[nmaps++] = current_global_map;
5086 /* Look up in each map the dummy prefix key `menu-bar'. */
5088 result = Qnil;
5090 for (mapno = nmaps - 1; mapno >= 0; mapno--)
5092 if (! NILP (maps[mapno]))
5093 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0));
5094 else
5095 def = Qnil;
5097 tem = Fkeymapp (def);
5098 if (!NILP (tem))
5099 menu_bar_one_keymap (def);
5102 /* Move to the end those items that should be at the end. */
5104 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCONS (tail)->cdr)
5106 int i;
5107 int end = menu_bar_items_index;
5109 for (i = 0; i < end; i += 4)
5110 if (EQ (XCONS (tail)->car, XVECTOR (menu_bar_items_vector)->contents[i]))
5112 Lisp_Object tem0, tem1, tem2, tem3;
5113 /* Move the item at index I to the end,
5114 shifting all the others forward. */
5115 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
5116 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
5117 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
5118 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
5119 if (end > i + 4)
5120 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
5121 &XVECTOR (menu_bar_items_vector)->contents[i],
5122 (end - i - 4) * sizeof (Lisp_Object));
5123 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
5124 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
5125 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
5126 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
5127 break;
5131 /* Add nil, nil, nil, nil at the end. */
5132 i = menu_bar_items_index;
5133 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
5135 Lisp_Object tem;
5136 int newsize = 2 * i;
5137 tem = Fmake_vector (make_number (2 * i), Qnil);
5138 bcopy (XVECTOR (menu_bar_items_vector)->contents,
5139 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
5140 menu_bar_items_vector = tem;
5142 /* Add this item. */
5143 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5144 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5145 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5146 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5147 menu_bar_items_index = i;
5149 Vinhibit_quit = oquit;
5150 UNGCPRO;
5151 return menu_bar_items_vector;
5154 /* Scan one map KEYMAP, accumulating any menu items it defines
5155 in menu_bar_items_vector. */
5157 static void
5158 menu_bar_one_keymap (keymap)
5159 Lisp_Object keymap;
5161 Lisp_Object tail, item, key, binding, item_string, table;
5163 /* Loop over all keymap entries that have menu strings. */
5164 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
5166 item = XCONS (tail)->car;
5167 if (CONSP (item))
5169 key = XCONS (item)->car;
5170 binding = XCONS (item)->cdr;
5171 if (CONSP (binding))
5173 item_string = XCONS (binding)->car;
5174 if (STRINGP (item_string))
5175 menu_bar_item (key, item_string, Fcdr (binding));
5177 else if (EQ (binding, Qundefined))
5178 menu_bar_item (key, Qnil, binding);
5180 else if (VECTORP (item))
5182 /* Loop over the char values represented in the vector. */
5183 int len = XVECTOR (item)->size;
5184 int c;
5185 for (c = 0; c < len; c++)
5187 Lisp_Object character;
5188 XSETFASTINT (character, c);
5189 binding = XVECTOR (item)->contents[c];
5190 if (CONSP (binding))
5192 item_string = XCONS (binding)->car;
5193 if (STRINGP (item_string))
5194 menu_bar_item (key, item_string, Fcdr (binding));
5196 else if (EQ (binding, Qundefined))
5197 menu_bar_item (key, Qnil, binding);
5203 /* This is used as the handler when calling internal_condition_case_1. */
5205 static Lisp_Object
5206 menu_bar_item_1 (arg)
5207 Lisp_Object arg;
5209 return Qnil;
5212 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
5213 If there's already an item for KEY, add this DEF to it. */
5215 static void
5216 menu_bar_item (key, item_string, def)
5217 Lisp_Object key, item_string, def;
5219 Lisp_Object tem;
5220 Lisp_Object enabled;
5221 int i;
5223 /* Skip menu-bar equiv keys data. */
5224 if (CONSP (def) && CONSP (XCONS (def)->car))
5225 def = XCONS (def)->cdr;
5227 if (EQ (def, Qundefined))
5229 /* If a map has an explicit `undefined' as definition,
5230 discard any previously made menu bar item. */
5232 for (i = 0; i < menu_bar_items_index; i += 4)
5233 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
5235 if (menu_bar_items_index > i + 4)
5236 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
5237 &XVECTOR (menu_bar_items_vector)->contents[i],
5238 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
5239 menu_bar_items_index -= 4;
5240 return;
5243 /* If there's no definition for this key yet,
5244 just ignore `undefined'. */
5245 return;
5248 /* See if this entry is enabled. */
5249 enabled = Qt;
5251 if (SYMBOLP (def))
5253 /* No property, or nil, means enable.
5254 Otherwise, enable if value is not nil. */
5255 tem = Fget (def, Qmenu_enable);
5256 if (!NILP (tem))
5257 /* (condition-case nil (eval tem)
5258 (error nil)) */
5259 enabled = internal_condition_case_1 (Feval, tem, Qerror,
5260 menu_bar_item_1);
5263 /* Ignore this item if it's not enabled. */
5264 if (NILP (enabled))
5265 return;
5267 /* Find any existing item for this KEY. */
5268 for (i = 0; i < menu_bar_items_index; i += 4)
5269 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
5270 break;
5272 /* If we did not find this KEY, add it at the end. */
5273 if (i == menu_bar_items_index)
5275 /* If vector is too small, get a bigger one. */
5276 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
5278 Lisp_Object tem;
5279 int newsize = 2 * i;
5280 tem = Fmake_vector (make_number (2 * i), Qnil);
5281 bcopy (XVECTOR (menu_bar_items_vector)->contents,
5282 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
5283 menu_bar_items_vector = tem;
5285 /* Add this item. */
5286 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
5287 XVECTOR (menu_bar_items_vector)->contents[i++] = item_string;
5288 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (def, Qnil);
5289 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
5290 menu_bar_items_index = i;
5292 /* We did find an item for this KEY. Add DEF to its list of maps. */
5293 else
5295 Lisp_Object old;
5296 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
5297 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (def, old);
5301 /* Read a character using menus based on maps in the array MAPS.
5302 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
5303 Return t if we displayed a menu but the user rejected it.
5305 PREV_EVENT is the previous input event, or nil if we are reading
5306 the first event of a key sequence.
5308 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
5309 if we used a mouse menu to read the input, or zero otherwise. If
5310 USED_MOUSE_MENU is null, we don't dereference it.
5312 The prompting is done based on the prompt-string of the map
5313 and the strings associated with various map elements.
5315 This can be done with X menus or with menus put in the minibuf.
5316 These are done in different ways, depending on how the input will be read.
5317 Menus using X are done after auto-saving in read-char, getting the input
5318 event from Fx_popup_menu; menus using the minibuf use read_char recursively
5319 and do auto-saving in the inner call of read_char. */
5321 static Lisp_Object
5322 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
5323 int nmaps;
5324 Lisp_Object *maps;
5325 Lisp_Object prev_event;
5326 int *used_mouse_menu;
5328 int mapno;
5329 register Lisp_Object name;
5330 Lisp_Object rest, vector;
5332 if (used_mouse_menu)
5333 *used_mouse_menu = 0;
5335 /* Use local over global Menu maps */
5337 if (! menu_prompting)
5338 return Qnil;
5340 /* Optionally disregard all but the global map. */
5341 if (inhibit_local_menu_bar_menus)
5343 maps += (nmaps - 1);
5344 nmaps = 1;
5347 /* Get the menu name from the first map that has one (a prompt string). */
5348 for (mapno = 0; mapno < nmaps; mapno++)
5350 name = map_prompt (maps[mapno]);
5351 if (!NILP (name))
5352 break;
5355 /* If we don't have any menus, just read a character normally. */
5356 if (mapno >= nmaps)
5357 return Qnil;
5359 #ifdef HAVE_MENUS
5360 /* If we got to this point via a mouse click,
5361 use a real menu for mouse selection. */
5362 if (EVENT_HAS_PARAMETERS (prev_event)
5363 && !EQ (XCONS (prev_event)->car, Qmenu_bar))
5365 /* Display the menu and get the selection. */
5366 Lisp_Object *realmaps
5367 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
5368 Lisp_Object value;
5369 int nmaps1 = 0;
5371 /* Use the maps that are not nil. */
5372 for (mapno = 0; mapno < nmaps; mapno++)
5373 if (!NILP (maps[mapno]))
5374 realmaps[nmaps1++] = maps[mapno];
5376 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
5377 if (CONSP (value))
5379 Lisp_Object tem;
5381 /* If we got multiple events, unread all but
5382 the first.
5383 There is no way to prevent those unread events
5384 from showing up later in last_nonmenu_event.
5385 So turn symbol and integer events into lists,
5386 to indicate that they came from a mouse menu,
5387 so that when present in last_nonmenu_event
5388 they won't confuse things. */
5389 for (tem = XCONS (value)->cdr; !NILP (tem);
5390 tem = XCONS (tem)->cdr)
5391 if (SYMBOLP (XCONS (tem)->car)
5392 || INTEGERP (XCONS (tem)->car))
5393 XCONS (tem)->car
5394 = Fcons (XCONS (tem)->car, Qnil);
5396 /* If we got more than one event, put all but the first
5397 onto this list to be read later.
5398 Return just the first event now. */
5399 Vunread_command_events
5400 = nconc2 (XCONS (value)->cdr, Vunread_command_events);
5401 value = XCONS (value)->car;
5403 else if (NILP (value))
5404 value = Qt;
5405 if (used_mouse_menu)
5406 *used_mouse_menu = 1;
5407 return value;
5409 #endif /* HAVE_MENUS */
5410 return Qnil ;
5413 /* Buffer in use so far for the minibuf prompts for menu keymaps.
5414 We make this bigger when necessary, and never free it. */
5415 static char *read_char_minibuf_menu_text;
5416 /* Size of that buffer. */
5417 static int read_char_minibuf_menu_width;
5419 static Lisp_Object
5420 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
5421 int commandflag ;
5422 int nmaps;
5423 Lisp_Object *maps;
5425 int mapno;
5426 register Lisp_Object name;
5427 int nlength;
5428 int width = FRAME_WIDTH (selected_frame) - 4;
5429 int idx = -1;
5430 int nobindings = 1;
5431 Lisp_Object rest, vector;
5432 char *menu;
5434 if (! menu_prompting)
5435 return Qnil;
5437 /* Make sure we have a big enough buffer for the menu text. */
5438 if (read_char_minibuf_menu_text == 0)
5440 read_char_minibuf_menu_width = width + 4;
5441 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
5443 else if (width + 4 > read_char_minibuf_menu_width)
5445 read_char_minibuf_menu_width = width + 4;
5446 read_char_minibuf_menu_text
5447 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
5449 menu = read_char_minibuf_menu_text;
5451 /* Get the menu name from the first map that has one (a prompt string). */
5452 for (mapno = 0; mapno < nmaps; mapno++)
5454 name = map_prompt (maps[mapno]);
5455 if (!NILP (name))
5456 break;
5459 /* If we don't have any menus, just read a character normally. */
5460 if (mapno >= nmaps)
5461 return Qnil;
5463 /* Prompt string always starts with map's prompt, and a space. */
5464 strcpy (menu, XSTRING (name)->data);
5465 nlength = XSTRING (name)->size;
5466 menu[nlength++] = ':';
5467 menu[nlength++] = ' ';
5468 menu[nlength] = 0;
5470 /* Start prompting at start of first map. */
5471 mapno = 0;
5472 rest = maps[mapno];
5474 /* Present the documented bindings, a line at a time. */
5475 while (1)
5477 int notfirst = 0;
5478 int i = nlength;
5479 Lisp_Object obj;
5480 int ch;
5481 Lisp_Object orig_defn_macro;
5483 /* Loop over elements of map. */
5484 while (i < width)
5486 Lisp_Object s, elt;
5488 /* If reached end of map, start at beginning of next map. */
5489 if (NILP (rest))
5491 mapno++;
5492 /* At end of last map, wrap around to first map if just starting,
5493 or end this line if already have something on it. */
5494 if (mapno == nmaps)
5496 mapno = 0;
5497 if (notfirst || nobindings) break;
5499 rest = maps[mapno];
5502 /* Look at the next element of the map. */
5503 if (idx >= 0)
5504 elt = XVECTOR (vector)->contents[idx];
5505 else
5506 elt = Fcar_safe (rest);
5508 if (idx < 0 && VECTORP (elt))
5510 /* If we found a dense table in the keymap,
5511 advanced past it, but start scanning its contents. */
5512 rest = Fcdr_safe (rest);
5513 vector = elt;
5514 idx = 0;
5516 else
5518 /* An ordinary element. */
5519 Lisp_Object event;
5521 if (idx < 0)
5523 s = Fcar_safe (Fcdr_safe (elt)); /* alist */
5524 event = Fcar_safe (elt);
5526 else
5528 s = Fcar_safe (elt); /* vector */
5529 XSETINT (event, idx);
5532 /* Ignore the element if it has no prompt string. */
5533 if (STRINGP (s) && INTEGERP (event))
5535 /* 1 if the char to type matches the string. */
5536 int char_matches;
5537 Lisp_Object upcased_event, downcased_event;
5538 Lisp_Object desc;
5540 upcased_event = Fupcase (event);
5541 downcased_event = Fdowncase (event);
5542 char_matches = (XINT (upcased_event) == XSTRING (s)->data[0]
5543 || XINT (downcased_event) == XSTRING (s)->data[0]);
5544 if (! char_matches)
5545 desc = Fsingle_key_description (event);
5547 /* If we have room for the prompt string, add it to this line.
5548 If this is the first on the line, always add it. */
5549 if ((XSTRING (s)->size + i + 2
5550 + (char_matches ? 0 : XSTRING (desc)->size + 3))
5551 < width
5552 || !notfirst)
5554 int thiswidth;
5556 /* Punctuate between strings. */
5557 if (notfirst)
5559 strcpy (menu + i, ", ");
5560 i += 2;
5562 notfirst = 1;
5563 nobindings = 0 ;
5565 /* If the char to type doesn't match the string's
5566 first char, explicitly show what char to type. */
5567 if (! char_matches)
5569 /* Add as much of string as fits. */
5570 thiswidth = XSTRING (desc)->size;
5571 if (thiswidth + i > width)
5572 thiswidth = width - i;
5573 bcopy (XSTRING (desc)->data, menu + i, thiswidth);
5574 i += thiswidth;
5575 strcpy (menu + i, " = ");
5576 i += 3;
5579 /* Add as much of string as fits. */
5580 thiswidth = XSTRING (s)->size;
5581 if (thiswidth + i > width)
5582 thiswidth = width - i;
5583 bcopy (XSTRING (s)->data, menu + i, thiswidth);
5584 i += thiswidth;
5585 menu[i] = 0;
5587 else
5589 /* If this element does not fit, end the line now,
5590 and save the element for the next line. */
5591 strcpy (menu + i, "...");
5592 break;
5596 /* Move past this element. */
5597 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
5598 /* Handle reaching end of dense table. */
5599 idx = -1;
5600 if (idx >= 0)
5601 idx++;
5602 else
5603 rest = Fcdr_safe (rest);
5607 /* Prompt with that and read response. */
5608 message1 (menu);
5610 /* Make believe its not a keyboard macro in case the help char
5611 is pressed. Help characters are not recorded because menu prompting
5612 is not used on replay.
5614 orig_defn_macro = current_kboard->defining_kbd_macro;
5615 current_kboard->defining_kbd_macro = Qnil;
5617 obj = read_char (commandflag, 0, 0, Qnil, 0);
5618 while (BUFFERP (obj));
5619 current_kboard->defining_kbd_macro = orig_defn_macro;
5621 if (!INTEGERP (obj))
5622 return obj;
5623 else
5624 ch = XINT (obj);
5626 if (! EQ (obj, menu_prompt_more_char)
5627 && (!INTEGERP (menu_prompt_more_char)
5628 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
5630 if (!NILP (current_kboard->defining_kbd_macro))
5631 store_kbd_macro_char (obj);
5632 return obj;
5634 /* Help char - go round again */
5638 /* Reading key sequences. */
5640 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
5641 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
5642 keymap, or nil otherwise. Return the index of the first keymap in
5643 which KEY has any binding, or NMAPS if no map has a binding.
5645 If KEY is a meta ASCII character, treat it like meta-prefix-char
5646 followed by the corresponding non-meta character. Keymaps in
5647 CURRENT with non-prefix bindings for meta-prefix-char become nil in
5648 NEXT.
5650 If KEY has no bindings in any of the CURRENT maps, NEXT is left
5651 unmodified.
5653 NEXT may be the same array as CURRENT. */
5655 static int
5656 follow_key (key, nmaps, current, defs, next)
5657 Lisp_Object key;
5658 Lisp_Object *current, *defs, *next;
5659 int nmaps;
5661 int i, first_binding;
5662 int did_meta = 0;
5664 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
5665 followed by the corresponding non-meta character.
5666 Put the results into DEFS, since we are going to alter that anyway.
5667 Do not alter CURRENT or NEXT. */
5668 if (INTEGERP (key) && (XINT (key) & CHAR_META))
5670 for (i = 0; i < nmaps; i++)
5671 if (! NILP (current[i]))
5673 Lisp_Object def;
5674 def = get_keyelt (access_keymap (current[i],
5675 meta_prefix_char, 1, 0));
5677 /* Note that since we pass the resulting bindings through
5678 get_keymap_1, non-prefix bindings for meta-prefix-char
5679 disappear. */
5680 defs[i] = get_keymap_1 (def, 0, 1);
5682 else
5683 defs[i] = Qnil;
5685 did_meta = 1;
5686 XSETINT (key, XFASTINT (key) & ~CHAR_META);
5689 first_binding = nmaps;
5690 for (i = nmaps - 1; i >= 0; i--)
5692 if (! NILP (current[i]))
5694 Lisp_Object map;
5695 if (did_meta)
5696 map = defs[i];
5697 else
5698 map = current[i];
5700 defs[i] = get_keyelt (access_keymap (map, key, 1, 0));
5701 if (! NILP (defs[i]))
5702 first_binding = i;
5704 else
5705 defs[i] = Qnil;
5708 /* Given the set of bindings we've found, produce the next set of maps. */
5709 if (first_binding < nmaps)
5710 for (i = 0; i < nmaps; i++)
5711 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
5713 return first_binding;
5716 /* Read a sequence of keys that ends with a non prefix character,
5717 storing it in KEYBUF, a buffer of size BUFSIZE.
5718 Prompt with PROMPT.
5719 Return the length of the key sequence stored.
5720 Return -1 if the user rejected a command menu.
5722 Echo starting immediately unless `prompt' is 0.
5724 Where a key sequence ends depends on the currently active keymaps.
5725 These include any minor mode keymaps active in the current buffer,
5726 the current buffer's local map, and the global map.
5728 If a key sequence has no other bindings, we check Vfunction_key_map
5729 to see if some trailing subsequence might be the beginning of a
5730 function key's sequence. If so, we try to read the whole function
5731 key, and substitute its symbolic name into the key sequence.
5733 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
5734 `double-' events into similar click events, if that would make them
5735 bound. We try to turn `triple-' events first into `double-' events,
5736 then into clicks.
5738 If we get a mouse click in a mode line, vertical divider, or other
5739 non-text area, we treat the click as if it were prefixed by the
5740 symbol denoting that area - `mode-line', `vertical-line', or
5741 whatever.
5743 If the sequence starts with a mouse click, we read the key sequence
5744 with respect to the buffer clicked on, not the current buffer.
5746 If the user switches frames in the midst of a key sequence, we put
5747 off the switch-frame event until later; the next call to
5748 read_char will return it. */
5750 static int
5751 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
5752 can_return_switch_frame)
5753 Lisp_Object *keybuf;
5754 int bufsize;
5755 Lisp_Object prompt;
5756 int dont_downcase_last;
5757 int can_return_switch_frame;
5759 int count = specpdl_ptr - specpdl;
5761 /* How many keys there are in the current key sequence. */
5762 int t;
5764 /* The length of the echo buffer when we started reading, and
5765 the length of this_command_keys when we started reading. */
5766 int echo_start;
5767 int keys_start;
5769 /* The number of keymaps we're scanning right now, and the number of
5770 keymaps we have allocated space for. */
5771 int nmaps;
5772 int nmaps_allocated = 0;
5774 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
5775 the current keymaps. */
5776 Lisp_Object *defs;
5778 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
5779 in the current keymaps, or nil where it is not a prefix. */
5780 Lisp_Object *submaps;
5782 /* The local map to start out with at start of key sequence. */
5783 Lisp_Object orig_local_map;
5785 /* 1 if we have already considered switching to the local-map property
5786 of the place where a mouse click occurred. */
5787 int localized_local_map = 0;
5789 /* The index in defs[] of the first keymap that has a binding for
5790 this key sequence. In other words, the lowest i such that
5791 defs[i] is non-nil. */
5792 int first_binding;
5794 /* If t < mock_input, then KEYBUF[t] should be read as the next
5795 input key.
5797 We use this to recover after recognizing a function key. Once we
5798 realize that a suffix of the current key sequence is actually a
5799 function key's escape sequence, we replace the suffix with the
5800 function key's binding from Vfunction_key_map. Now keybuf
5801 contains a new and different key sequence, so the echo area,
5802 this_command_keys, and the submaps and defs arrays are wrong. In
5803 this situation, we set mock_input to t, set t to 0, and jump to
5804 restart_sequence; the loop will read keys from keybuf up until
5805 mock_input, thus rebuilding the state; and then it will resume
5806 reading characters from the keyboard. */
5807 int mock_input = 0;
5809 /* If the sequence is unbound in submaps[], then
5810 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
5811 and fkey_map is its binding.
5813 These might be > t, indicating that all function key scanning
5814 should hold off until t reaches them. We do this when we've just
5815 recognized a function key, to avoid searching for the function
5816 key's again in Vfunction_key_map. */
5817 int fkey_start = 0, fkey_end = 0;
5818 Lisp_Object fkey_map;
5820 /* Likewise, for key_translation_map. */
5821 int keytran_start = 0, keytran_end = 0;
5822 Lisp_Object keytran_map;
5824 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
5825 we put it off for later. While we're reading, we keep the event here. */
5826 Lisp_Object delayed_switch_frame;
5828 /* See the comment below... */
5829 #if defined (GOBBLE_FIRST_EVENT)
5830 Lisp_Object first_event;
5831 #endif
5833 Lisp_Object original_uppercase;
5834 int original_uppercase_position = -1;
5836 /* Gets around Microsoft compiler limitations. */
5837 int dummyflag = 0;
5839 struct buffer *starting_buffer;
5841 /* Nonzero if we seem to have got the beginning of a binding
5842 in function_key_map. */
5843 int function_key_possible = 0;
5844 int key_translation_possible = 0;
5846 /* Save the status of key translation before each step,
5847 so that we can restore this after downcasing. */
5848 Lisp_Object prev_fkey_map;
5849 Lisp_Object prev_fkey_start;
5850 Lisp_Object prev_fkey_end;
5852 Lisp_Object prev_keytran_map;
5853 Lisp_Object prev_keytran_start;
5854 Lisp_Object prev_keytran_end;
5856 int junk;
5858 last_nonmenu_event = Qnil;
5860 delayed_switch_frame = Qnil;
5861 fkey_map = Vfunction_key_map;
5862 keytran_map = Vkey_translation_map;
5864 /* If there is no function-key-map, turn off function key scanning. */
5865 if (NILP (Fkeymapp (Vfunction_key_map)))
5866 fkey_start = fkey_end = bufsize + 1;
5868 /* If there is no key-translation-map, turn off scanning. */
5869 if (NILP (Fkeymapp (Vkey_translation_map)))
5870 keytran_start = keytran_end = bufsize + 1;
5872 if (INTERACTIVE)
5874 if (!NILP (prompt))
5875 echo_prompt (XSTRING (prompt)->data);
5876 else if (cursor_in_echo_area && echo_keystrokes)
5877 /* This doesn't put in a dash if the echo buffer is empty, so
5878 you don't always see a dash hanging out in the minibuffer. */
5879 echo_dash ();
5882 /* Record the initial state of the echo area and this_command_keys;
5883 we will need to restore them if we replay a key sequence. */
5884 if (INTERACTIVE)
5885 echo_start = echo_length ();
5886 keys_start = this_command_key_count;
5887 this_single_command_key_start = keys_start;
5889 #if defined (GOBBLE_FIRST_EVENT)
5890 /* This doesn't quite work, because some of the things that read_char
5891 does cannot safely be bypassed. It seems too risky to try to make
5892 this work right. */
5894 /* Read the first char of the sequence specially, before setting
5895 up any keymaps, in case a filter runs and switches buffers on us. */
5896 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
5897 &junk);
5898 #endif /* GOBBLE_FIRST_EVENT */
5900 orig_local_map = get_local_map (PT, current_buffer);
5902 /* We jump here when the key sequence has been thoroughly changed, and
5903 we need to rescan it starting from the beginning. When we jump here,
5904 keybuf[0..mock_input] holds the sequence we should reread. */
5905 replay_sequence:
5907 starting_buffer = current_buffer;
5908 function_key_possible = 0;
5909 key_translation_possible = 0;
5911 /* Build our list of keymaps.
5912 If we recognize a function key and replace its escape sequence in
5913 keybuf with its symbol, or if the sequence starts with a mouse
5914 click and we need to switch buffers, we jump back here to rebuild
5915 the initial keymaps from the current buffer. */
5917 Lisp_Object *maps;
5919 if (!NILP (current_kboard->Voverriding_terminal_local_map)
5920 || !NILP (Voverriding_local_map))
5922 if (3 > nmaps_allocated)
5924 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
5925 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
5926 nmaps_allocated = 3;
5928 nmaps = 0;
5929 if (!NILP (current_kboard->Voverriding_terminal_local_map))
5930 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
5931 if (!NILP (Voverriding_local_map))
5932 submaps[nmaps++] = Voverriding_local_map;
5934 else
5936 nmaps = current_minor_maps (0, &maps);
5937 if (nmaps + 2 > nmaps_allocated)
5939 submaps = (Lisp_Object *) alloca ((nmaps+2) * sizeof (submaps[0]));
5940 defs = (Lisp_Object *) alloca ((nmaps+2) * sizeof (defs[0]));
5941 nmaps_allocated = nmaps + 2;
5943 bcopy (maps, submaps, nmaps * sizeof (submaps[0]));
5944 #ifdef USE_TEXT_PROPERTIES
5945 submaps[nmaps++] = orig_local_map;
5946 #else
5947 submaps[nmaps++] = current_buffer->keymap;
5948 #endif
5950 submaps[nmaps++] = current_global_map;
5953 /* Find an accurate initial value for first_binding. */
5954 for (first_binding = 0; first_binding < nmaps; first_binding++)
5955 if (! NILP (submaps[first_binding]))
5956 break;
5958 /* Start from the beginning in keybuf. */
5959 t = 0;
5961 /* These are no-ops the first time through, but if we restart, they
5962 revert the echo area and this_command_keys to their original state. */
5963 this_command_key_count = keys_start;
5964 if (INTERACTIVE && t < mock_input)
5965 echo_truncate (echo_start);
5967 /* If the best binding for the current key sequence is a keymap, or
5968 we may be looking at a function key's escape sequence, keep on
5969 reading. */
5970 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
5971 || (first_binding >= nmaps
5972 && fkey_start < t
5973 /* mock input is never part of a function key's sequence. */
5974 && mock_input <= fkey_start)
5975 || (first_binding >= nmaps
5976 && keytran_start < t && key_translation_possible)
5977 /* Don't return in the middle of a possible function key sequence,
5978 if the only bindings we found were via case conversion.
5979 Thus, if ESC O a has a function-key-map translation
5980 and ESC o has a binding, don't return after ESC O,
5981 so that we can translate ESC O plus the next character. */
5984 Lisp_Object key;
5985 int used_mouse_menu = 0;
5987 /* Where the last real key started. If we need to throw away a
5988 key that has expanded into more than one element of keybuf
5989 (say, a mouse click on the mode line which is being treated
5990 as [mode-line (mouse-...)], then we backtrack to this point
5991 of keybuf. */
5992 int last_real_key_start;
5994 /* These variables are analogous to echo_start and keys_start;
5995 while those allow us to restart the entire key sequence,
5996 echo_local_start and keys_local_start allow us to throw away
5997 just one key. */
5998 int echo_local_start, keys_local_start, local_first_binding;
6000 if (t >= bufsize)
6001 error ("Key sequence too long");
6003 if (INTERACTIVE)
6004 echo_local_start = echo_length ();
6005 keys_local_start = this_command_key_count;
6006 local_first_binding = first_binding;
6008 replay_key:
6009 /* These are no-ops, unless we throw away a keystroke below and
6010 jumped back up to replay_key; in that case, these restore the
6011 variables to their original state, allowing us to replay the
6012 loop. */
6013 if (INTERACTIVE && t < mock_input)
6014 echo_truncate (echo_local_start);
6015 this_command_key_count = keys_local_start;
6016 first_binding = local_first_binding;
6018 /* By default, assume each event is "real". */
6019 last_real_key_start = t;
6021 /* Does mock_input indicate that we are re-reading a key sequence? */
6022 if (t < mock_input)
6024 key = keybuf[t];
6025 add_command_key (key);
6026 if (echo_keystrokes)
6027 echo_char (key);
6030 /* If not, we should actually read a character. */
6031 else
6033 struct buffer *buf = current_buffer;
6036 #ifdef MULTI_KBOARD
6037 KBOARD *interrupted_kboard = current_kboard;
6038 struct frame *interrupted_frame = selected_frame;
6039 if (setjmp (wrong_kboard_jmpbuf))
6041 if (!NILP (delayed_switch_frame))
6043 interrupted_kboard->kbd_queue
6044 = Fcons (delayed_switch_frame,
6045 interrupted_kboard->kbd_queue);
6046 delayed_switch_frame = Qnil;
6048 while (t > 0)
6049 interrupted_kboard->kbd_queue
6050 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
6052 /* If the side queue is non-empty, ensure it begins with a
6053 switch-frame, so we'll replay it in the right context. */
6054 if (CONSP (interrupted_kboard->kbd_queue)
6055 && (key = XCONS (interrupted_kboard->kbd_queue)->car,
6056 !(EVENT_HAS_PARAMETERS (key)
6057 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
6058 Qswitch_frame))))
6060 Lisp_Object frame;
6061 XSETFRAME (frame, interrupted_frame);
6062 interrupted_kboard->kbd_queue
6063 = Fcons (make_lispy_switch_frame (frame),
6064 interrupted_kboard->kbd_queue);
6066 mock_input = 0;
6067 orig_local_map = get_local_map (PT, current_buffer);
6068 goto replay_sequence;
6070 #endif
6071 key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
6072 &used_mouse_menu);
6075 /* read_char returns t when it shows a menu and the user rejects it.
6076 Just return -1. */
6077 if (EQ (key, Qt))
6078 return -1;
6080 /* read_char returns -1 at the end of a macro.
6081 Emacs 18 handles this by returning immediately with a
6082 zero, so that's what we'll do. */
6083 if (INTEGERP (key) && XINT (key) == -1)
6085 t = 0;
6086 /* The Microsoft C compiler can't handle the goto that
6087 would go here. */
6088 dummyflag = 1;
6089 break;
6092 /* If the current buffer has been changed from under us, the
6093 keymap may have changed, so replay the sequence. */
6094 if (BUFFERP (key))
6096 mock_input = t;
6097 orig_local_map = get_local_map (PT, current_buffer);
6098 goto replay_sequence;
6101 /* If we have a quit that was typed in another frame, and
6102 quit_throw_to_read_char switched buffers,
6103 replay to get the right keymap. */
6104 if (XINT (key) == quit_char && current_buffer != starting_buffer)
6106 keybuf[t++] = key;
6107 mock_input = t;
6108 Vquit_flag = Qnil;
6109 orig_local_map = get_local_map (PT, current_buffer);
6110 goto replay_sequence;
6113 Vquit_flag = Qnil;
6116 /* Clicks in non-text areas get prefixed by the symbol
6117 in their CHAR-ADDRESS field. For example, a click on
6118 the mode line is prefixed by the symbol `mode-line'.
6120 Furthermore, key sequences beginning with mouse clicks
6121 are read using the keymaps of the buffer clicked on, not
6122 the current buffer. So we may have to switch the buffer
6123 here.
6125 When we turn one event into two events, we must make sure
6126 that neither of the two looks like the original--so that,
6127 if we replay the events, they won't be expanded again.
6128 If not for this, such reexpansion could happen either here
6129 or when user programs play with this-command-keys. */
6130 if (EVENT_HAS_PARAMETERS (key))
6132 Lisp_Object kind;
6134 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
6135 if (EQ (kind, Qmouse_click))
6137 Lisp_Object window, posn;
6139 window = POSN_WINDOW (EVENT_START (key));
6140 posn = POSN_BUFFER_POSN (EVENT_START (key));
6141 if (CONSP (posn))
6143 /* We're looking at the second event of a
6144 sequence which we expanded before. Set
6145 last_real_key_start appropriately. */
6146 if (t > 0)
6147 last_real_key_start = t - 1;
6150 /* Key sequences beginning with mouse clicks are
6151 read using the keymaps in the buffer clicked on,
6152 not the current buffer. If we're at the
6153 beginning of a key sequence, switch buffers. */
6154 if (last_real_key_start == 0
6155 && WINDOWP (window)
6156 && BUFFERP (XWINDOW (window)->buffer)
6157 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
6159 keybuf[t] = key;
6160 mock_input = t + 1;
6162 /* Arrange to go back to the original buffer once we're
6163 done reading the key sequence. Note that we can't
6164 use save_excursion_{save,restore} here, because they
6165 save point as well as the current buffer; we don't
6166 want to save point, because redisplay may change it,
6167 to accommodate a Fset_window_start or something. We
6168 don't want to do this at the top of the function,
6169 because we may get input from a subprocess which
6170 wants to change the selected window and stuff (say,
6171 emacsclient). */
6172 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
6174 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
6175 orig_local_map = get_local_map (PT, current_buffer);
6176 goto replay_sequence;
6178 /* For a mouse click, get the local text-property keymap
6179 of the place clicked on, rather than point. */
6180 if (last_real_key_start == 0 && CONSP (XCONS (key)->cdr)
6181 && ! localized_local_map)
6183 Lisp_Object map_here, start, pos;
6185 localized_local_map = 1;
6186 start = EVENT_START (key);
6187 if (CONSP (start) && CONSP (XCONS (start)->cdr))
6189 pos = POSN_BUFFER_POSN (start);
6190 if (INTEGERP (pos)
6191 && XINT (pos) >= BEG && XINT (pos) <= Z)
6193 map_here = get_local_map (XINT (pos), current_buffer);
6194 if (!EQ (map_here, orig_local_map))
6196 orig_local_map = map_here;
6197 keybuf[t] = key;
6198 mock_input = t + 1;
6200 goto replay_sequence;
6206 /* Expand mode-line and scroll-bar events into two events:
6207 use posn as a fake prefix key. */
6208 if (SYMBOLP (posn))
6210 if (t + 1 >= bufsize)
6211 error ("Key sequence too long");
6212 keybuf[t] = posn;
6213 keybuf[t+1] = key;
6214 mock_input = t + 2;
6216 /* Zap the position in key, so we know that we've
6217 expanded it, and don't try to do so again. */
6218 POSN_BUFFER_POSN (EVENT_START (key))
6219 = Fcons (posn, Qnil);
6220 goto replay_key;
6223 else if (EQ (kind, Qswitch_frame))
6225 /* If we're at the beginning of a key sequence, and the caller
6226 says it's okay, go ahead and return this event. If we're
6227 in the midst of a key sequence, delay it until the end. */
6228 if (t > 0 || !can_return_switch_frame)
6230 delayed_switch_frame = key;
6231 goto replay_key;
6234 else if (CONSP (XCONS (key)->cdr)
6235 && CONSP (EVENT_START (key))
6236 && CONSP (XCONS (EVENT_START (key))->cdr))
6238 Lisp_Object posn;
6240 posn = POSN_BUFFER_POSN (EVENT_START (key));
6241 /* Handle menu-bar events:
6242 insert the dummy prefix event `menu-bar'. */
6243 if (EQ (posn, Qmenu_bar))
6245 if (t + 1 >= bufsize)
6246 error ("Key sequence too long");
6247 keybuf[t] = posn;
6248 keybuf[t+1] = key;
6250 /* Zap the position in key, so we know that we've
6251 expanded it, and don't try to do so again. */
6252 POSN_BUFFER_POSN (EVENT_START (key))
6253 = Fcons (posn, Qnil);
6255 mock_input = t + 2;
6256 goto replay_sequence;
6258 else if (CONSP (posn))
6260 /* We're looking at the second event of a
6261 sequence which we expanded before. Set
6262 last_real_key_start appropriately. */
6263 if (last_real_key_start == t && t > 0)
6264 last_real_key_start = t - 1;
6269 /* We have finally decided that KEY is something we might want
6270 to look up. */
6271 first_binding = (follow_key (key,
6272 nmaps - first_binding,
6273 submaps + first_binding,
6274 defs + first_binding,
6275 submaps + first_binding)
6276 + first_binding);
6278 /* If KEY wasn't bound, we'll try some fallbacks. */
6279 if (first_binding >= nmaps)
6281 Lisp_Object head;
6283 head = EVENT_HEAD (key);
6284 if (help_char_p (head) && t > 0)
6286 read_key_sequence_cmd = Vprefix_help_command;
6287 keybuf[t++] = key;
6288 last_nonmenu_event = key;
6289 /* The Microsoft C compiler can't handle the goto that
6290 would go here. */
6291 dummyflag = 1;
6292 break;
6295 if (SYMBOLP (head))
6297 Lisp_Object breakdown;
6298 int modifiers;
6300 breakdown = parse_modifiers (head);
6301 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
6302 /* Attempt to reduce an unbound mouse event to a simpler
6303 event that is bound:
6304 Drags reduce to clicks.
6305 Double-clicks reduce to clicks.
6306 Triple-clicks reduce to double-clicks, then to clicks.
6307 Down-clicks are eliminated.
6308 Double-downs reduce to downs, then are eliminated.
6309 Triple-downs reduce to double-downs, then to downs,
6310 then are eliminated. */
6311 if (modifiers & (down_modifier | drag_modifier
6312 | double_modifier | triple_modifier))
6314 while (modifiers & (down_modifier | drag_modifier
6315 | double_modifier | triple_modifier))
6317 Lisp_Object new_head, new_click;
6318 if (modifiers & triple_modifier)
6319 modifiers ^= (double_modifier | triple_modifier);
6320 else if (modifiers & double_modifier)
6321 modifiers &= ~double_modifier;
6322 else if (modifiers & drag_modifier)
6323 modifiers &= ~drag_modifier;
6324 else
6326 /* Dispose of this `down' event by simply jumping
6327 back to replay_key, to get another event.
6329 Note that if this event came from mock input,
6330 then just jumping back to replay_key will just
6331 hand it to us again. So we have to wipe out any
6332 mock input.
6334 We could delete keybuf[t] and shift everything
6335 after that to the left by one spot, but we'd also
6336 have to fix up any variable that points into
6337 keybuf, and shifting isn't really necessary
6338 anyway.
6340 Adding prefixes for non-textual mouse clicks
6341 creates two characters of mock input, and both
6342 must be thrown away. If we're only looking at
6343 the prefix now, we can just jump back to
6344 replay_key. On the other hand, if we've already
6345 processed the prefix, and now the actual click
6346 itself is giving us trouble, then we've lost the
6347 state of the keymaps we want to backtrack to, and
6348 we need to replay the whole sequence to rebuild
6351 Beyond that, only function key expansion could
6352 create more than two keys, but that should never
6353 generate mouse events, so it's okay to zero
6354 mock_input in that case too.
6356 Isn't this just the most wonderful code ever? */
6357 if (t == last_real_key_start)
6359 mock_input = 0;
6360 goto replay_key;
6362 else
6364 mock_input = last_real_key_start;
6365 goto replay_sequence;
6369 new_head
6370 = apply_modifiers (modifiers, XCONS (breakdown)->car);
6371 new_click
6372 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
6374 /* Look for a binding for this new key. follow_key
6375 promises that it didn't munge submaps the
6376 last time we called it, since key was unbound. */
6377 first_binding
6378 = (follow_key (new_click,
6379 nmaps - local_first_binding,
6380 submaps + local_first_binding,
6381 defs + local_first_binding,
6382 submaps + local_first_binding)
6383 + local_first_binding);
6385 /* If that click is bound, go for it. */
6386 if (first_binding < nmaps)
6388 key = new_click;
6389 break;
6391 /* Otherwise, we'll leave key set to the drag event. */
6397 keybuf[t++] = key;
6398 /* Normally, last_nonmenu_event gets the previous key we read.
6399 But when a mouse popup menu is being used,
6400 we don't update last_nonmenu_event; it continues to hold the mouse
6401 event that preceded the first level of menu. */
6402 if (!used_mouse_menu)
6403 last_nonmenu_event = key;
6405 /* Record what part of this_command_keys is the current key sequence. */
6406 this_single_command_key_start = this_command_key_count - t;
6408 prev_fkey_map = fkey_map;
6409 prev_fkey_start = fkey_start;
6410 prev_fkey_end = fkey_end;
6412 prev_keytran_map = keytran_map;
6413 prev_keytran_start = keytran_start;
6414 prev_keytran_end = keytran_end;
6416 /* If the sequence is unbound, see if we can hang a function key
6417 off the end of it. We only want to scan real keyboard input
6418 for function key sequences, so if mock_input says that we're
6419 re-reading old events, don't examine it. */
6420 if (first_binding >= nmaps
6421 && t >= mock_input)
6423 Lisp_Object fkey_next;
6425 /* Continue scan from fkey_end until we find a bound suffix.
6426 If we fail, increment fkey_start
6427 and start fkey_end from there. */
6428 while (fkey_end < t)
6430 Lisp_Object key;
6432 key = keybuf[fkey_end++];
6433 /* Look up meta-characters by prefixing them
6434 with meta_prefix_char. I hate this. */
6435 if (INTEGERP (key) && XINT (key) & meta_modifier)
6437 fkey_next
6438 = get_keymap_1
6439 (get_keyelt
6440 (access_keymap (fkey_map, meta_prefix_char, 1, 0)),
6441 0, 1);
6442 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
6444 else
6445 fkey_next = fkey_map;
6447 fkey_next
6448 = get_keyelt (access_keymap (fkey_next, key, 1, 0));
6450 #if 0 /* I didn't turn this on, because it might cause trouble
6451 for the mapping of return into C-m and tab into C-i. */
6452 /* Optionally don't map function keys into other things.
6453 This enables the user to redefine kp- keys easily. */
6454 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
6455 fkey_next = Qnil;
6456 #endif
6458 /* If the function key map gives a function, not an
6459 array, then call the function with no args and use
6460 its value instead. */
6461 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
6462 && fkey_end == t)
6464 struct gcpro gcpro1, gcpro2, gcpro3;
6465 Lisp_Object tem;
6466 tem = fkey_next;
6468 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
6469 fkey_next = call1 (fkey_next, prompt);
6470 UNGCPRO;
6471 /* If the function returned something invalid,
6472 barf--don't ignore it.
6473 (To ignore it safely, we would need to gcpro a bunch of
6474 other variables.) */
6475 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
6476 error ("Function in key-translation-map returns invalid key sequence");
6479 function_key_possible = ! NILP (fkey_next);
6481 /* If keybuf[fkey_start..fkey_end] is bound in the
6482 function key map and it's a suffix of the current
6483 sequence (i.e. fkey_end == t), replace it with
6484 the binding and restart with fkey_start at the end. */
6485 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
6486 && fkey_end == t)
6488 int len = XFASTINT (Flength (fkey_next));
6490 t = fkey_start + len;
6491 if (t >= bufsize)
6492 error ("Key sequence too long");
6494 if (VECTORP (fkey_next))
6495 bcopy (XVECTOR (fkey_next)->contents,
6496 keybuf + fkey_start,
6497 (t - fkey_start) * sizeof (keybuf[0]));
6498 else if (STRINGP (fkey_next))
6500 int i;
6502 for (i = 0; i < len; i++)
6503 XSETFASTINT (keybuf[fkey_start + i],
6504 XSTRING (fkey_next)->data[i]);
6507 mock_input = t;
6508 fkey_start = fkey_end = t;
6509 fkey_map = Vfunction_key_map;
6511 /* Do pass the results through key-translation-map. */
6512 keytran_start = keytran_end = 0;
6513 keytran_map = Vkey_translation_map;
6515 goto replay_sequence;
6518 fkey_map = get_keymap_1 (fkey_next, 0, 1);
6520 /* If we no longer have a bound suffix, try a new positions for
6521 fkey_start. */
6522 if (NILP (fkey_map))
6524 fkey_end = ++fkey_start;
6525 fkey_map = Vfunction_key_map;
6526 function_key_possible = 0;
6531 /* Look for this sequence in key-translation-map. */
6533 Lisp_Object keytran_next;
6535 /* Scan from keytran_end until we find a bound suffix. */
6536 while (keytran_end < t)
6538 Lisp_Object key;
6540 key = keybuf[keytran_end++];
6541 /* Look up meta-characters by prefixing them
6542 with meta_prefix_char. I hate this. */
6543 if (INTEGERP (key) && XINT (key) & meta_modifier)
6545 keytran_next
6546 = get_keymap_1
6547 (get_keyelt
6548 (access_keymap (keytran_map, meta_prefix_char, 1, 0)),
6549 0, 1);
6550 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
6552 else
6553 keytran_next = keytran_map;
6555 keytran_next
6556 = get_keyelt (access_keymap (keytran_next, key, 1, 0));
6558 /* If the key translation map gives a function, not an
6559 array, then call the function with no args and use
6560 its value instead. */
6561 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
6562 && keytran_end == t)
6564 struct gcpro gcpro1, gcpro2, gcpro3;
6565 Lisp_Object tem;
6566 tem = keytran_next;
6568 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
6569 keytran_next = call1 (keytran_next, prompt);
6570 UNGCPRO;
6571 /* If the function returned something invalid,
6572 barf--don't ignore it.
6573 (To ignore it safely, we would need to gcpro a bunch of
6574 other variables.) */
6575 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
6576 error ("Function in key-translation-map returns invalid key sequence");
6579 key_translation_possible = ! NILP (keytran_next);
6581 /* If keybuf[keytran_start..keytran_end] is bound in the
6582 key translation map and it's a suffix of the current
6583 sequence (i.e. keytran_end == t), replace it with
6584 the binding and restart with keytran_start at the end. */
6585 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
6586 && keytran_end == t)
6588 int len = XFASTINT (Flength (keytran_next));
6590 t = keytran_start + len;
6591 if (t >= bufsize)
6592 error ("Key sequence too long");
6594 if (VECTORP (keytran_next))
6595 bcopy (XVECTOR (keytran_next)->contents,
6596 keybuf + keytran_start,
6597 (t - keytran_start) * sizeof (keybuf[0]));
6598 else if (STRINGP (keytran_next))
6600 int i;
6602 for (i = 0; i < len; i++)
6603 XSETFASTINT (keybuf[keytran_start + i],
6604 XSTRING (keytran_next)->data[i]);
6607 mock_input = t;
6608 keytran_start = keytran_end = t;
6609 keytran_map = Vkey_translation_map;
6611 /* Don't pass the results of key-translation-map
6612 through function-key-map. */
6613 fkey_start = fkey_end = t;
6614 fkey_map = Vkey_translation_map;
6616 goto replay_sequence;
6619 keytran_map = get_keymap_1 (keytran_next, 0, 1);
6621 /* If we no longer have a bound suffix, try a new positions for
6622 keytran_start. */
6623 if (NILP (keytran_map))
6625 keytran_end = ++keytran_start;
6626 keytran_map = Vkey_translation_map;
6627 key_translation_possible = 0;
6632 /* If KEY is not defined in any of the keymaps,
6633 and cannot be part of a function key or translation,
6634 and is an upper case letter
6635 use the corresponding lower-case letter instead. */
6636 if (first_binding == nmaps && ! function_key_possible
6637 && ! key_translation_possible
6638 && INTEGERP (key)
6639 && ((((XINT (key) & 0x3ffff)
6640 < XSTRING (current_buffer->downcase_table)->size)
6641 && UPPERCASEP (XINT (key) & 0x3ffff))
6642 || (XINT (key) & shift_modifier)))
6644 Lisp_Object new_key;
6646 original_uppercase = key;
6647 original_uppercase_position = t - 1;
6649 if (XINT (key) & shift_modifier)
6650 XSETINT (new_key, XINT (key) & ~shift_modifier);
6651 else
6652 XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
6653 | (XINT (key) & ~0x3ffff)));
6655 /* We have to do this unconditionally, regardless of whether
6656 the lower-case char is defined in the keymaps, because they
6657 might get translated through function-key-map. */
6658 keybuf[t - 1] = new_key;
6659 mock_input = t;
6661 fkey_map = prev_fkey_map;
6662 fkey_start = prev_fkey_start;
6663 fkey_end = prev_fkey_end;
6665 keytran_map = prev_keytran_map;
6666 keytran_start = prev_keytran_start;
6667 keytran_end = prev_keytran_end;
6669 goto replay_sequence;
6671 /* If KEY is not defined in any of the keymaps,
6672 and cannot be part of a function key or translation,
6673 and is a shifted function key,
6674 use the corresponding unshifted function key instead. */
6675 if (first_binding == nmaps && ! function_key_possible
6676 && ! key_translation_possible
6677 && SYMBOLP (key))
6679 Lisp_Object breakdown;
6680 int modifiers;
6682 breakdown = parse_modifiers (key);
6683 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
6684 if (modifiers & shift_modifier)
6686 Lisp_Object new_key;
6688 original_uppercase = key;
6689 original_uppercase_position = t - 1;
6691 modifiers &= ~shift_modifier;
6692 new_key = apply_modifiers (modifiers,
6693 XCONS (breakdown)->car);
6695 keybuf[t - 1] = new_key;
6696 mock_input = t;
6698 fkey_map = prev_fkey_map;
6699 fkey_start = prev_fkey_start;
6700 fkey_end = prev_fkey_end;
6702 keytran_map = prev_keytran_map;
6703 keytran_start = prev_keytran_start;
6704 keytran_end = prev_keytran_end;
6706 goto replay_sequence;
6711 if (!dummyflag)
6712 read_key_sequence_cmd = (first_binding < nmaps
6713 ? defs[first_binding]
6714 : Qnil);
6716 unread_switch_frame = delayed_switch_frame;
6717 unbind_to (count, Qnil);
6719 /* Don't downcase the last character if the caller says don't.
6720 Don't downcase it if the result is undefined, either. */
6721 if ((dont_downcase_last || first_binding >= nmaps)
6722 && t - 1 == original_uppercase_position)
6723 keybuf[t - 1] = original_uppercase;
6725 /* Occasionally we fabricate events, perhaps by expanding something
6726 according to function-key-map, or by adding a prefix symbol to a
6727 mouse click in the scroll bar or modeline. In this cases, return
6728 the entire generated key sequence, even if we hit an unbound
6729 prefix or a definition before the end. This means that you will
6730 be able to push back the event properly, and also means that
6731 read-key-sequence will always return a logical unit.
6733 Better ideas? */
6734 for (; t < mock_input; t++)
6736 if (echo_keystrokes)
6737 echo_char (keybuf[t]);
6738 add_command_key (keybuf[t]);
6741 return t;
6744 #if 0 /* This doc string is too long for some compilers.
6745 This commented-out definition serves for DOC. */
6746 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
6747 "Read a sequence of keystrokes and return as a string or vector.\n\
6748 The sequence is sufficient to specify a non-prefix command in the\n\
6749 current local and global maps.\n\
6751 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
6752 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
6753 as a continuation of the previous key.\n\
6755 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
6756 convert the last event to lower case. (Normally any upper case event\n\
6757 is converted to lower case if the original event is undefined and the lower\n\
6758 case equivalent is defined.) A non-nil value is appropriate for reading\n\
6759 a key sequence to be defined.\n\
6761 A C-g typed while in this function is treated like any other character,\n\
6762 and `quit-flag' is not set.\n\
6764 If the key sequence starts with a mouse click, then the sequence is read\n\
6765 using the keymaps of the buffer of the window clicked in, not the buffer\n\
6766 of the selected window as normal.\n\
6767 ""\n\
6768 `read-key-sequence' drops unbound button-down events, since you normally\n\
6769 only care about the click or drag events which follow them. If a drag\n\
6770 or multi-click event is unbound, but the corresponding click event would\n\
6771 be bound, `read-key-sequence' turns the event into a click event at the\n\
6772 drag's starting position. This means that you don't have to distinguish\n\
6773 between click and drag, double, or triple events unless you want to.\n\
6775 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
6776 lines separating windows, and scroll bars with imaginary keys\n\
6777 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
6779 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
6780 function will process a switch-frame event if the user switches frames\n\
6781 before typing anything. If the user switches frames in the middle of a\n\
6782 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
6783 is nil, then the event will be put off until after the current key sequence.\n\
6785 `read-key-sequence' checks `function-key-map' for function key\n\
6786 sequences, where they wouldn't conflict with ordinary bindings. See\n\
6787 `function-key-map' for more details.")
6788 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
6789 #endif
6791 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
6793 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
6794 Lisp_Object prompt, continue_echo, dont_downcase_last;
6795 Lisp_Object can_return_switch_frame;
6797 Lisp_Object keybuf[30];
6798 register int i;
6799 struct gcpro gcpro1, gcpro2;
6801 if (!NILP (prompt))
6802 CHECK_STRING (prompt, 0);
6803 QUIT;
6805 bzero (keybuf, sizeof keybuf);
6806 GCPRO1 (keybuf[0]);
6807 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
6809 if (NILP (continue_echo))
6811 this_command_key_count = 0;
6812 this_single_command_key_start = 0;
6815 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
6816 prompt, ! NILP (dont_downcase_last),
6817 ! NILP (can_return_switch_frame));
6819 if (i == -1)
6821 Vquit_flag = Qt;
6822 QUIT;
6824 UNGCPRO;
6825 return make_event_array (i, keybuf);
6828 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
6829 "Execute CMD as an editor command.\n\
6830 CMD must be a symbol that satisfies the `commandp' predicate.\n\
6831 Optional second arg RECORD-FLAG non-nil\n\
6832 means unconditionally put this command in `command-history'.\n\
6833 Otherwise, that is done only if an arg is read using the minibuffer.\n\
6834 The argument KEYS specifies the value to use instead of (this-command-keys)\n\
6835 when reading the arguments; if it is nil, (this-command-keys) is used.\n\
6836 The argument SPECIAL, if non-nil, means that this command is executing\n\
6837 a special event, so ignore the prefix argument and don't clear it.")
6838 (cmd, record_flag, keys, special)
6839 Lisp_Object cmd, record_flag, keys, special;
6841 register Lisp_Object final;
6842 register Lisp_Object tem;
6843 Lisp_Object prefixarg;
6844 struct backtrace backtrace;
6845 extern int debug_on_next_call;
6847 debug_on_next_call = 0;
6849 if (NILP (special))
6851 prefixarg = current_kboard->Vprefix_arg;
6852 Vcurrent_prefix_arg = prefixarg;
6853 current_kboard->Vprefix_arg = Qnil;
6855 else
6856 prefixarg = Qnil;
6858 if (SYMBOLP (cmd))
6860 tem = Fget (cmd, Qdisabled);
6861 if (!NILP (tem) && !NILP (Vrun_hooks))
6863 tem = Fsymbol_value (Qdisabled_command_hook);
6864 if (!NILP (tem))
6865 return call1 (Vrun_hooks, Qdisabled_command_hook);
6869 while (1)
6871 final = Findirect_function (cmd);
6873 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
6874 do_autoload (final, cmd);
6875 else
6876 break;
6879 if (STRINGP (final) || VECTORP (final))
6881 /* If requested, place the macro in the command history. For
6882 other sorts of commands, call-interactively takes care of
6883 this. */
6884 if (!NILP (record_flag))
6885 Vcommand_history
6886 = Fcons (Fcons (Qexecute_kbd_macro,
6887 Fcons (final, Fcons (prefixarg, Qnil))),
6888 Vcommand_history);
6890 return Fexecute_kbd_macro (final, prefixarg);
6892 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
6894 backtrace.next = backtrace_list;
6895 backtrace_list = &backtrace;
6896 backtrace.function = &Qcall_interactively;
6897 backtrace.args = &cmd;
6898 backtrace.nargs = 1;
6899 backtrace.evalargs = 0;
6901 tem = Fcall_interactively (cmd, record_flag, keys);
6903 backtrace_list = backtrace.next;
6904 return tem;
6906 return Qnil;
6909 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
6910 1, 1, "P",
6911 "Read function name, then read its arguments and call it.")
6912 (prefixarg)
6913 Lisp_Object prefixarg;
6915 Lisp_Object function;
6916 char buf[40];
6917 Lisp_Object saved_keys;
6918 struct gcpro gcpro1;
6920 saved_keys = Fvector (this_command_key_count,
6921 XVECTOR (this_command_keys)->contents);
6922 buf[0] = 0;
6923 GCPRO1 (saved_keys);
6925 if (EQ (prefixarg, Qminus))
6926 strcpy (buf, "- ");
6927 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
6928 strcpy (buf, "C-u ");
6929 else if (CONSP (prefixarg) && INTEGERP (XCONS (prefixarg)->car))
6931 if (sizeof (int) == sizeof (EMACS_INT))
6932 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
6933 else if (sizeof (long) == sizeof (EMACS_INT))
6934 sprintf (buf, "%ld ", XINT (XCONS (prefixarg)->car));
6935 else
6936 abort ();
6938 else if (INTEGERP (prefixarg))
6940 if (sizeof (int) == sizeof (EMACS_INT))
6941 sprintf (buf, "%d ", XINT (prefixarg));
6942 else if (sizeof (long) == sizeof (EMACS_INT))
6943 sprintf (buf, "%ld ", XINT (prefixarg));
6944 else
6945 abort ();
6948 /* This isn't strictly correct if execute-extended-command
6949 is bound to anything else. Perhaps it should use
6950 this_command_keys? */
6951 strcat (buf, "M-x ");
6953 /* Prompt with buf, and then read a string, completing from and
6954 restricting to the set of all defined commands. Don't provide
6955 any initial input. Save the command read on the extended-command
6956 history list. */
6957 function = Fcompleting_read (build_string (buf),
6958 Vobarray, Qcommandp,
6959 Qt, Qnil, Qextended_command_history);
6961 if (STRINGP (function) && XSTRING (function)->size == 0)
6962 error ("No command name given");
6964 /* Set this_command_keys to the concatenation of saved_keys and
6965 function, followed by a RET. */
6967 struct Lisp_String *str;
6968 Lisp_Object *keys;
6969 int i;
6970 Lisp_Object tem;
6972 this_command_key_count = 0;
6973 this_single_command_key_start = 0;
6975 keys = XVECTOR (saved_keys)->contents;
6976 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
6977 add_command_key (keys[i]);
6979 str = XSTRING (function);
6980 for (i = 0; i < str->size; i++)
6982 XSETFASTINT (tem, str->data[i]);
6983 add_command_key (tem);
6986 XSETFASTINT (tem, '\015');
6987 add_command_key (tem);
6990 UNGCPRO;
6992 function = Fintern (function, Qnil);
6993 current_kboard->Vprefix_arg = prefixarg;
6994 this_command = function;
6996 /* If enabled, show which key runs this command. */
6997 if (!NILP (Vsuggest_key_bindings)
6998 && NILP (Vexecuting_macro)
6999 && SYMBOLP (function))
7001 Lisp_Object bindings;
7003 bindings = Fwhere_is_internal (function, Voverriding_local_map,
7004 Qt, Qnil);
7006 if (!NILP (bindings))
7008 message ("You can run the command `%s' by typing %s",
7009 XSYMBOL (function)->name->data,
7010 XSTRING (Fkey_description (bindings))->data);
7011 Fsit_for ((NUMBERP (Vsuggest_key_bindings)
7012 ? Vsuggest_key_bindings : make_number (2)),
7013 Qnil, Qnil);
7017 return Fcommand_execute (function, Qt, Qnil, Qnil);
7020 /* Find the set of keymaps now active.
7021 Store into *MAPS_P a vector holding the various maps
7022 and return the number of them. The vector was malloc'd
7023 and the caller should free it. */
7026 current_active_maps (maps_p)
7027 Lisp_Object **maps_p;
7029 Lisp_Object *tmaps, *maps;
7030 int nmaps;
7032 /* Should overriding-terminal-local-map and overriding-local-map apply? */
7033 if (!NILP (Voverriding_local_map_menu_flag))
7035 /* Yes, use them (if non-nil) as well as the global map. */
7036 maps = (Lisp_Object *) xmalloc (3 * sizeof (maps[0]));
7037 nmaps = 0;
7038 if (!NILP (current_kboard->Voverriding_terminal_local_map))
7039 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
7040 if (!NILP (Voverriding_local_map))
7041 maps[nmaps++] = Voverriding_local_map;
7043 else
7045 /* No, so use major and minor mode keymaps. */
7046 nmaps = current_minor_maps (NULL, &tmaps);
7047 maps = (Lisp_Object *) xmalloc ((nmaps + 2) * sizeof (maps[0]));
7048 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
7049 #ifdef USE_TEXT_PROPERTIES
7050 maps[nmaps++] = get_local_map (PT, current_buffer);
7051 #else
7052 maps[nmaps++] = current_buffer->keymap;
7053 #endif
7055 maps[nmaps++] = current_global_map;
7057 *maps_p = maps;
7058 return nmaps;
7061 /* Return nonzero if input events are pending. */
7063 detect_input_pending ()
7065 if (!input_pending)
7066 get_input_pending (&input_pending, 0);
7068 return input_pending;
7071 /* Return nonzero if input events are pending.
7072 Execute timers immediately; don't make events for them. */
7074 detect_input_pending_run_timers (do_display)
7075 int do_display;
7077 int old_timers_run = timers_run;
7079 if (!input_pending)
7080 get_input_pending (&input_pending, 1);
7082 if (old_timers_run != timers_run && do_display)
7083 redisplay_preserve_echo_area ();
7085 return input_pending;
7088 /* This is called in some cases before a possible quit.
7089 It cases the next call to detect_input_pending to recompute input_pending.
7090 So calling this function unnecessarily can't do any harm. */
7091 clear_input_pending ()
7093 input_pending = 0;
7096 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
7097 "T if command input is currently available with no waiting.\n\
7098 Actually, the value is nil only if we can be sure that no input is available.")
7101 if (!NILP (Vunread_command_events) || unread_command_char != -1)
7102 return (Qt);
7104 get_input_pending (&input_pending, 1);
7105 return input_pending > 0 ? Qt : Qnil;
7108 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
7109 "Return vector of last 100 events, not counting those from keyboard macros.")
7112 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
7113 Lisp_Object val;
7115 if (total_keys < NUM_RECENT_KEYS)
7116 return Fvector (total_keys, keys);
7117 else
7119 val = Fvector (NUM_RECENT_KEYS, keys);
7120 bcopy (keys + recent_keys_index,
7121 XVECTOR (val)->contents,
7122 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
7123 bcopy (keys,
7124 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
7125 recent_keys_index * sizeof (Lisp_Object));
7126 return val;
7130 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
7131 "Return the key sequence that invoked this command.\n\
7132 The value is a string or a vector.")
7135 return make_event_array (this_command_key_count,
7136 XVECTOR (this_command_keys)->contents);
7139 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
7140 Sthis_single_command_keys, 0, 0, 0,
7141 "Return the key sequence that invoked this command.\n\
7142 Unlike `this-command-keys', this function's value\n\
7143 does not include prefix arguments.\n\
7144 The value is a string or a vector.")
7147 return make_event_array (this_command_key_count
7148 - this_single_command_key_start,
7149 (XVECTOR (this_command_keys)->contents
7150 + this_single_command_key_start));
7153 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
7154 Sreset_this_command_lengths, 0, 0, 0,
7155 "Used for complicated reasons in `universal-argument-other-key'.\n\
7157 `universal-argument-other-key' rereads the event just typed.\n\
7158 It then gets translated through `function-key-map'.\n\
7159 The translated event gets included in the echo area and in\n\
7160 the value of `this-command-keys' in addition to the raw original event.\n\
7161 That is not right.\n\
7163 Calling this function directs the translated event to replace\n\
7164 the original event, so that only one version of the event actually\n\
7165 appears in the echo area and in the value of `this-command-keys.'.")
7168 before_command_restore_flag = 1;
7169 before_command_key_count_1 = before_command_key_count;
7170 before_command_echo_length_1 = before_command_echo_length;
7173 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
7174 "Return the current depth in recursive edits.")
7177 Lisp_Object temp;
7178 XSETFASTINT (temp, command_loop_level + minibuf_level);
7179 return temp;
7182 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
7183 "FOpen dribble file: ",
7184 "Start writing all keyboard characters to a dribble file called FILE.\n\
7185 If FILE is nil, close any open dribble file.")
7186 (file)
7187 Lisp_Object file;
7189 if (dribble)
7191 fclose (dribble);
7192 dribble = 0;
7194 if (!NILP (file))
7196 file = Fexpand_file_name (file, Qnil);
7197 dribble = fopen (XSTRING (file)->data, "w");
7198 if (dribble == 0)
7199 report_file_error ("Opening dribble", Fcons (file, Qnil));
7201 return Qnil;
7204 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
7205 "Discard the contents of the terminal input buffer.\n\
7206 Also cancel any kbd macro being defined.")
7209 current_kboard->defining_kbd_macro = Qnil;
7210 update_mode_lines++;
7212 Vunread_command_events = Qnil;
7213 unread_command_char = -1;
7215 discard_tty_input ();
7217 /* Without the cast, GCC complains that this assignment loses the
7218 volatile qualifier of kbd_store_ptr. Is there anything wrong
7219 with that? */
7220 kbd_fetch_ptr = (struct input_event *) kbd_store_ptr;
7221 Ffillarray (kbd_buffer_frame_or_window, Qnil);
7222 input_pending = 0;
7224 return Qnil;
7227 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
7228 "Stop Emacs and return to superior process. You can resume later.\n\
7229 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
7230 control, run a subshell instead.\n\n\
7231 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
7232 to be read as terminal input by Emacs's parent, after suspension.\n\
7234 Before suspending, run the normal hook `suspend-hook'.\n\
7235 After resumption run the normal hook `suspend-resume-hook'.\n\
7237 Some operating systems cannot stop the Emacs process and resume it later.\n\
7238 On such systems, Emacs starts a subshell instead of suspending.")
7239 (stuffstring)
7240 Lisp_Object stuffstring;
7242 Lisp_Object tem;
7243 int count = specpdl_ptr - specpdl;
7244 int old_height, old_width;
7245 int width, height;
7246 struct gcpro gcpro1, gcpro2;
7247 extern init_sys_modes ();
7249 if (!NILP (stuffstring))
7250 CHECK_STRING (stuffstring, 0);
7252 /* Run the functions in suspend-hook. */
7253 if (!NILP (Vrun_hooks))
7254 call1 (Vrun_hooks, intern ("suspend-hook"));
7256 GCPRO1 (stuffstring);
7257 get_frame_size (&old_width, &old_height);
7258 reset_sys_modes ();
7259 /* sys_suspend can get an error if it tries to fork a subshell
7260 and the system resources aren't available for that. */
7261 record_unwind_protect (init_sys_modes, 0);
7262 stuff_buffered_input (stuffstring);
7263 if (cannot_suspend)
7264 sys_subshell ();
7265 else
7266 sys_suspend ();
7267 unbind_to (count, Qnil);
7269 /* Check if terminal/window size has changed.
7270 Note that this is not useful when we are running directly
7271 with a window system; but suspend should be disabled in that case. */
7272 get_frame_size (&width, &height);
7273 if (width != old_width || height != old_height)
7274 change_frame_size (selected_frame, height, width, 0, 0);
7276 /* Run suspend-resume-hook. */
7277 if (!NILP (Vrun_hooks))
7278 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
7280 UNGCPRO;
7281 return Qnil;
7284 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
7285 Then in any case stuff anything Emacs has read ahead and not used. */
7287 stuff_buffered_input (stuffstring)
7288 Lisp_Object stuffstring;
7290 /* stuff_char works only in BSD, versions 4.2 and up. */
7291 #ifdef BSD
7292 #ifndef BSD4_1
7293 register unsigned char *p;
7295 if (STRINGP (stuffstring))
7297 register int count;
7299 p = XSTRING (stuffstring)->data;
7300 count = XSTRING (stuffstring)->size;
7301 while (count-- > 0)
7302 stuff_char (*p++);
7303 stuff_char ('\n');
7305 /* Anything we have read ahead, put back for the shell to read. */
7306 /* ?? What should this do when we have multiple keyboards??
7307 Should we ignore anything that was typed in at the "wrong" kboard? */
7308 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
7310 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
7311 kbd_fetch_ptr = kbd_buffer;
7312 if (kbd_fetch_ptr->kind == ascii_keystroke)
7313 stuff_char (kbd_fetch_ptr->code);
7314 kbd_fetch_ptr->kind = no_event;
7315 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
7316 - kbd_buffer]
7317 = Qnil);
7319 input_pending = 0;
7320 #endif
7321 #endif /* BSD and not BSD4_1 */
7324 set_waiting_for_input (time_to_clear)
7325 EMACS_TIME *time_to_clear;
7327 input_available_clear_time = time_to_clear;
7329 /* Tell interrupt_signal to throw back to read_char, */
7330 waiting_for_input = 1;
7332 /* If interrupt_signal was called before and buffered a C-g,
7333 make it run again now, to avoid timing error. */
7334 if (!NILP (Vquit_flag))
7335 quit_throw_to_read_char ();
7338 clear_waiting_for_input ()
7340 /* Tell interrupt_signal not to throw back to read_char, */
7341 waiting_for_input = 0;
7342 input_available_clear_time = 0;
7345 /* This routine is called at interrupt level in response to C-G.
7346 If interrupt_input, this is the handler for SIGINT.
7347 Otherwise, it is called from kbd_buffer_store_event,
7348 in handling SIGIO or SIGTINT.
7350 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
7351 immediately throw back to read_char.
7353 Otherwise it sets the Lisp variable quit-flag not-nil.
7354 This causes eval to throw, when it gets a chance.
7355 If quit-flag is already non-nil, it stops the job right away. */
7357 SIGTYPE
7358 interrupt_signal (signalnum) /* If we don't have an argument, */
7359 int signalnum; /* some compilers complain in signal calls. */
7361 char c;
7362 /* Must preserve main program's value of errno. */
7363 int old_errno = errno;
7365 #ifdef USG
7366 if (!read_socket_hook && NILP (Vwindow_system))
7368 /* USG systems forget handlers when they are used;
7369 must reestablish each time */
7370 signal (SIGINT, interrupt_signal);
7371 signal (SIGQUIT, interrupt_signal);
7373 #endif /* USG */
7375 cancel_echoing ();
7377 if (!NILP (Vquit_flag) && FRAME_TERMCAP_P (selected_frame))
7379 fflush (stdout);
7380 reset_sys_modes ();
7381 sigfree ();
7382 #ifdef SIGTSTP /* Support possible in later USG versions */
7384 * On systems which can suspend the current process and return to the original
7385 * shell, this command causes the user to end up back at the shell.
7386 * The "Auto-save" and "Abort" questions are not asked until
7387 * the user elects to return to emacs, at which point he can save the current
7388 * job and either dump core or continue.
7390 sys_suspend ();
7391 #else
7392 #ifdef VMS
7393 if (sys_suspend () == -1)
7395 printf ("Not running as a subprocess;\n");
7396 printf ("you can continue or abort.\n");
7398 #else /* not VMS */
7399 /* Perhaps should really fork an inferior shell?
7400 But that would not provide any way to get back
7401 to the original shell, ever. */
7402 printf ("No support for stopping a process on this operating system;\n");
7403 printf ("you can continue or abort.\n");
7404 #endif /* not VMS */
7405 #endif /* not SIGTSTP */
7406 #ifdef MSDOS
7407 /* We must remain inside the screen area when the internal terminal
7408 is used. Note that [Enter] is not echoed by dos. */
7409 cursor_to (0, 0);
7410 #endif
7411 /* It doesn't work to autosave while GC is in progress;
7412 the code used for auto-saving doesn't cope with the mark bit. */
7413 if (!gc_in_progress)
7415 printf ("Auto-save? (y or n) ");
7416 fflush (stdout);
7417 if (((c = getchar ()) & ~040) == 'Y')
7419 Fdo_auto_save (Qt, Qnil);
7420 #ifdef MSDOS
7421 printf ("\r\nAuto-save done");
7422 #else /* not MSDOS */
7423 printf ("Auto-save done\n");
7424 #endif /* not MSDOS */
7426 while (c != '\n') c = getchar ();
7428 else
7430 /* During GC, it must be safe to reenable quitting again. */
7431 Vinhibit_quit = Qnil;
7432 #ifdef MSDOS
7433 printf ("\r\n");
7434 #endif /* not MSDOS */
7435 printf ("Garbage collection in progress; cannot auto-save now\r\n");
7436 printf ("but will instead do a real quit after garbage collection ends\r\n");
7437 fflush (stdout);
7440 #ifdef MSDOS
7441 printf ("\r\nAbort? (y or n) ");
7442 #else /* not MSDOS */
7443 #ifdef VMS
7444 printf ("Abort (and enter debugger)? (y or n) ");
7445 #else /* not VMS */
7446 printf ("Abort (and dump core)? (y or n) ");
7447 #endif /* not VMS */
7448 #endif /* not MSDOS */
7449 fflush (stdout);
7450 if (((c = getchar ()) & ~040) == 'Y')
7451 abort ();
7452 while (c != '\n') c = getchar ();
7453 #ifdef MSDOS
7454 printf ("\r\nContinuing...\r\n");
7455 #else /* not MSDOS */
7456 printf ("Continuing...\n");
7457 #endif /* not MSDOS */
7458 fflush (stdout);
7459 init_sys_modes ();
7461 else
7463 /* If executing a function that wants to be interrupted out of
7464 and the user has not deferred quitting by binding `inhibit-quit'
7465 then quit right away. */
7466 if (immediate_quit && NILP (Vinhibit_quit))
7468 immediate_quit = 0;
7469 sigfree ();
7470 Fsignal (Qquit, Qnil);
7472 else
7473 /* Else request quit when it's safe */
7474 Vquit_flag = Qt;
7477 if (waiting_for_input && !echoing)
7478 quit_throw_to_read_char ();
7480 errno = old_errno;
7483 /* Handle a C-g by making read_char return C-g. */
7485 quit_throw_to_read_char ()
7487 quit_error_check ();
7488 sigfree ();
7489 /* Prevent another signal from doing this before we finish. */
7490 clear_waiting_for_input ();
7491 input_pending = 0;
7493 Vunread_command_events = Qnil;
7494 unread_command_char = -1;
7496 #if 0 /* Currently, sit_for is called from read_char without turning
7497 off polling. And that can call set_waiting_for_input.
7498 It seems to be harmless. */
7499 #ifdef POLL_FOR_INPUT
7500 /* May be > 1 if in recursive minibuffer. */
7501 if (poll_suppress_count == 0)
7502 abort ();
7503 #endif
7504 #endif
7505 #ifdef MULTI_FRAME
7506 if (FRAMEP (internal_last_event_frame)
7507 && XFRAME (internal_last_event_frame) != selected_frame)
7508 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
7509 Qnil, 0);
7510 #endif
7512 _longjmp (getcjmp, 1);
7515 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
7516 "Set mode of reading keyboard input.\n\
7517 First arg INTERRUPT non-nil means use input interrupts;\n\
7518 nil means use CBREAK mode.\n\
7519 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
7520 (no effect except in CBREAK mode).\n\
7521 Third arg META t means accept 8-bit input (for a Meta key).\n\
7522 META nil means ignore the top bit, on the assumption it is parity.\n\
7523 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
7524 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
7525 See also `current-input-mode'.")
7526 (interrupt, flow, meta, quit)
7527 Lisp_Object interrupt, flow, meta, quit;
7529 if (!NILP (quit)
7530 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
7531 error ("set-input-mode: QUIT must be an ASCII character");
7533 #ifdef POLL_FOR_INPUT
7534 stop_polling ();
7535 #endif
7537 #ifndef MSDOS
7538 /* this causes startup screen to be restored and messes with the mouse */
7539 reset_sys_modes ();
7540 #endif
7542 #ifdef SIGIO
7543 /* Note SIGIO has been undef'd if FIONREAD is missing. */
7544 if (read_socket_hook)
7546 /* When using X, don't give the user a real choice,
7547 because we haven't implemented the mechanisms to support it. */
7548 #ifdef NO_SOCK_SIGIO
7549 interrupt_input = 0;
7550 #else /* not NO_SOCK_SIGIO */
7551 interrupt_input = 1;
7552 #endif /* NO_SOCK_SIGIO */
7554 else
7555 interrupt_input = !NILP (interrupt);
7556 #else /* not SIGIO */
7557 interrupt_input = 0;
7558 #endif /* not SIGIO */
7560 /* Our VMS input only works by interrupts, as of now. */
7561 #ifdef VMS
7562 interrupt_input = 1;
7563 #endif
7565 flow_control = !NILP (flow);
7566 if (NILP (meta))
7567 meta_key = 0;
7568 else if (EQ (meta, Qt))
7569 meta_key = 1;
7570 else
7571 meta_key = 2;
7572 if (!NILP (quit))
7573 /* Don't let this value be out of range. */
7574 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
7576 #ifndef MSDOS
7577 init_sys_modes ();
7578 #endif
7580 #ifdef POLL_FOR_INPUT
7581 poll_suppress_count = 1;
7582 start_polling ();
7583 #endif
7584 return Qnil;
7587 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
7588 "Return information about the way Emacs currently reads keyboard input.\n\
7589 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
7590 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
7591 nil, Emacs is using CBREAK mode.\n\
7592 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
7593 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
7594 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
7595 META nil means ignoring the top bit, on the assumption it is parity.\n\
7596 META is neither t nor nil if accepting 8-bit input and using\n\
7597 all 8 bits as the character code.\n\
7598 QUIT is the character Emacs currently uses to quit.\n\
7599 The elements of this list correspond to the arguments of\n\
7600 `set-input-mode'.")
7603 Lisp_Object val[4];
7605 val[0] = interrupt_input ? Qt : Qnil;
7606 val[1] = flow_control ? Qt : Qnil;
7607 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
7608 XSETFASTINT (val[3], quit_char);
7610 return Flist (sizeof (val) / sizeof (val[0]), val);
7615 * Set up a new kboard object with reasonable initial values.
7617 void
7618 init_kboard (kb)
7619 KBOARD *kb;
7621 kb->Voverriding_terminal_local_map = Qnil;
7622 kb->Vlast_command = Qnil;
7623 kb->Vprefix_arg = Qnil;
7624 kb->kbd_queue = Qnil;
7625 kb->kbd_queue_has_data = 0;
7626 kb->immediate_echo = 0;
7627 kb->echoptr = kb->echobuf;
7628 kb->echo_after_prompt = -1;
7629 kb->kbd_macro_buffer = 0;
7630 kb->kbd_macro_bufsize = 0;
7631 kb->defining_kbd_macro = Qnil;
7632 kb->Vlast_kbd_macro = Qnil;
7633 kb->reference_count = 0;
7634 kb->Vsystem_key_alist = Qnil;
7635 kb->system_key_syms = Qnil;
7636 kb->Vdefault_minibuffer_frame = Qnil;
7640 * Destroy the contents of a kboard object, but not the object itself.
7641 * We use this just before deleting it, or if we're going to initialize
7642 * it a second time.
7644 static void
7645 wipe_kboard (kb)
7646 KBOARD *kb;
7648 if (kb->kbd_macro_buffer)
7649 xfree (kb->kbd_macro_buffer);
7652 #ifdef MULTI_KBOARD
7653 void
7654 delete_kboard (kb)
7655 KBOARD *kb;
7657 KBOARD **kbp;
7658 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
7659 if (*kbp == NULL)
7660 abort ();
7661 *kbp = kb->next_kboard;
7662 wipe_kboard (kb);
7663 xfree (kb);
7665 #endif
7667 init_keyboard ()
7669 /* This is correct before outermost invocation of the editor loop */
7670 command_loop_level = -1;
7671 immediate_quit = 0;
7672 quit_char = Ctl ('g');
7673 Vunread_command_events = Qnil;
7674 unread_command_char = -1;
7675 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
7676 total_keys = 0;
7677 recent_keys_index = 0;
7678 kbd_fetch_ptr = kbd_buffer;
7679 kbd_store_ptr = kbd_buffer;
7680 kbd_buffer_frame_or_window
7681 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
7682 #ifdef HAVE_MOUSE
7683 do_mouse_tracking = Qnil;
7684 #endif
7685 input_pending = 0;
7687 #ifdef MULTI_FRAME
7688 /* This means that command_loop_1 won't try to select anything the first
7689 time through. */
7690 internal_last_event_frame = Qnil;
7691 Vlast_event_frame = internal_last_event_frame;
7692 #endif
7694 #ifdef MULTI_KBOARD
7695 current_kboard = initial_kboard;
7696 #endif
7697 wipe_kboard (current_kboard);
7698 init_kboard (current_kboard);
7700 if (initialized)
7701 Ffillarray (kbd_buffer_frame_or_window, Qnil);
7703 kbd_buffer_frame_or_window
7704 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
7705 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
7707 signal (SIGINT, interrupt_signal);
7708 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
7709 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
7710 SIGQUIT and we can't tell which one it will give us. */
7711 signal (SIGQUIT, interrupt_signal);
7712 #endif /* HAVE_TERMIO */
7714 /* Note SIGIO has been undef'd if FIONREAD is missing. */
7715 #ifdef SIGIO
7716 if (!noninteractive)
7717 signal (SIGIO, input_available_signal);
7718 #endif /* SIGIO */
7720 /* Use interrupt input by default, if it works and noninterrupt input
7721 has deficiencies. */
7723 #ifdef INTERRUPT_INPUT
7724 interrupt_input = 1;
7725 #else
7726 interrupt_input = 0;
7727 #endif
7729 /* Our VMS input only works by interrupts, as of now. */
7730 #ifdef VMS
7731 interrupt_input = 1;
7732 #endif
7734 sigfree ();
7735 dribble = 0;
7737 if (keyboard_init_hook)
7738 (*keyboard_init_hook) ();
7740 #ifdef POLL_FOR_INPUT
7741 poll_suppress_count = 1;
7742 start_polling ();
7743 #endif
7746 /* This type's only use is in syms_of_keyboard, to initialize the
7747 event header symbols and put properties on them. */
7748 struct event_head {
7749 Lisp_Object *var;
7750 char *name;
7751 Lisp_Object *kind;
7754 struct event_head head_table[] = {
7755 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
7756 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
7757 &Qswitch_frame, "switch-frame", &Qswitch_frame,
7758 &Qdelete_frame, "delete-frame", &Qdelete_frame,
7759 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
7760 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
7763 syms_of_keyboard ()
7765 Qdisabled_command_hook = intern ("disabled-command-hook");
7766 staticpro (&Qdisabled_command_hook);
7768 Qself_insert_command = intern ("self-insert-command");
7769 staticpro (&Qself_insert_command);
7771 Qforward_char = intern ("forward-char");
7772 staticpro (&Qforward_char);
7774 Qbackward_char = intern ("backward-char");
7775 staticpro (&Qbackward_char);
7777 Qdisabled = intern ("disabled");
7778 staticpro (&Qdisabled);
7780 Qundefined = intern ("undefined");
7781 staticpro (&Qundefined);
7783 Qpre_command_hook = intern ("pre-command-hook");
7784 staticpro (&Qpre_command_hook);
7786 Qpost_command_hook = intern ("post-command-hook");
7787 staticpro (&Qpost_command_hook);
7789 Qpost_command_idle_hook = intern ("post-command-idle-hook");
7790 staticpro (&Qpost_command_idle_hook);
7792 Qdeferred_action_function = intern ("deferred-action-function");
7793 staticpro (&Qdeferred_action_function);
7795 Qcommand_hook_internal = intern ("command-hook-internal");
7796 staticpro (&Qcommand_hook_internal);
7798 Qfunction_key = intern ("function-key");
7799 staticpro (&Qfunction_key);
7800 Qmouse_click = intern ("mouse-click");
7801 staticpro (&Qmouse_click);
7802 Qtimer_event = intern ("timer-event");
7803 staticpro (&Qtimer_event);
7805 Qmenu_enable = intern ("menu-enable");
7806 staticpro (&Qmenu_enable);
7808 Qmode_line = intern ("mode-line");
7809 staticpro (&Qmode_line);
7810 Qvertical_line = intern ("vertical-line");
7811 staticpro (&Qvertical_line);
7812 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
7813 staticpro (&Qvertical_scroll_bar);
7814 Qmenu_bar = intern ("menu-bar");
7815 staticpro (&Qmenu_bar);
7817 Qabove_handle = intern ("above-handle");
7818 staticpro (&Qabove_handle);
7819 Qhandle = intern ("handle");
7820 staticpro (&Qhandle);
7821 Qbelow_handle = intern ("below-handle");
7822 staticpro (&Qbelow_handle);
7823 Qup = intern ("up");
7824 staticpro (&Qup);
7825 Qdown = intern ("down");
7826 staticpro (&Qdown);
7828 Qevent_kind = intern ("event-kind");
7829 staticpro (&Qevent_kind);
7830 Qevent_symbol_elements = intern ("event-symbol-elements");
7831 staticpro (&Qevent_symbol_elements);
7832 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
7833 staticpro (&Qevent_symbol_element_mask);
7834 Qmodifier_cache = intern ("modifier-cache");
7835 staticpro (&Qmodifier_cache);
7837 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
7838 staticpro (&Qrecompute_lucid_menubar);
7839 Qactivate_menubar_hook = intern ("activate-menubar-hook");
7840 staticpro (&Qactivate_menubar_hook);
7842 Qpolling_period = intern ("polling-period");
7843 staticpro (&Qpolling_period);
7846 struct event_head *p;
7848 for (p = head_table;
7849 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
7850 p++)
7852 *p->var = intern (p->name);
7853 staticpro (p->var);
7854 Fput (*p->var, Qevent_kind, *p->kind);
7855 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
7859 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
7860 staticpro (&button_down_location);
7863 int i;
7864 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
7866 modifier_symbols = Fmake_vector (make_number (len), Qnil);
7867 for (i = 0; i < len; i++)
7868 if (modifier_names[i])
7869 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
7870 staticpro (&modifier_symbols);
7873 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
7874 staticpro (&recent_keys);
7876 this_command_keys = Fmake_vector (make_number (40), Qnil);
7877 staticpro (&this_command_keys);
7879 Qextended_command_history = intern ("extended-command-history");
7880 Fset (Qextended_command_history, Qnil);
7881 staticpro (&Qextended_command_history);
7883 kbd_buffer_frame_or_window
7884 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
7885 staticpro (&kbd_buffer_frame_or_window);
7887 accent_key_syms = Qnil;
7888 staticpro (&accent_key_syms);
7890 func_key_syms = Qnil;
7891 staticpro (&func_key_syms);
7893 mouse_syms = Qnil;
7894 staticpro (&mouse_syms);
7896 unread_switch_frame = Qnil;
7897 staticpro (&unread_switch_frame);
7899 defsubr (&Sevent_convert_list);
7900 defsubr (&Sread_key_sequence);
7901 defsubr (&Srecursive_edit);
7902 #ifdef HAVE_MOUSE
7903 defsubr (&Strack_mouse);
7904 #endif
7905 defsubr (&Sinput_pending_p);
7906 defsubr (&Scommand_execute);
7907 defsubr (&Srecent_keys);
7908 defsubr (&Sthis_command_keys);
7909 defsubr (&Sthis_single_command_keys);
7910 defsubr (&Sreset_this_command_lengths);
7911 defsubr (&Ssuspend_emacs);
7912 defsubr (&Sabort_recursive_edit);
7913 defsubr (&Sexit_recursive_edit);
7914 defsubr (&Srecursion_depth);
7915 defsubr (&Stop_level);
7916 defsubr (&Sdiscard_input);
7917 defsubr (&Sopen_dribble_file);
7918 defsubr (&Sset_input_mode);
7919 defsubr (&Scurrent_input_mode);
7920 defsubr (&Sexecute_extended_command);
7922 DEFVAR_LISP ("last-command-char", &last_command_char,
7923 "Last input event that was part of a command.");
7925 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
7926 "Last input event that was part of a command.");
7928 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
7929 "Last input event in a command, except for mouse menu events.\n\
7930 Mouse menus give back keys that don't look like mouse events;\n\
7931 this variable holds the actual mouse event that led to the menu,\n\
7932 so that you can determine whether the command was run by mouse or not.");
7934 DEFVAR_LISP ("last-input-char", &last_input_char,
7935 "Last input event.");
7937 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
7938 "Last input event.");
7940 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
7941 "List of objects to be read as next command input events.");
7943 DEFVAR_INT ("unread-command-char", &unread_command_char,
7944 "If not -1, an object to be read as next command input event.");
7946 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
7947 "Meta-prefix character code. Meta-foo as command input\n\
7948 turns into this character followed by foo.");
7949 XSETINT (meta_prefix_char, 033);
7951 DEFVAR_KBOARD ("last-command", Vlast_command,
7952 "The last command executed. Normally a symbol with a function definition,\n\
7953 but can be whatever was found in the keymap, or whatever the variable\n\
7954 `this-command' was set to by that command.\n\
7956 The value `mode-exit' is special; it means that the previous command\n\
7957 read an event that told it to exit, and it did so and unread that event.\n\
7958 In other words, the present command is the event that made the previous\n\
7959 command exit.\n\
7961 The value `kill-region' is special; it means that the previous command\n\
7962 was a kill command.");
7964 DEFVAR_LISP ("this-command", &this_command,
7965 "The command now being executed.\n\
7966 The command can set this variable; whatever is put here\n\
7967 will be in `last-command' during the following command.");
7968 this_command = Qnil;
7970 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
7971 "*Number of keyboard input characters between auto-saves.\n\
7972 Zero means disable autosaving due to number of characters typed.");
7973 auto_save_interval = 300;
7975 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
7976 "*Number of seconds idle time before auto-save.\n\
7977 Zero or nil means disable auto-saving due to idleness.\n\
7978 After auto-saving due to this many seconds of idle time,\n\
7979 Emacs also does a garbage collection if that seems to be warranted.");
7980 XSETFASTINT (Vauto_save_timeout, 30);
7982 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
7983 "*Nonzero means echo unfinished commands after this many seconds of pause.");
7984 echo_keystrokes = 1;
7986 DEFVAR_INT ("polling-period", &polling_period,
7987 "*Interval between polling for input during Lisp execution.\n\
7988 The reason for polling is to make C-g work to stop a running program.\n\
7989 Polling is needed only when using X windows and SIGIO does not work.\n\
7990 Polling is automatically disabled in all other cases.");
7991 polling_period = 2;
7993 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
7994 "*Maximum time between mouse clicks to make a double-click.\n\
7995 Measured in milliseconds. nil means disable double-click recognition;\n\
7996 t means double-clicks have no time limit and are detected\n\
7997 by position only.");
7998 Vdouble_click_time = make_number (500);
8000 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
8001 "*Non-nil means inhibit local map menu bar menus.");
8002 inhibit_local_menu_bar_menus = 0;
8004 DEFVAR_INT ("num-input-keys", &num_input_keys,
8005 "Number of complete keys read from the keyboard so far.");
8006 num_input_keys = 0;
8008 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
8009 "The frame in which the most recently read event occurred.\n\
8010 If the last event came from a keyboard macro, this is set to `macro'.");
8011 Vlast_event_frame = Qnil;
8013 DEFVAR_LISP ("help-char", &Vhelp_char,
8014 "Character to recognize as meaning Help.\n\
8015 When it is read, do `(eval help-form)', and display result if it's a string.\n\
8016 If the value of `help-form' is nil, this char can be read normally.");
8017 XSETINT (Vhelp_char, Ctl ('H'));
8019 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
8020 "List of input events to recognize as meaning Help.\n\
8021 These work just like the value of `help-char' (see that).");
8022 Vhelp_event_list = Qnil;
8024 DEFVAR_LISP ("help-form", &Vhelp_form,
8025 "Form to execute when character `help-char' is read.\n\
8026 If the form returns a string, that string is displayed.\n\
8027 If `help-form' is nil, the help char is not recognized.");
8028 Vhelp_form = Qnil;
8030 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
8031 "Command to run when `help-char' character follows a prefix key.\n\
8032 This command is used only when there is no actual binding\n\
8033 for that character after that prefix key.");
8034 Vprefix_help_command = Qnil;
8036 DEFVAR_LISP ("top-level", &Vtop_level,
8037 "Form to evaluate when Emacs starts up.\n\
8038 Useful to set before you dump a modified Emacs.");
8039 Vtop_level = Qnil;
8041 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
8042 "Translate table for keyboard input, or nil.\n\
8043 Each character is looked up in this string and the contents used instead.\n\
8044 The value may be a string, a vector, or a char-table.\n\
8045 If it is a string or vector of length N,\n\
8046 character codes N and up are untranslated.\n\
8047 In a vector or a char-table, an element which is nil means \"no translation\".");
8048 Vkeyboard_translate_table = Qnil;
8050 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
8051 "Non-nil means to always spawn a subshell instead of suspending,\n\
8052 even if the operating system has support for stopping a process.");
8053 cannot_suspend = 0;
8055 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
8056 "Non-nil means prompt with menus when appropriate.\n\
8057 This is done when reading from a keymap that has a prompt string,\n\
8058 for elements that have prompt strings.\n\
8059 The menu is displayed on the screen\n\
8060 if X menus were enabled at configuration\n\
8061 time and the previous event was a mouse click prefix key.\n\
8062 Otherwise, menu prompting uses the echo area.");
8063 menu_prompting = 1;
8065 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
8066 "Character to see next line of menu prompt.\n\
8067 Type this character while in a menu prompt to rotate around the lines of it.");
8068 XSETINT (menu_prompt_more_char, ' ');
8070 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
8071 "A mask of additional modifier keys to use with every keyboard character.\n\
8072 Emacs applies the modifiers of the character stored here to each keyboard\n\
8073 character it reads. For example, after evaluating the expression\n\
8074 (setq extra-keyboard-modifiers ?\\C-x)\n\
8075 all input characters will have the control modifier applied to them.\n\
8077 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
8078 not count as a control character; rather, it counts as a character\n\
8079 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
8080 cancels any modification.");
8081 extra_keyboard_modifiers = 0;
8083 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
8084 "If an editing command sets this to t, deactivate the mark afterward.\n\
8085 The command loop sets this to nil before each command,\n\
8086 and tests the value when the command returns.\n\
8087 Buffer modification stores t in this variable.");
8088 Vdeactivate_mark = Qnil;
8090 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
8091 "Temporary storage of pre-command-hook or post-command-hook.");
8092 Vcommand_hook_internal = Qnil;
8094 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
8095 "Normal hook run before each command is executed.\n\
8096 Errors running the hook are caught and ignored.");
8097 Vpre_command_hook = Qnil;
8099 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
8100 "Normal hook run after each command is executed.\n\
8101 Errors running the hook are caught and ignored.");
8102 Vpost_command_hook = Qnil;
8104 DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
8105 "Normal hook run after each command is executed, if idle.\n\
8106 Errors running the hook are caught and ignored.\n\
8107 This feature is obsolete; use idle timers instead. See `etc/NEWS'.");
8108 Vpost_command_idle_hook = Qnil;
8110 DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
8111 "Delay time before running `post-command-idle-hook'.\n\
8112 This is measured in microseconds.");
8113 post_command_idle_delay = 100000;
8115 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
8116 "t means menu bar, specified Lucid style, needs to be recomputed.");
8117 Vlucid_menu_bar_dirty_flag = Qnil;
8119 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
8120 "List of menu bar items to move to the end of the menu bar.\n\
8121 The elements of the list are event types that may have menu bar bindings.");
8122 Vmenu_bar_final_items = Qnil;
8124 DEFVAR_KBOARD ("overriding-terminal-local-map",
8125 Voverriding_terminal_local_map,
8126 "Keymap that overrides all other local keymaps.\n\
8127 If this variable is non-nil, it is used as a keymap instead of the\n\
8128 buffer's local map, and the minor mode keymaps and text property keymaps.");
8130 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
8131 "Keymap that overrides all other local keymaps.\n\
8132 If this variable is non-nil, it is used as a keymap instead of the\n\
8133 buffer's local map, and the minor mode keymaps and text property keymaps.");
8134 Voverriding_local_map = Qnil;
8136 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
8137 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
8138 Otherwise, the menu bar continues to reflect the buffer's local map\n\
8139 and the minor mode maps regardless of `overriding-local-map'.");
8140 Voverriding_local_map_menu_flag = Qnil;
8142 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
8143 "Keymap defining bindings for special events to execute at low level.");
8144 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
8146 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
8147 "*Non-nil means generate motion events for mouse motion.");
8149 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
8150 "Alist of system-specific X windows key symbols.\n\
8151 Each element should have the form (N . SYMBOL) where N is the\n\
8152 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
8153 and SYMBOL is its name.");
8155 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
8156 "List of deferred actions to be performed at a later time.\n\
8157 The precise format isn't relevant here; we just check whether it is nil.");
8158 Vdeferred_action_list = Qnil;
8160 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
8161 "Function to call to handle deferred actions, after each command.\n\
8162 This function is called with no arguments after each command\n\
8163 whenever `deferred-action-list' is non-nil.");
8164 Vdeferred_action_function = Qnil;
8166 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
8167 "Non-nil means show the equivalent key-binding when M-x command has one.\n\
8168 The value can be a length of time to show the message for.\n\
8169 If the value is non-nil and not a number, we wait 2 seconds.");
8170 Vsuggest_key_bindings = Qt;
8172 DEFVAR_LISP ("timer-list", &Vtimer_list,
8173 "List of active absolute time timers in order of increasing time");
8174 Vtimer_list = Qnil;
8176 DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
8177 "List of active idle-time timers in order of increasing time");
8178 Vtimer_idle_list = Qnil;
8181 keys_of_keyboard ()
8183 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
8184 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
8185 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
8186 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
8187 initial_define_key (meta_map, 'x', "execute-extended-command");
8189 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
8190 "handle-delete-frame");
8191 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
8192 "ignore-event");
8193 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
8194 "ignore-event");