(speedbar-frame-parameters) Add : to custom prompt.
[emacs.git] / src / keyboard.c
blob466a19beb5b15ef93287ff91e2d28c25652b52a7
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 Vthis_command;
322 /* This is like Vthis_command, except that commands never set it. */
323 Lisp_Object real_this_command;
325 /* The value of point when the last command was executed. */
326 int last_point_position;
328 /* The buffer that was current when the last command was started. */
329 Lisp_Object last_point_position_buffer;
331 /* The frame in which the last input event occurred, or Qmacro if the
332 last event came from a macro. We use this to determine when to
333 generate switch-frame events. This may be cleared by functions
334 like Fselect_frame, to make sure that a switch-frame event is
335 generated by the next character. */
336 Lisp_Object internal_last_event_frame;
338 /* A user-visible version of the above, intended to allow users to
339 figure out where the last event came from, if the event doesn't
340 carry that information itself (i.e. if it was a character). */
341 Lisp_Object Vlast_event_frame;
343 /* The timestamp of the last input event we received from the X server.
344 X Windows wants this for selection ownership. */
345 unsigned long last_event_timestamp;
347 Lisp_Object Qself_insert_command;
348 Lisp_Object Qforward_char;
349 Lisp_Object Qbackward_char;
350 Lisp_Object Qundefined;
351 Lisp_Object Qtimer_event_handler;
353 /* read_key_sequence stores here the command definition of the
354 key sequence that it reads. */
355 Lisp_Object read_key_sequence_cmd;
357 /* Form to evaluate (if non-nil) when Emacs is started. */
358 Lisp_Object Vtop_level;
360 /* User-supplied string to translate input characters through. */
361 Lisp_Object Vkeyboard_translate_table;
363 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
364 extern Lisp_Object Vfunction_key_map;
366 /* Another keymap that maps key sequences into key sequences.
367 This one takes precedence over ordinary definitions. */
368 extern Lisp_Object Vkey_translation_map;
370 /* If non-nil, this implements the current input method. */
371 Lisp_Object Vinput_method_function;
372 Lisp_Object Qinput_method_function;
374 /* When we call Vinput_method_function,
375 this holds the echo area message that was just erased. */
376 Lisp_Object Vinput_method_previous_message;
378 /* Non-nil means deactivate the mark at end of this command. */
379 Lisp_Object Vdeactivate_mark;
381 /* Menu bar specified in Lucid Emacs fashion. */
383 Lisp_Object Vlucid_menu_bar_dirty_flag;
384 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
386 Lisp_Object Qecho_area_clear_hook;
388 /* Hooks to run before and after each command. */
389 Lisp_Object Qpre_command_hook, Vpre_command_hook;
390 Lisp_Object Qpost_command_hook, Vpost_command_hook;
391 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
392 /* Hook run after a command if there's no more input soon. */
393 Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook;
395 /* Delay time in microseconds before running post-command-idle-hook. */
396 int post_command_idle_delay;
398 /* List of deferred actions to be performed at a later time.
399 The precise format isn't relevant here; we just check whether it is nil. */
400 Lisp_Object Vdeferred_action_list;
402 /* Function to call to handle deferred actions, when there are any. */
403 Lisp_Object Vdeferred_action_function;
404 Lisp_Object Qdeferred_action_function;
406 Lisp_Object Qinput_method_exit_on_first_char;
407 Lisp_Object Qinput_method_use_echo_area;
409 /* File in which we write all commands we read. */
410 FILE *dribble;
412 /* Nonzero if input is available. */
413 int input_pending;
415 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
416 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
418 int meta_key;
420 extern char *pending_malloc_warning;
422 /* Circular buffer for pre-read keyboard input. */
423 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
425 /* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
427 The interrupt-level event handlers will never enqueue an event on a
428 frame which is not in Vframe_list, and once an event is dequeued,
429 internal_last_event_frame or the event itself points to the frame.
430 So that's all fine.
432 But while the event is sitting in the queue, it's completely
433 unprotected. Suppose the user types one command which will run for
434 a while and then delete a frame, and then types another event at
435 the frame that will be deleted, before the command gets around to
436 it. Suppose there are no references to this frame elsewhere in
437 Emacs, and a GC occurs before the second event is dequeued. Now we
438 have an event referring to a freed frame, which will crash Emacs
439 when it is dequeued.
441 Similar things happen when an event on a scroll bar is enqueued; the
442 window may be deleted while the event is in the queue.
444 So, we use this vector to protect the frame_or_window field in the
445 event queue. That way, they'll be dequeued as dead frames or
446 windows, but still valid lisp objects.
448 If kbd_buffer[i].kind != no_event, then
449 (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
450 == kbd_buffer[i].frame_or_window. */
451 static Lisp_Object kbd_buffer_frame_or_window;
453 /* Pointer to next available character in kbd_buffer.
454 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
455 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
456 next available char is in kbd_buffer[0]. */
457 static struct input_event *kbd_fetch_ptr;
459 /* Pointer to next place to store character in kbd_buffer. This
460 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
461 character should go in kbd_buffer[0]. */
462 static volatile struct input_event *kbd_store_ptr;
464 /* The above pair of variables forms a "queue empty" flag. When we
465 enqueue a non-hook event, we increment kbd_store_ptr. When we
466 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
467 there is input available iff the two pointers are not equal.
469 Why not just have a flag set and cleared by the enqueuing and
470 dequeuing functions? Such a flag could be screwed up by interrupts
471 at inopportune times. */
473 /* If this flag is non-nil, we check mouse_moved to see when the
474 mouse moves, and motion events will appear in the input stream.
475 Otherwise, mouse motion is ignored. */
476 static Lisp_Object do_mouse_tracking;
478 /* Symbols to head events. */
479 Lisp_Object Qmouse_movement;
480 Lisp_Object Qscroll_bar_movement;
481 Lisp_Object Qswitch_frame;
482 Lisp_Object Qdelete_frame;
483 Lisp_Object Qiconify_frame;
484 Lisp_Object Qmake_frame_visible;
486 /* Symbols to denote kinds of events. */
487 Lisp_Object Qfunction_key;
488 Lisp_Object Qmouse_click;
489 #ifdef WINDOWSNT
490 Lisp_Object Qmouse_wheel;
491 Lisp_Object Qlanguage_change;
492 #endif
493 Lisp_Object Qdrag_n_drop;
494 /* Lisp_Object Qmouse_movement; - also an event header */
496 /* Properties of event headers. */
497 Lisp_Object Qevent_kind;
498 Lisp_Object Qevent_symbol_elements;
500 /* menu item parts */
501 Lisp_Object Qmenu_alias;
502 Lisp_Object Qmenu_enable;
503 Lisp_Object QCenable, QCvisible, QChelp, QCfilter, QCkeys, QCkey_sequence;
504 Lisp_Object QCbutton, QCtoggle, QCradio;
505 extern Lisp_Object Vdefine_key_rebound_commands;
506 extern Lisp_Object Qmenu_item;
508 /* An event header symbol HEAD may have a property named
509 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
510 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
511 mask of modifiers applied to it. If present, this is used to help
512 speed up parse_modifiers. */
513 Lisp_Object Qevent_symbol_element_mask;
515 /* An unmodified event header BASE may have a property named
516 Qmodifier_cache, which is an alist mapping modifier masks onto
517 modified versions of BASE. If present, this helps speed up
518 apply_modifiers. */
519 Lisp_Object Qmodifier_cache;
521 /* Symbols to use for parts of windows. */
522 Lisp_Object Qmode_line;
523 Lisp_Object Qvertical_line;
524 Lisp_Object Qvertical_scroll_bar;
525 Lisp_Object Qmenu_bar;
527 Lisp_Object recursive_edit_unwind (), command_loop ();
528 Lisp_Object Fthis_command_keys ();
529 Lisp_Object Qextended_command_history;
530 EMACS_TIME timer_check ();
532 extern Lisp_Object Vhistory_length;
534 extern char *x_get_keysym_name ();
536 static void record_menu_key ();
538 Lisp_Object Qpolling_period;
540 /* List of absolute timers. Appears in order of next scheduled event. */
541 Lisp_Object Vtimer_list;
543 /* List of idle time timers. Appears in order of next scheduled event. */
544 Lisp_Object Vtimer_idle_list;
546 /* Incremented whenever a timer is run. */
547 int timers_run;
549 extern Lisp_Object Vprint_level, Vprint_length;
551 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
552 happens. */
553 EMACS_TIME *input_available_clear_time;
555 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
556 Default is 1 if INTERRUPT_INPUT is defined. */
557 int interrupt_input;
559 /* Nonzero while interrupts are temporarily deferred during redisplay. */
560 int interrupts_deferred;
562 /* Nonzero means use ^S/^Q for flow control. */
563 int flow_control;
565 /* Allow m- file to inhibit use of FIONREAD. */
566 #ifdef BROKEN_FIONREAD
567 #undef FIONREAD
568 #endif
570 /* We are unable to use interrupts if FIONREAD is not available,
571 so flush SIGIO so we won't try. */
572 #ifndef FIONREAD
573 #ifdef SIGIO
574 #undef SIGIO
575 #endif
576 #endif
578 /* If we support a window system, turn on the code to poll periodically
579 to detect C-g. It isn't actually used when doing interrupt input. */
580 #ifdef HAVE_WINDOW_SYSTEM
581 #define POLL_FOR_INPUT
582 #endif
584 /* Global variable declarations. */
586 /* Function for init_keyboard to call with no args (if nonzero). */
587 void (*keyboard_init_hook) ();
589 static int read_avail_input ();
590 static void get_input_pending ();
591 static int readable_events ();
592 static Lisp_Object read_char_x_menu_prompt ();
593 static Lisp_Object read_char_minibuf_menu_prompt ();
594 static Lisp_Object make_lispy_event ();
595 #ifdef HAVE_MOUSE
596 static Lisp_Object make_lispy_movement ();
597 #endif
598 static Lisp_Object modify_event_symbol ();
599 static Lisp_Object make_lispy_switch_frame ();
600 static int parse_solitary_modifier ();
601 static void save_getcjmp ();
602 static void restore_getcjmp ();
604 /* > 0 if we are to echo keystrokes. */
605 static int echo_keystrokes;
607 /* Nonzero means don't try to suspend even if the operating system seems
608 to support it. */
609 static int cannot_suspend;
611 #define min(a,b) ((a)<(b)?(a):(b))
612 #define max(a,b) ((a)>(b)?(a):(b))
614 /* Install the string STR as the beginning of the string of echoing,
615 so that it serves as a prompt for the next character.
616 Also start echoing. */
618 void
619 echo_prompt (str)
620 char *str;
622 int len = strlen (str);
624 if (len > ECHOBUFSIZE - 4)
625 len = ECHOBUFSIZE - 4;
626 bcopy (str, current_kboard->echobuf, len);
627 current_kboard->echoptr = current_kboard->echobuf + len;
628 *current_kboard->echoptr = '\0';
630 current_kboard->echo_after_prompt = len;
632 echo_now ();
635 /* Add C to the echo string, if echoing is going on.
636 C can be a character, which is printed prettily ("M-C-x" and all that
637 jazz), or a symbol, whose name is printed. */
639 void
640 echo_char (c)
641 Lisp_Object c;
643 extern char *push_key_description ();
645 if (current_kboard->immediate_echo)
647 char *ptr = current_kboard->echoptr;
649 if (ptr != current_kboard->echobuf)
650 *ptr++ = ' ';
652 /* If someone has passed us a composite event, use its head symbol. */
653 c = EVENT_HEAD (c);
655 if (INTEGERP (c))
657 if (ptr - current_kboard->echobuf > ECHOBUFSIZE - 6)
658 return;
660 ptr = push_key_description (XINT (c), ptr);
662 else if (SYMBOLP (c))
664 struct Lisp_String *name = XSYMBOL (c)->name;
665 if ((ptr - current_kboard->echobuf) + STRING_BYTES (name) + 4
666 > ECHOBUFSIZE)
667 return;
668 bcopy (name->data, ptr, STRING_BYTES (name));
669 ptr += STRING_BYTES (name);
672 if (current_kboard->echoptr == current_kboard->echobuf
673 && help_char_p (c))
675 strcpy (ptr, " (Type ? for further options)");
676 ptr += strlen (ptr);
679 *ptr = 0;
680 current_kboard->echoptr = ptr;
682 echo_now ();
686 /* Temporarily add a dash to the end of the echo string if it's not
687 empty, so that it serves as a mini-prompt for the very next character. */
689 void
690 echo_dash ()
692 if (!current_kboard->immediate_echo
693 && current_kboard->echoptr == current_kboard->echobuf)
694 return;
695 /* Do nothing if we just printed a prompt. */
696 if (current_kboard->echo_after_prompt
697 == current_kboard->echoptr - current_kboard->echobuf)
698 return;
699 /* Do nothing if not echoing at all. */
700 if (current_kboard->echoptr == 0)
701 return;
703 /* Put a dash at the end of the buffer temporarily,
704 but make it go away when the next character is added. */
705 current_kboard->echoptr[0] = '-';
706 current_kboard->echoptr[1] = 0;
708 echo_now ();
711 /* Display the current echo string, and begin echoing if not already
712 doing so. */
714 void
715 echo_now ()
717 if (!current_kboard->immediate_echo)
719 int i;
720 current_kboard->immediate_echo = 1;
722 for (i = 0; i < this_command_key_count; i++)
724 Lisp_Object c;
725 c = XVECTOR (this_command_keys)->contents[i];
726 if (! (EVENT_HAS_PARAMETERS (c)
727 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
728 echo_char (c);
730 echo_dash ();
733 echoing = 1;
734 message2_nolog (current_kboard->echobuf, strlen (current_kboard->echobuf),
735 ! NILP (current_buffer->enable_multibyte_characters));
737 echoing = 0;
739 if (waiting_for_input && !NILP (Vquit_flag))
740 quit_throw_to_read_char ();
743 /* Turn off echoing, for the start of a new command. */
745 void
746 cancel_echoing ()
748 current_kboard->immediate_echo = 0;
749 current_kboard->echoptr = current_kboard->echobuf;
750 current_kboard->echo_after_prompt = -1;
751 ok_to_echo_at_next_pause = 0;
754 /* Return the length of the current echo string. */
756 static int
757 echo_length ()
759 return current_kboard->echoptr - current_kboard->echobuf;
762 /* Truncate the current echo message to its first LEN chars.
763 This and echo_char get used by read_key_sequence when the user
764 switches frames while entering a key sequence. */
766 static void
767 echo_truncate (len)
768 int len;
770 current_kboard->echobuf[len] = '\0';
771 current_kboard->echoptr = current_kboard->echobuf + len;
772 truncate_echo_area (len);
776 /* Functions for manipulating this_command_keys. */
777 static void
778 add_command_key (key)
779 Lisp_Object key;
781 int size = XVECTOR (this_command_keys)->size;
783 /* If reset-this-command-length was called recently, obey it now.
784 See the doc string of that function for an explanation of why. */
785 if (before_command_restore_flag)
787 this_command_key_count = before_command_key_count_1;
788 if (this_command_key_count < this_single_command_key_start)
789 this_single_command_key_start = this_command_key_count;
790 echo_truncate (before_command_echo_length_1);
791 before_command_restore_flag = 0;
794 if (this_command_key_count >= size)
796 Lisp_Object new_keys;
798 new_keys = Fmake_vector (make_number (size * 2), Qnil);
799 bcopy (XVECTOR (this_command_keys)->contents,
800 XVECTOR (new_keys)->contents,
801 size * sizeof (Lisp_Object));
803 this_command_keys = new_keys;
806 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
809 Lisp_Object
810 recursive_edit_1 ()
812 int count = specpdl_ptr - specpdl;
813 Lisp_Object val;
815 if (command_loop_level > 0)
817 specbind (Qstandard_output, Qt);
818 specbind (Qstandard_input, Qt);
821 val = command_loop ();
822 if (EQ (val, Qt))
823 Fsignal (Qquit, Qnil);
824 /* Handle throw from read_minibuf when using minibuffer
825 while it's active but we're in another window. */
826 if (STRINGP (val))
827 Fsignal (Qerror, Fcons (val, Qnil));
829 return unbind_to (count, Qnil);
832 /* When an auto-save happens, record the "time", and don't do again soon. */
834 void
835 record_auto_save ()
837 last_auto_save = num_nonmacro_input_events;
840 /* Make an auto save happen as soon as possible at command level. */
842 void
843 force_auto_save_soon ()
845 last_auto_save = - auto_save_interval - 1;
847 record_asynch_buffer_change ();
850 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
851 "Invoke the editor command loop recursively.\n\
852 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
853 that tells this function to return.\n\
854 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
855 This function is called by the editor initialization to begin editing.")
858 int count = specpdl_ptr - specpdl;
859 Lisp_Object val;
861 command_loop_level++;
862 update_mode_lines = 1;
864 record_unwind_protect (recursive_edit_unwind,
865 (command_loop_level
866 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
867 ? Fcurrent_buffer ()
868 : Qnil);
869 recursive_edit_1 ();
870 return unbind_to (count, Qnil);
873 Lisp_Object
874 recursive_edit_unwind (buffer)
875 Lisp_Object buffer;
877 if (!NILP (buffer))
878 Fset_buffer (buffer);
880 command_loop_level--;
881 update_mode_lines = 1;
882 return Qnil;
885 static void
886 any_kboard_state ()
888 #ifdef MULTI_KBOARD
889 #if 0 /* Theory: if there's anything in Vunread_command_events,
890 it will right away be read by read_key_sequence,
891 and then if we do switch KBOARDS, it will go into the side
892 queue then. So we don't need to do anything special here -- rms. */
893 if (CONSP (Vunread_command_events))
895 current_kboard->kbd_queue
896 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
897 current_kboard->kbd_queue_has_data = 1;
899 Vunread_command_events = Qnil;
900 #endif
901 single_kboard = 0;
902 #endif
905 /* Switch to the single-kboard state, making current_kboard
906 the only KBOARD from which further input is accepted. */
908 void
909 single_kboard_state ()
911 #ifdef MULTI_KBOARD
912 single_kboard = 1;
913 #endif
916 /* Maintain a stack of kboards, so other parts of Emacs
917 can switch temporarily to the kboard of a given frame
918 and then revert to the previous status. */
920 struct kboard_stack
922 KBOARD *kboard;
923 struct kboard_stack *next;
926 static struct kboard_stack *kboard_stack;
928 void
929 push_frame_kboard (f)
930 FRAME_PTR f;
932 #ifdef MULTI_KBOARD
933 struct kboard_stack *p
934 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
936 p->next = kboard_stack;
937 p->kboard = current_kboard;
938 kboard_stack = p;
940 current_kboard = FRAME_KBOARD (f);
941 #endif
944 void
945 pop_frame_kboard ()
947 #ifdef MULTI_KBOARD
948 struct kboard_stack *p = kboard_stack;
949 current_kboard = p->kboard;
950 kboard_stack = p->next;
951 xfree (p);
952 #endif
955 /* Handle errors that are not handled at inner levels
956 by printing an error message and returning to the editor command loop. */
958 Lisp_Object
959 cmd_error (data)
960 Lisp_Object data;
962 Lisp_Object old_level, old_length;
963 char macroerror[50];
965 if (!NILP (executing_macro))
967 if (executing_macro_iterations == 1)
968 sprintf (macroerror, "After 1 kbd macro iteration: ");
969 else
970 sprintf (macroerror, "After %d kbd macro iterations: ",
971 executing_macro_iterations);
973 else
974 *macroerror = 0;
976 Vstandard_output = Qt;
977 Vstandard_input = Qt;
978 Vexecuting_macro = Qnil;
979 executing_macro = Qnil;
980 current_kboard->Vprefix_arg = Qnil;
981 current_kboard->Vlast_prefix_arg = Qnil;
982 cancel_echoing ();
984 /* Avoid unquittable loop if data contains a circular list. */
985 old_level = Vprint_level;
986 old_length = Vprint_length;
987 XSETFASTINT (Vprint_level, 10);
988 XSETFASTINT (Vprint_length, 10);
989 cmd_error_internal (data, macroerror);
990 Vprint_level = old_level;
991 Vprint_length = old_length;
993 Vquit_flag = Qnil;
995 Vinhibit_quit = Qnil;
996 #ifdef MULTI_KBOARD
997 any_kboard_state ();
998 #endif
1000 return make_number (0);
1003 /* Take actions on handling an error. DATA is the data that describes
1004 the error.
1006 CONTEXT is a C-string containing ASCII characters only which
1007 describes the context in which the error happened. If we need to
1008 generalize CONTEXT to allow multibyte characters, make it a Lisp
1009 string. */
1011 void
1012 cmd_error_internal (data, context)
1013 Lisp_Object data;
1014 char *context;
1016 Lisp_Object stream;
1018 Vquit_flag = Qnil;
1019 Vinhibit_quit = Qt;
1020 echo_area_glyphs = 0;
1022 /* If the window system or terminal frame hasn't been initialized
1023 yet, or we're not interactive, it's best to dump this message out
1024 to stderr and exit. */
1025 if (! FRAME_MESSAGE_BUF (selected_frame)
1026 || noninteractive)
1027 stream = Qexternal_debugging_output;
1028 else
1030 Fdiscard_input ();
1031 bitch_at_user ();
1032 stream = Qt;
1035 if (context != 0)
1036 write_string_1 (context, -1, stream);
1038 print_error_message (data, stream);
1040 /* If the window system or terminal frame hasn't been initialized
1041 yet, or we're in -batch mode, this error should cause Emacs to exit. */
1042 if (! FRAME_MESSAGE_BUF (selected_frame)
1043 || noninteractive)
1045 Fterpri (stream);
1046 Fkill_emacs (make_number (-1));
1050 Lisp_Object command_loop_1 ();
1051 Lisp_Object command_loop_2 ();
1052 Lisp_Object top_level_1 ();
1054 /* Entry to editor-command-loop.
1055 This level has the catches for exiting/returning to editor command loop.
1056 It returns nil to exit recursive edit, t to abort it. */
1058 Lisp_Object
1059 command_loop ()
1061 if (command_loop_level > 0 || minibuf_level > 0)
1063 Lisp_Object val;
1064 val = internal_catch (Qexit, command_loop_2, Qnil);
1065 executing_macro = Qnil;
1066 return val;
1068 else
1069 while (1)
1071 internal_catch (Qtop_level, top_level_1, Qnil);
1072 internal_catch (Qtop_level, command_loop_2, Qnil);
1073 executing_macro = Qnil;
1075 /* End of file in -batch run causes exit here. */
1076 if (noninteractive)
1077 Fkill_emacs (Qt);
1081 /* Here we catch errors in execution of commands within the
1082 editing loop, and reenter the editing loop.
1083 When there is an error, cmd_error runs and returns a non-nil
1084 value to us. A value of nil means that cmd_loop_1 itself
1085 returned due to end of file (or end of kbd macro). */
1087 Lisp_Object
1088 command_loop_2 ()
1090 register Lisp_Object val;
1093 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
1094 while (!NILP (val));
1096 return Qnil;
1099 Lisp_Object
1100 top_level_2 ()
1102 return Feval (Vtop_level);
1105 Lisp_Object
1106 top_level_1 ()
1108 /* On entry to the outer level, run the startup file */
1109 if (!NILP (Vtop_level))
1110 internal_condition_case (top_level_2, Qerror, cmd_error);
1111 else if (!NILP (Vpurify_flag))
1112 message ("Bare impure Emacs (standard Lisp code not loaded)");
1113 else
1114 message ("Bare Emacs (standard Lisp code not loaded)");
1115 return Qnil;
1118 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1119 "Exit all recursive editing levels.")
1122 Fthrow (Qtop_level, Qnil);
1125 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1126 "Exit from the innermost recursive edit or minibuffer.")
1129 if (command_loop_level > 0 || minibuf_level > 0)
1130 Fthrow (Qexit, Qnil);
1132 error ("No recursive edit is in progress");
1135 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1136 "Abort the command that requested this recursive edit or minibuffer input.")
1139 if (command_loop_level > 0 || minibuf_level > 0)
1140 Fthrow (Qexit, Qt);
1142 error ("No recursive edit is in progress");
1145 /* This is the actual command reading loop,
1146 sans error-handling encapsulation. */
1148 Lisp_Object Fcommand_execute ();
1149 static int read_key_sequence ();
1150 void safe_run_hooks ();
1152 Lisp_Object
1153 command_loop_1 ()
1155 Lisp_Object cmd, tem;
1156 int lose, lose2;
1157 int nonundocount;
1158 Lisp_Object keybuf[30];
1159 int i;
1160 int no_redisplay;
1161 int no_direct;
1162 int prev_modiff;
1163 struct buffer *prev_buffer;
1164 #ifdef MULTI_KBOARD
1165 int was_locked = single_kboard;
1166 #endif
1168 current_kboard->Vprefix_arg = Qnil;
1169 current_kboard->Vlast_prefix_arg = Qnil;
1170 Vdeactivate_mark = Qnil;
1171 waiting_for_input = 0;
1172 cancel_echoing ();
1174 nonundocount = 0;
1175 no_redisplay = 0;
1176 this_command_key_count = 0;
1177 this_single_command_key_start = 0;
1179 /* Make sure this hook runs after commands that get errors and
1180 throw to top level. */
1181 /* Note that the value cell will never directly contain nil
1182 if the symbol is a local variable. */
1183 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1184 safe_run_hooks (Qpost_command_hook);
1186 if (!NILP (Vdeferred_action_list))
1187 call0 (Vdeferred_action_function);
1189 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1191 if (NILP (Vunread_command_events)
1192 && NILP (Vunread_input_method_events)
1193 && NILP (Vunread_post_input_method_events)
1194 && NILP (Vexecuting_macro)
1195 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1196 safe_run_hooks (Qpost_command_idle_hook);
1199 /* Do this after running Vpost_command_hook, for consistency. */
1200 current_kboard->Vlast_command = Vthis_command;
1201 current_kboard->Vreal_last_command = real_this_command;
1203 while (1)
1205 if (! FRAME_LIVE_P (selected_frame))
1206 Fkill_emacs (Qnil);
1208 /* Make sure the current window's buffer is selected. */
1209 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1210 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1212 /* Display any malloc warning that just came out. Use while because
1213 displaying one warning can cause another. */
1215 while (pending_malloc_warning)
1216 display_malloc_warning ();
1218 no_direct = 0;
1220 Vdeactivate_mark = Qnil;
1222 /* If minibuffer on and echo area in use,
1223 wait 2 sec and redraw minibuffer. */
1225 if (minibuf_level && echo_area_glyphs
1226 && EQ (minibuf_window, echo_area_window))
1228 /* Bind inhibit-quit to t so that C-g gets read in
1229 rather than quitting back to the minibuffer. */
1230 int count = specpdl_ptr - specpdl;
1231 specbind (Qinhibit_quit, Qt);
1233 Fsit_for (make_number (2), Qnil, Qnil);
1234 /* Clear the echo area. */
1235 message2 (0, 0, 0);
1236 safe_run_hooks (Qecho_area_clear_hook);
1238 unbind_to (count, Qnil);
1240 /* If a C-g came in before, treat it as input now. */
1241 if (!NILP (Vquit_flag))
1243 Vquit_flag = Qnil;
1244 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1248 #ifdef C_ALLOCA
1249 alloca (0); /* Cause a garbage collection now */
1250 /* Since we can free the most stuff here. */
1251 #endif /* C_ALLOCA */
1253 #if 0
1254 /* Select the frame that the last event came from. Usually,
1255 switch-frame events will take care of this, but if some lisp
1256 code swallows a switch-frame event, we'll fix things up here.
1257 Is this a good idea? */
1258 if (FRAMEP (internal_last_event_frame)
1259 && XFRAME (internal_last_event_frame) != selected_frame)
1260 Fselect_frame (internal_last_event_frame, Qnil);
1261 #endif
1262 /* If it has changed current-menubar from previous value,
1263 really recompute the menubar from the value. */
1264 if (! NILP (Vlucid_menu_bar_dirty_flag)
1265 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1266 call0 (Qrecompute_lucid_menubar);
1268 before_command_key_count = this_command_key_count;
1269 before_command_echo_length = echo_length ();
1271 Vthis_command = Qnil;
1272 real_this_command = Qnil;
1274 /* Read next key sequence; i gets its length. */
1275 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1276 Qnil, 0, 1, 1);
1278 /* A filter may have run while we were reading the input. */
1279 if (! FRAME_LIVE_P (selected_frame))
1280 Fkill_emacs (Qnil);
1281 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1282 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1284 ++num_input_keys;
1286 /* Now we have read a key sequence of length I,
1287 or else I is 0 and we found end of file. */
1289 if (i == 0) /* End of file -- happens only in */
1290 return Qnil; /* a kbd macro, at the end. */
1291 /* -1 means read_key_sequence got a menu that was rejected.
1292 Just loop around and read another command. */
1293 if (i == -1)
1295 cancel_echoing ();
1296 this_command_key_count = 0;
1297 this_single_command_key_start = 0;
1298 goto finalize;
1301 last_command_char = keybuf[i - 1];
1303 /* If the previous command tried to force a specific window-start,
1304 forget about that, in case this command moves point far away
1305 from that position. But also throw away beg_unchanged and
1306 end_unchanged information in that case, so that redisplay will
1307 update the whole window properly. */
1308 if (!NILP (XWINDOW (selected_window)->force_start))
1310 XWINDOW (selected_window)->force_start = Qnil;
1311 beg_unchanged = end_unchanged = 0;
1314 cmd = read_key_sequence_cmd;
1315 if (!NILP (Vexecuting_macro))
1317 if (!NILP (Vquit_flag))
1319 Vexecuting_macro = Qt;
1320 QUIT; /* Make some noise. */
1321 /* Will return since macro now empty. */
1325 /* Do redisplay processing after this command except in special
1326 cases identified below that set no_redisplay to 1.
1327 (actually, there's currently no way to prevent the redisplay,
1328 and no_redisplay is ignored.
1329 Perhaps someday we will really implement it.) */
1330 no_redisplay = 0;
1332 prev_buffer = current_buffer;
1333 prev_modiff = MODIFF;
1334 last_point_position = PT;
1335 XSETBUFFER (last_point_position_buffer, prev_buffer);
1337 /* Execute the command. */
1339 Vthis_command = cmd;
1340 real_this_command = cmd;
1341 /* Note that the value cell will never directly contain nil
1342 if the symbol is a local variable. */
1343 if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
1344 safe_run_hooks (Qpre_command_hook);
1346 if (NILP (Vthis_command))
1348 /* nil means key is undefined. */
1349 bitch_at_user ();
1350 current_kboard->defining_kbd_macro = Qnil;
1351 update_mode_lines = 1;
1352 current_kboard->Vprefix_arg = Qnil;
1354 else
1356 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1358 /* In case we jump to directly_done. */
1359 Vcurrent_prefix_arg = current_kboard->Vprefix_arg;
1361 /* Recognize some common commands in common situations and
1362 do them directly. */
1363 if (EQ (Vthis_command, Qforward_char) && PT < ZV)
1365 struct Lisp_Char_Table *dp
1366 = window_display_table (XWINDOW (selected_window));
1367 lose = FETCH_CHAR (PT_BYTE);
1368 SET_PT (PT + 1);
1369 if ((dp
1370 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1371 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1372 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1373 && (lose >= 0x20 && lose < 0x7f)))
1374 : (lose >= 0x20 && lose < 0x7f))
1375 /* To extract the case of continuation on
1376 wide-column characters. */
1377 && (WIDTH_BY_CHAR_HEAD (FETCH_BYTE (PT_BYTE)) == 1)
1378 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1379 >= MODIFF)
1380 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1381 >= OVERLAY_MODIFF)
1382 && (XFASTINT (XWINDOW (selected_window)->last_point)
1383 == PT - 1)
1384 && !windows_or_buffers_changed
1385 && EQ (current_buffer->selective_display, Qnil)
1386 && !detect_input_pending ()
1387 && NILP (XWINDOW (selected_window)->column_number_displayed)
1388 && NILP (Vexecuting_macro))
1389 no_redisplay = direct_output_forward_char (1);
1390 goto directly_done;
1392 else if (EQ (Vthis_command, Qbackward_char) && PT > BEGV)
1394 struct Lisp_Char_Table *dp
1395 = window_display_table (XWINDOW (selected_window));
1396 SET_PT (PT - 1);
1397 lose = FETCH_CHAR (PT_BYTE);
1398 if ((dp
1399 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1400 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1401 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1402 && (lose >= 0x20 && lose < 0x7f)))
1403 : (lose >= 0x20 && lose < 0x7f))
1404 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1405 >= MODIFF)
1406 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1407 >= OVERLAY_MODIFF)
1408 && (XFASTINT (XWINDOW (selected_window)->last_point)
1409 == PT + 1)
1410 && !windows_or_buffers_changed
1411 && EQ (current_buffer->selective_display, Qnil)
1412 && !detect_input_pending ()
1413 && NILP (XWINDOW (selected_window)->column_number_displayed)
1414 && NILP (Vexecuting_macro))
1415 no_redisplay = direct_output_forward_char (-1);
1416 goto directly_done;
1418 else if (EQ (Vthis_command, Qself_insert_command)
1419 /* Try this optimization only on ascii keystrokes. */
1420 && INTEGERP (last_command_char))
1422 unsigned int c = XINT (last_command_char);
1423 int value;
1425 if (NILP (Vexecuting_macro)
1426 && !EQ (minibuf_window, selected_window))
1428 if (!nonundocount || nonundocount >= 20)
1430 Fundo_boundary ();
1431 nonundocount = 0;
1433 nonundocount++;
1435 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1436 < MODIFF)
1437 || (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1438 < OVERLAY_MODIFF)
1439 || (XFASTINT (XWINDOW (selected_window)->last_point)
1440 != PT)
1441 || MODIFF <= SAVE_MODIFF
1442 || windows_or_buffers_changed
1443 || !EQ (current_buffer->selective_display, Qnil)
1444 || detect_input_pending ()
1445 || !NILP (XWINDOW (selected_window)->column_number_displayed)
1446 || !NILP (Vexecuting_macro));
1447 value = internal_self_insert (c, 0);
1448 if (value)
1449 lose = 1;
1450 if (value == 2)
1451 nonundocount = 0;
1453 if (!lose
1454 && (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n'))
1456 struct Lisp_Char_Table *dp
1457 = window_display_table (XWINDOW (selected_window));
1458 int lose = c;
1460 /* Add the offset to the character, for Finsert_char.
1461 We pass internal_self_insert the unmodified character
1462 because it itself does this offsetting. */
1463 if (! NILP (current_buffer->enable_multibyte_characters))
1464 lose = unibyte_char_to_multibyte (lose);
1466 if (dp)
1468 Lisp_Object obj;
1470 obj = DISP_CHAR_VECTOR (dp, lose);
1471 if (NILP (obj))
1473 /* Do it only for char codes
1474 that by default display as themselves. */
1475 if (lose >= 0x20 && lose <= 0x7e)
1476 no_redisplay = direct_output_for_insert (lose);
1478 else if (VECTORP (obj)
1479 && XVECTOR (obj)->size == 1
1480 && (obj = XVECTOR (obj)->contents[0],
1481 INTEGERP (obj))
1482 /* Insist face not specified in glyph. */
1483 && (XINT (obj) & ((-1) << 8)) == 0)
1484 no_redisplay
1485 = direct_output_for_insert (XINT (obj));
1487 else
1489 if (lose >= 0x20 && lose <= 0x7e)
1490 no_redisplay = direct_output_for_insert (lose);
1493 goto directly_done;
1497 /* Here for a command that isn't executed directly */
1499 nonundocount = 0;
1500 if (NILP (current_kboard->Vprefix_arg))
1501 Fundo_boundary ();
1502 Fcommand_execute (Vthis_command, Qnil, Qnil, Qnil);
1504 directly_done: ;
1505 current_kboard->Vlast_prefix_arg = Vcurrent_prefix_arg;
1507 /* Note that the value cell will never directly contain nil
1508 if the symbol is a local variable. */
1509 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1510 safe_run_hooks (Qpost_command_hook);
1512 if (!NILP (Vdeferred_action_list))
1513 safe_run_hooks (Qdeferred_action_function);
1515 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1517 if (NILP (Vunread_command_events)
1518 && NILP (Vunread_input_method_events)
1519 && NILP (Vunread_post_input_method_events)
1520 && NILP (Vexecuting_macro)
1521 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1522 safe_run_hooks (Qpost_command_idle_hook);
1525 /* If there is a prefix argument,
1526 1) We don't want Vlast_command to be ``universal-argument''
1527 (that would be dumb), so don't set Vlast_command,
1528 2) we want to leave echoing on so that the prefix will be
1529 echoed as part of this key sequence, so don't call
1530 cancel_echoing, and
1531 3) we want to leave this_command_key_count non-zero, so that
1532 read_char will realize that it is re-reading a character, and
1533 not echo it a second time.
1535 If the command didn't actually create a prefix arg,
1536 but is merely a frame event that is transparent to prefix args,
1537 then the above doesn't apply. */
1538 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1540 current_kboard->Vlast_command = Vthis_command;
1541 current_kboard->Vreal_last_command = real_this_command;
1542 cancel_echoing ();
1543 this_command_key_count = 0;
1544 this_single_command_key_start = 0;
1547 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1549 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1551 current_buffer->mark_active = Qnil;
1552 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1554 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1555 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1558 finalize:
1559 /* Install chars successfully executed in kbd macro. */
1561 if (!NILP (current_kboard->defining_kbd_macro)
1562 && NILP (current_kboard->Vprefix_arg))
1563 finalize_kbd_macro_chars ();
1565 #ifdef MULTI_KBOARD
1566 if (!was_locked)
1567 any_kboard_state ();
1568 #endif
1572 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1574 static Lisp_Object
1575 safe_run_hooks_1 (hook)
1576 Lisp_Object hook;
1578 return call1 (Vrun_hooks, Vinhibit_quit);
1581 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1583 static Lisp_Object
1584 safe_run_hooks_error (data)
1585 Lisp_Object data;
1587 Fset (Vinhibit_quit, Qnil);
1590 /* If we get an error while running the hook, cause the hook variable
1591 to be nil. Also inhibit quits, so that C-g won't cause the hook
1592 to mysteriously evaporate. */
1594 void
1595 safe_run_hooks (hook)
1596 Lisp_Object hook;
1598 Lisp_Object value;
1599 int count = specpdl_ptr - specpdl;
1600 specbind (Qinhibit_quit, hook);
1602 internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
1604 unbind_to (count, Qnil);
1607 /* Number of seconds between polling for input. */
1608 int polling_period;
1610 /* Nonzero means polling for input is temporarily suppressed. */
1611 int poll_suppress_count;
1613 /* Nonzero if polling_for_input is actually being used. */
1614 int polling_for_input;
1616 #ifdef POLL_FOR_INPUT
1618 /* Handle an alarm once each second and read pending input
1619 so as to handle a C-g if it comces in. */
1621 SIGTYPE
1622 input_poll_signal (signalnum) /* If we don't have an argument, */
1623 int signalnum; /* some compilers complain in signal calls. */
1625 /* This causes the call to start_polling at the end
1626 to do its job. It also arranges for a quit or error
1627 from within read_avail_input to resume polling. */
1628 poll_suppress_count++;
1629 if (interrupt_input_blocked == 0
1630 && !waiting_for_input)
1631 read_avail_input (0);
1632 /* Turn on the SIGALRM handler and request another alarm. */
1633 start_polling ();
1636 #endif
1638 /* Begin signals to poll for input, if they are appropriate.
1639 This function is called unconditionally from various places. */
1641 void
1642 start_polling ()
1644 #ifdef POLL_FOR_INPUT
1645 if (read_socket_hook && !interrupt_input)
1647 poll_suppress_count--;
1648 if (poll_suppress_count == 0)
1650 signal (SIGALRM, input_poll_signal);
1651 polling_for_input = 1;
1652 alarm (polling_period);
1655 #endif
1658 /* Nonzero if we are using polling to handle input asynchronously. */
1661 input_polling_used ()
1663 #ifdef POLL_FOR_INPUT
1664 return read_socket_hook && !interrupt_input;
1665 #else
1666 return 0;
1667 #endif
1670 /* Turn off polling. */
1672 void
1673 stop_polling ()
1675 #ifdef POLL_FOR_INPUT
1676 if (read_socket_hook && !interrupt_input)
1678 if (poll_suppress_count == 0)
1680 polling_for_input = 0;
1681 alarm (0);
1683 poll_suppress_count++;
1685 #endif
1688 /* Set the value of poll_suppress_count to COUNT
1689 and start or stop polling accordingly. */
1691 void
1692 set_poll_suppress_count (count)
1693 int count;
1695 #ifdef POLL_FOR_INPUT
1696 if (count == 0 && poll_suppress_count != 0)
1698 poll_suppress_count = 1;
1699 start_polling ();
1701 else if (count != 0 && poll_suppress_count == 0)
1703 stop_polling ();
1705 poll_suppress_count = count;
1706 #endif
1709 /* Bind polling_period to a value at least N.
1710 But don't decrease it. */
1712 void
1713 bind_polling_period (n)
1714 int n;
1716 #ifdef POLL_FOR_INPUT
1717 int new = polling_period;
1719 if (n > new)
1720 new = n;
1722 stop_polling ();
1723 specbind (Qpolling_period, make_number (new));
1724 /* Start a new alarm with the new period. */
1725 start_polling ();
1726 #endif
1729 /* Apply the control modifier to CHARACTER. */
1732 make_ctrl_char (c)
1733 int c;
1735 /* Save the upper bits here. */
1736 int upper = c & ~0177;
1738 c &= 0177;
1740 /* Everything in the columns containing the upper-case letters
1741 denotes a control character. */
1742 if (c >= 0100 && c < 0140)
1744 int oc = c;
1745 c &= ~0140;
1746 /* Set the shift modifier for a control char
1747 made from a shifted letter. But only for letters! */
1748 if (oc >= 'A' && oc <= 'Z')
1749 c |= shift_modifier;
1752 /* The lower-case letters denote control characters too. */
1753 else if (c >= 'a' && c <= 'z')
1754 c &= ~0140;
1756 /* Include the bits for control and shift
1757 only if the basic ASCII code can't indicate them. */
1758 else if (c >= ' ')
1759 c |= ctrl_modifier;
1761 /* Replace the high bits. */
1762 c |= (upper & ~ctrl_modifier);
1764 return c;
1769 /* Input of single characters from keyboard */
1771 Lisp_Object print_help ();
1772 static Lisp_Object kbd_buffer_get_event ();
1773 static void record_char ();
1775 #ifdef MULTI_KBOARD
1776 static jmp_buf wrong_kboard_jmpbuf;
1777 #endif
1779 /* read a character from the keyboard; call the redisplay if needed */
1780 /* commandflag 0 means do not do auto-saving, but do do redisplay.
1781 -1 means do not do redisplay, but do do autosaving.
1782 1 means do both. */
1784 /* The arguments MAPS and NMAPS are for menu prompting.
1785 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1787 PREV_EVENT is the previous input event, or nil if we are reading
1788 the first event of a key sequence (or not reading a key sequence).
1789 If PREV_EVENT is t, that is a "magic" value that says
1790 not to run input methods, but in other respects to act as if
1791 not reading a key sequence.
1793 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
1794 if we used a mouse menu to read the input, or zero otherwise. If
1795 USED_MOUSE_MENU is null, we don't dereference it.
1797 Value is t if we showed a menu and the user rejected it. */
1799 Lisp_Object
1800 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
1801 int commandflag;
1802 int nmaps;
1803 Lisp_Object *maps;
1804 Lisp_Object prev_event;
1805 int *used_mouse_menu;
1807 Lisp_Object c;
1808 int count;
1809 jmp_buf local_getcjmp;
1810 jmp_buf save_jump;
1811 int key_already_recorded = 0;
1812 Lisp_Object tem, save;
1813 Lisp_Object echo_area_message;
1814 Lisp_Object also_record;
1815 int reread;
1816 struct gcpro gcpro1, gcpro2;
1818 also_record = Qnil;
1820 before_command_key_count = this_command_key_count;
1821 before_command_echo_length = echo_length ();
1822 c = Qnil;
1823 echo_area_message = Qnil;
1825 GCPRO2 (c, echo_area_message);
1827 retry:
1829 reread = 0;
1830 if (CONSP (Vunread_post_input_method_events))
1832 c = XCONS (Vunread_post_input_method_events)->car;
1833 Vunread_post_input_method_events
1834 = XCONS (Vunread_post_input_method_events)->cdr;
1836 /* Undo what read_char_x_menu_prompt did when it unread
1837 additional keys returned by Fx_popup_menu. */
1838 if (CONSP (c)
1839 && (SYMBOLP (XCONS (c)->car) || INTEGERP (XCONS (c)->car))
1840 && NILP (XCONS (c)->cdr))
1841 c = XCONS (c)->car;
1843 reread = 1;
1844 goto reread_first;
1847 if (unread_command_char != -1)
1849 XSETINT (c, unread_command_char);
1850 unread_command_char = -1;
1852 reread = 1;
1853 goto reread_first;
1856 if (CONSP (Vunread_command_events))
1858 c = XCONS (Vunread_command_events)->car;
1859 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
1861 /* Undo what read_char_x_menu_prompt did when it unread
1862 additional keys returned by Fx_popup_menu. */
1863 if (CONSP (c)
1864 && (SYMBOLP (XCONS (c)->car) || INTEGERP (XCONS (c)->car))
1865 && NILP (XCONS (c)->cdr))
1866 c = XCONS (c)->car;
1868 reread = 1;
1869 goto reread_for_input_method;
1872 if (CONSP (Vunread_input_method_events))
1874 c = XCONS (Vunread_input_method_events)->car;
1875 Vunread_input_method_events = XCONS (Vunread_input_method_events)->cdr;
1877 /* Undo what read_char_x_menu_prompt did when it unread
1878 additional keys returned by Fx_popup_menu. */
1879 if (CONSP (c)
1880 && (SYMBOLP (XCONS (c)->car) || INTEGERP (XCONS (c)->car))
1881 && NILP (XCONS (c)->cdr))
1882 c = XCONS (c)->car;
1883 reread = 1;
1884 goto reread_for_input_method;
1887 /* If there is no function key translated before
1888 reset-this-command-lengths takes effect, forget about it. */
1889 before_command_restore_flag = 0;
1891 if (!NILP (Vexecuting_macro))
1893 /* We set this to Qmacro; since that's not a frame, nobody will
1894 try to switch frames on us, and the selected window will
1895 remain unchanged.
1897 Since this event came from a macro, it would be misleading to
1898 leave internal_last_event_frame set to wherever the last
1899 real event came from. Normally, a switch-frame event selects
1900 internal_last_event_frame after each command is read, but
1901 events read from a macro should never cause a new frame to be
1902 selected. */
1903 Vlast_event_frame = internal_last_event_frame = Qmacro;
1905 /* Exit the macro if we are at the end.
1906 Also, some things replace the macro with t
1907 to force an early exit. */
1908 if (EQ (Vexecuting_macro, Qt)
1909 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
1911 XSETINT (c, -1);
1912 RETURN_UNGCPRO (c);
1915 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
1916 if (STRINGP (Vexecuting_macro)
1917 && (XINT (c) & 0x80))
1918 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
1920 executing_macro_index++;
1922 goto from_macro;
1925 if (!NILP (unread_switch_frame))
1927 c = unread_switch_frame;
1928 unread_switch_frame = Qnil;
1930 /* This event should make it into this_command_keys, and get echoed
1931 again, so we do not set `reread'. */
1932 goto reread_first;
1935 /* if redisplay was requested */
1936 if (commandflag >= 0)
1938 /* If there is pending input, process any events which are not
1939 user-visible, such as X selection_request events. */
1940 if (input_pending
1941 || detect_input_pending_run_timers (0))
1942 swallow_events (0); /* may clear input_pending */
1944 /* Redisplay if no pending input. */
1945 while (!input_pending)
1947 redisplay ();
1949 if (!input_pending)
1950 /* Normal case: no input arrived during redisplay. */
1951 break;
1953 /* Input arrived and pre-empted redisplay.
1954 Process any events which are not user-visible. */
1955 swallow_events (0);
1956 /* If that cleared input_pending, try again to redisplay. */
1960 /* Message turns off echoing unless more keystrokes turn it on again. */
1961 if (echo_area_glyphs && *echo_area_glyphs
1962 && echo_area_glyphs != current_kboard->echobuf
1963 && ok_to_echo_at_next_pause != echo_area_glyphs)
1964 cancel_echoing ();
1965 else
1966 /* If already echoing, continue. */
1967 echo_dash ();
1969 /* Try reading a character via menu prompting in the minibuf.
1970 Try this before the sit-for, because the sit-for
1971 would do the wrong thing if we are supposed to do
1972 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
1973 after a mouse event so don't try a minibuf menu. */
1974 c = Qnil;
1975 if (nmaps > 0 && INTERACTIVE
1976 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
1977 /* Don't bring up a menu if we already have another event. */
1978 && NILP (Vunread_command_events)
1979 && unread_command_char < 0
1980 && !detect_input_pending_run_timers (0))
1982 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
1983 if (! NILP (c))
1985 key_already_recorded = 1;
1986 goto non_reread_1;
1990 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
1991 We will do that below, temporarily for short sections of code,
1992 when appropriate. local_getcjmp must be in effect
1993 around any call to sit_for or kbd_buffer_get_event;
1994 it *must not* be in effect when we call redisplay. */
1996 if (_setjmp (local_getcjmp))
1998 XSETINT (c, quit_char);
1999 XSETFRAME (internal_last_event_frame, selected_frame);
2000 Vlast_event_frame = internal_last_event_frame;
2001 /* If we report the quit char as an event,
2002 don't do so more than once. */
2003 if (!NILP (Vinhibit_quit))
2004 Vquit_flag = Qnil;
2006 #ifdef MULTI_KBOARD
2008 KBOARD *kb = FRAME_KBOARD (selected_frame);
2009 if (kb != current_kboard)
2011 Lisp_Object *tailp = &kb->kbd_queue;
2012 /* We shouldn't get here if we were in single-kboard mode! */
2013 if (single_kboard)
2014 abort ();
2015 while (CONSP (*tailp))
2016 tailp = &XCONS (*tailp)->cdr;
2017 if (!NILP (*tailp))
2018 abort ();
2019 *tailp = Fcons (c, Qnil);
2020 kb->kbd_queue_has_data = 1;
2021 current_kboard = kb;
2022 /* This is going to exit from read_char
2023 so we had better get rid of this frame's stuff. */
2024 UNGCPRO;
2025 longjmp (wrong_kboard_jmpbuf, 1);
2028 #endif
2029 goto non_reread;
2032 timer_start_idle ();
2034 /* If in middle of key sequence and minibuffer not active,
2035 start echoing if enough time elapses. */
2037 if (minibuf_level == 0 && !current_kboard->immediate_echo
2038 && this_command_key_count > 0
2039 && ! noninteractive
2040 && echo_keystrokes > 0
2041 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0
2042 || ok_to_echo_at_next_pause == echo_area_glyphs))
2044 Lisp_Object tem0;
2046 /* After a mouse event, start echoing right away.
2047 This is because we are probably about to display a menu,
2048 and we don't want to delay before doing so. */
2049 if (EVENT_HAS_PARAMETERS (prev_event))
2050 echo_now ();
2051 else
2053 save_getcjmp (save_jump);
2054 restore_getcjmp (local_getcjmp);
2055 tem0 = sit_for (echo_keystrokes, 0, 1, 1, 0);
2056 restore_getcjmp (save_jump);
2057 if (EQ (tem0, Qt)
2058 && ! CONSP (Vunread_command_events))
2059 echo_now ();
2063 /* Maybe auto save due to number of keystrokes. */
2065 if (commandflag != 0
2066 && auto_save_interval > 0
2067 && num_nonmacro_input_events - last_auto_save > max (auto_save_interval, 20)
2068 && !detect_input_pending_run_timers (0))
2070 Fdo_auto_save (Qnil, Qnil);
2071 /* Hooks can actually change some buffers in auto save. */
2072 redisplay ();
2075 /* Try reading using an X menu.
2076 This is never confused with reading using the minibuf
2077 because the recursive call of read_char in read_char_minibuf_menu_prompt
2078 does not pass on any keymaps. */
2080 if (nmaps > 0 && INTERACTIVE
2081 && !NILP (prev_event)
2082 && EVENT_HAS_PARAMETERS (prev_event)
2083 && !EQ (XCONS (prev_event)->car, Qmenu_bar)
2084 /* Don't bring up a menu if we already have another event. */
2085 && NILP (Vunread_command_events)
2086 && unread_command_char < 0)
2088 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
2090 /* Now that we have read an event, Emacs is not idle. */
2091 timer_stop_idle ();
2093 RETURN_UNGCPRO (c);
2096 /* Maybe autosave and/or garbage collect due to idleness. */
2098 if (INTERACTIVE && NILP (c))
2100 int delay_level, buffer_size;
2102 /* Slow down auto saves logarithmically in size of current buffer,
2103 and garbage collect while we're at it. */
2104 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
2105 last_non_minibuf_size = Z - BEG;
2106 buffer_size = (last_non_minibuf_size >> 8) + 1;
2107 delay_level = 0;
2108 while (buffer_size > 64)
2109 delay_level++, buffer_size -= buffer_size >> 2;
2110 if (delay_level < 4) delay_level = 4;
2111 /* delay_level is 4 for files under around 50k, 7 at 100k,
2112 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
2114 /* Auto save if enough time goes by without input. */
2115 if (commandflag != 0
2116 && num_nonmacro_input_events > last_auto_save
2117 && INTEGERP (Vauto_save_timeout)
2118 && XINT (Vauto_save_timeout) > 0)
2120 Lisp_Object tem0;
2122 save_getcjmp (save_jump);
2123 restore_getcjmp (local_getcjmp);
2124 tem0 = sit_for (delay_level * XFASTINT (Vauto_save_timeout) / 4,
2125 0, 1, 1, 0);
2126 restore_getcjmp (save_jump);
2128 if (EQ (tem0, Qt)
2129 && ! CONSP (Vunread_command_events))
2131 Fdo_auto_save (Qnil, Qnil);
2133 /* If we have auto-saved and there is still no input
2134 available, garbage collect if there has been enough
2135 consing going on to make it worthwhile. */
2136 if (!detect_input_pending_run_timers (0)
2137 && consing_since_gc > gc_cons_threshold / 2)
2138 Fgarbage_collect ();
2140 redisplay ();
2145 /* If this has become non-nil here, it has been set by a timer
2146 or sentinel or filter. */
2147 if (CONSP (Vunread_command_events))
2149 c = XCONS (Vunread_command_events)->car;
2150 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
2153 /* Read something from current KBOARD's side queue, if possible. */
2155 if (NILP (c))
2157 if (current_kboard->kbd_queue_has_data)
2159 if (!CONSP (current_kboard->kbd_queue))
2160 abort ();
2161 c = XCONS (current_kboard->kbd_queue)->car;
2162 current_kboard->kbd_queue
2163 = XCONS (current_kboard->kbd_queue)->cdr;
2164 if (NILP (current_kboard->kbd_queue))
2165 current_kboard->kbd_queue_has_data = 0;
2166 input_pending = readable_events (0);
2167 if (EVENT_HAS_PARAMETERS (c)
2168 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
2169 internal_last_event_frame = XCONS (XCONS (c)->cdr)->car;
2170 Vlast_event_frame = internal_last_event_frame;
2174 #ifdef MULTI_KBOARD
2175 /* If current_kboard's side queue is empty check the other kboards.
2176 If one of them has data that we have not yet seen here,
2177 switch to it and process the data waiting for it.
2179 Note: if the events queued up for another kboard
2180 have already been seen here, and therefore are not a complete command,
2181 the kbd_queue_has_data field is 0, so we skip that kboard here.
2182 That's to avoid an infinite loop switching between kboards here. */
2183 if (NILP (c) && !single_kboard)
2185 KBOARD *kb;
2186 for (kb = all_kboards; kb; kb = kb->next_kboard)
2187 if (kb->kbd_queue_has_data)
2189 current_kboard = kb;
2190 /* This is going to exit from read_char
2191 so we had better get rid of this frame's stuff. */
2192 UNGCPRO;
2193 longjmp (wrong_kboard_jmpbuf, 1);
2196 #endif
2198 wrong_kboard:
2200 stop_polling ();
2202 /* Finally, we read from the main queue,
2203 and if that gives us something we can't use yet, we put it on the
2204 appropriate side queue and try again. */
2206 if (NILP (c))
2208 KBOARD *kb;
2210 /* Actually read a character, waiting if necessary. */
2211 save_getcjmp (save_jump);
2212 restore_getcjmp (local_getcjmp);
2213 c = kbd_buffer_get_event (&kb, used_mouse_menu);
2214 restore_getcjmp (save_jump);
2216 #ifdef MULTI_KBOARD
2217 if (! NILP (c) && (kb != current_kboard))
2219 Lisp_Object *tailp = &kb->kbd_queue;
2220 while (CONSP (*tailp))
2221 tailp = &XCONS (*tailp)->cdr;
2222 if (!NILP (*tailp))
2223 abort ();
2224 *tailp = Fcons (c, Qnil);
2225 kb->kbd_queue_has_data = 1;
2226 c = Qnil;
2227 if (single_kboard)
2228 goto wrong_kboard;
2229 current_kboard = kb;
2230 /* This is going to exit from read_char
2231 so we had better get rid of this frame's stuff. */
2232 UNGCPRO;
2233 longjmp (wrong_kboard_jmpbuf, 1);
2235 #endif
2238 /* Terminate Emacs in batch mode if at eof. */
2239 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
2240 Fkill_emacs (make_number (1));
2242 if (INTEGERP (c))
2244 /* Add in any extra modifiers, where appropriate. */
2245 if ((extra_keyboard_modifiers & CHAR_CTL)
2246 || ((extra_keyboard_modifiers & 0177) < ' '
2247 && (extra_keyboard_modifiers & 0177) != 0))
2248 XSETINT (c, make_ctrl_char (XINT (c)));
2250 /* Transfer any other modifier bits directly from
2251 extra_keyboard_modifiers to c. Ignore the actual character code
2252 in the low 16 bits of extra_keyboard_modifiers. */
2253 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
2256 non_reread:
2258 timer_stop_idle ();
2260 start_polling ();
2262 if (NILP (c))
2264 if (commandflag >= 0
2265 && !input_pending && !detect_input_pending_run_timers (0))
2266 redisplay ();
2268 goto wrong_kboard;
2271 non_reread_1:
2273 /* Buffer switch events are only for internal wakeups
2274 so don't show them to the user.
2275 Also, don't record a key if we already did. */
2276 if (BUFFERP (c) || key_already_recorded)
2277 RETURN_UNGCPRO (c);
2279 /* Process special events within read_char
2280 and loop around to read another event. */
2281 save = Vquit_flag;
2282 Vquit_flag = Qnil;
2283 tem = get_keyelt (access_keymap (get_keymap_1 (Vspecial_event_map, 0, 0),
2284 c, 0, 0), 1);
2285 Vquit_flag = save;
2287 if (!NILP (tem))
2289 int was_locked = single_kboard;
2291 last_input_char = c;
2292 Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt);
2294 /* Resume allowing input from any kboard, if that was true before. */
2295 if (!was_locked)
2296 any_kboard_state ();
2298 goto retry;
2301 /* Handle things that only apply to characters. */
2302 if (INTEGERP (c))
2304 /* If kbd_buffer_get_event gave us an EOF, return that. */
2305 if (XINT (c) == -1)
2306 RETURN_UNGCPRO (c);
2308 if ((STRINGP (Vkeyboard_translate_table)
2309 && XSTRING (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2310 || (VECTORP (Vkeyboard_translate_table)
2311 && XVECTOR (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2312 || (CHAR_TABLE_P (Vkeyboard_translate_table)
2313 && CHAR_TABLE_ORDINARY_SLOTS > (unsigned) XFASTINT (c)))
2315 Lisp_Object d;
2316 d = Faref (Vkeyboard_translate_table, c);
2317 /* nil in keyboard-translate-table means no translation. */
2318 if (!NILP (d))
2319 c = d;
2323 /* If this event is a mouse click in the menu bar,
2324 return just menu-bar for now. Modify the mouse click event
2325 so we won't do this twice, then queue it up. */
2326 if (EVENT_HAS_PARAMETERS (c)
2327 && CONSP (XCONS (c)->cdr)
2328 && CONSP (EVENT_START (c))
2329 && CONSP (XCONS (EVENT_START (c))->cdr))
2331 Lisp_Object posn;
2333 posn = POSN_BUFFER_POSN (EVENT_START (c));
2334 /* Handle menu-bar events:
2335 insert the dummy prefix event `menu-bar'. */
2336 if (EQ (posn, Qmenu_bar))
2338 /* Change menu-bar to (menu-bar) as the event "position". */
2339 POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
2341 also_record = c;
2342 Vunread_command_events = Fcons (c, Vunread_command_events);
2343 c = posn;
2347 /* Store these characters into recent_keys, the dribble file if any,
2348 and the keyboard macro being defined, if any. */
2349 record_char (c);
2350 if (! NILP (also_record))
2351 record_char (also_record);
2353 /* Wipe the echo area.
2354 But first, if we are about to use an input method,
2355 save the echo area contents for it to refer to. */
2356 if (INTEGERP (c)
2357 && ! NILP (Vinput_method_function)
2358 && (unsigned) XINT (c) >= ' '
2359 && (unsigned) XINT (c) < 127)
2360 Vinput_method_previous_message = echo_area_message = Fcurrent_message ();
2362 /* Now wipe the echo area. */
2363 if (echo_area_glyphs)
2364 safe_run_hooks (Qecho_area_clear_hook);
2365 echo_area_glyphs = 0;
2367 reread_for_input_method:
2368 from_macro:
2369 /* Pass this to the input method, if appropriate. */
2370 if (INTEGERP (c)
2371 && ! NILP (Vinput_method_function)
2372 /* Don't run the input method within a key sequence,
2373 after the first event of the key sequence. */
2374 && NILP (prev_event)
2375 && (unsigned) XINT (c) >= ' '
2376 && (unsigned) XINT (c) < 127)
2378 Lisp_Object keys;
2379 int key_count;
2380 struct gcpro gcpro1;
2381 int count = specpdl_ptr - specpdl;
2383 /* Save the echo status. */
2384 int saved_immediate_echo = current_kboard->immediate_echo;
2385 char *saved_ok_to_echo = ok_to_echo_at_next_pause;
2386 int saved_echo_after_prompt = current_kboard->echo_after_prompt;
2388 if (before_command_restore_flag)
2390 this_command_key_count = before_command_key_count_1;
2391 if (this_command_key_count < this_single_command_key_start)
2392 this_single_command_key_start = this_command_key_count;
2393 echo_truncate (before_command_echo_length_1);
2394 before_command_restore_flag = 0;
2397 /* Save the this_command_keys status. */
2398 key_count = this_command_key_count;
2400 if (key_count > 0)
2401 keys = Fcopy_sequence (this_command_keys);
2402 else
2403 keys = Qnil;
2404 GCPRO1 (keys);
2406 /* Clear out this_command_keys. */
2407 this_command_key_count = 0;
2409 /* Now wipe the echo area. */
2410 if (echo_area_glyphs)
2411 safe_run_hooks (Qecho_area_clear_hook);
2412 echo_area_glyphs = 0;
2413 echo_truncate (0);
2415 /* If we are not reading a key sequence,
2416 never use the echo area. */
2417 if (maps == 0)
2419 specbind (Qinput_method_exit_on_first_char, Qt);
2420 specbind (Qinput_method_use_echo_area, Qt);
2423 /* Call the input method. */
2424 tem = call1 (Vinput_method_function, c);
2426 tem = unbind_to (count, tem);
2428 /* Restore the saved echoing state
2429 and this_command_keys state. */
2430 this_command_key_count = key_count;
2431 if (key_count > 0)
2432 this_command_keys = keys;
2434 cancel_echoing ();
2435 ok_to_echo_at_next_pause = saved_ok_to_echo;
2436 current_kboard->echo_after_prompt = saved_echo_after_prompt;
2437 if (saved_immediate_echo)
2438 echo_now ();
2440 UNGCPRO;
2442 /* The input method can return no events. */
2443 if (! CONSP (tem))
2445 /* Bring back the previous message, if any. */
2446 if (! NILP (echo_area_message))
2447 message_with_string ("%s", echo_area_message, 0);
2448 goto retry;
2450 /* It returned one event or more. */
2451 c = XCONS (tem)->car;
2452 Vunread_post_input_method_events
2453 = nconc2 (XCONS (tem)->cdr, Vunread_post_input_method_events);
2456 reread_first:
2458 if (this_command_key_count == 0 || ! reread)
2460 before_command_key_count = this_command_key_count;
2461 before_command_echo_length = echo_length ();
2463 /* Don't echo mouse motion events. */
2464 if (echo_keystrokes
2465 && ! (EVENT_HAS_PARAMETERS (c)
2466 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
2468 echo_char (c);
2469 if (! NILP (also_record))
2470 echo_char (also_record);
2471 /* Once we reread a character, echoing can happen
2472 the next time we pause to read a new one. */
2473 ok_to_echo_at_next_pause = echo_area_glyphs;
2476 /* Record this character as part of the current key. */
2477 add_command_key (c);
2478 if (! NILP (also_record))
2479 add_command_key (also_record);
2482 last_input_char = c;
2483 num_input_events++;
2485 /* Process the help character specially if enabled */
2486 if (!NILP (Vhelp_form) && help_char_p (c))
2488 Lisp_Object tem0;
2489 count = specpdl_ptr - specpdl;
2491 record_unwind_protect (Fset_window_configuration,
2492 Fcurrent_window_configuration (Qnil));
2494 tem0 = Feval (Vhelp_form);
2495 if (STRINGP (tem0))
2496 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
2498 cancel_echoing ();
2500 c = read_char (0, 0, 0, Qnil, 0);
2501 while (BUFFERP (c));
2502 /* Remove the help from the frame */
2503 unbind_to (count, Qnil);
2505 redisplay ();
2506 if (EQ (c, make_number (040)))
2508 cancel_echoing ();
2510 c = read_char (0, 0, 0, Qnil, 0);
2511 while (BUFFERP (c));
2515 RETURN_UNGCPRO (c);
2518 /* Record a key that came from a mouse menu.
2519 Record it for echoing, for this-command-keys, and so on. */
2521 static void
2522 record_menu_key (c)
2523 Lisp_Object c;
2525 /* Wipe the echo area. */
2526 echo_area_glyphs = 0;
2528 record_char (c);
2530 before_command_key_count = this_command_key_count;
2531 before_command_echo_length = echo_length ();
2533 /* Don't echo mouse motion events. */
2534 if (echo_keystrokes)
2536 echo_char (c);
2538 /* Once we reread a character, echoing can happen
2539 the next time we pause to read a new one. */
2540 ok_to_echo_at_next_pause = 0;
2543 /* Record this character as part of the current key. */
2544 add_command_key (c);
2546 /* Re-reading in the middle of a command */
2547 last_input_char = c;
2548 num_input_events++;
2551 /* Return 1 if should recognize C as "the help character". */
2554 help_char_p (c)
2555 Lisp_Object c;
2557 Lisp_Object tail;
2559 if (EQ (c, Vhelp_char))
2560 return 1;
2561 for (tail = Vhelp_event_list; CONSP (tail); tail = XCONS (tail)->cdr)
2562 if (EQ (c, XCONS (tail)->car))
2563 return 1;
2564 return 0;
2567 /* Record the input event C in various ways. */
2569 static void
2570 record_char (c)
2571 Lisp_Object c;
2573 total_keys++;
2574 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
2575 if (++recent_keys_index >= NUM_RECENT_KEYS)
2576 recent_keys_index = 0;
2578 /* Write c to the dribble file. If c is a lispy event, write
2579 the event's symbol to the dribble file, in <brackets>. Bleaugh.
2580 If you, dear reader, have a better idea, you've got the source. :-) */
2581 if (dribble)
2583 if (INTEGERP (c))
2585 if (XUINT (c) < 0x100)
2586 putc (XINT (c), dribble);
2587 else
2588 fprintf (dribble, " 0x%x", (int) XUINT (c));
2590 else
2592 Lisp_Object dribblee;
2594 /* If it's a structured event, take the event header. */
2595 dribblee = EVENT_HEAD (c);
2597 if (SYMBOLP (dribblee))
2599 putc ('<', dribble);
2600 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
2601 STRING_BYTES (XSYMBOL (dribblee)->name),
2602 dribble);
2603 putc ('>', dribble);
2607 fflush (dribble);
2610 store_kbd_macro_char (c);
2612 num_nonmacro_input_events++;
2615 Lisp_Object
2616 print_help (object)
2617 Lisp_Object object;
2619 struct buffer *old = current_buffer;
2620 Fprinc (object, Qnil);
2621 set_buffer_internal (XBUFFER (Vstandard_output));
2622 call0 (intern ("help-mode"));
2623 set_buffer_internal (old);
2624 return Qnil;
2627 /* Copy out or in the info on where C-g should throw to.
2628 This is used when running Lisp code from within get_char,
2629 in case get_char is called recursively.
2630 See read_process_output. */
2632 static void
2633 save_getcjmp (temp)
2634 jmp_buf temp;
2636 bcopy (getcjmp, temp, sizeof getcjmp);
2639 static void
2640 restore_getcjmp (temp)
2641 jmp_buf temp;
2643 bcopy (temp, getcjmp, sizeof getcjmp);
2646 #ifdef HAVE_MOUSE
2648 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
2649 of this function. */
2651 static Lisp_Object
2652 tracking_off (old_value)
2653 Lisp_Object old_value;
2655 do_mouse_tracking = old_value;
2656 if (NILP (old_value))
2658 /* Redisplay may have been preempted because there was input
2659 available, and it assumes it will be called again after the
2660 input has been processed. If the only input available was
2661 the sort that we have just disabled, then we need to call
2662 redisplay. */
2663 if (!readable_events (1))
2665 redisplay_preserve_echo_area ();
2666 get_input_pending (&input_pending, 1);
2671 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
2672 "Evaluate BODY with mouse movement events enabled.\n\
2673 Within a `track-mouse' form, mouse motion generates input events that\n\
2674 you can read with `read-event'.\n\
2675 Normally, mouse motion is ignored.")
2676 (args)
2677 Lisp_Object args;
2679 int count = specpdl_ptr - specpdl;
2680 Lisp_Object val;
2682 record_unwind_protect (tracking_off, do_mouse_tracking);
2684 do_mouse_tracking = Qt;
2686 val = Fprogn (args);
2687 return unbind_to (count, val);
2690 /* If mouse has moved on some frame, return one of those frames.
2691 Return 0 otherwise. */
2693 static FRAME_PTR
2694 some_mouse_moved ()
2696 Lisp_Object tail, frame;
2698 FOR_EACH_FRAME (tail, frame)
2700 if (XFRAME (frame)->mouse_moved)
2701 return XFRAME (frame);
2704 return 0;
2707 #endif /* HAVE_MOUSE */
2709 /* Low level keyboard/mouse input.
2710 kbd_buffer_store_event places events in kbd_buffer, and
2711 kbd_buffer_get_event retrieves them. */
2713 /* Return true iff there are any events in the queue that read-char
2714 would return. If this returns false, a read-char would block. */
2715 static int
2716 readable_events (do_timers_now)
2717 int do_timers_now;
2719 if (do_timers_now)
2720 timer_check (do_timers_now);
2722 if (kbd_fetch_ptr != kbd_store_ptr)
2723 return 1;
2724 #ifdef HAVE_MOUSE
2725 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2726 return 1;
2727 #endif
2728 if (single_kboard)
2730 if (current_kboard->kbd_queue_has_data)
2731 return 1;
2733 else
2735 KBOARD *kb;
2736 for (kb = all_kboards; kb; kb = kb->next_kboard)
2737 if (kb->kbd_queue_has_data)
2738 return 1;
2740 return 0;
2743 /* Set this for debugging, to have a way to get out */
2744 int stop_character;
2746 #ifdef MULTI_KBOARD
2747 static KBOARD *
2748 event_to_kboard (event)
2749 struct input_event *event;
2751 Lisp_Object frame;
2752 frame = event->frame_or_window;
2753 if (CONSP (frame))
2754 frame = XCONS (frame)->car;
2755 else if (WINDOWP (frame))
2756 frame = WINDOW_FRAME (XWINDOW (frame));
2758 /* There are still some events that don't set this field.
2759 For now, just ignore the problem.
2760 Also ignore dead frames here. */
2761 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
2762 return 0;
2763 else
2764 return FRAME_KBOARD (XFRAME (frame));
2766 #endif
2768 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
2770 void
2771 kbd_buffer_store_event (event)
2772 register struct input_event *event;
2774 if (event->kind == no_event)
2775 abort ();
2777 if (event->kind == ascii_keystroke)
2779 register int c = event->code & 0377;
2781 if (event->modifiers & ctrl_modifier)
2782 c = make_ctrl_char (c);
2784 c |= (event->modifiers
2785 & (meta_modifier | alt_modifier
2786 | hyper_modifier | super_modifier));
2788 if (c == quit_char)
2790 extern SIGTYPE interrupt_signal ();
2791 #ifdef MULTI_KBOARD
2792 KBOARD *kb;
2793 struct input_event *sp;
2795 if (single_kboard
2796 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
2797 kb != current_kboard))
2799 kb->kbd_queue
2800 = Fcons (make_lispy_switch_frame (event->frame_or_window),
2801 Fcons (make_number (c), Qnil));
2802 kb->kbd_queue_has_data = 1;
2803 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
2805 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
2806 sp = kbd_buffer;
2808 if (event_to_kboard (sp) == kb)
2810 sp->kind = no_event;
2811 sp->frame_or_window = Qnil;
2814 return;
2816 #endif
2818 /* If this results in a quit_char being returned to Emacs as
2819 input, set Vlast_event_frame properly. If this doesn't
2820 get returned to Emacs as an event, the next event read
2821 will set Vlast_event_frame again, so this is safe to do. */
2823 Lisp_Object focus;
2825 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
2826 if (NILP (focus))
2827 focus = event->frame_or_window;
2828 internal_last_event_frame = focus;
2829 Vlast_event_frame = focus;
2832 last_event_timestamp = event->timestamp;
2833 interrupt_signal ();
2834 return;
2837 if (c && c == stop_character)
2839 sys_suspend ();
2840 return;
2843 /* Don't insert two buffer_switch_event's in a row.
2844 Just ignore the second one. */
2845 else if (event->kind == buffer_switch_event
2846 && kbd_fetch_ptr != kbd_store_ptr
2847 && kbd_store_ptr->kind == buffer_switch_event)
2848 return;
2850 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
2851 kbd_store_ptr = kbd_buffer;
2853 /* Don't let the very last slot in the buffer become full,
2854 since that would make the two pointers equal,
2855 and that is indistinguishable from an empty buffer.
2856 Discard the event if it would fill the last slot. */
2857 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
2859 volatile struct input_event *sp = kbd_store_ptr;
2860 sp->kind = event->kind;
2861 if (event->kind == selection_request_event)
2863 /* We must not use the ordinary copying code for this case,
2864 since `part' is an enum and copying it might not copy enough
2865 in this case. */
2866 bcopy (event, (char *) sp, sizeof (*event));
2868 else
2870 sp->code = event->code;
2871 sp->part = event->part;
2872 sp->frame_or_window = event->frame_or_window;
2873 sp->modifiers = event->modifiers;
2874 sp->x = event->x;
2875 sp->y = event->y;
2876 sp->timestamp = event->timestamp;
2878 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
2879 - kbd_buffer]
2880 = event->frame_or_window);
2882 kbd_store_ptr++;
2886 /* Discard any mouse events in the event buffer by setting them to
2887 no_event. */
2888 void
2889 discard_mouse_events ()
2891 struct input_event *sp;
2892 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
2894 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
2895 sp = kbd_buffer;
2897 if (sp->kind == mouse_click
2898 #ifdef WINDOWSNT
2899 || sp->kind == w32_scroll_bar_click
2900 #endif
2901 || sp->kind == scroll_bar_click)
2903 sp->kind = no_event;
2908 /* Read one event from the event buffer, waiting if necessary.
2909 The value is a Lisp object representing the event.
2910 The value is nil for an event that should be ignored,
2911 or that was handled here.
2912 We always read and discard one event. */
2914 static Lisp_Object
2915 kbd_buffer_get_event (kbp, used_mouse_menu)
2916 KBOARD **kbp;
2917 int *used_mouse_menu;
2919 register int c;
2920 Lisp_Object obj;
2921 EMACS_TIME next_timer_delay;
2923 if (noninteractive)
2925 c = getchar ();
2926 XSETINT (obj, c);
2927 *kbp = current_kboard;
2928 return obj;
2931 /* Wait until there is input available. */
2932 for (;;)
2934 if (kbd_fetch_ptr != kbd_store_ptr)
2935 break;
2936 #ifdef HAVE_MOUSE
2937 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2938 break;
2939 #endif
2941 /* If the quit flag is set, then read_char will return
2942 quit_char, so that counts as "available input." */
2943 if (!NILP (Vquit_flag))
2944 quit_throw_to_read_char ();
2946 /* One way or another, wait until input is available; then, if
2947 interrupt handlers have not read it, read it now. */
2949 #ifdef OLDVMS
2950 wait_for_kbd_input ();
2951 #else
2952 /* Note SIGIO has been undef'd if FIONREAD is missing. */
2953 #ifdef SIGIO
2954 gobble_input (0);
2955 #endif /* SIGIO */
2956 if (kbd_fetch_ptr != kbd_store_ptr)
2957 break;
2958 #ifdef HAVE_MOUSE
2959 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2960 break;
2961 #endif
2963 Lisp_Object minus_one;
2965 XSETINT (minus_one, -1);
2966 wait_reading_process_input (0, 0, minus_one, 1);
2968 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
2969 /* Pass 1 for EXPECT since we just waited to have input. */
2970 read_avail_input (1);
2972 #endif /* not VMS */
2975 if (CONSP (Vunread_command_events))
2977 Lisp_Object first;
2978 first = XCONS (Vunread_command_events)->car;
2979 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
2980 *kbp = current_kboard;
2981 return first;
2984 /* At this point, we know that there is a readable event available
2985 somewhere. If the event queue is empty, then there must be a
2986 mouse movement enabled and available. */
2987 if (kbd_fetch_ptr != kbd_store_ptr)
2989 struct input_event *event;
2991 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2992 ? kbd_fetch_ptr
2993 : kbd_buffer);
2995 last_event_timestamp = event->timestamp;
2997 #ifdef MULTI_KBOARD
2998 *kbp = event_to_kboard (event);
2999 if (*kbp == 0)
3000 *kbp = current_kboard; /* Better than returning null ptr? */
3001 #else
3002 *kbp = &the_only_kboard;
3003 #endif
3005 obj = Qnil;
3007 /* These two kinds of events get special handling
3008 and don't actually appear to the command loop.
3009 We return nil for them. */
3010 if (event->kind == selection_request_event)
3012 #ifdef HAVE_X11
3013 struct input_event copy;
3015 /* Remove it from the buffer before processing it,
3016 since otherwise swallow_events will see it
3017 and process it again. */
3018 copy = *event;
3019 kbd_fetch_ptr = event + 1;
3020 input_pending = readable_events (0);
3021 x_handle_selection_request (&copy);
3022 #else
3023 /* We're getting selection request events, but we don't have
3024 a window system. */
3025 abort ();
3026 #endif
3029 else if (event->kind == selection_clear_event)
3031 #ifdef HAVE_X11
3032 struct input_event copy;
3034 /* Remove it from the buffer before processing it. */
3035 copy = *event;
3036 kbd_fetch_ptr = event + 1;
3037 input_pending = readable_events (0);
3038 x_handle_selection_clear (&copy);
3039 #else
3040 /* We're getting selection request events, but we don't have
3041 a window system. */
3042 abort ();
3043 #endif
3045 #if defined (HAVE_X11) || defined (HAVE_NTGUI)
3046 else if (event->kind == delete_window_event)
3048 /* Make an event (delete-frame (FRAME)). */
3049 obj = Fcons (event->frame_or_window, Qnil);
3050 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
3051 kbd_fetch_ptr = event + 1;
3053 else if (event->kind == iconify_event)
3055 /* Make an event (iconify-frame (FRAME)). */
3056 obj = Fcons (event->frame_or_window, Qnil);
3057 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
3058 kbd_fetch_ptr = event + 1;
3060 else if (event->kind == deiconify_event)
3062 /* Make an event (make-frame-visible (FRAME)). */
3063 obj = Fcons (event->frame_or_window, Qnil);
3064 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
3065 kbd_fetch_ptr = event + 1;
3067 #endif
3068 else if (event->kind == buffer_switch_event)
3070 /* The value doesn't matter here; only the type is tested. */
3071 XSETBUFFER (obj, current_buffer);
3072 kbd_fetch_ptr = event + 1;
3074 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
3075 else if (event->kind == menu_bar_activate_event)
3077 kbd_fetch_ptr = event + 1;
3078 input_pending = readable_events (0);
3079 if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
3080 x_activate_menubar (XFRAME (event->frame_or_window));
3082 #endif
3083 #ifdef WINDOWSNT
3084 else if (event->kind == language_change_event)
3086 /* Make an event (language-change (FRAME CHARSET LCID)). */
3087 obj = Fcons (event->modifiers, Qnil);
3088 obj = Fcons (event->code, Qnil);
3089 obj = Fcons (event->frame_or_window, obj);
3090 obj = Fcons (Qlanguage_change, Fcons (obj, Qnil));
3091 kbd_fetch_ptr = event + 1;
3093 #endif
3094 /* Just discard these, by returning nil.
3095 With MULTI_KBOARD, these events are used as placeholders
3096 when we need to randomly delete events from the queue.
3097 (They shouldn't otherwise be found in the buffer,
3098 but on some machines it appears they do show up
3099 even without MULTI_KBOARD.) */
3100 /* On Windows NT/9X, no_event is used to delete extraneous
3101 mouse events during a popup-menu call. */
3102 else if (event->kind == no_event)
3103 kbd_fetch_ptr = event + 1;
3105 /* If this event is on a different frame, return a switch-frame this
3106 time, and leave the event in the queue for next time. */
3107 else
3109 Lisp_Object frame;
3110 Lisp_Object focus;
3112 frame = event->frame_or_window;
3113 if (CONSP (frame))
3114 frame = XCONS (frame)->car;
3115 else if (WINDOWP (frame))
3116 frame = WINDOW_FRAME (XWINDOW (frame));
3118 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
3119 if (! NILP (focus))
3120 frame = focus;
3122 if (! EQ (frame, internal_last_event_frame)
3123 && XFRAME (frame) != selected_frame)
3124 obj = make_lispy_switch_frame (frame);
3125 internal_last_event_frame = frame;
3127 /* If we didn't decide to make a switch-frame event, go ahead
3128 and build a real event from the queue entry. */
3130 if (NILP (obj))
3132 obj = make_lispy_event (event);
3133 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
3134 /* If this was a menu selection, then set the flag to inhibit
3135 writing to last_nonmenu_event. Don't do this if the event
3136 we're returning is (menu-bar), though; that indicates the
3137 beginning of the menu sequence, and we might as well leave
3138 that as the `event with parameters' for this selection. */
3139 if (event->kind == menu_bar_event
3140 && !(CONSP (obj) && EQ (XCONS (obj)->car, Qmenu_bar))
3141 && used_mouse_menu)
3142 *used_mouse_menu = 1;
3143 #endif
3145 /* Wipe out this event, to catch bugs. */
3146 event->kind = no_event;
3147 XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer] = Qnil;
3149 kbd_fetch_ptr = event + 1;
3153 #ifdef HAVE_MOUSE
3154 /* Try generating a mouse motion event. */
3155 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3157 FRAME_PTR f = some_mouse_moved ();
3158 Lisp_Object bar_window;
3159 enum scroll_bar_part part;
3160 Lisp_Object x, y;
3161 unsigned long time;
3163 *kbp = current_kboard;
3164 /* Note that this uses F to determine which display to look at.
3165 If there is no valid info, it does not store anything
3166 so x remains nil. */
3167 x = Qnil;
3168 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
3170 obj = Qnil;
3172 /* Decide if we should generate a switch-frame event. Don't
3173 generate switch-frame events for motion outside of all Emacs
3174 frames. */
3175 if (!NILP (x) && f)
3177 Lisp_Object frame;
3179 frame = FRAME_FOCUS_FRAME (f);
3180 if (NILP (frame))
3181 XSETFRAME (frame, f);
3183 if (! EQ (frame, internal_last_event_frame)
3184 && XFRAME (frame) != selected_frame)
3185 obj = make_lispy_switch_frame (frame);
3186 internal_last_event_frame = frame;
3189 /* If we didn't decide to make a switch-frame event, go ahead and
3190 return a mouse-motion event. */
3191 if (!NILP (x) && NILP (obj))
3192 obj = make_lispy_movement (f, bar_window, part, x, y, time);
3194 #endif /* HAVE_MOUSE */
3195 else
3196 /* We were promised by the above while loop that there was
3197 something for us to read! */
3198 abort ();
3200 input_pending = readable_events (0);
3202 Vlast_event_frame = internal_last_event_frame;
3204 return (obj);
3207 /* Process any events that are not user-visible,
3208 then return, without reading any user-visible events. */
3210 void
3211 swallow_events (do_display)
3212 int do_display;
3214 int old_timers_run;
3216 while (kbd_fetch_ptr != kbd_store_ptr)
3218 struct input_event *event;
3220 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3221 ? kbd_fetch_ptr
3222 : kbd_buffer);
3224 last_event_timestamp = event->timestamp;
3226 /* These two kinds of events get special handling
3227 and don't actually appear to the command loop. */
3228 if (event->kind == selection_request_event)
3230 #ifdef HAVE_X11
3231 struct input_event copy;
3233 /* Remove it from the buffer before processing it,
3234 since otherwise swallow_events called recursively could see it
3235 and process it again. */
3236 copy = *event;
3237 kbd_fetch_ptr = event + 1;
3238 input_pending = readable_events (0);
3239 x_handle_selection_request (&copy);
3240 #else
3241 /* We're getting selection request events, but we don't have
3242 a window system. */
3243 abort ();
3244 #endif
3247 else if (event->kind == selection_clear_event)
3249 #ifdef HAVE_X11
3250 struct input_event copy;
3252 /* Remove it from the buffer before processing it, */
3253 copy = *event;
3255 kbd_fetch_ptr = event + 1;
3256 input_pending = readable_events (0);
3257 x_handle_selection_clear (&copy);
3258 #else
3259 /* We're getting selection request events, but we don't have
3260 a window system. */
3261 abort ();
3262 #endif
3264 else
3265 break;
3268 old_timers_run = timers_run;
3269 get_input_pending (&input_pending, 1);
3271 if (timers_run != old_timers_run && do_display)
3272 redisplay_preserve_echo_area ();
3275 static EMACS_TIME timer_idleness_start_time;
3277 /* Record the start of when Emacs is idle,
3278 for the sake of running idle-time timers. */
3280 void
3281 timer_start_idle ()
3283 Lisp_Object timers;
3285 /* If we are already in the idle state, do nothing. */
3286 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3287 return;
3289 EMACS_GET_TIME (timer_idleness_start_time);
3291 /* Mark all idle-time timers as once again candidates for running. */
3292 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCONS (timers)->cdr)
3294 Lisp_Object timer;
3296 timer = XCONS (timers)->car;
3298 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3299 continue;
3300 XVECTOR (timer)->contents[0] = Qnil;
3304 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
3306 void
3307 timer_stop_idle ()
3309 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
3312 /* This is only for debugging. */
3313 struct input_event last_timer_event;
3315 /* Check whether a timer has fired. To prevent larger problems we simply
3316 disregard elements that are not proper timers. Do not make a circular
3317 timer list for the time being.
3319 Returns the number of seconds to wait until the next timer fires. If a
3320 timer is triggering now, return zero seconds.
3321 If no timer is active, return -1 seconds.
3323 If a timer is ripe, we run it, with quitting turned off.
3325 DO_IT_NOW is now ignored. It used to mean that we should
3326 run the timer directly instead of queueing a timer-event.
3327 Now we always run timers directly. */
3329 EMACS_TIME
3330 timer_check (do_it_now)
3331 int do_it_now;
3333 EMACS_TIME nexttime;
3334 EMACS_TIME now, idleness_now;
3335 Lisp_Object timers, idle_timers, chosen_timer;
3336 struct gcpro gcpro1, gcpro2, gcpro3;
3338 EMACS_SET_SECS (nexttime, -1);
3339 EMACS_SET_USECS (nexttime, -1);
3341 /* Always consider the ordinary timers. */
3342 timers = Vtimer_list;
3343 /* Consider the idle timers only if Emacs is idle. */
3344 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3345 idle_timers = Vtimer_idle_list;
3346 else
3347 idle_timers = Qnil;
3348 chosen_timer = Qnil;
3349 GCPRO3 (timers, idle_timers, chosen_timer);
3351 if (CONSP (timers) || CONSP (idle_timers))
3353 EMACS_GET_TIME (now);
3354 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3355 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
3358 while (CONSP (timers) || CONSP (idle_timers))
3360 int triggertime = EMACS_SECS (now);
3361 Lisp_Object *vector;
3362 Lisp_Object timer, idle_timer;
3363 EMACS_TIME timer_time, idle_timer_time;
3364 EMACS_TIME difference, timer_difference, idle_timer_difference;
3366 /* Skip past invalid timers and timers already handled. */
3367 if (!NILP (timers))
3369 timer = XCONS (timers)->car;
3370 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3372 timers = XCONS (timers)->cdr;
3373 continue;
3375 vector = XVECTOR (timer)->contents;
3377 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3378 || !INTEGERP (vector[3])
3379 || ! NILP (vector[0]))
3381 timers = XCONS (timers)->cdr;
3382 continue;
3385 if (!NILP (idle_timers))
3387 timer = XCONS (idle_timers)->car;
3388 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3390 idle_timers = XCONS (idle_timers)->cdr;
3391 continue;
3393 vector = XVECTOR (timer)->contents;
3395 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3396 || !INTEGERP (vector[3])
3397 || ! NILP (vector[0]))
3399 idle_timers = XCONS (idle_timers)->cdr;
3400 continue;
3404 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
3405 based on the next ordinary timer.
3406 TIMER_DIFFERENCE is the distance in time from NOW to when
3407 this timer becomes ripe (negative if it's already ripe). */
3408 if (!NILP (timers))
3410 timer = XCONS (timers)->car;
3411 vector = XVECTOR (timer)->contents;
3412 EMACS_SET_SECS (timer_time,
3413 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3414 EMACS_SET_USECS (timer_time, XINT (vector[3]));
3415 EMACS_SUB_TIME (timer_difference, timer_time, now);
3418 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
3419 based on the next idle timer. */
3420 if (!NILP (idle_timers))
3422 idle_timer = XCONS (idle_timers)->car;
3423 vector = XVECTOR (idle_timer)->contents;
3424 EMACS_SET_SECS (idle_timer_time,
3425 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3426 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
3427 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
3430 /* Decide which timer is the next timer,
3431 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
3432 Also step down the list where we found that timer. */
3434 if (! NILP (timers) && ! NILP (idle_timers))
3436 EMACS_TIME temp;
3437 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
3438 if (EMACS_TIME_NEG_P (temp))
3440 chosen_timer = timer;
3441 timers = XCONS (timers)->cdr;
3442 difference = timer_difference;
3444 else
3446 chosen_timer = idle_timer;
3447 idle_timers = XCONS (idle_timers)->cdr;
3448 difference = idle_timer_difference;
3451 else if (! NILP (timers))
3453 chosen_timer = timer;
3454 timers = XCONS (timers)->cdr;
3455 difference = timer_difference;
3457 else
3459 chosen_timer = idle_timer;
3460 idle_timers = XCONS (idle_timers)->cdr;
3461 difference = idle_timer_difference;
3463 vector = XVECTOR (chosen_timer)->contents;
3465 /* If timer is rupe, run it if it hasn't been run. */
3466 if (EMACS_TIME_NEG_P (difference)
3467 || (EMACS_SECS (difference) == 0
3468 && EMACS_USECS (difference) == 0))
3470 if (NILP (vector[0]))
3472 Lisp_Object tem;
3473 int was_locked = single_kboard;
3474 int count = specpdl_ptr - specpdl;
3476 /* Mark the timer as triggered to prevent problems if the lisp
3477 code fails to reschedule it right. */
3478 vector[0] = Qt;
3480 specbind (Qinhibit_quit, Qt);
3482 call1 (Qtimer_event_handler, chosen_timer);
3483 timers_run++;
3485 unbind_to (count, Qnil);
3487 /* Resume allowing input from any kboard, if that was true before. */
3488 if (!was_locked)
3489 any_kboard_state ();
3491 /* Since we have handled the event,
3492 we don't need to tell the caller to wake up and do it. */
3495 else
3496 /* When we encounter a timer that is still waiting,
3497 return the amount of time to wait before it is ripe. */
3499 UNGCPRO;
3500 return difference;
3504 /* No timers are pending in the future. */
3505 /* Return 0 if we generated an event, and -1 if not. */
3506 UNGCPRO;
3507 return nexttime;
3510 /* Caches for modify_event_symbol. */
3511 static Lisp_Object accent_key_syms;
3512 static Lisp_Object func_key_syms;
3513 static Lisp_Object mouse_syms;
3514 #ifdef WINDOWSNT
3515 static Lisp_Object mouse_wheel_syms;
3516 #endif
3517 static Lisp_Object drag_n_drop_syms;
3519 /* This is a list of keysym codes for special "accent" characters.
3520 It parallels lispy_accent_keys. */
3522 static int lispy_accent_codes[] =
3524 #ifdef XK_dead_circumflex
3525 XK_dead_circumflex,
3526 #else
3528 #endif
3529 #ifdef XK_dead_grave
3530 XK_dead_grave,
3531 #else
3533 #endif
3534 #ifdef XK_dead_tilde
3535 XK_dead_tilde,
3536 #else
3538 #endif
3539 #ifdef XK_dead_diaeresis
3540 XK_dead_diaeresis,
3541 #else
3543 #endif
3544 #ifdef XK_dead_macron
3545 XK_dead_macron,
3546 #else
3548 #endif
3549 #ifdef XK_dead_degree
3550 XK_dead_degree,
3551 #else
3553 #endif
3554 #ifdef XK_dead_acute
3555 XK_dead_acute,
3556 #else
3558 #endif
3559 #ifdef XK_dead_cedilla
3560 XK_dead_cedilla,
3561 #else
3563 #endif
3564 #ifdef XK_dead_breve
3565 XK_dead_breve,
3566 #else
3568 #endif
3569 #ifdef XK_dead_ogonek
3570 XK_dead_ogonek,
3571 #else
3573 #endif
3574 #ifdef XK_dead_caron
3575 XK_dead_caron,
3576 #else
3578 #endif
3579 #ifdef XK_dead_doubleacute
3580 XK_dead_doubleacute,
3581 #else
3583 #endif
3584 #ifdef XK_dead_abovedot
3585 XK_dead_abovedot,
3586 #else
3588 #endif
3591 /* This is a list of Lisp names for special "accent" characters.
3592 It parallels lispy_accent_codes. */
3594 static char *lispy_accent_keys[] =
3596 "dead-circumflex",
3597 "dead-grave",
3598 "dead-tilde",
3599 "dead-diaeresis",
3600 "dead-macron",
3601 "dead-degree",
3602 "dead-acute",
3603 "dead-cedilla",
3604 "dead-breve",
3605 "dead-ogonek",
3606 "dead-caron",
3607 "dead-doubleacute",
3608 "dead-abovedot",
3611 #ifdef HAVE_NTGUI
3612 #define FUNCTION_KEY_OFFSET 0x0
3614 char *lispy_function_keys[] =
3616 0, /* 0 */
3618 0, /* VK_LBUTTON 0x01 */
3619 0, /* VK_RBUTTON 0x02 */
3620 "cancel", /* VK_CANCEL 0x03 */
3621 0, /* VK_MBUTTON 0x04 */
3623 0, 0, 0, /* 0x05 .. 0x07 */
3625 "backspace", /* VK_BACK 0x08 */
3626 "tab", /* VK_TAB 0x09 */
3628 0, 0, /* 0x0A .. 0x0B */
3630 "clear", /* VK_CLEAR 0x0C */
3631 "return", /* VK_RETURN 0x0D */
3633 0, 0, /* 0x0E .. 0x0F */
3635 0, /* VK_SHIFT 0x10 */
3636 0, /* VK_CONTROL 0x11 */
3637 0, /* VK_MENU 0x12 */
3638 "pause", /* VK_PAUSE 0x13 */
3639 "capslock", /* VK_CAPITAL 0x14 */
3641 0, 0, 0, 0, 0, 0, /* 0x15 .. 0x1A */
3643 "escape", /* VK_ESCAPE 0x1B */
3645 0, 0, 0, 0, /* 0x1C .. 0x1F */
3647 0, /* VK_SPACE 0x20 */
3648 "prior", /* VK_PRIOR 0x21 */
3649 "next", /* VK_NEXT 0x22 */
3650 "end", /* VK_END 0x23 */
3651 "home", /* VK_HOME 0x24 */
3652 "left", /* VK_LEFT 0x25 */
3653 "up", /* VK_UP 0x26 */
3654 "right", /* VK_RIGHT 0x27 */
3655 "down", /* VK_DOWN 0x28 */
3656 "select", /* VK_SELECT 0x29 */
3657 "print", /* VK_PRINT 0x2A */
3658 "execute", /* VK_EXECUTE 0x2B */
3659 "snapshot", /* VK_SNAPSHOT 0x2C */
3660 "insert", /* VK_INSERT 0x2D */
3661 "delete", /* VK_DELETE 0x2E */
3662 "help", /* VK_HELP 0x2F */
3664 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
3666 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3668 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
3670 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
3672 0, 0, 0, 0, 0, 0, 0, 0, 0,
3673 0, 0, 0, 0, 0, 0, 0, 0, 0,
3674 0, 0, 0, 0, 0, 0, 0, 0,
3676 "lwindow", /* VK_LWIN 0x5B */
3677 "rwindow", /* VK_RWIN 0x5C */
3678 "apps", /* VK_APPS 0x5D */
3680 0, 0, /* 0x5E .. 0x5F */
3682 "kp-0", /* VK_NUMPAD0 0x60 */
3683 "kp-1", /* VK_NUMPAD1 0x61 */
3684 "kp-2", /* VK_NUMPAD2 0x62 */
3685 "kp-3", /* VK_NUMPAD3 0x63 */
3686 "kp-4", /* VK_NUMPAD4 0x64 */
3687 "kp-5", /* VK_NUMPAD5 0x65 */
3688 "kp-6", /* VK_NUMPAD6 0x66 */
3689 "kp-7", /* VK_NUMPAD7 0x67 */
3690 "kp-8", /* VK_NUMPAD8 0x68 */
3691 "kp-9", /* VK_NUMPAD9 0x69 */
3692 "kp-multiply", /* VK_MULTIPLY 0x6A */
3693 "kp-add", /* VK_ADD 0x6B */
3694 "kp-separator", /* VK_SEPARATOR 0x6C */
3695 "kp-subtract", /* VK_SUBTRACT 0x6D */
3696 "kp-decimal", /* VK_DECIMAL 0x6E */
3697 "kp-divide", /* VK_DIVIDE 0x6F */
3698 "f1", /* VK_F1 0x70 */
3699 "f2", /* VK_F2 0x71 */
3700 "f3", /* VK_F3 0x72 */
3701 "f4", /* VK_F4 0x73 */
3702 "f5", /* VK_F5 0x74 */
3703 "f6", /* VK_F6 0x75 */
3704 "f7", /* VK_F7 0x76 */
3705 "f8", /* VK_F8 0x77 */
3706 "f9", /* VK_F9 0x78 */
3707 "f10", /* VK_F10 0x79 */
3708 "f11", /* VK_F11 0x7A */
3709 "f12", /* VK_F12 0x7B */
3710 "f13", /* VK_F13 0x7C */
3711 "f14", /* VK_F14 0x7D */
3712 "f15", /* VK_F15 0x7E */
3713 "f16", /* VK_F16 0x7F */
3714 "f17", /* VK_F17 0x80 */
3715 "f18", /* VK_F18 0x81 */
3716 "f19", /* VK_F19 0x82 */
3717 "f20", /* VK_F20 0x83 */
3718 "f21", /* VK_F21 0x84 */
3719 "f22", /* VK_F22 0x85 */
3720 "f23", /* VK_F23 0x86 */
3721 "f24", /* VK_F24 0x87 */
3723 0, 0, 0, 0, /* 0x88 .. 0x8B */
3724 0, 0, 0, 0, /* 0x8C .. 0x8F */
3726 "kp-numlock", /* VK_NUMLOCK 0x90 */
3727 "scroll", /* VK_SCROLL 0x91 */
3729 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
3730 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
3731 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
3732 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
3733 "kp-end", /* VK_NUMPAD_END 0x96 */
3734 "kp-home", /* VK_NUMPAD_HOME 0x97 */
3735 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
3736 "kp-up", /* VK_NUMPAD_UP 0x99 */
3737 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
3738 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
3739 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
3740 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
3742 0, 0, /* 0x9E .. 0x9F */
3745 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
3746 * Used only as parameters to GetAsyncKeyState and GetKeyState.
3747 * No other API or message will distinguish left and right keys this way.
3749 /* 0xA0 .. 0xEF */
3751 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3752 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3753 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3754 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3755 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3757 /* 0xF0 .. 0xF5 */
3759 0, 0, 0, 0, 0, 0,
3761 "attn", /* VK_ATTN 0xF6 */
3762 "crsel", /* VK_CRSEL 0xF7 */
3763 "exsel", /* VK_EXSEL 0xF8 */
3764 "ereof", /* VK_EREOF 0xF9 */
3765 "play", /* VK_PLAY 0xFA */
3766 "zoom", /* VK_ZOOM 0xFB */
3767 "noname", /* VK_NONAME 0xFC */
3768 "pa1", /* VK_PA1 0xFD */
3769 "oem_clear", /* VK_OEM_CLEAR 0xFE */
3770 0 /* 0xFF */
3773 #else /* not HAVE_NTGUI */
3775 #ifdef XK_kana_A
3776 static char *lispy_kana_keys[] =
3778 /* X Keysym value */
3779 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x400 .. 0x40f */
3780 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x410 .. 0x41f */
3781 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x420 .. 0x42f */
3782 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x430 .. 0x43f */
3783 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x440 .. 0x44f */
3784 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x450 .. 0x45f */
3785 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x460 .. 0x46f */
3786 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0,
3787 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x480 .. 0x48f */
3788 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x490 .. 0x49f */
3789 0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket",
3790 "kana-comma", "kana-conjunctive", "kana-WO", "kana-a",
3791 "kana-i", "kana-u", "kana-e", "kana-o",
3792 "kana-ya", "kana-yu", "kana-yo", "kana-tsu",
3793 "prolongedsound", "kana-A", "kana-I", "kana-U",
3794 "kana-E", "kana-O", "kana-KA", "kana-KI",
3795 "kana-KU", "kana-KE", "kana-KO", "kana-SA",
3796 "kana-SHI", "kana-SU", "kana-SE", "kana-SO",
3797 "kana-TA", "kana-CHI", "kana-TSU", "kana-TE",
3798 "kana-TO", "kana-NA", "kana-NI", "kana-NU",
3799 "kana-NE", "kana-NO", "kana-HA", "kana-HI",
3800 "kana-FU", "kana-HE", "kana-HO", "kana-MA",
3801 "kana-MI", "kana-MU", "kana-ME", "kana-MO",
3802 "kana-YA", "kana-YU", "kana-YO", "kana-RA",
3803 "kana-RI", "kana-RU", "kana-RE", "kana-RO",
3804 "kana-WA", "kana-N", "voicedsound", "semivoicedsound",
3805 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4e0 .. 0x4ef */
3806 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f0 .. 0x4ff */
3808 #endif /* XK_kana_A */
3810 #define FUNCTION_KEY_OFFSET 0xff00
3812 /* You'll notice that this table is arranged to be conveniently
3813 indexed by X Windows keysym values. */
3814 static char *lispy_function_keys[] =
3816 /* X Keysym value */
3818 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00...0f */
3819 "backspace", "tab", "linefeed", "clear",
3820 0, "return", 0, 0,
3821 0, 0, 0, "pause", /* 0xff10...1f */
3822 0, 0, 0, 0, 0, 0, 0, "escape",
3823 0, 0, 0, 0,
3824 0, "kanji", "muhenkan", "henkan", /* 0xff20...2f */
3825 "romaji", "hiragana", "katakana", "hiragana-katakana",
3826 "zenkaku", "hankaku", "zenkaku-hankaku", "touroku",
3827 "massyo", "kana-lock", "kana-shift", "eisu-shift",
3828 "eisu-toggle", /* 0xff30...3f */
3829 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3830 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
3832 "home", "left", "up", "right", /* 0xff50 */ /* IsCursorKey */
3833 "down", "prior", "next", "end",
3834 "begin", 0, 0, 0, 0, 0, 0, 0,
3835 "select", /* 0xff60 */ /* IsMiscFunctionKey */
3836 "print",
3837 "execute",
3838 "insert",
3839 0, /* 0xff64 */
3840 "undo",
3841 "redo",
3842 "menu",
3843 "find",
3844 "cancel",
3845 "help",
3846 "break", /* 0xff6b */
3848 0, 0, 0, 0,
3849 0, 0, 0, 0, "backtab", 0, 0, 0, /* 0xff70... */
3850 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff78... */
3851 "kp-space", /* 0xff80 */ /* IsKeypadKey */
3852 0, 0, 0, 0, 0, 0, 0, 0,
3853 "kp-tab", /* 0xff89 */
3854 0, 0, 0,
3855 "kp-enter", /* 0xff8d */
3856 0, 0, 0,
3857 "kp-f1", /* 0xff91 */
3858 "kp-f2",
3859 "kp-f3",
3860 "kp-f4",
3861 "kp-home", /* 0xff95 */
3862 "kp-left",
3863 "kp-up",
3864 "kp-right",
3865 "kp-down",
3866 "kp-prior", /* kp-page-up */
3867 "kp-next", /* kp-page-down */
3868 "kp-end",
3869 "kp-begin",
3870 "kp-insert",
3871 "kp-delete",
3872 0, /* 0xffa0 */
3873 0, 0, 0, 0, 0, 0, 0, 0, 0,
3874 "kp-multiply", /* 0xffaa */
3875 "kp-add",
3876 "kp-separator",
3877 "kp-subtract",
3878 "kp-decimal",
3879 "kp-divide", /* 0xffaf */
3880 "kp-0", /* 0xffb0 */
3881 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
3882 0, /* 0xffba */
3883 0, 0,
3884 "kp-equal", /* 0xffbd */
3885 "f1", /* 0xffbe */ /* IsFunctionKey */
3886 "f2",
3887 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
3888 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
3889 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
3890 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
3891 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
3892 0, 0, 0, 0, 0, 0, 0, 0,
3893 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
3894 0, 0, 0, 0, 0, 0, 0, "delete"
3897 /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE. */
3898 #define ISO_FUNCTION_KEY_OFFSET 0xfe00
3900 static char *iso_lispy_function_keys[] =
3902 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe00 */
3903 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe08 */
3904 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe10 */
3905 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe18 */
3906 "iso-lefttab", /* 0xfe20 */
3907 "iso-move-line-up", "iso-move-line-down",
3908 "iso-partial-line-up", "iso-partial-line-down",
3909 "iso-partial-space-left", "iso-partial-space-right",
3910 "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */
3911 "iso-release-margin-left", "iso-release-margin-right",
3912 "iso-release-both-margins",
3913 "iso-fast-cursor-left", "iso-fast-cursor-right",
3914 "iso-fast-cursor-up", "iso-fast-cursor-down",
3915 "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */
3916 "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */
3919 #endif /* not HAVE_NTGUI */
3921 static char *lispy_mouse_names[] =
3923 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
3926 #ifdef WINDOWSNT
3927 /* mouse-wheel events are generated by the wheel on devices such as
3928 the MS Intellimouse. The wheel sits in between the left and right
3929 mouse buttons, and is typically used to scroll or zoom the window
3930 underneath the pointer. mouse-wheel events specify the object on
3931 which they operate, and a delta corresponding to the amount and
3932 direction that the wheel is rotated. Clicking the mouse-wheel
3933 generates a mouse-2 event. */
3934 static char *lispy_mouse_wheel_names[] =
3936 "mouse-wheel"
3939 #endif /* WINDOWSNT */
3941 /* drag-n-drop events are generated when a set of selected files are
3942 dragged from another application and dropped onto an Emacs window. */
3943 static char *lispy_drag_n_drop_names[] =
3945 "drag-n-drop"
3948 /* Scroll bar parts. */
3949 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
3950 Lisp_Object Qup, Qdown;
3952 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
3953 Lisp_Object *scroll_bar_parts[] = {
3954 &Qabove_handle, &Qhandle, &Qbelow_handle,
3955 &Qup, &Qdown,
3959 /* A vector, indexed by button number, giving the down-going location
3960 of currently depressed buttons, both scroll bar and non-scroll bar.
3962 The elements have the form
3963 (BUTTON-NUMBER MODIFIER-MASK . REST)
3964 where REST is the cdr of a position as it would be reported in the event.
3966 The make_lispy_event function stores positions here to tell the
3967 difference between click and drag events, and to store the starting
3968 location to be included in drag events. */
3970 static Lisp_Object button_down_location;
3972 /* Information about the most recent up-going button event: Which
3973 button, what location, and what time. */
3975 static int last_mouse_button;
3976 static int last_mouse_x;
3977 static int last_mouse_y;
3978 static unsigned long button_down_time;
3980 /* The maximum time between clicks to make a double-click,
3981 or Qnil to disable double-click detection,
3982 or Qt for no time limit. */
3983 Lisp_Object Vdouble_click_time;
3985 /* The number of clicks in this multiple-click. */
3987 int double_click_count;
3989 /* Given a struct input_event, build the lisp event which represents
3990 it. If EVENT is 0, build a mouse movement event from the mouse
3991 movement buffer, which should have a movement event in it.
3993 Note that events must be passed to this function in the order they
3994 are received; this function stores the location of button presses
3995 in order to build drag events when the button is released. */
3997 static Lisp_Object
3998 make_lispy_event (event)
3999 struct input_event *event;
4001 int i;
4003 switch (SWITCH_ENUM_CAST (event->kind))
4005 /* A simple keystroke. */
4006 case ascii_keystroke:
4008 Lisp_Object lispy_c;
4009 int c = event->code & 0377;
4010 /* Turn ASCII characters into control characters
4011 when proper. */
4012 if (event->modifiers & ctrl_modifier)
4013 c = make_ctrl_char (c);
4015 /* Add in the other modifier bits. We took care of ctrl_modifier
4016 just above, and the shift key was taken care of by the X code,
4017 and applied to control characters by make_ctrl_char. */
4018 c |= (event->modifiers
4019 & (meta_modifier | alt_modifier
4020 | hyper_modifier | super_modifier));
4021 /* Distinguish Shift-SPC from SPC. */
4022 if ((event->code & 0377) == 040
4023 && event->modifiers & shift_modifier)
4024 c |= shift_modifier;
4025 button_down_time = 0;
4026 XSETFASTINT (lispy_c, c);
4027 return lispy_c;
4030 /* A function key. The symbol may need to have modifier prefixes
4031 tacked onto it. */
4032 case non_ascii_keystroke:
4033 button_down_time = 0;
4035 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
4036 if (event->code == lispy_accent_codes[i])
4037 return modify_event_symbol (i,
4038 event->modifiers,
4039 Qfunction_key, Qnil,
4040 lispy_accent_keys, &accent_key_syms,
4041 (sizeof (lispy_accent_keys)
4042 / sizeof (lispy_accent_keys[0])));
4044 /* Handle system-specific keysyms. */
4045 if (event->code & (1 << 28))
4047 /* We need to use an alist rather than a vector as the cache
4048 since we can't make a vector long enuf. */
4049 if (NILP (current_kboard->system_key_syms))
4050 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
4051 return modify_event_symbol (event->code,
4052 event->modifiers,
4053 Qfunction_key,
4054 current_kboard->Vsystem_key_alist,
4055 0, &current_kboard->system_key_syms,
4056 (unsigned)-1);
4059 #ifdef XK_kana_A
4060 if (event->code >= 0x400 && event->code < 0x500)
4061 return modify_event_symbol (event->code - 0x400,
4062 event->modifiers & ~shift_modifier,
4063 Qfunction_key, Qnil,
4064 lispy_kana_keys, &func_key_syms,
4065 (sizeof (lispy_kana_keys)
4066 / sizeof (lispy_kana_keys[0])));
4067 #endif /* XK_kana_A */
4069 #ifdef ISO_FUNCTION_KEY_OFFSET
4070 if (event->code < FUNCTION_KEY_OFFSET
4071 && event->code >= ISO_FUNCTION_KEY_OFFSET)
4072 return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET,
4073 event->modifiers,
4074 Qfunction_key, Qnil,
4075 iso_lispy_function_keys, &func_key_syms,
4076 (sizeof (iso_lispy_function_keys)
4077 / sizeof (iso_lispy_function_keys[0])));
4078 else
4079 #endif
4080 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
4081 event->modifiers,
4082 Qfunction_key, Qnil,
4083 lispy_function_keys, &func_key_syms,
4084 (sizeof (lispy_function_keys)
4085 / sizeof (lispy_function_keys[0])));
4087 #ifdef HAVE_MOUSE
4088 /* A mouse click. Figure out where it is, decide whether it's
4089 a press, click or drag, and build the appropriate structure. */
4090 case mouse_click:
4091 case scroll_bar_click:
4093 int button = event->code;
4094 int is_double;
4095 Lisp_Object position;
4096 Lisp_Object *start_pos_ptr;
4097 Lisp_Object start_pos;
4099 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
4100 abort ();
4102 /* Build the position as appropriate for this mouse click. */
4103 if (event->kind == mouse_click)
4105 int part;
4106 FRAME_PTR f = XFRAME (event->frame_or_window);
4107 Lisp_Object window;
4108 Lisp_Object posn;
4109 int row, column;
4111 /* Ignore mouse events that were made on frame that
4112 have been deleted. */
4113 if (! FRAME_LIVE_P (f))
4114 return Qnil;
4116 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4117 &column, &row, NULL, 1);
4119 #ifndef USE_X_TOOLKIT
4120 /* In the non-toolkit version, clicks on the menu bar
4121 are ordinary button events in the event buffer.
4122 Distinguish them, and invoke the menu.
4124 (In the toolkit version, the toolkit handles the menu bar
4125 and Emacs doesn't know about it until after the user
4126 makes a selection.) */
4127 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
4128 && (event->modifiers & down_modifier))
4130 Lisp_Object items, item;
4131 int hpos;
4132 int i;
4134 #if 0
4135 /* Activate the menu bar on the down event. If the
4136 up event comes in before the menu code can deal with it,
4137 just ignore it. */
4138 if (! (event->modifiers & down_modifier))
4139 return Qnil;
4140 #endif
4142 item = Qnil;
4143 items = FRAME_MENU_BAR_ITEMS (f);
4144 for (i = 0; i < XVECTOR (items)->size; i += 4)
4146 Lisp_Object pos, string;
4147 string = XVECTOR (items)->contents[i + 1];
4148 pos = XVECTOR (items)->contents[i + 3];
4149 if (NILP (string))
4150 break;
4151 if (column >= XINT (pos)
4152 && column < XINT (pos) + XSTRING (string)->size)
4154 item = XVECTOR (items)->contents[i];
4155 break;
4159 position
4160 = Fcons (event->frame_or_window,
4161 Fcons (Qmenu_bar,
4162 Fcons (Fcons (event->x, event->y),
4163 Fcons (make_number (event->timestamp),
4164 Qnil))));
4166 return Fcons (item, Fcons (position, Qnil));
4168 #endif /* not USE_X_TOOLKIT */
4170 window = window_from_coordinates (f, column, row, &part);
4172 if (!WINDOWP (window))
4174 window = event->frame_or_window;
4175 posn = Qnil;
4177 else
4179 int pixcolumn, pixrow;
4180 column -= WINDOW_LEFT_MARGIN (XWINDOW (window));
4181 row -= XINT (XWINDOW (window)->top);
4182 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
4183 XSETINT (event->x, pixcolumn);
4184 XSETINT (event->y, pixrow);
4186 if (part == 1)
4187 posn = Qmode_line;
4188 else if (part == 2)
4189 posn = Qvertical_line;
4190 else
4191 XSETINT (posn,
4192 buffer_posn_from_coords (XWINDOW (window),
4193 column, row));
4196 position
4197 = Fcons (window,
4198 Fcons (posn,
4199 Fcons (Fcons (event->x, event->y),
4200 Fcons (make_number (event->timestamp),
4201 Qnil))));
4203 else
4205 Lisp_Object window;
4206 Lisp_Object portion_whole;
4207 Lisp_Object part;
4209 window = event->frame_or_window;
4210 portion_whole = Fcons (event->x, event->y);
4211 part = *scroll_bar_parts[(int) event->part];
4213 position
4214 = Fcons (window,
4215 Fcons (Qvertical_scroll_bar,
4216 Fcons (portion_whole,
4217 Fcons (make_number (event->timestamp),
4218 Fcons (part, Qnil)))));
4221 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
4223 start_pos = *start_pos_ptr;
4224 *start_pos_ptr = Qnil;
4226 is_double = (button == last_mouse_button
4227 && XINT (event->x) == last_mouse_x
4228 && XINT (event->y) == last_mouse_y
4229 && button_down_time != 0
4230 && (EQ (Vdouble_click_time, Qt)
4231 || (INTEGERP (Vdouble_click_time)
4232 && ((int)(event->timestamp - button_down_time)
4233 < XINT (Vdouble_click_time)))));
4234 last_mouse_button = button;
4235 last_mouse_x = XINT (event->x);
4236 last_mouse_y = XINT (event->y);
4238 /* If this is a button press, squirrel away the location, so
4239 we can decide later whether it was a click or a drag. */
4240 if (event->modifiers & down_modifier)
4242 if (is_double)
4244 double_click_count++;
4245 event->modifiers |= ((double_click_count > 2)
4246 ? triple_modifier
4247 : double_modifier);
4249 else
4250 double_click_count = 1;
4251 button_down_time = event->timestamp;
4252 *start_pos_ptr = Fcopy_alist (position);
4255 /* Now we're releasing a button - check the co-ordinates to
4256 see if this was a click or a drag. */
4257 else if (event->modifiers & up_modifier)
4259 /* If we did not see a down before this up,
4260 ignore the up. Probably this happened because
4261 the down event chose a menu item.
4262 It would be an annoyance to treat the release
4263 of the button that chose the menu item
4264 as a separate event. */
4266 if (!CONSP (start_pos))
4267 return Qnil;
4269 event->modifiers &= ~up_modifier;
4270 #if 0 /* Formerly we treated an up with no down as a click event. */
4271 if (!CONSP (start_pos))
4272 event->modifiers |= click_modifier;
4273 else
4274 #endif
4276 /* The third element of every position should be the (x,y)
4277 pair. */
4278 Lisp_Object down;
4280 down = Fnth (make_number (2), start_pos);
4281 if (EQ (event->x, XCONS (down)->car)
4282 && EQ (event->y, XCONS (down)->cdr))
4284 event->modifiers |= click_modifier;
4286 else
4288 button_down_time = 0;
4289 event->modifiers |= drag_modifier;
4291 /* Don't check is_double; treat this as multiple
4292 if the down-event was multiple. */
4293 if (double_click_count > 1)
4294 event->modifiers |= ((double_click_count > 2)
4295 ? triple_modifier
4296 : double_modifier);
4299 else
4300 /* Every mouse event should either have the down_modifier or
4301 the up_modifier set. */
4302 abort ();
4305 /* Get the symbol we should use for the mouse click. */
4306 Lisp_Object head;
4308 head = modify_event_symbol (button,
4309 event->modifiers,
4310 Qmouse_click, Qnil,
4311 lispy_mouse_names, &mouse_syms,
4312 (sizeof (lispy_mouse_names)
4313 / sizeof (lispy_mouse_names[0])));
4314 if (event->modifiers & drag_modifier)
4315 return Fcons (head,
4316 Fcons (start_pos,
4317 Fcons (position,
4318 Qnil)));
4319 else if (event->modifiers & (double_modifier | triple_modifier))
4320 return Fcons (head,
4321 Fcons (position,
4322 Fcons (make_number (double_click_count),
4323 Qnil)));
4324 else
4325 return Fcons (head,
4326 Fcons (position,
4327 Qnil));
4331 #ifdef WINDOWSNT
4332 case w32_scroll_bar_click:
4334 int button = event->code;
4335 int is_double;
4336 Lisp_Object position;
4337 Lisp_Object *start_pos_ptr;
4338 Lisp_Object start_pos;
4340 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
4341 abort ();
4344 Lisp_Object window;
4345 Lisp_Object portion_whole;
4346 Lisp_Object part;
4348 window = event->frame_or_window;
4349 portion_whole = Fcons (event->x, event->y);
4350 part = *scroll_bar_parts[(int) event->part];
4352 position
4353 = Fcons (window,
4354 Fcons (Qvertical_scroll_bar,
4355 Fcons (portion_whole,
4356 Fcons (make_number (event->timestamp),
4357 Fcons (part, Qnil)))));
4360 /* Always treat W32 scroll bar events as clicks. */
4361 event->modifiers |= click_modifier;
4364 /* Get the symbol we should use for the mouse click. */
4365 Lisp_Object head;
4367 head = modify_event_symbol (button,
4368 event->modifiers,
4369 Qmouse_click, Qnil,
4370 lispy_mouse_names, &mouse_syms,
4371 (sizeof (lispy_mouse_names)
4372 / sizeof (lispy_mouse_names[0])));
4373 return Fcons (head,
4374 Fcons (position,
4375 Qnil));
4378 case mouse_wheel:
4380 int part;
4381 FRAME_PTR f = XFRAME (event->frame_or_window);
4382 Lisp_Object window;
4383 Lisp_Object posn;
4384 Lisp_Object head, position;
4385 int row, column;
4387 /* Ignore mouse events that were made on frame that
4388 have been deleted. */
4389 if (! FRAME_LIVE_P (f))
4390 return Qnil;
4391 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4392 &column, &row, NULL, 1);
4393 window = window_from_coordinates (f, column, row, &part);
4395 if (!WINDOWP (window))
4397 window = event->frame_or_window;
4398 posn = Qnil;
4400 else
4402 int pixcolumn, pixrow;
4403 column -= XINT (XWINDOW (window)->left);
4404 row -= XINT (XWINDOW (window)->top);
4405 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
4406 XSETINT (event->x, pixcolumn);
4407 XSETINT (event->y, pixrow);
4409 if (part == 1)
4410 posn = Qmode_line;
4411 else if (part == 2)
4412 posn = Qvertical_line;
4413 else
4414 XSETINT (posn,
4415 buffer_posn_from_coords (XWINDOW (window),
4416 column, row));
4420 Lisp_Object head, position;
4422 position
4423 = Fcons (window,
4424 Fcons (posn,
4425 Fcons (Fcons (event->x, event->y),
4426 Fcons (make_number (event->timestamp),
4427 Qnil))));
4429 head = modify_event_symbol (0, event->modifiers,
4430 Qmouse_wheel, Qnil,
4431 lispy_mouse_wheel_names,
4432 &mouse_wheel_syms, 1);
4433 return Fcons (head,
4434 Fcons (position,
4435 Fcons (make_number (event->code),
4436 Qnil)));
4439 #endif /* WINDOWSNT */
4441 case drag_n_drop:
4443 int part;
4444 FRAME_PTR f;
4445 Lisp_Object window;
4446 Lisp_Object posn;
4447 Lisp_Object head, position;
4448 Lisp_Object files;
4449 int row, column;
4451 /* The frame_or_window field should be a cons of the frame in
4452 which the event occurred and a list of the filenames
4453 dropped. */
4454 if (! CONSP (event->frame_or_window))
4455 abort ();
4457 f = XFRAME (XCONS (event->frame_or_window)->car);
4458 files = XCONS (event->frame_or_window)->cdr;
4460 /* Ignore mouse events that were made on frames that
4461 have been deleted. */
4462 if (! FRAME_LIVE_P (f))
4463 return Qnil;
4464 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4465 &column, &row, NULL, 1);
4466 window = window_from_coordinates (f, column, row, &part);
4468 if (!WINDOWP (window))
4470 window = XCONS (event->frame_or_window)->car;
4471 posn = Qnil;
4473 else
4475 int pixcolumn, pixrow;
4476 column -= XINT (XWINDOW (window)->left);
4477 row -= XINT (XWINDOW (window)->top);
4478 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
4479 XSETINT (event->x, pixcolumn);
4480 XSETINT (event->y, pixrow);
4482 if (part == 1)
4483 posn = Qmode_line;
4484 else if (part == 2)
4485 posn = Qvertical_line;
4486 else
4487 XSETINT (posn,
4488 buffer_posn_from_coords (XWINDOW (window),
4489 column, row));
4493 Lisp_Object head, position;
4495 position
4496 = Fcons (window,
4497 Fcons (posn,
4498 Fcons (Fcons (event->x, event->y),
4499 Fcons (make_number (event->timestamp),
4500 Qnil))));
4502 head = modify_event_symbol (0, event->modifiers,
4503 Qdrag_n_drop, Qnil,
4504 lispy_drag_n_drop_names,
4505 &drag_n_drop_syms, 1);
4506 return Fcons (head,
4507 Fcons (position,
4508 Fcons (files,
4509 Qnil)));
4512 #endif /* HAVE_MOUSE */
4514 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
4515 case menu_bar_event:
4516 /* The event value is in the cdr of the frame_or_window slot. */
4517 if (!CONSP (event->frame_or_window))
4518 abort ();
4519 return XCONS (event->frame_or_window)->cdr;
4520 #endif
4522 /* The 'kind' field of the event is something we don't recognize. */
4523 default:
4524 abort ();
4528 #ifdef HAVE_MOUSE
4530 static Lisp_Object
4531 make_lispy_movement (frame, bar_window, part, x, y, time)
4532 FRAME_PTR frame;
4533 Lisp_Object bar_window;
4534 enum scroll_bar_part part;
4535 Lisp_Object x, y;
4536 unsigned long time;
4538 /* Is it a scroll bar movement? */
4539 if (frame && ! NILP (bar_window))
4541 Lisp_Object part_sym;
4543 part_sym = *scroll_bar_parts[(int) part];
4544 return Fcons (Qscroll_bar_movement,
4545 (Fcons (Fcons (bar_window,
4546 Fcons (Qvertical_scroll_bar,
4547 Fcons (Fcons (x, y),
4548 Fcons (make_number (time),
4549 Fcons (part_sym,
4550 Qnil))))),
4551 Qnil)));
4554 /* Or is it an ordinary mouse movement? */
4555 else
4557 int area;
4558 Lisp_Object window;
4559 Lisp_Object posn;
4560 int column, row;
4562 if (frame)
4564 /* It's in a frame; which window on that frame? */
4565 pixel_to_glyph_coords (frame, XINT (x), XINT (y), &column, &row,
4566 NULL, 1);
4567 window = window_from_coordinates (frame, column, row, &area);
4569 else
4570 window = Qnil;
4572 if (WINDOWP (window))
4574 int pixcolumn, pixrow;
4575 column -= WINDOW_LEFT_MARGIN (XWINDOW (window));
4576 row -= XINT (XWINDOW (window)->top);
4577 glyph_to_pixel_coords (frame, column, row, &pixcolumn, &pixrow);
4578 XSETINT (x, pixcolumn);
4579 XSETINT (y, pixrow);
4581 if (area == 1)
4582 posn = Qmode_line;
4583 else if (area == 2)
4584 posn = Qvertical_line;
4585 else
4586 XSETINT (posn,
4587 buffer_posn_from_coords (XWINDOW (window), column, row));
4589 else if (frame != 0)
4591 XSETFRAME (window, frame);
4592 posn = Qnil;
4594 else
4596 window = Qnil;
4597 posn = Qnil;
4598 XSETFASTINT (x, 0);
4599 XSETFASTINT (y, 0);
4602 return Fcons (Qmouse_movement,
4603 Fcons (Fcons (window,
4604 Fcons (posn,
4605 Fcons (Fcons (x, y),
4606 Fcons (make_number (time),
4607 Qnil)))),
4608 Qnil));
4612 #endif /* HAVE_MOUSE */
4614 /* Construct a switch frame event. */
4615 static Lisp_Object
4616 make_lispy_switch_frame (frame)
4617 Lisp_Object frame;
4619 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
4622 /* Manipulating modifiers. */
4624 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
4626 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
4627 SYMBOL's name of the end of the modifiers; the string from this
4628 position is the unmodified symbol name.
4630 This doesn't use any caches. */
4632 static int
4633 parse_modifiers_uncached (symbol, modifier_end)
4634 Lisp_Object symbol;
4635 int *modifier_end;
4637 struct Lisp_String *name;
4638 int i;
4639 int modifiers;
4641 CHECK_SYMBOL (symbol, 1);
4643 modifiers = 0;
4644 name = XSYMBOL (symbol)->name;
4646 for (i = 0; i+2 <= STRING_BYTES (name); )
4648 int this_mod_end = 0;
4649 int this_mod = 0;
4651 /* See if the name continues with a modifier word.
4652 Check that the word appears, but don't check what follows it.
4653 Set this_mod and this_mod_end to record what we find. */
4655 switch (name->data[i])
4657 #define SINGLE_LETTER_MOD(BIT) \
4658 (this_mod_end = i + 1, this_mod = BIT)
4660 case 'A':
4661 SINGLE_LETTER_MOD (alt_modifier);
4662 break;
4664 case 'C':
4665 SINGLE_LETTER_MOD (ctrl_modifier);
4666 break;
4668 case 'H':
4669 SINGLE_LETTER_MOD (hyper_modifier);
4670 break;
4672 case 'M':
4673 SINGLE_LETTER_MOD (meta_modifier);
4674 break;
4676 case 'S':
4677 SINGLE_LETTER_MOD (shift_modifier);
4678 break;
4680 case 's':
4681 SINGLE_LETTER_MOD (super_modifier);
4682 break;
4684 #undef SINGLE_LETTER_MOD
4687 /* If we found no modifier, stop looking for them. */
4688 if (this_mod_end == 0)
4689 break;
4691 /* Check there is a dash after the modifier, so that it
4692 really is a modifier. */
4693 if (this_mod_end >= STRING_BYTES (name)
4694 || name->data[this_mod_end] != '-')
4695 break;
4697 /* This modifier is real; look for another. */
4698 modifiers |= this_mod;
4699 i = this_mod_end + 1;
4702 /* Should we include the `click' modifier? */
4703 if (! (modifiers & (down_modifier | drag_modifier
4704 | double_modifier | triple_modifier))
4705 && i + 7 == STRING_BYTES (name)
4706 && strncmp (name->data + i, "mouse-", 6) == 0
4707 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
4708 modifiers |= click_modifier;
4710 if (modifier_end)
4711 *modifier_end = i;
4713 return modifiers;
4716 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
4717 prepended to the string BASE[0..BASE_LEN-1].
4718 This doesn't use any caches. */
4719 static Lisp_Object
4720 apply_modifiers_uncached (modifiers, base, base_len, base_len_byte)
4721 int modifiers;
4722 char *base;
4723 int base_len, base_len_byte;
4725 /* Since BASE could contain nulls, we can't use intern here; we have
4726 to use Fintern, which expects a genuine Lisp_String, and keeps a
4727 reference to it. */
4728 char *new_mods
4729 = (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
4730 int mod_len;
4733 char *p = new_mods;
4735 /* Only the event queue may use the `up' modifier; it should always
4736 be turned into a click or drag event before presented to lisp code. */
4737 if (modifiers & up_modifier)
4738 abort ();
4740 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
4741 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
4742 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
4743 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
4744 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
4745 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
4746 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
4747 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
4748 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
4749 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
4750 /* The click modifier is denoted by the absence of other modifiers. */
4752 *p = '\0';
4754 mod_len = p - new_mods;
4758 Lisp_Object new_name;
4760 new_name = make_uninit_multibyte_string (mod_len + base_len,
4761 mod_len + base_len_byte);
4762 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
4763 bcopy (base, XSTRING (new_name)->data + mod_len, base_len_byte);
4765 return Fintern (new_name, Qnil);
4770 static char *modifier_names[] =
4772 "up", "down", "drag", "click", "double", "triple", 0, 0,
4773 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4774 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
4776 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
4778 static Lisp_Object modifier_symbols;
4780 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
4781 static Lisp_Object
4782 lispy_modifier_list (modifiers)
4783 int modifiers;
4785 Lisp_Object modifier_list;
4786 int i;
4788 modifier_list = Qnil;
4789 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
4790 if (modifiers & (1<<i))
4791 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
4792 modifier_list);
4794 return modifier_list;
4798 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
4799 where UNMODIFIED is the unmodified form of SYMBOL,
4800 MASK is the set of modifiers present in SYMBOL's name.
4801 This is similar to parse_modifiers_uncached, but uses the cache in
4802 SYMBOL's Qevent_symbol_element_mask property, and maintains the
4803 Qevent_symbol_elements property. */
4805 Lisp_Object
4806 parse_modifiers (symbol)
4807 Lisp_Object symbol;
4809 Lisp_Object elements;
4811 elements = Fget (symbol, Qevent_symbol_element_mask);
4812 if (CONSP (elements))
4813 return elements;
4814 else
4816 int end;
4817 int modifiers = parse_modifiers_uncached (symbol, &end);
4818 Lisp_Object unmodified;
4819 Lisp_Object mask;
4821 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
4822 STRING_BYTES (XSYMBOL (symbol)->name) - end),
4823 Qnil);
4825 if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1))
4826 abort ();
4827 XSETFASTINT (mask, modifiers);
4828 elements = Fcons (unmodified, Fcons (mask, Qnil));
4830 /* Cache the parsing results on SYMBOL. */
4831 Fput (symbol, Qevent_symbol_element_mask,
4832 elements);
4833 Fput (symbol, Qevent_symbol_elements,
4834 Fcons (unmodified, lispy_modifier_list (modifiers)));
4836 /* Since we know that SYMBOL is modifiers applied to unmodified,
4837 it would be nice to put that in unmodified's cache.
4838 But we can't, since we're not sure that parse_modifiers is
4839 canonical. */
4841 return elements;
4845 /* Apply the modifiers MODIFIERS to the symbol BASE.
4846 BASE must be unmodified.
4848 This is like apply_modifiers_uncached, but uses BASE's
4849 Qmodifier_cache property, if present. It also builds
4850 Qevent_symbol_elements properties, since it has that info anyway.
4852 apply_modifiers copies the value of BASE's Qevent_kind property to
4853 the modified symbol. */
4854 static Lisp_Object
4855 apply_modifiers (modifiers, base)
4856 int modifiers;
4857 Lisp_Object base;
4859 Lisp_Object cache, index, entry, new_symbol;
4861 /* Mask out upper bits. We don't know where this value's been. */
4862 modifiers &= ((EMACS_INT)1 << VALBITS) - 1;
4864 /* The click modifier never figures into cache indices. */
4865 cache = Fget (base, Qmodifier_cache);
4866 XSETFASTINT (index, (modifiers & ~click_modifier));
4867 entry = assq_no_quit (index, cache);
4869 if (CONSP (entry))
4870 new_symbol = XCONS (entry)->cdr;
4871 else
4873 /* We have to create the symbol ourselves. */
4874 new_symbol = apply_modifiers_uncached (modifiers,
4875 XSYMBOL (base)->name->data,
4876 XSYMBOL (base)->name->size,
4877 STRING_BYTES (XSYMBOL (base)->name));
4879 /* Add the new symbol to the base's cache. */
4880 entry = Fcons (index, new_symbol);
4881 Fput (base, Qmodifier_cache, Fcons (entry, cache));
4883 /* We have the parsing info now for free, so add it to the caches. */
4884 XSETFASTINT (index, modifiers);
4885 Fput (new_symbol, Qevent_symbol_element_mask,
4886 Fcons (base, Fcons (index, Qnil)));
4887 Fput (new_symbol, Qevent_symbol_elements,
4888 Fcons (base, lispy_modifier_list (modifiers)));
4891 /* Make sure this symbol is of the same kind as BASE.
4893 You'd think we could just set this once and for all when we
4894 intern the symbol above, but reorder_modifiers may call us when
4895 BASE's property isn't set right; we can't assume that just
4896 because it has a Qmodifier_cache property it must have its
4897 Qevent_kind set right as well. */
4898 if (NILP (Fget (new_symbol, Qevent_kind)))
4900 Lisp_Object kind;
4902 kind = Fget (base, Qevent_kind);
4903 if (! NILP (kind))
4904 Fput (new_symbol, Qevent_kind, kind);
4907 return new_symbol;
4911 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
4912 return a symbol with the modifiers placed in the canonical order.
4913 Canonical order is alphabetical, except for down and drag, which
4914 always come last. The 'click' modifier is never written out.
4916 Fdefine_key calls this to make sure that (for example) C-M-foo
4917 and M-C-foo end up being equivalent in the keymap. */
4919 Lisp_Object
4920 reorder_modifiers (symbol)
4921 Lisp_Object symbol;
4923 /* It's hopefully okay to write the code this way, since everything
4924 will soon be in caches, and no consing will be done at all. */
4925 Lisp_Object parsed;
4927 parsed = parse_modifiers (symbol);
4928 return apply_modifiers ((int) XINT (XCONS (XCONS (parsed)->cdr)->car),
4929 XCONS (parsed)->car);
4933 /* For handling events, we often want to produce a symbol whose name
4934 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
4935 to some base, like the name of a function key or mouse button.
4936 modify_event_symbol produces symbols of this sort.
4938 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
4939 is the name of the i'th symbol. TABLE_SIZE is the number of elements
4940 in the table.
4942 Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
4943 NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
4945 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
4946 persist between calls to modify_event_symbol that it can use to
4947 store a cache of the symbols it's generated for this NAME_TABLE
4948 before. The object stored there may be a vector or an alist.
4950 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
4952 MODIFIERS is a set of modifier bits (as given in struct input_events)
4953 whose prefixes should be applied to the symbol name.
4955 SYMBOL_KIND is the value to be placed in the event_kind property of
4956 the returned symbol.
4958 The symbols we create are supposed to have an
4959 `event-symbol-elements' property, which lists the modifiers present
4960 in the symbol's name. */
4962 static Lisp_Object
4963 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
4964 name_table, symbol_table, table_size)
4965 int symbol_num;
4966 unsigned modifiers;
4967 Lisp_Object symbol_kind;
4968 Lisp_Object name_alist;
4969 char **name_table;
4970 Lisp_Object *symbol_table;
4971 unsigned int table_size;
4973 Lisp_Object value;
4974 Lisp_Object symbol_int;
4976 /* Get rid of the "vendor-specific" bit here. */
4977 XSETINT (symbol_int, symbol_num & 0xffffff);
4979 /* Is this a request for a valid symbol? */
4980 if (symbol_num < 0 || symbol_num >= table_size)
4981 return Qnil;
4983 if (CONSP (*symbol_table))
4984 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
4986 /* If *symbol_table doesn't seem to be initialized properly, fix that.
4987 *symbol_table should be a lisp vector TABLE_SIZE elements long,
4988 where the Nth element is the symbol for NAME_TABLE[N], or nil if
4989 we've never used that symbol before. */
4990 else
4992 if (! VECTORP (*symbol_table)
4993 || XVECTOR (*symbol_table)->size != table_size)
4995 Lisp_Object size;
4997 XSETFASTINT (size, table_size);
4998 *symbol_table = Fmake_vector (size, Qnil);
5001 value = XVECTOR (*symbol_table)->contents[symbol_num];
5004 /* Have we already used this symbol before? */
5005 if (NILP (value))
5007 /* No; let's create it. */
5008 if (!NILP (name_alist))
5009 value = Fcdr_safe (Fassq (symbol_int, name_alist));
5010 else if (name_table != 0 && name_table[symbol_num])
5011 value = intern (name_table[symbol_num]);
5013 #ifdef HAVE_WINDOW_SYSTEM
5014 if (NILP (value))
5016 char *name = x_get_keysym_name (symbol_num);
5017 if (name)
5018 value = intern (name);
5020 #endif
5022 if (NILP (value))
5024 char buf[20];
5025 sprintf (buf, "key-%d", symbol_num);
5026 value = intern (buf);
5029 if (CONSP (*symbol_table))
5030 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
5031 else
5032 XVECTOR (*symbol_table)->contents[symbol_num] = value;
5034 /* Fill in the cache entries for this symbol; this also
5035 builds the Qevent_symbol_elements property, which the user
5036 cares about. */
5037 apply_modifiers (modifiers & click_modifier, value);
5038 Fput (value, Qevent_kind, symbol_kind);
5041 /* Apply modifiers to that symbol. */
5042 return apply_modifiers (modifiers, value);
5045 /* Convert a list that represents an event type,
5046 such as (ctrl meta backspace), into the usual representation of that
5047 event type as a number or a symbol. */
5049 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
5050 "Convert the event description list EVENT-DESC to an event type.\n\
5051 EVENT-DESC should contain one base event type (a character or symbol)\n\
5052 and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\
5053 drag, down, double or triple). The base must be last.\n\
5054 The return value is an event type (a character or symbol) which\n\
5055 has the same base event type and all the specified modifiers.")
5056 (event_desc)
5057 Lisp_Object event_desc;
5059 Lisp_Object base;
5060 int modifiers = 0;
5061 Lisp_Object rest;
5063 base = Qnil;
5064 rest = event_desc;
5065 while (CONSP (rest))
5067 Lisp_Object elt;
5068 int this = 0;
5070 elt = XCONS (rest)->car;
5071 rest = XCONS (rest)->cdr;
5073 /* Given a symbol, see if it is a modifier name. */
5074 if (SYMBOLP (elt) && CONSP (rest))
5075 this = parse_solitary_modifier (elt);
5077 if (this != 0)
5078 modifiers |= this;
5079 else if (!NILP (base))
5080 error ("Two bases given in one event");
5081 else
5082 base = elt;
5086 /* Let the symbol A refer to the character A. */
5087 if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1)
5088 XSETINT (base, XSYMBOL (base)->name->data[0]);
5090 if (INTEGERP (base))
5092 /* Turn (shift a) into A. */
5093 if ((modifiers & shift_modifier) != 0
5094 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
5096 XSETINT (base, XINT (base) - ('a' - 'A'));
5097 modifiers &= ~shift_modifier;
5100 /* Turn (control a) into C-a. */
5101 if (modifiers & ctrl_modifier)
5102 return make_number ((modifiers & ~ctrl_modifier)
5103 | make_ctrl_char (XINT (base)));
5104 else
5105 return make_number (modifiers | XINT (base));
5107 else if (SYMBOLP (base))
5108 return apply_modifiers (modifiers, base);
5109 else
5110 error ("Invalid base event");
5113 /* Try to recognize SYMBOL as a modifier name.
5114 Return the modifier flag bit, or 0 if not recognized. */
5116 static int
5117 parse_solitary_modifier (symbol)
5118 Lisp_Object symbol;
5120 struct Lisp_String *name = XSYMBOL (symbol)->name;
5122 switch (name->data[0])
5124 #define SINGLE_LETTER_MOD(BIT) \
5125 if (STRING_BYTES (name) == 1) \
5126 return BIT;
5128 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
5129 if (LEN == STRING_BYTES (name) \
5130 && ! strncmp (name->data, NAME, LEN)) \
5131 return BIT;
5133 case 'A':
5134 SINGLE_LETTER_MOD (alt_modifier);
5135 break;
5137 case 'a':
5138 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
5139 break;
5141 case 'C':
5142 SINGLE_LETTER_MOD (ctrl_modifier);
5143 break;
5145 case 'c':
5146 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
5147 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
5148 break;
5150 case 'H':
5151 SINGLE_LETTER_MOD (hyper_modifier);
5152 break;
5154 case 'h':
5155 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
5156 break;
5158 case 'M':
5159 SINGLE_LETTER_MOD (meta_modifier);
5160 break;
5162 case 'm':
5163 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
5164 break;
5166 case 'S':
5167 SINGLE_LETTER_MOD (shift_modifier);
5168 break;
5170 case 's':
5171 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
5172 MULTI_LETTER_MOD (super_modifier, "super", 5);
5173 SINGLE_LETTER_MOD (super_modifier);
5174 break;
5176 case 'd':
5177 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
5178 MULTI_LETTER_MOD (down_modifier, "down", 4);
5179 MULTI_LETTER_MOD (double_modifier, "double", 6);
5180 break;
5182 case 't':
5183 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
5184 break;
5186 #undef SINGLE_LETTER_MOD
5187 #undef MULTI_LETTER_MOD
5190 return 0;
5193 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
5194 Such a list is not valid as an event,
5195 but it can be a Lucid-style event type list. */
5198 lucid_event_type_list_p (object)
5199 Lisp_Object object;
5201 Lisp_Object tail;
5203 if (! CONSP (object))
5204 return 0;
5206 for (tail = object; CONSP (tail); tail = XCONS (tail)->cdr)
5208 Lisp_Object elt;
5209 elt = XCONS (tail)->car;
5210 if (! (INTEGERP (elt) || SYMBOLP (elt)))
5211 return 0;
5214 return NILP (tail);
5217 /* Store into *addr a value nonzero if terminal input chars are available.
5218 Serves the purpose of ioctl (0, FIONREAD, addr)
5219 but works even if FIONREAD does not exist.
5220 (In fact, this may actually read some input.)
5222 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe. */
5224 static void
5225 get_input_pending (addr, do_timers_now)
5226 int *addr;
5227 int do_timers_now;
5229 /* First of all, have we already counted some input? */
5230 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
5232 /* If input is being read as it arrives, and we have none, there is none. */
5233 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
5234 return;
5236 /* Try to read some input and see how much we get. */
5237 gobble_input (0);
5238 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
5241 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
5243 void
5244 gobble_input (expected)
5245 int expected;
5247 #ifndef VMS
5248 #ifdef SIGIO
5249 if (interrupt_input)
5251 SIGMASKTYPE mask;
5252 mask = sigblock (sigmask (SIGIO));
5253 read_avail_input (expected);
5254 sigsetmask (mask);
5256 else
5257 #ifdef POLL_FOR_INPUT
5258 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
5260 SIGMASKTYPE mask;
5261 mask = sigblock (sigmask (SIGALRM));
5262 read_avail_input (expected);
5263 sigsetmask (mask);
5265 else
5266 #endif
5267 #endif
5268 read_avail_input (expected);
5269 #endif
5272 /* Put a buffer_switch_event in the buffer
5273 so that read_key_sequence will notice the new current buffer. */
5275 void
5276 record_asynch_buffer_change ()
5278 struct input_event event;
5279 Lisp_Object tem;
5281 event.kind = buffer_switch_event;
5282 event.frame_or_window = Qnil;
5284 #ifdef subprocesses
5285 /* We don't need a buffer-switch event unless Emacs is waiting for input.
5286 The purpose of the event is to make read_key_sequence look up the
5287 keymaps again. If we aren't in read_key_sequence, we don't need one,
5288 and the event could cause trouble by messing up (input-pending-p). */
5289 tem = Fwaiting_for_user_input_p ();
5290 if (NILP (tem))
5291 return;
5292 #else
5293 /* We never need these events if we have no asynchronous subprocesses. */
5294 return;
5295 #endif
5297 /* Make sure no interrupt happens while storing the event. */
5298 #ifdef SIGIO
5299 if (interrupt_input)
5301 SIGMASKTYPE mask;
5302 mask = sigblock (sigmask (SIGIO));
5303 kbd_buffer_store_event (&event);
5304 sigsetmask (mask);
5306 else
5307 #endif
5309 stop_polling ();
5310 kbd_buffer_store_event (&event);
5311 start_polling ();
5315 #ifndef VMS
5317 /* Read any terminal input already buffered up by the system
5318 into the kbd_buffer, but do not wait.
5320 EXPECTED should be nonzero if the caller knows there is some input.
5322 Except on VMS, all input is read by this function.
5323 If interrupt_input is nonzero, this function MUST be called
5324 only when SIGIO is blocked.
5326 Returns the number of keyboard chars read, or -1 meaning
5327 this is a bad time to try to read input. */
5329 static int
5330 read_avail_input (expected)
5331 int expected;
5333 struct input_event buf[KBD_BUFFER_SIZE];
5334 register int i;
5335 int nread;
5337 if (read_socket_hook)
5338 /* No need for FIONREAD or fcntl; just say don't wait. */
5339 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE, expected);
5340 else
5342 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
5343 the kbd_buffer can really hold. That may prevent loss
5344 of characters on some systems when input is stuffed at us. */
5345 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
5346 int n_to_read;
5348 /* Determine how many characters we should *try* to read. */
5349 #ifdef WINDOWSNT
5350 return 0;
5351 #else /* not WINDOWSNT */
5352 #ifdef MSDOS
5353 n_to_read = dos_keysns ();
5354 if (n_to_read == 0)
5355 return 0;
5356 #else /* not MSDOS */
5357 #ifdef FIONREAD
5358 /* Find out how much input is available. */
5359 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
5360 /* Formerly simply reported no input, but that sometimes led to
5361 a failure of Emacs to terminate.
5362 SIGHUP seems appropriate if we can't reach the terminal. */
5363 /* ??? Is it really right to send the signal just to this process
5364 rather than to the whole process group?
5365 Perhaps on systems with FIONREAD Emacs is alone in its group. */
5366 kill (getpid (), SIGHUP);
5367 if (n_to_read == 0)
5368 return 0;
5369 if (n_to_read > sizeof cbuf)
5370 n_to_read = sizeof cbuf;
5371 #else /* no FIONREAD */
5372 #if defined (USG) || defined (DGUX)
5373 /* Read some input if available, but don't wait. */
5374 n_to_read = sizeof cbuf;
5375 fcntl (input_fd, F_SETFL, O_NDELAY);
5376 #else
5377 you lose;
5378 #endif
5379 #endif
5380 #endif /* not MSDOS */
5381 #endif /* not WINDOWSNT */
5383 /* Now read; for one reason or another, this will not block.
5384 NREAD is set to the number of chars read. */
5387 #ifdef MSDOS
5388 cbuf[0] = dos_keyread ();
5389 nread = 1;
5390 #else
5391 nread = read (input_fd, cbuf, n_to_read);
5392 #endif
5393 /* POSIX infers that processes which are not in the session leader's
5394 process group won't get SIGHUP's at logout time. BSDI adheres to
5395 this part standard and returns -1 from read (0) with errno==EIO
5396 when the control tty is taken away.
5397 Jeffrey Honig <jch@bsdi.com> says this is generally safe. */
5398 if (nread == -1 && errno == EIO)
5399 kill (0, SIGHUP);
5400 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
5401 /* The kernel sometimes fails to deliver SIGHUP for ptys.
5402 This looks incorrect, but it isn't, because _BSD causes
5403 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
5404 and that causes a value other than 0 when there is no input. */
5405 if (nread == 0)
5406 kill (0, SIGHUP);
5407 #endif
5409 while (
5410 /* We used to retry the read if it was interrupted.
5411 But this does the wrong thing when O_NDELAY causes
5412 an EAGAIN error. Does anybody know of a situation
5413 where a retry is actually needed? */
5414 #if 0
5415 nread < 0 && (errno == EAGAIN
5416 #ifdef EFAULT
5417 || errno == EFAULT
5418 #endif
5419 #ifdef EBADSLT
5420 || errno == EBADSLT
5421 #endif
5423 #else
5425 #endif
5428 #ifndef FIONREAD
5429 #if defined (USG) || defined (DGUX)
5430 fcntl (input_fd, F_SETFL, 0);
5431 #endif /* USG or DGUX */
5432 #endif /* no FIONREAD */
5433 for (i = 0; i < nread; i++)
5435 buf[i].kind = ascii_keystroke;
5436 buf[i].modifiers = 0;
5437 if (meta_key == 1 && (cbuf[i] & 0x80))
5438 buf[i].modifiers = meta_modifier;
5439 if (meta_key != 2)
5440 cbuf[i] &= ~0x80;
5442 buf[i].code = cbuf[i];
5443 XSETFRAME (buf[i].frame_or_window, selected_frame);
5447 /* Scan the chars for C-g and store them in kbd_buffer. */
5448 for (i = 0; i < nread; i++)
5450 kbd_buffer_store_event (&buf[i]);
5451 /* Don't look at input that follows a C-g too closely.
5452 This reduces lossage due to autorepeat on C-g. */
5453 if (buf[i].kind == ascii_keystroke
5454 && buf[i].code == quit_char)
5455 break;
5458 return nread;
5460 #endif /* not VMS */
5462 #ifdef SIGIO /* for entire page */
5463 /* Note SIGIO has been undef'd if FIONREAD is missing. */
5465 SIGTYPE
5466 input_available_signal (signo)
5467 int signo;
5469 /* Must preserve main program's value of errno. */
5470 int old_errno = errno;
5471 #ifdef BSD4_1
5472 extern int select_alarmed;
5473 #endif
5475 #if defined (USG) && !defined (POSIX_SIGNALS)
5476 /* USG systems forget handlers when they are used;
5477 must reestablish each time */
5478 signal (signo, input_available_signal);
5479 #endif /* USG */
5481 #ifdef BSD4_1
5482 sigisheld (SIGIO);
5483 #endif
5485 if (input_available_clear_time)
5486 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
5488 while (1)
5490 int nread;
5491 nread = read_avail_input (1);
5492 /* -1 means it's not ok to read the input now.
5493 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
5494 0 means there was no keyboard input available. */
5495 if (nread <= 0)
5496 break;
5498 #ifdef BSD4_1
5499 select_alarmed = 1; /* Force the select emulator back to life */
5500 #endif
5503 #ifdef BSD4_1
5504 sigfree ();
5505 #endif
5506 errno = old_errno;
5508 #endif /* SIGIO */
5510 /* Send ourselves a SIGIO.
5512 This function exists so that the UNBLOCK_INPUT macro in
5513 blockinput.h can have some way to take care of input we put off
5514 dealing with, without assuming that every file which uses
5515 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
5516 void
5517 reinvoke_input_signal ()
5519 #ifdef SIGIO
5520 kill (getpid (), SIGIO);
5521 #endif
5526 /* Return the prompt-string of a sparse keymap.
5527 This is the first element which is a string.
5528 Return nil if there is none. */
5530 Lisp_Object
5531 map_prompt (map)
5532 Lisp_Object map;
5534 while (CONSP (map))
5536 register Lisp_Object tem;
5537 tem = Fcar (map);
5538 if (STRINGP (tem))
5539 return tem;
5540 map = Fcdr (map);
5542 return Qnil;
5545 static void menu_bar_item ();
5546 static void menu_bar_one_keymap ();
5548 /* These variables hold the vector under construction within
5549 menu_bar_items and its subroutines, and the current index
5550 for storing into that vector. */
5551 static Lisp_Object menu_bar_items_vector;
5552 static int menu_bar_items_index;
5554 /* Return a vector of menu items for a menu bar, appropriate
5555 to the current buffer. Each item has three elements in the vector:
5556 KEY STRING MAPLIST.
5558 OLD is an old vector we can optionally reuse, or nil. */
5560 Lisp_Object
5561 menu_bar_items (old)
5562 Lisp_Object old;
5564 /* The number of keymaps we're scanning right now, and the number of
5565 keymaps we have allocated space for. */
5566 int nmaps;
5568 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
5569 in the current keymaps, or nil where it is not a prefix. */
5570 Lisp_Object *maps;
5572 Lisp_Object def, tem, tail;
5574 Lisp_Object result;
5576 int mapno;
5577 Lisp_Object oquit;
5579 int i;
5581 struct gcpro gcpro1;
5583 /* In order to build the menus, we need to call the keymap
5584 accessors. They all call QUIT. But this function is called
5585 during redisplay, during which a quit is fatal. So inhibit
5586 quitting while building the menus.
5587 We do this instead of specbind because (1) errors will clear it anyway
5588 and (2) this avoids risk of specpdl overflow. */
5589 oquit = Vinhibit_quit;
5590 Vinhibit_quit = Qt;
5592 if (!NILP (old))
5593 menu_bar_items_vector = old;
5594 else
5595 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
5596 menu_bar_items_index = 0;
5598 GCPRO1 (menu_bar_items_vector);
5600 /* Build our list of keymaps.
5601 If we recognize a function key and replace its escape sequence in
5602 keybuf with its symbol, or if the sequence starts with a mouse
5603 click and we need to switch buffers, we jump back here to rebuild
5604 the initial keymaps from the current buffer. */
5606 Lisp_Object *tmaps;
5608 /* Should overriding-terminal-local-map and overriding-local-map apply? */
5609 if (!NILP (Voverriding_local_map_menu_flag))
5611 /* Yes, use them (if non-nil) as well as the global map. */
5612 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
5613 nmaps = 0;
5614 if (!NILP (current_kboard->Voverriding_terminal_local_map))
5615 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
5616 if (!NILP (Voverriding_local_map))
5617 maps[nmaps++] = Voverriding_local_map;
5619 else
5621 /* No, so use major and minor mode keymaps. */
5622 nmaps = current_minor_maps (NULL, &tmaps);
5623 maps = (Lisp_Object *) alloca ((nmaps + 2) * sizeof (maps[0]));
5624 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
5625 #ifdef USE_TEXT_PROPERTIES
5626 maps[nmaps++] = get_local_map (PT, current_buffer);
5627 #else
5628 maps[nmaps++] = current_buffer->keymap;
5629 #endif
5631 maps[nmaps++] = current_global_map;
5634 /* Look up in each map the dummy prefix key `menu-bar'. */
5636 result = Qnil;
5638 for (mapno = nmaps - 1; mapno >= 0; mapno--)
5640 if (! NILP (maps[mapno]))
5641 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0), 0);
5642 else
5643 def = Qnil;
5645 tem = Fkeymapp (def);
5646 if (!NILP (tem))
5647 menu_bar_one_keymap (def);
5650 /* Move to the end those items that should be at the end. */
5652 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCONS (tail)->cdr)
5654 int i;
5655 int end = menu_bar_items_index;
5657 for (i = 0; i < end; i += 4)
5658 if (EQ (XCONS (tail)->car, XVECTOR (menu_bar_items_vector)->contents[i]))
5660 Lisp_Object tem0, tem1, tem2, tem3;
5661 /* Move the item at index I to the end,
5662 shifting all the others forward. */
5663 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
5664 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
5665 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
5666 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
5667 if (end > i + 4)
5668 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
5669 &XVECTOR (menu_bar_items_vector)->contents[i],
5670 (end - i - 4) * sizeof (Lisp_Object));
5671 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
5672 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
5673 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
5674 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
5675 break;
5679 /* Add nil, nil, nil, nil at the end. */
5680 i = menu_bar_items_index;
5681 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
5683 Lisp_Object tem;
5684 int newsize = 2 * i;
5685 tem = Fmake_vector (make_number (2 * i), Qnil);
5686 bcopy (XVECTOR (menu_bar_items_vector)->contents,
5687 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
5688 menu_bar_items_vector = tem;
5690 /* Add this item. */
5691 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5692 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5693 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5694 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5695 menu_bar_items_index = i;
5697 Vinhibit_quit = oquit;
5698 UNGCPRO;
5699 return menu_bar_items_vector;
5702 /* Scan one map KEYMAP, accumulating any menu items it defines
5703 in menu_bar_items_vector. */
5705 static void
5706 menu_bar_one_keymap (keymap)
5707 Lisp_Object keymap;
5709 Lisp_Object tail, item, table;
5711 /* Loop over all keymap entries that have menu strings. */
5712 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
5714 item = XCONS (tail)->car;
5715 if (CONSP (item))
5716 menu_bar_item (XCONS (item)->car, XCONS (item)->cdr);
5717 else if (VECTORP (item))
5719 /* Loop over the char values represented in the vector. */
5720 int len = XVECTOR (item)->size;
5721 int c;
5722 for (c = 0; c < len; c++)
5724 Lisp_Object character;
5725 XSETFASTINT (character, c);
5726 menu_bar_item (character, XVECTOR (item)->contents[c]);
5732 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
5733 If there's already an item for KEY, add this DEF to it. */
5735 Lisp_Object item_properties;
5737 static void
5738 menu_bar_item (key, item)
5739 Lisp_Object key, item;
5741 struct gcpro gcpro1;
5742 int i;
5744 if (EQ (item, Qundefined))
5746 /* If a map has an explicit `undefined' as definition,
5747 discard any previously made menu bar item. */
5749 for (i = 0; i < menu_bar_items_index; i += 4)
5750 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
5752 if (menu_bar_items_index > i + 4)
5753 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
5754 &XVECTOR (menu_bar_items_vector)->contents[i],
5755 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
5756 menu_bar_items_index -= 4;
5757 return;
5760 /* If there's no definition for this key yet,
5761 just ignore `undefined'. */
5762 return;
5765 GCPRO1 (key); /* Is this necessary? */
5766 i = parse_menu_item (item, 0, 1);
5767 UNGCPRO;
5768 if (!i)
5769 return;
5771 item = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
5773 /* Find any existing item for this KEY. */
5774 for (i = 0; i < menu_bar_items_index; i += 4)
5775 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
5776 break;
5778 /* If we did not find this KEY, add it at the end. */
5779 if (i == menu_bar_items_index)
5781 /* If vector is too small, get a bigger one. */
5782 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
5784 Lisp_Object tem;
5785 int newsize = 2 * i;
5786 tem = Fmake_vector (make_number (2 * i), Qnil);
5787 bcopy (XVECTOR (menu_bar_items_vector)->contents,
5788 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
5789 menu_bar_items_vector = tem;
5792 /* Add this item. */
5793 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
5794 XVECTOR (menu_bar_items_vector)->contents[i++]
5795 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
5796 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (item, Qnil);
5797 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
5798 menu_bar_items_index = i;
5800 /* We did find an item for this KEY. Add ITEM to its list of maps. */
5801 else
5803 Lisp_Object old;
5804 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
5805 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (item, old);
5809 /* This is used as the handler when calling menu_item_eval_property. */
5810 static Lisp_Object
5811 menu_item_eval_property_1 (arg)
5812 Lisp_Object arg;
5814 /* If we got a quit from within the menu computation,
5815 quit all the way out of it. This takes care of C-] in the debugger. */
5816 if (CONSP (arg) && EQ (XCONS (arg)->car, Qquit))
5817 Fsignal (Qquit, Qnil);
5819 return Qnil;
5822 /* Evaluate an expression and return the result (or nil if something
5823 went wrong). Used to evaluate dynamic parts of menu items. */
5824 static Lisp_Object
5825 menu_item_eval_property (sexpr)
5826 Lisp_Object sexpr;
5828 int count = specpdl_ptr - specpdl;
5829 Lisp_Object val;
5830 specbind (Qinhibit_redisplay, Qt);
5831 val = internal_condition_case_1 (Feval, sexpr, Qerror,
5832 menu_item_eval_property_1);
5833 return unbind_to (count, val);
5836 /* This function parses a menu item and leaves the result in the
5837 vector item_properties.
5838 ITEM is a key binding, a possible menu item.
5839 If NOTREAL is nonzero, only check for equivalent key bindings, don't
5840 evaluate dynamic expressions in the menu item.
5841 INMENUBAR is > 0 when this is considered for an entry in a menu bar
5842 top level.
5843 INMENUBAR is < 0 when this is considered for an entry in a keyboard menu.
5844 parse_menu_item returns true if the item is a menu item and false
5845 otherwise. */
5848 parse_menu_item (item, notreal, inmenubar)
5849 Lisp_Object item;
5850 int notreal, inmenubar;
5852 Lisp_Object def, tem, item_string, start;
5853 Lisp_Object cachelist;
5854 Lisp_Object filter;
5855 Lisp_Object keyhint;
5856 int i;
5857 int newcache = 0;
5859 cachelist = Qnil;
5860 filter = Qnil;
5861 keyhint = Qnil;
5863 if (!CONSP (item))
5864 return 0;
5866 /* Create item_properties vector if necessary. */
5867 if (NILP (item_properties))
5868 item_properties
5869 = Fmake_vector (make_number (ITEM_PROPERTY_ENABLE + 1), Qnil);
5871 /* Initialize optional entries. */
5872 for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++)
5873 XVECTOR (item_properties)->contents[i] = Qnil;
5874 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = Qt;
5876 /* Save the item here to protect it from GC. */
5877 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ITEM] = item;
5879 item_string = XCONS (item)->car;
5881 start = item;
5882 item = XCONS (item)->cdr;
5883 if (STRINGP (item_string))
5885 /* Old format menu item. */
5886 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME] = item_string;
5888 /* Maybe help string. */
5889 if (CONSP (item) && STRINGP (XCONS (item)->car))
5891 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]
5892 = XCONS (item)->car;
5893 start = item;
5894 item = XCONS (item)->cdr;
5897 /* Maybee key binding cache. */
5898 if (CONSP (item) && CONSP (XCONS (item)->car)
5899 && (NILP (XCONS (XCONS (item)->car)->car)
5900 || VECTORP (XCONS (XCONS (item)->car)->car)))
5902 cachelist = XCONS (item)->car;
5903 item = XCONS (item)->cdr;
5906 /* This is the real definition--the function to run. */
5907 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = item;
5909 /* Get enable property, if any. */
5910 if (SYMBOLP (item))
5912 tem = Fget (item, Qmenu_enable);
5913 if (!NILP (tem))
5914 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = tem;
5917 else if (EQ (item_string, Qmenu_item) && CONSP (item))
5919 /* New format menu item. */
5920 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME]
5921 = XCONS (item)->car;
5922 start = XCONS (item)->cdr;
5923 if (CONSP (start))
5925 /* We have a real binding. */
5926 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF]
5927 = XCONS (start)->car;
5929 item = XCONS (start)->cdr;
5930 /* Is there a cache list with key equivalences. */
5931 if (CONSP (item) && CONSP (XCONS (item)->car))
5933 cachelist = XCONS (item)->car;
5934 item = XCONS (item)->cdr;
5937 /* Parse properties. */
5938 while (CONSP (item) && CONSP (XCONS (item)->cdr))
5940 tem = XCONS (item)->car;
5941 item = XCONS (item)->cdr;
5943 if (EQ (tem, QCenable))
5944 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE]
5945 = XCONS (item)->car;
5946 else if (EQ (tem, QCvisible) && !notreal)
5948 /* If got a visible property and that evaluates to nil
5949 then ignore this item. */
5950 tem = menu_item_eval_property (XCONS (item)->car);
5951 if (NILP (tem))
5952 return 0;
5954 else if (EQ (tem, QChelp))
5955 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]
5956 = XCONS (item)->car;
5957 else if (EQ (tem, QCfilter))
5958 filter = item;
5959 else if (EQ (tem, QCkey_sequence))
5961 tem = XCONS (item)->car;
5962 if (NILP (cachelist)
5963 && (SYMBOLP (tem) || STRINGP (tem) || VECTORP (tem)))
5964 /* Be GC protected. Set keyhint to item instead of tem. */
5965 keyhint = item;
5967 else if (EQ (tem, QCkeys))
5969 tem = XCONS (item)->car;
5970 if (CONSP (tem) || STRINGP (tem) && NILP (cachelist))
5971 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ]
5972 = tem;
5974 else if (EQ (tem, QCbutton) && CONSP (XCONS (item)->car))
5976 Lisp_Object type;
5977 tem = XCONS (item)->car;
5978 type = XCONS (tem)->car;
5979 if (EQ (type, QCtoggle) || EQ (type, QCradio))
5981 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]
5982 = XCONS (tem)->cdr;
5983 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE]
5984 = type;
5987 item = XCONS (item)->cdr;
5990 else if (inmenubar || !NILP (start))
5991 return 0;
5993 else
5994 return 0; /* not a menu item */
5996 /* If item string is not a string, evaluate it to get string.
5997 If we don't get a string, skip this item. */
5998 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
5999 if (!(STRINGP (item_string) || notreal))
6001 item_string = menu_item_eval_property (item_string);
6002 if (!STRINGP (item_string))
6003 return 0;
6004 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME] = item_string;
6007 /* If got a filter apply it on definition. */
6008 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6009 if (!NILP (filter))
6011 def = menu_item_eval_property (list2 (XCONS (filter)->car,
6012 list2 (Qquote, def)));
6014 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = def;
6017 /* If we got no definition, this item is just unselectable text which
6018 is OK in a submenu but not in the menubar. */
6019 if (NILP (def))
6020 return (inmenubar ? 0 : 1);
6022 /* Enable or disable selection of item. */
6023 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
6024 if (!EQ (tem, Qt))
6026 if (notreal)
6027 tem = Qt;
6028 else
6029 tem = menu_item_eval_property (tem);
6030 if (inmenubar && NILP (tem))
6031 return 0; /* Ignore disabled items in menu bar. */
6032 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = tem;
6035 /* See if this is a separate pane or a submenu. */
6036 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6037 tem = get_keymap_1 (def, 0, 1);
6038 /* For a subkeymap, just record its details and exit. */
6039 if (!NILP (tem))
6041 XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP] = tem;
6042 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = tem;
6043 return 1;
6045 /* At the top level in the menu bar, do likewise for commands also.
6046 The menu bar does not display equivalent key bindings anyway.
6047 ITEM_PROPERTY_DEF is already set up properly. */
6048 if (inmenubar > 0)
6049 return 1;
6051 /* This is a command. See if there is an equivalent key binding. */
6052 if (NILP (cachelist))
6054 /* We have to create a cachelist. */
6055 CHECK_IMPURE (start);
6056 XCONS (start)->cdr = Fcons (Fcons (Qnil, Qnil), XCONS (start)->cdr);
6057 cachelist = XCONS (XCONS (start)->cdr)->car;
6058 newcache = 1;
6059 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
6060 if (!NILP (keyhint))
6062 XCONS (cachelist)->car = XCONS (keyhint)->car;
6063 newcache = 0;
6065 else if (STRINGP (tem))
6067 XCONS (cachelist)->cdr = Fsubstitute_command_keys (tem);
6068 XCONS (cachelist)->car = Qt;
6071 tem = XCONS (cachelist)->car;
6072 if (!EQ (tem, Qt))
6074 int chkcache = 0;
6075 Lisp_Object prefix;
6077 if (!NILP (tem))
6078 tem = Fkey_binding (tem, Qnil);
6080 prefix = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
6081 if (CONSP (prefix))
6083 def = XCONS (prefix)->car;
6084 prefix = XCONS (prefix)->cdr;
6086 else
6087 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6089 if (NILP (XCONS (cachelist)->car)) /* Have no saved key. */
6091 if (newcache /* Always check first time. */
6092 /* Should we check everything when precomputing key
6093 bindings? */
6094 /* || notreal */
6095 /* If something had no key binding before, don't recheck it
6096 because that is too slow--except if we have a list of
6097 rebound commands in Vdefine_key_rebound_commands, do
6098 recheck any command that appears in that list. */
6099 || (CONSP (Vdefine_key_rebound_commands)
6100 && !NILP (Fmemq (def, Vdefine_key_rebound_commands))))
6101 chkcache = 1;
6103 /* We had a saved key. Is it still bound to the command? */
6104 else if (NILP (tem)
6105 || !EQ (tem, def)
6106 /* If the command is an alias for another
6107 (such as lmenu.el set it up), check if the
6108 original command matches the cached command. */
6109 && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function)))
6110 chkcache = 1; /* Need to recompute key binding. */
6112 if (chkcache)
6114 /* Recompute equivalent key binding. If the command is an alias
6115 for another (such as lmenu.el set it up), see if the original
6116 command name has equivalent keys. Otherwise look up the
6117 specified command itself. We don't try both, because that
6118 makes lmenu menus slow. */
6119 if (SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
6120 && ! NILP (Fget (def, Qmenu_alias)))
6121 def = XSYMBOL (def)->function;
6122 tem = Fwhere_is_internal (def, Qnil, Qt, Qnil);
6123 XCONS (cachelist)->car = tem;
6124 if (NILP (tem))
6126 XCONS (cachelist)->cdr = Qnil;
6127 chkcache = 0;
6130 else if (!NILP (keyhint) && !NILP (XCONS (cachelist)->car))
6132 tem = XCONS (cachelist)->car;
6133 chkcache = 1;
6136 newcache = chkcache;
6137 if (chkcache)
6139 tem = Fkey_description (tem);
6140 if (CONSP (prefix))
6142 if (STRINGP (XCONS (prefix)->car))
6143 tem = concat2 (XCONS (prefix)->car, tem);
6144 if (STRINGP (XCONS (prefix)->cdr))
6145 tem = concat2 (tem, XCONS (prefix)->cdr);
6147 XCONS (cachelist)->cdr = tem;
6151 tem = XCONS (cachelist)->cdr;
6152 if (newcache && !NILP (tem))
6154 tem = concat3 (build_string (" ("), tem, build_string (")"));
6155 XCONS (cachelist)->cdr = tem;
6158 /* If we only want to precompute equivalent key bindings, stop here. */
6159 if (notreal)
6160 return 1;
6162 /* If we have an equivalent key binding, use that. */
6163 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ] = tem;
6165 /* Include this when menu help is implemented.
6166 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP];
6167 if (!(NILP (tem) || STRINGP (tem)))
6169 tem = menu_item_eval_property (tem);
6170 if (!STRINGP (tem))
6171 tem = Qnil;
6172 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP] = tem;
6176 /* Handle radio buttons or toggle boxes. */
6177 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
6178 if (!NILP (tem))
6179 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]
6180 = menu_item_eval_property (tem);
6182 return 1;
6185 /* Read a character using menus based on maps in the array MAPS.
6186 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
6187 Return t if we displayed a menu but the user rejected it.
6189 PREV_EVENT is the previous input event, or nil if we are reading
6190 the first event of a key sequence.
6192 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
6193 if we used a mouse menu to read the input, or zero otherwise. If
6194 USED_MOUSE_MENU is null, we don't dereference it.
6196 The prompting is done based on the prompt-string of the map
6197 and the strings associated with various map elements.
6199 This can be done with X menus or with menus put in the minibuf.
6200 These are done in different ways, depending on how the input will be read.
6201 Menus using X are done after auto-saving in read-char, getting the input
6202 event from Fx_popup_menu; menus using the minibuf use read_char recursively
6203 and do auto-saving in the inner call of read_char. */
6205 static Lisp_Object
6206 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
6207 int nmaps;
6208 Lisp_Object *maps;
6209 Lisp_Object prev_event;
6210 int *used_mouse_menu;
6212 int mapno;
6213 register Lisp_Object name;
6214 Lisp_Object rest, vector;
6216 if (used_mouse_menu)
6217 *used_mouse_menu = 0;
6219 /* Use local over global Menu maps */
6221 if (! menu_prompting)
6222 return Qnil;
6224 /* Optionally disregard all but the global map. */
6225 if (inhibit_local_menu_bar_menus)
6227 maps += (nmaps - 1);
6228 nmaps = 1;
6231 /* Get the menu name from the first map that has one (a prompt string). */
6232 for (mapno = 0; mapno < nmaps; mapno++)
6234 name = map_prompt (maps[mapno]);
6235 if (!NILP (name))
6236 break;
6239 /* If we don't have any menus, just read a character normally. */
6240 if (mapno >= nmaps)
6241 return Qnil;
6243 #ifdef HAVE_MENUS
6244 /* If we got to this point via a mouse click,
6245 use a real menu for mouse selection. */
6246 if (EVENT_HAS_PARAMETERS (prev_event)
6247 && !EQ (XCONS (prev_event)->car, Qmenu_bar))
6249 /* Display the menu and get the selection. */
6250 Lisp_Object *realmaps
6251 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
6252 Lisp_Object value;
6253 int nmaps1 = 0;
6255 /* Use the maps that are not nil. */
6256 for (mapno = 0; mapno < nmaps; mapno++)
6257 if (!NILP (maps[mapno]))
6258 realmaps[nmaps1++] = maps[mapno];
6260 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
6261 if (CONSP (value))
6263 Lisp_Object tem;
6265 record_menu_key (XCONS (value)->car);
6267 /* If we got multiple events, unread all but
6268 the first.
6269 There is no way to prevent those unread events
6270 from showing up later in last_nonmenu_event.
6271 So turn symbol and integer events into lists,
6272 to indicate that they came from a mouse menu,
6273 so that when present in last_nonmenu_event
6274 they won't confuse things. */
6275 for (tem = XCONS (value)->cdr; !NILP (tem);
6276 tem = XCONS (tem)->cdr)
6278 record_menu_key (XCONS (tem)->car);
6279 if (SYMBOLP (XCONS (tem)->car)
6280 || INTEGERP (XCONS (tem)->car))
6281 XCONS (tem)->car
6282 = Fcons (XCONS (tem)->car, Qnil);
6285 /* If we got more than one event, put all but the first
6286 onto this list to be read later.
6287 Return just the first event now. */
6288 Vunread_command_events
6289 = nconc2 (XCONS (value)->cdr, Vunread_command_events);
6290 value = XCONS (value)->car;
6292 else if (NILP (value))
6293 value = Qt;
6294 if (used_mouse_menu)
6295 *used_mouse_menu = 1;
6296 return value;
6298 #endif /* HAVE_MENUS */
6299 return Qnil ;
6302 /* Buffer in use so far for the minibuf prompts for menu keymaps.
6303 We make this bigger when necessary, and never free it. */
6304 static char *read_char_minibuf_menu_text;
6305 /* Size of that buffer. */
6306 static int read_char_minibuf_menu_width;
6308 static Lisp_Object
6309 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
6310 int commandflag ;
6311 int nmaps;
6312 Lisp_Object *maps;
6314 int mapno;
6315 register Lisp_Object name;
6316 int nlength;
6317 int width = FRAME_WIDTH (selected_frame) - 4;
6318 int idx = -1;
6319 int nobindings = 1;
6320 Lisp_Object rest, vector;
6321 char *menu;
6323 if (! menu_prompting)
6324 return Qnil;
6326 /* Make sure we have a big enough buffer for the menu text. */
6327 if (read_char_minibuf_menu_text == 0)
6329 read_char_minibuf_menu_width = width + 4;
6330 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
6332 else if (width + 4 > read_char_minibuf_menu_width)
6334 read_char_minibuf_menu_width = width + 4;
6335 read_char_minibuf_menu_text
6336 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
6338 menu = read_char_minibuf_menu_text;
6340 /* Get the menu name from the first map that has one (a prompt string). */
6341 for (mapno = 0; mapno < nmaps; mapno++)
6343 name = map_prompt (maps[mapno]);
6344 if (!NILP (name))
6345 break;
6348 /* If we don't have any menus, just read a character normally. */
6349 if (mapno >= nmaps)
6350 return Qnil;
6352 /* Prompt string always starts with map's prompt, and a space. */
6353 strcpy (menu, XSTRING (name)->data);
6354 nlength = STRING_BYTES (XSTRING (name));
6355 menu[nlength++] = ':';
6356 menu[nlength++] = ' ';
6357 menu[nlength] = 0;
6359 /* Start prompting at start of first map. */
6360 mapno = 0;
6361 rest = maps[mapno];
6363 /* Present the documented bindings, a line at a time. */
6364 while (1)
6366 int notfirst = 0;
6367 int i = nlength;
6368 Lisp_Object obj;
6369 int ch;
6370 Lisp_Object orig_defn_macro;
6372 /* Loop over elements of map. */
6373 while (i < width)
6375 Lisp_Object elt;
6377 /* If reached end of map, start at beginning of next map. */
6378 if (NILP (rest))
6380 mapno++;
6381 /* At end of last map, wrap around to first map if just starting,
6382 or end this line if already have something on it. */
6383 if (mapno == nmaps)
6385 mapno = 0;
6386 if (notfirst || nobindings) break;
6388 rest = maps[mapno];
6391 /* Look at the next element of the map. */
6392 if (idx >= 0)
6393 elt = XVECTOR (vector)->contents[idx];
6394 else
6395 elt = Fcar_safe (rest);
6397 if (idx < 0 && VECTORP (elt))
6399 /* If we found a dense table in the keymap,
6400 advanced past it, but start scanning its contents. */
6401 rest = Fcdr_safe (rest);
6402 vector = elt;
6403 idx = 0;
6405 else
6407 /* An ordinary element. */
6408 Lisp_Object event, tem;
6410 if (idx < 0)
6412 event = Fcar_safe (elt); /* alist */
6413 elt = Fcdr_safe (elt);
6415 else
6417 XSETINT (event, idx); /* vector */
6420 /* Ignore the element if it has no prompt string. */
6421 if (INTEGERP (event) && parse_menu_item (elt, 0, -1))
6423 /* 1 if the char to type matches the string. */
6424 int char_matches;
6425 Lisp_Object upcased_event, downcased_event;
6426 Lisp_Object desc;
6427 Lisp_Object s
6428 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
6430 upcased_event = Fupcase (event);
6431 downcased_event = Fdowncase (event);
6432 char_matches = (XINT (upcased_event) == XSTRING (s)->data[0]
6433 || XINT (downcased_event) == XSTRING (s)->data[0]);
6434 if (! char_matches)
6435 desc = Fsingle_key_description (event);
6438 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
6439 if (!NILP (tem))
6440 /* Insert equivalent keybinding. */
6441 s = concat2 (s, tem);
6444 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
6445 if (EQ (tem, QCradio) || EQ (tem, QCtoggle))
6447 /* Insert button prefix. */
6448 Lisp_Object selected
6449 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
6450 if (EQ (tem, QCradio))
6451 tem = build_string (NILP (selected) ? "(*) " : "( ) ");
6452 else
6453 tem = build_string (NILP (selected) ? "[X] " : "[ ] ");
6454 s = concat2 (tem, s);
6458 /* If we have room for the prompt string, add it to this line.
6459 If this is the first on the line, always add it. */
6460 if ((XSTRING (s)->size + i + 2
6461 + (char_matches ? 0 : XSTRING (desc)->size + 3))
6462 < width
6463 || !notfirst)
6465 int thiswidth;
6467 /* Punctuate between strings. */
6468 if (notfirst)
6470 strcpy (menu + i, ", ");
6471 i += 2;
6473 notfirst = 1;
6474 nobindings = 0 ;
6476 /* If the char to type doesn't match the string's
6477 first char, explicitly show what char to type. */
6478 if (! char_matches)
6480 /* Add as much of string as fits. */
6481 thiswidth = XSTRING (desc)->size;
6482 if (thiswidth + i > width)
6483 thiswidth = width - i;
6484 bcopy (XSTRING (desc)->data, menu + i, thiswidth);
6485 i += thiswidth;
6486 strcpy (menu + i, " = ");
6487 i += 3;
6490 /* Add as much of string as fits. */
6491 thiswidth = XSTRING (s)->size;
6492 if (thiswidth + i > width)
6493 thiswidth = width - i;
6494 bcopy (XSTRING (s)->data, menu + i, thiswidth);
6495 i += thiswidth;
6496 menu[i] = 0;
6498 else
6500 /* If this element does not fit, end the line now,
6501 and save the element for the next line. */
6502 strcpy (menu + i, "...");
6503 break;
6507 /* Move past this element. */
6508 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
6509 /* Handle reaching end of dense table. */
6510 idx = -1;
6511 if (idx >= 0)
6512 idx++;
6513 else
6514 rest = Fcdr_safe (rest);
6518 /* Prompt with that and read response. */
6519 message2_nolog (menu, strlen (menu),
6520 ! NILP (current_buffer->enable_multibyte_characters));
6522 /* Make believe its not a keyboard macro in case the help char
6523 is pressed. Help characters are not recorded because menu prompting
6524 is not used on replay.
6526 orig_defn_macro = current_kboard->defining_kbd_macro;
6527 current_kboard->defining_kbd_macro = Qnil;
6529 obj = read_char (commandflag, 0, 0, Qnil, 0);
6530 while (BUFFERP (obj));
6531 current_kboard->defining_kbd_macro = orig_defn_macro;
6533 if (!INTEGERP (obj))
6534 return obj;
6535 else
6536 ch = XINT (obj);
6538 if (! EQ (obj, menu_prompt_more_char)
6539 && (!INTEGERP (menu_prompt_more_char)
6540 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
6542 if (!NILP (current_kboard->defining_kbd_macro))
6543 store_kbd_macro_char (obj);
6544 return obj;
6546 /* Help char - go round again */
6550 /* Reading key sequences. */
6552 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
6553 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
6554 keymap, or nil otherwise. Return the index of the first keymap in
6555 which KEY has any binding, or NMAPS if no map has a binding.
6557 If KEY is a meta ASCII character, treat it like meta-prefix-char
6558 followed by the corresponding non-meta character. Keymaps in
6559 CURRENT with non-prefix bindings for meta-prefix-char become nil in
6560 NEXT.
6562 If KEY has no bindings in any of the CURRENT maps, NEXT is left
6563 unmodified.
6565 NEXT may be the same array as CURRENT. */
6567 static int
6568 follow_key (key, nmaps, current, defs, next)
6569 Lisp_Object key;
6570 Lisp_Object *current, *defs, *next;
6571 int nmaps;
6573 int i, first_binding;
6574 int did_meta = 0;
6576 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
6577 followed by the corresponding non-meta character.
6578 Put the results into DEFS, since we are going to alter that anyway.
6579 Do not alter CURRENT or NEXT. */
6580 if (INTEGERP (key) && (XINT (key) & CHAR_META))
6582 for (i = 0; i < nmaps; i++)
6583 if (! NILP (current[i]))
6585 Lisp_Object def;
6586 def = get_keyelt (access_keymap (current[i],
6587 meta_prefix_char, 1, 0), 0);
6589 /* Note that since we pass the resulting bindings through
6590 get_keymap_1, non-prefix bindings for meta-prefix-char
6591 disappear. */
6592 defs[i] = get_keymap_1 (def, 0, 1);
6594 else
6595 defs[i] = Qnil;
6597 did_meta = 1;
6598 XSETINT (key, XFASTINT (key) & ~CHAR_META);
6601 first_binding = nmaps;
6602 for (i = nmaps - 1; i >= 0; i--)
6604 if (! NILP (current[i]))
6606 Lisp_Object map;
6607 if (did_meta)
6608 map = defs[i];
6609 else
6610 map = current[i];
6612 defs[i] = get_keyelt (access_keymap (map, key, 1, 0), 0);
6613 if (! NILP (defs[i]))
6614 first_binding = i;
6616 else
6617 defs[i] = Qnil;
6620 /* Given the set of bindings we've found, produce the next set of maps. */
6621 if (first_binding < nmaps)
6622 for (i = 0; i < nmaps; i++)
6623 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
6625 return first_binding;
6628 /* Read a sequence of keys that ends with a non prefix character,
6629 storing it in KEYBUF, a buffer of size BUFSIZE.
6630 Prompt with PROMPT.
6631 Return the length of the key sequence stored.
6632 Return -1 if the user rejected a command menu.
6634 Echo starting immediately unless `prompt' is 0.
6636 Where a key sequence ends depends on the currently active keymaps.
6637 These include any minor mode keymaps active in the current buffer,
6638 the current buffer's local map, and the global map.
6640 If a key sequence has no other bindings, we check Vfunction_key_map
6641 to see if some trailing subsequence might be the beginning of a
6642 function key's sequence. If so, we try to read the whole function
6643 key, and substitute its symbolic name into the key sequence.
6645 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
6646 `double-' events into similar click events, if that would make them
6647 bound. We try to turn `triple-' events first into `double-' events,
6648 then into clicks.
6650 If we get a mouse click in a mode line, vertical divider, or other
6651 non-text area, we treat the click as if it were prefixed by the
6652 symbol denoting that area - `mode-line', `vertical-line', or
6653 whatever.
6655 If the sequence starts with a mouse click, we read the key sequence
6656 with respect to the buffer clicked on, not the current buffer.
6658 If the user switches frames in the midst of a key sequence, we put
6659 off the switch-frame event until later; the next call to
6660 read_char will return it.
6662 If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
6663 from the selected window's buffer. */
6665 static int
6666 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
6667 can_return_switch_frame, fix_current_buffer)
6668 Lisp_Object *keybuf;
6669 int bufsize;
6670 Lisp_Object prompt;
6671 int dont_downcase_last;
6672 int can_return_switch_frame;
6673 int fix_current_buffer;
6675 int count = specpdl_ptr - specpdl;
6677 /* How many keys there are in the current key sequence. */
6678 int t;
6680 /* The length of the echo buffer when we started reading, and
6681 the length of this_command_keys when we started reading. */
6682 int echo_start;
6683 int keys_start;
6685 /* The number of keymaps we're scanning right now, and the number of
6686 keymaps we have allocated space for. */
6687 int nmaps;
6688 int nmaps_allocated = 0;
6690 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
6691 the current keymaps. */
6692 Lisp_Object *defs;
6694 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
6695 in the current keymaps, or nil where it is not a prefix. */
6696 Lisp_Object *submaps;
6698 /* The local map to start out with at start of key sequence. */
6699 Lisp_Object orig_local_map;
6701 /* 1 if we have already considered switching to the local-map property
6702 of the place where a mouse click occurred. */
6703 int localized_local_map = 0;
6705 /* The index in defs[] of the first keymap that has a binding for
6706 this key sequence. In other words, the lowest i such that
6707 defs[i] is non-nil. */
6708 int first_binding;
6710 /* If t < mock_input, then KEYBUF[t] should be read as the next
6711 input key.
6713 We use this to recover after recognizing a function key. Once we
6714 realize that a suffix of the current key sequence is actually a
6715 function key's escape sequence, we replace the suffix with the
6716 function key's binding from Vfunction_key_map. Now keybuf
6717 contains a new and different key sequence, so the echo area,
6718 this_command_keys, and the submaps and defs arrays are wrong. In
6719 this situation, we set mock_input to t, set t to 0, and jump to
6720 restart_sequence; the loop will read keys from keybuf up until
6721 mock_input, thus rebuilding the state; and then it will resume
6722 reading characters from the keyboard. */
6723 int mock_input = 0;
6725 /* If the sequence is unbound in submaps[], then
6726 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
6727 and fkey_map is its binding.
6729 These might be > t, indicating that all function key scanning
6730 should hold off until t reaches them. We do this when we've just
6731 recognized a function key, to avoid searching for the function
6732 key's again in Vfunction_key_map. */
6733 int fkey_start = 0, fkey_end = 0;
6734 Lisp_Object fkey_map;
6736 /* Likewise, for key_translation_map. */
6737 int keytran_start = 0, keytran_end = 0;
6738 Lisp_Object keytran_map;
6740 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
6741 we put it off for later. While we're reading, we keep the event here. */
6742 Lisp_Object delayed_switch_frame;
6744 /* See the comment below... */
6745 #if defined (GOBBLE_FIRST_EVENT)
6746 Lisp_Object first_event;
6747 #endif
6749 Lisp_Object original_uppercase;
6750 int original_uppercase_position = -1;
6752 /* Gets around Microsoft compiler limitations. */
6753 int dummyflag = 0;
6755 struct buffer *starting_buffer;
6757 /* Nonzero if we seem to have got the beginning of a binding
6758 in function_key_map. */
6759 int function_key_possible = 0;
6760 int key_translation_possible = 0;
6762 /* Save the status of key translation before each step,
6763 so that we can restore this after downcasing. */
6764 Lisp_Object prev_fkey_map;
6765 int prev_fkey_start;
6766 int prev_fkey_end;
6768 Lisp_Object prev_keytran_map;
6769 int prev_keytran_start;
6770 int prev_keytran_end;
6772 int junk;
6774 raw_keybuf_count = 0;
6776 last_nonmenu_event = Qnil;
6778 delayed_switch_frame = Qnil;
6779 fkey_map = Vfunction_key_map;
6780 keytran_map = Vkey_translation_map;
6782 /* If there is no function-key-map, turn off function key scanning. */
6783 if (NILP (Fkeymapp (Vfunction_key_map)))
6784 fkey_start = fkey_end = bufsize + 1;
6786 /* If there is no key-translation-map, turn off scanning. */
6787 if (NILP (Fkeymapp (Vkey_translation_map)))
6788 keytran_start = keytran_end = bufsize + 1;
6790 if (INTERACTIVE)
6792 if (!NILP (prompt))
6793 echo_prompt (XSTRING (prompt)->data);
6794 else if (cursor_in_echo_area && echo_keystrokes)
6795 /* This doesn't put in a dash if the echo buffer is empty, so
6796 you don't always see a dash hanging out in the minibuffer. */
6797 echo_dash ();
6800 /* Record the initial state of the echo area and this_command_keys;
6801 we will need to restore them if we replay a key sequence. */
6802 if (INTERACTIVE)
6803 echo_start = echo_length ();
6804 keys_start = this_command_key_count;
6805 this_single_command_key_start = keys_start;
6807 #if defined (GOBBLE_FIRST_EVENT)
6808 /* This doesn't quite work, because some of the things that read_char
6809 does cannot safely be bypassed. It seems too risky to try to make
6810 this work right. */
6812 /* Read the first char of the sequence specially, before setting
6813 up any keymaps, in case a filter runs and switches buffers on us. */
6814 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
6815 &junk);
6816 #endif /* GOBBLE_FIRST_EVENT */
6818 orig_local_map = get_local_map (PT, current_buffer);
6820 /* We jump here when the key sequence has been thoroughly changed, and
6821 we need to rescan it starting from the beginning. When we jump here,
6822 keybuf[0..mock_input] holds the sequence we should reread. */
6823 replay_sequence:
6825 starting_buffer = current_buffer;
6826 function_key_possible = 0;
6827 key_translation_possible = 0;
6829 /* Build our list of keymaps.
6830 If we recognize a function key and replace its escape sequence in
6831 keybuf with its symbol, or if the sequence starts with a mouse
6832 click and we need to switch buffers, we jump back here to rebuild
6833 the initial keymaps from the current buffer. */
6835 Lisp_Object *maps;
6837 if (!NILP (current_kboard->Voverriding_terminal_local_map)
6838 || !NILP (Voverriding_local_map))
6840 if (3 > nmaps_allocated)
6842 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
6843 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
6844 nmaps_allocated = 3;
6846 nmaps = 0;
6847 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6848 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6849 if (!NILP (Voverriding_local_map))
6850 submaps[nmaps++] = Voverriding_local_map;
6852 else
6854 nmaps = current_minor_maps (0, &maps);
6855 if (nmaps + 2 > nmaps_allocated)
6857 submaps = (Lisp_Object *) alloca ((nmaps+2) * sizeof (submaps[0]));
6858 defs = (Lisp_Object *) alloca ((nmaps+2) * sizeof (defs[0]));
6859 nmaps_allocated = nmaps + 2;
6861 bcopy (maps, submaps, nmaps * sizeof (submaps[0]));
6862 #ifdef USE_TEXT_PROPERTIES
6863 submaps[nmaps++] = orig_local_map;
6864 #else
6865 submaps[nmaps++] = current_buffer->keymap;
6866 #endif
6868 submaps[nmaps++] = current_global_map;
6871 /* Find an accurate initial value for first_binding. */
6872 for (first_binding = 0; first_binding < nmaps; first_binding++)
6873 if (! NILP (submaps[first_binding]))
6874 break;
6876 /* Start from the beginning in keybuf. */
6877 t = 0;
6879 /* These are no-ops the first time through, but if we restart, they
6880 revert the echo area and this_command_keys to their original state. */
6881 this_command_key_count = keys_start;
6882 if (INTERACTIVE && t < mock_input)
6883 echo_truncate (echo_start);
6885 /* If the best binding for the current key sequence is a keymap, or
6886 we may be looking at a function key's escape sequence, keep on
6887 reading. */
6888 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
6889 || (first_binding >= nmaps
6890 && fkey_start < t
6891 /* mock input is never part of a function key's sequence. */
6892 && mock_input <= fkey_start)
6893 || (first_binding >= nmaps
6894 && keytran_start < t && key_translation_possible)
6895 /* Don't return in the middle of a possible function key sequence,
6896 if the only bindings we found were via case conversion.
6897 Thus, if ESC O a has a function-key-map translation
6898 and ESC o has a binding, don't return after ESC O,
6899 so that we can translate ESC O plus the next character. */
6902 Lisp_Object key;
6903 int used_mouse_menu = 0;
6905 /* Where the last real key started. If we need to throw away a
6906 key that has expanded into more than one element of keybuf
6907 (say, a mouse click on the mode line which is being treated
6908 as [mode-line (mouse-...)], then we backtrack to this point
6909 of keybuf. */
6910 int last_real_key_start;
6912 /* These variables are analogous to echo_start and keys_start;
6913 while those allow us to restart the entire key sequence,
6914 echo_local_start and keys_local_start allow us to throw away
6915 just one key. */
6916 int echo_local_start, keys_local_start, local_first_binding;
6918 if (t >= bufsize)
6919 error ("Key sequence too long");
6921 if (INTERACTIVE)
6922 echo_local_start = echo_length ();
6923 keys_local_start = this_command_key_count;
6924 local_first_binding = first_binding;
6926 replay_key:
6927 /* These are no-ops, unless we throw away a keystroke below and
6928 jumped back up to replay_key; in that case, these restore the
6929 variables to their original state, allowing us to replay the
6930 loop. */
6931 if (INTERACTIVE && t < mock_input)
6932 echo_truncate (echo_local_start);
6933 this_command_key_count = keys_local_start;
6934 first_binding = local_first_binding;
6936 /* By default, assume each event is "real". */
6937 last_real_key_start = t;
6939 /* Does mock_input indicate that we are re-reading a key sequence? */
6940 if (t < mock_input)
6942 key = keybuf[t];
6943 add_command_key (key);
6944 if (echo_keystrokes)
6945 echo_char (key);
6948 /* If not, we should actually read a character. */
6949 else
6951 struct buffer *buf = current_buffer;
6954 #ifdef MULTI_KBOARD
6955 KBOARD *interrupted_kboard = current_kboard;
6956 struct frame *interrupted_frame = selected_frame;
6957 if (setjmp (wrong_kboard_jmpbuf))
6959 if (!NILP (delayed_switch_frame))
6961 interrupted_kboard->kbd_queue
6962 = Fcons (delayed_switch_frame,
6963 interrupted_kboard->kbd_queue);
6964 delayed_switch_frame = Qnil;
6966 while (t > 0)
6967 interrupted_kboard->kbd_queue
6968 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
6970 /* If the side queue is non-empty, ensure it begins with a
6971 switch-frame, so we'll replay it in the right context. */
6972 if (CONSP (interrupted_kboard->kbd_queue)
6973 && (key = XCONS (interrupted_kboard->kbd_queue)->car,
6974 !(EVENT_HAS_PARAMETERS (key)
6975 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
6976 Qswitch_frame))))
6978 Lisp_Object frame;
6979 XSETFRAME (frame, interrupted_frame);
6980 interrupted_kboard->kbd_queue
6981 = Fcons (make_lispy_switch_frame (frame),
6982 interrupted_kboard->kbd_queue);
6984 mock_input = 0;
6985 orig_local_map = get_local_map (PT, current_buffer);
6986 goto replay_sequence;
6988 #endif
6989 key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
6990 &used_mouse_menu);
6993 /* read_char returns t when it shows a menu and the user rejects it.
6994 Just return -1. */
6995 if (EQ (key, Qt))
6997 unbind_to (count, Qnil);
6998 return -1;
7001 /* read_char returns -1 at the end of a macro.
7002 Emacs 18 handles this by returning immediately with a
7003 zero, so that's what we'll do. */
7004 if (INTEGERP (key) && XINT (key) == -1)
7006 t = 0;
7007 /* The Microsoft C compiler can't handle the goto that
7008 would go here. */
7009 dummyflag = 1;
7010 break;
7013 /* If the current buffer has been changed from under us, the
7014 keymap may have changed, so replay the sequence. */
7015 if (BUFFERP (key))
7017 mock_input = t;
7018 /* Reset the current buffer from the selected window
7019 in case something changed the former and not the latter.
7020 This is to be more consistent with the behavior
7021 of the command_loop_1. */
7022 if (fix_current_buffer)
7024 if (! FRAME_LIVE_P (selected_frame))
7025 Fkill_emacs (Qnil);
7026 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
7027 Fset_buffer (XWINDOW (selected_window)->buffer);
7030 orig_local_map = get_local_map (PT, current_buffer);
7031 goto replay_sequence;
7034 /* If we have a quit that was typed in another frame, and
7035 quit_throw_to_read_char switched buffers,
7036 replay to get the right keymap. */
7037 if (XINT (key) == quit_char && current_buffer != starting_buffer)
7039 GROW_RAW_KEYBUF;
7040 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
7041 keybuf[t++] = key;
7042 mock_input = t;
7043 Vquit_flag = Qnil;
7044 orig_local_map = get_local_map (PT, current_buffer);
7045 goto replay_sequence;
7048 Vquit_flag = Qnil;
7050 if (EVENT_HAS_PARAMETERS (key)
7051 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), Qswitch_frame))
7053 /* If we're at the beginning of a key sequence, and the caller
7054 says it's okay, go ahead and return this event. If we're
7055 in the midst of a key sequence, delay it until the end. */
7056 if (t > 0 || !can_return_switch_frame)
7058 delayed_switch_frame = key;
7059 goto replay_key;
7063 GROW_RAW_KEYBUF;
7064 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
7067 /* Clicks in non-text areas get prefixed by the symbol
7068 in their CHAR-ADDRESS field. For example, a click on
7069 the mode line is prefixed by the symbol `mode-line'.
7071 Furthermore, key sequences beginning with mouse clicks
7072 are read using the keymaps of the buffer clicked on, not
7073 the current buffer. So we may have to switch the buffer
7074 here.
7076 When we turn one event into two events, we must make sure
7077 that neither of the two looks like the original--so that,
7078 if we replay the events, they won't be expanded again.
7079 If not for this, such reexpansion could happen either here
7080 or when user programs play with this-command-keys. */
7081 if (EVENT_HAS_PARAMETERS (key))
7083 Lisp_Object kind;
7085 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
7086 if (EQ (kind, Qmouse_click))
7088 Lisp_Object window, posn;
7090 window = POSN_WINDOW (EVENT_START (key));
7091 posn = POSN_BUFFER_POSN (EVENT_START (key));
7092 if (CONSP (posn))
7094 /* We're looking at the second event of a
7095 sequence which we expanded before. Set
7096 last_real_key_start appropriately. */
7097 if (t > 0)
7098 last_real_key_start = t - 1;
7101 /* Key sequences beginning with mouse clicks are
7102 read using the keymaps in the buffer clicked on,
7103 not the current buffer. If we're at the
7104 beginning of a key sequence, switch buffers. */
7105 if (last_real_key_start == 0
7106 && WINDOWP (window)
7107 && BUFFERP (XWINDOW (window)->buffer)
7108 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
7110 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
7111 keybuf[t] = key;
7112 mock_input = t + 1;
7114 /* Arrange to go back to the original buffer once we're
7115 done reading the key sequence. Note that we can't
7116 use save_excursion_{save,restore} here, because they
7117 save point as well as the current buffer; we don't
7118 want to save point, because redisplay may change it,
7119 to accommodate a Fset_window_start or something. We
7120 don't want to do this at the top of the function,
7121 because we may get input from a subprocess which
7122 wants to change the selected window and stuff (say,
7123 emacsclient). */
7124 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
7126 if (! FRAME_LIVE_P (selected_frame))
7127 Fkill_emacs (Qnil);
7128 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
7129 orig_local_map = get_local_map (PT, current_buffer);
7130 goto replay_sequence;
7132 /* For a mouse click, get the local text-property keymap
7133 of the place clicked on, rather than point. */
7134 if (last_real_key_start == 0 && CONSP (XCONS (key)->cdr)
7135 && ! localized_local_map)
7137 Lisp_Object map_here, start, pos;
7139 localized_local_map = 1;
7140 start = EVENT_START (key);
7141 if (CONSP (start) && CONSP (XCONS (start)->cdr))
7143 pos = POSN_BUFFER_POSN (start);
7144 if (INTEGERP (pos)
7145 && XINT (pos) >= BEG && XINT (pos) <= Z)
7147 map_here = get_local_map (XINT (pos), current_buffer);
7148 if (!EQ (map_here, orig_local_map))
7150 orig_local_map = map_here;
7151 keybuf[t] = key;
7152 mock_input = t + 1;
7154 goto replay_sequence;
7160 /* Expand mode-line and scroll-bar events into two events:
7161 use posn as a fake prefix key. */
7162 if (SYMBOLP (posn))
7164 if (t + 1 >= bufsize)
7165 error ("Key sequence too long");
7166 keybuf[t] = posn;
7167 keybuf[t+1] = key;
7168 mock_input = t + 2;
7170 /* Zap the position in key, so we know that we've
7171 expanded it, and don't try to do so again. */
7172 POSN_BUFFER_POSN (EVENT_START (key))
7173 = Fcons (posn, Qnil);
7174 goto replay_key;
7177 else if (CONSP (XCONS (key)->cdr)
7178 && CONSP (EVENT_START (key))
7179 && CONSP (XCONS (EVENT_START (key))->cdr))
7181 Lisp_Object posn;
7183 posn = POSN_BUFFER_POSN (EVENT_START (key));
7184 /* Handle menu-bar events:
7185 insert the dummy prefix event `menu-bar'. */
7186 if (EQ (posn, Qmenu_bar))
7188 if (t + 1 >= bufsize)
7189 error ("Key sequence too long");
7190 keybuf[t] = posn;
7191 keybuf[t+1] = key;
7193 /* Zap the position in key, so we know that we've
7194 expanded it, and don't try to do so again. */
7195 POSN_BUFFER_POSN (EVENT_START (key))
7196 = Fcons (posn, Qnil);
7198 mock_input = t + 2;
7199 goto replay_sequence;
7201 else if (CONSP (posn))
7203 /* We're looking at the second event of a
7204 sequence which we expanded before. Set
7205 last_real_key_start appropriately. */
7206 if (last_real_key_start == t && t > 0)
7207 last_real_key_start = t - 1;
7212 /* We have finally decided that KEY is something we might want
7213 to look up. */
7214 first_binding = (follow_key (key,
7215 nmaps - first_binding,
7216 submaps + first_binding,
7217 defs + first_binding,
7218 submaps + first_binding)
7219 + first_binding);
7221 /* If KEY wasn't bound, we'll try some fallbacks. */
7222 if (first_binding >= nmaps)
7224 Lisp_Object head;
7226 head = EVENT_HEAD (key);
7227 if (help_char_p (head) && t > 0)
7229 read_key_sequence_cmd = Vprefix_help_command;
7230 keybuf[t++] = key;
7231 last_nonmenu_event = key;
7232 /* The Microsoft C compiler can't handle the goto that
7233 would go here. */
7234 dummyflag = 1;
7235 break;
7238 if (SYMBOLP (head))
7240 Lisp_Object breakdown;
7241 int modifiers;
7243 breakdown = parse_modifiers (head);
7244 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
7245 /* Attempt to reduce an unbound mouse event to a simpler
7246 event that is bound:
7247 Drags reduce to clicks.
7248 Double-clicks reduce to clicks.
7249 Triple-clicks reduce to double-clicks, then to clicks.
7250 Down-clicks are eliminated.
7251 Double-downs reduce to downs, then are eliminated.
7252 Triple-downs reduce to double-downs, then to downs,
7253 then are eliminated. */
7254 if (modifiers & (down_modifier | drag_modifier
7255 | double_modifier | triple_modifier))
7257 while (modifiers & (down_modifier | drag_modifier
7258 | double_modifier | triple_modifier))
7260 Lisp_Object new_head, new_click;
7261 if (modifiers & triple_modifier)
7262 modifiers ^= (double_modifier | triple_modifier);
7263 else if (modifiers & double_modifier)
7264 modifiers &= ~double_modifier;
7265 else if (modifiers & drag_modifier)
7266 modifiers &= ~drag_modifier;
7267 else
7269 /* Dispose of this `down' event by simply jumping
7270 back to replay_key, to get another event.
7272 Note that if this event came from mock input,
7273 then just jumping back to replay_key will just
7274 hand it to us again. So we have to wipe out any
7275 mock input.
7277 We could delete keybuf[t] and shift everything
7278 after that to the left by one spot, but we'd also
7279 have to fix up any variable that points into
7280 keybuf, and shifting isn't really necessary
7281 anyway.
7283 Adding prefixes for non-textual mouse clicks
7284 creates two characters of mock input, and both
7285 must be thrown away. If we're only looking at
7286 the prefix now, we can just jump back to
7287 replay_key. On the other hand, if we've already
7288 processed the prefix, and now the actual click
7289 itself is giving us trouble, then we've lost the
7290 state of the keymaps we want to backtrack to, and
7291 we need to replay the whole sequence to rebuild
7294 Beyond that, only function key expansion could
7295 create more than two keys, but that should never
7296 generate mouse events, so it's okay to zero
7297 mock_input in that case too.
7299 Isn't this just the most wonderful code ever? */
7300 if (t == last_real_key_start)
7302 mock_input = 0;
7303 goto replay_key;
7305 else
7307 mock_input = last_real_key_start;
7308 goto replay_sequence;
7312 new_head
7313 = apply_modifiers (modifiers, XCONS (breakdown)->car);
7314 new_click
7315 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
7317 /* Look for a binding for this new key. follow_key
7318 promises that it didn't munge submaps the
7319 last time we called it, since key was unbound. */
7320 first_binding
7321 = (follow_key (new_click,
7322 nmaps - local_first_binding,
7323 submaps + local_first_binding,
7324 defs + local_first_binding,
7325 submaps + local_first_binding)
7326 + local_first_binding);
7328 /* If that click is bound, go for it. */
7329 if (first_binding < nmaps)
7331 key = new_click;
7332 break;
7334 /* Otherwise, we'll leave key set to the drag event. */
7340 keybuf[t++] = key;
7341 /* Normally, last_nonmenu_event gets the previous key we read.
7342 But when a mouse popup menu is being used,
7343 we don't update last_nonmenu_event; it continues to hold the mouse
7344 event that preceded the first level of menu. */
7345 if (!used_mouse_menu)
7346 last_nonmenu_event = key;
7348 /* Record what part of this_command_keys is the current key sequence. */
7349 this_single_command_key_start = this_command_key_count - t;
7351 prev_fkey_map = fkey_map;
7352 prev_fkey_start = fkey_start;
7353 prev_fkey_end = fkey_end;
7355 prev_keytran_map = keytran_map;
7356 prev_keytran_start = keytran_start;
7357 prev_keytran_end = keytran_end;
7359 /* If the sequence is unbound, see if we can hang a function key
7360 off the end of it. We only want to scan real keyboard input
7361 for function key sequences, so if mock_input says that we're
7362 re-reading old events, don't examine it. */
7363 if (first_binding >= nmaps
7364 && t >= mock_input)
7366 Lisp_Object fkey_next;
7368 /* Continue scan from fkey_end until we find a bound suffix.
7369 If we fail, increment fkey_start
7370 and start fkey_end from there. */
7371 while (fkey_end < t)
7373 Lisp_Object key;
7375 key = keybuf[fkey_end++];
7376 /* Look up meta-characters by prefixing them
7377 with meta_prefix_char. I hate this. */
7378 if (INTEGERP (key) && XINT (key) & meta_modifier)
7380 fkey_next
7381 = get_keymap_1
7382 (get_keyelt
7383 (access_keymap (fkey_map, meta_prefix_char, 1, 0), 0),
7384 0, 1);
7385 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
7387 else
7388 fkey_next = fkey_map;
7390 fkey_next
7391 = get_keyelt (access_keymap (fkey_next, key, 1, 0), 0);
7393 #if 0 /* I didn't turn this on, because it might cause trouble
7394 for the mapping of return into C-m and tab into C-i. */
7395 /* Optionally don't map function keys into other things.
7396 This enables the user to redefine kp- keys easily. */
7397 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
7398 fkey_next = Qnil;
7399 #endif
7401 /* If the function key map gives a function, not an
7402 array, then call the function with no args and use
7403 its value instead. */
7404 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
7405 && fkey_end == t)
7407 struct gcpro gcpro1, gcpro2, gcpro3;
7408 Lisp_Object tem;
7409 tem = fkey_next;
7411 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
7412 fkey_next = call1 (fkey_next, prompt);
7413 UNGCPRO;
7414 /* If the function returned something invalid,
7415 barf--don't ignore it.
7416 (To ignore it safely, we would need to gcpro a bunch of
7417 other variables.) */
7418 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
7419 error ("Function in key-translation-map returns invalid key sequence");
7422 function_key_possible = ! NILP (fkey_next);
7424 /* If keybuf[fkey_start..fkey_end] is bound in the
7425 function key map and it's a suffix of the current
7426 sequence (i.e. fkey_end == t), replace it with
7427 the binding and restart with fkey_start at the end. */
7428 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
7429 && fkey_end == t)
7431 int len = XFASTINT (Flength (fkey_next));
7433 t = fkey_start + len;
7434 if (t >= bufsize)
7435 error ("Key sequence too long");
7437 if (VECTORP (fkey_next))
7438 bcopy (XVECTOR (fkey_next)->contents,
7439 keybuf + fkey_start,
7440 (t - fkey_start) * sizeof (keybuf[0]));
7441 else if (STRINGP (fkey_next))
7443 int i;
7445 for (i = 0; i < len; i++)
7446 XSETFASTINT (keybuf[fkey_start + i],
7447 XSTRING (fkey_next)->data[i]);
7450 mock_input = t;
7451 fkey_start = fkey_end = t;
7452 fkey_map = Vfunction_key_map;
7454 /* Do pass the results through key-translation-map. */
7455 keytran_start = keytran_end = 0;
7456 keytran_map = Vkey_translation_map;
7458 goto replay_sequence;
7461 fkey_map = get_keymap_1 (fkey_next, 0, 1);
7463 /* If we no longer have a bound suffix, try a new positions for
7464 fkey_start. */
7465 if (NILP (fkey_map))
7467 fkey_end = ++fkey_start;
7468 fkey_map = Vfunction_key_map;
7469 function_key_possible = 0;
7474 /* Look for this sequence in key-translation-map. */
7476 Lisp_Object keytran_next;
7478 /* Scan from keytran_end until we find a bound suffix. */
7479 while (keytran_end < t)
7481 Lisp_Object key;
7483 key = keybuf[keytran_end++];
7484 /* Look up meta-characters by prefixing them
7485 with meta_prefix_char. I hate this. */
7486 if (INTEGERP (key) && XINT (key) & meta_modifier)
7488 keytran_next
7489 = get_keymap_1
7490 (get_keyelt
7491 (access_keymap (keytran_map, meta_prefix_char, 1, 0), 0),
7492 0, 1);
7493 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
7495 else
7496 keytran_next = keytran_map;
7498 keytran_next
7499 = get_keyelt (access_keymap (keytran_next, key, 1, 0), 0);
7501 /* If the key translation map gives a function, not an
7502 array, then call the function with no args and use
7503 its value instead. */
7504 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
7505 && keytran_end == t)
7507 struct gcpro gcpro1, gcpro2, gcpro3;
7508 Lisp_Object tem;
7509 tem = keytran_next;
7511 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
7512 keytran_next = call1 (keytran_next, prompt);
7513 UNGCPRO;
7514 /* If the function returned something invalid,
7515 barf--don't ignore it.
7516 (To ignore it safely, we would need to gcpro a bunch of
7517 other variables.) */
7518 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
7519 error ("Function in key-translation-map returns invalid key sequence");
7522 key_translation_possible = ! NILP (keytran_next);
7524 /* If keybuf[keytran_start..keytran_end] is bound in the
7525 key translation map and it's a suffix of the current
7526 sequence (i.e. keytran_end == t), replace it with
7527 the binding and restart with keytran_start at the end. */
7528 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
7529 && keytran_end == t)
7531 int len = XFASTINT (Flength (keytran_next));
7533 t = keytran_start + len;
7534 if (t >= bufsize)
7535 error ("Key sequence too long");
7537 if (VECTORP (keytran_next))
7538 bcopy (XVECTOR (keytran_next)->contents,
7539 keybuf + keytran_start,
7540 (t - keytran_start) * sizeof (keybuf[0]));
7541 else if (STRINGP (keytran_next))
7543 int i;
7545 for (i = 0; i < len; i++)
7546 XSETFASTINT (keybuf[keytran_start + i],
7547 XSTRING (keytran_next)->data[i]);
7550 mock_input = t;
7551 keytran_start = keytran_end = t;
7552 keytran_map = Vkey_translation_map;
7554 /* Don't pass the results of key-translation-map
7555 through function-key-map. */
7556 fkey_start = fkey_end = t;
7557 fkey_map = Vkey_translation_map;
7559 goto replay_sequence;
7562 keytran_map = get_keymap_1 (keytran_next, 0, 1);
7564 /* If we no longer have a bound suffix, try a new positions for
7565 keytran_start. */
7566 if (NILP (keytran_map))
7568 keytran_end = ++keytran_start;
7569 keytran_map = Vkey_translation_map;
7570 key_translation_possible = 0;
7575 /* If KEY is not defined in any of the keymaps,
7576 and cannot be part of a function key or translation,
7577 and is an upper case letter
7578 use the corresponding lower-case letter instead. */
7579 if (first_binding == nmaps && ! function_key_possible
7580 && ! key_translation_possible
7581 && INTEGERP (key)
7582 && ((((XINT (key) & 0x3ffff)
7583 < XCHAR_TABLE (current_buffer->downcase_table)->size)
7584 && UPPERCASEP (XINT (key) & 0x3ffff))
7585 || (XINT (key) & shift_modifier)))
7587 Lisp_Object new_key;
7589 original_uppercase = key;
7590 original_uppercase_position = t - 1;
7592 if (XINT (key) & shift_modifier)
7593 XSETINT (new_key, XINT (key) & ~shift_modifier);
7594 else
7595 XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
7596 | (XINT (key) & ~0x3ffff)));
7598 /* We have to do this unconditionally, regardless of whether
7599 the lower-case char is defined in the keymaps, because they
7600 might get translated through function-key-map. */
7601 keybuf[t - 1] = new_key;
7602 mock_input = t;
7604 fkey_map = prev_fkey_map;
7605 fkey_start = prev_fkey_start;
7606 fkey_end = prev_fkey_end;
7608 keytran_map = prev_keytran_map;
7609 keytran_start = prev_keytran_start;
7610 keytran_end = prev_keytran_end;
7612 goto replay_sequence;
7614 /* If KEY is not defined in any of the keymaps,
7615 and cannot be part of a function key or translation,
7616 and is a shifted function key,
7617 use the corresponding unshifted function key instead. */
7618 if (first_binding == nmaps && ! function_key_possible
7619 && ! key_translation_possible
7620 && SYMBOLP (key))
7622 Lisp_Object breakdown;
7623 int modifiers;
7625 breakdown = parse_modifiers (key);
7626 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
7627 if (modifiers & shift_modifier)
7629 Lisp_Object new_key;
7631 original_uppercase = key;
7632 original_uppercase_position = t - 1;
7634 modifiers &= ~shift_modifier;
7635 new_key = apply_modifiers (modifiers,
7636 XCONS (breakdown)->car);
7638 keybuf[t - 1] = new_key;
7639 mock_input = t;
7641 fkey_map = prev_fkey_map;
7642 fkey_start = prev_fkey_start;
7643 fkey_end = prev_fkey_end;
7645 keytran_map = prev_keytran_map;
7646 keytran_start = prev_keytran_start;
7647 keytran_end = prev_keytran_end;
7649 goto replay_sequence;
7654 if (!dummyflag)
7655 read_key_sequence_cmd = (first_binding < nmaps
7656 ? defs[first_binding]
7657 : Qnil);
7659 unread_switch_frame = delayed_switch_frame;
7660 unbind_to (count, Qnil);
7662 /* Don't downcase the last character if the caller says don't.
7663 Don't downcase it if the result is undefined, either. */
7664 if ((dont_downcase_last || first_binding >= nmaps)
7665 && t - 1 == original_uppercase_position)
7666 keybuf[t - 1] = original_uppercase;
7668 /* Occasionally we fabricate events, perhaps by expanding something
7669 according to function-key-map, or by adding a prefix symbol to a
7670 mouse click in the scroll bar or modeline. In this cases, return
7671 the entire generated key sequence, even if we hit an unbound
7672 prefix or a definition before the end. This means that you will
7673 be able to push back the event properly, and also means that
7674 read-key-sequence will always return a logical unit.
7676 Better ideas? */
7677 for (; t < mock_input; t++)
7679 if (echo_keystrokes)
7680 echo_char (keybuf[t]);
7681 add_command_key (keybuf[t]);
7686 return t;
7689 #if 0 /* This doc string is too long for some compilers.
7690 This commented-out definition serves for DOC. */
7691 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
7692 "Read a sequence of keystrokes and return as a string or vector.\n\
7693 The sequence is sufficient to specify a non-prefix command in the\n\
7694 current local and global maps.\n\
7696 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
7697 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
7698 as a continuation of the previous key.\n\
7700 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
7701 convert the last event to lower case. (Normally any upper case event\n\
7702 is converted to lower case if the original event is undefined and the lower\n\
7703 case equivalent is defined.) A non-nil value is appropriate for reading\n\
7704 a key sequence to be defined.\n\
7706 A C-g typed while in this function is treated like any other character,\n\
7707 and `quit-flag' is not set.\n\
7709 If the key sequence starts with a mouse click, then the sequence is read\n\
7710 using the keymaps of the buffer of the window clicked in, not the buffer\n\
7711 of the selected window as normal.\n\
7712 ""\n\
7713 `read-key-sequence' drops unbound button-down events, since you normally\n\
7714 only care about the click or drag events which follow them. If a drag\n\
7715 or multi-click event is unbound, but the corresponding click event would\n\
7716 be bound, `read-key-sequence' turns the event into a click event at the\n\
7717 drag's starting position. This means that you don't have to distinguish\n\
7718 between click and drag, double, or triple events unless you want to.\n\
7720 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
7721 lines separating windows, and scroll bars with imaginary keys\n\
7722 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
7724 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
7725 function will process a switch-frame event if the user switches frames\n\
7726 before typing anything. If the user switches frames in the middle of a\n\
7727 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
7728 is nil, then the event will be put off until after the current key sequence.\n\
7730 `read-key-sequence' checks `function-key-map' for function key\n\
7731 sequences, where they wouldn't conflict with ordinary bindings. See\n\
7732 `function-key-map' for more details.\n\
7734 The optional fifth argument COMMAND-LOOP, if non-nil, means\n\
7735 that this key sequence is being read by something that will\n\
7736 read commands one after another. It should be nil if the caller\n\
7737 will read just one key sequence.")
7738 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame, command-loop)
7739 #endif
7741 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 5, 0,
7743 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
7744 command_loop)
7745 Lisp_Object prompt, continue_echo, dont_downcase_last;
7746 Lisp_Object can_return_switch_frame, command_loop;
7748 Lisp_Object keybuf[30];
7749 register int i;
7750 struct gcpro gcpro1, gcpro2;
7751 int count = specpdl_ptr - specpdl;
7753 if (!NILP (prompt))
7754 CHECK_STRING (prompt, 0);
7755 QUIT;
7757 specbind (Qinput_method_exit_on_first_char,
7758 (NILP (command_loop) ? Qt : Qnil));
7759 specbind (Qinput_method_use_echo_area,
7760 (NILP (command_loop) ? Qt : Qnil));
7762 bzero (keybuf, sizeof keybuf);
7763 GCPRO1 (keybuf[0]);
7764 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
7766 if (NILP (continue_echo))
7768 this_command_key_count = 0;
7769 this_single_command_key_start = 0;
7772 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
7773 prompt, ! NILP (dont_downcase_last),
7774 ! NILP (can_return_switch_frame), 0);
7776 if (i == -1)
7778 Vquit_flag = Qt;
7779 QUIT;
7781 UNGCPRO;
7782 return unbind_to (count, make_event_array (i, keybuf));
7785 DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
7786 Sread_key_sequence_vector, 1, 5, 0,
7787 "Like `read-key-sequence' but always return a vector.")
7788 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
7789 command_loop)
7790 Lisp_Object prompt, continue_echo, dont_downcase_last;
7791 Lisp_Object can_return_switch_frame, command_loop;
7793 Lisp_Object keybuf[30];
7794 register int i;
7795 struct gcpro gcpro1, gcpro2;
7796 int count = specpdl_ptr - specpdl;
7798 if (!NILP (prompt))
7799 CHECK_STRING (prompt, 0);
7800 QUIT;
7802 specbind (Qinput_method_exit_on_first_char,
7803 (NILP (command_loop) ? Qt : Qnil));
7804 specbind (Qinput_method_use_echo_area,
7805 (NILP (command_loop) ? Qt : Qnil));
7807 bzero (keybuf, sizeof keybuf);
7808 GCPRO1 (keybuf[0]);
7809 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
7811 if (NILP (continue_echo))
7813 this_command_key_count = 0;
7814 this_single_command_key_start = 0;
7817 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
7818 prompt, ! NILP (dont_downcase_last),
7819 ! NILP (can_return_switch_frame), 0);
7821 if (i == -1)
7823 Vquit_flag = Qt;
7824 QUIT;
7826 UNGCPRO;
7827 return unbind_to (count, Fvector (i, keybuf));
7830 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
7831 "Execute CMD as an editor command.\n\
7832 CMD must be a symbol that satisfies the `commandp' predicate.\n\
7833 Optional second arg RECORD-FLAG non-nil\n\
7834 means unconditionally put this command in `command-history'.\n\
7835 Otherwise, that is done only if an arg is read using the minibuffer.\n\
7836 The argument KEYS specifies the value to use instead of (this-command-keys)\n\
7837 when reading the arguments; if it is nil, (this-command-keys) is used.\n\
7838 The argument SPECIAL, if non-nil, means that this command is executing\n\
7839 a special event, so ignore the prefix argument and don't clear it.")
7840 (cmd, record_flag, keys, special)
7841 Lisp_Object cmd, record_flag, keys, special;
7843 register Lisp_Object final;
7844 register Lisp_Object tem;
7845 Lisp_Object prefixarg;
7846 struct backtrace backtrace;
7847 extern int debug_on_next_call;
7849 debug_on_next_call = 0;
7851 if (NILP (special))
7853 prefixarg = current_kboard->Vprefix_arg;
7854 Vcurrent_prefix_arg = prefixarg;
7855 current_kboard->Vprefix_arg = Qnil;
7857 else
7858 prefixarg = Qnil;
7860 if (SYMBOLP (cmd))
7862 tem = Fget (cmd, Qdisabled);
7863 if (!NILP (tem) && !NILP (Vrun_hooks))
7865 tem = Fsymbol_value (Qdisabled_command_hook);
7866 if (!NILP (tem))
7867 return call1 (Vrun_hooks, Qdisabled_command_hook);
7871 while (1)
7873 final = Findirect_function (cmd);
7875 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
7877 struct gcpro gcpro1, gcpro2;
7879 GCPRO2 (cmd, prefixarg);
7880 do_autoload (final, cmd);
7881 UNGCPRO;
7883 else
7884 break;
7887 if (STRINGP (final) || VECTORP (final))
7889 /* If requested, place the macro in the command history. For
7890 other sorts of commands, call-interactively takes care of
7891 this. */
7892 if (!NILP (record_flag))
7894 Vcommand_history
7895 = Fcons (Fcons (Qexecute_kbd_macro,
7896 Fcons (final, Fcons (prefixarg, Qnil))),
7897 Vcommand_history);
7899 /* Don't keep command history around forever. */
7900 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
7902 tem = Fnthcdr (Vhistory_length, Vcommand_history);
7903 if (CONSP (tem))
7904 XCONS (tem)->cdr = Qnil;
7908 return Fexecute_kbd_macro (final, prefixarg);
7911 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
7913 backtrace.next = backtrace_list;
7914 backtrace_list = &backtrace;
7915 backtrace.function = &Qcall_interactively;
7916 backtrace.args = &cmd;
7917 backtrace.nargs = 1;
7918 backtrace.evalargs = 0;
7920 tem = Fcall_interactively (cmd, record_flag, keys);
7922 backtrace_list = backtrace.next;
7923 return tem;
7925 return Qnil;
7928 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
7929 1, 1, "P",
7930 "Read function name, then read its arguments and call it.")
7931 (prefixarg)
7932 Lisp_Object prefixarg;
7934 Lisp_Object function;
7935 char buf[40];
7936 Lisp_Object saved_keys;
7937 Lisp_Object bindings, value;
7938 struct gcpro gcpro1, gcpro2;
7940 saved_keys = Fvector (this_command_key_count,
7941 XVECTOR (this_command_keys)->contents);
7942 buf[0] = 0;
7943 GCPRO2 (saved_keys, prefixarg);
7945 if (EQ (prefixarg, Qminus))
7946 strcpy (buf, "- ");
7947 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
7948 strcpy (buf, "C-u ");
7949 else if (CONSP (prefixarg) && INTEGERP (XCONS (prefixarg)->car))
7951 if (sizeof (int) == sizeof (EMACS_INT))
7952 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
7953 else if (sizeof (long) == sizeof (EMACS_INT))
7954 sprintf (buf, "%ld ", XINT (XCONS (prefixarg)->car));
7955 else
7956 abort ();
7958 else if (INTEGERP (prefixarg))
7960 if (sizeof (int) == sizeof (EMACS_INT))
7961 sprintf (buf, "%d ", XINT (prefixarg));
7962 else if (sizeof (long) == sizeof (EMACS_INT))
7963 sprintf (buf, "%ld ", XINT (prefixarg));
7964 else
7965 abort ();
7968 /* This isn't strictly correct if execute-extended-command
7969 is bound to anything else. Perhaps it should use
7970 this_command_keys? */
7971 strcat (buf, "M-x ");
7973 /* Prompt with buf, and then read a string, completing from and
7974 restricting to the set of all defined commands. Don't provide
7975 any initial input. Save the command read on the extended-command
7976 history list. */
7977 function = Fcompleting_read (build_string (buf),
7978 Vobarray, Qcommandp,
7979 Qt, Qnil, Qextended_command_history, Qnil,
7980 Qnil);
7982 if (STRINGP (function) && XSTRING (function)->size == 0)
7983 error ("No command name given");
7985 /* Set this_command_keys to the concatenation of saved_keys and
7986 function, followed by a RET. */
7988 struct Lisp_String *str;
7989 Lisp_Object *keys;
7990 int i;
7992 this_command_key_count = 0;
7993 this_single_command_key_start = 0;
7995 keys = XVECTOR (saved_keys)->contents;
7996 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
7997 add_command_key (keys[i]);
7999 str = XSTRING (function);
8000 for (i = 0; i < str->size; i++)
8001 add_command_key (Faref (function, make_number (i)));
8003 add_command_key (make_number ('\015'));
8006 UNGCPRO;
8008 function = Fintern (function, Qnil);
8009 current_kboard->Vprefix_arg = prefixarg;
8010 Vthis_command = function;
8011 real_this_command = function;
8013 /* If enabled, show which key runs this command. */
8014 if (!NILP (Vsuggest_key_bindings)
8015 && NILP (Vexecuting_macro)
8016 && SYMBOLP (function))
8017 bindings = Fwhere_is_internal (function, Voverriding_local_map,
8018 Qt, Qnil);
8019 else
8020 bindings = Qnil;
8022 value = Qnil;
8023 GCPRO2 (bindings, value);
8024 value = Fcommand_execute (function, Qt, Qnil, Qnil);
8026 /* If the command has a key binding, print it now. */
8027 if (!NILP (bindings)
8028 && ! (VECTORP (bindings) && EQ (Faref (bindings, make_number (0)),
8029 Qmouse_movement)))
8031 /* But first wait, and skip the message if there is input. */
8032 int delay_time;
8033 if (echo_area_glyphs != 0)
8034 /* This command displayed something in the echo area;
8035 so wait a few seconds, then display our suggestion message. */
8036 delay_time = (NUMBERP (Vsuggest_key_bindings)
8037 ? XINT (Vsuggest_key_bindings) : 2);
8038 else
8039 /* This command left the echo area empty,
8040 so display our message immediately. */
8041 delay_time = 0;
8043 if (!NILP (Fsit_for (make_number (delay_time), Qnil, Qnil))
8044 && ! CONSP (Vunread_command_events))
8046 Lisp_Object binding;
8047 char *newmessage;
8048 char *oldmessage = echo_area_glyphs;
8049 int oldmessage_len = echo_area_glyphs_length;
8050 int oldmultibyte = message_enable_multibyte;
8052 binding = Fkey_description (bindings);
8054 newmessage
8055 = (char *) alloca (XSYMBOL (function)->name->size
8056 + STRING_BYTES (XSTRING (binding))
8057 + 100);
8058 sprintf (newmessage, "You can run the command `%s' with %s",
8059 XSYMBOL (function)->name->data,
8060 XSTRING (binding)->data);
8061 message2_nolog (newmessage,
8062 strlen (newmessage),
8063 STRING_MULTIBYTE (binding));
8064 if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
8065 ? Vsuggest_key_bindings : make_number (2)),
8066 Qnil, Qnil)))
8067 message2_nolog (oldmessage, oldmessage_len, oldmultibyte);
8071 RETURN_UNGCPRO (value);
8074 /* Find the set of keymaps now active.
8075 Store into *MAPS_P a vector holding the various maps
8076 and return the number of them. The vector was malloc'd
8077 and the caller should free it. */
8080 current_active_maps (maps_p)
8081 Lisp_Object **maps_p;
8083 Lisp_Object *tmaps, *maps;
8084 int nmaps;
8086 /* Should overriding-terminal-local-map and overriding-local-map apply? */
8087 if (!NILP (Voverriding_local_map_menu_flag))
8089 /* Yes, use them (if non-nil) as well as the global map. */
8090 maps = (Lisp_Object *) xmalloc (3 * sizeof (maps[0]));
8091 nmaps = 0;
8092 if (!NILP (current_kboard->Voverriding_terminal_local_map))
8093 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
8094 if (!NILP (Voverriding_local_map))
8095 maps[nmaps++] = Voverriding_local_map;
8097 else
8099 /* No, so use major and minor mode keymaps. */
8100 nmaps = current_minor_maps (NULL, &tmaps);
8101 maps = (Lisp_Object *) xmalloc ((nmaps + 2) * sizeof (maps[0]));
8102 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
8103 #ifdef USE_TEXT_PROPERTIES
8104 maps[nmaps++] = get_local_map (PT, current_buffer);
8105 #else
8106 maps[nmaps++] = current_buffer->keymap;
8107 #endif
8109 maps[nmaps++] = current_global_map;
8111 *maps_p = maps;
8112 return nmaps;
8115 /* Return nonzero if input events are pending. */
8118 detect_input_pending ()
8120 if (!input_pending)
8121 get_input_pending (&input_pending, 0);
8123 return input_pending;
8126 /* Return nonzero if input events are pending, and run any pending timers. */
8129 detect_input_pending_run_timers (do_display)
8130 int do_display;
8132 int old_timers_run = timers_run;
8134 if (!input_pending)
8135 get_input_pending (&input_pending, 1);
8137 if (old_timers_run != timers_run && do_display)
8138 redisplay_preserve_echo_area ();
8140 return input_pending;
8143 /* This is called in some cases before a possible quit.
8144 It cases the next call to detect_input_pending to recompute input_pending.
8145 So calling this function unnecessarily can't do any harm. */
8147 void
8148 clear_input_pending ()
8150 input_pending = 0;
8153 /* Return nonzero if there are pending requeued events.
8154 This isn't used yet. The hope is to make wait_reading_process_input
8155 call it, and return return if it runs Lisp code that unreads something.
8156 The problem is, kbd_buffer_get_event needs to be fixed to know what
8157 to do in that case. It isn't trivial. */
8160 requeued_events_pending_p ()
8162 return (!NILP (Vunread_command_events) || unread_command_char != -1);
8166 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
8167 "T if command input is currently available with no waiting.\n\
8168 Actually, the value is nil only if we can be sure that no input is available.")
8171 if (!NILP (Vunread_command_events) || unread_command_char != -1)
8172 return (Qt);
8174 get_input_pending (&input_pending, 1);
8175 return input_pending > 0 ? Qt : Qnil;
8178 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
8179 "Return vector of last 100 events, not counting those from keyboard macros.")
8182 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
8183 Lisp_Object val;
8185 if (total_keys < NUM_RECENT_KEYS)
8186 return Fvector (total_keys, keys);
8187 else
8189 val = Fvector (NUM_RECENT_KEYS, keys);
8190 bcopy (keys + recent_keys_index,
8191 XVECTOR (val)->contents,
8192 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
8193 bcopy (keys,
8194 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
8195 recent_keys_index * sizeof (Lisp_Object));
8196 return val;
8200 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
8201 "Return the key sequence that invoked this command.\n\
8202 The value is a string or a vector.")
8205 return make_event_array (this_command_key_count,
8206 XVECTOR (this_command_keys)->contents);
8209 DEFUN ("this-command-keys-vector", Fthis_command_keys_vector, Sthis_command_keys_vector, 0, 0, 0,
8210 "Return the key sequence that invoked this command, as a vector.")
8213 return Fvector (this_command_key_count,
8214 XVECTOR (this_command_keys)->contents);
8217 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
8218 Sthis_single_command_keys, 0, 0, 0,
8219 "Return the key sequence that invoked this command.\n\
8220 Unlike `this-command-keys', this function's value\n\
8221 does not include prefix arguments.\n\
8222 The value is always a vector.")
8225 return Fvector (this_command_key_count
8226 - this_single_command_key_start,
8227 (XVECTOR (this_command_keys)->contents
8228 + this_single_command_key_start));
8231 DEFUN ("this-single-command-raw-keys", Fthis_single_command_raw_keys,
8232 Sthis_single_command_raw_keys, 0, 0, 0,
8233 "Return the raw events that were read for this command.\n\
8234 Unlike `this-single-command-keys', this function's value\n\
8235 shows the events before all translations (except for input methods).\n\
8236 The value is always a vector.")
8239 return Fvector (raw_keybuf_count,
8240 (XVECTOR (raw_keybuf)->contents));
8243 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
8244 Sreset_this_command_lengths, 0, 0, 0,
8245 "Used for complicated reasons in `universal-argument-other-key'.\n\
8247 `universal-argument-other-key' rereads the event just typed.\n\
8248 It then gets translated through `function-key-map'.\n\
8249 The translated event gets included in the echo area and in\n\
8250 the value of `this-command-keys' in addition to the raw original event.\n\
8251 That is not right.\n\
8253 Calling this function directs the translated event to replace\n\
8254 the original event, so that only one version of the event actually\n\
8255 appears in the echo area and in the value of `this-command-keys.'.")
8258 before_command_restore_flag = 1;
8259 before_command_key_count_1 = before_command_key_count;
8260 before_command_echo_length_1 = before_command_echo_length;
8261 return Qnil;
8264 DEFUN ("clear-this-command-keys", Fclear_this_command_keys,
8265 Sclear_this_command_keys, 0, 0, 0,
8266 "Clear out the vector that `this-command-keys' returns.")
8269 this_command_key_count = 0;
8270 return Qnil;
8273 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
8274 "Return the current depth in recursive edits.")
8277 Lisp_Object temp;
8278 XSETFASTINT (temp, command_loop_level + minibuf_level);
8279 return temp;
8282 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
8283 "FOpen dribble file: ",
8284 "Start writing all keyboard characters to a dribble file called FILE.\n\
8285 If FILE is nil, close any open dribble file.")
8286 (file)
8287 Lisp_Object file;
8289 if (dribble)
8291 fclose (dribble);
8292 dribble = 0;
8294 if (!NILP (file))
8296 file = Fexpand_file_name (file, Qnil);
8297 dribble = fopen (XSTRING (file)->data, "w");
8298 if (dribble == 0)
8299 report_file_error ("Opening dribble", Fcons (file, Qnil));
8301 return Qnil;
8304 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
8305 "Discard the contents of the terminal input buffer.\n\
8306 Also cancel any kbd macro being defined.")
8309 current_kboard->defining_kbd_macro = Qnil;
8310 update_mode_lines++;
8312 Vunread_command_events = Qnil;
8313 unread_command_char = -1;
8315 discard_tty_input ();
8317 /* Without the cast, GCC complains that this assignment loses the
8318 volatile qualifier of kbd_store_ptr. Is there anything wrong
8319 with that? */
8320 kbd_fetch_ptr = (struct input_event *) kbd_store_ptr;
8321 Ffillarray (kbd_buffer_frame_or_window, Qnil);
8322 input_pending = 0;
8324 return Qnil;
8327 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
8328 "Stop Emacs and return to superior process. You can resume later.\n\
8329 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
8330 control, run a subshell instead.\n\n\
8331 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
8332 to be read as terminal input by Emacs's parent, after suspension.\n\
8334 Before suspending, run the normal hook `suspend-hook'.\n\
8335 After resumption run the normal hook `suspend-resume-hook'.\n\
8337 Some operating systems cannot stop the Emacs process and resume it later.\n\
8338 On such systems, Emacs starts a subshell instead of suspending.")
8339 (stuffstring)
8340 Lisp_Object stuffstring;
8342 Lisp_Object tem;
8343 int count = specpdl_ptr - specpdl;
8344 int old_height, old_width;
8345 int width, height;
8346 struct gcpro gcpro1, gcpro2;
8348 if (!NILP (stuffstring))
8349 CHECK_STRING (stuffstring, 0);
8351 /* Run the functions in suspend-hook. */
8352 if (!NILP (Vrun_hooks))
8353 call1 (Vrun_hooks, intern ("suspend-hook"));
8355 GCPRO1 (stuffstring);
8356 get_frame_size (&old_width, &old_height);
8357 reset_sys_modes ();
8358 /* sys_suspend can get an error if it tries to fork a subshell
8359 and the system resources aren't available for that. */
8360 record_unwind_protect ((Lisp_Object (*) P_ ((Lisp_Object))) init_sys_modes,
8361 Qnil);
8362 stuff_buffered_input (stuffstring);
8363 if (cannot_suspend)
8364 sys_subshell ();
8365 else
8366 sys_suspend ();
8367 unbind_to (count, Qnil);
8369 /* Check if terminal/window size has changed.
8370 Note that this is not useful when we are running directly
8371 with a window system; but suspend should be disabled in that case. */
8372 get_frame_size (&width, &height);
8373 if (width != old_width || height != old_height)
8374 change_frame_size (selected_frame, height, width, 0, 0);
8376 /* Run suspend-resume-hook. */
8377 if (!NILP (Vrun_hooks))
8378 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
8380 UNGCPRO;
8381 return Qnil;
8384 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
8385 Then in any case stuff anything Emacs has read ahead and not used. */
8387 void
8388 stuff_buffered_input (stuffstring)
8389 Lisp_Object stuffstring;
8391 /* stuff_char works only in BSD, versions 4.2 and up. */
8392 #ifdef BSD_SYSTEM
8393 #ifndef BSD4_1
8394 register unsigned char *p;
8396 if (STRINGP (stuffstring))
8398 register int count;
8400 p = XSTRING (stuffstring)->data;
8401 count = STRING_BYTES (XSTRING (stuffstring));
8402 while (count-- > 0)
8403 stuff_char (*p++);
8404 stuff_char ('\n');
8406 /* Anything we have read ahead, put back for the shell to read. */
8407 /* ?? What should this do when we have multiple keyboards??
8408 Should we ignore anything that was typed in at the "wrong" kboard? */
8409 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
8411 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
8412 kbd_fetch_ptr = kbd_buffer;
8413 if (kbd_fetch_ptr->kind == ascii_keystroke)
8414 stuff_char (kbd_fetch_ptr->code);
8415 kbd_fetch_ptr->kind = no_event;
8416 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
8417 - kbd_buffer]
8418 = Qnil);
8420 input_pending = 0;
8421 #endif
8422 #endif /* BSD_SYSTEM and not BSD4_1 */
8425 void
8426 set_waiting_for_input (time_to_clear)
8427 EMACS_TIME *time_to_clear;
8429 input_available_clear_time = time_to_clear;
8431 /* Tell interrupt_signal to throw back to read_char, */
8432 waiting_for_input = 1;
8434 /* If interrupt_signal was called before and buffered a C-g,
8435 make it run again now, to avoid timing error. */
8436 if (!NILP (Vquit_flag))
8437 quit_throw_to_read_char ();
8440 void
8441 clear_waiting_for_input ()
8443 /* Tell interrupt_signal not to throw back to read_char, */
8444 waiting_for_input = 0;
8445 input_available_clear_time = 0;
8448 /* This routine is called at interrupt level in response to C-G.
8449 If interrupt_input, this is the handler for SIGINT.
8450 Otherwise, it is called from kbd_buffer_store_event,
8451 in handling SIGIO or SIGTINT.
8453 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
8454 immediately throw back to read_char.
8456 Otherwise it sets the Lisp variable quit-flag not-nil.
8457 This causes eval to throw, when it gets a chance.
8458 If quit-flag is already non-nil, it stops the job right away. */
8460 SIGTYPE
8461 interrupt_signal (signalnum) /* If we don't have an argument, */
8462 int signalnum; /* some compilers complain in signal calls. */
8464 char c;
8465 /* Must preserve main program's value of errno. */
8466 int old_errno = errno;
8468 #if defined (USG) && !defined (POSIX_SIGNALS)
8469 if (!read_socket_hook && NILP (Vwindow_system))
8471 /* USG systems forget handlers when they are used;
8472 must reestablish each time */
8473 signal (SIGINT, interrupt_signal);
8474 signal (SIGQUIT, interrupt_signal);
8476 #endif /* USG */
8478 cancel_echoing ();
8480 if (!NILP (Vquit_flag)
8481 && (FRAME_TERMCAP_P (selected_frame) || FRAME_MSDOS_P (selected_frame)))
8483 /* If SIGINT isn't blocked, don't let us be interrupted by
8484 another SIGINT, it might be harmful due to non-reentrancy
8485 in I/O functions. */
8486 sigblock (sigmask (SIGINT));
8488 fflush (stdout);
8489 reset_sys_modes ();
8491 #ifdef SIGTSTP /* Support possible in later USG versions */
8493 * On systems which can suspend the current process and return to the original
8494 * shell, this command causes the user to end up back at the shell.
8495 * The "Auto-save" and "Abort" questions are not asked until
8496 * the user elects to return to emacs, at which point he can save the current
8497 * job and either dump core or continue.
8499 sys_suspend ();
8500 #else
8501 #ifdef VMS
8502 if (sys_suspend () == -1)
8504 printf ("Not running as a subprocess;\n");
8505 printf ("you can continue or abort.\n");
8507 #else /* not VMS */
8508 /* Perhaps should really fork an inferior shell?
8509 But that would not provide any way to get back
8510 to the original shell, ever. */
8511 printf ("No support for stopping a process on this operating system;\n");
8512 printf ("you can continue or abort.\n");
8513 #endif /* not VMS */
8514 #endif /* not SIGTSTP */
8515 #ifdef MSDOS
8516 /* We must remain inside the screen area when the internal terminal
8517 is used. Note that [Enter] is not echoed by dos. */
8518 cursor_to (0, 0);
8519 #endif
8520 /* It doesn't work to autosave while GC is in progress;
8521 the code used for auto-saving doesn't cope with the mark bit. */
8522 if (!gc_in_progress)
8524 printf ("Auto-save? (y or n) ");
8525 fflush (stdout);
8526 if (((c = getchar ()) & ~040) == 'Y')
8528 Fdo_auto_save (Qt, Qnil);
8529 #ifdef MSDOS
8530 printf ("\r\nAuto-save done");
8531 #else /* not MSDOS */
8532 printf ("Auto-save done\n");
8533 #endif /* not MSDOS */
8535 while (c != '\n') c = getchar ();
8537 else
8539 /* During GC, it must be safe to reenable quitting again. */
8540 Vinhibit_quit = Qnil;
8541 #ifdef MSDOS
8542 printf ("\r\n");
8543 #endif /* not MSDOS */
8544 printf ("Garbage collection in progress; cannot auto-save now\r\n");
8545 printf ("but will instead do a real quit after garbage collection ends\r\n");
8546 fflush (stdout);
8549 #ifdef MSDOS
8550 printf ("\r\nAbort? (y or n) ");
8551 #else /* not MSDOS */
8552 #ifdef VMS
8553 printf ("Abort (and enter debugger)? (y or n) ");
8554 #else /* not VMS */
8555 printf ("Abort (and dump core)? (y or n) ");
8556 #endif /* not VMS */
8557 #endif /* not MSDOS */
8558 fflush (stdout);
8559 if (((c = getchar ()) & ~040) == 'Y')
8560 abort ();
8561 while (c != '\n') c = getchar ();
8562 #ifdef MSDOS
8563 printf ("\r\nContinuing...\r\n");
8564 #else /* not MSDOS */
8565 printf ("Continuing...\n");
8566 #endif /* not MSDOS */
8567 fflush (stdout);
8568 init_sys_modes ();
8569 sigfree ();
8571 else
8573 /* If executing a function that wants to be interrupted out of
8574 and the user has not deferred quitting by binding `inhibit-quit'
8575 then quit right away. */
8576 if (immediate_quit && NILP (Vinhibit_quit))
8578 struct gl_state_s saved;
8579 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8581 immediate_quit = 0;
8582 sigfree ();
8583 saved = gl_state;
8584 GCPRO4 (saved.object, saved.global_code,
8585 saved.current_syntax_table, saved.old_prop);
8586 Fsignal (Qquit, Qnil);
8587 gl_state = saved;
8588 UNGCPRO;
8590 else
8591 /* Else request quit when it's safe */
8592 Vquit_flag = Qt;
8595 if (waiting_for_input && !echoing)
8596 quit_throw_to_read_char ();
8598 errno = old_errno;
8601 /* Handle a C-g by making read_char return C-g. */
8603 void
8604 quit_throw_to_read_char ()
8606 quit_error_check ();
8607 sigfree ();
8608 /* Prevent another signal from doing this before we finish. */
8609 clear_waiting_for_input ();
8610 input_pending = 0;
8612 Vunread_command_events = Qnil;
8613 unread_command_char = -1;
8615 #if 0 /* Currently, sit_for is called from read_char without turning
8616 off polling. And that can call set_waiting_for_input.
8617 It seems to be harmless. */
8618 #ifdef POLL_FOR_INPUT
8619 /* May be > 1 if in recursive minibuffer. */
8620 if (poll_suppress_count == 0)
8621 abort ();
8622 #endif
8623 #endif
8624 if (FRAMEP (internal_last_event_frame)
8625 && XFRAME (internal_last_event_frame) != selected_frame)
8626 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
8627 Qnil, 0);
8629 _longjmp (getcjmp, 1);
8632 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
8633 "Set mode of reading keyboard input.\n\
8634 First arg INTERRUPT non-nil means use input interrupts;\n\
8635 nil means use CBREAK mode.\n\
8636 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
8637 (no effect except in CBREAK mode).\n\
8638 Third arg META t means accept 8-bit input (for a Meta key).\n\
8639 META nil means ignore the top bit, on the assumption it is parity.\n\
8640 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
8641 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
8642 See also `current-input-mode'.")
8643 (interrupt, flow, meta, quit)
8644 Lisp_Object interrupt, flow, meta, quit;
8646 if (!NILP (quit)
8647 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
8648 error ("set-input-mode: QUIT must be an ASCII character");
8650 #ifdef POLL_FOR_INPUT
8651 stop_polling ();
8652 #endif
8654 #ifndef DOS_NT
8655 /* this causes startup screen to be restored and messes with the mouse */
8656 reset_sys_modes ();
8657 #endif
8659 #ifdef SIGIO
8660 /* Note SIGIO has been undef'd if FIONREAD is missing. */
8661 if (read_socket_hook)
8663 /* When using X, don't give the user a real choice,
8664 because we haven't implemented the mechanisms to support it. */
8665 #ifdef NO_SOCK_SIGIO
8666 interrupt_input = 0;
8667 #else /* not NO_SOCK_SIGIO */
8668 interrupt_input = 1;
8669 #endif /* NO_SOCK_SIGIO */
8671 else
8672 interrupt_input = !NILP (interrupt);
8673 #else /* not SIGIO */
8674 interrupt_input = 0;
8675 #endif /* not SIGIO */
8677 /* Our VMS input only works by interrupts, as of now. */
8678 #ifdef VMS
8679 interrupt_input = 1;
8680 #endif
8682 flow_control = !NILP (flow);
8683 if (NILP (meta))
8684 meta_key = 0;
8685 else if (EQ (meta, Qt))
8686 meta_key = 1;
8687 else
8688 meta_key = 2;
8689 if (!NILP (quit))
8690 /* Don't let this value be out of range. */
8691 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
8693 #ifndef DOS_NT
8694 init_sys_modes ();
8695 #endif
8697 #ifdef POLL_FOR_INPUT
8698 poll_suppress_count = 1;
8699 start_polling ();
8700 #endif
8701 return Qnil;
8704 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
8705 "Return information about the way Emacs currently reads keyboard input.\n\
8706 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
8707 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
8708 nil, Emacs is using CBREAK mode.\n\
8709 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
8710 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
8711 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
8712 META nil means ignoring the top bit, on the assumption it is parity.\n\
8713 META is neither t nor nil if accepting 8-bit input and using\n\
8714 all 8 bits as the character code.\n\
8715 QUIT is the character Emacs currently uses to quit.\n\
8716 The elements of this list correspond to the arguments of\n\
8717 `set-input-mode'.")
8720 Lisp_Object val[4];
8722 val[0] = interrupt_input ? Qt : Qnil;
8723 val[1] = flow_control ? Qt : Qnil;
8724 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
8725 XSETFASTINT (val[3], quit_char);
8727 return Flist (sizeof (val) / sizeof (val[0]), val);
8732 * Set up a new kboard object with reasonable initial values.
8734 void
8735 init_kboard (kb)
8736 KBOARD *kb;
8738 kb->Voverriding_terminal_local_map = Qnil;
8739 kb->Vlast_command = Qnil;
8740 kb->Vreal_last_command = Qnil;
8741 kb->Vprefix_arg = Qnil;
8742 kb->Vlast_prefix_arg = Qnil;
8743 kb->kbd_queue = Qnil;
8744 kb->kbd_queue_has_data = 0;
8745 kb->immediate_echo = 0;
8746 kb->echoptr = kb->echobuf;
8747 kb->echo_after_prompt = -1;
8748 kb->kbd_macro_buffer = 0;
8749 kb->kbd_macro_bufsize = 0;
8750 kb->defining_kbd_macro = Qnil;
8751 kb->Vlast_kbd_macro = Qnil;
8752 kb->reference_count = 0;
8753 kb->Vsystem_key_alist = Qnil;
8754 kb->system_key_syms = Qnil;
8755 kb->Vdefault_minibuffer_frame = Qnil;
8759 * Destroy the contents of a kboard object, but not the object itself.
8760 * We use this just before deleting it, or if we're going to initialize
8761 * it a second time.
8763 static void
8764 wipe_kboard (kb)
8765 KBOARD *kb;
8767 if (kb->kbd_macro_buffer)
8768 xfree (kb->kbd_macro_buffer);
8771 #ifdef MULTI_KBOARD
8772 void
8773 delete_kboard (kb)
8774 KBOARD *kb;
8776 KBOARD **kbp;
8777 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
8778 if (*kbp == NULL)
8779 abort ();
8780 *kbp = kb->next_kboard;
8781 wipe_kboard (kb);
8782 xfree (kb);
8784 #endif
8786 void
8787 init_keyboard ()
8789 /* This is correct before outermost invocation of the editor loop */
8790 command_loop_level = -1;
8791 immediate_quit = 0;
8792 quit_char = Ctl ('g');
8793 Vunread_command_events = Qnil;
8794 unread_command_char = -1;
8795 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
8796 total_keys = 0;
8797 recent_keys_index = 0;
8798 kbd_fetch_ptr = kbd_buffer;
8799 kbd_store_ptr = kbd_buffer;
8800 kbd_buffer_frame_or_window
8801 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
8802 #ifdef HAVE_MOUSE
8803 do_mouse_tracking = Qnil;
8804 #endif
8805 input_pending = 0;
8807 /* This means that command_loop_1 won't try to select anything the first
8808 time through. */
8809 internal_last_event_frame = Qnil;
8810 Vlast_event_frame = internal_last_event_frame;
8812 #ifdef MULTI_KBOARD
8813 current_kboard = initial_kboard;
8814 #endif
8815 wipe_kboard (current_kboard);
8816 init_kboard (current_kboard);
8818 if (initialized)
8819 Ffillarray (kbd_buffer_frame_or_window, Qnil);
8821 kbd_buffer_frame_or_window
8822 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
8823 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
8825 signal (SIGINT, interrupt_signal);
8826 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
8827 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
8828 SIGQUIT and we can't tell which one it will give us. */
8829 signal (SIGQUIT, interrupt_signal);
8830 #endif /* HAVE_TERMIO */
8832 /* Note SIGIO has been undef'd if FIONREAD is missing. */
8833 #ifdef SIGIO
8834 if (!noninteractive)
8835 signal (SIGIO, input_available_signal);
8836 #endif /* SIGIO */
8838 /* Use interrupt input by default, if it works and noninterrupt input
8839 has deficiencies. */
8841 #ifdef INTERRUPT_INPUT
8842 interrupt_input = 1;
8843 #else
8844 interrupt_input = 0;
8845 #endif
8847 /* Our VMS input only works by interrupts, as of now. */
8848 #ifdef VMS
8849 interrupt_input = 1;
8850 #endif
8852 sigfree ();
8853 dribble = 0;
8855 if (keyboard_init_hook)
8856 (*keyboard_init_hook) ();
8858 #ifdef POLL_FOR_INPUT
8859 poll_suppress_count = 1;
8860 start_polling ();
8861 #endif
8864 /* This type's only use is in syms_of_keyboard, to initialize the
8865 event header symbols and put properties on them. */
8866 struct event_head {
8867 Lisp_Object *var;
8868 char *name;
8869 Lisp_Object *kind;
8872 struct event_head head_table[] = {
8873 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
8874 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
8875 &Qswitch_frame, "switch-frame", &Qswitch_frame,
8876 &Qdelete_frame, "delete-frame", &Qdelete_frame,
8877 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
8878 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
8881 void
8882 syms_of_keyboard ()
8884 staticpro (&item_properties);
8885 item_properties = Qnil;
8887 staticpro (&real_this_command);
8888 real_this_command = Qnil;
8890 Qtimer_event_handler = intern ("timer-event-handler");
8891 staticpro (&Qtimer_event_handler);
8893 Qdisabled_command_hook = intern ("disabled-command-hook");
8894 staticpro (&Qdisabled_command_hook);
8896 Qself_insert_command = intern ("self-insert-command");
8897 staticpro (&Qself_insert_command);
8899 Qforward_char = intern ("forward-char");
8900 staticpro (&Qforward_char);
8902 Qbackward_char = intern ("backward-char");
8903 staticpro (&Qbackward_char);
8905 Qdisabled = intern ("disabled");
8906 staticpro (&Qdisabled);
8908 Qundefined = intern ("undefined");
8909 staticpro (&Qundefined);
8911 Qpre_command_hook = intern ("pre-command-hook");
8912 staticpro (&Qpre_command_hook);
8914 Qpost_command_hook = intern ("post-command-hook");
8915 staticpro (&Qpost_command_hook);
8917 Qpost_command_idle_hook = intern ("post-command-idle-hook");
8918 staticpro (&Qpost_command_idle_hook);
8920 Qdeferred_action_function = intern ("deferred-action-function");
8921 staticpro (&Qdeferred_action_function);
8923 Qcommand_hook_internal = intern ("command-hook-internal");
8924 staticpro (&Qcommand_hook_internal);
8926 Qfunction_key = intern ("function-key");
8927 staticpro (&Qfunction_key);
8928 Qmouse_click = intern ("mouse-click");
8929 staticpro (&Qmouse_click);
8930 #ifdef WINDOWSNT
8931 Qmouse_wheel = intern ("mouse-wheel");
8932 staticpro (&Qmouse_wheel);
8933 Qlanguage_change = intern ("language-change");
8934 staticpro (&Qlanguage_change);
8935 #endif
8936 Qdrag_n_drop = intern ("drag-n-drop");
8937 staticpro (&Qdrag_n_drop);
8939 Qmenu_enable = intern ("menu-enable");
8940 staticpro (&Qmenu_enable);
8941 Qmenu_alias = intern ("menu-alias");
8942 staticpro (&Qmenu_alias);
8943 QCenable = intern (":enable");
8944 staticpro (&QCenable);
8945 QCvisible = intern (":visible");
8946 staticpro (&QCvisible);
8947 QCfilter = intern (":filter");
8948 staticpro (&QCfilter);
8949 QCbutton = intern (":button");
8950 staticpro (&QCbutton);
8951 QCkeys = intern (":keys");
8952 staticpro (&QCkeys);
8953 QCkey_sequence = intern (":key-sequence");
8954 staticpro (&QCkey_sequence);
8955 QCtoggle = intern (":toggle");
8956 staticpro (&QCtoggle);
8957 QCradio = intern (":radio");
8958 staticpro (&QCradio);
8960 Qmode_line = intern ("mode-line");
8961 staticpro (&Qmode_line);
8962 Qvertical_line = intern ("vertical-line");
8963 staticpro (&Qvertical_line);
8964 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
8965 staticpro (&Qvertical_scroll_bar);
8966 Qmenu_bar = intern ("menu-bar");
8967 staticpro (&Qmenu_bar);
8969 Qabove_handle = intern ("above-handle");
8970 staticpro (&Qabove_handle);
8971 Qhandle = intern ("handle");
8972 staticpro (&Qhandle);
8973 Qbelow_handle = intern ("below-handle");
8974 staticpro (&Qbelow_handle);
8975 Qup = intern ("up");
8976 staticpro (&Qup);
8977 Qdown = intern ("down");
8978 staticpro (&Qdown);
8980 Qevent_kind = intern ("event-kind");
8981 staticpro (&Qevent_kind);
8982 Qevent_symbol_elements = intern ("event-symbol-elements");
8983 staticpro (&Qevent_symbol_elements);
8984 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
8985 staticpro (&Qevent_symbol_element_mask);
8986 Qmodifier_cache = intern ("modifier-cache");
8987 staticpro (&Qmodifier_cache);
8989 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
8990 staticpro (&Qrecompute_lucid_menubar);
8991 Qactivate_menubar_hook = intern ("activate-menubar-hook");
8992 staticpro (&Qactivate_menubar_hook);
8994 Qpolling_period = intern ("polling-period");
8995 staticpro (&Qpolling_period);
8997 Qinput_method_function = intern ("input-method-function");
8998 staticpro (&Qinput_method_function);
9000 Qinput_method_exit_on_first_char = intern ("input-method-exit-on-first-char");
9001 staticpro (&Qinput_method_exit_on_first_char);
9002 Qinput_method_use_echo_area = intern ("input-method-use-echo-area");
9003 staticpro (&Qinput_method_use_echo_area);
9005 Fset (Qinput_method_exit_on_first_char, Qnil);
9006 Fset (Qinput_method_use_echo_area, Qnil);
9009 struct event_head *p;
9011 for (p = head_table;
9012 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
9013 p++)
9015 *p->var = intern (p->name);
9016 staticpro (p->var);
9017 Fput (*p->var, Qevent_kind, *p->kind);
9018 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
9022 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
9023 staticpro (&button_down_location);
9026 int i;
9027 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
9029 modifier_symbols = Fmake_vector (make_number (len), Qnil);
9030 for (i = 0; i < len; i++)
9031 if (modifier_names[i])
9032 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
9033 staticpro (&modifier_symbols);
9036 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
9037 staticpro (&recent_keys);
9039 this_command_keys = Fmake_vector (make_number (40), Qnil);
9040 staticpro (&this_command_keys);
9042 raw_keybuf = Fmake_vector (make_number (30), Qnil);
9043 staticpro (&raw_keybuf);
9045 Qextended_command_history = intern ("extended-command-history");
9046 Fset (Qextended_command_history, Qnil);
9047 staticpro (&Qextended_command_history);
9049 kbd_buffer_frame_or_window
9050 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
9051 staticpro (&kbd_buffer_frame_or_window);
9053 accent_key_syms = Qnil;
9054 staticpro (&accent_key_syms);
9056 func_key_syms = Qnil;
9057 staticpro (&func_key_syms);
9059 mouse_syms = Qnil;
9060 staticpro (&mouse_syms);
9062 #ifdef WINDOWSNT
9063 mouse_wheel_syms = Qnil;
9064 staticpro (&mouse_wheel_syms);
9066 drag_n_drop_syms = Qnil;
9067 staticpro (&drag_n_drop_syms);
9068 #endif
9070 unread_switch_frame = Qnil;
9071 staticpro (&unread_switch_frame);
9073 internal_last_event_frame = Qnil;
9074 staticpro (&internal_last_event_frame);
9076 read_key_sequence_cmd = Qnil;
9077 staticpro (&read_key_sequence_cmd);
9079 defsubr (&Sevent_convert_list);
9080 defsubr (&Sread_key_sequence);
9081 defsubr (&Sread_key_sequence_vector);
9082 defsubr (&Srecursive_edit);
9083 #ifdef HAVE_MOUSE
9084 defsubr (&Strack_mouse);
9085 #endif
9086 defsubr (&Sinput_pending_p);
9087 defsubr (&Scommand_execute);
9088 defsubr (&Srecent_keys);
9089 defsubr (&Sthis_command_keys);
9090 defsubr (&Sthis_command_keys_vector);
9091 defsubr (&Sthis_single_command_keys);
9092 defsubr (&Sthis_single_command_raw_keys);
9093 defsubr (&Sreset_this_command_lengths);
9094 defsubr (&Sclear_this_command_keys);
9095 defsubr (&Ssuspend_emacs);
9096 defsubr (&Sabort_recursive_edit);
9097 defsubr (&Sexit_recursive_edit);
9098 defsubr (&Srecursion_depth);
9099 defsubr (&Stop_level);
9100 defsubr (&Sdiscard_input);
9101 defsubr (&Sopen_dribble_file);
9102 defsubr (&Sset_input_mode);
9103 defsubr (&Scurrent_input_mode);
9104 defsubr (&Sexecute_extended_command);
9106 DEFVAR_LISP ("last-command-char", &last_command_char,
9107 "Last input event that was part of a command.");
9109 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
9110 "Last input event that was part of a command.");
9112 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
9113 "Last input event in a command, except for mouse menu events.\n\
9114 Mouse menus give back keys that don't look like mouse events;\n\
9115 this variable holds the actual mouse event that led to the menu,\n\
9116 so that you can determine whether the command was run by mouse or not.");
9118 DEFVAR_LISP ("last-input-char", &last_input_char,
9119 "Last input event.");
9121 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
9122 "Last input event.");
9124 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
9125 "List of events to be read as the command input.\n\
9126 These events are processed first, before actual keyboard input.");
9127 Vunread_command_events = Qnil;
9129 DEFVAR_INT ("unread-command-char", &unread_command_char,
9130 "If not -1, an object to be read as next command input event.");
9132 DEFVAR_LISP ("unread-post-input-method-events", &Vunread_post_input_method_events,
9133 "List of events to be processed as input by input methods.\n\
9134 These events are processed after `unread-command-events', but\n\
9135 before actual keyboard input.");
9136 Vunread_post_input_method_events = Qnil;
9138 DEFVAR_LISP ("unread-input-method-events", &Vunread_input_method_events,
9139 "List of events to be processed as input by input methods.\n\
9140 These events are processed after `unread-command-events', but\n\
9141 before actual keyboard input.");
9142 Vunread_input_method_events = Qnil;
9144 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
9145 "Meta-prefix character code.\n\
9146 Meta-foo as command input turns into this character followed by foo.");
9147 XSETINT (meta_prefix_char, 033);
9149 DEFVAR_KBOARD ("last-command", Vlast_command,
9150 "The last command executed.\n\
9151 Normally a symbol with a function definition, but can be whatever was found\n\
9152 in the keymap, or whatever the variable `this-command' was set to by that\n\
9153 command.\n\
9155 The value `mode-exit' is special; it means that the previous command\n\
9156 read an event that told it to exit, and it did so and unread that event.\n\
9157 In other words, the present command is the event that made the previous\n\
9158 command exit.\n\
9160 The value `kill-region' is special; it means that the previous command\n\
9161 was a kill command.");
9163 DEFVAR_KBOARD ("real-last-command", Vreal_last_command,
9164 "Same as `last-command', but never altered by Lisp code.");
9166 DEFVAR_LISP ("this-command", &Vthis_command,
9167 "The command now being executed.\n\
9168 The command can set this variable; whatever is put here\n\
9169 will be in `last-command' during the following command.");
9170 Vthis_command = Qnil;
9172 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
9173 "*Number of keyboard input characters between auto-saves.\n\
9174 Zero means disable autosaving due to number of characters typed.");
9175 auto_save_interval = 300;
9177 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
9178 "*Number of seconds idle time before auto-save.\n\
9179 Zero or nil means disable auto-saving due to idleness.\n\
9180 After auto-saving due to this many seconds of idle time,\n\
9181 Emacs also does a garbage collection if that seems to be warranted.");
9182 XSETFASTINT (Vauto_save_timeout, 30);
9184 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
9185 "*Nonzero means echo unfinished commands after this many seconds of pause.");
9186 echo_keystrokes = 1;
9188 DEFVAR_INT ("polling-period", &polling_period,
9189 "*Interval between polling for input during Lisp execution.\n\
9190 The reason for polling is to make C-g work to stop a running program.\n\
9191 Polling is needed only when using X windows and SIGIO does not work.\n\
9192 Polling is automatically disabled in all other cases.");
9193 polling_period = 2;
9195 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
9196 "*Maximum time between mouse clicks to make a double-click.\n\
9197 Measured in milliseconds. nil means disable double-click recognition;\n\
9198 t means double-clicks have no time limit and are detected\n\
9199 by position only.");
9200 Vdouble_click_time = make_number (500);
9202 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
9203 "*Non-nil means inhibit local map menu bar menus.");
9204 inhibit_local_menu_bar_menus = 0;
9206 DEFVAR_INT ("num-input-keys", &num_input_keys,
9207 "Number of complete key sequences read as input so far.\n\
9208 This includes key sequences read from keyboard macros.\n\
9209 The number is effectively the number of interactive command invocations.");
9210 num_input_keys = 0;
9212 DEFVAR_INT ("num-nonmacro-input-events", &num_nonmacro_input_events,
9213 "Number of input events read from the keyboard so far.\n\
9214 This does not include events generated by keyboard macros.");
9215 num_nonmacro_input_events = 0;
9217 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
9218 "The frame in which the most recently read event occurred.\n\
9219 If the last event came from a keyboard macro, this is set to `macro'.");
9220 Vlast_event_frame = Qnil;
9222 /* This variable is set up in sysdep.c. */
9223 DEFVAR_LISP ("tty-erase-char", &Vtty_erase_char,
9224 "The ERASE character as set by the user with stty.");
9226 DEFVAR_LISP ("help-char", &Vhelp_char,
9227 "Character to recognize as meaning Help.\n\
9228 When it is read, do `(eval help-form)', and display result if it's a string.\n\
9229 If the value of `help-form' is nil, this char can be read normally.");
9230 XSETINT (Vhelp_char, Ctl ('H'));
9232 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
9233 "List of input events to recognize as meaning Help.\n\
9234 These work just like the value of `help-char' (see that).");
9235 Vhelp_event_list = Qnil;
9237 DEFVAR_LISP ("help-form", &Vhelp_form,
9238 "Form to execute when character `help-char' is read.\n\
9239 If the form returns a string, that string is displayed.\n\
9240 If `help-form' is nil, the help char is not recognized.");
9241 Vhelp_form = Qnil;
9243 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
9244 "Command to run when `help-char' character follows a prefix key.\n\
9245 This command is used only when there is no actual binding\n\
9246 for that character after that prefix key.");
9247 Vprefix_help_command = Qnil;
9249 DEFVAR_LISP ("top-level", &Vtop_level,
9250 "Form to evaluate when Emacs starts up.\n\
9251 Useful to set before you dump a modified Emacs.");
9252 Vtop_level = Qnil;
9254 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
9255 "Translate table for keyboard input, or nil.\n\
9256 Each character is looked up in this string and the contents used instead.\n\
9257 The value may be a string, a vector, or a char-table.\n\
9258 If it is a string or vector of length N,\n\
9259 character codes N and up are untranslated.\n\
9260 In a vector or a char-table, an element which is nil means \"no translation\".");
9261 Vkeyboard_translate_table = Qnil;
9263 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
9264 "Non-nil means to always spawn a subshell instead of suspending.\n\
9265 \(Even if the operating system has support for stopping a process.\)");
9266 cannot_suspend = 0;
9268 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
9269 "Non-nil means prompt with menus when appropriate.\n\
9270 This is done when reading from a keymap that has a prompt string,\n\
9271 for elements that have prompt strings.\n\
9272 The menu is displayed on the screen\n\
9273 if X menus were enabled at configuration\n\
9274 time and the previous event was a mouse click prefix key.\n\
9275 Otherwise, menu prompting uses the echo area.");
9276 menu_prompting = 1;
9278 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
9279 "Character to see next line of menu prompt.\n\
9280 Type this character while in a menu prompt to rotate around the lines of it.");
9281 XSETINT (menu_prompt_more_char, ' ');
9283 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
9284 "A mask of additional modifier keys to use with every keyboard character.\n\
9285 Emacs applies the modifiers of the character stored here to each keyboard\n\
9286 character it reads. For example, after evaluating the expression\n\
9287 (setq extra-keyboard-modifiers ?\\C-x)\n\
9288 all input characters will have the control modifier applied to them.\n\
9290 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
9291 not count as a control character; rather, it counts as a character\n\
9292 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
9293 cancels any modification.");
9294 extra_keyboard_modifiers = 0;
9296 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
9297 "If an editing command sets this to t, deactivate the mark afterward.\n\
9298 The command loop sets this to nil before each command,\n\
9299 and tests the value when the command returns.\n\
9300 Buffer modification stores t in this variable.");
9301 Vdeactivate_mark = Qnil;
9303 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
9304 "Temporary storage of pre-command-hook or post-command-hook.");
9305 Vcommand_hook_internal = Qnil;
9307 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
9308 "Normal hook run before each command is executed.\n\
9309 If an unhandled error happens in running this hook,\n\
9310 the hook value is set to nil, since otherwise the error\n\
9311 might happen repeatedly and make Emacs nonfunctional.");
9312 Vpre_command_hook = Qnil;
9314 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
9315 "Normal hook run after each command is executed.\n\
9316 If an unhandled error happens in running this hook,\n\
9317 the hook value is set to nil, since otherwise the error\n\
9318 might happen repeatedly and make Emacs nonfunctional.");
9319 Vpost_command_hook = Qnil;
9321 DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
9322 "Normal hook run after each command is executed, if idle.\n\
9323 Errors running the hook are caught and ignored.\n\
9324 This feature is obsolete; use idle timers instead. See `etc/NEWS'.");
9325 Vpost_command_idle_hook = Qnil;
9327 DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
9328 "Delay time before running `post-command-idle-hook'.\n\
9329 This is measured in microseconds.");
9330 post_command_idle_delay = 100000;
9332 #if 0
9333 DEFVAR_LISP ("echo-area-clear-hook", ...,
9334 "Normal hook run when clearing the echo area.");
9335 #endif
9336 Qecho_area_clear_hook = intern ("echo-area-clear-hook");
9337 XSYMBOL (Qecho_area_clear_hook)->value = Qnil;
9339 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
9340 "t means menu bar, specified Lucid style, needs to be recomputed.");
9341 Vlucid_menu_bar_dirty_flag = Qnil;
9343 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
9344 "List of menu bar items to move to the end of the menu bar.\n\
9345 The elements of the list are event types that may have menu bar bindings.");
9346 Vmenu_bar_final_items = Qnil;
9348 DEFVAR_KBOARD ("overriding-terminal-local-map",
9349 Voverriding_terminal_local_map,
9350 "Per-terminal keymap that overrides all other local keymaps.\n\
9351 If this variable is non-nil, it is used as a keymap instead of the\n\
9352 buffer's local map, and the minor mode keymaps and text property keymaps.\n\
9353 This variable is intended to let commands such as `universal-argumemnt'\n\
9354 set up a different keymap for reading the next command.");
9356 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
9357 "Keymap that overrides all other local keymaps.\n\
9358 If this variable is non-nil, it is used as a keymap instead of the\n\
9359 buffer's local map, and the minor mode keymaps and text property keymaps.");
9360 Voverriding_local_map = Qnil;
9362 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
9363 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
9364 Otherwise, the menu bar continues to reflect the buffer's local map\n\
9365 and the minor mode maps regardless of `overriding-local-map'.");
9366 Voverriding_local_map_menu_flag = Qnil;
9368 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
9369 "Keymap defining bindings for special events to execute at low level.");
9370 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
9372 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
9373 "*Non-nil means generate motion events for mouse motion.");
9375 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
9376 "Alist of system-specific X windows key symbols.\n\
9377 Each element should have the form (N . SYMBOL) where N is the\n\
9378 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
9379 and SYMBOL is its name.");
9381 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
9382 "List of deferred actions to be performed at a later time.\n\
9383 The precise format isn't relevant here; we just check whether it is nil.");
9384 Vdeferred_action_list = Qnil;
9386 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
9387 "Function to call to handle deferred actions, after each command.\n\
9388 This function is called with no arguments after each command\n\
9389 whenever `deferred-action-list' is non-nil.");
9390 Vdeferred_action_function = Qnil;
9392 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
9393 "*Non-nil means show the equivalent key-binding when M-x command has one.\n\
9394 The value can be a length of time to show the message for.\n\
9395 If the value is non-nil and not a number, we wait 2 seconds.");
9396 Vsuggest_key_bindings = Qt;
9398 DEFVAR_LISP ("timer-list", &Vtimer_list,
9399 "List of active absolute time timers in order of increasing time");
9400 Vtimer_list = Qnil;
9402 DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
9403 "List of active idle-time timers in order of increasing time");
9404 Vtimer_idle_list = Qnil;
9406 DEFVAR_LISP ("input-method-function", &Vinput_method_function,
9407 "If non-nil, the function that implements the current input method.\n\
9408 It's called with one argument, a printing character that was just read.\n\
9409 \(That means a character with code 040...0176.)\n\
9410 Typically this function uses `read-event' to read additional events.\n\
9411 When it does so, it should first bind `input-method-function' to nil\n\
9412 so it will not be called recursively.\n\
9414 The function should return a list of zero or more events\n\
9415 to be used as input. If it wants to put back some events\n\
9416 to be reconsidered, separately, by the input method,\n\
9417 it can add them to the beginning of `unread-command-events'.\n\
9419 The input method function can find in `input-method-previous-method'\n\
9420 the previous echo area message.\n\
9422 The input method function should refer to the variables\n\
9423 `input-method-use-echo-area' and `input-method-exit-on-first-char'\n\
9424 for guidance on what to do.");
9425 Vinput_method_function = Qnil;
9427 DEFVAR_LISP ("input-method-previous-message",
9428 &Vinput_method_previous_message,
9429 "When `input-method-function' is called, hold the previous echo area message.\n\
9430 This variable exists because `read-event' clears the echo area\n\
9431 before running the input method. It is nil if there was no message.");
9432 Vinput_method_previous_message = Qnil;
9435 void
9436 keys_of_keyboard ()
9438 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
9439 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
9440 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
9441 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
9442 initial_define_key (meta_map, 'x', "execute-extended-command");
9444 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
9445 "handle-delete-frame");
9446 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
9447 "ignore-event");
9448 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
9449 "ignore-event");