* bookmark.el (bookmark-bmenu-relocate): New function, as
[emacs.git] / src / keyboard.c
blob042c3108ca8ad83572758c2f8787fc84955737d8
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 /* Install the string STR as the beginning of the string of echoing,
685 so that it serves as a prompt for the next character.
686 Also start echoing. */
688 void
689 echo_prompt (str)
690 Lisp_Object str;
692 current_kboard->echo_string = str;
693 current_kboard->echo_after_prompt = SCHARS (str);
694 echo_now ();
697 /* Add C to the echo string, if echoing is going on.
698 C can be a character, which is printed prettily ("M-C-x" and all that
699 jazz), or a symbol, whose name is printed. */
701 void
702 echo_char (c)
703 Lisp_Object c;
705 if (current_kboard->immediate_echo)
707 int size = KEY_DESCRIPTION_SIZE + 100;
708 char *buffer = (char *) alloca (size);
709 char *ptr = buffer;
710 Lisp_Object echo_string;
712 echo_string = current_kboard->echo_string;
714 /* If someone has passed us a composite event, use its head symbol. */
715 c = EVENT_HEAD (c);
717 if (INTEGERP (c))
719 ptr = push_key_description (XINT (c), ptr, 1);
721 else if (SYMBOLP (c))
723 Lisp_Object name = SYMBOL_NAME (c);
724 int nbytes = SBYTES (name);
726 if (size - (ptr - buffer) < nbytes)
728 int offset = ptr - buffer;
729 size = max (2 * size, size + nbytes);
730 buffer = (char *) alloca (size);
731 ptr = buffer + offset;
734 ptr += copy_text (SDATA (name), ptr, nbytes,
735 STRING_MULTIBYTE (name), 1);
738 if ((NILP (echo_string) || SCHARS (echo_string) == 0)
739 && help_char_p (c))
741 const char *text = " (Type ? for further options)";
742 int len = strlen (text);
744 if (size - (ptr - buffer) < len)
746 int offset = ptr - buffer;
747 size += len;
748 buffer = (char *) alloca (size);
749 ptr = buffer + offset;
752 bcopy (text, ptr, len);
753 ptr += len;
756 /* Replace a dash from echo_dash with a space, otherwise
757 add a space at the end as a separator between keys. */
758 if (STRINGP (echo_string)
759 && SCHARS (echo_string) > 1)
761 Lisp_Object last_char, prev_char, idx;
763 idx = make_number (SCHARS (echo_string) - 2);
764 prev_char = Faref (echo_string, idx);
766 idx = make_number (SCHARS (echo_string) - 1);
767 last_char = Faref (echo_string, idx);
769 /* We test PREV_CHAR to make sure this isn't the echoing
770 of a minus-sign. */
771 if (XINT (last_char) == '-' && XINT (prev_char) != ' ')
772 Faset (echo_string, idx, make_number (' '));
773 else
774 echo_string = concat2 (echo_string, build_string (" "));
777 current_kboard->echo_string
778 = concat2 (echo_string, make_string (buffer, ptr - buffer));
780 echo_now ();
784 /* Temporarily add a dash to the end of the echo string if it's not
785 empty, so that it serves as a mini-prompt for the very next character. */
787 void
788 echo_dash ()
790 /* Do nothing if not echoing at all. */
791 if (NILP (current_kboard->echo_string))
792 return;
794 if (!current_kboard->immediate_echo
795 && SCHARS (current_kboard->echo_string) == 0)
796 return;
798 /* Do nothing if we just printed a prompt. */
799 if (current_kboard->echo_after_prompt
800 == SCHARS (current_kboard->echo_string))
801 return;
803 /* Put a dash at the end of the buffer temporarily,
804 but make it go away when the next character is added. */
805 current_kboard->echo_string = concat2 (current_kboard->echo_string,
806 build_string ("-"));
807 echo_now ();
810 /* Display the current echo string, and begin echoing if not already
811 doing so. */
813 void
814 echo_now ()
816 if (!current_kboard->immediate_echo)
818 int i;
819 current_kboard->immediate_echo = 1;
821 for (i = 0; i < this_command_key_count; i++)
823 Lisp_Object c;
825 /* Set before_command_echo_length to the value that would
826 have been saved before the start of this subcommand in
827 command_loop_1, if we had already been echoing then. */
828 if (i == this_single_command_key_start)
829 before_command_echo_length = echo_length ();
831 c = XVECTOR (this_command_keys)->contents[i];
832 if (! (EVENT_HAS_PARAMETERS (c)
833 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
834 echo_char (c);
837 /* Set before_command_echo_length to the value that would
838 have been saved before the start of this subcommand in
839 command_loop_1, if we had already been echoing then. */
840 if (this_command_key_count == this_single_command_key_start)
841 before_command_echo_length = echo_length ();
843 /* Put a dash at the end to invite the user to type more. */
844 echo_dash ();
847 echoing = 1;
848 message3_nolog (current_kboard->echo_string,
849 SBYTES (current_kboard->echo_string),
850 STRING_MULTIBYTE (current_kboard->echo_string));
851 echoing = 0;
853 /* Record in what buffer we echoed, and from which kboard. */
854 echo_message_buffer = echo_area_buffer[0];
855 echo_kboard = current_kboard;
857 if (waiting_for_input && !NILP (Vquit_flag))
858 quit_throw_to_read_char ();
861 /* Turn off echoing, for the start of a new command. */
863 void
864 cancel_echoing ()
866 current_kboard->immediate_echo = 0;
867 current_kboard->echo_after_prompt = -1;
868 current_kboard->echo_string = Qnil;
869 ok_to_echo_at_next_pause = NULL;
870 echo_kboard = NULL;
871 echo_message_buffer = Qnil;
874 /* Return the length of the current echo string. */
876 static int
877 echo_length ()
879 return (STRINGP (current_kboard->echo_string)
880 ? SCHARS (current_kboard->echo_string)
881 : 0);
884 /* Truncate the current echo message to its first LEN chars.
885 This and echo_char get used by read_key_sequence when the user
886 switches frames while entering a key sequence. */
888 static void
889 echo_truncate (nchars)
890 int nchars;
892 if (STRINGP (current_kboard->echo_string))
893 current_kboard->echo_string
894 = Fsubstring (current_kboard->echo_string,
895 make_number (0), make_number (nchars));
896 truncate_echo_area (nchars);
900 /* Functions for manipulating this_command_keys. */
901 static void
902 add_command_key (key)
903 Lisp_Object key;
905 #if 0 /* Not needed after we made Freset_this_command_lengths
906 do the job immediately. */
907 /* If reset-this-command-length was called recently, obey it now.
908 See the doc string of that function for an explanation of why. */
909 if (before_command_restore_flag)
911 this_command_key_count = before_command_key_count_1;
912 if (this_command_key_count < this_single_command_key_start)
913 this_single_command_key_start = this_command_key_count;
914 echo_truncate (before_command_echo_length_1);
915 before_command_restore_flag = 0;
917 #endif
919 if (this_command_key_count >= ASIZE (this_command_keys))
920 this_command_keys = larger_vector (this_command_keys,
921 2 * ASIZE (this_command_keys),
922 Qnil);
924 AREF (this_command_keys, this_command_key_count) = key;
925 ++this_command_key_count;
929 Lisp_Object
930 recursive_edit_1 ()
932 int count = SPECPDL_INDEX ();
933 Lisp_Object val;
935 if (command_loop_level > 0)
937 specbind (Qstandard_output, Qt);
938 specbind (Qstandard_input, Qt);
941 #ifdef HAVE_X_WINDOWS
942 /* The command loop has started an hourglass timer, so we have to
943 cancel it here, otherwise it will fire because the recursive edit
944 can take some time. Do not check for display_hourglass_p here,
945 because it could already be nil. */
946 cancel_hourglass ();
947 #endif
949 /* This function may have been called from a debugger called from
950 within redisplay, for instance by Edebugging a function called
951 from fontification-functions. We want to allow redisplay in
952 the debugging session.
954 The recursive edit is left with a `(throw exit ...)'. The `exit'
955 tag is not caught anywhere in redisplay, i.e. when we leave the
956 recursive edit, the original redisplay leading to the recursive
957 edit will be unwound. The outcome should therefore be safe. */
958 specbind (Qinhibit_redisplay, Qnil);
959 redisplaying_p = 0;
961 val = command_loop ();
962 if (EQ (val, Qt))
963 Fsignal (Qquit, Qnil);
964 /* Handle throw from read_minibuf when using minibuffer
965 while it's active but we're in another window. */
966 if (STRINGP (val))
967 Fsignal (Qerror, Fcons (val, Qnil));
969 return unbind_to (count, Qnil);
972 /* When an auto-save happens, record the "time", and don't do again soon. */
974 void
975 record_auto_save ()
977 last_auto_save = num_nonmacro_input_events;
980 /* Make an auto save happen as soon as possible at command level. */
982 void
983 force_auto_save_soon ()
985 last_auto_save = - auto_save_interval - 1;
987 record_asynch_buffer_change ();
990 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
991 doc: /* Invoke the editor command loop recursively.
992 To get out of the recursive edit, a command can do `(throw 'exit nil)';
993 that tells this function to return.
994 Alternately, `(throw 'exit t)' makes this function signal an error.
995 This function is called by the editor initialization to begin editing. */)
998 int count = SPECPDL_INDEX ();
999 Lisp_Object buffer;
1001 /* If we enter while input is blocked, don't lock up here.
1002 This may happen through the debugger during redisplay. */
1003 if (INPUT_BLOCKED_P)
1004 return Qnil;
1006 command_loop_level++;
1007 update_mode_lines = 1;
1009 if (command_loop_level
1010 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
1011 buffer = Fcurrent_buffer ();
1012 else
1013 buffer = Qnil;
1015 /* If we leave recursive_edit_1 below with a `throw' for instance,
1016 like it is done in the splash screen display, we have to
1017 make sure that we restore single_kboard as command_loop_1
1018 would have done if it were left normally. */
1019 record_unwind_protect (recursive_edit_unwind,
1020 Fcons (buffer, single_kboard ? Qt : Qnil));
1022 recursive_edit_1 ();
1023 return unbind_to (count, Qnil);
1026 Lisp_Object
1027 recursive_edit_unwind (info)
1028 Lisp_Object info;
1030 if (BUFFERP (XCAR (info)))
1031 Fset_buffer (XCAR (info));
1033 if (NILP (XCDR (info)))
1034 any_kboard_state ();
1035 else
1036 single_kboard_state ();
1038 command_loop_level--;
1039 update_mode_lines = 1;
1040 return Qnil;
1044 static void
1045 any_kboard_state ()
1047 #ifdef MULTI_KBOARD
1048 #if 0 /* Theory: if there's anything in Vunread_command_events,
1049 it will right away be read by read_key_sequence,
1050 and then if we do switch KBOARDS, it will go into the side
1051 queue then. So we don't need to do anything special here -- rms. */
1052 if (CONSP (Vunread_command_events))
1054 current_kboard->kbd_queue
1055 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
1056 current_kboard->kbd_queue_has_data = 1;
1058 Vunread_command_events = Qnil;
1059 #endif
1060 single_kboard = 0;
1061 #endif
1064 /* Switch to the single-kboard state, making current_kboard
1065 the only KBOARD from which further input is accepted. */
1067 void
1068 single_kboard_state ()
1070 #ifdef MULTI_KBOARD
1071 single_kboard = 1;
1072 #endif
1075 /* Maintain a stack of kboards, so other parts of Emacs
1076 can switch temporarily to the kboard of a given frame
1077 and then revert to the previous status. */
1079 struct kboard_stack
1081 KBOARD *kboard;
1082 struct kboard_stack *next;
1085 static struct kboard_stack *kboard_stack;
1087 void
1088 push_frame_kboard (f)
1089 FRAME_PTR f;
1091 #ifdef MULTI_KBOARD
1092 struct kboard_stack *p
1093 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
1095 p->next = kboard_stack;
1096 p->kboard = current_kboard;
1097 kboard_stack = p;
1099 current_kboard = FRAME_KBOARD (f);
1100 #endif
1103 void
1104 pop_frame_kboard ()
1106 #ifdef MULTI_KBOARD
1107 struct kboard_stack *p = kboard_stack;
1108 current_kboard = p->kboard;
1109 kboard_stack = p->next;
1110 xfree (p);
1111 #endif
1114 /* Handle errors that are not handled at inner levels
1115 by printing an error message and returning to the editor command loop. */
1117 Lisp_Object
1118 cmd_error (data)
1119 Lisp_Object data;
1121 Lisp_Object old_level, old_length;
1122 char macroerror[50];
1124 #ifdef HAVE_X_WINDOWS
1125 if (display_hourglass_p)
1126 cancel_hourglass ();
1127 #endif
1129 if (!NILP (executing_macro))
1131 if (executing_macro_iterations == 1)
1132 sprintf (macroerror, "After 1 kbd macro iteration: ");
1133 else
1134 sprintf (macroerror, "After %d kbd macro iterations: ",
1135 executing_macro_iterations);
1137 else
1138 *macroerror = 0;
1140 Vstandard_output = Qt;
1141 Vstandard_input = Qt;
1142 Vexecuting_macro = Qnil;
1143 executing_macro = Qnil;
1144 current_kboard->Vprefix_arg = Qnil;
1145 current_kboard->Vlast_prefix_arg = Qnil;
1146 cancel_echoing ();
1148 /* Avoid unquittable loop if data contains a circular list. */
1149 old_level = Vprint_level;
1150 old_length = Vprint_length;
1151 XSETFASTINT (Vprint_level, 10);
1152 XSETFASTINT (Vprint_length, 10);
1153 cmd_error_internal (data, macroerror);
1154 Vprint_level = old_level;
1155 Vprint_length = old_length;
1157 Vquit_flag = Qnil;
1159 Vinhibit_quit = Qnil;
1160 #ifdef MULTI_KBOARD
1161 any_kboard_state ();
1162 #endif
1164 return make_number (0);
1167 /* Take actions on handling an error. DATA is the data that describes
1168 the error.
1170 CONTEXT is a C-string containing ASCII characters only which
1171 describes the context in which the error happened. If we need to
1172 generalize CONTEXT to allow multibyte characters, make it a Lisp
1173 string. */
1175 void
1176 cmd_error_internal (data, context)
1177 Lisp_Object data;
1178 char *context;
1180 Lisp_Object stream;
1181 int kill_emacs_p = 0;
1182 struct frame *sf = SELECTED_FRAME ();
1184 Vquit_flag = Qnil;
1185 Vinhibit_quit = Qt;
1186 clear_message (1, 0);
1188 /* If the window system or terminal frame hasn't been initialized
1189 yet, or we're not interactive, it's best to dump this message out
1190 to stderr and exit. */
1191 if (!sf->glyphs_initialized_p
1192 /* This is the case of the frame dumped with Emacs, when we're
1193 running under a window system. */
1194 || (!NILP (Vwindow_system)
1195 && !inhibit_window_system
1196 && FRAME_TERMCAP_P (sf))
1197 || noninteractive)
1199 stream = Qexternal_debugging_output;
1200 kill_emacs_p = 1;
1202 else
1204 Fdiscard_input ();
1205 message_log_maybe_newline ();
1206 bitch_at_user ();
1207 stream = Qt;
1210 /* The immediate context is not interesting for Quits,
1211 since they are asyncronous. */
1212 if (EQ (XCAR (data), Qquit))
1213 Vsignaling_function = Qnil;
1215 print_error_message (data, stream, context, Vsignaling_function);
1217 Vsignaling_function = Qnil;
1219 /* If the window system or terminal frame hasn't been initialized
1220 yet, or we're in -batch mode, this error should cause Emacs to exit. */
1221 if (kill_emacs_p)
1223 Fterpri (stream);
1224 Fkill_emacs (make_number (-1));
1228 Lisp_Object command_loop_1 ();
1229 Lisp_Object command_loop_2 ();
1230 Lisp_Object top_level_1 ();
1232 /* Entry to editor-command-loop.
1233 This level has the catches for exiting/returning to editor command loop.
1234 It returns nil to exit recursive edit, t to abort it. */
1236 Lisp_Object
1237 command_loop ()
1239 if (command_loop_level > 0 || minibuf_level > 0)
1241 Lisp_Object val;
1242 val = internal_catch (Qexit, command_loop_2, Qnil);
1243 executing_macro = Qnil;
1244 return val;
1246 else
1247 while (1)
1249 internal_catch (Qtop_level, top_level_1, Qnil);
1250 internal_catch (Qtop_level, command_loop_2, Qnil);
1251 executing_macro = Qnil;
1253 /* End of file in -batch run causes exit here. */
1254 if (noninteractive)
1255 Fkill_emacs (Qt);
1259 /* Here we catch errors in execution of commands within the
1260 editing loop, and reenter the editing loop.
1261 When there is an error, cmd_error runs and returns a non-nil
1262 value to us. A value of nil means that command_loop_1 itself
1263 returned due to end of file (or end of kbd macro). */
1265 Lisp_Object
1266 command_loop_2 ()
1268 register Lisp_Object val;
1271 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
1272 while (!NILP (val));
1274 return Qnil;
1277 Lisp_Object
1278 top_level_2 ()
1280 return Feval (Vtop_level);
1283 Lisp_Object
1284 top_level_1 ()
1286 /* On entry to the outer level, run the startup file */
1287 if (!NILP (Vtop_level))
1288 internal_condition_case (top_level_2, Qerror, cmd_error);
1289 else if (!NILP (Vpurify_flag))
1290 message ("Bare impure Emacs (standard Lisp code not loaded)");
1291 else
1292 message ("Bare Emacs (standard Lisp code not loaded)");
1293 return Qnil;
1296 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1297 doc: /* Exit all recursive editing levels. */)
1300 #ifdef HAVE_X_WINDOWS
1301 if (display_hourglass_p)
1302 cancel_hourglass ();
1303 #endif
1305 /* Unblock input if we enter with input blocked. This may happen if
1306 redisplay traps e.g. during tool-bar update with input blocked. */
1307 while (INPUT_BLOCKED_P)
1308 UNBLOCK_INPUT;
1310 return Fthrow (Qtop_level, Qnil);
1313 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1314 doc: /* Exit from the innermost recursive edit or minibuffer. */)
1317 if (command_loop_level > 0 || minibuf_level > 0)
1318 Fthrow (Qexit, Qnil);
1320 error ("No recursive edit is in progress");
1321 return Qnil;
1324 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1325 doc: /* Abort the command that requested this recursive edit or minibuffer input. */)
1328 if (command_loop_level > 0 || minibuf_level > 0)
1329 Fthrow (Qexit, Qt);
1331 error ("No recursive edit is in progress");
1332 return Qnil;
1335 /* This is the actual command reading loop,
1336 sans error-handling encapsulation. */
1338 static int read_key_sequence P_ ((Lisp_Object *, int, Lisp_Object,
1339 int, int, int));
1340 void safe_run_hooks P_ ((Lisp_Object));
1341 static void adjust_point_for_property P_ ((int, int));
1343 /* Cancel hourglass from protect_unwind.
1344 ARG is not used. */
1345 #ifdef HAVE_X_WINDOWS
1346 static Lisp_Object
1347 cancel_hourglass_unwind (arg)
1348 Lisp_Object arg;
1350 cancel_hourglass ();
1352 #endif
1354 Lisp_Object
1355 command_loop_1 ()
1357 Lisp_Object cmd;
1358 int lose;
1359 int nonundocount;
1360 Lisp_Object keybuf[30];
1361 int i;
1362 int no_direct;
1363 int prev_modiff;
1364 struct buffer *prev_buffer = NULL;
1365 #ifdef MULTI_KBOARD
1366 int was_locked = single_kboard;
1367 #endif
1368 int already_adjusted;
1370 current_kboard->Vprefix_arg = Qnil;
1371 current_kboard->Vlast_prefix_arg = Qnil;
1372 Vdeactivate_mark = Qnil;
1373 waiting_for_input = 0;
1374 cancel_echoing ();
1376 nonundocount = 0;
1377 this_command_key_count = 0;
1378 this_command_key_count_reset = 0;
1379 this_single_command_key_start = 0;
1381 if (NILP (Vmemory_full))
1383 /* Make sure this hook runs after commands that get errors and
1384 throw to top level. */
1385 /* Note that the value cell will never directly contain nil
1386 if the symbol is a local variable. */
1387 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1388 safe_run_hooks (Qpost_command_hook);
1390 /* If displaying a message, resize the echo area window to fit
1391 that message's size exactly. */
1392 if (!NILP (echo_area_buffer[0]))
1393 resize_echo_area_exactly ();
1395 if (!NILP (Vdeferred_action_list))
1396 call0 (Vdeferred_action_function);
1398 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1400 if (NILP (Vunread_command_events)
1401 && NILP (Vunread_input_method_events)
1402 && NILP (Vunread_post_input_method_events)
1403 && NILP (Vexecuting_macro)
1404 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1405 safe_run_hooks (Qpost_command_idle_hook);
1409 Vmemory_full = Qnil;
1411 /* Do this after running Vpost_command_hook, for consistency. */
1412 current_kboard->Vlast_command = Vthis_command;
1413 current_kboard->Vreal_last_command = real_this_command;
1415 while (1)
1417 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1418 Fkill_emacs (Qnil);
1420 /* Make sure the current window's buffer is selected. */
1421 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1422 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1424 /* Display any malloc warning that just came out. Use while because
1425 displaying one warning can cause another. */
1427 while (pending_malloc_warning)
1428 display_malloc_warning ();
1430 no_direct = 0;
1432 Vdeactivate_mark = Qnil;
1434 /* If minibuffer on and echo area in use,
1435 wait a short time and redraw minibuffer. */
1437 if (minibuf_level
1438 && !NILP (echo_area_buffer[0])
1439 && EQ (minibuf_window, echo_area_window)
1440 && NUMBERP (Vminibuffer_message_timeout))
1442 /* Bind inhibit-quit to t so that C-g gets read in
1443 rather than quitting back to the minibuffer. */
1444 int count = SPECPDL_INDEX ();
1445 specbind (Qinhibit_quit, Qt);
1447 Fsit_for (Vminibuffer_message_timeout, Qnil, Qnil);
1448 /* Clear the echo area. */
1449 message2 (0, 0, 0);
1450 safe_run_hooks (Qecho_area_clear_hook);
1452 unbind_to (count, Qnil);
1454 /* If a C-g came in before, treat it as input now. */
1455 if (!NILP (Vquit_flag))
1457 Vquit_flag = Qnil;
1458 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1462 #ifdef C_ALLOCA
1463 alloca (0); /* Cause a garbage collection now */
1464 /* Since we can free the most stuff here. */
1465 #endif /* C_ALLOCA */
1467 #if 0
1468 /* Select the frame that the last event came from. Usually,
1469 switch-frame events will take care of this, but if some lisp
1470 code swallows a switch-frame event, we'll fix things up here.
1471 Is this a good idea? */
1472 if (FRAMEP (internal_last_event_frame)
1473 && !EQ (internal_last_event_frame, selected_frame))
1474 Fselect_frame (internal_last_event_frame, Qnil);
1475 #endif
1476 /* If it has changed current-menubar from previous value,
1477 really recompute the menubar from the value. */
1478 if (! NILP (Vlucid_menu_bar_dirty_flag)
1479 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1480 call0 (Qrecompute_lucid_menubar);
1482 before_command_key_count = this_command_key_count;
1483 before_command_echo_length = echo_length ();
1485 Vthis_command = Qnil;
1486 real_this_command = Qnil;
1488 /* Read next key sequence; i gets its length. */
1489 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1490 Qnil, 0, 1, 1);
1492 /* A filter may have run while we were reading the input. */
1493 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1494 Fkill_emacs (Qnil);
1495 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1496 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1498 ++num_input_keys;
1500 /* Now we have read a key sequence of length I,
1501 or else I is 0 and we found end of file. */
1503 if (i == 0) /* End of file -- happens only in */
1504 return Qnil; /* a kbd macro, at the end. */
1505 /* -1 means read_key_sequence got a menu that was rejected.
1506 Just loop around and read another command. */
1507 if (i == -1)
1509 cancel_echoing ();
1510 this_command_key_count = 0;
1511 this_command_key_count_reset = 0;
1512 this_single_command_key_start = 0;
1513 goto finalize;
1516 last_command_char = keybuf[i - 1];
1518 /* If the previous command tried to force a specific window-start,
1519 forget about that, in case this command moves point far away
1520 from that position. But also throw away beg_unchanged and
1521 end_unchanged information in that case, so that redisplay will
1522 update the whole window properly. */
1523 if (!NILP (XWINDOW (selected_window)->force_start))
1525 struct buffer *b;
1526 XWINDOW (selected_window)->force_start = Qnil;
1527 b = XBUFFER (XWINDOW (selected_window)->buffer);
1528 BUF_BEG_UNCHANGED (b) = BUF_END_UNCHANGED (b) = 0;
1531 cmd = read_key_sequence_cmd;
1532 if (!NILP (Vexecuting_macro))
1534 if (!NILP (Vquit_flag))
1536 Vexecuting_macro = Qt;
1537 QUIT; /* Make some noise. */
1538 /* Will return since macro now empty. */
1542 /* Do redisplay processing after this command except in special
1543 cases identified below. */
1544 prev_buffer = current_buffer;
1545 prev_modiff = MODIFF;
1546 last_point_position = PT;
1547 XSETBUFFER (last_point_position_buffer, prev_buffer);
1549 /* By default, we adjust point to a boundary of a region that
1550 has such a property that should be treated intangible
1551 (e.g. composition, display). But, some commands will set
1552 this variable differently. */
1553 Vdisable_point_adjustment = Qnil;
1555 /* Process filters and timers may have messed with deactivate-mark.
1556 reset it before we execute the command. */
1557 Vdeactivate_mark = Qnil;
1559 /* Remap command through active keymaps */
1560 Vthis_original_command = cmd;
1561 if (SYMBOLP (cmd))
1563 Lisp_Object cmd1;
1564 if (cmd1 = Fcommand_remapping (cmd), !NILP (cmd1))
1565 cmd = cmd1;
1568 /* Execute the command. */
1570 Vthis_command = cmd;
1571 real_this_command = cmd;
1572 /* Note that the value cell will never directly contain nil
1573 if the symbol is a local variable. */
1574 if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
1575 safe_run_hooks (Qpre_command_hook);
1577 already_adjusted = 0;
1579 if (NILP (Vthis_command))
1581 /* nil means key is undefined. */
1582 bitch_at_user ();
1583 current_kboard->defining_kbd_macro = Qnil;
1584 update_mode_lines = 1;
1585 current_kboard->Vprefix_arg = Qnil;
1587 else
1589 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1591 /* In case we jump to directly_done. */
1592 Vcurrent_prefix_arg = current_kboard->Vprefix_arg;
1594 /* Recognize some common commands in common situations and
1595 do them directly. */
1596 if (EQ (Vthis_command, Qforward_char) && PT < ZV)
1598 struct Lisp_Char_Table *dp
1599 = window_display_table (XWINDOW (selected_window));
1600 lose = FETCH_CHAR (PT_BYTE);
1601 SET_PT (PT + 1);
1602 if (! NILP (Vpost_command_hook))
1603 /* Put this before calling adjust_point_for_property
1604 so it will only get called once in any case. */
1605 goto directly_done;
1606 if (current_buffer == prev_buffer
1607 && last_point_position != PT
1608 && NILP (Vdisable_point_adjustment)
1609 && NILP (Vglobal_disable_point_adjustment))
1610 adjust_point_for_property (last_point_position, 0);
1611 already_adjusted = 1;
1612 if (PT == last_point_position + 1
1613 && (dp
1614 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1615 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1616 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1617 && (lose >= 0x20 && lose < 0x7f)))
1618 : (lose >= 0x20 && lose < 0x7f))
1619 /* To extract the case of continuation on
1620 wide-column characters. */
1621 && (WIDTH_BY_CHAR_HEAD (FETCH_BYTE (PT_BYTE)) == 1)
1622 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1623 >= MODIFF)
1624 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1625 >= OVERLAY_MODIFF)
1626 && (XFASTINT (XWINDOW (selected_window)->last_point)
1627 == PT - 1)
1628 && !windows_or_buffers_changed
1629 && EQ (current_buffer->selective_display, Qnil)
1630 && !detect_input_pending ()
1631 && NILP (XWINDOW (selected_window)->column_number_displayed)
1632 && NILP (Vexecuting_macro))
1633 direct_output_forward_char (1);
1634 goto directly_done;
1636 else if (EQ (Vthis_command, Qbackward_char) && PT > BEGV)
1638 struct Lisp_Char_Table *dp
1639 = window_display_table (XWINDOW (selected_window));
1640 SET_PT (PT - 1);
1641 lose = FETCH_CHAR (PT_BYTE);
1642 if (! NILP (Vpost_command_hook))
1643 goto directly_done;
1644 if (current_buffer == prev_buffer
1645 && last_point_position != PT
1646 && NILP (Vdisable_point_adjustment)
1647 && NILP (Vglobal_disable_point_adjustment))
1648 adjust_point_for_property (last_point_position, 0);
1649 already_adjusted = 1;
1650 if (PT == last_point_position - 1
1651 && (dp
1652 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1653 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1654 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1655 && (lose >= 0x20 && lose < 0x7f)))
1656 : (lose >= 0x20 && lose < 0x7f))
1657 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1658 >= MODIFF)
1659 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1660 >= OVERLAY_MODIFF)
1661 && (XFASTINT (XWINDOW (selected_window)->last_point)
1662 == PT + 1)
1663 && !windows_or_buffers_changed
1664 && EQ (current_buffer->selective_display, Qnil)
1665 && !detect_input_pending ()
1666 && NILP (XWINDOW (selected_window)->column_number_displayed)
1667 && NILP (Vexecuting_macro))
1668 direct_output_forward_char (-1);
1669 goto directly_done;
1671 else if (EQ (Vthis_command, Qself_insert_command)
1672 /* Try this optimization only on char keystrokes. */
1673 && NATNUMP (last_command_char)
1674 && CHAR_VALID_P (XFASTINT (last_command_char), 0))
1676 unsigned int c
1677 = translate_char (Vtranslation_table_for_input,
1678 XFASTINT (last_command_char), 0, 0, 0);
1679 int value;
1680 if (NILP (Vexecuting_macro)
1681 && !EQ (minibuf_window, selected_window))
1683 if (!nonundocount || nonundocount >= 20)
1685 Fundo_boundary ();
1686 nonundocount = 0;
1688 nonundocount++;
1691 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1692 < MODIFF)
1693 || (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1694 < OVERLAY_MODIFF)
1695 || (XFASTINT (XWINDOW (selected_window)->last_point)
1696 != PT)
1697 || MODIFF <= SAVE_MODIFF
1698 || windows_or_buffers_changed
1699 || !EQ (current_buffer->selective_display, Qnil)
1700 || detect_input_pending ()
1701 || !NILP (XWINDOW (selected_window)->column_number_displayed)
1702 || !NILP (Vexecuting_macro));
1704 value = internal_self_insert (c, 0);
1706 if (value == 2)
1707 nonundocount = 0;
1709 if (! NILP (Vpost_command_hook))
1710 /* Put this before calling adjust_point_for_property
1711 so it will only get called once in any case. */
1712 goto directly_done;
1714 /* VALUE == 1 when AFTER-CHANGE functions are
1715 installed which is the case most of the time
1716 because FONT-LOCK installs one. */
1717 if (!lose && !value)
1718 direct_output_for_insert (c);
1719 goto directly_done;
1723 /* Here for a command that isn't executed directly */
1726 #ifdef HAVE_X_WINDOWS
1727 int scount = SPECPDL_INDEX ();
1729 if (display_hourglass_p
1730 && NILP (Vexecuting_macro))
1732 record_unwind_protect (cancel_hourglass_unwind, Qnil);
1733 start_hourglass ();
1735 #endif
1737 nonundocount = 0;
1738 if (NILP (current_kboard->Vprefix_arg))
1739 Fundo_boundary ();
1740 Fcommand_execute (Vthis_command, Qnil, Qnil, Qnil);
1742 #ifdef HAVE_X_WINDOWS
1743 /* Do not check display_hourglass_p here, because
1744 Fcommand_execute could change it, but we should cancel
1745 hourglass cursor anyway.
1746 But don't cancel the hourglass within a macro
1747 just because a command in the macro finishes. */
1748 if (NILP (Vexecuting_macro))
1749 unbind_to (scount, Qnil);
1750 #endif
1753 directly_done: ;
1754 current_kboard->Vlast_prefix_arg = Vcurrent_prefix_arg;
1756 /* Note that the value cell will never directly contain nil
1757 if the symbol is a local variable. */
1758 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1759 safe_run_hooks (Qpost_command_hook);
1761 /* If displaying a message, resize the echo area window to fit
1762 that message's size exactly. */
1763 if (!NILP (echo_area_buffer[0]))
1764 resize_echo_area_exactly ();
1766 if (!NILP (Vdeferred_action_list))
1767 safe_run_hooks (Qdeferred_action_function);
1769 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1771 if (NILP (Vunread_command_events)
1772 && NILP (Vunread_input_method_events)
1773 && NILP (Vunread_post_input_method_events)
1774 && NILP (Vexecuting_macro)
1775 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1776 safe_run_hooks (Qpost_command_idle_hook);
1779 /* If there is a prefix argument,
1780 1) We don't want Vlast_command to be ``universal-argument''
1781 (that would be dumb), so don't set Vlast_command,
1782 2) we want to leave echoing on so that the prefix will be
1783 echoed as part of this key sequence, so don't call
1784 cancel_echoing, and
1785 3) we want to leave this_command_key_count non-zero, so that
1786 read_char will realize that it is re-reading a character, and
1787 not echo it a second time.
1789 If the command didn't actually create a prefix arg,
1790 but is merely a frame event that is transparent to prefix args,
1791 then the above doesn't apply. */
1792 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1794 current_kboard->Vlast_command = Vthis_command;
1795 current_kboard->Vreal_last_command = real_this_command;
1796 cancel_echoing ();
1797 this_command_key_count = 0;
1798 this_command_key_count_reset = 0;
1799 this_single_command_key_start = 0;
1802 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1804 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1806 /* We could also call `deactivate'mark'. */
1807 if (EQ (Vtransient_mark_mode, Qlambda))
1808 Vtransient_mark_mode = Qnil;
1809 else
1811 current_buffer->mark_active = Qnil;
1812 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1815 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1816 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1819 finalize:
1821 if (current_buffer == prev_buffer
1822 && last_point_position != PT
1823 && NILP (Vdisable_point_adjustment)
1824 && NILP (Vglobal_disable_point_adjustment)
1825 && !already_adjusted)
1826 adjust_point_for_property (last_point_position, MODIFF != prev_modiff);
1828 /* Install chars successfully executed in kbd macro. */
1830 if (!NILP (current_kboard->defining_kbd_macro)
1831 && NILP (current_kboard->Vprefix_arg))
1832 finalize_kbd_macro_chars ();
1834 #ifdef MULTI_KBOARD
1835 if (!was_locked)
1836 any_kboard_state ();
1837 #endif
1841 extern Lisp_Object Qcomposition, Qdisplay;
1843 /* Adjust point to a boundary of a region that has such a property
1844 that should be treated intangible. For the moment, we check
1845 `composition', `display' and `invisible' properties.
1846 LAST_PT is the last position of point. */
1848 extern Lisp_Object Qafter_string, Qbefore_string;
1849 extern Lisp_Object get_pos_property P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
1851 static void
1852 adjust_point_for_property (last_pt, modified)
1853 int last_pt;
1854 int modified;
1856 int beg, end;
1857 Lisp_Object val, overlay, tmp;
1858 int check_composition = 1, check_display = 1, check_invisible = 1;
1859 int orig_pt = PT;
1861 /* FIXME: cycling is probably not necessary because these properties
1862 can't be usefully combined anyway. */
1863 while (check_composition || check_display || check_invisible)
1865 if (check_composition
1866 && PT > BEGV && PT < ZV
1867 && get_property_and_range (PT, Qcomposition, &val, &beg, &end, Qnil)
1868 && COMPOSITION_VALID_P (beg, end, val)
1869 && beg < PT /* && end > PT <- It's always the case. */
1870 && (last_pt <= beg || last_pt >= end))
1872 xassert (end > PT);
1873 SET_PT (PT < last_pt ? beg : end);
1874 check_display = check_invisible = 1;
1876 check_composition = 0;
1877 if (check_display
1878 && PT > BEGV && PT < ZV
1879 && !NILP (val = get_char_property_and_overlay
1880 (make_number (PT), Qdisplay, Qnil, &overlay))
1881 && display_prop_intangible_p (val)
1882 && (!OVERLAYP (overlay)
1883 ? get_property_and_range (PT, Qdisplay, &val, &beg, &end, Qnil)
1884 : (beg = OVERLAY_POSITION (OVERLAY_START (overlay)),
1885 end = OVERLAY_POSITION (OVERLAY_END (overlay))))
1886 && beg < PT) /* && end > PT <- It's always the case. */
1888 xassert (end > PT);
1889 SET_PT (PT < last_pt ? beg : end);
1890 check_composition = check_invisible = 1;
1892 check_display = 0;
1893 if (check_invisible && PT > BEGV && PT < ZV)
1895 int inv, ellipsis = 0;
1896 beg = end = PT;
1898 /* Find boundaries `beg' and `end' of the invisible area, if any. */
1899 while (end < ZV
1900 && !NILP (val = get_char_property_and_overlay
1901 (make_number (end), Qinvisible, Qnil, &overlay))
1902 && (inv = TEXT_PROP_MEANS_INVISIBLE (val)))
1904 ellipsis = ellipsis || inv > 1
1905 || (OVERLAYP (overlay)
1906 && (!NILP (Foverlay_get (overlay, Qafter_string))
1907 || !NILP (Foverlay_get (overlay, Qbefore_string))));
1908 tmp = Fnext_single_char_property_change
1909 (make_number (end), Qinvisible, Qnil, Qnil);
1910 end = NATNUMP (tmp) ? XFASTINT (tmp) : ZV;
1912 while (beg > BEGV
1913 && !NILP (val = get_char_property_and_overlay
1914 (make_number (beg - 1), Qinvisible, Qnil, &overlay))
1915 && (inv = TEXT_PROP_MEANS_INVISIBLE (val)))
1917 ellipsis = ellipsis || inv > 1
1918 || (OVERLAYP (overlay)
1919 && (!NILP (Foverlay_get (overlay, Qafter_string))
1920 || !NILP (Foverlay_get (overlay, Qbefore_string))));
1921 tmp = Fprevious_single_char_property_change
1922 (make_number (beg), Qinvisible, Qnil, Qnil);
1923 beg = NATNUMP (tmp) ? XFASTINT (tmp) : BEGV;
1926 /* Move away from the inside area. */
1927 if (beg < PT && end > PT)
1929 SET_PT ((orig_pt == PT && (last_pt < beg || last_pt > end))
1930 /* We haven't moved yet (so we don't need to fear
1931 infinite-looping) and we were outside the range
1932 before (so either end of the range still corresponds
1933 to a move in the right direction): pretend we moved
1934 less than we actually did, so that we still have
1935 more freedom below in choosing which end of the range
1936 to go to. */
1937 ? (orig_pt = -1, PT < last_pt ? end : beg)
1938 /* We either have moved already or the last point
1939 was already in the range: we don't get to choose
1940 which end of the range we have to go to. */
1941 : (PT < last_pt ? beg : end));
1942 check_composition = check_display = 1;
1944 #if 0 /* This assertion isn't correct, because SET_PT may end up setting
1945 the point to something other than its argument, due to
1946 point-motion hooks, intangibility, etc. */
1947 xassert (PT == beg || PT == end);
1948 #endif
1950 /* Pretend the area doesn't exist if the buffer is not
1951 modified. */
1952 if (!modified && !ellipsis && beg < end)
1954 if (last_pt == beg && PT == end && end < ZV)
1955 (check_composition = check_display = 1, SET_PT (end + 1));
1956 else if (last_pt == end && PT == beg && beg > BEGV)
1957 (check_composition = check_display = 1, SET_PT (beg - 1));
1958 else if (PT == ((PT < last_pt) ? beg : end))
1959 /* We've already moved as far as we can. Trying to go
1960 to the other end would mean moving backwards and thus
1961 could lead to an infinite loop. */
1963 else if (val = get_pos_property (make_number (PT),
1964 Qinvisible, Qnil),
1965 TEXT_PROP_MEANS_INVISIBLE (val)
1966 && (val = get_pos_property
1967 (make_number (PT == beg ? end : beg),
1968 Qinvisible, Qnil),
1969 !TEXT_PROP_MEANS_INVISIBLE (val)))
1970 (check_composition = check_display = 1,
1971 SET_PT (PT == beg ? end : beg));
1974 check_invisible = 0;
1978 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1980 static Lisp_Object
1981 safe_run_hooks_1 (hook)
1982 Lisp_Object hook;
1984 return call1 (Vrun_hooks, Vinhibit_quit);
1987 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1989 static Lisp_Object
1990 safe_run_hooks_error (data)
1991 Lisp_Object data;
1993 Lisp_Object args[3];
1994 args[0] = build_string ("Error in %s: %s");
1995 args[1] = Vinhibit_quit;
1996 args[2] = data;
1997 Fmessage (3, args);
1998 return Fset (Vinhibit_quit, Qnil);
2001 /* If we get an error while running the hook, cause the hook variable
2002 to be nil. Also inhibit quits, so that C-g won't cause the hook
2003 to mysteriously evaporate. */
2005 void
2006 safe_run_hooks (hook)
2007 Lisp_Object hook;
2009 int count = SPECPDL_INDEX ();
2010 specbind (Qinhibit_quit, hook);
2012 internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
2014 unbind_to (count, Qnil);
2018 /* Number of seconds between polling for input. This is a Lisp
2019 variable that can be bound. */
2021 EMACS_INT polling_period;
2023 /* Nonzero means polling for input is temporarily suppressed. */
2025 int poll_suppress_count;
2027 /* Asynchronous timer for polling. */
2029 struct atimer *poll_timer;
2032 #ifdef POLL_FOR_INPUT
2034 /* Poll for input, so what we catch a C-g if it comes in. This
2035 function is called from x_make_frame_visible, see comment
2036 there. */
2038 void
2039 poll_for_input_1 ()
2041 if (interrupt_input_blocked == 0
2042 && !waiting_for_input)
2043 read_avail_input (0);
2046 /* Timer callback function for poll_timer. TIMER is equal to
2047 poll_timer. */
2049 void
2050 poll_for_input (timer)
2051 struct atimer *timer;
2053 if (poll_suppress_count == 0)
2054 poll_for_input_1 ();
2057 #endif /* POLL_FOR_INPUT */
2059 /* Begin signals to poll for input, if they are appropriate.
2060 This function is called unconditionally from various places. */
2062 void
2063 start_polling ()
2065 #ifdef POLL_FOR_INPUT
2066 if (read_socket_hook && !interrupt_input)
2068 /* Turn alarm handling on unconditionally. It might have
2069 been turned off in process.c. */
2070 turn_on_atimers (1);
2072 /* If poll timer doesn't exist, are we need one with
2073 a different interval, start a new one. */
2074 if (poll_timer == NULL
2075 || EMACS_SECS (poll_timer->interval) != polling_period)
2077 EMACS_TIME interval;
2079 if (poll_timer)
2080 cancel_atimer (poll_timer);
2082 EMACS_SET_SECS_USECS (interval, polling_period, 0);
2083 poll_timer = start_atimer (ATIMER_CONTINUOUS, interval,
2084 poll_for_input, NULL);
2087 /* Let the timer's callback function poll for input
2088 if this becomes zero. */
2089 --poll_suppress_count;
2091 #endif
2094 /* Nonzero if we are using polling to handle input asynchronously. */
2097 input_polling_used ()
2099 #ifdef POLL_FOR_INPUT
2100 return read_socket_hook && !interrupt_input;
2101 #else
2102 return 0;
2103 #endif
2106 /* Turn off polling. */
2108 void
2109 stop_polling ()
2111 #ifdef POLL_FOR_INPUT
2112 if (read_socket_hook && !interrupt_input)
2113 ++poll_suppress_count;
2114 #endif
2117 /* Set the value of poll_suppress_count to COUNT
2118 and start or stop polling accordingly. */
2120 void
2121 set_poll_suppress_count (count)
2122 int count;
2124 #ifdef POLL_FOR_INPUT
2125 if (count == 0 && poll_suppress_count != 0)
2127 poll_suppress_count = 1;
2128 start_polling ();
2130 else if (count != 0 && poll_suppress_count == 0)
2132 stop_polling ();
2134 poll_suppress_count = count;
2135 #endif
2138 /* Bind polling_period to a value at least N.
2139 But don't decrease it. */
2141 void
2142 bind_polling_period (n)
2143 int n;
2145 #ifdef POLL_FOR_INPUT
2146 int new = polling_period;
2148 if (n > new)
2149 new = n;
2151 stop_other_atimers (poll_timer);
2152 stop_polling ();
2153 specbind (Qpolling_period, make_number (new));
2154 /* Start a new alarm with the new period. */
2155 start_polling ();
2156 #endif
2159 /* Apply the control modifier to CHARACTER. */
2162 make_ctrl_char (c)
2163 int c;
2165 /* Save the upper bits here. */
2166 int upper = c & ~0177;
2168 c &= 0177;
2170 /* Everything in the columns containing the upper-case letters
2171 denotes a control character. */
2172 if (c >= 0100 && c < 0140)
2174 int oc = c;
2175 c &= ~0140;
2176 /* Set the shift modifier for a control char
2177 made from a shifted letter. But only for letters! */
2178 if (oc >= 'A' && oc <= 'Z')
2179 c |= shift_modifier;
2182 /* The lower-case letters denote control characters too. */
2183 else if (c >= 'a' && c <= 'z')
2184 c &= ~0140;
2186 /* Include the bits for control and shift
2187 only if the basic ASCII code can't indicate them. */
2188 else if (c >= ' ')
2189 c |= ctrl_modifier;
2191 /* Replace the high bits. */
2192 c |= (upper & ~ctrl_modifier);
2194 return c;
2197 /* Display help echo in the echo area.
2199 HELP a string means display that string, HELP nil means clear the
2200 help echo. If HELP is a function, call it with OBJECT and POS as
2201 arguments; the function should return a help string or nil for
2202 none. For all other types of HELP evaluate it to obtain a string.
2204 WINDOW is the window in which the help was generated, if any.
2205 It is nil if not in a window.
2207 If OBJECT is a buffer, POS is the position in the buffer where the
2208 `help-echo' text property was found.
2210 If OBJECT is an overlay, that overlay has a `help-echo' property,
2211 and POS is the position in the overlay's buffer under the mouse.
2213 If OBJECT is a string (an overlay string or a string displayed with
2214 the `display' property). POS is the position in that string under
2215 the mouse.
2217 OK_TO_OVERWRITE_KEYSTROKE_ECHO non-zero means it's okay if the help
2218 echo overwrites a keystroke echo currently displayed in the echo
2219 area.
2221 Note: this function may only be called with HELP nil or a string
2222 from X code running asynchronously. */
2224 void
2225 show_help_echo (help, window, object, pos, ok_to_overwrite_keystroke_echo)
2226 Lisp_Object help, window, object, pos;
2227 int ok_to_overwrite_keystroke_echo;
2229 if (!NILP (help) && !STRINGP (help))
2231 if (FUNCTIONP (help))
2233 Lisp_Object args[4];
2234 args[0] = help;
2235 args[1] = window;
2236 args[2] = object;
2237 args[3] = pos;
2238 help = safe_call (4, args);
2240 else
2241 help = safe_eval (help);
2243 if (!STRINGP (help))
2244 return;
2247 if (STRINGP (help) || NILP (help))
2249 if (!NILP (Vshow_help_function))
2250 call1 (Vshow_help_function, help);
2251 else if (/* Don't overwrite minibuffer contents. */
2252 !MINI_WINDOW_P (XWINDOW (selected_window))
2253 /* Don't overwrite a keystroke echo. */
2254 && (NILP (echo_message_buffer)
2255 || ok_to_overwrite_keystroke_echo)
2256 /* Don't overwrite a prompt. */
2257 && !cursor_in_echo_area)
2259 if (STRINGP (help))
2261 int count = SPECPDL_INDEX ();
2263 if (!help_echo_showing_p)
2264 Vpre_help_message = current_message ();
2266 specbind (Qmessage_truncate_lines, Qt);
2267 message3_nolog (help, SBYTES (help),
2268 STRING_MULTIBYTE (help));
2269 unbind_to (count, Qnil);
2271 else if (STRINGP (Vpre_help_message))
2273 message3_nolog (Vpre_help_message,
2274 SBYTES (Vpre_help_message),
2275 STRING_MULTIBYTE (Vpre_help_message));
2276 Vpre_help_message = Qnil;
2278 else
2279 message (0);
2282 help_echo_showing_p = STRINGP (help);
2288 /* Input of single characters from keyboard */
2290 Lisp_Object print_help ();
2291 static Lisp_Object kbd_buffer_get_event ();
2292 static void record_char ();
2294 #ifdef MULTI_KBOARD
2295 static jmp_buf wrong_kboard_jmpbuf;
2296 #endif
2298 #define STOP_POLLING \
2299 do { if (! polling_stopped_here) stop_polling (); \
2300 polling_stopped_here = 1; } while (0)
2302 #define RESUME_POLLING \
2303 do { if (polling_stopped_here) start_polling (); \
2304 polling_stopped_here = 0; } while (0)
2306 /* read a character from the keyboard; call the redisplay if needed */
2307 /* commandflag 0 means do not do auto-saving, but do do redisplay.
2308 -1 means do not do redisplay, but do do autosaving.
2309 1 means do both. */
2311 /* The arguments MAPS and NMAPS are for menu prompting.
2312 MAPS is an array of keymaps; NMAPS is the length of MAPS.
2314 PREV_EVENT is the previous input event, or nil if we are reading
2315 the first event of a key sequence (or not reading a key sequence).
2316 If PREV_EVENT is t, that is a "magic" value that says
2317 not to run input methods, but in other respects to act as if
2318 not reading a key sequence.
2320 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
2321 if we used a mouse menu to read the input, or zero otherwise. If
2322 USED_MOUSE_MENU is null, we don't dereference it.
2324 Value is t if we showed a menu and the user rejected it. */
2326 Lisp_Object
2327 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
2328 int commandflag;
2329 int nmaps;
2330 Lisp_Object *maps;
2331 Lisp_Object prev_event;
2332 int *used_mouse_menu;
2334 volatile Lisp_Object c;
2335 int count;
2336 jmp_buf local_getcjmp;
2337 jmp_buf save_jump;
2338 volatile int key_already_recorded = 0;
2339 Lisp_Object tem, save;
2340 volatile Lisp_Object previous_echo_area_message;
2341 volatile Lisp_Object also_record;
2342 volatile int reread;
2343 struct gcpro gcpro1, gcpro2;
2344 EMACS_TIME last_idle_start;
2345 int polling_stopped_here = 0;
2347 also_record = Qnil;
2349 #if 0 /* This was commented out as part of fixing echo for C-u left. */
2350 before_command_key_count = this_command_key_count;
2351 before_command_echo_length = echo_length ();
2352 #endif
2353 c = Qnil;
2354 previous_echo_area_message = Qnil;
2356 GCPRO2 (c, previous_echo_area_message);
2358 retry:
2360 reread = 0;
2361 if (CONSP (Vunread_post_input_method_events))
2363 c = XCAR (Vunread_post_input_method_events);
2364 Vunread_post_input_method_events
2365 = XCDR (Vunread_post_input_method_events);
2367 /* Undo what read_char_x_menu_prompt did when it unread
2368 additional keys returned by Fx_popup_menu. */
2369 if (CONSP (c)
2370 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2371 && NILP (XCDR (c)))
2372 c = XCAR (c);
2374 reread = 1;
2375 goto reread_first;
2378 if (unread_command_char != -1)
2380 XSETINT (c, unread_command_char);
2381 unread_command_char = -1;
2383 reread = 1;
2384 goto reread_first;
2387 if (CONSP (Vunread_command_events))
2389 c = XCAR (Vunread_command_events);
2390 Vunread_command_events = XCDR (Vunread_command_events);
2392 /* Undo what read_char_x_menu_prompt did when it unread
2393 additional keys returned by Fx_popup_menu. */
2394 if (CONSP (c)
2395 && EQ (XCDR (c), Qdisabled)
2396 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c))))
2397 c = XCAR (c);
2399 /* If the queued event is something that used the mouse,
2400 set used_mouse_menu accordingly. */
2401 if (used_mouse_menu
2402 && (EQ (c, Qtool_bar) || EQ (c, Qmenu_bar)))
2403 *used_mouse_menu = 1;
2405 reread = 1;
2406 goto reread_for_input_method;
2409 if (CONSP (Vunread_input_method_events))
2411 c = XCAR (Vunread_input_method_events);
2412 Vunread_input_method_events = XCDR (Vunread_input_method_events);
2414 /* Undo what read_char_x_menu_prompt did when it unread
2415 additional keys returned by Fx_popup_menu. */
2416 if (CONSP (c)
2417 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2418 && NILP (XCDR (c)))
2419 c = XCAR (c);
2420 reread = 1;
2421 goto reread_for_input_method;
2424 this_command_key_count_reset = 0;
2426 if (!NILP (Vexecuting_macro))
2428 /* We set this to Qmacro; since that's not a frame, nobody will
2429 try to switch frames on us, and the selected window will
2430 remain unchanged.
2432 Since this event came from a macro, it would be misleading to
2433 leave internal_last_event_frame set to wherever the last
2434 real event came from. Normally, a switch-frame event selects
2435 internal_last_event_frame after each command is read, but
2436 events read from a macro should never cause a new frame to be
2437 selected. */
2438 Vlast_event_frame = internal_last_event_frame = Qmacro;
2440 /* Exit the macro if we are at the end.
2441 Also, some things replace the macro with t
2442 to force an early exit. */
2443 if (EQ (Vexecuting_macro, Qt)
2444 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
2446 XSETINT (c, -1);
2447 goto exit;
2450 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
2451 if (STRINGP (Vexecuting_macro)
2452 && (XINT (c) & 0x80))
2453 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
2455 executing_macro_index++;
2457 goto from_macro;
2460 if (!NILP (unread_switch_frame))
2462 c = unread_switch_frame;
2463 unread_switch_frame = Qnil;
2465 /* This event should make it into this_command_keys, and get echoed
2466 again, so we do not set `reread'. */
2467 goto reread_first;
2470 /* if redisplay was requested */
2471 if (commandflag >= 0)
2473 /* If there is pending input, process any events which are not
2474 user-visible, such as X selection_request events. */
2475 if (input_pending
2476 || detect_input_pending_run_timers (0))
2477 swallow_events (0); /* may clear input_pending */
2479 /* Redisplay if no pending input. */
2480 while (!input_pending)
2482 if (help_echo_showing_p && !EQ (selected_window, minibuf_window))
2483 redisplay_preserve_echo_area (5);
2484 else
2485 redisplay ();
2487 if (!input_pending)
2488 /* Normal case: no input arrived during redisplay. */
2489 break;
2491 /* Input arrived and pre-empted redisplay.
2492 Process any events which are not user-visible. */
2493 swallow_events (0);
2494 /* If that cleared input_pending, try again to redisplay. */
2498 /* Message turns off echoing unless more keystrokes turn it on again.
2500 The code in 20.x for the condition was
2502 1. echo_area_glyphs && *echo_area_glyphs
2503 2. && echo_area_glyphs != current_kboard->echobuf
2504 3. && ok_to_echo_at_next_pause != echo_area_glyphs
2506 (1) means there's a current message displayed
2508 (2) means it's not the message from echoing from the current
2509 kboard.
2511 (3) There's only one place in 20.x where ok_to_echo_at_next_pause
2512 is set to a non-null value. This is done in read_char and it is
2513 set to echo_area_glyphs after a call to echo_char. That means
2514 ok_to_echo_at_next_pause is either null or
2515 current_kboard->echobuf with the appropriate current_kboard at
2516 that time.
2518 So, condition (3) means in clear text ok_to_echo_at_next_pause
2519 must be either null, or the current message isn't from echoing at
2520 all, or it's from echoing from a different kboard than the
2521 current one. */
2523 if (/* There currently is something in the echo area. */
2524 !NILP (echo_area_buffer[0])
2525 && (/* And it's either not from echoing. */
2526 !EQ (echo_area_buffer[0], echo_message_buffer)
2527 /* Or it's an echo from a different kboard. */
2528 || echo_kboard != current_kboard
2529 /* Or we explicitly allow overwriting whatever there is. */
2530 || ok_to_echo_at_next_pause == NULL))
2531 cancel_echoing ();
2532 else
2533 echo_dash ();
2535 /* Try reading a character via menu prompting in the minibuf.
2536 Try this before the sit-for, because the sit-for
2537 would do the wrong thing if we are supposed to do
2538 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
2539 after a mouse event so don't try a minibuf menu. */
2540 c = Qnil;
2541 if (nmaps > 0 && INTERACTIVE
2542 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
2543 /* Don't bring up a menu if we already have another event. */
2544 && NILP (Vunread_command_events)
2545 && unread_command_char < 0
2546 && !detect_input_pending_run_timers (0))
2548 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
2549 if (! NILP (c))
2551 key_already_recorded = 1;
2552 goto non_reread_1;
2556 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
2557 We will do that below, temporarily for short sections of code,
2558 when appropriate. local_getcjmp must be in effect
2559 around any call to sit_for or kbd_buffer_get_event;
2560 it *must not* be in effect when we call redisplay. */
2562 if (_setjmp (local_getcjmp))
2564 XSETINT (c, quit_char);
2565 internal_last_event_frame = selected_frame;
2566 Vlast_event_frame = internal_last_event_frame;
2567 /* If we report the quit char as an event,
2568 don't do so more than once. */
2569 if (!NILP (Vinhibit_quit))
2570 Vquit_flag = Qnil;
2572 #ifdef MULTI_KBOARD
2574 KBOARD *kb = FRAME_KBOARD (XFRAME (selected_frame));
2575 if (kb != current_kboard)
2577 Lisp_Object link = kb->kbd_queue;
2578 /* We shouldn't get here if we were in single-kboard mode! */
2579 if (single_kboard)
2580 abort ();
2581 if (CONSP (link))
2583 while (CONSP (XCDR (link)))
2584 link = XCDR (link);
2585 if (!NILP (XCDR (link)))
2586 abort ();
2588 if (!CONSP (link))
2589 kb->kbd_queue = Fcons (c, Qnil);
2590 else
2591 XSETCDR (link, Fcons (c, Qnil));
2592 kb->kbd_queue_has_data = 1;
2593 current_kboard = kb;
2594 /* This is going to exit from read_char
2595 so we had better get rid of this frame's stuff. */
2596 UNGCPRO;
2597 longjmp (wrong_kboard_jmpbuf, 1);
2600 #endif
2601 goto non_reread;
2604 timer_start_idle ();
2606 /* If in middle of key sequence and minibuffer not active,
2607 start echoing if enough time elapses. */
2609 if (minibuf_level == 0
2610 && !current_kboard->immediate_echo
2611 && this_command_key_count > 0
2612 && ! noninteractive
2613 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2614 && NILP (Fzerop (Vecho_keystrokes))
2615 && (/* No message. */
2616 NILP (echo_area_buffer[0])
2617 /* Or empty message. */
2618 || (BUF_BEG (XBUFFER (echo_area_buffer[0]))
2619 == BUF_Z (XBUFFER (echo_area_buffer[0])))
2620 /* Or already echoing from same kboard. */
2621 || (echo_kboard && ok_to_echo_at_next_pause == echo_kboard)
2622 /* Or not echoing before and echoing allowed. */
2623 || (!echo_kboard && ok_to_echo_at_next_pause)))
2625 Lisp_Object tem0;
2627 /* After a mouse event, start echoing right away.
2628 This is because we are probably about to display a menu,
2629 and we don't want to delay before doing so. */
2630 if (EVENT_HAS_PARAMETERS (prev_event))
2631 echo_now ();
2632 else
2634 int sec, usec;
2635 double duration = extract_float (Vecho_keystrokes);
2636 sec = (int) duration;
2637 usec = (duration - sec) * 1000000;
2638 save_getcjmp (save_jump);
2639 restore_getcjmp (local_getcjmp);
2640 tem0 = sit_for (sec, usec, 1, 1, 0);
2641 restore_getcjmp (save_jump);
2642 if (EQ (tem0, Qt)
2643 && ! CONSP (Vunread_command_events))
2644 echo_now ();
2648 /* Maybe auto save due to number of keystrokes. */
2650 if (commandflag != 0
2651 && auto_save_interval > 0
2652 && num_nonmacro_input_events - last_auto_save > max (auto_save_interval, 20)
2653 && !detect_input_pending_run_timers (0))
2655 Fdo_auto_save (Qnil, Qnil);
2656 /* Hooks can actually change some buffers in auto save. */
2657 redisplay ();
2660 /* Try reading using an X menu.
2661 This is never confused with reading using the minibuf
2662 because the recursive call of read_char in read_char_minibuf_menu_prompt
2663 does not pass on any keymaps. */
2665 if (nmaps > 0 && INTERACTIVE
2666 && !NILP (prev_event)
2667 && EVENT_HAS_PARAMETERS (prev_event)
2668 && !EQ (XCAR (prev_event), Qmenu_bar)
2669 && !EQ (XCAR (prev_event), Qtool_bar)
2670 /* Don't bring up a menu if we already have another event. */
2671 && NILP (Vunread_command_events)
2672 && unread_command_char < 0)
2674 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
2676 /* Now that we have read an event, Emacs is not idle. */
2677 timer_stop_idle ();
2679 goto exit;
2682 /* Maybe autosave and/or garbage collect due to idleness. */
2684 if (INTERACTIVE && NILP (c))
2686 int delay_level, buffer_size;
2688 /* Slow down auto saves logarithmically in size of current buffer,
2689 and garbage collect while we're at it. */
2690 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
2691 last_non_minibuf_size = Z - BEG;
2692 buffer_size = (last_non_minibuf_size >> 8) + 1;
2693 delay_level = 0;
2694 while (buffer_size > 64)
2695 delay_level++, buffer_size -= buffer_size >> 2;
2696 if (delay_level < 4) delay_level = 4;
2697 /* delay_level is 4 for files under around 50k, 7 at 100k,
2698 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
2700 /* Auto save if enough time goes by without input. */
2701 if (commandflag != 0
2702 && num_nonmacro_input_events > last_auto_save
2703 && INTEGERP (Vauto_save_timeout)
2704 && XINT (Vauto_save_timeout) > 0)
2706 Lisp_Object tem0;
2708 save_getcjmp (save_jump);
2709 restore_getcjmp (local_getcjmp);
2710 tem0 = sit_for (delay_level * XFASTINT (Vauto_save_timeout) / 4,
2711 0, 1, 1, 0);
2712 restore_getcjmp (save_jump);
2714 if (EQ (tem0, Qt)
2715 && ! CONSP (Vunread_command_events))
2717 Fdo_auto_save (Qnil, Qnil);
2719 /* If we have auto-saved and there is still no input
2720 available, garbage collect if there has been enough
2721 consing going on to make it worthwhile. */
2722 if (!detect_input_pending_run_timers (0)
2723 && consing_since_gc > gc_cons_threshold / 2)
2724 Fgarbage_collect ();
2726 redisplay ();
2731 /* If this has become non-nil here, it has been set by a timer
2732 or sentinel or filter. */
2733 if (CONSP (Vunread_command_events))
2735 c = XCAR (Vunread_command_events);
2736 Vunread_command_events = XCDR (Vunread_command_events);
2739 /* Read something from current KBOARD's side queue, if possible. */
2741 if (NILP (c))
2743 if (current_kboard->kbd_queue_has_data)
2745 if (!CONSP (current_kboard->kbd_queue))
2746 abort ();
2747 c = XCAR (current_kboard->kbd_queue);
2748 current_kboard->kbd_queue
2749 = XCDR (current_kboard->kbd_queue);
2750 if (NILP (current_kboard->kbd_queue))
2751 current_kboard->kbd_queue_has_data = 0;
2752 input_pending = readable_events (0);
2753 if (EVENT_HAS_PARAMETERS (c)
2754 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
2755 internal_last_event_frame = XCAR (XCDR (c));
2756 Vlast_event_frame = internal_last_event_frame;
2760 #ifdef MULTI_KBOARD
2761 /* If current_kboard's side queue is empty check the other kboards.
2762 If one of them has data that we have not yet seen here,
2763 switch to it and process the data waiting for it.
2765 Note: if the events queued up for another kboard
2766 have already been seen here, and therefore are not a complete command,
2767 the kbd_queue_has_data field is 0, so we skip that kboard here.
2768 That's to avoid an infinite loop switching between kboards here. */
2769 if (NILP (c) && !single_kboard)
2771 KBOARD *kb;
2772 for (kb = all_kboards; kb; kb = kb->next_kboard)
2773 if (kb->kbd_queue_has_data)
2775 current_kboard = kb;
2776 /* This is going to exit from read_char
2777 so we had better get rid of this frame's stuff. */
2778 UNGCPRO;
2779 longjmp (wrong_kboard_jmpbuf, 1);
2782 #endif
2784 wrong_kboard:
2786 STOP_POLLING;
2788 /* Finally, we read from the main queue,
2789 and if that gives us something we can't use yet, we put it on the
2790 appropriate side queue and try again. */
2792 if (NILP (c))
2794 KBOARD *kb;
2796 /* Actually read a character, waiting if necessary. */
2797 save_getcjmp (save_jump);
2798 restore_getcjmp (local_getcjmp);
2799 timer_start_idle ();
2800 c = kbd_buffer_get_event (&kb, used_mouse_menu);
2801 restore_getcjmp (save_jump);
2803 #ifdef MULTI_KBOARD
2804 if (! NILP (c) && (kb != current_kboard))
2806 Lisp_Object link = kb->kbd_queue;
2807 if (CONSP (link))
2809 while (CONSP (XCDR (link)))
2810 link = XCDR (link);
2811 if (!NILP (XCDR (link)))
2812 abort ();
2814 if (!CONSP (link))
2815 kb->kbd_queue = Fcons (c, Qnil);
2816 else
2817 XSETCDR (link, Fcons (c, Qnil));
2818 kb->kbd_queue_has_data = 1;
2819 c = Qnil;
2820 if (single_kboard)
2821 goto wrong_kboard;
2822 current_kboard = kb;
2823 /* This is going to exit from read_char
2824 so we had better get rid of this frame's stuff. */
2825 UNGCPRO;
2826 longjmp (wrong_kboard_jmpbuf, 1);
2828 #endif
2831 /* Terminate Emacs in batch mode if at eof. */
2832 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
2833 Fkill_emacs (make_number (1));
2835 if (INTEGERP (c))
2837 /* Add in any extra modifiers, where appropriate. */
2838 if ((extra_keyboard_modifiers & CHAR_CTL)
2839 || ((extra_keyboard_modifiers & 0177) < ' '
2840 && (extra_keyboard_modifiers & 0177) != 0))
2841 XSETINT (c, make_ctrl_char (XINT (c)));
2843 /* Transfer any other modifier bits directly from
2844 extra_keyboard_modifiers to c. Ignore the actual character code
2845 in the low 16 bits of extra_keyboard_modifiers. */
2846 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
2849 non_reread:
2851 /* Record the last idle start time so that we can reset it
2852 should the next event read be a help-echo. */
2853 last_idle_start = timer_idleness_start_time;
2854 timer_stop_idle ();
2855 RESUME_POLLING;
2857 if (NILP (c))
2859 if (commandflag >= 0
2860 && !input_pending && !detect_input_pending_run_timers (0))
2861 redisplay ();
2863 goto wrong_kboard;
2866 non_reread_1:
2868 /* Buffer switch events are only for internal wakeups
2869 so don't show them to the user.
2870 Also, don't record a key if we already did. */
2871 if (BUFFERP (c) || key_already_recorded)
2872 goto exit;
2874 /* Process special events within read_char
2875 and loop around to read another event. */
2876 save = Vquit_flag;
2877 Vquit_flag = Qnil;
2878 tem = access_keymap (get_keymap (Vspecial_event_map, 0, 1), c, 0, 0, 1);
2879 Vquit_flag = save;
2881 if (!NILP (tem))
2883 int was_locked = single_kboard;
2885 last_input_char = c;
2886 Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt);
2888 if (CONSP (c) && EQ (XCAR (c), Qselect_window))
2889 /* We stopped being idle for this event; undo that. This
2890 prevents automatic window selection (under
2891 mouse_autoselect_window from acting as a real input event, for
2892 example banishing the mouse under mouse-avoidance-mode. */
2893 timer_idleness_start_time = last_idle_start;
2895 /* Resume allowing input from any kboard, if that was true before. */
2896 if (!was_locked)
2897 any_kboard_state ();
2899 goto retry;
2902 /* Handle things that only apply to characters. */
2903 if (INTEGERP (c))
2905 /* If kbd_buffer_get_event gave us an EOF, return that. */
2906 if (XINT (c) == -1)
2907 goto exit;
2909 if ((STRINGP (Vkeyboard_translate_table)
2910 && SCHARS (Vkeyboard_translate_table) > (unsigned) XFASTINT (c))
2911 || (VECTORP (Vkeyboard_translate_table)
2912 && XVECTOR (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2913 || (CHAR_TABLE_P (Vkeyboard_translate_table)
2914 && CHAR_VALID_P (XINT (c), 0)))
2916 Lisp_Object d;
2917 d = Faref (Vkeyboard_translate_table, c);
2918 /* nil in keyboard-translate-table means no translation. */
2919 if (!NILP (d))
2920 c = d;
2924 /* If this event is a mouse click in the menu bar,
2925 return just menu-bar for now. Modify the mouse click event
2926 so we won't do this twice, then queue it up. */
2927 if (EVENT_HAS_PARAMETERS (c)
2928 && CONSP (XCDR (c))
2929 && CONSP (EVENT_START (c))
2930 && CONSP (XCDR (EVENT_START (c))))
2932 Lisp_Object posn;
2934 posn = POSN_POSN (EVENT_START (c));
2935 /* Handle menu-bar events:
2936 insert the dummy prefix event `menu-bar'. */
2937 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
2939 /* Change menu-bar to (menu-bar) as the event "position". */
2940 POSN_SET_POSN (EVENT_START (c), Fcons (posn, Qnil));
2942 also_record = c;
2943 Vunread_command_events = Fcons (c, Vunread_command_events);
2944 c = posn;
2948 /* Store these characters into recent_keys, the dribble file if any,
2949 and the keyboard macro being defined, if any. */
2950 record_char (c);
2951 if (! NILP (also_record))
2952 record_char (also_record);
2954 /* Wipe the echo area.
2955 But first, if we are about to use an input method,
2956 save the echo area contents for it to refer to. */
2957 if (INTEGERP (c)
2958 && ! NILP (Vinput_method_function)
2959 && (unsigned) XINT (c) >= ' '
2960 && (unsigned) XINT (c) != 127
2961 && (unsigned) XINT (c) < 256)
2963 previous_echo_area_message = Fcurrent_message ();
2964 Vinput_method_previous_message = previous_echo_area_message;
2967 /* Now wipe the echo area, except for help events which do their
2968 own stuff with the echo area. */
2969 if (!CONSP (c)
2970 || (!(EQ (Qhelp_echo, XCAR (c)))
2971 && !(EQ (Qswitch_frame, XCAR (c)))))
2973 if (!NILP (echo_area_buffer[0]))
2974 safe_run_hooks (Qecho_area_clear_hook);
2975 clear_message (1, 0);
2978 reread_for_input_method:
2979 from_macro:
2980 /* Pass this to the input method, if appropriate. */
2981 if (INTEGERP (c)
2982 && ! NILP (Vinput_method_function)
2983 /* Don't run the input method within a key sequence,
2984 after the first event of the key sequence. */
2985 && NILP (prev_event)
2986 && (unsigned) XINT (c) >= ' '
2987 && (unsigned) XINT (c) != 127
2988 && (unsigned) XINT (c) < 256)
2990 Lisp_Object keys;
2991 int key_count, key_count_reset;
2992 struct gcpro gcpro1;
2993 int count = SPECPDL_INDEX ();
2995 /* Save the echo status. */
2996 int saved_immediate_echo = current_kboard->immediate_echo;
2997 struct kboard *saved_ok_to_echo = ok_to_echo_at_next_pause;
2998 int saved_echo_after_prompt = current_kboard->echo_after_prompt;
3000 #if 0
3001 if (before_command_restore_flag)
3003 this_command_key_count = before_command_key_count_1;
3004 if (this_command_key_count < this_single_command_key_start)
3005 this_single_command_key_start = this_command_key_count;
3006 echo_truncate (before_command_echo_length_1);
3007 before_command_restore_flag = 0;
3009 #endif
3011 /* Save the this_command_keys status. */
3012 key_count = this_command_key_count;
3013 key_count_reset = this_command_key_count_reset;
3015 if (key_count > 0)
3016 keys = Fcopy_sequence (this_command_keys);
3017 else
3018 keys = Qnil;
3019 GCPRO1 (keys);
3021 /* Clear out this_command_keys. */
3022 this_command_key_count = 0;
3023 this_command_key_count_reset = 0;
3025 /* Now wipe the echo area. */
3026 if (!NILP (echo_area_buffer[0]))
3027 safe_run_hooks (Qecho_area_clear_hook);
3028 clear_message (1, 0);
3029 echo_truncate (0);
3031 /* If we are not reading a key sequence,
3032 never use the echo area. */
3033 if (maps == 0)
3035 specbind (Qinput_method_use_echo_area, Qt);
3038 /* Call the input method. */
3039 tem = call1 (Vinput_method_function, c);
3041 tem = unbind_to (count, tem);
3043 /* Restore the saved echoing state
3044 and this_command_keys state. */
3045 this_command_key_count = key_count;
3046 this_command_key_count_reset = key_count_reset;
3047 if (key_count > 0)
3048 this_command_keys = keys;
3050 cancel_echoing ();
3051 ok_to_echo_at_next_pause = saved_ok_to_echo;
3052 current_kboard->echo_after_prompt = saved_echo_after_prompt;
3053 if (saved_immediate_echo)
3054 echo_now ();
3056 UNGCPRO;
3058 /* The input method can return no events. */
3059 if (! CONSP (tem))
3061 /* Bring back the previous message, if any. */
3062 if (! NILP (previous_echo_area_message))
3063 message_with_string ("%s", previous_echo_area_message, 0);
3064 goto retry;
3066 /* It returned one event or more. */
3067 c = XCAR (tem);
3068 Vunread_post_input_method_events
3069 = nconc2 (XCDR (tem), Vunread_post_input_method_events);
3072 reread_first:
3074 /* Display help if not echoing. */
3075 if (CONSP (c) && EQ (XCAR (c), Qhelp_echo))
3077 /* (help-echo FRAME HELP WINDOW OBJECT POS). */
3078 Lisp_Object help, object, position, window, tem;
3080 tem = Fcdr (XCDR (c));
3081 help = Fcar (tem);
3082 tem = Fcdr (tem);
3083 window = Fcar (tem);
3084 tem = Fcdr (tem);
3085 object = Fcar (tem);
3086 tem = Fcdr (tem);
3087 position = Fcar (tem);
3089 show_help_echo (help, window, object, position, 0);
3091 /* We stopped being idle for this event; undo that. */
3092 timer_idleness_start_time = last_idle_start;
3093 goto retry;
3096 if (! reread || this_command_key_count == 0
3097 || this_command_key_count_reset)
3100 /* Don't echo mouse motion events. */
3101 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
3102 && NILP (Fzerop (Vecho_keystrokes))
3103 && ! (EVENT_HAS_PARAMETERS (c)
3104 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
3106 echo_char (c);
3107 if (! NILP (also_record))
3108 echo_char (also_record);
3109 /* Once we reread a character, echoing can happen
3110 the next time we pause to read a new one. */
3111 ok_to_echo_at_next_pause = current_kboard;
3114 /* Record this character as part of the current key. */
3115 add_command_key (c);
3116 if (! NILP (also_record))
3117 add_command_key (also_record);
3120 last_input_char = c;
3121 num_input_events++;
3123 /* Process the help character specially if enabled */
3124 if (!NILP (Vhelp_form) && help_char_p (c))
3126 Lisp_Object tem0;
3127 count = SPECPDL_INDEX ();
3129 record_unwind_protect (Fset_window_configuration,
3130 Fcurrent_window_configuration (Qnil));
3132 tem0 = Feval (Vhelp_form);
3133 if (STRINGP (tem0))
3134 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
3136 cancel_echoing ();
3138 c = read_char (0, 0, 0, Qnil, 0);
3139 while (BUFFERP (c));
3140 /* Remove the help from the frame */
3141 unbind_to (count, Qnil);
3143 redisplay ();
3144 if (EQ (c, make_number (040)))
3146 cancel_echoing ();
3148 c = read_char (0, 0, 0, Qnil, 0);
3149 while (BUFFERP (c));
3153 exit:
3154 RESUME_POLLING;
3155 RETURN_UNGCPRO (c);
3158 /* Record a key that came from a mouse menu.
3159 Record it for echoing, for this-command-keys, and so on. */
3161 static void
3162 record_menu_key (c)
3163 Lisp_Object c;
3165 /* Wipe the echo area. */
3166 clear_message (1, 0);
3168 record_char (c);
3170 #if 0
3171 before_command_key_count = this_command_key_count;
3172 before_command_echo_length = echo_length ();
3173 #endif
3175 /* Don't echo mouse motion events. */
3176 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
3177 && NILP (Fzerop (Vecho_keystrokes)))
3179 echo_char (c);
3181 /* Once we reread a character, echoing can happen
3182 the next time we pause to read a new one. */
3183 ok_to_echo_at_next_pause = 0;
3186 /* Record this character as part of the current key. */
3187 add_command_key (c);
3189 /* Re-reading in the middle of a command */
3190 last_input_char = c;
3191 num_input_events++;
3194 /* Return 1 if should recognize C as "the help character". */
3197 help_char_p (c)
3198 Lisp_Object c;
3200 Lisp_Object tail;
3202 if (EQ (c, Vhelp_char))
3203 return 1;
3204 for (tail = Vhelp_event_list; CONSP (tail); tail = XCDR (tail))
3205 if (EQ (c, XCAR (tail)))
3206 return 1;
3207 return 0;
3210 /* Record the input event C in various ways. */
3212 static void
3213 record_char (c)
3214 Lisp_Object c;
3216 int recorded = 0;
3218 if (CONSP (c) && (EQ (XCAR (c), Qhelp_echo) || EQ (XCAR (c), Qmouse_movement)))
3220 /* To avoid filling recent_keys with help-echo and mouse-movement
3221 events, we filter out repeated help-echo events, only store the
3222 first and last in a series of mouse-movement events, and don't
3223 store repeated help-echo events which are only separated by
3224 mouse-movement events. */
3226 Lisp_Object ev1, ev2, ev3;
3227 int ix1, ix2, ix3;
3229 if ((ix1 = recent_keys_index - 1) < 0)
3230 ix1 = NUM_RECENT_KEYS - 1;
3231 ev1 = AREF (recent_keys, ix1);
3233 if ((ix2 = ix1 - 1) < 0)
3234 ix2 = NUM_RECENT_KEYS - 1;
3235 ev2 = AREF (recent_keys, ix2);
3237 if ((ix3 = ix2 - 1) < 0)
3238 ix3 = NUM_RECENT_KEYS - 1;
3239 ev3 = AREF (recent_keys, ix3);
3241 if (EQ (XCAR (c), Qhelp_echo))
3243 /* Don't record `help-echo' in recent_keys unless it shows some help
3244 message, and a different help than the previously recorded
3245 event. */
3246 Lisp_Object help, last_help;
3248 help = Fcar_safe (Fcdr_safe (XCDR (c)));
3249 if (!STRINGP (help))
3250 recorded = 1;
3251 else if (CONSP (ev1) && EQ (XCAR (ev1), Qhelp_echo)
3252 && (last_help = Fcar_safe (Fcdr_safe (XCDR (ev1))), EQ (last_help, help)))
3253 recorded = 1;
3254 else if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
3255 && CONSP (ev2) && EQ (XCAR (ev2), Qhelp_echo)
3256 && (last_help = Fcar_safe (Fcdr_safe (XCDR (ev2))), EQ (last_help, help)))
3257 recorded = -1;
3258 else if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
3259 && CONSP (ev2) && EQ (XCAR (ev2), Qmouse_movement)
3260 && CONSP (ev3) && EQ (XCAR (ev3), Qhelp_echo)
3261 && (last_help = Fcar_safe (Fcdr_safe (XCDR (ev3))), EQ (last_help, help)))
3262 recorded = -2;
3264 else if (EQ (XCAR (c), Qmouse_movement))
3266 /* Only record one pair of `mouse-movement' on a window in recent_keys.
3267 So additional mouse movement events replace the last element. */
3268 Lisp_Object last_window, window;
3270 window = Fcar_safe (Fcar_safe (XCDR (c)));
3271 if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
3272 && (last_window = Fcar_safe (Fcar_safe (XCDR (ev1))), EQ (last_window, window))
3273 && CONSP (ev2) && EQ (XCAR (ev2), Qmouse_movement)
3274 && (last_window = Fcar_safe (Fcar_safe (XCDR (ev2))), EQ (last_window, window)))
3276 ASET (recent_keys, ix1, c);
3277 recorded = 1;
3281 else
3282 store_kbd_macro_char (c);
3284 if (!recorded)
3286 total_keys++;
3287 ASET (recent_keys, recent_keys_index, c);
3288 if (++recent_keys_index >= NUM_RECENT_KEYS)
3289 recent_keys_index = 0;
3291 else if (recorded < 0)
3293 /* We need to remove one or two events from recent_keys.
3294 To do this, we simply put nil at those events and move the
3295 recent_keys_index backwards over those events. Usually,
3296 users will never see those nil events, as they will be
3297 overwritten by the command keys entered to see recent_keys
3298 (e.g. C-h l). */
3300 while (recorded++ < 0 && total_keys > 0)
3302 if (total_keys < NUM_RECENT_KEYS)
3303 total_keys--;
3304 if (--recent_keys_index < 0)
3305 recent_keys_index = NUM_RECENT_KEYS - 1;
3306 ASET (recent_keys, recent_keys_index, Qnil);
3310 num_nonmacro_input_events++;
3312 /* Write c to the dribble file. If c is a lispy event, write
3313 the event's symbol to the dribble file, in <brackets>. Bleaugh.
3314 If you, dear reader, have a better idea, you've got the source. :-) */
3315 if (dribble)
3317 if (INTEGERP (c))
3319 if (XUINT (c) < 0x100)
3320 putc (XINT (c), dribble);
3321 else
3322 fprintf (dribble, " 0x%x", (int) XUINT (c));
3324 else
3326 Lisp_Object dribblee;
3328 /* If it's a structured event, take the event header. */
3329 dribblee = EVENT_HEAD (c);
3331 if (SYMBOLP (dribblee))
3333 putc ('<', dribble);
3334 fwrite (SDATA (SYMBOL_NAME (dribblee)), sizeof (char),
3335 SBYTES (SYMBOL_NAME (dribblee)),
3336 dribble);
3337 putc ('>', dribble);
3341 fflush (dribble);
3345 Lisp_Object
3346 print_help (object)
3347 Lisp_Object object;
3349 struct buffer *old = current_buffer;
3350 Fprinc (object, Qnil);
3351 set_buffer_internal (XBUFFER (Vstandard_output));
3352 call0 (intern ("help-mode"));
3353 set_buffer_internal (old);
3354 return Qnil;
3357 /* Copy out or in the info on where C-g should throw to.
3358 This is used when running Lisp code from within get_char,
3359 in case get_char is called recursively.
3360 See read_process_output. */
3362 static void
3363 save_getcjmp (temp)
3364 jmp_buf temp;
3366 bcopy (getcjmp, temp, sizeof getcjmp);
3369 static void
3370 restore_getcjmp (temp)
3371 jmp_buf temp;
3373 bcopy (temp, getcjmp, sizeof getcjmp);
3376 #ifdef HAVE_MOUSE
3378 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
3379 of this function. */
3381 static Lisp_Object
3382 tracking_off (old_value)
3383 Lisp_Object old_value;
3385 do_mouse_tracking = old_value;
3386 if (NILP (old_value))
3388 /* Redisplay may have been preempted because there was input
3389 available, and it assumes it will be called again after the
3390 input has been processed. If the only input available was
3391 the sort that we have just disabled, then we need to call
3392 redisplay. */
3393 if (!readable_events (1))
3395 redisplay_preserve_echo_area (6);
3396 get_input_pending (&input_pending, 1);
3399 return Qnil;
3402 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
3403 doc: /* Evaluate BODY with mouse movement events enabled.
3404 Within a `track-mouse' form, mouse motion generates input events that
3405 you can read with `read-event'.
3406 Normally, mouse motion is ignored.
3407 usage: (track-mouse BODY ...) */)
3408 (args)
3409 Lisp_Object args;
3411 int count = SPECPDL_INDEX ();
3412 Lisp_Object val;
3414 record_unwind_protect (tracking_off, do_mouse_tracking);
3416 do_mouse_tracking = Qt;
3418 val = Fprogn (args);
3419 return unbind_to (count, val);
3422 /* If mouse has moved on some frame, return one of those frames.
3423 Return 0 otherwise. */
3425 static FRAME_PTR
3426 some_mouse_moved ()
3428 Lisp_Object tail, frame;
3430 FOR_EACH_FRAME (tail, frame)
3432 if (XFRAME (frame)->mouse_moved)
3433 return XFRAME (frame);
3436 return 0;
3439 #endif /* HAVE_MOUSE */
3441 /* Low level keyboard/mouse input.
3442 kbd_buffer_store_event places events in kbd_buffer, and
3443 kbd_buffer_get_event retrieves them. */
3445 /* Return true iff there are any events in the queue that read-char
3446 would return. If this returns false, a read-char would block. */
3447 static int
3448 readable_filtered_events (do_timers_now, filter_events)
3449 int do_timers_now;
3450 int filter_events;
3452 if (do_timers_now)
3453 timer_check (do_timers_now);
3455 /* If the buffer contains only FOCUS_IN_EVENT events,
3456 and FILTER_EVENTS is nonzero, report it as empty. */
3457 if (kbd_fetch_ptr != kbd_store_ptr)
3459 int have_live_event = 1;
3461 if (filter_events)
3463 struct input_event *event;
3465 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3466 ? kbd_fetch_ptr
3467 : kbd_buffer);
3469 while (have_live_event && event->kind == FOCUS_IN_EVENT)
3471 event++;
3472 if (event == kbd_buffer + KBD_BUFFER_SIZE)
3473 event = kbd_buffer;
3474 if (event == kbd_store_ptr)
3475 have_live_event = 0;
3478 if (have_live_event) return 1;
3481 #ifdef HAVE_MOUSE
3482 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3483 return 1;
3484 #endif
3485 if (single_kboard)
3487 if (current_kboard->kbd_queue_has_data)
3488 return 1;
3490 else
3492 KBOARD *kb;
3493 for (kb = all_kboards; kb; kb = kb->next_kboard)
3494 if (kb->kbd_queue_has_data)
3495 return 1;
3497 return 0;
3500 /* Return true iff there are any events in the queue that read-char
3501 would return. If this returns false, a read-char would block. */
3502 static int
3503 readable_events (do_timers_now)
3504 int do_timers_now;
3506 return readable_filtered_events (do_timers_now, 0);
3509 /* Set this for debugging, to have a way to get out */
3510 int stop_character;
3512 #ifdef MULTI_KBOARD
3513 static KBOARD *
3514 event_to_kboard (event)
3515 struct input_event *event;
3517 Lisp_Object frame;
3518 frame = event->frame_or_window;
3519 if (CONSP (frame))
3520 frame = XCAR (frame);
3521 else if (WINDOWP (frame))
3522 frame = WINDOW_FRAME (XWINDOW (frame));
3524 /* There are still some events that don't set this field.
3525 For now, just ignore the problem.
3526 Also ignore dead frames here. */
3527 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
3528 return 0;
3529 else
3530 return FRAME_KBOARD (XFRAME (frame));
3532 #endif
3534 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
3536 void
3537 kbd_buffer_store_event (event)
3538 register struct input_event *event;
3540 kbd_buffer_store_event_hold (event, 0);
3543 /* Store EVENT obtained at interrupt level into kbd_buffer, fifo.
3545 If HOLD_QUIT is 0, just stuff EVENT into the fifo.
3546 Else, if HOLD_QUIT.kind != NO_EVENT, discard EVENT.
3547 Else, if EVENT is a quit event, store the quit event
3548 in HOLD_QUIT, and return (thus ignoring further events).
3550 This is used in read_avail_input to postpone the processing
3551 of the quit event until all subsequent input events have been
3552 parsed (and discarded).
3555 void
3556 kbd_buffer_store_event_hold (event, hold_quit)
3557 register struct input_event *event;
3558 struct input_event *hold_quit;
3560 if (event->kind == NO_EVENT)
3561 abort ();
3563 if (hold_quit && hold_quit->kind != NO_EVENT)
3564 return;
3566 if (event->kind == ASCII_KEYSTROKE_EVENT)
3568 register int c = event->code & 0377;
3570 if (event->modifiers & ctrl_modifier)
3571 c = make_ctrl_char (c);
3573 c |= (event->modifiers
3574 & (meta_modifier | alt_modifier
3575 | hyper_modifier | super_modifier));
3577 if (c == quit_char)
3579 #ifdef MULTI_KBOARD
3580 KBOARD *kb;
3581 struct input_event *sp;
3583 if (single_kboard
3584 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
3585 kb != current_kboard))
3587 kb->kbd_queue
3588 = Fcons (make_lispy_switch_frame (event->frame_or_window),
3589 Fcons (make_number (c), Qnil));
3590 kb->kbd_queue_has_data = 1;
3591 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3593 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3594 sp = kbd_buffer;
3596 if (event_to_kboard (sp) == kb)
3598 sp->kind = NO_EVENT;
3599 sp->frame_or_window = Qnil;
3600 sp->arg = Qnil;
3603 return;
3605 #endif
3607 if (hold_quit)
3609 bcopy (event, (char *) hold_quit, sizeof (*event));
3610 return;
3613 /* If this results in a quit_char being returned to Emacs as
3614 input, set Vlast_event_frame properly. If this doesn't
3615 get returned to Emacs as an event, the next event read
3616 will set Vlast_event_frame again, so this is safe to do. */
3618 Lisp_Object focus;
3620 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
3621 if (NILP (focus))
3622 focus = event->frame_or_window;
3623 internal_last_event_frame = focus;
3624 Vlast_event_frame = focus;
3627 last_event_timestamp = event->timestamp;
3628 interrupt_signal (0 /* dummy */);
3629 return;
3632 if (c && c == stop_character)
3634 sys_suspend ();
3635 return;
3638 /* Don't insert two BUFFER_SWITCH_EVENT's in a row.
3639 Just ignore the second one. */
3640 else if (event->kind == BUFFER_SWITCH_EVENT
3641 && kbd_fetch_ptr != kbd_store_ptr
3642 && ((kbd_store_ptr == kbd_buffer
3643 ? kbd_buffer + KBD_BUFFER_SIZE - 1
3644 : kbd_store_ptr - 1)->kind) == BUFFER_SWITCH_EVENT)
3645 return;
3647 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
3648 kbd_store_ptr = kbd_buffer;
3650 /* Don't let the very last slot in the buffer become full,
3651 since that would make the two pointers equal,
3652 and that is indistinguishable from an empty buffer.
3653 Discard the event if it would fill the last slot. */
3654 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
3657 #if 0 /* The SELECTION_REQUEST_EVENT case looks bogus, and it's error
3658 prone to assign individual members for other events, in case
3659 the input_event structure is changed. --2000-07-13, gerd. */
3660 struct input_event *sp = kbd_store_ptr;
3661 sp->kind = event->kind;
3662 if (event->kind == SELECTION_REQUEST_EVENT)
3664 /* We must not use the ordinary copying code for this case,
3665 since `part' is an enum and copying it might not copy enough
3666 in this case. */
3667 bcopy (event, (char *) sp, sizeof (*event));
3669 else
3672 sp->code = event->code;
3673 sp->part = event->part;
3674 sp->frame_or_window = event->frame_or_window;
3675 sp->arg = event->arg;
3676 sp->modifiers = event->modifiers;
3677 sp->x = event->x;
3678 sp->y = event->y;
3679 sp->timestamp = event->timestamp;
3681 #else
3682 *kbd_store_ptr = *event;
3683 #endif
3685 ++kbd_store_ptr;
3690 /* Generate HELP_EVENT input_events in BUFP which has room for
3691 SIZE events. If there's not enough room in BUFP, ignore this
3692 event.
3694 HELP is the help form.
3696 FRAME is the frame on which the help is generated. OBJECT is the
3697 Lisp object where the help was found (a buffer, a string, an
3698 overlay, or nil if neither from a string nor from a buffer. POS is
3699 the position within OBJECT where the help was found.
3701 Value is the number of input_events generated. */
3703 void
3704 gen_help_event (help, frame, window, object, pos)
3705 Lisp_Object help, frame, object, window;
3706 int pos;
3708 struct input_event event;
3710 EVENT_INIT (event);
3712 event.kind = HELP_EVENT;
3713 event.frame_or_window = frame;
3714 event.arg = object;
3715 event.x = WINDOWP (window) ? window : frame;
3716 event.y = help;
3717 event.code = pos;
3718 kbd_buffer_store_event (&event);
3722 /* Store HELP_EVENTs for HELP on FRAME in the input queue. */
3724 void
3725 kbd_buffer_store_help_event (frame, help)
3726 Lisp_Object frame, help;
3728 struct input_event event;
3730 event.kind = HELP_EVENT;
3731 event.frame_or_window = frame;
3732 event.arg = Qnil;
3733 event.x = Qnil;
3734 event.y = help;
3735 event.code = 0;
3736 kbd_buffer_store_event (&event);
3740 /* Discard any mouse events in the event buffer by setting them to
3741 NO_EVENT. */
3742 void
3743 discard_mouse_events ()
3745 struct input_event *sp;
3746 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3748 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3749 sp = kbd_buffer;
3751 if (sp->kind == MOUSE_CLICK_EVENT
3752 || sp->kind == WHEEL_EVENT
3753 #ifdef WINDOWSNT
3754 || sp->kind == W32_SCROLL_BAR_CLICK_EVENT
3755 #endif
3756 || sp->kind == SCROLL_BAR_CLICK_EVENT)
3758 sp->kind = NO_EVENT;
3764 /* Return non-zero if there are any real events waiting in the event
3765 buffer, not counting `NO_EVENT's.
3767 If DISCARD is non-zero, discard NO_EVENT events at the front of
3768 the input queue, possibly leaving the input queue empty if there
3769 are no real input events. */
3772 kbd_buffer_events_waiting (discard)
3773 int discard;
3775 struct input_event *sp;
3777 for (sp = kbd_fetch_ptr;
3778 sp != kbd_store_ptr && sp->kind == NO_EVENT;
3779 ++sp)
3781 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3782 sp = kbd_buffer;
3785 if (discard)
3786 kbd_fetch_ptr = sp;
3788 return sp != kbd_store_ptr && sp->kind != NO_EVENT;
3792 /* Clear input event EVENT. */
3794 static INLINE void
3795 clear_event (event)
3796 struct input_event *event;
3798 event->kind = NO_EVENT;
3802 /* Read one event from the event buffer, waiting if necessary.
3803 The value is a Lisp object representing the event.
3804 The value is nil for an event that should be ignored,
3805 or that was handled here.
3806 We always read and discard one event. */
3808 static Lisp_Object
3809 kbd_buffer_get_event (kbp, used_mouse_menu)
3810 KBOARD **kbp;
3811 int *used_mouse_menu;
3813 register int c;
3814 Lisp_Object obj;
3816 if (noninteractive)
3818 c = getchar ();
3819 XSETINT (obj, c);
3820 *kbp = current_kboard;
3821 return obj;
3824 /* Wait until there is input available. */
3825 for (;;)
3827 if (kbd_fetch_ptr != kbd_store_ptr)
3828 break;
3829 #ifdef HAVE_MOUSE
3830 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3831 break;
3832 #endif
3834 /* If the quit flag is set, then read_char will return
3835 quit_char, so that counts as "available input." */
3836 if (!NILP (Vquit_flag))
3837 quit_throw_to_read_char ();
3839 /* One way or another, wait until input is available; then, if
3840 interrupt handlers have not read it, read it now. */
3842 #ifdef OLDVMS
3843 wait_for_kbd_input ();
3844 #else
3845 /* Note SIGIO has been undef'd if FIONREAD is missing. */
3846 #ifdef SIGIO
3847 gobble_input (0);
3848 #endif /* SIGIO */
3849 if (kbd_fetch_ptr != kbd_store_ptr)
3850 break;
3851 #ifdef HAVE_MOUSE
3852 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3853 break;
3854 #endif
3856 Lisp_Object minus_one;
3858 XSETINT (minus_one, -1);
3859 wait_reading_process_input (0, 0, minus_one, 1);
3861 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
3862 /* Pass 1 for EXPECT since we just waited to have input. */
3863 read_avail_input (1);
3865 #endif /* not VMS */
3868 if (CONSP (Vunread_command_events))
3870 Lisp_Object first;
3871 first = XCAR (Vunread_command_events);
3872 Vunread_command_events = XCDR (Vunread_command_events);
3873 *kbp = current_kboard;
3874 return first;
3877 /* At this point, we know that there is a readable event available
3878 somewhere. If the event queue is empty, then there must be a
3879 mouse movement enabled and available. */
3880 if (kbd_fetch_ptr != kbd_store_ptr)
3882 struct input_event *event;
3884 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3885 ? kbd_fetch_ptr
3886 : kbd_buffer);
3888 last_event_timestamp = event->timestamp;
3890 #ifdef MULTI_KBOARD
3891 *kbp = event_to_kboard (event);
3892 if (*kbp == 0)
3893 *kbp = current_kboard; /* Better than returning null ptr? */
3894 #else
3895 *kbp = &the_only_kboard;
3896 #endif
3898 obj = Qnil;
3900 /* These two kinds of events get special handling
3901 and don't actually appear to the command loop.
3902 We return nil for them. */
3903 if (event->kind == SELECTION_REQUEST_EVENT)
3905 #ifdef HAVE_X11
3906 struct input_event copy;
3908 /* Remove it from the buffer before processing it,
3909 since otherwise swallow_events will see it
3910 and process it again. */
3911 copy = *event;
3912 kbd_fetch_ptr = event + 1;
3913 input_pending = readable_events (0);
3914 x_handle_selection_request (&copy);
3915 #else
3916 /* We're getting selection request events, but we don't have
3917 a window system. */
3918 abort ();
3919 #endif
3922 else if (event->kind == SELECTION_CLEAR_EVENT)
3924 #ifdef HAVE_X11
3925 struct input_event copy;
3927 /* Remove it from the buffer before processing it. */
3928 copy = *event;
3929 kbd_fetch_ptr = event + 1;
3930 input_pending = readable_events (0);
3931 x_handle_selection_clear (&copy);
3932 #else
3933 /* We're getting selection request events, but we don't have
3934 a window system. */
3935 abort ();
3936 #endif
3938 #if defined (HAVE_X11) || defined (HAVE_NTGUI) || defined (MAC_OS)
3939 else if (event->kind == DELETE_WINDOW_EVENT)
3941 /* Make an event (delete-frame (FRAME)). */
3942 obj = Fcons (event->frame_or_window, Qnil);
3943 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
3944 kbd_fetch_ptr = event + 1;
3946 #endif
3947 #if defined (HAVE_X11) || defined (HAVE_NTGUI)
3948 else if (event->kind == ICONIFY_EVENT)
3950 /* Make an event (iconify-frame (FRAME)). */
3951 obj = Fcons (event->frame_or_window, Qnil);
3952 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
3953 kbd_fetch_ptr = event + 1;
3955 else if (event->kind == DEICONIFY_EVENT)
3957 /* Make an event (make-frame-visible (FRAME)). */
3958 obj = Fcons (event->frame_or_window, Qnil);
3959 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
3960 kbd_fetch_ptr = event + 1;
3962 #endif
3963 else if (event->kind == BUFFER_SWITCH_EVENT)
3965 /* The value doesn't matter here; only the type is tested. */
3966 XSETBUFFER (obj, current_buffer);
3967 kbd_fetch_ptr = event + 1;
3969 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
3970 || defined (USE_GTK)
3971 else if (event->kind == MENU_BAR_ACTIVATE_EVENT)
3973 kbd_fetch_ptr = event + 1;
3974 input_pending = readable_events (0);
3975 if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
3976 x_activate_menubar (XFRAME (event->frame_or_window));
3978 #endif
3979 #ifdef WINDOWSNT
3980 else if (event->kind == LANGUAGE_CHANGE_EVENT)
3982 /* Make an event (language-change (FRAME CHARSET LCID)). */
3983 obj = Fcons (event->frame_or_window, Qnil);
3984 obj = Fcons (Qlanguage_change, Fcons (obj, Qnil));
3985 kbd_fetch_ptr = event + 1;
3987 #endif
3988 else if (event->kind == SAVE_SESSION_EVENT)
3990 obj = Fcons (Qsave_session, Qnil);
3991 kbd_fetch_ptr = event + 1;
3993 /* Just discard these, by returning nil.
3994 With MULTI_KBOARD, these events are used as placeholders
3995 when we need to randomly delete events from the queue.
3996 (They shouldn't otherwise be found in the buffer,
3997 but on some machines it appears they do show up
3998 even without MULTI_KBOARD.) */
3999 /* On Windows NT/9X, NO_EVENT is used to delete extraneous
4000 mouse events during a popup-menu call. */
4001 else if (event->kind == NO_EVENT)
4002 kbd_fetch_ptr = event + 1;
4003 else if (event->kind == HELP_EVENT)
4005 Lisp_Object object, position, help, frame, window;
4007 frame = event->frame_or_window;
4008 object = event->arg;
4009 position = make_number (event->code);
4010 window = event->x;
4011 help = event->y;
4012 clear_event (event);
4014 kbd_fetch_ptr = event + 1;
4015 if (!WINDOWP (window))
4016 window = Qnil;
4017 obj = Fcons (Qhelp_echo,
4018 list5 (frame, help, window, object, position));
4020 else if (event->kind == FOCUS_IN_EVENT)
4022 /* Notification of a FocusIn event. The frame receiving the
4023 focus is in event->frame_or_window. Generate a
4024 switch-frame event if necessary. */
4025 Lisp_Object frame, focus;
4027 frame = event->frame_or_window;
4028 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
4029 if (FRAMEP (focus))
4030 frame = focus;
4032 if (!EQ (frame, internal_last_event_frame)
4033 && !EQ (frame, selected_frame))
4034 obj = make_lispy_switch_frame (frame);
4035 internal_last_event_frame = frame;
4036 kbd_fetch_ptr = event + 1;
4038 else
4040 /* If this event is on a different frame, return a switch-frame this
4041 time, and leave the event in the queue for next time. */
4042 Lisp_Object frame;
4043 Lisp_Object focus;
4045 frame = event->frame_or_window;
4046 if (CONSP (frame))
4047 frame = XCAR (frame);
4048 else if (WINDOWP (frame))
4049 frame = WINDOW_FRAME (XWINDOW (frame));
4051 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
4052 if (! NILP (focus))
4053 frame = focus;
4055 if (! EQ (frame, internal_last_event_frame)
4056 && !EQ (frame, selected_frame))
4057 obj = make_lispy_switch_frame (frame);
4058 internal_last_event_frame = frame;
4060 /* If we didn't decide to make a switch-frame event, go ahead
4061 and build a real event from the queue entry. */
4063 if (NILP (obj))
4065 obj = make_lispy_event (event);
4067 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined(MAC_OS) \
4068 || defined (USE_GTK)
4069 /* If this was a menu selection, then set the flag to inhibit
4070 writing to last_nonmenu_event. Don't do this if the event
4071 we're returning is (menu-bar), though; that indicates the
4072 beginning of the menu sequence, and we might as well leave
4073 that as the `event with parameters' for this selection. */
4074 if (used_mouse_menu
4075 && !EQ (event->frame_or_window, event->arg)
4076 && (event->kind == MENU_BAR_EVENT
4077 || event->kind == TOOL_BAR_EVENT))
4078 *used_mouse_menu = 1;
4079 #endif
4081 /* Wipe out this event, to catch bugs. */
4082 clear_event (event);
4083 kbd_fetch_ptr = event + 1;
4087 #ifdef HAVE_MOUSE
4088 /* Try generating a mouse motion event. */
4089 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
4091 FRAME_PTR f = some_mouse_moved ();
4092 Lisp_Object bar_window;
4093 enum scroll_bar_part part;
4094 Lisp_Object x, y;
4095 unsigned long time;
4097 *kbp = current_kboard;
4098 /* Note that this uses F to determine which display to look at.
4099 If there is no valid info, it does not store anything
4100 so x remains nil. */
4101 x = Qnil;
4102 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
4104 obj = Qnil;
4106 /* Decide if we should generate a switch-frame event. Don't
4107 generate switch-frame events for motion outside of all Emacs
4108 frames. */
4109 if (!NILP (x) && f)
4111 Lisp_Object frame;
4113 frame = FRAME_FOCUS_FRAME (f);
4114 if (NILP (frame))
4115 XSETFRAME (frame, f);
4117 if (! EQ (frame, internal_last_event_frame)
4118 && !EQ (frame, selected_frame))
4119 obj = make_lispy_switch_frame (frame);
4120 internal_last_event_frame = frame;
4123 /* If we didn't decide to make a switch-frame event, go ahead and
4124 return a mouse-motion event. */
4125 if (!NILP (x) && NILP (obj))
4126 obj = make_lispy_movement (f, bar_window, part, x, y, time);
4128 #endif /* HAVE_MOUSE */
4129 else
4130 /* We were promised by the above while loop that there was
4131 something for us to read! */
4132 abort ();
4134 input_pending = readable_events (0);
4136 Vlast_event_frame = internal_last_event_frame;
4138 return (obj);
4141 /* Process any events that are not user-visible,
4142 then return, without reading any user-visible events. */
4144 void
4145 swallow_events (do_display)
4146 int do_display;
4148 int old_timers_run;
4150 while (kbd_fetch_ptr != kbd_store_ptr)
4152 struct input_event *event;
4154 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
4155 ? kbd_fetch_ptr
4156 : kbd_buffer);
4158 last_event_timestamp = event->timestamp;
4160 /* These two kinds of events get special handling
4161 and don't actually appear to the command loop. */
4162 if (event->kind == SELECTION_REQUEST_EVENT)
4164 #ifdef HAVE_X11
4165 struct input_event copy;
4167 /* Remove it from the buffer before processing it,
4168 since otherwise swallow_events called recursively could see it
4169 and process it again. */
4170 copy = *event;
4171 kbd_fetch_ptr = event + 1;
4172 input_pending = readable_events (0);
4173 x_handle_selection_request (&copy);
4174 #else
4175 /* We're getting selection request events, but we don't have
4176 a window system. */
4177 abort ();
4178 #endif
4181 else if (event->kind == SELECTION_CLEAR_EVENT)
4183 #ifdef HAVE_X11
4184 struct input_event copy;
4186 /* Remove it from the buffer before processing it, */
4187 copy = *event;
4189 kbd_fetch_ptr = event + 1;
4190 input_pending = readable_events (0);
4191 x_handle_selection_clear (&copy);
4192 #else
4193 /* We're getting selection request events, but we don't have
4194 a window system. */
4195 abort ();
4196 #endif
4198 else
4199 break;
4202 old_timers_run = timers_run;
4203 get_input_pending (&input_pending, 1);
4205 if (timers_run != old_timers_run && do_display)
4206 redisplay_preserve_echo_area (7);
4209 /* Record the start of when Emacs is idle,
4210 for the sake of running idle-time timers. */
4212 void
4213 timer_start_idle ()
4215 Lisp_Object timers;
4217 /* If we are already in the idle state, do nothing. */
4218 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4219 return;
4221 EMACS_GET_TIME (timer_idleness_start_time);
4223 timer_last_idleness_start_time = timer_idleness_start_time;
4225 /* Mark all idle-time timers as once again candidates for running. */
4226 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCDR (timers))
4228 Lisp_Object timer;
4230 timer = XCAR (timers);
4232 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
4233 continue;
4234 XVECTOR (timer)->contents[0] = Qnil;
4238 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
4240 void
4241 timer_stop_idle ()
4243 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
4246 /* This is only for debugging. */
4247 struct input_event last_timer_event;
4249 /* Check whether a timer has fired. To prevent larger problems we simply
4250 disregard elements that are not proper timers. Do not make a circular
4251 timer list for the time being.
4253 Returns the number of seconds to wait until the next timer fires. If a
4254 timer is triggering now, return zero seconds.
4255 If no timer is active, return -1 seconds.
4257 If a timer is ripe, we run it, with quitting turned off.
4259 DO_IT_NOW is now ignored. It used to mean that we should
4260 run the timer directly instead of queueing a timer-event.
4261 Now we always run timers directly. */
4263 EMACS_TIME
4264 timer_check (do_it_now)
4265 int do_it_now;
4267 EMACS_TIME nexttime;
4268 EMACS_TIME now, idleness_now;
4269 Lisp_Object timers, idle_timers, chosen_timer;
4270 struct gcpro gcpro1, gcpro2, gcpro3;
4272 EMACS_SET_SECS (nexttime, -1);
4273 EMACS_SET_USECS (nexttime, -1);
4275 /* Always consider the ordinary timers. */
4276 timers = Vtimer_list;
4277 /* Consider the idle timers only if Emacs is idle. */
4278 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4279 idle_timers = Vtimer_idle_list;
4280 else
4281 idle_timers = Qnil;
4282 chosen_timer = Qnil;
4283 GCPRO3 (timers, idle_timers, chosen_timer);
4285 if (CONSP (timers) || CONSP (idle_timers))
4287 EMACS_GET_TIME (now);
4288 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4289 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
4292 while (CONSP (timers) || CONSP (idle_timers))
4294 Lisp_Object *vector;
4295 Lisp_Object timer = Qnil, idle_timer = Qnil;
4296 EMACS_TIME timer_time, idle_timer_time;
4297 EMACS_TIME difference, timer_difference, idle_timer_difference;
4299 /* Skip past invalid timers and timers already handled. */
4300 if (!NILP (timers))
4302 timer = XCAR (timers);
4303 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
4305 timers = XCDR (timers);
4306 continue;
4308 vector = XVECTOR (timer)->contents;
4310 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
4311 || !INTEGERP (vector[3])
4312 || ! NILP (vector[0]))
4314 timers = XCDR (timers);
4315 continue;
4318 if (!NILP (idle_timers))
4320 timer = XCAR (idle_timers);
4321 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
4323 idle_timers = XCDR (idle_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 idle_timers = XCDR (idle_timers);
4333 continue;
4337 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
4338 based on the next ordinary timer.
4339 TIMER_DIFFERENCE is the distance in time from NOW to when
4340 this timer becomes ripe (negative if it's already ripe). */
4341 if (!NILP (timers))
4343 timer = XCAR (timers);
4344 vector = XVECTOR (timer)->contents;
4345 EMACS_SET_SECS (timer_time,
4346 (XINT (vector[1]) << 16) | (XINT (vector[2])));
4347 EMACS_SET_USECS (timer_time, XINT (vector[3]));
4348 EMACS_SUB_TIME (timer_difference, timer_time, now);
4351 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
4352 based on the next idle timer. */
4353 if (!NILP (idle_timers))
4355 idle_timer = XCAR (idle_timers);
4356 vector = XVECTOR (idle_timer)->contents;
4357 EMACS_SET_SECS (idle_timer_time,
4358 (XINT (vector[1]) << 16) | (XINT (vector[2])));
4359 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
4360 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
4363 /* Decide which timer is the next timer,
4364 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
4365 Also step down the list where we found that timer. */
4367 if (! NILP (timers) && ! NILP (idle_timers))
4369 EMACS_TIME temp;
4370 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
4371 if (EMACS_TIME_NEG_P (temp))
4373 chosen_timer = timer;
4374 timers = XCDR (timers);
4375 difference = timer_difference;
4377 else
4379 chosen_timer = idle_timer;
4380 idle_timers = XCDR (idle_timers);
4381 difference = idle_timer_difference;
4384 else if (! NILP (timers))
4386 chosen_timer = timer;
4387 timers = XCDR (timers);
4388 difference = timer_difference;
4390 else
4392 chosen_timer = idle_timer;
4393 idle_timers = XCDR (idle_timers);
4394 difference = idle_timer_difference;
4396 vector = XVECTOR (chosen_timer)->contents;
4398 /* If timer is ripe, run it if it hasn't been run. */
4399 if (EMACS_TIME_NEG_P (difference)
4400 || (EMACS_SECS (difference) == 0
4401 && EMACS_USECS (difference) == 0))
4403 if (NILP (vector[0]))
4405 int was_locked = single_kboard;
4406 int count = SPECPDL_INDEX ();
4407 Lisp_Object old_deactivate_mark = Vdeactivate_mark;
4409 /* Mark the timer as triggered to prevent problems if the lisp
4410 code fails to reschedule it right. */
4411 vector[0] = Qt;
4413 specbind (Qinhibit_quit, Qt);
4415 call1 (Qtimer_event_handler, chosen_timer);
4416 Vdeactivate_mark = old_deactivate_mark;
4417 timers_run++;
4418 unbind_to (count, Qnil);
4420 /* Resume allowing input from any kboard, if that was true before. */
4421 if (!was_locked)
4422 any_kboard_state ();
4424 /* Since we have handled the event,
4425 we don't need to tell the caller to wake up and do it. */
4428 else
4429 /* When we encounter a timer that is still waiting,
4430 return the amount of time to wait before it is ripe. */
4432 UNGCPRO;
4433 return difference;
4437 /* No timers are pending in the future. */
4438 /* Return 0 if we generated an event, and -1 if not. */
4439 UNGCPRO;
4440 return nexttime;
4443 /* Caches for modify_event_symbol. */
4444 static Lisp_Object accent_key_syms;
4445 static Lisp_Object func_key_syms;
4446 static Lisp_Object mouse_syms;
4447 static Lisp_Object wheel_syms;
4448 static Lisp_Object drag_n_drop_syms;
4450 /* This is a list of keysym codes for special "accent" characters.
4451 It parallels lispy_accent_keys. */
4453 static int lispy_accent_codes[] =
4455 #ifdef XK_dead_circumflex
4456 XK_dead_circumflex,
4457 #else
4459 #endif
4460 #ifdef XK_dead_grave
4461 XK_dead_grave,
4462 #else
4464 #endif
4465 #ifdef XK_dead_tilde
4466 XK_dead_tilde,
4467 #else
4469 #endif
4470 #ifdef XK_dead_diaeresis
4471 XK_dead_diaeresis,
4472 #else
4474 #endif
4475 #ifdef XK_dead_macron
4476 XK_dead_macron,
4477 #else
4479 #endif
4480 #ifdef XK_dead_degree
4481 XK_dead_degree,
4482 #else
4484 #endif
4485 #ifdef XK_dead_acute
4486 XK_dead_acute,
4487 #else
4489 #endif
4490 #ifdef XK_dead_cedilla
4491 XK_dead_cedilla,
4492 #else
4494 #endif
4495 #ifdef XK_dead_breve
4496 XK_dead_breve,
4497 #else
4499 #endif
4500 #ifdef XK_dead_ogonek
4501 XK_dead_ogonek,
4502 #else
4504 #endif
4505 #ifdef XK_dead_caron
4506 XK_dead_caron,
4507 #else
4509 #endif
4510 #ifdef XK_dead_doubleacute
4511 XK_dead_doubleacute,
4512 #else
4514 #endif
4515 #ifdef XK_dead_abovedot
4516 XK_dead_abovedot,
4517 #else
4519 #endif
4520 #ifdef XK_dead_abovering
4521 XK_dead_abovering,
4522 #else
4524 #endif
4525 #ifdef XK_dead_iota
4526 XK_dead_iota,
4527 #else
4529 #endif
4530 #ifdef XK_dead_belowdot
4531 XK_dead_belowdot,
4532 #else
4534 #endif
4535 #ifdef XK_dead_voiced_sound
4536 XK_dead_voiced_sound,
4537 #else
4539 #endif
4540 #ifdef XK_dead_semivoiced_sound
4541 XK_dead_semivoiced_sound,
4542 #else
4544 #endif
4545 #ifdef XK_dead_hook
4546 XK_dead_hook,
4547 #else
4549 #endif
4550 #ifdef XK_dead_horn
4551 XK_dead_horn,
4552 #else
4554 #endif
4557 /* This is a list of Lisp names for special "accent" characters.
4558 It parallels lispy_accent_codes. */
4560 static char *lispy_accent_keys[] =
4562 "dead-circumflex",
4563 "dead-grave",
4564 "dead-tilde",
4565 "dead-diaeresis",
4566 "dead-macron",
4567 "dead-degree",
4568 "dead-acute",
4569 "dead-cedilla",
4570 "dead-breve",
4571 "dead-ogonek",
4572 "dead-caron",
4573 "dead-doubleacute",
4574 "dead-abovedot",
4575 "dead-abovering",
4576 "dead-iota",
4577 "dead-belowdot",
4578 "dead-voiced-sound",
4579 "dead-semivoiced-sound",
4580 "dead-hook",
4581 "dead-horn",
4584 #ifdef HAVE_NTGUI
4585 #define FUNCTION_KEY_OFFSET 0x0
4587 char *lispy_function_keys[] =
4589 0, /* 0 */
4591 0, /* VK_LBUTTON 0x01 */
4592 0, /* VK_RBUTTON 0x02 */
4593 "cancel", /* VK_CANCEL 0x03 */
4594 0, /* VK_MBUTTON 0x04 */
4596 0, 0, 0, /* 0x05 .. 0x07 */
4598 "backspace", /* VK_BACK 0x08 */
4599 "tab", /* VK_TAB 0x09 */
4601 0, 0, /* 0x0A .. 0x0B */
4603 "clear", /* VK_CLEAR 0x0C */
4604 "return", /* VK_RETURN 0x0D */
4606 0, 0, /* 0x0E .. 0x0F */
4608 0, /* VK_SHIFT 0x10 */
4609 0, /* VK_CONTROL 0x11 */
4610 0, /* VK_MENU 0x12 */
4611 "pause", /* VK_PAUSE 0x13 */
4612 "capslock", /* VK_CAPITAL 0x14 */
4614 0, 0, 0, 0, 0, 0, /* 0x15 .. 0x1A */
4616 "escape", /* VK_ESCAPE 0x1B */
4618 0, 0, 0, 0, /* 0x1C .. 0x1F */
4620 0, /* VK_SPACE 0x20 */
4621 "prior", /* VK_PRIOR 0x21 */
4622 "next", /* VK_NEXT 0x22 */
4623 "end", /* VK_END 0x23 */
4624 "home", /* VK_HOME 0x24 */
4625 "left", /* VK_LEFT 0x25 */
4626 "up", /* VK_UP 0x26 */
4627 "right", /* VK_RIGHT 0x27 */
4628 "down", /* VK_DOWN 0x28 */
4629 "select", /* VK_SELECT 0x29 */
4630 "print", /* VK_PRINT 0x2A */
4631 "execute", /* VK_EXECUTE 0x2B */
4632 "snapshot", /* VK_SNAPSHOT 0x2C */
4633 "insert", /* VK_INSERT 0x2D */
4634 "delete", /* VK_DELETE 0x2E */
4635 "help", /* VK_HELP 0x2F */
4637 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
4639 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4641 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
4643 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
4645 0, 0, 0, 0, 0, 0, 0, 0, 0,
4646 0, 0, 0, 0, 0, 0, 0, 0, 0,
4647 0, 0, 0, 0, 0, 0, 0, 0,
4649 "lwindow", /* VK_LWIN 0x5B */
4650 "rwindow", /* VK_RWIN 0x5C */
4651 "apps", /* VK_APPS 0x5D */
4653 0, 0, /* 0x5E .. 0x5F */
4655 "kp-0", /* VK_NUMPAD0 0x60 */
4656 "kp-1", /* VK_NUMPAD1 0x61 */
4657 "kp-2", /* VK_NUMPAD2 0x62 */
4658 "kp-3", /* VK_NUMPAD3 0x63 */
4659 "kp-4", /* VK_NUMPAD4 0x64 */
4660 "kp-5", /* VK_NUMPAD5 0x65 */
4661 "kp-6", /* VK_NUMPAD6 0x66 */
4662 "kp-7", /* VK_NUMPAD7 0x67 */
4663 "kp-8", /* VK_NUMPAD8 0x68 */
4664 "kp-9", /* VK_NUMPAD9 0x69 */
4665 "kp-multiply", /* VK_MULTIPLY 0x6A */
4666 "kp-add", /* VK_ADD 0x6B */
4667 "kp-separator", /* VK_SEPARATOR 0x6C */
4668 "kp-subtract", /* VK_SUBTRACT 0x6D */
4669 "kp-decimal", /* VK_DECIMAL 0x6E */
4670 "kp-divide", /* VK_DIVIDE 0x6F */
4671 "f1", /* VK_F1 0x70 */
4672 "f2", /* VK_F2 0x71 */
4673 "f3", /* VK_F3 0x72 */
4674 "f4", /* VK_F4 0x73 */
4675 "f5", /* VK_F5 0x74 */
4676 "f6", /* VK_F6 0x75 */
4677 "f7", /* VK_F7 0x76 */
4678 "f8", /* VK_F8 0x77 */
4679 "f9", /* VK_F9 0x78 */
4680 "f10", /* VK_F10 0x79 */
4681 "f11", /* VK_F11 0x7A */
4682 "f12", /* VK_F12 0x7B */
4683 "f13", /* VK_F13 0x7C */
4684 "f14", /* VK_F14 0x7D */
4685 "f15", /* VK_F15 0x7E */
4686 "f16", /* VK_F16 0x7F */
4687 "f17", /* VK_F17 0x80 */
4688 "f18", /* VK_F18 0x81 */
4689 "f19", /* VK_F19 0x82 */
4690 "f20", /* VK_F20 0x83 */
4691 "f21", /* VK_F21 0x84 */
4692 "f22", /* VK_F22 0x85 */
4693 "f23", /* VK_F23 0x86 */
4694 "f24", /* VK_F24 0x87 */
4696 0, 0, 0, 0, /* 0x88 .. 0x8B */
4697 0, 0, 0, 0, /* 0x8C .. 0x8F */
4699 "kp-numlock", /* VK_NUMLOCK 0x90 */
4700 "scroll", /* VK_SCROLL 0x91 */
4702 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
4703 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
4704 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
4705 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
4706 "kp-end", /* VK_NUMPAD_END 0x96 */
4707 "kp-home", /* VK_NUMPAD_HOME 0x97 */
4708 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
4709 "kp-up", /* VK_NUMPAD_UP 0x99 */
4710 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
4711 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
4712 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
4713 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
4715 0, 0, /* 0x9E .. 0x9F */
4718 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
4719 * Used only as parameters to GetAsyncKeyState and GetKeyState.
4720 * No other API or message will distinguish left and right keys this way.
4722 /* 0xA0 .. 0xEF */
4724 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4725 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4726 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4727 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4728 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4730 /* 0xF0 .. 0xF5 */
4732 0, 0, 0, 0, 0, 0,
4734 "attn", /* VK_ATTN 0xF6 */
4735 "crsel", /* VK_CRSEL 0xF7 */
4736 "exsel", /* VK_EXSEL 0xF8 */
4737 "ereof", /* VK_EREOF 0xF9 */
4738 "play", /* VK_PLAY 0xFA */
4739 "zoom", /* VK_ZOOM 0xFB */
4740 "noname", /* VK_NONAME 0xFC */
4741 "pa1", /* VK_PA1 0xFD */
4742 "oem_clear", /* VK_OEM_CLEAR 0xFE */
4743 0 /* 0xFF */
4746 #else /* not HAVE_NTGUI */
4748 /* This should be dealt with in XTread_socket now, and that doesn't
4749 depend on the client system having the Kana syms defined. See also
4750 the XK_kana_A case below. */
4751 #if 0
4752 #ifdef XK_kana_A
4753 static char *lispy_kana_keys[] =
4755 /* X Keysym value */
4756 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x400 .. 0x40f */
4757 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x410 .. 0x41f */
4758 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x420 .. 0x42f */
4759 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x430 .. 0x43f */
4760 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x440 .. 0x44f */
4761 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x450 .. 0x45f */
4762 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x460 .. 0x46f */
4763 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0,
4764 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x480 .. 0x48f */
4765 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x490 .. 0x49f */
4766 0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket",
4767 "kana-comma", "kana-conjunctive", "kana-WO", "kana-a",
4768 "kana-i", "kana-u", "kana-e", "kana-o",
4769 "kana-ya", "kana-yu", "kana-yo", "kana-tsu",
4770 "prolongedsound", "kana-A", "kana-I", "kana-U",
4771 "kana-E", "kana-O", "kana-KA", "kana-KI",
4772 "kana-KU", "kana-KE", "kana-KO", "kana-SA",
4773 "kana-SHI", "kana-SU", "kana-SE", "kana-SO",
4774 "kana-TA", "kana-CHI", "kana-TSU", "kana-TE",
4775 "kana-TO", "kana-NA", "kana-NI", "kana-NU",
4776 "kana-NE", "kana-NO", "kana-HA", "kana-HI",
4777 "kana-FU", "kana-HE", "kana-HO", "kana-MA",
4778 "kana-MI", "kana-MU", "kana-ME", "kana-MO",
4779 "kana-YA", "kana-YU", "kana-YO", "kana-RA",
4780 "kana-RI", "kana-RU", "kana-RE", "kana-RO",
4781 "kana-WA", "kana-N", "voicedsound", "semivoicedsound",
4782 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4e0 .. 0x4ef */
4783 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f0 .. 0x4ff */
4785 #endif /* XK_kana_A */
4786 #endif /* 0 */
4788 #define FUNCTION_KEY_OFFSET 0xff00
4790 /* You'll notice that this table is arranged to be conveniently
4791 indexed by X Windows keysym values. */
4792 static char *lispy_function_keys[] =
4794 /* X Keysym value */
4796 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00...0f */
4797 "backspace", "tab", "linefeed", "clear",
4798 0, "return", 0, 0,
4799 0, 0, 0, "pause", /* 0xff10...1f */
4800 0, 0, 0, 0, 0, 0, 0, "escape",
4801 0, 0, 0, 0,
4802 0, "kanji", "muhenkan", "henkan", /* 0xff20...2f */
4803 "romaji", "hiragana", "katakana", "hiragana-katakana",
4804 "zenkaku", "hankaku", "zenkaku-hankaku", "touroku",
4805 "massyo", "kana-lock", "kana-shift", "eisu-shift",
4806 "eisu-toggle", /* 0xff30...3f */
4807 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4808 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
4810 "home", "left", "up", "right", /* 0xff50 */ /* IsCursorKey */
4811 "down", "prior", "next", "end",
4812 "begin", 0, 0, 0, 0, 0, 0, 0,
4813 "select", /* 0xff60 */ /* IsMiscFunctionKey */
4814 "print",
4815 "execute",
4816 "insert",
4817 0, /* 0xff64 */
4818 "undo",
4819 "redo",
4820 "menu",
4821 "find",
4822 "cancel",
4823 "help",
4824 "break", /* 0xff6b */
4826 0, 0, 0, 0,
4827 0, 0, 0, 0, "backtab", 0, 0, 0, /* 0xff70... */
4828 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff78... */
4829 "kp-space", /* 0xff80 */ /* IsKeypadKey */
4830 0, 0, 0, 0, 0, 0, 0, 0,
4831 "kp-tab", /* 0xff89 */
4832 0, 0, 0,
4833 "kp-enter", /* 0xff8d */
4834 0, 0, 0,
4835 "kp-f1", /* 0xff91 */
4836 "kp-f2",
4837 "kp-f3",
4838 "kp-f4",
4839 "kp-home", /* 0xff95 */
4840 "kp-left",
4841 "kp-up",
4842 "kp-right",
4843 "kp-down",
4844 "kp-prior", /* kp-page-up */
4845 "kp-next", /* kp-page-down */
4846 "kp-end",
4847 "kp-begin",
4848 "kp-insert",
4849 "kp-delete",
4850 0, /* 0xffa0 */
4851 0, 0, 0, 0, 0, 0, 0, 0, 0,
4852 "kp-multiply", /* 0xffaa */
4853 "kp-add",
4854 "kp-separator",
4855 "kp-subtract",
4856 "kp-decimal",
4857 "kp-divide", /* 0xffaf */
4858 "kp-0", /* 0xffb0 */
4859 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
4860 0, /* 0xffba */
4861 0, 0,
4862 "kp-equal", /* 0xffbd */
4863 "f1", /* 0xffbe */ /* IsFunctionKey */
4864 "f2",
4865 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
4866 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
4867 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
4868 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
4869 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
4870 0, 0, 0, 0, 0, 0, 0, 0,
4871 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
4872 0, 0, 0, 0, 0, 0, 0, "delete"
4875 /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE. */
4876 #define ISO_FUNCTION_KEY_OFFSET 0xfe00
4878 static char *iso_lispy_function_keys[] =
4880 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe00 */
4881 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe08 */
4882 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe10 */
4883 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe18 */
4884 "iso-lefttab", /* 0xfe20 */
4885 "iso-move-line-up", "iso-move-line-down",
4886 "iso-partial-line-up", "iso-partial-line-down",
4887 "iso-partial-space-left", "iso-partial-space-right",
4888 "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */
4889 "iso-release-margin-left", "iso-release-margin-right",
4890 "iso-release-both-margins",
4891 "iso-fast-cursor-left", "iso-fast-cursor-right",
4892 "iso-fast-cursor-up", "iso-fast-cursor-down",
4893 "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */
4894 "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */
4897 #endif /* not HAVE_NTGUI */
4899 Lisp_Object Vlispy_mouse_stem;
4901 static char *lispy_wheel_names[] =
4903 "wheel-up", "wheel-down"
4906 /* drag-n-drop events are generated when a set of selected files are
4907 dragged from another application and dropped onto an Emacs window. */
4908 static char *lispy_drag_n_drop_names[] =
4910 "drag-n-drop"
4913 /* Scroll bar parts. */
4914 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
4915 Lisp_Object Qup, Qdown, Qbottom, Qend_scroll;
4916 Lisp_Object Qtop, Qratio;
4918 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
4919 Lisp_Object *scroll_bar_parts[] = {
4920 &Qabove_handle, &Qhandle, &Qbelow_handle,
4921 &Qup, &Qdown, &Qtop, &Qbottom, &Qend_scroll, &Qratio
4924 /* User signal events. */
4925 Lisp_Object Qusr1_signal, Qusr2_signal;
4927 Lisp_Object *lispy_user_signals[] =
4929 &Qusr1_signal, &Qusr2_signal
4933 /* A vector, indexed by button number, giving the down-going location
4934 of currently depressed buttons, both scroll bar and non-scroll bar.
4936 The elements have the form
4937 (BUTTON-NUMBER MODIFIER-MASK . REST)
4938 where REST is the cdr of a position as it would be reported in the event.
4940 The make_lispy_event function stores positions here to tell the
4941 difference between click and drag events, and to store the starting
4942 location to be included in drag events. */
4944 static Lisp_Object button_down_location;
4946 /* Information about the most recent up-going button event: Which
4947 button, what location, and what time. */
4949 static int last_mouse_button;
4950 static int last_mouse_x;
4951 static int last_mouse_y;
4952 static unsigned long button_down_time;
4954 /* The maximum time between clicks to make a double-click, or Qnil to
4955 disable double-click detection, or Qt for no time limit. */
4957 Lisp_Object Vdouble_click_time;
4959 /* Maximum number of pixels the mouse may be moved between clicks
4960 to make a double-click. */
4962 EMACS_INT double_click_fuzz;
4964 /* The number of clicks in this multiple-click. */
4966 int double_click_count;
4968 /* Return position of a mouse click or wheel event */
4970 static Lisp_Object
4971 make_lispy_position (f, x, y, time)
4972 struct frame *f;
4973 Lisp_Object *x, *y;
4974 unsigned long time;
4976 Lisp_Object window;
4977 enum window_part part;
4978 Lisp_Object posn = Qnil;
4979 Lisp_Object extra_info = Qnil;
4980 int wx, wy;
4982 /* Set `window' to the window under frame pixel coordinates (x,y) */
4983 if (f)
4984 window = window_from_coordinates (f, XINT (*x), XINT (*y),
4985 &part, &wx, &wy, 0);
4986 else
4987 window = Qnil;
4989 if (WINDOWP (window))
4991 /* It's a click in window window at frame coordinates (x,y) */
4992 struct window *w = XWINDOW (window);
4993 Lisp_Object string_info = Qnil;
4994 int textpos = -1, rx = -1, ry = -1;
4995 int dx = -1, dy = -1;
4996 int width = -1, height = -1;
4997 Lisp_Object object = Qnil;
4999 /* Set event coordinates to window-relative coordinates
5000 for constructing the Lisp event below. */
5001 XSETINT (*x, wx);
5002 XSETINT (*y, wy);
5004 if (part == ON_MODE_LINE || part == ON_HEADER_LINE)
5006 /* Mode line or header line. Look for a string under
5007 the mouse that may have a `local-map' property. */
5008 Lisp_Object string;
5009 int charpos;
5011 posn = part == ON_MODE_LINE ? Qmode_line : Qheader_line;
5012 rx = wx, ry = wy;
5013 string = mode_line_string (w, part, &rx, &ry, &charpos,
5014 &object, &dx, &dy, &width, &height);
5015 if (STRINGP (string))
5016 string_info = Fcons (string, make_number (charpos));
5017 if (w == XWINDOW (selected_window))
5018 textpos = PT;
5019 else
5020 textpos = XMARKER (w->pointm)->charpos;
5022 else if (part == ON_VERTICAL_BORDER)
5024 posn = Qvertical_line;
5025 wx = -1;
5026 dx = 0;
5027 width = 1;
5029 else if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
5031 Lisp_Object string;
5032 int charpos;
5034 posn = (part == ON_LEFT_MARGIN) ? Qleft_margin : Qright_margin;
5035 rx = wx, ry = wy;
5036 string = marginal_area_string (w, part, &rx, &ry, &charpos,
5037 &object, &dx, &dy, &width, &height);
5038 if (STRINGP (string))
5039 string_info = Fcons (string, make_number (charpos));
5041 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
5043 posn = (part == ON_LEFT_FRINGE) ? Qleft_fringe : Qright_fringe;
5044 rx = 0;
5045 dx = wx;
5046 if (part == ON_RIGHT_FRINGE)
5047 dx -= (window_box_width (w, LEFT_MARGIN_AREA)
5048 + window_box_width (w, TEXT_AREA)
5049 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5050 ? window_box_width (w, RIGHT_MARGIN_AREA)
5051 : 0));
5052 else if (!WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
5053 dx -= window_box_width (w, LEFT_MARGIN_AREA);
5056 if (textpos < 0)
5058 Lisp_Object string2, object2 = Qnil;
5059 struct display_pos p;
5060 int dx2, dy2;
5061 int width2, height2;
5062 wx = max (WINDOW_LEFT_MARGIN_WIDTH (w), wx);
5063 string2 = buffer_posn_from_coords (w, &wx, &wy, &p,
5064 &object2, &dx2, &dy2,
5065 &width2, &height2);
5066 textpos = CHARPOS (p.pos);
5067 if (rx < 0) rx = wx;
5068 if (ry < 0) ry = wy;
5069 if (dx < 0) dx = dx2;
5070 if (dy < 0) dy = dy2;
5071 if (width < 0) width = width2;
5072 if (height < 0) height = height2;
5074 if (NILP (posn))
5076 posn = make_number (textpos);
5077 if (STRINGP (string2))
5078 string_info = Fcons (string2,
5079 make_number (CHARPOS (p.string_pos)));
5081 if (NILP (object))
5082 object = object2;
5085 #ifdef HAVE_WINDOW_SYSTEM
5086 if (IMAGEP (object))
5088 Lisp_Object image_map, hotspot;
5089 if ((image_map = Fplist_get (XCDR (object), QCmap),
5090 !NILP (image_map))
5091 && (hotspot = find_hot_spot (image_map, dx, dy),
5092 CONSP (hotspot))
5093 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
5094 posn = XCAR (hotspot);
5096 #endif
5098 /* Object info */
5099 extra_info = Fcons (object,
5100 Fcons (Fcons (make_number (dx),
5101 make_number (dy)),
5102 Fcons (Fcons (make_number (width),
5103 make_number (height)),
5104 Qnil)));
5106 /* String info */
5107 extra_info = Fcons (string_info,
5108 Fcons (make_number (textpos),
5109 Fcons (Fcons (make_number (rx),
5110 make_number (ry)),
5111 extra_info)));
5113 else if (f != 0)
5115 XSETFRAME (window, f);
5117 else
5119 window = Qnil;
5120 XSETFASTINT (*x, 0);
5121 XSETFASTINT (*y, 0);
5124 return Fcons (window,
5125 Fcons (posn,
5126 Fcons (Fcons (*x, *y),
5127 Fcons (make_number (time),
5128 extra_info))));
5131 /* Given a struct input_event, build the lisp event which represents
5132 it. If EVENT is 0, build a mouse movement event from the mouse
5133 movement buffer, which should have a movement event in it.
5135 Note that events must be passed to this function in the order they
5136 are received; this function stores the location of button presses
5137 in order to build drag events when the button is released. */
5139 static Lisp_Object
5140 make_lispy_event (event)
5141 struct input_event *event;
5143 int i;
5145 switch (SWITCH_ENUM_CAST (event->kind))
5147 /* A simple keystroke. */
5148 case ASCII_KEYSTROKE_EVENT:
5150 Lisp_Object lispy_c;
5151 int c = event->code & 0377;
5152 /* Turn ASCII characters into control characters
5153 when proper. */
5154 if (event->modifiers & ctrl_modifier)
5155 c = make_ctrl_char (c);
5157 /* Add in the other modifier bits. We took care of ctrl_modifier
5158 just above, and the shift key was taken care of by the X code,
5159 and applied to control characters by make_ctrl_char. */
5160 c |= (event->modifiers
5161 & (meta_modifier | alt_modifier
5162 | hyper_modifier | super_modifier));
5163 /* Distinguish Shift-SPC from SPC. */
5164 if ((event->code & 0377) == 040
5165 && event->modifiers & shift_modifier)
5166 c |= shift_modifier;
5167 button_down_time = 0;
5168 XSETFASTINT (lispy_c, c);
5169 return lispy_c;
5172 case MULTIBYTE_CHAR_KEYSTROKE_EVENT:
5174 Lisp_Object lispy_c;
5175 int c = event->code;
5177 /* Add in the other modifier bits. We took care of ctrl_modifier
5178 just above, and the shift key was taken care of by the X code,
5179 and applied to control characters by make_ctrl_char. */
5180 c |= (event->modifiers
5181 & (meta_modifier | alt_modifier
5182 | hyper_modifier | super_modifier | ctrl_modifier));
5183 /* What about the `shift' modifier ? */
5184 button_down_time = 0;
5185 XSETFASTINT (lispy_c, c);
5186 return lispy_c;
5189 /* A function key. The symbol may need to have modifier prefixes
5190 tacked onto it. */
5191 case NON_ASCII_KEYSTROKE_EVENT:
5192 button_down_time = 0;
5194 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
5195 if (event->code == lispy_accent_codes[i])
5196 return modify_event_symbol (i,
5197 event->modifiers,
5198 Qfunction_key, Qnil,
5199 lispy_accent_keys, &accent_key_syms,
5200 (sizeof (lispy_accent_keys)
5201 / sizeof (lispy_accent_keys[0])));
5203 #if 0
5204 #ifdef XK_kana_A
5205 if (event->code >= 0x400 && event->code < 0x500)
5206 return modify_event_symbol (event->code - 0x400,
5207 event->modifiers & ~shift_modifier,
5208 Qfunction_key, Qnil,
5209 lispy_kana_keys, &func_key_syms,
5210 (sizeof (lispy_kana_keys)
5211 / sizeof (lispy_kana_keys[0])));
5212 #endif /* XK_kana_A */
5213 #endif /* 0 */
5215 #ifdef ISO_FUNCTION_KEY_OFFSET
5216 if (event->code < FUNCTION_KEY_OFFSET
5217 && event->code >= ISO_FUNCTION_KEY_OFFSET)
5218 return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET,
5219 event->modifiers,
5220 Qfunction_key, Qnil,
5221 iso_lispy_function_keys, &func_key_syms,
5222 (sizeof (iso_lispy_function_keys)
5223 / sizeof (iso_lispy_function_keys[0])));
5224 #endif
5226 /* Handle system-specific or unknown keysyms. */
5227 if (event->code & (1 << 28)
5228 || event->code - FUNCTION_KEY_OFFSET < 0
5229 || (event->code - FUNCTION_KEY_OFFSET
5230 >= sizeof lispy_function_keys / sizeof *lispy_function_keys)
5231 || !lispy_function_keys[event->code - FUNCTION_KEY_OFFSET])
5233 /* We need to use an alist rather than a vector as the cache
5234 since we can't make a vector long enuf. */
5235 if (NILP (current_kboard->system_key_syms))
5236 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
5237 return modify_event_symbol (event->code,
5238 event->modifiers,
5239 Qfunction_key,
5240 current_kboard->Vsystem_key_alist,
5241 0, &current_kboard->system_key_syms,
5242 (unsigned) -1);
5245 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
5246 event->modifiers,
5247 Qfunction_key, Qnil,
5248 lispy_function_keys, &func_key_syms,
5249 (sizeof (lispy_function_keys)
5250 / sizeof (lispy_function_keys[0])));
5252 #ifdef HAVE_MOUSE
5253 /* A mouse click. Figure out where it is, decide whether it's
5254 a press, click or drag, and build the appropriate structure. */
5255 case MOUSE_CLICK_EVENT:
5256 #ifndef USE_TOOLKIT_SCROLL_BARS
5257 case SCROLL_BAR_CLICK_EVENT:
5258 #endif
5260 int button = event->code;
5261 int is_double;
5262 Lisp_Object position;
5263 Lisp_Object *start_pos_ptr;
5264 Lisp_Object start_pos;
5266 position = Qnil;
5268 /* Build the position as appropriate for this mouse click. */
5269 if (event->kind == MOUSE_CLICK_EVENT)
5271 struct frame *f = XFRAME (event->frame_or_window);
5272 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
5273 int row, column;
5274 #endif
5276 /* Ignore mouse events that were made on frame that
5277 have been deleted. */
5278 if (! FRAME_LIVE_P (f))
5279 return Qnil;
5281 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
5282 /* EVENT->x and EVENT->y are frame-relative pixel
5283 coordinates at this place. Under old redisplay, COLUMN
5284 and ROW are set to frame relative glyph coordinates
5285 which are then used to determine whether this click is
5286 in a menu (non-toolkit version). */
5287 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
5288 &column, &row, NULL, 1);
5290 /* In the non-toolkit version, clicks on the menu bar
5291 are ordinary button events in the event buffer.
5292 Distinguish them, and invoke the menu.
5294 (In the toolkit version, the toolkit handles the menu bar
5295 and Emacs doesn't know about it until after the user
5296 makes a selection.) */
5297 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
5298 && (event->modifiers & down_modifier))
5300 Lisp_Object items, item;
5301 int hpos;
5302 int i;
5304 #if 0
5305 /* Activate the menu bar on the down event. If the
5306 up event comes in before the menu code can deal with it,
5307 just ignore it. */
5308 if (! (event->modifiers & down_modifier))
5309 return Qnil;
5310 #endif
5312 /* Find the menu bar item under `column'. */
5313 item = Qnil;
5314 items = FRAME_MENU_BAR_ITEMS (f);
5315 for (i = 0; i < XVECTOR (items)->size; i += 4)
5317 Lisp_Object pos, string;
5318 string = AREF (items, i + 1);
5319 pos = AREF (items, i + 3);
5320 if (NILP (string))
5321 break;
5322 if (column >= XINT (pos)
5323 && column < XINT (pos) + SCHARS (string))
5325 item = AREF (items, i);
5326 break;
5330 /* ELisp manual 2.4b says (x y) are window relative but
5331 code says they are frame-relative. */
5332 position
5333 = Fcons (event->frame_or_window,
5334 Fcons (Qmenu_bar,
5335 Fcons (Fcons (event->x, event->y),
5336 Fcons (make_number (event->timestamp),
5337 Qnil))));
5339 return Fcons (item, Fcons (position, Qnil));
5341 #endif /* not USE_X_TOOLKIT && not USE_GTK */
5343 position = make_lispy_position (f, &event->x, &event->y,
5344 event->timestamp);
5346 #ifndef USE_TOOLKIT_SCROLL_BARS
5347 else
5349 /* It's a scrollbar click. */
5350 Lisp_Object window;
5351 Lisp_Object portion_whole;
5352 Lisp_Object part;
5354 window = event->frame_or_window;
5355 portion_whole = Fcons (event->x, event->y);
5356 part = *scroll_bar_parts[(int) event->part];
5358 position
5359 = Fcons (window,
5360 Fcons (Qvertical_scroll_bar,
5361 Fcons (portion_whole,
5362 Fcons (make_number (event->timestamp),
5363 Fcons (part, Qnil)))));
5365 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5367 if (button >= ASIZE (button_down_location))
5369 button_down_location = larger_vector (button_down_location,
5370 button + 1, Qnil);
5371 mouse_syms = larger_vector (mouse_syms, button + 1, Qnil);
5374 start_pos_ptr = &AREF (button_down_location, button);
5375 start_pos = *start_pos_ptr;
5376 *start_pos_ptr = Qnil;
5379 /* On window-system frames, use the value of
5380 double-click-fuzz as is. On other frames, interpret it
5381 as a multiple of 1/8 characters. */
5382 struct frame *f;
5383 int fuzz;
5385 if (WINDOWP (event->frame_or_window))
5386 f = XFRAME (XWINDOW (event->frame_or_window)->frame);
5387 else if (FRAMEP (event->frame_or_window))
5388 f = XFRAME (event->frame_or_window);
5389 else
5390 abort ();
5392 if (FRAME_WINDOW_P (f))
5393 fuzz = double_click_fuzz;
5394 else
5395 fuzz = double_click_fuzz / 8;
5397 is_double = (button == last_mouse_button
5398 && (abs (XINT (event->x) - last_mouse_x) <= fuzz)
5399 && (abs (XINT (event->y) - last_mouse_y) <= fuzz)
5400 && button_down_time != 0
5401 && (EQ (Vdouble_click_time, Qt)
5402 || (INTEGERP (Vdouble_click_time)
5403 && ((int)(event->timestamp - button_down_time)
5404 < XINT (Vdouble_click_time)))));
5407 last_mouse_button = button;
5408 last_mouse_x = XINT (event->x);
5409 last_mouse_y = XINT (event->y);
5411 /* If this is a button press, squirrel away the location, so
5412 we can decide later whether it was a click or a drag. */
5413 if (event->modifiers & down_modifier)
5415 if (is_double)
5417 double_click_count++;
5418 event->modifiers |= ((double_click_count > 2)
5419 ? triple_modifier
5420 : double_modifier);
5422 else
5423 double_click_count = 1;
5424 button_down_time = event->timestamp;
5425 *start_pos_ptr = Fcopy_alist (position);
5428 /* Now we're releasing a button - check the co-ordinates to
5429 see if this was a click or a drag. */
5430 else if (event->modifiers & up_modifier)
5432 /* If we did not see a down before this up, ignore the up.
5433 Probably this happened because the down event chose a
5434 menu item. It would be an annoyance to treat the
5435 release of the button that chose the menu item as a
5436 separate event. */
5438 if (!CONSP (start_pos))
5439 return Qnil;
5441 event->modifiers &= ~up_modifier;
5442 #if 0 /* Formerly we treated an up with no down as a click event. */
5443 if (!CONSP (start_pos))
5444 event->modifiers |= click_modifier;
5445 else
5446 #endif
5448 Lisp_Object down;
5449 EMACS_INT xdiff = double_click_fuzz, ydiff = double_click_fuzz;
5451 /* The third element of every position
5452 should be the (x,y) pair. */
5453 down = Fcar (Fcdr (Fcdr (start_pos)));
5454 if (CONSP (down)
5455 && INTEGERP (XCAR (down)) && INTEGERP (XCDR (down)))
5457 xdiff = XFASTINT (event->x) - XFASTINT (XCAR (down));
5458 ydiff = XFASTINT (event->y) - XFASTINT (XCDR (down));
5461 if (xdiff < double_click_fuzz && xdiff > - double_click_fuzz
5462 && ydiff < double_click_fuzz
5463 && ydiff > - double_click_fuzz)
5464 /* Mouse hasn't moved (much). */
5465 event->modifiers |= click_modifier;
5466 else
5468 button_down_time = 0;
5469 event->modifiers |= drag_modifier;
5472 /* Don't check is_double; treat this as multiple
5473 if the down-event was multiple. */
5474 if (double_click_count > 1)
5475 event->modifiers |= ((double_click_count > 2)
5476 ? triple_modifier
5477 : double_modifier);
5480 else
5481 /* Every mouse event should either have the down_modifier or
5482 the up_modifier set. */
5483 abort ();
5486 /* Get the symbol we should use for the mouse click. */
5487 Lisp_Object head;
5489 head = modify_event_symbol (button,
5490 event->modifiers,
5491 Qmouse_click, Vlispy_mouse_stem,
5492 NULL,
5493 &mouse_syms,
5494 XVECTOR (mouse_syms)->size);
5495 if (event->modifiers & drag_modifier)
5496 return Fcons (head,
5497 Fcons (start_pos,
5498 Fcons (position,
5499 Qnil)));
5500 else if (event->modifiers & (double_modifier | triple_modifier))
5501 return Fcons (head,
5502 Fcons (position,
5503 Fcons (make_number (double_click_count),
5504 Qnil)));
5505 else
5506 return Fcons (head,
5507 Fcons (position,
5508 Qnil));
5512 case WHEEL_EVENT:
5514 Lisp_Object position;
5515 Lisp_Object head;
5517 /* Build the position as appropriate for this mouse click. */
5518 struct frame *f = XFRAME (event->frame_or_window);
5520 /* Ignore wheel events that were made on frame that have been
5521 deleted. */
5522 if (! FRAME_LIVE_P (f))
5523 return Qnil;
5525 position = make_lispy_position (f, &event->x, &event->y,
5526 event->timestamp);
5528 /* Set double or triple modifiers to indicate the wheel speed. */
5530 /* On window-system frames, use the value of
5531 double-click-fuzz as is. On other frames, interpret it
5532 as a multiple of 1/8 characters. */
5533 struct frame *f;
5534 int fuzz;
5535 int is_double;
5537 if (WINDOWP (event->frame_or_window))
5538 f = XFRAME (XWINDOW (event->frame_or_window)->frame);
5539 else if (FRAMEP (event->frame_or_window))
5540 f = XFRAME (event->frame_or_window);
5541 else
5542 abort ();
5544 if (FRAME_WINDOW_P (f))
5545 fuzz = double_click_fuzz;
5546 else
5547 fuzz = double_click_fuzz / 8;
5549 is_double = (last_mouse_button < 0
5550 && (abs (XINT (event->x) - last_mouse_x) <= fuzz)
5551 && (abs (XINT (event->y) - last_mouse_y) <= fuzz)
5552 && button_down_time != 0
5553 && (EQ (Vdouble_click_time, Qt)
5554 || (INTEGERP (Vdouble_click_time)
5555 && ((int)(event->timestamp - button_down_time)
5556 < XINT (Vdouble_click_time)))));
5557 if (is_double)
5559 double_click_count++;
5560 event->modifiers |= ((double_click_count > 2)
5561 ? triple_modifier
5562 : double_modifier);
5564 else
5566 double_click_count = 1;
5567 event->modifiers |= click_modifier;
5570 button_down_time = event->timestamp;
5571 /* Use a negative value to distinguish wheel from mouse button. */
5572 last_mouse_button = -1;
5573 last_mouse_x = XINT (event->x);
5574 last_mouse_y = XINT (event->y);
5578 int symbol_num;
5580 if (event->modifiers & up_modifier)
5582 /* Emit a wheel-up event. */
5583 event->modifiers &= ~up_modifier;
5584 symbol_num = 0;
5586 else if (event->modifiers & down_modifier)
5588 /* Emit a wheel-down event. */
5589 event->modifiers &= ~down_modifier;
5590 symbol_num = 1;
5592 else
5593 /* Every wheel event should either have the down_modifier or
5594 the up_modifier set. */
5595 abort ();
5597 /* Get the symbol we should use for the wheel event. */
5598 head = modify_event_symbol (symbol_num,
5599 event->modifiers,
5600 Qmouse_click,
5601 Qnil,
5602 lispy_wheel_names,
5603 &wheel_syms,
5604 ASIZE (wheel_syms));
5607 if (event->modifiers & (double_modifier | triple_modifier))
5608 return Fcons (head,
5609 Fcons (position,
5610 Fcons (make_number (double_click_count),
5611 Qnil)));
5612 else
5613 return Fcons (head,
5614 Fcons (position,
5615 Qnil));
5619 #ifdef USE_TOOLKIT_SCROLL_BARS
5621 /* We don't have down and up events if using toolkit scroll bars,
5622 so make this always a click event. Store in the `part' of
5623 the Lisp event a symbol which maps to the following actions:
5625 `above_handle' page up
5626 `below_handle' page down
5627 `up' line up
5628 `down' line down
5629 `top' top of buffer
5630 `bottom' bottom of buffer
5631 `handle' thumb has been dragged.
5632 `end-scroll' end of interaction with scroll bar
5634 The incoming input_event contains in its `part' member an
5635 index of type `enum scroll_bar_part' which we can use as an
5636 index in scroll_bar_parts to get the appropriate symbol. */
5638 case SCROLL_BAR_CLICK_EVENT:
5640 Lisp_Object position, head, window, portion_whole, part;
5642 window = event->frame_or_window;
5643 portion_whole = Fcons (event->x, event->y);
5644 part = *scroll_bar_parts[(int) event->part];
5646 position
5647 = Fcons (window,
5648 Fcons (Qvertical_scroll_bar,
5649 Fcons (portion_whole,
5650 Fcons (make_number (event->timestamp),
5651 Fcons (part, Qnil)))));
5653 /* Always treat scroll bar events as clicks. */
5654 event->modifiers |= click_modifier;
5655 event->modifiers &= ~up_modifier;
5657 if (event->code >= ASIZE (mouse_syms))
5658 mouse_syms = larger_vector (mouse_syms, event->code + 1, Qnil);
5660 /* Get the symbol we should use for the mouse click. */
5661 head = modify_event_symbol (event->code,
5662 event->modifiers,
5663 Qmouse_click,
5664 Vlispy_mouse_stem,
5665 NULL, &mouse_syms,
5666 XVECTOR (mouse_syms)->size);
5667 return Fcons (head, Fcons (position, Qnil));
5670 #endif /* USE_TOOLKIT_SCROLL_BARS */
5672 #ifdef WINDOWSNT
5673 case W32_SCROLL_BAR_CLICK_EVENT:
5675 int button = event->code;
5676 int is_double;
5677 Lisp_Object position;
5678 Lisp_Object *start_pos_ptr;
5679 Lisp_Object start_pos;
5682 Lisp_Object window;
5683 Lisp_Object portion_whole;
5684 Lisp_Object part;
5686 window = event->frame_or_window;
5687 portion_whole = Fcons (event->x, event->y);
5688 part = *scroll_bar_parts[(int) event->part];
5690 position
5691 = Fcons (window,
5692 Fcons (Qvertical_scroll_bar,
5693 Fcons (portion_whole,
5694 Fcons (make_number (event->timestamp),
5695 Fcons (part, Qnil)))));
5698 /* Always treat W32 scroll bar events as clicks. */
5699 event->modifiers |= click_modifier;
5702 /* Get the symbol we should use for the mouse click. */
5703 Lisp_Object head;
5705 head = modify_event_symbol (button,
5706 event->modifiers,
5707 Qmouse_click,
5708 Vlispy_mouse_stem,
5709 NULL, &mouse_syms,
5710 XVECTOR (mouse_syms)->size);
5711 return Fcons (head,
5712 Fcons (position,
5713 Qnil));
5716 #endif /* WINDOWSNT */
5718 case DRAG_N_DROP_EVENT:
5720 FRAME_PTR f;
5721 Lisp_Object head, position;
5722 Lisp_Object files;
5724 /* The frame_or_window field should be a cons of the frame in
5725 which the event occurred and a list of the filenames
5726 dropped. */
5727 if (! CONSP (event->frame_or_window))
5728 abort ();
5730 f = XFRAME (XCAR (event->frame_or_window));
5731 files = XCDR (event->frame_or_window);
5733 /* Ignore mouse events that were made on frames that
5734 have been deleted. */
5735 if (! FRAME_LIVE_P (f))
5736 return Qnil;
5738 position = make_lispy_position (f, &event->x, &event->y,
5739 event->timestamp);
5741 head = modify_event_symbol (0, event->modifiers,
5742 Qdrag_n_drop, Qnil,
5743 lispy_drag_n_drop_names,
5744 &drag_n_drop_syms, 1);
5745 return Fcons (head,
5746 Fcons (position,
5747 Fcons (files,
5748 Qnil)));
5750 #endif /* HAVE_MOUSE */
5752 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
5753 || defined (USE_GTK)
5754 case MENU_BAR_EVENT:
5755 if (EQ (event->arg, event->frame_or_window))
5756 /* This is the prefix key. We translate this to
5757 `(menu_bar)' because the code in keyboard.c for menu
5758 events, which we use, relies on this. */
5759 return Fcons (Qmenu_bar, Qnil);
5760 return event->arg;
5761 #endif
5763 case SELECT_WINDOW_EVENT:
5764 /* Make an event (select-window (WINDOW)). */
5765 return Fcons (Qselect_window,
5766 Fcons (Fcons (event->frame_or_window, Qnil),
5767 Qnil));
5769 case TOOL_BAR_EVENT:
5770 if (EQ (event->arg, event->frame_or_window))
5771 /* This is the prefix key. We translate this to
5772 `(tool_bar)' because the code in keyboard.c for tool bar
5773 events, which we use, relies on this. */
5774 return Fcons (Qtool_bar, Qnil);
5775 else if (SYMBOLP (event->arg))
5776 return apply_modifiers (event->modifiers, event->arg);
5777 return event->arg;
5779 case USER_SIGNAL_EVENT:
5780 /* A user signal. */
5781 return *lispy_user_signals[event->code];
5783 case SAVE_SESSION_EVENT:
5784 return Qsave_session;
5786 /* The 'kind' field of the event is something we don't recognize. */
5787 default:
5788 abort ();
5792 #ifdef HAVE_MOUSE
5794 static Lisp_Object
5795 make_lispy_movement (frame, bar_window, part, x, y, time)
5796 FRAME_PTR frame;
5797 Lisp_Object bar_window;
5798 enum scroll_bar_part part;
5799 Lisp_Object x, y;
5800 unsigned long time;
5802 /* Is it a scroll bar movement? */
5803 if (frame && ! NILP (bar_window))
5805 Lisp_Object part_sym;
5807 part_sym = *scroll_bar_parts[(int) part];
5808 return Fcons (Qscroll_bar_movement,
5809 (Fcons (Fcons (bar_window,
5810 Fcons (Qvertical_scroll_bar,
5811 Fcons (Fcons (x, y),
5812 Fcons (make_number (time),
5813 Fcons (part_sym,
5814 Qnil))))),
5815 Qnil)));
5818 /* Or is it an ordinary mouse movement? */
5819 else
5821 Lisp_Object position;
5823 position = make_lispy_position (frame, &x, &y, time);
5825 return Fcons (Qmouse_movement,
5826 Fcons (position,
5827 Qnil));
5831 #endif /* HAVE_MOUSE */
5833 /* Construct a switch frame event. */
5834 static Lisp_Object
5835 make_lispy_switch_frame (frame)
5836 Lisp_Object frame;
5838 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
5841 /* Manipulating modifiers. */
5843 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
5845 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
5846 SYMBOL's name of the end of the modifiers; the string from this
5847 position is the unmodified symbol name.
5849 This doesn't use any caches. */
5851 static int
5852 parse_modifiers_uncached (symbol, modifier_end)
5853 Lisp_Object symbol;
5854 int *modifier_end;
5856 Lisp_Object name;
5857 int i;
5858 int modifiers;
5860 CHECK_SYMBOL (symbol);
5862 modifiers = 0;
5863 name = SYMBOL_NAME (symbol);
5865 for (i = 0; i+2 <= SBYTES (name); )
5867 int this_mod_end = 0;
5868 int this_mod = 0;
5870 /* See if the name continues with a modifier word.
5871 Check that the word appears, but don't check what follows it.
5872 Set this_mod and this_mod_end to record what we find. */
5874 switch (SREF (name, i))
5876 #define SINGLE_LETTER_MOD(BIT) \
5877 (this_mod_end = i + 1, this_mod = BIT)
5879 case 'A':
5880 SINGLE_LETTER_MOD (alt_modifier);
5881 break;
5883 case 'C':
5884 SINGLE_LETTER_MOD (ctrl_modifier);
5885 break;
5887 case 'H':
5888 SINGLE_LETTER_MOD (hyper_modifier);
5889 break;
5891 case 'M':
5892 SINGLE_LETTER_MOD (meta_modifier);
5893 break;
5895 case 'S':
5896 SINGLE_LETTER_MOD (shift_modifier);
5897 break;
5899 case 's':
5900 SINGLE_LETTER_MOD (super_modifier);
5901 break;
5903 #undef SINGLE_LETTER_MOD
5905 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
5906 if (i + LEN + 1 <= SBYTES (name) \
5907 && ! strncmp (SDATA (name) + i, NAME, LEN)) \
5909 this_mod_end = i + LEN; \
5910 this_mod = BIT; \
5913 case 'd':
5914 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
5915 MULTI_LETTER_MOD (down_modifier, "down", 4);
5916 MULTI_LETTER_MOD (double_modifier, "double", 6);
5917 break;
5919 case 't':
5920 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
5921 break;
5922 #undef MULTI_LETTER_MOD
5926 /* If we found no modifier, stop looking for them. */
5927 if (this_mod_end == 0)
5928 break;
5930 /* Check there is a dash after the modifier, so that it
5931 really is a modifier. */
5932 if (this_mod_end >= SBYTES (name)
5933 || SREF (name, this_mod_end) != '-')
5934 break;
5936 /* This modifier is real; look for another. */
5937 modifiers |= this_mod;
5938 i = this_mod_end + 1;
5941 /* Should we include the `click' modifier? */
5942 if (! (modifiers & (down_modifier | drag_modifier
5943 | double_modifier | triple_modifier))
5944 && i + 7 == SBYTES (name)
5945 && strncmp (SDATA (name) + i, "mouse-", 6) == 0
5946 && ('0' <= SREF (name, i + 6) && SREF (name, i + 6) <= '9'))
5947 modifiers |= click_modifier;
5949 if (modifier_end)
5950 *modifier_end = i;
5952 return modifiers;
5955 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
5956 prepended to the string BASE[0..BASE_LEN-1].
5957 This doesn't use any caches. */
5958 static Lisp_Object
5959 apply_modifiers_uncached (modifiers, base, base_len, base_len_byte)
5960 int modifiers;
5961 char *base;
5962 int base_len, base_len_byte;
5964 /* Since BASE could contain nulls, we can't use intern here; we have
5965 to use Fintern, which expects a genuine Lisp_String, and keeps a
5966 reference to it. */
5967 char *new_mods
5968 = (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
5969 int mod_len;
5972 char *p = new_mods;
5974 /* Only the event queue may use the `up' modifier; it should always
5975 be turned into a click or drag event before presented to lisp code. */
5976 if (modifiers & up_modifier)
5977 abort ();
5979 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
5980 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
5981 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
5982 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
5983 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
5984 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
5985 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
5986 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
5987 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
5988 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
5989 /* The click modifier is denoted by the absence of other modifiers. */
5991 *p = '\0';
5993 mod_len = p - new_mods;
5997 Lisp_Object new_name;
5999 new_name = make_uninit_multibyte_string (mod_len + base_len,
6000 mod_len + base_len_byte);
6001 bcopy (new_mods, SDATA (new_name), mod_len);
6002 bcopy (base, SDATA (new_name) + mod_len, base_len_byte);
6004 return Fintern (new_name, Qnil);
6009 static char *modifier_names[] =
6011 "up", "down", "drag", "click", "double", "triple", 0, 0,
6012 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6013 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
6015 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
6017 static Lisp_Object modifier_symbols;
6019 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
6020 static Lisp_Object
6021 lispy_modifier_list (modifiers)
6022 int modifiers;
6024 Lisp_Object modifier_list;
6025 int i;
6027 modifier_list = Qnil;
6028 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
6029 if (modifiers & (1<<i))
6030 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
6031 modifier_list);
6033 return modifier_list;
6037 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
6038 where UNMODIFIED is the unmodified form of SYMBOL,
6039 MASK is the set of modifiers present in SYMBOL's name.
6040 This is similar to parse_modifiers_uncached, but uses the cache in
6041 SYMBOL's Qevent_symbol_element_mask property, and maintains the
6042 Qevent_symbol_elements property. */
6044 Lisp_Object
6045 parse_modifiers (symbol)
6046 Lisp_Object symbol;
6048 Lisp_Object elements;
6050 elements = Fget (symbol, Qevent_symbol_element_mask);
6051 if (CONSP (elements))
6052 return elements;
6053 else
6055 int end;
6056 int modifiers = parse_modifiers_uncached (symbol, &end);
6057 Lisp_Object unmodified;
6058 Lisp_Object mask;
6060 unmodified = Fintern (make_string (SDATA (SYMBOL_NAME (symbol)) + end,
6061 SBYTES (SYMBOL_NAME (symbol)) - end),
6062 Qnil);
6064 if (modifiers & ~INTMASK)
6065 abort ();
6066 XSETFASTINT (mask, modifiers);
6067 elements = Fcons (unmodified, Fcons (mask, Qnil));
6069 /* Cache the parsing results on SYMBOL. */
6070 Fput (symbol, Qevent_symbol_element_mask,
6071 elements);
6072 Fput (symbol, Qevent_symbol_elements,
6073 Fcons (unmodified, lispy_modifier_list (modifiers)));
6075 /* Since we know that SYMBOL is modifiers applied to unmodified,
6076 it would be nice to put that in unmodified's cache.
6077 But we can't, since we're not sure that parse_modifiers is
6078 canonical. */
6080 return elements;
6084 /* Apply the modifiers MODIFIERS to the symbol BASE.
6085 BASE must be unmodified.
6087 This is like apply_modifiers_uncached, but uses BASE's
6088 Qmodifier_cache property, if present. It also builds
6089 Qevent_symbol_elements properties, since it has that info anyway.
6091 apply_modifiers copies the value of BASE's Qevent_kind property to
6092 the modified symbol. */
6093 static Lisp_Object
6094 apply_modifiers (modifiers, base)
6095 int modifiers;
6096 Lisp_Object base;
6098 Lisp_Object cache, index, entry, new_symbol;
6100 /* Mask out upper bits. We don't know where this value's been. */
6101 modifiers &= INTMASK;
6103 /* The click modifier never figures into cache indices. */
6104 cache = Fget (base, Qmodifier_cache);
6105 XSETFASTINT (index, (modifiers & ~click_modifier));
6106 entry = assq_no_quit (index, cache);
6108 if (CONSP (entry))
6109 new_symbol = XCDR (entry);
6110 else
6112 /* We have to create the symbol ourselves. */
6113 new_symbol = apply_modifiers_uncached (modifiers,
6114 SDATA (SYMBOL_NAME (base)),
6115 SCHARS (SYMBOL_NAME (base)),
6116 SBYTES (SYMBOL_NAME (base)));
6118 /* Add the new symbol to the base's cache. */
6119 entry = Fcons (index, new_symbol);
6120 Fput (base, Qmodifier_cache, Fcons (entry, cache));
6122 /* We have the parsing info now for free, so we could add it to
6123 the caches:
6124 XSETFASTINT (index, modifiers);
6125 Fput (new_symbol, Qevent_symbol_element_mask,
6126 Fcons (base, Fcons (index, Qnil)));
6127 Fput (new_symbol, Qevent_symbol_elements,
6128 Fcons (base, lispy_modifier_list (modifiers)));
6129 Sadly, this is only correct if `base' is indeed a base event,
6130 which is not necessarily the case. -stef */
6133 /* Make sure this symbol is of the same kind as BASE.
6135 You'd think we could just set this once and for all when we
6136 intern the symbol above, but reorder_modifiers may call us when
6137 BASE's property isn't set right; we can't assume that just
6138 because it has a Qmodifier_cache property it must have its
6139 Qevent_kind set right as well. */
6140 if (NILP (Fget (new_symbol, Qevent_kind)))
6142 Lisp_Object kind;
6144 kind = Fget (base, Qevent_kind);
6145 if (! NILP (kind))
6146 Fput (new_symbol, Qevent_kind, kind);
6149 return new_symbol;
6153 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
6154 return a symbol with the modifiers placed in the canonical order.
6155 Canonical order is alphabetical, except for down and drag, which
6156 always come last. The 'click' modifier is never written out.
6158 Fdefine_key calls this to make sure that (for example) C-M-foo
6159 and M-C-foo end up being equivalent in the keymap. */
6161 Lisp_Object
6162 reorder_modifiers (symbol)
6163 Lisp_Object symbol;
6165 /* It's hopefully okay to write the code this way, since everything
6166 will soon be in caches, and no consing will be done at all. */
6167 Lisp_Object parsed;
6169 parsed = parse_modifiers (symbol);
6170 return apply_modifiers ((int) XINT (XCAR (XCDR (parsed))),
6171 XCAR (parsed));
6175 /* For handling events, we often want to produce a symbol whose name
6176 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
6177 to some base, like the name of a function key or mouse button.
6178 modify_event_symbol produces symbols of this sort.
6180 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
6181 is the name of the i'th symbol. TABLE_SIZE is the number of elements
6182 in the table.
6184 Alternatively, NAME_ALIST_OR_STEM is either an alist mapping codes
6185 into symbol names, or a string specifying a name stem used to
6186 construct a symbol name or the form `STEM-N', where N is the decimal
6187 representation of SYMBOL_NUM. NAME_ALIST_OR_STEM is used if it is
6188 non-nil; otherwise NAME_TABLE is used.
6190 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
6191 persist between calls to modify_event_symbol that it can use to
6192 store a cache of the symbols it's generated for this NAME_TABLE
6193 before. The object stored there may be a vector or an alist.
6195 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
6197 MODIFIERS is a set of modifier bits (as given in struct input_events)
6198 whose prefixes should be applied to the symbol name.
6200 SYMBOL_KIND is the value to be placed in the event_kind property of
6201 the returned symbol.
6203 The symbols we create are supposed to have an
6204 `event-symbol-elements' property, which lists the modifiers present
6205 in the symbol's name. */
6207 static Lisp_Object
6208 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist_or_stem,
6209 name_table, symbol_table, table_size)
6210 int symbol_num;
6211 unsigned modifiers;
6212 Lisp_Object symbol_kind;
6213 Lisp_Object name_alist_or_stem;
6214 char **name_table;
6215 Lisp_Object *symbol_table;
6216 unsigned int table_size;
6218 Lisp_Object value;
6219 Lisp_Object symbol_int;
6221 /* Get rid of the "vendor-specific" bit here. */
6222 XSETINT (symbol_int, symbol_num & 0xffffff);
6224 /* Is this a request for a valid symbol? */
6225 if (symbol_num < 0 || symbol_num >= table_size)
6226 return Qnil;
6228 if (CONSP (*symbol_table))
6229 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
6231 /* If *symbol_table doesn't seem to be initialized properly, fix that.
6232 *symbol_table should be a lisp vector TABLE_SIZE elements long,
6233 where the Nth element is the symbol for NAME_TABLE[N], or nil if
6234 we've never used that symbol before. */
6235 else
6237 if (! VECTORP (*symbol_table)
6238 || XVECTOR (*symbol_table)->size != table_size)
6240 Lisp_Object size;
6242 XSETFASTINT (size, table_size);
6243 *symbol_table = Fmake_vector (size, Qnil);
6246 value = XVECTOR (*symbol_table)->contents[symbol_num];
6249 /* Have we already used this symbol before? */
6250 if (NILP (value))
6252 /* No; let's create it. */
6253 if (CONSP (name_alist_or_stem))
6254 value = Fcdr_safe (Fassq (symbol_int, name_alist_or_stem));
6255 else if (STRINGP (name_alist_or_stem))
6257 int len = SBYTES (name_alist_or_stem);
6258 char *buf = (char *) alloca (len + 50);
6259 if (sizeof (int) == sizeof (EMACS_INT))
6260 sprintf (buf, "%s-%d", SDATA (name_alist_or_stem),
6261 XINT (symbol_int) + 1);
6262 else if (sizeof (long) == sizeof (EMACS_INT))
6263 sprintf (buf, "%s-%ld", SDATA (name_alist_or_stem),
6264 XINT (symbol_int) + 1);
6265 value = intern (buf);
6267 else if (name_table != 0 && name_table[symbol_num])
6268 value = intern (name_table[symbol_num]);
6270 #ifdef HAVE_WINDOW_SYSTEM
6271 if (NILP (value))
6273 char *name = x_get_keysym_name (symbol_num);
6274 if (name)
6275 value = intern (name);
6277 #endif
6279 if (NILP (value))
6281 char buf[20];
6282 sprintf (buf, "key-%d", symbol_num);
6283 value = intern (buf);
6286 if (CONSP (*symbol_table))
6287 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
6288 else
6289 XVECTOR (*symbol_table)->contents[symbol_num] = value;
6291 /* Fill in the cache entries for this symbol; this also
6292 builds the Qevent_symbol_elements property, which the user
6293 cares about. */
6294 apply_modifiers (modifiers & click_modifier, value);
6295 Fput (value, Qevent_kind, symbol_kind);
6298 /* Apply modifiers to that symbol. */
6299 return apply_modifiers (modifiers, value);
6302 /* Convert a list that represents an event type,
6303 such as (ctrl meta backspace), into the usual representation of that
6304 event type as a number or a symbol. */
6306 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
6307 doc: /* Convert the event description list EVENT-DESC to an event type.
6308 EVENT-DESC should contain one base event type (a character or symbol)
6309 and zero or more modifier names (control, meta, hyper, super, shift, alt,
6310 drag, down, double or triple). The base must be last.
6311 The return value is an event type (a character or symbol) which
6312 has the same base event type and all the specified modifiers. */)
6313 (event_desc)
6314 Lisp_Object event_desc;
6316 Lisp_Object base;
6317 int modifiers = 0;
6318 Lisp_Object rest;
6320 base = Qnil;
6321 rest = event_desc;
6322 while (CONSP (rest))
6324 Lisp_Object elt;
6325 int this = 0;
6327 elt = XCAR (rest);
6328 rest = XCDR (rest);
6330 /* Given a symbol, see if it is a modifier name. */
6331 if (SYMBOLP (elt) && CONSP (rest))
6332 this = parse_solitary_modifier (elt);
6334 if (this != 0)
6335 modifiers |= this;
6336 else if (!NILP (base))
6337 error ("Two bases given in one event");
6338 else
6339 base = elt;
6343 /* Let the symbol A refer to the character A. */
6344 if (SYMBOLP (base) && SCHARS (SYMBOL_NAME (base)) == 1)
6345 XSETINT (base, SREF (SYMBOL_NAME (base), 0));
6347 if (INTEGERP (base))
6349 /* Turn (shift a) into A. */
6350 if ((modifiers & shift_modifier) != 0
6351 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
6353 XSETINT (base, XINT (base) - ('a' - 'A'));
6354 modifiers &= ~shift_modifier;
6357 /* Turn (control a) into C-a. */
6358 if (modifiers & ctrl_modifier)
6359 return make_number ((modifiers & ~ctrl_modifier)
6360 | make_ctrl_char (XINT (base)));
6361 else
6362 return make_number (modifiers | XINT (base));
6364 else if (SYMBOLP (base))
6365 return apply_modifiers (modifiers, base);
6366 else
6368 error ("Invalid base event");
6369 return Qnil;
6373 /* Try to recognize SYMBOL as a modifier name.
6374 Return the modifier flag bit, or 0 if not recognized. */
6376 static int
6377 parse_solitary_modifier (symbol)
6378 Lisp_Object symbol;
6380 Lisp_Object name = SYMBOL_NAME (symbol);
6382 switch (SREF (name, 0))
6384 #define SINGLE_LETTER_MOD(BIT) \
6385 if (SBYTES (name) == 1) \
6386 return BIT;
6388 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
6389 if (LEN == SBYTES (name) \
6390 && ! strncmp (SDATA (name), NAME, LEN)) \
6391 return BIT;
6393 case 'A':
6394 SINGLE_LETTER_MOD (alt_modifier);
6395 break;
6397 case 'a':
6398 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
6399 break;
6401 case 'C':
6402 SINGLE_LETTER_MOD (ctrl_modifier);
6403 break;
6405 case 'c':
6406 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
6407 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
6408 break;
6410 case 'H':
6411 SINGLE_LETTER_MOD (hyper_modifier);
6412 break;
6414 case 'h':
6415 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
6416 break;
6418 case 'M':
6419 SINGLE_LETTER_MOD (meta_modifier);
6420 break;
6422 case 'm':
6423 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
6424 break;
6426 case 'S':
6427 SINGLE_LETTER_MOD (shift_modifier);
6428 break;
6430 case 's':
6431 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
6432 MULTI_LETTER_MOD (super_modifier, "super", 5);
6433 SINGLE_LETTER_MOD (super_modifier);
6434 break;
6436 case 'd':
6437 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
6438 MULTI_LETTER_MOD (down_modifier, "down", 4);
6439 MULTI_LETTER_MOD (double_modifier, "double", 6);
6440 break;
6442 case 't':
6443 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
6444 break;
6446 #undef SINGLE_LETTER_MOD
6447 #undef MULTI_LETTER_MOD
6450 return 0;
6453 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
6454 Such a list is not valid as an event,
6455 but it can be a Lucid-style event type list. */
6458 lucid_event_type_list_p (object)
6459 Lisp_Object object;
6461 Lisp_Object tail;
6463 if (! CONSP (object))
6464 return 0;
6466 if (EQ (XCAR (object), Qhelp_echo)
6467 || EQ (XCAR (object), Qvertical_line)
6468 || EQ (XCAR (object), Qmode_line)
6469 || EQ (XCAR (object), Qheader_line))
6470 return 0;
6472 for (tail = object; CONSP (tail); tail = XCDR (tail))
6474 Lisp_Object elt;
6475 elt = XCAR (tail);
6476 if (! (INTEGERP (elt) || SYMBOLP (elt)))
6477 return 0;
6480 return NILP (tail);
6483 /* Store into *addr a value nonzero if terminal input chars are available.
6484 Serves the purpose of ioctl (0, FIONREAD, addr)
6485 but works even if FIONREAD does not exist.
6486 (In fact, this may actually read some input.)
6488 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe.
6489 If FILTER_EVENTS is nonzero, ignore internal events (FOCUS_IN_EVENT). */
6491 static void
6492 get_filtered_input_pending (addr, do_timers_now, filter_events)
6493 int *addr;
6494 int do_timers_now;
6495 int filter_events;
6497 /* First of all, have we already counted some input? */
6498 *addr = (!NILP (Vquit_flag)
6499 || readable_filtered_events (do_timers_now, filter_events));
6501 /* If input is being read as it arrives, and we have none, there is none. */
6502 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
6503 return;
6505 /* Try to read some input and see how much we get. */
6506 gobble_input (0);
6507 *addr = (!NILP (Vquit_flag)
6508 || readable_filtered_events (do_timers_now, filter_events));
6511 /* Store into *addr a value nonzero if terminal input chars are available.
6512 Serves the purpose of ioctl (0, FIONREAD, addr)
6513 but works even if FIONREAD does not exist.
6514 (In fact, this may actually read some input.)
6516 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe. */
6518 static void
6519 get_input_pending (addr, do_timers_now)
6520 int *addr;
6521 int do_timers_now;
6523 get_filtered_input_pending (addr, do_timers_now, 0);
6526 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
6528 void
6529 gobble_input (expected)
6530 int expected;
6532 #ifndef VMS
6533 #ifdef SIGIO
6534 if (interrupt_input)
6536 SIGMASKTYPE mask;
6537 mask = sigblock (sigmask (SIGIO));
6538 read_avail_input (expected);
6539 sigsetmask (mask);
6541 else
6542 #ifdef POLL_FOR_INPUT
6543 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
6545 SIGMASKTYPE mask;
6546 mask = sigblock (sigmask (SIGALRM));
6547 read_avail_input (expected);
6548 sigsetmask (mask);
6550 else
6551 #endif
6552 #endif
6553 read_avail_input (expected);
6554 #endif
6557 /* Put a BUFFER_SWITCH_EVENT in the buffer
6558 so that read_key_sequence will notice the new current buffer. */
6560 void
6561 record_asynch_buffer_change ()
6563 struct input_event event;
6564 Lisp_Object tem;
6565 EVENT_INIT (event);
6567 event.kind = BUFFER_SWITCH_EVENT;
6568 event.frame_or_window = Qnil;
6569 event.arg = Qnil;
6571 #ifdef subprocesses
6572 /* We don't need a buffer-switch event unless Emacs is waiting for input.
6573 The purpose of the event is to make read_key_sequence look up the
6574 keymaps again. If we aren't in read_key_sequence, we don't need one,
6575 and the event could cause trouble by messing up (input-pending-p). */
6576 tem = Fwaiting_for_user_input_p ();
6577 if (NILP (tem))
6578 return;
6579 #else
6580 /* We never need these events if we have no asynchronous subprocesses. */
6581 return;
6582 #endif
6584 /* Make sure no interrupt happens while storing the event. */
6585 #ifdef SIGIO
6586 if (interrupt_input)
6588 SIGMASKTYPE mask;
6589 mask = sigblock (sigmask (SIGIO));
6590 kbd_buffer_store_event (&event);
6591 sigsetmask (mask);
6593 else
6594 #endif
6596 stop_polling ();
6597 kbd_buffer_store_event (&event);
6598 start_polling ();
6602 #ifndef VMS
6604 /* Read any terminal input already buffered up by the system
6605 into the kbd_buffer, but do not wait.
6607 EXPECTED should be nonzero if the caller knows there is some input.
6609 Except on VMS, all input is read by this function.
6610 If interrupt_input is nonzero, this function MUST be called
6611 only when SIGIO is blocked.
6613 Returns the number of keyboard chars read, or -1 meaning
6614 this is a bad time to try to read input. */
6616 static int
6617 read_avail_input (expected)
6618 int expected;
6620 register int i;
6621 int nread = 0;
6623 if (read_socket_hook)
6625 int discard = 0;
6626 int nr;
6627 struct input_event hold_quit;
6629 EVENT_INIT (hold_quit);
6630 hold_quit.kind = NO_EVENT;
6632 /* No need for FIONREAD or fcntl; just say don't wait. */
6633 while (nr = (*read_socket_hook) (input_fd, expected, &hold_quit), nr > 0)
6635 nread += nr;
6636 expected = 0;
6638 if (hold_quit.kind != NO_EVENT)
6639 kbd_buffer_store_event (&hold_quit);
6641 else
6643 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
6644 the kbd_buffer can really hold. That may prevent loss
6645 of characters on some systems when input is stuffed at us. */
6646 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
6647 int n_to_read;
6649 /* Determine how many characters we should *try* to read. */
6650 #ifdef WINDOWSNT
6651 return 0;
6652 #else /* not WINDOWSNT */
6653 #ifdef MSDOS
6654 n_to_read = dos_keysns ();
6655 if (n_to_read == 0)
6656 return 0;
6657 #else /* not MSDOS */
6658 #ifdef FIONREAD
6659 /* Find out how much input is available. */
6660 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
6661 /* Formerly simply reported no input, but that sometimes led to
6662 a failure of Emacs to terminate.
6663 SIGHUP seems appropriate if we can't reach the terminal. */
6664 /* ??? Is it really right to send the signal just to this process
6665 rather than to the whole process group?
6666 Perhaps on systems with FIONREAD Emacs is alone in its group. */
6668 if (! noninteractive)
6669 kill (getpid (), SIGHUP);
6670 else
6671 n_to_read = 0;
6673 if (n_to_read == 0)
6674 return 0;
6675 if (n_to_read > sizeof cbuf)
6676 n_to_read = sizeof cbuf;
6677 #else /* no FIONREAD */
6678 #if defined (USG) || defined (DGUX) || defined(CYGWIN)
6679 /* Read some input if available, but don't wait. */
6680 n_to_read = sizeof cbuf;
6681 fcntl (input_fd, F_SETFL, O_NDELAY);
6682 #else
6683 you lose;
6684 #endif
6685 #endif
6686 #endif /* not MSDOS */
6687 #endif /* not WINDOWSNT */
6689 /* Now read; for one reason or another, this will not block.
6690 NREAD is set to the number of chars read. */
6693 #ifdef MSDOS
6694 cbuf[0] = dos_keyread ();
6695 nread = 1;
6696 #else
6697 nread = emacs_read (input_fd, cbuf, n_to_read);
6698 #endif
6699 /* POSIX infers that processes which are not in the session leader's
6700 process group won't get SIGHUP's at logout time. BSDI adheres to
6701 this part standard and returns -1 from read (0) with errno==EIO
6702 when the control tty is taken away.
6703 Jeffrey Honig <jch@bsdi.com> says this is generally safe. */
6704 if (nread == -1 && errno == EIO)
6705 kill (0, SIGHUP);
6706 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
6707 /* The kernel sometimes fails to deliver SIGHUP for ptys.
6708 This looks incorrect, but it isn't, because _BSD causes
6709 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
6710 and that causes a value other than 0 when there is no input. */
6711 if (nread == 0)
6712 kill (0, SIGHUP);
6713 #endif
6715 while (
6716 /* We used to retry the read if it was interrupted.
6717 But this does the wrong thing when O_NDELAY causes
6718 an EAGAIN error. Does anybody know of a situation
6719 where a retry is actually needed? */
6720 #if 0
6721 nread < 0 && (errno == EAGAIN
6722 #ifdef EFAULT
6723 || errno == EFAULT
6724 #endif
6725 #ifdef EBADSLT
6726 || errno == EBADSLT
6727 #endif
6729 #else
6731 #endif
6734 #ifndef FIONREAD
6735 #if defined (USG) || defined (DGUX) || defined (CYGWIN)
6736 fcntl (input_fd, F_SETFL, 0);
6737 #endif /* USG or DGUX or CYGWIN */
6738 #endif /* no FIONREAD */
6739 for (i = 0; i < nread; i++)
6741 struct input_event buf;
6742 EVENT_INIT (buf);
6743 buf.kind = ASCII_KEYSTROKE_EVENT;
6744 buf.modifiers = 0;
6745 if (meta_key == 1 && (cbuf[i] & 0x80))
6746 buf.modifiers = meta_modifier;
6747 if (meta_key != 2)
6748 cbuf[i] &= ~0x80;
6750 buf.code = cbuf[i];
6751 buf.frame_or_window = selected_frame;
6752 buf.arg = Qnil;
6754 kbd_buffer_store_event (&buf);
6755 /* Don't look at input that follows a C-g too closely.
6756 This reduces lossage due to autorepeat on C-g. */
6757 if (buf.kind == ASCII_KEYSTROKE_EVENT
6758 && buf.code == quit_char)
6759 break;
6763 return nread;
6765 #endif /* not VMS */
6767 void
6768 handle_async_input ()
6770 #ifdef BSD4_1
6771 extern int select_alarmed;
6772 #endif
6773 interrupt_input_pending = 0;
6775 while (1)
6777 int nread;
6778 nread = read_avail_input (1);
6779 /* -1 means it's not ok to read the input now.
6780 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
6781 0 means there was no keyboard input available. */
6782 if (nread <= 0)
6783 break;
6785 #ifdef BSD4_1
6786 select_alarmed = 1; /* Force the select emulator back to life */
6787 #endif
6791 #ifdef SIGIO /* for entire page */
6792 /* Note SIGIO has been undef'd if FIONREAD is missing. */
6794 static SIGTYPE
6795 input_available_signal (signo)
6796 int signo;
6798 /* Must preserve main program's value of errno. */
6799 int old_errno = errno;
6801 #if defined (USG) && !defined (POSIX_SIGNALS)
6802 /* USG systems forget handlers when they are used;
6803 must reestablish each time */
6804 signal (signo, input_available_signal);
6805 #endif /* USG */
6807 #ifdef BSD4_1
6808 sigisheld (SIGIO);
6809 #endif
6811 if (input_available_clear_time)
6812 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
6814 #ifdef SYNC_INPUT
6815 interrupt_input_pending = 1;
6816 #else
6817 handle_async_input ();
6818 #endif
6820 #ifdef BSD4_1
6821 sigfree ();
6822 #endif
6823 errno = old_errno;
6825 #endif /* SIGIO */
6827 /* Send ourselves a SIGIO.
6829 This function exists so that the UNBLOCK_INPUT macro in
6830 blockinput.h can have some way to take care of input we put off
6831 dealing with, without assuming that every file which uses
6832 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
6833 void
6834 reinvoke_input_signal ()
6836 #ifdef SIGIO
6837 handle_async_input ();
6838 #endif
6843 static void menu_bar_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object, void*));
6844 static Lisp_Object menu_bar_one_keymap_changed_items;
6846 /* These variables hold the vector under construction within
6847 menu_bar_items and its subroutines, and the current index
6848 for storing into that vector. */
6849 static Lisp_Object menu_bar_items_vector;
6850 static int menu_bar_items_index;
6852 /* Return a vector of menu items for a menu bar, appropriate
6853 to the current buffer. Each item has three elements in the vector:
6854 KEY STRING MAPLIST.
6856 OLD is an old vector we can optionally reuse, or nil. */
6858 Lisp_Object
6859 menu_bar_items (old)
6860 Lisp_Object old;
6862 /* The number of keymaps we're scanning right now, and the number of
6863 keymaps we have allocated space for. */
6864 int nmaps;
6866 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
6867 in the current keymaps, or nil where it is not a prefix. */
6868 Lisp_Object *maps;
6870 Lisp_Object def, tail;
6872 Lisp_Object result;
6874 int mapno;
6875 Lisp_Object oquit;
6877 int i;
6879 struct gcpro gcpro1;
6881 /* In order to build the menus, we need to call the keymap
6882 accessors. They all call QUIT. But this function is called
6883 during redisplay, during which a quit is fatal. So inhibit
6884 quitting while building the menus.
6885 We do this instead of specbind because (1) errors will clear it anyway
6886 and (2) this avoids risk of specpdl overflow. */
6887 oquit = Vinhibit_quit;
6888 Vinhibit_quit = Qt;
6890 if (!NILP (old))
6891 menu_bar_items_vector = old;
6892 else
6893 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
6894 menu_bar_items_index = 0;
6896 GCPRO1 (menu_bar_items_vector);
6898 /* Build our list of keymaps.
6899 If we recognize a function key and replace its escape sequence in
6900 keybuf with its symbol, or if the sequence starts with a mouse
6901 click and we need to switch buffers, we jump back here to rebuild
6902 the initial keymaps from the current buffer. */
6904 Lisp_Object *tmaps;
6906 /* Should overriding-terminal-local-map and overriding-local-map apply? */
6907 if (!NILP (Voverriding_local_map_menu_flag))
6909 /* Yes, use them (if non-nil) as well as the global map. */
6910 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
6911 nmaps = 0;
6912 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6913 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6914 if (!NILP (Voverriding_local_map))
6915 maps[nmaps++] = Voverriding_local_map;
6917 else
6919 /* No, so use major and minor mode keymaps and keymap property.
6920 Note that menu-bar bindings in the local-map and keymap
6921 properties may not work reliable, as they are only
6922 recognized when the menu-bar (or mode-line) is updated,
6923 which does not normally happen after every command. */
6924 Lisp_Object tem;
6925 int nminor;
6926 nminor = current_minor_maps (NULL, &tmaps);
6927 maps = (Lisp_Object *) alloca ((nminor + 3) * sizeof (maps[0]));
6928 nmaps = 0;
6929 if (tem = get_local_map (PT, current_buffer, Qkeymap), !NILP (tem))
6930 maps[nmaps++] = tem;
6931 bcopy (tmaps, (void *) (maps + nmaps), nminor * sizeof (maps[0]));
6932 nmaps += nminor;
6933 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
6935 maps[nmaps++] = current_global_map;
6938 /* Look up in each map the dummy prefix key `menu-bar'. */
6940 result = Qnil;
6942 for (mapno = nmaps - 1; mapno >= 0; mapno--)
6943 if (!NILP (maps[mapno]))
6945 def = get_keymap (access_keymap (maps[mapno], Qmenu_bar, 1, 0, 1),
6946 0, 1);
6947 if (CONSP (def))
6949 menu_bar_one_keymap_changed_items = Qnil;
6950 map_keymap (def, menu_bar_item, Qnil, NULL, 1);
6954 /* Move to the end those items that should be at the end. */
6956 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCDR (tail))
6958 int i;
6959 int end = menu_bar_items_index;
6961 for (i = 0; i < end; i += 4)
6962 if (EQ (XCAR (tail), XVECTOR (menu_bar_items_vector)->contents[i]))
6964 Lisp_Object tem0, tem1, tem2, tem3;
6965 /* Move the item at index I to the end,
6966 shifting all the others forward. */
6967 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
6968 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
6969 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
6970 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
6971 if (end > i + 4)
6972 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
6973 &XVECTOR (menu_bar_items_vector)->contents[i],
6974 (end - i - 4) * sizeof (Lisp_Object));
6975 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
6976 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
6977 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
6978 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
6979 break;
6983 /* Add nil, nil, nil, nil at the end. */
6984 i = menu_bar_items_index;
6985 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
6987 Lisp_Object tem;
6988 tem = Fmake_vector (make_number (2 * i), Qnil);
6989 bcopy (XVECTOR (menu_bar_items_vector)->contents,
6990 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
6991 menu_bar_items_vector = tem;
6993 /* Add this item. */
6994 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6995 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6996 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6997 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6998 menu_bar_items_index = i;
7000 Vinhibit_quit = oquit;
7001 UNGCPRO;
7002 return menu_bar_items_vector;
7005 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
7006 If there's already an item for KEY, add this DEF to it. */
7008 Lisp_Object item_properties;
7010 static void
7011 menu_bar_item (key, item, dummy1, dummy2)
7012 Lisp_Object key, item, dummy1;
7013 void *dummy2;
7015 struct gcpro gcpro1;
7016 int i;
7017 Lisp_Object tem;
7019 if (EQ (item, Qundefined))
7021 /* If a map has an explicit `undefined' as definition,
7022 discard any previously made menu bar item. */
7024 for (i = 0; i < menu_bar_items_index; i += 4)
7025 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
7027 if (menu_bar_items_index > i + 4)
7028 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
7029 &XVECTOR (menu_bar_items_vector)->contents[i],
7030 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
7031 menu_bar_items_index -= 4;
7035 /* If this keymap has already contributed to this KEY,
7036 don't contribute to it a second time. */
7037 tem = Fmemq (key, menu_bar_one_keymap_changed_items);
7038 if (!NILP (tem) || NILP (item))
7039 return;
7041 menu_bar_one_keymap_changed_items
7042 = Fcons (key, menu_bar_one_keymap_changed_items);
7044 /* We add to menu_bar_one_keymap_changed_items before doing the
7045 parse_menu_item, so that if it turns out it wasn't a menu item,
7046 it still correctly hides any further menu item. */
7047 GCPRO1 (key);
7048 i = parse_menu_item (item, 0, 1);
7049 UNGCPRO;
7050 if (!i)
7051 return;
7053 item = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
7055 /* Find any existing item for this KEY. */
7056 for (i = 0; i < menu_bar_items_index; i += 4)
7057 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
7058 break;
7060 /* If we did not find this KEY, add it at the end. */
7061 if (i == menu_bar_items_index)
7063 /* If vector is too small, get a bigger one. */
7064 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
7066 Lisp_Object tem;
7067 tem = Fmake_vector (make_number (2 * i), Qnil);
7068 bcopy (XVECTOR (menu_bar_items_vector)->contents,
7069 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
7070 menu_bar_items_vector = tem;
7073 /* Add this item. */
7074 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
7075 XVECTOR (menu_bar_items_vector)->contents[i++]
7076 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
7077 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (item, Qnil);
7078 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
7079 menu_bar_items_index = i;
7081 /* We did find an item for this KEY. Add ITEM to its list of maps. */
7082 else
7084 Lisp_Object old;
7085 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
7086 /* If the new and the old items are not both keymaps,
7087 the lookup will only find `item'. */
7088 item = Fcons (item, KEYMAPP (item) && KEYMAPP (XCAR (old)) ? old : Qnil);
7089 XVECTOR (menu_bar_items_vector)->contents[i + 2] = item;
7093 /* This is used as the handler when calling menu_item_eval_property. */
7094 static Lisp_Object
7095 menu_item_eval_property_1 (arg)
7096 Lisp_Object arg;
7098 /* If we got a quit from within the menu computation,
7099 quit all the way out of it. This takes care of C-] in the debugger. */
7100 if (CONSP (arg) && EQ (XCAR (arg), Qquit))
7101 Fsignal (Qquit, Qnil);
7103 return Qnil;
7106 /* Evaluate an expression and return the result (or nil if something
7107 went wrong). Used to evaluate dynamic parts of menu items. */
7108 Lisp_Object
7109 menu_item_eval_property (sexpr)
7110 Lisp_Object sexpr;
7112 int count = SPECPDL_INDEX ();
7113 Lisp_Object val;
7114 specbind (Qinhibit_redisplay, Qt);
7115 val = internal_condition_case_1 (Feval, sexpr, Qerror,
7116 menu_item_eval_property_1);
7117 return unbind_to (count, val);
7120 /* This function parses a menu item and leaves the result in the
7121 vector item_properties.
7122 ITEM is a key binding, a possible menu item.
7123 If NOTREAL is nonzero, only check for equivalent key bindings, don't
7124 evaluate dynamic expressions in the menu item.
7125 INMENUBAR is > 0 when this is considered for an entry in a menu bar
7126 top level.
7127 INMENUBAR is < 0 when this is considered for an entry in a keyboard menu.
7128 parse_menu_item returns true if the item is a menu item and false
7129 otherwise. */
7132 parse_menu_item (item, notreal, inmenubar)
7133 Lisp_Object item;
7134 int notreal, inmenubar;
7136 Lisp_Object def, tem, item_string, start;
7137 Lisp_Object cachelist;
7138 Lisp_Object filter;
7139 Lisp_Object keyhint;
7140 int i;
7141 int newcache = 0;
7143 cachelist = Qnil;
7144 filter = Qnil;
7145 keyhint = Qnil;
7147 if (!CONSP (item))
7148 return 0;
7150 /* Create item_properties vector if necessary. */
7151 if (NILP (item_properties))
7152 item_properties
7153 = Fmake_vector (make_number (ITEM_PROPERTY_ENABLE + 1), Qnil);
7155 /* Initialize optional entries. */
7156 for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++)
7157 AREF (item_properties, i) = Qnil;
7158 AREF (item_properties, ITEM_PROPERTY_ENABLE) = Qt;
7160 /* Save the item here to protect it from GC. */
7161 AREF (item_properties, ITEM_PROPERTY_ITEM) = item;
7163 item_string = XCAR (item);
7165 start = item;
7166 item = XCDR (item);
7167 if (STRINGP (item_string))
7169 /* Old format menu item. */
7170 AREF (item_properties, ITEM_PROPERTY_NAME) = item_string;
7172 /* Maybe help string. */
7173 if (CONSP (item) && STRINGP (XCAR (item)))
7175 AREF (item_properties, ITEM_PROPERTY_HELP) = XCAR (item);
7176 start = item;
7177 item = XCDR (item);
7180 /* Maybe key binding cache. */
7181 if (CONSP (item) && CONSP (XCAR (item))
7182 && (NILP (XCAR (XCAR (item)))
7183 || VECTORP (XCAR (XCAR (item)))))
7185 cachelist = XCAR (item);
7186 item = XCDR (item);
7189 /* This is the real definition--the function to run. */
7190 AREF (item_properties, ITEM_PROPERTY_DEF) = item;
7192 /* Get enable property, if any. */
7193 if (SYMBOLP (item))
7195 tem = Fget (item, Qmenu_enable);
7196 if (!NILP (tem))
7197 AREF (item_properties, ITEM_PROPERTY_ENABLE) = tem;
7200 else if (EQ (item_string, Qmenu_item) && CONSP (item))
7202 /* New format menu item. */
7203 AREF (item_properties, ITEM_PROPERTY_NAME) = XCAR (item);
7204 start = XCDR (item);
7205 if (CONSP (start))
7207 /* We have a real binding. */
7208 AREF (item_properties, ITEM_PROPERTY_DEF) = XCAR (start);
7210 item = XCDR (start);
7211 /* Is there a cache list with key equivalences. */
7212 if (CONSP (item) && CONSP (XCAR (item)))
7214 cachelist = XCAR (item);
7215 item = XCDR (item);
7218 /* Parse properties. */
7219 while (CONSP (item) && CONSP (XCDR (item)))
7221 tem = XCAR (item);
7222 item = XCDR (item);
7224 if (EQ (tem, QCenable))
7225 AREF (item_properties, ITEM_PROPERTY_ENABLE) = XCAR (item);
7226 else if (EQ (tem, QCvisible) && !notreal)
7228 /* If got a visible property and that evaluates to nil
7229 then ignore this item. */
7230 tem = menu_item_eval_property (XCAR (item));
7231 if (NILP (tem))
7232 return 0;
7234 else if (EQ (tem, QChelp))
7235 AREF (item_properties, ITEM_PROPERTY_HELP) = XCAR (item);
7236 else if (EQ (tem, QCfilter))
7237 filter = item;
7238 else if (EQ (tem, QCkey_sequence))
7240 tem = XCAR (item);
7241 if (NILP (cachelist)
7242 && (SYMBOLP (tem) || STRINGP (tem) || VECTORP (tem)))
7243 /* Be GC protected. Set keyhint to item instead of tem. */
7244 keyhint = item;
7246 else if (EQ (tem, QCkeys))
7248 tem = XCAR (item);
7249 if (CONSP (tem) || (STRINGP (tem) && NILP (cachelist)))
7250 AREF (item_properties, ITEM_PROPERTY_KEYEQ) = tem;
7252 else if (EQ (tem, QCbutton) && CONSP (XCAR (item)))
7254 Lisp_Object type;
7255 tem = XCAR (item);
7256 type = XCAR (tem);
7257 if (EQ (type, QCtoggle) || EQ (type, QCradio))
7259 AREF (item_properties, ITEM_PROPERTY_SELECTED)
7260 = XCDR (tem);
7261 AREF (item_properties, ITEM_PROPERTY_TYPE)
7262 = type;
7265 item = XCDR (item);
7268 else if (inmenubar || !NILP (start))
7269 return 0;
7271 else
7272 return 0; /* not a menu item */
7274 /* If item string is not a string, evaluate it to get string.
7275 If we don't get a string, skip this item. */
7276 item_string = AREF (item_properties, ITEM_PROPERTY_NAME);
7277 if (!(STRINGP (item_string) || notreal))
7279 item_string = menu_item_eval_property (item_string);
7280 if (!STRINGP (item_string))
7281 return 0;
7282 AREF (item_properties, ITEM_PROPERTY_NAME) = item_string;
7285 /* If got a filter apply it on definition. */
7286 def = AREF (item_properties, ITEM_PROPERTY_DEF);
7287 if (!NILP (filter))
7289 def = menu_item_eval_property (list2 (XCAR (filter),
7290 list2 (Qquote, def)));
7292 AREF (item_properties, ITEM_PROPERTY_DEF) = def;
7295 /* Enable or disable selection of item. */
7296 tem = AREF (item_properties, ITEM_PROPERTY_ENABLE);
7297 if (!EQ (tem, Qt))
7299 if (notreal)
7300 tem = Qt;
7301 else
7302 tem = menu_item_eval_property (tem);
7303 if (inmenubar && NILP (tem))
7304 return 0; /* Ignore disabled items in menu bar. */
7305 AREF (item_properties, ITEM_PROPERTY_ENABLE) = tem;
7308 /* If we got no definition, this item is just unselectable text which
7309 is OK in a submenu but not in the menubar. */
7310 if (NILP (def))
7311 return (inmenubar ? 0 : 1);
7313 /* See if this is a separate pane or a submenu. */
7314 def = AREF (item_properties, ITEM_PROPERTY_DEF);
7315 tem = get_keymap (def, 0, 1);
7316 /* For a subkeymap, just record its details and exit. */
7317 if (CONSP (tem))
7319 AREF (item_properties, ITEM_PROPERTY_MAP) = tem;
7320 AREF (item_properties, ITEM_PROPERTY_DEF) = tem;
7321 return 1;
7324 /* At the top level in the menu bar, do likewise for commands also.
7325 The menu bar does not display equivalent key bindings anyway.
7326 ITEM_PROPERTY_DEF is already set up properly. */
7327 if (inmenubar > 0)
7328 return 1;
7330 /* This is a command. See if there is an equivalent key binding. */
7331 if (NILP (cachelist))
7333 /* We have to create a cachelist. */
7334 CHECK_IMPURE (start);
7335 XSETCDR (start, Fcons (Fcons (Qnil, Qnil), XCDR (start)));
7336 cachelist = XCAR (XCDR (start));
7337 newcache = 1;
7338 tem = AREF (item_properties, ITEM_PROPERTY_KEYEQ);
7339 if (!NILP (keyhint))
7341 XSETCAR (cachelist, XCAR (keyhint));
7342 newcache = 0;
7344 else if (STRINGP (tem))
7346 XSETCDR (cachelist, Fsubstitute_command_keys (tem));
7347 XSETCAR (cachelist, Qt);
7351 tem = XCAR (cachelist);
7352 if (!EQ (tem, Qt))
7354 int chkcache = 0;
7355 Lisp_Object prefix;
7357 if (!NILP (tem))
7358 tem = Fkey_binding (tem, Qnil, Qnil);
7360 prefix = AREF (item_properties, ITEM_PROPERTY_KEYEQ);
7361 if (CONSP (prefix))
7363 def = XCAR (prefix);
7364 prefix = XCDR (prefix);
7366 else
7367 def = AREF (item_properties, ITEM_PROPERTY_DEF);
7369 if (!update_menu_bindings)
7370 chkcache = 0;
7371 else if (NILP (XCAR (cachelist))) /* Have no saved key. */
7373 if (newcache /* Always check first time. */
7374 /* Should we check everything when precomputing key
7375 bindings? */
7376 /* If something had no key binding before, don't recheck it
7377 because that is too slow--except if we have a list of
7378 rebound commands in Vdefine_key_rebound_commands, do
7379 recheck any command that appears in that list. */
7380 || (CONSP (Vdefine_key_rebound_commands)
7381 && !NILP (Fmemq (def, Vdefine_key_rebound_commands))))
7382 chkcache = 1;
7384 /* We had a saved key. Is it still bound to the command? */
7385 else if (NILP (tem)
7386 || (!EQ (tem, def)
7387 /* If the command is an alias for another
7388 (such as lmenu.el set it up), check if the
7389 original command matches the cached command. */
7390 && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function))))
7391 chkcache = 1; /* Need to recompute key binding. */
7393 if (chkcache)
7395 /* Recompute equivalent key binding. If the command is an alias
7396 for another (such as lmenu.el set it up), see if the original
7397 command name has equivalent keys. Otherwise look up the
7398 specified command itself. We don't try both, because that
7399 makes lmenu menus slow. */
7400 if (SYMBOLP (def)
7401 && SYMBOLP (XSYMBOL (def)->function)
7402 && ! NILP (Fget (def, Qmenu_alias)))
7403 def = XSYMBOL (def)->function;
7404 tem = Fwhere_is_internal (def, Qnil, Qt, Qnil, Qt);
7405 XSETCAR (cachelist, tem);
7406 if (NILP (tem))
7408 XSETCDR (cachelist, Qnil);
7409 chkcache = 0;
7412 else if (!NILP (keyhint) && !NILP (XCAR (cachelist)))
7414 tem = XCAR (cachelist);
7415 chkcache = 1;
7418 newcache = chkcache;
7419 if (chkcache)
7421 tem = Fkey_description (tem, Qnil);
7422 if (CONSP (prefix))
7424 if (STRINGP (XCAR (prefix)))
7425 tem = concat2 (XCAR (prefix), tem);
7426 if (STRINGP (XCDR (prefix)))
7427 tem = concat2 (tem, XCDR (prefix));
7429 XSETCDR (cachelist, tem);
7433 tem = XCDR (cachelist);
7434 if (newcache && !NILP (tem))
7436 tem = concat3 (build_string (" ("), tem, build_string (")"));
7437 XSETCDR (cachelist, tem);
7440 /* If we only want to precompute equivalent key bindings, stop here. */
7441 if (notreal)
7442 return 1;
7444 /* If we have an equivalent key binding, use that. */
7445 AREF (item_properties, ITEM_PROPERTY_KEYEQ) = tem;
7447 /* Include this when menu help is implemented.
7448 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP];
7449 if (!(NILP (tem) || STRINGP (tem)))
7451 tem = menu_item_eval_property (tem);
7452 if (!STRINGP (tem))
7453 tem = Qnil;
7454 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP] = tem;
7458 /* Handle radio buttons or toggle boxes. */
7459 tem = AREF (item_properties, ITEM_PROPERTY_SELECTED);
7460 if (!NILP (tem))
7461 AREF (item_properties, ITEM_PROPERTY_SELECTED)
7462 = menu_item_eval_property (tem);
7464 return 1;
7469 /***********************************************************************
7470 Tool-bars
7471 ***********************************************************************/
7473 /* A vector holding tool bar items while they are parsed in function
7474 tool_bar_items. Each item occupies TOOL_BAR_ITEM_NSCLOTS elements
7475 in the vector. */
7477 static Lisp_Object tool_bar_items_vector;
7479 /* A vector holding the result of parse_tool_bar_item. Layout is like
7480 the one for a single item in tool_bar_items_vector. */
7482 static Lisp_Object tool_bar_item_properties;
7484 /* Next free index in tool_bar_items_vector. */
7486 static int ntool_bar_items;
7488 /* The symbols `tool-bar', and `:image'. */
7490 extern Lisp_Object Qtool_bar;
7491 Lisp_Object QCimage;
7493 /* Function prototypes. */
7495 static void init_tool_bar_items P_ ((Lisp_Object));
7496 static void process_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
7497 static int parse_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
7498 static void append_tool_bar_item P_ ((void));
7501 /* Return a vector of tool bar items for keymaps currently in effect.
7502 Reuse vector REUSE if non-nil. Return in *NITEMS the number of
7503 tool bar items found. */
7505 Lisp_Object
7506 tool_bar_items (reuse, nitems)
7507 Lisp_Object reuse;
7508 int *nitems;
7510 Lisp_Object *maps;
7511 int nmaps, i;
7512 Lisp_Object oquit;
7513 Lisp_Object *tmaps;
7515 *nitems = 0;
7517 /* In order to build the menus, we need to call the keymap
7518 accessors. They all call QUIT. But this function is called
7519 during redisplay, during which a quit is fatal. So inhibit
7520 quitting while building the menus. We do this instead of
7521 specbind because (1) errors will clear it anyway and (2) this
7522 avoids risk of specpdl overflow. */
7523 oquit = Vinhibit_quit;
7524 Vinhibit_quit = Qt;
7526 /* Initialize tool_bar_items_vector and protect it from GC. */
7527 init_tool_bar_items (reuse);
7529 /* Build list of keymaps in maps. Set nmaps to the number of maps
7530 to process. */
7532 /* Should overriding-terminal-local-map and overriding-local-map apply? */
7533 if (!NILP (Voverriding_local_map_menu_flag))
7535 /* Yes, use them (if non-nil) as well as the global map. */
7536 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
7537 nmaps = 0;
7538 if (!NILP (current_kboard->Voverriding_terminal_local_map))
7539 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
7540 if (!NILP (Voverriding_local_map))
7541 maps[nmaps++] = Voverriding_local_map;
7543 else
7545 /* No, so use major and minor mode keymaps and keymap property.
7546 Note that tool-bar bindings in the local-map and keymap
7547 properties may not work reliable, as they are only
7548 recognized when the tool-bar (or mode-line) is updated,
7549 which does not normally happen after every command. */
7550 Lisp_Object tem;
7551 int nminor;
7552 nminor = current_minor_maps (NULL, &tmaps);
7553 maps = (Lisp_Object *) alloca ((nminor + 3) * sizeof (maps[0]));
7554 nmaps = 0;
7555 if (tem = get_local_map (PT, current_buffer, Qkeymap), !NILP (tem))
7556 maps[nmaps++] = tem;
7557 bcopy (tmaps, (void *) (maps + nmaps), nminor * sizeof (maps[0]));
7558 nmaps += nminor;
7559 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
7562 /* Add global keymap at the end. */
7563 maps[nmaps++] = current_global_map;
7565 /* Process maps in reverse order and look up in each map the prefix
7566 key `tool-bar'. */
7567 for (i = nmaps - 1; i >= 0; --i)
7568 if (!NILP (maps[i]))
7570 Lisp_Object keymap;
7572 keymap = get_keymap (access_keymap (maps[i], Qtool_bar, 1, 0, 1), 0, 1);
7573 if (CONSP (keymap))
7575 Lisp_Object tail;
7577 /* KEYMAP is a list `(keymap (KEY . BINDING) ...)'. */
7578 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
7580 Lisp_Object keydef = XCAR (tail);
7581 if (CONSP (keydef))
7582 process_tool_bar_item (XCAR (keydef), XCDR (keydef));
7587 Vinhibit_quit = oquit;
7588 *nitems = ntool_bar_items / TOOL_BAR_ITEM_NSLOTS;
7589 return tool_bar_items_vector;
7593 /* Process the definition of KEY which is DEF. */
7595 static void
7596 process_tool_bar_item (key, def)
7597 Lisp_Object key, def;
7599 int i;
7600 extern Lisp_Object Qundefined;
7601 struct gcpro gcpro1, gcpro2;
7603 /* Protect KEY and DEF from GC because parse_tool_bar_item may call
7604 eval. */
7605 GCPRO2 (key, def);
7607 if (EQ (def, Qundefined))
7609 /* If a map has an explicit `undefined' as definition,
7610 discard any previously made item. */
7611 for (i = 0; i < ntool_bar_items; i += TOOL_BAR_ITEM_NSLOTS)
7613 Lisp_Object *v = XVECTOR (tool_bar_items_vector)->contents + i;
7615 if (EQ (key, v[TOOL_BAR_ITEM_KEY]))
7617 if (ntool_bar_items > i + TOOL_BAR_ITEM_NSLOTS)
7618 bcopy (v + TOOL_BAR_ITEM_NSLOTS, v,
7619 ((ntool_bar_items - i - TOOL_BAR_ITEM_NSLOTS)
7620 * sizeof (Lisp_Object)));
7621 ntool_bar_items -= TOOL_BAR_ITEM_NSLOTS;
7622 break;
7626 else if (parse_tool_bar_item (key, def))
7627 /* Append a new tool bar item to tool_bar_items_vector. Accept
7628 more than one definition for the same key. */
7629 append_tool_bar_item ();
7631 UNGCPRO;
7635 /* Parse a tool bar item specification ITEM for key KEY and return the
7636 result in tool_bar_item_properties. Value is zero if ITEM is
7637 invalid.
7639 ITEM is a list `(menu-item CAPTION BINDING PROPS...)'.
7641 CAPTION is the caption of the item, If it's not a string, it is
7642 evaluated to get a string.
7644 BINDING is the tool bar item's binding. Tool-bar items with keymaps
7645 as binding are currently ignored.
7647 The following properties are recognized:
7649 - `:enable FORM'.
7651 FORM is evaluated and specifies whether the tool bar item is
7652 enabled or disabled.
7654 - `:visible FORM'
7656 FORM is evaluated and specifies whether the tool bar item is visible.
7658 - `:filter FUNCTION'
7660 FUNCTION is invoked with one parameter `(quote BINDING)'. Its
7661 result is stored as the new binding.
7663 - `:button (TYPE SELECTED)'
7665 TYPE must be one of `:radio' or `:toggle'. SELECTED is evaluated
7666 and specifies whether the button is selected (pressed) or not.
7668 - `:image IMAGES'
7670 IMAGES is either a single image specification or a vector of four
7671 image specifications. See enum tool_bar_item_images.
7673 - `:help HELP-STRING'.
7675 Gives a help string to display for the tool bar item. */
7677 static int
7678 parse_tool_bar_item (key, item)
7679 Lisp_Object key, item;
7681 /* Access slot with index IDX of vector tool_bar_item_properties. */
7682 #define PROP(IDX) XVECTOR (tool_bar_item_properties)->contents[IDX]
7684 Lisp_Object filter = Qnil;
7685 Lisp_Object caption;
7686 int i;
7688 /* Defininition looks like `(menu-item CAPTION BINDING PROPS...)'.
7689 Rule out items that aren't lists, don't start with
7690 `menu-item' or whose rest following `tool-bar-item' is not a
7691 list. */
7692 if (!CONSP (item)
7693 || !EQ (XCAR (item), Qmenu_item)
7694 || (item = XCDR (item),
7695 !CONSP (item)))
7696 return 0;
7698 /* Create tool_bar_item_properties vector if necessary. Reset it to
7699 defaults. */
7700 if (VECTORP (tool_bar_item_properties))
7702 for (i = 0; i < TOOL_BAR_ITEM_NSLOTS; ++i)
7703 PROP (i) = Qnil;
7705 else
7706 tool_bar_item_properties
7707 = Fmake_vector (make_number (TOOL_BAR_ITEM_NSLOTS), Qnil);
7709 /* Set defaults. */
7710 PROP (TOOL_BAR_ITEM_KEY) = key;
7711 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
7713 /* Get the caption of the item. If the caption is not a string,
7714 evaluate it to get a string. If we don't get a string, skip this
7715 item. */
7716 caption = XCAR (item);
7717 if (!STRINGP (caption))
7719 caption = menu_item_eval_property (caption);
7720 if (!STRINGP (caption))
7721 return 0;
7723 PROP (TOOL_BAR_ITEM_CAPTION) = caption;
7725 /* Give up if rest following the caption is not a list. */
7726 item = XCDR (item);
7727 if (!CONSP (item))
7728 return 0;
7730 /* Store the binding. */
7731 PROP (TOOL_BAR_ITEM_BINDING) = XCAR (item);
7732 item = XCDR (item);
7734 /* Ignore cached key binding, if any. */
7735 if (CONSP (item) && CONSP (XCAR (item)))
7736 item = XCDR (item);
7738 /* Process the rest of the properties. */
7739 for (; CONSP (item) && CONSP (XCDR (item)); item = XCDR (XCDR (item)))
7741 Lisp_Object key, value;
7743 key = XCAR (item);
7744 value = XCAR (XCDR (item));
7746 if (EQ (key, QCenable))
7747 /* `:enable FORM'. */
7748 PROP (TOOL_BAR_ITEM_ENABLED_P) = value;
7749 else if (EQ (key, QCvisible))
7751 /* `:visible FORM'. If got a visible property and that
7752 evaluates to nil then ignore this item. */
7753 if (NILP (menu_item_eval_property (value)))
7754 return 0;
7756 else if (EQ (key, QChelp))
7757 /* `:help HELP-STRING'. */
7758 PROP (TOOL_BAR_ITEM_HELP) = value;
7759 else if (EQ (key, QCfilter))
7760 /* ':filter FORM'. */
7761 filter = value;
7762 else if (EQ (key, QCbutton) && CONSP (value))
7764 /* `:button (TYPE . SELECTED)'. */
7765 Lisp_Object type, selected;
7767 type = XCAR (value);
7768 selected = XCDR (value);
7769 if (EQ (type, QCtoggle) || EQ (type, QCradio))
7771 PROP (TOOL_BAR_ITEM_SELECTED_P) = selected;
7772 PROP (TOOL_BAR_ITEM_TYPE) = type;
7775 else if (EQ (key, QCimage)
7776 && (CONSP (value)
7777 || (VECTORP (value) && XVECTOR (value)->size == 4)))
7778 /* Value is either a single image specification or a vector
7779 of 4 such specifications for the different button states. */
7780 PROP (TOOL_BAR_ITEM_IMAGES) = value;
7783 /* If got a filter apply it on binding. */
7784 if (!NILP (filter))
7785 PROP (TOOL_BAR_ITEM_BINDING)
7786 = menu_item_eval_property (list2 (filter,
7787 list2 (Qquote,
7788 PROP (TOOL_BAR_ITEM_BINDING))));
7790 /* See if the binding is a keymap. Give up if it is. */
7791 if (CONSP (get_keymap (PROP (TOOL_BAR_ITEM_BINDING), 0, 1)))
7792 return 0;
7794 /* Enable or disable selection of item. */
7795 if (!EQ (PROP (TOOL_BAR_ITEM_ENABLED_P), Qt))
7796 PROP (TOOL_BAR_ITEM_ENABLED_P)
7797 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_ENABLED_P));
7799 /* Handle radio buttons or toggle boxes. */
7800 if (!NILP (PROP (TOOL_BAR_ITEM_SELECTED_P)))
7801 PROP (TOOL_BAR_ITEM_SELECTED_P)
7802 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_SELECTED_P));
7804 return 1;
7806 #undef PROP
7810 /* Initialize tool_bar_items_vector. REUSE, if non-nil, is a vector
7811 that can be reused. */
7813 static void
7814 init_tool_bar_items (reuse)
7815 Lisp_Object reuse;
7817 if (VECTORP (reuse))
7818 tool_bar_items_vector = reuse;
7819 else
7820 tool_bar_items_vector = Fmake_vector (make_number (64), Qnil);
7821 ntool_bar_items = 0;
7825 /* Append parsed tool bar item properties from
7826 tool_bar_item_properties */
7828 static void
7829 append_tool_bar_item ()
7831 Lisp_Object *to, *from;
7833 /* Enlarge tool_bar_items_vector if necessary. */
7834 if (ntool_bar_items + TOOL_BAR_ITEM_NSLOTS
7835 >= XVECTOR (tool_bar_items_vector)->size)
7837 Lisp_Object new_vector;
7838 int old_size = XVECTOR (tool_bar_items_vector)->size;
7840 new_vector = Fmake_vector (make_number (2 * old_size), Qnil);
7841 bcopy (XVECTOR (tool_bar_items_vector)->contents,
7842 XVECTOR (new_vector)->contents,
7843 old_size * sizeof (Lisp_Object));
7844 tool_bar_items_vector = new_vector;
7847 /* Append entries from tool_bar_item_properties to the end of
7848 tool_bar_items_vector. */
7849 to = XVECTOR (tool_bar_items_vector)->contents + ntool_bar_items;
7850 from = XVECTOR (tool_bar_item_properties)->contents;
7851 bcopy (from, to, TOOL_BAR_ITEM_NSLOTS * sizeof *to);
7852 ntool_bar_items += TOOL_BAR_ITEM_NSLOTS;
7859 /* Read a character using menus based on maps in the array MAPS.
7860 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
7861 Return t if we displayed a menu but the user rejected it.
7863 PREV_EVENT is the previous input event, or nil if we are reading
7864 the first event of a key sequence.
7866 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
7867 if we used a mouse menu to read the input, or zero otherwise. If
7868 USED_MOUSE_MENU is null, we don't dereference it.
7870 The prompting is done based on the prompt-string of the map
7871 and the strings associated with various map elements.
7873 This can be done with X menus or with menus put in the minibuf.
7874 These are done in different ways, depending on how the input will be read.
7875 Menus using X are done after auto-saving in read-char, getting the input
7876 event from Fx_popup_menu; menus using the minibuf use read_char recursively
7877 and do auto-saving in the inner call of read_char. */
7879 static Lisp_Object
7880 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
7881 int nmaps;
7882 Lisp_Object *maps;
7883 Lisp_Object prev_event;
7884 int *used_mouse_menu;
7886 int mapno;
7887 register Lisp_Object name = Qnil;
7889 if (used_mouse_menu)
7890 *used_mouse_menu = 0;
7892 /* Use local over global Menu maps */
7894 if (! menu_prompting)
7895 return Qnil;
7897 /* Optionally disregard all but the global map. */
7898 if (inhibit_local_menu_bar_menus)
7900 maps += (nmaps - 1);
7901 nmaps = 1;
7904 /* Get the menu name from the first map that has one (a prompt string). */
7905 for (mapno = 0; mapno < nmaps; mapno++)
7907 name = Fkeymap_prompt (maps[mapno]);
7908 if (!NILP (name))
7909 break;
7912 /* If we don't have any menus, just read a character normally. */
7913 if (!STRINGP (name))
7914 return Qnil;
7916 #ifdef HAVE_MENUS
7917 /* If we got to this point via a mouse click,
7918 use a real menu for mouse selection. */
7919 if (EVENT_HAS_PARAMETERS (prev_event)
7920 && !EQ (XCAR (prev_event), Qmenu_bar)
7921 && !EQ (XCAR (prev_event), Qtool_bar))
7923 /* Display the menu and get the selection. */
7924 Lisp_Object *realmaps
7925 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
7926 Lisp_Object value;
7927 int nmaps1 = 0;
7929 /* Use the maps that are not nil. */
7930 for (mapno = 0; mapno < nmaps; mapno++)
7931 if (!NILP (maps[mapno]))
7932 realmaps[nmaps1++] = maps[mapno];
7934 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
7935 if (CONSP (value))
7937 Lisp_Object tem;
7939 record_menu_key (XCAR (value));
7941 /* If we got multiple events, unread all but
7942 the first.
7943 There is no way to prevent those unread events
7944 from showing up later in last_nonmenu_event.
7945 So turn symbol and integer events into lists,
7946 to indicate that they came from a mouse menu,
7947 so that when present in last_nonmenu_event
7948 they won't confuse things. */
7949 for (tem = XCDR (value); !NILP (tem); tem = XCDR (tem))
7951 record_menu_key (XCAR (tem));
7952 if (SYMBOLP (XCAR (tem))
7953 || INTEGERP (XCAR (tem)))
7954 XSETCAR (tem, Fcons (XCAR (tem), Qdisabled));
7957 /* If we got more than one event, put all but the first
7958 onto this list to be read later.
7959 Return just the first event now. */
7960 Vunread_command_events
7961 = nconc2 (XCDR (value), Vunread_command_events);
7962 value = XCAR (value);
7964 else if (NILP (value))
7965 value = Qt;
7966 if (used_mouse_menu)
7967 *used_mouse_menu = 1;
7968 return value;
7970 #endif /* HAVE_MENUS */
7971 return Qnil ;
7974 /* Buffer in use so far for the minibuf prompts for menu keymaps.
7975 We make this bigger when necessary, and never free it. */
7976 static char *read_char_minibuf_menu_text;
7977 /* Size of that buffer. */
7978 static int read_char_minibuf_menu_width;
7980 static Lisp_Object
7981 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
7982 int commandflag ;
7983 int nmaps;
7984 Lisp_Object *maps;
7986 int mapno;
7987 register Lisp_Object name;
7988 int nlength;
7989 /* FIXME: Use the minibuffer's frame width. */
7990 int width = FRAME_COLS (SELECTED_FRAME ()) - 4;
7991 int idx = -1;
7992 int nobindings = 1;
7993 Lisp_Object rest, vector;
7994 char *menu;
7996 vector = Qnil;
7997 name = Qnil;
7999 if (! menu_prompting)
8000 return Qnil;
8002 /* Make sure we have a big enough buffer for the menu text. */
8003 if (read_char_minibuf_menu_text == 0)
8005 read_char_minibuf_menu_width = width + 4;
8006 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
8008 else if (width + 4 > read_char_minibuf_menu_width)
8010 read_char_minibuf_menu_width = width + 4;
8011 read_char_minibuf_menu_text
8012 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
8014 menu = read_char_minibuf_menu_text;
8016 /* Get the menu name from the first map that has one (a prompt string). */
8017 for (mapno = 0; mapno < nmaps; mapno++)
8019 name = Fkeymap_prompt (maps[mapno]);
8020 if (!NILP (name))
8021 break;
8024 /* If we don't have any menus, just read a character normally. */
8025 if (!STRINGP (name))
8026 return Qnil;
8028 /* Prompt string always starts with map's prompt, and a space. */
8029 strcpy (menu, SDATA (name));
8030 nlength = SBYTES (name);
8031 menu[nlength++] = ':';
8032 menu[nlength++] = ' ';
8033 menu[nlength] = 0;
8035 /* Start prompting at start of first map. */
8036 mapno = 0;
8037 rest = maps[mapno];
8039 /* Present the documented bindings, a line at a time. */
8040 while (1)
8042 int notfirst = 0;
8043 int i = nlength;
8044 Lisp_Object obj;
8045 int ch;
8046 Lisp_Object orig_defn_macro;
8048 /* Loop over elements of map. */
8049 while (i < width)
8051 Lisp_Object elt;
8053 /* If reached end of map, start at beginning of next map. */
8054 if (NILP (rest))
8056 mapno++;
8057 /* At end of last map, wrap around to first map if just starting,
8058 or end this line if already have something on it. */
8059 if (mapno == nmaps)
8061 mapno = 0;
8062 if (notfirst || nobindings) break;
8064 rest = maps[mapno];
8067 /* Look at the next element of the map. */
8068 if (idx >= 0)
8069 elt = XVECTOR (vector)->contents[idx];
8070 else
8071 elt = Fcar_safe (rest);
8073 if (idx < 0 && VECTORP (elt))
8075 /* If we found a dense table in the keymap,
8076 advanced past it, but start scanning its contents. */
8077 rest = Fcdr_safe (rest);
8078 vector = elt;
8079 idx = 0;
8081 else
8083 /* An ordinary element. */
8084 Lisp_Object event, tem;
8086 if (idx < 0)
8088 event = Fcar_safe (elt); /* alist */
8089 elt = Fcdr_safe (elt);
8091 else
8093 XSETINT (event, idx); /* vector */
8096 /* Ignore the element if it has no prompt string. */
8097 if (INTEGERP (event) && parse_menu_item (elt, 0, -1))
8099 /* 1 if the char to type matches the string. */
8100 int char_matches;
8101 Lisp_Object upcased_event, downcased_event;
8102 Lisp_Object desc = Qnil;
8103 Lisp_Object s
8104 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
8106 upcased_event = Fupcase (event);
8107 downcased_event = Fdowncase (event);
8108 char_matches = (XINT (upcased_event) == SREF (s, 0)
8109 || XINT (downcased_event) == SREF (s, 0));
8110 if (! char_matches)
8111 desc = Fsingle_key_description (event, Qnil);
8113 #if 0 /* It is redundant to list the equivalent key bindings because
8114 the prefix is what the user has already typed. */
8116 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
8117 if (!NILP (tem))
8118 /* Insert equivalent keybinding. */
8119 s = concat2 (s, tem);
8120 #endif
8122 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
8123 if (EQ (tem, QCradio) || EQ (tem, QCtoggle))
8125 /* Insert button prefix. */
8126 Lisp_Object selected
8127 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
8128 if (EQ (tem, QCradio))
8129 tem = build_string (NILP (selected) ? "(*) " : "( ) ");
8130 else
8131 tem = build_string (NILP (selected) ? "[X] " : "[ ] ");
8132 s = concat2 (tem, s);
8136 /* If we have room for the prompt string, add it to this line.
8137 If this is the first on the line, always add it. */
8138 if ((SCHARS (s) + i + 2
8139 + (char_matches ? 0 : SCHARS (desc) + 3))
8140 < width
8141 || !notfirst)
8143 int thiswidth;
8145 /* Punctuate between strings. */
8146 if (notfirst)
8148 strcpy (menu + i, ", ");
8149 i += 2;
8151 notfirst = 1;
8152 nobindings = 0 ;
8154 /* If the char to type doesn't match the string's
8155 first char, explicitly show what char to type. */
8156 if (! char_matches)
8158 /* Add as much of string as fits. */
8159 thiswidth = SCHARS (desc);
8160 if (thiswidth + i > width)
8161 thiswidth = width - i;
8162 bcopy (SDATA (desc), menu + i, thiswidth);
8163 i += thiswidth;
8164 strcpy (menu + i, " = ");
8165 i += 3;
8168 /* Add as much of string as fits. */
8169 thiswidth = SCHARS (s);
8170 if (thiswidth + i > width)
8171 thiswidth = width - i;
8172 bcopy (SDATA (s), menu + i, thiswidth);
8173 i += thiswidth;
8174 menu[i] = 0;
8176 else
8178 /* If this element does not fit, end the line now,
8179 and save the element for the next line. */
8180 strcpy (menu + i, "...");
8181 break;
8185 /* Move past this element. */
8186 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
8187 /* Handle reaching end of dense table. */
8188 idx = -1;
8189 if (idx >= 0)
8190 idx++;
8191 else
8192 rest = Fcdr_safe (rest);
8196 /* Prompt with that and read response. */
8197 message2_nolog (menu, strlen (menu),
8198 ! NILP (current_buffer->enable_multibyte_characters));
8200 /* Make believe its not a keyboard macro in case the help char
8201 is pressed. Help characters are not recorded because menu prompting
8202 is not used on replay.
8204 orig_defn_macro = current_kboard->defining_kbd_macro;
8205 current_kboard->defining_kbd_macro = Qnil;
8207 obj = read_char (commandflag, 0, 0, Qt, 0);
8208 while (BUFFERP (obj));
8209 current_kboard->defining_kbd_macro = orig_defn_macro;
8211 if (!INTEGERP (obj))
8212 return obj;
8213 else
8214 ch = XINT (obj);
8216 if (! EQ (obj, menu_prompt_more_char)
8217 && (!INTEGERP (menu_prompt_more_char)
8218 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
8220 if (!NILP (current_kboard->defining_kbd_macro))
8221 store_kbd_macro_char (obj);
8222 return obj;
8224 /* Help char - go round again */
8228 /* Reading key sequences. */
8230 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
8231 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
8232 keymap, or nil otherwise. Return the index of the first keymap in
8233 which KEY has any binding, or NMAPS if no map has a binding.
8235 If KEY is a meta ASCII character, treat it like meta-prefix-char
8236 followed by the corresponding non-meta character. Keymaps in
8237 CURRENT with non-prefix bindings for meta-prefix-char become nil in
8238 NEXT.
8240 If KEY has no bindings in any of the CURRENT maps, NEXT is left
8241 unmodified.
8243 NEXT may be the same array as CURRENT. */
8245 static int
8246 follow_key (key, nmaps, current, defs, next)
8247 Lisp_Object key;
8248 Lisp_Object *current, *defs, *next;
8249 int nmaps;
8251 int i, first_binding;
8253 first_binding = nmaps;
8254 for (i = nmaps - 1; i >= 0; i--)
8256 if (! NILP (current[i]))
8258 defs[i] = access_keymap (current[i], key, 1, 0, 1);
8259 if (! NILP (defs[i]))
8260 first_binding = i;
8262 else
8263 defs[i] = Qnil;
8266 /* Given the set of bindings we've found, produce the next set of maps. */
8267 if (first_binding < nmaps)
8268 for (i = 0; i < nmaps; i++)
8269 next[i] = NILP (defs[i]) ? Qnil : get_keymap (defs[i], 0, 1);
8271 return first_binding;
8274 /* Structure used to keep track of partial application of key remapping
8275 such as Vfunction_key_map and Vkey_translation_map. */
8276 typedef struct keyremap
8278 Lisp_Object map, parent;
8279 int start, end;
8280 } keyremap;
8282 /* Lookup KEY in MAP.
8283 MAP is a keymap mapping keys to key vectors or functions.
8284 If the mapping is a function and DO_FUNCTION is non-zero, then
8285 the function is called with PROMPT as parameter and its return
8286 value is used as the return value of this function (after checking
8287 that it is indeed a vector). */
8289 static Lisp_Object
8290 access_keymap_keyremap (map, key, prompt, do_funcall)
8291 Lisp_Object map, key, prompt;
8292 int do_funcall;
8294 Lisp_Object next;
8296 next = access_keymap (map, key, 1, 0, 1);
8298 /* Handle symbol with autoload definition. */
8299 if (SYMBOLP (next) && !NILP (Ffboundp (next))
8300 && CONSP (XSYMBOL (next)->function)
8301 && EQ (XCAR (XSYMBOL (next)->function), Qautoload))
8302 do_autoload (XSYMBOL (next)->function, next);
8304 /* Handle a symbol whose function definition is a keymap
8305 or an array. */
8306 if (SYMBOLP (next) && !NILP (Ffboundp (next))
8307 && (!NILP (Farrayp (XSYMBOL (next)->function))
8308 || KEYMAPP (XSYMBOL (next)->function)))
8309 next = XSYMBOL (next)->function;
8311 /* If the keymap gives a function, not an
8312 array, then call the function with one arg and use
8313 its value instead. */
8314 if (SYMBOLP (next) && !NILP (Ffboundp (next)) && do_funcall)
8316 Lisp_Object tem;
8317 tem = next;
8319 next = call1 (next, prompt);
8320 /* If the function returned something invalid,
8321 barf--don't ignore it.
8322 (To ignore it safely, we would need to gcpro a bunch of
8323 other variables.) */
8324 if (! (VECTORP (next) || STRINGP (next)))
8325 error ("Function %s returns invalid key sequence", tem);
8327 return next;
8330 /* Do one step of the key remapping used for function-key-map and
8331 key-translation-map:
8332 KEYBUF is the buffer holding the input events.
8333 BUFSIZE is its maximum size.
8334 FKEY is a pointer to the keyremap structure to use.
8335 INPUT is the index of the last element in KEYBUF.
8336 DOIT if non-zero says that the remapping can actually take place.
8337 DIFF is used to return the number of keys added/removed by the remapping.
8338 PARENT is the root of the keymap.
8339 PROMPT is the prompt to use if the remapping happens through a function.
8340 The return value is non-zero if the remapping actually took place. */
8342 static int
8343 keyremap_step (keybuf, bufsize, fkey, input, doit, diff, prompt)
8344 Lisp_Object *keybuf, prompt;
8345 keyremap *fkey;
8346 int input, doit, *diff, bufsize;
8348 Lisp_Object next, key;
8350 key = keybuf[fkey->end++];
8351 next = access_keymap_keyremap (fkey->map, key, prompt, doit);
8353 /* If keybuf[fkey->start..fkey->end] is bound in the
8354 map and we're in a position to do the key remapping, replace it with
8355 the binding and restart with fkey->start at the end. */
8356 if ((VECTORP (next) || STRINGP (next)) && doit)
8358 int len = XFASTINT (Flength (next));
8359 int i;
8361 *diff = len - (fkey->end - fkey->start);
8363 if (input + *diff >= bufsize)
8364 error ("Key sequence too long");
8366 /* Shift the keys that follow fkey->end. */
8367 if (*diff < 0)
8368 for (i = fkey->end; i < input; i++)
8369 keybuf[i + *diff] = keybuf[i];
8370 else if (*diff > 0)
8371 for (i = input - 1; i >= fkey->end; i--)
8372 keybuf[i + *diff] = keybuf[i];
8373 /* Overwrite the old keys with the new ones. */
8374 for (i = 0; i < len; i++)
8375 keybuf[fkey->start + i]
8376 = Faref (next, make_number (i));
8378 fkey->start = fkey->end += *diff;
8379 fkey->map = fkey->parent;
8381 return 1;
8384 fkey->map = get_keymap (next, 0, 1);
8386 /* If we no longer have a bound suffix, try a new position for
8387 fkey->start. */
8388 if (!CONSP (fkey->map))
8390 fkey->end = ++fkey->start;
8391 fkey->map = fkey->parent;
8393 return 0;
8396 /* Read a sequence of keys that ends with a non prefix character,
8397 storing it in KEYBUF, a buffer of size BUFSIZE.
8398 Prompt with PROMPT.
8399 Return the length of the key sequence stored.
8400 Return -1 if the user rejected a command menu.
8402 Echo starting immediately unless `prompt' is 0.
8404 Where a key sequence ends depends on the currently active keymaps.
8405 These include any minor mode keymaps active in the current buffer,
8406 the current buffer's local map, and the global map.
8408 If a key sequence has no other bindings, we check Vfunction_key_map
8409 to see if some trailing subsequence might be the beginning of a
8410 function key's sequence. If so, we try to read the whole function
8411 key, and substitute its symbolic name into the key sequence.
8413 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
8414 `double-' events into similar click events, if that would make them
8415 bound. We try to turn `triple-' events first into `double-' events,
8416 then into clicks.
8418 If we get a mouse click in a mode line, vertical divider, or other
8419 non-text area, we treat the click as if it were prefixed by the
8420 symbol denoting that area - `mode-line', `vertical-line', or
8421 whatever.
8423 If the sequence starts with a mouse click, we read the key sequence
8424 with respect to the buffer clicked on, not the current buffer.
8426 If the user switches frames in the midst of a key sequence, we put
8427 off the switch-frame event until later; the next call to
8428 read_char will return it.
8430 If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
8431 from the selected window's buffer. */
8433 static int
8434 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
8435 can_return_switch_frame, fix_current_buffer)
8436 Lisp_Object *keybuf;
8437 int bufsize;
8438 Lisp_Object prompt;
8439 int dont_downcase_last;
8440 int can_return_switch_frame;
8441 int fix_current_buffer;
8443 volatile Lisp_Object from_string;
8444 volatile int count = SPECPDL_INDEX ();
8446 /* How many keys there are in the current key sequence. */
8447 volatile int t;
8449 /* The length of the echo buffer when we started reading, and
8450 the length of this_command_keys when we started reading. */
8451 volatile int echo_start;
8452 volatile int keys_start;
8454 /* The number of keymaps we're scanning right now, and the number of
8455 keymaps we have allocated space for. */
8456 volatile int nmaps;
8457 volatile int nmaps_allocated = 0;
8459 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
8460 the current keymaps. */
8461 Lisp_Object *volatile defs = NULL;
8463 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
8464 in the current keymaps, or nil where it is not a prefix. */
8465 Lisp_Object *volatile submaps = NULL;
8467 /* The local map to start out with at start of key sequence. */
8468 volatile Lisp_Object orig_local_map;
8470 /* The map from the `keymap' property to start out with at start of
8471 key sequence. */
8472 volatile Lisp_Object orig_keymap;
8474 /* 1 if we have already considered switching to the local-map property
8475 of the place where a mouse click occurred. */
8476 volatile int localized_local_map = 0;
8478 /* The index in defs[] of the first keymap that has a binding for
8479 this key sequence. In other words, the lowest i such that
8480 defs[i] is non-nil. */
8481 volatile int first_binding;
8482 /* Index of the first key that has no binding.
8483 It is useless to try fkey.start larger than that. */
8484 volatile int first_unbound;
8486 /* If t < mock_input, then KEYBUF[t] should be read as the next
8487 input key.
8489 We use this to recover after recognizing a function key. Once we
8490 realize that a suffix of the current key sequence is actually a
8491 function key's escape sequence, we replace the suffix with the
8492 function key's binding from Vfunction_key_map. Now keybuf
8493 contains a new and different key sequence, so the echo area,
8494 this_command_keys, and the submaps and defs arrays are wrong. In
8495 this situation, we set mock_input to t, set t to 0, and jump to
8496 restart_sequence; the loop will read keys from keybuf up until
8497 mock_input, thus rebuilding the state; and then it will resume
8498 reading characters from the keyboard. */
8499 volatile int mock_input = 0;
8501 /* If the sequence is unbound in submaps[], then
8502 keybuf[fkey.start..fkey.end-1] is a prefix in Vfunction_key_map,
8503 and fkey.map is its binding.
8505 These might be > t, indicating that all function key scanning
8506 should hold off until t reaches them. We do this when we've just
8507 recognized a function key, to avoid searching for the function
8508 key's again in Vfunction_key_map. */
8509 volatile keyremap fkey;
8511 /* Likewise, for key_translation_map. */
8512 volatile keyremap keytran;
8514 /* If we receive a `switch-frame' or `select-window' event in the middle of
8515 a key sequence, we put it off for later.
8516 While we're reading, we keep the event here. */
8517 volatile Lisp_Object delayed_switch_frame;
8519 /* See the comment below... */
8520 #if defined (GOBBLE_FIRST_EVENT)
8521 Lisp_Object first_event;
8522 #endif
8524 volatile Lisp_Object original_uppercase;
8525 volatile int original_uppercase_position = -1;
8527 /* Gets around Microsoft compiler limitations. */
8528 int dummyflag = 0;
8530 struct buffer *starting_buffer;
8532 /* List of events for which a fake prefix key has been generated. */
8533 volatile Lisp_Object fake_prefixed_keys = Qnil;
8535 #if defined (GOBBLE_FIRST_EVENT)
8536 int junk;
8537 #endif
8539 struct gcpro gcpro1;
8541 GCPRO1 (fake_prefixed_keys);
8542 raw_keybuf_count = 0;
8544 last_nonmenu_event = Qnil;
8546 delayed_switch_frame = Qnil;
8547 fkey.map = fkey.parent = Vfunction_key_map;
8548 keytran.map = keytran.parent = Vkey_translation_map;
8549 /* If there is no translation-map, turn off scanning. */
8550 fkey.start = fkey.end = KEYMAPP (fkey.map) ? 0 : bufsize + 1;
8551 keytran.start = keytran.end = KEYMAPP (keytran.map) ? 0 : bufsize + 1;
8553 if (INTERACTIVE)
8555 if (!NILP (prompt))
8556 echo_prompt (prompt);
8557 else if (cursor_in_echo_area
8558 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
8559 && NILP (Fzerop (Vecho_keystrokes)))
8560 /* This doesn't put in a dash if the echo buffer is empty, so
8561 you don't always see a dash hanging out in the minibuffer. */
8562 echo_dash ();
8565 /* Record the initial state of the echo area and this_command_keys;
8566 we will need to restore them if we replay a key sequence. */
8567 if (INTERACTIVE)
8568 echo_start = echo_length ();
8569 keys_start = this_command_key_count;
8570 this_single_command_key_start = keys_start;
8572 #if defined (GOBBLE_FIRST_EVENT)
8573 /* This doesn't quite work, because some of the things that read_char
8574 does cannot safely be bypassed. It seems too risky to try to make
8575 this work right. */
8577 /* Read the first char of the sequence specially, before setting
8578 up any keymaps, in case a filter runs and switches buffers on us. */
8579 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
8580 &junk);
8581 #endif /* GOBBLE_FIRST_EVENT */
8583 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8584 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8585 from_string = Qnil;
8587 /* We jump here when the key sequence has been thoroughly changed, and
8588 we need to rescan it starting from the beginning. When we jump here,
8589 keybuf[0..mock_input] holds the sequence we should reread. */
8590 replay_sequence:
8592 starting_buffer = current_buffer;
8593 first_unbound = bufsize + 1;
8595 /* Build our list of keymaps.
8596 If we recognize a function key and replace its escape sequence in
8597 keybuf with its symbol, or if the sequence starts with a mouse
8598 click and we need to switch buffers, we jump back here to rebuild
8599 the initial keymaps from the current buffer. */
8600 nmaps = 0;
8602 if (!NILP (current_kboard->Voverriding_terminal_local_map)
8603 || !NILP (Voverriding_local_map))
8605 if (3 > nmaps_allocated)
8607 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
8608 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
8609 nmaps_allocated = 3;
8611 if (!NILP (current_kboard->Voverriding_terminal_local_map))
8612 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
8613 if (!NILP (Voverriding_local_map))
8614 submaps[nmaps++] = Voverriding_local_map;
8616 else
8618 int nminor;
8619 int total;
8620 Lisp_Object *maps;
8622 nminor = current_minor_maps (0, &maps);
8623 total = nminor + (!NILP (orig_keymap) ? 3 : 2);
8625 if (total > nmaps_allocated)
8627 submaps = (Lisp_Object *) alloca (total * sizeof (submaps[0]));
8628 defs = (Lisp_Object *) alloca (total * sizeof (defs[0]));
8629 nmaps_allocated = total;
8632 if (!NILP (orig_keymap))
8633 submaps[nmaps++] = orig_keymap;
8635 bcopy (maps, (void *) (submaps + nmaps),
8636 nminor * sizeof (submaps[0]));
8638 nmaps += nminor;
8640 submaps[nmaps++] = orig_local_map;
8642 submaps[nmaps++] = current_global_map;
8644 /* Find an accurate initial value for first_binding. */
8645 for (first_binding = 0; first_binding < nmaps; first_binding++)
8646 if (! NILP (submaps[first_binding]))
8647 break;
8649 /* Start from the beginning in keybuf. */
8650 t = 0;
8652 /* These are no-ops the first time through, but if we restart, they
8653 revert the echo area and this_command_keys to their original state. */
8654 this_command_key_count = keys_start;
8655 if (INTERACTIVE && t < mock_input)
8656 echo_truncate (echo_start);
8658 /* If the best binding for the current key sequence is a keymap, or
8659 we may be looking at a function key's escape sequence, keep on
8660 reading. */
8661 while (first_binding < nmaps
8662 /* Keep reading as long as there's a prefix binding. */
8663 ? !NILP (submaps[first_binding])
8664 /* Don't return in the middle of a possible function key sequence,
8665 if the only bindings we found were via case conversion.
8666 Thus, if ESC O a has a function-key-map translation
8667 and ESC o has a binding, don't return after ESC O,
8668 so that we can translate ESC O plus the next character. */
8669 : (fkey.start < t || keytran.start < t))
8671 Lisp_Object key;
8672 int used_mouse_menu = 0;
8674 /* Where the last real key started. If we need to throw away a
8675 key that has expanded into more than one element of keybuf
8676 (say, a mouse click on the mode line which is being treated
8677 as [mode-line (mouse-...)], then we backtrack to this point
8678 of keybuf. */
8679 volatile int last_real_key_start;
8681 /* These variables are analogous to echo_start and keys_start;
8682 while those allow us to restart the entire key sequence,
8683 echo_local_start and keys_local_start allow us to throw away
8684 just one key. */
8685 volatile int echo_local_start, keys_local_start, local_first_binding;
8687 eassert (fkey.end == t || (fkey.end > t && fkey.end <= mock_input));
8688 eassert (fkey.start <= fkey.end);
8689 eassert (keytran.start <= keytran.end);
8690 /* key-translation-map is applied *after* function-key-map. */
8691 eassert (keytran.end <= fkey.start);
8693 if (first_unbound < fkey.start && first_unbound < keytran.start)
8694 { /* The prefix upto first_unbound has no binding and has
8695 no translation left to do either, so we know it's unbound.
8696 If we don't stop now, we risk staying here indefinitely
8697 (if the user keeps entering fkey or keytran prefixes
8698 like C-c ESC ESC ESC ESC ...) */
8699 int i;
8700 for (i = first_unbound + 1; i < t; i++)
8701 keybuf[i - first_unbound - 1] = keybuf[i];
8702 mock_input = t - first_unbound - 1;
8703 fkey.end = fkey.start -= first_unbound + 1;
8704 fkey.map = fkey.parent;
8705 keytran.end = keytran.start -= first_unbound + 1;
8706 keytran.map = keytran.parent;
8707 goto replay_sequence;
8710 if (t >= bufsize)
8711 error ("Key sequence too long");
8713 if (INTERACTIVE)
8714 echo_local_start = echo_length ();
8715 keys_local_start = this_command_key_count;
8716 local_first_binding = first_binding;
8718 replay_key:
8719 /* These are no-ops, unless we throw away a keystroke below and
8720 jumped back up to replay_key; in that case, these restore the
8721 variables to their original state, allowing us to replay the
8722 loop. */
8723 if (INTERACTIVE && t < mock_input)
8724 echo_truncate (echo_local_start);
8725 this_command_key_count = keys_local_start;
8726 first_binding = local_first_binding;
8728 /* By default, assume each event is "real". */
8729 last_real_key_start = t;
8731 /* Does mock_input indicate that we are re-reading a key sequence? */
8732 if (t < mock_input)
8734 key = keybuf[t];
8735 add_command_key (key);
8736 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
8737 && NILP (Fzerop (Vecho_keystrokes)))
8738 echo_char (key);
8741 /* If not, we should actually read a character. */
8742 else
8745 #ifdef MULTI_KBOARD
8746 KBOARD *interrupted_kboard = current_kboard;
8747 struct frame *interrupted_frame = SELECTED_FRAME ();
8748 if (setjmp (wrong_kboard_jmpbuf))
8750 if (!NILP (delayed_switch_frame))
8752 interrupted_kboard->kbd_queue
8753 = Fcons (delayed_switch_frame,
8754 interrupted_kboard->kbd_queue);
8755 delayed_switch_frame = Qnil;
8757 while (t > 0)
8758 interrupted_kboard->kbd_queue
8759 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
8761 /* If the side queue is non-empty, ensure it begins with a
8762 switch-frame, so we'll replay it in the right context. */
8763 if (CONSP (interrupted_kboard->kbd_queue)
8764 && (key = XCAR (interrupted_kboard->kbd_queue),
8765 !(EVENT_HAS_PARAMETERS (key)
8766 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
8767 Qswitch_frame))))
8769 Lisp_Object frame;
8770 XSETFRAME (frame, interrupted_frame);
8771 interrupted_kboard->kbd_queue
8772 = Fcons (make_lispy_switch_frame (frame),
8773 interrupted_kboard->kbd_queue);
8775 mock_input = 0;
8776 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8777 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8778 goto replay_sequence;
8780 #endif
8781 key = read_char (NILP (prompt), nmaps,
8782 (Lisp_Object *) submaps, last_nonmenu_event,
8783 &used_mouse_menu);
8786 /* read_char returns t when it shows a menu and the user rejects it.
8787 Just return -1. */
8788 if (EQ (key, Qt))
8790 unbind_to (count, Qnil);
8791 UNGCPRO;
8792 return -1;
8795 /* read_char returns -1 at the end of a macro.
8796 Emacs 18 handles this by returning immediately with a
8797 zero, so that's what we'll do. */
8798 if (INTEGERP (key) && XINT (key) == -1)
8800 t = 0;
8801 /* The Microsoft C compiler can't handle the goto that
8802 would go here. */
8803 dummyflag = 1;
8804 break;
8807 /* If the current buffer has been changed from under us, the
8808 keymap may have changed, so replay the sequence. */
8809 if (BUFFERP (key))
8811 EMACS_TIME initial_idleness_start_time;
8812 EMACS_SET_SECS_USECS (initial_idleness_start_time,
8813 EMACS_SECS (timer_last_idleness_start_time),
8814 EMACS_USECS (timer_last_idleness_start_time));
8816 /* Resume idle state, using the same start-time as before. */
8817 timer_start_idle ();
8818 timer_idleness_start_time = initial_idleness_start_time;
8820 mock_input = t;
8821 /* Reset the current buffer from the selected window
8822 in case something changed the former and not the latter.
8823 This is to be more consistent with the behavior
8824 of the command_loop_1. */
8825 if (fix_current_buffer)
8827 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8828 Fkill_emacs (Qnil);
8829 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
8830 Fset_buffer (XWINDOW (selected_window)->buffer);
8833 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8834 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8835 goto replay_sequence;
8838 /* If we have a quit that was typed in another frame, and
8839 quit_throw_to_read_char switched buffers,
8840 replay to get the right keymap. */
8841 if (INTEGERP (key)
8842 && XINT (key) == quit_char
8843 && current_buffer != starting_buffer)
8845 GROW_RAW_KEYBUF;
8846 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8847 keybuf[t++] = key;
8848 mock_input = t;
8849 Vquit_flag = Qnil;
8850 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8851 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8852 goto replay_sequence;
8855 Vquit_flag = Qnil;
8857 if (EVENT_HAS_PARAMETERS (key)
8858 /* Either a `switch-frame' or a `select-window' event. */
8859 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), Qswitch_frame))
8861 /* If we're at the beginning of a key sequence, and the caller
8862 says it's okay, go ahead and return this event. If we're
8863 in the midst of a key sequence, delay it until the end. */
8864 if (t > 0 || !can_return_switch_frame)
8866 delayed_switch_frame = key;
8867 goto replay_key;
8871 GROW_RAW_KEYBUF;
8872 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8875 /* Clicks in non-text areas get prefixed by the symbol
8876 in their CHAR-ADDRESS field. For example, a click on
8877 the mode line is prefixed by the symbol `mode-line'.
8879 Furthermore, key sequences beginning with mouse clicks
8880 are read using the keymaps of the buffer clicked on, not
8881 the current buffer. So we may have to switch the buffer
8882 here.
8884 When we turn one event into two events, we must make sure
8885 that neither of the two looks like the original--so that,
8886 if we replay the events, they won't be expanded again.
8887 If not for this, such reexpansion could happen either here
8888 or when user programs play with this-command-keys. */
8889 if (EVENT_HAS_PARAMETERS (key))
8891 Lisp_Object kind;
8892 Lisp_Object string;
8894 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
8895 if (EQ (kind, Qmouse_click))
8897 Lisp_Object window, posn;
8899 window = POSN_WINDOW (EVENT_START (key));
8900 posn = POSN_POSN (EVENT_START (key));
8902 if (CONSP (posn)
8903 || (!NILP (fake_prefixed_keys)
8904 && !NILP (Fmemq (key, fake_prefixed_keys))))
8906 /* We're looking a second time at an event for which
8907 we generated a fake prefix key. Set
8908 last_real_key_start appropriately. */
8909 if (t > 0)
8910 last_real_key_start = t - 1;
8913 /* Key sequences beginning with mouse clicks are
8914 read using the keymaps in the buffer clicked on,
8915 not the current buffer. If we're at the
8916 beginning of a key sequence, switch buffers. */
8917 if (last_real_key_start == 0
8918 && WINDOWP (window)
8919 && BUFFERP (XWINDOW (window)->buffer)
8920 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
8922 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8923 keybuf[t] = key;
8924 mock_input = t + 1;
8926 /* Arrange to go back to the original buffer once we're
8927 done reading the key sequence. Note that we can't
8928 use save_excursion_{save,restore} here, because they
8929 save point as well as the current buffer; we don't
8930 want to save point, because redisplay may change it,
8931 to accommodate a Fset_window_start or something. We
8932 don't want to do this at the top of the function,
8933 because we may get input from a subprocess which
8934 wants to change the selected window and stuff (say,
8935 emacsclient). */
8936 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
8938 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8939 Fkill_emacs (Qnil);
8940 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
8941 orig_local_map = get_local_map (PT, current_buffer,
8942 Qlocal_map);
8943 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8944 goto replay_sequence;
8947 /* For a mouse click, get the local text-property keymap
8948 of the place clicked on, rather than point. */
8949 if (last_real_key_start == 0
8950 && CONSP (XCDR (key))
8951 && ! localized_local_map)
8953 Lisp_Object map_here, start, pos;
8955 localized_local_map = 1;
8956 start = EVENT_START (key);
8958 if (CONSP (start) && POSN_INBUFFER_P (start))
8960 pos = POSN_BUFFER_POSN (start);
8961 if (INTEGERP (pos)
8962 && XINT (pos) >= BEG && XINT (pos) <= Z)
8964 map_here = get_local_map (XINT (pos),
8965 current_buffer, Qlocal_map);
8966 if (!EQ (map_here, orig_local_map))
8968 orig_local_map = map_here;
8969 keybuf[t] = key;
8970 mock_input = t + 1;
8972 goto replay_sequence;
8974 map_here = get_local_map (XINT (pos),
8975 current_buffer, Qkeymap);
8976 if (!EQ (map_here, orig_keymap))
8978 orig_keymap = map_here;
8979 keybuf[t] = key;
8980 mock_input = t + 1;
8982 goto replay_sequence;
8988 /* Expand mode-line and scroll-bar events into two events:
8989 use posn as a fake prefix key. */
8990 if (SYMBOLP (posn)
8991 && (NILP (fake_prefixed_keys)
8992 || NILP (Fmemq (key, fake_prefixed_keys))))
8994 if (t + 1 >= bufsize)
8995 error ("Key sequence too long");
8997 keybuf[t] = posn;
8998 keybuf[t + 1] = key;
8999 mock_input = t + 2;
9001 /* Record that a fake prefix key has been generated
9002 for KEY. Don't modify the event; this would
9003 prevent proper action when the event is pushed
9004 back into unread-command-events. */
9005 fake_prefixed_keys = Fcons (key, fake_prefixed_keys);
9007 /* If on a mode line string with a local keymap,
9008 reconsider the key sequence with that keymap. */
9009 if (string = POSN_STRING (EVENT_START (key)),
9010 (CONSP (string) && STRINGP (XCAR (string))))
9012 Lisp_Object pos, map, map2;
9014 pos = XCDR (string);
9015 string = XCAR (string);
9016 if (XINT (pos) >= 0
9017 && XINT (pos) < SCHARS (string))
9019 map = Fget_text_property (pos, Qlocal_map, string);
9020 if (!NILP (map))
9021 orig_local_map = map;
9022 map2 = Fget_text_property (pos, Qkeymap, string);
9023 if (!NILP (map2))
9024 orig_keymap = map2;
9025 if (!NILP (map) || !NILP (map2))
9026 goto replay_sequence;
9030 goto replay_key;
9032 else if (NILP (from_string)
9033 && (string = POSN_STRING (EVENT_START (key)),
9034 (CONSP (string) && STRINGP (XCAR (string)))))
9036 /* For a click on a string, i.e. overlay string or a
9037 string displayed via the `display' property,
9038 consider `local-map' and `keymap' properties of
9039 that string. */
9040 Lisp_Object pos, map, map2;
9042 pos = XCDR (string);
9043 string = XCAR (string);
9044 if (XINT (pos) >= 0
9045 && XINT (pos) < SCHARS (string))
9047 map = Fget_text_property (pos, Qlocal_map, string);
9048 if (!NILP (map))
9049 orig_local_map = map;
9050 map2 = Fget_text_property (pos, Qkeymap, string);
9051 if (!NILP (map2))
9052 orig_keymap = map2;
9054 if (!NILP (map) || !NILP (map2))
9056 from_string = string;
9057 goto replay_sequence;
9062 else if (CONSP (XCDR (key))
9063 && CONSP (EVENT_START (key))
9064 && CONSP (XCDR (EVENT_START (key))))
9066 Lisp_Object posn;
9068 posn = POSN_POSN (EVENT_START (key));
9069 /* Handle menu-bar events:
9070 insert the dummy prefix event `menu-bar'. */
9071 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
9073 if (t + 1 >= bufsize)
9074 error ("Key sequence too long");
9075 keybuf[t] = posn;
9076 keybuf[t+1] = key;
9078 /* Zap the position in key, so we know that we've
9079 expanded it, and don't try to do so again. */
9080 POSN_SET_POSN (EVENT_START (key),
9081 Fcons (posn, Qnil));
9083 mock_input = t + 2;
9084 goto replay_sequence;
9086 else if (CONSP (posn))
9088 /* We're looking at the second event of a
9089 sequence which we expanded before. Set
9090 last_real_key_start appropriately. */
9091 if (last_real_key_start == t && t > 0)
9092 last_real_key_start = t - 1;
9097 /* We have finally decided that KEY is something we might want
9098 to look up. */
9099 first_binding = (follow_key (key,
9100 nmaps - first_binding,
9101 submaps + first_binding,
9102 defs + first_binding,
9103 submaps + first_binding)
9104 + first_binding);
9106 /* If KEY wasn't bound, we'll try some fallbacks. */
9107 if (first_binding < nmaps)
9108 /* This is needed for the following scenario:
9109 event 0: a down-event that gets dropped by calling replay_key.
9110 event 1: some normal prefix like C-h.
9111 After event 0, first_unbound is 0, after event 1 fkey.start
9112 and keytran.start are both 1, so when we see that C-h is bound,
9113 we need to update first_unbound. */
9114 first_unbound = max (t + 1, first_unbound);
9115 else
9117 Lisp_Object head;
9119 /* Remember the position to put an upper bound on fkey.start. */
9120 first_unbound = min (t, first_unbound);
9122 head = EVENT_HEAD (key);
9123 if (help_char_p (head) && t > 0)
9125 read_key_sequence_cmd = Vprefix_help_command;
9126 keybuf[t++] = key;
9127 last_nonmenu_event = key;
9128 /* The Microsoft C compiler can't handle the goto that
9129 would go here. */
9130 dummyflag = 1;
9131 break;
9134 if (SYMBOLP (head))
9136 Lisp_Object breakdown;
9137 int modifiers;
9139 breakdown = parse_modifiers (head);
9140 modifiers = XINT (XCAR (XCDR (breakdown)));
9141 /* Attempt to reduce an unbound mouse event to a simpler
9142 event that is bound:
9143 Drags reduce to clicks.
9144 Double-clicks reduce to clicks.
9145 Triple-clicks reduce to double-clicks, then to clicks.
9146 Down-clicks are eliminated.
9147 Double-downs reduce to downs, then are eliminated.
9148 Triple-downs reduce to double-downs, then to downs,
9149 then are eliminated. */
9150 if (modifiers & (down_modifier | drag_modifier
9151 | double_modifier | triple_modifier))
9153 while (modifiers & (down_modifier | drag_modifier
9154 | double_modifier | triple_modifier))
9156 Lisp_Object new_head, new_click;
9157 if (modifiers & triple_modifier)
9158 modifiers ^= (double_modifier | triple_modifier);
9159 else if (modifiers & double_modifier)
9160 modifiers &= ~double_modifier;
9161 else if (modifiers & drag_modifier)
9162 modifiers &= ~drag_modifier;
9163 else
9165 /* Dispose of this `down' event by simply jumping
9166 back to replay_key, to get another event.
9168 Note that if this event came from mock input,
9169 then just jumping back to replay_key will just
9170 hand it to us again. So we have to wipe out any
9171 mock input.
9173 We could delete keybuf[t] and shift everything
9174 after that to the left by one spot, but we'd also
9175 have to fix up any variable that points into
9176 keybuf, and shifting isn't really necessary
9177 anyway.
9179 Adding prefixes for non-textual mouse clicks
9180 creates two characters of mock input, and both
9181 must be thrown away. If we're only looking at
9182 the prefix now, we can just jump back to
9183 replay_key. On the other hand, if we've already
9184 processed the prefix, and now the actual click
9185 itself is giving us trouble, then we've lost the
9186 state of the keymaps we want to backtrack to, and
9187 we need to replay the whole sequence to rebuild
9190 Beyond that, only function key expansion could
9191 create more than two keys, but that should never
9192 generate mouse events, so it's okay to zero
9193 mock_input in that case too.
9195 FIXME: The above paragraph seems just plain
9196 wrong, if you consider things like
9197 xterm-mouse-mode. -stef
9199 Isn't this just the most wonderful code ever? */
9201 /* If mock_input > t + 1, the above simplification
9202 will actually end up dropping keys on the floor.
9203 This is probably OK for now, but even
9204 if mock_input <= t + 1, we need to adjust fkey
9205 and keytran.
9206 Typical case [header-line down-mouse-N]:
9207 mock_input = 2, t = 1, fkey.end = 1,
9208 last_real_key_start = 0. */
9209 if (fkey.end > last_real_key_start)
9211 fkey.end = fkey.start
9212 = min (last_real_key_start, fkey.start);
9213 fkey.map = fkey.parent;
9214 if (keytran.end > last_real_key_start)
9216 keytran.end = keytran.start
9217 = min (last_real_key_start, keytran.start);
9218 keytran.map = keytran.parent;
9221 if (t == last_real_key_start)
9223 mock_input = 0;
9224 goto replay_key;
9226 else
9228 mock_input = last_real_key_start;
9229 goto replay_sequence;
9233 new_head
9234 = apply_modifiers (modifiers, XCAR (breakdown));
9235 new_click
9236 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
9238 /* Look for a binding for this new key. follow_key
9239 promises that it didn't munge submaps the
9240 last time we called it, since key was unbound. */
9241 first_binding
9242 = (follow_key (new_click,
9243 nmaps - local_first_binding,
9244 submaps + local_first_binding,
9245 defs + local_first_binding,
9246 submaps + local_first_binding)
9247 + local_first_binding);
9249 /* If that click is bound, go for it. */
9250 if (first_binding < nmaps)
9252 key = new_click;
9253 break;
9255 /* Otherwise, we'll leave key set to the drag event. */
9261 keybuf[t++] = key;
9262 /* Normally, last_nonmenu_event gets the previous key we read.
9263 But when a mouse popup menu is being used,
9264 we don't update last_nonmenu_event; it continues to hold the mouse
9265 event that preceded the first level of menu. */
9266 if (!used_mouse_menu)
9267 last_nonmenu_event = key;
9269 /* Record what part of this_command_keys is the current key sequence. */
9270 this_single_command_key_start = this_command_key_count - t;
9272 if (first_binding < nmaps && NILP (submaps[first_binding]))
9273 /* There is a binding and it's not a prefix.
9274 There is thus no function-key in this sequence.
9275 Moving fkey.start is important in this case to allow keytran.start
9276 to go over the sequence before we return (since we keep the
9277 invariant that keytran.end <= fkey.start). */
9279 if (fkey.start < t)
9280 (fkey.start = fkey.end = t, fkey.map = fkey.parent);
9282 else
9283 /* If the sequence is unbound, see if we can hang a function key
9284 off the end of it. */
9285 /* Continue scan from fkey.end until we find a bound suffix. */
9286 while (fkey.end < t)
9288 struct gcpro gcpro1, gcpro2, gcpro3;
9289 int done, diff;
9291 GCPRO3 (fkey.map, keytran.map, delayed_switch_frame);
9292 done = keyremap_step (keybuf, bufsize, &fkey,
9293 max (t, mock_input),
9294 /* If there's a binding (i.e.
9295 first_binding >= nmaps) we don't want
9296 to apply this function-key-mapping. */
9297 fkey.end + 1 == t && first_binding >= nmaps,
9298 &diff, prompt);
9299 UNGCPRO;
9300 if (done)
9302 mock_input = diff + max (t, mock_input);
9303 goto replay_sequence;
9307 /* Look for this sequence in key-translation-map.
9308 Scan from keytran.end until we find a bound suffix. */
9309 while (keytran.end < fkey.start)
9311 struct gcpro gcpro1, gcpro2, gcpro3;
9312 int done, diff;
9314 GCPRO3 (fkey.map, keytran.map, delayed_switch_frame);
9315 done = keyremap_step (keybuf, bufsize, &keytran, max (t, mock_input),
9316 1, &diff, prompt);
9317 UNGCPRO;
9318 if (done)
9320 mock_input = diff + max (t, mock_input);
9321 /* Adjust the function-key-map counters. */
9322 fkey.end += diff;
9323 fkey.start += diff;
9325 goto replay_sequence;
9329 /* If KEY is not defined in any of the keymaps,
9330 and cannot be part of a function key or translation,
9331 and is an upper case letter
9332 use the corresponding lower-case letter instead. */
9333 if (first_binding >= nmaps
9334 && fkey.start >= t && keytran.start >= t
9335 && INTEGERP (key)
9336 && ((((XINT (key) & 0x3ffff)
9337 < XCHAR_TABLE (current_buffer->downcase_table)->size)
9338 && UPPERCASEP (XINT (key) & 0x3ffff))
9339 || (XINT (key) & shift_modifier)))
9341 Lisp_Object new_key;
9343 original_uppercase = key;
9344 original_uppercase_position = t - 1;
9346 if (XINT (key) & shift_modifier)
9347 XSETINT (new_key, XINT (key) & ~shift_modifier);
9348 else
9349 XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
9350 | (XINT (key) & ~0x3ffff)));
9352 /* We have to do this unconditionally, regardless of whether
9353 the lower-case char is defined in the keymaps, because they
9354 might get translated through function-key-map. */
9355 keybuf[t - 1] = new_key;
9356 mock_input = max (t, mock_input);
9358 goto replay_sequence;
9360 /* If KEY is not defined in any of the keymaps,
9361 and cannot be part of a function key or translation,
9362 and is a shifted function key,
9363 use the corresponding unshifted function key instead. */
9364 if (first_binding >= nmaps
9365 && fkey.start >= t && keytran.start >= t
9366 && SYMBOLP (key))
9368 Lisp_Object breakdown;
9369 int modifiers;
9371 breakdown = parse_modifiers (key);
9372 modifiers = XINT (XCAR (XCDR (breakdown)));
9373 if (modifiers & shift_modifier)
9375 Lisp_Object new_key;
9377 original_uppercase = key;
9378 original_uppercase_position = t - 1;
9380 modifiers &= ~shift_modifier;
9381 new_key = apply_modifiers (modifiers,
9382 XCAR (breakdown));
9384 keybuf[t - 1] = new_key;
9385 mock_input = max (t, mock_input);
9386 fkey.start = fkey.end = KEYMAPP (fkey.map) ? 0 : bufsize + 1;
9387 keytran.start = keytran.end = KEYMAPP (keytran.map) ? 0 : bufsize + 1;
9389 goto replay_sequence;
9394 if (!dummyflag)
9395 read_key_sequence_cmd = (first_binding < nmaps
9396 ? defs[first_binding]
9397 : Qnil);
9399 unread_switch_frame = delayed_switch_frame;
9400 unbind_to (count, Qnil);
9402 /* Don't downcase the last character if the caller says don't.
9403 Don't downcase it if the result is undefined, either. */
9404 if ((dont_downcase_last || first_binding >= nmaps)
9405 && t - 1 == original_uppercase_position)
9406 keybuf[t - 1] = original_uppercase;
9408 /* Occasionally we fabricate events, perhaps by expanding something
9409 according to function-key-map, or by adding a prefix symbol to a
9410 mouse click in the scroll bar or modeline. In this cases, return
9411 the entire generated key sequence, even if we hit an unbound
9412 prefix or a definition before the end. This means that you will
9413 be able to push back the event properly, and also means that
9414 read-key-sequence will always return a logical unit.
9416 Better ideas? */
9417 for (; t < mock_input; t++)
9419 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
9420 && NILP (Fzerop (Vecho_keystrokes)))
9421 echo_char (keybuf[t]);
9422 add_command_key (keybuf[t]);
9427 UNGCPRO;
9428 return t;
9431 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 5, 0,
9432 doc: /* Read a sequence of keystrokes and return as a string or vector.
9433 The sequence is sufficient to specify a non-prefix command in the
9434 current local and global maps.
9436 First arg PROMPT is a prompt string. If nil, do not prompt specially.
9437 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos
9438 as a continuation of the previous key.
9440 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not
9441 convert the last event to lower case. (Normally any upper case event
9442 is converted to lower case if the original event is undefined and the lower
9443 case equivalent is defined.) A non-nil value is appropriate for reading
9444 a key sequence to be defined.
9446 A C-g typed while in this function is treated like any other character,
9447 and `quit-flag' is not set.
9449 If the key sequence starts with a mouse click, then the sequence is read
9450 using the keymaps of the buffer of the window clicked in, not the buffer
9451 of the selected window as normal.
9453 `read-key-sequence' drops unbound button-down events, since you normally
9454 only care about the click or drag events which follow them. If a drag
9455 or multi-click event is unbound, but the corresponding click event would
9456 be bound, `read-key-sequence' turns the event into a click event at the
9457 drag's starting position. This means that you don't have to distinguish
9458 between click and drag, double, or triple events unless you want to.
9460 `read-key-sequence' prefixes mouse events on mode lines, the vertical
9461 lines separating windows, and scroll bars with imaginary keys
9462 `mode-line', `vertical-line', and `vertical-scroll-bar'.
9464 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this
9465 function will process a switch-frame event if the user switches frames
9466 before typing anything. If the user switches frames in the middle of a
9467 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME
9468 is nil, then the event will be put off until after the current key sequence.
9470 `read-key-sequence' checks `function-key-map' for function key
9471 sequences, where they wouldn't conflict with ordinary bindings. See
9472 `function-key-map' for more details.
9474 The optional fifth argument COMMAND-LOOP, if non-nil, means
9475 that this key sequence is being read by something that will
9476 read commands one after another. It should be nil if the caller
9477 will read just one key sequence. */)
9478 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
9479 command_loop)
9480 Lisp_Object prompt, continue_echo, dont_downcase_last;
9481 Lisp_Object can_return_switch_frame, command_loop;
9483 Lisp_Object keybuf[30];
9484 register int i;
9485 struct gcpro gcpro1;
9486 int count = SPECPDL_INDEX ();
9488 if (!NILP (prompt))
9489 CHECK_STRING (prompt);
9490 QUIT;
9492 specbind (Qinput_method_exit_on_first_char,
9493 (NILP (command_loop) ? Qt : Qnil));
9494 specbind (Qinput_method_use_echo_area,
9495 (NILP (command_loop) ? Qt : Qnil));
9497 bzero (keybuf, sizeof keybuf);
9498 GCPRO1 (keybuf[0]);
9499 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
9501 if (NILP (continue_echo))
9503 this_command_key_count = 0;
9504 this_command_key_count_reset = 0;
9505 this_single_command_key_start = 0;
9508 #ifdef HAVE_X_WINDOWS
9509 if (display_hourglass_p)
9510 cancel_hourglass ();
9511 #endif
9513 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
9514 prompt, ! NILP (dont_downcase_last),
9515 ! NILP (can_return_switch_frame), 0);
9517 #if 0 /* The following is fine for code reading a key sequence and
9518 then proceeding with a lenghty computation, but it's not good
9519 for code reading keys in a loop, like an input method. */
9520 #ifdef HAVE_X_WINDOWS
9521 if (display_hourglass_p)
9522 start_hourglass ();
9523 #endif
9524 #endif
9526 if (i == -1)
9528 Vquit_flag = Qt;
9529 QUIT;
9531 UNGCPRO;
9532 return unbind_to (count, make_event_array (i, keybuf));
9535 DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
9536 Sread_key_sequence_vector, 1, 5, 0,
9537 doc: /* Like `read-key-sequence' but always return a vector. */)
9538 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
9539 command_loop)
9540 Lisp_Object prompt, continue_echo, dont_downcase_last;
9541 Lisp_Object can_return_switch_frame, command_loop;
9543 Lisp_Object keybuf[30];
9544 register int i;
9545 struct gcpro gcpro1;
9546 int count = SPECPDL_INDEX ();
9548 if (!NILP (prompt))
9549 CHECK_STRING (prompt);
9550 QUIT;
9552 specbind (Qinput_method_exit_on_first_char,
9553 (NILP (command_loop) ? Qt : Qnil));
9554 specbind (Qinput_method_use_echo_area,
9555 (NILP (command_loop) ? Qt : Qnil));
9557 bzero (keybuf, sizeof keybuf);
9558 GCPRO1 (keybuf[0]);
9559 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
9561 if (NILP (continue_echo))
9563 this_command_key_count = 0;
9564 this_command_key_count_reset = 0;
9565 this_single_command_key_start = 0;
9568 #ifdef HAVE_X_WINDOWS
9569 if (display_hourglass_p)
9570 cancel_hourglass ();
9571 #endif
9573 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
9574 prompt, ! NILP (dont_downcase_last),
9575 ! NILP (can_return_switch_frame), 0);
9577 #ifdef HAVE_X_WINDOWS
9578 if (display_hourglass_p)
9579 start_hourglass ();
9580 #endif
9582 if (i == -1)
9584 Vquit_flag = Qt;
9585 QUIT;
9587 UNGCPRO;
9588 return unbind_to (count, Fvector (i, keybuf));
9591 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
9592 doc: /* Execute CMD as an editor command.
9593 CMD must be a symbol that satisfies the `commandp' predicate.
9594 Optional second arg RECORD-FLAG non-nil
9595 means unconditionally put this command in `command-history'.
9596 Otherwise, that is done only if an arg is read using the minibuffer.
9597 The argument KEYS specifies the value to use instead of (this-command-keys)
9598 when reading the arguments; if it is nil, (this-command-keys) is used.
9599 The argument SPECIAL, if non-nil, means that this command is executing
9600 a special event, so ignore the prefix argument and don't clear it. */)
9601 (cmd, record_flag, keys, special)
9602 Lisp_Object cmd, record_flag, keys, special;
9604 register Lisp_Object final;
9605 register Lisp_Object tem;
9606 Lisp_Object prefixarg;
9607 struct backtrace backtrace;
9608 extern int debug_on_next_call;
9610 debug_on_next_call = 0;
9612 if (NILP (special))
9614 prefixarg = current_kboard->Vprefix_arg;
9615 Vcurrent_prefix_arg = prefixarg;
9616 current_kboard->Vprefix_arg = Qnil;
9618 else
9619 prefixarg = Qnil;
9621 if (SYMBOLP (cmd))
9623 tem = Fget (cmd, Qdisabled);
9624 if (!NILP (tem) && !NILP (Vrun_hooks))
9626 tem = Fsymbol_value (Qdisabled_command_hook);
9627 if (!NILP (tem))
9628 return call1 (Vrun_hooks, Qdisabled_command_hook);
9632 while (1)
9634 final = Findirect_function (cmd);
9636 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
9638 struct gcpro gcpro1, gcpro2;
9640 GCPRO2 (cmd, prefixarg);
9641 do_autoload (final, cmd);
9642 UNGCPRO;
9644 else
9645 break;
9648 if (STRINGP (final) || VECTORP (final))
9650 /* If requested, place the macro in the command history. For
9651 other sorts of commands, call-interactively takes care of
9652 this. */
9653 if (!NILP (record_flag))
9655 Vcommand_history
9656 = Fcons (Fcons (Qexecute_kbd_macro,
9657 Fcons (final, Fcons (prefixarg, Qnil))),
9658 Vcommand_history);
9660 /* Don't keep command history around forever. */
9661 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
9663 tem = Fnthcdr (Vhistory_length, Vcommand_history);
9664 if (CONSP (tem))
9665 XSETCDR (tem, Qnil);
9669 return Fexecute_kbd_macro (final, prefixarg, Qnil);
9672 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
9674 backtrace.next = backtrace_list;
9675 backtrace_list = &backtrace;
9676 backtrace.function = &Qcall_interactively;
9677 backtrace.args = &cmd;
9678 backtrace.nargs = 1;
9679 backtrace.evalargs = 0;
9680 backtrace.debug_on_exit = 0;
9682 tem = Fcall_interactively (cmd, record_flag, keys);
9684 backtrace_list = backtrace.next;
9685 return tem;
9687 return Qnil;
9692 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
9693 1, 1, "P",
9694 doc: /* Read function name, then read its arguments and call it. */)
9695 (prefixarg)
9696 Lisp_Object prefixarg;
9698 Lisp_Object function;
9699 char buf[40];
9700 int saved_last_point_position;
9701 Lisp_Object saved_keys, saved_last_point_position_buffer;
9702 Lisp_Object bindings, value;
9703 struct gcpro gcpro1, gcpro2, gcpro3;
9705 saved_keys = Fvector (this_command_key_count,
9706 XVECTOR (this_command_keys)->contents);
9707 saved_last_point_position_buffer = last_point_position_buffer;
9708 saved_last_point_position = last_point_position;
9709 buf[0] = 0;
9710 GCPRO3 (saved_keys, prefixarg, saved_last_point_position_buffer);
9712 if (EQ (prefixarg, Qminus))
9713 strcpy (buf, "- ");
9714 else if (CONSP (prefixarg) && XINT (XCAR (prefixarg)) == 4)
9715 strcpy (buf, "C-u ");
9716 else if (CONSP (prefixarg) && INTEGERP (XCAR (prefixarg)))
9718 if (sizeof (int) == sizeof (EMACS_INT))
9719 sprintf (buf, "%d ", XINT (XCAR (prefixarg)));
9720 else if (sizeof (long) == sizeof (EMACS_INT))
9721 sprintf (buf, "%ld ", (long) XINT (XCAR (prefixarg)));
9722 else
9723 abort ();
9725 else if (INTEGERP (prefixarg))
9727 if (sizeof (int) == sizeof (EMACS_INT))
9728 sprintf (buf, "%d ", XINT (prefixarg));
9729 else if (sizeof (long) == sizeof (EMACS_INT))
9730 sprintf (buf, "%ld ", (long) XINT (prefixarg));
9731 else
9732 abort ();
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 Each character is looked up in this string and the contents used instead.
11155 The value may be a string, a vector, or a char-table.
11156 If it is a string or vector of length N,
11157 character codes N and up are untranslated.
11158 In a vector or a char-table, an element which is nil means "no translation".
11160 This is applied to the characters supplied to input methods, not their
11161 output. See also `translation-table-for-input'. */);
11162 Vkeyboard_translate_table = Qnil;
11164 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
11165 doc: /* Non-nil means to always spawn a subshell instead of suspending.
11166 \(Even if the operating system has support for stopping a process.\) */);
11167 cannot_suspend = 0;
11169 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
11170 doc: /* Non-nil means prompt with menus when appropriate.
11171 This is done when reading from a keymap that has a prompt string,
11172 for elements that have prompt strings.
11173 The menu is displayed on the screen
11174 if X menus were enabled at configuration
11175 time and the previous event was a mouse click prefix key.
11176 Otherwise, menu prompting uses the echo area. */);
11177 menu_prompting = 1;
11179 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
11180 doc: /* Character to see next line of menu prompt.
11181 Type this character while in a menu prompt to rotate around the lines of it. */);
11182 XSETINT (menu_prompt_more_char, ' ');
11184 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
11185 doc: /* A mask of additional modifier keys to use with every keyboard character.
11186 Emacs applies the modifiers of the character stored here to each keyboard
11187 character it reads. For example, after evaluating the expression
11188 (setq extra-keyboard-modifiers ?\\C-x)
11189 all input characters will have the control modifier applied to them.
11191 Note that the character ?\\C-@, equivalent to the integer zero, does
11192 not count as a control character; rather, it counts as a character
11193 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero
11194 cancels any modification. */);
11195 extra_keyboard_modifiers = 0;
11197 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
11198 doc: /* If an editing command sets this to t, deactivate the mark afterward.
11199 The command loop sets this to nil before each command,
11200 and tests the value when the command returns.
11201 Buffer modification stores t in this variable. */);
11202 Vdeactivate_mark = Qnil;
11204 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
11205 doc: /* Temporary storage of pre-command-hook or post-command-hook. */);
11206 Vcommand_hook_internal = Qnil;
11208 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
11209 doc: /* Normal hook run before each command is executed.
11210 If an unhandled error happens in running this hook,
11211 the hook value is set to nil, since otherwise the error
11212 might happen repeatedly and make Emacs nonfunctional. */);
11213 Vpre_command_hook = Qnil;
11215 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
11216 doc: /* Normal hook run after each command is executed.
11217 If an unhandled error happens in running this hook,
11218 the hook value is set to nil, since otherwise the error
11219 might happen repeatedly and make Emacs nonfunctional. */);
11220 Vpost_command_hook = Qnil;
11222 DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
11223 doc: /* Normal hook run after each command is executed, if idle.
11224 Errors running the hook are caught and ignored. */);
11225 Vpost_command_idle_hook = Qnil;
11227 DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
11228 doc: /* Delay time before running `post-command-idle-hook'.
11229 This is measured in microseconds. */);
11230 post_command_idle_delay = 100000;
11232 #if 0
11233 DEFVAR_LISP ("echo-area-clear-hook", ...,
11234 doc: /* Normal hook run when clearing the echo area. */);
11235 #endif
11236 Qecho_area_clear_hook = intern ("echo-area-clear-hook");
11237 SET_SYMBOL_VALUE (Qecho_area_clear_hook, Qnil);
11239 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
11240 doc: /* Non-nil means menu bar, specified Lucid style, needs to be recomputed. */);
11241 Vlucid_menu_bar_dirty_flag = Qnil;
11243 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
11244 doc: /* List of menu bar items to move to the end of the menu bar.
11245 The elements of the list are event types that may have menu bar bindings. */);
11246 Vmenu_bar_final_items = Qnil;
11248 DEFVAR_KBOARD ("overriding-terminal-local-map",
11249 Voverriding_terminal_local_map,
11250 doc: /* Per-terminal keymap that overrides all other local keymaps.
11251 If this variable is non-nil, it is used as a keymap instead of the
11252 buffer's local map, and the minor mode keymaps and text property keymaps.
11253 This variable is intended to let commands such as `universal-argument'
11254 set up a different keymap for reading the next command. */);
11256 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
11257 doc: /* Keymap that overrides all other local keymaps.
11258 If this variable is non-nil, it is used as a keymap instead of the
11259 buffer's local map, and the minor mode keymaps and text property keymaps. */);
11260 Voverriding_local_map = Qnil;
11262 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
11263 doc: /* Non-nil means `overriding-local-map' applies to the menu bar.
11264 Otherwise, the menu bar continues to reflect the buffer's local map
11265 and the minor mode maps regardless of `overriding-local-map'. */);
11266 Voverriding_local_map_menu_flag = Qnil;
11268 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
11269 doc: /* Keymap defining bindings for special events to execute at low level. */);
11270 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
11272 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
11273 doc: /* *Non-nil means generate motion events for mouse motion. */);
11275 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
11276 doc: /* Alist of system-specific X windows key symbols.
11277 Each element should have the form (N . SYMBOL) where N is the
11278 numeric keysym code (sans the \"system-specific\" bit 1<<28)
11279 and SYMBOL is its name. */);
11281 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
11282 doc: /* List of deferred actions to be performed at a later time.
11283 The precise format isn't relevant here; we just check whether it is nil. */);
11284 Vdeferred_action_list = Qnil;
11286 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
11287 doc: /* Function to call to handle deferred actions, after each command.
11288 This function is called with no arguments after each command
11289 whenever `deferred-action-list' is non-nil. */);
11290 Vdeferred_action_function = Qnil;
11292 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
11293 doc: /* *Non-nil means show the equivalent key-binding when M-x command has one.
11294 The value can be a length of time to show the message for.
11295 If the value is non-nil and not a number, we wait 2 seconds. */);
11296 Vsuggest_key_bindings = Qt;
11298 DEFVAR_LISP ("timer-list", &Vtimer_list,
11299 doc: /* List of active absolute time timers in order of increasing time. */);
11300 Vtimer_list = Qnil;
11302 DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
11303 doc: /* List of active idle-time timers in order of increasing time. */);
11304 Vtimer_idle_list = Qnil;
11306 DEFVAR_LISP ("input-method-function", &Vinput_method_function,
11307 doc: /* If non-nil, the function that implements the current input method.
11308 It's called with one argument, a printing character that was just read.
11309 \(That means a character with code 040...0176.)
11310 Typically this function uses `read-event' to read additional events.
11311 When it does so, it should first bind `input-method-function' to nil
11312 so it will not be called recursively.
11314 The function should return a list of zero or more events
11315 to be used as input. If it wants to put back some events
11316 to be reconsidered, separately, by the input method,
11317 it can add them to the beginning of `unread-command-events'.
11319 The input method function can find in `input-method-previous-method'
11320 the previous echo area message.
11322 The input method function should refer to the variables
11323 `input-method-use-echo-area' and `input-method-exit-on-first-char'
11324 for guidance on what to do. */);
11325 Vinput_method_function = Qnil;
11327 DEFVAR_LISP ("input-method-previous-message",
11328 &Vinput_method_previous_message,
11329 doc: /* When `input-method-function' is called, hold the previous echo area message.
11330 This variable exists because `read-event' clears the echo area
11331 before running the input method. It is nil if there was no message. */);
11332 Vinput_method_previous_message = Qnil;
11334 DEFVAR_LISP ("show-help-function", &Vshow_help_function,
11335 doc: /* If non-nil, the function that implements the display of help.
11336 It's called with one argument, the help string to display. */);
11337 Vshow_help_function = Qnil;
11339 DEFVAR_LISP ("disable-point-adjustment", &Vdisable_point_adjustment,
11340 doc: /* If non-nil, suppress point adjustment after executing a command.
11342 After a command is executed, if point is moved into a region that has
11343 special properties (e.g. composition, display), we adjust point to
11344 the boundary of the region. But, several special commands sets this
11345 variable to non-nil, then we suppress the point adjustment.
11347 This variable is set to nil before reading a command, and is checked
11348 just after executing the command. */);
11349 Vdisable_point_adjustment = Qnil;
11351 DEFVAR_LISP ("global-disable-point-adjustment",
11352 &Vglobal_disable_point_adjustment,
11353 doc: /* *If non-nil, always suppress point adjustment.
11355 The default value is nil, in which case, point adjustment are
11356 suppressed only after special commands that set
11357 `disable-point-adjustment' (which see) to non-nil. */);
11358 Vglobal_disable_point_adjustment = Qnil;
11360 DEFVAR_BOOL ("update-menu-bindings", &update_menu_bindings,
11361 doc: /* Non-nil means updating menu bindings is allowed.
11362 A value of nil means menu bindings should not be updated.
11363 Used during Emacs' startup. */);
11364 update_menu_bindings = 1;
11366 DEFVAR_LISP ("minibuffer-message-timeout", &Vminibuffer_message_timeout,
11367 doc: /* *How long to display an echo-area message when the minibuffer is active.
11368 If the value is not a number, such messages don't time out. */);
11369 Vminibuffer_message_timeout = make_number (2);
11372 void
11373 keys_of_keyboard ()
11375 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
11376 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
11377 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
11378 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
11379 initial_define_key (meta_map, 'x', "execute-extended-command");
11381 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
11382 "handle-delete-frame");
11383 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
11384 "ignore-event");
11385 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
11386 "ignore-event");
11387 /* Handling it at such a low-level causes read_key_sequence to get
11388 * confused because it doesn't realize that the current_buffer was
11389 * changed by read_char.
11391 * initial_define_lispy_key (Vspecial_event_map, "select-window",
11392 * "handle-select-window"); */
11393 initial_define_lispy_key (Vspecial_event_map, "save-session",
11394 "handle-save-session");
11397 /* Mark the pointers in the kboard objects.
11398 Called by the Fgarbage_collector. */
11399 void
11400 mark_kboards ()
11402 KBOARD *kb;
11403 Lisp_Object *p;
11404 for (kb = all_kboards; kb; kb = kb->next_kboard)
11406 if (kb->kbd_macro_buffer)
11407 for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++)
11408 mark_object (*p);
11409 mark_object (kb->Voverriding_terminal_local_map);
11410 mark_object (kb->Vlast_command);
11411 mark_object (kb->Vreal_last_command);
11412 mark_object (kb->Vprefix_arg);
11413 mark_object (kb->Vlast_prefix_arg);
11414 mark_object (kb->kbd_queue);
11415 mark_object (kb->defining_kbd_macro);
11416 mark_object (kb->Vlast_kbd_macro);
11417 mark_object (kb->Vsystem_key_alist);
11418 mark_object (kb->system_key_syms);
11419 mark_object (kb->Vdefault_minibuffer_frame);
11420 mark_object (kb->echo_string);
11423 struct input_event *event;
11424 for (event = kbd_fetch_ptr; event != kbd_store_ptr; event++)
11426 if (event == kbd_buffer + KBD_BUFFER_SIZE)
11427 event = kbd_buffer;
11428 if (event->kind != SELECTION_REQUEST_EVENT)
11430 mark_object (event->x);
11431 mark_object (event->y);
11433 mark_object (event->frame_or_window);
11434 mark_object (event->arg);
11439 /* arch-tag: 774e34d7-6d31-42f3-8397-e079a4e4c9ca
11440 (do not change this comment) */