Merge from trunk
[emacs.git] / src / keyboard.c
blob63e7573fbe9c09ba40e0d58549e6b2c691f04975
1 /* Keyboard and mouse input; editor command loop.
3 Copyright (C) 1985-1989, 1993-1997, 1999-2011 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <setjmp.h>
24 #include "lisp.h"
25 #include "termchar.h"
26 #include "termopts.h"
27 #include "frame.h"
28 #include "termhooks.h"
29 #include "macros.h"
30 #include "keyboard.h"
31 #include "window.h"
32 #include "commands.h"
33 #include "buffer.h"
34 #include "character.h"
35 #include "disptab.h"
36 #include "dispextern.h"
37 #include "syntax.h"
38 #include "intervals.h"
39 #include "keymap.h"
40 #include "blockinput.h"
41 #include "puresize.h"
42 #include "systime.h"
43 #include "atimer.h"
44 #include "process.h"
45 #include <errno.h>
47 #ifdef HAVE_GTK_AND_PTHREAD
48 #include <pthread.h>
49 #endif
50 #ifdef MSDOS
51 #include "msdos.h"
52 #include <time.h>
53 #else /* not MSDOS */
54 #include <sys/ioctl.h>
55 #endif /* not MSDOS */
57 #include "syssignal.h"
59 #include <sys/types.h>
60 #include <unistd.h>
61 #include <fcntl.h>
63 /* This is to get the definitions of the XK_ symbols. */
64 #ifdef HAVE_X_WINDOWS
65 #include "xterm.h"
66 #endif
68 #ifdef HAVE_NTGUI
69 #include "w32term.h"
70 #endif /* HAVE_NTGUI */
72 #ifdef HAVE_NS
73 #include "nsterm.h"
74 #endif
76 /* Variables for blockinput.h: */
78 /* Non-zero if interrupt input is blocked right now. */
79 volatile int interrupt_input_blocked;
81 /* Nonzero means an input interrupt has arrived
82 during the current critical section. */
83 int interrupt_input_pending;
85 /* This var should be (interrupt_input_pending || pending_atimers).
86 The QUIT macro checks this instead of interrupt_input_pending and
87 pending_atimers separately, to reduce code size. So, any code that
88 changes interrupt_input_pending or pending_atimers should update
89 this too. */
90 #ifdef SYNC_INPUT
91 int pending_signals;
92 #endif
94 #define KBD_BUFFER_SIZE 4096
96 KBOARD *initial_kboard;
97 KBOARD *current_kboard;
98 KBOARD *all_kboards;
99 int single_kboard;
101 /* Non-nil disable property on a command means
102 do not execute it; call disabled-command-function's value instead. */
103 Lisp_Object Qdisabled, Qdisabled_command_function;
105 #define NUM_RECENT_KEYS (300)
106 int recent_keys_index; /* Index for storing next element into recent_keys */
107 int total_keys; /* Total number of elements stored into recent_keys */
108 Lisp_Object recent_keys; /* Vector holds the last NUM_RECENT_KEYS keystrokes */
110 /* Vector holding the key sequence that invoked the current command.
111 It is reused for each command, and it may be longer than the current
112 sequence; this_command_key_count indicates how many elements
113 actually mean something.
114 It's easier to staticpro a single Lisp_Object than an array. */
115 Lisp_Object this_command_keys;
116 int this_command_key_count;
118 /* 1 after calling Freset_this_command_lengths.
119 Usually it is 0. */
120 int this_command_key_count_reset;
122 /* This vector is used as a buffer to record the events that were actually read
123 by read_key_sequence. */
124 Lisp_Object raw_keybuf;
125 int raw_keybuf_count;
127 #define GROW_RAW_KEYBUF \
128 if (raw_keybuf_count == XVECTOR (raw_keybuf)->size) \
129 raw_keybuf = larger_vector (raw_keybuf, raw_keybuf_count * 2, Qnil) \
131 /* Number of elements of this_command_keys
132 that precede this key sequence. */
133 int this_single_command_key_start;
135 /* Record values of this_command_key_count and echo_length ()
136 before this command was read. */
137 static int before_command_key_count;
138 static int before_command_echo_length;
140 /* For longjmp to where kbd input is being done. */
142 static jmp_buf getcjmp;
144 /* True while doing kbd input. */
145 int waiting_for_input;
147 /* True while displaying for echoing. Delays C-g throwing. */
149 int echoing;
151 /* Non-null means we can start echoing at the next input pause even
152 though there is something in the echo area. */
154 static struct kboard *ok_to_echo_at_next_pause;
156 /* The kboard last echoing, or null for none. Reset to 0 in
157 cancel_echoing. If non-null, and a current echo area message
158 exists, and echo_message_buffer is eq to the current message
159 buffer, we know that the message comes from echo_kboard. */
161 struct kboard *echo_kboard;
163 /* The buffer used for echoing. Set in echo_now, reset in
164 cancel_echoing. */
166 Lisp_Object echo_message_buffer;
168 /* Nonzero means C-g should cause immediate error-signal. */
169 int immediate_quit;
171 /* Character that causes a quit. Normally C-g.
173 If we are running on an ordinary terminal, this must be an ordinary
174 ASCII char, since we want to make it our interrupt character.
176 If we are not running on an ordinary terminal, it still needs to be
177 an ordinary ASCII char. This character needs to be recognized in
178 the input interrupt handler. At this point, the keystroke is
179 represented as a struct input_event, while the desired quit
180 character is specified as a lispy event. The mapping from struct
181 input_events to lispy events cannot run in an interrupt handler,
182 and the reverse mapping is difficult for anything but ASCII
183 keystrokes.
185 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
186 ASCII character. */
187 int quit_char;
189 /* Current depth in recursive edits. */
190 int command_loop_level;
192 /* If not Qnil, this is a switch-frame event which we decided to put
193 off until the end of a key sequence. This should be read as the
194 next command input, after any unread_command_events.
196 read_key_sequence uses this to delay switch-frame events until the
197 end of the key sequence; Fread_char uses it to put off switch-frame
198 events until a non-ASCII event is acceptable as input. */
199 Lisp_Object unread_switch_frame;
201 /* Last size recorded for a current buffer which is not a minibuffer. */
202 static EMACS_INT last_non_minibuf_size;
204 /* Total number of times read_char has returned. */
205 int num_input_events;
207 /* Value of num_nonmacro_input_events as of last auto save. */
209 int last_auto_save;
211 /* This is like Vthis_command, except that commands never set it. */
212 Lisp_Object real_this_command;
214 /* The value of point when the last command was started. */
215 EMACS_INT last_point_position;
217 /* The buffer that was current when the last command was started. */
218 Lisp_Object last_point_position_buffer;
220 /* The window that was selected when the last command was started. */
221 Lisp_Object last_point_position_window;
223 /* The frame in which the last input event occurred, or Qmacro if the
224 last event came from a macro. We use this to determine when to
225 generate switch-frame events. This may be cleared by functions
226 like Fselect_frame, to make sure that a switch-frame event is
227 generated by the next character. */
228 Lisp_Object internal_last_event_frame;
230 /* The timestamp of the last input event we received from the X server.
231 X Windows wants this for selection ownership. */
232 unsigned long last_event_timestamp;
234 Lisp_Object Qx_set_selection, QPRIMARY, Qhandle_switch_frame;
236 Lisp_Object Qself_insert_command;
237 Lisp_Object Qforward_char;
238 Lisp_Object Qbackward_char;
239 Lisp_Object Qundefined;
240 Lisp_Object Qtimer_event_handler;
242 /* read_key_sequence stores here the command definition of the
243 key sequence that it reads. */
244 Lisp_Object read_key_sequence_cmd;
246 Lisp_Object Qinput_method_function;
248 Lisp_Object Qdeactivate_mark;
250 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
252 Lisp_Object Qecho_area_clear_hook;
254 /* Hooks to run before and after each command. */
255 Lisp_Object Qpre_command_hook;
256 Lisp_Object Qpost_command_hook;
257 Lisp_Object Qcommand_hook_internal;
259 Lisp_Object Qdeferred_action_function;
261 Lisp_Object Qinput_method_exit_on_first_char;
262 Lisp_Object Qinput_method_use_echo_area;
264 /* File in which we write all commands we read. */
265 FILE *dribble;
267 /* Nonzero if input is available. */
268 int input_pending;
270 /* Circular buffer for pre-read keyboard input. */
272 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
274 /* Pointer to next available character in kbd_buffer.
275 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
276 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the
277 next available char is in kbd_buffer[0]. */
278 static struct input_event *kbd_fetch_ptr;
280 /* Pointer to next place to store character in kbd_buffer. This
281 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
282 character should go in kbd_buffer[0]. */
283 static struct input_event * volatile kbd_store_ptr;
285 /* The above pair of variables forms a "queue empty" flag. When we
286 enqueue a non-hook event, we increment kbd_store_ptr. When we
287 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
288 there is input available if the two pointers are not equal.
290 Why not just have a flag set and cleared by the enqueuing and
291 dequeuing functions? Such a flag could be screwed up by interrupts
292 at inopportune times. */
294 /* Symbols to head events. */
295 Lisp_Object Qmouse_movement;
296 Lisp_Object Qscroll_bar_movement;
297 Lisp_Object Qswitch_frame;
298 Lisp_Object Qdelete_frame;
299 Lisp_Object Qiconify_frame;
300 Lisp_Object Qmake_frame_visible;
301 Lisp_Object Qselect_window;
302 Lisp_Object Qhelp_echo;
304 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
305 Lisp_Object Qmouse_fixup_help_message;
306 #endif
308 /* Symbols to denote kinds of events. */
309 Lisp_Object Qfunction_key;
310 Lisp_Object Qmouse_click;
311 #if defined (WINDOWSNT)
312 Lisp_Object Qlanguage_change;
313 #endif
314 Lisp_Object Qdrag_n_drop;
315 Lisp_Object Qsave_session;
316 #ifdef HAVE_DBUS
317 Lisp_Object Qdbus_event;
318 #endif
319 Lisp_Object Qconfig_changed_event;
321 /* Lisp_Object Qmouse_movement; - also an event header */
323 /* Properties of event headers. */
324 Lisp_Object Qevent_kind;
325 Lisp_Object Qevent_symbol_elements;
327 /* menu and tool bar item parts */
328 Lisp_Object Qmenu_enable;
329 Lisp_Object QCenable, QCvisible, QChelp, QCfilter, QCkeys, QCkey_sequence;
330 Lisp_Object QCbutton, QCtoggle, QCradio, QClabel, QCvert_only;
332 /* An event header symbol HEAD may have a property named
333 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
334 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
335 mask of modifiers applied to it. If present, this is used to help
336 speed up parse_modifiers. */
337 Lisp_Object Qevent_symbol_element_mask;
339 /* An unmodified event header BASE may have a property named
340 Qmodifier_cache, which is an alist mapping modifier masks onto
341 modified versions of BASE. If present, this helps speed up
342 apply_modifiers. */
343 Lisp_Object Qmodifier_cache;
345 /* Symbols to use for parts of windows. */
346 Lisp_Object Qmode_line;
347 Lisp_Object Qvertical_line;
348 Lisp_Object Qvertical_scroll_bar;
349 Lisp_Object Qmenu_bar;
351 Lisp_Object recursive_edit_unwind (Lisp_Object buffer), command_loop (void);
352 Lisp_Object Fthis_command_keys (void);
353 Lisp_Object Qextended_command_history;
354 EMACS_TIME timer_check (int do_it_now);
356 static void record_menu_key (Lisp_Object c);
357 static int echo_length (void);
359 Lisp_Object Qpolling_period;
361 /* Incremented whenever a timer is run. */
362 int timers_run;
364 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
365 happens. */
366 EMACS_TIME *input_available_clear_time;
368 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
369 Default is 1 if INTERRUPT_INPUT is defined. */
370 int interrupt_input;
372 /* Nonzero while interrupts are temporarily deferred during redisplay. */
373 int interrupts_deferred;
375 /* Allow m- file to inhibit use of FIONREAD. */
376 #ifdef BROKEN_FIONREAD
377 #undef FIONREAD
378 #endif
380 /* We are unable to use interrupts if FIONREAD is not available,
381 so flush SIGIO so we won't try. */
382 #if !defined (FIONREAD)
383 #ifdef SIGIO
384 #undef SIGIO
385 #endif
386 #endif
388 /* If we support a window system, turn on the code to poll periodically
389 to detect C-g. It isn't actually used when doing interrupt input. */
390 #if defined(HAVE_WINDOW_SYSTEM) && !defined(USE_ASYNC_EVENTS)
391 #define POLL_FOR_INPUT
392 #endif
394 /* The time when Emacs started being idle. */
396 static EMACS_TIME timer_idleness_start_time;
398 /* After Emacs stops being idle, this saves the last value
399 of timer_idleness_start_time from when it was idle. */
401 static EMACS_TIME timer_last_idleness_start_time;
404 /* Global variable declarations. */
406 /* Flags for readable_events. */
407 #define READABLE_EVENTS_DO_TIMERS_NOW (1 << 0)
408 #define READABLE_EVENTS_FILTER_EVENTS (1 << 1)
409 #define READABLE_EVENTS_IGNORE_SQUEEZABLES (1 << 2)
411 /* Function for init_keyboard to call with no args (if nonzero). */
412 void (*keyboard_init_hook) (void);
414 static int read_avail_input (int);
415 static void get_input_pending (int *, int);
416 static int readable_events (int);
417 static Lisp_Object read_char_x_menu_prompt (int, Lisp_Object *,
418 Lisp_Object, int *);
419 static Lisp_Object read_char_minibuf_menu_prompt (int, int,
420 Lisp_Object *);
421 static Lisp_Object make_lispy_event (struct input_event *);
422 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
423 static Lisp_Object make_lispy_movement (struct frame *, Lisp_Object,
424 enum scroll_bar_part,
425 Lisp_Object, Lisp_Object,
426 unsigned long);
427 #endif
428 static Lisp_Object modify_event_symbol (EMACS_INT, unsigned, Lisp_Object,
429 Lisp_Object, const char *const *,
430 Lisp_Object *, unsigned);
431 static Lisp_Object make_lispy_switch_frame (Lisp_Object);
432 static void save_getcjmp (jmp_buf);
433 static void restore_getcjmp (jmp_buf);
434 static Lisp_Object apply_modifiers (int, Lisp_Object);
435 static void clear_event (struct input_event *);
436 static Lisp_Object restore_kboard_configuration (Lisp_Object);
437 static SIGTYPE interrupt_signal (int signalnum);
438 #ifdef SIGIO
439 static SIGTYPE input_available_signal (int signo);
440 #endif
441 static void handle_interrupt (void);
442 static void timer_start_idle (void);
443 static void timer_stop_idle (void);
444 static void timer_resume_idle (void);
445 static SIGTYPE handle_user_signal (int);
446 static char *find_user_signal_name (int);
447 static int store_user_signal_events (void);
450 /* Add C to the echo string, if echoing is going on.
451 C can be a character, which is printed prettily ("M-C-x" and all that
452 jazz), or a symbol, whose name is printed. */
454 static void
455 echo_char (Lisp_Object c)
457 if (current_kboard->immediate_echo)
459 int size = KEY_DESCRIPTION_SIZE + 100;
460 char *buffer = (char *) alloca (size);
461 char *ptr = buffer;
462 Lisp_Object echo_string;
464 echo_string = KVAR (current_kboard, echo_string);
466 /* If someone has passed us a composite event, use its head symbol. */
467 c = EVENT_HEAD (c);
469 if (INTEGERP (c))
471 ptr = push_key_description (XINT (c), ptr, 1);
473 else if (SYMBOLP (c))
475 Lisp_Object name = SYMBOL_NAME (c);
476 int nbytes = SBYTES (name);
478 if (size - (ptr - buffer) < nbytes)
480 int offset = ptr - buffer;
481 size = max (2 * size, size + nbytes);
482 buffer = (char *) alloca (size);
483 ptr = buffer + offset;
486 ptr += copy_text (SDATA (name), (unsigned char *) ptr, nbytes,
487 STRING_MULTIBYTE (name), 1);
490 if ((NILP (echo_string) || SCHARS (echo_string) == 0)
491 && help_char_p (c))
493 const char *text = " (Type ? for further options)";
494 int len = strlen (text);
496 if (size - (ptr - buffer) < len)
498 int offset = ptr - buffer;
499 size += len;
500 buffer = (char *) alloca (size);
501 ptr = buffer + offset;
504 memcpy (ptr, text, len);
505 ptr += len;
508 /* Replace a dash from echo_dash with a space, otherwise
509 add a space at the end as a separator between keys. */
510 if (STRINGP (echo_string)
511 && SCHARS (echo_string) > 1)
513 Lisp_Object last_char, prev_char, idx;
515 idx = make_number (SCHARS (echo_string) - 2);
516 prev_char = Faref (echo_string, idx);
518 idx = make_number (SCHARS (echo_string) - 1);
519 last_char = Faref (echo_string, idx);
521 /* We test PREV_CHAR to make sure this isn't the echoing
522 of a minus-sign. */
523 if (XINT (last_char) == '-' && XINT (prev_char) != ' ')
524 Faset (echo_string, idx, make_number (' '));
525 else
526 echo_string = concat2 (echo_string, build_string (" "));
528 else if (STRINGP (echo_string))
529 echo_string = concat2 (echo_string, build_string (" "));
531 KVAR (current_kboard, echo_string)
532 = concat2 (echo_string, make_string (buffer, ptr - buffer));
534 echo_now ();
538 /* Temporarily add a dash to the end of the echo string if it's not
539 empty, so that it serves as a mini-prompt for the very next character. */
541 static void
542 echo_dash (void)
544 /* Do nothing if not echoing at all. */
545 if (NILP (KVAR (current_kboard, echo_string)))
546 return;
548 if (this_command_key_count == 0)
549 return;
551 if (!current_kboard->immediate_echo
552 && SCHARS (KVAR (current_kboard, echo_string)) == 0)
553 return;
555 /* Do nothing if we just printed a prompt. */
556 if (current_kboard->echo_after_prompt
557 == SCHARS (KVAR (current_kboard, echo_string)))
558 return;
560 /* Do nothing if we have already put a dash at the end. */
561 if (SCHARS (KVAR (current_kboard, echo_string)) > 1)
563 Lisp_Object last_char, prev_char, idx;
565 idx = make_number (SCHARS (KVAR (current_kboard, echo_string)) - 2);
566 prev_char = Faref (KVAR (current_kboard, echo_string), idx);
568 idx = make_number (SCHARS (KVAR (current_kboard, echo_string)) - 1);
569 last_char = Faref (KVAR (current_kboard, echo_string), idx);
571 if (XINT (last_char) == '-' && XINT (prev_char) != ' ')
572 return;
575 /* Put a dash at the end of the buffer temporarily,
576 but make it go away when the next character is added. */
577 KVAR (current_kboard, echo_string) = concat2 (KVAR (current_kboard, echo_string),
578 build_string ("-"));
579 echo_now ();
582 /* Display the current echo string, and begin echoing if not already
583 doing so. */
585 void
586 echo_now (void)
588 if (!current_kboard->immediate_echo)
590 int i;
591 current_kboard->immediate_echo = 1;
593 for (i = 0; i < this_command_key_count; i++)
595 Lisp_Object c;
597 /* Set before_command_echo_length to the value that would
598 have been saved before the start of this subcommand in
599 command_loop_1, if we had already been echoing then. */
600 if (i == this_single_command_key_start)
601 before_command_echo_length = echo_length ();
603 c = XVECTOR (this_command_keys)->contents[i];
604 if (! (EVENT_HAS_PARAMETERS (c)
605 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
606 echo_char (c);
609 /* Set before_command_echo_length to the value that would
610 have been saved before the start of this subcommand in
611 command_loop_1, if we had already been echoing then. */
612 if (this_command_key_count == this_single_command_key_start)
613 before_command_echo_length = echo_length ();
615 /* Put a dash at the end to invite the user to type more. */
616 echo_dash ();
619 echoing = 1;
620 message3_nolog (KVAR (current_kboard, echo_string),
621 SBYTES (KVAR (current_kboard, echo_string)),
622 STRING_MULTIBYTE (KVAR (current_kboard, echo_string)));
623 echoing = 0;
625 /* Record in what buffer we echoed, and from which kboard. */
626 echo_message_buffer = echo_area_buffer[0];
627 echo_kboard = current_kboard;
629 if (waiting_for_input && !NILP (Vquit_flag))
630 quit_throw_to_read_char ();
633 /* Turn off echoing, for the start of a new command. */
635 void
636 cancel_echoing (void)
638 current_kboard->immediate_echo = 0;
639 current_kboard->echo_after_prompt = -1;
640 KVAR (current_kboard, echo_string) = Qnil;
641 ok_to_echo_at_next_pause = NULL;
642 echo_kboard = NULL;
643 echo_message_buffer = Qnil;
646 /* Return the length of the current echo string. */
648 static int
649 echo_length (void)
651 return (STRINGP (KVAR (current_kboard, echo_string))
652 ? SCHARS (KVAR (current_kboard, echo_string))
653 : 0);
656 /* Truncate the current echo message to its first LEN chars.
657 This and echo_char get used by read_key_sequence when the user
658 switches frames while entering a key sequence. */
660 static void
661 echo_truncate (EMACS_INT nchars)
663 if (STRINGP (KVAR (current_kboard, echo_string)))
664 KVAR (current_kboard, echo_string)
665 = Fsubstring (KVAR (current_kboard, echo_string),
666 make_number (0), make_number (nchars));
667 truncate_echo_area (nchars);
671 /* Functions for manipulating this_command_keys. */
672 static void
673 add_command_key (Lisp_Object key)
675 #if 0 /* Not needed after we made Freset_this_command_lengths
676 do the job immediately. */
677 /* If reset-this-command-length was called recently, obey it now.
678 See the doc string of that function for an explanation of why. */
679 if (before_command_restore_flag)
681 this_command_key_count = before_command_key_count_1;
682 if (this_command_key_count < this_single_command_key_start)
683 this_single_command_key_start = this_command_key_count;
684 echo_truncate (before_command_echo_length_1);
685 before_command_restore_flag = 0;
687 #endif
689 if (this_command_key_count >= ASIZE (this_command_keys))
690 this_command_keys = larger_vector (this_command_keys,
691 2 * ASIZE (this_command_keys),
692 Qnil);
694 ASET (this_command_keys, this_command_key_count, key);
695 ++this_command_key_count;
699 Lisp_Object
700 recursive_edit_1 (void)
702 int count = SPECPDL_INDEX ();
703 Lisp_Object val;
705 if (command_loop_level > 0)
707 specbind (Qstandard_output, Qt);
708 specbind (Qstandard_input, Qt);
711 #ifdef HAVE_WINDOW_SYSTEM
712 /* The command loop has started an hourglass timer, so we have to
713 cancel it here, otherwise it will fire because the recursive edit
714 can take some time. Do not check for display_hourglass_p here,
715 because it could already be nil. */
716 cancel_hourglass ();
717 #endif
719 /* This function may have been called from a debugger called from
720 within redisplay, for instance by Edebugging a function called
721 from fontification-functions. We want to allow redisplay in
722 the debugging session.
724 The recursive edit is left with a `(throw exit ...)'. The `exit'
725 tag is not caught anywhere in redisplay, i.e. when we leave the
726 recursive edit, the original redisplay leading to the recursive
727 edit will be unwound. The outcome should therefore be safe. */
728 specbind (Qinhibit_redisplay, Qnil);
729 redisplaying_p = 0;
731 val = command_loop ();
732 if (EQ (val, Qt))
733 Fsignal (Qquit, Qnil);
734 /* Handle throw from read_minibuf when using minibuffer
735 while it's active but we're in another window. */
736 if (STRINGP (val))
737 xsignal1 (Qerror, val);
739 return unbind_to (count, Qnil);
742 /* When an auto-save happens, record the "time", and don't do again soon. */
744 void
745 record_auto_save (void)
747 last_auto_save = num_nonmacro_input_events;
750 /* Make an auto save happen as soon as possible at command level. */
752 void
753 force_auto_save_soon (void)
755 last_auto_save = - auto_save_interval - 1;
757 record_asynch_buffer_change ();
760 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
761 doc: /* Invoke the editor command loop recursively.
762 To get out of the recursive edit, a command can do `(throw 'exit nil)';
763 that tells this function to return.
764 Alternatively, `(throw 'exit t)' makes this function signal an error.
765 This function is called by the editor initialization to begin editing. */)
766 (void)
768 int count = SPECPDL_INDEX ();
769 Lisp_Object buffer;
771 /* If we enter while input is blocked, don't lock up here.
772 This may happen through the debugger during redisplay. */
773 if (INPUT_BLOCKED_P)
774 return Qnil;
776 command_loop_level++;
777 update_mode_lines = 1;
779 if (command_loop_level
780 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
781 buffer = Fcurrent_buffer ();
782 else
783 buffer = Qnil;
785 /* If we leave recursive_edit_1 below with a `throw' for instance,
786 like it is done in the splash screen display, we have to
787 make sure that we restore single_kboard as command_loop_1
788 would have done if it were left normally. */
789 if (command_loop_level > 0)
790 temporarily_switch_to_single_kboard (SELECTED_FRAME ());
791 record_unwind_protect (recursive_edit_unwind, buffer);
793 recursive_edit_1 ();
794 return unbind_to (count, Qnil);
797 Lisp_Object
798 recursive_edit_unwind (Lisp_Object buffer)
800 if (BUFFERP (buffer))
801 Fset_buffer (buffer);
803 command_loop_level--;
804 update_mode_lines = 1;
805 return Qnil;
809 #if 0 /* These two functions are now replaced with
810 temporarily_switch_to_single_kboard. */
811 static void
812 any_kboard_state ()
814 #if 0 /* Theory: if there's anything in Vunread_command_events,
815 it will right away be read by read_key_sequence,
816 and then if we do switch KBOARDS, it will go into the side
817 queue then. So we don't need to do anything special here -- rms. */
818 if (CONSP (Vunread_command_events))
820 current_kboard->kbd_queue
821 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
822 current_kboard->kbd_queue_has_data = 1;
824 Vunread_command_events = Qnil;
825 #endif
826 single_kboard = 0;
829 /* Switch to the single-kboard state, making current_kboard
830 the only KBOARD from which further input is accepted. */
832 void
833 single_kboard_state ()
835 single_kboard = 1;
837 #endif
839 /* If we're in single_kboard state for kboard KBOARD,
840 get out of it. */
842 void
843 not_single_kboard_state (KBOARD *kboard)
845 if (kboard == current_kboard)
846 single_kboard = 0;
849 /* Maintain a stack of kboards, so other parts of Emacs
850 can switch temporarily to the kboard of a given frame
851 and then revert to the previous status. */
853 struct kboard_stack
855 KBOARD *kboard;
856 struct kboard_stack *next;
859 static struct kboard_stack *kboard_stack;
861 void
862 push_kboard (struct kboard *k)
864 struct kboard_stack *p
865 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
867 p->next = kboard_stack;
868 p->kboard = current_kboard;
869 kboard_stack = p;
871 current_kboard = k;
874 void
875 pop_kboard (void)
877 struct terminal *t;
878 struct kboard_stack *p = kboard_stack;
879 int found = 0;
880 for (t = terminal_list; t; t = t->next_terminal)
882 if (t->kboard == p->kboard)
884 current_kboard = p->kboard;
885 found = 1;
886 break;
889 if (!found)
891 /* The terminal we remembered has been deleted. */
892 current_kboard = FRAME_KBOARD (SELECTED_FRAME ());
893 single_kboard = 0;
895 kboard_stack = p->next;
896 xfree (p);
899 /* Switch to single_kboard mode, making current_kboard the only KBOARD
900 from which further input is accepted. If F is non-nil, set its
901 KBOARD as the current keyboard.
903 This function uses record_unwind_protect to return to the previous
904 state later.
906 If Emacs is already in single_kboard mode, and F's keyboard is
907 locked, then this function will throw an errow. */
909 void
910 temporarily_switch_to_single_kboard (struct frame *f)
912 int was_locked = single_kboard;
913 if (was_locked)
915 if (f != NULL && FRAME_KBOARD (f) != current_kboard)
916 /* We can not switch keyboards while in single_kboard mode.
917 In rare cases, Lisp code may call `recursive-edit' (or
918 `read-minibuffer' or `y-or-n-p') after it switched to a
919 locked frame. For example, this is likely to happen
920 when server.el connects to a new terminal while Emacs is in
921 single_kboard mode. It is best to throw an error instead
922 of presenting the user with a frozen screen. */
923 error ("Terminal %d is locked, cannot read from it",
924 FRAME_TERMINAL (f)->id);
925 else
926 /* This call is unnecessary, but helps
927 `restore_kboard_configuration' discover if somebody changed
928 `current_kboard' behind our back. */
929 push_kboard (current_kboard);
931 else if (f != NULL)
932 current_kboard = FRAME_KBOARD (f);
933 single_kboard = 1;
934 record_unwind_protect (restore_kboard_configuration,
935 (was_locked ? Qt : Qnil));
938 #if 0 /* This function is not needed anymore. */
939 void
940 record_single_kboard_state ()
942 if (single_kboard)
943 push_kboard (current_kboard);
944 record_unwind_protect (restore_kboard_configuration,
945 (single_kboard ? Qt : Qnil));
947 #endif
949 static Lisp_Object
950 restore_kboard_configuration (Lisp_Object was_locked)
952 if (NILP (was_locked))
953 single_kboard = 0;
954 else
956 struct kboard *prev = current_kboard;
957 single_kboard = 1;
958 pop_kboard ();
959 /* The pop should not change the kboard. */
960 if (single_kboard && current_kboard != prev)
961 abort ();
963 return Qnil;
967 /* Handle errors that are not handled at inner levels
968 by printing an error message and returning to the editor command loop. */
970 static Lisp_Object
971 cmd_error (Lisp_Object data)
973 Lisp_Object old_level, old_length;
974 char macroerror[50];
976 #ifdef HAVE_WINDOW_SYSTEM
977 if (display_hourglass_p)
978 cancel_hourglass ();
979 #endif
981 if (!NILP (executing_kbd_macro))
983 if (executing_kbd_macro_iterations == 1)
984 sprintf (macroerror, "After 1 kbd macro iteration: ");
985 else
986 sprintf (macroerror, "After %d kbd macro iterations: ",
987 executing_kbd_macro_iterations);
989 else
990 *macroerror = 0;
992 Vstandard_output = Qt;
993 Vstandard_input = Qt;
994 Vexecuting_kbd_macro = Qnil;
995 executing_kbd_macro = Qnil;
996 KVAR (current_kboard, Vprefix_arg) = Qnil;
997 KVAR (current_kboard, Vlast_prefix_arg) = Qnil;
998 cancel_echoing ();
1000 /* Avoid unquittable loop if data contains a circular list. */
1001 old_level = Vprint_level;
1002 old_length = Vprint_length;
1003 XSETFASTINT (Vprint_level, 10);
1004 XSETFASTINT (Vprint_length, 10);
1005 cmd_error_internal (data, macroerror);
1006 Vprint_level = old_level;
1007 Vprint_length = old_length;
1009 Vquit_flag = Qnil;
1011 Vinhibit_quit = Qnil;
1012 #if 0 /* This shouldn't be necessary anymore. --lorentey */
1013 if (command_loop_level == 0 && minibuf_level == 0)
1014 any_kboard_state ();
1015 #endif
1017 return make_number (0);
1020 /* Take actions on handling an error. DATA is the data that describes
1021 the error.
1023 CONTEXT is a C-string containing ASCII characters only which
1024 describes the context in which the error happened. If we need to
1025 generalize CONTEXT to allow multibyte characters, make it a Lisp
1026 string. */
1028 void
1029 cmd_error_internal (Lisp_Object data, const char *context)
1031 struct frame *sf = SELECTED_FRAME ();
1033 /* The immediate context is not interesting for Quits,
1034 since they are asyncronous. */
1035 if (EQ (XCAR (data), Qquit))
1036 Vsignaling_function = Qnil;
1038 Vquit_flag = Qnil;
1039 Vinhibit_quit = Qt;
1041 /* Use user's specified output function if any. */
1042 if (!NILP (Vcommand_error_function))
1043 call3 (Vcommand_error_function, data,
1044 context ? build_string (context) : empty_unibyte_string,
1045 Vsignaling_function);
1046 /* If the window system or terminal frame hasn't been initialized
1047 yet, or we're not interactive, write the message to stderr and exit. */
1048 else if (!sf->glyphs_initialized_p
1049 /* The initial frame is a special non-displaying frame. It
1050 will be current in daemon mode when there are no frames
1051 to display, and in non-daemon mode before the real frame
1052 has finished initializing. If an error is thrown in the
1053 latter case while creating the frame, then the frame
1054 will never be displayed, so the safest thing to do is
1055 write to stderr and quit. In daemon mode, there are
1056 many other potential errors that do not prevent frames
1057 from being created, so continuing as normal is better in
1058 that case. */
1059 || (!IS_DAEMON && FRAME_INITIAL_P (sf))
1060 || noninteractive)
1062 print_error_message (data, Qexternal_debugging_output,
1063 context, Vsignaling_function);
1064 Fterpri (Qexternal_debugging_output);
1065 Fkill_emacs (make_number (-1));
1067 else
1069 clear_message (1, 0);
1070 Fdiscard_input ();
1071 message_log_maybe_newline ();
1072 bitch_at_user ();
1074 print_error_message (data, Qt, context, Vsignaling_function);
1077 Vsignaling_function = Qnil;
1080 Lisp_Object command_loop_1 (void);
1081 Lisp_Object command_loop_2 (Lisp_Object);
1082 Lisp_Object top_level_1 (Lisp_Object);
1084 /* Entry to editor-command-loop.
1085 This level has the catches for exiting/returning to editor command loop.
1086 It returns nil to exit recursive edit, t to abort it. */
1088 Lisp_Object
1089 command_loop (void)
1091 if (command_loop_level > 0 || minibuf_level > 0)
1093 Lisp_Object val;
1094 val = internal_catch (Qexit, command_loop_2, Qnil);
1095 executing_kbd_macro = Qnil;
1096 return val;
1098 else
1099 while (1)
1101 internal_catch (Qtop_level, top_level_1, Qnil);
1102 #if 0 /* This shouldn't be necessary anymore. --lorentey */
1103 /* Reset single_kboard in case top-level set it while
1104 evaluating an -f option, or we are stuck there for some
1105 other reason. */
1106 any_kboard_state ();
1107 #endif
1108 internal_catch (Qtop_level, command_loop_2, Qnil);
1109 executing_kbd_macro = Qnil;
1111 /* End of file in -batch run causes exit here. */
1112 if (noninteractive)
1113 Fkill_emacs (Qt);
1117 /* Here we catch errors in execution of commands within the
1118 editing loop, and reenter the editing loop.
1119 When there is an error, cmd_error runs and returns a non-nil
1120 value to us. A value of nil means that command_loop_1 itself
1121 returned due to end of file (or end of kbd macro). */
1123 Lisp_Object
1124 command_loop_2 (Lisp_Object ignore)
1126 register Lisp_Object val;
1129 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
1130 while (!NILP (val));
1132 return Qnil;
1135 static Lisp_Object
1136 top_level_2 (void)
1138 return Feval (Vtop_level, Qnil);
1141 Lisp_Object
1142 top_level_1 (Lisp_Object ignore)
1144 /* On entry to the outer level, run the startup file */
1145 if (!NILP (Vtop_level))
1146 internal_condition_case (top_level_2, Qerror, cmd_error);
1147 else if (!NILP (Vpurify_flag))
1148 message ("Bare impure Emacs (standard Lisp code not loaded)");
1149 else
1150 message ("Bare Emacs (standard Lisp code not loaded)");
1151 return Qnil;
1154 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1155 doc: /* Exit all recursive editing levels.
1156 This also exits all active minibuffers. */)
1157 (void)
1159 #ifdef HAVE_WINDOW_SYSTEM
1160 if (display_hourglass_p)
1161 cancel_hourglass ();
1162 #endif
1164 /* Unblock input if we enter with input blocked. This may happen if
1165 redisplay traps e.g. during tool-bar update with input blocked. */
1166 while (INPUT_BLOCKED_P)
1167 UNBLOCK_INPUT;
1169 Fthrow (Qtop_level, Qnil);
1172 Lisp_Object Fexit_recursive_edit (void) NO_RETURN;
1173 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1174 doc: /* Exit from the innermost recursive edit or minibuffer. */)
1175 (void)
1177 if (command_loop_level > 0 || minibuf_level > 0)
1178 Fthrow (Qexit, Qnil);
1180 error ("No recursive edit is in progress");
1183 Lisp_Object Fabort_recursive_edit (void) NO_RETURN;
1184 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1185 doc: /* Abort the command that requested this recursive edit or minibuffer input. */)
1186 (void)
1188 if (command_loop_level > 0 || minibuf_level > 0)
1189 Fthrow (Qexit, Qt);
1191 error ("No recursive edit is in progress");
1194 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
1196 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
1197 of this function. */
1199 static Lisp_Object
1200 tracking_off (Lisp_Object old_value)
1202 do_mouse_tracking = old_value;
1203 if (NILP (old_value))
1205 /* Redisplay may have been preempted because there was input
1206 available, and it assumes it will be called again after the
1207 input has been processed. If the only input available was
1208 the sort that we have just disabled, then we need to call
1209 redisplay. */
1210 if (!readable_events (READABLE_EVENTS_DO_TIMERS_NOW))
1212 redisplay_preserve_echo_area (6);
1213 get_input_pending (&input_pending,
1214 READABLE_EVENTS_DO_TIMERS_NOW);
1217 return Qnil;
1220 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
1221 doc: /* Evaluate BODY with mouse movement events enabled.
1222 Within a `track-mouse' form, mouse motion generates input events that
1223 you can read with `read-event'.
1224 Normally, mouse motion is ignored.
1225 usage: (track-mouse BODY...) */)
1226 (Lisp_Object args)
1228 int count = SPECPDL_INDEX ();
1229 Lisp_Object val;
1231 record_unwind_protect (tracking_off, do_mouse_tracking);
1233 do_mouse_tracking = Qt;
1235 val = Fprogn (args);
1236 return unbind_to (count, val);
1239 /* If mouse has moved on some frame, return one of those frames.
1241 Return 0 otherwise.
1243 If ignore_mouse_drag_p is non-zero, ignore (implicit) mouse movement
1244 after resizing the tool-bar window. */
1246 int ignore_mouse_drag_p;
1248 static FRAME_PTR
1249 some_mouse_moved (void)
1251 Lisp_Object tail, frame;
1253 if (ignore_mouse_drag_p)
1255 /* ignore_mouse_drag_p = 0; */
1256 return 0;
1259 FOR_EACH_FRAME (tail, frame)
1261 if (XFRAME (frame)->mouse_moved)
1262 return XFRAME (frame);
1265 return 0;
1268 #endif /* HAVE_MOUSE || HAVE_GPM */
1270 /* This is the actual command reading loop,
1271 sans error-handling encapsulation. */
1273 static int read_key_sequence (Lisp_Object *, int, Lisp_Object,
1274 int, int, int);
1275 void safe_run_hooks (Lisp_Object);
1276 static void adjust_point_for_property (EMACS_INT, int);
1278 /* Cancel hourglass from protect_unwind.
1279 ARG is not used. */
1280 #ifdef HAVE_WINDOW_SYSTEM
1281 static Lisp_Object
1282 cancel_hourglass_unwind (Lisp_Object arg)
1284 cancel_hourglass ();
1285 return Qnil;
1287 #endif
1289 /* FIXME: This is wrong rather than test window-system, we should call
1290 a new set-selection, which will then dispatch to x-set-selection, or
1291 tty-set-selection, or w32-set-selection, ... */
1292 EXFUN (Fwindow_system, 1);
1294 Lisp_Object
1295 command_loop_1 (void)
1297 Lisp_Object cmd;
1298 Lisp_Object keybuf[30];
1299 int i;
1300 int prev_modiff = 0;
1301 struct buffer *prev_buffer = NULL;
1302 #if 0 /* This shouldn't be necessary anymore. --lorentey */
1303 int was_locked = single_kboard;
1304 #endif
1305 int already_adjusted = 0;
1307 KVAR (current_kboard, Vprefix_arg) = Qnil;
1308 KVAR (current_kboard, Vlast_prefix_arg) = Qnil;
1309 Vdeactivate_mark = Qnil;
1310 waiting_for_input = 0;
1311 cancel_echoing ();
1313 this_command_key_count = 0;
1314 this_command_key_count_reset = 0;
1315 this_single_command_key_start = 0;
1317 if (NILP (Vmemory_full))
1319 /* Make sure this hook runs after commands that get errors and
1320 throw to top level. */
1321 /* Note that the value cell will never directly contain nil
1322 if the symbol is a local variable. */
1323 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1324 safe_run_hooks (Qpost_command_hook);
1326 /* If displaying a message, resize the echo area window to fit
1327 that message's size exactly. */
1328 if (!NILP (echo_area_buffer[0]))
1329 resize_echo_area_exactly ();
1331 if (!NILP (Vdeferred_action_list))
1332 safe_run_hooks (Qdeferred_action_function);
1335 /* Do this after running Vpost_command_hook, for consistency. */
1336 KVAR (current_kboard, Vlast_command) = Vthis_command;
1337 KVAR (current_kboard, Vreal_last_command) = real_this_command;
1338 if (!CONSP (last_command_event))
1339 KVAR (current_kboard, Vlast_repeatable_command) = real_this_command;
1341 while (1)
1343 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1344 Fkill_emacs (Qnil);
1346 /* Make sure the current window's buffer is selected. */
1347 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1348 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1350 /* Display any malloc warning that just came out. Use while because
1351 displaying one warning can cause another. */
1353 while (pending_malloc_warning)
1354 display_malloc_warning ();
1356 Vdeactivate_mark = Qnil;
1358 /* If minibuffer on and echo area in use,
1359 wait a short time and redraw minibuffer. */
1361 if (minibuf_level
1362 && !NILP (echo_area_buffer[0])
1363 && EQ (minibuf_window, echo_area_window)
1364 && NUMBERP (Vminibuffer_message_timeout))
1366 /* Bind inhibit-quit to t so that C-g gets read in
1367 rather than quitting back to the minibuffer. */
1368 int count = SPECPDL_INDEX ();
1369 specbind (Qinhibit_quit, Qt);
1371 sit_for (Vminibuffer_message_timeout, 0, 2);
1373 /* Clear the echo area. */
1374 message2 (0, 0, 0);
1375 safe_run_hooks (Qecho_area_clear_hook);
1377 unbind_to (count, Qnil);
1379 /* If a C-g came in before, treat it as input now. */
1380 if (!NILP (Vquit_flag))
1382 Vquit_flag = Qnil;
1383 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1387 #if 0
1388 /* Select the frame that the last event came from. Usually,
1389 switch-frame events will take care of this, but if some lisp
1390 code swallows a switch-frame event, we'll fix things up here.
1391 Is this a good idea? */
1392 if (FRAMEP (internal_last_event_frame)
1393 && !EQ (internal_last_event_frame, selected_frame))
1394 Fselect_frame (internal_last_event_frame, Qnil);
1395 #endif
1396 /* If it has changed current-menubar from previous value,
1397 really recompute the menubar from the value. */
1398 if (! NILP (Vlucid_menu_bar_dirty_flag)
1399 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1400 call0 (Qrecompute_lucid_menubar);
1402 before_command_key_count = this_command_key_count;
1403 before_command_echo_length = echo_length ();
1405 Vthis_command = Qnil;
1406 real_this_command = Qnil;
1407 Vthis_original_command = Qnil;
1408 Vthis_command_keys_shift_translated = Qnil;
1410 /* Read next key sequence; i gets its length. */
1411 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1412 Qnil, 0, 1, 1);
1414 /* A filter may have run while we were reading the input. */
1415 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1416 Fkill_emacs (Qnil);
1417 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1418 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1420 ++num_input_keys;
1422 /* Now we have read a key sequence of length I,
1423 or else I is 0 and we found end of file. */
1425 if (i == 0) /* End of file -- happens only in */
1426 return Qnil; /* a kbd macro, at the end. */
1427 /* -1 means read_key_sequence got a menu that was rejected.
1428 Just loop around and read another command. */
1429 if (i == -1)
1431 cancel_echoing ();
1432 this_command_key_count = 0;
1433 this_command_key_count_reset = 0;
1434 this_single_command_key_start = 0;
1435 goto finalize;
1438 last_command_event = keybuf[i - 1];
1440 /* If the previous command tried to force a specific window-start,
1441 forget about that, in case this command moves point far away
1442 from that position. But also throw away beg_unchanged and
1443 end_unchanged information in that case, so that redisplay will
1444 update the whole window properly. */
1445 if (!NILP (XWINDOW (selected_window)->force_start))
1447 struct buffer *b;
1448 XWINDOW (selected_window)->force_start = Qnil;
1449 b = XBUFFER (XWINDOW (selected_window)->buffer);
1450 BUF_BEG_UNCHANGED (b) = BUF_END_UNCHANGED (b) = 0;
1453 cmd = read_key_sequence_cmd;
1454 if (!NILP (Vexecuting_kbd_macro))
1456 if (!NILP (Vquit_flag))
1458 Vexecuting_kbd_macro = Qt;
1459 QUIT; /* Make some noise. */
1460 /* Will return since macro now empty. */
1464 /* Do redisplay processing after this command except in special
1465 cases identified below. */
1466 prev_buffer = current_buffer;
1467 prev_modiff = MODIFF;
1468 last_point_position = PT;
1469 last_point_position_window = selected_window;
1470 XSETBUFFER (last_point_position_buffer, prev_buffer);
1472 /* By default, we adjust point to a boundary of a region that
1473 has such a property that should be treated intangible
1474 (e.g. composition, display). But, some commands will set
1475 this variable differently. */
1476 Vdisable_point_adjustment = Qnil;
1478 /* Process filters and timers may have messed with deactivate-mark.
1479 reset it before we execute the command. */
1480 Vdeactivate_mark = Qnil;
1482 /* Remap command through active keymaps */
1483 Vthis_original_command = cmd;
1484 if (SYMBOLP (cmd))
1486 Lisp_Object cmd1;
1487 if (cmd1 = Fcommand_remapping (cmd, Qnil, Qnil), !NILP (cmd1))
1488 cmd = cmd1;
1491 /* Execute the command. */
1493 Vthis_command = cmd;
1494 real_this_command = cmd;
1495 /* Note that the value cell will never directly contain nil
1496 if the symbol is a local variable. */
1497 if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
1498 safe_run_hooks (Qpre_command_hook);
1500 already_adjusted = 0;
1502 if (NILP (Vthis_command))
1504 /* nil means key is undefined. */
1505 Lisp_Object keys = Fvector (i, keybuf);
1506 keys = Fkey_description (keys, Qnil);
1507 bitch_at_user ();
1508 message_with_string ("%s is undefined", keys, 0);
1509 KVAR (current_kboard, defining_kbd_macro) = Qnil;
1510 update_mode_lines = 1;
1511 KVAR (current_kboard, Vprefix_arg) = Qnil;
1513 else
1515 /* Here for a command that isn't executed directly */
1517 #ifdef HAVE_WINDOW_SYSTEM
1518 int scount = SPECPDL_INDEX ();
1520 if (display_hourglass_p
1521 && NILP (Vexecuting_kbd_macro))
1523 record_unwind_protect (cancel_hourglass_unwind, Qnil);
1524 start_hourglass ();
1526 #endif
1528 if (NILP (KVAR (current_kboard, Vprefix_arg))) /* FIXME: Why? --Stef */
1529 Fundo_boundary ();
1530 Fcommand_execute (Vthis_command, Qnil, Qnil, Qnil);
1532 #ifdef HAVE_WINDOW_SYSTEM
1533 /* Do not check display_hourglass_p here, because
1534 Fcommand_execute could change it, but we should cancel
1535 hourglass cursor anyway.
1536 But don't cancel the hourglass within a macro
1537 just because a command in the macro finishes. */
1538 if (NILP (Vexecuting_kbd_macro))
1539 unbind_to (scount, Qnil);
1540 #endif
1542 KVAR (current_kboard, Vlast_prefix_arg) = Vcurrent_prefix_arg;
1544 /* Note that the value cell will never directly contain nil
1545 if the symbol is a local variable. */
1546 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1547 safe_run_hooks (Qpost_command_hook);
1549 /* If displaying a message, resize the echo area window to fit
1550 that message's size exactly. */
1551 if (!NILP (echo_area_buffer[0]))
1552 resize_echo_area_exactly ();
1554 if (!NILP (Vdeferred_action_list))
1555 safe_run_hooks (Qdeferred_action_function);
1557 /* If there is a prefix argument,
1558 1) We don't want Vlast_command to be ``universal-argument''
1559 (that would be dumb), so don't set Vlast_command,
1560 2) we want to leave echoing on so that the prefix will be
1561 echoed as part of this key sequence, so don't call
1562 cancel_echoing, and
1563 3) we want to leave this_command_key_count non-zero, so that
1564 read_char will realize that it is re-reading a character, and
1565 not echo it a second time.
1567 If the command didn't actually create a prefix arg,
1568 but is merely a frame event that is transparent to prefix args,
1569 then the above doesn't apply. */
1570 if (NILP (KVAR (current_kboard, Vprefix_arg)) || CONSP (last_command_event))
1572 KVAR (current_kboard, Vlast_command) = Vthis_command;
1573 KVAR (current_kboard, Vreal_last_command) = real_this_command;
1574 if (!CONSP (last_command_event))
1575 KVAR (current_kboard, Vlast_repeatable_command) = real_this_command;
1576 cancel_echoing ();
1577 this_command_key_count = 0;
1578 this_command_key_count_reset = 0;
1579 this_single_command_key_start = 0;
1582 if (!NILP (BVAR (current_buffer, mark_active))
1583 && !NILP (Vrun_hooks))
1585 /* In Emacs 22, setting transient-mark-mode to `only' was a
1586 way of turning it on for just one command. This usage is
1587 obsolete, but support it anyway. */
1588 if (EQ (Vtransient_mark_mode, Qidentity))
1589 Vtransient_mark_mode = Qnil;
1590 else if (EQ (Vtransient_mark_mode, Qonly))
1591 Vtransient_mark_mode = Qidentity;
1593 if (!NILP (Vdeactivate_mark))
1594 /* If `select-active-regions' is non-nil, this call to
1595 `deactivate-mark' also sets the PRIMARY selection. */
1596 call0 (Qdeactivate_mark);
1597 else
1599 /* Even if not deactivating the mark, set PRIMARY if
1600 `select-active-regions' is non-nil. */
1601 if (!NILP (Fwindow_system (Qnil))
1602 /* Even if mark_active is non-nil, the actual buffer
1603 marker may not have been set yet (Bug#7044). */
1604 && XMARKER (BVAR (current_buffer, mark))->buffer
1605 && (EQ (Vselect_active_regions, Qonly)
1606 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
1607 : (!NILP (Vselect_active_regions)
1608 && !NILP (Vtransient_mark_mode)))
1609 && !EQ (Vthis_command, Qhandle_switch_frame))
1611 EMACS_INT beg =
1612 XINT (Fmarker_position (BVAR (current_buffer, mark)));
1613 EMACS_INT end = PT;
1614 if (beg < end)
1615 call2 (Qx_set_selection, QPRIMARY,
1616 make_buffer_string (beg, end, 0));
1617 else if (beg > end)
1618 call2 (Qx_set_selection, QPRIMARY,
1619 make_buffer_string (end, beg, 0));
1620 /* Don't set empty selections. */
1623 if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1624 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1627 Vsaved_region_selection = Qnil;
1630 finalize:
1632 if (current_buffer == prev_buffer
1633 && last_point_position != PT
1634 && NILP (Vdisable_point_adjustment)
1635 && NILP (Vglobal_disable_point_adjustment))
1637 if (last_point_position > BEGV
1638 && last_point_position < ZV
1639 && (composition_adjust_point (last_point_position,
1640 last_point_position)
1641 != last_point_position))
1642 /* The last point was temporarily set within a grapheme
1643 cluster to prevent automatic composition. To recover
1644 the automatic composition, we must update the
1645 display. */
1646 windows_or_buffers_changed++;
1647 if (!already_adjusted)
1648 adjust_point_for_property (last_point_position,
1649 MODIFF != prev_modiff);
1652 /* Install chars successfully executed in kbd macro. */
1654 if (!NILP (KVAR (current_kboard, defining_kbd_macro))
1655 && NILP (KVAR (current_kboard, Vprefix_arg)))
1656 finalize_kbd_macro_chars ();
1657 #if 0 /* This shouldn't be necessary anymore. --lorentey */
1658 if (!was_locked)
1659 any_kboard_state ();
1660 #endif
1664 /* Adjust point to a boundary of a region that has such a property
1665 that should be treated intangible. For the moment, we check
1666 `composition', `display' and `invisible' properties.
1667 LAST_PT is the last position of point. */
1669 static void
1670 adjust_point_for_property (EMACS_INT last_pt, int modified)
1672 EMACS_INT beg, end;
1673 Lisp_Object val, overlay, tmp;
1674 /* When called after buffer modification, we should temporarily
1675 suppress the point adjustment for automatic composition so that a
1676 user can keep inserting another character at point or keep
1677 deleting characters around point. */
1678 int check_composition = ! modified, check_display = 1, check_invisible = 1;
1679 EMACS_INT orig_pt = PT;
1681 /* FIXME: cycling is probably not necessary because these properties
1682 can't be usefully combined anyway. */
1683 while (check_composition || check_display || check_invisible)
1685 /* FIXME: check `intangible'. */
1686 if (check_composition
1687 && PT > BEGV && PT < ZV
1688 && (beg = composition_adjust_point (last_pt, PT)) != PT)
1690 SET_PT (beg);
1691 check_display = check_invisible = 1;
1693 check_composition = 0;
1694 if (check_display
1695 && PT > BEGV && PT < ZV
1696 && !NILP (val = get_char_property_and_overlay
1697 (make_number (PT), Qdisplay, Qnil, &overlay))
1698 && display_prop_intangible_p (val)
1699 && (!OVERLAYP (overlay)
1700 ? get_property_and_range (PT, Qdisplay, &val, &beg, &end, Qnil)
1701 : (beg = OVERLAY_POSITION (OVERLAY_START (overlay)),
1702 end = OVERLAY_POSITION (OVERLAY_END (overlay))))
1703 && (beg < PT /* && end > PT <- It's always the case. */
1704 || (beg <= PT && STRINGP (val) && SCHARS (val) == 0)))
1706 xassert (end > PT);
1707 SET_PT (PT < last_pt
1708 ? (STRINGP (val) && SCHARS (val) == 0 ? beg - 1 : beg)
1709 : end);
1710 check_composition = check_invisible = 1;
1712 check_display = 0;
1713 if (check_invisible && PT > BEGV && PT < ZV)
1715 int inv, ellipsis = 0;
1716 beg = end = PT;
1718 /* Find boundaries `beg' and `end' of the invisible area, if any. */
1719 while (end < ZV
1720 #if 0
1721 /* FIXME: We should stop if we find a spot between
1722 two runs of `invisible' where inserted text would
1723 be visible. This is important when we have two
1724 invisible boundaries that enclose an area: if the
1725 area is empty, we need this test in order to make
1726 it possible to place point in the middle rather
1727 than skip both boundaries. However, this code
1728 also stops anywhere in a non-sticky text-property,
1729 which breaks (e.g.) Org mode. */
1730 && (val = get_pos_property (make_number (end),
1731 Qinvisible, Qnil),
1732 TEXT_PROP_MEANS_INVISIBLE (val))
1733 #endif
1734 && !NILP (val = get_char_property_and_overlay
1735 (make_number (end), Qinvisible, Qnil, &overlay))
1736 && (inv = TEXT_PROP_MEANS_INVISIBLE (val)))
1738 ellipsis = ellipsis || inv > 1
1739 || (OVERLAYP (overlay)
1740 && (!NILP (Foverlay_get (overlay, Qafter_string))
1741 || !NILP (Foverlay_get (overlay, Qbefore_string))));
1742 tmp = Fnext_single_char_property_change
1743 (make_number (end), Qinvisible, Qnil, Qnil);
1744 end = NATNUMP (tmp) ? XFASTINT (tmp) : ZV;
1746 while (beg > BEGV
1747 #if 0
1748 && (val = get_pos_property (make_number (beg),
1749 Qinvisible, Qnil),
1750 TEXT_PROP_MEANS_INVISIBLE (val))
1751 #endif
1752 && !NILP (val = get_char_property_and_overlay
1753 (make_number (beg - 1), Qinvisible, Qnil, &overlay))
1754 && (inv = TEXT_PROP_MEANS_INVISIBLE (val)))
1756 ellipsis = ellipsis || inv > 1
1757 || (OVERLAYP (overlay)
1758 && (!NILP (Foverlay_get (overlay, Qafter_string))
1759 || !NILP (Foverlay_get (overlay, Qbefore_string))));
1760 tmp = Fprevious_single_char_property_change
1761 (make_number (beg), Qinvisible, Qnil, Qnil);
1762 beg = NATNUMP (tmp) ? XFASTINT (tmp) : BEGV;
1765 /* Move away from the inside area. */
1766 if (beg < PT && end > PT)
1768 SET_PT ((orig_pt == PT && (last_pt < beg || last_pt > end))
1769 /* We haven't moved yet (so we don't need to fear
1770 infinite-looping) and we were outside the range
1771 before (so either end of the range still corresponds
1772 to a move in the right direction): pretend we moved
1773 less than we actually did, so that we still have
1774 more freedom below in choosing which end of the range
1775 to go to. */
1776 ? (orig_pt = -1, PT < last_pt ? end : beg)
1777 /* We either have moved already or the last point
1778 was already in the range: we don't get to choose
1779 which end of the range we have to go to. */
1780 : (PT < last_pt ? beg : end));
1781 check_composition = check_display = 1;
1783 #if 0 /* This assertion isn't correct, because SET_PT may end up setting
1784 the point to something other than its argument, due to
1785 point-motion hooks, intangibility, etc. */
1786 xassert (PT == beg || PT == end);
1787 #endif
1789 /* Pretend the area doesn't exist if the buffer is not
1790 modified. */
1791 if (!modified && !ellipsis && beg < end)
1793 if (last_pt == beg && PT == end && end < ZV)
1794 (check_composition = check_display = 1, SET_PT (end + 1));
1795 else if (last_pt == end && PT == beg && beg > BEGV)
1796 (check_composition = check_display = 1, SET_PT (beg - 1));
1797 else if (PT == ((PT < last_pt) ? beg : end))
1798 /* We've already moved as far as we can. Trying to go
1799 to the other end would mean moving backwards and thus
1800 could lead to an infinite loop. */
1802 else if (val = get_pos_property (make_number (PT),
1803 Qinvisible, Qnil),
1804 TEXT_PROP_MEANS_INVISIBLE (val)
1805 && (val = get_pos_property
1806 (make_number (PT == beg ? end : beg),
1807 Qinvisible, Qnil),
1808 !TEXT_PROP_MEANS_INVISIBLE (val)))
1809 (check_composition = check_display = 1,
1810 SET_PT (PT == beg ? end : beg));
1813 check_invisible = 0;
1817 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1819 static Lisp_Object
1820 safe_run_hooks_1 (void)
1822 if (NILP (Vrun_hooks))
1823 return Qnil;
1824 return call1 (Vrun_hooks, Vinhibit_quit);
1827 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1829 static Lisp_Object
1830 safe_run_hooks_error (Lisp_Object data)
1832 Lisp_Object args[3];
1833 args[0] = build_string ("Error in %s: %s");
1834 args[1] = Vinhibit_quit;
1835 args[2] = data;
1836 Fmessage (3, args);
1837 return Fset (Vinhibit_quit, Qnil);
1840 /* If we get an error while running the hook, cause the hook variable
1841 to be nil. Also inhibit quits, so that C-g won't cause the hook
1842 to mysteriously evaporate. */
1844 void
1845 safe_run_hooks (Lisp_Object hook)
1847 int count = SPECPDL_INDEX ();
1848 specbind (Qinhibit_quit, hook);
1850 internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
1852 unbind_to (count, Qnil);
1856 /* Nonzero means polling for input is temporarily suppressed. */
1858 int poll_suppress_count;
1860 /* Asynchronous timer for polling. */
1862 struct atimer *poll_timer;
1865 #ifdef POLL_FOR_INPUT
1867 /* Poll for input, so that we catch a C-g if it comes in. This
1868 function is called from x_make_frame_visible, see comment
1869 there. */
1871 void
1872 poll_for_input_1 (void)
1874 /* Tell ns_read_socket() it is being called asynchronously so it can avoid
1875 doing anything dangerous. */
1876 #ifdef HAVE_NS
1877 ++handling_signal;
1878 #endif
1879 if (interrupt_input_blocked == 0
1880 && !waiting_for_input)
1881 read_avail_input (0);
1882 #ifdef HAVE_NS
1883 --handling_signal;
1884 #endif
1887 /* Timer callback function for poll_timer. TIMER is equal to
1888 poll_timer. */
1890 static void
1891 poll_for_input (struct atimer *timer)
1893 if (poll_suppress_count == 0)
1895 #ifdef SYNC_INPUT
1896 interrupt_input_pending = 1;
1897 pending_signals = 1;
1898 #else
1899 poll_for_input_1 ();
1900 #endif
1904 #endif /* POLL_FOR_INPUT */
1906 /* Begin signals to poll for input, if they are appropriate.
1907 This function is called unconditionally from various places. */
1909 void
1910 start_polling (void)
1912 #ifdef POLL_FOR_INPUT
1913 /* XXX This condition was (read_socket_hook && !interrupt_input),
1914 but read_socket_hook is not global anymore. Let's pretend that
1915 it's always set. */
1916 if (!interrupt_input)
1918 /* Turn alarm handling on unconditionally. It might have
1919 been turned off in process.c. */
1920 turn_on_atimers (1);
1922 /* If poll timer doesn't exist, are we need one with
1923 a different interval, start a new one. */
1924 if (poll_timer == NULL
1925 || EMACS_SECS (poll_timer->interval) != polling_period)
1927 EMACS_TIME interval;
1929 if (poll_timer)
1930 cancel_atimer (poll_timer);
1932 EMACS_SET_SECS_USECS (interval, polling_period, 0);
1933 poll_timer = start_atimer (ATIMER_CONTINUOUS, interval,
1934 poll_for_input, NULL);
1937 /* Let the timer's callback function poll for input
1938 if this becomes zero. */
1939 --poll_suppress_count;
1941 #endif
1944 /* Nonzero if we are using polling to handle input asynchronously. */
1947 input_polling_used (void)
1949 #ifdef POLL_FOR_INPUT
1950 /* XXX This condition was (read_socket_hook && !interrupt_input),
1951 but read_socket_hook is not global anymore. Let's pretend that
1952 it's always set. */
1953 return !interrupt_input;
1954 #else
1955 return 0;
1956 #endif
1959 /* Turn off polling. */
1961 void
1962 stop_polling (void)
1964 #ifdef POLL_FOR_INPUT
1965 /* XXX This condition was (read_socket_hook && !interrupt_input),
1966 but read_socket_hook is not global anymore. Let's pretend that
1967 it's always set. */
1968 if (!interrupt_input)
1969 ++poll_suppress_count;
1970 #endif
1973 /* Set the value of poll_suppress_count to COUNT
1974 and start or stop polling accordingly. */
1976 void
1977 set_poll_suppress_count (int count)
1979 #ifdef POLL_FOR_INPUT
1980 if (count == 0 && poll_suppress_count != 0)
1982 poll_suppress_count = 1;
1983 start_polling ();
1985 else if (count != 0 && poll_suppress_count == 0)
1987 stop_polling ();
1989 poll_suppress_count = count;
1990 #endif
1993 /* Bind polling_period to a value at least N.
1994 But don't decrease it. */
1996 void
1997 bind_polling_period (int n)
1999 #ifdef POLL_FOR_INPUT
2000 int new = polling_period;
2002 if (n > new)
2003 new = n;
2005 stop_other_atimers (poll_timer);
2006 stop_polling ();
2007 specbind (Qpolling_period, make_number (new));
2008 /* Start a new alarm with the new period. */
2009 start_polling ();
2010 #endif
2013 /* Apply the control modifier to CHARACTER. */
2016 make_ctrl_char (int c)
2018 /* Save the upper bits here. */
2019 int upper = c & ~0177;
2021 if (! ASCII_BYTE_P (c))
2022 return c |= ctrl_modifier;
2024 c &= 0177;
2026 /* Everything in the columns containing the upper-case letters
2027 denotes a control character. */
2028 if (c >= 0100 && c < 0140)
2030 int oc = c;
2031 c &= ~0140;
2032 /* Set the shift modifier for a control char
2033 made from a shifted letter. But only for letters! */
2034 if (oc >= 'A' && oc <= 'Z')
2035 c |= shift_modifier;
2038 /* The lower-case letters denote control characters too. */
2039 else if (c >= 'a' && c <= 'z')
2040 c &= ~0140;
2042 /* Include the bits for control and shift
2043 only if the basic ASCII code can't indicate them. */
2044 else if (c >= ' ')
2045 c |= ctrl_modifier;
2047 /* Replace the high bits. */
2048 c |= (upper & ~ctrl_modifier);
2050 return c;
2053 /* Display the help-echo property of the character after the mouse pointer.
2054 Either show it in the echo area, or call show-help-function to display
2055 it by other means (maybe in a tooltip).
2057 If HELP is nil, that means clear the previous help echo.
2059 If HELP is a string, display that string. If HELP is a function,
2060 call it with OBJECT and POS as arguments; the function should
2061 return a help string or nil for none. For all other types of HELP,
2062 evaluate it to obtain a string.
2064 WINDOW is the window in which the help was generated, if any.
2065 It is nil if not in a window.
2067 If OBJECT is a buffer, POS is the position in the buffer where the
2068 `help-echo' text property was found.
2070 If OBJECT is an overlay, that overlay has a `help-echo' property,
2071 and POS is the position in the overlay's buffer under the mouse.
2073 If OBJECT is a string (an overlay string or a string displayed with
2074 the `display' property). POS is the position in that string under
2075 the mouse.
2077 OK_TO_OVERWRITE_KEYSTROKE_ECHO non-zero means it's okay if the help
2078 echo overwrites a keystroke echo currently displayed in the echo
2079 area.
2081 Note: this function may only be called with HELP nil or a string
2082 from X code running asynchronously. */
2084 void
2085 show_help_echo (Lisp_Object help, Lisp_Object window, Lisp_Object object,
2086 Lisp_Object pos, int ok_to_overwrite_keystroke_echo)
2088 if (!NILP (help) && !STRINGP (help))
2090 if (FUNCTIONP (help))
2092 Lisp_Object args[4];
2093 args[0] = help;
2094 args[1] = window;
2095 args[2] = object;
2096 args[3] = pos;
2097 help = safe_call (4, args);
2099 else
2100 help = safe_eval (help);
2102 if (!STRINGP (help))
2103 return;
2106 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
2107 if (!noninteractive && STRINGP (help))
2109 /* The mouse-fixup-help-message Lisp function can call
2110 mouse_position_hook, which resets the mouse_moved flags.
2111 This causes trouble if we are trying to read a mouse motion
2112 event (i.e., if we are inside a `track-mouse' form), so we
2113 restore the mouse_moved flag. */
2114 FRAME_PTR f = NILP (do_mouse_tracking) ? NULL : some_mouse_moved ();
2115 help = call1 (Qmouse_fixup_help_message, help);
2116 if (f)
2117 f->mouse_moved = 1;
2119 #endif
2121 if (STRINGP (help) || NILP (help))
2123 if (!NILP (Vshow_help_function))
2124 call1 (Vshow_help_function, help);
2125 help_echo_showing_p = STRINGP (help);
2131 /* Input of single characters from keyboard */
2133 Lisp_Object print_help (Lisp_Object object);
2134 static Lisp_Object kbd_buffer_get_event (KBOARD **kbp, int *used_mouse_menu,
2135 struct timeval *end_time);
2136 static void record_char (Lisp_Object c);
2138 static Lisp_Object help_form_saved_window_configs;
2139 static Lisp_Object
2140 read_char_help_form_unwind (Lisp_Object arg)
2142 Lisp_Object window_config = XCAR (help_form_saved_window_configs);
2143 help_form_saved_window_configs = XCDR (help_form_saved_window_configs);
2144 if (!NILP (window_config))
2145 Fset_window_configuration (window_config);
2146 return Qnil;
2149 #define STOP_POLLING \
2150 do { if (! polling_stopped_here) stop_polling (); \
2151 polling_stopped_here = 1; } while (0)
2153 #define RESUME_POLLING \
2154 do { if (polling_stopped_here) start_polling (); \
2155 polling_stopped_here = 0; } while (0)
2157 /* read a character from the keyboard; call the redisplay if needed */
2158 /* commandflag 0 means do not do auto-saving, but do do redisplay.
2159 -1 means do not do redisplay, but do do autosaving.
2160 1 means do both. */
2162 /* The arguments MAPS and NMAPS are for menu prompting.
2163 MAPS is an array of keymaps; NMAPS is the length of MAPS.
2165 PREV_EVENT is the previous input event, or nil if we are reading
2166 the first event of a key sequence (or not reading a key sequence).
2167 If PREV_EVENT is t, that is a "magic" value that says
2168 not to run input methods, but in other respects to act as if
2169 not reading a key sequence.
2171 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
2172 if we used a mouse menu to read the input, or zero otherwise. If
2173 USED_MOUSE_MENU is null, we don't dereference it.
2175 Value is -2 when we find input on another keyboard. A second call
2176 to read_char will read it.
2178 If END_TIME is non-null, it is a pointer to an EMACS_TIME
2179 specifying the maximum time to wait until. If no input arrives by
2180 that time, stop waiting and return nil.
2182 Value is t if we showed a menu and the user rejected it. */
2184 Lisp_Object
2185 read_char (int commandflag, int nmaps, Lisp_Object *maps, Lisp_Object prev_event,
2186 int *used_mouse_menu, struct timeval *end_time)
2188 volatile Lisp_Object c;
2189 int jmpcount;
2190 jmp_buf local_getcjmp;
2191 jmp_buf save_jump;
2192 volatile int key_already_recorded = 0;
2193 Lisp_Object tem, save;
2194 volatile Lisp_Object previous_echo_area_message;
2195 volatile Lisp_Object also_record;
2196 volatile int reread;
2197 struct gcpro gcpro1, gcpro2;
2198 int polling_stopped_here = 0;
2199 struct kboard *orig_kboard = current_kboard;
2201 also_record = Qnil;
2203 #if 0 /* This was commented out as part of fixing echo for C-u left. */
2204 before_command_key_count = this_command_key_count;
2205 before_command_echo_length = echo_length ();
2206 #endif
2207 c = Qnil;
2208 previous_echo_area_message = Qnil;
2210 GCPRO2 (c, previous_echo_area_message);
2212 retry:
2214 reread = 0;
2215 if (CONSP (Vunread_post_input_method_events))
2217 c = XCAR (Vunread_post_input_method_events);
2218 Vunread_post_input_method_events
2219 = XCDR (Vunread_post_input_method_events);
2221 /* Undo what read_char_x_menu_prompt did when it unread
2222 additional keys returned by Fx_popup_menu. */
2223 if (CONSP (c)
2224 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2225 && NILP (XCDR (c)))
2226 c = XCAR (c);
2228 reread = 1;
2229 goto reread_first;
2232 if (unread_command_char != -1)
2234 XSETINT (c, unread_command_char);
2235 unread_command_char = -1;
2237 reread = 1;
2238 goto reread_first;
2241 if (CONSP (Vunread_command_events))
2243 int was_disabled = 0;
2245 c = XCAR (Vunread_command_events);
2246 Vunread_command_events = XCDR (Vunread_command_events);
2248 reread = 1;
2250 /* Undo what sit-for did when it unread additional keys
2251 inside universal-argument. */
2253 if (CONSP (c)
2254 && EQ (XCAR (c), Qt))
2256 reread = 0;
2257 c = XCDR (c);
2260 /* Undo what read_char_x_menu_prompt did when it unread
2261 additional keys returned by Fx_popup_menu. */
2262 if (CONSP (c)
2263 && EQ (XCDR (c), Qdisabled)
2264 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c))))
2266 was_disabled = 1;
2267 c = XCAR (c);
2270 /* If the queued event is something that used the mouse,
2271 set used_mouse_menu accordingly. */
2272 if (used_mouse_menu
2273 /* Also check was_disabled so last-nonmenu-event won't return
2274 a bad value when submenus are involved. (Bug#447) */
2275 && (EQ (c, Qtool_bar) || EQ (c, Qmenu_bar) || was_disabled))
2276 *used_mouse_menu = 1;
2278 goto reread_for_input_method;
2281 if (CONSP (Vunread_input_method_events))
2283 c = XCAR (Vunread_input_method_events);
2284 Vunread_input_method_events = XCDR (Vunread_input_method_events);
2286 /* Undo what read_char_x_menu_prompt did when it unread
2287 additional keys returned by Fx_popup_menu. */
2288 if (CONSP (c)
2289 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2290 && NILP (XCDR (c)))
2291 c = XCAR (c);
2292 reread = 1;
2293 goto reread_for_input_method;
2296 this_command_key_count_reset = 0;
2298 if (!NILP (Vexecuting_kbd_macro))
2300 /* We set this to Qmacro; since that's not a frame, nobody will
2301 try to switch frames on us, and the selected window will
2302 remain unchanged.
2304 Since this event came from a macro, it would be misleading to
2305 leave internal_last_event_frame set to wherever the last
2306 real event came from. Normally, a switch-frame event selects
2307 internal_last_event_frame after each command is read, but
2308 events read from a macro should never cause a new frame to be
2309 selected. */
2310 Vlast_event_frame = internal_last_event_frame = Qmacro;
2312 /* Exit the macro if we are at the end.
2313 Also, some things replace the macro with t
2314 to force an early exit. */
2315 if (EQ (Vexecuting_kbd_macro, Qt)
2316 || executing_kbd_macro_index >= XFASTINT (Flength (Vexecuting_kbd_macro)))
2318 XSETINT (c, -1);
2319 goto exit;
2322 c = Faref (Vexecuting_kbd_macro, make_number (executing_kbd_macro_index));
2323 if (STRINGP (Vexecuting_kbd_macro)
2324 && (XINT (c) & 0x80) && (XUINT (c) <= 0xff))
2325 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
2327 executing_kbd_macro_index++;
2329 goto from_macro;
2332 if (!NILP (unread_switch_frame))
2334 c = unread_switch_frame;
2335 unread_switch_frame = Qnil;
2337 /* This event should make it into this_command_keys, and get echoed
2338 again, so we do not set `reread'. */
2339 goto reread_first;
2342 /* if redisplay was requested */
2343 if (commandflag >= 0)
2345 int echo_current = EQ (echo_message_buffer, echo_area_buffer[0]);
2347 /* If there is pending input, process any events which are not
2348 user-visible, such as X selection_request events. */
2349 if (input_pending
2350 || detect_input_pending_run_timers (0))
2351 swallow_events (0); /* may clear input_pending */
2353 /* Redisplay if no pending input. */
2354 while (!input_pending)
2356 if (help_echo_showing_p && !EQ (selected_window, minibuf_window))
2357 redisplay_preserve_echo_area (5);
2358 else
2359 redisplay ();
2361 if (!input_pending)
2362 /* Normal case: no input arrived during redisplay. */
2363 break;
2365 /* Input arrived and pre-empted redisplay.
2366 Process any events which are not user-visible. */
2367 swallow_events (0);
2368 /* If that cleared input_pending, try again to redisplay. */
2371 /* Prevent the redisplay we just did
2372 from messing up echoing of the input after the prompt. */
2373 if (commandflag == 0 && echo_current)
2374 echo_message_buffer = echo_area_buffer[0];
2378 /* Message turns off echoing unless more keystrokes turn it on again.
2380 The code in 20.x for the condition was
2382 1. echo_area_glyphs && *echo_area_glyphs
2383 2. && echo_area_glyphs != current_kboard->echobuf
2384 3. && ok_to_echo_at_next_pause != echo_area_glyphs
2386 (1) means there's a current message displayed
2388 (2) means it's not the message from echoing from the current
2389 kboard.
2391 (3) There's only one place in 20.x where ok_to_echo_at_next_pause
2392 is set to a non-null value. This is done in read_char and it is
2393 set to echo_area_glyphs after a call to echo_char. That means
2394 ok_to_echo_at_next_pause is either null or
2395 current_kboard->echobuf with the appropriate current_kboard at
2396 that time.
2398 So, condition (3) means in clear text ok_to_echo_at_next_pause
2399 must be either null, or the current message isn't from echoing at
2400 all, or it's from echoing from a different kboard than the
2401 current one. */
2403 if (/* There currently is something in the echo area. */
2404 !NILP (echo_area_buffer[0])
2405 && (/* And it's either not from echoing. */
2406 !EQ (echo_area_buffer[0], echo_message_buffer)
2407 /* Or it's an echo from a different kboard. */
2408 || echo_kboard != current_kboard
2409 /* Or we explicitly allow overwriting whatever there is. */
2410 || ok_to_echo_at_next_pause == NULL))
2411 cancel_echoing ();
2412 else
2413 echo_dash ();
2415 /* Try reading a character via menu prompting in the minibuf.
2416 Try this before the sit-for, because the sit-for
2417 would do the wrong thing if we are supposed to do
2418 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
2419 after a mouse event so don't try a minibuf menu. */
2420 c = Qnil;
2421 if (nmaps > 0 && INTERACTIVE
2422 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
2423 /* Don't bring up a menu if we already have another event. */
2424 && NILP (Vunread_command_events)
2425 && unread_command_char < 0
2426 && !detect_input_pending_run_timers (0))
2428 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
2430 if (INTEGERP (c) && XINT (c) == -2)
2431 return c; /* wrong_kboard_jmpbuf */
2433 if (! NILP (c))
2435 key_already_recorded = 1;
2436 goto non_reread_1;
2440 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
2441 We will do that below, temporarily for short sections of code,
2442 when appropriate. local_getcjmp must be in effect
2443 around any call to sit_for or kbd_buffer_get_event;
2444 it *must not* be in effect when we call redisplay. */
2446 jmpcount = SPECPDL_INDEX ();
2447 if (_setjmp (local_getcjmp))
2449 /* Handle quits while reading the keyboard. */
2450 /* We must have saved the outer value of getcjmp here,
2451 so restore it now. */
2452 restore_getcjmp (save_jump);
2453 unbind_to (jmpcount, Qnil);
2454 XSETINT (c, quit_char);
2455 internal_last_event_frame = selected_frame;
2456 Vlast_event_frame = internal_last_event_frame;
2457 /* If we report the quit char as an event,
2458 don't do so more than once. */
2459 if (!NILP (Vinhibit_quit))
2460 Vquit_flag = Qnil;
2463 KBOARD *kb = FRAME_KBOARD (XFRAME (selected_frame));
2464 if (kb != current_kboard)
2466 Lisp_Object last = KVAR (kb, kbd_queue);
2467 /* We shouldn't get here if we were in single-kboard mode! */
2468 if (single_kboard)
2469 abort ();
2470 if (CONSP (last))
2472 while (CONSP (XCDR (last)))
2473 last = XCDR (last);
2474 if (!NILP (XCDR (last)))
2475 abort ();
2477 if (!CONSP (last))
2478 KVAR (kb, kbd_queue) = Fcons (c, Qnil);
2479 else
2480 XSETCDR (last, Fcons (c, Qnil));
2481 kb->kbd_queue_has_data = 1;
2482 current_kboard = kb;
2483 /* This is going to exit from read_char
2484 so we had better get rid of this frame's stuff. */
2485 UNGCPRO;
2486 return make_number (-2); /* wrong_kboard_jmpbuf */
2489 goto non_reread;
2492 /* Start idle timers if no time limit is supplied. We don't do it
2493 if a time limit is supplied to avoid an infinite recursion in the
2494 situation where an idle timer calls `sit-for'. */
2496 if (!end_time)
2497 timer_start_idle ();
2499 /* If in middle of key sequence and minibuffer not active,
2500 start echoing if enough time elapses. */
2502 if (minibuf_level == 0
2503 && !end_time
2504 && !current_kboard->immediate_echo
2505 && this_command_key_count > 0
2506 && ! noninteractive
2507 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2508 && NILP (Fzerop (Vecho_keystrokes))
2509 && (/* No message. */
2510 NILP (echo_area_buffer[0])
2511 /* Or empty message. */
2512 || (BUF_BEG (XBUFFER (echo_area_buffer[0]))
2513 == BUF_Z (XBUFFER (echo_area_buffer[0])))
2514 /* Or already echoing from same kboard. */
2515 || (echo_kboard && ok_to_echo_at_next_pause == echo_kboard)
2516 /* Or not echoing before and echoing allowed. */
2517 || (!echo_kboard && ok_to_echo_at_next_pause)))
2519 /* After a mouse event, start echoing right away.
2520 This is because we are probably about to display a menu,
2521 and we don't want to delay before doing so. */
2522 if (EVENT_HAS_PARAMETERS (prev_event))
2523 echo_now ();
2524 else
2526 Lisp_Object tem0;
2528 save_getcjmp (save_jump);
2529 restore_getcjmp (local_getcjmp);
2530 tem0 = sit_for (Vecho_keystrokes, 1, 1);
2531 restore_getcjmp (save_jump);
2532 if (EQ (tem0, Qt)
2533 && ! CONSP (Vunread_command_events))
2534 echo_now ();
2538 /* Maybe auto save due to number of keystrokes. */
2540 if (commandflag != 0
2541 && auto_save_interval > 0
2542 && num_nonmacro_input_events - last_auto_save > max (auto_save_interval, 20)
2543 && !detect_input_pending_run_timers (0))
2545 Fdo_auto_save (Qnil, Qnil);
2546 /* Hooks can actually change some buffers in auto save. */
2547 redisplay ();
2550 /* Try reading using an X menu.
2551 This is never confused with reading using the minibuf
2552 because the recursive call of read_char in read_char_minibuf_menu_prompt
2553 does not pass on any keymaps. */
2555 if (nmaps > 0 && INTERACTIVE
2556 && !NILP (prev_event)
2557 && EVENT_HAS_PARAMETERS (prev_event)
2558 && !EQ (XCAR (prev_event), Qmenu_bar)
2559 && !EQ (XCAR (prev_event), Qtool_bar)
2560 /* Don't bring up a menu if we already have another event. */
2561 && NILP (Vunread_command_events)
2562 && unread_command_char < 0)
2564 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
2566 /* Now that we have read an event, Emacs is not idle. */
2567 if (!end_time)
2568 timer_stop_idle ();
2570 goto exit;
2573 /* Maybe autosave and/or garbage collect due to idleness. */
2575 if (INTERACTIVE && NILP (c))
2577 int delay_level;
2578 EMACS_INT buffer_size;
2580 /* Slow down auto saves logarithmically in size of current buffer,
2581 and garbage collect while we're at it. */
2582 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
2583 last_non_minibuf_size = Z - BEG;
2584 buffer_size = (last_non_minibuf_size >> 8) + 1;
2585 delay_level = 0;
2586 while (buffer_size > 64)
2587 delay_level++, buffer_size -= buffer_size >> 2;
2588 if (delay_level < 4) delay_level = 4;
2589 /* delay_level is 4 for files under around 50k, 7 at 100k,
2590 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
2592 /* Auto save if enough time goes by without input. */
2593 if (commandflag != 0
2594 && num_nonmacro_input_events > last_auto_save
2595 && INTEGERP (Vauto_save_timeout)
2596 && XINT (Vauto_save_timeout) > 0)
2598 Lisp_Object tem0;
2599 int timeout = delay_level * XFASTINT (Vauto_save_timeout) / 4;
2601 save_getcjmp (save_jump);
2602 restore_getcjmp (local_getcjmp);
2603 tem0 = sit_for (make_number (timeout), 1, 1);
2604 restore_getcjmp (save_jump);
2606 if (EQ (tem0, Qt)
2607 && ! CONSP (Vunread_command_events))
2609 Fdo_auto_save (Qnil, Qnil);
2611 /* If we have auto-saved and there is still no input
2612 available, garbage collect if there has been enough
2613 consing going on to make it worthwhile. */
2614 if (!detect_input_pending_run_timers (0)
2615 && consing_since_gc > gc_cons_threshold / 2)
2616 Fgarbage_collect ();
2618 redisplay ();
2623 /* Notify the caller if an autosave hook, or a timer, sentinel or
2624 filter in the sit_for calls above have changed the current
2625 kboard. This could happen if they use the minibuffer or start a
2626 recursive edit, like the fancy splash screen in server.el's
2627 filter. If this longjmp wasn't here, read_key_sequence would
2628 interpret the next key sequence using the wrong translation
2629 tables and function keymaps. */
2630 if (NILP (c) && current_kboard != orig_kboard)
2632 UNGCPRO;
2633 return make_number (-2); /* wrong_kboard_jmpbuf */
2636 /* If this has become non-nil here, it has been set by a timer
2637 or sentinel or filter. */
2638 if (CONSP (Vunread_command_events))
2640 c = XCAR (Vunread_command_events);
2641 Vunread_command_events = XCDR (Vunread_command_events);
2644 /* Read something from current KBOARD's side queue, if possible. */
2646 if (NILP (c))
2648 if (current_kboard->kbd_queue_has_data)
2650 if (!CONSP (KVAR (current_kboard, kbd_queue)))
2651 abort ();
2652 c = XCAR (KVAR (current_kboard, kbd_queue));
2653 KVAR (current_kboard, kbd_queue)
2654 = XCDR (KVAR (current_kboard, kbd_queue));
2655 if (NILP (KVAR (current_kboard, kbd_queue)))
2656 current_kboard->kbd_queue_has_data = 0;
2657 input_pending = readable_events (0);
2658 if (EVENT_HAS_PARAMETERS (c)
2659 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
2660 internal_last_event_frame = XCAR (XCDR (c));
2661 Vlast_event_frame = internal_last_event_frame;
2665 /* If current_kboard's side queue is empty check the other kboards.
2666 If one of them has data that we have not yet seen here,
2667 switch to it and process the data waiting for it.
2669 Note: if the events queued up for another kboard
2670 have already been seen here, and therefore are not a complete command,
2671 the kbd_queue_has_data field is 0, so we skip that kboard here.
2672 That's to avoid an infinite loop switching between kboards here. */
2673 if (NILP (c) && !single_kboard)
2675 KBOARD *kb;
2676 for (kb = all_kboards; kb; kb = kb->next_kboard)
2677 if (kb->kbd_queue_has_data)
2679 current_kboard = kb;
2680 /* This is going to exit from read_char
2681 so we had better get rid of this frame's stuff. */
2682 UNGCPRO;
2683 return make_number (-2); /* wrong_kboard_jmpbuf */
2687 wrong_kboard:
2689 STOP_POLLING;
2691 /* Finally, we read from the main queue,
2692 and if that gives us something we can't use yet, we put it on the
2693 appropriate side queue and try again. */
2695 if (NILP (c))
2697 KBOARD *kb IF_LINT (= NULL);
2699 if (end_time)
2701 EMACS_TIME now;
2702 EMACS_GET_TIME (now);
2703 if (EMACS_TIME_GE (now, *end_time))
2704 goto exit;
2707 /* Actually read a character, waiting if necessary. */
2708 save_getcjmp (save_jump);
2709 restore_getcjmp (local_getcjmp);
2710 if (!end_time)
2711 timer_start_idle ();
2712 c = kbd_buffer_get_event (&kb, used_mouse_menu, end_time);
2713 restore_getcjmp (save_jump);
2715 if (! NILP (c) && (kb != current_kboard))
2717 Lisp_Object last = KVAR (kb, kbd_queue);
2718 if (CONSP (last))
2720 while (CONSP (XCDR (last)))
2721 last = XCDR (last);
2722 if (!NILP (XCDR (last)))
2723 abort ();
2725 if (!CONSP (last))
2726 KVAR (kb, kbd_queue) = Fcons (c, Qnil);
2727 else
2728 XSETCDR (last, Fcons (c, Qnil));
2729 kb->kbd_queue_has_data = 1;
2730 c = Qnil;
2731 if (single_kboard)
2732 goto wrong_kboard;
2733 current_kboard = kb;
2734 /* This is going to exit from read_char
2735 so we had better get rid of this frame's stuff. */
2736 UNGCPRO;
2737 return make_number (-2);
2741 /* Terminate Emacs in batch mode if at eof. */
2742 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
2743 Fkill_emacs (make_number (1));
2745 if (INTEGERP (c))
2747 /* Add in any extra modifiers, where appropriate. */
2748 if ((extra_keyboard_modifiers & CHAR_CTL)
2749 || ((extra_keyboard_modifiers & 0177) < ' '
2750 && (extra_keyboard_modifiers & 0177) != 0))
2751 XSETINT (c, make_ctrl_char (XINT (c)));
2753 /* Transfer any other modifier bits directly from
2754 extra_keyboard_modifiers to c. Ignore the actual character code
2755 in the low 16 bits of extra_keyboard_modifiers. */
2756 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
2759 non_reread:
2761 if (!end_time)
2762 timer_stop_idle ();
2763 RESUME_POLLING;
2765 if (NILP (c))
2767 if (commandflag >= 0
2768 && !input_pending && !detect_input_pending_run_timers (0))
2769 redisplay ();
2771 goto wrong_kboard;
2774 non_reread_1:
2776 /* Buffer switch events are only for internal wakeups
2777 so don't show them to the user.
2778 Also, don't record a key if we already did. */
2779 if (BUFFERP (c) || key_already_recorded)
2780 goto exit;
2782 /* Process special events within read_char
2783 and loop around to read another event. */
2784 save = Vquit_flag;
2785 Vquit_flag = Qnil;
2786 tem = access_keymap (get_keymap (Vspecial_event_map, 0, 1), c, 0, 0, 1);
2787 Vquit_flag = save;
2789 if (!NILP (tem))
2791 struct buffer *prev_buffer = current_buffer;
2792 #if 0 /* This shouldn't be necessary anymore. --lorentey */
2793 int was_locked = single_kboard;
2794 int count = SPECPDL_INDEX ();
2795 record_single_kboard_state ();
2796 #endif
2798 last_input_event = c;
2799 Fcommand_execute (tem, Qnil, Fvector (1, &last_input_event), Qt);
2801 if (CONSP (c) && EQ (XCAR (c), Qselect_window) && !end_time)
2802 /* We stopped being idle for this event; undo that. This
2803 prevents automatic window selection (under
2804 mouse_autoselect_window from acting as a real input event, for
2805 example banishing the mouse under mouse-avoidance-mode. */
2806 timer_resume_idle ();
2808 #if 0 /* This shouldn't be necessary anymore. --lorentey */
2809 /* Resume allowing input from any kboard, if that was true before. */
2810 if (!was_locked)
2811 any_kboard_state ();
2812 unbind_to (count, Qnil);
2813 #endif
2815 if (current_buffer != prev_buffer)
2817 /* The command may have changed the keymaps. Pretend there
2818 is input in another keyboard and return. This will
2819 recalculate keymaps. */
2820 c = make_number (-2);
2821 goto exit;
2823 else
2824 goto retry;
2827 /* Handle things that only apply to characters. */
2828 if (INTEGERP (c))
2830 /* If kbd_buffer_get_event gave us an EOF, return that. */
2831 if (XINT (c) == -1)
2832 goto exit;
2834 if ((STRINGP (KVAR (current_kboard, Vkeyboard_translate_table))
2835 && SCHARS (KVAR (current_kboard, Vkeyboard_translate_table)) > (unsigned) XFASTINT (c))
2836 || (VECTORP (KVAR (current_kboard, Vkeyboard_translate_table))
2837 && XVECTOR (KVAR (current_kboard, Vkeyboard_translate_table))->size > (unsigned) XFASTINT (c))
2838 || (CHAR_TABLE_P (KVAR (current_kboard, Vkeyboard_translate_table))
2839 && CHARACTERP (c)))
2841 Lisp_Object d;
2842 d = Faref (KVAR (current_kboard, Vkeyboard_translate_table), c);
2843 /* nil in keyboard-translate-table means no translation. */
2844 if (!NILP (d))
2845 c = d;
2849 /* If this event is a mouse click in the menu bar,
2850 return just menu-bar for now. Modify the mouse click event
2851 so we won't do this twice, then queue it up. */
2852 if (EVENT_HAS_PARAMETERS (c)
2853 && CONSP (XCDR (c))
2854 && CONSP (EVENT_START (c))
2855 && CONSP (XCDR (EVENT_START (c))))
2857 Lisp_Object posn;
2859 posn = POSN_POSN (EVENT_START (c));
2860 /* Handle menu-bar events:
2861 insert the dummy prefix event `menu-bar'. */
2862 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
2864 /* Change menu-bar to (menu-bar) as the event "position". */
2865 POSN_SET_POSN (EVENT_START (c), Fcons (posn, Qnil));
2867 also_record = c;
2868 Vunread_command_events = Fcons (c, Vunread_command_events);
2869 c = posn;
2873 /* Store these characters into recent_keys, the dribble file if any,
2874 and the keyboard macro being defined, if any. */
2875 record_char (c);
2876 if (! NILP (also_record))
2877 record_char (also_record);
2879 /* Wipe the echo area.
2880 But first, if we are about to use an input method,
2881 save the echo area contents for it to refer to. */
2882 if (INTEGERP (c)
2883 && ! NILP (Vinput_method_function)
2884 && (unsigned) XINT (c) >= ' '
2885 && (unsigned) XINT (c) != 127
2886 && (unsigned) XINT (c) < 256)
2888 previous_echo_area_message = Fcurrent_message ();
2889 Vinput_method_previous_message = previous_echo_area_message;
2892 /* Now wipe the echo area, except for help events which do their
2893 own stuff with the echo area. */
2894 if (!CONSP (c)
2895 || (!(EQ (Qhelp_echo, XCAR (c)))
2896 && !(EQ (Qswitch_frame, XCAR (c)))))
2898 if (!NILP (echo_area_buffer[0]))
2899 safe_run_hooks (Qecho_area_clear_hook);
2900 clear_message (1, 0);
2903 reread_for_input_method:
2904 from_macro:
2905 /* Pass this to the input method, if appropriate. */
2906 if (INTEGERP (c)
2907 && ! NILP (Vinput_method_function)
2908 /* Don't run the input method within a key sequence,
2909 after the first event of the key sequence. */
2910 && NILP (prev_event)
2911 && (unsigned) XINT (c) >= ' '
2912 && (unsigned) XINT (c) != 127
2913 && (unsigned) XINT (c) < 256)
2915 Lisp_Object keys;
2916 int key_count, key_count_reset;
2917 struct gcpro inner_gcpro1;
2918 int count = SPECPDL_INDEX ();
2920 /* Save the echo status. */
2921 int saved_immediate_echo = current_kboard->immediate_echo;
2922 struct kboard *saved_ok_to_echo = ok_to_echo_at_next_pause;
2923 Lisp_Object saved_echo_string = KVAR (current_kboard, echo_string);
2924 int saved_echo_after_prompt = current_kboard->echo_after_prompt;
2926 #if 0
2927 if (before_command_restore_flag)
2929 this_command_key_count = before_command_key_count_1;
2930 if (this_command_key_count < this_single_command_key_start)
2931 this_single_command_key_start = this_command_key_count;
2932 echo_truncate (before_command_echo_length_1);
2933 before_command_restore_flag = 0;
2935 #endif
2937 /* Save the this_command_keys status. */
2938 key_count = this_command_key_count;
2939 key_count_reset = this_command_key_count_reset;
2941 if (key_count > 0)
2942 keys = Fcopy_sequence (this_command_keys);
2943 else
2944 keys = Qnil;
2945 GCPRO1_VAR (keys, inner_gcpro);
2947 /* Clear out this_command_keys. */
2948 this_command_key_count = 0;
2949 this_command_key_count_reset = 0;
2951 /* Now wipe the echo area. */
2952 if (!NILP (echo_area_buffer[0]))
2953 safe_run_hooks (Qecho_area_clear_hook);
2954 clear_message (1, 0);
2955 echo_truncate (0);
2957 /* If we are not reading a key sequence,
2958 never use the echo area. */
2959 if (maps == 0)
2961 specbind (Qinput_method_use_echo_area, Qt);
2964 /* Call the input method. */
2965 tem = call1 (Vinput_method_function, c);
2967 tem = unbind_to (count, tem);
2969 /* Restore the saved echoing state
2970 and this_command_keys state. */
2971 this_command_key_count = key_count;
2972 this_command_key_count_reset = key_count_reset;
2973 if (key_count > 0)
2974 this_command_keys = keys;
2976 cancel_echoing ();
2977 ok_to_echo_at_next_pause = saved_ok_to_echo;
2978 KVAR (current_kboard, echo_string) = saved_echo_string;
2979 current_kboard->echo_after_prompt = saved_echo_after_prompt;
2980 if (saved_immediate_echo)
2981 echo_now ();
2983 UNGCPRO_VAR (inner_gcpro);
2985 /* The input method can return no events. */
2986 if (! CONSP (tem))
2988 /* Bring back the previous message, if any. */
2989 if (! NILP (previous_echo_area_message))
2990 message_with_string ("%s", previous_echo_area_message, 0);
2991 goto retry;
2993 /* It returned one event or more. */
2994 c = XCAR (tem);
2995 Vunread_post_input_method_events
2996 = nconc2 (XCDR (tem), Vunread_post_input_method_events);
2999 reread_first:
3001 /* Display help if not echoing. */
3002 if (CONSP (c) && EQ (XCAR (c), Qhelp_echo))
3004 /* (help-echo FRAME HELP WINDOW OBJECT POS). */
3005 Lisp_Object help, object, position, window, htem;
3007 htem = Fcdr (XCDR (c));
3008 help = Fcar (htem);
3009 htem = Fcdr (htem);
3010 window = Fcar (htem);
3011 htem = Fcdr (htem);
3012 object = Fcar (htem);
3013 htem = Fcdr (htem);
3014 position = Fcar (htem);
3016 show_help_echo (help, window, object, position, 0);
3018 /* We stopped being idle for this event; undo that. */
3019 if (!end_time)
3020 timer_resume_idle ();
3021 goto retry;
3024 if ((! reread || this_command_key_count == 0
3025 || this_command_key_count_reset)
3026 && !end_time)
3029 /* Don't echo mouse motion events. */
3030 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
3031 && NILP (Fzerop (Vecho_keystrokes))
3032 && ! (EVENT_HAS_PARAMETERS (c)
3033 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
3035 echo_char (c);
3036 if (! NILP (also_record))
3037 echo_char (also_record);
3038 /* Once we reread a character, echoing can happen
3039 the next time we pause to read a new one. */
3040 ok_to_echo_at_next_pause = current_kboard;
3043 /* Record this character as part of the current key. */
3044 add_command_key (c);
3045 if (! NILP (also_record))
3046 add_command_key (also_record);
3049 last_input_event = c;
3050 num_input_events++;
3052 /* Process the help character specially if enabled */
3053 if (!NILP (Vhelp_form) && help_char_p (c))
3055 Lisp_Object tem0;
3056 int count = SPECPDL_INDEX ();
3058 help_form_saved_window_configs
3059 = Fcons (Fcurrent_window_configuration (Qnil),
3060 help_form_saved_window_configs);
3061 record_unwind_protect (read_char_help_form_unwind, Qnil);
3063 tem0 = Feval (Vhelp_form, Qnil);
3064 if (STRINGP (tem0))
3065 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
3067 cancel_echoing ();
3070 c = read_char (0, 0, 0, Qnil, 0, NULL);
3071 if (EVENT_HAS_PARAMETERS (c)
3072 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_click))
3073 XSETCAR (help_form_saved_window_configs, Qnil);
3075 while (BUFFERP (c));
3076 /* Remove the help from the frame */
3077 unbind_to (count, Qnil);
3079 redisplay ();
3080 if (EQ (c, make_number (040)))
3082 cancel_echoing ();
3084 c = read_char (0, 0, 0, Qnil, 0, NULL);
3085 while (BUFFERP (c));
3089 exit:
3090 RESUME_POLLING;
3091 RETURN_UNGCPRO (c);
3094 /* Record a key that came from a mouse menu.
3095 Record it for echoing, for this-command-keys, and so on. */
3097 static void
3098 record_menu_key (Lisp_Object c)
3100 /* Wipe the echo area. */
3101 clear_message (1, 0);
3103 record_char (c);
3105 #if 0
3106 before_command_key_count = this_command_key_count;
3107 before_command_echo_length = echo_length ();
3108 #endif
3110 /* Don't echo mouse motion events. */
3111 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
3112 && NILP (Fzerop (Vecho_keystrokes)))
3114 echo_char (c);
3116 /* Once we reread a character, echoing can happen
3117 the next time we pause to read a new one. */
3118 ok_to_echo_at_next_pause = 0;
3121 /* Record this character as part of the current key. */
3122 add_command_key (c);
3124 /* Re-reading in the middle of a command */
3125 last_input_event = c;
3126 num_input_events++;
3129 /* Return 1 if should recognize C as "the help character". */
3132 help_char_p (Lisp_Object c)
3134 Lisp_Object tail;
3136 if (EQ (c, Vhelp_char))
3137 return 1;
3138 for (tail = Vhelp_event_list; CONSP (tail); tail = XCDR (tail))
3139 if (EQ (c, XCAR (tail)))
3140 return 1;
3141 return 0;
3144 /* Record the input event C in various ways. */
3146 static void
3147 record_char (Lisp_Object c)
3149 int recorded = 0;
3151 if (CONSP (c) && (EQ (XCAR (c), Qhelp_echo) || EQ (XCAR (c), Qmouse_movement)))
3153 /* To avoid filling recent_keys with help-echo and mouse-movement
3154 events, we filter out repeated help-echo events, only store the
3155 first and last in a series of mouse-movement events, and don't
3156 store repeated help-echo events which are only separated by
3157 mouse-movement events. */
3159 Lisp_Object ev1, ev2, ev3;
3160 int ix1, ix2, ix3;
3162 if ((ix1 = recent_keys_index - 1) < 0)
3163 ix1 = NUM_RECENT_KEYS - 1;
3164 ev1 = AREF (recent_keys, ix1);
3166 if ((ix2 = ix1 - 1) < 0)
3167 ix2 = NUM_RECENT_KEYS - 1;
3168 ev2 = AREF (recent_keys, ix2);
3170 if ((ix3 = ix2 - 1) < 0)
3171 ix3 = NUM_RECENT_KEYS - 1;
3172 ev3 = AREF (recent_keys, ix3);
3174 if (EQ (XCAR (c), Qhelp_echo))
3176 /* Don't record `help-echo' in recent_keys unless it shows some help
3177 message, and a different help than the previously recorded
3178 event. */
3179 Lisp_Object help, last_help;
3181 help = Fcar_safe (Fcdr_safe (XCDR (c)));
3182 if (!STRINGP (help))
3183 recorded = 1;
3184 else if (CONSP (ev1) && EQ (XCAR (ev1), Qhelp_echo)
3185 && (last_help = Fcar_safe (Fcdr_safe (XCDR (ev1))), EQ (last_help, help)))
3186 recorded = 1;
3187 else if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
3188 && CONSP (ev2) && EQ (XCAR (ev2), Qhelp_echo)
3189 && (last_help = Fcar_safe (Fcdr_safe (XCDR (ev2))), EQ (last_help, help)))
3190 recorded = -1;
3191 else if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
3192 && CONSP (ev2) && EQ (XCAR (ev2), Qmouse_movement)
3193 && CONSP (ev3) && EQ (XCAR (ev3), Qhelp_echo)
3194 && (last_help = Fcar_safe (Fcdr_safe (XCDR (ev3))), EQ (last_help, help)))
3195 recorded = -2;
3197 else if (EQ (XCAR (c), Qmouse_movement))
3199 /* Only record one pair of `mouse-movement' on a window in recent_keys.
3200 So additional mouse movement events replace the last element. */
3201 Lisp_Object last_window, window;
3203 window = Fcar_safe (Fcar_safe (XCDR (c)));
3204 if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
3205 && (last_window = Fcar_safe (Fcar_safe (XCDR (ev1))), EQ (last_window, window))
3206 && CONSP (ev2) && EQ (XCAR (ev2), Qmouse_movement)
3207 && (last_window = Fcar_safe (Fcar_safe (XCDR (ev2))), EQ (last_window, window)))
3209 ASET (recent_keys, ix1, c);
3210 recorded = 1;
3214 else
3215 store_kbd_macro_char (c);
3217 if (!recorded)
3219 total_keys++;
3220 ASET (recent_keys, recent_keys_index, c);
3221 if (++recent_keys_index >= NUM_RECENT_KEYS)
3222 recent_keys_index = 0;
3224 else if (recorded < 0)
3226 /* We need to remove one or two events from recent_keys.
3227 To do this, we simply put nil at those events and move the
3228 recent_keys_index backwards over those events. Usually,
3229 users will never see those nil events, as they will be
3230 overwritten by the command keys entered to see recent_keys
3231 (e.g. C-h l). */
3233 while (recorded++ < 0 && total_keys > 0)
3235 if (total_keys < NUM_RECENT_KEYS)
3236 total_keys--;
3237 if (--recent_keys_index < 0)
3238 recent_keys_index = NUM_RECENT_KEYS - 1;
3239 ASET (recent_keys, recent_keys_index, Qnil);
3243 num_nonmacro_input_events++;
3245 /* Write c to the dribble file. If c is a lispy event, write
3246 the event's symbol to the dribble file, in <brackets>. Bleaugh.
3247 If you, dear reader, have a better idea, you've got the source. :-) */
3248 if (dribble)
3250 BLOCK_INPUT;
3251 if (INTEGERP (c))
3253 if (XUINT (c) < 0x100)
3254 putc (XINT (c), dribble);
3255 else
3256 fprintf (dribble, " 0x%x", (int) XUINT (c));
3258 else
3260 Lisp_Object dribblee;
3262 /* If it's a structured event, take the event header. */
3263 dribblee = EVENT_HEAD (c);
3265 if (SYMBOLP (dribblee))
3267 putc ('<', dribble);
3268 fwrite (SDATA (SYMBOL_NAME (dribblee)), sizeof (char),
3269 SBYTES (SYMBOL_NAME (dribblee)),
3270 dribble);
3271 putc ('>', dribble);
3275 fflush (dribble);
3276 UNBLOCK_INPUT;
3280 Lisp_Object
3281 print_help (Lisp_Object object)
3283 struct buffer *old = current_buffer;
3284 Fprinc (object, Qnil);
3285 set_buffer_internal (XBUFFER (Vstandard_output));
3286 call0 (intern ("help-mode"));
3287 set_buffer_internal (old);
3288 return Qnil;
3291 /* Copy out or in the info on where C-g should throw to.
3292 This is used when running Lisp code from within get_char,
3293 in case get_char is called recursively.
3294 See read_process_output. */
3296 static void
3297 save_getcjmp (jmp_buf temp)
3299 memcpy (temp, getcjmp, sizeof getcjmp);
3302 static void
3303 restore_getcjmp (jmp_buf temp)
3305 memcpy (getcjmp, temp, sizeof getcjmp);
3308 /* Low level keyboard/mouse input.
3309 kbd_buffer_store_event places events in kbd_buffer, and
3310 kbd_buffer_get_event retrieves them. */
3312 /* Return true if there are any events in the queue that read-char
3313 would return. If this returns false, a read-char would block. */
3314 static int
3315 readable_events (int flags)
3317 if (flags & READABLE_EVENTS_DO_TIMERS_NOW)
3318 timer_check (1);
3320 /* If the buffer contains only FOCUS_IN_EVENT events, and
3321 READABLE_EVENTS_FILTER_EVENTS is set, report it as empty. */
3322 if (kbd_fetch_ptr != kbd_store_ptr)
3324 if (flags & (READABLE_EVENTS_FILTER_EVENTS
3325 #ifdef USE_TOOLKIT_SCROLL_BARS
3326 | READABLE_EVENTS_IGNORE_SQUEEZABLES
3327 #endif
3330 struct input_event *event;
3332 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3333 ? kbd_fetch_ptr
3334 : kbd_buffer);
3338 if (!(
3339 #ifdef USE_TOOLKIT_SCROLL_BARS
3340 (flags & READABLE_EVENTS_FILTER_EVENTS) &&
3341 #endif
3342 event->kind == FOCUS_IN_EVENT)
3343 #ifdef USE_TOOLKIT_SCROLL_BARS
3344 && !((flags & READABLE_EVENTS_IGNORE_SQUEEZABLES)
3345 && event->kind == SCROLL_BAR_CLICK_EVENT
3346 && event->part == scroll_bar_handle
3347 && event->modifiers == 0)
3348 #endif
3350 return 1;
3351 event++;
3352 if (event == kbd_buffer + KBD_BUFFER_SIZE)
3353 event = kbd_buffer;
3355 while (event != kbd_store_ptr);
3357 else
3358 return 1;
3361 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
3362 if (!(flags & READABLE_EVENTS_IGNORE_SQUEEZABLES)
3363 && !NILP (do_mouse_tracking) && some_mouse_moved ())
3364 return 1;
3365 #endif
3366 if (single_kboard)
3368 if (current_kboard->kbd_queue_has_data)
3369 return 1;
3371 else
3373 KBOARD *kb;
3374 for (kb = all_kboards; kb; kb = kb->next_kboard)
3375 if (kb->kbd_queue_has_data)
3376 return 1;
3378 return 0;
3381 /* Set this for debugging, to have a way to get out */
3382 int stop_character;
3384 static KBOARD *
3385 event_to_kboard (struct input_event *event)
3387 Lisp_Object frame;
3388 frame = event->frame_or_window;
3389 if (CONSP (frame))
3390 frame = XCAR (frame);
3391 else if (WINDOWP (frame))
3392 frame = WINDOW_FRAME (XWINDOW (frame));
3394 /* There are still some events that don't set this field.
3395 For now, just ignore the problem.
3396 Also ignore dead frames here. */
3397 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
3398 return 0;
3399 else
3400 return FRAME_KBOARD (XFRAME (frame));
3403 #ifdef subprocesses
3404 /* Return the number of slots occupied in kbd_buffer. */
3406 static int
3407 kbd_buffer_nr_stored (void)
3409 return kbd_fetch_ptr == kbd_store_ptr
3411 : (kbd_fetch_ptr < kbd_store_ptr
3412 ? kbd_store_ptr - kbd_fetch_ptr
3413 : ((kbd_buffer + KBD_BUFFER_SIZE) - kbd_fetch_ptr
3414 + (kbd_store_ptr - kbd_buffer)));
3416 #endif /* Store an event obtained at interrupt level into kbd_buffer, fifo */
3418 void
3419 kbd_buffer_store_event (register struct input_event *event)
3421 kbd_buffer_store_event_hold (event, 0);
3424 /* Store EVENT obtained at interrupt level into kbd_buffer, fifo.
3426 If HOLD_QUIT is 0, just stuff EVENT into the fifo.
3427 Else, if HOLD_QUIT.kind != NO_EVENT, discard EVENT.
3428 Else, if EVENT is a quit event, store the quit event
3429 in HOLD_QUIT, and return (thus ignoring further events).
3431 This is used in read_avail_input to postpone the processing
3432 of the quit event until all subsequent input events have been
3433 parsed (and discarded).
3436 void
3437 kbd_buffer_store_event_hold (register struct input_event *event,
3438 struct input_event *hold_quit)
3440 if (event->kind == NO_EVENT)
3441 abort ();
3443 if (hold_quit && hold_quit->kind != NO_EVENT)
3444 return;
3446 if (event->kind == ASCII_KEYSTROKE_EVENT)
3448 register int c = event->code & 0377;
3450 if (event->modifiers & ctrl_modifier)
3451 c = make_ctrl_char (c);
3453 c |= (event->modifiers
3454 & (meta_modifier | alt_modifier
3455 | hyper_modifier | super_modifier));
3457 if (c == quit_char)
3459 KBOARD *kb = FRAME_KBOARD (XFRAME (event->frame_or_window));
3460 struct input_event *sp;
3462 if (single_kboard && kb != current_kboard)
3464 KVAR (kb, kbd_queue)
3465 = Fcons (make_lispy_switch_frame (event->frame_or_window),
3466 Fcons (make_number (c), Qnil));
3467 kb->kbd_queue_has_data = 1;
3468 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3470 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3471 sp = kbd_buffer;
3473 if (event_to_kboard (sp) == kb)
3475 sp->kind = NO_EVENT;
3476 sp->frame_or_window = Qnil;
3477 sp->arg = Qnil;
3480 return;
3483 if (hold_quit)
3485 memcpy (hold_quit, event, sizeof (*event));
3486 return;
3489 /* If this results in a quit_char being returned to Emacs as
3490 input, set Vlast_event_frame properly. If this doesn't
3491 get returned to Emacs as an event, the next event read
3492 will set Vlast_event_frame again, so this is safe to do. */
3494 Lisp_Object focus;
3496 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
3497 if (NILP (focus))
3498 focus = event->frame_or_window;
3499 internal_last_event_frame = focus;
3500 Vlast_event_frame = focus;
3503 last_event_timestamp = event->timestamp;
3504 handle_interrupt ();
3505 return;
3508 if (c && c == stop_character)
3510 sys_suspend ();
3511 return;
3514 /* Don't insert two BUFFER_SWITCH_EVENT's in a row.
3515 Just ignore the second one. */
3516 else if (event->kind == BUFFER_SWITCH_EVENT
3517 && kbd_fetch_ptr != kbd_store_ptr
3518 && ((kbd_store_ptr == kbd_buffer
3519 ? kbd_buffer + KBD_BUFFER_SIZE - 1
3520 : kbd_store_ptr - 1)->kind) == BUFFER_SWITCH_EVENT)
3521 return;
3523 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
3524 kbd_store_ptr = kbd_buffer;
3526 /* Don't let the very last slot in the buffer become full,
3527 since that would make the two pointers equal,
3528 and that is indistinguishable from an empty buffer.
3529 Discard the event if it would fill the last slot. */
3530 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
3532 *kbd_store_ptr = *event;
3533 ++kbd_store_ptr;
3534 #ifdef subprocesses
3535 if (kbd_buffer_nr_stored () > KBD_BUFFER_SIZE/2 && ! kbd_on_hold_p ())
3537 /* Don't read keyboard input until we have processed kbd_buffer.
3538 This happens when pasting text longer than KBD_BUFFER_SIZE/2. */
3539 hold_keyboard_input ();
3540 #ifdef SIGIO
3541 if (!noninteractive)
3542 signal (SIGIO, SIG_IGN);
3543 #endif
3544 stop_polling ();
3546 #endif /* subprocesses */
3549 /* If we're inside while-no-input, and this event qualifies
3550 as input, set quit-flag to cause an interrupt. */
3551 if (!NILP (Vthrow_on_input)
3552 && event->kind != FOCUS_IN_EVENT
3553 && event->kind != HELP_EVENT
3554 && event->kind != DEICONIFY_EVENT)
3556 Vquit_flag = Vthrow_on_input;
3557 /* If we're inside a function that wants immediate quits,
3558 do it now. */
3559 if (immediate_quit && NILP (Vinhibit_quit))
3561 immediate_quit = 0;
3562 sigfree ();
3563 QUIT;
3569 /* Put an input event back in the head of the event queue. */
3571 void
3572 kbd_buffer_unget_event (register struct input_event *event)
3574 if (kbd_fetch_ptr == kbd_buffer)
3575 kbd_fetch_ptr = kbd_buffer + KBD_BUFFER_SIZE;
3577 /* Don't let the very last slot in the buffer become full, */
3578 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
3580 --kbd_fetch_ptr;
3581 *kbd_fetch_ptr = *event;
3586 /* Generate a HELP_EVENT input_event and store it in the keyboard
3587 buffer.
3589 HELP is the help form.
3591 FRAME and WINDOW are the frame and window where the help is
3592 generated. OBJECT is the Lisp object where the help was found (a
3593 buffer, a string, an overlay, or nil if neither from a string nor
3594 from a buffer). POS is the position within OBJECT where the help
3595 was found. */
3597 void
3598 gen_help_event (Lisp_Object help, Lisp_Object frame, Lisp_Object window,
3599 Lisp_Object object, EMACS_INT pos)
3601 struct input_event event;
3603 EVENT_INIT (event);
3605 event.kind = HELP_EVENT;
3606 event.frame_or_window = frame;
3607 event.arg = object;
3608 event.x = WINDOWP (window) ? window : frame;
3609 event.y = help;
3610 event.code = pos;
3611 kbd_buffer_store_event (&event);
3615 /* Store HELP_EVENTs for HELP on FRAME in the input queue. */
3617 void
3618 kbd_buffer_store_help_event (Lisp_Object frame, Lisp_Object help)
3620 struct input_event event;
3622 event.kind = HELP_EVENT;
3623 event.frame_or_window = frame;
3624 event.arg = Qnil;
3625 event.x = Qnil;
3626 event.y = help;
3627 event.code = 0;
3628 kbd_buffer_store_event (&event);
3632 /* Discard any mouse events in the event buffer by setting them to
3633 NO_EVENT. */
3634 void
3635 discard_mouse_events (void)
3637 struct input_event *sp;
3638 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3640 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3641 sp = kbd_buffer;
3643 if (sp->kind == MOUSE_CLICK_EVENT
3644 || sp->kind == WHEEL_EVENT
3645 || sp->kind == HORIZ_WHEEL_EVENT
3646 #ifdef HAVE_GPM
3647 || sp->kind == GPM_CLICK_EVENT
3648 #endif
3649 || sp->kind == SCROLL_BAR_CLICK_EVENT)
3651 sp->kind = NO_EVENT;
3657 /* Return non-zero if there are any real events waiting in the event
3658 buffer, not counting `NO_EVENT's.
3660 If DISCARD is non-zero, discard NO_EVENT events at the front of
3661 the input queue, possibly leaving the input queue empty if there
3662 are no real input events. */
3665 kbd_buffer_events_waiting (int discard)
3667 struct input_event *sp;
3669 for (sp = kbd_fetch_ptr;
3670 sp != kbd_store_ptr && sp->kind == NO_EVENT;
3671 ++sp)
3673 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3674 sp = kbd_buffer;
3677 if (discard)
3678 kbd_fetch_ptr = sp;
3680 return sp != kbd_store_ptr && sp->kind != NO_EVENT;
3684 /* Clear input event EVENT. */
3686 static INLINE void
3687 clear_event (struct input_event *event)
3689 event->kind = NO_EVENT;
3693 /* Read one event from the event buffer, waiting if necessary.
3694 The value is a Lisp object representing the event.
3695 The value is nil for an event that should be ignored,
3696 or that was handled here.
3697 We always read and discard one event. */
3699 static Lisp_Object
3700 kbd_buffer_get_event (KBOARD **kbp,
3701 int *used_mouse_menu,
3702 struct timeval *end_time)
3704 register int c;
3705 Lisp_Object obj;
3707 #ifdef subprocesses
3708 if (kbd_on_hold_p () && kbd_buffer_nr_stored () < KBD_BUFFER_SIZE/4)
3710 /* Start reading input again, we have processed enough so we can
3711 accept new events again. */
3712 unhold_keyboard_input ();
3713 #ifdef SIGIO
3714 if (!noninteractive)
3715 signal (SIGIO, input_available_signal);
3716 #endif /* SIGIO */
3717 start_polling ();
3719 #endif /* subprocesses */
3721 if (noninteractive
3722 /* In case we are running as a daemon, only do this before
3723 detaching from the terminal. */
3724 || (IS_DAEMON && daemon_pipe[1] >= 0))
3726 c = getchar ();
3727 XSETINT (obj, c);
3728 *kbp = current_kboard;
3729 return obj;
3732 /* Wait until there is input available. */
3733 for (;;)
3735 /* Break loop if there's an unread command event. Needed in
3736 moused window autoselection which uses a timer to insert such
3737 events. */
3738 if (CONSP (Vunread_command_events))
3739 break;
3741 if (kbd_fetch_ptr != kbd_store_ptr)
3742 break;
3743 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
3744 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3745 break;
3746 #endif
3748 /* If the quit flag is set, then read_char will return
3749 quit_char, so that counts as "available input." */
3750 if (!NILP (Vquit_flag))
3751 quit_throw_to_read_char ();
3753 /* One way or another, wait until input is available; then, if
3754 interrupt handlers have not read it, read it now. */
3756 /* Note SIGIO has been undef'd if FIONREAD is missing. */
3757 #ifdef SIGIO
3758 gobble_input (0);
3759 #endif /* SIGIO */
3760 if (kbd_fetch_ptr != kbd_store_ptr)
3761 break;
3762 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
3763 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3764 break;
3765 #endif
3766 if (end_time)
3768 EMACS_TIME duration;
3769 EMACS_GET_TIME (duration);
3770 if (EMACS_TIME_GE (duration, *end_time))
3771 return Qnil; /* finished waiting */
3772 else
3774 EMACS_SUB_TIME (duration, *end_time, duration);
3775 wait_reading_process_output (EMACS_SECS (duration),
3776 EMACS_USECS (duration),
3777 -1, 1, Qnil, NULL, 0);
3780 else
3781 wait_reading_process_output (0, 0, -1, 1, Qnil, NULL, 0);
3783 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
3784 /* Pass 1 for EXPECT since we just waited to have input. */
3785 read_avail_input (1);
3788 if (CONSP (Vunread_command_events))
3790 Lisp_Object first;
3791 first = XCAR (Vunread_command_events);
3792 Vunread_command_events = XCDR (Vunread_command_events);
3793 *kbp = current_kboard;
3794 return first;
3797 /* At this point, we know that there is a readable event available
3798 somewhere. If the event queue is empty, then there must be a
3799 mouse movement enabled and available. */
3800 if (kbd_fetch_ptr != kbd_store_ptr)
3802 struct input_event *event;
3804 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3805 ? kbd_fetch_ptr
3806 : kbd_buffer);
3808 last_event_timestamp = event->timestamp;
3810 *kbp = event_to_kboard (event);
3811 if (*kbp == 0)
3812 *kbp = current_kboard; /* Better than returning null ptr? */
3814 obj = Qnil;
3816 /* These two kinds of events get special handling
3817 and don't actually appear to the command loop.
3818 We return nil for them. */
3819 if (event->kind == SELECTION_REQUEST_EVENT
3820 || event->kind == SELECTION_CLEAR_EVENT)
3822 #ifdef HAVE_X11
3823 struct input_event copy;
3825 /* Remove it from the buffer before processing it,
3826 since otherwise swallow_events will see it
3827 and process it again. */
3828 copy = *event;
3829 kbd_fetch_ptr = event + 1;
3830 input_pending = readable_events (0);
3831 x_handle_selection_event (&copy);
3832 #else
3833 /* We're getting selection request events, but we don't have
3834 a window system. */
3835 abort ();
3836 #endif
3839 #if defined (HAVE_NS)
3840 else if (event->kind == NS_TEXT_EVENT)
3842 if (event->code == KEY_NS_PUT_WORKING_TEXT)
3843 obj = Fcons (intern ("ns-put-working-text"), Qnil);
3844 else
3845 obj = Fcons (intern ("ns-unput-working-text"), Qnil);
3846 kbd_fetch_ptr = event + 1;
3847 if (used_mouse_menu)
3848 *used_mouse_menu = 1;
3850 #endif
3852 #if defined (HAVE_X11) || defined (HAVE_NTGUI) \
3853 || defined (HAVE_NS)
3854 else if (event->kind == DELETE_WINDOW_EVENT)
3856 /* Make an event (delete-frame (FRAME)). */
3857 obj = Fcons (event->frame_or_window, Qnil);
3858 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
3859 kbd_fetch_ptr = event + 1;
3861 #endif
3862 #if defined (HAVE_X11) || defined (HAVE_NTGUI) \
3863 || defined (HAVE_NS)
3864 else if (event->kind == ICONIFY_EVENT)
3866 /* Make an event (iconify-frame (FRAME)). */
3867 obj = Fcons (event->frame_or_window, Qnil);
3868 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
3869 kbd_fetch_ptr = event + 1;
3871 else if (event->kind == DEICONIFY_EVENT)
3873 /* Make an event (make-frame-visible (FRAME)). */
3874 obj = Fcons (event->frame_or_window, Qnil);
3875 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
3876 kbd_fetch_ptr = event + 1;
3878 #endif
3879 else if (event->kind == BUFFER_SWITCH_EVENT)
3881 /* The value doesn't matter here; only the type is tested. */
3882 XSETBUFFER (obj, current_buffer);
3883 kbd_fetch_ptr = event + 1;
3885 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
3886 || defined(HAVE_NS) || defined (USE_GTK)
3887 else if (event->kind == MENU_BAR_ACTIVATE_EVENT)
3889 kbd_fetch_ptr = event + 1;
3890 input_pending = readable_events (0);
3891 if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
3892 x_activate_menubar (XFRAME (event->frame_or_window));
3894 #endif
3895 #if defined (WINDOWSNT)
3896 else if (event->kind == LANGUAGE_CHANGE_EVENT)
3898 /* Make an event (language-change (FRAME CHARSET LCID)). */
3899 obj = Fcons (event->frame_or_window, Qnil);
3900 obj = Fcons (Qlanguage_change, Fcons (obj, Qnil));
3901 kbd_fetch_ptr = event + 1;
3903 #endif
3904 else if (event->kind == SAVE_SESSION_EVENT)
3906 obj = Fcons (Qsave_session, Fcons (event->arg, Qnil));
3907 kbd_fetch_ptr = event + 1;
3909 /* Just discard these, by returning nil.
3910 With MULTI_KBOARD, these events are used as placeholders
3911 when we need to randomly delete events from the queue.
3912 (They shouldn't otherwise be found in the buffer,
3913 but on some machines it appears they do show up
3914 even without MULTI_KBOARD.) */
3915 /* On Windows NT/9X, NO_EVENT is used to delete extraneous
3916 mouse events during a popup-menu call. */
3917 else if (event->kind == NO_EVENT)
3918 kbd_fetch_ptr = event + 1;
3919 else if (event->kind == HELP_EVENT)
3921 Lisp_Object object, position, help, frame, window;
3923 frame = event->frame_or_window;
3924 object = event->arg;
3925 position = make_number (event->code);
3926 window = event->x;
3927 help = event->y;
3928 clear_event (event);
3930 kbd_fetch_ptr = event + 1;
3931 if (!WINDOWP (window))
3932 window = Qnil;
3933 obj = Fcons (Qhelp_echo,
3934 list5 (frame, help, window, object, position));
3936 else if (event->kind == FOCUS_IN_EVENT)
3938 /* Notification of a FocusIn event. The frame receiving the
3939 focus is in event->frame_or_window. Generate a
3940 switch-frame event if necessary. */
3941 Lisp_Object frame, focus;
3943 frame = event->frame_or_window;
3944 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
3945 if (FRAMEP (focus))
3946 frame = focus;
3948 if (!EQ (frame, internal_last_event_frame)
3949 && !EQ (frame, selected_frame))
3950 obj = make_lispy_switch_frame (frame);
3951 internal_last_event_frame = frame;
3952 kbd_fetch_ptr = event + 1;
3954 #ifdef HAVE_DBUS
3955 else if (event->kind == DBUS_EVENT)
3957 obj = make_lispy_event (event);
3958 kbd_fetch_ptr = event + 1;
3960 #endif
3961 else if (event->kind == CONFIG_CHANGED_EVENT)
3963 obj = make_lispy_event (event);
3964 kbd_fetch_ptr = event + 1;
3966 else
3968 /* If this event is on a different frame, return a switch-frame this
3969 time, and leave the event in the queue for next time. */
3970 Lisp_Object frame;
3971 Lisp_Object focus;
3973 frame = event->frame_or_window;
3974 if (CONSP (frame))
3975 frame = XCAR (frame);
3976 else if (WINDOWP (frame))
3977 frame = WINDOW_FRAME (XWINDOW (frame));
3979 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
3980 if (! NILP (focus))
3981 frame = focus;
3983 if (! EQ (frame, internal_last_event_frame)
3984 && !EQ (frame, selected_frame))
3985 obj = make_lispy_switch_frame (frame);
3986 internal_last_event_frame = frame;
3988 /* If we didn't decide to make a switch-frame event, go ahead
3989 and build a real event from the queue entry. */
3991 if (NILP (obj))
3993 obj = make_lispy_event (event);
3995 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
3996 || defined(HAVE_NS) || defined (USE_GTK)
3997 /* If this was a menu selection, then set the flag to inhibit
3998 writing to last_nonmenu_event. Don't do this if the event
3999 we're returning is (menu-bar), though; that indicates the
4000 beginning of the menu sequence, and we might as well leave
4001 that as the `event with parameters' for this selection. */
4002 if (used_mouse_menu
4003 && !EQ (event->frame_or_window, event->arg)
4004 && (event->kind == MENU_BAR_EVENT
4005 || event->kind == TOOL_BAR_EVENT))
4006 *used_mouse_menu = 1;
4007 #endif
4008 #ifdef HAVE_NS
4009 /* certain system events are non-key events */
4010 if (used_mouse_menu
4011 && event->kind == NS_NONKEY_EVENT)
4012 *used_mouse_menu = 1;
4013 #endif
4015 /* Wipe out this event, to catch bugs. */
4016 clear_event (event);
4017 kbd_fetch_ptr = event + 1;
4021 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
4022 /* Try generating a mouse motion event. */
4023 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
4025 FRAME_PTR f = some_mouse_moved ();
4026 Lisp_Object bar_window;
4027 enum scroll_bar_part part;
4028 Lisp_Object x, y;
4029 unsigned long t;
4031 *kbp = current_kboard;
4032 /* Note that this uses F to determine which terminal to look at.
4033 If there is no valid info, it does not store anything
4034 so x remains nil. */
4035 x = Qnil;
4037 /* XXX Can f or mouse_position_hook be NULL here? */
4038 if (f && FRAME_TERMINAL (f)->mouse_position_hook)
4039 (*FRAME_TERMINAL (f)->mouse_position_hook) (&f, 0, &bar_window,
4040 &part, &x, &y, &t);
4042 obj = Qnil;
4044 /* Decide if we should generate a switch-frame event. Don't
4045 generate switch-frame events for motion outside of all Emacs
4046 frames. */
4047 if (!NILP (x) && f)
4049 Lisp_Object frame;
4051 frame = FRAME_FOCUS_FRAME (f);
4052 if (NILP (frame))
4053 XSETFRAME (frame, f);
4055 if (! EQ (frame, internal_last_event_frame)
4056 && !EQ (frame, selected_frame))
4057 obj = make_lispy_switch_frame (frame);
4058 internal_last_event_frame = frame;
4061 /* If we didn't decide to make a switch-frame event, go ahead and
4062 return a mouse-motion event. */
4063 if (!NILP (x) && NILP (obj))
4064 obj = make_lispy_movement (f, bar_window, part, x, y, t);
4066 #endif /* HAVE_MOUSE || HAVE GPM */
4067 else
4068 /* We were promised by the above while loop that there was
4069 something for us to read! */
4070 abort ();
4072 input_pending = readable_events (0);
4074 Vlast_event_frame = internal_last_event_frame;
4076 return (obj);
4079 /* Process any events that are not user-visible,
4080 then return, without reading any user-visible events. */
4082 void
4083 swallow_events (int do_display)
4085 int old_timers_run;
4087 while (kbd_fetch_ptr != kbd_store_ptr)
4089 struct input_event *event;
4091 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
4092 ? kbd_fetch_ptr
4093 : kbd_buffer);
4095 last_event_timestamp = event->timestamp;
4097 /* These two kinds of events get special handling
4098 and don't actually appear to the command loop. */
4099 if (event->kind == SELECTION_REQUEST_EVENT
4100 || event->kind == SELECTION_CLEAR_EVENT)
4102 #ifdef HAVE_X11
4103 struct input_event copy;
4105 /* Remove it from the buffer before processing it,
4106 since otherwise swallow_events called recursively could see it
4107 and process it again. */
4108 copy = *event;
4109 kbd_fetch_ptr = event + 1;
4110 input_pending = readable_events (0);
4111 x_handle_selection_event (&copy);
4112 #else
4113 /* We're getting selection request events, but we don't have
4114 a window system. */
4115 abort ();
4116 #endif
4118 else
4119 break;
4122 old_timers_run = timers_run;
4123 get_input_pending (&input_pending, READABLE_EVENTS_DO_TIMERS_NOW);
4125 if (timers_run != old_timers_run && do_display)
4126 redisplay_preserve_echo_area (7);
4129 /* Record the start of when Emacs is idle,
4130 for the sake of running idle-time timers. */
4132 static void
4133 timer_start_idle (void)
4135 Lisp_Object timers;
4137 /* If we are already in the idle state, do nothing. */
4138 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4139 return;
4141 EMACS_GET_TIME (timer_idleness_start_time);
4143 timer_last_idleness_start_time = timer_idleness_start_time;
4145 /* Mark all idle-time timers as once again candidates for running. */
4146 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCDR (timers))
4148 Lisp_Object timer;
4150 timer = XCAR (timers);
4152 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
4153 continue;
4154 XVECTOR (timer)->contents[0] = Qnil;
4158 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
4160 static void
4161 timer_stop_idle (void)
4163 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
4166 /* Resume idle timer from last idle start time. */
4168 static void
4169 timer_resume_idle (void)
4171 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4172 return;
4174 timer_idleness_start_time = timer_last_idleness_start_time;
4177 /* This is only for debugging. */
4178 struct input_event last_timer_event;
4180 /* List of elisp functions to call, delayed because they were generated in
4181 a context where Elisp could not be safely run (e.g. redisplay, signal,
4182 ...). Each element has the form (FUN . ARGS). */
4183 Lisp_Object pending_funcalls;
4185 /* Check whether a timer has fired. To prevent larger problems we simply
4186 disregard elements that are not proper timers. Do not make a circular
4187 timer list for the time being.
4189 Returns the time to wait until the next timer fires. If a
4190 timer is triggering now, return zero.
4191 If no timer is active, return -1.
4193 If a timer is ripe, we run it, with quitting turned off.
4194 In that case we return 0 to indicate that a new timer_check_2 call
4195 should be done. */
4197 static EMACS_TIME
4198 timer_check_2 (void)
4200 EMACS_TIME nexttime;
4201 EMACS_TIME now, idleness_now;
4202 Lisp_Object timers, idle_timers, chosen_timer;
4203 struct gcpro gcpro1, gcpro2, gcpro3;
4205 EMACS_SET_SECS (nexttime, -1);
4206 EMACS_SET_USECS (nexttime, -1);
4208 /* Always consider the ordinary timers. */
4209 timers = Vtimer_list;
4210 /* Consider the idle timers only if Emacs is idle. */
4211 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4212 idle_timers = Vtimer_idle_list;
4213 else
4214 idle_timers = Qnil;
4215 chosen_timer = Qnil;
4216 GCPRO3 (timers, idle_timers, chosen_timer);
4218 /* First run the code that was delayed. */
4219 while (CONSP (pending_funcalls))
4221 Lisp_Object funcall = XCAR (pending_funcalls);
4222 pending_funcalls = XCDR (pending_funcalls);
4223 safe_call2 (Qapply, XCAR (funcall), XCDR (funcall));
4226 if (CONSP (timers) || CONSP (idle_timers))
4228 EMACS_GET_TIME (now);
4229 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4230 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
4233 while (CONSP (timers) || CONSP (idle_timers))
4235 Lisp_Object *vector;
4236 Lisp_Object timer = Qnil, idle_timer = Qnil;
4237 EMACS_TIME timer_time, idle_timer_time;
4238 EMACS_TIME difference, timer_difference, idle_timer_difference;
4240 /* Skip past invalid timers and timers already handled. */
4241 if (!NILP (timers))
4243 timer = XCAR (timers);
4244 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
4246 timers = XCDR (timers);
4247 continue;
4249 vector = XVECTOR (timer)->contents;
4251 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
4252 || !INTEGERP (vector[3])
4253 || ! NILP (vector[0]))
4255 timers = XCDR (timers);
4256 continue;
4259 if (!NILP (idle_timers))
4261 timer = XCAR (idle_timers);
4262 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
4264 idle_timers = XCDR (idle_timers);
4265 continue;
4267 vector = XVECTOR (timer)->contents;
4269 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
4270 || !INTEGERP (vector[3])
4271 || ! NILP (vector[0]))
4273 idle_timers = XCDR (idle_timers);
4274 continue;
4278 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
4279 based on the next ordinary timer.
4280 TIMER_DIFFERENCE is the distance in time from NOW to when
4281 this timer becomes ripe (negative if it's already ripe). */
4282 if (!NILP (timers))
4284 timer = XCAR (timers);
4285 vector = XVECTOR (timer)->contents;
4286 EMACS_SET_SECS (timer_time,
4287 (XINT (vector[1]) << 16) | (XINT (vector[2])));
4288 EMACS_SET_USECS (timer_time, XINT (vector[3]));
4289 EMACS_SUB_TIME (timer_difference, timer_time, now);
4292 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
4293 based on the next idle timer. */
4294 if (!NILP (idle_timers))
4296 idle_timer = XCAR (idle_timers);
4297 vector = XVECTOR (idle_timer)->contents;
4298 EMACS_SET_SECS (idle_timer_time,
4299 (XINT (vector[1]) << 16) | (XINT (vector[2])));
4300 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
4301 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
4304 /* Decide which timer is the next timer,
4305 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
4306 Also step down the list where we found that timer. */
4308 if (! NILP (timers) && ! NILP (idle_timers))
4310 EMACS_TIME temp;
4311 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
4312 if (EMACS_TIME_NEG_P (temp))
4314 chosen_timer = timer;
4315 timers = XCDR (timers);
4316 difference = timer_difference;
4318 else
4320 chosen_timer = idle_timer;
4321 idle_timers = XCDR (idle_timers);
4322 difference = idle_timer_difference;
4325 else if (! NILP (timers))
4327 chosen_timer = timer;
4328 timers = XCDR (timers);
4329 difference = timer_difference;
4331 else
4333 chosen_timer = idle_timer;
4334 idle_timers = XCDR (idle_timers);
4335 difference = idle_timer_difference;
4337 vector = XVECTOR (chosen_timer)->contents;
4339 /* If timer is ripe, run it if it hasn't been run. */
4340 if (EMACS_TIME_NEG_P (difference)
4341 || (EMACS_SECS (difference) == 0
4342 && EMACS_USECS (difference) == 0))
4344 if (NILP (vector[0]))
4346 int count = SPECPDL_INDEX ();
4347 Lisp_Object old_deactivate_mark = Vdeactivate_mark;
4349 /* Mark the timer as triggered to prevent problems if the lisp
4350 code fails to reschedule it right. */
4351 vector[0] = Qt;
4353 specbind (Qinhibit_quit, Qt);
4355 call1 (Qtimer_event_handler, chosen_timer);
4356 Vdeactivate_mark = old_deactivate_mark;
4357 timers_run++;
4358 unbind_to (count, Qnil);
4360 /* Since we have handled the event,
4361 we don't need to tell the caller to wake up and do it. */
4362 /* But the caller must still wait for the next timer, so
4363 return 0 to indicate that. */
4366 EMACS_SET_SECS (nexttime, 0);
4367 EMACS_SET_USECS (nexttime, 0);
4369 else
4370 /* When we encounter a timer that is still waiting,
4371 return the amount of time to wait before it is ripe. */
4373 UNGCPRO;
4374 return difference;
4378 /* No timers are pending in the future. */
4379 /* Return 0 if we generated an event, and -1 if not. */
4380 UNGCPRO;
4381 return nexttime;
4385 /* Check whether a timer has fired. To prevent larger problems we simply
4386 disregard elements that are not proper timers. Do not make a circular
4387 timer list for the time being.
4389 Returns the time to wait until the next timer fires.
4390 If no timer is active, return -1.
4392 As long as any timer is ripe, we run it.
4394 DO_IT_NOW is now ignored. It used to mean that we should
4395 run the timer directly instead of queueing a timer-event.
4396 Now we always run timers directly. */
4398 EMACS_TIME
4399 timer_check (int do_it_now)
4401 EMACS_TIME nexttime;
4405 nexttime = timer_check_2 ();
4407 while (EMACS_SECS (nexttime) == 0 && EMACS_USECS (nexttime) == 0);
4409 return nexttime;
4412 DEFUN ("current-idle-time", Fcurrent_idle_time, Scurrent_idle_time, 0, 0, 0,
4413 doc: /* Return the current length of Emacs idleness, or nil.
4414 The value when Emacs is idle is a list of three integers. The first has
4415 the most significant 16 bits of the seconds, while the second has the least
4416 significant 16 bits. The third integer gives the microsecond count.
4418 The value when Emacs is not idle is nil.
4420 The microsecond count is zero on systems that do not provide
4421 resolution finer than a second. */)
4422 (void)
4424 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4426 EMACS_TIME now, idleness_now;
4428 EMACS_GET_TIME (now);
4429 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
4431 return list3 (make_number ((EMACS_SECS (idleness_now) >> 16) & 0xffff),
4432 make_number ((EMACS_SECS (idleness_now) >> 0) & 0xffff),
4433 make_number (EMACS_USECS (idleness_now)));
4436 return Qnil;
4439 /* Caches for modify_event_symbol. */
4440 static Lisp_Object accent_key_syms;
4441 static Lisp_Object func_key_syms;
4442 static Lisp_Object mouse_syms;
4443 static Lisp_Object wheel_syms;
4444 static Lisp_Object drag_n_drop_syms;
4446 /* This is a list of keysym codes for special "accent" characters.
4447 It parallels lispy_accent_keys. */
4449 static const int lispy_accent_codes[] =
4451 #ifdef XK_dead_circumflex
4452 XK_dead_circumflex,
4453 #else
4455 #endif
4456 #ifdef XK_dead_grave
4457 XK_dead_grave,
4458 #else
4460 #endif
4461 #ifdef XK_dead_tilde
4462 XK_dead_tilde,
4463 #else
4465 #endif
4466 #ifdef XK_dead_diaeresis
4467 XK_dead_diaeresis,
4468 #else
4470 #endif
4471 #ifdef XK_dead_macron
4472 XK_dead_macron,
4473 #else
4475 #endif
4476 #ifdef XK_dead_degree
4477 XK_dead_degree,
4478 #else
4480 #endif
4481 #ifdef XK_dead_acute
4482 XK_dead_acute,
4483 #else
4485 #endif
4486 #ifdef XK_dead_cedilla
4487 XK_dead_cedilla,
4488 #else
4490 #endif
4491 #ifdef XK_dead_breve
4492 XK_dead_breve,
4493 #else
4495 #endif
4496 #ifdef XK_dead_ogonek
4497 XK_dead_ogonek,
4498 #else
4500 #endif
4501 #ifdef XK_dead_caron
4502 XK_dead_caron,
4503 #else
4505 #endif
4506 #ifdef XK_dead_doubleacute
4507 XK_dead_doubleacute,
4508 #else
4510 #endif
4511 #ifdef XK_dead_abovedot
4512 XK_dead_abovedot,
4513 #else
4515 #endif
4516 #ifdef XK_dead_abovering
4517 XK_dead_abovering,
4518 #else
4520 #endif
4521 #ifdef XK_dead_iota
4522 XK_dead_iota,
4523 #else
4525 #endif
4526 #ifdef XK_dead_belowdot
4527 XK_dead_belowdot,
4528 #else
4530 #endif
4531 #ifdef XK_dead_voiced_sound
4532 XK_dead_voiced_sound,
4533 #else
4535 #endif
4536 #ifdef XK_dead_semivoiced_sound
4537 XK_dead_semivoiced_sound,
4538 #else
4540 #endif
4541 #ifdef XK_dead_hook
4542 XK_dead_hook,
4543 #else
4545 #endif
4546 #ifdef XK_dead_horn
4547 XK_dead_horn,
4548 #else
4550 #endif
4553 /* This is a list of Lisp names for special "accent" characters.
4554 It parallels lispy_accent_codes. */
4556 static const char *const lispy_accent_keys[] =
4558 "dead-circumflex",
4559 "dead-grave",
4560 "dead-tilde",
4561 "dead-diaeresis",
4562 "dead-macron",
4563 "dead-degree",
4564 "dead-acute",
4565 "dead-cedilla",
4566 "dead-breve",
4567 "dead-ogonek",
4568 "dead-caron",
4569 "dead-doubleacute",
4570 "dead-abovedot",
4571 "dead-abovering",
4572 "dead-iota",
4573 "dead-belowdot",
4574 "dead-voiced-sound",
4575 "dead-semivoiced-sound",
4576 "dead-hook",
4577 "dead-horn",
4580 #ifdef HAVE_NTGUI
4581 #define FUNCTION_KEY_OFFSET 0x0
4583 const char *const lispy_function_keys[] =
4585 0, /* 0 */
4587 0, /* VK_LBUTTON 0x01 */
4588 0, /* VK_RBUTTON 0x02 */
4589 "cancel", /* VK_CANCEL 0x03 */
4590 0, /* VK_MBUTTON 0x04 */
4592 0, 0, 0, /* 0x05 .. 0x07 */
4594 "backspace", /* VK_BACK 0x08 */
4595 "tab", /* VK_TAB 0x09 */
4597 0, 0, /* 0x0A .. 0x0B */
4599 "clear", /* VK_CLEAR 0x0C */
4600 "return", /* VK_RETURN 0x0D */
4602 0, 0, /* 0x0E .. 0x0F */
4604 0, /* VK_SHIFT 0x10 */
4605 0, /* VK_CONTROL 0x11 */
4606 0, /* VK_MENU 0x12 */
4607 "pause", /* VK_PAUSE 0x13 */
4608 "capslock", /* VK_CAPITAL 0x14 */
4609 "kana", /* VK_KANA/VK_HANGUL 0x15 */
4610 0, /* 0x16 */
4611 "junja", /* VK_JUNJA 0x17 */
4612 "final", /* VK_FINAL 0x18 */
4613 "kanji", /* VK_KANJI/VK_HANJA 0x19 */
4614 0, /* 0x1A */
4615 "escape", /* VK_ESCAPE 0x1B */
4616 "convert", /* VK_CONVERT 0x1C */
4617 "non-convert", /* VK_NONCONVERT 0x1D */
4618 "accept", /* VK_ACCEPT 0x1E */
4619 "mode-change", /* VK_MODECHANGE 0x1F */
4620 0, /* VK_SPACE 0x20 */
4621 "prior", /* VK_PRIOR 0x21 */
4622 "next", /* VK_NEXT 0x22 */
4623 "end", /* VK_END 0x23 */
4624 "home", /* VK_HOME 0x24 */
4625 "left", /* VK_LEFT 0x25 */
4626 "up", /* VK_UP 0x26 */
4627 "right", /* VK_RIGHT 0x27 */
4628 "down", /* VK_DOWN 0x28 */
4629 "select", /* VK_SELECT 0x29 */
4630 "print", /* VK_PRINT 0x2A */
4631 "execute", /* VK_EXECUTE 0x2B */
4632 "snapshot", /* VK_SNAPSHOT 0x2C */
4633 "insert", /* VK_INSERT 0x2D */
4634 "delete", /* VK_DELETE 0x2E */
4635 "help", /* VK_HELP 0x2F */
4637 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
4639 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4641 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
4643 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
4645 0, 0, 0, 0, 0, 0, 0, 0, 0,
4646 0, 0, 0, 0, 0, 0, 0, 0, 0,
4647 0, 0, 0, 0, 0, 0, 0, 0,
4649 "lwindow", /* VK_LWIN 0x5B */
4650 "rwindow", /* VK_RWIN 0x5C */
4651 "apps", /* VK_APPS 0x5D */
4652 0, /* 0x5E */
4653 "sleep",
4654 "kp-0", /* VK_NUMPAD0 0x60 */
4655 "kp-1", /* VK_NUMPAD1 0x61 */
4656 "kp-2", /* VK_NUMPAD2 0x62 */
4657 "kp-3", /* VK_NUMPAD3 0x63 */
4658 "kp-4", /* VK_NUMPAD4 0x64 */
4659 "kp-5", /* VK_NUMPAD5 0x65 */
4660 "kp-6", /* VK_NUMPAD6 0x66 */
4661 "kp-7", /* VK_NUMPAD7 0x67 */
4662 "kp-8", /* VK_NUMPAD8 0x68 */
4663 "kp-9", /* VK_NUMPAD9 0x69 */
4664 "kp-multiply", /* VK_MULTIPLY 0x6A */
4665 "kp-add", /* VK_ADD 0x6B */
4666 "kp-separator", /* VK_SEPARATOR 0x6C */
4667 "kp-subtract", /* VK_SUBTRACT 0x6D */
4668 "kp-decimal", /* VK_DECIMAL 0x6E */
4669 "kp-divide", /* VK_DIVIDE 0x6F */
4670 "f1", /* VK_F1 0x70 */
4671 "f2", /* VK_F2 0x71 */
4672 "f3", /* VK_F3 0x72 */
4673 "f4", /* VK_F4 0x73 */
4674 "f5", /* VK_F5 0x74 */
4675 "f6", /* VK_F6 0x75 */
4676 "f7", /* VK_F7 0x76 */
4677 "f8", /* VK_F8 0x77 */
4678 "f9", /* VK_F9 0x78 */
4679 "f10", /* VK_F10 0x79 */
4680 "f11", /* VK_F11 0x7A */
4681 "f12", /* VK_F12 0x7B */
4682 "f13", /* VK_F13 0x7C */
4683 "f14", /* VK_F14 0x7D */
4684 "f15", /* VK_F15 0x7E */
4685 "f16", /* VK_F16 0x7F */
4686 "f17", /* VK_F17 0x80 */
4687 "f18", /* VK_F18 0x81 */
4688 "f19", /* VK_F19 0x82 */
4689 "f20", /* VK_F20 0x83 */
4690 "f21", /* VK_F21 0x84 */
4691 "f22", /* VK_F22 0x85 */
4692 "f23", /* VK_F23 0x86 */
4693 "f24", /* VK_F24 0x87 */
4695 0, 0, 0, 0, /* 0x88 .. 0x8B */
4696 0, 0, 0, 0, /* 0x8C .. 0x8F */
4698 "kp-numlock", /* VK_NUMLOCK 0x90 */
4699 "scroll", /* VK_SCROLL 0x91 */
4700 /* Not sure where the following block comes from.
4701 Windows headers have NEC and Fujitsu specific keys in
4702 this block, but nothing generic. */
4703 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
4704 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
4705 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
4706 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
4707 "kp-end", /* VK_NUMPAD_END 0x96 */
4708 "kp-home", /* VK_NUMPAD_HOME 0x97 */
4709 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
4710 "kp-up", /* VK_NUMPAD_UP 0x99 */
4711 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
4712 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
4713 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
4714 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
4716 0, 0, /* 0x9E .. 0x9F */
4719 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
4720 * Used only as parameters to GetAsyncKeyState and GetKeyState.
4721 * No other API or message will distinguish left and right keys this way.
4722 * 0xA0 .. 0xA5
4724 0, 0, 0, 0, 0, 0,
4726 /* Multimedia keys. These are handled as WM_APPCOMMAND, which allows us
4727 to enable them selectively, and gives access to a few more functions.
4728 See lispy_multimedia_keys below. */
4729 0, 0, 0, 0, 0, 0, 0, /* 0xA6 .. 0xAC Browser */
4730 0, 0, 0, /* 0xAD .. 0xAF Volume */
4731 0, 0, 0, 0, /* 0xB0 .. 0xB3 Media */
4732 0, 0, 0, 0, /* 0xB4 .. 0xB7 Apps */
4734 /* 0xB8 .. 0xC0 "OEM" keys - all seem to be punctuation. */
4735 0, 0, 0, 0, 0, 0, 0, 0, 0,
4737 /* 0xC1 - 0xDA unallocated, 0xDB-0xDF more OEM keys */
4738 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4739 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4741 0, /* 0xE0 */
4742 "ax", /* VK_OEM_AX 0xE1 */
4743 0, /* VK_OEM_102 0xE2 */
4744 "ico-help", /* VK_ICO_HELP 0xE3 */
4745 "ico-00", /* VK_ICO_00 0xE4 */
4746 0, /* VK_PROCESSKEY 0xE5 - used by IME */
4747 "ico-clear", /* VK_ICO_CLEAR 0xE6 */
4748 0, /* VK_PACKET 0xE7 - used to pass unicode chars */
4749 0, /* 0xE8 */
4750 "reset", /* VK_OEM_RESET 0xE9 */
4751 "jump", /* VK_OEM_JUMP 0xEA */
4752 "oem-pa1", /* VK_OEM_PA1 0xEB */
4753 "oem-pa2", /* VK_OEM_PA2 0xEC */
4754 "oem-pa3", /* VK_OEM_PA3 0xED */
4755 "wsctrl", /* VK_OEM_WSCTRL 0xEE */
4756 "cusel", /* VK_OEM_CUSEL 0xEF */
4757 "oem-attn", /* VK_OEM_ATTN 0xF0 */
4758 "finish", /* VK_OEM_FINISH 0xF1 */
4759 "copy", /* VK_OEM_COPY 0xF2 */
4760 "auto", /* VK_OEM_AUTO 0xF3 */
4761 "enlw", /* VK_OEM_ENLW 0xF4 */
4762 "backtab", /* VK_OEM_BACKTAB 0xF5 */
4763 "attn", /* VK_ATTN 0xF6 */
4764 "crsel", /* VK_CRSEL 0xF7 */
4765 "exsel", /* VK_EXSEL 0xF8 */
4766 "ereof", /* VK_EREOF 0xF9 */
4767 "play", /* VK_PLAY 0xFA */
4768 "zoom", /* VK_ZOOM 0xFB */
4769 "noname", /* VK_NONAME 0xFC */
4770 "pa1", /* VK_PA1 0xFD */
4771 "oem_clear", /* VK_OEM_CLEAR 0xFE */
4772 0 /* 0xFF */
4775 /* Some of these duplicate the "Media keys" on newer keyboards,
4776 but they are delivered to the application in a different way. */
4777 static const char *const lispy_multimedia_keys[] =
4780 "browser-back",
4781 "browser-forward",
4782 "browser-refresh",
4783 "browser-stop",
4784 "browser-search",
4785 "browser-favorites",
4786 "browser-home",
4787 "volume-mute",
4788 "volume-down",
4789 "volume-up",
4790 "media-next",
4791 "media-previous",
4792 "media-stop",
4793 "media-play-pause",
4794 "mail",
4795 "media-select",
4796 "app-1",
4797 "app-2",
4798 "bass-down",
4799 "bass-boost",
4800 "bass-up",
4801 "treble-down",
4802 "treble-up",
4803 "mic-volume-mute",
4804 "mic-volume-down",
4805 "mic-volume-up",
4806 "help",
4807 "find",
4808 "new",
4809 "open",
4810 "close",
4811 "save",
4812 "print",
4813 "undo",
4814 "redo",
4815 "copy",
4816 "cut",
4817 "paste",
4818 "mail-reply",
4819 "mail-forward",
4820 "mail-send",
4821 "spell-check",
4822 "toggle-dictate-command",
4823 "mic-toggle",
4824 "correction-list",
4825 "media-play",
4826 "media-pause",
4827 "media-record",
4828 "media-fast-forward",
4829 "media-rewind",
4830 "media-channel-up",
4831 "media-channel-down"
4834 #else /* not HAVE_NTGUI */
4836 /* This should be dealt with in XTread_socket now, and that doesn't
4837 depend on the client system having the Kana syms defined. See also
4838 the XK_kana_A case below. */
4839 #if 0
4840 #ifdef XK_kana_A
4841 static const char *const lispy_kana_keys[] =
4843 /* X Keysym value */
4844 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x400 .. 0x40f */
4845 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x410 .. 0x41f */
4846 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x420 .. 0x42f */
4847 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x430 .. 0x43f */
4848 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x440 .. 0x44f */
4849 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x450 .. 0x45f */
4850 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x460 .. 0x46f */
4851 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0,
4852 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x480 .. 0x48f */
4853 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x490 .. 0x49f */
4854 0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket",
4855 "kana-comma", "kana-conjunctive", "kana-WO", "kana-a",
4856 "kana-i", "kana-u", "kana-e", "kana-o",
4857 "kana-ya", "kana-yu", "kana-yo", "kana-tsu",
4858 "prolongedsound", "kana-A", "kana-I", "kana-U",
4859 "kana-E", "kana-O", "kana-KA", "kana-KI",
4860 "kana-KU", "kana-KE", "kana-KO", "kana-SA",
4861 "kana-SHI", "kana-SU", "kana-SE", "kana-SO",
4862 "kana-TA", "kana-CHI", "kana-TSU", "kana-TE",
4863 "kana-TO", "kana-NA", "kana-NI", "kana-NU",
4864 "kana-NE", "kana-NO", "kana-HA", "kana-HI",
4865 "kana-FU", "kana-HE", "kana-HO", "kana-MA",
4866 "kana-MI", "kana-MU", "kana-ME", "kana-MO",
4867 "kana-YA", "kana-YU", "kana-YO", "kana-RA",
4868 "kana-RI", "kana-RU", "kana-RE", "kana-RO",
4869 "kana-WA", "kana-N", "voicedsound", "semivoicedsound",
4870 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4e0 .. 0x4ef */
4871 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f0 .. 0x4ff */
4873 #endif /* XK_kana_A */
4874 #endif /* 0 */
4876 #define FUNCTION_KEY_OFFSET 0xff00
4878 /* You'll notice that this table is arranged to be conveniently
4879 indexed by X Windows keysym values. */
4880 static const char *const lispy_function_keys[] =
4882 /* X Keysym value */
4884 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00...0f */
4885 "backspace", "tab", "linefeed", "clear",
4886 0, "return", 0, 0,
4887 0, 0, 0, "pause", /* 0xff10...1f */
4888 0, 0, 0, 0, 0, 0, 0, "escape",
4889 0, 0, 0, 0,
4890 0, "kanji", "muhenkan", "henkan", /* 0xff20...2f */
4891 "romaji", "hiragana", "katakana", "hiragana-katakana",
4892 "zenkaku", "hankaku", "zenkaku-hankaku", "touroku",
4893 "massyo", "kana-lock", "kana-shift", "eisu-shift",
4894 "eisu-toggle", /* 0xff30...3f */
4895 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4896 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
4898 "home", "left", "up", "right", /* 0xff50 */ /* IsCursorKey */
4899 "down", "prior", "next", "end",
4900 "begin", 0, 0, 0, 0, 0, 0, 0,
4901 "select", /* 0xff60 */ /* IsMiscFunctionKey */
4902 "print",
4903 "execute",
4904 "insert",
4905 0, /* 0xff64 */
4906 "undo",
4907 "redo",
4908 "menu",
4909 "find",
4910 "cancel",
4911 "help",
4912 "break", /* 0xff6b */
4914 0, 0, 0, 0,
4915 0, 0, 0, 0, "backtab", 0, 0, 0, /* 0xff70... */
4916 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff78... */
4917 "kp-space", /* 0xff80 */ /* IsKeypadKey */
4918 0, 0, 0, 0, 0, 0, 0, 0,
4919 "kp-tab", /* 0xff89 */
4920 0, 0, 0,
4921 "kp-enter", /* 0xff8d */
4922 0, 0, 0,
4923 "kp-f1", /* 0xff91 */
4924 "kp-f2",
4925 "kp-f3",
4926 "kp-f4",
4927 "kp-home", /* 0xff95 */
4928 "kp-left",
4929 "kp-up",
4930 "kp-right",
4931 "kp-down",
4932 "kp-prior", /* kp-page-up */
4933 "kp-next", /* kp-page-down */
4934 "kp-end",
4935 "kp-begin",
4936 "kp-insert",
4937 "kp-delete",
4938 0, /* 0xffa0 */
4939 0, 0, 0, 0, 0, 0, 0, 0, 0,
4940 "kp-multiply", /* 0xffaa */
4941 "kp-add",
4942 "kp-separator",
4943 "kp-subtract",
4944 "kp-decimal",
4945 "kp-divide", /* 0xffaf */
4946 "kp-0", /* 0xffb0 */
4947 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
4948 0, /* 0xffba */
4949 0, 0,
4950 "kp-equal", /* 0xffbd */
4951 "f1", /* 0xffbe */ /* IsFunctionKey */
4952 "f2",
4953 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
4954 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
4955 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
4956 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
4957 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
4958 0, 0, 0, 0, 0, 0, 0, 0,
4959 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
4960 0, 0, 0, 0, 0, 0, 0, "delete"
4963 /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE. */
4964 #define ISO_FUNCTION_KEY_OFFSET 0xfe00
4966 static const char *const iso_lispy_function_keys[] =
4968 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe00 */
4969 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe08 */
4970 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe10 */
4971 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe18 */
4972 "iso-lefttab", /* 0xfe20 */
4973 "iso-move-line-up", "iso-move-line-down",
4974 "iso-partial-line-up", "iso-partial-line-down",
4975 "iso-partial-space-left", "iso-partial-space-right",
4976 "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */
4977 "iso-release-margin-left", "iso-release-margin-right",
4978 "iso-release-both-margins",
4979 "iso-fast-cursor-left", "iso-fast-cursor-right",
4980 "iso-fast-cursor-up", "iso-fast-cursor-down",
4981 "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */
4982 "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */
4985 #endif /* not HAVE_NTGUI */
4987 Lisp_Object Vlispy_mouse_stem;
4989 static const char *const lispy_wheel_names[] =
4991 "wheel-up", "wheel-down", "wheel-left", "wheel-right"
4994 /* drag-n-drop events are generated when a set of selected files are
4995 dragged from another application and dropped onto an Emacs window. */
4996 static const char *const lispy_drag_n_drop_names[] =
4998 "drag-n-drop"
5001 /* Scroll bar parts. */
5002 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
5003 Lisp_Object Qup, Qdown, Qbottom, Qend_scroll;
5004 Lisp_Object Qtop, Qratio;
5006 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
5007 static Lisp_Object *const scroll_bar_parts[] = {
5008 &Qabove_handle, &Qhandle, &Qbelow_handle,
5009 &Qup, &Qdown, &Qtop, &Qbottom, &Qend_scroll, &Qratio
5012 /* A vector, indexed by button number, giving the down-going location
5013 of currently depressed buttons, both scroll bar and non-scroll bar.
5015 The elements have the form
5016 (BUTTON-NUMBER MODIFIER-MASK . REST)
5017 where REST is the cdr of a position as it would be reported in the event.
5019 The make_lispy_event function stores positions here to tell the
5020 difference between click and drag events, and to store the starting
5021 location to be included in drag events. */
5023 static Lisp_Object button_down_location;
5025 /* Information about the most recent up-going button event: Which
5026 button, what location, and what time. */
5028 static int last_mouse_button;
5029 static int last_mouse_x;
5030 static int last_mouse_y;
5031 static unsigned long button_down_time;
5033 /* The number of clicks in this multiple-click. */
5035 int double_click_count;
5037 /* X and Y are frame-relative coordinates for a click or wheel event.
5038 Return a Lisp-style event list. */
5040 static Lisp_Object
5041 make_lispy_position (struct frame *f, Lisp_Object x, Lisp_Object y,
5042 unsigned long t)
5044 enum window_part part;
5045 Lisp_Object posn = Qnil;
5046 Lisp_Object extra_info = Qnil;
5047 /* Coordinate pixel positions to return. */
5048 int xret = 0, yret = 0;
5049 /* The window under frame pixel coordinates (x,y) */
5050 Lisp_Object window = f
5051 ? window_from_coordinates (f, XINT (x), XINT (y), &part, 0)
5052 : Qnil;
5054 if (WINDOWP (window))
5056 /* It's a click in window window at frame coordinates (x,y) */
5057 struct window *w = XWINDOW (window);
5058 Lisp_Object string_info = Qnil;
5059 EMACS_INT textpos = -1;
5060 int col = -1, row = -1;
5061 int dx = -1, dy = -1;
5062 int width = -1, height = -1;
5063 Lisp_Object object = Qnil;
5065 /* Pixel coordinates relative to the window corner. */
5066 int wx = XINT (x) - WINDOW_LEFT_EDGE_X (w);
5067 int wy = XINT (y) - WINDOW_TOP_EDGE_Y (w);
5069 /* For text area clicks, return X, Y relative to the corner of
5070 this text area. Note that dX, dY etc are set below, by
5071 buffer_posn_from_coords. */
5072 if (part == ON_TEXT)
5074 xret = XINT (x) - window_box_left (w, TEXT_AREA);
5075 yret = wy - WINDOW_HEADER_LINE_HEIGHT (w);
5077 /* For mode line and header line clicks, return X, Y relative to
5078 the left window edge. Use mode_line_string to look for a
5079 string on the click position. */
5080 else if (part == ON_MODE_LINE || part == ON_HEADER_LINE)
5082 Lisp_Object string;
5083 EMACS_INT charpos;
5085 posn = (part == ON_MODE_LINE) ? Qmode_line : Qheader_line;
5086 /* Note that mode_line_string takes COL, ROW as pixels and
5087 converts them to characters. */
5088 col = wx;
5089 row = wy;
5090 string = mode_line_string (w, part, &col, &row, &charpos,
5091 &object, &dx, &dy, &width, &height);
5092 if (STRINGP (string))
5093 string_info = Fcons (string, make_number (charpos));
5094 textpos = (w == XWINDOW (selected_window)
5095 && current_buffer == XBUFFER (w->buffer))
5096 ? PT : XMARKER (w->pointm)->charpos;
5098 xret = wx;
5099 yret = wy;
5101 /* For fringes and margins, Y is relative to the area's (and the
5102 window's) top edge, while X is meaningless. */
5103 else if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
5105 Lisp_Object string;
5106 EMACS_INT charpos;
5108 posn = (part == ON_LEFT_MARGIN) ? Qleft_margin : Qright_margin;
5109 col = wx;
5110 row = wy;
5111 string = marginal_area_string (w, part, &col, &row, &charpos,
5112 &object, &dx, &dy, &width, &height);
5113 if (STRINGP (string))
5114 string_info = Fcons (string, make_number (charpos));
5115 yret = wy - WINDOW_HEADER_LINE_HEIGHT (w);
5117 else if (part == ON_LEFT_FRINGE)
5119 posn = Qleft_fringe;
5120 col = 0;
5121 dx = wx
5122 - (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5123 ? 0 : window_box_width (w, LEFT_MARGIN_AREA));
5124 dy = yret = wy - WINDOW_HEADER_LINE_HEIGHT (w);
5126 else if (part == ON_RIGHT_FRINGE)
5128 posn = Qright_fringe;
5129 col = 0;
5130 dx = wx
5131 - window_box_width (w, LEFT_MARGIN_AREA)
5132 - window_box_width (w, TEXT_AREA)
5133 - (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5134 ? window_box_width (w, RIGHT_MARGIN_AREA)
5135 : 0);
5136 dy = yret = wy - WINDOW_HEADER_LINE_HEIGHT (w);
5138 else if (part == ON_VERTICAL_BORDER)
5140 posn = Qvertical_line;
5141 width = 1;
5142 dx = 0;
5143 dy = yret = wy;
5145 /* Nothing special for part == ON_SCROLL_BAR. */
5147 /* For clicks in the text area, fringes, or margins, call
5148 buffer_posn_from_coords to extract TEXTPOS, the buffer
5149 position nearest to the click. */
5150 if (textpos < 0)
5152 Lisp_Object string2, object2 = Qnil;
5153 struct display_pos p;
5154 int dx2, dy2;
5155 int width2, height2;
5156 /* The pixel X coordinate passed to buffer_posn_from_coords
5157 is the X coordinate relative to the text area for
5158 text-area and right-margin clicks, zero otherwise. */
5159 int x2
5160 = (part == ON_TEXT) ? xret
5161 : (part == ON_RIGHT_FRINGE || part == ON_RIGHT_MARGIN)
5162 ? (XINT (x) - window_box_left (w, TEXT_AREA))
5163 : 0;
5164 int y2 = wy;
5166 string2 = buffer_posn_from_coords (w, &x2, &y2, &p,
5167 &object2, &dx2, &dy2,
5168 &width2, &height2);
5169 textpos = CHARPOS (p.pos);
5170 if (col < 0) col = x2;
5171 if (row < 0) row = y2;
5172 if (dx < 0) dx = dx2;
5173 if (dy < 0) dy = dy2;
5174 if (width < 0) width = width2;
5175 if (height < 0) height = height2;
5177 if (NILP (posn))
5179 posn = make_number (textpos);
5180 if (STRINGP (string2))
5181 string_info = Fcons (string2,
5182 make_number (CHARPOS (p.string_pos)));
5184 if (NILP (object))
5185 object = object2;
5188 #ifdef HAVE_WINDOW_SYSTEM
5189 if (IMAGEP (object))
5191 Lisp_Object image_map, hotspot;
5192 if ((image_map = Fplist_get (XCDR (object), QCmap),
5193 !NILP (image_map))
5194 && (hotspot = find_hot_spot (image_map, dx, dy),
5195 CONSP (hotspot))
5196 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
5197 posn = XCAR (hotspot);
5199 #endif
5201 /* Object info */
5202 extra_info
5203 = list3 (object,
5204 Fcons (make_number (dx), make_number (dy)),
5205 Fcons (make_number (width), make_number (height)));
5207 /* String info */
5208 extra_info = Fcons (string_info,
5209 Fcons (make_number (textpos),
5210 Fcons (Fcons (make_number (col),
5211 make_number (row)),
5212 extra_info)));
5214 else if (f != 0)
5215 XSETFRAME (window, f);
5216 else
5217 window = Qnil;
5219 return Fcons (window,
5220 Fcons (posn,
5221 Fcons (Fcons (make_number (xret),
5222 make_number (yret)),
5223 Fcons (make_number (t),
5224 extra_info))));
5227 /* Given a struct input_event, build the lisp event which represents
5228 it. If EVENT is 0, build a mouse movement event from the mouse
5229 movement buffer, which should have a movement event in it.
5231 Note that events must be passed to this function in the order they
5232 are received; this function stores the location of button presses
5233 in order to build drag events when the button is released. */
5235 static Lisp_Object
5236 make_lispy_event (struct input_event *event)
5238 int i;
5240 switch (SWITCH_ENUM_CAST (event->kind))
5242 /* A simple keystroke. */
5243 case ASCII_KEYSTROKE_EVENT:
5244 case MULTIBYTE_CHAR_KEYSTROKE_EVENT:
5246 Lisp_Object lispy_c;
5247 EMACS_INT c = event->code;
5248 if (event->kind == ASCII_KEYSTROKE_EVENT)
5250 c &= 0377;
5251 eassert (c == event->code);
5252 /* Turn ASCII characters into control characters
5253 when proper. */
5254 if (event->modifiers & ctrl_modifier)
5256 c = make_ctrl_char (c);
5257 event->modifiers &= ~ctrl_modifier;
5261 /* Add in the other modifier bits. The shift key was taken care
5262 of by the X code. */
5263 c |= (event->modifiers
5264 & (meta_modifier | alt_modifier
5265 | hyper_modifier | super_modifier | ctrl_modifier));
5266 /* Distinguish Shift-SPC from SPC. */
5267 if ((event->code) == 040
5268 && event->modifiers & shift_modifier)
5269 c |= shift_modifier;
5270 button_down_time = 0;
5271 XSETFASTINT (lispy_c, c);
5272 return lispy_c;
5275 #ifdef HAVE_NS
5276 /* NS_NONKEY_EVENTs are just like NON_ASCII_KEYSTROKE_EVENTs,
5277 except that they are non-key events (last-nonmenu-event is nil). */
5278 case NS_NONKEY_EVENT:
5279 #endif
5281 /* A function key. The symbol may need to have modifier prefixes
5282 tacked onto it. */
5283 case NON_ASCII_KEYSTROKE_EVENT:
5284 button_down_time = 0;
5286 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
5287 if (event->code == lispy_accent_codes[i])
5288 return modify_event_symbol (i,
5289 event->modifiers,
5290 Qfunction_key, Qnil,
5291 lispy_accent_keys, &accent_key_syms,
5292 (sizeof (lispy_accent_keys)
5293 / sizeof (lispy_accent_keys[0])));
5295 #if 0
5296 #ifdef XK_kana_A
5297 if (event->code >= 0x400 && event->code < 0x500)
5298 return modify_event_symbol (event->code - 0x400,
5299 event->modifiers & ~shift_modifier,
5300 Qfunction_key, Qnil,
5301 lispy_kana_keys, &func_key_syms,
5302 (sizeof (lispy_kana_keys)
5303 / sizeof (lispy_kana_keys[0])));
5304 #endif /* XK_kana_A */
5305 #endif /* 0 */
5307 #ifdef ISO_FUNCTION_KEY_OFFSET
5308 if (event->code < FUNCTION_KEY_OFFSET
5309 && event->code >= ISO_FUNCTION_KEY_OFFSET)
5310 return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET,
5311 event->modifiers,
5312 Qfunction_key, Qnil,
5313 iso_lispy_function_keys, &func_key_syms,
5314 (sizeof (iso_lispy_function_keys)
5315 / sizeof (iso_lispy_function_keys[0])));
5316 #endif
5318 /* Handle system-specific or unknown keysyms. */
5319 if (event->code & (1 << 28)
5320 || event->code - FUNCTION_KEY_OFFSET < 0
5321 || (event->code - FUNCTION_KEY_OFFSET
5322 >= sizeof lispy_function_keys / sizeof *lispy_function_keys)
5323 || !lispy_function_keys[event->code - FUNCTION_KEY_OFFSET])
5325 /* We need to use an alist rather than a vector as the cache
5326 since we can't make a vector long enuf. */
5327 if (NILP (KVAR (current_kboard, system_key_syms)))
5328 KVAR (current_kboard, system_key_syms) = Fcons (Qnil, Qnil);
5329 return modify_event_symbol (event->code,
5330 event->modifiers,
5331 Qfunction_key,
5332 KVAR (current_kboard, Vsystem_key_alist),
5333 0, &KVAR (current_kboard, system_key_syms),
5334 (unsigned) -1);
5337 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
5338 event->modifiers,
5339 Qfunction_key, Qnil,
5340 lispy_function_keys, &func_key_syms,
5341 (sizeof (lispy_function_keys)
5342 / sizeof (lispy_function_keys[0])));
5344 #ifdef WINDOWSNT
5345 case MULTIMEDIA_KEY_EVENT:
5346 if (event->code < (sizeof (lispy_multimedia_keys)
5347 / sizeof (lispy_multimedia_keys[0]))
5348 && event->code > 0 && lispy_multimedia_keys[event->code])
5350 return modify_event_symbol (event->code, event->modifiers,
5351 Qfunction_key, Qnil,
5352 lispy_multimedia_keys, &func_key_syms,
5353 (sizeof (lispy_multimedia_keys)
5354 / sizeof (lispy_multimedia_keys[0])));
5356 return Qnil;
5357 #endif
5359 #ifdef HAVE_MOUSE
5360 /* A mouse click. Figure out where it is, decide whether it's
5361 a press, click or drag, and build the appropriate structure. */
5362 case MOUSE_CLICK_EVENT:
5363 #ifndef USE_TOOLKIT_SCROLL_BARS
5364 case SCROLL_BAR_CLICK_EVENT:
5365 #endif
5367 int button = event->code;
5368 int is_double;
5369 Lisp_Object position;
5370 Lisp_Object *start_pos_ptr;
5371 Lisp_Object start_pos;
5373 position = Qnil;
5375 /* Build the position as appropriate for this mouse click. */
5376 if (event->kind == MOUSE_CLICK_EVENT)
5378 struct frame *f = XFRAME (event->frame_or_window);
5379 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5380 int row, column;
5381 #endif
5383 /* Ignore mouse events that were made on frame that
5384 have been deleted. */
5385 if (! FRAME_LIVE_P (f))
5386 return Qnil;
5388 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5389 /* EVENT->x and EVENT->y are frame-relative pixel
5390 coordinates at this place. Under old redisplay, COLUMN
5391 and ROW are set to frame relative glyph coordinates
5392 which are then used to determine whether this click is
5393 in a menu (non-toolkit version). */
5394 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
5395 &column, &row, NULL, 1);
5397 /* In the non-toolkit version, clicks on the menu bar
5398 are ordinary button events in the event buffer.
5399 Distinguish them, and invoke the menu.
5401 (In the toolkit version, the toolkit handles the menu bar
5402 and Emacs doesn't know about it until after the user
5403 makes a selection.) */
5404 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
5405 && (event->modifiers & down_modifier))
5407 Lisp_Object items, item;
5408 int hpos;
5409 int i;
5411 /* Find the menu bar item under `column'. */
5412 item = Qnil;
5413 items = FRAME_MENU_BAR_ITEMS (f);
5414 for (i = 0; i < XVECTOR (items)->size; i += 4)
5416 Lisp_Object pos, string;
5417 string = AREF (items, i + 1);
5418 pos = AREF (items, i + 3);
5419 if (NILP (string))
5420 break;
5421 if (column >= XINT (pos)
5422 && column < XINT (pos) + SCHARS (string))
5424 item = AREF (items, i);
5425 break;
5429 /* ELisp manual 2.4b says (x y) are window relative but
5430 code says they are frame-relative. */
5431 position
5432 = Fcons (event->frame_or_window,
5433 Fcons (Qmenu_bar,
5434 Fcons (Fcons (event->x, event->y),
5435 Fcons (make_number (event->timestamp),
5436 Qnil))));
5438 return Fcons (item, Fcons (position, Qnil));
5440 #endif /* not USE_X_TOOLKIT && not USE_GTK && not HAVE_NS */
5442 position = make_lispy_position (f, event->x, event->y,
5443 event->timestamp);
5445 #ifndef USE_TOOLKIT_SCROLL_BARS
5446 else
5448 /* It's a scrollbar click. */
5449 Lisp_Object window;
5450 Lisp_Object portion_whole;
5451 Lisp_Object part;
5453 window = event->frame_or_window;
5454 portion_whole = Fcons (event->x, event->y);
5455 part = *scroll_bar_parts[(int) event->part];
5457 position
5458 = Fcons (window,
5459 Fcons (Qvertical_scroll_bar,
5460 Fcons (portion_whole,
5461 Fcons (make_number (event->timestamp),
5462 Fcons (part, Qnil)))));
5464 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5466 if (button >= ASIZE (button_down_location))
5468 button_down_location = larger_vector (button_down_location,
5469 button + 1, Qnil);
5470 mouse_syms = larger_vector (mouse_syms, button + 1, Qnil);
5473 start_pos_ptr = &AREF (button_down_location, button);
5474 start_pos = *start_pos_ptr;
5475 *start_pos_ptr = Qnil;
5478 /* On window-system frames, use the value of
5479 double-click-fuzz as is. On other frames, interpret it
5480 as a multiple of 1/8 characters. */
5481 struct frame *f;
5482 int fuzz;
5484 if (WINDOWP (event->frame_or_window))
5485 f = XFRAME (XWINDOW (event->frame_or_window)->frame);
5486 else if (FRAMEP (event->frame_or_window))
5487 f = XFRAME (event->frame_or_window);
5488 else
5489 abort ();
5491 if (FRAME_WINDOW_P (f))
5492 fuzz = double_click_fuzz;
5493 else
5494 fuzz = double_click_fuzz / 8;
5496 is_double = (button == last_mouse_button
5497 && (eabs (XINT (event->x) - last_mouse_x) <= fuzz)
5498 && (eabs (XINT (event->y) - last_mouse_y) <= fuzz)
5499 && button_down_time != 0
5500 && (EQ (Vdouble_click_time, Qt)
5501 || (INTEGERP (Vdouble_click_time)
5502 && ((int)(event->timestamp - button_down_time)
5503 < XINT (Vdouble_click_time)))));
5506 last_mouse_button = button;
5507 last_mouse_x = XINT (event->x);
5508 last_mouse_y = XINT (event->y);
5510 /* If this is a button press, squirrel away the location, so
5511 we can decide later whether it was a click or a drag. */
5512 if (event->modifiers & down_modifier)
5514 if (is_double)
5516 double_click_count++;
5517 event->modifiers |= ((double_click_count > 2)
5518 ? triple_modifier
5519 : double_modifier);
5521 else
5522 double_click_count = 1;
5523 button_down_time = event->timestamp;
5524 *start_pos_ptr = Fcopy_alist (position);
5525 ignore_mouse_drag_p = 0;
5528 /* Now we're releasing a button - check the co-ordinates to
5529 see if this was a click or a drag. */
5530 else if (event->modifiers & up_modifier)
5532 /* If we did not see a down before this up, ignore the up.
5533 Probably this happened because the down event chose a
5534 menu item. It would be an annoyance to treat the
5535 release of the button that chose the menu item as a
5536 separate event. */
5538 if (!CONSP (start_pos))
5539 return Qnil;
5541 event->modifiers &= ~up_modifier;
5544 Lisp_Object new_down, down;
5545 EMACS_INT xdiff = double_click_fuzz, ydiff = double_click_fuzz;
5547 /* The third element of every position
5548 should be the (x,y) pair. */
5549 down = Fcar (Fcdr (Fcdr (start_pos)));
5550 new_down = Fcar (Fcdr (Fcdr (position)));
5552 if (CONSP (down)
5553 && INTEGERP (XCAR (down)) && INTEGERP (XCDR (down)))
5555 xdiff = XINT (XCAR (new_down)) - XINT (XCAR (down));
5556 ydiff = XINT (XCDR (new_down)) - XINT (XCDR (down));
5559 if (ignore_mouse_drag_p)
5561 event->modifiers |= click_modifier;
5562 ignore_mouse_drag_p = 0;
5564 else if (xdiff < double_click_fuzz && xdiff > - double_click_fuzz
5565 && ydiff < double_click_fuzz && ydiff > - double_click_fuzz
5566 /* Maybe the mouse has moved a lot, caused scrolling, and
5567 eventually ended up at the same screen position (but
5568 not buffer position) in which case it is a drag, not
5569 a click. */
5570 /* FIXME: OTOH if the buffer position has changed
5571 because of a timer or process filter rather than
5572 because of mouse movement, it should be considered as
5573 a click. But mouse-drag-region completely ignores
5574 this case and it hasn't caused any real problem, so
5575 it's probably OK to ignore it as well. */
5576 && EQ (Fcar (Fcdr (start_pos)), Fcar (Fcdr (position))))
5577 /* Mouse hasn't moved (much). */
5578 event->modifiers |= click_modifier;
5579 else
5581 button_down_time = 0;
5582 event->modifiers |= drag_modifier;
5585 /* Don't check is_double; treat this as multiple
5586 if the down-event was multiple. */
5587 if (double_click_count > 1)
5588 event->modifiers |= ((double_click_count > 2)
5589 ? triple_modifier
5590 : double_modifier);
5593 else
5594 /* Every mouse event should either have the down_modifier or
5595 the up_modifier set. */
5596 abort ();
5599 /* Get the symbol we should use for the mouse click. */
5600 Lisp_Object head;
5602 head = modify_event_symbol (button,
5603 event->modifiers,
5604 Qmouse_click, Vlispy_mouse_stem,
5605 NULL,
5606 &mouse_syms,
5607 XVECTOR (mouse_syms)->size);
5608 if (event->modifiers & drag_modifier)
5609 return Fcons (head,
5610 Fcons (start_pos,
5611 Fcons (position,
5612 Qnil)));
5613 else if (event->modifiers & (double_modifier | triple_modifier))
5614 return Fcons (head,
5615 Fcons (position,
5616 Fcons (make_number (double_click_count),
5617 Qnil)));
5618 else
5619 return Fcons (head,
5620 Fcons (position,
5621 Qnil));
5625 case WHEEL_EVENT:
5626 case HORIZ_WHEEL_EVENT:
5628 Lisp_Object position;
5629 Lisp_Object head;
5631 /* Build the position as appropriate for this mouse click. */
5632 struct frame *f = XFRAME (event->frame_or_window);
5634 /* Ignore wheel events that were made on frame that have been
5635 deleted. */
5636 if (! FRAME_LIVE_P (f))
5637 return Qnil;
5639 position = make_lispy_position (f, event->x, event->y,
5640 event->timestamp);
5642 /* Set double or triple modifiers to indicate the wheel speed. */
5644 /* On window-system frames, use the value of
5645 double-click-fuzz as is. On other frames, interpret it
5646 as a multiple of 1/8 characters. */
5647 struct frame *fr;
5648 int fuzz;
5649 int symbol_num;
5650 int is_double;
5652 if (WINDOWP (event->frame_or_window))
5653 fr = XFRAME (XWINDOW (event->frame_or_window)->frame);
5654 else if (FRAMEP (event->frame_or_window))
5655 fr = XFRAME (event->frame_or_window);
5656 else
5657 abort ();
5659 fuzz = FRAME_WINDOW_P (fr)
5660 ? double_click_fuzz : double_click_fuzz / 8;
5662 if (event->modifiers & up_modifier)
5664 /* Emit a wheel-up event. */
5665 event->modifiers &= ~up_modifier;
5666 symbol_num = 0;
5668 else if (event->modifiers & down_modifier)
5670 /* Emit a wheel-down event. */
5671 event->modifiers &= ~down_modifier;
5672 symbol_num = 1;
5674 else
5675 /* Every wheel event should either have the down_modifier or
5676 the up_modifier set. */
5677 abort ();
5679 if (event->kind == HORIZ_WHEEL_EVENT)
5680 symbol_num += 2;
5682 is_double = (last_mouse_button == - (1 + symbol_num)
5683 && (eabs (XINT (event->x) - last_mouse_x) <= fuzz)
5684 && (eabs (XINT (event->y) - last_mouse_y) <= fuzz)
5685 && button_down_time != 0
5686 && (EQ (Vdouble_click_time, Qt)
5687 || (INTEGERP (Vdouble_click_time)
5688 && ((int)(event->timestamp - button_down_time)
5689 < XINT (Vdouble_click_time)))));
5690 if (is_double)
5692 double_click_count++;
5693 event->modifiers |= ((double_click_count > 2)
5694 ? triple_modifier
5695 : double_modifier);
5697 else
5699 double_click_count = 1;
5700 event->modifiers |= click_modifier;
5703 button_down_time = event->timestamp;
5704 /* Use a negative value to distinguish wheel from mouse button. */
5705 last_mouse_button = - (1 + symbol_num);
5706 last_mouse_x = XINT (event->x);
5707 last_mouse_y = XINT (event->y);
5709 /* Get the symbol we should use for the wheel event. */
5710 head = modify_event_symbol (symbol_num,
5711 event->modifiers,
5712 Qmouse_click,
5713 Qnil,
5714 lispy_wheel_names,
5715 &wheel_syms,
5716 ASIZE (wheel_syms));
5719 if (event->modifiers & (double_modifier | triple_modifier))
5720 return Fcons (head,
5721 Fcons (position,
5722 Fcons (make_number (double_click_count),
5723 Qnil)));
5724 else
5725 return Fcons (head,
5726 Fcons (position,
5727 Qnil));
5731 #ifdef USE_TOOLKIT_SCROLL_BARS
5733 /* We don't have down and up events if using toolkit scroll bars,
5734 so make this always a click event. Store in the `part' of
5735 the Lisp event a symbol which maps to the following actions:
5737 `above_handle' page up
5738 `below_handle' page down
5739 `up' line up
5740 `down' line down
5741 `top' top of buffer
5742 `bottom' bottom of buffer
5743 `handle' thumb has been dragged.
5744 `end-scroll' end of interaction with scroll bar
5746 The incoming input_event contains in its `part' member an
5747 index of type `enum scroll_bar_part' which we can use as an
5748 index in scroll_bar_parts to get the appropriate symbol. */
5750 case SCROLL_BAR_CLICK_EVENT:
5752 Lisp_Object position, head, window, portion_whole, part;
5754 window = event->frame_or_window;
5755 portion_whole = Fcons (event->x, event->y);
5756 part = *scroll_bar_parts[(int) event->part];
5758 position
5759 = Fcons (window,
5760 Fcons (Qvertical_scroll_bar,
5761 Fcons (portion_whole,
5762 Fcons (make_number (event->timestamp),
5763 Fcons (part, Qnil)))));
5765 /* Always treat scroll bar events as clicks. */
5766 event->modifiers |= click_modifier;
5767 event->modifiers &= ~up_modifier;
5769 if (event->code >= ASIZE (mouse_syms))
5770 mouse_syms = larger_vector (mouse_syms, event->code + 1, Qnil);
5772 /* Get the symbol we should use for the mouse click. */
5773 head = modify_event_symbol (event->code,
5774 event->modifiers,
5775 Qmouse_click,
5776 Vlispy_mouse_stem,
5777 NULL, &mouse_syms,
5778 XVECTOR (mouse_syms)->size);
5779 return Fcons (head, Fcons (position, Qnil));
5782 #endif /* USE_TOOLKIT_SCROLL_BARS */
5784 case DRAG_N_DROP_EVENT:
5786 FRAME_PTR f;
5787 Lisp_Object head, position;
5788 Lisp_Object files;
5790 f = XFRAME (event->frame_or_window);
5791 files = event->arg;
5793 /* Ignore mouse events that were made on frames that
5794 have been deleted. */
5795 if (! FRAME_LIVE_P (f))
5796 return Qnil;
5798 position = make_lispy_position (f, event->x, event->y,
5799 event->timestamp);
5801 head = modify_event_symbol (0, event->modifiers,
5802 Qdrag_n_drop, Qnil,
5803 lispy_drag_n_drop_names,
5804 &drag_n_drop_syms, 1);
5805 return Fcons (head,
5806 Fcons (position,
5807 Fcons (files,
5808 Qnil)));
5810 #endif /* HAVE_MOUSE */
5812 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
5813 || defined(HAVE_NS) || defined (USE_GTK)
5814 case MENU_BAR_EVENT:
5815 if (EQ (event->arg, event->frame_or_window))
5816 /* This is the prefix key. We translate this to
5817 `(menu_bar)' because the code in keyboard.c for menu
5818 events, which we use, relies on this. */
5819 return Fcons (Qmenu_bar, Qnil);
5820 return event->arg;
5821 #endif
5823 case SELECT_WINDOW_EVENT:
5824 /* Make an event (select-window (WINDOW)). */
5825 return Fcons (Qselect_window,
5826 Fcons (Fcons (event->frame_or_window, Qnil),
5827 Qnil));
5829 case TOOL_BAR_EVENT:
5830 if (EQ (event->arg, event->frame_or_window))
5831 /* This is the prefix key. We translate this to
5832 `(tool_bar)' because the code in keyboard.c for tool bar
5833 events, which we use, relies on this. */
5834 return Fcons (Qtool_bar, Qnil);
5835 else if (SYMBOLP (event->arg))
5836 return apply_modifiers (event->modifiers, event->arg);
5837 return event->arg;
5839 case USER_SIGNAL_EVENT:
5840 /* A user signal. */
5842 char *name = find_user_signal_name (event->code);
5843 if (!name)
5844 abort ();
5845 return intern (name);
5848 case SAVE_SESSION_EVENT:
5849 return Qsave_session;
5851 #ifdef HAVE_DBUS
5852 case DBUS_EVENT:
5854 return Fcons (Qdbus_event, event->arg);
5856 #endif /* HAVE_DBUS */
5858 case CONFIG_CHANGED_EVENT:
5859 return Fcons (Qconfig_changed_event,
5860 Fcons (event->arg,
5861 Fcons (event->frame_or_window, Qnil)));
5862 #ifdef HAVE_GPM
5863 case GPM_CLICK_EVENT:
5865 FRAME_PTR f = XFRAME (event->frame_or_window);
5866 Lisp_Object head, position;
5867 Lisp_Object *start_pos_ptr;
5868 Lisp_Object start_pos;
5869 int button = event->code;
5871 if (button >= ASIZE (button_down_location))
5873 button_down_location = larger_vector (button_down_location,
5874 button + 1, Qnil);
5875 mouse_syms = larger_vector (mouse_syms, button + 1, Qnil);
5878 start_pos_ptr = &AREF (button_down_location, button);
5879 start_pos = *start_pos_ptr;
5881 position = make_lispy_position (f, event->x, event->y,
5882 event->timestamp);
5884 if (event->modifiers & down_modifier)
5885 *start_pos_ptr = Fcopy_alist (position);
5886 else if (event->modifiers & (up_modifier | drag_modifier))
5888 if (!CONSP (start_pos))
5889 return Qnil;
5890 event->modifiers &= ~up_modifier;
5893 head = modify_event_symbol (button,
5894 event->modifiers,
5895 Qmouse_click, Vlispy_mouse_stem,
5896 NULL,
5897 &mouse_syms,
5898 XVECTOR (mouse_syms)->size);
5900 if (event->modifiers & drag_modifier)
5901 return Fcons (head,
5902 Fcons (start_pos,
5903 Fcons (position,
5904 Qnil)));
5905 else if (event->modifiers & double_modifier)
5906 return Fcons (head,
5907 Fcons (position,
5908 Fcons (make_number (2),
5909 Qnil)));
5910 else if (event->modifiers & triple_modifier)
5911 return Fcons (head,
5912 Fcons (position,
5913 Fcons (make_number (3),
5914 Qnil)));
5915 else
5916 return Fcons (head,
5917 Fcons (position,
5918 Qnil));
5920 #endif /* HAVE_GPM */
5922 /* The 'kind' field of the event is something we don't recognize. */
5923 default:
5924 abort ();
5928 #if defined(HAVE_MOUSE) || defined(HAVE_GPM)
5930 static Lisp_Object
5931 make_lispy_movement (FRAME_PTR frame, Lisp_Object bar_window, enum scroll_bar_part part,
5932 Lisp_Object x, Lisp_Object y, unsigned long t)
5934 /* Is it a scroll bar movement? */
5935 if (frame && ! NILP (bar_window))
5937 Lisp_Object part_sym;
5939 part_sym = *scroll_bar_parts[(int) part];
5940 return Fcons (Qscroll_bar_movement,
5941 Fcons (list5 (bar_window,
5942 Qvertical_scroll_bar,
5943 Fcons (x, y),
5944 make_number (t),
5945 part_sym),
5946 Qnil));
5948 /* Or is it an ordinary mouse movement? */
5949 else
5951 Lisp_Object position;
5952 position = make_lispy_position (frame, x, y, t);
5953 return list2 (Qmouse_movement, position);
5957 #endif /* HAVE_MOUSE || HAVE GPM */
5959 /* Construct a switch frame event. */
5960 static Lisp_Object
5961 make_lispy_switch_frame (Lisp_Object frame)
5963 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
5966 /* Manipulating modifiers. */
5968 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
5970 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
5971 SYMBOL's name of the end of the modifiers; the string from this
5972 position is the unmodified symbol name.
5974 This doesn't use any caches. */
5976 static int
5977 parse_modifiers_uncached (Lisp_Object symbol, int *modifier_end)
5979 Lisp_Object name;
5980 int i;
5981 int modifiers;
5983 CHECK_SYMBOL (symbol);
5985 modifiers = 0;
5986 name = SYMBOL_NAME (symbol);
5988 for (i = 0; i+2 <= SBYTES (name); )
5990 int this_mod_end = 0;
5991 int this_mod = 0;
5993 /* See if the name continues with a modifier word.
5994 Check that the word appears, but don't check what follows it.
5995 Set this_mod and this_mod_end to record what we find. */
5997 switch (SREF (name, i))
5999 #define SINGLE_LETTER_MOD(BIT) \
6000 (this_mod_end = i + 1, this_mod = BIT)
6002 case 'A':
6003 SINGLE_LETTER_MOD (alt_modifier);
6004 break;
6006 case 'C':
6007 SINGLE_LETTER_MOD (ctrl_modifier);
6008 break;
6010 case 'H':
6011 SINGLE_LETTER_MOD (hyper_modifier);
6012 break;
6014 case 'M':
6015 SINGLE_LETTER_MOD (meta_modifier);
6016 break;
6018 case 'S':
6019 SINGLE_LETTER_MOD (shift_modifier);
6020 break;
6022 case 's':
6023 SINGLE_LETTER_MOD (super_modifier);
6024 break;
6026 #undef SINGLE_LETTER_MOD
6028 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
6029 if (i + LEN + 1 <= SBYTES (name) \
6030 && ! strncmp (SSDATA (name) + i, NAME, LEN)) \
6032 this_mod_end = i + LEN; \
6033 this_mod = BIT; \
6036 case 'd':
6037 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
6038 MULTI_LETTER_MOD (down_modifier, "down", 4);
6039 MULTI_LETTER_MOD (double_modifier, "double", 6);
6040 break;
6042 case 't':
6043 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
6044 break;
6045 #undef MULTI_LETTER_MOD
6049 /* If we found no modifier, stop looking for them. */
6050 if (this_mod_end == 0)
6051 break;
6053 /* Check there is a dash after the modifier, so that it
6054 really is a modifier. */
6055 if (this_mod_end >= SBYTES (name)
6056 || SREF (name, this_mod_end) != '-')
6057 break;
6059 /* This modifier is real; look for another. */
6060 modifiers |= this_mod;
6061 i = this_mod_end + 1;
6064 /* Should we include the `click' modifier? */
6065 if (! (modifiers & (down_modifier | drag_modifier
6066 | double_modifier | triple_modifier))
6067 && i + 7 == SBYTES (name)
6068 && strncmp (SSDATA (name) + i, "mouse-", 6) == 0
6069 && ('0' <= SREF (name, i + 6) && SREF (name, i + 6) <= '9'))
6070 modifiers |= click_modifier;
6072 if (! (modifiers & (double_modifier | triple_modifier))
6073 && i + 6 < SBYTES (name)
6074 && strncmp (SSDATA (name) + i, "wheel-", 6) == 0)
6075 modifiers |= click_modifier;
6077 if (modifier_end)
6078 *modifier_end = i;
6080 return modifiers;
6083 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
6084 prepended to the string BASE[0..BASE_LEN-1].
6085 This doesn't use any caches. */
6086 static Lisp_Object
6087 apply_modifiers_uncached (int modifiers, char *base, int base_len, int base_len_byte)
6089 /* Since BASE could contain nulls, we can't use intern here; we have
6090 to use Fintern, which expects a genuine Lisp_String, and keeps a
6091 reference to it. */
6092 char *new_mods
6093 = (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
6094 int mod_len;
6097 char *p = new_mods;
6099 /* Only the event queue may use the `up' modifier; it should always
6100 be turned into a click or drag event before presented to lisp code. */
6101 if (modifiers & up_modifier)
6102 abort ();
6104 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
6105 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
6106 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
6107 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
6108 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
6109 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
6110 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
6111 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
6112 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
6113 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
6114 /* The click modifier is denoted by the absence of other modifiers. */
6116 *p = '\0';
6118 mod_len = p - new_mods;
6122 Lisp_Object new_name;
6124 new_name = make_uninit_multibyte_string (mod_len + base_len,
6125 mod_len + base_len_byte);
6126 memcpy (SDATA (new_name), new_mods, mod_len);
6127 memcpy (SDATA (new_name) + mod_len, base, base_len_byte);
6129 return Fintern (new_name, Qnil);
6134 static const char *const modifier_names[] =
6136 "up", "down", "drag", "click", "double", "triple", 0, 0,
6137 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6138 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
6140 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
6142 static Lisp_Object modifier_symbols;
6144 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
6145 static Lisp_Object
6146 lispy_modifier_list (int modifiers)
6148 Lisp_Object modifier_list;
6149 int i;
6151 modifier_list = Qnil;
6152 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
6153 if (modifiers & (1<<i))
6154 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
6155 modifier_list);
6157 return modifier_list;
6161 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
6162 where UNMODIFIED is the unmodified form of SYMBOL,
6163 MASK is the set of modifiers present in SYMBOL's name.
6164 This is similar to parse_modifiers_uncached, but uses the cache in
6165 SYMBOL's Qevent_symbol_element_mask property, and maintains the
6166 Qevent_symbol_elements property. */
6168 #define KEY_TO_CHAR(k) (XINT (k) & ((1 << CHARACTERBITS) - 1))
6170 Lisp_Object
6171 parse_modifiers (Lisp_Object symbol)
6173 Lisp_Object elements;
6175 if (INTEGERP (symbol))
6176 return (Fcons (make_number (KEY_TO_CHAR (symbol)),
6177 Fcons (make_number (XINT (symbol) & CHAR_MODIFIER_MASK),
6178 Qnil)));
6179 else if (!SYMBOLP (symbol))
6180 return Qnil;
6182 elements = Fget (symbol, Qevent_symbol_element_mask);
6183 if (CONSP (elements))
6184 return elements;
6185 else
6187 int end;
6188 int modifiers = parse_modifiers_uncached (symbol, &end);
6189 Lisp_Object unmodified;
6190 Lisp_Object mask;
6192 unmodified = Fintern (make_string (SSDATA (SYMBOL_NAME (symbol)) + end,
6193 SBYTES (SYMBOL_NAME (symbol)) - end),
6194 Qnil);
6196 if (modifiers & ~INTMASK)
6197 abort ();
6198 XSETFASTINT (mask, modifiers);
6199 elements = Fcons (unmodified, Fcons (mask, Qnil));
6201 /* Cache the parsing results on SYMBOL. */
6202 Fput (symbol, Qevent_symbol_element_mask,
6203 elements);
6204 Fput (symbol, Qevent_symbol_elements,
6205 Fcons (unmodified, lispy_modifier_list (modifiers)));
6207 /* Since we know that SYMBOL is modifiers applied to unmodified,
6208 it would be nice to put that in unmodified's cache.
6209 But we can't, since we're not sure that parse_modifiers is
6210 canonical. */
6212 return elements;
6216 DEFUN ("internal-event-symbol-parse-modifiers", Fevent_symbol_parse_modifiers,
6217 Sevent_symbol_parse_modifiers, 1, 1, 0,
6218 doc: /* Parse the event symbol. For internal use. */)
6219 (Lisp_Object symbol)
6221 /* Fill the cache if needed. */
6222 parse_modifiers (symbol);
6223 /* Ignore the result (which is stored on Qevent_symbol_element_mask)
6224 and use the Lispier representation stored on Qevent_symbol_elements
6225 instead. */
6226 return Fget (symbol, Qevent_symbol_elements);
6229 /* Apply the modifiers MODIFIERS to the symbol BASE.
6230 BASE must be unmodified.
6232 This is like apply_modifiers_uncached, but uses BASE's
6233 Qmodifier_cache property, if present. It also builds
6234 Qevent_symbol_elements properties, since it has that info anyway.
6236 apply_modifiers copies the value of BASE's Qevent_kind property to
6237 the modified symbol. */
6238 static Lisp_Object
6239 apply_modifiers (int modifiers, Lisp_Object base)
6241 Lisp_Object cache, idx, entry, new_symbol;
6243 /* Mask out upper bits. We don't know where this value's been. */
6244 modifiers &= INTMASK;
6246 if (INTEGERP (base))
6247 return make_number (XINT (base) | modifiers);
6249 /* The click modifier never figures into cache indices. */
6250 cache = Fget (base, Qmodifier_cache);
6251 XSETFASTINT (idx, (modifiers & ~click_modifier));
6252 entry = assq_no_quit (idx, cache);
6254 if (CONSP (entry))
6255 new_symbol = XCDR (entry);
6256 else
6258 /* We have to create the symbol ourselves. */
6259 new_symbol = apply_modifiers_uncached (modifiers,
6260 SSDATA (SYMBOL_NAME (base)),
6261 SCHARS (SYMBOL_NAME (base)),
6262 SBYTES (SYMBOL_NAME (base)));
6264 /* Add the new symbol to the base's cache. */
6265 entry = Fcons (idx, new_symbol);
6266 Fput (base, Qmodifier_cache, Fcons (entry, cache));
6268 /* We have the parsing info now for free, so we could add it to
6269 the caches:
6270 XSETFASTINT (idx, modifiers);
6271 Fput (new_symbol, Qevent_symbol_element_mask,
6272 Fcons (base, Fcons (idx, Qnil)));
6273 Fput (new_symbol, Qevent_symbol_elements,
6274 Fcons (base, lispy_modifier_list (modifiers)));
6275 Sadly, this is only correct if `base' is indeed a base event,
6276 which is not necessarily the case. -stef */
6279 /* Make sure this symbol is of the same kind as BASE.
6281 You'd think we could just set this once and for all when we
6282 intern the symbol above, but reorder_modifiers may call us when
6283 BASE's property isn't set right; we can't assume that just
6284 because it has a Qmodifier_cache property it must have its
6285 Qevent_kind set right as well. */
6286 if (NILP (Fget (new_symbol, Qevent_kind)))
6288 Lisp_Object kind;
6290 kind = Fget (base, Qevent_kind);
6291 if (! NILP (kind))
6292 Fput (new_symbol, Qevent_kind, kind);
6295 return new_symbol;
6299 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
6300 return a symbol with the modifiers placed in the canonical order.
6301 Canonical order is alphabetical, except for down and drag, which
6302 always come last. The 'click' modifier is never written out.
6304 Fdefine_key calls this to make sure that (for example) C-M-foo
6305 and M-C-foo end up being equivalent in the keymap. */
6307 Lisp_Object
6308 reorder_modifiers (Lisp_Object symbol)
6310 /* It's hopefully okay to write the code this way, since everything
6311 will soon be in caches, and no consing will be done at all. */
6312 Lisp_Object parsed;
6314 parsed = parse_modifiers (symbol);
6315 return apply_modifiers ((int) XINT (XCAR (XCDR (parsed))),
6316 XCAR (parsed));
6320 /* For handling events, we often want to produce a symbol whose name
6321 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
6322 to some base, like the name of a function key or mouse button.
6323 modify_event_symbol produces symbols of this sort.
6325 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
6326 is the name of the i'th symbol. TABLE_SIZE is the number of elements
6327 in the table.
6329 Alternatively, NAME_ALIST_OR_STEM is either an alist mapping codes
6330 into symbol names, or a string specifying a name stem used to
6331 construct a symbol name or the form `STEM-N', where N is the decimal
6332 representation of SYMBOL_NUM. NAME_ALIST_OR_STEM is used if it is
6333 non-nil; otherwise NAME_TABLE is used.
6335 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
6336 persist between calls to modify_event_symbol that it can use to
6337 store a cache of the symbols it's generated for this NAME_TABLE
6338 before. The object stored there may be a vector or an alist.
6340 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
6342 MODIFIERS is a set of modifier bits (as given in struct input_events)
6343 whose prefixes should be applied to the symbol name.
6345 SYMBOL_KIND is the value to be placed in the event_kind property of
6346 the returned symbol.
6348 The symbols we create are supposed to have an
6349 `event-symbol-elements' property, which lists the modifiers present
6350 in the symbol's name. */
6352 static Lisp_Object
6353 modify_event_symbol (EMACS_INT symbol_num, unsigned int modifiers, Lisp_Object symbol_kind,
6354 Lisp_Object name_alist_or_stem, const char *const *name_table,
6355 Lisp_Object *symbol_table, unsigned int table_size)
6357 Lisp_Object value;
6358 Lisp_Object symbol_int;
6360 /* Get rid of the "vendor-specific" bit here. */
6361 XSETINT (symbol_int, symbol_num & 0xffffff);
6363 /* Is this a request for a valid symbol? */
6364 if (symbol_num < 0 || symbol_num >= table_size)
6365 return Qnil;
6367 if (CONSP (*symbol_table))
6368 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
6370 /* If *symbol_table doesn't seem to be initialized properly, fix that.
6371 *symbol_table should be a lisp vector TABLE_SIZE elements long,
6372 where the Nth element is the symbol for NAME_TABLE[N], or nil if
6373 we've never used that symbol before. */
6374 else
6376 if (! VECTORP (*symbol_table)
6377 || XVECTOR (*symbol_table)->size != table_size)
6379 Lisp_Object size;
6381 XSETFASTINT (size, table_size);
6382 *symbol_table = Fmake_vector (size, Qnil);
6385 value = XVECTOR (*symbol_table)->contents[symbol_num];
6388 /* Have we already used this symbol before? */
6389 if (NILP (value))
6391 /* No; let's create it. */
6392 if (CONSP (name_alist_or_stem))
6393 value = Fcdr_safe (Fassq (symbol_int, name_alist_or_stem));
6394 else if (STRINGP (name_alist_or_stem))
6396 int len = SBYTES (name_alist_or_stem);
6397 char *buf = (char *) alloca (len + 50);
6398 sprintf (buf, "%s-%ld", SDATA (name_alist_or_stem),
6399 (long) XINT (symbol_int) + 1);
6400 value = intern (buf);
6402 else if (name_table != 0 && name_table[symbol_num])
6403 value = intern (name_table[symbol_num]);
6405 #ifdef HAVE_WINDOW_SYSTEM
6406 if (NILP (value))
6408 char *name = x_get_keysym_name (symbol_num);
6409 if (name)
6410 value = intern (name);
6412 #endif
6414 if (NILP (value))
6416 char buf[20];
6417 sprintf (buf, "key-%ld", (long)symbol_num);
6418 value = intern (buf);
6421 if (CONSP (*symbol_table))
6422 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
6423 else
6424 XVECTOR (*symbol_table)->contents[symbol_num] = value;
6426 /* Fill in the cache entries for this symbol; this also
6427 builds the Qevent_symbol_elements property, which the user
6428 cares about. */
6429 apply_modifiers (modifiers & click_modifier, value);
6430 Fput (value, Qevent_kind, symbol_kind);
6433 /* Apply modifiers to that symbol. */
6434 return apply_modifiers (modifiers, value);
6437 /* Convert a list that represents an event type,
6438 such as (ctrl meta backspace), into the usual representation of that
6439 event type as a number or a symbol. */
6441 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
6442 doc: /* Convert the event description list EVENT-DESC to an event type.
6443 EVENT-DESC should contain one base event type (a character or symbol)
6444 and zero or more modifier names (control, meta, hyper, super, shift, alt,
6445 drag, down, double or triple). The base must be last.
6446 The return value is an event type (a character or symbol) which
6447 has the same base event type and all the specified modifiers. */)
6448 (Lisp_Object event_desc)
6450 Lisp_Object base;
6451 int modifiers = 0;
6452 Lisp_Object rest;
6454 base = Qnil;
6455 rest = event_desc;
6456 while (CONSP (rest))
6458 Lisp_Object elt;
6459 int this = 0;
6461 elt = XCAR (rest);
6462 rest = XCDR (rest);
6464 /* Given a symbol, see if it is a modifier name. */
6465 if (SYMBOLP (elt) && CONSP (rest))
6466 this = parse_solitary_modifier (elt);
6468 if (this != 0)
6469 modifiers |= this;
6470 else if (!NILP (base))
6471 error ("Two bases given in one event");
6472 else
6473 base = elt;
6477 /* Let the symbol A refer to the character A. */
6478 if (SYMBOLP (base) && SCHARS (SYMBOL_NAME (base)) == 1)
6479 XSETINT (base, SREF (SYMBOL_NAME (base), 0));
6481 if (INTEGERP (base))
6483 /* Turn (shift a) into A. */
6484 if ((modifiers & shift_modifier) != 0
6485 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
6487 XSETINT (base, XINT (base) - ('a' - 'A'));
6488 modifiers &= ~shift_modifier;
6491 /* Turn (control a) into C-a. */
6492 if (modifiers & ctrl_modifier)
6493 return make_number ((modifiers & ~ctrl_modifier)
6494 | make_ctrl_char (XINT (base)));
6495 else
6496 return make_number (modifiers | XINT (base));
6498 else if (SYMBOLP (base))
6499 return apply_modifiers (modifiers, base);
6500 else
6502 error ("Invalid base event");
6503 return Qnil;
6507 /* Try to recognize SYMBOL as a modifier name.
6508 Return the modifier flag bit, or 0 if not recognized. */
6511 parse_solitary_modifier (Lisp_Object symbol)
6513 Lisp_Object name = SYMBOL_NAME (symbol);
6515 switch (SREF (name, 0))
6517 #define SINGLE_LETTER_MOD(BIT) \
6518 if (SBYTES (name) == 1) \
6519 return BIT;
6521 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
6522 if (LEN == SBYTES (name) \
6523 && ! strncmp (SSDATA (name), NAME, LEN)) \
6524 return BIT;
6526 case 'A':
6527 SINGLE_LETTER_MOD (alt_modifier);
6528 break;
6530 case 'a':
6531 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
6532 break;
6534 case 'C':
6535 SINGLE_LETTER_MOD (ctrl_modifier);
6536 break;
6538 case 'c':
6539 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
6540 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
6541 break;
6543 case 'H':
6544 SINGLE_LETTER_MOD (hyper_modifier);
6545 break;
6547 case 'h':
6548 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
6549 break;
6551 case 'M':
6552 SINGLE_LETTER_MOD (meta_modifier);
6553 break;
6555 case 'm':
6556 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
6557 break;
6559 case 'S':
6560 SINGLE_LETTER_MOD (shift_modifier);
6561 break;
6563 case 's':
6564 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
6565 MULTI_LETTER_MOD (super_modifier, "super", 5);
6566 SINGLE_LETTER_MOD (super_modifier);
6567 break;
6569 case 'd':
6570 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
6571 MULTI_LETTER_MOD (down_modifier, "down", 4);
6572 MULTI_LETTER_MOD (double_modifier, "double", 6);
6573 break;
6575 case 't':
6576 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
6577 break;
6579 #undef SINGLE_LETTER_MOD
6580 #undef MULTI_LETTER_MOD
6583 return 0;
6586 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
6587 Such a list is not valid as an event,
6588 but it can be a Lucid-style event type list. */
6591 lucid_event_type_list_p (Lisp_Object object)
6593 Lisp_Object tail;
6595 if (! CONSP (object))
6596 return 0;
6598 if (EQ (XCAR (object), Qhelp_echo)
6599 || EQ (XCAR (object), Qvertical_line)
6600 || EQ (XCAR (object), Qmode_line)
6601 || EQ (XCAR (object), Qheader_line))
6602 return 0;
6604 for (tail = object; CONSP (tail); tail = XCDR (tail))
6606 Lisp_Object elt;
6607 elt = XCAR (tail);
6608 if (! (INTEGERP (elt) || SYMBOLP (elt)))
6609 return 0;
6612 return NILP (tail);
6615 /* Store into *addr a value nonzero if terminal input chars are available.
6616 Serves the purpose of ioctl (0, FIONREAD, addr)
6617 but works even if FIONREAD does not exist.
6618 (In fact, this may actually read some input.)
6620 If READABLE_EVENTS_DO_TIMERS_NOW is set in FLAGS, actually run
6621 timer events that are ripe.
6622 If READABLE_EVENTS_FILTER_EVENTS is set in FLAGS, ignore internal
6623 events (FOCUS_IN_EVENT).
6624 If READABLE_EVENTS_IGNORE_SQUEEZABLES is set in FLAGS, ignore mouse
6625 movements and toolkit scroll bar thumb drags. */
6627 static void
6628 get_input_pending (int *addr, int flags)
6630 /* First of all, have we already counted some input? */
6631 *addr = (!NILP (Vquit_flag) || readable_events (flags));
6633 /* If input is being read as it arrives, and we have none, there is none. */
6634 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
6635 return;
6637 /* Try to read some input and see how much we get. */
6638 gobble_input (0);
6639 *addr = (!NILP (Vquit_flag) || readable_events (flags));
6642 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
6644 void
6645 gobble_input (int expected)
6647 #ifdef SIGIO
6648 if (interrupt_input)
6650 SIGMASKTYPE mask;
6651 mask = sigblock (sigmask (SIGIO));
6652 read_avail_input (expected);
6653 sigsetmask (mask);
6655 else
6656 #ifdef POLL_FOR_INPUT
6657 /* XXX This condition was (read_socket_hook && !interrupt_input),
6658 but read_socket_hook is not global anymore. Let's pretend that
6659 it's always set. */
6660 if (!interrupt_input && poll_suppress_count == 0)
6662 SIGMASKTYPE mask;
6663 mask = sigblock (sigmask (SIGALRM));
6664 read_avail_input (expected);
6665 sigsetmask (mask);
6667 else
6668 #endif
6669 #endif
6670 read_avail_input (expected);
6673 /* Put a BUFFER_SWITCH_EVENT in the buffer
6674 so that read_key_sequence will notice the new current buffer. */
6676 void
6677 record_asynch_buffer_change (void)
6679 struct input_event event;
6680 Lisp_Object tem;
6681 EVENT_INIT (event);
6683 event.kind = BUFFER_SWITCH_EVENT;
6684 event.frame_or_window = Qnil;
6685 event.arg = Qnil;
6687 /* We don't need a buffer-switch event unless Emacs is waiting for input.
6688 The purpose of the event is to make read_key_sequence look up the
6689 keymaps again. If we aren't in read_key_sequence, we don't need one,
6690 and the event could cause trouble by messing up (input-pending-p).
6691 Note: Fwaiting_for_user_input_p always returns nil when async
6692 subprocesses aren't supported. */
6693 tem = Fwaiting_for_user_input_p ();
6694 if (NILP (tem))
6695 return;
6697 /* Make sure no interrupt happens while storing the event. */
6698 #ifdef SIGIO
6699 if (interrupt_input)
6701 SIGMASKTYPE mask;
6702 mask = sigblock (sigmask (SIGIO));
6703 kbd_buffer_store_event (&event);
6704 sigsetmask (mask);
6706 else
6707 #endif
6709 stop_polling ();
6710 kbd_buffer_store_event (&event);
6711 start_polling ();
6715 /* Read any terminal input already buffered up by the system
6716 into the kbd_buffer, but do not wait.
6718 EXPECTED should be nonzero if the caller knows there is some input.
6720 Returns the number of keyboard chars read, or -1 meaning
6721 this is a bad time to try to read input. */
6723 static int
6724 read_avail_input (int expected)
6726 int nread = 0;
6727 int err = 0;
6728 struct terminal *t;
6730 /* Store pending user signal events, if any. */
6731 if (store_user_signal_events ())
6732 expected = 0;
6734 /* Loop through the available terminals, and call their input hooks. */
6735 t = terminal_list;
6736 while (t)
6738 struct terminal *next = t->next_terminal;
6740 if (t->read_socket_hook)
6742 int nr;
6743 struct input_event hold_quit;
6745 EVENT_INIT (hold_quit);
6746 hold_quit.kind = NO_EVENT;
6748 /* No need for FIONREAD or fcntl; just say don't wait. */
6749 while (nr = (*t->read_socket_hook) (t, expected, &hold_quit), nr > 0)
6751 nread += nr;
6752 expected = 0;
6755 if (nr == -1) /* Not OK to read input now. */
6757 err = 1;
6759 else if (nr == -2) /* Non-transient error. */
6761 /* The terminal device terminated; it should be closed. */
6763 /* Kill Emacs if this was our last terminal. */
6764 if (!terminal_list->next_terminal)
6765 /* Formerly simply reported no input, but that
6766 sometimes led to a failure of Emacs to terminate.
6767 SIGHUP seems appropriate if we can't reach the
6768 terminal. */
6769 /* ??? Is it really right to send the signal just to
6770 this process rather than to the whole process
6771 group? Perhaps on systems with FIONREAD Emacs is
6772 alone in its group. */
6773 kill (getpid (), SIGHUP);
6775 /* XXX Is calling delete_terminal safe here? It calls delete_frame. */
6777 Lisp_Object tmp;
6778 XSETTERMINAL (tmp, t);
6779 Fdelete_terminal (tmp, Qnoelisp);
6783 if (hold_quit.kind != NO_EVENT)
6784 kbd_buffer_store_event (&hold_quit);
6787 t = next;
6790 if (err && !nread)
6791 nread = -1;
6793 frame_make_pointer_visible ();
6795 return nread;
6798 static void
6799 decode_keyboard_code (struct tty_display_info *tty,
6800 struct coding_system *coding,
6801 unsigned char *buf, int nbytes)
6803 unsigned char *src = buf;
6804 const unsigned char *p;
6805 int i;
6807 if (nbytes == 0)
6808 return;
6809 if (tty->meta_key != 2)
6810 for (i = 0; i < nbytes; i++)
6811 buf[i] &= ~0x80;
6812 if (coding->carryover_bytes > 0)
6814 src = alloca (coding->carryover_bytes + nbytes);
6815 memcpy (src, coding->carryover, coding->carryover_bytes);
6816 memcpy (src + coding->carryover_bytes, buf, nbytes);
6817 nbytes += coding->carryover_bytes;
6819 coding->destination = alloca (nbytes * 4);
6820 coding->dst_bytes = nbytes * 4;
6821 decode_coding_c_string (coding, src, nbytes, Qnil);
6822 if (coding->produced_char == 0)
6823 return;
6824 for (i = 0, p = coding->destination; i < coding->produced_char; i++)
6826 struct input_event event_buf;
6828 EVENT_INIT (event_buf);
6829 event_buf.code = STRING_CHAR_ADVANCE (p);
6830 event_buf.kind =
6831 (ASCII_CHAR_P (event_buf.code)
6832 ? ASCII_KEYSTROKE_EVENT : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6833 /* See the comment in tty_read_avail_input. */
6834 event_buf.frame_or_window = tty->top_frame;
6835 event_buf.arg = Qnil;
6836 kbd_buffer_store_event (&event_buf);
6840 /* This is the tty way of reading available input.
6842 Note that each terminal device has its own `struct terminal' object,
6843 and so this function is called once for each individual termcap
6844 terminal. The first parameter indicates which terminal to read from. */
6847 tty_read_avail_input (struct terminal *terminal,
6848 int expected,
6849 struct input_event *hold_quit)
6851 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
6852 the kbd_buffer can really hold. That may prevent loss
6853 of characters on some systems when input is stuffed at us. */
6854 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
6855 int n_to_read, i;
6856 struct tty_display_info *tty = terminal->display_info.tty;
6857 int nread = 0;
6858 #ifdef subprocesses
6859 int buffer_free = KBD_BUFFER_SIZE - kbd_buffer_nr_stored () - 1;
6861 if (kbd_on_hold_p () || buffer_free <= 0)
6862 return 0;
6863 #endif /* subprocesses */
6865 if (!terminal->name) /* Don't read from a dead terminal. */
6866 return 0;
6868 if (terminal->type != output_termcap
6869 && terminal->type != output_msdos_raw)
6870 abort ();
6872 /* XXX I think the following code should be moved to separate hook
6873 functions in system-dependent files. */
6874 #ifdef WINDOWSNT
6875 return 0;
6876 #else /* not WINDOWSNT */
6877 if (! tty->term_initted) /* In case we get called during bootstrap. */
6878 return 0;
6880 if (! tty->input)
6881 return 0; /* The terminal is suspended. */
6883 #ifdef MSDOS
6884 n_to_read = dos_keysns ();
6885 if (n_to_read == 0)
6886 return 0;
6888 cbuf[0] = dos_keyread ();
6889 nread = 1;
6891 #else /* not MSDOS */
6892 #ifdef HAVE_GPM
6893 if (gpm_tty == tty)
6895 Gpm_Event event;
6896 struct input_event gpm_hold_quit;
6897 int gpm, fd = gpm_fd;
6899 EVENT_INIT (gpm_hold_quit);
6900 gpm_hold_quit.kind = NO_EVENT;
6902 /* gpm==1 if event received.
6903 gpm==0 if the GPM daemon has closed the connection, in which case
6904 Gpm_GetEvent closes gpm_fd and clears it to -1, which is why
6905 we save it in `fd' so close_gpm can remove it from the
6906 select masks.
6907 gpm==-1 if a protocol error or EWOULDBLOCK; the latter is normal. */
6908 while (gpm = Gpm_GetEvent (&event), gpm == 1) {
6909 nread += handle_one_term_event (tty, &event, &gpm_hold_quit);
6911 if (gpm == 0)
6912 /* Presumably the GPM daemon has closed the connection. */
6913 close_gpm (fd);
6914 if (gpm_hold_quit.kind != NO_EVENT)
6915 kbd_buffer_store_event (&gpm_hold_quit);
6916 if (nread)
6917 return nread;
6919 #endif /* HAVE_GPM */
6921 /* Determine how many characters we should *try* to read. */
6922 #ifdef FIONREAD
6923 /* Find out how much input is available. */
6924 if (ioctl (fileno (tty->input), FIONREAD, &n_to_read) < 0)
6926 if (! noninteractive)
6927 return -2; /* Close this terminal. */
6928 else
6929 n_to_read = 0;
6931 if (n_to_read == 0)
6932 return 0;
6933 if (n_to_read > sizeof cbuf)
6934 n_to_read = sizeof cbuf;
6935 #else /* no FIONREAD */
6936 #if defined (USG) || defined(CYGWIN)
6937 /* Read some input if available, but don't wait. */
6938 n_to_read = sizeof cbuf;
6939 fcntl (fileno (tty->input), F_SETFL, O_NDELAY);
6940 #else
6941 you lose;
6942 #endif
6943 #endif
6945 #ifdef subprocesses
6946 /* Don't read more than we can store. */
6947 if (n_to_read > buffer_free)
6948 n_to_read = buffer_free;
6949 #endif /* subprocesses */
6951 /* Now read; for one reason or another, this will not block.
6952 NREAD is set to the number of chars read. */
6955 nread = emacs_read (fileno (tty->input), (char *) cbuf, n_to_read);
6956 /* POSIX infers that processes which are not in the session leader's
6957 process group won't get SIGHUP's at logout time. BSDI adheres to
6958 this part standard and returns -1 from read (0) with errno==EIO
6959 when the control tty is taken away.
6960 Jeffrey Honig <jch@bsdi.com> says this is generally safe. */
6961 if (nread == -1 && errno == EIO)
6962 return -2; /* Close this terminal. */
6963 #if defined (AIX) && defined (_BSD)
6964 /* The kernel sometimes fails to deliver SIGHUP for ptys.
6965 This looks incorrect, but it isn't, because _BSD causes
6966 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
6967 and that causes a value other than 0 when there is no input. */
6968 if (nread == 0)
6969 return -2; /* Close this terminal. */
6970 #endif
6972 while (
6973 /* We used to retry the read if it was interrupted.
6974 But this does the wrong thing when O_NDELAY causes
6975 an EAGAIN error. Does anybody know of a situation
6976 where a retry is actually needed? */
6977 #if 0
6978 nread < 0 && (errno == EAGAIN
6979 #ifdef EFAULT
6980 || errno == EFAULT
6981 #endif
6982 #ifdef EBADSLT
6983 || errno == EBADSLT
6984 #endif
6986 #else
6988 #endif
6991 #ifndef FIONREAD
6992 #if defined (USG) || defined (CYGWIN)
6993 fcntl (fileno (tty->input), F_SETFL, 0);
6994 #endif /* USG or CYGWIN */
6995 #endif /* no FIONREAD */
6997 if (nread <= 0)
6998 return nread;
7000 #endif /* not MSDOS */
7001 #endif /* not WINDOWSNT */
7003 if (TERMINAL_KEYBOARD_CODING (terminal)->common_flags
7004 & CODING_REQUIRE_DECODING_MASK)
7006 struct coding_system *coding = TERMINAL_KEYBOARD_CODING (terminal);
7007 int from;
7009 /* Decode the key sequence except for those with meta
7010 modifiers. */
7011 for (i = from = 0; ; i++)
7012 if (i == nread || (tty->meta_key == 1 && (cbuf[i] & 0x80)))
7014 struct input_event buf;
7016 decode_keyboard_code (tty, coding, cbuf + from, i - from);
7017 if (i == nread)
7018 break;
7020 EVENT_INIT (buf);
7021 buf.kind = ASCII_KEYSTROKE_EVENT;
7022 buf.modifiers = meta_modifier;
7023 buf.code = cbuf[i] & ~0x80;
7024 /* See the comment below. */
7025 buf.frame_or_window = tty->top_frame;
7026 buf.arg = Qnil;
7027 kbd_buffer_store_event (&buf);
7028 from = i + 1;
7030 return nread;
7033 for (i = 0; i < nread; i++)
7035 struct input_event buf;
7036 EVENT_INIT (buf);
7037 buf.kind = ASCII_KEYSTROKE_EVENT;
7038 buf.modifiers = 0;
7039 if (tty->meta_key == 1 && (cbuf[i] & 0x80))
7040 buf.modifiers = meta_modifier;
7041 if (tty->meta_key != 2)
7042 cbuf[i] &= ~0x80;
7044 buf.code = cbuf[i];
7045 /* Set the frame corresponding to the active tty. Note that the
7046 value of selected_frame is not reliable here, redisplay tends
7047 to temporarily change it. */
7048 buf.frame_or_window = tty->top_frame;
7049 buf.arg = Qnil;
7051 kbd_buffer_store_event (&buf);
7052 /* Don't look at input that follows a C-g too closely.
7053 This reduces lossage due to autorepeat on C-g. */
7054 if (buf.kind == ASCII_KEYSTROKE_EVENT
7055 && buf.code == quit_char)
7056 break;
7059 return nread;
7062 static void
7063 handle_async_input (void)
7065 interrupt_input_pending = 0;
7066 #ifdef SYNC_INPUT
7067 pending_signals = pending_atimers;
7068 #endif
7069 /* Tell ns_read_socket() it is being called asynchronously so it can avoid
7070 doing anything dangerous. */
7071 #ifdef HAVE_NS
7072 ++handling_signal;
7073 #endif
7074 while (1)
7076 int nread;
7077 nread = read_avail_input (1);
7078 /* -1 means it's not ok to read the input now.
7079 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
7080 0 means there was no keyboard input available. */
7081 if (nread <= 0)
7082 break;
7084 #ifdef HAVE_NS
7085 --handling_signal;
7086 #endif
7089 void
7090 process_pending_signals (void)
7092 if (interrupt_input_pending)
7093 handle_async_input ();
7094 do_pending_atimers ();
7097 #ifdef SIGIO /* for entire page */
7098 /* Note SIGIO has been undef'd if FIONREAD is missing. */
7100 static SIGTYPE
7101 input_available_signal (int signo)
7103 /* Must preserve main program's value of errno. */
7104 int old_errno = errno;
7105 SIGNAL_THREAD_CHECK (signo);
7107 #ifdef SYNC_INPUT
7108 interrupt_input_pending = 1;
7109 pending_signals = 1;
7110 #endif
7112 if (input_available_clear_time)
7113 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
7115 #ifndef SYNC_INPUT
7116 handle_async_input ();
7117 #endif
7119 errno = old_errno;
7121 #endif /* SIGIO */
7123 /* Send ourselves a SIGIO.
7125 This function exists so that the UNBLOCK_INPUT macro in
7126 blockinput.h can have some way to take care of input we put off
7127 dealing with, without assuming that every file which uses
7128 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
7129 void
7130 reinvoke_input_signal (void)
7132 #ifdef SIGIO
7133 handle_async_input ();
7134 #endif
7139 /* User signal events. */
7141 struct user_signal_info
7143 /* Signal number. */
7144 int sig;
7146 /* Name of the signal. */
7147 char *name;
7149 /* Number of pending signals. */
7150 int npending;
7152 struct user_signal_info *next;
7155 /* List of user signals. */
7156 static struct user_signal_info *user_signals = NULL;
7158 void
7159 add_user_signal (int sig, const char *name)
7161 struct user_signal_info *p;
7163 for (p = user_signals; p; p = p->next)
7164 if (p->sig == sig)
7165 /* Already added. */
7166 return;
7168 p = xmalloc (sizeof (struct user_signal_info));
7169 p->sig = sig;
7170 p->name = xstrdup (name);
7171 p->npending = 0;
7172 p->next = user_signals;
7173 user_signals = p;
7175 signal (sig, handle_user_signal);
7178 static SIGTYPE
7179 handle_user_signal (int sig)
7181 int old_errno = errno;
7182 struct user_signal_info *p;
7184 SIGNAL_THREAD_CHECK (sig);
7186 for (p = user_signals; p; p = p->next)
7187 if (p->sig == sig)
7189 p->npending++;
7190 #ifdef SIGIO
7191 if (interrupt_input)
7192 kill (getpid (), SIGIO);
7193 else
7194 #endif
7196 /* Tell wait_reading_process_output that it needs to wake
7197 up and look around. */
7198 if (input_available_clear_time)
7199 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
7201 break;
7204 errno = old_errno;
7207 static char *
7208 find_user_signal_name (int sig)
7210 struct user_signal_info *p;
7212 for (p = user_signals; p; p = p->next)
7213 if (p->sig == sig)
7214 return p->name;
7216 return NULL;
7219 static int
7220 store_user_signal_events (void)
7222 struct user_signal_info *p;
7223 struct input_event buf;
7224 int nstored = 0;
7226 for (p = user_signals; p; p = p->next)
7227 if (p->npending > 0)
7229 SIGMASKTYPE mask;
7231 if (nstored == 0)
7233 memset (&buf, 0, sizeof buf);
7234 buf.kind = USER_SIGNAL_EVENT;
7235 buf.frame_or_window = selected_frame;
7237 nstored += p->npending;
7239 mask = sigblock (sigmask (p->sig));
7242 buf.code = p->sig;
7243 kbd_buffer_store_event (&buf);
7244 p->npending--;
7246 while (p->npending > 0);
7247 sigsetmask (mask);
7250 return nstored;
7254 static void menu_bar_item (Lisp_Object, Lisp_Object, Lisp_Object, void*);
7255 static Lisp_Object menu_bar_one_keymap_changed_items;
7257 /* These variables hold the vector under construction within
7258 menu_bar_items and its subroutines, and the current index
7259 for storing into that vector. */
7260 static Lisp_Object menu_bar_items_vector;
7261 static int menu_bar_items_index;
7264 static const char* separator_names[] = {
7265 "space",
7266 "no-line",
7267 "single-line",
7268 "double-line",
7269 "single-dashed-line",
7270 "double-dashed-line",
7271 "shadow-etched-in",
7272 "shadow-etched-out",
7273 "shadow-etched-in-dash",
7274 "shadow-etched-out-dash",
7275 "shadow-double-etched-in",
7276 "shadow-double-etched-out",
7277 "shadow-double-etched-in-dash",
7278 "shadow-double-etched-out-dash",
7282 /* Return non-zero if LABEL specifies a separator. */
7285 menu_separator_name_p (const char *label)
7287 if (!label)
7288 return 0;
7289 else if (strlen (label) > 3
7290 && strncmp (label, "--", 2) == 0
7291 && label[2] != '-')
7293 int i;
7294 label += 2;
7295 for (i = 0; separator_names[i]; ++i)
7296 if (strcmp (label, separator_names[i]) == 0)
7297 return 1;
7299 else
7301 /* It's a separator if it contains only dashes. */
7302 while (*label == '-')
7303 ++label;
7304 return (*label == 0);
7307 return 0;
7311 /* Return a vector of menu items for a menu bar, appropriate
7312 to the current buffer. Each item has three elements in the vector:
7313 KEY STRING MAPLIST.
7315 OLD is an old vector we can optionally reuse, or nil. */
7317 Lisp_Object
7318 menu_bar_items (Lisp_Object old)
7320 /* The number of keymaps we're scanning right now, and the number of
7321 keymaps we have allocated space for. */
7322 int nmaps;
7324 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
7325 in the current keymaps, or nil where it is not a prefix. */
7326 Lisp_Object *maps;
7328 Lisp_Object def, tail;
7330 Lisp_Object result;
7332 int mapno;
7333 Lisp_Object oquit;
7335 /* In order to build the menus, we need to call the keymap
7336 accessors. They all call QUIT. But this function is called
7337 during redisplay, during which a quit is fatal. So inhibit
7338 quitting while building the menus.
7339 We do this instead of specbind because (1) errors will clear it anyway
7340 and (2) this avoids risk of specpdl overflow. */
7341 oquit = Vinhibit_quit;
7342 Vinhibit_quit = Qt;
7344 if (!NILP (old))
7345 menu_bar_items_vector = old;
7346 else
7347 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
7348 menu_bar_items_index = 0;
7350 /* Build our list of keymaps.
7351 If we recognize a function key and replace its escape sequence in
7352 keybuf with its symbol, or if the sequence starts with a mouse
7353 click and we need to switch buffers, we jump back here to rebuild
7354 the initial keymaps from the current buffer. */
7356 Lisp_Object *tmaps;
7358 /* Should overriding-terminal-local-map and overriding-local-map apply? */
7359 if (!NILP (Voverriding_local_map_menu_flag))
7361 /* Yes, use them (if non-nil) as well as the global map. */
7362 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
7363 nmaps = 0;
7364 if (!NILP (KVAR (current_kboard, Voverriding_terminal_local_map)))
7365 maps[nmaps++] = KVAR (current_kboard, Voverriding_terminal_local_map);
7366 if (!NILP (Voverriding_local_map))
7367 maps[nmaps++] = Voverriding_local_map;
7369 else
7371 /* No, so use major and minor mode keymaps and keymap property.
7372 Note that menu-bar bindings in the local-map and keymap
7373 properties may not work reliable, as they are only
7374 recognized when the menu-bar (or mode-line) is updated,
7375 which does not normally happen after every command. */
7376 Lisp_Object tem;
7377 int nminor;
7378 nminor = current_minor_maps (NULL, &tmaps);
7379 maps = (Lisp_Object *) alloca ((nminor + 3) * sizeof (maps[0]));
7380 nmaps = 0;
7381 if (tem = get_local_map (PT, current_buffer, Qkeymap), !NILP (tem))
7382 maps[nmaps++] = tem;
7383 memcpy (maps + nmaps, tmaps, nminor * sizeof (maps[0]));
7384 nmaps += nminor;
7385 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
7387 maps[nmaps++] = current_global_map;
7390 /* Look up in each map the dummy prefix key `menu-bar'. */
7392 result = Qnil;
7394 for (mapno = nmaps - 1; mapno >= 0; mapno--)
7395 if (!NILP (maps[mapno]))
7397 def = get_keymap (access_keymap (maps[mapno], Qmenu_bar, 1, 0, 1),
7398 0, 1);
7399 if (CONSP (def))
7401 menu_bar_one_keymap_changed_items = Qnil;
7402 map_keymap (def, menu_bar_item, Qnil, NULL, 1);
7406 /* Move to the end those items that should be at the end. */
7408 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCDR (tail))
7410 int i;
7411 int end = menu_bar_items_index;
7413 for (i = 0; i < end; i += 4)
7414 if (EQ (XCAR (tail), XVECTOR (menu_bar_items_vector)->contents[i]))
7416 Lisp_Object tem0, tem1, tem2, tem3;
7417 /* Move the item at index I to the end,
7418 shifting all the others forward. */
7419 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
7420 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
7421 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
7422 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
7423 if (end > i + 4)
7424 memmove (&XVECTOR (menu_bar_items_vector)->contents[i],
7425 &XVECTOR (menu_bar_items_vector)->contents[i + 4],
7426 (end - i - 4) * sizeof (Lisp_Object));
7427 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
7428 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
7429 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
7430 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
7431 break;
7435 /* Add nil, nil, nil, nil at the end. */
7437 int i = menu_bar_items_index;
7438 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
7439 menu_bar_items_vector =
7440 larger_vector (menu_bar_items_vector, 2 * i, Qnil);
7441 /* Add this item. */
7442 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
7443 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
7444 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
7445 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
7446 menu_bar_items_index = i;
7449 Vinhibit_quit = oquit;
7450 return menu_bar_items_vector;
7453 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
7454 If there's already an item for KEY, add this DEF to it. */
7456 Lisp_Object item_properties;
7458 static void
7459 menu_bar_item (Lisp_Object key, Lisp_Object item, Lisp_Object dummy1, void *dummy2)
7461 struct gcpro gcpro1;
7462 int i;
7463 Lisp_Object tem;
7465 if (EQ (item, Qundefined))
7467 /* If a map has an explicit `undefined' as definition,
7468 discard any previously made menu bar item. */
7470 for (i = 0; i < menu_bar_items_index; i += 4)
7471 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
7473 if (menu_bar_items_index > i + 4)
7474 memmove (&XVECTOR (menu_bar_items_vector)->contents[i],
7475 &XVECTOR (menu_bar_items_vector)->contents[i + 4],
7476 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
7477 menu_bar_items_index -= 4;
7481 /* If this keymap has already contributed to this KEY,
7482 don't contribute to it a second time. */
7483 tem = Fmemq (key, menu_bar_one_keymap_changed_items);
7484 if (!NILP (tem) || NILP (item))
7485 return;
7487 menu_bar_one_keymap_changed_items
7488 = Fcons (key, menu_bar_one_keymap_changed_items);
7490 /* We add to menu_bar_one_keymap_changed_items before doing the
7491 parse_menu_item, so that if it turns out it wasn't a menu item,
7492 it still correctly hides any further menu item. */
7493 GCPRO1 (key);
7494 i = parse_menu_item (item, 1);
7495 UNGCPRO;
7496 if (!i)
7497 return;
7499 item = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
7501 /* Find any existing item for this KEY. */
7502 for (i = 0; i < menu_bar_items_index; i += 4)
7503 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
7504 break;
7506 /* If we did not find this KEY, add it at the end. */
7507 if (i == menu_bar_items_index)
7509 /* If vector is too small, get a bigger one. */
7510 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
7511 menu_bar_items_vector = larger_vector (menu_bar_items_vector, 2 * i, Qnil);
7512 /* Add this item. */
7513 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
7514 XVECTOR (menu_bar_items_vector)->contents[i++]
7515 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
7516 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (item, Qnil);
7517 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
7518 menu_bar_items_index = i;
7520 /* We did find an item for this KEY. Add ITEM to its list of maps. */
7521 else
7523 Lisp_Object old;
7524 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
7525 /* If the new and the old items are not both keymaps,
7526 the lookup will only find `item'. */
7527 item = Fcons (item, KEYMAPP (item) && KEYMAPP (XCAR (old)) ? old : Qnil);
7528 XVECTOR (menu_bar_items_vector)->contents[i + 2] = item;
7532 /* This is used as the handler when calling menu_item_eval_property. */
7533 static Lisp_Object
7534 menu_item_eval_property_1 (Lisp_Object arg)
7536 /* If we got a quit from within the menu computation,
7537 quit all the way out of it. This takes care of C-] in the debugger. */
7538 if (CONSP (arg) && EQ (XCAR (arg), Qquit))
7539 Fsignal (Qquit, Qnil);
7541 return Qnil;
7544 static Lisp_Object
7545 eval_dyn (Lisp_Object form)
7547 return Feval (form, Qnil);
7550 /* Evaluate an expression and return the result (or nil if something
7551 went wrong). Used to evaluate dynamic parts of menu items. */
7552 Lisp_Object
7553 menu_item_eval_property (Lisp_Object sexpr)
7555 int count = SPECPDL_INDEX ();
7556 Lisp_Object val;
7557 specbind (Qinhibit_redisplay, Qt);
7558 val = internal_condition_case_1 (eval_dyn, sexpr, Qerror,
7559 menu_item_eval_property_1);
7560 return unbind_to (count, val);
7563 /* This function parses a menu item and leaves the result in the
7564 vector item_properties.
7565 ITEM is a key binding, a possible menu item.
7566 INMENUBAR is > 0 when this is considered for an entry in a menu bar
7567 top level.
7568 INMENUBAR is < 0 when this is considered for an entry in a keyboard menu.
7569 parse_menu_item returns true if the item is a menu item and false
7570 otherwise. */
7573 parse_menu_item (Lisp_Object item, int inmenubar)
7575 Lisp_Object def, tem, item_string, start;
7576 Lisp_Object filter;
7577 Lisp_Object keyhint;
7578 int i;
7580 filter = Qnil;
7581 keyhint = Qnil;
7583 if (!CONSP (item))
7584 return 0;
7586 /* Create item_properties vector if necessary. */
7587 if (NILP (item_properties))
7588 item_properties
7589 = Fmake_vector (make_number (ITEM_PROPERTY_ENABLE + 1), Qnil);
7591 /* Initialize optional entries. */
7592 for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++)
7593 ASET (item_properties, i, Qnil);
7594 ASET (item_properties, ITEM_PROPERTY_ENABLE, Qt);
7596 /* Save the item here to protect it from GC. */
7597 ASET (item_properties, ITEM_PROPERTY_ITEM, item);
7599 item_string = XCAR (item);
7601 start = item;
7602 item = XCDR (item);
7603 if (STRINGP (item_string))
7605 /* Old format menu item. */
7606 ASET (item_properties, ITEM_PROPERTY_NAME, item_string);
7608 /* Maybe help string. */
7609 if (CONSP (item) && STRINGP (XCAR (item)))
7611 ASET (item_properties, ITEM_PROPERTY_HELP, XCAR (item));
7612 start = item;
7613 item = XCDR (item);
7616 /* Maybe an obsolete key binding cache. */
7617 if (CONSP (item) && CONSP (XCAR (item))
7618 && (NILP (XCAR (XCAR (item)))
7619 || VECTORP (XCAR (XCAR (item)))))
7620 item = XCDR (item);
7622 /* This is the real definition--the function to run. */
7623 ASET (item_properties, ITEM_PROPERTY_DEF, item);
7625 /* Get enable property, if any. */
7626 if (SYMBOLP (item))
7628 tem = Fget (item, Qmenu_enable);
7629 if (!NILP (Venable_disabled_menus_and_buttons))
7630 ASET (item_properties, ITEM_PROPERTY_ENABLE, Qt);
7631 else if (!NILP (tem))
7632 ASET (item_properties, ITEM_PROPERTY_ENABLE, tem);
7635 else if (EQ (item_string, Qmenu_item) && CONSP (item))
7637 /* New format menu item. */
7638 ASET (item_properties, ITEM_PROPERTY_NAME, XCAR (item));
7639 start = XCDR (item);
7640 if (CONSP (start))
7642 /* We have a real binding. */
7643 ASET (item_properties, ITEM_PROPERTY_DEF, XCAR (start));
7645 item = XCDR (start);
7646 /* Is there an obsolete cache list with key equivalences. */
7647 if (CONSP (item) && CONSP (XCAR (item)))
7648 item = XCDR (item);
7650 /* Parse properties. */
7651 while (CONSP (item) && CONSP (XCDR (item)))
7653 tem = XCAR (item);
7654 item = XCDR (item);
7656 if (EQ (tem, QCenable))
7658 if (!NILP (Venable_disabled_menus_and_buttons))
7659 ASET (item_properties, ITEM_PROPERTY_ENABLE, Qt);
7660 else
7661 ASET (item_properties, ITEM_PROPERTY_ENABLE, XCAR (item));
7663 else if (EQ (tem, QCvisible))
7665 /* If got a visible property and that evaluates to nil
7666 then ignore this item. */
7667 tem = menu_item_eval_property (XCAR (item));
7668 if (NILP (tem))
7669 return 0;
7671 else if (EQ (tem, QChelp))
7672 ASET (item_properties, ITEM_PROPERTY_HELP, XCAR (item));
7673 else if (EQ (tem, QCfilter))
7674 filter = item;
7675 else if (EQ (tem, QCkey_sequence))
7677 tem = XCAR (item);
7678 if (SYMBOLP (tem) || STRINGP (tem) || VECTORP (tem))
7679 /* Be GC protected. Set keyhint to item instead of tem. */
7680 keyhint = item;
7682 else if (EQ (tem, QCkeys))
7684 tem = XCAR (item);
7685 if (CONSP (tem) || STRINGP (tem))
7686 ASET (item_properties, ITEM_PROPERTY_KEYEQ, tem);
7688 else if (EQ (tem, QCbutton) && CONSP (XCAR (item)))
7690 Lisp_Object type;
7691 tem = XCAR (item);
7692 type = XCAR (tem);
7693 if (EQ (type, QCtoggle) || EQ (type, QCradio))
7695 ASET (item_properties, ITEM_PROPERTY_SELECTED,
7696 XCDR (tem));
7697 ASET (item_properties, ITEM_PROPERTY_TYPE, type);
7700 item = XCDR (item);
7703 else if (inmenubar || !NILP (start))
7704 return 0;
7706 else
7707 return 0; /* not a menu item */
7709 /* If item string is not a string, evaluate it to get string.
7710 If we don't get a string, skip this item. */
7711 item_string = AREF (item_properties, ITEM_PROPERTY_NAME);
7712 if (!(STRINGP (item_string)))
7714 item_string = menu_item_eval_property (item_string);
7715 if (!STRINGP (item_string))
7716 return 0;
7717 ASET (item_properties, ITEM_PROPERTY_NAME, item_string);
7720 /* If got a filter apply it on definition. */
7721 def = AREF (item_properties, ITEM_PROPERTY_DEF);
7722 if (!NILP (filter))
7724 def = menu_item_eval_property (list2 (XCAR (filter),
7725 list2 (Qquote, def)));
7727 ASET (item_properties, ITEM_PROPERTY_DEF, def);
7730 /* Enable or disable selection of item. */
7731 tem = AREF (item_properties, ITEM_PROPERTY_ENABLE);
7732 if (!EQ (tem, Qt))
7734 tem = menu_item_eval_property (tem);
7735 if (inmenubar && NILP (tem))
7736 return 0; /* Ignore disabled items in menu bar. */
7737 ASET (item_properties, ITEM_PROPERTY_ENABLE, tem);
7740 /* If we got no definition, this item is just unselectable text which
7741 is OK in a submenu but not in the menubar. */
7742 if (NILP (def))
7743 return (inmenubar ? 0 : 1);
7745 /* See if this is a separate pane or a submenu. */
7746 def = AREF (item_properties, ITEM_PROPERTY_DEF);
7747 tem = get_keymap (def, 0, 1);
7748 /* For a subkeymap, just record its details and exit. */
7749 if (CONSP (tem))
7751 ASET (item_properties, ITEM_PROPERTY_MAP, tem);
7752 ASET (item_properties, ITEM_PROPERTY_DEF, tem);
7753 return 1;
7756 /* At the top level in the menu bar, do likewise for commands also.
7757 The menu bar does not display equivalent key bindings anyway.
7758 ITEM_PROPERTY_DEF is already set up properly. */
7759 if (inmenubar > 0)
7760 return 1;
7762 { /* This is a command. See if there is an equivalent key binding. */
7763 Lisp_Object keyeq = AREF (item_properties, ITEM_PROPERTY_KEYEQ);
7765 /* The previous code preferred :key-sequence to :keys, so we
7766 preserve this behavior. */
7767 if (STRINGP (keyeq) && !CONSP (keyhint))
7768 keyeq = concat2 (build_string (" "), Fsubstitute_command_keys (keyeq));
7769 else
7771 Lisp_Object prefix = keyeq;
7772 Lisp_Object keys = Qnil;
7774 if (CONSP (prefix))
7776 def = XCAR (prefix);
7777 prefix = XCDR (prefix);
7779 else
7780 def = AREF (item_properties, ITEM_PROPERTY_DEF);
7782 if (CONSP (keyhint) && !NILP (XCAR (keyhint)))
7784 keys = XCAR (keyhint);
7785 tem = Fkey_binding (keys, Qnil, Qnil, Qnil);
7787 /* We have a suggested key. Is it bound to the command? */
7788 if (NILP (tem)
7789 || (!EQ (tem, def)
7790 /* If the command is an alias for another
7791 (such as lmenu.el set it up), check if the
7792 original command matches the cached command. */
7793 && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function))))
7794 keys = Qnil;
7797 if (NILP (keys))
7798 keys = Fwhere_is_internal (def, Qnil, Qt, Qnil, Qnil);
7800 if (!NILP (keys))
7802 tem = Fkey_description (keys, Qnil);
7803 if (CONSP (prefix))
7805 if (STRINGP (XCAR (prefix)))
7806 tem = concat2 (XCAR (prefix), tem);
7807 if (STRINGP (XCDR (prefix)))
7808 tem = concat2 (tem, XCDR (prefix));
7810 keyeq = concat2 (build_string (" "), tem);
7811 /* keyeq = concat3(build_string(" ("),tem,build_string(")")); */
7813 else
7814 keyeq = Qnil;
7817 /* If we have an equivalent key binding, use that. */
7818 ASET (item_properties, ITEM_PROPERTY_KEYEQ, keyeq);
7821 /* Include this when menu help is implemented.
7822 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP];
7823 if (!(NILP (tem) || STRINGP (tem)))
7825 tem = menu_item_eval_property (tem);
7826 if (!STRINGP (tem))
7827 tem = Qnil;
7828 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP] = tem;
7832 /* Handle radio buttons or toggle boxes. */
7833 tem = AREF (item_properties, ITEM_PROPERTY_SELECTED);
7834 if (!NILP (tem))
7835 ASET (item_properties, ITEM_PROPERTY_SELECTED,
7836 menu_item_eval_property (tem));
7838 return 1;
7843 /***********************************************************************
7844 Tool-bars
7845 ***********************************************************************/
7847 /* A vector holding tool bar items while they are parsed in function
7848 tool_bar_items. Each item occupies TOOL_BAR_ITEM_NSCLOTS elements
7849 in the vector. */
7851 static Lisp_Object tool_bar_items_vector;
7853 /* A vector holding the result of parse_tool_bar_item. Layout is like
7854 the one for a single item in tool_bar_items_vector. */
7856 static Lisp_Object tool_bar_item_properties;
7858 /* Next free index in tool_bar_items_vector. */
7860 static int ntool_bar_items;
7862 /* The symbols `:image' and `:rtl'. */
7864 Lisp_Object QCimage;
7865 Lisp_Object Qrtl;
7867 /* Function prototypes. */
7869 static void init_tool_bar_items (Lisp_Object);
7870 static void process_tool_bar_item (Lisp_Object, Lisp_Object, Lisp_Object, void*);
7871 static int parse_tool_bar_item (Lisp_Object, Lisp_Object);
7872 static void append_tool_bar_item (void);
7875 /* Return a vector of tool bar items for keymaps currently in effect.
7876 Reuse vector REUSE if non-nil. Return in *NITEMS the number of
7877 tool bar items found. */
7879 Lisp_Object
7880 tool_bar_items (Lisp_Object reuse, int *nitems)
7882 Lisp_Object *maps;
7883 int nmaps, i;
7884 Lisp_Object oquit;
7885 Lisp_Object *tmaps;
7887 *nitems = 0;
7889 /* In order to build the menus, we need to call the keymap
7890 accessors. They all call QUIT. But this function is called
7891 during redisplay, during which a quit is fatal. So inhibit
7892 quitting while building the menus. We do this instead of
7893 specbind because (1) errors will clear it anyway and (2) this
7894 avoids risk of specpdl overflow. */
7895 oquit = Vinhibit_quit;
7896 Vinhibit_quit = Qt;
7898 /* Initialize tool_bar_items_vector and protect it from GC. */
7899 init_tool_bar_items (reuse);
7901 /* Build list of keymaps in maps. Set nmaps to the number of maps
7902 to process. */
7904 /* Should overriding-terminal-local-map and overriding-local-map apply? */
7905 if (!NILP (Voverriding_local_map_menu_flag))
7907 /* Yes, use them (if non-nil) as well as the global map. */
7908 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
7909 nmaps = 0;
7910 if (!NILP (KVAR (current_kboard, Voverriding_terminal_local_map)))
7911 maps[nmaps++] = KVAR (current_kboard, Voverriding_terminal_local_map);
7912 if (!NILP (Voverriding_local_map))
7913 maps[nmaps++] = Voverriding_local_map;
7915 else
7917 /* No, so use major and minor mode keymaps and keymap property.
7918 Note that tool-bar bindings in the local-map and keymap
7919 properties may not work reliable, as they are only
7920 recognized when the tool-bar (or mode-line) is updated,
7921 which does not normally happen after every command. */
7922 Lisp_Object tem;
7923 int nminor;
7924 nminor = current_minor_maps (NULL, &tmaps);
7925 maps = (Lisp_Object *) alloca ((nminor + 3) * sizeof (maps[0]));
7926 nmaps = 0;
7927 if (tem = get_local_map (PT, current_buffer, Qkeymap), !NILP (tem))
7928 maps[nmaps++] = tem;
7929 memcpy (maps + nmaps, tmaps, nminor * sizeof (maps[0]));
7930 nmaps += nminor;
7931 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
7934 /* Add global keymap at the end. */
7935 maps[nmaps++] = current_global_map;
7937 /* Process maps in reverse order and look up in each map the prefix
7938 key `tool-bar'. */
7939 for (i = nmaps - 1; i >= 0; --i)
7940 if (!NILP (maps[i]))
7942 Lisp_Object keymap;
7944 keymap = get_keymap (access_keymap (maps[i], Qtool_bar, 1, 0, 1), 0, 1);
7945 if (CONSP (keymap))
7946 map_keymap (keymap, process_tool_bar_item, Qnil, NULL, 1);
7949 Vinhibit_quit = oquit;
7950 *nitems = ntool_bar_items / TOOL_BAR_ITEM_NSLOTS;
7951 return tool_bar_items_vector;
7955 /* Process the definition of KEY which is DEF. */
7957 static void
7958 process_tool_bar_item (Lisp_Object key, Lisp_Object def, Lisp_Object data, void *args)
7960 int i;
7961 struct gcpro gcpro1, gcpro2;
7963 /* Protect KEY and DEF from GC because parse_tool_bar_item may call
7964 eval. */
7965 GCPRO2 (key, def);
7967 if (EQ (def, Qundefined))
7969 /* If a map has an explicit `undefined' as definition,
7970 discard any previously made item. */
7971 for (i = 0; i < ntool_bar_items; i += TOOL_BAR_ITEM_NSLOTS)
7973 Lisp_Object *v = XVECTOR (tool_bar_items_vector)->contents + i;
7975 if (EQ (key, v[TOOL_BAR_ITEM_KEY]))
7977 if (ntool_bar_items > i + TOOL_BAR_ITEM_NSLOTS)
7978 memmove (v, v + TOOL_BAR_ITEM_NSLOTS,
7979 ((ntool_bar_items - i - TOOL_BAR_ITEM_NSLOTS)
7980 * sizeof (Lisp_Object)));
7981 ntool_bar_items -= TOOL_BAR_ITEM_NSLOTS;
7982 break;
7986 else if (parse_tool_bar_item (key, def))
7987 /* Append a new tool bar item to tool_bar_items_vector. Accept
7988 more than one definition for the same key. */
7989 append_tool_bar_item ();
7991 UNGCPRO;
7995 /* Parse a tool bar item specification ITEM for key KEY and return the
7996 result in tool_bar_item_properties. Value is zero if ITEM is
7997 invalid.
7999 ITEM is a list `(menu-item CAPTION BINDING PROPS...)'.
8001 CAPTION is the caption of the item, If it's not a string, it is
8002 evaluated to get a string.
8004 BINDING is the tool bar item's binding. Tool-bar items with keymaps
8005 as binding are currently ignored.
8007 The following properties are recognized:
8009 - `:enable FORM'.
8011 FORM is evaluated and specifies whether the tool bar item is
8012 enabled or disabled.
8014 - `:visible FORM'
8016 FORM is evaluated and specifies whether the tool bar item is visible.
8018 - `:filter FUNCTION'
8020 FUNCTION is invoked with one parameter `(quote BINDING)'. Its
8021 result is stored as the new binding.
8023 - `:button (TYPE SELECTED)'
8025 TYPE must be one of `:radio' or `:toggle'. SELECTED is evaluated
8026 and specifies whether the button is selected (pressed) or not.
8028 - `:image IMAGES'
8030 IMAGES is either a single image specification or a vector of four
8031 image specifications. See enum tool_bar_item_images.
8033 - `:help HELP-STRING'.
8035 Gives a help string to display for the tool bar item.
8037 - `:label LABEL-STRING'.
8039 A text label to show with the tool bar button if labels are enabled. */
8041 static int
8042 parse_tool_bar_item (Lisp_Object key, Lisp_Object item)
8044 /* Access slot with index IDX of vector tool_bar_item_properties. */
8045 #define PROP(IDX) XVECTOR (tool_bar_item_properties)->contents[IDX]
8047 Lisp_Object filter = Qnil;
8048 Lisp_Object caption;
8049 int i, have_label = 0;
8051 /* Defininition looks like `(menu-item CAPTION BINDING PROPS...)'.
8052 Rule out items that aren't lists, don't start with
8053 `menu-item' or whose rest following `tool-bar-item' is not a
8054 list. */
8055 if (!CONSP (item))
8056 return 0;
8058 /* As an exception, allow old-style menu separators. */
8059 if (STRINGP (XCAR (item)))
8060 item = Fcons (XCAR (item), Qnil);
8061 else if (!EQ (XCAR (item), Qmenu_item)
8062 || (item = XCDR (item), !CONSP (item)))
8063 return 0;
8065 /* Create tool_bar_item_properties vector if necessary. Reset it to
8066 defaults. */
8067 if (VECTORP (tool_bar_item_properties))
8069 for (i = 0; i < TOOL_BAR_ITEM_NSLOTS; ++i)
8070 PROP (i) = Qnil;
8072 else
8073 tool_bar_item_properties
8074 = Fmake_vector (make_number (TOOL_BAR_ITEM_NSLOTS), Qnil);
8076 /* Set defaults. */
8077 PROP (TOOL_BAR_ITEM_KEY) = key;
8078 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
8080 /* Get the caption of the item. If the caption is not a string,
8081 evaluate it to get a string. If we don't get a string, skip this
8082 item. */
8083 caption = XCAR (item);
8084 if (!STRINGP (caption))
8086 caption = menu_item_eval_property (caption);
8087 if (!STRINGP (caption))
8088 return 0;
8090 PROP (TOOL_BAR_ITEM_CAPTION) = caption;
8092 /* If the rest following the caption is not a list, the menu item is
8093 either a separator, or invalid. */
8094 item = XCDR (item);
8095 if (!CONSP (item))
8097 if (menu_separator_name_p (SSDATA (caption)))
8099 PROP (TOOL_BAR_ITEM_TYPE) = Qt;
8100 #if !defined (USE_GTK) && !defined (HAVE_NS)
8101 /* If we use build_desired_tool_bar_string to render the
8102 tool bar, the separator is rendered as an image. */
8103 PROP (TOOL_BAR_ITEM_IMAGES)
8104 = menu_item_eval_property (Vtool_bar_separator_image_expression);
8105 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qnil;
8106 PROP (TOOL_BAR_ITEM_SELECTED_P) = Qnil;
8107 PROP (TOOL_BAR_ITEM_CAPTION) = Qnil;
8108 #endif
8109 return 1;
8111 return 0;
8114 /* Store the binding. */
8115 PROP (TOOL_BAR_ITEM_BINDING) = XCAR (item);
8116 item = XCDR (item);
8118 /* Ignore cached key binding, if any. */
8119 if (CONSP (item) && CONSP (XCAR (item)))
8120 item = XCDR (item);
8122 /* Process the rest of the properties. */
8123 for (; CONSP (item) && CONSP (XCDR (item)); item = XCDR (XCDR (item)))
8125 Lisp_Object ikey, value;
8127 ikey = XCAR (item);
8128 value = XCAR (XCDR (item));
8130 if (EQ (ikey, QCenable))
8132 /* `:enable FORM'. */
8133 if (!NILP (Venable_disabled_menus_and_buttons))
8134 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
8135 else
8136 PROP (TOOL_BAR_ITEM_ENABLED_P) = value;
8138 else if (EQ (ikey, QCvisible))
8140 /* `:visible FORM'. If got a visible property and that
8141 evaluates to nil then ignore this item. */
8142 if (NILP (menu_item_eval_property (value)))
8143 return 0;
8145 else if (EQ (ikey, QChelp))
8146 /* `:help HELP-STRING'. */
8147 PROP (TOOL_BAR_ITEM_HELP) = value;
8148 else if (EQ (ikey, QCvert_only))
8149 /* `:vert-only t/nil'. */
8150 PROP (TOOL_BAR_ITEM_VERT_ONLY) = value;
8151 else if (EQ (ikey, QClabel))
8153 const char *bad_label = "!!?GARBLED ITEM?!!";
8154 /* `:label LABEL-STRING'. */
8155 PROP (TOOL_BAR_ITEM_LABEL) = STRINGP (value)
8156 ? value
8157 : make_string (bad_label, strlen (bad_label));
8158 have_label = 1;
8160 else if (EQ (ikey, QCfilter))
8161 /* ':filter FORM'. */
8162 filter = value;
8163 else if (EQ (ikey, QCbutton) && CONSP (value))
8165 /* `:button (TYPE . SELECTED)'. */
8166 Lisp_Object type, selected;
8168 type = XCAR (value);
8169 selected = XCDR (value);
8170 if (EQ (type, QCtoggle) || EQ (type, QCradio))
8172 PROP (TOOL_BAR_ITEM_SELECTED_P) = selected;
8173 PROP (TOOL_BAR_ITEM_TYPE) = type;
8176 else if (EQ (ikey, QCimage)
8177 && (CONSP (value)
8178 || (VECTORP (value) && XVECTOR (value)->size == 4)))
8179 /* Value is either a single image specification or a vector
8180 of 4 such specifications for the different button states. */
8181 PROP (TOOL_BAR_ITEM_IMAGES) = value;
8182 else if (EQ (ikey, Qrtl))
8183 /* ':rtl STRING' */
8184 PROP (TOOL_BAR_ITEM_RTL_IMAGE) = value;
8188 if (!have_label)
8190 /* Try to make one from caption and key. */
8191 Lisp_Object tkey = PROP (TOOL_BAR_ITEM_KEY);
8192 Lisp_Object tcapt = PROP (TOOL_BAR_ITEM_CAPTION);
8193 const char *label = SYMBOLP (tkey) ? SSDATA (SYMBOL_NAME (tkey)) : "";
8194 const char *capt = STRINGP (tcapt) ? SSDATA (tcapt) : "";
8195 EMACS_INT max_lbl = 2 * tool_bar_max_label_size;
8196 char *buf = (char *) xmalloc (max_lbl + 1);
8197 Lisp_Object new_lbl;
8198 size_t caption_len = strlen (capt);
8200 if (caption_len <= max_lbl && capt[0] != '\0')
8202 strcpy (buf, capt);
8203 while (caption_len > 0 && buf[caption_len - 1] == '.')
8204 caption_len--;
8205 buf[caption_len] = '\0';
8206 label = capt = buf;
8209 if (strlen (label) <= max_lbl && label[0] != '\0')
8211 int j;
8212 if (label != buf)
8213 strcpy (buf, label);
8215 for (j = 0; buf[j] != '\0'; ++j)
8216 if (buf[j] == '-')
8217 buf[j] = ' ';
8218 label = buf;
8220 else
8221 label = "";
8223 new_lbl = Fupcase_initials (make_string (label, strlen (label)));
8224 if (SCHARS (new_lbl) <= tool_bar_max_label_size)
8225 PROP (TOOL_BAR_ITEM_LABEL) = new_lbl;
8226 else
8227 PROP (TOOL_BAR_ITEM_LABEL) = make_string ("", 0);
8228 free (buf);
8231 /* If got a filter apply it on binding. */
8232 if (!NILP (filter))
8233 PROP (TOOL_BAR_ITEM_BINDING)
8234 = menu_item_eval_property (list2 (filter,
8235 list2 (Qquote,
8236 PROP (TOOL_BAR_ITEM_BINDING))));
8238 /* See if the binding is a keymap. Give up if it is. */
8239 if (CONSP (get_keymap (PROP (TOOL_BAR_ITEM_BINDING), 0, 1)))
8240 return 0;
8242 /* Enable or disable selection of item. */
8243 if (!EQ (PROP (TOOL_BAR_ITEM_ENABLED_P), Qt))
8244 PROP (TOOL_BAR_ITEM_ENABLED_P)
8245 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_ENABLED_P));
8247 /* Handle radio buttons or toggle boxes. */
8248 if (!NILP (PROP (TOOL_BAR_ITEM_SELECTED_P)))
8249 PROP (TOOL_BAR_ITEM_SELECTED_P)
8250 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_SELECTED_P));
8252 return 1;
8254 #undef PROP
8258 /* Initialize tool_bar_items_vector. REUSE, if non-nil, is a vector
8259 that can be reused. */
8261 static void
8262 init_tool_bar_items (Lisp_Object reuse)
8264 if (VECTORP (reuse))
8265 tool_bar_items_vector = reuse;
8266 else
8267 tool_bar_items_vector = Fmake_vector (make_number (64), Qnil);
8268 ntool_bar_items = 0;
8272 /* Append parsed tool bar item properties from
8273 tool_bar_item_properties */
8275 static void
8276 append_tool_bar_item (void)
8278 Lisp_Object *to, *from;
8280 /* Enlarge tool_bar_items_vector if necessary. */
8281 if (ntool_bar_items + TOOL_BAR_ITEM_NSLOTS
8282 >= XVECTOR (tool_bar_items_vector)->size)
8283 tool_bar_items_vector
8284 = larger_vector (tool_bar_items_vector,
8285 2 * XVECTOR (tool_bar_items_vector)->size, Qnil);
8287 /* Append entries from tool_bar_item_properties to the end of
8288 tool_bar_items_vector. */
8289 to = XVECTOR (tool_bar_items_vector)->contents + ntool_bar_items;
8290 from = XVECTOR (tool_bar_item_properties)->contents;
8291 memcpy (to, from, TOOL_BAR_ITEM_NSLOTS * sizeof *to);
8292 ntool_bar_items += TOOL_BAR_ITEM_NSLOTS;
8299 /* Read a character using menus based on maps in the array MAPS.
8300 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
8301 Return t if we displayed a menu but the user rejected it.
8303 PREV_EVENT is the previous input event, or nil if we are reading
8304 the first event of a key sequence.
8306 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
8307 if we used a mouse menu to read the input, or zero otherwise. If
8308 USED_MOUSE_MENU is null, we don't dereference it.
8310 The prompting is done based on the prompt-string of the map
8311 and the strings associated with various map elements.
8313 This can be done with X menus or with menus put in the minibuf.
8314 These are done in different ways, depending on how the input will be read.
8315 Menus using X are done after auto-saving in read-char, getting the input
8316 event from Fx_popup_menu; menus using the minibuf use read_char recursively
8317 and do auto-saving in the inner call of read_char. */
8319 static Lisp_Object
8320 read_char_x_menu_prompt (int nmaps, Lisp_Object *maps, Lisp_Object prev_event,
8321 int *used_mouse_menu)
8323 int mapno;
8325 if (used_mouse_menu)
8326 *used_mouse_menu = 0;
8328 /* Use local over global Menu maps */
8330 if (! menu_prompting)
8331 return Qnil;
8333 /* Optionally disregard all but the global map. */
8334 if (inhibit_local_menu_bar_menus)
8336 maps += (nmaps - 1);
8337 nmaps = 1;
8340 #ifdef HAVE_MENUS
8341 /* If we got to this point via a mouse click,
8342 use a real menu for mouse selection. */
8343 if (EVENT_HAS_PARAMETERS (prev_event)
8344 && !EQ (XCAR (prev_event), Qmenu_bar)
8345 && !EQ (XCAR (prev_event), Qtool_bar))
8347 /* Display the menu and get the selection. */
8348 Lisp_Object *realmaps
8349 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
8350 Lisp_Object value;
8351 int nmaps1 = 0;
8353 /* Use the maps that are not nil. */
8354 for (mapno = 0; mapno < nmaps; mapno++)
8355 if (!NILP (maps[mapno]))
8356 realmaps[nmaps1++] = maps[mapno];
8358 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
8359 if (CONSP (value))
8361 Lisp_Object tem;
8363 record_menu_key (XCAR (value));
8365 /* If we got multiple events, unread all but
8366 the first.
8367 There is no way to prevent those unread events
8368 from showing up later in last_nonmenu_event.
8369 So turn symbol and integer events into lists,
8370 to indicate that they came from a mouse menu,
8371 so that when present in last_nonmenu_event
8372 they won't confuse things. */
8373 for (tem = XCDR (value); CONSP (tem); tem = XCDR (tem))
8375 record_menu_key (XCAR (tem));
8376 if (SYMBOLP (XCAR (tem))
8377 || INTEGERP (XCAR (tem)))
8378 XSETCAR (tem, Fcons (XCAR (tem), Qdisabled));
8381 /* If we got more than one event, put all but the first
8382 onto this list to be read later.
8383 Return just the first event now. */
8384 Vunread_command_events
8385 = nconc2 (XCDR (value), Vunread_command_events);
8386 value = XCAR (value);
8388 else if (NILP (value))
8389 value = Qt;
8390 if (used_mouse_menu)
8391 *used_mouse_menu = 1;
8392 return value;
8394 #endif /* HAVE_MENUS */
8395 return Qnil ;
8398 /* Buffer in use so far for the minibuf prompts for menu keymaps.
8399 We make this bigger when necessary, and never free it. */
8400 static char *read_char_minibuf_menu_text;
8401 /* Size of that buffer. */
8402 static int read_char_minibuf_menu_width;
8404 static Lisp_Object
8405 read_char_minibuf_menu_prompt (int commandflag, int nmaps, Lisp_Object *maps)
8407 int mapno;
8408 register Lisp_Object name;
8409 int nlength;
8410 /* FIXME: Use the minibuffer's frame width. */
8411 int width = FRAME_COLS (SELECTED_FRAME ()) - 4;
8412 int idx = -1;
8413 int nobindings = 1;
8414 Lisp_Object rest, vector;
8415 char *menu;
8417 vector = Qnil;
8418 name = Qnil;
8420 if (! menu_prompting)
8421 return Qnil;
8423 /* Get the menu name from the first map that has one (a prompt string). */
8424 for (mapno = 0; mapno < nmaps; mapno++)
8426 name = Fkeymap_prompt (maps[mapno]);
8427 if (!NILP (name))
8428 break;
8431 /* If we don't have any menus, just read a character normally. */
8432 if (!STRINGP (name))
8433 return Qnil;
8435 /* Make sure we have a big enough buffer for the menu text. */
8436 width = max (width, SBYTES (name));
8437 if (read_char_minibuf_menu_text == 0)
8439 read_char_minibuf_menu_width = width + 4;
8440 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
8442 else if (width + 4 > read_char_minibuf_menu_width)
8444 read_char_minibuf_menu_width = width + 4;
8445 read_char_minibuf_menu_text
8446 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
8448 menu = read_char_minibuf_menu_text;
8450 /* Prompt string always starts with map's prompt, and a space. */
8451 strcpy (menu, SSDATA (name));
8452 nlength = SBYTES (name);
8453 menu[nlength++] = ':';
8454 menu[nlength++] = ' ';
8455 menu[nlength] = 0;
8457 /* Start prompting at start of first map. */
8458 mapno = 0;
8459 rest = maps[mapno];
8461 /* Present the documented bindings, a line at a time. */
8462 while (1)
8464 int notfirst = 0;
8465 int i = nlength;
8466 Lisp_Object obj;
8467 int ch;
8468 Lisp_Object orig_defn_macro;
8470 /* Loop over elements of map. */
8471 while (i < width)
8473 Lisp_Object elt;
8475 /* If reached end of map, start at beginning of next map. */
8476 if (NILP (rest))
8478 mapno++;
8479 /* At end of last map, wrap around to first map if just starting,
8480 or end this line if already have something on it. */
8481 if (mapno == nmaps)
8483 mapno = 0;
8484 if (notfirst || nobindings) break;
8486 rest = maps[mapno];
8489 /* Look at the next element of the map. */
8490 if (idx >= 0)
8491 elt = XVECTOR (vector)->contents[idx];
8492 else
8493 elt = Fcar_safe (rest);
8495 if (idx < 0 && VECTORP (elt))
8497 /* If we found a dense table in the keymap,
8498 advanced past it, but start scanning its contents. */
8499 rest = Fcdr_safe (rest);
8500 vector = elt;
8501 idx = 0;
8503 else
8505 /* An ordinary element. */
8506 Lisp_Object event, tem;
8508 if (idx < 0)
8510 event = Fcar_safe (elt); /* alist */
8511 elt = Fcdr_safe (elt);
8513 else
8515 XSETINT (event, idx); /* vector */
8518 /* Ignore the element if it has no prompt string. */
8519 if (INTEGERP (event) && parse_menu_item (elt, -1))
8521 /* 1 if the char to type matches the string. */
8522 int char_matches;
8523 Lisp_Object upcased_event, downcased_event;
8524 Lisp_Object desc = Qnil;
8525 Lisp_Object s
8526 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
8528 upcased_event = Fupcase (event);
8529 downcased_event = Fdowncase (event);
8530 char_matches = (XINT (upcased_event) == SREF (s, 0)
8531 || XINT (downcased_event) == SREF (s, 0));
8532 if (! char_matches)
8533 desc = Fsingle_key_description (event, Qnil);
8535 #if 0 /* It is redundant to list the equivalent key bindings because
8536 the prefix is what the user has already typed. */
8538 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
8539 if (!NILP (tem))
8540 /* Insert equivalent keybinding. */
8541 s = concat2 (s, tem);
8542 #endif
8544 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
8545 if (EQ (tem, QCradio) || EQ (tem, QCtoggle))
8547 /* Insert button prefix. */
8548 Lisp_Object selected
8549 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
8550 if (EQ (tem, QCradio))
8551 tem = build_string (NILP (selected) ? "(*) " : "( ) ");
8552 else
8553 tem = build_string (NILP (selected) ? "[X] " : "[ ] ");
8554 s = concat2 (tem, s);
8558 /* If we have room for the prompt string, add it to this line.
8559 If this is the first on the line, always add it. */
8560 if ((SCHARS (s) + i + 2
8561 + (char_matches ? 0 : SCHARS (desc) + 3))
8562 < width
8563 || !notfirst)
8565 int thiswidth;
8567 /* Punctuate between strings. */
8568 if (notfirst)
8570 strcpy (menu + i, ", ");
8571 i += 2;
8573 notfirst = 1;
8574 nobindings = 0 ;
8576 /* If the char to type doesn't match the string's
8577 first char, explicitly show what char to type. */
8578 if (! char_matches)
8580 /* Add as much of string as fits. */
8581 thiswidth = SCHARS (desc);
8582 if (thiswidth + i > width)
8583 thiswidth = width - i;
8584 memcpy (menu + i, SDATA (desc), thiswidth);
8585 i += thiswidth;
8586 strcpy (menu + i, " = ");
8587 i += 3;
8590 /* Add as much of string as fits. */
8591 thiswidth = SCHARS (s);
8592 if (thiswidth + i > width)
8593 thiswidth = width - i;
8594 memcpy (menu + i, SDATA (s), thiswidth);
8595 i += thiswidth;
8596 menu[i] = 0;
8598 else
8600 /* If this element does not fit, end the line now,
8601 and save the element for the next line. */
8602 strcpy (menu + i, "...");
8603 break;
8607 /* Move past this element. */
8608 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
8609 /* Handle reaching end of dense table. */
8610 idx = -1;
8611 if (idx >= 0)
8612 idx++;
8613 else
8614 rest = Fcdr_safe (rest);
8618 /* Prompt with that and read response. */
8619 message2_nolog (menu, strlen (menu),
8620 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
8622 /* Make believe its not a keyboard macro in case the help char
8623 is pressed. Help characters are not recorded because menu prompting
8624 is not used on replay.
8626 orig_defn_macro = KVAR (current_kboard, defining_kbd_macro);
8627 KVAR (current_kboard, defining_kbd_macro) = Qnil;
8629 obj = read_char (commandflag, 0, 0, Qt, 0, NULL);
8630 while (BUFFERP (obj));
8631 KVAR (current_kboard, defining_kbd_macro) = orig_defn_macro;
8633 if (!INTEGERP (obj))
8634 return obj;
8635 else if (XINT (obj) == -2)
8636 return obj;
8637 else
8638 ch = XINT (obj);
8640 if (! EQ (obj, menu_prompt_more_char)
8641 && (!INTEGERP (menu_prompt_more_char)
8642 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
8644 if (!NILP (KVAR (current_kboard, defining_kbd_macro)))
8645 store_kbd_macro_char (obj);
8646 return obj;
8648 /* Help char - go round again */
8652 /* Reading key sequences. */
8654 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
8655 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
8656 keymap, or nil otherwise. Return the index of the first keymap in
8657 which KEY has any binding, or NMAPS if no map has a binding.
8659 If KEY is a meta ASCII character, treat it like meta-prefix-char
8660 followed by the corresponding non-meta character. Keymaps in
8661 CURRENT with non-prefix bindings for meta-prefix-char become nil in
8662 NEXT.
8664 If KEY has no bindings in any of the CURRENT maps, NEXT is left
8665 unmodified.
8667 NEXT may be the same array as CURRENT. */
8669 static int
8670 follow_key (Lisp_Object key, int nmaps, Lisp_Object *current, Lisp_Object *defs,
8671 Lisp_Object *next)
8673 int i, first_binding;
8675 first_binding = nmaps;
8676 for (i = nmaps - 1; i >= 0; i--)
8678 if (! NILP (current[i]))
8680 defs[i] = access_keymap (current[i], key, 1, 0, 1);
8681 if (! NILP (defs[i]))
8682 first_binding = i;
8684 else
8685 defs[i] = Qnil;
8688 /* Given the set of bindings we've found, produce the next set of maps. */
8689 if (first_binding < nmaps)
8690 for (i = 0; i < nmaps; i++)
8691 next[i] = NILP (defs[i]) ? Qnil : get_keymap (defs[i], 0, 1);
8693 return first_binding;
8696 /* Structure used to keep track of partial application of key remapping
8697 such as Vfunction_key_map and Vkey_translation_map. */
8698 typedef struct keyremap
8700 /* This is the map originally specified for this use. */
8701 Lisp_Object parent;
8702 /* This is a submap reached by looking up, in PARENT,
8703 the events from START to END. */
8704 Lisp_Object map;
8705 /* Positions [START, END) in the key sequence buffer
8706 are the key that we have scanned so far.
8707 Those events are the ones that we will replace
8708 if PAREHT maps them into a key sequence. */
8709 int start, end;
8710 } keyremap;
8712 /* Lookup KEY in MAP.
8713 MAP is a keymap mapping keys to key vectors or functions.
8714 If the mapping is a function and DO_FUNCTION is non-zero, then
8715 the function is called with PROMPT as parameter and its return
8716 value is used as the return value of this function (after checking
8717 that it is indeed a vector). */
8719 static Lisp_Object
8720 access_keymap_keyremap (Lisp_Object map, Lisp_Object key, Lisp_Object prompt,
8721 int do_funcall)
8723 Lisp_Object next;
8725 next = access_keymap (map, key, 1, 0, 1);
8727 /* Handle symbol with autoload definition. */
8728 if (SYMBOLP (next) && !NILP (Ffboundp (next))
8729 && CONSP (XSYMBOL (next)->function)
8730 && EQ (XCAR (XSYMBOL (next)->function), Qautoload))
8731 do_autoload (XSYMBOL (next)->function, next);
8733 /* Handle a symbol whose function definition is a keymap
8734 or an array. */
8735 if (SYMBOLP (next) && !NILP (Ffboundp (next))
8736 && (ARRAYP (XSYMBOL (next)->function)
8737 || KEYMAPP (XSYMBOL (next)->function)))
8738 next = XSYMBOL (next)->function;
8740 /* If the keymap gives a function, not an
8741 array, then call the function with one arg and use
8742 its value instead. */
8743 if (SYMBOLP (next) && !NILP (Ffboundp (next)) && do_funcall)
8745 Lisp_Object tem;
8746 tem = next;
8748 next = call1 (next, prompt);
8749 /* If the function returned something invalid,
8750 barf--don't ignore it.
8751 (To ignore it safely, we would need to gcpro a bunch of
8752 other variables.) */
8753 if (! (VECTORP (next) || STRINGP (next)))
8754 error ("Function %s returns invalid key sequence", tem);
8756 return next;
8759 /* Do one step of the key remapping used for function-key-map and
8760 key-translation-map:
8761 KEYBUF is the buffer holding the input events.
8762 BUFSIZE is its maximum size.
8763 FKEY is a pointer to the keyremap structure to use.
8764 INPUT is the index of the last element in KEYBUF.
8765 DOIT if non-zero says that the remapping can actually take place.
8766 DIFF is used to return the number of keys added/removed by the remapping.
8767 PARENT is the root of the keymap.
8768 PROMPT is the prompt to use if the remapping happens through a function.
8769 The return value is non-zero if the remapping actually took place. */
8771 static int
8772 keyremap_step (Lisp_Object *keybuf, int bufsize, volatile keyremap *fkey,
8773 int input, int doit, int *diff, Lisp_Object prompt)
8775 Lisp_Object next, key;
8777 key = keybuf[fkey->end++];
8779 if (KEYMAPP (fkey->parent))
8780 next = access_keymap_keyremap (fkey->map, key, prompt, doit);
8781 else
8782 next = Qnil;
8784 /* If keybuf[fkey->start..fkey->end] is bound in the
8785 map and we're in a position to do the key remapping, replace it with
8786 the binding and restart with fkey->start at the end. */
8787 if ((VECTORP (next) || STRINGP (next)) && doit)
8789 int len = XFASTINT (Flength (next));
8790 int i;
8792 *diff = len - (fkey->end - fkey->start);
8794 if (input + *diff >= bufsize)
8795 error ("Key sequence too long");
8797 /* Shift the keys that follow fkey->end. */
8798 if (*diff < 0)
8799 for (i = fkey->end; i < input; i++)
8800 keybuf[i + *diff] = keybuf[i];
8801 else if (*diff > 0)
8802 for (i = input - 1; i >= fkey->end; i--)
8803 keybuf[i + *diff] = keybuf[i];
8804 /* Overwrite the old keys with the new ones. */
8805 for (i = 0; i < len; i++)
8806 keybuf[fkey->start + i]
8807 = Faref (next, make_number (i));
8809 fkey->start = fkey->end += *diff;
8810 fkey->map = fkey->parent;
8812 return 1;
8815 fkey->map = get_keymap (next, 0, 1);
8817 /* If we no longer have a bound suffix, try a new position for
8818 fkey->start. */
8819 if (!CONSP (fkey->map))
8821 fkey->end = ++fkey->start;
8822 fkey->map = fkey->parent;
8824 return 0;
8827 /* Read a sequence of keys that ends with a non prefix character,
8828 storing it in KEYBUF, a buffer of size BUFSIZE.
8829 Prompt with PROMPT.
8830 Return the length of the key sequence stored.
8831 Return -1 if the user rejected a command menu.
8833 Echo starting immediately unless `prompt' is 0.
8835 Where a key sequence ends depends on the currently active keymaps.
8836 These include any minor mode keymaps active in the current buffer,
8837 the current buffer's local map, and the global map.
8839 If a key sequence has no other bindings, we check Vfunction_key_map
8840 to see if some trailing subsequence might be the beginning of a
8841 function key's sequence. If so, we try to read the whole function
8842 key, and substitute its symbolic name into the key sequence.
8844 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
8845 `double-' events into similar click events, if that would make them
8846 bound. We try to turn `triple-' events first into `double-' events,
8847 then into clicks.
8849 If we get a mouse click in a mode line, vertical divider, or other
8850 non-text area, we treat the click as if it were prefixed by the
8851 symbol denoting that area - `mode-line', `vertical-line', or
8852 whatever.
8854 If the sequence starts with a mouse click, we read the key sequence
8855 with respect to the buffer clicked on, not the current buffer.
8857 If the user switches frames in the midst of a key sequence, we put
8858 off the switch-frame event until later; the next call to
8859 read_char will return it.
8861 If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
8862 from the selected window's buffer. */
8864 static int
8865 read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt,
8866 int dont_downcase_last, int can_return_switch_frame,
8867 int fix_current_buffer)
8869 Lisp_Object from_string;
8870 int count = SPECPDL_INDEX ();
8872 /* How many keys there are in the current key sequence. */
8873 int t;
8875 /* The length of the echo buffer when we started reading, and
8876 the length of this_command_keys when we started reading. */
8877 int echo_start IF_LINT (= 0);
8878 int keys_start;
8880 /* The number of keymaps we're scanning right now, and the number of
8881 keymaps we have allocated space for. */
8882 int nmaps;
8883 int nmaps_allocated = 0;
8885 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
8886 the current keymaps. */
8887 Lisp_Object *defs = NULL;
8889 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
8890 in the current keymaps, or nil where it is not a prefix. */
8891 Lisp_Object *submaps = NULL;
8893 /* The local map to start out with at start of key sequence. */
8894 Lisp_Object orig_local_map;
8896 /* The map from the `keymap' property to start out with at start of
8897 key sequence. */
8898 Lisp_Object orig_keymap;
8900 /* 1 if we have already considered switching to the local-map property
8901 of the place where a mouse click occurred. */
8902 int localized_local_map = 0;
8904 /* The index in submaps[] of the first keymap that has a binding for
8905 this key sequence. In other words, the lowest i such that
8906 submaps[i] is non-nil. */
8907 int first_binding;
8908 /* Index of the first key that has no binding.
8909 It is useless to try fkey.start larger than that. */
8910 int first_unbound;
8912 /* If t < mock_input, then KEYBUF[t] should be read as the next
8913 input key.
8915 We use this to recover after recognizing a function key. Once we
8916 realize that a suffix of the current key sequence is actually a
8917 function key's escape sequence, we replace the suffix with the
8918 function key's binding from Vfunction_key_map. Now keybuf
8919 contains a new and different key sequence, so the echo area,
8920 this_command_keys, and the submaps and defs arrays are wrong. In
8921 this situation, we set mock_input to t, set t to 0, and jump to
8922 restart_sequence; the loop will read keys from keybuf up until
8923 mock_input, thus rebuilding the state; and then it will resume
8924 reading characters from the keyboard. */
8925 int mock_input = 0;
8927 /* If the sequence is unbound in submaps[], then
8928 keybuf[fkey.start..fkey.end-1] is a prefix in Vfunction_key_map,
8929 and fkey.map is its binding.
8931 These might be > t, indicating that all function key scanning
8932 should hold off until t reaches them. We do this when we've just
8933 recognized a function key, to avoid searching for the function
8934 key's again in Vfunction_key_map. */
8935 keyremap fkey;
8937 /* Likewise, for key_translation_map and input-decode-map. */
8938 keyremap keytran, indec;
8940 /* Non-zero if we are trying to map a key by changing an upper-case
8941 letter to lower case, or a shifted function key to an unshifted
8942 one. */
8943 int shift_translated = 0;
8945 /* If we receive a `switch-frame' or `select-window' event in the middle of
8946 a key sequence, we put it off for later.
8947 While we're reading, we keep the event here. */
8948 Lisp_Object delayed_switch_frame;
8950 /* See the comment below... */
8951 #if defined (GOBBLE_FIRST_EVENT)
8952 Lisp_Object first_event;
8953 #endif
8955 Lisp_Object original_uppercase IF_LINT (= Qnil);
8956 int original_uppercase_position = -1;
8958 /* Gets around Microsoft compiler limitations. */
8959 int dummyflag = 0;
8961 struct buffer *starting_buffer;
8963 /* List of events for which a fake prefix key has been generated. */
8964 Lisp_Object fake_prefixed_keys = Qnil;
8966 #if defined (GOBBLE_FIRST_EVENT)
8967 int junk;
8968 #endif
8970 struct gcpro outer_gcpro1;
8972 GCPRO1_VAR (fake_prefixed_keys, outer_gcpro);
8973 raw_keybuf_count = 0;
8975 last_nonmenu_event = Qnil;
8977 delayed_switch_frame = Qnil;
8979 if (INTERACTIVE)
8981 if (!NILP (prompt))
8983 /* Install the string STR as the beginning of the string of
8984 echoing, so that it serves as a prompt for the next
8985 character. */
8986 KVAR (current_kboard, echo_string) = prompt;
8987 current_kboard->echo_after_prompt = SCHARS (prompt);
8988 echo_now ();
8990 else if (cursor_in_echo_area
8991 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
8992 && NILP (Fzerop (Vecho_keystrokes)))
8993 /* This doesn't put in a dash if the echo buffer is empty, so
8994 you don't always see a dash hanging out in the minibuffer. */
8995 echo_dash ();
8998 /* Record the initial state of the echo area and this_command_keys;
8999 we will need to restore them if we replay a key sequence. */
9000 if (INTERACTIVE)
9001 echo_start = echo_length ();
9002 keys_start = this_command_key_count;
9003 this_single_command_key_start = keys_start;
9005 #if defined (GOBBLE_FIRST_EVENT)
9006 /* This doesn't quite work, because some of the things that read_char
9007 does cannot safely be bypassed. It seems too risky to try to make
9008 this work right. */
9010 /* Read the first char of the sequence specially, before setting
9011 up any keymaps, in case a filter runs and switches buffers on us. */
9012 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
9013 &junk, NULL);
9014 #endif /* GOBBLE_FIRST_EVENT */
9016 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
9017 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
9018 from_string = Qnil;
9020 /* We jump here when we need to reinitialize fkey and keytran; this
9021 happens if we switch keyboards between rescans. */
9022 replay_entire_sequence:
9024 indec.map = indec.parent = KVAR (current_kboard, Vinput_decode_map);
9025 fkey.map = fkey.parent = KVAR (current_kboard, Vlocal_function_key_map);
9026 keytran.map = keytran.parent = Vkey_translation_map;
9027 indec.start = indec.end = 0;
9028 fkey.start = fkey.end = 0;
9029 keytran.start = keytran.end = 0;
9031 /* We jump here when the key sequence has been thoroughly changed, and
9032 we need to rescan it starting from the beginning. When we jump here,
9033 keybuf[0..mock_input] holds the sequence we should reread. */
9034 replay_sequence:
9036 starting_buffer = current_buffer;
9037 first_unbound = bufsize + 1;
9039 /* Build our list of keymaps.
9040 If we recognize a function key and replace its escape sequence in
9041 keybuf with its symbol, or if the sequence starts with a mouse
9042 click and we need to switch buffers, we jump back here to rebuild
9043 the initial keymaps from the current buffer. */
9044 nmaps = 0;
9046 if (!NILP (KVAR (current_kboard, Voverriding_terminal_local_map)))
9048 if (2 > nmaps_allocated)
9050 submaps = (Lisp_Object *) alloca (2 * sizeof (submaps[0]));
9051 defs = (Lisp_Object *) alloca (2 * sizeof (defs[0]));
9052 nmaps_allocated = 2;
9054 submaps[nmaps++] = KVAR (current_kboard, Voverriding_terminal_local_map);
9056 else if (!NILP (Voverriding_local_map))
9058 if (2 > nmaps_allocated)
9060 submaps = (Lisp_Object *) alloca (2 * sizeof (submaps[0]));
9061 defs = (Lisp_Object *) alloca (2 * sizeof (defs[0]));
9062 nmaps_allocated = 2;
9064 submaps[nmaps++] = Voverriding_local_map;
9066 else
9068 int nminor;
9069 int total;
9070 Lisp_Object *maps;
9072 nminor = current_minor_maps (0, &maps);
9073 total = nminor + (!NILP (orig_keymap) ? 3 : 2);
9075 if (total > nmaps_allocated)
9077 submaps = (Lisp_Object *) alloca (total * sizeof (submaps[0]));
9078 defs = (Lisp_Object *) alloca (total * sizeof (defs[0]));
9079 nmaps_allocated = total;
9082 if (!NILP (orig_keymap))
9083 submaps[nmaps++] = orig_keymap;
9085 memcpy (submaps + nmaps, maps, nminor * sizeof (submaps[0]));
9087 nmaps += nminor;
9089 submaps[nmaps++] = orig_local_map;
9091 submaps[nmaps++] = current_global_map;
9093 /* Find an accurate initial value for first_binding. */
9094 for (first_binding = 0; first_binding < nmaps; first_binding++)
9095 if (! NILP (submaps[first_binding]))
9096 break;
9098 /* Start from the beginning in keybuf. */
9099 t = 0;
9101 /* These are no-ops the first time through, but if we restart, they
9102 revert the echo area and this_command_keys to their original state. */
9103 this_command_key_count = keys_start;
9104 if (INTERACTIVE && t < mock_input)
9105 echo_truncate (echo_start);
9107 /* If the best binding for the current key sequence is a keymap, or
9108 we may be looking at a function key's escape sequence, keep on
9109 reading. */
9110 while (first_binding < nmaps
9111 /* Keep reading as long as there's a prefix binding. */
9112 ? !NILP (submaps[first_binding])
9113 /* Don't return in the middle of a possible function key sequence,
9114 if the only bindings we found were via case conversion.
9115 Thus, if ESC O a has a function-key-map translation
9116 and ESC o has a binding, don't return after ESC O,
9117 so that we can translate ESC O plus the next character. */
9118 : (/* indec.start < t || fkey.start < t || */ keytran.start < t))
9120 Lisp_Object key;
9121 int used_mouse_menu = 0;
9123 /* Where the last real key started. If we need to throw away a
9124 key that has expanded into more than one element of keybuf
9125 (say, a mouse click on the mode line which is being treated
9126 as [mode-line (mouse-...)], then we backtrack to this point
9127 of keybuf. */
9128 int last_real_key_start;
9130 /* These variables are analogous to echo_start and keys_start;
9131 while those allow us to restart the entire key sequence,
9132 echo_local_start and keys_local_start allow us to throw away
9133 just one key. */
9134 int echo_local_start IF_LINT (= 0);
9135 int keys_local_start, local_first_binding;
9137 eassert (indec.end == t || (indec.end > t && indec.end <= mock_input));
9138 eassert (indec.start <= indec.end);
9139 eassert (fkey.start <= fkey.end);
9140 eassert (keytran.start <= keytran.end);
9141 /* key-translation-map is applied *after* function-key-map
9142 which is itself applied *after* input-decode-map. */
9143 eassert (fkey.end <= indec.start);
9144 eassert (keytran.end <= fkey.start);
9146 if (/* first_unbound < indec.start && first_unbound < fkey.start && */
9147 first_unbound < keytran.start)
9148 { /* The prefix upto first_unbound has no binding and has
9149 no translation left to do either, so we know it's unbound.
9150 If we don't stop now, we risk staying here indefinitely
9151 (if the user keeps entering fkey or keytran prefixes
9152 like C-c ESC ESC ESC ESC ...) */
9153 int i;
9154 for (i = first_unbound + 1; i < t; i++)
9155 keybuf[i - first_unbound - 1] = keybuf[i];
9156 mock_input = t - first_unbound - 1;
9157 indec.end = indec.start -= first_unbound + 1;
9158 indec.map = indec.parent;
9159 fkey.end = fkey.start -= first_unbound + 1;
9160 fkey.map = fkey.parent;
9161 keytran.end = keytran.start -= first_unbound + 1;
9162 keytran.map = keytran.parent;
9163 goto replay_sequence;
9166 if (t >= bufsize)
9167 error ("Key sequence too long");
9169 if (INTERACTIVE)
9170 echo_local_start = echo_length ();
9171 keys_local_start = this_command_key_count;
9172 local_first_binding = first_binding;
9174 replay_key:
9175 /* These are no-ops, unless we throw away a keystroke below and
9176 jumped back up to replay_key; in that case, these restore the
9177 variables to their original state, allowing us to replay the
9178 loop. */
9179 if (INTERACTIVE && t < mock_input)
9180 echo_truncate (echo_local_start);
9181 this_command_key_count = keys_local_start;
9182 first_binding = local_first_binding;
9184 /* By default, assume each event is "real". */
9185 last_real_key_start = t;
9187 /* Does mock_input indicate that we are re-reading a key sequence? */
9188 if (t < mock_input)
9190 key = keybuf[t];
9191 add_command_key (key);
9192 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
9193 && NILP (Fzerop (Vecho_keystrokes)))
9194 echo_char (key);
9197 /* If not, we should actually read a character. */
9198 else
9201 KBOARD *interrupted_kboard = current_kboard;
9202 struct frame *interrupted_frame = SELECTED_FRAME ();
9203 key = read_char (NILP (prompt), nmaps,
9204 (Lisp_Object *) submaps, last_nonmenu_event,
9205 &used_mouse_menu, NULL);
9206 if ((INTEGERP (key) && XINT (key) == -2) /* wrong_kboard_jmpbuf */
9207 /* When switching to a new tty (with a new keyboard),
9208 read_char returns the new buffer, rather than -2
9209 (Bug#5095). This is because `terminal-init-xterm'
9210 calls read-char, which eats the wrong_kboard_jmpbuf
9211 return. Any better way to fix this? -- cyd */
9212 || (interrupted_kboard != current_kboard))
9214 int found = 0;
9215 struct kboard *k;
9217 for (k = all_kboards; k; k = k->next_kboard)
9218 if (k == interrupted_kboard)
9219 found = 1;
9221 if (!found)
9223 /* Don't touch interrupted_kboard when it's been
9224 deleted. */
9225 delayed_switch_frame = Qnil;
9226 goto replay_entire_sequence;
9229 if (!NILP (delayed_switch_frame))
9231 KVAR (interrupted_kboard, kbd_queue)
9232 = Fcons (delayed_switch_frame,
9233 KVAR (interrupted_kboard, kbd_queue));
9234 delayed_switch_frame = Qnil;
9237 while (t > 0)
9238 KVAR (interrupted_kboard, kbd_queue)
9239 = Fcons (keybuf[--t], KVAR (interrupted_kboard, kbd_queue));
9241 /* If the side queue is non-empty, ensure it begins with a
9242 switch-frame, so we'll replay it in the right context. */
9243 if (CONSP (KVAR (interrupted_kboard, kbd_queue))
9244 && (key = XCAR (KVAR (interrupted_kboard, kbd_queue)),
9245 !(EVENT_HAS_PARAMETERS (key)
9246 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
9247 Qswitch_frame))))
9249 Lisp_Object frame;
9250 XSETFRAME (frame, interrupted_frame);
9251 KVAR (interrupted_kboard, kbd_queue)
9252 = Fcons (make_lispy_switch_frame (frame),
9253 KVAR (interrupted_kboard, kbd_queue));
9255 mock_input = 0;
9256 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
9257 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
9258 goto replay_entire_sequence;
9262 /* read_char returns t when it shows a menu and the user rejects it.
9263 Just return -1. */
9264 if (EQ (key, Qt))
9266 unbind_to (count, Qnil);
9267 UNGCPRO_VAR (outer_gcpro);
9268 return -1;
9271 /* read_char returns -1 at the end of a macro.
9272 Emacs 18 handles this by returning immediately with a
9273 zero, so that's what we'll do. */
9274 if (INTEGERP (key) && XINT (key) == -1)
9276 t = 0;
9277 /* The Microsoft C compiler can't handle the goto that
9278 would go here. */
9279 dummyflag = 1;
9280 break;
9283 /* If the current buffer has been changed from under us, the
9284 keymap may have changed, so replay the sequence. */
9285 if (BUFFERP (key))
9287 timer_resume_idle ();
9289 mock_input = t;
9290 /* Reset the current buffer from the selected window
9291 in case something changed the former and not the latter.
9292 This is to be more consistent with the behavior
9293 of the command_loop_1. */
9294 if (fix_current_buffer)
9296 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9297 Fkill_emacs (Qnil);
9298 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
9299 Fset_buffer (XWINDOW (selected_window)->buffer);
9302 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
9303 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
9304 goto replay_sequence;
9307 /* If we have a quit that was typed in another frame, and
9308 quit_throw_to_read_char switched buffers,
9309 replay to get the right keymap. */
9310 if (INTEGERP (key)
9311 && XINT (key) == quit_char
9312 && current_buffer != starting_buffer)
9314 GROW_RAW_KEYBUF;
9315 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
9316 keybuf[t++] = key;
9317 mock_input = t;
9318 Vquit_flag = Qnil;
9319 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
9320 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
9321 goto replay_sequence;
9324 Vquit_flag = Qnil;
9326 if (EVENT_HAS_PARAMETERS (key)
9327 /* Either a `switch-frame' or a `select-window' event. */
9328 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), Qswitch_frame))
9330 /* If we're at the beginning of a key sequence, and the caller
9331 says it's okay, go ahead and return this event. If we're
9332 in the midst of a key sequence, delay it until the end. */
9333 if (t > 0 || !can_return_switch_frame)
9335 delayed_switch_frame = key;
9336 goto replay_key;
9340 GROW_RAW_KEYBUF;
9341 ASET (raw_keybuf, raw_keybuf_count, key);
9342 raw_keybuf_count++;
9345 /* Clicks in non-text areas get prefixed by the symbol
9346 in their CHAR-ADDRESS field. For example, a click on
9347 the mode line is prefixed by the symbol `mode-line'.
9349 Furthermore, key sequences beginning with mouse clicks
9350 are read using the keymaps of the buffer clicked on, not
9351 the current buffer. So we may have to switch the buffer
9352 here.
9354 When we turn one event into two events, we must make sure
9355 that neither of the two looks like the original--so that,
9356 if we replay the events, they won't be expanded again.
9357 If not for this, such reexpansion could happen either here
9358 or when user programs play with this-command-keys. */
9359 if (EVENT_HAS_PARAMETERS (key))
9361 Lisp_Object kind;
9362 Lisp_Object string;
9364 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
9365 if (EQ (kind, Qmouse_click))
9367 Lisp_Object window, posn;
9369 window = POSN_WINDOW (EVENT_START (key));
9370 posn = POSN_POSN (EVENT_START (key));
9372 if (CONSP (posn)
9373 || (!NILP (fake_prefixed_keys)
9374 && !NILP (Fmemq (key, fake_prefixed_keys))))
9376 /* We're looking a second time at an event for which
9377 we generated a fake prefix key. Set
9378 last_real_key_start appropriately. */
9379 if (t > 0)
9380 last_real_key_start = t - 1;
9383 /* Key sequences beginning with mouse clicks are
9384 read using the keymaps in the buffer clicked on,
9385 not the current buffer. If we're at the
9386 beginning of a key sequence, switch buffers. */
9387 if (last_real_key_start == 0
9388 && WINDOWP (window)
9389 && BUFFERP (XWINDOW (window)->buffer)
9390 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
9392 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
9393 keybuf[t] = key;
9394 mock_input = t + 1;
9396 /* Arrange to go back to the original buffer once we're
9397 done reading the key sequence. Note that we can't
9398 use save_excursion_{save,restore} here, because they
9399 save point as well as the current buffer; we don't
9400 want to save point, because redisplay may change it,
9401 to accommodate a Fset_window_start or something. We
9402 don't want to do this at the top of the function,
9403 because we may get input from a subprocess which
9404 wants to change the selected window and stuff (say,
9405 emacsclient). */
9406 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
9408 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9409 Fkill_emacs (Qnil);
9410 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
9411 orig_local_map = get_local_map (PT, current_buffer,
9412 Qlocal_map);
9413 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
9414 goto replay_sequence;
9417 /* For a mouse click, get the local text-property keymap
9418 of the place clicked on, rather than point. */
9419 if (last_real_key_start == 0
9420 && CONSP (XCDR (key))
9421 && ! localized_local_map)
9423 Lisp_Object map_here, start, pos;
9425 localized_local_map = 1;
9426 start = EVENT_START (key);
9428 if (CONSP (start) && POSN_INBUFFER_P (start))
9430 pos = POSN_BUFFER_POSN (start);
9431 if (INTEGERP (pos)
9432 && XINT (pos) >= BEGV
9433 && XINT (pos) <= ZV)
9435 map_here = get_local_map (XINT (pos),
9436 current_buffer, Qlocal_map);
9437 if (!EQ (map_here, orig_local_map))
9439 orig_local_map = map_here;
9440 ++localized_local_map;
9443 map_here = get_local_map (XINT (pos),
9444 current_buffer, Qkeymap);
9445 if (!EQ (map_here, orig_keymap))
9447 orig_keymap = map_here;
9448 ++localized_local_map;
9451 if (localized_local_map > 1)
9453 keybuf[t] = key;
9454 mock_input = t + 1;
9456 goto replay_sequence;
9462 /* Expand mode-line and scroll-bar events into two events:
9463 use posn as a fake prefix key. */
9464 if (SYMBOLP (posn)
9465 && (NILP (fake_prefixed_keys)
9466 || NILP (Fmemq (key, fake_prefixed_keys))))
9468 if (t + 1 >= bufsize)
9469 error ("Key sequence too long");
9471 keybuf[t] = posn;
9472 keybuf[t + 1] = key;
9473 mock_input = t + 2;
9475 /* Record that a fake prefix key has been generated
9476 for KEY. Don't modify the event; this would
9477 prevent proper action when the event is pushed
9478 back into unread-command-events. */
9479 fake_prefixed_keys = Fcons (key, fake_prefixed_keys);
9481 /* If on a mode line string with a local keymap,
9482 reconsider the key sequence with that keymap. */
9483 if (string = POSN_STRING (EVENT_START (key)),
9484 (CONSP (string) && STRINGP (XCAR (string))))
9486 Lisp_Object pos, map, map2;
9488 pos = XCDR (string);
9489 string = XCAR (string);
9490 if (XINT (pos) >= 0
9491 && XINT (pos) < SCHARS (string))
9493 map = Fget_text_property (pos, Qlocal_map, string);
9494 if (!NILP (map))
9495 orig_local_map = map;
9496 map2 = Fget_text_property (pos, Qkeymap, string);
9497 if (!NILP (map2))
9498 orig_keymap = map2;
9499 if (!NILP (map) || !NILP (map2))
9500 goto replay_sequence;
9504 goto replay_key;
9506 else if (NILP (from_string)
9507 && (string = POSN_STRING (EVENT_START (key)),
9508 (CONSP (string) && STRINGP (XCAR (string)))))
9510 /* For a click on a string, i.e. overlay string or a
9511 string displayed via the `display' property,
9512 consider `local-map' and `keymap' properties of
9513 that string. */
9514 Lisp_Object pos, map, map2;
9516 pos = XCDR (string);
9517 string = XCAR (string);
9518 if (XINT (pos) >= 0
9519 && XINT (pos) < SCHARS (string))
9521 map = Fget_text_property (pos, Qlocal_map, string);
9522 if (!NILP (map))
9523 orig_local_map = map;
9524 map2 = Fget_text_property (pos, Qkeymap, string);
9525 if (!NILP (map2))
9526 orig_keymap = map2;
9528 if (!NILP (map) || !NILP (map2))
9530 from_string = string;
9531 keybuf[t++] = key;
9532 mock_input = t;
9533 goto replay_sequence;
9538 else if (CONSP (XCDR (key))
9539 && CONSP (EVENT_START (key))
9540 && CONSP (XCDR (EVENT_START (key))))
9542 Lisp_Object posn;
9544 posn = POSN_POSN (EVENT_START (key));
9545 /* Handle menu-bar events:
9546 insert the dummy prefix event `menu-bar'. */
9547 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
9549 if (t + 1 >= bufsize)
9550 error ("Key sequence too long");
9551 keybuf[t] = posn;
9552 keybuf[t+1] = key;
9554 /* Zap the position in key, so we know that we've
9555 expanded it, and don't try to do so again. */
9556 POSN_SET_POSN (EVENT_START (key),
9557 Fcons (posn, Qnil));
9559 mock_input = t + 2;
9560 goto replay_sequence;
9562 else if (CONSP (posn))
9564 /* We're looking at the second event of a
9565 sequence which we expanded before. Set
9566 last_real_key_start appropriately. */
9567 if (last_real_key_start == t && t > 0)
9568 last_real_key_start = t - 1;
9573 /* We have finally decided that KEY is something we might want
9574 to look up. */
9575 first_binding = (follow_key (key,
9576 nmaps - first_binding,
9577 submaps + first_binding,
9578 defs + first_binding,
9579 submaps + first_binding)
9580 + first_binding);
9582 /* If KEY wasn't bound, we'll try some fallbacks. */
9583 if (first_binding < nmaps)
9584 /* This is needed for the following scenario:
9585 event 0: a down-event that gets dropped by calling replay_key.
9586 event 1: some normal prefix like C-h.
9587 After event 0, first_unbound is 0, after event 1 indec.start,
9588 fkey.start, and keytran.start are all 1, so when we see that
9589 C-h is bound, we need to update first_unbound. */
9590 first_unbound = max (t + 1, first_unbound);
9591 else
9593 Lisp_Object head;
9595 /* Remember the position to put an upper bound on indec.start. */
9596 first_unbound = min (t, first_unbound);
9598 head = EVENT_HEAD (key);
9599 if (help_char_p (head) && t > 0)
9601 read_key_sequence_cmd = Vprefix_help_command;
9602 keybuf[t++] = key;
9603 last_nonmenu_event = key;
9604 /* The Microsoft C compiler can't handle the goto that
9605 would go here. */
9606 dummyflag = 1;
9607 break;
9610 if (SYMBOLP (head))
9612 Lisp_Object breakdown;
9613 int modifiers;
9615 breakdown = parse_modifiers (head);
9616 modifiers = XINT (XCAR (XCDR (breakdown)));
9617 /* Attempt to reduce an unbound mouse event to a simpler
9618 event that is bound:
9619 Drags reduce to clicks.
9620 Double-clicks reduce to clicks.
9621 Triple-clicks reduce to double-clicks, then to clicks.
9622 Down-clicks are eliminated.
9623 Double-downs reduce to downs, then are eliminated.
9624 Triple-downs reduce to double-downs, then to downs,
9625 then are eliminated. */
9626 if (modifiers & (down_modifier | drag_modifier
9627 | double_modifier | triple_modifier))
9629 while (modifiers & (down_modifier | drag_modifier
9630 | double_modifier | triple_modifier))
9632 Lisp_Object new_head, new_click;
9633 if (modifiers & triple_modifier)
9634 modifiers ^= (double_modifier | triple_modifier);
9635 else if (modifiers & double_modifier)
9636 modifiers &= ~double_modifier;
9637 else if (modifiers & drag_modifier)
9638 modifiers &= ~drag_modifier;
9639 else
9641 /* Dispose of this `down' event by simply jumping
9642 back to replay_key, to get another event.
9644 Note that if this event came from mock input,
9645 then just jumping back to replay_key will just
9646 hand it to us again. So we have to wipe out any
9647 mock input.
9649 We could delete keybuf[t] and shift everything
9650 after that to the left by one spot, but we'd also
9651 have to fix up any variable that points into
9652 keybuf, and shifting isn't really necessary
9653 anyway.
9655 Adding prefixes for non-textual mouse clicks
9656 creates two characters of mock input, and both
9657 must be thrown away. If we're only looking at
9658 the prefix now, we can just jump back to
9659 replay_key. On the other hand, if we've already
9660 processed the prefix, and now the actual click
9661 itself is giving us trouble, then we've lost the
9662 state of the keymaps we want to backtrack to, and
9663 we need to replay the whole sequence to rebuild
9666 Beyond that, only function key expansion could
9667 create more than two keys, but that should never
9668 generate mouse events, so it's okay to zero
9669 mock_input in that case too.
9671 FIXME: The above paragraph seems just plain
9672 wrong, if you consider things like
9673 xterm-mouse-mode. -stef
9675 Isn't this just the most wonderful code ever? */
9677 /* If mock_input > t + 1, the above simplification
9678 will actually end up dropping keys on the floor.
9679 This is probably OK for now, but even
9680 if mock_input <= t + 1, we need to adjust indec,
9681 fkey, and keytran.
9682 Typical case [header-line down-mouse-N]:
9683 mock_input = 2, t = 1, fkey.end = 1,
9684 last_real_key_start = 0. */
9685 if (indec.end > last_real_key_start)
9687 indec.end = indec.start
9688 = min (last_real_key_start, indec.start);
9689 indec.map = indec.parent;
9690 if (fkey.end > last_real_key_start)
9692 fkey.end = fkey.start
9693 = min (last_real_key_start, fkey.start);
9694 fkey.map = fkey.parent;
9695 if (keytran.end > last_real_key_start)
9697 keytran.end = keytran.start
9698 = min (last_real_key_start, keytran.start);
9699 keytran.map = keytran.parent;
9703 if (t == last_real_key_start)
9705 mock_input = 0;
9706 goto replay_key;
9708 else
9710 mock_input = last_real_key_start;
9711 goto replay_sequence;
9715 new_head
9716 = apply_modifiers (modifiers, XCAR (breakdown));
9717 new_click
9718 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
9720 /* Look for a binding for this new key. follow_key
9721 promises that it didn't munge submaps the
9722 last time we called it, since key was unbound. */
9723 first_binding
9724 = (follow_key (new_click,
9725 nmaps - local_first_binding,
9726 submaps + local_first_binding,
9727 defs + local_first_binding,
9728 submaps + local_first_binding)
9729 + local_first_binding);
9731 /* If that click is bound, go for it. */
9732 if (first_binding < nmaps)
9734 key = new_click;
9735 break;
9737 /* Otherwise, we'll leave key set to the drag event. */
9743 keybuf[t++] = key;
9744 /* Normally, last_nonmenu_event gets the previous key we read.
9745 But when a mouse popup menu is being used,
9746 we don't update last_nonmenu_event; it continues to hold the mouse
9747 event that preceded the first level of menu. */
9748 if (!used_mouse_menu)
9749 last_nonmenu_event = key;
9751 /* Record what part of this_command_keys is the current key sequence. */
9752 this_single_command_key_start = this_command_key_count - t;
9754 /* Look for this sequence in input-decode-map.
9755 Scan from indec.end until we find a bound suffix. */
9756 while (indec.end < t)
9758 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9759 int done, diff;
9761 GCPRO4 (indec.map, fkey.map, keytran.map, delayed_switch_frame);
9762 done = keyremap_step (keybuf, bufsize, &indec, max (t, mock_input),
9763 1, &diff, prompt);
9764 UNGCPRO;
9765 if (done)
9767 mock_input = diff + max (t, mock_input);
9768 goto replay_sequence;
9772 if (first_binding < nmaps && NILP (submaps[first_binding])
9773 && indec.start >= t)
9774 /* There is a binding and it's not a prefix.
9775 (and it doesn't have any input-decode-map translation pending).
9776 There is thus no function-key in this sequence.
9777 Moving fkey.start is important in this case to allow keytran.start
9778 to go over the sequence before we return (since we keep the
9779 invariant that keytran.end <= fkey.start). */
9781 if (fkey.start < t)
9782 (fkey.start = fkey.end = t, fkey.map = fkey.parent);
9784 else
9785 /* If the sequence is unbound, see if we can hang a function key
9786 off the end of it. */
9787 /* Continue scan from fkey.end until we find a bound suffix. */
9788 while (fkey.end < indec.start)
9790 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9791 int done, diff;
9793 GCPRO4 (indec.map, fkey.map, keytran.map, delayed_switch_frame);
9794 done = keyremap_step (keybuf, bufsize, &fkey,
9795 max (t, mock_input),
9796 /* If there's a binding (i.e.
9797 first_binding >= nmaps) we don't want
9798 to apply this function-key-mapping. */
9799 fkey.end + 1 == t && first_binding >= nmaps,
9800 &diff, prompt);
9801 UNGCPRO;
9802 if (done)
9804 mock_input = diff + max (t, mock_input);
9805 /* Adjust the input-decode-map counters. */
9806 indec.end += diff;
9807 indec.start += diff;
9809 goto replay_sequence;
9813 /* Look for this sequence in key-translation-map.
9814 Scan from keytran.end until we find a bound suffix. */
9815 while (keytran.end < fkey.start)
9817 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9818 int done, diff;
9820 GCPRO4 (indec.map, fkey.map, keytran.map, delayed_switch_frame);
9821 done = keyremap_step (keybuf, bufsize, &keytran, max (t, mock_input),
9822 1, &diff, prompt);
9823 UNGCPRO;
9824 if (done)
9826 mock_input = diff + max (t, mock_input);
9827 /* Adjust the function-key-map and input-decode-map counters. */
9828 indec.end += diff;
9829 indec.start += diff;
9830 fkey.end += diff;
9831 fkey.start += diff;
9833 goto replay_sequence;
9837 /* If KEY is not defined in any of the keymaps,
9838 and cannot be part of a function key or translation,
9839 and is an upper case letter
9840 use the corresponding lower-case letter instead. */
9841 if (first_binding >= nmaps
9842 && /* indec.start >= t && fkey.start >= t && */ keytran.start >= t
9843 && INTEGERP (key)
9844 && ((CHARACTERP (make_number (XINT (key) & ~CHAR_MODIFIER_MASK))
9845 && uppercasep (XINT (key) & ~CHAR_MODIFIER_MASK))
9846 || (XINT (key) & shift_modifier)))
9848 Lisp_Object new_key;
9850 original_uppercase = key;
9851 original_uppercase_position = t - 1;
9853 if (XINT (key) & shift_modifier)
9854 XSETINT (new_key, XINT (key) & ~shift_modifier);
9855 else
9856 XSETINT (new_key, (downcase (XINT (key) & ~CHAR_MODIFIER_MASK)
9857 | (XINT (key) & CHAR_MODIFIER_MASK)));
9859 /* We have to do this unconditionally, regardless of whether
9860 the lower-case char is defined in the keymaps, because they
9861 might get translated through function-key-map. */
9862 keybuf[t - 1] = new_key;
9863 mock_input = max (t, mock_input);
9864 shift_translated = 1;
9866 goto replay_sequence;
9868 /* If KEY is not defined in any of the keymaps,
9869 and cannot be part of a function key or translation,
9870 and is a shifted function key,
9871 use the corresponding unshifted function key instead. */
9872 if (first_binding >= nmaps
9873 && /* indec.start >= t && fkey.start >= t && */ keytran.start >= t)
9875 Lisp_Object breakdown = parse_modifiers (key);
9876 int modifiers
9877 = CONSP (breakdown) ? (XINT (XCAR (XCDR (breakdown)))) : 0;
9879 if (modifiers & shift_modifier
9880 /* Treat uppercase keys as shifted. */
9881 || (INTEGERP (key)
9882 && (KEY_TO_CHAR (key)
9883 < XCHAR_TABLE (BVAR (current_buffer, downcase_table))->size)
9884 && uppercasep (KEY_TO_CHAR (key))))
9886 Lisp_Object new_key
9887 = (modifiers & shift_modifier
9888 ? apply_modifiers (modifiers & ~shift_modifier,
9889 XCAR (breakdown))
9890 : make_number (downcase (KEY_TO_CHAR (key)) | modifiers));
9892 original_uppercase = key;
9893 original_uppercase_position = t - 1;
9895 /* We have to do this unconditionally, regardless of whether
9896 the lower-case char is defined in the keymaps, because they
9897 might get translated through function-key-map. */
9898 keybuf[t - 1] = new_key;
9899 mock_input = max (t, mock_input);
9900 /* Reset fkey (and consequently keytran) to apply
9901 function-key-map on the result, so that S-backspace is
9902 correctly mapped to DEL (via backspace). OTOH,
9903 input-decode-map doesn't need to go through it again. */
9904 fkey.start = fkey.end = 0;
9905 keytran.start = keytran.end = 0;
9906 shift_translated = 1;
9908 goto replay_sequence;
9912 if (!dummyflag)
9913 read_key_sequence_cmd = (first_binding < nmaps
9914 ? defs[first_binding]
9915 : Qnil);
9917 unread_switch_frame = delayed_switch_frame;
9918 unbind_to (count, Qnil);
9920 /* Don't downcase the last character if the caller says don't.
9921 Don't downcase it if the result is undefined, either. */
9922 if ((dont_downcase_last || first_binding >= nmaps)
9923 && t > 0
9924 && t - 1 == original_uppercase_position)
9926 keybuf[t - 1] = original_uppercase;
9927 shift_translated = 0;
9930 if (shift_translated)
9931 Vthis_command_keys_shift_translated = Qt;
9933 /* Occasionally we fabricate events, perhaps by expanding something
9934 according to function-key-map, or by adding a prefix symbol to a
9935 mouse click in the scroll bar or modeline. In this cases, return
9936 the entire generated key sequence, even if we hit an unbound
9937 prefix or a definition before the end. This means that you will
9938 be able to push back the event properly, and also means that
9939 read-key-sequence will always return a logical unit.
9941 Better ideas? */
9942 for (; t < mock_input; t++)
9944 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
9945 && NILP (Fzerop (Vecho_keystrokes)))
9946 echo_char (keybuf[t]);
9947 add_command_key (keybuf[t]);
9950 UNGCPRO_VAR (outer_gcpro);
9951 return t;
9954 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 5, 0,
9955 doc: /* Read a sequence of keystrokes and return as a string or vector.
9956 The sequence is sufficient to specify a non-prefix command in the
9957 current local and global maps.
9959 First arg PROMPT is a prompt string. If nil, do not prompt specially.
9960 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos
9961 as a continuation of the previous key.
9963 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not
9964 convert the last event to lower case. (Normally any upper case event
9965 is converted to lower case if the original event is undefined and the lower
9966 case equivalent is defined.) A non-nil value is appropriate for reading
9967 a key sequence to be defined.
9969 A C-g typed while in this function is treated like any other character,
9970 and `quit-flag' is not set.
9972 If the key sequence starts with a mouse click, then the sequence is read
9973 using the keymaps of the buffer of the window clicked in, not the buffer
9974 of the selected window as normal.
9976 `read-key-sequence' drops unbound button-down events, since you normally
9977 only care about the click or drag events which follow them. If a drag
9978 or multi-click event is unbound, but the corresponding click event would
9979 be bound, `read-key-sequence' turns the event into a click event at the
9980 drag's starting position. This means that you don't have to distinguish
9981 between click and drag, double, or triple events unless you want to.
9983 `read-key-sequence' prefixes mouse events on mode lines, the vertical
9984 lines separating windows, and scroll bars with imaginary keys
9985 `mode-line', `vertical-line', and `vertical-scroll-bar'.
9987 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this
9988 function will process a switch-frame event if the user switches frames
9989 before typing anything. If the user switches frames in the middle of a
9990 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME
9991 is nil, then the event will be put off until after the current key sequence.
9993 `read-key-sequence' checks `function-key-map' for function key
9994 sequences, where they wouldn't conflict with ordinary bindings. See
9995 `function-key-map' for more details.
9997 The optional fifth argument CMD-LOOP, if non-nil, means
9998 that this key sequence is being read by something that will
9999 read commands one after another. It should be nil if the caller
10000 will read just one key sequence. */)
10001 (Lisp_Object prompt, Lisp_Object continue_echo, Lisp_Object dont_downcase_last, Lisp_Object can_return_switch_frame, Lisp_Object cmd_loop)
10003 Lisp_Object keybuf[30];
10004 register int i;
10005 struct gcpro gcpro1;
10006 int count = SPECPDL_INDEX ();
10008 if (!NILP (prompt))
10009 CHECK_STRING (prompt);
10010 QUIT;
10012 specbind (Qinput_method_exit_on_first_char,
10013 (NILP (cmd_loop) ? Qt : Qnil));
10014 specbind (Qinput_method_use_echo_area,
10015 (NILP (cmd_loop) ? Qt : Qnil));
10017 memset (keybuf, 0, sizeof keybuf);
10018 GCPRO1 (keybuf[0]);
10019 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
10021 if (NILP (continue_echo))
10023 this_command_key_count = 0;
10024 this_command_key_count_reset = 0;
10025 this_single_command_key_start = 0;
10028 #ifdef HAVE_WINDOW_SYSTEM
10029 if (display_hourglass_p)
10030 cancel_hourglass ();
10031 #endif
10033 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
10034 prompt, ! NILP (dont_downcase_last),
10035 ! NILP (can_return_switch_frame), 0);
10037 #if 0 /* The following is fine for code reading a key sequence and
10038 then proceeding with a lenghty computation, but it's not good
10039 for code reading keys in a loop, like an input method. */
10040 #ifdef HAVE_WINDOW_SYSTEM
10041 if (display_hourglass_p)
10042 start_hourglass ();
10043 #endif
10044 #endif
10046 if (i == -1)
10048 Vquit_flag = Qt;
10049 QUIT;
10051 UNGCPRO;
10052 return unbind_to (count, make_event_array (i, keybuf));
10055 DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
10056 Sread_key_sequence_vector, 1, 5, 0,
10057 doc: /* Like `read-key-sequence' but always return a vector. */)
10058 (Lisp_Object prompt, Lisp_Object continue_echo, Lisp_Object dont_downcase_last, Lisp_Object can_return_switch_frame, Lisp_Object cmd_loop)
10060 Lisp_Object keybuf[30];
10061 register int i;
10062 struct gcpro gcpro1;
10063 int count = SPECPDL_INDEX ();
10065 if (!NILP (prompt))
10066 CHECK_STRING (prompt);
10067 QUIT;
10069 specbind (Qinput_method_exit_on_first_char,
10070 (NILP (cmd_loop) ? Qt : Qnil));
10071 specbind (Qinput_method_use_echo_area,
10072 (NILP (cmd_loop) ? Qt : Qnil));
10074 memset (keybuf, 0, sizeof keybuf);
10075 GCPRO1 (keybuf[0]);
10076 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
10078 if (NILP (continue_echo))
10080 this_command_key_count = 0;
10081 this_command_key_count_reset = 0;
10082 this_single_command_key_start = 0;
10085 #ifdef HAVE_WINDOW_SYSTEM
10086 if (display_hourglass_p)
10087 cancel_hourglass ();
10088 #endif
10090 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
10091 prompt, ! NILP (dont_downcase_last),
10092 ! NILP (can_return_switch_frame), 0);
10094 #ifdef HAVE_WINDOW_SYSTEM
10095 if (display_hourglass_p)
10096 start_hourglass ();
10097 #endif
10099 if (i == -1)
10101 Vquit_flag = Qt;
10102 QUIT;
10104 UNGCPRO;
10105 return unbind_to (count, Fvector (i, keybuf));
10108 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
10109 doc: /* Execute CMD as an editor command.
10110 CMD must be a symbol that satisfies the `commandp' predicate.
10111 Optional second arg RECORD-FLAG non-nil
10112 means unconditionally put this command in `command-history'.
10113 Otherwise, that is done only if an arg is read using the minibuffer.
10114 The argument KEYS specifies the value to use instead of (this-command-keys)
10115 when reading the arguments; if it is nil, (this-command-keys) is used.
10116 The argument SPECIAL, if non-nil, means that this command is executing
10117 a special event, so ignore the prefix argument and don't clear it. */)
10118 (Lisp_Object cmd, Lisp_Object record_flag, Lisp_Object keys, Lisp_Object special)
10120 register Lisp_Object final;
10121 register Lisp_Object tem;
10122 Lisp_Object prefixarg;
10124 debug_on_next_call = 0;
10126 if (NILP (special))
10128 prefixarg = KVAR (current_kboard, Vprefix_arg);
10129 Vcurrent_prefix_arg = prefixarg;
10130 KVAR (current_kboard, Vprefix_arg) = Qnil;
10132 else
10133 prefixarg = Qnil;
10135 if (SYMBOLP (cmd))
10137 tem = Fget (cmd, Qdisabled);
10138 if (!NILP (tem) && !NILP (Vrun_hooks))
10140 tem = Fsymbol_value (Qdisabled_command_function);
10141 if (!NILP (tem))
10142 return call1 (Vrun_hooks, Qdisabled_command_function);
10146 while (1)
10148 final = Findirect_function (cmd, Qnil);
10150 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
10152 struct gcpro gcpro1, gcpro2;
10154 GCPRO2 (cmd, prefixarg);
10155 do_autoload (final, cmd);
10156 UNGCPRO;
10158 else
10159 break;
10162 if (STRINGP (final) || VECTORP (final))
10164 /* If requested, place the macro in the command history. For
10165 other sorts of commands, call-interactively takes care of
10166 this. */
10167 if (!NILP (record_flag))
10169 Vcommand_history
10170 = Fcons (Fcons (Qexecute_kbd_macro,
10171 Fcons (final, Fcons (prefixarg, Qnil))),
10172 Vcommand_history);
10174 /* Don't keep command history around forever. */
10175 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
10177 tem = Fnthcdr (Vhistory_length, Vcommand_history);
10178 if (CONSP (tem))
10179 XSETCDR (tem, Qnil);
10183 return Fexecute_kbd_macro (final, prefixarg, Qnil);
10186 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
10187 /* Don't call Fcall_interactively directly because we want to make
10188 sure the backtrace has an entry for `call-interactively'.
10189 For the same reason, pass `cmd' rather than `final'. */
10190 return call3 (Qcall_interactively, cmd, record_flag, keys);
10192 return Qnil;
10197 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
10198 1, 1, "P",
10199 doc: /* Read function name, then read its arguments and call it.
10201 To pass a numeric argument to the command you are invoking with, specify
10202 the numeric argument to this command.
10204 Noninteractively, the argument PREFIXARG is the prefix argument to
10205 give to the command you invoke, if it asks for an argument. */)
10206 (Lisp_Object prefixarg)
10208 Lisp_Object function;
10209 EMACS_INT saved_last_point_position;
10210 Lisp_Object saved_keys, saved_last_point_position_buffer;
10211 Lisp_Object bindings, value;
10212 struct gcpro gcpro1, gcpro2, gcpro3;
10213 #ifdef HAVE_WINDOW_SYSTEM
10214 /* The call to Fcompleting_read will start and cancel the hourglass,
10215 but if the hourglass was already scheduled, this means that no
10216 hourglass will be shown for the actual M-x command itself.
10217 So we restart it if it is already scheduled. Note that checking
10218 hourglass_shown_p is not enough, normally the hourglass is not shown,
10219 just scheduled to be shown. */
10220 int hstarted = hourglass_started ();
10221 #endif
10223 saved_keys = Fvector (this_command_key_count,
10224 XVECTOR (this_command_keys)->contents);
10225 saved_last_point_position_buffer = last_point_position_buffer;
10226 saved_last_point_position = last_point_position;
10227 GCPRO3 (saved_keys, prefixarg, saved_last_point_position_buffer);
10229 function = call0 (intern ("read-extended-command"));
10231 #ifdef HAVE_WINDOW_SYSTEM
10232 if (hstarted) start_hourglass ();
10233 #endif
10235 if (STRINGP (function) && SCHARS (function) == 0)
10236 error ("No command name given");
10238 /* Set this_command_keys to the concatenation of saved_keys and
10239 function, followed by a RET. */
10241 Lisp_Object *keys;
10242 int i;
10244 this_command_key_count = 0;
10245 this_command_key_count_reset = 0;
10246 this_single_command_key_start = 0;
10248 keys = XVECTOR (saved_keys)->contents;
10249 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
10250 add_command_key (keys[i]);
10252 for (i = 0; i < SCHARS (function); i++)
10253 add_command_key (Faref (function, make_number (i)));
10255 add_command_key (make_number ('\015'));
10258 last_point_position = saved_last_point_position;
10259 last_point_position_buffer = saved_last_point_position_buffer;
10261 UNGCPRO;
10263 function = Fintern (function, Qnil);
10264 KVAR (current_kboard, Vprefix_arg) = prefixarg;
10265 Vthis_command = function;
10266 real_this_command = function;
10268 /* If enabled, show which key runs this command. */
10269 if (!NILP (Vsuggest_key_bindings)
10270 && NILP (Vexecuting_kbd_macro)
10271 && SYMBOLP (function))
10272 bindings = Fwhere_is_internal (function, Voverriding_local_map,
10273 Qt, Qnil, Qnil);
10274 else
10275 bindings = Qnil;
10277 value = Qnil;
10278 GCPRO3 (bindings, value, function);
10279 value = Fcommand_execute (function, Qt, Qnil, Qnil);
10281 /* If the command has a key binding, print it now. */
10282 if (!NILP (bindings)
10283 && ! (VECTORP (bindings) && EQ (Faref (bindings, make_number (0)),
10284 Qmouse_movement)))
10286 /* But first wait, and skip the message if there is input. */
10287 Lisp_Object waited;
10289 /* If this command displayed something in the echo area;
10290 wait a few seconds, then display our suggestion message. */
10291 if (NILP (echo_area_buffer[0]))
10292 waited = sit_for (make_number (0), 0, 2);
10293 else if (NUMBERP (Vsuggest_key_bindings))
10294 waited = sit_for (Vsuggest_key_bindings, 0, 2);
10295 else
10296 waited = sit_for (make_number (2), 0, 2);
10298 if (!NILP (waited) && ! CONSP (Vunread_command_events))
10300 Lisp_Object binding;
10301 char *newmessage;
10302 int message_p = push_message ();
10303 int count = SPECPDL_INDEX ();
10305 record_unwind_protect (pop_message_unwind, Qnil);
10306 binding = Fkey_description (bindings, Qnil);
10308 newmessage
10309 = (char *) alloca (SCHARS (SYMBOL_NAME (function))
10310 + SBYTES (binding)
10311 + 100);
10312 sprintf (newmessage, "You can run the command `%s' with %s",
10313 SDATA (SYMBOL_NAME (function)),
10314 SDATA (binding));
10315 message2_nolog (newmessage,
10316 strlen (newmessage),
10317 STRING_MULTIBYTE (binding));
10318 if (NUMBERP (Vsuggest_key_bindings))
10319 waited = sit_for (Vsuggest_key_bindings, 0, 2);
10320 else
10321 waited = sit_for (make_number (2), 0, 2);
10323 if (!NILP (waited) && message_p)
10324 restore_message ();
10326 unbind_to (count, Qnil);
10330 RETURN_UNGCPRO (value);
10334 /* Return nonzero if input events are pending. */
10337 detect_input_pending (void)
10339 if (!input_pending)
10340 get_input_pending (&input_pending, 0);
10342 return input_pending;
10345 /* Return nonzero if input events other than mouse movements are
10346 pending. */
10349 detect_input_pending_ignore_squeezables (void)
10351 if (!input_pending)
10352 get_input_pending (&input_pending, READABLE_EVENTS_IGNORE_SQUEEZABLES);
10354 return input_pending;
10357 /* Return nonzero if input events are pending, and run any pending timers. */
10360 detect_input_pending_run_timers (int do_display)
10362 int old_timers_run = timers_run;
10364 if (!input_pending)
10365 get_input_pending (&input_pending, READABLE_EVENTS_DO_TIMERS_NOW);
10367 if (old_timers_run != timers_run && do_display)
10369 redisplay_preserve_echo_area (8);
10370 /* The following fixes a bug when using lazy-lock with
10371 lazy-lock-defer-on-the-fly set to t, i.e. when fontifying
10372 from an idle timer function. The symptom of the bug is that
10373 the cursor sometimes doesn't become visible until the next X
10374 event is processed. --gerd. */
10376 Lisp_Object tail, frame;
10377 FOR_EACH_FRAME (tail, frame)
10378 if (FRAME_RIF (XFRAME (frame)))
10379 FRAME_RIF (XFRAME (frame))->flush_display (XFRAME (frame));
10383 return input_pending;
10386 /* This is called in some cases before a possible quit.
10387 It cases the next call to detect_input_pending to recompute input_pending.
10388 So calling this function unnecessarily can't do any harm. */
10390 void
10391 clear_input_pending (void)
10393 input_pending = 0;
10396 /* Return nonzero if there are pending requeued events.
10397 This isn't used yet. The hope is to make wait_reading_process_output
10398 call it, and return if it runs Lisp code that unreads something.
10399 The problem is, kbd_buffer_get_event needs to be fixed to know what
10400 to do in that case. It isn't trivial. */
10403 requeued_events_pending_p (void)
10405 return (!NILP (Vunread_command_events) || unread_command_char != -1);
10409 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
10410 doc: /* Return t if command input is currently available with no wait.
10411 Actually, the value is nil only if we can be sure that no input is available;
10412 if there is a doubt, the value is t. */)
10413 (void)
10415 if (!NILP (Vunread_command_events) || unread_command_char != -1
10416 || !NILP (Vunread_post_input_method_events)
10417 || !NILP (Vunread_input_method_events))
10418 return (Qt);
10420 get_input_pending (&input_pending,
10421 READABLE_EVENTS_DO_TIMERS_NOW
10422 | READABLE_EVENTS_FILTER_EVENTS);
10423 return input_pending > 0 ? Qt : Qnil;
10426 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
10427 doc: /* Return vector of last 300 events, not counting those from keyboard macros. */)
10428 (void)
10430 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
10431 Lisp_Object val;
10433 if (total_keys < NUM_RECENT_KEYS)
10434 return Fvector (total_keys, keys);
10435 else
10437 val = Fvector (NUM_RECENT_KEYS, keys);
10438 memcpy (XVECTOR (val)->contents, keys + recent_keys_index,
10439 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
10440 memcpy (XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
10441 keys, recent_keys_index * sizeof (Lisp_Object));
10442 return val;
10446 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
10447 doc: /* Return the key sequence that invoked this command.
10448 However, if the command has called `read-key-sequence', it returns
10449 the last key sequence that has been read.
10450 The value is a string or a vector.
10452 See also `this-command-keys-vector'. */)
10453 (void)
10455 return make_event_array (this_command_key_count,
10456 XVECTOR (this_command_keys)->contents);
10459 DEFUN ("this-command-keys-vector", Fthis_command_keys_vector, Sthis_command_keys_vector, 0, 0, 0,
10460 doc: /* Return the key sequence that invoked this command, as a vector.
10461 However, if the command has called `read-key-sequence', it returns
10462 the last key sequence that has been read.
10464 See also `this-command-keys'. */)
10465 (void)
10467 return Fvector (this_command_key_count,
10468 XVECTOR (this_command_keys)->contents);
10471 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
10472 Sthis_single_command_keys, 0, 0, 0,
10473 doc: /* Return the key sequence that invoked this command.
10474 More generally, it returns the last key sequence read, either by
10475 the command loop or by `read-key-sequence'.
10476 Unlike `this-command-keys', this function's value
10477 does not include prefix arguments.
10478 The value is always a vector. */)
10479 (void)
10481 return Fvector (this_command_key_count
10482 - this_single_command_key_start,
10483 (XVECTOR (this_command_keys)->contents
10484 + this_single_command_key_start));
10487 DEFUN ("this-single-command-raw-keys", Fthis_single_command_raw_keys,
10488 Sthis_single_command_raw_keys, 0, 0, 0,
10489 doc: /* Return the raw events that were read for this command.
10490 More generally, it returns the last key sequence read, either by
10491 the command loop or by `read-key-sequence'.
10492 Unlike `this-single-command-keys', this function's value
10493 shows the events before all translations (except for input methods).
10494 The value is always a vector. */)
10495 (void)
10497 return Fvector (raw_keybuf_count,
10498 (XVECTOR (raw_keybuf)->contents));
10501 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
10502 Sreset_this_command_lengths, 0, 0, 0,
10503 doc: /* Make the unread events replace the last command and echo.
10504 Used in `universal-argument-other-key'.
10506 `universal-argument-other-key' rereads the event just typed.
10507 It then gets translated through `function-key-map'.
10508 The translated event has to replace the real events,
10509 both in the value of (this-command-keys) and in echoing.
10510 To achieve this, `universal-argument-other-key' calls
10511 `reset-this-command-lengths', which discards the record of reading
10512 these events the first time. */)
10513 (void)
10515 this_command_key_count = before_command_key_count;
10516 if (this_command_key_count < this_single_command_key_start)
10517 this_single_command_key_start = this_command_key_count;
10519 echo_truncate (before_command_echo_length);
10521 /* Cause whatever we put into unread-command-events
10522 to echo as if it were being freshly read from the keyboard. */
10523 this_command_key_count_reset = 1;
10525 return Qnil;
10528 DEFUN ("clear-this-command-keys", Fclear_this_command_keys,
10529 Sclear_this_command_keys, 0, 1, 0,
10530 doc: /* Clear out the vector that `this-command-keys' returns.
10531 Also clear the record of the last 100 events, unless optional arg
10532 KEEP-RECORD is non-nil. */)
10533 (Lisp_Object keep_record)
10535 int i;
10537 this_command_key_count = 0;
10538 this_command_key_count_reset = 0;
10540 if (NILP (keep_record))
10542 for (i = 0; i < XVECTOR (recent_keys)->size; ++i)
10543 XVECTOR (recent_keys)->contents[i] = Qnil;
10544 total_keys = 0;
10545 recent_keys_index = 0;
10547 return Qnil;
10550 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
10551 doc: /* Return the current depth in recursive edits. */)
10552 (void)
10554 Lisp_Object temp;
10555 XSETFASTINT (temp, command_loop_level + minibuf_level);
10556 return temp;
10559 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
10560 "FOpen dribble file: ",
10561 doc: /* Start writing all keyboard characters to a dribble file called FILE.
10562 If FILE is nil, close any open dribble file. */)
10563 (Lisp_Object file)
10565 if (dribble)
10567 BLOCK_INPUT;
10568 fclose (dribble);
10569 UNBLOCK_INPUT;
10570 dribble = 0;
10572 if (!NILP (file))
10574 file = Fexpand_file_name (file, Qnil);
10575 dribble = fopen (SSDATA (file), "w");
10576 if (dribble == 0)
10577 report_file_error ("Opening dribble", Fcons (file, Qnil));
10579 return Qnil;
10582 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
10583 doc: /* Discard the contents of the terminal input buffer.
10584 Also end any kbd macro being defined. */)
10585 (void)
10587 if (!NILP (KVAR (current_kboard, defining_kbd_macro)))
10589 /* Discard the last command from the macro. */
10590 Fcancel_kbd_macro_events ();
10591 end_kbd_macro ();
10594 update_mode_lines++;
10596 Vunread_command_events = Qnil;
10597 unread_command_char = -1;
10599 discard_tty_input ();
10601 kbd_fetch_ptr = kbd_store_ptr;
10602 input_pending = 0;
10604 return Qnil;
10607 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
10608 doc: /* Stop Emacs and return to superior process. You can resume later.
10609 If `cannot-suspend' is non-nil, or if the system doesn't support job
10610 control, run a subshell instead.
10612 If optional arg STUFFSTRING is non-nil, its characters are stuffed
10613 to be read as terminal input by Emacs's parent, after suspension.
10615 Before suspending, run the normal hook `suspend-hook'.
10616 After resumption run the normal hook `suspend-resume-hook'.
10618 Some operating systems cannot stop the Emacs process and resume it later.
10619 On such systems, Emacs starts a subshell instead of suspending. */)
10620 (Lisp_Object stuffstring)
10622 int count = SPECPDL_INDEX ();
10623 int old_height, old_width;
10624 int width, height;
10625 struct gcpro gcpro1;
10627 if (tty_list && tty_list->next)
10628 error ("There are other tty frames open; close them before suspending Emacs");
10630 if (!NILP (stuffstring))
10631 CHECK_STRING (stuffstring);
10633 /* Run the functions in suspend-hook. */
10634 if (!NILP (Vrun_hooks))
10635 call1 (Vrun_hooks, intern ("suspend-hook"));
10637 GCPRO1 (stuffstring);
10638 get_tty_size (fileno (CURTTY ()->input), &old_width, &old_height);
10639 reset_all_sys_modes ();
10640 /* sys_suspend can get an error if it tries to fork a subshell
10641 and the system resources aren't available for that. */
10642 record_unwind_protect ((Lisp_Object (*) (Lisp_Object)) init_all_sys_modes,
10643 Qnil);
10644 stuff_buffered_input (stuffstring);
10645 if (cannot_suspend)
10646 sys_subshell ();
10647 else
10648 sys_suspend ();
10649 unbind_to (count, Qnil);
10651 /* Check if terminal/window size has changed.
10652 Note that this is not useful when we are running directly
10653 with a window system; but suspend should be disabled in that case. */
10654 get_tty_size (fileno (CURTTY ()->input), &width, &height);
10655 if (width != old_width || height != old_height)
10656 change_frame_size (SELECTED_FRAME (), height, width, 0, 0, 0);
10658 /* Run suspend-resume-hook. */
10659 if (!NILP (Vrun_hooks))
10660 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
10662 UNGCPRO;
10663 return Qnil;
10666 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
10667 Then in any case stuff anything Emacs has read ahead and not used. */
10669 void
10670 stuff_buffered_input (Lisp_Object stuffstring)
10672 #ifdef SIGTSTP /* stuff_char is defined if SIGTSTP. */
10673 register unsigned char *p;
10675 if (STRINGP (stuffstring))
10677 register EMACS_INT count;
10679 p = SDATA (stuffstring);
10680 count = SBYTES (stuffstring);
10681 while (count-- > 0)
10682 stuff_char (*p++);
10683 stuff_char ('\n');
10686 /* Anything we have read ahead, put back for the shell to read. */
10687 /* ?? What should this do when we have multiple keyboards??
10688 Should we ignore anything that was typed in at the "wrong" kboard?
10690 rms: we should stuff everything back into the kboard
10691 it came from. */
10692 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
10695 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
10696 kbd_fetch_ptr = kbd_buffer;
10697 if (kbd_fetch_ptr->kind == ASCII_KEYSTROKE_EVENT)
10698 stuff_char (kbd_fetch_ptr->code);
10700 clear_event (kbd_fetch_ptr);
10703 input_pending = 0;
10704 #endif /* SIGTSTP */
10707 void
10708 set_waiting_for_input (struct timeval *time_to_clear)
10710 input_available_clear_time = time_to_clear;
10712 /* Tell handle_interrupt to throw back to read_char, */
10713 waiting_for_input = 1;
10715 /* If handle_interrupt was called before and buffered a C-g,
10716 make it run again now, to avoid timing error. */
10717 if (!NILP (Vquit_flag))
10718 quit_throw_to_read_char ();
10721 void
10722 clear_waiting_for_input (void)
10724 /* Tell handle_interrupt not to throw back to read_char, */
10725 waiting_for_input = 0;
10726 input_available_clear_time = 0;
10729 /* The SIGINT handler.
10731 If we have a frame on the controlling tty, we assume that the
10732 SIGINT was generated by C-g, so we call handle_interrupt.
10733 Otherwise, the handler kills Emacs. */
10735 static SIGTYPE
10736 interrupt_signal (int signalnum) /* If we don't have an argument, some */
10737 /* compilers complain in signal calls. */
10739 /* Must preserve main program's value of errno. */
10740 int old_errno = errno;
10741 struct terminal *terminal;
10743 SIGNAL_THREAD_CHECK (signalnum);
10745 /* See if we have an active terminal on our controlling tty. */
10746 terminal = get_named_tty ("/dev/tty");
10747 if (!terminal)
10749 /* If there are no frames there, let's pretend that we are a
10750 well-behaving UN*X program and quit. */
10751 Fkill_emacs (Qnil);
10753 else
10755 /* Otherwise, the SIGINT was probably generated by C-g. */
10757 /* Set internal_last_event_frame to the top frame of the
10758 controlling tty, if we have a frame there. We disable the
10759 interrupt key on secondary ttys, so the SIGINT must have come
10760 from the controlling tty. */
10761 internal_last_event_frame = terminal->display_info.tty->top_frame;
10763 handle_interrupt ();
10766 errno = old_errno;
10769 /* This routine is called at interrupt level in response to C-g.
10771 It is called from the SIGINT handler or kbd_buffer_store_event.
10773 If `waiting_for_input' is non zero, then unless `echoing' is
10774 nonzero, immediately throw back to read_char.
10776 Otherwise it sets the Lisp variable quit-flag not-nil. This causes
10777 eval to throw, when it gets a chance. If quit-flag is already
10778 non-nil, it stops the job right away. */
10780 static void
10781 handle_interrupt (void)
10783 char c;
10785 cancel_echoing ();
10787 /* XXX This code needs to be revised for multi-tty support. */
10788 if (!NILP (Vquit_flag) && get_named_tty ("/dev/tty"))
10790 /* If SIGINT isn't blocked, don't let us be interrupted by
10791 another SIGINT, it might be harmful due to non-reentrancy
10792 in I/O functions. */
10793 sigblock (sigmask (SIGINT));
10795 fflush (stdout);
10796 reset_all_sys_modes ();
10798 #ifdef SIGTSTP /* Support possible in later USG versions */
10800 * On systems which can suspend the current process and return to the original
10801 * shell, this command causes the user to end up back at the shell.
10802 * The "Auto-save" and "Abort" questions are not asked until
10803 * the user elects to return to emacs, at which point he can save the current
10804 * job and either dump core or continue.
10806 sys_suspend ();
10807 #else
10808 /* Perhaps should really fork an inferior shell?
10809 But that would not provide any way to get back
10810 to the original shell, ever. */
10811 printf ("No support for stopping a process on this operating system;\n");
10812 printf ("you can continue or abort.\n");
10813 #endif /* not SIGTSTP */
10814 #ifdef MSDOS
10815 /* We must remain inside the screen area when the internal terminal
10816 is used. Note that [Enter] is not echoed by dos. */
10817 cursor_to (SELECTED_FRAME (), 0, 0);
10818 #endif
10819 /* It doesn't work to autosave while GC is in progress;
10820 the code used for auto-saving doesn't cope with the mark bit. */
10821 if (!gc_in_progress)
10823 printf ("Auto-save? (y or n) ");
10824 fflush (stdout);
10825 if (((c = getchar ()) & ~040) == 'Y')
10827 Fdo_auto_save (Qt, Qnil);
10828 #ifdef MSDOS
10829 printf ("\r\nAuto-save done");
10830 #else /* not MSDOS */
10831 printf ("Auto-save done\n");
10832 #endif /* not MSDOS */
10834 while (c != '\n') c = getchar ();
10836 else
10838 /* During GC, it must be safe to reenable quitting again. */
10839 Vinhibit_quit = Qnil;
10840 #ifdef MSDOS
10841 printf ("\r\n");
10842 #endif /* not MSDOS */
10843 printf ("Garbage collection in progress; cannot auto-save now\r\n");
10844 printf ("but will instead do a real quit after garbage collection ends\r\n");
10845 fflush (stdout);
10848 #ifdef MSDOS
10849 printf ("\r\nAbort? (y or n) ");
10850 #else /* not MSDOS */
10851 printf ("Abort (and dump core)? (y or n) ");
10852 #endif /* not MSDOS */
10853 fflush (stdout);
10854 if (((c = getchar ()) & ~040) == 'Y')
10855 abort ();
10856 while (c != '\n') c = getchar ();
10857 #ifdef MSDOS
10858 printf ("\r\nContinuing...\r\n");
10859 #else /* not MSDOS */
10860 printf ("Continuing...\n");
10861 #endif /* not MSDOS */
10862 fflush (stdout);
10863 init_all_sys_modes ();
10864 sigfree ();
10866 else
10868 /* If executing a function that wants to be interrupted out of
10869 and the user has not deferred quitting by binding `inhibit-quit'
10870 then quit right away. */
10871 if (immediate_quit && NILP (Vinhibit_quit))
10873 struct gl_state_s saved;
10874 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
10876 immediate_quit = 0;
10877 sigfree ();
10878 saved = gl_state;
10879 GCPRO4 (saved.object, saved.global_code,
10880 saved.current_syntax_table, saved.old_prop);
10881 Fsignal (Qquit, Qnil);
10882 /* FIXME: AFAIK, `quit' can never return, so this code is dead! */
10883 gl_state = saved;
10884 UNGCPRO;
10886 else
10887 /* Else request quit when it's safe */
10888 Vquit_flag = Qt;
10891 /* TODO: The longjmp in this call throws the NS event loop integration off,
10892 and it seems to do fine without this. Probably some attention
10893 needs to be paid to the setting of waiting_for_input in
10894 wait_reading_process_output() under HAVE_NS because of the call
10895 to ns_select there (needed because otherwise events aren't picked up
10896 outside of polling since we don't get SIGIO like X and we don't have a
10897 separate event loop thread like W32. */
10898 #ifndef HAVE_NS
10899 if (waiting_for_input && !echoing)
10900 quit_throw_to_read_char ();
10901 #endif
10904 /* Handle a C-g by making read_char return C-g. */
10906 void
10907 quit_throw_to_read_char (void)
10909 sigfree ();
10910 /* Prevent another signal from doing this before we finish. */
10911 clear_waiting_for_input ();
10912 input_pending = 0;
10914 Vunread_command_events = Qnil;
10915 unread_command_char = -1;
10917 #if 0 /* Currently, sit_for is called from read_char without turning
10918 off polling. And that can call set_waiting_for_input.
10919 It seems to be harmless. */
10920 #ifdef POLL_FOR_INPUT
10921 /* May be > 1 if in recursive minibuffer. */
10922 if (poll_suppress_count == 0)
10923 abort ();
10924 #endif
10925 #endif
10926 if (FRAMEP (internal_last_event_frame)
10927 && !EQ (internal_last_event_frame, selected_frame))
10928 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
10929 0, 0, Qnil);
10931 _longjmp (getcjmp, 1);
10934 DEFUN ("set-input-interrupt-mode", Fset_input_interrupt_mode, Sset_input_interrupt_mode, 1, 1, 0,
10935 doc: /* Set interrupt mode of reading keyboard input.
10936 If INTERRUPT is non-nil, Emacs will use input interrupts;
10937 otherwise Emacs uses CBREAK mode.
10939 See also `current-input-mode'. */)
10940 (Lisp_Object interrupt)
10942 int new_interrupt_input;
10943 #ifdef SIGIO
10944 /* Note SIGIO has been undef'd if FIONREAD is missing. */
10945 #ifdef HAVE_X_WINDOWS
10946 if (x_display_list != NULL)
10948 /* When using X, don't give the user a real choice,
10949 because we haven't implemented the mechanisms to support it. */
10950 new_interrupt_input = 1;
10952 else
10953 #endif /* HAVE_X_WINDOWS */
10954 new_interrupt_input = !NILP (interrupt);
10955 #else /* not SIGIO */
10956 new_interrupt_input = 0;
10957 #endif /* not SIGIO */
10959 if (new_interrupt_input != interrupt_input)
10961 #ifdef POLL_FOR_INPUT
10962 stop_polling ();
10963 #endif
10964 #ifndef DOS_NT
10965 /* this causes startup screen to be restored and messes with the mouse */
10966 reset_all_sys_modes ();
10967 interrupt_input = new_interrupt_input;
10968 init_all_sys_modes ();
10969 #else
10970 interrupt_input = new_interrupt_input;
10971 #endif
10973 #ifdef POLL_FOR_INPUT
10974 poll_suppress_count = 1;
10975 start_polling ();
10976 #endif
10978 return Qnil;
10981 DEFUN ("set-output-flow-control", Fset_output_flow_control, Sset_output_flow_control, 1, 2, 0,
10982 doc: /* Enable or disable ^S/^Q flow control for output to TERMINAL.
10983 If FLOW is non-nil, flow control is enabled and you cannot use C-s or
10984 C-q in key sequences.
10986 This setting only has an effect on tty terminals and only when
10987 Emacs reads input in CBREAK mode; see `set-input-interrupt-mode'.
10989 See also `current-input-mode'. */)
10990 (Lisp_Object flow, Lisp_Object terminal)
10992 struct terminal *t = get_terminal (terminal, 1);
10993 struct tty_display_info *tty;
10994 if (t == NULL || (t->type != output_termcap && t->type != output_msdos_raw))
10995 return Qnil;
10996 tty = t->display_info.tty;
10998 if (tty->flow_control != !NILP (flow))
11000 #ifndef DOS_NT
11001 /* this causes startup screen to be restored and messes with the mouse */
11002 reset_sys_modes (tty);
11003 #endif
11005 tty->flow_control = !NILP (flow);
11007 #ifndef DOS_NT
11008 init_sys_modes (tty);
11009 #endif
11011 return Qnil;
11014 DEFUN ("set-input-meta-mode", Fset_input_meta_mode, Sset_input_meta_mode, 1, 2, 0,
11015 doc: /* Enable or disable 8-bit input on TERMINAL.
11016 If META is t, Emacs will accept 8-bit input, and interpret the 8th
11017 bit as the Meta modifier.
11019 If META is nil, Emacs will ignore the top bit, on the assumption it is
11020 parity.
11022 Otherwise, Emacs will accept and pass through 8-bit input without
11023 specially interpreting the top bit.
11025 This setting only has an effect on tty terminal devices.
11027 Optional parameter TERMINAL specifies the tty terminal device to use.
11028 It may be a terminal object, a frame, or nil for the terminal used by
11029 the currently selected frame.
11031 See also `current-input-mode'. */)
11032 (Lisp_Object meta, Lisp_Object terminal)
11034 struct terminal *t = get_terminal (terminal, 1);
11035 struct tty_display_info *tty;
11036 int new_meta;
11038 if (t == NULL || (t->type != output_termcap && t->type != output_msdos_raw))
11039 return Qnil;
11040 tty = t->display_info.tty;
11042 if (NILP (meta))
11043 new_meta = 0;
11044 else if (EQ (meta, Qt))
11045 new_meta = 1;
11046 else
11047 new_meta = 2;
11049 if (tty->meta_key != new_meta)
11051 #ifndef DOS_NT
11052 /* this causes startup screen to be restored and messes with the mouse */
11053 reset_sys_modes (tty);
11054 #endif
11056 tty->meta_key = new_meta;
11058 #ifndef DOS_NT
11059 init_sys_modes (tty);
11060 #endif
11062 return Qnil;
11065 DEFUN ("set-quit-char", Fset_quit_char, Sset_quit_char, 1, 1, 0,
11066 doc: /* Specify character used for quitting.
11067 QUIT must be an ASCII character.
11069 This function only has an effect on the controlling tty of the Emacs
11070 process.
11072 See also `current-input-mode'. */)
11073 (Lisp_Object quit)
11075 struct terminal *t = get_named_tty ("/dev/tty");
11076 struct tty_display_info *tty;
11077 if (t == NULL || (t->type != output_termcap && t->type != output_msdos_raw))
11078 return Qnil;
11079 tty = t->display_info.tty;
11081 if (NILP (quit) || !INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400)
11082 error ("QUIT must be an ASCII character");
11084 #ifndef DOS_NT
11085 /* this causes startup screen to be restored and messes with the mouse */
11086 reset_sys_modes (tty);
11087 #endif
11089 /* Don't let this value be out of range. */
11090 quit_char = XINT (quit) & (tty->meta_key == 0 ? 0177 : 0377);
11092 #ifndef DOS_NT
11093 init_sys_modes (tty);
11094 #endif
11096 return Qnil;
11099 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
11100 doc: /* Set mode of reading keyboard input.
11101 First arg INTERRUPT non-nil means use input interrupts;
11102 nil means use CBREAK mode.
11103 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal
11104 (no effect except in CBREAK mode).
11105 Third arg META t means accept 8-bit input (for a Meta key).
11106 META nil means ignore the top bit, on the assumption it is parity.
11107 Otherwise, accept 8-bit input and don't use the top bit for Meta.
11108 Optional fourth arg QUIT if non-nil specifies character to use for quitting.
11109 See also `current-input-mode'. */)
11110 (Lisp_Object interrupt, Lisp_Object flow, Lisp_Object meta, Lisp_Object quit)
11112 Fset_input_interrupt_mode (interrupt);
11113 Fset_output_flow_control (flow, Qnil);
11114 Fset_input_meta_mode (meta, Qnil);
11115 if (!NILP (quit))
11116 Fset_quit_char (quit);
11117 return Qnil;
11120 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
11121 doc: /* Return information about the way Emacs currently reads keyboard input.
11122 The value is a list of the form (INTERRUPT FLOW META QUIT), where
11123 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if
11124 nil, Emacs is using CBREAK mode.
11125 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the
11126 terminal; this does not apply if Emacs uses interrupt-driven input.
11127 META is t if accepting 8-bit input with 8th bit as Meta flag.
11128 META nil means ignoring the top bit, on the assumption it is parity.
11129 META is neither t nor nil if accepting 8-bit input and using
11130 all 8 bits as the character code.
11131 QUIT is the character Emacs currently uses to quit.
11132 The elements of this list correspond to the arguments of
11133 `set-input-mode'. */)
11134 (void)
11136 Lisp_Object val[4];
11137 struct frame *sf = XFRAME (selected_frame);
11139 val[0] = interrupt_input ? Qt : Qnil;
11140 if (FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11142 val[1] = FRAME_TTY (sf)->flow_control ? Qt : Qnil;
11143 val[2] = (FRAME_TTY (sf)->meta_key == 2
11144 ? make_number (0)
11145 : (CURTTY ()->meta_key == 1 ? Qt : Qnil));
11147 else
11149 val[1] = Qnil;
11150 val[2] = Qt;
11152 XSETFASTINT (val[3], quit_char);
11154 return Flist (sizeof (val) / sizeof (val[0]), val);
11157 DEFUN ("posn-at-x-y", Fposn_at_x_y, Sposn_at_x_y, 2, 4, 0,
11158 doc: /* Return position information for pixel coordinates X and Y.
11159 By default, X and Y are relative to text area of the selected window.
11160 Optional third arg FRAME-OR-WINDOW non-nil specifies frame or window.
11161 If optional fourth arg WHOLE is non-nil, X is relative to the left
11162 edge of the window.
11164 The return value is similar to a mouse click position:
11165 (WINDOW AREA-OR-POS (X . Y) TIMESTAMP OBJECT POS (COL . ROW)
11166 IMAGE (DX . DY) (WIDTH . HEIGHT))
11167 The `posn-' functions access elements of such lists. */)
11168 (Lisp_Object x, Lisp_Object y, Lisp_Object frame_or_window, Lisp_Object whole)
11170 CHECK_NATNUM (x);
11171 CHECK_NATNUM (y);
11173 if (NILP (frame_or_window))
11174 frame_or_window = selected_window;
11176 if (WINDOWP (frame_or_window))
11178 struct window *w;
11180 CHECK_LIVE_WINDOW (frame_or_window);
11182 w = XWINDOW (frame_or_window);
11183 XSETINT (x, (XINT (x)
11184 + WINDOW_LEFT_EDGE_X (w)
11185 + (NILP (whole)
11186 ? window_box_left_offset (w, TEXT_AREA)
11187 : 0)));
11188 XSETINT (y, WINDOW_TO_FRAME_PIXEL_Y (w, XINT (y)));
11189 frame_or_window = w->frame;
11192 CHECK_LIVE_FRAME (frame_or_window);
11194 return make_lispy_position (XFRAME (frame_or_window), x, y, 0);
11197 DEFUN ("posn-at-point", Fposn_at_point, Sposn_at_point, 0, 2, 0,
11198 doc: /* Return position information for buffer POS in WINDOW.
11199 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
11201 Return nil if position is not visible in window. Otherwise,
11202 the return value is similar to that returned by `event-start' for
11203 a mouse click at the upper left corner of the glyph corresponding
11204 to the given buffer position:
11205 (WINDOW AREA-OR-POS (X . Y) TIMESTAMP OBJECT POS (COL . ROW)
11206 IMAGE (DX . DY) (WIDTH . HEIGHT))
11207 The `posn-' functions access elements of such lists. */)
11208 (Lisp_Object pos, Lisp_Object window)
11210 Lisp_Object tem;
11212 if (NILP (window))
11213 window = selected_window;
11215 tem = Fpos_visible_in_window_p (pos, window, Qt);
11216 if (!NILP (tem))
11218 Lisp_Object x = XCAR (tem);
11219 Lisp_Object y = XCAR (XCDR (tem));
11221 /* Point invisible due to hscrolling? */
11222 if (XINT (x) < 0)
11223 return Qnil;
11224 tem = Fposn_at_x_y (x, y, window, Qnil);
11227 return tem;
11232 * Set up a new kboard object with reasonable initial values.
11234 void
11235 init_kboard (KBOARD *kb)
11237 KVAR (kb, Voverriding_terminal_local_map) = Qnil;
11238 KVAR (kb, Vlast_command) = Qnil;
11239 KVAR (kb, Vreal_last_command) = Qnil;
11240 KVAR (kb, Vkeyboard_translate_table) = Qnil;
11241 KVAR (kb, Vlast_repeatable_command) = Qnil;
11242 KVAR (kb, Vprefix_arg) = Qnil;
11243 KVAR (kb, Vlast_prefix_arg) = Qnil;
11244 KVAR (kb, kbd_queue) = Qnil;
11245 kb->kbd_queue_has_data = 0;
11246 kb->immediate_echo = 0;
11247 KVAR (kb, echo_string) = Qnil;
11248 kb->echo_after_prompt = -1;
11249 kb->kbd_macro_buffer = 0;
11250 kb->kbd_macro_bufsize = 0;
11251 KVAR (kb, defining_kbd_macro) = Qnil;
11252 KVAR (kb, Vlast_kbd_macro) = Qnil;
11253 kb->reference_count = 0;
11254 KVAR (kb, Vsystem_key_alist) = Qnil;
11255 KVAR (kb, system_key_syms) = Qnil;
11256 KVAR (kb, Vwindow_system) = Qt; /* Unset. */
11257 KVAR (kb, Vinput_decode_map) = Fmake_sparse_keymap (Qnil);
11258 KVAR (kb, Vlocal_function_key_map) = Fmake_sparse_keymap (Qnil);
11259 Fset_keymap_parent (KVAR (kb, Vlocal_function_key_map), Vfunction_key_map);
11260 KVAR (kb, Vdefault_minibuffer_frame) = Qnil;
11264 * Destroy the contents of a kboard object, but not the object itself.
11265 * We use this just before deleting it, or if we're going to initialize
11266 * it a second time.
11268 static void
11269 wipe_kboard (KBOARD *kb)
11271 xfree (kb->kbd_macro_buffer);
11274 /* Free KB and memory referenced from it. */
11276 void
11277 delete_kboard (KBOARD *kb)
11279 KBOARD **kbp;
11281 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
11282 if (*kbp == NULL)
11283 abort ();
11284 *kbp = kb->next_kboard;
11286 /* Prevent a dangling reference to KB. */
11287 if (kb == current_kboard
11288 && FRAMEP (selected_frame)
11289 && FRAME_LIVE_P (XFRAME (selected_frame)))
11291 current_kboard = FRAME_KBOARD (XFRAME (selected_frame));
11292 single_kboard = 0;
11293 if (current_kboard == kb)
11294 abort ();
11297 wipe_kboard (kb);
11298 xfree (kb);
11301 void
11302 init_keyboard (void)
11304 /* This is correct before outermost invocation of the editor loop */
11305 command_loop_level = -1;
11306 immediate_quit = 0;
11307 quit_char = Ctl ('g');
11308 Vunread_command_events = Qnil;
11309 unread_command_char = -1;
11310 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
11311 total_keys = 0;
11312 recent_keys_index = 0;
11313 kbd_fetch_ptr = kbd_buffer;
11314 kbd_store_ptr = kbd_buffer;
11315 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
11316 do_mouse_tracking = Qnil;
11317 #endif
11318 input_pending = 0;
11319 interrupt_input_blocked = 0;
11320 interrupt_input_pending = 0;
11321 #ifdef SYNC_INPUT
11322 pending_signals = 0;
11323 #endif
11325 /* This means that command_loop_1 won't try to select anything the first
11326 time through. */
11327 internal_last_event_frame = Qnil;
11328 Vlast_event_frame = internal_last_event_frame;
11330 current_kboard = initial_kboard;
11331 /* Re-initialize the keyboard again. */
11332 wipe_kboard (current_kboard);
11333 init_kboard (current_kboard);
11334 /* A value of nil for Vwindow_system normally means a tty, but we also use
11335 it for the initial terminal since there is no window system there. */
11336 KVAR (current_kboard, Vwindow_system) = Qnil;
11338 if (!noninteractive)
11340 /* Before multi-tty support, these handlers used to be installed
11341 only if the current session was a tty session. Now an Emacs
11342 session may have multiple display types, so we always handle
11343 SIGINT. There is special code in interrupt_signal to exit
11344 Emacs on SIGINT when there are no termcap frames on the
11345 controlling terminal. */
11346 signal (SIGINT, interrupt_signal);
11347 #ifndef DOS_NT
11348 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
11349 SIGQUIT and we can't tell which one it will give us. */
11350 signal (SIGQUIT, interrupt_signal);
11351 #endif /* not DOS_NT */
11353 /* Note SIGIO has been undef'd if FIONREAD is missing. */
11354 #ifdef SIGIO
11355 if (!noninteractive)
11356 signal (SIGIO, input_available_signal);
11357 #endif /* SIGIO */
11359 /* Use interrupt input by default, if it works and noninterrupt input
11360 has deficiencies. */
11362 #ifdef INTERRUPT_INPUT
11363 interrupt_input = 1;
11364 #else
11365 interrupt_input = 0;
11366 #endif
11368 sigfree ();
11369 dribble = 0;
11371 if (keyboard_init_hook)
11372 (*keyboard_init_hook) ();
11374 #ifdef POLL_FOR_INPUT
11375 poll_timer = NULL;
11376 poll_suppress_count = 1;
11377 start_polling ();
11378 #endif
11381 /* This type's only use is in syms_of_keyboard, to initialize the
11382 event header symbols and put properties on them. */
11383 struct event_head {
11384 Lisp_Object *var;
11385 const char *name;
11386 Lisp_Object *kind;
11389 static const struct event_head head_table[] = {
11390 {&Qmouse_movement, "mouse-movement", &Qmouse_movement},
11391 {&Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement},
11392 {&Qswitch_frame, "switch-frame", &Qswitch_frame},
11393 {&Qdelete_frame, "delete-frame", &Qdelete_frame},
11394 {&Qiconify_frame, "iconify-frame", &Qiconify_frame},
11395 {&Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible},
11396 /* `select-window' should be handled just like `switch-frame'
11397 in read_key_sequence. */
11398 {&Qselect_window, "select-window", &Qswitch_frame}
11401 void
11402 syms_of_keyboard (void)
11404 pending_funcalls = Qnil;
11405 staticpro (&pending_funcalls);
11407 Vlispy_mouse_stem = make_pure_c_string ("mouse");
11408 staticpro (&Vlispy_mouse_stem);
11410 /* Tool-bars. */
11411 QCimage = intern_c_string (":image");
11412 staticpro (&QCimage);
11414 staticpro (&Qhelp_echo);
11415 Qhelp_echo = intern_c_string ("help-echo");
11417 staticpro (&Qrtl);
11418 Qrtl = intern_c_string (":rtl");
11420 staticpro (&item_properties);
11421 item_properties = Qnil;
11423 staticpro (&tool_bar_item_properties);
11424 tool_bar_item_properties = Qnil;
11425 staticpro (&tool_bar_items_vector);
11426 tool_bar_items_vector = Qnil;
11428 staticpro (&real_this_command);
11429 real_this_command = Qnil;
11431 Qtimer_event_handler = intern_c_string ("timer-event-handler");
11432 staticpro (&Qtimer_event_handler);
11434 Qdisabled_command_function = intern_c_string ("disabled-command-function");
11435 staticpro (&Qdisabled_command_function);
11437 Qself_insert_command = intern_c_string ("self-insert-command");
11438 staticpro (&Qself_insert_command);
11440 Qforward_char = intern_c_string ("forward-char");
11441 staticpro (&Qforward_char);
11443 Qbackward_char = intern_c_string ("backward-char");
11444 staticpro (&Qbackward_char);
11446 Qdisabled = intern_c_string ("disabled");
11447 staticpro (&Qdisabled);
11449 Qundefined = intern_c_string ("undefined");
11450 staticpro (&Qundefined);
11452 Qpre_command_hook = intern_c_string ("pre-command-hook");
11453 staticpro (&Qpre_command_hook);
11455 Qpost_command_hook = intern_c_string ("post-command-hook");
11456 staticpro (&Qpost_command_hook);
11458 Qdeferred_action_function = intern_c_string ("deferred-action-function");
11459 staticpro (&Qdeferred_action_function);
11461 Qcommand_hook_internal = intern_c_string ("command-hook-internal");
11462 staticpro (&Qcommand_hook_internal);
11464 Qfunction_key = intern_c_string ("function-key");
11465 staticpro (&Qfunction_key);
11466 Qmouse_click = intern_c_string ("mouse-click");
11467 staticpro (&Qmouse_click);
11468 #if defined (WINDOWSNT)
11469 Qlanguage_change = intern_c_string ("language-change");
11470 staticpro (&Qlanguage_change);
11471 #endif
11472 Qdrag_n_drop = intern_c_string ("drag-n-drop");
11473 staticpro (&Qdrag_n_drop);
11475 Qsave_session = intern_c_string ("save-session");
11476 staticpro (&Qsave_session);
11478 #ifdef HAVE_DBUS
11479 Qdbus_event = intern_c_string ("dbus-event");
11480 staticpro (&Qdbus_event);
11481 #endif
11483 Qconfig_changed_event = intern_c_string ("config-changed-event");
11484 staticpro (&Qconfig_changed_event);
11486 Qmenu_enable = intern_c_string ("menu-enable");
11487 staticpro (&Qmenu_enable);
11488 QCenable = intern_c_string (":enable");
11489 staticpro (&QCenable);
11490 QCvisible = intern_c_string (":visible");
11491 staticpro (&QCvisible);
11492 QChelp = intern_c_string (":help");
11493 staticpro (&QChelp);
11494 QCfilter = intern_c_string (":filter");
11495 staticpro (&QCfilter);
11496 QCbutton = intern_c_string (":button");
11497 staticpro (&QCbutton);
11498 QCkeys = intern_c_string (":keys");
11499 staticpro (&QCkeys);
11500 QCkey_sequence = intern_c_string (":key-sequence");
11501 staticpro (&QCkey_sequence);
11502 QCtoggle = intern_c_string (":toggle");
11503 staticpro (&QCtoggle);
11504 QCradio = intern_c_string (":radio");
11505 staticpro (&QCradio);
11506 QClabel = intern_c_string (":label");
11507 staticpro (&QClabel);
11508 QCvert_only = intern_c_string (":vert-only");
11509 staticpro (&QCvert_only);
11511 Qmode_line = intern_c_string ("mode-line");
11512 staticpro (&Qmode_line);
11513 Qvertical_line = intern_c_string ("vertical-line");
11514 staticpro (&Qvertical_line);
11515 Qvertical_scroll_bar = intern_c_string ("vertical-scroll-bar");
11516 staticpro (&Qvertical_scroll_bar);
11517 Qmenu_bar = intern_c_string ("menu-bar");
11518 staticpro (&Qmenu_bar);
11520 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
11521 Qmouse_fixup_help_message = intern_c_string ("mouse-fixup-help-message");
11522 staticpro (&Qmouse_fixup_help_message);
11523 #endif
11525 Qabove_handle = intern_c_string ("above-handle");
11526 staticpro (&Qabove_handle);
11527 Qhandle = intern_c_string ("handle");
11528 staticpro (&Qhandle);
11529 Qbelow_handle = intern_c_string ("below-handle");
11530 staticpro (&Qbelow_handle);
11531 Qup = intern_c_string ("up");
11532 staticpro (&Qup);
11533 Qdown = intern_c_string ("down");
11534 staticpro (&Qdown);
11535 Qtop = intern_c_string ("top");
11536 staticpro (&Qtop);
11537 Qbottom = intern_c_string ("bottom");
11538 staticpro (&Qbottom);
11539 Qend_scroll = intern_c_string ("end-scroll");
11540 staticpro (&Qend_scroll);
11541 Qratio = intern_c_string ("ratio");
11542 staticpro (&Qratio);
11544 Qevent_kind = intern_c_string ("event-kind");
11545 staticpro (&Qevent_kind);
11546 Qevent_symbol_elements = intern_c_string ("event-symbol-elements");
11547 staticpro (&Qevent_symbol_elements);
11548 Qevent_symbol_element_mask = intern_c_string ("event-symbol-element-mask");
11549 staticpro (&Qevent_symbol_element_mask);
11550 Qmodifier_cache = intern_c_string ("modifier-cache");
11551 staticpro (&Qmodifier_cache);
11553 Qrecompute_lucid_menubar = intern_c_string ("recompute-lucid-menubar");
11554 staticpro (&Qrecompute_lucid_menubar);
11555 Qactivate_menubar_hook = intern_c_string ("activate-menubar-hook");
11556 staticpro (&Qactivate_menubar_hook);
11558 Qpolling_period = intern_c_string ("polling-period");
11559 staticpro (&Qpolling_period);
11561 Qinput_method_function = intern_c_string ("input-method-function");
11562 staticpro (&Qinput_method_function);
11564 Qx_set_selection = intern_c_string ("x-set-selection");
11565 staticpro (&Qx_set_selection);
11566 QPRIMARY = intern_c_string ("PRIMARY");
11567 staticpro (&QPRIMARY);
11568 Qhandle_switch_frame = intern_c_string ("handle-switch-frame");
11569 staticpro (&Qhandle_switch_frame);
11571 Qinput_method_exit_on_first_char = intern_c_string ("input-method-exit-on-first-char");
11572 staticpro (&Qinput_method_exit_on_first_char);
11573 Qinput_method_use_echo_area = intern_c_string ("input-method-use-echo-area");
11574 staticpro (&Qinput_method_use_echo_area);
11576 Fset (Qinput_method_exit_on_first_char, Qnil);
11577 Fset (Qinput_method_use_echo_area, Qnil);
11579 last_point_position_buffer = Qnil;
11580 last_point_position_window = Qnil;
11583 const struct event_head *p;
11585 for (p = head_table;
11586 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
11587 p++)
11589 *p->var = intern_c_string (p->name);
11590 staticpro (p->var);
11591 Fput (*p->var, Qevent_kind, *p->kind);
11592 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
11596 button_down_location = Fmake_vector (make_number (5), Qnil);
11597 staticpro (&button_down_location);
11598 mouse_syms = Fmake_vector (make_number (5), Qnil);
11599 staticpro (&mouse_syms);
11600 wheel_syms = Fmake_vector (make_number (sizeof (lispy_wheel_names)
11601 / sizeof (lispy_wheel_names[0])),
11602 Qnil);
11603 staticpro (&wheel_syms);
11606 int i;
11607 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
11609 modifier_symbols = Fmake_vector (make_number (len), Qnil);
11610 for (i = 0; i < len; i++)
11611 if (modifier_names[i])
11612 XVECTOR (modifier_symbols)->contents[i] = intern_c_string (modifier_names[i]);
11613 staticpro (&modifier_symbols);
11616 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
11617 staticpro (&recent_keys);
11619 this_command_keys = Fmake_vector (make_number (40), Qnil);
11620 staticpro (&this_command_keys);
11622 raw_keybuf = Fmake_vector (make_number (30), Qnil);
11623 staticpro (&raw_keybuf);
11625 Qextended_command_history = intern_c_string ("extended-command-history");
11626 Fset (Qextended_command_history, Qnil);
11627 staticpro (&Qextended_command_history);
11629 accent_key_syms = Qnil;
11630 staticpro (&accent_key_syms);
11632 func_key_syms = Qnil;
11633 staticpro (&func_key_syms);
11635 drag_n_drop_syms = Qnil;
11636 staticpro (&drag_n_drop_syms);
11638 unread_switch_frame = Qnil;
11639 staticpro (&unread_switch_frame);
11641 internal_last_event_frame = Qnil;
11642 staticpro (&internal_last_event_frame);
11644 read_key_sequence_cmd = Qnil;
11645 staticpro (&read_key_sequence_cmd);
11647 menu_bar_one_keymap_changed_items = Qnil;
11648 staticpro (&menu_bar_one_keymap_changed_items);
11650 menu_bar_items_vector = Qnil;
11651 staticpro (&menu_bar_items_vector);
11653 help_form_saved_window_configs = Qnil;
11654 staticpro (&help_form_saved_window_configs);
11656 defsubr (&Scurrent_idle_time);
11657 defsubr (&Sevent_symbol_parse_modifiers);
11658 defsubr (&Sevent_convert_list);
11659 defsubr (&Sread_key_sequence);
11660 defsubr (&Sread_key_sequence_vector);
11661 defsubr (&Srecursive_edit);
11662 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
11663 defsubr (&Strack_mouse);
11664 #endif
11665 defsubr (&Sinput_pending_p);
11666 defsubr (&Scommand_execute);
11667 defsubr (&Srecent_keys);
11668 defsubr (&Sthis_command_keys);
11669 defsubr (&Sthis_command_keys_vector);
11670 defsubr (&Sthis_single_command_keys);
11671 defsubr (&Sthis_single_command_raw_keys);
11672 defsubr (&Sreset_this_command_lengths);
11673 defsubr (&Sclear_this_command_keys);
11674 defsubr (&Ssuspend_emacs);
11675 defsubr (&Sabort_recursive_edit);
11676 defsubr (&Sexit_recursive_edit);
11677 defsubr (&Srecursion_depth);
11678 defsubr (&Stop_level);
11679 defsubr (&Sdiscard_input);
11680 defsubr (&Sopen_dribble_file);
11681 defsubr (&Sset_input_interrupt_mode);
11682 defsubr (&Sset_output_flow_control);
11683 defsubr (&Sset_input_meta_mode);
11684 defsubr (&Sset_quit_char);
11685 defsubr (&Sset_input_mode);
11686 defsubr (&Scurrent_input_mode);
11687 defsubr (&Sexecute_extended_command);
11688 defsubr (&Sposn_at_point);
11689 defsubr (&Sposn_at_x_y);
11691 DEFVAR_LISP ("last-command-event", last_command_event,
11692 doc: /* Last input event that was part of a command. */);
11694 DEFVAR_LISP ("last-nonmenu-event", last_nonmenu_event,
11695 doc: /* Last input event in a command, except for mouse menu events.
11696 Mouse menus give back keys that don't look like mouse events;
11697 this variable holds the actual mouse event that led to the menu,
11698 so that you can determine whether the command was run by mouse or not. */);
11700 DEFVAR_LISP ("last-input-event", last_input_event,
11701 doc: /* Last input event. */);
11703 DEFVAR_LISP ("unread-command-events", Vunread_command_events,
11704 doc: /* List of events to be read as the command input.
11705 These events are processed first, before actual keyboard input.
11706 Events read from this list are not normally added to `this-command-keys',
11707 as they will already have been added once as they were read for the first time.
11708 An element of the form (t . EVENT) forces EVENT to be added to that list. */);
11709 Vunread_command_events = Qnil;
11711 DEFVAR_INT ("unread-command-char", unread_command_char,
11712 doc: /* If not -1, an object to be read as next command input event. */);
11714 DEFVAR_LISP ("unread-post-input-method-events", Vunread_post_input_method_events,
11715 doc: /* List of events to be processed as input by input methods.
11716 These events are processed before `unread-command-events'
11717 and actual keyboard input, but are not given to `input-method-function'. */);
11718 Vunread_post_input_method_events = Qnil;
11720 DEFVAR_LISP ("unread-input-method-events", Vunread_input_method_events,
11721 doc: /* List of events to be processed as input by input methods.
11722 These events are processed after `unread-command-events', but
11723 before actual keyboard input.
11724 If there's an active input method, the events are given to
11725 `input-method-function'. */);
11726 Vunread_input_method_events = Qnil;
11728 DEFVAR_LISP ("meta-prefix-char", meta_prefix_char,
11729 doc: /* Meta-prefix character code.
11730 Meta-foo as command input turns into this character followed by foo. */);
11731 XSETINT (meta_prefix_char, 033);
11733 DEFVAR_KBOARD ("last-command", Vlast_command,
11734 doc: /* The last command executed.
11735 Normally a symbol with a function definition, but can be whatever was found
11736 in the keymap, or whatever the variable `this-command' was set to by that
11737 command.
11739 The value `mode-exit' is special; it means that the previous command
11740 read an event that told it to exit, and it did so and unread that event.
11741 In other words, the present command is the event that made the previous
11742 command exit.
11744 The value `kill-region' is special; it means that the previous command
11745 was a kill command.
11747 `last-command' has a separate binding for each terminal device.
11748 See Info node `(elisp)Multiple Terminals'. */);
11750 DEFVAR_KBOARD ("real-last-command", Vreal_last_command,
11751 doc: /* Same as `last-command', but never altered by Lisp code. */);
11753 DEFVAR_KBOARD ("last-repeatable-command", Vlast_repeatable_command,
11754 doc: /* Last command that may be repeated.
11755 The last command executed that was not bound to an input event.
11756 This is the command `repeat' will try to repeat. */);
11758 DEFVAR_LISP ("this-command", Vthis_command,
11759 doc: /* The command now being executed.
11760 The command can set this variable; whatever is put here
11761 will be in `last-command' during the following command. */);
11762 Vthis_command = Qnil;
11764 DEFVAR_LISP ("this-command-keys-shift-translated",
11765 Vthis_command_keys_shift_translated,
11766 doc: /* Non-nil if the key sequence activating this command was shift-translated.
11767 Shift-translation occurs when there is no binding for the key sequence
11768 as entered, but a binding was found by changing an upper-case letter
11769 to lower-case, or a shifted function key to an unshifted one. */);
11770 Vthis_command_keys_shift_translated = Qnil;
11772 DEFVAR_LISP ("this-original-command", Vthis_original_command,
11773 doc: /* The command bound to the current key sequence before remapping.
11774 It equals `this-command' if the original command was not remapped through
11775 any of the active keymaps. Otherwise, the value of `this-command' is the
11776 result of looking up the original command in the active keymaps. */);
11777 Vthis_original_command = Qnil;
11779 DEFVAR_INT ("auto-save-interval", auto_save_interval,
11780 doc: /* *Number of input events between auto-saves.
11781 Zero means disable autosaving due to number of characters typed. */);
11782 auto_save_interval = 300;
11784 DEFVAR_LISP ("auto-save-timeout", Vauto_save_timeout,
11785 doc: /* *Number of seconds idle time before auto-save.
11786 Zero or nil means disable auto-saving due to idleness.
11787 After auto-saving due to this many seconds of idle time,
11788 Emacs also does a garbage collection if that seems to be warranted. */);
11789 XSETFASTINT (Vauto_save_timeout, 30);
11791 DEFVAR_LISP ("echo-keystrokes", Vecho_keystrokes,
11792 doc: /* *Nonzero means echo unfinished commands after this many seconds of pause.
11793 The value may be integer or floating point. */);
11794 Vecho_keystrokes = make_number (1);
11796 DEFVAR_INT ("polling-period", polling_period,
11797 doc: /* *Interval between polling for input during Lisp execution.
11798 The reason for polling is to make C-g work to stop a running program.
11799 Polling is needed only when using X windows and SIGIO does not work.
11800 Polling is automatically disabled in all other cases. */);
11801 polling_period = 2;
11803 DEFVAR_LISP ("double-click-time", Vdouble_click_time,
11804 doc: /* *Maximum time between mouse clicks to make a double-click.
11805 Measured in milliseconds. The value nil means disable double-click
11806 recognition; t means double-clicks have no time limit and are detected
11807 by position only. */);
11808 Vdouble_click_time = make_number (500);
11810 DEFVAR_INT ("double-click-fuzz", double_click_fuzz,
11811 doc: /* *Maximum mouse movement between clicks to make a double-click.
11812 On window-system frames, value is the number of pixels the mouse may have
11813 moved horizontally or vertically between two clicks to make a double-click.
11814 On non window-system frames, value is interpreted in units of 1/8 characters
11815 instead of pixels.
11817 This variable is also the threshold for motion of the mouse
11818 to count as a drag. */);
11819 double_click_fuzz = 3;
11821 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", inhibit_local_menu_bar_menus,
11822 doc: /* *Non-nil means inhibit local map menu bar menus. */);
11823 inhibit_local_menu_bar_menus = 0;
11825 DEFVAR_INT ("num-input-keys", num_input_keys,
11826 doc: /* Number of complete key sequences read as input so far.
11827 This includes key sequences read from keyboard macros.
11828 The number is effectively the number of interactive command invocations. */);
11829 num_input_keys = 0;
11831 DEFVAR_INT ("num-nonmacro-input-events", num_nonmacro_input_events,
11832 doc: /* Number of input events read from the keyboard so far.
11833 This does not include events generated by keyboard macros. */);
11834 num_nonmacro_input_events = 0;
11836 DEFVAR_LISP ("last-event-frame", Vlast_event_frame,
11837 doc: /* The frame in which the most recently read event occurred.
11838 If the last event came from a keyboard macro, this is set to `macro'. */);
11839 Vlast_event_frame = Qnil;
11841 /* This variable is set up in sysdep.c. */
11842 DEFVAR_LISP ("tty-erase-char", Vtty_erase_char,
11843 doc: /* The ERASE character as set by the user with stty. */);
11845 DEFVAR_LISP ("help-char", Vhelp_char,
11846 doc: /* Character to recognize as meaning Help.
11847 When it is read, do `(eval help-form)', and display result if it's a string.
11848 If the value of `help-form' is nil, this char can be read normally. */);
11849 XSETINT (Vhelp_char, Ctl ('H'));
11851 DEFVAR_LISP ("help-event-list", Vhelp_event_list,
11852 doc: /* List of input events to recognize as meaning Help.
11853 These work just like the value of `help-char' (see that). */);
11854 Vhelp_event_list = Qnil;
11856 DEFVAR_LISP ("help-form", Vhelp_form,
11857 doc: /* Form to execute when character `help-char' is read.
11858 If the form returns a string, that string is displayed.
11859 If `help-form' is nil, the help char is not recognized. */);
11860 Vhelp_form = Qnil;
11862 DEFVAR_LISP ("prefix-help-command", Vprefix_help_command,
11863 doc: /* Command to run when `help-char' character follows a prefix key.
11864 This command is used only when there is no actual binding
11865 for that character after that prefix key. */);
11866 Vprefix_help_command = Qnil;
11868 DEFVAR_LISP ("top-level", Vtop_level,
11869 doc: /* Form to evaluate when Emacs starts up.
11870 Useful to set before you dump a modified Emacs. */);
11871 Vtop_level = Qnil;
11873 DEFVAR_KBOARD ("keyboard-translate-table", Vkeyboard_translate_table,
11874 doc: /* Translate table for local keyboard input, or nil.
11875 If non-nil, the value should be a char-table. Each character read
11876 from the keyboard is looked up in this char-table. If the value found
11877 there is non-nil, then it is used instead of the actual input character.
11879 The value can also be a string or vector, but this is considered obsolete.
11880 If it is a string or vector of length N, character codes N and up are left
11881 untranslated. In a vector, an element which is nil means "no translation".
11883 This is applied to the characters supplied to input methods, not their
11884 output. See also `translation-table-for-input'.
11886 This variable has a separate binding for each terminal.
11887 See Info node `(elisp)Multiple Terminals'. */);
11889 DEFVAR_BOOL ("cannot-suspend", cannot_suspend,
11890 doc: /* Non-nil means to always spawn a subshell instead of suspending.
11891 \(Even if the operating system has support for stopping a process.\) */);
11892 cannot_suspend = 0;
11894 DEFVAR_BOOL ("menu-prompting", menu_prompting,
11895 doc: /* Non-nil means prompt with menus when appropriate.
11896 This is done when reading from a keymap that has a prompt string,
11897 for elements that have prompt strings.
11898 The menu is displayed on the screen
11899 if X menus were enabled at configuration
11900 time and the previous event was a mouse click prefix key.
11901 Otherwise, menu prompting uses the echo area. */);
11902 menu_prompting = 1;
11904 DEFVAR_LISP ("menu-prompt-more-char", menu_prompt_more_char,
11905 doc: /* Character to see next line of menu prompt.
11906 Type this character while in a menu prompt to rotate around the lines of it. */);
11907 XSETINT (menu_prompt_more_char, ' ');
11909 DEFVAR_INT ("extra-keyboard-modifiers", extra_keyboard_modifiers,
11910 doc: /* A mask of additional modifier keys to use with every keyboard character.
11911 Emacs applies the modifiers of the character stored here to each keyboard
11912 character it reads. For example, after evaluating the expression
11913 (setq extra-keyboard-modifiers ?\\C-x)
11914 all input characters will have the control modifier applied to them.
11916 Note that the character ?\\C-@, equivalent to the integer zero, does
11917 not count as a control character; rather, it counts as a character
11918 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero
11919 cancels any modification. */);
11920 extra_keyboard_modifiers = 0;
11922 DEFVAR_LISP ("deactivate-mark", Vdeactivate_mark,
11923 doc: /* If an editing command sets this to t, deactivate the mark afterward.
11924 The command loop sets this to nil before each command,
11925 and tests the value when the command returns.
11926 Buffer modification stores t in this variable. */);
11927 Vdeactivate_mark = Qnil;
11928 Qdeactivate_mark = intern_c_string ("deactivate-mark");
11929 staticpro (&Qdeactivate_mark);
11931 DEFVAR_LISP ("command-hook-internal", Vcommand_hook_internal,
11932 doc: /* Temporary storage of `pre-command-hook' or `post-command-hook'. */);
11933 Vcommand_hook_internal = Qnil;
11935 DEFVAR_LISP ("pre-command-hook", Vpre_command_hook,
11936 doc: /* Normal hook run before each command is executed.
11937 If an unhandled error happens in running this hook,
11938 the hook value is set to nil, since otherwise the error
11939 might happen repeatedly and make Emacs nonfunctional. */);
11940 Vpre_command_hook = Qnil;
11942 DEFVAR_LISP ("post-command-hook", Vpost_command_hook,
11943 doc: /* Normal hook run after each command is executed.
11944 If an unhandled error happens in running this hook,
11945 the hook value is set to nil, since otherwise the error
11946 might happen repeatedly and make Emacs nonfunctional. */);
11947 Vpost_command_hook = Qnil;
11949 #if 0
11950 DEFVAR_LISP ("echo-area-clear-hook", ...,
11951 doc: /* Normal hook run when clearing the echo area. */);
11952 #endif
11953 Qecho_area_clear_hook = intern_c_string ("echo-area-clear-hook");
11954 staticpro (&Qecho_area_clear_hook);
11955 Fset (Qecho_area_clear_hook, Qnil);
11957 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", Vlucid_menu_bar_dirty_flag,
11958 doc: /* Non-nil means menu bar, specified Lucid style, needs to be recomputed. */);
11959 Vlucid_menu_bar_dirty_flag = Qnil;
11961 DEFVAR_LISP ("menu-bar-final-items", Vmenu_bar_final_items,
11962 doc: /* List of menu bar items to move to the end of the menu bar.
11963 The elements of the list are event types that may have menu bar bindings. */);
11964 Vmenu_bar_final_items = Qnil;
11966 DEFVAR_LISP ("tool-bar-separator-image-expression", Vtool_bar_separator_image_expression,
11967 doc: /* Expression evaluating to the image spec for a tool-bar separator.
11968 This is used internally by graphical displays that do not render
11969 tool-bar separators natively. Otherwise it is unused (e.g. on GTK). */);
11970 Vtool_bar_separator_image_expression = Qnil;
11972 DEFVAR_KBOARD ("overriding-terminal-local-map",
11973 Voverriding_terminal_local_map,
11974 doc: /* Per-terminal keymap that overrides all other local keymaps.
11975 If this variable is non-nil, it is used as a keymap instead of the
11976 buffer's local map, and the minor mode keymaps and text property keymaps.
11977 It also replaces `overriding-local-map'.
11979 This variable is intended to let commands such as `universal-argument'
11980 set up a different keymap for reading the next command.
11982 `overriding-terminal-local-map' has a separate binding for each
11983 terminal device.
11984 See Info node `(elisp)Multiple Terminals'. */);
11986 DEFVAR_LISP ("overriding-local-map", Voverriding_local_map,
11987 doc: /* Keymap that overrides all other local keymaps.
11988 If this variable is non-nil, it is used as a keymap--replacing the
11989 buffer's local map, the minor mode keymaps, and char property keymaps. */);
11990 Voverriding_local_map = Qnil;
11992 DEFVAR_LISP ("overriding-local-map-menu-flag", Voverriding_local_map_menu_flag,
11993 doc: /* Non-nil means `overriding-local-map' applies to the menu bar.
11994 Otherwise, the menu bar continues to reflect the buffer's local map
11995 and the minor mode maps regardless of `overriding-local-map'. */);
11996 Voverriding_local_map_menu_flag = Qnil;
11998 DEFVAR_LISP ("special-event-map", Vspecial_event_map,
11999 doc: /* Keymap defining bindings for special events to execute at low level. */);
12000 Vspecial_event_map = Fcons (intern_c_string ("keymap"), Qnil);
12002 DEFVAR_LISP ("track-mouse", do_mouse_tracking,
12003 doc: /* *Non-nil means generate motion events for mouse motion. */);
12005 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
12006 doc: /* Alist of system-specific X windows key symbols.
12007 Each element should have the form (N . SYMBOL) where N is the
12008 numeric keysym code (sans the \"system-specific\" bit 1<<28)
12009 and SYMBOL is its name.
12011 `system-key-alist' has a separate binding for each terminal device.
12012 See Info node `(elisp)Multiple Terminals'. */);
12014 DEFVAR_KBOARD ("local-function-key-map", Vlocal_function_key_map,
12015 doc: /* Keymap that translates key sequences to key sequences during input.
12016 This is used mainly for mapping key sequences into some preferred
12017 key events (symbols).
12019 The `read-key-sequence' function replaces any subsequence bound by
12020 `local-function-key-map' with its binding. More precisely, when the
12021 active keymaps have no binding for the current key sequence but
12022 `local-function-key-map' binds a suffix of the sequence to a vector or
12023 string, `read-key-sequence' replaces the matching suffix with its
12024 binding, and continues with the new sequence.
12026 If the binding is a function, it is called with one argument (the prompt)
12027 and its return value (a key sequence) is used.
12029 The events that come from bindings in `local-function-key-map' are not
12030 themselves looked up in `local-function-key-map'.
12032 For example, suppose `local-function-key-map' binds `ESC O P' to [f1].
12033 Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing
12034 `C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix key,
12035 typing `ESC O P x' would return [f1 x].
12037 `local-function-key-map' has a separate binding for each terminal
12038 device. See Info node `(elisp)Multiple Terminals'. If you need to
12039 define a binding on all terminals, change `function-key-map'
12040 instead. Initially, `local-function-key-map' is an empty keymap that
12041 has `function-key-map' as its parent on all terminal devices. */);
12043 DEFVAR_KBOARD ("input-decode-map", Vinput_decode_map,
12044 doc: /* Keymap that decodes input escape sequences.
12045 This is used mainly for mapping ASCII function key sequences into
12046 real Emacs function key events (symbols).
12048 The `read-key-sequence' function replaces any subsequence bound by
12049 `input-decode-map' with its binding. Contrary to `function-key-map',
12050 this map applies its rebinding regardless of the presence of an ordinary
12051 binding. So it is more like `key-translation-map' except that it applies
12052 before `function-key-map' rather than after.
12054 If the binding is a function, it is called with one argument (the prompt)
12055 and its return value (a key sequence) is used.
12057 The events that come from bindings in `input-decode-map' are not
12058 themselves looked up in `input-decode-map'.
12060 This variable is keyboard-local. */);
12062 DEFVAR_LISP ("function-key-map", Vfunction_key_map,
12063 doc: /* The parent keymap of all `local-function-key-map' instances.
12064 Function key definitions that apply to all terminal devices should go
12065 here. If a mapping is defined in both the current
12066 `local-function-key-map' binding and this variable, then the local
12067 definition will take precendence. */);
12068 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
12070 DEFVAR_LISP ("key-translation-map", Vkey_translation_map,
12071 doc: /* Keymap of key translations that can override keymaps.
12072 This keymap works like `function-key-map', but comes after that,
12073 and its non-prefix bindings override ordinary bindings.
12074 Another difference is that it is global rather than keyboard-local. */);
12075 Vkey_translation_map = Fmake_sparse_keymap (Qnil);
12077 DEFVAR_LISP ("deferred-action-list", Vdeferred_action_list,
12078 doc: /* List of deferred actions to be performed at a later time.
12079 The precise format isn't relevant here; we just check whether it is nil. */);
12080 Vdeferred_action_list = Qnil;
12082 DEFVAR_LISP ("deferred-action-function", Vdeferred_action_function,
12083 doc: /* Function to call to handle deferred actions, after each command.
12084 This function is called with no arguments after each command
12085 whenever `deferred-action-list' is non-nil. */);
12086 Vdeferred_action_function = Qnil;
12088 DEFVAR_LISP ("suggest-key-bindings", Vsuggest_key_bindings,
12089 doc: /* *Non-nil means show the equivalent key-binding when M-x command has one.
12090 The value can be a length of time to show the message for.
12091 If the value is non-nil and not a number, we wait 2 seconds. */);
12092 Vsuggest_key_bindings = Qt;
12094 DEFVAR_LISP ("timer-list", Vtimer_list,
12095 doc: /* List of active absolute time timers in order of increasing time. */);
12096 Vtimer_list = Qnil;
12098 DEFVAR_LISP ("timer-idle-list", Vtimer_idle_list,
12099 doc: /* List of active idle-time timers in order of increasing time. */);
12100 Vtimer_idle_list = Qnil;
12102 DEFVAR_LISP ("input-method-function", Vinput_method_function,
12103 doc: /* If non-nil, the function that implements the current input method.
12104 It's called with one argument, a printing character that was just read.
12105 \(That means a character with code 040...0176.)
12106 Typically this function uses `read-event' to read additional events.
12107 When it does so, it should first bind `input-method-function' to nil
12108 so it will not be called recursively.
12110 The function should return a list of zero or more events
12111 to be used as input. If it wants to put back some events
12112 to be reconsidered, separately, by the input method,
12113 it can add them to the beginning of `unread-command-events'.
12115 The input method function can find in `input-method-previous-message'
12116 the previous echo area message.
12118 The input method function should refer to the variables
12119 `input-method-use-echo-area' and `input-method-exit-on-first-char'
12120 for guidance on what to do. */);
12121 Vinput_method_function = Qnil;
12123 DEFVAR_LISP ("input-method-previous-message",
12124 Vinput_method_previous_message,
12125 doc: /* When `input-method-function' is called, hold the previous echo area message.
12126 This variable exists because `read-event' clears the echo area
12127 before running the input method. It is nil if there was no message. */);
12128 Vinput_method_previous_message = Qnil;
12130 DEFVAR_LISP ("show-help-function", Vshow_help_function,
12131 doc: /* If non-nil, the function that implements the display of help.
12132 It's called with one argument, the help string to display. */);
12133 Vshow_help_function = Qnil;
12135 DEFVAR_LISP ("disable-point-adjustment", Vdisable_point_adjustment,
12136 doc: /* If non-nil, suppress point adjustment after executing a command.
12138 After a command is executed, if point is moved into a region that has
12139 special properties (e.g. composition, display), we adjust point to
12140 the boundary of the region. But, when a command sets this variable to
12141 non-nil, we suppress the point adjustment.
12143 This variable is set to nil before reading a command, and is checked
12144 just after executing the command. */);
12145 Vdisable_point_adjustment = Qnil;
12147 DEFVAR_LISP ("global-disable-point-adjustment",
12148 Vglobal_disable_point_adjustment,
12149 doc: /* *If non-nil, always suppress point adjustment.
12151 The default value is nil, in which case, point adjustment are
12152 suppressed only after special commands that set
12153 `disable-point-adjustment' (which see) to non-nil. */);
12154 Vglobal_disable_point_adjustment = Qnil;
12156 DEFVAR_LISP ("minibuffer-message-timeout", Vminibuffer_message_timeout,
12157 doc: /* *How long to display an echo-area message when the minibuffer is active.
12158 If the value is not a number, such messages don't time out. */);
12159 Vminibuffer_message_timeout = make_number (2);
12161 DEFVAR_LISP ("throw-on-input", Vthrow_on_input,
12162 doc: /* If non-nil, any keyboard input throws to this symbol.
12163 The value of that variable is passed to `quit-flag' and later causes a
12164 peculiar kind of quitting. */);
12165 Vthrow_on_input = Qnil;
12167 DEFVAR_LISP ("command-error-function", Vcommand_error_function,
12168 doc: /* If non-nil, function to output error messages.
12169 The arguments are the error data, a list of the form
12170 (SIGNALED-CONDITIONS . SIGNAL-DATA)
12171 such as just as `condition-case' would bind its variable to,
12172 the context (a string which normally goes at the start of the message),
12173 and the Lisp function within which the error was signaled. */);
12174 Vcommand_error_function = Qnil;
12176 DEFVAR_LISP ("enable-disabled-menus-and-buttons",
12177 Venable_disabled_menus_and_buttons,
12178 doc: /* If non-nil, don't ignore events produced by disabled menu items and tool-bar.
12180 Help functions bind this to allow help on disabled menu items
12181 and tool-bar buttons. */);
12182 Venable_disabled_menus_and_buttons = Qnil;
12184 DEFVAR_LISP ("select-active-regions",
12185 Vselect_active_regions,
12186 doc: /* If non-nil, an active region automatically sets the primary selection.
12187 If the value is `only', only temporarily active regions (usually made
12188 by mouse-dragging or shift-selection) set the window selection.
12190 This takes effect only when Transient Mark mode is enabled. */);
12191 Vselect_active_regions = Qt;
12193 DEFVAR_LISP ("saved-region-selection",
12194 Vsaved_region_selection,
12195 doc: /* Contents of active region prior to buffer modification.
12196 If `select-active-regions' is non-nil, Emacs sets this to the
12197 text in the region before modifying the buffer. The next
12198 `deactivate-mark' call uses this to set the window selection. */);
12199 Vsaved_region_selection = Qnil;
12201 /* Create the initial keyboard. */
12202 initial_kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
12203 init_kboard (initial_kboard);
12204 /* Vwindow_system is left at t for now. */
12205 initial_kboard->next_kboard = all_kboards;
12206 all_kboards = initial_kboard;
12209 void
12210 keys_of_keyboard (void)
12212 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
12213 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
12214 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
12215 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
12216 initial_define_key (meta_map, 'x', "execute-extended-command");
12218 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
12219 "handle-delete-frame");
12220 initial_define_lispy_key (Vspecial_event_map, "ns-put-working-text",
12221 "ns-put-working-text");
12222 initial_define_lispy_key (Vspecial_event_map, "ns-unput-working-text",
12223 "ns-unput-working-text");
12224 /* Here we used to use `ignore-event' which would simple set prefix-arg to
12225 current-prefix-arg, as is done in `handle-switch-frame'.
12226 But `handle-switch-frame is not run from the special-map.
12227 Commands from that map are run in a special way that automatically
12228 preserves the prefix-arg. Restoring the prefix arg here is not just
12229 redundant but harmful:
12230 - C-u C-x v =
12231 - current-prefix-arg is set to non-nil, prefix-arg is set to nil.
12232 - after the first prompt, the exit-minibuffer-hook is run which may
12233 iconify a frame and thus push a `iconify-frame' event.
12234 - after running exit-minibuffer-hook, current-prefix-arg is
12235 restored to the non-nil value it had before the prompt.
12236 - we enter the second prompt.
12237 current-prefix-arg is non-nil, prefix-arg is nil.
12238 - before running the first real event, we run the special iconify-frame
12239 event, but we pass the `special' arg to execute-command so
12240 current-prefix-arg and prefix-arg are left untouched.
12241 - here we foolishly copy the non-nil current-prefix-arg to prefix-arg.
12242 - the next key event will have a spuriously non-nil current-prefix-arg. */
12243 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
12244 "ignore");
12245 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
12246 "ignore");
12247 /* Handling it at such a low-level causes read_key_sequence to get
12248 * confused because it doesn't realize that the current_buffer was
12249 * changed by read_char.
12251 * initial_define_lispy_key (Vspecial_event_map, "select-window",
12252 * "handle-select-window"); */
12253 initial_define_lispy_key (Vspecial_event_map, "save-session",
12254 "handle-save-session");
12256 #ifdef HAVE_DBUS
12257 /* Define a special event which is raised for dbus callback
12258 functions. */
12259 initial_define_lispy_key (Vspecial_event_map, "dbus-event",
12260 "dbus-handle-event");
12261 #endif
12263 initial_define_lispy_key (Vspecial_event_map, "config-changed-event",
12264 "ignore");
12267 /* Mark the pointers in the kboard objects.
12268 Called by the Fgarbage_collector. */
12269 void
12270 mark_kboards (void)
12272 KBOARD *kb;
12273 Lisp_Object *p;
12274 for (kb = all_kboards; kb; kb = kb->next_kboard)
12276 if (kb->kbd_macro_buffer)
12277 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
12278 mark_object (*p);
12279 mark_object (KVAR (kb, Voverriding_terminal_local_map));
12280 mark_object (KVAR (kb, Vlast_command));
12281 mark_object (KVAR (kb, Vreal_last_command));
12282 mark_object (KVAR (kb, Vkeyboard_translate_table));
12283 mark_object (KVAR (kb, Vlast_repeatable_command));
12284 mark_object (KVAR (kb, Vprefix_arg));
12285 mark_object (KVAR (kb, Vlast_prefix_arg));
12286 mark_object (KVAR (kb, kbd_queue));
12287 mark_object (KVAR (kb, defining_kbd_macro));
12288 mark_object (KVAR (kb, Vlast_kbd_macro));
12289 mark_object (KVAR (kb, Vsystem_key_alist));
12290 mark_object (KVAR (kb, system_key_syms));
12291 mark_object (KVAR (kb, Vwindow_system));
12292 mark_object (KVAR (kb, Vinput_decode_map));
12293 mark_object (KVAR (kb, Vlocal_function_key_map));
12294 mark_object (KVAR (kb, Vdefault_minibuffer_frame));
12295 mark_object (KVAR (kb, echo_string));
12298 struct input_event *event;
12299 for (event = kbd_fetch_ptr; event != kbd_store_ptr; event++)
12301 if (event == kbd_buffer + KBD_BUFFER_SIZE)
12302 event = kbd_buffer;
12303 if (event->kind != SELECTION_REQUEST_EVENT
12304 && event->kind != SELECTION_CLEAR_EVENT)
12306 mark_object (event->x);
12307 mark_object (event->y);
12309 mark_object (event->frame_or_window);
12310 mark_object (event->arg);