(speedbar-update-current-file): Added call to
[emacs.git] / src / keyboard.c
blob3c5ebbb9b8d1f19e1c27ffb688b7a2f19b0d63ae
1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985,86,87,88,89,93,94,95,96,97 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Allow config.h to undefine symbols found here. */
22 #include <signal.h>
24 #include <config.h>
25 #include <stdio.h>
26 #include "termchar.h"
27 #include "termopts.h"
28 #include "lisp.h"
29 #include "termhooks.h"
30 #include "macros.h"
31 #include "frame.h"
32 #include "window.h"
33 #include "commands.h"
34 #include "buffer.h"
35 #include "charset.h"
36 #include "disptab.h"
37 #include "dispextern.h"
38 #include "keyboard.h"
39 #include "syntax.h"
40 #include "intervals.h"
41 #include "blockinput.h"
42 #include "puresize.h"
43 #include <setjmp.h>
44 #include <errno.h>
46 #ifdef MSDOS
47 #include "msdos.h"
48 #include <time.h>
49 #else /* not MSDOS */
50 #ifndef VMS
51 #include <sys/ioctl.h>
52 #endif
53 #endif /* not MSDOS */
55 #include "syssignal.h"
56 #include "systty.h"
58 /* This is to get the definitions of the XK_ symbols. */
59 #ifdef HAVE_X_WINDOWS
60 #include "xterm.h"
61 #endif
63 #ifdef HAVE_NTGUI
64 #include "w32term.h"
65 #endif /* HAVE_NTGUI */
67 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
68 #include "systime.h"
70 extern int errno;
72 /* Variables for blockinput.h: */
74 /* Non-zero if interrupt input is blocked right now. */
75 int interrupt_input_blocked;
77 /* Nonzero means an input interrupt has arrived
78 during the current critical section. */
79 int interrupt_input_pending;
82 /* File descriptor to use for input. */
83 extern int input_fd;
85 #ifdef HAVE_WINDOW_SYSTEM
86 /* Make all keyboard buffers much bigger when using X windows. */
87 #define KBD_BUFFER_SIZE 4096
88 #else /* No X-windows, character input */
89 #define KBD_BUFFER_SIZE 256
90 #endif /* No X-windows */
92 /* Following definition copied from eval.c */
94 struct backtrace
96 struct backtrace *next;
97 Lisp_Object *function;
98 Lisp_Object *args; /* Points to vector of args. */
99 int nargs; /* length of vector. If nargs is UNEVALLED,
100 args points to slot holding list of
101 unevalled args */
102 char evalargs;
105 #ifdef MULTI_KBOARD
106 KBOARD *initial_kboard;
107 KBOARD *current_kboard;
108 KBOARD *all_kboards;
109 int single_kboard;
110 #else
111 KBOARD the_only_kboard;
112 #endif
114 /* Non-nil disable property on a command means
115 do not execute it; call disabled-command-hook's value instead. */
116 Lisp_Object Qdisabled, Qdisabled_command_hook;
118 #define NUM_RECENT_KEYS (100)
119 int recent_keys_index; /* Index for storing next element into recent_keys */
120 int total_keys; /* Total number of elements stored into recent_keys */
121 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
123 /* Vector holding the key sequence that invoked the current command.
124 It is reused for each command, and it may be longer than the current
125 sequence; this_command_key_count indicates how many elements
126 actually mean something.
127 It's easier to staticpro a single Lisp_Object than an array. */
128 Lisp_Object this_command_keys;
129 int this_command_key_count;
131 /* This vector is used as a buffer to record the events that were actually read
132 by read_key_sequence. */
133 Lisp_Object raw_keybuf;
134 int raw_keybuf_count;
136 #define GROW_RAW_KEYBUF \
137 if (raw_keybuf_count == XVECTOR (raw_keybuf)->size) \
139 int newsize = 2 * XVECTOR (raw_keybuf)->size; \
140 Lisp_Object new; \
141 new = Fmake_vector (make_number (newsize), Qnil); \
142 bcopy (XVECTOR (raw_keybuf)->contents, XVECTOR (new)->contents, \
143 raw_keybuf_count * sizeof (Lisp_Object)); \
144 raw_keybuf = new; \
147 /* Number of elements of this_command_keys
148 that precede this key sequence. */
149 int this_single_command_key_start;
151 /* Record values of this_command_key_count and echo_length ()
152 before this command was read. */
153 static int before_command_key_count;
154 static int before_command_echo_length;
155 /* Values of before_command_key_count and before_command_echo_length
156 saved by reset-this-command-lengths. */
157 static int before_command_key_count_1;
158 static int before_command_echo_length_1;
159 /* Flag set by reset-this-command-lengths,
160 saying to reset the lengths when add_command_key is called. */
161 static int before_command_restore_flag;
163 extern int minbuf_level;
165 extern int message_enable_multibyte;
167 extern struct backtrace *backtrace_list;
169 /* Nonzero means do menu prompting. */
170 static int menu_prompting;
172 /* Character to see next line of menu prompt. */
173 static Lisp_Object menu_prompt_more_char;
175 /* For longjmp to where kbd input is being done. */
176 static jmp_buf getcjmp;
178 /* True while doing kbd input. */
179 int waiting_for_input;
181 /* True while displaying for echoing. Delays C-g throwing. */
182 static int echoing;
184 /* True means we can start echoing at the next input pause
185 even though there is something in the echo area. */
186 static char *ok_to_echo_at_next_pause;
188 /* Nonzero means disregard local maps for the menu bar. */
189 static int inhibit_local_menu_bar_menus;
191 /* Nonzero means C-g should cause immediate error-signal. */
192 int immediate_quit;
194 /* The user's ERASE setting. */
195 Lisp_Object Vtty_erase_char;
197 /* Character to recognize as the help char. */
198 Lisp_Object Vhelp_char;
200 /* List of other event types to recognize as meaning "help". */
201 Lisp_Object Vhelp_event_list;
203 /* Form to execute when help char is typed. */
204 Lisp_Object Vhelp_form;
206 /* Command to run when the help character follows a prefix key. */
207 Lisp_Object Vprefix_help_command;
209 /* List of items that should move to the end of the menu bar. */
210 Lisp_Object Vmenu_bar_final_items;
212 /* Non-nil means show the equivalent key-binding for
213 any M-x command that has one.
214 The value can be a length of time to show the message for.
215 If the value is non-nil and not a number, we wait 2 seconds. */
216 Lisp_Object Vsuggest_key_bindings;
218 /* Character that causes a quit. Normally C-g.
220 If we are running on an ordinary terminal, this must be an ordinary
221 ASCII char, since we want to make it our interrupt character.
223 If we are not running on an ordinary terminal, it still needs to be
224 an ordinary ASCII char. This character needs to be recognized in
225 the input interrupt handler. At this point, the keystroke is
226 represented as a struct input_event, while the desired quit
227 character is specified as a lispy event. The mapping from struct
228 input_events to lispy events cannot run in an interrupt handler,
229 and the reverse mapping is difficult for anything but ASCII
230 keystrokes.
232 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
233 ASCII character. */
234 int quit_char;
236 extern Lisp_Object current_global_map;
237 extern int minibuf_level;
239 /* If non-nil, this is a map that overrides all other local maps. */
240 Lisp_Object Voverriding_local_map;
242 /* If non-nil, Voverriding_local_map applies to the menu bar. */
243 Lisp_Object Voverriding_local_map_menu_flag;
245 /* Keymap that defines special misc events that should
246 be processed immediately at a low level. */
247 Lisp_Object Vspecial_event_map;
249 /* Current depth in recursive edits. */
250 int command_loop_level;
252 /* Total number of times command_loop has read a key sequence. */
253 int num_input_keys;
255 /* Last input character read as a command. */
256 Lisp_Object last_command_char;
258 /* Last input character read as a command, not counting menus
259 reached by the mouse. */
260 Lisp_Object last_nonmenu_event;
262 /* Last input character read for any purpose. */
263 Lisp_Object last_input_char;
265 /* If not Qnil, a list of objects to be read as subsequent command input. */
266 Lisp_Object Vunread_command_events;
268 /* If not Qnil, a list of objects to be read as subsequent command input
269 including input method processing. */
270 Lisp_Object Vunread_input_method_events;
272 /* If not Qnil, a list of objects to be read as subsequent command input
273 but NOT including input method processing. */
274 Lisp_Object Vunread_post_input_method_events;
276 /* If not -1, an event to be read as subsequent command input. */
277 int unread_command_char;
279 /* If not Qnil, this is a switch-frame event which we decided to put
280 off until the end of a key sequence. This should be read as the
281 next command input, after any unread_command_events.
283 read_key_sequence uses this to delay switch-frame events until the
284 end of the key sequence; Fread_char uses it to put off switch-frame
285 events until a non-ASCII event is acceptable as input. */
286 Lisp_Object unread_switch_frame;
288 /* A mask of extra modifier bits to put into every keyboard char. */
289 int extra_keyboard_modifiers;
291 /* Char to use as prefix when a meta character is typed in.
292 This is bound on entry to minibuffer in case ESC is changed there. */
294 Lisp_Object meta_prefix_char;
296 /* Last size recorded for a current buffer which is not a minibuffer. */
297 static int last_non_minibuf_size;
299 /* Number of idle seconds before an auto-save and garbage collection. */
300 static Lisp_Object Vauto_save_timeout;
302 /* Total number of times read_char has returned. */
303 int num_input_events;
305 /* Total number of times read_char has returned, outside of macros. */
306 int num_nonmacro_input_events;
308 /* Auto-save automatically when this many characters have been typed
309 since the last time. */
311 static int auto_save_interval;
313 /* Value of num_nonmacro_input_events as of last auto save. */
315 int last_auto_save;
317 /* The command being executed by the command loop.
318 Commands may set this, and the value set will be copied into
319 current_kboard->Vlast_command instead of the actual command. */
320 Lisp_Object this_command;
322 /* The value of point when the last command was executed. */
323 int last_point_position;
325 /* The buffer that was current when the last command was started. */
326 Lisp_Object last_point_position_buffer;
328 /* The frame in which the last input event occurred, or Qmacro if the
329 last event came from a macro. We use this to determine when to
330 generate switch-frame events. This may be cleared by functions
331 like Fselect_frame, to make sure that a switch-frame event is
332 generated by the next character. */
333 Lisp_Object internal_last_event_frame;
335 /* A user-visible version of the above, intended to allow users to
336 figure out where the last event came from, if the event doesn't
337 carry that information itself (i.e. if it was a character). */
338 Lisp_Object Vlast_event_frame;
340 /* The timestamp of the last input event we received from the X server.
341 X Windows wants this for selection ownership. */
342 unsigned long last_event_timestamp;
344 Lisp_Object Qself_insert_command;
345 Lisp_Object Qforward_char;
346 Lisp_Object Qbackward_char;
347 Lisp_Object Qundefined;
348 Lisp_Object Qtimer_event_handler;
350 /* read_key_sequence stores here the command definition of the
351 key sequence that it reads. */
352 Lisp_Object read_key_sequence_cmd;
354 /* Form to evaluate (if non-nil) when Emacs is started. */
355 Lisp_Object Vtop_level;
357 /* User-supplied string to translate input characters through. */
358 Lisp_Object Vkeyboard_translate_table;
360 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
361 extern Lisp_Object Vfunction_key_map;
363 /* Another keymap that maps key sequences into key sequences.
364 This one takes precedence over ordinary definitions. */
365 extern Lisp_Object Vkey_translation_map;
367 /* If non-nil, this implements the current input method. */
368 Lisp_Object Vinput_method_function;
369 Lisp_Object Qinput_method_function;
371 /* Non-nil means deactivate the mark at end of this command. */
372 Lisp_Object Vdeactivate_mark;
374 /* Menu bar specified in Lucid Emacs fashion. */
376 Lisp_Object Vlucid_menu_bar_dirty_flag;
377 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
379 Lisp_Object Qecho_area_clear_hook;
381 /* Hooks to run before and after each command. */
382 Lisp_Object Qpre_command_hook, Vpre_command_hook;
383 Lisp_Object Qpost_command_hook, Vpost_command_hook;
384 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
385 /* Hook run after a command if there's no more input soon. */
386 Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook;
388 /* Delay time in microseconds before running post-command-idle-hook. */
389 int post_command_idle_delay;
391 /* List of deferred actions to be performed at a later time.
392 The precise format isn't relevant here; we just check whether it is nil. */
393 Lisp_Object Vdeferred_action_list;
395 /* Function to call to handle deferred actions, when there are any. */
396 Lisp_Object Vdeferred_action_function;
397 Lisp_Object Qdeferred_action_function;
399 /* File in which we write all commands we read. */
400 FILE *dribble;
402 /* Nonzero if input is available. */
403 int input_pending;
405 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
406 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
408 int meta_key;
410 extern char *pending_malloc_warning;
412 /* Circular buffer for pre-read keyboard input. */
413 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
415 /* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
417 The interrupt-level event handlers will never enqueue an event on a
418 frame which is not in Vframe_list, and once an event is dequeued,
419 internal_last_event_frame or the event itself points to the frame.
420 So that's all fine.
422 But while the event is sitting in the queue, it's completely
423 unprotected. Suppose the user types one command which will run for
424 a while and then delete a frame, and then types another event at
425 the frame that will be deleted, before the command gets around to
426 it. Suppose there are no references to this frame elsewhere in
427 Emacs, and a GC occurs before the second event is dequeued. Now we
428 have an event referring to a freed frame, which will crash Emacs
429 when it is dequeued.
431 Similar things happen when an event on a scroll bar is enqueued; the
432 window may be deleted while the event is in the queue.
434 So, we use this vector to protect the frame_or_window field in the
435 event queue. That way, they'll be dequeued as dead frames or
436 windows, but still valid lisp objects.
438 If kbd_buffer[i].kind != no_event, then
439 (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
440 == kbd_buffer[i].frame_or_window. */
441 static Lisp_Object kbd_buffer_frame_or_window;
443 /* Pointer to next available character in kbd_buffer.
444 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
445 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
446 next available char is in kbd_buffer[0]. */
447 static struct input_event *kbd_fetch_ptr;
449 /* Pointer to next place to store character in kbd_buffer. This
450 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
451 character should go in kbd_buffer[0]. */
452 static volatile struct input_event *kbd_store_ptr;
454 /* The above pair of variables forms a "queue empty" flag. When we
455 enqueue a non-hook event, we increment kbd_store_ptr. When we
456 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
457 there is input available iff the two pointers are not equal.
459 Why not just have a flag set and cleared by the enqueuing and
460 dequeuing functions? Such a flag could be screwed up by interrupts
461 at inopportune times. */
463 /* If this flag is non-nil, we check mouse_moved to see when the
464 mouse moves, and motion events will appear in the input stream.
465 Otherwise, mouse motion is ignored. */
466 static Lisp_Object do_mouse_tracking;
468 /* Symbols to head events. */
469 Lisp_Object Qmouse_movement;
470 Lisp_Object Qscroll_bar_movement;
471 Lisp_Object Qswitch_frame;
472 Lisp_Object Qdelete_frame;
473 Lisp_Object Qiconify_frame;
474 Lisp_Object Qmake_frame_visible;
476 /* Symbols to denote kinds of events. */
477 Lisp_Object Qfunction_key;
478 Lisp_Object Qmouse_click;
479 #ifdef WINDOWSNT
480 Lisp_Object Qmouse_wheel;
481 #endif
482 Lisp_Object Qdrag_n_drop;
483 /* Lisp_Object Qmouse_movement; - also an event header */
485 /* Properties of event headers. */
486 Lisp_Object Qevent_kind;
487 Lisp_Object Qevent_symbol_elements;
489 /* menu item parts */
490 Lisp_Object Qmenu_alias;
491 Lisp_Object Qmenu_enable;
492 Lisp_Object QCenable, QCvisible, QChelp, QCfilter, QCkeys, QCkey_sequence;
493 Lisp_Object QCbutton, QCtoggle, QCradio;
494 extern Lisp_Object Vdefine_key_rebound_commands;
495 extern Lisp_Object Qmenu_item;
497 /* An event header symbol HEAD may have a property named
498 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
499 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
500 mask of modifiers applied to it. If present, this is used to help
501 speed up parse_modifiers. */
502 Lisp_Object Qevent_symbol_element_mask;
504 /* An unmodified event header BASE may have a property named
505 Qmodifier_cache, which is an alist mapping modifier masks onto
506 modified versions of BASE. If present, this helps speed up
507 apply_modifiers. */
508 Lisp_Object Qmodifier_cache;
510 /* Symbols to use for parts of windows. */
511 Lisp_Object Qmode_line;
512 Lisp_Object Qvertical_line;
513 Lisp_Object Qvertical_scroll_bar;
514 Lisp_Object Qmenu_bar;
516 Lisp_Object recursive_edit_unwind (), command_loop ();
517 Lisp_Object Fthis_command_keys ();
518 Lisp_Object Qextended_command_history;
519 EMACS_TIME timer_check ();
521 extern Lisp_Object Vhistory_length;
523 extern char *x_get_keysym_name ();
525 static void record_menu_key ();
527 Lisp_Object Qpolling_period;
529 /* List of absolute timers. Appears in order of next scheduled event. */
530 Lisp_Object Vtimer_list;
532 /* List of idle time timers. Appears in order of next scheduled event. */
533 Lisp_Object Vtimer_idle_list;
535 /* Incremented whenever a timer is run. */
536 int timers_run;
538 extern Lisp_Object Vprint_level, Vprint_length;
540 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
541 happens. */
542 EMACS_TIME *input_available_clear_time;
544 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
545 Default is 1 if INTERRUPT_INPUT is defined. */
546 int interrupt_input;
548 /* Nonzero while interrupts are temporarily deferred during redisplay. */
549 int interrupts_deferred;
551 /* Nonzero means use ^S/^Q for flow control. */
552 int flow_control;
554 /* Allow m- file to inhibit use of FIONREAD. */
555 #ifdef BROKEN_FIONREAD
556 #undef FIONREAD
557 #endif
559 /* We are unable to use interrupts if FIONREAD is not available,
560 so flush SIGIO so we won't try. */
561 #ifndef FIONREAD
562 #ifdef SIGIO
563 #undef SIGIO
564 #endif
565 #endif
567 /* If we support a window system, turn on the code to poll periodically
568 to detect C-g. It isn't actually used when doing interrupt input. */
569 #ifdef HAVE_WINDOW_SYSTEM
570 #define POLL_FOR_INPUT
571 #endif
573 /* Global variable declarations. */
575 /* Function for init_keyboard to call with no args (if nonzero). */
576 void (*keyboard_init_hook) ();
578 static int read_avail_input ();
579 static void get_input_pending ();
580 static int readable_events ();
581 static Lisp_Object read_char_x_menu_prompt ();
582 static Lisp_Object read_char_minibuf_menu_prompt ();
583 static Lisp_Object make_lispy_event ();
584 #ifdef HAVE_MOUSE
585 static Lisp_Object make_lispy_movement ();
586 #endif
587 static Lisp_Object modify_event_symbol ();
588 static Lisp_Object make_lispy_switch_frame ();
589 static int parse_solitary_modifier ();
590 static void save_getcjmp ();
591 static void restore_getcjmp ();
593 /* > 0 if we are to echo keystrokes. */
594 static int echo_keystrokes;
596 /* Nonzero means don't try to suspend even if the operating system seems
597 to support it. */
598 static int cannot_suspend;
600 #define min(a,b) ((a)<(b)?(a):(b))
601 #define max(a,b) ((a)>(b)?(a):(b))
603 /* Install the string STR as the beginning of the string of echoing,
604 so that it serves as a prompt for the next character.
605 Also start echoing. */
607 void
608 echo_prompt (str)
609 char *str;
611 int len = strlen (str);
613 if (len > ECHOBUFSIZE - 4)
614 len = ECHOBUFSIZE - 4;
615 bcopy (str, current_kboard->echobuf, len);
616 current_kboard->echoptr = current_kboard->echobuf + len;
617 *current_kboard->echoptr = '\0';
619 current_kboard->echo_after_prompt = len;
621 echo_now ();
624 /* Add C to the echo string, if echoing is going on.
625 C can be a character, which is printed prettily ("M-C-x" and all that
626 jazz), or a symbol, whose name is printed. */
628 void
629 echo_char (c)
630 Lisp_Object c;
632 extern char *push_key_description ();
634 if (current_kboard->immediate_echo)
636 char *ptr = current_kboard->echoptr;
638 if (ptr != current_kboard->echobuf)
639 *ptr++ = ' ';
641 /* If someone has passed us a composite event, use its head symbol. */
642 c = EVENT_HEAD (c);
644 if (INTEGERP (c))
646 if (ptr - current_kboard->echobuf > ECHOBUFSIZE - 6)
647 return;
649 ptr = push_key_description (XINT (c), ptr);
651 else if (SYMBOLP (c))
653 struct Lisp_String *name = XSYMBOL (c)->name;
654 if ((ptr - current_kboard->echobuf) + STRING_BYTES (name) + 4
655 > ECHOBUFSIZE)
656 return;
657 bcopy (name->data, ptr, STRING_BYTES (name));
658 ptr += STRING_BYTES (name);
661 if (current_kboard->echoptr == current_kboard->echobuf
662 && help_char_p (c))
664 strcpy (ptr, " (Type ? for further options)");
665 ptr += strlen (ptr);
668 *ptr = 0;
669 current_kboard->echoptr = ptr;
671 echo_now ();
675 /* Temporarily add a dash to the end of the echo string if it's not
676 empty, so that it serves as a mini-prompt for the very next character. */
678 void
679 echo_dash ()
681 if (!current_kboard->immediate_echo
682 && current_kboard->echoptr == current_kboard->echobuf)
683 return;
684 /* Do nothing if we just printed a prompt. */
685 if (current_kboard->echo_after_prompt
686 == current_kboard->echoptr - current_kboard->echobuf)
687 return;
688 /* Do nothing if not echoing at all. */
689 if (current_kboard->echoptr == 0)
690 return;
692 /* Put a dash at the end of the buffer temporarily,
693 but make it go away when the next character is added. */
694 current_kboard->echoptr[0] = '-';
695 current_kboard->echoptr[1] = 0;
697 echo_now ();
700 /* Display the current echo string, and begin echoing if not already
701 doing so. */
703 void
704 echo_now ()
706 if (!current_kboard->immediate_echo)
708 int i;
709 current_kboard->immediate_echo = 1;
711 for (i = 0; i < this_command_key_count; i++)
713 Lisp_Object c;
714 c = XVECTOR (this_command_keys)->contents[i];
715 if (! (EVENT_HAS_PARAMETERS (c)
716 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
717 echo_char (c);
719 echo_dash ();
722 echoing = 1;
723 message2_nolog (current_kboard->echobuf, strlen (current_kboard->echobuf),
724 ! NILP (current_buffer->enable_multibyte_characters));
726 echoing = 0;
728 if (waiting_for_input && !NILP (Vquit_flag))
729 quit_throw_to_read_char ();
732 /* Turn off echoing, for the start of a new command. */
734 void
735 cancel_echoing ()
737 current_kboard->immediate_echo = 0;
738 current_kboard->echoptr = current_kboard->echobuf;
739 current_kboard->echo_after_prompt = -1;
740 ok_to_echo_at_next_pause = 0;
743 /* Return the length of the current echo string. */
745 static int
746 echo_length ()
748 return current_kboard->echoptr - current_kboard->echobuf;
751 /* Truncate the current echo message to its first LEN chars.
752 This and echo_char get used by read_key_sequence when the user
753 switches frames while entering a key sequence. */
755 static void
756 echo_truncate (len)
757 int len;
759 current_kboard->echobuf[len] = '\0';
760 current_kboard->echoptr = current_kboard->echobuf + len;
761 truncate_echo_area (len);
765 /* Functions for manipulating this_command_keys. */
766 static void
767 add_command_key (key)
768 Lisp_Object key;
770 int size = XVECTOR (this_command_keys)->size;
772 /* If reset-this-command-length was called recently, obey it now.
773 See the doc string of that function for an explanation of why. */
774 if (before_command_restore_flag)
776 this_command_key_count = before_command_key_count_1;
777 if (this_command_key_count < this_single_command_key_start)
778 this_single_command_key_start = this_command_key_count;
779 echo_truncate (before_command_echo_length_1);
780 before_command_restore_flag = 0;
783 if (this_command_key_count >= size)
785 Lisp_Object new_keys;
787 new_keys = Fmake_vector (make_number (size * 2), Qnil);
788 bcopy (XVECTOR (this_command_keys)->contents,
789 XVECTOR (new_keys)->contents,
790 size * sizeof (Lisp_Object));
792 this_command_keys = new_keys;
795 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
798 Lisp_Object
799 recursive_edit_1 ()
801 int count = specpdl_ptr - specpdl;
802 Lisp_Object val;
804 if (command_loop_level > 0)
806 specbind (Qstandard_output, Qt);
807 specbind (Qstandard_input, Qt);
810 val = command_loop ();
811 if (EQ (val, Qt))
812 Fsignal (Qquit, Qnil);
813 /* Handle throw from read_minibuf when using minibuffer
814 while it's active but we're in another window. */
815 if (STRINGP (val))
816 Fsignal (Qerror, Fcons (val, Qnil));
818 return unbind_to (count, Qnil);
821 /* When an auto-save happens, record the "time", and don't do again soon. */
823 void
824 record_auto_save ()
826 last_auto_save = num_nonmacro_input_events;
829 /* Make an auto save happen as soon as possible at command level. */
831 void
832 force_auto_save_soon ()
834 last_auto_save = - auto_save_interval - 1;
836 record_asynch_buffer_change ();
839 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
840 "Invoke the editor command loop recursively.\n\
841 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
842 that tells this function to return.\n\
843 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
844 This function is called by the editor initialization to begin editing.")
847 int count = specpdl_ptr - specpdl;
848 Lisp_Object val;
850 command_loop_level++;
851 update_mode_lines = 1;
853 record_unwind_protect (recursive_edit_unwind,
854 (command_loop_level
855 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
856 ? Fcurrent_buffer ()
857 : Qnil);
858 recursive_edit_1 ();
859 return unbind_to (count, Qnil);
862 Lisp_Object
863 recursive_edit_unwind (buffer)
864 Lisp_Object buffer;
866 if (!NILP (buffer))
867 Fset_buffer (buffer);
869 command_loop_level--;
870 update_mode_lines = 1;
871 return Qnil;
874 static void
875 any_kboard_state ()
877 #ifdef MULTI_KBOARD
878 #if 0 /* Theory: if there's anything in Vunread_command_events,
879 it will right away be read by read_key_sequence,
880 and then if we do switch KBOARDS, it will go into the side
881 queue then. So we don't need to do anything special here -- rms. */
882 if (CONSP (Vunread_command_events))
884 current_kboard->kbd_queue
885 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
886 current_kboard->kbd_queue_has_data = 1;
888 Vunread_command_events = Qnil;
889 #endif
890 single_kboard = 0;
891 #endif
894 /* Switch to the single-kboard state, making current_kboard
895 the only KBOARD from which further input is accepted. */
897 void
898 single_kboard_state ()
900 #ifdef MULTI_KBOARD
901 single_kboard = 1;
902 #endif
905 /* Maintain a stack of kboards, so other parts of Emacs
906 can switch temporarily to the kboard of a given frame
907 and then revert to the previous status. */
909 struct kboard_stack
911 KBOARD *kboard;
912 struct kboard_stack *next;
915 static struct kboard_stack *kboard_stack;
917 void
918 push_frame_kboard (f)
919 FRAME_PTR f;
921 #ifdef MULTI_KBOARD
922 struct kboard_stack *p
923 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
925 p->next = kboard_stack;
926 p->kboard = current_kboard;
927 kboard_stack = p;
929 current_kboard = FRAME_KBOARD (f);
930 #endif
933 void
934 pop_frame_kboard ()
936 #ifdef MULTI_KBOARD
937 struct kboard_stack *p = kboard_stack;
938 current_kboard = p->kboard;
939 kboard_stack = p->next;
940 xfree (p);
941 #endif
944 /* Handle errors that are not handled at inner levels
945 by printing an error message and returning to the editor command loop. */
947 Lisp_Object
948 cmd_error (data)
949 Lisp_Object data;
951 Lisp_Object old_level, old_length;
952 char macroerror[50];
954 if (!NILP (executing_macro))
956 if (executing_macro_iterations == 1)
957 sprintf (macroerror, "After 1 kbd macro iteration: ");
958 else
959 sprintf (macroerror, "After %d kbd macro iterations: ",
960 executing_macro_iterations);
962 else
963 *macroerror = 0;
965 Vstandard_output = Qt;
966 Vstandard_input = Qt;
967 Vexecuting_macro = Qnil;
968 executing_macro = Qnil;
969 current_kboard->Vprefix_arg = Qnil;
970 current_kboard->Vlast_prefix_arg = Qnil;
971 cancel_echoing ();
973 /* Avoid unquittable loop if data contains a circular list. */
974 old_level = Vprint_level;
975 old_length = Vprint_length;
976 XSETFASTINT (Vprint_level, 10);
977 XSETFASTINT (Vprint_length, 10);
978 cmd_error_internal (data, macroerror);
979 Vprint_level = old_level;
980 Vprint_length = old_length;
982 Vquit_flag = Qnil;
984 Vinhibit_quit = Qnil;
985 #ifdef MULTI_KBOARD
986 any_kboard_state ();
987 #endif
989 return make_number (0);
992 /* Take actions on handling an error. DATA is the data that describes
993 the error.
995 CONTEXT is a C-string containing ASCII characters only which
996 describes the context in which the error happened. If we need to
997 generalize CONTEXT to allow multibyte characters, make it a Lisp
998 string. */
1000 void
1001 cmd_error_internal (data, context)
1002 Lisp_Object data;
1003 char *context;
1005 Lisp_Object stream;
1007 Vquit_flag = Qnil;
1008 Vinhibit_quit = Qt;
1009 echo_area_glyphs = 0;
1011 /* If the window system or terminal frame hasn't been initialized
1012 yet, or we're not interactive, it's best to dump this message out
1013 to stderr and exit. */
1014 if (! FRAME_MESSAGE_BUF (selected_frame)
1015 || noninteractive)
1016 stream = Qexternal_debugging_output;
1017 else
1019 Fdiscard_input ();
1020 bitch_at_user ();
1021 stream = Qt;
1024 if (context != 0)
1025 write_string_1 (context, -1, stream);
1027 print_error_message (data, stream);
1029 /* If the window system or terminal frame hasn't been initialized
1030 yet, or we're in -batch mode, this error should cause Emacs to exit. */
1031 if (! FRAME_MESSAGE_BUF (selected_frame)
1032 || noninteractive)
1034 Fterpri (stream);
1035 Fkill_emacs (make_number (-1));
1039 Lisp_Object command_loop_1 ();
1040 Lisp_Object command_loop_2 ();
1041 Lisp_Object top_level_1 ();
1043 /* Entry to editor-command-loop.
1044 This level has the catches for exiting/returning to editor command loop.
1045 It returns nil to exit recursive edit, t to abort it. */
1047 Lisp_Object
1048 command_loop ()
1050 if (command_loop_level > 0 || minibuf_level > 0)
1052 Lisp_Object val = internal_catch (Qexit, command_loop_2, Qnil);
1053 executing_macro = Qnil;
1054 return val;
1056 else
1057 while (1)
1059 internal_catch (Qtop_level, top_level_1, Qnil);
1060 internal_catch (Qtop_level, command_loop_2, Qnil);
1061 executing_macro = Qnil;
1063 /* End of file in -batch run causes exit here. */
1064 if (noninteractive)
1065 Fkill_emacs (Qt);
1069 /* Here we catch errors in execution of commands within the
1070 editing loop, and reenter the editing loop.
1071 When there is an error, cmd_error runs and returns a non-nil
1072 value to us. A value of nil means that cmd_loop_1 itself
1073 returned due to end of file (or end of kbd macro). */
1075 Lisp_Object
1076 command_loop_2 ()
1078 register Lisp_Object val;
1081 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
1082 while (!NILP (val));
1084 return Qnil;
1087 Lisp_Object
1088 top_level_2 ()
1090 return Feval (Vtop_level);
1093 Lisp_Object
1094 top_level_1 ()
1096 /* On entry to the outer level, run the startup file */
1097 if (!NILP (Vtop_level))
1098 internal_condition_case (top_level_2, Qerror, cmd_error);
1099 else if (!NILP (Vpurify_flag))
1100 message ("Bare impure Emacs (standard Lisp code not loaded)");
1101 else
1102 message ("Bare Emacs (standard Lisp code not loaded)");
1103 return Qnil;
1106 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1107 "Exit all recursive editing levels.")
1110 Fthrow (Qtop_level, Qnil);
1113 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1114 "Exit from the innermost recursive edit or minibuffer.")
1117 if (command_loop_level > 0 || minibuf_level > 0)
1118 Fthrow (Qexit, Qnil);
1120 error ("No recursive edit is in progress");
1123 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1124 "Abort the command that requested this recursive edit or minibuffer input.")
1127 if (command_loop_level > 0 || minibuf_level > 0)
1128 Fthrow (Qexit, Qt);
1130 error ("No recursive edit is in progress");
1133 /* This is the actual command reading loop,
1134 sans error-handling encapsulation. */
1136 Lisp_Object Fcommand_execute ();
1137 static int read_key_sequence ();
1138 void safe_run_hooks ();
1140 Lisp_Object
1141 command_loop_1 ()
1143 Lisp_Object cmd, tem;
1144 int lose, lose2;
1145 int nonundocount;
1146 Lisp_Object keybuf[30];
1147 int i;
1148 int no_redisplay;
1149 int no_direct;
1150 int prev_modiff;
1151 struct buffer *prev_buffer;
1152 #ifdef MULTI_KBOARD
1153 int was_locked = single_kboard;
1154 #endif
1156 current_kboard->Vprefix_arg = Qnil;
1157 current_kboard->Vlast_prefix_arg = Qnil;
1158 Vdeactivate_mark = Qnil;
1159 waiting_for_input = 0;
1160 cancel_echoing ();
1162 nonundocount = 0;
1163 no_redisplay = 0;
1164 this_command_key_count = 0;
1165 this_single_command_key_start = 0;
1167 /* Make sure this hook runs after commands that get errors and
1168 throw to top level. */
1169 /* Note that the value cell will never directly contain nil
1170 if the symbol is a local variable. */
1171 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1172 safe_run_hooks (Qpost_command_hook);
1174 if (!NILP (Vdeferred_action_list))
1175 call0 (Vdeferred_action_function);
1177 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1179 if (NILP (Vunread_command_events)
1180 && NILP (Vunread_input_method_events)
1181 && NILP (Vunread_post_input_method_events)
1182 && NILP (Vexecuting_macro)
1183 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1184 safe_run_hooks (Qpost_command_idle_hook);
1187 /* Do this after running Vpost_command_hook, for consistency. */
1188 current_kboard->Vlast_command = this_command;
1189 current_kboard->Vreal_last_command = this_command;
1191 while (1)
1193 /* Make sure the current window's buffer is selected. */
1194 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1195 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1197 /* Display any malloc warning that just came out. Use while because
1198 displaying one warning can cause another. */
1200 while (pending_malloc_warning)
1201 display_malloc_warning ();
1203 no_direct = 0;
1205 Vdeactivate_mark = Qnil;
1207 /* If minibuffer on and echo area in use,
1208 wait 2 sec and redraw minibuffer. */
1210 if (minibuf_level && echo_area_glyphs
1211 && EQ (minibuf_window, echo_area_window))
1213 /* Bind inhibit-quit to t so that C-g gets read in
1214 rather than quitting back to the minibuffer. */
1215 int count = specpdl_ptr - specpdl;
1216 specbind (Qinhibit_quit, Qt);
1218 Fsit_for (make_number (2), Qnil, Qnil);
1219 /* Clear the echo area. */
1220 message2 (0, 0, 0);
1221 safe_run_hooks (Qecho_area_clear_hook);
1223 unbind_to (count, Qnil);
1225 /* If a C-g came in before, treat it as input now. */
1226 if (!NILP (Vquit_flag))
1228 Vquit_flag = Qnil;
1229 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1233 #ifdef C_ALLOCA
1234 alloca (0); /* Cause a garbage collection now */
1235 /* Since we can free the most stuff here. */
1236 #endif /* C_ALLOCA */
1238 #if 0
1239 /* Select the frame that the last event came from. Usually,
1240 switch-frame events will take care of this, but if some lisp
1241 code swallows a switch-frame event, we'll fix things up here.
1242 Is this a good idea? */
1243 if (FRAMEP (internal_last_event_frame)
1244 && XFRAME (internal_last_event_frame) != selected_frame)
1245 Fselect_frame (internal_last_event_frame, Qnil);
1246 #endif
1247 /* If it has changed current-menubar from previous value,
1248 really recompute the menubar from the value. */
1249 if (! NILP (Vlucid_menu_bar_dirty_flag)
1250 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1251 call0 (Qrecompute_lucid_menubar);
1253 before_command_key_count = this_command_key_count;
1254 before_command_echo_length = echo_length ();
1256 this_command = Qnil;
1258 /* Read next key sequence; i gets its length. */
1259 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1260 Qnil, 0, 1, 1);
1262 /* A filter may have run while we were reading the input. */
1263 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1264 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1266 ++num_input_keys;
1268 /* Now we have read a key sequence of length I,
1269 or else I is 0 and we found end of file. */
1271 if (i == 0) /* End of file -- happens only in */
1272 return Qnil; /* a kbd macro, at the end. */
1273 /* -1 means read_key_sequence got a menu that was rejected.
1274 Just loop around and read another command. */
1275 if (i == -1)
1277 cancel_echoing ();
1278 this_command_key_count = 0;
1279 this_single_command_key_start = 0;
1280 goto finalize;
1283 last_command_char = keybuf[i - 1];
1285 /* If the previous command tried to force a specific window-start,
1286 forget about that, in case this command moves point far away
1287 from that position. But also throw away beg_unchanged and
1288 end_unchanged information in that case, so that redisplay will
1289 update the whole window properly. */
1290 if (!NILP (XWINDOW (selected_window)->force_start))
1292 XWINDOW (selected_window)->force_start = Qnil;
1293 beg_unchanged = end_unchanged = 0;
1296 cmd = read_key_sequence_cmd;
1297 if (!NILP (Vexecuting_macro))
1299 if (!NILP (Vquit_flag))
1301 Vexecuting_macro = Qt;
1302 QUIT; /* Make some noise. */
1303 /* Will return since macro now empty. */
1307 /* Do redisplay processing after this command except in special
1308 cases identified below that set no_redisplay to 1.
1309 (actually, there's currently no way to prevent the redisplay,
1310 and no_redisplay is ignored.
1311 Perhaps someday we will really implement it.) */
1312 no_redisplay = 0;
1314 prev_buffer = current_buffer;
1315 prev_modiff = MODIFF;
1316 last_point_position = PT;
1317 XSETBUFFER (last_point_position_buffer, prev_buffer);
1319 /* Execute the command. */
1321 this_command = cmd;
1322 /* Note that the value cell will never directly contain nil
1323 if the symbol is a local variable. */
1324 if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
1325 safe_run_hooks (Qpre_command_hook);
1327 if (NILP (this_command))
1329 /* nil means key is undefined. */
1330 bitch_at_user ();
1331 current_kboard->defining_kbd_macro = Qnil;
1332 update_mode_lines = 1;
1333 current_kboard->Vprefix_arg = Qnil;
1335 else
1337 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1339 /* In case we jump to directly_done. */
1340 Vcurrent_prefix_arg = current_kboard->Vprefix_arg;
1342 /* Recognize some common commands in common situations and
1343 do them directly. */
1344 if (EQ (this_command, Qforward_char) && PT < ZV)
1346 struct Lisp_Char_Table *dp
1347 = window_display_table (XWINDOW (selected_window));
1348 lose = FETCH_CHAR (PT_BYTE);
1349 SET_PT (PT + 1);
1350 if ((dp
1351 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1352 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1353 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1354 && (lose >= 0x20 && lose < 0x7f)))
1355 : (lose >= 0x20 && lose < 0x7f))
1356 /* To extract the case of continuation on
1357 wide-column characters. */
1358 && (WIDTH_BY_CHAR_HEAD (FETCH_BYTE (PT_BYTE)) == 1)
1359 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1360 >= MODIFF)
1361 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1362 >= OVERLAY_MODIFF)
1363 && (XFASTINT (XWINDOW (selected_window)->last_point)
1364 == PT - 1)
1365 && !windows_or_buffers_changed
1366 && EQ (current_buffer->selective_display, Qnil)
1367 && !detect_input_pending ()
1368 && NILP (XWINDOW (selected_window)->column_number_displayed)
1369 && NILP (Vexecuting_macro))
1370 no_redisplay = direct_output_forward_char (1);
1371 goto directly_done;
1373 else if (EQ (this_command, Qbackward_char) && PT > BEGV)
1375 struct Lisp_Char_Table *dp
1376 = window_display_table (XWINDOW (selected_window));
1377 SET_PT (PT - 1);
1378 lose = FETCH_CHAR (PT_BYTE);
1379 if ((dp
1380 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1381 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1382 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1383 && (lose >= 0x20 && lose < 0x7f)))
1384 : (lose >= 0x20 && lose < 0x7f))
1385 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1386 >= MODIFF)
1387 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1388 >= OVERLAY_MODIFF)
1389 && (XFASTINT (XWINDOW (selected_window)->last_point)
1390 == PT + 1)
1391 && !windows_or_buffers_changed
1392 && EQ (current_buffer->selective_display, Qnil)
1393 && !detect_input_pending ()
1394 && NILP (XWINDOW (selected_window)->column_number_displayed)
1395 && NILP (Vexecuting_macro))
1396 no_redisplay = direct_output_forward_char (-1);
1397 goto directly_done;
1399 else if (EQ (this_command, Qself_insert_command)
1400 /* Try this optimization only on ascii keystrokes. */
1401 && INTEGERP (last_command_char))
1403 unsigned int c = XINT (last_command_char);
1404 int value;
1406 if (NILP (Vexecuting_macro)
1407 && !EQ (minibuf_window, selected_window))
1409 if (!nonundocount || nonundocount >= 20)
1411 Fundo_boundary ();
1412 nonundocount = 0;
1414 nonundocount++;
1416 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1417 < MODIFF)
1418 || (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1419 < OVERLAY_MODIFF)
1420 || (XFASTINT (XWINDOW (selected_window)->last_point)
1421 != PT)
1422 || MODIFF <= SAVE_MODIFF
1423 || windows_or_buffers_changed
1424 || !EQ (current_buffer->selective_display, Qnil)
1425 || detect_input_pending ()
1426 || !NILP (XWINDOW (selected_window)->column_number_displayed)
1427 || !NILP (Vexecuting_macro));
1428 value = internal_self_insert (c, 0);
1429 if (value)
1430 lose = 1;
1431 if (value == 2)
1432 nonundocount = 0;
1434 if (!lose
1435 && (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n'))
1437 struct Lisp_Char_Table *dp
1438 = window_display_table (XWINDOW (selected_window));
1439 int lose = c;
1441 /* Add the offset to the character, for Finsert_char.
1442 We pass internal_self_insert the unmodified character
1443 because it itself does this offsetting. */
1444 if (! NILP (current_buffer->enable_multibyte_characters))
1445 lose = unibyte_char_to_multibyte (lose);
1447 if (dp)
1449 Lisp_Object obj;
1451 obj = DISP_CHAR_VECTOR (dp, lose);
1452 if (NILP (obj))
1454 /* Do it only for char codes
1455 that by default display as themselves. */
1456 if (lose >= 0x20 && lose <= 0x7e)
1457 no_redisplay = direct_output_for_insert (lose);
1459 else if (VECTORP (obj)
1460 && XVECTOR (obj)->size == 1
1461 && (obj = XVECTOR (obj)->contents[0],
1462 INTEGERP (obj))
1463 /* Insist face not specified in glyph. */
1464 && (XINT (obj) & ((-1) << 8)) == 0)
1465 no_redisplay
1466 = direct_output_for_insert (XINT (obj));
1468 else
1470 if (lose >= 0x20 && lose <= 0x7e)
1471 no_redisplay = direct_output_for_insert (lose);
1474 goto directly_done;
1478 /* Here for a command that isn't executed directly */
1480 nonundocount = 0;
1481 if (NILP (current_kboard->Vprefix_arg))
1482 Fundo_boundary ();
1483 Fcommand_execute (this_command, Qnil, Qnil, Qnil);
1485 directly_done: ;
1486 current_kboard->Vlast_prefix_arg = Vcurrent_prefix_arg;
1488 /* Note that the value cell will never directly contain nil
1489 if the symbol is a local variable. */
1490 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1491 safe_run_hooks (Qpost_command_hook);
1493 if (!NILP (Vdeferred_action_list))
1494 safe_run_hooks (Qdeferred_action_function);
1496 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1498 if (NILP (Vunread_command_events)
1499 && NILP (Vunread_input_method_events)
1500 && NILP (Vunread_post_input_method_events)
1501 && NILP (Vexecuting_macro)
1502 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1503 safe_run_hooks (Qpost_command_idle_hook);
1506 /* If there is a prefix argument,
1507 1) We don't want Vlast_command to be ``universal-argument''
1508 (that would be dumb), so don't set Vlast_command,
1509 2) we want to leave echoing on so that the prefix will be
1510 echoed as part of this key sequence, so don't call
1511 cancel_echoing, and
1512 3) we want to leave this_command_key_count non-zero, so that
1513 read_char will realize that it is re-reading a character, and
1514 not echo it a second time.
1516 If the command didn't actually create a prefix arg,
1517 but is merely a frame event that is transparent to prefix args,
1518 then the above doesn't apply. */
1519 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1521 current_kboard->Vlast_command = this_command;
1522 current_kboard->Vreal_last_command = this_command;
1523 cancel_echoing ();
1524 this_command_key_count = 0;
1525 this_single_command_key_start = 0;
1528 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1530 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1532 current_buffer->mark_active = Qnil;
1533 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1535 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1536 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1539 finalize:
1540 /* Install chars successfully executed in kbd macro. */
1542 if (!NILP (current_kboard->defining_kbd_macro)
1543 && NILP (current_kboard->Vprefix_arg))
1544 finalize_kbd_macro_chars ();
1546 #ifdef MULTI_KBOARD
1547 if (!was_locked)
1548 any_kboard_state ();
1549 #endif
1553 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1555 static Lisp_Object
1556 safe_run_hooks_1 (hook)
1557 Lisp_Object hook;
1559 return call1 (Vrun_hooks, Vinhibit_quit);
1562 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1564 static Lisp_Object
1565 safe_run_hooks_error (data)
1566 Lisp_Object data;
1568 Fset (Vinhibit_quit, Qnil);
1571 /* If we get an error while running the hook, cause the hook variable
1572 to be nil. Also inhibit quits, so that C-g won't cause the hook
1573 to mysteriously evaporate. */
1575 void
1576 safe_run_hooks (hook)
1577 Lisp_Object hook;
1579 Lisp_Object value;
1580 int count = specpdl_ptr - specpdl;
1581 specbind (Qinhibit_quit, hook);
1583 internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
1585 unbind_to (count, Qnil);
1588 /* Number of seconds between polling for input. */
1589 int polling_period;
1591 /* Nonzero means polling for input is temporarily suppressed. */
1592 int poll_suppress_count;
1594 /* Nonzero if polling_for_input is actually being used. */
1595 int polling_for_input;
1597 #ifdef POLL_FOR_INPUT
1599 /* Handle an alarm once each second and read pending input
1600 so as to handle a C-g if it comces in. */
1602 SIGTYPE
1603 input_poll_signal (signalnum) /* If we don't have an argument, */
1604 int signalnum; /* some compilers complain in signal calls. */
1606 /* This causes the call to start_polling at the end
1607 to do its job. It also arranges for a quit or error
1608 from within read_avail_input to resume polling. */
1609 poll_suppress_count++;
1610 if (interrupt_input_blocked == 0
1611 && !waiting_for_input)
1612 read_avail_input (0);
1613 /* Turn on the SIGALRM handler and request another alarm. */
1614 start_polling ();
1617 #endif
1619 /* Begin signals to poll for input, if they are appropriate.
1620 This function is called unconditionally from various places. */
1622 void
1623 start_polling ()
1625 #ifdef POLL_FOR_INPUT
1626 if (read_socket_hook && !interrupt_input)
1628 poll_suppress_count--;
1629 if (poll_suppress_count == 0)
1631 signal (SIGALRM, input_poll_signal);
1632 polling_for_input = 1;
1633 alarm (polling_period);
1636 #endif
1639 /* Nonzero if we are using polling to handle input asynchronously. */
1642 input_polling_used ()
1644 #ifdef POLL_FOR_INPUT
1645 return read_socket_hook && !interrupt_input;
1646 #else
1647 return 0;
1648 #endif
1651 /* Turn off polling. */
1653 void
1654 stop_polling ()
1656 #ifdef POLL_FOR_INPUT
1657 if (read_socket_hook && !interrupt_input)
1659 if (poll_suppress_count == 0)
1661 polling_for_input = 0;
1662 alarm (0);
1664 poll_suppress_count++;
1666 #endif
1669 /* Set the value of poll_suppress_count to COUNT
1670 and start or stop polling accordingly. */
1672 void
1673 set_poll_suppress_count (count)
1674 int count;
1676 #ifdef POLL_FOR_INPUT
1677 if (count == 0 && poll_suppress_count != 0)
1679 poll_suppress_count = 1;
1680 start_polling ();
1682 else if (count != 0 && poll_suppress_count == 0)
1684 stop_polling ();
1686 poll_suppress_count = count;
1687 #endif
1690 /* Bind polling_period to a value at least N.
1691 But don't decrease it. */
1693 void
1694 bind_polling_period (n)
1695 int n;
1697 #ifdef POLL_FOR_INPUT
1698 int new = polling_period;
1700 if (n > new)
1701 new = n;
1703 stop_polling ();
1704 specbind (Qpolling_period, make_number (new));
1705 /* Start a new alarm with the new period. */
1706 start_polling ();
1707 #endif
1710 /* Apply the control modifier to CHARACTER. */
1713 make_ctrl_char (c)
1714 int c;
1716 /* Save the upper bits here. */
1717 int upper = c & ~0177;
1719 c &= 0177;
1721 /* Everything in the columns containing the upper-case letters
1722 denotes a control character. */
1723 if (c >= 0100 && c < 0140)
1725 int oc = c;
1726 c &= ~0140;
1727 /* Set the shift modifier for a control char
1728 made from a shifted letter. But only for letters! */
1729 if (oc >= 'A' && oc <= 'Z')
1730 c |= shift_modifier;
1733 /* The lower-case letters denote control characters too. */
1734 else if (c >= 'a' && c <= 'z')
1735 c &= ~0140;
1737 /* Include the bits for control and shift
1738 only if the basic ASCII code can't indicate them. */
1739 else if (c >= ' ')
1740 c |= ctrl_modifier;
1742 /* Replace the high bits. */
1743 c |= (upper & ~ctrl_modifier);
1745 return c;
1750 /* Input of single characters from keyboard */
1752 Lisp_Object print_help ();
1753 static Lisp_Object kbd_buffer_get_event ();
1754 static void record_char ();
1756 #ifdef MULTI_KBOARD
1757 static jmp_buf wrong_kboard_jmpbuf;
1758 #endif
1760 /* read a character from the keyboard; call the redisplay if needed */
1761 /* commandflag 0 means do not do auto-saving, but do do redisplay.
1762 -1 means do not do redisplay, but do do autosaving.
1763 1 means do both. */
1765 /* The arguments MAPS and NMAPS are for menu prompting.
1766 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1768 PREV_EVENT is the previous input event, or nil if we are reading
1769 the first event of a key sequence.
1771 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
1772 if we used a mouse menu to read the input, or zero otherwise. If
1773 USED_MOUSE_MENU is null, we don't dereference it.
1775 Value is t if we showed a menu and the user rejected it. */
1777 Lisp_Object
1778 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
1779 int commandflag;
1780 int nmaps;
1781 Lisp_Object *maps;
1782 Lisp_Object prev_event;
1783 int *used_mouse_menu;
1785 Lisp_Object c;
1786 int count;
1787 jmp_buf local_getcjmp;
1788 jmp_buf save_jump;
1789 int key_already_recorded = 0;
1790 Lisp_Object tem, save;
1791 Lisp_Object also_record;
1792 int reread;
1793 struct gcpro gcpro1;
1795 also_record = Qnil;
1797 before_command_key_count = this_command_key_count;
1798 before_command_echo_length = echo_length ();
1799 c = Qnil;
1801 GCPRO1 (c);
1803 retry:
1805 reread = 0;
1806 if (CONSP (Vunread_post_input_method_events))
1808 c = XCONS (Vunread_post_input_method_events)->car;
1809 Vunread_post_input_method_events
1810 = XCONS (Vunread_post_input_method_events)->cdr;
1812 /* Undo what read_char_x_menu_prompt did when it unread
1813 additional keys returned by Fx_popup_menu. */
1814 if (CONSP (c)
1815 && (SYMBOLP (XCONS (c)->car) || INTEGERP (XCONS (c)->car))
1816 && NILP (XCONS (c)->cdr))
1817 c = XCONS (c)->car;
1819 reread = 1;
1820 goto reread_first;
1823 if (unread_command_char != -1)
1825 XSETINT (c, unread_command_char);
1826 unread_command_char = -1;
1828 reread = 1;
1829 goto reread_first;
1832 if (CONSP (Vunread_command_events))
1834 c = XCONS (Vunread_command_events)->car;
1835 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
1837 /* Undo what read_char_x_menu_prompt did when it unread
1838 additional keys returned by Fx_popup_menu. */
1839 if (CONSP (c)
1840 && (SYMBOLP (XCONS (c)->car) || INTEGERP (XCONS (c)->car))
1841 && NILP (XCONS (c)->cdr))
1842 c = XCONS (c)->car;
1844 reread = 1;
1845 goto reread_for_input_method;
1848 if (CONSP (Vunread_input_method_events))
1850 c = XCONS (Vunread_input_method_events)->car;
1851 Vunread_input_method_events = XCONS (Vunread_input_method_events)->cdr;
1853 /* Undo what read_char_x_menu_prompt did when it unread
1854 additional keys returned by Fx_popup_menu. */
1855 if (CONSP (c)
1856 && (SYMBOLP (XCONS (c)->car) || INTEGERP (XCONS (c)->car))
1857 && NILP (XCONS (c)->cdr))
1858 c = XCONS (c)->car;
1859 reread = 1;
1860 goto reread_for_input_method;
1863 /* If there is no function key translated before
1864 reset-this-command-lengths takes effect, forget about it. */
1865 before_command_restore_flag = 0;
1867 if (!NILP (Vexecuting_macro))
1869 /* We set this to Qmacro; since that's not a frame, nobody will
1870 try to switch frames on us, and the selected window will
1871 remain unchanged.
1873 Since this event came from a macro, it would be misleading to
1874 leave internal_last_event_frame set to wherever the last
1875 real event came from. Normally, a switch-frame event selects
1876 internal_last_event_frame after each command is read, but
1877 events read from a macro should never cause a new frame to be
1878 selected. */
1879 Vlast_event_frame = internal_last_event_frame = Qmacro;
1881 /* Exit the macro if we are at the end.
1882 Also, some things replace the macro with t
1883 to force an early exit. */
1884 if (EQ (Vexecuting_macro, Qt)
1885 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
1887 XSETINT (c, -1);
1888 RETURN_UNGCPRO (c);
1891 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
1892 if (STRINGP (Vexecuting_macro)
1893 && (XINT (c) & 0x80))
1894 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
1896 executing_macro_index++;
1898 goto from_macro;
1901 if (!NILP (unread_switch_frame))
1903 c = unread_switch_frame;
1904 unread_switch_frame = Qnil;
1906 /* This event should make it into this_command_keys, and get echoed
1907 again, so we do not set `reread'. */
1908 goto reread_first;
1911 /* if redisplay was requested */
1912 if (commandflag >= 0)
1914 /* If there is pending input, process any events which are not
1915 user-visible, such as X selection_request events. */
1916 if (input_pending
1917 || detect_input_pending_run_timers (0))
1918 swallow_events (0); /* may clear input_pending */
1920 /* Redisplay if no pending input. */
1921 while (!input_pending)
1923 redisplay ();
1925 if (!input_pending)
1926 /* Normal case: no input arrived during redisplay. */
1927 break;
1929 /* Input arrived and pre-empted redisplay.
1930 Process any events which are not user-visible. */
1931 swallow_events (0);
1932 /* If that cleared input_pending, try again to redisplay. */
1936 /* Message turns off echoing unless more keystrokes turn it on again. */
1937 if (echo_area_glyphs && *echo_area_glyphs
1938 && echo_area_glyphs != current_kboard->echobuf
1939 && ok_to_echo_at_next_pause != echo_area_glyphs)
1940 cancel_echoing ();
1941 else
1942 /* If already echoing, continue. */
1943 echo_dash ();
1945 /* Try reading a character via menu prompting in the minibuf.
1946 Try this before the sit-for, because the sit-for
1947 would do the wrong thing if we are supposed to do
1948 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
1949 after a mouse event so don't try a minibuf menu. */
1950 c = Qnil;
1951 if (nmaps > 0 && INTERACTIVE
1952 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
1953 /* Don't bring up a menu if we already have another event. */
1954 && NILP (Vunread_command_events)
1955 && unread_command_char < 0
1956 && !detect_input_pending_run_timers (0))
1958 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
1959 if (! NILP (c))
1961 key_already_recorded = 1;
1962 goto non_reread_1;
1966 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
1967 We will do that below, temporarily for short sections of code,
1968 when appropriate. local_getcjmp must be in effect
1969 around any call to sit_for or kbd_buffer_get_event;
1970 it *must not* be in effect when we call redisplay. */
1972 if (_setjmp (local_getcjmp))
1974 XSETINT (c, quit_char);
1975 XSETFRAME (internal_last_event_frame, selected_frame);
1976 Vlast_event_frame = internal_last_event_frame;
1977 /* If we report the quit char as an event,
1978 don't do so more than once. */
1979 if (!NILP (Vinhibit_quit))
1980 Vquit_flag = Qnil;
1982 #ifdef MULTI_KBOARD
1984 KBOARD *kb = FRAME_KBOARD (selected_frame);
1985 if (kb != current_kboard)
1987 Lisp_Object *tailp = &kb->kbd_queue;
1988 /* We shouldn't get here if we were in single-kboard mode! */
1989 if (single_kboard)
1990 abort ();
1991 while (CONSP (*tailp))
1992 tailp = &XCONS (*tailp)->cdr;
1993 if (!NILP (*tailp))
1994 abort ();
1995 *tailp = Fcons (c, Qnil);
1996 kb->kbd_queue_has_data = 1;
1997 current_kboard = kb;
1998 /* This is going to exit from read_char
1999 so we had better get rid of this frame's stuff. */
2000 UNGCPRO;
2001 longjmp (wrong_kboard_jmpbuf, 1);
2004 #endif
2005 goto non_reread;
2008 timer_start_idle ();
2010 /* If in middle of key sequence and minibuffer not active,
2011 start echoing if enough time elapses. */
2013 if (minibuf_level == 0 && !current_kboard->immediate_echo
2014 && this_command_key_count > 0
2015 && ! noninteractive
2016 && echo_keystrokes > 0
2017 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0
2018 || ok_to_echo_at_next_pause == echo_area_glyphs))
2020 Lisp_Object tem0;
2022 /* After a mouse event, start echoing right away.
2023 This is because we are probably about to display a menu,
2024 and we don't want to delay before doing so. */
2025 if (EVENT_HAS_PARAMETERS (prev_event))
2026 echo_now ();
2027 else
2029 save_getcjmp (save_jump);
2030 restore_getcjmp (local_getcjmp);
2031 tem0 = sit_for (echo_keystrokes, 0, 1, 1, 0);
2032 restore_getcjmp (save_jump);
2033 if (EQ (tem0, Qt)
2034 && ! CONSP (Vunread_command_events))
2035 echo_now ();
2039 /* Maybe auto save due to number of keystrokes. */
2041 if (commandflag != 0
2042 && auto_save_interval > 0
2043 && num_nonmacro_input_events - last_auto_save > max (auto_save_interval, 20)
2044 && !detect_input_pending_run_timers (0))
2046 Fdo_auto_save (Qnil, Qnil);
2047 /* Hooks can actually change some buffers in auto save. */
2048 redisplay ();
2051 /* Try reading using an X menu.
2052 This is never confused with reading using the minibuf
2053 because the recursive call of read_char in read_char_minibuf_menu_prompt
2054 does not pass on any keymaps. */
2056 if (nmaps > 0 && INTERACTIVE
2057 && !NILP (prev_event)
2058 && EVENT_HAS_PARAMETERS (prev_event)
2059 && !EQ (XCONS (prev_event)->car, Qmenu_bar)
2060 /* Don't bring up a menu if we already have another event. */
2061 && NILP (Vunread_command_events)
2062 && unread_command_char < 0)
2064 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
2066 /* Now that we have read an event, Emacs is not idle. */
2067 timer_stop_idle ();
2069 RETURN_UNGCPRO (c);
2072 /* Maybe autosave and/or garbage collect due to idleness. */
2074 if (INTERACTIVE && NILP (c))
2076 int delay_level, buffer_size;
2078 /* Slow down auto saves logarithmically in size of current buffer,
2079 and garbage collect while we're at it. */
2080 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
2081 last_non_minibuf_size = Z - BEG;
2082 buffer_size = (last_non_minibuf_size >> 8) + 1;
2083 delay_level = 0;
2084 while (buffer_size > 64)
2085 delay_level++, buffer_size -= buffer_size >> 2;
2086 if (delay_level < 4) delay_level = 4;
2087 /* delay_level is 4 for files under around 50k, 7 at 100k,
2088 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
2090 /* Auto save if enough time goes by without input. */
2091 if (commandflag != 0
2092 && num_nonmacro_input_events > last_auto_save
2093 && INTEGERP (Vauto_save_timeout)
2094 && XINT (Vauto_save_timeout) > 0)
2096 Lisp_Object tem0;
2098 save_getcjmp (save_jump);
2099 restore_getcjmp (local_getcjmp);
2100 tem0 = sit_for (delay_level * XFASTINT (Vauto_save_timeout) / 4,
2101 0, 1, 1, 0);
2102 restore_getcjmp (save_jump);
2104 if (EQ (tem0, Qt)
2105 && ! CONSP (Vunread_command_events))
2107 Fdo_auto_save (Qnil, Qnil);
2109 /* If we have auto-saved and there is still no input
2110 available, garbage collect if there has been enough
2111 consing going on to make it worthwhile. */
2112 if (!detect_input_pending_run_timers (0)
2113 && consing_since_gc > gc_cons_threshold / 2)
2114 Fgarbage_collect ();
2116 redisplay ();
2121 /* If this has become non-nil here, it has been set by a timer
2122 or sentinel or filter. */
2123 if (CONSP (Vunread_command_events))
2125 c = XCONS (Vunread_command_events)->car;
2126 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
2129 /* Read something from current KBOARD's side queue, if possible. */
2131 if (NILP (c))
2133 if (current_kboard->kbd_queue_has_data)
2135 if (!CONSP (current_kboard->kbd_queue))
2136 abort ();
2137 c = XCONS (current_kboard->kbd_queue)->car;
2138 current_kboard->kbd_queue
2139 = XCONS (current_kboard->kbd_queue)->cdr;
2140 if (NILP (current_kboard->kbd_queue))
2141 current_kboard->kbd_queue_has_data = 0;
2142 input_pending = readable_events (0);
2143 if (EVENT_HAS_PARAMETERS (c)
2144 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
2145 internal_last_event_frame = XCONS (XCONS (c)->cdr)->car;
2146 Vlast_event_frame = internal_last_event_frame;
2150 #ifdef MULTI_KBOARD
2151 /* If current_kboard's side queue is empty check the other kboards.
2152 If one of them has data that we have not yet seen here,
2153 switch to it and process the data waiting for it.
2155 Note: if the events queued up for another kboard
2156 have already been seen here, and therefore are not a complete command,
2157 the kbd_queue_has_data field is 0, so we skip that kboard here.
2158 That's to avoid an infinite loop switching between kboards here. */
2159 if (NILP (c) && !single_kboard)
2161 KBOARD *kb;
2162 for (kb = all_kboards; kb; kb = kb->next_kboard)
2163 if (kb->kbd_queue_has_data)
2165 current_kboard = kb;
2166 /* This is going to exit from read_char
2167 so we had better get rid of this frame's stuff. */
2168 UNGCPRO;
2169 longjmp (wrong_kboard_jmpbuf, 1);
2172 #endif
2174 wrong_kboard:
2176 stop_polling ();
2178 /* Finally, we read from the main queue,
2179 and if that gives us something we can't use yet, we put it on the
2180 appropriate side queue and try again. */
2182 if (NILP (c))
2184 KBOARD *kb;
2186 /* Actually read a character, waiting if necessary. */
2187 save_getcjmp (save_jump);
2188 restore_getcjmp (local_getcjmp);
2189 c = kbd_buffer_get_event (&kb, used_mouse_menu);
2190 restore_getcjmp (save_jump);
2192 #ifdef MULTI_KBOARD
2193 if (! NILP (c) && (kb != current_kboard))
2195 Lisp_Object *tailp = &kb->kbd_queue;
2196 while (CONSP (*tailp))
2197 tailp = &XCONS (*tailp)->cdr;
2198 if (!NILP (*tailp))
2199 abort ();
2200 *tailp = Fcons (c, Qnil);
2201 kb->kbd_queue_has_data = 1;
2202 c = Qnil;
2203 if (single_kboard)
2204 goto wrong_kboard;
2205 current_kboard = kb;
2206 /* This is going to exit from read_char
2207 so we had better get rid of this frame's stuff. */
2208 UNGCPRO;
2209 longjmp (wrong_kboard_jmpbuf, 1);
2211 #endif
2214 /* Terminate Emacs in batch mode if at eof. */
2215 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
2216 Fkill_emacs (make_number (1));
2218 if (INTEGERP (c))
2220 /* Add in any extra modifiers, where appropriate. */
2221 if ((extra_keyboard_modifiers & CHAR_CTL)
2222 || ((extra_keyboard_modifiers & 0177) < ' '
2223 && (extra_keyboard_modifiers & 0177) != 0))
2224 XSETINT (c, make_ctrl_char (XINT (c)));
2226 /* Transfer any other modifier bits directly from
2227 extra_keyboard_modifiers to c. Ignore the actual character code
2228 in the low 16 bits of extra_keyboard_modifiers. */
2229 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
2232 non_reread:
2234 timer_stop_idle ();
2236 start_polling ();
2238 if (NILP (c))
2240 if (commandflag >= 0
2241 && !input_pending && !detect_input_pending_run_timers (0))
2242 redisplay ();
2244 goto wrong_kboard;
2247 non_reread_1:
2249 /* Buffer switch events are only for internal wakeups
2250 so don't show them to the user.
2251 Also, don't record a key if we already did. */
2252 if (BUFFERP (c) || key_already_recorded)
2253 RETURN_UNGCPRO (c);
2255 /* Process special events within read_char
2256 and loop around to read another event. */
2257 save = Vquit_flag;
2258 Vquit_flag = Qnil;
2259 tem = get_keyelt (access_keymap (get_keymap_1 (Vspecial_event_map, 0, 0),
2260 c, 0, 0), 1);
2261 Vquit_flag = save;
2263 if (!NILP (tem))
2265 int was_locked = single_kboard;
2267 last_input_char = c;
2268 Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt);
2270 /* Resume allowing input from any kboard, if that was true before. */
2271 if (!was_locked)
2272 any_kboard_state ();
2274 goto retry;
2277 /* Wipe the echo area. */
2278 if (echo_area_glyphs)
2279 safe_run_hooks (Qecho_area_clear_hook);
2280 echo_area_glyphs = 0;
2282 /* Handle things that only apply to characters. */
2283 if (INTEGERP (c))
2285 /* If kbd_buffer_get_event gave us an EOF, return that. */
2286 if (XINT (c) == -1)
2287 RETURN_UNGCPRO (c);
2289 if ((STRINGP (Vkeyboard_translate_table)
2290 && XSTRING (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2291 || (VECTORP (Vkeyboard_translate_table)
2292 && XVECTOR (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2293 || (CHAR_TABLE_P (Vkeyboard_translate_table)
2294 && CHAR_TABLE_ORDINARY_SLOTS > (unsigned) XFASTINT (c)))
2296 Lisp_Object d;
2297 d = Faref (Vkeyboard_translate_table, c);
2298 /* nil in keyboard-translate-table means no translation. */
2299 if (!NILP (d))
2300 c = d;
2304 /* If this event is a mouse click in the menu bar,
2305 return just menu-bar for now. Modify the mouse click event
2306 so we won't do this twice, then queue it up. */
2307 if (EVENT_HAS_PARAMETERS (c)
2308 && CONSP (XCONS (c)->cdr)
2309 && CONSP (EVENT_START (c))
2310 && CONSP (XCONS (EVENT_START (c))->cdr))
2312 Lisp_Object posn;
2314 posn = POSN_BUFFER_POSN (EVENT_START (c));
2315 /* Handle menu-bar events:
2316 insert the dummy prefix event `menu-bar'. */
2317 if (EQ (posn, Qmenu_bar))
2319 /* Change menu-bar to (menu-bar) as the event "position". */
2320 POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
2322 also_record = c;
2323 Vunread_command_events = Fcons (c, Vunread_command_events);
2324 c = posn;
2328 /* Store these characters into recent_keys, the dribble file if any,
2329 and the keyboard macro being defined, if any. */
2330 record_char (c);
2331 if (! NILP (also_record))
2332 record_char (also_record);
2334 reread_for_input_method:
2335 from_macro:
2336 /* Pass this to the input method, if appropriate. */
2337 if (INTEGERP (c))
2339 /* If this is a printing character, run the input method. */
2340 if (! NILP (Vinput_method_function)
2341 && (unsigned) XINT (c) >= ' '
2342 && (unsigned) XINT (c) < 127)
2344 int saved = current_kboard->immediate_echo;
2345 tem = call1 (Vinput_method_function, c);
2346 current_kboard->immediate_echo = saved;
2347 /* The input method can return no events. */
2348 if (! CONSP (tem))
2349 goto retry;
2350 /* It returned one event or more. */
2351 c = XCONS (tem)->car;
2352 Vunread_post_input_method_events
2353 = nconc2 (XCONS (tem)->cdr, Vunread_post_input_method_events);
2357 reread_first:
2359 if (this_command_key_count == 0 || ! reread)
2361 before_command_key_count = this_command_key_count;
2362 before_command_echo_length = echo_length ();
2364 /* Don't echo mouse motion events. */
2365 if (echo_keystrokes
2366 && ! (EVENT_HAS_PARAMETERS (c)
2367 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
2369 echo_char (c);
2370 if (! NILP (also_record))
2371 echo_char (also_record);
2372 /* Once we reread a character, echoing can happen
2373 the next time we pause to read a new one. */
2374 ok_to_echo_at_next_pause = echo_area_glyphs;
2377 /* Record this character as part of the current key. */
2378 add_command_key (c);
2379 if (! NILP (also_record))
2380 add_command_key (also_record);
2383 last_input_char = c;
2384 num_input_events++;
2386 /* Process the help character specially if enabled */
2387 if (!NILP (Vhelp_form) && help_char_p (c))
2389 Lisp_Object tem0;
2390 count = specpdl_ptr - specpdl;
2392 record_unwind_protect (Fset_window_configuration,
2393 Fcurrent_window_configuration (Qnil));
2395 tem0 = Feval (Vhelp_form);
2396 if (STRINGP (tem0))
2397 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
2399 cancel_echoing ();
2401 c = read_char (0, 0, 0, Qnil, 0);
2402 while (BUFFERP (c));
2403 /* Remove the help from the frame */
2404 unbind_to (count, Qnil);
2406 redisplay ();
2407 if (EQ (c, make_number (040)))
2409 cancel_echoing ();
2411 c = read_char (0, 0, 0, Qnil, 0);
2412 while (BUFFERP (c));
2416 RETURN_UNGCPRO (c);
2419 /* Record a key that came from a mouse menu.
2420 Record it for echoing, for this-command-keys, and so on. */
2422 static void
2423 record_menu_key (c)
2424 Lisp_Object c;
2426 /* Wipe the echo area. */
2427 echo_area_glyphs = 0;
2429 record_char (c);
2431 before_command_key_count = this_command_key_count;
2432 before_command_echo_length = echo_length ();
2434 /* Don't echo mouse motion events. */
2435 if (echo_keystrokes)
2437 echo_char (c);
2439 /* Once we reread a character, echoing can happen
2440 the next time we pause to read a new one. */
2441 ok_to_echo_at_next_pause = 0;
2444 /* Record this character as part of the current key. */
2445 add_command_key (c);
2447 /* Re-reading in the middle of a command */
2448 last_input_char = c;
2449 num_input_events++;
2452 /* Return 1 if should recognize C as "the help character". */
2455 help_char_p (c)
2456 Lisp_Object c;
2458 Lisp_Object tail;
2460 if (EQ (c, Vhelp_char))
2461 return 1;
2462 for (tail = Vhelp_event_list; CONSP (tail); tail = XCONS (tail)->cdr)
2463 if (EQ (c, XCONS (tail)->car))
2464 return 1;
2465 return 0;
2468 /* Record the input event C in various ways. */
2470 static void
2471 record_char (c)
2472 Lisp_Object c;
2474 total_keys++;
2475 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
2476 if (++recent_keys_index >= NUM_RECENT_KEYS)
2477 recent_keys_index = 0;
2479 /* Write c to the dribble file. If c is a lispy event, write
2480 the event's symbol to the dribble file, in <brackets>. Bleaugh.
2481 If you, dear reader, have a better idea, you've got the source. :-) */
2482 if (dribble)
2484 if (INTEGERP (c))
2486 if (XUINT (c) < 0x100)
2487 putc (XINT (c), dribble);
2488 else
2489 fprintf (dribble, " 0x%x", (int) XUINT (c));
2491 else
2493 Lisp_Object dribblee;
2495 /* If it's a structured event, take the event header. */
2496 dribblee = EVENT_HEAD (c);
2498 if (SYMBOLP (dribblee))
2500 putc ('<', dribble);
2501 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
2502 STRING_BYTES (XSYMBOL (dribblee)->name),
2503 dribble);
2504 putc ('>', dribble);
2508 fflush (dribble);
2511 store_kbd_macro_char (c);
2513 num_nonmacro_input_events++;
2516 Lisp_Object
2517 print_help (object)
2518 Lisp_Object object;
2520 struct buffer *old = current_buffer;
2521 Fprinc (object, Qnil);
2522 set_buffer_internal (XBUFFER (Vstandard_output));
2523 call0 (intern ("help-mode"));
2524 set_buffer_internal (old);
2525 return Qnil;
2528 /* Copy out or in the info on where C-g should throw to.
2529 This is used when running Lisp code from within get_char,
2530 in case get_char is called recursively.
2531 See read_process_output. */
2533 static void
2534 save_getcjmp (temp)
2535 jmp_buf temp;
2537 bcopy (getcjmp, temp, sizeof getcjmp);
2540 static void
2541 restore_getcjmp (temp)
2542 jmp_buf temp;
2544 bcopy (temp, getcjmp, sizeof getcjmp);
2547 #ifdef HAVE_MOUSE
2549 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
2550 of this function. */
2552 static Lisp_Object
2553 tracking_off (old_value)
2554 Lisp_Object old_value;
2556 do_mouse_tracking = old_value;
2557 if (NILP (old_value))
2559 /* Redisplay may have been preempted because there was input
2560 available, and it assumes it will be called again after the
2561 input has been processed. If the only input available was
2562 the sort that we have just disabled, then we need to call
2563 redisplay. */
2564 if (!readable_events (1))
2566 redisplay_preserve_echo_area ();
2567 get_input_pending (&input_pending, 1);
2572 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
2573 "Evaluate BODY with mouse movement events enabled.\n\
2574 Within a `track-mouse' form, mouse motion generates input events that\n\
2575 you can read with `read-event'.\n\
2576 Normally, mouse motion is ignored.")
2577 (args)
2578 Lisp_Object args;
2580 int count = specpdl_ptr - specpdl;
2581 Lisp_Object val;
2583 record_unwind_protect (tracking_off, do_mouse_tracking);
2585 do_mouse_tracking = Qt;
2587 val = Fprogn (args);
2588 return unbind_to (count, val);
2591 /* If mouse has moved on some frame, return one of those frames.
2592 Return 0 otherwise. */
2594 static FRAME_PTR
2595 some_mouse_moved ()
2597 Lisp_Object tail, frame;
2599 FOR_EACH_FRAME (tail, frame)
2601 if (XFRAME (frame)->mouse_moved)
2602 return XFRAME (frame);
2605 return 0;
2608 #endif /* HAVE_MOUSE */
2610 /* Low level keyboard/mouse input.
2611 kbd_buffer_store_event places events in kbd_buffer, and
2612 kbd_buffer_get_event retrieves them. */
2614 /* Return true iff there are any events in the queue that read-char
2615 would return. If this returns false, a read-char would block. */
2616 static int
2617 readable_events (do_timers_now)
2618 int do_timers_now;
2620 if (do_timers_now)
2621 timer_check (do_timers_now);
2623 if (kbd_fetch_ptr != kbd_store_ptr)
2624 return 1;
2625 #ifdef HAVE_MOUSE
2626 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2627 return 1;
2628 #endif
2629 if (single_kboard)
2631 if (current_kboard->kbd_queue_has_data)
2632 return 1;
2634 else
2636 KBOARD *kb;
2637 for (kb = all_kboards; kb; kb = kb->next_kboard)
2638 if (kb->kbd_queue_has_data)
2639 return 1;
2641 return 0;
2644 /* Set this for debugging, to have a way to get out */
2645 int stop_character;
2647 #ifdef MULTI_KBOARD
2648 static KBOARD *
2649 event_to_kboard (event)
2650 struct input_event *event;
2652 Lisp_Object frame;
2653 frame = event->frame_or_window;
2654 if (CONSP (frame))
2655 frame = XCONS (frame)->car;
2656 else if (WINDOWP (frame))
2657 frame = WINDOW_FRAME (XWINDOW (frame));
2659 /* There are still some events that don't set this field.
2660 For now, just ignore the problem.
2661 Also ignore dead frames here. */
2662 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
2663 return 0;
2664 else
2665 return FRAME_KBOARD (XFRAME (frame));
2667 #endif
2669 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
2671 void
2672 kbd_buffer_store_event (event)
2673 register struct input_event *event;
2675 if (event->kind == no_event)
2676 abort ();
2678 if (event->kind == ascii_keystroke)
2680 register int c = event->code & 0377;
2682 if (event->modifiers & ctrl_modifier)
2683 c = make_ctrl_char (c);
2685 c |= (event->modifiers
2686 & (meta_modifier | alt_modifier
2687 | hyper_modifier | super_modifier));
2689 if (c == quit_char)
2691 extern SIGTYPE interrupt_signal ();
2692 #ifdef MULTI_KBOARD
2693 KBOARD *kb;
2694 struct input_event *sp;
2696 if (single_kboard
2697 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
2698 kb != current_kboard))
2700 kb->kbd_queue
2701 = Fcons (make_lispy_switch_frame (event->frame_or_window),
2702 Fcons (make_number (c), Qnil));
2703 kb->kbd_queue_has_data = 1;
2704 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
2706 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
2707 sp = kbd_buffer;
2709 if (event_to_kboard (sp) == kb)
2711 sp->kind = no_event;
2712 sp->frame_or_window = Qnil;
2715 return;
2717 #endif
2719 /* If this results in a quit_char being returned to Emacs as
2720 input, set Vlast_event_frame properly. If this doesn't
2721 get returned to Emacs as an event, the next event read
2722 will set Vlast_event_frame again, so this is safe to do. */
2724 Lisp_Object focus;
2726 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
2727 if (NILP (focus))
2728 focus = event->frame_or_window;
2729 internal_last_event_frame = focus;
2730 Vlast_event_frame = focus;
2733 last_event_timestamp = event->timestamp;
2734 interrupt_signal ();
2735 return;
2738 if (c && c == stop_character)
2740 sys_suspend ();
2741 return;
2744 /* Don't insert two buffer_switch_event's in a row.
2745 Just ignore the second one. */
2746 else if (event->kind == buffer_switch_event
2747 && kbd_fetch_ptr != kbd_store_ptr
2748 && kbd_store_ptr->kind == buffer_switch_event)
2749 return;
2751 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
2752 kbd_store_ptr = kbd_buffer;
2754 /* Don't let the very last slot in the buffer become full,
2755 since that would make the two pointers equal,
2756 and that is indistinguishable from an empty buffer.
2757 Discard the event if it would fill the last slot. */
2758 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
2760 volatile struct input_event *sp = kbd_store_ptr;
2761 sp->kind = event->kind;
2762 if (event->kind == selection_request_event)
2764 /* We must not use the ordinary copying code for this case,
2765 since `part' is an enum and copying it might not copy enough
2766 in this case. */
2767 bcopy (event, (char *) sp, sizeof (*event));
2769 else
2771 sp->code = event->code;
2772 sp->part = event->part;
2773 sp->frame_or_window = event->frame_or_window;
2774 sp->modifiers = event->modifiers;
2775 sp->x = event->x;
2776 sp->y = event->y;
2777 sp->timestamp = event->timestamp;
2779 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
2780 - kbd_buffer]
2781 = event->frame_or_window);
2783 kbd_store_ptr++;
2787 /* Discard any mouse events in the event buffer by setting them to
2788 no_event. */
2789 void
2790 discard_mouse_events ()
2792 struct input_event *sp;
2793 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
2795 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
2796 sp = kbd_buffer;
2798 if (sp->kind == mouse_click
2799 #ifdef WINDOWSNT
2800 || sp->kind == w32_scroll_bar_click
2801 #endif
2802 || sp->kind == scroll_bar_click)
2804 sp->kind = no_event;
2809 /* Read one event from the event buffer, waiting if necessary.
2810 The value is a Lisp object representing the event.
2811 The value is nil for an event that should be ignored,
2812 or that was handled here.
2813 We always read and discard one event. */
2815 static Lisp_Object
2816 kbd_buffer_get_event (kbp, used_mouse_menu)
2817 KBOARD **kbp;
2818 int *used_mouse_menu;
2820 register int c;
2821 Lisp_Object obj;
2822 EMACS_TIME next_timer_delay;
2824 if (noninteractive)
2826 c = getchar ();
2827 XSETINT (obj, c);
2828 *kbp = current_kboard;
2829 return obj;
2832 /* Wait until there is input available. */
2833 for (;;)
2835 if (kbd_fetch_ptr != kbd_store_ptr)
2836 break;
2837 #ifdef HAVE_MOUSE
2838 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2839 break;
2840 #endif
2842 /* If the quit flag is set, then read_char will return
2843 quit_char, so that counts as "available input." */
2844 if (!NILP (Vquit_flag))
2845 quit_throw_to_read_char ();
2847 /* One way or another, wait until input is available; then, if
2848 interrupt handlers have not read it, read it now. */
2850 #ifdef OLDVMS
2851 wait_for_kbd_input ();
2852 #else
2853 /* Note SIGIO has been undef'd if FIONREAD is missing. */
2854 #ifdef SIGIO
2855 gobble_input (0);
2856 #endif /* SIGIO */
2857 if (kbd_fetch_ptr != kbd_store_ptr)
2858 break;
2859 #ifdef HAVE_MOUSE
2860 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2861 break;
2862 #endif
2864 Lisp_Object minus_one;
2866 XSETINT (minus_one, -1);
2867 wait_reading_process_input (0, 0, minus_one, 1);
2869 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
2870 /* Pass 1 for EXPECT since we just waited to have input. */
2871 read_avail_input (1);
2873 #endif /* not VMS */
2876 if (CONSP (Vunread_command_events))
2878 Lisp_Object first;
2879 first = XCONS (Vunread_command_events)->car;
2880 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
2881 *kbp = current_kboard;
2882 return first;
2885 /* At this point, we know that there is a readable event available
2886 somewhere. If the event queue is empty, then there must be a
2887 mouse movement enabled and available. */
2888 if (kbd_fetch_ptr != kbd_store_ptr)
2890 struct input_event *event;
2892 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2893 ? kbd_fetch_ptr
2894 : kbd_buffer);
2896 last_event_timestamp = event->timestamp;
2898 #ifdef MULTI_KBOARD
2899 *kbp = event_to_kboard (event);
2900 if (*kbp == 0)
2901 *kbp = current_kboard; /* Better than returning null ptr? */
2902 #else
2903 *kbp = &the_only_kboard;
2904 #endif
2906 obj = Qnil;
2908 /* These two kinds of events get special handling
2909 and don't actually appear to the command loop.
2910 We return nil for them. */
2911 if (event->kind == selection_request_event)
2913 #ifdef HAVE_X11
2914 struct input_event copy;
2916 /* Remove it from the buffer before processing it,
2917 since otherwise swallow_events will see it
2918 and process it again. */
2919 copy = *event;
2920 kbd_fetch_ptr = event + 1;
2921 input_pending = readable_events (0);
2922 x_handle_selection_request (&copy);
2923 #else
2924 /* We're getting selection request events, but we don't have
2925 a window system. */
2926 abort ();
2927 #endif
2930 else if (event->kind == selection_clear_event)
2932 #ifdef HAVE_X11
2933 struct input_event copy;
2935 /* Remove it from the buffer before processing it. */
2936 copy = *event;
2937 kbd_fetch_ptr = event + 1;
2938 input_pending = readable_events (0);
2939 x_handle_selection_clear (&copy);
2940 #else
2941 /* We're getting selection request events, but we don't have
2942 a window system. */
2943 abort ();
2944 #endif
2946 #if defined (HAVE_X11) || defined (HAVE_NTGUI)
2947 else if (event->kind == delete_window_event)
2949 /* Make an event (delete-frame (FRAME)). */
2950 obj = Fcons (event->frame_or_window, Qnil);
2951 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
2952 kbd_fetch_ptr = event + 1;
2954 else if (event->kind == iconify_event)
2956 /* Make an event (iconify-frame (FRAME)). */
2957 obj = Fcons (event->frame_or_window, Qnil);
2958 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
2959 kbd_fetch_ptr = event + 1;
2961 else if (event->kind == deiconify_event)
2963 /* Make an event (make-frame-visible (FRAME)). */
2964 obj = Fcons (event->frame_or_window, Qnil);
2965 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
2966 kbd_fetch_ptr = event + 1;
2968 #endif
2969 else if (event->kind == buffer_switch_event)
2971 /* The value doesn't matter here; only the type is tested. */
2972 XSETBUFFER (obj, current_buffer);
2973 kbd_fetch_ptr = event + 1;
2975 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
2976 else if (event->kind == menu_bar_activate_event)
2978 kbd_fetch_ptr = event + 1;
2979 input_pending = readable_events (0);
2980 if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
2981 x_activate_menubar (XFRAME (event->frame_or_window));
2983 #endif
2984 /* Just discard these, by returning nil.
2985 With MULTI_KBOARD, these events are used as placeholders
2986 when we need to randomly delete events from the queue.
2987 (They shouldn't otherwise be found in the buffer,
2988 but on some machines it appears they do show up
2989 even without MULTI_KBOARD.) */
2990 /* On Windows NT/9X, no_event is used to delete extraneous
2991 mouse events during a popup-menu call. */
2992 else if (event->kind == no_event)
2993 kbd_fetch_ptr = event + 1;
2995 /* If this event is on a different frame, return a switch-frame this
2996 time, and leave the event in the queue for next time. */
2997 else
2999 Lisp_Object frame;
3000 Lisp_Object focus;
3002 frame = event->frame_or_window;
3003 if (CONSP (frame))
3004 frame = XCONS (frame)->car;
3005 else if (WINDOWP (frame))
3006 frame = WINDOW_FRAME (XWINDOW (frame));
3008 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
3009 if (! NILP (focus))
3010 frame = focus;
3012 if (! EQ (frame, internal_last_event_frame)
3013 && XFRAME (frame) != selected_frame)
3014 obj = make_lispy_switch_frame (frame);
3015 internal_last_event_frame = frame;
3017 /* If we didn't decide to make a switch-frame event, go ahead
3018 and build a real event from the queue entry. */
3020 if (NILP (obj))
3022 obj = make_lispy_event (event);
3023 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
3024 /* If this was a menu selection, then set the flag to inhibit
3025 writing to last_nonmenu_event. Don't do this if the event
3026 we're returning is (menu-bar), though; that indicates the
3027 beginning of the menu sequence, and we might as well leave
3028 that as the `event with parameters' for this selection. */
3029 if (event->kind == menu_bar_event
3030 && !(CONSP (obj) && EQ (XCONS (obj)->car, Qmenu_bar))
3031 && used_mouse_menu)
3032 *used_mouse_menu = 1;
3033 #endif
3035 /* Wipe out this event, to catch bugs. */
3036 event->kind = no_event;
3037 XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer] = Qnil;
3039 kbd_fetch_ptr = event + 1;
3043 #ifdef HAVE_MOUSE
3044 /* Try generating a mouse motion event. */
3045 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3047 FRAME_PTR f = some_mouse_moved ();
3048 Lisp_Object bar_window;
3049 enum scroll_bar_part part;
3050 Lisp_Object x, y;
3051 unsigned long time;
3053 *kbp = current_kboard;
3054 /* Note that this uses F to determine which display to look at.
3055 If there is no valid info, it does not store anything
3056 so x remains nil. */
3057 x = Qnil;
3058 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
3060 obj = Qnil;
3062 /* Decide if we should generate a switch-frame event. Don't
3063 generate switch-frame events for motion outside of all Emacs
3064 frames. */
3065 if (!NILP (x) && f)
3067 Lisp_Object frame;
3069 frame = FRAME_FOCUS_FRAME (f);
3070 if (NILP (frame))
3071 XSETFRAME (frame, f);
3073 if (! EQ (frame, internal_last_event_frame)
3074 && XFRAME (frame) != selected_frame)
3075 obj = make_lispy_switch_frame (frame);
3076 internal_last_event_frame = frame;
3079 /* If we didn't decide to make a switch-frame event, go ahead and
3080 return a mouse-motion event. */
3081 if (!NILP (x) && NILP (obj))
3082 obj = make_lispy_movement (f, bar_window, part, x, y, time);
3084 #endif /* HAVE_MOUSE */
3085 else
3086 /* We were promised by the above while loop that there was
3087 something for us to read! */
3088 abort ();
3090 input_pending = readable_events (0);
3092 Vlast_event_frame = internal_last_event_frame;
3094 return (obj);
3097 /* Process any events that are not user-visible,
3098 then return, without reading any user-visible events. */
3100 void
3101 swallow_events (do_display)
3102 int do_display;
3104 int old_timers_run;
3106 while (kbd_fetch_ptr != kbd_store_ptr)
3108 struct input_event *event;
3110 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3111 ? kbd_fetch_ptr
3112 : kbd_buffer);
3114 last_event_timestamp = event->timestamp;
3116 /* These two kinds of events get special handling
3117 and don't actually appear to the command loop. */
3118 if (event->kind == selection_request_event)
3120 #ifdef HAVE_X11
3121 struct input_event copy;
3123 /* Remove it from the buffer before processing it,
3124 since otherwise swallow_events called recursively could see it
3125 and process it again. */
3126 copy = *event;
3127 kbd_fetch_ptr = event + 1;
3128 input_pending = readable_events (0);
3129 x_handle_selection_request (&copy);
3130 #else
3131 /* We're getting selection request events, but we don't have
3132 a window system. */
3133 abort ();
3134 #endif
3137 else if (event->kind == selection_clear_event)
3139 #ifdef HAVE_X11
3140 struct input_event copy;
3142 /* Remove it from the buffer before processing it, */
3143 copy = *event;
3145 kbd_fetch_ptr = event + 1;
3146 input_pending = readable_events (0);
3147 x_handle_selection_clear (&copy);
3148 #else
3149 /* We're getting selection request events, but we don't have
3150 a window system. */
3151 abort ();
3152 #endif
3154 else
3155 break;
3158 old_timers_run = timers_run;
3159 get_input_pending (&input_pending, 1);
3161 if (timers_run != old_timers_run && do_display)
3162 redisplay_preserve_echo_area ();
3165 static EMACS_TIME timer_idleness_start_time;
3167 /* Record the start of when Emacs is idle,
3168 for the sake of running idle-time timers. */
3170 void
3171 timer_start_idle ()
3173 Lisp_Object timers;
3175 /* If we are already in the idle state, do nothing. */
3176 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3177 return;
3179 EMACS_GET_TIME (timer_idleness_start_time);
3181 /* Mark all idle-time timers as once again candidates for running. */
3182 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCONS (timers)->cdr)
3184 Lisp_Object timer;
3186 timer = XCONS (timers)->car;
3188 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3189 continue;
3190 XVECTOR (timer)->contents[0] = Qnil;
3194 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
3196 void
3197 timer_stop_idle ()
3199 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
3202 /* This is only for debugging. */
3203 struct input_event last_timer_event;
3205 /* Check whether a timer has fired. To prevent larger problems we simply
3206 disregard elements that are not proper timers. Do not make a circular
3207 timer list for the time being.
3209 Returns the number of seconds to wait until the next timer fires. If a
3210 timer is triggering now, return zero seconds.
3211 If no timer is active, return -1 seconds.
3213 If a timer is ripe, we run it, with quitting turned off.
3215 DO_IT_NOW is now ignored. It used to mean that we should
3216 run the timer directly instead of queueing a timer-event.
3217 Now we always run timers directly. */
3219 EMACS_TIME
3220 timer_check (do_it_now)
3221 int do_it_now;
3223 EMACS_TIME nexttime;
3224 EMACS_TIME now, idleness_now;
3225 Lisp_Object timers, idle_timers, chosen_timer;
3226 struct gcpro gcpro1, gcpro2, gcpro3;
3228 EMACS_SET_SECS (nexttime, -1);
3229 EMACS_SET_USECS (nexttime, -1);
3231 /* Always consider the ordinary timers. */
3232 timers = Vtimer_list;
3233 /* Consider the idle timers only if Emacs is idle. */
3234 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3235 idle_timers = Vtimer_idle_list;
3236 else
3237 idle_timers = Qnil;
3238 chosen_timer = Qnil;
3239 GCPRO3 (timers, idle_timers, chosen_timer);
3241 if (CONSP (timers) || CONSP (idle_timers))
3243 EMACS_GET_TIME (now);
3244 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3245 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
3248 while (CONSP (timers) || CONSP (idle_timers))
3250 int triggertime = EMACS_SECS (now);
3251 Lisp_Object *vector;
3252 Lisp_Object timer, idle_timer;
3253 EMACS_TIME timer_time, idle_timer_time;
3254 EMACS_TIME difference, timer_difference, idle_timer_difference;
3256 /* Skip past invalid timers and timers already handled. */
3257 if (!NILP (timers))
3259 timer = XCONS (timers)->car;
3260 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3262 timers = XCONS (timers)->cdr;
3263 continue;
3265 vector = XVECTOR (timer)->contents;
3267 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3268 || !INTEGERP (vector[3])
3269 || ! NILP (vector[0]))
3271 timers = XCONS (timers)->cdr;
3272 continue;
3275 if (!NILP (idle_timers))
3277 timer = XCONS (idle_timers)->car;
3278 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3280 idle_timers = XCONS (idle_timers)->cdr;
3281 continue;
3283 vector = XVECTOR (timer)->contents;
3285 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3286 || !INTEGERP (vector[3])
3287 || ! NILP (vector[0]))
3289 idle_timers = XCONS (idle_timers)->cdr;
3290 continue;
3294 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
3295 based on the next ordinary timer.
3296 TIMER_DIFFERENCE is the distance in time from NOW to when
3297 this timer becomes ripe (negative if it's already ripe). */
3298 if (!NILP (timers))
3300 timer = XCONS (timers)->car;
3301 vector = XVECTOR (timer)->contents;
3302 EMACS_SET_SECS (timer_time,
3303 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3304 EMACS_SET_USECS (timer_time, XINT (vector[3]));
3305 EMACS_SUB_TIME (timer_difference, timer_time, now);
3308 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
3309 based on the next idle timer. */
3310 if (!NILP (idle_timers))
3312 idle_timer = XCONS (idle_timers)->car;
3313 vector = XVECTOR (idle_timer)->contents;
3314 EMACS_SET_SECS (idle_timer_time,
3315 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3316 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
3317 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
3320 /* Decide which timer is the next timer,
3321 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
3322 Also step down the list where we found that timer. */
3324 if (! NILP (timers) && ! NILP (idle_timers))
3326 EMACS_TIME temp;
3327 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
3328 if (EMACS_TIME_NEG_P (temp))
3330 chosen_timer = timer;
3331 timers = XCONS (timers)->cdr;
3332 difference = timer_difference;
3334 else
3336 chosen_timer = idle_timer;
3337 idle_timers = XCONS (idle_timers)->cdr;
3338 difference = idle_timer_difference;
3341 else if (! NILP (timers))
3343 chosen_timer = timer;
3344 timers = XCONS (timers)->cdr;
3345 difference = timer_difference;
3347 else
3349 chosen_timer = idle_timer;
3350 idle_timers = XCONS (idle_timers)->cdr;
3351 difference = idle_timer_difference;
3353 vector = XVECTOR (chosen_timer)->contents;
3355 /* If timer is rupe, run it if it hasn't been run. */
3356 if (EMACS_TIME_NEG_P (difference)
3357 || (EMACS_SECS (difference) == 0
3358 && EMACS_USECS (difference) == 0))
3360 if (NILP (vector[0]))
3362 Lisp_Object tem;
3363 int was_locked = single_kboard;
3364 int count = specpdl_ptr - specpdl;
3366 /* Mark the timer as triggered to prevent problems if the lisp
3367 code fails to reschedule it right. */
3368 vector[0] = Qt;
3370 specbind (Qinhibit_quit, Qt);
3372 call1 (Qtimer_event_handler, chosen_timer);
3373 timers_run++;
3375 unbind_to (count, Qnil);
3377 /* Resume allowing input from any kboard, if that was true before. */
3378 if (!was_locked)
3379 any_kboard_state ();
3381 /* Since we have handled the event,
3382 we don't need to tell the caller to wake up and do it. */
3385 else
3386 /* When we encounter a timer that is still waiting,
3387 return the amount of time to wait before it is ripe. */
3389 UNGCPRO;
3390 return difference;
3394 /* No timers are pending in the future. */
3395 /* Return 0 if we generated an event, and -1 if not. */
3396 UNGCPRO;
3397 return nexttime;
3400 /* Caches for modify_event_symbol. */
3401 static Lisp_Object accent_key_syms;
3402 static Lisp_Object func_key_syms;
3403 static Lisp_Object mouse_syms;
3404 #ifdef WINDOWSNT
3405 static Lisp_Object mouse_wheel_syms;
3406 #endif
3407 static Lisp_Object drag_n_drop_syms;
3409 /* This is a list of keysym codes for special "accent" characters.
3410 It parallels lispy_accent_keys. */
3412 static int lispy_accent_codes[] =
3414 #ifdef XK_dead_circumflex
3415 XK_dead_circumflex,
3416 #else
3418 #endif
3419 #ifdef XK_dead_grave
3420 XK_dead_grave,
3421 #else
3423 #endif
3424 #ifdef XK_dead_tilde
3425 XK_dead_tilde,
3426 #else
3428 #endif
3429 #ifdef XK_dead_diaeresis
3430 XK_dead_diaeresis,
3431 #else
3433 #endif
3434 #ifdef XK_dead_macron
3435 XK_dead_macron,
3436 #else
3438 #endif
3439 #ifdef XK_dead_degree
3440 XK_dead_degree,
3441 #else
3443 #endif
3444 #ifdef XK_dead_acute
3445 XK_dead_acute,
3446 #else
3448 #endif
3449 #ifdef XK_dead_cedilla
3450 XK_dead_cedilla,
3451 #else
3453 #endif
3454 #ifdef XK_dead_breve
3455 XK_dead_breve,
3456 #else
3458 #endif
3459 #ifdef XK_dead_ogonek
3460 XK_dead_ogonek,
3461 #else
3463 #endif
3464 #ifdef XK_dead_caron
3465 XK_dead_caron,
3466 #else
3468 #endif
3469 #ifdef XK_dead_doubleacute
3470 XK_dead_doubleacute,
3471 #else
3473 #endif
3474 #ifdef XK_dead_abovedot
3475 XK_dead_abovedot,
3476 #else
3478 #endif
3481 /* This is a list of Lisp names for special "accent" characters.
3482 It parallels lispy_accent_codes. */
3484 static char *lispy_accent_keys[] =
3486 "dead-circumflex",
3487 "dead-grave",
3488 "dead-tilde",
3489 "dead-diaeresis",
3490 "dead-macron",
3491 "dead-degree",
3492 "dead-acute",
3493 "dead-cedilla",
3494 "dead-breve",
3495 "dead-ogonek",
3496 "dead-caron",
3497 "dead-doubleacute",
3498 "dead-abovedot",
3501 #ifdef HAVE_NTGUI
3502 #define FUNCTION_KEY_OFFSET 0x0
3504 char *lispy_function_keys[] =
3506 0, /* 0 */
3508 0, /* VK_LBUTTON 0x01 */
3509 0, /* VK_RBUTTON 0x02 */
3510 "cancel", /* VK_CANCEL 0x03 */
3511 0, /* VK_MBUTTON 0x04 */
3513 0, 0, 0, /* 0x05 .. 0x07 */
3515 "backspace", /* VK_BACK 0x08 */
3516 "tab", /* VK_TAB 0x09 */
3518 0, 0, /* 0x0A .. 0x0B */
3520 "clear", /* VK_CLEAR 0x0C */
3521 "return", /* VK_RETURN 0x0D */
3523 0, 0, /* 0x0E .. 0x0F */
3525 "shift", /* VK_SHIFT 0x10 */
3526 "control", /* VK_CONTROL 0x11 */
3527 "menu", /* VK_MENU 0x12 */
3528 "pause", /* VK_PAUSE 0x13 */
3529 "capital", /* VK_CAPITAL 0x14 */
3531 0, 0, 0, 0, 0, 0, /* 0x15 .. 0x1A */
3533 0, /* VK_ESCAPE 0x1B */
3535 0, 0, 0, 0, /* 0x1C .. 0x1F */
3537 0, /* VK_SPACE 0x20 */
3538 "prior", /* VK_PRIOR 0x21 */
3539 "next", /* VK_NEXT 0x22 */
3540 "end", /* VK_END 0x23 */
3541 "home", /* VK_HOME 0x24 */
3542 "left", /* VK_LEFT 0x25 */
3543 "up", /* VK_UP 0x26 */
3544 "right", /* VK_RIGHT 0x27 */
3545 "down", /* VK_DOWN 0x28 */
3546 "select", /* VK_SELECT 0x29 */
3547 "print", /* VK_PRINT 0x2A */
3548 "execute", /* VK_EXECUTE 0x2B */
3549 "snapshot", /* VK_SNAPSHOT 0x2C */
3550 "insert", /* VK_INSERT 0x2D */
3551 "delete", /* VK_DELETE 0x2E */
3552 "help", /* VK_HELP 0x2F */
3554 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
3556 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3558 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
3560 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
3562 0, 0, 0, 0, 0, 0, 0, 0, 0,
3563 0, 0, 0, 0, 0, 0, 0, 0, 0,
3564 0, 0, 0, 0, 0, 0, 0, 0,
3566 "lwindow", /* VK_LWIN 0x5B */
3567 "rwindow", /* VK_RWIN 0x5C */
3568 "apps", /* VK_APPS 0x5D */
3570 0, 0, /* 0x5E .. 0x5F */
3572 "kp-0", /* VK_NUMPAD0 0x60 */
3573 "kp-1", /* VK_NUMPAD1 0x61 */
3574 "kp-2", /* VK_NUMPAD2 0x62 */
3575 "kp-3", /* VK_NUMPAD3 0x63 */
3576 "kp-4", /* VK_NUMPAD4 0x64 */
3577 "kp-5", /* VK_NUMPAD5 0x65 */
3578 "kp-6", /* VK_NUMPAD6 0x66 */
3579 "kp-7", /* VK_NUMPAD7 0x67 */
3580 "kp-8", /* VK_NUMPAD8 0x68 */
3581 "kp-9", /* VK_NUMPAD9 0x69 */
3582 "kp-multiply", /* VK_MULTIPLY 0x6A */
3583 "kp-add", /* VK_ADD 0x6B */
3584 "kp-separator", /* VK_SEPARATOR 0x6C */
3585 "kp-subtract", /* VK_SUBTRACT 0x6D */
3586 "kp-decimal", /* VK_DECIMAL 0x6E */
3587 "kp-divide", /* VK_DIVIDE 0x6F */
3588 "f1", /* VK_F1 0x70 */
3589 "f2", /* VK_F2 0x71 */
3590 "f3", /* VK_F3 0x72 */
3591 "f4", /* VK_F4 0x73 */
3592 "f5", /* VK_F5 0x74 */
3593 "f6", /* VK_F6 0x75 */
3594 "f7", /* VK_F7 0x76 */
3595 "f8", /* VK_F8 0x77 */
3596 "f9", /* VK_F9 0x78 */
3597 "f10", /* VK_F10 0x79 */
3598 "f11", /* VK_F11 0x7A */
3599 "f12", /* VK_F12 0x7B */
3600 "f13", /* VK_F13 0x7C */
3601 "f14", /* VK_F14 0x7D */
3602 "f15", /* VK_F15 0x7E */
3603 "f16", /* VK_F16 0x7F */
3604 "f17", /* VK_F17 0x80 */
3605 "f18", /* VK_F18 0x81 */
3606 "f19", /* VK_F19 0x82 */
3607 "f20", /* VK_F20 0x83 */
3608 "f21", /* VK_F21 0x84 */
3609 "f22", /* VK_F22 0x85 */
3610 "f23", /* VK_F23 0x86 */
3611 "f24", /* VK_F24 0x87 */
3613 0, 0, 0, 0, /* 0x88 .. 0x8B */
3614 0, 0, 0, 0, /* 0x8C .. 0x8F */
3616 "kp-numlock", /* VK_NUMLOCK 0x90 */
3617 "scroll", /* VK_SCROLL 0x91 */
3619 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
3620 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
3621 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
3622 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
3623 "kp-end", /* VK_NUMPAD_END 0x96 */
3624 "kp-home", /* VK_NUMPAD_HOME 0x97 */
3625 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
3626 "kp-up", /* VK_NUMPAD_UP 0x99 */
3627 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
3628 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
3629 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
3630 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
3632 0, 0, /* 0x9E .. 0x9F */
3635 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
3636 * Used only as parameters to GetAsyncKeyState and GetKeyState.
3637 * No other API or message will distinguish left and right keys this way.
3639 /* 0xA0 .. 0xEF */
3641 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3642 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3643 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3644 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3645 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3647 /* 0xF0 .. 0xF5 */
3649 0, 0, 0, 0, 0, 0,
3651 "attn", /* VK_ATTN 0xF6 */
3652 "crsel", /* VK_CRSEL 0xF7 */
3653 "exsel", /* VK_EXSEL 0xF8 */
3654 "ereof", /* VK_EREOF 0xF9 */
3655 "play", /* VK_PLAY 0xFA */
3656 "zoom", /* VK_ZOOM 0xFB */
3657 "noname", /* VK_NONAME 0xFC */
3658 "pa1", /* VK_PA1 0xFD */
3659 "oem_clear", /* VK_OEM_CLEAR 0xFE */
3662 #else /* not HAVE_NTGUI */
3664 #ifdef XK_kana_A
3665 static char *lispy_kana_keys[] =
3667 /* X Keysym value */
3668 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x400 .. 0x40f */
3669 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x410 .. 0x41f */
3670 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x420 .. 0x42f */
3671 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x430 .. 0x43f */
3672 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x440 .. 0x44f */
3673 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x450 .. 0x45f */
3674 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x460 .. 0x46f */
3675 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0,
3676 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x480 .. 0x48f */
3677 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x490 .. 0x49f */
3678 0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket",
3679 "kana-comma", "kana-conjunctive", "kana-WO", "kana-a",
3680 "kana-i", "kana-u", "kana-e", "kana-o",
3681 "kana-ya", "kana-yu", "kana-yo", "kana-tsu",
3682 "prolongedsound", "kana-A", "kana-I", "kana-U",
3683 "kana-E", "kana-O", "kana-KA", "kana-KI",
3684 "kana-KU", "kana-KE", "kana-KO", "kana-SA",
3685 "kana-SHI", "kana-SU", "kana-SE", "kana-SO",
3686 "kana-TA", "kana-CHI", "kana-TSU", "kana-TE",
3687 "kana-TO", "kana-NA", "kana-NI", "kana-NU",
3688 "kana-NE", "kana-NO", "kana-HA", "kana-HI",
3689 "kana-FU", "kana-HE", "kana-HO", "kana-MA",
3690 "kana-MI", "kana-MU", "kana-ME", "kana-MO",
3691 "kana-YA", "kana-YU", "kana-YO", "kana-RA",
3692 "kana-RI", "kana-RU", "kana-RE", "kana-RO",
3693 "kana-WA", "kana-N", "voicedsound", "semivoicedsound",
3694 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4e0 .. 0x4ef */
3695 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f0 .. 0x4ff */
3697 #endif /* XK_kana_A */
3699 #define FUNCTION_KEY_OFFSET 0xff00
3701 /* You'll notice that this table is arranged to be conveniently
3702 indexed by X Windows keysym values. */
3703 static char *lispy_function_keys[] =
3705 /* X Keysym value */
3707 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00...0f */
3708 "backspace", "tab", "linefeed", "clear",
3709 0, "return", 0, 0,
3710 0, 0, 0, "pause", /* 0xff10...1f */
3711 0, 0, 0, 0, 0, 0, 0, "escape",
3712 0, 0, 0, 0,
3713 0, "kanji", "muhenkan", "henkan", /* 0xff20...2f */
3714 "romaji", "hiragana", "katakana", "hiragana-katakana",
3715 "zenkaku", "hankaku", "zenkaku-hankaku", "touroku",
3716 "massyo", "kana-lock", "kana-shift", "eisu-shift",
3717 "eisu-toggle", /* 0xff30...3f */
3718 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3719 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
3721 "home", "left", "up", "right", /* 0xff50 */ /* IsCursorKey */
3722 "down", "prior", "next", "end",
3723 "begin", 0, 0, 0, 0, 0, 0, 0,
3724 "select", /* 0xff60 */ /* IsMiscFunctionKey */
3725 "print",
3726 "execute",
3727 "insert",
3728 0, /* 0xff64 */
3729 "undo",
3730 "redo",
3731 "menu",
3732 "find",
3733 "cancel",
3734 "help",
3735 "break", /* 0xff6b */
3737 0, 0, 0, 0,
3738 0, 0, 0, 0, "backtab", 0, 0, 0, /* 0xff70... */
3739 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff78... */
3740 "kp-space", /* 0xff80 */ /* IsKeypadKey */
3741 0, 0, 0, 0, 0, 0, 0, 0,
3742 "kp-tab", /* 0xff89 */
3743 0, 0, 0,
3744 "kp-enter", /* 0xff8d */
3745 0, 0, 0,
3746 "kp-f1", /* 0xff91 */
3747 "kp-f2",
3748 "kp-f3",
3749 "kp-f4",
3750 "kp-home", /* 0xff95 */
3751 "kp-left",
3752 "kp-up",
3753 "kp-right",
3754 "kp-down",
3755 "kp-prior", /* kp-page-up */
3756 "kp-next", /* kp-page-down */
3757 "kp-end",
3758 "kp-begin",
3759 "kp-insert",
3760 "kp-delete",
3761 0, /* 0xffa0 */
3762 0, 0, 0, 0, 0, 0, 0, 0, 0,
3763 "kp-multiply", /* 0xffaa */
3764 "kp-add",
3765 "kp-separator",
3766 "kp-subtract",
3767 "kp-decimal",
3768 "kp-divide", /* 0xffaf */
3769 "kp-0", /* 0xffb0 */
3770 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
3771 0, /* 0xffba */
3772 0, 0,
3773 "kp-equal", /* 0xffbd */
3774 "f1", /* 0xffbe */ /* IsFunctionKey */
3775 "f2",
3776 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
3777 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
3778 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
3779 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
3780 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
3781 0, 0, 0, 0, 0, 0, 0, 0,
3782 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
3783 0, 0, 0, 0, 0, 0, 0, "delete"
3786 /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE. */
3787 #define ISO_FUNCTION_KEY_OFFSET 0xfe00
3789 static char *iso_lispy_function_keys[] =
3791 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe00 */
3792 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe08 */
3793 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe10 */
3794 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe18 */
3795 "iso-lefttab", /* 0xfe20 */
3796 "iso-move-line-up", "iso-move-line-down",
3797 "iso-partial-line-up", "iso-partial-line-down",
3798 "iso-partial-space-left", "iso-partial-space-right",
3799 "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */
3800 "iso-release-margin-left", "iso-release-margin-right",
3801 "iso-release-both-margins",
3802 "iso-fast-cursor-left", "iso-fast-cursor-right",
3803 "iso-fast-cursor-up", "iso-fast-cursor-down",
3804 "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */
3805 "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */
3808 #endif /* not HAVE_NTGUI */
3810 static char *lispy_mouse_names[] =
3812 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
3815 #ifdef WINDOWSNT
3816 /* mouse-wheel events are generated by the wheel on devices such as
3817 the MS Intellimouse. The wheel sits in between the left and right
3818 mouse buttons, and is typically used to scroll or zoom the window
3819 underneath the pointer. mouse-wheel events specify the object on
3820 which they operate, and a delta corresponding to the amount and
3821 direction that the wheel is rotated. Clicking the mouse-wheel
3822 generates a mouse-2 event. */
3823 static char *lispy_mouse_wheel_names[] =
3825 "mouse-wheel"
3828 #endif /* WINDOWSNT */
3830 /* drag-n-drop events are generated when a set of selected files are
3831 dragged from another application and dropped onto an Emacs window. */
3832 static char *lispy_drag_n_drop_names[] =
3834 "drag-n-drop"
3837 /* Scroll bar parts. */
3838 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
3839 Lisp_Object Qup, Qdown;
3841 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
3842 Lisp_Object *scroll_bar_parts[] = {
3843 &Qabove_handle, &Qhandle, &Qbelow_handle,
3844 &Qup, &Qdown,
3848 /* A vector, indexed by button number, giving the down-going location
3849 of currently depressed buttons, both scroll bar and non-scroll bar.
3851 The elements have the form
3852 (BUTTON-NUMBER MODIFIER-MASK . REST)
3853 where REST is the cdr of a position as it would be reported in the event.
3855 The make_lispy_event function stores positions here to tell the
3856 difference between click and drag events, and to store the starting
3857 location to be included in drag events. */
3859 static Lisp_Object button_down_location;
3861 /* Information about the most recent up-going button event: Which
3862 button, what location, and what time. */
3864 static int last_mouse_button;
3865 static int last_mouse_x;
3866 static int last_mouse_y;
3867 static unsigned long button_down_time;
3869 /* The maximum time between clicks to make a double-click,
3870 or Qnil to disable double-click detection,
3871 or Qt for no time limit. */
3872 Lisp_Object Vdouble_click_time;
3874 /* The number of clicks in this multiple-click. */
3876 int double_click_count;
3878 /* Given a struct input_event, build the lisp event which represents
3879 it. If EVENT is 0, build a mouse movement event from the mouse
3880 movement buffer, which should have a movement event in it.
3882 Note that events must be passed to this function in the order they
3883 are received; this function stores the location of button presses
3884 in order to build drag events when the button is released. */
3886 static Lisp_Object
3887 make_lispy_event (event)
3888 struct input_event *event;
3890 int i;
3892 switch (SWITCH_ENUM_CAST (event->kind))
3894 /* A simple keystroke. */
3895 case ascii_keystroke:
3897 Lisp_Object lispy_c;
3898 int c = event->code & 0377;
3899 /* Turn ASCII characters into control characters
3900 when proper. */
3901 if (event->modifiers & ctrl_modifier)
3902 c = make_ctrl_char (c);
3904 /* Add in the other modifier bits. We took care of ctrl_modifier
3905 just above, and the shift key was taken care of by the X code,
3906 and applied to control characters by make_ctrl_char. */
3907 c |= (event->modifiers
3908 & (meta_modifier | alt_modifier
3909 | hyper_modifier | super_modifier));
3910 /* Distinguish Shift-SPC from SPC. */
3911 if ((event->code & 0377) == 040
3912 && event->modifiers & shift_modifier)
3913 c |= shift_modifier;
3914 button_down_time = 0;
3915 XSETFASTINT (lispy_c, c);
3916 return lispy_c;
3919 /* A function key. The symbol may need to have modifier prefixes
3920 tacked onto it. */
3921 case non_ascii_keystroke:
3922 button_down_time = 0;
3924 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
3925 if (event->code == lispy_accent_codes[i])
3926 return modify_event_symbol (i,
3927 event->modifiers,
3928 Qfunction_key, Qnil,
3929 lispy_accent_keys, &accent_key_syms,
3930 (sizeof (lispy_accent_keys)
3931 / sizeof (lispy_accent_keys[0])));
3933 /* Handle system-specific keysyms. */
3934 if (event->code & (1 << 28))
3936 /* We need to use an alist rather than a vector as the cache
3937 since we can't make a vector long enuf. */
3938 if (NILP (current_kboard->system_key_syms))
3939 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
3940 return modify_event_symbol (event->code,
3941 event->modifiers,
3942 Qfunction_key,
3943 current_kboard->Vsystem_key_alist,
3944 0, &current_kboard->system_key_syms,
3945 (unsigned)-1);
3948 #ifdef XK_kana_A
3949 if (event->code >= 0x400 && event->code < 0x500)
3950 return modify_event_symbol (event->code - 0x400,
3951 event->modifiers & ~shift_modifier,
3952 Qfunction_key, Qnil,
3953 lispy_kana_keys, &func_key_syms,
3954 (sizeof (lispy_kana_keys)
3955 / sizeof (lispy_kana_keys[0])));
3956 #endif /* XK_kana_A */
3958 #ifdef ISO_FUNCTION_KEY_OFFSET
3959 if (event->code < FUNCTION_KEY_OFFSET
3960 && event->code >= ISO_FUNCTION_KEY_OFFSET)
3961 return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET,
3962 event->modifiers,
3963 Qfunction_key, Qnil,
3964 iso_lispy_function_keys, &func_key_syms,
3965 (sizeof (iso_lispy_function_keys)
3966 / sizeof (iso_lispy_function_keys[0])));
3967 else
3968 #endif
3969 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
3970 event->modifiers,
3971 Qfunction_key, Qnil,
3972 lispy_function_keys, &func_key_syms,
3973 (sizeof (lispy_function_keys)
3974 / sizeof (lispy_function_keys[0])));
3976 #ifdef HAVE_MOUSE
3977 /* A mouse click. Figure out where it is, decide whether it's
3978 a press, click or drag, and build the appropriate structure. */
3979 case mouse_click:
3980 case scroll_bar_click:
3982 int button = event->code;
3983 int is_double;
3984 Lisp_Object position;
3985 Lisp_Object *start_pos_ptr;
3986 Lisp_Object start_pos;
3988 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
3989 abort ();
3991 /* Build the position as appropriate for this mouse click. */
3992 if (event->kind == mouse_click)
3994 int part;
3995 FRAME_PTR f = XFRAME (event->frame_or_window);
3996 Lisp_Object window;
3997 Lisp_Object posn;
3998 int row, column;
4000 /* Ignore mouse events that were made on frame that
4001 have been deleted. */
4002 if (! FRAME_LIVE_P (f))
4003 return Qnil;
4005 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4006 &column, &row, NULL, 1);
4008 #ifndef USE_X_TOOLKIT
4009 /* In the non-toolkit version, clicks on the menu bar
4010 are ordinary button events in the event buffer.
4011 Distinguish them, and invoke the menu.
4013 (In the toolkit version, the toolkit handles the menu bar
4014 and Emacs doesn't know about it until after the user
4015 makes a selection.) */
4016 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
4017 && (event->modifiers & down_modifier))
4019 Lisp_Object items, item;
4020 int hpos;
4021 int i;
4023 #if 0
4024 /* Activate the menu bar on the down event. If the
4025 up event comes in before the menu code can deal with it,
4026 just ignore it. */
4027 if (! (event->modifiers & down_modifier))
4028 return Qnil;
4029 #endif
4031 item = Qnil;
4032 items = FRAME_MENU_BAR_ITEMS (f);
4033 for (i = 0; i < XVECTOR (items)->size; i += 4)
4035 Lisp_Object pos, string;
4036 string = XVECTOR (items)->contents[i + 1];
4037 pos = XVECTOR (items)->contents[i + 3];
4038 if (NILP (string))
4039 break;
4040 if (column >= XINT (pos)
4041 && column < XINT (pos) + XSTRING (string)->size)
4043 item = XVECTOR (items)->contents[i];
4044 break;
4048 position
4049 = Fcons (event->frame_or_window,
4050 Fcons (Qmenu_bar,
4051 Fcons (Fcons (event->x, event->y),
4052 Fcons (make_number (event->timestamp),
4053 Qnil))));
4055 return Fcons (item, Fcons (position, Qnil));
4057 #endif /* not USE_X_TOOLKIT */
4059 window = window_from_coordinates (f, column, row, &part);
4061 if (!WINDOWP (window))
4063 window = event->frame_or_window;
4064 posn = Qnil;
4066 else
4068 int pixcolumn, pixrow;
4069 column -= WINDOW_LEFT_MARGIN (XWINDOW (window));
4070 row -= XINT (XWINDOW (window)->top);
4071 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
4072 XSETINT (event->x, pixcolumn);
4073 XSETINT (event->y, pixrow);
4075 if (part == 1)
4076 posn = Qmode_line;
4077 else if (part == 2)
4078 posn = Qvertical_line;
4079 else
4080 XSETINT (posn,
4081 buffer_posn_from_coords (XWINDOW (window),
4082 column, row));
4085 position
4086 = Fcons (window,
4087 Fcons (posn,
4088 Fcons (Fcons (event->x, event->y),
4089 Fcons (make_number (event->timestamp),
4090 Qnil))));
4092 else
4094 Lisp_Object window;
4095 Lisp_Object portion_whole;
4096 Lisp_Object part;
4098 window = event->frame_or_window;
4099 portion_whole = Fcons (event->x, event->y);
4100 part = *scroll_bar_parts[(int) event->part];
4102 position
4103 = Fcons (window,
4104 Fcons (Qvertical_scroll_bar,
4105 Fcons (portion_whole,
4106 Fcons (make_number (event->timestamp),
4107 Fcons (part, Qnil)))));
4110 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
4112 start_pos = *start_pos_ptr;
4113 *start_pos_ptr = Qnil;
4115 is_double = (button == last_mouse_button
4116 && XINT (event->x) == last_mouse_x
4117 && XINT (event->y) == last_mouse_y
4118 && button_down_time != 0
4119 && (EQ (Vdouble_click_time, Qt)
4120 || (INTEGERP (Vdouble_click_time)
4121 && ((int)(event->timestamp - button_down_time)
4122 < XINT (Vdouble_click_time)))));
4123 last_mouse_button = button;
4124 last_mouse_x = XINT (event->x);
4125 last_mouse_y = XINT (event->y);
4127 /* If this is a button press, squirrel away the location, so
4128 we can decide later whether it was a click or a drag. */
4129 if (event->modifiers & down_modifier)
4131 if (is_double)
4133 double_click_count++;
4134 event->modifiers |= ((double_click_count > 2)
4135 ? triple_modifier
4136 : double_modifier);
4138 else
4139 double_click_count = 1;
4140 button_down_time = event->timestamp;
4141 *start_pos_ptr = Fcopy_alist (position);
4144 /* Now we're releasing a button - check the co-ordinates to
4145 see if this was a click or a drag. */
4146 else if (event->modifiers & up_modifier)
4148 /* If we did not see a down before this up,
4149 ignore the up. Probably this happened because
4150 the down event chose a menu item.
4151 It would be an annoyance to treat the release
4152 of the button that chose the menu item
4153 as a separate event. */
4155 if (!CONSP (start_pos))
4156 return Qnil;
4158 event->modifiers &= ~up_modifier;
4159 #if 0 /* Formerly we treated an up with no down as a click event. */
4160 if (!CONSP (start_pos))
4161 event->modifiers |= click_modifier;
4162 else
4163 #endif
4165 /* The third element of every position should be the (x,y)
4166 pair. */
4167 Lisp_Object down;
4169 down = Fnth (make_number (2), start_pos);
4170 if (EQ (event->x, XCONS (down)->car)
4171 && EQ (event->y, XCONS (down)->cdr))
4173 event->modifiers |= click_modifier;
4175 else
4177 button_down_time = 0;
4178 event->modifiers |= drag_modifier;
4180 /* Don't check is_double; treat this as multiple
4181 if the down-event was multiple. */
4182 if (double_click_count > 1)
4183 event->modifiers |= ((double_click_count > 2)
4184 ? triple_modifier
4185 : double_modifier);
4188 else
4189 /* Every mouse event should either have the down_modifier or
4190 the up_modifier set. */
4191 abort ();
4194 /* Get the symbol we should use for the mouse click. */
4195 Lisp_Object head;
4197 head = modify_event_symbol (button,
4198 event->modifiers,
4199 Qmouse_click, Qnil,
4200 lispy_mouse_names, &mouse_syms,
4201 (sizeof (lispy_mouse_names)
4202 / sizeof (lispy_mouse_names[0])));
4203 if (event->modifiers & drag_modifier)
4204 return Fcons (head,
4205 Fcons (start_pos,
4206 Fcons (position,
4207 Qnil)));
4208 else if (event->modifiers & (double_modifier | triple_modifier))
4209 return Fcons (head,
4210 Fcons (position,
4211 Fcons (make_number (double_click_count),
4212 Qnil)));
4213 else
4214 return Fcons (head,
4215 Fcons (position,
4216 Qnil));
4220 #ifdef WINDOWSNT
4221 case w32_scroll_bar_click:
4223 int button = event->code;
4224 int is_double;
4225 Lisp_Object position;
4226 Lisp_Object *start_pos_ptr;
4227 Lisp_Object start_pos;
4229 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
4230 abort ();
4233 Lisp_Object window;
4234 Lisp_Object portion_whole;
4235 Lisp_Object part;
4237 window = event->frame_or_window;
4238 portion_whole = Fcons (event->x, event->y);
4239 part = *scroll_bar_parts[(int) event->part];
4241 position
4242 = Fcons (window,
4243 Fcons (Qvertical_scroll_bar,
4244 Fcons (portion_whole,
4245 Fcons (make_number (event->timestamp),
4246 Fcons (part, Qnil)))));
4249 /* Always treat W32 scroll bar events as clicks. */
4250 event->modifiers |= click_modifier;
4253 /* Get the symbol we should use for the mouse click. */
4254 Lisp_Object head;
4256 head = modify_event_symbol (button,
4257 event->modifiers,
4258 Qmouse_click, Qnil,
4259 lispy_mouse_names, &mouse_syms,
4260 (sizeof (lispy_mouse_names)
4261 / sizeof (lispy_mouse_names[0])));
4262 return Fcons (head,
4263 Fcons (position,
4264 Qnil));
4267 case mouse_wheel:
4269 int part;
4270 FRAME_PTR f = XFRAME (event->frame_or_window);
4271 Lisp_Object window;
4272 Lisp_Object posn;
4273 Lisp_Object head, position;
4274 int row, column;
4276 /* Ignore mouse events that were made on frame that
4277 have been deleted. */
4278 if (! FRAME_LIVE_P (f))
4279 return Qnil;
4280 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4281 &column, &row, NULL, 1);
4282 window = window_from_coordinates (f, column, row, &part);
4284 if (!WINDOWP (window))
4286 window = event->frame_or_window;
4287 posn = Qnil;
4289 else
4291 int pixcolumn, pixrow;
4292 column -= XINT (XWINDOW (window)->left);
4293 row -= XINT (XWINDOW (window)->top);
4294 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
4295 XSETINT (event->x, pixcolumn);
4296 XSETINT (event->y, pixrow);
4298 if (part == 1)
4299 posn = Qmode_line;
4300 else if (part == 2)
4301 posn = Qvertical_line;
4302 else
4303 XSETINT (posn,
4304 buffer_posn_from_coords (XWINDOW (window),
4305 column, row));
4309 Lisp_Object head, position;
4311 position
4312 = Fcons (window,
4313 Fcons (posn,
4314 Fcons (Fcons (event->x, event->y),
4315 Fcons (make_number (event->timestamp),
4316 Qnil))));
4318 head = modify_event_symbol (0, event->modifiers,
4319 Qmouse_wheel, Qnil,
4320 lispy_mouse_wheel_names,
4321 &mouse_wheel_syms, 1);
4322 return Fcons (head,
4323 Fcons (position,
4324 Fcons (make_number (event->code),
4325 Qnil)));
4328 #endif /* WINDOWSNT */
4330 case drag_n_drop:
4332 int part;
4333 FRAME_PTR f;
4334 Lisp_Object window;
4335 Lisp_Object posn;
4336 Lisp_Object head, position;
4337 Lisp_Object files;
4338 int row, column;
4340 /* The frame_or_window field should be a cons of the frame in
4341 which the event occurred and a list of the filenames
4342 dropped. */
4343 if (! CONSP (event->frame_or_window))
4344 abort ();
4346 f = XFRAME (XCONS (event->frame_or_window)->car);
4347 files = XCONS (event->frame_or_window)->cdr;
4349 /* Ignore mouse events that were made on frames that
4350 have been deleted. */
4351 if (! FRAME_LIVE_P (f))
4352 return Qnil;
4353 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4354 &column, &row, NULL, 1);
4355 window = window_from_coordinates (f, column, row, &part);
4357 if (!WINDOWP (window))
4359 window = XCONS (event->frame_or_window)->car;
4360 posn = Qnil;
4362 else
4364 int pixcolumn, pixrow;
4365 column -= XINT (XWINDOW (window)->left);
4366 row -= XINT (XWINDOW (window)->top);
4367 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
4368 XSETINT (event->x, pixcolumn);
4369 XSETINT (event->y, pixrow);
4371 if (part == 1)
4372 posn = Qmode_line;
4373 else if (part == 2)
4374 posn = Qvertical_line;
4375 else
4376 XSETINT (posn,
4377 buffer_posn_from_coords (XWINDOW (window),
4378 column, row));
4382 Lisp_Object head, position;
4384 position
4385 = Fcons (window,
4386 Fcons (posn,
4387 Fcons (Fcons (event->x, event->y),
4388 Fcons (make_number (event->timestamp),
4389 Qnil))));
4391 head = modify_event_symbol (0, event->modifiers,
4392 Qdrag_n_drop, Qnil,
4393 lispy_drag_n_drop_names,
4394 &drag_n_drop_syms, 1);
4395 return Fcons (head,
4396 Fcons (position,
4397 Fcons (files,
4398 Qnil)));
4401 #endif /* HAVE_MOUSE */
4403 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
4404 case menu_bar_event:
4405 /* The event value is in the cdr of the frame_or_window slot. */
4406 if (!CONSP (event->frame_or_window))
4407 abort ();
4408 return XCONS (event->frame_or_window)->cdr;
4409 #endif
4411 /* The 'kind' field of the event is something we don't recognize. */
4412 default:
4413 abort ();
4417 #ifdef HAVE_MOUSE
4419 static Lisp_Object
4420 make_lispy_movement (frame, bar_window, part, x, y, time)
4421 FRAME_PTR frame;
4422 Lisp_Object bar_window;
4423 enum scroll_bar_part part;
4424 Lisp_Object x, y;
4425 unsigned long time;
4427 /* Is it a scroll bar movement? */
4428 if (frame && ! NILP (bar_window))
4430 Lisp_Object part_sym;
4432 part_sym = *scroll_bar_parts[(int) part];
4433 return Fcons (Qscroll_bar_movement,
4434 (Fcons (Fcons (bar_window,
4435 Fcons (Qvertical_scroll_bar,
4436 Fcons (Fcons (x, y),
4437 Fcons (make_number (time),
4438 Fcons (part_sym,
4439 Qnil))))),
4440 Qnil)));
4443 /* Or is it an ordinary mouse movement? */
4444 else
4446 int area;
4447 Lisp_Object window;
4448 Lisp_Object posn;
4449 int column, row;
4451 if (frame)
4453 /* It's in a frame; which window on that frame? */
4454 pixel_to_glyph_coords (frame, XINT (x), XINT (y), &column, &row,
4455 NULL, 1);
4456 window = window_from_coordinates (frame, column, row, &area);
4458 else
4459 window = Qnil;
4461 if (WINDOWP (window))
4463 int pixcolumn, pixrow;
4464 column -= WINDOW_LEFT_MARGIN (XWINDOW (window));
4465 row -= XINT (XWINDOW (window)->top);
4466 glyph_to_pixel_coords (frame, column, row, &pixcolumn, &pixrow);
4467 XSETINT (x, pixcolumn);
4468 XSETINT (y, pixrow);
4470 if (area == 1)
4471 posn = Qmode_line;
4472 else if (area == 2)
4473 posn = Qvertical_line;
4474 else
4475 XSETINT (posn,
4476 buffer_posn_from_coords (XWINDOW (window), column, row));
4478 else if (frame != 0)
4480 XSETFRAME (window, frame);
4481 posn = Qnil;
4483 else
4485 window = Qnil;
4486 posn = Qnil;
4487 XSETFASTINT (x, 0);
4488 XSETFASTINT (y, 0);
4491 return Fcons (Qmouse_movement,
4492 Fcons (Fcons (window,
4493 Fcons (posn,
4494 Fcons (Fcons (x, y),
4495 Fcons (make_number (time),
4496 Qnil)))),
4497 Qnil));
4501 #endif /* HAVE_MOUSE */
4503 /* Construct a switch frame event. */
4504 static Lisp_Object
4505 make_lispy_switch_frame (frame)
4506 Lisp_Object frame;
4508 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
4511 /* Manipulating modifiers. */
4513 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
4515 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
4516 SYMBOL's name of the end of the modifiers; the string from this
4517 position is the unmodified symbol name.
4519 This doesn't use any caches. */
4521 static int
4522 parse_modifiers_uncached (symbol, modifier_end)
4523 Lisp_Object symbol;
4524 int *modifier_end;
4526 struct Lisp_String *name;
4527 int i;
4528 int modifiers;
4530 CHECK_SYMBOL (symbol, 1);
4532 modifiers = 0;
4533 name = XSYMBOL (symbol)->name;
4535 for (i = 0; i+2 <= STRING_BYTES (name); )
4537 int this_mod_end = 0;
4538 int this_mod = 0;
4540 /* See if the name continues with a modifier word.
4541 Check that the word appears, but don't check what follows it.
4542 Set this_mod and this_mod_end to record what we find. */
4544 switch (name->data[i])
4546 #define SINGLE_LETTER_MOD(BIT) \
4547 (this_mod_end = i + 1, this_mod = BIT)
4549 case 'A':
4550 SINGLE_LETTER_MOD (alt_modifier);
4551 break;
4553 case 'C':
4554 SINGLE_LETTER_MOD (ctrl_modifier);
4555 break;
4557 case 'H':
4558 SINGLE_LETTER_MOD (hyper_modifier);
4559 break;
4561 case 'M':
4562 SINGLE_LETTER_MOD (meta_modifier);
4563 break;
4565 case 'S':
4566 SINGLE_LETTER_MOD (shift_modifier);
4567 break;
4569 case 's':
4570 SINGLE_LETTER_MOD (super_modifier);
4571 break;
4573 #undef SINGLE_LETTER_MOD
4576 /* If we found no modifier, stop looking for them. */
4577 if (this_mod_end == 0)
4578 break;
4580 /* Check there is a dash after the modifier, so that it
4581 really is a modifier. */
4582 if (this_mod_end >= STRING_BYTES (name)
4583 || name->data[this_mod_end] != '-')
4584 break;
4586 /* This modifier is real; look for another. */
4587 modifiers |= this_mod;
4588 i = this_mod_end + 1;
4591 /* Should we include the `click' modifier? */
4592 if (! (modifiers & (down_modifier | drag_modifier
4593 | double_modifier | triple_modifier))
4594 && i + 7 == STRING_BYTES (name)
4595 && strncmp (name->data + i, "mouse-", 6) == 0
4596 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
4597 modifiers |= click_modifier;
4599 if (modifier_end)
4600 *modifier_end = i;
4602 return modifiers;
4605 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
4606 prepended to the string BASE[0..BASE_LEN-1].
4607 This doesn't use any caches. */
4608 static Lisp_Object
4609 apply_modifiers_uncached (modifiers, base, base_len, base_len_byte)
4610 int modifiers;
4611 char *base;
4612 int base_len, base_len_byte;
4614 /* Since BASE could contain nulls, we can't use intern here; we have
4615 to use Fintern, which expects a genuine Lisp_String, and keeps a
4616 reference to it. */
4617 char *new_mods
4618 = (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
4619 int mod_len;
4622 char *p = new_mods;
4624 /* Only the event queue may use the `up' modifier; it should always
4625 be turned into a click or drag event before presented to lisp code. */
4626 if (modifiers & up_modifier)
4627 abort ();
4629 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
4630 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
4631 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
4632 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
4633 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
4634 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
4635 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
4636 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
4637 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
4638 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
4639 /* The click modifier is denoted by the absence of other modifiers. */
4641 *p = '\0';
4643 mod_len = p - new_mods;
4647 Lisp_Object new_name;
4649 new_name = make_uninit_multibyte_string (mod_len + base_len,
4650 mod_len + base_len_byte);
4651 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
4652 bcopy (base, XSTRING (new_name)->data + mod_len, base_len_byte);
4654 return Fintern (new_name, Qnil);
4659 static char *modifier_names[] =
4661 "up", "down", "drag", "click", "double", "triple", 0, 0,
4662 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4663 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
4665 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
4667 static Lisp_Object modifier_symbols;
4669 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
4670 static Lisp_Object
4671 lispy_modifier_list (modifiers)
4672 int modifiers;
4674 Lisp_Object modifier_list;
4675 int i;
4677 modifier_list = Qnil;
4678 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
4679 if (modifiers & (1<<i))
4680 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
4681 modifier_list);
4683 return modifier_list;
4687 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
4688 where UNMODIFIED is the unmodified form of SYMBOL,
4689 MASK is the set of modifiers present in SYMBOL's name.
4690 This is similar to parse_modifiers_uncached, but uses the cache in
4691 SYMBOL's Qevent_symbol_element_mask property, and maintains the
4692 Qevent_symbol_elements property. */
4694 static Lisp_Object
4695 parse_modifiers (symbol)
4696 Lisp_Object symbol;
4698 Lisp_Object elements;
4700 elements = Fget (symbol, Qevent_symbol_element_mask);
4701 if (CONSP (elements))
4702 return elements;
4703 else
4705 int end;
4706 int modifiers = parse_modifiers_uncached (symbol, &end);
4707 Lisp_Object unmodified;
4708 Lisp_Object mask;
4710 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
4711 STRING_BYTES (XSYMBOL (symbol)->name) - end),
4712 Qnil);
4714 if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1))
4715 abort ();
4716 XSETFASTINT (mask, modifiers);
4717 elements = Fcons (unmodified, Fcons (mask, Qnil));
4719 /* Cache the parsing results on SYMBOL. */
4720 Fput (symbol, Qevent_symbol_element_mask,
4721 elements);
4722 Fput (symbol, Qevent_symbol_elements,
4723 Fcons (unmodified, lispy_modifier_list (modifiers)));
4725 /* Since we know that SYMBOL is modifiers applied to unmodified,
4726 it would be nice to put that in unmodified's cache.
4727 But we can't, since we're not sure that parse_modifiers is
4728 canonical. */
4730 return elements;
4734 /* Apply the modifiers MODIFIERS to the symbol BASE.
4735 BASE must be unmodified.
4737 This is like apply_modifiers_uncached, but uses BASE's
4738 Qmodifier_cache property, if present. It also builds
4739 Qevent_symbol_elements properties, since it has that info anyway.
4741 apply_modifiers copies the value of BASE's Qevent_kind property to
4742 the modified symbol. */
4743 static Lisp_Object
4744 apply_modifiers (modifiers, base)
4745 int modifiers;
4746 Lisp_Object base;
4748 Lisp_Object cache, index, entry, new_symbol;
4750 /* Mask out upper bits. We don't know where this value's been. */
4751 modifiers &= ((EMACS_INT)1 << VALBITS) - 1;
4753 /* The click modifier never figures into cache indices. */
4754 cache = Fget (base, Qmodifier_cache);
4755 XSETFASTINT (index, (modifiers & ~click_modifier));
4756 entry = assq_no_quit (index, cache);
4758 if (CONSP (entry))
4759 new_symbol = XCONS (entry)->cdr;
4760 else
4762 /* We have to create the symbol ourselves. */
4763 new_symbol = apply_modifiers_uncached (modifiers,
4764 XSYMBOL (base)->name->data,
4765 XSYMBOL (base)->name->size,
4766 STRING_BYTES (XSYMBOL (base)->name));
4768 /* Add the new symbol to the base's cache. */
4769 entry = Fcons (index, new_symbol);
4770 Fput (base, Qmodifier_cache, Fcons (entry, cache));
4772 /* We have the parsing info now for free, so add it to the caches. */
4773 XSETFASTINT (index, modifiers);
4774 Fput (new_symbol, Qevent_symbol_element_mask,
4775 Fcons (base, Fcons (index, Qnil)));
4776 Fput (new_symbol, Qevent_symbol_elements,
4777 Fcons (base, lispy_modifier_list (modifiers)));
4780 /* Make sure this symbol is of the same kind as BASE.
4782 You'd think we could just set this once and for all when we
4783 intern the symbol above, but reorder_modifiers may call us when
4784 BASE's property isn't set right; we can't assume that just
4785 because it has a Qmodifier_cache property it must have its
4786 Qevent_kind set right as well. */
4787 if (NILP (Fget (new_symbol, Qevent_kind)))
4789 Lisp_Object kind;
4791 kind = Fget (base, Qevent_kind);
4792 if (! NILP (kind))
4793 Fput (new_symbol, Qevent_kind, kind);
4796 return new_symbol;
4800 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
4801 return a symbol with the modifiers placed in the canonical order.
4802 Canonical order is alphabetical, except for down and drag, which
4803 always come last. The 'click' modifier is never written out.
4805 Fdefine_key calls this to make sure that (for example) C-M-foo
4806 and M-C-foo end up being equivalent in the keymap. */
4808 Lisp_Object
4809 reorder_modifiers (symbol)
4810 Lisp_Object symbol;
4812 /* It's hopefully okay to write the code this way, since everything
4813 will soon be in caches, and no consing will be done at all. */
4814 Lisp_Object parsed;
4816 parsed = parse_modifiers (symbol);
4817 return apply_modifiers ((int) XINT (XCONS (XCONS (parsed)->cdr)->car),
4818 XCONS (parsed)->car);
4822 /* For handling events, we often want to produce a symbol whose name
4823 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
4824 to some base, like the name of a function key or mouse button.
4825 modify_event_symbol produces symbols of this sort.
4827 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
4828 is the name of the i'th symbol. TABLE_SIZE is the number of elements
4829 in the table.
4831 Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
4832 NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
4834 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
4835 persist between calls to modify_event_symbol that it can use to
4836 store a cache of the symbols it's generated for this NAME_TABLE
4837 before. The object stored there may be a vector or an alist.
4839 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
4841 MODIFIERS is a set of modifier bits (as given in struct input_events)
4842 whose prefixes should be applied to the symbol name.
4844 SYMBOL_KIND is the value to be placed in the event_kind property of
4845 the returned symbol.
4847 The symbols we create are supposed to have an
4848 `event-symbol-elements' property, which lists the modifiers present
4849 in the symbol's name. */
4851 static Lisp_Object
4852 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
4853 name_table, symbol_table, table_size)
4854 int symbol_num;
4855 unsigned modifiers;
4856 Lisp_Object symbol_kind;
4857 Lisp_Object name_alist;
4858 char **name_table;
4859 Lisp_Object *symbol_table;
4860 unsigned int table_size;
4862 Lisp_Object value;
4863 Lisp_Object symbol_int;
4865 /* Get rid of the "vendor-specific" bit here. */
4866 XSETINT (symbol_int, symbol_num & 0xffffff);
4868 /* Is this a request for a valid symbol? */
4869 if (symbol_num < 0 || symbol_num >= table_size)
4870 return Qnil;
4872 if (CONSP (*symbol_table))
4873 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
4875 /* If *symbol_table doesn't seem to be initialized properly, fix that.
4876 *symbol_table should be a lisp vector TABLE_SIZE elements long,
4877 where the Nth element is the symbol for NAME_TABLE[N], or nil if
4878 we've never used that symbol before. */
4879 else
4881 if (! VECTORP (*symbol_table)
4882 || XVECTOR (*symbol_table)->size != table_size)
4884 Lisp_Object size;
4886 XSETFASTINT (size, table_size);
4887 *symbol_table = Fmake_vector (size, Qnil);
4890 value = XVECTOR (*symbol_table)->contents[symbol_num];
4893 /* Have we already used this symbol before? */
4894 if (NILP (value))
4896 /* No; let's create it. */
4897 if (!NILP (name_alist))
4898 value = Fcdr_safe (Fassq (symbol_int, name_alist));
4899 else if (name_table != 0 && name_table[symbol_num])
4900 value = intern (name_table[symbol_num]);
4902 #ifdef HAVE_WINDOW_SYSTEM
4903 if (NILP (value))
4905 char *name = x_get_keysym_name (symbol_num);
4906 if (name)
4907 value = intern (name);
4909 #endif
4911 if (NILP (value))
4913 char buf[20];
4914 sprintf (buf, "key-%d", symbol_num);
4915 value = intern (buf);
4918 if (CONSP (*symbol_table))
4919 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
4920 else
4921 XVECTOR (*symbol_table)->contents[symbol_num] = value;
4923 /* Fill in the cache entries for this symbol; this also
4924 builds the Qevent_symbol_elements property, which the user
4925 cares about. */
4926 apply_modifiers (modifiers & click_modifier, value);
4927 Fput (value, Qevent_kind, symbol_kind);
4930 /* Apply modifiers to that symbol. */
4931 return apply_modifiers (modifiers, value);
4934 /* Convert a list that represents an event type,
4935 such as (ctrl meta backspace), into the usual representation of that
4936 event type as a number or a symbol. */
4938 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
4939 "Convert the event description list EVENT-DESC to an event type.\n\
4940 EVENT-DESC should contain one base event type (a character or symbol)\n\
4941 and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\
4942 drag, down, double or triple). The base must be last.\n\
4943 The return value is an event type (a character or symbol) which\n\
4944 has the same base event type and all the specified modifiers.")
4945 (event_desc)
4946 Lisp_Object event_desc;
4948 Lisp_Object base;
4949 int modifiers = 0;
4950 Lisp_Object rest;
4952 base = Qnil;
4953 rest = event_desc;
4954 while (CONSP (rest))
4956 Lisp_Object elt;
4957 int this = 0;
4959 elt = XCONS (rest)->car;
4960 rest = XCONS (rest)->cdr;
4962 /* Given a symbol, see if it is a modifier name. */
4963 if (SYMBOLP (elt) && CONSP (rest))
4964 this = parse_solitary_modifier (elt);
4966 if (this != 0)
4967 modifiers |= this;
4968 else if (!NILP (base))
4969 error ("Two bases given in one event");
4970 else
4971 base = elt;
4975 /* Let the symbol A refer to the character A. */
4976 if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1)
4977 XSETINT (base, XSYMBOL (base)->name->data[0]);
4979 if (INTEGERP (base))
4981 /* Turn (shift a) into A. */
4982 if ((modifiers & shift_modifier) != 0
4983 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
4985 XSETINT (base, XINT (base) - ('a' - 'A'));
4986 modifiers &= ~shift_modifier;
4989 /* Turn (control a) into C-a. */
4990 if (modifiers & ctrl_modifier)
4991 return make_number ((modifiers & ~ctrl_modifier)
4992 | make_ctrl_char (XINT (base)));
4993 else
4994 return make_number (modifiers | XINT (base));
4996 else if (SYMBOLP (base))
4997 return apply_modifiers (modifiers, base);
4998 else
4999 error ("Invalid base event");
5002 /* Try to recognize SYMBOL as a modifier name.
5003 Return the modifier flag bit, or 0 if not recognized. */
5005 static int
5006 parse_solitary_modifier (symbol)
5007 Lisp_Object symbol;
5009 struct Lisp_String *name = XSYMBOL (symbol)->name;
5011 switch (name->data[0])
5013 #define SINGLE_LETTER_MOD(BIT) \
5014 if (STRING_BYTES (name) == 1) \
5015 return BIT;
5017 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
5018 if (LEN == STRING_BYTES (name) \
5019 && ! strncmp (name->data, NAME, LEN)) \
5020 return BIT;
5022 case 'A':
5023 SINGLE_LETTER_MOD (alt_modifier);
5024 break;
5026 case 'a':
5027 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
5028 break;
5030 case 'C':
5031 SINGLE_LETTER_MOD (ctrl_modifier);
5032 break;
5034 case 'c':
5035 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
5036 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
5037 break;
5039 case 'H':
5040 SINGLE_LETTER_MOD (hyper_modifier);
5041 break;
5043 case 'h':
5044 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
5045 break;
5047 case 'M':
5048 SINGLE_LETTER_MOD (meta_modifier);
5049 break;
5051 case 'm':
5052 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
5053 break;
5055 case 'S':
5056 SINGLE_LETTER_MOD (shift_modifier);
5057 break;
5059 case 's':
5060 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
5061 MULTI_LETTER_MOD (super_modifier, "super", 5);
5062 SINGLE_LETTER_MOD (super_modifier);
5063 break;
5065 case 'd':
5066 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
5067 MULTI_LETTER_MOD (down_modifier, "down", 4);
5068 MULTI_LETTER_MOD (double_modifier, "double", 6);
5069 break;
5071 case 't':
5072 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
5073 break;
5075 #undef SINGLE_LETTER_MOD
5076 #undef MULTI_LETTER_MOD
5079 return 0;
5082 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
5083 Such a list is not valid as an event,
5084 but it can be a Lucid-style event type list. */
5087 lucid_event_type_list_p (object)
5088 Lisp_Object object;
5090 Lisp_Object tail;
5092 if (! CONSP (object))
5093 return 0;
5095 for (tail = object; CONSP (tail); tail = XCONS (tail)->cdr)
5097 Lisp_Object elt;
5098 elt = XCONS (tail)->car;
5099 if (! (INTEGERP (elt) || SYMBOLP (elt)))
5100 return 0;
5103 return NILP (tail);
5106 /* Store into *addr a value nonzero if terminal input chars are available.
5107 Serves the purpose of ioctl (0, FIONREAD, addr)
5108 but works even if FIONREAD does not exist.
5109 (In fact, this may actually read some input.)
5111 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe. */
5113 static void
5114 get_input_pending (addr, do_timers_now)
5115 int *addr;
5116 int do_timers_now;
5118 /* First of all, have we already counted some input? */
5119 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
5121 /* If input is being read as it arrives, and we have none, there is none. */
5122 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
5123 return;
5125 /* Try to read some input and see how much we get. */
5126 gobble_input (0);
5127 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
5130 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
5132 void
5133 gobble_input (expected)
5134 int expected;
5136 #ifndef VMS
5137 #ifdef SIGIO
5138 if (interrupt_input)
5140 SIGMASKTYPE mask;
5141 mask = sigblock (sigmask (SIGIO));
5142 read_avail_input (expected);
5143 sigsetmask (mask);
5145 else
5146 #ifdef POLL_FOR_INPUT
5147 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
5149 SIGMASKTYPE mask;
5150 mask = sigblock (sigmask (SIGALRM));
5151 read_avail_input (expected);
5152 sigsetmask (mask);
5154 else
5155 #endif
5156 #endif
5157 read_avail_input (expected);
5158 #endif
5161 /* Put a buffer_switch_event in the buffer
5162 so that read_key_sequence will notice the new current buffer. */
5164 void
5165 record_asynch_buffer_change ()
5167 struct input_event event;
5168 Lisp_Object tem;
5170 event.kind = buffer_switch_event;
5171 event.frame_or_window = Qnil;
5173 #ifdef subprocesses
5174 /* We don't need a buffer-switch event unless Emacs is waiting for input.
5175 The purpose of the event is to make read_key_sequence look up the
5176 keymaps again. If we aren't in read_key_sequence, we don't need one,
5177 and the event could cause trouble by messing up (input-pending-p). */
5178 tem = Fwaiting_for_user_input_p ();
5179 if (NILP (tem))
5180 return;
5181 #else
5182 /* We never need these events if we have no asynchronous subprocesses. */
5183 return;
5184 #endif
5186 /* Make sure no interrupt happens while storing the event. */
5187 #ifdef SIGIO
5188 if (interrupt_input)
5190 SIGMASKTYPE mask;
5191 mask = sigblock (sigmask (SIGIO));
5192 kbd_buffer_store_event (&event);
5193 sigsetmask (mask);
5195 else
5196 #endif
5198 stop_polling ();
5199 kbd_buffer_store_event (&event);
5200 start_polling ();
5204 #ifndef VMS
5206 /* Read any terminal input already buffered up by the system
5207 into the kbd_buffer, but do not wait.
5209 EXPECTED should be nonzero if the caller knows there is some input.
5211 Except on VMS, all input is read by this function.
5212 If interrupt_input is nonzero, this function MUST be called
5213 only when SIGIO is blocked.
5215 Returns the number of keyboard chars read, or -1 meaning
5216 this is a bad time to try to read input. */
5218 static int
5219 read_avail_input (expected)
5220 int expected;
5222 struct input_event buf[KBD_BUFFER_SIZE];
5223 register int i;
5224 int nread;
5226 if (read_socket_hook)
5227 /* No need for FIONREAD or fcntl; just say don't wait. */
5228 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE, expected);
5229 else
5231 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
5232 the kbd_buffer can really hold. That may prevent loss
5233 of characters on some systems when input is stuffed at us. */
5234 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
5235 int n_to_read;
5237 /* Determine how many characters we should *try* to read. */
5238 #ifdef WINDOWSNT
5239 return 0;
5240 #else /* not WINDOWSNT */
5241 #ifdef MSDOS
5242 n_to_read = dos_keysns ();
5243 if (n_to_read == 0)
5244 return 0;
5245 #else /* not MSDOS */
5246 #ifdef FIONREAD
5247 /* Find out how much input is available. */
5248 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
5249 /* Formerly simply reported no input, but that sometimes led to
5250 a failure of Emacs to terminate.
5251 SIGHUP seems appropriate if we can't reach the terminal. */
5252 /* ??? Is it really right to send the signal just to this process
5253 rather than to the whole process group?
5254 Perhaps on systems with FIONREAD Emacs is alone in its group. */
5255 kill (getpid (), SIGHUP);
5256 if (n_to_read == 0)
5257 return 0;
5258 if (n_to_read > sizeof cbuf)
5259 n_to_read = sizeof cbuf;
5260 #else /* no FIONREAD */
5261 #if defined (USG) || defined (DGUX)
5262 /* Read some input if available, but don't wait. */
5263 n_to_read = sizeof cbuf;
5264 fcntl (input_fd, F_SETFL, O_NDELAY);
5265 #else
5266 you lose;
5267 #endif
5268 #endif
5269 #endif /* not MSDOS */
5270 #endif /* not WINDOWSNT */
5272 /* Now read; for one reason or another, this will not block.
5273 NREAD is set to the number of chars read. */
5276 #ifdef MSDOS
5277 cbuf[0] = dos_keyread ();
5278 nread = 1;
5279 #else
5280 nread = read (input_fd, cbuf, n_to_read);
5281 #endif
5282 /* POSIX infers that processes which are not in the session leader's
5283 process group won't get SIGHUP's at logout time. BSDI adheres to
5284 this part standard and returns -1 from read (0) with errno==EIO
5285 when the control tty is taken away.
5286 Jeffrey Honig <jch@bsdi.com> says this is generally safe. */
5287 if (nread == -1 && errno == EIO)
5288 kill (0, SIGHUP);
5289 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
5290 /* The kernel sometimes fails to deliver SIGHUP for ptys.
5291 This looks incorrect, but it isn't, because _BSD causes
5292 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
5293 and that causes a value other than 0 when there is no input. */
5294 if (nread == 0)
5295 kill (0, SIGHUP);
5296 #endif
5298 while (
5299 /* We used to retry the read if it was interrupted.
5300 But this does the wrong thing when O_NDELAY causes
5301 an EAGAIN error. Does anybody know of a situation
5302 where a retry is actually needed? */
5303 #if 0
5304 nread < 0 && (errno == EAGAIN
5305 #ifdef EFAULT
5306 || errno == EFAULT
5307 #endif
5308 #ifdef EBADSLT
5309 || errno == EBADSLT
5310 #endif
5312 #else
5314 #endif
5317 #ifndef FIONREAD
5318 #if defined (USG) || defined (DGUX)
5319 fcntl (input_fd, F_SETFL, 0);
5320 #endif /* USG or DGUX */
5321 #endif /* no FIONREAD */
5322 for (i = 0; i < nread; i++)
5324 buf[i].kind = ascii_keystroke;
5325 buf[i].modifiers = 0;
5326 if (meta_key == 1 && (cbuf[i] & 0x80))
5327 buf[i].modifiers = meta_modifier;
5328 if (meta_key != 2)
5329 cbuf[i] &= ~0x80;
5331 buf[i].code = cbuf[i];
5332 XSETFRAME (buf[i].frame_or_window, selected_frame);
5336 /* Scan the chars for C-g and store them in kbd_buffer. */
5337 for (i = 0; i < nread; i++)
5339 kbd_buffer_store_event (&buf[i]);
5340 /* Don't look at input that follows a C-g too closely.
5341 This reduces lossage due to autorepeat on C-g. */
5342 if (buf[i].kind == ascii_keystroke
5343 && buf[i].code == quit_char)
5344 break;
5347 return nread;
5349 #endif /* not VMS */
5351 #ifdef SIGIO /* for entire page */
5352 /* Note SIGIO has been undef'd if FIONREAD is missing. */
5354 SIGTYPE
5355 input_available_signal (signo)
5356 int signo;
5358 /* Must preserve main program's value of errno. */
5359 int old_errno = errno;
5360 #ifdef BSD4_1
5361 extern int select_alarmed;
5362 #endif
5364 #if defined (USG) && !defined (POSIX_SIGNALS)
5365 /* USG systems forget handlers when they are used;
5366 must reestablish each time */
5367 signal (signo, input_available_signal);
5368 #endif /* USG */
5370 #ifdef BSD4_1
5371 sigisheld (SIGIO);
5372 #endif
5374 if (input_available_clear_time)
5375 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
5377 while (1)
5379 int nread;
5380 nread = read_avail_input (1);
5381 /* -1 means it's not ok to read the input now.
5382 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
5383 0 means there was no keyboard input available. */
5384 if (nread <= 0)
5385 break;
5387 #ifdef BSD4_1
5388 select_alarmed = 1; /* Force the select emulator back to life */
5389 #endif
5392 #ifdef BSD4_1
5393 sigfree ();
5394 #endif
5395 errno = old_errno;
5397 #endif /* SIGIO */
5399 /* Send ourselves a SIGIO.
5401 This function exists so that the UNBLOCK_INPUT macro in
5402 blockinput.h can have some way to take care of input we put off
5403 dealing with, without assuming that every file which uses
5404 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
5405 void
5406 reinvoke_input_signal ()
5408 #ifdef SIGIO
5409 kill (getpid (), SIGIO);
5410 #endif
5415 /* Return the prompt-string of a sparse keymap.
5416 This is the first element which is a string.
5417 Return nil if there is none. */
5419 Lisp_Object
5420 map_prompt (map)
5421 Lisp_Object map;
5423 while (CONSP (map))
5425 register Lisp_Object tem;
5426 tem = Fcar (map);
5427 if (STRINGP (tem))
5428 return tem;
5429 map = Fcdr (map);
5431 return Qnil;
5434 static void menu_bar_item ();
5435 static void menu_bar_one_keymap ();
5437 /* These variables hold the vector under construction within
5438 menu_bar_items and its subroutines, and the current index
5439 for storing into that vector. */
5440 static Lisp_Object menu_bar_items_vector;
5441 static int menu_bar_items_index;
5443 /* Return a vector of menu items for a menu bar, appropriate
5444 to the current buffer. Each item has three elements in the vector:
5445 KEY STRING MAPLIST.
5447 OLD is an old vector we can optionally reuse, or nil. */
5449 Lisp_Object
5450 menu_bar_items (old)
5451 Lisp_Object old;
5453 /* The number of keymaps we're scanning right now, and the number of
5454 keymaps we have allocated space for. */
5455 int nmaps;
5457 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
5458 in the current keymaps, or nil where it is not a prefix. */
5459 Lisp_Object *maps;
5461 Lisp_Object def, tem, tail;
5463 Lisp_Object result;
5465 int mapno;
5466 Lisp_Object oquit;
5468 int i;
5470 struct gcpro gcpro1;
5472 /* In order to build the menus, we need to call the keymap
5473 accessors. They all call QUIT. But this function is called
5474 during redisplay, during which a quit is fatal. So inhibit
5475 quitting while building the menus.
5476 We do this instead of specbind because (1) errors will clear it anyway
5477 and (2) this avoids risk of specpdl overflow. */
5478 oquit = Vinhibit_quit;
5479 Vinhibit_quit = Qt;
5481 if (!NILP (old))
5482 menu_bar_items_vector = old;
5483 else
5484 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
5485 menu_bar_items_index = 0;
5487 GCPRO1 (menu_bar_items_vector);
5489 /* Build our list of keymaps.
5490 If we recognize a function key and replace its escape sequence in
5491 keybuf with its symbol, or if the sequence starts with a mouse
5492 click and we need to switch buffers, we jump back here to rebuild
5493 the initial keymaps from the current buffer. */
5495 Lisp_Object *tmaps;
5497 /* Should overriding-terminal-local-map and overriding-local-map apply? */
5498 if (!NILP (Voverriding_local_map_menu_flag))
5500 /* Yes, use them (if non-nil) as well as the global map. */
5501 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
5502 nmaps = 0;
5503 if (!NILP (current_kboard->Voverriding_terminal_local_map))
5504 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
5505 if (!NILP (Voverriding_local_map))
5506 maps[nmaps++] = Voverriding_local_map;
5508 else
5510 /* No, so use major and minor mode keymaps. */
5511 nmaps = current_minor_maps (NULL, &tmaps);
5512 maps = (Lisp_Object *) alloca ((nmaps + 2) * sizeof (maps[0]));
5513 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
5514 #ifdef USE_TEXT_PROPERTIES
5515 maps[nmaps++] = get_local_map (PT, current_buffer);
5516 #else
5517 maps[nmaps++] = current_buffer->keymap;
5518 #endif
5520 maps[nmaps++] = current_global_map;
5523 /* Look up in each map the dummy prefix key `menu-bar'. */
5525 result = Qnil;
5527 for (mapno = nmaps - 1; mapno >= 0; mapno--)
5529 if (! NILP (maps[mapno]))
5530 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0), 0);
5531 else
5532 def = Qnil;
5534 tem = Fkeymapp (def);
5535 if (!NILP (tem))
5536 menu_bar_one_keymap (def);
5539 /* Move to the end those items that should be at the end. */
5541 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCONS (tail)->cdr)
5543 int i;
5544 int end = menu_bar_items_index;
5546 for (i = 0; i < end; i += 4)
5547 if (EQ (XCONS (tail)->car, XVECTOR (menu_bar_items_vector)->contents[i]))
5549 Lisp_Object tem0, tem1, tem2, tem3;
5550 /* Move the item at index I to the end,
5551 shifting all the others forward. */
5552 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
5553 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
5554 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
5555 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
5556 if (end > i + 4)
5557 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
5558 &XVECTOR (menu_bar_items_vector)->contents[i],
5559 (end - i - 4) * sizeof (Lisp_Object));
5560 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
5561 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
5562 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
5563 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
5564 break;
5568 /* Add nil, nil, nil, nil at the end. */
5569 i = menu_bar_items_index;
5570 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
5572 Lisp_Object tem;
5573 int newsize = 2 * i;
5574 tem = Fmake_vector (make_number (2 * i), Qnil);
5575 bcopy (XVECTOR (menu_bar_items_vector)->contents,
5576 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
5577 menu_bar_items_vector = tem;
5579 /* Add this item. */
5580 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5581 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5582 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5583 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5584 menu_bar_items_index = i;
5586 Vinhibit_quit = oquit;
5587 UNGCPRO;
5588 return menu_bar_items_vector;
5591 /* Scan one map KEYMAP, accumulating any menu items it defines
5592 in menu_bar_items_vector. */
5594 static void
5595 menu_bar_one_keymap (keymap)
5596 Lisp_Object keymap;
5598 Lisp_Object tail, item, table;
5600 /* Loop over all keymap entries that have menu strings. */
5601 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
5603 item = XCONS (tail)->car;
5604 if (CONSP (item))
5605 menu_bar_item (XCONS (item)->car, XCONS (item)->cdr);
5606 else if (VECTORP (item))
5608 /* Loop over the char values represented in the vector. */
5609 int len = XVECTOR (item)->size;
5610 int c;
5611 for (c = 0; c < len; c++)
5613 Lisp_Object character;
5614 XSETFASTINT (character, c);
5615 menu_bar_item (character, XVECTOR (item)->contents[c]);
5621 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
5622 If there's already an item for KEY, add this DEF to it. */
5624 Lisp_Object item_properties;
5626 static void
5627 menu_bar_item (key, item)
5628 Lisp_Object key, item;
5630 struct gcpro gcpro1;
5631 int i;
5633 if (EQ (item, Qundefined))
5635 /* If a map has an explicit `undefined' as definition,
5636 discard any previously made menu bar item. */
5638 for (i = 0; i < menu_bar_items_index; i += 4)
5639 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
5641 if (menu_bar_items_index > i + 4)
5642 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
5643 &XVECTOR (menu_bar_items_vector)->contents[i],
5644 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
5645 menu_bar_items_index -= 4;
5646 return;
5649 /* If there's no definition for this key yet,
5650 just ignore `undefined'. */
5651 return;
5654 GCPRO1 (key); /* Is this necessary? */
5655 i = parse_menu_item (item, 0, 1);
5656 UNGCPRO;
5657 if (!i)
5658 return;
5660 item = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
5662 /* Find any existing item for this KEY. */
5663 for (i = 0; i < menu_bar_items_index; i += 4)
5664 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
5665 break;
5667 /* If we did not find this KEY, add it at the end. */
5668 if (i == menu_bar_items_index)
5670 /* If vector is too small, get a bigger one. */
5671 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
5673 Lisp_Object tem;
5674 int newsize = 2 * i;
5675 tem = Fmake_vector (make_number (2 * i), Qnil);
5676 bcopy (XVECTOR (menu_bar_items_vector)->contents,
5677 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
5678 menu_bar_items_vector = tem;
5681 /* Add this item. */
5682 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
5683 XVECTOR (menu_bar_items_vector)->contents[i++]
5684 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
5685 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (item, Qnil);
5686 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
5687 menu_bar_items_index = i;
5689 /* We did find an item for this KEY. Add ITEM to its list of maps. */
5690 else
5692 Lisp_Object old;
5693 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
5694 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (item, old);
5698 /* This is used as the handler when calling menu_item_eval_property. */
5699 static Lisp_Object
5700 menu_item_eval_property_1 (arg)
5701 Lisp_Object arg;
5703 /* If we got a quit from within the menu computation,
5704 quit all the way out of it. This takes care of C-] in the debugger. */
5705 if (CONSP (arg) && EQ (XCONS (arg)->car, Qquit))
5706 Fsignal (Qquit, Qnil);
5708 return Qnil;
5711 /* Evaluate an expression and return the result (or nil if something
5712 went wrong). Used to evaluate dynamic parts of menu items. */
5713 static Lisp_Object
5714 menu_item_eval_property (sexpr)
5715 Lisp_Object sexpr;
5717 int count = specpdl_ptr - specpdl;
5718 Lisp_Object val;
5719 specbind (Qinhibit_redisplay, Qt);
5720 val = internal_condition_case_1 (Feval, sexpr, Qerror,
5721 menu_item_eval_property_1);
5722 return unbind_to (count, val);
5725 /* This function parses a menu item and leaves the result in the
5726 vector item_properties.
5727 ITEM is a key binding, a possible menu item.
5728 If NOTREAL is nonzero, only check for equivalent key bindings, don't
5729 evaluate dynamic expressions in the menu item.
5730 INMENUBAR is > 0 when this is considered for an entry in a menu bar
5731 top level.
5732 INMENUBAR is < 0 when this is considered for an entry in a keyboard menu.
5733 parse_menu_item returns true if the item is a menu item and false
5734 otherwise. */
5737 parse_menu_item (item, notreal, inmenubar)
5738 Lisp_Object item;
5739 int notreal, inmenubar;
5741 Lisp_Object def, tem, item_string, start;
5742 Lisp_Object cachelist = Qnil;
5743 Lisp_Object filter = Qnil;
5744 Lisp_Object keyhint = Qnil;
5745 int i;
5746 int newcache = 0;
5748 if (!CONSP (item))
5749 return 0;
5751 /* Create item_properties vector if necessary. */
5752 if (NILP (item_properties))
5753 item_properties
5754 = Fmake_vector (make_number (ITEM_PROPERTY_ENABLE + 1), Qnil);
5756 /* Initialize optional entries. */
5757 for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++)
5758 XVECTOR (item_properties)->contents[i] = Qnil;
5759 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = Qt;
5761 /* Save the item here to protect it from GC. */
5762 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ITEM] = item;
5764 item_string = XCONS (item)->car;
5766 start = item;
5767 item = XCONS (item)->cdr;
5768 if (STRINGP (item_string))
5770 /* Old format menu item. */
5771 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME] = item_string;
5773 /* Maybe help string. */
5774 if (CONSP (item) && STRINGP (XCONS (item)->car))
5776 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]
5777 = XCONS (item)->car;
5778 start = item;
5779 item = XCONS (item)->cdr;
5782 /* Maybee key binding cache. */
5783 if (CONSP (item) && CONSP (XCONS (item)->car)
5784 && (NILP (XCONS (XCONS (item)->car)->car)
5785 || VECTORP (XCONS (XCONS (item)->car)->car)))
5787 cachelist = XCONS (item)->car;
5788 item = XCONS (item)->cdr;
5791 /* This is the real definition--the function to run. */
5792 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = item;
5794 /* Get enable property, if any. */
5795 if (SYMBOLP (item))
5797 tem = Fget (item, Qmenu_enable);
5798 if (!NILP (tem))
5799 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = tem;
5802 else if (EQ (item_string, Qmenu_item) && CONSP (item))
5804 /* New format menu item. */
5805 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME]
5806 = XCONS (item)->car;
5807 start = XCONS (item)->cdr;
5808 if (CONSP (start))
5810 /* We have a real binding. */
5811 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF]
5812 = XCONS (start)->car;
5814 item = XCONS (start)->cdr;
5815 /* Is there a cache list with key equivalences. */
5816 if (CONSP (item) && CONSP (XCONS (item)->car))
5818 cachelist = XCONS (item)->car;
5819 item = XCONS (item)->cdr;
5822 /* Parse properties. */
5823 while (CONSP (item) && CONSP (XCONS (item)->cdr))
5825 tem = XCONS (item)->car;
5826 item = XCONS (item)->cdr;
5828 if (EQ (tem, QCenable))
5829 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE]
5830 = XCONS (item)->car;
5831 else if (EQ (tem, QCvisible) && !notreal)
5833 /* If got a visible property and that evaluates to nil
5834 then ignore this item. */
5835 tem = menu_item_eval_property (XCONS (item)->car);
5836 if (NILP (tem))
5837 return 0;
5839 else if (EQ (tem, QChelp))
5840 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]
5841 = XCONS (item)->car;
5842 else if (EQ (tem, QCfilter))
5843 filter = item;
5844 else if (EQ (tem, QCkey_sequence))
5846 tem = XCONS (item)->car;
5847 if (NILP (cachelist)
5848 && (SYMBOLP (tem) || STRINGP (tem) || VECTORP (tem)))
5849 /* Be GC protected. Set keyhint to item instead of tem. */
5850 keyhint = item;
5852 else if (EQ (tem, QCkeys))
5854 tem = XCONS (item)->car;
5855 if (CONSP (tem) || STRINGP (tem) && NILP (cachelist))
5856 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ]
5857 = tem;
5859 else if (EQ (tem, QCbutton) && CONSP (XCONS (item)->car))
5861 Lisp_Object type;
5862 tem = XCONS (item)->car;
5863 type = XCONS (tem)->car;
5864 if (EQ (type, QCtoggle) || EQ (type, QCradio))
5866 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]
5867 = XCONS (tem)->cdr;
5868 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE]
5869 = type;
5872 item = XCONS (item)->cdr;
5875 else if (inmenubar || !NILP (start))
5876 return 0;
5878 else
5879 return 0; /* not a menu item */
5881 /* If item string is not a string, evaluate it to get string.
5882 If we don't get a string, skip this item. */
5883 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
5884 if (!(STRINGP (item_string) || notreal))
5886 item_string = menu_item_eval_property (item_string);
5887 if (!STRINGP (item_string))
5888 return 0;
5889 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME] = item_string;
5892 /* If got a filter apply it on definition. */
5893 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
5894 if (!NILP (filter))
5896 def = menu_item_eval_property (list2 (XCONS (filter)->car,
5897 list2 (Qquote, def)));
5899 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = def;
5902 /* If we got no definition, this item is just unselectable text which
5903 is OK in a submenu but not in the menubar. */
5904 if (NILP (def))
5905 return (inmenubar ? 0 : 1);
5907 /* Enable or disable selection of item. */
5908 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
5909 if (!EQ (tem, Qt))
5911 if (notreal)
5912 tem = Qt;
5913 else
5914 tem = menu_item_eval_property (tem);
5915 if (inmenubar && NILP (tem))
5916 return 0; /* Ignore disabled items in menu bar. */
5917 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = tem;
5920 /* See if this is a separate pane or a submenu. */
5921 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
5922 tem = get_keymap_1 (def, 0, 1);
5923 if (!NILP (tem))
5925 XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP] = tem;
5926 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = tem;
5927 return 1;
5929 else if (inmenubar > 0)
5930 return 0; /* Entries in menu bar must be submenus. */
5932 /* This is a command. See if there is an equivalent key binding. */
5933 if (NILP (cachelist))
5935 /* We have to create a cachelist. */
5936 CHECK_IMPURE (start);
5937 XCONS (start)->cdr = Fcons (Fcons (Qnil, Qnil), XCONS (start)->cdr);
5938 cachelist = XCONS (XCONS (start)->cdr)->car;
5939 newcache = 1;
5940 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
5941 if (!NILP (keyhint))
5943 XCONS (cachelist)->car = XCONS (keyhint)->car;
5944 newcache = 0;
5946 else if (STRINGP (tem))
5948 XCONS (cachelist)->cdr = Fsubstitute_command_keys (tem);
5949 XCONS (cachelist)->car = Qt;
5952 tem = XCONS (cachelist)->car;
5953 if (!EQ (tem, Qt))
5955 int chkcache = 0;
5956 Lisp_Object prefix;
5958 if (!NILP (tem))
5959 tem = Fkey_binding (tem, Qnil);
5961 prefix = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
5962 if (CONSP (prefix))
5964 def = XCONS (prefix)->car;
5965 prefix = XCONS (prefix)->cdr;
5967 else
5968 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
5970 if (NILP (XCONS (cachelist)->car)) /* Have no saved key. */
5972 if (newcache /* Always check first time. */
5973 /* Should we check everything when precomputing key
5974 bindings? */
5975 /* || notreal */
5976 /* If something had no key binding before, don't recheck it
5977 because that is too slow--except if we have a list of
5978 rebound commands in Vdefine_key_rebound_commands, do
5979 recheck any command that appears in that list. */
5980 || (CONSP (Vdefine_key_rebound_commands)
5981 && !NILP (Fmemq (def, Vdefine_key_rebound_commands))))
5982 chkcache = 1;
5984 /* We had a saved key. Is it still bound to the command? */
5985 else if (NILP (tem)
5986 || !EQ (tem, def)
5987 /* If the command is an alias for another
5988 (such as lmenu.el set it up), check if the
5989 original command matches the cached command. */
5990 && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function)))
5991 chkcache = 1; /* Need to recompute key binding. */
5993 if (chkcache)
5995 /* Recompute equivalent key binding. If the command is an alias
5996 for another (such as lmenu.el set it up), see if the original
5997 command name has equivalent keys. Otherwise look up the
5998 specified command itself. We don't try both, because that
5999 makes lmenu menus slow. */
6000 if (SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
6001 && ! NILP (Fget (def, Qmenu_alias)))
6002 def = XSYMBOL (def)->function;
6003 tem = Fwhere_is_internal (def, Qnil, Qt, Qnil);
6004 XCONS (cachelist)->car = tem;
6005 if (NILP (tem))
6007 XCONS (cachelist)->cdr = Qnil;
6008 chkcache = 0;
6011 else if (!NILP (keyhint) && !NILP (XCONS (cachelist)->car))
6013 tem = XCONS (cachelist)->car;
6014 chkcache = 1;
6017 newcache = chkcache;
6018 if (chkcache)
6020 tem = Fkey_description (tem);
6021 if (CONSP (prefix))
6023 if (STRINGP (XCONS (prefix)->car))
6024 tem = concat2 (XCONS (prefix)->car, tem);
6025 if (STRINGP (XCONS (prefix)->cdr))
6026 tem = concat2 (tem, XCONS (prefix)->cdr);
6028 XCONS (cachelist)->cdr = tem;
6032 tem = XCONS (cachelist)->cdr;
6033 if (newcache && !NILP (tem))
6035 tem = concat3 (build_string (" ("), tem, build_string (")"));
6036 XCONS (cachelist)->cdr = tem;
6039 /* If we only want to precompute equivalent key bindings, stop here. */
6040 if (notreal)
6041 return 1;
6043 /* If we have an equivalent key binding, use that. */
6044 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ] = tem;
6046 /* Include this when menu help is implemented.
6047 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP];
6048 if (!(NILP (tem) || STRINGP (tem)))
6050 tem = menu_item_eval_property (tem);
6051 if (!STRINGP (tem))
6052 tem = Qnil;
6053 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP] = tem;
6057 /* Handle radio buttons or toggle boxes. */
6058 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
6059 if (!NILP (tem))
6060 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]
6061 = menu_item_eval_property (tem);
6063 return 1;
6066 /* Read a character using menus based on maps in the array MAPS.
6067 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
6068 Return t if we displayed a menu but the user rejected it.
6070 PREV_EVENT is the previous input event, or nil if we are reading
6071 the first event of a key sequence.
6073 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
6074 if we used a mouse menu to read the input, or zero otherwise. If
6075 USED_MOUSE_MENU is null, we don't dereference it.
6077 The prompting is done based on the prompt-string of the map
6078 and the strings associated with various map elements.
6080 This can be done with X menus or with menus put in the minibuf.
6081 These are done in different ways, depending on how the input will be read.
6082 Menus using X are done after auto-saving in read-char, getting the input
6083 event from Fx_popup_menu; menus using the minibuf use read_char recursively
6084 and do auto-saving in the inner call of read_char. */
6086 static Lisp_Object
6087 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
6088 int nmaps;
6089 Lisp_Object *maps;
6090 Lisp_Object prev_event;
6091 int *used_mouse_menu;
6093 int mapno;
6094 register Lisp_Object name;
6095 Lisp_Object rest, vector;
6097 if (used_mouse_menu)
6098 *used_mouse_menu = 0;
6100 /* Use local over global Menu maps */
6102 if (! menu_prompting)
6103 return Qnil;
6105 /* Optionally disregard all but the global map. */
6106 if (inhibit_local_menu_bar_menus)
6108 maps += (nmaps - 1);
6109 nmaps = 1;
6112 /* Get the menu name from the first map that has one (a prompt string). */
6113 for (mapno = 0; mapno < nmaps; mapno++)
6115 name = map_prompt (maps[mapno]);
6116 if (!NILP (name))
6117 break;
6120 /* If we don't have any menus, just read a character normally. */
6121 if (mapno >= nmaps)
6122 return Qnil;
6124 #ifdef HAVE_MENUS
6125 /* If we got to this point via a mouse click,
6126 use a real menu for mouse selection. */
6127 if (EVENT_HAS_PARAMETERS (prev_event)
6128 && !EQ (XCONS (prev_event)->car, Qmenu_bar))
6130 /* Display the menu and get the selection. */
6131 Lisp_Object *realmaps
6132 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
6133 Lisp_Object value;
6134 int nmaps1 = 0;
6136 /* Use the maps that are not nil. */
6137 for (mapno = 0; mapno < nmaps; mapno++)
6138 if (!NILP (maps[mapno]))
6139 realmaps[nmaps1++] = maps[mapno];
6141 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
6142 if (CONSP (value))
6144 Lisp_Object tem;
6146 record_menu_key (XCONS (value)->car);
6148 /* If we got multiple events, unread all but
6149 the first.
6150 There is no way to prevent those unread events
6151 from showing up later in last_nonmenu_event.
6152 So turn symbol and integer events into lists,
6153 to indicate that they came from a mouse menu,
6154 so that when present in last_nonmenu_event
6155 they won't confuse things. */
6156 for (tem = XCONS (value)->cdr; !NILP (tem);
6157 tem = XCONS (tem)->cdr)
6159 record_menu_key (XCONS (tem)->car);
6160 if (SYMBOLP (XCONS (tem)->car)
6161 || INTEGERP (XCONS (tem)->car))
6162 XCONS (tem)->car
6163 = Fcons (XCONS (tem)->car, Qnil);
6166 /* If we got more than one event, put all but the first
6167 onto this list to be read later.
6168 Return just the first event now. */
6169 Vunread_command_events
6170 = nconc2 (XCONS (value)->cdr, Vunread_command_events);
6171 value = XCONS (value)->car;
6173 else if (NILP (value))
6174 value = Qt;
6175 if (used_mouse_menu)
6176 *used_mouse_menu = 1;
6177 return value;
6179 #endif /* HAVE_MENUS */
6180 return Qnil ;
6183 /* Buffer in use so far for the minibuf prompts for menu keymaps.
6184 We make this bigger when necessary, and never free it. */
6185 static char *read_char_minibuf_menu_text;
6186 /* Size of that buffer. */
6187 static int read_char_minibuf_menu_width;
6189 static Lisp_Object
6190 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
6191 int commandflag ;
6192 int nmaps;
6193 Lisp_Object *maps;
6195 int mapno;
6196 register Lisp_Object name;
6197 int nlength;
6198 int width = FRAME_WIDTH (selected_frame) - 4;
6199 int idx = -1;
6200 int nobindings = 1;
6201 Lisp_Object rest, vector;
6202 char *menu;
6204 if (! menu_prompting)
6205 return Qnil;
6207 /* Make sure we have a big enough buffer for the menu text. */
6208 if (read_char_minibuf_menu_text == 0)
6210 read_char_minibuf_menu_width = width + 4;
6211 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
6213 else if (width + 4 > read_char_minibuf_menu_width)
6215 read_char_minibuf_menu_width = width + 4;
6216 read_char_minibuf_menu_text
6217 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
6219 menu = read_char_minibuf_menu_text;
6221 /* Get the menu name from the first map that has one (a prompt string). */
6222 for (mapno = 0; mapno < nmaps; mapno++)
6224 name = map_prompt (maps[mapno]);
6225 if (!NILP (name))
6226 break;
6229 /* If we don't have any menus, just read a character normally. */
6230 if (mapno >= nmaps)
6231 return Qnil;
6233 /* Prompt string always starts with map's prompt, and a space. */
6234 strcpy (menu, XSTRING (name)->data);
6235 nlength = STRING_BYTES (XSTRING (name));
6236 menu[nlength++] = ':';
6237 menu[nlength++] = ' ';
6238 menu[nlength] = 0;
6240 /* Start prompting at start of first map. */
6241 mapno = 0;
6242 rest = maps[mapno];
6244 /* Present the documented bindings, a line at a time. */
6245 while (1)
6247 int notfirst = 0;
6248 int i = nlength;
6249 Lisp_Object obj;
6250 int ch;
6251 Lisp_Object orig_defn_macro;
6253 /* Loop over elements of map. */
6254 while (i < width)
6256 Lisp_Object elt;
6258 /* If reached end of map, start at beginning of next map. */
6259 if (NILP (rest))
6261 mapno++;
6262 /* At end of last map, wrap around to first map if just starting,
6263 or end this line if already have something on it. */
6264 if (mapno == nmaps)
6266 mapno = 0;
6267 if (notfirst || nobindings) break;
6269 rest = maps[mapno];
6272 /* Look at the next element of the map. */
6273 if (idx >= 0)
6274 elt = XVECTOR (vector)->contents[idx];
6275 else
6276 elt = Fcar_safe (rest);
6278 if (idx < 0 && VECTORP (elt))
6280 /* If we found a dense table in the keymap,
6281 advanced past it, but start scanning its contents. */
6282 rest = Fcdr_safe (rest);
6283 vector = elt;
6284 idx = 0;
6286 else
6288 /* An ordinary element. */
6289 Lisp_Object event, tem;
6291 if (idx < 0)
6293 event = Fcar_safe (elt); /* alist */
6294 elt = Fcdr_safe (elt);
6296 else
6298 XSETINT (event, idx); /* vector */
6301 /* Ignore the element if it has no prompt string. */
6302 if (INTEGERP (event) && parse_menu_item (elt, 0, -1))
6304 /* 1 if the char to type matches the string. */
6305 int char_matches;
6306 Lisp_Object upcased_event, downcased_event;
6307 Lisp_Object desc;
6308 Lisp_Object s
6309 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
6311 upcased_event = Fupcase (event);
6312 downcased_event = Fdowncase (event);
6313 char_matches = (XINT (upcased_event) == XSTRING (s)->data[0]
6314 || XINT (downcased_event) == XSTRING (s)->data[0]);
6315 if (! char_matches)
6316 desc = Fsingle_key_description (event);
6319 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
6320 if (!NILP (tem))
6321 /* Insert equivalent keybinding. */
6322 s = concat2 (s, tem);
6325 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
6326 if (EQ (tem, QCradio) || EQ (tem, QCtoggle))
6328 /* Insert button prefix. */
6329 Lisp_Object selected
6330 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
6331 if (EQ (tem, QCradio))
6332 tem = build_string (NILP (selected) ? "(*) " : "( ) ");
6333 else
6334 tem = build_string (NILP (selected) ? "[X] " : "[ ] ");
6335 s = concat2 (tem, s);
6339 /* If we have room for the prompt string, add it to this line.
6340 If this is the first on the line, always add it. */
6341 if ((XSTRING (s)->size + i + 2
6342 + (char_matches ? 0 : XSTRING (desc)->size + 3))
6343 < width
6344 || !notfirst)
6346 int thiswidth;
6348 /* Punctuate between strings. */
6349 if (notfirst)
6351 strcpy (menu + i, ", ");
6352 i += 2;
6354 notfirst = 1;
6355 nobindings = 0 ;
6357 /* If the char to type doesn't match the string's
6358 first char, explicitly show what char to type. */
6359 if (! char_matches)
6361 /* Add as much of string as fits. */
6362 thiswidth = XSTRING (desc)->size;
6363 if (thiswidth + i > width)
6364 thiswidth = width - i;
6365 bcopy (XSTRING (desc)->data, menu + i, thiswidth);
6366 i += thiswidth;
6367 strcpy (menu + i, " = ");
6368 i += 3;
6371 /* Add as much of string as fits. */
6372 thiswidth = XSTRING (s)->size;
6373 if (thiswidth + i > width)
6374 thiswidth = width - i;
6375 bcopy (XSTRING (s)->data, menu + i, thiswidth);
6376 i += thiswidth;
6377 menu[i] = 0;
6379 else
6381 /* If this element does not fit, end the line now,
6382 and save the element for the next line. */
6383 strcpy (menu + i, "...");
6384 break;
6388 /* Move past this element. */
6389 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
6390 /* Handle reaching end of dense table. */
6391 idx = -1;
6392 if (idx >= 0)
6393 idx++;
6394 else
6395 rest = Fcdr_safe (rest);
6399 /* Prompt with that and read response. */
6400 message2_nolog (menu, strlen (menu),
6401 ! NILP (current_buffer->enable_multibyte_characters));
6403 /* Make believe its not a keyboard macro in case the help char
6404 is pressed. Help characters are not recorded because menu prompting
6405 is not used on replay.
6407 orig_defn_macro = current_kboard->defining_kbd_macro;
6408 current_kboard->defining_kbd_macro = Qnil;
6410 obj = read_char (commandflag, 0, 0, Qnil, 0);
6411 while (BUFFERP (obj));
6412 current_kboard->defining_kbd_macro = orig_defn_macro;
6414 if (!INTEGERP (obj))
6415 return obj;
6416 else
6417 ch = XINT (obj);
6419 if (! EQ (obj, menu_prompt_more_char)
6420 && (!INTEGERP (menu_prompt_more_char)
6421 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
6423 if (!NILP (current_kboard->defining_kbd_macro))
6424 store_kbd_macro_char (obj);
6425 return obj;
6427 /* Help char - go round again */
6431 /* Reading key sequences. */
6433 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
6434 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
6435 keymap, or nil otherwise. Return the index of the first keymap in
6436 which KEY has any binding, or NMAPS if no map has a binding.
6438 If KEY is a meta ASCII character, treat it like meta-prefix-char
6439 followed by the corresponding non-meta character. Keymaps in
6440 CURRENT with non-prefix bindings for meta-prefix-char become nil in
6441 NEXT.
6443 If KEY has no bindings in any of the CURRENT maps, NEXT is left
6444 unmodified.
6446 NEXT may be the same array as CURRENT. */
6448 static int
6449 follow_key (key, nmaps, current, defs, next)
6450 Lisp_Object key;
6451 Lisp_Object *current, *defs, *next;
6452 int nmaps;
6454 int i, first_binding;
6455 int did_meta = 0;
6457 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
6458 followed by the corresponding non-meta character.
6459 Put the results into DEFS, since we are going to alter that anyway.
6460 Do not alter CURRENT or NEXT. */
6461 if (INTEGERP (key) && (XINT (key) & CHAR_META))
6463 for (i = 0; i < nmaps; i++)
6464 if (! NILP (current[i]))
6466 Lisp_Object def;
6467 def = get_keyelt (access_keymap (current[i],
6468 meta_prefix_char, 1, 0), 0);
6470 /* Note that since we pass the resulting bindings through
6471 get_keymap_1, non-prefix bindings for meta-prefix-char
6472 disappear. */
6473 defs[i] = get_keymap_1 (def, 0, 1);
6475 else
6476 defs[i] = Qnil;
6478 did_meta = 1;
6479 XSETINT (key, XFASTINT (key) & ~CHAR_META);
6482 first_binding = nmaps;
6483 for (i = nmaps - 1; i >= 0; i--)
6485 if (! NILP (current[i]))
6487 Lisp_Object map;
6488 if (did_meta)
6489 map = defs[i];
6490 else
6491 map = current[i];
6493 defs[i] = get_keyelt (access_keymap (map, key, 1, 0), 0);
6494 if (! NILP (defs[i]))
6495 first_binding = i;
6497 else
6498 defs[i] = Qnil;
6501 /* Given the set of bindings we've found, produce the next set of maps. */
6502 if (first_binding < nmaps)
6503 for (i = 0; i < nmaps; i++)
6504 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
6506 return first_binding;
6509 /* Read a sequence of keys that ends with a non prefix character,
6510 storing it in KEYBUF, a buffer of size BUFSIZE.
6511 Prompt with PROMPT.
6512 Return the length of the key sequence stored.
6513 Return -1 if the user rejected a command menu.
6515 Echo starting immediately unless `prompt' is 0.
6517 Where a key sequence ends depends on the currently active keymaps.
6518 These include any minor mode keymaps active in the current buffer,
6519 the current buffer's local map, and the global map.
6521 If a key sequence has no other bindings, we check Vfunction_key_map
6522 to see if some trailing subsequence might be the beginning of a
6523 function key's sequence. If so, we try to read the whole function
6524 key, and substitute its symbolic name into the key sequence.
6526 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
6527 `double-' events into similar click events, if that would make them
6528 bound. We try to turn `triple-' events first into `double-' events,
6529 then into clicks.
6531 If we get a mouse click in a mode line, vertical divider, or other
6532 non-text area, we treat the click as if it were prefixed by the
6533 symbol denoting that area - `mode-line', `vertical-line', or
6534 whatever.
6536 If the sequence starts with a mouse click, we read the key sequence
6537 with respect to the buffer clicked on, not the current buffer.
6539 If the user switches frames in the midst of a key sequence, we put
6540 off the switch-frame event until later; the next call to
6541 read_char will return it.
6543 If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
6544 from the selected window's buffer. */
6546 static int
6547 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
6548 can_return_switch_frame, fix_current_buffer)
6549 Lisp_Object *keybuf;
6550 int bufsize;
6551 Lisp_Object prompt;
6552 int dont_downcase_last;
6553 int can_return_switch_frame;
6554 int fix_current_buffer;
6556 int count = specpdl_ptr - specpdl;
6558 /* How many keys there are in the current key sequence. */
6559 int t;
6561 /* The length of the echo buffer when we started reading, and
6562 the length of this_command_keys when we started reading. */
6563 int echo_start;
6564 int keys_start;
6566 /* The number of keymaps we're scanning right now, and the number of
6567 keymaps we have allocated space for. */
6568 int nmaps;
6569 int nmaps_allocated = 0;
6571 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
6572 the current keymaps. */
6573 Lisp_Object *defs;
6575 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
6576 in the current keymaps, or nil where it is not a prefix. */
6577 Lisp_Object *submaps;
6579 /* The local map to start out with at start of key sequence. */
6580 Lisp_Object orig_local_map;
6582 /* 1 if we have already considered switching to the local-map property
6583 of the place where a mouse click occurred. */
6584 int localized_local_map = 0;
6586 /* The index in defs[] of the first keymap that has a binding for
6587 this key sequence. In other words, the lowest i such that
6588 defs[i] is non-nil. */
6589 int first_binding;
6591 /* If t < mock_input, then KEYBUF[t] should be read as the next
6592 input key.
6594 We use this to recover after recognizing a function key. Once we
6595 realize that a suffix of the current key sequence is actually a
6596 function key's escape sequence, we replace the suffix with the
6597 function key's binding from Vfunction_key_map. Now keybuf
6598 contains a new and different key sequence, so the echo area,
6599 this_command_keys, and the submaps and defs arrays are wrong. In
6600 this situation, we set mock_input to t, set t to 0, and jump to
6601 restart_sequence; the loop will read keys from keybuf up until
6602 mock_input, thus rebuilding the state; and then it will resume
6603 reading characters from the keyboard. */
6604 int mock_input = 0;
6606 /* If the sequence is unbound in submaps[], then
6607 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
6608 and fkey_map is its binding.
6610 These might be > t, indicating that all function key scanning
6611 should hold off until t reaches them. We do this when we've just
6612 recognized a function key, to avoid searching for the function
6613 key's again in Vfunction_key_map. */
6614 int fkey_start = 0, fkey_end = 0;
6615 Lisp_Object fkey_map;
6617 /* Likewise, for key_translation_map. */
6618 int keytran_start = 0, keytran_end = 0;
6619 Lisp_Object keytran_map;
6621 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
6622 we put it off for later. While we're reading, we keep the event here. */
6623 Lisp_Object delayed_switch_frame;
6625 /* See the comment below... */
6626 #if defined (GOBBLE_FIRST_EVENT)
6627 Lisp_Object first_event;
6628 #endif
6630 Lisp_Object original_uppercase;
6631 int original_uppercase_position = -1;
6633 /* Gets around Microsoft compiler limitations. */
6634 int dummyflag = 0;
6636 struct buffer *starting_buffer;
6638 /* Nonzero if we seem to have got the beginning of a binding
6639 in function_key_map. */
6640 int function_key_possible = 0;
6641 int key_translation_possible = 0;
6643 /* Save the status of key translation before each step,
6644 so that we can restore this after downcasing. */
6645 Lisp_Object prev_fkey_map;
6646 int prev_fkey_start;
6647 int prev_fkey_end;
6649 Lisp_Object prev_keytran_map;
6650 int prev_keytran_start;
6651 int prev_keytran_end;
6653 int junk;
6655 raw_keybuf_count = 0;
6657 last_nonmenu_event = Qnil;
6659 delayed_switch_frame = Qnil;
6660 fkey_map = Vfunction_key_map;
6661 keytran_map = Vkey_translation_map;
6663 /* If there is no function-key-map, turn off function key scanning. */
6664 if (NILP (Fkeymapp (Vfunction_key_map)))
6665 fkey_start = fkey_end = bufsize + 1;
6667 /* If there is no key-translation-map, turn off scanning. */
6668 if (NILP (Fkeymapp (Vkey_translation_map)))
6669 keytran_start = keytran_end = bufsize + 1;
6671 if (INTERACTIVE)
6673 if (!NILP (prompt))
6674 echo_prompt (XSTRING (prompt)->data);
6675 else if (cursor_in_echo_area && echo_keystrokes)
6676 /* This doesn't put in a dash if the echo buffer is empty, so
6677 you don't always see a dash hanging out in the minibuffer. */
6678 echo_dash ();
6681 /* Record the initial state of the echo area and this_command_keys;
6682 we will need to restore them if we replay a key sequence. */
6683 if (INTERACTIVE)
6684 echo_start = echo_length ();
6685 keys_start = this_command_key_count;
6686 this_single_command_key_start = keys_start;
6688 #if defined (GOBBLE_FIRST_EVENT)
6689 /* This doesn't quite work, because some of the things that read_char
6690 does cannot safely be bypassed. It seems too risky to try to make
6691 this work right. */
6693 /* Read the first char of the sequence specially, before setting
6694 up any keymaps, in case a filter runs and switches buffers on us. */
6695 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
6696 &junk);
6697 #endif /* GOBBLE_FIRST_EVENT */
6699 orig_local_map = get_local_map (PT, current_buffer);
6701 /* Bind input-method-function so that we can set it to nil
6702 temporarily after the first input event. */
6703 specbind (Qinput_method_function, Vinput_method_function);
6705 /* We jump here when the key sequence has been thoroughly changed, and
6706 we need to rescan it starting from the beginning. When we jump here,
6707 keybuf[0..mock_input] holds the sequence we should reread. */
6708 replay_sequence:
6710 starting_buffer = current_buffer;
6711 function_key_possible = 0;
6712 key_translation_possible = 0;
6714 /* Build our list of keymaps.
6715 If we recognize a function key and replace its escape sequence in
6716 keybuf with its symbol, or if the sequence starts with a mouse
6717 click and we need to switch buffers, we jump back here to rebuild
6718 the initial keymaps from the current buffer. */
6720 Lisp_Object *maps;
6722 if (!NILP (current_kboard->Voverriding_terminal_local_map)
6723 || !NILP (Voverriding_local_map))
6725 if (3 > nmaps_allocated)
6727 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
6728 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
6729 nmaps_allocated = 3;
6731 nmaps = 0;
6732 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6733 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6734 if (!NILP (Voverriding_local_map))
6735 submaps[nmaps++] = Voverriding_local_map;
6737 else
6739 nmaps = current_minor_maps (0, &maps);
6740 if (nmaps + 2 > nmaps_allocated)
6742 submaps = (Lisp_Object *) alloca ((nmaps+2) * sizeof (submaps[0]));
6743 defs = (Lisp_Object *) alloca ((nmaps+2) * sizeof (defs[0]));
6744 nmaps_allocated = nmaps + 2;
6746 bcopy (maps, submaps, nmaps * sizeof (submaps[0]));
6747 #ifdef USE_TEXT_PROPERTIES
6748 submaps[nmaps++] = orig_local_map;
6749 #else
6750 submaps[nmaps++] = current_buffer->keymap;
6751 #endif
6753 submaps[nmaps++] = current_global_map;
6756 /* Find an accurate initial value for first_binding. */
6757 for (first_binding = 0; first_binding < nmaps; first_binding++)
6758 if (! NILP (submaps[first_binding]))
6759 break;
6761 /* Start from the beginning in keybuf. */
6762 t = 0;
6764 /* These are no-ops the first time through, but if we restart, they
6765 revert the echo area and this_command_keys to their original state. */
6766 this_command_key_count = keys_start;
6767 if (INTERACTIVE && t < mock_input)
6768 echo_truncate (echo_start);
6770 /* If the best binding for the current key sequence is a keymap, or
6771 we may be looking at a function key's escape sequence, keep on
6772 reading. */
6773 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
6774 || (first_binding >= nmaps
6775 && fkey_start < t
6776 /* mock input is never part of a function key's sequence. */
6777 && mock_input <= fkey_start)
6778 || (first_binding >= nmaps
6779 && keytran_start < t && key_translation_possible)
6780 /* Don't return in the middle of a possible function key sequence,
6781 if the only bindings we found were via case conversion.
6782 Thus, if ESC O a has a function-key-map translation
6783 and ESC o has a binding, don't return after ESC O,
6784 so that we can translate ESC O plus the next character. */
6787 Lisp_Object key;
6788 int used_mouse_menu = 0;
6790 /* Where the last real key started. If we need to throw away a
6791 key that has expanded into more than one element of keybuf
6792 (say, a mouse click on the mode line which is being treated
6793 as [mode-line (mouse-...)], then we backtrack to this point
6794 of keybuf. */
6795 int last_real_key_start;
6797 /* These variables are analogous to echo_start and keys_start;
6798 while those allow us to restart the entire key sequence,
6799 echo_local_start and keys_local_start allow us to throw away
6800 just one key. */
6801 int echo_local_start, keys_local_start, local_first_binding;
6803 if (t >= bufsize)
6804 error ("Key sequence too long");
6806 if (INTERACTIVE)
6807 echo_local_start = echo_length ();
6808 keys_local_start = this_command_key_count;
6809 local_first_binding = first_binding;
6811 replay_key:
6812 /* These are no-ops, unless we throw away a keystroke below and
6813 jumped back up to replay_key; in that case, these restore the
6814 variables to their original state, allowing us to replay the
6815 loop. */
6816 if (INTERACTIVE && t < mock_input)
6817 echo_truncate (echo_local_start);
6818 this_command_key_count = keys_local_start;
6819 first_binding = local_first_binding;
6821 /* By default, assume each event is "real". */
6822 last_real_key_start = t;
6824 /* Does mock_input indicate that we are re-reading a key sequence? */
6825 if (t < mock_input)
6827 key = keybuf[t];
6828 add_command_key (key);
6829 if (echo_keystrokes)
6830 echo_char (key);
6833 /* If not, we should actually read a character. */
6834 else
6836 struct buffer *buf = current_buffer;
6839 #ifdef MULTI_KBOARD
6840 KBOARD *interrupted_kboard = current_kboard;
6841 struct frame *interrupted_frame = selected_frame;
6842 if (setjmp (wrong_kboard_jmpbuf))
6844 if (!NILP (delayed_switch_frame))
6846 interrupted_kboard->kbd_queue
6847 = Fcons (delayed_switch_frame,
6848 interrupted_kboard->kbd_queue);
6849 delayed_switch_frame = Qnil;
6851 while (t > 0)
6852 interrupted_kboard->kbd_queue
6853 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
6855 /* If the side queue is non-empty, ensure it begins with a
6856 switch-frame, so we'll replay it in the right context. */
6857 if (CONSP (interrupted_kboard->kbd_queue)
6858 && (key = XCONS (interrupted_kboard->kbd_queue)->car,
6859 !(EVENT_HAS_PARAMETERS (key)
6860 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
6861 Qswitch_frame))))
6863 Lisp_Object frame;
6864 XSETFRAME (frame, interrupted_frame);
6865 interrupted_kboard->kbd_queue
6866 = Fcons (make_lispy_switch_frame (frame),
6867 interrupted_kboard->kbd_queue);
6869 mock_input = 0;
6870 orig_local_map = get_local_map (PT, current_buffer);
6871 goto replay_sequence;
6873 #endif
6874 key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
6875 &used_mouse_menu);
6877 /* Turn off input methods after a prefix character. */
6878 Vinput_method_function = Qnil;
6881 /* read_char returns t when it shows a menu and the user rejects it.
6882 Just return -1. */
6883 if (EQ (key, Qt))
6885 unbind_to (count, Qnil);
6886 return -1;
6889 /* read_char returns -1 at the end of a macro.
6890 Emacs 18 handles this by returning immediately with a
6891 zero, so that's what we'll do. */
6892 if (INTEGERP (key) && XINT (key) == -1)
6894 t = 0;
6895 /* The Microsoft C compiler can't handle the goto that
6896 would go here. */
6897 dummyflag = 1;
6898 break;
6901 /* If the current buffer has been changed from under us, the
6902 keymap may have changed, so replay the sequence. */
6903 if (BUFFERP (key))
6905 mock_input = t;
6906 /* Reset the current buffer from the selected window
6907 in case something changed the former and not the latter.
6908 This is to be more consistent with the behavior
6909 of the command_loop_1. */
6910 if (fix_current_buffer)
6911 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
6912 Fset_buffer (XWINDOW (selected_window)->buffer);
6914 orig_local_map = get_local_map (PT, current_buffer);
6915 goto replay_sequence;
6918 /* If we have a quit that was typed in another frame, and
6919 quit_throw_to_read_char switched buffers,
6920 replay to get the right keymap. */
6921 if (XINT (key) == quit_char && current_buffer != starting_buffer)
6923 GROW_RAW_KEYBUF;
6924 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
6925 keybuf[t++] = key;
6926 mock_input = t;
6927 Vquit_flag = Qnil;
6928 orig_local_map = get_local_map (PT, current_buffer);
6929 goto replay_sequence;
6932 Vquit_flag = Qnil;
6934 if (EVENT_HAS_PARAMETERS (key)
6935 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), Qswitch_frame))
6937 /* If we're at the beginning of a key sequence, and the caller
6938 says it's okay, go ahead and return this event. If we're
6939 in the midst of a key sequence, delay it until the end. */
6940 if (t > 0 || !can_return_switch_frame)
6942 delayed_switch_frame = key;
6943 goto replay_key;
6947 GROW_RAW_KEYBUF;
6948 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
6951 /* Clicks in non-text areas get prefixed by the symbol
6952 in their CHAR-ADDRESS field. For example, a click on
6953 the mode line is prefixed by the symbol `mode-line'.
6955 Furthermore, key sequences beginning with mouse clicks
6956 are read using the keymaps of the buffer clicked on, not
6957 the current buffer. So we may have to switch the buffer
6958 here.
6960 When we turn one event into two events, we must make sure
6961 that neither of the two looks like the original--so that,
6962 if we replay the events, they won't be expanded again.
6963 If not for this, such reexpansion could happen either here
6964 or when user programs play with this-command-keys. */
6965 if (EVENT_HAS_PARAMETERS (key))
6967 Lisp_Object kind;
6969 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
6970 if (EQ (kind, Qmouse_click))
6972 Lisp_Object window, posn;
6974 window = POSN_WINDOW (EVENT_START (key));
6975 posn = POSN_BUFFER_POSN (EVENT_START (key));
6976 if (CONSP (posn))
6978 /* We're looking at the second event of a
6979 sequence which we expanded before. Set
6980 last_real_key_start appropriately. */
6981 if (t > 0)
6982 last_real_key_start = t - 1;
6985 /* Key sequences beginning with mouse clicks are
6986 read using the keymaps in the buffer clicked on,
6987 not the current buffer. If we're at the
6988 beginning of a key sequence, switch buffers. */
6989 if (last_real_key_start == 0
6990 && WINDOWP (window)
6991 && BUFFERP (XWINDOW (window)->buffer)
6992 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
6994 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
6995 keybuf[t] = key;
6996 mock_input = t + 1;
6998 /* Arrange to go back to the original buffer once we're
6999 done reading the key sequence. Note that we can't
7000 use save_excursion_{save,restore} here, because they
7001 save point as well as the current buffer; we don't
7002 want to save point, because redisplay may change it,
7003 to accommodate a Fset_window_start or something. We
7004 don't want to do this at the top of the function,
7005 because we may get input from a subprocess which
7006 wants to change the selected window and stuff (say,
7007 emacsclient). */
7008 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
7010 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
7011 orig_local_map = get_local_map (PT, current_buffer);
7012 goto replay_sequence;
7014 /* For a mouse click, get the local text-property keymap
7015 of the place clicked on, rather than point. */
7016 if (last_real_key_start == 0 && CONSP (XCONS (key)->cdr)
7017 && ! localized_local_map)
7019 Lisp_Object map_here, start, pos;
7021 localized_local_map = 1;
7022 start = EVENT_START (key);
7023 if (CONSP (start) && CONSP (XCONS (start)->cdr))
7025 pos = POSN_BUFFER_POSN (start);
7026 if (INTEGERP (pos)
7027 && XINT (pos) >= BEG && XINT (pos) <= Z)
7029 map_here = get_local_map (XINT (pos), current_buffer);
7030 if (!EQ (map_here, orig_local_map))
7032 orig_local_map = map_here;
7033 keybuf[t] = key;
7034 mock_input = t + 1;
7036 goto replay_sequence;
7042 /* Expand mode-line and scroll-bar events into two events:
7043 use posn as a fake prefix key. */
7044 if (SYMBOLP (posn))
7046 if (t + 1 >= bufsize)
7047 error ("Key sequence too long");
7048 keybuf[t] = posn;
7049 keybuf[t+1] = key;
7050 mock_input = t + 2;
7052 /* Zap the position in key, so we know that we've
7053 expanded it, and don't try to do so again. */
7054 POSN_BUFFER_POSN (EVENT_START (key))
7055 = Fcons (posn, Qnil);
7056 goto replay_key;
7059 else if (CONSP (XCONS (key)->cdr)
7060 && CONSP (EVENT_START (key))
7061 && CONSP (XCONS (EVENT_START (key))->cdr))
7063 Lisp_Object posn;
7065 posn = POSN_BUFFER_POSN (EVENT_START (key));
7066 /* Handle menu-bar events:
7067 insert the dummy prefix event `menu-bar'. */
7068 if (EQ (posn, Qmenu_bar))
7070 if (t + 1 >= bufsize)
7071 error ("Key sequence too long");
7072 keybuf[t] = posn;
7073 keybuf[t+1] = key;
7075 /* Zap the position in key, so we know that we've
7076 expanded it, and don't try to do so again. */
7077 POSN_BUFFER_POSN (EVENT_START (key))
7078 = Fcons (posn, Qnil);
7080 mock_input = t + 2;
7081 goto replay_sequence;
7083 else if (CONSP (posn))
7085 /* We're looking at the second event of a
7086 sequence which we expanded before. Set
7087 last_real_key_start appropriately. */
7088 if (last_real_key_start == t && t > 0)
7089 last_real_key_start = t - 1;
7094 /* We have finally decided that KEY is something we might want
7095 to look up. */
7096 first_binding = (follow_key (key,
7097 nmaps - first_binding,
7098 submaps + first_binding,
7099 defs + first_binding,
7100 submaps + first_binding)
7101 + first_binding);
7103 /* If KEY wasn't bound, we'll try some fallbacks. */
7104 if (first_binding >= nmaps)
7106 Lisp_Object head;
7108 head = EVENT_HEAD (key);
7109 if (help_char_p (head) && t > 0)
7111 read_key_sequence_cmd = Vprefix_help_command;
7112 keybuf[t++] = key;
7113 last_nonmenu_event = key;
7114 /* The Microsoft C compiler can't handle the goto that
7115 would go here. */
7116 dummyflag = 1;
7117 break;
7120 if (SYMBOLP (head))
7122 Lisp_Object breakdown;
7123 int modifiers;
7125 breakdown = parse_modifiers (head);
7126 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
7127 /* Attempt to reduce an unbound mouse event to a simpler
7128 event that is bound:
7129 Drags reduce to clicks.
7130 Double-clicks reduce to clicks.
7131 Triple-clicks reduce to double-clicks, then to clicks.
7132 Down-clicks are eliminated.
7133 Double-downs reduce to downs, then are eliminated.
7134 Triple-downs reduce to double-downs, then to downs,
7135 then are eliminated. */
7136 if (modifiers & (down_modifier | drag_modifier
7137 | double_modifier | triple_modifier))
7139 while (modifiers & (down_modifier | drag_modifier
7140 | double_modifier | triple_modifier))
7142 Lisp_Object new_head, new_click;
7143 if (modifiers & triple_modifier)
7144 modifiers ^= (double_modifier | triple_modifier);
7145 else if (modifiers & double_modifier)
7146 modifiers &= ~double_modifier;
7147 else if (modifiers & drag_modifier)
7148 modifiers &= ~drag_modifier;
7149 else
7151 /* Dispose of this `down' event by simply jumping
7152 back to replay_key, to get another event.
7154 Note that if this event came from mock input,
7155 then just jumping back to replay_key will just
7156 hand it to us again. So we have to wipe out any
7157 mock input.
7159 We could delete keybuf[t] and shift everything
7160 after that to the left by one spot, but we'd also
7161 have to fix up any variable that points into
7162 keybuf, and shifting isn't really necessary
7163 anyway.
7165 Adding prefixes for non-textual mouse clicks
7166 creates two characters of mock input, and both
7167 must be thrown away. If we're only looking at
7168 the prefix now, we can just jump back to
7169 replay_key. On the other hand, if we've already
7170 processed the prefix, and now the actual click
7171 itself is giving us trouble, then we've lost the
7172 state of the keymaps we want to backtrack to, and
7173 we need to replay the whole sequence to rebuild
7176 Beyond that, only function key expansion could
7177 create more than two keys, but that should never
7178 generate mouse events, so it's okay to zero
7179 mock_input in that case too.
7181 Isn't this just the most wonderful code ever? */
7182 if (t == last_real_key_start)
7184 mock_input = 0;
7185 goto replay_key;
7187 else
7189 mock_input = last_real_key_start;
7190 goto replay_sequence;
7194 new_head
7195 = apply_modifiers (modifiers, XCONS (breakdown)->car);
7196 new_click
7197 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
7199 /* Look for a binding for this new key. follow_key
7200 promises that it didn't munge submaps the
7201 last time we called it, since key was unbound. */
7202 first_binding
7203 = (follow_key (new_click,
7204 nmaps - local_first_binding,
7205 submaps + local_first_binding,
7206 defs + local_first_binding,
7207 submaps + local_first_binding)
7208 + local_first_binding);
7210 /* If that click is bound, go for it. */
7211 if (first_binding < nmaps)
7213 key = new_click;
7214 break;
7216 /* Otherwise, we'll leave key set to the drag event. */
7222 keybuf[t++] = key;
7223 /* Normally, last_nonmenu_event gets the previous key we read.
7224 But when a mouse popup menu is being used,
7225 we don't update last_nonmenu_event; it continues to hold the mouse
7226 event that preceded the first level of menu. */
7227 if (!used_mouse_menu)
7228 last_nonmenu_event = key;
7230 /* Record what part of this_command_keys is the current key sequence. */
7231 this_single_command_key_start = this_command_key_count - t;
7233 prev_fkey_map = fkey_map;
7234 prev_fkey_start = fkey_start;
7235 prev_fkey_end = fkey_end;
7237 prev_keytran_map = keytran_map;
7238 prev_keytran_start = keytran_start;
7239 prev_keytran_end = keytran_end;
7241 /* If the sequence is unbound, see if we can hang a function key
7242 off the end of it. We only want to scan real keyboard input
7243 for function key sequences, so if mock_input says that we're
7244 re-reading old events, don't examine it. */
7245 if (first_binding >= nmaps
7246 && t >= mock_input)
7248 Lisp_Object fkey_next;
7250 /* Continue scan from fkey_end until we find a bound suffix.
7251 If we fail, increment fkey_start
7252 and start fkey_end from there. */
7253 while (fkey_end < t)
7255 Lisp_Object key;
7257 key = keybuf[fkey_end++];
7258 /* Look up meta-characters by prefixing them
7259 with meta_prefix_char. I hate this. */
7260 if (INTEGERP (key) && XINT (key) & meta_modifier)
7262 fkey_next
7263 = get_keymap_1
7264 (get_keyelt
7265 (access_keymap (fkey_map, meta_prefix_char, 1, 0), 0),
7266 0, 1);
7267 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
7269 else
7270 fkey_next = fkey_map;
7272 fkey_next
7273 = get_keyelt (access_keymap (fkey_next, key, 1, 0), 0);
7275 #if 0 /* I didn't turn this on, because it might cause trouble
7276 for the mapping of return into C-m and tab into C-i. */
7277 /* Optionally don't map function keys into other things.
7278 This enables the user to redefine kp- keys easily. */
7279 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
7280 fkey_next = Qnil;
7281 #endif
7283 /* If the function key map gives a function, not an
7284 array, then call the function with no args and use
7285 its value instead. */
7286 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
7287 && fkey_end == t)
7289 struct gcpro gcpro1, gcpro2, gcpro3;
7290 Lisp_Object tem;
7291 tem = fkey_next;
7293 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
7294 fkey_next = call1 (fkey_next, prompt);
7295 UNGCPRO;
7296 /* If the function returned something invalid,
7297 barf--don't ignore it.
7298 (To ignore it safely, we would need to gcpro a bunch of
7299 other variables.) */
7300 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
7301 error ("Function in key-translation-map returns invalid key sequence");
7304 function_key_possible = ! NILP (fkey_next);
7306 /* If keybuf[fkey_start..fkey_end] is bound in the
7307 function key map and it's a suffix of the current
7308 sequence (i.e. fkey_end == t), replace it with
7309 the binding and restart with fkey_start at the end. */
7310 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
7311 && fkey_end == t)
7313 int len = XFASTINT (Flength (fkey_next));
7315 t = fkey_start + len;
7316 if (t >= bufsize)
7317 error ("Key sequence too long");
7319 if (VECTORP (fkey_next))
7320 bcopy (XVECTOR (fkey_next)->contents,
7321 keybuf + fkey_start,
7322 (t - fkey_start) * sizeof (keybuf[0]));
7323 else if (STRINGP (fkey_next))
7325 int i;
7327 for (i = 0; i < len; i++)
7328 XSETFASTINT (keybuf[fkey_start + i],
7329 XSTRING (fkey_next)->data[i]);
7332 mock_input = t;
7333 fkey_start = fkey_end = t;
7334 fkey_map = Vfunction_key_map;
7336 /* Do pass the results through key-translation-map. */
7337 keytran_start = keytran_end = 0;
7338 keytran_map = Vkey_translation_map;
7340 goto replay_sequence;
7343 fkey_map = get_keymap_1 (fkey_next, 0, 1);
7345 /* If we no longer have a bound suffix, try a new positions for
7346 fkey_start. */
7347 if (NILP (fkey_map))
7349 fkey_end = ++fkey_start;
7350 fkey_map = Vfunction_key_map;
7351 function_key_possible = 0;
7356 /* Look for this sequence in key-translation-map. */
7358 Lisp_Object keytran_next;
7360 /* Scan from keytran_end until we find a bound suffix. */
7361 while (keytran_end < t)
7363 Lisp_Object key;
7365 key = keybuf[keytran_end++];
7366 /* Look up meta-characters by prefixing them
7367 with meta_prefix_char. I hate this. */
7368 if (INTEGERP (key) && XINT (key) & meta_modifier)
7370 keytran_next
7371 = get_keymap_1
7372 (get_keyelt
7373 (access_keymap (keytran_map, meta_prefix_char, 1, 0), 0),
7374 0, 1);
7375 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
7377 else
7378 keytran_next = keytran_map;
7380 keytran_next
7381 = get_keyelt (access_keymap (keytran_next, key, 1, 0), 0);
7383 /* If the key translation map gives a function, not an
7384 array, then call the function with no args and use
7385 its value instead. */
7386 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
7387 && keytran_end == t)
7389 struct gcpro gcpro1, gcpro2, gcpro3;
7390 Lisp_Object tem;
7391 tem = keytran_next;
7393 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
7394 keytran_next = call1 (keytran_next, prompt);
7395 UNGCPRO;
7396 /* If the function returned something invalid,
7397 barf--don't ignore it.
7398 (To ignore it safely, we would need to gcpro a bunch of
7399 other variables.) */
7400 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
7401 error ("Function in key-translation-map returns invalid key sequence");
7404 key_translation_possible = ! NILP (keytran_next);
7406 /* If keybuf[keytran_start..keytran_end] is bound in the
7407 key translation map and it's a suffix of the current
7408 sequence (i.e. keytran_end == t), replace it with
7409 the binding and restart with keytran_start at the end. */
7410 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
7411 && keytran_end == t)
7413 int len = XFASTINT (Flength (keytran_next));
7415 t = keytran_start + len;
7416 if (t >= bufsize)
7417 error ("Key sequence too long");
7419 if (VECTORP (keytran_next))
7420 bcopy (XVECTOR (keytran_next)->contents,
7421 keybuf + keytran_start,
7422 (t - keytran_start) * sizeof (keybuf[0]));
7423 else if (STRINGP (keytran_next))
7425 int i;
7427 for (i = 0; i < len; i++)
7428 XSETFASTINT (keybuf[keytran_start + i],
7429 XSTRING (keytran_next)->data[i]);
7432 mock_input = t;
7433 keytran_start = keytran_end = t;
7434 keytran_map = Vkey_translation_map;
7436 /* Don't pass the results of key-translation-map
7437 through function-key-map. */
7438 fkey_start = fkey_end = t;
7439 fkey_map = Vkey_translation_map;
7441 goto replay_sequence;
7444 keytran_map = get_keymap_1 (keytran_next, 0, 1);
7446 /* If we no longer have a bound suffix, try a new positions for
7447 keytran_start. */
7448 if (NILP (keytran_map))
7450 keytran_end = ++keytran_start;
7451 keytran_map = Vkey_translation_map;
7452 key_translation_possible = 0;
7457 /* If KEY is not defined in any of the keymaps,
7458 and cannot be part of a function key or translation,
7459 and is an upper case letter
7460 use the corresponding lower-case letter instead. */
7461 if (first_binding == nmaps && ! function_key_possible
7462 && ! key_translation_possible
7463 && INTEGERP (key)
7464 && ((((XINT (key) & 0x3ffff)
7465 < XCHAR_TABLE (current_buffer->downcase_table)->size)
7466 && UPPERCASEP (XINT (key) & 0x3ffff))
7467 || (XINT (key) & shift_modifier)))
7469 Lisp_Object new_key;
7471 original_uppercase = key;
7472 original_uppercase_position = t - 1;
7474 if (XINT (key) & shift_modifier)
7475 XSETINT (new_key, XINT (key) & ~shift_modifier);
7476 else
7477 XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
7478 | (XINT (key) & ~0x3ffff)));
7480 /* We have to do this unconditionally, regardless of whether
7481 the lower-case char is defined in the keymaps, because they
7482 might get translated through function-key-map. */
7483 keybuf[t - 1] = new_key;
7484 mock_input = t;
7486 fkey_map = prev_fkey_map;
7487 fkey_start = prev_fkey_start;
7488 fkey_end = prev_fkey_end;
7490 keytran_map = prev_keytran_map;
7491 keytran_start = prev_keytran_start;
7492 keytran_end = prev_keytran_end;
7494 goto replay_sequence;
7496 /* If KEY is not defined in any of the keymaps,
7497 and cannot be part of a function key or translation,
7498 and is a shifted function key,
7499 use the corresponding unshifted function key instead. */
7500 if (first_binding == nmaps && ! function_key_possible
7501 && ! key_translation_possible
7502 && SYMBOLP (key))
7504 Lisp_Object breakdown;
7505 int modifiers;
7507 breakdown = parse_modifiers (key);
7508 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
7509 if (modifiers & shift_modifier)
7511 Lisp_Object new_key;
7513 original_uppercase = key;
7514 original_uppercase_position = t - 1;
7516 modifiers &= ~shift_modifier;
7517 new_key = apply_modifiers (modifiers,
7518 XCONS (breakdown)->car);
7520 keybuf[t - 1] = new_key;
7521 mock_input = t;
7523 fkey_map = prev_fkey_map;
7524 fkey_start = prev_fkey_start;
7525 fkey_end = prev_fkey_end;
7527 keytran_map = prev_keytran_map;
7528 keytran_start = prev_keytran_start;
7529 keytran_end = prev_keytran_end;
7531 goto replay_sequence;
7536 if (!dummyflag)
7537 read_key_sequence_cmd = (first_binding < nmaps
7538 ? defs[first_binding]
7539 : Qnil);
7541 unread_switch_frame = delayed_switch_frame;
7542 unbind_to (count, Qnil);
7544 /* Don't downcase the last character if the caller says don't.
7545 Don't downcase it if the result is undefined, either. */
7546 if ((dont_downcase_last || first_binding >= nmaps)
7547 && t - 1 == original_uppercase_position)
7548 keybuf[t - 1] = original_uppercase;
7550 /* Occasionally we fabricate events, perhaps by expanding something
7551 according to function-key-map, or by adding a prefix symbol to a
7552 mouse click in the scroll bar or modeline. In this cases, return
7553 the entire generated key sequence, even if we hit an unbound
7554 prefix or a definition before the end. This means that you will
7555 be able to push back the event properly, and also means that
7556 read-key-sequence will always return a logical unit.
7558 Better ideas? */
7559 for (; t < mock_input; t++)
7561 if (echo_keystrokes)
7562 echo_char (keybuf[t]);
7563 add_command_key (keybuf[t]);
7568 return t;
7571 #if 0 /* This doc string is too long for some compilers.
7572 This commented-out definition serves for DOC. */
7573 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
7574 "Read a sequence of keystrokes and return as a string or vector.\n\
7575 The sequence is sufficient to specify a non-prefix command in the\n\
7576 current local and global maps.\n\
7578 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
7579 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
7580 as a continuation of the previous key.\n\
7582 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
7583 convert the last event to lower case. (Normally any upper case event\n\
7584 is converted to lower case if the original event is undefined and the lower\n\
7585 case equivalent is defined.) A non-nil value is appropriate for reading\n\
7586 a key sequence to be defined.\n\
7588 A C-g typed while in this function is treated like any other character,\n\
7589 and `quit-flag' is not set.\n\
7591 If the key sequence starts with a mouse click, then the sequence is read\n\
7592 using the keymaps of the buffer of the window clicked in, not the buffer\n\
7593 of the selected window as normal.\n\
7594 ""\n\
7595 `read-key-sequence' drops unbound button-down events, since you normally\n\
7596 only care about the click or drag events which follow them. If a drag\n\
7597 or multi-click event is unbound, but the corresponding click event would\n\
7598 be bound, `read-key-sequence' turns the event into a click event at the\n\
7599 drag's starting position. This means that you don't have to distinguish\n\
7600 between click and drag, double, or triple events unless you want to.\n\
7602 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
7603 lines separating windows, and scroll bars with imaginary keys\n\
7604 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
7606 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
7607 function will process a switch-frame event if the user switches frames\n\
7608 before typing anything. If the user switches frames in the middle of a\n\
7609 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
7610 is nil, then the event will be put off until after the current key sequence.\n\
7612 `read-key-sequence' checks `function-key-map' for function key\n\
7613 sequences, where they wouldn't conflict with ordinary bindings. See\n\
7614 `function-key-map' for more details.")
7615 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
7616 #endif
7618 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
7620 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
7621 Lisp_Object prompt, continue_echo, dont_downcase_last;
7622 Lisp_Object can_return_switch_frame;
7624 Lisp_Object keybuf[30];
7625 register int i;
7626 struct gcpro gcpro1, gcpro2;
7628 if (!NILP (prompt))
7629 CHECK_STRING (prompt, 0);
7630 QUIT;
7632 bzero (keybuf, sizeof keybuf);
7633 GCPRO1 (keybuf[0]);
7634 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
7636 if (NILP (continue_echo))
7638 this_command_key_count = 0;
7639 this_single_command_key_start = 0;
7642 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
7643 prompt, ! NILP (dont_downcase_last),
7644 ! NILP (can_return_switch_frame), 0);
7646 if (i == -1)
7648 Vquit_flag = Qt;
7649 QUIT;
7651 UNGCPRO;
7652 return make_event_array (i, keybuf);
7655 DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
7656 Sread_key_sequence_vector, 1, 4, 0,
7657 "Like `read-key-sequence' but always return a vector.")
7658 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
7659 Lisp_Object prompt, continue_echo, dont_downcase_last;
7660 Lisp_Object can_return_switch_frame;
7662 Lisp_Object keybuf[30];
7663 register int i;
7664 struct gcpro gcpro1, gcpro2;
7666 if (!NILP (prompt))
7667 CHECK_STRING (prompt, 0);
7668 QUIT;
7670 bzero (keybuf, sizeof keybuf);
7671 GCPRO1 (keybuf[0]);
7672 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
7674 if (NILP (continue_echo))
7676 this_command_key_count = 0;
7677 this_single_command_key_start = 0;
7680 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
7681 prompt, ! NILP (dont_downcase_last),
7682 ! NILP (can_return_switch_frame), 0);
7684 if (i == -1)
7686 Vquit_flag = Qt;
7687 QUIT;
7689 UNGCPRO;
7690 return Fvector (i, keybuf);
7693 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
7694 "Execute CMD as an editor command.\n\
7695 CMD must be a symbol that satisfies the `commandp' predicate.\n\
7696 Optional second arg RECORD-FLAG non-nil\n\
7697 means unconditionally put this command in `command-history'.\n\
7698 Otherwise, that is done only if an arg is read using the minibuffer.\n\
7699 The argument KEYS specifies the value to use instead of (this-command-keys)\n\
7700 when reading the arguments; if it is nil, (this-command-keys) is used.\n\
7701 The argument SPECIAL, if non-nil, means that this command is executing\n\
7702 a special event, so ignore the prefix argument and don't clear it.")
7703 (cmd, record_flag, keys, special)
7704 Lisp_Object cmd, record_flag, keys, special;
7706 register Lisp_Object final;
7707 register Lisp_Object tem;
7708 Lisp_Object prefixarg;
7709 struct backtrace backtrace;
7710 extern int debug_on_next_call;
7712 debug_on_next_call = 0;
7714 if (NILP (special))
7716 prefixarg = current_kboard->Vprefix_arg;
7717 Vcurrent_prefix_arg = prefixarg;
7718 current_kboard->Vprefix_arg = Qnil;
7720 else
7721 prefixarg = Qnil;
7723 if (SYMBOLP (cmd))
7725 tem = Fget (cmd, Qdisabled);
7726 if (!NILP (tem) && !NILP (Vrun_hooks))
7728 tem = Fsymbol_value (Qdisabled_command_hook);
7729 if (!NILP (tem))
7730 return call1 (Vrun_hooks, Qdisabled_command_hook);
7734 while (1)
7736 final = Findirect_function (cmd);
7738 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
7740 struct gcpro gcpro1, gcpro2;
7742 GCPRO2 (cmd, prefixarg);
7743 do_autoload (final, cmd);
7744 UNGCPRO;
7746 else
7747 break;
7750 if (STRINGP (final) || VECTORP (final))
7752 /* If requested, place the macro in the command history. For
7753 other sorts of commands, call-interactively takes care of
7754 this. */
7755 if (!NILP (record_flag))
7757 Vcommand_history
7758 = Fcons (Fcons (Qexecute_kbd_macro,
7759 Fcons (final, Fcons (prefixarg, Qnil))),
7760 Vcommand_history);
7762 /* Don't keep command history around forever. */
7763 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
7765 tem = Fnthcdr (Vhistory_length, Vcommand_history);
7766 if (CONSP (tem))
7767 XCONS (tem)->cdr = Qnil;
7771 return Fexecute_kbd_macro (final, prefixarg);
7774 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
7776 backtrace.next = backtrace_list;
7777 backtrace_list = &backtrace;
7778 backtrace.function = &Qcall_interactively;
7779 backtrace.args = &cmd;
7780 backtrace.nargs = 1;
7781 backtrace.evalargs = 0;
7783 tem = Fcall_interactively (cmd, record_flag, keys);
7785 backtrace_list = backtrace.next;
7786 return tem;
7788 return Qnil;
7791 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
7792 1, 1, "P",
7793 "Read function name, then read its arguments and call it.")
7794 (prefixarg)
7795 Lisp_Object prefixarg;
7797 Lisp_Object function;
7798 char buf[40];
7799 Lisp_Object saved_keys;
7800 Lisp_Object bindings, value;
7801 struct gcpro gcpro1, gcpro2;
7803 saved_keys = Fvector (this_command_key_count,
7804 XVECTOR (this_command_keys)->contents);
7805 buf[0] = 0;
7806 GCPRO2 (saved_keys, prefixarg);
7808 if (EQ (prefixarg, Qminus))
7809 strcpy (buf, "- ");
7810 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
7811 strcpy (buf, "C-u ");
7812 else if (CONSP (prefixarg) && INTEGERP (XCONS (prefixarg)->car))
7814 if (sizeof (int) == sizeof (EMACS_INT))
7815 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
7816 else if (sizeof (long) == sizeof (EMACS_INT))
7817 sprintf (buf, "%ld ", XINT (XCONS (prefixarg)->car));
7818 else
7819 abort ();
7821 else if (INTEGERP (prefixarg))
7823 if (sizeof (int) == sizeof (EMACS_INT))
7824 sprintf (buf, "%d ", XINT (prefixarg));
7825 else if (sizeof (long) == sizeof (EMACS_INT))
7826 sprintf (buf, "%ld ", XINT (prefixarg));
7827 else
7828 abort ();
7831 /* This isn't strictly correct if execute-extended-command
7832 is bound to anything else. Perhaps it should use
7833 this_command_keys? */
7834 strcat (buf, "M-x ");
7836 /* Prompt with buf, and then read a string, completing from and
7837 restricting to the set of all defined commands. Don't provide
7838 any initial input. Save the command read on the extended-command
7839 history list. */
7840 function = Fcompleting_read (build_string (buf),
7841 Vobarray, Qcommandp,
7842 Qt, Qnil, Qextended_command_history, Qnil,
7843 Qnil);
7845 if (STRINGP (function) && XSTRING (function)->size == 0)
7846 error ("No command name given");
7848 /* Set this_command_keys to the concatenation of saved_keys and
7849 function, followed by a RET. */
7851 struct Lisp_String *str;
7852 Lisp_Object *keys;
7853 int i;
7855 this_command_key_count = 0;
7856 this_single_command_key_start = 0;
7858 keys = XVECTOR (saved_keys)->contents;
7859 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
7860 add_command_key (keys[i]);
7862 str = XSTRING (function);
7863 for (i = 0; i < str->size; i++)
7864 add_command_key (Faref (function, make_number (i)));
7866 add_command_key (make_number ('\015'));
7869 UNGCPRO;
7871 function = Fintern (function, Qnil);
7872 current_kboard->Vprefix_arg = prefixarg;
7873 this_command = function;
7875 /* If enabled, show which key runs this command. */
7876 if (!NILP (Vsuggest_key_bindings)
7877 && NILP (Vexecuting_macro)
7878 && SYMBOLP (function))
7879 bindings = Fwhere_is_internal (function, Voverriding_local_map,
7880 Qt, Qnil);
7881 else
7882 bindings = Qnil;
7884 value = Qnil;
7885 GCPRO2 (bindings, value);
7886 value = Fcommand_execute (function, Qt, Qnil, Qnil);
7888 /* If the command has a key binding, print it now. */
7889 if (!NILP (bindings)
7890 && ! (VECTORP (bindings) && EQ (Faref (bindings, make_number (0)),
7891 Qmouse_movement)))
7893 /* But first wait, and skip the message if there is input. */
7894 int delay_time;
7895 if (echo_area_glyphs != 0)
7896 /* This command displayed something in the echo area;
7897 so wait a few seconds, then display our suggestion message. */
7898 delay_time = (NUMBERP (Vsuggest_key_bindings)
7899 ? XINT (Vsuggest_key_bindings) : 2);
7900 else
7901 /* This command left the echo area empty,
7902 so display our message immediately. */
7903 delay_time = 0;
7905 if (!NILP (Fsit_for (make_number (delay_time), Qnil, Qnil))
7906 && ! CONSP (Vunread_command_events))
7908 Lisp_Object binding;
7909 char *newmessage;
7910 char *oldmessage = echo_area_glyphs;
7911 int oldmessage_len = echo_area_glyphs_length;
7912 int oldmultibyte = message_enable_multibyte;
7914 binding = Fkey_description (bindings);
7916 newmessage
7917 = (char *) alloca (XSYMBOL (function)->name->size
7918 + STRING_BYTES (XSTRING (binding))
7919 + 100);
7920 sprintf (newmessage, "You can run the command `%s' with %s",
7921 XSYMBOL (function)->name->data,
7922 XSTRING (binding)->data);
7923 message2_nolog (newmessage,
7924 strlen (newmessage),
7925 STRING_MULTIBYTE (binding));
7926 if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
7927 ? Vsuggest_key_bindings : make_number (2)),
7928 Qnil, Qnil)))
7929 message2_nolog (oldmessage, oldmessage_len, oldmultibyte);
7933 RETURN_UNGCPRO (value);
7936 /* Find the set of keymaps now active.
7937 Store into *MAPS_P a vector holding the various maps
7938 and return the number of them. The vector was malloc'd
7939 and the caller should free it. */
7942 current_active_maps (maps_p)
7943 Lisp_Object **maps_p;
7945 Lisp_Object *tmaps, *maps;
7946 int nmaps;
7948 /* Should overriding-terminal-local-map and overriding-local-map apply? */
7949 if (!NILP (Voverriding_local_map_menu_flag))
7951 /* Yes, use them (if non-nil) as well as the global map. */
7952 maps = (Lisp_Object *) xmalloc (3 * sizeof (maps[0]));
7953 nmaps = 0;
7954 if (!NILP (current_kboard->Voverriding_terminal_local_map))
7955 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
7956 if (!NILP (Voverriding_local_map))
7957 maps[nmaps++] = Voverriding_local_map;
7959 else
7961 /* No, so use major and minor mode keymaps. */
7962 nmaps = current_minor_maps (NULL, &tmaps);
7963 maps = (Lisp_Object *) xmalloc ((nmaps + 2) * sizeof (maps[0]));
7964 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
7965 #ifdef USE_TEXT_PROPERTIES
7966 maps[nmaps++] = get_local_map (PT, current_buffer);
7967 #else
7968 maps[nmaps++] = current_buffer->keymap;
7969 #endif
7971 maps[nmaps++] = current_global_map;
7973 *maps_p = maps;
7974 return nmaps;
7977 /* Return nonzero if input events are pending. */
7980 detect_input_pending ()
7982 if (!input_pending)
7983 get_input_pending (&input_pending, 0);
7985 return input_pending;
7988 /* Return nonzero if input events are pending, and run any pending timers. */
7991 detect_input_pending_run_timers (do_display)
7992 int do_display;
7994 int old_timers_run = timers_run;
7996 if (!input_pending)
7997 get_input_pending (&input_pending, 1);
7999 if (old_timers_run != timers_run && do_display)
8000 redisplay_preserve_echo_area ();
8002 return input_pending;
8005 /* This is called in some cases before a possible quit.
8006 It cases the next call to detect_input_pending to recompute input_pending.
8007 So calling this function unnecessarily can't do any harm. */
8009 void
8010 clear_input_pending ()
8012 input_pending = 0;
8015 /* Return nonzero if there are pending requeued events.
8016 This isn't used yet. The hope is to make wait_reading_process_input
8017 call it, and return return if it runs Lisp code that unreads something.
8018 The problem is, kbd_buffer_get_event needs to be fixed to know what
8019 to do in that case. It isn't trivial. */
8022 requeued_events_pending_p ()
8024 return (!NILP (Vunread_command_events) || unread_command_char != -1);
8028 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
8029 "T if command input is currently available with no waiting.\n\
8030 Actually, the value is nil only if we can be sure that no input is available.")
8033 if (!NILP (Vunread_command_events) || unread_command_char != -1)
8034 return (Qt);
8036 get_input_pending (&input_pending, 1);
8037 return input_pending > 0 ? Qt : Qnil;
8040 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
8041 "Return vector of last 100 events, not counting those from keyboard macros.")
8044 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
8045 Lisp_Object val;
8047 if (total_keys < NUM_RECENT_KEYS)
8048 return Fvector (total_keys, keys);
8049 else
8051 val = Fvector (NUM_RECENT_KEYS, keys);
8052 bcopy (keys + recent_keys_index,
8053 XVECTOR (val)->contents,
8054 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
8055 bcopy (keys,
8056 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
8057 recent_keys_index * sizeof (Lisp_Object));
8058 return val;
8062 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
8063 "Return the key sequence that invoked this command.\n\
8064 The value is a string or a vector.")
8067 return make_event_array (this_command_key_count,
8068 XVECTOR (this_command_keys)->contents);
8071 DEFUN ("this-command-keys-vector", Fthis_command_keys_vector, Sthis_command_keys_vector, 0, 0, 0,
8072 "Return the key sequence that invoked this command, as a vector.")
8075 return Fvector (this_command_key_count,
8076 XVECTOR (this_command_keys)->contents);
8079 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
8080 Sthis_single_command_keys, 0, 0, 0,
8081 "Return the key sequence that invoked this command.\n\
8082 Unlike `this-command-keys', this function's value\n\
8083 does not include prefix arguments.\n\
8084 The value is always a vector.")
8087 return Fvector (this_command_key_count
8088 - this_single_command_key_start,
8089 (XVECTOR (this_command_keys)->contents
8090 + this_single_command_key_start));
8093 DEFUN ("this-single-command-raw-keys", Fthis_single_command_raw_keys,
8094 Sthis_single_command_raw_keys, 0, 0, 0,
8095 "Return the raw events that were read for this command.\n\
8096 Unlike `this-single-command-keys', this function's value\n\
8097 shows the events before all translations (except for input methods).\n\
8098 The value is always a vector.")
8101 return Fvector (raw_keybuf_count,
8102 (XVECTOR (raw_keybuf)->contents));
8105 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
8106 Sreset_this_command_lengths, 0, 0, 0,
8107 "Used for complicated reasons in `universal-argument-other-key'.\n\
8109 `universal-argument-other-key' rereads the event just typed.\n\
8110 It then gets translated through `function-key-map'.\n\
8111 The translated event gets included in the echo area and in\n\
8112 the value of `this-command-keys' in addition to the raw original event.\n\
8113 That is not right.\n\
8115 Calling this function directs the translated event to replace\n\
8116 the original event, so that only one version of the event actually\n\
8117 appears in the echo area and in the value of `this-command-keys.'.")
8120 before_command_restore_flag = 1;
8121 before_command_key_count_1 = before_command_key_count;
8122 before_command_echo_length_1 = before_command_echo_length;
8125 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
8126 "Return the current depth in recursive edits.")
8129 Lisp_Object temp;
8130 XSETFASTINT (temp, command_loop_level + minibuf_level);
8131 return temp;
8134 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
8135 "FOpen dribble file: ",
8136 "Start writing all keyboard characters to a dribble file called FILE.\n\
8137 If FILE is nil, close any open dribble file.")
8138 (file)
8139 Lisp_Object file;
8141 if (dribble)
8143 fclose (dribble);
8144 dribble = 0;
8146 if (!NILP (file))
8148 file = Fexpand_file_name (file, Qnil);
8149 dribble = fopen (XSTRING (file)->data, "w");
8150 if (dribble == 0)
8151 report_file_error ("Opening dribble", Fcons (file, Qnil));
8153 return Qnil;
8156 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
8157 "Discard the contents of the terminal input buffer.\n\
8158 Also cancel any kbd macro being defined.")
8161 current_kboard->defining_kbd_macro = Qnil;
8162 update_mode_lines++;
8164 Vunread_command_events = Qnil;
8165 unread_command_char = -1;
8167 discard_tty_input ();
8169 /* Without the cast, GCC complains that this assignment loses the
8170 volatile qualifier of kbd_store_ptr. Is there anything wrong
8171 with that? */
8172 kbd_fetch_ptr = (struct input_event *) kbd_store_ptr;
8173 Ffillarray (kbd_buffer_frame_or_window, Qnil);
8174 input_pending = 0;
8176 return Qnil;
8179 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
8180 "Stop Emacs and return to superior process. You can resume later.\n\
8181 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
8182 control, run a subshell instead.\n\n\
8183 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
8184 to be read as terminal input by Emacs's parent, after suspension.\n\
8186 Before suspending, run the normal hook `suspend-hook'.\n\
8187 After resumption run the normal hook `suspend-resume-hook'.\n\
8189 Some operating systems cannot stop the Emacs process and resume it later.\n\
8190 On such systems, Emacs starts a subshell instead of suspending.")
8191 (stuffstring)
8192 Lisp_Object stuffstring;
8194 Lisp_Object tem;
8195 int count = specpdl_ptr - specpdl;
8196 int old_height, old_width;
8197 int width, height;
8198 struct gcpro gcpro1, gcpro2;
8200 if (!NILP (stuffstring))
8201 CHECK_STRING (stuffstring, 0);
8203 /* Run the functions in suspend-hook. */
8204 if (!NILP (Vrun_hooks))
8205 call1 (Vrun_hooks, intern ("suspend-hook"));
8207 GCPRO1 (stuffstring);
8208 get_frame_size (&old_width, &old_height);
8209 reset_sys_modes ();
8210 /* sys_suspend can get an error if it tries to fork a subshell
8211 and the system resources aren't available for that. */
8212 record_unwind_protect ((Lisp_Object (*) P_ ((Lisp_Object))) init_sys_modes,
8213 Qnil);
8214 stuff_buffered_input (stuffstring);
8215 if (cannot_suspend)
8216 sys_subshell ();
8217 else
8218 sys_suspend ();
8219 unbind_to (count, Qnil);
8221 /* Check if terminal/window size has changed.
8222 Note that this is not useful when we are running directly
8223 with a window system; but suspend should be disabled in that case. */
8224 get_frame_size (&width, &height);
8225 if (width != old_width || height != old_height)
8226 change_frame_size (selected_frame, height, width, 0, 0);
8228 /* Run suspend-resume-hook. */
8229 if (!NILP (Vrun_hooks))
8230 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
8232 UNGCPRO;
8233 return Qnil;
8236 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
8237 Then in any case stuff anything Emacs has read ahead and not used. */
8239 void
8240 stuff_buffered_input (stuffstring)
8241 Lisp_Object stuffstring;
8243 /* stuff_char works only in BSD, versions 4.2 and up. */
8244 #ifdef BSD_SYSTEM
8245 #ifndef BSD4_1
8246 register unsigned char *p;
8248 if (STRINGP (stuffstring))
8250 register int count;
8252 p = XSTRING (stuffstring)->data;
8253 count = STRING_BYTES (XSTRING (stuffstring));
8254 while (count-- > 0)
8255 stuff_char (*p++);
8256 stuff_char ('\n');
8258 /* Anything we have read ahead, put back for the shell to read. */
8259 /* ?? What should this do when we have multiple keyboards??
8260 Should we ignore anything that was typed in at the "wrong" kboard? */
8261 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
8263 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
8264 kbd_fetch_ptr = kbd_buffer;
8265 if (kbd_fetch_ptr->kind == ascii_keystroke)
8266 stuff_char (kbd_fetch_ptr->code);
8267 kbd_fetch_ptr->kind = no_event;
8268 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
8269 - kbd_buffer]
8270 = Qnil);
8272 input_pending = 0;
8273 #endif
8274 #endif /* BSD_SYSTEM and not BSD4_1 */
8277 void
8278 set_waiting_for_input (time_to_clear)
8279 EMACS_TIME *time_to_clear;
8281 input_available_clear_time = time_to_clear;
8283 /* Tell interrupt_signal to throw back to read_char, */
8284 waiting_for_input = 1;
8286 /* If interrupt_signal was called before and buffered a C-g,
8287 make it run again now, to avoid timing error. */
8288 if (!NILP (Vquit_flag))
8289 quit_throw_to_read_char ();
8292 void
8293 clear_waiting_for_input ()
8295 /* Tell interrupt_signal not to throw back to read_char, */
8296 waiting_for_input = 0;
8297 input_available_clear_time = 0;
8300 /* This routine is called at interrupt level in response to C-G.
8301 If interrupt_input, this is the handler for SIGINT.
8302 Otherwise, it is called from kbd_buffer_store_event,
8303 in handling SIGIO or SIGTINT.
8305 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
8306 immediately throw back to read_char.
8308 Otherwise it sets the Lisp variable quit-flag not-nil.
8309 This causes eval to throw, when it gets a chance.
8310 If quit-flag is already non-nil, it stops the job right away. */
8312 SIGTYPE
8313 interrupt_signal (signalnum) /* If we don't have an argument, */
8314 int signalnum; /* some compilers complain in signal calls. */
8316 char c;
8317 /* Must preserve main program's value of errno. */
8318 int old_errno = errno;
8320 #if defined (USG) && !defined (POSIX_SIGNALS)
8321 if (!read_socket_hook && NILP (Vwindow_system))
8323 /* USG systems forget handlers when they are used;
8324 must reestablish each time */
8325 signal (SIGINT, interrupt_signal);
8326 signal (SIGQUIT, interrupt_signal);
8328 #endif /* USG */
8330 cancel_echoing ();
8332 if (!NILP (Vquit_flag)
8333 && (FRAME_TERMCAP_P (selected_frame) || FRAME_MSDOS_P (selected_frame)))
8335 /* If SIGINT isn't blocked, don't let us be interrupted by
8336 another SIGINT, it might be harmful due to non-reentrancy
8337 in I/O functions. */
8338 sigblock (sigmask (SIGINT));
8340 fflush (stdout);
8341 reset_sys_modes ();
8343 #ifdef SIGTSTP /* Support possible in later USG versions */
8345 * On systems which can suspend the current process and return to the original
8346 * shell, this command causes the user to end up back at the shell.
8347 * The "Auto-save" and "Abort" questions are not asked until
8348 * the user elects to return to emacs, at which point he can save the current
8349 * job and either dump core or continue.
8351 sys_suspend ();
8352 #else
8353 #ifdef VMS
8354 if (sys_suspend () == -1)
8356 printf ("Not running as a subprocess;\n");
8357 printf ("you can continue or abort.\n");
8359 #else /* not VMS */
8360 /* Perhaps should really fork an inferior shell?
8361 But that would not provide any way to get back
8362 to the original shell, ever. */
8363 printf ("No support for stopping a process on this operating system;\n");
8364 printf ("you can continue or abort.\n");
8365 #endif /* not VMS */
8366 #endif /* not SIGTSTP */
8367 #ifdef MSDOS
8368 /* We must remain inside the screen area when the internal terminal
8369 is used. Note that [Enter] is not echoed by dos. */
8370 cursor_to (0, 0);
8371 #endif
8372 /* It doesn't work to autosave while GC is in progress;
8373 the code used for auto-saving doesn't cope with the mark bit. */
8374 if (!gc_in_progress)
8376 printf ("Auto-save? (y or n) ");
8377 fflush (stdout);
8378 if (((c = getchar ()) & ~040) == 'Y')
8380 Fdo_auto_save (Qt, Qnil);
8381 #ifdef MSDOS
8382 printf ("\r\nAuto-save done");
8383 #else /* not MSDOS */
8384 printf ("Auto-save done\n");
8385 #endif /* not MSDOS */
8387 while (c != '\n') c = getchar ();
8389 else
8391 /* During GC, it must be safe to reenable quitting again. */
8392 Vinhibit_quit = Qnil;
8393 #ifdef MSDOS
8394 printf ("\r\n");
8395 #endif /* not MSDOS */
8396 printf ("Garbage collection in progress; cannot auto-save now\r\n");
8397 printf ("but will instead do a real quit after garbage collection ends\r\n");
8398 fflush (stdout);
8401 #ifdef MSDOS
8402 printf ("\r\nAbort? (y or n) ");
8403 #else /* not MSDOS */
8404 #ifdef VMS
8405 printf ("Abort (and enter debugger)? (y or n) ");
8406 #else /* not VMS */
8407 printf ("Abort (and dump core)? (y or n) ");
8408 #endif /* not VMS */
8409 #endif /* not MSDOS */
8410 fflush (stdout);
8411 if (((c = getchar ()) & ~040) == 'Y')
8412 abort ();
8413 while (c != '\n') c = getchar ();
8414 #ifdef MSDOS
8415 printf ("\r\nContinuing...\r\n");
8416 #else /* not MSDOS */
8417 printf ("Continuing...\n");
8418 #endif /* not MSDOS */
8419 fflush (stdout);
8420 init_sys_modes ();
8421 sigfree ();
8423 else
8425 /* If executing a function that wants to be interrupted out of
8426 and the user has not deferred quitting by binding `inhibit-quit'
8427 then quit right away. */
8428 if (immediate_quit && NILP (Vinhibit_quit))
8430 struct gl_state_s saved;
8431 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8433 immediate_quit = 0;
8434 sigfree ();
8435 saved = gl_state;
8436 GCPRO4 (saved.object, saved.global_code,
8437 saved.current_syntax_table, saved.old_prop);
8438 Fsignal (Qquit, Qnil);
8439 gl_state = saved;
8440 UNGCPRO;
8442 else
8443 /* Else request quit when it's safe */
8444 Vquit_flag = Qt;
8447 if (waiting_for_input && !echoing)
8448 quit_throw_to_read_char ();
8450 errno = old_errno;
8453 /* Handle a C-g by making read_char return C-g. */
8455 void
8456 quit_throw_to_read_char ()
8458 quit_error_check ();
8459 sigfree ();
8460 /* Prevent another signal from doing this before we finish. */
8461 clear_waiting_for_input ();
8462 input_pending = 0;
8464 Vunread_command_events = Qnil;
8465 unread_command_char = -1;
8467 #if 0 /* Currently, sit_for is called from read_char without turning
8468 off polling. And that can call set_waiting_for_input.
8469 It seems to be harmless. */
8470 #ifdef POLL_FOR_INPUT
8471 /* May be > 1 if in recursive minibuffer. */
8472 if (poll_suppress_count == 0)
8473 abort ();
8474 #endif
8475 #endif
8476 if (FRAMEP (internal_last_event_frame)
8477 && XFRAME (internal_last_event_frame) != selected_frame)
8478 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
8479 Qnil, 0);
8481 _longjmp (getcjmp, 1);
8484 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
8485 "Set mode of reading keyboard input.\n\
8486 First arg INTERRUPT non-nil means use input interrupts;\n\
8487 nil means use CBREAK mode.\n\
8488 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
8489 (no effect except in CBREAK mode).\n\
8490 Third arg META t means accept 8-bit input (for a Meta key).\n\
8491 META nil means ignore the top bit, on the assumption it is parity.\n\
8492 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
8493 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
8494 See also `current-input-mode'.")
8495 (interrupt, flow, meta, quit)
8496 Lisp_Object interrupt, flow, meta, quit;
8498 if (!NILP (quit)
8499 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
8500 error ("set-input-mode: QUIT must be an ASCII character");
8502 #ifdef POLL_FOR_INPUT
8503 stop_polling ();
8504 #endif
8506 #ifndef DOS_NT
8507 /* this causes startup screen to be restored and messes with the mouse */
8508 reset_sys_modes ();
8509 #endif
8511 #ifdef SIGIO
8512 /* Note SIGIO has been undef'd if FIONREAD is missing. */
8513 if (read_socket_hook)
8515 /* When using X, don't give the user a real choice,
8516 because we haven't implemented the mechanisms to support it. */
8517 #ifdef NO_SOCK_SIGIO
8518 interrupt_input = 0;
8519 #else /* not NO_SOCK_SIGIO */
8520 interrupt_input = 1;
8521 #endif /* NO_SOCK_SIGIO */
8523 else
8524 interrupt_input = !NILP (interrupt);
8525 #else /* not SIGIO */
8526 interrupt_input = 0;
8527 #endif /* not SIGIO */
8529 /* Our VMS input only works by interrupts, as of now. */
8530 #ifdef VMS
8531 interrupt_input = 1;
8532 #endif
8534 flow_control = !NILP (flow);
8535 if (NILP (meta))
8536 meta_key = 0;
8537 else if (EQ (meta, Qt))
8538 meta_key = 1;
8539 else
8540 meta_key = 2;
8541 if (!NILP (quit))
8542 /* Don't let this value be out of range. */
8543 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
8545 #ifndef DOS_NT
8546 init_sys_modes ();
8547 #endif
8549 #ifdef POLL_FOR_INPUT
8550 poll_suppress_count = 1;
8551 start_polling ();
8552 #endif
8553 return Qnil;
8556 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
8557 "Return information about the way Emacs currently reads keyboard input.\n\
8558 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
8559 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
8560 nil, Emacs is using CBREAK mode.\n\
8561 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
8562 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
8563 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
8564 META nil means ignoring the top bit, on the assumption it is parity.\n\
8565 META is neither t nor nil if accepting 8-bit input and using\n\
8566 all 8 bits as the character code.\n\
8567 QUIT is the character Emacs currently uses to quit.\n\
8568 The elements of this list correspond to the arguments of\n\
8569 `set-input-mode'.")
8572 Lisp_Object val[4];
8574 val[0] = interrupt_input ? Qt : Qnil;
8575 val[1] = flow_control ? Qt : Qnil;
8576 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
8577 XSETFASTINT (val[3], quit_char);
8579 return Flist (sizeof (val) / sizeof (val[0]), val);
8584 * Set up a new kboard object with reasonable initial values.
8586 void
8587 init_kboard (kb)
8588 KBOARD *kb;
8590 kb->Voverriding_terminal_local_map = Qnil;
8591 kb->Vlast_command = Qnil;
8592 kb->Vreal_last_command = Qnil;
8593 kb->Vprefix_arg = Qnil;
8594 kb->Vlast_prefix_arg = Qnil;
8595 kb->kbd_queue = Qnil;
8596 kb->kbd_queue_has_data = 0;
8597 kb->immediate_echo = 0;
8598 kb->echoptr = kb->echobuf;
8599 kb->echo_after_prompt = -1;
8600 kb->kbd_macro_buffer = 0;
8601 kb->kbd_macro_bufsize = 0;
8602 kb->defining_kbd_macro = Qnil;
8603 kb->Vlast_kbd_macro = Qnil;
8604 kb->reference_count = 0;
8605 kb->Vsystem_key_alist = Qnil;
8606 kb->system_key_syms = Qnil;
8607 kb->Vdefault_minibuffer_frame = Qnil;
8611 * Destroy the contents of a kboard object, but not the object itself.
8612 * We use this just before deleting it, or if we're going to initialize
8613 * it a second time.
8615 static void
8616 wipe_kboard (kb)
8617 KBOARD *kb;
8619 if (kb->kbd_macro_buffer)
8620 xfree (kb->kbd_macro_buffer);
8623 #ifdef MULTI_KBOARD
8624 void
8625 delete_kboard (kb)
8626 KBOARD *kb;
8628 KBOARD **kbp;
8629 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
8630 if (*kbp == NULL)
8631 abort ();
8632 *kbp = kb->next_kboard;
8633 wipe_kboard (kb);
8634 xfree (kb);
8636 #endif
8638 void
8639 init_keyboard ()
8641 /* This is correct before outermost invocation of the editor loop */
8642 command_loop_level = -1;
8643 immediate_quit = 0;
8644 quit_char = Ctl ('g');
8645 Vunread_command_events = Qnil;
8646 unread_command_char = -1;
8647 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
8648 total_keys = 0;
8649 recent_keys_index = 0;
8650 kbd_fetch_ptr = kbd_buffer;
8651 kbd_store_ptr = kbd_buffer;
8652 kbd_buffer_frame_or_window
8653 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
8654 #ifdef HAVE_MOUSE
8655 do_mouse_tracking = Qnil;
8656 #endif
8657 input_pending = 0;
8659 /* This means that command_loop_1 won't try to select anything the first
8660 time through. */
8661 internal_last_event_frame = Qnil;
8662 Vlast_event_frame = internal_last_event_frame;
8664 #ifdef MULTI_KBOARD
8665 current_kboard = initial_kboard;
8666 #endif
8667 wipe_kboard (current_kboard);
8668 init_kboard (current_kboard);
8670 if (initialized)
8671 Ffillarray (kbd_buffer_frame_or_window, Qnil);
8673 kbd_buffer_frame_or_window
8674 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
8675 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
8677 signal (SIGINT, interrupt_signal);
8678 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
8679 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
8680 SIGQUIT and we can't tell which one it will give us. */
8681 signal (SIGQUIT, interrupt_signal);
8682 #endif /* HAVE_TERMIO */
8684 /* Note SIGIO has been undef'd if FIONREAD is missing. */
8685 #ifdef SIGIO
8686 if (!noninteractive)
8687 signal (SIGIO, input_available_signal);
8688 #endif /* SIGIO */
8690 /* Use interrupt input by default, if it works and noninterrupt input
8691 has deficiencies. */
8693 #ifdef INTERRUPT_INPUT
8694 interrupt_input = 1;
8695 #else
8696 interrupt_input = 0;
8697 #endif
8699 /* Our VMS input only works by interrupts, as of now. */
8700 #ifdef VMS
8701 interrupt_input = 1;
8702 #endif
8704 sigfree ();
8705 dribble = 0;
8707 if (keyboard_init_hook)
8708 (*keyboard_init_hook) ();
8710 #ifdef POLL_FOR_INPUT
8711 poll_suppress_count = 1;
8712 start_polling ();
8713 #endif
8716 /* This type's only use is in syms_of_keyboard, to initialize the
8717 event header symbols and put properties on them. */
8718 struct event_head {
8719 Lisp_Object *var;
8720 char *name;
8721 Lisp_Object *kind;
8724 struct event_head head_table[] = {
8725 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
8726 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
8727 &Qswitch_frame, "switch-frame", &Qswitch_frame,
8728 &Qdelete_frame, "delete-frame", &Qdelete_frame,
8729 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
8730 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
8733 void
8734 syms_of_keyboard ()
8736 staticpro (&item_properties);
8737 item_properties = Qnil;
8739 Qtimer_event_handler = intern ("timer-event-handler");
8740 staticpro (&Qtimer_event_handler);
8742 Qdisabled_command_hook = intern ("disabled-command-hook");
8743 staticpro (&Qdisabled_command_hook);
8745 Qself_insert_command = intern ("self-insert-command");
8746 staticpro (&Qself_insert_command);
8748 Qforward_char = intern ("forward-char");
8749 staticpro (&Qforward_char);
8751 Qbackward_char = intern ("backward-char");
8752 staticpro (&Qbackward_char);
8754 Qdisabled = intern ("disabled");
8755 staticpro (&Qdisabled);
8757 Qundefined = intern ("undefined");
8758 staticpro (&Qundefined);
8760 Qpre_command_hook = intern ("pre-command-hook");
8761 staticpro (&Qpre_command_hook);
8763 Qpost_command_hook = intern ("post-command-hook");
8764 staticpro (&Qpost_command_hook);
8766 Qpost_command_idle_hook = intern ("post-command-idle-hook");
8767 staticpro (&Qpost_command_idle_hook);
8769 Qdeferred_action_function = intern ("deferred-action-function");
8770 staticpro (&Qdeferred_action_function);
8772 Qcommand_hook_internal = intern ("command-hook-internal");
8773 staticpro (&Qcommand_hook_internal);
8775 Qfunction_key = intern ("function-key");
8776 staticpro (&Qfunction_key);
8777 Qmouse_click = intern ("mouse-click");
8778 staticpro (&Qmouse_click);
8779 #ifdef WINDOWSNT
8780 Qmouse_wheel = intern ("mouse-wheel");
8781 staticpro (&Qmouse_wheel);
8782 #endif
8783 Qdrag_n_drop = intern ("drag-n-drop");
8784 staticpro (&Qdrag_n_drop);
8786 Qmenu_enable = intern ("menu-enable");
8787 staticpro (&Qmenu_enable);
8788 Qmenu_alias = intern ("menu-alias");
8789 staticpro (&Qmenu_alias);
8790 QCenable = intern (":enable");
8791 staticpro (&QCenable);
8792 QCvisible = intern (":visible");
8793 staticpro (&QCvisible);
8794 QCfilter = intern (":filter");
8795 staticpro (&QCfilter);
8796 QCbutton = intern (":button");
8797 staticpro (&QCbutton);
8798 QCkeys = intern (":keys");
8799 staticpro (&QCkeys);
8800 QCkey_sequence = intern (":key-sequence");
8801 staticpro (&QCkey_sequence);
8802 QCtoggle = intern (":toggle");
8803 staticpro (&QCtoggle);
8804 QCradio = intern (":radio");
8805 staticpro (&QCradio);
8807 Qmode_line = intern ("mode-line");
8808 staticpro (&Qmode_line);
8809 Qvertical_line = intern ("vertical-line");
8810 staticpro (&Qvertical_line);
8811 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
8812 staticpro (&Qvertical_scroll_bar);
8813 Qmenu_bar = intern ("menu-bar");
8814 staticpro (&Qmenu_bar);
8816 Qabove_handle = intern ("above-handle");
8817 staticpro (&Qabove_handle);
8818 Qhandle = intern ("handle");
8819 staticpro (&Qhandle);
8820 Qbelow_handle = intern ("below-handle");
8821 staticpro (&Qbelow_handle);
8822 Qup = intern ("up");
8823 staticpro (&Qup);
8824 Qdown = intern ("down");
8825 staticpro (&Qdown);
8827 Qevent_kind = intern ("event-kind");
8828 staticpro (&Qevent_kind);
8829 Qevent_symbol_elements = intern ("event-symbol-elements");
8830 staticpro (&Qevent_symbol_elements);
8831 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
8832 staticpro (&Qevent_symbol_element_mask);
8833 Qmodifier_cache = intern ("modifier-cache");
8834 staticpro (&Qmodifier_cache);
8836 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
8837 staticpro (&Qrecompute_lucid_menubar);
8838 Qactivate_menubar_hook = intern ("activate-menubar-hook");
8839 staticpro (&Qactivate_menubar_hook);
8841 Qpolling_period = intern ("polling-period");
8842 staticpro (&Qpolling_period);
8844 Qinput_method_function = intern ("input-method-function");
8845 staticpro (&Qinput_method_function);
8848 struct event_head *p;
8850 for (p = head_table;
8851 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
8852 p++)
8854 *p->var = intern (p->name);
8855 staticpro (p->var);
8856 Fput (*p->var, Qevent_kind, *p->kind);
8857 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
8861 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
8862 staticpro (&button_down_location);
8865 int i;
8866 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
8868 modifier_symbols = Fmake_vector (make_number (len), Qnil);
8869 for (i = 0; i < len; i++)
8870 if (modifier_names[i])
8871 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
8872 staticpro (&modifier_symbols);
8875 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
8876 staticpro (&recent_keys);
8878 this_command_keys = Fmake_vector (make_number (40), Qnil);
8879 staticpro (&this_command_keys);
8881 raw_keybuf = Fmake_vector (make_number (30), Qnil);
8882 staticpro (&raw_keybuf);
8884 Qextended_command_history = intern ("extended-command-history");
8885 Fset (Qextended_command_history, Qnil);
8886 staticpro (&Qextended_command_history);
8888 kbd_buffer_frame_or_window
8889 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
8890 staticpro (&kbd_buffer_frame_or_window);
8892 accent_key_syms = Qnil;
8893 staticpro (&accent_key_syms);
8895 func_key_syms = Qnil;
8896 staticpro (&func_key_syms);
8898 mouse_syms = Qnil;
8899 staticpro (&mouse_syms);
8901 #ifdef WINDOWSNT
8902 mouse_wheel_syms = Qnil;
8903 staticpro (&mouse_wheel_syms);
8905 drag_n_drop_syms = Qnil;
8906 staticpro (&drag_n_drop_syms);
8907 #endif
8909 unread_switch_frame = Qnil;
8910 staticpro (&unread_switch_frame);
8912 internal_last_event_frame = Qnil;
8913 staticpro (&internal_last_event_frame);
8915 read_key_sequence_cmd = Qnil;
8916 staticpro (&read_key_sequence_cmd);
8918 defsubr (&Sevent_convert_list);
8919 defsubr (&Sread_key_sequence);
8920 defsubr (&Sread_key_sequence_vector);
8921 defsubr (&Srecursive_edit);
8922 #ifdef HAVE_MOUSE
8923 defsubr (&Strack_mouse);
8924 #endif
8925 defsubr (&Sinput_pending_p);
8926 defsubr (&Scommand_execute);
8927 defsubr (&Srecent_keys);
8928 defsubr (&Sthis_command_keys);
8929 defsubr (&Sthis_command_keys_vector);
8930 defsubr (&Sthis_single_command_keys);
8931 defsubr (&Sthis_single_command_raw_keys);
8932 defsubr (&Sreset_this_command_lengths);
8933 defsubr (&Ssuspend_emacs);
8934 defsubr (&Sabort_recursive_edit);
8935 defsubr (&Sexit_recursive_edit);
8936 defsubr (&Srecursion_depth);
8937 defsubr (&Stop_level);
8938 defsubr (&Sdiscard_input);
8939 defsubr (&Sopen_dribble_file);
8940 defsubr (&Sset_input_mode);
8941 defsubr (&Scurrent_input_mode);
8942 defsubr (&Sexecute_extended_command);
8944 DEFVAR_LISP ("last-command-char", &last_command_char,
8945 "Last input event that was part of a command.");
8947 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
8948 "Last input event that was part of a command.");
8950 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
8951 "Last input event in a command, except for mouse menu events.\n\
8952 Mouse menus give back keys that don't look like mouse events;\n\
8953 this variable holds the actual mouse event that led to the menu,\n\
8954 so that you can determine whether the command was run by mouse or not.");
8956 DEFVAR_LISP ("last-input-char", &last_input_char,
8957 "Last input event.");
8959 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
8960 "Last input event.");
8962 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
8963 "List of events to be read as the command input.\n\
8964 These events are processed first, before actual keyboard input.");
8965 Vunread_command_events = Qnil;
8967 DEFVAR_INT ("unread-command-char", &unread_command_char,
8968 "If not -1, an object to be read as next command input event.");
8970 DEFVAR_LISP ("unread-post-input-method-events", &Vunread_post_input_method_events,
8971 "List of events to be processed as input by input methods.\n\
8972 These events are processed after `unread-command-events', but\n\
8973 before actual keyboard input.");
8974 Vunread_post_input_method_events = Qnil;
8976 DEFVAR_LISP ("unread-input-method-events", &Vunread_input_method_events,
8977 "List of events to be processed as input by input methods.\n\
8978 These events are processed after `unread-command-events', but\n\
8979 before actual keyboard input.");
8980 Vunread_input_method_events = Qnil;
8982 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
8983 "Meta-prefix character code. Meta-foo as command input\n\
8984 turns into this character followed by foo.");
8985 XSETINT (meta_prefix_char, 033);
8987 DEFVAR_KBOARD ("last-command", Vlast_command,
8988 "The last command executed. Normally a symbol with a function definition,\n\
8989 but can be whatever was found in the keymap, or whatever the variable\n\
8990 `this-command' was set to by that command.\n\
8992 The value `mode-exit' is special; it means that the previous command\n\
8993 read an event that told it to exit, and it did so and unread that event.\n\
8994 In other words, the present command is the event that made the previous\n\
8995 command exit.\n\
8997 The value `kill-region' is special; it means that the previous command\n\
8998 was a kill command.");
9000 DEFVAR_KBOARD ("real-last-command", Vreal_last_command,
9001 "Same as `last-command', but never altered by Lisp code.");
9003 DEFVAR_LISP ("this-command", &this_command,
9004 "The command now being executed.\n\
9005 The command can set this variable; whatever is put here\n\
9006 will be in `last-command' during the following command.");
9007 this_command = Qnil;
9009 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
9010 "*Number of keyboard input characters between auto-saves.\n\
9011 Zero means disable autosaving due to number of characters typed.");
9012 auto_save_interval = 300;
9014 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
9015 "*Number of seconds idle time before auto-save.\n\
9016 Zero or nil means disable auto-saving due to idleness.\n\
9017 After auto-saving due to this many seconds of idle time,\n\
9018 Emacs also does a garbage collection if that seems to be warranted.");
9019 XSETFASTINT (Vauto_save_timeout, 30);
9021 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
9022 "*Nonzero means echo unfinished commands after this many seconds of pause.");
9023 echo_keystrokes = 1;
9025 DEFVAR_INT ("polling-period", &polling_period,
9026 "*Interval between polling for input during Lisp execution.\n\
9027 The reason for polling is to make C-g work to stop a running program.\n\
9028 Polling is needed only when using X windows and SIGIO does not work.\n\
9029 Polling is automatically disabled in all other cases.");
9030 polling_period = 2;
9032 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
9033 "*Maximum time between mouse clicks to make a double-click.\n\
9034 Measured in milliseconds. nil means disable double-click recognition;\n\
9035 t means double-clicks have no time limit and are detected\n\
9036 by position only.");
9037 Vdouble_click_time = make_number (500);
9039 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
9040 "*Non-nil means inhibit local map menu bar menus.");
9041 inhibit_local_menu_bar_menus = 0;
9043 DEFVAR_INT ("num-input-keys", &num_input_keys,
9044 "Number of complete key sequences read as input so far.\n\
9045 This includes key sequences read from keyboard macros.\n\
9046 The number is effectively the number of interactive command invocations.");
9047 num_input_keys = 0;
9049 DEFVAR_INT ("num-nonmacro-input-events", &num_nonmacro_input_events,
9050 "Number of input events read from the keyboard so far.\n\
9051 This does not include events generated by keyboard macros.");
9052 num_nonmacro_input_events = 0;
9054 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
9055 "The frame in which the most recently read event occurred.\n\
9056 If the last event came from a keyboard macro, this is set to `macro'.");
9057 Vlast_event_frame = Qnil;
9059 /* This variable is set up in sysdep.c. */
9060 DEFVAR_LISP ("tty-erase-char", &Vtty_erase_char,
9061 "The ERASE character as set by the user with stty.");
9063 DEFVAR_LISP ("help-char", &Vhelp_char,
9064 "Character to recognize as meaning Help.\n\
9065 When it is read, do `(eval help-form)', and display result if it's a string.\n\
9066 If the value of `help-form' is nil, this char can be read normally.");
9067 XSETINT (Vhelp_char, Ctl ('H'));
9069 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
9070 "List of input events to recognize as meaning Help.\n\
9071 These work just like the value of `help-char' (see that).");
9072 Vhelp_event_list = Qnil;
9074 DEFVAR_LISP ("help-form", &Vhelp_form,
9075 "Form to execute when character `help-char' is read.\n\
9076 If the form returns a string, that string is displayed.\n\
9077 If `help-form' is nil, the help char is not recognized.");
9078 Vhelp_form = Qnil;
9080 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
9081 "Command to run when `help-char' character follows a prefix key.\n\
9082 This command is used only when there is no actual binding\n\
9083 for that character after that prefix key.");
9084 Vprefix_help_command = Qnil;
9086 DEFVAR_LISP ("top-level", &Vtop_level,
9087 "Form to evaluate when Emacs starts up.\n\
9088 Useful to set before you dump a modified Emacs.");
9089 Vtop_level = Qnil;
9091 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
9092 "Translate table for keyboard input, or nil.\n\
9093 Each character is looked up in this string and the contents used instead.\n\
9094 The value may be a string, a vector, or a char-table.\n\
9095 If it is a string or vector of length N,\n\
9096 character codes N and up are untranslated.\n\
9097 In a vector or a char-table, an element which is nil means \"no translation\".");
9098 Vkeyboard_translate_table = Qnil;
9100 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
9101 "Non-nil means to always spawn a subshell instead of suspending,\n\
9102 even if the operating system has support for stopping a process.");
9103 cannot_suspend = 0;
9105 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
9106 "Non-nil means prompt with menus when appropriate.\n\
9107 This is done when reading from a keymap that has a prompt string,\n\
9108 for elements that have prompt strings.\n\
9109 The menu is displayed on the screen\n\
9110 if X menus were enabled at configuration\n\
9111 time and the previous event was a mouse click prefix key.\n\
9112 Otherwise, menu prompting uses the echo area.");
9113 menu_prompting = 1;
9115 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
9116 "Character to see next line of menu prompt.\n\
9117 Type this character while in a menu prompt to rotate around the lines of it.");
9118 XSETINT (menu_prompt_more_char, ' ');
9120 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
9121 "A mask of additional modifier keys to use with every keyboard character.\n\
9122 Emacs applies the modifiers of the character stored here to each keyboard\n\
9123 character it reads. For example, after evaluating the expression\n\
9124 (setq extra-keyboard-modifiers ?\\C-x)\n\
9125 all input characters will have the control modifier applied to them.\n\
9127 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
9128 not count as a control character; rather, it counts as a character\n\
9129 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
9130 cancels any modification.");
9131 extra_keyboard_modifiers = 0;
9133 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
9134 "If an editing command sets this to t, deactivate the mark afterward.\n\
9135 The command loop sets this to nil before each command,\n\
9136 and tests the value when the command returns.\n\
9137 Buffer modification stores t in this variable.");
9138 Vdeactivate_mark = Qnil;
9140 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
9141 "Temporary storage of pre-command-hook or post-command-hook.");
9142 Vcommand_hook_internal = Qnil;
9144 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
9145 "Normal hook run before each command is executed.\n\
9146 Errors running the hook are caught and ignored.");
9147 Vpre_command_hook = Qnil;
9149 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
9150 "Normal hook run after each command is executed.\n\
9151 Errors running the hook are caught and ignored.");
9152 Vpost_command_hook = Qnil;
9154 DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
9155 "Normal hook run after each command is executed, if idle.\n\
9156 Errors running the hook are caught and ignored.\n\
9157 This feature is obsolete; use idle timers instead. See `etc/NEWS'.");
9158 Vpost_command_idle_hook = Qnil;
9160 DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
9161 "Delay time before running `post-command-idle-hook'.\n\
9162 This is measured in microseconds.");
9163 post_command_idle_delay = 100000;
9165 #if 0
9166 DEFVAR_LISP ("echo-area-clear-hook", ...,
9167 "Normal hook run when clearing the echo area.");
9168 #endif
9169 Qecho_area_clear_hook = intern ("echo-area-clear-hook");
9170 XSYMBOL (Qecho_area_clear_hook)->value = Qnil;
9172 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
9173 "t means menu bar, specified Lucid style, needs to be recomputed.");
9174 Vlucid_menu_bar_dirty_flag = Qnil;
9176 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
9177 "List of menu bar items to move to the end of the menu bar.\n\
9178 The elements of the list are event types that may have menu bar bindings.");
9179 Vmenu_bar_final_items = Qnil;
9181 DEFVAR_KBOARD ("overriding-terminal-local-map",
9182 Voverriding_terminal_local_map,
9183 "Per-terminal keymap that overrides all other local keymaps.\n\
9184 If this variable is non-nil, it is used as a keymap instead of the\n\
9185 buffer's local map, and the minor mode keymaps and text property keymaps.\n\
9186 This variable is intended to let commands such as `universal-argumemnt'\n\
9187 set up a different keymap for reading the next command.");
9189 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
9190 "Keymap that overrides all other local keymaps.\n\
9191 If this variable is non-nil, it is used as a keymap instead of the\n\
9192 buffer's local map, and the minor mode keymaps and text property keymaps.");
9193 Voverriding_local_map = Qnil;
9195 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
9196 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
9197 Otherwise, the menu bar continues to reflect the buffer's local map\n\
9198 and the minor mode maps regardless of `overriding-local-map'.");
9199 Voverriding_local_map_menu_flag = Qnil;
9201 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
9202 "Keymap defining bindings for special events to execute at low level.");
9203 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
9205 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
9206 "*Non-nil means generate motion events for mouse motion.");
9208 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
9209 "Alist of system-specific X windows key symbols.\n\
9210 Each element should have the form (N . SYMBOL) where N is the\n\
9211 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
9212 and SYMBOL is its name.");
9214 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
9215 "List of deferred actions to be performed at a later time.\n\
9216 The precise format isn't relevant here; we just check whether it is nil.");
9217 Vdeferred_action_list = Qnil;
9219 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
9220 "Function to call to handle deferred actions, after each command.\n\
9221 This function is called with no arguments after each command\n\
9222 whenever `deferred-action-list' is non-nil.");
9223 Vdeferred_action_function = Qnil;
9225 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
9226 "*Non-nil means show the equivalent key-binding when M-x command has one.\n\
9227 The value can be a length of time to show the message for.\n\
9228 If the value is non-nil and not a number, we wait 2 seconds.");
9229 Vsuggest_key_bindings = Qt;
9231 DEFVAR_LISP ("timer-list", &Vtimer_list,
9232 "List of active absolute time timers in order of increasing time");
9233 Vtimer_list = Qnil;
9235 DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
9236 "List of active idle-time timers in order of increasing time");
9237 Vtimer_idle_list = Qnil;
9239 DEFVAR_LISP ("input-method-function", &Vinput_method_function,
9240 "If non-nil, the function that implements the current input method.\n\
9241 It's called with one argument, a printing character that was just read.\n\
9242 \(That means a character with code 040...0176.)\n\
9243 Typically this function uses `read-event' to read additional events.\n\
9244 When it does so, it should first bind `input-method-function' to nil\n\
9245 so it will not be called recursively.\n\
9247 The function should return a list of zero or more events\n\
9248 to be used as input. If it wants to put back some events\n\
9249 to be reconsidered, separately, by the input method,\n\
9250 it can add them to the beginning of `unread-command-events'.");
9251 Vinput_method_function = Qnil;
9254 void
9255 keys_of_keyboard ()
9257 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
9258 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
9259 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
9260 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
9261 initial_define_key (meta_map, 'x', "execute-extended-command");
9263 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
9264 "handle-delete-frame");
9265 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
9266 "ignore-event");
9267 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
9268 "ignore-event");