.
[emacs.git] / src / keyboard.c
blobbaddff3414a1c46401738dfd1a4ecc9b94fa90f6
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 #include <sys/types.h>
59 #ifdef HAVE_UNISTD_H
60 #include <unistd.h>
61 #endif
63 /* This is to get the definitions of the XK_ symbols. */
64 #ifdef HAVE_X_WINDOWS
65 #include "xterm.h"
66 #endif
68 #ifdef HAVE_NTGUI
69 #include "w32term.h"
70 #endif /* HAVE_NTGUI */
72 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
73 #include "systime.h"
75 extern int errno;
77 /* Variables for blockinput.h: */
79 /* Non-zero if interrupt input is blocked right now. */
80 int interrupt_input_blocked;
82 /* Nonzero means an input interrupt has arrived
83 during the current critical section. */
84 int interrupt_input_pending;
87 /* File descriptor to use for input. */
88 extern int input_fd;
90 #ifdef HAVE_WINDOW_SYSTEM
91 /* Make all keyboard buffers much bigger when using X windows. */
92 #ifdef macintosh
93 /* But not too big (local data > 32K error) if on macintosh */
94 #define KBD_BUFFER_SIZE 512
95 #else
96 #define KBD_BUFFER_SIZE 4096
97 #endif
98 #else /* No X-windows, character input */
99 #define KBD_BUFFER_SIZE 256
100 #endif /* No X-windows */
102 /* Following definition copied from eval.c */
104 struct backtrace
106 struct backtrace *next;
107 Lisp_Object *function;
108 Lisp_Object *args; /* Points to vector of args. */
109 int nargs; /* length of vector. If nargs is UNEVALLED,
110 args points to slot holding list of
111 unevalled args */
112 char evalargs;
115 #ifdef MULTI_KBOARD
116 KBOARD *initial_kboard;
117 KBOARD *current_kboard;
118 KBOARD *all_kboards;
119 int single_kboard;
120 #else
121 KBOARD the_only_kboard;
122 #endif
124 /* Non-nil disable property on a command means
125 do not execute it; call disabled-command-hook's value instead. */
126 Lisp_Object Qdisabled, Qdisabled_command_hook;
128 #define NUM_RECENT_KEYS (100)
129 int recent_keys_index; /* Index for storing next element into recent_keys */
130 int total_keys; /* Total number of elements stored into recent_keys */
131 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
133 /* Vector holding the key sequence that invoked the current command.
134 It is reused for each command, and it may be longer than the current
135 sequence; this_command_key_count indicates how many elements
136 actually mean something.
137 It's easier to staticpro a single Lisp_Object than an array. */
138 Lisp_Object this_command_keys;
139 int this_command_key_count;
141 /* This vector is used as a buffer to record the events that were actually read
142 by read_key_sequence. */
143 Lisp_Object raw_keybuf;
144 int raw_keybuf_count;
146 #define GROW_RAW_KEYBUF \
147 if (raw_keybuf_count == XVECTOR (raw_keybuf)->size) \
149 int newsize = 2 * XVECTOR (raw_keybuf)->size; \
150 Lisp_Object new; \
151 new = Fmake_vector (make_number (newsize), Qnil); \
152 bcopy (XVECTOR (raw_keybuf)->contents, XVECTOR (new)->contents, \
153 raw_keybuf_count * sizeof (Lisp_Object)); \
154 raw_keybuf = new; \
157 /* Number of elements of this_command_keys
158 that precede this key sequence. */
159 int this_single_command_key_start;
161 /* Record values of this_command_key_count and echo_length ()
162 before this command was read. */
163 static int before_command_key_count;
164 static int before_command_echo_length;
165 /* Values of before_command_key_count and before_command_echo_length
166 saved by reset-this-command-lengths. */
167 static int before_command_key_count_1;
168 static int before_command_echo_length_1;
169 /* Flag set by reset-this-command-lengths,
170 saying to reset the lengths when add_command_key is called. */
171 static int before_command_restore_flag;
173 extern int minbuf_level;
175 extern int message_enable_multibyte;
177 extern struct backtrace *backtrace_list;
179 /* If non-nil, the function that implements the display of help.
180 It's called with one argument, the help string to display. */
182 Lisp_Object Vshow_help_function;
184 /* Nonzero means do menu prompting. */
185 static int menu_prompting;
187 /* Character to see next line of menu prompt. */
188 static Lisp_Object menu_prompt_more_char;
190 /* For longjmp to where kbd input is being done. */
191 static jmp_buf getcjmp;
193 /* True while doing kbd input. */
194 int waiting_for_input;
196 /* True while displaying for echoing. Delays C-g throwing. */
198 static int echoing;
200 /* Non-null means we can start echoing at the next input pause even
201 though there is something in the echo area. */
203 static struct kboard *ok_to_echo_at_next_pause;
205 /* The kboard currently echoing, or null for none. Set in echo_now to
206 the kboard echoing. Reset to 0 in cancel_echoing. If non-null,
207 and a current echo area message exists, we know that it comes from
208 echoing. */
210 static struct kboard *echo_kboard;
212 /* Nonzero means disregard local maps for the menu bar. */
213 static int inhibit_local_menu_bar_menus;
215 /* Nonzero means C-g should cause immediate error-signal. */
216 int immediate_quit;
218 /* The user's ERASE setting. */
219 Lisp_Object Vtty_erase_char;
221 /* Character to recognize as the help char. */
222 Lisp_Object Vhelp_char;
224 /* List of other event types to recognize as meaning "help". */
225 Lisp_Object Vhelp_event_list;
227 /* Form to execute when help char is typed. */
228 Lisp_Object Vhelp_form;
230 /* Command to run when the help character follows a prefix key. */
231 Lisp_Object Vprefix_help_command;
233 /* List of items that should move to the end of the menu bar. */
234 Lisp_Object Vmenu_bar_final_items;
236 /* Non-nil means show the equivalent key-binding for
237 any M-x command that has one.
238 The value can be a length of time to show the message for.
239 If the value is non-nil and not a number, we wait 2 seconds. */
240 Lisp_Object Vsuggest_key_bindings;
242 /* Character that causes a quit. Normally C-g.
244 If we are running on an ordinary terminal, this must be an ordinary
245 ASCII char, since we want to make it our interrupt character.
247 If we are not running on an ordinary terminal, it still needs to be
248 an ordinary ASCII char. This character needs to be recognized in
249 the input interrupt handler. At this point, the keystroke is
250 represented as a struct input_event, while the desired quit
251 character is specified as a lispy event. The mapping from struct
252 input_events to lispy events cannot run in an interrupt handler,
253 and the reverse mapping is difficult for anything but ASCII
254 keystrokes.
256 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
257 ASCII character. */
258 int quit_char;
260 extern Lisp_Object current_global_map;
261 extern int minibuf_level;
263 /* If non-nil, this is a map that overrides all other local maps. */
264 Lisp_Object Voverriding_local_map;
266 /* If non-nil, Voverriding_local_map applies to the menu bar. */
267 Lisp_Object Voverriding_local_map_menu_flag;
269 /* Keymap that defines special misc events that should
270 be processed immediately at a low level. */
271 Lisp_Object Vspecial_event_map;
273 /* Current depth in recursive edits. */
274 int command_loop_level;
276 /* Total number of times command_loop has read a key sequence. */
277 int num_input_keys;
279 /* Last input character read as a command. */
280 Lisp_Object last_command_char;
282 /* Last input character read as a command, not counting menus
283 reached by the mouse. */
284 Lisp_Object last_nonmenu_event;
286 /* Last input character read for any purpose. */
287 Lisp_Object last_input_char;
289 /* If not Qnil, a list of objects to be read as subsequent command input. */
290 Lisp_Object Vunread_command_events;
292 /* If not Qnil, a list of objects to be read as subsequent command input
293 including input method processing. */
294 Lisp_Object Vunread_input_method_events;
296 /* If not Qnil, a list of objects to be read as subsequent command input
297 but NOT including input method processing. */
298 Lisp_Object Vunread_post_input_method_events;
300 /* If not -1, an event to be read as subsequent command input. */
301 int unread_command_char;
303 /* If not Qnil, this is a switch-frame event which we decided to put
304 off until the end of a key sequence. This should be read as the
305 next command input, after any unread_command_events.
307 read_key_sequence uses this to delay switch-frame events until the
308 end of the key sequence; Fread_char uses it to put off switch-frame
309 events until a non-ASCII event is acceptable as input. */
310 Lisp_Object unread_switch_frame;
312 /* A mask of extra modifier bits to put into every keyboard char. */
313 int extra_keyboard_modifiers;
315 /* Char to use as prefix when a meta character is typed in.
316 This is bound on entry to minibuffer in case ESC is changed there. */
318 Lisp_Object meta_prefix_char;
320 /* Last size recorded for a current buffer which is not a minibuffer. */
321 static int last_non_minibuf_size;
323 /* Number of idle seconds before an auto-save and garbage collection. */
324 static Lisp_Object Vauto_save_timeout;
326 /* Total number of times read_char has returned. */
327 int num_input_events;
329 /* Total number of times read_char has returned, outside of macros. */
330 int num_nonmacro_input_events;
332 /* Auto-save automatically when this many characters have been typed
333 since the last time. */
335 static int auto_save_interval;
337 /* Value of num_nonmacro_input_events as of last auto save. */
339 int last_auto_save;
341 /* The command being executed by the command loop.
342 Commands may set this, and the value set will be copied into
343 current_kboard->Vlast_command instead of the actual command. */
344 Lisp_Object Vthis_command;
346 /* This is like Vthis_command, except that commands never set it. */
347 Lisp_Object real_this_command;
349 /* The value of point when the last command was executed. */
350 int last_point_position;
352 /* The buffer that was current when the last command was started. */
353 Lisp_Object last_point_position_buffer;
355 /* The frame in which the last input event occurred, or Qmacro if the
356 last event came from a macro. We use this to determine when to
357 generate switch-frame events. This may be cleared by functions
358 like Fselect_frame, to make sure that a switch-frame event is
359 generated by the next character. */
360 Lisp_Object internal_last_event_frame;
362 /* A user-visible version of the above, intended to allow users to
363 figure out where the last event came from, if the event doesn't
364 carry that information itself (i.e. if it was a character). */
365 Lisp_Object Vlast_event_frame;
367 /* The timestamp of the last input event we received from the X server.
368 X Windows wants this for selection ownership. */
369 unsigned long last_event_timestamp;
371 Lisp_Object Qself_insert_command;
372 Lisp_Object Qforward_char;
373 Lisp_Object Qbackward_char;
374 Lisp_Object Qundefined;
375 Lisp_Object Qtimer_event_handler;
377 /* read_key_sequence stores here the command definition of the
378 key sequence that it reads. */
379 Lisp_Object read_key_sequence_cmd;
381 /* Form to evaluate (if non-nil) when Emacs is started. */
382 Lisp_Object Vtop_level;
384 /* User-supplied string to translate input characters through. */
385 Lisp_Object Vkeyboard_translate_table;
387 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
388 extern Lisp_Object Vfunction_key_map;
390 /* Another keymap that maps key sequences into key sequences.
391 This one takes precedence over ordinary definitions. */
392 extern Lisp_Object Vkey_translation_map;
394 /* If non-nil, this implements the current input method. */
395 Lisp_Object Vinput_method_function;
396 Lisp_Object Qinput_method_function;
398 /* When we call Vinput_method_function,
399 this holds the echo area message that was just erased. */
400 Lisp_Object Vinput_method_previous_message;
402 /* Non-nil means deactivate the mark at end of this command. */
403 Lisp_Object Vdeactivate_mark;
405 /* Menu bar specified in Lucid Emacs fashion. */
407 Lisp_Object Vlucid_menu_bar_dirty_flag;
408 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
410 Lisp_Object Qecho_area_clear_hook;
412 /* Hooks to run before and after each command. */
413 Lisp_Object Qpre_command_hook, Vpre_command_hook;
414 Lisp_Object Qpost_command_hook, Vpost_command_hook;
415 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
416 /* Hook run after a command if there's no more input soon. */
417 Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook;
419 /* Delay time in microseconds before running post-command-idle-hook. */
420 int post_command_idle_delay;
422 /* List of deferred actions to be performed at a later time.
423 The precise format isn't relevant here; we just check whether it is nil. */
424 Lisp_Object Vdeferred_action_list;
426 /* Function to call to handle deferred actions, when there are any. */
427 Lisp_Object Vdeferred_action_function;
428 Lisp_Object Qdeferred_action_function;
430 Lisp_Object Qinput_method_exit_on_first_char;
431 Lisp_Object Qinput_method_use_echo_area;
433 /* File in which we write all commands we read. */
434 FILE *dribble;
436 /* Nonzero if input is available. */
437 int input_pending;
439 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
440 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
442 int meta_key;
444 extern char *pending_malloc_warning;
446 /* Circular buffer for pre-read keyboard input. */
447 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
449 /* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
451 The interrupt-level event handlers will never enqueue an event on a
452 frame which is not in Vframe_list, and once an event is dequeued,
453 internal_last_event_frame or the event itself points to the frame.
454 So that's all fine.
456 But while the event is sitting in the queue, it's completely
457 unprotected. Suppose the user types one command which will run for
458 a while and then delete a frame, and then types another event at
459 the frame that will be deleted, before the command gets around to
460 it. Suppose there are no references to this frame elsewhere in
461 Emacs, and a GC occurs before the second event is dequeued. Now we
462 have an event referring to a freed frame, which will crash Emacs
463 when it is dequeued.
465 Similar things happen when an event on a scroll bar is enqueued; the
466 window may be deleted while the event is in the queue.
468 So, we use this vector to protect the frame_or_window field in the
469 event queue. That way, they'll be dequeued as dead frames or
470 windows, but still valid lisp objects.
472 If kbd_buffer[i].kind != no_event, then
473 (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
474 == kbd_buffer[i].frame_or_window. */
475 static Lisp_Object kbd_buffer_frame_or_window;
477 /* Pointer to next available character in kbd_buffer.
478 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
479 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
480 next available char is in kbd_buffer[0]. */
481 static struct input_event *kbd_fetch_ptr;
483 /* Pointer to next place to store character in kbd_buffer. This
484 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
485 character should go in kbd_buffer[0]. */
486 static struct input_event * volatile kbd_store_ptr;
488 /* The above pair of variables forms a "queue empty" flag. When we
489 enqueue a non-hook event, we increment kbd_store_ptr. When we
490 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
491 there is input available iff the two pointers are not equal.
493 Why not just have a flag set and cleared by the enqueuing and
494 dequeuing functions? Such a flag could be screwed up by interrupts
495 at inopportune times. */
497 /* If this flag is non-nil, we check mouse_moved to see when the
498 mouse moves, and motion events will appear in the input stream.
499 Otherwise, mouse motion is ignored. */
500 static Lisp_Object do_mouse_tracking;
502 /* Symbols to head events. */
503 Lisp_Object Qmouse_movement;
504 Lisp_Object Qscroll_bar_movement;
505 Lisp_Object Qswitch_frame;
506 Lisp_Object Qdelete_frame;
507 Lisp_Object Qiconify_frame;
508 Lisp_Object Qmake_frame_visible;
509 Lisp_Object Qhelp_echo;
511 /* Symbols to denote kinds of events. */
512 Lisp_Object Qfunction_key;
513 Lisp_Object Qmouse_click;
514 #ifdef WINDOWSNT
515 Lisp_Object Qmouse_wheel;
516 Lisp_Object Qlanguage_change;
517 #endif
518 Lisp_Object Qdrag_n_drop;
519 /* Lisp_Object Qmouse_movement; - also an event header */
521 /* Properties of event headers. */
522 Lisp_Object Qevent_kind;
523 Lisp_Object Qevent_symbol_elements;
525 /* menu item parts */
526 Lisp_Object Qmenu_alias;
527 Lisp_Object Qmenu_enable;
528 Lisp_Object QCenable, QCvisible, QChelp, QCfilter, QCkeys, QCkey_sequence;
529 Lisp_Object QCbutton, QCtoggle, QCradio;
530 extern Lisp_Object Vdefine_key_rebound_commands;
531 extern Lisp_Object Qmenu_item;
533 /* An event header symbol HEAD may have a property named
534 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
535 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
536 mask of modifiers applied to it. If present, this is used to help
537 speed up parse_modifiers. */
538 Lisp_Object Qevent_symbol_element_mask;
540 /* An unmodified event header BASE may have a property named
541 Qmodifier_cache, which is an alist mapping modifier masks onto
542 modified versions of BASE. If present, this helps speed up
543 apply_modifiers. */
544 Lisp_Object Qmodifier_cache;
546 /* Symbols to use for parts of windows. */
547 Lisp_Object Qmode_line;
548 Lisp_Object Qvertical_line;
549 Lisp_Object Qvertical_scroll_bar;
550 Lisp_Object Qmenu_bar;
552 Lisp_Object recursive_edit_unwind (), command_loop ();
553 Lisp_Object Fthis_command_keys ();
554 Lisp_Object Qextended_command_history;
555 EMACS_TIME timer_check ();
557 extern Lisp_Object Vhistory_length;
559 extern char *x_get_keysym_name ();
561 static void record_menu_key ();
563 Lisp_Object Qpolling_period;
565 /* List of absolute timers. Appears in order of next scheduled event. */
566 Lisp_Object Vtimer_list;
568 /* List of idle time timers. Appears in order of next scheduled event. */
569 Lisp_Object Vtimer_idle_list;
571 /* Incremented whenever a timer is run. */
572 int timers_run;
574 extern Lisp_Object Vprint_level, Vprint_length;
576 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
577 happens. */
578 EMACS_TIME *input_available_clear_time;
580 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
581 Default is 1 if INTERRUPT_INPUT is defined. */
582 int interrupt_input;
584 /* Nonzero while interrupts are temporarily deferred during redisplay. */
585 int interrupts_deferred;
587 /* Nonzero means use ^S/^Q for flow control. */
588 int flow_control;
590 /* Allow m- file to inhibit use of FIONREAD. */
591 #ifdef BROKEN_FIONREAD
592 #undef FIONREAD
593 #endif
595 /* We are unable to use interrupts if FIONREAD is not available,
596 so flush SIGIO so we won't try. */
597 #ifndef FIONREAD
598 #ifdef SIGIO
599 #undef SIGIO
600 #endif
601 #endif
603 /* If we support a window system, turn on the code to poll periodically
604 to detect C-g. It isn't actually used when doing interrupt input. */
605 #ifdef HAVE_WINDOW_SYSTEM
606 #define POLL_FOR_INPUT
607 #endif
609 /* Global variable declarations. */
611 /* Function for init_keyboard to call with no args (if nonzero). */
612 void (*keyboard_init_hook) ();
614 static int read_avail_input ();
615 static void get_input_pending ();
616 static int readable_events ();
617 static Lisp_Object read_char_x_menu_prompt ();
618 static Lisp_Object read_char_minibuf_menu_prompt ();
619 static Lisp_Object make_lispy_event ();
620 #ifdef HAVE_MOUSE
621 static Lisp_Object make_lispy_movement ();
622 #endif
623 static Lisp_Object modify_event_symbol ();
624 static Lisp_Object make_lispy_switch_frame ();
625 static int parse_solitary_modifier ();
626 static void save_getcjmp ();
627 static void restore_getcjmp ();
628 static Lisp_Object apply_modifiers P_ ((int, Lisp_Object));
630 /* > 0 if we are to echo keystrokes. */
631 static int echo_keystrokes;
633 /* Nonzero means don't try to suspend even if the operating system seems
634 to support it. */
635 static int cannot_suspend;
637 #define min(a,b) ((a)<(b)?(a):(b))
638 #define max(a,b) ((a)>(b)?(a):(b))
640 /* Install the string STR as the beginning of the string of echoing,
641 so that it serves as a prompt for the next character.
642 Also start echoing. */
644 void
645 echo_prompt (str)
646 char *str;
648 int len = strlen (str);
650 if (len > ECHOBUFSIZE - 4)
651 len = ECHOBUFSIZE - 4;
652 bcopy (str, current_kboard->echobuf, len);
653 current_kboard->echoptr = current_kboard->echobuf + len;
654 *current_kboard->echoptr = '\0';
656 current_kboard->echo_after_prompt = len;
658 echo_now ();
661 /* Add C to the echo string, if echoing is going on.
662 C can be a character, which is printed prettily ("M-C-x" and all that
663 jazz), or a symbol, whose name is printed. */
665 void
666 echo_char (c)
667 Lisp_Object c;
669 extern char *push_key_description ();
671 if (current_kboard->immediate_echo)
673 char *ptr = current_kboard->echoptr;
675 if (ptr != current_kboard->echobuf)
676 *ptr++ = ' ';
678 /* If someone has passed us a composite event, use its head symbol. */
679 c = EVENT_HEAD (c);
681 if (INTEGERP (c))
683 if (ptr - current_kboard->echobuf > ECHOBUFSIZE - 6)
684 return;
686 ptr = push_key_description (XINT (c), ptr);
688 else if (SYMBOLP (c))
690 struct Lisp_String *name = XSYMBOL (c)->name;
691 if ((ptr - current_kboard->echobuf) + STRING_BYTES (name) + 4
692 > ECHOBUFSIZE)
693 return;
694 bcopy (name->data, ptr, STRING_BYTES (name));
695 ptr += STRING_BYTES (name);
698 if (current_kboard->echoptr == current_kboard->echobuf
699 && help_char_p (c))
701 strcpy (ptr, " (Type ? for further options)");
702 ptr += strlen (ptr);
705 *ptr = 0;
706 current_kboard->echoptr = ptr;
708 echo_now ();
712 /* Temporarily add a dash to the end of the echo string if it's not
713 empty, so that it serves as a mini-prompt for the very next character. */
715 void
716 echo_dash ()
718 if (!current_kboard->immediate_echo
719 && current_kboard->echoptr == current_kboard->echobuf)
720 return;
721 /* Do nothing if we just printed a prompt. */
722 if (current_kboard->echo_after_prompt
723 == current_kboard->echoptr - current_kboard->echobuf)
724 return;
725 /* Do nothing if not echoing at all. */
726 if (current_kboard->echoptr == 0)
727 return;
729 /* Put a dash at the end of the buffer temporarily,
730 but make it go away when the next character is added. */
731 current_kboard->echoptr[0] = '-';
732 current_kboard->echoptr[1] = 0;
734 echo_now ();
737 /* Display the current echo string, and begin echoing if not already
738 doing so. */
740 void
741 echo_now ()
743 if (!current_kboard->immediate_echo)
745 int i;
746 current_kboard->immediate_echo = 1;
748 for (i = 0; i < this_command_key_count; i++)
750 Lisp_Object c;
751 c = XVECTOR (this_command_keys)->contents[i];
752 if (! (EVENT_HAS_PARAMETERS (c)
753 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
754 echo_char (c);
756 echo_dash ();
759 echoing = 1;
760 echo_kboard = current_kboard;
761 message2_nolog (current_kboard->echobuf, strlen (current_kboard->echobuf),
762 ! NILP (current_buffer->enable_multibyte_characters));
763 echoing = 0;
765 if (waiting_for_input && !NILP (Vquit_flag))
766 quit_throw_to_read_char ();
769 /* Turn off echoing, for the start of a new command. */
771 void
772 cancel_echoing ()
774 current_kboard->immediate_echo = 0;
775 current_kboard->echoptr = current_kboard->echobuf;
776 current_kboard->echo_after_prompt = -1;
777 ok_to_echo_at_next_pause = 0;
778 echo_kboard = 0;
781 /* Return the length of the current echo string. */
783 static int
784 echo_length ()
786 return current_kboard->echoptr - current_kboard->echobuf;
789 /* Truncate the current echo message to its first LEN chars.
790 This and echo_char get used by read_key_sequence when the user
791 switches frames while entering a key sequence. */
793 static void
794 echo_truncate (len)
795 int len;
797 current_kboard->echobuf[len] = '\0';
798 current_kboard->echoptr = current_kboard->echobuf + len;
799 truncate_echo_area (len);
803 /* Functions for manipulating this_command_keys. */
804 static void
805 add_command_key (key)
806 Lisp_Object key;
808 int size = XVECTOR (this_command_keys)->size;
810 /* If reset-this-command-length was called recently, obey it now.
811 See the doc string of that function for an explanation of why. */
812 if (before_command_restore_flag)
814 this_command_key_count = before_command_key_count_1;
815 if (this_command_key_count < this_single_command_key_start)
816 this_single_command_key_start = this_command_key_count;
817 echo_truncate (before_command_echo_length_1);
818 before_command_restore_flag = 0;
821 if (this_command_key_count >= size)
823 Lisp_Object new_keys;
825 new_keys = Fmake_vector (make_number (size * 2), Qnil);
826 bcopy (XVECTOR (this_command_keys)->contents,
827 XVECTOR (new_keys)->contents,
828 size * sizeof (Lisp_Object));
830 this_command_keys = new_keys;
833 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
836 Lisp_Object
837 recursive_edit_1 ()
839 int count = specpdl_ptr - specpdl;
840 Lisp_Object val;
842 if (command_loop_level > 0)
844 specbind (Qstandard_output, Qt);
845 specbind (Qstandard_input, Qt);
848 val = command_loop ();
849 if (EQ (val, Qt))
850 Fsignal (Qquit, Qnil);
851 /* Handle throw from read_minibuf when using minibuffer
852 while it's active but we're in another window. */
853 if (STRINGP (val))
854 Fsignal (Qerror, Fcons (val, Qnil));
856 return unbind_to (count, Qnil);
859 /* When an auto-save happens, record the "time", and don't do again soon. */
861 void
862 record_auto_save ()
864 last_auto_save = num_nonmacro_input_events;
867 /* Make an auto save happen as soon as possible at command level. */
869 void
870 force_auto_save_soon ()
872 last_auto_save = - auto_save_interval - 1;
874 record_asynch_buffer_change ();
877 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
878 "Invoke the editor command loop recursively.\n\
879 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
880 that tells this function to return.\n\
881 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
882 This function is called by the editor initialization to begin editing.")
885 int count = specpdl_ptr - specpdl;
887 command_loop_level++;
888 update_mode_lines = 1;
890 record_unwind_protect (recursive_edit_unwind,
891 (command_loop_level
892 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
893 ? Fcurrent_buffer ()
894 : Qnil);
895 recursive_edit_1 ();
896 return unbind_to (count, Qnil);
899 Lisp_Object
900 recursive_edit_unwind (buffer)
901 Lisp_Object buffer;
903 if (!NILP (buffer))
904 Fset_buffer (buffer);
906 command_loop_level--;
907 update_mode_lines = 1;
908 return Qnil;
911 static void
912 any_kboard_state ()
914 #ifdef MULTI_KBOARD
915 #if 0 /* Theory: if there's anything in Vunread_command_events,
916 it will right away be read by read_key_sequence,
917 and then if we do switch KBOARDS, it will go into the side
918 queue then. So we don't need to do anything special here -- rms. */
919 if (CONSP (Vunread_command_events))
921 current_kboard->kbd_queue
922 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
923 current_kboard->kbd_queue_has_data = 1;
925 Vunread_command_events = Qnil;
926 #endif
927 single_kboard = 0;
928 #endif
931 /* Switch to the single-kboard state, making current_kboard
932 the only KBOARD from which further input is accepted. */
934 void
935 single_kboard_state ()
937 #ifdef MULTI_KBOARD
938 single_kboard = 1;
939 #endif
942 /* Maintain a stack of kboards, so other parts of Emacs
943 can switch temporarily to the kboard of a given frame
944 and then revert to the previous status. */
946 struct kboard_stack
948 KBOARD *kboard;
949 struct kboard_stack *next;
952 static struct kboard_stack *kboard_stack;
954 void
955 push_frame_kboard (f)
956 FRAME_PTR f;
958 #ifdef MULTI_KBOARD
959 struct kboard_stack *p
960 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
962 p->next = kboard_stack;
963 p->kboard = current_kboard;
964 kboard_stack = p;
966 current_kboard = FRAME_KBOARD (f);
967 #endif
970 void
971 pop_frame_kboard ()
973 #ifdef MULTI_KBOARD
974 struct kboard_stack *p = kboard_stack;
975 current_kboard = p->kboard;
976 kboard_stack = p->next;
977 xfree (p);
978 #endif
981 /* Handle errors that are not handled at inner levels
982 by printing an error message and returning to the editor command loop. */
984 Lisp_Object
985 cmd_error (data)
986 Lisp_Object data;
988 Lisp_Object old_level, old_length;
989 char macroerror[50];
991 if (!NILP (executing_macro))
993 if (executing_macro_iterations == 1)
994 sprintf (macroerror, "After 1 kbd macro iteration: ");
995 else
996 sprintf (macroerror, "After %d kbd macro iterations: ",
997 executing_macro_iterations);
999 else
1000 *macroerror = 0;
1002 Vstandard_output = Qt;
1003 Vstandard_input = Qt;
1004 Vexecuting_macro = Qnil;
1005 executing_macro = Qnil;
1006 current_kboard->Vprefix_arg = Qnil;
1007 current_kboard->Vlast_prefix_arg = Qnil;
1008 cancel_echoing ();
1010 /* Avoid unquittable loop if data contains a circular list. */
1011 old_level = Vprint_level;
1012 old_length = Vprint_length;
1013 XSETFASTINT (Vprint_level, 10);
1014 XSETFASTINT (Vprint_length, 10);
1015 cmd_error_internal (data, macroerror);
1016 Vprint_level = old_level;
1017 Vprint_length = old_length;
1019 Vquit_flag = Qnil;
1021 Vinhibit_quit = Qnil;
1022 #ifdef MULTI_KBOARD
1023 any_kboard_state ();
1024 #endif
1026 return make_number (0);
1029 /* Take actions on handling an error. DATA is the data that describes
1030 the error.
1032 CONTEXT is a C-string containing ASCII characters only which
1033 describes the context in which the error happened. If we need to
1034 generalize CONTEXT to allow multibyte characters, make it a Lisp
1035 string. */
1037 void
1038 cmd_error_internal (data, context)
1039 Lisp_Object data;
1040 char *context;
1042 Lisp_Object stream;
1043 int kill_emacs_p = 0;
1044 struct frame *sf = SELECTED_FRAME ();
1046 Vquit_flag = Qnil;
1047 Vinhibit_quit = Qt;
1048 clear_message (1, 0);
1050 /* If the window system or terminal frame hasn't been initialized
1051 yet, or we're not interactive, it's best to dump this message out
1052 to stderr and exit. */
1053 if (!sf->glyphs_initialized_p
1054 /* This is the case of the frame dumped with Emacs, when we're
1055 running under a window system. */
1056 || (!NILP (Vwindow_system)
1057 && !inhibit_window_system
1058 && FRAME_TERMCAP_P (sf))
1059 || noninteractive)
1061 stream = Qexternal_debugging_output;
1062 kill_emacs_p = 1;
1064 else
1066 Fdiscard_input ();
1067 bitch_at_user ();
1068 stream = Qt;
1071 if (context != 0)
1072 write_string_1 (context, -1, stream);
1074 print_error_message (data, stream);
1076 /* If the window system or terminal frame hasn't been initialized
1077 yet, or we're in -batch mode, this error should cause Emacs to exit. */
1078 if (kill_emacs_p)
1080 Fterpri (stream);
1081 Fkill_emacs (make_number (-1));
1085 Lisp_Object command_loop_1 ();
1086 Lisp_Object command_loop_2 ();
1087 Lisp_Object top_level_1 ();
1089 /* Entry to editor-command-loop.
1090 This level has the catches for exiting/returning to editor command loop.
1091 It returns nil to exit recursive edit, t to abort it. */
1093 Lisp_Object
1094 command_loop ()
1096 if (command_loop_level > 0 || minibuf_level > 0)
1098 Lisp_Object val;
1099 val = internal_catch (Qexit, command_loop_2, Qnil);
1100 executing_macro = Qnil;
1101 return val;
1103 else
1104 while (1)
1106 internal_catch (Qtop_level, top_level_1, Qnil);
1107 internal_catch (Qtop_level, command_loop_2, Qnil);
1108 executing_macro = Qnil;
1110 /* End of file in -batch run causes exit here. */
1111 if (noninteractive)
1112 Fkill_emacs (Qt);
1116 /* Here we catch errors in execution of commands within the
1117 editing loop, and reenter the editing loop.
1118 When there is an error, cmd_error runs and returns a non-nil
1119 value to us. A value of nil means that cmd_loop_1 itself
1120 returned due to end of file (or end of kbd macro). */
1122 Lisp_Object
1123 command_loop_2 ()
1125 register Lisp_Object val;
1128 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
1129 while (!NILP (val));
1131 return Qnil;
1134 Lisp_Object
1135 top_level_2 ()
1137 return Feval (Vtop_level);
1140 Lisp_Object
1141 top_level_1 ()
1143 /* On entry to the outer level, run the startup file */
1144 if (!NILP (Vtop_level))
1145 internal_condition_case (top_level_2, Qerror, cmd_error);
1146 else if (!NILP (Vpurify_flag))
1147 message ("Bare impure Emacs (standard Lisp code not loaded)");
1148 else
1149 message ("Bare Emacs (standard Lisp code not loaded)");
1150 return Qnil;
1153 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1154 "Exit all recursive editing levels.")
1157 Fthrow (Qtop_level, Qnil);
1160 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1161 "Exit from the innermost recursive edit or minibuffer.")
1164 if (command_loop_level > 0 || minibuf_level > 0)
1165 Fthrow (Qexit, Qnil);
1167 error ("No recursive edit is in progress");
1170 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1171 "Abort the command that requested this recursive edit or minibuffer input.")
1174 if (command_loop_level > 0 || minibuf_level > 0)
1175 Fthrow (Qexit, Qt);
1177 error ("No recursive edit is in progress");
1180 /* This is the actual command reading loop,
1181 sans error-handling encapsulation. */
1183 Lisp_Object Fcommand_execute ();
1184 static int read_key_sequence ();
1185 void safe_run_hooks ();
1187 Lisp_Object
1188 command_loop_1 ()
1190 Lisp_Object cmd;
1191 int lose;
1192 int nonundocount;
1193 Lisp_Object keybuf[30];
1194 int i;
1195 int no_redisplay;
1196 int no_direct;
1197 int prev_modiff;
1198 struct buffer *prev_buffer;
1199 #ifdef MULTI_KBOARD
1200 int was_locked = single_kboard;
1201 #endif
1203 current_kboard->Vprefix_arg = Qnil;
1204 current_kboard->Vlast_prefix_arg = Qnil;
1205 Vdeactivate_mark = Qnil;
1206 waiting_for_input = 0;
1207 cancel_echoing ();
1209 nonundocount = 0;
1210 no_redisplay = 0;
1211 this_command_key_count = 0;
1212 this_single_command_key_start = 0;
1214 /* Make sure this hook runs after commands that get errors and
1215 throw to top level. */
1216 /* Note that the value cell will never directly contain nil
1217 if the symbol is a local variable. */
1218 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1219 safe_run_hooks (Qpost_command_hook);
1221 /* If displaying a message, resize the echo area window to fit
1222 that message's size exactly. */
1223 if (!NILP (echo_area_buffer[0]))
1224 resize_echo_area_axactly ();
1226 if (!NILP (Vdeferred_action_list))
1227 call0 (Vdeferred_action_function);
1229 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1231 if (NILP (Vunread_command_events)
1232 && NILP (Vunread_input_method_events)
1233 && NILP (Vunread_post_input_method_events)
1234 && NILP (Vexecuting_macro)
1235 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1236 safe_run_hooks (Qpost_command_idle_hook);
1239 /* Do this after running Vpost_command_hook, for consistency. */
1240 current_kboard->Vlast_command = Vthis_command;
1241 current_kboard->Vreal_last_command = real_this_command;
1243 while (1)
1245 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1246 Fkill_emacs (Qnil);
1248 /* Make sure the current window's buffer is selected. */
1249 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1250 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1252 /* Display any malloc warning that just came out. Use while because
1253 displaying one warning can cause another. */
1255 while (pending_malloc_warning)
1256 display_malloc_warning ();
1258 no_direct = 0;
1260 Vdeactivate_mark = Qnil;
1262 /* If minibuffer on and echo area in use,
1263 wait 2 sec and redraw minibuffer. */
1265 if (minibuf_level
1266 && !NILP (echo_area_buffer[0])
1267 && EQ (minibuf_window, echo_area_window))
1269 /* Bind inhibit-quit to t so that C-g gets read in
1270 rather than quitting back to the minibuffer. */
1271 int count = specpdl_ptr - specpdl;
1272 specbind (Qinhibit_quit, Qt);
1274 Fsit_for (make_number (2), Qnil, Qnil);
1275 /* Clear the echo area. */
1276 message2 (0, 0, 0);
1277 safe_run_hooks (Qecho_area_clear_hook);
1279 unbind_to (count, Qnil);
1281 /* If a C-g came in before, treat it as input now. */
1282 if (!NILP (Vquit_flag))
1284 Vquit_flag = Qnil;
1285 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1289 #ifdef C_ALLOCA
1290 alloca (0); /* Cause a garbage collection now */
1291 /* Since we can free the most stuff here. */
1292 #endif /* C_ALLOCA */
1294 #if 0
1295 /* Select the frame that the last event came from. Usually,
1296 switch-frame events will take care of this, but if some lisp
1297 code swallows a switch-frame event, we'll fix things up here.
1298 Is this a good idea? */
1299 if (FRAMEP (internal_last_event_frame)
1300 && !EQ (internal_last_event_frame, selected_frame))
1301 Fselect_frame (internal_last_event_frame, Qnil);
1302 #endif
1303 /* If it has changed current-menubar from previous value,
1304 really recompute the menubar from the value. */
1305 if (! NILP (Vlucid_menu_bar_dirty_flag)
1306 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1307 call0 (Qrecompute_lucid_menubar);
1309 before_command_key_count = this_command_key_count;
1310 before_command_echo_length = echo_length ();
1312 Vthis_command = Qnil;
1313 real_this_command = Qnil;
1315 /* Read next key sequence; i gets its length. */
1316 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1317 Qnil, 0, 1, 1);
1319 /* A filter may have run while we were reading the input. */
1320 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1321 Fkill_emacs (Qnil);
1322 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1323 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1325 ++num_input_keys;
1327 /* Now we have read a key sequence of length I,
1328 or else I is 0 and we found end of file. */
1330 if (i == 0) /* End of file -- happens only in */
1331 return Qnil; /* a kbd macro, at the end. */
1332 /* -1 means read_key_sequence got a menu that was rejected.
1333 Just loop around and read another command. */
1334 if (i == -1)
1336 cancel_echoing ();
1337 this_command_key_count = 0;
1338 this_single_command_key_start = 0;
1339 goto finalize;
1342 last_command_char = keybuf[i - 1];
1344 /* If the previous command tried to force a specific window-start,
1345 forget about that, in case this command moves point far away
1346 from that position. But also throw away beg_unchanged and
1347 end_unchanged information in that case, so that redisplay will
1348 update the whole window properly. */
1349 if (!NILP (XWINDOW (selected_window)->force_start))
1351 struct buffer *b;
1352 XWINDOW (selected_window)->force_start = Qnil;
1353 b = XBUFFER (XWINDOW (selected_window)->buffer);
1354 BUF_BEG_UNCHANGED (b) = BUF_END_UNCHANGED (b) = 0;
1357 cmd = read_key_sequence_cmd;
1358 if (!NILP (Vexecuting_macro))
1360 if (!NILP (Vquit_flag))
1362 Vexecuting_macro = Qt;
1363 QUIT; /* Make some noise. */
1364 /* Will return since macro now empty. */
1368 /* Do redisplay processing after this command except in special
1369 cases identified below that set no_redisplay to 1.
1370 (actually, there's currently no way to prevent the redisplay,
1371 and no_redisplay is ignored.
1372 Perhaps someday we will really implement it.) */
1373 no_redisplay = 0;
1375 prev_buffer = current_buffer;
1376 prev_modiff = MODIFF;
1377 last_point_position = PT;
1378 XSETBUFFER (last_point_position_buffer, prev_buffer);
1380 /* Execute the command. */
1382 Vthis_command = cmd;
1383 real_this_command = cmd;
1384 /* Note that the value cell will never directly contain nil
1385 if the symbol is a local variable. */
1386 if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
1387 safe_run_hooks (Qpre_command_hook);
1389 if (NILP (Vthis_command))
1391 /* nil means key is undefined. */
1392 bitch_at_user ();
1393 current_kboard->defining_kbd_macro = Qnil;
1394 update_mode_lines = 1;
1395 current_kboard->Vprefix_arg = Qnil;
1397 else
1399 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1401 /* In case we jump to directly_done. */
1402 Vcurrent_prefix_arg = current_kboard->Vprefix_arg;
1404 /* Recognize some common commands in common situations and
1405 do them directly. */
1406 if (EQ (Vthis_command, Qforward_char) && PT < ZV)
1408 struct Lisp_Char_Table *dp
1409 = window_display_table (XWINDOW (selected_window));
1410 lose = FETCH_CHAR (PT_BYTE);
1411 SET_PT (PT + 1);
1412 if ((dp
1413 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1414 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1415 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1416 && (lose >= 0x20 && lose < 0x7f)))
1417 : (lose >= 0x20 && lose < 0x7f))
1418 /* To extract the case of continuation on
1419 wide-column characters. */
1420 && (WIDTH_BY_CHAR_HEAD (FETCH_BYTE (PT_BYTE)) == 1)
1421 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1422 >= MODIFF)
1423 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1424 >= OVERLAY_MODIFF)
1425 && (XFASTINT (XWINDOW (selected_window)->last_point)
1426 == PT - 1)
1427 && !windows_or_buffers_changed
1428 && EQ (current_buffer->selective_display, Qnil)
1429 && !detect_input_pending ()
1430 && NILP (XWINDOW (selected_window)->column_number_displayed)
1431 && NILP (Vexecuting_macro))
1432 no_redisplay = direct_output_forward_char (1);
1433 goto directly_done;
1435 else if (EQ (Vthis_command, Qbackward_char) && PT > BEGV)
1437 struct Lisp_Char_Table *dp
1438 = window_display_table (XWINDOW (selected_window));
1439 SET_PT (PT - 1);
1440 lose = FETCH_CHAR (PT_BYTE);
1441 if ((dp
1442 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1443 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1444 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1445 && (lose >= 0x20 && lose < 0x7f)))
1446 : (lose >= 0x20 && lose < 0x7f))
1447 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1448 >= MODIFF)
1449 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1450 >= OVERLAY_MODIFF)
1451 && (XFASTINT (XWINDOW (selected_window)->last_point)
1452 == PT + 1)
1453 && !windows_or_buffers_changed
1454 && EQ (current_buffer->selective_display, Qnil)
1455 && !detect_input_pending ()
1456 && NILP (XWINDOW (selected_window)->column_number_displayed)
1457 && NILP (Vexecuting_macro))
1458 no_redisplay = direct_output_forward_char (-1);
1459 goto directly_done;
1461 else if (EQ (Vthis_command, Qself_insert_command)
1462 /* Try this optimization only on ascii keystrokes. */
1463 && INTEGERP (last_command_char))
1465 unsigned int c = XINT (last_command_char);
1466 int value;
1467 if (NILP (Vexecuting_macro)
1468 && !EQ (minibuf_window, selected_window))
1470 if (!nonundocount || nonundocount >= 20)
1472 Fundo_boundary ();
1473 nonundocount = 0;
1475 nonundocount++;
1478 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1479 < MODIFF)
1480 || (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1481 < OVERLAY_MODIFF)
1482 || (XFASTINT (XWINDOW (selected_window)->last_point)
1483 != PT)
1484 || MODIFF <= SAVE_MODIFF
1485 || windows_or_buffers_changed
1486 || !EQ (current_buffer->selective_display, Qnil)
1487 || detect_input_pending ()
1488 || !NILP (XWINDOW (selected_window)->column_number_displayed)
1489 || !NILP (Vexecuting_macro));
1491 value = internal_self_insert (c, 0);
1493 if (value == 2)
1494 nonundocount = 0;
1496 /* VALUE == 1 when AFTER-CHANGE functions are
1497 installed which is the case most of the time
1498 because FONT-LOCK installs one. */
1499 if (!lose && !value)
1500 no_redisplay = direct_output_for_insert (c);
1501 goto directly_done;
1505 /* Here for a command that isn't executed directly */
1507 #ifdef HAVE_X_WINDOWS
1508 if (display_busy_cursor_p)
1510 if (inhibit_busy_cursor != 2)
1511 inhibit_busy_cursor = 0;
1512 if (!inhibit_busy_cursor)
1513 Fx_show_busy_cursor ();
1515 #endif
1517 nonundocount = 0;
1518 if (NILP (current_kboard->Vprefix_arg))
1519 Fundo_boundary ();
1520 Fcommand_execute (Vthis_command, Qnil, Qnil, Qnil);
1522 directly_done: ;
1523 current_kboard->Vlast_prefix_arg = Vcurrent_prefix_arg;
1525 /* Note that the value cell will never directly contain nil
1526 if the symbol is a local variable. */
1527 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1528 safe_run_hooks (Qpost_command_hook);
1530 /* If displaying a message, resize the echo area window to fit
1531 that message's size exactly. */
1532 if (!NILP (echo_area_buffer[0]))
1533 resize_echo_area_axactly ();
1535 if (!NILP (Vdeferred_action_list))
1536 safe_run_hooks (Qdeferred_action_function);
1538 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1540 if (NILP (Vunread_command_events)
1541 && NILP (Vunread_input_method_events)
1542 && NILP (Vunread_post_input_method_events)
1543 && NILP (Vexecuting_macro)
1544 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1545 safe_run_hooks (Qpost_command_idle_hook);
1548 /* If there is a prefix argument,
1549 1) We don't want Vlast_command to be ``universal-argument''
1550 (that would be dumb), so don't set Vlast_command,
1551 2) we want to leave echoing on so that the prefix will be
1552 echoed as part of this key sequence, so don't call
1553 cancel_echoing, and
1554 3) we want to leave this_command_key_count non-zero, so that
1555 read_char will realize that it is re-reading a character, and
1556 not echo it a second time.
1558 If the command didn't actually create a prefix arg,
1559 but is merely a frame event that is transparent to prefix args,
1560 then the above doesn't apply. */
1561 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1563 current_kboard->Vlast_command = Vthis_command;
1564 current_kboard->Vreal_last_command = real_this_command;
1565 cancel_echoing ();
1566 this_command_key_count = 0;
1567 this_single_command_key_start = 0;
1570 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1572 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1574 current_buffer->mark_active = Qnil;
1575 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1577 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1578 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1581 finalize:
1582 /* Install chars successfully executed in kbd macro. */
1584 if (!NILP (current_kboard->defining_kbd_macro)
1585 && NILP (current_kboard->Vprefix_arg))
1586 finalize_kbd_macro_chars ();
1588 #ifdef MULTI_KBOARD
1589 if (!was_locked)
1590 any_kboard_state ();
1591 #endif
1595 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1597 static Lisp_Object
1598 safe_run_hooks_1 (hook)
1599 Lisp_Object hook;
1601 return call1 (Vrun_hooks, Vinhibit_quit);
1604 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1606 static Lisp_Object
1607 safe_run_hooks_error (data)
1608 Lisp_Object data;
1610 Fset (Vinhibit_quit, Qnil);
1613 /* If we get an error while running the hook, cause the hook variable
1614 to be nil. Also inhibit quits, so that C-g won't cause the hook
1615 to mysteriously evaporate. */
1617 void
1618 safe_run_hooks (hook)
1619 Lisp_Object hook;
1621 int count = specpdl_ptr - specpdl;
1622 specbind (Qinhibit_quit, hook);
1624 internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
1626 unbind_to (count, Qnil);
1629 /* Number of seconds between polling for input. */
1630 int polling_period;
1632 /* Nonzero means polling for input is temporarily suppressed. */
1633 int poll_suppress_count;
1635 /* Nonzero if polling_for_input is actually being used. */
1636 int polling_for_input;
1638 #ifdef POLL_FOR_INPUT
1640 /* Handle an alarm once each second and read pending input
1641 so as to handle a C-g if it comces in. */
1643 SIGTYPE
1644 input_poll_signal (signalnum) /* If we don't have an argument, */
1645 int signalnum; /* some compilers complain in signal calls. */
1647 /* This causes the call to start_polling at the end
1648 to do its job. It also arranges for a quit or error
1649 from within read_avail_input to resume polling. */
1650 poll_suppress_count++;
1651 if (interrupt_input_blocked == 0
1652 && !waiting_for_input)
1653 read_avail_input (0);
1654 /* Turn on the SIGALRM handler and request another alarm. */
1655 start_polling ();
1658 #endif
1660 /* Begin signals to poll for input, if they are appropriate.
1661 This function is called unconditionally from various places. */
1663 void
1664 start_polling ()
1666 #ifdef POLL_FOR_INPUT
1667 if (read_socket_hook && !interrupt_input)
1669 poll_suppress_count--;
1670 if (poll_suppress_count == 0)
1672 signal (SIGALRM, input_poll_signal);
1673 polling_for_input = 1;
1674 alarm (polling_period);
1677 #endif
1680 /* Nonzero if we are using polling to handle input asynchronously. */
1683 input_polling_used ()
1685 #ifdef POLL_FOR_INPUT
1686 return read_socket_hook && !interrupt_input;
1687 #else
1688 return 0;
1689 #endif
1692 /* Turn off polling. */
1694 void
1695 stop_polling ()
1697 #ifdef POLL_FOR_INPUT
1698 if (read_socket_hook && !interrupt_input)
1700 if (poll_suppress_count == 0)
1702 polling_for_input = 0;
1703 alarm (0);
1705 poll_suppress_count++;
1707 #endif
1710 /* Set the value of poll_suppress_count to COUNT
1711 and start or stop polling accordingly. */
1713 void
1714 set_poll_suppress_count (count)
1715 int count;
1717 #ifdef POLL_FOR_INPUT
1718 if (count == 0 && poll_suppress_count != 0)
1720 poll_suppress_count = 1;
1721 start_polling ();
1723 else if (count != 0 && poll_suppress_count == 0)
1725 stop_polling ();
1727 poll_suppress_count = count;
1728 #endif
1731 /* Bind polling_period to a value at least N.
1732 But don't decrease it. */
1734 void
1735 bind_polling_period (n)
1736 int n;
1738 #ifdef POLL_FOR_INPUT
1739 int new = polling_period;
1741 if (n > new)
1742 new = n;
1744 stop_polling ();
1745 specbind (Qpolling_period, make_number (new));
1746 /* Start a new alarm with the new period. */
1747 start_polling ();
1748 #endif
1751 /* Apply the control modifier to CHARACTER. */
1754 make_ctrl_char (c)
1755 int c;
1757 /* Save the upper bits here. */
1758 int upper = c & ~0177;
1760 c &= 0177;
1762 /* Everything in the columns containing the upper-case letters
1763 denotes a control character. */
1764 if (c >= 0100 && c < 0140)
1766 int oc = c;
1767 c &= ~0140;
1768 /* Set the shift modifier for a control char
1769 made from a shifted letter. But only for letters! */
1770 if (oc >= 'A' && oc <= 'Z')
1771 c |= shift_modifier;
1774 /* The lower-case letters denote control characters too. */
1775 else if (c >= 'a' && c <= 'z')
1776 c &= ~0140;
1778 /* Include the bits for control and shift
1779 only if the basic ASCII code can't indicate them. */
1780 else if (c >= ' ')
1781 c |= ctrl_modifier;
1783 /* Replace the high bits. */
1784 c |= (upper & ~ctrl_modifier);
1786 return c;
1791 /* Input of single characters from keyboard */
1793 Lisp_Object print_help ();
1794 static Lisp_Object kbd_buffer_get_event ();
1795 static void record_char ();
1797 #ifdef MULTI_KBOARD
1798 static jmp_buf wrong_kboard_jmpbuf;
1799 #endif
1801 /* read a character from the keyboard; call the redisplay if needed */
1802 /* commandflag 0 means do not do auto-saving, but do do redisplay.
1803 -1 means do not do redisplay, but do do autosaving.
1804 1 means do both. */
1806 /* The arguments MAPS and NMAPS are for menu prompting.
1807 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1809 PREV_EVENT is the previous input event, or nil if we are reading
1810 the first event of a key sequence (or not reading a key sequence).
1811 If PREV_EVENT is t, that is a "magic" value that says
1812 not to run input methods, but in other respects to act as if
1813 not reading a key sequence.
1815 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
1816 if we used a mouse menu to read the input, or zero otherwise. If
1817 USED_MOUSE_MENU is null, we don't dereference it.
1819 Value is t if we showed a menu and the user rejected it. */
1821 Lisp_Object
1822 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
1823 int commandflag;
1824 int nmaps;
1825 Lisp_Object *maps;
1826 Lisp_Object prev_event;
1827 int *used_mouse_menu;
1829 Lisp_Object c;
1830 int count;
1831 jmp_buf local_getcjmp;
1832 jmp_buf save_jump;
1833 int key_already_recorded = 0;
1834 Lisp_Object tem, save;
1835 Lisp_Object previous_echo_area_message;
1836 Lisp_Object also_record;
1837 int reread;
1838 struct gcpro gcpro1, gcpro2;
1840 also_record = Qnil;
1842 before_command_key_count = this_command_key_count;
1843 before_command_echo_length = echo_length ();
1844 c = Qnil;
1845 previous_echo_area_message = Qnil;
1847 GCPRO2 (c, previous_echo_area_message);
1849 retry:
1851 reread = 0;
1852 if (CONSP (Vunread_post_input_method_events))
1854 c = XCONS (Vunread_post_input_method_events)->car;
1855 Vunread_post_input_method_events
1856 = XCONS (Vunread_post_input_method_events)->cdr;
1858 /* Undo what read_char_x_menu_prompt did when it unread
1859 additional keys returned by Fx_popup_menu. */
1860 if (CONSP (c)
1861 && (SYMBOLP (XCONS (c)->car) || INTEGERP (XCONS (c)->car))
1862 && NILP (XCONS (c)->cdr))
1863 c = XCONS (c)->car;
1865 reread = 1;
1866 goto reread_first;
1869 if (unread_command_char != -1)
1871 XSETINT (c, unread_command_char);
1872 unread_command_char = -1;
1874 reread = 1;
1875 goto reread_first;
1878 if (CONSP (Vunread_command_events))
1880 c = XCONS (Vunread_command_events)->car;
1881 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
1883 /* Undo what read_char_x_menu_prompt did when it unread
1884 additional keys returned by Fx_popup_menu. */
1885 if (CONSP (c)
1886 && (SYMBOLP (XCONS (c)->car) || INTEGERP (XCONS (c)->car))
1887 && NILP (XCONS (c)->cdr))
1888 c = XCONS (c)->car;
1890 reread = 1;
1891 goto reread_for_input_method;
1894 if (CONSP (Vunread_input_method_events))
1896 c = XCONS (Vunread_input_method_events)->car;
1897 Vunread_input_method_events = XCONS (Vunread_input_method_events)->cdr;
1899 /* Undo what read_char_x_menu_prompt did when it unread
1900 additional keys returned by Fx_popup_menu. */
1901 if (CONSP (c)
1902 && (SYMBOLP (XCONS (c)->car) || INTEGERP (XCONS (c)->car))
1903 && NILP (XCONS (c)->cdr))
1904 c = XCONS (c)->car;
1905 reread = 1;
1906 goto reread_for_input_method;
1909 /* If there is no function key translated before
1910 reset-this-command-lengths takes effect, forget about it. */
1911 before_command_restore_flag = 0;
1913 if (!NILP (Vexecuting_macro))
1915 /* We set this to Qmacro; since that's not a frame, nobody will
1916 try to switch frames on us, and the selected window will
1917 remain unchanged.
1919 Since this event came from a macro, it would be misleading to
1920 leave internal_last_event_frame set to wherever the last
1921 real event came from. Normally, a switch-frame event selects
1922 internal_last_event_frame after each command is read, but
1923 events read from a macro should never cause a new frame to be
1924 selected. */
1925 Vlast_event_frame = internal_last_event_frame = Qmacro;
1927 /* Exit the macro if we are at the end.
1928 Also, some things replace the macro with t
1929 to force an early exit. */
1930 if (EQ (Vexecuting_macro, Qt)
1931 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
1933 XSETINT (c, -1);
1934 RETURN_UNGCPRO (c);
1937 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
1938 if (STRINGP (Vexecuting_macro)
1939 && (XINT (c) & 0x80))
1940 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
1942 executing_macro_index++;
1944 goto from_macro;
1947 if (!NILP (unread_switch_frame))
1949 c = unread_switch_frame;
1950 unread_switch_frame = Qnil;
1952 /* This event should make it into this_command_keys, and get echoed
1953 again, so we do not set `reread'. */
1954 goto reread_first;
1957 /* if redisplay was requested */
1958 if (commandflag >= 0)
1960 /* If there is pending input, process any events which are not
1961 user-visible, such as X selection_request events. */
1962 if (input_pending
1963 || detect_input_pending_run_timers (0))
1964 swallow_events (0); /* may clear input_pending */
1966 /* Redisplay if no pending input. */
1967 while (!input_pending)
1969 redisplay ();
1971 if (!input_pending)
1972 /* Normal case: no input arrived during redisplay. */
1973 break;
1975 /* Input arrived and pre-empted redisplay.
1976 Process any events which are not user-visible. */
1977 swallow_events (0);
1978 /* If that cleared input_pending, try again to redisplay. */
1982 /* Message turns off echoing unless more keystrokes turn it on again. */
1983 if (/* There is a current message. */
1984 !NILP (echo_area_buffer[0])
1985 /* And we're not echoing from this kboard. */
1986 && echo_kboard != current_kboard
1987 /* And it's either not ok to echo (ok_to_echo == NULL), or the
1988 last char echoed was from a different kboard. */
1989 && ok_to_echo_at_next_pause != echo_kboard)
1990 cancel_echoing ();
1991 else
1992 /* If already echoing, continue. */
1993 echo_dash ();
1995 /* Try reading a character via menu prompting in the minibuf.
1996 Try this before the sit-for, because the sit-for
1997 would do the wrong thing if we are supposed to do
1998 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
1999 after a mouse event so don't try a minibuf menu. */
2000 c = Qnil;
2001 if (nmaps > 0 && INTERACTIVE
2002 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
2003 /* Don't bring up a menu if we already have another event. */
2004 && NILP (Vunread_command_events)
2005 && unread_command_char < 0
2006 && !detect_input_pending_run_timers (0))
2008 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
2009 if (! NILP (c))
2011 key_already_recorded = 1;
2012 goto non_reread_1;
2016 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
2017 We will do that below, temporarily for short sections of code,
2018 when appropriate. local_getcjmp must be in effect
2019 around any call to sit_for or kbd_buffer_get_event;
2020 it *must not* be in effect when we call redisplay. */
2022 if (_setjmp (local_getcjmp))
2024 XSETINT (c, quit_char);
2025 internal_last_event_frame = selected_frame;
2026 Vlast_event_frame = internal_last_event_frame;
2027 /* If we report the quit char as an event,
2028 don't do so more than once. */
2029 if (!NILP (Vinhibit_quit))
2030 Vquit_flag = Qnil;
2032 #ifdef MULTI_KBOARD
2034 KBOARD *kb = FRAME_KBOARD (XFRAME (selected_frame));
2035 if (kb != current_kboard)
2037 Lisp_Object *tailp = &kb->kbd_queue;
2038 /* We shouldn't get here if we were in single-kboard mode! */
2039 if (single_kboard)
2040 abort ();
2041 while (CONSP (*tailp))
2042 tailp = &XCONS (*tailp)->cdr;
2043 if (!NILP (*tailp))
2044 abort ();
2045 *tailp = Fcons (c, Qnil);
2046 kb->kbd_queue_has_data = 1;
2047 current_kboard = kb;
2048 /* This is going to exit from read_char
2049 so we had better get rid of this frame's stuff. */
2050 UNGCPRO;
2051 longjmp (wrong_kboard_jmpbuf, 1);
2054 #endif
2055 goto non_reread;
2058 timer_start_idle ();
2060 /* If in middle of key sequence and minibuffer not active,
2061 start echoing if enough time elapses. */
2063 if (minibuf_level == 0
2064 && !current_kboard->immediate_echo
2065 && this_command_key_count > 0
2066 && ! noninteractive
2067 && echo_keystrokes > 0
2068 && (/* No message. */
2069 NILP (echo_area_buffer[0])
2070 /* Or empty message. */
2071 || (BUF_BEG (XBUFFER (echo_area_buffer[0]))
2072 == BUF_Z (XBUFFER (echo_area_buffer[0])))
2073 /* Or already echoing from same kboard. */
2074 || (echo_kboard && ok_to_echo_at_next_pause == echo_kboard)
2075 /* Or not echoing before and echoing allowed. */
2076 || (!echo_kboard && ok_to_echo_at_next_pause)))
2078 Lisp_Object tem0;
2080 /* After a mouse event, start echoing right away.
2081 This is because we are probably about to display a menu,
2082 and we don't want to delay before doing so. */
2083 if (EVENT_HAS_PARAMETERS (prev_event))
2084 echo_now ();
2085 else
2087 save_getcjmp (save_jump);
2088 restore_getcjmp (local_getcjmp);
2089 tem0 = sit_for (echo_keystrokes, 0, 1, 1, 0);
2090 restore_getcjmp (save_jump);
2091 if (EQ (tem0, Qt)
2092 && ! CONSP (Vunread_command_events))
2093 echo_now ();
2097 /* Maybe auto save due to number of keystrokes. */
2099 if (commandflag != 0
2100 && auto_save_interval > 0
2101 && num_nonmacro_input_events - last_auto_save > max (auto_save_interval, 20)
2102 && !detect_input_pending_run_timers (0))
2104 Fdo_auto_save (Qnil, Qnil);
2105 /* Hooks can actually change some buffers in auto save. */
2106 redisplay ();
2109 /* Try reading using an X menu.
2110 This is never confused with reading using the minibuf
2111 because the recursive call of read_char in read_char_minibuf_menu_prompt
2112 does not pass on any keymaps. */
2114 if (nmaps > 0 && INTERACTIVE
2115 && !NILP (prev_event)
2116 && EVENT_HAS_PARAMETERS (prev_event)
2117 && !EQ (XCONS (prev_event)->car, Qmenu_bar)
2118 && !EQ (XCONS (prev_event)->car, Qtool_bar)
2119 /* Don't bring up a menu if we already have another event. */
2120 && NILP (Vunread_command_events)
2121 && unread_command_char < 0)
2123 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
2125 /* Now that we have read an event, Emacs is not idle. */
2126 timer_stop_idle ();
2128 RETURN_UNGCPRO (c);
2131 /* Maybe autosave and/or garbage collect due to idleness. */
2133 if (INTERACTIVE && NILP (c))
2135 int delay_level, buffer_size;
2137 /* Slow down auto saves logarithmically in size of current buffer,
2138 and garbage collect while we're at it. */
2139 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
2140 last_non_minibuf_size = Z - BEG;
2141 buffer_size = (last_non_minibuf_size >> 8) + 1;
2142 delay_level = 0;
2143 while (buffer_size > 64)
2144 delay_level++, buffer_size -= buffer_size >> 2;
2145 if (delay_level < 4) delay_level = 4;
2146 /* delay_level is 4 for files under around 50k, 7 at 100k,
2147 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
2149 /* Auto save if enough time goes by without input. */
2150 if (commandflag != 0
2151 && num_nonmacro_input_events > last_auto_save
2152 && INTEGERP (Vauto_save_timeout)
2153 && XINT (Vauto_save_timeout) > 0)
2155 Lisp_Object tem0;
2157 save_getcjmp (save_jump);
2158 restore_getcjmp (local_getcjmp);
2159 tem0 = sit_for (delay_level * XFASTINT (Vauto_save_timeout) / 4,
2160 0, 1, 1, 0);
2161 restore_getcjmp (save_jump);
2163 if (EQ (tem0, Qt)
2164 && ! CONSP (Vunread_command_events))
2166 Fdo_auto_save (Qnil, Qnil);
2168 /* If we have auto-saved and there is still no input
2169 available, garbage collect if there has been enough
2170 consing going on to make it worthwhile. */
2171 if (!detect_input_pending_run_timers (0)
2172 && consing_since_gc > gc_cons_threshold / 2)
2173 Fgarbage_collect ();
2175 redisplay ();
2180 /* If this has become non-nil here, it has been set by a timer
2181 or sentinel or filter. */
2182 if (CONSP (Vunread_command_events))
2184 c = XCONS (Vunread_command_events)->car;
2185 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
2188 /* Read something from current KBOARD's side queue, if possible. */
2190 if (NILP (c))
2192 if (current_kboard->kbd_queue_has_data)
2194 if (!CONSP (current_kboard->kbd_queue))
2195 abort ();
2196 c = XCONS (current_kboard->kbd_queue)->car;
2197 current_kboard->kbd_queue
2198 = XCONS (current_kboard->kbd_queue)->cdr;
2199 if (NILP (current_kboard->kbd_queue))
2200 current_kboard->kbd_queue_has_data = 0;
2201 input_pending = readable_events (0);
2202 if (EVENT_HAS_PARAMETERS (c)
2203 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
2204 internal_last_event_frame = XCONS (XCONS (c)->cdr)->car;
2205 Vlast_event_frame = internal_last_event_frame;
2209 #ifdef MULTI_KBOARD
2210 /* If current_kboard's side queue is empty check the other kboards.
2211 If one of them has data that we have not yet seen here,
2212 switch to it and process the data waiting for it.
2214 Note: if the events queued up for another kboard
2215 have already been seen here, and therefore are not a complete command,
2216 the kbd_queue_has_data field is 0, so we skip that kboard here.
2217 That's to avoid an infinite loop switching between kboards here. */
2218 if (NILP (c) && !single_kboard)
2220 KBOARD *kb;
2221 for (kb = all_kboards; kb; kb = kb->next_kboard)
2222 if (kb->kbd_queue_has_data)
2224 current_kboard = kb;
2225 /* This is going to exit from read_char
2226 so we had better get rid of this frame's stuff. */
2227 UNGCPRO;
2228 longjmp (wrong_kboard_jmpbuf, 1);
2231 #endif
2233 wrong_kboard:
2235 stop_polling ();
2237 /* Finally, we read from the main queue,
2238 and if that gives us something we can't use yet, we put it on the
2239 appropriate side queue and try again. */
2241 if (NILP (c))
2243 KBOARD *kb;
2245 /* Actually read a character, waiting if necessary. */
2246 save_getcjmp (save_jump);
2247 restore_getcjmp (local_getcjmp);
2248 c = kbd_buffer_get_event (&kb, used_mouse_menu);
2249 restore_getcjmp (save_jump);
2251 #ifdef MULTI_KBOARD
2252 if (! NILP (c) && (kb != current_kboard))
2254 Lisp_Object *tailp = &kb->kbd_queue;
2255 while (CONSP (*tailp))
2256 tailp = &XCONS (*tailp)->cdr;
2257 if (!NILP (*tailp))
2258 abort ();
2259 *tailp = Fcons (c, Qnil);
2260 kb->kbd_queue_has_data = 1;
2261 c = Qnil;
2262 if (single_kboard)
2263 goto wrong_kboard;
2264 current_kboard = kb;
2265 /* This is going to exit from read_char
2266 so we had better get rid of this frame's stuff. */
2267 UNGCPRO;
2268 longjmp (wrong_kboard_jmpbuf, 1);
2270 #endif
2273 /* Terminate Emacs in batch mode if at eof. */
2274 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
2275 Fkill_emacs (make_number (1));
2277 if (INTEGERP (c))
2279 /* Add in any extra modifiers, where appropriate. */
2280 if ((extra_keyboard_modifiers & CHAR_CTL)
2281 || ((extra_keyboard_modifiers & 0177) < ' '
2282 && (extra_keyboard_modifiers & 0177) != 0))
2283 XSETINT (c, make_ctrl_char (XINT (c)));
2285 /* Transfer any other modifier bits directly from
2286 extra_keyboard_modifiers to c. Ignore the actual character code
2287 in the low 16 bits of extra_keyboard_modifiers. */
2288 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
2291 non_reread:
2293 timer_stop_idle ();
2295 start_polling ();
2297 if (NILP (c))
2299 if (commandflag >= 0
2300 && !input_pending && !detect_input_pending_run_timers (0))
2301 redisplay ();
2303 goto wrong_kboard;
2306 non_reread_1:
2308 /* Buffer switch events are only for internal wakeups
2309 so don't show them to the user.
2310 Also, don't record a key if we already did. */
2311 if (BUFFERP (c) || key_already_recorded)
2312 RETURN_UNGCPRO (c);
2314 /* Process special events within read_char
2315 and loop around to read another event. */
2316 save = Vquit_flag;
2317 Vquit_flag = Qnil;
2318 tem = get_keyelt (access_keymap (get_keymap_1 (Vspecial_event_map, 0, 0),
2319 c, 0, 0), 1);
2320 Vquit_flag = save;
2322 if (!NILP (tem))
2324 int was_locked = single_kboard;
2326 last_input_char = c;
2327 Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt);
2329 /* Resume allowing input from any kboard, if that was true before. */
2330 if (!was_locked)
2331 any_kboard_state ();
2333 goto retry;
2336 /* Handle things that only apply to characters. */
2337 if (INTEGERP (c))
2339 /* If kbd_buffer_get_event gave us an EOF, return that. */
2340 if (XINT (c) == -1)
2341 RETURN_UNGCPRO (c);
2343 if ((STRINGP (Vkeyboard_translate_table)
2344 && XSTRING (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2345 || (VECTORP (Vkeyboard_translate_table)
2346 && XVECTOR (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2347 || (CHAR_TABLE_P (Vkeyboard_translate_table)
2348 && CHAR_TABLE_ORDINARY_SLOTS > (unsigned) XFASTINT (c)))
2350 Lisp_Object d;
2351 d = Faref (Vkeyboard_translate_table, c);
2352 /* nil in keyboard-translate-table means no translation. */
2353 if (!NILP (d))
2354 c = d;
2358 /* If this event is a mouse click in the menu bar,
2359 return just menu-bar for now. Modify the mouse click event
2360 so we won't do this twice, then queue it up. */
2361 if (EVENT_HAS_PARAMETERS (c)
2362 && CONSP (XCONS (c)->cdr)
2363 && CONSP (EVENT_START (c))
2364 && CONSP (XCONS (EVENT_START (c))->cdr))
2366 Lisp_Object posn;
2368 posn = POSN_BUFFER_POSN (EVENT_START (c));
2369 /* Handle menu-bar events:
2370 insert the dummy prefix event `menu-bar'. */
2371 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
2373 /* Change menu-bar to (menu-bar) as the event "position". */
2374 POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
2376 also_record = c;
2377 Vunread_command_events = Fcons (c, Vunread_command_events);
2378 c = posn;
2382 /* Store these characters into recent_keys, the dribble file if any,
2383 and the keyboard macro being defined, if any. */
2384 record_char (c);
2385 if (! NILP (also_record))
2386 record_char (also_record);
2388 /* Wipe the echo area.
2389 But first, if we are about to use an input method,
2390 save the echo area contents for it to refer to. */
2391 if (INTEGERP (c)
2392 && ! NILP (Vinput_method_function)
2393 && (unsigned) XINT (c) >= ' '
2394 && (unsigned) XINT (c) < 127)
2396 previous_echo_area_message = Fcurrent_message ();
2397 Vinput_method_previous_message = previous_echo_area_message;
2400 /* Now wipe the echo area. */
2401 if (!NILP (echo_area_buffer[0]))
2402 safe_run_hooks (Qecho_area_clear_hook);
2403 clear_message (1, 0);
2405 reread_for_input_method:
2406 from_macro:
2407 /* Pass this to the input method, if appropriate. */
2408 if (INTEGERP (c)
2409 && ! NILP (Vinput_method_function)
2410 /* Don't run the input method within a key sequence,
2411 after the first event of the key sequence. */
2412 && NILP (prev_event)
2413 && (unsigned) XINT (c) >= ' '
2414 && (unsigned) XINT (c) < 127)
2416 Lisp_Object keys;
2417 int key_count;
2418 struct gcpro gcpro1;
2419 int count = specpdl_ptr - specpdl;
2421 /* Save the echo status. */
2422 int saved_immediate_echo = current_kboard->immediate_echo;
2423 struct kboard *saved_ok_to_echo = ok_to_echo_at_next_pause;
2424 int saved_echo_after_prompt = current_kboard->echo_after_prompt;
2426 if (before_command_restore_flag)
2428 this_command_key_count = before_command_key_count_1;
2429 if (this_command_key_count < this_single_command_key_start)
2430 this_single_command_key_start = this_command_key_count;
2431 echo_truncate (before_command_echo_length_1);
2432 before_command_restore_flag = 0;
2435 /* Save the this_command_keys status. */
2436 key_count = this_command_key_count;
2438 if (key_count > 0)
2439 keys = Fcopy_sequence (this_command_keys);
2440 else
2441 keys = Qnil;
2442 GCPRO1 (keys);
2444 /* Clear out this_command_keys. */
2445 this_command_key_count = 0;
2447 /* Now wipe the echo area. */
2448 if (!NILP (echo_area_buffer[0]))
2449 safe_run_hooks (Qecho_area_clear_hook);
2450 clear_message (1, 0);
2451 echo_truncate (0);
2453 /* If we are not reading a key sequence,
2454 never use the echo area. */
2455 if (maps == 0)
2457 specbind (Qinput_method_use_echo_area, Qt);
2460 /* Call the input method. */
2461 tem = call1 (Vinput_method_function, c);
2463 tem = unbind_to (count, tem);
2465 /* Restore the saved echoing state
2466 and this_command_keys state. */
2467 this_command_key_count = key_count;
2468 if (key_count > 0)
2469 this_command_keys = keys;
2471 cancel_echoing ();
2472 ok_to_echo_at_next_pause = saved_ok_to_echo;
2473 current_kboard->echo_after_prompt = saved_echo_after_prompt;
2474 if (saved_immediate_echo)
2475 echo_now ();
2477 UNGCPRO;
2479 /* The input method can return no events. */
2480 if (! CONSP (tem))
2482 /* Bring back the previous message, if any. */
2483 if (! NILP (previous_echo_area_message))
2484 message_with_string ("%s", previous_echo_area_message, 0);
2485 goto retry;
2487 /* It returned one event or more. */
2488 c = XCONS (tem)->car;
2489 Vunread_post_input_method_events
2490 = nconc2 (XCONS (tem)->cdr, Vunread_post_input_method_events);
2493 reread_first:
2495 /* Display help if not echoing. */
2496 if (CONSP (c)
2497 && EQ (XCAR (c), Qhelp_echo))
2499 Lisp_Object msg = XCDR (XCDR (c));
2501 if (!NILP (Vshow_help_function))
2502 call1 (Vshow_help_function, msg);
2503 else if (!echoing && !MINI_WINDOW_P (XWINDOW (selected_window)))
2505 if (STRINGP (msg))
2506 message3_nolog (msg, XSTRING (msg)->size, STRING_MULTIBYTE (msg));
2507 else
2508 message (0);
2511 goto retry;
2514 if (this_command_key_count == 0 || ! reread)
2516 before_command_key_count = this_command_key_count;
2517 before_command_echo_length = echo_length ();
2519 /* Don't echo mouse motion events. */
2520 if (echo_keystrokes
2521 && ! (EVENT_HAS_PARAMETERS (c)
2522 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
2524 echo_char (c);
2525 if (! NILP (also_record))
2526 echo_char (also_record);
2527 /* Once we reread a character, echoing can happen
2528 the next time we pause to read a new one. */
2529 ok_to_echo_at_next_pause = current_kboard;
2532 /* Record this character as part of the current key. */
2533 add_command_key (c);
2534 if (! NILP (also_record))
2535 add_command_key (also_record);
2538 last_input_char = c;
2539 num_input_events++;
2541 /* Process the help character specially if enabled */
2542 if (!NILP (Vhelp_form) && help_char_p (c))
2544 Lisp_Object tem0;
2545 count = specpdl_ptr - specpdl;
2547 record_unwind_protect (Fset_window_configuration,
2548 Fcurrent_window_configuration (Qnil));
2550 tem0 = Feval (Vhelp_form);
2551 if (STRINGP (tem0))
2552 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
2554 cancel_echoing ();
2556 c = read_char (0, 0, 0, Qnil, 0);
2557 while (BUFFERP (c));
2558 /* Remove the help from the frame */
2559 unbind_to (count, Qnil);
2561 redisplay ();
2562 if (EQ (c, make_number (040)))
2564 cancel_echoing ();
2566 c = read_char (0, 0, 0, Qnil, 0);
2567 while (BUFFERP (c));
2571 RETURN_UNGCPRO (c);
2574 /* Record a key that came from a mouse menu.
2575 Record it for echoing, for this-command-keys, and so on. */
2577 static void
2578 record_menu_key (c)
2579 Lisp_Object c;
2581 /* Wipe the echo area. */
2582 clear_message (1, 0);
2584 record_char (c);
2586 before_command_key_count = this_command_key_count;
2587 before_command_echo_length = echo_length ();
2589 /* Don't echo mouse motion events. */
2590 if (echo_keystrokes)
2592 echo_char (c);
2594 /* Once we reread a character, echoing can happen
2595 the next time we pause to read a new one. */
2596 ok_to_echo_at_next_pause = 0;
2599 /* Record this character as part of the current key. */
2600 add_command_key (c);
2602 /* Re-reading in the middle of a command */
2603 last_input_char = c;
2604 num_input_events++;
2607 /* Return 1 if should recognize C as "the help character". */
2610 help_char_p (c)
2611 Lisp_Object c;
2613 Lisp_Object tail;
2615 if (EQ (c, Vhelp_char))
2616 return 1;
2617 for (tail = Vhelp_event_list; CONSP (tail); tail = XCONS (tail)->cdr)
2618 if (EQ (c, XCONS (tail)->car))
2619 return 1;
2620 return 0;
2623 /* Record the input event C in various ways. */
2625 static void
2626 record_char (c)
2627 Lisp_Object c;
2629 total_keys++;
2630 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
2631 if (++recent_keys_index >= NUM_RECENT_KEYS)
2632 recent_keys_index = 0;
2634 /* Write c to the dribble file. If c is a lispy event, write
2635 the event's symbol to the dribble file, in <brackets>. Bleaugh.
2636 If you, dear reader, have a better idea, you've got the source. :-) */
2637 if (dribble)
2639 if (INTEGERP (c))
2641 if (XUINT (c) < 0x100)
2642 putc (XINT (c), dribble);
2643 else
2644 fprintf (dribble, " 0x%x", (int) XUINT (c));
2646 else
2648 Lisp_Object dribblee;
2650 /* If it's a structured event, take the event header. */
2651 dribblee = EVENT_HEAD (c);
2653 if (SYMBOLP (dribblee))
2655 putc ('<', dribble);
2656 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
2657 STRING_BYTES (XSYMBOL (dribblee)->name),
2658 dribble);
2659 putc ('>', dribble);
2663 fflush (dribble);
2666 store_kbd_macro_char (c);
2668 num_nonmacro_input_events++;
2671 Lisp_Object
2672 print_help (object)
2673 Lisp_Object object;
2675 struct buffer *old = current_buffer;
2676 Fprinc (object, Qnil);
2677 set_buffer_internal (XBUFFER (Vstandard_output));
2678 call0 (intern ("help-mode"));
2679 set_buffer_internal (old);
2680 return Qnil;
2683 /* Copy out or in the info on where C-g should throw to.
2684 This is used when running Lisp code from within get_char,
2685 in case get_char is called recursively.
2686 See read_process_output. */
2688 static void
2689 save_getcjmp (temp)
2690 jmp_buf temp;
2692 bcopy (getcjmp, temp, sizeof getcjmp);
2695 static void
2696 restore_getcjmp (temp)
2697 jmp_buf temp;
2699 bcopy (temp, getcjmp, sizeof getcjmp);
2702 #ifdef HAVE_MOUSE
2704 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
2705 of this function. */
2707 static Lisp_Object
2708 tracking_off (old_value)
2709 Lisp_Object old_value;
2711 do_mouse_tracking = old_value;
2712 if (NILP (old_value))
2714 /* Redisplay may have been preempted because there was input
2715 available, and it assumes it will be called again after the
2716 input has been processed. If the only input available was
2717 the sort that we have just disabled, then we need to call
2718 redisplay. */
2719 if (!readable_events (1))
2721 redisplay_preserve_echo_area ();
2722 get_input_pending (&input_pending, 1);
2727 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
2728 "Evaluate BODY with mouse movement events enabled.\n\
2729 Within a `track-mouse' form, mouse motion generates input events that\n\
2730 you can read with `read-event'.\n\
2731 Normally, mouse motion is ignored.")
2732 (args)
2733 Lisp_Object args;
2735 int count = specpdl_ptr - specpdl;
2736 Lisp_Object val;
2738 record_unwind_protect (tracking_off, do_mouse_tracking);
2740 do_mouse_tracking = Qt;
2742 val = Fprogn (args);
2743 return unbind_to (count, val);
2746 /* If mouse has moved on some frame, return one of those frames.
2747 Return 0 otherwise. */
2749 static FRAME_PTR
2750 some_mouse_moved ()
2752 Lisp_Object tail, frame;
2754 FOR_EACH_FRAME (tail, frame)
2756 if (XFRAME (frame)->mouse_moved)
2757 return XFRAME (frame);
2760 return 0;
2763 #endif /* HAVE_MOUSE */
2765 /* Low level keyboard/mouse input.
2766 kbd_buffer_store_event places events in kbd_buffer, and
2767 kbd_buffer_get_event retrieves them. */
2769 /* Return true iff there are any events in the queue that read-char
2770 would return. If this returns false, a read-char would block. */
2771 static int
2772 readable_events (do_timers_now)
2773 int do_timers_now;
2775 if (do_timers_now)
2776 timer_check (do_timers_now);
2778 if (kbd_fetch_ptr != kbd_store_ptr)
2779 return 1;
2780 #ifdef HAVE_MOUSE
2781 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2782 return 1;
2783 #endif
2784 if (single_kboard)
2786 if (current_kboard->kbd_queue_has_data)
2787 return 1;
2789 else
2791 KBOARD *kb;
2792 for (kb = all_kboards; kb; kb = kb->next_kboard)
2793 if (kb->kbd_queue_has_data)
2794 return 1;
2796 return 0;
2799 /* Set this for debugging, to have a way to get out */
2800 int stop_character;
2802 #ifdef MULTI_KBOARD
2803 static KBOARD *
2804 event_to_kboard (event)
2805 struct input_event *event;
2807 Lisp_Object frame;
2808 frame = event->frame_or_window;
2809 if (CONSP (frame))
2810 frame = XCONS (frame)->car;
2811 else if (WINDOWP (frame))
2812 frame = WINDOW_FRAME (XWINDOW (frame));
2814 /* There are still some events that don't set this field.
2815 For now, just ignore the problem.
2816 Also ignore dead frames here. */
2817 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
2818 return 0;
2819 else
2820 return FRAME_KBOARD (XFRAME (frame));
2822 #endif
2824 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
2826 void
2827 kbd_buffer_store_event (event)
2828 register struct input_event *event;
2830 if (event->kind == no_event)
2831 abort ();
2833 if (event->kind == ascii_keystroke)
2835 register int c = event->code & 0377;
2837 if (event->modifiers & ctrl_modifier)
2838 c = make_ctrl_char (c);
2840 c |= (event->modifiers
2841 & (meta_modifier | alt_modifier
2842 | hyper_modifier | super_modifier));
2844 if (c == quit_char)
2846 extern SIGTYPE interrupt_signal ();
2847 #ifdef MULTI_KBOARD
2848 KBOARD *kb;
2849 struct input_event *sp;
2851 if (single_kboard
2852 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
2853 kb != current_kboard))
2855 kb->kbd_queue
2856 = Fcons (make_lispy_switch_frame (event->frame_or_window),
2857 Fcons (make_number (c), Qnil));
2858 kb->kbd_queue_has_data = 1;
2859 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
2861 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
2862 sp = kbd_buffer;
2864 if (event_to_kboard (sp) == kb)
2866 sp->kind = no_event;
2867 sp->frame_or_window = Qnil;
2870 return;
2872 #endif
2874 /* If this results in a quit_char being returned to Emacs as
2875 input, set Vlast_event_frame properly. If this doesn't
2876 get returned to Emacs as an event, the next event read
2877 will set Vlast_event_frame again, so this is safe to do. */
2879 Lisp_Object focus;
2881 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
2882 if (NILP (focus))
2883 focus = event->frame_or_window;
2884 internal_last_event_frame = focus;
2885 Vlast_event_frame = focus;
2888 last_event_timestamp = event->timestamp;
2889 interrupt_signal ();
2890 return;
2893 if (c && c == stop_character)
2895 sys_suspend ();
2896 return;
2899 /* Don't insert two buffer_switch_event's in a row.
2900 Just ignore the second one. */
2901 else if (event->kind == buffer_switch_event
2902 && kbd_fetch_ptr != kbd_store_ptr
2903 && kbd_store_ptr->kind == buffer_switch_event)
2904 return;
2906 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
2907 kbd_store_ptr = kbd_buffer;
2909 /* Don't let the very last slot in the buffer become full,
2910 since that would make the two pointers equal,
2911 and that is indistinguishable from an empty buffer.
2912 Discard the event if it would fill the last slot. */
2913 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
2915 struct input_event *sp = kbd_store_ptr;
2916 sp->kind = event->kind;
2917 if (event->kind == selection_request_event)
2919 /* We must not use the ordinary copying code for this case,
2920 since `part' is an enum and copying it might not copy enough
2921 in this case. */
2922 bcopy (event, (char *) sp, sizeof (*event));
2924 else
2926 sp->code = event->code;
2927 sp->part = event->part;
2928 sp->frame_or_window = event->frame_or_window;
2929 sp->modifiers = event->modifiers;
2930 sp->x = event->x;
2931 sp->y = event->y;
2932 sp->timestamp = event->timestamp;
2934 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
2935 - kbd_buffer]
2936 = event->frame_or_window);
2938 kbd_store_ptr++;
2942 /* Discard any mouse events in the event buffer by setting them to
2943 no_event. */
2944 void
2945 discard_mouse_events ()
2947 struct input_event *sp;
2948 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
2950 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
2951 sp = kbd_buffer;
2953 if (sp->kind == mouse_click
2954 #ifdef WINDOWSNT
2955 || sp->kind == w32_scroll_bar_click
2956 #endif
2957 || sp->kind == scroll_bar_click)
2959 sp->kind = no_event;
2964 /* Read one event from the event buffer, waiting if necessary.
2965 The value is a Lisp object representing the event.
2966 The value is nil for an event that should be ignored,
2967 or that was handled here.
2968 We always read and discard one event. */
2970 static Lisp_Object
2971 kbd_buffer_get_event (kbp, used_mouse_menu)
2972 KBOARD **kbp;
2973 int *used_mouse_menu;
2975 register int c;
2976 Lisp_Object obj;
2978 if (noninteractive)
2980 c = getchar ();
2981 XSETINT (obj, c);
2982 *kbp = current_kboard;
2983 return obj;
2986 /* Wait until there is input available. */
2987 for (;;)
2989 if (kbd_fetch_ptr != kbd_store_ptr)
2990 break;
2991 #ifdef HAVE_MOUSE
2992 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2993 break;
2994 #endif
2996 /* If the quit flag is set, then read_char will return
2997 quit_char, so that counts as "available input." */
2998 if (!NILP (Vquit_flag))
2999 quit_throw_to_read_char ();
3001 /* One way or another, wait until input is available; then, if
3002 interrupt handlers have not read it, read it now. */
3004 #ifdef OLDVMS
3005 wait_for_kbd_input ();
3006 #else
3007 /* Note SIGIO has been undef'd if FIONREAD is missing. */
3008 #ifdef SIGIO
3009 gobble_input (0);
3010 #endif /* SIGIO */
3011 if (kbd_fetch_ptr != kbd_store_ptr)
3012 break;
3013 #ifdef HAVE_MOUSE
3014 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3015 break;
3016 #endif
3018 Lisp_Object minus_one;
3020 XSETINT (minus_one, -1);
3021 wait_reading_process_input (0, 0, minus_one, 1);
3023 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
3024 /* Pass 1 for EXPECT since we just waited to have input. */
3025 read_avail_input (1);
3027 #endif /* not VMS */
3030 if (CONSP (Vunread_command_events))
3032 Lisp_Object first;
3033 first = XCONS (Vunread_command_events)->car;
3034 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
3035 *kbp = current_kboard;
3036 return first;
3039 /* At this point, we know that there is a readable event available
3040 somewhere. If the event queue is empty, then there must be a
3041 mouse movement enabled and available. */
3042 if (kbd_fetch_ptr != kbd_store_ptr)
3044 struct input_event *event;
3046 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3047 ? kbd_fetch_ptr
3048 : kbd_buffer);
3050 last_event_timestamp = event->timestamp;
3052 #ifdef MULTI_KBOARD
3053 *kbp = event_to_kboard (event);
3054 if (*kbp == 0)
3055 *kbp = current_kboard; /* Better than returning null ptr? */
3056 #else
3057 *kbp = &the_only_kboard;
3058 #endif
3060 obj = Qnil;
3062 /* These two kinds of events get special handling
3063 and don't actually appear to the command loop.
3064 We return nil for them. */
3065 if (event->kind == selection_request_event)
3067 #ifdef HAVE_X11
3068 struct input_event copy;
3070 /* Remove it from the buffer before processing it,
3071 since otherwise swallow_events will see it
3072 and process it again. */
3073 copy = *event;
3074 kbd_fetch_ptr = event + 1;
3075 input_pending = readable_events (0);
3076 x_handle_selection_request (&copy);
3077 #else
3078 /* We're getting selection request events, but we don't have
3079 a window system. */
3080 abort ();
3081 #endif
3084 else if (event->kind == selection_clear_event)
3086 #ifdef HAVE_X11
3087 struct input_event copy;
3089 /* Remove it from the buffer before processing it. */
3090 copy = *event;
3091 kbd_fetch_ptr = event + 1;
3092 input_pending = readable_events (0);
3093 x_handle_selection_clear (&copy);
3094 #else
3095 /* We're getting selection request events, but we don't have
3096 a window system. */
3097 abort ();
3098 #endif
3100 #if defined (HAVE_X11) || defined (HAVE_NTGUI)
3101 else if (event->kind == delete_window_event)
3103 /* Make an event (delete-frame (FRAME)). */
3104 obj = Fcons (event->frame_or_window, Qnil);
3105 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
3106 kbd_fetch_ptr = event + 1;
3108 else if (event->kind == iconify_event)
3110 /* Make an event (iconify-frame (FRAME)). */
3111 obj = Fcons (event->frame_or_window, Qnil);
3112 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
3113 kbd_fetch_ptr = event + 1;
3115 else if (event->kind == deiconify_event)
3117 /* Make an event (make-frame-visible (FRAME)). */
3118 obj = Fcons (event->frame_or_window, Qnil);
3119 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
3120 kbd_fetch_ptr = event + 1;
3122 #endif
3123 else if (event->kind == buffer_switch_event)
3125 /* The value doesn't matter here; only the type is tested. */
3126 XSETBUFFER (obj, current_buffer);
3127 kbd_fetch_ptr = event + 1;
3129 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
3130 else if (event->kind == menu_bar_activate_event)
3132 kbd_fetch_ptr = event + 1;
3133 input_pending = readable_events (0);
3134 if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
3135 x_activate_menubar (XFRAME (event->frame_or_window));
3137 #endif
3138 #ifdef WINDOWSNT
3139 else if (event->kind == language_change_event)
3141 /* Make an event (language-change (FRAME CHARSET LCID)). */
3142 obj = Fcons (event->modifiers, Qnil);
3143 obj = Fcons (event->code, Qnil);
3144 obj = Fcons (event->frame_or_window, obj);
3145 obj = Fcons (Qlanguage_change, Fcons (obj, Qnil));
3146 kbd_fetch_ptr = event + 1;
3148 #endif
3149 /* Just discard these, by returning nil.
3150 With MULTI_KBOARD, these events are used as placeholders
3151 when we need to randomly delete events from the queue.
3152 (They shouldn't otherwise be found in the buffer,
3153 but on some machines it appears they do show up
3154 even without MULTI_KBOARD.) */
3155 /* On Windows NT/9X, no_event is used to delete extraneous
3156 mouse events during a popup-menu call. */
3157 else if (event->kind == no_event)
3158 kbd_fetch_ptr = event + 1;
3159 else if (event->kind == HELP_EVENT)
3161 /* The car of event->frame_or_window is a frame,
3162 the cdr is the help to display. */
3163 obj = Fcons (Qhelp_echo, event->frame_or_window);
3164 kbd_fetch_ptr = event + 1;
3166 /* If this event is on a different frame, return a switch-frame this
3167 time, and leave the event in the queue for next time. */
3168 else
3170 Lisp_Object frame;
3171 Lisp_Object focus;
3173 frame = event->frame_or_window;
3174 if (CONSP (frame))
3175 frame = XCONS (frame)->car;
3176 else if (WINDOWP (frame))
3177 frame = WINDOW_FRAME (XWINDOW (frame));
3179 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
3180 if (! NILP (focus))
3181 frame = focus;
3183 if (! EQ (frame, internal_last_event_frame)
3184 && !EQ (frame, selected_frame))
3185 obj = make_lispy_switch_frame (frame);
3186 internal_last_event_frame = frame;
3188 /* If we didn't decide to make a switch-frame event, go ahead
3189 and build a real event from the queue entry. */
3191 if (NILP (obj))
3193 obj = make_lispy_event (event);
3194 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
3195 /* If this was a menu selection, then set the flag to inhibit
3196 writing to last_nonmenu_event. Don't do this if the event
3197 we're returning is (menu-bar), though; that indicates the
3198 beginning of the menu sequence, and we might as well leave
3199 that as the `event with parameters' for this selection. */
3200 if ((event->kind == menu_bar_event
3201 || event->kind == TOOL_BAR_EVENT)
3202 && !(CONSP (obj) && EQ (XCONS (obj)->car, Qmenu_bar))
3203 && !(CONSP (obj) && EQ (XCONS (obj)->car, Qtool_bar))
3204 && used_mouse_menu)
3205 *used_mouse_menu = 1;
3206 #endif
3208 /* Wipe out this event, to catch bugs. */
3209 event->kind = no_event;
3210 XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer] = Qnil;
3212 kbd_fetch_ptr = event + 1;
3216 #ifdef HAVE_MOUSE
3217 /* Try generating a mouse motion event. */
3218 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3220 FRAME_PTR f = some_mouse_moved ();
3221 Lisp_Object bar_window;
3222 enum scroll_bar_part part;
3223 Lisp_Object x, y;
3224 unsigned long time;
3226 *kbp = current_kboard;
3227 /* Note that this uses F to determine which display to look at.
3228 If there is no valid info, it does not store anything
3229 so x remains nil. */
3230 x = Qnil;
3231 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
3233 obj = Qnil;
3235 /* Decide if we should generate a switch-frame event. Don't
3236 generate switch-frame events for motion outside of all Emacs
3237 frames. */
3238 if (!NILP (x) && f)
3240 Lisp_Object frame;
3242 frame = FRAME_FOCUS_FRAME (f);
3243 if (NILP (frame))
3244 XSETFRAME (frame, f);
3246 if (! EQ (frame, internal_last_event_frame)
3247 && !EQ (frame, selected_frame))
3248 obj = make_lispy_switch_frame (frame);
3249 internal_last_event_frame = frame;
3252 /* If we didn't decide to make a switch-frame event, go ahead and
3253 return a mouse-motion event. */
3254 if (!NILP (x) && NILP (obj))
3255 obj = make_lispy_movement (f, bar_window, part, x, y, time);
3257 #endif /* HAVE_MOUSE */
3258 else
3259 /* We were promised by the above while loop that there was
3260 something for us to read! */
3261 abort ();
3263 input_pending = readable_events (0);
3265 Vlast_event_frame = internal_last_event_frame;
3267 return (obj);
3270 /* Process any events that are not user-visible,
3271 then return, without reading any user-visible events. */
3273 void
3274 swallow_events (do_display)
3275 int do_display;
3277 int old_timers_run;
3279 while (kbd_fetch_ptr != kbd_store_ptr)
3281 struct input_event *event;
3283 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3284 ? kbd_fetch_ptr
3285 : kbd_buffer);
3287 last_event_timestamp = event->timestamp;
3289 /* These two kinds of events get special handling
3290 and don't actually appear to the command loop. */
3291 if (event->kind == selection_request_event)
3293 #ifdef HAVE_X11
3294 struct input_event copy;
3296 /* Remove it from the buffer before processing it,
3297 since otherwise swallow_events called recursively could see it
3298 and process it again. */
3299 copy = *event;
3300 kbd_fetch_ptr = event + 1;
3301 input_pending = readable_events (0);
3302 x_handle_selection_request (&copy);
3303 #else
3304 /* We're getting selection request events, but we don't have
3305 a window system. */
3306 abort ();
3307 #endif
3310 else if (event->kind == selection_clear_event)
3312 #ifdef HAVE_X11
3313 struct input_event copy;
3315 /* Remove it from the buffer before processing it, */
3316 copy = *event;
3318 kbd_fetch_ptr = event + 1;
3319 input_pending = readable_events (0);
3320 x_handle_selection_clear (&copy);
3321 #else
3322 /* We're getting selection request events, but we don't have
3323 a window system. */
3324 abort ();
3325 #endif
3327 else
3328 break;
3331 old_timers_run = timers_run;
3332 get_input_pending (&input_pending, 1);
3334 if (timers_run != old_timers_run && do_display)
3335 redisplay_preserve_echo_area ();
3338 static EMACS_TIME timer_idleness_start_time;
3340 /* Record the start of when Emacs is idle,
3341 for the sake of running idle-time timers. */
3343 void
3344 timer_start_idle ()
3346 Lisp_Object timers;
3348 /* If we are already in the idle state, do nothing. */
3349 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3350 return;
3352 EMACS_GET_TIME (timer_idleness_start_time);
3354 /* Mark all idle-time timers as once again candidates for running. */
3355 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCONS (timers)->cdr)
3357 Lisp_Object timer;
3359 timer = XCONS (timers)->car;
3361 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3362 continue;
3363 XVECTOR (timer)->contents[0] = Qnil;
3367 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
3369 void
3370 timer_stop_idle ()
3372 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
3375 /* This is only for debugging. */
3376 struct input_event last_timer_event;
3378 /* Check whether a timer has fired. To prevent larger problems we simply
3379 disregard elements that are not proper timers. Do not make a circular
3380 timer list for the time being.
3382 Returns the number of seconds to wait until the next timer fires. If a
3383 timer is triggering now, return zero seconds.
3384 If no timer is active, return -1 seconds.
3386 If a timer is ripe, we run it, with quitting turned off.
3388 DO_IT_NOW is now ignored. It used to mean that we should
3389 run the timer directly instead of queueing a timer-event.
3390 Now we always run timers directly. */
3392 EMACS_TIME
3393 timer_check (do_it_now)
3394 int do_it_now;
3396 EMACS_TIME nexttime;
3397 EMACS_TIME now, idleness_now;
3398 Lisp_Object timers, idle_timers, chosen_timer;
3399 struct gcpro gcpro1, gcpro2, gcpro3;
3401 EMACS_SET_SECS (nexttime, -1);
3402 EMACS_SET_USECS (nexttime, -1);
3404 /* Always consider the ordinary timers. */
3405 timers = Vtimer_list;
3406 /* Consider the idle timers only if Emacs is idle. */
3407 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3408 idle_timers = Vtimer_idle_list;
3409 else
3410 idle_timers = Qnil;
3411 chosen_timer = Qnil;
3412 GCPRO3 (timers, idle_timers, chosen_timer);
3414 if (CONSP (timers) || CONSP (idle_timers))
3416 EMACS_GET_TIME (now);
3417 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3418 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
3421 while (CONSP (timers) || CONSP (idle_timers))
3423 Lisp_Object *vector;
3424 Lisp_Object timer, idle_timer;
3425 EMACS_TIME timer_time, idle_timer_time;
3426 EMACS_TIME difference, timer_difference, idle_timer_difference;
3428 /* Skip past invalid timers and timers already handled. */
3429 if (!NILP (timers))
3431 timer = XCONS (timers)->car;
3432 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3434 timers = XCONS (timers)->cdr;
3435 continue;
3437 vector = XVECTOR (timer)->contents;
3439 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3440 || !INTEGERP (vector[3])
3441 || ! NILP (vector[0]))
3443 timers = XCONS (timers)->cdr;
3444 continue;
3447 if (!NILP (idle_timers))
3449 timer = XCONS (idle_timers)->car;
3450 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3452 idle_timers = XCONS (idle_timers)->cdr;
3453 continue;
3455 vector = XVECTOR (timer)->contents;
3457 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3458 || !INTEGERP (vector[3])
3459 || ! NILP (vector[0]))
3461 idle_timers = XCONS (idle_timers)->cdr;
3462 continue;
3466 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
3467 based on the next ordinary timer.
3468 TIMER_DIFFERENCE is the distance in time from NOW to when
3469 this timer becomes ripe (negative if it's already ripe). */
3470 if (!NILP (timers))
3472 timer = XCONS (timers)->car;
3473 vector = XVECTOR (timer)->contents;
3474 EMACS_SET_SECS (timer_time,
3475 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3476 EMACS_SET_USECS (timer_time, XINT (vector[3]));
3477 EMACS_SUB_TIME (timer_difference, timer_time, now);
3480 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
3481 based on the next idle timer. */
3482 if (!NILP (idle_timers))
3484 idle_timer = XCONS (idle_timers)->car;
3485 vector = XVECTOR (idle_timer)->contents;
3486 EMACS_SET_SECS (idle_timer_time,
3487 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3488 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
3489 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
3492 /* Decide which timer is the next timer,
3493 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
3494 Also step down the list where we found that timer. */
3496 if (! NILP (timers) && ! NILP (idle_timers))
3498 EMACS_TIME temp;
3499 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
3500 if (EMACS_TIME_NEG_P (temp))
3502 chosen_timer = timer;
3503 timers = XCONS (timers)->cdr;
3504 difference = timer_difference;
3506 else
3508 chosen_timer = idle_timer;
3509 idle_timers = XCONS (idle_timers)->cdr;
3510 difference = idle_timer_difference;
3513 else if (! NILP (timers))
3515 chosen_timer = timer;
3516 timers = XCONS (timers)->cdr;
3517 difference = timer_difference;
3519 else
3521 chosen_timer = idle_timer;
3522 idle_timers = XCONS (idle_timers)->cdr;
3523 difference = idle_timer_difference;
3525 vector = XVECTOR (chosen_timer)->contents;
3527 /* If timer is rupe, run it if it hasn't been run. */
3528 if (EMACS_TIME_NEG_P (difference)
3529 || (EMACS_SECS (difference) == 0
3530 && EMACS_USECS (difference) == 0))
3532 if (NILP (vector[0]))
3534 int was_locked = single_kboard;
3535 int count = specpdl_ptr - specpdl;
3536 #ifdef HAVE_WINDOW_SYSTEM
3537 int old_inhibit_busy_cursor = inhibit_busy_cursor;
3538 #endif
3540 /* Mark the timer as triggered to prevent problems if the lisp
3541 code fails to reschedule it right. */
3542 vector[0] = Qt;
3544 specbind (Qinhibit_quit, Qt);
3546 #ifdef HAVE_WINDOW_SYSTEM
3547 inhibit_busy_cursor = 2;
3548 #endif
3550 call1 (Qtimer_event_handler, chosen_timer);
3551 timers_run++;
3553 #ifdef HAVE_WINDOW_SYSTEM
3554 inhibit_busy_cursor = old_inhibit_busy_cursor;
3555 #endif
3557 unbind_to (count, Qnil);
3559 /* Resume allowing input from any kboard, if that was true before. */
3560 if (!was_locked)
3561 any_kboard_state ();
3563 /* Since we have handled the event,
3564 we don't need to tell the caller to wake up and do it. */
3567 else
3568 /* When we encounter a timer that is still waiting,
3569 return the amount of time to wait before it is ripe. */
3571 UNGCPRO;
3572 return difference;
3576 /* No timers are pending in the future. */
3577 /* Return 0 if we generated an event, and -1 if not. */
3578 UNGCPRO;
3579 return nexttime;
3582 /* Caches for modify_event_symbol. */
3583 static Lisp_Object accent_key_syms;
3584 static Lisp_Object func_key_syms;
3585 static Lisp_Object mouse_syms;
3586 #ifdef WINDOWSNT
3587 static Lisp_Object mouse_wheel_syms;
3588 #endif
3589 static Lisp_Object drag_n_drop_syms;
3591 /* This is a list of keysym codes for special "accent" characters.
3592 It parallels lispy_accent_keys. */
3594 static int lispy_accent_codes[] =
3596 #ifdef XK_dead_circumflex
3597 XK_dead_circumflex,
3598 #else
3600 #endif
3601 #ifdef XK_dead_grave
3602 XK_dead_grave,
3603 #else
3605 #endif
3606 #ifdef XK_dead_tilde
3607 XK_dead_tilde,
3608 #else
3610 #endif
3611 #ifdef XK_dead_diaeresis
3612 XK_dead_diaeresis,
3613 #else
3615 #endif
3616 #ifdef XK_dead_macron
3617 XK_dead_macron,
3618 #else
3620 #endif
3621 #ifdef XK_dead_degree
3622 XK_dead_degree,
3623 #else
3625 #endif
3626 #ifdef XK_dead_acute
3627 XK_dead_acute,
3628 #else
3630 #endif
3631 #ifdef XK_dead_cedilla
3632 XK_dead_cedilla,
3633 #else
3635 #endif
3636 #ifdef XK_dead_breve
3637 XK_dead_breve,
3638 #else
3640 #endif
3641 #ifdef XK_dead_ogonek
3642 XK_dead_ogonek,
3643 #else
3645 #endif
3646 #ifdef XK_dead_caron
3647 XK_dead_caron,
3648 #else
3650 #endif
3651 #ifdef XK_dead_doubleacute
3652 XK_dead_doubleacute,
3653 #else
3655 #endif
3656 #ifdef XK_dead_abovedot
3657 XK_dead_abovedot,
3658 #else
3660 #endif
3663 /* This is a list of Lisp names for special "accent" characters.
3664 It parallels lispy_accent_codes. */
3666 static char *lispy_accent_keys[] =
3668 "dead-circumflex",
3669 "dead-grave",
3670 "dead-tilde",
3671 "dead-diaeresis",
3672 "dead-macron",
3673 "dead-degree",
3674 "dead-acute",
3675 "dead-cedilla",
3676 "dead-breve",
3677 "dead-ogonek",
3678 "dead-caron",
3679 "dead-doubleacute",
3680 "dead-abovedot",
3683 #ifdef HAVE_NTGUI
3684 #define FUNCTION_KEY_OFFSET 0x0
3686 char *lispy_function_keys[] =
3688 0, /* 0 */
3690 0, /* VK_LBUTTON 0x01 */
3691 0, /* VK_RBUTTON 0x02 */
3692 "cancel", /* VK_CANCEL 0x03 */
3693 0, /* VK_MBUTTON 0x04 */
3695 0, 0, 0, /* 0x05 .. 0x07 */
3697 "backspace", /* VK_BACK 0x08 */
3698 "tab", /* VK_TAB 0x09 */
3700 0, 0, /* 0x0A .. 0x0B */
3702 "clear", /* VK_CLEAR 0x0C */
3703 "return", /* VK_RETURN 0x0D */
3705 0, 0, /* 0x0E .. 0x0F */
3707 0, /* VK_SHIFT 0x10 */
3708 0, /* VK_CONTROL 0x11 */
3709 0, /* VK_MENU 0x12 */
3710 "pause", /* VK_PAUSE 0x13 */
3711 "capslock", /* VK_CAPITAL 0x14 */
3713 0, 0, 0, 0, 0, 0, /* 0x15 .. 0x1A */
3715 "escape", /* VK_ESCAPE 0x1B */
3717 0, 0, 0, 0, /* 0x1C .. 0x1F */
3719 0, /* VK_SPACE 0x20 */
3720 "prior", /* VK_PRIOR 0x21 */
3721 "next", /* VK_NEXT 0x22 */
3722 "end", /* VK_END 0x23 */
3723 "home", /* VK_HOME 0x24 */
3724 "left", /* VK_LEFT 0x25 */
3725 "up", /* VK_UP 0x26 */
3726 "right", /* VK_RIGHT 0x27 */
3727 "down", /* VK_DOWN 0x28 */
3728 "select", /* VK_SELECT 0x29 */
3729 "print", /* VK_PRINT 0x2A */
3730 "execute", /* VK_EXECUTE 0x2B */
3731 "snapshot", /* VK_SNAPSHOT 0x2C */
3732 "insert", /* VK_INSERT 0x2D */
3733 "delete", /* VK_DELETE 0x2E */
3734 "help", /* VK_HELP 0x2F */
3736 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
3738 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3740 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
3742 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
3744 0, 0, 0, 0, 0, 0, 0, 0, 0,
3745 0, 0, 0, 0, 0, 0, 0, 0, 0,
3746 0, 0, 0, 0, 0, 0, 0, 0,
3748 "lwindow", /* VK_LWIN 0x5B */
3749 "rwindow", /* VK_RWIN 0x5C */
3750 "apps", /* VK_APPS 0x5D */
3752 0, 0, /* 0x5E .. 0x5F */
3754 "kp-0", /* VK_NUMPAD0 0x60 */
3755 "kp-1", /* VK_NUMPAD1 0x61 */
3756 "kp-2", /* VK_NUMPAD2 0x62 */
3757 "kp-3", /* VK_NUMPAD3 0x63 */
3758 "kp-4", /* VK_NUMPAD4 0x64 */
3759 "kp-5", /* VK_NUMPAD5 0x65 */
3760 "kp-6", /* VK_NUMPAD6 0x66 */
3761 "kp-7", /* VK_NUMPAD7 0x67 */
3762 "kp-8", /* VK_NUMPAD8 0x68 */
3763 "kp-9", /* VK_NUMPAD9 0x69 */
3764 "kp-multiply", /* VK_MULTIPLY 0x6A */
3765 "kp-add", /* VK_ADD 0x6B */
3766 "kp-separator", /* VK_SEPARATOR 0x6C */
3767 "kp-subtract", /* VK_SUBTRACT 0x6D */
3768 "kp-decimal", /* VK_DECIMAL 0x6E */
3769 "kp-divide", /* VK_DIVIDE 0x6F */
3770 "f1", /* VK_F1 0x70 */
3771 "f2", /* VK_F2 0x71 */
3772 "f3", /* VK_F3 0x72 */
3773 "f4", /* VK_F4 0x73 */
3774 "f5", /* VK_F5 0x74 */
3775 "f6", /* VK_F6 0x75 */
3776 "f7", /* VK_F7 0x76 */
3777 "f8", /* VK_F8 0x77 */
3778 "f9", /* VK_F9 0x78 */
3779 "f10", /* VK_F10 0x79 */
3780 "f11", /* VK_F11 0x7A */
3781 "f12", /* VK_F12 0x7B */
3782 "f13", /* VK_F13 0x7C */
3783 "f14", /* VK_F14 0x7D */
3784 "f15", /* VK_F15 0x7E */
3785 "f16", /* VK_F16 0x7F */
3786 "f17", /* VK_F17 0x80 */
3787 "f18", /* VK_F18 0x81 */
3788 "f19", /* VK_F19 0x82 */
3789 "f20", /* VK_F20 0x83 */
3790 "f21", /* VK_F21 0x84 */
3791 "f22", /* VK_F22 0x85 */
3792 "f23", /* VK_F23 0x86 */
3793 "f24", /* VK_F24 0x87 */
3795 0, 0, 0, 0, /* 0x88 .. 0x8B */
3796 0, 0, 0, 0, /* 0x8C .. 0x8F */
3798 "kp-numlock", /* VK_NUMLOCK 0x90 */
3799 "scroll", /* VK_SCROLL 0x91 */
3801 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
3802 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
3803 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
3804 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
3805 "kp-end", /* VK_NUMPAD_END 0x96 */
3806 "kp-home", /* VK_NUMPAD_HOME 0x97 */
3807 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
3808 "kp-up", /* VK_NUMPAD_UP 0x99 */
3809 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
3810 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
3811 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
3812 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
3814 0, 0, /* 0x9E .. 0x9F */
3817 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
3818 * Used only as parameters to GetAsyncKeyState and GetKeyState.
3819 * No other API or message will distinguish left and right keys this way.
3821 /* 0xA0 .. 0xEF */
3823 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3824 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3825 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3826 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3827 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3829 /* 0xF0 .. 0xF5 */
3831 0, 0, 0, 0, 0, 0,
3833 "attn", /* VK_ATTN 0xF6 */
3834 "crsel", /* VK_CRSEL 0xF7 */
3835 "exsel", /* VK_EXSEL 0xF8 */
3836 "ereof", /* VK_EREOF 0xF9 */
3837 "play", /* VK_PLAY 0xFA */
3838 "zoom", /* VK_ZOOM 0xFB */
3839 "noname", /* VK_NONAME 0xFC */
3840 "pa1", /* VK_PA1 0xFD */
3841 "oem_clear", /* VK_OEM_CLEAR 0xFE */
3842 0 /* 0xFF */
3845 #else /* not HAVE_NTGUI */
3847 #ifdef XK_kana_A
3848 static char *lispy_kana_keys[] =
3850 /* X Keysym value */
3851 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x400 .. 0x40f */
3852 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x410 .. 0x41f */
3853 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x420 .. 0x42f */
3854 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x430 .. 0x43f */
3855 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x440 .. 0x44f */
3856 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x450 .. 0x45f */
3857 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x460 .. 0x46f */
3858 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0,
3859 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x480 .. 0x48f */
3860 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x490 .. 0x49f */
3861 0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket",
3862 "kana-comma", "kana-conjunctive", "kana-WO", "kana-a",
3863 "kana-i", "kana-u", "kana-e", "kana-o",
3864 "kana-ya", "kana-yu", "kana-yo", "kana-tsu",
3865 "prolongedsound", "kana-A", "kana-I", "kana-U",
3866 "kana-E", "kana-O", "kana-KA", "kana-KI",
3867 "kana-KU", "kana-KE", "kana-KO", "kana-SA",
3868 "kana-SHI", "kana-SU", "kana-SE", "kana-SO",
3869 "kana-TA", "kana-CHI", "kana-TSU", "kana-TE",
3870 "kana-TO", "kana-NA", "kana-NI", "kana-NU",
3871 "kana-NE", "kana-NO", "kana-HA", "kana-HI",
3872 "kana-FU", "kana-HE", "kana-HO", "kana-MA",
3873 "kana-MI", "kana-MU", "kana-ME", "kana-MO",
3874 "kana-YA", "kana-YU", "kana-YO", "kana-RA",
3875 "kana-RI", "kana-RU", "kana-RE", "kana-RO",
3876 "kana-WA", "kana-N", "voicedsound", "semivoicedsound",
3877 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4e0 .. 0x4ef */
3878 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f0 .. 0x4ff */
3880 #endif /* XK_kana_A */
3882 #define FUNCTION_KEY_OFFSET 0xff00
3884 /* You'll notice that this table is arranged to be conveniently
3885 indexed by X Windows keysym values. */
3886 static char *lispy_function_keys[] =
3888 /* X Keysym value */
3890 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00...0f */
3891 "backspace", "tab", "linefeed", "clear",
3892 0, "return", 0, 0,
3893 0, 0, 0, "pause", /* 0xff10...1f */
3894 0, 0, 0, 0, 0, 0, 0, "escape",
3895 0, 0, 0, 0,
3896 0, "kanji", "muhenkan", "henkan", /* 0xff20...2f */
3897 "romaji", "hiragana", "katakana", "hiragana-katakana",
3898 "zenkaku", "hankaku", "zenkaku-hankaku", "touroku",
3899 "massyo", "kana-lock", "kana-shift", "eisu-shift",
3900 "eisu-toggle", /* 0xff30...3f */
3901 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3902 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
3904 "home", "left", "up", "right", /* 0xff50 */ /* IsCursorKey */
3905 "down", "prior", "next", "end",
3906 "begin", 0, 0, 0, 0, 0, 0, 0,
3907 "select", /* 0xff60 */ /* IsMiscFunctionKey */
3908 "print",
3909 "execute",
3910 "insert",
3911 0, /* 0xff64 */
3912 "undo",
3913 "redo",
3914 "menu",
3915 "find",
3916 "cancel",
3917 "help",
3918 "break", /* 0xff6b */
3920 0, 0, 0, 0,
3921 0, 0, 0, 0, "backtab", 0, 0, 0, /* 0xff70... */
3922 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff78... */
3923 "kp-space", /* 0xff80 */ /* IsKeypadKey */
3924 0, 0, 0, 0, 0, 0, 0, 0,
3925 "kp-tab", /* 0xff89 */
3926 0, 0, 0,
3927 "kp-enter", /* 0xff8d */
3928 0, 0, 0,
3929 "kp-f1", /* 0xff91 */
3930 "kp-f2",
3931 "kp-f3",
3932 "kp-f4",
3933 "kp-home", /* 0xff95 */
3934 "kp-left",
3935 "kp-up",
3936 "kp-right",
3937 "kp-down",
3938 "kp-prior", /* kp-page-up */
3939 "kp-next", /* kp-page-down */
3940 "kp-end",
3941 "kp-begin",
3942 "kp-insert",
3943 "kp-delete",
3944 0, /* 0xffa0 */
3945 0, 0, 0, 0, 0, 0, 0, 0, 0,
3946 "kp-multiply", /* 0xffaa */
3947 "kp-add",
3948 "kp-separator",
3949 "kp-subtract",
3950 "kp-decimal",
3951 "kp-divide", /* 0xffaf */
3952 "kp-0", /* 0xffb0 */
3953 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
3954 0, /* 0xffba */
3955 0, 0,
3956 "kp-equal", /* 0xffbd */
3957 "f1", /* 0xffbe */ /* IsFunctionKey */
3958 "f2",
3959 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
3960 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
3961 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
3962 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
3963 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
3964 0, 0, 0, 0, 0, 0, 0, 0,
3965 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
3966 0, 0, 0, 0, 0, 0, 0, "delete"
3969 /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE. */
3970 #define ISO_FUNCTION_KEY_OFFSET 0xfe00
3972 static char *iso_lispy_function_keys[] =
3974 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe00 */
3975 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe08 */
3976 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe10 */
3977 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe18 */
3978 "iso-lefttab", /* 0xfe20 */
3979 "iso-move-line-up", "iso-move-line-down",
3980 "iso-partial-line-up", "iso-partial-line-down",
3981 "iso-partial-space-left", "iso-partial-space-right",
3982 "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */
3983 "iso-release-margin-left", "iso-release-margin-right",
3984 "iso-release-both-margins",
3985 "iso-fast-cursor-left", "iso-fast-cursor-right",
3986 "iso-fast-cursor-up", "iso-fast-cursor-down",
3987 "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */
3988 "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */
3991 #endif /* not HAVE_NTGUI */
3993 static char *lispy_mouse_names[] =
3995 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
3998 #ifdef WINDOWSNT
3999 /* mouse-wheel events are generated by the wheel on devices such as
4000 the MS Intellimouse. The wheel sits in between the left and right
4001 mouse buttons, and is typically used to scroll or zoom the window
4002 underneath the pointer. mouse-wheel events specify the object on
4003 which they operate, and a delta corresponding to the amount and
4004 direction that the wheel is rotated. Clicking the mouse-wheel
4005 generates a mouse-2 event. */
4006 static char *lispy_mouse_wheel_names[] =
4008 "mouse-wheel"
4011 #endif /* WINDOWSNT */
4013 /* drag-n-drop events are generated when a set of selected files are
4014 dragged from another application and dropped onto an Emacs window. */
4015 static char *lispy_drag_n_drop_names[] =
4017 "drag-n-drop"
4020 /* Scroll bar parts. */
4021 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
4022 Lisp_Object Qup, Qdown, Qbottom, Qend_scroll;
4023 Lisp_Object Qtop;
4025 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
4026 Lisp_Object *scroll_bar_parts[] = {
4027 &Qabove_handle, &Qhandle, &Qbelow_handle,
4028 &Qup, &Qdown, &Qtop, &Qbottom, &Qend_scroll
4031 /* User signal events. */
4032 Lisp_Object Qusr1_signal, Qusr2_signal;
4034 Lisp_Object *lispy_user_signals[] =
4036 &Qusr1_signal, &Qusr2_signal
4040 /* A vector, indexed by button number, giving the down-going location
4041 of currently depressed buttons, both scroll bar and non-scroll bar.
4043 The elements have the form
4044 (BUTTON-NUMBER MODIFIER-MASK . REST)
4045 where REST is the cdr of a position as it would be reported in the event.
4047 The make_lispy_event function stores positions here to tell the
4048 difference between click and drag events, and to store the starting
4049 location to be included in drag events. */
4051 static Lisp_Object button_down_location;
4053 /* Information about the most recent up-going button event: Which
4054 button, what location, and what time. */
4056 static int last_mouse_button;
4057 static int last_mouse_x;
4058 static int last_mouse_y;
4059 static unsigned long button_down_time;
4061 /* The maximum time between clicks to make a double-click,
4062 or Qnil to disable double-click detection,
4063 or Qt for no time limit. */
4064 Lisp_Object Vdouble_click_time;
4066 /* The number of clicks in this multiple-click. */
4068 int double_click_count;
4070 /* Given a struct input_event, build the lisp event which represents
4071 it. If EVENT is 0, build a mouse movement event from the mouse
4072 movement buffer, which should have a movement event in it.
4074 Note that events must be passed to this function in the order they
4075 are received; this function stores the location of button presses
4076 in order to build drag events when the button is released. */
4078 static Lisp_Object
4079 make_lispy_event (event)
4080 struct input_event *event;
4082 int i;
4084 switch (SWITCH_ENUM_CAST (event->kind))
4086 /* A simple keystroke. */
4087 case ascii_keystroke:
4089 Lisp_Object lispy_c;
4090 int c = event->code & 0377;
4091 /* Turn ASCII characters into control characters
4092 when proper. */
4093 if (event->modifiers & ctrl_modifier)
4094 c = make_ctrl_char (c);
4096 /* Add in the other modifier bits. We took care of ctrl_modifier
4097 just above, and the shift key was taken care of by the X code,
4098 and applied to control characters by make_ctrl_char. */
4099 c |= (event->modifiers
4100 & (meta_modifier | alt_modifier
4101 | hyper_modifier | super_modifier));
4102 /* Distinguish Shift-SPC from SPC. */
4103 if ((event->code & 0377) == 040
4104 && event->modifiers & shift_modifier)
4105 c |= shift_modifier;
4106 button_down_time = 0;
4107 XSETFASTINT (lispy_c, c);
4108 return lispy_c;
4111 /* A function key. The symbol may need to have modifier prefixes
4112 tacked onto it. */
4113 case non_ascii_keystroke:
4114 button_down_time = 0;
4116 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
4117 if (event->code == lispy_accent_codes[i])
4118 return modify_event_symbol (i,
4119 event->modifiers,
4120 Qfunction_key, Qnil,
4121 lispy_accent_keys, &accent_key_syms,
4122 (sizeof (lispy_accent_keys)
4123 / sizeof (lispy_accent_keys[0])));
4125 /* Handle system-specific keysyms. */
4126 if (event->code & (1 << 28))
4128 /* We need to use an alist rather than a vector as the cache
4129 since we can't make a vector long enuf. */
4130 if (NILP (current_kboard->system_key_syms))
4131 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
4132 return modify_event_symbol (event->code,
4133 event->modifiers,
4134 Qfunction_key,
4135 current_kboard->Vsystem_key_alist,
4136 0, &current_kboard->system_key_syms,
4137 (unsigned)-1);
4140 #ifdef XK_kana_A
4141 if (event->code >= 0x400 && event->code < 0x500)
4142 return modify_event_symbol (event->code - 0x400,
4143 event->modifiers & ~shift_modifier,
4144 Qfunction_key, Qnil,
4145 lispy_kana_keys, &func_key_syms,
4146 (sizeof (lispy_kana_keys)
4147 / sizeof (lispy_kana_keys[0])));
4148 #endif /* XK_kana_A */
4150 #ifdef ISO_FUNCTION_KEY_OFFSET
4151 if (event->code < FUNCTION_KEY_OFFSET
4152 && event->code >= ISO_FUNCTION_KEY_OFFSET)
4153 return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET,
4154 event->modifiers,
4155 Qfunction_key, Qnil,
4156 iso_lispy_function_keys, &func_key_syms,
4157 (sizeof (iso_lispy_function_keys)
4158 / sizeof (iso_lispy_function_keys[0])));
4159 else
4160 #endif
4161 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
4162 event->modifiers,
4163 Qfunction_key, Qnil,
4164 lispy_function_keys, &func_key_syms,
4165 (sizeof (lispy_function_keys)
4166 / sizeof (lispy_function_keys[0])));
4168 #ifdef HAVE_MOUSE
4169 /* A mouse click. Figure out where it is, decide whether it's
4170 a press, click or drag, and build the appropriate structure. */
4171 case mouse_click:
4172 #ifndef USE_TOOLKIT_SCROLL_BARS
4173 case scroll_bar_click:
4174 #endif
4176 int button = event->code;
4177 int is_double;
4178 Lisp_Object position;
4179 Lisp_Object *start_pos_ptr;
4180 Lisp_Object start_pos;
4182 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
4183 abort ();
4185 /* Build the position as appropriate for this mouse click. */
4186 if (event->kind == mouse_click)
4188 int part;
4189 FRAME_PTR f = XFRAME (event->frame_or_window);
4190 Lisp_Object window;
4191 Lisp_Object posn;
4192 Lisp_Object string_info = Qnil;
4193 int row, column;
4195 /* Ignore mouse events that were made on frame that
4196 have been deleted. */
4197 if (! FRAME_LIVE_P (f))
4198 return Qnil;
4200 /* EVENT->x and EVENT->y are frame-relative pixel
4201 coordinates at this place. Under old redisplay, COLUMN
4202 and ROW are set to frame relative glyph coordinates
4203 which are then used to determine whether this click is
4204 in a menu (non-toolkit version). */
4205 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4206 &column, &row, NULL, 1);
4208 #ifndef USE_X_TOOLKIT
4209 /* In the non-toolkit version, clicks on the menu bar
4210 are ordinary button events in the event buffer.
4211 Distinguish them, and invoke the menu.
4213 (In the toolkit version, the toolkit handles the menu bar
4214 and Emacs doesn't know about it until after the user
4215 makes a selection.) */
4216 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
4217 && (event->modifiers & down_modifier))
4219 Lisp_Object items, item;
4220 int hpos;
4221 int i;
4223 #if 0
4224 /* Activate the menu bar on the down event. If the
4225 up event comes in before the menu code can deal with it,
4226 just ignore it. */
4227 if (! (event->modifiers & down_modifier))
4228 return Qnil;
4229 #endif
4231 /* Find the menu bar item under `column'. */
4232 item = Qnil;
4233 items = FRAME_MENU_BAR_ITEMS (f);
4234 for (i = 0; i < XVECTOR (items)->size; i += 4)
4236 Lisp_Object pos, string;
4237 string = XVECTOR (items)->contents[i + 1];
4238 pos = XVECTOR (items)->contents[i + 3];
4239 if (NILP (string))
4240 break;
4241 if (column >= XINT (pos)
4242 && column < XINT (pos) + XSTRING (string)->size)
4244 item = XVECTOR (items)->contents[i];
4245 break;
4249 /* ELisp manual 2.4b says (x y) are window relative but
4250 code says they are frame-relative. */
4251 position
4252 = Fcons (event->frame_or_window,
4253 Fcons (Qmenu_bar,
4254 Fcons (Fcons (event->x, event->y),
4255 Fcons (make_number (event->timestamp),
4256 Qnil))));
4258 return Fcons (item, Fcons (position, Qnil));
4260 #endif /* not USE_X_TOOLKIT */
4262 /* Set `window' to the window under frame pixel coordinates
4263 event->x/event->y. */
4264 window = window_from_coordinates (f, XINT (event->x),
4265 XINT (event->y), &part, 0);
4267 if (!WINDOWP (window))
4269 window = event->frame_or_window;
4270 posn = Qnil;
4272 else
4274 /* It's a click in window window at frame coordinates
4275 event->x/ event->y. */
4276 struct window *w = XWINDOW (window);
4278 /* Get window relative coordinates. Original code
4279 `rounded' this to glyph boundaries. */
4280 int wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (event->x));
4281 int wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (event->y));
4283 /* Set event coordinates to window-relative coordinates
4284 for constructing the Lisp event below. */
4285 XSETINT (event->x, wx);
4286 XSETINT (event->y, wy);
4288 if (part == 1 || part == 3)
4290 /* Mode line or top line. Look for a string under
4291 the mouse that may have a `local-map' property. */
4292 Lisp_Object string;
4293 int charpos;
4295 posn = part == 1 ? Qmode_line : Qheader_line;
4296 string = mode_line_string (w, wx, wy, part == 1, &charpos);
4297 if (STRINGP (string))
4298 string_info = Fcons (string, make_number (charpos));
4300 else if (part == 2)
4301 posn = Qvertical_line;
4302 else
4303 XSETINT (posn, buffer_posn_from_coords (w, &wx, &wy));
4306 position
4307 = Fcons (window,
4308 Fcons (posn,
4309 Fcons (Fcons (event->x, event->y),
4310 Fcons (make_number (event->timestamp),
4311 (NILP (string_info)
4312 ? Qnil
4313 : Fcons (string_info, Qnil))))));
4315 #ifndef USE_TOOLKIT_SCROLL_BARS
4316 else
4318 /* It's a scrollbar click. */
4319 Lisp_Object window;
4320 Lisp_Object portion_whole;
4321 Lisp_Object part;
4323 window = event->frame_or_window;
4324 portion_whole = Fcons (event->x, event->y);
4325 part = *scroll_bar_parts[(int) event->part];
4327 position
4328 = Fcons (window,
4329 Fcons (Qvertical_scroll_bar,
4330 Fcons (portion_whole,
4331 Fcons (make_number (event->timestamp),
4332 Fcons (part, Qnil)))));
4334 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4336 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
4338 start_pos = *start_pos_ptr;
4339 *start_pos_ptr = Qnil;
4341 is_double = (button == last_mouse_button
4342 && XINT (event->x) == last_mouse_x
4343 && XINT (event->y) == last_mouse_y
4344 && button_down_time != 0
4345 && (EQ (Vdouble_click_time, Qt)
4346 || (INTEGERP (Vdouble_click_time)
4347 && ((int)(event->timestamp - button_down_time)
4348 < XINT (Vdouble_click_time)))));
4349 last_mouse_button = button;
4350 last_mouse_x = XINT (event->x);
4351 last_mouse_y = XINT (event->y);
4353 /* If this is a button press, squirrel away the location, so
4354 we can decide later whether it was a click or a drag. */
4355 if (event->modifiers & down_modifier)
4357 if (is_double)
4359 double_click_count++;
4360 event->modifiers |= ((double_click_count > 2)
4361 ? triple_modifier
4362 : double_modifier);
4364 else
4365 double_click_count = 1;
4366 button_down_time = event->timestamp;
4367 *start_pos_ptr = Fcopy_alist (position);
4370 /* Now we're releasing a button - check the co-ordinates to
4371 see if this was a click or a drag. */
4372 else if (event->modifiers & up_modifier)
4374 /* If we did not see a down before this up,
4375 ignore the up. Probably this happened because
4376 the down event chose a menu item.
4377 It would be an annoyance to treat the release
4378 of the button that chose the menu item
4379 as a separate event. */
4381 if (!CONSP (start_pos))
4382 return Qnil;
4384 event->modifiers &= ~up_modifier;
4385 #if 0 /* Formerly we treated an up with no down as a click event. */
4386 if (!CONSP (start_pos))
4387 event->modifiers |= click_modifier;
4388 else
4389 #endif
4391 /* The third element of every position should be the (x,y)
4392 pair. */
4393 Lisp_Object down;
4395 down = Fnth (make_number (2), start_pos);
4396 if (EQ (event->x, XCONS (down)->car)
4397 && EQ (event->y, XCONS (down)->cdr))
4399 event->modifiers |= click_modifier;
4401 else
4403 button_down_time = 0;
4404 event->modifiers |= drag_modifier;
4406 /* Don't check is_double; treat this as multiple
4407 if the down-event was multiple. */
4408 if (double_click_count > 1)
4409 event->modifiers |= ((double_click_count > 2)
4410 ? triple_modifier
4411 : double_modifier);
4414 else
4415 /* Every mouse event should either have the down_modifier or
4416 the up_modifier set. */
4417 abort ();
4420 /* Get the symbol we should use for the mouse click. */
4421 Lisp_Object head;
4423 head = modify_event_symbol (button,
4424 event->modifiers,
4425 Qmouse_click, Qnil,
4426 lispy_mouse_names, &mouse_syms,
4427 (sizeof (lispy_mouse_names)
4428 / sizeof (lispy_mouse_names[0])));
4429 if (event->modifiers & drag_modifier)
4430 return Fcons (head,
4431 Fcons (start_pos,
4432 Fcons (position,
4433 Qnil)));
4434 else if (event->modifiers & (double_modifier | triple_modifier))
4435 return Fcons (head,
4436 Fcons (position,
4437 Fcons (make_number (double_click_count),
4438 Qnil)));
4439 else
4440 return Fcons (head,
4441 Fcons (position,
4442 Qnil));
4446 #if USE_TOOLKIT_SCROLL_BARS
4448 /* We don't have down and up events if using toolkit scroll bars,
4449 so make this always a click event. Store in the `part' of
4450 the Lisp event a symbol which maps to the following actions:
4452 `above_handle' page up
4453 `below_handle' page down
4454 `up' line up
4455 `down' line down
4456 `top' top of buffer
4457 `bottom' bottom of buffer
4458 `handle' thumb has been dragged.
4459 `end-scroll' end of interaction with scroll bar
4461 The incoming input_event contains in its `part' member an
4462 index of type `enum scroll_bar_part' which we can use as an
4463 index in scroll_bar_parts to get the appropriate symbol. */
4465 case scroll_bar_click:
4467 Lisp_Object position, head, window, portion_whole, part;
4469 window = event->frame_or_window;
4470 portion_whole = Fcons (event->x, event->y);
4471 part = *scroll_bar_parts[(int) event->part];
4473 position
4474 = Fcons (window,
4475 Fcons (Qvertical_scroll_bar,
4476 Fcons (portion_whole,
4477 Fcons (make_number (event->timestamp),
4478 Fcons (part, Qnil)))));
4480 /* Always treat scroll bar events as clicks. */
4481 event->modifiers |= click_modifier;
4483 /* Get the symbol we should use for the mouse click. */
4484 head = modify_event_symbol (event->code,
4485 event->modifiers,
4486 Qmouse_click, Qnil,
4487 lispy_mouse_names, &mouse_syms,
4488 (sizeof (lispy_mouse_names)
4489 / sizeof (lispy_mouse_names[0])));
4490 return Fcons (head, Fcons (position, Qnil));
4493 #endif /* USE_TOOLKIT_SCROLL_BARS */
4495 #ifdef WINDOWSNT
4496 case w32_scroll_bar_click:
4498 int button = event->code;
4499 int is_double;
4500 Lisp_Object position;
4501 Lisp_Object *start_pos_ptr;
4502 Lisp_Object start_pos;
4504 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
4505 abort ();
4508 Lisp_Object window;
4509 Lisp_Object portion_whole;
4510 Lisp_Object part;
4512 window = event->frame_or_window;
4513 portion_whole = Fcons (event->x, event->y);
4514 part = *scroll_bar_parts[(int) event->part];
4516 position
4517 = Fcons (window,
4518 Fcons (Qvertical_scroll_bar,
4519 Fcons (portion_whole,
4520 Fcons (make_number (event->timestamp),
4521 Fcons (part, Qnil)))));
4524 /* Always treat W32 scroll bar events as clicks. */
4525 event->modifiers |= click_modifier;
4528 /* Get the symbol we should use for the mouse click. */
4529 Lisp_Object head;
4531 head = modify_event_symbol (button,
4532 event->modifiers,
4533 Qmouse_click, Qnil,
4534 lispy_mouse_names, &mouse_syms,
4535 (sizeof (lispy_mouse_names)
4536 / sizeof (lispy_mouse_names[0])));
4537 return Fcons (head,
4538 Fcons (position,
4539 Qnil));
4542 case mouse_wheel:
4544 int part;
4545 FRAME_PTR f = XFRAME (event->frame_or_window);
4546 Lisp_Object window;
4547 Lisp_Object posn;
4548 Lisp_Object head, position;
4549 int row, column;
4551 /* Ignore mouse events that were made on frame that
4552 have been deleted. */
4553 if (! FRAME_LIVE_P (f))
4554 return Qnil;
4555 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4556 &column, &row, NULL, 1);
4557 window = window_from_coordinates (f, column, row, &part, 0);
4559 if (!WINDOWP (window))
4561 window = event->frame_or_window;
4562 posn = Qnil;
4564 else
4566 int pixcolumn, pixrow;
4567 column -= XINT (XWINDOW (window)->left);
4568 row -= XINT (XWINDOW (window)->top);
4569 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
4570 XSETINT (event->x, pixcolumn);
4571 XSETINT (event->y, pixrow);
4573 if (part == 1)
4574 posn = Qmode_line;
4575 else if (part == 2)
4576 posn = Qvertical_line;
4577 else if (part == 3)
4578 posn = Qheader_line;
4579 else
4580 XSETINT (posn,
4581 buffer_posn_from_coords (XWINDOW (window),
4582 column, row));
4586 Lisp_Object head, position;
4588 position
4589 = Fcons (window,
4590 Fcons (posn,
4591 Fcons (Fcons (event->x, event->y),
4592 Fcons (make_number (event->timestamp),
4593 Qnil))));
4595 head = modify_event_symbol (0, event->modifiers,
4596 Qmouse_wheel, Qnil,
4597 lispy_mouse_wheel_names,
4598 &mouse_wheel_syms, 1);
4599 return Fcons (head,
4600 Fcons (position,
4601 Fcons (make_number (event->code),
4602 Qnil)));
4605 #endif /* WINDOWSNT */
4607 case drag_n_drop:
4609 int part;
4610 FRAME_PTR f;
4611 Lisp_Object window;
4612 Lisp_Object posn;
4613 Lisp_Object files;
4614 int row, column;
4616 /* The frame_or_window field should be a cons of the frame in
4617 which the event occurred and a list of the filenames
4618 dropped. */
4619 if (! CONSP (event->frame_or_window))
4620 abort ();
4622 f = XFRAME (XCONS (event->frame_or_window)->car);
4623 files = XCONS (event->frame_or_window)->cdr;
4625 /* Ignore mouse events that were made on frames that
4626 have been deleted. */
4627 if (! FRAME_LIVE_P (f))
4628 return Qnil;
4629 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4630 &column, &row, NULL, 1);
4631 window = window_from_coordinates (f, column, row, &part, 0);
4633 if (!WINDOWP (window))
4635 window = XCONS (event->frame_or_window)->car;
4636 posn = Qnil;
4638 else
4640 /* It's an event in window `window' at frame coordinates
4641 event->x/ event->y. */
4642 struct window *w = XWINDOW (window);
4644 /* Get window relative coordinates. */
4645 int wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (event->x));
4646 int wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (event->y));
4648 /* Set event coordinates to window-relative coordinates
4649 for constructing the Lisp event below. */
4650 XSETINT (event->x, wx);
4651 XSETINT (event->y, wy);
4653 if (part == 1)
4654 posn = Qmode_line;
4655 else if (part == 2)
4656 posn = Qvertical_line;
4657 else if (part == 3)
4658 posn = Qheader_line;
4659 else
4660 XSETINT (posn, buffer_posn_from_coords (w, &wx, &wy));
4664 Lisp_Object head, position;
4666 position
4667 = Fcons (window,
4668 Fcons (posn,
4669 Fcons (Fcons (event->x, event->y),
4670 Fcons (make_number (event->timestamp),
4671 Qnil))));
4673 head = modify_event_symbol (0, event->modifiers,
4674 Qdrag_n_drop, Qnil,
4675 lispy_drag_n_drop_names,
4676 &drag_n_drop_syms, 1);
4677 return Fcons (head,
4678 Fcons (position,
4679 Fcons (files,
4680 Qnil)));
4683 #endif /* HAVE_MOUSE */
4685 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
4686 case menu_bar_event:
4687 /* The event value is in the cdr of the frame_or_window slot. */
4688 if (!CONSP (event->frame_or_window))
4689 abort ();
4690 return XCONS (event->frame_or_window)->cdr;
4691 #endif
4693 case TOOL_BAR_EVENT:
4695 Lisp_Object key;
4696 if (!CONSP (event->frame_or_window))
4697 abort ();
4698 key = XCDR (event->frame_or_window);
4699 if (SYMBOLP (key))
4700 key = apply_modifiers (event->modifiers, key);
4701 return key;
4704 case user_signal:
4705 /* A user signal. */
4706 return *lispy_user_signals[event->code];
4708 /* The 'kind' field of the event is something we don't recognize. */
4709 default:
4710 abort ();
4714 #ifdef HAVE_MOUSE
4716 static Lisp_Object
4717 make_lispy_movement (frame, bar_window, part, x, y, time)
4718 FRAME_PTR frame;
4719 Lisp_Object bar_window;
4720 enum scroll_bar_part part;
4721 Lisp_Object x, y;
4722 unsigned long time;
4724 /* Is it a scroll bar movement? */
4725 if (frame && ! NILP (bar_window))
4727 Lisp_Object part_sym;
4729 part_sym = *scroll_bar_parts[(int) part];
4730 return Fcons (Qscroll_bar_movement,
4731 (Fcons (Fcons (bar_window,
4732 Fcons (Qvertical_scroll_bar,
4733 Fcons (Fcons (x, y),
4734 Fcons (make_number (time),
4735 Fcons (part_sym,
4736 Qnil))))),
4737 Qnil)));
4740 /* Or is it an ordinary mouse movement? */
4741 else
4743 int area;
4744 Lisp_Object window;
4745 Lisp_Object posn;
4747 if (frame)
4748 /* It's in a frame; which window on that frame? */
4749 window = window_from_coordinates (frame, XINT (x), XINT (y), &area, 0);
4750 else
4751 window = Qnil;
4753 if (WINDOWP (window))
4755 struct window *w = XWINDOW (window);
4756 int wx, wy;
4758 /* Get window relative coordinates. */
4759 wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (x));
4760 wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (y));
4761 XSETINT (x, wx);
4762 XSETINT (y, wy);
4764 if (area == 1)
4765 posn = Qmode_line;
4766 else if (area == 2)
4767 posn = Qvertical_line;
4768 else if (area == 3)
4769 posn = Qheader_line;
4770 else
4771 XSETINT (posn, buffer_posn_from_coords (w, &wx, &wy));
4773 else if (frame != 0)
4775 XSETFRAME (window, frame);
4776 posn = Qnil;
4778 else
4780 window = Qnil;
4781 posn = Qnil;
4782 XSETFASTINT (x, 0);
4783 XSETFASTINT (y, 0);
4786 return Fcons (Qmouse_movement,
4787 Fcons (Fcons (window,
4788 Fcons (posn,
4789 Fcons (Fcons (x, y),
4790 Fcons (make_number (time),
4791 Qnil)))),
4792 Qnil));
4796 #endif /* HAVE_MOUSE */
4798 /* Construct a switch frame event. */
4799 static Lisp_Object
4800 make_lispy_switch_frame (frame)
4801 Lisp_Object frame;
4803 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
4806 /* Manipulating modifiers. */
4808 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
4810 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
4811 SYMBOL's name of the end of the modifiers; the string from this
4812 position is the unmodified symbol name.
4814 This doesn't use any caches. */
4816 static int
4817 parse_modifiers_uncached (symbol, modifier_end)
4818 Lisp_Object symbol;
4819 int *modifier_end;
4821 struct Lisp_String *name;
4822 int i;
4823 int modifiers;
4825 CHECK_SYMBOL (symbol, 1);
4827 modifiers = 0;
4828 name = XSYMBOL (symbol)->name;
4830 for (i = 0; i+2 <= STRING_BYTES (name); )
4832 int this_mod_end = 0;
4833 int this_mod = 0;
4835 /* See if the name continues with a modifier word.
4836 Check that the word appears, but don't check what follows it.
4837 Set this_mod and this_mod_end to record what we find. */
4839 switch (name->data[i])
4841 #define SINGLE_LETTER_MOD(BIT) \
4842 (this_mod_end = i + 1, this_mod = BIT)
4844 case 'A':
4845 SINGLE_LETTER_MOD (alt_modifier);
4846 break;
4848 case 'C':
4849 SINGLE_LETTER_MOD (ctrl_modifier);
4850 break;
4852 case 'H':
4853 SINGLE_LETTER_MOD (hyper_modifier);
4854 break;
4856 case 'M':
4857 SINGLE_LETTER_MOD (meta_modifier);
4858 break;
4860 case 'S':
4861 SINGLE_LETTER_MOD (shift_modifier);
4862 break;
4864 case 's':
4865 SINGLE_LETTER_MOD (super_modifier);
4866 break;
4868 #undef SINGLE_LETTER_MOD
4871 /* If we found no modifier, stop looking for them. */
4872 if (this_mod_end == 0)
4873 break;
4875 /* Check there is a dash after the modifier, so that it
4876 really is a modifier. */
4877 if (this_mod_end >= STRING_BYTES (name)
4878 || name->data[this_mod_end] != '-')
4879 break;
4881 /* This modifier is real; look for another. */
4882 modifiers |= this_mod;
4883 i = this_mod_end + 1;
4886 /* Should we include the `click' modifier? */
4887 if (! (modifiers & (down_modifier | drag_modifier
4888 | double_modifier | triple_modifier))
4889 && i + 7 == STRING_BYTES (name)
4890 && strncmp (name->data + i, "mouse-", 6) == 0
4891 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
4892 modifiers |= click_modifier;
4894 if (modifier_end)
4895 *modifier_end = i;
4897 return modifiers;
4900 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
4901 prepended to the string BASE[0..BASE_LEN-1].
4902 This doesn't use any caches. */
4903 static Lisp_Object
4904 apply_modifiers_uncached (modifiers, base, base_len, base_len_byte)
4905 int modifiers;
4906 char *base;
4907 int base_len, base_len_byte;
4909 /* Since BASE could contain nulls, we can't use intern here; we have
4910 to use Fintern, which expects a genuine Lisp_String, and keeps a
4911 reference to it. */
4912 char *new_mods
4913 = (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
4914 int mod_len;
4917 char *p = new_mods;
4919 /* Only the event queue may use the `up' modifier; it should always
4920 be turned into a click or drag event before presented to lisp code. */
4921 if (modifiers & up_modifier)
4922 abort ();
4924 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
4925 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
4926 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
4927 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
4928 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
4929 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
4930 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
4931 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
4932 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
4933 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
4934 /* The click modifier is denoted by the absence of other modifiers. */
4936 *p = '\0';
4938 mod_len = p - new_mods;
4942 Lisp_Object new_name;
4944 new_name = make_uninit_multibyte_string (mod_len + base_len,
4945 mod_len + base_len_byte);
4946 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
4947 bcopy (base, XSTRING (new_name)->data + mod_len, base_len_byte);
4949 return Fintern (new_name, Qnil);
4954 static char *modifier_names[] =
4956 "up", "down", "drag", "click", "double", "triple", 0, 0,
4957 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4958 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
4960 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
4962 static Lisp_Object modifier_symbols;
4964 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
4965 static Lisp_Object
4966 lispy_modifier_list (modifiers)
4967 int modifiers;
4969 Lisp_Object modifier_list;
4970 int i;
4972 modifier_list = Qnil;
4973 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
4974 if (modifiers & (1<<i))
4975 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
4976 modifier_list);
4978 return modifier_list;
4982 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
4983 where UNMODIFIED is the unmodified form of SYMBOL,
4984 MASK is the set of modifiers present in SYMBOL's name.
4985 This is similar to parse_modifiers_uncached, but uses the cache in
4986 SYMBOL's Qevent_symbol_element_mask property, and maintains the
4987 Qevent_symbol_elements property. */
4989 Lisp_Object
4990 parse_modifiers (symbol)
4991 Lisp_Object symbol;
4993 Lisp_Object elements;
4995 elements = Fget (symbol, Qevent_symbol_element_mask);
4996 if (CONSP (elements))
4997 return elements;
4998 else
5000 int end;
5001 int modifiers = parse_modifiers_uncached (symbol, &end);
5002 Lisp_Object unmodified;
5003 Lisp_Object mask;
5005 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
5006 STRING_BYTES (XSYMBOL (symbol)->name) - end),
5007 Qnil);
5009 if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1))
5010 abort ();
5011 XSETFASTINT (mask, modifiers);
5012 elements = Fcons (unmodified, Fcons (mask, Qnil));
5014 /* Cache the parsing results on SYMBOL. */
5015 Fput (symbol, Qevent_symbol_element_mask,
5016 elements);
5017 Fput (symbol, Qevent_symbol_elements,
5018 Fcons (unmodified, lispy_modifier_list (modifiers)));
5020 /* Since we know that SYMBOL is modifiers applied to unmodified,
5021 it would be nice to put that in unmodified's cache.
5022 But we can't, since we're not sure that parse_modifiers is
5023 canonical. */
5025 return elements;
5029 /* Apply the modifiers MODIFIERS to the symbol BASE.
5030 BASE must be unmodified.
5032 This is like apply_modifiers_uncached, but uses BASE's
5033 Qmodifier_cache property, if present. It also builds
5034 Qevent_symbol_elements properties, since it has that info anyway.
5036 apply_modifiers copies the value of BASE's Qevent_kind property to
5037 the modified symbol. */
5038 static Lisp_Object
5039 apply_modifiers (modifiers, base)
5040 int modifiers;
5041 Lisp_Object base;
5043 Lisp_Object cache, index, entry, new_symbol;
5045 /* Mask out upper bits. We don't know where this value's been. */
5046 modifiers &= ((EMACS_INT)1 << VALBITS) - 1;
5048 /* The click modifier never figures into cache indices. */
5049 cache = Fget (base, Qmodifier_cache);
5050 XSETFASTINT (index, (modifiers & ~click_modifier));
5051 entry = assq_no_quit (index, cache);
5053 if (CONSP (entry))
5054 new_symbol = XCONS (entry)->cdr;
5055 else
5057 /* We have to create the symbol ourselves. */
5058 new_symbol = apply_modifiers_uncached (modifiers,
5059 XSYMBOL (base)->name->data,
5060 XSYMBOL (base)->name->size,
5061 STRING_BYTES (XSYMBOL (base)->name));
5063 /* Add the new symbol to the base's cache. */
5064 entry = Fcons (index, new_symbol);
5065 Fput (base, Qmodifier_cache, Fcons (entry, cache));
5067 /* We have the parsing info now for free, so add it to the caches. */
5068 XSETFASTINT (index, modifiers);
5069 Fput (new_symbol, Qevent_symbol_element_mask,
5070 Fcons (base, Fcons (index, Qnil)));
5071 Fput (new_symbol, Qevent_symbol_elements,
5072 Fcons (base, lispy_modifier_list (modifiers)));
5075 /* Make sure this symbol is of the same kind as BASE.
5077 You'd think we could just set this once and for all when we
5078 intern the symbol above, but reorder_modifiers may call us when
5079 BASE's property isn't set right; we can't assume that just
5080 because it has a Qmodifier_cache property it must have its
5081 Qevent_kind set right as well. */
5082 if (NILP (Fget (new_symbol, Qevent_kind)))
5084 Lisp_Object kind;
5086 kind = Fget (base, Qevent_kind);
5087 if (! NILP (kind))
5088 Fput (new_symbol, Qevent_kind, kind);
5091 return new_symbol;
5095 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
5096 return a symbol with the modifiers placed in the canonical order.
5097 Canonical order is alphabetical, except for down and drag, which
5098 always come last. The 'click' modifier is never written out.
5100 Fdefine_key calls this to make sure that (for example) C-M-foo
5101 and M-C-foo end up being equivalent in the keymap. */
5103 Lisp_Object
5104 reorder_modifiers (symbol)
5105 Lisp_Object symbol;
5107 /* It's hopefully okay to write the code this way, since everything
5108 will soon be in caches, and no consing will be done at all. */
5109 Lisp_Object parsed;
5111 parsed = parse_modifiers (symbol);
5112 return apply_modifiers ((int) XINT (XCONS (XCONS (parsed)->cdr)->car),
5113 XCONS (parsed)->car);
5117 /* For handling events, we often want to produce a symbol whose name
5118 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
5119 to some base, like the name of a function key or mouse button.
5120 modify_event_symbol produces symbols of this sort.
5122 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
5123 is the name of the i'th symbol. TABLE_SIZE is the number of elements
5124 in the table.
5126 Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
5127 NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
5129 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
5130 persist between calls to modify_event_symbol that it can use to
5131 store a cache of the symbols it's generated for this NAME_TABLE
5132 before. The object stored there may be a vector or an alist.
5134 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
5136 MODIFIERS is a set of modifier bits (as given in struct input_events)
5137 whose prefixes should be applied to the symbol name.
5139 SYMBOL_KIND is the value to be placed in the event_kind property of
5140 the returned symbol.
5142 The symbols we create are supposed to have an
5143 `event-symbol-elements' property, which lists the modifiers present
5144 in the symbol's name. */
5146 static Lisp_Object
5147 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
5148 name_table, symbol_table, table_size)
5149 int symbol_num;
5150 unsigned modifiers;
5151 Lisp_Object symbol_kind;
5152 Lisp_Object name_alist;
5153 char **name_table;
5154 Lisp_Object *symbol_table;
5155 unsigned int table_size;
5157 Lisp_Object value;
5158 Lisp_Object symbol_int;
5160 /* Get rid of the "vendor-specific" bit here. */
5161 XSETINT (symbol_int, symbol_num & 0xffffff);
5163 /* Is this a request for a valid symbol? */
5164 if (symbol_num < 0 || symbol_num >= table_size)
5165 return Qnil;
5167 if (CONSP (*symbol_table))
5168 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
5170 /* If *symbol_table doesn't seem to be initialized properly, fix that.
5171 *symbol_table should be a lisp vector TABLE_SIZE elements long,
5172 where the Nth element is the symbol for NAME_TABLE[N], or nil if
5173 we've never used that symbol before. */
5174 else
5176 if (! VECTORP (*symbol_table)
5177 || XVECTOR (*symbol_table)->size != table_size)
5179 Lisp_Object size;
5181 XSETFASTINT (size, table_size);
5182 *symbol_table = Fmake_vector (size, Qnil);
5185 value = XVECTOR (*symbol_table)->contents[symbol_num];
5188 /* Have we already used this symbol before? */
5189 if (NILP (value))
5191 /* No; let's create it. */
5192 if (!NILP (name_alist))
5193 value = Fcdr_safe (Fassq (symbol_int, name_alist));
5194 else if (name_table != 0 && name_table[symbol_num])
5195 value = intern (name_table[symbol_num]);
5197 #ifdef HAVE_WINDOW_SYSTEM
5198 if (NILP (value))
5200 char *name = x_get_keysym_name (symbol_num);
5201 if (name)
5202 value = intern (name);
5204 #endif
5206 if (NILP (value))
5208 char buf[20];
5209 sprintf (buf, "key-%d", symbol_num);
5210 value = intern (buf);
5213 if (CONSP (*symbol_table))
5214 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
5215 else
5216 XVECTOR (*symbol_table)->contents[symbol_num] = value;
5218 /* Fill in the cache entries for this symbol; this also
5219 builds the Qevent_symbol_elements property, which the user
5220 cares about. */
5221 apply_modifiers (modifiers & click_modifier, value);
5222 Fput (value, Qevent_kind, symbol_kind);
5225 /* Apply modifiers to that symbol. */
5226 return apply_modifiers (modifiers, value);
5229 /* Convert a list that represents an event type,
5230 such as (ctrl meta backspace), into the usual representation of that
5231 event type as a number or a symbol. */
5233 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
5234 "Convert the event description list EVENT-DESC to an event type.\n\
5235 EVENT-DESC should contain one base event type (a character or symbol)\n\
5236 and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\
5237 drag, down, double or triple). The base must be last.\n\
5238 The return value is an event type (a character or symbol) which\n\
5239 has the same base event type and all the specified modifiers.")
5240 (event_desc)
5241 Lisp_Object event_desc;
5243 Lisp_Object base;
5244 int modifiers = 0;
5245 Lisp_Object rest;
5247 base = Qnil;
5248 rest = event_desc;
5249 while (CONSP (rest))
5251 Lisp_Object elt;
5252 int this = 0;
5254 elt = XCONS (rest)->car;
5255 rest = XCONS (rest)->cdr;
5257 /* Given a symbol, see if it is a modifier name. */
5258 if (SYMBOLP (elt) && CONSP (rest))
5259 this = parse_solitary_modifier (elt);
5261 if (this != 0)
5262 modifiers |= this;
5263 else if (!NILP (base))
5264 error ("Two bases given in one event");
5265 else
5266 base = elt;
5270 /* Let the symbol A refer to the character A. */
5271 if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1)
5272 XSETINT (base, XSYMBOL (base)->name->data[0]);
5274 if (INTEGERP (base))
5276 /* Turn (shift a) into A. */
5277 if ((modifiers & shift_modifier) != 0
5278 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
5280 XSETINT (base, XINT (base) - ('a' - 'A'));
5281 modifiers &= ~shift_modifier;
5284 /* Turn (control a) into C-a. */
5285 if (modifiers & ctrl_modifier)
5286 return make_number ((modifiers & ~ctrl_modifier)
5287 | make_ctrl_char (XINT (base)));
5288 else
5289 return make_number (modifiers | XINT (base));
5291 else if (SYMBOLP (base))
5292 return apply_modifiers (modifiers, base);
5293 else
5294 error ("Invalid base event");
5297 /* Try to recognize SYMBOL as a modifier name.
5298 Return the modifier flag bit, or 0 if not recognized. */
5300 static int
5301 parse_solitary_modifier (symbol)
5302 Lisp_Object symbol;
5304 struct Lisp_String *name = XSYMBOL (symbol)->name;
5306 switch (name->data[0])
5308 #define SINGLE_LETTER_MOD(BIT) \
5309 if (STRING_BYTES (name) == 1) \
5310 return BIT;
5312 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
5313 if (LEN == STRING_BYTES (name) \
5314 && ! strncmp (name->data, NAME, LEN)) \
5315 return BIT;
5317 case 'A':
5318 SINGLE_LETTER_MOD (alt_modifier);
5319 break;
5321 case 'a':
5322 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
5323 break;
5325 case 'C':
5326 SINGLE_LETTER_MOD (ctrl_modifier);
5327 break;
5329 case 'c':
5330 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
5331 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
5332 break;
5334 case 'H':
5335 SINGLE_LETTER_MOD (hyper_modifier);
5336 break;
5338 case 'h':
5339 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
5340 break;
5342 case 'M':
5343 SINGLE_LETTER_MOD (meta_modifier);
5344 break;
5346 case 'm':
5347 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
5348 break;
5350 case 'S':
5351 SINGLE_LETTER_MOD (shift_modifier);
5352 break;
5354 case 's':
5355 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
5356 MULTI_LETTER_MOD (super_modifier, "super", 5);
5357 SINGLE_LETTER_MOD (super_modifier);
5358 break;
5360 case 'd':
5361 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
5362 MULTI_LETTER_MOD (down_modifier, "down", 4);
5363 MULTI_LETTER_MOD (double_modifier, "double", 6);
5364 break;
5366 case 't':
5367 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
5368 break;
5370 #undef SINGLE_LETTER_MOD
5371 #undef MULTI_LETTER_MOD
5374 return 0;
5377 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
5378 Such a list is not valid as an event,
5379 but it can be a Lucid-style event type list. */
5382 lucid_event_type_list_p (object)
5383 Lisp_Object object;
5385 Lisp_Object tail;
5387 if (! CONSP (object))
5388 return 0;
5390 for (tail = object; CONSP (tail); tail = XCONS (tail)->cdr)
5392 Lisp_Object elt;
5393 elt = XCONS (tail)->car;
5394 if (! (INTEGERP (elt) || SYMBOLP (elt)))
5395 return 0;
5398 return NILP (tail);
5401 /* Store into *addr a value nonzero if terminal input chars are available.
5402 Serves the purpose of ioctl (0, FIONREAD, addr)
5403 but works even if FIONREAD does not exist.
5404 (In fact, this may actually read some input.)
5406 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe. */
5408 static void
5409 get_input_pending (addr, do_timers_now)
5410 int *addr;
5411 int do_timers_now;
5413 /* First of all, have we already counted some input? */
5414 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
5416 /* If input is being read as it arrives, and we have none, there is none. */
5417 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
5418 return;
5420 /* Try to read some input and see how much we get. */
5421 gobble_input (0);
5422 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
5425 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
5427 void
5428 gobble_input (expected)
5429 int expected;
5431 #ifndef VMS
5432 #ifdef SIGIO
5433 if (interrupt_input)
5435 SIGMASKTYPE mask;
5436 mask = sigblock (sigmask (SIGIO));
5437 read_avail_input (expected);
5438 sigsetmask (mask);
5440 else
5441 #ifdef POLL_FOR_INPUT
5442 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
5444 SIGMASKTYPE mask;
5445 mask = sigblock (sigmask (SIGALRM));
5446 read_avail_input (expected);
5447 sigsetmask (mask);
5449 else
5450 #endif
5451 #endif
5452 read_avail_input (expected);
5453 #endif
5456 /* Put a buffer_switch_event in the buffer
5457 so that read_key_sequence will notice the new current buffer. */
5459 void
5460 record_asynch_buffer_change ()
5462 struct input_event event;
5463 Lisp_Object tem;
5465 event.kind = buffer_switch_event;
5466 event.frame_or_window = Qnil;
5468 #ifdef subprocesses
5469 /* We don't need a buffer-switch event unless Emacs is waiting for input.
5470 The purpose of the event is to make read_key_sequence look up the
5471 keymaps again. If we aren't in read_key_sequence, we don't need one,
5472 and the event could cause trouble by messing up (input-pending-p). */
5473 tem = Fwaiting_for_user_input_p ();
5474 if (NILP (tem))
5475 return;
5476 #else
5477 /* We never need these events if we have no asynchronous subprocesses. */
5478 return;
5479 #endif
5481 /* Make sure no interrupt happens while storing the event. */
5482 #ifdef SIGIO
5483 if (interrupt_input)
5485 SIGMASKTYPE mask;
5486 mask = sigblock (sigmask (SIGIO));
5487 kbd_buffer_store_event (&event);
5488 sigsetmask (mask);
5490 else
5491 #endif
5493 stop_polling ();
5494 kbd_buffer_store_event (&event);
5495 start_polling ();
5499 #ifndef VMS
5501 /* Read any terminal input already buffered up by the system
5502 into the kbd_buffer, but do not wait.
5504 EXPECTED should be nonzero if the caller knows there is some input.
5506 Except on VMS, all input is read by this function.
5507 If interrupt_input is nonzero, this function MUST be called
5508 only when SIGIO is blocked.
5510 Returns the number of keyboard chars read, or -1 meaning
5511 this is a bad time to try to read input. */
5513 static int
5514 read_avail_input (expected)
5515 int expected;
5517 struct input_event buf[KBD_BUFFER_SIZE];
5518 register int i;
5519 int nread;
5521 if (read_socket_hook)
5522 /* No need for FIONREAD or fcntl; just say don't wait. */
5523 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE, expected);
5524 else
5526 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
5527 the kbd_buffer can really hold. That may prevent loss
5528 of characters on some systems when input is stuffed at us. */
5529 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
5530 int n_to_read;
5532 /* Determine how many characters we should *try* to read. */
5533 #ifdef WINDOWSNT
5534 return 0;
5535 #else /* not WINDOWSNT */
5536 #ifdef MSDOS
5537 n_to_read = dos_keysns ();
5538 if (n_to_read == 0)
5539 return 0;
5540 #else /* not MSDOS */
5541 #ifdef FIONREAD
5542 /* Find out how much input is available. */
5543 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
5544 /* Formerly simply reported no input, but that sometimes led to
5545 a failure of Emacs to terminate.
5546 SIGHUP seems appropriate if we can't reach the terminal. */
5547 /* ??? Is it really right to send the signal just to this process
5548 rather than to the whole process group?
5549 Perhaps on systems with FIONREAD Emacs is alone in its group. */
5550 kill (getpid (), SIGHUP);
5551 if (n_to_read == 0)
5552 return 0;
5553 if (n_to_read > sizeof cbuf)
5554 n_to_read = sizeof cbuf;
5555 #else /* no FIONREAD */
5556 #if defined (USG) || defined (DGUX)
5557 /* Read some input if available, but don't wait. */
5558 n_to_read = sizeof cbuf;
5559 fcntl (input_fd, F_SETFL, O_NDELAY);
5560 #else
5561 you lose;
5562 #endif
5563 #endif
5564 #endif /* not MSDOS */
5565 #endif /* not WINDOWSNT */
5567 /* Now read; for one reason or another, this will not block.
5568 NREAD is set to the number of chars read. */
5571 #ifdef MSDOS
5572 cbuf[0] = dos_keyread ();
5573 nread = 1;
5574 #else
5575 nread = read (input_fd, cbuf, n_to_read);
5576 #endif
5577 /* POSIX infers that processes which are not in the session leader's
5578 process group won't get SIGHUP's at logout time. BSDI adheres to
5579 this part standard and returns -1 from read (0) with errno==EIO
5580 when the control tty is taken away.
5581 Jeffrey Honig <jch@bsdi.com> says this is generally safe. */
5582 if (nread == -1 && errno == EIO)
5583 kill (0, SIGHUP);
5584 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
5585 /* The kernel sometimes fails to deliver SIGHUP for ptys.
5586 This looks incorrect, but it isn't, because _BSD causes
5587 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
5588 and that causes a value other than 0 when there is no input. */
5589 if (nread == 0)
5590 kill (0, SIGHUP);
5591 #endif
5593 while (
5594 /* We used to retry the read if it was interrupted.
5595 But this does the wrong thing when O_NDELAY causes
5596 an EAGAIN error. Does anybody know of a situation
5597 where a retry is actually needed? */
5598 #if 0
5599 nread < 0 && (errno == EAGAIN
5600 #ifdef EFAULT
5601 || errno == EFAULT
5602 #endif
5603 #ifdef EBADSLT
5604 || errno == EBADSLT
5605 #endif
5607 #else
5609 #endif
5612 #ifndef FIONREAD
5613 #if defined (USG) || defined (DGUX)
5614 fcntl (input_fd, F_SETFL, 0);
5615 #endif /* USG or DGUX */
5616 #endif /* no FIONREAD */
5617 for (i = 0; i < nread; i++)
5619 buf[i].kind = ascii_keystroke;
5620 buf[i].modifiers = 0;
5621 if (meta_key == 1 && (cbuf[i] & 0x80))
5622 buf[i].modifiers = meta_modifier;
5623 if (meta_key != 2)
5624 cbuf[i] &= ~0x80;
5626 buf[i].code = cbuf[i];
5627 buf[i].frame_or_window = selected_frame;
5631 /* Scan the chars for C-g and store them in kbd_buffer. */
5632 for (i = 0; i < nread; i++)
5634 kbd_buffer_store_event (&buf[i]);
5635 /* Don't look at input that follows a C-g too closely.
5636 This reduces lossage due to autorepeat on C-g. */
5637 if (buf[i].kind == ascii_keystroke
5638 && buf[i].code == quit_char)
5639 break;
5642 return nread;
5644 #endif /* not VMS */
5646 #ifdef SIGIO /* for entire page */
5647 /* Note SIGIO has been undef'd if FIONREAD is missing. */
5649 SIGTYPE
5650 input_available_signal (signo)
5651 int signo;
5653 /* Must preserve main program's value of errno. */
5654 int old_errno = errno;
5655 #ifdef BSD4_1
5656 extern int select_alarmed;
5657 #endif
5659 #if defined (USG) && !defined (POSIX_SIGNALS)
5660 /* USG systems forget handlers when they are used;
5661 must reestablish each time */
5662 signal (signo, input_available_signal);
5663 #endif /* USG */
5665 #ifdef BSD4_1
5666 sigisheld (SIGIO);
5667 #endif
5669 if (input_available_clear_time)
5670 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
5672 while (1)
5674 int nread;
5675 nread = read_avail_input (1);
5676 /* -1 means it's not ok to read the input now.
5677 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
5678 0 means there was no keyboard input available. */
5679 if (nread <= 0)
5680 break;
5682 #ifdef BSD4_1
5683 select_alarmed = 1; /* Force the select emulator back to life */
5684 #endif
5687 #ifdef BSD4_1
5688 sigfree ();
5689 #endif
5690 errno = old_errno;
5692 #endif /* SIGIO */
5694 /* Send ourselves a SIGIO.
5696 This function exists so that the UNBLOCK_INPUT macro in
5697 blockinput.h can have some way to take care of input we put off
5698 dealing with, without assuming that every file which uses
5699 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
5700 void
5701 reinvoke_input_signal ()
5703 #ifdef SIGIO
5704 kill (getpid (), SIGIO);
5705 #endif
5710 /* Return the prompt-string of a sparse keymap.
5711 This is the first element which is a string.
5712 Return nil if there is none. */
5714 Lisp_Object
5715 map_prompt (map)
5716 Lisp_Object map;
5718 while (CONSP (map))
5720 register Lisp_Object tem;
5721 tem = Fcar (map);
5722 if (STRINGP (tem))
5723 return tem;
5724 map = Fcdr (map);
5726 return Qnil;
5729 static void menu_bar_item ();
5730 static void menu_bar_one_keymap ();
5732 /* These variables hold the vector under construction within
5733 menu_bar_items and its subroutines, and the current index
5734 for storing into that vector. */
5735 static Lisp_Object menu_bar_items_vector;
5736 static int menu_bar_items_index;
5738 /* Return a vector of menu items for a menu bar, appropriate
5739 to the current buffer. Each item has three elements in the vector:
5740 KEY STRING MAPLIST.
5742 OLD is an old vector we can optionally reuse, or nil. */
5744 Lisp_Object
5745 menu_bar_items (old)
5746 Lisp_Object old;
5748 /* The number of keymaps we're scanning right now, and the number of
5749 keymaps we have allocated space for. */
5750 int nmaps;
5752 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
5753 in the current keymaps, or nil where it is not a prefix. */
5754 Lisp_Object *maps;
5756 Lisp_Object def, tem, tail;
5758 Lisp_Object result;
5760 int mapno;
5761 Lisp_Object oquit;
5763 int i;
5765 struct gcpro gcpro1;
5767 /* In order to build the menus, we need to call the keymap
5768 accessors. They all call QUIT. But this function is called
5769 during redisplay, during which a quit is fatal. So inhibit
5770 quitting while building the menus.
5771 We do this instead of specbind because (1) errors will clear it anyway
5772 and (2) this avoids risk of specpdl overflow. */
5773 oquit = Vinhibit_quit;
5774 Vinhibit_quit = Qt;
5776 if (!NILP (old))
5777 menu_bar_items_vector = old;
5778 else
5779 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
5780 menu_bar_items_index = 0;
5782 GCPRO1 (menu_bar_items_vector);
5784 /* Build our list of keymaps.
5785 If we recognize a function key and replace its escape sequence in
5786 keybuf with its symbol, or if the sequence starts with a mouse
5787 click and we need to switch buffers, we jump back here to rebuild
5788 the initial keymaps from the current buffer. */
5790 Lisp_Object *tmaps;
5792 /* Should overriding-terminal-local-map and overriding-local-map apply? */
5793 if (!NILP (Voverriding_local_map_menu_flag))
5795 /* Yes, use them (if non-nil) as well as the global map. */
5796 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
5797 nmaps = 0;
5798 if (!NILP (current_kboard->Voverriding_terminal_local_map))
5799 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
5800 if (!NILP (Voverriding_local_map))
5801 maps[nmaps++] = Voverriding_local_map;
5803 else
5805 /* No, so use major and minor mode keymaps. */
5806 nmaps = current_minor_maps (NULL, &tmaps);
5807 maps = (Lisp_Object *) alloca ((nmaps + 2) * sizeof (maps[0]));
5808 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
5809 #ifdef USE_TEXT_PROPERTIES
5810 maps[nmaps++] = get_local_map (PT, current_buffer);
5811 #else
5812 maps[nmaps++] = current_buffer->keymap;
5813 #endif
5815 maps[nmaps++] = current_global_map;
5818 /* Look up in each map the dummy prefix key `menu-bar'. */
5820 result = Qnil;
5822 for (mapno = nmaps - 1; mapno >= 0; mapno--)
5824 if (! NILP (maps[mapno]))
5825 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0), 0);
5826 else
5827 def = Qnil;
5829 tem = Fkeymapp (def);
5830 if (!NILP (tem))
5831 menu_bar_one_keymap (def);
5834 /* Move to the end those items that should be at the end. */
5836 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCONS (tail)->cdr)
5838 int i;
5839 int end = menu_bar_items_index;
5841 for (i = 0; i < end; i += 4)
5842 if (EQ (XCONS (tail)->car, XVECTOR (menu_bar_items_vector)->contents[i]))
5844 Lisp_Object tem0, tem1, tem2, tem3;
5845 /* Move the item at index I to the end,
5846 shifting all the others forward. */
5847 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
5848 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
5849 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
5850 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
5851 if (end > i + 4)
5852 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
5853 &XVECTOR (menu_bar_items_vector)->contents[i],
5854 (end - i - 4) * sizeof (Lisp_Object));
5855 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
5856 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
5857 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
5858 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
5859 break;
5863 /* Add nil, nil, nil, nil at the end. */
5864 i = menu_bar_items_index;
5865 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
5867 Lisp_Object tem;
5868 tem = Fmake_vector (make_number (2 * i), Qnil);
5869 bcopy (XVECTOR (menu_bar_items_vector)->contents,
5870 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
5871 menu_bar_items_vector = tem;
5873 /* Add this item. */
5874 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5875 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5876 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5877 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5878 menu_bar_items_index = i;
5880 Vinhibit_quit = oquit;
5881 UNGCPRO;
5882 return menu_bar_items_vector;
5885 /* Scan one map KEYMAP, accumulating any menu items it defines
5886 in menu_bar_items_vector. */
5888 static Lisp_Object menu_bar_one_keymap_changed_items;
5890 static void
5891 menu_bar_one_keymap (keymap)
5892 Lisp_Object keymap;
5894 Lisp_Object tail, item;
5896 menu_bar_one_keymap_changed_items = Qnil;
5898 /* Loop over all keymap entries that have menu strings. */
5899 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
5901 item = XCONS (tail)->car;
5902 if (CONSP (item))
5903 menu_bar_item (XCONS (item)->car, XCONS (item)->cdr);
5904 else if (VECTORP (item))
5906 /* Loop over the char values represented in the vector. */
5907 int len = XVECTOR (item)->size;
5908 int c;
5909 for (c = 0; c < len; c++)
5911 Lisp_Object character;
5912 XSETFASTINT (character, c);
5913 menu_bar_item (character, XVECTOR (item)->contents[c]);
5919 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
5920 If there's already an item for KEY, add this DEF to it. */
5922 Lisp_Object item_properties;
5924 static void
5925 menu_bar_item (key, item)
5926 Lisp_Object key, item;
5928 struct gcpro gcpro1;
5929 int i;
5930 Lisp_Object tem;
5932 if (EQ (item, Qundefined))
5934 /* If a map has an explicit `undefined' as definition,
5935 discard any previously made menu bar item. */
5937 for (i = 0; i < menu_bar_items_index; i += 4)
5938 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
5940 if (menu_bar_items_index > i + 4)
5941 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
5942 &XVECTOR (menu_bar_items_vector)->contents[i],
5943 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
5944 menu_bar_items_index -= 4;
5945 return;
5948 /* If there's no definition for this key yet,
5949 just ignore `undefined'. */
5950 return;
5953 GCPRO1 (key); /* Is this necessary? */
5954 i = parse_menu_item (item, 0, 1);
5955 UNGCPRO;
5956 if (!i)
5957 return;
5959 /* If this keymap has already contributed to this KEY,
5960 don't contribute to it a second time. */
5961 tem = Fmemq (key, menu_bar_one_keymap_changed_items);
5962 if (!NILP (tem))
5963 return;
5965 menu_bar_one_keymap_changed_items
5966 = Fcons (key, menu_bar_one_keymap_changed_items);
5968 item = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
5970 /* Find any existing item for this KEY. */
5971 for (i = 0; i < menu_bar_items_index; i += 4)
5972 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
5973 break;
5975 /* If we did not find this KEY, add it at the end. */
5976 if (i == menu_bar_items_index)
5978 /* If vector is too small, get a bigger one. */
5979 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
5981 Lisp_Object tem;
5982 tem = Fmake_vector (make_number (2 * i), Qnil);
5983 bcopy (XVECTOR (menu_bar_items_vector)->contents,
5984 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
5985 menu_bar_items_vector = tem;
5988 /* Add this item. */
5989 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
5990 XVECTOR (menu_bar_items_vector)->contents[i++]
5991 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
5992 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (item, Qnil);
5993 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
5994 menu_bar_items_index = i;
5996 /* We did find an item for this KEY. Add ITEM to its list of maps. */
5997 else
5999 Lisp_Object old;
6000 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
6001 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (item, old);
6005 /* This is used as the handler when calling menu_item_eval_property. */
6006 static Lisp_Object
6007 menu_item_eval_property_1 (arg)
6008 Lisp_Object arg;
6010 /* If we got a quit from within the menu computation,
6011 quit all the way out of it. This takes care of C-] in the debugger. */
6012 if (CONSP (arg) && EQ (XCONS (arg)->car, Qquit))
6013 Fsignal (Qquit, Qnil);
6015 return Qnil;
6018 /* Evaluate an expression and return the result (or nil if something
6019 went wrong). Used to evaluate dynamic parts of menu items. */
6020 Lisp_Object
6021 menu_item_eval_property (sexpr)
6022 Lisp_Object sexpr;
6024 int count = specpdl_ptr - specpdl;
6025 Lisp_Object val;
6026 specbind (Qinhibit_redisplay, Qt);
6027 val = internal_condition_case_1 (Feval, sexpr, Qerror,
6028 menu_item_eval_property_1);
6029 return unbind_to (count, val);
6032 /* This function parses a menu item and leaves the result in the
6033 vector item_properties.
6034 ITEM is a key binding, a possible menu item.
6035 If NOTREAL is nonzero, only check for equivalent key bindings, don't
6036 evaluate dynamic expressions in the menu item.
6037 INMENUBAR is > 0 when this is considered for an entry in a menu bar
6038 top level.
6039 INMENUBAR is < 0 when this is considered for an entry in a keyboard menu.
6040 parse_menu_item returns true if the item is a menu item and false
6041 otherwise. */
6044 parse_menu_item (item, notreal, inmenubar)
6045 Lisp_Object item;
6046 int notreal, inmenubar;
6048 Lisp_Object def, tem, item_string, start;
6049 Lisp_Object cachelist;
6050 Lisp_Object filter;
6051 Lisp_Object keyhint;
6052 int i;
6053 int newcache = 0;
6055 cachelist = Qnil;
6056 filter = Qnil;
6057 keyhint = Qnil;
6059 if (!CONSP (item))
6060 return 0;
6062 /* Create item_properties vector if necessary. */
6063 if (NILP (item_properties))
6064 item_properties
6065 = Fmake_vector (make_number (ITEM_PROPERTY_ENABLE + 1), Qnil);
6067 /* Initialize optional entries. */
6068 for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++)
6069 XVECTOR (item_properties)->contents[i] = Qnil;
6070 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = Qt;
6072 /* Save the item here to protect it from GC. */
6073 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ITEM] = item;
6075 item_string = XCONS (item)->car;
6077 start = item;
6078 item = XCONS (item)->cdr;
6079 if (STRINGP (item_string))
6081 /* Old format menu item. */
6082 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME] = item_string;
6084 /* Maybe help string. */
6085 if (CONSP (item) && STRINGP (XCONS (item)->car))
6087 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]
6088 = XCONS (item)->car;
6089 start = item;
6090 item = XCONS (item)->cdr;
6093 /* Maybee key binding cache. */
6094 if (CONSP (item) && CONSP (XCONS (item)->car)
6095 && (NILP (XCONS (XCONS (item)->car)->car)
6096 || VECTORP (XCONS (XCONS (item)->car)->car)))
6098 cachelist = XCONS (item)->car;
6099 item = XCONS (item)->cdr;
6102 /* This is the real definition--the function to run. */
6103 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = item;
6105 /* Get enable property, if any. */
6106 if (SYMBOLP (item))
6108 tem = Fget (item, Qmenu_enable);
6109 if (!NILP (tem))
6110 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = tem;
6113 else if (EQ (item_string, Qmenu_item) && CONSP (item))
6115 /* New format menu item. */
6116 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME]
6117 = XCONS (item)->car;
6118 start = XCONS (item)->cdr;
6119 if (CONSP (start))
6121 /* We have a real binding. */
6122 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF]
6123 = XCONS (start)->car;
6125 item = XCONS (start)->cdr;
6126 /* Is there a cache list with key equivalences. */
6127 if (CONSP (item) && CONSP (XCONS (item)->car))
6129 cachelist = XCONS (item)->car;
6130 item = XCONS (item)->cdr;
6133 /* Parse properties. */
6134 while (CONSP (item) && CONSP (XCONS (item)->cdr))
6136 tem = XCONS (item)->car;
6137 item = XCONS (item)->cdr;
6139 if (EQ (tem, QCenable))
6140 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE]
6141 = XCONS (item)->car;
6142 else if (EQ (tem, QCvisible) && !notreal)
6144 /* If got a visible property and that evaluates to nil
6145 then ignore this item. */
6146 tem = menu_item_eval_property (XCONS (item)->car);
6147 if (NILP (tem))
6148 return 0;
6150 else if (EQ (tem, QChelp))
6151 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]
6152 = XCONS (item)->car;
6153 else if (EQ (tem, QCfilter))
6154 filter = item;
6155 else if (EQ (tem, QCkey_sequence))
6157 tem = XCONS (item)->car;
6158 if (NILP (cachelist)
6159 && (SYMBOLP (tem) || STRINGP (tem) || VECTORP (tem)))
6160 /* Be GC protected. Set keyhint to item instead of tem. */
6161 keyhint = item;
6163 else if (EQ (tem, QCkeys))
6165 tem = XCONS (item)->car;
6166 if (CONSP (tem) || (STRINGP (tem) && NILP (cachelist)))
6167 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ]
6168 = tem;
6170 else if (EQ (tem, QCbutton) && CONSP (XCONS (item)->car))
6172 Lisp_Object type;
6173 tem = XCONS (item)->car;
6174 type = XCONS (tem)->car;
6175 if (EQ (type, QCtoggle) || EQ (type, QCradio))
6177 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]
6178 = XCONS (tem)->cdr;
6179 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE]
6180 = type;
6183 item = XCONS (item)->cdr;
6186 else if (inmenubar || !NILP (start))
6187 return 0;
6189 else
6190 return 0; /* not a menu item */
6192 /* If item string is not a string, evaluate it to get string.
6193 If we don't get a string, skip this item. */
6194 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
6195 if (!(STRINGP (item_string) || notreal))
6197 item_string = menu_item_eval_property (item_string);
6198 if (!STRINGP (item_string))
6199 return 0;
6200 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME] = item_string;
6203 /* If got a filter apply it on definition. */
6204 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6205 if (!NILP (filter))
6207 def = menu_item_eval_property (list2 (XCONS (filter)->car,
6208 list2 (Qquote, def)));
6210 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = def;
6213 /* If we got no definition, this item is just unselectable text which
6214 is OK in a submenu but not in the menubar. */
6215 if (NILP (def))
6216 return (inmenubar ? 0 : 1);
6218 /* Enable or disable selection of item. */
6219 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
6220 if (!EQ (tem, Qt))
6222 if (notreal)
6223 tem = Qt;
6224 else
6225 tem = menu_item_eval_property (tem);
6226 if (inmenubar && NILP (tem))
6227 return 0; /* Ignore disabled items in menu bar. */
6228 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = tem;
6231 /* See if this is a separate pane or a submenu. */
6232 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6233 tem = get_keymap_1 (def, 0, 1);
6234 /* For a subkeymap, just record its details and exit. */
6235 if (!NILP (tem))
6237 XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP] = tem;
6238 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = tem;
6239 return 1;
6241 /* At the top level in the menu bar, do likewise for commands also.
6242 The menu bar does not display equivalent key bindings anyway.
6243 ITEM_PROPERTY_DEF is already set up properly. */
6244 if (inmenubar > 0)
6245 return 1;
6247 /* This is a command. See if there is an equivalent key binding. */
6248 if (NILP (cachelist))
6250 /* We have to create a cachelist. */
6251 CHECK_IMPURE (start);
6252 XCONS (start)->cdr = Fcons (Fcons (Qnil, Qnil), XCONS (start)->cdr);
6253 cachelist = XCONS (XCONS (start)->cdr)->car;
6254 newcache = 1;
6255 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
6256 if (!NILP (keyhint))
6258 XCONS (cachelist)->car = XCONS (keyhint)->car;
6259 newcache = 0;
6261 else if (STRINGP (tem))
6263 XCONS (cachelist)->cdr = Fsubstitute_command_keys (tem);
6264 XCONS (cachelist)->car = Qt;
6267 tem = XCONS (cachelist)->car;
6268 if (!EQ (tem, Qt))
6270 int chkcache = 0;
6271 Lisp_Object prefix;
6273 if (!NILP (tem))
6274 tem = Fkey_binding (tem, Qnil);
6276 prefix = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
6277 if (CONSP (prefix))
6279 def = XCONS (prefix)->car;
6280 prefix = XCONS (prefix)->cdr;
6282 else
6283 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6285 if (NILP (XCONS (cachelist)->car)) /* Have no saved key. */
6287 if (newcache /* Always check first time. */
6288 /* Should we check everything when precomputing key
6289 bindings? */
6290 /* || notreal */
6291 /* If something had no key binding before, don't recheck it
6292 because that is too slow--except if we have a list of
6293 rebound commands in Vdefine_key_rebound_commands, do
6294 recheck any command that appears in that list. */
6295 || (CONSP (Vdefine_key_rebound_commands)
6296 && !NILP (Fmemq (def, Vdefine_key_rebound_commands))))
6297 chkcache = 1;
6299 /* We had a saved key. Is it still bound to the command? */
6300 else if (NILP (tem)
6301 || (!EQ (tem, def)
6302 /* If the command is an alias for another
6303 (such as lmenu.el set it up), check if the
6304 original command matches the cached command. */
6305 && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function))))
6306 chkcache = 1; /* Need to recompute key binding. */
6308 if (chkcache)
6310 /* Recompute equivalent key binding. If the command is an alias
6311 for another (such as lmenu.el set it up), see if the original
6312 command name has equivalent keys. Otherwise look up the
6313 specified command itself. We don't try both, because that
6314 makes lmenu menus slow. */
6315 if (SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
6316 && ! NILP (Fget (def, Qmenu_alias)))
6317 def = XSYMBOL (def)->function;
6318 tem = Fwhere_is_internal (def, Qnil, Qt, Qnil);
6319 XCONS (cachelist)->car = tem;
6320 if (NILP (tem))
6322 XCONS (cachelist)->cdr = Qnil;
6323 chkcache = 0;
6326 else if (!NILP (keyhint) && !NILP (XCONS (cachelist)->car))
6328 tem = XCONS (cachelist)->car;
6329 chkcache = 1;
6332 newcache = chkcache;
6333 if (chkcache)
6335 tem = Fkey_description (tem);
6336 if (CONSP (prefix))
6338 if (STRINGP (XCONS (prefix)->car))
6339 tem = concat2 (XCONS (prefix)->car, tem);
6340 if (STRINGP (XCONS (prefix)->cdr))
6341 tem = concat2 (tem, XCONS (prefix)->cdr);
6343 XCONS (cachelist)->cdr = tem;
6347 tem = XCONS (cachelist)->cdr;
6348 if (newcache && !NILP (tem))
6350 tem = concat3 (build_string (" ("), tem, build_string (")"));
6351 XCONS (cachelist)->cdr = tem;
6354 /* If we only want to precompute equivalent key bindings, stop here. */
6355 if (notreal)
6356 return 1;
6358 /* If we have an equivalent key binding, use that. */
6359 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ] = tem;
6361 /* Include this when menu help is implemented.
6362 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP];
6363 if (!(NILP (tem) || STRINGP (tem)))
6365 tem = menu_item_eval_property (tem);
6366 if (!STRINGP (tem))
6367 tem = Qnil;
6368 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP] = tem;
6372 /* Handle radio buttons or toggle boxes. */
6373 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
6374 if (!NILP (tem))
6375 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]
6376 = menu_item_eval_property (tem);
6378 return 1;
6383 /***********************************************************************
6384 Tool-bars
6385 ***********************************************************************/
6387 /* A vector holding tool bar items while they are parsed in function
6388 tool_bar_items runs Each item occupies TOOL_BAR_ITEM_NSCLOTS elements
6389 in the vector. */
6391 static Lisp_Object tool_bar_items_vector;
6393 /* A vector holding the result of parse_tool_bar_item. Layout is like
6394 the one for a single item in tool_bar_items_vector. */
6396 static Lisp_Object tool_bar_item_properties;
6398 /* Next free index in tool_bar_items_vector. */
6400 static int ntool_bar_items;
6402 /* The symbols `tool-bar', and `:image'. */
6404 extern Lisp_Object Qtool_bar;
6405 Lisp_Object QCimage;
6407 /* Function prototypes. */
6409 static void init_tool_bar_items P_ ((Lisp_Object));
6410 static void process_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
6411 static int parse_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
6412 static void append_tool_bar_item P_ ((void));
6415 /* Return a vector of tool bar items for keymaps currently in effect.
6416 Reuse vector REUSE if non-nil. Return in *NITEMS the number of
6417 tool bar items found. */
6419 Lisp_Object
6420 tool_bar_items (reuse, nitems)
6421 Lisp_Object reuse;
6422 int *nitems;
6424 Lisp_Object *maps;
6425 int nmaps, i;
6426 Lisp_Object oquit;
6427 Lisp_Object *tmaps;
6428 extern Lisp_Object Voverriding_local_map_menu_flag;
6429 extern Lisp_Object Voverriding_local_map;
6431 *nitems = 0;
6433 /* In order to build the menus, we need to call the keymap
6434 accessors. They all call QUIT. But this function is called
6435 during redisplay, during which a quit is fatal. So inhibit
6436 quitting while building the menus. We do this instead of
6437 specbind because (1) errors will clear it anyway and (2) this
6438 avoids risk of specpdl overflow. */
6439 oquit = Vinhibit_quit;
6440 Vinhibit_quit = Qt;
6442 /* Initialize tool_bar_items_vector and protect it from GC. */
6443 init_tool_bar_items (reuse);
6445 /* Build list of keymaps in maps. Set nmaps to the number of maps
6446 to process. */
6448 /* Should overriding-terminal-local-map and overriding-local-map apply? */
6449 if (!NILP (Voverriding_local_map_menu_flag))
6451 /* Yes, use them (if non-nil) as well as the global map. */
6452 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
6453 nmaps = 0;
6454 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6455 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6456 if (!NILP (Voverriding_local_map))
6457 maps[nmaps++] = Voverriding_local_map;
6459 else
6461 /* No, so use major and minor mode keymaps. */
6462 nmaps = current_minor_maps (NULL, &tmaps);
6463 maps = (Lisp_Object *) alloca ((nmaps + 2) * sizeof (maps[0]));
6464 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
6465 #ifdef USE_TEXT_PROPERTIES
6466 maps[nmaps++] = get_local_map (PT, current_buffer);
6467 #else
6468 maps[nmaps++] = current_buffer->keymap;
6469 #endif
6472 /* Add global keymap at the end. */
6473 maps[nmaps++] = current_global_map;
6475 /* Process maps in reverse order and look up in each map the prefix
6476 key `tool-bar'. */
6477 for (i = nmaps - 1; i >= 0; --i)
6478 if (!NILP (maps[i]))
6480 Lisp_Object keymap;
6482 keymap = get_keyelt (access_keymap (maps[i], Qtool_bar, 1, 1), 0);
6483 if (!NILP (Fkeymapp (keymap)))
6485 Lisp_Object tail;
6487 /* KEYMAP is a list `(keymap (KEY . BINDING) ...)'. */
6488 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
6490 Lisp_Object keydef = XCAR (tail);
6491 if (CONSP (keydef))
6492 process_tool_bar_item (XCAR (keydef), XCDR (keydef));
6497 Vinhibit_quit = oquit;
6498 *nitems = ntool_bar_items / TOOL_BAR_ITEM_NSLOTS;
6499 return tool_bar_items_vector;
6503 /* Process the definition of KEY which is DEF. */
6505 static void
6506 process_tool_bar_item (key, def)
6507 Lisp_Object key, def;
6509 int i;
6510 extern Lisp_Object Qundefined;
6511 struct gcpro gcpro1, gcpro2;
6513 /* Protect KEY and DEF from GC because parse_tool_bar_item may call
6514 eval. */
6515 GCPRO2 (key, def);
6517 if (EQ (def, Qundefined))
6519 /* If a map has an explicit `undefined' as definition,
6520 discard any previously made item. */
6521 for (i = 0; i < ntool_bar_items; i += TOOL_BAR_ITEM_NSLOTS)
6523 Lisp_Object *v = XVECTOR (tool_bar_items_vector)->contents + i;
6525 if (EQ (key, v[TOOL_BAR_ITEM_KEY]))
6527 if (ntool_bar_items > i + TOOL_BAR_ITEM_NSLOTS)
6528 bcopy (v + TOOL_BAR_ITEM_NSLOTS, v,
6529 ((ntool_bar_items - i - TOOL_BAR_ITEM_NSLOTS)
6530 * sizeof (Lisp_Object)));
6531 ntool_bar_items -= TOOL_BAR_ITEM_NSLOTS;
6532 break;
6536 else if (parse_tool_bar_item (key, def))
6537 /* Append a new tool bar item to tool_bar_items_vector. Accept
6538 more than one definition for the same key. */
6539 append_tool_bar_item ();
6541 UNGCPRO;
6545 /* Parse a tool bar item specification ITEM for key KEY and return the
6546 result in tool_bar_item_properties. Value is zero if ITEM is
6547 invalid.
6549 ITEM is a list `(menu-item CAPTION BINDING PROPS...)'.
6551 CAPTION is the caption of the item, If it's not a string, it is
6552 evaluated to get a string.
6554 BINDING is the tool bar item's binding. Tool-bar items with keymaps
6555 as binding are currently ignored.
6557 The following properties are recognized:
6559 - `:enable FORM'.
6561 FORM is evaluated and specifies whether the tool bar item is
6562 enabled or disabled.
6564 - `:visible FORM'
6566 FORM is evaluated and specifies whether the tool bar item is visible.
6568 - `:filter FUNCTION'
6570 FUNCTION is invoked with one parameter `(quote BINDING)'. Its
6571 result is stored as the new binding.
6573 - `:button (TYPE SELECTED)'
6575 TYPE must be one of `:radio' or `:toggle'. SELECTED is evaluated
6576 and specifies whether the button is selected (pressed) or not.
6578 - `:image IMAGES'
6580 IMAGES is either a single image specification or a vector of four
6581 image specifications. See enum tool_bar_item_images.
6583 - `:help HELP-STRING'.
6585 Gives a help string to display for the tool bar item. */
6587 static int
6588 parse_tool_bar_item (key, item)
6589 Lisp_Object key, item;
6591 /* Access slot with index IDX of vector tool_bar_item_properties. */
6592 #define PROP(IDX) XVECTOR (tool_bar_item_properties)->contents[IDX]
6594 Lisp_Object filter = Qnil;
6595 Lisp_Object caption;
6596 extern Lisp_Object QCenable, QCvisible, QChelp, QCfilter;
6597 extern Lisp_Object QCbutton, QCtoggle, QCradio;
6598 int i;
6600 /* Defininition looks like `(tool-bar-item CAPTION BINDING
6601 PROPS...)'. Rule out items that aren't lists, don't start with
6602 `tool-bar-item' or whose rest following `tool-bar-item' is not a
6603 list. */
6604 if (!CONSP (item)
6605 || !EQ (XCAR (item), Qmenu_item)
6606 || (item = XCDR (item),
6607 !CONSP (item)))
6608 return 0;
6610 /* Create tool_bar_item_properties vector if necessary. Reset it to
6611 defaults. */
6612 if (VECTORP (tool_bar_item_properties))
6614 for (i = 0; i < TOOL_BAR_ITEM_NSLOTS; ++i)
6615 PROP (i) = Qnil;
6617 else
6618 tool_bar_item_properties
6619 = Fmake_vector (make_number (TOOL_BAR_ITEM_NSLOTS), Qnil);
6621 /* Set defaults. */
6622 PROP (TOOL_BAR_ITEM_KEY) = key;
6623 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
6625 /* Get the caption of the item. If the caption is not a string,
6626 evaluate it to get a string. If we don't get a string, skip this
6627 item. */
6628 caption = XCAR (item);
6629 if (!STRINGP (caption))
6631 caption = menu_item_eval_property (caption);
6632 if (!STRINGP (caption))
6633 return 0;
6635 PROP (TOOL_BAR_ITEM_CAPTION) = caption;
6637 /* Give up if rest following the caption is not a list. */
6638 item = XCDR (item);
6639 if (!CONSP (item))
6640 return 0;
6642 /* Store the binding. */
6643 PROP (TOOL_BAR_ITEM_BINDING) = XCAR (item);
6644 item = XCDR (item);
6646 /* Process the rest of the properties. */
6647 for (; CONSP (item) && CONSP (XCDR (item)); item = XCDR (XCDR (item)))
6649 Lisp_Object key, value;
6651 key = XCAR (item);
6652 value = XCAR (XCDR (item));
6654 if (EQ (key, QCenable))
6655 /* `:enable FORM'. */
6656 PROP (TOOL_BAR_ITEM_ENABLED_P) = value;
6657 else if (EQ (key, QCvisible))
6659 /* `:visible FORM'. If got a visible property and that
6660 evaluates to nil then ignore this item. */
6661 if (NILP (menu_item_eval_property (value)))
6662 return 0;
6664 else if (EQ (key, QChelp))
6665 /* `:help HELP-STRING'. */
6666 PROP (TOOL_BAR_ITEM_HELP) = value;
6667 else if (EQ (key, QCfilter))
6668 /* ':filter FORM'. */
6669 filter = value;
6670 else if (EQ (key, QCbutton) && CONSP (value))
6672 /* `:button (TYPE . SELECTED)'. */
6673 Lisp_Object type, selected;
6675 type = XCAR (value);
6676 selected = XCDR (value);
6677 if (EQ (type, QCtoggle) || EQ (type, QCradio))
6679 PROP (TOOL_BAR_ITEM_SELECTED_P) = selected;
6680 PROP (TOOL_BAR_ITEM_TYPE) = type;
6683 else if (EQ (key, QCimage)
6684 && (CONSP (value)
6685 || (VECTORP (value) && XVECTOR (value)->size == 4)))
6686 /* Value is either a single image specification or a vector
6687 of 4 such specifications for the different buttion states. */
6688 PROP (TOOL_BAR_ITEM_IMAGES) = value;
6691 /* If got a filter apply it on binding. */
6692 if (!NILP (filter))
6693 PROP (TOOL_BAR_ITEM_BINDING)
6694 = menu_item_eval_property (list2 (filter,
6695 list2 (Qquote,
6696 PROP (TOOL_BAR_ITEM_BINDING))));
6698 /* See if the binding is a keymap. Give up if it is. */
6699 if (!NILP (get_keymap_1 (PROP (TOOL_BAR_ITEM_BINDING), 0, 1)))
6700 return 0;
6702 /* Enable or disable selection of item. */
6703 if (!EQ (PROP (TOOL_BAR_ITEM_ENABLED_P), Qt))
6704 PROP (TOOL_BAR_ITEM_ENABLED_P)
6705 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_ENABLED_P));
6707 /* Handle radio buttons or toggle boxes. */
6708 if (!NILP (PROP (TOOL_BAR_ITEM_SELECTED_P)))
6709 PROP (TOOL_BAR_ITEM_SELECTED_P)
6710 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_SELECTED_P));
6712 return 1;
6714 #undef PROP
6718 /* Initialize tool_bar_items_vector. REUSE, if non-nil, is a vector
6719 that can be reused. */
6721 static void
6722 init_tool_bar_items (reuse)
6723 Lisp_Object reuse;
6725 if (VECTORP (reuse))
6726 tool_bar_items_vector = reuse;
6727 else
6728 tool_bar_items_vector = Fmake_vector (make_number (64), Qnil);
6729 ntool_bar_items = 0;
6733 /* Append parsed tool bar item properties from
6734 tool_bar_item_properties */
6736 static void
6737 append_tool_bar_item ()
6739 Lisp_Object *to, *from;
6741 /* Enlarge tool_bar_items_vector if necessary. */
6742 if (ntool_bar_items + TOOL_BAR_ITEM_NSLOTS
6743 >= XVECTOR (tool_bar_items_vector)->size)
6745 Lisp_Object new_vector;
6746 int old_size = XVECTOR (tool_bar_items_vector)->size;
6748 new_vector = Fmake_vector (make_number (2 * old_size), Qnil);
6749 bcopy (XVECTOR (tool_bar_items_vector)->contents,
6750 XVECTOR (new_vector)->contents,
6751 old_size * sizeof (Lisp_Object));
6752 tool_bar_items_vector = new_vector;
6755 /* Append entries from tool_bar_item_properties to the end of
6756 tool_bar_items_vector. */
6757 to = XVECTOR (tool_bar_items_vector)->contents + ntool_bar_items;
6758 from = XVECTOR (tool_bar_item_properties)->contents;
6759 bcopy (from, to, TOOL_BAR_ITEM_NSLOTS * sizeof *to);
6760 ntool_bar_items += TOOL_BAR_ITEM_NSLOTS;
6767 /* Read a character using menus based on maps in the array MAPS.
6768 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
6769 Return t if we displayed a menu but the user rejected it.
6771 PREV_EVENT is the previous input event, or nil if we are reading
6772 the first event of a key sequence.
6774 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
6775 if we used a mouse menu to read the input, or zero otherwise. If
6776 USED_MOUSE_MENU is null, we don't dereference it.
6778 The prompting is done based on the prompt-string of the map
6779 and the strings associated with various map elements.
6781 This can be done with X menus or with menus put in the minibuf.
6782 These are done in different ways, depending on how the input will be read.
6783 Menus using X are done after auto-saving in read-char, getting the input
6784 event from Fx_popup_menu; menus using the minibuf use read_char recursively
6785 and do auto-saving in the inner call of read_char. */
6787 static Lisp_Object
6788 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
6789 int nmaps;
6790 Lisp_Object *maps;
6791 Lisp_Object prev_event;
6792 int *used_mouse_menu;
6794 int mapno;
6795 register Lisp_Object name;
6797 if (used_mouse_menu)
6798 *used_mouse_menu = 0;
6800 /* Use local over global Menu maps */
6802 if (! menu_prompting)
6803 return Qnil;
6805 /* Optionally disregard all but the global map. */
6806 if (inhibit_local_menu_bar_menus)
6808 maps += (nmaps - 1);
6809 nmaps = 1;
6812 /* Get the menu name from the first map that has one (a prompt string). */
6813 for (mapno = 0; mapno < nmaps; mapno++)
6815 name = map_prompt (maps[mapno]);
6816 if (!NILP (name))
6817 break;
6820 /* If we don't have any menus, just read a character normally. */
6821 if (mapno >= nmaps)
6822 return Qnil;
6824 #ifdef HAVE_MENUS
6825 /* If we got to this point via a mouse click,
6826 use a real menu for mouse selection. */
6827 if (EVENT_HAS_PARAMETERS (prev_event)
6828 && !EQ (XCONS (prev_event)->car, Qmenu_bar)
6829 && !EQ (XCONS (prev_event)->car, Qtool_bar))
6831 /* Display the menu and get the selection. */
6832 Lisp_Object *realmaps
6833 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
6834 Lisp_Object value;
6835 int nmaps1 = 0;
6837 /* Use the maps that are not nil. */
6838 for (mapno = 0; mapno < nmaps; mapno++)
6839 if (!NILP (maps[mapno]))
6840 realmaps[nmaps1++] = maps[mapno];
6842 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
6843 if (CONSP (value))
6845 Lisp_Object tem;
6847 record_menu_key (XCONS (value)->car);
6849 /* If we got multiple events, unread all but
6850 the first.
6851 There is no way to prevent those unread events
6852 from showing up later in last_nonmenu_event.
6853 So turn symbol and integer events into lists,
6854 to indicate that they came from a mouse menu,
6855 so that when present in last_nonmenu_event
6856 they won't confuse things. */
6857 for (tem = XCONS (value)->cdr; !NILP (tem);
6858 tem = XCONS (tem)->cdr)
6860 record_menu_key (XCONS (tem)->car);
6861 if (SYMBOLP (XCONS (tem)->car)
6862 || INTEGERP (XCONS (tem)->car))
6863 XCONS (tem)->car
6864 = Fcons (XCONS (tem)->car, Qnil);
6867 /* If we got more than one event, put all but the first
6868 onto this list to be read later.
6869 Return just the first event now. */
6870 Vunread_command_events
6871 = nconc2 (XCONS (value)->cdr, Vunread_command_events);
6872 value = XCONS (value)->car;
6874 else if (NILP (value))
6875 value = Qt;
6876 if (used_mouse_menu)
6877 *used_mouse_menu = 1;
6878 return value;
6880 #endif /* HAVE_MENUS */
6881 return Qnil ;
6884 /* Buffer in use so far for the minibuf prompts for menu keymaps.
6885 We make this bigger when necessary, and never free it. */
6886 static char *read_char_minibuf_menu_text;
6887 /* Size of that buffer. */
6888 static int read_char_minibuf_menu_width;
6890 static Lisp_Object
6891 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
6892 int commandflag ;
6893 int nmaps;
6894 Lisp_Object *maps;
6896 int mapno;
6897 register Lisp_Object name;
6898 int nlength;
6899 int width = FRAME_WIDTH (SELECTED_FRAME ()) - 4;
6900 int idx = -1;
6901 int nobindings = 1;
6902 Lisp_Object rest, vector;
6903 char *menu;
6905 if (! menu_prompting)
6906 return Qnil;
6908 /* Make sure we have a big enough buffer for the menu text. */
6909 if (read_char_minibuf_menu_text == 0)
6911 read_char_minibuf_menu_width = width + 4;
6912 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
6914 else if (width + 4 > read_char_minibuf_menu_width)
6916 read_char_minibuf_menu_width = width + 4;
6917 read_char_minibuf_menu_text
6918 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
6920 menu = read_char_minibuf_menu_text;
6922 /* Get the menu name from the first map that has one (a prompt string). */
6923 for (mapno = 0; mapno < nmaps; mapno++)
6925 name = map_prompt (maps[mapno]);
6926 if (!NILP (name))
6927 break;
6930 /* If we don't have any menus, just read a character normally. */
6931 if (mapno >= nmaps)
6932 return Qnil;
6934 /* Prompt string always starts with map's prompt, and a space. */
6935 strcpy (menu, XSTRING (name)->data);
6936 nlength = STRING_BYTES (XSTRING (name));
6937 menu[nlength++] = ':';
6938 menu[nlength++] = ' ';
6939 menu[nlength] = 0;
6941 /* Start prompting at start of first map. */
6942 mapno = 0;
6943 rest = maps[mapno];
6945 /* Present the documented bindings, a line at a time. */
6946 while (1)
6948 int notfirst = 0;
6949 int i = nlength;
6950 Lisp_Object obj;
6951 int ch;
6952 Lisp_Object orig_defn_macro;
6954 /* Loop over elements of map. */
6955 while (i < width)
6957 Lisp_Object elt;
6959 /* If reached end of map, start at beginning of next map. */
6960 if (NILP (rest))
6962 mapno++;
6963 /* At end of last map, wrap around to first map if just starting,
6964 or end this line if already have something on it. */
6965 if (mapno == nmaps)
6967 mapno = 0;
6968 if (notfirst || nobindings) break;
6970 rest = maps[mapno];
6973 /* Look at the next element of the map. */
6974 if (idx >= 0)
6975 elt = XVECTOR (vector)->contents[idx];
6976 else
6977 elt = Fcar_safe (rest);
6979 if (idx < 0 && VECTORP (elt))
6981 /* If we found a dense table in the keymap,
6982 advanced past it, but start scanning its contents. */
6983 rest = Fcdr_safe (rest);
6984 vector = elt;
6985 idx = 0;
6987 else
6989 /* An ordinary element. */
6990 Lisp_Object event, tem;
6992 if (idx < 0)
6994 event = Fcar_safe (elt); /* alist */
6995 elt = Fcdr_safe (elt);
6997 else
6999 XSETINT (event, idx); /* vector */
7002 /* Ignore the element if it has no prompt string. */
7003 if (INTEGERP (event) && parse_menu_item (elt, 0, -1))
7005 /* 1 if the char to type matches the string. */
7006 int char_matches;
7007 Lisp_Object upcased_event, downcased_event;
7008 Lisp_Object desc;
7009 Lisp_Object s
7010 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
7012 upcased_event = Fupcase (event);
7013 downcased_event = Fdowncase (event);
7014 char_matches = (XINT (upcased_event) == XSTRING (s)->data[0]
7015 || XINT (downcased_event) == XSTRING (s)->data[0]);
7016 if (! char_matches)
7017 desc = Fsingle_key_description (event);
7020 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
7021 if (!NILP (tem))
7022 /* Insert equivalent keybinding. */
7023 s = concat2 (s, tem);
7026 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
7027 if (EQ (tem, QCradio) || EQ (tem, QCtoggle))
7029 /* Insert button prefix. */
7030 Lisp_Object selected
7031 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
7032 if (EQ (tem, QCradio))
7033 tem = build_string (NILP (selected) ? "(*) " : "( ) ");
7034 else
7035 tem = build_string (NILP (selected) ? "[X] " : "[ ] ");
7036 s = concat2 (tem, s);
7040 /* If we have room for the prompt string, add it to this line.
7041 If this is the first on the line, always add it. */
7042 if ((XSTRING (s)->size + i + 2
7043 + (char_matches ? 0 : XSTRING (desc)->size + 3))
7044 < width
7045 || !notfirst)
7047 int thiswidth;
7049 /* Punctuate between strings. */
7050 if (notfirst)
7052 strcpy (menu + i, ", ");
7053 i += 2;
7055 notfirst = 1;
7056 nobindings = 0 ;
7058 /* If the char to type doesn't match the string's
7059 first char, explicitly show what char to type. */
7060 if (! char_matches)
7062 /* Add as much of string as fits. */
7063 thiswidth = XSTRING (desc)->size;
7064 if (thiswidth + i > width)
7065 thiswidth = width - i;
7066 bcopy (XSTRING (desc)->data, menu + i, thiswidth);
7067 i += thiswidth;
7068 strcpy (menu + i, " = ");
7069 i += 3;
7072 /* Add as much of string as fits. */
7073 thiswidth = XSTRING (s)->size;
7074 if (thiswidth + i > width)
7075 thiswidth = width - i;
7076 bcopy (XSTRING (s)->data, menu + i, thiswidth);
7077 i += thiswidth;
7078 menu[i] = 0;
7080 else
7082 /* If this element does not fit, end the line now,
7083 and save the element for the next line. */
7084 strcpy (menu + i, "...");
7085 break;
7089 /* Move past this element. */
7090 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
7091 /* Handle reaching end of dense table. */
7092 idx = -1;
7093 if (idx >= 0)
7094 idx++;
7095 else
7096 rest = Fcdr_safe (rest);
7100 /* Prompt with that and read response. */
7101 message2_nolog (menu, strlen (menu),
7102 ! NILP (current_buffer->enable_multibyte_characters));
7104 /* Make believe its not a keyboard macro in case the help char
7105 is pressed. Help characters are not recorded because menu prompting
7106 is not used on replay.
7108 orig_defn_macro = current_kboard->defining_kbd_macro;
7109 current_kboard->defining_kbd_macro = Qnil;
7111 obj = read_char (commandflag, 0, 0, Qnil, 0);
7112 while (BUFFERP (obj));
7113 current_kboard->defining_kbd_macro = orig_defn_macro;
7115 if (!INTEGERP (obj))
7116 return obj;
7117 else
7118 ch = XINT (obj);
7120 if (! EQ (obj, menu_prompt_more_char)
7121 && (!INTEGERP (menu_prompt_more_char)
7122 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
7124 if (!NILP (current_kboard->defining_kbd_macro))
7125 store_kbd_macro_char (obj);
7126 return obj;
7128 /* Help char - go round again */
7132 /* Reading key sequences. */
7134 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
7135 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
7136 keymap, or nil otherwise. Return the index of the first keymap in
7137 which KEY has any binding, or NMAPS if no map has a binding.
7139 If KEY is a meta ASCII character, treat it like meta-prefix-char
7140 followed by the corresponding non-meta character. Keymaps in
7141 CURRENT with non-prefix bindings for meta-prefix-char become nil in
7142 NEXT.
7144 If KEY has no bindings in any of the CURRENT maps, NEXT is left
7145 unmodified.
7147 NEXT may be the same array as CURRENT. */
7149 static int
7150 follow_key (key, nmaps, current, defs, next)
7151 Lisp_Object key;
7152 Lisp_Object *current, *defs, *next;
7153 int nmaps;
7155 int i, first_binding;
7156 int did_meta = 0;
7158 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
7159 followed by the corresponding non-meta character.
7160 Put the results into DEFS, since we are going to alter that anyway.
7161 Do not alter CURRENT or NEXT. */
7162 if (INTEGERP (key) && (XINT (key) & CHAR_META))
7164 for (i = 0; i < nmaps; i++)
7165 if (! NILP (current[i]))
7167 Lisp_Object def;
7168 def = get_keyelt (access_keymap (current[i],
7169 meta_prefix_char, 1, 0), 0);
7171 /* Note that since we pass the resulting bindings through
7172 get_keymap_1, non-prefix bindings for meta-prefix-char
7173 disappear. */
7174 defs[i] = get_keymap_1 (def, 0, 1);
7176 else
7177 defs[i] = Qnil;
7179 did_meta = 1;
7180 XSETINT (key, XFASTINT (key) & ~CHAR_META);
7183 first_binding = nmaps;
7184 for (i = nmaps - 1; i >= 0; i--)
7186 if (! NILP (current[i]))
7188 Lisp_Object map;
7189 if (did_meta)
7190 map = defs[i];
7191 else
7192 map = current[i];
7194 defs[i] = get_keyelt (access_keymap (map, key, 1, 0), 0);
7195 if (! NILP (defs[i]))
7196 first_binding = i;
7198 else
7199 defs[i] = Qnil;
7202 /* Given the set of bindings we've found, produce the next set of maps. */
7203 if (first_binding < nmaps)
7204 for (i = 0; i < nmaps; i++)
7205 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
7207 return first_binding;
7210 /* Read a sequence of keys that ends with a non prefix character,
7211 storing it in KEYBUF, a buffer of size BUFSIZE.
7212 Prompt with PROMPT.
7213 Return the length of the key sequence stored.
7214 Return -1 if the user rejected a command menu.
7216 Echo starting immediately unless `prompt' is 0.
7218 Where a key sequence ends depends on the currently active keymaps.
7219 These include any minor mode keymaps active in the current buffer,
7220 the current buffer's local map, and the global map.
7222 If a key sequence has no other bindings, we check Vfunction_key_map
7223 to see if some trailing subsequence might be the beginning of a
7224 function key's sequence. If so, we try to read the whole function
7225 key, and substitute its symbolic name into the key sequence.
7227 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
7228 `double-' events into similar click events, if that would make them
7229 bound. We try to turn `triple-' events first into `double-' events,
7230 then into clicks.
7232 If we get a mouse click in a mode line, vertical divider, or other
7233 non-text area, we treat the click as if it were prefixed by the
7234 symbol denoting that area - `mode-line', `vertical-line', or
7235 whatever.
7237 If the sequence starts with a mouse click, we read the key sequence
7238 with respect to the buffer clicked on, not the current buffer.
7240 If the user switches frames in the midst of a key sequence, we put
7241 off the switch-frame event until later; the next call to
7242 read_char will return it.
7244 If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
7245 from the selected window's buffer. */
7247 static int
7248 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
7249 can_return_switch_frame, fix_current_buffer)
7250 Lisp_Object *keybuf;
7251 int bufsize;
7252 Lisp_Object prompt;
7253 int dont_downcase_last;
7254 int can_return_switch_frame;
7255 int fix_current_buffer;
7257 int count = specpdl_ptr - specpdl;
7259 /* How many keys there are in the current key sequence. */
7260 int t;
7262 /* The length of the echo buffer when we started reading, and
7263 the length of this_command_keys when we started reading. */
7264 int echo_start;
7265 int keys_start;
7267 /* The number of keymaps we're scanning right now, and the number of
7268 keymaps we have allocated space for. */
7269 int nmaps;
7270 int nmaps_allocated = 0;
7272 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
7273 the current keymaps. */
7274 Lisp_Object *defs;
7276 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
7277 in the current keymaps, or nil where it is not a prefix. */
7278 Lisp_Object *submaps;
7280 /* The local map to start out with at start of key sequence. */
7281 Lisp_Object orig_local_map;
7283 /* 1 if we have already considered switching to the local-map property
7284 of the place where a mouse click occurred. */
7285 int localized_local_map = 0;
7287 /* The index in defs[] of the first keymap that has a binding for
7288 this key sequence. In other words, the lowest i such that
7289 defs[i] is non-nil. */
7290 int first_binding;
7292 /* If t < mock_input, then KEYBUF[t] should be read as the next
7293 input key.
7295 We use this to recover after recognizing a function key. Once we
7296 realize that a suffix of the current key sequence is actually a
7297 function key's escape sequence, we replace the suffix with the
7298 function key's binding from Vfunction_key_map. Now keybuf
7299 contains a new and different key sequence, so the echo area,
7300 this_command_keys, and the submaps and defs arrays are wrong. In
7301 this situation, we set mock_input to t, set t to 0, and jump to
7302 restart_sequence; the loop will read keys from keybuf up until
7303 mock_input, thus rebuilding the state; and then it will resume
7304 reading characters from the keyboard. */
7305 int mock_input = 0;
7307 /* If the sequence is unbound in submaps[], then
7308 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
7309 and fkey_map is its binding.
7311 These might be > t, indicating that all function key scanning
7312 should hold off until t reaches them. We do this when we've just
7313 recognized a function key, to avoid searching for the function
7314 key's again in Vfunction_key_map. */
7315 int fkey_start = 0, fkey_end = 0;
7316 Lisp_Object fkey_map;
7318 /* Likewise, for key_translation_map. */
7319 int keytran_start = 0, keytran_end = 0;
7320 Lisp_Object keytran_map;
7322 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
7323 we put it off for later. While we're reading, we keep the event here. */
7324 Lisp_Object delayed_switch_frame;
7326 /* See the comment below... */
7327 #if defined (GOBBLE_FIRST_EVENT)
7328 Lisp_Object first_event;
7329 #endif
7331 Lisp_Object original_uppercase;
7332 int original_uppercase_position = -1;
7334 /* Gets around Microsoft compiler limitations. */
7335 int dummyflag = 0;
7337 struct buffer *starting_buffer;
7339 /* Nonzero if we seem to have got the beginning of a binding
7340 in function_key_map. */
7341 int function_key_possible = 0;
7342 int key_translation_possible = 0;
7344 /* Save the status of key translation before each step,
7345 so that we can restore this after downcasing. */
7346 Lisp_Object prev_fkey_map;
7347 int prev_fkey_start;
7348 int prev_fkey_end;
7350 Lisp_Object prev_keytran_map;
7351 int prev_keytran_start;
7352 int prev_keytran_end;
7354 #if defined (GOBBLE_FIRST_EVENT)
7355 int junk;
7356 #endif
7358 raw_keybuf_count = 0;
7360 last_nonmenu_event = Qnil;
7362 delayed_switch_frame = Qnil;
7363 fkey_map = Vfunction_key_map;
7364 keytran_map = Vkey_translation_map;
7366 /* If there is no function-key-map, turn off function key scanning. */
7367 if (NILP (Fkeymapp (Vfunction_key_map)))
7368 fkey_start = fkey_end = bufsize + 1;
7370 /* If there is no key-translation-map, turn off scanning. */
7371 if (NILP (Fkeymapp (Vkey_translation_map)))
7372 keytran_start = keytran_end = bufsize + 1;
7374 if (INTERACTIVE)
7376 if (!NILP (prompt))
7377 echo_prompt (XSTRING (prompt)->data);
7378 else if (cursor_in_echo_area && echo_keystrokes)
7379 /* This doesn't put in a dash if the echo buffer is empty, so
7380 you don't always see a dash hanging out in the minibuffer. */
7381 echo_dash ();
7384 /* Record the initial state of the echo area and this_command_keys;
7385 we will need to restore them if we replay a key sequence. */
7386 if (INTERACTIVE)
7387 echo_start = echo_length ();
7388 keys_start = this_command_key_count;
7389 this_single_command_key_start = keys_start;
7391 #if defined (GOBBLE_FIRST_EVENT)
7392 /* This doesn't quite work, because some of the things that read_char
7393 does cannot safely be bypassed. It seems too risky to try to make
7394 this work right. */
7396 /* Read the first char of the sequence specially, before setting
7397 up any keymaps, in case a filter runs and switches buffers on us. */
7398 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
7399 &junk);
7400 #endif /* GOBBLE_FIRST_EVENT */
7402 orig_local_map = get_local_map (PT, current_buffer);
7404 /* We jump here when the key sequence has been thoroughly changed, and
7405 we need to rescan it starting from the beginning. When we jump here,
7406 keybuf[0..mock_input] holds the sequence we should reread. */
7407 replay_sequence:
7409 starting_buffer = current_buffer;
7410 function_key_possible = 0;
7411 key_translation_possible = 0;
7413 /* Build our list of keymaps.
7414 If we recognize a function key and replace its escape sequence in
7415 keybuf with its symbol, or if the sequence starts with a mouse
7416 click and we need to switch buffers, we jump back here to rebuild
7417 the initial keymaps from the current buffer. */
7419 Lisp_Object *maps;
7421 if (!NILP (current_kboard->Voverriding_terminal_local_map)
7422 || !NILP (Voverriding_local_map))
7424 if (3 > nmaps_allocated)
7426 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
7427 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
7428 nmaps_allocated = 3;
7430 nmaps = 0;
7431 if (!NILP (current_kboard->Voverriding_terminal_local_map))
7432 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
7433 if (!NILP (Voverriding_local_map))
7434 submaps[nmaps++] = Voverriding_local_map;
7436 else
7438 nmaps = current_minor_maps (0, &maps);
7439 if (nmaps + 2 > nmaps_allocated)
7441 submaps = (Lisp_Object *) alloca ((nmaps+2) * sizeof (submaps[0]));
7442 defs = (Lisp_Object *) alloca ((nmaps+2) * sizeof (defs[0]));
7443 nmaps_allocated = nmaps + 2;
7445 bcopy (maps, submaps, nmaps * sizeof (submaps[0]));
7446 #ifdef USE_TEXT_PROPERTIES
7447 submaps[nmaps++] = orig_local_map;
7448 #else
7449 submaps[nmaps++] = current_buffer->keymap;
7450 #endif
7452 submaps[nmaps++] = current_global_map;
7455 /* Find an accurate initial value for first_binding. */
7456 for (first_binding = 0; first_binding < nmaps; first_binding++)
7457 if (! NILP (submaps[first_binding]))
7458 break;
7460 /* Start from the beginning in keybuf. */
7461 t = 0;
7463 /* These are no-ops the first time through, but if we restart, they
7464 revert the echo area and this_command_keys to their original state. */
7465 this_command_key_count = keys_start;
7466 if (INTERACTIVE && t < mock_input)
7467 echo_truncate (echo_start);
7469 /* If the best binding for the current key sequence is a keymap, or
7470 we may be looking at a function key's escape sequence, keep on
7471 reading. */
7472 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
7473 || (first_binding >= nmaps
7474 && fkey_start < t
7475 /* mock input is never part of a function key's sequence. */
7476 && mock_input <= fkey_start)
7477 || (first_binding >= nmaps
7478 && keytran_start < t && key_translation_possible)
7479 /* Don't return in the middle of a possible function key sequence,
7480 if the only bindings we found were via case conversion.
7481 Thus, if ESC O a has a function-key-map translation
7482 and ESC o has a binding, don't return after ESC O,
7483 so that we can translate ESC O plus the next character. */
7486 Lisp_Object key;
7487 int used_mouse_menu = 0;
7489 /* Where the last real key started. If we need to throw away a
7490 key that has expanded into more than one element of keybuf
7491 (say, a mouse click on the mode line which is being treated
7492 as [mode-line (mouse-...)], then we backtrack to this point
7493 of keybuf. */
7494 int last_real_key_start;
7496 /* These variables are analogous to echo_start and keys_start;
7497 while those allow us to restart the entire key sequence,
7498 echo_local_start and keys_local_start allow us to throw away
7499 just one key. */
7500 int echo_local_start, keys_local_start, local_first_binding;
7502 if (t >= bufsize)
7503 error ("Key sequence too long");
7505 if (INTERACTIVE)
7506 echo_local_start = echo_length ();
7507 keys_local_start = this_command_key_count;
7508 local_first_binding = first_binding;
7510 replay_key:
7511 /* These are no-ops, unless we throw away a keystroke below and
7512 jumped back up to replay_key; in that case, these restore the
7513 variables to their original state, allowing us to replay the
7514 loop. */
7515 if (INTERACTIVE && t < mock_input)
7516 echo_truncate (echo_local_start);
7517 this_command_key_count = keys_local_start;
7518 first_binding = local_first_binding;
7520 /* By default, assume each event is "real". */
7521 last_real_key_start = t;
7523 /* Does mock_input indicate that we are re-reading a key sequence? */
7524 if (t < mock_input)
7526 key = keybuf[t];
7527 add_command_key (key);
7528 if (echo_keystrokes)
7529 echo_char (key);
7532 /* If not, we should actually read a character. */
7533 else
7536 #ifdef MULTI_KBOARD
7537 KBOARD *interrupted_kboard = current_kboard;
7538 struct frame *interrupted_frame = SELECTED_FRAME ();
7539 if (setjmp (wrong_kboard_jmpbuf))
7541 if (!NILP (delayed_switch_frame))
7543 interrupted_kboard->kbd_queue
7544 = Fcons (delayed_switch_frame,
7545 interrupted_kboard->kbd_queue);
7546 delayed_switch_frame = Qnil;
7548 while (t > 0)
7549 interrupted_kboard->kbd_queue
7550 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
7552 /* If the side queue is non-empty, ensure it begins with a
7553 switch-frame, so we'll replay it in the right context. */
7554 if (CONSP (interrupted_kboard->kbd_queue)
7555 && (key = XCONS (interrupted_kboard->kbd_queue)->car,
7556 !(EVENT_HAS_PARAMETERS (key)
7557 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
7558 Qswitch_frame))))
7560 Lisp_Object frame;
7561 XSETFRAME (frame, interrupted_frame);
7562 interrupted_kboard->kbd_queue
7563 = Fcons (make_lispy_switch_frame (frame),
7564 interrupted_kboard->kbd_queue);
7566 mock_input = 0;
7567 orig_local_map = get_local_map (PT, current_buffer);
7568 goto replay_sequence;
7570 #endif
7571 key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
7572 &used_mouse_menu);
7575 /* read_char returns t when it shows a menu and the user rejects it.
7576 Just return -1. */
7577 if (EQ (key, Qt))
7579 unbind_to (count, Qnil);
7580 return -1;
7583 /* read_char returns -1 at the end of a macro.
7584 Emacs 18 handles this by returning immediately with a
7585 zero, so that's what we'll do. */
7586 if (INTEGERP (key) && XINT (key) == -1)
7588 t = 0;
7589 /* The Microsoft C compiler can't handle the goto that
7590 would go here. */
7591 dummyflag = 1;
7592 break;
7595 /* If the current buffer has been changed from under us, the
7596 keymap may have changed, so replay the sequence. */
7597 if (BUFFERP (key))
7599 mock_input = t;
7600 /* Reset the current buffer from the selected window
7601 in case something changed the former and not the latter.
7602 This is to be more consistent with the behavior
7603 of the command_loop_1. */
7604 if (fix_current_buffer)
7606 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7607 Fkill_emacs (Qnil);
7608 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
7609 Fset_buffer (XWINDOW (selected_window)->buffer);
7612 orig_local_map = get_local_map (PT, current_buffer);
7613 goto replay_sequence;
7616 /* If we have a quit that was typed in another frame, and
7617 quit_throw_to_read_char switched buffers,
7618 replay to get the right keymap. */
7619 if (XINT (key) == quit_char && current_buffer != starting_buffer)
7621 GROW_RAW_KEYBUF;
7622 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
7623 keybuf[t++] = key;
7624 mock_input = t;
7625 Vquit_flag = Qnil;
7626 orig_local_map = get_local_map (PT, current_buffer);
7627 goto replay_sequence;
7630 Vquit_flag = Qnil;
7632 if (EVENT_HAS_PARAMETERS (key)
7633 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), Qswitch_frame))
7635 /* If we're at the beginning of a key sequence, and the caller
7636 says it's okay, go ahead and return this event. If we're
7637 in the midst of a key sequence, delay it until the end. */
7638 if (t > 0 || !can_return_switch_frame)
7640 delayed_switch_frame = key;
7641 goto replay_key;
7645 GROW_RAW_KEYBUF;
7646 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
7649 /* Clicks in non-text areas get prefixed by the symbol
7650 in their CHAR-ADDRESS field. For example, a click on
7651 the mode line is prefixed by the symbol `mode-line'.
7653 Furthermore, key sequences beginning with mouse clicks
7654 are read using the keymaps of the buffer clicked on, not
7655 the current buffer. So we may have to switch the buffer
7656 here.
7658 When we turn one event into two events, we must make sure
7659 that neither of the two looks like the original--so that,
7660 if we replay the events, they won't be expanded again.
7661 If not for this, such reexpansion could happen either here
7662 or when user programs play with this-command-keys. */
7663 if (EVENT_HAS_PARAMETERS (key))
7665 Lisp_Object kind;
7667 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
7668 if (EQ (kind, Qmouse_click))
7670 Lisp_Object window, posn;
7672 window = POSN_WINDOW (EVENT_START (key));
7673 posn = POSN_BUFFER_POSN (EVENT_START (key));
7675 if (CONSP (posn))
7677 /* We're looking at the second event of a
7678 sequence which we expanded before. Set
7679 last_real_key_start appropriately. */
7680 if (t > 0)
7681 last_real_key_start = t - 1;
7684 /* Key sequences beginning with mouse clicks are
7685 read using the keymaps in the buffer clicked on,
7686 not the current buffer. If we're at the
7687 beginning of a key sequence, switch buffers. */
7688 if (last_real_key_start == 0
7689 && WINDOWP (window)
7690 && BUFFERP (XWINDOW (window)->buffer)
7691 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
7693 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
7694 keybuf[t] = key;
7695 mock_input = t + 1;
7697 /* Arrange to go back to the original buffer once we're
7698 done reading the key sequence. Note that we can't
7699 use save_excursion_{save,restore} here, because they
7700 save point as well as the current buffer; we don't
7701 want to save point, because redisplay may change it,
7702 to accommodate a Fset_window_start or something. We
7703 don't want to do this at the top of the function,
7704 because we may get input from a subprocess which
7705 wants to change the selected window and stuff (say,
7706 emacsclient). */
7707 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
7709 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7710 Fkill_emacs (Qnil);
7711 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
7712 orig_local_map = get_local_map (PT, current_buffer);
7713 goto replay_sequence;
7716 /* For a mouse click, get the local text-property keymap
7717 of the place clicked on, rather than point. */
7718 if (last_real_key_start == 0
7719 && CONSP (XCONS (key)->cdr)
7720 && ! localized_local_map)
7722 Lisp_Object map_here, start, pos;
7724 localized_local_map = 1;
7725 start = EVENT_START (key);
7727 if (CONSP (start) && CONSP (XCONS (start)->cdr))
7729 pos = POSN_BUFFER_POSN (start);
7730 if (INTEGERP (pos)
7731 && XINT (pos) >= BEG && XINT (pos) <= Z)
7733 map_here = get_local_map (XINT (pos), current_buffer);
7734 if (!EQ (map_here, orig_local_map))
7736 orig_local_map = map_here;
7737 keybuf[t] = key;
7738 mock_input = t + 1;
7740 goto replay_sequence;
7746 /* Expand mode-line and scroll-bar events into two events:
7747 use posn as a fake prefix key. */
7748 if (SYMBOLP (posn))
7750 if (t + 1 >= bufsize)
7751 error ("Key sequence too long");
7752 keybuf[t] = posn;
7753 keybuf[t+1] = key;
7754 mock_input = t + 2;
7756 /* Zap the position in key, so we know that we've
7757 expanded it, and don't try to do so again. */
7758 POSN_BUFFER_POSN (EVENT_START (key))
7759 = Fcons (posn, Qnil);
7761 /* If on a mode line string with a local keymap,
7762 reconsider the key sequence with that keymap. */
7763 if (CONSP (POSN_STRING (EVENT_START (key))))
7765 Lisp_Object string, pos, map;
7767 string = POSN_STRING (EVENT_START (key));
7768 pos = XCDR (string);
7769 string = XCAR (string);
7771 if (pos >= 0
7772 && pos < XSTRING (string)->size
7773 && (map = Fget_text_property (pos, Qlocal_map,
7774 string),
7775 !NILP (map)))
7777 orig_local_map = map;
7778 goto replay_sequence;
7782 goto replay_key;
7785 else if (CONSP (XCONS (key)->cdr)
7786 && CONSP (EVENT_START (key))
7787 && CONSP (XCONS (EVENT_START (key))->cdr))
7789 Lisp_Object posn;
7791 posn = POSN_BUFFER_POSN (EVENT_START (key));
7792 /* Handle menu-bar events:
7793 insert the dummy prefix event `menu-bar'. */
7794 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
7796 if (t + 1 >= bufsize)
7797 error ("Key sequence too long");
7798 keybuf[t] = posn;
7799 keybuf[t+1] = key;
7801 /* Zap the position in key, so we know that we've
7802 expanded it, and don't try to do so again. */
7803 POSN_BUFFER_POSN (EVENT_START (key))
7804 = Fcons (posn, Qnil);
7806 mock_input = t + 2;
7807 goto replay_sequence;
7809 else if (CONSP (posn))
7811 /* We're looking at the second event of a
7812 sequence which we expanded before. Set
7813 last_real_key_start appropriately. */
7814 if (last_real_key_start == t && t > 0)
7815 last_real_key_start = t - 1;
7820 /* We have finally decided that KEY is something we might want
7821 to look up. */
7822 first_binding = (follow_key (key,
7823 nmaps - first_binding,
7824 submaps + first_binding,
7825 defs + first_binding,
7826 submaps + first_binding)
7827 + first_binding);
7829 /* If KEY wasn't bound, we'll try some fallbacks. */
7830 if (first_binding >= nmaps)
7832 Lisp_Object head;
7834 head = EVENT_HEAD (key);
7835 if (help_char_p (head) && t > 0)
7837 read_key_sequence_cmd = Vprefix_help_command;
7838 keybuf[t++] = key;
7839 last_nonmenu_event = key;
7840 /* The Microsoft C compiler can't handle the goto that
7841 would go here. */
7842 dummyflag = 1;
7843 break;
7846 if (SYMBOLP (head))
7848 Lisp_Object breakdown;
7849 int modifiers;
7851 breakdown = parse_modifiers (head);
7852 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
7853 /* Attempt to reduce an unbound mouse event to a simpler
7854 event that is bound:
7855 Drags reduce to clicks.
7856 Double-clicks reduce to clicks.
7857 Triple-clicks reduce to double-clicks, then to clicks.
7858 Down-clicks are eliminated.
7859 Double-downs reduce to downs, then are eliminated.
7860 Triple-downs reduce to double-downs, then to downs,
7861 then are eliminated. */
7862 if (modifiers & (down_modifier | drag_modifier
7863 | double_modifier | triple_modifier))
7865 while (modifiers & (down_modifier | drag_modifier
7866 | double_modifier | triple_modifier))
7868 Lisp_Object new_head, new_click;
7869 if (modifiers & triple_modifier)
7870 modifiers ^= (double_modifier | triple_modifier);
7871 else if (modifiers & double_modifier)
7872 modifiers &= ~double_modifier;
7873 else if (modifiers & drag_modifier)
7874 modifiers &= ~drag_modifier;
7875 else
7877 /* Dispose of this `down' event by simply jumping
7878 back to replay_key, to get another event.
7880 Note that if this event came from mock input,
7881 then just jumping back to replay_key will just
7882 hand it to us again. So we have to wipe out any
7883 mock input.
7885 We could delete keybuf[t] and shift everything
7886 after that to the left by one spot, but we'd also
7887 have to fix up any variable that points into
7888 keybuf, and shifting isn't really necessary
7889 anyway.
7891 Adding prefixes for non-textual mouse clicks
7892 creates two characters of mock input, and both
7893 must be thrown away. If we're only looking at
7894 the prefix now, we can just jump back to
7895 replay_key. On the other hand, if we've already
7896 processed the prefix, and now the actual click
7897 itself is giving us trouble, then we've lost the
7898 state of the keymaps we want to backtrack to, and
7899 we need to replay the whole sequence to rebuild
7902 Beyond that, only function key expansion could
7903 create more than two keys, but that should never
7904 generate mouse events, so it's okay to zero
7905 mock_input in that case too.
7907 Isn't this just the most wonderful code ever? */
7908 if (t == last_real_key_start)
7910 mock_input = 0;
7911 goto replay_key;
7913 else
7915 mock_input = last_real_key_start;
7916 goto replay_sequence;
7920 new_head
7921 = apply_modifiers (modifiers, XCONS (breakdown)->car);
7922 new_click
7923 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
7925 /* Look for a binding for this new key. follow_key
7926 promises that it didn't munge submaps the
7927 last time we called it, since key was unbound. */
7928 first_binding
7929 = (follow_key (new_click,
7930 nmaps - local_first_binding,
7931 submaps + local_first_binding,
7932 defs + local_first_binding,
7933 submaps + local_first_binding)
7934 + local_first_binding);
7936 /* If that click is bound, go for it. */
7937 if (first_binding < nmaps)
7939 key = new_click;
7940 break;
7942 /* Otherwise, we'll leave key set to the drag event. */
7948 keybuf[t++] = key;
7949 /* Normally, last_nonmenu_event gets the previous key we read.
7950 But when a mouse popup menu is being used,
7951 we don't update last_nonmenu_event; it continues to hold the mouse
7952 event that preceded the first level of menu. */
7953 if (!used_mouse_menu)
7954 last_nonmenu_event = key;
7956 /* Record what part of this_command_keys is the current key sequence. */
7957 this_single_command_key_start = this_command_key_count - t;
7959 prev_fkey_map = fkey_map;
7960 prev_fkey_start = fkey_start;
7961 prev_fkey_end = fkey_end;
7963 prev_keytran_map = keytran_map;
7964 prev_keytran_start = keytran_start;
7965 prev_keytran_end = keytran_end;
7967 /* If the sequence is unbound, see if we can hang a function key
7968 off the end of it. We only want to scan real keyboard input
7969 for function key sequences, so if mock_input says that we're
7970 re-reading old events, don't examine it. */
7971 if (first_binding >= nmaps
7972 && t >= mock_input)
7974 Lisp_Object fkey_next;
7976 /* Continue scan from fkey_end until we find a bound suffix.
7977 If we fail, increment fkey_start
7978 and start fkey_end from there. */
7979 while (fkey_end < t)
7981 Lisp_Object key;
7983 key = keybuf[fkey_end++];
7984 /* Look up meta-characters by prefixing them
7985 with meta_prefix_char. I hate this. */
7986 if (INTEGERP (key) && XINT (key) & meta_modifier)
7988 fkey_next
7989 = get_keymap_1
7990 (get_keyelt
7991 (access_keymap (fkey_map, meta_prefix_char, 1, 0), 0),
7992 0, 1);
7993 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
7995 else
7996 fkey_next = fkey_map;
7998 fkey_next
7999 = get_keyelt (access_keymap (fkey_next, key, 1, 0), 1);
8001 /* Handle symbol with autoload definition. */
8002 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8003 && CONSP (XSYMBOL (fkey_next)->function)
8004 && EQ (XCONS (XSYMBOL (fkey_next)->function)->car, Qautoload))
8005 do_autoload (XSYMBOL (fkey_next)->function,
8006 fkey_next);
8008 /* Handle a symbol whose function definition is a keymap
8009 or an array. */
8010 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8011 && (!NILP (Farrayp (XSYMBOL (fkey_next)->function))
8012 || !NILP (Fkeymapp (XSYMBOL (fkey_next)->function))))
8013 fkey_next = XSYMBOL (fkey_next)->function;
8015 #if 0 /* I didn't turn this on, because it might cause trouble
8016 for the mapping of return into C-m and tab into C-i. */
8017 /* Optionally don't map function keys into other things.
8018 This enables the user to redefine kp- keys easily. */
8019 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
8020 fkey_next = Qnil;
8021 #endif
8023 /* If the function key map gives a function, not an
8024 array, then call the function with no args and use
8025 its value instead. */
8026 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8027 && fkey_end == t)
8029 struct gcpro gcpro1, gcpro2, gcpro3;
8030 Lisp_Object tem;
8031 tem = fkey_next;
8033 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
8034 fkey_next = call1 (fkey_next, prompt);
8035 UNGCPRO;
8036 /* If the function returned something invalid,
8037 barf--don't ignore it.
8038 (To ignore it safely, we would need to gcpro a bunch of
8039 other variables.) */
8040 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
8041 error ("Function in key-translation-map returns invalid key sequence");
8044 function_key_possible = ! NILP (fkey_next);
8046 /* If keybuf[fkey_start..fkey_end] is bound in the
8047 function key map and it's a suffix of the current
8048 sequence (i.e. fkey_end == t), replace it with
8049 the binding and restart with fkey_start at the end. */
8050 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
8051 && fkey_end == t)
8053 int len = XFASTINT (Flength (fkey_next));
8055 t = fkey_start + len;
8056 if (t >= bufsize)
8057 error ("Key sequence too long");
8059 if (VECTORP (fkey_next))
8060 bcopy (XVECTOR (fkey_next)->contents,
8061 keybuf + fkey_start,
8062 (t - fkey_start) * sizeof (keybuf[0]));
8063 else if (STRINGP (fkey_next))
8065 int i;
8067 for (i = 0; i < len; i++)
8068 XSETFASTINT (keybuf[fkey_start + i],
8069 XSTRING (fkey_next)->data[i]);
8072 mock_input = t;
8073 fkey_start = fkey_end = t;
8074 fkey_map = Vfunction_key_map;
8076 /* Do pass the results through key-translation-map.
8077 But don't retranslate what key-translation-map
8078 has already translated. */
8079 keytran_end = keytran_start;
8080 keytran_map = Vkey_translation_map;
8082 goto replay_sequence;
8085 fkey_map = get_keymap_1 (fkey_next, 0, 1);
8087 /* If we no longer have a bound suffix, try a new positions for
8088 fkey_start. */
8089 if (NILP (fkey_map))
8091 fkey_end = ++fkey_start;
8092 fkey_map = Vfunction_key_map;
8093 function_key_possible = 0;
8098 /* Look for this sequence in key-translation-map. */
8100 Lisp_Object keytran_next;
8102 /* Scan from keytran_end until we find a bound suffix. */
8103 while (keytran_end < t)
8105 Lisp_Object key;
8107 key = keybuf[keytran_end++];
8108 /* Look up meta-characters by prefixing them
8109 with meta_prefix_char. I hate this. */
8110 if (INTEGERP (key) && XINT (key) & meta_modifier)
8112 keytran_next
8113 = get_keymap_1
8114 (get_keyelt
8115 (access_keymap (keytran_map, meta_prefix_char, 1, 0), 0),
8116 0, 1);
8117 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
8119 else
8120 keytran_next = keytran_map;
8122 keytran_next
8123 = get_keyelt (access_keymap (keytran_next, key, 1, 0), 1);
8125 /* Handle symbol with autoload definition. */
8126 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8127 && CONSP (XSYMBOL (keytran_next)->function)
8128 && EQ (XCONS (XSYMBOL (keytran_next)->function)->car, Qautoload))
8129 do_autoload (XSYMBOL (keytran_next)->function,
8130 keytran_next);
8132 /* Handle a symbol whose function definition is a keymap
8133 or an array. */
8134 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8135 && (!NILP (Farrayp (XSYMBOL (keytran_next)->function))
8136 || !NILP (Fkeymapp (XSYMBOL (keytran_next)->function))))
8137 keytran_next = XSYMBOL (keytran_next)->function;
8139 /* If the key translation map gives a function, not an
8140 array, then call the function with one arg and use
8141 its value instead. */
8142 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8143 && keytran_end == t)
8145 struct gcpro gcpro1, gcpro2, gcpro3;
8146 Lisp_Object tem;
8147 tem = keytran_next;
8149 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
8150 keytran_next = call1 (keytran_next, prompt);
8151 UNGCPRO;
8152 /* If the function returned something invalid,
8153 barf--don't ignore it.
8154 (To ignore it safely, we would need to gcpro a bunch of
8155 other variables.) */
8156 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
8157 error ("Function in key-translation-map returns invalid key sequence");
8160 key_translation_possible = ! NILP (keytran_next);
8162 /* If keybuf[keytran_start..keytran_end] is bound in the
8163 key translation map and it's a suffix of the current
8164 sequence (i.e. keytran_end == t), replace it with
8165 the binding and restart with keytran_start at the end. */
8166 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
8167 && keytran_end == t)
8169 int len = XFASTINT (Flength (keytran_next));
8171 t = keytran_start + len;
8172 if (t >= bufsize)
8173 error ("Key sequence too long");
8175 if (VECTORP (keytran_next))
8176 bcopy (XVECTOR (keytran_next)->contents,
8177 keybuf + keytran_start,
8178 (t - keytran_start) * sizeof (keybuf[0]));
8179 else if (STRINGP (keytran_next))
8181 int i;
8183 for (i = 0; i < len; i++)
8184 XSETFASTINT (keybuf[keytran_start + i],
8185 XSTRING (keytran_next)->data[i]);
8188 mock_input = t;
8189 keytran_start = keytran_end = t;
8190 keytran_map = Vkey_translation_map;
8192 /* Don't pass the results of key-translation-map
8193 through function-key-map. */
8194 fkey_start = fkey_end = t;
8195 fkey_map = Vfunction_key_map;
8197 goto replay_sequence;
8200 keytran_map = get_keymap_1 (keytran_next, 0, 1);
8202 /* If we no longer have a bound suffix, try a new positions for
8203 keytran_start. */
8204 if (NILP (keytran_map))
8206 keytran_end = ++keytran_start;
8207 keytran_map = Vkey_translation_map;
8208 key_translation_possible = 0;
8213 /* If KEY is not defined in any of the keymaps,
8214 and cannot be part of a function key or translation,
8215 and is an upper case letter
8216 use the corresponding lower-case letter instead. */
8217 if (first_binding == nmaps && ! function_key_possible
8218 && ! key_translation_possible
8219 && INTEGERP (key)
8220 && ((((XINT (key) & 0x3ffff)
8221 < XCHAR_TABLE (current_buffer->downcase_table)->size)
8222 && UPPERCASEP (XINT (key) & 0x3ffff))
8223 || (XINT (key) & shift_modifier)))
8225 Lisp_Object new_key;
8227 original_uppercase = key;
8228 original_uppercase_position = t - 1;
8230 if (XINT (key) & shift_modifier)
8231 XSETINT (new_key, XINT (key) & ~shift_modifier);
8232 else
8233 XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
8234 | (XINT (key) & ~0x3ffff)));
8236 /* We have to do this unconditionally, regardless of whether
8237 the lower-case char is defined in the keymaps, because they
8238 might get translated through function-key-map. */
8239 keybuf[t - 1] = new_key;
8240 mock_input = t;
8242 fkey_map = prev_fkey_map;
8243 fkey_start = prev_fkey_start;
8244 fkey_end = prev_fkey_end;
8246 keytran_map = prev_keytran_map;
8247 keytran_start = prev_keytran_start;
8248 keytran_end = prev_keytran_end;
8250 goto replay_sequence;
8252 /* If KEY is not defined in any of the keymaps,
8253 and cannot be part of a function key or translation,
8254 and is a shifted function key,
8255 use the corresponding unshifted function key instead. */
8256 if (first_binding == nmaps && ! function_key_possible
8257 && ! key_translation_possible
8258 && SYMBOLP (key))
8260 Lisp_Object breakdown;
8261 int modifiers;
8263 breakdown = parse_modifiers (key);
8264 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
8265 if (modifiers & shift_modifier)
8267 Lisp_Object new_key;
8269 original_uppercase = key;
8270 original_uppercase_position = t - 1;
8272 modifiers &= ~shift_modifier;
8273 new_key = apply_modifiers (modifiers,
8274 XCONS (breakdown)->car);
8276 keybuf[t - 1] = new_key;
8277 mock_input = t;
8279 fkey_map = prev_fkey_map;
8280 fkey_start = prev_fkey_start;
8281 fkey_end = prev_fkey_end;
8283 keytran_map = prev_keytran_map;
8284 keytran_start = prev_keytran_start;
8285 keytran_end = prev_keytran_end;
8287 goto replay_sequence;
8292 if (!dummyflag)
8293 read_key_sequence_cmd = (first_binding < nmaps
8294 ? defs[first_binding]
8295 : Qnil);
8297 unread_switch_frame = delayed_switch_frame;
8298 unbind_to (count, Qnil);
8300 /* Don't downcase the last character if the caller says don't.
8301 Don't downcase it if the result is undefined, either. */
8302 if ((dont_downcase_last || first_binding >= nmaps)
8303 && t - 1 == original_uppercase_position)
8304 keybuf[t - 1] = original_uppercase;
8306 /* Occasionally we fabricate events, perhaps by expanding something
8307 according to function-key-map, or by adding a prefix symbol to a
8308 mouse click in the scroll bar or modeline. In this cases, return
8309 the entire generated key sequence, even if we hit an unbound
8310 prefix or a definition before the end. This means that you will
8311 be able to push back the event properly, and also means that
8312 read-key-sequence will always return a logical unit.
8314 Better ideas? */
8315 for (; t < mock_input; t++)
8317 if (echo_keystrokes)
8318 echo_char (keybuf[t]);
8319 add_command_key (keybuf[t]);
8324 return t;
8327 #if 0 /* This doc string is too long for some compilers.
8328 This commented-out definition serves for DOC. */
8329 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
8330 "Read a sequence of keystrokes and return as a string or vector.\n\
8331 The sequence is sufficient to specify a non-prefix command in the\n\
8332 current local and global maps.\n\
8334 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
8335 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
8336 as a continuation of the previous key.\n\
8338 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
8339 convert the last event to lower case. (Normally any upper case event\n\
8340 is converted to lower case if the original event is undefined and the lower\n\
8341 case equivalent is defined.) A non-nil value is appropriate for reading\n\
8342 a key sequence to be defined.\n\
8344 A C-g typed while in this function is treated like any other character,\n\
8345 and `quit-flag' is not set.\n\
8347 If the key sequence starts with a mouse click, then the sequence is read\n\
8348 using the keymaps of the buffer of the window clicked in, not the buffer\n\
8349 of the selected window as normal.\n\
8350 ""\n\
8351 `read-key-sequence' drops unbound button-down events, since you normally\n\
8352 only care about the click or drag events which follow them. If a drag\n\
8353 or multi-click event is unbound, but the corresponding click event would\n\
8354 be bound, `read-key-sequence' turns the event into a click event at the\n\
8355 drag's starting position. This means that you don't have to distinguish\n\
8356 between click and drag, double, or triple events unless you want to.\n\
8358 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
8359 lines separating windows, and scroll bars with imaginary keys\n\
8360 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
8362 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
8363 function will process a switch-frame event if the user switches frames\n\
8364 before typing anything. If the user switches frames in the middle of a\n\
8365 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
8366 is nil, then the event will be put off until after the current key sequence.\n\
8368 `read-key-sequence' checks `function-key-map' for function key\n\
8369 sequences, where they wouldn't conflict with ordinary bindings. See\n\
8370 `function-key-map' for more details.\n\
8372 The optional fifth argument COMMAND-LOOP, if non-nil, means\n\
8373 that this key sequence is being read by something that will\n\
8374 read commands one after another. It should be nil if the caller\n\
8375 will read just one key sequence.")
8376 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame, command-loop)
8377 #endif
8379 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 5, 0,
8381 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
8382 command_loop)
8383 Lisp_Object prompt, continue_echo, dont_downcase_last;
8384 Lisp_Object can_return_switch_frame, command_loop;
8386 Lisp_Object keybuf[30];
8387 register int i;
8388 struct gcpro gcpro1;
8389 int count = specpdl_ptr - specpdl;
8391 if (!NILP (prompt))
8392 CHECK_STRING (prompt, 0);
8393 QUIT;
8395 specbind (Qinput_method_exit_on_first_char,
8396 (NILP (command_loop) ? Qt : Qnil));
8397 specbind (Qinput_method_use_echo_area,
8398 (NILP (command_loop) ? Qt : Qnil));
8400 bzero (keybuf, sizeof keybuf);
8401 GCPRO1 (keybuf[0]);
8402 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
8404 if (NILP (continue_echo))
8406 this_command_key_count = 0;
8407 this_single_command_key_start = 0;
8410 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
8411 prompt, ! NILP (dont_downcase_last),
8412 ! NILP (can_return_switch_frame), 0);
8414 if (i == -1)
8416 Vquit_flag = Qt;
8417 QUIT;
8419 UNGCPRO;
8420 return unbind_to (count, make_event_array (i, keybuf));
8423 DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
8424 Sread_key_sequence_vector, 1, 5, 0,
8425 "Like `read-key-sequence' but always return a vector.")
8426 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
8427 command_loop)
8428 Lisp_Object prompt, continue_echo, dont_downcase_last;
8429 Lisp_Object can_return_switch_frame, command_loop;
8431 Lisp_Object keybuf[30];
8432 register int i;
8433 struct gcpro gcpro1;
8434 int count = specpdl_ptr - specpdl;
8436 if (!NILP (prompt))
8437 CHECK_STRING (prompt, 0);
8438 QUIT;
8440 specbind (Qinput_method_exit_on_first_char,
8441 (NILP (command_loop) ? Qt : Qnil));
8442 specbind (Qinput_method_use_echo_area,
8443 (NILP (command_loop) ? Qt : Qnil));
8445 bzero (keybuf, sizeof keybuf);
8446 GCPRO1 (keybuf[0]);
8447 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
8449 if (NILP (continue_echo))
8451 this_command_key_count = 0;
8452 this_single_command_key_start = 0;
8455 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
8456 prompt, ! NILP (dont_downcase_last),
8457 ! NILP (can_return_switch_frame), 0);
8459 if (i == -1)
8461 Vquit_flag = Qt;
8462 QUIT;
8464 UNGCPRO;
8465 return unbind_to (count, Fvector (i, keybuf));
8468 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
8469 "Execute CMD as an editor command.\n\
8470 CMD must be a symbol that satisfies the `commandp' predicate.\n\
8471 Optional second arg RECORD-FLAG non-nil\n\
8472 means unconditionally put this command in `command-history'.\n\
8473 Otherwise, that is done only if an arg is read using the minibuffer.\n\
8474 The argument KEYS specifies the value to use instead of (this-command-keys)\n\
8475 when reading the arguments; if it is nil, (this-command-keys) is used.\n\
8476 The argument SPECIAL, if non-nil, means that this command is executing\n\
8477 a special event, so ignore the prefix argument and don't clear it.")
8478 (cmd, record_flag, keys, special)
8479 Lisp_Object cmd, record_flag, keys, special;
8481 register Lisp_Object final;
8482 register Lisp_Object tem;
8483 Lisp_Object prefixarg;
8484 struct backtrace backtrace;
8485 extern int debug_on_next_call;
8487 debug_on_next_call = 0;
8489 if (NILP (special))
8491 prefixarg = current_kboard->Vprefix_arg;
8492 Vcurrent_prefix_arg = prefixarg;
8493 current_kboard->Vprefix_arg = Qnil;
8495 else
8496 prefixarg = Qnil;
8498 if (SYMBOLP (cmd))
8500 tem = Fget (cmd, Qdisabled);
8501 if (!NILP (tem) && !NILP (Vrun_hooks))
8503 tem = Fsymbol_value (Qdisabled_command_hook);
8504 if (!NILP (tem))
8505 return call1 (Vrun_hooks, Qdisabled_command_hook);
8509 while (1)
8511 final = Findirect_function (cmd);
8513 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
8515 struct gcpro gcpro1, gcpro2;
8517 GCPRO2 (cmd, prefixarg);
8518 do_autoload (final, cmd);
8519 UNGCPRO;
8521 else
8522 break;
8525 if (STRINGP (final) || VECTORP (final))
8527 /* If requested, place the macro in the command history. For
8528 other sorts of commands, call-interactively takes care of
8529 this. */
8530 if (!NILP (record_flag))
8532 Vcommand_history
8533 = Fcons (Fcons (Qexecute_kbd_macro,
8534 Fcons (final, Fcons (prefixarg, Qnil))),
8535 Vcommand_history);
8537 /* Don't keep command history around forever. */
8538 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
8540 tem = Fnthcdr (Vhistory_length, Vcommand_history);
8541 if (CONSP (tem))
8542 XCONS (tem)->cdr = Qnil;
8546 return Fexecute_kbd_macro (final, prefixarg);
8549 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
8551 backtrace.next = backtrace_list;
8552 backtrace_list = &backtrace;
8553 backtrace.function = &Qcall_interactively;
8554 backtrace.args = &cmd;
8555 backtrace.nargs = 1;
8556 backtrace.evalargs = 0;
8558 tem = Fcall_interactively (cmd, record_flag, keys);
8560 backtrace_list = backtrace.next;
8561 return tem;
8563 return Qnil;
8566 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
8567 1, 1, "P",
8568 "Read function name, then read its arguments and call it.")
8569 (prefixarg)
8570 Lisp_Object prefixarg;
8572 Lisp_Object function;
8573 char buf[40];
8574 Lisp_Object saved_keys;
8575 Lisp_Object bindings, value;
8576 struct gcpro gcpro1, gcpro2;
8578 saved_keys = Fvector (this_command_key_count,
8579 XVECTOR (this_command_keys)->contents);
8580 buf[0] = 0;
8581 GCPRO2 (saved_keys, prefixarg);
8583 if (EQ (prefixarg, Qminus))
8584 strcpy (buf, "- ");
8585 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
8586 strcpy (buf, "C-u ");
8587 else if (CONSP (prefixarg) && INTEGERP (XCONS (prefixarg)->car))
8589 if (sizeof (int) == sizeof (EMACS_INT))
8590 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
8591 else if (sizeof (long) == sizeof (EMACS_INT))
8592 sprintf (buf, "%ld ", (long) XINT (XCONS (prefixarg)->car));
8593 else
8594 abort ();
8596 else if (INTEGERP (prefixarg))
8598 if (sizeof (int) == sizeof (EMACS_INT))
8599 sprintf (buf, "%d ", XINT (prefixarg));
8600 else if (sizeof (long) == sizeof (EMACS_INT))
8601 sprintf (buf, "%ld ", (long) XINT (prefixarg));
8602 else
8603 abort ();
8606 /* This isn't strictly correct if execute-extended-command
8607 is bound to anything else. Perhaps it should use
8608 this_command_keys? */
8609 strcat (buf, "M-x ");
8611 /* Prompt with buf, and then read a string, completing from and
8612 restricting to the set of all defined commands. Don't provide
8613 any initial input. Save the command read on the extended-command
8614 history list. */
8615 function = Fcompleting_read (build_string (buf),
8616 Vobarray, Qcommandp,
8617 Qt, Qnil, Qextended_command_history, Qnil,
8618 Qnil);
8620 if (STRINGP (function) && XSTRING (function)->size == 0)
8621 error ("No command name given");
8623 /* Set this_command_keys to the concatenation of saved_keys and
8624 function, followed by a RET. */
8626 struct Lisp_String *str;
8627 Lisp_Object *keys;
8628 int i;
8630 this_command_key_count = 0;
8631 this_single_command_key_start = 0;
8633 keys = XVECTOR (saved_keys)->contents;
8634 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
8635 add_command_key (keys[i]);
8637 str = XSTRING (function);
8638 for (i = 0; i < str->size; i++)
8639 add_command_key (Faref (function, make_number (i)));
8641 add_command_key (make_number ('\015'));
8644 UNGCPRO;
8646 function = Fintern (function, Qnil);
8647 current_kboard->Vprefix_arg = prefixarg;
8648 Vthis_command = function;
8649 real_this_command = function;
8651 /* If enabled, show which key runs this command. */
8652 if (!NILP (Vsuggest_key_bindings)
8653 && NILP (Vexecuting_macro)
8654 && SYMBOLP (function))
8655 bindings = Fwhere_is_internal (function, Voverriding_local_map,
8656 Qt, Qnil);
8657 else
8658 bindings = Qnil;
8660 value = Qnil;
8661 GCPRO2 (bindings, value);
8662 value = Fcommand_execute (function, Qt, Qnil, Qnil);
8664 /* If the command has a key binding, print it now. */
8665 if (!NILP (bindings)
8666 && ! (VECTORP (bindings) && EQ (Faref (bindings, make_number (0)),
8667 Qmouse_movement)))
8669 /* But first wait, and skip the message if there is input. */
8670 int delay_time;
8671 if (!NILP (echo_area_buffer[0]))
8672 /* This command displayed something in the echo area;
8673 so wait a few seconds, then display our suggestion message. */
8674 delay_time = (NUMBERP (Vsuggest_key_bindings)
8675 ? XINT (Vsuggest_key_bindings) : 2);
8676 else
8677 /* This command left the echo area empty,
8678 so display our message immediately. */
8679 delay_time = 0;
8681 if (!NILP (Fsit_for (make_number (delay_time), Qnil, Qnil))
8682 && ! CONSP (Vunread_command_events))
8684 Lisp_Object binding;
8685 char *newmessage;
8686 int message_p = push_message ();
8688 binding = Fkey_description (bindings);
8690 newmessage
8691 = (char *) alloca (XSYMBOL (function)->name->size
8692 + STRING_BYTES (XSTRING (binding))
8693 + 100);
8694 sprintf (newmessage, "You can run the command `%s' with %s",
8695 XSYMBOL (function)->name->data,
8696 XSTRING (binding)->data);
8697 message2_nolog (newmessage,
8698 strlen (newmessage),
8699 STRING_MULTIBYTE (binding));
8700 if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
8701 ? Vsuggest_key_bindings : make_number (2)),
8702 Qnil, Qnil))
8703 && message_p)
8704 restore_message ();
8706 pop_message ();
8710 RETURN_UNGCPRO (value);
8713 /* Find the set of keymaps now active.
8714 Store into *MAPS_P a vector holding the various maps
8715 and return the number of them. The vector was malloc'd
8716 and the caller should free it. */
8719 current_active_maps (maps_p)
8720 Lisp_Object **maps_p;
8722 Lisp_Object *tmaps, *maps;
8723 int nmaps;
8725 /* Should overriding-terminal-local-map and overriding-local-map apply? */
8726 if (!NILP (Voverriding_local_map_menu_flag))
8728 /* Yes, use them (if non-nil) as well as the global map. */
8729 maps = (Lisp_Object *) xmalloc (3 * sizeof (maps[0]));
8730 nmaps = 0;
8731 if (!NILP (current_kboard->Voverriding_terminal_local_map))
8732 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
8733 if (!NILP (Voverriding_local_map))
8734 maps[nmaps++] = Voverriding_local_map;
8736 else
8738 /* No, so use major and minor mode keymaps. */
8739 nmaps = current_minor_maps (NULL, &tmaps);
8740 maps = (Lisp_Object *) xmalloc ((nmaps + 2) * sizeof (maps[0]));
8741 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
8742 #ifdef USE_TEXT_PROPERTIES
8743 maps[nmaps++] = get_local_map (PT, current_buffer);
8744 #else
8745 maps[nmaps++] = current_buffer->keymap;
8746 #endif
8748 maps[nmaps++] = current_global_map;
8750 *maps_p = maps;
8751 return nmaps;
8754 /* Return nonzero if input events are pending. */
8757 detect_input_pending ()
8759 if (!input_pending)
8760 get_input_pending (&input_pending, 0);
8762 return input_pending;
8765 /* Return nonzero if input events are pending, and run any pending timers. */
8768 detect_input_pending_run_timers (do_display)
8769 int do_display;
8771 int old_timers_run = timers_run;
8773 if (!input_pending)
8774 get_input_pending (&input_pending, 1);
8776 if (old_timers_run != timers_run && do_display)
8778 redisplay_preserve_echo_area ();
8779 /* The following fixes a bug when using lazy-lock with
8780 lazy-lock-defer-on-the-fly set to t, i.e. when fontifying
8781 from an idle timer function. The symptom of the bug is that
8782 the cursor sometimes doesn't become visible until the next X
8783 event is processed. --gerd. */
8784 if (rif)
8785 rif->flush_display (NULL);
8788 return input_pending;
8791 /* This is called in some cases before a possible quit.
8792 It cases the next call to detect_input_pending to recompute input_pending.
8793 So calling this function unnecessarily can't do any harm. */
8795 void
8796 clear_input_pending ()
8798 input_pending = 0;
8801 /* Return nonzero if there are pending requeued events.
8802 This isn't used yet. The hope is to make wait_reading_process_input
8803 call it, and return return if it runs Lisp code that unreads something.
8804 The problem is, kbd_buffer_get_event needs to be fixed to know what
8805 to do in that case. It isn't trivial. */
8808 requeued_events_pending_p ()
8810 return (!NILP (Vunread_command_events) || unread_command_char != -1);
8814 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
8815 "T if command input is currently available with no waiting.\n\
8816 Actually, the value is nil only if we can be sure that no input is available.")
8819 if (!NILP (Vunread_command_events) || unread_command_char != -1)
8820 return (Qt);
8822 get_input_pending (&input_pending, 1);
8823 return input_pending > 0 ? Qt : Qnil;
8826 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
8827 "Return vector of last 100 events, not counting those from keyboard macros.")
8830 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
8831 Lisp_Object val;
8833 if (total_keys < NUM_RECENT_KEYS)
8834 return Fvector (total_keys, keys);
8835 else
8837 val = Fvector (NUM_RECENT_KEYS, keys);
8838 bcopy (keys + recent_keys_index,
8839 XVECTOR (val)->contents,
8840 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
8841 bcopy (keys,
8842 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
8843 recent_keys_index * sizeof (Lisp_Object));
8844 return val;
8848 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
8849 "Return the key sequence that invoked this command.\n\
8850 The value is a string or a vector.")
8853 return make_event_array (this_command_key_count,
8854 XVECTOR (this_command_keys)->contents);
8857 DEFUN ("this-command-keys-vector", Fthis_command_keys_vector, Sthis_command_keys_vector, 0, 0, 0,
8858 "Return the key sequence that invoked this command, as a vector.")
8861 return Fvector (this_command_key_count,
8862 XVECTOR (this_command_keys)->contents);
8865 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
8866 Sthis_single_command_keys, 0, 0, 0,
8867 "Return the key sequence that invoked this command.\n\
8868 Unlike `this-command-keys', this function's value\n\
8869 does not include prefix arguments.\n\
8870 The value is always a vector.")
8873 return Fvector (this_command_key_count
8874 - this_single_command_key_start,
8875 (XVECTOR (this_command_keys)->contents
8876 + this_single_command_key_start));
8879 DEFUN ("this-single-command-raw-keys", Fthis_single_command_raw_keys,
8880 Sthis_single_command_raw_keys, 0, 0, 0,
8881 "Return the raw events that were read for this command.\n\
8882 Unlike `this-single-command-keys', this function's value\n\
8883 shows the events before all translations (except for input methods).\n\
8884 The value is always a vector.")
8887 return Fvector (raw_keybuf_count,
8888 (XVECTOR (raw_keybuf)->contents));
8891 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
8892 Sreset_this_command_lengths, 0, 0, 0,
8893 "Used for complicated reasons in `universal-argument-other-key'.\n\
8895 `universal-argument-other-key' rereads the event just typed.\n\
8896 It then gets translated through `function-key-map'.\n\
8897 The translated event gets included in the echo area and in\n\
8898 the value of `this-command-keys' in addition to the raw original event.\n\
8899 That is not right.\n\
8901 Calling this function directs the translated event to replace\n\
8902 the original event, so that only one version of the event actually\n\
8903 appears in the echo area and in the value of `this-command-keys.'.")
8906 before_command_restore_flag = 1;
8907 before_command_key_count_1 = before_command_key_count;
8908 before_command_echo_length_1 = before_command_echo_length;
8909 return Qnil;
8912 DEFUN ("clear-this-command-keys", Fclear_this_command_keys,
8913 Sclear_this_command_keys, 0, 0, 0,
8914 "Clear out the vector that `this-command-keys' returns.")
8917 this_command_key_count = 0;
8918 return Qnil;
8921 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
8922 "Return the current depth in recursive edits.")
8925 Lisp_Object temp;
8926 XSETFASTINT (temp, command_loop_level + minibuf_level);
8927 return temp;
8930 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
8931 "FOpen dribble file: ",
8932 "Start writing all keyboard characters to a dribble file called FILE.\n\
8933 If FILE is nil, close any open dribble file.")
8934 (file)
8935 Lisp_Object file;
8937 if (dribble)
8939 fclose (dribble);
8940 dribble = 0;
8942 if (!NILP (file))
8944 file = Fexpand_file_name (file, Qnil);
8945 dribble = fopen (XSTRING (file)->data, "w");
8946 if (dribble == 0)
8947 report_file_error ("Opening dribble", Fcons (file, Qnil));
8949 return Qnil;
8952 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
8953 "Discard the contents of the terminal input buffer.\n\
8954 Also cancel any kbd macro being defined.")
8957 current_kboard->defining_kbd_macro = Qnil;
8958 update_mode_lines++;
8960 Vunread_command_events = Qnil;
8961 unread_command_char = -1;
8963 discard_tty_input ();
8965 kbd_fetch_ptr = kbd_store_ptr;
8966 Ffillarray (kbd_buffer_frame_or_window, Qnil);
8967 input_pending = 0;
8969 return Qnil;
8972 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
8973 "Stop Emacs and return to superior process. You can resume later.\n\
8974 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
8975 control, run a subshell instead.\n\n\
8976 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
8977 to be read as terminal input by Emacs's parent, after suspension.\n\
8979 Before suspending, run the normal hook `suspend-hook'.\n\
8980 After resumption run the normal hook `suspend-resume-hook'.\n\
8982 Some operating systems cannot stop the Emacs process and resume it later.\n\
8983 On such systems, Emacs starts a subshell instead of suspending.")
8984 (stuffstring)
8985 Lisp_Object stuffstring;
8987 int count = specpdl_ptr - specpdl;
8988 int old_height, old_width;
8989 int width, height;
8990 struct gcpro gcpro1;
8992 if (!NILP (stuffstring))
8993 CHECK_STRING (stuffstring, 0);
8995 /* Run the functions in suspend-hook. */
8996 if (!NILP (Vrun_hooks))
8997 call1 (Vrun_hooks, intern ("suspend-hook"));
8999 GCPRO1 (stuffstring);
9000 get_frame_size (&old_width, &old_height);
9001 reset_sys_modes ();
9002 /* sys_suspend can get an error if it tries to fork a subshell
9003 and the system resources aren't available for that. */
9004 record_unwind_protect ((Lisp_Object (*) P_ ((Lisp_Object))) init_sys_modes,
9005 Qnil);
9006 stuff_buffered_input (stuffstring);
9007 if (cannot_suspend)
9008 sys_subshell ();
9009 else
9010 sys_suspend ();
9011 unbind_to (count, Qnil);
9013 /* Check if terminal/window size has changed.
9014 Note that this is not useful when we are running directly
9015 with a window system; but suspend should be disabled in that case. */
9016 get_frame_size (&width, &height);
9017 if (width != old_width || height != old_height)
9018 change_frame_size (SELECTED_FRAME (), height, width, 0, 0, 0);
9020 /* Run suspend-resume-hook. */
9021 if (!NILP (Vrun_hooks))
9022 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
9024 UNGCPRO;
9025 return Qnil;
9028 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
9029 Then in any case stuff anything Emacs has read ahead and not used. */
9031 void
9032 stuff_buffered_input (stuffstring)
9033 Lisp_Object stuffstring;
9035 /* stuff_char works only in BSD, versions 4.2 and up. */
9036 #ifdef BSD_SYSTEM
9037 #ifndef BSD4_1
9038 register unsigned char *p;
9040 if (STRINGP (stuffstring))
9042 register int count;
9044 p = XSTRING (stuffstring)->data;
9045 count = STRING_BYTES (XSTRING (stuffstring));
9046 while (count-- > 0)
9047 stuff_char (*p++);
9048 stuff_char ('\n');
9050 /* Anything we have read ahead, put back for the shell to read. */
9051 /* ?? What should this do when we have multiple keyboards??
9052 Should we ignore anything that was typed in at the "wrong" kboard? */
9053 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
9055 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
9056 kbd_fetch_ptr = kbd_buffer;
9057 if (kbd_fetch_ptr->kind == ascii_keystroke)
9058 stuff_char (kbd_fetch_ptr->code);
9059 kbd_fetch_ptr->kind = no_event;
9060 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
9061 - kbd_buffer]
9062 = Qnil);
9064 input_pending = 0;
9065 #endif
9066 #endif /* BSD_SYSTEM and not BSD4_1 */
9069 void
9070 set_waiting_for_input (time_to_clear)
9071 EMACS_TIME *time_to_clear;
9073 input_available_clear_time = time_to_clear;
9075 /* Tell interrupt_signal to throw back to read_char, */
9076 waiting_for_input = 1;
9078 /* If interrupt_signal was called before and buffered a C-g,
9079 make it run again now, to avoid timing error. */
9080 if (!NILP (Vquit_flag))
9081 quit_throw_to_read_char ();
9084 void
9085 clear_waiting_for_input ()
9087 /* Tell interrupt_signal not to throw back to read_char, */
9088 waiting_for_input = 0;
9089 input_available_clear_time = 0;
9092 /* This routine is called at interrupt level in response to C-G.
9093 If interrupt_input, this is the handler for SIGINT.
9094 Otherwise, it is called from kbd_buffer_store_event,
9095 in handling SIGIO or SIGTINT.
9097 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
9098 immediately throw back to read_char.
9100 Otherwise it sets the Lisp variable quit-flag not-nil.
9101 This causes eval to throw, when it gets a chance.
9102 If quit-flag is already non-nil, it stops the job right away. */
9104 SIGTYPE
9105 interrupt_signal (signalnum) /* If we don't have an argument, */
9106 int signalnum; /* some compilers complain in signal calls. */
9108 char c;
9109 /* Must preserve main program's value of errno. */
9110 int old_errno = errno;
9111 struct frame *sf = SELECTED_FRAME ();
9113 #if defined (USG) && !defined (POSIX_SIGNALS)
9114 if (!read_socket_hook && NILP (Vwindow_system))
9116 /* USG systems forget handlers when they are used;
9117 must reestablish each time */
9118 signal (SIGINT, interrupt_signal);
9119 signal (SIGQUIT, interrupt_signal);
9121 #endif /* USG */
9123 cancel_echoing ();
9125 if (!NILP (Vquit_flag)
9126 && (FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf)))
9128 /* If SIGINT isn't blocked, don't let us be interrupted by
9129 another SIGINT, it might be harmful due to non-reentrancy
9130 in I/O functions. */
9131 sigblock (sigmask (SIGINT));
9133 fflush (stdout);
9134 reset_sys_modes ();
9136 #ifdef SIGTSTP /* Support possible in later USG versions */
9138 * On systems which can suspend the current process and return to the original
9139 * shell, this command causes the user to end up back at the shell.
9140 * The "Auto-save" and "Abort" questions are not asked until
9141 * the user elects to return to emacs, at which point he can save the current
9142 * job and either dump core or continue.
9144 sys_suspend ();
9145 #else
9146 #ifdef VMS
9147 if (sys_suspend () == -1)
9149 printf ("Not running as a subprocess;\n");
9150 printf ("you can continue or abort.\n");
9152 #else /* not VMS */
9153 /* Perhaps should really fork an inferior shell?
9154 But that would not provide any way to get back
9155 to the original shell, ever. */
9156 printf ("No support for stopping a process on this operating system;\n");
9157 printf ("you can continue or abort.\n");
9158 #endif /* not VMS */
9159 #endif /* not SIGTSTP */
9160 #ifdef MSDOS
9161 /* We must remain inside the screen area when the internal terminal
9162 is used. Note that [Enter] is not echoed by dos. */
9163 cursor_to (0, 0);
9164 #endif
9165 /* It doesn't work to autosave while GC is in progress;
9166 the code used for auto-saving doesn't cope with the mark bit. */
9167 if (!gc_in_progress)
9169 printf ("Auto-save? (y or n) ");
9170 fflush (stdout);
9171 if (((c = getchar ()) & ~040) == 'Y')
9173 Fdo_auto_save (Qt, Qnil);
9174 #ifdef MSDOS
9175 printf ("\r\nAuto-save done");
9176 #else /* not MSDOS */
9177 printf ("Auto-save done\n");
9178 #endif /* not MSDOS */
9180 while (c != '\n') c = getchar ();
9182 else
9184 /* During GC, it must be safe to reenable quitting again. */
9185 Vinhibit_quit = Qnil;
9186 #ifdef MSDOS
9187 printf ("\r\n");
9188 #endif /* not MSDOS */
9189 printf ("Garbage collection in progress; cannot auto-save now\r\n");
9190 printf ("but will instead do a real quit after garbage collection ends\r\n");
9191 fflush (stdout);
9194 #ifdef MSDOS
9195 printf ("\r\nAbort? (y or n) ");
9196 #else /* not MSDOS */
9197 #ifdef VMS
9198 printf ("Abort (and enter debugger)? (y or n) ");
9199 #else /* not VMS */
9200 printf ("Abort (and dump core)? (y or n) ");
9201 #endif /* not VMS */
9202 #endif /* not MSDOS */
9203 fflush (stdout);
9204 if (((c = getchar ()) & ~040) == 'Y')
9205 abort ();
9206 while (c != '\n') c = getchar ();
9207 #ifdef MSDOS
9208 printf ("\r\nContinuing...\r\n");
9209 #else /* not MSDOS */
9210 printf ("Continuing...\n");
9211 #endif /* not MSDOS */
9212 fflush (stdout);
9213 init_sys_modes ();
9214 sigfree ();
9216 else
9218 /* If executing a function that wants to be interrupted out of
9219 and the user has not deferred quitting by binding `inhibit-quit'
9220 then quit right away. */
9221 if (immediate_quit && NILP (Vinhibit_quit))
9223 struct gl_state_s saved;
9224 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9226 immediate_quit = 0;
9227 sigfree ();
9228 saved = gl_state;
9229 GCPRO4 (saved.object, saved.global_code,
9230 saved.current_syntax_table, saved.old_prop);
9231 Fsignal (Qquit, Qnil);
9232 gl_state = saved;
9233 UNGCPRO;
9235 else
9236 /* Else request quit when it's safe */
9237 Vquit_flag = Qt;
9240 if (waiting_for_input && !echoing)
9241 quit_throw_to_read_char ();
9243 errno = old_errno;
9246 /* Handle a C-g by making read_char return C-g. */
9248 void
9249 quit_throw_to_read_char ()
9251 sigfree ();
9252 /* Prevent another signal from doing this before we finish. */
9253 clear_waiting_for_input ();
9254 input_pending = 0;
9256 Vunread_command_events = Qnil;
9257 unread_command_char = -1;
9259 #if 0 /* Currently, sit_for is called from read_char without turning
9260 off polling. And that can call set_waiting_for_input.
9261 It seems to be harmless. */
9262 #ifdef POLL_FOR_INPUT
9263 /* May be > 1 if in recursive minibuffer. */
9264 if (poll_suppress_count == 0)
9265 abort ();
9266 #endif
9267 #endif
9268 if (FRAMEP (internal_last_event_frame)
9269 && !EQ (internal_last_event_frame, selected_frame))
9270 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
9271 Qnil, 0);
9273 _longjmp (getcjmp, 1);
9276 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
9277 "Set mode of reading keyboard input.\n\
9278 First arg INTERRUPT non-nil means use input interrupts;\n\
9279 nil means use CBREAK mode.\n\
9280 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
9281 (no effect except in CBREAK mode).\n\
9282 Third arg META t means accept 8-bit input (for a Meta key).\n\
9283 META nil means ignore the top bit, on the assumption it is parity.\n\
9284 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
9285 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
9286 See also `current-input-mode'.")
9287 (interrupt, flow, meta, quit)
9288 Lisp_Object interrupt, flow, meta, quit;
9290 if (!NILP (quit)
9291 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
9292 error ("set-input-mode: QUIT must be an ASCII character");
9294 #ifdef POLL_FOR_INPUT
9295 stop_polling ();
9296 #endif
9298 #ifndef DOS_NT
9299 /* this causes startup screen to be restored and messes with the mouse */
9300 reset_sys_modes ();
9301 #endif
9303 #ifdef SIGIO
9304 /* Note SIGIO has been undef'd if FIONREAD is missing. */
9305 if (read_socket_hook)
9307 /* When using X, don't give the user a real choice,
9308 because we haven't implemented the mechanisms to support it. */
9309 #ifdef NO_SOCK_SIGIO
9310 interrupt_input = 0;
9311 #else /* not NO_SOCK_SIGIO */
9312 interrupt_input = 1;
9313 #endif /* NO_SOCK_SIGIO */
9315 else
9316 interrupt_input = !NILP (interrupt);
9317 #else /* not SIGIO */
9318 interrupt_input = 0;
9319 #endif /* not SIGIO */
9321 /* Our VMS input only works by interrupts, as of now. */
9322 #ifdef VMS
9323 interrupt_input = 1;
9324 #endif
9326 flow_control = !NILP (flow);
9327 if (NILP (meta))
9328 meta_key = 0;
9329 else if (EQ (meta, Qt))
9330 meta_key = 1;
9331 else
9332 meta_key = 2;
9333 if (!NILP (quit))
9334 /* Don't let this value be out of range. */
9335 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
9337 #ifndef DOS_NT
9338 init_sys_modes ();
9339 #endif
9341 #ifdef POLL_FOR_INPUT
9342 poll_suppress_count = 1;
9343 start_polling ();
9344 #endif
9345 return Qnil;
9348 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
9349 "Return information about the way Emacs currently reads keyboard input.\n\
9350 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
9351 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
9352 nil, Emacs is using CBREAK mode.\n\
9353 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
9354 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
9355 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
9356 META nil means ignoring the top bit, on the assumption it is parity.\n\
9357 META is neither t nor nil if accepting 8-bit input and using\n\
9358 all 8 bits as the character code.\n\
9359 QUIT is the character Emacs currently uses to quit.\n\
9360 The elements of this list correspond to the arguments of\n\
9361 `set-input-mode'.")
9364 Lisp_Object val[4];
9366 val[0] = interrupt_input ? Qt : Qnil;
9367 val[1] = flow_control ? Qt : Qnil;
9368 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
9369 XSETFASTINT (val[3], quit_char);
9371 return Flist (sizeof (val) / sizeof (val[0]), val);
9376 * Set up a new kboard object with reasonable initial values.
9378 void
9379 init_kboard (kb)
9380 KBOARD *kb;
9382 kb->Voverriding_terminal_local_map = Qnil;
9383 kb->Vlast_command = Qnil;
9384 kb->Vreal_last_command = Qnil;
9385 kb->Vprefix_arg = Qnil;
9386 kb->Vlast_prefix_arg = Qnil;
9387 kb->kbd_queue = Qnil;
9388 kb->kbd_queue_has_data = 0;
9389 kb->immediate_echo = 0;
9390 kb->echoptr = kb->echobuf;
9391 kb->echo_after_prompt = -1;
9392 kb->kbd_macro_buffer = 0;
9393 kb->kbd_macro_bufsize = 0;
9394 kb->defining_kbd_macro = Qnil;
9395 kb->Vlast_kbd_macro = Qnil;
9396 kb->reference_count = 0;
9397 kb->Vsystem_key_alist = Qnil;
9398 kb->system_key_syms = Qnil;
9399 kb->Vdefault_minibuffer_frame = Qnil;
9403 * Destroy the contents of a kboard object, but not the object itself.
9404 * We use this just before deleting it, or if we're going to initialize
9405 * it a second time.
9407 static void
9408 wipe_kboard (kb)
9409 KBOARD *kb;
9411 if (kb->kbd_macro_buffer)
9412 xfree (kb->kbd_macro_buffer);
9415 #ifdef MULTI_KBOARD
9416 void
9417 delete_kboard (kb)
9418 KBOARD *kb;
9420 KBOARD **kbp;
9421 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
9422 if (*kbp == NULL)
9423 abort ();
9424 *kbp = kb->next_kboard;
9425 wipe_kboard (kb);
9426 xfree (kb);
9428 #endif
9430 void
9431 init_keyboard ()
9433 /* This is correct before outermost invocation of the editor loop */
9434 command_loop_level = -1;
9435 immediate_quit = 0;
9436 quit_char = Ctl ('g');
9437 Vunread_command_events = Qnil;
9438 unread_command_char = -1;
9439 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
9440 total_keys = 0;
9441 recent_keys_index = 0;
9442 kbd_fetch_ptr = kbd_buffer;
9443 kbd_store_ptr = kbd_buffer;
9444 kbd_buffer_frame_or_window
9445 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
9446 #ifdef HAVE_MOUSE
9447 do_mouse_tracking = Qnil;
9448 #endif
9449 input_pending = 0;
9451 /* This means that command_loop_1 won't try to select anything the first
9452 time through. */
9453 internal_last_event_frame = Qnil;
9454 Vlast_event_frame = internal_last_event_frame;
9456 #ifdef MULTI_KBOARD
9457 current_kboard = initial_kboard;
9458 #endif
9459 wipe_kboard (current_kboard);
9460 init_kboard (current_kboard);
9462 if (initialized)
9463 Ffillarray (kbd_buffer_frame_or_window, Qnil);
9465 kbd_buffer_frame_or_window
9466 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
9467 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
9469 signal (SIGINT, interrupt_signal);
9470 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
9471 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
9472 SIGQUIT and we can't tell which one it will give us. */
9473 signal (SIGQUIT, interrupt_signal);
9474 #endif /* HAVE_TERMIO */
9476 /* Note SIGIO has been undef'd if FIONREAD is missing. */
9477 #ifdef SIGIO
9478 if (!noninteractive)
9479 signal (SIGIO, input_available_signal);
9480 #endif /* SIGIO */
9482 /* Use interrupt input by default, if it works and noninterrupt input
9483 has deficiencies. */
9485 #ifdef INTERRUPT_INPUT
9486 interrupt_input = 1;
9487 #else
9488 interrupt_input = 0;
9489 #endif
9491 /* Our VMS input only works by interrupts, as of now. */
9492 #ifdef VMS
9493 interrupt_input = 1;
9494 #endif
9496 sigfree ();
9497 dribble = 0;
9499 if (keyboard_init_hook)
9500 (*keyboard_init_hook) ();
9502 #ifdef POLL_FOR_INPUT
9503 poll_suppress_count = 1;
9504 start_polling ();
9505 #endif
9508 /* This type's only use is in syms_of_keyboard, to initialize the
9509 event header symbols and put properties on them. */
9510 struct event_head {
9511 Lisp_Object *var;
9512 char *name;
9513 Lisp_Object *kind;
9516 struct event_head head_table[] = {
9517 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
9518 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
9519 &Qswitch_frame, "switch-frame", &Qswitch_frame,
9520 &Qdelete_frame, "delete-frame", &Qdelete_frame,
9521 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
9522 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
9525 void
9526 syms_of_keyboard ()
9528 /* Tool-bars. */
9529 QCimage = intern (":image");
9530 staticpro (&QCimage);
9532 staticpro (&Qhelp_echo);
9533 Qhelp_echo = intern ("help-echo");
9535 staticpro (&item_properties);
9536 item_properties = Qnil;
9538 staticpro (&tool_bar_item_properties);
9539 tool_bar_item_properties = Qnil;
9540 staticpro (&tool_bar_items_vector);
9541 tool_bar_items_vector = Qnil;
9543 staticpro (&real_this_command);
9544 real_this_command = Qnil;
9546 Qtimer_event_handler = intern ("timer-event-handler");
9547 staticpro (&Qtimer_event_handler);
9549 Qdisabled_command_hook = intern ("disabled-command-hook");
9550 staticpro (&Qdisabled_command_hook);
9552 Qself_insert_command = intern ("self-insert-command");
9553 staticpro (&Qself_insert_command);
9555 Qforward_char = intern ("forward-char");
9556 staticpro (&Qforward_char);
9558 Qbackward_char = intern ("backward-char");
9559 staticpro (&Qbackward_char);
9561 Qdisabled = intern ("disabled");
9562 staticpro (&Qdisabled);
9564 Qundefined = intern ("undefined");
9565 staticpro (&Qundefined);
9567 Qpre_command_hook = intern ("pre-command-hook");
9568 staticpro (&Qpre_command_hook);
9570 Qpost_command_hook = intern ("post-command-hook");
9571 staticpro (&Qpost_command_hook);
9573 Qpost_command_idle_hook = intern ("post-command-idle-hook");
9574 staticpro (&Qpost_command_idle_hook);
9576 Qdeferred_action_function = intern ("deferred-action-function");
9577 staticpro (&Qdeferred_action_function);
9579 Qcommand_hook_internal = intern ("command-hook-internal");
9580 staticpro (&Qcommand_hook_internal);
9582 Qfunction_key = intern ("function-key");
9583 staticpro (&Qfunction_key);
9584 Qmouse_click = intern ("mouse-click");
9585 staticpro (&Qmouse_click);
9586 #ifdef WINDOWSNT
9587 Qmouse_wheel = intern ("mouse-wheel");
9588 staticpro (&Qmouse_wheel);
9589 Qlanguage_change = intern ("language-change");
9590 staticpro (&Qlanguage_change);
9591 #endif
9592 Qdrag_n_drop = intern ("drag-n-drop");
9593 staticpro (&Qdrag_n_drop);
9595 Qusr1_signal = intern ("usr1-signal");
9596 staticpro (&Qusr1_signal);
9597 Qusr2_signal = intern ("usr2-signal");
9598 staticpro (&Qusr2_signal);
9600 Qmenu_enable = intern ("menu-enable");
9601 staticpro (&Qmenu_enable);
9602 Qmenu_alias = intern ("menu-alias");
9603 staticpro (&Qmenu_alias);
9604 QCenable = intern (":enable");
9605 staticpro (&QCenable);
9606 QCvisible = intern (":visible");
9607 staticpro (&QCvisible);
9608 QChelp = intern (":help");
9609 staticpro (&QChelp);
9610 QCfilter = intern (":filter");
9611 staticpro (&QCfilter);
9612 QCbutton = intern (":button");
9613 staticpro (&QCbutton);
9614 QCkeys = intern (":keys");
9615 staticpro (&QCkeys);
9616 QCkey_sequence = intern (":key-sequence");
9617 staticpro (&QCkey_sequence);
9618 QCtoggle = intern (":toggle");
9619 staticpro (&QCtoggle);
9620 QCradio = intern (":radio");
9621 staticpro (&QCradio);
9623 Qmode_line = intern ("mode-line");
9624 staticpro (&Qmode_line);
9625 Qvertical_line = intern ("vertical-line");
9626 staticpro (&Qvertical_line);
9627 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
9628 staticpro (&Qvertical_scroll_bar);
9629 Qmenu_bar = intern ("menu-bar");
9630 staticpro (&Qmenu_bar);
9632 Qabove_handle = intern ("above-handle");
9633 staticpro (&Qabove_handle);
9634 Qhandle = intern ("handle");
9635 staticpro (&Qhandle);
9636 Qbelow_handle = intern ("below-handle");
9637 staticpro (&Qbelow_handle);
9638 Qup = intern ("up");
9639 staticpro (&Qup);
9640 Qdown = intern ("down");
9641 staticpro (&Qdown);
9642 Qtop = intern ("top");
9643 staticpro (&Qtop);
9644 Qbottom = intern ("bottom");
9645 staticpro (&Qbottom);
9646 Qend_scroll = intern ("end-scroll");
9647 staticpro (&Qend_scroll);
9649 Qevent_kind = intern ("event-kind");
9650 staticpro (&Qevent_kind);
9651 Qevent_symbol_elements = intern ("event-symbol-elements");
9652 staticpro (&Qevent_symbol_elements);
9653 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
9654 staticpro (&Qevent_symbol_element_mask);
9655 Qmodifier_cache = intern ("modifier-cache");
9656 staticpro (&Qmodifier_cache);
9658 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
9659 staticpro (&Qrecompute_lucid_menubar);
9660 Qactivate_menubar_hook = intern ("activate-menubar-hook");
9661 staticpro (&Qactivate_menubar_hook);
9663 Qpolling_period = intern ("polling-period");
9664 staticpro (&Qpolling_period);
9666 Qinput_method_function = intern ("input-method-function");
9667 staticpro (&Qinput_method_function);
9669 Qinput_method_exit_on_first_char = intern ("input-method-exit-on-first-char");
9670 staticpro (&Qinput_method_exit_on_first_char);
9671 Qinput_method_use_echo_area = intern ("input-method-use-echo-area");
9672 staticpro (&Qinput_method_use_echo_area);
9674 Fset (Qinput_method_exit_on_first_char, Qnil);
9675 Fset (Qinput_method_use_echo_area, Qnil);
9678 struct event_head *p;
9680 for (p = head_table;
9681 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
9682 p++)
9684 *p->var = intern (p->name);
9685 staticpro (p->var);
9686 Fput (*p->var, Qevent_kind, *p->kind);
9687 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
9691 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
9692 staticpro (&button_down_location);
9695 int i;
9696 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
9698 modifier_symbols = Fmake_vector (make_number (len), Qnil);
9699 for (i = 0; i < len; i++)
9700 if (modifier_names[i])
9701 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
9702 staticpro (&modifier_symbols);
9705 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
9706 staticpro (&recent_keys);
9708 this_command_keys = Fmake_vector (make_number (40), Qnil);
9709 staticpro (&this_command_keys);
9711 raw_keybuf = Fmake_vector (make_number (30), Qnil);
9712 staticpro (&raw_keybuf);
9714 Qextended_command_history = intern ("extended-command-history");
9715 Fset (Qextended_command_history, Qnil);
9716 staticpro (&Qextended_command_history);
9718 kbd_buffer_frame_or_window
9719 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
9720 staticpro (&kbd_buffer_frame_or_window);
9722 accent_key_syms = Qnil;
9723 staticpro (&accent_key_syms);
9725 func_key_syms = Qnil;
9726 staticpro (&func_key_syms);
9728 mouse_syms = Qnil;
9729 staticpro (&mouse_syms);
9731 #ifdef WINDOWSNT
9732 mouse_wheel_syms = Qnil;
9733 staticpro (&mouse_wheel_syms);
9735 drag_n_drop_syms = Qnil;
9736 staticpro (&drag_n_drop_syms);
9737 #endif
9739 unread_switch_frame = Qnil;
9740 staticpro (&unread_switch_frame);
9742 internal_last_event_frame = Qnil;
9743 staticpro (&internal_last_event_frame);
9745 read_key_sequence_cmd = Qnil;
9746 staticpro (&read_key_sequence_cmd);
9748 menu_bar_one_keymap_changed_items = Qnil;
9749 staticpro (&menu_bar_one_keymap_changed_items);
9751 defsubr (&Sevent_convert_list);
9752 defsubr (&Sread_key_sequence);
9753 defsubr (&Sread_key_sequence_vector);
9754 defsubr (&Srecursive_edit);
9755 #ifdef HAVE_MOUSE
9756 defsubr (&Strack_mouse);
9757 #endif
9758 defsubr (&Sinput_pending_p);
9759 defsubr (&Scommand_execute);
9760 defsubr (&Srecent_keys);
9761 defsubr (&Sthis_command_keys);
9762 defsubr (&Sthis_command_keys_vector);
9763 defsubr (&Sthis_single_command_keys);
9764 defsubr (&Sthis_single_command_raw_keys);
9765 defsubr (&Sreset_this_command_lengths);
9766 defsubr (&Sclear_this_command_keys);
9767 defsubr (&Ssuspend_emacs);
9768 defsubr (&Sabort_recursive_edit);
9769 defsubr (&Sexit_recursive_edit);
9770 defsubr (&Srecursion_depth);
9771 defsubr (&Stop_level);
9772 defsubr (&Sdiscard_input);
9773 defsubr (&Sopen_dribble_file);
9774 defsubr (&Sset_input_mode);
9775 defsubr (&Scurrent_input_mode);
9776 defsubr (&Sexecute_extended_command);
9778 DEFVAR_LISP ("last-command-char", &last_command_char,
9779 "Last input event that was part of a command.");
9781 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
9782 "Last input event that was part of a command.");
9784 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
9785 "Last input event in a command, except for mouse menu events.\n\
9786 Mouse menus give back keys that don't look like mouse events;\n\
9787 this variable holds the actual mouse event that led to the menu,\n\
9788 so that you can determine whether the command was run by mouse or not.");
9790 DEFVAR_LISP ("last-input-char", &last_input_char,
9791 "Last input event.");
9793 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
9794 "Last input event.");
9796 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
9797 "List of events to be read as the command input.\n\
9798 These events are processed first, before actual keyboard input.");
9799 Vunread_command_events = Qnil;
9801 DEFVAR_INT ("unread-command-char", &unread_command_char,
9802 "If not -1, an object to be read as next command input event.");
9804 DEFVAR_LISP ("unread-post-input-method-events", &Vunread_post_input_method_events,
9805 "List of events to be processed as input by input methods.\n\
9806 These events are processed after `unread-command-events', but\n\
9807 before actual keyboard input.");
9808 Vunread_post_input_method_events = Qnil;
9810 DEFVAR_LISP ("unread-input-method-events", &Vunread_input_method_events,
9811 "List of events to be processed as input by input methods.\n\
9812 These events are processed after `unread-command-events', but\n\
9813 before actual keyboard input.");
9814 Vunread_input_method_events = Qnil;
9816 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
9817 "Meta-prefix character code.\n\
9818 Meta-foo as command input turns into this character followed by foo.");
9819 XSETINT (meta_prefix_char, 033);
9821 DEFVAR_KBOARD ("last-command", Vlast_command,
9822 "The last command executed.\n\
9823 Normally a symbol with a function definition, but can be whatever was found\n\
9824 in the keymap, or whatever the variable `this-command' was set to by that\n\
9825 command.\n\
9827 The value `mode-exit' is special; it means that the previous command\n\
9828 read an event that told it to exit, and it did so and unread that event.\n\
9829 In other words, the present command is the event that made the previous\n\
9830 command exit.\n\
9832 The value `kill-region' is special; it means that the previous command\n\
9833 was a kill command.");
9835 DEFVAR_KBOARD ("real-last-command", Vreal_last_command,
9836 "Same as `last-command', but never altered by Lisp code.");
9838 DEFVAR_LISP ("this-command", &Vthis_command,
9839 "The command now being executed.\n\
9840 The command can set this variable; whatever is put here\n\
9841 will be in `last-command' during the following command.");
9842 Vthis_command = Qnil;
9844 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
9845 "*Number of keyboard input characters between auto-saves.\n\
9846 Zero means disable autosaving due to number of characters typed.");
9847 auto_save_interval = 300;
9849 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
9850 "*Number of seconds idle time before auto-save.\n\
9851 Zero or nil means disable auto-saving due to idleness.\n\
9852 After auto-saving due to this many seconds of idle time,\n\
9853 Emacs also does a garbage collection if that seems to be warranted.");
9854 XSETFASTINT (Vauto_save_timeout, 30);
9856 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
9857 "*Nonzero means echo unfinished commands after this many seconds of pause.");
9858 echo_keystrokes = 1;
9860 DEFVAR_INT ("polling-period", &polling_period,
9861 "*Interval between polling for input during Lisp execution.\n\
9862 The reason for polling is to make C-g work to stop a running program.\n\
9863 Polling is needed only when using X windows and SIGIO does not work.\n\
9864 Polling is automatically disabled in all other cases.");
9865 polling_period = 2;
9867 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
9868 "*Maximum time between mouse clicks to make a double-click.\n\
9869 Measured in milliseconds. nil means disable double-click recognition;\n\
9870 t means double-clicks have no time limit and are detected\n\
9871 by position only.");
9872 Vdouble_click_time = make_number (500);
9874 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
9875 "*Non-nil means inhibit local map menu bar menus.");
9876 inhibit_local_menu_bar_menus = 0;
9878 DEFVAR_INT ("num-input-keys", &num_input_keys,
9879 "Number of complete key sequences read as input so far.\n\
9880 This includes key sequences read from keyboard macros.\n\
9881 The number is effectively the number of interactive command invocations.");
9882 num_input_keys = 0;
9884 DEFVAR_INT ("num-nonmacro-input-events", &num_nonmacro_input_events,
9885 "Number of input events read from the keyboard so far.\n\
9886 This does not include events generated by keyboard macros.");
9887 num_nonmacro_input_events = 0;
9889 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
9890 "The frame in which the most recently read event occurred.\n\
9891 If the last event came from a keyboard macro, this is set to `macro'.");
9892 Vlast_event_frame = Qnil;
9894 /* This variable is set up in sysdep.c. */
9895 DEFVAR_LISP ("tty-erase-char", &Vtty_erase_char,
9896 "The ERASE character as set by the user with stty.");
9898 DEFVAR_LISP ("help-char", &Vhelp_char,
9899 "Character to recognize as meaning Help.\n\
9900 When it is read, do `(eval help-form)', and display result if it's a string.\n\
9901 If the value of `help-form' is nil, this char can be read normally.");
9902 XSETINT (Vhelp_char, Ctl ('H'));
9904 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
9905 "List of input events to recognize as meaning Help.\n\
9906 These work just like the value of `help-char' (see that).");
9907 Vhelp_event_list = Qnil;
9909 DEFVAR_LISP ("help-form", &Vhelp_form,
9910 "Form to execute when character `help-char' is read.\n\
9911 If the form returns a string, that string is displayed.\n\
9912 If `help-form' is nil, the help char is not recognized.");
9913 Vhelp_form = Qnil;
9915 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
9916 "Command to run when `help-char' character follows a prefix key.\n\
9917 This command is used only when there is no actual binding\n\
9918 for that character after that prefix key.");
9919 Vprefix_help_command = Qnil;
9921 DEFVAR_LISP ("top-level", &Vtop_level,
9922 "Form to evaluate when Emacs starts up.\n\
9923 Useful to set before you dump a modified Emacs.");
9924 Vtop_level = Qnil;
9926 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
9927 "Translate table for keyboard input, or nil.\n\
9928 Each character is looked up in this string and the contents used instead.\n\
9929 The value may be a string, a vector, or a char-table.\n\
9930 If it is a string or vector of length N,\n\
9931 character codes N and up are untranslated.\n\
9932 In a vector or a char-table, an element which is nil means \"no translation\".");
9933 Vkeyboard_translate_table = Qnil;
9935 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
9936 "Non-nil means to always spawn a subshell instead of suspending.\n\
9937 \(Even if the operating system has support for stopping a process.\)");
9938 cannot_suspend = 0;
9940 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
9941 "Non-nil means prompt with menus when appropriate.\n\
9942 This is done when reading from a keymap that has a prompt string,\n\
9943 for elements that have prompt strings.\n\
9944 The menu is displayed on the screen\n\
9945 if X menus were enabled at configuration\n\
9946 time and the previous event was a mouse click prefix key.\n\
9947 Otherwise, menu prompting uses the echo area.");
9948 menu_prompting = 1;
9950 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
9951 "Character to see next line of menu prompt.\n\
9952 Type this character while in a menu prompt to rotate around the lines of it.");
9953 XSETINT (menu_prompt_more_char, ' ');
9955 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
9956 "A mask of additional modifier keys to use with every keyboard character.\n\
9957 Emacs applies the modifiers of the character stored here to each keyboard\n\
9958 character it reads. For example, after evaluating the expression\n\
9959 (setq extra-keyboard-modifiers ?\\C-x)\n\
9960 all input characters will have the control modifier applied to them.\n\
9962 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
9963 not count as a control character; rather, it counts as a character\n\
9964 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
9965 cancels any modification.");
9966 extra_keyboard_modifiers = 0;
9968 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
9969 "If an editing command sets this to t, deactivate the mark afterward.\n\
9970 The command loop sets this to nil before each command,\n\
9971 and tests the value when the command returns.\n\
9972 Buffer modification stores t in this variable.");
9973 Vdeactivate_mark = Qnil;
9975 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
9976 "Temporary storage of pre-command-hook or post-command-hook.");
9977 Vcommand_hook_internal = Qnil;
9979 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
9980 "Normal hook run before each command is executed.\n\
9981 If an unhandled error happens in running this hook,\n\
9982 the hook value is set to nil, since otherwise the error\n\
9983 might happen repeatedly and make Emacs nonfunctional.");
9984 Vpre_command_hook = Qnil;
9986 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
9987 "Normal hook run after each command is executed.\n\
9988 If an unhandled error happens in running this hook,\n\
9989 the hook value is set to nil, since otherwise the error\n\
9990 might happen repeatedly and make Emacs nonfunctional.");
9991 Vpost_command_hook = Qnil;
9993 DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
9994 "Normal hook run after each command is executed, if idle.\n\
9995 Errors running the hook are caught and ignored.\n\
9996 This feature is obsolete; use idle timers instead. See `etc/NEWS'.");
9997 Vpost_command_idle_hook = Qnil;
9999 DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
10000 "Delay time before running `post-command-idle-hook'.\n\
10001 This is measured in microseconds.");
10002 post_command_idle_delay = 100000;
10004 #if 0
10005 DEFVAR_LISP ("echo-area-clear-hook", ...,
10006 "Normal hook run when clearing the echo area.");
10007 #endif
10008 Qecho_area_clear_hook = intern ("echo-area-clear-hook");
10009 XSYMBOL (Qecho_area_clear_hook)->value = Qnil;
10011 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
10012 "t means menu bar, specified Lucid style, needs to be recomputed.");
10013 Vlucid_menu_bar_dirty_flag = Qnil;
10015 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
10016 "List of menu bar items to move to the end of the menu bar.\n\
10017 The elements of the list are event types that may have menu bar bindings.");
10018 Vmenu_bar_final_items = Qnil;
10020 DEFVAR_KBOARD ("overriding-terminal-local-map",
10021 Voverriding_terminal_local_map,
10022 "Per-terminal keymap that overrides all other local keymaps.\n\
10023 If this variable is non-nil, it is used as a keymap instead of the\n\
10024 buffer's local map, and the minor mode keymaps and text property keymaps.\n\
10025 This variable is intended to let commands such as `universal-argumemnt'\n\
10026 set up a different keymap for reading the next command.");
10028 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
10029 "Keymap that overrides all other local keymaps.\n\
10030 If this variable is non-nil, it is used as a keymap instead of the\n\
10031 buffer's local map, and the minor mode keymaps and text property keymaps.");
10032 Voverriding_local_map = Qnil;
10034 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
10035 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
10036 Otherwise, the menu bar continues to reflect the buffer's local map\n\
10037 and the minor mode maps regardless of `overriding-local-map'.");
10038 Voverriding_local_map_menu_flag = Qnil;
10040 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
10041 "Keymap defining bindings for special events to execute at low level.");
10042 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
10044 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
10045 "*Non-nil means generate motion events for mouse motion.");
10047 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
10048 "Alist of system-specific X windows key symbols.\n\
10049 Each element should have the form (N . SYMBOL) where N is the\n\
10050 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
10051 and SYMBOL is its name.");
10053 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
10054 "List of deferred actions to be performed at a later time.\n\
10055 The precise format isn't relevant here; we just check whether it is nil.");
10056 Vdeferred_action_list = Qnil;
10058 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
10059 "Function to call to handle deferred actions, after each command.\n\
10060 This function is called with no arguments after each command\n\
10061 whenever `deferred-action-list' is non-nil.");
10062 Vdeferred_action_function = Qnil;
10064 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
10065 "*Non-nil means show the equivalent key-binding when M-x command has one.\n\
10066 The value can be a length of time to show the message for.\n\
10067 If the value is non-nil and not a number, we wait 2 seconds.");
10068 Vsuggest_key_bindings = Qt;
10070 DEFVAR_LISP ("timer-list", &Vtimer_list,
10071 "List of active absolute time timers in order of increasing time");
10072 Vtimer_list = Qnil;
10074 DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
10075 "List of active idle-time timers in order of increasing time");
10076 Vtimer_idle_list = Qnil;
10078 DEFVAR_LISP ("input-method-function", &Vinput_method_function,
10079 "If non-nil, the function that implements the current input method.\n\
10080 It's called with one argument, a printing character that was just read.\n\
10081 \(That means a character with code 040...0176.)\n\
10082 Typically this function uses `read-event' to read additional events.\n\
10083 When it does so, it should first bind `input-method-function' to nil\n\
10084 so it will not be called recursively.\n\
10086 The function should return a list of zero or more events\n\
10087 to be used as input. If it wants to put back some events\n\
10088 to be reconsidered, separately, by the input method,\n\
10089 it can add them to the beginning of `unread-command-events'.\n\
10091 The input method function can find in `input-method-previous-method'\n\
10092 the previous echo area message.\n\
10094 The input method function should refer to the variables\n\
10095 `input-method-use-echo-area' and `input-method-exit-on-first-char'\n\
10096 for guidance on what to do.");
10097 Vinput_method_function = Qnil;
10099 DEFVAR_LISP ("input-method-previous-message",
10100 &Vinput_method_previous_message,
10101 "When `input-method-function' is called, hold the previous echo area message.\n\
10102 This variable exists because `read-event' clears the echo area\n\
10103 before running the input method. It is nil if there was no message.");
10104 Vinput_method_previous_message = Qnil;
10106 DEFVAR_LISP ("show-help-function", &Vshow_help_function,
10107 "If non-nil, the function that implements the display of help.\n\
10108 It's called with one argument, the help string to display.");
10109 Vshow_help_function = Qnil;
10112 void
10113 keys_of_keyboard ()
10115 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
10116 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
10117 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
10118 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
10119 initial_define_key (meta_map, 'x', "execute-extended-command");
10121 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
10122 "handle-delete-frame");
10123 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
10124 "ignore-event");
10125 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
10126 "ignore-event");