(bahai-holidays): Re-order.
[emacs.git] / src / keyboard.c
blob54a626268a268953e4b953d008fa693437b1d960
1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994, 1995,
3 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 #include <config.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include "lisp.h"
27 #include "termchar.h"
28 #include "termopts.h"
29 #include "frame.h"
30 #include "termhooks.h"
31 #include "macros.h"
32 #include "keyboard.h"
33 #include "window.h"
34 #include "commands.h"
35 #include "buffer.h"
36 #include "character.h"
37 #include "disptab.h"
38 #include "dispextern.h"
39 #include "syntax.h"
40 #include "intervals.h"
41 #include "keymap.h"
42 #include "blockinput.h"
43 #include "puresize.h"
44 #include "systime.h"
45 #include "atimer.h"
46 #include <setjmp.h>
47 #include <errno.h>
49 #ifdef HAVE_GTK_AND_PTHREAD
50 #include <pthread.h>
51 #endif
52 #ifdef MSDOS
53 #include "msdos.h"
54 #include <time.h>
55 #else /* not MSDOS */
56 #ifndef VMS
57 #include <sys/ioctl.h>
58 #endif
59 #endif /* not MSDOS */
61 #include "syssignal.h"
63 #include <sys/types.h>
64 #ifdef HAVE_UNISTD_H
65 #include <unistd.h>
66 #endif
68 #ifdef HAVE_FCNTL_H
69 #include <fcntl.h>
70 #endif
72 /* This is to get the definitions of the XK_ symbols. */
73 #ifdef HAVE_X_WINDOWS
74 #include "xterm.h"
75 #endif
77 #ifdef HAVE_NTGUI
78 #include "w32term.h"
79 #endif /* HAVE_NTGUI */
81 #ifdef MAC_OS
82 #include "macterm.h"
83 #endif
85 #ifndef USE_CRT_DLL
86 extern int errno;
87 #endif
89 /* Variables for blockinput.h: */
91 /* Non-zero if interrupt input is blocked right now. */
92 volatile int interrupt_input_blocked;
94 /* Nonzero means an input interrupt has arrived
95 during the current critical section. */
96 int interrupt_input_pending;
98 #define KBD_BUFFER_SIZE 4096
100 #ifdef MULTI_KBOARD
101 KBOARD *initial_kboard;
102 KBOARD *current_kboard;
103 KBOARD *all_kboards;
104 int single_kboard;
105 #else
106 KBOARD the_only_kboard;
107 #endif
109 /* Non-nil disable property on a command means
110 do not execute it; call disabled-command-function's value instead. */
111 Lisp_Object Qdisabled, Qdisabled_command_function;
113 #define NUM_RECENT_KEYS (300)
114 int recent_keys_index; /* Index for storing next element into recent_keys */
115 int total_keys; /* Total number of elements stored into recent_keys */
116 Lisp_Object recent_keys; /* Vector holds the last NUM_RECENT_KEYS keystrokes */
118 /* Vector holding the key sequence that invoked the current command.
119 It is reused for each command, and it may be longer than the current
120 sequence; this_command_key_count indicates how many elements
121 actually mean something.
122 It's easier to staticpro a single Lisp_Object than an array. */
123 Lisp_Object this_command_keys;
124 int this_command_key_count;
126 /* 1 after calling Freset_this_command_lengths.
127 Usually it is 0. */
128 int this_command_key_count_reset;
130 /* This vector is used as a buffer to record the events that were actually read
131 by read_key_sequence. */
132 Lisp_Object raw_keybuf;
133 int raw_keybuf_count;
135 /* Non-nil if the present key sequence was obtained by shift translation. */
136 Lisp_Object Vthis_command_keys_shift_translated;
138 #define GROW_RAW_KEYBUF \
139 if (raw_keybuf_count == XVECTOR (raw_keybuf)->size) \
140 raw_keybuf = larger_vector (raw_keybuf, raw_keybuf_count * 2, Qnil) \
142 /* Number of elements of this_command_keys
143 that precede this key sequence. */
144 int this_single_command_key_start;
146 /* Record values of this_command_key_count and echo_length ()
147 before this command was read. */
148 static int before_command_key_count;
149 static int before_command_echo_length;
151 extern int minbuf_level;
153 extern int message_enable_multibyte;
155 /* If non-nil, the function that implements the display of help.
156 It's called with one argument, the help string to display. */
158 Lisp_Object Vshow_help_function;
160 /* If a string, the message displayed before displaying a help-echo
161 in the echo area. */
163 Lisp_Object Vpre_help_message;
165 /* Nonzero means do menu prompting. */
167 static int menu_prompting;
169 /* Character to see next line of menu prompt. */
171 static Lisp_Object menu_prompt_more_char;
173 /* For longjmp to where kbd input is being done. */
175 static jmp_buf getcjmp;
177 /* True while doing kbd input. */
178 int waiting_for_input;
180 /* True while displaying for echoing. Delays C-g throwing. */
182 int echoing;
184 /* Non-null means we can start echoing at the next input pause even
185 though there is something in the echo area. */
187 static struct kboard *ok_to_echo_at_next_pause;
189 /* The kboard last echoing, or null for none. Reset to 0 in
190 cancel_echoing. If non-null, and a current echo area message
191 exists, and echo_message_buffer is eq to the current message
192 buffer, we know that the message comes from echo_kboard. */
194 struct kboard *echo_kboard;
196 /* The buffer used for echoing. Set in echo_now, reset in
197 cancel_echoing. */
199 Lisp_Object echo_message_buffer;
201 /* Nonzero means disregard local maps for the menu bar. */
202 static int inhibit_local_menu_bar_menus;
204 /* Nonzero means C-g should cause immediate error-signal. */
205 int immediate_quit;
207 /* The user's hook function for outputting an error message. */
208 Lisp_Object Vcommand_error_function;
210 /* The user's ERASE setting. */
211 Lisp_Object Vtty_erase_char;
213 /* Character to recognize as the help char. */
214 Lisp_Object Vhelp_char;
216 /* List of other event types to recognize as meaning "help". */
217 Lisp_Object Vhelp_event_list;
219 /* Form to execute when help char is typed. */
220 Lisp_Object Vhelp_form;
222 /* Command to run when the help character follows a prefix key. */
223 Lisp_Object Vprefix_help_command;
225 /* List of items that should move to the end of the menu bar. */
226 Lisp_Object Vmenu_bar_final_items;
228 /* Non-nil means show the equivalent key-binding for
229 any M-x command that has one.
230 The value can be a length of time to show the message for.
231 If the value is non-nil and not a number, we wait 2 seconds. */
232 Lisp_Object Vsuggest_key_bindings;
234 /* How long to display an echo-area message when the minibuffer is active.
235 If the value is not a number, such messages don't time out. */
236 Lisp_Object Vminibuffer_message_timeout;
238 /* Character that causes a quit. Normally C-g.
240 If we are running on an ordinary terminal, this must be an ordinary
241 ASCII char, since we want to make it our interrupt character.
243 If we are not running on an ordinary terminal, it still needs to be
244 an ordinary ASCII char. This character needs to be recognized in
245 the input interrupt handler. At this point, the keystroke is
246 represented as a struct input_event, while the desired quit
247 character is specified as a lispy event. The mapping from struct
248 input_events to lispy events cannot run in an interrupt handler,
249 and the reverse mapping is difficult for anything but ASCII
250 keystrokes.
252 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
253 ASCII character. */
254 int quit_char;
256 extern Lisp_Object current_global_map;
257 extern int minibuf_level;
259 /* If non-nil, this is a map that overrides all other local maps. */
260 Lisp_Object Voverriding_local_map;
262 /* If non-nil, Voverriding_local_map applies to the menu bar. */
263 Lisp_Object Voverriding_local_map_menu_flag;
265 /* Keymap that defines special misc events that should
266 be processed immediately at a low level. */
267 Lisp_Object Vspecial_event_map;
269 /* Current depth in recursive edits. */
270 int command_loop_level;
272 /* Total number of times command_loop has read a key sequence. */
273 EMACS_INT num_input_keys;
275 /* Last input character read as a command. */
276 Lisp_Object last_command_char;
278 /* Last input character read as a command, not counting menus
279 reached by the mouse. */
280 Lisp_Object last_nonmenu_event;
282 /* Last input character read for any purpose. */
283 Lisp_Object last_input_char;
285 /* If not Qnil, a list of objects to be read as subsequent command input. */
286 Lisp_Object Vunread_command_events;
288 /* If not Qnil, a list of objects to be read as subsequent command input
289 including input method processing. */
290 Lisp_Object Vunread_input_method_events;
292 /* If not Qnil, a list of objects to be read as subsequent command input
293 but NOT including input method processing. */
294 Lisp_Object Vunread_post_input_method_events;
296 /* If not -1, an event to be read as subsequent command input. */
297 EMACS_INT unread_command_char;
299 /* If not Qnil, this is a switch-frame event which we decided to put
300 off until the end of a key sequence. This should be read as the
301 next command input, after any unread_command_events.
303 read_key_sequence uses this to delay switch-frame events until the
304 end of the key sequence; Fread_char uses it to put off switch-frame
305 events until a non-ASCII event is acceptable as input. */
306 Lisp_Object unread_switch_frame;
308 /* A mask of extra modifier bits to put into every keyboard char. */
309 EMACS_INT extra_keyboard_modifiers;
311 /* Char to use as prefix when a meta character is typed in.
312 This is bound on entry to minibuffer in case ESC is changed there. */
314 Lisp_Object meta_prefix_char;
316 /* Last size recorded for a current buffer which is not a minibuffer. */
317 static int last_non_minibuf_size;
319 /* Number of idle seconds before an auto-save and garbage collection. */
320 static Lisp_Object Vauto_save_timeout;
322 /* Total number of times read_char has returned. */
323 int num_input_events;
325 /* Total number of times read_char has returned, outside of macros. */
326 EMACS_INT num_nonmacro_input_events;
328 /* Auto-save automatically when this many characters have been typed
329 since the last time. */
331 static EMACS_INT auto_save_interval;
333 /* Value of num_nonmacro_input_events as of last auto save. */
335 int last_auto_save;
337 /* The command being executed by the command loop.
338 Commands may set this, and the value set will be copied into
339 current_kboard->Vlast_command instead of the actual command. */
340 Lisp_Object Vthis_command;
342 /* This is like Vthis_command, except that commands never set it. */
343 Lisp_Object real_this_command;
345 /* If the lookup of the command returns a binding, the original
346 command is stored in this-original-command. It is nil otherwise. */
347 Lisp_Object Vthis_original_command;
349 /* The value of point when the last command was started. */
350 int last_point_position;
352 /* The buffer that was current when the last command was started. */
353 Lisp_Object last_point_position_buffer;
355 /* The window that was selected when the last command was started. */
356 Lisp_Object last_point_position_window;
358 /* The frame in which the last input event occurred, or Qmacro if the
359 last event came from a macro. We use this to determine when to
360 generate switch-frame events. This may be cleared by functions
361 like Fselect_frame, to make sure that a switch-frame event is
362 generated by the next character. */
363 Lisp_Object internal_last_event_frame;
365 /* A user-visible version of the above, intended to allow users to
366 figure out where the last event came from, if the event doesn't
367 carry that information itself (i.e. if it was a character). */
368 Lisp_Object Vlast_event_frame;
370 /* The timestamp of the last input event we received from the X server.
371 X Windows wants this for selection ownership. */
372 unsigned long last_event_timestamp;
374 Lisp_Object Qself_insert_command;
375 Lisp_Object Qforward_char;
376 Lisp_Object Qbackward_char;
377 Lisp_Object Qundefined;
378 Lisp_Object Qtimer_event_handler;
380 /* read_key_sequence stores here the command definition of the
381 key sequence that it reads. */
382 Lisp_Object read_key_sequence_cmd;
384 /* Echo unfinished commands after this many seconds of pause. */
385 Lisp_Object Vecho_keystrokes;
387 /* Form to evaluate (if non-nil) when Emacs is started. */
388 Lisp_Object Vtop_level;
390 /* If non-nil, this implements the current input method. */
391 Lisp_Object Vinput_method_function;
392 Lisp_Object Qinput_method_function;
394 /* When we call Vinput_method_function,
395 this holds the echo area message that was just erased. */
396 Lisp_Object Vinput_method_previous_message;
398 /* Non-nil means deactivate the mark at end of this command. */
399 Lisp_Object Vdeactivate_mark;
401 /* Menu bar specified in Lucid Emacs fashion. */
403 Lisp_Object Vlucid_menu_bar_dirty_flag;
404 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
406 Lisp_Object Qecho_area_clear_hook;
408 /* Hooks to run before and after each command. */
409 Lisp_Object Qpre_command_hook, Vpre_command_hook;
410 Lisp_Object Qpost_command_hook, Vpost_command_hook;
411 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
413 /* Parent keymap of terminal-local function-key-map instances. */
414 Lisp_Object Vfunction_key_map;
416 /* Keymap of key translations that can override keymaps. */
417 Lisp_Object Vkey_translation_map;
419 /* List of deferred actions to be performed at a later time.
420 The precise format isn't relevant here; we just check whether it is nil. */
421 Lisp_Object Vdeferred_action_list;
423 /* Function to call to handle deferred actions, when there are any. */
424 Lisp_Object Vdeferred_action_function;
425 Lisp_Object Qdeferred_action_function;
427 Lisp_Object Qinput_method_exit_on_first_char;
428 Lisp_Object Qinput_method_use_echo_area;
430 /* File in which we write all commands we read. */
431 FILE *dribble;
433 /* Nonzero if input is available. */
434 int input_pending;
436 extern char *pending_malloc_warning;
438 /* Circular buffer for pre-read keyboard input. */
440 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
442 /* Pointer to next available character in kbd_buffer.
443 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
444 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the
445 next available char is in kbd_buffer[0]. */
446 static struct input_event *kbd_fetch_ptr;
448 /* Pointer to next place to store character in kbd_buffer. This
449 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
450 character should go in kbd_buffer[0]. */
451 static struct input_event * volatile kbd_store_ptr;
453 /* The above pair of variables forms a "queue empty" flag. When we
454 enqueue a non-hook event, we increment kbd_store_ptr. When we
455 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
456 there is input available if the two pointers are not equal.
458 Why not just have a flag set and cleared by the enqueuing and
459 dequeuing functions? Such a flag could be screwed up by interrupts
460 at inopportune times. */
462 /* If this flag is non-nil, we check mouse_moved to see when the
463 mouse moves, and motion events will appear in the input stream.
464 Otherwise, mouse motion is ignored. */
465 Lisp_Object do_mouse_tracking;
467 /* Symbols to head events. */
468 Lisp_Object Qmouse_movement;
469 Lisp_Object Qscroll_bar_movement;
470 Lisp_Object Qswitch_frame;
471 Lisp_Object Qdelete_frame;
472 Lisp_Object Qiconify_frame;
473 Lisp_Object Qmake_frame_visible;
474 Lisp_Object Qselect_window;
475 Lisp_Object Qhelp_echo;
477 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
478 Lisp_Object Qmouse_fixup_help_message;
479 #endif
481 /* Symbols to denote kinds of events. */
482 Lisp_Object Qfunction_key;
483 Lisp_Object Qmouse_click;
484 #if defined (WINDOWSNT) || defined (MAC_OS)
485 Lisp_Object Qlanguage_change;
486 #endif
487 Lisp_Object Qdrag_n_drop;
488 Lisp_Object Qsave_session;
489 #ifdef MAC_OS
490 Lisp_Object Qmac_apple_event;
491 #endif
492 #ifdef HAVE_DBUS
493 Lisp_Object Qdbus_event;
494 #endif
495 /* Lisp_Object Qmouse_movement; - also an event header */
497 /* Properties of event headers. */
498 Lisp_Object Qevent_kind;
499 Lisp_Object Qevent_symbol_elements;
501 /* menu item parts */
502 Lisp_Object Qmenu_alias;
503 Lisp_Object Qmenu_enable;
504 Lisp_Object QCenable, QCvisible, QChelp, QCfilter, QCkeys, QCkey_sequence;
505 Lisp_Object QCbutton, QCtoggle, QCradio;
506 extern Lisp_Object Vdefine_key_rebound_commands;
507 extern Lisp_Object Qmenu_item;
509 /* An event header symbol HEAD may have a property named
510 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
511 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
512 mask of modifiers applied to it. If present, this is used to help
513 speed up parse_modifiers. */
514 Lisp_Object Qevent_symbol_element_mask;
516 /* An unmodified event header BASE may have a property named
517 Qmodifier_cache, which is an alist mapping modifier masks onto
518 modified versions of BASE. If present, this helps speed up
519 apply_modifiers. */
520 Lisp_Object Qmodifier_cache;
522 /* Symbols to use for parts of windows. */
523 Lisp_Object Qmode_line;
524 Lisp_Object Qvertical_line;
525 Lisp_Object Qvertical_scroll_bar;
526 Lisp_Object Qmenu_bar;
527 extern Lisp_Object Qleft_margin, Qright_margin;
528 extern Lisp_Object Qleft_fringe, Qright_fringe;
529 extern Lisp_Object QCmap;
531 Lisp_Object recursive_edit_unwind (), command_loop ();
532 Lisp_Object Fthis_command_keys ();
533 Lisp_Object Qextended_command_history;
534 EMACS_TIME timer_check ();
536 extern Lisp_Object Vhistory_length, Vtranslation_table_for_input;
538 extern char *x_get_keysym_name ();
540 static void record_menu_key ();
541 static int echo_length ();
543 Lisp_Object Qpolling_period;
545 /* List of absolute timers. Appears in order of next scheduled event. */
546 Lisp_Object Vtimer_list;
548 /* List of idle time timers. Appears in order of next scheduled event. */
549 Lisp_Object Vtimer_idle_list;
551 /* Incremented whenever a timer is run. */
552 int timers_run;
554 extern Lisp_Object Vprint_level, Vprint_length;
556 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
557 happens. */
558 EMACS_TIME *input_available_clear_time;
560 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
561 Default is 1 if INTERRUPT_INPUT is defined. */
562 int interrupt_input;
564 /* Nonzero while interrupts are temporarily deferred during redisplay. */
565 int interrupts_deferred;
567 /* Allow m- file to inhibit use of FIONREAD. */
568 #ifdef BROKEN_FIONREAD
569 #undef FIONREAD
570 #endif
572 /* We are unable to use interrupts if FIONREAD is not available,
573 so flush SIGIO so we won't try. */
574 #if !defined (FIONREAD)
575 #ifdef SIGIO
576 #undef SIGIO
577 #endif
578 #endif
580 /* If we support a window system, turn on the code to poll periodically
581 to detect C-g. It isn't actually used when doing interrupt input. */
582 #if defined(HAVE_WINDOW_SYSTEM) && !defined(USE_ASYNC_EVENTS)
583 #define POLL_FOR_INPUT
584 #endif
586 /* After a command is executed, if point is moved into a region that
587 has specific properties (e.g. composition, display), we adjust
588 point to the boundary of the region. But, if a command sets this
589 variable to non-nil, we suppress this point adjustment. This
590 variable is set to nil before reading a command. */
592 Lisp_Object Vdisable_point_adjustment;
594 /* If non-nil, always disable point adjustment. */
596 Lisp_Object Vglobal_disable_point_adjustment;
598 /* The time when Emacs started being idle. */
600 static EMACS_TIME timer_idleness_start_time;
602 /* After Emacs stops being idle, this saves the last value
603 of timer_idleness_start_time from when it was idle. */
605 static EMACS_TIME timer_last_idleness_start_time;
607 /* If non-nil, events produced by disabled menu items and tool-bar
608 buttons are not ignored. Help functions bind this to allow help on
609 those items and buttons. */
610 Lisp_Object Venable_disabled_menus_and_buttons;
613 /* Global variable declarations. */
615 /* Flags for readable_events. */
616 #define READABLE_EVENTS_DO_TIMERS_NOW (1 << 0)
617 #define READABLE_EVENTS_FILTER_EVENTS (1 << 1)
618 #define READABLE_EVENTS_IGNORE_SQUEEZABLES (1 << 2)
620 /* Function for init_keyboard to call with no args (if nonzero). */
621 void (*keyboard_init_hook) ();
623 static int read_avail_input P_ ((int));
624 static void get_input_pending P_ ((int *, int));
625 static int readable_events P_ ((int));
626 static Lisp_Object read_char_x_menu_prompt P_ ((int, Lisp_Object *,
627 Lisp_Object, int *));
628 static Lisp_Object read_char_x_menu_prompt ();
629 static Lisp_Object read_char_minibuf_menu_prompt P_ ((int, int,
630 Lisp_Object *));
631 static Lisp_Object make_lispy_event P_ ((struct input_event *));
632 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
633 static Lisp_Object make_lispy_movement P_ ((struct frame *, Lisp_Object,
634 enum scroll_bar_part,
635 Lisp_Object, Lisp_Object,
636 unsigned long));
637 #endif
638 static Lisp_Object modify_event_symbol P_ ((int, unsigned, Lisp_Object,
639 Lisp_Object, char **,
640 Lisp_Object *, unsigned));
641 static Lisp_Object make_lispy_switch_frame P_ ((Lisp_Object));
642 static int parse_solitary_modifier P_ ((Lisp_Object));
643 static int parse_solitary_modifier ();
644 static void save_getcjmp P_ ((jmp_buf));
645 static void save_getcjmp ();
646 static void restore_getcjmp P_ ((jmp_buf));
647 static Lisp_Object apply_modifiers P_ ((int, Lisp_Object));
648 static void clear_event P_ ((struct input_event *));
649 #ifdef MULTI_KBOARD
650 static Lisp_Object restore_kboard_configuration P_ ((Lisp_Object));
651 #endif
652 static SIGTYPE interrupt_signal P_ ((int signalnum));
653 static void handle_interrupt P_ ((void));
654 static void timer_start_idle P_ ((void));
655 static void timer_stop_idle P_ ((void));
656 static void timer_resume_idle P_ ((void));
657 static SIGTYPE handle_user_signal P_ ((int));
658 static char *find_user_signal_name P_ ((int));
659 static int store_user_signal_events P_ ((void));
661 /* Nonzero means don't try to suspend even if the operating system seems
662 to support it. */
663 static int cannot_suspend;
665 extern Lisp_Object Qidentity, Qonly;
667 /* Install the string STR as the beginning of the string of echoing,
668 so that it serves as a prompt for the next character.
669 Also start echoing. */
671 void
672 echo_prompt (str)
673 Lisp_Object str;
675 current_kboard->echo_string = str;
676 current_kboard->echo_after_prompt = SCHARS (str);
677 echo_now ();
680 /* Add C to the echo string, if echoing is going on.
681 C can be a character, which is printed prettily ("M-C-x" and all that
682 jazz), or a symbol, whose name is printed. */
684 void
685 echo_char (c)
686 Lisp_Object c;
688 if (current_kboard->immediate_echo)
690 int size = KEY_DESCRIPTION_SIZE + 100;
691 char *buffer = (char *) alloca (size);
692 char *ptr = buffer;
693 Lisp_Object echo_string;
695 echo_string = current_kboard->echo_string;
697 /* If someone has passed us a composite event, use its head symbol. */
698 c = EVENT_HEAD (c);
700 if (INTEGERP (c))
702 ptr = push_key_description (XINT (c), ptr, 1);
704 else if (SYMBOLP (c))
706 Lisp_Object name = SYMBOL_NAME (c);
707 int nbytes = SBYTES (name);
709 if (size - (ptr - buffer) < nbytes)
711 int offset = ptr - buffer;
712 size = max (2 * size, size + nbytes);
713 buffer = (char *) alloca (size);
714 ptr = buffer + offset;
717 ptr += copy_text (SDATA (name), ptr, nbytes,
718 STRING_MULTIBYTE (name), 1);
721 if ((NILP (echo_string) || SCHARS (echo_string) == 0)
722 && help_char_p (c))
724 const char *text = " (Type ? for further options)";
725 int len = strlen (text);
727 if (size - (ptr - buffer) < len)
729 int offset = ptr - buffer;
730 size += len;
731 buffer = (char *) alloca (size);
732 ptr = buffer + offset;
735 bcopy (text, ptr, len);
736 ptr += len;
739 /* Replace a dash from echo_dash with a space, otherwise
740 add a space at the end as a separator between keys. */
741 if (STRINGP (echo_string)
742 && SCHARS (echo_string) > 1)
744 Lisp_Object last_char, prev_char, idx;
746 idx = make_number (SCHARS (echo_string) - 2);
747 prev_char = Faref (echo_string, idx);
749 idx = make_number (SCHARS (echo_string) - 1);
750 last_char = Faref (echo_string, idx);
752 /* We test PREV_CHAR to make sure this isn't the echoing
753 of a minus-sign. */
754 if (XINT (last_char) == '-' && XINT (prev_char) != ' ')
755 Faset (echo_string, idx, make_number (' '));
756 else
757 echo_string = concat2 (echo_string, build_string (" "));
759 else if (STRINGP (echo_string))
760 echo_string = concat2 (echo_string, build_string (" "));
762 current_kboard->echo_string
763 = concat2 (echo_string, make_string (buffer, ptr - buffer));
765 echo_now ();
769 /* Temporarily add a dash to the end of the echo string if it's not
770 empty, so that it serves as a mini-prompt for the very next character. */
772 void
773 echo_dash ()
775 /* Do nothing if not echoing at all. */
776 if (NILP (current_kboard->echo_string))
777 return;
779 if (!current_kboard->immediate_echo
780 && SCHARS (current_kboard->echo_string) == 0)
781 return;
783 /* Do nothing if we just printed a prompt. */
784 if (current_kboard->echo_after_prompt
785 == SCHARS (current_kboard->echo_string))
786 return;
788 /* Do nothing if we have already put a dash at the end. */
789 if (SCHARS (current_kboard->echo_string) > 1)
791 Lisp_Object last_char, prev_char, idx;
793 idx = make_number (SCHARS (current_kboard->echo_string) - 2);
794 prev_char = Faref (current_kboard->echo_string, idx);
796 idx = make_number (SCHARS (current_kboard->echo_string) - 1);
797 last_char = Faref (current_kboard->echo_string, idx);
799 if (XINT (last_char) == '-' && XINT (prev_char) != ' ')
800 return;
803 /* Put a dash at the end of the buffer temporarily,
804 but make it go away when the next character is added. */
805 current_kboard->echo_string = concat2 (current_kboard->echo_string,
806 build_string ("-"));
807 echo_now ();
810 /* Display the current echo string, and begin echoing if not already
811 doing so. */
813 void
814 echo_now ()
816 if (!current_kboard->immediate_echo)
818 int i;
819 current_kboard->immediate_echo = 1;
821 for (i = 0; i < this_command_key_count; i++)
823 Lisp_Object c;
825 /* Set before_command_echo_length to the value that would
826 have been saved before the start of this subcommand in
827 command_loop_1, if we had already been echoing then. */
828 if (i == this_single_command_key_start)
829 before_command_echo_length = echo_length ();
831 c = XVECTOR (this_command_keys)->contents[i];
832 if (! (EVENT_HAS_PARAMETERS (c)
833 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
834 echo_char (c);
837 /* Set before_command_echo_length to the value that would
838 have been saved before the start of this subcommand in
839 command_loop_1, if we had already been echoing then. */
840 if (this_command_key_count == this_single_command_key_start)
841 before_command_echo_length = echo_length ();
843 /* Put a dash at the end to invite the user to type more. */
844 echo_dash ();
847 echoing = 1;
848 message3_nolog (current_kboard->echo_string,
849 SBYTES (current_kboard->echo_string),
850 STRING_MULTIBYTE (current_kboard->echo_string));
851 echoing = 0;
853 /* Record in what buffer we echoed, and from which kboard. */
854 echo_message_buffer = echo_area_buffer[0];
855 echo_kboard = current_kboard;
857 if (waiting_for_input && !NILP (Vquit_flag))
858 quit_throw_to_read_char ();
861 /* Turn off echoing, for the start of a new command. */
863 void
864 cancel_echoing ()
866 current_kboard->immediate_echo = 0;
867 current_kboard->echo_after_prompt = -1;
868 current_kboard->echo_string = Qnil;
869 ok_to_echo_at_next_pause = NULL;
870 echo_kboard = NULL;
871 echo_message_buffer = Qnil;
874 /* Return the length of the current echo string. */
876 static int
877 echo_length ()
879 return (STRINGP (current_kboard->echo_string)
880 ? SCHARS (current_kboard->echo_string)
881 : 0);
884 /* Truncate the current echo message to its first LEN chars.
885 This and echo_char get used by read_key_sequence when the user
886 switches frames while entering a key sequence. */
888 static void
889 echo_truncate (nchars)
890 int nchars;
892 if (STRINGP (current_kboard->echo_string))
893 current_kboard->echo_string
894 = Fsubstring (current_kboard->echo_string,
895 make_number (0), make_number (nchars));
896 truncate_echo_area (nchars);
900 /* Functions for manipulating this_command_keys. */
901 static void
902 add_command_key (key)
903 Lisp_Object key;
905 #if 0 /* Not needed after we made Freset_this_command_lengths
906 do the job immediately. */
907 /* If reset-this-command-length was called recently, obey it now.
908 See the doc string of that function for an explanation of why. */
909 if (before_command_restore_flag)
911 this_command_key_count = before_command_key_count_1;
912 if (this_command_key_count < this_single_command_key_start)
913 this_single_command_key_start = this_command_key_count;
914 echo_truncate (before_command_echo_length_1);
915 before_command_restore_flag = 0;
917 #endif
919 if (this_command_key_count >= ASIZE (this_command_keys))
920 this_command_keys = larger_vector (this_command_keys,
921 2 * ASIZE (this_command_keys),
922 Qnil);
924 ASET (this_command_keys, this_command_key_count, key);
925 ++this_command_key_count;
929 Lisp_Object
930 recursive_edit_1 ()
932 int count = SPECPDL_INDEX ();
933 Lisp_Object val;
935 if (command_loop_level > 0)
937 specbind (Qstandard_output, Qt);
938 specbind (Qstandard_input, Qt);
941 #ifdef HAVE_WINDOW_SYSTEM
942 /* The command loop has started an hourglass timer, so we have to
943 cancel it here, otherwise it will fire because the recursive edit
944 can take some time. Do not check for display_hourglass_p here,
945 because it could already be nil. */
946 cancel_hourglass ();
947 #endif
949 /* This function may have been called from a debugger called from
950 within redisplay, for instance by Edebugging a function called
951 from fontification-functions. We want to allow redisplay in
952 the debugging session.
954 The recursive edit is left with a `(throw exit ...)'. The `exit'
955 tag is not caught anywhere in redisplay, i.e. when we leave the
956 recursive edit, the original redisplay leading to the recursive
957 edit will be unwound. The outcome should therefore be safe. */
958 specbind (Qinhibit_redisplay, Qnil);
959 redisplaying_p = 0;
961 val = command_loop ();
962 if (EQ (val, Qt))
963 Fsignal (Qquit, Qnil);
964 /* Handle throw from read_minibuf when using minibuffer
965 while it's active but we're in another window. */
966 if (STRINGP (val))
967 xsignal1 (Qerror, val);
969 return unbind_to (count, Qnil);
972 /* When an auto-save happens, record the "time", and don't do again soon. */
974 void
975 record_auto_save ()
977 last_auto_save = num_nonmacro_input_events;
980 /* Make an auto save happen as soon as possible at command level. */
982 void
983 force_auto_save_soon ()
985 last_auto_save = - auto_save_interval - 1;
987 record_asynch_buffer_change ();
990 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
991 doc: /* Invoke the editor command loop recursively.
992 To get out of the recursive edit, a command can do `(throw 'exit nil)';
993 that tells this function to return.
994 Alternatively, `(throw 'exit t)' makes this function signal an error.
995 This function is called by the editor initialization to begin editing. */)
998 int count = SPECPDL_INDEX ();
999 Lisp_Object buffer;
1001 /* If we enter while input is blocked, don't lock up here.
1002 This may happen through the debugger during redisplay. */
1003 if (INPUT_BLOCKED_P)
1004 return Qnil;
1006 command_loop_level++;
1007 update_mode_lines = 1;
1009 if (command_loop_level
1010 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
1011 buffer = Fcurrent_buffer ();
1012 else
1013 buffer = Qnil;
1015 /* If we leave recursive_edit_1 below with a `throw' for instance,
1016 like it is done in the splash screen display, we have to
1017 make sure that we restore single_kboard as command_loop_1
1018 would have done if it were left normally. */
1019 if (command_loop_level > 0)
1020 temporarily_switch_to_single_kboard (SELECTED_FRAME ());
1021 record_unwind_protect (recursive_edit_unwind, buffer);
1023 recursive_edit_1 ();
1024 return unbind_to (count, Qnil);
1027 Lisp_Object
1028 recursive_edit_unwind (buffer)
1029 Lisp_Object buffer;
1031 if (BUFFERP (buffer))
1032 Fset_buffer (buffer);
1034 command_loop_level--;
1035 update_mode_lines = 1;
1036 return Qnil;
1040 #if 0 /* These two functions are now replaced with
1041 temporarily_switch_to_single_kboard. */
1042 static void
1043 any_kboard_state ()
1045 #ifdef MULTI_KBOARD
1046 #if 0 /* Theory: if there's anything in Vunread_command_events,
1047 it will right away be read by read_key_sequence,
1048 and then if we do switch KBOARDS, it will go into the side
1049 queue then. So we don't need to do anything special here -- rms. */
1050 if (CONSP (Vunread_command_events))
1052 current_kboard->kbd_queue
1053 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
1054 current_kboard->kbd_queue_has_data = 1;
1056 Vunread_command_events = Qnil;
1057 #endif
1058 single_kboard = 0;
1059 #endif
1062 /* Switch to the single-kboard state, making current_kboard
1063 the only KBOARD from which further input is accepted. */
1065 void
1066 single_kboard_state ()
1068 #ifdef MULTI_KBOARD
1069 single_kboard = 1;
1070 #endif
1072 #endif
1074 /* If we're in single_kboard state for kboard KBOARD,
1075 get out of it. */
1077 void
1078 not_single_kboard_state (kboard)
1079 KBOARD *kboard;
1081 #ifdef MULTI_KBOARD
1082 if (kboard == current_kboard)
1083 single_kboard = 0;
1084 #endif
1087 /* Maintain a stack of kboards, so other parts of Emacs
1088 can switch temporarily to the kboard of a given frame
1089 and then revert to the previous status. */
1091 struct kboard_stack
1093 KBOARD *kboard;
1094 struct kboard_stack *next;
1097 static struct kboard_stack *kboard_stack;
1099 void
1100 push_kboard (k)
1101 struct kboard *k;
1103 #ifdef MULTI_KBOARD
1104 struct kboard_stack *p
1105 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
1107 p->next = kboard_stack;
1108 p->kboard = current_kboard;
1109 kboard_stack = p;
1111 current_kboard = k;
1112 #endif
1115 void
1116 pop_kboard ()
1118 #ifdef MULTI_KBOARD
1119 struct terminal *t;
1120 struct kboard_stack *p = kboard_stack;
1121 int found = 0;
1122 for (t = terminal_list; t; t = t->next_terminal)
1124 if (t->kboard == p->kboard)
1126 current_kboard = p->kboard;
1127 found = 1;
1128 break;
1131 if (!found)
1133 /* The terminal we remembered has been deleted. */
1134 current_kboard = FRAME_KBOARD (SELECTED_FRAME ());
1135 single_kboard = 0;
1137 kboard_stack = p->next;
1138 xfree (p);
1139 #endif
1142 /* Switch to single_kboard mode, making current_kboard the only KBOARD
1143 from which further input is accepted. If F is non-nil, set its
1144 KBOARD as the current keyboard.
1146 This function uses record_unwind_protect to return to the previous
1147 state later.
1149 If Emacs is already in single_kboard mode, and F's keyboard is
1150 locked, then this function will throw an errow. */
1152 void
1153 temporarily_switch_to_single_kboard (f)
1154 struct frame *f;
1156 #ifdef MULTI_KBOARD
1157 int was_locked = single_kboard;
1158 if (was_locked)
1160 if (f != NULL && FRAME_KBOARD (f) != current_kboard)
1161 /* We can not switch keyboards while in single_kboard mode.
1162 In rare cases, Lisp code may call `recursive-edit' (or
1163 `read-minibuffer' or `y-or-n-p') after it switched to a
1164 locked frame. For example, this is likely to happen
1165 when server.el connects to a new terminal while Emacs is in
1166 single_kboard mode. It is best to throw an error instead
1167 of presenting the user with a frozen screen. */
1168 error ("Terminal %d is locked, cannot read from it",
1169 FRAME_TERMINAL (f)->id);
1170 else
1171 /* This call is unnecessary, but helps
1172 `restore_kboard_configuration' discover if somebody changed
1173 `current_kboard' behind our back. */
1174 push_kboard (current_kboard);
1176 else if (f != NULL)
1177 current_kboard = FRAME_KBOARD (f);
1178 single_kboard = 1;
1179 record_unwind_protect (restore_kboard_configuration,
1180 (was_locked ? Qt : Qnil));
1181 #endif
1184 #if 0 /* This function is not needed anymore. */
1185 void
1186 record_single_kboard_state ()
1188 if (single_kboard)
1189 push_kboard (current_kboard);
1190 record_unwind_protect (restore_kboard_configuration,
1191 (single_kboard ? Qt : Qnil));
1193 #endif
1195 #ifdef MULTI_KBOARD
1196 static Lisp_Object
1197 restore_kboard_configuration (was_locked)
1198 Lisp_Object was_locked;
1200 if (NILP (was_locked))
1201 single_kboard = 0;
1202 else
1204 struct kboard *prev = current_kboard;
1205 single_kboard = 1;
1206 pop_kboard ();
1207 /* The pop should not change the kboard. */
1208 if (single_kboard && current_kboard != prev)
1209 abort ();
1211 return Qnil;
1213 #endif
1216 /* Handle errors that are not handled at inner levels
1217 by printing an error message and returning to the editor command loop. */
1219 Lisp_Object
1220 cmd_error (data)
1221 Lisp_Object data;
1223 Lisp_Object old_level, old_length;
1224 char macroerror[50];
1226 #ifdef HAVE_WINDOW_SYSTEM
1227 if (display_hourglass_p)
1228 cancel_hourglass ();
1229 #endif
1231 if (!NILP (executing_kbd_macro))
1233 if (executing_kbd_macro_iterations == 1)
1234 sprintf (macroerror, "After 1 kbd macro iteration: ");
1235 else
1236 sprintf (macroerror, "After %d kbd macro iterations: ",
1237 executing_kbd_macro_iterations);
1239 else
1240 *macroerror = 0;
1242 Vstandard_output = Qt;
1243 Vstandard_input = Qt;
1244 Vexecuting_kbd_macro = Qnil;
1245 executing_kbd_macro = Qnil;
1246 current_kboard->Vprefix_arg = Qnil;
1247 current_kboard->Vlast_prefix_arg = Qnil;
1248 cancel_echoing ();
1250 /* Avoid unquittable loop if data contains a circular list. */
1251 old_level = Vprint_level;
1252 old_length = Vprint_length;
1253 XSETFASTINT (Vprint_level, 10);
1254 XSETFASTINT (Vprint_length, 10);
1255 cmd_error_internal (data, macroerror);
1256 Vprint_level = old_level;
1257 Vprint_length = old_length;
1259 Vquit_flag = Qnil;
1261 Vinhibit_quit = Qnil;
1262 #if 0 /* This shouldn't be necessary anymore. --lorentey */
1263 #ifdef MULTI_KBOARD
1264 if (command_loop_level == 0 && minibuf_level == 0)
1265 any_kboard_state ();
1266 #endif
1267 #endif
1269 return make_number (0);
1272 /* Take actions on handling an error. DATA is the data that describes
1273 the error.
1275 CONTEXT is a C-string containing ASCII characters only which
1276 describes the context in which the error happened. If we need to
1277 generalize CONTEXT to allow multibyte characters, make it a Lisp
1278 string. */
1280 void
1281 cmd_error_internal (data, context)
1282 Lisp_Object data;
1283 char *context;
1285 struct frame *sf = SELECTED_FRAME ();
1287 /* The immediate context is not interesting for Quits,
1288 since they are asyncronous. */
1289 if (EQ (XCAR (data), Qquit))
1290 Vsignaling_function = Qnil;
1292 Vquit_flag = Qnil;
1293 Vinhibit_quit = Qt;
1295 /* Use user's specified output function if any. */
1296 if (!NILP (Vcommand_error_function))
1297 call3 (Vcommand_error_function, data,
1298 context ? build_string (context) : empty_unibyte_string,
1299 Vsignaling_function);
1300 /* If the window system or terminal frame hasn't been initialized
1301 yet, or we're not interactive, write the message to stderr and exit. */
1302 else if (!sf->glyphs_initialized_p
1303 || FRAME_INITIAL_P (sf)
1304 || noninteractive)
1306 print_error_message (data, Qexternal_debugging_output,
1307 context, Vsignaling_function);
1308 Fterpri (Qexternal_debugging_output);
1309 Fkill_emacs (make_number (-1));
1311 else
1313 clear_message (1, 0);
1314 Fdiscard_input ();
1315 message_log_maybe_newline ();
1316 bitch_at_user ();
1318 print_error_message (data, Qt, context, Vsignaling_function);
1321 Vsignaling_function = Qnil;
1324 Lisp_Object command_loop_1 ();
1325 Lisp_Object command_loop_2 ();
1326 Lisp_Object top_level_1 ();
1328 /* Entry to editor-command-loop.
1329 This level has the catches for exiting/returning to editor command loop.
1330 It returns nil to exit recursive edit, t to abort it. */
1332 Lisp_Object
1333 command_loop ()
1335 if (command_loop_level > 0 || minibuf_level > 0)
1337 Lisp_Object val;
1338 val = internal_catch (Qexit, command_loop_2, Qnil);
1339 executing_kbd_macro = Qnil;
1340 return val;
1342 else
1343 while (1)
1345 internal_catch (Qtop_level, top_level_1, Qnil);
1346 #if 0 /* This shouldn't be necessary anymore. --lorentey */
1347 /* Reset single_kboard in case top-level set it while
1348 evaluating an -f option, or we are stuck there for some
1349 other reason. */
1350 any_kboard_state ();
1351 #endif
1352 internal_catch (Qtop_level, command_loop_2, Qnil);
1353 executing_kbd_macro = Qnil;
1355 /* End of file in -batch run causes exit here. */
1356 if (noninteractive)
1357 Fkill_emacs (Qt);
1361 /* Here we catch errors in execution of commands within the
1362 editing loop, and reenter the editing loop.
1363 When there is an error, cmd_error runs and returns a non-nil
1364 value to us. A value of nil means that command_loop_1 itself
1365 returned due to end of file (or end of kbd macro). */
1367 Lisp_Object
1368 command_loop_2 ()
1370 register Lisp_Object val;
1373 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
1374 while (!NILP (val));
1376 return Qnil;
1379 Lisp_Object
1380 top_level_2 ()
1382 return Feval (Vtop_level);
1385 Lisp_Object
1386 top_level_1 ()
1388 /* On entry to the outer level, run the startup file */
1389 if (!NILP (Vtop_level))
1390 internal_condition_case (top_level_2, Qerror, cmd_error);
1391 else if (!NILP (Vpurify_flag))
1392 message ("Bare impure Emacs (standard Lisp code not loaded)");
1393 else
1394 message ("Bare Emacs (standard Lisp code not loaded)");
1395 return Qnil;
1398 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1399 doc: /* Exit all recursive editing levels. */)
1402 #ifdef HAVE_WINDOW_SYSTEM
1403 if (display_hourglass_p)
1404 cancel_hourglass ();
1405 #endif
1407 /* Unblock input if we enter with input blocked. This may happen if
1408 redisplay traps e.g. during tool-bar update with input blocked. */
1409 while (INPUT_BLOCKED_P)
1410 UNBLOCK_INPUT;
1412 return Fthrow (Qtop_level, Qnil);
1415 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1416 doc: /* Exit from the innermost recursive edit or minibuffer. */)
1419 if (command_loop_level > 0 || minibuf_level > 0)
1420 Fthrow (Qexit, Qnil);
1422 error ("No recursive edit is in progress");
1423 return Qnil;
1426 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1427 doc: /* Abort the command that requested this recursive edit or minibuffer input. */)
1430 if (command_loop_level > 0 || minibuf_level > 0)
1431 Fthrow (Qexit, Qt);
1433 error ("No recursive edit is in progress");
1434 return Qnil;
1437 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
1439 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
1440 of this function. */
1442 static Lisp_Object
1443 tracking_off (old_value)
1444 Lisp_Object old_value;
1446 do_mouse_tracking = old_value;
1447 if (NILP (old_value))
1449 /* Redisplay may have been preempted because there was input
1450 available, and it assumes it will be called again after the
1451 input has been processed. If the only input available was
1452 the sort that we have just disabled, then we need to call
1453 redisplay. */
1454 if (!readable_events (READABLE_EVENTS_DO_TIMERS_NOW))
1456 redisplay_preserve_echo_area (6);
1457 get_input_pending (&input_pending,
1458 READABLE_EVENTS_DO_TIMERS_NOW);
1461 return Qnil;
1464 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
1465 doc: /* Evaluate BODY with mouse movement events enabled.
1466 Within a `track-mouse' form, mouse motion generates input events that
1467 you can read with `read-event'.
1468 Normally, mouse motion is ignored.
1469 usage: (track-mouse BODY...) */)
1470 (args)
1471 Lisp_Object args;
1473 int count = SPECPDL_INDEX ();
1474 Lisp_Object val;
1476 record_unwind_protect (tracking_off, do_mouse_tracking);
1478 do_mouse_tracking = Qt;
1480 val = Fprogn (args);
1481 return unbind_to (count, val);
1484 /* If mouse has moved on some frame, return one of those frames.
1486 Return 0 otherwise.
1488 If ignore_mouse_drag_p is non-zero, ignore (implicit) mouse movement
1489 after resizing the tool-bar window. */
1491 int ignore_mouse_drag_p;
1493 static FRAME_PTR
1494 some_mouse_moved ()
1496 Lisp_Object tail, frame;
1498 if (ignore_mouse_drag_p)
1500 /* ignore_mouse_drag_p = 0; */
1501 return 0;
1504 FOR_EACH_FRAME (tail, frame)
1506 if (XFRAME (frame)->mouse_moved)
1507 return XFRAME (frame);
1510 return 0;
1513 #endif /* HAVE_MOUSE || HAVE_GPM */
1515 /* This is the actual command reading loop,
1516 sans error-handling encapsulation. */
1518 static int read_key_sequence P_ ((Lisp_Object *, int, Lisp_Object,
1519 int, int, int));
1520 void safe_run_hooks P_ ((Lisp_Object));
1521 static void adjust_point_for_property P_ ((int, int));
1523 /* Cancel hourglass from protect_unwind.
1524 ARG is not used. */
1525 #ifdef HAVE_WINDOW_SYSTEM
1526 static Lisp_Object
1527 cancel_hourglass_unwind (arg)
1528 Lisp_Object arg;
1530 cancel_hourglass ();
1531 return Qnil;
1533 #endif
1535 Lisp_Object
1536 command_loop_1 ()
1538 Lisp_Object cmd;
1539 int lose;
1540 int nonundocount;
1541 Lisp_Object keybuf[30];
1542 int i;
1543 int prev_modiff = 0;
1544 struct buffer *prev_buffer = NULL;
1545 #if 0 /* This shouldn't be necessary anymore. --lorentey */
1546 #ifdef MULTI_KBOARD
1547 int was_locked = single_kboard;
1548 #endif
1549 #endif
1550 int already_adjusted = 0;
1552 current_kboard->Vprefix_arg = Qnil;
1553 current_kboard->Vlast_prefix_arg = Qnil;
1554 Vdeactivate_mark = Qnil;
1555 waiting_for_input = 0;
1556 cancel_echoing ();
1558 nonundocount = 0;
1559 this_command_key_count = 0;
1560 this_command_key_count_reset = 0;
1561 this_single_command_key_start = 0;
1563 if (NILP (Vmemory_full))
1565 /* Make sure this hook runs after commands that get errors and
1566 throw to top level. */
1567 /* Note that the value cell will never directly contain nil
1568 if the symbol is a local variable. */
1569 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1570 safe_run_hooks (Qpost_command_hook);
1572 /* If displaying a message, resize the echo area window to fit
1573 that message's size exactly. */
1574 if (!NILP (echo_area_buffer[0]))
1575 resize_echo_area_exactly ();
1577 if (!NILP (Vdeferred_action_list))
1578 safe_run_hooks (Qdeferred_action_function);
1581 /* Do this after running Vpost_command_hook, for consistency. */
1582 current_kboard->Vlast_command = Vthis_command;
1583 current_kboard->Vreal_last_command = real_this_command;
1584 if (!CONSP (last_command_char))
1585 current_kboard->Vlast_repeatable_command = real_this_command;
1587 while (1)
1589 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1590 Fkill_emacs (Qnil);
1592 /* Make sure the current window's buffer is selected. */
1593 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1594 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1596 /* Display any malloc warning that just came out. Use while because
1597 displaying one warning can cause another. */
1599 while (pending_malloc_warning)
1600 display_malloc_warning ();
1602 Vdeactivate_mark = Qnil;
1604 /* If minibuffer on and echo area in use,
1605 wait a short time and redraw minibuffer. */
1607 if (minibuf_level
1608 && !NILP (echo_area_buffer[0])
1609 && EQ (minibuf_window, echo_area_window)
1610 && NUMBERP (Vminibuffer_message_timeout))
1612 /* Bind inhibit-quit to t so that C-g gets read in
1613 rather than quitting back to the minibuffer. */
1614 int count = SPECPDL_INDEX ();
1615 specbind (Qinhibit_quit, Qt);
1617 sit_for (Vminibuffer_message_timeout, 0, 2);
1619 /* Clear the echo area. */
1620 message2 (0, 0, 0);
1621 safe_run_hooks (Qecho_area_clear_hook);
1623 unbind_to (count, Qnil);
1625 /* If a C-g came in before, treat it as input now. */
1626 if (!NILP (Vquit_flag))
1628 Vquit_flag = Qnil;
1629 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1633 #if 0
1634 /* Select the frame that the last event came from. Usually,
1635 switch-frame events will take care of this, but if some lisp
1636 code swallows a switch-frame event, we'll fix things up here.
1637 Is this a good idea? */
1638 if (FRAMEP (internal_last_event_frame)
1639 && !EQ (internal_last_event_frame, selected_frame))
1640 Fselect_frame (internal_last_event_frame);
1641 #endif
1642 /* If it has changed current-menubar from previous value,
1643 really recompute the menubar from the value. */
1644 if (! NILP (Vlucid_menu_bar_dirty_flag)
1645 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1646 call0 (Qrecompute_lucid_menubar);
1648 before_command_key_count = this_command_key_count;
1649 before_command_echo_length = echo_length ();
1651 Vthis_command = Qnil;
1652 real_this_command = Qnil;
1653 Vthis_original_command = Qnil;
1654 Vthis_command_keys_shift_translated = Qnil;
1656 /* Read next key sequence; i gets its length. */
1657 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1658 Qnil, 0, 1, 1);
1660 /* A filter may have run while we were reading the input. */
1661 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1662 Fkill_emacs (Qnil);
1663 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1664 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1666 ++num_input_keys;
1668 /* Now we have read a key sequence of length I,
1669 or else I is 0 and we found end of file. */
1671 if (i == 0) /* End of file -- happens only in */
1672 return Qnil; /* a kbd macro, at the end. */
1673 /* -1 means read_key_sequence got a menu that was rejected.
1674 Just loop around and read another command. */
1675 if (i == -1)
1677 cancel_echoing ();
1678 this_command_key_count = 0;
1679 this_command_key_count_reset = 0;
1680 this_single_command_key_start = 0;
1681 goto finalize;
1684 last_command_char = keybuf[i - 1];
1686 /* If the previous command tried to force a specific window-start,
1687 forget about that, in case this command moves point far away
1688 from that position. But also throw away beg_unchanged and
1689 end_unchanged information in that case, so that redisplay will
1690 update the whole window properly. */
1691 if (!NILP (XWINDOW (selected_window)->force_start))
1693 struct buffer *b;
1694 XWINDOW (selected_window)->force_start = Qnil;
1695 b = XBUFFER (XWINDOW (selected_window)->buffer);
1696 BUF_BEG_UNCHANGED (b) = BUF_END_UNCHANGED (b) = 0;
1699 cmd = read_key_sequence_cmd;
1700 if (!NILP (Vexecuting_kbd_macro))
1702 if (!NILP (Vquit_flag))
1704 Vexecuting_kbd_macro = Qt;
1705 QUIT; /* Make some noise. */
1706 /* Will return since macro now empty. */
1710 /* Do redisplay processing after this command except in special
1711 cases identified below. */
1712 prev_buffer = current_buffer;
1713 prev_modiff = MODIFF;
1714 last_point_position = PT;
1715 last_point_position_window = selected_window;
1716 XSETBUFFER (last_point_position_buffer, prev_buffer);
1718 /* By default, we adjust point to a boundary of a region that
1719 has such a property that should be treated intangible
1720 (e.g. composition, display). But, some commands will set
1721 this variable differently. */
1722 Vdisable_point_adjustment = Qnil;
1724 /* Process filters and timers may have messed with deactivate-mark.
1725 reset it before we execute the command. */
1726 Vdeactivate_mark = Qnil;
1728 /* Remap command through active keymaps */
1729 Vthis_original_command = cmd;
1730 if (SYMBOLP (cmd))
1732 Lisp_Object cmd1;
1733 if (cmd1 = Fcommand_remapping (cmd, Qnil, Qnil), !NILP (cmd1))
1734 cmd = cmd1;
1737 /* Execute the command. */
1739 Vthis_command = cmd;
1740 real_this_command = cmd;
1741 /* Note that the value cell will never directly contain nil
1742 if the symbol is a local variable. */
1743 if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
1744 safe_run_hooks (Qpre_command_hook);
1746 already_adjusted = 0;
1748 if (NILP (Vthis_command))
1750 /* nil means key is undefined. */
1751 Lisp_Object keys = Fvector (i, keybuf);
1752 keys = Fkey_description (keys, Qnil);
1753 bitch_at_user ();
1754 message_with_string ("%s is undefined", keys, 0);
1755 current_kboard->defining_kbd_macro = Qnil;
1756 update_mode_lines = 1;
1757 current_kboard->Vprefix_arg = Qnil;
1759 else
1761 if (NILP (current_kboard->Vprefix_arg))
1763 /* In case we jump to directly_done. */
1764 Vcurrent_prefix_arg = current_kboard->Vprefix_arg;
1766 /* Recognize some common commands in common situations and
1767 do them directly. */
1768 if (EQ (Vthis_command, Qforward_char) && PT < ZV
1769 && NILP (Vthis_command_keys_shift_translated)
1770 && !CONSP (Vtransient_mark_mode))
1772 struct Lisp_Char_Table *dp
1773 = window_display_table (XWINDOW (selected_window));
1774 lose = FETCH_CHAR (PT_BYTE);
1775 SET_PT (PT + 1);
1776 if (! NILP (Vpost_command_hook))
1777 /* Put this before calling adjust_point_for_property
1778 so it will only get called once in any case. */
1779 goto directly_done;
1780 if (current_buffer == prev_buffer
1781 && last_point_position != PT
1782 && NILP (Vdisable_point_adjustment)
1783 && NILP (Vglobal_disable_point_adjustment))
1784 adjust_point_for_property (last_point_position, 0);
1785 already_adjusted = 1;
1786 if (PT == last_point_position + 1
1787 && (dp
1788 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1789 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1790 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1791 && (lose >= 0x20 && lose < 0x7f)))
1792 : (lose >= 0x20 && lose < 0x7f))
1793 /* To extract the case of continuation on
1794 wide-column characters. */
1795 && ASCII_BYTE_P (lose)
1796 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1797 >= MODIFF)
1798 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1799 >= OVERLAY_MODIFF)
1800 && (XFASTINT (XWINDOW (selected_window)->last_point)
1801 == PT - 1)
1802 && !windows_or_buffers_changed
1803 && EQ (current_buffer->selective_display, Qnil)
1804 && !detect_input_pending ()
1805 && NILP (XWINDOW (selected_window)->column_number_displayed)
1806 && NILP (Vexecuting_kbd_macro))
1807 direct_output_forward_char (1);
1808 goto directly_done;
1810 else if (EQ (Vthis_command, Qbackward_char) && PT > BEGV
1811 && NILP (Vthis_command_keys_shift_translated)
1812 && !CONSP (Vtransient_mark_mode))
1814 struct Lisp_Char_Table *dp
1815 = window_display_table (XWINDOW (selected_window));
1816 SET_PT (PT - 1);
1817 lose = FETCH_CHAR (PT_BYTE);
1818 if (! NILP (Vpost_command_hook))
1819 goto directly_done;
1820 if (current_buffer == prev_buffer
1821 && last_point_position != PT
1822 && NILP (Vdisable_point_adjustment)
1823 && NILP (Vglobal_disable_point_adjustment))
1824 adjust_point_for_property (last_point_position, 0);
1825 already_adjusted = 1;
1826 if (PT == last_point_position - 1
1827 && (dp
1828 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1829 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1830 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1831 && (lose >= 0x20 && lose < 0x7f)))
1832 : (lose >= 0x20 && lose < 0x7f))
1833 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1834 >= MODIFF)
1835 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1836 >= OVERLAY_MODIFF)
1837 && (XFASTINT (XWINDOW (selected_window)->last_point)
1838 == PT + 1)
1839 && !windows_or_buffers_changed
1840 && EQ (current_buffer->selective_display, Qnil)
1841 && !detect_input_pending ()
1842 && NILP (XWINDOW (selected_window)->column_number_displayed)
1843 && NILP (Vexecuting_kbd_macro))
1844 direct_output_forward_char (-1);
1845 goto directly_done;
1847 else if (EQ (Vthis_command, Qself_insert_command)
1848 /* Try this optimization only on char keystrokes. */
1849 && NATNUMP (last_command_char)
1850 && CHAR_VALID_P (XFASTINT (last_command_char), 0))
1852 unsigned int c
1853 = translate_char (Vtranslation_table_for_input,
1854 XFASTINT (last_command_char));
1855 int value;
1856 if (NILP (Vexecuting_kbd_macro)
1857 && !EQ (minibuf_window, selected_window))
1859 if (!nonundocount || nonundocount >= 20)
1861 Fundo_boundary ();
1862 nonundocount = 0;
1864 nonundocount++;
1867 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1868 < MODIFF)
1869 || (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1870 < OVERLAY_MODIFF)
1871 || (XFASTINT (XWINDOW (selected_window)->last_point)
1872 != PT)
1873 || MODIFF <= SAVE_MODIFF
1874 || windows_or_buffers_changed
1875 || !EQ (current_buffer->selective_display, Qnil)
1876 || detect_input_pending ()
1877 || !NILP (XWINDOW (selected_window)->column_number_displayed)
1878 || !NILP (Vexecuting_kbd_macro));
1880 value = internal_self_insert (c, 0);
1882 if (value == 2)
1883 nonundocount = 0;
1885 if (! NILP (Vpost_command_hook))
1886 /* Put this before calling adjust_point_for_property
1887 so it will only get called once in any case. */
1888 goto directly_done;
1890 /* VALUE == 1 when AFTER-CHANGE functions are
1891 installed which is the case most of the time
1892 because FONT-LOCK installs one. */
1893 if (!lose && !value)
1894 direct_output_for_insert (c);
1895 goto directly_done;
1899 /* Here for a command that isn't executed directly */
1902 #ifdef HAVE_WINDOW_SYSTEM
1903 int scount = SPECPDL_INDEX ();
1905 if (display_hourglass_p
1906 && NILP (Vexecuting_kbd_macro))
1908 record_unwind_protect (cancel_hourglass_unwind, Qnil);
1909 start_hourglass ();
1911 #endif
1913 nonundocount = 0;
1914 if (NILP (current_kboard->Vprefix_arg))
1915 Fundo_boundary ();
1916 Fcommand_execute (Vthis_command, Qnil, Qnil, Qnil);
1918 #ifdef HAVE_WINDOW_SYSTEM
1919 /* Do not check display_hourglass_p here, because
1920 Fcommand_execute could change it, but we should cancel
1921 hourglass cursor anyway.
1922 But don't cancel the hourglass within a macro
1923 just because a command in the macro finishes. */
1924 if (NILP (Vexecuting_kbd_macro))
1925 unbind_to (scount, Qnil);
1926 #endif
1929 directly_done: ;
1930 current_kboard->Vlast_prefix_arg = Vcurrent_prefix_arg;
1932 /* Note that the value cell will never directly contain nil
1933 if the symbol is a local variable. */
1934 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1935 safe_run_hooks (Qpost_command_hook);
1937 /* If displaying a message, resize the echo area window to fit
1938 that message's size exactly. */
1939 if (!NILP (echo_area_buffer[0]))
1940 resize_echo_area_exactly ();
1942 if (!NILP (Vdeferred_action_list))
1943 safe_run_hooks (Qdeferred_action_function);
1945 /* If there is a prefix argument,
1946 1) We don't want Vlast_command to be ``universal-argument''
1947 (that would be dumb), so don't set Vlast_command,
1948 2) we want to leave echoing on so that the prefix will be
1949 echoed as part of this key sequence, so don't call
1950 cancel_echoing, and
1951 3) we want to leave this_command_key_count non-zero, so that
1952 read_char will realize that it is re-reading a character, and
1953 not echo it a second time.
1955 If the command didn't actually create a prefix arg,
1956 but is merely a frame event that is transparent to prefix args,
1957 then the above doesn't apply. */
1958 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1960 current_kboard->Vlast_command = Vthis_command;
1961 current_kboard->Vreal_last_command = real_this_command;
1962 if (!CONSP (last_command_char))
1963 current_kboard->Vlast_repeatable_command = real_this_command;
1964 cancel_echoing ();
1965 this_command_key_count = 0;
1966 this_command_key_count_reset = 0;
1967 this_single_command_key_start = 0;
1970 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1972 /* In Emacs 22, setting transient-mark-mode to `only' was a
1973 way of turning it on for just one command. This usage is
1974 obsolete, but support it anyway. */
1975 if (EQ (Vtransient_mark_mode, Qidentity))
1976 Vtransient_mark_mode = Qnil;
1977 else if (EQ (Vtransient_mark_mode, Qonly))
1978 Vtransient_mark_mode = Qidentity;
1980 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1982 /* We could also call `deactivate'mark'. */
1983 if (EQ (Vtransient_mark_mode, Qlambda))
1984 Vtransient_mark_mode = Qnil;
1985 else
1987 current_buffer->mark_active = Qnil;
1988 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1991 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1992 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1995 finalize:
1997 if (current_buffer == prev_buffer
1998 && last_point_position != PT
1999 && NILP (Vdisable_point_adjustment)
2000 && NILP (Vglobal_disable_point_adjustment)
2001 && !already_adjusted)
2002 adjust_point_for_property (last_point_position, MODIFF != prev_modiff);
2004 /* Install chars successfully executed in kbd macro. */
2006 if (!NILP (current_kboard->defining_kbd_macro)
2007 && NILP (current_kboard->Vprefix_arg))
2008 finalize_kbd_macro_chars ();
2009 #if 0 /* This shouldn't be necessary anymore. --lorentey */
2010 #ifdef MULTI_KBOARD
2011 if (!was_locked)
2012 any_kboard_state ();
2013 #endif
2014 #endif
2018 extern Lisp_Object Qcomposition, Qdisplay;
2020 /* Adjust point to a boundary of a region that has such a property
2021 that should be treated intangible. For the moment, we check
2022 `composition', `display' and `invisible' properties.
2023 LAST_PT is the last position of point. */
2025 extern Lisp_Object Qafter_string, Qbefore_string;
2026 extern Lisp_Object get_pos_property P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
2028 static void
2029 adjust_point_for_property (last_pt, modified)
2030 int last_pt;
2031 int modified;
2033 EMACS_INT beg, end;
2034 Lisp_Object val, overlay, tmp;
2035 int check_composition = 1, check_display = 1, check_invisible = 1;
2036 int orig_pt = PT;
2038 /* FIXME: cycling is probably not necessary because these properties
2039 can't be usefully combined anyway. */
2040 while (check_composition || check_display || check_invisible)
2042 if (check_composition
2043 && PT > BEGV && PT < ZV
2044 && get_property_and_range (PT, Qcomposition, &val, &beg, &end, Qnil)
2045 && COMPOSITION_VALID_P (beg, end, val)
2046 && beg < PT /* && end > PT <- It's always the case. */
2047 && (last_pt <= beg || last_pt >= end))
2049 xassert (end > PT);
2050 SET_PT (PT < last_pt ? beg : end);
2051 check_display = check_invisible = 1;
2053 check_composition = 0;
2054 if (check_display
2055 && PT > BEGV && PT < ZV
2056 && !NILP (val = get_char_property_and_overlay
2057 (make_number (PT), Qdisplay, Qnil, &overlay))
2058 && display_prop_intangible_p (val)
2059 && (!OVERLAYP (overlay)
2060 ? get_property_and_range (PT, Qdisplay, &val, &beg, &end, Qnil)
2061 : (beg = OVERLAY_POSITION (OVERLAY_START (overlay)),
2062 end = OVERLAY_POSITION (OVERLAY_END (overlay))))
2063 && (beg < PT /* && end > PT <- It's always the case. */
2064 || (beg <= PT && STRINGP (val) && SCHARS (val) == 0)))
2066 xassert (end > PT);
2067 SET_PT (PT < last_pt
2068 ? (STRINGP (val) && SCHARS (val) == 0 ? beg - 1 : beg)
2069 : end);
2070 check_composition = check_invisible = 1;
2072 check_display = 0;
2073 if (check_invisible && PT > BEGV && PT < ZV)
2075 int inv, ellipsis = 0;
2076 beg = end = PT;
2078 /* Find boundaries `beg' and `end' of the invisible area, if any. */
2079 while (end < ZV
2080 && !NILP (val = get_char_property_and_overlay
2081 (make_number (end), Qinvisible, Qnil, &overlay))
2082 && (inv = TEXT_PROP_MEANS_INVISIBLE (val)))
2084 ellipsis = ellipsis || inv > 1
2085 || (OVERLAYP (overlay)
2086 && (!NILP (Foverlay_get (overlay, Qafter_string))
2087 || !NILP (Foverlay_get (overlay, Qbefore_string))));
2088 tmp = Fnext_single_char_property_change
2089 (make_number (end), Qinvisible, Qnil, Qnil);
2090 end = NATNUMP (tmp) ? XFASTINT (tmp) : ZV;
2092 while (beg > BEGV
2093 && !NILP (val = get_char_property_and_overlay
2094 (make_number (beg - 1), Qinvisible, Qnil, &overlay))
2095 && (inv = TEXT_PROP_MEANS_INVISIBLE (val)))
2097 ellipsis = ellipsis || inv > 1
2098 || (OVERLAYP (overlay)
2099 && (!NILP (Foverlay_get (overlay, Qafter_string))
2100 || !NILP (Foverlay_get (overlay, Qbefore_string))));
2101 tmp = Fprevious_single_char_property_change
2102 (make_number (beg), Qinvisible, Qnil, Qnil);
2103 beg = NATNUMP (tmp) ? XFASTINT (tmp) : BEGV;
2106 /* Move away from the inside area. */
2107 if (beg < PT && end > PT)
2109 SET_PT ((orig_pt == PT && (last_pt < beg || last_pt > end))
2110 /* We haven't moved yet (so we don't need to fear
2111 infinite-looping) and we were outside the range
2112 before (so either end of the range still corresponds
2113 to a move in the right direction): pretend we moved
2114 less than we actually did, so that we still have
2115 more freedom below in choosing which end of the range
2116 to go to. */
2117 ? (orig_pt = -1, PT < last_pt ? end : beg)
2118 /* We either have moved already or the last point
2119 was already in the range: we don't get to choose
2120 which end of the range we have to go to. */
2121 : (PT < last_pt ? beg : end));
2122 check_composition = check_display = 1;
2124 #if 0 /* This assertion isn't correct, because SET_PT may end up setting
2125 the point to something other than its argument, due to
2126 point-motion hooks, intangibility, etc. */
2127 xassert (PT == beg || PT == end);
2128 #endif
2130 /* Pretend the area doesn't exist if the buffer is not
2131 modified. */
2132 if (!modified && !ellipsis && beg < end)
2134 if (last_pt == beg && PT == end && end < ZV)
2135 (check_composition = check_display = 1, SET_PT (end + 1));
2136 else if (last_pt == end && PT == beg && beg > BEGV)
2137 (check_composition = check_display = 1, SET_PT (beg - 1));
2138 else if (PT == ((PT < last_pt) ? beg : end))
2139 /* We've already moved as far as we can. Trying to go
2140 to the other end would mean moving backwards and thus
2141 could lead to an infinite loop. */
2143 else if (val = get_pos_property (make_number (PT),
2144 Qinvisible, Qnil),
2145 TEXT_PROP_MEANS_INVISIBLE (val)
2146 && (val = get_pos_property
2147 (make_number (PT == beg ? end : beg),
2148 Qinvisible, Qnil),
2149 !TEXT_PROP_MEANS_INVISIBLE (val)))
2150 (check_composition = check_display = 1,
2151 SET_PT (PT == beg ? end : beg));
2154 check_invisible = 0;
2158 /* Subroutine for safe_run_hooks: run the hook HOOK. */
2160 static Lisp_Object
2161 safe_run_hooks_1 (hook)
2162 Lisp_Object hook;
2164 if (NILP (Vrun_hooks))
2165 return Qnil;
2166 return call1 (Vrun_hooks, Vinhibit_quit);
2169 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
2171 static Lisp_Object
2172 safe_run_hooks_error (data)
2173 Lisp_Object data;
2175 Lisp_Object args[3];
2176 args[0] = build_string ("Error in %s: %s");
2177 args[1] = Vinhibit_quit;
2178 args[2] = data;
2179 Fmessage (3, args);
2180 return Fset (Vinhibit_quit, Qnil);
2183 /* If we get an error while running the hook, cause the hook variable
2184 to be nil. Also inhibit quits, so that C-g won't cause the hook
2185 to mysteriously evaporate. */
2187 void
2188 safe_run_hooks (hook)
2189 Lisp_Object hook;
2191 int count = SPECPDL_INDEX ();
2192 specbind (Qinhibit_quit, hook);
2194 internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
2196 unbind_to (count, Qnil);
2200 /* Number of seconds between polling for input. This is a Lisp
2201 variable that can be bound. */
2203 EMACS_INT polling_period;
2205 /* Nonzero means polling for input is temporarily suppressed. */
2207 int poll_suppress_count;
2209 /* Asynchronous timer for polling. */
2211 struct atimer *poll_timer;
2214 #ifdef POLL_FOR_INPUT
2216 /* Poll for input, so what we catch a C-g if it comes in. This
2217 function is called from x_make_frame_visible, see comment
2218 there. */
2220 void
2221 poll_for_input_1 ()
2223 if (interrupt_input_blocked == 0
2224 && !waiting_for_input)
2225 read_avail_input (0);
2228 /* Timer callback function for poll_timer. TIMER is equal to
2229 poll_timer. */
2231 void
2232 poll_for_input (timer)
2233 struct atimer *timer;
2235 if (poll_suppress_count == 0)
2236 #ifdef SYNC_INPUT
2237 interrupt_input_pending = 1;
2238 #else
2239 poll_for_input_1 ();
2240 #endif
2243 #endif /* POLL_FOR_INPUT */
2245 /* Begin signals to poll for input, if they are appropriate.
2246 This function is called unconditionally from various places. */
2248 void
2249 start_polling ()
2251 #ifdef POLL_FOR_INPUT
2252 /* XXX This condition was (read_socket_hook && !interrupt_input),
2253 but read_socket_hook is not global anymore. Let's pretend that
2254 it's always set. */
2255 if (!interrupt_input)
2257 /* Turn alarm handling on unconditionally. It might have
2258 been turned off in process.c. */
2259 turn_on_atimers (1);
2261 /* If poll timer doesn't exist, are we need one with
2262 a different interval, start a new one. */
2263 if (poll_timer == NULL
2264 || EMACS_SECS (poll_timer->interval) != polling_period)
2266 EMACS_TIME interval;
2268 if (poll_timer)
2269 cancel_atimer (poll_timer);
2271 EMACS_SET_SECS_USECS (interval, polling_period, 0);
2272 poll_timer = start_atimer (ATIMER_CONTINUOUS, interval,
2273 poll_for_input, NULL);
2276 /* Let the timer's callback function poll for input
2277 if this becomes zero. */
2278 --poll_suppress_count;
2280 #endif
2283 /* Nonzero if we are using polling to handle input asynchronously. */
2286 input_polling_used ()
2288 #ifdef POLL_FOR_INPUT
2289 /* XXX This condition was (read_socket_hook && !interrupt_input),
2290 but read_socket_hook is not global anymore. Let's pretend that
2291 it's always set. */
2292 return !interrupt_input;
2293 #else
2294 return 0;
2295 #endif
2298 /* Turn off polling. */
2300 void
2301 stop_polling ()
2303 #ifdef POLL_FOR_INPUT
2304 /* XXX This condition was (read_socket_hook && !interrupt_input),
2305 but read_socket_hook is not global anymore. Let's pretend that
2306 it's always set. */
2307 if (!interrupt_input)
2308 ++poll_suppress_count;
2309 #endif
2312 /* Set the value of poll_suppress_count to COUNT
2313 and start or stop polling accordingly. */
2315 void
2316 set_poll_suppress_count (count)
2317 int count;
2319 #ifdef POLL_FOR_INPUT
2320 if (count == 0 && poll_suppress_count != 0)
2322 poll_suppress_count = 1;
2323 start_polling ();
2325 else if (count != 0 && poll_suppress_count == 0)
2327 stop_polling ();
2329 poll_suppress_count = count;
2330 #endif
2333 /* Bind polling_period to a value at least N.
2334 But don't decrease it. */
2336 void
2337 bind_polling_period (n)
2338 int n;
2340 #ifdef POLL_FOR_INPUT
2341 int new = polling_period;
2343 if (n > new)
2344 new = n;
2346 stop_other_atimers (poll_timer);
2347 stop_polling ();
2348 specbind (Qpolling_period, make_number (new));
2349 /* Start a new alarm with the new period. */
2350 start_polling ();
2351 #endif
2354 /* Apply the control modifier to CHARACTER. */
2357 make_ctrl_char (c)
2358 int c;
2360 /* Save the upper bits here. */
2361 int upper = c & ~0177;
2363 if (! ASCII_BYTE_P (c))
2364 return c |= ctrl_modifier;
2366 c &= 0177;
2368 /* Everything in the columns containing the upper-case letters
2369 denotes a control character. */
2370 if (c >= 0100 && c < 0140)
2372 int oc = c;
2373 c &= ~0140;
2374 /* Set the shift modifier for a control char
2375 made from a shifted letter. But only for letters! */
2376 if (oc >= 'A' && oc <= 'Z')
2377 c |= shift_modifier;
2380 /* The lower-case letters denote control characters too. */
2381 else if (c >= 'a' && c <= 'z')
2382 c &= ~0140;
2384 /* Include the bits for control and shift
2385 only if the basic ASCII code can't indicate them. */
2386 else if (c >= ' ')
2387 c |= ctrl_modifier;
2389 /* Replace the high bits. */
2390 c |= (upper & ~ctrl_modifier);
2392 return c;
2395 /* Display the help-echo property of the character after the mouse pointer.
2396 Either show it in the echo area, or call show-help-function to display
2397 it by other means (maybe in a tooltip).
2399 If HELP is nil, that means clear the previous help echo.
2401 If HELP is a string, display that string. If HELP is a function,
2402 call it with OBJECT and POS as arguments; the function should
2403 return a help string or nil for none. For all other types of HELP,
2404 evaluate it to obtain a string.
2406 WINDOW is the window in which the help was generated, if any.
2407 It is nil if not in a window.
2409 If OBJECT is a buffer, POS is the position in the buffer where the
2410 `help-echo' text property was found.
2412 If OBJECT is an overlay, that overlay has a `help-echo' property,
2413 and POS is the position in the overlay's buffer under the mouse.
2415 If OBJECT is a string (an overlay string or a string displayed with
2416 the `display' property). POS is the position in that string under
2417 the mouse.
2419 OK_TO_OVERWRITE_KEYSTROKE_ECHO non-zero means it's okay if the help
2420 echo overwrites a keystroke echo currently displayed in the echo
2421 area.
2423 Note: this function may only be called with HELP nil or a string
2424 from X code running asynchronously. */
2426 void
2427 show_help_echo (help, window, object, pos, ok_to_overwrite_keystroke_echo)
2428 Lisp_Object help, window, object, pos;
2429 int ok_to_overwrite_keystroke_echo;
2431 if (!NILP (help) && !STRINGP (help))
2433 if (FUNCTIONP (help))
2435 Lisp_Object args[4];
2436 args[0] = help;
2437 args[1] = window;
2438 args[2] = object;
2439 args[3] = pos;
2440 help = safe_call (4, args);
2442 else
2443 help = safe_eval (help);
2445 if (!STRINGP (help))
2446 return;
2449 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
2450 if (!noninteractive && STRINGP (help))
2452 /* The mouse-fixup-help-message Lisp function can call
2453 mouse_position_hook, which resets the mouse_moved flags.
2454 This causes trouble if we are trying to read a mouse motion
2455 event (i.e., if we are inside a `track-mouse' form), so we
2456 restore the mouse_moved flag. */
2457 FRAME_PTR f = NILP (do_mouse_tracking) ? NULL : some_mouse_moved ();
2458 help = call1 (Qmouse_fixup_help_message, help);
2459 if (f)
2460 f->mouse_moved = 1;
2462 #endif
2464 if (STRINGP (help) || NILP (help))
2466 if (!NILP (Vshow_help_function))
2467 call1 (Vshow_help_function, help);
2468 else if (/* Don't overwrite minibuffer contents. */
2469 !MINI_WINDOW_P (XWINDOW (selected_window))
2470 /* Don't overwrite a keystroke echo. */
2471 && (NILP (echo_message_buffer)
2472 || ok_to_overwrite_keystroke_echo)
2473 /* Don't overwrite a prompt. */
2474 && !cursor_in_echo_area)
2476 if (STRINGP (help))
2478 int count = SPECPDL_INDEX ();
2480 if (!help_echo_showing_p)
2481 Vpre_help_message = current_message ();
2483 specbind (Qmessage_truncate_lines, Qt);
2484 message3_nolog (help, SBYTES (help),
2485 STRING_MULTIBYTE (help));
2486 unbind_to (count, Qnil);
2488 else if (STRINGP (Vpre_help_message))
2490 message3_nolog (Vpre_help_message,
2491 SBYTES (Vpre_help_message),
2492 STRING_MULTIBYTE (Vpre_help_message));
2493 Vpre_help_message = Qnil;
2495 else
2496 message (0);
2499 help_echo_showing_p = STRINGP (help);
2505 /* Input of single characters from keyboard */
2507 Lisp_Object print_help ();
2508 static Lisp_Object kbd_buffer_get_event ();
2509 static void record_char ();
2511 static Lisp_Object help_form_saved_window_configs;
2512 static Lisp_Object
2513 read_char_help_form_unwind (arg)
2515 Lisp_Object window_config = XCAR (help_form_saved_window_configs);
2516 help_form_saved_window_configs = XCDR (help_form_saved_window_configs);
2517 if (!NILP (window_config))
2518 Fset_window_configuration (window_config);
2519 return Qnil;
2522 #define STOP_POLLING \
2523 do { if (! polling_stopped_here) stop_polling (); \
2524 polling_stopped_here = 1; } while (0)
2526 #define RESUME_POLLING \
2527 do { if (polling_stopped_here) start_polling (); \
2528 polling_stopped_here = 0; } while (0)
2530 /* read a character from the keyboard; call the redisplay if needed */
2531 /* commandflag 0 means do not do auto-saving, but do do redisplay.
2532 -1 means do not do redisplay, but do do autosaving.
2533 1 means do both. */
2535 /* The arguments MAPS and NMAPS are for menu prompting.
2536 MAPS is an array of keymaps; NMAPS is the length of MAPS.
2538 PREV_EVENT is the previous input event, or nil if we are reading
2539 the first event of a key sequence (or not reading a key sequence).
2540 If PREV_EVENT is t, that is a "magic" value that says
2541 not to run input methods, but in other respects to act as if
2542 not reading a key sequence.
2544 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
2545 if we used a mouse menu to read the input, or zero otherwise. If
2546 USED_MOUSE_MENU is null, we don't dereference it.
2548 Value is -2 when we find input on another keyboard. A second call
2549 to read_char will read it.
2551 If END_TIME is non-null, it is a pointer to an EMACS_TIME
2552 specifying the maximum time to wait until. If no input arrives by
2553 that time, stop waiting and return nil.
2555 Value is t if we showed a menu and the user rejected it. */
2557 Lisp_Object
2558 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu, end_time)
2559 int commandflag;
2560 int nmaps;
2561 Lisp_Object *maps;
2562 Lisp_Object prev_event;
2563 int *used_mouse_menu;
2564 EMACS_TIME *end_time;
2566 volatile Lisp_Object c;
2567 int count, jmpcount;
2568 jmp_buf local_getcjmp;
2569 jmp_buf save_jump;
2570 volatile int key_already_recorded = 0;
2571 Lisp_Object tem, save;
2572 volatile Lisp_Object previous_echo_area_message;
2573 volatile Lisp_Object also_record;
2574 volatile int reread;
2575 struct gcpro gcpro1, gcpro2;
2576 int polling_stopped_here = 0;
2577 struct kboard *orig_kboard = current_kboard;
2579 also_record = Qnil;
2581 #if 0 /* This was commented out as part of fixing echo for C-u left. */
2582 before_command_key_count = this_command_key_count;
2583 before_command_echo_length = echo_length ();
2584 #endif
2585 c = Qnil;
2586 previous_echo_area_message = Qnil;
2588 GCPRO2 (c, previous_echo_area_message);
2590 retry:
2592 reread = 0;
2593 if (CONSP (Vunread_post_input_method_events))
2595 c = XCAR (Vunread_post_input_method_events);
2596 Vunread_post_input_method_events
2597 = XCDR (Vunread_post_input_method_events);
2599 /* Undo what read_char_x_menu_prompt did when it unread
2600 additional keys returned by Fx_popup_menu. */
2601 if (CONSP (c)
2602 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2603 && NILP (XCDR (c)))
2604 c = XCAR (c);
2606 reread = 1;
2607 goto reread_first;
2610 if (unread_command_char != -1)
2612 XSETINT (c, unread_command_char);
2613 unread_command_char = -1;
2615 reread = 1;
2616 goto reread_first;
2619 if (CONSP (Vunread_command_events))
2621 c = XCAR (Vunread_command_events);
2622 Vunread_command_events = XCDR (Vunread_command_events);
2624 reread = 1;
2626 /* Undo what sit-for did when it unread additional keys
2627 inside universal-argument. */
2629 if (CONSP (c)
2630 && EQ (XCAR (c), Qt))
2632 reread = 0;
2633 c = XCDR (c);
2636 /* Undo what read_char_x_menu_prompt did when it unread
2637 additional keys returned by Fx_popup_menu. */
2638 if (CONSP (c)
2639 && EQ (XCDR (c), Qdisabled)
2640 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c))))
2641 c = XCAR (c);
2643 /* If the queued event is something that used the mouse,
2644 set used_mouse_menu accordingly. */
2645 if (used_mouse_menu
2646 && (EQ (c, Qtool_bar) || EQ (c, Qmenu_bar)))
2647 *used_mouse_menu = 1;
2649 goto reread_for_input_method;
2652 if (CONSP (Vunread_input_method_events))
2654 c = XCAR (Vunread_input_method_events);
2655 Vunread_input_method_events = XCDR (Vunread_input_method_events);
2657 /* Undo what read_char_x_menu_prompt did when it unread
2658 additional keys returned by Fx_popup_menu. */
2659 if (CONSP (c)
2660 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2661 && NILP (XCDR (c)))
2662 c = XCAR (c);
2663 reread = 1;
2664 goto reread_for_input_method;
2667 this_command_key_count_reset = 0;
2669 if (!NILP (Vexecuting_kbd_macro))
2671 /* We set this to Qmacro; since that's not a frame, nobody will
2672 try to switch frames on us, and the selected window will
2673 remain unchanged.
2675 Since this event came from a macro, it would be misleading to
2676 leave internal_last_event_frame set to wherever the last
2677 real event came from. Normally, a switch-frame event selects
2678 internal_last_event_frame after each command is read, but
2679 events read from a macro should never cause a new frame to be
2680 selected. */
2681 Vlast_event_frame = internal_last_event_frame = Qmacro;
2683 /* Exit the macro if we are at the end.
2684 Also, some things replace the macro with t
2685 to force an early exit. */
2686 if (EQ (Vexecuting_kbd_macro, Qt)
2687 || executing_kbd_macro_index >= XFASTINT (Flength (Vexecuting_kbd_macro)))
2689 XSETINT (c, -1);
2690 goto exit;
2693 c = Faref (Vexecuting_kbd_macro, make_number (executing_kbd_macro_index));
2694 if (STRINGP (Vexecuting_kbd_macro)
2695 && (XINT (c) & 0x80) && (XUINT (c) <= 0xff))
2696 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
2698 executing_kbd_macro_index++;
2700 goto from_macro;
2703 if (!NILP (unread_switch_frame))
2705 c = unread_switch_frame;
2706 unread_switch_frame = Qnil;
2708 /* This event should make it into this_command_keys, and get echoed
2709 again, so we do not set `reread'. */
2710 goto reread_first;
2713 /* if redisplay was requested */
2714 if (commandflag >= 0)
2716 int echo_current = EQ (echo_message_buffer, echo_area_buffer[0]);
2718 /* If there is pending input, process any events which are not
2719 user-visible, such as X selection_request events. */
2720 if (input_pending
2721 || detect_input_pending_run_timers (0))
2722 swallow_events (0); /* may clear input_pending */
2724 /* Redisplay if no pending input. */
2725 while (!input_pending)
2727 if (help_echo_showing_p && !EQ (selected_window, minibuf_window))
2728 redisplay_preserve_echo_area (5);
2729 else
2730 redisplay ();
2732 if (!input_pending)
2733 /* Normal case: no input arrived during redisplay. */
2734 break;
2736 /* Input arrived and pre-empted redisplay.
2737 Process any events which are not user-visible. */
2738 swallow_events (0);
2739 /* If that cleared input_pending, try again to redisplay. */
2742 /* Prevent the redisplay we just did
2743 from messing up echoing of the input after the prompt. */
2744 if (commandflag == 0 && echo_current)
2745 echo_message_buffer = echo_area_buffer[0];
2749 /* Message turns off echoing unless more keystrokes turn it on again.
2751 The code in 20.x for the condition was
2753 1. echo_area_glyphs && *echo_area_glyphs
2754 2. && echo_area_glyphs != current_kboard->echobuf
2755 3. && ok_to_echo_at_next_pause != echo_area_glyphs
2757 (1) means there's a current message displayed
2759 (2) means it's not the message from echoing from the current
2760 kboard.
2762 (3) There's only one place in 20.x where ok_to_echo_at_next_pause
2763 is set to a non-null value. This is done in read_char and it is
2764 set to echo_area_glyphs after a call to echo_char. That means
2765 ok_to_echo_at_next_pause is either null or
2766 current_kboard->echobuf with the appropriate current_kboard at
2767 that time.
2769 So, condition (3) means in clear text ok_to_echo_at_next_pause
2770 must be either null, or the current message isn't from echoing at
2771 all, or it's from echoing from a different kboard than the
2772 current one. */
2774 if (/* There currently is something in the echo area. */
2775 !NILP (echo_area_buffer[0])
2776 && (/* And it's either not from echoing. */
2777 !EQ (echo_area_buffer[0], echo_message_buffer)
2778 /* Or it's an echo from a different kboard. */
2779 || echo_kboard != current_kboard
2780 /* Or we explicitly allow overwriting whatever there is. */
2781 || ok_to_echo_at_next_pause == NULL))
2782 cancel_echoing ();
2783 else
2784 echo_dash ();
2786 /* Try reading a character via menu prompting in the minibuf.
2787 Try this before the sit-for, because the sit-for
2788 would do the wrong thing if we are supposed to do
2789 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
2790 after a mouse event so don't try a minibuf menu. */
2791 c = Qnil;
2792 if (nmaps > 0 && INTERACTIVE
2793 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
2794 /* Don't bring up a menu if we already have another event. */
2795 && NILP (Vunread_command_events)
2796 && unread_command_char < 0
2797 && !detect_input_pending_run_timers (0))
2799 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
2801 if (INTEGERP (c) && XINT (c) == -2)
2802 return c; /* wrong_kboard_jmpbuf */
2804 if (! NILP (c))
2806 key_already_recorded = 1;
2807 goto non_reread_1;
2811 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
2812 We will do that below, temporarily for short sections of code,
2813 when appropriate. local_getcjmp must be in effect
2814 around any call to sit_for or kbd_buffer_get_event;
2815 it *must not* be in effect when we call redisplay. */
2817 jmpcount = SPECPDL_INDEX ();
2818 if (_setjmp (local_getcjmp))
2820 /* Handle quits while reading the keyboard. */
2821 /* We must have saved the outer value of getcjmp here,
2822 so restore it now. */
2823 restore_getcjmp (save_jump);
2824 unbind_to (jmpcount, Qnil);
2825 XSETINT (c, quit_char);
2826 internal_last_event_frame = selected_frame;
2827 Vlast_event_frame = internal_last_event_frame;
2828 /* If we report the quit char as an event,
2829 don't do so more than once. */
2830 if (!NILP (Vinhibit_quit))
2831 Vquit_flag = Qnil;
2833 #ifdef MULTI_KBOARD
2835 KBOARD *kb = FRAME_KBOARD (XFRAME (selected_frame));
2836 if (kb != current_kboard)
2838 Lisp_Object link = kb->kbd_queue;
2839 /* We shouldn't get here if we were in single-kboard mode! */
2840 if (single_kboard)
2841 abort ();
2842 if (CONSP (link))
2844 while (CONSP (XCDR (link)))
2845 link = XCDR (link);
2846 if (!NILP (XCDR (link)))
2847 abort ();
2849 if (!CONSP (link))
2850 kb->kbd_queue = Fcons (c, Qnil);
2851 else
2852 XSETCDR (link, Fcons (c, Qnil));
2853 kb->kbd_queue_has_data = 1;
2854 current_kboard = kb;
2855 /* This is going to exit from read_char
2856 so we had better get rid of this frame's stuff. */
2857 UNGCPRO;
2858 return make_number (-2); /* wrong_kboard_jmpbuf */
2861 #endif
2862 goto non_reread;
2865 /* Start idle timers if no time limit is supplied. We don't do it
2866 if a time limit is supplied to avoid an infinite recursion in the
2867 situation where an idle timer calls `sit-for'. */
2869 if (!end_time)
2870 timer_start_idle ();
2872 /* If in middle of key sequence and minibuffer not active,
2873 start echoing if enough time elapses. */
2875 if (minibuf_level == 0
2876 && !end_time
2877 && !current_kboard->immediate_echo
2878 && this_command_key_count > 0
2879 && ! noninteractive
2880 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2881 && NILP (Fzerop (Vecho_keystrokes))
2882 && (/* No message. */
2883 NILP (echo_area_buffer[0])
2884 /* Or empty message. */
2885 || (BUF_BEG (XBUFFER (echo_area_buffer[0]))
2886 == BUF_Z (XBUFFER (echo_area_buffer[0])))
2887 /* Or already echoing from same kboard. */
2888 || (echo_kboard && ok_to_echo_at_next_pause == echo_kboard)
2889 /* Or not echoing before and echoing allowed. */
2890 || (!echo_kboard && ok_to_echo_at_next_pause)))
2892 /* After a mouse event, start echoing right away.
2893 This is because we are probably about to display a menu,
2894 and we don't want to delay before doing so. */
2895 if (EVENT_HAS_PARAMETERS (prev_event))
2896 echo_now ();
2897 else
2899 Lisp_Object tem0;
2901 save_getcjmp (save_jump);
2902 restore_getcjmp (local_getcjmp);
2903 tem0 = sit_for (Vecho_keystrokes, 1, 1);
2904 restore_getcjmp (save_jump);
2905 if (EQ (tem0, Qt)
2906 && ! CONSP (Vunread_command_events))
2907 echo_now ();
2911 /* Maybe auto save due to number of keystrokes. */
2913 if (commandflag != 0
2914 && auto_save_interval > 0
2915 && num_nonmacro_input_events - last_auto_save > max (auto_save_interval, 20)
2916 && !detect_input_pending_run_timers (0))
2918 Fdo_auto_save (Qnil, Qnil);
2919 /* Hooks can actually change some buffers in auto save. */
2920 redisplay ();
2923 /* Try reading using an X menu.
2924 This is never confused with reading using the minibuf
2925 because the recursive call of read_char in read_char_minibuf_menu_prompt
2926 does not pass on any keymaps. */
2928 if (nmaps > 0 && INTERACTIVE
2929 && !NILP (prev_event)
2930 && EVENT_HAS_PARAMETERS (prev_event)
2931 && !EQ (XCAR (prev_event), Qmenu_bar)
2932 && !EQ (XCAR (prev_event), Qtool_bar)
2933 /* Don't bring up a menu if we already have another event. */
2934 && NILP (Vunread_command_events)
2935 && unread_command_char < 0)
2937 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
2939 /* Now that we have read an event, Emacs is not idle. */
2940 if (!end_time)
2941 timer_stop_idle ();
2943 goto exit;
2946 /* Maybe autosave and/or garbage collect due to idleness. */
2948 if (INTERACTIVE && NILP (c))
2950 int delay_level, buffer_size;
2952 /* Slow down auto saves logarithmically in size of current buffer,
2953 and garbage collect while we're at it. */
2954 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
2955 last_non_minibuf_size = Z - BEG;
2956 buffer_size = (last_non_minibuf_size >> 8) + 1;
2957 delay_level = 0;
2958 while (buffer_size > 64)
2959 delay_level++, buffer_size -= buffer_size >> 2;
2960 if (delay_level < 4) delay_level = 4;
2961 /* delay_level is 4 for files under around 50k, 7 at 100k,
2962 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
2964 /* Auto save if enough time goes by without input. */
2965 if (commandflag != 0
2966 && num_nonmacro_input_events > last_auto_save
2967 && INTEGERP (Vauto_save_timeout)
2968 && XINT (Vauto_save_timeout) > 0)
2970 Lisp_Object tem0;
2971 int timeout = delay_level * XFASTINT (Vauto_save_timeout) / 4;
2973 save_getcjmp (save_jump);
2974 restore_getcjmp (local_getcjmp);
2975 tem0 = sit_for (make_number (timeout), 1, 1);
2976 restore_getcjmp (save_jump);
2978 if (EQ (tem0, Qt)
2979 && ! CONSP (Vunread_command_events))
2981 Fdo_auto_save (Qnil, Qnil);
2983 /* If we have auto-saved and there is still no input
2984 available, garbage collect if there has been enough
2985 consing going on to make it worthwhile. */
2986 if (!detect_input_pending_run_timers (0)
2987 && consing_since_gc > gc_cons_threshold / 2)
2988 Fgarbage_collect ();
2990 redisplay ();
2995 /* Notify the caller if an autosave hook, or a timer, sentinel or
2996 filter in the sit_for calls above have changed the current
2997 kboard. This could happen if they use the minibuffer or start a
2998 recursive edit, like the fancy splash screen in server.el's
2999 filter. If this longjmp wasn't here, read_key_sequence would
3000 interpret the next key sequence using the wrong translation
3001 tables and function keymaps. */
3002 if (NILP (c) && current_kboard != orig_kboard)
3004 UNGCPRO;
3005 return make_number (-2); /* wrong_kboard_jmpbuf */
3008 /* If this has become non-nil here, it has been set by a timer
3009 or sentinel or filter. */
3010 if (CONSP (Vunread_command_events))
3012 c = XCAR (Vunread_command_events);
3013 Vunread_command_events = XCDR (Vunread_command_events);
3016 /* Read something from current KBOARD's side queue, if possible. */
3018 if (NILP (c))
3020 if (current_kboard->kbd_queue_has_data)
3022 if (!CONSP (current_kboard->kbd_queue))
3023 abort ();
3024 c = XCAR (current_kboard->kbd_queue);
3025 current_kboard->kbd_queue
3026 = XCDR (current_kboard->kbd_queue);
3027 if (NILP (current_kboard->kbd_queue))
3028 current_kboard->kbd_queue_has_data = 0;
3029 input_pending = readable_events (0);
3030 if (EVENT_HAS_PARAMETERS (c)
3031 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
3032 internal_last_event_frame = XCAR (XCDR (c));
3033 Vlast_event_frame = internal_last_event_frame;
3037 #ifdef MULTI_KBOARD
3038 /* If current_kboard's side queue is empty check the other kboards.
3039 If one of them has data that we have not yet seen here,
3040 switch to it and process the data waiting for it.
3042 Note: if the events queued up for another kboard
3043 have already been seen here, and therefore are not a complete command,
3044 the kbd_queue_has_data field is 0, so we skip that kboard here.
3045 That's to avoid an infinite loop switching between kboards here. */
3046 if (NILP (c) && !single_kboard)
3048 KBOARD *kb;
3049 for (kb = all_kboards; kb; kb = kb->next_kboard)
3050 if (kb->kbd_queue_has_data)
3052 current_kboard = kb;
3053 /* This is going to exit from read_char
3054 so we had better get rid of this frame's stuff. */
3055 UNGCPRO;
3056 return make_number (-2); /* wrong_kboard_jmpbuf */
3059 #endif
3061 wrong_kboard:
3063 STOP_POLLING;
3065 /* Finally, we read from the main queue,
3066 and if that gives us something we can't use yet, we put it on the
3067 appropriate side queue and try again. */
3069 if (NILP (c))
3071 KBOARD *kb;
3073 if (end_time)
3075 EMACS_TIME now;
3076 EMACS_GET_TIME (now);
3077 if (EMACS_TIME_GE (now, *end_time))
3078 goto exit;
3081 /* Actually read a character, waiting if necessary. */
3082 save_getcjmp (save_jump);
3083 restore_getcjmp (local_getcjmp);
3084 if (!end_time)
3085 timer_start_idle ();
3086 c = kbd_buffer_get_event (&kb, used_mouse_menu, end_time);
3087 restore_getcjmp (save_jump);
3089 #ifdef MULTI_KBOARD
3090 if (! NILP (c) && (kb != current_kboard))
3092 Lisp_Object link = kb->kbd_queue;
3093 if (CONSP (link))
3095 while (CONSP (XCDR (link)))
3096 link = XCDR (link);
3097 if (!NILP (XCDR (link)))
3098 abort ();
3100 if (!CONSP (link))
3101 kb->kbd_queue = Fcons (c, Qnil);
3102 else
3103 XSETCDR (link, Fcons (c, Qnil));
3104 kb->kbd_queue_has_data = 1;
3105 c = Qnil;
3106 if (single_kboard)
3107 goto wrong_kboard;
3108 current_kboard = kb;
3109 /* This is going to exit from read_char
3110 so we had better get rid of this frame's stuff. */
3111 UNGCPRO;
3112 return make_number (-2);
3114 #endif
3117 /* Terminate Emacs in batch mode if at eof. */
3118 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
3119 Fkill_emacs (make_number (1));
3121 if (INTEGERP (c))
3123 /* Add in any extra modifiers, where appropriate. */
3124 if ((extra_keyboard_modifiers & CHAR_CTL)
3125 || ((extra_keyboard_modifiers & 0177) < ' '
3126 && (extra_keyboard_modifiers & 0177) != 0))
3127 XSETINT (c, make_ctrl_char (XINT (c)));
3129 /* Transfer any other modifier bits directly from
3130 extra_keyboard_modifiers to c. Ignore the actual character code
3131 in the low 16 bits of extra_keyboard_modifiers. */
3132 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
3135 non_reread:
3137 if (!end_time)
3138 timer_stop_idle ();
3139 RESUME_POLLING;
3141 if (NILP (c))
3143 if (commandflag >= 0
3144 && !input_pending && !detect_input_pending_run_timers (0))
3145 redisplay ();
3147 goto wrong_kboard;
3150 non_reread_1:
3152 /* Buffer switch events are only for internal wakeups
3153 so don't show them to the user.
3154 Also, don't record a key if we already did. */
3155 if (BUFFERP (c) || key_already_recorded)
3156 goto exit;
3158 /* Process special events within read_char
3159 and loop around to read another event. */
3160 save = Vquit_flag;
3161 Vquit_flag = Qnil;
3162 tem = access_keymap (get_keymap (Vspecial_event_map, 0, 1), c, 0, 0, 1);
3163 Vquit_flag = save;
3165 if (!NILP (tem))
3167 #if 0 /* This shouldn't be necessary anymore. --lorentey */
3168 int was_locked = single_kboard;
3169 int count = SPECPDL_INDEX ();
3170 record_single_kboard_state ();
3171 #endif
3173 last_input_char = c;
3174 Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt);
3176 if (CONSP (c) && EQ (XCAR (c), Qselect_window) && !end_time)
3177 /* We stopped being idle for this event; undo that. This
3178 prevents automatic window selection (under
3179 mouse_autoselect_window from acting as a real input event, for
3180 example banishing the mouse under mouse-avoidance-mode. */
3181 timer_resume_idle ();
3183 #if 0 /* This shouldn't be necessary anymore. --lorentey */
3184 /* Resume allowing input from any kboard, if that was true before. */
3185 if (!was_locked)
3186 any_kboard_state ();
3187 unbind_to (count, Qnil);
3188 #endif
3190 goto retry;
3193 /* Handle things that only apply to characters. */
3194 if (INTEGERP (c))
3196 /* If kbd_buffer_get_event gave us an EOF, return that. */
3197 if (XINT (c) == -1)
3198 goto exit;
3200 if ((STRINGP (current_kboard->Vkeyboard_translate_table)
3201 && SCHARS (current_kboard->Vkeyboard_translate_table) > (unsigned) XFASTINT (c))
3202 || (VECTORP (current_kboard->Vkeyboard_translate_table)
3203 && XVECTOR (current_kboard->Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
3204 || (CHAR_TABLE_P (current_kboard->Vkeyboard_translate_table)
3205 && CHARACTERP (c)))
3207 Lisp_Object d;
3208 d = Faref (current_kboard->Vkeyboard_translate_table, c);
3209 /* nil in keyboard-translate-table means no translation. */
3210 if (!NILP (d))
3211 c = d;
3215 /* If this event is a mouse click in the menu bar,
3216 return just menu-bar for now. Modify the mouse click event
3217 so we won't do this twice, then queue it up. */
3218 if (EVENT_HAS_PARAMETERS (c)
3219 && CONSP (XCDR (c))
3220 && CONSP (EVENT_START (c))
3221 && CONSP (XCDR (EVENT_START (c))))
3223 Lisp_Object posn;
3225 posn = POSN_POSN (EVENT_START (c));
3226 /* Handle menu-bar events:
3227 insert the dummy prefix event `menu-bar'. */
3228 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
3230 /* Change menu-bar to (menu-bar) as the event "position". */
3231 POSN_SET_POSN (EVENT_START (c), Fcons (posn, Qnil));
3233 also_record = c;
3234 Vunread_command_events = Fcons (c, Vunread_command_events);
3235 c = posn;
3239 /* Store these characters into recent_keys, the dribble file if any,
3240 and the keyboard macro being defined, if any. */
3241 record_char (c);
3242 if (! NILP (also_record))
3243 record_char (also_record);
3245 /* Wipe the echo area.
3246 But first, if we are about to use an input method,
3247 save the echo area contents for it to refer to. */
3248 if (INTEGERP (c)
3249 && ! NILP (Vinput_method_function)
3250 && (unsigned) XINT (c) >= ' '
3251 && (unsigned) XINT (c) != 127
3252 && (unsigned) XINT (c) < 256)
3254 previous_echo_area_message = Fcurrent_message ();
3255 Vinput_method_previous_message = previous_echo_area_message;
3258 /* Now wipe the echo area, except for help events which do their
3259 own stuff with the echo area. */
3260 if (!CONSP (c)
3261 || (!(EQ (Qhelp_echo, XCAR (c)))
3262 && !(EQ (Qswitch_frame, XCAR (c)))))
3264 if (!NILP (echo_area_buffer[0]))
3265 safe_run_hooks (Qecho_area_clear_hook);
3266 clear_message (1, 0);
3269 reread_for_input_method:
3270 from_macro:
3271 /* Pass this to the input method, if appropriate. */
3272 if (INTEGERP (c)
3273 && ! NILP (Vinput_method_function)
3274 /* Don't run the input method within a key sequence,
3275 after the first event of the key sequence. */
3276 && NILP (prev_event)
3277 && (unsigned) XINT (c) >= ' '
3278 && (unsigned) XINT (c) != 127
3279 && (unsigned) XINT (c) < 256)
3281 Lisp_Object keys;
3282 int key_count, key_count_reset;
3283 struct gcpro gcpro1;
3284 int count = SPECPDL_INDEX ();
3286 /* Save the echo status. */
3287 int saved_immediate_echo = current_kboard->immediate_echo;
3288 struct kboard *saved_ok_to_echo = ok_to_echo_at_next_pause;
3289 Lisp_Object saved_echo_string = current_kboard->echo_string;
3290 int saved_echo_after_prompt = current_kboard->echo_after_prompt;
3292 #if 0
3293 if (before_command_restore_flag)
3295 this_command_key_count = before_command_key_count_1;
3296 if (this_command_key_count < this_single_command_key_start)
3297 this_single_command_key_start = this_command_key_count;
3298 echo_truncate (before_command_echo_length_1);
3299 before_command_restore_flag = 0;
3301 #endif
3303 /* Save the this_command_keys status. */
3304 key_count = this_command_key_count;
3305 key_count_reset = this_command_key_count_reset;
3307 if (key_count > 0)
3308 keys = Fcopy_sequence (this_command_keys);
3309 else
3310 keys = Qnil;
3311 GCPRO1 (keys);
3313 /* Clear out this_command_keys. */
3314 this_command_key_count = 0;
3315 this_command_key_count_reset = 0;
3317 /* Now wipe the echo area. */
3318 if (!NILP (echo_area_buffer[0]))
3319 safe_run_hooks (Qecho_area_clear_hook);
3320 clear_message (1, 0);
3321 echo_truncate (0);
3323 /* If we are not reading a key sequence,
3324 never use the echo area. */
3325 if (maps == 0)
3327 specbind (Qinput_method_use_echo_area, Qt);
3330 /* Call the input method. */
3331 tem = call1 (Vinput_method_function, c);
3333 tem = unbind_to (count, tem);
3335 /* Restore the saved echoing state
3336 and this_command_keys state. */
3337 this_command_key_count = key_count;
3338 this_command_key_count_reset = key_count_reset;
3339 if (key_count > 0)
3340 this_command_keys = keys;
3342 cancel_echoing ();
3343 ok_to_echo_at_next_pause = saved_ok_to_echo;
3344 current_kboard->echo_string = saved_echo_string;
3345 current_kboard->echo_after_prompt = saved_echo_after_prompt;
3346 if (saved_immediate_echo)
3347 echo_now ();
3349 UNGCPRO;
3351 /* The input method can return no events. */
3352 if (! CONSP (tem))
3354 /* Bring back the previous message, if any. */
3355 if (! NILP (previous_echo_area_message))
3356 message_with_string ("%s", previous_echo_area_message, 0);
3357 goto retry;
3359 /* It returned one event or more. */
3360 c = XCAR (tem);
3361 Vunread_post_input_method_events
3362 = nconc2 (XCDR (tem), Vunread_post_input_method_events);
3365 reread_first:
3367 /* Display help if not echoing. */
3368 if (CONSP (c) && EQ (XCAR (c), Qhelp_echo))
3370 /* (help-echo FRAME HELP WINDOW OBJECT POS). */
3371 Lisp_Object help, object, position, window, tem;
3373 tem = Fcdr (XCDR (c));
3374 help = Fcar (tem);
3375 tem = Fcdr (tem);
3376 window = Fcar (tem);
3377 tem = Fcdr (tem);
3378 object = Fcar (tem);
3379 tem = Fcdr (tem);
3380 position = Fcar (tem);
3382 show_help_echo (help, window, object, position, 0);
3384 /* We stopped being idle for this event; undo that. */
3385 if (!end_time)
3386 timer_resume_idle ();
3387 goto retry;
3390 if ((! reread || this_command_key_count == 0
3391 || this_command_key_count_reset)
3392 && !end_time)
3395 /* Don't echo mouse motion events. */
3396 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
3397 && NILP (Fzerop (Vecho_keystrokes))
3398 && ! (EVENT_HAS_PARAMETERS (c)
3399 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
3401 echo_char (c);
3402 if (! NILP (also_record))
3403 echo_char (also_record);
3404 /* Once we reread a character, echoing can happen
3405 the next time we pause to read a new one. */
3406 ok_to_echo_at_next_pause = current_kboard;
3409 /* Record this character as part of the current key. */
3410 add_command_key (c);
3411 if (! NILP (also_record))
3412 add_command_key (also_record);
3415 last_input_char = c;
3416 num_input_events++;
3418 /* Process the help character specially if enabled */
3419 if (!NILP (Vhelp_form) && help_char_p (c))
3421 Lisp_Object tem0;
3422 count = SPECPDL_INDEX ();
3424 help_form_saved_window_configs
3425 = Fcons (Fcurrent_window_configuration (Qnil),
3426 help_form_saved_window_configs);
3427 record_unwind_protect (read_char_help_form_unwind, Qnil);
3429 tem0 = Feval (Vhelp_form);
3430 if (STRINGP (tem0))
3431 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
3433 cancel_echoing ();
3436 c = read_char (0, 0, 0, Qnil, 0, NULL);
3437 if (EVENT_HAS_PARAMETERS (c)
3438 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_click))
3439 XSETCAR (help_form_saved_window_configs, Qnil);
3441 while (BUFFERP (c));
3442 /* Remove the help from the frame */
3443 unbind_to (count, Qnil);
3445 redisplay ();
3446 if (EQ (c, make_number (040)))
3448 cancel_echoing ();
3450 c = read_char (0, 0, 0, Qnil, 0, NULL);
3451 while (BUFFERP (c));
3455 exit:
3456 RESUME_POLLING;
3457 RETURN_UNGCPRO (c);
3460 /* Record a key that came from a mouse menu.
3461 Record it for echoing, for this-command-keys, and so on. */
3463 static void
3464 record_menu_key (c)
3465 Lisp_Object c;
3467 /* Wipe the echo area. */
3468 clear_message (1, 0);
3470 record_char (c);
3472 #if 0
3473 before_command_key_count = this_command_key_count;
3474 before_command_echo_length = echo_length ();
3475 #endif
3477 /* Don't echo mouse motion events. */
3478 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
3479 && NILP (Fzerop (Vecho_keystrokes)))
3481 echo_char (c);
3483 /* Once we reread a character, echoing can happen
3484 the next time we pause to read a new one. */
3485 ok_to_echo_at_next_pause = 0;
3488 /* Record this character as part of the current key. */
3489 add_command_key (c);
3491 /* Re-reading in the middle of a command */
3492 last_input_char = c;
3493 num_input_events++;
3496 /* Return 1 if should recognize C as "the help character". */
3499 help_char_p (c)
3500 Lisp_Object c;
3502 Lisp_Object tail;
3504 if (EQ (c, Vhelp_char))
3505 return 1;
3506 for (tail = Vhelp_event_list; CONSP (tail); tail = XCDR (tail))
3507 if (EQ (c, XCAR (tail)))
3508 return 1;
3509 return 0;
3512 /* Record the input event C in various ways. */
3514 static void
3515 record_char (c)
3516 Lisp_Object c;
3518 int recorded = 0;
3520 if (CONSP (c) && (EQ (XCAR (c), Qhelp_echo) || EQ (XCAR (c), Qmouse_movement)))
3522 /* To avoid filling recent_keys with help-echo and mouse-movement
3523 events, we filter out repeated help-echo events, only store the
3524 first and last in a series of mouse-movement events, and don't
3525 store repeated help-echo events which are only separated by
3526 mouse-movement events. */
3528 Lisp_Object ev1, ev2, ev3;
3529 int ix1, ix2, ix3;
3531 if ((ix1 = recent_keys_index - 1) < 0)
3532 ix1 = NUM_RECENT_KEYS - 1;
3533 ev1 = AREF (recent_keys, ix1);
3535 if ((ix2 = ix1 - 1) < 0)
3536 ix2 = NUM_RECENT_KEYS - 1;
3537 ev2 = AREF (recent_keys, ix2);
3539 if ((ix3 = ix2 - 1) < 0)
3540 ix3 = NUM_RECENT_KEYS - 1;
3541 ev3 = AREF (recent_keys, ix3);
3543 if (EQ (XCAR (c), Qhelp_echo))
3545 /* Don't record `help-echo' in recent_keys unless it shows some help
3546 message, and a different help than the previously recorded
3547 event. */
3548 Lisp_Object help, last_help;
3550 help = Fcar_safe (Fcdr_safe (XCDR (c)));
3551 if (!STRINGP (help))
3552 recorded = 1;
3553 else if (CONSP (ev1) && EQ (XCAR (ev1), Qhelp_echo)
3554 && (last_help = Fcar_safe (Fcdr_safe (XCDR (ev1))), EQ (last_help, help)))
3555 recorded = 1;
3556 else if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
3557 && CONSP (ev2) && EQ (XCAR (ev2), Qhelp_echo)
3558 && (last_help = Fcar_safe (Fcdr_safe (XCDR (ev2))), EQ (last_help, help)))
3559 recorded = -1;
3560 else if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
3561 && CONSP (ev2) && EQ (XCAR (ev2), Qmouse_movement)
3562 && CONSP (ev3) && EQ (XCAR (ev3), Qhelp_echo)
3563 && (last_help = Fcar_safe (Fcdr_safe (XCDR (ev3))), EQ (last_help, help)))
3564 recorded = -2;
3566 else if (EQ (XCAR (c), Qmouse_movement))
3568 /* Only record one pair of `mouse-movement' on a window in recent_keys.
3569 So additional mouse movement events replace the last element. */
3570 Lisp_Object last_window, window;
3572 window = Fcar_safe (Fcar_safe (XCDR (c)));
3573 if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
3574 && (last_window = Fcar_safe (Fcar_safe (XCDR (ev1))), EQ (last_window, window))
3575 && CONSP (ev2) && EQ (XCAR (ev2), Qmouse_movement)
3576 && (last_window = Fcar_safe (Fcar_safe (XCDR (ev2))), EQ (last_window, window)))
3578 ASET (recent_keys, ix1, c);
3579 recorded = 1;
3583 else
3584 store_kbd_macro_char (c);
3586 if (!recorded)
3588 total_keys++;
3589 ASET (recent_keys, recent_keys_index, c);
3590 if (++recent_keys_index >= NUM_RECENT_KEYS)
3591 recent_keys_index = 0;
3593 else if (recorded < 0)
3595 /* We need to remove one or two events from recent_keys.
3596 To do this, we simply put nil at those events and move the
3597 recent_keys_index backwards over those events. Usually,
3598 users will never see those nil events, as they will be
3599 overwritten by the command keys entered to see recent_keys
3600 (e.g. C-h l). */
3602 while (recorded++ < 0 && total_keys > 0)
3604 if (total_keys < NUM_RECENT_KEYS)
3605 total_keys--;
3606 if (--recent_keys_index < 0)
3607 recent_keys_index = NUM_RECENT_KEYS - 1;
3608 ASET (recent_keys, recent_keys_index, Qnil);
3612 num_nonmacro_input_events++;
3614 /* Write c to the dribble file. If c is a lispy event, write
3615 the event's symbol to the dribble file, in <brackets>. Bleaugh.
3616 If you, dear reader, have a better idea, you've got the source. :-) */
3617 if (dribble)
3619 BLOCK_INPUT;
3620 if (INTEGERP (c))
3622 if (XUINT (c) < 0x100)
3623 putc (XINT (c), dribble);
3624 else
3625 fprintf (dribble, " 0x%x", (int) XUINT (c));
3627 else
3629 Lisp_Object dribblee;
3631 /* If it's a structured event, take the event header. */
3632 dribblee = EVENT_HEAD (c);
3634 if (SYMBOLP (dribblee))
3636 putc ('<', dribble);
3637 fwrite (SDATA (SYMBOL_NAME (dribblee)), sizeof (char),
3638 SBYTES (SYMBOL_NAME (dribblee)),
3639 dribble);
3640 putc ('>', dribble);
3644 fflush (dribble);
3645 UNBLOCK_INPUT;
3649 Lisp_Object
3650 print_help (object)
3651 Lisp_Object object;
3653 struct buffer *old = current_buffer;
3654 Fprinc (object, Qnil);
3655 set_buffer_internal (XBUFFER (Vstandard_output));
3656 call0 (intern ("help-mode"));
3657 set_buffer_internal (old);
3658 return Qnil;
3661 /* Copy out or in the info on where C-g should throw to.
3662 This is used when running Lisp code from within get_char,
3663 in case get_char is called recursively.
3664 See read_process_output. */
3666 static void
3667 save_getcjmp (temp)
3668 jmp_buf temp;
3670 bcopy (getcjmp, temp, sizeof getcjmp);
3673 static void
3674 restore_getcjmp (temp)
3675 jmp_buf temp;
3677 bcopy (temp, getcjmp, sizeof getcjmp);
3680 /* Low level keyboard/mouse input.
3681 kbd_buffer_store_event places events in kbd_buffer, and
3682 kbd_buffer_get_event retrieves them. */
3684 /* Return true if there are any events in the queue that read-char
3685 would return. If this returns false, a read-char would block. */
3686 static int
3687 readable_events (flags)
3688 int flags;
3690 if (flags & READABLE_EVENTS_DO_TIMERS_NOW)
3691 timer_check (1);
3693 /* If the buffer contains only FOCUS_IN_EVENT events, and
3694 READABLE_EVENTS_FILTER_EVENTS is set, report it as empty. */
3695 if (kbd_fetch_ptr != kbd_store_ptr)
3697 if (flags & (READABLE_EVENTS_FILTER_EVENTS
3698 #ifdef USE_TOOLKIT_SCROLL_BARS
3699 | READABLE_EVENTS_IGNORE_SQUEEZABLES
3700 #endif
3703 struct input_event *event;
3705 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3706 ? kbd_fetch_ptr
3707 : kbd_buffer);
3711 if (!(
3712 #ifdef USE_TOOLKIT_SCROLL_BARS
3713 (flags & READABLE_EVENTS_FILTER_EVENTS) &&
3714 #endif
3715 event->kind == FOCUS_IN_EVENT)
3716 #ifdef USE_TOOLKIT_SCROLL_BARS
3717 && !((flags & READABLE_EVENTS_IGNORE_SQUEEZABLES)
3718 && event->kind == SCROLL_BAR_CLICK_EVENT
3719 && event->part == scroll_bar_handle
3720 && event->modifiers == 0)
3721 #endif
3723 return 1;
3724 event++;
3725 if (event == kbd_buffer + KBD_BUFFER_SIZE)
3726 event = kbd_buffer;
3728 while (event != kbd_store_ptr);
3730 else
3731 return 1;
3734 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
3735 if (!(flags & READABLE_EVENTS_IGNORE_SQUEEZABLES)
3736 && !NILP (do_mouse_tracking) && some_mouse_moved ())
3737 return 1;
3738 #endif
3739 if (single_kboard)
3741 if (current_kboard->kbd_queue_has_data)
3742 return 1;
3744 else
3746 KBOARD *kb;
3747 for (kb = all_kboards; kb; kb = kb->next_kboard)
3748 if (kb->kbd_queue_has_data)
3749 return 1;
3751 return 0;
3754 /* Set this for debugging, to have a way to get out */
3755 int stop_character;
3757 #ifdef MULTI_KBOARD
3758 static KBOARD *
3759 event_to_kboard (event)
3760 struct input_event *event;
3762 Lisp_Object frame;
3763 frame = event->frame_or_window;
3764 if (CONSP (frame))
3765 frame = XCAR (frame);
3766 else if (WINDOWP (frame))
3767 frame = WINDOW_FRAME (XWINDOW (frame));
3769 /* There are still some events that don't set this field.
3770 For now, just ignore the problem.
3771 Also ignore dead frames here. */
3772 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
3773 return 0;
3774 else
3775 return FRAME_KBOARD (XFRAME (frame));
3777 #endif
3780 Lisp_Object Vthrow_on_input;
3782 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
3784 void
3785 kbd_buffer_store_event (event)
3786 register struct input_event *event;
3788 kbd_buffer_store_event_hold (event, 0);
3791 /* Store EVENT obtained at interrupt level into kbd_buffer, fifo.
3793 If HOLD_QUIT is 0, just stuff EVENT into the fifo.
3794 Else, if HOLD_QUIT.kind != NO_EVENT, discard EVENT.
3795 Else, if EVENT is a quit event, store the quit event
3796 in HOLD_QUIT, and return (thus ignoring further events).
3798 This is used in read_avail_input to postpone the processing
3799 of the quit event until all subsequent input events have been
3800 parsed (and discarded).
3803 void
3804 kbd_buffer_store_event_hold (event, hold_quit)
3805 register struct input_event *event;
3806 struct input_event *hold_quit;
3808 if (event->kind == NO_EVENT)
3809 abort ();
3811 if (hold_quit && hold_quit->kind != NO_EVENT)
3812 return;
3814 if (event->kind == ASCII_KEYSTROKE_EVENT)
3816 register int c = event->code & 0377;
3818 if (event->modifiers & ctrl_modifier)
3819 c = make_ctrl_char (c);
3821 c |= (event->modifiers
3822 & (meta_modifier | alt_modifier
3823 | hyper_modifier | super_modifier));
3825 if (c == quit_char)
3827 #ifdef MULTI_KBOARD
3828 KBOARD *kb = FRAME_KBOARD (XFRAME (event->frame_or_window));
3829 struct input_event *sp;
3831 if (single_kboard && kb != current_kboard)
3833 kb->kbd_queue
3834 = Fcons (make_lispy_switch_frame (event->frame_or_window),
3835 Fcons (make_number (c), Qnil));
3836 kb->kbd_queue_has_data = 1;
3837 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3839 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3840 sp = kbd_buffer;
3842 if (event_to_kboard (sp) == kb)
3844 sp->kind = NO_EVENT;
3845 sp->frame_or_window = Qnil;
3846 sp->arg = Qnil;
3849 return;
3851 #endif
3853 if (hold_quit)
3855 bcopy (event, (char *) hold_quit, sizeof (*event));
3856 return;
3859 /* If this results in a quit_char being returned to Emacs as
3860 input, set Vlast_event_frame properly. If this doesn't
3861 get returned to Emacs as an event, the next event read
3862 will set Vlast_event_frame again, so this is safe to do. */
3864 Lisp_Object focus;
3866 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
3867 if (NILP (focus))
3868 focus = event->frame_or_window;
3869 internal_last_event_frame = focus;
3870 Vlast_event_frame = focus;
3873 last_event_timestamp = event->timestamp;
3874 handle_interrupt ();
3875 return;
3878 if (c && c == stop_character)
3880 sys_suspend ();
3881 return;
3884 /* Don't insert two BUFFER_SWITCH_EVENT's in a row.
3885 Just ignore the second one. */
3886 else if (event->kind == BUFFER_SWITCH_EVENT
3887 && kbd_fetch_ptr != kbd_store_ptr
3888 && ((kbd_store_ptr == kbd_buffer
3889 ? kbd_buffer + KBD_BUFFER_SIZE - 1
3890 : kbd_store_ptr - 1)->kind) == BUFFER_SWITCH_EVENT)
3891 return;
3893 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
3894 kbd_store_ptr = kbd_buffer;
3896 /* Don't let the very last slot in the buffer become full,
3897 since that would make the two pointers equal,
3898 and that is indistinguishable from an empty buffer.
3899 Discard the event if it would fill the last slot. */
3900 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
3902 *kbd_store_ptr = *event;
3903 ++kbd_store_ptr;
3906 /* If we're inside while-no-input, and this event qualifies
3907 as input, set quit-flag to cause an interrupt. */
3908 if (!NILP (Vthrow_on_input)
3909 && event->kind != FOCUS_IN_EVENT
3910 && event->kind != HELP_EVENT
3911 && event->kind != DEICONIFY_EVENT)
3913 Vquit_flag = Vthrow_on_input;
3914 /* If we're inside a function that wants immediate quits,
3915 do it now. */
3916 if (immediate_quit && NILP (Vinhibit_quit))
3918 immediate_quit = 0;
3919 sigfree ();
3920 QUIT;
3926 /* Put an input event back in the head of the event queue. */
3928 void
3929 kbd_buffer_unget_event (event)
3930 register struct input_event *event;
3932 if (kbd_fetch_ptr == kbd_buffer)
3933 kbd_fetch_ptr = kbd_buffer + KBD_BUFFER_SIZE;
3935 /* Don't let the very last slot in the buffer become full, */
3936 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
3938 --kbd_fetch_ptr;
3939 *kbd_fetch_ptr = *event;
3944 /* Generate HELP_EVENT input_events in BUFP which has room for
3945 SIZE events. If there's not enough room in BUFP, ignore this
3946 event.
3948 HELP is the help form.
3950 FRAME is the frame on which the help is generated. OBJECT is the
3951 Lisp object where the help was found (a buffer, a string, an
3952 overlay, or nil if neither from a string nor from a buffer. POS is
3953 the position within OBJECT where the help was found.
3955 Value is the number of input_events generated. */
3957 void
3958 gen_help_event (help, frame, window, object, pos)
3959 Lisp_Object help, frame, object, window;
3960 int pos;
3962 struct input_event event;
3964 EVENT_INIT (event);
3966 event.kind = HELP_EVENT;
3967 event.frame_or_window = frame;
3968 event.arg = object;
3969 event.x = WINDOWP (window) ? window : frame;
3970 event.y = help;
3971 event.code = pos;
3972 kbd_buffer_store_event (&event);
3976 /* Store HELP_EVENTs for HELP on FRAME in the input queue. */
3978 void
3979 kbd_buffer_store_help_event (frame, help)
3980 Lisp_Object frame, help;
3982 struct input_event event;
3984 event.kind = HELP_EVENT;
3985 event.frame_or_window = frame;
3986 event.arg = Qnil;
3987 event.x = Qnil;
3988 event.y = help;
3989 event.code = 0;
3990 kbd_buffer_store_event (&event);
3994 /* Discard any mouse events in the event buffer by setting them to
3995 NO_EVENT. */
3996 void
3997 discard_mouse_events ()
3999 struct input_event *sp;
4000 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
4002 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
4003 sp = kbd_buffer;
4005 if (sp->kind == MOUSE_CLICK_EVENT
4006 || sp->kind == WHEEL_EVENT
4007 || sp->kind == HORIZ_WHEEL_EVENT
4008 #ifdef HAVE_GPM
4009 || sp->kind == GPM_CLICK_EVENT
4010 #endif
4011 || sp->kind == SCROLL_BAR_CLICK_EVENT)
4013 sp->kind = NO_EVENT;
4019 /* Return non-zero if there are any real events waiting in the event
4020 buffer, not counting `NO_EVENT's.
4022 If DISCARD is non-zero, discard NO_EVENT events at the front of
4023 the input queue, possibly leaving the input queue empty if there
4024 are no real input events. */
4027 kbd_buffer_events_waiting (discard)
4028 int discard;
4030 struct input_event *sp;
4032 for (sp = kbd_fetch_ptr;
4033 sp != kbd_store_ptr && sp->kind == NO_EVENT;
4034 ++sp)
4036 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
4037 sp = kbd_buffer;
4040 if (discard)
4041 kbd_fetch_ptr = sp;
4043 return sp != kbd_store_ptr && sp->kind != NO_EVENT;
4047 /* Clear input event EVENT. */
4049 static INLINE void
4050 clear_event (event)
4051 struct input_event *event;
4053 event->kind = NO_EVENT;
4057 /* Read one event from the event buffer, waiting if necessary.
4058 The value is a Lisp object representing the event.
4059 The value is nil for an event that should be ignored,
4060 or that was handled here.
4061 We always read and discard one event. */
4063 static Lisp_Object
4064 kbd_buffer_get_event (kbp, used_mouse_menu, end_time)
4065 KBOARD **kbp;
4066 int *used_mouse_menu;
4067 EMACS_TIME *end_time;
4069 register int c;
4070 Lisp_Object obj;
4072 if (noninteractive)
4074 c = getchar ();
4075 XSETINT (obj, c);
4076 *kbp = current_kboard;
4077 return obj;
4080 /* Wait until there is input available. */
4081 for (;;)
4083 /* Break loop if there's an unread command event. Needed in
4084 moused window autoselection which uses a timer to insert such
4085 events. */
4086 if (CONSP (Vunread_command_events))
4087 break;
4089 if (kbd_fetch_ptr != kbd_store_ptr)
4090 break;
4091 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
4092 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
4093 break;
4094 #endif
4096 /* If the quit flag is set, then read_char will return
4097 quit_char, so that counts as "available input." */
4098 if (!NILP (Vquit_flag))
4099 quit_throw_to_read_char ();
4101 /* One way or another, wait until input is available; then, if
4102 interrupt handlers have not read it, read it now. */
4104 #ifdef OLDVMS
4105 wait_for_kbd_input ();
4106 #else
4107 /* Note SIGIO has been undef'd if FIONREAD is missing. */
4108 #ifdef SIGIO
4109 gobble_input (0);
4110 #endif /* SIGIO */
4111 if (kbd_fetch_ptr != kbd_store_ptr)
4112 break;
4113 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
4114 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
4115 break;
4116 #endif
4117 if (end_time)
4119 EMACS_TIME duration;
4120 EMACS_GET_TIME (duration);
4121 if (EMACS_TIME_GE (duration, *end_time))
4122 return Qnil; /* finished waiting */
4123 else
4125 EMACS_SUB_TIME (duration, *end_time, duration);
4126 wait_reading_process_output (EMACS_SECS (duration),
4127 EMACS_USECS (duration),
4128 -1, 1, Qnil, NULL, 0);
4131 else
4132 wait_reading_process_output (0, 0, -1, 1, Qnil, NULL, 0);
4134 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
4135 /* Pass 1 for EXPECT since we just waited to have input. */
4136 read_avail_input (1);
4137 #endif /* not VMS */
4140 if (CONSP (Vunread_command_events))
4142 Lisp_Object first;
4143 first = XCAR (Vunread_command_events);
4144 Vunread_command_events = XCDR (Vunread_command_events);
4145 *kbp = current_kboard;
4146 return first;
4149 /* At this point, we know that there is a readable event available
4150 somewhere. If the event queue is empty, then there must be a
4151 mouse movement enabled and available. */
4152 if (kbd_fetch_ptr != kbd_store_ptr)
4154 struct input_event *event;
4156 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
4157 ? kbd_fetch_ptr
4158 : kbd_buffer);
4160 last_event_timestamp = event->timestamp;
4162 #ifdef MULTI_KBOARD
4163 *kbp = event_to_kboard (event);
4164 if (*kbp == 0)
4165 *kbp = current_kboard; /* Better than returning null ptr? */
4166 #else
4167 *kbp = &the_only_kboard;
4168 #endif
4170 obj = Qnil;
4172 /* These two kinds of events get special handling
4173 and don't actually appear to the command loop.
4174 We return nil for them. */
4175 if (event->kind == SELECTION_REQUEST_EVENT
4176 || event->kind == SELECTION_CLEAR_EVENT)
4178 #ifdef HAVE_X11
4179 struct input_event copy;
4181 /* Remove it from the buffer before processing it,
4182 since otherwise swallow_events will see it
4183 and process it again. */
4184 copy = *event;
4185 kbd_fetch_ptr = event + 1;
4186 input_pending = readable_events (0);
4187 x_handle_selection_event (&copy);
4188 #else
4189 /* We're getting selection request events, but we don't have
4190 a window system. */
4191 abort ();
4192 #endif
4195 #if defined (HAVE_X11) || defined (HAVE_NTGUI) || defined (MAC_OS)
4196 else if (event->kind == DELETE_WINDOW_EVENT)
4198 /* Make an event (delete-frame (FRAME)). */
4199 obj = Fcons (event->frame_or_window, Qnil);
4200 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
4201 kbd_fetch_ptr = event + 1;
4203 #endif
4204 #if defined (HAVE_X11) || defined (HAVE_NTGUI) || defined (MAC_OS)
4205 else if (event->kind == ICONIFY_EVENT)
4207 /* Make an event (iconify-frame (FRAME)). */
4208 obj = Fcons (event->frame_or_window, Qnil);
4209 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
4210 kbd_fetch_ptr = event + 1;
4212 else if (event->kind == DEICONIFY_EVENT)
4214 /* Make an event (make-frame-visible (FRAME)). */
4215 obj = Fcons (event->frame_or_window, Qnil);
4216 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
4217 kbd_fetch_ptr = event + 1;
4219 #endif
4220 else if (event->kind == BUFFER_SWITCH_EVENT)
4222 /* The value doesn't matter here; only the type is tested. */
4223 XSETBUFFER (obj, current_buffer);
4224 kbd_fetch_ptr = event + 1;
4226 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
4227 || defined (USE_GTK)
4228 else if (event->kind == MENU_BAR_ACTIVATE_EVENT)
4230 kbd_fetch_ptr = event + 1;
4231 input_pending = readable_events (0);
4232 if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
4233 x_activate_menubar (XFRAME (event->frame_or_window));
4235 #endif
4236 #if defined (WINDOWSNT) || defined (MAC_OS)
4237 else if (event->kind == LANGUAGE_CHANGE_EVENT)
4239 #ifdef MAC_OS
4240 /* Make an event (language-change (KEY_SCRIPT)). */
4241 obj = Fcons (make_number (event->code), Qnil);
4242 #else
4243 /* Make an event (language-change (FRAME CHARSET LCID)). */
4244 obj = Fcons (event->frame_or_window, Qnil);
4245 #endif
4246 obj = Fcons (Qlanguage_change, Fcons (obj, Qnil));
4247 kbd_fetch_ptr = event + 1;
4249 #endif
4250 else if (event->kind == SAVE_SESSION_EVENT)
4252 obj = Fcons (Qsave_session, Qnil);
4253 kbd_fetch_ptr = event + 1;
4255 /* Just discard these, by returning nil.
4256 With MULTI_KBOARD, these events are used as placeholders
4257 when we need to randomly delete events from the queue.
4258 (They shouldn't otherwise be found in the buffer,
4259 but on some machines it appears they do show up
4260 even without MULTI_KBOARD.) */
4261 /* On Windows NT/9X, NO_EVENT is used to delete extraneous
4262 mouse events during a popup-menu call. */
4263 else if (event->kind == NO_EVENT)
4264 kbd_fetch_ptr = event + 1;
4265 else if (event->kind == HELP_EVENT)
4267 Lisp_Object object, position, help, frame, window;
4269 frame = event->frame_or_window;
4270 object = event->arg;
4271 position = make_number (event->code);
4272 window = event->x;
4273 help = event->y;
4274 clear_event (event);
4276 kbd_fetch_ptr = event + 1;
4277 if (!WINDOWP (window))
4278 window = Qnil;
4279 obj = Fcons (Qhelp_echo,
4280 list5 (frame, help, window, object, position));
4282 else if (event->kind == FOCUS_IN_EVENT)
4284 /* Notification of a FocusIn event. The frame receiving the
4285 focus is in event->frame_or_window. Generate a
4286 switch-frame event if necessary. */
4287 Lisp_Object frame, focus;
4289 frame = event->frame_or_window;
4290 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
4291 if (FRAMEP (focus))
4292 frame = focus;
4294 if (!EQ (frame, internal_last_event_frame)
4295 && !EQ (frame, selected_frame))
4296 obj = make_lispy_switch_frame (frame);
4297 internal_last_event_frame = frame;
4298 kbd_fetch_ptr = event + 1;
4300 #ifdef HAVE_DBUS
4301 else if (event->kind == DBUS_EVENT)
4303 obj = make_lispy_event (event);
4304 kbd_fetch_ptr = event + 1;
4306 #endif
4307 else
4309 /* If this event is on a different frame, return a switch-frame this
4310 time, and leave the event in the queue for next time. */
4311 Lisp_Object frame;
4312 Lisp_Object focus;
4314 frame = event->frame_or_window;
4315 if (CONSP (frame))
4316 frame = XCAR (frame);
4317 else if (WINDOWP (frame))
4318 frame = WINDOW_FRAME (XWINDOW (frame));
4320 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
4321 if (! NILP (focus))
4322 frame = focus;
4324 if (! EQ (frame, internal_last_event_frame)
4325 && !EQ (frame, selected_frame))
4326 obj = make_lispy_switch_frame (frame);
4327 internal_last_event_frame = frame;
4329 /* If we didn't decide to make a switch-frame event, go ahead
4330 and build a real event from the queue entry. */
4332 if (NILP (obj))
4334 obj = make_lispy_event (event);
4336 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined(MAC_OS) \
4337 || defined (USE_GTK)
4338 /* If this was a menu selection, then set the flag to inhibit
4339 writing to last_nonmenu_event. Don't do this if the event
4340 we're returning is (menu-bar), though; that indicates the
4341 beginning of the menu sequence, and we might as well leave
4342 that as the `event with parameters' for this selection. */
4343 if (used_mouse_menu
4344 && !EQ (event->frame_or_window, event->arg)
4345 && (event->kind == MENU_BAR_EVENT
4346 || event->kind == TOOL_BAR_EVENT))
4347 *used_mouse_menu = 1;
4348 #endif
4350 /* Wipe out this event, to catch bugs. */
4351 clear_event (event);
4352 kbd_fetch_ptr = event + 1;
4356 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
4357 /* Try generating a mouse motion event. */
4358 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
4360 FRAME_PTR f = some_mouse_moved ();
4361 Lisp_Object bar_window;
4362 enum scroll_bar_part part;
4363 Lisp_Object x, y;
4364 unsigned long time;
4366 *kbp = current_kboard;
4367 /* Note that this uses F to determine which terminal to look at.
4368 If there is no valid info, it does not store anything
4369 so x remains nil. */
4370 x = Qnil;
4372 /* XXX Can f or mouse_position_hook be NULL here? */
4373 if (f && FRAME_TERMINAL (f)->mouse_position_hook)
4374 (*FRAME_TERMINAL (f)->mouse_position_hook) (&f, 0, &bar_window,
4375 &part, &x, &y, &time);
4377 obj = Qnil;
4379 /* Decide if we should generate a switch-frame event. Don't
4380 generate switch-frame events for motion outside of all Emacs
4381 frames. */
4382 if (!NILP (x) && f)
4384 Lisp_Object frame;
4386 frame = FRAME_FOCUS_FRAME (f);
4387 if (NILP (frame))
4388 XSETFRAME (frame, f);
4390 if (! EQ (frame, internal_last_event_frame)
4391 && !EQ (frame, selected_frame))
4392 obj = make_lispy_switch_frame (frame);
4393 internal_last_event_frame = frame;
4396 /* If we didn't decide to make a switch-frame event, go ahead and
4397 return a mouse-motion event. */
4398 if (!NILP (x) && NILP (obj))
4399 obj = make_lispy_movement (f, bar_window, part, x, y, time);
4401 #endif /* HAVE_MOUSE || HAVE GPM */
4402 else
4403 /* We were promised by the above while loop that there was
4404 something for us to read! */
4405 abort ();
4407 input_pending = readable_events (0);
4409 Vlast_event_frame = internal_last_event_frame;
4411 return (obj);
4414 /* Process any events that are not user-visible,
4415 then return, without reading any user-visible events. */
4417 void
4418 swallow_events (do_display)
4419 int do_display;
4421 int old_timers_run;
4423 while (kbd_fetch_ptr != kbd_store_ptr)
4425 struct input_event *event;
4427 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
4428 ? kbd_fetch_ptr
4429 : kbd_buffer);
4431 last_event_timestamp = event->timestamp;
4433 /* These two kinds of events get special handling
4434 and don't actually appear to the command loop. */
4435 if (event->kind == SELECTION_REQUEST_EVENT
4436 || event->kind == SELECTION_CLEAR_EVENT)
4438 #ifdef HAVE_X11
4439 struct input_event copy;
4441 /* Remove it from the buffer before processing it,
4442 since otherwise swallow_events called recursively could see it
4443 and process it again. */
4444 copy = *event;
4445 kbd_fetch_ptr = event + 1;
4446 input_pending = readable_events (0);
4447 x_handle_selection_event (&copy);
4448 #else
4449 /* We're getting selection request events, but we don't have
4450 a window system. */
4451 abort ();
4452 #endif
4454 else
4455 break;
4458 old_timers_run = timers_run;
4459 get_input_pending (&input_pending, READABLE_EVENTS_DO_TIMERS_NOW);
4461 if (timers_run != old_timers_run && do_display)
4462 redisplay_preserve_echo_area (7);
4465 /* Record the start of when Emacs is idle,
4466 for the sake of running idle-time timers. */
4468 static void
4469 timer_start_idle ()
4471 Lisp_Object timers;
4473 /* If we are already in the idle state, do nothing. */
4474 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4475 return;
4477 EMACS_GET_TIME (timer_idleness_start_time);
4479 timer_last_idleness_start_time = timer_idleness_start_time;
4481 /* Mark all idle-time timers as once again candidates for running. */
4482 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCDR (timers))
4484 Lisp_Object timer;
4486 timer = XCAR (timers);
4488 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
4489 continue;
4490 XVECTOR (timer)->contents[0] = Qnil;
4494 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
4496 static void
4497 timer_stop_idle ()
4499 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
4502 /* Resume idle timer from last idle start time. */
4504 static void
4505 timer_resume_idle ()
4507 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4508 return;
4510 timer_idleness_start_time = timer_last_idleness_start_time;
4513 /* This is only for debugging. */
4514 struct input_event last_timer_event;
4516 /* List of elisp functions to call, delayed because they were generated in
4517 a context where Elisp could not be safely run (e.g. redisplay, signal,
4518 ...). Each lement has the form (FUN . ARGS). */
4519 Lisp_Object pending_funcalls;
4521 extern Lisp_Object Qapply;
4523 /* Check whether a timer has fired. To prevent larger problems we simply
4524 disregard elements that are not proper timers. Do not make a circular
4525 timer list for the time being.
4527 Returns the number of seconds to wait until the next timer fires. If a
4528 timer is triggering now, return zero seconds.
4529 If no timer is active, return -1 seconds.
4531 If a timer is ripe, we run it, with quitting turned off.
4533 DO_IT_NOW is now ignored. It used to mean that we should
4534 run the timer directly instead of queueing a timer-event.
4535 Now we always run timers directly. */
4537 EMACS_TIME
4538 timer_check (do_it_now)
4539 int do_it_now;
4541 EMACS_TIME nexttime;
4542 EMACS_TIME now, idleness_now;
4543 Lisp_Object timers, idle_timers, chosen_timer;
4544 struct gcpro gcpro1, gcpro2, gcpro3;
4546 EMACS_SET_SECS (nexttime, -1);
4547 EMACS_SET_USECS (nexttime, -1);
4549 /* Always consider the ordinary timers. */
4550 timers = Vtimer_list;
4551 /* Consider the idle timers only if Emacs is idle. */
4552 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4553 idle_timers = Vtimer_idle_list;
4554 else
4555 idle_timers = Qnil;
4556 chosen_timer = Qnil;
4557 GCPRO3 (timers, idle_timers, chosen_timer);
4559 /* First run the code that was delayed. */
4560 while (CONSP (pending_funcalls))
4562 Lisp_Object funcall = XCAR (pending_funcalls);
4563 pending_funcalls = XCDR (pending_funcalls);
4564 safe_call2 (Qapply, XCAR (funcall), XCDR (funcall));
4567 if (CONSP (timers) || CONSP (idle_timers))
4569 EMACS_GET_TIME (now);
4570 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4571 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
4574 while (CONSP (timers) || CONSP (idle_timers))
4576 Lisp_Object *vector;
4577 Lisp_Object timer = Qnil, idle_timer = Qnil;
4578 EMACS_TIME timer_time, idle_timer_time;
4579 EMACS_TIME difference, timer_difference, idle_timer_difference;
4581 /* Skip past invalid timers and timers already handled. */
4582 if (!NILP (timers))
4584 timer = XCAR (timers);
4585 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
4587 timers = XCDR (timers);
4588 continue;
4590 vector = XVECTOR (timer)->contents;
4592 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
4593 || !INTEGERP (vector[3])
4594 || ! NILP (vector[0]))
4596 timers = XCDR (timers);
4597 continue;
4600 if (!NILP (idle_timers))
4602 timer = XCAR (idle_timers);
4603 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
4605 idle_timers = XCDR (idle_timers);
4606 continue;
4608 vector = XVECTOR (timer)->contents;
4610 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
4611 || !INTEGERP (vector[3])
4612 || ! NILP (vector[0]))
4614 idle_timers = XCDR (idle_timers);
4615 continue;
4619 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
4620 based on the next ordinary timer.
4621 TIMER_DIFFERENCE is the distance in time from NOW to when
4622 this timer becomes ripe (negative if it's already ripe). */
4623 if (!NILP (timers))
4625 timer = XCAR (timers);
4626 vector = XVECTOR (timer)->contents;
4627 EMACS_SET_SECS (timer_time,
4628 (XINT (vector[1]) << 16) | (XINT (vector[2])));
4629 EMACS_SET_USECS (timer_time, XINT (vector[3]));
4630 EMACS_SUB_TIME (timer_difference, timer_time, now);
4633 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
4634 based on the next idle timer. */
4635 if (!NILP (idle_timers))
4637 idle_timer = XCAR (idle_timers);
4638 vector = XVECTOR (idle_timer)->contents;
4639 EMACS_SET_SECS (idle_timer_time,
4640 (XINT (vector[1]) << 16) | (XINT (vector[2])));
4641 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
4642 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
4645 /* Decide which timer is the next timer,
4646 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
4647 Also step down the list where we found that timer. */
4649 if (! NILP (timers) && ! NILP (idle_timers))
4651 EMACS_TIME temp;
4652 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
4653 if (EMACS_TIME_NEG_P (temp))
4655 chosen_timer = timer;
4656 timers = XCDR (timers);
4657 difference = timer_difference;
4659 else
4661 chosen_timer = idle_timer;
4662 idle_timers = XCDR (idle_timers);
4663 difference = idle_timer_difference;
4666 else if (! NILP (timers))
4668 chosen_timer = timer;
4669 timers = XCDR (timers);
4670 difference = timer_difference;
4672 else
4674 chosen_timer = idle_timer;
4675 idle_timers = XCDR (idle_timers);
4676 difference = idle_timer_difference;
4678 vector = XVECTOR (chosen_timer)->contents;
4680 /* If timer is ripe, run it if it hasn't been run. */
4681 if (EMACS_TIME_NEG_P (difference)
4682 || (EMACS_SECS (difference) == 0
4683 && EMACS_USECS (difference) == 0))
4685 if (NILP (vector[0]))
4687 int count = SPECPDL_INDEX ();
4688 Lisp_Object old_deactivate_mark = Vdeactivate_mark;
4690 #if 0 /* This shouldn't be necessary anymore. --lorentey */
4691 /* On unbind_to, resume allowing input from any kboard, if that
4692 was true before. */
4693 record_single_kboard_state ();
4694 #endif
4695 /* Mark the timer as triggered to prevent problems if the lisp
4696 code fails to reschedule it right. */
4697 vector[0] = Qt;
4699 specbind (Qinhibit_quit, Qt);
4701 call1 (Qtimer_event_handler, chosen_timer);
4702 Vdeactivate_mark = old_deactivate_mark;
4703 timers_run++;
4704 unbind_to (count, Qnil);
4706 /* Since we have handled the event,
4707 we don't need to tell the caller to wake up and do it. */
4710 else
4711 /* When we encounter a timer that is still waiting,
4712 return the amount of time to wait before it is ripe. */
4714 UNGCPRO;
4715 return difference;
4719 /* No timers are pending in the future. */
4720 /* Return 0 if we generated an event, and -1 if not. */
4721 UNGCPRO;
4722 return nexttime;
4725 DEFUN ("current-idle-time", Fcurrent_idle_time, Scurrent_idle_time, 0, 0, 0,
4726 doc: /* Return the current length of Emacs idleness, or nil.
4727 The value when Emacs is idle is a list of three integers. The first has the
4728 most significant 16 bits of the seconds, while the second has the
4729 least significant 16 bits. The third integer gives the microsecond
4730 count.
4732 The value when Emacs is not idle is nil.
4734 The microsecond count is zero on systems that do not provide
4735 resolution finer than a second. */)
4738 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4740 EMACS_TIME now, idleness_now;
4742 EMACS_GET_TIME (now);
4743 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
4745 return list3 (make_number ((EMACS_SECS (idleness_now) >> 16) & 0xffff),
4746 make_number ((EMACS_SECS (idleness_now) >> 0) & 0xffff),
4747 make_number (EMACS_USECS (idleness_now)));
4750 return Qnil;
4753 /* Caches for modify_event_symbol. */
4754 static Lisp_Object accent_key_syms;
4755 static Lisp_Object func_key_syms;
4756 static Lisp_Object mouse_syms;
4757 static Lisp_Object wheel_syms;
4758 static Lisp_Object drag_n_drop_syms;
4760 /* This is a list of keysym codes for special "accent" characters.
4761 It parallels lispy_accent_keys. */
4763 static int lispy_accent_codes[] =
4765 #ifdef XK_dead_circumflex
4766 XK_dead_circumflex,
4767 #else
4769 #endif
4770 #ifdef XK_dead_grave
4771 XK_dead_grave,
4772 #else
4774 #endif
4775 #ifdef XK_dead_tilde
4776 XK_dead_tilde,
4777 #else
4779 #endif
4780 #ifdef XK_dead_diaeresis
4781 XK_dead_diaeresis,
4782 #else
4784 #endif
4785 #ifdef XK_dead_macron
4786 XK_dead_macron,
4787 #else
4789 #endif
4790 #ifdef XK_dead_degree
4791 XK_dead_degree,
4792 #else
4794 #endif
4795 #ifdef XK_dead_acute
4796 XK_dead_acute,
4797 #else
4799 #endif
4800 #ifdef XK_dead_cedilla
4801 XK_dead_cedilla,
4802 #else
4804 #endif
4805 #ifdef XK_dead_breve
4806 XK_dead_breve,
4807 #else
4809 #endif
4810 #ifdef XK_dead_ogonek
4811 XK_dead_ogonek,
4812 #else
4814 #endif
4815 #ifdef XK_dead_caron
4816 XK_dead_caron,
4817 #else
4819 #endif
4820 #ifdef XK_dead_doubleacute
4821 XK_dead_doubleacute,
4822 #else
4824 #endif
4825 #ifdef XK_dead_abovedot
4826 XK_dead_abovedot,
4827 #else
4829 #endif
4830 #ifdef XK_dead_abovering
4831 XK_dead_abovering,
4832 #else
4834 #endif
4835 #ifdef XK_dead_iota
4836 XK_dead_iota,
4837 #else
4839 #endif
4840 #ifdef XK_dead_belowdot
4841 XK_dead_belowdot,
4842 #else
4844 #endif
4845 #ifdef XK_dead_voiced_sound
4846 XK_dead_voiced_sound,
4847 #else
4849 #endif
4850 #ifdef XK_dead_semivoiced_sound
4851 XK_dead_semivoiced_sound,
4852 #else
4854 #endif
4855 #ifdef XK_dead_hook
4856 XK_dead_hook,
4857 #else
4859 #endif
4860 #ifdef XK_dead_horn
4861 XK_dead_horn,
4862 #else
4864 #endif
4867 /* This is a list of Lisp names for special "accent" characters.
4868 It parallels lispy_accent_codes. */
4870 static char *lispy_accent_keys[] =
4872 "dead-circumflex",
4873 "dead-grave",
4874 "dead-tilde",
4875 "dead-diaeresis",
4876 "dead-macron",
4877 "dead-degree",
4878 "dead-acute",
4879 "dead-cedilla",
4880 "dead-breve",
4881 "dead-ogonek",
4882 "dead-caron",
4883 "dead-doubleacute",
4884 "dead-abovedot",
4885 "dead-abovering",
4886 "dead-iota",
4887 "dead-belowdot",
4888 "dead-voiced-sound",
4889 "dead-semivoiced-sound",
4890 "dead-hook",
4891 "dead-horn",
4894 #ifdef HAVE_NTGUI
4895 #define FUNCTION_KEY_OFFSET 0x0
4897 char *lispy_function_keys[] =
4899 0, /* 0 */
4901 0, /* VK_LBUTTON 0x01 */
4902 0, /* VK_RBUTTON 0x02 */
4903 "cancel", /* VK_CANCEL 0x03 */
4904 0, /* VK_MBUTTON 0x04 */
4906 0, 0, 0, /* 0x05 .. 0x07 */
4908 "backspace", /* VK_BACK 0x08 */
4909 "tab", /* VK_TAB 0x09 */
4911 0, 0, /* 0x0A .. 0x0B */
4913 "clear", /* VK_CLEAR 0x0C */
4914 "return", /* VK_RETURN 0x0D */
4916 0, 0, /* 0x0E .. 0x0F */
4918 0, /* VK_SHIFT 0x10 */
4919 0, /* VK_CONTROL 0x11 */
4920 0, /* VK_MENU 0x12 */
4921 "pause", /* VK_PAUSE 0x13 */
4922 "capslock", /* VK_CAPITAL 0x14 */
4923 "kana", /* VK_KANA/VK_HANGUL 0x15 */
4924 0, /* 0x16 */
4925 "junja", /* VK_JUNJA 0x17 */
4926 "final", /* VK_FINAL 0x18 */
4927 "kanji", /* VK_KANJI/VK_HANJA 0x19 */
4928 0, /* 0x1A */
4929 "escape", /* VK_ESCAPE 0x1B */
4930 "convert", /* VK_CONVERT 0x1C */
4931 "non-convert", /* VK_NONCONVERT 0x1D */
4932 "accept", /* VK_ACCEPT 0x1E */
4933 "mode-change", /* VK_MODECHANGE 0x1F */
4934 0, /* VK_SPACE 0x20 */
4935 "prior", /* VK_PRIOR 0x21 */
4936 "next", /* VK_NEXT 0x22 */
4937 "end", /* VK_END 0x23 */
4938 "home", /* VK_HOME 0x24 */
4939 "left", /* VK_LEFT 0x25 */
4940 "up", /* VK_UP 0x26 */
4941 "right", /* VK_RIGHT 0x27 */
4942 "down", /* VK_DOWN 0x28 */
4943 "select", /* VK_SELECT 0x29 */
4944 "print", /* VK_PRINT 0x2A */
4945 "execute", /* VK_EXECUTE 0x2B */
4946 "snapshot", /* VK_SNAPSHOT 0x2C */
4947 "insert", /* VK_INSERT 0x2D */
4948 "delete", /* VK_DELETE 0x2E */
4949 "help", /* VK_HELP 0x2F */
4951 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
4953 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4955 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
4957 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
4959 0, 0, 0, 0, 0, 0, 0, 0, 0,
4960 0, 0, 0, 0, 0, 0, 0, 0, 0,
4961 0, 0, 0, 0, 0, 0, 0, 0,
4963 "lwindow", /* VK_LWIN 0x5B */
4964 "rwindow", /* VK_RWIN 0x5C */
4965 "apps", /* VK_APPS 0x5D */
4966 0, /* 0x5E */
4967 "sleep",
4968 "kp-0", /* VK_NUMPAD0 0x60 */
4969 "kp-1", /* VK_NUMPAD1 0x61 */
4970 "kp-2", /* VK_NUMPAD2 0x62 */
4971 "kp-3", /* VK_NUMPAD3 0x63 */
4972 "kp-4", /* VK_NUMPAD4 0x64 */
4973 "kp-5", /* VK_NUMPAD5 0x65 */
4974 "kp-6", /* VK_NUMPAD6 0x66 */
4975 "kp-7", /* VK_NUMPAD7 0x67 */
4976 "kp-8", /* VK_NUMPAD8 0x68 */
4977 "kp-9", /* VK_NUMPAD9 0x69 */
4978 "kp-multiply", /* VK_MULTIPLY 0x6A */
4979 "kp-add", /* VK_ADD 0x6B */
4980 "kp-separator", /* VK_SEPARATOR 0x6C */
4981 "kp-subtract", /* VK_SUBTRACT 0x6D */
4982 "kp-decimal", /* VK_DECIMAL 0x6E */
4983 "kp-divide", /* VK_DIVIDE 0x6F */
4984 "f1", /* VK_F1 0x70 */
4985 "f2", /* VK_F2 0x71 */
4986 "f3", /* VK_F3 0x72 */
4987 "f4", /* VK_F4 0x73 */
4988 "f5", /* VK_F5 0x74 */
4989 "f6", /* VK_F6 0x75 */
4990 "f7", /* VK_F7 0x76 */
4991 "f8", /* VK_F8 0x77 */
4992 "f9", /* VK_F9 0x78 */
4993 "f10", /* VK_F10 0x79 */
4994 "f11", /* VK_F11 0x7A */
4995 "f12", /* VK_F12 0x7B */
4996 "f13", /* VK_F13 0x7C */
4997 "f14", /* VK_F14 0x7D */
4998 "f15", /* VK_F15 0x7E */
4999 "f16", /* VK_F16 0x7F */
5000 "f17", /* VK_F17 0x80 */
5001 "f18", /* VK_F18 0x81 */
5002 "f19", /* VK_F19 0x82 */
5003 "f20", /* VK_F20 0x83 */
5004 "f21", /* VK_F21 0x84 */
5005 "f22", /* VK_F22 0x85 */
5006 "f23", /* VK_F23 0x86 */
5007 "f24", /* VK_F24 0x87 */
5009 0, 0, 0, 0, /* 0x88 .. 0x8B */
5010 0, 0, 0, 0, /* 0x8C .. 0x8F */
5012 "kp-numlock", /* VK_NUMLOCK 0x90 */
5013 "scroll", /* VK_SCROLL 0x91 */
5014 /* Not sure where the following block comes from.
5015 Windows headers have NEC and Fujitsu specific keys in
5016 this block, but nothing generic. */
5017 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
5018 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
5019 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
5020 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
5021 "kp-end", /* VK_NUMPAD_END 0x96 */
5022 "kp-home", /* VK_NUMPAD_HOME 0x97 */
5023 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
5024 "kp-up", /* VK_NUMPAD_UP 0x99 */
5025 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
5026 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
5027 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
5028 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
5030 0, 0, /* 0x9E .. 0x9F */
5033 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
5034 * Used only as parameters to GetAsyncKeyState and GetKeyState.
5035 * No other API or message will distinguish left and right keys this way.
5036 * 0xA0 .. 0xA5
5038 0, 0, 0, 0, 0, 0,
5040 /* Multimedia keys. These are handled as WM_APPCOMMAND, which allows us
5041 to enable them selectively, and gives access to a few more functions.
5042 See lispy_multimedia_keys below. */
5043 0, 0, 0, 0, 0, 0, 0, /* 0xA6 .. 0xAC Browser */
5044 0, 0, 0, /* 0xAD .. 0xAF Volume */
5045 0, 0, 0, 0, /* 0xB0 .. 0xB3 Media */
5046 0, 0, 0, 0, /* 0xB4 .. 0xB7 Apps */
5048 /* 0xB8 .. 0xC0 "OEM" keys - all seem to be punctuation. */
5049 0, 0, 0, 0, 0, 0, 0, 0, 0,
5051 /* 0xC1 - 0xDA unallocated, 0xDB-0xDF more OEM keys */
5052 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5053 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5055 0, /* 0xE0 */
5056 "ax", /* VK_OEM_AX 0xE1 */
5057 0, /* VK_OEM_102 0xE2 */
5058 "ico-help", /* VK_ICO_HELP 0xE3 */
5059 "ico-00", /* VK_ICO_00 0xE4 */
5060 0, /* VK_PROCESSKEY 0xE5 */
5061 "ico-clear", /* VK_ICO_CLEAR 0xE6 */
5062 "packet", /* VK_PACKET 0xE7 */
5063 0, /* 0xE8 */
5064 "reset", /* VK_OEM_RESET 0xE9 */
5065 "jump", /* VK_OEM_JUMP 0xEA */
5066 "oem-pa1", /* VK_OEM_PA1 0xEB */
5067 "oem-pa2", /* VK_OEM_PA2 0xEC */
5068 "oem-pa3", /* VK_OEM_PA3 0xED */
5069 "wsctrl", /* VK_OEM_WSCTRL 0xEE */
5070 "cusel", /* VK_OEM_CUSEL 0xEF */
5071 "oem-attn", /* VK_OEM_ATTN 0xF0 */
5072 "finish", /* VK_OEM_FINISH 0xF1 */
5073 "copy", /* VK_OEM_COPY 0xF2 */
5074 "auto", /* VK_OEM_AUTO 0xF3 */
5075 "enlw", /* VK_OEM_ENLW 0xF4 */
5076 "backtab", /* VK_OEM_BACKTAB 0xF5 */
5077 "attn", /* VK_ATTN 0xF6 */
5078 "crsel", /* VK_CRSEL 0xF7 */
5079 "exsel", /* VK_EXSEL 0xF8 */
5080 "ereof", /* VK_EREOF 0xF9 */
5081 "play", /* VK_PLAY 0xFA */
5082 "zoom", /* VK_ZOOM 0xFB */
5083 "noname", /* VK_NONAME 0xFC */
5084 "pa1", /* VK_PA1 0xFD */
5085 "oem_clear", /* VK_OEM_CLEAR 0xFE */
5086 0 /* 0xFF */
5089 /* Some of these duplicate the "Media keys" on newer keyboards,
5090 but they are delivered to the application in a different way. */
5091 static char *lispy_multimedia_keys[] =
5094 "browser-back",
5095 "browser-forward",
5096 "browser-refresh",
5097 "browser-stop",
5098 "browser-search",
5099 "browser-favorites",
5100 "browser-home",
5101 "volume-mute",
5102 "volume-down",
5103 "volume-up",
5104 "media-next",
5105 "media-previous",
5106 "media-stop",
5107 "media-play-pause",
5108 "mail",
5109 "media-select",
5110 "app-1",
5111 "app-2",
5112 "bass-down",
5113 "bass-boost",
5114 "bass-up",
5115 "treble-down",
5116 "treble-up",
5117 "mic-volume-mute",
5118 "mic-volume-down",
5119 "mic-volume-up",
5120 "help",
5121 "find",
5122 "new",
5123 "open",
5124 "close",
5125 "save",
5126 "print",
5127 "undo",
5128 "redo",
5129 "copy",
5130 "cut",
5131 "paste",
5132 "mail-reply",
5133 "mail-forward",
5134 "mail-send",
5135 "spell-check",
5136 "toggle-dictate-command",
5137 "mic-toggle",
5138 "correction-list",
5139 "media-play",
5140 "media-pause",
5141 "media-record",
5142 "media-fast-forward",
5143 "media-rewind",
5144 "media-channel-up",
5145 "media-channel-down"
5148 #else /* not HAVE_NTGUI */
5150 /* This should be dealt with in XTread_socket now, and that doesn't
5151 depend on the client system having the Kana syms defined. See also
5152 the XK_kana_A case below. */
5153 #if 0
5154 #ifdef XK_kana_A
5155 static char *lispy_kana_keys[] =
5157 /* X Keysym value */
5158 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x400 .. 0x40f */
5159 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x410 .. 0x41f */
5160 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x420 .. 0x42f */
5161 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x430 .. 0x43f */
5162 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x440 .. 0x44f */
5163 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x450 .. 0x45f */
5164 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x460 .. 0x46f */
5165 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0,
5166 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x480 .. 0x48f */
5167 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x490 .. 0x49f */
5168 0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket",
5169 "kana-comma", "kana-conjunctive", "kana-WO", "kana-a",
5170 "kana-i", "kana-u", "kana-e", "kana-o",
5171 "kana-ya", "kana-yu", "kana-yo", "kana-tsu",
5172 "prolongedsound", "kana-A", "kana-I", "kana-U",
5173 "kana-E", "kana-O", "kana-KA", "kana-KI",
5174 "kana-KU", "kana-KE", "kana-KO", "kana-SA",
5175 "kana-SHI", "kana-SU", "kana-SE", "kana-SO",
5176 "kana-TA", "kana-CHI", "kana-TSU", "kana-TE",
5177 "kana-TO", "kana-NA", "kana-NI", "kana-NU",
5178 "kana-NE", "kana-NO", "kana-HA", "kana-HI",
5179 "kana-FU", "kana-HE", "kana-HO", "kana-MA",
5180 "kana-MI", "kana-MU", "kana-ME", "kana-MO",
5181 "kana-YA", "kana-YU", "kana-YO", "kana-RA",
5182 "kana-RI", "kana-RU", "kana-RE", "kana-RO",
5183 "kana-WA", "kana-N", "voicedsound", "semivoicedsound",
5184 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4e0 .. 0x4ef */
5185 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f0 .. 0x4ff */
5187 #endif /* XK_kana_A */
5188 #endif /* 0 */
5190 #define FUNCTION_KEY_OFFSET 0xff00
5192 /* You'll notice that this table is arranged to be conveniently
5193 indexed by X Windows keysym values. */
5194 static char *lispy_function_keys[] =
5196 /* X Keysym value */
5198 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00...0f */
5199 "backspace", "tab", "linefeed", "clear",
5200 0, "return", 0, 0,
5201 0, 0, 0, "pause", /* 0xff10...1f */
5202 0, 0, 0, 0, 0, 0, 0, "escape",
5203 0, 0, 0, 0,
5204 0, "kanji", "muhenkan", "henkan", /* 0xff20...2f */
5205 "romaji", "hiragana", "katakana", "hiragana-katakana",
5206 "zenkaku", "hankaku", "zenkaku-hankaku", "touroku",
5207 "massyo", "kana-lock", "kana-shift", "eisu-shift",
5208 "eisu-toggle", /* 0xff30...3f */
5209 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5210 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
5212 "home", "left", "up", "right", /* 0xff50 */ /* IsCursorKey */
5213 "down", "prior", "next", "end",
5214 "begin", 0, 0, 0, 0, 0, 0, 0,
5215 "select", /* 0xff60 */ /* IsMiscFunctionKey */
5216 "print",
5217 "execute",
5218 "insert",
5219 0, /* 0xff64 */
5220 "undo",
5221 "redo",
5222 "menu",
5223 "find",
5224 "cancel",
5225 "help",
5226 "break", /* 0xff6b */
5228 0, 0, 0, 0,
5229 0, 0, 0, 0, "backtab", 0, 0, 0, /* 0xff70... */
5230 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff78... */
5231 "kp-space", /* 0xff80 */ /* IsKeypadKey */
5232 0, 0, 0, 0, 0, 0, 0, 0,
5233 "kp-tab", /* 0xff89 */
5234 0, 0, 0,
5235 "kp-enter", /* 0xff8d */
5236 0, 0, 0,
5237 "kp-f1", /* 0xff91 */
5238 "kp-f2",
5239 "kp-f3",
5240 "kp-f4",
5241 "kp-home", /* 0xff95 */
5242 "kp-left",
5243 "kp-up",
5244 "kp-right",
5245 "kp-down",
5246 "kp-prior", /* kp-page-up */
5247 "kp-next", /* kp-page-down */
5248 "kp-end",
5249 "kp-begin",
5250 "kp-insert",
5251 "kp-delete",
5252 0, /* 0xffa0 */
5253 0, 0, 0, 0, 0, 0, 0, 0, 0,
5254 "kp-multiply", /* 0xffaa */
5255 "kp-add",
5256 "kp-separator",
5257 "kp-subtract",
5258 "kp-decimal",
5259 "kp-divide", /* 0xffaf */
5260 "kp-0", /* 0xffb0 */
5261 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
5262 0, /* 0xffba */
5263 0, 0,
5264 "kp-equal", /* 0xffbd */
5265 "f1", /* 0xffbe */ /* IsFunctionKey */
5266 "f2",
5267 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
5268 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
5269 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
5270 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
5271 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
5272 0, 0, 0, 0, 0, 0, 0, 0,
5273 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
5274 0, 0, 0, 0, 0, 0, 0, "delete"
5277 /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE. */
5278 #define ISO_FUNCTION_KEY_OFFSET 0xfe00
5280 static char *iso_lispy_function_keys[] =
5282 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe00 */
5283 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe08 */
5284 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe10 */
5285 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe18 */
5286 "iso-lefttab", /* 0xfe20 */
5287 "iso-move-line-up", "iso-move-line-down",
5288 "iso-partial-line-up", "iso-partial-line-down",
5289 "iso-partial-space-left", "iso-partial-space-right",
5290 "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */
5291 "iso-release-margin-left", "iso-release-margin-right",
5292 "iso-release-both-margins",
5293 "iso-fast-cursor-left", "iso-fast-cursor-right",
5294 "iso-fast-cursor-up", "iso-fast-cursor-down",
5295 "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */
5296 "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */
5299 #endif /* not HAVE_NTGUI */
5301 Lisp_Object Vlispy_mouse_stem;
5303 static char *lispy_wheel_names[] =
5305 "wheel-up", "wheel-down", "wheel-left", "wheel-right"
5308 /* drag-n-drop events are generated when a set of selected files are
5309 dragged from another application and dropped onto an Emacs window. */
5310 static char *lispy_drag_n_drop_names[] =
5312 "drag-n-drop"
5315 /* Scroll bar parts. */
5316 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
5317 Lisp_Object Qup, Qdown, Qbottom, Qend_scroll;
5318 Lisp_Object Qtop, Qratio;
5320 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
5321 Lisp_Object *scroll_bar_parts[] = {
5322 &Qabove_handle, &Qhandle, &Qbelow_handle,
5323 &Qup, &Qdown, &Qtop, &Qbottom, &Qend_scroll, &Qratio
5326 /* A vector, indexed by button number, giving the down-going location
5327 of currently depressed buttons, both scroll bar and non-scroll bar.
5329 The elements have the form
5330 (BUTTON-NUMBER MODIFIER-MASK . REST)
5331 where REST is the cdr of a position as it would be reported in the event.
5333 The make_lispy_event function stores positions here to tell the
5334 difference between click and drag events, and to store the starting
5335 location to be included in drag events. */
5337 static Lisp_Object button_down_location;
5339 /* Information about the most recent up-going button event: Which
5340 button, what location, and what time. */
5342 static int last_mouse_button;
5343 static int last_mouse_x;
5344 static int last_mouse_y;
5345 static unsigned long button_down_time;
5347 /* The maximum time between clicks to make a double-click, or Qnil to
5348 disable double-click detection, or Qt for no time limit. */
5350 Lisp_Object Vdouble_click_time;
5352 /* Maximum number of pixels the mouse may be moved between clicks
5353 to make a double-click. */
5355 EMACS_INT double_click_fuzz;
5357 /* The number of clicks in this multiple-click. */
5359 int double_click_count;
5361 /* Return position of a mouse click or wheel event */
5363 static Lisp_Object
5364 make_lispy_position (f, x, y, time)
5365 struct frame *f;
5366 Lisp_Object *x, *y;
5367 unsigned long time;
5369 Lisp_Object window;
5370 enum window_part part;
5371 Lisp_Object posn = Qnil;
5372 Lisp_Object extra_info = Qnil;
5373 int wx, wy;
5375 /* Set `window' to the window under frame pixel coordinates (x,y) */
5376 if (f)
5377 window = window_from_coordinates (f, XINT (*x), XINT (*y),
5378 &part, &wx, &wy, 0);
5379 else
5380 window = Qnil;
5382 if (WINDOWP (window))
5384 /* It's a click in window window at frame coordinates (x,y) */
5385 struct window *w = XWINDOW (window);
5386 Lisp_Object string_info = Qnil;
5387 int textpos = -1, rx = -1, ry = -1;
5388 int dx = -1, dy = -1;
5389 int width = -1, height = -1;
5390 Lisp_Object object = Qnil;
5392 /* Set event coordinates to window-relative coordinates
5393 for constructing the Lisp event below. */
5394 XSETINT (*x, wx);
5395 XSETINT (*y, wy);
5397 if (part == ON_TEXT)
5399 wx += WINDOW_LEFT_MARGIN_WIDTH (w);
5401 else if (part == ON_MODE_LINE || part == ON_HEADER_LINE)
5403 /* Mode line or header line. Look for a string under
5404 the mouse that may have a `local-map' property. */
5405 Lisp_Object string;
5406 int charpos;
5408 posn = part == ON_MODE_LINE ? Qmode_line : Qheader_line;
5409 rx = wx, ry = wy;
5410 string = mode_line_string (w, part, &rx, &ry, &charpos,
5411 &object, &dx, &dy, &width, &height);
5412 if (STRINGP (string))
5413 string_info = Fcons (string, make_number (charpos));
5414 if (w == XWINDOW (selected_window))
5415 textpos = PT;
5416 else
5417 textpos = XMARKER (w->pointm)->charpos;
5419 else if (part == ON_VERTICAL_BORDER)
5421 posn = Qvertical_line;
5422 wx = -1;
5423 dx = 0;
5424 width = 1;
5426 else if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
5428 Lisp_Object string;
5429 int charpos;
5431 posn = (part == ON_LEFT_MARGIN) ? Qleft_margin : Qright_margin;
5432 rx = wx, ry = wy;
5433 string = marginal_area_string (w, part, &rx, &ry, &charpos,
5434 &object, &dx, &dy, &width, &height);
5435 if (STRINGP (string))
5436 string_info = Fcons (string, make_number (charpos));
5437 if (part == ON_LEFT_MARGIN)
5438 wx = 0;
5439 else
5440 wx = window_box_right_offset (w, TEXT_AREA) - 1;
5442 else if (part == ON_LEFT_FRINGE)
5444 posn = Qleft_fringe;
5445 rx = 0;
5446 dx = wx;
5447 wx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5449 : window_box_width (w, LEFT_MARGIN_AREA));
5450 dx -= wx;
5452 else if (part == ON_RIGHT_FRINGE)
5454 posn = Qright_fringe;
5455 rx = 0;
5456 dx = wx;
5457 wx = (window_box_width (w, LEFT_MARGIN_AREA)
5458 + window_box_width (w, TEXT_AREA)
5459 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5460 ? window_box_width (w, RIGHT_MARGIN_AREA)
5461 : 0));
5462 dx -= wx;
5464 else
5466 /* Note: We have no special posn for part == ON_SCROLL_BAR. */
5467 wx = max (WINDOW_LEFT_MARGIN_WIDTH (w), wx);
5470 if (textpos < 0)
5472 Lisp_Object string2, object2 = Qnil;
5473 struct display_pos p;
5474 int dx2, dy2;
5475 int width2, height2;
5476 string2 = buffer_posn_from_coords (w, &wx, &wy, &p,
5477 &object2, &dx2, &dy2,
5478 &width2, &height2);
5479 textpos = CHARPOS (p.pos);
5480 if (rx < 0) rx = wx;
5481 if (ry < 0) ry = wy;
5482 if (dx < 0) dx = dx2;
5483 if (dy < 0) dy = dy2;
5484 if (width < 0) width = width2;
5485 if (height < 0) height = height2;
5487 if (NILP (posn))
5489 posn = make_number (textpos);
5490 if (STRINGP (string2))
5491 string_info = Fcons (string2,
5492 make_number (CHARPOS (p.string_pos)));
5494 if (NILP (object))
5495 object = object2;
5498 #ifdef HAVE_WINDOW_SYSTEM
5499 if (IMAGEP (object))
5501 Lisp_Object image_map, hotspot;
5502 if ((image_map = Fplist_get (XCDR (object), QCmap),
5503 !NILP (image_map))
5504 && (hotspot = find_hot_spot (image_map, dx, dy),
5505 CONSP (hotspot))
5506 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
5507 posn = XCAR (hotspot);
5509 #endif
5511 /* Object info */
5512 extra_info = Fcons (object,
5513 Fcons (Fcons (make_number (dx),
5514 make_number (dy)),
5515 Fcons (Fcons (make_number (width),
5516 make_number (height)),
5517 Qnil)));
5519 /* String info */
5520 extra_info = Fcons (string_info,
5521 Fcons (make_number (textpos),
5522 Fcons (Fcons (make_number (rx),
5523 make_number (ry)),
5524 extra_info)));
5526 else if (f != 0)
5528 XSETFRAME (window, f);
5530 else
5532 window = Qnil;
5533 XSETFASTINT (*x, 0);
5534 XSETFASTINT (*y, 0);
5537 return Fcons (window,
5538 Fcons (posn,
5539 Fcons (Fcons (*x, *y),
5540 Fcons (make_number (time),
5541 extra_info))));
5544 /* Given a struct input_event, build the lisp event which represents
5545 it. If EVENT is 0, build a mouse movement event from the mouse
5546 movement buffer, which should have a movement event in it.
5548 Note that events must be passed to this function in the order they
5549 are received; this function stores the location of button presses
5550 in order to build drag events when the button is released. */
5552 static Lisp_Object
5553 make_lispy_event (event)
5554 struct input_event *event;
5556 int i;
5558 switch (SWITCH_ENUM_CAST (event->kind))
5560 /* A simple keystroke. */
5561 case ASCII_KEYSTROKE_EVENT:
5562 case MULTIBYTE_CHAR_KEYSTROKE_EVENT:
5564 Lisp_Object lispy_c;
5565 int c = event->code;
5566 if (event->kind == ASCII_KEYSTROKE_EVENT)
5568 c &= 0377;
5569 eassert (c == event->code);
5570 /* Turn ASCII characters into control characters
5571 when proper. */
5572 if (event->modifiers & ctrl_modifier)
5574 c = make_ctrl_char (c);
5575 event->modifiers &= ~ctrl_modifier;
5579 /* Add in the other modifier bits. The shift key was taken care
5580 of by the X code. */
5581 c |= (event->modifiers
5582 & (meta_modifier | alt_modifier
5583 | hyper_modifier | super_modifier | ctrl_modifier));
5584 /* Distinguish Shift-SPC from SPC. */
5585 if ((event->code) == 040
5586 && event->modifiers & shift_modifier)
5587 c |= shift_modifier;
5588 button_down_time = 0;
5589 XSETFASTINT (lispy_c, c);
5590 return lispy_c;
5593 /* A function key. The symbol may need to have modifier prefixes
5594 tacked onto it. */
5595 case NON_ASCII_KEYSTROKE_EVENT:
5596 button_down_time = 0;
5598 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
5599 if (event->code == lispy_accent_codes[i])
5600 return modify_event_symbol (i,
5601 event->modifiers,
5602 Qfunction_key, Qnil,
5603 lispy_accent_keys, &accent_key_syms,
5604 (sizeof (lispy_accent_keys)
5605 / sizeof (lispy_accent_keys[0])));
5607 #if 0
5608 #ifdef XK_kana_A
5609 if (event->code >= 0x400 && event->code < 0x500)
5610 return modify_event_symbol (event->code - 0x400,
5611 event->modifiers & ~shift_modifier,
5612 Qfunction_key, Qnil,
5613 lispy_kana_keys, &func_key_syms,
5614 (sizeof (lispy_kana_keys)
5615 / sizeof (lispy_kana_keys[0])));
5616 #endif /* XK_kana_A */
5617 #endif /* 0 */
5619 #ifdef ISO_FUNCTION_KEY_OFFSET
5620 if (event->code < FUNCTION_KEY_OFFSET
5621 && event->code >= ISO_FUNCTION_KEY_OFFSET)
5622 return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET,
5623 event->modifiers,
5624 Qfunction_key, Qnil,
5625 iso_lispy_function_keys, &func_key_syms,
5626 (sizeof (iso_lispy_function_keys)
5627 / sizeof (iso_lispy_function_keys[0])));
5628 #endif
5630 /* Handle system-specific or unknown keysyms. */
5631 if (event->code & (1 << 28)
5632 || event->code - FUNCTION_KEY_OFFSET < 0
5633 || (event->code - FUNCTION_KEY_OFFSET
5634 >= sizeof lispy_function_keys / sizeof *lispy_function_keys)
5635 || !lispy_function_keys[event->code - FUNCTION_KEY_OFFSET])
5637 /* We need to use an alist rather than a vector as the cache
5638 since we can't make a vector long enuf. */
5639 if (NILP (current_kboard->system_key_syms))
5640 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
5641 return modify_event_symbol (event->code,
5642 event->modifiers,
5643 Qfunction_key,
5644 current_kboard->Vsystem_key_alist,
5645 0, &current_kboard->system_key_syms,
5646 (unsigned) -1);
5649 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
5650 event->modifiers,
5651 Qfunction_key, Qnil,
5652 lispy_function_keys, &func_key_syms,
5653 (sizeof (lispy_function_keys)
5654 / sizeof (lispy_function_keys[0])));
5656 #ifdef WINDOWSNT
5657 case MULTIMEDIA_KEY_EVENT:
5658 if (event->code < (sizeof (lispy_multimedia_keys)
5659 / sizeof (lispy_multimedia_keys[0]))
5660 && event->code > 0 && lispy_multimedia_keys[event->code])
5662 return modify_event_symbol (event->code, event->modifiers,
5663 Qfunction_key, Qnil,
5664 lispy_multimedia_keys, &func_key_syms,
5665 (sizeof (lispy_multimedia_keys)
5666 / sizeof (lispy_multimedia_keys[0])));
5668 return Qnil;
5669 #endif
5671 #ifdef HAVE_MOUSE
5672 /* A mouse click. Figure out where it is, decide whether it's
5673 a press, click or drag, and build the appropriate structure. */
5674 case MOUSE_CLICK_EVENT:
5675 #ifndef USE_TOOLKIT_SCROLL_BARS
5676 case SCROLL_BAR_CLICK_EVENT:
5677 #endif
5679 int button = event->code;
5680 int is_double;
5681 Lisp_Object position;
5682 Lisp_Object *start_pos_ptr;
5683 Lisp_Object start_pos;
5685 position = Qnil;
5687 /* Build the position as appropriate for this mouse click. */
5688 if (event->kind == MOUSE_CLICK_EVENT)
5690 struct frame *f = XFRAME (event->frame_or_window);
5691 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
5692 int row, column;
5693 #endif
5695 /* Ignore mouse events that were made on frame that
5696 have been deleted. */
5697 if (! FRAME_LIVE_P (f))
5698 return Qnil;
5700 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
5701 /* EVENT->x and EVENT->y are frame-relative pixel
5702 coordinates at this place. Under old redisplay, COLUMN
5703 and ROW are set to frame relative glyph coordinates
5704 which are then used to determine whether this click is
5705 in a menu (non-toolkit version). */
5706 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
5707 &column, &row, NULL, 1);
5709 /* In the non-toolkit version, clicks on the menu bar
5710 are ordinary button events in the event buffer.
5711 Distinguish them, and invoke the menu.
5713 (In the toolkit version, the toolkit handles the menu bar
5714 and Emacs doesn't know about it until after the user
5715 makes a selection.) */
5716 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
5717 && (event->modifiers & down_modifier))
5719 Lisp_Object items, item;
5720 int hpos;
5721 int i;
5723 #if 0
5724 /* Activate the menu bar on the down event. If the
5725 up event comes in before the menu code can deal with it,
5726 just ignore it. */
5727 if (! (event->modifiers & down_modifier))
5728 return Qnil;
5729 #endif
5731 /* Find the menu bar item under `column'. */
5732 item = Qnil;
5733 items = FRAME_MENU_BAR_ITEMS (f);
5734 for (i = 0; i < XVECTOR (items)->size; i += 4)
5736 Lisp_Object pos, string;
5737 string = AREF (items, i + 1);
5738 pos = AREF (items, i + 3);
5739 if (NILP (string))
5740 break;
5741 if (column >= XINT (pos)
5742 && column < XINT (pos) + SCHARS (string))
5744 item = AREF (items, i);
5745 break;
5749 /* ELisp manual 2.4b says (x y) are window relative but
5750 code says they are frame-relative. */
5751 position
5752 = Fcons (event->frame_or_window,
5753 Fcons (Qmenu_bar,
5754 Fcons (Fcons (event->x, event->y),
5755 Fcons (make_number (event->timestamp),
5756 Qnil))));
5758 return Fcons (item, Fcons (position, Qnil));
5760 #endif /* not USE_X_TOOLKIT && not USE_GTK */
5762 position = make_lispy_position (f, &event->x, &event->y,
5763 event->timestamp);
5765 #ifndef USE_TOOLKIT_SCROLL_BARS
5766 else
5768 /* It's a scrollbar click. */
5769 Lisp_Object window;
5770 Lisp_Object portion_whole;
5771 Lisp_Object part;
5773 window = event->frame_or_window;
5774 portion_whole = Fcons (event->x, event->y);
5775 part = *scroll_bar_parts[(int) event->part];
5777 position
5778 = Fcons (window,
5779 Fcons (Qvertical_scroll_bar,
5780 Fcons (portion_whole,
5781 Fcons (make_number (event->timestamp),
5782 Fcons (part, Qnil)))));
5784 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5786 if (button >= ASIZE (button_down_location))
5788 button_down_location = larger_vector (button_down_location,
5789 button + 1, Qnil);
5790 mouse_syms = larger_vector (mouse_syms, button + 1, Qnil);
5793 start_pos_ptr = &AREF (button_down_location, button);
5794 start_pos = *start_pos_ptr;
5795 *start_pos_ptr = Qnil;
5798 /* On window-system frames, use the value of
5799 double-click-fuzz as is. On other frames, interpret it
5800 as a multiple of 1/8 characters. */
5801 struct frame *f;
5802 int fuzz;
5804 if (WINDOWP (event->frame_or_window))
5805 f = XFRAME (XWINDOW (event->frame_or_window)->frame);
5806 else if (FRAMEP (event->frame_or_window))
5807 f = XFRAME (event->frame_or_window);
5808 else
5809 abort ();
5811 if (FRAME_WINDOW_P (f))
5812 fuzz = double_click_fuzz;
5813 else
5814 fuzz = double_click_fuzz / 8;
5816 is_double = (button == last_mouse_button
5817 && (eabs (XINT (event->x) - last_mouse_x) <= fuzz)
5818 && (eabs (XINT (event->y) - last_mouse_y) <= fuzz)
5819 && button_down_time != 0
5820 && (EQ (Vdouble_click_time, Qt)
5821 || (INTEGERP (Vdouble_click_time)
5822 && ((int)(event->timestamp - button_down_time)
5823 < XINT (Vdouble_click_time)))));
5826 last_mouse_button = button;
5827 last_mouse_x = XINT (event->x);
5828 last_mouse_y = XINT (event->y);
5830 /* If this is a button press, squirrel away the location, so
5831 we can decide later whether it was a click or a drag. */
5832 if (event->modifiers & down_modifier)
5834 if (is_double)
5836 double_click_count++;
5837 event->modifiers |= ((double_click_count > 2)
5838 ? triple_modifier
5839 : double_modifier);
5841 else
5842 double_click_count = 1;
5843 button_down_time = event->timestamp;
5844 *start_pos_ptr = Fcopy_alist (position);
5845 ignore_mouse_drag_p = 0;
5848 /* Now we're releasing a button - check the co-ordinates to
5849 see if this was a click or a drag. */
5850 else if (event->modifiers & up_modifier)
5852 /* If we did not see a down before this up, ignore the up.
5853 Probably this happened because the down event chose a
5854 menu item. It would be an annoyance to treat the
5855 release of the button that chose the menu item as a
5856 separate event. */
5858 if (!CONSP (start_pos))
5859 return Qnil;
5861 event->modifiers &= ~up_modifier;
5862 #if 0 /* Formerly we treated an up with no down as a click event. */
5863 if (!CONSP (start_pos))
5864 event->modifiers |= click_modifier;
5865 else
5866 #endif
5868 Lisp_Object down;
5869 EMACS_INT xdiff = double_click_fuzz, ydiff = double_click_fuzz;
5871 /* The third element of every position
5872 should be the (x,y) pair. */
5873 down = Fcar (Fcdr (Fcdr (start_pos)));
5874 if (CONSP (down)
5875 && INTEGERP (XCAR (down)) && INTEGERP (XCDR (down)))
5877 xdiff = XINT (event->x) - XINT (XCAR (down));
5878 ydiff = XINT (event->y) - XINT (XCDR (down));
5881 if (ignore_mouse_drag_p)
5883 event->modifiers |= click_modifier;
5884 ignore_mouse_drag_p = 0;
5886 else if (xdiff < double_click_fuzz && xdiff > - double_click_fuzz
5887 && ydiff < double_click_fuzz && ydiff > - double_click_fuzz
5888 /* Maybe the mouse has moved a lot, caused scrolling, and
5889 eventually ended up at the same screen position (but
5890 not buffer position) in which case it is a drag, not
5891 a click. */
5892 /* FIXME: OTOH if the buffer position has changed
5893 because of a timer or process filter rather than
5894 because of mouse movement, it should be considered as
5895 a click. But mouse-drag-region completely ignores
5896 this case and it hasn't caused any real problem, so
5897 it's probably OK to ignore it as well. */
5898 && EQ (Fcar (Fcdr (start_pos)), Fcar (Fcdr (position))))
5899 /* Mouse hasn't moved (much). */
5900 event->modifiers |= click_modifier;
5901 else
5903 button_down_time = 0;
5904 event->modifiers |= drag_modifier;
5907 /* Don't check is_double; treat this as multiple
5908 if the down-event was multiple. */
5909 if (double_click_count > 1)
5910 event->modifiers |= ((double_click_count > 2)
5911 ? triple_modifier
5912 : double_modifier);
5915 else
5916 /* Every mouse event should either have the down_modifier or
5917 the up_modifier set. */
5918 abort ();
5921 /* Get the symbol we should use for the mouse click. */
5922 Lisp_Object head;
5924 head = modify_event_symbol (button,
5925 event->modifiers,
5926 Qmouse_click, Vlispy_mouse_stem,
5927 NULL,
5928 &mouse_syms,
5929 XVECTOR (mouse_syms)->size);
5930 if (event->modifiers & drag_modifier)
5931 return Fcons (head,
5932 Fcons (start_pos,
5933 Fcons (position,
5934 Qnil)));
5935 else if (event->modifiers & (double_modifier | triple_modifier))
5936 return Fcons (head,
5937 Fcons (position,
5938 Fcons (make_number (double_click_count),
5939 Qnil)));
5940 else
5941 return Fcons (head,
5942 Fcons (position,
5943 Qnil));
5947 case WHEEL_EVENT:
5948 case HORIZ_WHEEL_EVENT:
5950 Lisp_Object position;
5951 Lisp_Object head;
5953 /* Build the position as appropriate for this mouse click. */
5954 struct frame *f = XFRAME (event->frame_or_window);
5956 /* Ignore wheel events that were made on frame that have been
5957 deleted. */
5958 if (! FRAME_LIVE_P (f))
5959 return Qnil;
5961 position = make_lispy_position (f, &event->x, &event->y,
5962 event->timestamp);
5964 /* Set double or triple modifiers to indicate the wheel speed. */
5966 /* On window-system frames, use the value of
5967 double-click-fuzz as is. On other frames, interpret it
5968 as a multiple of 1/8 characters. */
5969 struct frame *f;
5970 int fuzz;
5971 int is_double;
5973 if (WINDOWP (event->frame_or_window))
5974 f = XFRAME (XWINDOW (event->frame_or_window)->frame);
5975 else if (FRAMEP (event->frame_or_window))
5976 f = XFRAME (event->frame_or_window);
5977 else
5978 abort ();
5980 if (FRAME_WINDOW_P (f))
5981 fuzz = double_click_fuzz;
5982 else
5983 fuzz = double_click_fuzz / 8;
5985 is_double = (last_mouse_button < 0
5986 && (eabs (XINT (event->x) - last_mouse_x) <= fuzz)
5987 && (eabs (XINT (event->y) - last_mouse_y) <= fuzz)
5988 && button_down_time != 0
5989 && (EQ (Vdouble_click_time, Qt)
5990 || (INTEGERP (Vdouble_click_time)
5991 && ((int)(event->timestamp - button_down_time)
5992 < XINT (Vdouble_click_time)))));
5993 if (is_double)
5995 double_click_count++;
5996 event->modifiers |= ((double_click_count > 2)
5997 ? triple_modifier
5998 : double_modifier);
6000 else
6002 double_click_count = 1;
6003 event->modifiers |= click_modifier;
6006 button_down_time = event->timestamp;
6007 /* Use a negative value to distinguish wheel from mouse button. */
6008 last_mouse_button = -1;
6009 last_mouse_x = XINT (event->x);
6010 last_mouse_y = XINT (event->y);
6014 int symbol_num;
6016 if (event->modifiers & up_modifier)
6018 /* Emit a wheel-up event. */
6019 event->modifiers &= ~up_modifier;
6020 symbol_num = 0;
6022 else if (event->modifiers & down_modifier)
6024 /* Emit a wheel-down event. */
6025 event->modifiers &= ~down_modifier;
6026 symbol_num = 1;
6028 else
6029 /* Every wheel event should either have the down_modifier or
6030 the up_modifier set. */
6031 abort ();
6033 if (event->kind == HORIZ_WHEEL_EVENT)
6034 symbol_num += 2;
6036 /* Get the symbol we should use for the wheel event. */
6037 head = modify_event_symbol (symbol_num,
6038 event->modifiers,
6039 Qmouse_click,
6040 Qnil,
6041 lispy_wheel_names,
6042 &wheel_syms,
6043 ASIZE (wheel_syms));
6046 if (event->modifiers & (double_modifier | triple_modifier))
6047 return Fcons (head,
6048 Fcons (position,
6049 Fcons (make_number (double_click_count),
6050 Qnil)));
6051 else
6052 return Fcons (head,
6053 Fcons (position,
6054 Qnil));
6058 #ifdef USE_TOOLKIT_SCROLL_BARS
6060 /* We don't have down and up events if using toolkit scroll bars,
6061 so make this always a click event. Store in the `part' of
6062 the Lisp event a symbol which maps to the following actions:
6064 `above_handle' page up
6065 `below_handle' page down
6066 `up' line up
6067 `down' line down
6068 `top' top of buffer
6069 `bottom' bottom of buffer
6070 `handle' thumb has been dragged.
6071 `end-scroll' end of interaction with scroll bar
6073 The incoming input_event contains in its `part' member an
6074 index of type `enum scroll_bar_part' which we can use as an
6075 index in scroll_bar_parts to get the appropriate symbol. */
6077 case SCROLL_BAR_CLICK_EVENT:
6079 Lisp_Object position, head, window, portion_whole, part;
6081 window = event->frame_or_window;
6082 portion_whole = Fcons (event->x, event->y);
6083 part = *scroll_bar_parts[(int) event->part];
6085 position
6086 = Fcons (window,
6087 Fcons (Qvertical_scroll_bar,
6088 Fcons (portion_whole,
6089 Fcons (make_number (event->timestamp),
6090 Fcons (part, Qnil)))));
6092 /* Always treat scroll bar events as clicks. */
6093 event->modifiers |= click_modifier;
6094 event->modifiers &= ~up_modifier;
6096 if (event->code >= ASIZE (mouse_syms))
6097 mouse_syms = larger_vector (mouse_syms, event->code + 1, Qnil);
6099 /* Get the symbol we should use for the mouse click. */
6100 head = modify_event_symbol (event->code,
6101 event->modifiers,
6102 Qmouse_click,
6103 Vlispy_mouse_stem,
6104 NULL, &mouse_syms,
6105 XVECTOR (mouse_syms)->size);
6106 return Fcons (head, Fcons (position, Qnil));
6109 #endif /* USE_TOOLKIT_SCROLL_BARS */
6111 case DRAG_N_DROP_EVENT:
6113 FRAME_PTR f;
6114 Lisp_Object head, position;
6115 Lisp_Object files;
6117 f = XFRAME (event->frame_or_window);
6118 files = event->arg;
6120 /* Ignore mouse events that were made on frames that
6121 have been deleted. */
6122 if (! FRAME_LIVE_P (f))
6123 return Qnil;
6125 position = make_lispy_position (f, &event->x, &event->y,
6126 event->timestamp);
6128 head = modify_event_symbol (0, event->modifiers,
6129 Qdrag_n_drop, Qnil,
6130 lispy_drag_n_drop_names,
6131 &drag_n_drop_syms, 1);
6132 return Fcons (head,
6133 Fcons (position,
6134 Fcons (files,
6135 Qnil)));
6137 #endif /* HAVE_MOUSE */
6139 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
6140 || defined (USE_GTK)
6141 case MENU_BAR_EVENT:
6142 if (EQ (event->arg, event->frame_or_window))
6143 /* This is the prefix key. We translate this to
6144 `(menu_bar)' because the code in keyboard.c for menu
6145 events, which we use, relies on this. */
6146 return Fcons (Qmenu_bar, Qnil);
6147 return event->arg;
6148 #endif
6150 case SELECT_WINDOW_EVENT:
6151 /* Make an event (select-window (WINDOW)). */
6152 return Fcons (Qselect_window,
6153 Fcons (Fcons (event->frame_or_window, Qnil),
6154 Qnil));
6156 case TOOL_BAR_EVENT:
6157 if (EQ (event->arg, event->frame_or_window))
6158 /* This is the prefix key. We translate this to
6159 `(tool_bar)' because the code in keyboard.c for tool bar
6160 events, which we use, relies on this. */
6161 return Fcons (Qtool_bar, Qnil);
6162 else if (SYMBOLP (event->arg))
6163 return apply_modifiers (event->modifiers, event->arg);
6164 return event->arg;
6166 case USER_SIGNAL_EVENT:
6167 /* A user signal. */
6169 char *name = find_user_signal_name (event->code);
6170 if (!name)
6171 abort ();
6172 return intern (name);
6175 case SAVE_SESSION_EVENT:
6176 return Qsave_session;
6178 #ifdef MAC_OS
6179 case MAC_APPLE_EVENT:
6181 Lisp_Object spec[2];
6183 spec[0] = event->x;
6184 spec[1] = event->y;
6185 return Fcons (Qmac_apple_event,
6186 Fcons (Fvector (2, spec),
6187 Fcons (event->arg, Qnil)));
6189 #endif
6191 #ifdef HAVE_DBUS
6192 case DBUS_EVENT:
6194 return Fcons (Qdbus_event, event->arg);
6196 #endif /* HAVE_DBUS */
6198 #ifdef HAVE_GPM
6199 case GPM_CLICK_EVENT:
6201 FRAME_PTR f = XFRAME (event->frame_or_window);
6202 Lisp_Object head, position;
6203 Lisp_Object *start_pos_ptr;
6204 Lisp_Object start_pos;
6205 int button = event->code;
6207 if (button >= ASIZE (button_down_location))
6209 button_down_location = larger_vector (button_down_location,
6210 button + 1, Qnil);
6211 mouse_syms = larger_vector (mouse_syms, button + 1, Qnil);
6214 start_pos_ptr = &AREF (button_down_location, button);
6215 start_pos = *start_pos_ptr;
6217 position = make_lispy_position (f, &event->x, &event->y,
6218 event->timestamp);
6220 if (event->modifiers & down_modifier)
6221 *start_pos_ptr = Fcopy_alist (position);
6222 else if (event->modifiers & (up_modifier | drag_modifier))
6224 if (!CONSP (start_pos))
6225 return Qnil;
6226 event->modifiers &= ~up_modifier;
6229 head = modify_event_symbol (button,
6230 event->modifiers,
6231 Qmouse_click, Vlispy_mouse_stem,
6232 NULL,
6233 &mouse_syms,
6234 XVECTOR (mouse_syms)->size);
6236 if (event->modifiers & drag_modifier)
6237 return Fcons (head,
6238 Fcons (start_pos,
6239 Fcons (position,
6240 Qnil)));
6241 else if (event->modifiers & double_modifier)
6242 return Fcons (head,
6243 Fcons (position,
6244 Fcons (make_number (2),
6245 Qnil)));
6246 else if (event->modifiers & triple_modifier)
6247 return Fcons (head,
6248 Fcons (position,
6249 Fcons (make_number (3),
6250 Qnil)));
6251 else
6252 return Fcons (head,
6253 Fcons (position,
6254 Qnil));
6256 #endif /* HAVE_GPM */
6258 /* The 'kind' field of the event is something we don't recognize. */
6259 default:
6260 abort ();
6264 #if defined(HAVE_MOUSE) || defined(HAVE_GPM)
6266 static Lisp_Object
6267 make_lispy_movement (frame, bar_window, part, x, y, time)
6268 FRAME_PTR frame;
6269 Lisp_Object bar_window;
6270 enum scroll_bar_part part;
6271 Lisp_Object x, y;
6272 unsigned long time;
6274 /* Is it a scroll bar movement? */
6275 if (frame && ! NILP (bar_window))
6277 Lisp_Object part_sym;
6279 part_sym = *scroll_bar_parts[(int) part];
6280 return Fcons (Qscroll_bar_movement,
6281 (Fcons (Fcons (bar_window,
6282 Fcons (Qvertical_scroll_bar,
6283 Fcons (Fcons (x, y),
6284 Fcons (make_number (time),
6285 Fcons (part_sym,
6286 Qnil))))),
6287 Qnil)));
6290 /* Or is it an ordinary mouse movement? */
6291 else
6293 Lisp_Object position;
6295 position = make_lispy_position (frame, &x, &y, time);
6297 return Fcons (Qmouse_movement,
6298 Fcons (position,
6299 Qnil));
6303 #endif /* HAVE_MOUSE || HAVE GPM */
6305 /* Construct a switch frame event. */
6306 static Lisp_Object
6307 make_lispy_switch_frame (frame)
6308 Lisp_Object frame;
6310 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
6313 /* Manipulating modifiers. */
6315 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
6317 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
6318 SYMBOL's name of the end of the modifiers; the string from this
6319 position is the unmodified symbol name.
6321 This doesn't use any caches. */
6323 static int
6324 parse_modifiers_uncached (symbol, modifier_end)
6325 Lisp_Object symbol;
6326 int *modifier_end;
6328 Lisp_Object name;
6329 int i;
6330 int modifiers;
6332 CHECK_SYMBOL (symbol);
6334 modifiers = 0;
6335 name = SYMBOL_NAME (symbol);
6337 for (i = 0; i+2 <= SBYTES (name); )
6339 int this_mod_end = 0;
6340 int this_mod = 0;
6342 /* See if the name continues with a modifier word.
6343 Check that the word appears, but don't check what follows it.
6344 Set this_mod and this_mod_end to record what we find. */
6346 switch (SREF (name, i))
6348 #define SINGLE_LETTER_MOD(BIT) \
6349 (this_mod_end = i + 1, this_mod = BIT)
6351 case 'A':
6352 SINGLE_LETTER_MOD (alt_modifier);
6353 break;
6355 case 'C':
6356 SINGLE_LETTER_MOD (ctrl_modifier);
6357 break;
6359 case 'H':
6360 SINGLE_LETTER_MOD (hyper_modifier);
6361 break;
6363 case 'M':
6364 SINGLE_LETTER_MOD (meta_modifier);
6365 break;
6367 case 'S':
6368 SINGLE_LETTER_MOD (shift_modifier);
6369 break;
6371 case 's':
6372 SINGLE_LETTER_MOD (super_modifier);
6373 break;
6375 #undef SINGLE_LETTER_MOD
6377 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
6378 if (i + LEN + 1 <= SBYTES (name) \
6379 && ! strncmp (SDATA (name) + i, NAME, LEN)) \
6381 this_mod_end = i + LEN; \
6382 this_mod = BIT; \
6385 case 'd':
6386 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
6387 MULTI_LETTER_MOD (down_modifier, "down", 4);
6388 MULTI_LETTER_MOD (double_modifier, "double", 6);
6389 break;
6391 case 't':
6392 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
6393 break;
6394 #undef MULTI_LETTER_MOD
6398 /* If we found no modifier, stop looking for them. */
6399 if (this_mod_end == 0)
6400 break;
6402 /* Check there is a dash after the modifier, so that it
6403 really is a modifier. */
6404 if (this_mod_end >= SBYTES (name)
6405 || SREF (name, this_mod_end) != '-')
6406 break;
6408 /* This modifier is real; look for another. */
6409 modifiers |= this_mod;
6410 i = this_mod_end + 1;
6413 /* Should we include the `click' modifier? */
6414 if (! (modifiers & (down_modifier | drag_modifier
6415 | double_modifier | triple_modifier))
6416 && i + 7 == SBYTES (name)
6417 && strncmp (SDATA (name) + i, "mouse-", 6) == 0
6418 && ('0' <= SREF (name, i + 6) && SREF (name, i + 6) <= '9'))
6419 modifiers |= click_modifier;
6421 if (modifier_end)
6422 *modifier_end = i;
6424 return modifiers;
6427 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
6428 prepended to the string BASE[0..BASE_LEN-1].
6429 This doesn't use any caches. */
6430 static Lisp_Object
6431 apply_modifiers_uncached (modifiers, base, base_len, base_len_byte)
6432 int modifiers;
6433 char *base;
6434 int base_len, base_len_byte;
6436 /* Since BASE could contain nulls, we can't use intern here; we have
6437 to use Fintern, which expects a genuine Lisp_String, and keeps a
6438 reference to it. */
6439 char *new_mods
6440 = (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
6441 int mod_len;
6444 char *p = new_mods;
6446 /* Only the event queue may use the `up' modifier; it should always
6447 be turned into a click or drag event before presented to lisp code. */
6448 if (modifiers & up_modifier)
6449 abort ();
6451 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
6452 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
6453 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
6454 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
6455 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
6456 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
6457 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
6458 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
6459 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
6460 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
6461 /* The click modifier is denoted by the absence of other modifiers. */
6463 *p = '\0';
6465 mod_len = p - new_mods;
6469 Lisp_Object new_name;
6471 new_name = make_uninit_multibyte_string (mod_len + base_len,
6472 mod_len + base_len_byte);
6473 bcopy (new_mods, SDATA (new_name), mod_len);
6474 bcopy (base, SDATA (new_name) + mod_len, base_len_byte);
6476 return Fintern (new_name, Qnil);
6481 static char *modifier_names[] =
6483 "up", "down", "drag", "click", "double", "triple", 0, 0,
6484 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6485 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
6487 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
6489 static Lisp_Object modifier_symbols;
6491 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
6492 static Lisp_Object
6493 lispy_modifier_list (modifiers)
6494 int modifiers;
6496 Lisp_Object modifier_list;
6497 int i;
6499 modifier_list = Qnil;
6500 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
6501 if (modifiers & (1<<i))
6502 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
6503 modifier_list);
6505 return modifier_list;
6509 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
6510 where UNMODIFIED is the unmodified form of SYMBOL,
6511 MASK is the set of modifiers present in SYMBOL's name.
6512 This is similar to parse_modifiers_uncached, but uses the cache in
6513 SYMBOL's Qevent_symbol_element_mask property, and maintains the
6514 Qevent_symbol_elements property. */
6516 #define KEY_TO_CHAR(k) (XINT (k) & ((1 << CHARACTERBITS) - 1))
6518 Lisp_Object
6519 parse_modifiers (symbol)
6520 Lisp_Object symbol;
6522 Lisp_Object elements;
6524 if (INTEGERP (symbol))
6525 return (Fcons (make_number (KEY_TO_CHAR (symbol)),
6526 Fcons (make_number (XINT (symbol) & CHAR_MODIFIER_MASK),
6527 Qnil)));
6528 else if (!SYMBOLP (symbol))
6529 return Qnil;
6531 elements = Fget (symbol, Qevent_symbol_element_mask);
6532 if (CONSP (elements))
6533 return elements;
6534 else
6536 int end;
6537 int modifiers = parse_modifiers_uncached (symbol, &end);
6538 Lisp_Object unmodified;
6539 Lisp_Object mask;
6541 unmodified = Fintern (make_string (SDATA (SYMBOL_NAME (symbol)) + end,
6542 SBYTES (SYMBOL_NAME (symbol)) - end),
6543 Qnil);
6545 if (modifiers & ~INTMASK)
6546 abort ();
6547 XSETFASTINT (mask, modifiers);
6548 elements = Fcons (unmodified, Fcons (mask, Qnil));
6550 /* Cache the parsing results on SYMBOL. */
6551 Fput (symbol, Qevent_symbol_element_mask,
6552 elements);
6553 Fput (symbol, Qevent_symbol_elements,
6554 Fcons (unmodified, lispy_modifier_list (modifiers)));
6556 /* Since we know that SYMBOL is modifiers applied to unmodified,
6557 it would be nice to put that in unmodified's cache.
6558 But we can't, since we're not sure that parse_modifiers is
6559 canonical. */
6561 return elements;
6565 DEFUN ("internal-event-symbol-parse-modifiers", Fevent_symbol_parse_modifiers,
6566 Sevent_symbol_parse_modifiers, 1, 1, 0,
6567 doc: /* Parse the event symbol. For internal use. */)
6568 (symbol)
6569 Lisp_Object symbol;
6571 /* Fill the cache if needed. */
6572 parse_modifiers (symbol);
6573 /* Ignore the result (which is stored on Qevent_symbol_element_mask)
6574 and use the Lispier representation stored on Qevent_symbol_elements
6575 instead. */
6576 return Fget (symbol, Qevent_symbol_elements);
6579 /* Apply the modifiers MODIFIERS to the symbol BASE.
6580 BASE must be unmodified.
6582 This is like apply_modifiers_uncached, but uses BASE's
6583 Qmodifier_cache property, if present. It also builds
6584 Qevent_symbol_elements properties, since it has that info anyway.
6586 apply_modifiers copies the value of BASE's Qevent_kind property to
6587 the modified symbol. */
6588 static Lisp_Object
6589 apply_modifiers (modifiers, base)
6590 int modifiers;
6591 Lisp_Object base;
6593 Lisp_Object cache, index, entry, new_symbol;
6595 /* Mask out upper bits. We don't know where this value's been. */
6596 modifiers &= INTMASK;
6598 if (INTEGERP (base))
6599 return make_number (XINT (base) | modifiers);
6601 /* The click modifier never figures into cache indices. */
6602 cache = Fget (base, Qmodifier_cache);
6603 XSETFASTINT (index, (modifiers & ~click_modifier));
6604 entry = assq_no_quit (index, cache);
6606 if (CONSP (entry))
6607 new_symbol = XCDR (entry);
6608 else
6610 /* We have to create the symbol ourselves. */
6611 new_symbol = apply_modifiers_uncached (modifiers,
6612 SDATA (SYMBOL_NAME (base)),
6613 SCHARS (SYMBOL_NAME (base)),
6614 SBYTES (SYMBOL_NAME (base)));
6616 /* Add the new symbol to the base's cache. */
6617 entry = Fcons (index, new_symbol);
6618 Fput (base, Qmodifier_cache, Fcons (entry, cache));
6620 /* We have the parsing info now for free, so we could add it to
6621 the caches:
6622 XSETFASTINT (index, modifiers);
6623 Fput (new_symbol, Qevent_symbol_element_mask,
6624 Fcons (base, Fcons (index, Qnil)));
6625 Fput (new_symbol, Qevent_symbol_elements,
6626 Fcons (base, lispy_modifier_list (modifiers)));
6627 Sadly, this is only correct if `base' is indeed a base event,
6628 which is not necessarily the case. -stef */
6631 /* Make sure this symbol is of the same kind as BASE.
6633 You'd think we could just set this once and for all when we
6634 intern the symbol above, but reorder_modifiers may call us when
6635 BASE's property isn't set right; we can't assume that just
6636 because it has a Qmodifier_cache property it must have its
6637 Qevent_kind set right as well. */
6638 if (NILP (Fget (new_symbol, Qevent_kind)))
6640 Lisp_Object kind;
6642 kind = Fget (base, Qevent_kind);
6643 if (! NILP (kind))
6644 Fput (new_symbol, Qevent_kind, kind);
6647 return new_symbol;
6651 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
6652 return a symbol with the modifiers placed in the canonical order.
6653 Canonical order is alphabetical, except for down and drag, which
6654 always come last. The 'click' modifier is never written out.
6656 Fdefine_key calls this to make sure that (for example) C-M-foo
6657 and M-C-foo end up being equivalent in the keymap. */
6659 Lisp_Object
6660 reorder_modifiers (symbol)
6661 Lisp_Object symbol;
6663 /* It's hopefully okay to write the code this way, since everything
6664 will soon be in caches, and no consing will be done at all. */
6665 Lisp_Object parsed;
6667 parsed = parse_modifiers (symbol);
6668 return apply_modifiers ((int) XINT (XCAR (XCDR (parsed))),
6669 XCAR (parsed));
6673 /* For handling events, we often want to produce a symbol whose name
6674 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
6675 to some base, like the name of a function key or mouse button.
6676 modify_event_symbol produces symbols of this sort.
6678 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
6679 is the name of the i'th symbol. TABLE_SIZE is the number of elements
6680 in the table.
6682 Alternatively, NAME_ALIST_OR_STEM is either an alist mapping codes
6683 into symbol names, or a string specifying a name stem used to
6684 construct a symbol name or the form `STEM-N', where N is the decimal
6685 representation of SYMBOL_NUM. NAME_ALIST_OR_STEM is used if it is
6686 non-nil; otherwise NAME_TABLE is used.
6688 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
6689 persist between calls to modify_event_symbol that it can use to
6690 store a cache of the symbols it's generated for this NAME_TABLE
6691 before. The object stored there may be a vector or an alist.
6693 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
6695 MODIFIERS is a set of modifier bits (as given in struct input_events)
6696 whose prefixes should be applied to the symbol name.
6698 SYMBOL_KIND is the value to be placed in the event_kind property of
6699 the returned symbol.
6701 The symbols we create are supposed to have an
6702 `event-symbol-elements' property, which lists the modifiers present
6703 in the symbol's name. */
6705 static Lisp_Object
6706 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist_or_stem,
6707 name_table, symbol_table, table_size)
6708 int symbol_num;
6709 unsigned modifiers;
6710 Lisp_Object symbol_kind;
6711 Lisp_Object name_alist_or_stem;
6712 char **name_table;
6713 Lisp_Object *symbol_table;
6714 unsigned int table_size;
6716 Lisp_Object value;
6717 Lisp_Object symbol_int;
6719 /* Get rid of the "vendor-specific" bit here. */
6720 XSETINT (symbol_int, symbol_num & 0xffffff);
6722 /* Is this a request for a valid symbol? */
6723 if (symbol_num < 0 || symbol_num >= table_size)
6724 return Qnil;
6726 if (CONSP (*symbol_table))
6727 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
6729 /* If *symbol_table doesn't seem to be initialized properly, fix that.
6730 *symbol_table should be a lisp vector TABLE_SIZE elements long,
6731 where the Nth element is the symbol for NAME_TABLE[N], or nil if
6732 we've never used that symbol before. */
6733 else
6735 if (! VECTORP (*symbol_table)
6736 || XVECTOR (*symbol_table)->size != table_size)
6738 Lisp_Object size;
6740 XSETFASTINT (size, table_size);
6741 *symbol_table = Fmake_vector (size, Qnil);
6744 value = XVECTOR (*symbol_table)->contents[symbol_num];
6747 /* Have we already used this symbol before? */
6748 if (NILP (value))
6750 /* No; let's create it. */
6751 if (CONSP (name_alist_or_stem))
6752 value = Fcdr_safe (Fassq (symbol_int, name_alist_or_stem));
6753 else if (STRINGP (name_alist_or_stem))
6755 int len = SBYTES (name_alist_or_stem);
6756 char *buf = (char *) alloca (len + 50);
6757 sprintf (buf, "%s-%ld", SDATA (name_alist_or_stem),
6758 (long) XINT (symbol_int) + 1);
6759 value = intern (buf);
6761 else if (name_table != 0 && name_table[symbol_num])
6762 value = intern (name_table[symbol_num]);
6764 #ifdef HAVE_WINDOW_SYSTEM
6765 if (NILP (value))
6767 char *name = x_get_keysym_name (symbol_num);
6768 if (name)
6769 value = intern (name);
6771 #endif
6773 if (NILP (value))
6775 char buf[20];
6776 sprintf (buf, "key-%d", symbol_num);
6777 value = intern (buf);
6780 if (CONSP (*symbol_table))
6781 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
6782 else
6783 XVECTOR (*symbol_table)->contents[symbol_num] = value;
6785 /* Fill in the cache entries for this symbol; this also
6786 builds the Qevent_symbol_elements property, which the user
6787 cares about. */
6788 apply_modifiers (modifiers & click_modifier, value);
6789 Fput (value, Qevent_kind, symbol_kind);
6792 /* Apply modifiers to that symbol. */
6793 return apply_modifiers (modifiers, value);
6796 /* Convert a list that represents an event type,
6797 such as (ctrl meta backspace), into the usual representation of that
6798 event type as a number or a symbol. */
6800 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
6801 doc: /* Convert the event description list EVENT-DESC to an event type.
6802 EVENT-DESC should contain one base event type (a character or symbol)
6803 and zero or more modifier names (control, meta, hyper, super, shift, alt,
6804 drag, down, double or triple). The base must be last.
6805 The return value is an event type (a character or symbol) which
6806 has the same base event type and all the specified modifiers. */)
6807 (event_desc)
6808 Lisp_Object event_desc;
6810 Lisp_Object base;
6811 int modifiers = 0;
6812 Lisp_Object rest;
6814 base = Qnil;
6815 rest = event_desc;
6816 while (CONSP (rest))
6818 Lisp_Object elt;
6819 int this = 0;
6821 elt = XCAR (rest);
6822 rest = XCDR (rest);
6824 /* Given a symbol, see if it is a modifier name. */
6825 if (SYMBOLP (elt) && CONSP (rest))
6826 this = parse_solitary_modifier (elt);
6828 if (this != 0)
6829 modifiers |= this;
6830 else if (!NILP (base))
6831 error ("Two bases given in one event");
6832 else
6833 base = elt;
6837 /* Let the symbol A refer to the character A. */
6838 if (SYMBOLP (base) && SCHARS (SYMBOL_NAME (base)) == 1)
6839 XSETINT (base, SREF (SYMBOL_NAME (base), 0));
6841 if (INTEGERP (base))
6843 /* Turn (shift a) into A. */
6844 if ((modifiers & shift_modifier) != 0
6845 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
6847 XSETINT (base, XINT (base) - ('a' - 'A'));
6848 modifiers &= ~shift_modifier;
6851 /* Turn (control a) into C-a. */
6852 if (modifiers & ctrl_modifier)
6853 return make_number ((modifiers & ~ctrl_modifier)
6854 | make_ctrl_char (XINT (base)));
6855 else
6856 return make_number (modifiers | XINT (base));
6858 else if (SYMBOLP (base))
6859 return apply_modifiers (modifiers, base);
6860 else
6862 error ("Invalid base event");
6863 return Qnil;
6867 /* Try to recognize SYMBOL as a modifier name.
6868 Return the modifier flag bit, or 0 if not recognized. */
6870 static int
6871 parse_solitary_modifier (symbol)
6872 Lisp_Object symbol;
6874 Lisp_Object name = SYMBOL_NAME (symbol);
6876 switch (SREF (name, 0))
6878 #define SINGLE_LETTER_MOD(BIT) \
6879 if (SBYTES (name) == 1) \
6880 return BIT;
6882 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
6883 if (LEN == SBYTES (name) \
6884 && ! strncmp (SDATA (name), NAME, LEN)) \
6885 return BIT;
6887 case 'A':
6888 SINGLE_LETTER_MOD (alt_modifier);
6889 break;
6891 case 'a':
6892 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
6893 break;
6895 case 'C':
6896 SINGLE_LETTER_MOD (ctrl_modifier);
6897 break;
6899 case 'c':
6900 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
6901 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
6902 break;
6904 case 'H':
6905 SINGLE_LETTER_MOD (hyper_modifier);
6906 break;
6908 case 'h':
6909 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
6910 break;
6912 case 'M':
6913 SINGLE_LETTER_MOD (meta_modifier);
6914 break;
6916 case 'm':
6917 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
6918 break;
6920 case 'S':
6921 SINGLE_LETTER_MOD (shift_modifier);
6922 break;
6924 case 's':
6925 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
6926 MULTI_LETTER_MOD (super_modifier, "super", 5);
6927 SINGLE_LETTER_MOD (super_modifier);
6928 break;
6930 case 'd':
6931 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
6932 MULTI_LETTER_MOD (down_modifier, "down", 4);
6933 MULTI_LETTER_MOD (double_modifier, "double", 6);
6934 break;
6936 case 't':
6937 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
6938 break;
6940 #undef SINGLE_LETTER_MOD
6941 #undef MULTI_LETTER_MOD
6944 return 0;
6947 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
6948 Such a list is not valid as an event,
6949 but it can be a Lucid-style event type list. */
6952 lucid_event_type_list_p (object)
6953 Lisp_Object object;
6955 Lisp_Object tail;
6957 if (! CONSP (object))
6958 return 0;
6960 if (EQ (XCAR (object), Qhelp_echo)
6961 || EQ (XCAR (object), Qvertical_line)
6962 || EQ (XCAR (object), Qmode_line)
6963 || EQ (XCAR (object), Qheader_line))
6964 return 0;
6966 for (tail = object; CONSP (tail); tail = XCDR (tail))
6968 Lisp_Object elt;
6969 elt = XCAR (tail);
6970 if (! (INTEGERP (elt) || SYMBOLP (elt)))
6971 return 0;
6974 return NILP (tail);
6977 /* Store into *addr a value nonzero if terminal input chars are available.
6978 Serves the purpose of ioctl (0, FIONREAD, addr)
6979 but works even if FIONREAD does not exist.
6980 (In fact, this may actually read some input.)
6982 If READABLE_EVENTS_DO_TIMERS_NOW is set in FLAGS, actually run
6983 timer events that are ripe.
6984 If READABLE_EVENTS_FILTER_EVENTS is set in FLAGS, ignore internal
6985 events (FOCUS_IN_EVENT).
6986 If READABLE_EVENTS_IGNORE_SQUEEZABLES is set in FLAGS, ignore mouse
6987 movements and toolkit scroll bar thumb drags. */
6989 static void
6990 get_input_pending (addr, flags)
6991 int *addr;
6992 int flags;
6994 /* First of all, have we already counted some input? */
6995 *addr = (!NILP (Vquit_flag) || readable_events (flags));
6997 /* If input is being read as it arrives, and we have none, there is none. */
6998 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
6999 return;
7001 /* Try to read some input and see how much we get. */
7002 gobble_input (0);
7003 *addr = (!NILP (Vquit_flag) || readable_events (flags));
7006 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
7008 void
7009 gobble_input (expected)
7010 int expected;
7012 #ifdef HAVE_DBUS
7013 /* Check whether a D-Bus message has arrived. */
7014 xd_read_queued_messages ();
7015 #endif /* HAVE_DBUS */
7017 #ifndef VMS
7018 #ifdef SIGIO
7019 if (interrupt_input)
7021 SIGMASKTYPE mask;
7022 mask = sigblock (sigmask (SIGIO));
7023 read_avail_input (expected);
7024 sigsetmask (mask);
7026 else
7027 #ifdef POLL_FOR_INPUT
7028 /* XXX This condition was (read_socket_hook && !interrupt_input),
7029 but read_socket_hook is not global anymore. Let's pretend that
7030 it's always set. */
7031 if (!interrupt_input && poll_suppress_count == 0)
7033 SIGMASKTYPE mask;
7034 mask = sigblock (sigmask (SIGALRM));
7035 read_avail_input (expected);
7036 sigsetmask (mask);
7038 else
7039 #endif
7040 #endif
7041 read_avail_input (expected);
7042 #endif
7045 /* Put a BUFFER_SWITCH_EVENT in the buffer
7046 so that read_key_sequence will notice the new current buffer. */
7048 void
7049 record_asynch_buffer_change ()
7051 struct input_event event;
7052 Lisp_Object tem;
7053 EVENT_INIT (event);
7055 event.kind = BUFFER_SWITCH_EVENT;
7056 event.frame_or_window = Qnil;
7057 event.arg = Qnil;
7059 #ifdef subprocesses
7060 /* We don't need a buffer-switch event unless Emacs is waiting for input.
7061 The purpose of the event is to make read_key_sequence look up the
7062 keymaps again. If we aren't in read_key_sequence, we don't need one,
7063 and the event could cause trouble by messing up (input-pending-p). */
7064 tem = Fwaiting_for_user_input_p ();
7065 if (NILP (tem))
7066 return;
7067 #else
7068 /* We never need these events if we have no asynchronous subprocesses. */
7069 return;
7070 #endif
7072 /* Make sure no interrupt happens while storing the event. */
7073 #ifdef SIGIO
7074 if (interrupt_input)
7076 SIGMASKTYPE mask;
7077 mask = sigblock (sigmask (SIGIO));
7078 kbd_buffer_store_event (&event);
7079 sigsetmask (mask);
7081 else
7082 #endif
7084 stop_polling ();
7085 kbd_buffer_store_event (&event);
7086 start_polling ();
7090 #ifndef VMS
7092 /* Read any terminal input already buffered up by the system
7093 into the kbd_buffer, but do not wait.
7095 EXPECTED should be nonzero if the caller knows there is some input.
7097 Except on VMS, all input is read by this function.
7098 If interrupt_input is nonzero, this function MUST be called
7099 only when SIGIO is blocked.
7101 Returns the number of keyboard chars read, or -1 meaning
7102 this is a bad time to try to read input. */
7104 static int
7105 read_avail_input (expected)
7106 int expected;
7108 int nread = 0;
7109 int err = 0;
7110 struct terminal *t;
7112 /* Store pending user signal events, if any. */
7113 if (store_user_signal_events ())
7114 expected = 0;
7116 /* Loop through the available terminals, and call their input hooks. */
7117 t = terminal_list;
7118 while (t)
7120 struct terminal *next = t->next_terminal;
7122 if (t->read_socket_hook)
7124 int nr;
7125 struct input_event hold_quit;
7127 EVENT_INIT (hold_quit);
7128 hold_quit.kind = NO_EVENT;
7130 /* No need for FIONREAD or fcntl; just say don't wait. */
7131 while (nr = (*t->read_socket_hook) (t, expected, &hold_quit), nr > 0)
7133 nread += nr;
7134 expected = 0;
7137 if (nr == -1) /* Not OK to read input now. */
7139 err = 1;
7141 else if (nr == -2) /* Non-transient error. */
7143 /* The terminal device terminated; it should be closed. */
7145 /* Kill Emacs if this was our last terminal. */
7146 if (!terminal_list->next_terminal)
7147 /* Formerly simply reported no input, but that
7148 sometimes led to a failure of Emacs to terminate.
7149 SIGHUP seems appropriate if we can't reach the
7150 terminal. */
7151 /* ??? Is it really right to send the signal just to
7152 this process rather than to the whole process
7153 group? Perhaps on systems with FIONREAD Emacs is
7154 alone in its group. */
7155 kill (getpid (), SIGHUP);
7157 /* XXX Is calling delete_terminal safe here? It calls Fdelete_frame. */
7159 Lisp_Object tmp;
7160 XSETTERMINAL (tmp, t);
7161 Fdelete_terminal (tmp, Qnoelisp);
7165 if (hold_quit.kind != NO_EVENT)
7166 kbd_buffer_store_event (&hold_quit);
7169 t = next;
7172 if (err && !nread)
7173 nread = -1;
7175 return nread;
7178 /* This is the tty way of reading available input.
7180 Note that each terminal device has its own `struct terminal' object,
7181 and so this function is called once for each individual termcap
7182 terminal. The first parameter indicates which terminal to read from. */
7185 tty_read_avail_input (struct terminal *terminal,
7186 int expected,
7187 struct input_event *hold_quit)
7189 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
7190 the kbd_buffer can really hold. That may prevent loss
7191 of characters on some systems when input is stuffed at us. */
7192 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
7193 int n_to_read, i;
7194 struct tty_display_info *tty = terminal->display_info.tty;
7195 int nread = 0;
7197 if (!terminal->name) /* Don't read from a dead terminal. */
7198 return 0;
7200 if (terminal->type != output_termcap)
7201 abort ();
7203 /* XXX I think the following code should be moved to separate hook
7204 functions in system-dependent files. */
7205 #ifdef WINDOWSNT
7206 return 0;
7207 #else /* not WINDOWSNT */
7208 #ifdef MSDOS
7209 n_to_read = dos_keysns ();
7210 if (n_to_read == 0)
7211 return 0;
7213 cbuf[0] = dos_keyread ();
7214 nread = 1;
7216 #else /* not MSDOS */
7218 if (! tty->term_initted) /* In case we get called during bootstrap. */
7219 return 0;
7221 if (! tty->input)
7222 return 0; /* The terminal is suspended. */
7224 #ifdef HAVE_GPM
7225 if (gpm_tty == tty)
7227 Gpm_Event event;
7228 struct input_event hold_quit;
7229 int gpm;
7231 EVENT_INIT (hold_quit);
7232 hold_quit.kind = NO_EVENT;
7234 while (gpm = Gpm_GetEvent (&event), gpm == 1) {
7235 nread += handle_one_term_event (tty, &event, &hold_quit);
7237 if (hold_quit.kind != NO_EVENT)
7238 kbd_buffer_store_event (&hold_quit);
7239 if (nread)
7240 return nread;
7242 #endif /* HAVE_GPM */
7244 /* Determine how many characters we should *try* to read. */
7245 #ifdef FIONREAD
7246 /* Find out how much input is available. */
7247 if (ioctl (fileno (tty->input), FIONREAD, &n_to_read) < 0)
7249 if (! noninteractive)
7250 return -2; /* Close this terminal. */
7251 else
7252 n_to_read = 0;
7254 if (n_to_read == 0)
7255 return 0;
7256 if (n_to_read > sizeof cbuf)
7257 n_to_read = sizeof cbuf;
7258 #else /* no FIONREAD */
7259 #if defined (USG) || defined(CYGWIN)
7260 /* Read some input if available, but don't wait. */
7261 n_to_read = sizeof cbuf;
7262 fcntl (fileno (tty->input), F_SETFL, O_NDELAY);
7263 #else
7264 you lose;
7265 #endif
7266 #endif
7268 /* Now read; for one reason or another, this will not block.
7269 NREAD is set to the number of chars read. */
7272 nread = emacs_read (fileno (tty->input), cbuf, n_to_read);
7273 /* POSIX infers that processes which are not in the session leader's
7274 process group won't get SIGHUP's at logout time. BSDI adheres to
7275 this part standard and returns -1 from read (0) with errno==EIO
7276 when the control tty is taken away.
7277 Jeffrey Honig <jch@bsdi.com> says this is generally safe. */
7278 if (nread == -1 && errno == EIO)
7279 return -2; /* Close this terminal. */
7280 #if defined (AIX) && defined (_BSD)
7281 /* The kernel sometimes fails to deliver SIGHUP for ptys.
7282 This looks incorrect, but it isn't, because _BSD causes
7283 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
7284 and that causes a value other than 0 when there is no input. */
7285 if (nread == 0)
7286 return -2; /* Close this terminal. */
7287 #endif
7289 while (
7290 /* We used to retry the read if it was interrupted.
7291 But this does the wrong thing when O_NDELAY causes
7292 an EAGAIN error. Does anybody know of a situation
7293 where a retry is actually needed? */
7294 #if 0
7295 nread < 0 && (errno == EAGAIN
7296 #ifdef EFAULT
7297 || errno == EFAULT
7298 #endif
7299 #ifdef EBADSLT
7300 || errno == EBADSLT
7301 #endif
7303 #else
7305 #endif
7308 #ifndef FIONREAD
7309 #if defined (USG) || defined (CYGWIN)
7310 fcntl (fileno (tty->input), F_SETFL, 0);
7311 #endif /* USG or CYGWIN */
7312 #endif /* no FIONREAD */
7314 if (nread <= 0)
7315 return nread;
7317 #endif /* not MSDOS */
7318 #endif /* not WINDOWSNT */
7320 for (i = 0; i < nread; i++)
7322 struct input_event buf;
7323 EVENT_INIT (buf);
7324 buf.kind = ASCII_KEYSTROKE_EVENT;
7325 buf.modifiers = 0;
7326 if (tty->meta_key == 1 && (cbuf[i] & 0x80))
7327 buf.modifiers = meta_modifier;
7328 if (tty->meta_key != 2)
7329 cbuf[i] &= ~0x80;
7331 buf.code = cbuf[i];
7332 /* Set the frame corresponding to the active tty. Note that the
7333 value of selected_frame is not reliable here, redisplay tends
7334 to temporarily change it. */
7335 buf.frame_or_window = tty->top_frame;
7336 buf.arg = Qnil;
7338 kbd_buffer_store_event (&buf);
7339 /* Don't look at input that follows a C-g too closely.
7340 This reduces lossage due to autorepeat on C-g. */
7341 if (buf.kind == ASCII_KEYSTROKE_EVENT
7342 && buf.code == quit_char)
7343 break;
7346 return nread;
7348 #endif /* not VMS */
7350 void
7351 handle_async_input ()
7353 #ifdef BSD4_1
7354 extern int select_alarmed;
7355 #endif
7357 interrupt_input_pending = 0;
7359 while (1)
7361 int nread;
7362 nread = read_avail_input (1);
7363 /* -1 means it's not ok to read the input now.
7364 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
7365 0 means there was no keyboard input available. */
7366 if (nread <= 0)
7367 break;
7369 #ifdef BSD4_1
7370 select_alarmed = 1; /* Force the select emulator back to life */
7371 #endif
7375 #ifdef SIGIO /* for entire page */
7376 /* Note SIGIO has been undef'd if FIONREAD is missing. */
7378 static SIGTYPE
7379 input_available_signal (signo)
7380 int signo;
7382 /* Must preserve main program's value of errno. */
7383 int old_errno = errno;
7384 #if defined (USG) && !defined (POSIX_SIGNALS)
7385 /* USG systems forget handlers when they are used;
7386 must reestablish each time */
7387 signal (signo, input_available_signal);
7388 #endif /* USG */
7390 #ifdef BSD4_1
7391 sigisheld (SIGIO);
7392 #endif
7394 #ifdef SYNC_INPUT
7395 interrupt_input_pending = 1;
7396 #else
7397 SIGNAL_THREAD_CHECK (signo);
7398 #endif
7400 if (input_available_clear_time)
7401 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
7403 #ifndef SYNC_INPUT
7404 handle_async_input ();
7405 #endif
7407 #ifdef BSD4_1
7408 sigfree ();
7409 #endif
7410 errno = old_errno;
7412 #endif /* SIGIO */
7414 /* Send ourselves a SIGIO.
7416 This function exists so that the UNBLOCK_INPUT macro in
7417 blockinput.h can have some way to take care of input we put off
7418 dealing with, without assuming that every file which uses
7419 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
7420 void
7421 reinvoke_input_signal ()
7423 #ifdef SIGIO
7424 handle_async_input ();
7425 #endif
7430 /* User signal events. */
7432 struct user_signal_info
7434 /* Signal number. */
7435 int sig;
7437 /* Name of the signal. */
7438 char *name;
7440 /* Number of pending signals. */
7441 int npending;
7443 struct user_signal_info *next;
7446 /* List of user signals. */
7447 static struct user_signal_info *user_signals = NULL;
7449 void
7450 add_user_signal (sig, name)
7451 int sig;
7452 const char *name;
7454 struct user_signal_info *p;
7456 for (p = user_signals; p; p = p->next)
7457 if (p->sig == sig)
7458 /* Already added. */
7459 return;
7461 p = xmalloc (sizeof (struct user_signal_info));
7462 p->sig = sig;
7463 p->name = xstrdup (name);
7464 p->npending = 0;
7465 p->next = user_signals;
7466 user_signals = p;
7468 signal (sig, handle_user_signal);
7471 static SIGTYPE
7472 handle_user_signal (sig)
7473 int sig;
7475 int old_errno = errno;
7476 struct user_signal_info *p;
7478 #if defined (USG) && !defined (POSIX_SIGNALS)
7479 /* USG systems forget handlers when they are used;
7480 must reestablish each time */
7481 signal (sig, handle_user_signal);
7482 #endif
7484 SIGNAL_THREAD_CHECK (sig);
7486 for (p = user_signals; p; p = p->next)
7487 if (p->sig == sig)
7489 p->npending++;
7490 #ifdef SIGIO
7491 if (interrupt_input)
7492 kill (getpid (), SIGIO);
7493 else
7494 #endif
7496 /* Tell wait_reading_process_output that it needs to wake
7497 up and look around. */
7498 if (input_available_clear_time)
7499 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
7501 break;
7504 errno = old_errno;
7507 static char *
7508 find_user_signal_name (sig)
7509 int sig;
7511 struct user_signal_info *p;
7513 for (p = user_signals; p; p = p->next)
7514 if (p->sig == sig)
7515 return p->name;
7517 return NULL;
7520 static int
7521 store_user_signal_events ()
7523 struct user_signal_info *p;
7524 struct input_event buf;
7525 int nstored = 0;
7527 for (p = user_signals; p; p = p->next)
7528 if (p->npending > 0)
7530 SIGMASKTYPE mask;
7532 if (nstored == 0)
7534 bzero (&buf, sizeof buf);
7535 buf.kind = USER_SIGNAL_EVENT;
7536 buf.frame_or_window = selected_frame;
7538 nstored += p->npending;
7540 mask = sigblock (sigmask (p->sig));
7543 buf.code = p->sig;
7544 kbd_buffer_store_event (&buf);
7545 p->npending--;
7547 while (p->npending > 0);
7548 sigsetmask (mask);
7551 return nstored;
7555 static void menu_bar_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object, void*));
7556 static Lisp_Object menu_bar_one_keymap_changed_items;
7558 /* These variables hold the vector under construction within
7559 menu_bar_items and its subroutines, and the current index
7560 for storing into that vector. */
7561 static Lisp_Object menu_bar_items_vector;
7562 static int menu_bar_items_index;
7564 /* Return a vector of menu items for a menu bar, appropriate
7565 to the current buffer. Each item has three elements in the vector:
7566 KEY STRING MAPLIST.
7568 OLD is an old vector we can optionally reuse, or nil. */
7570 Lisp_Object
7571 menu_bar_items (old)
7572 Lisp_Object old;
7574 /* The number of keymaps we're scanning right now, and the number of
7575 keymaps we have allocated space for. */
7576 int nmaps;
7578 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
7579 in the current keymaps, or nil where it is not a prefix. */
7580 Lisp_Object *maps;
7582 Lisp_Object def, tail;
7584 Lisp_Object result;
7586 int mapno;
7587 Lisp_Object oquit;
7589 int i;
7591 /* In order to build the menus, we need to call the keymap
7592 accessors. They all call QUIT. But this function is called
7593 during redisplay, during which a quit is fatal. So inhibit
7594 quitting while building the menus.
7595 We do this instead of specbind because (1) errors will clear it anyway
7596 and (2) this avoids risk of specpdl overflow. */
7597 oquit = Vinhibit_quit;
7598 Vinhibit_quit = Qt;
7600 if (!NILP (old))
7601 menu_bar_items_vector = old;
7602 else
7603 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
7604 menu_bar_items_index = 0;
7606 /* Build our list of keymaps.
7607 If we recognize a function key and replace its escape sequence in
7608 keybuf with its symbol, or if the sequence starts with a mouse
7609 click and we need to switch buffers, we jump back here to rebuild
7610 the initial keymaps from the current buffer. */
7612 Lisp_Object *tmaps;
7614 /* Should overriding-terminal-local-map and overriding-local-map apply? */
7615 if (!NILP (Voverriding_local_map_menu_flag))
7617 /* Yes, use them (if non-nil) as well as the global map. */
7618 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
7619 nmaps = 0;
7620 if (!NILP (current_kboard->Voverriding_terminal_local_map))
7621 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
7622 if (!NILP (Voverriding_local_map))
7623 maps[nmaps++] = Voverriding_local_map;
7625 else
7627 /* No, so use major and minor mode keymaps and keymap property.
7628 Note that menu-bar bindings in the local-map and keymap
7629 properties may not work reliable, as they are only
7630 recognized when the menu-bar (or mode-line) is updated,
7631 which does not normally happen after every command. */
7632 Lisp_Object tem;
7633 int nminor;
7634 nminor = current_minor_maps (NULL, &tmaps);
7635 maps = (Lisp_Object *) alloca ((nminor + 3) * sizeof (maps[0]));
7636 nmaps = 0;
7637 if (tem = get_local_map (PT, current_buffer, Qkeymap), !NILP (tem))
7638 maps[nmaps++] = tem;
7639 bcopy (tmaps, (void *) (maps + nmaps), nminor * sizeof (maps[0]));
7640 nmaps += nminor;
7641 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
7643 maps[nmaps++] = current_global_map;
7646 /* Look up in each map the dummy prefix key `menu-bar'. */
7648 result = Qnil;
7650 for (mapno = nmaps - 1; mapno >= 0; mapno--)
7651 if (!NILP (maps[mapno]))
7653 def = get_keymap (access_keymap (maps[mapno], Qmenu_bar, 1, 0, 1),
7654 0, 1);
7655 if (CONSP (def))
7657 menu_bar_one_keymap_changed_items = Qnil;
7658 map_keymap (def, menu_bar_item, Qnil, NULL, 1);
7662 /* Move to the end those items that should be at the end. */
7664 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCDR (tail))
7666 int i;
7667 int end = menu_bar_items_index;
7669 for (i = 0; i < end; i += 4)
7670 if (EQ (XCAR (tail), XVECTOR (menu_bar_items_vector)->contents[i]))
7672 Lisp_Object tem0, tem1, tem2, tem3;
7673 /* Move the item at index I to the end,
7674 shifting all the others forward. */
7675 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
7676 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
7677 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
7678 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
7679 if (end > i + 4)
7680 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
7681 &XVECTOR (menu_bar_items_vector)->contents[i],
7682 (end - i - 4) * sizeof (Lisp_Object));
7683 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
7684 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
7685 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
7686 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
7687 break;
7691 /* Add nil, nil, nil, nil at the end. */
7692 i = menu_bar_items_index;
7693 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
7694 menu_bar_items_vector = larger_vector (menu_bar_items_vector, 2 * i, Qnil);
7695 /* Add this item. */
7696 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
7697 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
7698 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
7699 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
7700 menu_bar_items_index = i;
7702 Vinhibit_quit = oquit;
7703 return menu_bar_items_vector;
7706 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
7707 If there's already an item for KEY, add this DEF to it. */
7709 Lisp_Object item_properties;
7711 static void
7712 menu_bar_item (key, item, dummy1, dummy2)
7713 Lisp_Object key, item, dummy1;
7714 void *dummy2;
7716 struct gcpro gcpro1;
7717 int i;
7718 Lisp_Object tem;
7720 if (EQ (item, Qundefined))
7722 /* If a map has an explicit `undefined' as definition,
7723 discard any previously made menu bar item. */
7725 for (i = 0; i < menu_bar_items_index; i += 4)
7726 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
7728 if (menu_bar_items_index > i + 4)
7729 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
7730 &XVECTOR (menu_bar_items_vector)->contents[i],
7731 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
7732 menu_bar_items_index -= 4;
7736 /* If this keymap has already contributed to this KEY,
7737 don't contribute to it a second time. */
7738 tem = Fmemq (key, menu_bar_one_keymap_changed_items);
7739 if (!NILP (tem) || NILP (item))
7740 return;
7742 menu_bar_one_keymap_changed_items
7743 = Fcons (key, menu_bar_one_keymap_changed_items);
7745 /* We add to menu_bar_one_keymap_changed_items before doing the
7746 parse_menu_item, so that if it turns out it wasn't a menu item,
7747 it still correctly hides any further menu item. */
7748 GCPRO1 (key);
7749 i = parse_menu_item (item, 0, 1);
7750 UNGCPRO;
7751 if (!i)
7752 return;
7754 item = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
7756 /* Find any existing item for this KEY. */
7757 for (i = 0; i < menu_bar_items_index; i += 4)
7758 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
7759 break;
7761 /* If we did not find this KEY, add it at the end. */
7762 if (i == menu_bar_items_index)
7764 /* If vector is too small, get a bigger one. */
7765 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
7766 menu_bar_items_vector = larger_vector (menu_bar_items_vector, 2 * i, Qnil);
7767 /* Add this item. */
7768 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
7769 XVECTOR (menu_bar_items_vector)->contents[i++]
7770 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
7771 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (item, Qnil);
7772 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
7773 menu_bar_items_index = i;
7775 /* We did find an item for this KEY. Add ITEM to its list of maps. */
7776 else
7778 Lisp_Object old;
7779 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
7780 /* If the new and the old items are not both keymaps,
7781 the lookup will only find `item'. */
7782 item = Fcons (item, KEYMAPP (item) && KEYMAPP (XCAR (old)) ? old : Qnil);
7783 XVECTOR (menu_bar_items_vector)->contents[i + 2] = item;
7787 /* This is used as the handler when calling menu_item_eval_property. */
7788 static Lisp_Object
7789 menu_item_eval_property_1 (arg)
7790 Lisp_Object arg;
7792 /* If we got a quit from within the menu computation,
7793 quit all the way out of it. This takes care of C-] in the debugger. */
7794 if (CONSP (arg) && EQ (XCAR (arg), Qquit))
7795 Fsignal (Qquit, Qnil);
7797 return Qnil;
7800 /* Evaluate an expression and return the result (or nil if something
7801 went wrong). Used to evaluate dynamic parts of menu items. */
7802 Lisp_Object
7803 menu_item_eval_property (sexpr)
7804 Lisp_Object sexpr;
7806 int count = SPECPDL_INDEX ();
7807 Lisp_Object val;
7808 specbind (Qinhibit_redisplay, Qt);
7809 val = internal_condition_case_1 (Feval, sexpr, Qerror,
7810 menu_item_eval_property_1);
7811 return unbind_to (count, val);
7814 /* This function parses a menu item and leaves the result in the
7815 vector item_properties.
7816 ITEM is a key binding, a possible menu item.
7817 If NOTREAL is nonzero, only check for equivalent key bindings, don't
7818 evaluate dynamic expressions in the menu item.
7819 INMENUBAR is > 0 when this is considered for an entry in a menu bar
7820 top level.
7821 INMENUBAR is < 0 when this is considered for an entry in a keyboard menu.
7822 parse_menu_item returns true if the item is a menu item and false
7823 otherwise. */
7826 parse_menu_item (item, notreal, inmenubar)
7827 Lisp_Object item;
7828 int notreal, inmenubar;
7830 Lisp_Object def, tem, item_string, start;
7831 Lisp_Object cachelist;
7832 Lisp_Object filter;
7833 Lisp_Object keyhint;
7834 int i;
7835 int newcache = 0;
7837 cachelist = Qnil;
7838 filter = Qnil;
7839 keyhint = Qnil;
7841 if (!CONSP (item))
7842 return 0;
7844 /* Create item_properties vector if necessary. */
7845 if (NILP (item_properties))
7846 item_properties
7847 = Fmake_vector (make_number (ITEM_PROPERTY_ENABLE + 1), Qnil);
7849 /* Initialize optional entries. */
7850 for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++)
7851 ASET (item_properties, i, Qnil);
7852 ASET (item_properties, ITEM_PROPERTY_ENABLE, Qt);
7854 /* Save the item here to protect it from GC. */
7855 ASET (item_properties, ITEM_PROPERTY_ITEM, item);
7857 item_string = XCAR (item);
7859 start = item;
7860 item = XCDR (item);
7861 if (STRINGP (item_string))
7863 /* Old format menu item. */
7864 ASET (item_properties, ITEM_PROPERTY_NAME, item_string);
7866 /* Maybe help string. */
7867 if (CONSP (item) && STRINGP (XCAR (item)))
7869 ASET (item_properties, ITEM_PROPERTY_HELP, XCAR (item));
7870 start = item;
7871 item = XCDR (item);
7874 /* Maybe key binding cache. */
7875 if (CONSP (item) && CONSP (XCAR (item))
7876 && (NILP (XCAR (XCAR (item)))
7877 || VECTORP (XCAR (XCAR (item)))))
7879 cachelist = XCAR (item);
7880 item = XCDR (item);
7883 /* This is the real definition--the function to run. */
7884 ASET (item_properties, ITEM_PROPERTY_DEF, item);
7886 /* Get enable property, if any. */
7887 if (SYMBOLP (item))
7889 tem = Fget (item, Qmenu_enable);
7890 if (!NILP (Venable_disabled_menus_and_buttons))
7891 ASET (item_properties, ITEM_PROPERTY_ENABLE, Qt);
7892 else if (!NILP (tem))
7893 ASET (item_properties, ITEM_PROPERTY_ENABLE, tem);
7896 else if (EQ (item_string, Qmenu_item) && CONSP (item))
7898 /* New format menu item. */
7899 ASET (item_properties, ITEM_PROPERTY_NAME, XCAR (item));
7900 start = XCDR (item);
7901 if (CONSP (start))
7903 /* We have a real binding. */
7904 ASET (item_properties, ITEM_PROPERTY_DEF, XCAR (start));
7906 item = XCDR (start);
7907 /* Is there a cache list with key equivalences. */
7908 if (CONSP (item) && CONSP (XCAR (item)))
7910 cachelist = XCAR (item);
7911 item = XCDR (item);
7914 /* Parse properties. */
7915 while (CONSP (item) && CONSP (XCDR (item)))
7917 tem = XCAR (item);
7918 item = XCDR (item);
7920 if (EQ (tem, QCenable))
7922 if (!NILP (Venable_disabled_menus_and_buttons))
7923 ASET (item_properties, ITEM_PROPERTY_ENABLE, Qt);
7924 else
7925 ASET (item_properties, ITEM_PROPERTY_ENABLE, XCAR (item));
7927 else if (EQ (tem, QCvisible) && !notreal)
7929 /* If got a visible property and that evaluates to nil
7930 then ignore this item. */
7931 tem = menu_item_eval_property (XCAR (item));
7932 if (NILP (tem))
7933 return 0;
7935 else if (EQ (tem, QChelp))
7936 ASET (item_properties, ITEM_PROPERTY_HELP, XCAR (item));
7937 else if (EQ (tem, QCfilter))
7938 filter = item;
7939 else if (EQ (tem, QCkey_sequence))
7941 tem = XCAR (item);
7942 if (NILP (cachelist)
7943 && (SYMBOLP (tem) || STRINGP (tem) || VECTORP (tem)))
7944 /* Be GC protected. Set keyhint to item instead of tem. */
7945 keyhint = item;
7947 else if (EQ (tem, QCkeys))
7949 tem = XCAR (item);
7950 if (CONSP (tem) || (STRINGP (tem) && NILP (cachelist)))
7951 ASET (item_properties, ITEM_PROPERTY_KEYEQ, tem);
7953 else if (EQ (tem, QCbutton) && CONSP (XCAR (item)))
7955 Lisp_Object type;
7956 tem = XCAR (item);
7957 type = XCAR (tem);
7958 if (EQ (type, QCtoggle) || EQ (type, QCradio))
7960 ASET (item_properties, ITEM_PROPERTY_SELECTED,
7961 XCDR (tem));
7962 ASET (item_properties, ITEM_PROPERTY_TYPE, type);
7965 item = XCDR (item);
7968 else if (inmenubar || !NILP (start))
7969 return 0;
7971 else
7972 return 0; /* not a menu item */
7974 /* If item string is not a string, evaluate it to get string.
7975 If we don't get a string, skip this item. */
7976 item_string = AREF (item_properties, ITEM_PROPERTY_NAME);
7977 if (!(STRINGP (item_string) || notreal))
7979 item_string = menu_item_eval_property (item_string);
7980 if (!STRINGP (item_string))
7981 return 0;
7982 ASET (item_properties, ITEM_PROPERTY_NAME, item_string);
7985 /* If got a filter apply it on definition. */
7986 def = AREF (item_properties, ITEM_PROPERTY_DEF);
7987 if (!NILP (filter))
7989 def = menu_item_eval_property (list2 (XCAR (filter),
7990 list2 (Qquote, def)));
7992 ASET (item_properties, ITEM_PROPERTY_DEF, def);
7995 /* Enable or disable selection of item. */
7996 tem = AREF (item_properties, ITEM_PROPERTY_ENABLE);
7997 if (!EQ (tem, Qt))
7999 if (notreal)
8000 tem = Qt;
8001 else
8002 tem = menu_item_eval_property (tem);
8003 if (inmenubar && NILP (tem))
8004 return 0; /* Ignore disabled items in menu bar. */
8005 ASET (item_properties, ITEM_PROPERTY_ENABLE, tem);
8008 /* If we got no definition, this item is just unselectable text which
8009 is OK in a submenu but not in the menubar. */
8010 if (NILP (def))
8011 return (inmenubar ? 0 : 1);
8013 /* See if this is a separate pane or a submenu. */
8014 def = AREF (item_properties, ITEM_PROPERTY_DEF);
8015 tem = get_keymap (def, 0, 1);
8016 /* For a subkeymap, just record its details and exit. */
8017 if (CONSP (tem))
8019 ASET (item_properties, ITEM_PROPERTY_MAP, tem);
8020 ASET (item_properties, ITEM_PROPERTY_DEF, tem);
8021 return 1;
8024 /* At the top level in the menu bar, do likewise for commands also.
8025 The menu bar does not display equivalent key bindings anyway.
8026 ITEM_PROPERTY_DEF is already set up properly. */
8027 if (inmenubar > 0)
8028 return 1;
8030 /* This is a command. See if there is an equivalent key binding. */
8031 if (NILP (cachelist))
8033 /* We have to create a cachelist. */
8034 CHECK_IMPURE (start);
8035 XSETCDR (start, Fcons (Fcons (Qnil, Qnil), XCDR (start)));
8036 cachelist = XCAR (XCDR (start));
8037 newcache = 1;
8038 tem = AREF (item_properties, ITEM_PROPERTY_KEYEQ);
8039 if (!NILP (keyhint))
8041 XSETCAR (cachelist, XCAR (keyhint));
8042 newcache = 0;
8044 else if (STRINGP (tem))
8046 XSETCDR (cachelist, Fsubstitute_command_keys (tem));
8047 XSETCAR (cachelist, Qt);
8051 tem = XCAR (cachelist);
8052 if (!EQ (tem, Qt))
8054 int chkcache = 0;
8055 Lisp_Object prefix;
8057 if (!NILP (tem))
8058 tem = Fkey_binding (tem, Qnil, Qnil, Qnil);
8060 prefix = AREF (item_properties, ITEM_PROPERTY_KEYEQ);
8061 if (CONSP (prefix))
8063 def = XCAR (prefix);
8064 prefix = XCDR (prefix);
8066 else
8067 def = AREF (item_properties, ITEM_PROPERTY_DEF);
8069 if (NILP (XCAR (cachelist))) /* Have no saved key. */
8071 if (newcache /* Always check first time. */
8072 /* Should we check everything when precomputing key
8073 bindings? */
8074 /* If something had no key binding before, don't recheck it
8075 because that is too slow--except if we have a list of
8076 rebound commands in Vdefine_key_rebound_commands, do
8077 recheck any command that appears in that list. */
8078 || (CONSP (Vdefine_key_rebound_commands)
8079 && !NILP (Fmemq (def, Vdefine_key_rebound_commands))))
8080 chkcache = 1;
8082 /* We had a saved key. Is it still bound to the command? */
8083 else if (NILP (tem)
8084 || (!EQ (tem, def)
8085 /* If the command is an alias for another
8086 (such as lmenu.el set it up), check if the
8087 original command matches the cached command. */
8088 && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function))))
8089 chkcache = 1; /* Need to recompute key binding. */
8091 if (chkcache)
8093 /* Recompute equivalent key binding. If the command is an alias
8094 for another (such as lmenu.el set it up), see if the original
8095 command name has equivalent keys. Otherwise look up the
8096 specified command itself. We don't try both, because that
8097 makes lmenu menus slow. */
8098 if (SYMBOLP (def)
8099 && SYMBOLP (XSYMBOL (def)->function)
8100 && ! NILP (Fget (def, Qmenu_alias)))
8101 def = XSYMBOL (def)->function;
8102 tem = Fwhere_is_internal (def, Qnil, Qt, Qnil, Qt);
8103 XSETCAR (cachelist, tem);
8104 if (NILP (tem))
8106 XSETCDR (cachelist, Qnil);
8107 chkcache = 0;
8110 else if (!NILP (keyhint) && !NILP (XCAR (cachelist)))
8112 tem = XCAR (cachelist);
8113 chkcache = 1;
8116 newcache = chkcache;
8117 if (chkcache)
8119 tem = Fkey_description (tem, Qnil);
8120 if (CONSP (prefix))
8122 if (STRINGP (XCAR (prefix)))
8123 tem = concat2 (XCAR (prefix), tem);
8124 if (STRINGP (XCDR (prefix)))
8125 tem = concat2 (tem, XCDR (prefix));
8127 XSETCDR (cachelist, tem);
8131 tem = XCDR (cachelist);
8132 if (newcache && !NILP (tem))
8134 tem = concat2 (build_string (" "), tem);
8135 // tem = concat3 (build_string (" ("), tem, build_string (")"));
8136 XSETCDR (cachelist, tem);
8139 /* If we only want to precompute equivalent key bindings, stop here. */
8140 if (notreal)
8141 return 1;
8143 /* If we have an equivalent key binding, use that. */
8144 ASET (item_properties, ITEM_PROPERTY_KEYEQ, tem);
8146 /* Include this when menu help is implemented.
8147 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP];
8148 if (!(NILP (tem) || STRINGP (tem)))
8150 tem = menu_item_eval_property (tem);
8151 if (!STRINGP (tem))
8152 tem = Qnil;
8153 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP] = tem;
8157 /* Handle radio buttons or toggle boxes. */
8158 tem = AREF (item_properties, ITEM_PROPERTY_SELECTED);
8159 if (!NILP (tem))
8160 ASET (item_properties, ITEM_PROPERTY_SELECTED,
8161 menu_item_eval_property (tem));
8163 return 1;
8168 /***********************************************************************
8169 Tool-bars
8170 ***********************************************************************/
8172 /* A vector holding tool bar items while they are parsed in function
8173 tool_bar_items. Each item occupies TOOL_BAR_ITEM_NSCLOTS elements
8174 in the vector. */
8176 static Lisp_Object tool_bar_items_vector;
8178 /* A vector holding the result of parse_tool_bar_item. Layout is like
8179 the one for a single item in tool_bar_items_vector. */
8181 static Lisp_Object tool_bar_item_properties;
8183 /* Next free index in tool_bar_items_vector. */
8185 static int ntool_bar_items;
8187 /* The symbols `tool-bar', `:image' and `:rtl'. */
8189 extern Lisp_Object Qtool_bar;
8190 Lisp_Object QCimage;
8191 Lisp_Object Qrtl;
8193 /* Function prototypes. */
8195 static void init_tool_bar_items P_ ((Lisp_Object));
8196 static void process_tool_bar_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object, void*));
8197 static int parse_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
8198 static void append_tool_bar_item P_ ((void));
8201 /* Return a vector of tool bar items for keymaps currently in effect.
8202 Reuse vector REUSE if non-nil. Return in *NITEMS the number of
8203 tool bar items found. */
8205 Lisp_Object
8206 tool_bar_items (reuse, nitems)
8207 Lisp_Object reuse;
8208 int *nitems;
8210 Lisp_Object *maps;
8211 int nmaps, i;
8212 Lisp_Object oquit;
8213 Lisp_Object *tmaps;
8215 *nitems = 0;
8217 /* In order to build the menus, we need to call the keymap
8218 accessors. They all call QUIT. But this function is called
8219 during redisplay, during which a quit is fatal. So inhibit
8220 quitting while building the menus. We do this instead of
8221 specbind because (1) errors will clear it anyway and (2) this
8222 avoids risk of specpdl overflow. */
8223 oquit = Vinhibit_quit;
8224 Vinhibit_quit = Qt;
8226 /* Initialize tool_bar_items_vector and protect it from GC. */
8227 init_tool_bar_items (reuse);
8229 /* Build list of keymaps in maps. Set nmaps to the number of maps
8230 to process. */
8232 /* Should overriding-terminal-local-map and overriding-local-map apply? */
8233 if (!NILP (Voverriding_local_map_menu_flag))
8235 /* Yes, use them (if non-nil) as well as the global map. */
8236 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
8237 nmaps = 0;
8238 if (!NILP (current_kboard->Voverriding_terminal_local_map))
8239 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
8240 if (!NILP (Voverriding_local_map))
8241 maps[nmaps++] = Voverriding_local_map;
8243 else
8245 /* No, so use major and minor mode keymaps and keymap property.
8246 Note that tool-bar bindings in the local-map and keymap
8247 properties may not work reliable, as they are only
8248 recognized when the tool-bar (or mode-line) is updated,
8249 which does not normally happen after every command. */
8250 Lisp_Object tem;
8251 int nminor;
8252 nminor = current_minor_maps (NULL, &tmaps);
8253 maps = (Lisp_Object *) alloca ((nminor + 3) * sizeof (maps[0]));
8254 nmaps = 0;
8255 if (tem = get_local_map (PT, current_buffer, Qkeymap), !NILP (tem))
8256 maps[nmaps++] = tem;
8257 bcopy (tmaps, (void *) (maps + nmaps), nminor * sizeof (maps[0]));
8258 nmaps += nminor;
8259 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
8262 /* Add global keymap at the end. */
8263 maps[nmaps++] = current_global_map;
8265 /* Process maps in reverse order and look up in each map the prefix
8266 key `tool-bar'. */
8267 for (i = nmaps - 1; i >= 0; --i)
8268 if (!NILP (maps[i]))
8270 Lisp_Object keymap;
8272 keymap = get_keymap (access_keymap (maps[i], Qtool_bar, 1, 0, 1), 0, 1);
8273 if (CONSP (keymap))
8274 map_keymap (keymap, process_tool_bar_item, Qnil, NULL, 1);
8277 Vinhibit_quit = oquit;
8278 *nitems = ntool_bar_items / TOOL_BAR_ITEM_NSLOTS;
8279 return tool_bar_items_vector;
8283 /* Process the definition of KEY which is DEF. */
8285 static void
8286 process_tool_bar_item (key, def, data, args)
8287 Lisp_Object key, def, data;
8288 void *args;
8290 int i;
8291 extern Lisp_Object Qundefined;
8292 struct gcpro gcpro1, gcpro2;
8294 /* Protect KEY and DEF from GC because parse_tool_bar_item may call
8295 eval. */
8296 GCPRO2 (key, def);
8298 if (EQ (def, Qundefined))
8300 /* If a map has an explicit `undefined' as definition,
8301 discard any previously made item. */
8302 for (i = 0; i < ntool_bar_items; i += TOOL_BAR_ITEM_NSLOTS)
8304 Lisp_Object *v = XVECTOR (tool_bar_items_vector)->contents + i;
8306 if (EQ (key, v[TOOL_BAR_ITEM_KEY]))
8308 if (ntool_bar_items > i + TOOL_BAR_ITEM_NSLOTS)
8309 bcopy (v + TOOL_BAR_ITEM_NSLOTS, v,
8310 ((ntool_bar_items - i - TOOL_BAR_ITEM_NSLOTS)
8311 * sizeof (Lisp_Object)));
8312 ntool_bar_items -= TOOL_BAR_ITEM_NSLOTS;
8313 break;
8317 else if (parse_tool_bar_item (key, def))
8318 /* Append a new tool bar item to tool_bar_items_vector. Accept
8319 more than one definition for the same key. */
8320 append_tool_bar_item ();
8322 UNGCPRO;
8326 /* Parse a tool bar item specification ITEM for key KEY and return the
8327 result in tool_bar_item_properties. Value is zero if ITEM is
8328 invalid.
8330 ITEM is a list `(menu-item CAPTION BINDING PROPS...)'.
8332 CAPTION is the caption of the item, If it's not a string, it is
8333 evaluated to get a string.
8335 BINDING is the tool bar item's binding. Tool-bar items with keymaps
8336 as binding are currently ignored.
8338 The following properties are recognized:
8340 - `:enable FORM'.
8342 FORM is evaluated and specifies whether the tool bar item is
8343 enabled or disabled.
8345 - `:visible FORM'
8347 FORM is evaluated and specifies whether the tool bar item is visible.
8349 - `:filter FUNCTION'
8351 FUNCTION is invoked with one parameter `(quote BINDING)'. Its
8352 result is stored as the new binding.
8354 - `:button (TYPE SELECTED)'
8356 TYPE must be one of `:radio' or `:toggle'. SELECTED is evaluated
8357 and specifies whether the button is selected (pressed) or not.
8359 - `:image IMAGES'
8361 IMAGES is either a single image specification or a vector of four
8362 image specifications. See enum tool_bar_item_images.
8364 - `:help HELP-STRING'.
8366 Gives a help string to display for the tool bar item. */
8368 static int
8369 parse_tool_bar_item (key, item)
8370 Lisp_Object key, item;
8372 /* Access slot with index IDX of vector tool_bar_item_properties. */
8373 #define PROP(IDX) XVECTOR (tool_bar_item_properties)->contents[IDX]
8375 Lisp_Object filter = Qnil;
8376 Lisp_Object caption;
8377 int i;
8379 /* Defininition looks like `(menu-item CAPTION BINDING PROPS...)'.
8380 Rule out items that aren't lists, don't start with
8381 `menu-item' or whose rest following `tool-bar-item' is not a
8382 list. */
8383 if (!CONSP (item)
8384 || !EQ (XCAR (item), Qmenu_item)
8385 || (item = XCDR (item),
8386 !CONSP (item)))
8387 return 0;
8389 /* Create tool_bar_item_properties vector if necessary. Reset it to
8390 defaults. */
8391 if (VECTORP (tool_bar_item_properties))
8393 for (i = 0; i < TOOL_BAR_ITEM_NSLOTS; ++i)
8394 PROP (i) = Qnil;
8396 else
8397 tool_bar_item_properties
8398 = Fmake_vector (make_number (TOOL_BAR_ITEM_NSLOTS), Qnil);
8400 /* Set defaults. */
8401 PROP (TOOL_BAR_ITEM_KEY) = key;
8402 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
8404 /* Get the caption of the item. If the caption is not a string,
8405 evaluate it to get a string. If we don't get a string, skip this
8406 item. */
8407 caption = XCAR (item);
8408 if (!STRINGP (caption))
8410 caption = menu_item_eval_property (caption);
8411 if (!STRINGP (caption))
8412 return 0;
8414 PROP (TOOL_BAR_ITEM_CAPTION) = caption;
8416 /* Give up if rest following the caption is not a list. */
8417 item = XCDR (item);
8418 if (!CONSP (item))
8419 return 0;
8421 /* Store the binding. */
8422 PROP (TOOL_BAR_ITEM_BINDING) = XCAR (item);
8423 item = XCDR (item);
8425 /* Ignore cached key binding, if any. */
8426 if (CONSP (item) && CONSP (XCAR (item)))
8427 item = XCDR (item);
8429 /* Process the rest of the properties. */
8430 for (; CONSP (item) && CONSP (XCDR (item)); item = XCDR (XCDR (item)))
8432 Lisp_Object key, value;
8434 key = XCAR (item);
8435 value = XCAR (XCDR (item));
8437 if (EQ (key, QCenable))
8439 /* `:enable FORM'. */
8440 if (!NILP (Venable_disabled_menus_and_buttons))
8441 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
8442 else
8443 PROP (TOOL_BAR_ITEM_ENABLED_P) = value;
8445 else if (EQ (key, QCvisible))
8447 /* `:visible FORM'. If got a visible property and that
8448 evaluates to nil then ignore this item. */
8449 if (NILP (menu_item_eval_property (value)))
8450 return 0;
8452 else if (EQ (key, QChelp))
8453 /* `:help HELP-STRING'. */
8454 PROP (TOOL_BAR_ITEM_HELP) = value;
8455 else if (EQ (key, QCfilter))
8456 /* ':filter FORM'. */
8457 filter = value;
8458 else if (EQ (key, QCbutton) && CONSP (value))
8460 /* `:button (TYPE . SELECTED)'. */
8461 Lisp_Object type, selected;
8463 type = XCAR (value);
8464 selected = XCDR (value);
8465 if (EQ (type, QCtoggle) || EQ (type, QCradio))
8467 PROP (TOOL_BAR_ITEM_SELECTED_P) = selected;
8468 PROP (TOOL_BAR_ITEM_TYPE) = type;
8471 else if (EQ (key, QCimage)
8472 && (CONSP (value)
8473 || (VECTORP (value) && XVECTOR (value)->size == 4)))
8474 /* Value is either a single image specification or a vector
8475 of 4 such specifications for the different button states. */
8476 PROP (TOOL_BAR_ITEM_IMAGES) = value;
8477 else if (EQ (key, Qrtl))
8478 /* ':rtl STRING' */
8479 PROP (TOOL_BAR_ITEM_RTL_IMAGE) = value;
8482 /* If got a filter apply it on binding. */
8483 if (!NILP (filter))
8484 PROP (TOOL_BAR_ITEM_BINDING)
8485 = menu_item_eval_property (list2 (filter,
8486 list2 (Qquote,
8487 PROP (TOOL_BAR_ITEM_BINDING))));
8489 /* See if the binding is a keymap. Give up if it is. */
8490 if (CONSP (get_keymap (PROP (TOOL_BAR_ITEM_BINDING), 0, 1)))
8491 return 0;
8493 /* Enable or disable selection of item. */
8494 if (!EQ (PROP (TOOL_BAR_ITEM_ENABLED_P), Qt))
8495 PROP (TOOL_BAR_ITEM_ENABLED_P)
8496 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_ENABLED_P));
8498 /* Handle radio buttons or toggle boxes. */
8499 if (!NILP (PROP (TOOL_BAR_ITEM_SELECTED_P)))
8500 PROP (TOOL_BAR_ITEM_SELECTED_P)
8501 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_SELECTED_P));
8503 return 1;
8505 #undef PROP
8509 /* Initialize tool_bar_items_vector. REUSE, if non-nil, is a vector
8510 that can be reused. */
8512 static void
8513 init_tool_bar_items (reuse)
8514 Lisp_Object reuse;
8516 if (VECTORP (reuse))
8517 tool_bar_items_vector = reuse;
8518 else
8519 tool_bar_items_vector = Fmake_vector (make_number (64), Qnil);
8520 ntool_bar_items = 0;
8524 /* Append parsed tool bar item properties from
8525 tool_bar_item_properties */
8527 static void
8528 append_tool_bar_item ()
8530 Lisp_Object *to, *from;
8532 /* Enlarge tool_bar_items_vector if necessary. */
8533 if (ntool_bar_items + TOOL_BAR_ITEM_NSLOTS
8534 >= XVECTOR (tool_bar_items_vector)->size)
8535 tool_bar_items_vector
8536 = larger_vector (tool_bar_items_vector,
8537 2 * XVECTOR (tool_bar_items_vector)->size, Qnil);
8539 /* Append entries from tool_bar_item_properties to the end of
8540 tool_bar_items_vector. */
8541 to = XVECTOR (tool_bar_items_vector)->contents + ntool_bar_items;
8542 from = XVECTOR (tool_bar_item_properties)->contents;
8543 bcopy (from, to, TOOL_BAR_ITEM_NSLOTS * sizeof *to);
8544 ntool_bar_items += TOOL_BAR_ITEM_NSLOTS;
8551 /* Read a character using menus based on maps in the array MAPS.
8552 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
8553 Return t if we displayed a menu but the user rejected it.
8555 PREV_EVENT is the previous input event, or nil if we are reading
8556 the first event of a key sequence.
8558 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
8559 if we used a mouse menu to read the input, or zero otherwise. If
8560 USED_MOUSE_MENU is null, we don't dereference it.
8562 The prompting is done based on the prompt-string of the map
8563 and the strings associated with various map elements.
8565 This can be done with X menus or with menus put in the minibuf.
8566 These are done in different ways, depending on how the input will be read.
8567 Menus using X are done after auto-saving in read-char, getting the input
8568 event from Fx_popup_menu; menus using the minibuf use read_char recursively
8569 and do auto-saving in the inner call of read_char. */
8571 static Lisp_Object
8572 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
8573 int nmaps;
8574 Lisp_Object *maps;
8575 Lisp_Object prev_event;
8576 int *used_mouse_menu;
8578 int mapno;
8579 register Lisp_Object name = Qnil;
8581 if (used_mouse_menu)
8582 *used_mouse_menu = 0;
8584 /* Use local over global Menu maps */
8586 if (! menu_prompting)
8587 return Qnil;
8589 /* Optionally disregard all but the global map. */
8590 if (inhibit_local_menu_bar_menus)
8592 maps += (nmaps - 1);
8593 nmaps = 1;
8596 /* Get the menu name from the first map that has one (a prompt string). */
8597 for (mapno = 0; mapno < nmaps; mapno++)
8599 name = Fkeymap_prompt (maps[mapno]);
8600 if (!NILP (name))
8601 break;
8604 /* If we don't have any menus, just read a character normally. */
8605 if (!STRINGP (name))
8606 return Qnil;
8608 #ifdef HAVE_MENUS
8609 /* If we got to this point via a mouse click,
8610 use a real menu for mouse selection. */
8611 if (EVENT_HAS_PARAMETERS (prev_event)
8612 && !EQ (XCAR (prev_event), Qmenu_bar)
8613 && !EQ (XCAR (prev_event), Qtool_bar))
8615 /* Display the menu and get the selection. */
8616 Lisp_Object *realmaps
8617 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
8618 Lisp_Object value;
8619 int nmaps1 = 0;
8621 /* Use the maps that are not nil. */
8622 for (mapno = 0; mapno < nmaps; mapno++)
8623 if (!NILP (maps[mapno]))
8624 realmaps[nmaps1++] = maps[mapno];
8626 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
8627 if (CONSP (value))
8629 Lisp_Object tem;
8631 record_menu_key (XCAR (value));
8633 /* If we got multiple events, unread all but
8634 the first.
8635 There is no way to prevent those unread events
8636 from showing up later in last_nonmenu_event.
8637 So turn symbol and integer events into lists,
8638 to indicate that they came from a mouse menu,
8639 so that when present in last_nonmenu_event
8640 they won't confuse things. */
8641 for (tem = XCDR (value); CONSP (tem); tem = XCDR (tem))
8643 record_menu_key (XCAR (tem));
8644 if (SYMBOLP (XCAR (tem))
8645 || INTEGERP (XCAR (tem)))
8646 XSETCAR (tem, Fcons (XCAR (tem), Qdisabled));
8649 /* If we got more than one event, put all but the first
8650 onto this list to be read later.
8651 Return just the first event now. */
8652 Vunread_command_events
8653 = nconc2 (XCDR (value), Vunread_command_events);
8654 value = XCAR (value);
8656 else if (NILP (value))
8657 value = Qt;
8658 if (used_mouse_menu)
8659 *used_mouse_menu = 1;
8660 return value;
8662 #endif /* HAVE_MENUS */
8663 return Qnil ;
8666 /* Buffer in use so far for the minibuf prompts for menu keymaps.
8667 We make this bigger when necessary, and never free it. */
8668 static char *read_char_minibuf_menu_text;
8669 /* Size of that buffer. */
8670 static int read_char_minibuf_menu_width;
8672 static Lisp_Object
8673 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
8674 int commandflag ;
8675 int nmaps;
8676 Lisp_Object *maps;
8678 int mapno;
8679 register Lisp_Object name;
8680 int nlength;
8681 /* FIXME: Use the minibuffer's frame width. */
8682 int width = FRAME_COLS (SELECTED_FRAME ()) - 4;
8683 int idx = -1;
8684 int nobindings = 1;
8685 Lisp_Object rest, vector;
8686 char *menu;
8688 vector = Qnil;
8689 name = Qnil;
8691 if (! menu_prompting)
8692 return Qnil;
8694 /* Make sure we have a big enough buffer for the menu text. */
8695 if (read_char_minibuf_menu_text == 0)
8697 read_char_minibuf_menu_width = width + 4;
8698 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
8700 else if (width + 4 > read_char_minibuf_menu_width)
8702 read_char_minibuf_menu_width = width + 4;
8703 read_char_minibuf_menu_text
8704 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
8706 menu = read_char_minibuf_menu_text;
8708 /* Get the menu name from the first map that has one (a prompt string). */
8709 for (mapno = 0; mapno < nmaps; mapno++)
8711 name = Fkeymap_prompt (maps[mapno]);
8712 if (!NILP (name))
8713 break;
8716 /* If we don't have any menus, just read a character normally. */
8717 if (!STRINGP (name))
8718 return Qnil;
8720 /* Prompt string always starts with map's prompt, and a space. */
8721 strcpy (menu, SDATA (name));
8722 nlength = SBYTES (name);
8723 menu[nlength++] = ':';
8724 menu[nlength++] = ' ';
8725 menu[nlength] = 0;
8727 /* Start prompting at start of first map. */
8728 mapno = 0;
8729 rest = maps[mapno];
8731 /* Present the documented bindings, a line at a time. */
8732 while (1)
8734 int notfirst = 0;
8735 int i = nlength;
8736 Lisp_Object obj;
8737 int ch;
8738 Lisp_Object orig_defn_macro;
8740 /* Loop over elements of map. */
8741 while (i < width)
8743 Lisp_Object elt;
8745 /* If reached end of map, start at beginning of next map. */
8746 if (NILP (rest))
8748 mapno++;
8749 /* At end of last map, wrap around to first map if just starting,
8750 or end this line if already have something on it. */
8751 if (mapno == nmaps)
8753 mapno = 0;
8754 if (notfirst || nobindings) break;
8756 rest = maps[mapno];
8759 /* Look at the next element of the map. */
8760 if (idx >= 0)
8761 elt = XVECTOR (vector)->contents[idx];
8762 else
8763 elt = Fcar_safe (rest);
8765 if (idx < 0 && VECTORP (elt))
8767 /* If we found a dense table in the keymap,
8768 advanced past it, but start scanning its contents. */
8769 rest = Fcdr_safe (rest);
8770 vector = elt;
8771 idx = 0;
8773 else
8775 /* An ordinary element. */
8776 Lisp_Object event, tem;
8778 if (idx < 0)
8780 event = Fcar_safe (elt); /* alist */
8781 elt = Fcdr_safe (elt);
8783 else
8785 XSETINT (event, idx); /* vector */
8788 /* Ignore the element if it has no prompt string. */
8789 if (INTEGERP (event) && parse_menu_item (elt, 0, -1))
8791 /* 1 if the char to type matches the string. */
8792 int char_matches;
8793 Lisp_Object upcased_event, downcased_event;
8794 Lisp_Object desc = Qnil;
8795 Lisp_Object s
8796 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
8798 upcased_event = Fupcase (event);
8799 downcased_event = Fdowncase (event);
8800 char_matches = (XINT (upcased_event) == SREF (s, 0)
8801 || XINT (downcased_event) == SREF (s, 0));
8802 if (! char_matches)
8803 desc = Fsingle_key_description (event, Qnil);
8805 #if 0 /* It is redundant to list the equivalent key bindings because
8806 the prefix is what the user has already typed. */
8808 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
8809 if (!NILP (tem))
8810 /* Insert equivalent keybinding. */
8811 s = concat2 (s, tem);
8812 #endif
8814 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
8815 if (EQ (tem, QCradio) || EQ (tem, QCtoggle))
8817 /* Insert button prefix. */
8818 Lisp_Object selected
8819 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
8820 if (EQ (tem, QCradio))
8821 tem = build_string (NILP (selected) ? "(*) " : "( ) ");
8822 else
8823 tem = build_string (NILP (selected) ? "[X] " : "[ ] ");
8824 s = concat2 (tem, s);
8828 /* If we have room for the prompt string, add it to this line.
8829 If this is the first on the line, always add it. */
8830 if ((SCHARS (s) + i + 2
8831 + (char_matches ? 0 : SCHARS (desc) + 3))
8832 < width
8833 || !notfirst)
8835 int thiswidth;
8837 /* Punctuate between strings. */
8838 if (notfirst)
8840 strcpy (menu + i, ", ");
8841 i += 2;
8843 notfirst = 1;
8844 nobindings = 0 ;
8846 /* If the char to type doesn't match the string's
8847 first char, explicitly show what char to type. */
8848 if (! char_matches)
8850 /* Add as much of string as fits. */
8851 thiswidth = SCHARS (desc);
8852 if (thiswidth + i > width)
8853 thiswidth = width - i;
8854 bcopy (SDATA (desc), menu + i, thiswidth);
8855 i += thiswidth;
8856 strcpy (menu + i, " = ");
8857 i += 3;
8860 /* Add as much of string as fits. */
8861 thiswidth = SCHARS (s);
8862 if (thiswidth + i > width)
8863 thiswidth = width - i;
8864 bcopy (SDATA (s), menu + i, thiswidth);
8865 i += thiswidth;
8866 menu[i] = 0;
8868 else
8870 /* If this element does not fit, end the line now,
8871 and save the element for the next line. */
8872 strcpy (menu + i, "...");
8873 break;
8877 /* Move past this element. */
8878 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
8879 /* Handle reaching end of dense table. */
8880 idx = -1;
8881 if (idx >= 0)
8882 idx++;
8883 else
8884 rest = Fcdr_safe (rest);
8888 /* Prompt with that and read response. */
8889 message2_nolog (menu, strlen (menu),
8890 ! NILP (current_buffer->enable_multibyte_characters));
8892 /* Make believe its not a keyboard macro in case the help char
8893 is pressed. Help characters are not recorded because menu prompting
8894 is not used on replay.
8896 orig_defn_macro = current_kboard->defining_kbd_macro;
8897 current_kboard->defining_kbd_macro = Qnil;
8899 obj = read_char (commandflag, 0, 0, Qt, 0, NULL);
8900 while (BUFFERP (obj));
8901 current_kboard->defining_kbd_macro = orig_defn_macro;
8903 if (!INTEGERP (obj))
8904 return obj;
8905 else if (XINT (obj) == -2)
8906 return obj;
8907 else
8908 ch = XINT (obj);
8910 if (! EQ (obj, menu_prompt_more_char)
8911 && (!INTEGERP (menu_prompt_more_char)
8912 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
8914 if (!NILP (current_kboard->defining_kbd_macro))
8915 store_kbd_macro_char (obj);
8916 return obj;
8918 /* Help char - go round again */
8922 /* Reading key sequences. */
8924 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
8925 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
8926 keymap, or nil otherwise. Return the index of the first keymap in
8927 which KEY has any binding, or NMAPS if no map has a binding.
8929 If KEY is a meta ASCII character, treat it like meta-prefix-char
8930 followed by the corresponding non-meta character. Keymaps in
8931 CURRENT with non-prefix bindings for meta-prefix-char become nil in
8932 NEXT.
8934 If KEY has no bindings in any of the CURRENT maps, NEXT is left
8935 unmodified.
8937 NEXT may be the same array as CURRENT. */
8939 static int
8940 follow_key (key, nmaps, current, defs, next)
8941 Lisp_Object key;
8942 Lisp_Object *current, *defs, *next;
8943 int nmaps;
8945 int i, first_binding;
8947 first_binding = nmaps;
8948 for (i = nmaps - 1; i >= 0; i--)
8950 if (! NILP (current[i]))
8952 defs[i] = access_keymap (current[i], key, 1, 0, 1);
8953 if (! NILP (defs[i]))
8954 first_binding = i;
8956 else
8957 defs[i] = Qnil;
8960 /* Given the set of bindings we've found, produce the next set of maps. */
8961 if (first_binding < nmaps)
8962 for (i = 0; i < nmaps; i++)
8963 next[i] = NILP (defs[i]) ? Qnil : get_keymap (defs[i], 0, 1);
8965 return first_binding;
8968 /* Structure used to keep track of partial application of key remapping
8969 such as Vfunction_key_map and Vkey_translation_map. */
8970 typedef struct keyremap
8972 /* This is the map originally specified for this use. */
8973 Lisp_Object parent;
8974 /* This is a submap reached by looking up, in PARENT,
8975 the events from START to END. */
8976 Lisp_Object map;
8977 /* Positions [START, END) in the key sequence buffer
8978 are the key that we have scanned so far.
8979 Those events are the ones that we will replace
8980 if PAREHT maps them into a key sequence. */
8981 int start, end;
8982 } keyremap;
8984 /* Lookup KEY in MAP.
8985 MAP is a keymap mapping keys to key vectors or functions.
8986 If the mapping is a function and DO_FUNCTION is non-zero, then
8987 the function is called with PROMPT as parameter and its return
8988 value is used as the return value of this function (after checking
8989 that it is indeed a vector). */
8991 static Lisp_Object
8992 access_keymap_keyremap (map, key, prompt, do_funcall)
8993 Lisp_Object map, key, prompt;
8994 int do_funcall;
8996 Lisp_Object next;
8998 next = access_keymap (map, key, 1, 0, 1);
9000 /* Handle symbol with autoload definition. */
9001 if (SYMBOLP (next) && !NILP (Ffboundp (next))
9002 && CONSP (XSYMBOL (next)->function)
9003 && EQ (XCAR (XSYMBOL (next)->function), Qautoload))
9004 do_autoload (XSYMBOL (next)->function, next);
9006 /* Handle a symbol whose function definition is a keymap
9007 or an array. */
9008 if (SYMBOLP (next) && !NILP (Ffboundp (next))
9009 && (ARRAYP (XSYMBOL (next)->function)
9010 || KEYMAPP (XSYMBOL (next)->function)))
9011 next = XSYMBOL (next)->function;
9013 /* If the keymap gives a function, not an
9014 array, then call the function with one arg and use
9015 its value instead. */
9016 if (SYMBOLP (next) && !NILP (Ffboundp (next)) && do_funcall)
9018 Lisp_Object tem;
9019 tem = next;
9021 next = call1 (next, prompt);
9022 /* If the function returned something invalid,
9023 barf--don't ignore it.
9024 (To ignore it safely, we would need to gcpro a bunch of
9025 other variables.) */
9026 if (! (VECTORP (next) || STRINGP (next)))
9027 error ("Function %s returns invalid key sequence", tem);
9029 return next;
9032 /* Do one step of the key remapping used for function-key-map and
9033 key-translation-map:
9034 KEYBUF is the buffer holding the input events.
9035 BUFSIZE is its maximum size.
9036 FKEY is a pointer to the keyremap structure to use.
9037 INPUT is the index of the last element in KEYBUF.
9038 DOIT if non-zero says that the remapping can actually take place.
9039 DIFF is used to return the number of keys added/removed by the remapping.
9040 PARENT is the root of the keymap.
9041 PROMPT is the prompt to use if the remapping happens through a function.
9042 The return value is non-zero if the remapping actually took place. */
9044 static int
9045 keyremap_step (keybuf, bufsize, fkey, input, doit, diff, prompt)
9046 Lisp_Object *keybuf, prompt;
9047 keyremap *fkey;
9048 int input, doit, *diff, bufsize;
9050 Lisp_Object next, key;
9052 key = keybuf[fkey->end++];
9054 if (KEYMAPP (fkey->parent))
9055 next = access_keymap_keyremap (fkey->map, key, prompt, doit);
9056 else
9057 next = Qnil;
9059 /* If keybuf[fkey->start..fkey->end] is bound in the
9060 map and we're in a position to do the key remapping, replace it with
9061 the binding and restart with fkey->start at the end. */
9062 if ((VECTORP (next) || STRINGP (next)) && doit)
9064 int len = XFASTINT (Flength (next));
9065 int i;
9067 *diff = len - (fkey->end - fkey->start);
9069 if (input + *diff >= bufsize)
9070 error ("Key sequence too long");
9072 /* Shift the keys that follow fkey->end. */
9073 if (*diff < 0)
9074 for (i = fkey->end; i < input; i++)
9075 keybuf[i + *diff] = keybuf[i];
9076 else if (*diff > 0)
9077 for (i = input - 1; i >= fkey->end; i--)
9078 keybuf[i + *diff] = keybuf[i];
9079 /* Overwrite the old keys with the new ones. */
9080 for (i = 0; i < len; i++)
9081 keybuf[fkey->start + i]
9082 = Faref (next, make_number (i));
9084 fkey->start = fkey->end += *diff;
9085 fkey->map = fkey->parent;
9087 return 1;
9090 fkey->map = get_keymap (next, 0, 1);
9092 /* If we no longer have a bound suffix, try a new position for
9093 fkey->start. */
9094 if (!CONSP (fkey->map))
9096 fkey->end = ++fkey->start;
9097 fkey->map = fkey->parent;
9099 return 0;
9102 /* Read a sequence of keys that ends with a non prefix character,
9103 storing it in KEYBUF, a buffer of size BUFSIZE.
9104 Prompt with PROMPT.
9105 Return the length of the key sequence stored.
9106 Return -1 if the user rejected a command menu.
9108 Echo starting immediately unless `prompt' is 0.
9110 Where a key sequence ends depends on the currently active keymaps.
9111 These include any minor mode keymaps active in the current buffer,
9112 the current buffer's local map, and the global map.
9114 If a key sequence has no other bindings, we check Vfunction_key_map
9115 to see if some trailing subsequence might be the beginning of a
9116 function key's sequence. If so, we try to read the whole function
9117 key, and substitute its symbolic name into the key sequence.
9119 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
9120 `double-' events into similar click events, if that would make them
9121 bound. We try to turn `triple-' events first into `double-' events,
9122 then into clicks.
9124 If we get a mouse click in a mode line, vertical divider, or other
9125 non-text area, we treat the click as if it were prefixed by the
9126 symbol denoting that area - `mode-line', `vertical-line', or
9127 whatever.
9129 If the sequence starts with a mouse click, we read the key sequence
9130 with respect to the buffer clicked on, not the current buffer.
9132 If the user switches frames in the midst of a key sequence, we put
9133 off the switch-frame event until later; the next call to
9134 read_char will return it.
9136 If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
9137 from the selected window's buffer. */
9139 static int
9140 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
9141 can_return_switch_frame, fix_current_buffer)
9142 Lisp_Object *keybuf;
9143 int bufsize;
9144 Lisp_Object prompt;
9145 int dont_downcase_last;
9146 int can_return_switch_frame;
9147 int fix_current_buffer;
9149 volatile Lisp_Object from_string;
9150 volatile int count = SPECPDL_INDEX ();
9152 /* How many keys there are in the current key sequence. */
9153 volatile int t;
9155 /* The length of the echo buffer when we started reading, and
9156 the length of this_command_keys when we started reading. */
9157 volatile int echo_start;
9158 volatile int keys_start;
9160 /* The number of keymaps we're scanning right now, and the number of
9161 keymaps we have allocated space for. */
9162 volatile int nmaps;
9163 volatile int nmaps_allocated = 0;
9165 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
9166 the current keymaps. */
9167 Lisp_Object *volatile defs = NULL;
9169 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
9170 in the current keymaps, or nil where it is not a prefix. */
9171 Lisp_Object *volatile submaps = NULL;
9173 /* The local map to start out with at start of key sequence. */
9174 volatile Lisp_Object orig_local_map;
9176 /* The map from the `keymap' property to start out with at start of
9177 key sequence. */
9178 volatile Lisp_Object orig_keymap;
9180 /* 1 if we have already considered switching to the local-map property
9181 of the place where a mouse click occurred. */
9182 volatile int localized_local_map = 0;
9184 /* The index in submaps[] of the first keymap that has a binding for
9185 this key sequence. In other words, the lowest i such that
9186 submaps[i] is non-nil. */
9187 volatile int first_binding;
9188 /* Index of the first key that has no binding.
9189 It is useless to try fkey.start larger than that. */
9190 volatile int first_unbound;
9192 /* If t < mock_input, then KEYBUF[t] should be read as the next
9193 input key.
9195 We use this to recover after recognizing a function key. Once we
9196 realize that a suffix of the current key sequence is actually a
9197 function key's escape sequence, we replace the suffix with the
9198 function key's binding from Vfunction_key_map. Now keybuf
9199 contains a new and different key sequence, so the echo area,
9200 this_command_keys, and the submaps and defs arrays are wrong. In
9201 this situation, we set mock_input to t, set t to 0, and jump to
9202 restart_sequence; the loop will read keys from keybuf up until
9203 mock_input, thus rebuilding the state; and then it will resume
9204 reading characters from the keyboard. */
9205 volatile int mock_input = 0;
9207 /* If the sequence is unbound in submaps[], then
9208 keybuf[fkey.start..fkey.end-1] is a prefix in Vfunction_key_map,
9209 and fkey.map is its binding.
9211 These might be > t, indicating that all function key scanning
9212 should hold off until t reaches them. We do this when we've just
9213 recognized a function key, to avoid searching for the function
9214 key's again in Vfunction_key_map. */
9215 volatile keyremap fkey;
9217 /* Likewise, for key_translation_map and input-decode-map. */
9218 volatile keyremap keytran, indec;
9220 /* Non-zero if we are trying to map a key by changing an upper-case
9221 letter to lower case, or a shifted function key to an unshifted
9222 one. */
9223 volatile int shift_translated = 0;
9225 /* If we receive a `switch-frame' or `select-window' event in the middle of
9226 a key sequence, we put it off for later.
9227 While we're reading, we keep the event here. */
9228 volatile Lisp_Object delayed_switch_frame;
9230 /* See the comment below... */
9231 #if defined (GOBBLE_FIRST_EVENT)
9232 Lisp_Object first_event;
9233 #endif
9235 volatile Lisp_Object original_uppercase;
9236 volatile int original_uppercase_position = -1;
9238 /* Gets around Microsoft compiler limitations. */
9239 int dummyflag = 0;
9241 struct buffer *starting_buffer;
9243 /* List of events for which a fake prefix key has been generated. */
9244 volatile Lisp_Object fake_prefixed_keys = Qnil;
9246 #if defined (GOBBLE_FIRST_EVENT)
9247 int junk;
9248 #endif
9250 struct gcpro gcpro1;
9252 GCPRO1 (fake_prefixed_keys);
9253 raw_keybuf_count = 0;
9255 last_nonmenu_event = Qnil;
9257 delayed_switch_frame = Qnil;
9259 if (INTERACTIVE)
9261 if (!NILP (prompt))
9262 echo_prompt (prompt);
9263 else if (cursor_in_echo_area
9264 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
9265 && NILP (Fzerop (Vecho_keystrokes)))
9266 /* This doesn't put in a dash if the echo buffer is empty, so
9267 you don't always see a dash hanging out in the minibuffer. */
9268 echo_dash ();
9271 /* Record the initial state of the echo area and this_command_keys;
9272 we will need to restore them if we replay a key sequence. */
9273 if (INTERACTIVE)
9274 echo_start = echo_length ();
9275 keys_start = this_command_key_count;
9276 this_single_command_key_start = keys_start;
9278 #if defined (GOBBLE_FIRST_EVENT)
9279 /* This doesn't quite work, because some of the things that read_char
9280 does cannot safely be bypassed. It seems too risky to try to make
9281 this work right. */
9283 /* Read the first char of the sequence specially, before setting
9284 up any keymaps, in case a filter runs and switches buffers on us. */
9285 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
9286 &junk, NULL);
9287 #endif /* GOBBLE_FIRST_EVENT */
9289 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
9290 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
9291 from_string = Qnil;
9293 /* The multi-tty merge moved the code below to right after
9294 `replay_sequence' which caused all these translation maps to be applied
9295 repeatedly, even tho their doc says very clearly they are not applied to
9296 their own output.
9297 The reason for this move was: "We may switch keyboards between rescans,
9298 so we need to reinitialize fkey and keytran before each replay".
9299 This move was wrong (even if we switch keyboards, keybuf still holds the
9300 keys we've read already from the original keyboard and some of those keys
9301 may have already been translated). So there may still be a bug out there
9302 lurking. */
9303 indec.map = indec.parent = current_kboard->Vinput_decode_map;
9304 fkey.map = fkey.parent = current_kboard->Vlocal_function_key_map;
9305 keytran.map = keytran.parent = Vkey_translation_map;
9306 indec.start = indec.end = 0;
9307 fkey.start = fkey.end = 0;
9308 keytran.start = keytran.end = 0;
9310 /* We jump here when the key sequence has been thoroughly changed, and
9311 we need to rescan it starting from the beginning. When we jump here,
9312 keybuf[0..mock_input] holds the sequence we should reread. */
9313 replay_sequence:
9315 starting_buffer = current_buffer;
9316 first_unbound = bufsize + 1;
9318 /* Build our list of keymaps.
9319 If we recognize a function key and replace its escape sequence in
9320 keybuf with its symbol, or if the sequence starts with a mouse
9321 click and we need to switch buffers, we jump back here to rebuild
9322 the initial keymaps from the current buffer. */
9323 nmaps = 0;
9325 if (!NILP (current_kboard->Voverriding_terminal_local_map))
9327 if (2 > nmaps_allocated)
9329 submaps = (Lisp_Object *) alloca (2 * sizeof (submaps[0]));
9330 defs = (Lisp_Object *) alloca (2 * sizeof (defs[0]));
9331 nmaps_allocated = 2;
9333 if (!NILP (current_kboard->Voverriding_terminal_local_map))
9334 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
9336 else if (!NILP (Voverriding_local_map))
9338 if (2 > nmaps_allocated)
9340 submaps = (Lisp_Object *) alloca (2 * sizeof (submaps[0]));
9341 defs = (Lisp_Object *) alloca (2 * sizeof (defs[0]));
9342 nmaps_allocated = 2;
9344 if (!NILP (Voverriding_local_map))
9345 submaps[nmaps++] = Voverriding_local_map;
9347 else
9349 int nminor;
9350 int total;
9351 Lisp_Object *maps;
9353 nminor = current_minor_maps (0, &maps);
9354 total = nminor + (!NILP (orig_keymap) ? 3 : 2);
9356 if (total > nmaps_allocated)
9358 submaps = (Lisp_Object *) alloca (total * sizeof (submaps[0]));
9359 defs = (Lisp_Object *) alloca (total * sizeof (defs[0]));
9360 nmaps_allocated = total;
9363 if (!NILP (orig_keymap))
9364 submaps[nmaps++] = orig_keymap;
9366 bcopy (maps, (void *) (submaps + nmaps),
9367 nminor * sizeof (submaps[0]));
9369 nmaps += nminor;
9371 submaps[nmaps++] = orig_local_map;
9373 submaps[nmaps++] = current_global_map;
9375 /* Find an accurate initial value for first_binding. */
9376 for (first_binding = 0; first_binding < nmaps; first_binding++)
9377 if (! NILP (submaps[first_binding]))
9378 break;
9380 /* Start from the beginning in keybuf. */
9381 t = 0;
9383 /* These are no-ops the first time through, but if we restart, they
9384 revert the echo area and this_command_keys to their original state. */
9385 this_command_key_count = keys_start;
9386 if (INTERACTIVE && t < mock_input)
9387 echo_truncate (echo_start);
9389 /* If the best binding for the current key sequence is a keymap, or
9390 we may be looking at a function key's escape sequence, keep on
9391 reading. */
9392 while (first_binding < nmaps
9393 /* Keep reading as long as there's a prefix binding. */
9394 ? !NILP (submaps[first_binding])
9395 /* Don't return in the middle of a possible function key sequence,
9396 if the only bindings we found were via case conversion.
9397 Thus, if ESC O a has a function-key-map translation
9398 and ESC o has a binding, don't return after ESC O,
9399 so that we can translate ESC O plus the next character. */
9400 : (/* indec.start < t || fkey.start < t || */ keytran.start < t))
9402 Lisp_Object key;
9403 int used_mouse_menu = 0;
9405 /* Where the last real key started. If we need to throw away a
9406 key that has expanded into more than one element of keybuf
9407 (say, a mouse click on the mode line which is being treated
9408 as [mode-line (mouse-...)], then we backtrack to this point
9409 of keybuf. */
9410 volatile int last_real_key_start;
9412 /* These variables are analogous to echo_start and keys_start;
9413 while those allow us to restart the entire key sequence,
9414 echo_local_start and keys_local_start allow us to throw away
9415 just one key. */
9416 volatile int echo_local_start, keys_local_start, local_first_binding;
9418 eassert (indec.end == t || (indec.end > t && indec.end <= mock_input));
9419 eassert (indec.start <= indec.end);
9420 eassert (fkey.start <= fkey.end);
9421 eassert (keytran.start <= keytran.end);
9422 /* key-translation-map is applied *after* function-key-map
9423 which is itself applied *after* input-decode-map. */
9424 eassert (fkey.end <= indec.start);
9425 eassert (keytran.end <= fkey.start);
9427 if (/* first_unbound < indec.start && first_unbound < fkey.start && */
9428 first_unbound < keytran.start)
9429 { /* The prefix upto first_unbound has no binding and has
9430 no translation left to do either, so we know it's unbound.
9431 If we don't stop now, we risk staying here indefinitely
9432 (if the user keeps entering fkey or keytran prefixes
9433 like C-c ESC ESC ESC ESC ...) */
9434 int i;
9435 for (i = first_unbound + 1; i < t; i++)
9436 keybuf[i - first_unbound - 1] = keybuf[i];
9437 mock_input = t - first_unbound - 1;
9438 indec.end = indec.start -= first_unbound + 1;
9439 indec.map = indec.parent;
9440 fkey.end = fkey.start -= first_unbound + 1;
9441 fkey.map = fkey.parent;
9442 keytran.end = keytran.start -= first_unbound + 1;
9443 keytran.map = keytran.parent;
9444 goto replay_sequence;
9447 if (t >= bufsize)
9448 error ("Key sequence too long");
9450 if (INTERACTIVE)
9451 echo_local_start = echo_length ();
9452 keys_local_start = this_command_key_count;
9453 local_first_binding = first_binding;
9455 replay_key:
9456 /* These are no-ops, unless we throw away a keystroke below and
9457 jumped back up to replay_key; in that case, these restore the
9458 variables to their original state, allowing us to replay the
9459 loop. */
9460 if (INTERACTIVE && t < mock_input)
9461 echo_truncate (echo_local_start);
9462 this_command_key_count = keys_local_start;
9463 first_binding = local_first_binding;
9465 /* By default, assume each event is "real". */
9466 last_real_key_start = t;
9468 /* Does mock_input indicate that we are re-reading a key sequence? */
9469 if (t < mock_input)
9471 key = keybuf[t];
9472 add_command_key (key);
9473 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
9474 && NILP (Fzerop (Vecho_keystrokes)))
9475 echo_char (key);
9478 /* If not, we should actually read a character. */
9479 else
9482 #ifdef MULTI_KBOARD
9483 KBOARD *interrupted_kboard = current_kboard;
9484 struct frame *interrupted_frame = SELECTED_FRAME ();
9485 #endif
9486 key = read_char (NILP (prompt), nmaps,
9487 (Lisp_Object *) submaps, last_nonmenu_event,
9488 &used_mouse_menu, NULL);
9489 #ifdef MULTI_KBOARD
9490 if (INTEGERP (key) && XINT (key) == -2) /* wrong_kboard_jmpbuf */
9492 int found = 0;
9493 struct kboard *k;
9495 for (k = all_kboards; k; k = k->next_kboard)
9496 if (k == interrupted_kboard)
9497 found = 1;
9499 if (!found)
9501 /* Don't touch interrupted_kboard when it's been
9502 deleted. */
9503 delayed_switch_frame = Qnil;
9504 goto replay_sequence;
9507 if (!NILP (delayed_switch_frame))
9509 interrupted_kboard->kbd_queue
9510 = Fcons (delayed_switch_frame,
9511 interrupted_kboard->kbd_queue);
9512 delayed_switch_frame = Qnil;
9515 while (t > 0)
9516 interrupted_kboard->kbd_queue
9517 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
9519 /* If the side queue is non-empty, ensure it begins with a
9520 switch-frame, so we'll replay it in the right context. */
9521 if (CONSP (interrupted_kboard->kbd_queue)
9522 && (key = XCAR (interrupted_kboard->kbd_queue),
9523 !(EVENT_HAS_PARAMETERS (key)
9524 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
9525 Qswitch_frame))))
9527 Lisp_Object frame;
9528 XSETFRAME (frame, interrupted_frame);
9529 interrupted_kboard->kbd_queue
9530 = Fcons (make_lispy_switch_frame (frame),
9531 interrupted_kboard->kbd_queue);
9533 mock_input = 0;
9534 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
9535 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
9536 goto replay_sequence;
9538 #endif
9541 /* read_char returns t when it shows a menu and the user rejects it.
9542 Just return -1. */
9543 if (EQ (key, Qt))
9545 unbind_to (count, Qnil);
9546 UNGCPRO;
9547 return -1;
9550 /* read_char returns -1 at the end of a macro.
9551 Emacs 18 handles this by returning immediately with a
9552 zero, so that's what we'll do. */
9553 if (INTEGERP (key) && XINT (key) == -1)
9555 t = 0;
9556 /* The Microsoft C compiler can't handle the goto that
9557 would go here. */
9558 dummyflag = 1;
9559 break;
9562 /* If the current buffer has been changed from under us, the
9563 keymap may have changed, so replay the sequence. */
9564 if (BUFFERP (key))
9566 timer_resume_idle ();
9568 mock_input = t;
9569 /* Reset the current buffer from the selected window
9570 in case something changed the former and not the latter.
9571 This is to be more consistent with the behavior
9572 of the command_loop_1. */
9573 if (fix_current_buffer)
9575 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9576 Fkill_emacs (Qnil);
9577 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
9578 Fset_buffer (XWINDOW (selected_window)->buffer);
9581 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
9582 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
9583 goto replay_sequence;
9586 /* If we have a quit that was typed in another frame, and
9587 quit_throw_to_read_char switched buffers,
9588 replay to get the right keymap. */
9589 if (INTEGERP (key)
9590 && XINT (key) == quit_char
9591 && current_buffer != starting_buffer)
9593 GROW_RAW_KEYBUF;
9594 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
9595 keybuf[t++] = key;
9596 mock_input = t;
9597 Vquit_flag = Qnil;
9598 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
9599 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
9600 goto replay_sequence;
9603 Vquit_flag = Qnil;
9605 if (EVENT_HAS_PARAMETERS (key)
9606 /* Either a `switch-frame' or a `select-window' event. */
9607 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), Qswitch_frame))
9609 /* If we're at the beginning of a key sequence, and the caller
9610 says it's okay, go ahead and return this event. If we're
9611 in the midst of a key sequence, delay it until the end. */
9612 if (t > 0 || !can_return_switch_frame)
9614 delayed_switch_frame = key;
9615 goto replay_key;
9619 GROW_RAW_KEYBUF;
9620 ASET (raw_keybuf, raw_keybuf_count, key);
9621 raw_keybuf_count++;
9624 /* Clicks in non-text areas get prefixed by the symbol
9625 in their CHAR-ADDRESS field. For example, a click on
9626 the mode line is prefixed by the symbol `mode-line'.
9628 Furthermore, key sequences beginning with mouse clicks
9629 are read using the keymaps of the buffer clicked on, not
9630 the current buffer. So we may have to switch the buffer
9631 here.
9633 When we turn one event into two events, we must make sure
9634 that neither of the two looks like the original--so that,
9635 if we replay the events, they won't be expanded again.
9636 If not for this, such reexpansion could happen either here
9637 or when user programs play with this-command-keys. */
9638 if (EVENT_HAS_PARAMETERS (key))
9640 Lisp_Object kind;
9641 Lisp_Object string;
9643 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
9644 if (EQ (kind, Qmouse_click))
9646 Lisp_Object window, posn;
9648 window = POSN_WINDOW (EVENT_START (key));
9649 posn = POSN_POSN (EVENT_START (key));
9651 if (CONSP (posn)
9652 || (!NILP (fake_prefixed_keys)
9653 && !NILP (Fmemq (key, fake_prefixed_keys))))
9655 /* We're looking a second time at an event for which
9656 we generated a fake prefix key. Set
9657 last_real_key_start appropriately. */
9658 if (t > 0)
9659 last_real_key_start = t - 1;
9662 /* Key sequences beginning with mouse clicks are
9663 read using the keymaps in the buffer clicked on,
9664 not the current buffer. If we're at the
9665 beginning of a key sequence, switch buffers. */
9666 if (last_real_key_start == 0
9667 && WINDOWP (window)
9668 && BUFFERP (XWINDOW (window)->buffer)
9669 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
9671 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
9672 keybuf[t] = key;
9673 mock_input = t + 1;
9675 /* Arrange to go back to the original buffer once we're
9676 done reading the key sequence. Note that we can't
9677 use save_excursion_{save,restore} here, because they
9678 save point as well as the current buffer; we don't
9679 want to save point, because redisplay may change it,
9680 to accommodate a Fset_window_start or something. We
9681 don't want to do this at the top of the function,
9682 because we may get input from a subprocess which
9683 wants to change the selected window and stuff (say,
9684 emacsclient). */
9685 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
9687 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9688 Fkill_emacs (Qnil);
9689 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
9690 orig_local_map = get_local_map (PT, current_buffer,
9691 Qlocal_map);
9692 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
9693 goto replay_sequence;
9696 /* For a mouse click, get the local text-property keymap
9697 of the place clicked on, rather than point. */
9698 if (last_real_key_start == 0
9699 && CONSP (XCDR (key))
9700 && ! localized_local_map)
9702 Lisp_Object map_here, start, pos;
9704 localized_local_map = 1;
9705 start = EVENT_START (key);
9707 if (CONSP (start) && POSN_INBUFFER_P (start))
9709 pos = POSN_BUFFER_POSN (start);
9710 if (INTEGERP (pos)
9711 && XINT (pos) >= BEGV
9712 && XINT (pos) <= ZV)
9714 map_here = get_local_map (XINT (pos),
9715 current_buffer, Qlocal_map);
9716 if (!EQ (map_here, orig_local_map))
9718 orig_local_map = map_here;
9719 ++localized_local_map;
9722 map_here = get_local_map (XINT (pos),
9723 current_buffer, Qkeymap);
9724 if (!EQ (map_here, orig_keymap))
9726 orig_keymap = map_here;
9727 ++localized_local_map;
9730 if (localized_local_map > 1)
9732 keybuf[t] = key;
9733 mock_input = t + 1;
9735 goto replay_sequence;
9741 /* Expand mode-line and scroll-bar events into two events:
9742 use posn as a fake prefix key. */
9743 if (SYMBOLP (posn)
9744 && (NILP (fake_prefixed_keys)
9745 || NILP (Fmemq (key, fake_prefixed_keys))))
9747 if (t + 1 >= bufsize)
9748 error ("Key sequence too long");
9750 keybuf[t] = posn;
9751 keybuf[t + 1] = key;
9752 mock_input = t + 2;
9754 /* Record that a fake prefix key has been generated
9755 for KEY. Don't modify the event; this would
9756 prevent proper action when the event is pushed
9757 back into unread-command-events. */
9758 fake_prefixed_keys = Fcons (key, fake_prefixed_keys);
9760 /* If on a mode line string with a local keymap,
9761 reconsider the key sequence with that keymap. */
9762 if (string = POSN_STRING (EVENT_START (key)),
9763 (CONSP (string) && STRINGP (XCAR (string))))
9765 Lisp_Object pos, map, map2;
9767 pos = XCDR (string);
9768 string = XCAR (string);
9769 if (XINT (pos) >= 0
9770 && XINT (pos) < SCHARS (string))
9772 map = Fget_text_property (pos, Qlocal_map, string);
9773 if (!NILP (map))
9774 orig_local_map = map;
9775 map2 = Fget_text_property (pos, Qkeymap, string);
9776 if (!NILP (map2))
9777 orig_keymap = map2;
9778 if (!NILP (map) || !NILP (map2))
9779 goto replay_sequence;
9783 goto replay_key;
9785 else if (NILP (from_string)
9786 && (string = POSN_STRING (EVENT_START (key)),
9787 (CONSP (string) && STRINGP (XCAR (string)))))
9789 /* For a click on a string, i.e. overlay string or a
9790 string displayed via the `display' property,
9791 consider `local-map' and `keymap' properties of
9792 that string. */
9793 Lisp_Object pos, map, map2;
9795 pos = XCDR (string);
9796 string = XCAR (string);
9797 if (XINT (pos) >= 0
9798 && XINT (pos) < SCHARS (string))
9800 map = Fget_text_property (pos, Qlocal_map, string);
9801 if (!NILP (map))
9802 orig_local_map = map;
9803 map2 = Fget_text_property (pos, Qkeymap, string);
9804 if (!NILP (map2))
9805 orig_keymap = map2;
9807 if (!NILP (map) || !NILP (map2))
9809 from_string = string;
9810 keybuf[t++] = key;
9811 mock_input = t;
9812 goto replay_sequence;
9817 else if (CONSP (XCDR (key))
9818 && CONSP (EVENT_START (key))
9819 && CONSP (XCDR (EVENT_START (key))))
9821 Lisp_Object posn;
9823 posn = POSN_POSN (EVENT_START (key));
9824 /* Handle menu-bar events:
9825 insert the dummy prefix event `menu-bar'. */
9826 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
9828 if (t + 1 >= bufsize)
9829 error ("Key sequence too long");
9830 keybuf[t] = posn;
9831 keybuf[t+1] = key;
9833 /* Zap the position in key, so we know that we've
9834 expanded it, and don't try to do so again. */
9835 POSN_SET_POSN (EVENT_START (key),
9836 Fcons (posn, Qnil));
9838 mock_input = t + 2;
9839 goto replay_sequence;
9841 else if (CONSP (posn))
9843 /* We're looking at the second event of a
9844 sequence which we expanded before. Set
9845 last_real_key_start appropriately. */
9846 if (last_real_key_start == t && t > 0)
9847 last_real_key_start = t - 1;
9852 /* We have finally decided that KEY is something we might want
9853 to look up. */
9854 first_binding = (follow_key (key,
9855 nmaps - first_binding,
9856 submaps + first_binding,
9857 defs + first_binding,
9858 submaps + first_binding)
9859 + first_binding);
9861 /* If KEY wasn't bound, we'll try some fallbacks. */
9862 if (first_binding < nmaps)
9863 /* This is needed for the following scenario:
9864 event 0: a down-event that gets dropped by calling replay_key.
9865 event 1: some normal prefix like C-h.
9866 After event 0, first_unbound is 0, after event 1 indec.start,
9867 fkey.start, and keytran.start are all 1, so when we see that
9868 C-h is bound, we need to update first_unbound. */
9869 first_unbound = max (t + 1, first_unbound);
9870 else
9872 Lisp_Object head;
9874 /* Remember the position to put an upper bound on indec.start. */
9875 first_unbound = min (t, first_unbound);
9877 head = EVENT_HEAD (key);
9878 if (help_char_p (head) && t > 0)
9880 read_key_sequence_cmd = Vprefix_help_command;
9881 keybuf[t++] = key;
9882 last_nonmenu_event = key;
9883 /* The Microsoft C compiler can't handle the goto that
9884 would go here. */
9885 dummyflag = 1;
9886 break;
9889 if (SYMBOLP (head))
9891 Lisp_Object breakdown;
9892 int modifiers;
9894 breakdown = parse_modifiers (head);
9895 modifiers = XINT (XCAR (XCDR (breakdown)));
9896 /* Attempt to reduce an unbound mouse event to a simpler
9897 event that is bound:
9898 Drags reduce to clicks.
9899 Double-clicks reduce to clicks.
9900 Triple-clicks reduce to double-clicks, then to clicks.
9901 Down-clicks are eliminated.
9902 Double-downs reduce to downs, then are eliminated.
9903 Triple-downs reduce to double-downs, then to downs,
9904 then are eliminated. */
9905 if (modifiers & (down_modifier | drag_modifier
9906 | double_modifier | triple_modifier))
9908 while (modifiers & (down_modifier | drag_modifier
9909 | double_modifier | triple_modifier))
9911 Lisp_Object new_head, new_click;
9912 if (modifiers & triple_modifier)
9913 modifiers ^= (double_modifier | triple_modifier);
9914 else if (modifiers & double_modifier)
9915 modifiers &= ~double_modifier;
9916 else if (modifiers & drag_modifier)
9917 modifiers &= ~drag_modifier;
9918 else
9920 /* Dispose of this `down' event by simply jumping
9921 back to replay_key, to get another event.
9923 Note that if this event came from mock input,
9924 then just jumping back to replay_key will just
9925 hand it to us again. So we have to wipe out any
9926 mock input.
9928 We could delete keybuf[t] and shift everything
9929 after that to the left by one spot, but we'd also
9930 have to fix up any variable that points into
9931 keybuf, and shifting isn't really necessary
9932 anyway.
9934 Adding prefixes for non-textual mouse clicks
9935 creates two characters of mock input, and both
9936 must be thrown away. If we're only looking at
9937 the prefix now, we can just jump back to
9938 replay_key. On the other hand, if we've already
9939 processed the prefix, and now the actual click
9940 itself is giving us trouble, then we've lost the
9941 state of the keymaps we want to backtrack to, and
9942 we need to replay the whole sequence to rebuild
9945 Beyond that, only function key expansion could
9946 create more than two keys, but that should never
9947 generate mouse events, so it's okay to zero
9948 mock_input in that case too.
9950 FIXME: The above paragraph seems just plain
9951 wrong, if you consider things like
9952 xterm-mouse-mode. -stef
9954 Isn't this just the most wonderful code ever? */
9956 /* If mock_input > t + 1, the above simplification
9957 will actually end up dropping keys on the floor.
9958 This is probably OK for now, but even
9959 if mock_input <= t + 1, we need to adjust indec,
9960 fkey, and keytran.
9961 Typical case [header-line down-mouse-N]:
9962 mock_input = 2, t = 1, fkey.end = 1,
9963 last_real_key_start = 0. */
9964 if (indec.end > last_real_key_start)
9966 indec.end = indec.start
9967 = min (last_real_key_start, indec.start);
9968 indec.map = indec.parent;
9969 if (fkey.end > last_real_key_start)
9971 fkey.end = fkey.start
9972 = min (last_real_key_start, fkey.start);
9973 fkey.map = fkey.parent;
9974 if (keytran.end > last_real_key_start)
9976 keytran.end = keytran.start
9977 = min (last_real_key_start, keytran.start);
9978 keytran.map = keytran.parent;
9982 if (t == last_real_key_start)
9984 mock_input = 0;
9985 goto replay_key;
9987 else
9989 mock_input = last_real_key_start;
9990 goto replay_sequence;
9994 new_head
9995 = apply_modifiers (modifiers, XCAR (breakdown));
9996 new_click
9997 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
9999 /* Look for a binding for this new key. follow_key
10000 promises that it didn't munge submaps the
10001 last time we called it, since key was unbound. */
10002 first_binding
10003 = (follow_key (new_click,
10004 nmaps - local_first_binding,
10005 submaps + local_first_binding,
10006 defs + local_first_binding,
10007 submaps + local_first_binding)
10008 + local_first_binding);
10010 /* If that click is bound, go for it. */
10011 if (first_binding < nmaps)
10013 key = new_click;
10014 break;
10016 /* Otherwise, we'll leave key set to the drag event. */
10022 keybuf[t++] = key;
10023 /* Normally, last_nonmenu_event gets the previous key we read.
10024 But when a mouse popup menu is being used,
10025 we don't update last_nonmenu_event; it continues to hold the mouse
10026 event that preceded the first level of menu. */
10027 if (!used_mouse_menu)
10028 last_nonmenu_event = key;
10030 /* Record what part of this_command_keys is the current key sequence. */
10031 this_single_command_key_start = this_command_key_count - t;
10033 /* Look for this sequence in input-decode-map.
10034 Scan from indec.end until we find a bound suffix. */
10035 while (indec.end < t)
10037 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
10038 int done, diff;
10040 GCPRO4 (indec.map, fkey.map, keytran.map, delayed_switch_frame);
10041 done = keyremap_step (keybuf, bufsize, &indec, max (t, mock_input),
10042 1, &diff, prompt);
10043 UNGCPRO;
10044 if (done)
10046 mock_input = diff + max (t, mock_input);
10047 goto replay_sequence;
10051 if (first_binding < nmaps && NILP (submaps[first_binding])
10052 && indec.start >= t)
10053 /* There is a binding and it's not a prefix.
10054 (and it doesn't have any input-decode-map translation pending).
10055 There is thus no function-key in this sequence.
10056 Moving fkey.start is important in this case to allow keytran.start
10057 to go over the sequence before we return (since we keep the
10058 invariant that keytran.end <= fkey.start). */
10060 if (fkey.start < t)
10061 (fkey.start = fkey.end = t, fkey.map = fkey.parent);
10063 else
10064 /* If the sequence is unbound, see if we can hang a function key
10065 off the end of it. */
10066 /* Continue scan from fkey.end until we find a bound suffix. */
10067 while (fkey.end < indec.start)
10069 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
10070 int done, diff;
10072 GCPRO4 (indec.map, fkey.map, keytran.map, delayed_switch_frame);
10073 done = keyremap_step (keybuf, bufsize, &fkey,
10074 max (t, mock_input),
10075 /* If there's a binding (i.e.
10076 first_binding >= nmaps) we don't want
10077 to apply this function-key-mapping. */
10078 fkey.end + 1 == t && first_binding >= nmaps,
10079 &diff, prompt);
10080 UNGCPRO;
10081 if (done)
10083 mock_input = diff + max (t, mock_input);
10084 /* Adjust the input-decode-map counters. */
10085 indec.end += diff;
10086 indec.start += diff;
10088 goto replay_sequence;
10092 /* Look for this sequence in key-translation-map.
10093 Scan from keytran.end until we find a bound suffix. */
10094 while (keytran.end < fkey.start)
10096 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
10097 int done, diff;
10099 GCPRO4 (indec.map, fkey.map, keytran.map, delayed_switch_frame);
10100 done = keyremap_step (keybuf, bufsize, &keytran, max (t, mock_input),
10101 1, &diff, prompt);
10102 UNGCPRO;
10103 if (done)
10105 mock_input = diff + max (t, mock_input);
10106 /* Adjust the function-key-map and input-decode-map counters. */
10107 indec.end += diff;
10108 indec.start += diff;
10109 fkey.end += diff;
10110 fkey.start += diff;
10112 goto replay_sequence;
10116 /* If KEY is not defined in any of the keymaps,
10117 and cannot be part of a function key or translation,
10118 and is an upper case letter
10119 use the corresponding lower-case letter instead. */
10120 if (first_binding >= nmaps
10121 && /* indec.start >= t && fkey.start >= t && */ keytran.start >= t
10122 && INTEGERP (key)
10123 && ((CHARACTERP (make_number (XINT (key) & ~CHAR_MODIFIER_MASK))
10124 && UPPERCASEP (XINT (key) & ~CHAR_MODIFIER_MASK))
10125 || (XINT (key) & shift_modifier)))
10127 Lisp_Object new_key;
10129 original_uppercase = key;
10130 original_uppercase_position = t - 1;
10132 if (XINT (key) & shift_modifier)
10133 XSETINT (new_key, XINT (key) & ~shift_modifier);
10134 else
10135 XSETINT (new_key, (DOWNCASE (XINT (key) & ~CHAR_MODIFIER_MASK)
10136 | (XINT (key) & CHAR_MODIFIER_MASK)));
10138 /* We have to do this unconditionally, regardless of whether
10139 the lower-case char is defined in the keymaps, because they
10140 might get translated through function-key-map. */
10141 keybuf[t - 1] = new_key;
10142 mock_input = max (t, mock_input);
10143 shift_translated = 1;
10145 goto replay_sequence;
10147 /* If KEY is not defined in any of the keymaps,
10148 and cannot be part of a function key or translation,
10149 and is a shifted function key,
10150 use the corresponding unshifted function key instead. */
10151 if (first_binding >= nmaps
10152 && /* indec.start >= t && fkey.start >= t && */ keytran.start >= t)
10154 Lisp_Object breakdown = parse_modifiers (key);
10155 int modifiers
10156 = CONSP (breakdown) ? (XINT (XCAR (XCDR (breakdown)))) : 0;
10158 if (modifiers & shift_modifier
10159 /* Treat uppercase keys as shifted. */
10160 || (INTEGERP (key)
10161 && (KEY_TO_CHAR (key)
10162 < XCHAR_TABLE (current_buffer->downcase_table)->size)
10163 && UPPERCASEP (KEY_TO_CHAR (key))))
10165 Lisp_Object new_key
10166 = (modifiers & shift_modifier
10167 ? apply_modifiers (modifiers & ~shift_modifier,
10168 XCAR (breakdown))
10169 : make_number (DOWNCASE (KEY_TO_CHAR (key)) | modifiers));
10171 original_uppercase = key;
10172 original_uppercase_position = t - 1;
10174 /* We have to do this unconditionally, regardless of whether
10175 the lower-case char is defined in the keymaps, because they
10176 might get translated through function-key-map. */
10177 keybuf[t - 1] = new_key;
10178 mock_input = max (t, mock_input);
10179 /* Reset fkey (and consequently keytran) to apply
10180 function-key-map on the result, so that S-backspace is
10181 correctly mapped to DEL (via backspace). OTOH,
10182 input-decode-map doesn't need to go through it again. */
10183 fkey.start = fkey.end = 0;
10184 keytran.start = keytran.end = 0;
10185 shift_translated = 1;
10187 goto replay_sequence;
10191 if (!dummyflag)
10192 read_key_sequence_cmd = (first_binding < nmaps
10193 ? defs[first_binding]
10194 : Qnil);
10196 unread_switch_frame = delayed_switch_frame;
10197 unbind_to (count, Qnil);
10199 /* Don't downcase the last character if the caller says don't.
10200 Don't downcase it if the result is undefined, either. */
10201 if ((dont_downcase_last || first_binding >= nmaps)
10202 && t > 0
10203 && t - 1 == original_uppercase_position)
10205 keybuf[t - 1] = original_uppercase;
10206 shift_translated = 0;
10209 if (shift_translated)
10210 Vthis_command_keys_shift_translated = Qt;
10212 /* Occasionally we fabricate events, perhaps by expanding something
10213 according to function-key-map, or by adding a prefix symbol to a
10214 mouse click in the scroll bar or modeline. In this cases, return
10215 the entire generated key sequence, even if we hit an unbound
10216 prefix or a definition before the end. This means that you will
10217 be able to push back the event properly, and also means that
10218 read-key-sequence will always return a logical unit.
10220 Better ideas? */
10221 for (; t < mock_input; t++)
10223 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
10224 && NILP (Fzerop (Vecho_keystrokes)))
10225 echo_char (keybuf[t]);
10226 add_command_key (keybuf[t]);
10229 UNGCPRO;
10230 return t;
10233 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 5, 0,
10234 doc: /* Read a sequence of keystrokes and return as a string or vector.
10235 The sequence is sufficient to specify a non-prefix command in the
10236 current local and global maps.
10238 First arg PROMPT is a prompt string. If nil, do not prompt specially.
10239 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos
10240 as a continuation of the previous key.
10242 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not
10243 convert the last event to lower case. (Normally any upper case event
10244 is converted to lower case if the original event is undefined and the lower
10245 case equivalent is defined.) A non-nil value is appropriate for reading
10246 a key sequence to be defined.
10248 A C-g typed while in this function is treated like any other character,
10249 and `quit-flag' is not set.
10251 If the key sequence starts with a mouse click, then the sequence is read
10252 using the keymaps of the buffer of the window clicked in, not the buffer
10253 of the selected window as normal.
10255 `read-key-sequence' drops unbound button-down events, since you normally
10256 only care about the click or drag events which follow them. If a drag
10257 or multi-click event is unbound, but the corresponding click event would
10258 be bound, `read-key-sequence' turns the event into a click event at the
10259 drag's starting position. This means that you don't have to distinguish
10260 between click and drag, double, or triple events unless you want to.
10262 `read-key-sequence' prefixes mouse events on mode lines, the vertical
10263 lines separating windows, and scroll bars with imaginary keys
10264 `mode-line', `vertical-line', and `vertical-scroll-bar'.
10266 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this
10267 function will process a switch-frame event if the user switches frames
10268 before typing anything. If the user switches frames in the middle of a
10269 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME
10270 is nil, then the event will be put off until after the current key sequence.
10272 `read-key-sequence' checks `function-key-map' for function key
10273 sequences, where they wouldn't conflict with ordinary bindings. See
10274 `function-key-map' for more details.
10276 The optional fifth argument COMMAND-LOOP, if non-nil, means
10277 that this key sequence is being read by something that will
10278 read commands one after another. It should be nil if the caller
10279 will read just one key sequence. */)
10280 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
10281 command_loop)
10282 Lisp_Object prompt, continue_echo, dont_downcase_last;
10283 Lisp_Object can_return_switch_frame, command_loop;
10285 Lisp_Object keybuf[30];
10286 register int i;
10287 struct gcpro gcpro1;
10288 int count = SPECPDL_INDEX ();
10290 if (!NILP (prompt))
10291 CHECK_STRING (prompt);
10292 QUIT;
10294 specbind (Qinput_method_exit_on_first_char,
10295 (NILP (command_loop) ? Qt : Qnil));
10296 specbind (Qinput_method_use_echo_area,
10297 (NILP (command_loop) ? Qt : Qnil));
10299 bzero (keybuf, sizeof keybuf);
10300 GCPRO1 (keybuf[0]);
10301 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
10303 if (NILP (continue_echo))
10305 this_command_key_count = 0;
10306 this_command_key_count_reset = 0;
10307 this_single_command_key_start = 0;
10310 #ifdef HAVE_WINDOW_SYSTEM
10311 if (display_hourglass_p)
10312 cancel_hourglass ();
10313 #endif
10315 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
10316 prompt, ! NILP (dont_downcase_last),
10317 ! NILP (can_return_switch_frame), 0);
10319 #if 0 /* The following is fine for code reading a key sequence and
10320 then proceeding with a lenghty computation, but it's not good
10321 for code reading keys in a loop, like an input method. */
10322 #ifdef HAVE_WINDOW_SYSTEM
10323 if (display_hourglass_p)
10324 start_hourglass ();
10325 #endif
10326 #endif
10328 if (i == -1)
10330 Vquit_flag = Qt;
10331 QUIT;
10333 UNGCPRO;
10334 return unbind_to (count, make_event_array (i, keybuf));
10337 DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
10338 Sread_key_sequence_vector, 1, 5, 0,
10339 doc: /* Like `read-key-sequence' but always return a vector. */)
10340 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
10341 command_loop)
10342 Lisp_Object prompt, continue_echo, dont_downcase_last;
10343 Lisp_Object can_return_switch_frame, command_loop;
10345 Lisp_Object keybuf[30];
10346 register int i;
10347 struct gcpro gcpro1;
10348 int count = SPECPDL_INDEX ();
10350 if (!NILP (prompt))
10351 CHECK_STRING (prompt);
10352 QUIT;
10354 specbind (Qinput_method_exit_on_first_char,
10355 (NILP (command_loop) ? Qt : Qnil));
10356 specbind (Qinput_method_use_echo_area,
10357 (NILP (command_loop) ? Qt : Qnil));
10359 bzero (keybuf, sizeof keybuf);
10360 GCPRO1 (keybuf[0]);
10361 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
10363 if (NILP (continue_echo))
10365 this_command_key_count = 0;
10366 this_command_key_count_reset = 0;
10367 this_single_command_key_start = 0;
10370 #ifdef HAVE_WINDOW_SYSTEM
10371 if (display_hourglass_p)
10372 cancel_hourglass ();
10373 #endif
10375 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
10376 prompt, ! NILP (dont_downcase_last),
10377 ! NILP (can_return_switch_frame), 0);
10379 #ifdef HAVE_WINDOW_SYSTEM
10380 if (display_hourglass_p)
10381 start_hourglass ();
10382 #endif
10384 if (i == -1)
10386 Vquit_flag = Qt;
10387 QUIT;
10389 UNGCPRO;
10390 return unbind_to (count, Fvector (i, keybuf));
10393 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
10394 doc: /* Execute CMD as an editor command.
10395 CMD must be a symbol that satisfies the `commandp' predicate.
10396 Optional second arg RECORD-FLAG non-nil
10397 means unconditionally put this command in `command-history'.
10398 Otherwise, that is done only if an arg is read using the minibuffer.
10399 The argument KEYS specifies the value to use instead of (this-command-keys)
10400 when reading the arguments; if it is nil, (this-command-keys) is used.
10401 The argument SPECIAL, if non-nil, means that this command is executing
10402 a special event, so ignore the prefix argument and don't clear it. */)
10403 (cmd, record_flag, keys, special)
10404 Lisp_Object cmd, record_flag, keys, special;
10406 register Lisp_Object final;
10407 register Lisp_Object tem;
10408 Lisp_Object prefixarg;
10409 extern int debug_on_next_call;
10411 debug_on_next_call = 0;
10413 if (NILP (special))
10415 prefixarg = current_kboard->Vprefix_arg;
10416 Vcurrent_prefix_arg = prefixarg;
10417 current_kboard->Vprefix_arg = Qnil;
10419 else
10420 prefixarg = Qnil;
10422 if (SYMBOLP (cmd))
10424 tem = Fget (cmd, Qdisabled);
10425 if (!NILP (tem) && !NILP (Vrun_hooks))
10427 tem = Fsymbol_value (Qdisabled_command_function);
10428 if (!NILP (tem))
10429 return call1 (Vrun_hooks, Qdisabled_command_function);
10433 while (1)
10435 final = Findirect_function (cmd, Qnil);
10437 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
10439 struct gcpro gcpro1, gcpro2;
10441 GCPRO2 (cmd, prefixarg);
10442 do_autoload (final, cmd);
10443 UNGCPRO;
10445 else
10446 break;
10449 if (STRINGP (final) || VECTORP (final))
10451 /* If requested, place the macro in the command history. For
10452 other sorts of commands, call-interactively takes care of
10453 this. */
10454 if (!NILP (record_flag))
10456 Vcommand_history
10457 = Fcons (Fcons (Qexecute_kbd_macro,
10458 Fcons (final, Fcons (prefixarg, Qnil))),
10459 Vcommand_history);
10461 /* Don't keep command history around forever. */
10462 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
10464 tem = Fnthcdr (Vhistory_length, Vcommand_history);
10465 if (CONSP (tem))
10466 XSETCDR (tem, Qnil);
10470 return Fexecute_kbd_macro (final, prefixarg, Qnil);
10473 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
10474 /* Don't call Fcall_interactively directly because we want to make
10475 sure the backtrace has an entry for `call-interactively'.
10476 For the same reason, pass `cmd' rather than `final'. */
10477 return call3 (Qcall_interactively, cmd, record_flag, keys);
10479 return Qnil;
10484 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
10485 1, 1, "P",
10486 doc: /* Read function name, then read its arguments and call it.
10488 To pass a numeric argument to the command you are invoking with, specify
10489 the numeric argument to this command.
10491 Noninteractively, the argument PREFIXARG is the prefix argument to
10492 give to the command you invoke, if it asks for an argument. */)
10493 (prefixarg)
10494 Lisp_Object prefixarg;
10496 Lisp_Object function;
10497 char buf[40];
10498 int saved_last_point_position;
10499 Lisp_Object saved_keys, saved_last_point_position_buffer;
10500 Lisp_Object bindings, value;
10501 struct gcpro gcpro1, gcpro2, gcpro3;
10502 #ifdef HAVE_WINDOW_SYSTEM
10503 /* The call to Fcompleting_read wil start and cancel the hourglass,
10504 but if the hourglass was already scheduled, this means that no
10505 hourglass will be shown for the actual M-x command itself.
10506 So we restart it if it is already scheduled. Note that checking
10507 hourglass_shown_p is not enough, normally the hourglass is not shown,
10508 just scheduled to be shown. */
10509 int hstarted = hourglass_started ();
10510 #endif
10512 saved_keys = Fvector (this_command_key_count,
10513 XVECTOR (this_command_keys)->contents);
10514 saved_last_point_position_buffer = last_point_position_buffer;
10515 saved_last_point_position = last_point_position;
10516 buf[0] = 0;
10517 GCPRO3 (saved_keys, prefixarg, saved_last_point_position_buffer);
10519 if (EQ (prefixarg, Qminus))
10520 strcpy (buf, "- ");
10521 else if (CONSP (prefixarg) && XINT (XCAR (prefixarg)) == 4)
10522 strcpy (buf, "C-u ");
10523 else if (CONSP (prefixarg) && INTEGERP (XCAR (prefixarg)))
10524 sprintf (buf, "%ld ", (long) XINT (XCAR (prefixarg)));
10525 else if (INTEGERP (prefixarg))
10526 sprintf (buf, "%ld ", (long) XINT (prefixarg));
10528 /* This isn't strictly correct if execute-extended-command
10529 is bound to anything else. Perhaps it should use
10530 this_command_keys? */
10531 strcat (buf, "M-x ");
10533 /* Prompt with buf, and then read a string, completing from and
10534 restricting to the set of all defined commands. Don't provide
10535 any initial input. Save the command read on the extended-command
10536 history list. */
10537 function = Fcompleting_read (build_string (buf),
10538 Vobarray, Qcommandp,
10539 Qt, Qnil, Qextended_command_history, Qnil,
10540 Qnil);
10542 #ifdef HAVE_WINDOW_SYSTEM
10543 if (hstarted) start_hourglass ();
10544 #endif
10546 if (STRINGP (function) && SCHARS (function) == 0)
10547 error ("No command name given");
10549 /* Set this_command_keys to the concatenation of saved_keys and
10550 function, followed by a RET. */
10552 Lisp_Object *keys;
10553 int i;
10555 this_command_key_count = 0;
10556 this_command_key_count_reset = 0;
10557 this_single_command_key_start = 0;
10559 keys = XVECTOR (saved_keys)->contents;
10560 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
10561 add_command_key (keys[i]);
10563 for (i = 0; i < SCHARS (function); i++)
10564 add_command_key (Faref (function, make_number (i)));
10566 add_command_key (make_number ('\015'));
10569 last_point_position = saved_last_point_position;
10570 last_point_position_buffer = saved_last_point_position_buffer;
10572 UNGCPRO;
10574 function = Fintern (function, Qnil);
10575 current_kboard->Vprefix_arg = prefixarg;
10576 Vthis_command = function;
10577 real_this_command = function;
10579 /* If enabled, show which key runs this command. */
10580 if (!NILP (Vsuggest_key_bindings)
10581 && NILP (Vexecuting_kbd_macro)
10582 && SYMBOLP (function))
10583 bindings = Fwhere_is_internal (function, Voverriding_local_map,
10584 Qt, Qnil, Qnil);
10585 else
10586 bindings = Qnil;
10588 value = Qnil;
10589 GCPRO3 (bindings, value, function);
10590 value = Fcommand_execute (function, Qt, Qnil, Qnil);
10592 /* If the command has a key binding, print it now. */
10593 if (!NILP (bindings)
10594 && ! (VECTORP (bindings) && EQ (Faref (bindings, make_number (0)),
10595 Qmouse_movement)))
10597 /* But first wait, and skip the message if there is input. */
10598 Lisp_Object waited;
10600 /* If this command displayed something in the echo area;
10601 wait a few seconds, then display our suggestion message. */
10602 if (NILP (echo_area_buffer[0]))
10603 waited = sit_for (make_number (0), 0, 2);
10604 else if (NUMBERP (Vsuggest_key_bindings))
10605 waited = sit_for (Vsuggest_key_bindings, 0, 2);
10606 else
10607 waited = sit_for (make_number (2), 0, 2);
10609 if (!NILP (waited) && ! CONSP (Vunread_command_events))
10611 Lisp_Object binding;
10612 char *newmessage;
10613 int message_p = push_message ();
10614 int count = SPECPDL_INDEX ();
10616 record_unwind_protect (pop_message_unwind, Qnil);
10617 binding = Fkey_description (bindings, Qnil);
10619 newmessage
10620 = (char *) alloca (SCHARS (SYMBOL_NAME (function))
10621 + SBYTES (binding)
10622 + 100);
10623 sprintf (newmessage, "You can run the command `%s' with %s",
10624 SDATA (SYMBOL_NAME (function)),
10625 SDATA (binding));
10626 message2_nolog (newmessage,
10627 strlen (newmessage),
10628 STRING_MULTIBYTE (binding));
10629 if (NUMBERP (Vsuggest_key_bindings))
10630 waited = sit_for (Vsuggest_key_bindings, 0, 2);
10631 else
10632 waited = sit_for (make_number (2), 0, 2);
10634 if (!NILP (waited) && message_p)
10635 restore_message ();
10637 unbind_to (count, Qnil);
10641 RETURN_UNGCPRO (value);
10645 /* Return nonzero if input events are pending. */
10648 detect_input_pending ()
10650 if (!input_pending)
10651 get_input_pending (&input_pending, 0);
10653 return input_pending;
10656 /* Return nonzero if input events other than mouse movements are
10657 pending. */
10660 detect_input_pending_ignore_squeezables ()
10662 if (!input_pending)
10663 get_input_pending (&input_pending, READABLE_EVENTS_IGNORE_SQUEEZABLES);
10665 return input_pending;
10668 /* Return nonzero if input events are pending, and run any pending timers. */
10671 detect_input_pending_run_timers (do_display)
10672 int do_display;
10674 int old_timers_run = timers_run;
10676 if (!input_pending)
10677 get_input_pending (&input_pending, READABLE_EVENTS_DO_TIMERS_NOW);
10679 if (old_timers_run != timers_run && do_display)
10681 redisplay_preserve_echo_area (8);
10682 /* The following fixes a bug when using lazy-lock with
10683 lazy-lock-defer-on-the-fly set to t, i.e. when fontifying
10684 from an idle timer function. The symptom of the bug is that
10685 the cursor sometimes doesn't become visible until the next X
10686 event is processed. --gerd. */
10688 Lisp_Object tail, frame;
10689 FOR_EACH_FRAME (tail, frame)
10690 if (FRAME_RIF (XFRAME (frame)))
10691 FRAME_RIF (XFRAME (frame))->flush_display (XFRAME (frame));
10695 return input_pending;
10698 /* This is called in some cases before a possible quit.
10699 It cases the next call to detect_input_pending to recompute input_pending.
10700 So calling this function unnecessarily can't do any harm. */
10702 void
10703 clear_input_pending ()
10705 input_pending = 0;
10708 /* Return nonzero if there are pending requeued events.
10709 This isn't used yet. The hope is to make wait_reading_process_output
10710 call it, and return if it runs Lisp code that unreads something.
10711 The problem is, kbd_buffer_get_event needs to be fixed to know what
10712 to do in that case. It isn't trivial. */
10715 requeued_events_pending_p ()
10717 return (!NILP (Vunread_command_events) || unread_command_char != -1);
10721 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
10722 doc: /* Return t if command input is currently available with no wait.
10723 Actually, the value is nil only if we can be sure that no input is available;
10724 if there is a doubt, the value is t. */)
10727 if (!NILP (Vunread_command_events) || unread_command_char != -1
10728 || !NILP (Vunread_post_input_method_events)
10729 || !NILP (Vunread_input_method_events))
10730 return (Qt);
10732 get_input_pending (&input_pending,
10733 READABLE_EVENTS_DO_TIMERS_NOW
10734 | READABLE_EVENTS_FILTER_EVENTS);
10735 return input_pending > 0 ? Qt : Qnil;
10738 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
10739 doc: /* Return vector of last 300 events, not counting those from keyboard macros. */)
10742 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
10743 Lisp_Object val;
10745 if (total_keys < NUM_RECENT_KEYS)
10746 return Fvector (total_keys, keys);
10747 else
10749 val = Fvector (NUM_RECENT_KEYS, keys);
10750 bcopy (keys + recent_keys_index,
10751 XVECTOR (val)->contents,
10752 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
10753 bcopy (keys,
10754 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
10755 recent_keys_index * sizeof (Lisp_Object));
10756 return val;
10760 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
10761 doc: /* Return the key sequence that invoked this command.
10762 However, if the command has called `read-key-sequence', it returns
10763 the last key sequence that has been read.
10764 The value is a string or a vector.
10766 See also `this-command-keys-vector'. */)
10769 return make_event_array (this_command_key_count,
10770 XVECTOR (this_command_keys)->contents);
10773 DEFUN ("this-command-keys-vector", Fthis_command_keys_vector, Sthis_command_keys_vector, 0, 0, 0,
10774 doc: /* Return the key sequence that invoked this command, as a vector.
10775 However, if the command has called `read-key-sequence', it returns
10776 the last key sequence that has been read.
10778 See also `this-command-keys'. */)
10781 return Fvector (this_command_key_count,
10782 XVECTOR (this_command_keys)->contents);
10785 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
10786 Sthis_single_command_keys, 0, 0, 0,
10787 doc: /* Return the key sequence that invoked this command.
10788 More generally, it returns the last key sequence read, either by
10789 the command loop or by `read-key-sequence'.
10790 Unlike `this-command-keys', this function's value
10791 does not include prefix arguments.
10792 The value is always a vector. */)
10795 return Fvector (this_command_key_count
10796 - this_single_command_key_start,
10797 (XVECTOR (this_command_keys)->contents
10798 + this_single_command_key_start));
10801 DEFUN ("this-single-command-raw-keys", Fthis_single_command_raw_keys,
10802 Sthis_single_command_raw_keys, 0, 0, 0,
10803 doc: /* Return the raw events that were read for this command.
10804 More generally, it returns the last key sequence read, either by
10805 the command loop or by `read-key-sequence'.
10806 Unlike `this-single-command-keys', this function's value
10807 shows the events before all translations (except for input methods).
10808 The value is always a vector. */)
10811 return Fvector (raw_keybuf_count,
10812 (XVECTOR (raw_keybuf)->contents));
10815 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
10816 Sreset_this_command_lengths, 0, 0, 0,
10817 doc: /* Make the unread events replace the last command and echo.
10818 Used in `universal-argument-other-key'.
10820 `universal-argument-other-key' rereads the event just typed.
10821 It then gets translated through `function-key-map'.
10822 The translated event has to replace the real events,
10823 both in the value of (this-command-keys) and in echoing.
10824 To achieve this, `universal-argument-other-key' calls
10825 `reset-this-command-lengths', which discards the record of reading
10826 these events the first time. */)
10829 this_command_key_count = before_command_key_count;
10830 if (this_command_key_count < this_single_command_key_start)
10831 this_single_command_key_start = this_command_key_count;
10833 echo_truncate (before_command_echo_length);
10835 /* Cause whatever we put into unread-command-events
10836 to echo as if it were being freshly read from the keyboard. */
10837 this_command_key_count_reset = 1;
10839 return Qnil;
10842 DEFUN ("clear-this-command-keys", Fclear_this_command_keys,
10843 Sclear_this_command_keys, 0, 1, 0,
10844 doc: /* Clear out the vector that `this-command-keys' returns.
10845 Also clear the record of the last 100 events, unless optional arg
10846 KEEP-RECORD is non-nil. */)
10847 (keep_record)
10848 Lisp_Object keep_record;
10850 int i;
10852 this_command_key_count = 0;
10853 this_command_key_count_reset = 0;
10855 if (NILP (keep_record))
10857 for (i = 0; i < XVECTOR (recent_keys)->size; ++i)
10858 XVECTOR (recent_keys)->contents[i] = Qnil;
10859 total_keys = 0;
10860 recent_keys_index = 0;
10862 return Qnil;
10865 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
10866 doc: /* Return the current depth in recursive edits. */)
10869 Lisp_Object temp;
10870 XSETFASTINT (temp, command_loop_level + minibuf_level);
10871 return temp;
10874 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
10875 "FOpen dribble file: ",
10876 doc: /* Start writing all keyboard characters to a dribble file called FILE.
10877 If FILE is nil, close any open dribble file. */)
10878 (file)
10879 Lisp_Object file;
10881 if (dribble)
10883 BLOCK_INPUT;
10884 fclose (dribble);
10885 UNBLOCK_INPUT;
10886 dribble = 0;
10888 if (!NILP (file))
10890 file = Fexpand_file_name (file, Qnil);
10891 dribble = fopen (SDATA (file), "w");
10892 if (dribble == 0)
10893 report_file_error ("Opening dribble", Fcons (file, Qnil));
10895 return Qnil;
10898 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
10899 doc: /* Discard the contents of the terminal input buffer.
10900 Also end any kbd macro being defined. */)
10903 if (!NILP (current_kboard->defining_kbd_macro))
10905 /* Discard the last command from the macro. */
10906 Fcancel_kbd_macro_events ();
10907 end_kbd_macro ();
10910 update_mode_lines++;
10912 Vunread_command_events = Qnil;
10913 unread_command_char = -1;
10915 discard_tty_input ();
10917 kbd_fetch_ptr = kbd_store_ptr;
10918 input_pending = 0;
10920 return Qnil;
10923 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
10924 doc: /* Stop Emacs and return to superior process. You can resume later.
10925 If `cannot-suspend' is non-nil, or if the system doesn't support job
10926 control, run a subshell instead.
10928 If optional arg STUFFSTRING is non-nil, its characters are stuffed
10929 to be read as terminal input by Emacs's parent, after suspension.
10931 Before suspending, run the normal hook `suspend-hook'.
10932 After resumption run the normal hook `suspend-resume-hook'.
10934 Some operating systems cannot stop the Emacs process and resume it later.
10935 On such systems, Emacs starts a subshell instead of suspending. */)
10936 (stuffstring)
10937 Lisp_Object stuffstring;
10939 int count = SPECPDL_INDEX ();
10940 int old_height, old_width;
10941 int width, height;
10942 struct gcpro gcpro1;
10944 if (tty_list && tty_list->next)
10945 error ("There are other tty frames open; close them before suspending Emacs");
10947 if (!NILP (stuffstring))
10948 CHECK_STRING (stuffstring);
10950 /* Run the functions in suspend-hook. */
10951 if (!NILP (Vrun_hooks))
10952 call1 (Vrun_hooks, intern ("suspend-hook"));
10954 GCPRO1 (stuffstring);
10955 get_tty_size (fileno (CURTTY ()->input), &old_width, &old_height);
10956 reset_all_sys_modes ();
10957 /* sys_suspend can get an error if it tries to fork a subshell
10958 and the system resources aren't available for that. */
10959 record_unwind_protect ((Lisp_Object (*) P_ ((Lisp_Object))) init_all_sys_modes,
10960 Qnil);
10961 stuff_buffered_input (stuffstring);
10962 if (cannot_suspend)
10963 sys_subshell ();
10964 else
10965 sys_suspend ();
10966 unbind_to (count, Qnil);
10968 /* Check if terminal/window size has changed.
10969 Note that this is not useful when we are running directly
10970 with a window system; but suspend should be disabled in that case. */
10971 get_tty_size (fileno (CURTTY ()->input), &width, &height);
10972 if (width != old_width || height != old_height)
10973 change_frame_size (SELECTED_FRAME (), height, width, 0, 0, 0);
10975 /* Run suspend-resume-hook. */
10976 if (!NILP (Vrun_hooks))
10977 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
10979 UNGCPRO;
10980 return Qnil;
10983 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
10984 Then in any case stuff anything Emacs has read ahead and not used. */
10986 void
10987 stuff_buffered_input (stuffstring)
10988 Lisp_Object stuffstring;
10990 #ifdef SIGTSTP /* stuff_char is defined if SIGTSTP. */
10991 register unsigned char *p;
10993 if (STRINGP (stuffstring))
10995 register int count;
10997 p = SDATA (stuffstring);
10998 count = SBYTES (stuffstring);
10999 while (count-- > 0)
11000 stuff_char (*p++);
11001 stuff_char ('\n');
11004 /* Anything we have read ahead, put back for the shell to read. */
11005 /* ?? What should this do when we have multiple keyboards??
11006 Should we ignore anything that was typed in at the "wrong" kboard?
11008 rms: we should stuff everything back into the kboard
11009 it came from. */
11010 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
11013 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
11014 kbd_fetch_ptr = kbd_buffer;
11015 if (kbd_fetch_ptr->kind == ASCII_KEYSTROKE_EVENT)
11016 stuff_char (kbd_fetch_ptr->code);
11018 clear_event (kbd_fetch_ptr);
11021 input_pending = 0;
11022 #endif /* SIGTSTP */
11025 void
11026 set_waiting_for_input (time_to_clear)
11027 EMACS_TIME *time_to_clear;
11029 input_available_clear_time = time_to_clear;
11031 /* Tell handle_interrupt to throw back to read_char, */
11032 waiting_for_input = 1;
11034 /* If handle_interrupt was called before and buffered a C-g,
11035 make it run again now, to avoid timing error. */
11036 if (!NILP (Vquit_flag))
11037 quit_throw_to_read_char ();
11040 void
11041 clear_waiting_for_input ()
11043 /* Tell handle_interrupt not to throw back to read_char, */
11044 waiting_for_input = 0;
11045 input_available_clear_time = 0;
11048 /* The SIGINT handler.
11050 If we have a frame on the controlling tty, we assume that the
11051 SIGINT was generated by C-g, so we call handle_interrupt.
11052 Otherwise, the handler kills Emacs. */
11054 static SIGTYPE
11055 interrupt_signal (signalnum) /* If we don't have an argument, */
11056 int signalnum; /* some compilers complain in signal calls. */
11058 /* Must preserve main program's value of errno. */
11059 int old_errno = errno;
11060 struct terminal *terminal;
11062 #if defined (USG) && !defined (POSIX_SIGNALS)
11063 /* USG systems forget handlers when they are used;
11064 must reestablish each time */
11065 signal (SIGINT, interrupt_signal);
11066 signal (SIGQUIT, interrupt_signal);
11067 #endif /* USG */
11069 SIGNAL_THREAD_CHECK (signalnum);
11071 /* See if we have an active terminal on our controlling tty. */
11072 terminal = get_named_tty ("/dev/tty");
11073 if (!terminal)
11075 /* If there are no frames there, let's pretend that we are a
11076 well-behaving UN*X program and quit. */
11077 Fkill_emacs (Qnil);
11079 else
11081 /* Otherwise, the SIGINT was probably generated by C-g. */
11083 /* Set internal_last_event_frame to the top frame of the
11084 controlling tty, if we have a frame there. We disable the
11085 interrupt key on secondary ttys, so the SIGINT must have come
11086 from the controlling tty. */
11087 internal_last_event_frame = terminal->display_info.tty->top_frame;
11089 handle_interrupt ();
11092 errno = old_errno;
11095 /* This routine is called at interrupt level in response to C-g.
11097 It is called from the SIGINT handler or kbd_buffer_store_event.
11099 If `waiting_for_input' is non zero, then unless `echoing' is
11100 nonzero, immediately throw back to read_char.
11102 Otherwise it sets the Lisp variable quit-flag not-nil. This causes
11103 eval to throw, when it gets a chance. If quit-flag is already
11104 non-nil, it stops the job right away. */
11106 static void
11107 handle_interrupt ()
11109 char c;
11111 cancel_echoing ();
11113 /* XXX This code needs to be revised for multi-tty support. */
11114 if (!NILP (Vquit_flag)
11115 #ifndef MSDOS
11116 && get_named_tty ("/dev/tty")
11117 #endif
11120 /* If SIGINT isn't blocked, don't let us be interrupted by
11121 another SIGINT, it might be harmful due to non-reentrancy
11122 in I/O functions. */
11123 sigblock (sigmask (SIGINT));
11125 fflush (stdout);
11126 reset_all_sys_modes ();
11128 #ifdef SIGTSTP /* Support possible in later USG versions */
11130 * On systems which can suspend the current process and return to the original
11131 * shell, this command causes the user to end up back at the shell.
11132 * The "Auto-save" and "Abort" questions are not asked until
11133 * the user elects to return to emacs, at which point he can save the current
11134 * job and either dump core or continue.
11136 sys_suspend ();
11137 #else
11138 #ifdef VMS
11139 if (sys_suspend () == -1)
11141 printf ("Not running as a subprocess;\n");
11142 printf ("you can continue or abort.\n");
11144 #else /* not VMS */
11145 /* Perhaps should really fork an inferior shell?
11146 But that would not provide any way to get back
11147 to the original shell, ever. */
11148 printf ("No support for stopping a process on this operating system;\n");
11149 printf ("you can continue or abort.\n");
11150 #endif /* not VMS */
11151 #endif /* not SIGTSTP */
11152 #ifdef MSDOS
11153 /* We must remain inside the screen area when the internal terminal
11154 is used. Note that [Enter] is not echoed by dos. */
11155 cursor_to (0, 0);
11156 #endif
11157 /* It doesn't work to autosave while GC is in progress;
11158 the code used for auto-saving doesn't cope with the mark bit. */
11159 if (!gc_in_progress)
11161 printf ("Auto-save? (y or n) ");
11162 fflush (stdout);
11163 if (((c = getchar ()) & ~040) == 'Y')
11165 Fdo_auto_save (Qt, Qnil);
11166 #ifdef MSDOS
11167 printf ("\r\nAuto-save done");
11168 #else /* not MSDOS */
11169 printf ("Auto-save done\n");
11170 #endif /* not MSDOS */
11172 while (c != '\n') c = getchar ();
11174 else
11176 /* During GC, it must be safe to reenable quitting again. */
11177 Vinhibit_quit = Qnil;
11178 #ifdef MSDOS
11179 printf ("\r\n");
11180 #endif /* not MSDOS */
11181 printf ("Garbage collection in progress; cannot auto-save now\r\n");
11182 printf ("but will instead do a real quit after garbage collection ends\r\n");
11183 fflush (stdout);
11186 #ifdef MSDOS
11187 printf ("\r\nAbort? (y or n) ");
11188 #else /* not MSDOS */
11189 #ifdef VMS
11190 printf ("Abort (and enter debugger)? (y or n) ");
11191 #else /* not VMS */
11192 printf ("Abort (and dump core)? (y or n) ");
11193 #endif /* not VMS */
11194 #endif /* not MSDOS */
11195 fflush (stdout);
11196 if (((c = getchar ()) & ~040) == 'Y')
11197 abort ();
11198 while (c != '\n') c = getchar ();
11199 #ifdef MSDOS
11200 printf ("\r\nContinuing...\r\n");
11201 #else /* not MSDOS */
11202 printf ("Continuing...\n");
11203 #endif /* not MSDOS */
11204 fflush (stdout);
11205 init_all_sys_modes ();
11206 sigfree ();
11208 else
11210 /* If executing a function that wants to be interrupted out of
11211 and the user has not deferred quitting by binding `inhibit-quit'
11212 then quit right away. */
11213 if (immediate_quit && NILP (Vinhibit_quit))
11215 struct gl_state_s saved;
11216 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
11218 immediate_quit = 0;
11219 sigfree ();
11220 saved = gl_state;
11221 GCPRO4 (saved.object, saved.global_code,
11222 saved.current_syntax_table, saved.old_prop);
11223 Fsignal (Qquit, Qnil);
11224 gl_state = saved;
11225 UNGCPRO;
11227 else
11228 /* Else request quit when it's safe */
11229 Vquit_flag = Qt;
11232 if (waiting_for_input && !echoing)
11233 quit_throw_to_read_char ();
11236 /* Handle a C-g by making read_char return C-g. */
11238 void
11239 quit_throw_to_read_char ()
11241 sigfree ();
11242 /* Prevent another signal from doing this before we finish. */
11243 clear_waiting_for_input ();
11244 input_pending = 0;
11246 Vunread_command_events = Qnil;
11247 unread_command_char = -1;
11249 #if 0 /* Currently, sit_for is called from read_char without turning
11250 off polling. And that can call set_waiting_for_input.
11251 It seems to be harmless. */
11252 #ifdef POLL_FOR_INPUT
11253 /* May be > 1 if in recursive minibuffer. */
11254 if (poll_suppress_count == 0)
11255 abort ();
11256 #endif
11257 #endif
11258 if (FRAMEP (internal_last_event_frame)
11259 && !EQ (internal_last_event_frame, selected_frame))
11260 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
11261 0, 0);
11263 _longjmp (getcjmp, 1);
11266 DEFUN ("set-input-interrupt-mode", Fset_input_interrupt_mode, Sset_input_interrupt_mode, 1, 1, 0,
11267 doc: /* Set interrupt mode of reading keyboard input.
11268 If INTERRUPT is non-nil, Emacs will use input interrupts;
11269 otherwise Emacs uses CBREAK mode.
11271 See also `current-input-mode'. */)
11272 (interrupt)
11273 Lisp_Object interrupt;
11275 int new_interrupt_input;
11276 #ifdef SIGIO
11277 /* Note SIGIO has been undef'd if FIONREAD is missing. */
11278 #ifdef HAVE_X_WINDOWS
11279 if (x_display_list != NULL)
11281 /* When using X, don't give the user a real choice,
11282 because we haven't implemented the mechanisms to support it. */
11283 #ifdef NO_SOCK_SIGIO
11284 new_interrupt_input = 0;
11285 #else /* not NO_SOCK_SIGIO */
11286 new_interrupt_input = 1;
11287 #endif /* NO_SOCK_SIGIO */
11289 else
11290 #endif
11291 new_interrupt_input = !NILP (interrupt);
11292 #else /* not SIGIO */
11293 new_interrupt_input = 0;
11294 #endif /* not SIGIO */
11296 /* Our VMS input only works by interrupts, as of now. */
11297 #ifdef VMS
11298 new_interrupt_input = 1;
11299 #endif
11301 if (new_interrupt_input != interrupt_input)
11303 #ifdef POLL_FOR_INPUT
11304 stop_polling ();
11305 #endif
11306 #ifndef DOS_NT
11307 /* this causes startup screen to be restored and messes with the mouse */
11308 reset_all_sys_modes ();
11309 #endif
11310 interrupt_input = new_interrupt_input;
11311 #ifndef DOS_NT
11312 init_all_sys_modes ();
11313 #endif
11315 #ifdef POLL_FOR_INPUT
11316 poll_suppress_count = 1;
11317 start_polling ();
11318 #endif
11320 return Qnil;
11323 DEFUN ("set-output-flow-control", Fset_output_flow_control, Sset_output_flow_control, 1, 2, 0,
11324 doc: /* Enable or disable ^S/^Q flow control for output to TERMINAL.
11325 If FLOW is non-nil, flow control is enabled and you cannot use C-s or
11326 C-q in key sequences.
11328 This setting only has an effect on tty terminals and only when
11329 Emacs reads input in CBREAK mode; see `set-input-interrupt-mode'.
11331 See also `current-input-mode'. */)
11332 (flow, terminal)
11333 Lisp_Object flow, terminal;
11335 struct terminal *t = get_terminal (terminal, 1);
11336 struct tty_display_info *tty;
11337 if (t == NULL || t->type != output_termcap)
11338 return Qnil;
11339 tty = t->display_info.tty;
11341 if (tty->flow_control != !NILP (flow))
11343 #ifndef DOS_NT
11344 /* this causes startup screen to be restored and messes with the mouse */
11345 reset_sys_modes (tty);
11346 #endif
11348 tty->flow_control = !NILP (flow);
11350 #ifndef DOS_NT
11351 init_sys_modes (tty);
11352 #endif
11354 return Qnil;
11357 DEFUN ("set-input-meta-mode", Fset_input_meta_mode, Sset_input_meta_mode, 1, 2, 0,
11358 doc: /* Enable or disable 8-bit input on TERMINAL.
11359 If META is t, Emacs will accept 8-bit input, and interpret the 8th
11360 bit as the Meta modifier.
11362 If META is nil, Emacs will ignore the top bit, on the assumption it is
11363 parity.
11365 Otherwise, Emacs will accept and pass through 8-bit input without
11366 specially interpreting the top bit.
11368 This setting only has an effect on tty terminal devices.
11370 Optional parameter TERMINAL specifies the tty terminal device to use.
11371 It may be a terminal id, a frame, or nil for the terminal used by the
11372 currently selected frame.
11374 See also `current-input-mode'. */)
11375 (meta, terminal)
11376 Lisp_Object meta, terminal;
11378 struct terminal *t = get_terminal (terminal, 1);
11379 struct tty_display_info *tty;
11380 int new_meta;
11382 if (t == NULL || t->type != output_termcap)
11383 return Qnil;
11384 tty = t->display_info.tty;
11386 if (NILP (meta))
11387 new_meta = 0;
11388 else if (EQ (meta, Qt))
11389 new_meta = 1;
11390 else
11391 new_meta = 2;
11393 if (tty->meta_key != new_meta)
11395 #ifndef DOS_NT
11396 /* this causes startup screen to be restored and messes with the mouse */
11397 reset_sys_modes (tty);
11398 #endif
11400 tty->meta_key = new_meta;
11402 #ifndef DOS_NT
11403 init_sys_modes (tty);
11404 #endif
11406 return Qnil;
11409 DEFUN ("set-quit-char", Fset_quit_char, Sset_quit_char, 1, 1, 0,
11410 doc: /* Specify character used for quitting.
11411 QUIT must be an ASCII character.
11413 This function only has an effect on the controlling tty of the Emacs
11414 process.
11416 See also `current-input-mode'. */)
11417 (quit)
11418 Lisp_Object quit;
11420 struct terminal *t = get_named_tty ("/dev/tty");
11421 struct tty_display_info *tty;
11422 if (t == NULL || t->type != output_termcap)
11423 return Qnil;
11424 tty = t->display_info.tty;
11426 if (NILP (quit) || !INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400)
11427 error ("QUIT must be an ASCII character");
11429 #ifndef DOS_NT
11430 /* this causes startup screen to be restored and messes with the mouse */
11431 reset_sys_modes (tty);
11432 #endif
11434 /* Don't let this value be out of range. */
11435 quit_char = XINT (quit) & (tty->meta_key == 0 ? 0177 : 0377);
11437 #ifndef DOS_NT
11438 init_sys_modes (tty);
11439 #endif
11441 return Qnil;
11444 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
11445 doc: /* Set mode of reading keyboard input.
11446 First arg INTERRUPT non-nil means use input interrupts;
11447 nil means use CBREAK mode.
11448 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal
11449 (no effect except in CBREAK mode).
11450 Third arg META t means accept 8-bit input (for a Meta key).
11451 META nil means ignore the top bit, on the assumption it is parity.
11452 Otherwise, accept 8-bit input and don't use the top bit for Meta.
11453 Optional fourth arg QUIT if non-nil specifies character to use for quitting.
11454 See also `current-input-mode'. */)
11455 (interrupt, flow, meta, quit)
11456 Lisp_Object interrupt, flow, meta, quit;
11458 Fset_input_interrupt_mode (interrupt);
11459 Fset_output_flow_control (flow, Qnil);
11460 Fset_input_meta_mode (meta, Qnil);
11461 if (!NILP (quit))
11462 Fset_quit_char (quit);
11463 return Qnil;
11466 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
11467 doc: /* Return information about the way Emacs currently reads keyboard input.
11468 The value is a list of the form (INTERRUPT FLOW META QUIT), where
11469 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if
11470 nil, Emacs is using CBREAK mode.
11471 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the
11472 terminal; this does not apply if Emacs uses interrupt-driven input.
11473 META is t if accepting 8-bit input with 8th bit as Meta flag.
11474 META nil means ignoring the top bit, on the assumption it is parity.
11475 META is neither t nor nil if accepting 8-bit input and using
11476 all 8 bits as the character code.
11477 QUIT is the character Emacs currently uses to quit.
11478 The elements of this list correspond to the arguments of
11479 `set-input-mode'. */)
11482 Lisp_Object val[4];
11483 struct frame *sf = XFRAME (selected_frame);
11485 val[0] = interrupt_input ? Qt : Qnil;
11486 if (FRAME_TERMCAP_P (sf))
11488 val[1] = FRAME_TTY (sf)->flow_control ? Qt : Qnil;
11489 val[2] = (FRAME_TTY (sf)->meta_key == 2
11490 ? make_number (0)
11491 : (CURTTY ()->meta_key == 1 ? Qt : Qnil));
11493 else
11495 val[1] = Qnil;
11496 val[2] = Qt;
11498 XSETFASTINT (val[3], quit_char);
11500 return Flist (sizeof (val) / sizeof (val[0]), val);
11503 DEFUN ("posn-at-x-y", Fposn_at_x_y, Sposn_at_x_y, 2, 4, 0,
11504 doc: /* Return position information for pixel coordinates X and Y.
11505 By default, X and Y are relative to text area of the selected window.
11506 Optional third arg FRAME-OR-WINDOW non-nil specifies frame or window.
11507 If optional fourth arg WHOLE is non-nil, X is relative to the left
11508 edge of the window.
11510 The return value is similar to a mouse click position:
11511 (WINDOW AREA-OR-POS (X . Y) TIMESTAMP OBJECT POS (COL . ROW)
11512 IMAGE (DX . DY) (WIDTH . HEIGHT))
11513 The `posn-' functions access elements of such lists. */)
11514 (x, y, frame_or_window, whole)
11515 Lisp_Object x, y, frame_or_window, whole;
11517 CHECK_NATNUM (x);
11518 CHECK_NATNUM (y);
11520 if (NILP (frame_or_window))
11521 frame_or_window = selected_window;
11523 if (WINDOWP (frame_or_window))
11525 struct window *w;
11527 CHECK_LIVE_WINDOW (frame_or_window);
11529 w = XWINDOW (frame_or_window);
11530 XSETINT (x, (XINT (x)
11531 + WINDOW_LEFT_EDGE_X (w)
11532 + (NILP (whole)
11533 ? window_box_left_offset (w, TEXT_AREA)
11534 : 0)));
11535 XSETINT (y, WINDOW_TO_FRAME_PIXEL_Y (w, XINT (y)));
11536 frame_or_window = w->frame;
11539 CHECK_LIVE_FRAME (frame_or_window);
11541 return make_lispy_position (XFRAME (frame_or_window), &x, &y, 0);
11544 DEFUN ("posn-at-point", Fposn_at_point, Sposn_at_point, 0, 2, 0,
11545 doc: /* Return position information for buffer POS in WINDOW.
11546 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
11548 Return nil if position is not visible in window. Otherwise,
11549 the return value is similar to that returned by `event-start' for
11550 a mouse click at the upper left corner of the glyph corresponding
11551 to the given buffer position:
11552 (WINDOW AREA-OR-POS (X . Y) TIMESTAMP OBJECT POS (COL . ROW)
11553 IMAGE (DX . DY) (WIDTH . HEIGHT))
11554 The `posn-' functions access elements of such lists. */)
11555 (pos, window)
11556 Lisp_Object pos, window;
11558 Lisp_Object tem;
11560 if (NILP (window))
11561 window = selected_window;
11563 tem = Fpos_visible_in_window_p (pos, window, Qt);
11564 if (!NILP (tem))
11566 Lisp_Object x = XCAR (tem);
11567 Lisp_Object y = XCAR (XCDR (tem));
11569 /* Point invisible due to hscrolling? */
11570 if (XINT (x) < 0)
11571 return Qnil;
11572 tem = Fposn_at_x_y (x, y, window, Qnil);
11575 return tem;
11580 * Set up a new kboard object with reasonable initial values.
11582 void
11583 init_kboard (kb)
11584 KBOARD *kb;
11586 kb->Voverriding_terminal_local_map = Qnil;
11587 kb->Vlast_command = Qnil;
11588 kb->Vreal_last_command = Qnil;
11589 kb->Vkeyboard_translate_table = Qnil;
11590 kb->Vlast_repeatable_command = Qnil;
11591 kb->Vprefix_arg = Qnil;
11592 kb->Vlast_prefix_arg = Qnil;
11593 kb->kbd_queue = Qnil;
11594 kb->kbd_queue_has_data = 0;
11595 kb->immediate_echo = 0;
11596 kb->echo_string = Qnil;
11597 kb->echo_after_prompt = -1;
11598 kb->kbd_macro_buffer = 0;
11599 kb->kbd_macro_bufsize = 0;
11600 kb->defining_kbd_macro = Qnil;
11601 kb->Vlast_kbd_macro = Qnil;
11602 kb->reference_count = 0;
11603 kb->Vsystem_key_alist = Qnil;
11604 kb->system_key_syms = Qnil;
11605 kb->Vwindow_system = Qt; /* Unset. */
11606 kb->Vinput_decode_map = Fmake_sparse_keymap (Qnil);
11607 kb->Vlocal_function_key_map = Fmake_sparse_keymap (Qnil);
11608 Fset_keymap_parent (kb->Vlocal_function_key_map, Vfunction_key_map);
11609 kb->Vdefault_minibuffer_frame = Qnil;
11613 * Destroy the contents of a kboard object, but not the object itself.
11614 * We use this just before deleting it, or if we're going to initialize
11615 * it a second time.
11617 static void
11618 wipe_kboard (kb)
11619 KBOARD *kb;
11621 if (kb->kbd_macro_buffer)
11622 xfree (kb->kbd_macro_buffer);
11625 #ifdef MULTI_KBOARD
11627 /* Free KB and memory referenced from it. */
11629 void
11630 delete_kboard (kb)
11631 KBOARD *kb;
11633 KBOARD **kbp;
11635 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
11636 if (*kbp == NULL)
11637 abort ();
11638 *kbp = kb->next_kboard;
11640 /* Prevent a dangling reference to KB. */
11641 if (kb == current_kboard
11642 && FRAMEP (selected_frame)
11643 && FRAME_LIVE_P (XFRAME (selected_frame)))
11645 current_kboard = FRAME_KBOARD (XFRAME (selected_frame));
11646 single_kboard = 0;
11647 if (current_kboard == kb)
11648 abort ();
11651 wipe_kboard (kb);
11652 xfree (kb);
11655 #endif /* MULTI_KBOARD */
11657 void
11658 init_keyboard ()
11660 /* This is correct before outermost invocation of the editor loop */
11661 command_loop_level = -1;
11662 immediate_quit = 0;
11663 quit_char = Ctl ('g');
11664 Vunread_command_events = Qnil;
11665 unread_command_char = -1;
11666 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
11667 total_keys = 0;
11668 recent_keys_index = 0;
11669 kbd_fetch_ptr = kbd_buffer;
11670 kbd_store_ptr = kbd_buffer;
11671 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
11672 do_mouse_tracking = Qnil;
11673 #endif
11674 input_pending = 0;
11675 interrupt_input_blocked = 0;
11676 interrupt_input_pending = 0;
11678 /* This means that command_loop_1 won't try to select anything the first
11679 time through. */
11680 internal_last_event_frame = Qnil;
11681 Vlast_event_frame = internal_last_event_frame;
11683 #ifdef MULTI_KBOARD
11684 current_kboard = initial_kboard;
11685 #endif
11686 /* Re-initialize the keyboard again. */
11687 wipe_kboard (current_kboard);
11688 init_kboard (current_kboard);
11689 /* A value of nil for Vwindow_system normally means a tty, but we also use
11690 it for the initial terminal since there is no window system there. */
11691 current_kboard->Vwindow_system = Qnil;
11693 if (!noninteractive)
11695 /* Before multi-tty support, these handlers used to be installed
11696 only if the current session was a tty session. Now an Emacs
11697 session may have multiple display types, so we always handle
11698 SIGINT. There is special code in interrupt_signal to exit
11699 Emacs on SIGINT when there are no termcap frames on the
11700 controlling terminal. */
11701 signal (SIGINT, interrupt_signal);
11702 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
11703 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
11704 SIGQUIT and we can't tell which one it will give us. */
11705 signal (SIGQUIT, interrupt_signal);
11706 #endif /* HAVE_TERMIO */
11708 /* Note SIGIO has been undef'd if FIONREAD is missing. */
11709 #ifdef SIGIO
11710 if (!noninteractive)
11711 signal (SIGIO, input_available_signal);
11712 #endif /* SIGIO */
11714 /* Use interrupt input by default, if it works and noninterrupt input
11715 has deficiencies. */
11717 #ifdef INTERRUPT_INPUT
11718 interrupt_input = 1;
11719 #else
11720 interrupt_input = 0;
11721 #endif
11723 /* Our VMS input only works by interrupts, as of now. */
11724 #ifdef VMS
11725 interrupt_input = 1;
11726 #endif
11728 sigfree ();
11729 dribble = 0;
11731 if (keyboard_init_hook)
11732 (*keyboard_init_hook) ();
11734 #ifdef POLL_FOR_INPUT
11735 poll_suppress_count = 1;
11736 start_polling ();
11737 #endif
11740 /* This type's only use is in syms_of_keyboard, to initialize the
11741 event header symbols and put properties on them. */
11742 struct event_head {
11743 Lisp_Object *var;
11744 char *name;
11745 Lisp_Object *kind;
11748 struct event_head head_table[] = {
11749 {&Qmouse_movement, "mouse-movement", &Qmouse_movement},
11750 {&Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement},
11751 {&Qswitch_frame, "switch-frame", &Qswitch_frame},
11752 {&Qdelete_frame, "delete-frame", &Qdelete_frame},
11753 {&Qiconify_frame, "iconify-frame", &Qiconify_frame},
11754 {&Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible},
11755 /* `select-window' should be handled just like `switch-frame'
11756 in read_key_sequence. */
11757 {&Qselect_window, "select-window", &Qswitch_frame}
11760 void
11761 syms_of_keyboard ()
11763 pending_funcalls = Qnil;
11765 Vpre_help_message = Qnil;
11766 staticpro (&Vpre_help_message);
11768 Vlispy_mouse_stem = build_string ("mouse");
11769 staticpro (&Vlispy_mouse_stem);
11771 /* Tool-bars. */
11772 QCimage = intern (":image");
11773 staticpro (&QCimage);
11775 staticpro (&Qhelp_echo);
11776 Qhelp_echo = intern ("help-echo");
11778 staticpro (&Qrtl);
11779 Qrtl = intern (":rtl");
11781 staticpro (&item_properties);
11782 item_properties = Qnil;
11784 staticpro (&tool_bar_item_properties);
11785 tool_bar_item_properties = Qnil;
11786 staticpro (&tool_bar_items_vector);
11787 tool_bar_items_vector = Qnil;
11789 staticpro (&real_this_command);
11790 real_this_command = Qnil;
11792 Qtimer_event_handler = intern ("timer-event-handler");
11793 staticpro (&Qtimer_event_handler);
11795 Qdisabled_command_function = intern ("disabled-command-function");
11796 staticpro (&Qdisabled_command_function);
11798 Qself_insert_command = intern ("self-insert-command");
11799 staticpro (&Qself_insert_command);
11801 Qforward_char = intern ("forward-char");
11802 staticpro (&Qforward_char);
11804 Qbackward_char = intern ("backward-char");
11805 staticpro (&Qbackward_char);
11807 Qdisabled = intern ("disabled");
11808 staticpro (&Qdisabled);
11810 Qundefined = intern ("undefined");
11811 staticpro (&Qundefined);
11813 Qpre_command_hook = intern ("pre-command-hook");
11814 staticpro (&Qpre_command_hook);
11816 Qpost_command_hook = intern ("post-command-hook");
11817 staticpro (&Qpost_command_hook);
11819 Qdeferred_action_function = intern ("deferred-action-function");
11820 staticpro (&Qdeferred_action_function);
11822 Qcommand_hook_internal = intern ("command-hook-internal");
11823 staticpro (&Qcommand_hook_internal);
11825 Qfunction_key = intern ("function-key");
11826 staticpro (&Qfunction_key);
11827 Qmouse_click = intern ("mouse-click");
11828 staticpro (&Qmouse_click);
11829 #if defined (WINDOWSNT) || defined (MAC_OS)
11830 Qlanguage_change = intern ("language-change");
11831 staticpro (&Qlanguage_change);
11832 #endif
11833 Qdrag_n_drop = intern ("drag-n-drop");
11834 staticpro (&Qdrag_n_drop);
11836 Qsave_session = intern ("save-session");
11837 staticpro (&Qsave_session);
11839 #ifdef MAC_OS
11840 Qmac_apple_event = intern ("mac-apple-event");
11841 staticpro (&Qmac_apple_event);
11842 #endif
11844 #ifdef HAVE_DBUS
11845 Qdbus_event = intern ("dbus-event");
11846 staticpro (&Qdbus_event);
11847 #endif
11849 Qmenu_enable = intern ("menu-enable");
11850 staticpro (&Qmenu_enable);
11851 Qmenu_alias = intern ("menu-alias");
11852 staticpro (&Qmenu_alias);
11853 QCenable = intern (":enable");
11854 staticpro (&QCenable);
11855 QCvisible = intern (":visible");
11856 staticpro (&QCvisible);
11857 QChelp = intern (":help");
11858 staticpro (&QChelp);
11859 QCfilter = intern (":filter");
11860 staticpro (&QCfilter);
11861 QCbutton = intern (":button");
11862 staticpro (&QCbutton);
11863 QCkeys = intern (":keys");
11864 staticpro (&QCkeys);
11865 QCkey_sequence = intern (":key-sequence");
11866 staticpro (&QCkey_sequence);
11867 QCtoggle = intern (":toggle");
11868 staticpro (&QCtoggle);
11869 QCradio = intern (":radio");
11870 staticpro (&QCradio);
11872 Qmode_line = intern ("mode-line");
11873 staticpro (&Qmode_line);
11874 Qvertical_line = intern ("vertical-line");
11875 staticpro (&Qvertical_line);
11876 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
11877 staticpro (&Qvertical_scroll_bar);
11878 Qmenu_bar = intern ("menu-bar");
11879 staticpro (&Qmenu_bar);
11881 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
11882 Qmouse_fixup_help_message = intern ("mouse-fixup-help-message");
11883 staticpro (&Qmouse_fixup_help_message);
11884 #endif
11886 Qabove_handle = intern ("above-handle");
11887 staticpro (&Qabove_handle);
11888 Qhandle = intern ("handle");
11889 staticpro (&Qhandle);
11890 Qbelow_handle = intern ("below-handle");
11891 staticpro (&Qbelow_handle);
11892 Qup = intern ("up");
11893 staticpro (&Qup);
11894 Qdown = intern ("down");
11895 staticpro (&Qdown);
11896 Qtop = intern ("top");
11897 staticpro (&Qtop);
11898 Qbottom = intern ("bottom");
11899 staticpro (&Qbottom);
11900 Qend_scroll = intern ("end-scroll");
11901 staticpro (&Qend_scroll);
11902 Qratio = intern ("ratio");
11903 staticpro (&Qratio);
11905 Qevent_kind = intern ("event-kind");
11906 staticpro (&Qevent_kind);
11907 Qevent_symbol_elements = intern ("event-symbol-elements");
11908 staticpro (&Qevent_symbol_elements);
11909 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
11910 staticpro (&Qevent_symbol_element_mask);
11911 Qmodifier_cache = intern ("modifier-cache");
11912 staticpro (&Qmodifier_cache);
11914 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
11915 staticpro (&Qrecompute_lucid_menubar);
11916 Qactivate_menubar_hook = intern ("activate-menubar-hook");
11917 staticpro (&Qactivate_menubar_hook);
11919 Qpolling_period = intern ("polling-period");
11920 staticpro (&Qpolling_period);
11922 Qinput_method_function = intern ("input-method-function");
11923 staticpro (&Qinput_method_function);
11925 Qinput_method_exit_on_first_char = intern ("input-method-exit-on-first-char");
11926 staticpro (&Qinput_method_exit_on_first_char);
11927 Qinput_method_use_echo_area = intern ("input-method-use-echo-area");
11928 staticpro (&Qinput_method_use_echo_area);
11930 Fset (Qinput_method_exit_on_first_char, Qnil);
11931 Fset (Qinput_method_use_echo_area, Qnil);
11933 last_point_position_buffer = Qnil;
11934 last_point_position_window = Qnil;
11937 struct event_head *p;
11939 for (p = head_table;
11940 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
11941 p++)
11943 *p->var = intern (p->name);
11944 staticpro (p->var);
11945 Fput (*p->var, Qevent_kind, *p->kind);
11946 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
11950 button_down_location = Fmake_vector (make_number (1), Qnil);
11951 staticpro (&button_down_location);
11952 mouse_syms = Fmake_vector (make_number (1), Qnil);
11953 staticpro (&mouse_syms);
11954 wheel_syms = Fmake_vector (make_number (4), Qnil);
11955 staticpro (&wheel_syms);
11958 int i;
11959 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
11961 modifier_symbols = Fmake_vector (make_number (len), Qnil);
11962 for (i = 0; i < len; i++)
11963 if (modifier_names[i])
11964 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
11965 staticpro (&modifier_symbols);
11968 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
11969 staticpro (&recent_keys);
11971 this_command_keys = Fmake_vector (make_number (40), Qnil);
11972 staticpro (&this_command_keys);
11974 raw_keybuf = Fmake_vector (make_number (30), Qnil);
11975 staticpro (&raw_keybuf);
11977 Qextended_command_history = intern ("extended-command-history");
11978 Fset (Qextended_command_history, Qnil);
11979 staticpro (&Qextended_command_history);
11981 accent_key_syms = Qnil;
11982 staticpro (&accent_key_syms);
11984 func_key_syms = Qnil;
11985 staticpro (&func_key_syms);
11987 drag_n_drop_syms = Qnil;
11988 staticpro (&drag_n_drop_syms);
11990 unread_switch_frame = Qnil;
11991 staticpro (&unread_switch_frame);
11993 internal_last_event_frame = Qnil;
11994 staticpro (&internal_last_event_frame);
11996 read_key_sequence_cmd = Qnil;
11997 staticpro (&read_key_sequence_cmd);
11999 menu_bar_one_keymap_changed_items = Qnil;
12000 staticpro (&menu_bar_one_keymap_changed_items);
12002 menu_bar_items_vector = Qnil;
12003 staticpro (&menu_bar_items_vector);
12005 help_form_saved_window_configs = Qnil;
12006 staticpro (&help_form_saved_window_configs);
12008 defsubr (&Scurrent_idle_time);
12009 defsubr (&Sevent_symbol_parse_modifiers);
12010 defsubr (&Sevent_convert_list);
12011 defsubr (&Sread_key_sequence);
12012 defsubr (&Sread_key_sequence_vector);
12013 defsubr (&Srecursive_edit);
12014 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
12015 defsubr (&Strack_mouse);
12016 #endif
12017 defsubr (&Sinput_pending_p);
12018 defsubr (&Scommand_execute);
12019 defsubr (&Srecent_keys);
12020 defsubr (&Sthis_command_keys);
12021 defsubr (&Sthis_command_keys_vector);
12022 defsubr (&Sthis_single_command_keys);
12023 defsubr (&Sthis_single_command_raw_keys);
12024 defsubr (&Sreset_this_command_lengths);
12025 defsubr (&Sclear_this_command_keys);
12026 defsubr (&Ssuspend_emacs);
12027 defsubr (&Sabort_recursive_edit);
12028 defsubr (&Sexit_recursive_edit);
12029 defsubr (&Srecursion_depth);
12030 defsubr (&Stop_level);
12031 defsubr (&Sdiscard_input);
12032 defsubr (&Sopen_dribble_file);
12033 defsubr (&Sset_input_interrupt_mode);
12034 defsubr (&Sset_output_flow_control);
12035 defsubr (&Sset_input_meta_mode);
12036 defsubr (&Sset_quit_char);
12037 defsubr (&Sset_input_mode);
12038 defsubr (&Scurrent_input_mode);
12039 defsubr (&Sexecute_extended_command);
12040 defsubr (&Sposn_at_point);
12041 defsubr (&Sposn_at_x_y);
12043 DEFVAR_LISP ("last-command-char", &last_command_char,
12044 doc: /* Last input event that was part of a command. */);
12046 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
12047 doc: /* Last input event that was part of a command. */);
12049 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
12050 doc: /* Last input event in a command, except for mouse menu events.
12051 Mouse menus give back keys that don't look like mouse events;
12052 this variable holds the actual mouse event that led to the menu,
12053 so that you can determine whether the command was run by mouse or not. */);
12055 DEFVAR_LISP ("last-input-char", &last_input_char,
12056 doc: /* Last input event. */);
12058 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
12059 doc: /* Last input event. */);
12061 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
12062 doc: /* List of events to be read as the command input.
12063 These events are processed first, before actual keyboard input.
12064 Events read from this list are not normally added to `this-command-keys',
12065 as they will already have been added once as they were read for the first time.
12066 An element of the form (t . EVENT) forces EVENT to be added to that list. */);
12067 Vunread_command_events = Qnil;
12069 DEFVAR_INT ("unread-command-char", &unread_command_char,
12070 doc: /* If not -1, an object to be read as next command input event. */);
12072 DEFVAR_LISP ("unread-post-input-method-events", &Vunread_post_input_method_events,
12073 doc: /* List of events to be processed as input by input methods.
12074 These events are processed before `unread-command-events'
12075 and actual keyboard input, but are not given to `input-method-function'. */);
12076 Vunread_post_input_method_events = Qnil;
12078 DEFVAR_LISP ("unread-input-method-events", &Vunread_input_method_events,
12079 doc: /* List of events to be processed as input by input methods.
12080 These events are processed after `unread-command-events', but
12081 before actual keyboard input.
12082 If there's an active input method, the events are given to
12083 `input-method-function'. */);
12084 Vunread_input_method_events = Qnil;
12086 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
12087 doc: /* Meta-prefix character code.
12088 Meta-foo as command input turns into this character followed by foo. */);
12089 XSETINT (meta_prefix_char, 033);
12091 DEFVAR_KBOARD ("last-command", Vlast_command,
12092 doc: /* The last command executed.
12093 Normally a symbol with a function definition, but can be whatever was found
12094 in the keymap, or whatever the variable `this-command' was set to by that
12095 command.
12097 The value `mode-exit' is special; it means that the previous command
12098 read an event that told it to exit, and it did so and unread that event.
12099 In other words, the present command is the event that made the previous
12100 command exit.
12102 The value `kill-region' is special; it means that the previous command
12103 was a kill command.
12105 `last-command' has a separate binding for each terminal device.
12106 See Info node `(elisp)Multiple displays'. */);
12108 DEFVAR_KBOARD ("real-last-command", Vreal_last_command,
12109 doc: /* Same as `last-command', but never altered by Lisp code. */);
12111 DEFVAR_KBOARD ("last-repeatable-command", Vlast_repeatable_command,
12112 doc: /* Last command that may be repeated.
12113 The last command executed that was not bound to an input event.
12114 This is the command `repeat' will try to repeat. */);
12116 DEFVAR_LISP ("this-command", &Vthis_command,
12117 doc: /* The command now being executed.
12118 The command can set this variable; whatever is put here
12119 will be in `last-command' during the following command. */);
12120 Vthis_command = Qnil;
12122 DEFVAR_LISP ("this-command-keys-shift-translated",
12123 &Vthis_command_keys_shift_translated,
12124 doc: /* Non-nil if the key sequence activating this command was shift-translated.
12125 Shift-translation occurs when there is no binding for the key sequence
12126 as entered, but a binding was found by changing an upper-case letter
12127 to lower-case, or a shifted function key to an unshifted one. */);
12128 Vthis_command_keys_shift_translated = Qnil;
12130 DEFVAR_LISP ("this-original-command", &Vthis_original_command,
12131 doc: /* The command bound to the current key sequence before remapping.
12132 It equals `this-command' if the original command was not remapped through
12133 any of the active keymaps. Otherwise, the value of `this-command' is the
12134 result of looking up the original command in the active keymaps. */);
12135 Vthis_original_command = Qnil;
12137 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
12138 doc: /* *Number of input events between auto-saves.
12139 Zero means disable autosaving due to number of characters typed. */);
12140 auto_save_interval = 300;
12142 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
12143 doc: /* *Number of seconds idle time before auto-save.
12144 Zero or nil means disable auto-saving due to idleness.
12145 After auto-saving due to this many seconds of idle time,
12146 Emacs also does a garbage collection if that seems to be warranted. */);
12147 XSETFASTINT (Vauto_save_timeout, 30);
12149 DEFVAR_LISP ("echo-keystrokes", &Vecho_keystrokes,
12150 doc: /* *Nonzero means echo unfinished commands after this many seconds of pause.
12151 The value may be integer or floating point. */);
12152 Vecho_keystrokes = make_number (1);
12154 DEFVAR_INT ("polling-period", &polling_period,
12155 doc: /* *Interval between polling for input during Lisp execution.
12156 The reason for polling is to make C-g work to stop a running program.
12157 Polling is needed only when using X windows and SIGIO does not work.
12158 Polling is automatically disabled in all other cases. */);
12159 polling_period = 2;
12161 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
12162 doc: /* *Maximum time between mouse clicks to make a double-click.
12163 Measured in milliseconds. The value nil means disable double-click
12164 recognition; t means double-clicks have no time limit and are detected
12165 by position only. */);
12166 Vdouble_click_time = make_number (500);
12168 DEFVAR_INT ("double-click-fuzz", &double_click_fuzz,
12169 doc: /* *Maximum mouse movement between clicks to make a double-click.
12170 On window-system frames, value is the number of pixels the mouse may have
12171 moved horizontally or vertically between two clicks to make a double-click.
12172 On non window-system frames, value is interpreted in units of 1/8 characters
12173 instead of pixels.
12175 This variable is also the threshold for motion of the mouse
12176 to count as a drag. */);
12177 double_click_fuzz = 3;
12179 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
12180 doc: /* *Non-nil means inhibit local map menu bar menus. */);
12181 inhibit_local_menu_bar_menus = 0;
12183 DEFVAR_INT ("num-input-keys", &num_input_keys,
12184 doc: /* Number of complete key sequences read as input so far.
12185 This includes key sequences read from keyboard macros.
12186 The number is effectively the number of interactive command invocations. */);
12187 num_input_keys = 0;
12189 DEFVAR_INT ("num-nonmacro-input-events", &num_nonmacro_input_events,
12190 doc: /* Number of input events read from the keyboard so far.
12191 This does not include events generated by keyboard macros. */);
12192 num_nonmacro_input_events = 0;
12194 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
12195 doc: /* The frame in which the most recently read event occurred.
12196 If the last event came from a keyboard macro, this is set to `macro'. */);
12197 Vlast_event_frame = Qnil;
12199 /* This variable is set up in sysdep.c. */
12200 DEFVAR_LISP ("tty-erase-char", &Vtty_erase_char,
12201 doc: /* The ERASE character as set by the user with stty. */);
12203 DEFVAR_LISP ("help-char", &Vhelp_char,
12204 doc: /* Character to recognize as meaning Help.
12205 When it is read, do `(eval help-form)', and display result if it's a string.
12206 If the value of `help-form' is nil, this char can be read normally. */);
12207 XSETINT (Vhelp_char, Ctl ('H'));
12209 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
12210 doc: /* List of input events to recognize as meaning Help.
12211 These work just like the value of `help-char' (see that). */);
12212 Vhelp_event_list = Qnil;
12214 DEFVAR_LISP ("help-form", &Vhelp_form,
12215 doc: /* Form to execute when character `help-char' is read.
12216 If the form returns a string, that string is displayed.
12217 If `help-form' is nil, the help char is not recognized. */);
12218 Vhelp_form = Qnil;
12220 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
12221 doc: /* Command to run when `help-char' character follows a prefix key.
12222 This command is used only when there is no actual binding
12223 for that character after that prefix key. */);
12224 Vprefix_help_command = Qnil;
12226 DEFVAR_LISP ("top-level", &Vtop_level,
12227 doc: /* Form to evaluate when Emacs starts up.
12228 Useful to set before you dump a modified Emacs. */);
12229 Vtop_level = Qnil;
12231 DEFVAR_KBOARD ("keyboard-translate-table", Vkeyboard_translate_table,
12232 doc: /* Translate table for local keyboard input, or nil.
12233 If non-nil, the value should be a char-table. Each character read
12234 from the keyboard is looked up in this char-table. If the value found
12235 there is non-nil, then it is used instead of the actual input character.
12237 The value can also be a string or vector, but this is considered obsolete.
12238 If it is a string or vector of length N, character codes N and up are left
12239 untranslated. In a vector, an element which is nil means "no translation".
12241 This is applied to the characters supplied to input methods, not their
12242 output. See also `translation-table-for-input'.
12244 This variable has a separate binding for each terminal. See Info node
12245 `(elisp)Multiple displays'. */);
12247 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
12248 doc: /* Non-nil means to always spawn a subshell instead of suspending.
12249 \(Even if the operating system has support for stopping a process.\) */);
12250 cannot_suspend = 0;
12252 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
12253 doc: /* Non-nil means prompt with menus when appropriate.
12254 This is done when reading from a keymap that has a prompt string,
12255 for elements that have prompt strings.
12256 The menu is displayed on the screen
12257 if X menus were enabled at configuration
12258 time and the previous event was a mouse click prefix key.
12259 Otherwise, menu prompting uses the echo area. */);
12260 menu_prompting = 1;
12262 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
12263 doc: /* Character to see next line of menu prompt.
12264 Type this character while in a menu prompt to rotate around the lines of it. */);
12265 XSETINT (menu_prompt_more_char, ' ');
12267 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
12268 doc: /* A mask of additional modifier keys to use with every keyboard character.
12269 Emacs applies the modifiers of the character stored here to each keyboard
12270 character it reads. For example, after evaluating the expression
12271 (setq extra-keyboard-modifiers ?\\C-x)
12272 all input characters will have the control modifier applied to them.
12274 Note that the character ?\\C-@, equivalent to the integer zero, does
12275 not count as a control character; rather, it counts as a character
12276 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero
12277 cancels any modification. */);
12278 extra_keyboard_modifiers = 0;
12280 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
12281 doc: /* If an editing command sets this to t, deactivate the mark afterward.
12282 The command loop sets this to nil before each command,
12283 and tests the value when the command returns.
12284 Buffer modification stores t in this variable. */);
12285 Vdeactivate_mark = Qnil;
12287 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
12288 doc: /* Temporary storage of pre-command-hook or post-command-hook. */);
12289 Vcommand_hook_internal = Qnil;
12291 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
12292 doc: /* Normal hook run before each command is executed.
12293 If an unhandled error happens in running this hook,
12294 the hook value is set to nil, since otherwise the error
12295 might happen repeatedly and make Emacs nonfunctional. */);
12296 Vpre_command_hook = Qnil;
12298 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
12299 doc: /* Normal hook run after each command is executed.
12300 If an unhandled error happens in running this hook,
12301 the hook value is set to nil, since otherwise the error
12302 might happen repeatedly and make Emacs nonfunctional. */);
12303 Vpost_command_hook = Qnil;
12305 #if 0
12306 DEFVAR_LISP ("echo-area-clear-hook", ...,
12307 doc: /* Normal hook run when clearing the echo area. */);
12308 #endif
12309 Qecho_area_clear_hook = intern ("echo-area-clear-hook");
12310 staticpro (&Qecho_area_clear_hook);
12311 SET_SYMBOL_VALUE (Qecho_area_clear_hook, Qnil);
12313 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
12314 doc: /* Non-nil means menu bar, specified Lucid style, needs to be recomputed. */);
12315 Vlucid_menu_bar_dirty_flag = Qnil;
12317 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
12318 doc: /* List of menu bar items to move to the end of the menu bar.
12319 The elements of the list are event types that may have menu bar bindings. */);
12320 Vmenu_bar_final_items = Qnil;
12322 DEFVAR_KBOARD ("overriding-terminal-local-map",
12323 Voverriding_terminal_local_map,
12324 doc: /* Per-terminal keymap that overrides all other local keymaps.
12325 If this variable is non-nil, it is used as a keymap instead of the
12326 buffer's local map, and the minor mode keymaps and text property keymaps.
12327 It also replaces `overriding-local-map'.
12329 This variable is intended to let commands such as `universal-argument'
12330 set up a different keymap for reading the next command.
12332 `overriding-terminal-local-map' has a separate binding for each
12333 terminal device.
12334 See Info node `(elisp)Multiple displays'. */);
12336 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
12337 doc: /* Keymap that overrides all other local keymaps.
12338 If this variable is non-nil, it is used as a keymap--replacing the
12339 buffer's local map, the minor mode keymaps, and char property keymaps. */);
12340 Voverriding_local_map = Qnil;
12342 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
12343 doc: /* Non-nil means `overriding-local-map' applies to the menu bar.
12344 Otherwise, the menu bar continues to reflect the buffer's local map
12345 and the minor mode maps regardless of `overriding-local-map'. */);
12346 Voverriding_local_map_menu_flag = Qnil;
12348 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
12349 doc: /* Keymap defining bindings for special events to execute at low level. */);
12350 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
12352 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
12353 doc: /* *Non-nil means generate motion events for mouse motion. */);
12355 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
12356 doc: /* Alist of system-specific X windows key symbols.
12357 Each element should have the form (N . SYMBOL) where N is the
12358 numeric keysym code (sans the \"system-specific\" bit 1<<28)
12359 and SYMBOL is its name.
12361 `system-key-alist' has a separate binding for each terminal device.
12362 See Info node `(elisp)Multiple displays'. */);
12364 DEFVAR_KBOARD ("local-function-key-map", Vlocal_function_key_map,
12365 doc: /* Keymap that translates key sequences to key sequences during input.
12366 This is used mainly for mapping key sequences into some preferred
12367 key events (symbols).
12369 The `read-key-sequence' function replaces any subsequence bound by
12370 `local-function-key-map' with its binding. More precisely, when the
12371 active keymaps have no binding for the current key sequence but
12372 `local-function-key-map' binds a suffix of the sequence to a vector or
12373 string, `read-key-sequence' replaces the matching suffix with its
12374 binding, and continues with the new sequence.
12376 If the binding is a function, it is called with one argument (the prompt)
12377 and its return value (a key sequence) is used.
12379 The events that come from bindings in `local-function-key-map' are not
12380 themselves looked up in `local-function-key-map'.
12382 For example, suppose `local-function-key-map' binds `ESC O P' to [f1].
12383 Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing
12384 `C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix key,
12385 typing `ESC O P x' would return [f1 x].
12387 `local-function-key-map' has a separate binding for each terminal
12388 device. See Info node `(elisp)Multiple displays'. If you need to
12389 define a binding on all terminals, change `function-key-map'
12390 instead. Initially, `local-function-key-map' is an empty keymap that
12391 has `function-key-map' as its parent on all terminal devices. */);
12393 DEFVAR_KBOARD ("input-decode-map", Vinput_decode_map,
12394 doc: /* Keymap that decodes input escape sequences.
12395 This is used mainly for mapping ASCII function key sequences into
12396 real Emacs function key events (symbols).
12398 The `read-key-sequence' function replaces any subsequence bound by
12399 `local-function-key-map' with its binding. Contrary to `function-key-map',
12400 this map applies its rebinding regardless of the presence of an ordinary
12401 binding. So it is more like `key-translation-map' except that it applies
12402 before `function-key-map' rather than after.
12404 If the binding is a function, it is called with one argument (the prompt)
12405 and its return value (a key sequence) is used.
12407 The events that come from bindings in `input-decode-map' are not
12408 themselves looked up in `input-decode-map'.
12410 This variable is keyboard-local. */);
12412 DEFVAR_LISP ("function-key-map", &Vfunction_key_map,
12413 doc: /* The parent keymap of all `local-function-key-map' instances.
12414 Function key definitions that apply to all terminal devices should go
12415 here. If a mapping is defined in both the current
12416 `local-function-key-map' binding and this variable, then the local
12417 definition will take precendence. */);
12418 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
12420 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
12421 doc: /* Keymap of key translations that can override keymaps.
12422 This keymap works like `function-key-map', but comes after that,
12423 and its non-prefix bindings override ordinary bindings.
12424 Another difference is that it is global rather than keyboard-local. */);
12425 Vkey_translation_map = Fmake_sparse_keymap (Qnil);
12427 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
12428 doc: /* List of deferred actions to be performed at a later time.
12429 The precise format isn't relevant here; we just check whether it is nil. */);
12430 Vdeferred_action_list = Qnil;
12432 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
12433 doc: /* Function to call to handle deferred actions, after each command.
12434 This function is called with no arguments after each command
12435 whenever `deferred-action-list' is non-nil. */);
12436 Vdeferred_action_function = Qnil;
12438 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
12439 doc: /* *Non-nil means show the equivalent key-binding when M-x command has one.
12440 The value can be a length of time to show the message for.
12441 If the value is non-nil and not a number, we wait 2 seconds. */);
12442 Vsuggest_key_bindings = Qt;
12444 DEFVAR_LISP ("timer-list", &Vtimer_list,
12445 doc: /* List of active absolute time timers in order of increasing time. */);
12446 Vtimer_list = Qnil;
12448 DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
12449 doc: /* List of active idle-time timers in order of increasing time. */);
12450 Vtimer_idle_list = Qnil;
12452 DEFVAR_LISP ("input-method-function", &Vinput_method_function,
12453 doc: /* If non-nil, the function that implements the current input method.
12454 It's called with one argument, a printing character that was just read.
12455 \(That means a character with code 040...0176.)
12456 Typically this function uses `read-event' to read additional events.
12457 When it does so, it should first bind `input-method-function' to nil
12458 so it will not be called recursively.
12460 The function should return a list of zero or more events
12461 to be used as input. If it wants to put back some events
12462 to be reconsidered, separately, by the input method,
12463 it can add them to the beginning of `unread-command-events'.
12465 The input method function can find in `input-method-previous-method'
12466 the previous echo area message.
12468 The input method function should refer to the variables
12469 `input-method-use-echo-area' and `input-method-exit-on-first-char'
12470 for guidance on what to do. */);
12471 Vinput_method_function = Qnil;
12473 DEFVAR_LISP ("input-method-previous-message",
12474 &Vinput_method_previous_message,
12475 doc: /* When `input-method-function' is called, hold the previous echo area message.
12476 This variable exists because `read-event' clears the echo area
12477 before running the input method. It is nil if there was no message. */);
12478 Vinput_method_previous_message = Qnil;
12480 DEFVAR_LISP ("show-help-function", &Vshow_help_function,
12481 doc: /* If non-nil, the function that implements the display of help.
12482 It's called with one argument, the help string to display. */);
12483 Vshow_help_function = Qnil;
12485 DEFVAR_LISP ("disable-point-adjustment", &Vdisable_point_adjustment,
12486 doc: /* If non-nil, suppress point adjustment after executing a command.
12488 After a command is executed, if point is moved into a region that has
12489 special properties (e.g. composition, display), we adjust point to
12490 the boundary of the region. But, when a command sets this variable to
12491 non-nil, we suppress the point adjustment.
12493 This variable is set to nil before reading a command, and is checked
12494 just after executing the command. */);
12495 Vdisable_point_adjustment = Qnil;
12497 DEFVAR_LISP ("global-disable-point-adjustment",
12498 &Vglobal_disable_point_adjustment,
12499 doc: /* *If non-nil, always suppress point adjustment.
12501 The default value is nil, in which case, point adjustment are
12502 suppressed only after special commands that set
12503 `disable-point-adjustment' (which see) to non-nil. */);
12504 Vglobal_disable_point_adjustment = Qnil;
12506 DEFVAR_LISP ("minibuffer-message-timeout", &Vminibuffer_message_timeout,
12507 doc: /* *How long to display an echo-area message when the minibuffer is active.
12508 If the value is not a number, such messages don't time out. */);
12509 Vminibuffer_message_timeout = make_number (2);
12511 DEFVAR_LISP ("throw-on-input", &Vthrow_on_input,
12512 doc: /* If non-nil, any keyboard input throws to this symbol.
12513 The value of that variable is passed to `quit-flag' and later causes a
12514 peculiar kind of quitting. */);
12515 Vthrow_on_input = Qnil;
12517 DEFVAR_LISP ("command-error-function", &Vcommand_error_function,
12518 doc: /* If non-nil, function to output error messages.
12519 The arguments are the error data, a list of the form
12520 (SIGNALED-CONDITIONS . SIGNAL-DATA)
12521 such as just as `condition-case' would bind its variable to,
12522 the context (a string which normally goes at the start of the message),
12523 and the Lisp function within which the error was signaled. */);
12524 Vcommand_error_function = Qnil;
12526 DEFVAR_LISP ("enable-disabled-menus-and-buttons",
12527 &Venable_disabled_menus_and_buttons,
12528 doc: /* If non-nil, don't ignore events produced by disabled menu items and tool-bar.
12530 Help functions bind this to allow help on disabled menu items
12531 and tool-bar buttons. */);
12532 Venable_disabled_menus_and_buttons = Qnil;
12534 #ifdef MULTI_KBOARD
12535 /* Create the initial keyboard. */
12536 initial_kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
12537 init_kboard (initial_kboard);
12538 /* Vwindow_system is left at t for now. */
12539 initial_kboard->next_kboard = all_kboards;
12540 all_kboards = initial_kboard;
12541 #endif
12544 void
12545 keys_of_keyboard ()
12547 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
12548 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
12549 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
12550 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
12551 initial_define_key (meta_map, 'x', "execute-extended-command");
12553 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
12554 "handle-delete-frame");
12555 /* Here we used to use `ignore-event' which would simple set prefix-arg to
12556 current-prefix-arg, as is done in `handle-switch-frame'.
12557 But `handle-switch-frame is not run from the special-map.
12558 Commands from that map are run in a special way that automatically
12559 preserves the prefix-arg. Restoring the prefix arg here is not just
12560 redundant but harmful:
12561 - C-u C-x v =
12562 - current-prefix-arg is set to non-nil, prefix-arg is set to nil.
12563 - after the first prompt, the exit-minibuffer-hook is run which may
12564 iconify a frame and thus push a `iconify-frame' event.
12565 - after running exit-minibuffer-hook, current-prefix-arg is
12566 restored to the non-nil value it had before the prompt.
12567 - we enter the second prompt.
12568 current-prefix-arg is non-nil, prefix-arg is nil.
12569 - before running the first real event, we run the special iconify-frame
12570 event, but we pass the `special' arg to execute-command so
12571 current-prefix-arg and prefix-arg are left untouched.
12572 - here we foolishly copy the non-nil current-prefix-arg to prefix-arg.
12573 - the next key event will have a spuriously non-nil current-prefix-arg. */
12574 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
12575 "ignore");
12576 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
12577 "ignore");
12578 /* Handling it at such a low-level causes read_key_sequence to get
12579 * confused because it doesn't realize that the current_buffer was
12580 * changed by read_char.
12582 * initial_define_lispy_key (Vspecial_event_map, "select-window",
12583 * "handle-select-window"); */
12584 initial_define_lispy_key (Vspecial_event_map, "save-session",
12585 "handle-save-session");
12587 #ifdef HAVE_DBUS
12588 /* Define a special event which is raised for dbus callback
12589 functions. */
12590 initial_define_lispy_key (Vspecial_event_map, "dbus-event",
12591 "dbus-handle-event");
12592 #endif
12595 /* Mark the pointers in the kboard objects.
12596 Called by the Fgarbage_collector. */
12597 void
12598 mark_kboards ()
12600 KBOARD *kb;
12601 Lisp_Object *p;
12602 for (kb = all_kboards; kb; kb = kb->next_kboard)
12604 if (kb->kbd_macro_buffer)
12605 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
12606 mark_object (*p);
12607 mark_object (kb->Voverriding_terminal_local_map);
12608 mark_object (kb->Vlast_command);
12609 mark_object (kb->Vreal_last_command);
12610 mark_object (kb->Vkeyboard_translate_table);
12611 mark_object (kb->Vlast_repeatable_command);
12612 mark_object (kb->Vprefix_arg);
12613 mark_object (kb->Vlast_prefix_arg);
12614 mark_object (kb->kbd_queue);
12615 mark_object (kb->defining_kbd_macro);
12616 mark_object (kb->Vlast_kbd_macro);
12617 mark_object (kb->Vsystem_key_alist);
12618 mark_object (kb->system_key_syms);
12619 mark_object (kb->Vwindow_system);
12620 mark_object (kb->Vinput_decode_map);
12621 mark_object (kb->Vlocal_function_key_map);
12622 mark_object (kb->Vdefault_minibuffer_frame);
12623 mark_object (kb->echo_string);
12626 struct input_event *event;
12627 for (event = kbd_fetch_ptr; event != kbd_store_ptr; event++)
12629 if (event == kbd_buffer + KBD_BUFFER_SIZE)
12630 event = kbd_buffer;
12631 if (event->kind != SELECTION_REQUEST_EVENT
12632 && event->kind != SELECTION_CLEAR_EVENT)
12634 mark_object (event->x);
12635 mark_object (event->y);
12637 mark_object (event->frame_or_window);
12638 mark_object (event->arg);
12643 /* arch-tag: 774e34d7-6d31-42f3-8397-e079a4e4c9ca
12644 (do not change this comment) */