* keyboard.c (syms_of_keyboard): Use the same style as later
[emacs.git] / src / keyboard.c
blob72d955637b34212a4a13c2a14f4d6227d2539bb1
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);
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);
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 /* Evaluate an expression and return the result (or nil if something
7545 went wrong). Used to evaluate dynamic parts of menu items. */
7546 Lisp_Object
7547 menu_item_eval_property (Lisp_Object sexpr)
7549 int count = SPECPDL_INDEX ();
7550 Lisp_Object val;
7551 specbind (Qinhibit_redisplay, Qt);
7552 val = internal_condition_case_1 (Feval, sexpr, Qerror,
7553 menu_item_eval_property_1);
7554 return unbind_to (count, val);
7557 /* This function parses a menu item and leaves the result in the
7558 vector item_properties.
7559 ITEM is a key binding, a possible menu item.
7560 INMENUBAR is > 0 when this is considered for an entry in a menu bar
7561 top level.
7562 INMENUBAR is < 0 when this is considered for an entry in a keyboard menu.
7563 parse_menu_item returns true if the item is a menu item and false
7564 otherwise. */
7567 parse_menu_item (Lisp_Object item, int inmenubar)
7569 Lisp_Object def, tem, item_string, start;
7570 Lisp_Object filter;
7571 Lisp_Object keyhint;
7572 int i;
7574 filter = Qnil;
7575 keyhint = Qnil;
7577 if (!CONSP (item))
7578 return 0;
7580 /* Create item_properties vector if necessary. */
7581 if (NILP (item_properties))
7582 item_properties
7583 = Fmake_vector (make_number (ITEM_PROPERTY_ENABLE + 1), Qnil);
7585 /* Initialize optional entries. */
7586 for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++)
7587 ASET (item_properties, i, Qnil);
7588 ASET (item_properties, ITEM_PROPERTY_ENABLE, Qt);
7590 /* Save the item here to protect it from GC. */
7591 ASET (item_properties, ITEM_PROPERTY_ITEM, item);
7593 item_string = XCAR (item);
7595 start = item;
7596 item = XCDR (item);
7597 if (STRINGP (item_string))
7599 /* Old format menu item. */
7600 ASET (item_properties, ITEM_PROPERTY_NAME, item_string);
7602 /* Maybe help string. */
7603 if (CONSP (item) && STRINGP (XCAR (item)))
7605 ASET (item_properties, ITEM_PROPERTY_HELP, XCAR (item));
7606 start = item;
7607 item = XCDR (item);
7610 /* Maybe an obsolete key binding cache. */
7611 if (CONSP (item) && CONSP (XCAR (item))
7612 && (NILP (XCAR (XCAR (item)))
7613 || VECTORP (XCAR (XCAR (item)))))
7614 item = XCDR (item);
7616 /* This is the real definition--the function to run. */
7617 ASET (item_properties, ITEM_PROPERTY_DEF, item);
7619 /* Get enable property, if any. */
7620 if (SYMBOLP (item))
7622 tem = Fget (item, Qmenu_enable);
7623 if (!NILP (Venable_disabled_menus_and_buttons))
7624 ASET (item_properties, ITEM_PROPERTY_ENABLE, Qt);
7625 else if (!NILP (tem))
7626 ASET (item_properties, ITEM_PROPERTY_ENABLE, tem);
7629 else if (EQ (item_string, Qmenu_item) && CONSP (item))
7631 /* New format menu item. */
7632 ASET (item_properties, ITEM_PROPERTY_NAME, XCAR (item));
7633 start = XCDR (item);
7634 if (CONSP (start))
7636 /* We have a real binding. */
7637 ASET (item_properties, ITEM_PROPERTY_DEF, XCAR (start));
7639 item = XCDR (start);
7640 /* Is there an obsolete cache list with key equivalences. */
7641 if (CONSP (item) && CONSP (XCAR (item)))
7642 item = XCDR (item);
7644 /* Parse properties. */
7645 while (CONSP (item) && CONSP (XCDR (item)))
7647 tem = XCAR (item);
7648 item = XCDR (item);
7650 if (EQ (tem, QCenable))
7652 if (!NILP (Venable_disabled_menus_and_buttons))
7653 ASET (item_properties, ITEM_PROPERTY_ENABLE, Qt);
7654 else
7655 ASET (item_properties, ITEM_PROPERTY_ENABLE, XCAR (item));
7657 else if (EQ (tem, QCvisible))
7659 /* If got a visible property and that evaluates to nil
7660 then ignore this item. */
7661 tem = menu_item_eval_property (XCAR (item));
7662 if (NILP (tem))
7663 return 0;
7665 else if (EQ (tem, QChelp))
7666 ASET (item_properties, ITEM_PROPERTY_HELP, XCAR (item));
7667 else if (EQ (tem, QCfilter))
7668 filter = item;
7669 else if (EQ (tem, QCkey_sequence))
7671 tem = XCAR (item);
7672 if (SYMBOLP (tem) || STRINGP (tem) || VECTORP (tem))
7673 /* Be GC protected. Set keyhint to item instead of tem. */
7674 keyhint = item;
7676 else if (EQ (tem, QCkeys))
7678 tem = XCAR (item);
7679 if (CONSP (tem) || STRINGP (tem))
7680 ASET (item_properties, ITEM_PROPERTY_KEYEQ, tem);
7682 else if (EQ (tem, QCbutton) && CONSP (XCAR (item)))
7684 Lisp_Object type;
7685 tem = XCAR (item);
7686 type = XCAR (tem);
7687 if (EQ (type, QCtoggle) || EQ (type, QCradio))
7689 ASET (item_properties, ITEM_PROPERTY_SELECTED,
7690 XCDR (tem));
7691 ASET (item_properties, ITEM_PROPERTY_TYPE, type);
7694 item = XCDR (item);
7697 else if (inmenubar || !NILP (start))
7698 return 0;
7700 else
7701 return 0; /* not a menu item */
7703 /* If item string is not a string, evaluate it to get string.
7704 If we don't get a string, skip this item. */
7705 item_string = AREF (item_properties, ITEM_PROPERTY_NAME);
7706 if (!(STRINGP (item_string)))
7708 item_string = menu_item_eval_property (item_string);
7709 if (!STRINGP (item_string))
7710 return 0;
7711 ASET (item_properties, ITEM_PROPERTY_NAME, item_string);
7714 /* If got a filter apply it on definition. */
7715 def = AREF (item_properties, ITEM_PROPERTY_DEF);
7716 if (!NILP (filter))
7718 def = menu_item_eval_property (list2 (XCAR (filter),
7719 list2 (Qquote, def)));
7721 ASET (item_properties, ITEM_PROPERTY_DEF, def);
7724 /* Enable or disable selection of item. */
7725 tem = AREF (item_properties, ITEM_PROPERTY_ENABLE);
7726 if (!EQ (tem, Qt))
7728 tem = menu_item_eval_property (tem);
7729 if (inmenubar && NILP (tem))
7730 return 0; /* Ignore disabled items in menu bar. */
7731 ASET (item_properties, ITEM_PROPERTY_ENABLE, tem);
7734 /* If we got no definition, this item is just unselectable text which
7735 is OK in a submenu but not in the menubar. */
7736 if (NILP (def))
7737 return (inmenubar ? 0 : 1);
7739 /* See if this is a separate pane or a submenu. */
7740 def = AREF (item_properties, ITEM_PROPERTY_DEF);
7741 tem = get_keymap (def, 0, 1);
7742 /* For a subkeymap, just record its details and exit. */
7743 if (CONSP (tem))
7745 ASET (item_properties, ITEM_PROPERTY_MAP, tem);
7746 ASET (item_properties, ITEM_PROPERTY_DEF, tem);
7747 return 1;
7750 /* At the top level in the menu bar, do likewise for commands also.
7751 The menu bar does not display equivalent key bindings anyway.
7752 ITEM_PROPERTY_DEF is already set up properly. */
7753 if (inmenubar > 0)
7754 return 1;
7756 { /* This is a command. See if there is an equivalent key binding. */
7757 Lisp_Object keyeq = AREF (item_properties, ITEM_PROPERTY_KEYEQ);
7759 /* The previous code preferred :key-sequence to :keys, so we
7760 preserve this behavior. */
7761 if (STRINGP (keyeq) && !CONSP (keyhint))
7762 keyeq = concat2 (build_string (" "), Fsubstitute_command_keys (keyeq));
7763 else
7765 Lisp_Object prefix = keyeq;
7766 Lisp_Object keys = Qnil;
7768 if (CONSP (prefix))
7770 def = XCAR (prefix);
7771 prefix = XCDR (prefix);
7773 else
7774 def = AREF (item_properties, ITEM_PROPERTY_DEF);
7776 if (CONSP (keyhint) && !NILP (XCAR (keyhint)))
7778 keys = XCAR (keyhint);
7779 tem = Fkey_binding (keys, Qnil, Qnil, Qnil);
7781 /* We have a suggested key. Is it bound to the command? */
7782 if (NILP (tem)
7783 || (!EQ (tem, def)
7784 /* If the command is an alias for another
7785 (such as lmenu.el set it up), check if the
7786 original command matches the cached command. */
7787 && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function))))
7788 keys = Qnil;
7791 if (NILP (keys))
7792 keys = Fwhere_is_internal (def, Qnil, Qt, Qnil, Qnil);
7794 if (!NILP (keys))
7796 tem = Fkey_description (keys, Qnil);
7797 if (CONSP (prefix))
7799 if (STRINGP (XCAR (prefix)))
7800 tem = concat2 (XCAR (prefix), tem);
7801 if (STRINGP (XCDR (prefix)))
7802 tem = concat2 (tem, XCDR (prefix));
7804 keyeq = concat2 (build_string (" "), tem);
7805 /* keyeq = concat3(build_string(" ("),tem,build_string(")")); */
7807 else
7808 keyeq = Qnil;
7811 /* If we have an equivalent key binding, use that. */
7812 ASET (item_properties, ITEM_PROPERTY_KEYEQ, keyeq);
7815 /* Include this when menu help is implemented.
7816 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP];
7817 if (!(NILP (tem) || STRINGP (tem)))
7819 tem = menu_item_eval_property (tem);
7820 if (!STRINGP (tem))
7821 tem = Qnil;
7822 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP] = tem;
7826 /* Handle radio buttons or toggle boxes. */
7827 tem = AREF (item_properties, ITEM_PROPERTY_SELECTED);
7828 if (!NILP (tem))
7829 ASET (item_properties, ITEM_PROPERTY_SELECTED,
7830 menu_item_eval_property (tem));
7832 return 1;
7837 /***********************************************************************
7838 Tool-bars
7839 ***********************************************************************/
7841 /* A vector holding tool bar items while they are parsed in function
7842 tool_bar_items. Each item occupies TOOL_BAR_ITEM_NSCLOTS elements
7843 in the vector. */
7845 static Lisp_Object tool_bar_items_vector;
7847 /* A vector holding the result of parse_tool_bar_item. Layout is like
7848 the one for a single item in tool_bar_items_vector. */
7850 static Lisp_Object tool_bar_item_properties;
7852 /* Next free index in tool_bar_items_vector. */
7854 static int ntool_bar_items;
7856 /* The symbols `:image' and `:rtl'. */
7858 Lisp_Object QCimage;
7859 Lisp_Object Qrtl;
7861 /* Function prototypes. */
7863 static void init_tool_bar_items (Lisp_Object);
7864 static void process_tool_bar_item (Lisp_Object, Lisp_Object, Lisp_Object, void*);
7865 static int parse_tool_bar_item (Lisp_Object, Lisp_Object);
7866 static void append_tool_bar_item (void);
7869 /* Return a vector of tool bar items for keymaps currently in effect.
7870 Reuse vector REUSE if non-nil. Return in *NITEMS the number of
7871 tool bar items found. */
7873 Lisp_Object
7874 tool_bar_items (Lisp_Object reuse, int *nitems)
7876 Lisp_Object *maps;
7877 int nmaps, i;
7878 Lisp_Object oquit;
7879 Lisp_Object *tmaps;
7881 *nitems = 0;
7883 /* In order to build the menus, we need to call the keymap
7884 accessors. They all call QUIT. But this function is called
7885 during redisplay, during which a quit is fatal. So inhibit
7886 quitting while building the menus. We do this instead of
7887 specbind because (1) errors will clear it anyway and (2) this
7888 avoids risk of specpdl overflow. */
7889 oquit = Vinhibit_quit;
7890 Vinhibit_quit = Qt;
7892 /* Initialize tool_bar_items_vector and protect it from GC. */
7893 init_tool_bar_items (reuse);
7895 /* Build list of keymaps in maps. Set nmaps to the number of maps
7896 to process. */
7898 /* Should overriding-terminal-local-map and overriding-local-map apply? */
7899 if (!NILP (Voverriding_local_map_menu_flag))
7901 /* Yes, use them (if non-nil) as well as the global map. */
7902 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
7903 nmaps = 0;
7904 if (!NILP (KVAR (current_kboard, Voverriding_terminal_local_map)))
7905 maps[nmaps++] = KVAR (current_kboard, Voverriding_terminal_local_map);
7906 if (!NILP (Voverriding_local_map))
7907 maps[nmaps++] = Voverriding_local_map;
7909 else
7911 /* No, so use major and minor mode keymaps and keymap property.
7912 Note that tool-bar bindings in the local-map and keymap
7913 properties may not work reliable, as they are only
7914 recognized when the tool-bar (or mode-line) is updated,
7915 which does not normally happen after every command. */
7916 Lisp_Object tem;
7917 int nminor;
7918 nminor = current_minor_maps (NULL, &tmaps);
7919 maps = (Lisp_Object *) alloca ((nminor + 3) * sizeof (maps[0]));
7920 nmaps = 0;
7921 if (tem = get_local_map (PT, current_buffer, Qkeymap), !NILP (tem))
7922 maps[nmaps++] = tem;
7923 memcpy (maps + nmaps, tmaps, nminor * sizeof (maps[0]));
7924 nmaps += nminor;
7925 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
7928 /* Add global keymap at the end. */
7929 maps[nmaps++] = current_global_map;
7931 /* Process maps in reverse order and look up in each map the prefix
7932 key `tool-bar'. */
7933 for (i = nmaps - 1; i >= 0; --i)
7934 if (!NILP (maps[i]))
7936 Lisp_Object keymap;
7938 keymap = get_keymap (access_keymap (maps[i], Qtool_bar, 1, 0, 1), 0, 1);
7939 if (CONSP (keymap))
7940 map_keymap (keymap, process_tool_bar_item, Qnil, NULL, 1);
7943 Vinhibit_quit = oquit;
7944 *nitems = ntool_bar_items / TOOL_BAR_ITEM_NSLOTS;
7945 return tool_bar_items_vector;
7949 /* Process the definition of KEY which is DEF. */
7951 static void
7952 process_tool_bar_item (Lisp_Object key, Lisp_Object def, Lisp_Object data, void *args)
7954 int i;
7955 struct gcpro gcpro1, gcpro2;
7957 /* Protect KEY and DEF from GC because parse_tool_bar_item may call
7958 eval. */
7959 GCPRO2 (key, def);
7961 if (EQ (def, Qundefined))
7963 /* If a map has an explicit `undefined' as definition,
7964 discard any previously made item. */
7965 for (i = 0; i < ntool_bar_items; i += TOOL_BAR_ITEM_NSLOTS)
7967 Lisp_Object *v = XVECTOR (tool_bar_items_vector)->contents + i;
7969 if (EQ (key, v[TOOL_BAR_ITEM_KEY]))
7971 if (ntool_bar_items > i + TOOL_BAR_ITEM_NSLOTS)
7972 memmove (v, v + TOOL_BAR_ITEM_NSLOTS,
7973 ((ntool_bar_items - i - TOOL_BAR_ITEM_NSLOTS)
7974 * sizeof (Lisp_Object)));
7975 ntool_bar_items -= TOOL_BAR_ITEM_NSLOTS;
7976 break;
7980 else if (parse_tool_bar_item (key, def))
7981 /* Append a new tool bar item to tool_bar_items_vector. Accept
7982 more than one definition for the same key. */
7983 append_tool_bar_item ();
7985 UNGCPRO;
7989 /* Parse a tool bar item specification ITEM for key KEY and return the
7990 result in tool_bar_item_properties. Value is zero if ITEM is
7991 invalid.
7993 ITEM is a list `(menu-item CAPTION BINDING PROPS...)'.
7995 CAPTION is the caption of the item, If it's not a string, it is
7996 evaluated to get a string.
7998 BINDING is the tool bar item's binding. Tool-bar items with keymaps
7999 as binding are currently ignored.
8001 The following properties are recognized:
8003 - `:enable FORM'.
8005 FORM is evaluated and specifies whether the tool bar item is
8006 enabled or disabled.
8008 - `:visible FORM'
8010 FORM is evaluated and specifies whether the tool bar item is visible.
8012 - `:filter FUNCTION'
8014 FUNCTION is invoked with one parameter `(quote BINDING)'. Its
8015 result is stored as the new binding.
8017 - `:button (TYPE SELECTED)'
8019 TYPE must be one of `:radio' or `:toggle'. SELECTED is evaluated
8020 and specifies whether the button is selected (pressed) or not.
8022 - `:image IMAGES'
8024 IMAGES is either a single image specification or a vector of four
8025 image specifications. See enum tool_bar_item_images.
8027 - `:help HELP-STRING'.
8029 Gives a help string to display for the tool bar item.
8031 - `:label LABEL-STRING'.
8033 A text label to show with the tool bar button if labels are enabled. */
8035 static int
8036 parse_tool_bar_item (Lisp_Object key, Lisp_Object item)
8038 /* Access slot with index IDX of vector tool_bar_item_properties. */
8039 #define PROP(IDX) XVECTOR (tool_bar_item_properties)->contents[IDX]
8041 Lisp_Object filter = Qnil;
8042 Lisp_Object caption;
8043 int i, have_label = 0;
8045 /* Defininition looks like `(menu-item CAPTION BINDING PROPS...)'.
8046 Rule out items that aren't lists, don't start with
8047 `menu-item' or whose rest following `tool-bar-item' is not a
8048 list. */
8049 if (!CONSP (item))
8050 return 0;
8052 /* As an exception, allow old-style menu separators. */
8053 if (STRINGP (XCAR (item)))
8054 item = Fcons (XCAR (item), Qnil);
8055 else if (!EQ (XCAR (item), Qmenu_item)
8056 || (item = XCDR (item), !CONSP (item)))
8057 return 0;
8059 /* Create tool_bar_item_properties vector if necessary. Reset it to
8060 defaults. */
8061 if (VECTORP (tool_bar_item_properties))
8063 for (i = 0; i < TOOL_BAR_ITEM_NSLOTS; ++i)
8064 PROP (i) = Qnil;
8066 else
8067 tool_bar_item_properties
8068 = Fmake_vector (make_number (TOOL_BAR_ITEM_NSLOTS), Qnil);
8070 /* Set defaults. */
8071 PROP (TOOL_BAR_ITEM_KEY) = key;
8072 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
8074 /* Get the caption of the item. If the caption is not a string,
8075 evaluate it to get a string. If we don't get a string, skip this
8076 item. */
8077 caption = XCAR (item);
8078 if (!STRINGP (caption))
8080 caption = menu_item_eval_property (caption);
8081 if (!STRINGP (caption))
8082 return 0;
8084 PROP (TOOL_BAR_ITEM_CAPTION) = caption;
8086 /* If the rest following the caption is not a list, the menu item is
8087 either a separator, or invalid. */
8088 item = XCDR (item);
8089 if (!CONSP (item))
8091 if (menu_separator_name_p (SSDATA (caption)))
8093 PROP (TOOL_BAR_ITEM_TYPE) = Qt;
8094 #if !defined (USE_GTK) && !defined (HAVE_NS)
8095 /* If we use build_desired_tool_bar_string to render the
8096 tool bar, the separator is rendered as an image. */
8097 PROP (TOOL_BAR_ITEM_IMAGES)
8098 = menu_item_eval_property (Vtool_bar_separator_image_expression);
8099 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qnil;
8100 PROP (TOOL_BAR_ITEM_SELECTED_P) = Qnil;
8101 PROP (TOOL_BAR_ITEM_CAPTION) = Qnil;
8102 #endif
8103 return 1;
8105 return 0;
8108 /* Store the binding. */
8109 PROP (TOOL_BAR_ITEM_BINDING) = XCAR (item);
8110 item = XCDR (item);
8112 /* Ignore cached key binding, if any. */
8113 if (CONSP (item) && CONSP (XCAR (item)))
8114 item = XCDR (item);
8116 /* Process the rest of the properties. */
8117 for (; CONSP (item) && CONSP (XCDR (item)); item = XCDR (XCDR (item)))
8119 Lisp_Object ikey, value;
8121 ikey = XCAR (item);
8122 value = XCAR (XCDR (item));
8124 if (EQ (ikey, QCenable))
8126 /* `:enable FORM'. */
8127 if (!NILP (Venable_disabled_menus_and_buttons))
8128 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
8129 else
8130 PROP (TOOL_BAR_ITEM_ENABLED_P) = value;
8132 else if (EQ (ikey, QCvisible))
8134 /* `:visible FORM'. If got a visible property and that
8135 evaluates to nil then ignore this item. */
8136 if (NILP (menu_item_eval_property (value)))
8137 return 0;
8139 else if (EQ (ikey, QChelp))
8140 /* `:help HELP-STRING'. */
8141 PROP (TOOL_BAR_ITEM_HELP) = value;
8142 else if (EQ (ikey, QCvert_only))
8143 /* `:vert-only t/nil'. */
8144 PROP (TOOL_BAR_ITEM_VERT_ONLY) = value;
8145 else if (EQ (ikey, QClabel))
8147 const char *bad_label = "!!?GARBLED ITEM?!!";
8148 /* `:label LABEL-STRING'. */
8149 PROP (TOOL_BAR_ITEM_LABEL) = STRINGP (value)
8150 ? value
8151 : make_string (bad_label, strlen (bad_label));
8152 have_label = 1;
8154 else if (EQ (ikey, QCfilter))
8155 /* ':filter FORM'. */
8156 filter = value;
8157 else if (EQ (ikey, QCbutton) && CONSP (value))
8159 /* `:button (TYPE . SELECTED)'. */
8160 Lisp_Object type, selected;
8162 type = XCAR (value);
8163 selected = XCDR (value);
8164 if (EQ (type, QCtoggle) || EQ (type, QCradio))
8166 PROP (TOOL_BAR_ITEM_SELECTED_P) = selected;
8167 PROP (TOOL_BAR_ITEM_TYPE) = type;
8170 else if (EQ (ikey, QCimage)
8171 && (CONSP (value)
8172 || (VECTORP (value) && XVECTOR (value)->size == 4)))
8173 /* Value is either a single image specification or a vector
8174 of 4 such specifications for the different button states. */
8175 PROP (TOOL_BAR_ITEM_IMAGES) = value;
8176 else if (EQ (ikey, Qrtl))
8177 /* ':rtl STRING' */
8178 PROP (TOOL_BAR_ITEM_RTL_IMAGE) = value;
8182 if (!have_label)
8184 /* Try to make one from caption and key. */
8185 Lisp_Object tkey = PROP (TOOL_BAR_ITEM_KEY);
8186 Lisp_Object tcapt = PROP (TOOL_BAR_ITEM_CAPTION);
8187 const char *label = SYMBOLP (tkey) ? SSDATA (SYMBOL_NAME (tkey)) : "";
8188 const char *capt = STRINGP (tcapt) ? SSDATA (tcapt) : "";
8189 EMACS_INT max_lbl = 2 * tool_bar_max_label_size;
8190 char *buf = (char *) xmalloc (max_lbl + 1);
8191 Lisp_Object new_lbl;
8192 size_t caption_len = strlen (capt);
8194 if (caption_len <= max_lbl && capt[0] != '\0')
8196 strcpy (buf, capt);
8197 while (caption_len > 0 && buf[caption_len - 1] == '.')
8198 caption_len--;
8199 buf[caption_len] = '\0';
8200 label = capt = buf;
8203 if (strlen (label) <= max_lbl && label[0] != '\0')
8205 int j;
8206 if (label != buf)
8207 strcpy (buf, label);
8209 for (j = 0; buf[j] != '\0'; ++j)
8210 if (buf[j] == '-')
8211 buf[j] = ' ';
8212 label = buf;
8214 else
8215 label = "";
8217 new_lbl = Fupcase_initials (make_string (label, strlen (label)));
8218 if (SCHARS (new_lbl) <= tool_bar_max_label_size)
8219 PROP (TOOL_BAR_ITEM_LABEL) = new_lbl;
8220 else
8221 PROP (TOOL_BAR_ITEM_LABEL) = make_string ("", 0);
8222 free (buf);
8225 /* If got a filter apply it on binding. */
8226 if (!NILP (filter))
8227 PROP (TOOL_BAR_ITEM_BINDING)
8228 = menu_item_eval_property (list2 (filter,
8229 list2 (Qquote,
8230 PROP (TOOL_BAR_ITEM_BINDING))));
8232 /* See if the binding is a keymap. Give up if it is. */
8233 if (CONSP (get_keymap (PROP (TOOL_BAR_ITEM_BINDING), 0, 1)))
8234 return 0;
8236 /* Enable or disable selection of item. */
8237 if (!EQ (PROP (TOOL_BAR_ITEM_ENABLED_P), Qt))
8238 PROP (TOOL_BAR_ITEM_ENABLED_P)
8239 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_ENABLED_P));
8241 /* Handle radio buttons or toggle boxes. */
8242 if (!NILP (PROP (TOOL_BAR_ITEM_SELECTED_P)))
8243 PROP (TOOL_BAR_ITEM_SELECTED_P)
8244 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_SELECTED_P));
8246 return 1;
8248 #undef PROP
8252 /* Initialize tool_bar_items_vector. REUSE, if non-nil, is a vector
8253 that can be reused. */
8255 static void
8256 init_tool_bar_items (Lisp_Object reuse)
8258 if (VECTORP (reuse))
8259 tool_bar_items_vector = reuse;
8260 else
8261 tool_bar_items_vector = Fmake_vector (make_number (64), Qnil);
8262 ntool_bar_items = 0;
8266 /* Append parsed tool bar item properties from
8267 tool_bar_item_properties */
8269 static void
8270 append_tool_bar_item (void)
8272 Lisp_Object *to, *from;
8274 /* Enlarge tool_bar_items_vector if necessary. */
8275 if (ntool_bar_items + TOOL_BAR_ITEM_NSLOTS
8276 >= XVECTOR (tool_bar_items_vector)->size)
8277 tool_bar_items_vector
8278 = larger_vector (tool_bar_items_vector,
8279 2 * XVECTOR (tool_bar_items_vector)->size, Qnil);
8281 /* Append entries from tool_bar_item_properties to the end of
8282 tool_bar_items_vector. */
8283 to = XVECTOR (tool_bar_items_vector)->contents + ntool_bar_items;
8284 from = XVECTOR (tool_bar_item_properties)->contents;
8285 memcpy (to, from, TOOL_BAR_ITEM_NSLOTS * sizeof *to);
8286 ntool_bar_items += TOOL_BAR_ITEM_NSLOTS;
8293 /* Read a character using menus based on maps in the array MAPS.
8294 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
8295 Return t if we displayed a menu but the user rejected it.
8297 PREV_EVENT is the previous input event, or nil if we are reading
8298 the first event of a key sequence.
8300 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
8301 if we used a mouse menu to read the input, or zero otherwise. If
8302 USED_MOUSE_MENU is null, we don't dereference it.
8304 The prompting is done based on the prompt-string of the map
8305 and the strings associated with various map elements.
8307 This can be done with X menus or with menus put in the minibuf.
8308 These are done in different ways, depending on how the input will be read.
8309 Menus using X are done after auto-saving in read-char, getting the input
8310 event from Fx_popup_menu; menus using the minibuf use read_char recursively
8311 and do auto-saving in the inner call of read_char. */
8313 static Lisp_Object
8314 read_char_x_menu_prompt (int nmaps, Lisp_Object *maps, Lisp_Object prev_event,
8315 int *used_mouse_menu)
8317 int mapno;
8319 if (used_mouse_menu)
8320 *used_mouse_menu = 0;
8322 /* Use local over global Menu maps */
8324 if (! menu_prompting)
8325 return Qnil;
8327 /* Optionally disregard all but the global map. */
8328 if (inhibit_local_menu_bar_menus)
8330 maps += (nmaps - 1);
8331 nmaps = 1;
8334 #ifdef HAVE_MENUS
8335 /* If we got to this point via a mouse click,
8336 use a real menu for mouse selection. */
8337 if (EVENT_HAS_PARAMETERS (prev_event)
8338 && !EQ (XCAR (prev_event), Qmenu_bar)
8339 && !EQ (XCAR (prev_event), Qtool_bar))
8341 /* Display the menu and get the selection. */
8342 Lisp_Object *realmaps
8343 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
8344 Lisp_Object value;
8345 int nmaps1 = 0;
8347 /* Use the maps that are not nil. */
8348 for (mapno = 0; mapno < nmaps; mapno++)
8349 if (!NILP (maps[mapno]))
8350 realmaps[nmaps1++] = maps[mapno];
8352 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
8353 if (CONSP (value))
8355 Lisp_Object tem;
8357 record_menu_key (XCAR (value));
8359 /* If we got multiple events, unread all but
8360 the first.
8361 There is no way to prevent those unread events
8362 from showing up later in last_nonmenu_event.
8363 So turn symbol and integer events into lists,
8364 to indicate that they came from a mouse menu,
8365 so that when present in last_nonmenu_event
8366 they won't confuse things. */
8367 for (tem = XCDR (value); CONSP (tem); tem = XCDR (tem))
8369 record_menu_key (XCAR (tem));
8370 if (SYMBOLP (XCAR (tem))
8371 || INTEGERP (XCAR (tem)))
8372 XSETCAR (tem, Fcons (XCAR (tem), Qdisabled));
8375 /* If we got more than one event, put all but the first
8376 onto this list to be read later.
8377 Return just the first event now. */
8378 Vunread_command_events
8379 = nconc2 (XCDR (value), Vunread_command_events);
8380 value = XCAR (value);
8382 else if (NILP (value))
8383 value = Qt;
8384 if (used_mouse_menu)
8385 *used_mouse_menu = 1;
8386 return value;
8388 #endif /* HAVE_MENUS */
8389 return Qnil ;
8392 /* Buffer in use so far for the minibuf prompts for menu keymaps.
8393 We make this bigger when necessary, and never free it. */
8394 static char *read_char_minibuf_menu_text;
8395 /* Size of that buffer. */
8396 static int read_char_minibuf_menu_width;
8398 static Lisp_Object
8399 read_char_minibuf_menu_prompt (int commandflag, int nmaps, Lisp_Object *maps)
8401 int mapno;
8402 register Lisp_Object name;
8403 int nlength;
8404 /* FIXME: Use the minibuffer's frame width. */
8405 int width = FRAME_COLS (SELECTED_FRAME ()) - 4;
8406 int idx = -1;
8407 int nobindings = 1;
8408 Lisp_Object rest, vector;
8409 char *menu;
8411 vector = Qnil;
8412 name = Qnil;
8414 if (! menu_prompting)
8415 return Qnil;
8417 /* Get the menu name from the first map that has one (a prompt string). */
8418 for (mapno = 0; mapno < nmaps; mapno++)
8420 name = Fkeymap_prompt (maps[mapno]);
8421 if (!NILP (name))
8422 break;
8425 /* If we don't have any menus, just read a character normally. */
8426 if (!STRINGP (name))
8427 return Qnil;
8429 /* Make sure we have a big enough buffer for the menu text. */
8430 width = max (width, SBYTES (name));
8431 if (read_char_minibuf_menu_text == 0)
8433 read_char_minibuf_menu_width = width + 4;
8434 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
8436 else if (width + 4 > read_char_minibuf_menu_width)
8438 read_char_minibuf_menu_width = width + 4;
8439 read_char_minibuf_menu_text
8440 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
8442 menu = read_char_minibuf_menu_text;
8444 /* Prompt string always starts with map's prompt, and a space. */
8445 strcpy (menu, SSDATA (name));
8446 nlength = SBYTES (name);
8447 menu[nlength++] = ':';
8448 menu[nlength++] = ' ';
8449 menu[nlength] = 0;
8451 /* Start prompting at start of first map. */
8452 mapno = 0;
8453 rest = maps[mapno];
8455 /* Present the documented bindings, a line at a time. */
8456 while (1)
8458 int notfirst = 0;
8459 int i = nlength;
8460 Lisp_Object obj;
8461 int ch;
8462 Lisp_Object orig_defn_macro;
8464 /* Loop over elements of map. */
8465 while (i < width)
8467 Lisp_Object elt;
8469 /* If reached end of map, start at beginning of next map. */
8470 if (NILP (rest))
8472 mapno++;
8473 /* At end of last map, wrap around to first map if just starting,
8474 or end this line if already have something on it. */
8475 if (mapno == nmaps)
8477 mapno = 0;
8478 if (notfirst || nobindings) break;
8480 rest = maps[mapno];
8483 /* Look at the next element of the map. */
8484 if (idx >= 0)
8485 elt = XVECTOR (vector)->contents[idx];
8486 else
8487 elt = Fcar_safe (rest);
8489 if (idx < 0 && VECTORP (elt))
8491 /* If we found a dense table in the keymap,
8492 advanced past it, but start scanning its contents. */
8493 rest = Fcdr_safe (rest);
8494 vector = elt;
8495 idx = 0;
8497 else
8499 /* An ordinary element. */
8500 Lisp_Object event, tem;
8502 if (idx < 0)
8504 event = Fcar_safe (elt); /* alist */
8505 elt = Fcdr_safe (elt);
8507 else
8509 XSETINT (event, idx); /* vector */
8512 /* Ignore the element if it has no prompt string. */
8513 if (INTEGERP (event) && parse_menu_item (elt, -1))
8515 /* 1 if the char to type matches the string. */
8516 int char_matches;
8517 Lisp_Object upcased_event, downcased_event;
8518 Lisp_Object desc = Qnil;
8519 Lisp_Object s
8520 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
8522 upcased_event = Fupcase (event);
8523 downcased_event = Fdowncase (event);
8524 char_matches = (XINT (upcased_event) == SREF (s, 0)
8525 || XINT (downcased_event) == SREF (s, 0));
8526 if (! char_matches)
8527 desc = Fsingle_key_description (event, Qnil);
8529 #if 0 /* It is redundant to list the equivalent key bindings because
8530 the prefix is what the user has already typed. */
8532 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
8533 if (!NILP (tem))
8534 /* Insert equivalent keybinding. */
8535 s = concat2 (s, tem);
8536 #endif
8538 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
8539 if (EQ (tem, QCradio) || EQ (tem, QCtoggle))
8541 /* Insert button prefix. */
8542 Lisp_Object selected
8543 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
8544 if (EQ (tem, QCradio))
8545 tem = build_string (NILP (selected) ? "(*) " : "( ) ");
8546 else
8547 tem = build_string (NILP (selected) ? "[X] " : "[ ] ");
8548 s = concat2 (tem, s);
8552 /* If we have room for the prompt string, add it to this line.
8553 If this is the first on the line, always add it. */
8554 if ((SCHARS (s) + i + 2
8555 + (char_matches ? 0 : SCHARS (desc) + 3))
8556 < width
8557 || !notfirst)
8559 int thiswidth;
8561 /* Punctuate between strings. */
8562 if (notfirst)
8564 strcpy (menu + i, ", ");
8565 i += 2;
8567 notfirst = 1;
8568 nobindings = 0 ;
8570 /* If the char to type doesn't match the string's
8571 first char, explicitly show what char to type. */
8572 if (! char_matches)
8574 /* Add as much of string as fits. */
8575 thiswidth = SCHARS (desc);
8576 if (thiswidth + i > width)
8577 thiswidth = width - i;
8578 memcpy (menu + i, SDATA (desc), thiswidth);
8579 i += thiswidth;
8580 strcpy (menu + i, " = ");
8581 i += 3;
8584 /* Add as much of string as fits. */
8585 thiswidth = SCHARS (s);
8586 if (thiswidth + i > width)
8587 thiswidth = width - i;
8588 memcpy (menu + i, SDATA (s), thiswidth);
8589 i += thiswidth;
8590 menu[i] = 0;
8592 else
8594 /* If this element does not fit, end the line now,
8595 and save the element for the next line. */
8596 strcpy (menu + i, "...");
8597 break;
8601 /* Move past this element. */
8602 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
8603 /* Handle reaching end of dense table. */
8604 idx = -1;
8605 if (idx >= 0)
8606 idx++;
8607 else
8608 rest = Fcdr_safe (rest);
8612 /* Prompt with that and read response. */
8613 message2_nolog (menu, strlen (menu),
8614 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
8616 /* Make believe its not a keyboard macro in case the help char
8617 is pressed. Help characters are not recorded because menu prompting
8618 is not used on replay.
8620 orig_defn_macro = KVAR (current_kboard, defining_kbd_macro);
8621 KVAR (current_kboard, defining_kbd_macro) = Qnil;
8623 obj = read_char (commandflag, 0, 0, Qt, 0, NULL);
8624 while (BUFFERP (obj));
8625 KVAR (current_kboard, defining_kbd_macro) = orig_defn_macro;
8627 if (!INTEGERP (obj))
8628 return obj;
8629 else if (XINT (obj) == -2)
8630 return obj;
8631 else
8632 ch = XINT (obj);
8634 if (! EQ (obj, menu_prompt_more_char)
8635 && (!INTEGERP (menu_prompt_more_char)
8636 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
8638 if (!NILP (KVAR (current_kboard, defining_kbd_macro)))
8639 store_kbd_macro_char (obj);
8640 return obj;
8642 /* Help char - go round again */
8646 /* Reading key sequences. */
8648 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
8649 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
8650 keymap, or nil otherwise. Return the index of the first keymap in
8651 which KEY has any binding, or NMAPS if no map has a binding.
8653 If KEY is a meta ASCII character, treat it like meta-prefix-char
8654 followed by the corresponding non-meta character. Keymaps in
8655 CURRENT with non-prefix bindings for meta-prefix-char become nil in
8656 NEXT.
8658 If KEY has no bindings in any of the CURRENT maps, NEXT is left
8659 unmodified.
8661 NEXT may be the same array as CURRENT. */
8663 static int
8664 follow_key (Lisp_Object key, int nmaps, Lisp_Object *current, Lisp_Object *defs,
8665 Lisp_Object *next)
8667 int i, first_binding;
8669 first_binding = nmaps;
8670 for (i = nmaps - 1; i >= 0; i--)
8672 if (! NILP (current[i]))
8674 defs[i] = access_keymap (current[i], key, 1, 0, 1);
8675 if (! NILP (defs[i]))
8676 first_binding = i;
8678 else
8679 defs[i] = Qnil;
8682 /* Given the set of bindings we've found, produce the next set of maps. */
8683 if (first_binding < nmaps)
8684 for (i = 0; i < nmaps; i++)
8685 next[i] = NILP (defs[i]) ? Qnil : get_keymap (defs[i], 0, 1);
8687 return first_binding;
8690 /* Structure used to keep track of partial application of key remapping
8691 such as Vfunction_key_map and Vkey_translation_map. */
8692 typedef struct keyremap
8694 /* This is the map originally specified for this use. */
8695 Lisp_Object parent;
8696 /* This is a submap reached by looking up, in PARENT,
8697 the events from START to END. */
8698 Lisp_Object map;
8699 /* Positions [START, END) in the key sequence buffer
8700 are the key that we have scanned so far.
8701 Those events are the ones that we will replace
8702 if PAREHT maps them into a key sequence. */
8703 int start, end;
8704 } keyremap;
8706 /* Lookup KEY in MAP.
8707 MAP is a keymap mapping keys to key vectors or functions.
8708 If the mapping is a function and DO_FUNCTION is non-zero, then
8709 the function is called with PROMPT as parameter and its return
8710 value is used as the return value of this function (after checking
8711 that it is indeed a vector). */
8713 static Lisp_Object
8714 access_keymap_keyremap (Lisp_Object map, Lisp_Object key, Lisp_Object prompt,
8715 int do_funcall)
8717 Lisp_Object next;
8719 next = access_keymap (map, key, 1, 0, 1);
8721 /* Handle symbol with autoload definition. */
8722 if (SYMBOLP (next) && !NILP (Ffboundp (next))
8723 && CONSP (XSYMBOL (next)->function)
8724 && EQ (XCAR (XSYMBOL (next)->function), Qautoload))
8725 do_autoload (XSYMBOL (next)->function, next);
8727 /* Handle a symbol whose function definition is a keymap
8728 or an array. */
8729 if (SYMBOLP (next) && !NILP (Ffboundp (next))
8730 && (ARRAYP (XSYMBOL (next)->function)
8731 || KEYMAPP (XSYMBOL (next)->function)))
8732 next = XSYMBOL (next)->function;
8734 /* If the keymap gives a function, not an
8735 array, then call the function with one arg and use
8736 its value instead. */
8737 if (SYMBOLP (next) && !NILP (Ffboundp (next)) && do_funcall)
8739 Lisp_Object tem;
8740 tem = next;
8742 next = call1 (next, prompt);
8743 /* If the function returned something invalid,
8744 barf--don't ignore it.
8745 (To ignore it safely, we would need to gcpro a bunch of
8746 other variables.) */
8747 if (! (VECTORP (next) || STRINGP (next)))
8748 error ("Function %s returns invalid key sequence", tem);
8750 return next;
8753 /* Do one step of the key remapping used for function-key-map and
8754 key-translation-map:
8755 KEYBUF is the buffer holding the input events.
8756 BUFSIZE is its maximum size.
8757 FKEY is a pointer to the keyremap structure to use.
8758 INPUT is the index of the last element in KEYBUF.
8759 DOIT if non-zero says that the remapping can actually take place.
8760 DIFF is used to return the number of keys added/removed by the remapping.
8761 PARENT is the root of the keymap.
8762 PROMPT is the prompt to use if the remapping happens through a function.
8763 The return value is non-zero if the remapping actually took place. */
8765 static int
8766 keyremap_step (Lisp_Object *keybuf, int bufsize, volatile keyremap *fkey,
8767 int input, int doit, int *diff, Lisp_Object prompt)
8769 Lisp_Object next, key;
8771 key = keybuf[fkey->end++];
8773 if (KEYMAPP (fkey->parent))
8774 next = access_keymap_keyremap (fkey->map, key, prompt, doit);
8775 else
8776 next = Qnil;
8778 /* If keybuf[fkey->start..fkey->end] is bound in the
8779 map and we're in a position to do the key remapping, replace it with
8780 the binding and restart with fkey->start at the end. */
8781 if ((VECTORP (next) || STRINGP (next)) && doit)
8783 int len = XFASTINT (Flength (next));
8784 int i;
8786 *diff = len - (fkey->end - fkey->start);
8788 if (input + *diff >= bufsize)
8789 error ("Key sequence too long");
8791 /* Shift the keys that follow fkey->end. */
8792 if (*diff < 0)
8793 for (i = fkey->end; i < input; i++)
8794 keybuf[i + *diff] = keybuf[i];
8795 else if (*diff > 0)
8796 for (i = input - 1; i >= fkey->end; i--)
8797 keybuf[i + *diff] = keybuf[i];
8798 /* Overwrite the old keys with the new ones. */
8799 for (i = 0; i < len; i++)
8800 keybuf[fkey->start + i]
8801 = Faref (next, make_number (i));
8803 fkey->start = fkey->end += *diff;
8804 fkey->map = fkey->parent;
8806 return 1;
8809 fkey->map = get_keymap (next, 0, 1);
8811 /* If we no longer have a bound suffix, try a new position for
8812 fkey->start. */
8813 if (!CONSP (fkey->map))
8815 fkey->end = ++fkey->start;
8816 fkey->map = fkey->parent;
8818 return 0;
8821 /* Read a sequence of keys that ends with a non prefix character,
8822 storing it in KEYBUF, a buffer of size BUFSIZE.
8823 Prompt with PROMPT.
8824 Return the length of the key sequence stored.
8825 Return -1 if the user rejected a command menu.
8827 Echo starting immediately unless `prompt' is 0.
8829 Where a key sequence ends depends on the currently active keymaps.
8830 These include any minor mode keymaps active in the current buffer,
8831 the current buffer's local map, and the global map.
8833 If a key sequence has no other bindings, we check Vfunction_key_map
8834 to see if some trailing subsequence might be the beginning of a
8835 function key's sequence. If so, we try to read the whole function
8836 key, and substitute its symbolic name into the key sequence.
8838 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
8839 `double-' events into similar click events, if that would make them
8840 bound. We try to turn `triple-' events first into `double-' events,
8841 then into clicks.
8843 If we get a mouse click in a mode line, vertical divider, or other
8844 non-text area, we treat the click as if it were prefixed by the
8845 symbol denoting that area - `mode-line', `vertical-line', or
8846 whatever.
8848 If the sequence starts with a mouse click, we read the key sequence
8849 with respect to the buffer clicked on, not the current buffer.
8851 If the user switches frames in the midst of a key sequence, we put
8852 off the switch-frame event until later; the next call to
8853 read_char will return it.
8855 If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
8856 from the selected window's buffer. */
8858 static int
8859 read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt,
8860 int dont_downcase_last, int can_return_switch_frame,
8861 int fix_current_buffer)
8863 Lisp_Object from_string;
8864 int count = SPECPDL_INDEX ();
8866 /* How many keys there are in the current key sequence. */
8867 int t;
8869 /* The length of the echo buffer when we started reading, and
8870 the length of this_command_keys when we started reading. */
8871 int echo_start IF_LINT (= 0);
8872 int keys_start;
8874 /* The number of keymaps we're scanning right now, and the number of
8875 keymaps we have allocated space for. */
8876 int nmaps;
8877 int nmaps_allocated = 0;
8879 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
8880 the current keymaps. */
8881 Lisp_Object *defs = NULL;
8883 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
8884 in the current keymaps, or nil where it is not a prefix. */
8885 Lisp_Object *submaps = NULL;
8887 /* The local map to start out with at start of key sequence. */
8888 Lisp_Object orig_local_map;
8890 /* The map from the `keymap' property to start out with at start of
8891 key sequence. */
8892 Lisp_Object orig_keymap;
8894 /* 1 if we have already considered switching to the local-map property
8895 of the place where a mouse click occurred. */
8896 int localized_local_map = 0;
8898 /* The index in submaps[] of the first keymap that has a binding for
8899 this key sequence. In other words, the lowest i such that
8900 submaps[i] is non-nil. */
8901 int first_binding;
8902 /* Index of the first key that has no binding.
8903 It is useless to try fkey.start larger than that. */
8904 int first_unbound;
8906 /* If t < mock_input, then KEYBUF[t] should be read as the next
8907 input key.
8909 We use this to recover after recognizing a function key. Once we
8910 realize that a suffix of the current key sequence is actually a
8911 function key's escape sequence, we replace the suffix with the
8912 function key's binding from Vfunction_key_map. Now keybuf
8913 contains a new and different key sequence, so the echo area,
8914 this_command_keys, and the submaps and defs arrays are wrong. In
8915 this situation, we set mock_input to t, set t to 0, and jump to
8916 restart_sequence; the loop will read keys from keybuf up until
8917 mock_input, thus rebuilding the state; and then it will resume
8918 reading characters from the keyboard. */
8919 int mock_input = 0;
8921 /* If the sequence is unbound in submaps[], then
8922 keybuf[fkey.start..fkey.end-1] is a prefix in Vfunction_key_map,
8923 and fkey.map is its binding.
8925 These might be > t, indicating that all function key scanning
8926 should hold off until t reaches them. We do this when we've just
8927 recognized a function key, to avoid searching for the function
8928 key's again in Vfunction_key_map. */
8929 keyremap fkey;
8931 /* Likewise, for key_translation_map and input-decode-map. */
8932 keyremap keytran, indec;
8934 /* Non-zero if we are trying to map a key by changing an upper-case
8935 letter to lower case, or a shifted function key to an unshifted
8936 one. */
8937 int shift_translated = 0;
8939 /* If we receive a `switch-frame' or `select-window' event in the middle of
8940 a key sequence, we put it off for later.
8941 While we're reading, we keep the event here. */
8942 Lisp_Object delayed_switch_frame;
8944 /* See the comment below... */
8945 #if defined (GOBBLE_FIRST_EVENT)
8946 Lisp_Object first_event;
8947 #endif
8949 Lisp_Object original_uppercase IF_LINT (= Qnil);
8950 int original_uppercase_position = -1;
8952 /* Gets around Microsoft compiler limitations. */
8953 int dummyflag = 0;
8955 struct buffer *starting_buffer;
8957 /* List of events for which a fake prefix key has been generated. */
8958 Lisp_Object fake_prefixed_keys = Qnil;
8960 #if defined (GOBBLE_FIRST_EVENT)
8961 int junk;
8962 #endif
8964 struct gcpro outer_gcpro1;
8966 GCPRO1_VAR (fake_prefixed_keys, outer_gcpro);
8967 raw_keybuf_count = 0;
8969 last_nonmenu_event = Qnil;
8971 delayed_switch_frame = Qnil;
8973 if (INTERACTIVE)
8975 if (!NILP (prompt))
8977 /* Install the string STR as the beginning of the string of
8978 echoing, so that it serves as a prompt for the next
8979 character. */
8980 KVAR (current_kboard, echo_string) = prompt;
8981 current_kboard->echo_after_prompt = SCHARS (prompt);
8982 echo_now ();
8984 else if (cursor_in_echo_area
8985 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
8986 && NILP (Fzerop (Vecho_keystrokes)))
8987 /* This doesn't put in a dash if the echo buffer is empty, so
8988 you don't always see a dash hanging out in the minibuffer. */
8989 echo_dash ();
8992 /* Record the initial state of the echo area and this_command_keys;
8993 we will need to restore them if we replay a key sequence. */
8994 if (INTERACTIVE)
8995 echo_start = echo_length ();
8996 keys_start = this_command_key_count;
8997 this_single_command_key_start = keys_start;
8999 #if defined (GOBBLE_FIRST_EVENT)
9000 /* This doesn't quite work, because some of the things that read_char
9001 does cannot safely be bypassed. It seems too risky to try to make
9002 this work right. */
9004 /* Read the first char of the sequence specially, before setting
9005 up any keymaps, in case a filter runs and switches buffers on us. */
9006 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
9007 &junk, NULL);
9008 #endif /* GOBBLE_FIRST_EVENT */
9010 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
9011 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
9012 from_string = Qnil;
9014 /* We jump here when we need to reinitialize fkey and keytran; this
9015 happens if we switch keyboards between rescans. */
9016 replay_entire_sequence:
9018 indec.map = indec.parent = KVAR (current_kboard, Vinput_decode_map);
9019 fkey.map = fkey.parent = KVAR (current_kboard, Vlocal_function_key_map);
9020 keytran.map = keytran.parent = Vkey_translation_map;
9021 indec.start = indec.end = 0;
9022 fkey.start = fkey.end = 0;
9023 keytran.start = keytran.end = 0;
9025 /* We jump here when the key sequence has been thoroughly changed, and
9026 we need to rescan it starting from the beginning. When we jump here,
9027 keybuf[0..mock_input] holds the sequence we should reread. */
9028 replay_sequence:
9030 starting_buffer = current_buffer;
9031 first_unbound = bufsize + 1;
9033 /* Build our list of keymaps.
9034 If we recognize a function key and replace its escape sequence in
9035 keybuf with its symbol, or if the sequence starts with a mouse
9036 click and we need to switch buffers, we jump back here to rebuild
9037 the initial keymaps from the current buffer. */
9038 nmaps = 0;
9040 if (!NILP (KVAR (current_kboard, Voverriding_terminal_local_map)))
9042 if (2 > nmaps_allocated)
9044 submaps = (Lisp_Object *) alloca (2 * sizeof (submaps[0]));
9045 defs = (Lisp_Object *) alloca (2 * sizeof (defs[0]));
9046 nmaps_allocated = 2;
9048 submaps[nmaps++] = KVAR (current_kboard, Voverriding_terminal_local_map);
9050 else if (!NILP (Voverriding_local_map))
9052 if (2 > nmaps_allocated)
9054 submaps = (Lisp_Object *) alloca (2 * sizeof (submaps[0]));
9055 defs = (Lisp_Object *) alloca (2 * sizeof (defs[0]));
9056 nmaps_allocated = 2;
9058 submaps[nmaps++] = Voverriding_local_map;
9060 else
9062 int nminor;
9063 int total;
9064 Lisp_Object *maps;
9066 nminor = current_minor_maps (0, &maps);
9067 total = nminor + (!NILP (orig_keymap) ? 3 : 2);
9069 if (total > nmaps_allocated)
9071 submaps = (Lisp_Object *) alloca (total * sizeof (submaps[0]));
9072 defs = (Lisp_Object *) alloca (total * sizeof (defs[0]));
9073 nmaps_allocated = total;
9076 if (!NILP (orig_keymap))
9077 submaps[nmaps++] = orig_keymap;
9079 memcpy (submaps + nmaps, maps, nminor * sizeof (submaps[0]));
9081 nmaps += nminor;
9083 submaps[nmaps++] = orig_local_map;
9085 submaps[nmaps++] = current_global_map;
9087 /* Find an accurate initial value for first_binding. */
9088 for (first_binding = 0; first_binding < nmaps; first_binding++)
9089 if (! NILP (submaps[first_binding]))
9090 break;
9092 /* Start from the beginning in keybuf. */
9093 t = 0;
9095 /* These are no-ops the first time through, but if we restart, they
9096 revert the echo area and this_command_keys to their original state. */
9097 this_command_key_count = keys_start;
9098 if (INTERACTIVE && t < mock_input)
9099 echo_truncate (echo_start);
9101 /* If the best binding for the current key sequence is a keymap, or
9102 we may be looking at a function key's escape sequence, keep on
9103 reading. */
9104 while (first_binding < nmaps
9105 /* Keep reading as long as there's a prefix binding. */
9106 ? !NILP (submaps[first_binding])
9107 /* Don't return in the middle of a possible function key sequence,
9108 if the only bindings we found were via case conversion.
9109 Thus, if ESC O a has a function-key-map translation
9110 and ESC o has a binding, don't return after ESC O,
9111 so that we can translate ESC O plus the next character. */
9112 : (/* indec.start < t || fkey.start < t || */ keytran.start < t))
9114 Lisp_Object key;
9115 int used_mouse_menu = 0;
9117 /* Where the last real key started. If we need to throw away a
9118 key that has expanded into more than one element of keybuf
9119 (say, a mouse click on the mode line which is being treated
9120 as [mode-line (mouse-...)], then we backtrack to this point
9121 of keybuf. */
9122 int last_real_key_start;
9124 /* These variables are analogous to echo_start and keys_start;
9125 while those allow us to restart the entire key sequence,
9126 echo_local_start and keys_local_start allow us to throw away
9127 just one key. */
9128 int echo_local_start IF_LINT (= 0);
9129 int keys_local_start, local_first_binding;
9131 eassert (indec.end == t || (indec.end > t && indec.end <= mock_input));
9132 eassert (indec.start <= indec.end);
9133 eassert (fkey.start <= fkey.end);
9134 eassert (keytran.start <= keytran.end);
9135 /* key-translation-map is applied *after* function-key-map
9136 which is itself applied *after* input-decode-map. */
9137 eassert (fkey.end <= indec.start);
9138 eassert (keytran.end <= fkey.start);
9140 if (/* first_unbound < indec.start && first_unbound < fkey.start && */
9141 first_unbound < keytran.start)
9142 { /* The prefix upto first_unbound has no binding and has
9143 no translation left to do either, so we know it's unbound.
9144 If we don't stop now, we risk staying here indefinitely
9145 (if the user keeps entering fkey or keytran prefixes
9146 like C-c ESC ESC ESC ESC ...) */
9147 int i;
9148 for (i = first_unbound + 1; i < t; i++)
9149 keybuf[i - first_unbound - 1] = keybuf[i];
9150 mock_input = t - first_unbound - 1;
9151 indec.end = indec.start -= first_unbound + 1;
9152 indec.map = indec.parent;
9153 fkey.end = fkey.start -= first_unbound + 1;
9154 fkey.map = fkey.parent;
9155 keytran.end = keytran.start -= first_unbound + 1;
9156 keytran.map = keytran.parent;
9157 goto replay_sequence;
9160 if (t >= bufsize)
9161 error ("Key sequence too long");
9163 if (INTERACTIVE)
9164 echo_local_start = echo_length ();
9165 keys_local_start = this_command_key_count;
9166 local_first_binding = first_binding;
9168 replay_key:
9169 /* These are no-ops, unless we throw away a keystroke below and
9170 jumped back up to replay_key; in that case, these restore the
9171 variables to their original state, allowing us to replay the
9172 loop. */
9173 if (INTERACTIVE && t < mock_input)
9174 echo_truncate (echo_local_start);
9175 this_command_key_count = keys_local_start;
9176 first_binding = local_first_binding;
9178 /* By default, assume each event is "real". */
9179 last_real_key_start = t;
9181 /* Does mock_input indicate that we are re-reading a key sequence? */
9182 if (t < mock_input)
9184 key = keybuf[t];
9185 add_command_key (key);
9186 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
9187 && NILP (Fzerop (Vecho_keystrokes)))
9188 echo_char (key);
9191 /* If not, we should actually read a character. */
9192 else
9195 KBOARD *interrupted_kboard = current_kboard;
9196 struct frame *interrupted_frame = SELECTED_FRAME ();
9197 key = read_char (NILP (prompt), nmaps,
9198 (Lisp_Object *) submaps, last_nonmenu_event,
9199 &used_mouse_menu, NULL);
9200 if ((INTEGERP (key) && XINT (key) == -2) /* wrong_kboard_jmpbuf */
9201 /* When switching to a new tty (with a new keyboard),
9202 read_char returns the new buffer, rather than -2
9203 (Bug#5095). This is because `terminal-init-xterm'
9204 calls read-char, which eats the wrong_kboard_jmpbuf
9205 return. Any better way to fix this? -- cyd */
9206 || (interrupted_kboard != current_kboard))
9208 int found = 0;
9209 struct kboard *k;
9211 for (k = all_kboards; k; k = k->next_kboard)
9212 if (k == interrupted_kboard)
9213 found = 1;
9215 if (!found)
9217 /* Don't touch interrupted_kboard when it's been
9218 deleted. */
9219 delayed_switch_frame = Qnil;
9220 goto replay_entire_sequence;
9223 if (!NILP (delayed_switch_frame))
9225 KVAR (interrupted_kboard, kbd_queue)
9226 = Fcons (delayed_switch_frame,
9227 KVAR (interrupted_kboard, kbd_queue));
9228 delayed_switch_frame = Qnil;
9231 while (t > 0)
9232 KVAR (interrupted_kboard, kbd_queue)
9233 = Fcons (keybuf[--t], KVAR (interrupted_kboard, kbd_queue));
9235 /* If the side queue is non-empty, ensure it begins with a
9236 switch-frame, so we'll replay it in the right context. */
9237 if (CONSP (KVAR (interrupted_kboard, kbd_queue))
9238 && (key = XCAR (KVAR (interrupted_kboard, kbd_queue)),
9239 !(EVENT_HAS_PARAMETERS (key)
9240 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
9241 Qswitch_frame))))
9243 Lisp_Object frame;
9244 XSETFRAME (frame, interrupted_frame);
9245 KVAR (interrupted_kboard, kbd_queue)
9246 = Fcons (make_lispy_switch_frame (frame),
9247 KVAR (interrupted_kboard, kbd_queue));
9249 mock_input = 0;
9250 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
9251 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
9252 goto replay_entire_sequence;
9256 /* read_char returns t when it shows a menu and the user rejects it.
9257 Just return -1. */
9258 if (EQ (key, Qt))
9260 unbind_to (count, Qnil);
9261 UNGCPRO_VAR (outer_gcpro);
9262 return -1;
9265 /* read_char returns -1 at the end of a macro.
9266 Emacs 18 handles this by returning immediately with a
9267 zero, so that's what we'll do. */
9268 if (INTEGERP (key) && XINT (key) == -1)
9270 t = 0;
9271 /* The Microsoft C compiler can't handle the goto that
9272 would go here. */
9273 dummyflag = 1;
9274 break;
9277 /* If the current buffer has been changed from under us, the
9278 keymap may have changed, so replay the sequence. */
9279 if (BUFFERP (key))
9281 timer_resume_idle ();
9283 mock_input = t;
9284 /* Reset the current buffer from the selected window
9285 in case something changed the former and not the latter.
9286 This is to be more consistent with the behavior
9287 of the command_loop_1. */
9288 if (fix_current_buffer)
9290 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9291 Fkill_emacs (Qnil);
9292 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
9293 Fset_buffer (XWINDOW (selected_window)->buffer);
9296 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
9297 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
9298 goto replay_sequence;
9301 /* If we have a quit that was typed in another frame, and
9302 quit_throw_to_read_char switched buffers,
9303 replay to get the right keymap. */
9304 if (INTEGERP (key)
9305 && XINT (key) == quit_char
9306 && current_buffer != starting_buffer)
9308 GROW_RAW_KEYBUF;
9309 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
9310 keybuf[t++] = key;
9311 mock_input = t;
9312 Vquit_flag = Qnil;
9313 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
9314 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
9315 goto replay_sequence;
9318 Vquit_flag = Qnil;
9320 if (EVENT_HAS_PARAMETERS (key)
9321 /* Either a `switch-frame' or a `select-window' event. */
9322 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), Qswitch_frame))
9324 /* If we're at the beginning of a key sequence, and the caller
9325 says it's okay, go ahead and return this event. If we're
9326 in the midst of a key sequence, delay it until the end. */
9327 if (t > 0 || !can_return_switch_frame)
9329 delayed_switch_frame = key;
9330 goto replay_key;
9334 GROW_RAW_KEYBUF;
9335 ASET (raw_keybuf, raw_keybuf_count, key);
9336 raw_keybuf_count++;
9339 /* Clicks in non-text areas get prefixed by the symbol
9340 in their CHAR-ADDRESS field. For example, a click on
9341 the mode line is prefixed by the symbol `mode-line'.
9343 Furthermore, key sequences beginning with mouse clicks
9344 are read using the keymaps of the buffer clicked on, not
9345 the current buffer. So we may have to switch the buffer
9346 here.
9348 When we turn one event into two events, we must make sure
9349 that neither of the two looks like the original--so that,
9350 if we replay the events, they won't be expanded again.
9351 If not for this, such reexpansion could happen either here
9352 or when user programs play with this-command-keys. */
9353 if (EVENT_HAS_PARAMETERS (key))
9355 Lisp_Object kind;
9356 Lisp_Object string;
9358 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
9359 if (EQ (kind, Qmouse_click))
9361 Lisp_Object window, posn;
9363 window = POSN_WINDOW (EVENT_START (key));
9364 posn = POSN_POSN (EVENT_START (key));
9366 if (CONSP (posn)
9367 || (!NILP (fake_prefixed_keys)
9368 && !NILP (Fmemq (key, fake_prefixed_keys))))
9370 /* We're looking a second time at an event for which
9371 we generated a fake prefix key. Set
9372 last_real_key_start appropriately. */
9373 if (t > 0)
9374 last_real_key_start = t - 1;
9377 /* Key sequences beginning with mouse clicks are
9378 read using the keymaps in the buffer clicked on,
9379 not the current buffer. If we're at the
9380 beginning of a key sequence, switch buffers. */
9381 if (last_real_key_start == 0
9382 && WINDOWP (window)
9383 && BUFFERP (XWINDOW (window)->buffer)
9384 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
9386 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
9387 keybuf[t] = key;
9388 mock_input = t + 1;
9390 /* Arrange to go back to the original buffer once we're
9391 done reading the key sequence. Note that we can't
9392 use save_excursion_{save,restore} here, because they
9393 save point as well as the current buffer; we don't
9394 want to save point, because redisplay may change it,
9395 to accommodate a Fset_window_start or something. We
9396 don't want to do this at the top of the function,
9397 because we may get input from a subprocess which
9398 wants to change the selected window and stuff (say,
9399 emacsclient). */
9400 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
9402 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9403 Fkill_emacs (Qnil);
9404 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
9405 orig_local_map = get_local_map (PT, current_buffer,
9406 Qlocal_map);
9407 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
9408 goto replay_sequence;
9411 /* For a mouse click, get the local text-property keymap
9412 of the place clicked on, rather than point. */
9413 if (last_real_key_start == 0
9414 && CONSP (XCDR (key))
9415 && ! localized_local_map)
9417 Lisp_Object map_here, start, pos;
9419 localized_local_map = 1;
9420 start = EVENT_START (key);
9422 if (CONSP (start) && POSN_INBUFFER_P (start))
9424 pos = POSN_BUFFER_POSN (start);
9425 if (INTEGERP (pos)
9426 && XINT (pos) >= BEGV
9427 && XINT (pos) <= ZV)
9429 map_here = get_local_map (XINT (pos),
9430 current_buffer, Qlocal_map);
9431 if (!EQ (map_here, orig_local_map))
9433 orig_local_map = map_here;
9434 ++localized_local_map;
9437 map_here = get_local_map (XINT (pos),
9438 current_buffer, Qkeymap);
9439 if (!EQ (map_here, orig_keymap))
9441 orig_keymap = map_here;
9442 ++localized_local_map;
9445 if (localized_local_map > 1)
9447 keybuf[t] = key;
9448 mock_input = t + 1;
9450 goto replay_sequence;
9456 /* Expand mode-line and scroll-bar events into two events:
9457 use posn as a fake prefix key. */
9458 if (SYMBOLP (posn)
9459 && (NILP (fake_prefixed_keys)
9460 || NILP (Fmemq (key, fake_prefixed_keys))))
9462 if (t + 1 >= bufsize)
9463 error ("Key sequence too long");
9465 keybuf[t] = posn;
9466 keybuf[t + 1] = key;
9467 mock_input = t + 2;
9469 /* Record that a fake prefix key has been generated
9470 for KEY. Don't modify the event; this would
9471 prevent proper action when the event is pushed
9472 back into unread-command-events. */
9473 fake_prefixed_keys = Fcons (key, fake_prefixed_keys);
9475 /* If on a mode line string with a local keymap,
9476 reconsider the key sequence with that keymap. */
9477 if (string = POSN_STRING (EVENT_START (key)),
9478 (CONSP (string) && STRINGP (XCAR (string))))
9480 Lisp_Object pos, map, map2;
9482 pos = XCDR (string);
9483 string = XCAR (string);
9484 if (XINT (pos) >= 0
9485 && XINT (pos) < SCHARS (string))
9487 map = Fget_text_property (pos, Qlocal_map, string);
9488 if (!NILP (map))
9489 orig_local_map = map;
9490 map2 = Fget_text_property (pos, Qkeymap, string);
9491 if (!NILP (map2))
9492 orig_keymap = map2;
9493 if (!NILP (map) || !NILP (map2))
9494 goto replay_sequence;
9498 goto replay_key;
9500 else if (NILP (from_string)
9501 && (string = POSN_STRING (EVENT_START (key)),
9502 (CONSP (string) && STRINGP (XCAR (string)))))
9504 /* For a click on a string, i.e. overlay string or a
9505 string displayed via the `display' property,
9506 consider `local-map' and `keymap' properties of
9507 that string. */
9508 Lisp_Object pos, map, map2;
9510 pos = XCDR (string);
9511 string = XCAR (string);
9512 if (XINT (pos) >= 0
9513 && XINT (pos) < SCHARS (string))
9515 map = Fget_text_property (pos, Qlocal_map, string);
9516 if (!NILP (map))
9517 orig_local_map = map;
9518 map2 = Fget_text_property (pos, Qkeymap, string);
9519 if (!NILP (map2))
9520 orig_keymap = map2;
9522 if (!NILP (map) || !NILP (map2))
9524 from_string = string;
9525 keybuf[t++] = key;
9526 mock_input = t;
9527 goto replay_sequence;
9532 else if (CONSP (XCDR (key))
9533 && CONSP (EVENT_START (key))
9534 && CONSP (XCDR (EVENT_START (key))))
9536 Lisp_Object posn;
9538 posn = POSN_POSN (EVENT_START (key));
9539 /* Handle menu-bar events:
9540 insert the dummy prefix event `menu-bar'. */
9541 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
9543 if (t + 1 >= bufsize)
9544 error ("Key sequence too long");
9545 keybuf[t] = posn;
9546 keybuf[t+1] = key;
9548 /* Zap the position in key, so we know that we've
9549 expanded it, and don't try to do so again. */
9550 POSN_SET_POSN (EVENT_START (key),
9551 Fcons (posn, Qnil));
9553 mock_input = t + 2;
9554 goto replay_sequence;
9556 else if (CONSP (posn))
9558 /* We're looking at the second event of a
9559 sequence which we expanded before. Set
9560 last_real_key_start appropriately. */
9561 if (last_real_key_start == t && t > 0)
9562 last_real_key_start = t - 1;
9567 /* We have finally decided that KEY is something we might want
9568 to look up. */
9569 first_binding = (follow_key (key,
9570 nmaps - first_binding,
9571 submaps + first_binding,
9572 defs + first_binding,
9573 submaps + first_binding)
9574 + first_binding);
9576 /* If KEY wasn't bound, we'll try some fallbacks. */
9577 if (first_binding < nmaps)
9578 /* This is needed for the following scenario:
9579 event 0: a down-event that gets dropped by calling replay_key.
9580 event 1: some normal prefix like C-h.
9581 After event 0, first_unbound is 0, after event 1 indec.start,
9582 fkey.start, and keytran.start are all 1, so when we see that
9583 C-h is bound, we need to update first_unbound. */
9584 first_unbound = max (t + 1, first_unbound);
9585 else
9587 Lisp_Object head;
9589 /* Remember the position to put an upper bound on indec.start. */
9590 first_unbound = min (t, first_unbound);
9592 head = EVENT_HEAD (key);
9593 if (help_char_p (head) && t > 0)
9595 read_key_sequence_cmd = Vprefix_help_command;
9596 keybuf[t++] = key;
9597 last_nonmenu_event = key;
9598 /* The Microsoft C compiler can't handle the goto that
9599 would go here. */
9600 dummyflag = 1;
9601 break;
9604 if (SYMBOLP (head))
9606 Lisp_Object breakdown;
9607 int modifiers;
9609 breakdown = parse_modifiers (head);
9610 modifiers = XINT (XCAR (XCDR (breakdown)));
9611 /* Attempt to reduce an unbound mouse event to a simpler
9612 event that is bound:
9613 Drags reduce to clicks.
9614 Double-clicks reduce to clicks.
9615 Triple-clicks reduce to double-clicks, then to clicks.
9616 Down-clicks are eliminated.
9617 Double-downs reduce to downs, then are eliminated.
9618 Triple-downs reduce to double-downs, then to downs,
9619 then are eliminated. */
9620 if (modifiers & (down_modifier | drag_modifier
9621 | double_modifier | triple_modifier))
9623 while (modifiers & (down_modifier | drag_modifier
9624 | double_modifier | triple_modifier))
9626 Lisp_Object new_head, new_click;
9627 if (modifiers & triple_modifier)
9628 modifiers ^= (double_modifier | triple_modifier);
9629 else if (modifiers & double_modifier)
9630 modifiers &= ~double_modifier;
9631 else if (modifiers & drag_modifier)
9632 modifiers &= ~drag_modifier;
9633 else
9635 /* Dispose of this `down' event by simply jumping
9636 back to replay_key, to get another event.
9638 Note that if this event came from mock input,
9639 then just jumping back to replay_key will just
9640 hand it to us again. So we have to wipe out any
9641 mock input.
9643 We could delete keybuf[t] and shift everything
9644 after that to the left by one spot, but we'd also
9645 have to fix up any variable that points into
9646 keybuf, and shifting isn't really necessary
9647 anyway.
9649 Adding prefixes for non-textual mouse clicks
9650 creates two characters of mock input, and both
9651 must be thrown away. If we're only looking at
9652 the prefix now, we can just jump back to
9653 replay_key. On the other hand, if we've already
9654 processed the prefix, and now the actual click
9655 itself is giving us trouble, then we've lost the
9656 state of the keymaps we want to backtrack to, and
9657 we need to replay the whole sequence to rebuild
9660 Beyond that, only function key expansion could
9661 create more than two keys, but that should never
9662 generate mouse events, so it's okay to zero
9663 mock_input in that case too.
9665 FIXME: The above paragraph seems just plain
9666 wrong, if you consider things like
9667 xterm-mouse-mode. -stef
9669 Isn't this just the most wonderful code ever? */
9671 /* If mock_input > t + 1, the above simplification
9672 will actually end up dropping keys on the floor.
9673 This is probably OK for now, but even
9674 if mock_input <= t + 1, we need to adjust indec,
9675 fkey, and keytran.
9676 Typical case [header-line down-mouse-N]:
9677 mock_input = 2, t = 1, fkey.end = 1,
9678 last_real_key_start = 0. */
9679 if (indec.end > last_real_key_start)
9681 indec.end = indec.start
9682 = min (last_real_key_start, indec.start);
9683 indec.map = indec.parent;
9684 if (fkey.end > last_real_key_start)
9686 fkey.end = fkey.start
9687 = min (last_real_key_start, fkey.start);
9688 fkey.map = fkey.parent;
9689 if (keytran.end > last_real_key_start)
9691 keytran.end = keytran.start
9692 = min (last_real_key_start, keytran.start);
9693 keytran.map = keytran.parent;
9697 if (t == last_real_key_start)
9699 mock_input = 0;
9700 goto replay_key;
9702 else
9704 mock_input = last_real_key_start;
9705 goto replay_sequence;
9709 new_head
9710 = apply_modifiers (modifiers, XCAR (breakdown));
9711 new_click
9712 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
9714 /* Look for a binding for this new key. follow_key
9715 promises that it didn't munge submaps the
9716 last time we called it, since key was unbound. */
9717 first_binding
9718 = (follow_key (new_click,
9719 nmaps - local_first_binding,
9720 submaps + local_first_binding,
9721 defs + local_first_binding,
9722 submaps + local_first_binding)
9723 + local_first_binding);
9725 /* If that click is bound, go for it. */
9726 if (first_binding < nmaps)
9728 key = new_click;
9729 break;
9731 /* Otherwise, we'll leave key set to the drag event. */
9737 keybuf[t++] = key;
9738 /* Normally, last_nonmenu_event gets the previous key we read.
9739 But when a mouse popup menu is being used,
9740 we don't update last_nonmenu_event; it continues to hold the mouse
9741 event that preceded the first level of menu. */
9742 if (!used_mouse_menu)
9743 last_nonmenu_event = key;
9745 /* Record what part of this_command_keys is the current key sequence. */
9746 this_single_command_key_start = this_command_key_count - t;
9748 /* Look for this sequence in input-decode-map.
9749 Scan from indec.end until we find a bound suffix. */
9750 while (indec.end < t)
9752 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9753 int done, diff;
9755 GCPRO4 (indec.map, fkey.map, keytran.map, delayed_switch_frame);
9756 done = keyremap_step (keybuf, bufsize, &indec, max (t, mock_input),
9757 1, &diff, prompt);
9758 UNGCPRO;
9759 if (done)
9761 mock_input = diff + max (t, mock_input);
9762 goto replay_sequence;
9766 if (first_binding < nmaps && NILP (submaps[first_binding])
9767 && indec.start >= t)
9768 /* There is a binding and it's not a prefix.
9769 (and it doesn't have any input-decode-map translation pending).
9770 There is thus no function-key in this sequence.
9771 Moving fkey.start is important in this case to allow keytran.start
9772 to go over the sequence before we return (since we keep the
9773 invariant that keytran.end <= fkey.start). */
9775 if (fkey.start < t)
9776 (fkey.start = fkey.end = t, fkey.map = fkey.parent);
9778 else
9779 /* If the sequence is unbound, see if we can hang a function key
9780 off the end of it. */
9781 /* Continue scan from fkey.end until we find a bound suffix. */
9782 while (fkey.end < indec.start)
9784 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9785 int done, diff;
9787 GCPRO4 (indec.map, fkey.map, keytran.map, delayed_switch_frame);
9788 done = keyremap_step (keybuf, bufsize, &fkey,
9789 max (t, mock_input),
9790 /* If there's a binding (i.e.
9791 first_binding >= nmaps) we don't want
9792 to apply this function-key-mapping. */
9793 fkey.end + 1 == t && first_binding >= nmaps,
9794 &diff, prompt);
9795 UNGCPRO;
9796 if (done)
9798 mock_input = diff + max (t, mock_input);
9799 /* Adjust the input-decode-map counters. */
9800 indec.end += diff;
9801 indec.start += diff;
9803 goto replay_sequence;
9807 /* Look for this sequence in key-translation-map.
9808 Scan from keytran.end until we find a bound suffix. */
9809 while (keytran.end < fkey.start)
9811 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9812 int done, diff;
9814 GCPRO4 (indec.map, fkey.map, keytran.map, delayed_switch_frame);
9815 done = keyremap_step (keybuf, bufsize, &keytran, max (t, mock_input),
9816 1, &diff, prompt);
9817 UNGCPRO;
9818 if (done)
9820 mock_input = diff + max (t, mock_input);
9821 /* Adjust the function-key-map and input-decode-map counters. */
9822 indec.end += diff;
9823 indec.start += diff;
9824 fkey.end += diff;
9825 fkey.start += diff;
9827 goto replay_sequence;
9831 /* If KEY is not defined in any of the keymaps,
9832 and cannot be part of a function key or translation,
9833 and is an upper case letter
9834 use the corresponding lower-case letter instead. */
9835 if (first_binding >= nmaps
9836 && /* indec.start >= t && fkey.start >= t && */ keytran.start >= t
9837 && INTEGERP (key)
9838 && ((CHARACTERP (make_number (XINT (key) & ~CHAR_MODIFIER_MASK))
9839 && uppercasep (XINT (key) & ~CHAR_MODIFIER_MASK))
9840 || (XINT (key) & shift_modifier)))
9842 Lisp_Object new_key;
9844 original_uppercase = key;
9845 original_uppercase_position = t - 1;
9847 if (XINT (key) & shift_modifier)
9848 XSETINT (new_key, XINT (key) & ~shift_modifier);
9849 else
9850 XSETINT (new_key, (downcase (XINT (key) & ~CHAR_MODIFIER_MASK)
9851 | (XINT (key) & CHAR_MODIFIER_MASK)));
9853 /* We have to do this unconditionally, regardless of whether
9854 the lower-case char is defined in the keymaps, because they
9855 might get translated through function-key-map. */
9856 keybuf[t - 1] = new_key;
9857 mock_input = max (t, mock_input);
9858 shift_translated = 1;
9860 goto replay_sequence;
9862 /* If KEY is not defined in any of the keymaps,
9863 and cannot be part of a function key or translation,
9864 and is a shifted function key,
9865 use the corresponding unshifted function key instead. */
9866 if (first_binding >= nmaps
9867 && /* indec.start >= t && fkey.start >= t && */ keytran.start >= t)
9869 Lisp_Object breakdown = parse_modifiers (key);
9870 int modifiers
9871 = CONSP (breakdown) ? (XINT (XCAR (XCDR (breakdown)))) : 0;
9873 if (modifiers & shift_modifier
9874 /* Treat uppercase keys as shifted. */
9875 || (INTEGERP (key)
9876 && (KEY_TO_CHAR (key)
9877 < XCHAR_TABLE (BVAR (current_buffer, downcase_table))->size)
9878 && uppercasep (KEY_TO_CHAR (key))))
9880 Lisp_Object new_key
9881 = (modifiers & shift_modifier
9882 ? apply_modifiers (modifiers & ~shift_modifier,
9883 XCAR (breakdown))
9884 : make_number (downcase (KEY_TO_CHAR (key)) | modifiers));
9886 original_uppercase = key;
9887 original_uppercase_position = t - 1;
9889 /* We have to do this unconditionally, regardless of whether
9890 the lower-case char is defined in the keymaps, because they
9891 might get translated through function-key-map. */
9892 keybuf[t - 1] = new_key;
9893 mock_input = max (t, mock_input);
9894 /* Reset fkey (and consequently keytran) to apply
9895 function-key-map on the result, so that S-backspace is
9896 correctly mapped to DEL (via backspace). OTOH,
9897 input-decode-map doesn't need to go through it again. */
9898 fkey.start = fkey.end = 0;
9899 keytran.start = keytran.end = 0;
9900 shift_translated = 1;
9902 goto replay_sequence;
9906 if (!dummyflag)
9907 read_key_sequence_cmd = (first_binding < nmaps
9908 ? defs[first_binding]
9909 : Qnil);
9911 unread_switch_frame = delayed_switch_frame;
9912 unbind_to (count, Qnil);
9914 /* Don't downcase the last character if the caller says don't.
9915 Don't downcase it if the result is undefined, either. */
9916 if ((dont_downcase_last || first_binding >= nmaps)
9917 && t > 0
9918 && t - 1 == original_uppercase_position)
9920 keybuf[t - 1] = original_uppercase;
9921 shift_translated = 0;
9924 if (shift_translated)
9925 Vthis_command_keys_shift_translated = Qt;
9927 /* Occasionally we fabricate events, perhaps by expanding something
9928 according to function-key-map, or by adding a prefix symbol to a
9929 mouse click in the scroll bar or modeline. In this cases, return
9930 the entire generated key sequence, even if we hit an unbound
9931 prefix or a definition before the end. This means that you will
9932 be able to push back the event properly, and also means that
9933 read-key-sequence will always return a logical unit.
9935 Better ideas? */
9936 for (; t < mock_input; t++)
9938 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
9939 && NILP (Fzerop (Vecho_keystrokes)))
9940 echo_char (keybuf[t]);
9941 add_command_key (keybuf[t]);
9944 UNGCPRO_VAR (outer_gcpro);
9945 return t;
9948 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 5, 0,
9949 doc: /* Read a sequence of keystrokes and return as a string or vector.
9950 The sequence is sufficient to specify a non-prefix command in the
9951 current local and global maps.
9953 First arg PROMPT is a prompt string. If nil, do not prompt specially.
9954 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos
9955 as a continuation of the previous key.
9957 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not
9958 convert the last event to lower case. (Normally any upper case event
9959 is converted to lower case if the original event is undefined and the lower
9960 case equivalent is defined.) A non-nil value is appropriate for reading
9961 a key sequence to be defined.
9963 A C-g typed while in this function is treated like any other character,
9964 and `quit-flag' is not set.
9966 If the key sequence starts with a mouse click, then the sequence is read
9967 using the keymaps of the buffer of the window clicked in, not the buffer
9968 of the selected window as normal.
9970 `read-key-sequence' drops unbound button-down events, since you normally
9971 only care about the click or drag events which follow them. If a drag
9972 or multi-click event is unbound, but the corresponding click event would
9973 be bound, `read-key-sequence' turns the event into a click event at the
9974 drag's starting position. This means that you don't have to distinguish
9975 between click and drag, double, or triple events unless you want to.
9977 `read-key-sequence' prefixes mouse events on mode lines, the vertical
9978 lines separating windows, and scroll bars with imaginary keys
9979 `mode-line', `vertical-line', and `vertical-scroll-bar'.
9981 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this
9982 function will process a switch-frame event if the user switches frames
9983 before typing anything. If the user switches frames in the middle of a
9984 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME
9985 is nil, then the event will be put off until after the current key sequence.
9987 `read-key-sequence' checks `function-key-map' for function key
9988 sequences, where they wouldn't conflict with ordinary bindings. See
9989 `function-key-map' for more details.
9991 The optional fifth argument CMD-LOOP, if non-nil, means
9992 that this key sequence is being read by something that will
9993 read commands one after another. It should be nil if the caller
9994 will read just one key sequence. */)
9995 (Lisp_Object prompt, Lisp_Object continue_echo, Lisp_Object dont_downcase_last, Lisp_Object can_return_switch_frame, Lisp_Object cmd_loop)
9997 Lisp_Object keybuf[30];
9998 register int i;
9999 struct gcpro gcpro1;
10000 int count = SPECPDL_INDEX ();
10002 if (!NILP (prompt))
10003 CHECK_STRING (prompt);
10004 QUIT;
10006 specbind (Qinput_method_exit_on_first_char,
10007 (NILP (cmd_loop) ? Qt : Qnil));
10008 specbind (Qinput_method_use_echo_area,
10009 (NILP (cmd_loop) ? Qt : Qnil));
10011 memset (keybuf, 0, sizeof keybuf);
10012 GCPRO1 (keybuf[0]);
10013 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
10015 if (NILP (continue_echo))
10017 this_command_key_count = 0;
10018 this_command_key_count_reset = 0;
10019 this_single_command_key_start = 0;
10022 #ifdef HAVE_WINDOW_SYSTEM
10023 if (display_hourglass_p)
10024 cancel_hourglass ();
10025 #endif
10027 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
10028 prompt, ! NILP (dont_downcase_last),
10029 ! NILP (can_return_switch_frame), 0);
10031 #if 0 /* The following is fine for code reading a key sequence and
10032 then proceeding with a lenghty computation, but it's not good
10033 for code reading keys in a loop, like an input method. */
10034 #ifdef HAVE_WINDOW_SYSTEM
10035 if (display_hourglass_p)
10036 start_hourglass ();
10037 #endif
10038 #endif
10040 if (i == -1)
10042 Vquit_flag = Qt;
10043 QUIT;
10045 UNGCPRO;
10046 return unbind_to (count, make_event_array (i, keybuf));
10049 DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
10050 Sread_key_sequence_vector, 1, 5, 0,
10051 doc: /* Like `read-key-sequence' but always return a vector. */)
10052 (Lisp_Object prompt, Lisp_Object continue_echo, Lisp_Object dont_downcase_last, Lisp_Object can_return_switch_frame, Lisp_Object cmd_loop)
10054 Lisp_Object keybuf[30];
10055 register int i;
10056 struct gcpro gcpro1;
10057 int count = SPECPDL_INDEX ();
10059 if (!NILP (prompt))
10060 CHECK_STRING (prompt);
10061 QUIT;
10063 specbind (Qinput_method_exit_on_first_char,
10064 (NILP (cmd_loop) ? Qt : Qnil));
10065 specbind (Qinput_method_use_echo_area,
10066 (NILP (cmd_loop) ? Qt : Qnil));
10068 memset (keybuf, 0, sizeof keybuf);
10069 GCPRO1 (keybuf[0]);
10070 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
10072 if (NILP (continue_echo))
10074 this_command_key_count = 0;
10075 this_command_key_count_reset = 0;
10076 this_single_command_key_start = 0;
10079 #ifdef HAVE_WINDOW_SYSTEM
10080 if (display_hourglass_p)
10081 cancel_hourglass ();
10082 #endif
10084 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
10085 prompt, ! NILP (dont_downcase_last),
10086 ! NILP (can_return_switch_frame), 0);
10088 #ifdef HAVE_WINDOW_SYSTEM
10089 if (display_hourglass_p)
10090 start_hourglass ();
10091 #endif
10093 if (i == -1)
10095 Vquit_flag = Qt;
10096 QUIT;
10098 UNGCPRO;
10099 return unbind_to (count, Fvector (i, keybuf));
10102 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
10103 doc: /* Execute CMD as an editor command.
10104 CMD must be a symbol that satisfies the `commandp' predicate.
10105 Optional second arg RECORD-FLAG non-nil
10106 means unconditionally put this command in `command-history'.
10107 Otherwise, that is done only if an arg is read using the minibuffer.
10108 The argument KEYS specifies the value to use instead of (this-command-keys)
10109 when reading the arguments; if it is nil, (this-command-keys) is used.
10110 The argument SPECIAL, if non-nil, means that this command is executing
10111 a special event, so ignore the prefix argument and don't clear it. */)
10112 (Lisp_Object cmd, Lisp_Object record_flag, Lisp_Object keys, Lisp_Object special)
10114 register Lisp_Object final;
10115 register Lisp_Object tem;
10116 Lisp_Object prefixarg;
10118 debug_on_next_call = 0;
10120 if (NILP (special))
10122 prefixarg = KVAR (current_kboard, Vprefix_arg);
10123 Vcurrent_prefix_arg = prefixarg;
10124 KVAR (current_kboard, Vprefix_arg) = Qnil;
10126 else
10127 prefixarg = Qnil;
10129 if (SYMBOLP (cmd))
10131 tem = Fget (cmd, Qdisabled);
10132 if (!NILP (tem) && !NILP (Vrun_hooks))
10134 tem = Fsymbol_value (Qdisabled_command_function);
10135 if (!NILP (tem))
10136 return call1 (Vrun_hooks, Qdisabled_command_function);
10140 while (1)
10142 final = Findirect_function (cmd, Qnil);
10144 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
10146 struct gcpro gcpro1, gcpro2;
10148 GCPRO2 (cmd, prefixarg);
10149 do_autoload (final, cmd);
10150 UNGCPRO;
10152 else
10153 break;
10156 if (STRINGP (final) || VECTORP (final))
10158 /* If requested, place the macro in the command history. For
10159 other sorts of commands, call-interactively takes care of
10160 this. */
10161 if (!NILP (record_flag))
10163 Vcommand_history
10164 = Fcons (Fcons (Qexecute_kbd_macro,
10165 Fcons (final, Fcons (prefixarg, Qnil))),
10166 Vcommand_history);
10168 /* Don't keep command history around forever. */
10169 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
10171 tem = Fnthcdr (Vhistory_length, Vcommand_history);
10172 if (CONSP (tem))
10173 XSETCDR (tem, Qnil);
10177 return Fexecute_kbd_macro (final, prefixarg, Qnil);
10180 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
10181 /* Don't call Fcall_interactively directly because we want to make
10182 sure the backtrace has an entry for `call-interactively'.
10183 For the same reason, pass `cmd' rather than `final'. */
10184 return call3 (Qcall_interactively, cmd, record_flag, keys);
10186 return Qnil;
10191 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
10192 1, 1, "P",
10193 doc: /* Read function name, then read its arguments and call it.
10195 To pass a numeric argument to the command you are invoking with, specify
10196 the numeric argument to this command.
10198 Noninteractively, the argument PREFIXARG is the prefix argument to
10199 give to the command you invoke, if it asks for an argument. */)
10200 (Lisp_Object prefixarg)
10202 Lisp_Object function;
10203 EMACS_INT saved_last_point_position;
10204 Lisp_Object saved_keys, saved_last_point_position_buffer;
10205 Lisp_Object bindings, value;
10206 struct gcpro gcpro1, gcpro2, gcpro3;
10207 #ifdef HAVE_WINDOW_SYSTEM
10208 /* The call to Fcompleting_read will start and cancel the hourglass,
10209 but if the hourglass was already scheduled, this means that no
10210 hourglass will be shown for the actual M-x command itself.
10211 So we restart it if it is already scheduled. Note that checking
10212 hourglass_shown_p is not enough, normally the hourglass is not shown,
10213 just scheduled to be shown. */
10214 int hstarted = hourglass_started ();
10215 #endif
10217 saved_keys = Fvector (this_command_key_count,
10218 XVECTOR (this_command_keys)->contents);
10219 saved_last_point_position_buffer = last_point_position_buffer;
10220 saved_last_point_position = last_point_position;
10221 GCPRO3 (saved_keys, prefixarg, saved_last_point_position_buffer);
10223 function = call0 (intern ("read-extended-command"));
10225 #ifdef HAVE_WINDOW_SYSTEM
10226 if (hstarted) start_hourglass ();
10227 #endif
10229 if (STRINGP (function) && SCHARS (function) == 0)
10230 error ("No command name given");
10232 /* Set this_command_keys to the concatenation of saved_keys and
10233 function, followed by a RET. */
10235 Lisp_Object *keys;
10236 int i;
10238 this_command_key_count = 0;
10239 this_command_key_count_reset = 0;
10240 this_single_command_key_start = 0;
10242 keys = XVECTOR (saved_keys)->contents;
10243 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
10244 add_command_key (keys[i]);
10246 for (i = 0; i < SCHARS (function); i++)
10247 add_command_key (Faref (function, make_number (i)));
10249 add_command_key (make_number ('\015'));
10252 last_point_position = saved_last_point_position;
10253 last_point_position_buffer = saved_last_point_position_buffer;
10255 UNGCPRO;
10257 function = Fintern (function, Qnil);
10258 KVAR (current_kboard, Vprefix_arg) = prefixarg;
10259 Vthis_command = function;
10260 real_this_command = function;
10262 /* If enabled, show which key runs this command. */
10263 if (!NILP (Vsuggest_key_bindings)
10264 && NILP (Vexecuting_kbd_macro)
10265 && SYMBOLP (function))
10266 bindings = Fwhere_is_internal (function, Voverriding_local_map,
10267 Qt, Qnil, Qnil);
10268 else
10269 bindings = Qnil;
10271 value = Qnil;
10272 GCPRO3 (bindings, value, function);
10273 value = Fcommand_execute (function, Qt, Qnil, Qnil);
10275 /* If the command has a key binding, print it now. */
10276 if (!NILP (bindings)
10277 && ! (VECTORP (bindings) && EQ (Faref (bindings, make_number (0)),
10278 Qmouse_movement)))
10280 /* But first wait, and skip the message if there is input. */
10281 Lisp_Object waited;
10283 /* If this command displayed something in the echo area;
10284 wait a few seconds, then display our suggestion message. */
10285 if (NILP (echo_area_buffer[0]))
10286 waited = sit_for (make_number (0), 0, 2);
10287 else if (NUMBERP (Vsuggest_key_bindings))
10288 waited = sit_for (Vsuggest_key_bindings, 0, 2);
10289 else
10290 waited = sit_for (make_number (2), 0, 2);
10292 if (!NILP (waited) && ! CONSP (Vunread_command_events))
10294 Lisp_Object binding;
10295 char *newmessage;
10296 int message_p = push_message ();
10297 int count = SPECPDL_INDEX ();
10299 record_unwind_protect (pop_message_unwind, Qnil);
10300 binding = Fkey_description (bindings, Qnil);
10302 newmessage
10303 = (char *) alloca (SCHARS (SYMBOL_NAME (function))
10304 + SBYTES (binding)
10305 + 100);
10306 sprintf (newmessage, "You can run the command `%s' with %s",
10307 SDATA (SYMBOL_NAME (function)),
10308 SDATA (binding));
10309 message2_nolog (newmessage,
10310 strlen (newmessage),
10311 STRING_MULTIBYTE (binding));
10312 if (NUMBERP (Vsuggest_key_bindings))
10313 waited = sit_for (Vsuggest_key_bindings, 0, 2);
10314 else
10315 waited = sit_for (make_number (2), 0, 2);
10317 if (!NILP (waited) && message_p)
10318 restore_message ();
10320 unbind_to (count, Qnil);
10324 RETURN_UNGCPRO (value);
10328 /* Return nonzero if input events are pending. */
10331 detect_input_pending (void)
10333 if (!input_pending)
10334 get_input_pending (&input_pending, 0);
10336 return input_pending;
10339 /* Return nonzero if input events other than mouse movements are
10340 pending. */
10343 detect_input_pending_ignore_squeezables (void)
10345 if (!input_pending)
10346 get_input_pending (&input_pending, READABLE_EVENTS_IGNORE_SQUEEZABLES);
10348 return input_pending;
10351 /* Return nonzero if input events are pending, and run any pending timers. */
10354 detect_input_pending_run_timers (int do_display)
10356 int old_timers_run = timers_run;
10358 if (!input_pending)
10359 get_input_pending (&input_pending, READABLE_EVENTS_DO_TIMERS_NOW);
10361 if (old_timers_run != timers_run && do_display)
10363 redisplay_preserve_echo_area (8);
10364 /* The following fixes a bug when using lazy-lock with
10365 lazy-lock-defer-on-the-fly set to t, i.e. when fontifying
10366 from an idle timer function. The symptom of the bug is that
10367 the cursor sometimes doesn't become visible until the next X
10368 event is processed. --gerd. */
10370 Lisp_Object tail, frame;
10371 FOR_EACH_FRAME (tail, frame)
10372 if (FRAME_RIF (XFRAME (frame)))
10373 FRAME_RIF (XFRAME (frame))->flush_display (XFRAME (frame));
10377 return input_pending;
10380 /* This is called in some cases before a possible quit.
10381 It cases the next call to detect_input_pending to recompute input_pending.
10382 So calling this function unnecessarily can't do any harm. */
10384 void
10385 clear_input_pending (void)
10387 input_pending = 0;
10390 /* Return nonzero if there are pending requeued events.
10391 This isn't used yet. The hope is to make wait_reading_process_output
10392 call it, and return if it runs Lisp code that unreads something.
10393 The problem is, kbd_buffer_get_event needs to be fixed to know what
10394 to do in that case. It isn't trivial. */
10397 requeued_events_pending_p (void)
10399 return (!NILP (Vunread_command_events) || unread_command_char != -1);
10403 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
10404 doc: /* Return t if command input is currently available with no wait.
10405 Actually, the value is nil only if we can be sure that no input is available;
10406 if there is a doubt, the value is t. */)
10407 (void)
10409 if (!NILP (Vunread_command_events) || unread_command_char != -1
10410 || !NILP (Vunread_post_input_method_events)
10411 || !NILP (Vunread_input_method_events))
10412 return (Qt);
10414 get_input_pending (&input_pending,
10415 READABLE_EVENTS_DO_TIMERS_NOW
10416 | READABLE_EVENTS_FILTER_EVENTS);
10417 return input_pending > 0 ? Qt : Qnil;
10420 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
10421 doc: /* Return vector of last 300 events, not counting those from keyboard macros. */)
10422 (void)
10424 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
10425 Lisp_Object val;
10427 if (total_keys < NUM_RECENT_KEYS)
10428 return Fvector (total_keys, keys);
10429 else
10431 val = Fvector (NUM_RECENT_KEYS, keys);
10432 memcpy (XVECTOR (val)->contents, keys + recent_keys_index,
10433 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
10434 memcpy (XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
10435 keys, recent_keys_index * sizeof (Lisp_Object));
10436 return val;
10440 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
10441 doc: /* Return the key sequence that invoked this command.
10442 However, if the command has called `read-key-sequence', it returns
10443 the last key sequence that has been read.
10444 The value is a string or a vector.
10446 See also `this-command-keys-vector'. */)
10447 (void)
10449 return make_event_array (this_command_key_count,
10450 XVECTOR (this_command_keys)->contents);
10453 DEFUN ("this-command-keys-vector", Fthis_command_keys_vector, Sthis_command_keys_vector, 0, 0, 0,
10454 doc: /* Return the key sequence that invoked this command, as a vector.
10455 However, if the command has called `read-key-sequence', it returns
10456 the last key sequence that has been read.
10458 See also `this-command-keys'. */)
10459 (void)
10461 return Fvector (this_command_key_count,
10462 XVECTOR (this_command_keys)->contents);
10465 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
10466 Sthis_single_command_keys, 0, 0, 0,
10467 doc: /* Return the key sequence that invoked this command.
10468 More generally, it returns the last key sequence read, either by
10469 the command loop or by `read-key-sequence'.
10470 Unlike `this-command-keys', this function's value
10471 does not include prefix arguments.
10472 The value is always a vector. */)
10473 (void)
10475 return Fvector (this_command_key_count
10476 - this_single_command_key_start,
10477 (XVECTOR (this_command_keys)->contents
10478 + this_single_command_key_start));
10481 DEFUN ("this-single-command-raw-keys", Fthis_single_command_raw_keys,
10482 Sthis_single_command_raw_keys, 0, 0, 0,
10483 doc: /* Return the raw events that were read for this command.
10484 More generally, it returns the last key sequence read, either by
10485 the command loop or by `read-key-sequence'.
10486 Unlike `this-single-command-keys', this function's value
10487 shows the events before all translations (except for input methods).
10488 The value is always a vector. */)
10489 (void)
10491 return Fvector (raw_keybuf_count,
10492 (XVECTOR (raw_keybuf)->contents));
10495 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
10496 Sreset_this_command_lengths, 0, 0, 0,
10497 doc: /* Make the unread events replace the last command and echo.
10498 Used in `universal-argument-other-key'.
10500 `universal-argument-other-key' rereads the event just typed.
10501 It then gets translated through `function-key-map'.
10502 The translated event has to replace the real events,
10503 both in the value of (this-command-keys) and in echoing.
10504 To achieve this, `universal-argument-other-key' calls
10505 `reset-this-command-lengths', which discards the record of reading
10506 these events the first time. */)
10507 (void)
10509 this_command_key_count = before_command_key_count;
10510 if (this_command_key_count < this_single_command_key_start)
10511 this_single_command_key_start = this_command_key_count;
10513 echo_truncate (before_command_echo_length);
10515 /* Cause whatever we put into unread-command-events
10516 to echo as if it were being freshly read from the keyboard. */
10517 this_command_key_count_reset = 1;
10519 return Qnil;
10522 DEFUN ("clear-this-command-keys", Fclear_this_command_keys,
10523 Sclear_this_command_keys, 0, 1, 0,
10524 doc: /* Clear out the vector that `this-command-keys' returns.
10525 Also clear the record of the last 100 events, unless optional arg
10526 KEEP-RECORD is non-nil. */)
10527 (Lisp_Object keep_record)
10529 int i;
10531 this_command_key_count = 0;
10532 this_command_key_count_reset = 0;
10534 if (NILP (keep_record))
10536 for (i = 0; i < XVECTOR (recent_keys)->size; ++i)
10537 XVECTOR (recent_keys)->contents[i] = Qnil;
10538 total_keys = 0;
10539 recent_keys_index = 0;
10541 return Qnil;
10544 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
10545 doc: /* Return the current depth in recursive edits. */)
10546 (void)
10548 Lisp_Object temp;
10549 XSETFASTINT (temp, command_loop_level + minibuf_level);
10550 return temp;
10553 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
10554 "FOpen dribble file: ",
10555 doc: /* Start writing all keyboard characters to a dribble file called FILE.
10556 If FILE is nil, close any open dribble file. */)
10557 (Lisp_Object file)
10559 if (dribble)
10561 BLOCK_INPUT;
10562 fclose (dribble);
10563 UNBLOCK_INPUT;
10564 dribble = 0;
10566 if (!NILP (file))
10568 file = Fexpand_file_name (file, Qnil);
10569 dribble = fopen (SSDATA (file), "w");
10570 if (dribble == 0)
10571 report_file_error ("Opening dribble", Fcons (file, Qnil));
10573 return Qnil;
10576 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
10577 doc: /* Discard the contents of the terminal input buffer.
10578 Also end any kbd macro being defined. */)
10579 (void)
10581 if (!NILP (KVAR (current_kboard, defining_kbd_macro)))
10583 /* Discard the last command from the macro. */
10584 Fcancel_kbd_macro_events ();
10585 end_kbd_macro ();
10588 update_mode_lines++;
10590 Vunread_command_events = Qnil;
10591 unread_command_char = -1;
10593 discard_tty_input ();
10595 kbd_fetch_ptr = kbd_store_ptr;
10596 input_pending = 0;
10598 return Qnil;
10601 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
10602 doc: /* Stop Emacs and return to superior process. You can resume later.
10603 If `cannot-suspend' is non-nil, or if the system doesn't support job
10604 control, run a subshell instead.
10606 If optional arg STUFFSTRING is non-nil, its characters are stuffed
10607 to be read as terminal input by Emacs's parent, after suspension.
10609 Before suspending, run the normal hook `suspend-hook'.
10610 After resumption run the normal hook `suspend-resume-hook'.
10612 Some operating systems cannot stop the Emacs process and resume it later.
10613 On such systems, Emacs starts a subshell instead of suspending. */)
10614 (Lisp_Object stuffstring)
10616 int count = SPECPDL_INDEX ();
10617 int old_height, old_width;
10618 int width, height;
10619 struct gcpro gcpro1;
10621 if (tty_list && tty_list->next)
10622 error ("There are other tty frames open; close them before suspending Emacs");
10624 if (!NILP (stuffstring))
10625 CHECK_STRING (stuffstring);
10627 /* Run the functions in suspend-hook. */
10628 if (!NILP (Vrun_hooks))
10629 call1 (Vrun_hooks, intern ("suspend-hook"));
10631 GCPRO1 (stuffstring);
10632 get_tty_size (fileno (CURTTY ()->input), &old_width, &old_height);
10633 reset_all_sys_modes ();
10634 /* sys_suspend can get an error if it tries to fork a subshell
10635 and the system resources aren't available for that. */
10636 record_unwind_protect ((Lisp_Object (*) (Lisp_Object)) init_all_sys_modes,
10637 Qnil);
10638 stuff_buffered_input (stuffstring);
10639 if (cannot_suspend)
10640 sys_subshell ();
10641 else
10642 sys_suspend ();
10643 unbind_to (count, Qnil);
10645 /* Check if terminal/window size has changed.
10646 Note that this is not useful when we are running directly
10647 with a window system; but suspend should be disabled in that case. */
10648 get_tty_size (fileno (CURTTY ()->input), &width, &height);
10649 if (width != old_width || height != old_height)
10650 change_frame_size (SELECTED_FRAME (), height, width, 0, 0, 0);
10652 /* Run suspend-resume-hook. */
10653 if (!NILP (Vrun_hooks))
10654 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
10656 UNGCPRO;
10657 return Qnil;
10660 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
10661 Then in any case stuff anything Emacs has read ahead and not used. */
10663 void
10664 stuff_buffered_input (Lisp_Object stuffstring)
10666 #ifdef SIGTSTP /* stuff_char is defined if SIGTSTP. */
10667 register unsigned char *p;
10669 if (STRINGP (stuffstring))
10671 register EMACS_INT count;
10673 p = SDATA (stuffstring);
10674 count = SBYTES (stuffstring);
10675 while (count-- > 0)
10676 stuff_char (*p++);
10677 stuff_char ('\n');
10680 /* Anything we have read ahead, put back for the shell to read. */
10681 /* ?? What should this do when we have multiple keyboards??
10682 Should we ignore anything that was typed in at the "wrong" kboard?
10684 rms: we should stuff everything back into the kboard
10685 it came from. */
10686 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
10689 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
10690 kbd_fetch_ptr = kbd_buffer;
10691 if (kbd_fetch_ptr->kind == ASCII_KEYSTROKE_EVENT)
10692 stuff_char (kbd_fetch_ptr->code);
10694 clear_event (kbd_fetch_ptr);
10697 input_pending = 0;
10698 #endif /* SIGTSTP */
10701 void
10702 set_waiting_for_input (struct timeval *time_to_clear)
10704 input_available_clear_time = time_to_clear;
10706 /* Tell handle_interrupt to throw back to read_char, */
10707 waiting_for_input = 1;
10709 /* If handle_interrupt was called before and buffered a C-g,
10710 make it run again now, to avoid timing error. */
10711 if (!NILP (Vquit_flag))
10712 quit_throw_to_read_char ();
10715 void
10716 clear_waiting_for_input (void)
10718 /* Tell handle_interrupt not to throw back to read_char, */
10719 waiting_for_input = 0;
10720 input_available_clear_time = 0;
10723 /* The SIGINT handler.
10725 If we have a frame on the controlling tty, we assume that the
10726 SIGINT was generated by C-g, so we call handle_interrupt.
10727 Otherwise, the handler kills Emacs. */
10729 static SIGTYPE
10730 interrupt_signal (int signalnum) /* If we don't have an argument, some */
10731 /* compilers complain in signal calls. */
10733 /* Must preserve main program's value of errno. */
10734 int old_errno = errno;
10735 struct terminal *terminal;
10737 SIGNAL_THREAD_CHECK (signalnum);
10739 /* See if we have an active terminal on our controlling tty. */
10740 terminal = get_named_tty ("/dev/tty");
10741 if (!terminal)
10743 /* If there are no frames there, let's pretend that we are a
10744 well-behaving UN*X program and quit. */
10745 Fkill_emacs (Qnil);
10747 else
10749 /* Otherwise, the SIGINT was probably generated by C-g. */
10751 /* Set internal_last_event_frame to the top frame of the
10752 controlling tty, if we have a frame there. We disable the
10753 interrupt key on secondary ttys, so the SIGINT must have come
10754 from the controlling tty. */
10755 internal_last_event_frame = terminal->display_info.tty->top_frame;
10757 handle_interrupt ();
10760 errno = old_errno;
10763 /* This routine is called at interrupt level in response to C-g.
10765 It is called from the SIGINT handler or kbd_buffer_store_event.
10767 If `waiting_for_input' is non zero, then unless `echoing' is
10768 nonzero, immediately throw back to read_char.
10770 Otherwise it sets the Lisp variable quit-flag not-nil. This causes
10771 eval to throw, when it gets a chance. If quit-flag is already
10772 non-nil, it stops the job right away. */
10774 static void
10775 handle_interrupt (void)
10777 char c;
10779 cancel_echoing ();
10781 /* XXX This code needs to be revised for multi-tty support. */
10782 if (!NILP (Vquit_flag) && get_named_tty ("/dev/tty"))
10784 /* If SIGINT isn't blocked, don't let us be interrupted by
10785 another SIGINT, it might be harmful due to non-reentrancy
10786 in I/O functions. */
10787 sigblock (sigmask (SIGINT));
10789 fflush (stdout);
10790 reset_all_sys_modes ();
10792 #ifdef SIGTSTP /* Support possible in later USG versions */
10794 * On systems which can suspend the current process and return to the original
10795 * shell, this command causes the user to end up back at the shell.
10796 * The "Auto-save" and "Abort" questions are not asked until
10797 * the user elects to return to emacs, at which point he can save the current
10798 * job and either dump core or continue.
10800 sys_suspend ();
10801 #else
10802 /* Perhaps should really fork an inferior shell?
10803 But that would not provide any way to get back
10804 to the original shell, ever. */
10805 printf ("No support for stopping a process on this operating system;\n");
10806 printf ("you can continue or abort.\n");
10807 #endif /* not SIGTSTP */
10808 #ifdef MSDOS
10809 /* We must remain inside the screen area when the internal terminal
10810 is used. Note that [Enter] is not echoed by dos. */
10811 cursor_to (SELECTED_FRAME (), 0, 0);
10812 #endif
10813 /* It doesn't work to autosave while GC is in progress;
10814 the code used for auto-saving doesn't cope with the mark bit. */
10815 if (!gc_in_progress)
10817 printf ("Auto-save? (y or n) ");
10818 fflush (stdout);
10819 if (((c = getchar ()) & ~040) == 'Y')
10821 Fdo_auto_save (Qt, Qnil);
10822 #ifdef MSDOS
10823 printf ("\r\nAuto-save done");
10824 #else /* not MSDOS */
10825 printf ("Auto-save done\n");
10826 #endif /* not MSDOS */
10828 while (c != '\n') c = getchar ();
10830 else
10832 /* During GC, it must be safe to reenable quitting again. */
10833 Vinhibit_quit = Qnil;
10834 #ifdef MSDOS
10835 printf ("\r\n");
10836 #endif /* not MSDOS */
10837 printf ("Garbage collection in progress; cannot auto-save now\r\n");
10838 printf ("but will instead do a real quit after garbage collection ends\r\n");
10839 fflush (stdout);
10842 #ifdef MSDOS
10843 printf ("\r\nAbort? (y or n) ");
10844 #else /* not MSDOS */
10845 printf ("Abort (and dump core)? (y or n) ");
10846 #endif /* not MSDOS */
10847 fflush (stdout);
10848 if (((c = getchar ()) & ~040) == 'Y')
10849 abort ();
10850 while (c != '\n') c = getchar ();
10851 #ifdef MSDOS
10852 printf ("\r\nContinuing...\r\n");
10853 #else /* not MSDOS */
10854 printf ("Continuing...\n");
10855 #endif /* not MSDOS */
10856 fflush (stdout);
10857 init_all_sys_modes ();
10858 sigfree ();
10860 else
10862 /* If executing a function that wants to be interrupted out of
10863 and the user has not deferred quitting by binding `inhibit-quit'
10864 then quit right away. */
10865 if (immediate_quit && NILP (Vinhibit_quit))
10867 struct gl_state_s saved;
10868 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
10870 immediate_quit = 0;
10871 sigfree ();
10872 saved = gl_state;
10873 GCPRO4 (saved.object, saved.global_code,
10874 saved.current_syntax_table, saved.old_prop);
10875 Fsignal (Qquit, Qnil);
10876 /* FIXME: AFAIK, `quit' can never return, so this code is dead! */
10877 gl_state = saved;
10878 UNGCPRO;
10880 else
10881 /* Else request quit when it's safe */
10882 Vquit_flag = Qt;
10885 /* TODO: The longjmp in this call throws the NS event loop integration off,
10886 and it seems to do fine without this. Probably some attention
10887 needs to be paid to the setting of waiting_for_input in
10888 wait_reading_process_output() under HAVE_NS because of the call
10889 to ns_select there (needed because otherwise events aren't picked up
10890 outside of polling since we don't get SIGIO like X and we don't have a
10891 separate event loop thread like W32. */
10892 #ifndef HAVE_NS
10893 if (waiting_for_input && !echoing)
10894 quit_throw_to_read_char ();
10895 #endif
10898 /* Handle a C-g by making read_char return C-g. */
10900 void
10901 quit_throw_to_read_char (void)
10903 sigfree ();
10904 /* Prevent another signal from doing this before we finish. */
10905 clear_waiting_for_input ();
10906 input_pending = 0;
10908 Vunread_command_events = Qnil;
10909 unread_command_char = -1;
10911 #if 0 /* Currently, sit_for is called from read_char without turning
10912 off polling. And that can call set_waiting_for_input.
10913 It seems to be harmless. */
10914 #ifdef POLL_FOR_INPUT
10915 /* May be > 1 if in recursive minibuffer. */
10916 if (poll_suppress_count == 0)
10917 abort ();
10918 #endif
10919 #endif
10920 if (FRAMEP (internal_last_event_frame)
10921 && !EQ (internal_last_event_frame, selected_frame))
10922 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
10923 0, 0, Qnil);
10925 _longjmp (getcjmp, 1);
10928 DEFUN ("set-input-interrupt-mode", Fset_input_interrupt_mode, Sset_input_interrupt_mode, 1, 1, 0,
10929 doc: /* Set interrupt mode of reading keyboard input.
10930 If INTERRUPT is non-nil, Emacs will use input interrupts;
10931 otherwise Emacs uses CBREAK mode.
10933 See also `current-input-mode'. */)
10934 (Lisp_Object interrupt)
10936 int new_interrupt_input;
10937 #ifdef SIGIO
10938 /* Note SIGIO has been undef'd if FIONREAD is missing. */
10939 #ifdef HAVE_X_WINDOWS
10940 if (x_display_list != NULL)
10942 /* When using X, don't give the user a real choice,
10943 because we haven't implemented the mechanisms to support it. */
10944 new_interrupt_input = 1;
10946 else
10947 #endif /* HAVE_X_WINDOWS */
10948 new_interrupt_input = !NILP (interrupt);
10949 #else /* not SIGIO */
10950 new_interrupt_input = 0;
10951 #endif /* not SIGIO */
10953 if (new_interrupt_input != interrupt_input)
10955 #ifdef POLL_FOR_INPUT
10956 stop_polling ();
10957 #endif
10958 #ifndef DOS_NT
10959 /* this causes startup screen to be restored and messes with the mouse */
10960 reset_all_sys_modes ();
10961 interrupt_input = new_interrupt_input;
10962 init_all_sys_modes ();
10963 #else
10964 interrupt_input = new_interrupt_input;
10965 #endif
10967 #ifdef POLL_FOR_INPUT
10968 poll_suppress_count = 1;
10969 start_polling ();
10970 #endif
10972 return Qnil;
10975 DEFUN ("set-output-flow-control", Fset_output_flow_control, Sset_output_flow_control, 1, 2, 0,
10976 doc: /* Enable or disable ^S/^Q flow control for output to TERMINAL.
10977 If FLOW is non-nil, flow control is enabled and you cannot use C-s or
10978 C-q in key sequences.
10980 This setting only has an effect on tty terminals and only when
10981 Emacs reads input in CBREAK mode; see `set-input-interrupt-mode'.
10983 See also `current-input-mode'. */)
10984 (Lisp_Object flow, Lisp_Object terminal)
10986 struct terminal *t = get_terminal (terminal, 1);
10987 struct tty_display_info *tty;
10988 if (t == NULL || (t->type != output_termcap && t->type != output_msdos_raw))
10989 return Qnil;
10990 tty = t->display_info.tty;
10992 if (tty->flow_control != !NILP (flow))
10994 #ifndef DOS_NT
10995 /* this causes startup screen to be restored and messes with the mouse */
10996 reset_sys_modes (tty);
10997 #endif
10999 tty->flow_control = !NILP (flow);
11001 #ifndef DOS_NT
11002 init_sys_modes (tty);
11003 #endif
11005 return Qnil;
11008 DEFUN ("set-input-meta-mode", Fset_input_meta_mode, Sset_input_meta_mode, 1, 2, 0,
11009 doc: /* Enable or disable 8-bit input on TERMINAL.
11010 If META is t, Emacs will accept 8-bit input, and interpret the 8th
11011 bit as the Meta modifier.
11013 If META is nil, Emacs will ignore the top bit, on the assumption it is
11014 parity.
11016 Otherwise, Emacs will accept and pass through 8-bit input without
11017 specially interpreting the top bit.
11019 This setting only has an effect on tty terminal devices.
11021 Optional parameter TERMINAL specifies the tty terminal device to use.
11022 It may be a terminal object, a frame, or nil for the terminal used by
11023 the currently selected frame.
11025 See also `current-input-mode'. */)
11026 (Lisp_Object meta, Lisp_Object terminal)
11028 struct terminal *t = get_terminal (terminal, 1);
11029 struct tty_display_info *tty;
11030 int new_meta;
11032 if (t == NULL || (t->type != output_termcap && t->type != output_msdos_raw))
11033 return Qnil;
11034 tty = t->display_info.tty;
11036 if (NILP (meta))
11037 new_meta = 0;
11038 else if (EQ (meta, Qt))
11039 new_meta = 1;
11040 else
11041 new_meta = 2;
11043 if (tty->meta_key != new_meta)
11045 #ifndef DOS_NT
11046 /* this causes startup screen to be restored and messes with the mouse */
11047 reset_sys_modes (tty);
11048 #endif
11050 tty->meta_key = new_meta;
11052 #ifndef DOS_NT
11053 init_sys_modes (tty);
11054 #endif
11056 return Qnil;
11059 DEFUN ("set-quit-char", Fset_quit_char, Sset_quit_char, 1, 1, 0,
11060 doc: /* Specify character used for quitting.
11061 QUIT must be an ASCII character.
11063 This function only has an effect on the controlling tty of the Emacs
11064 process.
11066 See also `current-input-mode'. */)
11067 (Lisp_Object quit)
11069 struct terminal *t = get_named_tty ("/dev/tty");
11070 struct tty_display_info *tty;
11071 if (t == NULL || (t->type != output_termcap && t->type != output_msdos_raw))
11072 return Qnil;
11073 tty = t->display_info.tty;
11075 if (NILP (quit) || !INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400)
11076 error ("QUIT must be an ASCII character");
11078 #ifndef DOS_NT
11079 /* this causes startup screen to be restored and messes with the mouse */
11080 reset_sys_modes (tty);
11081 #endif
11083 /* Don't let this value be out of range. */
11084 quit_char = XINT (quit) & (tty->meta_key == 0 ? 0177 : 0377);
11086 #ifndef DOS_NT
11087 init_sys_modes (tty);
11088 #endif
11090 return Qnil;
11093 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
11094 doc: /* Set mode of reading keyboard input.
11095 First arg INTERRUPT non-nil means use input interrupts;
11096 nil means use CBREAK mode.
11097 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal
11098 (no effect except in CBREAK mode).
11099 Third arg META t means accept 8-bit input (for a Meta key).
11100 META nil means ignore the top bit, on the assumption it is parity.
11101 Otherwise, accept 8-bit input and don't use the top bit for Meta.
11102 Optional fourth arg QUIT if non-nil specifies character to use for quitting.
11103 See also `current-input-mode'. */)
11104 (Lisp_Object interrupt, Lisp_Object flow, Lisp_Object meta, Lisp_Object quit)
11106 Fset_input_interrupt_mode (interrupt);
11107 Fset_output_flow_control (flow, Qnil);
11108 Fset_input_meta_mode (meta, Qnil);
11109 if (!NILP (quit))
11110 Fset_quit_char (quit);
11111 return Qnil;
11114 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
11115 doc: /* Return information about the way Emacs currently reads keyboard input.
11116 The value is a list of the form (INTERRUPT FLOW META QUIT), where
11117 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if
11118 nil, Emacs is using CBREAK mode.
11119 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the
11120 terminal; this does not apply if Emacs uses interrupt-driven input.
11121 META is t if accepting 8-bit input with 8th bit as Meta flag.
11122 META nil means ignoring the top bit, on the assumption it is parity.
11123 META is neither t nor nil if accepting 8-bit input and using
11124 all 8 bits as the character code.
11125 QUIT is the character Emacs currently uses to quit.
11126 The elements of this list correspond to the arguments of
11127 `set-input-mode'. */)
11128 (void)
11130 Lisp_Object val[4];
11131 struct frame *sf = XFRAME (selected_frame);
11133 val[0] = interrupt_input ? Qt : Qnil;
11134 if (FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11136 val[1] = FRAME_TTY (sf)->flow_control ? Qt : Qnil;
11137 val[2] = (FRAME_TTY (sf)->meta_key == 2
11138 ? make_number (0)
11139 : (CURTTY ()->meta_key == 1 ? Qt : Qnil));
11141 else
11143 val[1] = Qnil;
11144 val[2] = Qt;
11146 XSETFASTINT (val[3], quit_char);
11148 return Flist (sizeof (val) / sizeof (val[0]), val);
11151 DEFUN ("posn-at-x-y", Fposn_at_x_y, Sposn_at_x_y, 2, 4, 0,
11152 doc: /* Return position information for pixel coordinates X and Y.
11153 By default, X and Y are relative to text area of the selected window.
11154 Optional third arg FRAME-OR-WINDOW non-nil specifies frame or window.
11155 If optional fourth arg WHOLE is non-nil, X is relative to the left
11156 edge of the window.
11158 The return value is similar to a mouse click position:
11159 (WINDOW AREA-OR-POS (X . Y) TIMESTAMP OBJECT POS (COL . ROW)
11160 IMAGE (DX . DY) (WIDTH . HEIGHT))
11161 The `posn-' functions access elements of such lists. */)
11162 (Lisp_Object x, Lisp_Object y, Lisp_Object frame_or_window, Lisp_Object whole)
11164 CHECK_NATNUM (x);
11165 CHECK_NATNUM (y);
11167 if (NILP (frame_or_window))
11168 frame_or_window = selected_window;
11170 if (WINDOWP (frame_or_window))
11172 struct window *w;
11174 CHECK_LIVE_WINDOW (frame_or_window);
11176 w = XWINDOW (frame_or_window);
11177 XSETINT (x, (XINT (x)
11178 + WINDOW_LEFT_EDGE_X (w)
11179 + (NILP (whole)
11180 ? window_box_left_offset (w, TEXT_AREA)
11181 : 0)));
11182 XSETINT (y, WINDOW_TO_FRAME_PIXEL_Y (w, XINT (y)));
11183 frame_or_window = w->frame;
11186 CHECK_LIVE_FRAME (frame_or_window);
11188 return make_lispy_position (XFRAME (frame_or_window), x, y, 0);
11191 DEFUN ("posn-at-point", Fposn_at_point, Sposn_at_point, 0, 2, 0,
11192 doc: /* Return position information for buffer POS in WINDOW.
11193 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
11195 Return nil if position is not visible in window. Otherwise,
11196 the return value is similar to that returned by `event-start' for
11197 a mouse click at the upper left corner of the glyph corresponding
11198 to the given buffer position:
11199 (WINDOW AREA-OR-POS (X . Y) TIMESTAMP OBJECT POS (COL . ROW)
11200 IMAGE (DX . DY) (WIDTH . HEIGHT))
11201 The `posn-' functions access elements of such lists. */)
11202 (Lisp_Object pos, Lisp_Object window)
11204 Lisp_Object tem;
11206 if (NILP (window))
11207 window = selected_window;
11209 tem = Fpos_visible_in_window_p (pos, window, Qt);
11210 if (!NILP (tem))
11212 Lisp_Object x = XCAR (tem);
11213 Lisp_Object y = XCAR (XCDR (tem));
11215 /* Point invisible due to hscrolling? */
11216 if (XINT (x) < 0)
11217 return Qnil;
11218 tem = Fposn_at_x_y (x, y, window, Qnil);
11221 return tem;
11226 * Set up a new kboard object with reasonable initial values.
11228 void
11229 init_kboard (KBOARD *kb)
11231 KVAR (kb, Voverriding_terminal_local_map) = Qnil;
11232 KVAR (kb, Vlast_command) = Qnil;
11233 KVAR (kb, Vreal_last_command) = Qnil;
11234 KVAR (kb, Vkeyboard_translate_table) = Qnil;
11235 KVAR (kb, Vlast_repeatable_command) = Qnil;
11236 KVAR (kb, Vprefix_arg) = Qnil;
11237 KVAR (kb, Vlast_prefix_arg) = Qnil;
11238 KVAR (kb, kbd_queue) = Qnil;
11239 kb->kbd_queue_has_data = 0;
11240 kb->immediate_echo = 0;
11241 KVAR (kb, echo_string) = Qnil;
11242 kb->echo_after_prompt = -1;
11243 kb->kbd_macro_buffer = 0;
11244 kb->kbd_macro_bufsize = 0;
11245 KVAR (kb, defining_kbd_macro) = Qnil;
11246 KVAR (kb, Vlast_kbd_macro) = Qnil;
11247 kb->reference_count = 0;
11248 KVAR (kb, Vsystem_key_alist) = Qnil;
11249 KVAR (kb, system_key_syms) = Qnil;
11250 KVAR (kb, Vwindow_system) = Qt; /* Unset. */
11251 KVAR (kb, Vinput_decode_map) = Fmake_sparse_keymap (Qnil);
11252 KVAR (kb, Vlocal_function_key_map) = Fmake_sparse_keymap (Qnil);
11253 Fset_keymap_parent (KVAR (kb, Vlocal_function_key_map), Vfunction_key_map);
11254 KVAR (kb, Vdefault_minibuffer_frame) = Qnil;
11258 * Destroy the contents of a kboard object, but not the object itself.
11259 * We use this just before deleting it, or if we're going to initialize
11260 * it a second time.
11262 static void
11263 wipe_kboard (KBOARD *kb)
11265 xfree (kb->kbd_macro_buffer);
11268 /* Free KB and memory referenced from it. */
11270 void
11271 delete_kboard (KBOARD *kb)
11273 KBOARD **kbp;
11275 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
11276 if (*kbp == NULL)
11277 abort ();
11278 *kbp = kb->next_kboard;
11280 /* Prevent a dangling reference to KB. */
11281 if (kb == current_kboard
11282 && FRAMEP (selected_frame)
11283 && FRAME_LIVE_P (XFRAME (selected_frame)))
11285 current_kboard = FRAME_KBOARD (XFRAME (selected_frame));
11286 single_kboard = 0;
11287 if (current_kboard == kb)
11288 abort ();
11291 wipe_kboard (kb);
11292 xfree (kb);
11295 void
11296 init_keyboard (void)
11298 /* This is correct before outermost invocation of the editor loop */
11299 command_loop_level = -1;
11300 immediate_quit = 0;
11301 quit_char = Ctl ('g');
11302 Vunread_command_events = Qnil;
11303 unread_command_char = -1;
11304 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
11305 total_keys = 0;
11306 recent_keys_index = 0;
11307 kbd_fetch_ptr = kbd_buffer;
11308 kbd_store_ptr = kbd_buffer;
11309 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
11310 do_mouse_tracking = Qnil;
11311 #endif
11312 input_pending = 0;
11313 interrupt_input_blocked = 0;
11314 interrupt_input_pending = 0;
11315 #ifdef SYNC_INPUT
11316 pending_signals = 0;
11317 #endif
11319 /* This means that command_loop_1 won't try to select anything the first
11320 time through. */
11321 internal_last_event_frame = Qnil;
11322 Vlast_event_frame = internal_last_event_frame;
11324 current_kboard = initial_kboard;
11325 /* Re-initialize the keyboard again. */
11326 wipe_kboard (current_kboard);
11327 init_kboard (current_kboard);
11328 /* A value of nil for Vwindow_system normally means a tty, but we also use
11329 it for the initial terminal since there is no window system there. */
11330 KVAR (current_kboard, Vwindow_system) = Qnil;
11332 if (!noninteractive)
11334 /* Before multi-tty support, these handlers used to be installed
11335 only if the current session was a tty session. Now an Emacs
11336 session may have multiple display types, so we always handle
11337 SIGINT. There is special code in interrupt_signal to exit
11338 Emacs on SIGINT when there are no termcap frames on the
11339 controlling terminal. */
11340 signal (SIGINT, interrupt_signal);
11341 #ifndef DOS_NT
11342 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
11343 SIGQUIT and we can't tell which one it will give us. */
11344 signal (SIGQUIT, interrupt_signal);
11345 #endif /* not DOS_NT */
11347 /* Note SIGIO has been undef'd if FIONREAD is missing. */
11348 #ifdef SIGIO
11349 if (!noninteractive)
11350 signal (SIGIO, input_available_signal);
11351 #endif /* SIGIO */
11353 /* Use interrupt input by default, if it works and noninterrupt input
11354 has deficiencies. */
11356 #ifdef INTERRUPT_INPUT
11357 interrupt_input = 1;
11358 #else
11359 interrupt_input = 0;
11360 #endif
11362 sigfree ();
11363 dribble = 0;
11365 if (keyboard_init_hook)
11366 (*keyboard_init_hook) ();
11368 #ifdef POLL_FOR_INPUT
11369 poll_timer = NULL;
11370 poll_suppress_count = 1;
11371 start_polling ();
11372 #endif
11375 /* This type's only use is in syms_of_keyboard, to initialize the
11376 event header symbols and put properties on them. */
11377 struct event_head {
11378 Lisp_Object *var;
11379 const char *name;
11380 Lisp_Object *kind;
11383 static const struct event_head head_table[] = {
11384 {&Qmouse_movement, "mouse-movement", &Qmouse_movement},
11385 {&Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement},
11386 {&Qswitch_frame, "switch-frame", &Qswitch_frame},
11387 {&Qdelete_frame, "delete-frame", &Qdelete_frame},
11388 {&Qiconify_frame, "iconify-frame", &Qiconify_frame},
11389 {&Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible},
11390 /* `select-window' should be handled just like `switch-frame'
11391 in read_key_sequence. */
11392 {&Qselect_window, "select-window", &Qswitch_frame}
11395 void
11396 syms_of_keyboard (void)
11398 pending_funcalls = Qnil;
11399 staticpro (&pending_funcalls);
11401 Vlispy_mouse_stem = make_pure_c_string ("mouse");
11402 staticpro (&Vlispy_mouse_stem);
11404 /* Tool-bars. */
11405 QCimage = intern_c_string (":image");
11406 staticpro (&QCimage);
11408 staticpro (&Qhelp_echo);
11409 Qhelp_echo = intern_c_string ("help-echo");
11411 staticpro (&Qrtl);
11412 Qrtl = intern_c_string (":rtl");
11414 staticpro (&item_properties);
11415 item_properties = Qnil;
11417 staticpro (&tool_bar_item_properties);
11418 tool_bar_item_properties = Qnil;
11419 staticpro (&tool_bar_items_vector);
11420 tool_bar_items_vector = Qnil;
11422 staticpro (&real_this_command);
11423 real_this_command = Qnil;
11425 Qtimer_event_handler = intern_c_string ("timer-event-handler");
11426 staticpro (&Qtimer_event_handler);
11428 Qdisabled_command_function = intern_c_string ("disabled-command-function");
11429 staticpro (&Qdisabled_command_function);
11431 Qself_insert_command = intern_c_string ("self-insert-command");
11432 staticpro (&Qself_insert_command);
11434 Qforward_char = intern_c_string ("forward-char");
11435 staticpro (&Qforward_char);
11437 Qbackward_char = intern_c_string ("backward-char");
11438 staticpro (&Qbackward_char);
11440 Qdisabled = intern_c_string ("disabled");
11441 staticpro (&Qdisabled);
11443 Qundefined = intern_c_string ("undefined");
11444 staticpro (&Qundefined);
11446 Qpre_command_hook = intern_c_string ("pre-command-hook");
11447 staticpro (&Qpre_command_hook);
11449 Qpost_command_hook = intern_c_string ("post-command-hook");
11450 staticpro (&Qpost_command_hook);
11452 Qdeferred_action_function = intern_c_string ("deferred-action-function");
11453 staticpro (&Qdeferred_action_function);
11455 Qcommand_hook_internal = intern_c_string ("command-hook-internal");
11456 staticpro (&Qcommand_hook_internal);
11458 Qfunction_key = intern_c_string ("function-key");
11459 staticpro (&Qfunction_key);
11460 Qmouse_click = intern_c_string ("mouse-click");
11461 staticpro (&Qmouse_click);
11462 #if defined (WINDOWSNT)
11463 Qlanguage_change = intern_c_string ("language-change");
11464 staticpro (&Qlanguage_change);
11465 #endif
11466 Qdrag_n_drop = intern_c_string ("drag-n-drop");
11467 staticpro (&Qdrag_n_drop);
11469 Qsave_session = intern_c_string ("save-session");
11470 staticpro (&Qsave_session);
11472 #ifdef HAVE_DBUS
11473 Qdbus_event = intern_c_string ("dbus-event");
11474 staticpro (&Qdbus_event);
11475 #endif
11477 Qconfig_changed_event = intern_c_string ("config-changed-event");
11478 staticpro (&Qconfig_changed_event);
11480 Qmenu_enable = intern_c_string ("menu-enable");
11481 staticpro (&Qmenu_enable);
11482 QCenable = intern_c_string (":enable");
11483 staticpro (&QCenable);
11484 QCvisible = intern_c_string (":visible");
11485 staticpro (&QCvisible);
11486 QChelp = intern_c_string (":help");
11487 staticpro (&QChelp);
11488 QCfilter = intern_c_string (":filter");
11489 staticpro (&QCfilter);
11490 QCbutton = intern_c_string (":button");
11491 staticpro (&QCbutton);
11492 QCkeys = intern_c_string (":keys");
11493 staticpro (&QCkeys);
11494 QCkey_sequence = intern_c_string (":key-sequence");
11495 staticpro (&QCkey_sequence);
11496 QCtoggle = intern_c_string (":toggle");
11497 staticpro (&QCtoggle);
11498 QCradio = intern_c_string (":radio");
11499 staticpro (&QCradio);
11500 QClabel = intern_c_string (":label");
11501 staticpro (&QClabel);
11502 QCvert_only = intern_c_string (":vert-only");
11503 staticpro (&QCvert_only);
11505 Qmode_line = intern_c_string ("mode-line");
11506 staticpro (&Qmode_line);
11507 Qvertical_line = intern_c_string ("vertical-line");
11508 staticpro (&Qvertical_line);
11509 Qvertical_scroll_bar = intern_c_string ("vertical-scroll-bar");
11510 staticpro (&Qvertical_scroll_bar);
11511 Qmenu_bar = intern_c_string ("menu-bar");
11512 staticpro (&Qmenu_bar);
11514 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
11515 Qmouse_fixup_help_message = intern_c_string ("mouse-fixup-help-message");
11516 staticpro (&Qmouse_fixup_help_message);
11517 #endif
11519 Qabove_handle = intern_c_string ("above-handle");
11520 staticpro (&Qabove_handle);
11521 Qhandle = intern_c_string ("handle");
11522 staticpro (&Qhandle);
11523 Qbelow_handle = intern_c_string ("below-handle");
11524 staticpro (&Qbelow_handle);
11525 Qup = intern_c_string ("up");
11526 staticpro (&Qup);
11527 Qdown = intern_c_string ("down");
11528 staticpro (&Qdown);
11529 Qtop = intern_c_string ("top");
11530 staticpro (&Qtop);
11531 Qbottom = intern_c_string ("bottom");
11532 staticpro (&Qbottom);
11533 Qend_scroll = intern_c_string ("end-scroll");
11534 staticpro (&Qend_scroll);
11535 Qratio = intern_c_string ("ratio");
11536 staticpro (&Qratio);
11538 Qevent_kind = intern_c_string ("event-kind");
11539 staticpro (&Qevent_kind);
11540 Qevent_symbol_elements = intern_c_string ("event-symbol-elements");
11541 staticpro (&Qevent_symbol_elements);
11542 Qevent_symbol_element_mask = intern_c_string ("event-symbol-element-mask");
11543 staticpro (&Qevent_symbol_element_mask);
11544 Qmodifier_cache = intern_c_string ("modifier-cache");
11545 staticpro (&Qmodifier_cache);
11547 Qrecompute_lucid_menubar = intern_c_string ("recompute-lucid-menubar");
11548 staticpro (&Qrecompute_lucid_menubar);
11549 Qactivate_menubar_hook = intern_c_string ("activate-menubar-hook");
11550 staticpro (&Qactivate_menubar_hook);
11552 Qpolling_period = intern_c_string ("polling-period");
11553 staticpro (&Qpolling_period);
11555 Qinput_method_function = intern_c_string ("input-method-function");
11556 staticpro (&Qinput_method_function);
11558 Qx_set_selection = intern_c_string ("x-set-selection");
11559 staticpro (&Qx_set_selection);
11560 QPRIMARY = intern_c_string ("PRIMARY");
11561 staticpro (&QPRIMARY);
11562 Qhandle_switch_frame = intern_c_string ("handle-switch-frame");
11563 staticpro (&Qhandle_switch_frame);
11565 Qinput_method_exit_on_first_char = intern_c_string ("input-method-exit-on-first-char");
11566 staticpro (&Qinput_method_exit_on_first_char);
11567 Qinput_method_use_echo_area = intern_c_string ("input-method-use-echo-area");
11568 staticpro (&Qinput_method_use_echo_area);
11570 Fset (Qinput_method_exit_on_first_char, Qnil);
11571 Fset (Qinput_method_use_echo_area, Qnil);
11573 last_point_position_buffer = Qnil;
11574 last_point_position_window = Qnil;
11577 int i;
11578 int len = sizeof (head_table) / sizeof (head_table[0]);
11580 for (i = 0; i < len; i++)
11582 const struct event_head *p = &head_table[i];
11583 *p->var = intern_c_string (p->name);
11584 staticpro (p->var);
11585 Fput (*p->var, Qevent_kind, *p->kind);
11586 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
11590 button_down_location = Fmake_vector (make_number (5), Qnil);
11591 staticpro (&button_down_location);
11592 mouse_syms = Fmake_vector (make_number (5), Qnil);
11593 staticpro (&mouse_syms);
11594 wheel_syms = Fmake_vector (make_number (sizeof (lispy_wheel_names)
11595 / sizeof (lispy_wheel_names[0])),
11596 Qnil);
11597 staticpro (&wheel_syms);
11600 int i;
11601 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
11603 modifier_symbols = Fmake_vector (make_number (len), Qnil);
11604 for (i = 0; i < len; i++)
11605 if (modifier_names[i])
11606 XVECTOR (modifier_symbols)->contents[i] = intern_c_string (modifier_names[i]);
11607 staticpro (&modifier_symbols);
11610 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
11611 staticpro (&recent_keys);
11613 this_command_keys = Fmake_vector (make_number (40), Qnil);
11614 staticpro (&this_command_keys);
11616 raw_keybuf = Fmake_vector (make_number (30), Qnil);
11617 staticpro (&raw_keybuf);
11619 Qextended_command_history = intern_c_string ("extended-command-history");
11620 Fset (Qextended_command_history, Qnil);
11621 staticpro (&Qextended_command_history);
11623 accent_key_syms = Qnil;
11624 staticpro (&accent_key_syms);
11626 func_key_syms = Qnil;
11627 staticpro (&func_key_syms);
11629 drag_n_drop_syms = Qnil;
11630 staticpro (&drag_n_drop_syms);
11632 unread_switch_frame = Qnil;
11633 staticpro (&unread_switch_frame);
11635 internal_last_event_frame = Qnil;
11636 staticpro (&internal_last_event_frame);
11638 read_key_sequence_cmd = Qnil;
11639 staticpro (&read_key_sequence_cmd);
11641 menu_bar_one_keymap_changed_items = Qnil;
11642 staticpro (&menu_bar_one_keymap_changed_items);
11644 menu_bar_items_vector = Qnil;
11645 staticpro (&menu_bar_items_vector);
11647 help_form_saved_window_configs = Qnil;
11648 staticpro (&help_form_saved_window_configs);
11650 defsubr (&Scurrent_idle_time);
11651 defsubr (&Sevent_symbol_parse_modifiers);
11652 defsubr (&Sevent_convert_list);
11653 defsubr (&Sread_key_sequence);
11654 defsubr (&Sread_key_sequence_vector);
11655 defsubr (&Srecursive_edit);
11656 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
11657 defsubr (&Strack_mouse);
11658 #endif
11659 defsubr (&Sinput_pending_p);
11660 defsubr (&Scommand_execute);
11661 defsubr (&Srecent_keys);
11662 defsubr (&Sthis_command_keys);
11663 defsubr (&Sthis_command_keys_vector);
11664 defsubr (&Sthis_single_command_keys);
11665 defsubr (&Sthis_single_command_raw_keys);
11666 defsubr (&Sreset_this_command_lengths);
11667 defsubr (&Sclear_this_command_keys);
11668 defsubr (&Ssuspend_emacs);
11669 defsubr (&Sabort_recursive_edit);
11670 defsubr (&Sexit_recursive_edit);
11671 defsubr (&Srecursion_depth);
11672 defsubr (&Stop_level);
11673 defsubr (&Sdiscard_input);
11674 defsubr (&Sopen_dribble_file);
11675 defsubr (&Sset_input_interrupt_mode);
11676 defsubr (&Sset_output_flow_control);
11677 defsubr (&Sset_input_meta_mode);
11678 defsubr (&Sset_quit_char);
11679 defsubr (&Sset_input_mode);
11680 defsubr (&Scurrent_input_mode);
11681 defsubr (&Sexecute_extended_command);
11682 defsubr (&Sposn_at_point);
11683 defsubr (&Sposn_at_x_y);
11685 DEFVAR_LISP ("last-command-event", last_command_event,
11686 doc: /* Last input event that was part of a command. */);
11688 DEFVAR_LISP ("last-nonmenu-event", last_nonmenu_event,
11689 doc: /* Last input event in a command, except for mouse menu events.
11690 Mouse menus give back keys that don't look like mouse events;
11691 this variable holds the actual mouse event that led to the menu,
11692 so that you can determine whether the command was run by mouse or not. */);
11694 DEFVAR_LISP ("last-input-event", last_input_event,
11695 doc: /* Last input event. */);
11697 DEFVAR_LISP ("unread-command-events", Vunread_command_events,
11698 doc: /* List of events to be read as the command input.
11699 These events are processed first, before actual keyboard input.
11700 Events read from this list are not normally added to `this-command-keys',
11701 as they will already have been added once as they were read for the first time.
11702 An element of the form (t . EVENT) forces EVENT to be added to that list. */);
11703 Vunread_command_events = Qnil;
11705 DEFVAR_INT ("unread-command-char", unread_command_char,
11706 doc: /* If not -1, an object to be read as next command input event. */);
11708 DEFVAR_LISP ("unread-post-input-method-events", Vunread_post_input_method_events,
11709 doc: /* List of events to be processed as input by input methods.
11710 These events are processed before `unread-command-events'
11711 and actual keyboard input, but are not given to `input-method-function'. */);
11712 Vunread_post_input_method_events = Qnil;
11714 DEFVAR_LISP ("unread-input-method-events", Vunread_input_method_events,
11715 doc: /* List of events to be processed as input by input methods.
11716 These events are processed after `unread-command-events', but
11717 before actual keyboard input.
11718 If there's an active input method, the events are given to
11719 `input-method-function'. */);
11720 Vunread_input_method_events = Qnil;
11722 DEFVAR_LISP ("meta-prefix-char", meta_prefix_char,
11723 doc: /* Meta-prefix character code.
11724 Meta-foo as command input turns into this character followed by foo. */);
11725 XSETINT (meta_prefix_char, 033);
11727 DEFVAR_KBOARD ("last-command", Vlast_command,
11728 doc: /* The last command executed.
11729 Normally a symbol with a function definition, but can be whatever was found
11730 in the keymap, or whatever the variable `this-command' was set to by that
11731 command.
11733 The value `mode-exit' is special; it means that the previous command
11734 read an event that told it to exit, and it did so and unread that event.
11735 In other words, the present command is the event that made the previous
11736 command exit.
11738 The value `kill-region' is special; it means that the previous command
11739 was a kill command.
11741 `last-command' has a separate binding for each terminal device.
11742 See Info node `(elisp)Multiple Terminals'. */);
11744 DEFVAR_KBOARD ("real-last-command", Vreal_last_command,
11745 doc: /* Same as `last-command', but never altered by Lisp code. */);
11747 DEFVAR_KBOARD ("last-repeatable-command", Vlast_repeatable_command,
11748 doc: /* Last command that may be repeated.
11749 The last command executed that was not bound to an input event.
11750 This is the command `repeat' will try to repeat. */);
11752 DEFVAR_LISP ("this-command", Vthis_command,
11753 doc: /* The command now being executed.
11754 The command can set this variable; whatever is put here
11755 will be in `last-command' during the following command. */);
11756 Vthis_command = Qnil;
11758 DEFVAR_LISP ("this-command-keys-shift-translated",
11759 Vthis_command_keys_shift_translated,
11760 doc: /* Non-nil if the key sequence activating this command was shift-translated.
11761 Shift-translation occurs when there is no binding for the key sequence
11762 as entered, but a binding was found by changing an upper-case letter
11763 to lower-case, or a shifted function key to an unshifted one. */);
11764 Vthis_command_keys_shift_translated = Qnil;
11766 DEFVAR_LISP ("this-original-command", Vthis_original_command,
11767 doc: /* The command bound to the current key sequence before remapping.
11768 It equals `this-command' if the original command was not remapped through
11769 any of the active keymaps. Otherwise, the value of `this-command' is the
11770 result of looking up the original command in the active keymaps. */);
11771 Vthis_original_command = Qnil;
11773 DEFVAR_INT ("auto-save-interval", auto_save_interval,
11774 doc: /* *Number of input events between auto-saves.
11775 Zero means disable autosaving due to number of characters typed. */);
11776 auto_save_interval = 300;
11778 DEFVAR_LISP ("auto-save-timeout", Vauto_save_timeout,
11779 doc: /* *Number of seconds idle time before auto-save.
11780 Zero or nil means disable auto-saving due to idleness.
11781 After auto-saving due to this many seconds of idle time,
11782 Emacs also does a garbage collection if that seems to be warranted. */);
11783 XSETFASTINT (Vauto_save_timeout, 30);
11785 DEFVAR_LISP ("echo-keystrokes", Vecho_keystrokes,
11786 doc: /* *Nonzero means echo unfinished commands after this many seconds of pause.
11787 The value may be integer or floating point. */);
11788 Vecho_keystrokes = make_number (1);
11790 DEFVAR_INT ("polling-period", polling_period,
11791 doc: /* *Interval between polling for input during Lisp execution.
11792 The reason for polling is to make C-g work to stop a running program.
11793 Polling is needed only when using X windows and SIGIO does not work.
11794 Polling is automatically disabled in all other cases. */);
11795 polling_period = 2;
11797 DEFVAR_LISP ("double-click-time", Vdouble_click_time,
11798 doc: /* *Maximum time between mouse clicks to make a double-click.
11799 Measured in milliseconds. The value nil means disable double-click
11800 recognition; t means double-clicks have no time limit and are detected
11801 by position only. */);
11802 Vdouble_click_time = make_number (500);
11804 DEFVAR_INT ("double-click-fuzz", double_click_fuzz,
11805 doc: /* *Maximum mouse movement between clicks to make a double-click.
11806 On window-system frames, value is the number of pixels the mouse may have
11807 moved horizontally or vertically between two clicks to make a double-click.
11808 On non window-system frames, value is interpreted in units of 1/8 characters
11809 instead of pixels.
11811 This variable is also the threshold for motion of the mouse
11812 to count as a drag. */);
11813 double_click_fuzz = 3;
11815 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", inhibit_local_menu_bar_menus,
11816 doc: /* *Non-nil means inhibit local map menu bar menus. */);
11817 inhibit_local_menu_bar_menus = 0;
11819 DEFVAR_INT ("num-input-keys", num_input_keys,
11820 doc: /* Number of complete key sequences read as input so far.
11821 This includes key sequences read from keyboard macros.
11822 The number is effectively the number of interactive command invocations. */);
11823 num_input_keys = 0;
11825 DEFVAR_INT ("num-nonmacro-input-events", num_nonmacro_input_events,
11826 doc: /* Number of input events read from the keyboard so far.
11827 This does not include events generated by keyboard macros. */);
11828 num_nonmacro_input_events = 0;
11830 DEFVAR_LISP ("last-event-frame", Vlast_event_frame,
11831 doc: /* The frame in which the most recently read event occurred.
11832 If the last event came from a keyboard macro, this is set to `macro'. */);
11833 Vlast_event_frame = Qnil;
11835 /* This variable is set up in sysdep.c. */
11836 DEFVAR_LISP ("tty-erase-char", Vtty_erase_char,
11837 doc: /* The ERASE character as set by the user with stty. */);
11839 DEFVAR_LISP ("help-char", Vhelp_char,
11840 doc: /* Character to recognize as meaning Help.
11841 When it is read, do `(eval help-form)', and display result if it's a string.
11842 If the value of `help-form' is nil, this char can be read normally. */);
11843 XSETINT (Vhelp_char, Ctl ('H'));
11845 DEFVAR_LISP ("help-event-list", Vhelp_event_list,
11846 doc: /* List of input events to recognize as meaning Help.
11847 These work just like the value of `help-char' (see that). */);
11848 Vhelp_event_list = Qnil;
11850 DEFVAR_LISP ("help-form", Vhelp_form,
11851 doc: /* Form to execute when character `help-char' is read.
11852 If the form returns a string, that string is displayed.
11853 If `help-form' is nil, the help char is not recognized. */);
11854 Vhelp_form = Qnil;
11856 DEFVAR_LISP ("prefix-help-command", Vprefix_help_command,
11857 doc: /* Command to run when `help-char' character follows a prefix key.
11858 This command is used only when there is no actual binding
11859 for that character after that prefix key. */);
11860 Vprefix_help_command = Qnil;
11862 DEFVAR_LISP ("top-level", Vtop_level,
11863 doc: /* Form to evaluate when Emacs starts up.
11864 Useful to set before you dump a modified Emacs. */);
11865 Vtop_level = Qnil;
11867 DEFVAR_KBOARD ("keyboard-translate-table", Vkeyboard_translate_table,
11868 doc: /* Translate table for local keyboard input, or nil.
11869 If non-nil, the value should be a char-table. Each character read
11870 from the keyboard is looked up in this char-table. If the value found
11871 there is non-nil, then it is used instead of the actual input character.
11873 The value can also be a string or vector, but this is considered obsolete.
11874 If it is a string or vector of length N, character codes N and up are left
11875 untranslated. In a vector, an element which is nil means "no translation".
11877 This is applied to the characters supplied to input methods, not their
11878 output. See also `translation-table-for-input'.
11880 This variable has a separate binding for each terminal.
11881 See Info node `(elisp)Multiple Terminals'. */);
11883 DEFVAR_BOOL ("cannot-suspend", cannot_suspend,
11884 doc: /* Non-nil means to always spawn a subshell instead of suspending.
11885 \(Even if the operating system has support for stopping a process.\) */);
11886 cannot_suspend = 0;
11888 DEFVAR_BOOL ("menu-prompting", menu_prompting,
11889 doc: /* Non-nil means prompt with menus when appropriate.
11890 This is done when reading from a keymap that has a prompt string,
11891 for elements that have prompt strings.
11892 The menu is displayed on the screen
11893 if X menus were enabled at configuration
11894 time and the previous event was a mouse click prefix key.
11895 Otherwise, menu prompting uses the echo area. */);
11896 menu_prompting = 1;
11898 DEFVAR_LISP ("menu-prompt-more-char", menu_prompt_more_char,
11899 doc: /* Character to see next line of menu prompt.
11900 Type this character while in a menu prompt to rotate around the lines of it. */);
11901 XSETINT (menu_prompt_more_char, ' ');
11903 DEFVAR_INT ("extra-keyboard-modifiers", extra_keyboard_modifiers,
11904 doc: /* A mask of additional modifier keys to use with every keyboard character.
11905 Emacs applies the modifiers of the character stored here to each keyboard
11906 character it reads. For example, after evaluating the expression
11907 (setq extra-keyboard-modifiers ?\\C-x)
11908 all input characters will have the control modifier applied to them.
11910 Note that the character ?\\C-@, equivalent to the integer zero, does
11911 not count as a control character; rather, it counts as a character
11912 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero
11913 cancels any modification. */);
11914 extra_keyboard_modifiers = 0;
11916 DEFVAR_LISP ("deactivate-mark", Vdeactivate_mark,
11917 doc: /* If an editing command sets this to t, deactivate the mark afterward.
11918 The command loop sets this to nil before each command,
11919 and tests the value when the command returns.
11920 Buffer modification stores t in this variable. */);
11921 Vdeactivate_mark = Qnil;
11922 Qdeactivate_mark = intern_c_string ("deactivate-mark");
11923 staticpro (&Qdeactivate_mark);
11925 DEFVAR_LISP ("command-hook-internal", Vcommand_hook_internal,
11926 doc: /* Temporary storage of `pre-command-hook' or `post-command-hook'. */);
11927 Vcommand_hook_internal = Qnil;
11929 DEFVAR_LISP ("pre-command-hook", Vpre_command_hook,
11930 doc: /* Normal hook run before each command is executed.
11931 If an unhandled error happens in running this hook,
11932 the hook value is set to nil, since otherwise the error
11933 might happen repeatedly and make Emacs nonfunctional. */);
11934 Vpre_command_hook = Qnil;
11936 DEFVAR_LISP ("post-command-hook", Vpost_command_hook,
11937 doc: /* Normal hook run after each command is executed.
11938 If an unhandled error happens in running this hook,
11939 the hook value is set to nil, since otherwise the error
11940 might happen repeatedly and make Emacs nonfunctional. */);
11941 Vpost_command_hook = Qnil;
11943 #if 0
11944 DEFVAR_LISP ("echo-area-clear-hook", ...,
11945 doc: /* Normal hook run when clearing the echo area. */);
11946 #endif
11947 Qecho_area_clear_hook = intern_c_string ("echo-area-clear-hook");
11948 staticpro (&Qecho_area_clear_hook);
11949 Fset (Qecho_area_clear_hook, Qnil);
11951 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", Vlucid_menu_bar_dirty_flag,
11952 doc: /* Non-nil means menu bar, specified Lucid style, needs to be recomputed. */);
11953 Vlucid_menu_bar_dirty_flag = Qnil;
11955 DEFVAR_LISP ("menu-bar-final-items", Vmenu_bar_final_items,
11956 doc: /* List of menu bar items to move to the end of the menu bar.
11957 The elements of the list are event types that may have menu bar bindings. */);
11958 Vmenu_bar_final_items = Qnil;
11960 DEFVAR_LISP ("tool-bar-separator-image-expression", Vtool_bar_separator_image_expression,
11961 doc: /* Expression evaluating to the image spec for a tool-bar separator.
11962 This is used internally by graphical displays that do not render
11963 tool-bar separators natively. Otherwise it is unused (e.g. on GTK). */);
11964 Vtool_bar_separator_image_expression = Qnil;
11966 DEFVAR_KBOARD ("overriding-terminal-local-map",
11967 Voverriding_terminal_local_map,
11968 doc: /* Per-terminal keymap that overrides all other local keymaps.
11969 If this variable is non-nil, it is used as a keymap instead of the
11970 buffer's local map, and the minor mode keymaps and text property keymaps.
11971 It also replaces `overriding-local-map'.
11973 This variable is intended to let commands such as `universal-argument'
11974 set up a different keymap for reading the next command.
11976 `overriding-terminal-local-map' has a separate binding for each
11977 terminal device.
11978 See Info node `(elisp)Multiple Terminals'. */);
11980 DEFVAR_LISP ("overriding-local-map", Voverriding_local_map,
11981 doc: /* Keymap that overrides all other local keymaps.
11982 If this variable is non-nil, it is used as a keymap--replacing the
11983 buffer's local map, the minor mode keymaps, and char property keymaps. */);
11984 Voverriding_local_map = Qnil;
11986 DEFVAR_LISP ("overriding-local-map-menu-flag", Voverriding_local_map_menu_flag,
11987 doc: /* Non-nil means `overriding-local-map' applies to the menu bar.
11988 Otherwise, the menu bar continues to reflect the buffer's local map
11989 and the minor mode maps regardless of `overriding-local-map'. */);
11990 Voverriding_local_map_menu_flag = Qnil;
11992 DEFVAR_LISP ("special-event-map", Vspecial_event_map,
11993 doc: /* Keymap defining bindings for special events to execute at low level. */);
11994 Vspecial_event_map = Fcons (intern_c_string ("keymap"), Qnil);
11996 DEFVAR_LISP ("track-mouse", do_mouse_tracking,
11997 doc: /* *Non-nil means generate motion events for mouse motion. */);
11999 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
12000 doc: /* Alist of system-specific X windows key symbols.
12001 Each element should have the form (N . SYMBOL) where N is the
12002 numeric keysym code (sans the \"system-specific\" bit 1<<28)
12003 and SYMBOL is its name.
12005 `system-key-alist' has a separate binding for each terminal device.
12006 See Info node `(elisp)Multiple Terminals'. */);
12008 DEFVAR_KBOARD ("local-function-key-map", Vlocal_function_key_map,
12009 doc: /* Keymap that translates key sequences to key sequences during input.
12010 This is used mainly for mapping key sequences into some preferred
12011 key events (symbols).
12013 The `read-key-sequence' function replaces any subsequence bound by
12014 `local-function-key-map' with its binding. More precisely, when the
12015 active keymaps have no binding for the current key sequence but
12016 `local-function-key-map' binds a suffix of the sequence to a vector or
12017 string, `read-key-sequence' replaces the matching suffix with its
12018 binding, and continues with the new sequence.
12020 If the binding is a function, it is called with one argument (the prompt)
12021 and its return value (a key sequence) is used.
12023 The events that come from bindings in `local-function-key-map' are not
12024 themselves looked up in `local-function-key-map'.
12026 For example, suppose `local-function-key-map' binds `ESC O P' to [f1].
12027 Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing
12028 `C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix key,
12029 typing `ESC O P x' would return [f1 x].
12031 `local-function-key-map' has a separate binding for each terminal
12032 device. See Info node `(elisp)Multiple Terminals'. If you need to
12033 define a binding on all terminals, change `function-key-map'
12034 instead. Initially, `local-function-key-map' is an empty keymap that
12035 has `function-key-map' as its parent on all terminal devices. */);
12037 DEFVAR_KBOARD ("input-decode-map", Vinput_decode_map,
12038 doc: /* Keymap that decodes input escape sequences.
12039 This is used mainly for mapping ASCII function key sequences into
12040 real Emacs function key events (symbols).
12042 The `read-key-sequence' function replaces any subsequence bound by
12043 `input-decode-map' with its binding. Contrary to `function-key-map',
12044 this map applies its rebinding regardless of the presence of an ordinary
12045 binding. So it is more like `key-translation-map' except that it applies
12046 before `function-key-map' rather than after.
12048 If the binding is a function, it is called with one argument (the prompt)
12049 and its return value (a key sequence) is used.
12051 The events that come from bindings in `input-decode-map' are not
12052 themselves looked up in `input-decode-map'.
12054 This variable is keyboard-local. */);
12056 DEFVAR_LISP ("function-key-map", Vfunction_key_map,
12057 doc: /* The parent keymap of all `local-function-key-map' instances.
12058 Function key definitions that apply to all terminal devices should go
12059 here. If a mapping is defined in both the current
12060 `local-function-key-map' binding and this variable, then the local
12061 definition will take precendence. */);
12062 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
12064 DEFVAR_LISP ("key-translation-map", Vkey_translation_map,
12065 doc: /* Keymap of key translations that can override keymaps.
12066 This keymap works like `function-key-map', but comes after that,
12067 and its non-prefix bindings override ordinary bindings.
12068 Another difference is that it is global rather than keyboard-local. */);
12069 Vkey_translation_map = Fmake_sparse_keymap (Qnil);
12071 DEFVAR_LISP ("deferred-action-list", Vdeferred_action_list,
12072 doc: /* List of deferred actions to be performed at a later time.
12073 The precise format isn't relevant here; we just check whether it is nil. */);
12074 Vdeferred_action_list = Qnil;
12076 DEFVAR_LISP ("deferred-action-function", Vdeferred_action_function,
12077 doc: /* Function to call to handle deferred actions, after each command.
12078 This function is called with no arguments after each command
12079 whenever `deferred-action-list' is non-nil. */);
12080 Vdeferred_action_function = Qnil;
12082 DEFVAR_LISP ("suggest-key-bindings", Vsuggest_key_bindings,
12083 doc: /* *Non-nil means show the equivalent key-binding when M-x command has one.
12084 The value can be a length of time to show the message for.
12085 If the value is non-nil and not a number, we wait 2 seconds. */);
12086 Vsuggest_key_bindings = Qt;
12088 DEFVAR_LISP ("timer-list", Vtimer_list,
12089 doc: /* List of active absolute time timers in order of increasing time. */);
12090 Vtimer_list = Qnil;
12092 DEFVAR_LISP ("timer-idle-list", Vtimer_idle_list,
12093 doc: /* List of active idle-time timers in order of increasing time. */);
12094 Vtimer_idle_list = Qnil;
12096 DEFVAR_LISP ("input-method-function", Vinput_method_function,
12097 doc: /* If non-nil, the function that implements the current input method.
12098 It's called with one argument, a printing character that was just read.
12099 \(That means a character with code 040...0176.)
12100 Typically this function uses `read-event' to read additional events.
12101 When it does so, it should first bind `input-method-function' to nil
12102 so it will not be called recursively.
12104 The function should return a list of zero or more events
12105 to be used as input. If it wants to put back some events
12106 to be reconsidered, separately, by the input method,
12107 it can add them to the beginning of `unread-command-events'.
12109 The input method function can find in `input-method-previous-message'
12110 the previous echo area message.
12112 The input method function should refer to the variables
12113 `input-method-use-echo-area' and `input-method-exit-on-first-char'
12114 for guidance on what to do. */);
12115 Vinput_method_function = Qnil;
12117 DEFVAR_LISP ("input-method-previous-message",
12118 Vinput_method_previous_message,
12119 doc: /* When `input-method-function' is called, hold the previous echo area message.
12120 This variable exists because `read-event' clears the echo area
12121 before running the input method. It is nil if there was no message. */);
12122 Vinput_method_previous_message = Qnil;
12124 DEFVAR_LISP ("show-help-function", Vshow_help_function,
12125 doc: /* If non-nil, the function that implements the display of help.
12126 It's called with one argument, the help string to display. */);
12127 Vshow_help_function = Qnil;
12129 DEFVAR_LISP ("disable-point-adjustment", Vdisable_point_adjustment,
12130 doc: /* If non-nil, suppress point adjustment after executing a command.
12132 After a command is executed, if point is moved into a region that has
12133 special properties (e.g. composition, display), we adjust point to
12134 the boundary of the region. But, when a command sets this variable to
12135 non-nil, we suppress the point adjustment.
12137 This variable is set to nil before reading a command, and is checked
12138 just after executing the command. */);
12139 Vdisable_point_adjustment = Qnil;
12141 DEFVAR_LISP ("global-disable-point-adjustment",
12142 Vglobal_disable_point_adjustment,
12143 doc: /* *If non-nil, always suppress point adjustment.
12145 The default value is nil, in which case, point adjustment are
12146 suppressed only after special commands that set
12147 `disable-point-adjustment' (which see) to non-nil. */);
12148 Vglobal_disable_point_adjustment = Qnil;
12150 DEFVAR_LISP ("minibuffer-message-timeout", Vminibuffer_message_timeout,
12151 doc: /* *How long to display an echo-area message when the minibuffer is active.
12152 If the value is not a number, such messages don't time out. */);
12153 Vminibuffer_message_timeout = make_number (2);
12155 DEFVAR_LISP ("throw-on-input", Vthrow_on_input,
12156 doc: /* If non-nil, any keyboard input throws to this symbol.
12157 The value of that variable is passed to `quit-flag' and later causes a
12158 peculiar kind of quitting. */);
12159 Vthrow_on_input = Qnil;
12161 DEFVAR_LISP ("command-error-function", Vcommand_error_function,
12162 doc: /* If non-nil, function to output error messages.
12163 The arguments are the error data, a list of the form
12164 (SIGNALED-CONDITIONS . SIGNAL-DATA)
12165 such as just as `condition-case' would bind its variable to,
12166 the context (a string which normally goes at the start of the message),
12167 and the Lisp function within which the error was signaled. */);
12168 Vcommand_error_function = Qnil;
12170 DEFVAR_LISP ("enable-disabled-menus-and-buttons",
12171 Venable_disabled_menus_and_buttons,
12172 doc: /* If non-nil, don't ignore events produced by disabled menu items and tool-bar.
12174 Help functions bind this to allow help on disabled menu items
12175 and tool-bar buttons. */);
12176 Venable_disabled_menus_and_buttons = Qnil;
12178 DEFVAR_LISP ("select-active-regions",
12179 Vselect_active_regions,
12180 doc: /* If non-nil, an active region automatically sets the primary selection.
12181 If the value is `only', only temporarily active regions (usually made
12182 by mouse-dragging or shift-selection) set the window selection.
12184 This takes effect only when Transient Mark mode is enabled. */);
12185 Vselect_active_regions = Qt;
12187 DEFVAR_LISP ("saved-region-selection",
12188 Vsaved_region_selection,
12189 doc: /* Contents of active region prior to buffer modification.
12190 If `select-active-regions' is non-nil, Emacs sets this to the
12191 text in the region before modifying the buffer. The next
12192 `deactivate-mark' call uses this to set the window selection. */);
12193 Vsaved_region_selection = Qnil;
12195 /* Create the initial keyboard. */
12196 initial_kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
12197 init_kboard (initial_kboard);
12198 /* Vwindow_system is left at t for now. */
12199 initial_kboard->next_kboard = all_kboards;
12200 all_kboards = initial_kboard;
12203 void
12204 keys_of_keyboard (void)
12206 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
12207 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
12208 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
12209 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
12210 initial_define_key (meta_map, 'x', "execute-extended-command");
12212 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
12213 "handle-delete-frame");
12214 initial_define_lispy_key (Vspecial_event_map, "ns-put-working-text",
12215 "ns-put-working-text");
12216 initial_define_lispy_key (Vspecial_event_map, "ns-unput-working-text",
12217 "ns-unput-working-text");
12218 /* Here we used to use `ignore-event' which would simple set prefix-arg to
12219 current-prefix-arg, as is done in `handle-switch-frame'.
12220 But `handle-switch-frame is not run from the special-map.
12221 Commands from that map are run in a special way that automatically
12222 preserves the prefix-arg. Restoring the prefix arg here is not just
12223 redundant but harmful:
12224 - C-u C-x v =
12225 - current-prefix-arg is set to non-nil, prefix-arg is set to nil.
12226 - after the first prompt, the exit-minibuffer-hook is run which may
12227 iconify a frame and thus push a `iconify-frame' event.
12228 - after running exit-minibuffer-hook, current-prefix-arg is
12229 restored to the non-nil value it had before the prompt.
12230 - we enter the second prompt.
12231 current-prefix-arg is non-nil, prefix-arg is nil.
12232 - before running the first real event, we run the special iconify-frame
12233 event, but we pass the `special' arg to execute-command so
12234 current-prefix-arg and prefix-arg are left untouched.
12235 - here we foolishly copy the non-nil current-prefix-arg to prefix-arg.
12236 - the next key event will have a spuriously non-nil current-prefix-arg. */
12237 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
12238 "ignore");
12239 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
12240 "ignore");
12241 /* Handling it at such a low-level causes read_key_sequence to get
12242 * confused because it doesn't realize that the current_buffer was
12243 * changed by read_char.
12245 * initial_define_lispy_key (Vspecial_event_map, "select-window",
12246 * "handle-select-window"); */
12247 initial_define_lispy_key (Vspecial_event_map, "save-session",
12248 "handle-save-session");
12250 #ifdef HAVE_DBUS
12251 /* Define a special event which is raised for dbus callback
12252 functions. */
12253 initial_define_lispy_key (Vspecial_event_map, "dbus-event",
12254 "dbus-handle-event");
12255 #endif
12257 initial_define_lispy_key (Vspecial_event_map, "config-changed-event",
12258 "ignore");
12261 /* Mark the pointers in the kboard objects.
12262 Called by the Fgarbage_collector. */
12263 void
12264 mark_kboards (void)
12266 KBOARD *kb;
12267 Lisp_Object *p;
12268 for (kb = all_kboards; kb; kb = kb->next_kboard)
12270 if (kb->kbd_macro_buffer)
12271 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
12272 mark_object (*p);
12273 mark_object (KVAR (kb, Voverriding_terminal_local_map));
12274 mark_object (KVAR (kb, Vlast_command));
12275 mark_object (KVAR (kb, Vreal_last_command));
12276 mark_object (KVAR (kb, Vkeyboard_translate_table));
12277 mark_object (KVAR (kb, Vlast_repeatable_command));
12278 mark_object (KVAR (kb, Vprefix_arg));
12279 mark_object (KVAR (kb, Vlast_prefix_arg));
12280 mark_object (KVAR (kb, kbd_queue));
12281 mark_object (KVAR (kb, defining_kbd_macro));
12282 mark_object (KVAR (kb, Vlast_kbd_macro));
12283 mark_object (KVAR (kb, Vsystem_key_alist));
12284 mark_object (KVAR (kb, system_key_syms));
12285 mark_object (KVAR (kb, Vwindow_system));
12286 mark_object (KVAR (kb, Vinput_decode_map));
12287 mark_object (KVAR (kb, Vlocal_function_key_map));
12288 mark_object (KVAR (kb, Vdefault_minibuffer_frame));
12289 mark_object (KVAR (kb, echo_string));
12292 struct input_event *event;
12293 for (event = kbd_fetch_ptr; event != kbd_store_ptr; event++)
12295 if (event == kbd_buffer + KBD_BUFFER_SIZE)
12296 event = kbd_buffer;
12297 if (event->kind != SELECTION_REQUEST_EVENT
12298 && event->kind != SELECTION_CLEAR_EVENT)
12300 mark_object (event->x);
12301 mark_object (event->y);
12303 mark_object (event->frame_or_window);
12304 mark_object (event->arg);