Checked in at last a lot of improvementes and bug fixes. The oldest dating
[emacs/old-mirror.git] / src / keyboard.c
blobeb050c05c2525d47d34e04fcb88490203ab0f1da
1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985,86,87,88,89,93,94,95,96,97,99 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 #include <config.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include "termchar.h"
25 #include "termopts.h"
26 #include "lisp.h"
27 #include "termhooks.h"
28 #include "macros.h"
29 #include "frame.h"
30 #include "window.h"
31 #include "commands.h"
32 #include "buffer.h"
33 #include "charset.h"
34 #include "disptab.h"
35 #include "dispextern.h"
36 #include "keyboard.h"
37 #include "syntax.h"
38 #include "intervals.h"
39 #include "blockinput.h"
40 #include "puresize.h"
41 #include <setjmp.h>
42 #include <errno.h>
44 #ifdef MSDOS
45 #include "msdos.h"
46 #include <time.h>
47 #else /* not MSDOS */
48 #ifndef VMS
49 #include <sys/ioctl.h>
50 #endif
51 #endif /* not MSDOS */
53 #include "syssignal.h"
54 #include "systty.h"
56 #include <sys/types.h>
57 #ifdef HAVE_UNISTD_H
58 #include <unistd.h>
59 #endif
61 /* This is to get the definitions of the XK_ symbols. */
62 #ifdef HAVE_X_WINDOWS
63 #include "xterm.h"
64 #endif
66 #ifdef HAVE_NTGUI
67 #include "w32term.h"
68 #endif /* HAVE_NTGUI */
70 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
71 #include "systime.h"
73 extern int errno;
75 /* Variables for blockinput.h: */
77 /* Non-zero if interrupt input is blocked right now. */
78 int interrupt_input_blocked;
80 /* Nonzero means an input interrupt has arrived
81 during the current critical section. */
82 int interrupt_input_pending;
85 /* File descriptor to use for input. */
86 extern int input_fd;
88 #ifdef HAVE_WINDOW_SYSTEM
89 /* Make all keyboard buffers much bigger when using X windows. */
90 #ifdef macintosh
91 /* But not too big (local data > 32K error) if on macintosh */
92 #define KBD_BUFFER_SIZE 512
93 #else
94 #define KBD_BUFFER_SIZE 4096
95 #endif
96 #else /* No X-windows, character input */
97 #define KBD_BUFFER_SIZE 256
98 #endif /* No X-windows */
100 /* Following definition copied from eval.c */
102 struct backtrace
104 struct backtrace *next;
105 Lisp_Object *function;
106 Lisp_Object *args; /* Points to vector of args. */
107 int nargs; /* length of vector. If nargs is UNEVALLED,
108 args points to slot holding list of
109 unevalled args */
110 char evalargs;
113 #ifdef MULTI_KBOARD
114 KBOARD *initial_kboard;
115 KBOARD *current_kboard;
116 KBOARD *all_kboards;
117 int single_kboard;
118 #else
119 KBOARD the_only_kboard;
120 #endif
122 /* Non-nil disable property on a command means
123 do not execute it; call disabled-command-hook's value instead. */
124 Lisp_Object Qdisabled, Qdisabled_command_hook;
126 #define NUM_RECENT_KEYS (100)
127 int recent_keys_index; /* Index for storing next element into recent_keys */
128 int total_keys; /* Total number of elements stored into recent_keys */
129 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
131 /* Vector holding the key sequence that invoked the current command.
132 It is reused for each command, and it may be longer than the current
133 sequence; this_command_key_count indicates how many elements
134 actually mean something.
135 It's easier to staticpro a single Lisp_Object than an array. */
136 Lisp_Object this_command_keys;
137 int this_command_key_count;
139 /* This vector is used as a buffer to record the events that were actually read
140 by read_key_sequence. */
141 Lisp_Object raw_keybuf;
142 int raw_keybuf_count;
144 #define GROW_RAW_KEYBUF \
145 if (raw_keybuf_count == XVECTOR (raw_keybuf)->size) \
147 int newsize = 2 * XVECTOR (raw_keybuf)->size; \
148 Lisp_Object new; \
149 new = Fmake_vector (make_number (newsize), Qnil); \
150 bcopy (XVECTOR (raw_keybuf)->contents, XVECTOR (new)->contents, \
151 raw_keybuf_count * sizeof (Lisp_Object)); \
152 raw_keybuf = new; \
155 /* Number of elements of this_command_keys
156 that precede this key sequence. */
157 int this_single_command_key_start;
159 /* Record values of this_command_key_count and echo_length ()
160 before this command was read. */
161 static int before_command_key_count;
162 static int before_command_echo_length;
163 /* Values of before_command_key_count and before_command_echo_length
164 saved by reset-this-command-lengths. */
165 static int before_command_key_count_1;
166 static int before_command_echo_length_1;
167 /* Flag set by reset-this-command-lengths,
168 saying to reset the lengths when add_command_key is called. */
169 static int before_command_restore_flag;
171 extern int minbuf_level;
173 extern int message_enable_multibyte;
175 extern struct backtrace *backtrace_list;
177 /* If non-nil, the function that implements the display of help.
178 It's called with one argument, the help string to display. */
180 Lisp_Object Vshow_help_function;
182 /* Nonzero means do menu prompting. */
183 static int menu_prompting;
185 /* Character to see next line of menu prompt. */
186 static Lisp_Object menu_prompt_more_char;
188 /* For longjmp to where kbd input is being done. */
189 static jmp_buf getcjmp;
191 /* True while doing kbd input. */
192 int waiting_for_input;
194 /* True while displaying for echoing. Delays C-g throwing. */
196 static int echoing;
198 /* Non-null means we can start echoing at the next input pause even
199 though there is something in the echo area. */
201 static struct kboard *ok_to_echo_at_next_pause;
203 /* The kboard currently echoing, or null for none. Set in echo_now to
204 the kboard echoing. Reset to 0 in cancel_echoing. If non-null,
205 and a current echo area message exists, we know that it comes from
206 echoing. */
208 static struct kboard *echo_kboard;
210 /* Nonzero means disregard local maps for the menu bar. */
211 static int inhibit_local_menu_bar_menus;
213 /* Nonzero means C-g should cause immediate error-signal. */
214 int immediate_quit;
216 /* The user's ERASE setting. */
217 Lisp_Object Vtty_erase_char;
219 /* Character to recognize as the help char. */
220 Lisp_Object Vhelp_char;
222 /* List of other event types to recognize as meaning "help". */
223 Lisp_Object Vhelp_event_list;
225 /* Form to execute when help char is typed. */
226 Lisp_Object Vhelp_form;
228 /* Command to run when the help character follows a prefix key. */
229 Lisp_Object Vprefix_help_command;
231 /* List of items that should move to the end of the menu bar. */
232 Lisp_Object Vmenu_bar_final_items;
234 /* Non-nil means show the equivalent key-binding for
235 any M-x command that has one.
236 The value can be a length of time to show the message for.
237 If the value is non-nil and not a number, we wait 2 seconds. */
238 Lisp_Object Vsuggest_key_bindings;
240 /* Character that causes a quit. Normally C-g.
242 If we are running on an ordinary terminal, this must be an ordinary
243 ASCII char, since we want to make it our interrupt character.
245 If we are not running on an ordinary terminal, it still needs to be
246 an ordinary ASCII char. This character needs to be recognized in
247 the input interrupt handler. At this point, the keystroke is
248 represented as a struct input_event, while the desired quit
249 character is specified as a lispy event. The mapping from struct
250 input_events to lispy events cannot run in an interrupt handler,
251 and the reverse mapping is difficult for anything but ASCII
252 keystrokes.
254 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
255 ASCII character. */
256 int quit_char;
258 extern Lisp_Object current_global_map;
259 extern int minibuf_level;
261 /* If non-nil, this is a map that overrides all other local maps. */
262 Lisp_Object Voverriding_local_map;
264 /* If non-nil, Voverriding_local_map applies to the menu bar. */
265 Lisp_Object Voverriding_local_map_menu_flag;
267 /* Keymap that defines special misc events that should
268 be processed immediately at a low level. */
269 Lisp_Object Vspecial_event_map;
271 /* Current depth in recursive edits. */
272 int command_loop_level;
274 /* Total number of times command_loop has read a key sequence. */
275 int num_input_keys;
277 /* Last input character read as a command. */
278 Lisp_Object last_command_char;
280 /* Last input character read as a command, not counting menus
281 reached by the mouse. */
282 Lisp_Object last_nonmenu_event;
284 /* Last input character read for any purpose. */
285 Lisp_Object last_input_char;
287 /* If not Qnil, a list of objects to be read as subsequent command input. */
288 Lisp_Object Vunread_command_events;
290 /* If not Qnil, a list of objects to be read as subsequent command input
291 including input method processing. */
292 Lisp_Object Vunread_input_method_events;
294 /* If not Qnil, a list of objects to be read as subsequent command input
295 but NOT including input method processing. */
296 Lisp_Object Vunread_post_input_method_events;
298 /* If not -1, an event to be read as subsequent command input. */
299 int unread_command_char;
301 /* If not Qnil, this is a switch-frame event which we decided to put
302 off until the end of a key sequence. This should be read as the
303 next command input, after any unread_command_events.
305 read_key_sequence uses this to delay switch-frame events until the
306 end of the key sequence; Fread_char uses it to put off switch-frame
307 events until a non-ASCII event is acceptable as input. */
308 Lisp_Object unread_switch_frame;
310 /* A mask of extra modifier bits to put into every keyboard char. */
311 int extra_keyboard_modifiers;
313 /* Char to use as prefix when a meta character is typed in.
314 This is bound on entry to minibuffer in case ESC is changed there. */
316 Lisp_Object meta_prefix_char;
318 /* Last size recorded for a current buffer which is not a minibuffer. */
319 static int last_non_minibuf_size;
321 /* Number of idle seconds before an auto-save and garbage collection. */
322 static Lisp_Object Vauto_save_timeout;
324 /* Total number of times read_char has returned. */
325 int num_input_events;
327 /* Total number of times read_char has returned, outside of macros. */
328 int num_nonmacro_input_events;
330 /* Auto-save automatically when this many characters have been typed
331 since the last time. */
333 static int auto_save_interval;
335 /* Value of num_nonmacro_input_events as of last auto save. */
337 int last_auto_save;
339 /* The command being executed by the command loop.
340 Commands may set this, and the value set will be copied into
341 current_kboard->Vlast_command instead of the actual command. */
342 Lisp_Object Vthis_command;
344 /* This is like Vthis_command, except that commands never set it. */
345 Lisp_Object real_this_command;
347 /* The value of point when the last command was executed. */
348 int last_point_position;
350 /* The buffer that was current when the last command was started. */
351 Lisp_Object last_point_position_buffer;
353 /* The frame in which the last input event occurred, or Qmacro if the
354 last event came from a macro. We use this to determine when to
355 generate switch-frame events. This may be cleared by functions
356 like Fselect_frame, to make sure that a switch-frame event is
357 generated by the next character. */
358 Lisp_Object internal_last_event_frame;
360 /* A user-visible version of the above, intended to allow users to
361 figure out where the last event came from, if the event doesn't
362 carry that information itself (i.e. if it was a character). */
363 Lisp_Object Vlast_event_frame;
365 /* The timestamp of the last input event we received from the X server.
366 X Windows wants this for selection ownership. */
367 unsigned long last_event_timestamp;
369 Lisp_Object Qself_insert_command;
370 Lisp_Object Qforward_char;
371 Lisp_Object Qbackward_char;
372 Lisp_Object Qundefined;
373 Lisp_Object Qtimer_event_handler;
375 /* read_key_sequence stores here the command definition of the
376 key sequence that it reads. */
377 Lisp_Object read_key_sequence_cmd;
379 /* Form to evaluate (if non-nil) when Emacs is started. */
380 Lisp_Object Vtop_level;
382 /* User-supplied string to translate input characters through. */
383 Lisp_Object Vkeyboard_translate_table;
385 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
386 extern Lisp_Object Vfunction_key_map;
388 /* Another keymap that maps key sequences into key sequences.
389 This one takes precedence over ordinary definitions. */
390 extern Lisp_Object Vkey_translation_map;
392 /* If non-nil, this implements the current input method. */
393 Lisp_Object Vinput_method_function;
394 Lisp_Object Qinput_method_function;
396 /* When we call Vinput_method_function,
397 this holds the echo area message that was just erased. */
398 Lisp_Object Vinput_method_previous_message;
400 /* Non-nil means deactivate the mark at end of this command. */
401 Lisp_Object Vdeactivate_mark;
403 /* Menu bar specified in Lucid Emacs fashion. */
405 Lisp_Object Vlucid_menu_bar_dirty_flag;
406 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
408 Lisp_Object Qecho_area_clear_hook;
410 /* Hooks to run before and after each command. */
411 Lisp_Object Qpre_command_hook, Vpre_command_hook;
412 Lisp_Object Qpost_command_hook, Vpost_command_hook;
413 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
414 /* Hook run after a command if there's no more input soon. */
415 Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook;
417 /* Delay time in microseconds before running post-command-idle-hook. */
418 int post_command_idle_delay;
420 /* List of deferred actions to be performed at a later time.
421 The precise format isn't relevant here; we just check whether it is nil. */
422 Lisp_Object Vdeferred_action_list;
424 /* Function to call to handle deferred actions, when there are any. */
425 Lisp_Object Vdeferred_action_function;
426 Lisp_Object Qdeferred_action_function;
428 Lisp_Object Qinput_method_exit_on_first_char;
429 Lisp_Object Qinput_method_use_echo_area;
431 /* File in which we write all commands we read. */
432 FILE *dribble;
434 /* Nonzero if input is available. */
435 int input_pending;
437 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
438 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
440 int meta_key;
442 extern char *pending_malloc_warning;
444 /* Circular buffer for pre-read keyboard input. */
445 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
447 /* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
449 The interrupt-level event handlers will never enqueue an event on a
450 frame which is not in Vframe_list, and once an event is dequeued,
451 internal_last_event_frame or the event itself points to the frame.
452 So that's all fine.
454 But while the event is sitting in the queue, it's completely
455 unprotected. Suppose the user types one command which will run for
456 a while and then delete a frame, and then types another event at
457 the frame that will be deleted, before the command gets around to
458 it. Suppose there are no references to this frame elsewhere in
459 Emacs, and a GC occurs before the second event is dequeued. Now we
460 have an event referring to a freed frame, which will crash Emacs
461 when it is dequeued.
463 Similar things happen when an event on a scroll bar is enqueued; the
464 window may be deleted while the event is in the queue.
466 So, we use this vector to protect the frame_or_window field in the
467 event queue. That way, they'll be dequeued as dead frames or
468 windows, but still valid lisp objects.
470 If kbd_buffer[i].kind != no_event, then
471 (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
472 == kbd_buffer[i].frame_or_window. */
473 static Lisp_Object kbd_buffer_frame_or_window;
475 /* Pointer to next available character in kbd_buffer.
476 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
477 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
478 next available char is in kbd_buffer[0]. */
479 static struct input_event *kbd_fetch_ptr;
481 /* Pointer to next place to store character in kbd_buffer. This
482 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
483 character should go in kbd_buffer[0]. */
484 static struct input_event * volatile kbd_store_ptr;
486 /* The above pair of variables forms a "queue empty" flag. When we
487 enqueue a non-hook event, we increment kbd_store_ptr. When we
488 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
489 there is input available iff the two pointers are not equal.
491 Why not just have a flag set and cleared by the enqueuing and
492 dequeuing functions? Such a flag could be screwed up by interrupts
493 at inopportune times. */
495 /* If this flag is non-nil, we check mouse_moved to see when the
496 mouse moves, and motion events will appear in the input stream.
497 Otherwise, mouse motion is ignored. */
498 static Lisp_Object do_mouse_tracking;
500 /* Symbols to head events. */
501 Lisp_Object Qmouse_movement;
502 Lisp_Object Qscroll_bar_movement;
503 Lisp_Object Qswitch_frame;
504 Lisp_Object Qdelete_frame;
505 Lisp_Object Qiconify_frame;
506 Lisp_Object Qmake_frame_visible;
507 Lisp_Object Qhelp_echo;
509 /* Symbols to denote kinds of events. */
510 Lisp_Object Qfunction_key;
511 Lisp_Object Qmouse_click;
512 #ifdef WINDOWSNT
513 Lisp_Object Qmouse_wheel;
514 Lisp_Object Qlanguage_change;
515 #endif
516 Lisp_Object Qdrag_n_drop;
517 /* Lisp_Object Qmouse_movement; - also an event header */
519 /* Properties of event headers. */
520 Lisp_Object Qevent_kind;
521 Lisp_Object Qevent_symbol_elements;
523 /* menu item parts */
524 Lisp_Object Qmenu_alias;
525 Lisp_Object Qmenu_enable;
526 Lisp_Object QCenable, QCvisible, QChelp, QCfilter, QCkeys, QCkey_sequence;
527 Lisp_Object QCbutton, QCtoggle, QCradio;
528 extern Lisp_Object Vdefine_key_rebound_commands;
529 extern Lisp_Object Qmenu_item;
531 /* An event header symbol HEAD may have a property named
532 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
533 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
534 mask of modifiers applied to it. If present, this is used to help
535 speed up parse_modifiers. */
536 Lisp_Object Qevent_symbol_element_mask;
538 /* An unmodified event header BASE may have a property named
539 Qmodifier_cache, which is an alist mapping modifier masks onto
540 modified versions of BASE. If present, this helps speed up
541 apply_modifiers. */
542 Lisp_Object Qmodifier_cache;
544 /* Symbols to use for parts of windows. */
545 Lisp_Object Qmode_line;
546 Lisp_Object Qvertical_line;
547 Lisp_Object Qvertical_scroll_bar;
548 Lisp_Object Qmenu_bar;
550 Lisp_Object recursive_edit_unwind (), command_loop ();
551 Lisp_Object Fthis_command_keys ();
552 Lisp_Object Qextended_command_history;
553 EMACS_TIME timer_check ();
555 extern Lisp_Object Vhistory_length;
557 extern char *x_get_keysym_name ();
559 static void record_menu_key ();
561 Lisp_Object Qpolling_period;
563 /* List of absolute timers. Appears in order of next scheduled event. */
564 Lisp_Object Vtimer_list;
566 /* List of idle time timers. Appears in order of next scheduled event. */
567 Lisp_Object Vtimer_idle_list;
569 /* Incremented whenever a timer is run. */
570 int timers_run;
572 extern Lisp_Object Vprint_level, Vprint_length;
574 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
575 happens. */
576 EMACS_TIME *input_available_clear_time;
578 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
579 Default is 1 if INTERRUPT_INPUT is defined. */
580 int interrupt_input;
582 /* Nonzero while interrupts are temporarily deferred during redisplay. */
583 int interrupts_deferred;
585 /* Nonzero means use ^S/^Q for flow control. */
586 int flow_control;
588 /* Allow m- file to inhibit use of FIONREAD. */
589 #ifdef BROKEN_FIONREAD
590 #undef FIONREAD
591 #endif
593 /* We are unable to use interrupts if FIONREAD is not available,
594 so flush SIGIO so we won't try. */
595 #ifndef FIONREAD
596 #ifdef SIGIO
597 #undef SIGIO
598 #endif
599 #endif
601 /* If we support a window system, turn on the code to poll periodically
602 to detect C-g. It isn't actually used when doing interrupt input. */
603 #ifdef HAVE_WINDOW_SYSTEM
604 #define POLL_FOR_INPUT
605 #endif
607 /* Global variable declarations. */
609 /* Function for init_keyboard to call with no args (if nonzero). */
610 void (*keyboard_init_hook) ();
612 static int read_avail_input ();
613 static void get_input_pending ();
614 static int readable_events ();
615 static Lisp_Object read_char_x_menu_prompt ();
616 static Lisp_Object read_char_minibuf_menu_prompt ();
617 static Lisp_Object make_lispy_event ();
618 #ifdef HAVE_MOUSE
619 static Lisp_Object make_lispy_movement ();
620 #endif
621 static Lisp_Object modify_event_symbol ();
622 static Lisp_Object make_lispy_switch_frame ();
623 static int parse_solitary_modifier ();
624 static void save_getcjmp ();
625 static void restore_getcjmp ();
626 static Lisp_Object apply_modifiers P_ ((int, Lisp_Object));
628 /* > 0 if we are to echo keystrokes. */
629 static int echo_keystrokes;
631 /* Nonzero means don't try to suspend even if the operating system seems
632 to support it. */
633 static int cannot_suspend;
635 #define min(a,b) ((a)<(b)?(a):(b))
636 #define max(a,b) ((a)>(b)?(a):(b))
638 /* Install the string STR as the beginning of the string of echoing,
639 so that it serves as a prompt for the next character.
640 Also start echoing. */
642 void
643 echo_prompt (str)
644 char *str;
646 int len = strlen (str);
648 if (len > ECHOBUFSIZE - 4)
649 len = ECHOBUFSIZE - 4;
650 bcopy (str, current_kboard->echobuf, len);
651 current_kboard->echoptr = current_kboard->echobuf + len;
652 *current_kboard->echoptr = '\0';
654 current_kboard->echo_after_prompt = len;
656 echo_now ();
659 /* Add C to the echo string, if echoing is going on.
660 C can be a character, which is printed prettily ("M-C-x" and all that
661 jazz), or a symbol, whose name is printed. */
663 void
664 echo_char (c)
665 Lisp_Object c;
667 extern char *push_key_description ();
669 if (current_kboard->immediate_echo)
671 char *ptr = current_kboard->echoptr;
673 if (ptr != current_kboard->echobuf)
674 *ptr++ = ' ';
676 /* If someone has passed us a composite event, use its head symbol. */
677 c = EVENT_HEAD (c);
679 if (INTEGERP (c))
681 if (ptr - current_kboard->echobuf > ECHOBUFSIZE - 6)
682 return;
684 ptr = push_key_description (XINT (c), ptr);
686 else if (SYMBOLP (c))
688 struct Lisp_String *name = XSYMBOL (c)->name;
689 if ((ptr - current_kboard->echobuf) + STRING_BYTES (name) + 4
690 > ECHOBUFSIZE)
691 return;
692 bcopy (name->data, ptr, STRING_BYTES (name));
693 ptr += STRING_BYTES (name);
696 if (current_kboard->echoptr == current_kboard->echobuf
697 && help_char_p (c))
699 strcpy (ptr, " (Type ? for further options)");
700 ptr += strlen (ptr);
703 *ptr = 0;
704 current_kboard->echoptr = ptr;
706 echo_now ();
710 /* Temporarily add a dash to the end of the echo string if it's not
711 empty, so that it serves as a mini-prompt for the very next character. */
713 void
714 echo_dash ()
716 if (!current_kboard->immediate_echo
717 && current_kboard->echoptr == current_kboard->echobuf)
718 return;
719 /* Do nothing if we just printed a prompt. */
720 if (current_kboard->echo_after_prompt
721 == current_kboard->echoptr - current_kboard->echobuf)
722 return;
723 /* Do nothing if not echoing at all. */
724 if (current_kboard->echoptr == 0)
725 return;
727 /* Put a dash at the end of the buffer temporarily,
728 but make it go away when the next character is added. */
729 current_kboard->echoptr[0] = '-';
730 current_kboard->echoptr[1] = 0;
732 echo_now ();
735 /* Display the current echo string, and begin echoing if not already
736 doing so. */
738 void
739 echo_now ()
741 if (!current_kboard->immediate_echo)
743 int i;
744 current_kboard->immediate_echo = 1;
746 for (i = 0; i < this_command_key_count; i++)
748 Lisp_Object c;
749 c = XVECTOR (this_command_keys)->contents[i];
750 if (! (EVENT_HAS_PARAMETERS (c)
751 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
752 echo_char (c);
754 echo_dash ();
757 echoing = 1;
758 echo_kboard = current_kboard;
759 message2_nolog (current_kboard->echobuf, strlen (current_kboard->echobuf),
760 ! NILP (current_buffer->enable_multibyte_characters));
761 echoing = 0;
763 if (waiting_for_input && !NILP (Vquit_flag))
764 quit_throw_to_read_char ();
767 /* Turn off echoing, for the start of a new command. */
769 void
770 cancel_echoing ()
772 current_kboard->immediate_echo = 0;
773 current_kboard->echoptr = current_kboard->echobuf;
774 current_kboard->echo_after_prompt = -1;
775 ok_to_echo_at_next_pause = 0;
776 echo_kboard = 0;
779 /* Return the length of the current echo string. */
781 static int
782 echo_length ()
784 return current_kboard->echoptr - current_kboard->echobuf;
787 /* Truncate the current echo message to its first LEN chars.
788 This and echo_char get used by read_key_sequence when the user
789 switches frames while entering a key sequence. */
791 static void
792 echo_truncate (len)
793 int len;
795 current_kboard->echobuf[len] = '\0';
796 current_kboard->echoptr = current_kboard->echobuf + len;
797 truncate_echo_area (len);
801 /* Functions for manipulating this_command_keys. */
802 static void
803 add_command_key (key)
804 Lisp_Object key;
806 int size = XVECTOR (this_command_keys)->size;
808 /* If reset-this-command-length was called recently, obey it now.
809 See the doc string of that function for an explanation of why. */
810 if (before_command_restore_flag)
812 this_command_key_count = before_command_key_count_1;
813 if (this_command_key_count < this_single_command_key_start)
814 this_single_command_key_start = this_command_key_count;
815 echo_truncate (before_command_echo_length_1);
816 before_command_restore_flag = 0;
819 if (this_command_key_count >= size)
821 Lisp_Object new_keys;
823 new_keys = Fmake_vector (make_number (size * 2), Qnil);
824 bcopy (XVECTOR (this_command_keys)->contents,
825 XVECTOR (new_keys)->contents,
826 size * sizeof (Lisp_Object));
828 this_command_keys = new_keys;
831 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
834 Lisp_Object
835 recursive_edit_1 ()
837 int count = specpdl_ptr - specpdl;
838 Lisp_Object val;
840 if (command_loop_level > 0)
842 specbind (Qstandard_output, Qt);
843 specbind (Qstandard_input, Qt);
846 val = command_loop ();
847 if (EQ (val, Qt))
848 Fsignal (Qquit, Qnil);
849 /* Handle throw from read_minibuf when using minibuffer
850 while it's active but we're in another window. */
851 if (STRINGP (val))
852 Fsignal (Qerror, Fcons (val, Qnil));
854 return unbind_to (count, Qnil);
857 /* When an auto-save happens, record the "time", and don't do again soon. */
859 void
860 record_auto_save ()
862 last_auto_save = num_nonmacro_input_events;
865 /* Make an auto save happen as soon as possible at command level. */
867 void
868 force_auto_save_soon ()
870 last_auto_save = - auto_save_interval - 1;
872 record_asynch_buffer_change ();
875 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
876 "Invoke the editor command loop recursively.\n\
877 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
878 that tells this function to return.\n\
879 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
880 This function is called by the editor initialization to begin editing.")
883 int count = specpdl_ptr - specpdl;
885 command_loop_level++;
886 update_mode_lines = 1;
888 record_unwind_protect (recursive_edit_unwind,
889 (command_loop_level
890 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
891 ? Fcurrent_buffer ()
892 : Qnil);
893 recursive_edit_1 ();
894 return unbind_to (count, Qnil);
897 Lisp_Object
898 recursive_edit_unwind (buffer)
899 Lisp_Object buffer;
901 if (!NILP (buffer))
902 Fset_buffer (buffer);
904 command_loop_level--;
905 update_mode_lines = 1;
906 return Qnil;
909 static void
910 any_kboard_state ()
912 #ifdef MULTI_KBOARD
913 #if 0 /* Theory: if there's anything in Vunread_command_events,
914 it will right away be read by read_key_sequence,
915 and then if we do switch KBOARDS, it will go into the side
916 queue then. So we don't need to do anything special here -- rms. */
917 if (CONSP (Vunread_command_events))
919 current_kboard->kbd_queue
920 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
921 current_kboard->kbd_queue_has_data = 1;
923 Vunread_command_events = Qnil;
924 #endif
925 single_kboard = 0;
926 #endif
929 /* Switch to the single-kboard state, making current_kboard
930 the only KBOARD from which further input is accepted. */
932 void
933 single_kboard_state ()
935 #ifdef MULTI_KBOARD
936 single_kboard = 1;
937 #endif
940 /* Maintain a stack of kboards, so other parts of Emacs
941 can switch temporarily to the kboard of a given frame
942 and then revert to the previous status. */
944 struct kboard_stack
946 KBOARD *kboard;
947 struct kboard_stack *next;
950 static struct kboard_stack *kboard_stack;
952 void
953 push_frame_kboard (f)
954 FRAME_PTR f;
956 #ifdef MULTI_KBOARD
957 struct kboard_stack *p
958 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
960 p->next = kboard_stack;
961 p->kboard = current_kboard;
962 kboard_stack = p;
964 current_kboard = FRAME_KBOARD (f);
965 #endif
968 void
969 pop_frame_kboard ()
971 #ifdef MULTI_KBOARD
972 struct kboard_stack *p = kboard_stack;
973 current_kboard = p->kboard;
974 kboard_stack = p->next;
975 xfree (p);
976 #endif
979 /* Handle errors that are not handled at inner levels
980 by printing an error message and returning to the editor command loop. */
982 Lisp_Object
983 cmd_error (data)
984 Lisp_Object data;
986 Lisp_Object old_level, old_length;
987 char macroerror[50];
989 if (!NILP (executing_macro))
991 if (executing_macro_iterations == 1)
992 sprintf (macroerror, "After 1 kbd macro iteration: ");
993 else
994 sprintf (macroerror, "After %d kbd macro iterations: ",
995 executing_macro_iterations);
997 else
998 *macroerror = 0;
1000 Vstandard_output = Qt;
1001 Vstandard_input = Qt;
1002 Vexecuting_macro = Qnil;
1003 executing_macro = Qnil;
1004 current_kboard->Vprefix_arg = Qnil;
1005 current_kboard->Vlast_prefix_arg = Qnil;
1006 cancel_echoing ();
1008 /* Avoid unquittable loop if data contains a circular list. */
1009 old_level = Vprint_level;
1010 old_length = Vprint_length;
1011 XSETFASTINT (Vprint_level, 10);
1012 XSETFASTINT (Vprint_length, 10);
1013 cmd_error_internal (data, macroerror);
1014 Vprint_level = old_level;
1015 Vprint_length = old_length;
1017 Vquit_flag = Qnil;
1019 Vinhibit_quit = Qnil;
1020 #ifdef MULTI_KBOARD
1021 any_kboard_state ();
1022 #endif
1024 return make_number (0);
1027 /* Take actions on handling an error. DATA is the data that describes
1028 the error.
1030 CONTEXT is a C-string containing ASCII characters only which
1031 describes the context in which the error happened. If we need to
1032 generalize CONTEXT to allow multibyte characters, make it a Lisp
1033 string. */
1035 void
1036 cmd_error_internal (data, context)
1037 Lisp_Object data;
1038 char *context;
1040 Lisp_Object stream;
1041 int kill_emacs_p = 0;
1042 struct frame *sf = SELECTED_FRAME ();
1044 Vquit_flag = Qnil;
1045 Vinhibit_quit = Qt;
1046 clear_message (1, 0);
1048 /* If the window system or terminal frame hasn't been initialized
1049 yet, or we're not interactive, it's best to dump this message out
1050 to stderr and exit. */
1051 if (!sf->glyphs_initialized_p
1052 /* This is the case of the frame dumped with Emacs, when we're
1053 running under a window system. */
1054 || (!NILP (Vwindow_system)
1055 && !inhibit_window_system
1056 && FRAME_TERMCAP_P (sf))
1057 || noninteractive)
1059 stream = Qexternal_debugging_output;
1060 kill_emacs_p = 1;
1062 else
1064 Fdiscard_input ();
1065 bitch_at_user ();
1066 stream = Qt;
1069 if (context != 0)
1070 write_string_1 (context, -1, stream);
1072 print_error_message (data, stream);
1074 /* If the window system or terminal frame hasn't been initialized
1075 yet, or we're in -batch mode, this error should cause Emacs to exit. */
1076 if (kill_emacs_p)
1078 Fterpri (stream);
1079 Fkill_emacs (make_number (-1));
1083 Lisp_Object command_loop_1 ();
1084 Lisp_Object command_loop_2 ();
1085 Lisp_Object top_level_1 ();
1087 /* Entry to editor-command-loop.
1088 This level has the catches for exiting/returning to editor command loop.
1089 It returns nil to exit recursive edit, t to abort it. */
1091 Lisp_Object
1092 command_loop ()
1094 if (command_loop_level > 0 || minibuf_level > 0)
1096 Lisp_Object val;
1097 val = internal_catch (Qexit, command_loop_2, Qnil);
1098 executing_macro = Qnil;
1099 return val;
1101 else
1102 while (1)
1104 internal_catch (Qtop_level, top_level_1, Qnil);
1105 internal_catch (Qtop_level, command_loop_2, Qnil);
1106 executing_macro = Qnil;
1108 /* End of file in -batch run causes exit here. */
1109 if (noninteractive)
1110 Fkill_emacs (Qt);
1114 /* Here we catch errors in execution of commands within the
1115 editing loop, and reenter the editing loop.
1116 When there is an error, cmd_error runs and returns a non-nil
1117 value to us. A value of nil means that cmd_loop_1 itself
1118 returned due to end of file (or end of kbd macro). */
1120 Lisp_Object
1121 command_loop_2 ()
1123 register Lisp_Object val;
1126 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
1127 while (!NILP (val));
1129 return Qnil;
1132 Lisp_Object
1133 top_level_2 ()
1135 return Feval (Vtop_level);
1138 Lisp_Object
1139 top_level_1 ()
1141 /* On entry to the outer level, run the startup file */
1142 if (!NILP (Vtop_level))
1143 internal_condition_case (top_level_2, Qerror, cmd_error);
1144 else if (!NILP (Vpurify_flag))
1145 message ("Bare impure Emacs (standard Lisp code not loaded)");
1146 else
1147 message ("Bare Emacs (standard Lisp code not loaded)");
1148 return Qnil;
1151 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1152 "Exit all recursive editing levels.")
1155 Fthrow (Qtop_level, Qnil);
1158 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1159 "Exit from the innermost recursive edit or minibuffer.")
1162 if (command_loop_level > 0 || minibuf_level > 0)
1163 Fthrow (Qexit, Qnil);
1165 error ("No recursive edit is in progress");
1168 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1169 "Abort the command that requested this recursive edit or minibuffer input.")
1172 if (command_loop_level > 0 || minibuf_level > 0)
1173 Fthrow (Qexit, Qt);
1175 error ("No recursive edit is in progress");
1178 /* This is the actual command reading loop,
1179 sans error-handling encapsulation. */
1181 Lisp_Object Fcommand_execute ();
1182 static int read_key_sequence ();
1183 void safe_run_hooks ();
1185 Lisp_Object
1186 command_loop_1 ()
1188 Lisp_Object cmd;
1189 int lose;
1190 int nonundocount;
1191 Lisp_Object keybuf[30];
1192 int i;
1193 int no_redisplay;
1194 int no_direct;
1195 int prev_modiff;
1196 struct buffer *prev_buffer;
1197 #ifdef MULTI_KBOARD
1198 int was_locked = single_kboard;
1199 #endif
1201 current_kboard->Vprefix_arg = Qnil;
1202 current_kboard->Vlast_prefix_arg = Qnil;
1203 Vdeactivate_mark = Qnil;
1204 waiting_for_input = 0;
1205 cancel_echoing ();
1207 nonundocount = 0;
1208 no_redisplay = 0;
1209 this_command_key_count = 0;
1210 this_single_command_key_start = 0;
1212 /* Make sure this hook runs after commands that get errors and
1213 throw to top level. */
1214 /* Note that the value cell will never directly contain nil
1215 if the symbol is a local variable. */
1216 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1217 safe_run_hooks (Qpost_command_hook);
1219 /* If displaying a message, resize the echo area window to fit
1220 that message's size exactly. */
1221 if (!NILP (echo_area_buffer[0]))
1222 resize_echo_area_axactly ();
1224 if (!NILP (Vdeferred_action_list))
1225 call0 (Vdeferred_action_function);
1227 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1229 if (NILP (Vunread_command_events)
1230 && NILP (Vunread_input_method_events)
1231 && NILP (Vunread_post_input_method_events)
1232 && NILP (Vexecuting_macro)
1233 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1234 safe_run_hooks (Qpost_command_idle_hook);
1237 /* Do this after running Vpost_command_hook, for consistency. */
1238 current_kboard->Vlast_command = Vthis_command;
1239 current_kboard->Vreal_last_command = real_this_command;
1241 while (1)
1243 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1244 Fkill_emacs (Qnil);
1246 /* Make sure the current window's buffer is selected. */
1247 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1248 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1250 /* Display any malloc warning that just came out. Use while because
1251 displaying one warning can cause another. */
1253 while (pending_malloc_warning)
1254 display_malloc_warning ();
1256 no_direct = 0;
1258 Vdeactivate_mark = Qnil;
1260 /* If minibuffer on and echo area in use,
1261 wait 2 sec and redraw minibuffer. */
1263 if (minibuf_level
1264 && !NILP (echo_area_buffer[0])
1265 && EQ (minibuf_window, echo_area_window))
1267 /* Bind inhibit-quit to t so that C-g gets read in
1268 rather than quitting back to the minibuffer. */
1269 int count = specpdl_ptr - specpdl;
1270 specbind (Qinhibit_quit, Qt);
1272 Fsit_for (make_number (2), Qnil, Qnil);
1273 /* Clear the echo area. */
1274 message2 (0, 0, 0);
1275 safe_run_hooks (Qecho_area_clear_hook);
1277 unbind_to (count, Qnil);
1279 /* If a C-g came in before, treat it as input now. */
1280 if (!NILP (Vquit_flag))
1282 Vquit_flag = Qnil;
1283 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1287 #ifdef C_ALLOCA
1288 alloca (0); /* Cause a garbage collection now */
1289 /* Since we can free the most stuff here. */
1290 #endif /* C_ALLOCA */
1292 #if 0
1293 /* Select the frame that the last event came from. Usually,
1294 switch-frame events will take care of this, but if some lisp
1295 code swallows a switch-frame event, we'll fix things up here.
1296 Is this a good idea? */
1297 if (FRAMEP (internal_last_event_frame)
1298 && !EQ (internal_last_event_frame, selected_frame))
1299 Fselect_frame (internal_last_event_frame, Qnil);
1300 #endif
1301 /* If it has changed current-menubar from previous value,
1302 really recompute the menubar from the value. */
1303 if (! NILP (Vlucid_menu_bar_dirty_flag)
1304 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1305 call0 (Qrecompute_lucid_menubar);
1307 before_command_key_count = this_command_key_count;
1308 before_command_echo_length = echo_length ();
1310 Vthis_command = Qnil;
1311 real_this_command = Qnil;
1313 /* Read next key sequence; i gets its length. */
1314 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1315 Qnil, 0, 1, 1);
1317 /* A filter may have run while we were reading the input. */
1318 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1319 Fkill_emacs (Qnil);
1320 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1321 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1323 ++num_input_keys;
1325 /* Now we have read a key sequence of length I,
1326 or else I is 0 and we found end of file. */
1328 if (i == 0) /* End of file -- happens only in */
1329 return Qnil; /* a kbd macro, at the end. */
1330 /* -1 means read_key_sequence got a menu that was rejected.
1331 Just loop around and read another command. */
1332 if (i == -1)
1334 cancel_echoing ();
1335 this_command_key_count = 0;
1336 this_single_command_key_start = 0;
1337 goto finalize;
1340 last_command_char = keybuf[i - 1];
1342 /* If the previous command tried to force a specific window-start,
1343 forget about that, in case this command moves point far away
1344 from that position. But also throw away beg_unchanged and
1345 end_unchanged information in that case, so that redisplay will
1346 update the whole window properly. */
1347 if (!NILP (XWINDOW (selected_window)->force_start))
1349 struct buffer *b;
1350 XWINDOW (selected_window)->force_start = Qnil;
1351 b = XBUFFER (XWINDOW (selected_window)->buffer);
1352 BUF_BEG_UNCHANGED (b) = BUF_END_UNCHANGED (b) = 0;
1355 cmd = read_key_sequence_cmd;
1356 if (!NILP (Vexecuting_macro))
1358 if (!NILP (Vquit_flag))
1360 Vexecuting_macro = Qt;
1361 QUIT; /* Make some noise. */
1362 /* Will return since macro now empty. */
1366 /* Do redisplay processing after this command except in special
1367 cases identified below that set no_redisplay to 1.
1368 (actually, there's currently no way to prevent the redisplay,
1369 and no_redisplay is ignored.
1370 Perhaps someday we will really implement it.) */
1371 no_redisplay = 0;
1373 prev_buffer = current_buffer;
1374 prev_modiff = MODIFF;
1375 last_point_position = PT;
1376 XSETBUFFER (last_point_position_buffer, prev_buffer);
1378 /* Execute the command. */
1380 Vthis_command = cmd;
1381 real_this_command = cmd;
1382 /* Note that the value cell will never directly contain nil
1383 if the symbol is a local variable. */
1384 if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
1385 safe_run_hooks (Qpre_command_hook);
1387 if (NILP (Vthis_command))
1389 /* nil means key is undefined. */
1390 bitch_at_user ();
1391 current_kboard->defining_kbd_macro = Qnil;
1392 update_mode_lines = 1;
1393 current_kboard->Vprefix_arg = Qnil;
1395 else
1397 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1399 /* In case we jump to directly_done. */
1400 Vcurrent_prefix_arg = current_kboard->Vprefix_arg;
1402 /* Recognize some common commands in common situations and
1403 do them directly. */
1404 if (EQ (Vthis_command, Qforward_char) && PT < ZV)
1406 struct Lisp_Char_Table *dp
1407 = window_display_table (XWINDOW (selected_window));
1408 lose = FETCH_CHAR (PT_BYTE);
1409 SET_PT (PT + 1);
1410 if ((dp
1411 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1412 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1413 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1414 && (lose >= 0x20 && lose < 0x7f)))
1415 : (lose >= 0x20 && lose < 0x7f))
1416 /* To extract the case of continuation on
1417 wide-column characters. */
1418 && (WIDTH_BY_CHAR_HEAD (FETCH_BYTE (PT_BYTE)) == 1)
1419 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1420 >= MODIFF)
1421 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1422 >= OVERLAY_MODIFF)
1423 && (XFASTINT (XWINDOW (selected_window)->last_point)
1424 == PT - 1)
1425 && !windows_or_buffers_changed
1426 && EQ (current_buffer->selective_display, Qnil)
1427 && !detect_input_pending ()
1428 && NILP (XWINDOW (selected_window)->column_number_displayed)
1429 && NILP (Vexecuting_macro))
1430 no_redisplay = direct_output_forward_char (1);
1431 goto directly_done;
1433 else if (EQ (Vthis_command, Qbackward_char) && PT > BEGV)
1435 struct Lisp_Char_Table *dp
1436 = window_display_table (XWINDOW (selected_window));
1437 SET_PT (PT - 1);
1438 lose = FETCH_CHAR (PT_BYTE);
1439 if ((dp
1440 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1441 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1442 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1443 && (lose >= 0x20 && lose < 0x7f)))
1444 : (lose >= 0x20 && lose < 0x7f))
1445 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1446 >= MODIFF)
1447 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1448 >= OVERLAY_MODIFF)
1449 && (XFASTINT (XWINDOW (selected_window)->last_point)
1450 == PT + 1)
1451 && !windows_or_buffers_changed
1452 && EQ (current_buffer->selective_display, Qnil)
1453 && !detect_input_pending ()
1454 && NILP (XWINDOW (selected_window)->column_number_displayed)
1455 && NILP (Vexecuting_macro))
1456 no_redisplay = direct_output_forward_char (-1);
1457 goto directly_done;
1459 else if (EQ (Vthis_command, Qself_insert_command)
1460 /* Try this optimization only on ascii keystrokes. */
1461 && INTEGERP (last_command_char))
1463 unsigned int c = XINT (last_command_char);
1464 int value;
1465 if (NILP (Vexecuting_macro)
1466 && !EQ (minibuf_window, selected_window))
1468 if (!nonundocount || nonundocount >= 20)
1470 Fundo_boundary ();
1471 nonundocount = 0;
1473 nonundocount++;
1476 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1477 < MODIFF)
1478 || (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1479 < OVERLAY_MODIFF)
1480 || (XFASTINT (XWINDOW (selected_window)->last_point)
1481 != PT)
1482 || MODIFF <= SAVE_MODIFF
1483 || windows_or_buffers_changed
1484 || !EQ (current_buffer->selective_display, Qnil)
1485 || detect_input_pending ()
1486 || !NILP (XWINDOW (selected_window)->column_number_displayed)
1487 || !NILP (Vexecuting_macro));
1489 value = internal_self_insert (c, 0);
1491 if (value == 2)
1492 nonundocount = 0;
1494 /* VALUE == 1 when AFTER-CHANGE functions are
1495 installed which is the case most of the time
1496 because FONT-LOCK installs one. */
1497 if (!lose && !value)
1498 no_redisplay = direct_output_for_insert (c);
1499 goto directly_done;
1503 /* Here for a command that isn't executed directly */
1505 #ifdef HAVE_X_WINDOWS
1506 if (display_busy_cursor_p)
1508 if (inhibit_busy_cursor != 2)
1509 inhibit_busy_cursor = 0;
1510 if (!inhibit_busy_cursor)
1511 Fx_show_busy_cursor ();
1513 #endif
1515 nonundocount = 0;
1516 if (NILP (current_kboard->Vprefix_arg))
1517 Fundo_boundary ();
1518 Fcommand_execute (Vthis_command, Qnil, Qnil, Qnil);
1520 directly_done: ;
1521 current_kboard->Vlast_prefix_arg = Vcurrent_prefix_arg;
1523 /* Note that the value cell will never directly contain nil
1524 if the symbol is a local variable. */
1525 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1526 safe_run_hooks (Qpost_command_hook);
1528 /* If displaying a message, resize the echo area window to fit
1529 that message's size exactly. */
1530 if (!NILP (echo_area_buffer[0]))
1531 resize_echo_area_axactly ();
1533 if (!NILP (Vdeferred_action_list))
1534 safe_run_hooks (Qdeferred_action_function);
1536 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1538 if (NILP (Vunread_command_events)
1539 && NILP (Vunread_input_method_events)
1540 && NILP (Vunread_post_input_method_events)
1541 && NILP (Vexecuting_macro)
1542 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1543 safe_run_hooks (Qpost_command_idle_hook);
1546 /* If there is a prefix argument,
1547 1) We don't want Vlast_command to be ``universal-argument''
1548 (that would be dumb), so don't set Vlast_command,
1549 2) we want to leave echoing on so that the prefix will be
1550 echoed as part of this key sequence, so don't call
1551 cancel_echoing, and
1552 3) we want to leave this_command_key_count non-zero, so that
1553 read_char will realize that it is re-reading a character, and
1554 not echo it a second time.
1556 If the command didn't actually create a prefix arg,
1557 but is merely a frame event that is transparent to prefix args,
1558 then the above doesn't apply. */
1559 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1561 current_kboard->Vlast_command = Vthis_command;
1562 current_kboard->Vreal_last_command = real_this_command;
1563 cancel_echoing ();
1564 this_command_key_count = 0;
1565 this_single_command_key_start = 0;
1568 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1570 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1572 current_buffer->mark_active = Qnil;
1573 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1575 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1576 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1579 finalize:
1580 /* Install chars successfully executed in kbd macro. */
1582 if (!NILP (current_kboard->defining_kbd_macro)
1583 && NILP (current_kboard->Vprefix_arg))
1584 finalize_kbd_macro_chars ();
1586 #ifdef MULTI_KBOARD
1587 if (!was_locked)
1588 any_kboard_state ();
1589 #endif
1593 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1595 static Lisp_Object
1596 safe_run_hooks_1 (hook)
1597 Lisp_Object hook;
1599 return call1 (Vrun_hooks, Vinhibit_quit);
1602 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1604 static Lisp_Object
1605 safe_run_hooks_error (data)
1606 Lisp_Object data;
1608 Fset (Vinhibit_quit, Qnil);
1611 /* If we get an error while running the hook, cause the hook variable
1612 to be nil. Also inhibit quits, so that C-g won't cause the hook
1613 to mysteriously evaporate. */
1615 void
1616 safe_run_hooks (hook)
1617 Lisp_Object hook;
1619 int count = specpdl_ptr - specpdl;
1620 specbind (Qinhibit_quit, hook);
1622 internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
1624 unbind_to (count, Qnil);
1627 /* Number of seconds between polling for input. */
1628 int polling_period;
1630 /* Nonzero means polling for input is temporarily suppressed. */
1631 int poll_suppress_count;
1633 /* Nonzero if polling_for_input is actually being used. */
1634 int polling_for_input;
1636 #ifdef POLL_FOR_INPUT
1638 /* Handle an alarm once each second and read pending input
1639 so as to handle a C-g if it comces in. */
1641 SIGTYPE
1642 input_poll_signal (signalnum) /* If we don't have an argument, */
1643 int signalnum; /* some compilers complain in signal calls. */
1645 /* This causes the call to start_polling at the end
1646 to do its job. It also arranges for a quit or error
1647 from within read_avail_input to resume polling. */
1648 poll_suppress_count++;
1649 if (interrupt_input_blocked == 0
1650 && !waiting_for_input)
1651 read_avail_input (0);
1652 /* Turn on the SIGALRM handler and request another alarm. */
1653 start_polling ();
1656 #endif
1658 /* Begin signals to poll for input, if they are appropriate.
1659 This function is called unconditionally from various places. */
1661 void
1662 start_polling ()
1664 #ifdef POLL_FOR_INPUT
1665 if (read_socket_hook && !interrupt_input)
1667 poll_suppress_count--;
1668 if (poll_suppress_count == 0)
1670 signal (SIGALRM, input_poll_signal);
1671 polling_for_input = 1;
1672 alarm (polling_period);
1675 #endif
1678 /* Nonzero if we are using polling to handle input asynchronously. */
1681 input_polling_used ()
1683 #ifdef POLL_FOR_INPUT
1684 return read_socket_hook && !interrupt_input;
1685 #else
1686 return 0;
1687 #endif
1690 /* Turn off polling. */
1692 void
1693 stop_polling ()
1695 #ifdef POLL_FOR_INPUT
1696 if (read_socket_hook && !interrupt_input)
1698 if (poll_suppress_count == 0)
1700 polling_for_input = 0;
1701 alarm (0);
1703 poll_suppress_count++;
1705 #endif
1708 /* Set the value of poll_suppress_count to COUNT
1709 and start or stop polling accordingly. */
1711 void
1712 set_poll_suppress_count (count)
1713 int count;
1715 #ifdef POLL_FOR_INPUT
1716 if (count == 0 && poll_suppress_count != 0)
1718 poll_suppress_count = 1;
1719 start_polling ();
1721 else if (count != 0 && poll_suppress_count == 0)
1723 stop_polling ();
1725 poll_suppress_count = count;
1726 #endif
1729 /* Bind polling_period to a value at least N.
1730 But don't decrease it. */
1732 void
1733 bind_polling_period (n)
1734 int n;
1736 #ifdef POLL_FOR_INPUT
1737 int new = polling_period;
1739 if (n > new)
1740 new = n;
1742 stop_polling ();
1743 specbind (Qpolling_period, make_number (new));
1744 /* Start a new alarm with the new period. */
1745 start_polling ();
1746 #endif
1749 /* Apply the control modifier to CHARACTER. */
1752 make_ctrl_char (c)
1753 int c;
1755 /* Save the upper bits here. */
1756 int upper = c & ~0177;
1758 c &= 0177;
1760 /* Everything in the columns containing the upper-case letters
1761 denotes a control character. */
1762 if (c >= 0100 && c < 0140)
1764 int oc = c;
1765 c &= ~0140;
1766 /* Set the shift modifier for a control char
1767 made from a shifted letter. But only for letters! */
1768 if (oc >= 'A' && oc <= 'Z')
1769 c |= shift_modifier;
1772 /* The lower-case letters denote control characters too. */
1773 else if (c >= 'a' && c <= 'z')
1774 c &= ~0140;
1776 /* Include the bits for control and shift
1777 only if the basic ASCII code can't indicate them. */
1778 else if (c >= ' ')
1779 c |= ctrl_modifier;
1781 /* Replace the high bits. */
1782 c |= (upper & ~ctrl_modifier);
1784 return c;
1789 /* Input of single characters from keyboard */
1791 Lisp_Object print_help ();
1792 static Lisp_Object kbd_buffer_get_event ();
1793 static void record_char ();
1795 #ifdef MULTI_KBOARD
1796 static jmp_buf wrong_kboard_jmpbuf;
1797 #endif
1799 /* read a character from the keyboard; call the redisplay if needed */
1800 /* commandflag 0 means do not do auto-saving, but do do redisplay.
1801 -1 means do not do redisplay, but do do autosaving.
1802 1 means do both. */
1804 /* The arguments MAPS and NMAPS are for menu prompting.
1805 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1807 PREV_EVENT is the previous input event, or nil if we are reading
1808 the first event of a key sequence (or not reading a key sequence).
1809 If PREV_EVENT is t, that is a "magic" value that says
1810 not to run input methods, but in other respects to act as if
1811 not reading a key sequence.
1813 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
1814 if we used a mouse menu to read the input, or zero otherwise. If
1815 USED_MOUSE_MENU is null, we don't dereference it.
1817 Value is t if we showed a menu and the user rejected it. */
1819 Lisp_Object
1820 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
1821 int commandflag;
1822 int nmaps;
1823 Lisp_Object *maps;
1824 Lisp_Object prev_event;
1825 int *used_mouse_menu;
1827 Lisp_Object c;
1828 int count;
1829 jmp_buf local_getcjmp;
1830 jmp_buf save_jump;
1831 int key_already_recorded = 0;
1832 Lisp_Object tem, save;
1833 Lisp_Object previous_echo_area_message;
1834 Lisp_Object also_record;
1835 int reread;
1836 struct gcpro gcpro1, gcpro2;
1838 also_record = Qnil;
1840 before_command_key_count = this_command_key_count;
1841 before_command_echo_length = echo_length ();
1842 c = Qnil;
1843 previous_echo_area_message = Qnil;
1845 GCPRO2 (c, previous_echo_area_message);
1847 retry:
1849 reread = 0;
1850 if (CONSP (Vunread_post_input_method_events))
1852 c = XCAR (Vunread_post_input_method_events);
1853 Vunread_post_input_method_events
1854 = XCDR (Vunread_post_input_method_events);
1856 /* Undo what read_char_x_menu_prompt did when it unread
1857 additional keys returned by Fx_popup_menu. */
1858 if (CONSP (c)
1859 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
1860 && NILP (XCDR (c)))
1861 c = XCAR (c);
1863 reread = 1;
1864 goto reread_first;
1867 if (unread_command_char != -1)
1869 XSETINT (c, unread_command_char);
1870 unread_command_char = -1;
1872 reread = 1;
1873 goto reread_first;
1876 if (CONSP (Vunread_command_events))
1878 c = XCAR (Vunread_command_events);
1879 Vunread_command_events = XCDR (Vunread_command_events);
1881 /* Undo what read_char_x_menu_prompt did when it unread
1882 additional keys returned by Fx_popup_menu. */
1883 if (CONSP (c)
1884 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
1885 && NILP (XCDR (c)))
1886 c = XCAR (c);
1888 reread = 1;
1889 goto reread_for_input_method;
1892 if (CONSP (Vunread_input_method_events))
1894 c = XCAR (Vunread_input_method_events);
1895 Vunread_input_method_events = XCDR (Vunread_input_method_events);
1897 /* Undo what read_char_x_menu_prompt did when it unread
1898 additional keys returned by Fx_popup_menu. */
1899 if (CONSP (c)
1900 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
1901 && NILP (XCDR (c)))
1902 c = XCAR (c);
1903 reread = 1;
1904 goto reread_for_input_method;
1907 /* If there is no function key translated before
1908 reset-this-command-lengths takes effect, forget about it. */
1909 before_command_restore_flag = 0;
1911 if (!NILP (Vexecuting_macro))
1913 /* We set this to Qmacro; since that's not a frame, nobody will
1914 try to switch frames on us, and the selected window will
1915 remain unchanged.
1917 Since this event came from a macro, it would be misleading to
1918 leave internal_last_event_frame set to wherever the last
1919 real event came from. Normally, a switch-frame event selects
1920 internal_last_event_frame after each command is read, but
1921 events read from a macro should never cause a new frame to be
1922 selected. */
1923 Vlast_event_frame = internal_last_event_frame = Qmacro;
1925 /* Exit the macro if we are at the end.
1926 Also, some things replace the macro with t
1927 to force an early exit. */
1928 if (EQ (Vexecuting_macro, Qt)
1929 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
1931 XSETINT (c, -1);
1932 RETURN_UNGCPRO (c);
1935 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
1936 if (STRINGP (Vexecuting_macro)
1937 && (XINT (c) & 0x80))
1938 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
1940 executing_macro_index++;
1942 goto from_macro;
1945 if (!NILP (unread_switch_frame))
1947 c = unread_switch_frame;
1948 unread_switch_frame = Qnil;
1950 /* This event should make it into this_command_keys, and get echoed
1951 again, so we do not set `reread'. */
1952 goto reread_first;
1955 /* if redisplay was requested */
1956 if (commandflag >= 0)
1958 /* If there is pending input, process any events which are not
1959 user-visible, such as X selection_request events. */
1960 if (input_pending
1961 || detect_input_pending_run_timers (0))
1962 swallow_events (0); /* may clear input_pending */
1964 /* Redisplay if no pending input. */
1965 while (!input_pending)
1967 redisplay ();
1969 if (!input_pending)
1970 /* Normal case: no input arrived during redisplay. */
1971 break;
1973 /* Input arrived and pre-empted redisplay.
1974 Process any events which are not user-visible. */
1975 swallow_events (0);
1976 /* If that cleared input_pending, try again to redisplay. */
1980 /* Message turns off echoing unless more keystrokes turn it on again. */
1981 if (/* There is a current message. */
1982 !NILP (echo_area_buffer[0])
1983 /* And we're not echoing from this kboard. */
1984 && echo_kboard != current_kboard
1985 /* And it's either not ok to echo (ok_to_echo == NULL), or the
1986 last char echoed was from a different kboard. */
1987 && ok_to_echo_at_next_pause != echo_kboard)
1988 cancel_echoing ();
1989 else
1990 /* If already echoing, continue. */
1991 echo_dash ();
1993 /* Try reading a character via menu prompting in the minibuf.
1994 Try this before the sit-for, because the sit-for
1995 would do the wrong thing if we are supposed to do
1996 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
1997 after a mouse event so don't try a minibuf menu. */
1998 c = Qnil;
1999 if (nmaps > 0 && INTERACTIVE
2000 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
2001 /* Don't bring up a menu if we already have another event. */
2002 && NILP (Vunread_command_events)
2003 && unread_command_char < 0
2004 && !detect_input_pending_run_timers (0))
2006 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
2007 if (! NILP (c))
2009 key_already_recorded = 1;
2010 goto non_reread_1;
2014 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
2015 We will do that below, temporarily for short sections of code,
2016 when appropriate. local_getcjmp must be in effect
2017 around any call to sit_for or kbd_buffer_get_event;
2018 it *must not* be in effect when we call redisplay. */
2020 if (_setjmp (local_getcjmp))
2022 XSETINT (c, quit_char);
2023 internal_last_event_frame = selected_frame;
2024 Vlast_event_frame = internal_last_event_frame;
2025 /* If we report the quit char as an event,
2026 don't do so more than once. */
2027 if (!NILP (Vinhibit_quit))
2028 Vquit_flag = Qnil;
2030 #ifdef MULTI_KBOARD
2032 KBOARD *kb = FRAME_KBOARD (XFRAME (selected_frame));
2033 if (kb != current_kboard)
2035 Lisp_Object *tailp = &kb->kbd_queue;
2036 /* We shouldn't get here if we were in single-kboard mode! */
2037 if (single_kboard)
2038 abort ();
2039 while (CONSP (*tailp))
2040 tailp = &XCDR (*tailp);
2041 if (!NILP (*tailp))
2042 abort ();
2043 *tailp = Fcons (c, Qnil);
2044 kb->kbd_queue_has_data = 1;
2045 current_kboard = kb;
2046 /* This is going to exit from read_char
2047 so we had better get rid of this frame's stuff. */
2048 UNGCPRO;
2049 longjmp (wrong_kboard_jmpbuf, 1);
2052 #endif
2053 goto non_reread;
2056 timer_start_idle ();
2058 /* If in middle of key sequence and minibuffer not active,
2059 start echoing if enough time elapses. */
2061 if (minibuf_level == 0
2062 && !current_kboard->immediate_echo
2063 && this_command_key_count > 0
2064 && ! noninteractive
2065 && echo_keystrokes > 0
2066 && (/* No message. */
2067 NILP (echo_area_buffer[0])
2068 /* Or empty message. */
2069 || (BUF_BEG (XBUFFER (echo_area_buffer[0]))
2070 == BUF_Z (XBUFFER (echo_area_buffer[0])))
2071 /* Or already echoing from same kboard. */
2072 || (echo_kboard && ok_to_echo_at_next_pause == echo_kboard)
2073 /* Or not echoing before and echoing allowed. */
2074 || (!echo_kboard && ok_to_echo_at_next_pause)))
2076 Lisp_Object tem0;
2078 /* After a mouse event, start echoing right away.
2079 This is because we are probably about to display a menu,
2080 and we don't want to delay before doing so. */
2081 if (EVENT_HAS_PARAMETERS (prev_event))
2082 echo_now ();
2083 else
2085 save_getcjmp (save_jump);
2086 restore_getcjmp (local_getcjmp);
2087 tem0 = sit_for (echo_keystrokes, 0, 1, 1, 0);
2088 restore_getcjmp (save_jump);
2089 if (EQ (tem0, Qt)
2090 && ! CONSP (Vunread_command_events))
2091 echo_now ();
2095 /* Maybe auto save due to number of keystrokes. */
2097 if (commandflag != 0
2098 && auto_save_interval > 0
2099 && num_nonmacro_input_events - last_auto_save > max (auto_save_interval, 20)
2100 && !detect_input_pending_run_timers (0))
2102 Fdo_auto_save (Qnil, Qnil);
2103 /* Hooks can actually change some buffers in auto save. */
2104 redisplay ();
2107 /* Try reading using an X menu.
2108 This is never confused with reading using the minibuf
2109 because the recursive call of read_char in read_char_minibuf_menu_prompt
2110 does not pass on any keymaps. */
2112 if (nmaps > 0 && INTERACTIVE
2113 && !NILP (prev_event)
2114 && EVENT_HAS_PARAMETERS (prev_event)
2115 && !EQ (XCAR (prev_event), Qmenu_bar)
2116 && !EQ (XCAR (prev_event), Qtool_bar)
2117 /* Don't bring up a menu if we already have another event. */
2118 && NILP (Vunread_command_events)
2119 && unread_command_char < 0)
2121 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
2123 /* Now that we have read an event, Emacs is not idle. */
2124 timer_stop_idle ();
2126 RETURN_UNGCPRO (c);
2129 /* Maybe autosave and/or garbage collect due to idleness. */
2131 if (INTERACTIVE && NILP (c))
2133 int delay_level, buffer_size;
2135 /* Slow down auto saves logarithmically in size of current buffer,
2136 and garbage collect while we're at it. */
2137 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
2138 last_non_minibuf_size = Z - BEG;
2139 buffer_size = (last_non_minibuf_size >> 8) + 1;
2140 delay_level = 0;
2141 while (buffer_size > 64)
2142 delay_level++, buffer_size -= buffer_size >> 2;
2143 if (delay_level < 4) delay_level = 4;
2144 /* delay_level is 4 for files under around 50k, 7 at 100k,
2145 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
2147 /* Auto save if enough time goes by without input. */
2148 if (commandflag != 0
2149 && num_nonmacro_input_events > last_auto_save
2150 && INTEGERP (Vauto_save_timeout)
2151 && XINT (Vauto_save_timeout) > 0)
2153 Lisp_Object tem0;
2155 save_getcjmp (save_jump);
2156 restore_getcjmp (local_getcjmp);
2157 tem0 = sit_for (delay_level * XFASTINT (Vauto_save_timeout) / 4,
2158 0, 1, 1, 0);
2159 restore_getcjmp (save_jump);
2161 if (EQ (tem0, Qt)
2162 && ! CONSP (Vunread_command_events))
2164 Fdo_auto_save (Qnil, Qnil);
2166 /* If we have auto-saved and there is still no input
2167 available, garbage collect if there has been enough
2168 consing going on to make it worthwhile. */
2169 if (!detect_input_pending_run_timers (0)
2170 && consing_since_gc > gc_cons_threshold / 2)
2171 Fgarbage_collect ();
2173 redisplay ();
2178 /* If this has become non-nil here, it has been set by a timer
2179 or sentinel or filter. */
2180 if (CONSP (Vunread_command_events))
2182 c = XCAR (Vunread_command_events);
2183 Vunread_command_events = XCDR (Vunread_command_events);
2186 /* Read something from current KBOARD's side queue, if possible. */
2188 if (NILP (c))
2190 if (current_kboard->kbd_queue_has_data)
2192 if (!CONSP (current_kboard->kbd_queue))
2193 abort ();
2194 c = XCAR (current_kboard->kbd_queue);
2195 current_kboard->kbd_queue
2196 = XCDR (current_kboard->kbd_queue);
2197 if (NILP (current_kboard->kbd_queue))
2198 current_kboard->kbd_queue_has_data = 0;
2199 input_pending = readable_events (0);
2200 if (EVENT_HAS_PARAMETERS (c)
2201 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
2202 internal_last_event_frame = XCAR (XCDR (c));
2203 Vlast_event_frame = internal_last_event_frame;
2207 #ifdef MULTI_KBOARD
2208 /* If current_kboard's side queue is empty check the other kboards.
2209 If one of them has data that we have not yet seen here,
2210 switch to it and process the data waiting for it.
2212 Note: if the events queued up for another kboard
2213 have already been seen here, and therefore are not a complete command,
2214 the kbd_queue_has_data field is 0, so we skip that kboard here.
2215 That's to avoid an infinite loop switching between kboards here. */
2216 if (NILP (c) && !single_kboard)
2218 KBOARD *kb;
2219 for (kb = all_kboards; kb; kb = kb->next_kboard)
2220 if (kb->kbd_queue_has_data)
2222 current_kboard = kb;
2223 /* This is going to exit from read_char
2224 so we had better get rid of this frame's stuff. */
2225 UNGCPRO;
2226 longjmp (wrong_kboard_jmpbuf, 1);
2229 #endif
2231 wrong_kboard:
2233 stop_polling ();
2235 /* Finally, we read from the main queue,
2236 and if that gives us something we can't use yet, we put it on the
2237 appropriate side queue and try again. */
2239 if (NILP (c))
2241 KBOARD *kb;
2243 /* Actually read a character, waiting if necessary. */
2244 save_getcjmp (save_jump);
2245 restore_getcjmp (local_getcjmp);
2246 c = kbd_buffer_get_event (&kb, used_mouse_menu);
2247 restore_getcjmp (save_jump);
2249 #ifdef MULTI_KBOARD
2250 if (! NILP (c) && (kb != current_kboard))
2252 Lisp_Object *tailp = &kb->kbd_queue;
2253 while (CONSP (*tailp))
2254 tailp = &XCDR (*tailp);
2255 if (!NILP (*tailp))
2256 abort ();
2257 *tailp = Fcons (c, Qnil);
2258 kb->kbd_queue_has_data = 1;
2259 c = Qnil;
2260 if (single_kboard)
2261 goto wrong_kboard;
2262 current_kboard = kb;
2263 /* This is going to exit from read_char
2264 so we had better get rid of this frame's stuff. */
2265 UNGCPRO;
2266 longjmp (wrong_kboard_jmpbuf, 1);
2268 #endif
2271 /* Terminate Emacs in batch mode if at eof. */
2272 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
2273 Fkill_emacs (make_number (1));
2275 if (INTEGERP (c))
2277 /* Add in any extra modifiers, where appropriate. */
2278 if ((extra_keyboard_modifiers & CHAR_CTL)
2279 || ((extra_keyboard_modifiers & 0177) < ' '
2280 && (extra_keyboard_modifiers & 0177) != 0))
2281 XSETINT (c, make_ctrl_char (XINT (c)));
2283 /* Transfer any other modifier bits directly from
2284 extra_keyboard_modifiers to c. Ignore the actual character code
2285 in the low 16 bits of extra_keyboard_modifiers. */
2286 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
2289 non_reread:
2291 timer_stop_idle ();
2293 start_polling ();
2295 if (NILP (c))
2297 if (commandflag >= 0
2298 && !input_pending && !detect_input_pending_run_timers (0))
2299 redisplay ();
2301 goto wrong_kboard;
2304 non_reread_1:
2306 /* Buffer switch events are only for internal wakeups
2307 so don't show them to the user.
2308 Also, don't record a key if we already did. */
2309 if (BUFFERP (c) || key_already_recorded)
2310 RETURN_UNGCPRO (c);
2312 /* Process special events within read_char
2313 and loop around to read another event. */
2314 save = Vquit_flag;
2315 Vquit_flag = Qnil;
2316 tem = get_keyelt (access_keymap (get_keymap_1 (Vspecial_event_map, 0, 0),
2317 c, 0, 0), 1);
2318 Vquit_flag = save;
2320 if (!NILP (tem))
2322 int was_locked = single_kboard;
2324 last_input_char = c;
2325 Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt);
2327 /* Resume allowing input from any kboard, if that was true before. */
2328 if (!was_locked)
2329 any_kboard_state ();
2331 goto retry;
2334 /* Handle things that only apply to characters. */
2335 if (INTEGERP (c))
2337 /* If kbd_buffer_get_event gave us an EOF, return that. */
2338 if (XINT (c) == -1)
2339 RETURN_UNGCPRO (c);
2341 if ((STRINGP (Vkeyboard_translate_table)
2342 && XSTRING (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2343 || (VECTORP (Vkeyboard_translate_table)
2344 && XVECTOR (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2345 || (CHAR_TABLE_P (Vkeyboard_translate_table)
2346 && CHAR_TABLE_ORDINARY_SLOTS > (unsigned) XFASTINT (c)))
2348 Lisp_Object d;
2349 d = Faref (Vkeyboard_translate_table, c);
2350 /* nil in keyboard-translate-table means no translation. */
2351 if (!NILP (d))
2352 c = d;
2356 /* If this event is a mouse click in the menu bar,
2357 return just menu-bar for now. Modify the mouse click event
2358 so we won't do this twice, then queue it up. */
2359 if (EVENT_HAS_PARAMETERS (c)
2360 && CONSP (XCDR (c))
2361 && CONSP (EVENT_START (c))
2362 && CONSP (XCDR (EVENT_START (c))))
2364 Lisp_Object posn;
2366 posn = POSN_BUFFER_POSN (EVENT_START (c));
2367 /* Handle menu-bar events:
2368 insert the dummy prefix event `menu-bar'. */
2369 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
2371 /* Change menu-bar to (menu-bar) as the event "position". */
2372 POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
2374 also_record = c;
2375 Vunread_command_events = Fcons (c, Vunread_command_events);
2376 c = posn;
2380 /* Store these characters into recent_keys, the dribble file if any,
2381 and the keyboard macro being defined, if any. */
2382 record_char (c);
2383 if (! NILP (also_record))
2384 record_char (also_record);
2386 /* Wipe the echo area.
2387 But first, if we are about to use an input method,
2388 save the echo area contents for it to refer to. */
2389 if (INTEGERP (c)
2390 && ! NILP (Vinput_method_function)
2391 && (unsigned) XINT (c) >= ' '
2392 && (unsigned) XINT (c) < 127)
2394 previous_echo_area_message = Fcurrent_message ();
2395 Vinput_method_previous_message = previous_echo_area_message;
2398 /* Now wipe the echo area. */
2399 if (!NILP (echo_area_buffer[0]))
2400 safe_run_hooks (Qecho_area_clear_hook);
2401 clear_message (1, 0);
2403 reread_for_input_method:
2404 from_macro:
2405 /* Pass this to the input method, if appropriate. */
2406 if (INTEGERP (c)
2407 && ! NILP (Vinput_method_function)
2408 /* Don't run the input method within a key sequence,
2409 after the first event of the key sequence. */
2410 && NILP (prev_event)
2411 && (unsigned) XINT (c) >= ' '
2412 && (unsigned) XINT (c) < 127)
2414 Lisp_Object keys;
2415 int key_count;
2416 struct gcpro gcpro1;
2417 int count = specpdl_ptr - specpdl;
2419 /* Save the echo status. */
2420 int saved_immediate_echo = current_kboard->immediate_echo;
2421 struct kboard *saved_ok_to_echo = ok_to_echo_at_next_pause;
2422 int saved_echo_after_prompt = current_kboard->echo_after_prompt;
2424 if (before_command_restore_flag)
2426 this_command_key_count = before_command_key_count_1;
2427 if (this_command_key_count < this_single_command_key_start)
2428 this_single_command_key_start = this_command_key_count;
2429 echo_truncate (before_command_echo_length_1);
2430 before_command_restore_flag = 0;
2433 /* Save the this_command_keys status. */
2434 key_count = this_command_key_count;
2436 if (key_count > 0)
2437 keys = Fcopy_sequence (this_command_keys);
2438 else
2439 keys = Qnil;
2440 GCPRO1 (keys);
2442 /* Clear out this_command_keys. */
2443 this_command_key_count = 0;
2445 /* Now wipe the echo area. */
2446 if (!NILP (echo_area_buffer[0]))
2447 safe_run_hooks (Qecho_area_clear_hook);
2448 clear_message (1, 0);
2449 echo_truncate (0);
2451 /* If we are not reading a key sequence,
2452 never use the echo area. */
2453 if (maps == 0)
2455 specbind (Qinput_method_use_echo_area, Qt);
2458 /* Call the input method. */
2459 tem = call1 (Vinput_method_function, c);
2461 tem = unbind_to (count, tem);
2463 /* Restore the saved echoing state
2464 and this_command_keys state. */
2465 this_command_key_count = key_count;
2466 if (key_count > 0)
2467 this_command_keys = keys;
2469 cancel_echoing ();
2470 ok_to_echo_at_next_pause = saved_ok_to_echo;
2471 current_kboard->echo_after_prompt = saved_echo_after_prompt;
2472 if (saved_immediate_echo)
2473 echo_now ();
2475 UNGCPRO;
2477 /* The input method can return no events. */
2478 if (! CONSP (tem))
2480 /* Bring back the previous message, if any. */
2481 if (! NILP (previous_echo_area_message))
2482 message_with_string ("%s", previous_echo_area_message, 0);
2483 goto retry;
2485 /* It returned one event or more. */
2486 c = XCAR (tem);
2487 Vunread_post_input_method_events
2488 = nconc2 (XCDR (tem), Vunread_post_input_method_events);
2491 reread_first:
2493 /* Display help if not echoing. */
2494 if (CONSP (c)
2495 && EQ (XCAR (c), Qhelp_echo))
2497 Lisp_Object msg = XCDR (XCDR (c));
2499 if (!NILP (Vshow_help_function))
2500 call1 (Vshow_help_function, msg);
2501 else if (!echoing && !MINI_WINDOW_P (XWINDOW (selected_window)))
2503 if (STRINGP (msg))
2504 message3_nolog (msg, XSTRING (msg)->size, STRING_MULTIBYTE (msg));
2505 else
2506 message (0);
2509 goto retry;
2512 if (this_command_key_count == 0 || ! reread)
2514 before_command_key_count = this_command_key_count;
2515 before_command_echo_length = echo_length ();
2517 /* Don't echo mouse motion events. */
2518 if (echo_keystrokes
2519 && ! (EVENT_HAS_PARAMETERS (c)
2520 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
2522 echo_char (c);
2523 if (! NILP (also_record))
2524 echo_char (also_record);
2525 /* Once we reread a character, echoing can happen
2526 the next time we pause to read a new one. */
2527 ok_to_echo_at_next_pause = current_kboard;
2530 /* Record this character as part of the current key. */
2531 add_command_key (c);
2532 if (! NILP (also_record))
2533 add_command_key (also_record);
2536 last_input_char = c;
2537 num_input_events++;
2539 /* Process the help character specially if enabled */
2540 if (!NILP (Vhelp_form) && help_char_p (c))
2542 Lisp_Object tem0;
2543 count = specpdl_ptr - specpdl;
2545 record_unwind_protect (Fset_window_configuration,
2546 Fcurrent_window_configuration (Qnil));
2548 tem0 = Feval (Vhelp_form);
2549 if (STRINGP (tem0))
2550 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
2552 cancel_echoing ();
2554 c = read_char (0, 0, 0, Qnil, 0);
2555 while (BUFFERP (c));
2556 /* Remove the help from the frame */
2557 unbind_to (count, Qnil);
2559 redisplay ();
2560 if (EQ (c, make_number (040)))
2562 cancel_echoing ();
2564 c = read_char (0, 0, 0, Qnil, 0);
2565 while (BUFFERP (c));
2569 RETURN_UNGCPRO (c);
2572 /* Record a key that came from a mouse menu.
2573 Record it for echoing, for this-command-keys, and so on. */
2575 static void
2576 record_menu_key (c)
2577 Lisp_Object c;
2579 /* Wipe the echo area. */
2580 clear_message (1, 0);
2582 record_char (c);
2584 before_command_key_count = this_command_key_count;
2585 before_command_echo_length = echo_length ();
2587 /* Don't echo mouse motion events. */
2588 if (echo_keystrokes)
2590 echo_char (c);
2592 /* Once we reread a character, echoing can happen
2593 the next time we pause to read a new one. */
2594 ok_to_echo_at_next_pause = 0;
2597 /* Record this character as part of the current key. */
2598 add_command_key (c);
2600 /* Re-reading in the middle of a command */
2601 last_input_char = c;
2602 num_input_events++;
2605 /* Return 1 if should recognize C as "the help character". */
2608 help_char_p (c)
2609 Lisp_Object c;
2611 Lisp_Object tail;
2613 if (EQ (c, Vhelp_char))
2614 return 1;
2615 for (tail = Vhelp_event_list; CONSP (tail); tail = XCDR (tail))
2616 if (EQ (c, XCAR (tail)))
2617 return 1;
2618 return 0;
2621 /* Record the input event C in various ways. */
2623 static void
2624 record_char (c)
2625 Lisp_Object c;
2627 total_keys++;
2628 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
2629 if (++recent_keys_index >= NUM_RECENT_KEYS)
2630 recent_keys_index = 0;
2632 /* Write c to the dribble file. If c is a lispy event, write
2633 the event's symbol to the dribble file, in <brackets>. Bleaugh.
2634 If you, dear reader, have a better idea, you've got the source. :-) */
2635 if (dribble)
2637 if (INTEGERP (c))
2639 if (XUINT (c) < 0x100)
2640 putc (XINT (c), dribble);
2641 else
2642 fprintf (dribble, " 0x%x", (int) XUINT (c));
2644 else
2646 Lisp_Object dribblee;
2648 /* If it's a structured event, take the event header. */
2649 dribblee = EVENT_HEAD (c);
2651 if (SYMBOLP (dribblee))
2653 putc ('<', dribble);
2654 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
2655 STRING_BYTES (XSYMBOL (dribblee)->name),
2656 dribble);
2657 putc ('>', dribble);
2661 fflush (dribble);
2664 store_kbd_macro_char (c);
2666 num_nonmacro_input_events++;
2669 Lisp_Object
2670 print_help (object)
2671 Lisp_Object object;
2673 struct buffer *old = current_buffer;
2674 Fprinc (object, Qnil);
2675 set_buffer_internal (XBUFFER (Vstandard_output));
2676 call0 (intern ("help-mode"));
2677 set_buffer_internal (old);
2678 return Qnil;
2681 /* Copy out or in the info on where C-g should throw to.
2682 This is used when running Lisp code from within get_char,
2683 in case get_char is called recursively.
2684 See read_process_output. */
2686 static void
2687 save_getcjmp (temp)
2688 jmp_buf temp;
2690 bcopy (getcjmp, temp, sizeof getcjmp);
2693 static void
2694 restore_getcjmp (temp)
2695 jmp_buf temp;
2697 bcopy (temp, getcjmp, sizeof getcjmp);
2700 #ifdef HAVE_MOUSE
2702 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
2703 of this function. */
2705 static Lisp_Object
2706 tracking_off (old_value)
2707 Lisp_Object old_value;
2709 do_mouse_tracking = old_value;
2710 if (NILP (old_value))
2712 /* Redisplay may have been preempted because there was input
2713 available, and it assumes it will be called again after the
2714 input has been processed. If the only input available was
2715 the sort that we have just disabled, then we need to call
2716 redisplay. */
2717 if (!readable_events (1))
2719 redisplay_preserve_echo_area ();
2720 get_input_pending (&input_pending, 1);
2725 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
2726 "Evaluate BODY with mouse movement events enabled.\n\
2727 Within a `track-mouse' form, mouse motion generates input events that\n\
2728 you can read with `read-event'.\n\
2729 Normally, mouse motion is ignored.")
2730 (args)
2731 Lisp_Object args;
2733 int count = specpdl_ptr - specpdl;
2734 Lisp_Object val;
2736 record_unwind_protect (tracking_off, do_mouse_tracking);
2738 do_mouse_tracking = Qt;
2740 val = Fprogn (args);
2741 return unbind_to (count, val);
2744 /* If mouse has moved on some frame, return one of those frames.
2745 Return 0 otherwise. */
2747 static FRAME_PTR
2748 some_mouse_moved ()
2750 Lisp_Object tail, frame;
2752 FOR_EACH_FRAME (tail, frame)
2754 if (XFRAME (frame)->mouse_moved)
2755 return XFRAME (frame);
2758 return 0;
2761 #endif /* HAVE_MOUSE */
2763 /* Low level keyboard/mouse input.
2764 kbd_buffer_store_event places events in kbd_buffer, and
2765 kbd_buffer_get_event retrieves them. */
2767 /* Return true iff there are any events in the queue that read-char
2768 would return. If this returns false, a read-char would block. */
2769 static int
2770 readable_events (do_timers_now)
2771 int do_timers_now;
2773 if (do_timers_now)
2774 timer_check (do_timers_now);
2776 if (kbd_fetch_ptr != kbd_store_ptr)
2777 return 1;
2778 #ifdef HAVE_MOUSE
2779 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2780 return 1;
2781 #endif
2782 if (single_kboard)
2784 if (current_kboard->kbd_queue_has_data)
2785 return 1;
2787 else
2789 KBOARD *kb;
2790 for (kb = all_kboards; kb; kb = kb->next_kboard)
2791 if (kb->kbd_queue_has_data)
2792 return 1;
2794 return 0;
2797 /* Set this for debugging, to have a way to get out */
2798 int stop_character;
2800 #ifdef MULTI_KBOARD
2801 static KBOARD *
2802 event_to_kboard (event)
2803 struct input_event *event;
2805 Lisp_Object frame;
2806 frame = event->frame_or_window;
2807 if (CONSP (frame))
2808 frame = XCAR (frame);
2809 else if (WINDOWP (frame))
2810 frame = WINDOW_FRAME (XWINDOW (frame));
2812 /* There are still some events that don't set this field.
2813 For now, just ignore the problem.
2814 Also ignore dead frames here. */
2815 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
2816 return 0;
2817 else
2818 return FRAME_KBOARD (XFRAME (frame));
2820 #endif
2822 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
2824 void
2825 kbd_buffer_store_event (event)
2826 register struct input_event *event;
2828 if (event->kind == no_event)
2829 abort ();
2831 if (event->kind == ascii_keystroke)
2833 register int c = event->code & 0377;
2835 if (event->modifiers & ctrl_modifier)
2836 c = make_ctrl_char (c);
2838 c |= (event->modifiers
2839 & (meta_modifier | alt_modifier
2840 | hyper_modifier | super_modifier));
2842 if (c == quit_char)
2844 extern SIGTYPE interrupt_signal ();
2845 #ifdef MULTI_KBOARD
2846 KBOARD *kb;
2847 struct input_event *sp;
2849 if (single_kboard
2850 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
2851 kb != current_kboard))
2853 kb->kbd_queue
2854 = Fcons (make_lispy_switch_frame (event->frame_or_window),
2855 Fcons (make_number (c), Qnil));
2856 kb->kbd_queue_has_data = 1;
2857 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
2859 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
2860 sp = kbd_buffer;
2862 if (event_to_kboard (sp) == kb)
2864 sp->kind = no_event;
2865 sp->frame_or_window = Qnil;
2868 return;
2870 #endif
2872 /* If this results in a quit_char being returned to Emacs as
2873 input, set Vlast_event_frame properly. If this doesn't
2874 get returned to Emacs as an event, the next event read
2875 will set Vlast_event_frame again, so this is safe to do. */
2877 Lisp_Object focus;
2879 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
2880 if (NILP (focus))
2881 focus = event->frame_or_window;
2882 internal_last_event_frame = focus;
2883 Vlast_event_frame = focus;
2886 last_event_timestamp = event->timestamp;
2887 interrupt_signal ();
2888 return;
2891 if (c && c == stop_character)
2893 sys_suspend ();
2894 return;
2897 /* Don't insert two buffer_switch_event's in a row.
2898 Just ignore the second one. */
2899 else if (event->kind == buffer_switch_event
2900 && kbd_fetch_ptr != kbd_store_ptr
2901 && kbd_store_ptr->kind == buffer_switch_event)
2902 return;
2904 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
2905 kbd_store_ptr = kbd_buffer;
2907 /* Don't let the very last slot in the buffer become full,
2908 since that would make the two pointers equal,
2909 and that is indistinguishable from an empty buffer.
2910 Discard the event if it would fill the last slot. */
2911 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
2913 struct input_event *sp = kbd_store_ptr;
2914 sp->kind = event->kind;
2915 if (event->kind == selection_request_event)
2917 /* We must not use the ordinary copying code for this case,
2918 since `part' is an enum and copying it might not copy enough
2919 in this case. */
2920 bcopy (event, (char *) sp, sizeof (*event));
2922 else
2924 sp->code = event->code;
2925 sp->part = event->part;
2926 sp->frame_or_window = event->frame_or_window;
2927 sp->modifiers = event->modifiers;
2928 sp->x = event->x;
2929 sp->y = event->y;
2930 sp->timestamp = event->timestamp;
2932 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
2933 - kbd_buffer]
2934 = event->frame_or_window);
2936 kbd_store_ptr++;
2940 /* Discard any mouse events in the event buffer by setting them to
2941 no_event. */
2942 void
2943 discard_mouse_events ()
2945 struct input_event *sp;
2946 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
2948 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
2949 sp = kbd_buffer;
2951 if (sp->kind == mouse_click
2952 #ifdef WINDOWSNT
2953 || sp->kind == w32_scroll_bar_click
2954 #endif
2955 || sp->kind == scroll_bar_click)
2957 sp->kind = no_event;
2962 /* Read one event from the event buffer, waiting if necessary.
2963 The value is a Lisp object representing the event.
2964 The value is nil for an event that should be ignored,
2965 or that was handled here.
2966 We always read and discard one event. */
2968 static Lisp_Object
2969 kbd_buffer_get_event (kbp, used_mouse_menu)
2970 KBOARD **kbp;
2971 int *used_mouse_menu;
2973 register int c;
2974 Lisp_Object obj;
2976 if (noninteractive)
2978 c = getchar ();
2979 XSETINT (obj, c);
2980 *kbp = current_kboard;
2981 return obj;
2984 /* Wait until there is input available. */
2985 for (;;)
2987 if (kbd_fetch_ptr != kbd_store_ptr)
2988 break;
2989 #ifdef HAVE_MOUSE
2990 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2991 break;
2992 #endif
2994 /* If the quit flag is set, then read_char will return
2995 quit_char, so that counts as "available input." */
2996 if (!NILP (Vquit_flag))
2997 quit_throw_to_read_char ();
2999 /* One way or another, wait until input is available; then, if
3000 interrupt handlers have not read it, read it now. */
3002 #ifdef OLDVMS
3003 wait_for_kbd_input ();
3004 #else
3005 /* Note SIGIO has been undef'd if FIONREAD is missing. */
3006 #ifdef SIGIO
3007 gobble_input (0);
3008 #endif /* SIGIO */
3009 if (kbd_fetch_ptr != kbd_store_ptr)
3010 break;
3011 #ifdef HAVE_MOUSE
3012 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3013 break;
3014 #endif
3016 Lisp_Object minus_one;
3018 XSETINT (minus_one, -1);
3019 wait_reading_process_input (0, 0, minus_one, 1);
3021 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
3022 /* Pass 1 for EXPECT since we just waited to have input. */
3023 read_avail_input (1);
3025 #endif /* not VMS */
3028 if (CONSP (Vunread_command_events))
3030 Lisp_Object first;
3031 first = XCAR (Vunread_command_events);
3032 Vunread_command_events = XCDR (Vunread_command_events);
3033 *kbp = current_kboard;
3034 return first;
3037 /* At this point, we know that there is a readable event available
3038 somewhere. If the event queue is empty, then there must be a
3039 mouse movement enabled and available. */
3040 if (kbd_fetch_ptr != kbd_store_ptr)
3042 struct input_event *event;
3044 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3045 ? kbd_fetch_ptr
3046 : kbd_buffer);
3048 last_event_timestamp = event->timestamp;
3050 #ifdef MULTI_KBOARD
3051 *kbp = event_to_kboard (event);
3052 if (*kbp == 0)
3053 *kbp = current_kboard; /* Better than returning null ptr? */
3054 #else
3055 *kbp = &the_only_kboard;
3056 #endif
3058 obj = Qnil;
3060 /* These two kinds of events get special handling
3061 and don't actually appear to the command loop.
3062 We return nil for them. */
3063 if (event->kind == selection_request_event)
3065 #ifdef HAVE_X11
3066 struct input_event copy;
3068 /* Remove it from the buffer before processing it,
3069 since otherwise swallow_events will see it
3070 and process it again. */
3071 copy = *event;
3072 kbd_fetch_ptr = event + 1;
3073 input_pending = readable_events (0);
3074 x_handle_selection_request (&copy);
3075 #else
3076 /* We're getting selection request events, but we don't have
3077 a window system. */
3078 abort ();
3079 #endif
3082 else if (event->kind == selection_clear_event)
3084 #ifdef HAVE_X11
3085 struct input_event copy;
3087 /* Remove it from the buffer before processing it. */
3088 copy = *event;
3089 kbd_fetch_ptr = event + 1;
3090 input_pending = readable_events (0);
3091 x_handle_selection_clear (&copy);
3092 #else
3093 /* We're getting selection request events, but we don't have
3094 a window system. */
3095 abort ();
3096 #endif
3098 #if defined (HAVE_X11) || defined (HAVE_NTGUI)
3099 else if (event->kind == delete_window_event)
3101 /* Make an event (delete-frame (FRAME)). */
3102 obj = Fcons (event->frame_or_window, Qnil);
3103 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
3104 kbd_fetch_ptr = event + 1;
3106 else if (event->kind == iconify_event)
3108 /* Make an event (iconify-frame (FRAME)). */
3109 obj = Fcons (event->frame_or_window, Qnil);
3110 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
3111 kbd_fetch_ptr = event + 1;
3113 else if (event->kind == deiconify_event)
3115 /* Make an event (make-frame-visible (FRAME)). */
3116 obj = Fcons (event->frame_or_window, Qnil);
3117 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
3118 kbd_fetch_ptr = event + 1;
3120 #endif
3121 else if (event->kind == buffer_switch_event)
3123 /* The value doesn't matter here; only the type is tested. */
3124 XSETBUFFER (obj, current_buffer);
3125 kbd_fetch_ptr = event + 1;
3127 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
3128 else if (event->kind == menu_bar_activate_event)
3130 kbd_fetch_ptr = event + 1;
3131 input_pending = readable_events (0);
3132 if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
3133 x_activate_menubar (XFRAME (event->frame_or_window));
3135 #endif
3136 #ifdef WINDOWSNT
3137 else if (event->kind == language_change_event)
3139 /* Make an event (language-change (FRAME CHARSET LCID)). */
3140 obj = Fcons (event->modifiers, Qnil);
3141 obj = Fcons (event->code, Qnil);
3142 obj = Fcons (event->frame_or_window, obj);
3143 obj = Fcons (Qlanguage_change, Fcons (obj, Qnil));
3144 kbd_fetch_ptr = event + 1;
3146 #endif
3147 /* Just discard these, by returning nil.
3148 With MULTI_KBOARD, these events are used as placeholders
3149 when we need to randomly delete events from the queue.
3150 (They shouldn't otherwise be found in the buffer,
3151 but on some machines it appears they do show up
3152 even without MULTI_KBOARD.) */
3153 /* On Windows NT/9X, no_event is used to delete extraneous
3154 mouse events during a popup-menu call. */
3155 else if (event->kind == no_event)
3156 kbd_fetch_ptr = event + 1;
3157 else if (event->kind == HELP_EVENT)
3159 /* The car of event->frame_or_window is a frame,
3160 the cdr is the help to display. */
3161 obj = Fcons (Qhelp_echo, event->frame_or_window);
3162 kbd_fetch_ptr = event + 1;
3164 /* If this event is on a different frame, return a switch-frame this
3165 time, and leave the event in the queue for next time. */
3166 else
3168 Lisp_Object frame;
3169 Lisp_Object focus;
3171 frame = event->frame_or_window;
3172 if (CONSP (frame))
3173 frame = XCAR (frame);
3174 else if (WINDOWP (frame))
3175 frame = WINDOW_FRAME (XWINDOW (frame));
3177 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
3178 if (! NILP (focus))
3179 frame = focus;
3181 if (! EQ (frame, internal_last_event_frame)
3182 && !EQ (frame, selected_frame))
3183 obj = make_lispy_switch_frame (frame);
3184 internal_last_event_frame = frame;
3186 /* If we didn't decide to make a switch-frame event, go ahead
3187 and build a real event from the queue entry. */
3189 if (NILP (obj))
3191 obj = make_lispy_event (event);
3192 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
3193 /* If this was a menu selection, then set the flag to inhibit
3194 writing to last_nonmenu_event. Don't do this if the event
3195 we're returning is (menu-bar), though; that indicates the
3196 beginning of the menu sequence, and we might as well leave
3197 that as the `event with parameters' for this selection. */
3198 if ((event->kind == menu_bar_event
3199 || event->kind == TOOL_BAR_EVENT)
3200 && !(CONSP (obj) && EQ (XCAR (obj), Qmenu_bar))
3201 && !(CONSP (obj) && EQ (XCAR (obj), Qtool_bar))
3202 && used_mouse_menu)
3203 *used_mouse_menu = 1;
3204 #endif
3206 /* Wipe out this event, to catch bugs. */
3207 event->kind = no_event;
3208 XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer] = Qnil;
3210 kbd_fetch_ptr = event + 1;
3214 #ifdef HAVE_MOUSE
3215 /* Try generating a mouse motion event. */
3216 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3218 FRAME_PTR f = some_mouse_moved ();
3219 Lisp_Object bar_window;
3220 enum scroll_bar_part part;
3221 Lisp_Object x, y;
3222 unsigned long time;
3224 *kbp = current_kboard;
3225 /* Note that this uses F to determine which display to look at.
3226 If there is no valid info, it does not store anything
3227 so x remains nil. */
3228 x = Qnil;
3229 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
3231 obj = Qnil;
3233 /* Decide if we should generate a switch-frame event. Don't
3234 generate switch-frame events for motion outside of all Emacs
3235 frames. */
3236 if (!NILP (x) && f)
3238 Lisp_Object frame;
3240 frame = FRAME_FOCUS_FRAME (f);
3241 if (NILP (frame))
3242 XSETFRAME (frame, f);
3244 if (! EQ (frame, internal_last_event_frame)
3245 && !EQ (frame, selected_frame))
3246 obj = make_lispy_switch_frame (frame);
3247 internal_last_event_frame = frame;
3250 /* If we didn't decide to make a switch-frame event, go ahead and
3251 return a mouse-motion event. */
3252 if (!NILP (x) && NILP (obj))
3253 obj = make_lispy_movement (f, bar_window, part, x, y, time);
3255 #endif /* HAVE_MOUSE */
3256 else
3257 /* We were promised by the above while loop that there was
3258 something for us to read! */
3259 abort ();
3261 input_pending = readable_events (0);
3263 Vlast_event_frame = internal_last_event_frame;
3265 return (obj);
3268 /* Process any events that are not user-visible,
3269 then return, without reading any user-visible events. */
3271 void
3272 swallow_events (do_display)
3273 int do_display;
3275 int old_timers_run;
3277 while (kbd_fetch_ptr != kbd_store_ptr)
3279 struct input_event *event;
3281 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3282 ? kbd_fetch_ptr
3283 : kbd_buffer);
3285 last_event_timestamp = event->timestamp;
3287 /* These two kinds of events get special handling
3288 and don't actually appear to the command loop. */
3289 if (event->kind == selection_request_event)
3291 #ifdef HAVE_X11
3292 struct input_event copy;
3294 /* Remove it from the buffer before processing it,
3295 since otherwise swallow_events called recursively could see it
3296 and process it again. */
3297 copy = *event;
3298 kbd_fetch_ptr = event + 1;
3299 input_pending = readable_events (0);
3300 x_handle_selection_request (&copy);
3301 #else
3302 /* We're getting selection request events, but we don't have
3303 a window system. */
3304 abort ();
3305 #endif
3308 else if (event->kind == selection_clear_event)
3310 #ifdef HAVE_X11
3311 struct input_event copy;
3313 /* Remove it from the buffer before processing it, */
3314 copy = *event;
3316 kbd_fetch_ptr = event + 1;
3317 input_pending = readable_events (0);
3318 x_handle_selection_clear (&copy);
3319 #else
3320 /* We're getting selection request events, but we don't have
3321 a window system. */
3322 abort ();
3323 #endif
3325 else
3326 break;
3329 old_timers_run = timers_run;
3330 get_input_pending (&input_pending, 1);
3332 if (timers_run != old_timers_run && do_display)
3333 redisplay_preserve_echo_area ();
3336 static EMACS_TIME timer_idleness_start_time;
3338 /* Record the start of when Emacs is idle,
3339 for the sake of running idle-time timers. */
3341 void
3342 timer_start_idle ()
3344 Lisp_Object timers;
3346 /* If we are already in the idle state, do nothing. */
3347 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3348 return;
3350 EMACS_GET_TIME (timer_idleness_start_time);
3352 /* Mark all idle-time timers as once again candidates for running. */
3353 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCDR (timers))
3355 Lisp_Object timer;
3357 timer = XCAR (timers);
3359 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3360 continue;
3361 XVECTOR (timer)->contents[0] = Qnil;
3365 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
3367 void
3368 timer_stop_idle ()
3370 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
3373 /* This is only for debugging. */
3374 struct input_event last_timer_event;
3376 /* Check whether a timer has fired. To prevent larger problems we simply
3377 disregard elements that are not proper timers. Do not make a circular
3378 timer list for the time being.
3380 Returns the number of seconds to wait until the next timer fires. If a
3381 timer is triggering now, return zero seconds.
3382 If no timer is active, return -1 seconds.
3384 If a timer is ripe, we run it, with quitting turned off.
3386 DO_IT_NOW is now ignored. It used to mean that we should
3387 run the timer directly instead of queueing a timer-event.
3388 Now we always run timers directly. */
3390 EMACS_TIME
3391 timer_check (do_it_now)
3392 int do_it_now;
3394 EMACS_TIME nexttime;
3395 EMACS_TIME now, idleness_now;
3396 Lisp_Object timers, idle_timers, chosen_timer;
3397 struct gcpro gcpro1, gcpro2, gcpro3;
3399 EMACS_SET_SECS (nexttime, -1);
3400 EMACS_SET_USECS (nexttime, -1);
3402 /* Always consider the ordinary timers. */
3403 timers = Vtimer_list;
3404 /* Consider the idle timers only if Emacs is idle. */
3405 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3406 idle_timers = Vtimer_idle_list;
3407 else
3408 idle_timers = Qnil;
3409 chosen_timer = Qnil;
3410 GCPRO3 (timers, idle_timers, chosen_timer);
3412 if (CONSP (timers) || CONSP (idle_timers))
3414 EMACS_GET_TIME (now);
3415 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3416 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
3419 while (CONSP (timers) || CONSP (idle_timers))
3421 Lisp_Object *vector;
3422 Lisp_Object timer, idle_timer;
3423 EMACS_TIME timer_time, idle_timer_time;
3424 EMACS_TIME difference, timer_difference, idle_timer_difference;
3426 /* Skip past invalid timers and timers already handled. */
3427 if (!NILP (timers))
3429 timer = XCAR (timers);
3430 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3432 timers = XCDR (timers);
3433 continue;
3435 vector = XVECTOR (timer)->contents;
3437 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3438 || !INTEGERP (vector[3])
3439 || ! NILP (vector[0]))
3441 timers = XCDR (timers);
3442 continue;
3445 if (!NILP (idle_timers))
3447 timer = XCAR (idle_timers);
3448 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3450 idle_timers = XCDR (idle_timers);
3451 continue;
3453 vector = XVECTOR (timer)->contents;
3455 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3456 || !INTEGERP (vector[3])
3457 || ! NILP (vector[0]))
3459 idle_timers = XCDR (idle_timers);
3460 continue;
3464 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
3465 based on the next ordinary timer.
3466 TIMER_DIFFERENCE is the distance in time from NOW to when
3467 this timer becomes ripe (negative if it's already ripe). */
3468 if (!NILP (timers))
3470 timer = XCAR (timers);
3471 vector = XVECTOR (timer)->contents;
3472 EMACS_SET_SECS (timer_time,
3473 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3474 EMACS_SET_USECS (timer_time, XINT (vector[3]));
3475 EMACS_SUB_TIME (timer_difference, timer_time, now);
3478 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
3479 based on the next idle timer. */
3480 if (!NILP (idle_timers))
3482 idle_timer = XCAR (idle_timers);
3483 vector = XVECTOR (idle_timer)->contents;
3484 EMACS_SET_SECS (idle_timer_time,
3485 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3486 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
3487 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
3490 /* Decide which timer is the next timer,
3491 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
3492 Also step down the list where we found that timer. */
3494 if (! NILP (timers) && ! NILP (idle_timers))
3496 EMACS_TIME temp;
3497 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
3498 if (EMACS_TIME_NEG_P (temp))
3500 chosen_timer = timer;
3501 timers = XCDR (timers);
3502 difference = timer_difference;
3504 else
3506 chosen_timer = idle_timer;
3507 idle_timers = XCDR (idle_timers);
3508 difference = idle_timer_difference;
3511 else if (! NILP (timers))
3513 chosen_timer = timer;
3514 timers = XCDR (timers);
3515 difference = timer_difference;
3517 else
3519 chosen_timer = idle_timer;
3520 idle_timers = XCDR (idle_timers);
3521 difference = idle_timer_difference;
3523 vector = XVECTOR (chosen_timer)->contents;
3525 /* If timer is rupe, run it if it hasn't been run. */
3526 if (EMACS_TIME_NEG_P (difference)
3527 || (EMACS_SECS (difference) == 0
3528 && EMACS_USECS (difference) == 0))
3530 if (NILP (vector[0]))
3532 int was_locked = single_kboard;
3533 int count = specpdl_ptr - specpdl;
3534 #ifdef HAVE_WINDOW_SYSTEM
3535 int old_inhibit_busy_cursor = inhibit_busy_cursor;
3536 #endif
3538 /* Mark the timer as triggered to prevent problems if the lisp
3539 code fails to reschedule it right. */
3540 vector[0] = Qt;
3542 specbind (Qinhibit_quit, Qt);
3544 #ifdef HAVE_WINDOW_SYSTEM
3545 inhibit_busy_cursor = 2;
3546 #endif
3548 call1 (Qtimer_event_handler, chosen_timer);
3549 timers_run++;
3551 #ifdef HAVE_WINDOW_SYSTEM
3552 inhibit_busy_cursor = old_inhibit_busy_cursor;
3553 #endif
3555 unbind_to (count, Qnil);
3557 /* Resume allowing input from any kboard, if that was true before. */
3558 if (!was_locked)
3559 any_kboard_state ();
3561 /* Since we have handled the event,
3562 we don't need to tell the caller to wake up and do it. */
3565 else
3566 /* When we encounter a timer that is still waiting,
3567 return the amount of time to wait before it is ripe. */
3569 UNGCPRO;
3570 return difference;
3574 /* No timers are pending in the future. */
3575 /* Return 0 if we generated an event, and -1 if not. */
3576 UNGCPRO;
3577 return nexttime;
3580 /* Caches for modify_event_symbol. */
3581 static Lisp_Object accent_key_syms;
3582 static Lisp_Object func_key_syms;
3583 static Lisp_Object mouse_syms;
3584 #ifdef WINDOWSNT
3585 static Lisp_Object mouse_wheel_syms;
3586 #endif
3587 static Lisp_Object drag_n_drop_syms;
3589 /* This is a list of keysym codes for special "accent" characters.
3590 It parallels lispy_accent_keys. */
3592 static int lispy_accent_codes[] =
3594 #ifdef XK_dead_circumflex
3595 XK_dead_circumflex,
3596 #else
3598 #endif
3599 #ifdef XK_dead_grave
3600 XK_dead_grave,
3601 #else
3603 #endif
3604 #ifdef XK_dead_tilde
3605 XK_dead_tilde,
3606 #else
3608 #endif
3609 #ifdef XK_dead_diaeresis
3610 XK_dead_diaeresis,
3611 #else
3613 #endif
3614 #ifdef XK_dead_macron
3615 XK_dead_macron,
3616 #else
3618 #endif
3619 #ifdef XK_dead_degree
3620 XK_dead_degree,
3621 #else
3623 #endif
3624 #ifdef XK_dead_acute
3625 XK_dead_acute,
3626 #else
3628 #endif
3629 #ifdef XK_dead_cedilla
3630 XK_dead_cedilla,
3631 #else
3633 #endif
3634 #ifdef XK_dead_breve
3635 XK_dead_breve,
3636 #else
3638 #endif
3639 #ifdef XK_dead_ogonek
3640 XK_dead_ogonek,
3641 #else
3643 #endif
3644 #ifdef XK_dead_caron
3645 XK_dead_caron,
3646 #else
3648 #endif
3649 #ifdef XK_dead_doubleacute
3650 XK_dead_doubleacute,
3651 #else
3653 #endif
3654 #ifdef XK_dead_abovedot
3655 XK_dead_abovedot,
3656 #else
3658 #endif
3661 /* This is a list of Lisp names for special "accent" characters.
3662 It parallels lispy_accent_codes. */
3664 static char *lispy_accent_keys[] =
3666 "dead-circumflex",
3667 "dead-grave",
3668 "dead-tilde",
3669 "dead-diaeresis",
3670 "dead-macron",
3671 "dead-degree",
3672 "dead-acute",
3673 "dead-cedilla",
3674 "dead-breve",
3675 "dead-ogonek",
3676 "dead-caron",
3677 "dead-doubleacute",
3678 "dead-abovedot",
3681 #ifdef HAVE_NTGUI
3682 #define FUNCTION_KEY_OFFSET 0x0
3684 char *lispy_function_keys[] =
3686 0, /* 0 */
3688 0, /* VK_LBUTTON 0x01 */
3689 0, /* VK_RBUTTON 0x02 */
3690 "cancel", /* VK_CANCEL 0x03 */
3691 0, /* VK_MBUTTON 0x04 */
3693 0, 0, 0, /* 0x05 .. 0x07 */
3695 "backspace", /* VK_BACK 0x08 */
3696 "tab", /* VK_TAB 0x09 */
3698 0, 0, /* 0x0A .. 0x0B */
3700 "clear", /* VK_CLEAR 0x0C */
3701 "return", /* VK_RETURN 0x0D */
3703 0, 0, /* 0x0E .. 0x0F */
3705 0, /* VK_SHIFT 0x10 */
3706 0, /* VK_CONTROL 0x11 */
3707 0, /* VK_MENU 0x12 */
3708 "pause", /* VK_PAUSE 0x13 */
3709 "capslock", /* VK_CAPITAL 0x14 */
3711 0, 0, 0, 0, 0, 0, /* 0x15 .. 0x1A */
3713 "escape", /* VK_ESCAPE 0x1B */
3715 0, 0, 0, 0, /* 0x1C .. 0x1F */
3717 0, /* VK_SPACE 0x20 */
3718 "prior", /* VK_PRIOR 0x21 */
3719 "next", /* VK_NEXT 0x22 */
3720 "end", /* VK_END 0x23 */
3721 "home", /* VK_HOME 0x24 */
3722 "left", /* VK_LEFT 0x25 */
3723 "up", /* VK_UP 0x26 */
3724 "right", /* VK_RIGHT 0x27 */
3725 "down", /* VK_DOWN 0x28 */
3726 "select", /* VK_SELECT 0x29 */
3727 "print", /* VK_PRINT 0x2A */
3728 "execute", /* VK_EXECUTE 0x2B */
3729 "snapshot", /* VK_SNAPSHOT 0x2C */
3730 "insert", /* VK_INSERT 0x2D */
3731 "delete", /* VK_DELETE 0x2E */
3732 "help", /* VK_HELP 0x2F */
3734 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
3736 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3738 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
3740 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
3742 0, 0, 0, 0, 0, 0, 0, 0, 0,
3743 0, 0, 0, 0, 0, 0, 0, 0, 0,
3744 0, 0, 0, 0, 0, 0, 0, 0,
3746 "lwindow", /* VK_LWIN 0x5B */
3747 "rwindow", /* VK_RWIN 0x5C */
3748 "apps", /* VK_APPS 0x5D */
3750 0, 0, /* 0x5E .. 0x5F */
3752 "kp-0", /* VK_NUMPAD0 0x60 */
3753 "kp-1", /* VK_NUMPAD1 0x61 */
3754 "kp-2", /* VK_NUMPAD2 0x62 */
3755 "kp-3", /* VK_NUMPAD3 0x63 */
3756 "kp-4", /* VK_NUMPAD4 0x64 */
3757 "kp-5", /* VK_NUMPAD5 0x65 */
3758 "kp-6", /* VK_NUMPAD6 0x66 */
3759 "kp-7", /* VK_NUMPAD7 0x67 */
3760 "kp-8", /* VK_NUMPAD8 0x68 */
3761 "kp-9", /* VK_NUMPAD9 0x69 */
3762 "kp-multiply", /* VK_MULTIPLY 0x6A */
3763 "kp-add", /* VK_ADD 0x6B */
3764 "kp-separator", /* VK_SEPARATOR 0x6C */
3765 "kp-subtract", /* VK_SUBTRACT 0x6D */
3766 "kp-decimal", /* VK_DECIMAL 0x6E */
3767 "kp-divide", /* VK_DIVIDE 0x6F */
3768 "f1", /* VK_F1 0x70 */
3769 "f2", /* VK_F2 0x71 */
3770 "f3", /* VK_F3 0x72 */
3771 "f4", /* VK_F4 0x73 */
3772 "f5", /* VK_F5 0x74 */
3773 "f6", /* VK_F6 0x75 */
3774 "f7", /* VK_F7 0x76 */
3775 "f8", /* VK_F8 0x77 */
3776 "f9", /* VK_F9 0x78 */
3777 "f10", /* VK_F10 0x79 */
3778 "f11", /* VK_F11 0x7A */
3779 "f12", /* VK_F12 0x7B */
3780 "f13", /* VK_F13 0x7C */
3781 "f14", /* VK_F14 0x7D */
3782 "f15", /* VK_F15 0x7E */
3783 "f16", /* VK_F16 0x7F */
3784 "f17", /* VK_F17 0x80 */
3785 "f18", /* VK_F18 0x81 */
3786 "f19", /* VK_F19 0x82 */
3787 "f20", /* VK_F20 0x83 */
3788 "f21", /* VK_F21 0x84 */
3789 "f22", /* VK_F22 0x85 */
3790 "f23", /* VK_F23 0x86 */
3791 "f24", /* VK_F24 0x87 */
3793 0, 0, 0, 0, /* 0x88 .. 0x8B */
3794 0, 0, 0, 0, /* 0x8C .. 0x8F */
3796 "kp-numlock", /* VK_NUMLOCK 0x90 */
3797 "scroll", /* VK_SCROLL 0x91 */
3799 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
3800 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
3801 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
3802 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
3803 "kp-end", /* VK_NUMPAD_END 0x96 */
3804 "kp-home", /* VK_NUMPAD_HOME 0x97 */
3805 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
3806 "kp-up", /* VK_NUMPAD_UP 0x99 */
3807 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
3808 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
3809 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
3810 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
3812 0, 0, /* 0x9E .. 0x9F */
3815 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
3816 * Used only as parameters to GetAsyncKeyState and GetKeyState.
3817 * No other API or message will distinguish left and right keys this way.
3819 /* 0xA0 .. 0xEF */
3821 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3822 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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,
3827 /* 0xF0 .. 0xF5 */
3829 0, 0, 0, 0, 0, 0,
3831 "attn", /* VK_ATTN 0xF6 */
3832 "crsel", /* VK_CRSEL 0xF7 */
3833 "exsel", /* VK_EXSEL 0xF8 */
3834 "ereof", /* VK_EREOF 0xF9 */
3835 "play", /* VK_PLAY 0xFA */
3836 "zoom", /* VK_ZOOM 0xFB */
3837 "noname", /* VK_NONAME 0xFC */
3838 "pa1", /* VK_PA1 0xFD */
3839 "oem_clear", /* VK_OEM_CLEAR 0xFE */
3840 0 /* 0xFF */
3843 #else /* not HAVE_NTGUI */
3845 #ifdef XK_kana_A
3846 static char *lispy_kana_keys[] =
3848 /* X Keysym value */
3849 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x400 .. 0x40f */
3850 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x410 .. 0x41f */
3851 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x420 .. 0x42f */
3852 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x430 .. 0x43f */
3853 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x440 .. 0x44f */
3854 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x450 .. 0x45f */
3855 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x460 .. 0x46f */
3856 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0,
3857 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x480 .. 0x48f */
3858 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x490 .. 0x49f */
3859 0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket",
3860 "kana-comma", "kana-conjunctive", "kana-WO", "kana-a",
3861 "kana-i", "kana-u", "kana-e", "kana-o",
3862 "kana-ya", "kana-yu", "kana-yo", "kana-tsu",
3863 "prolongedsound", "kana-A", "kana-I", "kana-U",
3864 "kana-E", "kana-O", "kana-KA", "kana-KI",
3865 "kana-KU", "kana-KE", "kana-KO", "kana-SA",
3866 "kana-SHI", "kana-SU", "kana-SE", "kana-SO",
3867 "kana-TA", "kana-CHI", "kana-TSU", "kana-TE",
3868 "kana-TO", "kana-NA", "kana-NI", "kana-NU",
3869 "kana-NE", "kana-NO", "kana-HA", "kana-HI",
3870 "kana-FU", "kana-HE", "kana-HO", "kana-MA",
3871 "kana-MI", "kana-MU", "kana-ME", "kana-MO",
3872 "kana-YA", "kana-YU", "kana-YO", "kana-RA",
3873 "kana-RI", "kana-RU", "kana-RE", "kana-RO",
3874 "kana-WA", "kana-N", "voicedsound", "semivoicedsound",
3875 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4e0 .. 0x4ef */
3876 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f0 .. 0x4ff */
3878 #endif /* XK_kana_A */
3880 #define FUNCTION_KEY_OFFSET 0xff00
3882 /* You'll notice that this table is arranged to be conveniently
3883 indexed by X Windows keysym values. */
3884 static char *lispy_function_keys[] =
3886 /* X Keysym value */
3888 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00...0f */
3889 "backspace", "tab", "linefeed", "clear",
3890 0, "return", 0, 0,
3891 0, 0, 0, "pause", /* 0xff10...1f */
3892 0, 0, 0, 0, 0, 0, 0, "escape",
3893 0, 0, 0, 0,
3894 0, "kanji", "muhenkan", "henkan", /* 0xff20...2f */
3895 "romaji", "hiragana", "katakana", "hiragana-katakana",
3896 "zenkaku", "hankaku", "zenkaku-hankaku", "touroku",
3897 "massyo", "kana-lock", "kana-shift", "eisu-shift",
3898 "eisu-toggle", /* 0xff30...3f */
3899 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3900 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
3902 "home", "left", "up", "right", /* 0xff50 */ /* IsCursorKey */
3903 "down", "prior", "next", "end",
3904 "begin", 0, 0, 0, 0, 0, 0, 0,
3905 "select", /* 0xff60 */ /* IsMiscFunctionKey */
3906 "print",
3907 "execute",
3908 "insert",
3909 0, /* 0xff64 */
3910 "undo",
3911 "redo",
3912 "menu",
3913 "find",
3914 "cancel",
3915 "help",
3916 "break", /* 0xff6b */
3918 0, 0, 0, 0,
3919 0, 0, 0, 0, "backtab", 0, 0, 0, /* 0xff70... */
3920 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff78... */
3921 "kp-space", /* 0xff80 */ /* IsKeypadKey */
3922 0, 0, 0, 0, 0, 0, 0, 0,
3923 "kp-tab", /* 0xff89 */
3924 0, 0, 0,
3925 "kp-enter", /* 0xff8d */
3926 0, 0, 0,
3927 "kp-f1", /* 0xff91 */
3928 "kp-f2",
3929 "kp-f3",
3930 "kp-f4",
3931 "kp-home", /* 0xff95 */
3932 "kp-left",
3933 "kp-up",
3934 "kp-right",
3935 "kp-down",
3936 "kp-prior", /* kp-page-up */
3937 "kp-next", /* kp-page-down */
3938 "kp-end",
3939 "kp-begin",
3940 "kp-insert",
3941 "kp-delete",
3942 0, /* 0xffa0 */
3943 0, 0, 0, 0, 0, 0, 0, 0, 0,
3944 "kp-multiply", /* 0xffaa */
3945 "kp-add",
3946 "kp-separator",
3947 "kp-subtract",
3948 "kp-decimal",
3949 "kp-divide", /* 0xffaf */
3950 "kp-0", /* 0xffb0 */
3951 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
3952 0, /* 0xffba */
3953 0, 0,
3954 "kp-equal", /* 0xffbd */
3955 "f1", /* 0xffbe */ /* IsFunctionKey */
3956 "f2",
3957 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
3958 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
3959 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
3960 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
3961 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
3962 0, 0, 0, 0, 0, 0, 0, 0,
3963 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
3964 0, 0, 0, 0, 0, 0, 0, "delete"
3967 /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE. */
3968 #define ISO_FUNCTION_KEY_OFFSET 0xfe00
3970 static char *iso_lispy_function_keys[] =
3972 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe00 */
3973 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe08 */
3974 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe10 */
3975 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe18 */
3976 "iso-lefttab", /* 0xfe20 */
3977 "iso-move-line-up", "iso-move-line-down",
3978 "iso-partial-line-up", "iso-partial-line-down",
3979 "iso-partial-space-left", "iso-partial-space-right",
3980 "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */
3981 "iso-release-margin-left", "iso-release-margin-right",
3982 "iso-release-both-margins",
3983 "iso-fast-cursor-left", "iso-fast-cursor-right",
3984 "iso-fast-cursor-up", "iso-fast-cursor-down",
3985 "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */
3986 "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */
3989 #endif /* not HAVE_NTGUI */
3991 static char *lispy_mouse_names[] =
3993 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
3996 #ifdef WINDOWSNT
3997 /* mouse-wheel events are generated by the wheel on devices such as
3998 the MS Intellimouse. The wheel sits in between the left and right
3999 mouse buttons, and is typically used to scroll or zoom the window
4000 underneath the pointer. mouse-wheel events specify the object on
4001 which they operate, and a delta corresponding to the amount and
4002 direction that the wheel is rotated. Clicking the mouse-wheel
4003 generates a mouse-2 event. */
4004 static char *lispy_mouse_wheel_names[] =
4006 "mouse-wheel"
4009 #endif /* WINDOWSNT */
4011 /* drag-n-drop events are generated when a set of selected files are
4012 dragged from another application and dropped onto an Emacs window. */
4013 static char *lispy_drag_n_drop_names[] =
4015 "drag-n-drop"
4018 /* Scroll bar parts. */
4019 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
4020 Lisp_Object Qup, Qdown, Qbottom, Qend_scroll;
4021 Lisp_Object Qtop;
4023 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
4024 Lisp_Object *scroll_bar_parts[] = {
4025 &Qabove_handle, &Qhandle, &Qbelow_handle,
4026 &Qup, &Qdown, &Qtop, &Qbottom, &Qend_scroll
4029 /* User signal events. */
4030 Lisp_Object Qusr1_signal, Qusr2_signal;
4032 Lisp_Object *lispy_user_signals[] =
4034 &Qusr1_signal, &Qusr2_signal
4038 /* A vector, indexed by button number, giving the down-going location
4039 of currently depressed buttons, both scroll bar and non-scroll bar.
4041 The elements have the form
4042 (BUTTON-NUMBER MODIFIER-MASK . REST)
4043 where REST is the cdr of a position as it would be reported in the event.
4045 The make_lispy_event function stores positions here to tell the
4046 difference between click and drag events, and to store the starting
4047 location to be included in drag events. */
4049 static Lisp_Object button_down_location;
4051 /* Information about the most recent up-going button event: Which
4052 button, what location, and what time. */
4054 static int last_mouse_button;
4055 static int last_mouse_x;
4056 static int last_mouse_y;
4057 static unsigned long button_down_time;
4059 /* The maximum time between clicks to make a double-click,
4060 or Qnil to disable double-click detection,
4061 or Qt for no time limit. */
4062 Lisp_Object Vdouble_click_time;
4064 /* The number of clicks in this multiple-click. */
4066 int double_click_count;
4068 /* Given a struct input_event, build the lisp event which represents
4069 it. If EVENT is 0, build a mouse movement event from the mouse
4070 movement buffer, which should have a movement event in it.
4072 Note that events must be passed to this function in the order they
4073 are received; this function stores the location of button presses
4074 in order to build drag events when the button is released. */
4076 static Lisp_Object
4077 make_lispy_event (event)
4078 struct input_event *event;
4080 int i;
4082 switch (SWITCH_ENUM_CAST (event->kind))
4084 /* A simple keystroke. */
4085 case ascii_keystroke:
4087 Lisp_Object lispy_c;
4088 int c = event->code & 0377;
4089 /* Turn ASCII characters into control characters
4090 when proper. */
4091 if (event->modifiers & ctrl_modifier)
4092 c = make_ctrl_char (c);
4094 /* Add in the other modifier bits. We took care of ctrl_modifier
4095 just above, and the shift key was taken care of by the X code,
4096 and applied to control characters by make_ctrl_char. */
4097 c |= (event->modifiers
4098 & (meta_modifier | alt_modifier
4099 | hyper_modifier | super_modifier));
4100 /* Distinguish Shift-SPC from SPC. */
4101 if ((event->code & 0377) == 040
4102 && event->modifiers & shift_modifier)
4103 c |= shift_modifier;
4104 button_down_time = 0;
4105 XSETFASTINT (lispy_c, c);
4106 return lispy_c;
4109 /* A function key. The symbol may need to have modifier prefixes
4110 tacked onto it. */
4111 case non_ascii_keystroke:
4112 button_down_time = 0;
4114 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
4115 if (event->code == lispy_accent_codes[i])
4116 return modify_event_symbol (i,
4117 event->modifiers,
4118 Qfunction_key, Qnil,
4119 lispy_accent_keys, &accent_key_syms,
4120 (sizeof (lispy_accent_keys)
4121 / sizeof (lispy_accent_keys[0])));
4123 /* Handle system-specific keysyms. */
4124 if (event->code & (1 << 28))
4126 /* We need to use an alist rather than a vector as the cache
4127 since we can't make a vector long enuf. */
4128 if (NILP (current_kboard->system_key_syms))
4129 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
4130 return modify_event_symbol (event->code,
4131 event->modifiers,
4132 Qfunction_key,
4133 current_kboard->Vsystem_key_alist,
4134 0, &current_kboard->system_key_syms,
4135 (unsigned)-1);
4138 #ifdef XK_kana_A
4139 if (event->code >= 0x400 && event->code < 0x500)
4140 return modify_event_symbol (event->code - 0x400,
4141 event->modifiers & ~shift_modifier,
4142 Qfunction_key, Qnil,
4143 lispy_kana_keys, &func_key_syms,
4144 (sizeof (lispy_kana_keys)
4145 / sizeof (lispy_kana_keys[0])));
4146 #endif /* XK_kana_A */
4148 #ifdef ISO_FUNCTION_KEY_OFFSET
4149 if (event->code < FUNCTION_KEY_OFFSET
4150 && event->code >= ISO_FUNCTION_KEY_OFFSET)
4151 return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET,
4152 event->modifiers,
4153 Qfunction_key, Qnil,
4154 iso_lispy_function_keys, &func_key_syms,
4155 (sizeof (iso_lispy_function_keys)
4156 / sizeof (iso_lispy_function_keys[0])));
4157 else
4158 #endif
4159 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
4160 event->modifiers,
4161 Qfunction_key, Qnil,
4162 lispy_function_keys, &func_key_syms,
4163 (sizeof (lispy_function_keys)
4164 / sizeof (lispy_function_keys[0])));
4166 #ifdef HAVE_MOUSE
4167 /* A mouse click. Figure out where it is, decide whether it's
4168 a press, click or drag, and build the appropriate structure. */
4169 case mouse_click:
4170 #ifndef USE_TOOLKIT_SCROLL_BARS
4171 case scroll_bar_click:
4172 #endif
4174 int button = event->code;
4175 int is_double;
4176 Lisp_Object position;
4177 Lisp_Object *start_pos_ptr;
4178 Lisp_Object start_pos;
4180 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
4181 abort ();
4183 /* Build the position as appropriate for this mouse click. */
4184 if (event->kind == mouse_click)
4186 int part;
4187 FRAME_PTR f = XFRAME (event->frame_or_window);
4188 Lisp_Object window;
4189 Lisp_Object posn;
4190 Lisp_Object string_info = Qnil;
4191 int row, column;
4193 /* Ignore mouse events that were made on frame that
4194 have been deleted. */
4195 if (! FRAME_LIVE_P (f))
4196 return Qnil;
4198 /* EVENT->x and EVENT->y are frame-relative pixel
4199 coordinates at this place. Under old redisplay, COLUMN
4200 and ROW are set to frame relative glyph coordinates
4201 which are then used to determine whether this click is
4202 in a menu (non-toolkit version). */
4203 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4204 &column, &row, NULL, 1);
4206 #ifndef USE_X_TOOLKIT
4207 /* In the non-toolkit version, clicks on the menu bar
4208 are ordinary button events in the event buffer.
4209 Distinguish them, and invoke the menu.
4211 (In the toolkit version, the toolkit handles the menu bar
4212 and Emacs doesn't know about it until after the user
4213 makes a selection.) */
4214 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
4215 && (event->modifiers & down_modifier))
4217 Lisp_Object items, item;
4218 int hpos;
4219 int i;
4221 #if 0
4222 /* Activate the menu bar on the down event. If the
4223 up event comes in before the menu code can deal with it,
4224 just ignore it. */
4225 if (! (event->modifiers & down_modifier))
4226 return Qnil;
4227 #endif
4229 /* Find the menu bar item under `column'. */
4230 item = Qnil;
4231 items = FRAME_MENU_BAR_ITEMS (f);
4232 for (i = 0; i < XVECTOR (items)->size; i += 4)
4234 Lisp_Object pos, string;
4235 string = XVECTOR (items)->contents[i + 1];
4236 pos = XVECTOR (items)->contents[i + 3];
4237 if (NILP (string))
4238 break;
4239 if (column >= XINT (pos)
4240 && column < XINT (pos) + XSTRING (string)->size)
4242 item = XVECTOR (items)->contents[i];
4243 break;
4247 /* ELisp manual 2.4b says (x y) are window relative but
4248 code says they are frame-relative. */
4249 position
4250 = Fcons (event->frame_or_window,
4251 Fcons (Qmenu_bar,
4252 Fcons (Fcons (event->x, event->y),
4253 Fcons (make_number (event->timestamp),
4254 Qnil))));
4256 return Fcons (item, Fcons (position, Qnil));
4258 #endif /* not USE_X_TOOLKIT */
4260 /* Set `window' to the window under frame pixel coordinates
4261 event->x/event->y. */
4262 window = window_from_coordinates (f, XINT (event->x),
4263 XINT (event->y), &part, 0);
4265 if (!WINDOWP (window))
4267 window = event->frame_or_window;
4268 posn = Qnil;
4270 else
4272 /* It's a click in window window at frame coordinates
4273 event->x/ event->y. */
4274 struct window *w = XWINDOW (window);
4276 /* Get window relative coordinates. Original code
4277 `rounded' this to glyph boundaries. */
4278 int wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (event->x));
4279 int wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (event->y));
4281 /* Set event coordinates to window-relative coordinates
4282 for constructing the Lisp event below. */
4283 XSETINT (event->x, wx);
4284 XSETINT (event->y, wy);
4286 if (part == 1 || part == 3)
4288 /* Mode line or top line. Look for a string under
4289 the mouse that may have a `local-map' property. */
4290 Lisp_Object string;
4291 int charpos;
4293 posn = part == 1 ? Qmode_line : Qheader_line;
4294 string = mode_line_string (w, wx, wy, part == 1, &charpos);
4295 if (STRINGP (string))
4296 string_info = Fcons (string, make_number (charpos));
4298 else if (part == 2)
4299 posn = Qvertical_line;
4300 else
4301 XSETINT (posn, buffer_posn_from_coords (w, &wx, &wy));
4304 position
4305 = Fcons (window,
4306 Fcons (posn,
4307 Fcons (Fcons (event->x, event->y),
4308 Fcons (make_number (event->timestamp),
4309 (NILP (string_info)
4310 ? Qnil
4311 : Fcons (string_info, Qnil))))));
4313 #ifndef USE_TOOLKIT_SCROLL_BARS
4314 else
4316 /* It's a scrollbar click. */
4317 Lisp_Object window;
4318 Lisp_Object portion_whole;
4319 Lisp_Object part;
4321 window = event->frame_or_window;
4322 portion_whole = Fcons (event->x, event->y);
4323 part = *scroll_bar_parts[(int) event->part];
4325 position
4326 = Fcons (window,
4327 Fcons (Qvertical_scroll_bar,
4328 Fcons (portion_whole,
4329 Fcons (make_number (event->timestamp),
4330 Fcons (part, Qnil)))));
4332 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4334 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
4336 start_pos = *start_pos_ptr;
4337 *start_pos_ptr = Qnil;
4339 is_double = (button == last_mouse_button
4340 && XINT (event->x) == last_mouse_x
4341 && XINT (event->y) == last_mouse_y
4342 && button_down_time != 0
4343 && (EQ (Vdouble_click_time, Qt)
4344 || (INTEGERP (Vdouble_click_time)
4345 && ((int)(event->timestamp - button_down_time)
4346 < XINT (Vdouble_click_time)))));
4347 last_mouse_button = button;
4348 last_mouse_x = XINT (event->x);
4349 last_mouse_y = XINT (event->y);
4351 /* If this is a button press, squirrel away the location, so
4352 we can decide later whether it was a click or a drag. */
4353 if (event->modifiers & down_modifier)
4355 if (is_double)
4357 double_click_count++;
4358 event->modifiers |= ((double_click_count > 2)
4359 ? triple_modifier
4360 : double_modifier);
4362 else
4363 double_click_count = 1;
4364 button_down_time = event->timestamp;
4365 *start_pos_ptr = Fcopy_alist (position);
4368 /* Now we're releasing a button - check the co-ordinates to
4369 see if this was a click or a drag. */
4370 else if (event->modifiers & up_modifier)
4372 /* If we did not see a down before this up,
4373 ignore the up. Probably this happened because
4374 the down event chose a menu item.
4375 It would be an annoyance to treat the release
4376 of the button that chose the menu item
4377 as a separate event. */
4379 if (!CONSP (start_pos))
4380 return Qnil;
4382 event->modifiers &= ~up_modifier;
4383 #if 0 /* Formerly we treated an up with no down as a click event. */
4384 if (!CONSP (start_pos))
4385 event->modifiers |= click_modifier;
4386 else
4387 #endif
4389 /* The third element of every position should be the (x,y)
4390 pair. */
4391 Lisp_Object down;
4393 down = Fnth (make_number (2), start_pos);
4394 if (EQ (event->x, XCAR (down))
4395 && EQ (event->y, XCDR (down)))
4397 event->modifiers |= click_modifier;
4399 else
4401 button_down_time = 0;
4402 event->modifiers |= drag_modifier;
4404 /* Don't check is_double; treat this as multiple
4405 if the down-event was multiple. */
4406 if (double_click_count > 1)
4407 event->modifiers |= ((double_click_count > 2)
4408 ? triple_modifier
4409 : double_modifier);
4412 else
4413 /* Every mouse event should either have the down_modifier or
4414 the up_modifier set. */
4415 abort ();
4418 /* Get the symbol we should use for the mouse click. */
4419 Lisp_Object head;
4421 head = modify_event_symbol (button,
4422 event->modifiers,
4423 Qmouse_click, Qnil,
4424 lispy_mouse_names, &mouse_syms,
4425 (sizeof (lispy_mouse_names)
4426 / sizeof (lispy_mouse_names[0])));
4427 if (event->modifiers & drag_modifier)
4428 return Fcons (head,
4429 Fcons (start_pos,
4430 Fcons (position,
4431 Qnil)));
4432 else if (event->modifiers & (double_modifier | triple_modifier))
4433 return Fcons (head,
4434 Fcons (position,
4435 Fcons (make_number (double_click_count),
4436 Qnil)));
4437 else
4438 return Fcons (head,
4439 Fcons (position,
4440 Qnil));
4444 #if USE_TOOLKIT_SCROLL_BARS
4446 /* We don't have down and up events if using toolkit scroll bars,
4447 so make this always a click event. Store in the `part' of
4448 the Lisp event a symbol which maps to the following actions:
4450 `above_handle' page up
4451 `below_handle' page down
4452 `up' line up
4453 `down' line down
4454 `top' top of buffer
4455 `bottom' bottom of buffer
4456 `handle' thumb has been dragged.
4457 `end-scroll' end of interaction with scroll bar
4459 The incoming input_event contains in its `part' member an
4460 index of type `enum scroll_bar_part' which we can use as an
4461 index in scroll_bar_parts to get the appropriate symbol. */
4463 case scroll_bar_click:
4465 Lisp_Object position, head, window, portion_whole, part;
4467 window = event->frame_or_window;
4468 portion_whole = Fcons (event->x, event->y);
4469 part = *scroll_bar_parts[(int) event->part];
4471 position
4472 = Fcons (window,
4473 Fcons (Qvertical_scroll_bar,
4474 Fcons (portion_whole,
4475 Fcons (make_number (event->timestamp),
4476 Fcons (part, Qnil)))));
4478 /* Always treat scroll bar events as clicks. */
4479 event->modifiers |= click_modifier;
4481 /* Get the symbol we should use for the mouse click. */
4482 head = modify_event_symbol (event->code,
4483 event->modifiers,
4484 Qmouse_click, Qnil,
4485 lispy_mouse_names, &mouse_syms,
4486 (sizeof (lispy_mouse_names)
4487 / sizeof (lispy_mouse_names[0])));
4488 return Fcons (head, Fcons (position, Qnil));
4491 #endif /* USE_TOOLKIT_SCROLL_BARS */
4493 #ifdef WINDOWSNT
4494 case w32_scroll_bar_click:
4496 int button = event->code;
4497 int is_double;
4498 Lisp_Object position;
4499 Lisp_Object *start_pos_ptr;
4500 Lisp_Object start_pos;
4502 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
4503 abort ();
4506 Lisp_Object window;
4507 Lisp_Object portion_whole;
4508 Lisp_Object part;
4510 window = event->frame_or_window;
4511 portion_whole = Fcons (event->x, event->y);
4512 part = *scroll_bar_parts[(int) event->part];
4514 position
4515 = Fcons (window,
4516 Fcons (Qvertical_scroll_bar,
4517 Fcons (portion_whole,
4518 Fcons (make_number (event->timestamp),
4519 Fcons (part, Qnil)))));
4522 /* Always treat W32 scroll bar events as clicks. */
4523 event->modifiers |= click_modifier;
4526 /* Get the symbol we should use for the mouse click. */
4527 Lisp_Object head;
4529 head = modify_event_symbol (button,
4530 event->modifiers,
4531 Qmouse_click, Qnil,
4532 lispy_mouse_names, &mouse_syms,
4533 (sizeof (lispy_mouse_names)
4534 / sizeof (lispy_mouse_names[0])));
4535 return Fcons (head,
4536 Fcons (position,
4537 Qnil));
4540 case mouse_wheel:
4542 int part;
4543 FRAME_PTR f = XFRAME (event->frame_or_window);
4544 Lisp_Object window;
4545 Lisp_Object posn;
4546 Lisp_Object head, position;
4547 int row, column;
4549 /* Ignore mouse events that were made on frame that
4550 have been deleted. */
4551 if (! FRAME_LIVE_P (f))
4552 return Qnil;
4553 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4554 &column, &row, NULL, 1);
4555 window = window_from_coordinates (f, column, row, &part, 0);
4557 if (!WINDOWP (window))
4559 window = event->frame_or_window;
4560 posn = Qnil;
4562 else
4564 int pixcolumn, pixrow;
4565 column -= XINT (XWINDOW (window)->left);
4566 row -= XINT (XWINDOW (window)->top);
4567 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
4568 XSETINT (event->x, pixcolumn);
4569 XSETINT (event->y, pixrow);
4571 if (part == 1)
4572 posn = Qmode_line;
4573 else if (part == 2)
4574 posn = Qvertical_line;
4575 else if (part == 3)
4576 posn = Qheader_line;
4577 else
4578 XSETINT (posn,
4579 buffer_posn_from_coords (XWINDOW (window),
4580 column, row));
4584 Lisp_Object head, position;
4586 position
4587 = Fcons (window,
4588 Fcons (posn,
4589 Fcons (Fcons (event->x, event->y),
4590 Fcons (make_number (event->timestamp),
4591 Qnil))));
4593 head = modify_event_symbol (0, event->modifiers,
4594 Qmouse_wheel, Qnil,
4595 lispy_mouse_wheel_names,
4596 &mouse_wheel_syms, 1);
4597 return Fcons (head,
4598 Fcons (position,
4599 Fcons (make_number (event->code),
4600 Qnil)));
4603 #endif /* WINDOWSNT */
4605 case drag_n_drop:
4607 int part;
4608 FRAME_PTR f;
4609 Lisp_Object window;
4610 Lisp_Object posn;
4611 Lisp_Object files;
4612 int row, column;
4614 /* The frame_or_window field should be a cons of the frame in
4615 which the event occurred and a list of the filenames
4616 dropped. */
4617 if (! CONSP (event->frame_or_window))
4618 abort ();
4620 f = XFRAME (XCAR (event->frame_or_window));
4621 files = XCDR (event->frame_or_window);
4623 /* Ignore mouse events that were made on frames that
4624 have been deleted. */
4625 if (! FRAME_LIVE_P (f))
4626 return Qnil;
4627 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4628 &column, &row, NULL, 1);
4629 window = window_from_coordinates (f, column, row, &part, 0);
4631 if (!WINDOWP (window))
4633 window = XCAR (event->frame_or_window);
4634 posn = Qnil;
4636 else
4638 /* It's an event in window `window' at frame coordinates
4639 event->x/ event->y. */
4640 struct window *w = XWINDOW (window);
4642 /* Get window relative coordinates. */
4643 int wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (event->x));
4644 int wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (event->y));
4646 /* Set event coordinates to window-relative coordinates
4647 for constructing the Lisp event below. */
4648 XSETINT (event->x, wx);
4649 XSETINT (event->y, wy);
4651 if (part == 1)
4652 posn = Qmode_line;
4653 else if (part == 2)
4654 posn = Qvertical_line;
4655 else if (part == 3)
4656 posn = Qheader_line;
4657 else
4658 XSETINT (posn, buffer_posn_from_coords (w, &wx, &wy));
4662 Lisp_Object head, position;
4664 position
4665 = Fcons (window,
4666 Fcons (posn,
4667 Fcons (Fcons (event->x, event->y),
4668 Fcons (make_number (event->timestamp),
4669 Qnil))));
4671 head = modify_event_symbol (0, event->modifiers,
4672 Qdrag_n_drop, Qnil,
4673 lispy_drag_n_drop_names,
4674 &drag_n_drop_syms, 1);
4675 return Fcons (head,
4676 Fcons (position,
4677 Fcons (files,
4678 Qnil)));
4681 #endif /* HAVE_MOUSE */
4683 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
4684 case menu_bar_event:
4685 /* The event value is in the cdr of the frame_or_window slot. */
4686 if (!CONSP (event->frame_or_window))
4687 abort ();
4688 return XCDR (event->frame_or_window);
4689 #endif
4691 case TOOL_BAR_EVENT:
4693 Lisp_Object key;
4694 if (!CONSP (event->frame_or_window))
4695 abort ();
4696 key = XCDR (event->frame_or_window);
4697 if (SYMBOLP (key))
4698 key = apply_modifiers (event->modifiers, key);
4699 return key;
4702 case user_signal:
4703 /* A user signal. */
4704 return *lispy_user_signals[event->code];
4706 /* The 'kind' field of the event is something we don't recognize. */
4707 default:
4708 abort ();
4712 #ifdef HAVE_MOUSE
4714 static Lisp_Object
4715 make_lispy_movement (frame, bar_window, part, x, y, time)
4716 FRAME_PTR frame;
4717 Lisp_Object bar_window;
4718 enum scroll_bar_part part;
4719 Lisp_Object x, y;
4720 unsigned long time;
4722 /* Is it a scroll bar movement? */
4723 if (frame && ! NILP (bar_window))
4725 Lisp_Object part_sym;
4727 part_sym = *scroll_bar_parts[(int) part];
4728 return Fcons (Qscroll_bar_movement,
4729 (Fcons (Fcons (bar_window,
4730 Fcons (Qvertical_scroll_bar,
4731 Fcons (Fcons (x, y),
4732 Fcons (make_number (time),
4733 Fcons (part_sym,
4734 Qnil))))),
4735 Qnil)));
4738 /* Or is it an ordinary mouse movement? */
4739 else
4741 int area;
4742 Lisp_Object window;
4743 Lisp_Object posn;
4745 if (frame)
4746 /* It's in a frame; which window on that frame? */
4747 window = window_from_coordinates (frame, XINT (x), XINT (y), &area, 0);
4748 else
4749 window = Qnil;
4751 if (WINDOWP (window))
4753 struct window *w = XWINDOW (window);
4754 int wx, wy;
4756 /* Get window relative coordinates. */
4757 wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (x));
4758 wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (y));
4759 XSETINT (x, wx);
4760 XSETINT (y, wy);
4762 if (area == 1)
4763 posn = Qmode_line;
4764 else if (area == 2)
4765 posn = Qvertical_line;
4766 else if (area == 3)
4767 posn = Qheader_line;
4768 else
4769 XSETINT (posn, buffer_posn_from_coords (w, &wx, &wy));
4771 else if (frame != 0)
4773 XSETFRAME (window, frame);
4774 posn = Qnil;
4776 else
4778 window = Qnil;
4779 posn = Qnil;
4780 XSETFASTINT (x, 0);
4781 XSETFASTINT (y, 0);
4784 return Fcons (Qmouse_movement,
4785 Fcons (Fcons (window,
4786 Fcons (posn,
4787 Fcons (Fcons (x, y),
4788 Fcons (make_number (time),
4789 Qnil)))),
4790 Qnil));
4794 #endif /* HAVE_MOUSE */
4796 /* Construct a switch frame event. */
4797 static Lisp_Object
4798 make_lispy_switch_frame (frame)
4799 Lisp_Object frame;
4801 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
4804 /* Manipulating modifiers. */
4806 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
4808 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
4809 SYMBOL's name of the end of the modifiers; the string from this
4810 position is the unmodified symbol name.
4812 This doesn't use any caches. */
4814 static int
4815 parse_modifiers_uncached (symbol, modifier_end)
4816 Lisp_Object symbol;
4817 int *modifier_end;
4819 struct Lisp_String *name;
4820 int i;
4821 int modifiers;
4823 CHECK_SYMBOL (symbol, 1);
4825 modifiers = 0;
4826 name = XSYMBOL (symbol)->name;
4828 for (i = 0; i+2 <= STRING_BYTES (name); )
4830 int this_mod_end = 0;
4831 int this_mod = 0;
4833 /* See if the name continues with a modifier word.
4834 Check that the word appears, but don't check what follows it.
4835 Set this_mod and this_mod_end to record what we find. */
4837 switch (name->data[i])
4839 #define SINGLE_LETTER_MOD(BIT) \
4840 (this_mod_end = i + 1, this_mod = BIT)
4842 case 'A':
4843 SINGLE_LETTER_MOD (alt_modifier);
4844 break;
4846 case 'C':
4847 SINGLE_LETTER_MOD (ctrl_modifier);
4848 break;
4850 case 'H':
4851 SINGLE_LETTER_MOD (hyper_modifier);
4852 break;
4854 case 'M':
4855 SINGLE_LETTER_MOD (meta_modifier);
4856 break;
4858 case 'S':
4859 SINGLE_LETTER_MOD (shift_modifier);
4860 break;
4862 case 's':
4863 SINGLE_LETTER_MOD (super_modifier);
4864 break;
4866 #undef SINGLE_LETTER_MOD
4869 /* If we found no modifier, stop looking for them. */
4870 if (this_mod_end == 0)
4871 break;
4873 /* Check there is a dash after the modifier, so that it
4874 really is a modifier. */
4875 if (this_mod_end >= STRING_BYTES (name)
4876 || name->data[this_mod_end] != '-')
4877 break;
4879 /* This modifier is real; look for another. */
4880 modifiers |= this_mod;
4881 i = this_mod_end + 1;
4884 /* Should we include the `click' modifier? */
4885 if (! (modifiers & (down_modifier | drag_modifier
4886 | double_modifier | triple_modifier))
4887 && i + 7 == STRING_BYTES (name)
4888 && strncmp (name->data + i, "mouse-", 6) == 0
4889 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
4890 modifiers |= click_modifier;
4892 if (modifier_end)
4893 *modifier_end = i;
4895 return modifiers;
4898 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
4899 prepended to the string BASE[0..BASE_LEN-1].
4900 This doesn't use any caches. */
4901 static Lisp_Object
4902 apply_modifiers_uncached (modifiers, base, base_len, base_len_byte)
4903 int modifiers;
4904 char *base;
4905 int base_len, base_len_byte;
4907 /* Since BASE could contain nulls, we can't use intern here; we have
4908 to use Fintern, which expects a genuine Lisp_String, and keeps a
4909 reference to it. */
4910 char *new_mods
4911 = (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
4912 int mod_len;
4915 char *p = new_mods;
4917 /* Only the event queue may use the `up' modifier; it should always
4918 be turned into a click or drag event before presented to lisp code. */
4919 if (modifiers & up_modifier)
4920 abort ();
4922 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
4923 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
4924 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
4925 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
4926 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
4927 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
4928 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
4929 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
4930 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
4931 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
4932 /* The click modifier is denoted by the absence of other modifiers. */
4934 *p = '\0';
4936 mod_len = p - new_mods;
4940 Lisp_Object new_name;
4942 new_name = make_uninit_multibyte_string (mod_len + base_len,
4943 mod_len + base_len_byte);
4944 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
4945 bcopy (base, XSTRING (new_name)->data + mod_len, base_len_byte);
4947 return Fintern (new_name, Qnil);
4952 static char *modifier_names[] =
4954 "up", "down", "drag", "click", "double", "triple", 0, 0,
4955 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4956 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
4958 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
4960 static Lisp_Object modifier_symbols;
4962 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
4963 static Lisp_Object
4964 lispy_modifier_list (modifiers)
4965 int modifiers;
4967 Lisp_Object modifier_list;
4968 int i;
4970 modifier_list = Qnil;
4971 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
4972 if (modifiers & (1<<i))
4973 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
4974 modifier_list);
4976 return modifier_list;
4980 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
4981 where UNMODIFIED is the unmodified form of SYMBOL,
4982 MASK is the set of modifiers present in SYMBOL's name.
4983 This is similar to parse_modifiers_uncached, but uses the cache in
4984 SYMBOL's Qevent_symbol_element_mask property, and maintains the
4985 Qevent_symbol_elements property. */
4987 Lisp_Object
4988 parse_modifiers (symbol)
4989 Lisp_Object symbol;
4991 Lisp_Object elements;
4993 elements = Fget (symbol, Qevent_symbol_element_mask);
4994 if (CONSP (elements))
4995 return elements;
4996 else
4998 int end;
4999 int modifiers = parse_modifiers_uncached (symbol, &end);
5000 Lisp_Object unmodified;
5001 Lisp_Object mask;
5003 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
5004 STRING_BYTES (XSYMBOL (symbol)->name) - end),
5005 Qnil);
5007 if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1))
5008 abort ();
5009 XSETFASTINT (mask, modifiers);
5010 elements = Fcons (unmodified, Fcons (mask, Qnil));
5012 /* Cache the parsing results on SYMBOL. */
5013 Fput (symbol, Qevent_symbol_element_mask,
5014 elements);
5015 Fput (symbol, Qevent_symbol_elements,
5016 Fcons (unmodified, lispy_modifier_list (modifiers)));
5018 /* Since we know that SYMBOL is modifiers applied to unmodified,
5019 it would be nice to put that in unmodified's cache.
5020 But we can't, since we're not sure that parse_modifiers is
5021 canonical. */
5023 return elements;
5027 /* Apply the modifiers MODIFIERS to the symbol BASE.
5028 BASE must be unmodified.
5030 This is like apply_modifiers_uncached, but uses BASE's
5031 Qmodifier_cache property, if present. It also builds
5032 Qevent_symbol_elements properties, since it has that info anyway.
5034 apply_modifiers copies the value of BASE's Qevent_kind property to
5035 the modified symbol. */
5036 static Lisp_Object
5037 apply_modifiers (modifiers, base)
5038 int modifiers;
5039 Lisp_Object base;
5041 Lisp_Object cache, index, entry, new_symbol;
5043 /* Mask out upper bits. We don't know where this value's been. */
5044 modifiers &= ((EMACS_INT)1 << VALBITS) - 1;
5046 /* The click modifier never figures into cache indices. */
5047 cache = Fget (base, Qmodifier_cache);
5048 XSETFASTINT (index, (modifiers & ~click_modifier));
5049 entry = assq_no_quit (index, cache);
5051 if (CONSP (entry))
5052 new_symbol = XCDR (entry);
5053 else
5055 /* We have to create the symbol ourselves. */
5056 new_symbol = apply_modifiers_uncached (modifiers,
5057 XSYMBOL (base)->name->data,
5058 XSYMBOL (base)->name->size,
5059 STRING_BYTES (XSYMBOL (base)->name));
5061 /* Add the new symbol to the base's cache. */
5062 entry = Fcons (index, new_symbol);
5063 Fput (base, Qmodifier_cache, Fcons (entry, cache));
5065 /* We have the parsing info now for free, so add it to the caches. */
5066 XSETFASTINT (index, modifiers);
5067 Fput (new_symbol, Qevent_symbol_element_mask,
5068 Fcons (base, Fcons (index, Qnil)));
5069 Fput (new_symbol, Qevent_symbol_elements,
5070 Fcons (base, lispy_modifier_list (modifiers)));
5073 /* Make sure this symbol is of the same kind as BASE.
5075 You'd think we could just set this once and for all when we
5076 intern the symbol above, but reorder_modifiers may call us when
5077 BASE's property isn't set right; we can't assume that just
5078 because it has a Qmodifier_cache property it must have its
5079 Qevent_kind set right as well. */
5080 if (NILP (Fget (new_symbol, Qevent_kind)))
5082 Lisp_Object kind;
5084 kind = Fget (base, Qevent_kind);
5085 if (! NILP (kind))
5086 Fput (new_symbol, Qevent_kind, kind);
5089 return new_symbol;
5093 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
5094 return a symbol with the modifiers placed in the canonical order.
5095 Canonical order is alphabetical, except for down and drag, which
5096 always come last. The 'click' modifier is never written out.
5098 Fdefine_key calls this to make sure that (for example) C-M-foo
5099 and M-C-foo end up being equivalent in the keymap. */
5101 Lisp_Object
5102 reorder_modifiers (symbol)
5103 Lisp_Object symbol;
5105 /* It's hopefully okay to write the code this way, since everything
5106 will soon be in caches, and no consing will be done at all. */
5107 Lisp_Object parsed;
5109 parsed = parse_modifiers (symbol);
5110 return apply_modifiers ((int) XINT (XCAR (XCDR (parsed))),
5111 XCAR (parsed));
5115 /* For handling events, we often want to produce a symbol whose name
5116 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
5117 to some base, like the name of a function key or mouse button.
5118 modify_event_symbol produces symbols of this sort.
5120 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
5121 is the name of the i'th symbol. TABLE_SIZE is the number of elements
5122 in the table.
5124 Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
5125 NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
5127 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
5128 persist between calls to modify_event_symbol that it can use to
5129 store a cache of the symbols it's generated for this NAME_TABLE
5130 before. The object stored there may be a vector or an alist.
5132 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
5134 MODIFIERS is a set of modifier bits (as given in struct input_events)
5135 whose prefixes should be applied to the symbol name.
5137 SYMBOL_KIND is the value to be placed in the event_kind property of
5138 the returned symbol.
5140 The symbols we create are supposed to have an
5141 `event-symbol-elements' property, which lists the modifiers present
5142 in the symbol's name. */
5144 static Lisp_Object
5145 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
5146 name_table, symbol_table, table_size)
5147 int symbol_num;
5148 unsigned modifiers;
5149 Lisp_Object symbol_kind;
5150 Lisp_Object name_alist;
5151 char **name_table;
5152 Lisp_Object *symbol_table;
5153 unsigned int table_size;
5155 Lisp_Object value;
5156 Lisp_Object symbol_int;
5158 /* Get rid of the "vendor-specific" bit here. */
5159 XSETINT (symbol_int, symbol_num & 0xffffff);
5161 /* Is this a request for a valid symbol? */
5162 if (symbol_num < 0 || symbol_num >= table_size)
5163 return Qnil;
5165 if (CONSP (*symbol_table))
5166 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
5168 /* If *symbol_table doesn't seem to be initialized properly, fix that.
5169 *symbol_table should be a lisp vector TABLE_SIZE elements long,
5170 where the Nth element is the symbol for NAME_TABLE[N], or nil if
5171 we've never used that symbol before. */
5172 else
5174 if (! VECTORP (*symbol_table)
5175 || XVECTOR (*symbol_table)->size != table_size)
5177 Lisp_Object size;
5179 XSETFASTINT (size, table_size);
5180 *symbol_table = Fmake_vector (size, Qnil);
5183 value = XVECTOR (*symbol_table)->contents[symbol_num];
5186 /* Have we already used this symbol before? */
5187 if (NILP (value))
5189 /* No; let's create it. */
5190 if (!NILP (name_alist))
5191 value = Fcdr_safe (Fassq (symbol_int, name_alist));
5192 else if (name_table != 0 && name_table[symbol_num])
5193 value = intern (name_table[symbol_num]);
5195 #ifdef HAVE_WINDOW_SYSTEM
5196 if (NILP (value))
5198 char *name = x_get_keysym_name (symbol_num);
5199 if (name)
5200 value = intern (name);
5202 #endif
5204 if (NILP (value))
5206 char buf[20];
5207 sprintf (buf, "key-%d", symbol_num);
5208 value = intern (buf);
5211 if (CONSP (*symbol_table))
5212 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
5213 else
5214 XVECTOR (*symbol_table)->contents[symbol_num] = value;
5216 /* Fill in the cache entries for this symbol; this also
5217 builds the Qevent_symbol_elements property, which the user
5218 cares about. */
5219 apply_modifiers (modifiers & click_modifier, value);
5220 Fput (value, Qevent_kind, symbol_kind);
5223 /* Apply modifiers to that symbol. */
5224 return apply_modifiers (modifiers, value);
5227 /* Convert a list that represents an event type,
5228 such as (ctrl meta backspace), into the usual representation of that
5229 event type as a number or a symbol. */
5231 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
5232 "Convert the event description list EVENT-DESC to an event type.\n\
5233 EVENT-DESC should contain one base event type (a character or symbol)\n\
5234 and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\
5235 drag, down, double or triple). The base must be last.\n\
5236 The return value is an event type (a character or symbol) which\n\
5237 has the same base event type and all the specified modifiers.")
5238 (event_desc)
5239 Lisp_Object event_desc;
5241 Lisp_Object base;
5242 int modifiers = 0;
5243 Lisp_Object rest;
5245 base = Qnil;
5246 rest = event_desc;
5247 while (CONSP (rest))
5249 Lisp_Object elt;
5250 int this = 0;
5252 elt = XCAR (rest);
5253 rest = XCDR (rest);
5255 /* Given a symbol, see if it is a modifier name. */
5256 if (SYMBOLP (elt) && CONSP (rest))
5257 this = parse_solitary_modifier (elt);
5259 if (this != 0)
5260 modifiers |= this;
5261 else if (!NILP (base))
5262 error ("Two bases given in one event");
5263 else
5264 base = elt;
5268 /* Let the symbol A refer to the character A. */
5269 if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1)
5270 XSETINT (base, XSYMBOL (base)->name->data[0]);
5272 if (INTEGERP (base))
5274 /* Turn (shift a) into A. */
5275 if ((modifiers & shift_modifier) != 0
5276 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
5278 XSETINT (base, XINT (base) - ('a' - 'A'));
5279 modifiers &= ~shift_modifier;
5282 /* Turn (control a) into C-a. */
5283 if (modifiers & ctrl_modifier)
5284 return make_number ((modifiers & ~ctrl_modifier)
5285 | make_ctrl_char (XINT (base)));
5286 else
5287 return make_number (modifiers | XINT (base));
5289 else if (SYMBOLP (base))
5290 return apply_modifiers (modifiers, base);
5291 else
5292 error ("Invalid base event");
5295 /* Try to recognize SYMBOL as a modifier name.
5296 Return the modifier flag bit, or 0 if not recognized. */
5298 static int
5299 parse_solitary_modifier (symbol)
5300 Lisp_Object symbol;
5302 struct Lisp_String *name = XSYMBOL (symbol)->name;
5304 switch (name->data[0])
5306 #define SINGLE_LETTER_MOD(BIT) \
5307 if (STRING_BYTES (name) == 1) \
5308 return BIT;
5310 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
5311 if (LEN == STRING_BYTES (name) \
5312 && ! strncmp (name->data, NAME, LEN)) \
5313 return BIT;
5315 case 'A':
5316 SINGLE_LETTER_MOD (alt_modifier);
5317 break;
5319 case 'a':
5320 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
5321 break;
5323 case 'C':
5324 SINGLE_LETTER_MOD (ctrl_modifier);
5325 break;
5327 case 'c':
5328 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
5329 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
5330 break;
5332 case 'H':
5333 SINGLE_LETTER_MOD (hyper_modifier);
5334 break;
5336 case 'h':
5337 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
5338 break;
5340 case 'M':
5341 SINGLE_LETTER_MOD (meta_modifier);
5342 break;
5344 case 'm':
5345 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
5346 break;
5348 case 'S':
5349 SINGLE_LETTER_MOD (shift_modifier);
5350 break;
5352 case 's':
5353 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
5354 MULTI_LETTER_MOD (super_modifier, "super", 5);
5355 SINGLE_LETTER_MOD (super_modifier);
5356 break;
5358 case 'd':
5359 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
5360 MULTI_LETTER_MOD (down_modifier, "down", 4);
5361 MULTI_LETTER_MOD (double_modifier, "double", 6);
5362 break;
5364 case 't':
5365 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
5366 break;
5368 #undef SINGLE_LETTER_MOD
5369 #undef MULTI_LETTER_MOD
5372 return 0;
5375 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
5376 Such a list is not valid as an event,
5377 but it can be a Lucid-style event type list. */
5380 lucid_event_type_list_p (object)
5381 Lisp_Object object;
5383 Lisp_Object tail;
5385 if (! CONSP (object))
5386 return 0;
5388 for (tail = object; CONSP (tail); tail = XCDR (tail))
5390 Lisp_Object elt;
5391 elt = XCAR (tail);
5392 if (! (INTEGERP (elt) || SYMBOLP (elt)))
5393 return 0;
5396 return NILP (tail);
5399 /* Store into *addr a value nonzero if terminal input chars are available.
5400 Serves the purpose of ioctl (0, FIONREAD, addr)
5401 but works even if FIONREAD does not exist.
5402 (In fact, this may actually read some input.)
5404 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe. */
5406 static void
5407 get_input_pending (addr, do_timers_now)
5408 int *addr;
5409 int do_timers_now;
5411 /* First of all, have we already counted some input? */
5412 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
5414 /* If input is being read as it arrives, and we have none, there is none. */
5415 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
5416 return;
5418 /* Try to read some input and see how much we get. */
5419 gobble_input (0);
5420 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
5423 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
5425 void
5426 gobble_input (expected)
5427 int expected;
5429 #ifndef VMS
5430 #ifdef SIGIO
5431 if (interrupt_input)
5433 SIGMASKTYPE mask;
5434 mask = sigblock (sigmask (SIGIO));
5435 read_avail_input (expected);
5436 sigsetmask (mask);
5438 else
5439 #ifdef POLL_FOR_INPUT
5440 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
5442 SIGMASKTYPE mask;
5443 mask = sigblock (sigmask (SIGALRM));
5444 read_avail_input (expected);
5445 sigsetmask (mask);
5447 else
5448 #endif
5449 #endif
5450 read_avail_input (expected);
5451 #endif
5454 /* Put a buffer_switch_event in the buffer
5455 so that read_key_sequence will notice the new current buffer. */
5457 void
5458 record_asynch_buffer_change ()
5460 struct input_event event;
5461 Lisp_Object tem;
5463 event.kind = buffer_switch_event;
5464 event.frame_or_window = Qnil;
5466 #ifdef subprocesses
5467 /* We don't need a buffer-switch event unless Emacs is waiting for input.
5468 The purpose of the event is to make read_key_sequence look up the
5469 keymaps again. If we aren't in read_key_sequence, we don't need one,
5470 and the event could cause trouble by messing up (input-pending-p). */
5471 tem = Fwaiting_for_user_input_p ();
5472 if (NILP (tem))
5473 return;
5474 #else
5475 /* We never need these events if we have no asynchronous subprocesses. */
5476 return;
5477 #endif
5479 /* Make sure no interrupt happens while storing the event. */
5480 #ifdef SIGIO
5481 if (interrupt_input)
5483 SIGMASKTYPE mask;
5484 mask = sigblock (sigmask (SIGIO));
5485 kbd_buffer_store_event (&event);
5486 sigsetmask (mask);
5488 else
5489 #endif
5491 stop_polling ();
5492 kbd_buffer_store_event (&event);
5493 start_polling ();
5497 #ifndef VMS
5499 /* Read any terminal input already buffered up by the system
5500 into the kbd_buffer, but do not wait.
5502 EXPECTED should be nonzero if the caller knows there is some input.
5504 Except on VMS, all input is read by this function.
5505 If interrupt_input is nonzero, this function MUST be called
5506 only when SIGIO is blocked.
5508 Returns the number of keyboard chars read, or -1 meaning
5509 this is a bad time to try to read input. */
5511 static int
5512 read_avail_input (expected)
5513 int expected;
5515 struct input_event buf[KBD_BUFFER_SIZE];
5516 register int i;
5517 int nread;
5519 if (read_socket_hook)
5520 /* No need for FIONREAD or fcntl; just say don't wait. */
5521 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE, expected);
5522 else
5524 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
5525 the kbd_buffer can really hold. That may prevent loss
5526 of characters on some systems when input is stuffed at us. */
5527 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
5528 int n_to_read;
5530 /* Determine how many characters we should *try* to read. */
5531 #ifdef WINDOWSNT
5532 return 0;
5533 #else /* not WINDOWSNT */
5534 #ifdef MSDOS
5535 n_to_read = dos_keysns ();
5536 if (n_to_read == 0)
5537 return 0;
5538 #else /* not MSDOS */
5539 #ifdef FIONREAD
5540 /* Find out how much input is available. */
5541 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
5542 /* Formerly simply reported no input, but that sometimes led to
5543 a failure of Emacs to terminate.
5544 SIGHUP seems appropriate if we can't reach the terminal. */
5545 /* ??? Is it really right to send the signal just to this process
5546 rather than to the whole process group?
5547 Perhaps on systems with FIONREAD Emacs is alone in its group. */
5548 kill (getpid (), SIGHUP);
5549 if (n_to_read == 0)
5550 return 0;
5551 if (n_to_read > sizeof cbuf)
5552 n_to_read = sizeof cbuf;
5553 #else /* no FIONREAD */
5554 #if defined (USG) || defined (DGUX)
5555 /* Read some input if available, but don't wait. */
5556 n_to_read = sizeof cbuf;
5557 fcntl (input_fd, F_SETFL, O_NDELAY);
5558 #else
5559 you lose;
5560 #endif
5561 #endif
5562 #endif /* not MSDOS */
5563 #endif /* not WINDOWSNT */
5565 /* Now read; for one reason or another, this will not block.
5566 NREAD is set to the number of chars read. */
5569 #ifdef MSDOS
5570 cbuf[0] = dos_keyread ();
5571 nread = 1;
5572 #else
5573 nread = emacs_read (input_fd, cbuf, n_to_read);
5574 #endif
5575 /* POSIX infers that processes which are not in the session leader's
5576 process group won't get SIGHUP's at logout time. BSDI adheres to
5577 this part standard and returns -1 from read (0) with errno==EIO
5578 when the control tty is taken away.
5579 Jeffrey Honig <jch@bsdi.com> says this is generally safe. */
5580 if (nread == -1 && errno == EIO)
5581 kill (0, SIGHUP);
5582 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
5583 /* The kernel sometimes fails to deliver SIGHUP for ptys.
5584 This looks incorrect, but it isn't, because _BSD causes
5585 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
5586 and that causes a value other than 0 when there is no input. */
5587 if (nread == 0)
5588 kill (0, SIGHUP);
5589 #endif
5591 while (
5592 /* We used to retry the read if it was interrupted.
5593 But this does the wrong thing when O_NDELAY causes
5594 an EAGAIN error. Does anybody know of a situation
5595 where a retry is actually needed? */
5596 #if 0
5597 nread < 0 && (errno == EAGAIN
5598 #ifdef EFAULT
5599 || errno == EFAULT
5600 #endif
5601 #ifdef EBADSLT
5602 || errno == EBADSLT
5603 #endif
5605 #else
5607 #endif
5610 #ifndef FIONREAD
5611 #if defined (USG) || defined (DGUX)
5612 fcntl (input_fd, F_SETFL, 0);
5613 #endif /* USG or DGUX */
5614 #endif /* no FIONREAD */
5615 for (i = 0; i < nread; i++)
5617 buf[i].kind = ascii_keystroke;
5618 buf[i].modifiers = 0;
5619 if (meta_key == 1 && (cbuf[i] & 0x80))
5620 buf[i].modifiers = meta_modifier;
5621 if (meta_key != 2)
5622 cbuf[i] &= ~0x80;
5624 buf[i].code = cbuf[i];
5625 buf[i].frame_or_window = selected_frame;
5629 /* Scan the chars for C-g and store them in kbd_buffer. */
5630 for (i = 0; i < nread; i++)
5632 kbd_buffer_store_event (&buf[i]);
5633 /* Don't look at input that follows a C-g too closely.
5634 This reduces lossage due to autorepeat on C-g. */
5635 if (buf[i].kind == ascii_keystroke
5636 && buf[i].code == quit_char)
5637 break;
5640 return nread;
5642 #endif /* not VMS */
5644 #ifdef SIGIO /* for entire page */
5645 /* Note SIGIO has been undef'd if FIONREAD is missing. */
5647 SIGTYPE
5648 input_available_signal (signo)
5649 int signo;
5651 /* Must preserve main program's value of errno. */
5652 int old_errno = errno;
5653 #ifdef BSD4_1
5654 extern int select_alarmed;
5655 #endif
5657 #if defined (USG) && !defined (POSIX_SIGNALS)
5658 /* USG systems forget handlers when they are used;
5659 must reestablish each time */
5660 signal (signo, input_available_signal);
5661 #endif /* USG */
5663 #ifdef BSD4_1
5664 sigisheld (SIGIO);
5665 #endif
5667 if (input_available_clear_time)
5668 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
5670 while (1)
5672 int nread;
5673 nread = read_avail_input (1);
5674 /* -1 means it's not ok to read the input now.
5675 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
5676 0 means there was no keyboard input available. */
5677 if (nread <= 0)
5678 break;
5680 #ifdef BSD4_1
5681 select_alarmed = 1; /* Force the select emulator back to life */
5682 #endif
5685 #ifdef BSD4_1
5686 sigfree ();
5687 #endif
5688 errno = old_errno;
5690 #endif /* SIGIO */
5692 /* Send ourselves a SIGIO.
5694 This function exists so that the UNBLOCK_INPUT macro in
5695 blockinput.h can have some way to take care of input we put off
5696 dealing with, without assuming that every file which uses
5697 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
5698 void
5699 reinvoke_input_signal ()
5701 #ifdef SIGIO
5702 kill (getpid (), SIGIO);
5703 #endif
5708 /* Return the prompt-string of a sparse keymap.
5709 This is the first element which is a string.
5710 Return nil if there is none. */
5712 Lisp_Object
5713 map_prompt (map)
5714 Lisp_Object map;
5716 while (CONSP (map))
5718 register Lisp_Object tem;
5719 tem = Fcar (map);
5720 if (STRINGP (tem))
5721 return tem;
5722 map = Fcdr (map);
5724 return Qnil;
5727 static void menu_bar_item ();
5728 static void menu_bar_one_keymap ();
5730 /* These variables hold the vector under construction within
5731 menu_bar_items and its subroutines, and the current index
5732 for storing into that vector. */
5733 static Lisp_Object menu_bar_items_vector;
5734 static int menu_bar_items_index;
5736 /* Return a vector of menu items for a menu bar, appropriate
5737 to the current buffer. Each item has three elements in the vector:
5738 KEY STRING MAPLIST.
5740 OLD is an old vector we can optionally reuse, or nil. */
5742 Lisp_Object
5743 menu_bar_items (old)
5744 Lisp_Object old;
5746 /* The number of keymaps we're scanning right now, and the number of
5747 keymaps we have allocated space for. */
5748 int nmaps;
5750 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
5751 in the current keymaps, or nil where it is not a prefix. */
5752 Lisp_Object *maps;
5754 Lisp_Object def, tem, tail;
5756 Lisp_Object result;
5758 int mapno;
5759 Lisp_Object oquit;
5761 int i;
5763 struct gcpro gcpro1;
5765 /* In order to build the menus, we need to call the keymap
5766 accessors. They all call QUIT. But this function is called
5767 during redisplay, during which a quit is fatal. So inhibit
5768 quitting while building the menus.
5769 We do this instead of specbind because (1) errors will clear it anyway
5770 and (2) this avoids risk of specpdl overflow. */
5771 oquit = Vinhibit_quit;
5772 Vinhibit_quit = Qt;
5774 if (!NILP (old))
5775 menu_bar_items_vector = old;
5776 else
5777 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
5778 menu_bar_items_index = 0;
5780 GCPRO1 (menu_bar_items_vector);
5782 /* Build our list of keymaps.
5783 If we recognize a function key and replace its escape sequence in
5784 keybuf with its symbol, or if the sequence starts with a mouse
5785 click and we need to switch buffers, we jump back here to rebuild
5786 the initial keymaps from the current buffer. */
5788 Lisp_Object *tmaps;
5790 /* Should overriding-terminal-local-map and overriding-local-map apply? */
5791 if (!NILP (Voverriding_local_map_menu_flag))
5793 /* Yes, use them (if non-nil) as well as the global map. */
5794 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
5795 nmaps = 0;
5796 if (!NILP (current_kboard->Voverriding_terminal_local_map))
5797 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
5798 if (!NILP (Voverriding_local_map))
5799 maps[nmaps++] = Voverriding_local_map;
5801 else
5803 /* No, so use major and minor mode keymaps. */
5804 nmaps = current_minor_maps (NULL, &tmaps);
5805 maps = (Lisp_Object *) alloca ((nmaps + 2) * sizeof (maps[0]));
5806 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
5807 #ifdef USE_TEXT_PROPERTIES
5808 maps[nmaps++] = get_local_map (PT, current_buffer);
5809 #else
5810 maps[nmaps++] = current_buffer->keymap;
5811 #endif
5813 maps[nmaps++] = current_global_map;
5816 /* Look up in each map the dummy prefix key `menu-bar'. */
5818 result = Qnil;
5820 for (mapno = nmaps - 1; mapno >= 0; mapno--)
5822 if (! NILP (maps[mapno]))
5823 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0), 0);
5824 else
5825 def = Qnil;
5827 tem = Fkeymapp (def);
5828 if (!NILP (tem))
5829 menu_bar_one_keymap (def);
5832 /* Move to the end those items that should be at the end. */
5834 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCDR (tail))
5836 int i;
5837 int end = menu_bar_items_index;
5839 for (i = 0; i < end; i += 4)
5840 if (EQ (XCAR (tail), XVECTOR (menu_bar_items_vector)->contents[i]))
5842 Lisp_Object tem0, tem1, tem2, tem3;
5843 /* Move the item at index I to the end,
5844 shifting all the others forward. */
5845 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
5846 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
5847 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
5848 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
5849 if (end > i + 4)
5850 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
5851 &XVECTOR (menu_bar_items_vector)->contents[i],
5852 (end - i - 4) * sizeof (Lisp_Object));
5853 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
5854 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
5855 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
5856 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
5857 break;
5861 /* Add nil, nil, nil, nil at the end. */
5862 i = menu_bar_items_index;
5863 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
5865 Lisp_Object tem;
5866 tem = Fmake_vector (make_number (2 * i), Qnil);
5867 bcopy (XVECTOR (menu_bar_items_vector)->contents,
5868 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
5869 menu_bar_items_vector = tem;
5871 /* Add this item. */
5872 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5873 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5874 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5875 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
5876 menu_bar_items_index = i;
5878 Vinhibit_quit = oquit;
5879 UNGCPRO;
5880 return menu_bar_items_vector;
5883 /* Scan one map KEYMAP, accumulating any menu items it defines
5884 in menu_bar_items_vector. */
5886 static Lisp_Object menu_bar_one_keymap_changed_items;
5888 static void
5889 menu_bar_one_keymap (keymap)
5890 Lisp_Object keymap;
5892 Lisp_Object tail, item;
5894 menu_bar_one_keymap_changed_items = Qnil;
5896 /* Loop over all keymap entries that have menu strings. */
5897 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
5899 item = XCAR (tail);
5900 if (CONSP (item))
5901 menu_bar_item (XCAR (item), XCDR (item));
5902 else if (VECTORP (item))
5904 /* Loop over the char values represented in the vector. */
5905 int len = XVECTOR (item)->size;
5906 int c;
5907 for (c = 0; c < len; c++)
5909 Lisp_Object character;
5910 XSETFASTINT (character, c);
5911 menu_bar_item (character, XVECTOR (item)->contents[c]);
5917 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
5918 If there's already an item for KEY, add this DEF to it. */
5920 Lisp_Object item_properties;
5922 static void
5923 menu_bar_item (key, item)
5924 Lisp_Object key, item;
5926 struct gcpro gcpro1;
5927 int i;
5928 Lisp_Object tem;
5930 if (EQ (item, Qundefined))
5932 /* If a map has an explicit `undefined' as definition,
5933 discard any previously made menu bar item. */
5935 for (i = 0; i < menu_bar_items_index; i += 4)
5936 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
5938 if (menu_bar_items_index > i + 4)
5939 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
5940 &XVECTOR (menu_bar_items_vector)->contents[i],
5941 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
5942 menu_bar_items_index -= 4;
5943 return;
5946 /* If there's no definition for this key yet,
5947 just ignore `undefined'. */
5948 return;
5951 GCPRO1 (key); /* Is this necessary? */
5952 i = parse_menu_item (item, 0, 1);
5953 UNGCPRO;
5954 if (!i)
5955 return;
5957 /* If this keymap has already contributed to this KEY,
5958 don't contribute to it a second time. */
5959 tem = Fmemq (key, menu_bar_one_keymap_changed_items);
5960 if (!NILP (tem))
5961 return;
5963 menu_bar_one_keymap_changed_items
5964 = Fcons (key, menu_bar_one_keymap_changed_items);
5966 item = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
5968 /* Find any existing item for this KEY. */
5969 for (i = 0; i < menu_bar_items_index; i += 4)
5970 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
5971 break;
5973 /* If we did not find this KEY, add it at the end. */
5974 if (i == menu_bar_items_index)
5976 /* If vector is too small, get a bigger one. */
5977 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
5979 Lisp_Object tem;
5980 tem = Fmake_vector (make_number (2 * i), Qnil);
5981 bcopy (XVECTOR (menu_bar_items_vector)->contents,
5982 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
5983 menu_bar_items_vector = tem;
5986 /* Add this item. */
5987 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
5988 XVECTOR (menu_bar_items_vector)->contents[i++]
5989 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
5990 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (item, Qnil);
5991 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
5992 menu_bar_items_index = i;
5994 /* We did find an item for this KEY. Add ITEM to its list of maps. */
5995 else
5997 Lisp_Object old;
5998 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
5999 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (item, old);
6003 /* This is used as the handler when calling menu_item_eval_property. */
6004 static Lisp_Object
6005 menu_item_eval_property_1 (arg)
6006 Lisp_Object arg;
6008 /* If we got a quit from within the menu computation,
6009 quit all the way out of it. This takes care of C-] in the debugger. */
6010 if (CONSP (arg) && EQ (XCAR (arg), Qquit))
6011 Fsignal (Qquit, Qnil);
6013 return Qnil;
6016 /* Evaluate an expression and return the result (or nil if something
6017 went wrong). Used to evaluate dynamic parts of menu items. */
6018 Lisp_Object
6019 menu_item_eval_property (sexpr)
6020 Lisp_Object sexpr;
6022 int count = specpdl_ptr - specpdl;
6023 Lisp_Object val;
6024 specbind (Qinhibit_redisplay, Qt);
6025 val = internal_condition_case_1 (Feval, sexpr, Qerror,
6026 menu_item_eval_property_1);
6027 return unbind_to (count, val);
6030 /* This function parses a menu item and leaves the result in the
6031 vector item_properties.
6032 ITEM is a key binding, a possible menu item.
6033 If NOTREAL is nonzero, only check for equivalent key bindings, don't
6034 evaluate dynamic expressions in the menu item.
6035 INMENUBAR is > 0 when this is considered for an entry in a menu bar
6036 top level.
6037 INMENUBAR is < 0 when this is considered for an entry in a keyboard menu.
6038 parse_menu_item returns true if the item is a menu item and false
6039 otherwise. */
6042 parse_menu_item (item, notreal, inmenubar)
6043 Lisp_Object item;
6044 int notreal, inmenubar;
6046 Lisp_Object def, tem, item_string, start;
6047 Lisp_Object cachelist;
6048 Lisp_Object filter;
6049 Lisp_Object keyhint;
6050 int i;
6051 int newcache = 0;
6053 cachelist = Qnil;
6054 filter = Qnil;
6055 keyhint = Qnil;
6057 if (!CONSP (item))
6058 return 0;
6060 /* Create item_properties vector if necessary. */
6061 if (NILP (item_properties))
6062 item_properties
6063 = Fmake_vector (make_number (ITEM_PROPERTY_ENABLE + 1), Qnil);
6065 /* Initialize optional entries. */
6066 for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++)
6067 XVECTOR (item_properties)->contents[i] = Qnil;
6068 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = Qt;
6070 /* Save the item here to protect it from GC. */
6071 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ITEM] = item;
6073 item_string = XCAR (item);
6075 start = item;
6076 item = XCDR (item);
6077 if (STRINGP (item_string))
6079 /* Old format menu item. */
6080 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME] = item_string;
6082 /* Maybe help string. */
6083 if (CONSP (item) && STRINGP (XCAR (item)))
6085 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]
6086 = XCAR (item);
6087 start = item;
6088 item = XCDR (item);
6091 /* Maybee key binding cache. */
6092 if (CONSP (item) && CONSP (XCAR (item))
6093 && (NILP (XCAR (XCAR (item)))
6094 || VECTORP (XCAR (XCAR (item)))))
6096 cachelist = XCAR (item);
6097 item = XCDR (item);
6100 /* This is the real definition--the function to run. */
6101 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = item;
6103 /* Get enable property, if any. */
6104 if (SYMBOLP (item))
6106 tem = Fget (item, Qmenu_enable);
6107 if (!NILP (tem))
6108 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = tem;
6111 else if (EQ (item_string, Qmenu_item) && CONSP (item))
6113 /* New format menu item. */
6114 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME]
6115 = XCAR (item);
6116 start = XCDR (item);
6117 if (CONSP (start))
6119 /* We have a real binding. */
6120 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF]
6121 = XCAR (start);
6123 item = XCDR (start);
6124 /* Is there a cache list with key equivalences. */
6125 if (CONSP (item) && CONSP (XCAR (item)))
6127 cachelist = XCAR (item);
6128 item = XCDR (item);
6131 /* Parse properties. */
6132 while (CONSP (item) && CONSP (XCDR (item)))
6134 tem = XCAR (item);
6135 item = XCDR (item);
6137 if (EQ (tem, QCenable))
6138 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE]
6139 = XCAR (item);
6140 else if (EQ (tem, QCvisible) && !notreal)
6142 /* If got a visible property and that evaluates to nil
6143 then ignore this item. */
6144 tem = menu_item_eval_property (XCAR (item));
6145 if (NILP (tem))
6146 return 0;
6148 else if (EQ (tem, QChelp))
6149 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]
6150 = XCAR (item);
6151 else if (EQ (tem, QCfilter))
6152 filter = item;
6153 else if (EQ (tem, QCkey_sequence))
6155 tem = XCAR (item);
6156 if (NILP (cachelist)
6157 && (SYMBOLP (tem) || STRINGP (tem) || VECTORP (tem)))
6158 /* Be GC protected. Set keyhint to item instead of tem. */
6159 keyhint = item;
6161 else if (EQ (tem, QCkeys))
6163 tem = XCAR (item);
6164 if (CONSP (tem) || (STRINGP (tem) && NILP (cachelist)))
6165 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ]
6166 = tem;
6168 else if (EQ (tem, QCbutton) && CONSP (XCAR (item)))
6170 Lisp_Object type;
6171 tem = XCAR (item);
6172 type = XCAR (tem);
6173 if (EQ (type, QCtoggle) || EQ (type, QCradio))
6175 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]
6176 = XCDR (tem);
6177 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE]
6178 = type;
6181 item = XCDR (item);
6184 else if (inmenubar || !NILP (start))
6185 return 0;
6187 else
6188 return 0; /* not a menu item */
6190 /* If item string is not a string, evaluate it to get string.
6191 If we don't get a string, skip this item. */
6192 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
6193 if (!(STRINGP (item_string) || notreal))
6195 item_string = menu_item_eval_property (item_string);
6196 if (!STRINGP (item_string))
6197 return 0;
6198 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME] = item_string;
6201 /* If got a filter apply it on definition. */
6202 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6203 if (!NILP (filter))
6205 def = menu_item_eval_property (list2 (XCAR (filter),
6206 list2 (Qquote, def)));
6208 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = def;
6211 /* If we got no definition, this item is just unselectable text which
6212 is OK in a submenu but not in the menubar. */
6213 if (NILP (def))
6214 return (inmenubar ? 0 : 1);
6216 /* Enable or disable selection of item. */
6217 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
6218 if (!EQ (tem, Qt))
6220 if (notreal)
6221 tem = Qt;
6222 else
6223 tem = menu_item_eval_property (tem);
6224 if (inmenubar && NILP (tem))
6225 return 0; /* Ignore disabled items in menu bar. */
6226 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = tem;
6229 /* See if this is a separate pane or a submenu. */
6230 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6231 tem = get_keymap_1 (def, 0, 1);
6232 /* For a subkeymap, just record its details and exit. */
6233 if (!NILP (tem))
6235 XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP] = tem;
6236 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = tem;
6237 return 1;
6239 /* At the top level in the menu bar, do likewise for commands also.
6240 The menu bar does not display equivalent key bindings anyway.
6241 ITEM_PROPERTY_DEF is already set up properly. */
6242 if (inmenubar > 0)
6243 return 1;
6245 /* This is a command. See if there is an equivalent key binding. */
6246 if (NILP (cachelist))
6248 /* We have to create a cachelist. */
6249 CHECK_IMPURE (start);
6250 XCDR (start) = Fcons (Fcons (Qnil, Qnil), XCDR (start));
6251 cachelist = XCAR (XCDR (start));
6252 newcache = 1;
6253 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
6254 if (!NILP (keyhint))
6256 XCAR (cachelist) = XCAR (keyhint);
6257 newcache = 0;
6259 else if (STRINGP (tem))
6261 XCDR (cachelist) = Fsubstitute_command_keys (tem);
6262 XCAR (cachelist) = Qt;
6265 tem = XCAR (cachelist);
6266 if (!EQ (tem, Qt))
6268 int chkcache = 0;
6269 Lisp_Object prefix;
6271 if (!NILP (tem))
6272 tem = Fkey_binding (tem, Qnil);
6274 prefix = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
6275 if (CONSP (prefix))
6277 def = XCAR (prefix);
6278 prefix = XCDR (prefix);
6280 else
6281 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6283 if (NILP (XCAR (cachelist))) /* Have no saved key. */
6285 if (newcache /* Always check first time. */
6286 /* Should we check everything when precomputing key
6287 bindings? */
6288 /* || notreal */
6289 /* If something had no key binding before, don't recheck it
6290 because that is too slow--except if we have a list of
6291 rebound commands in Vdefine_key_rebound_commands, do
6292 recheck any command that appears in that list. */
6293 || (CONSP (Vdefine_key_rebound_commands)
6294 && !NILP (Fmemq (def, Vdefine_key_rebound_commands))))
6295 chkcache = 1;
6297 /* We had a saved key. Is it still bound to the command? */
6298 else if (NILP (tem)
6299 || (!EQ (tem, def)
6300 /* If the command is an alias for another
6301 (such as lmenu.el set it up), check if the
6302 original command matches the cached command. */
6303 && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function))))
6304 chkcache = 1; /* Need to recompute key binding. */
6306 if (chkcache)
6308 /* Recompute equivalent key binding. If the command is an alias
6309 for another (such as lmenu.el set it up), see if the original
6310 command name has equivalent keys. Otherwise look up the
6311 specified command itself. We don't try both, because that
6312 makes lmenu menus slow. */
6313 if (SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
6314 && ! NILP (Fget (def, Qmenu_alias)))
6315 def = XSYMBOL (def)->function;
6316 tem = Fwhere_is_internal (def, Qnil, Qt, Qnil);
6317 XCAR (cachelist) = tem;
6318 if (NILP (tem))
6320 XCDR (cachelist) = Qnil;
6321 chkcache = 0;
6324 else if (!NILP (keyhint) && !NILP (XCAR (cachelist)))
6326 tem = XCAR (cachelist);
6327 chkcache = 1;
6330 newcache = chkcache;
6331 if (chkcache)
6333 tem = Fkey_description (tem);
6334 if (CONSP (prefix))
6336 if (STRINGP (XCAR (prefix)))
6337 tem = concat2 (XCAR (prefix), tem);
6338 if (STRINGP (XCDR (prefix)))
6339 tem = concat2 (tem, XCDR (prefix));
6341 XCDR (cachelist) = tem;
6345 tem = XCDR (cachelist);
6346 if (newcache && !NILP (tem))
6348 tem = concat3 (build_string (" ("), tem, build_string (")"));
6349 XCDR (cachelist) = tem;
6352 /* If we only want to precompute equivalent key bindings, stop here. */
6353 if (notreal)
6354 return 1;
6356 /* If we have an equivalent key binding, use that. */
6357 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ] = tem;
6359 /* Include this when menu help is implemented.
6360 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP];
6361 if (!(NILP (tem) || STRINGP (tem)))
6363 tem = menu_item_eval_property (tem);
6364 if (!STRINGP (tem))
6365 tem = Qnil;
6366 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP] = tem;
6370 /* Handle radio buttons or toggle boxes. */
6371 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
6372 if (!NILP (tem))
6373 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]
6374 = menu_item_eval_property (tem);
6376 return 1;
6381 /***********************************************************************
6382 Tool-bars
6383 ***********************************************************************/
6385 /* A vector holding tool bar items while they are parsed in function
6386 tool_bar_items runs Each item occupies TOOL_BAR_ITEM_NSCLOTS elements
6387 in the vector. */
6389 static Lisp_Object tool_bar_items_vector;
6391 /* A vector holding the result of parse_tool_bar_item. Layout is like
6392 the one for a single item in tool_bar_items_vector. */
6394 static Lisp_Object tool_bar_item_properties;
6396 /* Next free index in tool_bar_items_vector. */
6398 static int ntool_bar_items;
6400 /* The symbols `tool-bar', and `:image'. */
6402 extern Lisp_Object Qtool_bar;
6403 Lisp_Object QCimage;
6405 /* Function prototypes. */
6407 static void init_tool_bar_items P_ ((Lisp_Object));
6408 static void process_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
6409 static int parse_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
6410 static void append_tool_bar_item P_ ((void));
6413 /* Return a vector of tool bar items for keymaps currently in effect.
6414 Reuse vector REUSE if non-nil. Return in *NITEMS the number of
6415 tool bar items found. */
6417 Lisp_Object
6418 tool_bar_items (reuse, nitems)
6419 Lisp_Object reuse;
6420 int *nitems;
6422 Lisp_Object *maps;
6423 int nmaps, i;
6424 Lisp_Object oquit;
6425 Lisp_Object *tmaps;
6426 extern Lisp_Object Voverriding_local_map_menu_flag;
6427 extern Lisp_Object Voverriding_local_map;
6429 *nitems = 0;
6431 /* In order to build the menus, we need to call the keymap
6432 accessors. They all call QUIT. But this function is called
6433 during redisplay, during which a quit is fatal. So inhibit
6434 quitting while building the menus. We do this instead of
6435 specbind because (1) errors will clear it anyway and (2) this
6436 avoids risk of specpdl overflow. */
6437 oquit = Vinhibit_quit;
6438 Vinhibit_quit = Qt;
6440 /* Initialize tool_bar_items_vector and protect it from GC. */
6441 init_tool_bar_items (reuse);
6443 /* Build list of keymaps in maps. Set nmaps to the number of maps
6444 to process. */
6446 /* Should overriding-terminal-local-map and overriding-local-map apply? */
6447 if (!NILP (Voverriding_local_map_menu_flag))
6449 /* Yes, use them (if non-nil) as well as the global map. */
6450 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
6451 nmaps = 0;
6452 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6453 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6454 if (!NILP (Voverriding_local_map))
6455 maps[nmaps++] = Voverriding_local_map;
6457 else
6459 /* No, so use major and minor mode keymaps. */
6460 nmaps = current_minor_maps (NULL, &tmaps);
6461 maps = (Lisp_Object *) alloca ((nmaps + 2) * sizeof (maps[0]));
6462 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
6463 #ifdef USE_TEXT_PROPERTIES
6464 maps[nmaps++] = get_local_map (PT, current_buffer);
6465 #else
6466 maps[nmaps++] = current_buffer->keymap;
6467 #endif
6470 /* Add global keymap at the end. */
6471 maps[nmaps++] = current_global_map;
6473 /* Process maps in reverse order and look up in each map the prefix
6474 key `tool-bar'. */
6475 for (i = nmaps - 1; i >= 0; --i)
6476 if (!NILP (maps[i]))
6478 Lisp_Object keymap;
6480 keymap = get_keyelt (access_keymap (maps[i], Qtool_bar, 1, 1), 0);
6481 if (!NILP (Fkeymapp (keymap)))
6483 Lisp_Object tail;
6485 /* KEYMAP is a list `(keymap (KEY . BINDING) ...)'. */
6486 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
6488 Lisp_Object keydef = XCAR (tail);
6489 if (CONSP (keydef))
6490 process_tool_bar_item (XCAR (keydef), XCDR (keydef));
6495 Vinhibit_quit = oquit;
6496 *nitems = ntool_bar_items / TOOL_BAR_ITEM_NSLOTS;
6497 return tool_bar_items_vector;
6501 /* Process the definition of KEY which is DEF. */
6503 static void
6504 process_tool_bar_item (key, def)
6505 Lisp_Object key, def;
6507 int i;
6508 extern Lisp_Object Qundefined;
6509 struct gcpro gcpro1, gcpro2;
6511 /* Protect KEY and DEF from GC because parse_tool_bar_item may call
6512 eval. */
6513 GCPRO2 (key, def);
6515 if (EQ (def, Qundefined))
6517 /* If a map has an explicit `undefined' as definition,
6518 discard any previously made item. */
6519 for (i = 0; i < ntool_bar_items; i += TOOL_BAR_ITEM_NSLOTS)
6521 Lisp_Object *v = XVECTOR (tool_bar_items_vector)->contents + i;
6523 if (EQ (key, v[TOOL_BAR_ITEM_KEY]))
6525 if (ntool_bar_items > i + TOOL_BAR_ITEM_NSLOTS)
6526 bcopy (v + TOOL_BAR_ITEM_NSLOTS, v,
6527 ((ntool_bar_items - i - TOOL_BAR_ITEM_NSLOTS)
6528 * sizeof (Lisp_Object)));
6529 ntool_bar_items -= TOOL_BAR_ITEM_NSLOTS;
6530 break;
6534 else if (parse_tool_bar_item (key, def))
6535 /* Append a new tool bar item to tool_bar_items_vector. Accept
6536 more than one definition for the same key. */
6537 append_tool_bar_item ();
6539 UNGCPRO;
6543 /* Parse a tool bar item specification ITEM for key KEY and return the
6544 result in tool_bar_item_properties. Value is zero if ITEM is
6545 invalid.
6547 ITEM is a list `(menu-item CAPTION BINDING PROPS...)'.
6549 CAPTION is the caption of the item, If it's not a string, it is
6550 evaluated to get a string.
6552 BINDING is the tool bar item's binding. Tool-bar items with keymaps
6553 as binding are currently ignored.
6555 The following properties are recognized:
6557 - `:enable FORM'.
6559 FORM is evaluated and specifies whether the tool bar item is
6560 enabled or disabled.
6562 - `:visible FORM'
6564 FORM is evaluated and specifies whether the tool bar item is visible.
6566 - `:filter FUNCTION'
6568 FUNCTION is invoked with one parameter `(quote BINDING)'. Its
6569 result is stored as the new binding.
6571 - `:button (TYPE SELECTED)'
6573 TYPE must be one of `:radio' or `:toggle'. SELECTED is evaluated
6574 and specifies whether the button is selected (pressed) or not.
6576 - `:image IMAGES'
6578 IMAGES is either a single image specification or a vector of four
6579 image specifications. See enum tool_bar_item_images.
6581 - `:help HELP-STRING'.
6583 Gives a help string to display for the tool bar item. */
6585 static int
6586 parse_tool_bar_item (key, item)
6587 Lisp_Object key, item;
6589 /* Access slot with index IDX of vector tool_bar_item_properties. */
6590 #define PROP(IDX) XVECTOR (tool_bar_item_properties)->contents[IDX]
6592 Lisp_Object filter = Qnil;
6593 Lisp_Object caption;
6594 extern Lisp_Object QCenable, QCvisible, QChelp, QCfilter;
6595 extern Lisp_Object QCbutton, QCtoggle, QCradio;
6596 int i;
6598 /* Defininition looks like `(tool-bar-item CAPTION BINDING
6599 PROPS...)'. Rule out items that aren't lists, don't start with
6600 `tool-bar-item' or whose rest following `tool-bar-item' is not a
6601 list. */
6602 if (!CONSP (item)
6603 || !EQ (XCAR (item), Qmenu_item)
6604 || (item = XCDR (item),
6605 !CONSP (item)))
6606 return 0;
6608 /* Create tool_bar_item_properties vector if necessary. Reset it to
6609 defaults. */
6610 if (VECTORP (tool_bar_item_properties))
6612 for (i = 0; i < TOOL_BAR_ITEM_NSLOTS; ++i)
6613 PROP (i) = Qnil;
6615 else
6616 tool_bar_item_properties
6617 = Fmake_vector (make_number (TOOL_BAR_ITEM_NSLOTS), Qnil);
6619 /* Set defaults. */
6620 PROP (TOOL_BAR_ITEM_KEY) = key;
6621 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
6623 /* Get the caption of the item. If the caption is not a string,
6624 evaluate it to get a string. If we don't get a string, skip this
6625 item. */
6626 caption = XCAR (item);
6627 if (!STRINGP (caption))
6629 caption = menu_item_eval_property (caption);
6630 if (!STRINGP (caption))
6631 return 0;
6633 PROP (TOOL_BAR_ITEM_CAPTION) = caption;
6635 /* Give up if rest following the caption is not a list. */
6636 item = XCDR (item);
6637 if (!CONSP (item))
6638 return 0;
6640 /* Store the binding. */
6641 PROP (TOOL_BAR_ITEM_BINDING) = XCAR (item);
6642 item = XCDR (item);
6644 /* Process the rest of the properties. */
6645 for (; CONSP (item) && CONSP (XCDR (item)); item = XCDR (XCDR (item)))
6647 Lisp_Object key, value;
6649 key = XCAR (item);
6650 value = XCAR (XCDR (item));
6652 if (EQ (key, QCenable))
6653 /* `:enable FORM'. */
6654 PROP (TOOL_BAR_ITEM_ENABLED_P) = value;
6655 else if (EQ (key, QCvisible))
6657 /* `:visible FORM'. If got a visible property and that
6658 evaluates to nil then ignore this item. */
6659 if (NILP (menu_item_eval_property (value)))
6660 return 0;
6662 else if (EQ (key, QChelp))
6663 /* `:help HELP-STRING'. */
6664 PROP (TOOL_BAR_ITEM_HELP) = value;
6665 else if (EQ (key, QCfilter))
6666 /* ':filter FORM'. */
6667 filter = value;
6668 else if (EQ (key, QCbutton) && CONSP (value))
6670 /* `:button (TYPE . SELECTED)'. */
6671 Lisp_Object type, selected;
6673 type = XCAR (value);
6674 selected = XCDR (value);
6675 if (EQ (type, QCtoggle) || EQ (type, QCradio))
6677 PROP (TOOL_BAR_ITEM_SELECTED_P) = selected;
6678 PROP (TOOL_BAR_ITEM_TYPE) = type;
6681 else if (EQ (key, QCimage)
6682 && (CONSP (value)
6683 || (VECTORP (value) && XVECTOR (value)->size == 4)))
6684 /* Value is either a single image specification or a vector
6685 of 4 such specifications for the different buttion states. */
6686 PROP (TOOL_BAR_ITEM_IMAGES) = value;
6689 /* If got a filter apply it on binding. */
6690 if (!NILP (filter))
6691 PROP (TOOL_BAR_ITEM_BINDING)
6692 = menu_item_eval_property (list2 (filter,
6693 list2 (Qquote,
6694 PROP (TOOL_BAR_ITEM_BINDING))));
6696 /* See if the binding is a keymap. Give up if it is. */
6697 if (!NILP (get_keymap_1 (PROP (TOOL_BAR_ITEM_BINDING), 0, 1)))
6698 return 0;
6700 /* Enable or disable selection of item. */
6701 if (!EQ (PROP (TOOL_BAR_ITEM_ENABLED_P), Qt))
6702 PROP (TOOL_BAR_ITEM_ENABLED_P)
6703 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_ENABLED_P));
6705 /* Handle radio buttons or toggle boxes. */
6706 if (!NILP (PROP (TOOL_BAR_ITEM_SELECTED_P)))
6707 PROP (TOOL_BAR_ITEM_SELECTED_P)
6708 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_SELECTED_P));
6710 return 1;
6712 #undef PROP
6716 /* Initialize tool_bar_items_vector. REUSE, if non-nil, is a vector
6717 that can be reused. */
6719 static void
6720 init_tool_bar_items (reuse)
6721 Lisp_Object reuse;
6723 if (VECTORP (reuse))
6724 tool_bar_items_vector = reuse;
6725 else
6726 tool_bar_items_vector = Fmake_vector (make_number (64), Qnil);
6727 ntool_bar_items = 0;
6731 /* Append parsed tool bar item properties from
6732 tool_bar_item_properties */
6734 static void
6735 append_tool_bar_item ()
6737 Lisp_Object *to, *from;
6739 /* Enlarge tool_bar_items_vector if necessary. */
6740 if (ntool_bar_items + TOOL_BAR_ITEM_NSLOTS
6741 >= XVECTOR (tool_bar_items_vector)->size)
6743 Lisp_Object new_vector;
6744 int old_size = XVECTOR (tool_bar_items_vector)->size;
6746 new_vector = Fmake_vector (make_number (2 * old_size), Qnil);
6747 bcopy (XVECTOR (tool_bar_items_vector)->contents,
6748 XVECTOR (new_vector)->contents,
6749 old_size * sizeof (Lisp_Object));
6750 tool_bar_items_vector = new_vector;
6753 /* Append entries from tool_bar_item_properties to the end of
6754 tool_bar_items_vector. */
6755 to = XVECTOR (tool_bar_items_vector)->contents + ntool_bar_items;
6756 from = XVECTOR (tool_bar_item_properties)->contents;
6757 bcopy (from, to, TOOL_BAR_ITEM_NSLOTS * sizeof *to);
6758 ntool_bar_items += TOOL_BAR_ITEM_NSLOTS;
6765 /* Read a character using menus based on maps in the array MAPS.
6766 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
6767 Return t if we displayed a menu but the user rejected it.
6769 PREV_EVENT is the previous input event, or nil if we are reading
6770 the first event of a key sequence.
6772 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
6773 if we used a mouse menu to read the input, or zero otherwise. If
6774 USED_MOUSE_MENU is null, we don't dereference it.
6776 The prompting is done based on the prompt-string of the map
6777 and the strings associated with various map elements.
6779 This can be done with X menus or with menus put in the minibuf.
6780 These are done in different ways, depending on how the input will be read.
6781 Menus using X are done after auto-saving in read-char, getting the input
6782 event from Fx_popup_menu; menus using the minibuf use read_char recursively
6783 and do auto-saving in the inner call of read_char. */
6785 static Lisp_Object
6786 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
6787 int nmaps;
6788 Lisp_Object *maps;
6789 Lisp_Object prev_event;
6790 int *used_mouse_menu;
6792 int mapno;
6793 register Lisp_Object name;
6795 if (used_mouse_menu)
6796 *used_mouse_menu = 0;
6798 /* Use local over global Menu maps */
6800 if (! menu_prompting)
6801 return Qnil;
6803 /* Optionally disregard all but the global map. */
6804 if (inhibit_local_menu_bar_menus)
6806 maps += (nmaps - 1);
6807 nmaps = 1;
6810 /* Get the menu name from the first map that has one (a prompt string). */
6811 for (mapno = 0; mapno < nmaps; mapno++)
6813 name = map_prompt (maps[mapno]);
6814 if (!NILP (name))
6815 break;
6818 /* If we don't have any menus, just read a character normally. */
6819 if (mapno >= nmaps)
6820 return Qnil;
6822 #ifdef HAVE_MENUS
6823 /* If we got to this point via a mouse click,
6824 use a real menu for mouse selection. */
6825 if (EVENT_HAS_PARAMETERS (prev_event)
6826 && !EQ (XCAR (prev_event), Qmenu_bar)
6827 && !EQ (XCAR (prev_event), Qtool_bar))
6829 /* Display the menu and get the selection. */
6830 Lisp_Object *realmaps
6831 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
6832 Lisp_Object value;
6833 int nmaps1 = 0;
6835 /* Use the maps that are not nil. */
6836 for (mapno = 0; mapno < nmaps; mapno++)
6837 if (!NILP (maps[mapno]))
6838 realmaps[nmaps1++] = maps[mapno];
6840 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
6841 if (CONSP (value))
6843 Lisp_Object tem;
6845 record_menu_key (XCAR (value));
6847 /* If we got multiple events, unread all but
6848 the first.
6849 There is no way to prevent those unread events
6850 from showing up later in last_nonmenu_event.
6851 So turn symbol and integer events into lists,
6852 to indicate that they came from a mouse menu,
6853 so that when present in last_nonmenu_event
6854 they won't confuse things. */
6855 for (tem = XCDR (value); !NILP (tem);
6856 tem = XCDR (tem))
6858 record_menu_key (XCAR (tem));
6859 if (SYMBOLP (XCAR (tem))
6860 || INTEGERP (XCAR (tem)))
6861 XCAR (tem)
6862 = Fcons (XCAR (tem), Qnil);
6865 /* If we got more than one event, put all but the first
6866 onto this list to be read later.
6867 Return just the first event now. */
6868 Vunread_command_events
6869 = nconc2 (XCDR (value), Vunread_command_events);
6870 value = XCAR (value);
6872 else if (NILP (value))
6873 value = Qt;
6874 if (used_mouse_menu)
6875 *used_mouse_menu = 1;
6876 return value;
6878 #endif /* HAVE_MENUS */
6879 return Qnil ;
6882 /* Buffer in use so far for the minibuf prompts for menu keymaps.
6883 We make this bigger when necessary, and never free it. */
6884 static char *read_char_minibuf_menu_text;
6885 /* Size of that buffer. */
6886 static int read_char_minibuf_menu_width;
6888 static Lisp_Object
6889 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
6890 int commandflag ;
6891 int nmaps;
6892 Lisp_Object *maps;
6894 int mapno;
6895 register Lisp_Object name;
6896 int nlength;
6897 int width = FRAME_WIDTH (SELECTED_FRAME ()) - 4;
6898 int idx = -1;
6899 int nobindings = 1;
6900 Lisp_Object rest, vector;
6901 char *menu;
6903 if (! menu_prompting)
6904 return Qnil;
6906 /* Make sure we have a big enough buffer for the menu text. */
6907 if (read_char_minibuf_menu_text == 0)
6909 read_char_minibuf_menu_width = width + 4;
6910 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
6912 else if (width + 4 > read_char_minibuf_menu_width)
6914 read_char_minibuf_menu_width = width + 4;
6915 read_char_minibuf_menu_text
6916 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
6918 menu = read_char_minibuf_menu_text;
6920 /* Get the menu name from the first map that has one (a prompt string). */
6921 for (mapno = 0; mapno < nmaps; mapno++)
6923 name = map_prompt (maps[mapno]);
6924 if (!NILP (name))
6925 break;
6928 /* If we don't have any menus, just read a character normally. */
6929 if (mapno >= nmaps)
6930 return Qnil;
6932 /* Prompt string always starts with map's prompt, and a space. */
6933 strcpy (menu, XSTRING (name)->data);
6934 nlength = STRING_BYTES (XSTRING (name));
6935 menu[nlength++] = ':';
6936 menu[nlength++] = ' ';
6937 menu[nlength] = 0;
6939 /* Start prompting at start of first map. */
6940 mapno = 0;
6941 rest = maps[mapno];
6943 /* Present the documented bindings, a line at a time. */
6944 while (1)
6946 int notfirst = 0;
6947 int i = nlength;
6948 Lisp_Object obj;
6949 int ch;
6950 Lisp_Object orig_defn_macro;
6952 /* Loop over elements of map. */
6953 while (i < width)
6955 Lisp_Object elt;
6957 /* If reached end of map, start at beginning of next map. */
6958 if (NILP (rest))
6960 mapno++;
6961 /* At end of last map, wrap around to first map if just starting,
6962 or end this line if already have something on it. */
6963 if (mapno == nmaps)
6965 mapno = 0;
6966 if (notfirst || nobindings) break;
6968 rest = maps[mapno];
6971 /* Look at the next element of the map. */
6972 if (idx >= 0)
6973 elt = XVECTOR (vector)->contents[idx];
6974 else
6975 elt = Fcar_safe (rest);
6977 if (idx < 0 && VECTORP (elt))
6979 /* If we found a dense table in the keymap,
6980 advanced past it, but start scanning its contents. */
6981 rest = Fcdr_safe (rest);
6982 vector = elt;
6983 idx = 0;
6985 else
6987 /* An ordinary element. */
6988 Lisp_Object event, tem;
6990 if (idx < 0)
6992 event = Fcar_safe (elt); /* alist */
6993 elt = Fcdr_safe (elt);
6995 else
6997 XSETINT (event, idx); /* vector */
7000 /* Ignore the element if it has no prompt string. */
7001 if (INTEGERP (event) && parse_menu_item (elt, 0, -1))
7003 /* 1 if the char to type matches the string. */
7004 int char_matches;
7005 Lisp_Object upcased_event, downcased_event;
7006 Lisp_Object desc;
7007 Lisp_Object s
7008 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
7010 upcased_event = Fupcase (event);
7011 downcased_event = Fdowncase (event);
7012 char_matches = (XINT (upcased_event) == XSTRING (s)->data[0]
7013 || XINT (downcased_event) == XSTRING (s)->data[0]);
7014 if (! char_matches)
7015 desc = Fsingle_key_description (event);
7018 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
7019 if (!NILP (tem))
7020 /* Insert equivalent keybinding. */
7021 s = concat2 (s, tem);
7024 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
7025 if (EQ (tem, QCradio) || EQ (tem, QCtoggle))
7027 /* Insert button prefix. */
7028 Lisp_Object selected
7029 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
7030 if (EQ (tem, QCradio))
7031 tem = build_string (NILP (selected) ? "(*) " : "( ) ");
7032 else
7033 tem = build_string (NILP (selected) ? "[X] " : "[ ] ");
7034 s = concat2 (tem, s);
7038 /* If we have room for the prompt string, add it to this line.
7039 If this is the first on the line, always add it. */
7040 if ((XSTRING (s)->size + i + 2
7041 + (char_matches ? 0 : XSTRING (desc)->size + 3))
7042 < width
7043 || !notfirst)
7045 int thiswidth;
7047 /* Punctuate between strings. */
7048 if (notfirst)
7050 strcpy (menu + i, ", ");
7051 i += 2;
7053 notfirst = 1;
7054 nobindings = 0 ;
7056 /* If the char to type doesn't match the string's
7057 first char, explicitly show what char to type. */
7058 if (! char_matches)
7060 /* Add as much of string as fits. */
7061 thiswidth = XSTRING (desc)->size;
7062 if (thiswidth + i > width)
7063 thiswidth = width - i;
7064 bcopy (XSTRING (desc)->data, menu + i, thiswidth);
7065 i += thiswidth;
7066 strcpy (menu + i, " = ");
7067 i += 3;
7070 /* Add as much of string as fits. */
7071 thiswidth = XSTRING (s)->size;
7072 if (thiswidth + i > width)
7073 thiswidth = width - i;
7074 bcopy (XSTRING (s)->data, menu + i, thiswidth);
7075 i += thiswidth;
7076 menu[i] = 0;
7078 else
7080 /* If this element does not fit, end the line now,
7081 and save the element for the next line. */
7082 strcpy (menu + i, "...");
7083 break;
7087 /* Move past this element. */
7088 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
7089 /* Handle reaching end of dense table. */
7090 idx = -1;
7091 if (idx >= 0)
7092 idx++;
7093 else
7094 rest = Fcdr_safe (rest);
7098 /* Prompt with that and read response. */
7099 message2_nolog (menu, strlen (menu),
7100 ! NILP (current_buffer->enable_multibyte_characters));
7102 /* Make believe its not a keyboard macro in case the help char
7103 is pressed. Help characters are not recorded because menu prompting
7104 is not used on replay.
7106 orig_defn_macro = current_kboard->defining_kbd_macro;
7107 current_kboard->defining_kbd_macro = Qnil;
7109 obj = read_char (commandflag, 0, 0, Qnil, 0);
7110 while (BUFFERP (obj));
7111 current_kboard->defining_kbd_macro = orig_defn_macro;
7113 if (!INTEGERP (obj))
7114 return obj;
7115 else
7116 ch = XINT (obj);
7118 if (! EQ (obj, menu_prompt_more_char)
7119 && (!INTEGERP (menu_prompt_more_char)
7120 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
7122 if (!NILP (current_kboard->defining_kbd_macro))
7123 store_kbd_macro_char (obj);
7124 return obj;
7126 /* Help char - go round again */
7130 /* Reading key sequences. */
7132 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
7133 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
7134 keymap, or nil otherwise. Return the index of the first keymap in
7135 which KEY has any binding, or NMAPS if no map has a binding.
7137 If KEY is a meta ASCII character, treat it like meta-prefix-char
7138 followed by the corresponding non-meta character. Keymaps in
7139 CURRENT with non-prefix bindings for meta-prefix-char become nil in
7140 NEXT.
7142 If KEY has no bindings in any of the CURRENT maps, NEXT is left
7143 unmodified.
7145 NEXT may be the same array as CURRENT. */
7147 static int
7148 follow_key (key, nmaps, current, defs, next)
7149 Lisp_Object key;
7150 Lisp_Object *current, *defs, *next;
7151 int nmaps;
7153 int i, first_binding;
7154 int did_meta = 0;
7156 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
7157 followed by the corresponding non-meta character.
7158 Put the results into DEFS, since we are going to alter that anyway.
7159 Do not alter CURRENT or NEXT. */
7160 if (INTEGERP (key) && (XINT (key) & CHAR_META))
7162 for (i = 0; i < nmaps; i++)
7163 if (! NILP (current[i]))
7165 Lisp_Object def;
7166 def = get_keyelt (access_keymap (current[i],
7167 meta_prefix_char, 1, 0), 0);
7169 /* Note that since we pass the resulting bindings through
7170 get_keymap_1, non-prefix bindings for meta-prefix-char
7171 disappear. */
7172 defs[i] = get_keymap_1 (def, 0, 1);
7174 else
7175 defs[i] = Qnil;
7177 did_meta = 1;
7178 XSETINT (key, XFASTINT (key) & ~CHAR_META);
7181 first_binding = nmaps;
7182 for (i = nmaps - 1; i >= 0; i--)
7184 if (! NILP (current[i]))
7186 Lisp_Object map;
7187 if (did_meta)
7188 map = defs[i];
7189 else
7190 map = current[i];
7192 defs[i] = get_keyelt (access_keymap (map, key, 1, 0), 0);
7193 if (! NILP (defs[i]))
7194 first_binding = i;
7196 else
7197 defs[i] = Qnil;
7200 /* Given the set of bindings we've found, produce the next set of maps. */
7201 if (first_binding < nmaps)
7202 for (i = 0; i < nmaps; i++)
7203 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
7205 return first_binding;
7208 /* Read a sequence of keys that ends with a non prefix character,
7209 storing it in KEYBUF, a buffer of size BUFSIZE.
7210 Prompt with PROMPT.
7211 Return the length of the key sequence stored.
7212 Return -1 if the user rejected a command menu.
7214 Echo starting immediately unless `prompt' is 0.
7216 Where a key sequence ends depends on the currently active keymaps.
7217 These include any minor mode keymaps active in the current buffer,
7218 the current buffer's local map, and the global map.
7220 If a key sequence has no other bindings, we check Vfunction_key_map
7221 to see if some trailing subsequence might be the beginning of a
7222 function key's sequence. If so, we try to read the whole function
7223 key, and substitute its symbolic name into the key sequence.
7225 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
7226 `double-' events into similar click events, if that would make them
7227 bound. We try to turn `triple-' events first into `double-' events,
7228 then into clicks.
7230 If we get a mouse click in a mode line, vertical divider, or other
7231 non-text area, we treat the click as if it were prefixed by the
7232 symbol denoting that area - `mode-line', `vertical-line', or
7233 whatever.
7235 If the sequence starts with a mouse click, we read the key sequence
7236 with respect to the buffer clicked on, not the current buffer.
7238 If the user switches frames in the midst of a key sequence, we put
7239 off the switch-frame event until later; the next call to
7240 read_char will return it.
7242 If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
7243 from the selected window's buffer. */
7245 static int
7246 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
7247 can_return_switch_frame, fix_current_buffer)
7248 Lisp_Object *keybuf;
7249 int bufsize;
7250 Lisp_Object prompt;
7251 int dont_downcase_last;
7252 int can_return_switch_frame;
7253 int fix_current_buffer;
7255 int count = specpdl_ptr - specpdl;
7257 /* How many keys there are in the current key sequence. */
7258 int t;
7260 /* The length of the echo buffer when we started reading, and
7261 the length of this_command_keys when we started reading. */
7262 int echo_start;
7263 int keys_start;
7265 /* The number of keymaps we're scanning right now, and the number of
7266 keymaps we have allocated space for. */
7267 int nmaps;
7268 int nmaps_allocated = 0;
7270 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
7271 the current keymaps. */
7272 Lisp_Object *defs;
7274 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
7275 in the current keymaps, or nil where it is not a prefix. */
7276 Lisp_Object *submaps;
7278 /* The local map to start out with at start of key sequence. */
7279 Lisp_Object orig_local_map;
7281 /* 1 if we have already considered switching to the local-map property
7282 of the place where a mouse click occurred. */
7283 int localized_local_map = 0;
7285 /* The index in defs[] of the first keymap that has a binding for
7286 this key sequence. In other words, the lowest i such that
7287 defs[i] is non-nil. */
7288 int first_binding;
7290 /* If t < mock_input, then KEYBUF[t] should be read as the next
7291 input key.
7293 We use this to recover after recognizing a function key. Once we
7294 realize that a suffix of the current key sequence is actually a
7295 function key's escape sequence, we replace the suffix with the
7296 function key's binding from Vfunction_key_map. Now keybuf
7297 contains a new and different key sequence, so the echo area,
7298 this_command_keys, and the submaps and defs arrays are wrong. In
7299 this situation, we set mock_input to t, set t to 0, and jump to
7300 restart_sequence; the loop will read keys from keybuf up until
7301 mock_input, thus rebuilding the state; and then it will resume
7302 reading characters from the keyboard. */
7303 int mock_input = 0;
7305 /* If the sequence is unbound in submaps[], then
7306 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
7307 and fkey_map is its binding.
7309 These might be > t, indicating that all function key scanning
7310 should hold off until t reaches them. We do this when we've just
7311 recognized a function key, to avoid searching for the function
7312 key's again in Vfunction_key_map. */
7313 int fkey_start = 0, fkey_end = 0;
7314 Lisp_Object fkey_map;
7316 /* Likewise, for key_translation_map. */
7317 int keytran_start = 0, keytran_end = 0;
7318 Lisp_Object keytran_map;
7320 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
7321 we put it off for later. While we're reading, we keep the event here. */
7322 Lisp_Object delayed_switch_frame;
7324 /* See the comment below... */
7325 #if defined (GOBBLE_FIRST_EVENT)
7326 Lisp_Object first_event;
7327 #endif
7329 Lisp_Object original_uppercase;
7330 int original_uppercase_position = -1;
7332 /* Gets around Microsoft compiler limitations. */
7333 int dummyflag = 0;
7335 struct buffer *starting_buffer;
7337 /* Nonzero if we seem to have got the beginning of a binding
7338 in function_key_map. */
7339 int function_key_possible = 0;
7340 int key_translation_possible = 0;
7342 /* Save the status of key translation before each step,
7343 so that we can restore this after downcasing. */
7344 Lisp_Object prev_fkey_map;
7345 int prev_fkey_start;
7346 int prev_fkey_end;
7348 Lisp_Object prev_keytran_map;
7349 int prev_keytran_start;
7350 int prev_keytran_end;
7352 #if defined (GOBBLE_FIRST_EVENT)
7353 int junk;
7354 #endif
7356 raw_keybuf_count = 0;
7358 last_nonmenu_event = Qnil;
7360 delayed_switch_frame = Qnil;
7361 fkey_map = Vfunction_key_map;
7362 keytran_map = Vkey_translation_map;
7364 /* If there is no function-key-map, turn off function key scanning. */
7365 if (NILP (Fkeymapp (Vfunction_key_map)))
7366 fkey_start = fkey_end = bufsize + 1;
7368 /* If there is no key-translation-map, turn off scanning. */
7369 if (NILP (Fkeymapp (Vkey_translation_map)))
7370 keytran_start = keytran_end = bufsize + 1;
7372 if (INTERACTIVE)
7374 if (!NILP (prompt))
7375 echo_prompt (XSTRING (prompt)->data);
7376 else if (cursor_in_echo_area && echo_keystrokes)
7377 /* This doesn't put in a dash if the echo buffer is empty, so
7378 you don't always see a dash hanging out in the minibuffer. */
7379 echo_dash ();
7382 /* Record the initial state of the echo area and this_command_keys;
7383 we will need to restore them if we replay a key sequence. */
7384 if (INTERACTIVE)
7385 echo_start = echo_length ();
7386 keys_start = this_command_key_count;
7387 this_single_command_key_start = keys_start;
7389 #if defined (GOBBLE_FIRST_EVENT)
7390 /* This doesn't quite work, because some of the things that read_char
7391 does cannot safely be bypassed. It seems too risky to try to make
7392 this work right. */
7394 /* Read the first char of the sequence specially, before setting
7395 up any keymaps, in case a filter runs and switches buffers on us. */
7396 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
7397 &junk);
7398 #endif /* GOBBLE_FIRST_EVENT */
7400 orig_local_map = get_local_map (PT, current_buffer);
7402 /* We jump here when the key sequence has been thoroughly changed, and
7403 we need to rescan it starting from the beginning. When we jump here,
7404 keybuf[0..mock_input] holds the sequence we should reread. */
7405 replay_sequence:
7407 starting_buffer = current_buffer;
7408 function_key_possible = 0;
7409 key_translation_possible = 0;
7411 /* Build our list of keymaps.
7412 If we recognize a function key and replace its escape sequence in
7413 keybuf with its symbol, or if the sequence starts with a mouse
7414 click and we need to switch buffers, we jump back here to rebuild
7415 the initial keymaps from the current buffer. */
7417 Lisp_Object *maps;
7419 if (!NILP (current_kboard->Voverriding_terminal_local_map)
7420 || !NILP (Voverriding_local_map))
7422 if (3 > nmaps_allocated)
7424 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
7425 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
7426 nmaps_allocated = 3;
7428 nmaps = 0;
7429 if (!NILP (current_kboard->Voverriding_terminal_local_map))
7430 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
7431 if (!NILP (Voverriding_local_map))
7432 submaps[nmaps++] = Voverriding_local_map;
7434 else
7436 nmaps = current_minor_maps (0, &maps);
7437 if (nmaps + 2 > nmaps_allocated)
7439 submaps = (Lisp_Object *) alloca ((nmaps+2) * sizeof (submaps[0]));
7440 defs = (Lisp_Object *) alloca ((nmaps+2) * sizeof (defs[0]));
7441 nmaps_allocated = nmaps + 2;
7443 bcopy (maps, submaps, nmaps * sizeof (submaps[0]));
7444 #ifdef USE_TEXT_PROPERTIES
7445 submaps[nmaps++] = orig_local_map;
7446 #else
7447 submaps[nmaps++] = current_buffer->keymap;
7448 #endif
7450 submaps[nmaps++] = current_global_map;
7453 /* Find an accurate initial value for first_binding. */
7454 for (first_binding = 0; first_binding < nmaps; first_binding++)
7455 if (! NILP (submaps[first_binding]))
7456 break;
7458 /* Start from the beginning in keybuf. */
7459 t = 0;
7461 /* These are no-ops the first time through, but if we restart, they
7462 revert the echo area and this_command_keys to their original state. */
7463 this_command_key_count = keys_start;
7464 if (INTERACTIVE && t < mock_input)
7465 echo_truncate (echo_start);
7467 /* If the best binding for the current key sequence is a keymap, or
7468 we may be looking at a function key's escape sequence, keep on
7469 reading. */
7470 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
7471 || (first_binding >= nmaps
7472 && fkey_start < t
7473 /* mock input is never part of a function key's sequence. */
7474 && mock_input <= fkey_start)
7475 || (first_binding >= nmaps
7476 && keytran_start < t && key_translation_possible)
7477 /* Don't return in the middle of a possible function key sequence,
7478 if the only bindings we found were via case conversion.
7479 Thus, if ESC O a has a function-key-map translation
7480 and ESC o has a binding, don't return after ESC O,
7481 so that we can translate ESC O plus the next character. */
7484 Lisp_Object key;
7485 int used_mouse_menu = 0;
7487 /* Where the last real key started. If we need to throw away a
7488 key that has expanded into more than one element of keybuf
7489 (say, a mouse click on the mode line which is being treated
7490 as [mode-line (mouse-...)], then we backtrack to this point
7491 of keybuf. */
7492 int last_real_key_start;
7494 /* These variables are analogous to echo_start and keys_start;
7495 while those allow us to restart the entire key sequence,
7496 echo_local_start and keys_local_start allow us to throw away
7497 just one key. */
7498 int echo_local_start, keys_local_start, local_first_binding;
7500 if (t >= bufsize)
7501 error ("Key sequence too long");
7503 if (INTERACTIVE)
7504 echo_local_start = echo_length ();
7505 keys_local_start = this_command_key_count;
7506 local_first_binding = first_binding;
7508 replay_key:
7509 /* These are no-ops, unless we throw away a keystroke below and
7510 jumped back up to replay_key; in that case, these restore the
7511 variables to their original state, allowing us to replay the
7512 loop. */
7513 if (INTERACTIVE && t < mock_input)
7514 echo_truncate (echo_local_start);
7515 this_command_key_count = keys_local_start;
7516 first_binding = local_first_binding;
7518 /* By default, assume each event is "real". */
7519 last_real_key_start = t;
7521 /* Does mock_input indicate that we are re-reading a key sequence? */
7522 if (t < mock_input)
7524 key = keybuf[t];
7525 add_command_key (key);
7526 if (echo_keystrokes)
7527 echo_char (key);
7530 /* If not, we should actually read a character. */
7531 else
7534 #ifdef MULTI_KBOARD
7535 KBOARD *interrupted_kboard = current_kboard;
7536 struct frame *interrupted_frame = SELECTED_FRAME ();
7537 if (setjmp (wrong_kboard_jmpbuf))
7539 if (!NILP (delayed_switch_frame))
7541 interrupted_kboard->kbd_queue
7542 = Fcons (delayed_switch_frame,
7543 interrupted_kboard->kbd_queue);
7544 delayed_switch_frame = Qnil;
7546 while (t > 0)
7547 interrupted_kboard->kbd_queue
7548 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
7550 /* If the side queue is non-empty, ensure it begins with a
7551 switch-frame, so we'll replay it in the right context. */
7552 if (CONSP (interrupted_kboard->kbd_queue)
7553 && (key = XCAR (interrupted_kboard->kbd_queue),
7554 !(EVENT_HAS_PARAMETERS (key)
7555 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
7556 Qswitch_frame))))
7558 Lisp_Object frame;
7559 XSETFRAME (frame, interrupted_frame);
7560 interrupted_kboard->kbd_queue
7561 = Fcons (make_lispy_switch_frame (frame),
7562 interrupted_kboard->kbd_queue);
7564 mock_input = 0;
7565 orig_local_map = get_local_map (PT, current_buffer);
7566 goto replay_sequence;
7568 #endif
7569 key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
7570 &used_mouse_menu);
7573 /* read_char returns t when it shows a menu and the user rejects it.
7574 Just return -1. */
7575 if (EQ (key, Qt))
7577 unbind_to (count, Qnil);
7578 return -1;
7581 /* read_char returns -1 at the end of a macro.
7582 Emacs 18 handles this by returning immediately with a
7583 zero, so that's what we'll do. */
7584 if (INTEGERP (key) && XINT (key) == -1)
7586 t = 0;
7587 /* The Microsoft C compiler can't handle the goto that
7588 would go here. */
7589 dummyflag = 1;
7590 break;
7593 /* If the current buffer has been changed from under us, the
7594 keymap may have changed, so replay the sequence. */
7595 if (BUFFERP (key))
7597 mock_input = t;
7598 /* Reset the current buffer from the selected window
7599 in case something changed the former and not the latter.
7600 This is to be more consistent with the behavior
7601 of the command_loop_1. */
7602 if (fix_current_buffer)
7604 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7605 Fkill_emacs (Qnil);
7606 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
7607 Fset_buffer (XWINDOW (selected_window)->buffer);
7610 orig_local_map = get_local_map (PT, current_buffer);
7611 goto replay_sequence;
7614 /* If we have a quit that was typed in another frame, and
7615 quit_throw_to_read_char switched buffers,
7616 replay to get the right keymap. */
7617 if (XINT (key) == quit_char && current_buffer != starting_buffer)
7619 GROW_RAW_KEYBUF;
7620 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
7621 keybuf[t++] = key;
7622 mock_input = t;
7623 Vquit_flag = Qnil;
7624 orig_local_map = get_local_map (PT, current_buffer);
7625 goto replay_sequence;
7628 Vquit_flag = Qnil;
7630 if (EVENT_HAS_PARAMETERS (key)
7631 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), Qswitch_frame))
7633 /* If we're at the beginning of a key sequence, and the caller
7634 says it's okay, go ahead and return this event. If we're
7635 in the midst of a key sequence, delay it until the end. */
7636 if (t > 0 || !can_return_switch_frame)
7638 delayed_switch_frame = key;
7639 goto replay_key;
7643 GROW_RAW_KEYBUF;
7644 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
7647 /* Clicks in non-text areas get prefixed by the symbol
7648 in their CHAR-ADDRESS field. For example, a click on
7649 the mode line is prefixed by the symbol `mode-line'.
7651 Furthermore, key sequences beginning with mouse clicks
7652 are read using the keymaps of the buffer clicked on, not
7653 the current buffer. So we may have to switch the buffer
7654 here.
7656 When we turn one event into two events, we must make sure
7657 that neither of the two looks like the original--so that,
7658 if we replay the events, they won't be expanded again.
7659 If not for this, such reexpansion could happen either here
7660 or when user programs play with this-command-keys. */
7661 if (EVENT_HAS_PARAMETERS (key))
7663 Lisp_Object kind;
7665 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
7666 if (EQ (kind, Qmouse_click))
7668 Lisp_Object window, posn;
7670 window = POSN_WINDOW (EVENT_START (key));
7671 posn = POSN_BUFFER_POSN (EVENT_START (key));
7673 if (CONSP (posn))
7675 /* We're looking at the second event of a
7676 sequence which we expanded before. Set
7677 last_real_key_start appropriately. */
7678 if (t > 0)
7679 last_real_key_start = t - 1;
7682 /* Key sequences beginning with mouse clicks are
7683 read using the keymaps in the buffer clicked on,
7684 not the current buffer. If we're at the
7685 beginning of a key sequence, switch buffers. */
7686 if (last_real_key_start == 0
7687 && WINDOWP (window)
7688 && BUFFERP (XWINDOW (window)->buffer)
7689 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
7691 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
7692 keybuf[t] = key;
7693 mock_input = t + 1;
7695 /* Arrange to go back to the original buffer once we're
7696 done reading the key sequence. Note that we can't
7697 use save_excursion_{save,restore} here, because they
7698 save point as well as the current buffer; we don't
7699 want to save point, because redisplay may change it,
7700 to accommodate a Fset_window_start or something. We
7701 don't want to do this at the top of the function,
7702 because we may get input from a subprocess which
7703 wants to change the selected window and stuff (say,
7704 emacsclient). */
7705 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
7707 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7708 Fkill_emacs (Qnil);
7709 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
7710 orig_local_map = get_local_map (PT, current_buffer);
7711 goto replay_sequence;
7714 /* For a mouse click, get the local text-property keymap
7715 of the place clicked on, rather than point. */
7716 if (last_real_key_start == 0
7717 && CONSP (XCDR (key))
7718 && ! localized_local_map)
7720 Lisp_Object map_here, start, pos;
7722 localized_local_map = 1;
7723 start = EVENT_START (key);
7725 if (CONSP (start) && CONSP (XCDR (start)))
7727 pos = POSN_BUFFER_POSN (start);
7728 if (INTEGERP (pos)
7729 && XINT (pos) >= BEG && XINT (pos) <= Z)
7731 map_here = get_local_map (XINT (pos), current_buffer);
7732 if (!EQ (map_here, orig_local_map))
7734 orig_local_map = map_here;
7735 keybuf[t] = key;
7736 mock_input = t + 1;
7738 goto replay_sequence;
7744 /* Expand mode-line and scroll-bar events into two events:
7745 use posn as a fake prefix key. */
7746 if (SYMBOLP (posn))
7748 if (t + 1 >= bufsize)
7749 error ("Key sequence too long");
7750 keybuf[t] = posn;
7751 keybuf[t+1] = key;
7752 mock_input = t + 2;
7754 /* Zap the position in key, so we know that we've
7755 expanded it, and don't try to do so again. */
7756 POSN_BUFFER_POSN (EVENT_START (key))
7757 = Fcons (posn, Qnil);
7759 /* If on a mode line string with a local keymap,
7760 reconsider the key sequence with that keymap. */
7761 if (CONSP (POSN_STRING (EVENT_START (key))))
7763 Lisp_Object string, pos, map;
7765 string = POSN_STRING (EVENT_START (key));
7766 pos = XCDR (string);
7767 string = XCAR (string);
7769 if (pos >= 0
7770 && pos < XSTRING (string)->size
7771 && (map = Fget_text_property (pos, Qlocal_map,
7772 string),
7773 !NILP (map)))
7775 orig_local_map = map;
7776 goto replay_sequence;
7780 goto replay_key;
7783 else if (CONSP (XCDR (key))
7784 && CONSP (EVENT_START (key))
7785 && CONSP (XCDR (EVENT_START (key))))
7787 Lisp_Object posn;
7789 posn = POSN_BUFFER_POSN (EVENT_START (key));
7790 /* Handle menu-bar events:
7791 insert the dummy prefix event `menu-bar'. */
7792 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
7794 if (t + 1 >= bufsize)
7795 error ("Key sequence too long");
7796 keybuf[t] = posn;
7797 keybuf[t+1] = key;
7799 /* Zap the position in key, so we know that we've
7800 expanded it, and don't try to do so again. */
7801 POSN_BUFFER_POSN (EVENT_START (key))
7802 = Fcons (posn, Qnil);
7804 mock_input = t + 2;
7805 goto replay_sequence;
7807 else if (CONSP (posn))
7809 /* We're looking at the second event of a
7810 sequence which we expanded before. Set
7811 last_real_key_start appropriately. */
7812 if (last_real_key_start == t && t > 0)
7813 last_real_key_start = t - 1;
7818 /* We have finally decided that KEY is something we might want
7819 to look up. */
7820 first_binding = (follow_key (key,
7821 nmaps - first_binding,
7822 submaps + first_binding,
7823 defs + first_binding,
7824 submaps + first_binding)
7825 + first_binding);
7827 /* If KEY wasn't bound, we'll try some fallbacks. */
7828 if (first_binding >= nmaps)
7830 Lisp_Object head;
7832 head = EVENT_HEAD (key);
7833 if (help_char_p (head) && t > 0)
7835 read_key_sequence_cmd = Vprefix_help_command;
7836 keybuf[t++] = key;
7837 last_nonmenu_event = key;
7838 /* The Microsoft C compiler can't handle the goto that
7839 would go here. */
7840 dummyflag = 1;
7841 break;
7844 if (SYMBOLP (head))
7846 Lisp_Object breakdown;
7847 int modifiers;
7849 breakdown = parse_modifiers (head);
7850 modifiers = XINT (XCAR (XCDR (breakdown)));
7851 /* Attempt to reduce an unbound mouse event to a simpler
7852 event that is bound:
7853 Drags reduce to clicks.
7854 Double-clicks reduce to clicks.
7855 Triple-clicks reduce to double-clicks, then to clicks.
7856 Down-clicks are eliminated.
7857 Double-downs reduce to downs, then are eliminated.
7858 Triple-downs reduce to double-downs, then to downs,
7859 then are eliminated. */
7860 if (modifiers & (down_modifier | drag_modifier
7861 | double_modifier | triple_modifier))
7863 while (modifiers & (down_modifier | drag_modifier
7864 | double_modifier | triple_modifier))
7866 Lisp_Object new_head, new_click;
7867 if (modifiers & triple_modifier)
7868 modifiers ^= (double_modifier | triple_modifier);
7869 else if (modifiers & double_modifier)
7870 modifiers &= ~double_modifier;
7871 else if (modifiers & drag_modifier)
7872 modifiers &= ~drag_modifier;
7873 else
7875 /* Dispose of this `down' event by simply jumping
7876 back to replay_key, to get another event.
7878 Note that if this event came from mock input,
7879 then just jumping back to replay_key will just
7880 hand it to us again. So we have to wipe out any
7881 mock input.
7883 We could delete keybuf[t] and shift everything
7884 after that to the left by one spot, but we'd also
7885 have to fix up any variable that points into
7886 keybuf, and shifting isn't really necessary
7887 anyway.
7889 Adding prefixes for non-textual mouse clicks
7890 creates two characters of mock input, and both
7891 must be thrown away. If we're only looking at
7892 the prefix now, we can just jump back to
7893 replay_key. On the other hand, if we've already
7894 processed the prefix, and now the actual click
7895 itself is giving us trouble, then we've lost the
7896 state of the keymaps we want to backtrack to, and
7897 we need to replay the whole sequence to rebuild
7900 Beyond that, only function key expansion could
7901 create more than two keys, but that should never
7902 generate mouse events, so it's okay to zero
7903 mock_input in that case too.
7905 Isn't this just the most wonderful code ever? */
7906 if (t == last_real_key_start)
7908 mock_input = 0;
7909 goto replay_key;
7911 else
7913 mock_input = last_real_key_start;
7914 goto replay_sequence;
7918 new_head
7919 = apply_modifiers (modifiers, XCAR (breakdown));
7920 new_click
7921 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
7923 /* Look for a binding for this new key. follow_key
7924 promises that it didn't munge submaps the
7925 last time we called it, since key was unbound. */
7926 first_binding
7927 = (follow_key (new_click,
7928 nmaps - local_first_binding,
7929 submaps + local_first_binding,
7930 defs + local_first_binding,
7931 submaps + local_first_binding)
7932 + local_first_binding);
7934 /* If that click is bound, go for it. */
7935 if (first_binding < nmaps)
7937 key = new_click;
7938 break;
7940 /* Otherwise, we'll leave key set to the drag event. */
7946 keybuf[t++] = key;
7947 /* Normally, last_nonmenu_event gets the previous key we read.
7948 But when a mouse popup menu is being used,
7949 we don't update last_nonmenu_event; it continues to hold the mouse
7950 event that preceded the first level of menu. */
7951 if (!used_mouse_menu)
7952 last_nonmenu_event = key;
7954 /* Record what part of this_command_keys is the current key sequence. */
7955 this_single_command_key_start = this_command_key_count - t;
7957 prev_fkey_map = fkey_map;
7958 prev_fkey_start = fkey_start;
7959 prev_fkey_end = fkey_end;
7961 prev_keytran_map = keytran_map;
7962 prev_keytran_start = keytran_start;
7963 prev_keytran_end = keytran_end;
7965 /* If the sequence is unbound, see if we can hang a function key
7966 off the end of it. We only want to scan real keyboard input
7967 for function key sequences, so if mock_input says that we're
7968 re-reading old events, don't examine it. */
7969 if (first_binding >= nmaps
7970 && t >= mock_input)
7972 Lisp_Object fkey_next;
7974 /* Continue scan from fkey_end until we find a bound suffix.
7975 If we fail, increment fkey_start
7976 and start fkey_end from there. */
7977 while (fkey_end < t)
7979 Lisp_Object key;
7981 key = keybuf[fkey_end++];
7982 /* Look up meta-characters by prefixing them
7983 with meta_prefix_char. I hate this. */
7984 if (INTEGERP (key) && XINT (key) & meta_modifier)
7986 fkey_next
7987 = get_keymap_1
7988 (get_keyelt
7989 (access_keymap (fkey_map, meta_prefix_char, 1, 0), 0),
7990 0, 1);
7991 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
7993 else
7994 fkey_next = fkey_map;
7996 fkey_next
7997 = get_keyelt (access_keymap (fkey_next, key, 1, 0), 1);
7999 /* Handle symbol with autoload definition. */
8000 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8001 && CONSP (XSYMBOL (fkey_next)->function)
8002 && EQ (XCAR (XSYMBOL (fkey_next)->function), Qautoload))
8003 do_autoload (XSYMBOL (fkey_next)->function,
8004 fkey_next);
8006 /* Handle a symbol whose function definition is a keymap
8007 or an array. */
8008 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8009 && (!NILP (Farrayp (XSYMBOL (fkey_next)->function))
8010 || !NILP (Fkeymapp (XSYMBOL (fkey_next)->function))))
8011 fkey_next = XSYMBOL (fkey_next)->function;
8013 #if 0 /* I didn't turn this on, because it might cause trouble
8014 for the mapping of return into C-m and tab into C-i. */
8015 /* Optionally don't map function keys into other things.
8016 This enables the user to redefine kp- keys easily. */
8017 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
8018 fkey_next = Qnil;
8019 #endif
8021 /* If the function key map gives a function, not an
8022 array, then call the function with no args and use
8023 its value instead. */
8024 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8025 && fkey_end == t)
8027 struct gcpro gcpro1, gcpro2, gcpro3;
8028 Lisp_Object tem;
8029 tem = fkey_next;
8031 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
8032 fkey_next = call1 (fkey_next, prompt);
8033 UNGCPRO;
8034 /* If the function returned something invalid,
8035 barf--don't ignore it.
8036 (To ignore it safely, we would need to gcpro a bunch of
8037 other variables.) */
8038 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
8039 error ("Function in key-translation-map returns invalid key sequence");
8042 function_key_possible = ! NILP (fkey_next);
8044 /* If keybuf[fkey_start..fkey_end] is bound in the
8045 function key map and it's a suffix of the current
8046 sequence (i.e. fkey_end == t), replace it with
8047 the binding and restart with fkey_start at the end. */
8048 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
8049 && fkey_end == t)
8051 int len = XFASTINT (Flength (fkey_next));
8053 t = fkey_start + len;
8054 if (t >= bufsize)
8055 error ("Key sequence too long");
8057 if (VECTORP (fkey_next))
8058 bcopy (XVECTOR (fkey_next)->contents,
8059 keybuf + fkey_start,
8060 (t - fkey_start) * sizeof (keybuf[0]));
8061 else if (STRINGP (fkey_next))
8063 int i;
8065 for (i = 0; i < len; i++)
8066 XSETFASTINT (keybuf[fkey_start + i],
8067 XSTRING (fkey_next)->data[i]);
8070 mock_input = t;
8071 fkey_start = fkey_end = t;
8072 fkey_map = Vfunction_key_map;
8074 /* Do pass the results through key-translation-map.
8075 But don't retranslate what key-translation-map
8076 has already translated. */
8077 keytran_end = keytran_start;
8078 keytran_map = Vkey_translation_map;
8080 goto replay_sequence;
8083 fkey_map = get_keymap_1 (fkey_next, 0, 1);
8085 /* If we no longer have a bound suffix, try a new positions for
8086 fkey_start. */
8087 if (NILP (fkey_map))
8089 fkey_end = ++fkey_start;
8090 fkey_map = Vfunction_key_map;
8091 function_key_possible = 0;
8096 /* Look for this sequence in key-translation-map. */
8098 Lisp_Object keytran_next;
8100 /* Scan from keytran_end until we find a bound suffix. */
8101 while (keytran_end < t)
8103 Lisp_Object key;
8105 key = keybuf[keytran_end++];
8106 /* Look up meta-characters by prefixing them
8107 with meta_prefix_char. I hate this. */
8108 if (INTEGERP (key) && XINT (key) & meta_modifier)
8110 keytran_next
8111 = get_keymap_1
8112 (get_keyelt
8113 (access_keymap (keytran_map, meta_prefix_char, 1, 0), 0),
8114 0, 1);
8115 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
8117 else
8118 keytran_next = keytran_map;
8120 keytran_next
8121 = get_keyelt (access_keymap (keytran_next, key, 1, 0), 1);
8123 /* Handle symbol with autoload definition. */
8124 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8125 && CONSP (XSYMBOL (keytran_next)->function)
8126 && EQ (XCAR (XSYMBOL (keytran_next)->function), Qautoload))
8127 do_autoload (XSYMBOL (keytran_next)->function,
8128 keytran_next);
8130 /* Handle a symbol whose function definition is a keymap
8131 or an array. */
8132 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8133 && (!NILP (Farrayp (XSYMBOL (keytran_next)->function))
8134 || !NILP (Fkeymapp (XSYMBOL (keytran_next)->function))))
8135 keytran_next = XSYMBOL (keytran_next)->function;
8137 /* If the key translation map gives a function, not an
8138 array, then call the function with one arg and use
8139 its value instead. */
8140 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8141 && keytran_end == t)
8143 struct gcpro gcpro1, gcpro2, gcpro3;
8144 Lisp_Object tem;
8145 tem = keytran_next;
8147 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
8148 keytran_next = call1 (keytran_next, prompt);
8149 UNGCPRO;
8150 /* If the function returned something invalid,
8151 barf--don't ignore it.
8152 (To ignore it safely, we would need to gcpro a bunch of
8153 other variables.) */
8154 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
8155 error ("Function in key-translation-map returns invalid key sequence");
8158 key_translation_possible = ! NILP (keytran_next);
8160 /* If keybuf[keytran_start..keytran_end] is bound in the
8161 key translation map and it's a suffix of the current
8162 sequence (i.e. keytran_end == t), replace it with
8163 the binding and restart with keytran_start at the end. */
8164 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
8165 && keytran_end == t)
8167 int len = XFASTINT (Flength (keytran_next));
8169 t = keytran_start + len;
8170 if (t >= bufsize)
8171 error ("Key sequence too long");
8173 if (VECTORP (keytran_next))
8174 bcopy (XVECTOR (keytran_next)->contents,
8175 keybuf + keytran_start,
8176 (t - keytran_start) * sizeof (keybuf[0]));
8177 else if (STRINGP (keytran_next))
8179 int i;
8181 for (i = 0; i < len; i++)
8182 XSETFASTINT (keybuf[keytran_start + i],
8183 XSTRING (keytran_next)->data[i]);
8186 mock_input = t;
8187 keytran_start = keytran_end = t;
8188 keytran_map = Vkey_translation_map;
8190 /* Don't pass the results of key-translation-map
8191 through function-key-map. */
8192 fkey_start = fkey_end = t;
8193 fkey_map = Vfunction_key_map;
8195 goto replay_sequence;
8198 keytran_map = get_keymap_1 (keytran_next, 0, 1);
8200 /* If we no longer have a bound suffix, try a new positions for
8201 keytran_start. */
8202 if (NILP (keytran_map))
8204 keytran_end = ++keytran_start;
8205 keytran_map = Vkey_translation_map;
8206 key_translation_possible = 0;
8211 /* If KEY is not defined in any of the keymaps,
8212 and cannot be part of a function key or translation,
8213 and is an upper case letter
8214 use the corresponding lower-case letter instead. */
8215 if (first_binding == nmaps && ! function_key_possible
8216 && ! key_translation_possible
8217 && INTEGERP (key)
8218 && ((((XINT (key) & 0x3ffff)
8219 < XCHAR_TABLE (current_buffer->downcase_table)->size)
8220 && UPPERCASEP (XINT (key) & 0x3ffff))
8221 || (XINT (key) & shift_modifier)))
8223 Lisp_Object new_key;
8225 original_uppercase = key;
8226 original_uppercase_position = t - 1;
8228 if (XINT (key) & shift_modifier)
8229 XSETINT (new_key, XINT (key) & ~shift_modifier);
8230 else
8231 XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
8232 | (XINT (key) & ~0x3ffff)));
8234 /* We have to do this unconditionally, regardless of whether
8235 the lower-case char is defined in the keymaps, because they
8236 might get translated through function-key-map. */
8237 keybuf[t - 1] = new_key;
8238 mock_input = t;
8240 fkey_map = prev_fkey_map;
8241 fkey_start = prev_fkey_start;
8242 fkey_end = prev_fkey_end;
8244 keytran_map = prev_keytran_map;
8245 keytran_start = prev_keytran_start;
8246 keytran_end = prev_keytran_end;
8248 goto replay_sequence;
8250 /* If KEY is not defined in any of the keymaps,
8251 and cannot be part of a function key or translation,
8252 and is a shifted function key,
8253 use the corresponding unshifted function key instead. */
8254 if (first_binding == nmaps && ! function_key_possible
8255 && ! key_translation_possible
8256 && SYMBOLP (key))
8258 Lisp_Object breakdown;
8259 int modifiers;
8261 breakdown = parse_modifiers (key);
8262 modifiers = XINT (XCAR (XCDR (breakdown)));
8263 if (modifiers & shift_modifier)
8265 Lisp_Object new_key;
8267 original_uppercase = key;
8268 original_uppercase_position = t - 1;
8270 modifiers &= ~shift_modifier;
8271 new_key = apply_modifiers (modifiers,
8272 XCAR (breakdown));
8274 keybuf[t - 1] = new_key;
8275 mock_input = t;
8277 fkey_map = prev_fkey_map;
8278 fkey_start = prev_fkey_start;
8279 fkey_end = prev_fkey_end;
8281 keytran_map = prev_keytran_map;
8282 keytran_start = prev_keytran_start;
8283 keytran_end = prev_keytran_end;
8285 goto replay_sequence;
8290 if (!dummyflag)
8291 read_key_sequence_cmd = (first_binding < nmaps
8292 ? defs[first_binding]
8293 : Qnil);
8295 unread_switch_frame = delayed_switch_frame;
8296 unbind_to (count, Qnil);
8298 /* Don't downcase the last character if the caller says don't.
8299 Don't downcase it if the result is undefined, either. */
8300 if ((dont_downcase_last || first_binding >= nmaps)
8301 && t - 1 == original_uppercase_position)
8302 keybuf[t - 1] = original_uppercase;
8304 /* Occasionally we fabricate events, perhaps by expanding something
8305 according to function-key-map, or by adding a prefix symbol to a
8306 mouse click in the scroll bar or modeline. In this cases, return
8307 the entire generated key sequence, even if we hit an unbound
8308 prefix or a definition before the end. This means that you will
8309 be able to push back the event properly, and also means that
8310 read-key-sequence will always return a logical unit.
8312 Better ideas? */
8313 for (; t < mock_input; t++)
8315 if (echo_keystrokes)
8316 echo_char (keybuf[t]);
8317 add_command_key (keybuf[t]);
8322 return t;
8325 #if 0 /* This doc string is too long for some compilers.
8326 This commented-out definition serves for DOC. */
8327 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
8328 "Read a sequence of keystrokes and return as a string or vector.\n\
8329 The sequence is sufficient to specify a non-prefix command in the\n\
8330 current local and global maps.\n\
8332 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
8333 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
8334 as a continuation of the previous key.\n\
8336 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
8337 convert the last event to lower case. (Normally any upper case event\n\
8338 is converted to lower case if the original event is undefined and the lower\n\
8339 case equivalent is defined.) A non-nil value is appropriate for reading\n\
8340 a key sequence to be defined.\n\
8342 A C-g typed while in this function is treated like any other character,\n\
8343 and `quit-flag' is not set.\n\
8345 If the key sequence starts with a mouse click, then the sequence is read\n\
8346 using the keymaps of the buffer of the window clicked in, not the buffer\n\
8347 of the selected window as normal.\n\
8348 ""\n\
8349 `read-key-sequence' drops unbound button-down events, since you normally\n\
8350 only care about the click or drag events which follow them. If a drag\n\
8351 or multi-click event is unbound, but the corresponding click event would\n\
8352 be bound, `read-key-sequence' turns the event into a click event at the\n\
8353 drag's starting position. This means that you don't have to distinguish\n\
8354 between click and drag, double, or triple events unless you want to.\n\
8356 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
8357 lines separating windows, and scroll bars with imaginary keys\n\
8358 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
8360 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
8361 function will process a switch-frame event if the user switches frames\n\
8362 before typing anything. If the user switches frames in the middle of a\n\
8363 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
8364 is nil, then the event will be put off until after the current key sequence.\n\
8366 `read-key-sequence' checks `function-key-map' for function key\n\
8367 sequences, where they wouldn't conflict with ordinary bindings. See\n\
8368 `function-key-map' for more details.\n\
8370 The optional fifth argument COMMAND-LOOP, if non-nil, means\n\
8371 that this key sequence is being read by something that will\n\
8372 read commands one after another. It should be nil if the caller\n\
8373 will read just one key sequence.")
8374 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame, command-loop)
8375 #endif
8377 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 5, 0,
8379 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
8380 command_loop)
8381 Lisp_Object prompt, continue_echo, dont_downcase_last;
8382 Lisp_Object can_return_switch_frame, command_loop;
8384 Lisp_Object keybuf[30];
8385 register int i;
8386 struct gcpro gcpro1;
8387 int count = specpdl_ptr - specpdl;
8389 if (!NILP (prompt))
8390 CHECK_STRING (prompt, 0);
8391 QUIT;
8393 specbind (Qinput_method_exit_on_first_char,
8394 (NILP (command_loop) ? Qt : Qnil));
8395 specbind (Qinput_method_use_echo_area,
8396 (NILP (command_loop) ? Qt : Qnil));
8398 bzero (keybuf, sizeof keybuf);
8399 GCPRO1 (keybuf[0]);
8400 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
8402 if (NILP (continue_echo))
8404 this_command_key_count = 0;
8405 this_single_command_key_start = 0;
8408 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
8409 prompt, ! NILP (dont_downcase_last),
8410 ! NILP (can_return_switch_frame), 0);
8412 if (i == -1)
8414 Vquit_flag = Qt;
8415 QUIT;
8417 UNGCPRO;
8418 return unbind_to (count, make_event_array (i, keybuf));
8421 DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
8422 Sread_key_sequence_vector, 1, 5, 0,
8423 "Like `read-key-sequence' but always return a vector.")
8424 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
8425 command_loop)
8426 Lisp_Object prompt, continue_echo, dont_downcase_last;
8427 Lisp_Object can_return_switch_frame, command_loop;
8429 Lisp_Object keybuf[30];
8430 register int i;
8431 struct gcpro gcpro1;
8432 int count = specpdl_ptr - specpdl;
8434 if (!NILP (prompt))
8435 CHECK_STRING (prompt, 0);
8436 QUIT;
8438 specbind (Qinput_method_exit_on_first_char,
8439 (NILP (command_loop) ? Qt : Qnil));
8440 specbind (Qinput_method_use_echo_area,
8441 (NILP (command_loop) ? Qt : Qnil));
8443 bzero (keybuf, sizeof keybuf);
8444 GCPRO1 (keybuf[0]);
8445 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
8447 if (NILP (continue_echo))
8449 this_command_key_count = 0;
8450 this_single_command_key_start = 0;
8453 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
8454 prompt, ! NILP (dont_downcase_last),
8455 ! NILP (can_return_switch_frame), 0);
8457 if (i == -1)
8459 Vquit_flag = Qt;
8460 QUIT;
8462 UNGCPRO;
8463 return unbind_to (count, Fvector (i, keybuf));
8466 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
8467 "Execute CMD as an editor command.\n\
8468 CMD must be a symbol that satisfies the `commandp' predicate.\n\
8469 Optional second arg RECORD-FLAG non-nil\n\
8470 means unconditionally put this command in `command-history'.\n\
8471 Otherwise, that is done only if an arg is read using the minibuffer.\n\
8472 The argument KEYS specifies the value to use instead of (this-command-keys)\n\
8473 when reading the arguments; if it is nil, (this-command-keys) is used.\n\
8474 The argument SPECIAL, if non-nil, means that this command is executing\n\
8475 a special event, so ignore the prefix argument and don't clear it.")
8476 (cmd, record_flag, keys, special)
8477 Lisp_Object cmd, record_flag, keys, special;
8479 register Lisp_Object final;
8480 register Lisp_Object tem;
8481 Lisp_Object prefixarg;
8482 struct backtrace backtrace;
8483 extern int debug_on_next_call;
8485 debug_on_next_call = 0;
8487 if (NILP (special))
8489 prefixarg = current_kboard->Vprefix_arg;
8490 Vcurrent_prefix_arg = prefixarg;
8491 current_kboard->Vprefix_arg = Qnil;
8493 else
8494 prefixarg = Qnil;
8496 if (SYMBOLP (cmd))
8498 tem = Fget (cmd, Qdisabled);
8499 if (!NILP (tem) && !NILP (Vrun_hooks))
8501 tem = Fsymbol_value (Qdisabled_command_hook);
8502 if (!NILP (tem))
8503 return call1 (Vrun_hooks, Qdisabled_command_hook);
8507 while (1)
8509 final = Findirect_function (cmd);
8511 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
8513 struct gcpro gcpro1, gcpro2;
8515 GCPRO2 (cmd, prefixarg);
8516 do_autoload (final, cmd);
8517 UNGCPRO;
8519 else
8520 break;
8523 if (STRINGP (final) || VECTORP (final))
8525 /* If requested, place the macro in the command history. For
8526 other sorts of commands, call-interactively takes care of
8527 this. */
8528 if (!NILP (record_flag))
8530 Vcommand_history
8531 = Fcons (Fcons (Qexecute_kbd_macro,
8532 Fcons (final, Fcons (prefixarg, Qnil))),
8533 Vcommand_history);
8535 /* Don't keep command history around forever. */
8536 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
8538 tem = Fnthcdr (Vhistory_length, Vcommand_history);
8539 if (CONSP (tem))
8540 XCDR (tem) = Qnil;
8544 return Fexecute_kbd_macro (final, prefixarg);
8547 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
8549 backtrace.next = backtrace_list;
8550 backtrace_list = &backtrace;
8551 backtrace.function = &Qcall_interactively;
8552 backtrace.args = &cmd;
8553 backtrace.nargs = 1;
8554 backtrace.evalargs = 0;
8556 tem = Fcall_interactively (cmd, record_flag, keys);
8558 backtrace_list = backtrace.next;
8559 return tem;
8561 return Qnil;
8564 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
8565 1, 1, "P",
8566 "Read function name, then read its arguments and call it.")
8567 (prefixarg)
8568 Lisp_Object prefixarg;
8570 Lisp_Object function;
8571 char buf[40];
8572 Lisp_Object saved_keys;
8573 Lisp_Object bindings, value;
8574 struct gcpro gcpro1, gcpro2;
8576 saved_keys = Fvector (this_command_key_count,
8577 XVECTOR (this_command_keys)->contents);
8578 buf[0] = 0;
8579 GCPRO2 (saved_keys, prefixarg);
8581 if (EQ (prefixarg, Qminus))
8582 strcpy (buf, "- ");
8583 else if (CONSP (prefixarg) && XINT (XCAR (prefixarg)) == 4)
8584 strcpy (buf, "C-u ");
8585 else if (CONSP (prefixarg) && INTEGERP (XCAR (prefixarg)))
8587 if (sizeof (int) == sizeof (EMACS_INT))
8588 sprintf (buf, "%d ", XINT (XCAR (prefixarg)));
8589 else if (sizeof (long) == sizeof (EMACS_INT))
8590 sprintf (buf, "%ld ", (long) XINT (XCAR (prefixarg)));
8591 else
8592 abort ();
8594 else if (INTEGERP (prefixarg))
8596 if (sizeof (int) == sizeof (EMACS_INT))
8597 sprintf (buf, "%d ", XINT (prefixarg));
8598 else if (sizeof (long) == sizeof (EMACS_INT))
8599 sprintf (buf, "%ld ", (long) XINT (prefixarg));
8600 else
8601 abort ();
8604 /* This isn't strictly correct if execute-extended-command
8605 is bound to anything else. Perhaps it should use
8606 this_command_keys? */
8607 strcat (buf, "M-x ");
8609 /* Prompt with buf, and then read a string, completing from and
8610 restricting to the set of all defined commands. Don't provide
8611 any initial input. Save the command read on the extended-command
8612 history list. */
8613 function = Fcompleting_read (build_string (buf),
8614 Vobarray, Qcommandp,
8615 Qt, Qnil, Qextended_command_history, Qnil,
8616 Qnil);
8618 if (STRINGP (function) && XSTRING (function)->size == 0)
8619 error ("No command name given");
8621 /* Set this_command_keys to the concatenation of saved_keys and
8622 function, followed by a RET. */
8624 struct Lisp_String *str;
8625 Lisp_Object *keys;
8626 int i;
8628 this_command_key_count = 0;
8629 this_single_command_key_start = 0;
8631 keys = XVECTOR (saved_keys)->contents;
8632 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
8633 add_command_key (keys[i]);
8635 str = XSTRING (function);
8636 for (i = 0; i < str->size; i++)
8637 add_command_key (Faref (function, make_number (i)));
8639 add_command_key (make_number ('\015'));
8642 UNGCPRO;
8644 function = Fintern (function, Qnil);
8645 current_kboard->Vprefix_arg = prefixarg;
8646 Vthis_command = function;
8647 real_this_command = function;
8649 /* If enabled, show which key runs this command. */
8650 if (!NILP (Vsuggest_key_bindings)
8651 && NILP (Vexecuting_macro)
8652 && SYMBOLP (function))
8653 bindings = Fwhere_is_internal (function, Voverriding_local_map,
8654 Qt, Qnil);
8655 else
8656 bindings = Qnil;
8658 value = Qnil;
8659 GCPRO2 (bindings, value);
8660 value = Fcommand_execute (function, Qt, Qnil, Qnil);
8662 /* If the command has a key binding, print it now. */
8663 if (!NILP (bindings)
8664 && ! (VECTORP (bindings) && EQ (Faref (bindings, make_number (0)),
8665 Qmouse_movement)))
8667 /* But first wait, and skip the message if there is input. */
8668 int delay_time;
8669 if (!NILP (echo_area_buffer[0]))
8670 /* This command displayed something in the echo area;
8671 so wait a few seconds, then display our suggestion message. */
8672 delay_time = (NUMBERP (Vsuggest_key_bindings)
8673 ? XINT (Vsuggest_key_bindings) : 2);
8674 else
8675 /* This command left the echo area empty,
8676 so display our message immediately. */
8677 delay_time = 0;
8679 if (!NILP (Fsit_for (make_number (delay_time), Qnil, Qnil))
8680 && ! CONSP (Vunread_command_events))
8682 Lisp_Object binding;
8683 char *newmessage;
8684 int message_p = push_message ();
8686 binding = Fkey_description (bindings);
8688 newmessage
8689 = (char *) alloca (XSYMBOL (function)->name->size
8690 + STRING_BYTES (XSTRING (binding))
8691 + 100);
8692 sprintf (newmessage, "You can run the command `%s' with %s",
8693 XSYMBOL (function)->name->data,
8694 XSTRING (binding)->data);
8695 message2_nolog (newmessage,
8696 strlen (newmessage),
8697 STRING_MULTIBYTE (binding));
8698 if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
8699 ? Vsuggest_key_bindings : make_number (2)),
8700 Qnil, Qnil))
8701 && message_p)
8702 restore_message ();
8704 pop_message ();
8708 RETURN_UNGCPRO (value);
8711 /* Find the set of keymaps now active.
8712 Store into *MAPS_P a vector holding the various maps
8713 and return the number of them. The vector was malloc'd
8714 and the caller should free it. */
8717 current_active_maps (maps_p)
8718 Lisp_Object **maps_p;
8720 Lisp_Object *tmaps, *maps;
8721 int nmaps;
8723 /* Should overriding-terminal-local-map and overriding-local-map apply? */
8724 if (!NILP (Voverriding_local_map_menu_flag))
8726 /* Yes, use them (if non-nil) as well as the global map. */
8727 maps = (Lisp_Object *) xmalloc (3 * sizeof (maps[0]));
8728 nmaps = 0;
8729 if (!NILP (current_kboard->Voverriding_terminal_local_map))
8730 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
8731 if (!NILP (Voverriding_local_map))
8732 maps[nmaps++] = Voverriding_local_map;
8734 else
8736 /* No, so use major and minor mode keymaps. */
8737 nmaps = current_minor_maps (NULL, &tmaps);
8738 maps = (Lisp_Object *) xmalloc ((nmaps + 2) * sizeof (maps[0]));
8739 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
8740 #ifdef USE_TEXT_PROPERTIES
8741 maps[nmaps++] = get_local_map (PT, current_buffer);
8742 #else
8743 maps[nmaps++] = current_buffer->keymap;
8744 #endif
8746 maps[nmaps++] = current_global_map;
8748 *maps_p = maps;
8749 return nmaps;
8752 /* Return nonzero if input events are pending. */
8755 detect_input_pending ()
8757 if (!input_pending)
8758 get_input_pending (&input_pending, 0);
8760 return input_pending;
8763 /* Return nonzero if input events are pending, and run any pending timers. */
8766 detect_input_pending_run_timers (do_display)
8767 int do_display;
8769 int old_timers_run = timers_run;
8771 if (!input_pending)
8772 get_input_pending (&input_pending, 1);
8774 if (old_timers_run != timers_run && do_display)
8776 redisplay_preserve_echo_area ();
8777 /* The following fixes a bug when using lazy-lock with
8778 lazy-lock-defer-on-the-fly set to t, i.e. when fontifying
8779 from an idle timer function. The symptom of the bug is that
8780 the cursor sometimes doesn't become visible until the next X
8781 event is processed. --gerd. */
8782 if (rif)
8783 rif->flush_display (NULL);
8786 return input_pending;
8789 /* This is called in some cases before a possible quit.
8790 It cases the next call to detect_input_pending to recompute input_pending.
8791 So calling this function unnecessarily can't do any harm. */
8793 void
8794 clear_input_pending ()
8796 input_pending = 0;
8799 /* Return nonzero if there are pending requeued events.
8800 This isn't used yet. The hope is to make wait_reading_process_input
8801 call it, and return return if it runs Lisp code that unreads something.
8802 The problem is, kbd_buffer_get_event needs to be fixed to know what
8803 to do in that case. It isn't trivial. */
8806 requeued_events_pending_p ()
8808 return (!NILP (Vunread_command_events) || unread_command_char != -1);
8812 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
8813 "T if command input is currently available with no waiting.\n\
8814 Actually, the value is nil only if we can be sure that no input is available.")
8817 if (!NILP (Vunread_command_events) || unread_command_char != -1)
8818 return (Qt);
8820 get_input_pending (&input_pending, 1);
8821 return input_pending > 0 ? Qt : Qnil;
8824 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
8825 "Return vector of last 100 events, not counting those from keyboard macros.")
8828 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
8829 Lisp_Object val;
8831 if (total_keys < NUM_RECENT_KEYS)
8832 return Fvector (total_keys, keys);
8833 else
8835 val = Fvector (NUM_RECENT_KEYS, keys);
8836 bcopy (keys + recent_keys_index,
8837 XVECTOR (val)->contents,
8838 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
8839 bcopy (keys,
8840 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
8841 recent_keys_index * sizeof (Lisp_Object));
8842 return val;
8846 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
8847 "Return the key sequence that invoked this command.\n\
8848 The value is a string or a vector.")
8851 return make_event_array (this_command_key_count,
8852 XVECTOR (this_command_keys)->contents);
8855 DEFUN ("this-command-keys-vector", Fthis_command_keys_vector, Sthis_command_keys_vector, 0, 0, 0,
8856 "Return the key sequence that invoked this command, as a vector.")
8859 return Fvector (this_command_key_count,
8860 XVECTOR (this_command_keys)->contents);
8863 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
8864 Sthis_single_command_keys, 0, 0, 0,
8865 "Return the key sequence that invoked this command.\n\
8866 Unlike `this-command-keys', this function's value\n\
8867 does not include prefix arguments.\n\
8868 The value is always a vector.")
8871 return Fvector (this_command_key_count
8872 - this_single_command_key_start,
8873 (XVECTOR (this_command_keys)->contents
8874 + this_single_command_key_start));
8877 DEFUN ("this-single-command-raw-keys", Fthis_single_command_raw_keys,
8878 Sthis_single_command_raw_keys, 0, 0, 0,
8879 "Return the raw events that were read for this command.\n\
8880 Unlike `this-single-command-keys', this function's value\n\
8881 shows the events before all translations (except for input methods).\n\
8882 The value is always a vector.")
8885 return Fvector (raw_keybuf_count,
8886 (XVECTOR (raw_keybuf)->contents));
8889 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
8890 Sreset_this_command_lengths, 0, 0, 0,
8891 "Used for complicated reasons in `universal-argument-other-key'.\n\
8893 `universal-argument-other-key' rereads the event just typed.\n\
8894 It then gets translated through `function-key-map'.\n\
8895 The translated event gets included in the echo area and in\n\
8896 the value of `this-command-keys' in addition to the raw original event.\n\
8897 That is not right.\n\
8899 Calling this function directs the translated event to replace\n\
8900 the original event, so that only one version of the event actually\n\
8901 appears in the echo area and in the value of `this-command-keys.'.")
8904 before_command_restore_flag = 1;
8905 before_command_key_count_1 = before_command_key_count;
8906 before_command_echo_length_1 = before_command_echo_length;
8907 return Qnil;
8910 DEFUN ("clear-this-command-keys", Fclear_this_command_keys,
8911 Sclear_this_command_keys, 0, 0, 0,
8912 "Clear out the vector that `this-command-keys' returns.")
8915 this_command_key_count = 0;
8916 return Qnil;
8919 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
8920 "Return the current depth in recursive edits.")
8923 Lisp_Object temp;
8924 XSETFASTINT (temp, command_loop_level + minibuf_level);
8925 return temp;
8928 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
8929 "FOpen dribble file: ",
8930 "Start writing all keyboard characters to a dribble file called FILE.\n\
8931 If FILE is nil, close any open dribble file.")
8932 (file)
8933 Lisp_Object file;
8935 if (dribble)
8937 fclose (dribble);
8938 dribble = 0;
8940 if (!NILP (file))
8942 file = Fexpand_file_name (file, Qnil);
8943 dribble = fopen (XSTRING (file)->data, "w");
8944 if (dribble == 0)
8945 report_file_error ("Opening dribble", Fcons (file, Qnil));
8947 return Qnil;
8950 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
8951 "Discard the contents of the terminal input buffer.\n\
8952 Also cancel any kbd macro being defined.")
8955 current_kboard->defining_kbd_macro = Qnil;
8956 update_mode_lines++;
8958 Vunread_command_events = Qnil;
8959 unread_command_char = -1;
8961 discard_tty_input ();
8963 kbd_fetch_ptr = kbd_store_ptr;
8964 Ffillarray (kbd_buffer_frame_or_window, Qnil);
8965 input_pending = 0;
8967 return Qnil;
8970 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
8971 "Stop Emacs and return to superior process. You can resume later.\n\
8972 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
8973 control, run a subshell instead.\n\n\
8974 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
8975 to be read as terminal input by Emacs's parent, after suspension.\n\
8977 Before suspending, run the normal hook `suspend-hook'.\n\
8978 After resumption run the normal hook `suspend-resume-hook'.\n\
8980 Some operating systems cannot stop the Emacs process and resume it later.\n\
8981 On such systems, Emacs starts a subshell instead of suspending.")
8982 (stuffstring)
8983 Lisp_Object stuffstring;
8985 int count = specpdl_ptr - specpdl;
8986 int old_height, old_width;
8987 int width, height;
8988 struct gcpro gcpro1;
8990 if (!NILP (stuffstring))
8991 CHECK_STRING (stuffstring, 0);
8993 /* Run the functions in suspend-hook. */
8994 if (!NILP (Vrun_hooks))
8995 call1 (Vrun_hooks, intern ("suspend-hook"));
8997 GCPRO1 (stuffstring);
8998 get_frame_size (&old_width, &old_height);
8999 reset_sys_modes ();
9000 /* sys_suspend can get an error if it tries to fork a subshell
9001 and the system resources aren't available for that. */
9002 record_unwind_protect ((Lisp_Object (*) P_ ((Lisp_Object))) init_sys_modes,
9003 Qnil);
9004 stuff_buffered_input (stuffstring);
9005 if (cannot_suspend)
9006 sys_subshell ();
9007 else
9008 sys_suspend ();
9009 unbind_to (count, Qnil);
9011 /* Check if terminal/window size has changed.
9012 Note that this is not useful when we are running directly
9013 with a window system; but suspend should be disabled in that case. */
9014 get_frame_size (&width, &height);
9015 if (width != old_width || height != old_height)
9016 change_frame_size (SELECTED_FRAME (), height, width, 0, 0, 0);
9018 /* Run suspend-resume-hook. */
9019 if (!NILP (Vrun_hooks))
9020 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
9022 UNGCPRO;
9023 return Qnil;
9026 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
9027 Then in any case stuff anything Emacs has read ahead and not used. */
9029 void
9030 stuff_buffered_input (stuffstring)
9031 Lisp_Object stuffstring;
9033 /* stuff_char works only in BSD, versions 4.2 and up. */
9034 #ifdef BSD_SYSTEM
9035 #ifndef BSD4_1
9036 register unsigned char *p;
9038 if (STRINGP (stuffstring))
9040 register int count;
9042 p = XSTRING (stuffstring)->data;
9043 count = STRING_BYTES (XSTRING (stuffstring));
9044 while (count-- > 0)
9045 stuff_char (*p++);
9046 stuff_char ('\n');
9048 /* Anything we have read ahead, put back for the shell to read. */
9049 /* ?? What should this do when we have multiple keyboards??
9050 Should we ignore anything that was typed in at the "wrong" kboard? */
9051 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
9053 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
9054 kbd_fetch_ptr = kbd_buffer;
9055 if (kbd_fetch_ptr->kind == ascii_keystroke)
9056 stuff_char (kbd_fetch_ptr->code);
9057 kbd_fetch_ptr->kind = no_event;
9058 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
9059 - kbd_buffer]
9060 = Qnil);
9062 input_pending = 0;
9063 #endif
9064 #endif /* BSD_SYSTEM and not BSD4_1 */
9067 void
9068 set_waiting_for_input (time_to_clear)
9069 EMACS_TIME *time_to_clear;
9071 input_available_clear_time = time_to_clear;
9073 /* Tell interrupt_signal to throw back to read_char, */
9074 waiting_for_input = 1;
9076 /* If interrupt_signal was called before and buffered a C-g,
9077 make it run again now, to avoid timing error. */
9078 if (!NILP (Vquit_flag))
9079 quit_throw_to_read_char ();
9082 void
9083 clear_waiting_for_input ()
9085 /* Tell interrupt_signal not to throw back to read_char, */
9086 waiting_for_input = 0;
9087 input_available_clear_time = 0;
9090 /* This routine is called at interrupt level in response to C-G.
9091 If interrupt_input, this is the handler for SIGINT.
9092 Otherwise, it is called from kbd_buffer_store_event,
9093 in handling SIGIO or SIGTINT.
9095 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
9096 immediately throw back to read_char.
9098 Otherwise it sets the Lisp variable quit-flag not-nil.
9099 This causes eval to throw, when it gets a chance.
9100 If quit-flag is already non-nil, it stops the job right away. */
9102 SIGTYPE
9103 interrupt_signal (signalnum) /* If we don't have an argument, */
9104 int signalnum; /* some compilers complain in signal calls. */
9106 char c;
9107 /* Must preserve main program's value of errno. */
9108 int old_errno = errno;
9109 struct frame *sf = SELECTED_FRAME ();
9111 #if defined (USG) && !defined (POSIX_SIGNALS)
9112 if (!read_socket_hook && NILP (Vwindow_system))
9114 /* USG systems forget handlers when they are used;
9115 must reestablish each time */
9116 signal (SIGINT, interrupt_signal);
9117 signal (SIGQUIT, interrupt_signal);
9119 #endif /* USG */
9121 cancel_echoing ();
9123 if (!NILP (Vquit_flag)
9124 && (FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf)))
9126 /* If SIGINT isn't blocked, don't let us be interrupted by
9127 another SIGINT, it might be harmful due to non-reentrancy
9128 in I/O functions. */
9129 sigblock (sigmask (SIGINT));
9131 fflush (stdout);
9132 reset_sys_modes ();
9134 #ifdef SIGTSTP /* Support possible in later USG versions */
9136 * On systems which can suspend the current process and return to the original
9137 * shell, this command causes the user to end up back at the shell.
9138 * The "Auto-save" and "Abort" questions are not asked until
9139 * the user elects to return to emacs, at which point he can save the current
9140 * job and either dump core or continue.
9142 sys_suspend ();
9143 #else
9144 #ifdef VMS
9145 if (sys_suspend () == -1)
9147 printf ("Not running as a subprocess;\n");
9148 printf ("you can continue or abort.\n");
9150 #else /* not VMS */
9151 /* Perhaps should really fork an inferior shell?
9152 But that would not provide any way to get back
9153 to the original shell, ever. */
9154 printf ("No support for stopping a process on this operating system;\n");
9155 printf ("you can continue or abort.\n");
9156 #endif /* not VMS */
9157 #endif /* not SIGTSTP */
9158 #ifdef MSDOS
9159 /* We must remain inside the screen area when the internal terminal
9160 is used. Note that [Enter] is not echoed by dos. */
9161 cursor_to (0, 0);
9162 #endif
9163 /* It doesn't work to autosave while GC is in progress;
9164 the code used for auto-saving doesn't cope with the mark bit. */
9165 if (!gc_in_progress)
9167 printf ("Auto-save? (y or n) ");
9168 fflush (stdout);
9169 if (((c = getchar ()) & ~040) == 'Y')
9171 Fdo_auto_save (Qt, Qnil);
9172 #ifdef MSDOS
9173 printf ("\r\nAuto-save done");
9174 #else /* not MSDOS */
9175 printf ("Auto-save done\n");
9176 #endif /* not MSDOS */
9178 while (c != '\n') c = getchar ();
9180 else
9182 /* During GC, it must be safe to reenable quitting again. */
9183 Vinhibit_quit = Qnil;
9184 #ifdef MSDOS
9185 printf ("\r\n");
9186 #endif /* not MSDOS */
9187 printf ("Garbage collection in progress; cannot auto-save now\r\n");
9188 printf ("but will instead do a real quit after garbage collection ends\r\n");
9189 fflush (stdout);
9192 #ifdef MSDOS
9193 printf ("\r\nAbort? (y or n) ");
9194 #else /* not MSDOS */
9195 #ifdef VMS
9196 printf ("Abort (and enter debugger)? (y or n) ");
9197 #else /* not VMS */
9198 printf ("Abort (and dump core)? (y or n) ");
9199 #endif /* not VMS */
9200 #endif /* not MSDOS */
9201 fflush (stdout);
9202 if (((c = getchar ()) & ~040) == 'Y')
9203 abort ();
9204 while (c != '\n') c = getchar ();
9205 #ifdef MSDOS
9206 printf ("\r\nContinuing...\r\n");
9207 #else /* not MSDOS */
9208 printf ("Continuing...\n");
9209 #endif /* not MSDOS */
9210 fflush (stdout);
9211 init_sys_modes ();
9212 sigfree ();
9214 else
9216 /* If executing a function that wants to be interrupted out of
9217 and the user has not deferred quitting by binding `inhibit-quit'
9218 then quit right away. */
9219 if (immediate_quit && NILP (Vinhibit_quit))
9221 struct gl_state_s saved;
9222 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9224 immediate_quit = 0;
9225 sigfree ();
9226 saved = gl_state;
9227 GCPRO4 (saved.object, saved.global_code,
9228 saved.current_syntax_table, saved.old_prop);
9229 Fsignal (Qquit, Qnil);
9230 gl_state = saved;
9231 UNGCPRO;
9233 else
9234 /* Else request quit when it's safe */
9235 Vquit_flag = Qt;
9238 if (waiting_for_input && !echoing)
9239 quit_throw_to_read_char ();
9241 errno = old_errno;
9244 /* Handle a C-g by making read_char return C-g. */
9246 void
9247 quit_throw_to_read_char ()
9249 sigfree ();
9250 /* Prevent another signal from doing this before we finish. */
9251 clear_waiting_for_input ();
9252 input_pending = 0;
9254 Vunread_command_events = Qnil;
9255 unread_command_char = -1;
9257 #if 0 /* Currently, sit_for is called from read_char without turning
9258 off polling. And that can call set_waiting_for_input.
9259 It seems to be harmless. */
9260 #ifdef POLL_FOR_INPUT
9261 /* May be > 1 if in recursive minibuffer. */
9262 if (poll_suppress_count == 0)
9263 abort ();
9264 #endif
9265 #endif
9266 if (FRAMEP (internal_last_event_frame)
9267 && !EQ (internal_last_event_frame, selected_frame))
9268 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
9269 Qnil, 0);
9271 _longjmp (getcjmp, 1);
9274 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
9275 "Set mode of reading keyboard input.\n\
9276 First arg INTERRUPT non-nil means use input interrupts;\n\
9277 nil means use CBREAK mode.\n\
9278 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
9279 (no effect except in CBREAK mode).\n\
9280 Third arg META t means accept 8-bit input (for a Meta key).\n\
9281 META nil means ignore the top bit, on the assumption it is parity.\n\
9282 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
9283 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
9284 See also `current-input-mode'.")
9285 (interrupt, flow, meta, quit)
9286 Lisp_Object interrupt, flow, meta, quit;
9288 if (!NILP (quit)
9289 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
9290 error ("set-input-mode: QUIT must be an ASCII character");
9292 #ifdef POLL_FOR_INPUT
9293 stop_polling ();
9294 #endif
9296 #ifndef DOS_NT
9297 /* this causes startup screen to be restored and messes with the mouse */
9298 reset_sys_modes ();
9299 #endif
9301 #ifdef SIGIO
9302 /* Note SIGIO has been undef'd if FIONREAD is missing. */
9303 if (read_socket_hook)
9305 /* When using X, don't give the user a real choice,
9306 because we haven't implemented the mechanisms to support it. */
9307 #ifdef NO_SOCK_SIGIO
9308 interrupt_input = 0;
9309 #else /* not NO_SOCK_SIGIO */
9310 interrupt_input = 1;
9311 #endif /* NO_SOCK_SIGIO */
9313 else
9314 interrupt_input = !NILP (interrupt);
9315 #else /* not SIGIO */
9316 interrupt_input = 0;
9317 #endif /* not SIGIO */
9319 /* Our VMS input only works by interrupts, as of now. */
9320 #ifdef VMS
9321 interrupt_input = 1;
9322 #endif
9324 flow_control = !NILP (flow);
9325 if (NILP (meta))
9326 meta_key = 0;
9327 else if (EQ (meta, Qt))
9328 meta_key = 1;
9329 else
9330 meta_key = 2;
9331 if (!NILP (quit))
9332 /* Don't let this value be out of range. */
9333 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
9335 #ifndef DOS_NT
9336 init_sys_modes ();
9337 #endif
9339 #ifdef POLL_FOR_INPUT
9340 poll_suppress_count = 1;
9341 start_polling ();
9342 #endif
9343 return Qnil;
9346 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
9347 "Return information about the way Emacs currently reads keyboard input.\n\
9348 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
9349 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
9350 nil, Emacs is using CBREAK mode.\n\
9351 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
9352 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
9353 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
9354 META nil means ignoring the top bit, on the assumption it is parity.\n\
9355 META is neither t nor nil if accepting 8-bit input and using\n\
9356 all 8 bits as the character code.\n\
9357 QUIT is the character Emacs currently uses to quit.\n\
9358 The elements of this list correspond to the arguments of\n\
9359 `set-input-mode'.")
9362 Lisp_Object val[4];
9364 val[0] = interrupt_input ? Qt : Qnil;
9365 val[1] = flow_control ? Qt : Qnil;
9366 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
9367 XSETFASTINT (val[3], quit_char);
9369 return Flist (sizeof (val) / sizeof (val[0]), val);
9374 * Set up a new kboard object with reasonable initial values.
9376 void
9377 init_kboard (kb)
9378 KBOARD *kb;
9380 kb->Voverriding_terminal_local_map = Qnil;
9381 kb->Vlast_command = Qnil;
9382 kb->Vreal_last_command = Qnil;
9383 kb->Vprefix_arg = Qnil;
9384 kb->Vlast_prefix_arg = Qnil;
9385 kb->kbd_queue = Qnil;
9386 kb->kbd_queue_has_data = 0;
9387 kb->immediate_echo = 0;
9388 kb->echoptr = kb->echobuf;
9389 kb->echo_after_prompt = -1;
9390 kb->kbd_macro_buffer = 0;
9391 kb->kbd_macro_bufsize = 0;
9392 kb->defining_kbd_macro = Qnil;
9393 kb->Vlast_kbd_macro = Qnil;
9394 kb->reference_count = 0;
9395 kb->Vsystem_key_alist = Qnil;
9396 kb->system_key_syms = Qnil;
9397 kb->Vdefault_minibuffer_frame = Qnil;
9401 * Destroy the contents of a kboard object, but not the object itself.
9402 * We use this just before deleting it, or if we're going to initialize
9403 * it a second time.
9405 static void
9406 wipe_kboard (kb)
9407 KBOARD *kb;
9409 if (kb->kbd_macro_buffer)
9410 xfree (kb->kbd_macro_buffer);
9413 #ifdef MULTI_KBOARD
9414 void
9415 delete_kboard (kb)
9416 KBOARD *kb;
9418 KBOARD **kbp;
9419 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
9420 if (*kbp == NULL)
9421 abort ();
9422 *kbp = kb->next_kboard;
9423 wipe_kboard (kb);
9424 xfree (kb);
9426 #endif
9428 void
9429 init_keyboard ()
9431 /* This is correct before outermost invocation of the editor loop */
9432 command_loop_level = -1;
9433 immediate_quit = 0;
9434 quit_char = Ctl ('g');
9435 Vunread_command_events = Qnil;
9436 unread_command_char = -1;
9437 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
9438 total_keys = 0;
9439 recent_keys_index = 0;
9440 kbd_fetch_ptr = kbd_buffer;
9441 kbd_store_ptr = kbd_buffer;
9442 kbd_buffer_frame_or_window
9443 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
9444 #ifdef HAVE_MOUSE
9445 do_mouse_tracking = Qnil;
9446 #endif
9447 input_pending = 0;
9449 /* This means that command_loop_1 won't try to select anything the first
9450 time through. */
9451 internal_last_event_frame = Qnil;
9452 Vlast_event_frame = internal_last_event_frame;
9454 #ifdef MULTI_KBOARD
9455 current_kboard = initial_kboard;
9456 #endif
9457 wipe_kboard (current_kboard);
9458 init_kboard (current_kboard);
9460 if (initialized)
9461 Ffillarray (kbd_buffer_frame_or_window, Qnil);
9463 kbd_buffer_frame_or_window
9464 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
9465 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
9467 signal (SIGINT, interrupt_signal);
9468 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
9469 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
9470 SIGQUIT and we can't tell which one it will give us. */
9471 signal (SIGQUIT, interrupt_signal);
9472 #endif /* HAVE_TERMIO */
9474 /* Note SIGIO has been undef'd if FIONREAD is missing. */
9475 #ifdef SIGIO
9476 if (!noninteractive)
9477 signal (SIGIO, input_available_signal);
9478 #endif /* SIGIO */
9480 /* Use interrupt input by default, if it works and noninterrupt input
9481 has deficiencies. */
9483 #ifdef INTERRUPT_INPUT
9484 interrupt_input = 1;
9485 #else
9486 interrupt_input = 0;
9487 #endif
9489 /* Our VMS input only works by interrupts, as of now. */
9490 #ifdef VMS
9491 interrupt_input = 1;
9492 #endif
9494 sigfree ();
9495 dribble = 0;
9497 if (keyboard_init_hook)
9498 (*keyboard_init_hook) ();
9500 #ifdef POLL_FOR_INPUT
9501 poll_suppress_count = 1;
9502 start_polling ();
9503 #endif
9506 /* This type's only use is in syms_of_keyboard, to initialize the
9507 event header symbols and put properties on them. */
9508 struct event_head {
9509 Lisp_Object *var;
9510 char *name;
9511 Lisp_Object *kind;
9514 struct event_head head_table[] = {
9515 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
9516 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
9517 &Qswitch_frame, "switch-frame", &Qswitch_frame,
9518 &Qdelete_frame, "delete-frame", &Qdelete_frame,
9519 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
9520 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
9523 void
9524 syms_of_keyboard ()
9526 /* Tool-bars. */
9527 QCimage = intern (":image");
9528 staticpro (&QCimage);
9530 staticpro (&Qhelp_echo);
9531 Qhelp_echo = intern ("help-echo");
9533 staticpro (&item_properties);
9534 item_properties = Qnil;
9536 staticpro (&tool_bar_item_properties);
9537 tool_bar_item_properties = Qnil;
9538 staticpro (&tool_bar_items_vector);
9539 tool_bar_items_vector = Qnil;
9541 staticpro (&real_this_command);
9542 real_this_command = Qnil;
9544 Qtimer_event_handler = intern ("timer-event-handler");
9545 staticpro (&Qtimer_event_handler);
9547 Qdisabled_command_hook = intern ("disabled-command-hook");
9548 staticpro (&Qdisabled_command_hook);
9550 Qself_insert_command = intern ("self-insert-command");
9551 staticpro (&Qself_insert_command);
9553 Qforward_char = intern ("forward-char");
9554 staticpro (&Qforward_char);
9556 Qbackward_char = intern ("backward-char");
9557 staticpro (&Qbackward_char);
9559 Qdisabled = intern ("disabled");
9560 staticpro (&Qdisabled);
9562 Qundefined = intern ("undefined");
9563 staticpro (&Qundefined);
9565 Qpre_command_hook = intern ("pre-command-hook");
9566 staticpro (&Qpre_command_hook);
9568 Qpost_command_hook = intern ("post-command-hook");
9569 staticpro (&Qpost_command_hook);
9571 Qpost_command_idle_hook = intern ("post-command-idle-hook");
9572 staticpro (&Qpost_command_idle_hook);
9574 Qdeferred_action_function = intern ("deferred-action-function");
9575 staticpro (&Qdeferred_action_function);
9577 Qcommand_hook_internal = intern ("command-hook-internal");
9578 staticpro (&Qcommand_hook_internal);
9580 Qfunction_key = intern ("function-key");
9581 staticpro (&Qfunction_key);
9582 Qmouse_click = intern ("mouse-click");
9583 staticpro (&Qmouse_click);
9584 #ifdef WINDOWSNT
9585 Qmouse_wheel = intern ("mouse-wheel");
9586 staticpro (&Qmouse_wheel);
9587 Qlanguage_change = intern ("language-change");
9588 staticpro (&Qlanguage_change);
9589 #endif
9590 Qdrag_n_drop = intern ("drag-n-drop");
9591 staticpro (&Qdrag_n_drop);
9593 Qusr1_signal = intern ("usr1-signal");
9594 staticpro (&Qusr1_signal);
9595 Qusr2_signal = intern ("usr2-signal");
9596 staticpro (&Qusr2_signal);
9598 Qmenu_enable = intern ("menu-enable");
9599 staticpro (&Qmenu_enable);
9600 Qmenu_alias = intern ("menu-alias");
9601 staticpro (&Qmenu_alias);
9602 QCenable = intern (":enable");
9603 staticpro (&QCenable);
9604 QCvisible = intern (":visible");
9605 staticpro (&QCvisible);
9606 QChelp = intern (":help");
9607 staticpro (&QChelp);
9608 QCfilter = intern (":filter");
9609 staticpro (&QCfilter);
9610 QCbutton = intern (":button");
9611 staticpro (&QCbutton);
9612 QCkeys = intern (":keys");
9613 staticpro (&QCkeys);
9614 QCkey_sequence = intern (":key-sequence");
9615 staticpro (&QCkey_sequence);
9616 QCtoggle = intern (":toggle");
9617 staticpro (&QCtoggle);
9618 QCradio = intern (":radio");
9619 staticpro (&QCradio);
9621 Qmode_line = intern ("mode-line");
9622 staticpro (&Qmode_line);
9623 Qvertical_line = intern ("vertical-line");
9624 staticpro (&Qvertical_line);
9625 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
9626 staticpro (&Qvertical_scroll_bar);
9627 Qmenu_bar = intern ("menu-bar");
9628 staticpro (&Qmenu_bar);
9630 Qabove_handle = intern ("above-handle");
9631 staticpro (&Qabove_handle);
9632 Qhandle = intern ("handle");
9633 staticpro (&Qhandle);
9634 Qbelow_handle = intern ("below-handle");
9635 staticpro (&Qbelow_handle);
9636 Qup = intern ("up");
9637 staticpro (&Qup);
9638 Qdown = intern ("down");
9639 staticpro (&Qdown);
9640 Qtop = intern ("top");
9641 staticpro (&Qtop);
9642 Qbottom = intern ("bottom");
9643 staticpro (&Qbottom);
9644 Qend_scroll = intern ("end-scroll");
9645 staticpro (&Qend_scroll);
9647 Qevent_kind = intern ("event-kind");
9648 staticpro (&Qevent_kind);
9649 Qevent_symbol_elements = intern ("event-symbol-elements");
9650 staticpro (&Qevent_symbol_elements);
9651 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
9652 staticpro (&Qevent_symbol_element_mask);
9653 Qmodifier_cache = intern ("modifier-cache");
9654 staticpro (&Qmodifier_cache);
9656 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
9657 staticpro (&Qrecompute_lucid_menubar);
9658 Qactivate_menubar_hook = intern ("activate-menubar-hook");
9659 staticpro (&Qactivate_menubar_hook);
9661 Qpolling_period = intern ("polling-period");
9662 staticpro (&Qpolling_period);
9664 Qinput_method_function = intern ("input-method-function");
9665 staticpro (&Qinput_method_function);
9667 Qinput_method_exit_on_first_char = intern ("input-method-exit-on-first-char");
9668 staticpro (&Qinput_method_exit_on_first_char);
9669 Qinput_method_use_echo_area = intern ("input-method-use-echo-area");
9670 staticpro (&Qinput_method_use_echo_area);
9672 Fset (Qinput_method_exit_on_first_char, Qnil);
9673 Fset (Qinput_method_use_echo_area, Qnil);
9676 struct event_head *p;
9678 for (p = head_table;
9679 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
9680 p++)
9682 *p->var = intern (p->name);
9683 staticpro (p->var);
9684 Fput (*p->var, Qevent_kind, *p->kind);
9685 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
9689 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
9690 staticpro (&button_down_location);
9693 int i;
9694 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
9696 modifier_symbols = Fmake_vector (make_number (len), Qnil);
9697 for (i = 0; i < len; i++)
9698 if (modifier_names[i])
9699 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
9700 staticpro (&modifier_symbols);
9703 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
9704 staticpro (&recent_keys);
9706 this_command_keys = Fmake_vector (make_number (40), Qnil);
9707 staticpro (&this_command_keys);
9709 raw_keybuf = Fmake_vector (make_number (30), Qnil);
9710 staticpro (&raw_keybuf);
9712 Qextended_command_history = intern ("extended-command-history");
9713 Fset (Qextended_command_history, Qnil);
9714 staticpro (&Qextended_command_history);
9716 kbd_buffer_frame_or_window
9717 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
9718 staticpro (&kbd_buffer_frame_or_window);
9720 accent_key_syms = Qnil;
9721 staticpro (&accent_key_syms);
9723 func_key_syms = Qnil;
9724 staticpro (&func_key_syms);
9726 mouse_syms = Qnil;
9727 staticpro (&mouse_syms);
9729 #ifdef WINDOWSNT
9730 mouse_wheel_syms = Qnil;
9731 staticpro (&mouse_wheel_syms);
9733 drag_n_drop_syms = Qnil;
9734 staticpro (&drag_n_drop_syms);
9735 #endif
9737 unread_switch_frame = Qnil;
9738 staticpro (&unread_switch_frame);
9740 internal_last_event_frame = Qnil;
9741 staticpro (&internal_last_event_frame);
9743 read_key_sequence_cmd = Qnil;
9744 staticpro (&read_key_sequence_cmd);
9746 menu_bar_one_keymap_changed_items = Qnil;
9747 staticpro (&menu_bar_one_keymap_changed_items);
9749 defsubr (&Sevent_convert_list);
9750 defsubr (&Sread_key_sequence);
9751 defsubr (&Sread_key_sequence_vector);
9752 defsubr (&Srecursive_edit);
9753 #ifdef HAVE_MOUSE
9754 defsubr (&Strack_mouse);
9755 #endif
9756 defsubr (&Sinput_pending_p);
9757 defsubr (&Scommand_execute);
9758 defsubr (&Srecent_keys);
9759 defsubr (&Sthis_command_keys);
9760 defsubr (&Sthis_command_keys_vector);
9761 defsubr (&Sthis_single_command_keys);
9762 defsubr (&Sthis_single_command_raw_keys);
9763 defsubr (&Sreset_this_command_lengths);
9764 defsubr (&Sclear_this_command_keys);
9765 defsubr (&Ssuspend_emacs);
9766 defsubr (&Sabort_recursive_edit);
9767 defsubr (&Sexit_recursive_edit);
9768 defsubr (&Srecursion_depth);
9769 defsubr (&Stop_level);
9770 defsubr (&Sdiscard_input);
9771 defsubr (&Sopen_dribble_file);
9772 defsubr (&Sset_input_mode);
9773 defsubr (&Scurrent_input_mode);
9774 defsubr (&Sexecute_extended_command);
9776 DEFVAR_LISP ("last-command-char", &last_command_char,
9777 "Last input event that was part of a command.");
9779 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
9780 "Last input event that was part of a command.");
9782 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
9783 "Last input event in a command, except for mouse menu events.\n\
9784 Mouse menus give back keys that don't look like mouse events;\n\
9785 this variable holds the actual mouse event that led to the menu,\n\
9786 so that you can determine whether the command was run by mouse or not.");
9788 DEFVAR_LISP ("last-input-char", &last_input_char,
9789 "Last input event.");
9791 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
9792 "Last input event.");
9794 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
9795 "List of events to be read as the command input.\n\
9796 These events are processed first, before actual keyboard input.");
9797 Vunread_command_events = Qnil;
9799 DEFVAR_INT ("unread-command-char", &unread_command_char,
9800 "If not -1, an object to be read as next command input event.");
9802 DEFVAR_LISP ("unread-post-input-method-events", &Vunread_post_input_method_events,
9803 "List of events to be processed as input by input methods.\n\
9804 These events are processed after `unread-command-events', but\n\
9805 before actual keyboard input.");
9806 Vunread_post_input_method_events = Qnil;
9808 DEFVAR_LISP ("unread-input-method-events", &Vunread_input_method_events,
9809 "List of events to be processed as input by input methods.\n\
9810 These events are processed after `unread-command-events', but\n\
9811 before actual keyboard input.");
9812 Vunread_input_method_events = Qnil;
9814 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
9815 "Meta-prefix character code.\n\
9816 Meta-foo as command input turns into this character followed by foo.");
9817 XSETINT (meta_prefix_char, 033);
9819 DEFVAR_KBOARD ("last-command", Vlast_command,
9820 "The last command executed.\n\
9821 Normally a symbol with a function definition, but can be whatever was found\n\
9822 in the keymap, or whatever the variable `this-command' was set to by that\n\
9823 command.\n\
9825 The value `mode-exit' is special; it means that the previous command\n\
9826 read an event that told it to exit, and it did so and unread that event.\n\
9827 In other words, the present command is the event that made the previous\n\
9828 command exit.\n\
9830 The value `kill-region' is special; it means that the previous command\n\
9831 was a kill command.");
9833 DEFVAR_KBOARD ("real-last-command", Vreal_last_command,
9834 "Same as `last-command', but never altered by Lisp code.");
9836 DEFVAR_LISP ("this-command", &Vthis_command,
9837 "The command now being executed.\n\
9838 The command can set this variable; whatever is put here\n\
9839 will be in `last-command' during the following command.");
9840 Vthis_command = Qnil;
9842 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
9843 "*Number of input events between auto-saves.\n\
9844 Zero means disable autosaving due to number of characters typed.");
9845 auto_save_interval = 300;
9847 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
9848 "*Number of seconds idle time before auto-save.\n\
9849 Zero or nil means disable auto-saving due to idleness.\n\
9850 After auto-saving due to this many seconds of idle time,\n\
9851 Emacs also does a garbage collection if that seems to be warranted.");
9852 XSETFASTINT (Vauto_save_timeout, 30);
9854 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
9855 "*Nonzero means echo unfinished commands after this many seconds of pause.");
9856 echo_keystrokes = 1;
9858 DEFVAR_INT ("polling-period", &polling_period,
9859 "*Interval between polling for input during Lisp execution.\n\
9860 The reason for polling is to make C-g work to stop a running program.\n\
9861 Polling is needed only when using X windows and SIGIO does not work.\n\
9862 Polling is automatically disabled in all other cases.");
9863 polling_period = 2;
9865 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
9866 "*Maximum time between mouse clicks to make a double-click.\n\
9867 Measured in milliseconds. nil means disable double-click recognition;\n\
9868 t means double-clicks have no time limit and are detected\n\
9869 by position only.");
9870 Vdouble_click_time = make_number (500);
9872 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
9873 "*Non-nil means inhibit local map menu bar menus.");
9874 inhibit_local_menu_bar_menus = 0;
9876 DEFVAR_INT ("num-input-keys", &num_input_keys,
9877 "Number of complete key sequences read as input so far.\n\
9878 This includes key sequences read from keyboard macros.\n\
9879 The number is effectively the number of interactive command invocations.");
9880 num_input_keys = 0;
9882 DEFVAR_INT ("num-nonmacro-input-events", &num_nonmacro_input_events,
9883 "Number of input events read from the keyboard so far.\n\
9884 This does not include events generated by keyboard macros.");
9885 num_nonmacro_input_events = 0;
9887 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
9888 "The frame in which the most recently read event occurred.\n\
9889 If the last event came from a keyboard macro, this is set to `macro'.");
9890 Vlast_event_frame = Qnil;
9892 /* This variable is set up in sysdep.c. */
9893 DEFVAR_LISP ("tty-erase-char", &Vtty_erase_char,
9894 "The ERASE character as set by the user with stty.");
9896 DEFVAR_LISP ("help-char", &Vhelp_char,
9897 "Character to recognize as meaning Help.\n\
9898 When it is read, do `(eval help-form)', and display result if it's a string.\n\
9899 If the value of `help-form' is nil, this char can be read normally.");
9900 XSETINT (Vhelp_char, Ctl ('H'));
9902 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
9903 "List of input events to recognize as meaning Help.\n\
9904 These work just like the value of `help-char' (see that).");
9905 Vhelp_event_list = Qnil;
9907 DEFVAR_LISP ("help-form", &Vhelp_form,
9908 "Form to execute when character `help-char' is read.\n\
9909 If the form returns a string, that string is displayed.\n\
9910 If `help-form' is nil, the help char is not recognized.");
9911 Vhelp_form = Qnil;
9913 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
9914 "Command to run when `help-char' character follows a prefix key.\n\
9915 This command is used only when there is no actual binding\n\
9916 for that character after that prefix key.");
9917 Vprefix_help_command = Qnil;
9919 DEFVAR_LISP ("top-level", &Vtop_level,
9920 "Form to evaluate when Emacs starts up.\n\
9921 Useful to set before you dump a modified Emacs.");
9922 Vtop_level = Qnil;
9924 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
9925 "Translate table for keyboard input, or nil.\n\
9926 Each character is looked up in this string and the contents used instead.\n\
9927 The value may be a string, a vector, or a char-table.\n\
9928 If it is a string or vector of length N,\n\
9929 character codes N and up are untranslated.\n\
9930 In a vector or a char-table, an element which is nil means \"no translation\".");
9931 Vkeyboard_translate_table = Qnil;
9933 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
9934 "Non-nil means to always spawn a subshell instead of suspending.\n\
9935 \(Even if the operating system has support for stopping a process.\)");
9936 cannot_suspend = 0;
9938 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
9939 "Non-nil means prompt with menus when appropriate.\n\
9940 This is done when reading from a keymap that has a prompt string,\n\
9941 for elements that have prompt strings.\n\
9942 The menu is displayed on the screen\n\
9943 if X menus were enabled at configuration\n\
9944 time and the previous event was a mouse click prefix key.\n\
9945 Otherwise, menu prompting uses the echo area.");
9946 menu_prompting = 1;
9948 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
9949 "Character to see next line of menu prompt.\n\
9950 Type this character while in a menu prompt to rotate around the lines of it.");
9951 XSETINT (menu_prompt_more_char, ' ');
9953 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
9954 "A mask of additional modifier keys to use with every keyboard character.\n\
9955 Emacs applies the modifiers of the character stored here to each keyboard\n\
9956 character it reads. For example, after evaluating the expression\n\
9957 (setq extra-keyboard-modifiers ?\\C-x)\n\
9958 all input characters will have the control modifier applied to them.\n\
9960 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
9961 not count as a control character; rather, it counts as a character\n\
9962 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
9963 cancels any modification.");
9964 extra_keyboard_modifiers = 0;
9966 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
9967 "If an editing command sets this to t, deactivate the mark afterward.\n\
9968 The command loop sets this to nil before each command,\n\
9969 and tests the value when the command returns.\n\
9970 Buffer modification stores t in this variable.");
9971 Vdeactivate_mark = Qnil;
9973 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
9974 "Temporary storage of pre-command-hook or post-command-hook.");
9975 Vcommand_hook_internal = Qnil;
9977 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
9978 "Normal hook run before each command is executed.\n\
9979 If an unhandled error happens in running this hook,\n\
9980 the hook value is set to nil, since otherwise the error\n\
9981 might happen repeatedly and make Emacs nonfunctional.");
9982 Vpre_command_hook = Qnil;
9984 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
9985 "Normal hook run after each command is executed.\n\
9986 If an unhandled error happens in running this hook,\n\
9987 the hook value is set to nil, since otherwise the error\n\
9988 might happen repeatedly and make Emacs nonfunctional.");
9989 Vpost_command_hook = Qnil;
9991 DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
9992 "Normal hook run after each command is executed, if idle.\n\
9993 Errors running the hook are caught and ignored.\n\
9994 This feature is obsolete; use idle timers instead. See `etc/NEWS'.");
9995 Vpost_command_idle_hook = Qnil;
9997 DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
9998 "Delay time before running `post-command-idle-hook'.\n\
9999 This is measured in microseconds.");
10000 post_command_idle_delay = 100000;
10002 #if 0
10003 DEFVAR_LISP ("echo-area-clear-hook", ...,
10004 "Normal hook run when clearing the echo area.");
10005 #endif
10006 Qecho_area_clear_hook = intern ("echo-area-clear-hook");
10007 XSYMBOL (Qecho_area_clear_hook)->value = Qnil;
10009 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
10010 "t means menu bar, specified Lucid style, needs to be recomputed.");
10011 Vlucid_menu_bar_dirty_flag = Qnil;
10013 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
10014 "List of menu bar items to move to the end of the menu bar.\n\
10015 The elements of the list are event types that may have menu bar bindings.");
10016 Vmenu_bar_final_items = Qnil;
10018 DEFVAR_KBOARD ("overriding-terminal-local-map",
10019 Voverriding_terminal_local_map,
10020 "Per-terminal keymap that overrides all other local keymaps.\n\
10021 If this variable is non-nil, it is used as a keymap instead of the\n\
10022 buffer's local map, and the minor mode keymaps and text property keymaps.\n\
10023 This variable is intended to let commands such as `universal-argumemnt'\n\
10024 set up a different keymap for reading the next command.");
10026 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
10027 "Keymap that overrides all other local keymaps.\n\
10028 If this variable is non-nil, it is used as a keymap instead of the\n\
10029 buffer's local map, and the minor mode keymaps and text property keymaps.");
10030 Voverriding_local_map = Qnil;
10032 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
10033 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
10034 Otherwise, the menu bar continues to reflect the buffer's local map\n\
10035 and the minor mode maps regardless of `overriding-local-map'.");
10036 Voverriding_local_map_menu_flag = Qnil;
10038 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
10039 "Keymap defining bindings for special events to execute at low level.");
10040 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
10042 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
10043 "*Non-nil means generate motion events for mouse motion.");
10045 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
10046 "Alist of system-specific X windows key symbols.\n\
10047 Each element should have the form (N . SYMBOL) where N is the\n\
10048 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
10049 and SYMBOL is its name.");
10051 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
10052 "List of deferred actions to be performed at a later time.\n\
10053 The precise format isn't relevant here; we just check whether it is nil.");
10054 Vdeferred_action_list = Qnil;
10056 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
10057 "Function to call to handle deferred actions, after each command.\n\
10058 This function is called with no arguments after each command\n\
10059 whenever `deferred-action-list' is non-nil.");
10060 Vdeferred_action_function = Qnil;
10062 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
10063 "*Non-nil means show the equivalent key-binding when M-x command has one.\n\
10064 The value can be a length of time to show the message for.\n\
10065 If the value is non-nil and not a number, we wait 2 seconds.");
10066 Vsuggest_key_bindings = Qt;
10068 DEFVAR_LISP ("timer-list", &Vtimer_list,
10069 "List of active absolute time timers in order of increasing time");
10070 Vtimer_list = Qnil;
10072 DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
10073 "List of active idle-time timers in order of increasing time");
10074 Vtimer_idle_list = Qnil;
10076 DEFVAR_LISP ("input-method-function", &Vinput_method_function,
10077 "If non-nil, the function that implements the current input method.\n\
10078 It's called with one argument, a printing character that was just read.\n\
10079 \(That means a character with code 040...0176.)\n\
10080 Typically this function uses `read-event' to read additional events.\n\
10081 When it does so, it should first bind `input-method-function' to nil\n\
10082 so it will not be called recursively.\n\
10084 The function should return a list of zero or more events\n\
10085 to be used as input. If it wants to put back some events\n\
10086 to be reconsidered, separately, by the input method,\n\
10087 it can add them to the beginning of `unread-command-events'.\n\
10089 The input method function can find in `input-method-previous-method'\n\
10090 the previous echo area message.\n\
10092 The input method function should refer to the variables\n\
10093 `input-method-use-echo-area' and `input-method-exit-on-first-char'\n\
10094 for guidance on what to do.");
10095 Vinput_method_function = Qnil;
10097 DEFVAR_LISP ("input-method-previous-message",
10098 &Vinput_method_previous_message,
10099 "When `input-method-function' is called, hold the previous echo area message.\n\
10100 This variable exists because `read-event' clears the echo area\n\
10101 before running the input method. It is nil if there was no message.");
10102 Vinput_method_previous_message = Qnil;
10104 DEFVAR_LISP ("show-help-function", &Vshow_help_function,
10105 "If non-nil, the function that implements the display of help.\n\
10106 It's called with one argument, the help string to display.");
10107 Vshow_help_function = Qnil;
10110 void
10111 keys_of_keyboard ()
10113 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
10114 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
10115 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
10116 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
10117 initial_define_key (meta_map, 'x', "execute-extended-command");
10119 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
10120 "handle-delete-frame");
10121 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
10122 "ignore-event");
10123 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
10124 "ignore-event");