Various small changes in addition to:
[emacs.git] / src / keyboard.c
blobf016a1283e4e48bb96c0f89520f789c639d90dfd
1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985,86,87,88,89,93,94,95,96,97,99,2000,01,02,03,04
3 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 2, or (at your option)
10 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; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include "termchar.h"
26 #include "termopts.h"
27 #include "lisp.h"
28 #include "termhooks.h"
29 #include "macros.h"
30 #include "keyboard.h"
31 #include "frame.h"
32 #include "window.h"
33 #include "commands.h"
34 #include "buffer.h"
35 #include "charset.h"
36 #include "disptab.h"
37 #include "dispextern.h"
38 #include "syntax.h"
39 #include "intervals.h"
40 #include "keymap.h"
41 #include "blockinput.h"
42 #include "puresize.h"
43 #include "systime.h"
44 #include "atimer.h"
45 #include <setjmp.h>
46 #include <errno.h>
48 #ifdef MSDOS
49 #include "msdos.h"
50 #include <time.h>
51 #else /* not MSDOS */
52 #ifndef VMS
53 #include <sys/ioctl.h>
54 #endif
55 #endif /* not MSDOS */
57 #include "syssignal.h"
58 #include "systty.h"
60 #include <sys/types.h>
61 #ifdef HAVE_UNISTD_H
62 #include <unistd.h>
63 #endif
65 /* This is to get the definitions of the XK_ symbols. */
66 #ifdef HAVE_X_WINDOWS
67 #include "xterm.h"
68 #endif
70 #ifdef HAVE_NTGUI
71 #include "w32term.h"
72 #endif /* HAVE_NTGUI */
74 #ifdef MAC_OS
75 #include "macterm.h"
76 #endif
78 #ifndef USE_CRT_DLL
79 extern int errno;
80 #endif
82 /* Variables for blockinput.h: */
84 /* Non-zero if interrupt input is blocked right now. */
85 int interrupt_input_blocked;
87 /* Nonzero means an input interrupt has arrived
88 during the current critical section. */
89 int interrupt_input_pending;
92 /* File descriptor to use for input. */
93 extern int input_fd;
95 #ifdef HAVE_WINDOW_SYSTEM
96 /* Make all keyboard buffers much bigger when using X windows. */
97 #ifdef MAC_OS8
98 /* But not too big (local data > 32K error) if on Mac OS Classic. */
99 #define KBD_BUFFER_SIZE 512
100 #else
101 #define KBD_BUFFER_SIZE 4096
102 #endif
103 #else /* No X-windows, character input */
104 #define KBD_BUFFER_SIZE 4096
105 #endif /* No X-windows */
107 #define abs(x) ((x) >= 0 ? (x) : -(x))
109 /* Following definition copied from eval.c */
111 struct backtrace
113 struct backtrace *next;
114 Lisp_Object *function;
115 Lisp_Object *args; /* Points to vector of args. */
116 int nargs; /* length of vector. If nargs is UNEVALLED,
117 args points to slot holding list of
118 unevalled args */
119 char evalargs;
120 /* Nonzero means call value of debugger when done with this operation. */
121 char debug_on_exit;
124 #ifdef MULTI_KBOARD
125 KBOARD *initial_kboard;
126 KBOARD *current_kboard;
127 KBOARD *all_kboards;
128 int single_kboard;
129 #else
130 KBOARD the_only_kboard;
131 #endif
133 /* Non-nil disable property on a command means
134 do not execute it; call disabled-command-hook's value instead. */
135 Lisp_Object Qdisabled, Qdisabled_command_hook;
137 #define NUM_RECENT_KEYS (100)
138 int recent_keys_index; /* Index for storing next element into recent_keys */
139 int total_keys; /* Total number of elements stored into recent_keys */
140 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
142 /* Vector holding the key sequence that invoked the current command.
143 It is reused for each command, and it may be longer than the current
144 sequence; this_command_key_count indicates how many elements
145 actually mean something.
146 It's easier to staticpro a single Lisp_Object than an array. */
147 Lisp_Object this_command_keys;
148 int this_command_key_count;
150 /* 1 after calling Freset_this_command_lengths.
151 Usually it is 0. */
152 int this_command_key_count_reset;
154 /* This vector is used as a buffer to record the events that were actually read
155 by read_key_sequence. */
156 Lisp_Object raw_keybuf;
157 int raw_keybuf_count;
159 #define GROW_RAW_KEYBUF \
160 if (raw_keybuf_count == XVECTOR (raw_keybuf)->size) \
162 int newsize = 2 * XVECTOR (raw_keybuf)->size; \
163 Lisp_Object new; \
164 new = Fmake_vector (make_number (newsize), Qnil); \
165 bcopy (XVECTOR (raw_keybuf)->contents, XVECTOR (new)->contents, \
166 raw_keybuf_count * sizeof (Lisp_Object)); \
167 raw_keybuf = new; \
170 /* Number of elements of this_command_keys
171 that precede this key sequence. */
172 int this_single_command_key_start;
174 /* Record values of this_command_key_count and echo_length ()
175 before this command was read. */
176 static int before_command_key_count;
177 static int before_command_echo_length;
179 extern int minbuf_level;
181 extern int message_enable_multibyte;
183 extern struct backtrace *backtrace_list;
185 /* If non-nil, the function that implements the display of help.
186 It's called with one argument, the help string to display. */
188 Lisp_Object Vshow_help_function;
190 /* If a string, the message displayed before displaying a help-echo
191 in the echo area. */
193 Lisp_Object Vpre_help_message;
195 /* Nonzero means do menu prompting. */
197 static int menu_prompting;
199 /* Character to see next line of menu prompt. */
201 static Lisp_Object menu_prompt_more_char;
203 /* For longjmp to where kbd input is being done. */
205 static jmp_buf getcjmp;
207 /* True while doing kbd input. */
208 int waiting_for_input;
210 /* True while displaying for echoing. Delays C-g throwing. */
212 int echoing;
214 /* Non-null means we can start echoing at the next input pause even
215 though there is something in the echo area. */
217 static struct kboard *ok_to_echo_at_next_pause;
219 /* The kboard last echoing, or null for none. Reset to 0 in
220 cancel_echoing. If non-null, and a current echo area message
221 exists, and echo_message_buffer is eq to the current message
222 buffer, we know that the message comes from echo_kboard. */
224 struct kboard *echo_kboard;
226 /* The buffer used for echoing. Set in echo_now, reset in
227 cancel_echoing. */
229 Lisp_Object echo_message_buffer;
231 /* Nonzero means disregard local maps for the menu bar. */
232 static int inhibit_local_menu_bar_menus;
234 /* Nonzero means C-g should cause immediate error-signal. */
235 int immediate_quit;
237 /* The user's ERASE setting. */
238 Lisp_Object Vtty_erase_char;
240 /* Character to recognize as the help char. */
241 Lisp_Object Vhelp_char;
243 /* List of other event types to recognize as meaning "help". */
244 Lisp_Object Vhelp_event_list;
246 /* Form to execute when help char is typed. */
247 Lisp_Object Vhelp_form;
249 /* Command to run when the help character follows a prefix key. */
250 Lisp_Object Vprefix_help_command;
252 /* List of items that should move to the end of the menu bar. */
253 Lisp_Object Vmenu_bar_final_items;
255 /* Non-nil means show the equivalent key-binding for
256 any M-x command that has one.
257 The value can be a length of time to show the message for.
258 If the value is non-nil and not a number, we wait 2 seconds. */
259 Lisp_Object Vsuggest_key_bindings;
261 /* How long to display an echo-area message when the minibuffer is active.
262 If the value is not a number, such messages don't time out. */
263 Lisp_Object Vminibuffer_message_timeout;
265 /* Character that causes a quit. Normally C-g.
267 If we are running on an ordinary terminal, this must be an ordinary
268 ASCII char, since we want to make it our interrupt character.
270 If we are not running on an ordinary terminal, it still needs to be
271 an ordinary ASCII char. This character needs to be recognized in
272 the input interrupt handler. At this point, the keystroke is
273 represented as a struct input_event, while the desired quit
274 character is specified as a lispy event. The mapping from struct
275 input_events to lispy events cannot run in an interrupt handler,
276 and the reverse mapping is difficult for anything but ASCII
277 keystrokes.
279 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
280 ASCII character. */
281 int quit_char;
283 extern Lisp_Object current_global_map;
284 extern int minibuf_level;
286 /* If non-nil, this is a map that overrides all other local maps. */
287 Lisp_Object Voverriding_local_map;
289 /* If non-nil, Voverriding_local_map applies to the menu bar. */
290 Lisp_Object Voverriding_local_map_menu_flag;
292 /* Keymap that defines special misc events that should
293 be processed immediately at a low level. */
294 Lisp_Object Vspecial_event_map;
296 /* Current depth in recursive edits. */
297 int command_loop_level;
299 /* Total number of times command_loop has read a key sequence. */
300 EMACS_INT num_input_keys;
302 /* Last input character read as a command. */
303 Lisp_Object last_command_char;
305 /* Last input character read as a command, not counting menus
306 reached by the mouse. */
307 Lisp_Object last_nonmenu_event;
309 /* Last input character read for any purpose. */
310 Lisp_Object last_input_char;
312 /* If not Qnil, a list of objects to be read as subsequent command input. */
313 Lisp_Object Vunread_command_events;
315 /* If not Qnil, a list of objects to be read as subsequent command input
316 including input method processing. */
317 Lisp_Object Vunread_input_method_events;
319 /* If not Qnil, a list of objects to be read as subsequent command input
320 but NOT including input method processing. */
321 Lisp_Object Vunread_post_input_method_events;
323 /* If not -1, an event to be read as subsequent command input. */
324 EMACS_INT unread_command_char;
326 /* If not Qnil, this is a switch-frame event which we decided to put
327 off until the end of a key sequence. This should be read as the
328 next command input, after any unread_command_events.
330 read_key_sequence uses this to delay switch-frame events until the
331 end of the key sequence; Fread_char uses it to put off switch-frame
332 events until a non-ASCII event is acceptable as input. */
333 Lisp_Object unread_switch_frame;
335 /* A mask of extra modifier bits to put into every keyboard char. */
336 EMACS_INT extra_keyboard_modifiers;
338 /* Char to use as prefix when a meta character is typed in.
339 This is bound on entry to minibuffer in case ESC is changed there. */
341 Lisp_Object meta_prefix_char;
343 /* Last size recorded for a current buffer which is not a minibuffer. */
344 static int last_non_minibuf_size;
346 /* Number of idle seconds before an auto-save and garbage collection. */
347 static Lisp_Object Vauto_save_timeout;
349 /* Total number of times read_char has returned. */
350 int num_input_events;
352 /* Total number of times read_char has returned, outside of macros. */
353 EMACS_INT num_nonmacro_input_events;
355 /* Auto-save automatically when this many characters have been typed
356 since the last time. */
358 static EMACS_INT auto_save_interval;
360 /* Value of num_nonmacro_input_events as of last auto save. */
362 int last_auto_save;
364 /* The command being executed by the command loop.
365 Commands may set this, and the value set will be copied into
366 current_kboard->Vlast_command instead of the actual command. */
367 Lisp_Object Vthis_command;
369 /* This is like Vthis_command, except that commands never set it. */
370 Lisp_Object real_this_command;
372 /* If the lookup of the command returns a binding, the original
373 command is stored in this-original-command. It is nil otherwise. */
374 Lisp_Object Vthis_original_command;
376 /* The value of point when the last command was executed. */
377 int last_point_position;
379 /* The buffer that was current when the last command was started. */
380 Lisp_Object last_point_position_buffer;
382 /* The frame in which the last input event occurred, or Qmacro if the
383 last event came from a macro. We use this to determine when to
384 generate switch-frame events. This may be cleared by functions
385 like Fselect_frame, to make sure that a switch-frame event is
386 generated by the next character. */
387 Lisp_Object internal_last_event_frame;
389 /* A user-visible version of the above, intended to allow users to
390 figure out where the last event came from, if the event doesn't
391 carry that information itself (i.e. if it was a character). */
392 Lisp_Object Vlast_event_frame;
394 /* The timestamp of the last input event we received from the X server.
395 X Windows wants this for selection ownership. */
396 unsigned long last_event_timestamp;
398 Lisp_Object Qself_insert_command;
399 Lisp_Object Qforward_char;
400 Lisp_Object Qbackward_char;
401 Lisp_Object Qundefined;
402 Lisp_Object Qtimer_event_handler;
404 /* read_key_sequence stores here the command definition of the
405 key sequence that it reads. */
406 Lisp_Object read_key_sequence_cmd;
408 /* Echo unfinished commands after this many seconds of pause. */
409 Lisp_Object Vecho_keystrokes;
411 /* Form to evaluate (if non-nil) when Emacs is started. */
412 Lisp_Object Vtop_level;
414 /* User-supplied table to translate input characters. */
415 Lisp_Object Vkeyboard_translate_table;
417 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
418 extern Lisp_Object Vfunction_key_map;
420 /* Another keymap that maps key sequences into key sequences.
421 This one takes precedence over ordinary definitions. */
422 extern Lisp_Object Vkey_translation_map;
424 /* If non-nil, this implements the current input method. */
425 Lisp_Object Vinput_method_function;
426 Lisp_Object Qinput_method_function;
428 /* When we call Vinput_method_function,
429 this holds the echo area message that was just erased. */
430 Lisp_Object Vinput_method_previous_message;
432 /* Non-nil means deactivate the mark at end of this command. */
433 Lisp_Object Vdeactivate_mark;
435 /* Menu bar specified in Lucid Emacs fashion. */
437 Lisp_Object Vlucid_menu_bar_dirty_flag;
438 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
440 Lisp_Object Qecho_area_clear_hook;
442 /* Hooks to run before and after each command. */
443 Lisp_Object Qpre_command_hook, Vpre_command_hook;
444 Lisp_Object Qpost_command_hook, Vpost_command_hook;
445 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
446 /* Hook run after a command if there's no more input soon. */
447 Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook;
449 /* Delay time in microseconds before running post-command-idle-hook. */
450 EMACS_INT post_command_idle_delay;
452 /* List of deferred actions to be performed at a later time.
453 The precise format isn't relevant here; we just check whether it is nil. */
454 Lisp_Object Vdeferred_action_list;
456 /* Function to call to handle deferred actions, when there are any. */
457 Lisp_Object Vdeferred_action_function;
458 Lisp_Object Qdeferred_action_function;
460 Lisp_Object Qinput_method_exit_on_first_char;
461 Lisp_Object Qinput_method_use_echo_area;
463 /* File in which we write all commands we read. */
464 FILE *dribble;
466 /* Nonzero if input is available. */
467 int input_pending;
469 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
470 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
472 int meta_key;
474 /* Non-zero means force key bindings update in parse_menu_item. */
476 int update_menu_bindings;
478 extern char *pending_malloc_warning;
480 /* Circular buffer for pre-read keyboard input. */
482 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
484 /* Pointer to next available character in kbd_buffer.
485 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
486 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the
487 next available char is in kbd_buffer[0]. */
488 static struct input_event *kbd_fetch_ptr;
490 /* Pointer to next place to store character in kbd_buffer. This
491 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
492 character should go in kbd_buffer[0]. */
493 static struct input_event * volatile kbd_store_ptr;
495 /* The above pair of variables forms a "queue empty" flag. When we
496 enqueue a non-hook event, we increment kbd_store_ptr. When we
497 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
498 there is input available iff the two pointers are not equal.
500 Why not just have a flag set and cleared by the enqueuing and
501 dequeuing functions? Such a flag could be screwed up by interrupts
502 at inopportune times. */
504 /* If this flag is non-nil, we check mouse_moved to see when the
505 mouse moves, and motion events will appear in the input stream.
506 Otherwise, mouse motion is ignored. */
507 Lisp_Object do_mouse_tracking;
509 /* Symbols to head events. */
510 Lisp_Object Qmouse_movement;
511 Lisp_Object Qscroll_bar_movement;
512 Lisp_Object Qswitch_frame;
513 Lisp_Object Qdelete_frame;
514 Lisp_Object Qiconify_frame;
515 Lisp_Object Qmake_frame_visible;
516 Lisp_Object Qselect_window;
517 Lisp_Object Qhelp_echo;
519 /* Symbols to denote kinds of events. */
520 Lisp_Object Qfunction_key;
521 Lisp_Object Qmouse_click;
522 #ifdef WINDOWSNT
523 Lisp_Object Qlanguage_change;
524 #endif
525 Lisp_Object Qdrag_n_drop;
526 Lisp_Object Qsave_session;
528 /* Lisp_Object Qmouse_movement; - also an event header */
530 /* Properties of event headers. */
531 Lisp_Object Qevent_kind;
532 Lisp_Object Qevent_symbol_elements;
534 /* menu item parts */
535 Lisp_Object Qmenu_alias;
536 Lisp_Object Qmenu_enable;
537 Lisp_Object QCenable, QCvisible, QChelp, QCfilter, QCkeys, QCkey_sequence;
538 Lisp_Object QCbutton, QCtoggle, QCradio;
539 extern Lisp_Object Vdefine_key_rebound_commands;
540 extern Lisp_Object Qmenu_item;
542 /* An event header symbol HEAD may have a property named
543 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
544 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
545 mask of modifiers applied to it. If present, this is used to help
546 speed up parse_modifiers. */
547 Lisp_Object Qevent_symbol_element_mask;
549 /* An unmodified event header BASE may have a property named
550 Qmodifier_cache, which is an alist mapping modifier masks onto
551 modified versions of BASE. If present, this helps speed up
552 apply_modifiers. */
553 Lisp_Object Qmodifier_cache;
555 /* Symbols to use for parts of windows. */
556 Lisp_Object Qmode_line;
557 Lisp_Object Qvertical_line;
558 Lisp_Object Qvertical_scroll_bar;
559 Lisp_Object Qmenu_bar;
560 extern Lisp_Object Qleft_margin, Qright_margin;
561 extern Lisp_Object Qleft_fringe, Qright_fringe;
562 extern Lisp_Object QCmap;
564 Lisp_Object recursive_edit_unwind (), command_loop ();
565 Lisp_Object Fthis_command_keys ();
566 Lisp_Object Qextended_command_history;
567 EMACS_TIME timer_check ();
569 extern Lisp_Object Vhistory_length, Vtranslation_table_for_input;
571 extern char *x_get_keysym_name ();
573 static void record_menu_key ();
574 static int echo_length ();
576 Lisp_Object Qpolling_period;
578 /* List of absolute timers. Appears in order of next scheduled event. */
579 Lisp_Object Vtimer_list;
581 /* List of idle time timers. Appears in order of next scheduled event. */
582 Lisp_Object Vtimer_idle_list;
584 /* Incremented whenever a timer is run. */
585 int timers_run;
587 extern Lisp_Object Vprint_level, Vprint_length;
589 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
590 happens. */
591 EMACS_TIME *input_available_clear_time;
593 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
594 Default is 1 if INTERRUPT_INPUT is defined. */
595 int interrupt_input;
597 /* Nonzero while interrupts are temporarily deferred during redisplay. */
598 int interrupts_deferred;
600 /* Nonzero means use ^S/^Q for flow control. */
601 int flow_control;
603 /* Allow m- file to inhibit use of FIONREAD. */
604 #ifdef BROKEN_FIONREAD
605 #undef FIONREAD
606 #endif
608 /* We are unable to use interrupts if FIONREAD is not available,
609 so flush SIGIO so we won't try. */
610 #ifndef FIONREAD
611 #ifdef SIGIO
612 #undef SIGIO
613 #endif
614 #endif
616 /* If we support a window system, turn on the code to poll periodically
617 to detect C-g. It isn't actually used when doing interrupt input. */
618 #if defined(HAVE_WINDOW_SYSTEM) && !defined(USE_ASYNC_EVENTS)
619 #define POLL_FOR_INPUT
620 #endif
622 /* After a command is executed, if point is moved into a region that
623 has specific properties (e.g. composition, display), we adjust
624 point to the boundary of the region. But, if a command sets this
625 variable to non-nil, we suppress this point adjustment. This
626 variable is set to nil before reading a command. */
628 Lisp_Object Vdisable_point_adjustment;
630 /* If non-nil, always disable point adjustment. */
632 Lisp_Object Vglobal_disable_point_adjustment;
634 /* The time when Emacs started being idle. */
636 static EMACS_TIME timer_idleness_start_time;
638 /* After Emacs stops being idle, this saves the last value
639 of timer_idleness_start_time from when it was idle. */
641 static EMACS_TIME timer_last_idleness_start_time;
644 /* Global variable declarations. */
646 /* Function for init_keyboard to call with no args (if nonzero). */
647 void (*keyboard_init_hook) ();
649 static int read_avail_input P_ ((int));
650 static void get_input_pending P_ ((int *, int));
651 static void get_filtered_input_pending P_ ((int *, int, int));
652 static int readable_events P_ ((int));
653 static int readable_filtered_events P_ ((int, int));
654 static Lisp_Object read_char_x_menu_prompt P_ ((int, Lisp_Object *,
655 Lisp_Object, int *));
656 static Lisp_Object read_char_x_menu_prompt ();
657 static Lisp_Object read_char_minibuf_menu_prompt P_ ((int, int,
658 Lisp_Object *));
659 static Lisp_Object make_lispy_event P_ ((struct input_event *));
660 #ifdef HAVE_MOUSE
661 static Lisp_Object make_lispy_movement P_ ((struct frame *, Lisp_Object,
662 enum scroll_bar_part,
663 Lisp_Object, Lisp_Object,
664 unsigned long));
665 #endif
666 static Lisp_Object modify_event_symbol P_ ((int, unsigned, Lisp_Object,
667 Lisp_Object, char **,
668 Lisp_Object *, unsigned));
669 static Lisp_Object make_lispy_switch_frame P_ ((Lisp_Object));
670 static int parse_solitary_modifier P_ ((Lisp_Object));
671 static int parse_solitary_modifier ();
672 static void save_getcjmp P_ ((jmp_buf));
673 static void save_getcjmp ();
674 static void restore_getcjmp P_ ((jmp_buf));
675 static Lisp_Object apply_modifiers P_ ((int, Lisp_Object));
676 static void clear_event P_ ((struct input_event *));
677 static void any_kboard_state P_ ((void));
678 static SIGTYPE interrupt_signal P_ ((int signalnum));
680 /* Nonzero means don't try to suspend even if the operating system seems
681 to support it. */
682 static int cannot_suspend;
684 extern Lisp_Object Qidentity, Qonly;
686 /* Install the string STR as the beginning of the string of echoing,
687 so that it serves as a prompt for the next character.
688 Also start echoing. */
690 void
691 echo_prompt (str)
692 Lisp_Object str;
694 current_kboard->echo_string = str;
695 current_kboard->echo_after_prompt = SCHARS (str);
696 echo_now ();
699 /* Add C to the echo string, if echoing is going on.
700 C can be a character, which is printed prettily ("M-C-x" and all that
701 jazz), or a symbol, whose name is printed. */
703 void
704 echo_char (c)
705 Lisp_Object c;
707 if (current_kboard->immediate_echo)
709 int size = KEY_DESCRIPTION_SIZE + 100;
710 char *buffer = (char *) alloca (size);
711 char *ptr = buffer;
712 Lisp_Object echo_string;
714 echo_string = current_kboard->echo_string;
716 /* If someone has passed us a composite event, use its head symbol. */
717 c = EVENT_HEAD (c);
719 if (INTEGERP (c))
721 ptr = push_key_description (XINT (c), ptr, 1);
723 else if (SYMBOLP (c))
725 Lisp_Object name = SYMBOL_NAME (c);
726 int nbytes = SBYTES (name);
728 if (size - (ptr - buffer) < nbytes)
730 int offset = ptr - buffer;
731 size = max (2 * size, size + nbytes);
732 buffer = (char *) alloca (size);
733 ptr = buffer + offset;
736 ptr += copy_text (SDATA (name), ptr, nbytes,
737 STRING_MULTIBYTE (name), 1);
740 if ((NILP (echo_string) || SCHARS (echo_string) == 0)
741 && help_char_p (c))
743 const char *text = " (Type ? for further options)";
744 int len = strlen (text);
746 if (size - (ptr - buffer) < len)
748 int offset = ptr - buffer;
749 size += len;
750 buffer = (char *) alloca (size);
751 ptr = buffer + offset;
754 bcopy (text, ptr, len);
755 ptr += len;
758 /* Replace a dash from echo_dash with a space, otherwise
759 add a space at the end as a separator between keys. */
760 if (STRINGP (echo_string)
761 && SCHARS (echo_string) > 1)
763 Lisp_Object last_char, prev_char, idx;
765 idx = make_number (SCHARS (echo_string) - 2);
766 prev_char = Faref (echo_string, idx);
768 idx = make_number (SCHARS (echo_string) - 1);
769 last_char = Faref (echo_string, idx);
771 /* We test PREV_CHAR to make sure this isn't the echoing
772 of a minus-sign. */
773 if (XINT (last_char) == '-' && XINT (prev_char) != ' ')
774 Faset (echo_string, idx, make_number (' '));
775 else
776 echo_string = concat2 (echo_string, build_string (" "));
779 current_kboard->echo_string
780 = concat2 (echo_string, make_string (buffer, ptr - buffer));
782 echo_now ();
786 /* Temporarily add a dash to the end of the echo string if it's not
787 empty, so that it serves as a mini-prompt for the very next character. */
789 void
790 echo_dash ()
792 /* Do nothing if not echoing at all. */
793 if (NILP (current_kboard->echo_string))
794 return;
796 if (!current_kboard->immediate_echo
797 && SCHARS (current_kboard->echo_string) == 0)
798 return;
800 /* Do nothing if we just printed a prompt. */
801 if (current_kboard->echo_after_prompt
802 == SCHARS (current_kboard->echo_string))
803 return;
805 /* Put a dash at the end of the buffer temporarily,
806 but make it go away when the next character is added. */
807 current_kboard->echo_string = concat2 (current_kboard->echo_string,
808 build_string ("-"));
809 echo_now ();
812 /* Display the current echo string, and begin echoing if not already
813 doing so. */
815 void
816 echo_now ()
818 if (!current_kboard->immediate_echo)
820 int i;
821 current_kboard->immediate_echo = 1;
823 for (i = 0; i < this_command_key_count; i++)
825 Lisp_Object c;
827 /* Set before_command_echo_length to the value that would
828 have been saved before the start of this subcommand in
829 command_loop_1, if we had already been echoing then. */
830 if (i == this_single_command_key_start)
831 before_command_echo_length = echo_length ();
833 c = XVECTOR (this_command_keys)->contents[i];
834 if (! (EVENT_HAS_PARAMETERS (c)
835 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
836 echo_char (c);
839 /* Set before_command_echo_length to the value that would
840 have been saved before the start of this subcommand in
841 command_loop_1, if we had already been echoing then. */
842 if (this_command_key_count == this_single_command_key_start)
843 before_command_echo_length = echo_length ();
845 /* Put a dash at the end to invite the user to type more. */
846 echo_dash ();
849 echoing = 1;
850 message3_nolog (current_kboard->echo_string,
851 SBYTES (current_kboard->echo_string),
852 STRING_MULTIBYTE (current_kboard->echo_string));
853 echoing = 0;
855 /* Record in what buffer we echoed, and from which kboard. */
856 echo_message_buffer = echo_area_buffer[0];
857 echo_kboard = current_kboard;
859 if (waiting_for_input && !NILP (Vquit_flag))
860 quit_throw_to_read_char ();
863 /* Turn off echoing, for the start of a new command. */
865 void
866 cancel_echoing ()
868 current_kboard->immediate_echo = 0;
869 current_kboard->echo_after_prompt = -1;
870 current_kboard->echo_string = Qnil;
871 ok_to_echo_at_next_pause = NULL;
872 echo_kboard = NULL;
873 echo_message_buffer = Qnil;
876 /* Return the length of the current echo string. */
878 static int
879 echo_length ()
881 return (STRINGP (current_kboard->echo_string)
882 ? SCHARS (current_kboard->echo_string)
883 : 0);
886 /* Truncate the current echo message to its first LEN chars.
887 This and echo_char get used by read_key_sequence when the user
888 switches frames while entering a key sequence. */
890 static void
891 echo_truncate (nchars)
892 int nchars;
894 if (STRINGP (current_kboard->echo_string))
895 current_kboard->echo_string
896 = Fsubstring (current_kboard->echo_string,
897 make_number (0), make_number (nchars));
898 truncate_echo_area (nchars);
902 /* Functions for manipulating this_command_keys. */
903 static void
904 add_command_key (key)
905 Lisp_Object key;
907 #if 0 /* Not needed after we made Freset_this_command_lengths
908 do the job immediately. */
909 /* If reset-this-command-length was called recently, obey it now.
910 See the doc string of that function for an explanation of why. */
911 if (before_command_restore_flag)
913 this_command_key_count = before_command_key_count_1;
914 if (this_command_key_count < this_single_command_key_start)
915 this_single_command_key_start = this_command_key_count;
916 echo_truncate (before_command_echo_length_1);
917 before_command_restore_flag = 0;
919 #endif
921 if (this_command_key_count >= ASIZE (this_command_keys))
922 this_command_keys = larger_vector (this_command_keys,
923 2 * ASIZE (this_command_keys),
924 Qnil);
926 AREF (this_command_keys, this_command_key_count) = key;
927 ++this_command_key_count;
931 Lisp_Object
932 recursive_edit_1 ()
934 int count = SPECPDL_INDEX ();
935 Lisp_Object val;
937 if (command_loop_level > 0)
939 specbind (Qstandard_output, Qt);
940 specbind (Qstandard_input, Qt);
943 #ifdef HAVE_X_WINDOWS
944 /* The command loop has started an hourglass timer, so we have to
945 cancel it here, otherwise it will fire because the recursive edit
946 can take some time. Do not check for display_hourglass_p here,
947 because it could already be nil. */
948 cancel_hourglass ();
949 #endif
951 /* This function may have been called from a debugger called from
952 within redisplay, for instance by Edebugging a function called
953 from fontification-functions. We want to allow redisplay in
954 the debugging session.
956 The recursive edit is left with a `(throw exit ...)'. The `exit'
957 tag is not caught anywhere in redisplay, i.e. when we leave the
958 recursive edit, the original redisplay leading to the recursive
959 edit will be unwound. The outcome should therefore be safe. */
960 specbind (Qinhibit_redisplay, Qnil);
961 redisplaying_p = 0;
963 val = command_loop ();
964 if (EQ (val, Qt))
965 Fsignal (Qquit, Qnil);
966 /* Handle throw from read_minibuf when using minibuffer
967 while it's active but we're in another window. */
968 if (STRINGP (val))
969 Fsignal (Qerror, Fcons (val, Qnil));
971 return unbind_to (count, Qnil);
974 /* When an auto-save happens, record the "time", and don't do again soon. */
976 void
977 record_auto_save ()
979 last_auto_save = num_nonmacro_input_events;
982 /* Make an auto save happen as soon as possible at command level. */
984 void
985 force_auto_save_soon ()
987 last_auto_save = - auto_save_interval - 1;
989 record_asynch_buffer_change ();
992 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
993 doc: /* Invoke the editor command loop recursively.
994 To get out of the recursive edit, a command can do `(throw 'exit nil)';
995 that tells this function to return.
996 Alternately, `(throw 'exit t)' makes this function signal an error.
997 This function is called by the editor initialization to begin editing. */)
1000 int count = SPECPDL_INDEX ();
1001 Lisp_Object buffer;
1003 /* If we enter while input is blocked, don't lock up here.
1004 This may happen through the debugger during redisplay. */
1005 if (INPUT_BLOCKED_P)
1006 return Qnil;
1008 command_loop_level++;
1009 update_mode_lines = 1;
1011 if (command_loop_level
1012 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
1013 buffer = Fcurrent_buffer ();
1014 else
1015 buffer = Qnil;
1017 /* If we leave recursive_edit_1 below with a `throw' for instance,
1018 like it is done in the splash screen display, we have to
1019 make sure that we restore single_kboard as command_loop_1
1020 would have done if it were left normally. */
1021 record_unwind_protect (recursive_edit_unwind,
1022 Fcons (buffer, single_kboard ? Qt : Qnil));
1024 recursive_edit_1 ();
1025 return unbind_to (count, Qnil);
1028 Lisp_Object
1029 recursive_edit_unwind (info)
1030 Lisp_Object info;
1032 if (BUFFERP (XCAR (info)))
1033 Fset_buffer (XCAR (info));
1035 if (NILP (XCDR (info)))
1036 any_kboard_state ();
1037 else
1038 single_kboard_state ();
1040 command_loop_level--;
1041 update_mode_lines = 1;
1042 return Qnil;
1046 static void
1047 any_kboard_state ()
1049 #ifdef MULTI_KBOARD
1050 #if 0 /* Theory: if there's anything in Vunread_command_events,
1051 it will right away be read by read_key_sequence,
1052 and then if we do switch KBOARDS, it will go into the side
1053 queue then. So we don't need to do anything special here -- rms. */
1054 if (CONSP (Vunread_command_events))
1056 current_kboard->kbd_queue
1057 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
1058 current_kboard->kbd_queue_has_data = 1;
1060 Vunread_command_events = Qnil;
1061 #endif
1062 single_kboard = 0;
1063 #endif
1066 /* Switch to the single-kboard state, making current_kboard
1067 the only KBOARD from which further input is accepted. */
1069 void
1070 single_kboard_state ()
1072 #ifdef MULTI_KBOARD
1073 single_kboard = 1;
1074 #endif
1077 /* Maintain a stack of kboards, so other parts of Emacs
1078 can switch temporarily to the kboard of a given frame
1079 and then revert to the previous status. */
1081 struct kboard_stack
1083 KBOARD *kboard;
1084 struct kboard_stack *next;
1087 static struct kboard_stack *kboard_stack;
1089 void
1090 push_frame_kboard (f)
1091 FRAME_PTR f;
1093 #ifdef MULTI_KBOARD
1094 struct kboard_stack *p
1095 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
1097 p->next = kboard_stack;
1098 p->kboard = current_kboard;
1099 kboard_stack = p;
1101 current_kboard = FRAME_KBOARD (f);
1102 #endif
1105 void
1106 pop_frame_kboard ()
1108 #ifdef MULTI_KBOARD
1109 struct kboard_stack *p = kboard_stack;
1110 current_kboard = p->kboard;
1111 kboard_stack = p->next;
1112 xfree (p);
1113 #endif
1116 /* Handle errors that are not handled at inner levels
1117 by printing an error message and returning to the editor command loop. */
1119 Lisp_Object
1120 cmd_error (data)
1121 Lisp_Object data;
1123 Lisp_Object old_level, old_length;
1124 char macroerror[50];
1126 #ifdef HAVE_X_WINDOWS
1127 if (display_hourglass_p)
1128 cancel_hourglass ();
1129 #endif
1131 if (!NILP (executing_macro))
1133 if (executing_macro_iterations == 1)
1134 sprintf (macroerror, "After 1 kbd macro iteration: ");
1135 else
1136 sprintf (macroerror, "After %d kbd macro iterations: ",
1137 executing_macro_iterations);
1139 else
1140 *macroerror = 0;
1142 Vstandard_output = Qt;
1143 Vstandard_input = Qt;
1144 Vexecuting_macro = Qnil;
1145 executing_macro = Qnil;
1146 current_kboard->Vprefix_arg = Qnil;
1147 current_kboard->Vlast_prefix_arg = Qnil;
1148 cancel_echoing ();
1150 /* Avoid unquittable loop if data contains a circular list. */
1151 old_level = Vprint_level;
1152 old_length = Vprint_length;
1153 XSETFASTINT (Vprint_level, 10);
1154 XSETFASTINT (Vprint_length, 10);
1155 cmd_error_internal (data, macroerror);
1156 Vprint_level = old_level;
1157 Vprint_length = old_length;
1159 Vquit_flag = Qnil;
1161 Vinhibit_quit = Qnil;
1162 #ifdef MULTI_KBOARD
1163 if (command_loop_level == 0 && minibuf_level == 0)
1164 any_kboard_state ();
1165 #endif
1167 return make_number (0);
1170 /* Take actions on handling an error. DATA is the data that describes
1171 the error.
1173 CONTEXT is a C-string containing ASCII characters only which
1174 describes the context in which the error happened. If we need to
1175 generalize CONTEXT to allow multibyte characters, make it a Lisp
1176 string. */
1178 void
1179 cmd_error_internal (data, context)
1180 Lisp_Object data;
1181 char *context;
1183 Lisp_Object stream;
1184 int kill_emacs_p = 0;
1185 struct frame *sf = SELECTED_FRAME ();
1187 Vquit_flag = Qnil;
1188 Vinhibit_quit = Qt;
1189 clear_message (1, 0);
1191 /* If the window system or terminal frame hasn't been initialized
1192 yet, or we're not interactive, it's best to dump this message out
1193 to stderr and exit. */
1194 if (!sf->glyphs_initialized_p
1195 /* This is the case of the frame dumped with Emacs, when we're
1196 running under a window system. */
1197 || (!NILP (Vwindow_system)
1198 && !inhibit_window_system
1199 && FRAME_TERMCAP_P (sf))
1200 || noninteractive)
1202 stream = Qexternal_debugging_output;
1203 kill_emacs_p = 1;
1205 else
1207 Fdiscard_input ();
1208 message_log_maybe_newline ();
1209 bitch_at_user ();
1210 stream = Qt;
1213 /* The immediate context is not interesting for Quits,
1214 since they are asyncronous. */
1215 if (EQ (XCAR (data), Qquit))
1216 Vsignaling_function = Qnil;
1218 print_error_message (data, stream, context, Vsignaling_function);
1220 Vsignaling_function = Qnil;
1222 /* If the window system or terminal frame hasn't been initialized
1223 yet, or we're in -batch mode, this error should cause Emacs to exit. */
1224 if (kill_emacs_p)
1226 Fterpri (stream);
1227 Fkill_emacs (make_number (-1));
1231 Lisp_Object command_loop_1 ();
1232 Lisp_Object command_loop_2 ();
1233 Lisp_Object top_level_1 ();
1235 /* Entry to editor-command-loop.
1236 This level has the catches for exiting/returning to editor command loop.
1237 It returns nil to exit recursive edit, t to abort it. */
1239 Lisp_Object
1240 command_loop ()
1242 if (command_loop_level > 0 || minibuf_level > 0)
1244 Lisp_Object val;
1245 val = internal_catch (Qexit, command_loop_2, Qnil);
1246 executing_macro = Qnil;
1247 return val;
1249 else
1250 while (1)
1252 internal_catch (Qtop_level, top_level_1, Qnil);
1253 /* Reset single_kboard in case top-level set it while
1254 evaluating an -f option, or we are stuck there for some
1255 other reason. */
1256 any_kboard_state ();
1257 internal_catch (Qtop_level, command_loop_2, Qnil);
1258 executing_macro = Qnil;
1260 /* End of file in -batch run causes exit here. */
1261 if (noninteractive)
1262 Fkill_emacs (Qt);
1266 /* Here we catch errors in execution of commands within the
1267 editing loop, and reenter the editing loop.
1268 When there is an error, cmd_error runs and returns a non-nil
1269 value to us. A value of nil means that command_loop_1 itself
1270 returned due to end of file (or end of kbd macro). */
1272 Lisp_Object
1273 command_loop_2 ()
1275 register Lisp_Object val;
1278 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
1279 while (!NILP (val));
1281 return Qnil;
1284 Lisp_Object
1285 top_level_2 ()
1287 return Feval (Vtop_level);
1290 Lisp_Object
1291 top_level_1 ()
1293 /* On entry to the outer level, run the startup file */
1294 if (!NILP (Vtop_level))
1295 internal_condition_case (top_level_2, Qerror, cmd_error);
1296 else if (!NILP (Vpurify_flag))
1297 message ("Bare impure Emacs (standard Lisp code not loaded)");
1298 else
1299 message ("Bare Emacs (standard Lisp code not loaded)");
1300 return Qnil;
1303 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1304 doc: /* Exit all recursive editing levels. */)
1307 #ifdef HAVE_X_WINDOWS
1308 if (display_hourglass_p)
1309 cancel_hourglass ();
1310 #endif
1312 /* Unblock input if we enter with input blocked. This may happen if
1313 redisplay traps e.g. during tool-bar update with input blocked. */
1314 while (INPUT_BLOCKED_P)
1315 UNBLOCK_INPUT;
1317 return Fthrow (Qtop_level, Qnil);
1320 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1321 doc: /* Exit from the innermost recursive edit or minibuffer. */)
1324 if (command_loop_level > 0 || minibuf_level > 0)
1325 Fthrow (Qexit, Qnil);
1327 error ("No recursive edit is in progress");
1328 return Qnil;
1331 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1332 doc: /* Abort the command that requested this recursive edit or minibuffer input. */)
1335 if (command_loop_level > 0 || minibuf_level > 0)
1336 Fthrow (Qexit, Qt);
1338 error ("No recursive edit is in progress");
1339 return Qnil;
1342 /* This is the actual command reading loop,
1343 sans error-handling encapsulation. */
1345 static int read_key_sequence P_ ((Lisp_Object *, int, Lisp_Object,
1346 int, int, int));
1347 void safe_run_hooks P_ ((Lisp_Object));
1348 static void adjust_point_for_property P_ ((int, int));
1350 /* Cancel hourglass from protect_unwind.
1351 ARG is not used. */
1352 #ifdef HAVE_X_WINDOWS
1353 static Lisp_Object
1354 cancel_hourglass_unwind (arg)
1355 Lisp_Object arg;
1357 cancel_hourglass ();
1358 return Qnil;
1360 #endif
1362 Lisp_Object
1363 command_loop_1 ()
1365 Lisp_Object cmd;
1366 int lose;
1367 int nonundocount;
1368 Lisp_Object keybuf[30];
1369 int i;
1370 int no_direct;
1371 int prev_modiff;
1372 struct buffer *prev_buffer = NULL;
1373 #ifdef MULTI_KBOARD
1374 int was_locked = single_kboard;
1375 #endif
1376 int already_adjusted;
1378 current_kboard->Vprefix_arg = Qnil;
1379 current_kboard->Vlast_prefix_arg = Qnil;
1380 Vdeactivate_mark = Qnil;
1381 waiting_for_input = 0;
1382 cancel_echoing ();
1384 nonundocount = 0;
1385 this_command_key_count = 0;
1386 this_command_key_count_reset = 0;
1387 this_single_command_key_start = 0;
1389 if (NILP (Vmemory_full))
1391 /* Make sure this hook runs after commands that get errors and
1392 throw to top level. */
1393 /* Note that the value cell will never directly contain nil
1394 if the symbol is a local variable. */
1395 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1396 safe_run_hooks (Qpost_command_hook);
1398 /* If displaying a message, resize the echo area window to fit
1399 that message's size exactly. */
1400 if (!NILP (echo_area_buffer[0]))
1401 resize_echo_area_exactly ();
1403 if (!NILP (Vdeferred_action_list))
1404 call0 (Vdeferred_action_function);
1406 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1408 if (NILP (Vunread_command_events)
1409 && NILP (Vunread_input_method_events)
1410 && NILP (Vunread_post_input_method_events)
1411 && NILP (Vexecuting_macro)
1412 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1413 safe_run_hooks (Qpost_command_idle_hook);
1417 Vmemory_full = Qnil;
1419 /* Do this after running Vpost_command_hook, for consistency. */
1420 current_kboard->Vlast_command = Vthis_command;
1421 current_kboard->Vreal_last_command = real_this_command;
1423 while (1)
1425 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1426 Fkill_emacs (Qnil);
1428 /* Make sure the current window's buffer is selected. */
1429 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1430 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1432 /* Display any malloc warning that just came out. Use while because
1433 displaying one warning can cause another. */
1435 while (pending_malloc_warning)
1436 display_malloc_warning ();
1438 no_direct = 0;
1440 Vdeactivate_mark = Qnil;
1442 /* If minibuffer on and echo area in use,
1443 wait a short time and redraw minibuffer. */
1445 if (minibuf_level
1446 && !NILP (echo_area_buffer[0])
1447 && EQ (minibuf_window, echo_area_window)
1448 && NUMBERP (Vminibuffer_message_timeout))
1450 /* Bind inhibit-quit to t so that C-g gets read in
1451 rather than quitting back to the minibuffer. */
1452 int count = SPECPDL_INDEX ();
1453 specbind (Qinhibit_quit, Qt);
1455 Fsit_for (Vminibuffer_message_timeout, Qnil, Qnil);
1456 /* Clear the echo area. */
1457 message2 (0, 0, 0);
1458 safe_run_hooks (Qecho_area_clear_hook);
1460 unbind_to (count, Qnil);
1462 /* If a C-g came in before, treat it as input now. */
1463 if (!NILP (Vquit_flag))
1465 Vquit_flag = Qnil;
1466 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1470 #ifdef C_ALLOCA
1471 alloca (0); /* Cause a garbage collection now */
1472 /* Since we can free the most stuff here. */
1473 #endif /* C_ALLOCA */
1475 #if 0
1476 /* Select the frame that the last event came from. Usually,
1477 switch-frame events will take care of this, but if some lisp
1478 code swallows a switch-frame event, we'll fix things up here.
1479 Is this a good idea? */
1480 if (FRAMEP (internal_last_event_frame)
1481 && !EQ (internal_last_event_frame, selected_frame))
1482 Fselect_frame (internal_last_event_frame, Qnil);
1483 #endif
1484 /* If it has changed current-menubar from previous value,
1485 really recompute the menubar from the value. */
1486 if (! NILP (Vlucid_menu_bar_dirty_flag)
1487 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1488 call0 (Qrecompute_lucid_menubar);
1490 before_command_key_count = this_command_key_count;
1491 before_command_echo_length = echo_length ();
1493 Vthis_command = Qnil;
1494 real_this_command = Qnil;
1496 /* Read next key sequence; i gets its length. */
1497 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1498 Qnil, 0, 1, 1);
1500 /* A filter may have run while we were reading the input. */
1501 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1502 Fkill_emacs (Qnil);
1503 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1504 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1506 ++num_input_keys;
1508 /* Now we have read a key sequence of length I,
1509 or else I is 0 and we found end of file. */
1511 if (i == 0) /* End of file -- happens only in */
1512 return Qnil; /* a kbd macro, at the end. */
1513 /* -1 means read_key_sequence got a menu that was rejected.
1514 Just loop around and read another command. */
1515 if (i == -1)
1517 cancel_echoing ();
1518 this_command_key_count = 0;
1519 this_command_key_count_reset = 0;
1520 this_single_command_key_start = 0;
1521 goto finalize;
1524 last_command_char = keybuf[i - 1];
1526 /* If the previous command tried to force a specific window-start,
1527 forget about that, in case this command moves point far away
1528 from that position. But also throw away beg_unchanged and
1529 end_unchanged information in that case, so that redisplay will
1530 update the whole window properly. */
1531 if (!NILP (XWINDOW (selected_window)->force_start))
1533 struct buffer *b;
1534 XWINDOW (selected_window)->force_start = Qnil;
1535 b = XBUFFER (XWINDOW (selected_window)->buffer);
1536 BUF_BEG_UNCHANGED (b) = BUF_END_UNCHANGED (b) = 0;
1539 cmd = read_key_sequence_cmd;
1540 if (!NILP (Vexecuting_macro))
1542 if (!NILP (Vquit_flag))
1544 Vexecuting_macro = Qt;
1545 QUIT; /* Make some noise. */
1546 /* Will return since macro now empty. */
1550 /* Do redisplay processing after this command except in special
1551 cases identified below. */
1552 prev_buffer = current_buffer;
1553 prev_modiff = MODIFF;
1554 last_point_position = PT;
1555 XSETBUFFER (last_point_position_buffer, prev_buffer);
1557 /* By default, we adjust point to a boundary of a region that
1558 has such a property that should be treated intangible
1559 (e.g. composition, display). But, some commands will set
1560 this variable differently. */
1561 Vdisable_point_adjustment = Qnil;
1563 /* Process filters and timers may have messed with deactivate-mark.
1564 reset it before we execute the command. */
1565 Vdeactivate_mark = Qnil;
1567 /* Remap command through active keymaps */
1568 Vthis_original_command = cmd;
1569 if (SYMBOLP (cmd))
1571 Lisp_Object cmd1;
1572 if (cmd1 = Fcommand_remapping (cmd), !NILP (cmd1))
1573 cmd = cmd1;
1576 /* Execute the command. */
1578 Vthis_command = cmd;
1579 real_this_command = cmd;
1580 /* Note that the value cell will never directly contain nil
1581 if the symbol is a local variable. */
1582 if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
1583 safe_run_hooks (Qpre_command_hook);
1585 already_adjusted = 0;
1587 if (NILP (Vthis_command))
1589 /* nil means key is undefined. */
1590 bitch_at_user ();
1591 current_kboard->defining_kbd_macro = Qnil;
1592 update_mode_lines = 1;
1593 current_kboard->Vprefix_arg = Qnil;
1595 else
1597 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1599 /* In case we jump to directly_done. */
1600 Vcurrent_prefix_arg = current_kboard->Vprefix_arg;
1602 /* Recognize some common commands in common situations and
1603 do them directly. */
1604 if (EQ (Vthis_command, Qforward_char) && PT < ZV)
1606 struct Lisp_Char_Table *dp
1607 = window_display_table (XWINDOW (selected_window));
1608 lose = FETCH_CHAR (PT_BYTE);
1609 SET_PT (PT + 1);
1610 if (! NILP (Vpost_command_hook))
1611 /* Put this before calling adjust_point_for_property
1612 so it will only get called once in any case. */
1613 goto directly_done;
1614 if (current_buffer == prev_buffer
1615 && last_point_position != PT
1616 && NILP (Vdisable_point_adjustment)
1617 && NILP (Vglobal_disable_point_adjustment))
1618 adjust_point_for_property (last_point_position, 0);
1619 already_adjusted = 1;
1620 if (PT == last_point_position + 1
1621 && (dp
1622 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1623 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1624 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1625 && (lose >= 0x20 && lose < 0x7f)))
1626 : (lose >= 0x20 && lose < 0x7f))
1627 /* To extract the case of continuation on
1628 wide-column characters. */
1629 && (WIDTH_BY_CHAR_HEAD (FETCH_BYTE (PT_BYTE)) == 1)
1630 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1631 >= MODIFF)
1632 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1633 >= OVERLAY_MODIFF)
1634 && (XFASTINT (XWINDOW (selected_window)->last_point)
1635 == PT - 1)
1636 && !windows_or_buffers_changed
1637 && EQ (current_buffer->selective_display, Qnil)
1638 && !detect_input_pending ()
1639 && NILP (XWINDOW (selected_window)->column_number_displayed)
1640 && NILP (Vexecuting_macro))
1641 direct_output_forward_char (1);
1642 goto directly_done;
1644 else if (EQ (Vthis_command, Qbackward_char) && PT > BEGV)
1646 struct Lisp_Char_Table *dp
1647 = window_display_table (XWINDOW (selected_window));
1648 SET_PT (PT - 1);
1649 lose = FETCH_CHAR (PT_BYTE);
1650 if (! NILP (Vpost_command_hook))
1651 goto directly_done;
1652 if (current_buffer == prev_buffer
1653 && last_point_position != PT
1654 && NILP (Vdisable_point_adjustment)
1655 && NILP (Vglobal_disable_point_adjustment))
1656 adjust_point_for_property (last_point_position, 0);
1657 already_adjusted = 1;
1658 if (PT == last_point_position - 1
1659 && (dp
1660 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1661 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1662 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1663 && (lose >= 0x20 && lose < 0x7f)))
1664 : (lose >= 0x20 && lose < 0x7f))
1665 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1666 >= MODIFF)
1667 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1668 >= OVERLAY_MODIFF)
1669 && (XFASTINT (XWINDOW (selected_window)->last_point)
1670 == PT + 1)
1671 && !windows_or_buffers_changed
1672 && EQ (current_buffer->selective_display, Qnil)
1673 && !detect_input_pending ()
1674 && NILP (XWINDOW (selected_window)->column_number_displayed)
1675 && NILP (Vexecuting_macro))
1676 direct_output_forward_char (-1);
1677 goto directly_done;
1679 else if (EQ (Vthis_command, Qself_insert_command)
1680 /* Try this optimization only on char keystrokes. */
1681 && NATNUMP (last_command_char)
1682 && CHAR_VALID_P (XFASTINT (last_command_char), 0))
1684 unsigned int c
1685 = translate_char (Vtranslation_table_for_input,
1686 XFASTINT (last_command_char), 0, 0, 0);
1687 int value;
1688 if (NILP (Vexecuting_macro)
1689 && !EQ (minibuf_window, selected_window))
1691 if (!nonundocount || nonundocount >= 20)
1693 Fundo_boundary ();
1694 nonundocount = 0;
1696 nonundocount++;
1699 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1700 < MODIFF)
1701 || (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1702 < OVERLAY_MODIFF)
1703 || (XFASTINT (XWINDOW (selected_window)->last_point)
1704 != PT)
1705 || MODIFF <= SAVE_MODIFF
1706 || windows_or_buffers_changed
1707 || !EQ (current_buffer->selective_display, Qnil)
1708 || detect_input_pending ()
1709 || !NILP (XWINDOW (selected_window)->column_number_displayed)
1710 || !NILP (Vexecuting_macro));
1712 value = internal_self_insert (c, 0);
1714 if (value == 2)
1715 nonundocount = 0;
1717 if (! NILP (Vpost_command_hook))
1718 /* Put this before calling adjust_point_for_property
1719 so it will only get called once in any case. */
1720 goto directly_done;
1722 /* VALUE == 1 when AFTER-CHANGE functions are
1723 installed which is the case most of the time
1724 because FONT-LOCK installs one. */
1725 if (!lose && !value)
1726 direct_output_for_insert (c);
1727 goto directly_done;
1731 /* Here for a command that isn't executed directly */
1734 #ifdef HAVE_X_WINDOWS
1735 int scount = SPECPDL_INDEX ();
1737 if (display_hourglass_p
1738 && NILP (Vexecuting_macro))
1740 record_unwind_protect (cancel_hourglass_unwind, Qnil);
1741 start_hourglass ();
1743 #endif
1745 nonundocount = 0;
1746 if (NILP (current_kboard->Vprefix_arg))
1747 Fundo_boundary ();
1748 Fcommand_execute (Vthis_command, Qnil, Qnil, Qnil);
1750 #ifdef HAVE_X_WINDOWS
1751 /* Do not check display_hourglass_p here, because
1752 Fcommand_execute could change it, but we should cancel
1753 hourglass cursor anyway.
1754 But don't cancel the hourglass within a macro
1755 just because a command in the macro finishes. */
1756 if (NILP (Vexecuting_macro))
1757 unbind_to (scount, Qnil);
1758 #endif
1761 directly_done: ;
1762 current_kboard->Vlast_prefix_arg = Vcurrent_prefix_arg;
1764 /* Note that the value cell will never directly contain nil
1765 if the symbol is a local variable. */
1766 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1767 safe_run_hooks (Qpost_command_hook);
1769 /* If displaying a message, resize the echo area window to fit
1770 that message's size exactly. */
1771 if (!NILP (echo_area_buffer[0]))
1772 resize_echo_area_exactly ();
1774 if (!NILP (Vdeferred_action_list))
1775 safe_run_hooks (Qdeferred_action_function);
1777 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1779 if (NILP (Vunread_command_events)
1780 && NILP (Vunread_input_method_events)
1781 && NILP (Vunread_post_input_method_events)
1782 && NILP (Vexecuting_macro)
1783 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1784 safe_run_hooks (Qpost_command_idle_hook);
1787 /* If there is a prefix argument,
1788 1) We don't want Vlast_command to be ``universal-argument''
1789 (that would be dumb), so don't set Vlast_command,
1790 2) we want to leave echoing on so that the prefix will be
1791 echoed as part of this key sequence, so don't call
1792 cancel_echoing, and
1793 3) we want to leave this_command_key_count non-zero, so that
1794 read_char will realize that it is re-reading a character, and
1795 not echo it a second time.
1797 If the command didn't actually create a prefix arg,
1798 but is merely a frame event that is transparent to prefix args,
1799 then the above doesn't apply. */
1800 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1802 current_kboard->Vlast_command = Vthis_command;
1803 current_kboard->Vreal_last_command = real_this_command;
1804 cancel_echoing ();
1805 this_command_key_count = 0;
1806 this_command_key_count_reset = 0;
1807 this_single_command_key_start = 0;
1810 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1812 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1814 /* We could also call `deactivate'mark'. */
1815 if (EQ (Vtransient_mark_mode, Qlambda))
1816 Vtransient_mark_mode = Qnil;
1817 else
1819 current_buffer->mark_active = Qnil;
1820 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1823 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1824 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1827 /* Setting transient-mark-mode to `only' is a way of
1828 turning it on for just one command. */
1829 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1831 if (EQ (Vtransient_mark_mode, Qidentity))
1832 Vtransient_mark_mode = Qnil;
1833 if (EQ (Vtransient_mark_mode, Qonly))
1834 Vtransient_mark_mode = Qidentity;
1837 finalize:
1839 if (current_buffer == prev_buffer
1840 && last_point_position != PT
1841 && NILP (Vdisable_point_adjustment)
1842 && NILP (Vglobal_disable_point_adjustment)
1843 && !already_adjusted)
1844 adjust_point_for_property (last_point_position, MODIFF != prev_modiff);
1846 /* Install chars successfully executed in kbd macro. */
1848 if (!NILP (current_kboard->defining_kbd_macro)
1849 && NILP (current_kboard->Vprefix_arg))
1850 finalize_kbd_macro_chars ();
1852 #ifdef MULTI_KBOARD
1853 if (!was_locked)
1854 any_kboard_state ();
1855 #endif
1859 extern Lisp_Object Qcomposition, Qdisplay;
1861 /* Adjust point to a boundary of a region that has such a property
1862 that should be treated intangible. For the moment, we check
1863 `composition', `display' and `invisible' properties.
1864 LAST_PT is the last position of point. */
1866 extern Lisp_Object Qafter_string, Qbefore_string;
1867 extern Lisp_Object get_pos_property P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
1869 static void
1870 adjust_point_for_property (last_pt, modified)
1871 int last_pt;
1872 int modified;
1874 int beg, end;
1875 Lisp_Object val, overlay, tmp;
1876 int check_composition = 1, check_display = 1, check_invisible = 1;
1877 int orig_pt = PT;
1879 /* FIXME: cycling is probably not necessary because these properties
1880 can't be usefully combined anyway. */
1881 while (check_composition || check_display || check_invisible)
1883 if (check_composition
1884 && PT > BEGV && PT < ZV
1885 && get_property_and_range (PT, Qcomposition, &val, &beg, &end, Qnil)
1886 && COMPOSITION_VALID_P (beg, end, val)
1887 && beg < PT /* && end > PT <- It's always the case. */
1888 && (last_pt <= beg || last_pt >= end))
1890 xassert (end > PT);
1891 SET_PT (PT < last_pt ? beg : end);
1892 check_display = check_invisible = 1;
1894 check_composition = 0;
1895 if (check_display
1896 && PT > BEGV && PT < ZV
1897 && !NILP (val = get_char_property_and_overlay
1898 (make_number (PT), Qdisplay, Qnil, &overlay))
1899 && display_prop_intangible_p (val)
1900 && (!OVERLAYP (overlay)
1901 ? get_property_and_range (PT, Qdisplay, &val, &beg, &end, Qnil)
1902 : (beg = OVERLAY_POSITION (OVERLAY_START (overlay)),
1903 end = OVERLAY_POSITION (OVERLAY_END (overlay))))
1904 && beg < PT) /* && end > PT <- It's always the case. */
1906 xassert (end > PT);
1907 SET_PT (PT < last_pt ? beg : end);
1908 check_composition = check_invisible = 1;
1910 check_display = 0;
1911 if (check_invisible && PT > BEGV && PT < ZV)
1913 int inv, ellipsis = 0;
1914 beg = end = PT;
1916 /* Find boundaries `beg' and `end' of the invisible area, if any. */
1917 while (end < ZV
1918 && !NILP (val = get_char_property_and_overlay
1919 (make_number (end), Qinvisible, Qnil, &overlay))
1920 && (inv = TEXT_PROP_MEANS_INVISIBLE (val)))
1922 ellipsis = ellipsis || inv > 1
1923 || (OVERLAYP (overlay)
1924 && (!NILP (Foverlay_get (overlay, Qafter_string))
1925 || !NILP (Foverlay_get (overlay, Qbefore_string))));
1926 tmp = Fnext_single_char_property_change
1927 (make_number (end), Qinvisible, Qnil, Qnil);
1928 end = NATNUMP (tmp) ? XFASTINT (tmp) : ZV;
1930 while (beg > BEGV
1931 && !NILP (val = get_char_property_and_overlay
1932 (make_number (beg - 1), Qinvisible, Qnil, &overlay))
1933 && (inv = TEXT_PROP_MEANS_INVISIBLE (val)))
1935 ellipsis = ellipsis || inv > 1
1936 || (OVERLAYP (overlay)
1937 && (!NILP (Foverlay_get (overlay, Qafter_string))
1938 || !NILP (Foverlay_get (overlay, Qbefore_string))));
1939 tmp = Fprevious_single_char_property_change
1940 (make_number (beg), Qinvisible, Qnil, Qnil);
1941 beg = NATNUMP (tmp) ? XFASTINT (tmp) : BEGV;
1944 /* Move away from the inside area. */
1945 if (beg < PT && end > PT)
1947 SET_PT ((orig_pt == PT && (last_pt < beg || last_pt > end))
1948 /* We haven't moved yet (so we don't need to fear
1949 infinite-looping) and we were outside the range
1950 before (so either end of the range still corresponds
1951 to a move in the right direction): pretend we moved
1952 less than we actually did, so that we still have
1953 more freedom below in choosing which end of the range
1954 to go to. */
1955 ? (orig_pt = -1, PT < last_pt ? end : beg)
1956 /* We either have moved already or the last point
1957 was already in the range: we don't get to choose
1958 which end of the range we have to go to. */
1959 : (PT < last_pt ? beg : end));
1960 check_composition = check_display = 1;
1962 #if 0 /* This assertion isn't correct, because SET_PT may end up setting
1963 the point to something other than its argument, due to
1964 point-motion hooks, intangibility, etc. */
1965 xassert (PT == beg || PT == end);
1966 #endif
1968 /* Pretend the area doesn't exist if the buffer is not
1969 modified. */
1970 if (!modified && !ellipsis && beg < end)
1972 if (last_pt == beg && PT == end && end < ZV)
1973 (check_composition = check_display = 1, SET_PT (end + 1));
1974 else if (last_pt == end && PT == beg && beg > BEGV)
1975 (check_composition = check_display = 1, SET_PT (beg - 1));
1976 else if (PT == ((PT < last_pt) ? beg : end))
1977 /* We've already moved as far as we can. Trying to go
1978 to the other end would mean moving backwards and thus
1979 could lead to an infinite loop. */
1981 else if (val = get_pos_property (make_number (PT),
1982 Qinvisible, Qnil),
1983 TEXT_PROP_MEANS_INVISIBLE (val)
1984 && (val = get_pos_property
1985 (make_number (PT == beg ? end : beg),
1986 Qinvisible, Qnil),
1987 !TEXT_PROP_MEANS_INVISIBLE (val)))
1988 (check_composition = check_display = 1,
1989 SET_PT (PT == beg ? end : beg));
1992 check_invisible = 0;
1996 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1998 static Lisp_Object
1999 safe_run_hooks_1 (hook)
2000 Lisp_Object hook;
2002 return call1 (Vrun_hooks, Vinhibit_quit);
2005 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
2007 static Lisp_Object
2008 safe_run_hooks_error (data)
2009 Lisp_Object data;
2011 Lisp_Object args[3];
2012 args[0] = build_string ("Error in %s: %s");
2013 args[1] = Vinhibit_quit;
2014 args[2] = data;
2015 Fmessage (3, args);
2016 return Fset (Vinhibit_quit, Qnil);
2019 /* If we get an error while running the hook, cause the hook variable
2020 to be nil. Also inhibit quits, so that C-g won't cause the hook
2021 to mysteriously evaporate. */
2023 void
2024 safe_run_hooks (hook)
2025 Lisp_Object hook;
2027 int count = SPECPDL_INDEX ();
2028 specbind (Qinhibit_quit, hook);
2030 internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
2032 unbind_to (count, Qnil);
2036 /* Number of seconds between polling for input. This is a Lisp
2037 variable that can be bound. */
2039 EMACS_INT polling_period;
2041 /* Nonzero means polling for input is temporarily suppressed. */
2043 int poll_suppress_count;
2045 /* Asynchronous timer for polling. */
2047 struct atimer *poll_timer;
2050 #ifdef POLL_FOR_INPUT
2052 /* Poll for input, so what we catch a C-g if it comes in. This
2053 function is called from x_make_frame_visible, see comment
2054 there. */
2056 void
2057 poll_for_input_1 ()
2059 if (interrupt_input_blocked == 0
2060 && !waiting_for_input)
2061 read_avail_input (0);
2064 /* Timer callback function for poll_timer. TIMER is equal to
2065 poll_timer. */
2067 void
2068 poll_for_input (timer)
2069 struct atimer *timer;
2071 if (poll_suppress_count == 0)
2072 poll_for_input_1 ();
2075 #endif /* POLL_FOR_INPUT */
2077 /* Begin signals to poll for input, if they are appropriate.
2078 This function is called unconditionally from various places. */
2080 void
2081 start_polling ()
2083 #ifdef POLL_FOR_INPUT
2084 if (read_socket_hook && !interrupt_input)
2086 /* Turn alarm handling on unconditionally. It might have
2087 been turned off in process.c. */
2088 turn_on_atimers (1);
2090 /* If poll timer doesn't exist, are we need one with
2091 a different interval, start a new one. */
2092 if (poll_timer == NULL
2093 || EMACS_SECS (poll_timer->interval) != polling_period)
2095 EMACS_TIME interval;
2097 if (poll_timer)
2098 cancel_atimer (poll_timer);
2100 EMACS_SET_SECS_USECS (interval, polling_period, 0);
2101 poll_timer = start_atimer (ATIMER_CONTINUOUS, interval,
2102 poll_for_input, NULL);
2105 /* Let the timer's callback function poll for input
2106 if this becomes zero. */
2107 --poll_suppress_count;
2109 #endif
2112 /* Nonzero if we are using polling to handle input asynchronously. */
2115 input_polling_used ()
2117 #ifdef POLL_FOR_INPUT
2118 return read_socket_hook && !interrupt_input;
2119 #else
2120 return 0;
2121 #endif
2124 /* Turn off polling. */
2126 void
2127 stop_polling ()
2129 #ifdef POLL_FOR_INPUT
2130 if (read_socket_hook && !interrupt_input)
2131 ++poll_suppress_count;
2132 #endif
2135 /* Set the value of poll_suppress_count to COUNT
2136 and start or stop polling accordingly. */
2138 void
2139 set_poll_suppress_count (count)
2140 int count;
2142 #ifdef POLL_FOR_INPUT
2143 if (count == 0 && poll_suppress_count != 0)
2145 poll_suppress_count = 1;
2146 start_polling ();
2148 else if (count != 0 && poll_suppress_count == 0)
2150 stop_polling ();
2152 poll_suppress_count = count;
2153 #endif
2156 /* Bind polling_period to a value at least N.
2157 But don't decrease it. */
2159 void
2160 bind_polling_period (n)
2161 int n;
2163 #ifdef POLL_FOR_INPUT
2164 int new = polling_period;
2166 if (n > new)
2167 new = n;
2169 stop_other_atimers (poll_timer);
2170 stop_polling ();
2171 specbind (Qpolling_period, make_number (new));
2172 /* Start a new alarm with the new period. */
2173 start_polling ();
2174 #endif
2177 /* Apply the control modifier to CHARACTER. */
2180 make_ctrl_char (c)
2181 int c;
2183 /* Save the upper bits here. */
2184 int upper = c & ~0177;
2186 c &= 0177;
2188 /* Everything in the columns containing the upper-case letters
2189 denotes a control character. */
2190 if (c >= 0100 && c < 0140)
2192 int oc = c;
2193 c &= ~0140;
2194 /* Set the shift modifier for a control char
2195 made from a shifted letter. But only for letters! */
2196 if (oc >= 'A' && oc <= 'Z')
2197 c |= shift_modifier;
2200 /* The lower-case letters denote control characters too. */
2201 else if (c >= 'a' && c <= 'z')
2202 c &= ~0140;
2204 /* Include the bits for control and shift
2205 only if the basic ASCII code can't indicate them. */
2206 else if (c >= ' ')
2207 c |= ctrl_modifier;
2209 /* Replace the high bits. */
2210 c |= (upper & ~ctrl_modifier);
2212 return c;
2215 /* Display help echo in the echo area.
2217 HELP a string means display that string, HELP nil means clear the
2218 help echo. If HELP is a function, call it with OBJECT and POS as
2219 arguments; the function should return a help string or nil for
2220 none. For all other types of HELP evaluate it to obtain a string.
2222 WINDOW is the window in which the help was generated, if any.
2223 It is nil if not in a window.
2225 If OBJECT is a buffer, POS is the position in the buffer where the
2226 `help-echo' text property was found.
2228 If OBJECT is an overlay, that overlay has a `help-echo' property,
2229 and POS is the position in the overlay's buffer under the mouse.
2231 If OBJECT is a string (an overlay string or a string displayed with
2232 the `display' property). POS is the position in that string under
2233 the mouse.
2235 OK_TO_OVERWRITE_KEYSTROKE_ECHO non-zero means it's okay if the help
2236 echo overwrites a keystroke echo currently displayed in the echo
2237 area.
2239 Note: this function may only be called with HELP nil or a string
2240 from X code running asynchronously. */
2242 void
2243 show_help_echo (help, window, object, pos, ok_to_overwrite_keystroke_echo)
2244 Lisp_Object help, window, object, pos;
2245 int ok_to_overwrite_keystroke_echo;
2247 if (!NILP (help) && !STRINGP (help))
2249 if (FUNCTIONP (help))
2251 Lisp_Object args[4];
2252 args[0] = help;
2253 args[1] = window;
2254 args[2] = object;
2255 args[3] = pos;
2256 help = safe_call (4, args);
2258 else
2259 help = safe_eval (help);
2261 if (!STRINGP (help))
2262 return;
2265 if (STRINGP (help) || NILP (help))
2267 if (!NILP (Vshow_help_function))
2268 call1 (Vshow_help_function, help);
2269 else if (/* Don't overwrite minibuffer contents. */
2270 !MINI_WINDOW_P (XWINDOW (selected_window))
2271 /* Don't overwrite a keystroke echo. */
2272 && (NILP (echo_message_buffer)
2273 || ok_to_overwrite_keystroke_echo)
2274 /* Don't overwrite a prompt. */
2275 && !cursor_in_echo_area)
2277 if (STRINGP (help))
2279 int count = SPECPDL_INDEX ();
2281 if (!help_echo_showing_p)
2282 Vpre_help_message = current_message ();
2284 specbind (Qmessage_truncate_lines, Qt);
2285 message3_nolog (help, SBYTES (help),
2286 STRING_MULTIBYTE (help));
2287 unbind_to (count, Qnil);
2289 else if (STRINGP (Vpre_help_message))
2291 message3_nolog (Vpre_help_message,
2292 SBYTES (Vpre_help_message),
2293 STRING_MULTIBYTE (Vpre_help_message));
2294 Vpre_help_message = Qnil;
2296 else
2297 message (0);
2300 help_echo_showing_p = STRINGP (help);
2306 /* Input of single characters from keyboard */
2308 Lisp_Object print_help ();
2309 static Lisp_Object kbd_buffer_get_event ();
2310 static void record_char ();
2312 #ifdef MULTI_KBOARD
2313 static jmp_buf wrong_kboard_jmpbuf;
2314 #endif
2316 #define STOP_POLLING \
2317 do { if (! polling_stopped_here) stop_polling (); \
2318 polling_stopped_here = 1; } while (0)
2320 #define RESUME_POLLING \
2321 do { if (polling_stopped_here) start_polling (); \
2322 polling_stopped_here = 0; } while (0)
2324 /* read a character from the keyboard; call the redisplay if needed */
2325 /* commandflag 0 means do not do auto-saving, but do do redisplay.
2326 -1 means do not do redisplay, but do do autosaving.
2327 1 means do both. */
2329 /* The arguments MAPS and NMAPS are for menu prompting.
2330 MAPS is an array of keymaps; NMAPS is the length of MAPS.
2332 PREV_EVENT is the previous input event, or nil if we are reading
2333 the first event of a key sequence (or not reading a key sequence).
2334 If PREV_EVENT is t, that is a "magic" value that says
2335 not to run input methods, but in other respects to act as if
2336 not reading a key sequence.
2338 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
2339 if we used a mouse menu to read the input, or zero otherwise. If
2340 USED_MOUSE_MENU is null, we don't dereference it.
2342 Value is t if we showed a menu and the user rejected it. */
2344 Lisp_Object
2345 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
2346 int commandflag;
2347 int nmaps;
2348 Lisp_Object *maps;
2349 Lisp_Object prev_event;
2350 int *used_mouse_menu;
2352 volatile Lisp_Object c;
2353 int count;
2354 jmp_buf local_getcjmp;
2355 jmp_buf save_jump;
2356 volatile int key_already_recorded = 0;
2357 Lisp_Object tem, save;
2358 volatile Lisp_Object previous_echo_area_message;
2359 volatile Lisp_Object also_record;
2360 volatile int reread;
2361 struct gcpro gcpro1, gcpro2;
2362 EMACS_TIME last_idle_start;
2363 int polling_stopped_here = 0;
2365 also_record = Qnil;
2367 #if 0 /* This was commented out as part of fixing echo for C-u left. */
2368 before_command_key_count = this_command_key_count;
2369 before_command_echo_length = echo_length ();
2370 #endif
2371 c = Qnil;
2372 previous_echo_area_message = Qnil;
2374 GCPRO2 (c, previous_echo_area_message);
2376 retry:
2378 reread = 0;
2379 if (CONSP (Vunread_post_input_method_events))
2381 c = XCAR (Vunread_post_input_method_events);
2382 Vunread_post_input_method_events
2383 = XCDR (Vunread_post_input_method_events);
2385 /* Undo what read_char_x_menu_prompt did when it unread
2386 additional keys returned by Fx_popup_menu. */
2387 if (CONSP (c)
2388 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2389 && NILP (XCDR (c)))
2390 c = XCAR (c);
2392 reread = 1;
2393 goto reread_first;
2396 if (unread_command_char != -1)
2398 XSETINT (c, unread_command_char);
2399 unread_command_char = -1;
2401 reread = 1;
2402 goto reread_first;
2405 if (CONSP (Vunread_command_events))
2407 c = XCAR (Vunread_command_events);
2408 Vunread_command_events = XCDR (Vunread_command_events);
2410 /* Undo what read_char_x_menu_prompt did when it unread
2411 additional keys returned by Fx_popup_menu. */
2412 if (CONSP (c)
2413 && EQ (XCDR (c), Qdisabled)
2414 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c))))
2415 c = XCAR (c);
2417 /* If the queued event is something that used the mouse,
2418 set used_mouse_menu accordingly. */
2419 if (used_mouse_menu
2420 && (EQ (c, Qtool_bar) || EQ (c, Qmenu_bar)))
2421 *used_mouse_menu = 1;
2423 reread = 1;
2424 goto reread_for_input_method;
2427 if (CONSP (Vunread_input_method_events))
2429 c = XCAR (Vunread_input_method_events);
2430 Vunread_input_method_events = XCDR (Vunread_input_method_events);
2432 /* Undo what read_char_x_menu_prompt did when it unread
2433 additional keys returned by Fx_popup_menu. */
2434 if (CONSP (c)
2435 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2436 && NILP (XCDR (c)))
2437 c = XCAR (c);
2438 reread = 1;
2439 goto reread_for_input_method;
2442 this_command_key_count_reset = 0;
2444 if (!NILP (Vexecuting_macro))
2446 /* We set this to Qmacro; since that's not a frame, nobody will
2447 try to switch frames on us, and the selected window will
2448 remain unchanged.
2450 Since this event came from a macro, it would be misleading to
2451 leave internal_last_event_frame set to wherever the last
2452 real event came from. Normally, a switch-frame event selects
2453 internal_last_event_frame after each command is read, but
2454 events read from a macro should never cause a new frame to be
2455 selected. */
2456 Vlast_event_frame = internal_last_event_frame = Qmacro;
2458 /* Exit the macro if we are at the end.
2459 Also, some things replace the macro with t
2460 to force an early exit. */
2461 if (EQ (Vexecuting_macro, Qt)
2462 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
2464 XSETINT (c, -1);
2465 goto exit;
2468 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
2469 if (STRINGP (Vexecuting_macro)
2470 && (XINT (c) & 0x80))
2471 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
2473 executing_macro_index++;
2475 goto from_macro;
2478 if (!NILP (unread_switch_frame))
2480 c = unread_switch_frame;
2481 unread_switch_frame = Qnil;
2483 /* This event should make it into this_command_keys, and get echoed
2484 again, so we do not set `reread'. */
2485 goto reread_first;
2488 /* if redisplay was requested */
2489 if (commandflag >= 0)
2491 /* If there is pending input, process any events which are not
2492 user-visible, such as X selection_request events. */
2493 if (input_pending
2494 || detect_input_pending_run_timers (0))
2495 swallow_events (0); /* may clear input_pending */
2497 /* Redisplay if no pending input. */
2498 while (!input_pending)
2500 if (help_echo_showing_p && !EQ (selected_window, minibuf_window))
2501 redisplay_preserve_echo_area (5);
2502 else
2503 redisplay ();
2505 if (!input_pending)
2506 /* Normal case: no input arrived during redisplay. */
2507 break;
2509 /* Input arrived and pre-empted redisplay.
2510 Process any events which are not user-visible. */
2511 swallow_events (0);
2512 /* If that cleared input_pending, try again to redisplay. */
2516 /* Message turns off echoing unless more keystrokes turn it on again.
2518 The code in 20.x for the condition was
2520 1. echo_area_glyphs && *echo_area_glyphs
2521 2. && echo_area_glyphs != current_kboard->echobuf
2522 3. && ok_to_echo_at_next_pause != echo_area_glyphs
2524 (1) means there's a current message displayed
2526 (2) means it's not the message from echoing from the current
2527 kboard.
2529 (3) There's only one place in 20.x where ok_to_echo_at_next_pause
2530 is set to a non-null value. This is done in read_char and it is
2531 set to echo_area_glyphs after a call to echo_char. That means
2532 ok_to_echo_at_next_pause is either null or
2533 current_kboard->echobuf with the appropriate current_kboard at
2534 that time.
2536 So, condition (3) means in clear text ok_to_echo_at_next_pause
2537 must be either null, or the current message isn't from echoing at
2538 all, or it's from echoing from a different kboard than the
2539 current one. */
2541 if (/* There currently is something in the echo area. */
2542 !NILP (echo_area_buffer[0])
2543 && (/* And it's either not from echoing. */
2544 !EQ (echo_area_buffer[0], echo_message_buffer)
2545 /* Or it's an echo from a different kboard. */
2546 || echo_kboard != current_kboard
2547 /* Or we explicitly allow overwriting whatever there is. */
2548 || ok_to_echo_at_next_pause == NULL))
2549 cancel_echoing ();
2550 else
2551 echo_dash ();
2553 /* Try reading a character via menu prompting in the minibuf.
2554 Try this before the sit-for, because the sit-for
2555 would do the wrong thing if we are supposed to do
2556 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
2557 after a mouse event so don't try a minibuf menu. */
2558 c = Qnil;
2559 if (nmaps > 0 && INTERACTIVE
2560 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
2561 /* Don't bring up a menu if we already have another event. */
2562 && NILP (Vunread_command_events)
2563 && unread_command_char < 0
2564 && !detect_input_pending_run_timers (0))
2566 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
2567 if (! NILP (c))
2569 key_already_recorded = 1;
2570 goto non_reread_1;
2574 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
2575 We will do that below, temporarily for short sections of code,
2576 when appropriate. local_getcjmp must be in effect
2577 around any call to sit_for or kbd_buffer_get_event;
2578 it *must not* be in effect when we call redisplay. */
2580 if (_setjmp (local_getcjmp))
2582 XSETINT (c, quit_char);
2583 internal_last_event_frame = selected_frame;
2584 Vlast_event_frame = internal_last_event_frame;
2585 /* If we report the quit char as an event,
2586 don't do so more than once. */
2587 if (!NILP (Vinhibit_quit))
2588 Vquit_flag = Qnil;
2590 #ifdef MULTI_KBOARD
2592 KBOARD *kb = FRAME_KBOARD (XFRAME (selected_frame));
2593 if (kb != current_kboard)
2595 Lisp_Object link = kb->kbd_queue;
2596 /* We shouldn't get here if we were in single-kboard mode! */
2597 if (single_kboard)
2598 abort ();
2599 if (CONSP (link))
2601 while (CONSP (XCDR (link)))
2602 link = XCDR (link);
2603 if (!NILP (XCDR (link)))
2604 abort ();
2606 if (!CONSP (link))
2607 kb->kbd_queue = Fcons (c, Qnil);
2608 else
2609 XSETCDR (link, Fcons (c, Qnil));
2610 kb->kbd_queue_has_data = 1;
2611 current_kboard = kb;
2612 /* This is going to exit from read_char
2613 so we had better get rid of this frame's stuff. */
2614 UNGCPRO;
2615 longjmp (wrong_kboard_jmpbuf, 1);
2618 #endif
2619 goto non_reread;
2622 timer_start_idle ();
2624 /* If in middle of key sequence and minibuffer not active,
2625 start echoing if enough time elapses. */
2627 if (minibuf_level == 0
2628 && !current_kboard->immediate_echo
2629 && this_command_key_count > 0
2630 && ! noninteractive
2631 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2632 && NILP (Fzerop (Vecho_keystrokes))
2633 && (/* No message. */
2634 NILP (echo_area_buffer[0])
2635 /* Or empty message. */
2636 || (BUF_BEG (XBUFFER (echo_area_buffer[0]))
2637 == BUF_Z (XBUFFER (echo_area_buffer[0])))
2638 /* Or already echoing from same kboard. */
2639 || (echo_kboard && ok_to_echo_at_next_pause == echo_kboard)
2640 /* Or not echoing before and echoing allowed. */
2641 || (!echo_kboard && ok_to_echo_at_next_pause)))
2643 Lisp_Object tem0;
2645 /* After a mouse event, start echoing right away.
2646 This is because we are probably about to display a menu,
2647 and we don't want to delay before doing so. */
2648 if (EVENT_HAS_PARAMETERS (prev_event))
2649 echo_now ();
2650 else
2652 int sec, usec;
2653 double duration = extract_float (Vecho_keystrokes);
2654 sec = (int) duration;
2655 usec = (duration - sec) * 1000000;
2656 save_getcjmp (save_jump);
2657 restore_getcjmp (local_getcjmp);
2658 tem0 = sit_for (sec, usec, 1, 1, 0);
2659 restore_getcjmp (save_jump);
2660 if (EQ (tem0, Qt)
2661 && ! CONSP (Vunread_command_events))
2662 echo_now ();
2666 /* Maybe auto save due to number of keystrokes. */
2668 if (commandflag != 0
2669 && auto_save_interval > 0
2670 && num_nonmacro_input_events - last_auto_save > max (auto_save_interval, 20)
2671 && !detect_input_pending_run_timers (0))
2673 Fdo_auto_save (Qnil, Qnil);
2674 /* Hooks can actually change some buffers in auto save. */
2675 redisplay ();
2678 /* Try reading using an X menu.
2679 This is never confused with reading using the minibuf
2680 because the recursive call of read_char in read_char_minibuf_menu_prompt
2681 does not pass on any keymaps. */
2683 if (nmaps > 0 && INTERACTIVE
2684 && !NILP (prev_event)
2685 && EVENT_HAS_PARAMETERS (prev_event)
2686 && !EQ (XCAR (prev_event), Qmenu_bar)
2687 && !EQ (XCAR (prev_event), Qtool_bar)
2688 /* Don't bring up a menu if we already have another event. */
2689 && NILP (Vunread_command_events)
2690 && unread_command_char < 0)
2692 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
2694 /* Now that we have read an event, Emacs is not idle. */
2695 timer_stop_idle ();
2697 goto exit;
2700 /* Maybe autosave and/or garbage collect due to idleness. */
2702 if (INTERACTIVE && NILP (c))
2704 int delay_level, buffer_size;
2706 /* Slow down auto saves logarithmically in size of current buffer,
2707 and garbage collect while we're at it. */
2708 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
2709 last_non_minibuf_size = Z - BEG;
2710 buffer_size = (last_non_minibuf_size >> 8) + 1;
2711 delay_level = 0;
2712 while (buffer_size > 64)
2713 delay_level++, buffer_size -= buffer_size >> 2;
2714 if (delay_level < 4) delay_level = 4;
2715 /* delay_level is 4 for files under around 50k, 7 at 100k,
2716 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
2718 /* Auto save if enough time goes by without input. */
2719 if (commandflag != 0
2720 && num_nonmacro_input_events > last_auto_save
2721 && INTEGERP (Vauto_save_timeout)
2722 && XINT (Vauto_save_timeout) > 0)
2724 Lisp_Object tem0;
2726 save_getcjmp (save_jump);
2727 restore_getcjmp (local_getcjmp);
2728 tem0 = sit_for (delay_level * XFASTINT (Vauto_save_timeout) / 4,
2729 0, 1, 1, 0);
2730 restore_getcjmp (save_jump);
2732 if (EQ (tem0, Qt)
2733 && ! CONSP (Vunread_command_events))
2735 Fdo_auto_save (Qnil, Qnil);
2737 /* If we have auto-saved and there is still no input
2738 available, garbage collect if there has been enough
2739 consing going on to make it worthwhile. */
2740 if (!detect_input_pending_run_timers (0)
2741 && consing_since_gc > gc_cons_threshold / 2)
2742 Fgarbage_collect ();
2744 redisplay ();
2749 /* If this has become non-nil here, it has been set by a timer
2750 or sentinel or filter. */
2751 if (CONSP (Vunread_command_events))
2753 c = XCAR (Vunread_command_events);
2754 Vunread_command_events = XCDR (Vunread_command_events);
2757 /* Read something from current KBOARD's side queue, if possible. */
2759 if (NILP (c))
2761 if (current_kboard->kbd_queue_has_data)
2763 if (!CONSP (current_kboard->kbd_queue))
2764 abort ();
2765 c = XCAR (current_kboard->kbd_queue);
2766 current_kboard->kbd_queue
2767 = XCDR (current_kboard->kbd_queue);
2768 if (NILP (current_kboard->kbd_queue))
2769 current_kboard->kbd_queue_has_data = 0;
2770 input_pending = readable_events (0);
2771 if (EVENT_HAS_PARAMETERS (c)
2772 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
2773 internal_last_event_frame = XCAR (XCDR (c));
2774 Vlast_event_frame = internal_last_event_frame;
2778 #ifdef MULTI_KBOARD
2779 /* If current_kboard's side queue is empty check the other kboards.
2780 If one of them has data that we have not yet seen here,
2781 switch to it and process the data waiting for it.
2783 Note: if the events queued up for another kboard
2784 have already been seen here, and therefore are not a complete command,
2785 the kbd_queue_has_data field is 0, so we skip that kboard here.
2786 That's to avoid an infinite loop switching between kboards here. */
2787 if (NILP (c) && !single_kboard)
2789 KBOARD *kb;
2790 for (kb = all_kboards; kb; kb = kb->next_kboard)
2791 if (kb->kbd_queue_has_data)
2793 current_kboard = kb;
2794 /* This is going to exit from read_char
2795 so we had better get rid of this frame's stuff. */
2796 UNGCPRO;
2797 longjmp (wrong_kboard_jmpbuf, 1);
2800 #endif
2802 wrong_kboard:
2804 STOP_POLLING;
2806 /* Finally, we read from the main queue,
2807 and if that gives us something we can't use yet, we put it on the
2808 appropriate side queue and try again. */
2810 if (NILP (c))
2812 KBOARD *kb;
2814 /* Actually read a character, waiting if necessary. */
2815 save_getcjmp (save_jump);
2816 restore_getcjmp (local_getcjmp);
2817 timer_start_idle ();
2818 c = kbd_buffer_get_event (&kb, used_mouse_menu);
2819 restore_getcjmp (save_jump);
2821 #ifdef MULTI_KBOARD
2822 if (! NILP (c) && (kb != current_kboard))
2824 Lisp_Object link = kb->kbd_queue;
2825 if (CONSP (link))
2827 while (CONSP (XCDR (link)))
2828 link = XCDR (link);
2829 if (!NILP (XCDR (link)))
2830 abort ();
2832 if (!CONSP (link))
2833 kb->kbd_queue = Fcons (c, Qnil);
2834 else
2835 XSETCDR (link, Fcons (c, Qnil));
2836 kb->kbd_queue_has_data = 1;
2837 c = Qnil;
2838 if (single_kboard)
2839 goto wrong_kboard;
2840 current_kboard = kb;
2841 /* This is going to exit from read_char
2842 so we had better get rid of this frame's stuff. */
2843 UNGCPRO;
2844 longjmp (wrong_kboard_jmpbuf, 1);
2846 #endif
2849 /* Terminate Emacs in batch mode if at eof. */
2850 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
2851 Fkill_emacs (make_number (1));
2853 if (INTEGERP (c))
2855 /* Add in any extra modifiers, where appropriate. */
2856 if ((extra_keyboard_modifiers & CHAR_CTL)
2857 || ((extra_keyboard_modifiers & 0177) < ' '
2858 && (extra_keyboard_modifiers & 0177) != 0))
2859 XSETINT (c, make_ctrl_char (XINT (c)));
2861 /* Transfer any other modifier bits directly from
2862 extra_keyboard_modifiers to c. Ignore the actual character code
2863 in the low 16 bits of extra_keyboard_modifiers. */
2864 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
2867 non_reread:
2869 /* Record the last idle start time so that we can reset it
2870 should the next event read be a help-echo. */
2871 last_idle_start = timer_idleness_start_time;
2872 timer_stop_idle ();
2873 RESUME_POLLING;
2875 if (NILP (c))
2877 if (commandflag >= 0
2878 && !input_pending && !detect_input_pending_run_timers (0))
2879 redisplay ();
2881 goto wrong_kboard;
2884 non_reread_1:
2886 /* Buffer switch events are only for internal wakeups
2887 so don't show them to the user.
2888 Also, don't record a key if we already did. */
2889 if (BUFFERP (c) || key_already_recorded)
2890 goto exit;
2892 /* Process special events within read_char
2893 and loop around to read another event. */
2894 save = Vquit_flag;
2895 Vquit_flag = Qnil;
2896 tem = access_keymap (get_keymap (Vspecial_event_map, 0, 1), c, 0, 0, 1);
2897 Vquit_flag = save;
2899 if (!NILP (tem))
2901 int was_locked = single_kboard;
2903 last_input_char = c;
2904 Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt);
2906 if (CONSP (c) && EQ (XCAR (c), Qselect_window))
2907 /* We stopped being idle for this event; undo that. This
2908 prevents automatic window selection (under
2909 mouse_autoselect_window from acting as a real input event, for
2910 example banishing the mouse under mouse-avoidance-mode. */
2911 timer_idleness_start_time = last_idle_start;
2913 /* Resume allowing input from any kboard, if that was true before. */
2914 if (!was_locked)
2915 any_kboard_state ();
2917 goto retry;
2920 /* Handle things that only apply to characters. */
2921 if (INTEGERP (c))
2923 /* If kbd_buffer_get_event gave us an EOF, return that. */
2924 if (XINT (c) == -1)
2925 goto exit;
2927 if ((STRINGP (Vkeyboard_translate_table)
2928 && SCHARS (Vkeyboard_translate_table) > (unsigned) XFASTINT (c))
2929 || (VECTORP (Vkeyboard_translate_table)
2930 && XVECTOR (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2931 || (CHAR_TABLE_P (Vkeyboard_translate_table)
2932 && CHAR_VALID_P (XINT (c), 0)))
2934 Lisp_Object d;
2935 d = Faref (Vkeyboard_translate_table, c);
2936 /* nil in keyboard-translate-table means no translation. */
2937 if (!NILP (d))
2938 c = d;
2942 /* If this event is a mouse click in the menu bar,
2943 return just menu-bar for now. Modify the mouse click event
2944 so we won't do this twice, then queue it up. */
2945 if (EVENT_HAS_PARAMETERS (c)
2946 && CONSP (XCDR (c))
2947 && CONSP (EVENT_START (c))
2948 && CONSP (XCDR (EVENT_START (c))))
2950 Lisp_Object posn;
2952 posn = POSN_POSN (EVENT_START (c));
2953 /* Handle menu-bar events:
2954 insert the dummy prefix event `menu-bar'. */
2955 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
2957 /* Change menu-bar to (menu-bar) as the event "position". */
2958 POSN_SET_POSN (EVENT_START (c), Fcons (posn, Qnil));
2960 also_record = c;
2961 Vunread_command_events = Fcons (c, Vunread_command_events);
2962 c = posn;
2966 /* Store these characters into recent_keys, the dribble file if any,
2967 and the keyboard macro being defined, if any. */
2968 record_char (c);
2969 if (! NILP (also_record))
2970 record_char (also_record);
2972 /* Wipe the echo area.
2973 But first, if we are about to use an input method,
2974 save the echo area contents for it to refer to. */
2975 if (INTEGERP (c)
2976 && ! NILP (Vinput_method_function)
2977 && (unsigned) XINT (c) >= ' '
2978 && (unsigned) XINT (c) != 127
2979 && (unsigned) XINT (c) < 256)
2981 previous_echo_area_message = Fcurrent_message ();
2982 Vinput_method_previous_message = previous_echo_area_message;
2985 /* Now wipe the echo area, except for help events which do their
2986 own stuff with the echo area. */
2987 if (!CONSP (c)
2988 || (!(EQ (Qhelp_echo, XCAR (c)))
2989 && !(EQ (Qswitch_frame, XCAR (c)))))
2991 if (!NILP (echo_area_buffer[0]))
2992 safe_run_hooks (Qecho_area_clear_hook);
2993 clear_message (1, 0);
2996 reread_for_input_method:
2997 from_macro:
2998 /* Pass this to the input method, if appropriate. */
2999 if (INTEGERP (c)
3000 && ! NILP (Vinput_method_function)
3001 /* Don't run the input method within a key sequence,
3002 after the first event of the key sequence. */
3003 && NILP (prev_event)
3004 && (unsigned) XINT (c) >= ' '
3005 && (unsigned) XINT (c) != 127
3006 && (unsigned) XINT (c) < 256)
3008 Lisp_Object keys;
3009 int key_count, key_count_reset;
3010 struct gcpro gcpro1;
3011 int count = SPECPDL_INDEX ();
3013 /* Save the echo status. */
3014 int saved_immediate_echo = current_kboard->immediate_echo;
3015 struct kboard *saved_ok_to_echo = ok_to_echo_at_next_pause;
3016 int saved_echo_after_prompt = current_kboard->echo_after_prompt;
3018 #if 0
3019 if (before_command_restore_flag)
3021 this_command_key_count = before_command_key_count_1;
3022 if (this_command_key_count < this_single_command_key_start)
3023 this_single_command_key_start = this_command_key_count;
3024 echo_truncate (before_command_echo_length_1);
3025 before_command_restore_flag = 0;
3027 #endif
3029 /* Save the this_command_keys status. */
3030 key_count = this_command_key_count;
3031 key_count_reset = this_command_key_count_reset;
3033 if (key_count > 0)
3034 keys = Fcopy_sequence (this_command_keys);
3035 else
3036 keys = Qnil;
3037 GCPRO1 (keys);
3039 /* Clear out this_command_keys. */
3040 this_command_key_count = 0;
3041 this_command_key_count_reset = 0;
3043 /* Now wipe the echo area. */
3044 if (!NILP (echo_area_buffer[0]))
3045 safe_run_hooks (Qecho_area_clear_hook);
3046 clear_message (1, 0);
3047 echo_truncate (0);
3049 /* If we are not reading a key sequence,
3050 never use the echo area. */
3051 if (maps == 0)
3053 specbind (Qinput_method_use_echo_area, Qt);
3056 /* Call the input method. */
3057 tem = call1 (Vinput_method_function, c);
3059 tem = unbind_to (count, tem);
3061 /* Restore the saved echoing state
3062 and this_command_keys state. */
3063 this_command_key_count = key_count;
3064 this_command_key_count_reset = key_count_reset;
3065 if (key_count > 0)
3066 this_command_keys = keys;
3068 cancel_echoing ();
3069 ok_to_echo_at_next_pause = saved_ok_to_echo;
3070 current_kboard->echo_after_prompt = saved_echo_after_prompt;
3071 if (saved_immediate_echo)
3072 echo_now ();
3074 UNGCPRO;
3076 /* The input method can return no events. */
3077 if (! CONSP (tem))
3079 /* Bring back the previous message, if any. */
3080 if (! NILP (previous_echo_area_message))
3081 message_with_string ("%s", previous_echo_area_message, 0);
3082 goto retry;
3084 /* It returned one event or more. */
3085 c = XCAR (tem);
3086 Vunread_post_input_method_events
3087 = nconc2 (XCDR (tem), Vunread_post_input_method_events);
3090 reread_first:
3092 /* Display help if not echoing. */
3093 if (CONSP (c) && EQ (XCAR (c), Qhelp_echo))
3095 /* (help-echo FRAME HELP WINDOW OBJECT POS). */
3096 Lisp_Object help, object, position, window, tem;
3098 tem = Fcdr (XCDR (c));
3099 help = Fcar (tem);
3100 tem = Fcdr (tem);
3101 window = Fcar (tem);
3102 tem = Fcdr (tem);
3103 object = Fcar (tem);
3104 tem = Fcdr (tem);
3105 position = Fcar (tem);
3107 show_help_echo (help, window, object, position, 0);
3109 /* We stopped being idle for this event; undo that. */
3110 timer_idleness_start_time = last_idle_start;
3111 goto retry;
3114 if (! reread || this_command_key_count == 0
3115 || this_command_key_count_reset)
3118 /* Don't echo mouse motion events. */
3119 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
3120 && NILP (Fzerop (Vecho_keystrokes))
3121 && ! (EVENT_HAS_PARAMETERS (c)
3122 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
3124 echo_char (c);
3125 if (! NILP (also_record))
3126 echo_char (also_record);
3127 /* Once we reread a character, echoing can happen
3128 the next time we pause to read a new one. */
3129 ok_to_echo_at_next_pause = current_kboard;
3132 /* Record this character as part of the current key. */
3133 add_command_key (c);
3134 if (! NILP (also_record))
3135 add_command_key (also_record);
3138 last_input_char = c;
3139 num_input_events++;
3141 /* Process the help character specially if enabled */
3142 if (!NILP (Vhelp_form) && help_char_p (c))
3144 Lisp_Object tem0;
3145 count = SPECPDL_INDEX ();
3147 record_unwind_protect (Fset_window_configuration,
3148 Fcurrent_window_configuration (Qnil));
3150 tem0 = Feval (Vhelp_form);
3151 if (STRINGP (tem0))
3152 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
3154 cancel_echoing ();
3156 c = read_char (0, 0, 0, Qnil, 0);
3157 while (BUFFERP (c));
3158 /* Remove the help from the frame */
3159 unbind_to (count, Qnil);
3161 redisplay ();
3162 if (EQ (c, make_number (040)))
3164 cancel_echoing ();
3166 c = read_char (0, 0, 0, Qnil, 0);
3167 while (BUFFERP (c));
3171 exit:
3172 RESUME_POLLING;
3173 RETURN_UNGCPRO (c);
3176 /* Record a key that came from a mouse menu.
3177 Record it for echoing, for this-command-keys, and so on. */
3179 static void
3180 record_menu_key (c)
3181 Lisp_Object c;
3183 /* Wipe the echo area. */
3184 clear_message (1, 0);
3186 record_char (c);
3188 #if 0
3189 before_command_key_count = this_command_key_count;
3190 before_command_echo_length = echo_length ();
3191 #endif
3193 /* Don't echo mouse motion events. */
3194 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
3195 && NILP (Fzerop (Vecho_keystrokes)))
3197 echo_char (c);
3199 /* Once we reread a character, echoing can happen
3200 the next time we pause to read a new one. */
3201 ok_to_echo_at_next_pause = 0;
3204 /* Record this character as part of the current key. */
3205 add_command_key (c);
3207 /* Re-reading in the middle of a command */
3208 last_input_char = c;
3209 num_input_events++;
3212 /* Return 1 if should recognize C as "the help character". */
3215 help_char_p (c)
3216 Lisp_Object c;
3218 Lisp_Object tail;
3220 if (EQ (c, Vhelp_char))
3221 return 1;
3222 for (tail = Vhelp_event_list; CONSP (tail); tail = XCDR (tail))
3223 if (EQ (c, XCAR (tail)))
3224 return 1;
3225 return 0;
3228 /* Record the input event C in various ways. */
3230 static void
3231 record_char (c)
3232 Lisp_Object c;
3234 int recorded = 0;
3236 if (CONSP (c) && (EQ (XCAR (c), Qhelp_echo) || EQ (XCAR (c), Qmouse_movement)))
3238 /* To avoid filling recent_keys with help-echo and mouse-movement
3239 events, we filter out repeated help-echo events, only store the
3240 first and last in a series of mouse-movement events, and don't
3241 store repeated help-echo events which are only separated by
3242 mouse-movement events. */
3244 Lisp_Object ev1, ev2, ev3;
3245 int ix1, ix2, ix3;
3247 if ((ix1 = recent_keys_index - 1) < 0)
3248 ix1 = NUM_RECENT_KEYS - 1;
3249 ev1 = AREF (recent_keys, ix1);
3251 if ((ix2 = ix1 - 1) < 0)
3252 ix2 = NUM_RECENT_KEYS - 1;
3253 ev2 = AREF (recent_keys, ix2);
3255 if ((ix3 = ix2 - 1) < 0)
3256 ix3 = NUM_RECENT_KEYS - 1;
3257 ev3 = AREF (recent_keys, ix3);
3259 if (EQ (XCAR (c), Qhelp_echo))
3261 /* Don't record `help-echo' in recent_keys unless it shows some help
3262 message, and a different help than the previously recorded
3263 event. */
3264 Lisp_Object help, last_help;
3266 help = Fcar_safe (Fcdr_safe (XCDR (c)));
3267 if (!STRINGP (help))
3268 recorded = 1;
3269 else if (CONSP (ev1) && EQ (XCAR (ev1), Qhelp_echo)
3270 && (last_help = Fcar_safe (Fcdr_safe (XCDR (ev1))), EQ (last_help, help)))
3271 recorded = 1;
3272 else if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
3273 && CONSP (ev2) && EQ (XCAR (ev2), Qhelp_echo)
3274 && (last_help = Fcar_safe (Fcdr_safe (XCDR (ev2))), EQ (last_help, help)))
3275 recorded = -1;
3276 else if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
3277 && CONSP (ev2) && EQ (XCAR (ev2), Qmouse_movement)
3278 && CONSP (ev3) && EQ (XCAR (ev3), Qhelp_echo)
3279 && (last_help = Fcar_safe (Fcdr_safe (XCDR (ev3))), EQ (last_help, help)))
3280 recorded = -2;
3282 else if (EQ (XCAR (c), Qmouse_movement))
3284 /* Only record one pair of `mouse-movement' on a window in recent_keys.
3285 So additional mouse movement events replace the last element. */
3286 Lisp_Object last_window, window;
3288 window = Fcar_safe (Fcar_safe (XCDR (c)));
3289 if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
3290 && (last_window = Fcar_safe (Fcar_safe (XCDR (ev1))), EQ (last_window, window))
3291 && CONSP (ev2) && EQ (XCAR (ev2), Qmouse_movement)
3292 && (last_window = Fcar_safe (Fcar_safe (XCDR (ev2))), EQ (last_window, window)))
3294 ASET (recent_keys, ix1, c);
3295 recorded = 1;
3299 else
3300 store_kbd_macro_char (c);
3302 if (!recorded)
3304 total_keys++;
3305 ASET (recent_keys, recent_keys_index, c);
3306 if (++recent_keys_index >= NUM_RECENT_KEYS)
3307 recent_keys_index = 0;
3309 else if (recorded < 0)
3311 /* We need to remove one or two events from recent_keys.
3312 To do this, we simply put nil at those events and move the
3313 recent_keys_index backwards over those events. Usually,
3314 users will never see those nil events, as they will be
3315 overwritten by the command keys entered to see recent_keys
3316 (e.g. C-h l). */
3318 while (recorded++ < 0 && total_keys > 0)
3320 if (total_keys < NUM_RECENT_KEYS)
3321 total_keys--;
3322 if (--recent_keys_index < 0)
3323 recent_keys_index = NUM_RECENT_KEYS - 1;
3324 ASET (recent_keys, recent_keys_index, Qnil);
3328 num_nonmacro_input_events++;
3330 /* Write c to the dribble file. If c is a lispy event, write
3331 the event's symbol to the dribble file, in <brackets>. Bleaugh.
3332 If you, dear reader, have a better idea, you've got the source. :-) */
3333 if (dribble)
3335 if (INTEGERP (c))
3337 if (XUINT (c) < 0x100)
3338 putc (XINT (c), dribble);
3339 else
3340 fprintf (dribble, " 0x%x", (int) XUINT (c));
3342 else
3344 Lisp_Object dribblee;
3346 /* If it's a structured event, take the event header. */
3347 dribblee = EVENT_HEAD (c);
3349 if (SYMBOLP (dribblee))
3351 putc ('<', dribble);
3352 fwrite (SDATA (SYMBOL_NAME (dribblee)), sizeof (char),
3353 SBYTES (SYMBOL_NAME (dribblee)),
3354 dribble);
3355 putc ('>', dribble);
3359 fflush (dribble);
3363 Lisp_Object
3364 print_help (object)
3365 Lisp_Object object;
3367 struct buffer *old = current_buffer;
3368 Fprinc (object, Qnil);
3369 set_buffer_internal (XBUFFER (Vstandard_output));
3370 call0 (intern ("help-mode"));
3371 set_buffer_internal (old);
3372 return Qnil;
3375 /* Copy out or in the info on where C-g should throw to.
3376 This is used when running Lisp code from within get_char,
3377 in case get_char is called recursively.
3378 See read_process_output. */
3380 static void
3381 save_getcjmp (temp)
3382 jmp_buf temp;
3384 bcopy (getcjmp, temp, sizeof getcjmp);
3387 static void
3388 restore_getcjmp (temp)
3389 jmp_buf temp;
3391 bcopy (temp, getcjmp, sizeof getcjmp);
3394 #ifdef HAVE_MOUSE
3396 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
3397 of this function. */
3399 static Lisp_Object
3400 tracking_off (old_value)
3401 Lisp_Object old_value;
3403 do_mouse_tracking = old_value;
3404 if (NILP (old_value))
3406 /* Redisplay may have been preempted because there was input
3407 available, and it assumes it will be called again after the
3408 input has been processed. If the only input available was
3409 the sort that we have just disabled, then we need to call
3410 redisplay. */
3411 if (!readable_events (1))
3413 redisplay_preserve_echo_area (6);
3414 get_input_pending (&input_pending, 1);
3417 return Qnil;
3420 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
3421 doc: /* Evaluate BODY with mouse movement events enabled.
3422 Within a `track-mouse' form, mouse motion generates input events that
3423 you can read with `read-event'.
3424 Normally, mouse motion is ignored.
3425 usage: (track-mouse BODY ...) */)
3426 (args)
3427 Lisp_Object args;
3429 int count = SPECPDL_INDEX ();
3430 Lisp_Object val;
3432 record_unwind_protect (tracking_off, do_mouse_tracking);
3434 do_mouse_tracking = Qt;
3436 val = Fprogn (args);
3437 return unbind_to (count, val);
3440 /* If mouse has moved on some frame, return one of those frames.
3441 Return 0 otherwise. */
3443 static FRAME_PTR
3444 some_mouse_moved ()
3446 Lisp_Object tail, frame;
3448 FOR_EACH_FRAME (tail, frame)
3450 if (XFRAME (frame)->mouse_moved)
3451 return XFRAME (frame);
3454 return 0;
3457 #endif /* HAVE_MOUSE */
3459 /* Low level keyboard/mouse input.
3460 kbd_buffer_store_event places events in kbd_buffer, and
3461 kbd_buffer_get_event retrieves them. */
3463 /* Return true iff there are any events in the queue that read-char
3464 would return. If this returns false, a read-char would block. */
3465 static int
3466 readable_filtered_events (do_timers_now, filter_events)
3467 int do_timers_now;
3468 int filter_events;
3470 if (do_timers_now)
3471 timer_check (do_timers_now);
3473 /* If the buffer contains only FOCUS_IN_EVENT events,
3474 and FILTER_EVENTS is nonzero, report it as empty. */
3475 if (kbd_fetch_ptr != kbd_store_ptr)
3477 int have_live_event = 1;
3479 if (filter_events)
3481 struct input_event *event;
3483 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3484 ? kbd_fetch_ptr
3485 : kbd_buffer);
3487 while (have_live_event && event->kind == FOCUS_IN_EVENT)
3489 event++;
3490 if (event == kbd_buffer + KBD_BUFFER_SIZE)
3491 event = kbd_buffer;
3492 if (event == kbd_store_ptr)
3493 have_live_event = 0;
3496 if (have_live_event) return 1;
3499 #ifdef HAVE_MOUSE
3500 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3501 return 1;
3502 #endif
3503 if (single_kboard)
3505 if (current_kboard->kbd_queue_has_data)
3506 return 1;
3508 else
3510 KBOARD *kb;
3511 for (kb = all_kboards; kb; kb = kb->next_kboard)
3512 if (kb->kbd_queue_has_data)
3513 return 1;
3515 return 0;
3518 /* Return true iff there are any events in the queue that read-char
3519 would return. If this returns false, a read-char would block. */
3520 static int
3521 readable_events (do_timers_now)
3522 int do_timers_now;
3524 return readable_filtered_events (do_timers_now, 0);
3527 /* Set this for debugging, to have a way to get out */
3528 int stop_character;
3530 #ifdef MULTI_KBOARD
3531 static KBOARD *
3532 event_to_kboard (event)
3533 struct input_event *event;
3535 Lisp_Object frame;
3536 frame = event->frame_or_window;
3537 if (CONSP (frame))
3538 frame = XCAR (frame);
3539 else if (WINDOWP (frame))
3540 frame = WINDOW_FRAME (XWINDOW (frame));
3542 /* There are still some events that don't set this field.
3543 For now, just ignore the problem.
3544 Also ignore dead frames here. */
3545 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
3546 return 0;
3547 else
3548 return FRAME_KBOARD (XFRAME (frame));
3550 #endif
3552 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
3554 void
3555 kbd_buffer_store_event (event)
3556 register struct input_event *event;
3558 kbd_buffer_store_event_hold (event, 0);
3561 /* Store EVENT obtained at interrupt level into kbd_buffer, fifo.
3563 If HOLD_QUIT is 0, just stuff EVENT into the fifo.
3564 Else, if HOLD_QUIT.kind != NO_EVENT, discard EVENT.
3565 Else, if EVENT is a quit event, store the quit event
3566 in HOLD_QUIT, and return (thus ignoring further events).
3568 This is used in read_avail_input to postpone the processing
3569 of the quit event until all subsequent input events have been
3570 parsed (and discarded).
3573 void
3574 kbd_buffer_store_event_hold (event, hold_quit)
3575 register struct input_event *event;
3576 struct input_event *hold_quit;
3578 if (event->kind == NO_EVENT)
3579 abort ();
3581 if (hold_quit && hold_quit->kind != NO_EVENT)
3582 return;
3584 if (event->kind == ASCII_KEYSTROKE_EVENT)
3586 register int c = event->code & 0377;
3588 if (event->modifiers & ctrl_modifier)
3589 c = make_ctrl_char (c);
3591 c |= (event->modifiers
3592 & (meta_modifier | alt_modifier
3593 | hyper_modifier | super_modifier));
3595 if (c == quit_char)
3597 #ifdef MULTI_KBOARD
3598 KBOARD *kb;
3599 struct input_event *sp;
3601 if (single_kboard
3602 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
3603 kb != current_kboard))
3605 kb->kbd_queue
3606 = Fcons (make_lispy_switch_frame (event->frame_or_window),
3607 Fcons (make_number (c), Qnil));
3608 kb->kbd_queue_has_data = 1;
3609 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3611 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3612 sp = kbd_buffer;
3614 if (event_to_kboard (sp) == kb)
3616 sp->kind = NO_EVENT;
3617 sp->frame_or_window = Qnil;
3618 sp->arg = Qnil;
3621 return;
3623 #endif
3625 if (hold_quit)
3627 bcopy (event, (char *) hold_quit, sizeof (*event));
3628 return;
3631 /* If this results in a quit_char being returned to Emacs as
3632 input, set Vlast_event_frame properly. If this doesn't
3633 get returned to Emacs as an event, the next event read
3634 will set Vlast_event_frame again, so this is safe to do. */
3636 Lisp_Object focus;
3638 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
3639 if (NILP (focus))
3640 focus = event->frame_or_window;
3641 internal_last_event_frame = focus;
3642 Vlast_event_frame = focus;
3645 last_event_timestamp = event->timestamp;
3646 interrupt_signal (0 /* dummy */);
3647 return;
3650 if (c && c == stop_character)
3652 sys_suspend ();
3653 return;
3656 /* Don't insert two BUFFER_SWITCH_EVENT's in a row.
3657 Just ignore the second one. */
3658 else if (event->kind == BUFFER_SWITCH_EVENT
3659 && kbd_fetch_ptr != kbd_store_ptr
3660 && ((kbd_store_ptr == kbd_buffer
3661 ? kbd_buffer + KBD_BUFFER_SIZE - 1
3662 : kbd_store_ptr - 1)->kind) == BUFFER_SWITCH_EVENT)
3663 return;
3665 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
3666 kbd_store_ptr = kbd_buffer;
3668 /* Don't let the very last slot in the buffer become full,
3669 since that would make the two pointers equal,
3670 and that is indistinguishable from an empty buffer.
3671 Discard the event if it would fill the last slot. */
3672 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
3675 #if 0 /* The SELECTION_REQUEST_EVENT case looks bogus, and it's error
3676 prone to assign individual members for other events, in case
3677 the input_event structure is changed. --2000-07-13, gerd. */
3678 struct input_event *sp = kbd_store_ptr;
3679 sp->kind = event->kind;
3680 if (event->kind == SELECTION_REQUEST_EVENT)
3682 /* We must not use the ordinary copying code for this case,
3683 since `part' is an enum and copying it might not copy enough
3684 in this case. */
3685 bcopy (event, (char *) sp, sizeof (*event));
3687 else
3690 sp->code = event->code;
3691 sp->part = event->part;
3692 sp->frame_or_window = event->frame_or_window;
3693 sp->arg = event->arg;
3694 sp->modifiers = event->modifiers;
3695 sp->x = event->x;
3696 sp->y = event->y;
3697 sp->timestamp = event->timestamp;
3699 #else
3700 *kbd_store_ptr = *event;
3701 #endif
3703 ++kbd_store_ptr;
3708 /* Generate HELP_EVENT input_events in BUFP which has room for
3709 SIZE events. If there's not enough room in BUFP, ignore this
3710 event.
3712 HELP is the help form.
3714 FRAME is the frame on which the help is generated. OBJECT is the
3715 Lisp object where the help was found (a buffer, a string, an
3716 overlay, or nil if neither from a string nor from a buffer. POS is
3717 the position within OBJECT where the help was found.
3719 Value is the number of input_events generated. */
3721 void
3722 gen_help_event (help, frame, window, object, pos)
3723 Lisp_Object help, frame, object, window;
3724 int pos;
3726 struct input_event event;
3728 EVENT_INIT (event);
3730 event.kind = HELP_EVENT;
3731 event.frame_or_window = frame;
3732 event.arg = object;
3733 event.x = WINDOWP (window) ? window : frame;
3734 event.y = help;
3735 event.code = pos;
3736 kbd_buffer_store_event (&event);
3740 /* Store HELP_EVENTs for HELP on FRAME in the input queue. */
3742 void
3743 kbd_buffer_store_help_event (frame, help)
3744 Lisp_Object frame, help;
3746 struct input_event event;
3748 event.kind = HELP_EVENT;
3749 event.frame_or_window = frame;
3750 event.arg = Qnil;
3751 event.x = Qnil;
3752 event.y = help;
3753 event.code = 0;
3754 kbd_buffer_store_event (&event);
3758 /* Discard any mouse events in the event buffer by setting them to
3759 NO_EVENT. */
3760 void
3761 discard_mouse_events ()
3763 struct input_event *sp;
3764 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3766 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3767 sp = kbd_buffer;
3769 if (sp->kind == MOUSE_CLICK_EVENT
3770 || sp->kind == WHEEL_EVENT
3771 #ifdef WINDOWSNT
3772 || sp->kind == W32_SCROLL_BAR_CLICK_EVENT
3773 #endif
3774 || sp->kind == SCROLL_BAR_CLICK_EVENT)
3776 sp->kind = NO_EVENT;
3782 /* Return non-zero if there are any real events waiting in the event
3783 buffer, not counting `NO_EVENT's.
3785 If DISCARD is non-zero, discard NO_EVENT events at the front of
3786 the input queue, possibly leaving the input queue empty if there
3787 are no real input events. */
3790 kbd_buffer_events_waiting (discard)
3791 int discard;
3793 struct input_event *sp;
3795 for (sp = kbd_fetch_ptr;
3796 sp != kbd_store_ptr && sp->kind == NO_EVENT;
3797 ++sp)
3799 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3800 sp = kbd_buffer;
3803 if (discard)
3804 kbd_fetch_ptr = sp;
3806 return sp != kbd_store_ptr && sp->kind != NO_EVENT;
3810 /* Clear input event EVENT. */
3812 static INLINE void
3813 clear_event (event)
3814 struct input_event *event;
3816 event->kind = NO_EVENT;
3820 /* Read one event from the event buffer, waiting if necessary.
3821 The value is a Lisp object representing the event.
3822 The value is nil for an event that should be ignored,
3823 or that was handled here.
3824 We always read and discard one event. */
3826 static Lisp_Object
3827 kbd_buffer_get_event (kbp, used_mouse_menu)
3828 KBOARD **kbp;
3829 int *used_mouse_menu;
3831 register int c;
3832 Lisp_Object obj;
3834 if (noninteractive)
3836 c = getchar ();
3837 XSETINT (obj, c);
3838 *kbp = current_kboard;
3839 return obj;
3842 /* Wait until there is input available. */
3843 for (;;)
3845 if (kbd_fetch_ptr != kbd_store_ptr)
3846 break;
3847 #ifdef HAVE_MOUSE
3848 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3849 break;
3850 #endif
3852 /* If the quit flag is set, then read_char will return
3853 quit_char, so that counts as "available input." */
3854 if (!NILP (Vquit_flag))
3855 quit_throw_to_read_char ();
3857 /* One way or another, wait until input is available; then, if
3858 interrupt handlers have not read it, read it now. */
3860 #ifdef OLDVMS
3861 wait_for_kbd_input ();
3862 #else
3863 /* Note SIGIO has been undef'd if FIONREAD is missing. */
3864 #ifdef SIGIO
3865 gobble_input (0);
3866 #endif /* SIGIO */
3867 if (kbd_fetch_ptr != kbd_store_ptr)
3868 break;
3869 #ifdef HAVE_MOUSE
3870 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3871 break;
3872 #endif
3874 Lisp_Object minus_one;
3876 XSETINT (minus_one, -1);
3877 wait_reading_process_input (0, 0, minus_one, 1);
3879 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
3880 /* Pass 1 for EXPECT since we just waited to have input. */
3881 read_avail_input (1);
3883 #endif /* not VMS */
3886 if (CONSP (Vunread_command_events))
3888 Lisp_Object first;
3889 first = XCAR (Vunread_command_events);
3890 Vunread_command_events = XCDR (Vunread_command_events);
3891 *kbp = current_kboard;
3892 return first;
3895 /* At this point, we know that there is a readable event available
3896 somewhere. If the event queue is empty, then there must be a
3897 mouse movement enabled and available. */
3898 if (kbd_fetch_ptr != kbd_store_ptr)
3900 struct input_event *event;
3902 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3903 ? kbd_fetch_ptr
3904 : kbd_buffer);
3906 last_event_timestamp = event->timestamp;
3908 #ifdef MULTI_KBOARD
3909 *kbp = event_to_kboard (event);
3910 if (*kbp == 0)
3911 *kbp = current_kboard; /* Better than returning null ptr? */
3912 #else
3913 *kbp = &the_only_kboard;
3914 #endif
3916 obj = Qnil;
3918 /* These two kinds of events get special handling
3919 and don't actually appear to the command loop.
3920 We return nil for them. */
3921 if (event->kind == SELECTION_REQUEST_EVENT)
3923 #ifdef HAVE_X11
3924 struct input_event copy;
3926 /* Remove it from the buffer before processing it,
3927 since otherwise swallow_events will see it
3928 and process it again. */
3929 copy = *event;
3930 kbd_fetch_ptr = event + 1;
3931 input_pending = readable_events (0);
3932 x_handle_selection_request (&copy);
3933 #else
3934 /* We're getting selection request events, but we don't have
3935 a window system. */
3936 abort ();
3937 #endif
3940 else if (event->kind == SELECTION_CLEAR_EVENT)
3942 #ifdef HAVE_X11
3943 struct input_event copy;
3945 /* Remove it from the buffer before processing it. */
3946 copy = *event;
3947 kbd_fetch_ptr = event + 1;
3948 input_pending = readable_events (0);
3949 x_handle_selection_clear (&copy);
3950 #else
3951 /* We're getting selection request events, but we don't have
3952 a window system. */
3953 abort ();
3954 #endif
3956 #if defined (HAVE_X11) || defined (HAVE_NTGUI) || defined (MAC_OS)
3957 else if (event->kind == DELETE_WINDOW_EVENT)
3959 /* Make an event (delete-frame (FRAME)). */
3960 obj = Fcons (event->frame_or_window, Qnil);
3961 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
3962 kbd_fetch_ptr = event + 1;
3964 #endif
3965 #if defined (HAVE_X11) || defined (HAVE_NTGUI)
3966 else if (event->kind == ICONIFY_EVENT)
3968 /* Make an event (iconify-frame (FRAME)). */
3969 obj = Fcons (event->frame_or_window, Qnil);
3970 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
3971 kbd_fetch_ptr = event + 1;
3973 else if (event->kind == DEICONIFY_EVENT)
3975 /* Make an event (make-frame-visible (FRAME)). */
3976 obj = Fcons (event->frame_or_window, Qnil);
3977 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
3978 kbd_fetch_ptr = event + 1;
3980 #endif
3981 else if (event->kind == BUFFER_SWITCH_EVENT)
3983 /* The value doesn't matter here; only the type is tested. */
3984 XSETBUFFER (obj, current_buffer);
3985 kbd_fetch_ptr = event + 1;
3987 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
3988 || defined (USE_GTK)
3989 else if (event->kind == MENU_BAR_ACTIVATE_EVENT)
3991 kbd_fetch_ptr = event + 1;
3992 input_pending = readable_events (0);
3993 if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
3994 x_activate_menubar (XFRAME (event->frame_or_window));
3996 #endif
3997 #ifdef WINDOWSNT
3998 else if (event->kind == LANGUAGE_CHANGE_EVENT)
4000 /* Make an event (language-change (FRAME CHARSET LCID)). */
4001 obj = Fcons (event->frame_or_window, Qnil);
4002 obj = Fcons (Qlanguage_change, Fcons (obj, Qnil));
4003 kbd_fetch_ptr = event + 1;
4005 #endif
4006 else if (event->kind == SAVE_SESSION_EVENT)
4008 obj = Fcons (Qsave_session, Qnil);
4009 kbd_fetch_ptr = event + 1;
4011 /* Just discard these, by returning nil.
4012 With MULTI_KBOARD, these events are used as placeholders
4013 when we need to randomly delete events from the queue.
4014 (They shouldn't otherwise be found in the buffer,
4015 but on some machines it appears they do show up
4016 even without MULTI_KBOARD.) */
4017 /* On Windows NT/9X, NO_EVENT is used to delete extraneous
4018 mouse events during a popup-menu call. */
4019 else if (event->kind == NO_EVENT)
4020 kbd_fetch_ptr = event + 1;
4021 else if (event->kind == HELP_EVENT)
4023 Lisp_Object object, position, help, frame, window;
4025 frame = event->frame_or_window;
4026 object = event->arg;
4027 position = make_number (event->code);
4028 window = event->x;
4029 help = event->y;
4030 clear_event (event);
4032 kbd_fetch_ptr = event + 1;
4033 if (!WINDOWP (window))
4034 window = Qnil;
4035 obj = Fcons (Qhelp_echo,
4036 list5 (frame, help, window, object, position));
4038 else if (event->kind == FOCUS_IN_EVENT)
4040 /* Notification of a FocusIn event. The frame receiving the
4041 focus is in event->frame_or_window. Generate a
4042 switch-frame event if necessary. */
4043 Lisp_Object frame, focus;
4045 frame = event->frame_or_window;
4046 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
4047 if (FRAMEP (focus))
4048 frame = focus;
4050 if (!EQ (frame, internal_last_event_frame)
4051 && !EQ (frame, selected_frame))
4052 obj = make_lispy_switch_frame (frame);
4053 internal_last_event_frame = frame;
4054 kbd_fetch_ptr = event + 1;
4056 else
4058 /* If this event is on a different frame, return a switch-frame this
4059 time, and leave the event in the queue for next time. */
4060 Lisp_Object frame;
4061 Lisp_Object focus;
4063 frame = event->frame_or_window;
4064 if (CONSP (frame))
4065 frame = XCAR (frame);
4066 else if (WINDOWP (frame))
4067 frame = WINDOW_FRAME (XWINDOW (frame));
4069 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
4070 if (! NILP (focus))
4071 frame = focus;
4073 if (! EQ (frame, internal_last_event_frame)
4074 && !EQ (frame, selected_frame))
4075 obj = make_lispy_switch_frame (frame);
4076 internal_last_event_frame = frame;
4078 /* If we didn't decide to make a switch-frame event, go ahead
4079 and build a real event from the queue entry. */
4081 if (NILP (obj))
4083 obj = make_lispy_event (event);
4085 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined(MAC_OS) \
4086 || defined (USE_GTK)
4087 /* If this was a menu selection, then set the flag to inhibit
4088 writing to last_nonmenu_event. Don't do this if the event
4089 we're returning is (menu-bar), though; that indicates the
4090 beginning of the menu sequence, and we might as well leave
4091 that as the `event with parameters' for this selection. */
4092 if (used_mouse_menu
4093 && !EQ (event->frame_or_window, event->arg)
4094 && (event->kind == MENU_BAR_EVENT
4095 || event->kind == TOOL_BAR_EVENT))
4096 *used_mouse_menu = 1;
4097 #endif
4099 /* Wipe out this event, to catch bugs. */
4100 clear_event (event);
4101 kbd_fetch_ptr = event + 1;
4105 #ifdef HAVE_MOUSE
4106 /* Try generating a mouse motion event. */
4107 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
4109 FRAME_PTR f = some_mouse_moved ();
4110 Lisp_Object bar_window;
4111 enum scroll_bar_part part;
4112 Lisp_Object x, y;
4113 unsigned long time;
4115 *kbp = current_kboard;
4116 /* Note that this uses F to determine which display to look at.
4117 If there is no valid info, it does not store anything
4118 so x remains nil. */
4119 x = Qnil;
4120 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
4122 obj = Qnil;
4124 /* Decide if we should generate a switch-frame event. Don't
4125 generate switch-frame events for motion outside of all Emacs
4126 frames. */
4127 if (!NILP (x) && f)
4129 Lisp_Object frame;
4131 frame = FRAME_FOCUS_FRAME (f);
4132 if (NILP (frame))
4133 XSETFRAME (frame, f);
4135 if (! EQ (frame, internal_last_event_frame)
4136 && !EQ (frame, selected_frame))
4137 obj = make_lispy_switch_frame (frame);
4138 internal_last_event_frame = frame;
4141 /* If we didn't decide to make a switch-frame event, go ahead and
4142 return a mouse-motion event. */
4143 if (!NILP (x) && NILP (obj))
4144 obj = make_lispy_movement (f, bar_window, part, x, y, time);
4146 #endif /* HAVE_MOUSE */
4147 else
4148 /* We were promised by the above while loop that there was
4149 something for us to read! */
4150 abort ();
4152 input_pending = readable_events (0);
4154 Vlast_event_frame = internal_last_event_frame;
4156 return (obj);
4159 /* Process any events that are not user-visible,
4160 then return, without reading any user-visible events. */
4162 void
4163 swallow_events (do_display)
4164 int do_display;
4166 int old_timers_run;
4168 while (kbd_fetch_ptr != kbd_store_ptr)
4170 struct input_event *event;
4172 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
4173 ? kbd_fetch_ptr
4174 : kbd_buffer);
4176 last_event_timestamp = event->timestamp;
4178 /* These two kinds of events get special handling
4179 and don't actually appear to the command loop. */
4180 if (event->kind == SELECTION_REQUEST_EVENT)
4182 #ifdef HAVE_X11
4183 struct input_event copy;
4185 /* Remove it from the buffer before processing it,
4186 since otherwise swallow_events called recursively could see it
4187 and process it again. */
4188 copy = *event;
4189 kbd_fetch_ptr = event + 1;
4190 input_pending = readable_events (0);
4191 x_handle_selection_request (&copy);
4192 #else
4193 /* We're getting selection request events, but we don't have
4194 a window system. */
4195 abort ();
4196 #endif
4199 else if (event->kind == SELECTION_CLEAR_EVENT)
4201 #ifdef HAVE_X11
4202 struct input_event copy;
4204 /* Remove it from the buffer before processing it, */
4205 copy = *event;
4207 kbd_fetch_ptr = event + 1;
4208 input_pending = readable_events (0);
4209 x_handle_selection_clear (&copy);
4210 #else
4211 /* We're getting selection request events, but we don't have
4212 a window system. */
4213 abort ();
4214 #endif
4216 else
4217 break;
4220 old_timers_run = timers_run;
4221 get_input_pending (&input_pending, 1);
4223 if (timers_run != old_timers_run && do_display)
4224 redisplay_preserve_echo_area (7);
4227 /* Record the start of when Emacs is idle,
4228 for the sake of running idle-time timers. */
4230 void
4231 timer_start_idle ()
4233 Lisp_Object timers;
4235 /* If we are already in the idle state, do nothing. */
4236 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4237 return;
4239 EMACS_GET_TIME (timer_idleness_start_time);
4241 timer_last_idleness_start_time = timer_idleness_start_time;
4243 /* Mark all idle-time timers as once again candidates for running. */
4244 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCDR (timers))
4246 Lisp_Object timer;
4248 timer = XCAR (timers);
4250 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
4251 continue;
4252 XVECTOR (timer)->contents[0] = Qnil;
4256 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
4258 void
4259 timer_stop_idle ()
4261 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
4264 /* This is only for debugging. */
4265 struct input_event last_timer_event;
4267 /* Check whether a timer has fired. To prevent larger problems we simply
4268 disregard elements that are not proper timers. Do not make a circular
4269 timer list for the time being.
4271 Returns the number of seconds to wait until the next timer fires. If a
4272 timer is triggering now, return zero seconds.
4273 If no timer is active, return -1 seconds.
4275 If a timer is ripe, we run it, with quitting turned off.
4277 DO_IT_NOW is now ignored. It used to mean that we should
4278 run the timer directly instead of queueing a timer-event.
4279 Now we always run timers directly. */
4281 EMACS_TIME
4282 timer_check (do_it_now)
4283 int do_it_now;
4285 EMACS_TIME nexttime;
4286 EMACS_TIME now, idleness_now;
4287 Lisp_Object timers, idle_timers, chosen_timer;
4288 struct gcpro gcpro1, gcpro2, gcpro3;
4290 EMACS_SET_SECS (nexttime, -1);
4291 EMACS_SET_USECS (nexttime, -1);
4293 /* Always consider the ordinary timers. */
4294 timers = Vtimer_list;
4295 /* Consider the idle timers only if Emacs is idle. */
4296 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4297 idle_timers = Vtimer_idle_list;
4298 else
4299 idle_timers = Qnil;
4300 chosen_timer = Qnil;
4301 GCPRO3 (timers, idle_timers, chosen_timer);
4303 if (CONSP (timers) || CONSP (idle_timers))
4305 EMACS_GET_TIME (now);
4306 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4307 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
4310 while (CONSP (timers) || CONSP (idle_timers))
4312 Lisp_Object *vector;
4313 Lisp_Object timer = Qnil, idle_timer = Qnil;
4314 EMACS_TIME timer_time, idle_timer_time;
4315 EMACS_TIME difference, timer_difference, idle_timer_difference;
4317 /* Skip past invalid timers and timers already handled. */
4318 if (!NILP (timers))
4320 timer = XCAR (timers);
4321 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
4323 timers = XCDR (timers);
4324 continue;
4326 vector = XVECTOR (timer)->contents;
4328 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
4329 || !INTEGERP (vector[3])
4330 || ! NILP (vector[0]))
4332 timers = XCDR (timers);
4333 continue;
4336 if (!NILP (idle_timers))
4338 timer = XCAR (idle_timers);
4339 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
4341 idle_timers = XCDR (idle_timers);
4342 continue;
4344 vector = XVECTOR (timer)->contents;
4346 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
4347 || !INTEGERP (vector[3])
4348 || ! NILP (vector[0]))
4350 idle_timers = XCDR (idle_timers);
4351 continue;
4355 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
4356 based on the next ordinary timer.
4357 TIMER_DIFFERENCE is the distance in time from NOW to when
4358 this timer becomes ripe (negative if it's already ripe). */
4359 if (!NILP (timers))
4361 timer = XCAR (timers);
4362 vector = XVECTOR (timer)->contents;
4363 EMACS_SET_SECS (timer_time,
4364 (XINT (vector[1]) << 16) | (XINT (vector[2])));
4365 EMACS_SET_USECS (timer_time, XINT (vector[3]));
4366 EMACS_SUB_TIME (timer_difference, timer_time, now);
4369 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
4370 based on the next idle timer. */
4371 if (!NILP (idle_timers))
4373 idle_timer = XCAR (idle_timers);
4374 vector = XVECTOR (idle_timer)->contents;
4375 EMACS_SET_SECS (idle_timer_time,
4376 (XINT (vector[1]) << 16) | (XINT (vector[2])));
4377 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
4378 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
4381 /* Decide which timer is the next timer,
4382 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
4383 Also step down the list where we found that timer. */
4385 if (! NILP (timers) && ! NILP (idle_timers))
4387 EMACS_TIME temp;
4388 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
4389 if (EMACS_TIME_NEG_P (temp))
4391 chosen_timer = timer;
4392 timers = XCDR (timers);
4393 difference = timer_difference;
4395 else
4397 chosen_timer = idle_timer;
4398 idle_timers = XCDR (idle_timers);
4399 difference = idle_timer_difference;
4402 else if (! NILP (timers))
4404 chosen_timer = timer;
4405 timers = XCDR (timers);
4406 difference = timer_difference;
4408 else
4410 chosen_timer = idle_timer;
4411 idle_timers = XCDR (idle_timers);
4412 difference = idle_timer_difference;
4414 vector = XVECTOR (chosen_timer)->contents;
4416 /* If timer is ripe, run it if it hasn't been run. */
4417 if (EMACS_TIME_NEG_P (difference)
4418 || (EMACS_SECS (difference) == 0
4419 && EMACS_USECS (difference) == 0))
4421 if (NILP (vector[0]))
4423 int was_locked = single_kboard;
4424 int count = SPECPDL_INDEX ();
4425 Lisp_Object old_deactivate_mark = Vdeactivate_mark;
4427 /* Mark the timer as triggered to prevent problems if the lisp
4428 code fails to reschedule it right. */
4429 vector[0] = Qt;
4431 specbind (Qinhibit_quit, Qt);
4433 call1 (Qtimer_event_handler, chosen_timer);
4434 Vdeactivate_mark = old_deactivate_mark;
4435 timers_run++;
4436 unbind_to (count, Qnil);
4438 /* Resume allowing input from any kboard, if that was true before. */
4439 if (!was_locked)
4440 any_kboard_state ();
4442 /* Since we have handled the event,
4443 we don't need to tell the caller to wake up and do it. */
4446 else
4447 /* When we encounter a timer that is still waiting,
4448 return the amount of time to wait before it is ripe. */
4450 UNGCPRO;
4451 return difference;
4455 /* No timers are pending in the future. */
4456 /* Return 0 if we generated an event, and -1 if not. */
4457 UNGCPRO;
4458 return nexttime;
4461 /* Caches for modify_event_symbol. */
4462 static Lisp_Object accent_key_syms;
4463 static Lisp_Object func_key_syms;
4464 static Lisp_Object mouse_syms;
4465 static Lisp_Object wheel_syms;
4466 static Lisp_Object drag_n_drop_syms;
4468 /* This is a list of keysym codes for special "accent" characters.
4469 It parallels lispy_accent_keys. */
4471 static int lispy_accent_codes[] =
4473 #ifdef XK_dead_circumflex
4474 XK_dead_circumflex,
4475 #else
4477 #endif
4478 #ifdef XK_dead_grave
4479 XK_dead_grave,
4480 #else
4482 #endif
4483 #ifdef XK_dead_tilde
4484 XK_dead_tilde,
4485 #else
4487 #endif
4488 #ifdef XK_dead_diaeresis
4489 XK_dead_diaeresis,
4490 #else
4492 #endif
4493 #ifdef XK_dead_macron
4494 XK_dead_macron,
4495 #else
4497 #endif
4498 #ifdef XK_dead_degree
4499 XK_dead_degree,
4500 #else
4502 #endif
4503 #ifdef XK_dead_acute
4504 XK_dead_acute,
4505 #else
4507 #endif
4508 #ifdef XK_dead_cedilla
4509 XK_dead_cedilla,
4510 #else
4512 #endif
4513 #ifdef XK_dead_breve
4514 XK_dead_breve,
4515 #else
4517 #endif
4518 #ifdef XK_dead_ogonek
4519 XK_dead_ogonek,
4520 #else
4522 #endif
4523 #ifdef XK_dead_caron
4524 XK_dead_caron,
4525 #else
4527 #endif
4528 #ifdef XK_dead_doubleacute
4529 XK_dead_doubleacute,
4530 #else
4532 #endif
4533 #ifdef XK_dead_abovedot
4534 XK_dead_abovedot,
4535 #else
4537 #endif
4538 #ifdef XK_dead_abovering
4539 XK_dead_abovering,
4540 #else
4542 #endif
4543 #ifdef XK_dead_iota
4544 XK_dead_iota,
4545 #else
4547 #endif
4548 #ifdef XK_dead_belowdot
4549 XK_dead_belowdot,
4550 #else
4552 #endif
4553 #ifdef XK_dead_voiced_sound
4554 XK_dead_voiced_sound,
4555 #else
4557 #endif
4558 #ifdef XK_dead_semivoiced_sound
4559 XK_dead_semivoiced_sound,
4560 #else
4562 #endif
4563 #ifdef XK_dead_hook
4564 XK_dead_hook,
4565 #else
4567 #endif
4568 #ifdef XK_dead_horn
4569 XK_dead_horn,
4570 #else
4572 #endif
4575 /* This is a list of Lisp names for special "accent" characters.
4576 It parallels lispy_accent_codes. */
4578 static char *lispy_accent_keys[] =
4580 "dead-circumflex",
4581 "dead-grave",
4582 "dead-tilde",
4583 "dead-diaeresis",
4584 "dead-macron",
4585 "dead-degree",
4586 "dead-acute",
4587 "dead-cedilla",
4588 "dead-breve",
4589 "dead-ogonek",
4590 "dead-caron",
4591 "dead-doubleacute",
4592 "dead-abovedot",
4593 "dead-abovering",
4594 "dead-iota",
4595 "dead-belowdot",
4596 "dead-voiced-sound",
4597 "dead-semivoiced-sound",
4598 "dead-hook",
4599 "dead-horn",
4602 #ifdef HAVE_NTGUI
4603 #define FUNCTION_KEY_OFFSET 0x0
4605 char *lispy_function_keys[] =
4607 0, /* 0 */
4609 0, /* VK_LBUTTON 0x01 */
4610 0, /* VK_RBUTTON 0x02 */
4611 "cancel", /* VK_CANCEL 0x03 */
4612 0, /* VK_MBUTTON 0x04 */
4614 0, 0, 0, /* 0x05 .. 0x07 */
4616 "backspace", /* VK_BACK 0x08 */
4617 "tab", /* VK_TAB 0x09 */
4619 0, 0, /* 0x0A .. 0x0B */
4621 "clear", /* VK_CLEAR 0x0C */
4622 "return", /* VK_RETURN 0x0D */
4624 0, 0, /* 0x0E .. 0x0F */
4626 0, /* VK_SHIFT 0x10 */
4627 0, /* VK_CONTROL 0x11 */
4628 0, /* VK_MENU 0x12 */
4629 "pause", /* VK_PAUSE 0x13 */
4630 "capslock", /* VK_CAPITAL 0x14 */
4632 0, 0, 0, 0, 0, 0, /* 0x15 .. 0x1A */
4634 "escape", /* VK_ESCAPE 0x1B */
4636 0, 0, 0, 0, /* 0x1C .. 0x1F */
4638 0, /* VK_SPACE 0x20 */
4639 "prior", /* VK_PRIOR 0x21 */
4640 "next", /* VK_NEXT 0x22 */
4641 "end", /* VK_END 0x23 */
4642 "home", /* VK_HOME 0x24 */
4643 "left", /* VK_LEFT 0x25 */
4644 "up", /* VK_UP 0x26 */
4645 "right", /* VK_RIGHT 0x27 */
4646 "down", /* VK_DOWN 0x28 */
4647 "select", /* VK_SELECT 0x29 */
4648 "print", /* VK_PRINT 0x2A */
4649 "execute", /* VK_EXECUTE 0x2B */
4650 "snapshot", /* VK_SNAPSHOT 0x2C */
4651 "insert", /* VK_INSERT 0x2D */
4652 "delete", /* VK_DELETE 0x2E */
4653 "help", /* VK_HELP 0x2F */
4655 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
4657 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4659 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
4661 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
4663 0, 0, 0, 0, 0, 0, 0, 0, 0,
4664 0, 0, 0, 0, 0, 0, 0, 0, 0,
4665 0, 0, 0, 0, 0, 0, 0, 0,
4667 "lwindow", /* VK_LWIN 0x5B */
4668 "rwindow", /* VK_RWIN 0x5C */
4669 "apps", /* VK_APPS 0x5D */
4671 0, 0, /* 0x5E .. 0x5F */
4673 "kp-0", /* VK_NUMPAD0 0x60 */
4674 "kp-1", /* VK_NUMPAD1 0x61 */
4675 "kp-2", /* VK_NUMPAD2 0x62 */
4676 "kp-3", /* VK_NUMPAD3 0x63 */
4677 "kp-4", /* VK_NUMPAD4 0x64 */
4678 "kp-5", /* VK_NUMPAD5 0x65 */
4679 "kp-6", /* VK_NUMPAD6 0x66 */
4680 "kp-7", /* VK_NUMPAD7 0x67 */
4681 "kp-8", /* VK_NUMPAD8 0x68 */
4682 "kp-9", /* VK_NUMPAD9 0x69 */
4683 "kp-multiply", /* VK_MULTIPLY 0x6A */
4684 "kp-add", /* VK_ADD 0x6B */
4685 "kp-separator", /* VK_SEPARATOR 0x6C */
4686 "kp-subtract", /* VK_SUBTRACT 0x6D */
4687 "kp-decimal", /* VK_DECIMAL 0x6E */
4688 "kp-divide", /* VK_DIVIDE 0x6F */
4689 "f1", /* VK_F1 0x70 */
4690 "f2", /* VK_F2 0x71 */
4691 "f3", /* VK_F3 0x72 */
4692 "f4", /* VK_F4 0x73 */
4693 "f5", /* VK_F5 0x74 */
4694 "f6", /* VK_F6 0x75 */
4695 "f7", /* VK_F7 0x76 */
4696 "f8", /* VK_F8 0x77 */
4697 "f9", /* VK_F9 0x78 */
4698 "f10", /* VK_F10 0x79 */
4699 "f11", /* VK_F11 0x7A */
4700 "f12", /* VK_F12 0x7B */
4701 "f13", /* VK_F13 0x7C */
4702 "f14", /* VK_F14 0x7D */
4703 "f15", /* VK_F15 0x7E */
4704 "f16", /* VK_F16 0x7F */
4705 "f17", /* VK_F17 0x80 */
4706 "f18", /* VK_F18 0x81 */
4707 "f19", /* VK_F19 0x82 */
4708 "f20", /* VK_F20 0x83 */
4709 "f21", /* VK_F21 0x84 */
4710 "f22", /* VK_F22 0x85 */
4711 "f23", /* VK_F23 0x86 */
4712 "f24", /* VK_F24 0x87 */
4714 0, 0, 0, 0, /* 0x88 .. 0x8B */
4715 0, 0, 0, 0, /* 0x8C .. 0x8F */
4717 "kp-numlock", /* VK_NUMLOCK 0x90 */
4718 "scroll", /* VK_SCROLL 0x91 */
4720 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
4721 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
4722 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
4723 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
4724 "kp-end", /* VK_NUMPAD_END 0x96 */
4725 "kp-home", /* VK_NUMPAD_HOME 0x97 */
4726 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
4727 "kp-up", /* VK_NUMPAD_UP 0x99 */
4728 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
4729 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
4730 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
4731 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
4733 0, 0, /* 0x9E .. 0x9F */
4736 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
4737 * Used only as parameters to GetAsyncKeyState and GetKeyState.
4738 * No other API or message will distinguish left and right keys this way.
4740 /* 0xA0 .. 0xEF */
4742 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4743 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4744 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4745 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4746 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4748 /* 0xF0 .. 0xF5 */
4750 0, 0, 0, 0, 0, 0,
4752 "attn", /* VK_ATTN 0xF6 */
4753 "crsel", /* VK_CRSEL 0xF7 */
4754 "exsel", /* VK_EXSEL 0xF8 */
4755 "ereof", /* VK_EREOF 0xF9 */
4756 "play", /* VK_PLAY 0xFA */
4757 "zoom", /* VK_ZOOM 0xFB */
4758 "noname", /* VK_NONAME 0xFC */
4759 "pa1", /* VK_PA1 0xFD */
4760 "oem_clear", /* VK_OEM_CLEAR 0xFE */
4761 0 /* 0xFF */
4764 #else /* not HAVE_NTGUI */
4766 /* This should be dealt with in XTread_socket now, and that doesn't
4767 depend on the client system having the Kana syms defined. See also
4768 the XK_kana_A case below. */
4769 #if 0
4770 #ifdef XK_kana_A
4771 static char *lispy_kana_keys[] =
4773 /* X Keysym value */
4774 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x400 .. 0x40f */
4775 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x410 .. 0x41f */
4776 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x420 .. 0x42f */
4777 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x430 .. 0x43f */
4778 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x440 .. 0x44f */
4779 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x450 .. 0x45f */
4780 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x460 .. 0x46f */
4781 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0,
4782 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x480 .. 0x48f */
4783 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x490 .. 0x49f */
4784 0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket",
4785 "kana-comma", "kana-conjunctive", "kana-WO", "kana-a",
4786 "kana-i", "kana-u", "kana-e", "kana-o",
4787 "kana-ya", "kana-yu", "kana-yo", "kana-tsu",
4788 "prolongedsound", "kana-A", "kana-I", "kana-U",
4789 "kana-E", "kana-O", "kana-KA", "kana-KI",
4790 "kana-KU", "kana-KE", "kana-KO", "kana-SA",
4791 "kana-SHI", "kana-SU", "kana-SE", "kana-SO",
4792 "kana-TA", "kana-CHI", "kana-TSU", "kana-TE",
4793 "kana-TO", "kana-NA", "kana-NI", "kana-NU",
4794 "kana-NE", "kana-NO", "kana-HA", "kana-HI",
4795 "kana-FU", "kana-HE", "kana-HO", "kana-MA",
4796 "kana-MI", "kana-MU", "kana-ME", "kana-MO",
4797 "kana-YA", "kana-YU", "kana-YO", "kana-RA",
4798 "kana-RI", "kana-RU", "kana-RE", "kana-RO",
4799 "kana-WA", "kana-N", "voicedsound", "semivoicedsound",
4800 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4e0 .. 0x4ef */
4801 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f0 .. 0x4ff */
4803 #endif /* XK_kana_A */
4804 #endif /* 0 */
4806 #define FUNCTION_KEY_OFFSET 0xff00
4808 /* You'll notice that this table is arranged to be conveniently
4809 indexed by X Windows keysym values. */
4810 static char *lispy_function_keys[] =
4812 /* X Keysym value */
4814 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00...0f */
4815 "backspace", "tab", "linefeed", "clear",
4816 0, "return", 0, 0,
4817 0, 0, 0, "pause", /* 0xff10...1f */
4818 0, 0, 0, 0, 0, 0, 0, "escape",
4819 0, 0, 0, 0,
4820 0, "kanji", "muhenkan", "henkan", /* 0xff20...2f */
4821 "romaji", "hiragana", "katakana", "hiragana-katakana",
4822 "zenkaku", "hankaku", "zenkaku-hankaku", "touroku",
4823 "massyo", "kana-lock", "kana-shift", "eisu-shift",
4824 "eisu-toggle", /* 0xff30...3f */
4825 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4826 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
4828 "home", "left", "up", "right", /* 0xff50 */ /* IsCursorKey */
4829 "down", "prior", "next", "end",
4830 "begin", 0, 0, 0, 0, 0, 0, 0,
4831 "select", /* 0xff60 */ /* IsMiscFunctionKey */
4832 "print",
4833 "execute",
4834 "insert",
4835 0, /* 0xff64 */
4836 "undo",
4837 "redo",
4838 "menu",
4839 "find",
4840 "cancel",
4841 "help",
4842 "break", /* 0xff6b */
4844 0, 0, 0, 0,
4845 0, 0, 0, 0, "backtab", 0, 0, 0, /* 0xff70... */
4846 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff78... */
4847 "kp-space", /* 0xff80 */ /* IsKeypadKey */
4848 0, 0, 0, 0, 0, 0, 0, 0,
4849 "kp-tab", /* 0xff89 */
4850 0, 0, 0,
4851 "kp-enter", /* 0xff8d */
4852 0, 0, 0,
4853 "kp-f1", /* 0xff91 */
4854 "kp-f2",
4855 "kp-f3",
4856 "kp-f4",
4857 "kp-home", /* 0xff95 */
4858 "kp-left",
4859 "kp-up",
4860 "kp-right",
4861 "kp-down",
4862 "kp-prior", /* kp-page-up */
4863 "kp-next", /* kp-page-down */
4864 "kp-end",
4865 "kp-begin",
4866 "kp-insert",
4867 "kp-delete",
4868 0, /* 0xffa0 */
4869 0, 0, 0, 0, 0, 0, 0, 0, 0,
4870 "kp-multiply", /* 0xffaa */
4871 "kp-add",
4872 "kp-separator",
4873 "kp-subtract",
4874 "kp-decimal",
4875 "kp-divide", /* 0xffaf */
4876 "kp-0", /* 0xffb0 */
4877 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
4878 0, /* 0xffba */
4879 0, 0,
4880 "kp-equal", /* 0xffbd */
4881 "f1", /* 0xffbe */ /* IsFunctionKey */
4882 "f2",
4883 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
4884 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
4885 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
4886 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
4887 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
4888 0, 0, 0, 0, 0, 0, 0, 0,
4889 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
4890 0, 0, 0, 0, 0, 0, 0, "delete"
4893 /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE. */
4894 #define ISO_FUNCTION_KEY_OFFSET 0xfe00
4896 static char *iso_lispy_function_keys[] =
4898 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe00 */
4899 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe08 */
4900 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe10 */
4901 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe18 */
4902 "iso-lefttab", /* 0xfe20 */
4903 "iso-move-line-up", "iso-move-line-down",
4904 "iso-partial-line-up", "iso-partial-line-down",
4905 "iso-partial-space-left", "iso-partial-space-right",
4906 "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */
4907 "iso-release-margin-left", "iso-release-margin-right",
4908 "iso-release-both-margins",
4909 "iso-fast-cursor-left", "iso-fast-cursor-right",
4910 "iso-fast-cursor-up", "iso-fast-cursor-down",
4911 "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */
4912 "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */
4915 #endif /* not HAVE_NTGUI */
4917 Lisp_Object Vlispy_mouse_stem;
4919 static char *lispy_wheel_names[] =
4921 "wheel-up", "wheel-down"
4924 /* drag-n-drop events are generated when a set of selected files are
4925 dragged from another application and dropped onto an Emacs window. */
4926 static char *lispy_drag_n_drop_names[] =
4928 "drag-n-drop"
4931 /* Scroll bar parts. */
4932 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
4933 Lisp_Object Qup, Qdown, Qbottom, Qend_scroll;
4934 Lisp_Object Qtop, Qratio;
4936 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
4937 Lisp_Object *scroll_bar_parts[] = {
4938 &Qabove_handle, &Qhandle, &Qbelow_handle,
4939 &Qup, &Qdown, &Qtop, &Qbottom, &Qend_scroll, &Qratio
4942 /* User signal events. */
4943 Lisp_Object Qusr1_signal, Qusr2_signal;
4945 Lisp_Object *lispy_user_signals[] =
4947 &Qusr1_signal, &Qusr2_signal
4951 /* A vector, indexed by button number, giving the down-going location
4952 of currently depressed buttons, both scroll bar and non-scroll bar.
4954 The elements have the form
4955 (BUTTON-NUMBER MODIFIER-MASK . REST)
4956 where REST is the cdr of a position as it would be reported in the event.
4958 The make_lispy_event function stores positions here to tell the
4959 difference between click and drag events, and to store the starting
4960 location to be included in drag events. */
4962 static Lisp_Object button_down_location;
4964 /* Information about the most recent up-going button event: Which
4965 button, what location, and what time. */
4967 static int last_mouse_button;
4968 static int last_mouse_x;
4969 static int last_mouse_y;
4970 static unsigned long button_down_time;
4972 /* The maximum time between clicks to make a double-click, or Qnil to
4973 disable double-click detection, or Qt for no time limit. */
4975 Lisp_Object Vdouble_click_time;
4977 /* Maximum number of pixels the mouse may be moved between clicks
4978 to make a double-click. */
4980 EMACS_INT double_click_fuzz;
4982 /* The number of clicks in this multiple-click. */
4984 int double_click_count;
4986 /* Return position of a mouse click or wheel event */
4988 static Lisp_Object
4989 make_lispy_position (f, x, y, time)
4990 struct frame *f;
4991 Lisp_Object *x, *y;
4992 unsigned long time;
4994 Lisp_Object window;
4995 enum window_part part;
4996 Lisp_Object posn = Qnil;
4997 Lisp_Object extra_info = Qnil;
4998 int wx, wy;
5000 /* Set `window' to the window under frame pixel coordinates (x,y) */
5001 if (f)
5002 window = window_from_coordinates (f, XINT (*x), XINT (*y),
5003 &part, &wx, &wy, 0);
5004 else
5005 window = Qnil;
5007 if (WINDOWP (window))
5009 /* It's a click in window window at frame coordinates (x,y) */
5010 struct window *w = XWINDOW (window);
5011 Lisp_Object string_info = Qnil;
5012 int textpos = -1, rx = -1, ry = -1;
5013 int dx = -1, dy = -1;
5014 int width = -1, height = -1;
5015 Lisp_Object object = Qnil;
5017 /* Set event coordinates to window-relative coordinates
5018 for constructing the Lisp event below. */
5019 XSETINT (*x, wx);
5020 XSETINT (*y, wy);
5022 if (part == ON_MODE_LINE || part == ON_HEADER_LINE)
5024 /* Mode line or header line. Look for a string under
5025 the mouse that may have a `local-map' property. */
5026 Lisp_Object string;
5027 int charpos;
5029 posn = part == ON_MODE_LINE ? Qmode_line : Qheader_line;
5030 rx = wx, ry = wy;
5031 string = mode_line_string (w, part, &rx, &ry, &charpos,
5032 &object, &dx, &dy, &width, &height);
5033 if (STRINGP (string))
5034 string_info = Fcons (string, make_number (charpos));
5035 if (w == XWINDOW (selected_window))
5036 textpos = PT;
5037 else
5038 textpos = XMARKER (w->pointm)->charpos;
5040 else if (part == ON_VERTICAL_BORDER)
5042 posn = Qvertical_line;
5043 wx = -1;
5044 dx = 0;
5045 width = 1;
5047 else if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
5049 Lisp_Object string;
5050 int charpos;
5052 posn = (part == ON_LEFT_MARGIN) ? Qleft_margin : Qright_margin;
5053 rx = wx, ry = wy;
5054 string = marginal_area_string (w, part, &rx, &ry, &charpos,
5055 &object, &dx, &dy, &width, &height);
5056 if (STRINGP (string))
5057 string_info = Fcons (string, make_number (charpos));
5059 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
5061 posn = (part == ON_LEFT_FRINGE) ? Qleft_fringe : Qright_fringe;
5062 rx = 0;
5063 dx = wx;
5064 if (part == ON_RIGHT_FRINGE)
5065 dx -= (window_box_width (w, LEFT_MARGIN_AREA)
5066 + window_box_width (w, TEXT_AREA)
5067 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5068 ? window_box_width (w, RIGHT_MARGIN_AREA)
5069 : 0));
5070 else if (!WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
5071 dx -= window_box_width (w, LEFT_MARGIN_AREA);
5074 if (textpos < 0)
5076 Lisp_Object string2, object2 = Qnil;
5077 struct display_pos p;
5078 int dx2, dy2;
5079 int width2, height2;
5080 wx = max (WINDOW_LEFT_MARGIN_WIDTH (w), wx);
5081 string2 = buffer_posn_from_coords (w, &wx, &wy, &p,
5082 &object2, &dx2, &dy2,
5083 &width2, &height2);
5084 textpos = CHARPOS (p.pos);
5085 if (rx < 0) rx = wx;
5086 if (ry < 0) ry = wy;
5087 if (dx < 0) dx = dx2;
5088 if (dy < 0) dy = dy2;
5089 if (width < 0) width = width2;
5090 if (height < 0) height = height2;
5092 if (NILP (posn))
5094 posn = make_number (textpos);
5095 if (STRINGP (string2))
5096 string_info = Fcons (string2,
5097 make_number (CHARPOS (p.string_pos)));
5099 if (NILP (object))
5100 object = object2;
5103 #ifdef HAVE_WINDOW_SYSTEM
5104 if (IMAGEP (object))
5106 Lisp_Object image_map, hotspot;
5107 if ((image_map = Fplist_get (XCDR (object), QCmap),
5108 !NILP (image_map))
5109 && (hotspot = find_hot_spot (image_map, dx, dy),
5110 CONSP (hotspot))
5111 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
5112 posn = XCAR (hotspot);
5114 #endif
5116 /* Object info */
5117 extra_info = Fcons (object,
5118 Fcons (Fcons (make_number (dx),
5119 make_number (dy)),
5120 Fcons (Fcons (make_number (width),
5121 make_number (height)),
5122 Qnil)));
5124 /* String info */
5125 extra_info = Fcons (string_info,
5126 Fcons (make_number (textpos),
5127 Fcons (Fcons (make_number (rx),
5128 make_number (ry)),
5129 extra_info)));
5131 else if (f != 0)
5133 XSETFRAME (window, f);
5135 else
5137 window = Qnil;
5138 XSETFASTINT (*x, 0);
5139 XSETFASTINT (*y, 0);
5142 return Fcons (window,
5143 Fcons (posn,
5144 Fcons (Fcons (*x, *y),
5145 Fcons (make_number (time),
5146 extra_info))));
5149 /* Given a struct input_event, build the lisp event which represents
5150 it. If EVENT is 0, build a mouse movement event from the mouse
5151 movement buffer, which should have a movement event in it.
5153 Note that events must be passed to this function in the order they
5154 are received; this function stores the location of button presses
5155 in order to build drag events when the button is released. */
5157 static Lisp_Object
5158 make_lispy_event (event)
5159 struct input_event *event;
5161 int i;
5163 switch (SWITCH_ENUM_CAST (event->kind))
5165 /* A simple keystroke. */
5166 case ASCII_KEYSTROKE_EVENT:
5168 Lisp_Object lispy_c;
5169 int c = event->code & 0377;
5170 /* Turn ASCII characters into control characters
5171 when proper. */
5172 if (event->modifiers & ctrl_modifier)
5173 c = make_ctrl_char (c);
5175 /* Add in the other modifier bits. We took care of ctrl_modifier
5176 just above, and the shift key was taken care of by the X code,
5177 and applied to control characters by make_ctrl_char. */
5178 c |= (event->modifiers
5179 & (meta_modifier | alt_modifier
5180 | hyper_modifier | super_modifier));
5181 /* Distinguish Shift-SPC from SPC. */
5182 if ((event->code & 0377) == 040
5183 && event->modifiers & shift_modifier)
5184 c |= shift_modifier;
5185 button_down_time = 0;
5186 XSETFASTINT (lispy_c, c);
5187 return lispy_c;
5190 case MULTIBYTE_CHAR_KEYSTROKE_EVENT:
5192 Lisp_Object lispy_c;
5193 int c = event->code;
5195 /* Add in the other modifier bits. We took care of ctrl_modifier
5196 just above, and the shift key was taken care of by the X code,
5197 and applied to control characters by make_ctrl_char. */
5198 c |= (event->modifiers
5199 & (meta_modifier | alt_modifier
5200 | hyper_modifier | super_modifier | ctrl_modifier));
5201 /* What about the `shift' modifier ? */
5202 button_down_time = 0;
5203 XSETFASTINT (lispy_c, c);
5204 return lispy_c;
5207 /* A function key. The symbol may need to have modifier prefixes
5208 tacked onto it. */
5209 case NON_ASCII_KEYSTROKE_EVENT:
5210 button_down_time = 0;
5212 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
5213 if (event->code == lispy_accent_codes[i])
5214 return modify_event_symbol (i,
5215 event->modifiers,
5216 Qfunction_key, Qnil,
5217 lispy_accent_keys, &accent_key_syms,
5218 (sizeof (lispy_accent_keys)
5219 / sizeof (lispy_accent_keys[0])));
5221 #if 0
5222 #ifdef XK_kana_A
5223 if (event->code >= 0x400 && event->code < 0x500)
5224 return modify_event_symbol (event->code - 0x400,
5225 event->modifiers & ~shift_modifier,
5226 Qfunction_key, Qnil,
5227 lispy_kana_keys, &func_key_syms,
5228 (sizeof (lispy_kana_keys)
5229 / sizeof (lispy_kana_keys[0])));
5230 #endif /* XK_kana_A */
5231 #endif /* 0 */
5233 #ifdef ISO_FUNCTION_KEY_OFFSET
5234 if (event->code < FUNCTION_KEY_OFFSET
5235 && event->code >= ISO_FUNCTION_KEY_OFFSET)
5236 return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET,
5237 event->modifiers,
5238 Qfunction_key, Qnil,
5239 iso_lispy_function_keys, &func_key_syms,
5240 (sizeof (iso_lispy_function_keys)
5241 / sizeof (iso_lispy_function_keys[0])));
5242 #endif
5244 /* Handle system-specific or unknown keysyms. */
5245 if (event->code & (1 << 28)
5246 || event->code - FUNCTION_KEY_OFFSET < 0
5247 || (event->code - FUNCTION_KEY_OFFSET
5248 >= sizeof lispy_function_keys / sizeof *lispy_function_keys)
5249 || !lispy_function_keys[event->code - FUNCTION_KEY_OFFSET])
5251 /* We need to use an alist rather than a vector as the cache
5252 since we can't make a vector long enuf. */
5253 if (NILP (current_kboard->system_key_syms))
5254 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
5255 return modify_event_symbol (event->code,
5256 event->modifiers,
5257 Qfunction_key,
5258 current_kboard->Vsystem_key_alist,
5259 0, &current_kboard->system_key_syms,
5260 (unsigned) -1);
5263 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
5264 event->modifiers,
5265 Qfunction_key, Qnil,
5266 lispy_function_keys, &func_key_syms,
5267 (sizeof (lispy_function_keys)
5268 / sizeof (lispy_function_keys[0])));
5270 #ifdef HAVE_MOUSE
5271 /* A mouse click. Figure out where it is, decide whether it's
5272 a press, click or drag, and build the appropriate structure. */
5273 case MOUSE_CLICK_EVENT:
5274 #ifndef USE_TOOLKIT_SCROLL_BARS
5275 case SCROLL_BAR_CLICK_EVENT:
5276 #endif
5278 int button = event->code;
5279 int is_double;
5280 Lisp_Object position;
5281 Lisp_Object *start_pos_ptr;
5282 Lisp_Object start_pos;
5284 position = Qnil;
5286 /* Build the position as appropriate for this mouse click. */
5287 if (event->kind == MOUSE_CLICK_EVENT)
5289 struct frame *f = XFRAME (event->frame_or_window);
5290 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
5291 int row, column;
5292 #endif
5294 /* Ignore mouse events that were made on frame that
5295 have been deleted. */
5296 if (! FRAME_LIVE_P (f))
5297 return Qnil;
5299 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
5300 /* EVENT->x and EVENT->y are frame-relative pixel
5301 coordinates at this place. Under old redisplay, COLUMN
5302 and ROW are set to frame relative glyph coordinates
5303 which are then used to determine whether this click is
5304 in a menu (non-toolkit version). */
5305 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
5306 &column, &row, NULL, 1);
5308 /* In the non-toolkit version, clicks on the menu bar
5309 are ordinary button events in the event buffer.
5310 Distinguish them, and invoke the menu.
5312 (In the toolkit version, the toolkit handles the menu bar
5313 and Emacs doesn't know about it until after the user
5314 makes a selection.) */
5315 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
5316 && (event->modifiers & down_modifier))
5318 Lisp_Object items, item;
5319 int hpos;
5320 int i;
5322 #if 0
5323 /* Activate the menu bar on the down event. If the
5324 up event comes in before the menu code can deal with it,
5325 just ignore it. */
5326 if (! (event->modifiers & down_modifier))
5327 return Qnil;
5328 #endif
5330 /* Find the menu bar item under `column'. */
5331 item = Qnil;
5332 items = FRAME_MENU_BAR_ITEMS (f);
5333 for (i = 0; i < XVECTOR (items)->size; i += 4)
5335 Lisp_Object pos, string;
5336 string = AREF (items, i + 1);
5337 pos = AREF (items, i + 3);
5338 if (NILP (string))
5339 break;
5340 if (column >= XINT (pos)
5341 && column < XINT (pos) + SCHARS (string))
5343 item = AREF (items, i);
5344 break;
5348 /* ELisp manual 2.4b says (x y) are window relative but
5349 code says they are frame-relative. */
5350 position
5351 = Fcons (event->frame_or_window,
5352 Fcons (Qmenu_bar,
5353 Fcons (Fcons (event->x, event->y),
5354 Fcons (make_number (event->timestamp),
5355 Qnil))));
5357 return Fcons (item, Fcons (position, Qnil));
5359 #endif /* not USE_X_TOOLKIT && not USE_GTK */
5361 position = make_lispy_position (f, &event->x, &event->y,
5362 event->timestamp);
5364 #ifndef USE_TOOLKIT_SCROLL_BARS
5365 else
5367 /* It's a scrollbar click. */
5368 Lisp_Object window;
5369 Lisp_Object portion_whole;
5370 Lisp_Object part;
5372 window = event->frame_or_window;
5373 portion_whole = Fcons (event->x, event->y);
5374 part = *scroll_bar_parts[(int) event->part];
5376 position
5377 = Fcons (window,
5378 Fcons (Qvertical_scroll_bar,
5379 Fcons (portion_whole,
5380 Fcons (make_number (event->timestamp),
5381 Fcons (part, Qnil)))));
5383 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5385 if (button >= ASIZE (button_down_location))
5387 button_down_location = larger_vector (button_down_location,
5388 button + 1, Qnil);
5389 mouse_syms = larger_vector (mouse_syms, button + 1, Qnil);
5392 start_pos_ptr = &AREF (button_down_location, button);
5393 start_pos = *start_pos_ptr;
5394 *start_pos_ptr = Qnil;
5397 /* On window-system frames, use the value of
5398 double-click-fuzz as is. On other frames, interpret it
5399 as a multiple of 1/8 characters. */
5400 struct frame *f;
5401 int fuzz;
5403 if (WINDOWP (event->frame_or_window))
5404 f = XFRAME (XWINDOW (event->frame_or_window)->frame);
5405 else if (FRAMEP (event->frame_or_window))
5406 f = XFRAME (event->frame_or_window);
5407 else
5408 abort ();
5410 if (FRAME_WINDOW_P (f))
5411 fuzz = double_click_fuzz;
5412 else
5413 fuzz = double_click_fuzz / 8;
5415 is_double = (button == last_mouse_button
5416 && (abs (XINT (event->x) - last_mouse_x) <= fuzz)
5417 && (abs (XINT (event->y) - last_mouse_y) <= fuzz)
5418 && button_down_time != 0
5419 && (EQ (Vdouble_click_time, Qt)
5420 || (INTEGERP (Vdouble_click_time)
5421 && ((int)(event->timestamp - button_down_time)
5422 < XINT (Vdouble_click_time)))));
5425 last_mouse_button = button;
5426 last_mouse_x = XINT (event->x);
5427 last_mouse_y = XINT (event->y);
5429 /* If this is a button press, squirrel away the location, so
5430 we can decide later whether it was a click or a drag. */
5431 if (event->modifiers & down_modifier)
5433 if (is_double)
5435 double_click_count++;
5436 event->modifiers |= ((double_click_count > 2)
5437 ? triple_modifier
5438 : double_modifier);
5440 else
5441 double_click_count = 1;
5442 button_down_time = event->timestamp;
5443 *start_pos_ptr = Fcopy_alist (position);
5446 /* Now we're releasing a button - check the co-ordinates to
5447 see if this was a click or a drag. */
5448 else if (event->modifiers & up_modifier)
5450 /* If we did not see a down before this up, ignore the up.
5451 Probably this happened because the down event chose a
5452 menu item. It would be an annoyance to treat the
5453 release of the button that chose the menu item as a
5454 separate event. */
5456 if (!CONSP (start_pos))
5457 return Qnil;
5459 event->modifiers &= ~up_modifier;
5460 #if 0 /* Formerly we treated an up with no down as a click event. */
5461 if (!CONSP (start_pos))
5462 event->modifiers |= click_modifier;
5463 else
5464 #endif
5466 Lisp_Object down;
5467 EMACS_INT xdiff = double_click_fuzz, ydiff = double_click_fuzz;
5469 /* The third element of every position
5470 should be the (x,y) pair. */
5471 down = Fcar (Fcdr (Fcdr (start_pos)));
5472 if (CONSP (down)
5473 && INTEGERP (XCAR (down)) && INTEGERP (XCDR (down)))
5475 xdiff = XFASTINT (event->x) - XFASTINT (XCAR (down));
5476 ydiff = XFASTINT (event->y) - XFASTINT (XCDR (down));
5479 if (xdiff < double_click_fuzz && xdiff > - double_click_fuzz
5480 && ydiff < double_click_fuzz
5481 && ydiff > - double_click_fuzz)
5482 /* Mouse hasn't moved (much). */
5483 event->modifiers |= click_modifier;
5484 else
5486 button_down_time = 0;
5487 event->modifiers |= drag_modifier;
5490 /* Don't check is_double; treat this as multiple
5491 if the down-event was multiple. */
5492 if (double_click_count > 1)
5493 event->modifiers |= ((double_click_count > 2)
5494 ? triple_modifier
5495 : double_modifier);
5498 else
5499 /* Every mouse event should either have the down_modifier or
5500 the up_modifier set. */
5501 abort ();
5504 /* Get the symbol we should use for the mouse click. */
5505 Lisp_Object head;
5507 head = modify_event_symbol (button,
5508 event->modifiers,
5509 Qmouse_click, Vlispy_mouse_stem,
5510 NULL,
5511 &mouse_syms,
5512 XVECTOR (mouse_syms)->size);
5513 if (event->modifiers & drag_modifier)
5514 return Fcons (head,
5515 Fcons (start_pos,
5516 Fcons (position,
5517 Qnil)));
5518 else if (event->modifiers & (double_modifier | triple_modifier))
5519 return Fcons (head,
5520 Fcons (position,
5521 Fcons (make_number (double_click_count),
5522 Qnil)));
5523 else
5524 return Fcons (head,
5525 Fcons (position,
5526 Qnil));
5530 case WHEEL_EVENT:
5532 Lisp_Object position;
5533 Lisp_Object head;
5535 /* Build the position as appropriate for this mouse click. */
5536 struct frame *f = XFRAME (event->frame_or_window);
5538 /* Ignore wheel events that were made on frame that have been
5539 deleted. */
5540 if (! FRAME_LIVE_P (f))
5541 return Qnil;
5543 position = make_lispy_position (f, &event->x, &event->y,
5544 event->timestamp);
5546 /* Set double or triple modifiers to indicate the wheel speed. */
5548 /* On window-system frames, use the value of
5549 double-click-fuzz as is. On other frames, interpret it
5550 as a multiple of 1/8 characters. */
5551 struct frame *f;
5552 int fuzz;
5553 int is_double;
5555 if (WINDOWP (event->frame_or_window))
5556 f = XFRAME (XWINDOW (event->frame_or_window)->frame);
5557 else if (FRAMEP (event->frame_or_window))
5558 f = XFRAME (event->frame_or_window);
5559 else
5560 abort ();
5562 if (FRAME_WINDOW_P (f))
5563 fuzz = double_click_fuzz;
5564 else
5565 fuzz = double_click_fuzz / 8;
5567 is_double = (last_mouse_button < 0
5568 && (abs (XINT (event->x) - last_mouse_x) <= fuzz)
5569 && (abs (XINT (event->y) - last_mouse_y) <= fuzz)
5570 && button_down_time != 0
5571 && (EQ (Vdouble_click_time, Qt)
5572 || (INTEGERP (Vdouble_click_time)
5573 && ((int)(event->timestamp - button_down_time)
5574 < XINT (Vdouble_click_time)))));
5575 if (is_double)
5577 double_click_count++;
5578 event->modifiers |= ((double_click_count > 2)
5579 ? triple_modifier
5580 : double_modifier);
5582 else
5584 double_click_count = 1;
5585 event->modifiers |= click_modifier;
5588 button_down_time = event->timestamp;
5589 /* Use a negative value to distinguish wheel from mouse button. */
5590 last_mouse_button = -1;
5591 last_mouse_x = XINT (event->x);
5592 last_mouse_y = XINT (event->y);
5596 int symbol_num;
5598 if (event->modifiers & up_modifier)
5600 /* Emit a wheel-up event. */
5601 event->modifiers &= ~up_modifier;
5602 symbol_num = 0;
5604 else if (event->modifiers & down_modifier)
5606 /* Emit a wheel-down event. */
5607 event->modifiers &= ~down_modifier;
5608 symbol_num = 1;
5610 else
5611 /* Every wheel event should either have the down_modifier or
5612 the up_modifier set. */
5613 abort ();
5615 /* Get the symbol we should use for the wheel event. */
5616 head = modify_event_symbol (symbol_num,
5617 event->modifiers,
5618 Qmouse_click,
5619 Qnil,
5620 lispy_wheel_names,
5621 &wheel_syms,
5622 ASIZE (wheel_syms));
5625 if (event->modifiers & (double_modifier | triple_modifier))
5626 return Fcons (head,
5627 Fcons (position,
5628 Fcons (make_number (double_click_count),
5629 Qnil)));
5630 else
5631 return Fcons (head,
5632 Fcons (position,
5633 Qnil));
5637 #ifdef USE_TOOLKIT_SCROLL_BARS
5639 /* We don't have down and up events if using toolkit scroll bars,
5640 so make this always a click event. Store in the `part' of
5641 the Lisp event a symbol which maps to the following actions:
5643 `above_handle' page up
5644 `below_handle' page down
5645 `up' line up
5646 `down' line down
5647 `top' top of buffer
5648 `bottom' bottom of buffer
5649 `handle' thumb has been dragged.
5650 `end-scroll' end of interaction with scroll bar
5652 The incoming input_event contains in its `part' member an
5653 index of type `enum scroll_bar_part' which we can use as an
5654 index in scroll_bar_parts to get the appropriate symbol. */
5656 case SCROLL_BAR_CLICK_EVENT:
5658 Lisp_Object position, head, window, portion_whole, part;
5660 window = event->frame_or_window;
5661 portion_whole = Fcons (event->x, event->y);
5662 part = *scroll_bar_parts[(int) event->part];
5664 position
5665 = Fcons (window,
5666 Fcons (Qvertical_scroll_bar,
5667 Fcons (portion_whole,
5668 Fcons (make_number (event->timestamp),
5669 Fcons (part, Qnil)))));
5671 /* Always treat scroll bar events as clicks. */
5672 event->modifiers |= click_modifier;
5673 event->modifiers &= ~up_modifier;
5675 if (event->code >= ASIZE (mouse_syms))
5676 mouse_syms = larger_vector (mouse_syms, event->code + 1, Qnil);
5678 /* Get the symbol we should use for the mouse click. */
5679 head = modify_event_symbol (event->code,
5680 event->modifiers,
5681 Qmouse_click,
5682 Vlispy_mouse_stem,
5683 NULL, &mouse_syms,
5684 XVECTOR (mouse_syms)->size);
5685 return Fcons (head, Fcons (position, Qnil));
5688 #endif /* USE_TOOLKIT_SCROLL_BARS */
5690 #ifdef WINDOWSNT
5691 case W32_SCROLL_BAR_CLICK_EVENT:
5693 int button = event->code;
5694 int is_double;
5695 Lisp_Object position;
5696 Lisp_Object *start_pos_ptr;
5697 Lisp_Object start_pos;
5700 Lisp_Object window;
5701 Lisp_Object portion_whole;
5702 Lisp_Object part;
5704 window = event->frame_or_window;
5705 portion_whole = Fcons (event->x, event->y);
5706 part = *scroll_bar_parts[(int) event->part];
5708 position
5709 = Fcons (window,
5710 Fcons (Qvertical_scroll_bar,
5711 Fcons (portion_whole,
5712 Fcons (make_number (event->timestamp),
5713 Fcons (part, Qnil)))));
5716 /* Always treat W32 scroll bar events as clicks. */
5717 event->modifiers |= click_modifier;
5720 /* Get the symbol we should use for the mouse click. */
5721 Lisp_Object head;
5723 head = modify_event_symbol (button,
5724 event->modifiers,
5725 Qmouse_click,
5726 Vlispy_mouse_stem,
5727 NULL, &mouse_syms,
5728 XVECTOR (mouse_syms)->size);
5729 return Fcons (head,
5730 Fcons (position,
5731 Qnil));
5734 #endif /* WINDOWSNT */
5736 case DRAG_N_DROP_EVENT:
5738 FRAME_PTR f;
5739 Lisp_Object head, position;
5740 Lisp_Object files;
5742 /* The frame_or_window field should be a cons of the frame in
5743 which the event occurred and a list of the filenames
5744 dropped. */
5745 if (! CONSP (event->frame_or_window))
5746 abort ();
5748 f = XFRAME (XCAR (event->frame_or_window));
5749 files = XCDR (event->frame_or_window);
5751 /* Ignore mouse events that were made on frames that
5752 have been deleted. */
5753 if (! FRAME_LIVE_P (f))
5754 return Qnil;
5756 position = make_lispy_position (f, &event->x, &event->y,
5757 event->timestamp);
5759 head = modify_event_symbol (0, event->modifiers,
5760 Qdrag_n_drop, Qnil,
5761 lispy_drag_n_drop_names,
5762 &drag_n_drop_syms, 1);
5763 return Fcons (head,
5764 Fcons (position,
5765 Fcons (files,
5766 Qnil)));
5768 #endif /* HAVE_MOUSE */
5770 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
5771 || defined (USE_GTK)
5772 case MENU_BAR_EVENT:
5773 if (EQ (event->arg, event->frame_or_window))
5774 /* This is the prefix key. We translate this to
5775 `(menu_bar)' because the code in keyboard.c for menu
5776 events, which we use, relies on this. */
5777 return Fcons (Qmenu_bar, Qnil);
5778 return event->arg;
5779 #endif
5781 case SELECT_WINDOW_EVENT:
5782 /* Make an event (select-window (WINDOW)). */
5783 return Fcons (Qselect_window,
5784 Fcons (Fcons (event->frame_or_window, Qnil),
5785 Qnil));
5787 case TOOL_BAR_EVENT:
5788 if (EQ (event->arg, event->frame_or_window))
5789 /* This is the prefix key. We translate this to
5790 `(tool_bar)' because the code in keyboard.c for tool bar
5791 events, which we use, relies on this. */
5792 return Fcons (Qtool_bar, Qnil);
5793 else if (SYMBOLP (event->arg))
5794 return apply_modifiers (event->modifiers, event->arg);
5795 return event->arg;
5797 case USER_SIGNAL_EVENT:
5798 /* A user signal. */
5799 return *lispy_user_signals[event->code];
5801 case SAVE_SESSION_EVENT:
5802 return Qsave_session;
5804 /* The 'kind' field of the event is something we don't recognize. */
5805 default:
5806 abort ();
5810 #ifdef HAVE_MOUSE
5812 static Lisp_Object
5813 make_lispy_movement (frame, bar_window, part, x, y, time)
5814 FRAME_PTR frame;
5815 Lisp_Object bar_window;
5816 enum scroll_bar_part part;
5817 Lisp_Object x, y;
5818 unsigned long time;
5820 /* Is it a scroll bar movement? */
5821 if (frame && ! NILP (bar_window))
5823 Lisp_Object part_sym;
5825 part_sym = *scroll_bar_parts[(int) part];
5826 return Fcons (Qscroll_bar_movement,
5827 (Fcons (Fcons (bar_window,
5828 Fcons (Qvertical_scroll_bar,
5829 Fcons (Fcons (x, y),
5830 Fcons (make_number (time),
5831 Fcons (part_sym,
5832 Qnil))))),
5833 Qnil)));
5836 /* Or is it an ordinary mouse movement? */
5837 else
5839 Lisp_Object position;
5841 position = make_lispy_position (frame, &x, &y, time);
5843 return Fcons (Qmouse_movement,
5844 Fcons (position,
5845 Qnil));
5849 #endif /* HAVE_MOUSE */
5851 /* Construct a switch frame event. */
5852 static Lisp_Object
5853 make_lispy_switch_frame (frame)
5854 Lisp_Object frame;
5856 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
5859 /* Manipulating modifiers. */
5861 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
5863 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
5864 SYMBOL's name of the end of the modifiers; the string from this
5865 position is the unmodified symbol name.
5867 This doesn't use any caches. */
5869 static int
5870 parse_modifiers_uncached (symbol, modifier_end)
5871 Lisp_Object symbol;
5872 int *modifier_end;
5874 Lisp_Object name;
5875 int i;
5876 int modifiers;
5878 CHECK_SYMBOL (symbol);
5880 modifiers = 0;
5881 name = SYMBOL_NAME (symbol);
5883 for (i = 0; i+2 <= SBYTES (name); )
5885 int this_mod_end = 0;
5886 int this_mod = 0;
5888 /* See if the name continues with a modifier word.
5889 Check that the word appears, but don't check what follows it.
5890 Set this_mod and this_mod_end to record what we find. */
5892 switch (SREF (name, i))
5894 #define SINGLE_LETTER_MOD(BIT) \
5895 (this_mod_end = i + 1, this_mod = BIT)
5897 case 'A':
5898 SINGLE_LETTER_MOD (alt_modifier);
5899 break;
5901 case 'C':
5902 SINGLE_LETTER_MOD (ctrl_modifier);
5903 break;
5905 case 'H':
5906 SINGLE_LETTER_MOD (hyper_modifier);
5907 break;
5909 case 'M':
5910 SINGLE_LETTER_MOD (meta_modifier);
5911 break;
5913 case 'S':
5914 SINGLE_LETTER_MOD (shift_modifier);
5915 break;
5917 case 's':
5918 SINGLE_LETTER_MOD (super_modifier);
5919 break;
5921 #undef SINGLE_LETTER_MOD
5923 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
5924 if (i + LEN + 1 <= SBYTES (name) \
5925 && ! strncmp (SDATA (name) + i, NAME, LEN)) \
5927 this_mod_end = i + LEN; \
5928 this_mod = BIT; \
5931 case 'd':
5932 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
5933 MULTI_LETTER_MOD (down_modifier, "down", 4);
5934 MULTI_LETTER_MOD (double_modifier, "double", 6);
5935 break;
5937 case 't':
5938 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
5939 break;
5940 #undef MULTI_LETTER_MOD
5944 /* If we found no modifier, stop looking for them. */
5945 if (this_mod_end == 0)
5946 break;
5948 /* Check there is a dash after the modifier, so that it
5949 really is a modifier. */
5950 if (this_mod_end >= SBYTES (name)
5951 || SREF (name, this_mod_end) != '-')
5952 break;
5954 /* This modifier is real; look for another. */
5955 modifiers |= this_mod;
5956 i = this_mod_end + 1;
5959 /* Should we include the `click' modifier? */
5960 if (! (modifiers & (down_modifier | drag_modifier
5961 | double_modifier | triple_modifier))
5962 && i + 7 == SBYTES (name)
5963 && strncmp (SDATA (name) + i, "mouse-", 6) == 0
5964 && ('0' <= SREF (name, i + 6) && SREF (name, i + 6) <= '9'))
5965 modifiers |= click_modifier;
5967 if (modifier_end)
5968 *modifier_end = i;
5970 return modifiers;
5973 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
5974 prepended to the string BASE[0..BASE_LEN-1].
5975 This doesn't use any caches. */
5976 static Lisp_Object
5977 apply_modifiers_uncached (modifiers, base, base_len, base_len_byte)
5978 int modifiers;
5979 char *base;
5980 int base_len, base_len_byte;
5982 /* Since BASE could contain nulls, we can't use intern here; we have
5983 to use Fintern, which expects a genuine Lisp_String, and keeps a
5984 reference to it. */
5985 char *new_mods
5986 = (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
5987 int mod_len;
5990 char *p = new_mods;
5992 /* Only the event queue may use the `up' modifier; it should always
5993 be turned into a click or drag event before presented to lisp code. */
5994 if (modifiers & up_modifier)
5995 abort ();
5997 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
5998 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
5999 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
6000 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
6001 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
6002 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
6003 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
6004 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
6005 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
6006 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
6007 /* The click modifier is denoted by the absence of other modifiers. */
6009 *p = '\0';
6011 mod_len = p - new_mods;
6015 Lisp_Object new_name;
6017 new_name = make_uninit_multibyte_string (mod_len + base_len,
6018 mod_len + base_len_byte);
6019 bcopy (new_mods, SDATA (new_name), mod_len);
6020 bcopy (base, SDATA (new_name) + mod_len, base_len_byte);
6022 return Fintern (new_name, Qnil);
6027 static char *modifier_names[] =
6029 "up", "down", "drag", "click", "double", "triple", 0, 0,
6030 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6031 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
6033 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
6035 static Lisp_Object modifier_symbols;
6037 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
6038 static Lisp_Object
6039 lispy_modifier_list (modifiers)
6040 int modifiers;
6042 Lisp_Object modifier_list;
6043 int i;
6045 modifier_list = Qnil;
6046 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
6047 if (modifiers & (1<<i))
6048 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
6049 modifier_list);
6051 return modifier_list;
6055 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
6056 where UNMODIFIED is the unmodified form of SYMBOL,
6057 MASK is the set of modifiers present in SYMBOL's name.
6058 This is similar to parse_modifiers_uncached, but uses the cache in
6059 SYMBOL's Qevent_symbol_element_mask property, and maintains the
6060 Qevent_symbol_elements property. */
6062 Lisp_Object
6063 parse_modifiers (symbol)
6064 Lisp_Object symbol;
6066 Lisp_Object elements;
6068 elements = Fget (symbol, Qevent_symbol_element_mask);
6069 if (CONSP (elements))
6070 return elements;
6071 else
6073 int end;
6074 int modifiers = parse_modifiers_uncached (symbol, &end);
6075 Lisp_Object unmodified;
6076 Lisp_Object mask;
6078 unmodified = Fintern (make_string (SDATA (SYMBOL_NAME (symbol)) + end,
6079 SBYTES (SYMBOL_NAME (symbol)) - end),
6080 Qnil);
6082 if (modifiers & ~INTMASK)
6083 abort ();
6084 XSETFASTINT (mask, modifiers);
6085 elements = Fcons (unmodified, Fcons (mask, Qnil));
6087 /* Cache the parsing results on SYMBOL. */
6088 Fput (symbol, Qevent_symbol_element_mask,
6089 elements);
6090 Fput (symbol, Qevent_symbol_elements,
6091 Fcons (unmodified, lispy_modifier_list (modifiers)));
6093 /* Since we know that SYMBOL is modifiers applied to unmodified,
6094 it would be nice to put that in unmodified's cache.
6095 But we can't, since we're not sure that parse_modifiers is
6096 canonical. */
6098 return elements;
6102 /* Apply the modifiers MODIFIERS to the symbol BASE.
6103 BASE must be unmodified.
6105 This is like apply_modifiers_uncached, but uses BASE's
6106 Qmodifier_cache property, if present. It also builds
6107 Qevent_symbol_elements properties, since it has that info anyway.
6109 apply_modifiers copies the value of BASE's Qevent_kind property to
6110 the modified symbol. */
6111 static Lisp_Object
6112 apply_modifiers (modifiers, base)
6113 int modifiers;
6114 Lisp_Object base;
6116 Lisp_Object cache, index, entry, new_symbol;
6118 /* Mask out upper bits. We don't know where this value's been. */
6119 modifiers &= INTMASK;
6121 /* The click modifier never figures into cache indices. */
6122 cache = Fget (base, Qmodifier_cache);
6123 XSETFASTINT (index, (modifiers & ~click_modifier));
6124 entry = assq_no_quit (index, cache);
6126 if (CONSP (entry))
6127 new_symbol = XCDR (entry);
6128 else
6130 /* We have to create the symbol ourselves. */
6131 new_symbol = apply_modifiers_uncached (modifiers,
6132 SDATA (SYMBOL_NAME (base)),
6133 SCHARS (SYMBOL_NAME (base)),
6134 SBYTES (SYMBOL_NAME (base)));
6136 /* Add the new symbol to the base's cache. */
6137 entry = Fcons (index, new_symbol);
6138 Fput (base, Qmodifier_cache, Fcons (entry, cache));
6140 /* We have the parsing info now for free, so we could add it to
6141 the caches:
6142 XSETFASTINT (index, modifiers);
6143 Fput (new_symbol, Qevent_symbol_element_mask,
6144 Fcons (base, Fcons (index, Qnil)));
6145 Fput (new_symbol, Qevent_symbol_elements,
6146 Fcons (base, lispy_modifier_list (modifiers)));
6147 Sadly, this is only correct if `base' is indeed a base event,
6148 which is not necessarily the case. -stef */
6151 /* Make sure this symbol is of the same kind as BASE.
6153 You'd think we could just set this once and for all when we
6154 intern the symbol above, but reorder_modifiers may call us when
6155 BASE's property isn't set right; we can't assume that just
6156 because it has a Qmodifier_cache property it must have its
6157 Qevent_kind set right as well. */
6158 if (NILP (Fget (new_symbol, Qevent_kind)))
6160 Lisp_Object kind;
6162 kind = Fget (base, Qevent_kind);
6163 if (! NILP (kind))
6164 Fput (new_symbol, Qevent_kind, kind);
6167 return new_symbol;
6171 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
6172 return a symbol with the modifiers placed in the canonical order.
6173 Canonical order is alphabetical, except for down and drag, which
6174 always come last. The 'click' modifier is never written out.
6176 Fdefine_key calls this to make sure that (for example) C-M-foo
6177 and M-C-foo end up being equivalent in the keymap. */
6179 Lisp_Object
6180 reorder_modifiers (symbol)
6181 Lisp_Object symbol;
6183 /* It's hopefully okay to write the code this way, since everything
6184 will soon be in caches, and no consing will be done at all. */
6185 Lisp_Object parsed;
6187 parsed = parse_modifiers (symbol);
6188 return apply_modifiers ((int) XINT (XCAR (XCDR (parsed))),
6189 XCAR (parsed));
6193 /* For handling events, we often want to produce a symbol whose name
6194 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
6195 to some base, like the name of a function key or mouse button.
6196 modify_event_symbol produces symbols of this sort.
6198 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
6199 is the name of the i'th symbol. TABLE_SIZE is the number of elements
6200 in the table.
6202 Alternatively, NAME_ALIST_OR_STEM is either an alist mapping codes
6203 into symbol names, or a string specifying a name stem used to
6204 construct a symbol name or the form `STEM-N', where N is the decimal
6205 representation of SYMBOL_NUM. NAME_ALIST_OR_STEM is used if it is
6206 non-nil; otherwise NAME_TABLE is used.
6208 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
6209 persist between calls to modify_event_symbol that it can use to
6210 store a cache of the symbols it's generated for this NAME_TABLE
6211 before. The object stored there may be a vector or an alist.
6213 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
6215 MODIFIERS is a set of modifier bits (as given in struct input_events)
6216 whose prefixes should be applied to the symbol name.
6218 SYMBOL_KIND is the value to be placed in the event_kind property of
6219 the returned symbol.
6221 The symbols we create are supposed to have an
6222 `event-symbol-elements' property, which lists the modifiers present
6223 in the symbol's name. */
6225 static Lisp_Object
6226 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist_or_stem,
6227 name_table, symbol_table, table_size)
6228 int symbol_num;
6229 unsigned modifiers;
6230 Lisp_Object symbol_kind;
6231 Lisp_Object name_alist_or_stem;
6232 char **name_table;
6233 Lisp_Object *symbol_table;
6234 unsigned int table_size;
6236 Lisp_Object value;
6237 Lisp_Object symbol_int;
6239 /* Get rid of the "vendor-specific" bit here. */
6240 XSETINT (symbol_int, symbol_num & 0xffffff);
6242 /* Is this a request for a valid symbol? */
6243 if (symbol_num < 0 || symbol_num >= table_size)
6244 return Qnil;
6246 if (CONSP (*symbol_table))
6247 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
6249 /* If *symbol_table doesn't seem to be initialized properly, fix that.
6250 *symbol_table should be a lisp vector TABLE_SIZE elements long,
6251 where the Nth element is the symbol for NAME_TABLE[N], or nil if
6252 we've never used that symbol before. */
6253 else
6255 if (! VECTORP (*symbol_table)
6256 || XVECTOR (*symbol_table)->size != table_size)
6258 Lisp_Object size;
6260 XSETFASTINT (size, table_size);
6261 *symbol_table = Fmake_vector (size, Qnil);
6264 value = XVECTOR (*symbol_table)->contents[symbol_num];
6267 /* Have we already used this symbol before? */
6268 if (NILP (value))
6270 /* No; let's create it. */
6271 if (CONSP (name_alist_or_stem))
6272 value = Fcdr_safe (Fassq (symbol_int, name_alist_or_stem));
6273 else if (STRINGP (name_alist_or_stem))
6275 int len = SBYTES (name_alist_or_stem);
6276 char *buf = (char *) alloca (len + 50);
6277 sprintf (buf, "%s-%ld", SDATA (name_alist_or_stem),
6278 (long) XINT (symbol_int) + 1);
6279 value = intern (buf);
6281 else if (name_table != 0 && name_table[symbol_num])
6282 value = intern (name_table[symbol_num]);
6284 #ifdef HAVE_WINDOW_SYSTEM
6285 if (NILP (value))
6287 char *name = x_get_keysym_name (symbol_num);
6288 if (name)
6289 value = intern (name);
6291 #endif
6293 if (NILP (value))
6295 char buf[20];
6296 sprintf (buf, "key-%d", symbol_num);
6297 value = intern (buf);
6300 if (CONSP (*symbol_table))
6301 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
6302 else
6303 XVECTOR (*symbol_table)->contents[symbol_num] = value;
6305 /* Fill in the cache entries for this symbol; this also
6306 builds the Qevent_symbol_elements property, which the user
6307 cares about. */
6308 apply_modifiers (modifiers & click_modifier, value);
6309 Fput (value, Qevent_kind, symbol_kind);
6312 /* Apply modifiers to that symbol. */
6313 return apply_modifiers (modifiers, value);
6316 /* Convert a list that represents an event type,
6317 such as (ctrl meta backspace), into the usual representation of that
6318 event type as a number or a symbol. */
6320 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
6321 doc: /* Convert the event description list EVENT-DESC to an event type.
6322 EVENT-DESC should contain one base event type (a character or symbol)
6323 and zero or more modifier names (control, meta, hyper, super, shift, alt,
6324 drag, down, double or triple). The base must be last.
6325 The return value is an event type (a character or symbol) which
6326 has the same base event type and all the specified modifiers. */)
6327 (event_desc)
6328 Lisp_Object event_desc;
6330 Lisp_Object base;
6331 int modifiers = 0;
6332 Lisp_Object rest;
6334 base = Qnil;
6335 rest = event_desc;
6336 while (CONSP (rest))
6338 Lisp_Object elt;
6339 int this = 0;
6341 elt = XCAR (rest);
6342 rest = XCDR (rest);
6344 /* Given a symbol, see if it is a modifier name. */
6345 if (SYMBOLP (elt) && CONSP (rest))
6346 this = parse_solitary_modifier (elt);
6348 if (this != 0)
6349 modifiers |= this;
6350 else if (!NILP (base))
6351 error ("Two bases given in one event");
6352 else
6353 base = elt;
6357 /* Let the symbol A refer to the character A. */
6358 if (SYMBOLP (base) && SCHARS (SYMBOL_NAME (base)) == 1)
6359 XSETINT (base, SREF (SYMBOL_NAME (base), 0));
6361 if (INTEGERP (base))
6363 /* Turn (shift a) into A. */
6364 if ((modifiers & shift_modifier) != 0
6365 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
6367 XSETINT (base, XINT (base) - ('a' - 'A'));
6368 modifiers &= ~shift_modifier;
6371 /* Turn (control a) into C-a. */
6372 if (modifiers & ctrl_modifier)
6373 return make_number ((modifiers & ~ctrl_modifier)
6374 | make_ctrl_char (XINT (base)));
6375 else
6376 return make_number (modifiers | XINT (base));
6378 else if (SYMBOLP (base))
6379 return apply_modifiers (modifiers, base);
6380 else
6382 error ("Invalid base event");
6383 return Qnil;
6387 /* Try to recognize SYMBOL as a modifier name.
6388 Return the modifier flag bit, or 0 if not recognized. */
6390 static int
6391 parse_solitary_modifier (symbol)
6392 Lisp_Object symbol;
6394 Lisp_Object name = SYMBOL_NAME (symbol);
6396 switch (SREF (name, 0))
6398 #define SINGLE_LETTER_MOD(BIT) \
6399 if (SBYTES (name) == 1) \
6400 return BIT;
6402 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
6403 if (LEN == SBYTES (name) \
6404 && ! strncmp (SDATA (name), NAME, LEN)) \
6405 return BIT;
6407 case 'A':
6408 SINGLE_LETTER_MOD (alt_modifier);
6409 break;
6411 case 'a':
6412 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
6413 break;
6415 case 'C':
6416 SINGLE_LETTER_MOD (ctrl_modifier);
6417 break;
6419 case 'c':
6420 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
6421 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
6422 break;
6424 case 'H':
6425 SINGLE_LETTER_MOD (hyper_modifier);
6426 break;
6428 case 'h':
6429 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
6430 break;
6432 case 'M':
6433 SINGLE_LETTER_MOD (meta_modifier);
6434 break;
6436 case 'm':
6437 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
6438 break;
6440 case 'S':
6441 SINGLE_LETTER_MOD (shift_modifier);
6442 break;
6444 case 's':
6445 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
6446 MULTI_LETTER_MOD (super_modifier, "super", 5);
6447 SINGLE_LETTER_MOD (super_modifier);
6448 break;
6450 case 'd':
6451 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
6452 MULTI_LETTER_MOD (down_modifier, "down", 4);
6453 MULTI_LETTER_MOD (double_modifier, "double", 6);
6454 break;
6456 case 't':
6457 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
6458 break;
6460 #undef SINGLE_LETTER_MOD
6461 #undef MULTI_LETTER_MOD
6464 return 0;
6467 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
6468 Such a list is not valid as an event,
6469 but it can be a Lucid-style event type list. */
6472 lucid_event_type_list_p (object)
6473 Lisp_Object object;
6475 Lisp_Object tail;
6477 if (! CONSP (object))
6478 return 0;
6480 if (EQ (XCAR (object), Qhelp_echo)
6481 || EQ (XCAR (object), Qvertical_line)
6482 || EQ (XCAR (object), Qmode_line)
6483 || EQ (XCAR (object), Qheader_line))
6484 return 0;
6486 for (tail = object; CONSP (tail); tail = XCDR (tail))
6488 Lisp_Object elt;
6489 elt = XCAR (tail);
6490 if (! (INTEGERP (elt) || SYMBOLP (elt)))
6491 return 0;
6494 return NILP (tail);
6497 /* Store into *addr a value nonzero if terminal input chars are available.
6498 Serves the purpose of ioctl (0, FIONREAD, addr)
6499 but works even if FIONREAD does not exist.
6500 (In fact, this may actually read some input.)
6502 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe.
6503 If FILTER_EVENTS is nonzero, ignore internal events (FOCUS_IN_EVENT). */
6505 static void
6506 get_filtered_input_pending (addr, do_timers_now, filter_events)
6507 int *addr;
6508 int do_timers_now;
6509 int filter_events;
6511 /* First of all, have we already counted some input? */
6512 *addr = (!NILP (Vquit_flag)
6513 || readable_filtered_events (do_timers_now, filter_events));
6515 /* If input is being read as it arrives, and we have none, there is none. */
6516 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
6517 return;
6519 /* Try to read some input and see how much we get. */
6520 gobble_input (0);
6521 *addr = (!NILP (Vquit_flag)
6522 || readable_filtered_events (do_timers_now, filter_events));
6525 /* Store into *addr a value nonzero if terminal input chars are available.
6526 Serves the purpose of ioctl (0, FIONREAD, addr)
6527 but works even if FIONREAD does not exist.
6528 (In fact, this may actually read some input.)
6530 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe. */
6532 static void
6533 get_input_pending (addr, do_timers_now)
6534 int *addr;
6535 int do_timers_now;
6537 get_filtered_input_pending (addr, do_timers_now, 0);
6540 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
6542 void
6543 gobble_input (expected)
6544 int expected;
6546 #ifndef VMS
6547 #ifdef SIGIO
6548 if (interrupt_input)
6550 SIGMASKTYPE mask;
6551 mask = sigblock (sigmask (SIGIO));
6552 read_avail_input (expected);
6553 sigsetmask (mask);
6555 else
6556 #ifdef POLL_FOR_INPUT
6557 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
6559 SIGMASKTYPE mask;
6560 mask = sigblock (sigmask (SIGALRM));
6561 read_avail_input (expected);
6562 sigsetmask (mask);
6564 else
6565 #endif
6566 #endif
6567 read_avail_input (expected);
6568 #endif
6571 /* Put a BUFFER_SWITCH_EVENT in the buffer
6572 so that read_key_sequence will notice the new current buffer. */
6574 void
6575 record_asynch_buffer_change ()
6577 struct input_event event;
6578 Lisp_Object tem;
6579 EVENT_INIT (event);
6581 event.kind = BUFFER_SWITCH_EVENT;
6582 event.frame_or_window = Qnil;
6583 event.arg = Qnil;
6585 #ifdef subprocesses
6586 /* We don't need a buffer-switch event unless Emacs is waiting for input.
6587 The purpose of the event is to make read_key_sequence look up the
6588 keymaps again. If we aren't in read_key_sequence, we don't need one,
6589 and the event could cause trouble by messing up (input-pending-p). */
6590 tem = Fwaiting_for_user_input_p ();
6591 if (NILP (tem))
6592 return;
6593 #else
6594 /* We never need these events if we have no asynchronous subprocesses. */
6595 return;
6596 #endif
6598 /* Make sure no interrupt happens while storing the event. */
6599 #ifdef SIGIO
6600 if (interrupt_input)
6602 SIGMASKTYPE mask;
6603 mask = sigblock (sigmask (SIGIO));
6604 kbd_buffer_store_event (&event);
6605 sigsetmask (mask);
6607 else
6608 #endif
6610 stop_polling ();
6611 kbd_buffer_store_event (&event);
6612 start_polling ();
6616 #ifndef VMS
6618 /* Read any terminal input already buffered up by the system
6619 into the kbd_buffer, but do not wait.
6621 EXPECTED should be nonzero if the caller knows there is some input.
6623 Except on VMS, all input is read by this function.
6624 If interrupt_input is nonzero, this function MUST be called
6625 only when SIGIO is blocked.
6627 Returns the number of keyboard chars read, or -1 meaning
6628 this is a bad time to try to read input. */
6630 static int
6631 read_avail_input (expected)
6632 int expected;
6634 register int i;
6635 int nread = 0;
6637 if (read_socket_hook)
6639 int discard = 0;
6640 int nr;
6641 struct input_event hold_quit;
6643 EVENT_INIT (hold_quit);
6644 hold_quit.kind = NO_EVENT;
6646 /* No need for FIONREAD or fcntl; just say don't wait. */
6647 while (nr = (*read_socket_hook) (input_fd, expected, &hold_quit), nr > 0)
6649 nread += nr;
6650 expected = 0;
6652 if (hold_quit.kind != NO_EVENT)
6653 kbd_buffer_store_event (&hold_quit);
6655 else
6657 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
6658 the kbd_buffer can really hold. That may prevent loss
6659 of characters on some systems when input is stuffed at us. */
6660 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
6661 int n_to_read;
6663 /* Determine how many characters we should *try* to read. */
6664 #ifdef WINDOWSNT
6665 return 0;
6666 #else /* not WINDOWSNT */
6667 #ifdef MSDOS
6668 n_to_read = dos_keysns ();
6669 if (n_to_read == 0)
6670 return 0;
6671 #else /* not MSDOS */
6672 #ifdef FIONREAD
6673 /* Find out how much input is available. */
6674 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
6675 /* Formerly simply reported no input, but that sometimes led to
6676 a failure of Emacs to terminate.
6677 SIGHUP seems appropriate if we can't reach the terminal. */
6678 /* ??? Is it really right to send the signal just to this process
6679 rather than to the whole process group?
6680 Perhaps on systems with FIONREAD Emacs is alone in its group. */
6682 if (! noninteractive)
6683 kill (getpid (), SIGHUP);
6684 else
6685 n_to_read = 0;
6687 if (n_to_read == 0)
6688 return 0;
6689 if (n_to_read > sizeof cbuf)
6690 n_to_read = sizeof cbuf;
6691 #else /* no FIONREAD */
6692 #if defined (USG) || defined (DGUX) || defined(CYGWIN)
6693 /* Read some input if available, but don't wait. */
6694 n_to_read = sizeof cbuf;
6695 fcntl (input_fd, F_SETFL, O_NDELAY);
6696 #else
6697 you lose;
6698 #endif
6699 #endif
6700 #endif /* not MSDOS */
6701 #endif /* not WINDOWSNT */
6703 /* Now read; for one reason or another, this will not block.
6704 NREAD is set to the number of chars read. */
6707 #ifdef MSDOS
6708 cbuf[0] = dos_keyread ();
6709 nread = 1;
6710 #else
6711 nread = emacs_read (input_fd, cbuf, n_to_read);
6712 #endif
6713 /* POSIX infers that processes which are not in the session leader's
6714 process group won't get SIGHUP's at logout time. BSDI adheres to
6715 this part standard and returns -1 from read (0) with errno==EIO
6716 when the control tty is taken away.
6717 Jeffrey Honig <jch@bsdi.com> says this is generally safe. */
6718 if (nread == -1 && errno == EIO)
6719 kill (0, SIGHUP);
6720 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
6721 /* The kernel sometimes fails to deliver SIGHUP for ptys.
6722 This looks incorrect, but it isn't, because _BSD causes
6723 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
6724 and that causes a value other than 0 when there is no input. */
6725 if (nread == 0)
6726 kill (0, SIGHUP);
6727 #endif
6729 while (
6730 /* We used to retry the read if it was interrupted.
6731 But this does the wrong thing when O_NDELAY causes
6732 an EAGAIN error. Does anybody know of a situation
6733 where a retry is actually needed? */
6734 #if 0
6735 nread < 0 && (errno == EAGAIN
6736 #ifdef EFAULT
6737 || errno == EFAULT
6738 #endif
6739 #ifdef EBADSLT
6740 || errno == EBADSLT
6741 #endif
6743 #else
6745 #endif
6748 #ifndef FIONREAD
6749 #if defined (USG) || defined (DGUX) || defined (CYGWIN)
6750 fcntl (input_fd, F_SETFL, 0);
6751 #endif /* USG or DGUX or CYGWIN */
6752 #endif /* no FIONREAD */
6753 for (i = 0; i < nread; i++)
6755 struct input_event buf;
6756 EVENT_INIT (buf);
6757 buf.kind = ASCII_KEYSTROKE_EVENT;
6758 buf.modifiers = 0;
6759 if (meta_key == 1 && (cbuf[i] & 0x80))
6760 buf.modifiers = meta_modifier;
6761 if (meta_key != 2)
6762 cbuf[i] &= ~0x80;
6764 buf.code = cbuf[i];
6765 buf.frame_or_window = selected_frame;
6766 buf.arg = Qnil;
6768 kbd_buffer_store_event (&buf);
6769 /* Don't look at input that follows a C-g too closely.
6770 This reduces lossage due to autorepeat on C-g. */
6771 if (buf.kind == ASCII_KEYSTROKE_EVENT
6772 && buf.code == quit_char)
6773 break;
6777 return nread;
6779 #endif /* not VMS */
6781 void
6782 handle_async_input ()
6784 #ifdef BSD4_1
6785 extern int select_alarmed;
6786 #endif
6787 interrupt_input_pending = 0;
6789 while (1)
6791 int nread;
6792 nread = read_avail_input (1);
6793 /* -1 means it's not ok to read the input now.
6794 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
6795 0 means there was no keyboard input available. */
6796 if (nread <= 0)
6797 break;
6799 #ifdef BSD4_1
6800 select_alarmed = 1; /* Force the select emulator back to life */
6801 #endif
6805 #ifdef SIGIO /* for entire page */
6806 /* Note SIGIO has been undef'd if FIONREAD is missing. */
6808 static SIGTYPE
6809 input_available_signal (signo)
6810 int signo;
6812 /* Must preserve main program's value of errno. */
6813 int old_errno = errno;
6815 #if defined (USG) && !defined (POSIX_SIGNALS)
6816 /* USG systems forget handlers when they are used;
6817 must reestablish each time */
6818 signal (signo, input_available_signal);
6819 #endif /* USG */
6821 #ifdef BSD4_1
6822 sigisheld (SIGIO);
6823 #endif
6825 if (input_available_clear_time)
6826 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
6828 #ifdef SYNC_INPUT
6829 interrupt_input_pending = 1;
6830 #else
6831 handle_async_input ();
6832 #endif
6834 #ifdef BSD4_1
6835 sigfree ();
6836 #endif
6837 errno = old_errno;
6839 #endif /* SIGIO */
6841 /* Send ourselves a SIGIO.
6843 This function exists so that the UNBLOCK_INPUT macro in
6844 blockinput.h can have some way to take care of input we put off
6845 dealing with, without assuming that every file which uses
6846 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
6847 void
6848 reinvoke_input_signal ()
6850 #ifdef SIGIO
6851 handle_async_input ();
6852 #endif
6857 static void menu_bar_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object, void*));
6858 static Lisp_Object menu_bar_one_keymap_changed_items;
6860 /* These variables hold the vector under construction within
6861 menu_bar_items and its subroutines, and the current index
6862 for storing into that vector. */
6863 static Lisp_Object menu_bar_items_vector;
6864 static int menu_bar_items_index;
6866 /* Return a vector of menu items for a menu bar, appropriate
6867 to the current buffer. Each item has three elements in the vector:
6868 KEY STRING MAPLIST.
6870 OLD is an old vector we can optionally reuse, or nil. */
6872 Lisp_Object
6873 menu_bar_items (old)
6874 Lisp_Object old;
6876 /* The number of keymaps we're scanning right now, and the number of
6877 keymaps we have allocated space for. */
6878 int nmaps;
6880 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
6881 in the current keymaps, or nil where it is not a prefix. */
6882 Lisp_Object *maps;
6884 Lisp_Object def, tail;
6886 Lisp_Object result;
6888 int mapno;
6889 Lisp_Object oquit;
6891 int i;
6893 struct gcpro gcpro1;
6895 /* In order to build the menus, we need to call the keymap
6896 accessors. They all call QUIT. But this function is called
6897 during redisplay, during which a quit is fatal. So inhibit
6898 quitting while building the menus.
6899 We do this instead of specbind because (1) errors will clear it anyway
6900 and (2) this avoids risk of specpdl overflow. */
6901 oquit = Vinhibit_quit;
6902 Vinhibit_quit = Qt;
6904 if (!NILP (old))
6905 menu_bar_items_vector = old;
6906 else
6907 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
6908 menu_bar_items_index = 0;
6910 GCPRO1 (menu_bar_items_vector);
6912 /* Build our list of keymaps.
6913 If we recognize a function key and replace its escape sequence in
6914 keybuf with its symbol, or if the sequence starts with a mouse
6915 click and we need to switch buffers, we jump back here to rebuild
6916 the initial keymaps from the current buffer. */
6918 Lisp_Object *tmaps;
6920 /* Should overriding-terminal-local-map and overriding-local-map apply? */
6921 if (!NILP (Voverriding_local_map_menu_flag))
6923 /* Yes, use them (if non-nil) as well as the global map. */
6924 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
6925 nmaps = 0;
6926 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6927 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6928 if (!NILP (Voverriding_local_map))
6929 maps[nmaps++] = Voverriding_local_map;
6931 else
6933 /* No, so use major and minor mode keymaps and keymap property.
6934 Note that menu-bar bindings in the local-map and keymap
6935 properties may not work reliable, as they are only
6936 recognized when the menu-bar (or mode-line) is updated,
6937 which does not normally happen after every command. */
6938 Lisp_Object tem;
6939 int nminor;
6940 nminor = current_minor_maps (NULL, &tmaps);
6941 maps = (Lisp_Object *) alloca ((nminor + 3) * sizeof (maps[0]));
6942 nmaps = 0;
6943 if (tem = get_local_map (PT, current_buffer, Qkeymap), !NILP (tem))
6944 maps[nmaps++] = tem;
6945 bcopy (tmaps, (void *) (maps + nmaps), nminor * sizeof (maps[0]));
6946 nmaps += nminor;
6947 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
6949 maps[nmaps++] = current_global_map;
6952 /* Look up in each map the dummy prefix key `menu-bar'. */
6954 result = Qnil;
6956 for (mapno = nmaps - 1; mapno >= 0; mapno--)
6957 if (!NILP (maps[mapno]))
6959 def = get_keymap (access_keymap (maps[mapno], Qmenu_bar, 1, 0, 1),
6960 0, 1);
6961 if (CONSP (def))
6963 menu_bar_one_keymap_changed_items = Qnil;
6964 map_keymap (def, menu_bar_item, Qnil, NULL, 1);
6968 /* Move to the end those items that should be at the end. */
6970 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCDR (tail))
6972 int i;
6973 int end = menu_bar_items_index;
6975 for (i = 0; i < end; i += 4)
6976 if (EQ (XCAR (tail), XVECTOR (menu_bar_items_vector)->contents[i]))
6978 Lisp_Object tem0, tem1, tem2, tem3;
6979 /* Move the item at index I to the end,
6980 shifting all the others forward. */
6981 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
6982 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
6983 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
6984 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
6985 if (end > i + 4)
6986 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
6987 &XVECTOR (menu_bar_items_vector)->contents[i],
6988 (end - i - 4) * sizeof (Lisp_Object));
6989 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
6990 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
6991 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
6992 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
6993 break;
6997 /* Add nil, nil, nil, nil at the end. */
6998 i = menu_bar_items_index;
6999 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
7001 Lisp_Object tem;
7002 tem = Fmake_vector (make_number (2 * i), Qnil);
7003 bcopy (XVECTOR (menu_bar_items_vector)->contents,
7004 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
7005 menu_bar_items_vector = tem;
7007 /* Add this item. */
7008 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
7009 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
7010 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
7011 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
7012 menu_bar_items_index = i;
7014 Vinhibit_quit = oquit;
7015 UNGCPRO;
7016 return menu_bar_items_vector;
7019 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
7020 If there's already an item for KEY, add this DEF to it. */
7022 Lisp_Object item_properties;
7024 static void
7025 menu_bar_item (key, item, dummy1, dummy2)
7026 Lisp_Object key, item, dummy1;
7027 void *dummy2;
7029 struct gcpro gcpro1;
7030 int i;
7031 Lisp_Object tem;
7033 if (EQ (item, Qundefined))
7035 /* If a map has an explicit `undefined' as definition,
7036 discard any previously made menu bar item. */
7038 for (i = 0; i < menu_bar_items_index; i += 4)
7039 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
7041 if (menu_bar_items_index > i + 4)
7042 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
7043 &XVECTOR (menu_bar_items_vector)->contents[i],
7044 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
7045 menu_bar_items_index -= 4;
7049 /* If this keymap has already contributed to this KEY,
7050 don't contribute to it a second time. */
7051 tem = Fmemq (key, menu_bar_one_keymap_changed_items);
7052 if (!NILP (tem) || NILP (item))
7053 return;
7055 menu_bar_one_keymap_changed_items
7056 = Fcons (key, menu_bar_one_keymap_changed_items);
7058 /* We add to menu_bar_one_keymap_changed_items before doing the
7059 parse_menu_item, so that if it turns out it wasn't a menu item,
7060 it still correctly hides any further menu item. */
7061 GCPRO1 (key);
7062 i = parse_menu_item (item, 0, 1);
7063 UNGCPRO;
7064 if (!i)
7065 return;
7067 item = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
7069 /* Find any existing item for this KEY. */
7070 for (i = 0; i < menu_bar_items_index; i += 4)
7071 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
7072 break;
7074 /* If we did not find this KEY, add it at the end. */
7075 if (i == menu_bar_items_index)
7077 /* If vector is too small, get a bigger one. */
7078 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
7080 Lisp_Object tem;
7081 tem = Fmake_vector (make_number (2 * i), Qnil);
7082 bcopy (XVECTOR (menu_bar_items_vector)->contents,
7083 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
7084 menu_bar_items_vector = tem;
7087 /* Add this item. */
7088 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
7089 XVECTOR (menu_bar_items_vector)->contents[i++]
7090 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
7091 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (item, Qnil);
7092 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
7093 menu_bar_items_index = i;
7095 /* We did find an item for this KEY. Add ITEM to its list of maps. */
7096 else
7098 Lisp_Object old;
7099 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
7100 /* If the new and the old items are not both keymaps,
7101 the lookup will only find `item'. */
7102 item = Fcons (item, KEYMAPP (item) && KEYMAPP (XCAR (old)) ? old : Qnil);
7103 XVECTOR (menu_bar_items_vector)->contents[i + 2] = item;
7107 /* This is used as the handler when calling menu_item_eval_property. */
7108 static Lisp_Object
7109 menu_item_eval_property_1 (arg)
7110 Lisp_Object arg;
7112 /* If we got a quit from within the menu computation,
7113 quit all the way out of it. This takes care of C-] in the debugger. */
7114 if (CONSP (arg) && EQ (XCAR (arg), Qquit))
7115 Fsignal (Qquit, Qnil);
7117 return Qnil;
7120 /* Evaluate an expression and return the result (or nil if something
7121 went wrong). Used to evaluate dynamic parts of menu items. */
7122 Lisp_Object
7123 menu_item_eval_property (sexpr)
7124 Lisp_Object sexpr;
7126 int count = SPECPDL_INDEX ();
7127 Lisp_Object val;
7128 specbind (Qinhibit_redisplay, Qt);
7129 val = internal_condition_case_1 (Feval, sexpr, Qerror,
7130 menu_item_eval_property_1);
7131 return unbind_to (count, val);
7134 /* This function parses a menu item and leaves the result in the
7135 vector item_properties.
7136 ITEM is a key binding, a possible menu item.
7137 If NOTREAL is nonzero, only check for equivalent key bindings, don't
7138 evaluate dynamic expressions in the menu item.
7139 INMENUBAR is > 0 when this is considered for an entry in a menu bar
7140 top level.
7141 INMENUBAR is < 0 when this is considered for an entry in a keyboard menu.
7142 parse_menu_item returns true if the item is a menu item and false
7143 otherwise. */
7146 parse_menu_item (item, notreal, inmenubar)
7147 Lisp_Object item;
7148 int notreal, inmenubar;
7150 Lisp_Object def, tem, item_string, start;
7151 Lisp_Object cachelist;
7152 Lisp_Object filter;
7153 Lisp_Object keyhint;
7154 int i;
7155 int newcache = 0;
7157 cachelist = Qnil;
7158 filter = Qnil;
7159 keyhint = Qnil;
7161 if (!CONSP (item))
7162 return 0;
7164 /* Create item_properties vector if necessary. */
7165 if (NILP (item_properties))
7166 item_properties
7167 = Fmake_vector (make_number (ITEM_PROPERTY_ENABLE + 1), Qnil);
7169 /* Initialize optional entries. */
7170 for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++)
7171 AREF (item_properties, i) = Qnil;
7172 AREF (item_properties, ITEM_PROPERTY_ENABLE) = Qt;
7174 /* Save the item here to protect it from GC. */
7175 AREF (item_properties, ITEM_PROPERTY_ITEM) = item;
7177 item_string = XCAR (item);
7179 start = item;
7180 item = XCDR (item);
7181 if (STRINGP (item_string))
7183 /* Old format menu item. */
7184 AREF (item_properties, ITEM_PROPERTY_NAME) = item_string;
7186 /* Maybe help string. */
7187 if (CONSP (item) && STRINGP (XCAR (item)))
7189 AREF (item_properties, ITEM_PROPERTY_HELP) = XCAR (item);
7190 start = item;
7191 item = XCDR (item);
7194 /* Maybe key binding cache. */
7195 if (CONSP (item) && CONSP (XCAR (item))
7196 && (NILP (XCAR (XCAR (item)))
7197 || VECTORP (XCAR (XCAR (item)))))
7199 cachelist = XCAR (item);
7200 item = XCDR (item);
7203 /* This is the real definition--the function to run. */
7204 AREF (item_properties, ITEM_PROPERTY_DEF) = item;
7206 /* Get enable property, if any. */
7207 if (SYMBOLP (item))
7209 tem = Fget (item, Qmenu_enable);
7210 if (!NILP (tem))
7211 AREF (item_properties, ITEM_PROPERTY_ENABLE) = tem;
7214 else if (EQ (item_string, Qmenu_item) && CONSP (item))
7216 /* New format menu item. */
7217 AREF (item_properties, ITEM_PROPERTY_NAME) = XCAR (item);
7218 start = XCDR (item);
7219 if (CONSP (start))
7221 /* We have a real binding. */
7222 AREF (item_properties, ITEM_PROPERTY_DEF) = XCAR (start);
7224 item = XCDR (start);
7225 /* Is there a cache list with key equivalences. */
7226 if (CONSP (item) && CONSP (XCAR (item)))
7228 cachelist = XCAR (item);
7229 item = XCDR (item);
7232 /* Parse properties. */
7233 while (CONSP (item) && CONSP (XCDR (item)))
7235 tem = XCAR (item);
7236 item = XCDR (item);
7238 if (EQ (tem, QCenable))
7239 AREF (item_properties, ITEM_PROPERTY_ENABLE) = XCAR (item);
7240 else if (EQ (tem, QCvisible) && !notreal)
7242 /* If got a visible property and that evaluates to nil
7243 then ignore this item. */
7244 tem = menu_item_eval_property (XCAR (item));
7245 if (NILP (tem))
7246 return 0;
7248 else if (EQ (tem, QChelp))
7249 AREF (item_properties, ITEM_PROPERTY_HELP) = XCAR (item);
7250 else if (EQ (tem, QCfilter))
7251 filter = item;
7252 else if (EQ (tem, QCkey_sequence))
7254 tem = XCAR (item);
7255 if (NILP (cachelist)
7256 && (SYMBOLP (tem) || STRINGP (tem) || VECTORP (tem)))
7257 /* Be GC protected. Set keyhint to item instead of tem. */
7258 keyhint = item;
7260 else if (EQ (tem, QCkeys))
7262 tem = XCAR (item);
7263 if (CONSP (tem) || (STRINGP (tem) && NILP (cachelist)))
7264 AREF (item_properties, ITEM_PROPERTY_KEYEQ) = tem;
7266 else if (EQ (tem, QCbutton) && CONSP (XCAR (item)))
7268 Lisp_Object type;
7269 tem = XCAR (item);
7270 type = XCAR (tem);
7271 if (EQ (type, QCtoggle) || EQ (type, QCradio))
7273 AREF (item_properties, ITEM_PROPERTY_SELECTED)
7274 = XCDR (tem);
7275 AREF (item_properties, ITEM_PROPERTY_TYPE)
7276 = type;
7279 item = XCDR (item);
7282 else if (inmenubar || !NILP (start))
7283 return 0;
7285 else
7286 return 0; /* not a menu item */
7288 /* If item string is not a string, evaluate it to get string.
7289 If we don't get a string, skip this item. */
7290 item_string = AREF (item_properties, ITEM_PROPERTY_NAME);
7291 if (!(STRINGP (item_string) || notreal))
7293 item_string = menu_item_eval_property (item_string);
7294 if (!STRINGP (item_string))
7295 return 0;
7296 AREF (item_properties, ITEM_PROPERTY_NAME) = item_string;
7299 /* If got a filter apply it on definition. */
7300 def = AREF (item_properties, ITEM_PROPERTY_DEF);
7301 if (!NILP (filter))
7303 def = menu_item_eval_property (list2 (XCAR (filter),
7304 list2 (Qquote, def)));
7306 AREF (item_properties, ITEM_PROPERTY_DEF) = def;
7309 /* Enable or disable selection of item. */
7310 tem = AREF (item_properties, ITEM_PROPERTY_ENABLE);
7311 if (!EQ (tem, Qt))
7313 if (notreal)
7314 tem = Qt;
7315 else
7316 tem = menu_item_eval_property (tem);
7317 if (inmenubar && NILP (tem))
7318 return 0; /* Ignore disabled items in menu bar. */
7319 AREF (item_properties, ITEM_PROPERTY_ENABLE) = tem;
7322 /* If we got no definition, this item is just unselectable text which
7323 is OK in a submenu but not in the menubar. */
7324 if (NILP (def))
7325 return (inmenubar ? 0 : 1);
7327 /* See if this is a separate pane or a submenu. */
7328 def = AREF (item_properties, ITEM_PROPERTY_DEF);
7329 tem = get_keymap (def, 0, 1);
7330 /* For a subkeymap, just record its details and exit. */
7331 if (CONSP (tem))
7333 AREF (item_properties, ITEM_PROPERTY_MAP) = tem;
7334 AREF (item_properties, ITEM_PROPERTY_DEF) = tem;
7335 return 1;
7338 /* At the top level in the menu bar, do likewise for commands also.
7339 The menu bar does not display equivalent key bindings anyway.
7340 ITEM_PROPERTY_DEF is already set up properly. */
7341 if (inmenubar > 0)
7342 return 1;
7344 /* This is a command. See if there is an equivalent key binding. */
7345 if (NILP (cachelist))
7347 /* We have to create a cachelist. */
7348 CHECK_IMPURE (start);
7349 XSETCDR (start, Fcons (Fcons (Qnil, Qnil), XCDR (start)));
7350 cachelist = XCAR (XCDR (start));
7351 newcache = 1;
7352 tem = AREF (item_properties, ITEM_PROPERTY_KEYEQ);
7353 if (!NILP (keyhint))
7355 XSETCAR (cachelist, XCAR (keyhint));
7356 newcache = 0;
7358 else if (STRINGP (tem))
7360 XSETCDR (cachelist, Fsubstitute_command_keys (tem));
7361 XSETCAR (cachelist, Qt);
7365 tem = XCAR (cachelist);
7366 if (!EQ (tem, Qt))
7368 int chkcache = 0;
7369 Lisp_Object prefix;
7371 if (!NILP (tem))
7372 tem = Fkey_binding (tem, Qnil, Qnil);
7374 prefix = AREF (item_properties, ITEM_PROPERTY_KEYEQ);
7375 if (CONSP (prefix))
7377 def = XCAR (prefix);
7378 prefix = XCDR (prefix);
7380 else
7381 def = AREF (item_properties, ITEM_PROPERTY_DEF);
7383 if (!update_menu_bindings)
7384 chkcache = 0;
7385 else if (NILP (XCAR (cachelist))) /* Have no saved key. */
7387 if (newcache /* Always check first time. */
7388 /* Should we check everything when precomputing key
7389 bindings? */
7390 /* If something had no key binding before, don't recheck it
7391 because that is too slow--except if we have a list of
7392 rebound commands in Vdefine_key_rebound_commands, do
7393 recheck any command that appears in that list. */
7394 || (CONSP (Vdefine_key_rebound_commands)
7395 && !NILP (Fmemq (def, Vdefine_key_rebound_commands))))
7396 chkcache = 1;
7398 /* We had a saved key. Is it still bound to the command? */
7399 else if (NILP (tem)
7400 || (!EQ (tem, def)
7401 /* If the command is an alias for another
7402 (such as lmenu.el set it up), check if the
7403 original command matches the cached command. */
7404 && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function))))
7405 chkcache = 1; /* Need to recompute key binding. */
7407 if (chkcache)
7409 /* Recompute equivalent key binding. If the command is an alias
7410 for another (such as lmenu.el set it up), see if the original
7411 command name has equivalent keys. Otherwise look up the
7412 specified command itself. We don't try both, because that
7413 makes lmenu menus slow. */
7414 if (SYMBOLP (def)
7415 && SYMBOLP (XSYMBOL (def)->function)
7416 && ! NILP (Fget (def, Qmenu_alias)))
7417 def = XSYMBOL (def)->function;
7418 tem = Fwhere_is_internal (def, Qnil, Qt, Qnil, Qt);
7419 XSETCAR (cachelist, tem);
7420 if (NILP (tem))
7422 XSETCDR (cachelist, Qnil);
7423 chkcache = 0;
7426 else if (!NILP (keyhint) && !NILP (XCAR (cachelist)))
7428 tem = XCAR (cachelist);
7429 chkcache = 1;
7432 newcache = chkcache;
7433 if (chkcache)
7435 tem = Fkey_description (tem, Qnil);
7436 if (CONSP (prefix))
7438 if (STRINGP (XCAR (prefix)))
7439 tem = concat2 (XCAR (prefix), tem);
7440 if (STRINGP (XCDR (prefix)))
7441 tem = concat2 (tem, XCDR (prefix));
7443 XSETCDR (cachelist, tem);
7447 tem = XCDR (cachelist);
7448 if (newcache && !NILP (tem))
7450 tem = concat3 (build_string (" ("), tem, build_string (")"));
7451 XSETCDR (cachelist, tem);
7454 /* If we only want to precompute equivalent key bindings, stop here. */
7455 if (notreal)
7456 return 1;
7458 /* If we have an equivalent key binding, use that. */
7459 AREF (item_properties, ITEM_PROPERTY_KEYEQ) = tem;
7461 /* Include this when menu help is implemented.
7462 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP];
7463 if (!(NILP (tem) || STRINGP (tem)))
7465 tem = menu_item_eval_property (tem);
7466 if (!STRINGP (tem))
7467 tem = Qnil;
7468 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP] = tem;
7472 /* Handle radio buttons or toggle boxes. */
7473 tem = AREF (item_properties, ITEM_PROPERTY_SELECTED);
7474 if (!NILP (tem))
7475 AREF (item_properties, ITEM_PROPERTY_SELECTED)
7476 = menu_item_eval_property (tem);
7478 return 1;
7483 /***********************************************************************
7484 Tool-bars
7485 ***********************************************************************/
7487 /* A vector holding tool bar items while they are parsed in function
7488 tool_bar_items. Each item occupies TOOL_BAR_ITEM_NSCLOTS elements
7489 in the vector. */
7491 static Lisp_Object tool_bar_items_vector;
7493 /* A vector holding the result of parse_tool_bar_item. Layout is like
7494 the one for a single item in tool_bar_items_vector. */
7496 static Lisp_Object tool_bar_item_properties;
7498 /* Next free index in tool_bar_items_vector. */
7500 static int ntool_bar_items;
7502 /* The symbols `tool-bar', and `:image'. */
7504 extern Lisp_Object Qtool_bar;
7505 Lisp_Object QCimage;
7507 /* Function prototypes. */
7509 static void init_tool_bar_items P_ ((Lisp_Object));
7510 static void process_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
7511 static int parse_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
7512 static void append_tool_bar_item P_ ((void));
7515 /* Return a vector of tool bar items for keymaps currently in effect.
7516 Reuse vector REUSE if non-nil. Return in *NITEMS the number of
7517 tool bar items found. */
7519 Lisp_Object
7520 tool_bar_items (reuse, nitems)
7521 Lisp_Object reuse;
7522 int *nitems;
7524 Lisp_Object *maps;
7525 int nmaps, i;
7526 Lisp_Object oquit;
7527 Lisp_Object *tmaps;
7529 *nitems = 0;
7531 /* In order to build the menus, we need to call the keymap
7532 accessors. They all call QUIT. But this function is called
7533 during redisplay, during which a quit is fatal. So inhibit
7534 quitting while building the menus. We do this instead of
7535 specbind because (1) errors will clear it anyway and (2) this
7536 avoids risk of specpdl overflow. */
7537 oquit = Vinhibit_quit;
7538 Vinhibit_quit = Qt;
7540 /* Initialize tool_bar_items_vector and protect it from GC. */
7541 init_tool_bar_items (reuse);
7543 /* Build list of keymaps in maps. Set nmaps to the number of maps
7544 to process. */
7546 /* Should overriding-terminal-local-map and overriding-local-map apply? */
7547 if (!NILP (Voverriding_local_map_menu_flag))
7549 /* Yes, use them (if non-nil) as well as the global map. */
7550 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
7551 nmaps = 0;
7552 if (!NILP (current_kboard->Voverriding_terminal_local_map))
7553 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
7554 if (!NILP (Voverriding_local_map))
7555 maps[nmaps++] = Voverriding_local_map;
7557 else
7559 /* No, so use major and minor mode keymaps and keymap property.
7560 Note that tool-bar bindings in the local-map and keymap
7561 properties may not work reliable, as they are only
7562 recognized when the tool-bar (or mode-line) is updated,
7563 which does not normally happen after every command. */
7564 Lisp_Object tem;
7565 int nminor;
7566 nminor = current_minor_maps (NULL, &tmaps);
7567 maps = (Lisp_Object *) alloca ((nminor + 3) * sizeof (maps[0]));
7568 nmaps = 0;
7569 if (tem = get_local_map (PT, current_buffer, Qkeymap), !NILP (tem))
7570 maps[nmaps++] = tem;
7571 bcopy (tmaps, (void *) (maps + nmaps), nminor * sizeof (maps[0]));
7572 nmaps += nminor;
7573 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
7576 /* Add global keymap at the end. */
7577 maps[nmaps++] = current_global_map;
7579 /* Process maps in reverse order and look up in each map the prefix
7580 key `tool-bar'. */
7581 for (i = nmaps - 1; i >= 0; --i)
7582 if (!NILP (maps[i]))
7584 Lisp_Object keymap;
7586 keymap = get_keymap (access_keymap (maps[i], Qtool_bar, 1, 0, 1), 0, 1);
7587 if (CONSP (keymap))
7589 Lisp_Object tail;
7591 /* KEYMAP is a list `(keymap (KEY . BINDING) ...)'. */
7592 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
7594 Lisp_Object keydef = XCAR (tail);
7595 if (CONSP (keydef))
7596 process_tool_bar_item (XCAR (keydef), XCDR (keydef));
7601 Vinhibit_quit = oquit;
7602 *nitems = ntool_bar_items / TOOL_BAR_ITEM_NSLOTS;
7603 return tool_bar_items_vector;
7607 /* Process the definition of KEY which is DEF. */
7609 static void
7610 process_tool_bar_item (key, def)
7611 Lisp_Object key, def;
7613 int i;
7614 extern Lisp_Object Qundefined;
7615 struct gcpro gcpro1, gcpro2;
7617 /* Protect KEY and DEF from GC because parse_tool_bar_item may call
7618 eval. */
7619 GCPRO2 (key, def);
7621 if (EQ (def, Qundefined))
7623 /* If a map has an explicit `undefined' as definition,
7624 discard any previously made item. */
7625 for (i = 0; i < ntool_bar_items; i += TOOL_BAR_ITEM_NSLOTS)
7627 Lisp_Object *v = XVECTOR (tool_bar_items_vector)->contents + i;
7629 if (EQ (key, v[TOOL_BAR_ITEM_KEY]))
7631 if (ntool_bar_items > i + TOOL_BAR_ITEM_NSLOTS)
7632 bcopy (v + TOOL_BAR_ITEM_NSLOTS, v,
7633 ((ntool_bar_items - i - TOOL_BAR_ITEM_NSLOTS)
7634 * sizeof (Lisp_Object)));
7635 ntool_bar_items -= TOOL_BAR_ITEM_NSLOTS;
7636 break;
7640 else if (parse_tool_bar_item (key, def))
7641 /* Append a new tool bar item to tool_bar_items_vector. Accept
7642 more than one definition for the same key. */
7643 append_tool_bar_item ();
7645 UNGCPRO;
7649 /* Parse a tool bar item specification ITEM for key KEY and return the
7650 result in tool_bar_item_properties. Value is zero if ITEM is
7651 invalid.
7653 ITEM is a list `(menu-item CAPTION BINDING PROPS...)'.
7655 CAPTION is the caption of the item, If it's not a string, it is
7656 evaluated to get a string.
7658 BINDING is the tool bar item's binding. Tool-bar items with keymaps
7659 as binding are currently ignored.
7661 The following properties are recognized:
7663 - `:enable FORM'.
7665 FORM is evaluated and specifies whether the tool bar item is
7666 enabled or disabled.
7668 - `:visible FORM'
7670 FORM is evaluated and specifies whether the tool bar item is visible.
7672 - `:filter FUNCTION'
7674 FUNCTION is invoked with one parameter `(quote BINDING)'. Its
7675 result is stored as the new binding.
7677 - `:button (TYPE SELECTED)'
7679 TYPE must be one of `:radio' or `:toggle'. SELECTED is evaluated
7680 and specifies whether the button is selected (pressed) or not.
7682 - `:image IMAGES'
7684 IMAGES is either a single image specification or a vector of four
7685 image specifications. See enum tool_bar_item_images.
7687 - `:help HELP-STRING'.
7689 Gives a help string to display for the tool bar item. */
7691 static int
7692 parse_tool_bar_item (key, item)
7693 Lisp_Object key, item;
7695 /* Access slot with index IDX of vector tool_bar_item_properties. */
7696 #define PROP(IDX) XVECTOR (tool_bar_item_properties)->contents[IDX]
7698 Lisp_Object filter = Qnil;
7699 Lisp_Object caption;
7700 int i;
7702 /* Defininition looks like `(menu-item CAPTION BINDING PROPS...)'.
7703 Rule out items that aren't lists, don't start with
7704 `menu-item' or whose rest following `tool-bar-item' is not a
7705 list. */
7706 if (!CONSP (item)
7707 || !EQ (XCAR (item), Qmenu_item)
7708 || (item = XCDR (item),
7709 !CONSP (item)))
7710 return 0;
7712 /* Create tool_bar_item_properties vector if necessary. Reset it to
7713 defaults. */
7714 if (VECTORP (tool_bar_item_properties))
7716 for (i = 0; i < TOOL_BAR_ITEM_NSLOTS; ++i)
7717 PROP (i) = Qnil;
7719 else
7720 tool_bar_item_properties
7721 = Fmake_vector (make_number (TOOL_BAR_ITEM_NSLOTS), Qnil);
7723 /* Set defaults. */
7724 PROP (TOOL_BAR_ITEM_KEY) = key;
7725 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
7727 /* Get the caption of the item. If the caption is not a string,
7728 evaluate it to get a string. If we don't get a string, skip this
7729 item. */
7730 caption = XCAR (item);
7731 if (!STRINGP (caption))
7733 caption = menu_item_eval_property (caption);
7734 if (!STRINGP (caption))
7735 return 0;
7737 PROP (TOOL_BAR_ITEM_CAPTION) = caption;
7739 /* Give up if rest following the caption is not a list. */
7740 item = XCDR (item);
7741 if (!CONSP (item))
7742 return 0;
7744 /* Store the binding. */
7745 PROP (TOOL_BAR_ITEM_BINDING) = XCAR (item);
7746 item = XCDR (item);
7748 /* Ignore cached key binding, if any. */
7749 if (CONSP (item) && CONSP (XCAR (item)))
7750 item = XCDR (item);
7752 /* Process the rest of the properties. */
7753 for (; CONSP (item) && CONSP (XCDR (item)); item = XCDR (XCDR (item)))
7755 Lisp_Object key, value;
7757 key = XCAR (item);
7758 value = XCAR (XCDR (item));
7760 if (EQ (key, QCenable))
7761 /* `:enable FORM'. */
7762 PROP (TOOL_BAR_ITEM_ENABLED_P) = value;
7763 else if (EQ (key, QCvisible))
7765 /* `:visible FORM'. If got a visible property and that
7766 evaluates to nil then ignore this item. */
7767 if (NILP (menu_item_eval_property (value)))
7768 return 0;
7770 else if (EQ (key, QChelp))
7771 /* `:help HELP-STRING'. */
7772 PROP (TOOL_BAR_ITEM_HELP) = value;
7773 else if (EQ (key, QCfilter))
7774 /* ':filter FORM'. */
7775 filter = value;
7776 else if (EQ (key, QCbutton) && CONSP (value))
7778 /* `:button (TYPE . SELECTED)'. */
7779 Lisp_Object type, selected;
7781 type = XCAR (value);
7782 selected = XCDR (value);
7783 if (EQ (type, QCtoggle) || EQ (type, QCradio))
7785 PROP (TOOL_BAR_ITEM_SELECTED_P) = selected;
7786 PROP (TOOL_BAR_ITEM_TYPE) = type;
7789 else if (EQ (key, QCimage)
7790 && (CONSP (value)
7791 || (VECTORP (value) && XVECTOR (value)->size == 4)))
7792 /* Value is either a single image specification or a vector
7793 of 4 such specifications for the different button states. */
7794 PROP (TOOL_BAR_ITEM_IMAGES) = value;
7797 /* If got a filter apply it on binding. */
7798 if (!NILP (filter))
7799 PROP (TOOL_BAR_ITEM_BINDING)
7800 = menu_item_eval_property (list2 (filter,
7801 list2 (Qquote,
7802 PROP (TOOL_BAR_ITEM_BINDING))));
7804 /* See if the binding is a keymap. Give up if it is. */
7805 if (CONSP (get_keymap (PROP (TOOL_BAR_ITEM_BINDING), 0, 1)))
7806 return 0;
7808 /* Enable or disable selection of item. */
7809 if (!EQ (PROP (TOOL_BAR_ITEM_ENABLED_P), Qt))
7810 PROP (TOOL_BAR_ITEM_ENABLED_P)
7811 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_ENABLED_P));
7813 /* Handle radio buttons or toggle boxes. */
7814 if (!NILP (PROP (TOOL_BAR_ITEM_SELECTED_P)))
7815 PROP (TOOL_BAR_ITEM_SELECTED_P)
7816 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_SELECTED_P));
7818 return 1;
7820 #undef PROP
7824 /* Initialize tool_bar_items_vector. REUSE, if non-nil, is a vector
7825 that can be reused. */
7827 static void
7828 init_tool_bar_items (reuse)
7829 Lisp_Object reuse;
7831 if (VECTORP (reuse))
7832 tool_bar_items_vector = reuse;
7833 else
7834 tool_bar_items_vector = Fmake_vector (make_number (64), Qnil);
7835 ntool_bar_items = 0;
7839 /* Append parsed tool bar item properties from
7840 tool_bar_item_properties */
7842 static void
7843 append_tool_bar_item ()
7845 Lisp_Object *to, *from;
7847 /* Enlarge tool_bar_items_vector if necessary. */
7848 if (ntool_bar_items + TOOL_BAR_ITEM_NSLOTS
7849 >= XVECTOR (tool_bar_items_vector)->size)
7851 Lisp_Object new_vector;
7852 int old_size = XVECTOR (tool_bar_items_vector)->size;
7854 new_vector = Fmake_vector (make_number (2 * old_size), Qnil);
7855 bcopy (XVECTOR (tool_bar_items_vector)->contents,
7856 XVECTOR (new_vector)->contents,
7857 old_size * sizeof (Lisp_Object));
7858 tool_bar_items_vector = new_vector;
7861 /* Append entries from tool_bar_item_properties to the end of
7862 tool_bar_items_vector. */
7863 to = XVECTOR (tool_bar_items_vector)->contents + ntool_bar_items;
7864 from = XVECTOR (tool_bar_item_properties)->contents;
7865 bcopy (from, to, TOOL_BAR_ITEM_NSLOTS * sizeof *to);
7866 ntool_bar_items += TOOL_BAR_ITEM_NSLOTS;
7873 /* Read a character using menus based on maps in the array MAPS.
7874 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
7875 Return t if we displayed a menu but the user rejected it.
7877 PREV_EVENT is the previous input event, or nil if we are reading
7878 the first event of a key sequence.
7880 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
7881 if we used a mouse menu to read the input, or zero otherwise. If
7882 USED_MOUSE_MENU is null, we don't dereference it.
7884 The prompting is done based on the prompt-string of the map
7885 and the strings associated with various map elements.
7887 This can be done with X menus or with menus put in the minibuf.
7888 These are done in different ways, depending on how the input will be read.
7889 Menus using X are done after auto-saving in read-char, getting the input
7890 event from Fx_popup_menu; menus using the minibuf use read_char recursively
7891 and do auto-saving in the inner call of read_char. */
7893 static Lisp_Object
7894 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
7895 int nmaps;
7896 Lisp_Object *maps;
7897 Lisp_Object prev_event;
7898 int *used_mouse_menu;
7900 int mapno;
7901 register Lisp_Object name = Qnil;
7903 if (used_mouse_menu)
7904 *used_mouse_menu = 0;
7906 /* Use local over global Menu maps */
7908 if (! menu_prompting)
7909 return Qnil;
7911 /* Optionally disregard all but the global map. */
7912 if (inhibit_local_menu_bar_menus)
7914 maps += (nmaps - 1);
7915 nmaps = 1;
7918 /* Get the menu name from the first map that has one (a prompt string). */
7919 for (mapno = 0; mapno < nmaps; mapno++)
7921 name = Fkeymap_prompt (maps[mapno]);
7922 if (!NILP (name))
7923 break;
7926 /* If we don't have any menus, just read a character normally. */
7927 if (!STRINGP (name))
7928 return Qnil;
7930 #ifdef HAVE_MENUS
7931 /* If we got to this point via a mouse click,
7932 use a real menu for mouse selection. */
7933 if (EVENT_HAS_PARAMETERS (prev_event)
7934 && !EQ (XCAR (prev_event), Qmenu_bar)
7935 && !EQ (XCAR (prev_event), Qtool_bar))
7937 /* Display the menu and get the selection. */
7938 Lisp_Object *realmaps
7939 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
7940 Lisp_Object value;
7941 int nmaps1 = 0;
7943 /* Use the maps that are not nil. */
7944 for (mapno = 0; mapno < nmaps; mapno++)
7945 if (!NILP (maps[mapno]))
7946 realmaps[nmaps1++] = maps[mapno];
7948 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
7949 if (CONSP (value))
7951 Lisp_Object tem;
7953 record_menu_key (XCAR (value));
7955 /* If we got multiple events, unread all but
7956 the first.
7957 There is no way to prevent those unread events
7958 from showing up later in last_nonmenu_event.
7959 So turn symbol and integer events into lists,
7960 to indicate that they came from a mouse menu,
7961 so that when present in last_nonmenu_event
7962 they won't confuse things. */
7963 for (tem = XCDR (value); !NILP (tem); tem = XCDR (tem))
7965 record_menu_key (XCAR (tem));
7966 if (SYMBOLP (XCAR (tem))
7967 || INTEGERP (XCAR (tem)))
7968 XSETCAR (tem, Fcons (XCAR (tem), Qdisabled));
7971 /* If we got more than one event, put all but the first
7972 onto this list to be read later.
7973 Return just the first event now. */
7974 Vunread_command_events
7975 = nconc2 (XCDR (value), Vunread_command_events);
7976 value = XCAR (value);
7978 else if (NILP (value))
7979 value = Qt;
7980 if (used_mouse_menu)
7981 *used_mouse_menu = 1;
7982 return value;
7984 #endif /* HAVE_MENUS */
7985 return Qnil ;
7988 /* Buffer in use so far for the minibuf prompts for menu keymaps.
7989 We make this bigger when necessary, and never free it. */
7990 static char *read_char_minibuf_menu_text;
7991 /* Size of that buffer. */
7992 static int read_char_minibuf_menu_width;
7994 static Lisp_Object
7995 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
7996 int commandflag ;
7997 int nmaps;
7998 Lisp_Object *maps;
8000 int mapno;
8001 register Lisp_Object name;
8002 int nlength;
8003 /* FIXME: Use the minibuffer's frame width. */
8004 int width = FRAME_COLS (SELECTED_FRAME ()) - 4;
8005 int idx = -1;
8006 int nobindings = 1;
8007 Lisp_Object rest, vector;
8008 char *menu;
8010 vector = Qnil;
8011 name = Qnil;
8013 if (! menu_prompting)
8014 return Qnil;
8016 /* Make sure we have a big enough buffer for the menu text. */
8017 if (read_char_minibuf_menu_text == 0)
8019 read_char_minibuf_menu_width = width + 4;
8020 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
8022 else if (width + 4 > read_char_minibuf_menu_width)
8024 read_char_minibuf_menu_width = width + 4;
8025 read_char_minibuf_menu_text
8026 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
8028 menu = read_char_minibuf_menu_text;
8030 /* Get the menu name from the first map that has one (a prompt string). */
8031 for (mapno = 0; mapno < nmaps; mapno++)
8033 name = Fkeymap_prompt (maps[mapno]);
8034 if (!NILP (name))
8035 break;
8038 /* If we don't have any menus, just read a character normally. */
8039 if (!STRINGP (name))
8040 return Qnil;
8042 /* Prompt string always starts with map's prompt, and a space. */
8043 strcpy (menu, SDATA (name));
8044 nlength = SBYTES (name);
8045 menu[nlength++] = ':';
8046 menu[nlength++] = ' ';
8047 menu[nlength] = 0;
8049 /* Start prompting at start of first map. */
8050 mapno = 0;
8051 rest = maps[mapno];
8053 /* Present the documented bindings, a line at a time. */
8054 while (1)
8056 int notfirst = 0;
8057 int i = nlength;
8058 Lisp_Object obj;
8059 int ch;
8060 Lisp_Object orig_defn_macro;
8062 /* Loop over elements of map. */
8063 while (i < width)
8065 Lisp_Object elt;
8067 /* If reached end of map, start at beginning of next map. */
8068 if (NILP (rest))
8070 mapno++;
8071 /* At end of last map, wrap around to first map if just starting,
8072 or end this line if already have something on it. */
8073 if (mapno == nmaps)
8075 mapno = 0;
8076 if (notfirst || nobindings) break;
8078 rest = maps[mapno];
8081 /* Look at the next element of the map. */
8082 if (idx >= 0)
8083 elt = XVECTOR (vector)->contents[idx];
8084 else
8085 elt = Fcar_safe (rest);
8087 if (idx < 0 && VECTORP (elt))
8089 /* If we found a dense table in the keymap,
8090 advanced past it, but start scanning its contents. */
8091 rest = Fcdr_safe (rest);
8092 vector = elt;
8093 idx = 0;
8095 else
8097 /* An ordinary element. */
8098 Lisp_Object event, tem;
8100 if (idx < 0)
8102 event = Fcar_safe (elt); /* alist */
8103 elt = Fcdr_safe (elt);
8105 else
8107 XSETINT (event, idx); /* vector */
8110 /* Ignore the element if it has no prompt string. */
8111 if (INTEGERP (event) && parse_menu_item (elt, 0, -1))
8113 /* 1 if the char to type matches the string. */
8114 int char_matches;
8115 Lisp_Object upcased_event, downcased_event;
8116 Lisp_Object desc = Qnil;
8117 Lisp_Object s
8118 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
8120 upcased_event = Fupcase (event);
8121 downcased_event = Fdowncase (event);
8122 char_matches = (XINT (upcased_event) == SREF (s, 0)
8123 || XINT (downcased_event) == SREF (s, 0));
8124 if (! char_matches)
8125 desc = Fsingle_key_description (event, Qnil);
8127 #if 0 /* It is redundant to list the equivalent key bindings because
8128 the prefix is what the user has already typed. */
8130 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
8131 if (!NILP (tem))
8132 /* Insert equivalent keybinding. */
8133 s = concat2 (s, tem);
8134 #endif
8136 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
8137 if (EQ (tem, QCradio) || EQ (tem, QCtoggle))
8139 /* Insert button prefix. */
8140 Lisp_Object selected
8141 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
8142 if (EQ (tem, QCradio))
8143 tem = build_string (NILP (selected) ? "(*) " : "( ) ");
8144 else
8145 tem = build_string (NILP (selected) ? "[X] " : "[ ] ");
8146 s = concat2 (tem, s);
8150 /* If we have room for the prompt string, add it to this line.
8151 If this is the first on the line, always add it. */
8152 if ((SCHARS (s) + i + 2
8153 + (char_matches ? 0 : SCHARS (desc) + 3))
8154 < width
8155 || !notfirst)
8157 int thiswidth;
8159 /* Punctuate between strings. */
8160 if (notfirst)
8162 strcpy (menu + i, ", ");
8163 i += 2;
8165 notfirst = 1;
8166 nobindings = 0 ;
8168 /* If the char to type doesn't match the string's
8169 first char, explicitly show what char to type. */
8170 if (! char_matches)
8172 /* Add as much of string as fits. */
8173 thiswidth = SCHARS (desc);
8174 if (thiswidth + i > width)
8175 thiswidth = width - i;
8176 bcopy (SDATA (desc), menu + i, thiswidth);
8177 i += thiswidth;
8178 strcpy (menu + i, " = ");
8179 i += 3;
8182 /* Add as much of string as fits. */
8183 thiswidth = SCHARS (s);
8184 if (thiswidth + i > width)
8185 thiswidth = width - i;
8186 bcopy (SDATA (s), menu + i, thiswidth);
8187 i += thiswidth;
8188 menu[i] = 0;
8190 else
8192 /* If this element does not fit, end the line now,
8193 and save the element for the next line. */
8194 strcpy (menu + i, "...");
8195 break;
8199 /* Move past this element. */
8200 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
8201 /* Handle reaching end of dense table. */
8202 idx = -1;
8203 if (idx >= 0)
8204 idx++;
8205 else
8206 rest = Fcdr_safe (rest);
8210 /* Prompt with that and read response. */
8211 message2_nolog (menu, strlen (menu),
8212 ! NILP (current_buffer->enable_multibyte_characters));
8214 /* Make believe its not a keyboard macro in case the help char
8215 is pressed. Help characters are not recorded because menu prompting
8216 is not used on replay.
8218 orig_defn_macro = current_kboard->defining_kbd_macro;
8219 current_kboard->defining_kbd_macro = Qnil;
8221 obj = read_char (commandflag, 0, 0, Qt, 0);
8222 while (BUFFERP (obj));
8223 current_kboard->defining_kbd_macro = orig_defn_macro;
8225 if (!INTEGERP (obj))
8226 return obj;
8227 else
8228 ch = XINT (obj);
8230 if (! EQ (obj, menu_prompt_more_char)
8231 && (!INTEGERP (menu_prompt_more_char)
8232 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
8234 if (!NILP (current_kboard->defining_kbd_macro))
8235 store_kbd_macro_char (obj);
8236 return obj;
8238 /* Help char - go round again */
8242 /* Reading key sequences. */
8244 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
8245 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
8246 keymap, or nil otherwise. Return the index of the first keymap in
8247 which KEY has any binding, or NMAPS if no map has a binding.
8249 If KEY is a meta ASCII character, treat it like meta-prefix-char
8250 followed by the corresponding non-meta character. Keymaps in
8251 CURRENT with non-prefix bindings for meta-prefix-char become nil in
8252 NEXT.
8254 If KEY has no bindings in any of the CURRENT maps, NEXT is left
8255 unmodified.
8257 NEXT may be the same array as CURRENT. */
8259 static int
8260 follow_key (key, nmaps, current, defs, next)
8261 Lisp_Object key;
8262 Lisp_Object *current, *defs, *next;
8263 int nmaps;
8265 int i, first_binding;
8267 first_binding = nmaps;
8268 for (i = nmaps - 1; i >= 0; i--)
8270 if (! NILP (current[i]))
8272 defs[i] = access_keymap (current[i], key, 1, 0, 1);
8273 if (! NILP (defs[i]))
8274 first_binding = i;
8276 else
8277 defs[i] = Qnil;
8280 /* Given the set of bindings we've found, produce the next set of maps. */
8281 if (first_binding < nmaps)
8282 for (i = 0; i < nmaps; i++)
8283 next[i] = NILP (defs[i]) ? Qnil : get_keymap (defs[i], 0, 1);
8285 return first_binding;
8288 /* Structure used to keep track of partial application of key remapping
8289 such as Vfunction_key_map and Vkey_translation_map. */
8290 typedef struct keyremap
8292 Lisp_Object map, parent;
8293 int start, end;
8294 } keyremap;
8296 /* Lookup KEY in MAP.
8297 MAP is a keymap mapping keys to key vectors or functions.
8298 If the mapping is a function and DO_FUNCTION is non-zero, then
8299 the function is called with PROMPT as parameter and its return
8300 value is used as the return value of this function (after checking
8301 that it is indeed a vector). */
8303 static Lisp_Object
8304 access_keymap_keyremap (map, key, prompt, do_funcall)
8305 Lisp_Object map, key, prompt;
8306 int do_funcall;
8308 Lisp_Object next;
8310 next = access_keymap (map, key, 1, 0, 1);
8312 /* Handle symbol with autoload definition. */
8313 if (SYMBOLP (next) && !NILP (Ffboundp (next))
8314 && CONSP (XSYMBOL (next)->function)
8315 && EQ (XCAR (XSYMBOL (next)->function), Qautoload))
8316 do_autoload (XSYMBOL (next)->function, next);
8318 /* Handle a symbol whose function definition is a keymap
8319 or an array. */
8320 if (SYMBOLP (next) && !NILP (Ffboundp (next))
8321 && (!NILP (Farrayp (XSYMBOL (next)->function))
8322 || KEYMAPP (XSYMBOL (next)->function)))
8323 next = XSYMBOL (next)->function;
8325 /* If the keymap gives a function, not an
8326 array, then call the function with one arg and use
8327 its value instead. */
8328 if (SYMBOLP (next) && !NILP (Ffboundp (next)) && do_funcall)
8330 Lisp_Object tem;
8331 tem = next;
8333 next = call1 (next, prompt);
8334 /* If the function returned something invalid,
8335 barf--don't ignore it.
8336 (To ignore it safely, we would need to gcpro a bunch of
8337 other variables.) */
8338 if (! (VECTORP (next) || STRINGP (next)))
8339 error ("Function %s returns invalid key sequence", tem);
8341 return next;
8344 /* Do one step of the key remapping used for function-key-map and
8345 key-translation-map:
8346 KEYBUF is the buffer holding the input events.
8347 BUFSIZE is its maximum size.
8348 FKEY is a pointer to the keyremap structure to use.
8349 INPUT is the index of the last element in KEYBUF.
8350 DOIT if non-zero says that the remapping can actually take place.
8351 DIFF is used to return the number of keys added/removed by the remapping.
8352 PARENT is the root of the keymap.
8353 PROMPT is the prompt to use if the remapping happens through a function.
8354 The return value is non-zero if the remapping actually took place. */
8356 static int
8357 keyremap_step (keybuf, bufsize, fkey, input, doit, diff, prompt)
8358 Lisp_Object *keybuf, prompt;
8359 keyremap *fkey;
8360 int input, doit, *diff, bufsize;
8362 Lisp_Object next, key;
8364 key = keybuf[fkey->end++];
8365 next = access_keymap_keyremap (fkey->map, key, prompt, doit);
8367 /* If keybuf[fkey->start..fkey->end] is bound in the
8368 map and we're in a position to do the key remapping, replace it with
8369 the binding and restart with fkey->start at the end. */
8370 if ((VECTORP (next) || STRINGP (next)) && doit)
8372 int len = XFASTINT (Flength (next));
8373 int i;
8375 *diff = len - (fkey->end - fkey->start);
8377 if (input + *diff >= bufsize)
8378 error ("Key sequence too long");
8380 /* Shift the keys that follow fkey->end. */
8381 if (*diff < 0)
8382 for (i = fkey->end; i < input; i++)
8383 keybuf[i + *diff] = keybuf[i];
8384 else if (*diff > 0)
8385 for (i = input - 1; i >= fkey->end; i--)
8386 keybuf[i + *diff] = keybuf[i];
8387 /* Overwrite the old keys with the new ones. */
8388 for (i = 0; i < len; i++)
8389 keybuf[fkey->start + i]
8390 = Faref (next, make_number (i));
8392 fkey->start = fkey->end += *diff;
8393 fkey->map = fkey->parent;
8395 return 1;
8398 fkey->map = get_keymap (next, 0, 1);
8400 /* If we no longer have a bound suffix, try a new position for
8401 fkey->start. */
8402 if (!CONSP (fkey->map))
8404 fkey->end = ++fkey->start;
8405 fkey->map = fkey->parent;
8407 return 0;
8410 /* Read a sequence of keys that ends with a non prefix character,
8411 storing it in KEYBUF, a buffer of size BUFSIZE.
8412 Prompt with PROMPT.
8413 Return the length of the key sequence stored.
8414 Return -1 if the user rejected a command menu.
8416 Echo starting immediately unless `prompt' is 0.
8418 Where a key sequence ends depends on the currently active keymaps.
8419 These include any minor mode keymaps active in the current buffer,
8420 the current buffer's local map, and the global map.
8422 If a key sequence has no other bindings, we check Vfunction_key_map
8423 to see if some trailing subsequence might be the beginning of a
8424 function key's sequence. If so, we try to read the whole function
8425 key, and substitute its symbolic name into the key sequence.
8427 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
8428 `double-' events into similar click events, if that would make them
8429 bound. We try to turn `triple-' events first into `double-' events,
8430 then into clicks.
8432 If we get a mouse click in a mode line, vertical divider, or other
8433 non-text area, we treat the click as if it were prefixed by the
8434 symbol denoting that area - `mode-line', `vertical-line', or
8435 whatever.
8437 If the sequence starts with a mouse click, we read the key sequence
8438 with respect to the buffer clicked on, not the current buffer.
8440 If the user switches frames in the midst of a key sequence, we put
8441 off the switch-frame event until later; the next call to
8442 read_char will return it.
8444 If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
8445 from the selected window's buffer. */
8447 static int
8448 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
8449 can_return_switch_frame, fix_current_buffer)
8450 Lisp_Object *keybuf;
8451 int bufsize;
8452 Lisp_Object prompt;
8453 int dont_downcase_last;
8454 int can_return_switch_frame;
8455 int fix_current_buffer;
8457 volatile Lisp_Object from_string;
8458 volatile int count = SPECPDL_INDEX ();
8460 /* How many keys there are in the current key sequence. */
8461 volatile int t;
8463 /* The length of the echo buffer when we started reading, and
8464 the length of this_command_keys when we started reading. */
8465 volatile int echo_start;
8466 volatile int keys_start;
8468 /* The number of keymaps we're scanning right now, and the number of
8469 keymaps we have allocated space for. */
8470 volatile int nmaps;
8471 volatile int nmaps_allocated = 0;
8473 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
8474 the current keymaps. */
8475 Lisp_Object *volatile defs = NULL;
8477 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
8478 in the current keymaps, or nil where it is not a prefix. */
8479 Lisp_Object *volatile submaps = NULL;
8481 /* The local map to start out with at start of key sequence. */
8482 volatile Lisp_Object orig_local_map;
8484 /* The map from the `keymap' property to start out with at start of
8485 key sequence. */
8486 volatile Lisp_Object orig_keymap;
8488 /* 1 if we have already considered switching to the local-map property
8489 of the place where a mouse click occurred. */
8490 volatile int localized_local_map = 0;
8492 /* The index in defs[] of the first keymap that has a binding for
8493 this key sequence. In other words, the lowest i such that
8494 defs[i] is non-nil. */
8495 volatile int first_binding;
8496 /* Index of the first key that has no binding.
8497 It is useless to try fkey.start larger than that. */
8498 volatile int first_unbound;
8500 /* If t < mock_input, then KEYBUF[t] should be read as the next
8501 input key.
8503 We use this to recover after recognizing a function key. Once we
8504 realize that a suffix of the current key sequence is actually a
8505 function key's escape sequence, we replace the suffix with the
8506 function key's binding from Vfunction_key_map. Now keybuf
8507 contains a new and different key sequence, so the echo area,
8508 this_command_keys, and the submaps and defs arrays are wrong. In
8509 this situation, we set mock_input to t, set t to 0, and jump to
8510 restart_sequence; the loop will read keys from keybuf up until
8511 mock_input, thus rebuilding the state; and then it will resume
8512 reading characters from the keyboard. */
8513 volatile int mock_input = 0;
8515 /* If the sequence is unbound in submaps[], then
8516 keybuf[fkey.start..fkey.end-1] is a prefix in Vfunction_key_map,
8517 and fkey.map is its binding.
8519 These might be > t, indicating that all function key scanning
8520 should hold off until t reaches them. We do this when we've just
8521 recognized a function key, to avoid searching for the function
8522 key's again in Vfunction_key_map. */
8523 volatile keyremap fkey;
8525 /* Likewise, for key_translation_map. */
8526 volatile keyremap keytran;
8528 /* If we receive a `switch-frame' or `select-window' event in the middle of
8529 a key sequence, we put it off for later.
8530 While we're reading, we keep the event here. */
8531 volatile Lisp_Object delayed_switch_frame;
8533 /* See the comment below... */
8534 #if defined (GOBBLE_FIRST_EVENT)
8535 Lisp_Object first_event;
8536 #endif
8538 volatile Lisp_Object original_uppercase;
8539 volatile int original_uppercase_position = -1;
8541 /* Gets around Microsoft compiler limitations. */
8542 int dummyflag = 0;
8544 struct buffer *starting_buffer;
8546 /* List of events for which a fake prefix key has been generated. */
8547 volatile Lisp_Object fake_prefixed_keys = Qnil;
8549 #if defined (GOBBLE_FIRST_EVENT)
8550 int junk;
8551 #endif
8553 struct gcpro gcpro1;
8555 GCPRO1 (fake_prefixed_keys);
8556 raw_keybuf_count = 0;
8558 last_nonmenu_event = Qnil;
8560 delayed_switch_frame = Qnil;
8561 fkey.map = fkey.parent = Vfunction_key_map;
8562 keytran.map = keytran.parent = Vkey_translation_map;
8563 /* If there is no translation-map, turn off scanning. */
8564 fkey.start = fkey.end = KEYMAPP (fkey.map) ? 0 : bufsize + 1;
8565 keytran.start = keytran.end = KEYMAPP (keytran.map) ? 0 : bufsize + 1;
8567 if (INTERACTIVE)
8569 if (!NILP (prompt))
8570 echo_prompt (prompt);
8571 else if (cursor_in_echo_area
8572 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
8573 && NILP (Fzerop (Vecho_keystrokes)))
8574 /* This doesn't put in a dash if the echo buffer is empty, so
8575 you don't always see a dash hanging out in the minibuffer. */
8576 echo_dash ();
8579 /* Record the initial state of the echo area and this_command_keys;
8580 we will need to restore them if we replay a key sequence. */
8581 if (INTERACTIVE)
8582 echo_start = echo_length ();
8583 keys_start = this_command_key_count;
8584 this_single_command_key_start = keys_start;
8586 #if defined (GOBBLE_FIRST_EVENT)
8587 /* This doesn't quite work, because some of the things that read_char
8588 does cannot safely be bypassed. It seems too risky to try to make
8589 this work right. */
8591 /* Read the first char of the sequence specially, before setting
8592 up any keymaps, in case a filter runs and switches buffers on us. */
8593 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
8594 &junk);
8595 #endif /* GOBBLE_FIRST_EVENT */
8597 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8598 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8599 from_string = Qnil;
8601 /* We jump here when the key sequence has been thoroughly changed, and
8602 we need to rescan it starting from the beginning. When we jump here,
8603 keybuf[0..mock_input] holds the sequence we should reread. */
8604 replay_sequence:
8606 starting_buffer = current_buffer;
8607 first_unbound = bufsize + 1;
8609 /* Build our list of keymaps.
8610 If we recognize a function key and replace its escape sequence in
8611 keybuf with its symbol, or if the sequence starts with a mouse
8612 click and we need to switch buffers, we jump back here to rebuild
8613 the initial keymaps from the current buffer. */
8614 nmaps = 0;
8616 if (!NILP (current_kboard->Voverriding_terminal_local_map)
8617 || !NILP (Voverriding_local_map))
8619 if (3 > nmaps_allocated)
8621 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
8622 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
8623 nmaps_allocated = 3;
8625 if (!NILP (current_kboard->Voverriding_terminal_local_map))
8626 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
8627 if (!NILP (Voverriding_local_map))
8628 submaps[nmaps++] = Voverriding_local_map;
8630 else
8632 int nminor;
8633 int total;
8634 Lisp_Object *maps;
8636 nminor = current_minor_maps (0, &maps);
8637 total = nminor + (!NILP (orig_keymap) ? 3 : 2);
8639 if (total > nmaps_allocated)
8641 submaps = (Lisp_Object *) alloca (total * sizeof (submaps[0]));
8642 defs = (Lisp_Object *) alloca (total * sizeof (defs[0]));
8643 nmaps_allocated = total;
8646 if (!NILP (orig_keymap))
8647 submaps[nmaps++] = orig_keymap;
8649 bcopy (maps, (void *) (submaps + nmaps),
8650 nminor * sizeof (submaps[0]));
8652 nmaps += nminor;
8654 submaps[nmaps++] = orig_local_map;
8656 submaps[nmaps++] = current_global_map;
8658 /* Find an accurate initial value for first_binding. */
8659 for (first_binding = 0; first_binding < nmaps; first_binding++)
8660 if (! NILP (submaps[first_binding]))
8661 break;
8663 /* Start from the beginning in keybuf. */
8664 t = 0;
8666 /* These are no-ops the first time through, but if we restart, they
8667 revert the echo area and this_command_keys to their original state. */
8668 this_command_key_count = keys_start;
8669 if (INTERACTIVE && t < mock_input)
8670 echo_truncate (echo_start);
8672 /* If the best binding for the current key sequence is a keymap, or
8673 we may be looking at a function key's escape sequence, keep on
8674 reading. */
8675 while (first_binding < nmaps
8676 /* Keep reading as long as there's a prefix binding. */
8677 ? !NILP (submaps[first_binding])
8678 /* Don't return in the middle of a possible function key sequence,
8679 if the only bindings we found were via case conversion.
8680 Thus, if ESC O a has a function-key-map translation
8681 and ESC o has a binding, don't return after ESC O,
8682 so that we can translate ESC O plus the next character. */
8683 : (fkey.start < t || keytran.start < t))
8685 Lisp_Object key;
8686 int used_mouse_menu = 0;
8688 /* Where the last real key started. If we need to throw away a
8689 key that has expanded into more than one element of keybuf
8690 (say, a mouse click on the mode line which is being treated
8691 as [mode-line (mouse-...)], then we backtrack to this point
8692 of keybuf. */
8693 volatile int last_real_key_start;
8695 /* These variables are analogous to echo_start and keys_start;
8696 while those allow us to restart the entire key sequence,
8697 echo_local_start and keys_local_start allow us to throw away
8698 just one key. */
8699 volatile int echo_local_start, keys_local_start, local_first_binding;
8701 eassert (fkey.end == t || (fkey.end > t && fkey.end <= mock_input));
8702 eassert (fkey.start <= fkey.end);
8703 eassert (keytran.start <= keytran.end);
8704 /* key-translation-map is applied *after* function-key-map. */
8705 eassert (keytran.end <= fkey.start);
8707 if (first_unbound < fkey.start && first_unbound < keytran.start)
8708 { /* The prefix upto first_unbound has no binding and has
8709 no translation left to do either, so we know it's unbound.
8710 If we don't stop now, we risk staying here indefinitely
8711 (if the user keeps entering fkey or keytran prefixes
8712 like C-c ESC ESC ESC ESC ...) */
8713 int i;
8714 for (i = first_unbound + 1; i < t; i++)
8715 keybuf[i - first_unbound - 1] = keybuf[i];
8716 mock_input = t - first_unbound - 1;
8717 fkey.end = fkey.start -= first_unbound + 1;
8718 fkey.map = fkey.parent;
8719 keytran.end = keytran.start -= first_unbound + 1;
8720 keytran.map = keytran.parent;
8721 goto replay_sequence;
8724 if (t >= bufsize)
8725 error ("Key sequence too long");
8727 if (INTERACTIVE)
8728 echo_local_start = echo_length ();
8729 keys_local_start = this_command_key_count;
8730 local_first_binding = first_binding;
8732 replay_key:
8733 /* These are no-ops, unless we throw away a keystroke below and
8734 jumped back up to replay_key; in that case, these restore the
8735 variables to their original state, allowing us to replay the
8736 loop. */
8737 if (INTERACTIVE && t < mock_input)
8738 echo_truncate (echo_local_start);
8739 this_command_key_count = keys_local_start;
8740 first_binding = local_first_binding;
8742 /* By default, assume each event is "real". */
8743 last_real_key_start = t;
8745 /* Does mock_input indicate that we are re-reading a key sequence? */
8746 if (t < mock_input)
8748 key = keybuf[t];
8749 add_command_key (key);
8750 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
8751 && NILP (Fzerop (Vecho_keystrokes)))
8752 echo_char (key);
8755 /* If not, we should actually read a character. */
8756 else
8759 #ifdef MULTI_KBOARD
8760 KBOARD *interrupted_kboard = current_kboard;
8761 struct frame *interrupted_frame = SELECTED_FRAME ();
8762 if (setjmp (wrong_kboard_jmpbuf))
8764 if (!NILP (delayed_switch_frame))
8766 interrupted_kboard->kbd_queue
8767 = Fcons (delayed_switch_frame,
8768 interrupted_kboard->kbd_queue);
8769 delayed_switch_frame = Qnil;
8771 while (t > 0)
8772 interrupted_kboard->kbd_queue
8773 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
8775 /* If the side queue is non-empty, ensure it begins with a
8776 switch-frame, so we'll replay it in the right context. */
8777 if (CONSP (interrupted_kboard->kbd_queue)
8778 && (key = XCAR (interrupted_kboard->kbd_queue),
8779 !(EVENT_HAS_PARAMETERS (key)
8780 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
8781 Qswitch_frame))))
8783 Lisp_Object frame;
8784 XSETFRAME (frame, interrupted_frame);
8785 interrupted_kboard->kbd_queue
8786 = Fcons (make_lispy_switch_frame (frame),
8787 interrupted_kboard->kbd_queue);
8789 mock_input = 0;
8790 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8791 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8792 goto replay_sequence;
8794 #endif
8795 key = read_char (NILP (prompt), nmaps,
8796 (Lisp_Object *) submaps, last_nonmenu_event,
8797 &used_mouse_menu);
8800 /* read_char returns t when it shows a menu and the user rejects it.
8801 Just return -1. */
8802 if (EQ (key, Qt))
8804 unbind_to (count, Qnil);
8805 UNGCPRO;
8806 return -1;
8809 /* read_char returns -1 at the end of a macro.
8810 Emacs 18 handles this by returning immediately with a
8811 zero, so that's what we'll do. */
8812 if (INTEGERP (key) && XINT (key) == -1)
8814 t = 0;
8815 /* The Microsoft C compiler can't handle the goto that
8816 would go here. */
8817 dummyflag = 1;
8818 break;
8821 /* If the current buffer has been changed from under us, the
8822 keymap may have changed, so replay the sequence. */
8823 if (BUFFERP (key))
8825 EMACS_TIME initial_idleness_start_time;
8826 EMACS_SET_SECS_USECS (initial_idleness_start_time,
8827 EMACS_SECS (timer_last_idleness_start_time),
8828 EMACS_USECS (timer_last_idleness_start_time));
8830 /* Resume idle state, using the same start-time as before. */
8831 timer_start_idle ();
8832 timer_idleness_start_time = initial_idleness_start_time;
8834 mock_input = t;
8835 /* Reset the current buffer from the selected window
8836 in case something changed the former and not the latter.
8837 This is to be more consistent with the behavior
8838 of the command_loop_1. */
8839 if (fix_current_buffer)
8841 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8842 Fkill_emacs (Qnil);
8843 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
8844 Fset_buffer (XWINDOW (selected_window)->buffer);
8847 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8848 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8849 goto replay_sequence;
8852 /* If we have a quit that was typed in another frame, and
8853 quit_throw_to_read_char switched buffers,
8854 replay to get the right keymap. */
8855 if (INTEGERP (key)
8856 && XINT (key) == quit_char
8857 && current_buffer != starting_buffer)
8859 GROW_RAW_KEYBUF;
8860 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8861 keybuf[t++] = key;
8862 mock_input = t;
8863 Vquit_flag = Qnil;
8864 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8865 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8866 goto replay_sequence;
8869 Vquit_flag = Qnil;
8871 if (EVENT_HAS_PARAMETERS (key)
8872 /* Either a `switch-frame' or a `select-window' event. */
8873 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), Qswitch_frame))
8875 /* If we're at the beginning of a key sequence, and the caller
8876 says it's okay, go ahead and return this event. If we're
8877 in the midst of a key sequence, delay it until the end. */
8878 if (t > 0 || !can_return_switch_frame)
8880 delayed_switch_frame = key;
8881 goto replay_key;
8885 GROW_RAW_KEYBUF;
8886 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8889 /* Clicks in non-text areas get prefixed by the symbol
8890 in their CHAR-ADDRESS field. For example, a click on
8891 the mode line is prefixed by the symbol `mode-line'.
8893 Furthermore, key sequences beginning with mouse clicks
8894 are read using the keymaps of the buffer clicked on, not
8895 the current buffer. So we may have to switch the buffer
8896 here.
8898 When we turn one event into two events, we must make sure
8899 that neither of the two looks like the original--so that,
8900 if we replay the events, they won't be expanded again.
8901 If not for this, such reexpansion could happen either here
8902 or when user programs play with this-command-keys. */
8903 if (EVENT_HAS_PARAMETERS (key))
8905 Lisp_Object kind;
8906 Lisp_Object string;
8908 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
8909 if (EQ (kind, Qmouse_click))
8911 Lisp_Object window, posn;
8913 window = POSN_WINDOW (EVENT_START (key));
8914 posn = POSN_POSN (EVENT_START (key));
8916 if (CONSP (posn)
8917 || (!NILP (fake_prefixed_keys)
8918 && !NILP (Fmemq (key, fake_prefixed_keys))))
8920 /* We're looking a second time at an event for which
8921 we generated a fake prefix key. Set
8922 last_real_key_start appropriately. */
8923 if (t > 0)
8924 last_real_key_start = t - 1;
8927 /* Key sequences beginning with mouse clicks are
8928 read using the keymaps in the buffer clicked on,
8929 not the current buffer. If we're at the
8930 beginning of a key sequence, switch buffers. */
8931 if (last_real_key_start == 0
8932 && WINDOWP (window)
8933 && BUFFERP (XWINDOW (window)->buffer)
8934 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
8936 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8937 keybuf[t] = key;
8938 mock_input = t + 1;
8940 /* Arrange to go back to the original buffer once we're
8941 done reading the key sequence. Note that we can't
8942 use save_excursion_{save,restore} here, because they
8943 save point as well as the current buffer; we don't
8944 want to save point, because redisplay may change it,
8945 to accommodate a Fset_window_start or something. We
8946 don't want to do this at the top of the function,
8947 because we may get input from a subprocess which
8948 wants to change the selected window and stuff (say,
8949 emacsclient). */
8950 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
8952 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8953 Fkill_emacs (Qnil);
8954 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
8955 orig_local_map = get_local_map (PT, current_buffer,
8956 Qlocal_map);
8957 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8958 goto replay_sequence;
8961 /* For a mouse click, get the local text-property keymap
8962 of the place clicked on, rather than point. */
8963 if (last_real_key_start == 0
8964 && CONSP (XCDR (key))
8965 && ! localized_local_map)
8967 Lisp_Object map_here, start, pos;
8969 localized_local_map = 1;
8970 start = EVENT_START (key);
8972 if (CONSP (start) && POSN_INBUFFER_P (start))
8974 pos = POSN_BUFFER_POSN (start);
8975 if (INTEGERP (pos)
8976 && XINT (pos) >= BEG && XINT (pos) <= Z)
8978 map_here = get_local_map (XINT (pos),
8979 current_buffer, Qlocal_map);
8980 if (!EQ (map_here, orig_local_map))
8982 orig_local_map = map_here;
8983 keybuf[t] = key;
8984 mock_input = t + 1;
8986 goto replay_sequence;
8988 map_here = get_local_map (XINT (pos),
8989 current_buffer, Qkeymap);
8990 if (!EQ (map_here, orig_keymap))
8992 orig_keymap = map_here;
8993 keybuf[t] = key;
8994 mock_input = t + 1;
8996 goto replay_sequence;
9002 /* Expand mode-line and scroll-bar events into two events:
9003 use posn as a fake prefix key. */
9004 if (SYMBOLP (posn)
9005 && (NILP (fake_prefixed_keys)
9006 || NILP (Fmemq (key, fake_prefixed_keys))))
9008 if (t + 1 >= bufsize)
9009 error ("Key sequence too long");
9011 keybuf[t] = posn;
9012 keybuf[t + 1] = key;
9013 mock_input = t + 2;
9015 /* Record that a fake prefix key has been generated
9016 for KEY. Don't modify the event; this would
9017 prevent proper action when the event is pushed
9018 back into unread-command-events. */
9019 fake_prefixed_keys = Fcons (key, fake_prefixed_keys);
9021 /* If on a mode line string with a local keymap,
9022 reconsider the key sequence with that keymap. */
9023 if (string = POSN_STRING (EVENT_START (key)),
9024 (CONSP (string) && STRINGP (XCAR (string))))
9026 Lisp_Object pos, map, map2;
9028 pos = XCDR (string);
9029 string = XCAR (string);
9030 if (XINT (pos) >= 0
9031 && XINT (pos) < SCHARS (string))
9033 map = Fget_text_property (pos, Qlocal_map, string);
9034 if (!NILP (map))
9035 orig_local_map = map;
9036 map2 = Fget_text_property (pos, Qkeymap, string);
9037 if (!NILP (map2))
9038 orig_keymap = map2;
9039 if (!NILP (map) || !NILP (map2))
9040 goto replay_sequence;
9044 goto replay_key;
9046 else if (NILP (from_string)
9047 && (string = POSN_STRING (EVENT_START (key)),
9048 (CONSP (string) && STRINGP (XCAR (string)))))
9050 /* For a click on a string, i.e. overlay string or a
9051 string displayed via the `display' property,
9052 consider `local-map' and `keymap' properties of
9053 that string. */
9054 Lisp_Object pos, map, map2;
9056 pos = XCDR (string);
9057 string = XCAR (string);
9058 if (XINT (pos) >= 0
9059 && XINT (pos) < SCHARS (string))
9061 map = Fget_text_property (pos, Qlocal_map, string);
9062 if (!NILP (map))
9063 orig_local_map = map;
9064 map2 = Fget_text_property (pos, Qkeymap, string);
9065 if (!NILP (map2))
9066 orig_keymap = map2;
9068 if (!NILP (map) || !NILP (map2))
9070 from_string = string;
9071 goto replay_sequence;
9076 else if (CONSP (XCDR (key))
9077 && CONSP (EVENT_START (key))
9078 && CONSP (XCDR (EVENT_START (key))))
9080 Lisp_Object posn;
9082 posn = POSN_POSN (EVENT_START (key));
9083 /* Handle menu-bar events:
9084 insert the dummy prefix event `menu-bar'. */
9085 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
9087 if (t + 1 >= bufsize)
9088 error ("Key sequence too long");
9089 keybuf[t] = posn;
9090 keybuf[t+1] = key;
9092 /* Zap the position in key, so we know that we've
9093 expanded it, and don't try to do so again. */
9094 POSN_SET_POSN (EVENT_START (key),
9095 Fcons (posn, Qnil));
9097 mock_input = t + 2;
9098 goto replay_sequence;
9100 else if (CONSP (posn))
9102 /* We're looking at the second event of a
9103 sequence which we expanded before. Set
9104 last_real_key_start appropriately. */
9105 if (last_real_key_start == t && t > 0)
9106 last_real_key_start = t - 1;
9111 /* We have finally decided that KEY is something we might want
9112 to look up. */
9113 first_binding = (follow_key (key,
9114 nmaps - first_binding,
9115 submaps + first_binding,
9116 defs + first_binding,
9117 submaps + first_binding)
9118 + first_binding);
9120 /* If KEY wasn't bound, we'll try some fallbacks. */
9121 if (first_binding < nmaps)
9122 /* This is needed for the following scenario:
9123 event 0: a down-event that gets dropped by calling replay_key.
9124 event 1: some normal prefix like C-h.
9125 After event 0, first_unbound is 0, after event 1 fkey.start
9126 and keytran.start are both 1, so when we see that C-h is bound,
9127 we need to update first_unbound. */
9128 first_unbound = max (t + 1, first_unbound);
9129 else
9131 Lisp_Object head;
9133 /* Remember the position to put an upper bound on fkey.start. */
9134 first_unbound = min (t, first_unbound);
9136 head = EVENT_HEAD (key);
9137 if (help_char_p (head) && t > 0)
9139 read_key_sequence_cmd = Vprefix_help_command;
9140 keybuf[t++] = key;
9141 last_nonmenu_event = key;
9142 /* The Microsoft C compiler can't handle the goto that
9143 would go here. */
9144 dummyflag = 1;
9145 break;
9148 if (SYMBOLP (head))
9150 Lisp_Object breakdown;
9151 int modifiers;
9153 breakdown = parse_modifiers (head);
9154 modifiers = XINT (XCAR (XCDR (breakdown)));
9155 /* Attempt to reduce an unbound mouse event to a simpler
9156 event that is bound:
9157 Drags reduce to clicks.
9158 Double-clicks reduce to clicks.
9159 Triple-clicks reduce to double-clicks, then to clicks.
9160 Down-clicks are eliminated.
9161 Double-downs reduce to downs, then are eliminated.
9162 Triple-downs reduce to double-downs, then to downs,
9163 then are eliminated. */
9164 if (modifiers & (down_modifier | drag_modifier
9165 | double_modifier | triple_modifier))
9167 while (modifiers & (down_modifier | drag_modifier
9168 | double_modifier | triple_modifier))
9170 Lisp_Object new_head, new_click;
9171 if (modifiers & triple_modifier)
9172 modifiers ^= (double_modifier | triple_modifier);
9173 else if (modifiers & double_modifier)
9174 modifiers &= ~double_modifier;
9175 else if (modifiers & drag_modifier)
9176 modifiers &= ~drag_modifier;
9177 else
9179 /* Dispose of this `down' event by simply jumping
9180 back to replay_key, to get another event.
9182 Note that if this event came from mock input,
9183 then just jumping back to replay_key will just
9184 hand it to us again. So we have to wipe out any
9185 mock input.
9187 We could delete keybuf[t] and shift everything
9188 after that to the left by one spot, but we'd also
9189 have to fix up any variable that points into
9190 keybuf, and shifting isn't really necessary
9191 anyway.
9193 Adding prefixes for non-textual mouse clicks
9194 creates two characters of mock input, and both
9195 must be thrown away. If we're only looking at
9196 the prefix now, we can just jump back to
9197 replay_key. On the other hand, if we've already
9198 processed the prefix, and now the actual click
9199 itself is giving us trouble, then we've lost the
9200 state of the keymaps we want to backtrack to, and
9201 we need to replay the whole sequence to rebuild
9204 Beyond that, only function key expansion could
9205 create more than two keys, but that should never
9206 generate mouse events, so it's okay to zero
9207 mock_input in that case too.
9209 FIXME: The above paragraph seems just plain
9210 wrong, if you consider things like
9211 xterm-mouse-mode. -stef
9213 Isn't this just the most wonderful code ever? */
9215 /* If mock_input > t + 1, the above simplification
9216 will actually end up dropping keys on the floor.
9217 This is probably OK for now, but even
9218 if mock_input <= t + 1, we need to adjust fkey
9219 and keytran.
9220 Typical case [header-line down-mouse-N]:
9221 mock_input = 2, t = 1, fkey.end = 1,
9222 last_real_key_start = 0. */
9223 if (fkey.end > last_real_key_start)
9225 fkey.end = fkey.start
9226 = min (last_real_key_start, fkey.start);
9227 fkey.map = fkey.parent;
9228 if (keytran.end > last_real_key_start)
9230 keytran.end = keytran.start
9231 = min (last_real_key_start, keytran.start);
9232 keytran.map = keytran.parent;
9235 if (t == last_real_key_start)
9237 mock_input = 0;
9238 goto replay_key;
9240 else
9242 mock_input = last_real_key_start;
9243 goto replay_sequence;
9247 new_head
9248 = apply_modifiers (modifiers, XCAR (breakdown));
9249 new_click
9250 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
9252 /* Look for a binding for this new key. follow_key
9253 promises that it didn't munge submaps the
9254 last time we called it, since key was unbound. */
9255 first_binding
9256 = (follow_key (new_click,
9257 nmaps - local_first_binding,
9258 submaps + local_first_binding,
9259 defs + local_first_binding,
9260 submaps + local_first_binding)
9261 + local_first_binding);
9263 /* If that click is bound, go for it. */
9264 if (first_binding < nmaps)
9266 key = new_click;
9267 break;
9269 /* Otherwise, we'll leave key set to the drag event. */
9275 keybuf[t++] = key;
9276 /* Normally, last_nonmenu_event gets the previous key we read.
9277 But when a mouse popup menu is being used,
9278 we don't update last_nonmenu_event; it continues to hold the mouse
9279 event that preceded the first level of menu. */
9280 if (!used_mouse_menu)
9281 last_nonmenu_event = key;
9283 /* Record what part of this_command_keys is the current key sequence. */
9284 this_single_command_key_start = this_command_key_count - t;
9286 if (first_binding < nmaps && NILP (submaps[first_binding]))
9287 /* There is a binding and it's not a prefix.
9288 There is thus no function-key in this sequence.
9289 Moving fkey.start is important in this case to allow keytran.start
9290 to go over the sequence before we return (since we keep the
9291 invariant that keytran.end <= fkey.start). */
9293 if (fkey.start < t)
9294 (fkey.start = fkey.end = t, fkey.map = fkey.parent);
9296 else
9297 /* If the sequence is unbound, see if we can hang a function key
9298 off the end of it. */
9299 /* Continue scan from fkey.end until we find a bound suffix. */
9300 while (fkey.end < t)
9302 struct gcpro gcpro1, gcpro2, gcpro3;
9303 int done, diff;
9305 GCPRO3 (fkey.map, keytran.map, delayed_switch_frame);
9306 done = keyremap_step (keybuf, bufsize, &fkey,
9307 max (t, mock_input),
9308 /* If there's a binding (i.e.
9309 first_binding >= nmaps) we don't want
9310 to apply this function-key-mapping. */
9311 fkey.end + 1 == t && first_binding >= nmaps,
9312 &diff, prompt);
9313 UNGCPRO;
9314 if (done)
9316 mock_input = diff + max (t, mock_input);
9317 goto replay_sequence;
9321 /* Look for this sequence in key-translation-map.
9322 Scan from keytran.end until we find a bound suffix. */
9323 while (keytran.end < fkey.start)
9325 struct gcpro gcpro1, gcpro2, gcpro3;
9326 int done, diff;
9328 GCPRO3 (fkey.map, keytran.map, delayed_switch_frame);
9329 done = keyremap_step (keybuf, bufsize, &keytran, max (t, mock_input),
9330 1, &diff, prompt);
9331 UNGCPRO;
9332 if (done)
9334 mock_input = diff + max (t, mock_input);
9335 /* Adjust the function-key-map counters. */
9336 fkey.end += diff;
9337 fkey.start += diff;
9339 goto replay_sequence;
9343 /* If KEY is not defined in any of the keymaps,
9344 and cannot be part of a function key or translation,
9345 and is an upper case letter
9346 use the corresponding lower-case letter instead. */
9347 if (first_binding >= nmaps
9348 && fkey.start >= t && keytran.start >= t
9349 && INTEGERP (key)
9350 && ((((XINT (key) & 0x3ffff)
9351 < XCHAR_TABLE (current_buffer->downcase_table)->size)
9352 && UPPERCASEP (XINT (key) & 0x3ffff))
9353 || (XINT (key) & shift_modifier)))
9355 Lisp_Object new_key;
9357 original_uppercase = key;
9358 original_uppercase_position = t - 1;
9360 if (XINT (key) & shift_modifier)
9361 XSETINT (new_key, XINT (key) & ~shift_modifier);
9362 else
9363 XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
9364 | (XINT (key) & ~0x3ffff)));
9366 /* We have to do this unconditionally, regardless of whether
9367 the lower-case char is defined in the keymaps, because they
9368 might get translated through function-key-map. */
9369 keybuf[t - 1] = new_key;
9370 mock_input = max (t, mock_input);
9372 goto replay_sequence;
9374 /* If KEY is not defined in any of the keymaps,
9375 and cannot be part of a function key or translation,
9376 and is a shifted function key,
9377 use the corresponding unshifted function key instead. */
9378 if (first_binding >= nmaps
9379 && fkey.start >= t && keytran.start >= t
9380 && SYMBOLP (key))
9382 Lisp_Object breakdown;
9383 int modifiers;
9385 breakdown = parse_modifiers (key);
9386 modifiers = XINT (XCAR (XCDR (breakdown)));
9387 if (modifiers & shift_modifier)
9389 Lisp_Object new_key;
9391 original_uppercase = key;
9392 original_uppercase_position = t - 1;
9394 modifiers &= ~shift_modifier;
9395 new_key = apply_modifiers (modifiers,
9396 XCAR (breakdown));
9398 keybuf[t - 1] = new_key;
9399 mock_input = max (t, mock_input);
9400 fkey.start = fkey.end = KEYMAPP (fkey.map) ? 0 : bufsize + 1;
9401 keytran.start = keytran.end = KEYMAPP (keytran.map) ? 0 : bufsize + 1;
9403 goto replay_sequence;
9408 if (!dummyflag)
9409 read_key_sequence_cmd = (first_binding < nmaps
9410 ? defs[first_binding]
9411 : Qnil);
9413 unread_switch_frame = delayed_switch_frame;
9414 unbind_to (count, Qnil);
9416 /* Don't downcase the last character if the caller says don't.
9417 Don't downcase it if the result is undefined, either. */
9418 if ((dont_downcase_last || first_binding >= nmaps)
9419 && t - 1 == original_uppercase_position)
9420 keybuf[t - 1] = original_uppercase;
9422 /* Occasionally we fabricate events, perhaps by expanding something
9423 according to function-key-map, or by adding a prefix symbol to a
9424 mouse click in the scroll bar or modeline. In this cases, return
9425 the entire generated key sequence, even if we hit an unbound
9426 prefix or a definition before the end. This means that you will
9427 be able to push back the event properly, and also means that
9428 read-key-sequence will always return a logical unit.
9430 Better ideas? */
9431 for (; t < mock_input; t++)
9433 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
9434 && NILP (Fzerop (Vecho_keystrokes)))
9435 echo_char (keybuf[t]);
9436 add_command_key (keybuf[t]);
9441 UNGCPRO;
9442 return t;
9445 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 5, 0,
9446 doc: /* Read a sequence of keystrokes and return as a string or vector.
9447 The sequence is sufficient to specify a non-prefix command in the
9448 current local and global maps.
9450 First arg PROMPT is a prompt string. If nil, do not prompt specially.
9451 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos
9452 as a continuation of the previous key.
9454 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not
9455 convert the last event to lower case. (Normally any upper case event
9456 is converted to lower case if the original event is undefined and the lower
9457 case equivalent is defined.) A non-nil value is appropriate for reading
9458 a key sequence to be defined.
9460 A C-g typed while in this function is treated like any other character,
9461 and `quit-flag' is not set.
9463 If the key sequence starts with a mouse click, then the sequence is read
9464 using the keymaps of the buffer of the window clicked in, not the buffer
9465 of the selected window as normal.
9467 `read-key-sequence' drops unbound button-down events, since you normally
9468 only care about the click or drag events which follow them. If a drag
9469 or multi-click event is unbound, but the corresponding click event would
9470 be bound, `read-key-sequence' turns the event into a click event at the
9471 drag's starting position. This means that you don't have to distinguish
9472 between click and drag, double, or triple events unless you want to.
9474 `read-key-sequence' prefixes mouse events on mode lines, the vertical
9475 lines separating windows, and scroll bars with imaginary keys
9476 `mode-line', `vertical-line', and `vertical-scroll-bar'.
9478 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this
9479 function will process a switch-frame event if the user switches frames
9480 before typing anything. If the user switches frames in the middle of a
9481 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME
9482 is nil, then the event will be put off until after the current key sequence.
9484 `read-key-sequence' checks `function-key-map' for function key
9485 sequences, where they wouldn't conflict with ordinary bindings. See
9486 `function-key-map' for more details.
9488 The optional fifth argument COMMAND-LOOP, if non-nil, means
9489 that this key sequence is being read by something that will
9490 read commands one after another. It should be nil if the caller
9491 will read just one key sequence. */)
9492 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
9493 command_loop)
9494 Lisp_Object prompt, continue_echo, dont_downcase_last;
9495 Lisp_Object can_return_switch_frame, command_loop;
9497 Lisp_Object keybuf[30];
9498 register int i;
9499 struct gcpro gcpro1;
9500 int count = SPECPDL_INDEX ();
9502 if (!NILP (prompt))
9503 CHECK_STRING (prompt);
9504 QUIT;
9506 specbind (Qinput_method_exit_on_first_char,
9507 (NILP (command_loop) ? Qt : Qnil));
9508 specbind (Qinput_method_use_echo_area,
9509 (NILP (command_loop) ? Qt : Qnil));
9511 bzero (keybuf, sizeof keybuf);
9512 GCPRO1 (keybuf[0]);
9513 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
9515 if (NILP (continue_echo))
9517 this_command_key_count = 0;
9518 this_command_key_count_reset = 0;
9519 this_single_command_key_start = 0;
9522 #ifdef HAVE_X_WINDOWS
9523 if (display_hourglass_p)
9524 cancel_hourglass ();
9525 #endif
9527 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
9528 prompt, ! NILP (dont_downcase_last),
9529 ! NILP (can_return_switch_frame), 0);
9531 #if 0 /* The following is fine for code reading a key sequence and
9532 then proceeding with a lenghty computation, but it's not good
9533 for code reading keys in a loop, like an input method. */
9534 #ifdef HAVE_X_WINDOWS
9535 if (display_hourglass_p)
9536 start_hourglass ();
9537 #endif
9538 #endif
9540 if (i == -1)
9542 Vquit_flag = Qt;
9543 QUIT;
9545 UNGCPRO;
9546 return unbind_to (count, make_event_array (i, keybuf));
9549 DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
9550 Sread_key_sequence_vector, 1, 5, 0,
9551 doc: /* Like `read-key-sequence' but always return a vector. */)
9552 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
9553 command_loop)
9554 Lisp_Object prompt, continue_echo, dont_downcase_last;
9555 Lisp_Object can_return_switch_frame, command_loop;
9557 Lisp_Object keybuf[30];
9558 register int i;
9559 struct gcpro gcpro1;
9560 int count = SPECPDL_INDEX ();
9562 if (!NILP (prompt))
9563 CHECK_STRING (prompt);
9564 QUIT;
9566 specbind (Qinput_method_exit_on_first_char,
9567 (NILP (command_loop) ? Qt : Qnil));
9568 specbind (Qinput_method_use_echo_area,
9569 (NILP (command_loop) ? Qt : Qnil));
9571 bzero (keybuf, sizeof keybuf);
9572 GCPRO1 (keybuf[0]);
9573 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
9575 if (NILP (continue_echo))
9577 this_command_key_count = 0;
9578 this_command_key_count_reset = 0;
9579 this_single_command_key_start = 0;
9582 #ifdef HAVE_X_WINDOWS
9583 if (display_hourglass_p)
9584 cancel_hourglass ();
9585 #endif
9587 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
9588 prompt, ! NILP (dont_downcase_last),
9589 ! NILP (can_return_switch_frame), 0);
9591 #ifdef HAVE_X_WINDOWS
9592 if (display_hourglass_p)
9593 start_hourglass ();
9594 #endif
9596 if (i == -1)
9598 Vquit_flag = Qt;
9599 QUIT;
9601 UNGCPRO;
9602 return unbind_to (count, Fvector (i, keybuf));
9605 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
9606 doc: /* Execute CMD as an editor command.
9607 CMD must be a symbol that satisfies the `commandp' predicate.
9608 Optional second arg RECORD-FLAG non-nil
9609 means unconditionally put this command in `command-history'.
9610 Otherwise, that is done only if an arg is read using the minibuffer.
9611 The argument KEYS specifies the value to use instead of (this-command-keys)
9612 when reading the arguments; if it is nil, (this-command-keys) is used.
9613 The argument SPECIAL, if non-nil, means that this command is executing
9614 a special event, so ignore the prefix argument and don't clear it. */)
9615 (cmd, record_flag, keys, special)
9616 Lisp_Object cmd, record_flag, keys, special;
9618 register Lisp_Object final;
9619 register Lisp_Object tem;
9620 Lisp_Object prefixarg;
9621 struct backtrace backtrace;
9622 extern int debug_on_next_call;
9624 debug_on_next_call = 0;
9626 if (NILP (special))
9628 prefixarg = current_kboard->Vprefix_arg;
9629 Vcurrent_prefix_arg = prefixarg;
9630 current_kboard->Vprefix_arg = Qnil;
9632 else
9633 prefixarg = Qnil;
9635 if (SYMBOLP (cmd))
9637 tem = Fget (cmd, Qdisabled);
9638 if (!NILP (tem) && !NILP (Vrun_hooks))
9640 tem = Fsymbol_value (Qdisabled_command_hook);
9641 if (!NILP (tem))
9642 return call1 (Vrun_hooks, Qdisabled_command_hook);
9646 while (1)
9648 final = Findirect_function (cmd);
9650 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
9652 struct gcpro gcpro1, gcpro2;
9654 GCPRO2 (cmd, prefixarg);
9655 do_autoload (final, cmd);
9656 UNGCPRO;
9658 else
9659 break;
9662 if (STRINGP (final) || VECTORP (final))
9664 /* If requested, place the macro in the command history. For
9665 other sorts of commands, call-interactively takes care of
9666 this. */
9667 if (!NILP (record_flag))
9669 Vcommand_history
9670 = Fcons (Fcons (Qexecute_kbd_macro,
9671 Fcons (final, Fcons (prefixarg, Qnil))),
9672 Vcommand_history);
9674 /* Don't keep command history around forever. */
9675 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
9677 tem = Fnthcdr (Vhistory_length, Vcommand_history);
9678 if (CONSP (tem))
9679 XSETCDR (tem, Qnil);
9683 return Fexecute_kbd_macro (final, prefixarg, Qnil);
9686 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
9688 backtrace.next = backtrace_list;
9689 backtrace_list = &backtrace;
9690 backtrace.function = &Qcall_interactively;
9691 backtrace.args = &cmd;
9692 backtrace.nargs = 1;
9693 backtrace.evalargs = 0;
9694 backtrace.debug_on_exit = 0;
9696 tem = Fcall_interactively (cmd, record_flag, keys);
9698 backtrace_list = backtrace.next;
9699 return tem;
9701 return Qnil;
9706 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
9707 1, 1, "P",
9708 doc: /* Read function name, then read its arguments and call it. */)
9709 (prefixarg)
9710 Lisp_Object prefixarg;
9712 Lisp_Object function;
9713 char buf[40];
9714 int saved_last_point_position;
9715 Lisp_Object saved_keys, saved_last_point_position_buffer;
9716 Lisp_Object bindings, value;
9717 struct gcpro gcpro1, gcpro2, gcpro3;
9719 saved_keys = Fvector (this_command_key_count,
9720 XVECTOR (this_command_keys)->contents);
9721 saved_last_point_position_buffer = last_point_position_buffer;
9722 saved_last_point_position = last_point_position;
9723 buf[0] = 0;
9724 GCPRO3 (saved_keys, prefixarg, saved_last_point_position_buffer);
9726 if (EQ (prefixarg, Qminus))
9727 strcpy (buf, "- ");
9728 else if (CONSP (prefixarg) && XINT (XCAR (prefixarg)) == 4)
9729 strcpy (buf, "C-u ");
9730 else if (CONSP (prefixarg) && INTEGERP (XCAR (prefixarg)))
9731 sprintf (buf, "%ld ", (long) XINT (XCAR (prefixarg)));
9732 else if (INTEGERP (prefixarg))
9733 sprintf (buf, "%ld ", (long) XINT (prefixarg));
9735 /* This isn't strictly correct if execute-extended-command
9736 is bound to anything else. Perhaps it should use
9737 this_command_keys? */
9738 strcat (buf, "M-x ");
9740 /* Prompt with buf, and then read a string, completing from and
9741 restricting to the set of all defined commands. Don't provide
9742 any initial input. Save the command read on the extended-command
9743 history list. */
9744 function = Fcompleting_read (build_string (buf),
9745 Vobarray, Qcommandp,
9746 Qt, Qnil, Qextended_command_history, Qnil,
9747 Qnil);
9749 if (STRINGP (function) && SCHARS (function) == 0)
9750 error ("No command name given");
9752 /* Set this_command_keys to the concatenation of saved_keys and
9753 function, followed by a RET. */
9755 Lisp_Object *keys;
9756 int i;
9758 this_command_key_count = 0;
9759 this_command_key_count_reset = 0;
9760 this_single_command_key_start = 0;
9762 keys = XVECTOR (saved_keys)->contents;
9763 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
9764 add_command_key (keys[i]);
9766 for (i = 0; i < SCHARS (function); i++)
9767 add_command_key (Faref (function, make_number (i)));
9769 add_command_key (make_number ('\015'));
9772 last_point_position = saved_last_point_position;
9773 last_point_position_buffer = saved_last_point_position_buffer;
9775 UNGCPRO;
9777 function = Fintern (function, Qnil);
9778 current_kboard->Vprefix_arg = prefixarg;
9779 Vthis_command = function;
9780 real_this_command = function;
9782 /* If enabled, show which key runs this command. */
9783 if (!NILP (Vsuggest_key_bindings)
9784 && NILP (Vexecuting_macro)
9785 && SYMBOLP (function))
9786 bindings = Fwhere_is_internal (function, Voverriding_local_map,
9787 Qt, Qnil, Qnil);
9788 else
9789 bindings = Qnil;
9791 value = Qnil;
9792 GCPRO2 (bindings, value);
9793 value = Fcommand_execute (function, Qt, Qnil, Qnil);
9795 /* If the command has a key binding, print it now. */
9796 if (!NILP (bindings)
9797 && ! (VECTORP (bindings) && EQ (Faref (bindings, make_number (0)),
9798 Qmouse_movement)))
9800 /* But first wait, and skip the message if there is input. */
9801 int delay_time;
9802 if (!NILP (echo_area_buffer[0]))
9803 /* This command displayed something in the echo area;
9804 so wait a few seconds, then display our suggestion message. */
9805 delay_time = (NUMBERP (Vsuggest_key_bindings)
9806 ? XINT (Vsuggest_key_bindings) : 2);
9807 else
9808 /* This command left the echo area empty,
9809 so display our message immediately. */
9810 delay_time = 0;
9812 if (!NILP (Fsit_for (make_number (delay_time), Qnil, Qnil))
9813 && ! CONSP (Vunread_command_events))
9815 Lisp_Object binding;
9816 char *newmessage;
9817 int message_p = push_message ();
9818 int count = SPECPDL_INDEX ();
9820 record_unwind_protect (pop_message_unwind, Qnil);
9821 binding = Fkey_description (bindings, Qnil);
9823 newmessage
9824 = (char *) alloca (SCHARS (SYMBOL_NAME (function))
9825 + SBYTES (binding)
9826 + 100);
9827 sprintf (newmessage, "You can run the command `%s' with %s",
9828 SDATA (SYMBOL_NAME (function)),
9829 SDATA (binding));
9830 message2_nolog (newmessage,
9831 strlen (newmessage),
9832 STRING_MULTIBYTE (binding));
9833 if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
9834 ? Vsuggest_key_bindings : make_number (2)),
9835 Qnil, Qnil))
9836 && message_p)
9837 restore_message ();
9839 unbind_to (count, Qnil);
9843 RETURN_UNGCPRO (value);
9847 /* Return nonzero if input events are pending. */
9850 detect_input_pending ()
9852 if (!input_pending)
9853 get_input_pending (&input_pending, 0);
9855 return input_pending;
9858 /* Return nonzero if input events are pending, and run any pending timers. */
9861 detect_input_pending_run_timers (do_display)
9862 int do_display;
9864 int old_timers_run = timers_run;
9866 if (!input_pending)
9867 get_input_pending (&input_pending, 1);
9869 if (old_timers_run != timers_run && do_display)
9871 redisplay_preserve_echo_area (8);
9872 /* The following fixes a bug when using lazy-lock with
9873 lazy-lock-defer-on-the-fly set to t, i.e. when fontifying
9874 from an idle timer function. The symptom of the bug is that
9875 the cursor sometimes doesn't become visible until the next X
9876 event is processed. --gerd. */
9877 if (rif)
9878 rif->flush_display (NULL);
9881 return input_pending;
9884 /* This is called in some cases before a possible quit.
9885 It cases the next call to detect_input_pending to recompute input_pending.
9886 So calling this function unnecessarily can't do any harm. */
9888 void
9889 clear_input_pending ()
9891 input_pending = 0;
9894 /* Return nonzero if there are pending requeued events.
9895 This isn't used yet. The hope is to make wait_reading_process_input
9896 call it, and return if it runs Lisp code that unreads something.
9897 The problem is, kbd_buffer_get_event needs to be fixed to know what
9898 to do in that case. It isn't trivial. */
9901 requeued_events_pending_p ()
9903 return (!NILP (Vunread_command_events) || unread_command_char != -1);
9907 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
9908 doc: /* Return t if command input is currently available with no wait.
9909 Actually, the value is nil only if we can be sure that no input is available;
9910 if there is a doubt, the value is t. */)
9913 if (!NILP (Vunread_command_events) || unread_command_char != -1)
9914 return (Qt);
9916 get_filtered_input_pending (&input_pending, 1, 1);
9917 return input_pending > 0 ? Qt : Qnil;
9920 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
9921 doc: /* Return vector of last 100 events, not counting those from keyboard macros. */)
9924 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
9925 Lisp_Object val;
9927 if (total_keys < NUM_RECENT_KEYS)
9928 return Fvector (total_keys, keys);
9929 else
9931 val = Fvector (NUM_RECENT_KEYS, keys);
9932 bcopy (keys + recent_keys_index,
9933 XVECTOR (val)->contents,
9934 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
9935 bcopy (keys,
9936 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
9937 recent_keys_index * sizeof (Lisp_Object));
9938 return val;
9942 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
9943 doc: /* Return the key sequence that invoked this command.
9944 However, if the command has called `read-key-sequence', it returns
9945 the last key sequence that has been read.
9946 The value is a string or a vector. */)
9949 return make_event_array (this_command_key_count,
9950 XVECTOR (this_command_keys)->contents);
9953 DEFUN ("this-command-keys-vector", Fthis_command_keys_vector, Sthis_command_keys_vector, 0, 0, 0,
9954 doc: /* Return the key sequence that invoked this command, as a vector.
9955 However, if the command has called `read-key-sequence', it returns
9956 the last key sequence that has been read. */)
9959 return Fvector (this_command_key_count,
9960 XVECTOR (this_command_keys)->contents);
9963 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
9964 Sthis_single_command_keys, 0, 0, 0,
9965 doc: /* Return the key sequence that invoked this command.
9966 More generally, it returns the last key sequence read, either by
9967 the command loop or by `read-key-sequence'.
9968 Unlike `this-command-keys', this function's value
9969 does not include prefix arguments.
9970 The value is always a vector. */)
9973 return Fvector (this_command_key_count
9974 - this_single_command_key_start,
9975 (XVECTOR (this_command_keys)->contents
9976 + this_single_command_key_start));
9979 DEFUN ("this-single-command-raw-keys", Fthis_single_command_raw_keys,
9980 Sthis_single_command_raw_keys, 0, 0, 0,
9981 doc: /* Return the raw events that were read for this command.
9982 More generally, it returns the last key sequence read, either by
9983 the command loop or by `read-key-sequence'.
9984 Unlike `this-single-command-keys', this function's value
9985 shows the events before all translations (except for input methods).
9986 The value is always a vector. */)
9989 return Fvector (raw_keybuf_count,
9990 (XVECTOR (raw_keybuf)->contents));
9993 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
9994 Sreset_this_command_lengths, 0, 0, 0,
9995 doc: /* Make the unread events replace the last command and echo.
9996 Used in `universal-argument-other-key'.
9998 `universal-argument-other-key' rereads the event just typed.
9999 It then gets translated through `function-key-map'.
10000 The translated event has to replace the real events,
10001 both in the value of (this-command-keys) and in echoing.
10002 To achieve this, `universal-argument-other-key' calls
10003 `reset-this-command-lengths', which discards the record of reading
10004 these events the first time. */)
10007 this_command_key_count = before_command_key_count;
10008 if (this_command_key_count < this_single_command_key_start)
10009 this_single_command_key_start = this_command_key_count;
10011 echo_truncate (before_command_echo_length);
10013 /* Cause whatever we put into unread-command-events
10014 to echo as if it were being freshly read from the keyboard. */
10015 this_command_key_count_reset = 1;
10017 return Qnil;
10020 DEFUN ("clear-this-command-keys", Fclear_this_command_keys,
10021 Sclear_this_command_keys, 0, 1, 0,
10022 doc: /* Clear out the vector that `this-command-keys' returns.
10023 Also clear the record of the last 100 events, unless optional arg
10024 KEEP-RECORD is non-nil. */)
10025 (keep_record)
10026 Lisp_Object keep_record;
10028 int i;
10030 this_command_key_count = 0;
10031 this_command_key_count_reset = 0;
10033 if (NILP (keep_record))
10035 for (i = 0; i < XVECTOR (recent_keys)->size; ++i)
10036 XVECTOR (recent_keys)->contents[i] = Qnil;
10037 total_keys = 0;
10038 recent_keys_index = 0;
10040 return Qnil;
10043 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
10044 doc: /* Return the current depth in recursive edits. */)
10047 Lisp_Object temp;
10048 XSETFASTINT (temp, command_loop_level + minibuf_level);
10049 return temp;
10052 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
10053 "FOpen dribble file: ",
10054 doc: /* Start writing all keyboard characters to a dribble file called FILE.
10055 If FILE is nil, close any open dribble file. */)
10056 (file)
10057 Lisp_Object file;
10059 if (dribble)
10061 fclose (dribble);
10062 dribble = 0;
10064 if (!NILP (file))
10066 file = Fexpand_file_name (file, Qnil);
10067 dribble = fopen (SDATA (file), "w");
10068 if (dribble == 0)
10069 report_file_error ("Opening dribble", Fcons (file, Qnil));
10071 return Qnil;
10074 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
10075 doc: /* Discard the contents of the terminal input buffer.
10076 Also end any kbd macro being defined. */)
10079 if (!NILP (current_kboard->defining_kbd_macro))
10081 /* Discard the last command from the macro. */
10082 Fcancel_kbd_macro_events ();
10083 end_kbd_macro ();
10086 update_mode_lines++;
10088 Vunread_command_events = Qnil;
10089 unread_command_char = -1;
10091 discard_tty_input ();
10093 kbd_fetch_ptr = kbd_store_ptr;
10094 input_pending = 0;
10096 return Qnil;
10099 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
10100 doc: /* Stop Emacs and return to superior process. You can resume later.
10101 If `cannot-suspend' is non-nil, or if the system doesn't support job
10102 control, run a subshell instead.
10104 If optional arg STUFFSTRING is non-nil, its characters are stuffed
10105 to be read as terminal input by Emacs's parent, after suspension.
10107 Before suspending, run the normal hook `suspend-hook'.
10108 After resumption run the normal hook `suspend-resume-hook'.
10110 Some operating systems cannot stop the Emacs process and resume it later.
10111 On such systems, Emacs starts a subshell instead of suspending. */)
10112 (stuffstring)
10113 Lisp_Object stuffstring;
10115 int count = SPECPDL_INDEX ();
10116 int old_height, old_width;
10117 int width, height;
10118 struct gcpro gcpro1;
10120 if (!NILP (stuffstring))
10121 CHECK_STRING (stuffstring);
10123 /* Run the functions in suspend-hook. */
10124 if (!NILP (Vrun_hooks))
10125 call1 (Vrun_hooks, intern ("suspend-hook"));
10127 GCPRO1 (stuffstring);
10128 get_frame_size (&old_width, &old_height);
10129 reset_sys_modes ();
10130 /* sys_suspend can get an error if it tries to fork a subshell
10131 and the system resources aren't available for that. */
10132 record_unwind_protect ((Lisp_Object (*) P_ ((Lisp_Object))) init_sys_modes,
10133 Qnil);
10134 stuff_buffered_input (stuffstring);
10135 if (cannot_suspend)
10136 sys_subshell ();
10137 else
10138 sys_suspend ();
10139 unbind_to (count, Qnil);
10141 /* Check if terminal/window size has changed.
10142 Note that this is not useful when we are running directly
10143 with a window system; but suspend should be disabled in that case. */
10144 get_frame_size (&width, &height);
10145 if (width != old_width || height != old_height)
10146 change_frame_size (SELECTED_FRAME (), height, width, 0, 0, 0);
10148 /* Run suspend-resume-hook. */
10149 if (!NILP (Vrun_hooks))
10150 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
10152 UNGCPRO;
10153 return Qnil;
10156 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
10157 Then in any case stuff anything Emacs has read ahead and not used. */
10159 void
10160 stuff_buffered_input (stuffstring)
10161 Lisp_Object stuffstring;
10163 /* stuff_char works only in BSD, versions 4.2 and up. */
10164 #ifdef BSD_SYSTEM
10165 #ifndef BSD4_1
10166 register unsigned char *p;
10168 if (STRINGP (stuffstring))
10170 register int count;
10172 p = SDATA (stuffstring);
10173 count = SBYTES (stuffstring);
10174 while (count-- > 0)
10175 stuff_char (*p++);
10176 stuff_char ('\n');
10179 /* Anything we have read ahead, put back for the shell to read. */
10180 /* ?? What should this do when we have multiple keyboards??
10181 Should we ignore anything that was typed in at the "wrong" kboard? */
10182 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
10185 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
10186 kbd_fetch_ptr = kbd_buffer;
10187 if (kbd_fetch_ptr->kind == ASCII_KEYSTROKE_EVENT)
10188 stuff_char (kbd_fetch_ptr->code);
10190 clear_event (kbd_fetch_ptr);
10193 input_pending = 0;
10194 #endif
10195 #endif /* BSD_SYSTEM and not BSD4_1 */
10198 void
10199 set_waiting_for_input (time_to_clear)
10200 EMACS_TIME *time_to_clear;
10202 input_available_clear_time = time_to_clear;
10204 /* Tell interrupt_signal to throw back to read_char, */
10205 waiting_for_input = 1;
10207 /* If interrupt_signal was called before and buffered a C-g,
10208 make it run again now, to avoid timing error. */
10209 if (!NILP (Vquit_flag))
10210 quit_throw_to_read_char ();
10213 void
10214 clear_waiting_for_input ()
10216 /* Tell interrupt_signal not to throw back to read_char, */
10217 waiting_for_input = 0;
10218 input_available_clear_time = 0;
10221 /* This routine is called at interrupt level in response to C-g.
10223 If interrupt_input, this is the handler for SIGINT. Otherwise, it
10224 is called from kbd_buffer_store_event, in handling SIGIO or
10225 SIGTINT.
10227 If `waiting_for_input' is non zero, then unless `echoing' is
10228 nonzero, immediately throw back to read_char.
10230 Otherwise it sets the Lisp variable quit-flag not-nil. This causes
10231 eval to throw, when it gets a chance. If quit-flag is already
10232 non-nil, it stops the job right away. */
10234 static SIGTYPE
10235 interrupt_signal (signalnum) /* If we don't have an argument, */
10236 int signalnum; /* some compilers complain in signal calls. */
10238 char c;
10239 /* Must preserve main program's value of errno. */
10240 int old_errno = errno;
10241 struct frame *sf = SELECTED_FRAME ();
10243 #if defined (USG) && !defined (POSIX_SIGNALS)
10244 if (!read_socket_hook && NILP (Vwindow_system))
10246 /* USG systems forget handlers when they are used;
10247 must reestablish each time */
10248 signal (SIGINT, interrupt_signal);
10249 signal (SIGQUIT, interrupt_signal);
10251 #endif /* USG */
10253 cancel_echoing ();
10255 if (!NILP (Vquit_flag)
10256 && (FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf)))
10258 /* If SIGINT isn't blocked, don't let us be interrupted by
10259 another SIGINT, it might be harmful due to non-reentrancy
10260 in I/O functions. */
10261 sigblock (sigmask (SIGINT));
10263 fflush (stdout);
10264 reset_sys_modes ();
10266 #ifdef SIGTSTP /* Support possible in later USG versions */
10268 * On systems which can suspend the current process and return to the original
10269 * shell, this command causes the user to end up back at the shell.
10270 * The "Auto-save" and "Abort" questions are not asked until
10271 * the user elects to return to emacs, at which point he can save the current
10272 * job and either dump core or continue.
10274 sys_suspend ();
10275 #else
10276 #ifdef VMS
10277 if (sys_suspend () == -1)
10279 printf ("Not running as a subprocess;\n");
10280 printf ("you can continue or abort.\n");
10282 #else /* not VMS */
10283 /* Perhaps should really fork an inferior shell?
10284 But that would not provide any way to get back
10285 to the original shell, ever. */
10286 printf ("No support for stopping a process on this operating system;\n");
10287 printf ("you can continue or abort.\n");
10288 #endif /* not VMS */
10289 #endif /* not SIGTSTP */
10290 #ifdef MSDOS
10291 /* We must remain inside the screen area when the internal terminal
10292 is used. Note that [Enter] is not echoed by dos. */
10293 cursor_to (0, 0);
10294 #endif
10295 /* It doesn't work to autosave while GC is in progress;
10296 the code used for auto-saving doesn't cope with the mark bit. */
10297 if (!gc_in_progress)
10299 printf ("Auto-save? (y or n) ");
10300 fflush (stdout);
10301 if (((c = getchar ()) & ~040) == 'Y')
10303 Fdo_auto_save (Qt, Qnil);
10304 #ifdef MSDOS
10305 printf ("\r\nAuto-save done");
10306 #else /* not MSDOS */
10307 printf ("Auto-save done\n");
10308 #endif /* not MSDOS */
10310 while (c != '\n') c = getchar ();
10312 else
10314 /* During GC, it must be safe to reenable quitting again. */
10315 Vinhibit_quit = Qnil;
10316 #ifdef MSDOS
10317 printf ("\r\n");
10318 #endif /* not MSDOS */
10319 printf ("Garbage collection in progress; cannot auto-save now\r\n");
10320 printf ("but will instead do a real quit after garbage collection ends\r\n");
10321 fflush (stdout);
10324 #ifdef MSDOS
10325 printf ("\r\nAbort? (y or n) ");
10326 #else /* not MSDOS */
10327 #ifdef VMS
10328 printf ("Abort (and enter debugger)? (y or n) ");
10329 #else /* not VMS */
10330 printf ("Abort (and dump core)? (y or n) ");
10331 #endif /* not VMS */
10332 #endif /* not MSDOS */
10333 fflush (stdout);
10334 if (((c = getchar ()) & ~040) == 'Y')
10335 abort ();
10336 while (c != '\n') c = getchar ();
10337 #ifdef MSDOS
10338 printf ("\r\nContinuing...\r\n");
10339 #else /* not MSDOS */
10340 printf ("Continuing...\n");
10341 #endif /* not MSDOS */
10342 fflush (stdout);
10343 init_sys_modes ();
10344 sigfree ();
10346 else
10348 /* If executing a function that wants to be interrupted out of
10349 and the user has not deferred quitting by binding `inhibit-quit'
10350 then quit right away. */
10351 if (immediate_quit && NILP (Vinhibit_quit))
10353 struct gl_state_s saved;
10354 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
10356 immediate_quit = 0;
10357 sigfree ();
10358 saved = gl_state;
10359 GCPRO4 (saved.object, saved.global_code,
10360 saved.current_syntax_table, saved.old_prop);
10361 Fsignal (Qquit, Qnil);
10362 gl_state = saved;
10363 UNGCPRO;
10365 else
10366 /* Else request quit when it's safe */
10367 Vquit_flag = Qt;
10370 if (waiting_for_input && !echoing)
10371 quit_throw_to_read_char ();
10373 errno = old_errno;
10376 /* Handle a C-g by making read_char return C-g. */
10378 void
10379 quit_throw_to_read_char ()
10381 sigfree ();
10382 /* Prevent another signal from doing this before we finish. */
10383 clear_waiting_for_input ();
10384 input_pending = 0;
10386 Vunread_command_events = Qnil;
10387 unread_command_char = -1;
10389 #if 0 /* Currently, sit_for is called from read_char without turning
10390 off polling. And that can call set_waiting_for_input.
10391 It seems to be harmless. */
10392 #ifdef POLL_FOR_INPUT
10393 /* May be > 1 if in recursive minibuffer. */
10394 if (poll_suppress_count == 0)
10395 abort ();
10396 #endif
10397 #endif
10398 if (FRAMEP (internal_last_event_frame)
10399 && !EQ (internal_last_event_frame, selected_frame))
10400 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
10401 0, 0);
10403 _longjmp (getcjmp, 1);
10406 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
10407 doc: /* Set mode of reading keyboard input.
10408 First arg INTERRUPT non-nil means use input interrupts;
10409 nil means use CBREAK mode.
10410 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal
10411 (no effect except in CBREAK mode).
10412 Third arg META t means accept 8-bit input (for a Meta key).
10413 META nil means ignore the top bit, on the assumption it is parity.
10414 Otherwise, accept 8-bit input and don't use the top bit for Meta.
10415 Optional fourth arg QUIT if non-nil specifies character to use for quitting.
10416 See also `current-input-mode'. */)
10417 (interrupt, flow, meta, quit)
10418 Lisp_Object interrupt, flow, meta, quit;
10420 if (!NILP (quit)
10421 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
10422 error ("set-input-mode: QUIT must be an ASCII character");
10424 #ifdef POLL_FOR_INPUT
10425 stop_polling ();
10426 #endif
10428 #ifndef DOS_NT
10429 /* this causes startup screen to be restored and messes with the mouse */
10430 reset_sys_modes ();
10431 #endif
10433 #ifdef SIGIO
10434 /* Note SIGIO has been undef'd if FIONREAD is missing. */
10435 if (read_socket_hook)
10437 /* When using X, don't give the user a real choice,
10438 because we haven't implemented the mechanisms to support it. */
10439 #ifdef NO_SOCK_SIGIO
10440 interrupt_input = 0;
10441 #else /* not NO_SOCK_SIGIO */
10442 interrupt_input = 1;
10443 #endif /* NO_SOCK_SIGIO */
10445 else
10446 interrupt_input = !NILP (interrupt);
10447 #else /* not SIGIO */
10448 interrupt_input = 0;
10449 #endif /* not SIGIO */
10451 /* Our VMS input only works by interrupts, as of now. */
10452 #ifdef VMS
10453 interrupt_input = 1;
10454 #endif
10456 flow_control = !NILP (flow);
10457 if (NILP (meta))
10458 meta_key = 0;
10459 else if (EQ (meta, Qt))
10460 meta_key = 1;
10461 else
10462 meta_key = 2;
10463 if (!NILP (quit))
10464 /* Don't let this value be out of range. */
10465 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
10467 #ifndef DOS_NT
10468 init_sys_modes ();
10469 #endif
10471 #ifdef POLL_FOR_INPUT
10472 poll_suppress_count = 1;
10473 start_polling ();
10474 #endif
10475 return Qnil;
10478 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
10479 doc: /* Return information about the way Emacs currently reads keyboard input.
10480 The value is a list of the form (INTERRUPT FLOW META QUIT), where
10481 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if
10482 nil, Emacs is using CBREAK mode.
10483 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the
10484 terminal; this does not apply if Emacs uses interrupt-driven input.
10485 META is t if accepting 8-bit input with 8th bit as Meta flag.
10486 META nil means ignoring the top bit, on the assumption it is parity.
10487 META is neither t nor nil if accepting 8-bit input and using
10488 all 8 bits as the character code.
10489 QUIT is the character Emacs currently uses to quit.
10490 The elements of this list correspond to the arguments of
10491 `set-input-mode'. */)
10494 Lisp_Object val[4];
10496 val[0] = interrupt_input ? Qt : Qnil;
10497 val[1] = flow_control ? Qt : Qnil;
10498 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
10499 XSETFASTINT (val[3], quit_char);
10501 return Flist (sizeof (val) / sizeof (val[0]), val);
10504 DEFUN ("posn-at-x-y", Fposn_at_x_y, Sposn_at_x_y, 2, 3, 0,
10505 doc: /* Return position information for pixel coordinates X and Y.
10506 By default, X and Y are relative to text area of the selected window.
10507 Optional third arg FRAME_OR_WINDOW non-nil specifies frame or window.
10509 The return value is similar to a mouse click position:
10510 (WINDOW AREA-OR-POS (X . Y) TIMESTAMP OBJECT POS (COL . ROW)
10511 IMAGE (DX . DY) (WIDTH . HEIGHT))
10512 The `posn-' functions access elements of such lists. */)
10513 (x, y, frame_or_window)
10514 Lisp_Object x, y, frame_or_window;
10516 if (NILP (frame_or_window))
10517 frame_or_window = selected_window;
10519 if (WINDOWP (frame_or_window))
10521 struct window *w;
10523 CHECK_LIVE_WINDOW (frame_or_window);
10525 w = XWINDOW (frame_or_window);
10526 XSETINT (x, (WINDOW_TO_FRAME_PIXEL_X (w, XINT (x))
10527 + window_box_left_offset (w, TEXT_AREA)));
10528 XSETINT (y, WINDOW_TO_FRAME_PIXEL_Y (w, XINT (y)));
10529 frame_or_window = w->frame;
10532 CHECK_LIVE_FRAME (frame_or_window);
10534 return make_lispy_position (XFRAME (frame_or_window), &x, &y, 0);
10537 DEFUN ("posn-at-point", Fposn_at_point, Sposn_at_point, 0, 2, 0,
10538 doc: /* Return position information for buffer POS in WINDOW.
10539 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
10541 Return nil if position is not visible in window. Otherwise,
10542 the return value is similar to that returned by `event-start' for
10543 a mouse click at the upper left corner of the glyph corresponding
10544 to the given buffer position:
10545 (WINDOW AREA-OR-POS (X . Y) TIMESTAMP OBJECT POS (COL . ROW)
10546 IMAGE (DX . DY) (WIDTH . HEIGHT))
10547 The `posn-' functions access elements of such lists. */*/)
10548 (pos, window)
10549 Lisp_Object pos, window;
10551 Lisp_Object tem;
10553 tem = Fpos_visible_in_window_p (pos, window, Qt);
10554 if (!NILP (tem))
10555 tem = Fposn_at_x_y (XCAR (tem), XCAR (XCDR (tem)), window);
10556 return tem;
10561 * Set up a new kboard object with reasonable initial values.
10563 void
10564 init_kboard (kb)
10565 KBOARD *kb;
10567 kb->Voverriding_terminal_local_map = Qnil;
10568 kb->Vlast_command = Qnil;
10569 kb->Vreal_last_command = Qnil;
10570 kb->Vprefix_arg = Qnil;
10571 kb->Vlast_prefix_arg = Qnil;
10572 kb->kbd_queue = Qnil;
10573 kb->kbd_queue_has_data = 0;
10574 kb->immediate_echo = 0;
10575 kb->echo_string = Qnil;
10576 kb->echo_after_prompt = -1;
10577 kb->kbd_macro_buffer = 0;
10578 kb->kbd_macro_bufsize = 0;
10579 kb->defining_kbd_macro = Qnil;
10580 kb->Vlast_kbd_macro = Qnil;
10581 kb->reference_count = 0;
10582 kb->Vsystem_key_alist = Qnil;
10583 kb->system_key_syms = Qnil;
10584 kb->Vdefault_minibuffer_frame = Qnil;
10588 * Destroy the contents of a kboard object, but not the object itself.
10589 * We use this just before deleting it, or if we're going to initialize
10590 * it a second time.
10592 static void
10593 wipe_kboard (kb)
10594 KBOARD *kb;
10596 if (kb->kbd_macro_buffer)
10597 xfree (kb->kbd_macro_buffer);
10600 #ifdef MULTI_KBOARD
10602 /* Free KB and memory referenced from it. */
10604 void
10605 delete_kboard (kb)
10606 KBOARD *kb;
10608 KBOARD **kbp;
10610 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
10611 if (*kbp == NULL)
10612 abort ();
10613 *kbp = kb->next_kboard;
10615 /* Prevent a dangling reference to KB. */
10616 if (kb == current_kboard
10617 && FRAMEP (selected_frame)
10618 && FRAME_LIVE_P (XFRAME (selected_frame)))
10620 current_kboard = XFRAME (selected_frame)->kboard;
10621 if (current_kboard == kb)
10622 abort ();
10625 wipe_kboard (kb);
10626 xfree (kb);
10629 #endif /* MULTI_KBOARD */
10631 void
10632 init_keyboard ()
10634 /* This is correct before outermost invocation of the editor loop */
10635 command_loop_level = -1;
10636 immediate_quit = 0;
10637 quit_char = Ctl ('g');
10638 Vunread_command_events = Qnil;
10639 unread_command_char = -1;
10640 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
10641 total_keys = 0;
10642 recent_keys_index = 0;
10643 kbd_fetch_ptr = kbd_buffer;
10644 kbd_store_ptr = kbd_buffer;
10645 #ifdef HAVE_MOUSE
10646 do_mouse_tracking = Qnil;
10647 #endif
10648 input_pending = 0;
10650 /* This means that command_loop_1 won't try to select anything the first
10651 time through. */
10652 internal_last_event_frame = Qnil;
10653 Vlast_event_frame = internal_last_event_frame;
10655 #ifdef MULTI_KBOARD
10656 current_kboard = initial_kboard;
10657 #endif
10658 wipe_kboard (current_kboard);
10659 init_kboard (current_kboard);
10661 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
10663 signal (SIGINT, interrupt_signal);
10664 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
10665 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
10666 SIGQUIT and we can't tell which one it will give us. */
10667 signal (SIGQUIT, interrupt_signal);
10668 #endif /* HAVE_TERMIO */
10670 /* Note SIGIO has been undef'd if FIONREAD is missing. */
10671 #ifdef SIGIO
10672 if (!noninteractive)
10673 signal (SIGIO, input_available_signal);
10674 #endif /* SIGIO */
10676 /* Use interrupt input by default, if it works and noninterrupt input
10677 has deficiencies. */
10679 #ifdef INTERRUPT_INPUT
10680 interrupt_input = 1;
10681 #else
10682 interrupt_input = 0;
10683 #endif
10685 /* Our VMS input only works by interrupts, as of now. */
10686 #ifdef VMS
10687 interrupt_input = 1;
10688 #endif
10690 sigfree ();
10691 dribble = 0;
10693 if (keyboard_init_hook)
10694 (*keyboard_init_hook) ();
10696 #ifdef POLL_FOR_INPUT
10697 poll_suppress_count = 1;
10698 start_polling ();
10699 #endif
10701 #ifdef MAC_OSX
10702 /* At least provide an escape route since C-g doesn't work. */
10703 signal (SIGINT, interrupt_signal);
10704 #endif
10707 /* This type's only use is in syms_of_keyboard, to initialize the
10708 event header symbols and put properties on them. */
10709 struct event_head {
10710 Lisp_Object *var;
10711 char *name;
10712 Lisp_Object *kind;
10715 struct event_head head_table[] = {
10716 {&Qmouse_movement, "mouse-movement", &Qmouse_movement},
10717 {&Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement},
10718 {&Qswitch_frame, "switch-frame", &Qswitch_frame},
10719 {&Qdelete_frame, "delete-frame", &Qdelete_frame},
10720 {&Qiconify_frame, "iconify-frame", &Qiconify_frame},
10721 {&Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible},
10722 /* `select-window' should be handled just like `switch-frame'
10723 in read_key_sequence. */
10724 {&Qselect_window, "select-window", &Qswitch_frame}
10727 void
10728 syms_of_keyboard ()
10730 Vpre_help_message = Qnil;
10731 staticpro (&Vpre_help_message);
10733 Vlispy_mouse_stem = build_string ("mouse");
10734 staticpro (&Vlispy_mouse_stem);
10736 /* Tool-bars. */
10737 QCimage = intern (":image");
10738 staticpro (&QCimage);
10740 staticpro (&Qhelp_echo);
10741 Qhelp_echo = intern ("help-echo");
10743 staticpro (&item_properties);
10744 item_properties = Qnil;
10746 staticpro (&tool_bar_item_properties);
10747 tool_bar_item_properties = Qnil;
10748 staticpro (&tool_bar_items_vector);
10749 tool_bar_items_vector = Qnil;
10751 staticpro (&real_this_command);
10752 real_this_command = Qnil;
10754 Qtimer_event_handler = intern ("timer-event-handler");
10755 staticpro (&Qtimer_event_handler);
10757 Qdisabled_command_hook = intern ("disabled-command-hook");
10758 staticpro (&Qdisabled_command_hook);
10760 Qself_insert_command = intern ("self-insert-command");
10761 staticpro (&Qself_insert_command);
10763 Qforward_char = intern ("forward-char");
10764 staticpro (&Qforward_char);
10766 Qbackward_char = intern ("backward-char");
10767 staticpro (&Qbackward_char);
10769 Qdisabled = intern ("disabled");
10770 staticpro (&Qdisabled);
10772 Qundefined = intern ("undefined");
10773 staticpro (&Qundefined);
10775 Qpre_command_hook = intern ("pre-command-hook");
10776 staticpro (&Qpre_command_hook);
10778 Qpost_command_hook = intern ("post-command-hook");
10779 staticpro (&Qpost_command_hook);
10781 Qpost_command_idle_hook = intern ("post-command-idle-hook");
10782 staticpro (&Qpost_command_idle_hook);
10784 Qdeferred_action_function = intern ("deferred-action-function");
10785 staticpro (&Qdeferred_action_function);
10787 Qcommand_hook_internal = intern ("command-hook-internal");
10788 staticpro (&Qcommand_hook_internal);
10790 Qfunction_key = intern ("function-key");
10791 staticpro (&Qfunction_key);
10792 Qmouse_click = intern ("mouse-click");
10793 staticpro (&Qmouse_click);
10794 #ifdef WINDOWSNT
10795 Qlanguage_change = intern ("language-change");
10796 staticpro (&Qlanguage_change);
10797 #endif
10798 Qdrag_n_drop = intern ("drag-n-drop");
10799 staticpro (&Qdrag_n_drop);
10801 Qsave_session = intern ("save-session");
10802 staticpro(&Qsave_session);
10804 Qusr1_signal = intern ("usr1-signal");
10805 staticpro (&Qusr1_signal);
10806 Qusr2_signal = intern ("usr2-signal");
10807 staticpro (&Qusr2_signal);
10809 Qmenu_enable = intern ("menu-enable");
10810 staticpro (&Qmenu_enable);
10811 Qmenu_alias = intern ("menu-alias");
10812 staticpro (&Qmenu_alias);
10813 QCenable = intern (":enable");
10814 staticpro (&QCenable);
10815 QCvisible = intern (":visible");
10816 staticpro (&QCvisible);
10817 QChelp = intern (":help");
10818 staticpro (&QChelp);
10819 QCfilter = intern (":filter");
10820 staticpro (&QCfilter);
10821 QCbutton = intern (":button");
10822 staticpro (&QCbutton);
10823 QCkeys = intern (":keys");
10824 staticpro (&QCkeys);
10825 QCkey_sequence = intern (":key-sequence");
10826 staticpro (&QCkey_sequence);
10827 QCtoggle = intern (":toggle");
10828 staticpro (&QCtoggle);
10829 QCradio = intern (":radio");
10830 staticpro (&QCradio);
10832 Qmode_line = intern ("mode-line");
10833 staticpro (&Qmode_line);
10834 Qvertical_line = intern ("vertical-line");
10835 staticpro (&Qvertical_line);
10836 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
10837 staticpro (&Qvertical_scroll_bar);
10838 Qmenu_bar = intern ("menu-bar");
10839 staticpro (&Qmenu_bar);
10841 Qabove_handle = intern ("above-handle");
10842 staticpro (&Qabove_handle);
10843 Qhandle = intern ("handle");
10844 staticpro (&Qhandle);
10845 Qbelow_handle = intern ("below-handle");
10846 staticpro (&Qbelow_handle);
10847 Qup = intern ("up");
10848 staticpro (&Qup);
10849 Qdown = intern ("down");
10850 staticpro (&Qdown);
10851 Qtop = intern ("top");
10852 staticpro (&Qtop);
10853 Qbottom = intern ("bottom");
10854 staticpro (&Qbottom);
10855 Qend_scroll = intern ("end-scroll");
10856 staticpro (&Qend_scroll);
10857 Qratio = intern ("ratio");
10858 staticpro (&Qratio);
10860 Qevent_kind = intern ("event-kind");
10861 staticpro (&Qevent_kind);
10862 Qevent_symbol_elements = intern ("event-symbol-elements");
10863 staticpro (&Qevent_symbol_elements);
10864 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
10865 staticpro (&Qevent_symbol_element_mask);
10866 Qmodifier_cache = intern ("modifier-cache");
10867 staticpro (&Qmodifier_cache);
10869 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
10870 staticpro (&Qrecompute_lucid_menubar);
10871 Qactivate_menubar_hook = intern ("activate-menubar-hook");
10872 staticpro (&Qactivate_menubar_hook);
10874 Qpolling_period = intern ("polling-period");
10875 staticpro (&Qpolling_period);
10877 Qinput_method_function = intern ("input-method-function");
10878 staticpro (&Qinput_method_function);
10880 Qinput_method_exit_on_first_char = intern ("input-method-exit-on-first-char");
10881 staticpro (&Qinput_method_exit_on_first_char);
10882 Qinput_method_use_echo_area = intern ("input-method-use-echo-area");
10883 staticpro (&Qinput_method_use_echo_area);
10885 Fset (Qinput_method_exit_on_first_char, Qnil);
10886 Fset (Qinput_method_use_echo_area, Qnil);
10888 last_point_position_buffer = Qnil;
10891 struct event_head *p;
10893 for (p = head_table;
10894 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
10895 p++)
10897 *p->var = intern (p->name);
10898 staticpro (p->var);
10899 Fput (*p->var, Qevent_kind, *p->kind);
10900 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
10904 button_down_location = Fmake_vector (make_number (1), Qnil);
10905 staticpro (&button_down_location);
10906 mouse_syms = Fmake_vector (make_number (1), Qnil);
10907 staticpro (&mouse_syms);
10908 wheel_syms = Fmake_vector (make_number (2), Qnil);
10909 staticpro (&wheel_syms);
10912 int i;
10913 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
10915 modifier_symbols = Fmake_vector (make_number (len), Qnil);
10916 for (i = 0; i < len; i++)
10917 if (modifier_names[i])
10918 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
10919 staticpro (&modifier_symbols);
10922 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
10923 staticpro (&recent_keys);
10925 this_command_keys = Fmake_vector (make_number (40), Qnil);
10926 staticpro (&this_command_keys);
10928 raw_keybuf = Fmake_vector (make_number (30), Qnil);
10929 staticpro (&raw_keybuf);
10931 Qextended_command_history = intern ("extended-command-history");
10932 Fset (Qextended_command_history, Qnil);
10933 staticpro (&Qextended_command_history);
10935 accent_key_syms = Qnil;
10936 staticpro (&accent_key_syms);
10938 func_key_syms = Qnil;
10939 staticpro (&func_key_syms);
10941 drag_n_drop_syms = Qnil;
10942 staticpro (&drag_n_drop_syms);
10944 unread_switch_frame = Qnil;
10945 staticpro (&unread_switch_frame);
10947 internal_last_event_frame = Qnil;
10948 staticpro (&internal_last_event_frame);
10950 read_key_sequence_cmd = Qnil;
10951 staticpro (&read_key_sequence_cmd);
10953 menu_bar_one_keymap_changed_items = Qnil;
10954 staticpro (&menu_bar_one_keymap_changed_items);
10956 defsubr (&Sevent_convert_list);
10957 defsubr (&Sread_key_sequence);
10958 defsubr (&Sread_key_sequence_vector);
10959 defsubr (&Srecursive_edit);
10960 #ifdef HAVE_MOUSE
10961 defsubr (&Strack_mouse);
10962 #endif
10963 defsubr (&Sinput_pending_p);
10964 defsubr (&Scommand_execute);
10965 defsubr (&Srecent_keys);
10966 defsubr (&Sthis_command_keys);
10967 defsubr (&Sthis_command_keys_vector);
10968 defsubr (&Sthis_single_command_keys);
10969 defsubr (&Sthis_single_command_raw_keys);
10970 defsubr (&Sreset_this_command_lengths);
10971 defsubr (&Sclear_this_command_keys);
10972 defsubr (&Ssuspend_emacs);
10973 defsubr (&Sabort_recursive_edit);
10974 defsubr (&Sexit_recursive_edit);
10975 defsubr (&Srecursion_depth);
10976 defsubr (&Stop_level);
10977 defsubr (&Sdiscard_input);
10978 defsubr (&Sopen_dribble_file);
10979 defsubr (&Sset_input_mode);
10980 defsubr (&Scurrent_input_mode);
10981 defsubr (&Sexecute_extended_command);
10982 defsubr (&Sposn_at_point);
10983 defsubr (&Sposn_at_x_y);
10985 DEFVAR_LISP ("last-command-char", &last_command_char,
10986 doc: /* Last input event that was part of a command. */);
10988 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
10989 doc: /* Last input event that was part of a command. */);
10991 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
10992 doc: /* Last input event in a command, except for mouse menu events.
10993 Mouse menus give back keys that don't look like mouse events;
10994 this variable holds the actual mouse event that led to the menu,
10995 so that you can determine whether the command was run by mouse or not. */);
10997 DEFVAR_LISP ("last-input-char", &last_input_char,
10998 doc: /* Last input event. */);
11000 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
11001 doc: /* Last input event. */);
11003 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
11004 doc: /* List of events to be read as the command input.
11005 These events are processed first, before actual keyboard input. */);
11006 Vunread_command_events = Qnil;
11008 DEFVAR_INT ("unread-command-char", &unread_command_char,
11009 doc: /* If not -1, an object to be read as next command input event. */);
11011 DEFVAR_LISP ("unread-post-input-method-events", &Vunread_post_input_method_events,
11012 doc: /* List of events to be processed as input by input methods.
11013 These events are processed after `unread-command-events', but
11014 before actual keyboard input. */);
11015 Vunread_post_input_method_events = Qnil;
11017 DEFVAR_LISP ("unread-input-method-events", &Vunread_input_method_events,
11018 doc: /* List of events to be processed as input by input methods.
11019 These events are processed after `unread-command-events', but
11020 before actual keyboard input. */);
11021 Vunread_input_method_events = Qnil;
11023 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
11024 doc: /* Meta-prefix character code.
11025 Meta-foo as command input turns into this character followed by foo. */);
11026 XSETINT (meta_prefix_char, 033);
11028 DEFVAR_KBOARD ("last-command", Vlast_command,
11029 doc: /* The last command executed.
11030 Normally a symbol with a function definition, but can be whatever was found
11031 in the keymap, or whatever the variable `this-command' was set to by that
11032 command.
11034 The value `mode-exit' is special; it means that the previous command
11035 read an event that told it to exit, and it did so and unread that event.
11036 In other words, the present command is the event that made the previous
11037 command exit.
11039 The value `kill-region' is special; it means that the previous command
11040 was a kill command. */);
11042 DEFVAR_KBOARD ("real-last-command", Vreal_last_command,
11043 doc: /* Same as `last-command', but never altered by Lisp code. */);
11045 DEFVAR_LISP ("this-command", &Vthis_command,
11046 doc: /* The command now being executed.
11047 The command can set this variable; whatever is put here
11048 will be in `last-command' during the following command. */);
11049 Vthis_command = Qnil;
11051 DEFVAR_LISP ("this-original-command", &Vthis_original_command,
11052 doc: /* The command bound to the current key sequence before remapping.
11053 It equals `this-command' if the original command was not remapped through
11054 any of the active keymaps. Otherwise, the value of `this-command' is the
11055 result of looking up the original command in the active keymaps. */);
11056 Vthis_original_command = Qnil;
11058 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
11059 doc: /* *Number of input events between auto-saves.
11060 Zero means disable autosaving due to number of characters typed. */);
11061 auto_save_interval = 300;
11063 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
11064 doc: /* *Number of seconds idle time before auto-save.
11065 Zero or nil means disable auto-saving due to idleness.
11066 After auto-saving due to this many seconds of idle time,
11067 Emacs also does a garbage collection if that seems to be warranted. */);
11068 XSETFASTINT (Vauto_save_timeout, 30);
11070 DEFVAR_LISP ("echo-keystrokes", &Vecho_keystrokes,
11071 doc: /* *Nonzero means echo unfinished commands after this many seconds of pause.
11072 The value may be integer or floating point. */);
11073 Vecho_keystrokes = make_number (1);
11075 DEFVAR_INT ("polling-period", &polling_period,
11076 doc: /* *Interval between polling for input during Lisp execution.
11077 The reason for polling is to make C-g work to stop a running program.
11078 Polling is needed only when using X windows and SIGIO does not work.
11079 Polling is automatically disabled in all other cases. */);
11080 polling_period = 2;
11082 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
11083 doc: /* *Maximum time between mouse clicks to make a double-click.
11084 Measured in milliseconds. nil means disable double-click recognition;
11085 t means double-clicks have no time limit and are detected
11086 by position only. */);
11087 Vdouble_click_time = make_number (500);
11089 DEFVAR_INT ("double-click-fuzz", &double_click_fuzz,
11090 doc: /* *Maximum mouse movement between clicks to make a double-click.
11091 On window-system frames, value is the number of pixels the mouse may have
11092 moved horizontally or vertically between two clicks to make a double-click.
11093 On non window-system frames, value is interpreted in units of 1/8 characters
11094 instead of pixels.
11096 This variable is also the threshold for motion of the mouse
11097 to count as a drag. */);
11098 double_click_fuzz = 3;
11100 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
11101 doc: /* *Non-nil means inhibit local map menu bar menus. */);
11102 inhibit_local_menu_bar_menus = 0;
11104 DEFVAR_INT ("num-input-keys", &num_input_keys,
11105 doc: /* Number of complete key sequences read as input so far.
11106 This includes key sequences read from keyboard macros.
11107 The number is effectively the number of interactive command invocations. */);
11108 num_input_keys = 0;
11110 DEFVAR_INT ("num-nonmacro-input-events", &num_nonmacro_input_events,
11111 doc: /* Number of input events read from the keyboard so far.
11112 This does not include events generated by keyboard macros. */);
11113 num_nonmacro_input_events = 0;
11115 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
11116 doc: /* The frame in which the most recently read event occurred.
11117 If the last event came from a keyboard macro, this is set to `macro'. */);
11118 Vlast_event_frame = Qnil;
11120 /* This variable is set up in sysdep.c. */
11121 DEFVAR_LISP ("tty-erase-char", &Vtty_erase_char,
11122 doc: /* The ERASE character as set by the user with stty. */);
11124 DEFVAR_LISP ("help-char", &Vhelp_char,
11125 doc: /* Character to recognize as meaning Help.
11126 When it is read, do `(eval help-form)', and display result if it's a string.
11127 If the value of `help-form' is nil, this char can be read normally. */);
11128 XSETINT (Vhelp_char, Ctl ('H'));
11130 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
11131 doc: /* List of input events to recognize as meaning Help.
11132 These work just like the value of `help-char' (see that). */);
11133 Vhelp_event_list = Qnil;
11135 DEFVAR_LISP ("help-form", &Vhelp_form,
11136 doc: /* Form to execute when character `help-char' is read.
11137 If the form returns a string, that string is displayed.
11138 If `help-form' is nil, the help char is not recognized. */);
11139 Vhelp_form = Qnil;
11141 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
11142 doc: /* Command to run when `help-char' character follows a prefix key.
11143 This command is used only when there is no actual binding
11144 for that character after that prefix key. */);
11145 Vprefix_help_command = Qnil;
11147 DEFVAR_LISP ("top-level", &Vtop_level,
11148 doc: /* Form to evaluate when Emacs starts up.
11149 Useful to set before you dump a modified Emacs. */);
11150 Vtop_level = Qnil;
11152 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
11153 doc: /* Translate table for keyboard input, or nil.
11154 If non-nil, the value should be a char-table. Each character read
11155 from the keyboard is looked up in this char-table. If the value found
11156 there is non-nil, then it is used instead of the actual input character.
11158 The value can also be a string or vector, but this is considered obsolete.
11159 If it is a string or vector of length N, character codes N and up are left
11160 untranslated. In a vector, an element which is nil means "no translation".
11162 This is applied to the characters supplied to input methods, not their
11163 output. See also `translation-table-for-input'. */);
11164 Vkeyboard_translate_table = Qnil;
11166 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
11167 doc: /* Non-nil means to always spawn a subshell instead of suspending.
11168 \(Even if the operating system has support for stopping a process.\) */);
11169 cannot_suspend = 0;
11171 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
11172 doc: /* Non-nil means prompt with menus when appropriate.
11173 This is done when reading from a keymap that has a prompt string,
11174 for elements that have prompt strings.
11175 The menu is displayed on the screen
11176 if X menus were enabled at configuration
11177 time and the previous event was a mouse click prefix key.
11178 Otherwise, menu prompting uses the echo area. */);
11179 menu_prompting = 1;
11181 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
11182 doc: /* Character to see next line of menu prompt.
11183 Type this character while in a menu prompt to rotate around the lines of it. */);
11184 XSETINT (menu_prompt_more_char, ' ');
11186 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
11187 doc: /* A mask of additional modifier keys to use with every keyboard character.
11188 Emacs applies the modifiers of the character stored here to each keyboard
11189 character it reads. For example, after evaluating the expression
11190 (setq extra-keyboard-modifiers ?\\C-x)
11191 all input characters will have the control modifier applied to them.
11193 Note that the character ?\\C-@, equivalent to the integer zero, does
11194 not count as a control character; rather, it counts as a character
11195 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero
11196 cancels any modification. */);
11197 extra_keyboard_modifiers = 0;
11199 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
11200 doc: /* If an editing command sets this to t, deactivate the mark afterward.
11201 The command loop sets this to nil before each command,
11202 and tests the value when the command returns.
11203 Buffer modification stores t in this variable. */);
11204 Vdeactivate_mark = Qnil;
11206 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
11207 doc: /* Temporary storage of pre-command-hook or post-command-hook. */);
11208 Vcommand_hook_internal = Qnil;
11210 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
11211 doc: /* Normal hook run before each command is executed.
11212 If an unhandled error happens in running this hook,
11213 the hook value is set to nil, since otherwise the error
11214 might happen repeatedly and make Emacs nonfunctional. */);
11215 Vpre_command_hook = Qnil;
11217 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
11218 doc: /* Normal hook run after each command is executed.
11219 If an unhandled error happens in running this hook,
11220 the hook value is set to nil, since otherwise the error
11221 might happen repeatedly and make Emacs nonfunctional. */);
11222 Vpost_command_hook = Qnil;
11224 DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
11225 doc: /* Normal hook run after each command is executed, if idle.
11226 Errors running the hook are caught and ignored. */);
11227 Vpost_command_idle_hook = Qnil;
11229 DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
11230 doc: /* Delay time before running `post-command-idle-hook'.
11231 This is measured in microseconds. */);
11232 post_command_idle_delay = 100000;
11234 #if 0
11235 DEFVAR_LISP ("echo-area-clear-hook", ...,
11236 doc: /* Normal hook run when clearing the echo area. */);
11237 #endif
11238 Qecho_area_clear_hook = intern ("echo-area-clear-hook");
11239 SET_SYMBOL_VALUE (Qecho_area_clear_hook, Qnil);
11241 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
11242 doc: /* Non-nil means menu bar, specified Lucid style, needs to be recomputed. */);
11243 Vlucid_menu_bar_dirty_flag = Qnil;
11245 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
11246 doc: /* List of menu bar items to move to the end of the menu bar.
11247 The elements of the list are event types that may have menu bar bindings. */);
11248 Vmenu_bar_final_items = Qnil;
11250 DEFVAR_KBOARD ("overriding-terminal-local-map",
11251 Voverriding_terminal_local_map,
11252 doc: /* Per-terminal keymap that overrides all other local keymaps.
11253 If this variable is non-nil, it is used as a keymap instead of the
11254 buffer's local map, and the minor mode keymaps and text property keymaps.
11255 This variable is intended to let commands such as `universal-argument'
11256 set up a different keymap for reading the next command. */);
11258 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
11259 doc: /* Keymap that overrides all other local keymaps.
11260 If this variable is non-nil, it is used as a keymap instead of the
11261 buffer's local map, and the minor mode keymaps and text property keymaps. */);
11262 Voverriding_local_map = Qnil;
11264 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
11265 doc: /* Non-nil means `overriding-local-map' applies to the menu bar.
11266 Otherwise, the menu bar continues to reflect the buffer's local map
11267 and the minor mode maps regardless of `overriding-local-map'. */);
11268 Voverriding_local_map_menu_flag = Qnil;
11270 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
11271 doc: /* Keymap defining bindings for special events to execute at low level. */);
11272 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
11274 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
11275 doc: /* *Non-nil means generate motion events for mouse motion. */);
11277 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
11278 doc: /* Alist of system-specific X windows key symbols.
11279 Each element should have the form (N . SYMBOL) where N is the
11280 numeric keysym code (sans the \"system-specific\" bit 1<<28)
11281 and SYMBOL is its name. */);
11283 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
11284 doc: /* List of deferred actions to be performed at a later time.
11285 The precise format isn't relevant here; we just check whether it is nil. */);
11286 Vdeferred_action_list = Qnil;
11288 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
11289 doc: /* Function to call to handle deferred actions, after each command.
11290 This function is called with no arguments after each command
11291 whenever `deferred-action-list' is non-nil. */);
11292 Vdeferred_action_function = Qnil;
11294 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
11295 doc: /* *Non-nil means show the equivalent key-binding when M-x command has one.
11296 The value can be a length of time to show the message for.
11297 If the value is non-nil and not a number, we wait 2 seconds. */);
11298 Vsuggest_key_bindings = Qt;
11300 DEFVAR_LISP ("timer-list", &Vtimer_list,
11301 doc: /* List of active absolute time timers in order of increasing time. */);
11302 Vtimer_list = Qnil;
11304 DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
11305 doc: /* List of active idle-time timers in order of increasing time. */);
11306 Vtimer_idle_list = Qnil;
11308 DEFVAR_LISP ("input-method-function", &Vinput_method_function,
11309 doc: /* If non-nil, the function that implements the current input method.
11310 It's called with one argument, a printing character that was just read.
11311 \(That means a character with code 040...0176.)
11312 Typically this function uses `read-event' to read additional events.
11313 When it does so, it should first bind `input-method-function' to nil
11314 so it will not be called recursively.
11316 The function should return a list of zero or more events
11317 to be used as input. If it wants to put back some events
11318 to be reconsidered, separately, by the input method,
11319 it can add them to the beginning of `unread-command-events'.
11321 The input method function can find in `input-method-previous-method'
11322 the previous echo area message.
11324 The input method function should refer to the variables
11325 `input-method-use-echo-area' and `input-method-exit-on-first-char'
11326 for guidance on what to do. */);
11327 Vinput_method_function = Qnil;
11329 DEFVAR_LISP ("input-method-previous-message",
11330 &Vinput_method_previous_message,
11331 doc: /* When `input-method-function' is called, hold the previous echo area message.
11332 This variable exists because `read-event' clears the echo area
11333 before running the input method. It is nil if there was no message. */);
11334 Vinput_method_previous_message = Qnil;
11336 DEFVAR_LISP ("show-help-function", &Vshow_help_function,
11337 doc: /* If non-nil, the function that implements the display of help.
11338 It's called with one argument, the help string to display. */);
11339 Vshow_help_function = Qnil;
11341 DEFVAR_LISP ("disable-point-adjustment", &Vdisable_point_adjustment,
11342 doc: /* If non-nil, suppress point adjustment after executing a command.
11344 After a command is executed, if point is moved into a region that has
11345 special properties (e.g. composition, display), we adjust point to
11346 the boundary of the region. But, several special commands sets this
11347 variable to non-nil, then we suppress the point adjustment.
11349 This variable is set to nil before reading a command, and is checked
11350 just after executing the command. */);
11351 Vdisable_point_adjustment = Qnil;
11353 DEFVAR_LISP ("global-disable-point-adjustment",
11354 &Vglobal_disable_point_adjustment,
11355 doc: /* *If non-nil, always suppress point adjustment.
11357 The default value is nil, in which case, point adjustment are
11358 suppressed only after special commands that set
11359 `disable-point-adjustment' (which see) to non-nil. */);
11360 Vglobal_disable_point_adjustment = Qnil;
11362 DEFVAR_BOOL ("update-menu-bindings", &update_menu_bindings,
11363 doc: /* Non-nil means updating menu bindings is allowed.
11364 A value of nil means menu bindings should not be updated.
11365 Used during Emacs' startup. */);
11366 update_menu_bindings = 1;
11368 DEFVAR_LISP ("minibuffer-message-timeout", &Vminibuffer_message_timeout,
11369 doc: /* *How long to display an echo-area message when the minibuffer is active.
11370 If the value is not a number, such messages don't time out. */);
11371 Vminibuffer_message_timeout = make_number (2);
11374 void
11375 keys_of_keyboard ()
11377 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
11378 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
11379 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
11380 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
11381 initial_define_key (meta_map, 'x', "execute-extended-command");
11383 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
11384 "handle-delete-frame");
11385 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
11386 "ignore-event");
11387 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
11388 "ignore-event");
11389 /* Handling it at such a low-level causes read_key_sequence to get
11390 * confused because it doesn't realize that the current_buffer was
11391 * changed by read_char.
11393 * initial_define_lispy_key (Vspecial_event_map, "select-window",
11394 * "handle-select-window"); */
11395 initial_define_lispy_key (Vspecial_event_map, "save-session",
11396 "handle-save-session");
11399 /* Mark the pointers in the kboard objects.
11400 Called by the Fgarbage_collector. */
11401 void
11402 mark_kboards ()
11404 KBOARD *kb;
11405 Lisp_Object *p;
11406 for (kb = all_kboards; kb; kb = kb->next_kboard)
11408 if (kb->kbd_macro_buffer)
11409 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
11410 mark_object (*p);
11411 mark_object (kb->Voverriding_terminal_local_map);
11412 mark_object (kb->Vlast_command);
11413 mark_object (kb->Vreal_last_command);
11414 mark_object (kb->Vprefix_arg);
11415 mark_object (kb->Vlast_prefix_arg);
11416 mark_object (kb->kbd_queue);
11417 mark_object (kb->defining_kbd_macro);
11418 mark_object (kb->Vlast_kbd_macro);
11419 mark_object (kb->Vsystem_key_alist);
11420 mark_object (kb->system_key_syms);
11421 mark_object (kb->Vdefault_minibuffer_frame);
11422 mark_object (kb->echo_string);
11425 struct input_event *event;
11426 for (event = kbd_fetch_ptr; event != kbd_store_ptr; event++)
11428 if (event == kbd_buffer + KBD_BUFFER_SIZE)
11429 event = kbd_buffer;
11430 if (event->kind != SELECTION_REQUEST_EVENT)
11432 mark_object (event->x);
11433 mark_object (event->y);
11435 mark_object (event->frame_or_window);
11436 mark_object (event->arg);
11441 /* arch-tag: 774e34d7-6d31-42f3-8397-e079a4e4c9ca
11442 (do not change this comment) */