*** empty log message ***
[emacs.git] / src / keyboard.c
blobd54dcd22bc4f00517ff8216bd62cdd63996caa01
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
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;
122 #ifdef MULTI_KBOARD
123 KBOARD *initial_kboard;
124 KBOARD *current_kboard;
125 KBOARD *all_kboards;
126 int single_kboard;
127 #else
128 KBOARD the_only_kboard;
129 #endif
131 /* Non-nil disable property on a command means
132 do not execute it; call disabled-command-hook's value instead. */
133 Lisp_Object Qdisabled, Qdisabled_command_hook;
135 #define NUM_RECENT_KEYS (100)
136 int recent_keys_index; /* Index for storing next element into recent_keys */
137 int total_keys; /* Total number of elements stored into recent_keys */
138 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
140 /* Vector holding the key sequence that invoked the current command.
141 It is reused for each command, and it may be longer than the current
142 sequence; this_command_key_count indicates how many elements
143 actually mean something.
144 It's easier to staticpro a single Lisp_Object than an array. */
145 Lisp_Object this_command_keys;
146 int this_command_key_count;
148 /* This vector is used as a buffer to record the events that were actually read
149 by read_key_sequence. */
150 Lisp_Object raw_keybuf;
151 int raw_keybuf_count;
153 #define GROW_RAW_KEYBUF \
154 if (raw_keybuf_count == XVECTOR (raw_keybuf)->size) \
156 int newsize = 2 * XVECTOR (raw_keybuf)->size; \
157 Lisp_Object new; \
158 new = Fmake_vector (make_number (newsize), Qnil); \
159 bcopy (XVECTOR (raw_keybuf)->contents, XVECTOR (new)->contents, \
160 raw_keybuf_count * sizeof (Lisp_Object)); \
161 raw_keybuf = new; \
164 /* Number of elements of this_command_keys
165 that precede this key sequence. */
166 int this_single_command_key_start;
168 /* Record values of this_command_key_count and echo_length ()
169 before this command was read. */
170 static int before_command_key_count;
171 static int before_command_echo_length;
172 /* Values of before_command_key_count and before_command_echo_length
173 saved by reset-this-command-lengths. */
174 static int before_command_key_count_1;
175 static int before_command_echo_length_1;
176 /* Flag set by reset-this-command-lengths,
177 saying to reset the lengths when add_command_key is called. */
178 static int before_command_restore_flag;
180 extern int minbuf_level;
182 extern int message_enable_multibyte;
184 extern struct backtrace *backtrace_list;
186 /* If non-nil, the function that implements the display of help.
187 It's called with one argument, the help string to display. */
189 Lisp_Object Vshow_help_function;
191 /* If a string, the message displayed before displaying a help-echo
192 in the echo area. */
194 Lisp_Object Vpre_help_message;
196 /* Nonzero means do menu prompting. */
198 static int menu_prompting;
200 /* Character to see next line of menu prompt. */
202 static Lisp_Object menu_prompt_more_char;
204 /* For longjmp to where kbd input is being done. */
206 static jmp_buf getcjmp;
208 /* True while doing kbd input. */
209 int waiting_for_input;
211 /* True while displaying for echoing. Delays C-g throwing. */
213 int echoing;
215 /* Non-null means we can start echoing at the next input pause even
216 though there is something in the echo area. */
218 static struct kboard *ok_to_echo_at_next_pause;
220 /* The kboard last echoing, or null for none. Reset to 0 in
221 cancel_echoing. If non-null, and a current echo area message
222 exists, and echo_message_buffer is eq to the current message
223 buffer, we know that the message comes from echo_kboard. */
225 struct kboard *echo_kboard;
227 /* The buffer used for echoing. Set in echo_now, reset in
228 cancel_echoing. */
230 Lisp_Object echo_message_buffer;
232 /* Nonzero means disregard local maps for the menu bar. */
233 static int inhibit_local_menu_bar_menus;
235 /* Nonzero means C-g should cause immediate error-signal. */
236 int immediate_quit;
238 /* The user's ERASE setting. */
239 Lisp_Object Vtty_erase_char;
241 /* Character to recognize as the help char. */
242 Lisp_Object Vhelp_char;
244 /* List of other event types to recognize as meaning "help". */
245 Lisp_Object Vhelp_event_list;
247 /* Form to execute when help char is typed. */
248 Lisp_Object Vhelp_form;
250 /* Command to run when the help character follows a prefix key. */
251 Lisp_Object Vprefix_help_command;
253 /* List of items that should move to the end of the menu bar. */
254 Lisp_Object Vmenu_bar_final_items;
256 /* Non-nil means show the equivalent key-binding for
257 any M-x command that has one.
258 The value can be a length of time to show the message for.
259 If the value is non-nil and not a number, we wait 2 seconds. */
260 Lisp_Object Vsuggest_key_bindings;
262 /* How long to display an echo-area message when the minibuffer is active.
263 If the value is not a number, such messages don't time out. */
264 Lisp_Object Vminibuffer_message_timeout;
266 /* Character that causes a quit. Normally C-g.
268 If we are running on an ordinary terminal, this must be an ordinary
269 ASCII char, since we want to make it our interrupt character.
271 If we are not running on an ordinary terminal, it still needs to be
272 an ordinary ASCII char. This character needs to be recognized in
273 the input interrupt handler. At this point, the keystroke is
274 represented as a struct input_event, while the desired quit
275 character is specified as a lispy event. The mapping from struct
276 input_events to lispy events cannot run in an interrupt handler,
277 and the reverse mapping is difficult for anything but ASCII
278 keystrokes.
280 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
281 ASCII character. */
282 int quit_char;
284 extern Lisp_Object current_global_map;
285 extern int minibuf_level;
287 /* If non-nil, this is a map that overrides all other local maps. */
288 Lisp_Object Voverriding_local_map;
290 /* If non-nil, Voverriding_local_map applies to the menu bar. */
291 Lisp_Object Voverriding_local_map_menu_flag;
293 /* Keymap that defines special misc events that should
294 be processed immediately at a low level. */
295 Lisp_Object Vspecial_event_map;
297 /* Current depth in recursive edits. */
298 int command_loop_level;
300 /* Total number of times command_loop has read a key sequence. */
301 EMACS_INT num_input_keys;
303 /* Last input character read as a command. */
304 Lisp_Object last_command_char;
306 /* Last input character read as a command, not counting menus
307 reached by the mouse. */
308 Lisp_Object last_nonmenu_event;
310 /* Last input character read for any purpose. */
311 Lisp_Object last_input_char;
313 /* If not Qnil, a list of objects to be read as subsequent command input. */
314 Lisp_Object Vunread_command_events;
316 /* If not Qnil, a list of objects to be read as subsequent command input
317 including input method processing. */
318 Lisp_Object Vunread_input_method_events;
320 /* If not Qnil, a list of objects to be read as subsequent command input
321 but NOT including input method processing. */
322 Lisp_Object Vunread_post_input_method_events;
324 /* If not -1, an event to be read as subsequent command input. */
325 EMACS_INT unread_command_char;
327 /* If not Qnil, this is a switch-frame event which we decided to put
328 off until the end of a key sequence. This should be read as the
329 next command input, after any unread_command_events.
331 read_key_sequence uses this to delay switch-frame events until the
332 end of the key sequence; Fread_char uses it to put off switch-frame
333 events until a non-ASCII event is acceptable as input. */
334 Lisp_Object unread_switch_frame;
336 /* A mask of extra modifier bits to put into every keyboard char. */
337 EMACS_INT extra_keyboard_modifiers;
339 /* Char to use as prefix when a meta character is typed in.
340 This is bound on entry to minibuffer in case ESC is changed there. */
342 Lisp_Object meta_prefix_char;
344 /* Last size recorded for a current buffer which is not a minibuffer. */
345 static int last_non_minibuf_size;
347 /* Number of idle seconds before an auto-save and garbage collection. */
348 static Lisp_Object Vauto_save_timeout;
350 /* Total number of times read_char has returned. */
351 int num_input_events;
353 /* Total number of times read_char has returned, outside of macros. */
354 EMACS_INT num_nonmacro_input_events;
356 /* Auto-save automatically when this many characters have been typed
357 since the last time. */
359 static EMACS_INT auto_save_interval;
361 /* Value of num_nonmacro_input_events as of last auto save. */
363 int last_auto_save;
365 /* The command being executed by the command loop.
366 Commands may set this, and the value set will be copied into
367 current_kboard->Vlast_command instead of the actual command. */
368 Lisp_Object Vthis_command;
370 /* This is like Vthis_command, except that commands never set it. */
371 Lisp_Object real_this_command;
373 /* If the lookup of the command returns a binding, the original
374 command is stored in this-original-command. It is nil otherwise. */
375 Lisp_Object Vthis_original_command;
377 /* The value of point when the last command was executed. */
378 int last_point_position;
380 /* The buffer that was current when the last command was started. */
381 Lisp_Object last_point_position_buffer;
383 /* The frame in which the last input event occurred, or Qmacro if the
384 last event came from a macro. We use this to determine when to
385 generate switch-frame events. This may be cleared by functions
386 like Fselect_frame, to make sure that a switch-frame event is
387 generated by the next character. */
388 Lisp_Object internal_last_event_frame;
390 /* A user-visible version of the above, intended to allow users to
391 figure out where the last event came from, if the event doesn't
392 carry that information itself (i.e. if it was a character). */
393 Lisp_Object Vlast_event_frame;
395 /* The timestamp of the last input event we received from the X server.
396 X Windows wants this for selection ownership. */
397 unsigned long last_event_timestamp;
399 Lisp_Object Qself_insert_command;
400 Lisp_Object Qforward_char;
401 Lisp_Object Qbackward_char;
402 Lisp_Object Qundefined;
403 Lisp_Object Qtimer_event_handler;
405 /* read_key_sequence stores here the command definition of the
406 key sequence that it reads. */
407 Lisp_Object read_key_sequence_cmd;
409 /* Echo unfinished commands after this many seconds of pause. */
410 Lisp_Object Vecho_keystrokes;
412 /* Form to evaluate (if non-nil) when Emacs is started. */
413 Lisp_Object Vtop_level;
415 /* User-supplied table to translate input characters. */
416 Lisp_Object Vkeyboard_translate_table;
418 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
419 extern Lisp_Object Vfunction_key_map;
421 /* Another keymap that maps key sequences into key sequences.
422 This one takes precedence over ordinary definitions. */
423 extern Lisp_Object Vkey_translation_map;
425 /* If non-nil, this implements the current input method. */
426 Lisp_Object Vinput_method_function;
427 Lisp_Object Qinput_method_function;
429 /* When we call Vinput_method_function,
430 this holds the echo area message that was just erased. */
431 Lisp_Object Vinput_method_previous_message;
433 /* Non-nil means deactivate the mark at end of this command. */
434 Lisp_Object Vdeactivate_mark;
436 /* Menu bar specified in Lucid Emacs fashion. */
438 Lisp_Object Vlucid_menu_bar_dirty_flag;
439 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
441 Lisp_Object Qecho_area_clear_hook;
443 /* Hooks to run before and after each command. */
444 Lisp_Object Qpre_command_hook, Vpre_command_hook;
445 Lisp_Object Qpost_command_hook, Vpost_command_hook;
446 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
447 /* Hook run after a command if there's no more input soon. */
448 Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook;
450 /* Delay time in microseconds before running post-command-idle-hook. */
451 EMACS_INT post_command_idle_delay;
453 /* List of deferred actions to be performed at a later time.
454 The precise format isn't relevant here; we just check whether it is nil. */
455 Lisp_Object Vdeferred_action_list;
457 /* Function to call to handle deferred actions, when there are any. */
458 Lisp_Object Vdeferred_action_function;
459 Lisp_Object Qdeferred_action_function;
461 Lisp_Object Qinput_method_exit_on_first_char;
462 Lisp_Object Qinput_method_use_echo_area;
464 /* File in which we write all commands we read. */
465 FILE *dribble;
467 /* Nonzero if input is available. */
468 int input_pending;
470 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
471 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
473 int meta_key;
475 /* Non-zero means force key bindings update in parse_menu_item. */
477 int update_menu_bindings;
479 extern char *pending_malloc_warning;
481 /* Circular buffer for pre-read keyboard input. */
483 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
485 /* Vector to GCPRO the Lisp objects referenced from kbd_buffer.
487 The interrupt-level event handlers will never enqueue an event on a
488 frame which is not in Vframe_list, and once an event is dequeued,
489 internal_last_event_frame or the event itself points to the frame.
490 So that's all fine.
492 But while the event is sitting in the queue, it's completely
493 unprotected. Suppose the user types one command which will run for
494 a while and then delete a frame, and then types another event at
495 the frame that will be deleted, before the command gets around to
496 it. Suppose there are no references to this frame elsewhere in
497 Emacs, and a GC occurs before the second event is dequeued. Now we
498 have an event referring to a freed frame, which will crash Emacs
499 when it is dequeued.
501 Similar things happen when an event on a scroll bar is enqueued; the
502 window may be deleted while the event is in the queue.
504 So, we use this vector to protect the Lisp_Objects in the event
505 queue. That way, they'll be dequeued as dead frames or windows,
506 but still valid Lisp objects.
508 If kbd_buffer[i].kind != NO_EVENT, then
510 AREF (kbd_buffer_gcpro, 2 * i) == kbd_buffer[i].frame_or_window.
511 AREF (kbd_buffer_gcpro, 2 * i + 1) == kbd_buffer[i].arg. */
513 static Lisp_Object kbd_buffer_gcpro;
515 /* Pointer to next available character in kbd_buffer.
516 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
517 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the
518 next available char is in kbd_buffer[0]. */
519 static struct input_event *kbd_fetch_ptr;
521 /* Pointer to next place to store character in kbd_buffer. This
522 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
523 character should go in kbd_buffer[0]. */
524 static struct input_event * volatile kbd_store_ptr;
526 /* The above pair of variables forms a "queue empty" flag. When we
527 enqueue a non-hook event, we increment kbd_store_ptr. When we
528 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
529 there is input available iff the two pointers are not equal.
531 Why not just have a flag set and cleared by the enqueuing and
532 dequeuing functions? Such a flag could be screwed up by interrupts
533 at inopportune times. */
535 /* If this flag is non-nil, we check mouse_moved to see when the
536 mouse moves, and motion events will appear in the input stream.
537 Otherwise, mouse motion is ignored. */
538 Lisp_Object do_mouse_tracking;
540 /* Symbols to head events. */
541 Lisp_Object Qmouse_movement;
542 Lisp_Object Qscroll_bar_movement;
543 Lisp_Object Qswitch_frame;
544 Lisp_Object Qdelete_frame;
545 Lisp_Object Qiconify_frame;
546 Lisp_Object Qmake_frame_visible;
547 Lisp_Object Qselect_window;
548 Lisp_Object Qhelp_echo;
550 /* Symbols to denote kinds of events. */
551 Lisp_Object Qfunction_key;
552 Lisp_Object Qmouse_click;
553 #if defined(WINDOWSNT) || defined(MAC_OSX)
554 Lisp_Object Qmouse_wheel;
555 #endif
556 #ifdef WINDOWSNT
557 Lisp_Object Qlanguage_change;
558 #endif
559 Lisp_Object Qdrag_n_drop;
560 Lisp_Object Qsave_session;
562 /* Lisp_Object Qmouse_movement; - also an event header */
564 /* Properties of event headers. */
565 Lisp_Object Qevent_kind;
566 Lisp_Object Qevent_symbol_elements;
568 /* menu item parts */
569 Lisp_Object Qmenu_alias;
570 Lisp_Object Qmenu_enable;
571 Lisp_Object QCenable, QCvisible, QChelp, QCfilter, QCkeys, QCkey_sequence;
572 Lisp_Object QCbutton, QCtoggle, QCradio;
573 extern Lisp_Object Vdefine_key_rebound_commands;
574 extern Lisp_Object Qmenu_item;
576 /* An event header symbol HEAD may have a property named
577 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
578 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
579 mask of modifiers applied to it. If present, this is used to help
580 speed up parse_modifiers. */
581 Lisp_Object Qevent_symbol_element_mask;
583 /* An unmodified event header BASE may have a property named
584 Qmodifier_cache, which is an alist mapping modifier masks onto
585 modified versions of BASE. If present, this helps speed up
586 apply_modifiers. */
587 Lisp_Object Qmodifier_cache;
589 /* Symbols to use for parts of windows. */
590 Lisp_Object Qmode_line;
591 Lisp_Object Qvertical_line;
592 Lisp_Object Qvertical_scroll_bar;
593 Lisp_Object Qmenu_bar;
594 extern Lisp_Object Qleft_margin, Qright_margin;
596 Lisp_Object recursive_edit_unwind (), command_loop ();
597 Lisp_Object Fthis_command_keys ();
598 Lisp_Object Qextended_command_history;
599 EMACS_TIME timer_check ();
601 extern Lisp_Object Vhistory_length, Vtranslation_table_for_input;
603 extern char *x_get_keysym_name ();
605 static void record_menu_key ();
607 Lisp_Object Qpolling_period;
609 /* List of absolute timers. Appears in order of next scheduled event. */
610 Lisp_Object Vtimer_list;
612 /* List of idle time timers. Appears in order of next scheduled event. */
613 Lisp_Object Vtimer_idle_list;
615 /* Incremented whenever a timer is run. */
616 int timers_run;
618 extern Lisp_Object Vprint_level, Vprint_length;
620 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
621 happens. */
622 EMACS_TIME *input_available_clear_time;
624 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
625 Default is 1 if INTERRUPT_INPUT is defined. */
626 int interrupt_input;
628 /* Nonzero while interrupts are temporarily deferred during redisplay. */
629 int interrupts_deferred;
631 /* Nonzero means use ^S/^Q for flow control. */
632 int flow_control;
634 /* Allow m- file to inhibit use of FIONREAD. */
635 #ifdef BROKEN_FIONREAD
636 #undef FIONREAD
637 #endif
639 /* We are unable to use interrupts if FIONREAD is not available,
640 so flush SIGIO so we won't try. */
641 #ifndef FIONREAD
642 #ifdef SIGIO
643 #undef SIGIO
644 #endif
645 #endif
647 /* If we support a window system, turn on the code to poll periodically
648 to detect C-g. It isn't actually used when doing interrupt input. */
649 #if defined(HAVE_WINDOW_SYSTEM) && !defined(USE_ASYNC_EVENTS)
650 #define POLL_FOR_INPUT
651 #endif
653 /* After a command is executed, if point is moved into a region that
654 has specific properties (e.g. composition, display), we adjust
655 point to the boundary of the region. But, if a command sets this
656 variable to non-nil, we suppress this point adjustment. This
657 variable is set to nil before reading a command. */
659 Lisp_Object Vdisable_point_adjustment;
661 /* If non-nil, always disable point adjustment. */
663 Lisp_Object Vglobal_disable_point_adjustment;
665 /* The time when Emacs started being idle. */
667 static EMACS_TIME timer_idleness_start_time;
669 /* After Emacs stops being idle, this saves the last value
670 of timer_idleness_start_time from when it was idle. */
672 static EMACS_TIME timer_last_idleness_start_time;
675 /* Global variable declarations. */
677 /* Function for init_keyboard to call with no args (if nonzero). */
678 void (*keyboard_init_hook) ();
680 static int read_avail_input P_ ((int));
681 static void get_input_pending P_ ((int *, int));
682 static void get_filtered_input_pending P_ ((int *, int, int));
683 static int readable_events P_ ((int));
684 static int readable_filtered_events P_ ((int, int));
685 static Lisp_Object read_char_x_menu_prompt P_ ((int, Lisp_Object *,
686 Lisp_Object, int *));
687 static Lisp_Object read_char_x_menu_prompt ();
688 static Lisp_Object read_char_minibuf_menu_prompt P_ ((int, int,
689 Lisp_Object *));
690 static Lisp_Object make_lispy_event P_ ((struct input_event *));
691 #ifdef HAVE_MOUSE
692 static Lisp_Object make_lispy_movement P_ ((struct frame *, Lisp_Object,
693 enum scroll_bar_part,
694 Lisp_Object, Lisp_Object,
695 unsigned long));
696 #endif
697 static Lisp_Object modify_event_symbol P_ ((int, unsigned, Lisp_Object,
698 Lisp_Object, char **,
699 Lisp_Object *, unsigned));
700 static Lisp_Object make_lispy_switch_frame P_ ((Lisp_Object));
701 static int parse_solitary_modifier P_ ((Lisp_Object));
702 static int parse_solitary_modifier ();
703 static void save_getcjmp P_ ((jmp_buf));
704 static void save_getcjmp ();
705 static void restore_getcjmp P_ ((jmp_buf));
706 static Lisp_Object apply_modifiers P_ ((int, Lisp_Object));
707 static void clear_event P_ ((struct input_event *));
708 static void any_kboard_state P_ ((void));
709 static SIGTYPE interrupt_signal P_ ((int signalnum));
711 /* Nonzero means don't try to suspend even if the operating system seems
712 to support it. */
713 static int cannot_suspend;
715 /* Install the string STR as the beginning of the string of echoing,
716 so that it serves as a prompt for the next character.
717 Also start echoing. */
719 void
720 echo_prompt (str)
721 Lisp_Object str;
723 current_kboard->echo_string = str;
724 current_kboard->echo_after_prompt = SCHARS (str);
725 echo_now ();
728 /* Add C to the echo string, if echoing is going on.
729 C can be a character, which is printed prettily ("M-C-x" and all that
730 jazz), or a symbol, whose name is printed. */
732 void
733 echo_char (c)
734 Lisp_Object c;
736 if (current_kboard->immediate_echo)
738 int size = KEY_DESCRIPTION_SIZE + 100;
739 char *buffer = (char *) alloca (size);
740 char *ptr = buffer;
741 Lisp_Object echo_string;
743 echo_string = current_kboard->echo_string;
745 /* If someone has passed us a composite event, use its head symbol. */
746 c = EVENT_HEAD (c);
748 if (INTEGERP (c))
750 ptr = push_key_description (XINT (c), ptr, 1);
752 else if (SYMBOLP (c))
754 Lisp_Object name = SYMBOL_NAME (c);
755 int nbytes = SBYTES (name);
757 if (size - (ptr - buffer) < nbytes)
759 int offset = ptr - buffer;
760 size = max (2 * size, size + nbytes);
761 buffer = (char *) alloca (size);
762 ptr = buffer + offset;
765 ptr += copy_text (SDATA (name), ptr, nbytes,
766 STRING_MULTIBYTE (name), 1);
769 if ((NILP (echo_string) || SCHARS (echo_string) == 0)
770 && help_char_p (c))
772 const char *text = " (Type ? for further options)";
773 int len = strlen (text);
775 if (size - (ptr - buffer) < len)
777 int offset = ptr - buffer;
778 size += len;
779 buffer = (char *) alloca (size);
780 ptr = buffer + offset;
783 bcopy (text, ptr, len);
784 ptr += len;
787 /* Replace a dash from echo_dash with a space, otherwise
788 add a space at the end as a separator between keys. */
789 if (STRINGP (echo_string)
790 && SCHARS (echo_string) > 0)
792 Lisp_Object last_char, idx;
794 idx = make_number (SCHARS (echo_string) - 1);
795 last_char = Faref (echo_string, idx);
797 if (XINT (last_char) == '-')
798 Faset (echo_string, idx, make_number (' '));
799 else
800 echo_string = concat2 (echo_string, build_string (" "));
803 current_kboard->echo_string
804 = concat2 (echo_string, make_string (buffer, ptr - buffer));
806 echo_now ();
810 /* Temporarily add a dash to the end of the echo string if it's not
811 empty, so that it serves as a mini-prompt for the very next character. */
813 void
814 echo_dash ()
816 /* Do nothing if not echoing at all. */
817 if (NILP (current_kboard->echo_string))
818 return;
820 if (!current_kboard->immediate_echo
821 && SCHARS (current_kboard->echo_string) == 0)
822 return;
824 /* Do nothing if we just printed a prompt. */
825 if (current_kboard->echo_after_prompt
826 == SCHARS (current_kboard->echo_string))
827 return;
829 /* Put a dash at the end of the buffer temporarily,
830 but make it go away when the next character is added. */
831 current_kboard->echo_string = concat2 (current_kboard->echo_string,
832 build_string ("-"));
833 echo_now ();
836 /* Display the current echo string, and begin echoing if not already
837 doing so. */
839 void
840 echo_now ()
842 if (!current_kboard->immediate_echo)
844 int i;
845 current_kboard->immediate_echo = 1;
847 for (i = 0; i < this_command_key_count; i++)
849 Lisp_Object c;
850 c = XVECTOR (this_command_keys)->contents[i];
851 if (! (EVENT_HAS_PARAMETERS (c)
852 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
853 echo_char (c);
855 echo_dash ();
858 echoing = 1;
859 message3_nolog (current_kboard->echo_string,
860 SBYTES (current_kboard->echo_string),
861 STRING_MULTIBYTE (current_kboard->echo_string));
862 echoing = 0;
864 /* Record in what buffer we echoed, and from which kboard. */
865 echo_message_buffer = echo_area_buffer[0];
866 echo_kboard = current_kboard;
868 if (waiting_for_input && !NILP (Vquit_flag))
869 quit_throw_to_read_char ();
872 /* Turn off echoing, for the start of a new command. */
874 void
875 cancel_echoing ()
877 current_kboard->immediate_echo = 0;
878 current_kboard->echo_after_prompt = -1;
879 current_kboard->echo_string = Qnil;
880 ok_to_echo_at_next_pause = NULL;
881 echo_kboard = NULL;
882 echo_message_buffer = Qnil;
885 /* Return the length of the current echo string. */
887 static int
888 echo_length ()
890 return (STRINGP (current_kboard->echo_string)
891 ? SCHARS (current_kboard->echo_string)
892 : 0);
895 /* Truncate the current echo message to its first LEN chars.
896 This and echo_char get used by read_key_sequence when the user
897 switches frames while entering a key sequence. */
899 static void
900 echo_truncate (nchars)
901 int nchars;
903 if (STRINGP (current_kboard->echo_string))
904 current_kboard->echo_string
905 = Fsubstring (current_kboard->echo_string,
906 make_number (0), make_number (nchars));
907 truncate_echo_area (nchars);
911 /* Functions for manipulating this_command_keys. */
912 static void
913 add_command_key (key)
914 Lisp_Object key;
916 /* If reset-this-command-length was called recently, obey it now.
917 See the doc string of that function for an explanation of why. */
918 if (before_command_restore_flag)
920 this_command_key_count = before_command_key_count_1;
921 if (this_command_key_count < this_single_command_key_start)
922 this_single_command_key_start = this_command_key_count;
923 echo_truncate (before_command_echo_length_1);
924 before_command_restore_flag = 0;
927 if (this_command_key_count >= ASIZE (this_command_keys))
928 this_command_keys = larger_vector (this_command_keys,
929 2 * ASIZE (this_command_keys),
930 Qnil);
932 AREF (this_command_keys, this_command_key_count) = key;
933 ++this_command_key_count;
937 Lisp_Object
938 recursive_edit_1 ()
940 int count = SPECPDL_INDEX ();
941 Lisp_Object val;
943 if (command_loop_level > 0)
945 specbind (Qstandard_output, Qt);
946 specbind (Qstandard_input, Qt);
949 #ifdef HAVE_X_WINDOWS
950 /* The command loop has started an hourglass timer, so we have to
951 cancel it here, otherwise it will fire because the recursive edit
952 can take some time. Do not check for display_hourglass_p here,
953 because it could already be nil. */
954 cancel_hourglass ();
955 #endif
957 /* This function may have been called from a debugger called from
958 within redisplay, for instance by Edebugging a function called
959 from fontification-functions. We want to allow redisplay in
960 the debugging session.
962 The recursive edit is left with a `(throw exit ...)'. The `exit'
963 tag is not caught anywhere in redisplay, i.e. when we leave the
964 recursive edit, the original redisplay leading to the recursive
965 edit will be unwound. The outcome should therefore be safe. */
966 specbind (Qinhibit_redisplay, Qnil);
967 redisplaying_p = 0;
969 val = command_loop ();
970 if (EQ (val, Qt))
971 Fsignal (Qquit, Qnil);
972 /* Handle throw from read_minibuf when using minibuffer
973 while it's active but we're in another window. */
974 if (STRINGP (val))
975 Fsignal (Qerror, Fcons (val, Qnil));
977 return unbind_to (count, Qnil);
980 /* When an auto-save happens, record the "time", and don't do again soon. */
982 void
983 record_auto_save ()
985 last_auto_save = num_nonmacro_input_events;
988 /* Make an auto save happen as soon as possible at command level. */
990 void
991 force_auto_save_soon ()
993 last_auto_save = - auto_save_interval - 1;
995 record_asynch_buffer_change ();
998 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
999 doc: /* Invoke the editor command loop recursively.
1000 To get out of the recursive edit, a command can do `(throw 'exit nil)';
1001 that tells this function to return.
1002 Alternately, `(throw 'exit t)' makes this function signal an error.
1003 This function is called by the editor initialization to begin editing. */)
1006 int count = SPECPDL_INDEX ();
1007 Lisp_Object buffer;
1009 command_loop_level++;
1010 update_mode_lines = 1;
1012 if (command_loop_level
1013 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
1014 buffer = Fcurrent_buffer ();
1015 else
1016 buffer = Qnil;
1018 /* If we leave recursive_edit_1 below with a `throw' for instance,
1019 like it is done in the splash screen display, we have to
1020 make sure that we restore single_kboard as command_loop_1
1021 would have done if it were left normally. */
1022 record_unwind_protect (recursive_edit_unwind,
1023 Fcons (buffer, single_kboard ? Qt : Qnil));
1025 recursive_edit_1 ();
1026 return unbind_to (count, Qnil);
1029 Lisp_Object
1030 recursive_edit_unwind (info)
1031 Lisp_Object info;
1033 if (BUFFERP (XCAR (info)))
1034 Fset_buffer (XCAR (info));
1036 if (NILP (XCDR (info)))
1037 any_kboard_state ();
1038 else
1039 single_kboard_state ();
1041 command_loop_level--;
1042 update_mode_lines = 1;
1043 return Qnil;
1047 static void
1048 any_kboard_state ()
1050 #ifdef MULTI_KBOARD
1051 #if 0 /* Theory: if there's anything in Vunread_command_events,
1052 it will right away be read by read_key_sequence,
1053 and then if we do switch KBOARDS, it will go into the side
1054 queue then. So we don't need to do anything special here -- rms. */
1055 if (CONSP (Vunread_command_events))
1057 current_kboard->kbd_queue
1058 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
1059 current_kboard->kbd_queue_has_data = 1;
1061 Vunread_command_events = Qnil;
1062 #endif
1063 single_kboard = 0;
1064 #endif
1067 /* Switch to the single-kboard state, making current_kboard
1068 the only KBOARD from which further input is accepted. */
1070 void
1071 single_kboard_state ()
1073 #ifdef MULTI_KBOARD
1074 single_kboard = 1;
1075 #endif
1078 /* Maintain a stack of kboards, so other parts of Emacs
1079 can switch temporarily to the kboard of a given frame
1080 and then revert to the previous status. */
1082 struct kboard_stack
1084 KBOARD *kboard;
1085 struct kboard_stack *next;
1088 static struct kboard_stack *kboard_stack;
1090 void
1091 push_frame_kboard (f)
1092 FRAME_PTR f;
1094 #ifdef MULTI_KBOARD
1095 struct kboard_stack *p
1096 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
1098 p->next = kboard_stack;
1099 p->kboard = current_kboard;
1100 kboard_stack = p;
1102 current_kboard = FRAME_KBOARD (f);
1103 #endif
1106 void
1107 pop_frame_kboard ()
1109 #ifdef MULTI_KBOARD
1110 struct kboard_stack *p = kboard_stack;
1111 current_kboard = p->kboard;
1112 kboard_stack = p->next;
1113 xfree (p);
1114 #endif
1117 /* Handle errors that are not handled at inner levels
1118 by printing an error message and returning to the editor command loop. */
1120 Lisp_Object
1121 cmd_error (data)
1122 Lisp_Object data;
1124 Lisp_Object old_level, old_length;
1125 char macroerror[50];
1127 #ifdef HAVE_X_WINDOWS
1128 if (display_hourglass_p)
1129 cancel_hourglass ();
1130 #endif
1132 if (!NILP (executing_macro))
1134 if (executing_macro_iterations == 1)
1135 sprintf (macroerror, "After 1 kbd macro iteration: ");
1136 else
1137 sprintf (macroerror, "After %d kbd macro iterations: ",
1138 executing_macro_iterations);
1140 else
1141 *macroerror = 0;
1143 Vstandard_output = Qt;
1144 Vstandard_input = Qt;
1145 Vexecuting_macro = Qnil;
1146 executing_macro = Qnil;
1147 current_kboard->Vprefix_arg = Qnil;
1148 current_kboard->Vlast_prefix_arg = Qnil;
1149 cancel_echoing ();
1151 /* Avoid unquittable loop if data contains a circular list. */
1152 old_level = Vprint_level;
1153 old_length = Vprint_length;
1154 XSETFASTINT (Vprint_level, 10);
1155 XSETFASTINT (Vprint_length, 10);
1156 cmd_error_internal (data, macroerror);
1157 Vprint_level = old_level;
1158 Vprint_length = old_length;
1160 Vquit_flag = Qnil;
1162 Vinhibit_quit = Qnil;
1163 #ifdef MULTI_KBOARD
1164 any_kboard_state ();
1165 #endif
1167 return make_number (0);
1170 /* Take actions on handling an error. DATA is the data that describes
1171 the error.
1173 CONTEXT is a C-string containing ASCII characters only which
1174 describes the context in which the error happened. If we need to
1175 generalize CONTEXT to allow multibyte characters, make it a Lisp
1176 string. */
1178 void
1179 cmd_error_internal (data, context)
1180 Lisp_Object data;
1181 char *context;
1183 Lisp_Object stream;
1184 int kill_emacs_p = 0;
1185 struct frame *sf = SELECTED_FRAME ();
1187 Vquit_flag = Qnil;
1188 Vinhibit_quit = Qt;
1189 clear_message (1, 0);
1191 /* If the window system or terminal frame hasn't been initialized
1192 yet, or we're not interactive, it's best to dump this message out
1193 to stderr and exit. */
1194 if (!sf->glyphs_initialized_p
1195 /* This is the case of the frame dumped with Emacs, when we're
1196 running under a window system. */
1197 || (!NILP (Vwindow_system)
1198 && !inhibit_window_system
1199 && FRAME_TERMCAP_P (sf))
1200 || noninteractive)
1202 stream = Qexternal_debugging_output;
1203 kill_emacs_p = 1;
1205 else
1207 Fdiscard_input ();
1208 message_log_maybe_newline ();
1209 bitch_at_user ();
1210 stream = Qt;
1213 /* The immediate context is not interesting for Quits,
1214 since they are asyncronous. */
1215 if (EQ (XCAR (data), Qquit))
1216 Vsignaling_function = Qnil;
1218 print_error_message (data, stream, context, Vsignaling_function);
1220 Vsignaling_function = Qnil;
1222 /* If the window system or terminal frame hasn't been initialized
1223 yet, or we're in -batch mode, this error should cause Emacs to exit. */
1224 if (kill_emacs_p)
1226 Fterpri (stream);
1227 Fkill_emacs (make_number (-1));
1231 Lisp_Object command_loop_1 ();
1232 Lisp_Object command_loop_2 ();
1233 Lisp_Object top_level_1 ();
1235 /* Entry to editor-command-loop.
1236 This level has the catches for exiting/returning to editor command loop.
1237 It returns nil to exit recursive edit, t to abort it. */
1239 Lisp_Object
1240 command_loop ()
1242 if (command_loop_level > 0 || minibuf_level > 0)
1244 Lisp_Object val;
1245 val = internal_catch (Qexit, command_loop_2, Qnil);
1246 executing_macro = Qnil;
1247 return val;
1249 else
1250 while (1)
1252 internal_catch (Qtop_level, top_level_1, Qnil);
1253 internal_catch (Qtop_level, command_loop_2, Qnil);
1254 executing_macro = Qnil;
1256 /* End of file in -batch run causes exit here. */
1257 if (noninteractive)
1258 Fkill_emacs (Qt);
1262 /* Here we catch errors in execution of commands within the
1263 editing loop, and reenter the editing loop.
1264 When there is an error, cmd_error runs and returns a non-nil
1265 value to us. A value of nil means that command_loop_1 itself
1266 returned due to end of file (or end of kbd macro). */
1268 Lisp_Object
1269 command_loop_2 ()
1271 register Lisp_Object val;
1274 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
1275 while (!NILP (val));
1277 return Qnil;
1280 Lisp_Object
1281 top_level_2 ()
1283 return Feval (Vtop_level);
1286 Lisp_Object
1287 top_level_1 ()
1289 /* On entry to the outer level, run the startup file */
1290 if (!NILP (Vtop_level))
1291 internal_condition_case (top_level_2, Qerror, cmd_error);
1292 else if (!NILP (Vpurify_flag))
1293 message ("Bare impure Emacs (standard Lisp code not loaded)");
1294 else
1295 message ("Bare Emacs (standard Lisp code not loaded)");
1296 return Qnil;
1299 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1300 doc: /* Exit all recursive editing levels. */)
1303 #ifdef HAVE_X_WINDOWS
1304 if (display_hourglass_p)
1305 cancel_hourglass ();
1306 #endif
1307 return Fthrow (Qtop_level, Qnil);
1310 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1311 doc: /* Exit from the innermost recursive edit or minibuffer. */)
1314 if (command_loop_level > 0 || minibuf_level > 0)
1315 Fthrow (Qexit, Qnil);
1317 error ("No recursive edit is in progress");
1318 return Qnil;
1321 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1322 doc: /* Abort the command that requested this recursive edit or minibuffer input. */)
1325 if (command_loop_level > 0 || minibuf_level > 0)
1326 Fthrow (Qexit, Qt);
1328 error ("No recursive edit is in progress");
1329 return Qnil;
1332 /* This is the actual command reading loop,
1333 sans error-handling encapsulation. */
1335 static int read_key_sequence P_ ((Lisp_Object *, int, Lisp_Object,
1336 int, int, int));
1337 void safe_run_hooks P_ ((Lisp_Object));
1338 static void adjust_point_for_property P_ ((int));
1340 Lisp_Object
1341 command_loop_1 ()
1343 Lisp_Object cmd;
1344 int lose;
1345 int nonundocount;
1346 Lisp_Object keybuf[30];
1347 int i;
1348 int no_direct;
1349 int prev_modiff;
1350 struct buffer *prev_buffer = NULL;
1351 #ifdef MULTI_KBOARD
1352 int was_locked = single_kboard;
1353 #endif
1354 int already_adjusted;
1356 current_kboard->Vprefix_arg = Qnil;
1357 current_kboard->Vlast_prefix_arg = Qnil;
1358 Vdeactivate_mark = Qnil;
1359 waiting_for_input = 0;
1360 cancel_echoing ();
1362 nonundocount = 0;
1363 this_command_key_count = 0;
1364 this_single_command_key_start = 0;
1366 if (NILP (Vmemory_full))
1368 /* Make sure this hook runs after commands that get errors and
1369 throw to top level. */
1370 /* Note that the value cell will never directly contain nil
1371 if the symbol is a local variable. */
1372 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1373 safe_run_hooks (Qpost_command_hook);
1375 /* If displaying a message, resize the echo area window to fit
1376 that message's size exactly. */
1377 if (!NILP (echo_area_buffer[0]))
1378 resize_echo_area_exactly ();
1380 if (!NILP (Vdeferred_action_list))
1381 call0 (Vdeferred_action_function);
1383 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1385 if (NILP (Vunread_command_events)
1386 && NILP (Vunread_input_method_events)
1387 && NILP (Vunread_post_input_method_events)
1388 && NILP (Vexecuting_macro)
1389 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1390 safe_run_hooks (Qpost_command_idle_hook);
1394 Vmemory_full = Qnil;
1396 /* Do this after running Vpost_command_hook, for consistency. */
1397 current_kboard->Vlast_command = Vthis_command;
1398 current_kboard->Vreal_last_command = real_this_command;
1400 while (1)
1402 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1403 Fkill_emacs (Qnil);
1405 /* Make sure the current window's buffer is selected. */
1406 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1407 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1409 /* Display any malloc warning that just came out. Use while because
1410 displaying one warning can cause another. */
1412 while (pending_malloc_warning)
1413 display_malloc_warning ();
1415 no_direct = 0;
1417 Vdeactivate_mark = Qnil;
1419 /* If minibuffer on and echo area in use,
1420 wait a short time and redraw minibuffer. */
1422 if (minibuf_level
1423 && !NILP (echo_area_buffer[0])
1424 && EQ (minibuf_window, echo_area_window)
1425 && NUMBERP (Vminibuffer_message_timeout))
1427 /* Bind inhibit-quit to t so that C-g gets read in
1428 rather than quitting back to the minibuffer. */
1429 int count = SPECPDL_INDEX ();
1430 specbind (Qinhibit_quit, Qt);
1432 Fsit_for (Vminibuffer_message_timeout, Qnil, Qnil);
1433 /* Clear the echo area. */
1434 message2 (0, 0, 0);
1435 safe_run_hooks (Qecho_area_clear_hook);
1437 unbind_to (count, Qnil);
1439 /* If a C-g came in before, treat it as input now. */
1440 if (!NILP (Vquit_flag))
1442 Vquit_flag = Qnil;
1443 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1447 #ifdef C_ALLOCA
1448 alloca (0); /* Cause a garbage collection now */
1449 /* Since we can free the most stuff here. */
1450 #endif /* C_ALLOCA */
1452 #if 0
1453 /* Select the frame that the last event came from. Usually,
1454 switch-frame events will take care of this, but if some lisp
1455 code swallows a switch-frame event, we'll fix things up here.
1456 Is this a good idea? */
1457 if (FRAMEP (internal_last_event_frame)
1458 && !EQ (internal_last_event_frame, selected_frame))
1459 Fselect_frame (internal_last_event_frame, Qnil);
1460 #endif
1461 /* If it has changed current-menubar from previous value,
1462 really recompute the menubar from the value. */
1463 if (! NILP (Vlucid_menu_bar_dirty_flag)
1464 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1465 call0 (Qrecompute_lucid_menubar);
1467 before_command_key_count = this_command_key_count;
1468 before_command_echo_length = echo_length ();
1470 Vthis_command = Qnil;
1471 real_this_command = Qnil;
1473 /* Read next key sequence; i gets its length. */
1474 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1475 Qnil, 0, 1, 1);
1477 /* A filter may have run while we were reading the input. */
1478 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1479 Fkill_emacs (Qnil);
1480 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1481 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1483 ++num_input_keys;
1485 /* Now we have read a key sequence of length I,
1486 or else I is 0 and we found end of file. */
1488 if (i == 0) /* End of file -- happens only in */
1489 return Qnil; /* a kbd macro, at the end. */
1490 /* -1 means read_key_sequence got a menu that was rejected.
1491 Just loop around and read another command. */
1492 if (i == -1)
1494 cancel_echoing ();
1495 this_command_key_count = 0;
1496 this_single_command_key_start = 0;
1497 goto finalize;
1500 last_command_char = keybuf[i - 1];
1502 /* If the previous command tried to force a specific window-start,
1503 forget about that, in case this command moves point far away
1504 from that position. But also throw away beg_unchanged and
1505 end_unchanged information in that case, so that redisplay will
1506 update the whole window properly. */
1507 if (!NILP (XWINDOW (selected_window)->force_start))
1509 struct buffer *b;
1510 XWINDOW (selected_window)->force_start = Qnil;
1511 b = XBUFFER (XWINDOW (selected_window)->buffer);
1512 BUF_BEG_UNCHANGED (b) = BUF_END_UNCHANGED (b) = 0;
1515 cmd = read_key_sequence_cmd;
1516 if (!NILP (Vexecuting_macro))
1518 if (!NILP (Vquit_flag))
1520 Vexecuting_macro = Qt;
1521 QUIT; /* Make some noise. */
1522 /* Will return since macro now empty. */
1526 /* Do redisplay processing after this command except in special
1527 cases identified below. */
1528 prev_buffer = current_buffer;
1529 prev_modiff = MODIFF;
1530 last_point_position = PT;
1531 XSETBUFFER (last_point_position_buffer, prev_buffer);
1533 /* By default, we adjust point to a boundary of a region that
1534 has such a property that should be treated intangible
1535 (e.g. composition, display). But, some commands will set
1536 this variable differently. */
1537 Vdisable_point_adjustment = Qnil;
1539 /* Process filters and timers may have messed with deactivate-mark.
1540 reset it before we execute the command. */
1541 Vdeactivate_mark = Qnil;
1543 /* Remap command through active keymaps */
1544 Vthis_original_command = cmd;
1545 if (SYMBOLP (cmd))
1547 Lisp_Object cmd1;
1548 if (cmd1 = Fremap_command (cmd), !NILP (cmd1))
1549 cmd = cmd1;
1552 /* Execute the command. */
1554 Vthis_command = cmd;
1555 real_this_command = cmd;
1556 /* Note that the value cell will never directly contain nil
1557 if the symbol is a local variable. */
1558 if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
1559 safe_run_hooks (Qpre_command_hook);
1561 already_adjusted = 0;
1563 if (NILP (Vthis_command))
1565 /* nil means key is undefined. */
1566 bitch_at_user ();
1567 current_kboard->defining_kbd_macro = Qnil;
1568 update_mode_lines = 1;
1569 current_kboard->Vprefix_arg = Qnil;
1571 else
1573 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1575 /* In case we jump to directly_done. */
1576 Vcurrent_prefix_arg = current_kboard->Vprefix_arg;
1578 /* Recognize some common commands in common situations and
1579 do them directly. */
1580 if (EQ (Vthis_command, Qforward_char) && PT < ZV)
1582 struct Lisp_Char_Table *dp
1583 = window_display_table (XWINDOW (selected_window));
1584 lose = FETCH_CHAR (PT_BYTE);
1585 SET_PT (PT + 1);
1586 if (! NILP (Vpost_command_hook))
1587 /* Put this before calling adjust_point_for_property
1588 so it will only get called once in any case. */
1589 goto directly_done;
1590 adjust_point_for_property (last_point_position);
1591 already_adjusted = 1;
1592 if (PT == last_point_position + 1
1593 && (dp
1594 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1595 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1596 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1597 && (lose >= 0x20 && lose < 0x7f)))
1598 : (lose >= 0x20 && lose < 0x7f))
1599 /* To extract the case of continuation on
1600 wide-column characters. */
1601 && (WIDTH_BY_CHAR_HEAD (FETCH_BYTE (PT_BYTE)) == 1)
1602 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1603 >= MODIFF)
1604 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1605 >= OVERLAY_MODIFF)
1606 && (XFASTINT (XWINDOW (selected_window)->last_point)
1607 == PT - 1)
1608 && !windows_or_buffers_changed
1609 && EQ (current_buffer->selective_display, Qnil)
1610 && !detect_input_pending ()
1611 && NILP (XWINDOW (selected_window)->column_number_displayed)
1612 && NILP (Vexecuting_macro))
1613 direct_output_forward_char (1);
1614 goto directly_done;
1616 else if (EQ (Vthis_command, Qbackward_char) && PT > BEGV)
1618 struct Lisp_Char_Table *dp
1619 = window_display_table (XWINDOW (selected_window));
1620 SET_PT (PT - 1);
1621 lose = FETCH_CHAR (PT_BYTE);
1622 if (! NILP (Vpost_command_hook))
1623 goto directly_done;
1624 adjust_point_for_property (last_point_position);
1625 already_adjusted = 1;
1626 if (PT == last_point_position - 1
1627 && (dp
1628 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1629 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1630 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1631 && (lose >= 0x20 && lose < 0x7f)))
1632 : (lose >= 0x20 && lose < 0x7f))
1633 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1634 >= MODIFF)
1635 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1636 >= OVERLAY_MODIFF)
1637 && (XFASTINT (XWINDOW (selected_window)->last_point)
1638 == PT + 1)
1639 && !windows_or_buffers_changed
1640 && EQ (current_buffer->selective_display, Qnil)
1641 && !detect_input_pending ()
1642 && NILP (XWINDOW (selected_window)->column_number_displayed)
1643 && NILP (Vexecuting_macro))
1644 direct_output_forward_char (-1);
1645 goto directly_done;
1647 else if (EQ (Vthis_command, Qself_insert_command)
1648 /* Try this optimization only on char keystrokes. */
1649 && NATNUMP (last_command_char)
1650 && CHAR_VALID_P (XFASTINT (last_command_char), 0))
1652 unsigned int c
1653 = translate_char (Vtranslation_table_for_input,
1654 XFASTINT (last_command_char), 0, 0, 0);
1655 int value;
1656 if (NILP (Vexecuting_macro)
1657 && !EQ (minibuf_window, selected_window))
1659 if (!nonundocount || nonundocount >= 20)
1661 Fundo_boundary ();
1662 nonundocount = 0;
1664 nonundocount++;
1667 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1668 < MODIFF)
1669 || (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1670 < OVERLAY_MODIFF)
1671 || (XFASTINT (XWINDOW (selected_window)->last_point)
1672 != PT)
1673 || MODIFF <= SAVE_MODIFF
1674 || windows_or_buffers_changed
1675 || !EQ (current_buffer->selective_display, Qnil)
1676 || detect_input_pending ()
1677 || !NILP (XWINDOW (selected_window)->column_number_displayed)
1678 || !NILP (Vexecuting_macro));
1680 value = internal_self_insert (c, 0);
1682 if (value == 2)
1683 nonundocount = 0;
1685 if (! NILP (Vpost_command_hook))
1686 /* Put this before calling adjust_point_for_property
1687 so it will only get called once in any case. */
1688 goto directly_done;
1690 /* VALUE == 1 when AFTER-CHANGE functions are
1691 installed which is the case most of the time
1692 because FONT-LOCK installs one. */
1693 if (!lose && !value)
1694 direct_output_for_insert (c);
1695 goto directly_done;
1699 /* Here for a command that isn't executed directly */
1701 #ifdef HAVE_X_WINDOWS
1702 if (display_hourglass_p
1703 && NILP (Vexecuting_macro))
1704 start_hourglass ();
1705 #endif
1707 nonundocount = 0;
1708 if (NILP (current_kboard->Vprefix_arg))
1709 Fundo_boundary ();
1710 Fcommand_execute (Vthis_command, Qnil, Qnil, Qnil);
1712 #ifdef HAVE_X_WINDOWS
1713 /* Do not check display_hourglass_p here, because
1714 Fcommand_execute could change it, but we should cancel
1715 hourglass cursor anyway.
1716 But don't cancel the hourglass within a macro
1717 just because a command in the macro finishes. */
1718 if (NILP (Vexecuting_macro))
1719 cancel_hourglass ();
1720 #endif
1722 directly_done: ;
1723 current_kboard->Vlast_prefix_arg = Vcurrent_prefix_arg;
1725 /* Note that the value cell will never directly contain nil
1726 if the symbol is a local variable. */
1727 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1728 safe_run_hooks (Qpost_command_hook);
1730 /* If displaying a message, resize the echo area window to fit
1731 that message's size exactly. */
1732 if (!NILP (echo_area_buffer[0]))
1733 resize_echo_area_exactly ();
1735 if (!NILP (Vdeferred_action_list))
1736 safe_run_hooks (Qdeferred_action_function);
1738 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1740 if (NILP (Vunread_command_events)
1741 && NILP (Vunread_input_method_events)
1742 && NILP (Vunread_post_input_method_events)
1743 && NILP (Vexecuting_macro)
1744 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1745 safe_run_hooks (Qpost_command_idle_hook);
1748 /* If there is a prefix argument,
1749 1) We don't want Vlast_command to be ``universal-argument''
1750 (that would be dumb), so don't set Vlast_command,
1751 2) we want to leave echoing on so that the prefix will be
1752 echoed as part of this key sequence, so don't call
1753 cancel_echoing, and
1754 3) we want to leave this_command_key_count non-zero, so that
1755 read_char will realize that it is re-reading a character, and
1756 not echo it a second time.
1758 If the command didn't actually create a prefix arg,
1759 but is merely a frame event that is transparent to prefix args,
1760 then the above doesn't apply. */
1761 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1763 current_kboard->Vlast_command = Vthis_command;
1764 current_kboard->Vreal_last_command = real_this_command;
1765 cancel_echoing ();
1766 this_command_key_count = 0;
1767 this_single_command_key_start = 0;
1770 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1772 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1774 /* We could also call `deactivate'mark'. */
1775 if (EQ (Vtransient_mark_mode, Qlambda))
1776 Vtransient_mark_mode = Qnil;
1777 else
1779 current_buffer->mark_active = Qnil;
1780 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1783 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1784 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1787 finalize:
1789 if (current_buffer == prev_buffer
1790 && last_point_position != PT
1791 && NILP (Vdisable_point_adjustment)
1792 && NILP (Vglobal_disable_point_adjustment)
1793 && !already_adjusted)
1794 adjust_point_for_property (last_point_position);
1796 /* Install chars successfully executed in kbd macro. */
1798 if (!NILP (current_kboard->defining_kbd_macro)
1799 && NILP (current_kboard->Vprefix_arg))
1800 finalize_kbd_macro_chars ();
1802 #ifdef MULTI_KBOARD
1803 if (!was_locked)
1804 any_kboard_state ();
1805 #endif
1809 extern Lisp_Object Qcomposition, Qdisplay;
1811 /* Adjust point to a boundary of a region that has such a property
1812 that should be treated intangible. For the moment, we check
1813 `composition', `display' and `invisible' properties.
1814 LAST_PT is the last position of point. */
1816 extern Lisp_Object Qafter_string, Qbefore_string;
1817 extern Lisp_Object get_pos_property P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
1819 static void
1820 adjust_point_for_property (last_pt)
1821 int last_pt;
1823 int beg, end;
1824 Lisp_Object val, overlay, tmp;
1825 int check_composition = 1, check_display = 1, check_invisible = 1;
1827 while (check_composition || check_display || check_invisible)
1829 if (check_composition
1830 && PT > BEGV && PT < ZV
1831 && get_property_and_range (PT, Qcomposition, &val, &beg, &end, Qnil)
1832 && COMPOSITION_VALID_P (beg, end, val)
1833 && beg < PT /* && end > PT <- It's always the case. */
1834 && (last_pt <= beg || last_pt >= end))
1836 xassert (end > PT);
1837 SET_PT (PT < last_pt ? beg : end);
1838 check_display = check_invisible = 1;
1840 check_composition = 0;
1841 if (check_display
1842 && PT > BEGV && PT < ZV
1843 && !NILP (val = get_char_property_and_overlay
1844 (make_number (PT), Qdisplay, Qnil, &overlay))
1845 && display_prop_intangible_p (val)
1846 && (!OVERLAYP (overlay)
1847 ? get_property_and_range (PT, Qdisplay, &val, &beg, &end, Qnil)
1848 : (beg = OVERLAY_POSITION (OVERLAY_START (overlay)),
1849 end = OVERLAY_POSITION (OVERLAY_END (overlay))))
1850 && beg < PT) /* && end > PT <- It's always the case. */
1852 xassert (end > PT);
1853 SET_PT (PT < last_pt ? beg : end);
1854 check_composition = check_invisible = 1;
1856 check_display = 0;
1857 if (check_invisible && PT > BEGV && PT < ZV)
1859 int inv, ellipsis = 0;
1860 beg = end = PT;
1862 /* Find boundaries `beg' and `end' of the invisible area, if any. */
1863 while (end < ZV
1864 && !NILP (val = get_char_property_and_overlay
1865 (make_number (end), Qinvisible, Qnil, &overlay))
1866 && (inv = TEXT_PROP_MEANS_INVISIBLE (val)))
1868 ellipsis = ellipsis || inv > 1
1869 || (OVERLAYP (overlay)
1870 && (!NILP (Foverlay_get (overlay, Qafter_string))
1871 || !NILP (Foverlay_get (overlay, Qbefore_string))));
1872 tmp = Fnext_single_char_property_change
1873 (make_number (end), Qinvisible, Qnil, Qnil);
1874 end = NATNUMP (tmp) ? XFASTINT (tmp) : ZV;
1876 while (beg > BEGV
1877 && !NILP (val = get_char_property_and_overlay
1878 (make_number (beg - 1), Qinvisible, Qnil, &overlay))
1879 && (inv = TEXT_PROP_MEANS_INVISIBLE (val)))
1881 ellipsis = ellipsis || inv > 1
1882 || (OVERLAYP (overlay)
1883 && (!NILP (Foverlay_get (overlay, Qafter_string))
1884 || !NILP (Foverlay_get (overlay, Qbefore_string))));
1885 tmp = Fprevious_single_char_property_change
1886 (make_number (beg), Qinvisible, Qnil, Qnil);
1887 beg = NATNUMP (tmp) ? XFASTINT (tmp) : BEGV;
1890 /* Move away from the inside area. */
1891 if (beg < PT && end > PT)
1893 SET_PT (PT < last_pt ? beg : end);
1894 check_composition = check_display = 1;
1896 xassert (PT == beg || PT == end);
1897 /* Pretend the area doesn't exist. */
1898 if (!ellipsis && beg < end)
1900 if (last_pt == beg && PT == end && end < ZV)
1901 (check_composition = check_display = 1, SET_PT (end + 1));
1902 else if (last_pt == end && PT == beg && beg > BEGV)
1903 (check_composition = check_display = 1, SET_PT (beg - 1));
1904 else if (PT == ((PT < last_pt) ? beg : end))
1905 /* We've already moved as far as we can. Trying to go
1906 to the other end would mean moving backwards and thus
1907 could lead to an infinite loop. */
1909 else if (val = get_pos_property (make_number (PT),
1910 Qinvisible, Qnil),
1911 TEXT_PROP_MEANS_INVISIBLE (val)
1912 && (val = get_pos_property
1913 (make_number (PT == beg ? end : beg),
1914 Qinvisible, Qnil),
1915 !TEXT_PROP_MEANS_INVISIBLE (val)))
1916 (check_composition = check_display = 1,
1917 SET_PT (PT == beg ? end : beg));
1920 check_invisible = 0;
1924 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1926 static Lisp_Object
1927 safe_run_hooks_1 (hook)
1928 Lisp_Object hook;
1930 return call1 (Vrun_hooks, Vinhibit_quit);
1933 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1935 static Lisp_Object
1936 safe_run_hooks_error (data)
1937 Lisp_Object data;
1939 return Fset (Vinhibit_quit, Qnil);
1942 /* If we get an error while running the hook, cause the hook variable
1943 to be nil. Also inhibit quits, so that C-g won't cause the hook
1944 to mysteriously evaporate. */
1946 void
1947 safe_run_hooks (hook)
1948 Lisp_Object hook;
1950 int count = SPECPDL_INDEX ();
1951 specbind (Qinhibit_quit, hook);
1953 internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
1955 unbind_to (count, Qnil);
1959 /* Number of seconds between polling for input. This is a Lisp
1960 variable that can be bound. */
1962 EMACS_INT polling_period;
1964 /* Nonzero means polling for input is temporarily suppressed. */
1966 int poll_suppress_count;
1968 /* Asynchronous timer for polling. */
1970 struct atimer *poll_timer;
1973 #ifdef POLL_FOR_INPUT
1975 /* Poll for input, so what we catch a C-g if it comes in. This
1976 function is called from x_make_frame_visible, see comment
1977 there. */
1979 void
1980 poll_for_input_1 ()
1982 if (interrupt_input_blocked == 0
1983 && !waiting_for_input)
1984 read_avail_input (0);
1987 /* Timer callback function for poll_timer. TIMER is equal to
1988 poll_timer. */
1990 void
1991 poll_for_input (timer)
1992 struct atimer *timer;
1994 if (poll_suppress_count == 0)
1995 poll_for_input_1 ();
1998 #endif /* POLL_FOR_INPUT */
2000 /* Begin signals to poll for input, if they are appropriate.
2001 This function is called unconditionally from various places. */
2003 void
2004 start_polling ()
2006 #ifdef POLL_FOR_INPUT
2007 if (read_socket_hook && !interrupt_input)
2009 /* Turn alarm handling on unconditionally. It might have
2010 been turned off in process.c. */
2011 turn_on_atimers (1);
2013 /* If poll timer doesn't exist, are we need one with
2014 a different interval, start a new one. */
2015 if (poll_timer == NULL
2016 || EMACS_SECS (poll_timer->interval) != polling_period)
2018 EMACS_TIME interval;
2020 if (poll_timer)
2021 cancel_atimer (poll_timer);
2023 EMACS_SET_SECS_USECS (interval, polling_period, 0);
2024 poll_timer = start_atimer (ATIMER_CONTINUOUS, interval,
2025 poll_for_input, NULL);
2028 /* Let the timer's callback function poll for input
2029 if this becomes zero. */
2030 --poll_suppress_count;
2032 #endif
2035 /* Nonzero if we are using polling to handle input asynchronously. */
2038 input_polling_used ()
2040 #ifdef POLL_FOR_INPUT
2041 return read_socket_hook && !interrupt_input;
2042 #else
2043 return 0;
2044 #endif
2047 /* Turn off polling. */
2049 void
2050 stop_polling ()
2052 #ifdef POLL_FOR_INPUT
2053 if (read_socket_hook && !interrupt_input)
2054 ++poll_suppress_count;
2055 #endif
2058 /* Set the value of poll_suppress_count to COUNT
2059 and start or stop polling accordingly. */
2061 void
2062 set_poll_suppress_count (count)
2063 int count;
2065 #ifdef POLL_FOR_INPUT
2066 if (count == 0 && poll_suppress_count != 0)
2068 poll_suppress_count = 1;
2069 start_polling ();
2071 else if (count != 0 && poll_suppress_count == 0)
2073 stop_polling ();
2075 poll_suppress_count = count;
2076 #endif
2079 /* Bind polling_period to a value at least N.
2080 But don't decrease it. */
2082 void
2083 bind_polling_period (n)
2084 int n;
2086 #ifdef POLL_FOR_INPUT
2087 int new = polling_period;
2089 if (n > new)
2090 new = n;
2092 stop_other_atimers (poll_timer);
2093 stop_polling ();
2094 specbind (Qpolling_period, make_number (new));
2095 /* Start a new alarm with the new period. */
2096 start_polling ();
2097 #endif
2100 /* Apply the control modifier to CHARACTER. */
2103 make_ctrl_char (c)
2104 int c;
2106 /* Save the upper bits here. */
2107 int upper = c & ~0177;
2109 c &= 0177;
2111 /* Everything in the columns containing the upper-case letters
2112 denotes a control character. */
2113 if (c >= 0100 && c < 0140)
2115 int oc = c;
2116 c &= ~0140;
2117 /* Set the shift modifier for a control char
2118 made from a shifted letter. But only for letters! */
2119 if (oc >= 'A' && oc <= 'Z')
2120 c |= shift_modifier;
2123 /* The lower-case letters denote control characters too. */
2124 else if (c >= 'a' && c <= 'z')
2125 c &= ~0140;
2127 /* Include the bits for control and shift
2128 only if the basic ASCII code can't indicate them. */
2129 else if (c >= ' ')
2130 c |= ctrl_modifier;
2132 /* Replace the high bits. */
2133 c |= (upper & ~ctrl_modifier);
2135 return c;
2138 /* Display help echo in the echo area.
2140 HELP a string means display that string, HELP nil means clear the
2141 help echo. If HELP is a function, call it with OBJECT and POS as
2142 arguments; the function should return a help string or nil for
2143 none. For all other types of HELP evaluate it to obtain a string.
2145 WINDOW is the window in which the help was generated, if any.
2146 It is nil if not in a window.
2148 If OBJECT is a buffer, POS is the position in the buffer where the
2149 `help-echo' text property was found.
2151 If OBJECT is an overlay, that overlay has a `help-echo' property,
2152 and POS is the position in the overlay's buffer under the mouse.
2154 If OBJECT is a string (an overlay string or a string displayed with
2155 the `display' property). POS is the position in that string under
2156 the mouse.
2158 OK_TO_OVERWRITE_KEYSTROKE_ECHO non-zero means it's okay if the help
2159 echo overwrites a keystroke echo currently displayed in the echo
2160 area.
2162 Note: this function may only be called with HELP nil or a string
2163 from X code running asynchronously. */
2165 void
2166 show_help_echo (help, window, object, pos, ok_to_overwrite_keystroke_echo)
2167 Lisp_Object help, window, object, pos;
2168 int ok_to_overwrite_keystroke_echo;
2170 if (!NILP (help) && !STRINGP (help))
2172 if (FUNCTIONP (help))
2174 Lisp_Object args[4];
2175 args[0] = help;
2176 args[1] = window;
2177 args[2] = object;
2178 args[3] = pos;
2179 help = safe_call (4, args);
2181 else
2182 help = safe_eval (help);
2184 if (!STRINGP (help))
2185 return;
2188 if (STRINGP (help) || NILP (help))
2190 if (!NILP (Vshow_help_function))
2191 call1 (Vshow_help_function, help);
2192 else if (/* Don't overwrite minibuffer contents. */
2193 !MINI_WINDOW_P (XWINDOW (selected_window))
2194 /* Don't overwrite a keystroke echo. */
2195 && (NILP (echo_message_buffer)
2196 || ok_to_overwrite_keystroke_echo)
2197 /* Don't overwrite a prompt. */
2198 && !cursor_in_echo_area)
2200 if (STRINGP (help))
2202 int count = SPECPDL_INDEX ();
2204 if (!help_echo_showing_p)
2205 Vpre_help_message = current_message ();
2207 specbind (Qmessage_truncate_lines, Qt);
2208 message3_nolog (help, SBYTES (help),
2209 STRING_MULTIBYTE (help));
2210 unbind_to (count, Qnil);
2212 else if (STRINGP (Vpre_help_message))
2214 message3_nolog (Vpre_help_message,
2215 SBYTES (Vpre_help_message),
2216 STRING_MULTIBYTE (Vpre_help_message));
2217 Vpre_help_message = Qnil;
2219 else
2220 message (0);
2223 help_echo_showing_p = STRINGP (help);
2229 /* Input of single characters from keyboard */
2231 Lisp_Object print_help ();
2232 static Lisp_Object kbd_buffer_get_event ();
2233 static void record_char ();
2235 #ifdef MULTI_KBOARD
2236 static jmp_buf wrong_kboard_jmpbuf;
2237 #endif
2239 #define STOP_POLLING \
2240 do { if (! polling_stopped_here) stop_polling (); \
2241 polling_stopped_here = 1; } while (0)
2243 #define RESUME_POLLING \
2244 do { if (polling_stopped_here) start_polling (); \
2245 polling_stopped_here = 0; } while (0)
2247 /* read a character from the keyboard; call the redisplay if needed */
2248 /* commandflag 0 means do not do auto-saving, but do do redisplay.
2249 -1 means do not do redisplay, but do do autosaving.
2250 1 means do both. */
2252 /* The arguments MAPS and NMAPS are for menu prompting.
2253 MAPS is an array of keymaps; NMAPS is the length of MAPS.
2255 PREV_EVENT is the previous input event, or nil if we are reading
2256 the first event of a key sequence (or not reading a key sequence).
2257 If PREV_EVENT is t, that is a "magic" value that says
2258 not to run input methods, but in other respects to act as if
2259 not reading a key sequence.
2261 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
2262 if we used a mouse menu to read the input, or zero otherwise. If
2263 USED_MOUSE_MENU is null, we don't dereference it.
2265 Value is t if we showed a menu and the user rejected it. */
2267 Lisp_Object
2268 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
2269 int commandflag;
2270 int nmaps;
2271 Lisp_Object *maps;
2272 Lisp_Object prev_event;
2273 int *used_mouse_menu;
2275 volatile Lisp_Object c;
2276 int count;
2277 jmp_buf local_getcjmp;
2278 jmp_buf save_jump;
2279 volatile int key_already_recorded = 0;
2280 Lisp_Object tem, save;
2281 volatile Lisp_Object previous_echo_area_message;
2282 volatile Lisp_Object also_record;
2283 volatile int reread;
2284 struct gcpro gcpro1, gcpro2;
2285 EMACS_TIME last_idle_start;
2286 int polling_stopped_here = 0;
2288 also_record = Qnil;
2290 before_command_key_count = this_command_key_count;
2291 before_command_echo_length = echo_length ();
2292 c = Qnil;
2293 previous_echo_area_message = Qnil;
2295 GCPRO2 (c, previous_echo_area_message);
2297 retry:
2299 reread = 0;
2300 if (CONSP (Vunread_post_input_method_events))
2302 c = XCAR (Vunread_post_input_method_events);
2303 Vunread_post_input_method_events
2304 = XCDR (Vunread_post_input_method_events);
2306 /* Undo what read_char_x_menu_prompt did when it unread
2307 additional keys returned by Fx_popup_menu. */
2308 if (CONSP (c)
2309 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2310 && NILP (XCDR (c)))
2311 c = XCAR (c);
2313 reread = 1;
2314 goto reread_first;
2317 if (unread_command_char != -1)
2319 XSETINT (c, unread_command_char);
2320 unread_command_char = -1;
2322 reread = 1;
2323 goto reread_first;
2326 if (CONSP (Vunread_command_events))
2328 c = XCAR (Vunread_command_events);
2329 Vunread_command_events = XCDR (Vunread_command_events);
2331 /* Undo what read_char_x_menu_prompt did when it unread
2332 additional keys returned by Fx_popup_menu. */
2333 if (CONSP (c)
2334 && EQ (XCDR (c), Qdisabled)
2335 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c))))
2336 c = XCAR (c);
2338 /* If the queued event is something that used the mouse,
2339 set used_mouse_menu accordingly. */
2340 if (used_mouse_menu
2341 && (EQ (c, Qtool_bar) || EQ (c, Qmenu_bar)))
2342 *used_mouse_menu = 1;
2344 reread = 1;
2345 goto reread_for_input_method;
2348 if (CONSP (Vunread_input_method_events))
2350 c = XCAR (Vunread_input_method_events);
2351 Vunread_input_method_events = XCDR (Vunread_input_method_events);
2353 /* Undo what read_char_x_menu_prompt did when it unread
2354 additional keys returned by Fx_popup_menu. */
2355 if (CONSP (c)
2356 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2357 && NILP (XCDR (c)))
2358 c = XCAR (c);
2359 reread = 1;
2360 goto reread_for_input_method;
2363 /* If there is no function key translated before
2364 reset-this-command-lengths takes effect, forget about it. */
2365 before_command_restore_flag = 0;
2367 if (!NILP (Vexecuting_macro))
2369 /* We set this to Qmacro; since that's not a frame, nobody will
2370 try to switch frames on us, and the selected window will
2371 remain unchanged.
2373 Since this event came from a macro, it would be misleading to
2374 leave internal_last_event_frame set to wherever the last
2375 real event came from. Normally, a switch-frame event selects
2376 internal_last_event_frame after each command is read, but
2377 events read from a macro should never cause a new frame to be
2378 selected. */
2379 Vlast_event_frame = internal_last_event_frame = Qmacro;
2381 /* Exit the macro if we are at the end.
2382 Also, some things replace the macro with t
2383 to force an early exit. */
2384 if (EQ (Vexecuting_macro, Qt)
2385 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
2387 XSETINT (c, -1);
2388 goto exit;
2391 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
2392 if (STRINGP (Vexecuting_macro)
2393 && (XINT (c) & 0x80))
2394 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
2396 executing_macro_index++;
2398 goto from_macro;
2401 if (!NILP (unread_switch_frame))
2403 c = unread_switch_frame;
2404 unread_switch_frame = Qnil;
2406 /* This event should make it into this_command_keys, and get echoed
2407 again, so we do not set `reread'. */
2408 goto reread_first;
2411 /* if redisplay was requested */
2412 if (commandflag >= 0)
2414 /* If there is pending input, process any events which are not
2415 user-visible, such as X selection_request events. */
2416 if (input_pending
2417 || detect_input_pending_run_timers (0))
2418 swallow_events (0); /* may clear input_pending */
2420 /* Redisplay if no pending input. */
2421 while (!input_pending)
2423 if (help_echo_showing_p && !EQ (selected_window, minibuf_window))
2424 redisplay_preserve_echo_area (5);
2425 else
2426 redisplay ();
2428 if (!input_pending)
2429 /* Normal case: no input arrived during redisplay. */
2430 break;
2432 /* Input arrived and pre-empted redisplay.
2433 Process any events which are not user-visible. */
2434 swallow_events (0);
2435 /* If that cleared input_pending, try again to redisplay. */
2439 /* Message turns off echoing unless more keystrokes turn it on again.
2441 The code in 20.x for the condition was
2443 1. echo_area_glyphs && *echo_area_glyphs
2444 2. && echo_area_glyphs != current_kboard->echobuf
2445 3. && ok_to_echo_at_next_pause != echo_area_glyphs
2447 (1) means there's a current message displayed
2449 (2) means it's not the message from echoing from the current
2450 kboard.
2452 (3) There's only one place in 20.x where ok_to_echo_at_next_pause
2453 is set to a non-null value. This is done in read_char and it is
2454 set to echo_area_glyphs after a call to echo_char. That means
2455 ok_to_echo_at_next_pause is either null or
2456 current_kboard->echobuf with the appropriate current_kboard at
2457 that time.
2459 So, condition (3) means in clear text ok_to_echo_at_next_pause
2460 must be either null, or the current message isn't from echoing at
2461 all, or it's from echoing from a different kboard than the
2462 current one. */
2464 if (/* There currently is something in the echo area. */
2465 !NILP (echo_area_buffer[0])
2466 && (/* And it's either not from echoing. */
2467 !EQ (echo_area_buffer[0], echo_message_buffer)
2468 /* Or it's an echo from a different kboard. */
2469 || echo_kboard != current_kboard
2470 /* Or we explicitly allow overwriting whatever there is. */
2471 || ok_to_echo_at_next_pause == NULL))
2472 cancel_echoing ();
2473 else
2474 echo_dash ();
2476 /* Try reading a character via menu prompting in the minibuf.
2477 Try this before the sit-for, because the sit-for
2478 would do the wrong thing if we are supposed to do
2479 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
2480 after a mouse event so don't try a minibuf menu. */
2481 c = Qnil;
2482 if (nmaps > 0 && INTERACTIVE
2483 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
2484 /* Don't bring up a menu if we already have another event. */
2485 && NILP (Vunread_command_events)
2486 && unread_command_char < 0
2487 && !detect_input_pending_run_timers (0))
2489 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
2490 if (! NILP (c))
2492 key_already_recorded = 1;
2493 goto non_reread_1;
2497 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
2498 We will do that below, temporarily for short sections of code,
2499 when appropriate. local_getcjmp must be in effect
2500 around any call to sit_for or kbd_buffer_get_event;
2501 it *must not* be in effect when we call redisplay. */
2503 if (_setjmp (local_getcjmp))
2505 XSETINT (c, quit_char);
2506 internal_last_event_frame = selected_frame;
2507 Vlast_event_frame = internal_last_event_frame;
2508 /* If we report the quit char as an event,
2509 don't do so more than once. */
2510 if (!NILP (Vinhibit_quit))
2511 Vquit_flag = Qnil;
2513 #ifdef MULTI_KBOARD
2515 KBOARD *kb = FRAME_KBOARD (XFRAME (selected_frame));
2516 if (kb != current_kboard)
2518 Lisp_Object link = kb->kbd_queue;
2519 /* We shouldn't get here if we were in single-kboard mode! */
2520 if (single_kboard)
2521 abort ();
2522 if (CONSP (link))
2524 while (CONSP (XCDR (link)))
2525 link = XCDR (link);
2526 if (!NILP (XCDR (link)))
2527 abort ();
2529 if (!CONSP (link))
2530 kb->kbd_queue = Fcons (c, Qnil);
2531 else
2532 XSETCDR (link, Fcons (c, Qnil));
2533 kb->kbd_queue_has_data = 1;
2534 current_kboard = kb;
2535 /* This is going to exit from read_char
2536 so we had better get rid of this frame's stuff. */
2537 UNGCPRO;
2538 longjmp (wrong_kboard_jmpbuf, 1);
2541 #endif
2542 goto non_reread;
2545 timer_start_idle ();
2547 /* If in middle of key sequence and minibuffer not active,
2548 start echoing if enough time elapses. */
2550 if (minibuf_level == 0
2551 && !current_kboard->immediate_echo
2552 && this_command_key_count > 0
2553 && ! noninteractive
2554 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2555 && NILP (Fzerop (Vecho_keystrokes))
2556 && (/* No message. */
2557 NILP (echo_area_buffer[0])
2558 /* Or empty message. */
2559 || (BUF_BEG (XBUFFER (echo_area_buffer[0]))
2560 == BUF_Z (XBUFFER (echo_area_buffer[0])))
2561 /* Or already echoing from same kboard. */
2562 || (echo_kboard && ok_to_echo_at_next_pause == echo_kboard)
2563 /* Or not echoing before and echoing allowed. */
2564 || (!echo_kboard && ok_to_echo_at_next_pause)))
2566 Lisp_Object tem0;
2568 /* After a mouse event, start echoing right away.
2569 This is because we are probably about to display a menu,
2570 and we don't want to delay before doing so. */
2571 if (EVENT_HAS_PARAMETERS (prev_event))
2572 echo_now ();
2573 else
2575 int sec, usec;
2576 double duration = extract_float (Vecho_keystrokes);
2577 sec = (int) duration;
2578 usec = (duration - sec) * 1000000;
2579 save_getcjmp (save_jump);
2580 restore_getcjmp (local_getcjmp);
2581 tem0 = sit_for (sec, usec, 1, 1, 0);
2582 restore_getcjmp (save_jump);
2583 if (EQ (tem0, Qt)
2584 && ! CONSP (Vunread_command_events))
2585 echo_now ();
2589 /* Maybe auto save due to number of keystrokes. */
2591 if (commandflag != 0
2592 && auto_save_interval > 0
2593 && num_nonmacro_input_events - last_auto_save > max (auto_save_interval, 20)
2594 && !detect_input_pending_run_timers (0))
2596 Fdo_auto_save (Qnil, Qnil);
2597 /* Hooks can actually change some buffers in auto save. */
2598 redisplay ();
2601 /* Try reading using an X menu.
2602 This is never confused with reading using the minibuf
2603 because the recursive call of read_char in read_char_minibuf_menu_prompt
2604 does not pass on any keymaps. */
2606 if (nmaps > 0 && INTERACTIVE
2607 && !NILP (prev_event)
2608 && EVENT_HAS_PARAMETERS (prev_event)
2609 && !EQ (XCAR (prev_event), Qmenu_bar)
2610 && !EQ (XCAR (prev_event), Qtool_bar)
2611 /* Don't bring up a menu if we already have another event. */
2612 && NILP (Vunread_command_events)
2613 && unread_command_char < 0)
2615 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
2617 /* Now that we have read an event, Emacs is not idle. */
2618 timer_stop_idle ();
2620 goto exit;
2623 /* Maybe autosave and/or garbage collect due to idleness. */
2625 if (INTERACTIVE && NILP (c))
2627 int delay_level, buffer_size;
2629 /* Slow down auto saves logarithmically in size of current buffer,
2630 and garbage collect while we're at it. */
2631 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
2632 last_non_minibuf_size = Z - BEG;
2633 buffer_size = (last_non_minibuf_size >> 8) + 1;
2634 delay_level = 0;
2635 while (buffer_size > 64)
2636 delay_level++, buffer_size -= buffer_size >> 2;
2637 if (delay_level < 4) delay_level = 4;
2638 /* delay_level is 4 for files under around 50k, 7 at 100k,
2639 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
2641 /* Auto save if enough time goes by without input. */
2642 if (commandflag != 0
2643 && num_nonmacro_input_events > last_auto_save
2644 && INTEGERP (Vauto_save_timeout)
2645 && XINT (Vauto_save_timeout) > 0)
2647 Lisp_Object tem0;
2649 save_getcjmp (save_jump);
2650 restore_getcjmp (local_getcjmp);
2651 tem0 = sit_for (delay_level * XFASTINT (Vauto_save_timeout) / 4,
2652 0, 1, 1, 0);
2653 restore_getcjmp (save_jump);
2655 if (EQ (tem0, Qt)
2656 && ! CONSP (Vunread_command_events))
2658 Fdo_auto_save (Qnil, Qnil);
2660 /* If we have auto-saved and there is still no input
2661 available, garbage collect if there has been enough
2662 consing going on to make it worthwhile. */
2663 if (!detect_input_pending_run_timers (0)
2664 && consing_since_gc > gc_cons_threshold / 2)
2665 Fgarbage_collect ();
2667 redisplay ();
2672 /* If this has become non-nil here, it has been set by a timer
2673 or sentinel or filter. */
2674 if (CONSP (Vunread_command_events))
2676 c = XCAR (Vunread_command_events);
2677 Vunread_command_events = XCDR (Vunread_command_events);
2680 /* Read something from current KBOARD's side queue, if possible. */
2682 if (NILP (c))
2684 if (current_kboard->kbd_queue_has_data)
2686 if (!CONSP (current_kboard->kbd_queue))
2687 abort ();
2688 c = XCAR (current_kboard->kbd_queue);
2689 current_kboard->kbd_queue
2690 = XCDR (current_kboard->kbd_queue);
2691 if (NILP (current_kboard->kbd_queue))
2692 current_kboard->kbd_queue_has_data = 0;
2693 input_pending = readable_events (0);
2694 if (EVENT_HAS_PARAMETERS (c)
2695 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
2696 internal_last_event_frame = XCAR (XCDR (c));
2697 Vlast_event_frame = internal_last_event_frame;
2701 #ifdef MULTI_KBOARD
2702 /* If current_kboard's side queue is empty check the other kboards.
2703 If one of them has data that we have not yet seen here,
2704 switch to it and process the data waiting for it.
2706 Note: if the events queued up for another kboard
2707 have already been seen here, and therefore are not a complete command,
2708 the kbd_queue_has_data field is 0, so we skip that kboard here.
2709 That's to avoid an infinite loop switching between kboards here. */
2710 if (NILP (c) && !single_kboard)
2712 KBOARD *kb;
2713 for (kb = all_kboards; kb; kb = kb->next_kboard)
2714 if (kb->kbd_queue_has_data)
2716 current_kboard = kb;
2717 /* This is going to exit from read_char
2718 so we had better get rid of this frame's stuff. */
2719 UNGCPRO;
2720 longjmp (wrong_kboard_jmpbuf, 1);
2723 #endif
2725 wrong_kboard:
2727 STOP_POLLING;
2729 /* Finally, we read from the main queue,
2730 and if that gives us something we can't use yet, we put it on the
2731 appropriate side queue and try again. */
2733 if (NILP (c))
2735 KBOARD *kb;
2737 /* Actually read a character, waiting if necessary. */
2738 save_getcjmp (save_jump);
2739 restore_getcjmp (local_getcjmp);
2740 timer_start_idle ();
2741 c = kbd_buffer_get_event (&kb, used_mouse_menu);
2742 restore_getcjmp (save_jump);
2744 #ifdef MULTI_KBOARD
2745 if (! NILP (c) && (kb != current_kboard))
2747 Lisp_Object link = kb->kbd_queue;
2748 if (CONSP (link))
2750 while (CONSP (XCDR (link)))
2751 link = XCDR (link);
2752 if (!NILP (XCDR (link)))
2753 abort ();
2755 if (!CONSP (link))
2756 kb->kbd_queue = Fcons (c, Qnil);
2757 else
2758 XSETCDR (link, Fcons (c, Qnil));
2759 kb->kbd_queue_has_data = 1;
2760 c = Qnil;
2761 if (single_kboard)
2762 goto wrong_kboard;
2763 current_kboard = kb;
2764 /* This is going to exit from read_char
2765 so we had better get rid of this frame's stuff. */
2766 UNGCPRO;
2767 longjmp (wrong_kboard_jmpbuf, 1);
2769 #endif
2772 /* Terminate Emacs in batch mode if at eof. */
2773 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
2774 Fkill_emacs (make_number (1));
2776 if (INTEGERP (c))
2778 /* Add in any extra modifiers, where appropriate. */
2779 if ((extra_keyboard_modifiers & CHAR_CTL)
2780 || ((extra_keyboard_modifiers & 0177) < ' '
2781 && (extra_keyboard_modifiers & 0177) != 0))
2782 XSETINT (c, make_ctrl_char (XINT (c)));
2784 /* Transfer any other modifier bits directly from
2785 extra_keyboard_modifiers to c. Ignore the actual character code
2786 in the low 16 bits of extra_keyboard_modifiers. */
2787 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
2790 non_reread:
2792 /* Record the last idle start time so that we can reset it
2793 should the next event read be a help-echo. */
2794 last_idle_start = timer_idleness_start_time;
2795 timer_stop_idle ();
2796 RESUME_POLLING;
2798 if (NILP (c))
2800 if (commandflag >= 0
2801 && !input_pending && !detect_input_pending_run_timers (0))
2802 redisplay ();
2804 goto wrong_kboard;
2807 non_reread_1:
2809 /* Buffer switch events are only for internal wakeups
2810 so don't show them to the user.
2811 Also, don't record a key if we already did. */
2812 if (BUFFERP (c) || key_already_recorded)
2813 goto exit;
2815 /* Process special events within read_char
2816 and loop around to read another event. */
2817 save = Vquit_flag;
2818 Vquit_flag = Qnil;
2819 tem = access_keymap (get_keymap (Vspecial_event_map, 0, 1), c, 0, 0, 1);
2820 Vquit_flag = save;
2822 if (!NILP (tem))
2824 int was_locked = single_kboard;
2826 last_input_char = c;
2827 Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt);
2829 if (CONSP (c) && EQ (XCAR (c), Qselect_window))
2830 /* We stopped being idle for this event; undo that. This
2831 prevents automatic window selection (under
2832 mouse_autoselect_window from acting as a real input event, for
2833 example banishing the mouse under mouse-avoidance-mode. */
2834 timer_idleness_start_time = last_idle_start;
2836 /* Resume allowing input from any kboard, if that was true before. */
2837 if (!was_locked)
2838 any_kboard_state ();
2840 goto retry;
2843 /* Handle things that only apply to characters. */
2844 if (INTEGERP (c))
2846 /* If kbd_buffer_get_event gave us an EOF, return that. */
2847 if (XINT (c) == -1)
2848 goto exit;
2850 if ((STRINGP (Vkeyboard_translate_table)
2851 && SCHARS (Vkeyboard_translate_table) > (unsigned) XFASTINT (c))
2852 || (VECTORP (Vkeyboard_translate_table)
2853 && XVECTOR (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2854 || (CHAR_TABLE_P (Vkeyboard_translate_table)
2855 && CHAR_VALID_P (XINT (c), 0)))
2857 Lisp_Object d;
2858 d = Faref (Vkeyboard_translate_table, c);
2859 /* nil in keyboard-translate-table means no translation. */
2860 if (!NILP (d))
2861 c = d;
2865 /* If this event is a mouse click in the menu bar,
2866 return just menu-bar for now. Modify the mouse click event
2867 so we won't do this twice, then queue it up. */
2868 if (EVENT_HAS_PARAMETERS (c)
2869 && CONSP (XCDR (c))
2870 && CONSP (EVENT_START (c))
2871 && CONSP (XCDR (EVENT_START (c))))
2873 Lisp_Object posn;
2875 posn = POSN_BUFFER_POSN (EVENT_START (c));
2876 /* Handle menu-bar events:
2877 insert the dummy prefix event `menu-bar'. */
2878 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
2880 /* Change menu-bar to (menu-bar) as the event "position". */
2881 POSN_BUFFER_SET_POSN (EVENT_START (c), Fcons (posn, Qnil));
2883 also_record = c;
2884 Vunread_command_events = Fcons (c, Vunread_command_events);
2885 c = posn;
2889 /* Store these characters into recent_keys, the dribble file if any,
2890 and the keyboard macro being defined, if any. */
2891 record_char (c);
2892 if (! NILP (also_record))
2893 record_char (also_record);
2895 /* Wipe the echo area.
2896 But first, if we are about to use an input method,
2897 save the echo area contents for it to refer to. */
2898 if (INTEGERP (c)
2899 && ! NILP (Vinput_method_function)
2900 && (unsigned) XINT (c) >= ' '
2901 && (unsigned) XINT (c) != 127
2902 && (unsigned) XINT (c) < 256)
2904 previous_echo_area_message = Fcurrent_message ();
2905 Vinput_method_previous_message = previous_echo_area_message;
2908 /* Now wipe the echo area, except for help events which do their
2909 own stuff with the echo area. */
2910 if (!CONSP (c)
2911 || (!(EQ (Qhelp_echo, XCAR (c)))
2912 && !(EQ (Qswitch_frame, XCAR (c)))))
2914 if (!NILP (echo_area_buffer[0]))
2915 safe_run_hooks (Qecho_area_clear_hook);
2916 clear_message (1, 0);
2919 reread_for_input_method:
2920 from_macro:
2921 /* Pass this to the input method, if appropriate. */
2922 if (INTEGERP (c)
2923 && ! NILP (Vinput_method_function)
2924 /* Don't run the input method within a key sequence,
2925 after the first event of the key sequence. */
2926 && NILP (prev_event)
2927 && (unsigned) XINT (c) >= ' '
2928 && (unsigned) XINT (c) != 127
2929 && (unsigned) XINT (c) < 256)
2931 Lisp_Object keys;
2932 int key_count;
2933 struct gcpro gcpro1;
2934 int count = SPECPDL_INDEX ();
2936 /* Save the echo status. */
2937 int saved_immediate_echo = current_kboard->immediate_echo;
2938 struct kboard *saved_ok_to_echo = ok_to_echo_at_next_pause;
2939 int saved_echo_after_prompt = current_kboard->echo_after_prompt;
2941 if (before_command_restore_flag)
2943 this_command_key_count = before_command_key_count_1;
2944 if (this_command_key_count < this_single_command_key_start)
2945 this_single_command_key_start = this_command_key_count;
2946 echo_truncate (before_command_echo_length_1);
2947 before_command_restore_flag = 0;
2950 /* Save the this_command_keys status. */
2951 key_count = this_command_key_count;
2953 if (key_count > 0)
2954 keys = Fcopy_sequence (this_command_keys);
2955 else
2956 keys = Qnil;
2957 GCPRO1 (keys);
2959 /* Clear out this_command_keys. */
2960 this_command_key_count = 0;
2962 /* Now wipe the echo area. */
2963 if (!NILP (echo_area_buffer[0]))
2964 safe_run_hooks (Qecho_area_clear_hook);
2965 clear_message (1, 0);
2966 echo_truncate (0);
2968 /* If we are not reading a key sequence,
2969 never use the echo area. */
2970 if (maps == 0)
2972 specbind (Qinput_method_use_echo_area, Qt);
2975 /* Call the input method. */
2976 tem = call1 (Vinput_method_function, c);
2978 tem = unbind_to (count, tem);
2980 /* Restore the saved echoing state
2981 and this_command_keys state. */
2982 this_command_key_count = key_count;
2983 if (key_count > 0)
2984 this_command_keys = keys;
2986 cancel_echoing ();
2987 ok_to_echo_at_next_pause = saved_ok_to_echo;
2988 current_kboard->echo_after_prompt = saved_echo_after_prompt;
2989 if (saved_immediate_echo)
2990 echo_now ();
2992 UNGCPRO;
2994 /* The input method can return no events. */
2995 if (! CONSP (tem))
2997 /* Bring back the previous message, if any. */
2998 if (! NILP (previous_echo_area_message))
2999 message_with_string ("%s", previous_echo_area_message, 0);
3000 goto retry;
3002 /* It returned one event or more. */
3003 c = XCAR (tem);
3004 Vunread_post_input_method_events
3005 = nconc2 (XCDR (tem), Vunread_post_input_method_events);
3008 reread_first:
3010 /* Display help if not echoing. */
3011 if (CONSP (c) && EQ (XCAR (c), Qhelp_echo))
3013 /* (help-echo FRAME HELP WINDOW OBJECT POS). */
3014 Lisp_Object help, object, position, window, tem;
3016 tem = Fcdr (XCDR (c));
3017 help = Fcar (tem);
3018 tem = Fcdr (tem);
3019 window = Fcar (tem);
3020 tem = Fcdr (tem);
3021 object = Fcar (tem);
3022 tem = Fcdr (tem);
3023 position = Fcar (tem);
3025 show_help_echo (help, window, object, position, 0);
3027 /* We stopped being idle for this event; undo that. */
3028 timer_idleness_start_time = last_idle_start;
3029 goto retry;
3032 if (this_command_key_count == 0 || ! reread)
3034 before_command_key_count = this_command_key_count;
3035 before_command_echo_length = echo_length ();
3037 /* Don't echo mouse motion events. */
3038 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
3039 && NILP (Fzerop (Vecho_keystrokes))
3040 && ! (EVENT_HAS_PARAMETERS (c)
3041 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
3043 echo_char (c);
3044 if (! NILP (also_record))
3045 echo_char (also_record);
3046 /* Once we reread a character, echoing can happen
3047 the next time we pause to read a new one. */
3048 ok_to_echo_at_next_pause = current_kboard;
3051 /* Record this character as part of the current key. */
3052 add_command_key (c);
3053 if (! NILP (also_record))
3054 add_command_key (also_record);
3057 last_input_char = c;
3058 num_input_events++;
3060 /* Process the help character specially if enabled */
3061 if (!NILP (Vhelp_form) && help_char_p (c))
3063 Lisp_Object tem0;
3064 count = SPECPDL_INDEX ();
3066 record_unwind_protect (Fset_window_configuration,
3067 Fcurrent_window_configuration (Qnil));
3069 tem0 = Feval (Vhelp_form);
3070 if (STRINGP (tem0))
3071 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
3073 cancel_echoing ();
3075 c = read_char (0, 0, 0, Qnil, 0);
3076 while (BUFFERP (c));
3077 /* Remove the help from the frame */
3078 unbind_to (count, Qnil);
3080 redisplay ();
3081 if (EQ (c, make_number (040)))
3083 cancel_echoing ();
3085 c = read_char (0, 0, 0, Qnil, 0);
3086 while (BUFFERP (c));
3090 exit:
3091 RESUME_POLLING;
3092 RETURN_UNGCPRO (c);
3095 /* Record a key that came from a mouse menu.
3096 Record it for echoing, for this-command-keys, and so on. */
3098 static void
3099 record_menu_key (c)
3100 Lisp_Object c;
3102 /* Wipe the echo area. */
3103 clear_message (1, 0);
3105 record_char (c);
3107 before_command_key_count = this_command_key_count;
3108 before_command_echo_length = echo_length ();
3110 /* Don't echo mouse motion events. */
3111 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
3112 && NILP (Fzerop (Vecho_keystrokes)))
3114 echo_char (c);
3116 /* Once we reread a character, echoing can happen
3117 the next time we pause to read a new one. */
3118 ok_to_echo_at_next_pause = 0;
3121 /* Record this character as part of the current key. */
3122 add_command_key (c);
3124 /* Re-reading in the middle of a command */
3125 last_input_char = c;
3126 num_input_events++;
3129 /* Return 1 if should recognize C as "the help character". */
3132 help_char_p (c)
3133 Lisp_Object c;
3135 Lisp_Object tail;
3137 if (EQ (c, Vhelp_char))
3138 return 1;
3139 for (tail = Vhelp_event_list; CONSP (tail); tail = XCDR (tail))
3140 if (EQ (c, XCAR (tail)))
3141 return 1;
3142 return 0;
3145 /* Record the input event C in various ways. */
3147 static void
3148 record_char (c)
3149 Lisp_Object c;
3151 int recorded = 0;
3153 if (CONSP (c) && (EQ (XCAR (c), Qhelp_echo) || EQ (XCAR (c), Qmouse_movement)))
3155 /* To avoid filling recent_keys with help-echo and mouse-movement
3156 events, we filter out repeated help-echo events, only store the
3157 first and last in a series of mouse-movement events, and don't
3158 store repeated help-echo events which are only separated by
3159 mouse-movement events. */
3161 Lisp_Object ev1, ev2, ev3;
3162 int ix1, ix2, ix3;
3164 if ((ix1 = recent_keys_index - 1) < 0)
3165 ix1 = NUM_RECENT_KEYS - 1;
3166 ev1 = AREF (recent_keys, ix1);
3168 if ((ix2 = ix1 - 1) < 0)
3169 ix2 = NUM_RECENT_KEYS - 1;
3170 ev2 = AREF (recent_keys, ix2);
3172 if ((ix3 = ix2 - 1) < 0)
3173 ix3 = NUM_RECENT_KEYS - 1;
3174 ev3 = AREF (recent_keys, ix3);
3176 if (EQ (XCAR (c), Qhelp_echo))
3178 /* Don't record `help-echo' in recent_keys unless it shows some help
3179 message, and a different help than the previously recorded
3180 event. */
3181 Lisp_Object help, last_help;
3183 help = Fcar_safe (Fcdr_safe (XCDR (c)));
3184 if (!STRINGP (help))
3185 recorded = 1;
3186 else if (CONSP (ev1) && EQ (XCAR (ev1), Qhelp_echo)
3187 && (last_help = Fcar_safe (Fcdr_safe (XCDR (ev1))), EQ (last_help, help)))
3188 recorded = 1;
3189 else if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
3190 && CONSP (ev2) && EQ (XCAR (ev2), Qhelp_echo)
3191 && (last_help = Fcar_safe (Fcdr_safe (XCDR (ev2))), EQ (last_help, help)))
3192 recorded = -1;
3193 else if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
3194 && CONSP (ev2) && EQ (XCAR (ev2), Qmouse_movement)
3195 && CONSP (ev3) && EQ (XCAR (ev3), Qhelp_echo)
3196 && (last_help = Fcar_safe (Fcdr_safe (XCDR (ev3))), EQ (last_help, help)))
3197 recorded = -2;
3199 else if (EQ (XCAR (c), Qmouse_movement))
3201 /* Only record one pair of `mouse-movement' on a window in recent_keys.
3202 So additional mouse movement events replace the last element. */
3203 Lisp_Object last_window, window;
3205 window = Fcar_safe (Fcar_safe (XCDR (c)));
3206 if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
3207 && (last_window = Fcar_safe (Fcar_safe (XCDR (ev1))), EQ (last_window, window))
3208 && CONSP (ev2) && EQ (XCAR (ev2), Qmouse_movement)
3209 && (last_window = Fcar_safe (Fcar_safe (XCDR (ev2))), EQ (last_window, window)))
3211 ASET (recent_keys, ix1, c);
3212 recorded = 1;
3216 else
3217 store_kbd_macro_char (c);
3219 if (!recorded)
3221 total_keys++;
3222 ASET (recent_keys, recent_keys_index, c);
3223 if (++recent_keys_index >= NUM_RECENT_KEYS)
3224 recent_keys_index = 0;
3226 else if (recorded < 0)
3228 /* We need to remove one or two events from recent_keys.
3229 To do this, we simply put nil at those events and move the
3230 recent_keys_index backwards over those events. Usually,
3231 users will never see those nil events, as they will be
3232 overwritten by the command keys entered to see recent_keys
3233 (e.g. C-h l). */
3235 while (recorded++ < 0 && total_keys > 0)
3237 if (total_keys < NUM_RECENT_KEYS)
3238 total_keys--;
3239 if (--recent_keys_index < 0)
3240 recent_keys_index = NUM_RECENT_KEYS - 1;
3241 ASET (recent_keys, recent_keys_index, Qnil);
3245 num_nonmacro_input_events++;
3247 /* Write c to the dribble file. If c is a lispy event, write
3248 the event's symbol to the dribble file, in <brackets>. Bleaugh.
3249 If you, dear reader, have a better idea, you've got the source. :-) */
3250 if (dribble)
3252 if (INTEGERP (c))
3254 if (XUINT (c) < 0x100)
3255 putc (XINT (c), dribble);
3256 else
3257 fprintf (dribble, " 0x%x", (int) XUINT (c));
3259 else
3261 Lisp_Object dribblee;
3263 /* If it's a structured event, take the event header. */
3264 dribblee = EVENT_HEAD (c);
3266 if (SYMBOLP (dribblee))
3268 putc ('<', dribble);
3269 fwrite (SDATA (SYMBOL_NAME (dribblee)), sizeof (char),
3270 SBYTES (SYMBOL_NAME (dribblee)),
3271 dribble);
3272 putc ('>', dribble);
3276 fflush (dribble);
3280 Lisp_Object
3281 print_help (object)
3282 Lisp_Object object;
3284 struct buffer *old = current_buffer;
3285 Fprinc (object, Qnil);
3286 set_buffer_internal (XBUFFER (Vstandard_output));
3287 call0 (intern ("help-mode"));
3288 set_buffer_internal (old);
3289 return Qnil;
3292 /* Copy out or in the info on where C-g should throw to.
3293 This is used when running Lisp code from within get_char,
3294 in case get_char is called recursively.
3295 See read_process_output. */
3297 static void
3298 save_getcjmp (temp)
3299 jmp_buf temp;
3301 bcopy (getcjmp, temp, sizeof getcjmp);
3304 static void
3305 restore_getcjmp (temp)
3306 jmp_buf temp;
3308 bcopy (temp, getcjmp, sizeof getcjmp);
3311 #ifdef HAVE_MOUSE
3313 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
3314 of this function. */
3316 static Lisp_Object
3317 tracking_off (old_value)
3318 Lisp_Object old_value;
3320 do_mouse_tracking = old_value;
3321 if (NILP (old_value))
3323 /* Redisplay may have been preempted because there was input
3324 available, and it assumes it will be called again after the
3325 input has been processed. If the only input available was
3326 the sort that we have just disabled, then we need to call
3327 redisplay. */
3328 if (!readable_events (1))
3330 redisplay_preserve_echo_area (6);
3331 get_input_pending (&input_pending, 1);
3334 return Qnil;
3337 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
3338 doc: /* Evaluate BODY with mouse movement events enabled.
3339 Within a `track-mouse' form, mouse motion generates input events that
3340 you can read with `read-event'.
3341 Normally, mouse motion is ignored.
3342 usage: (track-mouse BODY ...) */)
3343 (args)
3344 Lisp_Object args;
3346 int count = SPECPDL_INDEX ();
3347 Lisp_Object val;
3349 record_unwind_protect (tracking_off, do_mouse_tracking);
3351 do_mouse_tracking = Qt;
3353 val = Fprogn (args);
3354 return unbind_to (count, val);
3357 /* If mouse has moved on some frame, return one of those frames.
3358 Return 0 otherwise. */
3360 static FRAME_PTR
3361 some_mouse_moved ()
3363 Lisp_Object tail, frame;
3365 FOR_EACH_FRAME (tail, frame)
3367 if (XFRAME (frame)->mouse_moved)
3368 return XFRAME (frame);
3371 return 0;
3374 #endif /* HAVE_MOUSE */
3376 /* Low level keyboard/mouse input.
3377 kbd_buffer_store_event places events in kbd_buffer, and
3378 kbd_buffer_get_event retrieves them. */
3380 /* Return true iff there are any events in the queue that read-char
3381 would return. If this returns false, a read-char would block. */
3382 static int
3383 readable_filtered_events (do_timers_now, filter_events)
3384 int do_timers_now;
3385 int filter_events;
3387 if (do_timers_now)
3388 timer_check (do_timers_now);
3390 /* If the buffer contains only FOCUS_IN_EVENT events,
3391 and FILTER_EVENTS is nonzero, report it as empty. */
3392 if (kbd_fetch_ptr != kbd_store_ptr)
3394 int have_live_event = 1;
3396 if (filter_events)
3398 struct input_event *event;
3400 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3401 ? kbd_fetch_ptr
3402 : kbd_buffer);
3404 while (have_live_event && event->kind == FOCUS_IN_EVENT)
3406 event++;
3407 if (event == kbd_buffer + KBD_BUFFER_SIZE)
3408 event = kbd_buffer;
3409 if (event == kbd_store_ptr)
3410 have_live_event = 0;
3413 if (have_live_event) return 1;
3416 #ifdef HAVE_MOUSE
3417 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3418 return 1;
3419 #endif
3420 if (single_kboard)
3422 if (current_kboard->kbd_queue_has_data)
3423 return 1;
3425 else
3427 KBOARD *kb;
3428 for (kb = all_kboards; kb; kb = kb->next_kboard)
3429 if (kb->kbd_queue_has_data)
3430 return 1;
3432 return 0;
3435 /* Return true iff there are any events in the queue that read-char
3436 would return. If this returns false, a read-char would block. */
3437 static int
3438 readable_events (do_timers_now)
3439 int do_timers_now;
3441 return readable_filtered_events (do_timers_now, 0);
3444 /* Set this for debugging, to have a way to get out */
3445 int stop_character;
3447 #ifdef MULTI_KBOARD
3448 static KBOARD *
3449 event_to_kboard (event)
3450 struct input_event *event;
3452 Lisp_Object frame;
3453 frame = event->frame_or_window;
3454 if (CONSP (frame))
3455 frame = XCAR (frame);
3456 else if (WINDOWP (frame))
3457 frame = WINDOW_FRAME (XWINDOW (frame));
3459 /* There are still some events that don't set this field.
3460 For now, just ignore the problem.
3461 Also ignore dead frames here. */
3462 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
3463 return 0;
3464 else
3465 return FRAME_KBOARD (XFRAME (frame));
3467 #endif
3469 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
3471 void
3472 kbd_buffer_store_event (event)
3473 register struct input_event *event;
3475 if (event->kind == NO_EVENT)
3476 abort ();
3478 if (event->kind == ASCII_KEYSTROKE_EVENT)
3480 register int c = event->code & 0377;
3482 if (event->modifiers & ctrl_modifier)
3483 c = make_ctrl_char (c);
3485 c |= (event->modifiers
3486 & (meta_modifier | alt_modifier
3487 | hyper_modifier | super_modifier));
3489 if (c == quit_char)
3491 #ifdef MULTI_KBOARD
3492 KBOARD *kb;
3493 struct input_event *sp;
3495 if (single_kboard
3496 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
3497 kb != current_kboard))
3499 kb->kbd_queue
3500 = Fcons (make_lispy_switch_frame (event->frame_or_window),
3501 Fcons (make_number (c), Qnil));
3502 kb->kbd_queue_has_data = 1;
3503 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3505 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3506 sp = kbd_buffer;
3508 if (event_to_kboard (sp) == kb)
3510 sp->kind = NO_EVENT;
3511 sp->frame_or_window = Qnil;
3512 sp->arg = Qnil;
3515 return;
3517 #endif
3519 /* If this results in a quit_char being returned to Emacs as
3520 input, set Vlast_event_frame properly. If this doesn't
3521 get returned to Emacs as an event, the next event read
3522 will set Vlast_event_frame again, so this is safe to do. */
3524 Lisp_Object focus;
3526 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
3527 if (NILP (focus))
3528 focus = event->frame_or_window;
3529 internal_last_event_frame = focus;
3530 Vlast_event_frame = focus;
3533 last_event_timestamp = event->timestamp;
3534 interrupt_signal (0 /* dummy */);
3535 return;
3538 if (c && c == stop_character)
3540 sys_suspend ();
3541 return;
3544 /* Don't insert two BUFFER_SWITCH_EVENT's in a row.
3545 Just ignore the second one. */
3546 else if (event->kind == BUFFER_SWITCH_EVENT
3547 && kbd_fetch_ptr != kbd_store_ptr
3548 && kbd_store_ptr->kind == BUFFER_SWITCH_EVENT)
3549 return;
3551 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
3552 kbd_store_ptr = kbd_buffer;
3554 /* Don't let the very last slot in the buffer become full,
3555 since that would make the two pointers equal,
3556 and that is indistinguishable from an empty buffer.
3557 Discard the event if it would fill the last slot. */
3558 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
3560 int idx;
3562 #if 0 /* The SELECTION_REQUEST_EVENT case looks bogus, and it's error
3563 prone to assign individual members for other events, in case
3564 the input_event structure is changed. --2000-07-13, gerd. */
3565 struct input_event *sp = kbd_store_ptr;
3566 sp->kind = event->kind;
3567 if (event->kind == SELECTION_REQUEST_EVENT)
3569 /* We must not use the ordinary copying code for this case,
3570 since `part' is an enum and copying it might not copy enough
3571 in this case. */
3572 bcopy (event, (char *) sp, sizeof (*event));
3574 else
3577 sp->code = event->code;
3578 sp->part = event->part;
3579 sp->frame_or_window = event->frame_or_window;
3580 sp->arg = event->arg;
3581 sp->modifiers = event->modifiers;
3582 sp->x = event->x;
3583 sp->y = event->y;
3584 sp->timestamp = event->timestamp;
3586 #else
3587 *kbd_store_ptr = *event;
3588 #endif
3590 idx = 2 * (kbd_store_ptr - kbd_buffer);
3591 ASET (kbd_buffer_gcpro, idx, event->frame_or_window);
3592 ASET (kbd_buffer_gcpro, idx + 1, event->arg);
3593 ++kbd_store_ptr;
3598 /* Generate HELP_EVENT input_events in BUFP which has room for
3599 SIZE events. If there's not enough room in BUFP, ignore this
3600 event.
3602 HELP is the help form.
3604 FRAME is the frame on which the help is generated. OBJECT is the
3605 Lisp object where the help was found (a buffer, a string, an
3606 overlay, or nil if neither from a string nor from a buffer. POS is
3607 the position within OBJECT where the help was found.
3609 Value is the number of input_events generated. */
3612 gen_help_event (bufp, size, help, frame, window, object, pos)
3613 struct input_event *bufp;
3614 int size;
3615 Lisp_Object help, frame, object, window;
3616 int pos;
3618 if (size >= 1)
3620 bufp->kind = HELP_EVENT;
3621 bufp->frame_or_window = frame;
3622 bufp->arg = object;
3623 bufp->x = WINDOWP (window) ? window : frame;
3624 bufp->y = help;
3625 bufp->code = pos;
3626 return 1;
3628 return 0;
3632 /* Store HELP_EVENTs for HELP on FRAME in the input queue. */
3634 void
3635 kbd_buffer_store_help_event (frame, help)
3636 Lisp_Object frame, help;
3638 struct input_event event;
3640 event.kind = HELP_EVENT;
3641 event.frame_or_window = frame;
3642 event.arg = Qnil;
3643 event.x = Qnil;
3644 event.y = help;
3645 event.code = 0;
3646 kbd_buffer_store_event (&event);
3650 /* Discard any mouse events in the event buffer by setting them to
3651 NO_EVENT. */
3652 void
3653 discard_mouse_events ()
3655 struct input_event *sp;
3656 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3658 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3659 sp = kbd_buffer;
3661 if (sp->kind == MOUSE_CLICK_EVENT
3662 #ifdef WINDOWSNT
3663 || sp->kind == W32_SCROLL_BAR_CLICK_EVENT
3664 #endif
3665 || sp->kind == SCROLL_BAR_CLICK_EVENT)
3667 sp->kind = NO_EVENT;
3673 /* Return non-zero if there are any real events waiting in the event
3674 buffer, not counting `NO_EVENT's.
3676 If DISCARD is non-zero, discard NO_EVENT events at the front of
3677 the input queue, possibly leaving the input queue empty if there
3678 are no real input events. */
3681 kbd_buffer_events_waiting (discard)
3682 int discard;
3684 struct input_event *sp;
3686 for (sp = kbd_fetch_ptr;
3687 sp != kbd_store_ptr && sp->kind == NO_EVENT;
3688 ++sp)
3690 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3691 sp = kbd_buffer;
3694 if (discard)
3695 kbd_fetch_ptr = sp;
3697 return sp != kbd_store_ptr && sp->kind != NO_EVENT;
3701 /* Clear input event EVENT. */
3703 static INLINE void
3704 clear_event (event)
3705 struct input_event *event;
3707 int idx = 2 * (event - kbd_buffer);
3708 ASET (kbd_buffer_gcpro, idx, Qnil);
3709 ASET (kbd_buffer_gcpro, idx + 1, Qnil);
3710 event->kind = NO_EVENT;
3714 /* Read one event from the event buffer, waiting if necessary.
3715 The value is a Lisp object representing the event.
3716 The value is nil for an event that should be ignored,
3717 or that was handled here.
3718 We always read and discard one event. */
3720 static Lisp_Object
3721 kbd_buffer_get_event (kbp, used_mouse_menu)
3722 KBOARD **kbp;
3723 int *used_mouse_menu;
3725 register int c;
3726 Lisp_Object obj;
3728 if (noninteractive)
3730 c = getchar ();
3731 XSETINT (obj, c);
3732 *kbp = current_kboard;
3733 return obj;
3736 /* Wait until there is input available. */
3737 for (;;)
3739 if (kbd_fetch_ptr != kbd_store_ptr)
3740 break;
3741 #ifdef HAVE_MOUSE
3742 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3743 break;
3744 #endif
3746 /* If the quit flag is set, then read_char will return
3747 quit_char, so that counts as "available input." */
3748 if (!NILP (Vquit_flag))
3749 quit_throw_to_read_char ();
3751 /* One way or another, wait until input is available; then, if
3752 interrupt handlers have not read it, read it now. */
3754 #ifdef OLDVMS
3755 wait_for_kbd_input ();
3756 #else
3757 /* Note SIGIO has been undef'd if FIONREAD is missing. */
3758 #ifdef SIGIO
3759 gobble_input (0);
3760 #endif /* SIGIO */
3761 if (kbd_fetch_ptr != kbd_store_ptr)
3762 break;
3763 #ifdef HAVE_MOUSE
3764 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3765 break;
3766 #endif
3768 Lisp_Object minus_one;
3770 XSETINT (minus_one, -1);
3771 wait_reading_process_input (0, 0, minus_one, 1);
3773 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
3774 /* Pass 1 for EXPECT since we just waited to have input. */
3775 read_avail_input (1);
3777 #endif /* not VMS */
3780 if (CONSP (Vunread_command_events))
3782 Lisp_Object first;
3783 first = XCAR (Vunread_command_events);
3784 Vunread_command_events = XCDR (Vunread_command_events);
3785 *kbp = current_kboard;
3786 return first;
3789 /* At this point, we know that there is a readable event available
3790 somewhere. If the event queue is empty, then there must be a
3791 mouse movement enabled and available. */
3792 if (kbd_fetch_ptr != kbd_store_ptr)
3794 struct input_event *event;
3796 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3797 ? kbd_fetch_ptr
3798 : kbd_buffer);
3800 last_event_timestamp = event->timestamp;
3802 #ifdef MULTI_KBOARD
3803 *kbp = event_to_kboard (event);
3804 if (*kbp == 0)
3805 *kbp = current_kboard; /* Better than returning null ptr? */
3806 #else
3807 *kbp = &the_only_kboard;
3808 #endif
3810 obj = Qnil;
3812 /* These two kinds of events get special handling
3813 and don't actually appear to the command loop.
3814 We return nil for them. */
3815 if (event->kind == SELECTION_REQUEST_EVENT)
3817 #ifdef HAVE_X11
3818 struct input_event copy;
3820 /* Remove it from the buffer before processing it,
3821 since otherwise swallow_events will see it
3822 and process it again. */
3823 copy = *event;
3824 kbd_fetch_ptr = event + 1;
3825 input_pending = readable_events (0);
3826 x_handle_selection_request (&copy);
3827 #else
3828 /* We're getting selection request events, but we don't have
3829 a window system. */
3830 abort ();
3831 #endif
3834 else if (event->kind == SELECTION_CLEAR_EVENT)
3836 #ifdef HAVE_X11
3837 struct input_event copy;
3839 /* Remove it from the buffer before processing it. */
3840 copy = *event;
3841 kbd_fetch_ptr = event + 1;
3842 input_pending = readable_events (0);
3843 x_handle_selection_clear (&copy);
3844 #else
3845 /* We're getting selection request events, but we don't have
3846 a window system. */
3847 abort ();
3848 #endif
3850 #if defined (HAVE_X11) || defined (HAVE_NTGUI) || defined (MAC_OS)
3851 else if (event->kind == DELETE_WINDOW_EVENT)
3853 /* Make an event (delete-frame (FRAME)). */
3854 obj = Fcons (event->frame_or_window, Qnil);
3855 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
3856 kbd_fetch_ptr = event + 1;
3858 #endif
3859 #if defined (HAVE_X11) || defined (HAVE_NTGUI)
3860 else if (event->kind == ICONIFY_EVENT)
3862 /* Make an event (iconify-frame (FRAME)). */
3863 obj = Fcons (event->frame_or_window, Qnil);
3864 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
3865 kbd_fetch_ptr = event + 1;
3867 else if (event->kind == DEICONIFY_EVENT)
3869 /* Make an event (make-frame-visible (FRAME)). */
3870 obj = Fcons (event->frame_or_window, Qnil);
3871 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
3872 kbd_fetch_ptr = event + 1;
3874 #endif
3875 else if (event->kind == BUFFER_SWITCH_EVENT)
3877 /* The value doesn't matter here; only the type is tested. */
3878 XSETBUFFER (obj, current_buffer);
3879 kbd_fetch_ptr = event + 1;
3881 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
3882 || defined (USE_GTK)
3883 else if (event->kind == MENU_BAR_ACTIVATE_EVENT)
3885 kbd_fetch_ptr = event + 1;
3886 input_pending = readable_events (0);
3887 if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
3888 x_activate_menubar (XFRAME (event->frame_or_window));
3890 #endif
3891 #ifdef WINDOWSNT
3892 else if (event->kind == LANGUAGE_CHANGE_EVENT)
3894 /* Make an event (language-change (FRAME CHARSET LCID)). */
3895 obj = Fcons (event->modifiers, Qnil);
3896 obj = Fcons (event->code, obj);
3897 obj = Fcons (event->frame_or_window, obj);
3898 obj = Fcons (Qlanguage_change, Fcons (obj, Qnil));
3899 kbd_fetch_ptr = event + 1;
3901 #endif
3902 else if (event->kind == SAVE_SESSION_EVENT)
3904 obj = Fcons (Qsave_session, Qnil);
3905 kbd_fetch_ptr = event + 1;
3907 /* Just discard these, by returning nil.
3908 With MULTI_KBOARD, these events are used as placeholders
3909 when we need to randomly delete events from the queue.
3910 (They shouldn't otherwise be found in the buffer,
3911 but on some machines it appears they do show up
3912 even without MULTI_KBOARD.) */
3913 /* On Windows NT/9X, NO_EVENT is used to delete extraneous
3914 mouse events during a popup-menu call. */
3915 else if (event->kind == NO_EVENT)
3916 kbd_fetch_ptr = event + 1;
3917 else if (event->kind == HELP_EVENT)
3919 Lisp_Object object, position, help, frame, window;
3921 frame = event->frame_or_window;
3922 object = event->arg;
3923 position = make_number (event->code);
3924 window = event->x;
3925 help = event->y;
3926 clear_event (event);
3928 kbd_fetch_ptr = event + 1;
3929 if (!WINDOWP (window))
3930 window = Qnil;
3931 obj = Fcons (Qhelp_echo,
3932 list5 (frame, help, window, object, position));
3934 else if (event->kind == FOCUS_IN_EVENT)
3936 /* Notification of a FocusIn event. The frame receiving the
3937 focus is in event->frame_or_window. Generate a
3938 switch-frame event if necessary. */
3939 Lisp_Object frame, focus;
3941 frame = event->frame_or_window;
3942 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
3943 if (FRAMEP (focus))
3944 frame = focus;
3946 if (!EQ (frame, internal_last_event_frame)
3947 && !EQ (frame, selected_frame))
3948 obj = make_lispy_switch_frame (frame);
3949 internal_last_event_frame = frame;
3950 kbd_fetch_ptr = event + 1;
3952 else if (event->kind == SELECT_WINDOW_EVENT)
3954 /* Make an event (select-window (WINDOW)). */
3955 obj = Fcons (event->frame_or_window, Qnil);
3956 obj = Fcons (Qselect_window, Fcons (obj, Qnil));
3958 kbd_fetch_ptr = event + 1;
3960 else
3962 /* If this event is on a different frame, return a switch-frame this
3963 time, and leave the event in the queue for next time. */
3964 Lisp_Object frame;
3965 Lisp_Object focus;
3967 frame = event->frame_or_window;
3968 if (CONSP (frame))
3969 frame = XCAR (frame);
3970 else if (WINDOWP (frame))
3971 frame = WINDOW_FRAME (XWINDOW (frame));
3973 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
3974 if (! NILP (focus))
3975 frame = focus;
3977 if (! EQ (frame, internal_last_event_frame)
3978 && !EQ (frame, selected_frame))
3979 obj = make_lispy_switch_frame (frame);
3980 internal_last_event_frame = frame;
3982 /* If we didn't decide to make a switch-frame event, go ahead
3983 and build a real event from the queue entry. */
3985 if (NILP (obj))
3987 obj = make_lispy_event (event);
3989 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined(MAC_OS) \
3990 || defined (USE_GTK)
3991 /* If this was a menu selection, then set the flag to inhibit
3992 writing to last_nonmenu_event. Don't do this if the event
3993 we're returning is (menu-bar), though; that indicates the
3994 beginning of the menu sequence, and we might as well leave
3995 that as the `event with parameters' for this selection. */
3996 if (used_mouse_menu
3997 && !EQ (event->frame_or_window, event->arg)
3998 && (event->kind == MENU_BAR_EVENT
3999 || event->kind == TOOL_BAR_EVENT))
4000 *used_mouse_menu = 1;
4001 #endif
4003 /* Wipe out this event, to catch bugs. */
4004 clear_event (event);
4005 kbd_fetch_ptr = event + 1;
4009 #ifdef HAVE_MOUSE
4010 /* Try generating a mouse motion event. */
4011 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
4013 FRAME_PTR f = some_mouse_moved ();
4014 Lisp_Object bar_window;
4015 enum scroll_bar_part part;
4016 Lisp_Object x, y;
4017 unsigned long time;
4019 *kbp = current_kboard;
4020 /* Note that this uses F to determine which display to look at.
4021 If there is no valid info, it does not store anything
4022 so x remains nil. */
4023 x = Qnil;
4024 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
4026 obj = Qnil;
4028 /* Decide if we should generate a switch-frame event. Don't
4029 generate switch-frame events for motion outside of all Emacs
4030 frames. */
4031 if (!NILP (x) && f)
4033 Lisp_Object frame;
4035 frame = FRAME_FOCUS_FRAME (f);
4036 if (NILP (frame))
4037 XSETFRAME (frame, f);
4039 if (! EQ (frame, internal_last_event_frame)
4040 && !EQ (frame, selected_frame))
4041 obj = make_lispy_switch_frame (frame);
4042 internal_last_event_frame = frame;
4045 /* If we didn't decide to make a switch-frame event, go ahead and
4046 return a mouse-motion event. */
4047 if (!NILP (x) && NILP (obj))
4048 obj = make_lispy_movement (f, bar_window, part, x, y, time);
4050 #endif /* HAVE_MOUSE */
4051 else
4052 /* We were promised by the above while loop that there was
4053 something for us to read! */
4054 abort ();
4056 input_pending = readable_events (0);
4058 Vlast_event_frame = internal_last_event_frame;
4060 return (obj);
4063 /* Process any events that are not user-visible,
4064 then return, without reading any user-visible events. */
4066 void
4067 swallow_events (do_display)
4068 int do_display;
4070 int old_timers_run;
4072 while (kbd_fetch_ptr != kbd_store_ptr)
4074 struct input_event *event;
4076 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
4077 ? kbd_fetch_ptr
4078 : kbd_buffer);
4080 last_event_timestamp = event->timestamp;
4082 /* These two kinds of events get special handling
4083 and don't actually appear to the command loop. */
4084 if (event->kind == SELECTION_REQUEST_EVENT)
4086 #ifdef HAVE_X11
4087 struct input_event copy;
4089 /* Remove it from the buffer before processing it,
4090 since otherwise swallow_events called recursively could see it
4091 and process it again. */
4092 copy = *event;
4093 kbd_fetch_ptr = event + 1;
4094 input_pending = readable_events (0);
4095 x_handle_selection_request (&copy);
4096 #else
4097 /* We're getting selection request events, but we don't have
4098 a window system. */
4099 abort ();
4100 #endif
4103 else if (event->kind == SELECTION_CLEAR_EVENT)
4105 #ifdef HAVE_X11
4106 struct input_event copy;
4108 /* Remove it from the buffer before processing it, */
4109 copy = *event;
4111 kbd_fetch_ptr = event + 1;
4112 input_pending = readable_events (0);
4113 x_handle_selection_clear (&copy);
4114 #else
4115 /* We're getting selection request events, but we don't have
4116 a window system. */
4117 abort ();
4118 #endif
4120 else
4121 break;
4124 old_timers_run = timers_run;
4125 get_input_pending (&input_pending, 1);
4127 if (timers_run != old_timers_run && do_display)
4128 redisplay_preserve_echo_area (7);
4131 /* Record the start of when Emacs is idle,
4132 for the sake of running idle-time timers. */
4134 void
4135 timer_start_idle ()
4137 Lisp_Object timers;
4139 /* If we are already in the idle state, do nothing. */
4140 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4141 return;
4143 EMACS_GET_TIME (timer_idleness_start_time);
4145 timer_last_idleness_start_time = timer_idleness_start_time;
4147 /* Mark all idle-time timers as once again candidates for running. */
4148 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCDR (timers))
4150 Lisp_Object timer;
4152 timer = XCAR (timers);
4154 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
4155 continue;
4156 XVECTOR (timer)->contents[0] = Qnil;
4160 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
4162 void
4163 timer_stop_idle ()
4165 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
4168 /* This is only for debugging. */
4169 struct input_event last_timer_event;
4171 /* Check whether a timer has fired. To prevent larger problems we simply
4172 disregard elements that are not proper timers. Do not make a circular
4173 timer list for the time being.
4175 Returns the number of seconds to wait until the next timer fires. If a
4176 timer is triggering now, return zero seconds.
4177 If no timer is active, return -1 seconds.
4179 If a timer is ripe, we run it, with quitting turned off.
4181 DO_IT_NOW is now ignored. It used to mean that we should
4182 run the timer directly instead of queueing a timer-event.
4183 Now we always run timers directly. */
4185 EMACS_TIME
4186 timer_check (do_it_now)
4187 int do_it_now;
4189 EMACS_TIME nexttime;
4190 EMACS_TIME now, idleness_now;
4191 Lisp_Object timers, idle_timers, chosen_timer;
4192 struct gcpro gcpro1, gcpro2, gcpro3;
4194 EMACS_SET_SECS (nexttime, -1);
4195 EMACS_SET_USECS (nexttime, -1);
4197 /* Always consider the ordinary timers. */
4198 timers = Vtimer_list;
4199 /* Consider the idle timers only if Emacs is idle. */
4200 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4201 idle_timers = Vtimer_idle_list;
4202 else
4203 idle_timers = Qnil;
4204 chosen_timer = Qnil;
4205 GCPRO3 (timers, idle_timers, chosen_timer);
4207 if (CONSP (timers) || CONSP (idle_timers))
4209 EMACS_GET_TIME (now);
4210 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
4211 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
4214 while (CONSP (timers) || CONSP (idle_timers))
4216 Lisp_Object *vector;
4217 Lisp_Object timer = Qnil, idle_timer = Qnil;
4218 EMACS_TIME timer_time, idle_timer_time;
4219 EMACS_TIME difference, timer_difference, idle_timer_difference;
4221 /* Skip past invalid timers and timers already handled. */
4222 if (!NILP (timers))
4224 timer = XCAR (timers);
4225 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
4227 timers = XCDR (timers);
4228 continue;
4230 vector = XVECTOR (timer)->contents;
4232 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
4233 || !INTEGERP (vector[3])
4234 || ! NILP (vector[0]))
4236 timers = XCDR (timers);
4237 continue;
4240 if (!NILP (idle_timers))
4242 timer = XCAR (idle_timers);
4243 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
4245 idle_timers = XCDR (idle_timers);
4246 continue;
4248 vector = XVECTOR (timer)->contents;
4250 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
4251 || !INTEGERP (vector[3])
4252 || ! NILP (vector[0]))
4254 idle_timers = XCDR (idle_timers);
4255 continue;
4259 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
4260 based on the next ordinary timer.
4261 TIMER_DIFFERENCE is the distance in time from NOW to when
4262 this timer becomes ripe (negative if it's already ripe). */
4263 if (!NILP (timers))
4265 timer = XCAR (timers);
4266 vector = XVECTOR (timer)->contents;
4267 EMACS_SET_SECS (timer_time,
4268 (XINT (vector[1]) << 16) | (XINT (vector[2])));
4269 EMACS_SET_USECS (timer_time, XINT (vector[3]));
4270 EMACS_SUB_TIME (timer_difference, timer_time, now);
4273 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
4274 based on the next idle timer. */
4275 if (!NILP (idle_timers))
4277 idle_timer = XCAR (idle_timers);
4278 vector = XVECTOR (idle_timer)->contents;
4279 EMACS_SET_SECS (idle_timer_time,
4280 (XINT (vector[1]) << 16) | (XINT (vector[2])));
4281 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
4282 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
4285 /* Decide which timer is the next timer,
4286 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
4287 Also step down the list where we found that timer. */
4289 if (! NILP (timers) && ! NILP (idle_timers))
4291 EMACS_TIME temp;
4292 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
4293 if (EMACS_TIME_NEG_P (temp))
4295 chosen_timer = timer;
4296 timers = XCDR (timers);
4297 difference = timer_difference;
4299 else
4301 chosen_timer = idle_timer;
4302 idle_timers = XCDR (idle_timers);
4303 difference = idle_timer_difference;
4306 else if (! NILP (timers))
4308 chosen_timer = timer;
4309 timers = XCDR (timers);
4310 difference = timer_difference;
4312 else
4314 chosen_timer = idle_timer;
4315 idle_timers = XCDR (idle_timers);
4316 difference = idle_timer_difference;
4318 vector = XVECTOR (chosen_timer)->contents;
4320 /* If timer is ripe, run it if it hasn't been run. */
4321 if (EMACS_TIME_NEG_P (difference)
4322 || (EMACS_SECS (difference) == 0
4323 && EMACS_USECS (difference) == 0))
4325 if (NILP (vector[0]))
4327 int was_locked = single_kboard;
4328 int count = SPECPDL_INDEX ();
4329 Lisp_Object old_deactivate_mark = Vdeactivate_mark;
4331 /* Mark the timer as triggered to prevent problems if the lisp
4332 code fails to reschedule it right. */
4333 vector[0] = Qt;
4335 specbind (Qinhibit_quit, Qt);
4337 call1 (Qtimer_event_handler, chosen_timer);
4338 Vdeactivate_mark = old_deactivate_mark;
4339 timers_run++;
4340 unbind_to (count, Qnil);
4342 /* Resume allowing input from any kboard, if that was true before. */
4343 if (!was_locked)
4344 any_kboard_state ();
4346 /* Since we have handled the event,
4347 we don't need to tell the caller to wake up and do it. */
4350 else
4351 /* When we encounter a timer that is still waiting,
4352 return the amount of time to wait before it is ripe. */
4354 UNGCPRO;
4355 return difference;
4359 /* No timers are pending in the future. */
4360 /* Return 0 if we generated an event, and -1 if not. */
4361 UNGCPRO;
4362 return nexttime;
4365 /* Caches for modify_event_symbol. */
4366 static Lisp_Object accent_key_syms;
4367 static Lisp_Object func_key_syms;
4368 static Lisp_Object mouse_syms;
4369 #if defined(WINDOWSNT) || defined(MAC_OSX)
4370 static Lisp_Object mouse_wheel_syms;
4371 #endif
4372 static Lisp_Object drag_n_drop_syms;
4374 /* This is a list of keysym codes for special "accent" characters.
4375 It parallels lispy_accent_keys. */
4377 static int lispy_accent_codes[] =
4379 #ifdef XK_dead_circumflex
4380 XK_dead_circumflex,
4381 #else
4383 #endif
4384 #ifdef XK_dead_grave
4385 XK_dead_grave,
4386 #else
4388 #endif
4389 #ifdef XK_dead_tilde
4390 XK_dead_tilde,
4391 #else
4393 #endif
4394 #ifdef XK_dead_diaeresis
4395 XK_dead_diaeresis,
4396 #else
4398 #endif
4399 #ifdef XK_dead_macron
4400 XK_dead_macron,
4401 #else
4403 #endif
4404 #ifdef XK_dead_degree
4405 XK_dead_degree,
4406 #else
4408 #endif
4409 #ifdef XK_dead_acute
4410 XK_dead_acute,
4411 #else
4413 #endif
4414 #ifdef XK_dead_cedilla
4415 XK_dead_cedilla,
4416 #else
4418 #endif
4419 #ifdef XK_dead_breve
4420 XK_dead_breve,
4421 #else
4423 #endif
4424 #ifdef XK_dead_ogonek
4425 XK_dead_ogonek,
4426 #else
4428 #endif
4429 #ifdef XK_dead_caron
4430 XK_dead_caron,
4431 #else
4433 #endif
4434 #ifdef XK_dead_doubleacute
4435 XK_dead_doubleacute,
4436 #else
4438 #endif
4439 #ifdef XK_dead_abovedot
4440 XK_dead_abovedot,
4441 #else
4443 #endif
4444 #ifdef XK_dead_abovering
4445 XK_dead_abovering,
4446 #else
4448 #endif
4449 #ifdef XK_dead_iota
4450 XK_dead_iota,
4451 #else
4453 #endif
4454 #ifdef XK_dead_belowdot
4455 XK_dead_belowdot,
4456 #else
4458 #endif
4459 #ifdef XK_dead_voiced_sound
4460 XK_dead_voiced_sound,
4461 #else
4463 #endif
4464 #ifdef XK_dead_semivoiced_sound
4465 XK_dead_semivoiced_sound,
4466 #else
4468 #endif
4469 #ifdef XK_dead_hook
4470 XK_dead_hook,
4471 #else
4473 #endif
4474 #ifdef XK_dead_horn
4475 XK_dead_horn,
4476 #else
4478 #endif
4481 /* This is a list of Lisp names for special "accent" characters.
4482 It parallels lispy_accent_codes. */
4484 static char *lispy_accent_keys[] =
4486 "dead-circumflex",
4487 "dead-grave",
4488 "dead-tilde",
4489 "dead-diaeresis",
4490 "dead-macron",
4491 "dead-degree",
4492 "dead-acute",
4493 "dead-cedilla",
4494 "dead-breve",
4495 "dead-ogonek",
4496 "dead-caron",
4497 "dead-doubleacute",
4498 "dead-abovedot",
4499 "dead-abovering",
4500 "dead-iota",
4501 "dead-belowdot",
4502 "dead-voiced-sound",
4503 "dead-semivoiced-sound",
4504 "dead-hook",
4505 "dead-horn",
4508 #ifdef HAVE_NTGUI
4509 #define FUNCTION_KEY_OFFSET 0x0
4511 char *lispy_function_keys[] =
4513 0, /* 0 */
4515 0, /* VK_LBUTTON 0x01 */
4516 0, /* VK_RBUTTON 0x02 */
4517 "cancel", /* VK_CANCEL 0x03 */
4518 0, /* VK_MBUTTON 0x04 */
4520 0, 0, 0, /* 0x05 .. 0x07 */
4522 "backspace", /* VK_BACK 0x08 */
4523 "tab", /* VK_TAB 0x09 */
4525 0, 0, /* 0x0A .. 0x0B */
4527 "clear", /* VK_CLEAR 0x0C */
4528 "return", /* VK_RETURN 0x0D */
4530 0, 0, /* 0x0E .. 0x0F */
4532 0, /* VK_SHIFT 0x10 */
4533 0, /* VK_CONTROL 0x11 */
4534 0, /* VK_MENU 0x12 */
4535 "pause", /* VK_PAUSE 0x13 */
4536 "capslock", /* VK_CAPITAL 0x14 */
4538 0, 0, 0, 0, 0, 0, /* 0x15 .. 0x1A */
4540 "escape", /* VK_ESCAPE 0x1B */
4542 0, 0, 0, 0, /* 0x1C .. 0x1F */
4544 0, /* VK_SPACE 0x20 */
4545 "prior", /* VK_PRIOR 0x21 */
4546 "next", /* VK_NEXT 0x22 */
4547 "end", /* VK_END 0x23 */
4548 "home", /* VK_HOME 0x24 */
4549 "left", /* VK_LEFT 0x25 */
4550 "up", /* VK_UP 0x26 */
4551 "right", /* VK_RIGHT 0x27 */
4552 "down", /* VK_DOWN 0x28 */
4553 "select", /* VK_SELECT 0x29 */
4554 "print", /* VK_PRINT 0x2A */
4555 "execute", /* VK_EXECUTE 0x2B */
4556 "snapshot", /* VK_SNAPSHOT 0x2C */
4557 "insert", /* VK_INSERT 0x2D */
4558 "delete", /* VK_DELETE 0x2E */
4559 "help", /* VK_HELP 0x2F */
4561 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
4563 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4565 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
4567 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
4569 0, 0, 0, 0, 0, 0, 0, 0, 0,
4570 0, 0, 0, 0, 0, 0, 0, 0, 0,
4571 0, 0, 0, 0, 0, 0, 0, 0,
4573 "lwindow", /* VK_LWIN 0x5B */
4574 "rwindow", /* VK_RWIN 0x5C */
4575 "apps", /* VK_APPS 0x5D */
4577 0, 0, /* 0x5E .. 0x5F */
4579 "kp-0", /* VK_NUMPAD0 0x60 */
4580 "kp-1", /* VK_NUMPAD1 0x61 */
4581 "kp-2", /* VK_NUMPAD2 0x62 */
4582 "kp-3", /* VK_NUMPAD3 0x63 */
4583 "kp-4", /* VK_NUMPAD4 0x64 */
4584 "kp-5", /* VK_NUMPAD5 0x65 */
4585 "kp-6", /* VK_NUMPAD6 0x66 */
4586 "kp-7", /* VK_NUMPAD7 0x67 */
4587 "kp-8", /* VK_NUMPAD8 0x68 */
4588 "kp-9", /* VK_NUMPAD9 0x69 */
4589 "kp-multiply", /* VK_MULTIPLY 0x6A */
4590 "kp-add", /* VK_ADD 0x6B */
4591 "kp-separator", /* VK_SEPARATOR 0x6C */
4592 "kp-subtract", /* VK_SUBTRACT 0x6D */
4593 "kp-decimal", /* VK_DECIMAL 0x6E */
4594 "kp-divide", /* VK_DIVIDE 0x6F */
4595 "f1", /* VK_F1 0x70 */
4596 "f2", /* VK_F2 0x71 */
4597 "f3", /* VK_F3 0x72 */
4598 "f4", /* VK_F4 0x73 */
4599 "f5", /* VK_F5 0x74 */
4600 "f6", /* VK_F6 0x75 */
4601 "f7", /* VK_F7 0x76 */
4602 "f8", /* VK_F8 0x77 */
4603 "f9", /* VK_F9 0x78 */
4604 "f10", /* VK_F10 0x79 */
4605 "f11", /* VK_F11 0x7A */
4606 "f12", /* VK_F12 0x7B */
4607 "f13", /* VK_F13 0x7C */
4608 "f14", /* VK_F14 0x7D */
4609 "f15", /* VK_F15 0x7E */
4610 "f16", /* VK_F16 0x7F */
4611 "f17", /* VK_F17 0x80 */
4612 "f18", /* VK_F18 0x81 */
4613 "f19", /* VK_F19 0x82 */
4614 "f20", /* VK_F20 0x83 */
4615 "f21", /* VK_F21 0x84 */
4616 "f22", /* VK_F22 0x85 */
4617 "f23", /* VK_F23 0x86 */
4618 "f24", /* VK_F24 0x87 */
4620 0, 0, 0, 0, /* 0x88 .. 0x8B */
4621 0, 0, 0, 0, /* 0x8C .. 0x8F */
4623 "kp-numlock", /* VK_NUMLOCK 0x90 */
4624 "scroll", /* VK_SCROLL 0x91 */
4626 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
4627 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
4628 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
4629 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
4630 "kp-end", /* VK_NUMPAD_END 0x96 */
4631 "kp-home", /* VK_NUMPAD_HOME 0x97 */
4632 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
4633 "kp-up", /* VK_NUMPAD_UP 0x99 */
4634 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
4635 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
4636 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
4637 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
4639 0, 0, /* 0x9E .. 0x9F */
4642 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
4643 * Used only as parameters to GetAsyncKeyState and GetKeyState.
4644 * No other API or message will distinguish left and right keys this way.
4646 /* 0xA0 .. 0xEF */
4648 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4649 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4650 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4651 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4652 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4654 /* 0xF0 .. 0xF5 */
4656 0, 0, 0, 0, 0, 0,
4658 "attn", /* VK_ATTN 0xF6 */
4659 "crsel", /* VK_CRSEL 0xF7 */
4660 "exsel", /* VK_EXSEL 0xF8 */
4661 "ereof", /* VK_EREOF 0xF9 */
4662 "play", /* VK_PLAY 0xFA */
4663 "zoom", /* VK_ZOOM 0xFB */
4664 "noname", /* VK_NONAME 0xFC */
4665 "pa1", /* VK_PA1 0xFD */
4666 "oem_clear", /* VK_OEM_CLEAR 0xFE */
4667 0 /* 0xFF */
4670 #else /* not HAVE_NTGUI */
4672 /* This should be dealt with in XTread_socket now, and that doesn't
4673 depend on the client system having the Kana syms defined. See also
4674 the XK_kana_A case below. */
4675 #if 0
4676 #ifdef XK_kana_A
4677 static char *lispy_kana_keys[] =
4679 /* X Keysym value */
4680 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x400 .. 0x40f */
4681 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x410 .. 0x41f */
4682 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x420 .. 0x42f */
4683 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x430 .. 0x43f */
4684 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x440 .. 0x44f */
4685 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x450 .. 0x45f */
4686 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x460 .. 0x46f */
4687 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0,
4688 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x480 .. 0x48f */
4689 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x490 .. 0x49f */
4690 0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket",
4691 "kana-comma", "kana-conjunctive", "kana-WO", "kana-a",
4692 "kana-i", "kana-u", "kana-e", "kana-o",
4693 "kana-ya", "kana-yu", "kana-yo", "kana-tsu",
4694 "prolongedsound", "kana-A", "kana-I", "kana-U",
4695 "kana-E", "kana-O", "kana-KA", "kana-KI",
4696 "kana-KU", "kana-KE", "kana-KO", "kana-SA",
4697 "kana-SHI", "kana-SU", "kana-SE", "kana-SO",
4698 "kana-TA", "kana-CHI", "kana-TSU", "kana-TE",
4699 "kana-TO", "kana-NA", "kana-NI", "kana-NU",
4700 "kana-NE", "kana-NO", "kana-HA", "kana-HI",
4701 "kana-FU", "kana-HE", "kana-HO", "kana-MA",
4702 "kana-MI", "kana-MU", "kana-ME", "kana-MO",
4703 "kana-YA", "kana-YU", "kana-YO", "kana-RA",
4704 "kana-RI", "kana-RU", "kana-RE", "kana-RO",
4705 "kana-WA", "kana-N", "voicedsound", "semivoicedsound",
4706 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4e0 .. 0x4ef */
4707 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f0 .. 0x4ff */
4709 #endif /* XK_kana_A */
4710 #endif /* 0 */
4712 #define FUNCTION_KEY_OFFSET 0xff00
4714 /* You'll notice that this table is arranged to be conveniently
4715 indexed by X Windows keysym values. */
4716 static char *lispy_function_keys[] =
4718 /* X Keysym value */
4720 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00...0f */
4721 "backspace", "tab", "linefeed", "clear",
4722 0, "return", 0, 0,
4723 0, 0, 0, "pause", /* 0xff10...1f */
4724 0, 0, 0, 0, 0, 0, 0, "escape",
4725 0, 0, 0, 0,
4726 0, "kanji", "muhenkan", "henkan", /* 0xff20...2f */
4727 "romaji", "hiragana", "katakana", "hiragana-katakana",
4728 "zenkaku", "hankaku", "zenkaku-hankaku", "touroku",
4729 "massyo", "kana-lock", "kana-shift", "eisu-shift",
4730 "eisu-toggle", /* 0xff30...3f */
4731 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4732 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
4734 "home", "left", "up", "right", /* 0xff50 */ /* IsCursorKey */
4735 "down", "prior", "next", "end",
4736 "begin", 0, 0, 0, 0, 0, 0, 0,
4737 "select", /* 0xff60 */ /* IsMiscFunctionKey */
4738 "print",
4739 "execute",
4740 "insert",
4741 0, /* 0xff64 */
4742 "undo",
4743 "redo",
4744 "menu",
4745 "find",
4746 "cancel",
4747 "help",
4748 "break", /* 0xff6b */
4750 0, 0, 0, 0,
4751 0, 0, 0, 0, "backtab", 0, 0, 0, /* 0xff70... */
4752 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff78... */
4753 "kp-space", /* 0xff80 */ /* IsKeypadKey */
4754 0, 0, 0, 0, 0, 0, 0, 0,
4755 "kp-tab", /* 0xff89 */
4756 0, 0, 0,
4757 "kp-enter", /* 0xff8d */
4758 0, 0, 0,
4759 "kp-f1", /* 0xff91 */
4760 "kp-f2",
4761 "kp-f3",
4762 "kp-f4",
4763 "kp-home", /* 0xff95 */
4764 "kp-left",
4765 "kp-up",
4766 "kp-right",
4767 "kp-down",
4768 "kp-prior", /* kp-page-up */
4769 "kp-next", /* kp-page-down */
4770 "kp-end",
4771 "kp-begin",
4772 "kp-insert",
4773 "kp-delete",
4774 0, /* 0xffa0 */
4775 0, 0, 0, 0, 0, 0, 0, 0, 0,
4776 "kp-multiply", /* 0xffaa */
4777 "kp-add",
4778 "kp-separator",
4779 "kp-subtract",
4780 "kp-decimal",
4781 "kp-divide", /* 0xffaf */
4782 "kp-0", /* 0xffb0 */
4783 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
4784 0, /* 0xffba */
4785 0, 0,
4786 "kp-equal", /* 0xffbd */
4787 "f1", /* 0xffbe */ /* IsFunctionKey */
4788 "f2",
4789 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
4790 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
4791 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
4792 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
4793 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
4794 0, 0, 0, 0, 0, 0, 0, 0,
4795 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
4796 0, 0, 0, 0, 0, 0, 0, "delete"
4799 /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE. */
4800 #define ISO_FUNCTION_KEY_OFFSET 0xfe00
4802 static char *iso_lispy_function_keys[] =
4804 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe00 */
4805 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe08 */
4806 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe10 */
4807 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe18 */
4808 "iso-lefttab", /* 0xfe20 */
4809 "iso-move-line-up", "iso-move-line-down",
4810 "iso-partial-line-up", "iso-partial-line-down",
4811 "iso-partial-space-left", "iso-partial-space-right",
4812 "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */
4813 "iso-release-margin-left", "iso-release-margin-right",
4814 "iso-release-both-margins",
4815 "iso-fast-cursor-left", "iso-fast-cursor-right",
4816 "iso-fast-cursor-up", "iso-fast-cursor-down",
4817 "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */
4818 "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */
4821 #endif /* not HAVE_NTGUI */
4823 Lisp_Object Vlispy_mouse_stem;
4825 #if defined(WINDOWSNT) || defined(MAC_OSX)
4826 /* mouse-wheel events are generated by the wheel on devices such as
4827 the MS Intellimouse. The wheel sits in between the left and right
4828 mouse buttons, and is typically used to scroll or zoom the window
4829 underneath the pointer. mouse-wheel events specify the object on
4830 which they operate, and a delta corresponding to the amount and
4831 direction that the wheel is rotated. Clicking the mouse-wheel
4832 generates a mouse-2 event. */
4833 static char *lispy_mouse_wheel_names[] =
4835 "mouse-wheel"
4838 #endif /* WINDOWSNT */
4840 /* drag-n-drop events are generated when a set of selected files are
4841 dragged from another application and dropped onto an Emacs window. */
4842 static char *lispy_drag_n_drop_names[] =
4844 "drag-n-drop"
4847 /* Scroll bar parts. */
4848 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
4849 Lisp_Object Qup, Qdown, Qbottom, Qend_scroll;
4850 Lisp_Object Qtop, Qratio;
4852 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
4853 Lisp_Object *scroll_bar_parts[] = {
4854 &Qabove_handle, &Qhandle, &Qbelow_handle,
4855 &Qup, &Qdown, &Qtop, &Qbottom, &Qend_scroll, &Qratio
4858 /* User signal events. */
4859 Lisp_Object Qusr1_signal, Qusr2_signal;
4861 Lisp_Object *lispy_user_signals[] =
4863 &Qusr1_signal, &Qusr2_signal
4867 /* A vector, indexed by button number, giving the down-going location
4868 of currently depressed buttons, both scroll bar and non-scroll bar.
4870 The elements have the form
4871 (BUTTON-NUMBER MODIFIER-MASK . REST)
4872 where REST is the cdr of a position as it would be reported in the event.
4874 The make_lispy_event function stores positions here to tell the
4875 difference between click and drag events, and to store the starting
4876 location to be included in drag events. */
4878 static Lisp_Object button_down_location;
4880 /* Information about the most recent up-going button event: Which
4881 button, what location, and what time. */
4883 static int last_mouse_button;
4884 static int last_mouse_x;
4885 static int last_mouse_y;
4886 static unsigned long button_down_time;
4888 /* The maximum time between clicks to make a double-click, or Qnil to
4889 disable double-click detection, or Qt for no time limit. */
4891 Lisp_Object Vdouble_click_time;
4893 /* Maximum number of pixels the mouse may be moved between clicks
4894 to make a double-click. */
4896 EMACS_INT double_click_fuzz;
4898 /* The number of clicks in this multiple-click. */
4900 int double_click_count;
4902 /* Given a struct input_event, build the lisp event which represents
4903 it. If EVENT is 0, build a mouse movement event from the mouse
4904 movement buffer, which should have a movement event in it.
4906 Note that events must be passed to this function in the order they
4907 are received; this function stores the location of button presses
4908 in order to build drag events when the button is released. */
4910 static Lisp_Object
4911 make_lispy_event (event)
4912 struct input_event *event;
4914 int i;
4916 switch (SWITCH_ENUM_CAST (event->kind))
4918 /* A simple keystroke. */
4919 case ASCII_KEYSTROKE_EVENT:
4921 Lisp_Object lispy_c;
4922 int c = event->code & 0377;
4923 /* Turn ASCII characters into control characters
4924 when proper. */
4925 if (event->modifiers & ctrl_modifier)
4926 c = make_ctrl_char (c);
4928 /* Add in the other modifier bits. We took care of ctrl_modifier
4929 just above, and the shift key was taken care of by the X code,
4930 and applied to control characters by make_ctrl_char. */
4931 c |= (event->modifiers
4932 & (meta_modifier | alt_modifier
4933 | hyper_modifier | super_modifier));
4934 /* Distinguish Shift-SPC from SPC. */
4935 if ((event->code & 0377) == 040
4936 && event->modifiers & shift_modifier)
4937 c |= shift_modifier;
4938 button_down_time = 0;
4939 XSETFASTINT (lispy_c, c);
4940 return lispy_c;
4943 case MULTIBYTE_CHAR_KEYSTROKE_EVENT:
4945 Lisp_Object lispy_c;
4947 XSETFASTINT (lispy_c, event->code);
4948 return lispy_c;
4951 /* A function key. The symbol may need to have modifier prefixes
4952 tacked onto it. */
4953 case NON_ASCII_KEYSTROKE_EVENT:
4954 button_down_time = 0;
4956 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
4957 if (event->code == lispy_accent_codes[i])
4958 return modify_event_symbol (i,
4959 event->modifiers,
4960 Qfunction_key, Qnil,
4961 lispy_accent_keys, &accent_key_syms,
4962 (sizeof (lispy_accent_keys)
4963 / sizeof (lispy_accent_keys[0])));
4965 #if 0
4966 #ifdef XK_kana_A
4967 if (event->code >= 0x400 && event->code < 0x500)
4968 return modify_event_symbol (event->code - 0x400,
4969 event->modifiers & ~shift_modifier,
4970 Qfunction_key, Qnil,
4971 lispy_kana_keys, &func_key_syms,
4972 (sizeof (lispy_kana_keys)
4973 / sizeof (lispy_kana_keys[0])));
4974 #endif /* XK_kana_A */
4975 #endif /* 0 */
4977 #ifdef ISO_FUNCTION_KEY_OFFSET
4978 if (event->code < FUNCTION_KEY_OFFSET
4979 && event->code >= ISO_FUNCTION_KEY_OFFSET)
4980 return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET,
4981 event->modifiers,
4982 Qfunction_key, Qnil,
4983 iso_lispy_function_keys, &func_key_syms,
4984 (sizeof (iso_lispy_function_keys)
4985 / sizeof (iso_lispy_function_keys[0])));
4986 #endif
4988 /* Handle system-specific or unknown keysyms. */
4989 if (event->code & (1 << 28)
4990 || event->code - FUNCTION_KEY_OFFSET < 0
4991 || (event->code - FUNCTION_KEY_OFFSET
4992 >= sizeof lispy_function_keys / sizeof *lispy_function_keys)
4993 || !lispy_function_keys[event->code - FUNCTION_KEY_OFFSET])
4995 /* We need to use an alist rather than a vector as the cache
4996 since we can't make a vector long enuf. */
4997 if (NILP (current_kboard->system_key_syms))
4998 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
4999 return modify_event_symbol (event->code,
5000 event->modifiers,
5001 Qfunction_key,
5002 current_kboard->Vsystem_key_alist,
5003 0, &current_kboard->system_key_syms,
5004 (unsigned) -1);
5007 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
5008 event->modifiers,
5009 Qfunction_key, Qnil,
5010 lispy_function_keys, &func_key_syms,
5011 (sizeof (lispy_function_keys)
5012 / sizeof (lispy_function_keys[0])));
5014 #ifdef HAVE_MOUSE
5015 /* A mouse click. Figure out where it is, decide whether it's
5016 a press, click or drag, and build the appropriate structure. */
5017 case MOUSE_CLICK_EVENT:
5018 #ifndef USE_TOOLKIT_SCROLL_BARS
5019 case SCROLL_BAR_CLICK_EVENT:
5020 #endif
5022 int button = event->code;
5023 int is_double;
5024 Lisp_Object position;
5025 Lisp_Object *start_pos_ptr;
5026 Lisp_Object start_pos;
5027 Lisp_Object window;
5029 position = Qnil;
5031 /* Build the position as appropriate for this mouse click. */
5032 if (event->kind == MOUSE_CLICK_EVENT)
5034 int part;
5035 struct frame *f = XFRAME (event->frame_or_window);
5036 Lisp_Object posn;
5037 Lisp_Object string_info = Qnil;
5038 int row, column;
5040 /* Ignore mouse events that were made on frame that
5041 have been deleted. */
5042 if (! FRAME_LIVE_P (f))
5043 return Qnil;
5045 /* EVENT->x and EVENT->y are frame-relative pixel
5046 coordinates at this place. Under old redisplay, COLUMN
5047 and ROW are set to frame relative glyph coordinates
5048 which are then used to determine whether this click is
5049 in a menu (non-toolkit version). */
5050 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
5051 &column, &row, NULL, 1);
5053 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
5054 /* In the non-toolkit version, clicks on the menu bar
5055 are ordinary button events in the event buffer.
5056 Distinguish them, and invoke the menu.
5058 (In the toolkit version, the toolkit handles the menu bar
5059 and Emacs doesn't know about it until after the user
5060 makes a selection.) */
5061 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
5062 && (event->modifiers & down_modifier))
5064 Lisp_Object items, item;
5065 int hpos;
5066 int i;
5068 #if 0
5069 /* Activate the menu bar on the down event. If the
5070 up event comes in before the menu code can deal with it,
5071 just ignore it. */
5072 if (! (event->modifiers & down_modifier))
5073 return Qnil;
5074 #endif
5076 /* Find the menu bar item under `column'. */
5077 item = Qnil;
5078 items = FRAME_MENU_BAR_ITEMS (f);
5079 for (i = 0; i < XVECTOR (items)->size; i += 4)
5081 Lisp_Object pos, string;
5082 string = AREF (items, i + 1);
5083 pos = AREF (items, i + 3);
5084 if (NILP (string))
5085 break;
5086 if (column >= XINT (pos)
5087 && column < XINT (pos) + SCHARS (string))
5089 item = AREF (items, i);
5090 break;
5094 /* ELisp manual 2.4b says (x y) are window relative but
5095 code says they are frame-relative. */
5096 position
5097 = Fcons (event->frame_or_window,
5098 Fcons (Qmenu_bar,
5099 Fcons (Fcons (event->x, event->y),
5100 Fcons (make_number (event->timestamp),
5101 Qnil))));
5103 return Fcons (item, Fcons (position, Qnil));
5105 #endif /* not USE_X_TOOLKIT && not USE_GTK */
5107 /* Set `window' to the window under frame pixel coordinates
5108 event->x/event->y. */
5109 window = window_from_coordinates (f, XINT (event->x),
5110 XINT (event->y), &part, 0);
5112 if (!WINDOWP (window))
5114 window = event->frame_or_window;
5115 posn = Qnil;
5117 else
5119 /* It's a click in window window at frame coordinates
5120 event->x/ event->y. */
5121 struct window *w = XWINDOW (window);
5123 /* Get window relative coordinates. Original code
5124 `rounded' this to glyph boundaries. */
5125 int wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (event->x));
5126 int wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (event->y));
5128 /* Set event coordinates to window-relative coordinates
5129 for constructing the Lisp event below. */
5130 XSETINT (event->x, wx);
5131 XSETINT (event->y, wy);
5133 if (part == 1 || part == 3)
5135 /* Mode line or header line. Look for a string under
5136 the mouse that may have a `local-map' property. */
5137 Lisp_Object string;
5138 int charpos;
5140 posn = part == 1 ? Qmode_line : Qheader_line;
5141 string = mode_line_string (w, wx, wy, part == 1, &charpos);
5142 if (STRINGP (string))
5143 string_info = Fcons (string, make_number (charpos));
5145 else if (part == 2)
5146 posn = Qvertical_line;
5147 else if (part == 6 || part == 7)
5149 int charpos;
5150 Lisp_Object object = marginal_area_string (w, wx, wy, part,
5151 &charpos);
5152 posn = (part == 6) ? Qleft_margin : Qright_margin;
5153 if (STRINGP (object))
5154 string_info = Fcons (object, make_number (charpos));
5156 else
5158 Lisp_Object object;
5159 struct display_pos p;
5160 buffer_posn_from_coords (w, &wx, &wy, &object, &p);
5161 posn = make_number (CHARPOS (p.pos));
5162 if (STRINGP (object))
5163 string_info
5164 = Fcons (object,
5165 make_number (CHARPOS (p.string_pos)));
5169 position
5170 = Fcons (window,
5171 Fcons (posn,
5172 Fcons (Fcons (event->x, event->y),
5173 Fcons (make_number (event->timestamp),
5174 (NILP (string_info)
5175 ? Qnil
5176 : Fcons (string_info, Qnil))))));
5178 #ifndef USE_TOOLKIT_SCROLL_BARS
5179 else
5181 /* It's a scrollbar click. */
5182 Lisp_Object portion_whole;
5183 Lisp_Object part;
5185 window = event->frame_or_window;
5186 portion_whole = Fcons (event->x, event->y);
5187 part = *scroll_bar_parts[(int) event->part];
5189 position
5190 = Fcons (window,
5191 Fcons (Qvertical_scroll_bar,
5192 Fcons (portion_whole,
5193 Fcons (make_number (event->timestamp),
5194 Fcons (part, Qnil)))));
5196 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5198 if (button >= ASIZE (button_down_location))
5200 button_down_location = larger_vector (button_down_location,
5201 button + 1, Qnil);
5202 mouse_syms = larger_vector (mouse_syms, button + 1, Qnil);
5205 start_pos_ptr = &AREF (button_down_location, button);
5206 start_pos = *start_pos_ptr;
5207 *start_pos_ptr = Qnil;
5210 /* On window-system frames, use the value of
5211 double-click-fuzz as is. On other frames, interpret it
5212 as a multiple of 1/8 characters. */
5213 struct frame *f;
5214 int fuzz;
5216 if (WINDOWP (event->frame_or_window))
5217 f = XFRAME (XWINDOW (event->frame_or_window)->frame);
5218 else if (FRAMEP (event->frame_or_window))
5219 f = XFRAME (event->frame_or_window);
5220 else
5221 abort ();
5223 if (FRAME_WINDOW_P (f))
5224 fuzz = double_click_fuzz;
5225 else
5226 fuzz = double_click_fuzz / 8;
5228 is_double = (button == last_mouse_button
5229 && (abs (XINT (event->x) - last_mouse_x) <= fuzz)
5230 && (abs (XINT (event->y) - last_mouse_y) <= fuzz)
5231 && button_down_time != 0
5232 && (EQ (Vdouble_click_time, Qt)
5233 || (INTEGERP (Vdouble_click_time)
5234 && ((int)(event->timestamp - button_down_time)
5235 < XINT (Vdouble_click_time)))));
5238 last_mouse_button = button;
5239 last_mouse_x = XINT (event->x);
5240 last_mouse_y = XINT (event->y);
5242 /* If this is a button press, squirrel away the location, so
5243 we can decide later whether it was a click or a drag. */
5244 if (event->modifiers & down_modifier)
5246 if (is_double)
5248 double_click_count++;
5249 event->modifiers |= ((double_click_count > 2)
5250 ? triple_modifier
5251 : double_modifier);
5253 else
5254 double_click_count = 1;
5255 button_down_time = event->timestamp;
5256 *start_pos_ptr = Fcopy_alist (position);
5259 /* Now we're releasing a button - check the co-ordinates to
5260 see if this was a click or a drag. */
5261 else if (event->modifiers & up_modifier)
5263 /* If we did not see a down before this up, ignore the up.
5264 Probably this happened because the down event chose a
5265 menu item. It would be an annoyance to treat the
5266 release of the button that chose the menu item as a
5267 separate event. */
5269 if (!CONSP (start_pos))
5270 return Qnil;
5272 event->modifiers &= ~up_modifier;
5273 #if 0 /* Formerly we treated an up with no down as a click event. */
5274 if (!CONSP (start_pos))
5275 event->modifiers |= click_modifier;
5276 else
5277 #endif
5279 Lisp_Object down;
5280 EMACS_INT xdiff = double_click_fuzz, ydiff = double_click_fuzz;
5282 /* The third element of every position
5283 should be the (x,y) pair. */
5284 down = Fcar (Fcdr (Fcdr (start_pos)));
5285 if (CONSP (down)
5286 && INTEGERP (XCAR (down)) && INTEGERP (XCDR (down)))
5288 xdiff = XFASTINT (event->x) - XFASTINT (XCAR (down));
5289 ydiff = XFASTINT (event->y) - XFASTINT (XCDR (down));
5292 if (xdiff < double_click_fuzz && xdiff > - double_click_fuzz
5293 && ydiff < double_click_fuzz
5294 && ydiff > - double_click_fuzz)
5295 /* Mouse hasn't moved (much). */
5296 event->modifiers |= click_modifier;
5297 else
5299 button_down_time = 0;
5300 event->modifiers |= drag_modifier;
5303 /* Don't check is_double; treat this as multiple
5304 if the down-event was multiple. */
5305 if (double_click_count > 1)
5306 event->modifiers |= ((double_click_count > 2)
5307 ? triple_modifier
5308 : double_modifier);
5311 else
5312 /* Every mouse event should either have the down_modifier or
5313 the up_modifier set. */
5314 abort ();
5317 /* Get the symbol we should use for the mouse click. */
5318 Lisp_Object head;
5320 head = modify_event_symbol (button,
5321 event->modifiers,
5322 Qmouse_click, Vlispy_mouse_stem,
5323 NULL,
5324 &mouse_syms,
5325 XVECTOR (mouse_syms)->size);
5326 if (event->modifiers & drag_modifier)
5327 return Fcons (head,
5328 Fcons (start_pos,
5329 Fcons (position,
5330 Qnil)));
5331 else if (event->modifiers & (double_modifier | triple_modifier))
5332 return Fcons (head,
5333 Fcons (position,
5334 Fcons (make_number (double_click_count),
5335 Qnil)));
5336 else
5337 return Fcons (head,
5338 Fcons (position,
5339 Qnil));
5343 #ifdef USE_TOOLKIT_SCROLL_BARS
5345 /* We don't have down and up events if using toolkit scroll bars,
5346 so make this always a click event. Store in the `part' of
5347 the Lisp event a symbol which maps to the following actions:
5349 `above_handle' page up
5350 `below_handle' page down
5351 `up' line up
5352 `down' line down
5353 `top' top of buffer
5354 `bottom' bottom of buffer
5355 `handle' thumb has been dragged.
5356 `end-scroll' end of interaction with scroll bar
5358 The incoming input_event contains in its `part' member an
5359 index of type `enum scroll_bar_part' which we can use as an
5360 index in scroll_bar_parts to get the appropriate symbol. */
5362 case SCROLL_BAR_CLICK_EVENT:
5364 Lisp_Object position, head, window, portion_whole, part;
5366 window = event->frame_or_window;
5367 portion_whole = Fcons (event->x, event->y);
5368 part = *scroll_bar_parts[(int) event->part];
5370 position
5371 = Fcons (window,
5372 Fcons (Qvertical_scroll_bar,
5373 Fcons (portion_whole,
5374 Fcons (make_number (event->timestamp),
5375 Fcons (part, Qnil)))));
5377 /* Always treat scroll bar events as clicks. */
5378 event->modifiers |= click_modifier;
5379 event->modifiers &= ~up_modifier;
5381 /* Get the symbol we should use for the mouse click. */
5382 head = modify_event_symbol (event->code,
5383 event->modifiers,
5384 Qmouse_click,
5385 Vlispy_mouse_stem,
5386 NULL, &mouse_syms,
5387 XVECTOR (mouse_syms)->size);
5388 return Fcons (head, Fcons (position, Qnil));
5391 #endif /* USE_TOOLKIT_SCROLL_BARS */
5393 #ifdef WINDOWSNT
5394 case W32_SCROLL_BAR_CLICK_EVENT:
5396 int button = event->code;
5397 int is_double;
5398 Lisp_Object position;
5399 Lisp_Object *start_pos_ptr;
5400 Lisp_Object start_pos;
5403 Lisp_Object window;
5404 Lisp_Object portion_whole;
5405 Lisp_Object part;
5407 window = event->frame_or_window;
5408 portion_whole = Fcons (event->x, event->y);
5409 part = *scroll_bar_parts[(int) event->part];
5411 position
5412 = Fcons (window,
5413 Fcons (Qvertical_scroll_bar,
5414 Fcons (portion_whole,
5415 Fcons (make_number (event->timestamp),
5416 Fcons (part, Qnil)))));
5419 /* Always treat W32 scroll bar events as clicks. */
5420 event->modifiers |= click_modifier;
5423 /* Get the symbol we should use for the mouse click. */
5424 Lisp_Object head;
5426 head = modify_event_symbol (button,
5427 event->modifiers,
5428 Qmouse_click,
5429 Vlispy_mouse_stem,
5430 NULL, &mouse_syms,
5431 XVECTOR (mouse_syms)->size);
5432 return Fcons (head,
5433 Fcons (position,
5434 Qnil));
5437 #endif /* WINDOWSNT */
5438 #if defined(WINDOWSNT) || defined(MAC_OSX)
5439 case MOUSE_WHEEL_EVENT:
5441 int part;
5442 FRAME_PTR f = XFRAME (event->frame_or_window);
5443 Lisp_Object window;
5444 Lisp_Object posn;
5445 Lisp_Object head, position;
5446 int row, column;
5448 /* Ignore mouse events that were made on frame that
5449 have been deleted. */
5450 if (! FRAME_LIVE_P (f))
5451 return Qnil;
5452 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
5453 &column, &row, NULL, 1);
5454 window = window_from_coordinates (f, XINT (event->x),
5455 XINT (event->y), &part, 0);
5457 if (!WINDOWP (window))
5459 window = event->frame_or_window;
5460 posn = Qnil;
5462 else
5464 int pixcolumn, pixrow;
5465 column -= XINT (XWINDOW (window)->left);
5466 row -= XINT (XWINDOW (window)->top);
5467 glyph_to_pixel_coords (XWINDOW(window), column, row,
5468 &pixcolumn, &pixrow);
5469 XSETINT (event->x, pixcolumn);
5470 XSETINT (event->y, pixrow);
5472 if (part == 1)
5473 posn = Qmode_line;
5474 else if (part == 2)
5475 posn = Qvertical_line;
5476 else if (part == 3)
5477 posn = Qheader_line;
5478 else
5480 Lisp_Object object;
5481 struct display_pos p;
5482 buffer_posn_from_coords (XWINDOW (window), &column, &row,
5483 &object, &p);
5484 posn = make_number (CHARPOS (p.pos));
5489 Lisp_Object head, position;
5491 position
5492 = Fcons (window,
5493 Fcons (posn,
5494 Fcons (Fcons (event->x, event->y),
5495 Fcons (make_number (event->timestamp),
5496 Qnil))));
5498 head = modify_event_symbol (0, event->modifiers,
5499 Qmouse_wheel, Qnil,
5500 lispy_mouse_wheel_names,
5501 &mouse_wheel_syms, 1);
5502 return Fcons (head,
5503 Fcons (position,
5504 /* Insert 1 here so event-click-count works. */
5505 Fcons (make_number (1),
5506 Fcons (make_number (event->code),
5507 Qnil))));
5510 #endif /* WINDOWSNT || MAC_OSX */
5512 case DRAG_N_DROP_EVENT:
5514 int part;
5515 FRAME_PTR f;
5516 Lisp_Object window;
5517 Lisp_Object posn;
5518 Lisp_Object files;
5520 /* The frame_or_window field should be a cons of the frame in
5521 which the event occurred and a list of the filenames
5522 dropped. */
5523 if (! CONSP (event->frame_or_window))
5524 abort ();
5526 f = XFRAME (XCAR (event->frame_or_window));
5527 files = XCDR (event->frame_or_window);
5529 /* Ignore mouse events that were made on frames that
5530 have been deleted. */
5531 if (! FRAME_LIVE_P (f))
5532 return Qnil;
5534 window = window_from_coordinates (f, XINT (event->x),
5535 XINT (event->y), &part, 0);
5537 if (!WINDOWP (window))
5539 window = XCAR (event->frame_or_window);
5540 posn = Qnil;
5542 else
5544 /* It's an event in window `window' at frame coordinates
5545 event->x/ event->y. */
5546 struct window *w = XWINDOW (window);
5548 /* Get window relative coordinates. */
5549 int wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (event->x));
5550 int wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (event->y));
5552 /* Set event coordinates to window-relative coordinates
5553 for constructing the Lisp event below. */
5554 XSETINT (event->x, wx);
5555 XSETINT (event->y, wy);
5557 if (part == 1)
5558 posn = Qmode_line;
5559 else if (part == 2)
5560 posn = Qvertical_line;
5561 else if (part == 3)
5562 posn = Qheader_line;
5563 else
5565 Lisp_Object object;
5566 struct display_pos p;
5567 buffer_posn_from_coords (w, &wx, &wy, &object, &p);
5568 posn = make_number (CHARPOS (p.pos));
5573 Lisp_Object head, position;
5575 position
5576 = Fcons (window,
5577 Fcons (posn,
5578 Fcons (Fcons (event->x, event->y),
5579 Fcons (make_number (event->timestamp),
5580 Qnil))));
5582 head = modify_event_symbol (0, event->modifiers,
5583 Qdrag_n_drop, Qnil,
5584 lispy_drag_n_drop_names,
5585 &drag_n_drop_syms, 1);
5586 return Fcons (head,
5587 Fcons (position,
5588 Fcons (files,
5589 Qnil)));
5592 #endif /* HAVE_MOUSE */
5594 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
5595 || defined (USE_GTK)
5596 case MENU_BAR_EVENT:
5597 if (EQ (event->arg, event->frame_or_window))
5598 /* This is the prefix key. We translate this to
5599 `(menu_bar)' because the code in keyboard.c for menu
5600 events, which we use, relies on this. */
5601 return Fcons (Qmenu_bar, Qnil);
5602 return event->arg;
5603 #endif
5605 case TOOL_BAR_EVENT:
5606 if (EQ (event->arg, event->frame_or_window))
5607 /* This is the prefix key. We translate this to
5608 `(tool_bar)' because the code in keyboard.c for tool bar
5609 events, which we use, relies on this. */
5610 return Fcons (Qtool_bar, Qnil);
5611 else if (SYMBOLP (event->arg))
5612 return apply_modifiers (event->modifiers, event->arg);
5613 return event->arg;
5615 case USER_SIGNAL_EVENT:
5616 /* A user signal. */
5617 return *lispy_user_signals[event->code];
5619 case SAVE_SESSION_EVENT:
5620 return Qsave_session;
5622 /* The 'kind' field of the event is something we don't recognize. */
5623 default:
5624 abort ();
5628 #ifdef HAVE_MOUSE
5630 static Lisp_Object
5631 make_lispy_movement (frame, bar_window, part, x, y, time)
5632 FRAME_PTR frame;
5633 Lisp_Object bar_window;
5634 enum scroll_bar_part part;
5635 Lisp_Object x, y;
5636 unsigned long time;
5638 /* Is it a scroll bar movement? */
5639 if (frame && ! NILP (bar_window))
5641 Lisp_Object part_sym;
5643 part_sym = *scroll_bar_parts[(int) part];
5644 return Fcons (Qscroll_bar_movement,
5645 (Fcons (Fcons (bar_window,
5646 Fcons (Qvertical_scroll_bar,
5647 Fcons (Fcons (x, y),
5648 Fcons (make_number (time),
5649 Fcons (part_sym,
5650 Qnil))))),
5651 Qnil)));
5654 /* Or is it an ordinary mouse movement? */
5655 else
5657 int area;
5658 Lisp_Object window;
5659 Lisp_Object posn;
5661 if (frame)
5662 /* It's in a frame; which window on that frame? */
5663 window = window_from_coordinates (frame, XINT (x), XINT (y), &area, 0);
5664 else
5665 window = Qnil;
5667 if (WINDOWP (window))
5669 struct window *w = XWINDOW (window);
5670 int wx, wy;
5672 /* Get window relative coordinates. */
5673 wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (x));
5674 wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (y));
5675 XSETINT (x, wx);
5676 XSETINT (y, wy);
5678 if (area == 1)
5679 posn = Qmode_line;
5680 else if (area == 2)
5681 posn = Qvertical_line;
5682 else if (area == 3)
5683 posn = Qheader_line;
5684 else
5686 Lisp_Object object;
5687 struct display_pos p;
5688 buffer_posn_from_coords (w, &wx, &wy, &object, &p);
5689 posn = make_number (CHARPOS (p.pos));
5692 else if (frame != 0)
5694 XSETFRAME (window, frame);
5695 posn = Qnil;
5697 else
5699 window = Qnil;
5700 posn = Qnil;
5701 XSETFASTINT (x, 0);
5702 XSETFASTINT (y, 0);
5705 return Fcons (Qmouse_movement,
5706 Fcons (Fcons (window,
5707 Fcons (posn,
5708 Fcons (Fcons (x, y),
5709 Fcons (make_number (time),
5710 Qnil)))),
5711 Qnil));
5715 #endif /* HAVE_MOUSE */
5717 /* Construct a switch frame event. */
5718 static Lisp_Object
5719 make_lispy_switch_frame (frame)
5720 Lisp_Object frame;
5722 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
5725 /* Manipulating modifiers. */
5727 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
5729 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
5730 SYMBOL's name of the end of the modifiers; the string from this
5731 position is the unmodified symbol name.
5733 This doesn't use any caches. */
5735 static int
5736 parse_modifiers_uncached (symbol, modifier_end)
5737 Lisp_Object symbol;
5738 int *modifier_end;
5740 Lisp_Object name;
5741 int i;
5742 int modifiers;
5744 CHECK_SYMBOL (symbol);
5746 modifiers = 0;
5747 name = SYMBOL_NAME (symbol);
5749 for (i = 0; i+2 <= SBYTES (name); )
5751 int this_mod_end = 0;
5752 int this_mod = 0;
5754 /* See if the name continues with a modifier word.
5755 Check that the word appears, but don't check what follows it.
5756 Set this_mod and this_mod_end to record what we find. */
5758 switch (SREF (name, i))
5760 #define SINGLE_LETTER_MOD(BIT) \
5761 (this_mod_end = i + 1, this_mod = BIT)
5763 case 'A':
5764 SINGLE_LETTER_MOD (alt_modifier);
5765 break;
5767 case 'C':
5768 SINGLE_LETTER_MOD (ctrl_modifier);
5769 break;
5771 case 'H':
5772 SINGLE_LETTER_MOD (hyper_modifier);
5773 break;
5775 case 'M':
5776 SINGLE_LETTER_MOD (meta_modifier);
5777 break;
5779 case 'S':
5780 SINGLE_LETTER_MOD (shift_modifier);
5781 break;
5783 case 's':
5784 SINGLE_LETTER_MOD (super_modifier);
5785 break;
5787 #undef SINGLE_LETTER_MOD
5790 /* If we found no modifier, stop looking for them. */
5791 if (this_mod_end == 0)
5792 break;
5794 /* Check there is a dash after the modifier, so that it
5795 really is a modifier. */
5796 if (this_mod_end >= SBYTES (name)
5797 || SREF (name, this_mod_end) != '-')
5798 break;
5800 /* This modifier is real; look for another. */
5801 modifiers |= this_mod;
5802 i = this_mod_end + 1;
5805 /* Should we include the `click' modifier? */
5806 if (! (modifiers & (down_modifier | drag_modifier
5807 | double_modifier | triple_modifier))
5808 && i + 7 == SBYTES (name)
5809 && strncmp (SDATA (name) + i, "mouse-", 6) == 0
5810 && ('0' <= SREF (name, i + 6) && SREF (name, i + 6) <= '9'))
5811 modifiers |= click_modifier;
5813 if (modifier_end)
5814 *modifier_end = i;
5816 return modifiers;
5819 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
5820 prepended to the string BASE[0..BASE_LEN-1].
5821 This doesn't use any caches. */
5822 static Lisp_Object
5823 apply_modifiers_uncached (modifiers, base, base_len, base_len_byte)
5824 int modifiers;
5825 char *base;
5826 int base_len, base_len_byte;
5828 /* Since BASE could contain nulls, we can't use intern here; we have
5829 to use Fintern, which expects a genuine Lisp_String, and keeps a
5830 reference to it. */
5831 char *new_mods
5832 = (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
5833 int mod_len;
5836 char *p = new_mods;
5838 /* Only the event queue may use the `up' modifier; it should always
5839 be turned into a click or drag event before presented to lisp code. */
5840 if (modifiers & up_modifier)
5841 abort ();
5843 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
5844 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
5845 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
5846 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
5847 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
5848 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
5849 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
5850 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
5851 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
5852 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
5853 /* The click modifier is denoted by the absence of other modifiers. */
5855 *p = '\0';
5857 mod_len = p - new_mods;
5861 Lisp_Object new_name;
5863 new_name = make_uninit_multibyte_string (mod_len + base_len,
5864 mod_len + base_len_byte);
5865 bcopy (new_mods, SDATA (new_name), mod_len);
5866 bcopy (base, SDATA (new_name) + mod_len, base_len_byte);
5868 return Fintern (new_name, Qnil);
5873 static char *modifier_names[] =
5875 "up", "down", "drag", "click", "double", "triple", 0, 0,
5876 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5877 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
5879 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
5881 static Lisp_Object modifier_symbols;
5883 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
5884 static Lisp_Object
5885 lispy_modifier_list (modifiers)
5886 int modifiers;
5888 Lisp_Object modifier_list;
5889 int i;
5891 modifier_list = Qnil;
5892 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
5893 if (modifiers & (1<<i))
5894 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
5895 modifier_list);
5897 return modifier_list;
5901 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
5902 where UNMODIFIED is the unmodified form of SYMBOL,
5903 MASK is the set of modifiers present in SYMBOL's name.
5904 This is similar to parse_modifiers_uncached, but uses the cache in
5905 SYMBOL's Qevent_symbol_element_mask property, and maintains the
5906 Qevent_symbol_elements property. */
5908 Lisp_Object
5909 parse_modifiers (symbol)
5910 Lisp_Object symbol;
5912 Lisp_Object elements;
5914 elements = Fget (symbol, Qevent_symbol_element_mask);
5915 if (CONSP (elements))
5916 return elements;
5917 else
5919 int end;
5920 int modifiers = parse_modifiers_uncached (symbol, &end);
5921 Lisp_Object unmodified;
5922 Lisp_Object mask;
5924 unmodified = Fintern (make_string (SDATA (SYMBOL_NAME (symbol)) + end,
5925 SBYTES (SYMBOL_NAME (symbol)) - end),
5926 Qnil);
5928 if (modifiers & ~VALMASK)
5929 abort ();
5930 XSETFASTINT (mask, modifiers);
5931 elements = Fcons (unmodified, Fcons (mask, Qnil));
5933 /* Cache the parsing results on SYMBOL. */
5934 Fput (symbol, Qevent_symbol_element_mask,
5935 elements);
5936 Fput (symbol, Qevent_symbol_elements,
5937 Fcons (unmodified, lispy_modifier_list (modifiers)));
5939 /* Since we know that SYMBOL is modifiers applied to unmodified,
5940 it would be nice to put that in unmodified's cache.
5941 But we can't, since we're not sure that parse_modifiers is
5942 canonical. */
5944 return elements;
5948 /* Apply the modifiers MODIFIERS to the symbol BASE.
5949 BASE must be unmodified.
5951 This is like apply_modifiers_uncached, but uses BASE's
5952 Qmodifier_cache property, if present. It also builds
5953 Qevent_symbol_elements properties, since it has that info anyway.
5955 apply_modifiers copies the value of BASE's Qevent_kind property to
5956 the modified symbol. */
5957 static Lisp_Object
5958 apply_modifiers (modifiers, base)
5959 int modifiers;
5960 Lisp_Object base;
5962 Lisp_Object cache, index, entry, new_symbol;
5964 /* Mask out upper bits. We don't know where this value's been. */
5965 modifiers &= VALMASK;
5967 /* The click modifier never figures into cache indices. */
5968 cache = Fget (base, Qmodifier_cache);
5969 XSETFASTINT (index, (modifiers & ~click_modifier));
5970 entry = assq_no_quit (index, cache);
5972 if (CONSP (entry))
5973 new_symbol = XCDR (entry);
5974 else
5976 /* We have to create the symbol ourselves. */
5977 new_symbol = apply_modifiers_uncached (modifiers,
5978 SDATA (SYMBOL_NAME (base)),
5979 SCHARS (SYMBOL_NAME (base)),
5980 SBYTES (SYMBOL_NAME (base)));
5982 /* Add the new symbol to the base's cache. */
5983 entry = Fcons (index, new_symbol);
5984 Fput (base, Qmodifier_cache, Fcons (entry, cache));
5986 /* We have the parsing info now for free, so add it to the caches. */
5987 XSETFASTINT (index, modifiers);
5988 Fput (new_symbol, Qevent_symbol_element_mask,
5989 Fcons (base, Fcons (index, Qnil)));
5990 Fput (new_symbol, Qevent_symbol_elements,
5991 Fcons (base, lispy_modifier_list (modifiers)));
5994 /* Make sure this symbol is of the same kind as BASE.
5996 You'd think we could just set this once and for all when we
5997 intern the symbol above, but reorder_modifiers may call us when
5998 BASE's property isn't set right; we can't assume that just
5999 because it has a Qmodifier_cache property it must have its
6000 Qevent_kind set right as well. */
6001 if (NILP (Fget (new_symbol, Qevent_kind)))
6003 Lisp_Object kind;
6005 kind = Fget (base, Qevent_kind);
6006 if (! NILP (kind))
6007 Fput (new_symbol, Qevent_kind, kind);
6010 return new_symbol;
6014 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
6015 return a symbol with the modifiers placed in the canonical order.
6016 Canonical order is alphabetical, except for down and drag, which
6017 always come last. The 'click' modifier is never written out.
6019 Fdefine_key calls this to make sure that (for example) C-M-foo
6020 and M-C-foo end up being equivalent in the keymap. */
6022 Lisp_Object
6023 reorder_modifiers (symbol)
6024 Lisp_Object symbol;
6026 /* It's hopefully okay to write the code this way, since everything
6027 will soon be in caches, and no consing will be done at all. */
6028 Lisp_Object parsed;
6030 parsed = parse_modifiers (symbol);
6031 return apply_modifiers ((int) XINT (XCAR (XCDR (parsed))),
6032 XCAR (parsed));
6036 /* For handling events, we often want to produce a symbol whose name
6037 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
6038 to some base, like the name of a function key or mouse button.
6039 modify_event_symbol produces symbols of this sort.
6041 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
6042 is the name of the i'th symbol. TABLE_SIZE is the number of elements
6043 in the table.
6045 Alternatively, NAME_ALIST_OR_STEM is either an alist mapping codes
6046 into symbol names, or a string specifying a name stem used to
6047 construct a symbol name or the form `STEM-N', where N is the decimal
6048 representation of SYMBOL_NUM. NAME_ALIST_OR_STEM is used if it is
6049 non-nil; otherwise NAME_TABLE is used.
6051 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
6052 persist between calls to modify_event_symbol that it can use to
6053 store a cache of the symbols it's generated for this NAME_TABLE
6054 before. The object stored there may be a vector or an alist.
6056 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
6058 MODIFIERS is a set of modifier bits (as given in struct input_events)
6059 whose prefixes should be applied to the symbol name.
6061 SYMBOL_KIND is the value to be placed in the event_kind property of
6062 the returned symbol.
6064 The symbols we create are supposed to have an
6065 `event-symbol-elements' property, which lists the modifiers present
6066 in the symbol's name. */
6068 static Lisp_Object
6069 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist_or_stem,
6070 name_table, symbol_table, table_size)
6071 int symbol_num;
6072 unsigned modifiers;
6073 Lisp_Object symbol_kind;
6074 Lisp_Object name_alist_or_stem;
6075 char **name_table;
6076 Lisp_Object *symbol_table;
6077 unsigned int table_size;
6079 Lisp_Object value;
6080 Lisp_Object symbol_int;
6082 /* Get rid of the "vendor-specific" bit here. */
6083 XSETINT (symbol_int, symbol_num & 0xffffff);
6085 /* Is this a request for a valid symbol? */
6086 if (symbol_num < 0 || symbol_num >= table_size)
6087 return Qnil;
6089 if (CONSP (*symbol_table))
6090 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
6092 /* If *symbol_table doesn't seem to be initialized properly, fix that.
6093 *symbol_table should be a lisp vector TABLE_SIZE elements long,
6094 where the Nth element is the symbol for NAME_TABLE[N], or nil if
6095 we've never used that symbol before. */
6096 else
6098 if (! VECTORP (*symbol_table)
6099 || XVECTOR (*symbol_table)->size != table_size)
6101 Lisp_Object size;
6103 XSETFASTINT (size, table_size);
6104 *symbol_table = Fmake_vector (size, Qnil);
6107 value = XVECTOR (*symbol_table)->contents[symbol_num];
6110 /* Have we already used this symbol before? */
6111 if (NILP (value))
6113 /* No; let's create it. */
6114 if (CONSP (name_alist_or_stem))
6115 value = Fcdr_safe (Fassq (symbol_int, name_alist_or_stem));
6116 else if (STRINGP (name_alist_or_stem))
6118 int len = SBYTES (name_alist_or_stem);
6119 char *buf = (char *) alloca (len + 50);
6120 if (sizeof (int) == sizeof (EMACS_INT))
6121 sprintf (buf, "%s-%d", SDATA (name_alist_or_stem),
6122 XINT (symbol_int) + 1);
6123 else if (sizeof (long) == sizeof (EMACS_INT))
6124 sprintf (buf, "%s-%ld", SDATA (name_alist_or_stem),
6125 XINT (symbol_int) + 1);
6126 value = intern (buf);
6128 else if (name_table != 0 && name_table[symbol_num])
6129 value = intern (name_table[symbol_num]);
6131 #ifdef HAVE_WINDOW_SYSTEM
6132 if (NILP (value))
6134 char *name = x_get_keysym_name (symbol_num);
6135 if (name)
6136 value = intern (name);
6138 #endif
6140 if (NILP (value))
6142 char buf[20];
6143 sprintf (buf, "key-%d", symbol_num);
6144 value = intern (buf);
6147 if (CONSP (*symbol_table))
6148 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
6149 else
6150 XVECTOR (*symbol_table)->contents[symbol_num] = value;
6152 /* Fill in the cache entries for this symbol; this also
6153 builds the Qevent_symbol_elements property, which the user
6154 cares about. */
6155 apply_modifiers (modifiers & click_modifier, value);
6156 Fput (value, Qevent_kind, symbol_kind);
6159 /* Apply modifiers to that symbol. */
6160 return apply_modifiers (modifiers, value);
6163 /* Convert a list that represents an event type,
6164 such as (ctrl meta backspace), into the usual representation of that
6165 event type as a number or a symbol. */
6167 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
6168 doc: /* Convert the event description list EVENT-DESC to an event type.
6169 EVENT-DESC should contain one base event type (a character or symbol)
6170 and zero or more modifier names (control, meta, hyper, super, shift, alt,
6171 drag, down, double or triple). The base must be last.
6172 The return value is an event type (a character or symbol) which
6173 has the same base event type and all the specified modifiers. */)
6174 (event_desc)
6175 Lisp_Object event_desc;
6177 Lisp_Object base;
6178 int modifiers = 0;
6179 Lisp_Object rest;
6181 base = Qnil;
6182 rest = event_desc;
6183 while (CONSP (rest))
6185 Lisp_Object elt;
6186 int this = 0;
6188 elt = XCAR (rest);
6189 rest = XCDR (rest);
6191 /* Given a symbol, see if it is a modifier name. */
6192 if (SYMBOLP (elt) && CONSP (rest))
6193 this = parse_solitary_modifier (elt);
6195 if (this != 0)
6196 modifiers |= this;
6197 else if (!NILP (base))
6198 error ("Two bases given in one event");
6199 else
6200 base = elt;
6204 /* Let the symbol A refer to the character A. */
6205 if (SYMBOLP (base) && SCHARS (SYMBOL_NAME (base)) == 1)
6206 XSETINT (base, SREF (SYMBOL_NAME (base), 0));
6208 if (INTEGERP (base))
6210 /* Turn (shift a) into A. */
6211 if ((modifiers & shift_modifier) != 0
6212 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
6214 XSETINT (base, XINT (base) - ('a' - 'A'));
6215 modifiers &= ~shift_modifier;
6218 /* Turn (control a) into C-a. */
6219 if (modifiers & ctrl_modifier)
6220 return make_number ((modifiers & ~ctrl_modifier)
6221 | make_ctrl_char (XINT (base)));
6222 else
6223 return make_number (modifiers | XINT (base));
6225 else if (SYMBOLP (base))
6226 return apply_modifiers (modifiers, base);
6227 else
6229 error ("Invalid base event");
6230 return Qnil;
6234 /* Try to recognize SYMBOL as a modifier name.
6235 Return the modifier flag bit, or 0 if not recognized. */
6237 static int
6238 parse_solitary_modifier (symbol)
6239 Lisp_Object symbol;
6241 Lisp_Object name = SYMBOL_NAME (symbol);
6243 switch (SREF (name, 0))
6245 #define SINGLE_LETTER_MOD(BIT) \
6246 if (SBYTES (name) == 1) \
6247 return BIT;
6249 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
6250 if (LEN == SBYTES (name) \
6251 && ! strncmp (SDATA (name), NAME, LEN)) \
6252 return BIT;
6254 case 'A':
6255 SINGLE_LETTER_MOD (alt_modifier);
6256 break;
6258 case 'a':
6259 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
6260 break;
6262 case 'C':
6263 SINGLE_LETTER_MOD (ctrl_modifier);
6264 break;
6266 case 'c':
6267 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
6268 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
6269 break;
6271 case 'H':
6272 SINGLE_LETTER_MOD (hyper_modifier);
6273 break;
6275 case 'h':
6276 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
6277 break;
6279 case 'M':
6280 SINGLE_LETTER_MOD (meta_modifier);
6281 break;
6283 case 'm':
6284 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
6285 break;
6287 case 'S':
6288 SINGLE_LETTER_MOD (shift_modifier);
6289 break;
6291 case 's':
6292 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
6293 MULTI_LETTER_MOD (super_modifier, "super", 5);
6294 SINGLE_LETTER_MOD (super_modifier);
6295 break;
6297 case 'd':
6298 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
6299 MULTI_LETTER_MOD (down_modifier, "down", 4);
6300 MULTI_LETTER_MOD (double_modifier, "double", 6);
6301 break;
6303 case 't':
6304 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
6305 break;
6307 #undef SINGLE_LETTER_MOD
6308 #undef MULTI_LETTER_MOD
6311 return 0;
6314 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
6315 Such a list is not valid as an event,
6316 but it can be a Lucid-style event type list. */
6319 lucid_event_type_list_p (object)
6320 Lisp_Object object;
6322 Lisp_Object tail;
6324 if (! CONSP (object))
6325 return 0;
6327 if (EQ (XCAR (object), Qhelp_echo)
6328 || EQ (XCAR (object), Qvertical_line)
6329 || EQ (XCAR (object), Qmode_line)
6330 || EQ (XCAR (object), Qheader_line))
6331 return 0;
6333 for (tail = object; CONSP (tail); tail = XCDR (tail))
6335 Lisp_Object elt;
6336 elt = XCAR (tail);
6337 if (! (INTEGERP (elt) || SYMBOLP (elt)))
6338 return 0;
6341 return NILP (tail);
6344 /* Store into *addr a value nonzero if terminal input chars are available.
6345 Serves the purpose of ioctl (0, FIONREAD, addr)
6346 but works even if FIONREAD does not exist.
6347 (In fact, this may actually read some input.)
6349 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe.
6350 If FILTER_EVENTS is nonzero, ignore internal events (FOCUS_IN_EVENT). */
6352 static void
6353 get_filtered_input_pending (addr, do_timers_now, filter_events)
6354 int *addr;
6355 int do_timers_now;
6356 int filter_events;
6358 /* First of all, have we already counted some input? */
6359 *addr = (!NILP (Vquit_flag)
6360 || readable_filtered_events (do_timers_now, filter_events));
6362 /* If input is being read as it arrives, and we have none, there is none. */
6363 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
6364 return;
6366 /* Try to read some input and see how much we get. */
6367 gobble_input (0);
6368 *addr = (!NILP (Vquit_flag)
6369 || readable_filtered_events (do_timers_now, filter_events));
6372 /* Store into *addr a value nonzero if terminal input chars are available.
6373 Serves the purpose of ioctl (0, FIONREAD, addr)
6374 but works even if FIONREAD does not exist.
6375 (In fact, this may actually read some input.)
6377 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe. */
6379 static void
6380 get_input_pending (addr, do_timers_now)
6381 int *addr;
6382 int do_timers_now;
6384 get_filtered_input_pending (addr, do_timers_now, 0);
6387 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
6389 void
6390 gobble_input (expected)
6391 int expected;
6393 #ifndef VMS
6394 #ifdef SIGIO
6395 if (interrupt_input)
6397 SIGMASKTYPE mask;
6398 mask = sigblock (sigmask (SIGIO));
6399 read_avail_input (expected);
6400 sigsetmask (mask);
6402 else
6403 #ifdef POLL_FOR_INPUT
6404 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
6406 SIGMASKTYPE mask;
6407 mask = sigblock (sigmask (SIGALRM));
6408 read_avail_input (expected);
6409 sigsetmask (mask);
6411 else
6412 #endif
6413 #endif
6414 read_avail_input (expected);
6415 #endif
6418 /* Put a BUFFER_SWITCH_EVENT in the buffer
6419 so that read_key_sequence will notice the new current buffer. */
6421 void
6422 record_asynch_buffer_change ()
6424 struct input_event event;
6425 Lisp_Object tem;
6427 event.kind = BUFFER_SWITCH_EVENT;
6428 event.frame_or_window = Qnil;
6429 event.arg = Qnil;
6431 #ifdef subprocesses
6432 /* We don't need a buffer-switch event unless Emacs is waiting for input.
6433 The purpose of the event is to make read_key_sequence look up the
6434 keymaps again. If we aren't in read_key_sequence, we don't need one,
6435 and the event could cause trouble by messing up (input-pending-p). */
6436 tem = Fwaiting_for_user_input_p ();
6437 if (NILP (tem))
6438 return;
6439 #else
6440 /* We never need these events if we have no asynchronous subprocesses. */
6441 return;
6442 #endif
6444 /* Make sure no interrupt happens while storing the event. */
6445 #ifdef SIGIO
6446 if (interrupt_input)
6448 SIGMASKTYPE mask;
6449 mask = sigblock (sigmask (SIGIO));
6450 kbd_buffer_store_event (&event);
6451 sigsetmask (mask);
6453 else
6454 #endif
6456 stop_polling ();
6457 kbd_buffer_store_event (&event);
6458 start_polling ();
6462 #ifndef VMS
6464 /* Read any terminal input already buffered up by the system
6465 into the kbd_buffer, but do not wait.
6467 EXPECTED should be nonzero if the caller knows there is some input.
6469 Except on VMS, all input is read by this function.
6470 If interrupt_input is nonzero, this function MUST be called
6471 only when SIGIO is blocked.
6473 Returns the number of keyboard chars read, or -1 meaning
6474 this is a bad time to try to read input. */
6476 static int
6477 read_avail_input (expected)
6478 int expected;
6480 struct input_event buf[KBD_BUFFER_SIZE];
6481 register int i;
6482 int nread;
6484 if (read_socket_hook)
6485 /* No need for FIONREAD or fcntl; just say don't wait. */
6486 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE, expected);
6487 else
6489 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
6490 the kbd_buffer can really hold. That may prevent loss
6491 of characters on some systems when input is stuffed at us. */
6492 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
6493 int n_to_read;
6495 /* Determine how many characters we should *try* to read. */
6496 #ifdef WINDOWSNT
6497 return 0;
6498 #else /* not WINDOWSNT */
6499 #ifdef MSDOS
6500 n_to_read = dos_keysns ();
6501 if (n_to_read == 0)
6502 return 0;
6503 #else /* not MSDOS */
6504 #ifdef FIONREAD
6505 /* Find out how much input is available. */
6506 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
6507 /* Formerly simply reported no input, but that sometimes led to
6508 a failure of Emacs to terminate.
6509 SIGHUP seems appropriate if we can't reach the terminal. */
6510 /* ??? Is it really right to send the signal just to this process
6511 rather than to the whole process group?
6512 Perhaps on systems with FIONREAD Emacs is alone in its group. */
6513 kill (getpid (), SIGHUP);
6514 if (n_to_read == 0)
6515 return 0;
6516 if (n_to_read > sizeof cbuf)
6517 n_to_read = sizeof cbuf;
6518 #else /* no FIONREAD */
6519 #if defined (USG) || defined (DGUX)
6520 /* Read some input if available, but don't wait. */
6521 n_to_read = sizeof cbuf;
6522 fcntl (input_fd, F_SETFL, O_NDELAY);
6523 #else
6524 you lose;
6525 #endif
6526 #endif
6527 #endif /* not MSDOS */
6528 #endif /* not WINDOWSNT */
6530 /* Now read; for one reason or another, this will not block.
6531 NREAD is set to the number of chars read. */
6534 #ifdef MSDOS
6535 cbuf[0] = dos_keyread ();
6536 nread = 1;
6537 #else
6538 nread = emacs_read (input_fd, cbuf, n_to_read);
6539 #endif
6540 /* POSIX infers that processes which are not in the session leader's
6541 process group won't get SIGHUP's at logout time. BSDI adheres to
6542 this part standard and returns -1 from read (0) with errno==EIO
6543 when the control tty is taken away.
6544 Jeffrey Honig <jch@bsdi.com> says this is generally safe. */
6545 if (nread == -1 && errno == EIO)
6546 kill (0, SIGHUP);
6547 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
6548 /* The kernel sometimes fails to deliver SIGHUP for ptys.
6549 This looks incorrect, but it isn't, because _BSD causes
6550 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
6551 and that causes a value other than 0 when there is no input. */
6552 if (nread == 0)
6553 kill (0, SIGHUP);
6554 #endif
6556 while (
6557 /* We used to retry the read if it was interrupted.
6558 But this does the wrong thing when O_NDELAY causes
6559 an EAGAIN error. Does anybody know of a situation
6560 where a retry is actually needed? */
6561 #if 0
6562 nread < 0 && (errno == EAGAIN
6563 #ifdef EFAULT
6564 || errno == EFAULT
6565 #endif
6566 #ifdef EBADSLT
6567 || errno == EBADSLT
6568 #endif
6570 #else
6572 #endif
6575 #ifndef FIONREAD
6576 #if defined (USG) || defined (DGUX)
6577 fcntl (input_fd, F_SETFL, 0);
6578 #endif /* USG or DGUX */
6579 #endif /* no FIONREAD */
6580 for (i = 0; i < nread; i++)
6582 buf[i].kind = ASCII_KEYSTROKE_EVENT;
6583 buf[i].modifiers = 0;
6584 if (meta_key == 1 && (cbuf[i] & 0x80))
6585 buf[i].modifiers = meta_modifier;
6586 if (meta_key != 2)
6587 cbuf[i] &= ~0x80;
6589 buf[i].code = cbuf[i];
6590 buf[i].frame_or_window = selected_frame;
6591 buf[i].arg = Qnil;
6595 /* Scan the chars for C-g and store them in kbd_buffer. */
6596 for (i = 0; i < nread; i++)
6598 kbd_buffer_store_event (&buf[i]);
6599 /* Don't look at input that follows a C-g too closely.
6600 This reduces lossage due to autorepeat on C-g. */
6601 if (buf[i].kind == ASCII_KEYSTROKE_EVENT
6602 && buf[i].code == quit_char)
6603 break;
6606 return nread;
6608 #endif /* not VMS */
6610 #ifdef SIGIO /* for entire page */
6611 /* Note SIGIO has been undef'd if FIONREAD is missing. */
6613 SIGTYPE
6614 input_available_signal (signo)
6615 int signo;
6617 /* Must preserve main program's value of errno. */
6618 int old_errno = errno;
6619 #ifdef BSD4_1
6620 extern int select_alarmed;
6621 #endif
6623 #if defined (USG) && !defined (POSIX_SIGNALS)
6624 /* USG systems forget handlers when they are used;
6625 must reestablish each time */
6626 signal (signo, input_available_signal);
6627 #endif /* USG */
6629 #ifdef BSD4_1
6630 sigisheld (SIGIO);
6631 #endif
6633 if (input_available_clear_time)
6634 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
6636 while (1)
6638 int nread;
6639 nread = read_avail_input (1);
6640 /* -1 means it's not ok to read the input now.
6641 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
6642 0 means there was no keyboard input available. */
6643 if (nread <= 0)
6644 break;
6646 #ifdef BSD4_1
6647 select_alarmed = 1; /* Force the select emulator back to life */
6648 #endif
6651 #ifdef BSD4_1
6652 sigfree ();
6653 #endif
6654 errno = old_errno;
6656 #endif /* SIGIO */
6658 /* Send ourselves a SIGIO.
6660 This function exists so that the UNBLOCK_INPUT macro in
6661 blockinput.h can have some way to take care of input we put off
6662 dealing with, without assuming that every file which uses
6663 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
6664 void
6665 reinvoke_input_signal ()
6667 #ifdef SIGIO
6668 kill (getpid (), SIGIO);
6669 #endif
6674 static void menu_bar_item P_ ((Lisp_Object, Lisp_Object));
6675 static void menu_bar_one_keymap P_ ((Lisp_Object));
6677 /* These variables hold the vector under construction within
6678 menu_bar_items and its subroutines, and the current index
6679 for storing into that vector. */
6680 static Lisp_Object menu_bar_items_vector;
6681 static int menu_bar_items_index;
6683 /* Return a vector of menu items for a menu bar, appropriate
6684 to the current buffer. Each item has three elements in the vector:
6685 KEY STRING MAPLIST.
6687 OLD is an old vector we can optionally reuse, or nil. */
6689 Lisp_Object
6690 menu_bar_items (old)
6691 Lisp_Object old;
6693 /* The number of keymaps we're scanning right now, and the number of
6694 keymaps we have allocated space for. */
6695 int nmaps;
6697 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
6698 in the current keymaps, or nil where it is not a prefix. */
6699 Lisp_Object *maps;
6701 Lisp_Object def, tail;
6703 Lisp_Object result;
6705 int mapno;
6706 Lisp_Object oquit;
6708 int i;
6710 struct gcpro gcpro1;
6712 /* In order to build the menus, we need to call the keymap
6713 accessors. They all call QUIT. But this function is called
6714 during redisplay, during which a quit is fatal. So inhibit
6715 quitting while building the menus.
6716 We do this instead of specbind because (1) errors will clear it anyway
6717 and (2) this avoids risk of specpdl overflow. */
6718 oquit = Vinhibit_quit;
6719 Vinhibit_quit = Qt;
6721 if (!NILP (old))
6722 menu_bar_items_vector = old;
6723 else
6724 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
6725 menu_bar_items_index = 0;
6727 GCPRO1 (menu_bar_items_vector);
6729 /* Build our list of keymaps.
6730 If we recognize a function key and replace its escape sequence in
6731 keybuf with its symbol, or if the sequence starts with a mouse
6732 click and we need to switch buffers, we jump back here to rebuild
6733 the initial keymaps from the current buffer. */
6735 Lisp_Object *tmaps;
6737 /* Should overriding-terminal-local-map and overriding-local-map apply? */
6738 if (!NILP (Voverriding_local_map_menu_flag))
6740 /* Yes, use them (if non-nil) as well as the global map. */
6741 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
6742 nmaps = 0;
6743 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6744 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6745 if (!NILP (Voverriding_local_map))
6746 maps[nmaps++] = Voverriding_local_map;
6748 else
6750 /* No, so use major and minor mode keymaps and keymap property.
6751 Note that menu-bar bindings in the local-map and keymap
6752 properties may not work reliable, as they are only
6753 recognized when the menu-bar (or mode-line) is updated,
6754 which does not normally happen after every command. */
6755 Lisp_Object tem;
6756 int nminor;
6757 nminor = current_minor_maps (NULL, &tmaps);
6758 maps = (Lisp_Object *) alloca ((nminor + 3) * sizeof (maps[0]));
6759 nmaps = 0;
6760 if (tem = get_local_map (PT, current_buffer, Qkeymap), !NILP (tem))
6761 maps[nmaps++] = tem;
6762 bcopy (tmaps, (void *) (maps + nmaps), nminor * sizeof (maps[0]));
6763 nmaps += nminor;
6764 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
6766 maps[nmaps++] = current_global_map;
6769 /* Look up in each map the dummy prefix key `menu-bar'. */
6771 result = Qnil;
6773 for (mapno = nmaps - 1; mapno >= 0; mapno--)
6774 if (!NILP (maps[mapno]))
6776 def = get_keymap (access_keymap (maps[mapno], Qmenu_bar, 1, 0, 1),
6777 0, 1);
6778 if (CONSP (def))
6779 menu_bar_one_keymap (def);
6782 /* Move to the end those items that should be at the end. */
6784 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCDR (tail))
6786 int i;
6787 int end = menu_bar_items_index;
6789 for (i = 0; i < end; i += 4)
6790 if (EQ (XCAR (tail), XVECTOR (menu_bar_items_vector)->contents[i]))
6792 Lisp_Object tem0, tem1, tem2, tem3;
6793 /* Move the item at index I to the end,
6794 shifting all the others forward. */
6795 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
6796 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
6797 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
6798 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
6799 if (end > i + 4)
6800 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
6801 &XVECTOR (menu_bar_items_vector)->contents[i],
6802 (end - i - 4) * sizeof (Lisp_Object));
6803 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
6804 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
6805 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
6806 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
6807 break;
6811 /* Add nil, nil, nil, nil at the end. */
6812 i = menu_bar_items_index;
6813 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
6815 Lisp_Object tem;
6816 tem = Fmake_vector (make_number (2 * i), Qnil);
6817 bcopy (XVECTOR (menu_bar_items_vector)->contents,
6818 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
6819 menu_bar_items_vector = tem;
6821 /* Add this item. */
6822 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6823 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6824 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6825 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6826 menu_bar_items_index = i;
6828 Vinhibit_quit = oquit;
6829 UNGCPRO;
6830 return menu_bar_items_vector;
6833 /* Scan one map KEYMAP, accumulating any menu items it defines
6834 in menu_bar_items_vector. */
6836 static Lisp_Object menu_bar_one_keymap_changed_items;
6838 static void
6839 menu_bar_one_keymap (keymap)
6840 Lisp_Object keymap;
6842 Lisp_Object tail, item;
6844 menu_bar_one_keymap_changed_items = Qnil;
6846 /* Loop over all keymap entries that have menu strings. */
6847 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
6849 item = XCAR (tail);
6850 if (CONSP (item))
6851 menu_bar_item (XCAR (item), XCDR (item));
6852 else if (VECTORP (item))
6854 /* Loop over the char values represented in the vector. */
6855 int len = XVECTOR (item)->size;
6856 int c;
6857 for (c = 0; c < len; c++)
6859 Lisp_Object character;
6860 XSETFASTINT (character, c);
6861 menu_bar_item (character, XVECTOR (item)->contents[c]);
6867 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
6868 If there's already an item for KEY, add this DEF to it. */
6870 Lisp_Object item_properties;
6872 static void
6873 menu_bar_item (key, item)
6874 Lisp_Object key, item;
6876 struct gcpro gcpro1;
6877 int i;
6878 Lisp_Object tem;
6880 if (EQ (item, Qundefined))
6882 /* If a map has an explicit `undefined' as definition,
6883 discard any previously made menu bar item. */
6885 for (i = 0; i < menu_bar_items_index; i += 4)
6886 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
6888 if (menu_bar_items_index > i + 4)
6889 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
6890 &XVECTOR (menu_bar_items_vector)->contents[i],
6891 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
6892 menu_bar_items_index -= 4;
6896 /* If this keymap has already contributed to this KEY,
6897 don't contribute to it a second time. */
6898 tem = Fmemq (key, menu_bar_one_keymap_changed_items);
6899 if (!NILP (tem) || NILP (item))
6900 return;
6902 menu_bar_one_keymap_changed_items
6903 = Fcons (key, menu_bar_one_keymap_changed_items);
6905 /* We add to menu_bar_one_keymap_changed_items before doing the
6906 parse_menu_item, so that if it turns out it wasn't a menu item,
6907 it still correctly hides any further menu item. */
6908 GCPRO1 (key);
6909 i = parse_menu_item (item, 0, 1);
6910 UNGCPRO;
6911 if (!i)
6912 return;
6914 item = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6916 /* Find any existing item for this KEY. */
6917 for (i = 0; i < menu_bar_items_index; i += 4)
6918 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
6919 break;
6921 /* If we did not find this KEY, add it at the end. */
6922 if (i == menu_bar_items_index)
6924 /* If vector is too small, get a bigger one. */
6925 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
6927 Lisp_Object tem;
6928 tem = Fmake_vector (make_number (2 * i), Qnil);
6929 bcopy (XVECTOR (menu_bar_items_vector)->contents,
6930 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
6931 menu_bar_items_vector = tem;
6934 /* Add this item. */
6935 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
6936 XVECTOR (menu_bar_items_vector)->contents[i++]
6937 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
6938 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (item, Qnil);
6939 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
6940 menu_bar_items_index = i;
6942 /* We did find an item for this KEY. Add ITEM to its list of maps. */
6943 else
6945 Lisp_Object old;
6946 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
6947 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (item, old);
6951 /* This is used as the handler when calling menu_item_eval_property. */
6952 static Lisp_Object
6953 menu_item_eval_property_1 (arg)
6954 Lisp_Object arg;
6956 /* If we got a quit from within the menu computation,
6957 quit all the way out of it. This takes care of C-] in the debugger. */
6958 if (CONSP (arg) && EQ (XCAR (arg), Qquit))
6959 Fsignal (Qquit, Qnil);
6961 return Qnil;
6964 /* Evaluate an expression and return the result (or nil if something
6965 went wrong). Used to evaluate dynamic parts of menu items. */
6966 Lisp_Object
6967 menu_item_eval_property (sexpr)
6968 Lisp_Object sexpr;
6970 int count = SPECPDL_INDEX ();
6971 Lisp_Object val;
6972 specbind (Qinhibit_redisplay, Qt);
6973 val = internal_condition_case_1 (Feval, sexpr, Qerror,
6974 menu_item_eval_property_1);
6975 return unbind_to (count, val);
6978 /* This function parses a menu item and leaves the result in the
6979 vector item_properties.
6980 ITEM is a key binding, a possible menu item.
6981 If NOTREAL is nonzero, only check for equivalent key bindings, don't
6982 evaluate dynamic expressions in the menu item.
6983 INMENUBAR is > 0 when this is considered for an entry in a menu bar
6984 top level.
6985 INMENUBAR is < 0 when this is considered for an entry in a keyboard menu.
6986 parse_menu_item returns true if the item is a menu item and false
6987 otherwise. */
6990 parse_menu_item (item, notreal, inmenubar)
6991 Lisp_Object item;
6992 int notreal, inmenubar;
6994 Lisp_Object def, tem, item_string, start;
6995 Lisp_Object cachelist;
6996 Lisp_Object filter;
6997 Lisp_Object keyhint;
6998 int i;
6999 int newcache = 0;
7001 cachelist = Qnil;
7002 filter = Qnil;
7003 keyhint = Qnil;
7005 if (!CONSP (item))
7006 return 0;
7008 /* Create item_properties vector if necessary. */
7009 if (NILP (item_properties))
7010 item_properties
7011 = Fmake_vector (make_number (ITEM_PROPERTY_ENABLE + 1), Qnil);
7013 /* Initialize optional entries. */
7014 for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++)
7015 AREF (item_properties, i) = Qnil;
7016 AREF (item_properties, ITEM_PROPERTY_ENABLE) = Qt;
7018 /* Save the item here to protect it from GC. */
7019 AREF (item_properties, ITEM_PROPERTY_ITEM) = item;
7021 item_string = XCAR (item);
7023 start = item;
7024 item = XCDR (item);
7025 if (STRINGP (item_string))
7027 /* Old format menu item. */
7028 AREF (item_properties, ITEM_PROPERTY_NAME) = item_string;
7030 /* Maybe help string. */
7031 if (CONSP (item) && STRINGP (XCAR (item)))
7033 AREF (item_properties, ITEM_PROPERTY_HELP) = XCAR (item);
7034 start = item;
7035 item = XCDR (item);
7038 /* Maybe key binding cache. */
7039 if (CONSP (item) && CONSP (XCAR (item))
7040 && (NILP (XCAR (XCAR (item)))
7041 || VECTORP (XCAR (XCAR (item)))))
7043 cachelist = XCAR (item);
7044 item = XCDR (item);
7047 /* This is the real definition--the function to run. */
7048 AREF (item_properties, ITEM_PROPERTY_DEF) = item;
7050 /* Get enable property, if any. */
7051 if (SYMBOLP (item))
7053 tem = Fget (item, Qmenu_enable);
7054 if (!NILP (tem))
7055 AREF (item_properties, ITEM_PROPERTY_ENABLE) = tem;
7058 else if (EQ (item_string, Qmenu_item) && CONSP (item))
7060 /* New format menu item. */
7061 AREF (item_properties, ITEM_PROPERTY_NAME) = XCAR (item);
7062 start = XCDR (item);
7063 if (CONSP (start))
7065 /* We have a real binding. */
7066 AREF (item_properties, ITEM_PROPERTY_DEF) = XCAR (start);
7068 item = XCDR (start);
7069 /* Is there a cache list with key equivalences. */
7070 if (CONSP (item) && CONSP (XCAR (item)))
7072 cachelist = XCAR (item);
7073 item = XCDR (item);
7076 /* Parse properties. */
7077 while (CONSP (item) && CONSP (XCDR (item)))
7079 tem = XCAR (item);
7080 item = XCDR (item);
7082 if (EQ (tem, QCenable))
7083 AREF (item_properties, ITEM_PROPERTY_ENABLE) = XCAR (item);
7084 else if (EQ (tem, QCvisible) && !notreal)
7086 /* If got a visible property and that evaluates to nil
7087 then ignore this item. */
7088 tem = menu_item_eval_property (XCAR (item));
7089 if (NILP (tem))
7090 return 0;
7092 else if (EQ (tem, QChelp))
7093 AREF (item_properties, ITEM_PROPERTY_HELP) = XCAR (item);
7094 else if (EQ (tem, QCfilter))
7095 filter = item;
7096 else if (EQ (tem, QCkey_sequence))
7098 tem = XCAR (item);
7099 if (NILP (cachelist)
7100 && (SYMBOLP (tem) || STRINGP (tem) || VECTORP (tem)))
7101 /* Be GC protected. Set keyhint to item instead of tem. */
7102 keyhint = item;
7104 else if (EQ (tem, QCkeys))
7106 tem = XCAR (item);
7107 if (CONSP (tem) || (STRINGP (tem) && NILP (cachelist)))
7108 AREF (item_properties, ITEM_PROPERTY_KEYEQ) = tem;
7110 else if (EQ (tem, QCbutton) && CONSP (XCAR (item)))
7112 Lisp_Object type;
7113 tem = XCAR (item);
7114 type = XCAR (tem);
7115 if (EQ (type, QCtoggle) || EQ (type, QCradio))
7117 AREF (item_properties, ITEM_PROPERTY_SELECTED)
7118 = XCDR (tem);
7119 AREF (item_properties, ITEM_PROPERTY_TYPE)
7120 = type;
7123 item = XCDR (item);
7126 else if (inmenubar || !NILP (start))
7127 return 0;
7129 else
7130 return 0; /* not a menu item */
7132 /* If item string is not a string, evaluate it to get string.
7133 If we don't get a string, skip this item. */
7134 item_string = AREF (item_properties, ITEM_PROPERTY_NAME);
7135 if (!(STRINGP (item_string) || notreal))
7137 item_string = menu_item_eval_property (item_string);
7138 if (!STRINGP (item_string))
7139 return 0;
7140 AREF (item_properties, ITEM_PROPERTY_NAME) = item_string;
7143 /* If got a filter apply it on definition. */
7144 def = AREF (item_properties, ITEM_PROPERTY_DEF);
7145 if (!NILP (filter))
7147 def = menu_item_eval_property (list2 (XCAR (filter),
7148 list2 (Qquote, def)));
7150 AREF (item_properties, ITEM_PROPERTY_DEF) = def;
7153 /* Enable or disable selection of item. */
7154 tem = AREF (item_properties, ITEM_PROPERTY_ENABLE);
7155 if (!EQ (tem, Qt))
7157 if (notreal)
7158 tem = Qt;
7159 else
7160 tem = menu_item_eval_property (tem);
7161 if (inmenubar && NILP (tem))
7162 return 0; /* Ignore disabled items in menu bar. */
7163 AREF (item_properties, ITEM_PROPERTY_ENABLE) = tem;
7166 /* If we got no definition, this item is just unselectable text which
7167 is OK in a submenu but not in the menubar. */
7168 if (NILP (def))
7169 return (inmenubar ? 0 : 1);
7171 /* See if this is a separate pane or a submenu. */
7172 def = AREF (item_properties, ITEM_PROPERTY_DEF);
7173 tem = get_keymap (def, 0, 1);
7174 /* For a subkeymap, just record its details and exit. */
7175 if (CONSP (tem))
7177 AREF (item_properties, ITEM_PROPERTY_MAP) = tem;
7178 AREF (item_properties, ITEM_PROPERTY_DEF) = tem;
7179 return 1;
7182 /* At the top level in the menu bar, do likewise for commands also.
7183 The menu bar does not display equivalent key bindings anyway.
7184 ITEM_PROPERTY_DEF is already set up properly. */
7185 if (inmenubar > 0)
7186 return 1;
7188 /* This is a command. See if there is an equivalent key binding. */
7189 if (NILP (cachelist))
7191 /* We have to create a cachelist. */
7192 CHECK_IMPURE (start);
7193 XSETCDR (start, Fcons (Fcons (Qnil, Qnil), XCDR (start)));
7194 cachelist = XCAR (XCDR (start));
7195 newcache = 1;
7196 tem = AREF (item_properties, ITEM_PROPERTY_KEYEQ);
7197 if (!NILP (keyhint))
7199 XSETCAR (cachelist, XCAR (keyhint));
7200 newcache = 0;
7202 else if (STRINGP (tem))
7204 XSETCDR (cachelist, Fsubstitute_command_keys (tem));
7205 XSETCAR (cachelist, Qt);
7209 tem = XCAR (cachelist);
7210 if (!EQ (tem, Qt))
7212 int chkcache = 0;
7213 Lisp_Object prefix;
7215 if (!NILP (tem))
7216 tem = Fkey_binding (tem, Qnil, Qnil);
7218 prefix = AREF (item_properties, ITEM_PROPERTY_KEYEQ);
7219 if (CONSP (prefix))
7221 def = XCAR (prefix);
7222 prefix = XCDR (prefix);
7224 else
7225 def = AREF (item_properties, ITEM_PROPERTY_DEF);
7227 if (!update_menu_bindings)
7228 chkcache = 0;
7229 else if (NILP (XCAR (cachelist))) /* Have no saved key. */
7231 if (newcache /* Always check first time. */
7232 /* Should we check everything when precomputing key
7233 bindings? */
7234 /* If something had no key binding before, don't recheck it
7235 because that is too slow--except if we have a list of
7236 rebound commands in Vdefine_key_rebound_commands, do
7237 recheck any command that appears in that list. */
7238 || (CONSP (Vdefine_key_rebound_commands)
7239 && !NILP (Fmemq (def, Vdefine_key_rebound_commands))))
7240 chkcache = 1;
7242 /* We had a saved key. Is it still bound to the command? */
7243 else if (NILP (tem)
7244 || (!EQ (tem, def)
7245 /* If the command is an alias for another
7246 (such as lmenu.el set it up), check if the
7247 original command matches the cached command. */
7248 && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function))))
7249 chkcache = 1; /* Need to recompute key binding. */
7251 if (chkcache)
7253 /* Recompute equivalent key binding. If the command is an alias
7254 for another (such as lmenu.el set it up), see if the original
7255 command name has equivalent keys. Otherwise look up the
7256 specified command itself. We don't try both, because that
7257 makes lmenu menus slow. */
7258 if (SYMBOLP (def)
7259 && SYMBOLP (XSYMBOL (def)->function)
7260 && ! NILP (Fget (def, Qmenu_alias)))
7261 def = XSYMBOL (def)->function;
7262 tem = Fwhere_is_internal (def, Qnil, Qt, Qnil, Qt);
7263 XSETCAR (cachelist, tem);
7264 if (NILP (tem))
7266 XSETCDR (cachelist, Qnil);
7267 chkcache = 0;
7270 else if (!NILP (keyhint) && !NILP (XCAR (cachelist)))
7272 tem = XCAR (cachelist);
7273 chkcache = 1;
7276 newcache = chkcache;
7277 if (chkcache)
7279 tem = Fkey_description (tem);
7280 if (CONSP (prefix))
7282 if (STRINGP (XCAR (prefix)))
7283 tem = concat2 (XCAR (prefix), tem);
7284 if (STRINGP (XCDR (prefix)))
7285 tem = concat2 (tem, XCDR (prefix));
7287 XSETCDR (cachelist, tem);
7291 tem = XCDR (cachelist);
7292 if (newcache && !NILP (tem))
7294 tem = concat3 (build_string (" ("), tem, build_string (")"));
7295 XSETCDR (cachelist, tem);
7298 /* If we only want to precompute equivalent key bindings, stop here. */
7299 if (notreal)
7300 return 1;
7302 /* If we have an equivalent key binding, use that. */
7303 AREF (item_properties, ITEM_PROPERTY_KEYEQ) = tem;
7305 /* Include this when menu help is implemented.
7306 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP];
7307 if (!(NILP (tem) || STRINGP (tem)))
7309 tem = menu_item_eval_property (tem);
7310 if (!STRINGP (tem))
7311 tem = Qnil;
7312 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP] = tem;
7316 /* Handle radio buttons or toggle boxes. */
7317 tem = AREF (item_properties, ITEM_PROPERTY_SELECTED);
7318 if (!NILP (tem))
7319 AREF (item_properties, ITEM_PROPERTY_SELECTED)
7320 = menu_item_eval_property (tem);
7322 return 1;
7327 /***********************************************************************
7328 Tool-bars
7329 ***********************************************************************/
7331 /* A vector holding tool bar items while they are parsed in function
7332 tool_bar_items. Each item occupies TOOL_BAR_ITEM_NSCLOTS elements
7333 in the vector. */
7335 static Lisp_Object tool_bar_items_vector;
7337 /* A vector holding the result of parse_tool_bar_item. Layout is like
7338 the one for a single item in tool_bar_items_vector. */
7340 static Lisp_Object tool_bar_item_properties;
7342 /* Next free index in tool_bar_items_vector. */
7344 static int ntool_bar_items;
7346 /* The symbols `tool-bar', and `:image'. */
7348 extern Lisp_Object Qtool_bar;
7349 Lisp_Object QCimage;
7351 /* Function prototypes. */
7353 static void init_tool_bar_items P_ ((Lisp_Object));
7354 static void process_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
7355 static int parse_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
7356 static void append_tool_bar_item P_ ((void));
7359 /* Return a vector of tool bar items for keymaps currently in effect.
7360 Reuse vector REUSE if non-nil. Return in *NITEMS the number of
7361 tool bar items found. */
7363 Lisp_Object
7364 tool_bar_items (reuse, nitems)
7365 Lisp_Object reuse;
7366 int *nitems;
7368 Lisp_Object *maps;
7369 int nmaps, i;
7370 Lisp_Object oquit;
7371 Lisp_Object *tmaps;
7373 *nitems = 0;
7375 /* In order to build the menus, we need to call the keymap
7376 accessors. They all call QUIT. But this function is called
7377 during redisplay, during which a quit is fatal. So inhibit
7378 quitting while building the menus. We do this instead of
7379 specbind because (1) errors will clear it anyway and (2) this
7380 avoids risk of specpdl overflow. */
7381 oquit = Vinhibit_quit;
7382 Vinhibit_quit = Qt;
7384 /* Initialize tool_bar_items_vector and protect it from GC. */
7385 init_tool_bar_items (reuse);
7387 /* Build list of keymaps in maps. Set nmaps to the number of maps
7388 to process. */
7390 /* Should overriding-terminal-local-map and overriding-local-map apply? */
7391 if (!NILP (Voverriding_local_map_menu_flag))
7393 /* Yes, use them (if non-nil) as well as the global map. */
7394 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
7395 nmaps = 0;
7396 if (!NILP (current_kboard->Voverriding_terminal_local_map))
7397 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
7398 if (!NILP (Voverriding_local_map))
7399 maps[nmaps++] = Voverriding_local_map;
7401 else
7403 /* No, so use major and minor mode keymaps and keymap property.
7404 Note that tool-bar bindings in the local-map and keymap
7405 properties may not work reliable, as they are only
7406 recognized when the tool-bar (or mode-line) is updated,
7407 which does not normally happen after every command. */
7408 Lisp_Object tem;
7409 int nminor;
7410 nminor = current_minor_maps (NULL, &tmaps);
7411 maps = (Lisp_Object *) alloca ((nminor + 3) * sizeof (maps[0]));
7412 nmaps = 0;
7413 if (tem = get_local_map (PT, current_buffer, Qkeymap), !NILP (tem))
7414 maps[nmaps++] = tem;
7415 bcopy (tmaps, (void *) (maps + nmaps), nminor * sizeof (maps[0]));
7416 nmaps += nminor;
7417 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
7420 /* Add global keymap at the end. */
7421 maps[nmaps++] = current_global_map;
7423 /* Process maps in reverse order and look up in each map the prefix
7424 key `tool-bar'. */
7425 for (i = nmaps - 1; i >= 0; --i)
7426 if (!NILP (maps[i]))
7428 Lisp_Object keymap;
7430 keymap = get_keymap (access_keymap (maps[i], Qtool_bar, 1, 0, 1), 0, 1);
7431 if (CONSP (keymap))
7433 Lisp_Object tail;
7435 /* KEYMAP is a list `(keymap (KEY . BINDING) ...)'. */
7436 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
7438 Lisp_Object keydef = XCAR (tail);
7439 if (CONSP (keydef))
7440 process_tool_bar_item (XCAR (keydef), XCDR (keydef));
7445 Vinhibit_quit = oquit;
7446 *nitems = ntool_bar_items / TOOL_BAR_ITEM_NSLOTS;
7447 return tool_bar_items_vector;
7451 /* Process the definition of KEY which is DEF. */
7453 static void
7454 process_tool_bar_item (key, def)
7455 Lisp_Object key, def;
7457 int i;
7458 extern Lisp_Object Qundefined;
7459 struct gcpro gcpro1, gcpro2;
7461 /* Protect KEY and DEF from GC because parse_tool_bar_item may call
7462 eval. */
7463 GCPRO2 (key, def);
7465 if (EQ (def, Qundefined))
7467 /* If a map has an explicit `undefined' as definition,
7468 discard any previously made item. */
7469 for (i = 0; i < ntool_bar_items; i += TOOL_BAR_ITEM_NSLOTS)
7471 Lisp_Object *v = XVECTOR (tool_bar_items_vector)->contents + i;
7473 if (EQ (key, v[TOOL_BAR_ITEM_KEY]))
7475 if (ntool_bar_items > i + TOOL_BAR_ITEM_NSLOTS)
7476 bcopy (v + TOOL_BAR_ITEM_NSLOTS, v,
7477 ((ntool_bar_items - i - TOOL_BAR_ITEM_NSLOTS)
7478 * sizeof (Lisp_Object)));
7479 ntool_bar_items -= TOOL_BAR_ITEM_NSLOTS;
7480 break;
7484 else if (parse_tool_bar_item (key, def))
7485 /* Append a new tool bar item to tool_bar_items_vector. Accept
7486 more than one definition for the same key. */
7487 append_tool_bar_item ();
7489 UNGCPRO;
7493 /* Parse a tool bar item specification ITEM for key KEY and return the
7494 result in tool_bar_item_properties. Value is zero if ITEM is
7495 invalid.
7497 ITEM is a list `(menu-item CAPTION BINDING PROPS...)'.
7499 CAPTION is the caption of the item, If it's not a string, it is
7500 evaluated to get a string.
7502 BINDING is the tool bar item's binding. Tool-bar items with keymaps
7503 as binding are currently ignored.
7505 The following properties are recognized:
7507 - `:enable FORM'.
7509 FORM is evaluated and specifies whether the tool bar item is
7510 enabled or disabled.
7512 - `:visible FORM'
7514 FORM is evaluated and specifies whether the tool bar item is visible.
7516 - `:filter FUNCTION'
7518 FUNCTION is invoked with one parameter `(quote BINDING)'. Its
7519 result is stored as the new binding.
7521 - `:button (TYPE SELECTED)'
7523 TYPE must be one of `:radio' or `:toggle'. SELECTED is evaluated
7524 and specifies whether the button is selected (pressed) or not.
7526 - `:image IMAGES'
7528 IMAGES is either a single image specification or a vector of four
7529 image specifications. See enum tool_bar_item_images.
7531 - `:help HELP-STRING'.
7533 Gives a help string to display for the tool bar item. */
7535 static int
7536 parse_tool_bar_item (key, item)
7537 Lisp_Object key, item;
7539 /* Access slot with index IDX of vector tool_bar_item_properties. */
7540 #define PROP(IDX) XVECTOR (tool_bar_item_properties)->contents[IDX]
7542 Lisp_Object filter = Qnil;
7543 Lisp_Object caption;
7544 int i;
7546 /* Defininition looks like `(menu-item CAPTION BINDING PROPS...)'.
7547 Rule out items that aren't lists, don't start with
7548 `menu-item' or whose rest following `tool-bar-item' is not a
7549 list. */
7550 if (!CONSP (item)
7551 || !EQ (XCAR (item), Qmenu_item)
7552 || (item = XCDR (item),
7553 !CONSP (item)))
7554 return 0;
7556 /* Create tool_bar_item_properties vector if necessary. Reset it to
7557 defaults. */
7558 if (VECTORP (tool_bar_item_properties))
7560 for (i = 0; i < TOOL_BAR_ITEM_NSLOTS; ++i)
7561 PROP (i) = Qnil;
7563 else
7564 tool_bar_item_properties
7565 = Fmake_vector (make_number (TOOL_BAR_ITEM_NSLOTS), Qnil);
7567 /* Set defaults. */
7568 PROP (TOOL_BAR_ITEM_KEY) = key;
7569 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
7571 /* Get the caption of the item. If the caption is not a string,
7572 evaluate it to get a string. If we don't get a string, skip this
7573 item. */
7574 caption = XCAR (item);
7575 if (!STRINGP (caption))
7577 caption = menu_item_eval_property (caption);
7578 if (!STRINGP (caption))
7579 return 0;
7581 PROP (TOOL_BAR_ITEM_CAPTION) = caption;
7583 /* Give up if rest following the caption is not a list. */
7584 item = XCDR (item);
7585 if (!CONSP (item))
7586 return 0;
7588 /* Store the binding. */
7589 PROP (TOOL_BAR_ITEM_BINDING) = XCAR (item);
7590 item = XCDR (item);
7592 /* Ignore cached key binding, if any. */
7593 if (CONSP (item) && CONSP (XCAR (item)))
7594 item = XCDR (item);
7596 /* Process the rest of the properties. */
7597 for (; CONSP (item) && CONSP (XCDR (item)); item = XCDR (XCDR (item)))
7599 Lisp_Object key, value;
7601 key = XCAR (item);
7602 value = XCAR (XCDR (item));
7604 if (EQ (key, QCenable))
7605 /* `:enable FORM'. */
7606 PROP (TOOL_BAR_ITEM_ENABLED_P) = value;
7607 else if (EQ (key, QCvisible))
7609 /* `:visible FORM'. If got a visible property and that
7610 evaluates to nil then ignore this item. */
7611 if (NILP (menu_item_eval_property (value)))
7612 return 0;
7614 else if (EQ (key, QChelp))
7615 /* `:help HELP-STRING'. */
7616 PROP (TOOL_BAR_ITEM_HELP) = value;
7617 else if (EQ (key, QCfilter))
7618 /* ':filter FORM'. */
7619 filter = value;
7620 else if (EQ (key, QCbutton) && CONSP (value))
7622 /* `:button (TYPE . SELECTED)'. */
7623 Lisp_Object type, selected;
7625 type = XCAR (value);
7626 selected = XCDR (value);
7627 if (EQ (type, QCtoggle) || EQ (type, QCradio))
7629 PROP (TOOL_BAR_ITEM_SELECTED_P) = selected;
7630 PROP (TOOL_BAR_ITEM_TYPE) = type;
7633 else if (EQ (key, QCimage)
7634 && (CONSP (value)
7635 || (VECTORP (value) && XVECTOR (value)->size == 4)))
7636 /* Value is either a single image specification or a vector
7637 of 4 such specifications for the different button states. */
7638 PROP (TOOL_BAR_ITEM_IMAGES) = value;
7641 /* If got a filter apply it on binding. */
7642 if (!NILP (filter))
7643 PROP (TOOL_BAR_ITEM_BINDING)
7644 = menu_item_eval_property (list2 (filter,
7645 list2 (Qquote,
7646 PROP (TOOL_BAR_ITEM_BINDING))));
7648 /* See if the binding is a keymap. Give up if it is. */
7649 if (CONSP (get_keymap (PROP (TOOL_BAR_ITEM_BINDING), 0, 1)))
7650 return 0;
7652 /* Enable or disable selection of item. */
7653 if (!EQ (PROP (TOOL_BAR_ITEM_ENABLED_P), Qt))
7654 PROP (TOOL_BAR_ITEM_ENABLED_P)
7655 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_ENABLED_P));
7657 /* Handle radio buttons or toggle boxes. */
7658 if (!NILP (PROP (TOOL_BAR_ITEM_SELECTED_P)))
7659 PROP (TOOL_BAR_ITEM_SELECTED_P)
7660 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_SELECTED_P));
7662 return 1;
7664 #undef PROP
7668 /* Initialize tool_bar_items_vector. REUSE, if non-nil, is a vector
7669 that can be reused. */
7671 static void
7672 init_tool_bar_items (reuse)
7673 Lisp_Object reuse;
7675 if (VECTORP (reuse))
7676 tool_bar_items_vector = reuse;
7677 else
7678 tool_bar_items_vector = Fmake_vector (make_number (64), Qnil);
7679 ntool_bar_items = 0;
7683 /* Append parsed tool bar item properties from
7684 tool_bar_item_properties */
7686 static void
7687 append_tool_bar_item ()
7689 Lisp_Object *to, *from;
7691 /* Enlarge tool_bar_items_vector if necessary. */
7692 if (ntool_bar_items + TOOL_BAR_ITEM_NSLOTS
7693 >= XVECTOR (tool_bar_items_vector)->size)
7695 Lisp_Object new_vector;
7696 int old_size = XVECTOR (tool_bar_items_vector)->size;
7698 new_vector = Fmake_vector (make_number (2 * old_size), Qnil);
7699 bcopy (XVECTOR (tool_bar_items_vector)->contents,
7700 XVECTOR (new_vector)->contents,
7701 old_size * sizeof (Lisp_Object));
7702 tool_bar_items_vector = new_vector;
7705 /* Append entries from tool_bar_item_properties to the end of
7706 tool_bar_items_vector. */
7707 to = XVECTOR (tool_bar_items_vector)->contents + ntool_bar_items;
7708 from = XVECTOR (tool_bar_item_properties)->contents;
7709 bcopy (from, to, TOOL_BAR_ITEM_NSLOTS * sizeof *to);
7710 ntool_bar_items += TOOL_BAR_ITEM_NSLOTS;
7717 /* Read a character using menus based on maps in the array MAPS.
7718 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
7719 Return t if we displayed a menu but the user rejected it.
7721 PREV_EVENT is the previous input event, or nil if we are reading
7722 the first event of a key sequence.
7724 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
7725 if we used a mouse menu to read the input, or zero otherwise. If
7726 USED_MOUSE_MENU is null, we don't dereference it.
7728 The prompting is done based on the prompt-string of the map
7729 and the strings associated with various map elements.
7731 This can be done with X menus or with menus put in the minibuf.
7732 These are done in different ways, depending on how the input will be read.
7733 Menus using X are done after auto-saving in read-char, getting the input
7734 event from Fx_popup_menu; menus using the minibuf use read_char recursively
7735 and do auto-saving in the inner call of read_char. */
7737 static Lisp_Object
7738 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
7739 int nmaps;
7740 Lisp_Object *maps;
7741 Lisp_Object prev_event;
7742 int *used_mouse_menu;
7744 int mapno;
7745 register Lisp_Object name = Qnil;
7747 if (used_mouse_menu)
7748 *used_mouse_menu = 0;
7750 /* Use local over global Menu maps */
7752 if (! menu_prompting)
7753 return Qnil;
7755 /* Optionally disregard all but the global map. */
7756 if (inhibit_local_menu_bar_menus)
7758 maps += (nmaps - 1);
7759 nmaps = 1;
7762 /* Get the menu name from the first map that has one (a prompt string). */
7763 for (mapno = 0; mapno < nmaps; mapno++)
7765 name = Fkeymap_prompt (maps[mapno]);
7766 if (!NILP (name))
7767 break;
7770 /* If we don't have any menus, just read a character normally. */
7771 if (!STRINGP (name))
7772 return Qnil;
7774 #ifdef HAVE_MENUS
7775 /* If we got to this point via a mouse click,
7776 use a real menu for mouse selection. */
7777 if (EVENT_HAS_PARAMETERS (prev_event)
7778 && !EQ (XCAR (prev_event), Qmenu_bar)
7779 && !EQ (XCAR (prev_event), Qtool_bar))
7781 /* Display the menu and get the selection. */
7782 Lisp_Object *realmaps
7783 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
7784 Lisp_Object value;
7785 int nmaps1 = 0;
7787 /* Use the maps that are not nil. */
7788 for (mapno = 0; mapno < nmaps; mapno++)
7789 if (!NILP (maps[mapno]))
7790 realmaps[nmaps1++] = maps[mapno];
7792 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
7793 if (CONSP (value))
7795 Lisp_Object tem;
7797 record_menu_key (XCAR (value));
7799 /* If we got multiple events, unread all but
7800 the first.
7801 There is no way to prevent those unread events
7802 from showing up later in last_nonmenu_event.
7803 So turn symbol and integer events into lists,
7804 to indicate that they came from a mouse menu,
7805 so that when present in last_nonmenu_event
7806 they won't confuse things. */
7807 for (tem = XCDR (value); !NILP (tem); tem = XCDR (tem))
7809 record_menu_key (XCAR (tem));
7810 if (SYMBOLP (XCAR (tem))
7811 || INTEGERP (XCAR (tem)))
7812 XSETCAR (tem, Fcons (XCAR (tem), Qdisabled));
7815 /* If we got more than one event, put all but the first
7816 onto this list to be read later.
7817 Return just the first event now. */
7818 Vunread_command_events
7819 = nconc2 (XCDR (value), Vunread_command_events);
7820 value = XCAR (value);
7822 else if (NILP (value))
7823 value = Qt;
7824 if (used_mouse_menu)
7825 *used_mouse_menu = 1;
7826 return value;
7828 #endif /* HAVE_MENUS */
7829 return Qnil ;
7832 /* Buffer in use so far for the minibuf prompts for menu keymaps.
7833 We make this bigger when necessary, and never free it. */
7834 static char *read_char_minibuf_menu_text;
7835 /* Size of that buffer. */
7836 static int read_char_minibuf_menu_width;
7838 static Lisp_Object
7839 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
7840 int commandflag ;
7841 int nmaps;
7842 Lisp_Object *maps;
7844 int mapno;
7845 register Lisp_Object name;
7846 int nlength;
7847 /* FIXME: Use the minibuffer's frame width. */
7848 int width = FRAME_WIDTH (SELECTED_FRAME ()) - 4;
7849 int idx = -1;
7850 int nobindings = 1;
7851 Lisp_Object rest, vector;
7852 char *menu;
7854 vector = Qnil;
7855 name = Qnil;
7857 if (! menu_prompting)
7858 return Qnil;
7860 /* Make sure we have a big enough buffer for the menu text. */
7861 if (read_char_minibuf_menu_text == 0)
7863 read_char_minibuf_menu_width = width + 4;
7864 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
7866 else if (width + 4 > read_char_minibuf_menu_width)
7868 read_char_minibuf_menu_width = width + 4;
7869 read_char_minibuf_menu_text
7870 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
7872 menu = read_char_minibuf_menu_text;
7874 /* Get the menu name from the first map that has one (a prompt string). */
7875 for (mapno = 0; mapno < nmaps; mapno++)
7877 name = Fkeymap_prompt (maps[mapno]);
7878 if (!NILP (name))
7879 break;
7882 /* If we don't have any menus, just read a character normally. */
7883 if (!STRINGP (name))
7884 return Qnil;
7886 /* Prompt string always starts with map's prompt, and a space. */
7887 strcpy (menu, SDATA (name));
7888 nlength = SBYTES (name);
7889 menu[nlength++] = ':';
7890 menu[nlength++] = ' ';
7891 menu[nlength] = 0;
7893 /* Start prompting at start of first map. */
7894 mapno = 0;
7895 rest = maps[mapno];
7897 /* Present the documented bindings, a line at a time. */
7898 while (1)
7900 int notfirst = 0;
7901 int i = nlength;
7902 Lisp_Object obj;
7903 int ch;
7904 Lisp_Object orig_defn_macro;
7906 /* Loop over elements of map. */
7907 while (i < width)
7909 Lisp_Object elt;
7911 /* If reached end of map, start at beginning of next map. */
7912 if (NILP (rest))
7914 mapno++;
7915 /* At end of last map, wrap around to first map if just starting,
7916 or end this line if already have something on it. */
7917 if (mapno == nmaps)
7919 mapno = 0;
7920 if (notfirst || nobindings) break;
7922 rest = maps[mapno];
7925 /* Look at the next element of the map. */
7926 if (idx >= 0)
7927 elt = XVECTOR (vector)->contents[idx];
7928 else
7929 elt = Fcar_safe (rest);
7931 if (idx < 0 && VECTORP (elt))
7933 /* If we found a dense table in the keymap,
7934 advanced past it, but start scanning its contents. */
7935 rest = Fcdr_safe (rest);
7936 vector = elt;
7937 idx = 0;
7939 else
7941 /* An ordinary element. */
7942 Lisp_Object event, tem;
7944 if (idx < 0)
7946 event = Fcar_safe (elt); /* alist */
7947 elt = Fcdr_safe (elt);
7949 else
7951 XSETINT (event, idx); /* vector */
7954 /* Ignore the element if it has no prompt string. */
7955 if (INTEGERP (event) && parse_menu_item (elt, 0, -1))
7957 /* 1 if the char to type matches the string. */
7958 int char_matches;
7959 Lisp_Object upcased_event, downcased_event;
7960 Lisp_Object desc = Qnil;
7961 Lisp_Object s
7962 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
7964 upcased_event = Fupcase (event);
7965 downcased_event = Fdowncase (event);
7966 char_matches = (XINT (upcased_event) == SREF (s, 0)
7967 || XINT (downcased_event) == SREF (s, 0));
7968 if (! char_matches)
7969 desc = Fsingle_key_description (event, Qnil);
7971 #if 0 /* It is redundant to list the equivalent key bindings because
7972 the prefix is what the user has already typed. */
7974 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
7975 if (!NILP (tem))
7976 /* Insert equivalent keybinding. */
7977 s = concat2 (s, tem);
7978 #endif
7980 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
7981 if (EQ (tem, QCradio) || EQ (tem, QCtoggle))
7983 /* Insert button prefix. */
7984 Lisp_Object selected
7985 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
7986 if (EQ (tem, QCradio))
7987 tem = build_string (NILP (selected) ? "(*) " : "( ) ");
7988 else
7989 tem = build_string (NILP (selected) ? "[X] " : "[ ] ");
7990 s = concat2 (tem, s);
7994 /* If we have room for the prompt string, add it to this line.
7995 If this is the first on the line, always add it. */
7996 if ((SCHARS (s) + i + 2
7997 + (char_matches ? 0 : SCHARS (desc) + 3))
7998 < width
7999 || !notfirst)
8001 int thiswidth;
8003 /* Punctuate between strings. */
8004 if (notfirst)
8006 strcpy (menu + i, ", ");
8007 i += 2;
8009 notfirst = 1;
8010 nobindings = 0 ;
8012 /* If the char to type doesn't match the string's
8013 first char, explicitly show what char to type. */
8014 if (! char_matches)
8016 /* Add as much of string as fits. */
8017 thiswidth = SCHARS (desc);
8018 if (thiswidth + i > width)
8019 thiswidth = width - i;
8020 bcopy (SDATA (desc), menu + i, thiswidth);
8021 i += thiswidth;
8022 strcpy (menu + i, " = ");
8023 i += 3;
8026 /* Add as much of string as fits. */
8027 thiswidth = SCHARS (s);
8028 if (thiswidth + i > width)
8029 thiswidth = width - i;
8030 bcopy (SDATA (s), menu + i, thiswidth);
8031 i += thiswidth;
8032 menu[i] = 0;
8034 else
8036 /* If this element does not fit, end the line now,
8037 and save the element for the next line. */
8038 strcpy (menu + i, "...");
8039 break;
8043 /* Move past this element. */
8044 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
8045 /* Handle reaching end of dense table. */
8046 idx = -1;
8047 if (idx >= 0)
8048 idx++;
8049 else
8050 rest = Fcdr_safe (rest);
8054 /* Prompt with that and read response. */
8055 message2_nolog (menu, strlen (menu),
8056 ! NILP (current_buffer->enable_multibyte_characters));
8058 /* Make believe its not a keyboard macro in case the help char
8059 is pressed. Help characters are not recorded because menu prompting
8060 is not used on replay.
8062 orig_defn_macro = current_kboard->defining_kbd_macro;
8063 current_kboard->defining_kbd_macro = Qnil;
8065 obj = read_char (commandflag, 0, 0, Qt, 0);
8066 while (BUFFERP (obj));
8067 current_kboard->defining_kbd_macro = orig_defn_macro;
8069 if (!INTEGERP (obj))
8070 return obj;
8071 else
8072 ch = XINT (obj);
8074 if (! EQ (obj, menu_prompt_more_char)
8075 && (!INTEGERP (menu_prompt_more_char)
8076 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
8078 if (!NILP (current_kboard->defining_kbd_macro))
8079 store_kbd_macro_char (obj);
8080 return obj;
8082 /* Help char - go round again */
8086 /* Reading key sequences. */
8088 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
8089 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
8090 keymap, or nil otherwise. Return the index of the first keymap in
8091 which KEY has any binding, or NMAPS if no map has a binding.
8093 If KEY is a meta ASCII character, treat it like meta-prefix-char
8094 followed by the corresponding non-meta character. Keymaps in
8095 CURRENT with non-prefix bindings for meta-prefix-char become nil in
8096 NEXT.
8098 If KEY has no bindings in any of the CURRENT maps, NEXT is left
8099 unmodified.
8101 NEXT may be the same array as CURRENT. */
8103 static int
8104 follow_key (key, nmaps, current, defs, next)
8105 Lisp_Object key;
8106 Lisp_Object *current, *defs, *next;
8107 int nmaps;
8109 int i, first_binding;
8110 int did_meta = 0;
8112 first_binding = nmaps;
8113 for (i = nmaps - 1; i >= 0; i--)
8115 if (! NILP (current[i]))
8117 Lisp_Object map;
8118 if (did_meta)
8119 map = defs[i];
8120 else
8121 map = current[i];
8123 defs[i] = access_keymap (map, key, 1, 0, 1);
8124 if (! NILP (defs[i]))
8125 first_binding = i;
8127 else
8128 defs[i] = Qnil;
8131 /* Given the set of bindings we've found, produce the next set of maps. */
8132 if (first_binding < nmaps)
8133 for (i = 0; i < nmaps; i++)
8134 next[i] = NILP (defs[i]) ? Qnil : get_keymap (defs[i], 0, 1);
8136 return first_binding;
8139 /* Structure used to keep track of partial application of key remapping
8140 such as Vfunction_key_map and Vkey_translation_map. */
8141 typedef struct keyremap
8143 Lisp_Object map;
8144 int start, end;
8145 } keyremap;
8148 /* Read a sequence of keys that ends with a non prefix character,
8149 storing it in KEYBUF, a buffer of size BUFSIZE.
8150 Prompt with PROMPT.
8151 Return the length of the key sequence stored.
8152 Return -1 if the user rejected a command menu.
8154 Echo starting immediately unless `prompt' is 0.
8156 Where a key sequence ends depends on the currently active keymaps.
8157 These include any minor mode keymaps active in the current buffer,
8158 the current buffer's local map, and the global map.
8160 If a key sequence has no other bindings, we check Vfunction_key_map
8161 to see if some trailing subsequence might be the beginning of a
8162 function key's sequence. If so, we try to read the whole function
8163 key, and substitute its symbolic name into the key sequence.
8165 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
8166 `double-' events into similar click events, if that would make them
8167 bound. We try to turn `triple-' events first into `double-' events,
8168 then into clicks.
8170 If we get a mouse click in a mode line, vertical divider, or other
8171 non-text area, we treat the click as if it were prefixed by the
8172 symbol denoting that area - `mode-line', `vertical-line', or
8173 whatever.
8175 If the sequence starts with a mouse click, we read the key sequence
8176 with respect to the buffer clicked on, not the current buffer.
8178 If the user switches frames in the midst of a key sequence, we put
8179 off the switch-frame event until later; the next call to
8180 read_char will return it.
8182 If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
8183 from the selected window's buffer. */
8185 static int
8186 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
8187 can_return_switch_frame, fix_current_buffer)
8188 Lisp_Object *keybuf;
8189 int bufsize;
8190 Lisp_Object prompt;
8191 int dont_downcase_last;
8192 int can_return_switch_frame;
8193 int fix_current_buffer;
8195 volatile Lisp_Object from_string;
8196 volatile int count = SPECPDL_INDEX ();
8198 /* How many keys there are in the current key sequence. */
8199 volatile int t;
8201 /* The length of the echo buffer when we started reading, and
8202 the length of this_command_keys when we started reading. */
8203 volatile int echo_start;
8204 volatile int keys_start;
8206 /* The number of keymaps we're scanning right now, and the number of
8207 keymaps we have allocated space for. */
8208 volatile int nmaps;
8209 volatile int nmaps_allocated = 0;
8211 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
8212 the current keymaps. */
8213 Lisp_Object *volatile defs = NULL;
8215 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
8216 in the current keymaps, or nil where it is not a prefix. */
8217 Lisp_Object *volatile submaps = NULL;
8219 /* The local map to start out with at start of key sequence. */
8220 volatile Lisp_Object orig_local_map;
8222 /* The map from the `keymap' property to start out with at start of
8223 key sequence. */
8224 volatile Lisp_Object orig_keymap;
8226 /* 1 if we have already considered switching to the local-map property
8227 of the place where a mouse click occurred. */
8228 volatile int localized_local_map = 0;
8230 /* The index in defs[] of the first keymap that has a binding for
8231 this key sequence. In other words, the lowest i such that
8232 defs[i] is non-nil. */
8233 volatile int first_binding;
8234 /* Index of the first key that has no binding.
8235 It is useless to try fkey.start larger than that. */
8236 volatile int first_unbound;
8238 /* If t < mock_input, then KEYBUF[t] should be read as the next
8239 input key.
8241 We use this to recover after recognizing a function key. Once we
8242 realize that a suffix of the current key sequence is actually a
8243 function key's escape sequence, we replace the suffix with the
8244 function key's binding from Vfunction_key_map. Now keybuf
8245 contains a new and different key sequence, so the echo area,
8246 this_command_keys, and the submaps and defs arrays are wrong. In
8247 this situation, we set mock_input to t, set t to 0, and jump to
8248 restart_sequence; the loop will read keys from keybuf up until
8249 mock_input, thus rebuilding the state; and then it will resume
8250 reading characters from the keyboard. */
8251 volatile int mock_input = 0;
8253 /* If the sequence is unbound in submaps[], then
8254 keybuf[fkey.start..fkey.end-1] is a prefix in Vfunction_key_map,
8255 and fkey.map is its binding.
8257 These might be > t, indicating that all function key scanning
8258 should hold off until t reaches them. We do this when we've just
8259 recognized a function key, to avoid searching for the function
8260 key's again in Vfunction_key_map. */
8261 volatile keyremap fkey;
8263 /* Likewise, for key_translation_map. */
8264 volatile keyremap keytran;
8266 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
8267 we put it off for later. While we're reading, we keep the event here. */
8268 volatile Lisp_Object delayed_switch_frame;
8270 /* See the comment below... */
8271 #if defined (GOBBLE_FIRST_EVENT)
8272 Lisp_Object first_event;
8273 #endif
8275 volatile Lisp_Object original_uppercase;
8276 volatile int original_uppercase_position = -1;
8278 /* Gets around Microsoft compiler limitations. */
8279 int dummyflag = 0;
8281 struct buffer *starting_buffer;
8283 /* List of events for which a fake prefix key has been generated. */
8284 volatile Lisp_Object fake_prefixed_keys = Qnil;
8286 #if defined (GOBBLE_FIRST_EVENT)
8287 int junk;
8288 #endif
8290 struct gcpro gcpro1;
8292 GCPRO1 (fake_prefixed_keys);
8293 raw_keybuf_count = 0;
8295 last_nonmenu_event = Qnil;
8297 delayed_switch_frame = Qnil;
8298 fkey.map = Vfunction_key_map;
8299 keytran.map = Vkey_translation_map;
8300 /* If there is no translation-map, turn off scanning. */
8301 fkey.start = fkey.end = KEYMAPP (fkey.map) ? 0 : bufsize + 1;
8302 keytran.start = keytran.end = KEYMAPP (keytran.map) ? 0 : bufsize + 1;
8304 if (INTERACTIVE)
8306 if (!NILP (prompt))
8307 echo_prompt (prompt);
8308 else if (cursor_in_echo_area
8309 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
8310 && NILP (Fzerop (Vecho_keystrokes)))
8311 /* This doesn't put in a dash if the echo buffer is empty, so
8312 you don't always see a dash hanging out in the minibuffer. */
8313 echo_dash ();
8316 /* Record the initial state of the echo area and this_command_keys;
8317 we will need to restore them if we replay a key sequence. */
8318 if (INTERACTIVE)
8319 echo_start = echo_length ();
8320 keys_start = this_command_key_count;
8321 this_single_command_key_start = keys_start;
8323 #if defined (GOBBLE_FIRST_EVENT)
8324 /* This doesn't quite work, because some of the things that read_char
8325 does cannot safely be bypassed. It seems too risky to try to make
8326 this work right. */
8328 /* Read the first char of the sequence specially, before setting
8329 up any keymaps, in case a filter runs and switches buffers on us. */
8330 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
8331 &junk);
8332 #endif /* GOBBLE_FIRST_EVENT */
8334 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8335 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8336 from_string = Qnil;
8338 /* We jump here when the key sequence has been thoroughly changed, and
8339 we need to rescan it starting from the beginning. When we jump here,
8340 keybuf[0..mock_input] holds the sequence we should reread. */
8341 replay_sequence:
8343 starting_buffer = current_buffer;
8344 first_unbound = bufsize + 1;
8346 /* Build our list of keymaps.
8347 If we recognize a function key and replace its escape sequence in
8348 keybuf with its symbol, or if the sequence starts with a mouse
8349 click and we need to switch buffers, we jump back here to rebuild
8350 the initial keymaps from the current buffer. */
8351 nmaps = 0;
8353 if (!NILP (current_kboard->Voverriding_terminal_local_map)
8354 || !NILP (Voverriding_local_map))
8356 if (3 > nmaps_allocated)
8358 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
8359 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
8360 nmaps_allocated = 3;
8362 if (!NILP (current_kboard->Voverriding_terminal_local_map))
8363 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
8364 if (!NILP (Voverriding_local_map))
8365 submaps[nmaps++] = Voverriding_local_map;
8367 else
8369 int nminor;
8370 int total;
8371 Lisp_Object *maps;
8373 nminor = current_minor_maps (0, &maps);
8374 total = nminor + (!NILP (orig_keymap) ? 3 : 2);
8376 if (total > nmaps_allocated)
8378 submaps = (Lisp_Object *) alloca (total * sizeof (submaps[0]));
8379 defs = (Lisp_Object *) alloca (total * sizeof (defs[0]));
8380 nmaps_allocated = total;
8383 if (!NILP (orig_keymap))
8384 submaps[nmaps++] = orig_keymap;
8386 bcopy (maps, (void *) (submaps + nmaps),
8387 nminor * sizeof (submaps[0]));
8389 nmaps += nminor;
8391 submaps[nmaps++] = orig_local_map;
8393 submaps[nmaps++] = current_global_map;
8395 /* Find an accurate initial value for first_binding. */
8396 for (first_binding = 0; first_binding < nmaps; first_binding++)
8397 if (! NILP (submaps[first_binding]))
8398 break;
8400 /* Start from the beginning in keybuf. */
8401 t = 0;
8403 /* These are no-ops the first time through, but if we restart, they
8404 revert the echo area and this_command_keys to their original state. */
8405 this_command_key_count = keys_start;
8406 if (INTERACTIVE && t < mock_input)
8407 echo_truncate (echo_start);
8409 /* If the best binding for the current key sequence is a keymap, or
8410 we may be looking at a function key's escape sequence, keep on
8411 reading. */
8412 while (first_binding < nmaps
8413 /* Keep reading as long as there's a prefix binding. */
8414 ? !NILP (submaps[first_binding])
8415 /* Don't return in the middle of a possible function key sequence,
8416 if the only bindings we found were via case conversion.
8417 Thus, if ESC O a has a function-key-map translation
8418 and ESC o has a binding, don't return after ESC O,
8419 so that we can translate ESC O plus the next character. */
8420 : (fkey.start < t || keytran.start < t))
8422 Lisp_Object key;
8423 int used_mouse_menu = 0;
8425 /* Where the last real key started. If we need to throw away a
8426 key that has expanded into more than one element of keybuf
8427 (say, a mouse click on the mode line which is being treated
8428 as [mode-line (mouse-...)], then we backtrack to this point
8429 of keybuf. */
8430 volatile int last_real_key_start;
8432 /* These variables are analogous to echo_start and keys_start;
8433 while those allow us to restart the entire key sequence,
8434 echo_local_start and keys_local_start allow us to throw away
8435 just one key. */
8436 volatile int echo_local_start, keys_local_start, local_first_binding;
8438 /* key-translation-map is applied *after* function-key-map. */
8439 eassert (keytran.end <= fkey.start);
8441 if (first_unbound < fkey.start && first_unbound < keytran.start)
8442 { /* The prefix upto first_unbound has no binding and has
8443 no translation left to do either, so we know it's unbound.
8444 If we don't stop now, we risk staying here indefinitely
8445 (if the user keeps entering fkey or keytran prefixes
8446 like C-c ESC ESC ESC ESC ...) */
8447 int i;
8448 for (i = first_unbound + 1; i < t; i++)
8449 keybuf[i - first_unbound - 1] = keybuf[i];
8450 mock_input = t - first_unbound - 1;
8451 fkey.end = fkey.start -= first_unbound + 1;
8452 fkey.map = Vfunction_key_map;
8453 keytran.end = keytran.start -= first_unbound + 1;
8454 keytran.map = Vkey_translation_map;
8455 goto replay_sequence;
8458 if (t >= bufsize)
8459 error ("Key sequence too long");
8461 if (INTERACTIVE)
8462 echo_local_start = echo_length ();
8463 keys_local_start = this_command_key_count;
8464 local_first_binding = first_binding;
8466 replay_key:
8467 /* These are no-ops, unless we throw away a keystroke below and
8468 jumped back up to replay_key; in that case, these restore the
8469 variables to their original state, allowing us to replay the
8470 loop. */
8471 if (INTERACTIVE && t < mock_input)
8472 echo_truncate (echo_local_start);
8473 this_command_key_count = keys_local_start;
8474 first_binding = local_first_binding;
8476 /* By default, assume each event is "real". */
8477 last_real_key_start = t;
8479 /* Does mock_input indicate that we are re-reading a key sequence? */
8480 if (t < mock_input)
8482 key = keybuf[t];
8483 add_command_key (key);
8484 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
8485 && NILP (Fzerop (Vecho_keystrokes)))
8486 echo_char (key);
8489 /* If not, we should actually read a character. */
8490 else
8493 #ifdef MULTI_KBOARD
8494 KBOARD *interrupted_kboard = current_kboard;
8495 struct frame *interrupted_frame = SELECTED_FRAME ();
8496 if (setjmp (wrong_kboard_jmpbuf))
8498 if (!NILP (delayed_switch_frame))
8500 interrupted_kboard->kbd_queue
8501 = Fcons (delayed_switch_frame,
8502 interrupted_kboard->kbd_queue);
8503 delayed_switch_frame = Qnil;
8505 while (t > 0)
8506 interrupted_kboard->kbd_queue
8507 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
8509 /* If the side queue is non-empty, ensure it begins with a
8510 switch-frame, so we'll replay it in the right context. */
8511 if (CONSP (interrupted_kboard->kbd_queue)
8512 && (key = XCAR (interrupted_kboard->kbd_queue),
8513 !(EVENT_HAS_PARAMETERS (key)
8514 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
8515 Qswitch_frame))))
8517 Lisp_Object frame;
8518 XSETFRAME (frame, interrupted_frame);
8519 interrupted_kboard->kbd_queue
8520 = Fcons (make_lispy_switch_frame (frame),
8521 interrupted_kboard->kbd_queue);
8523 mock_input = 0;
8524 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8525 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8526 goto replay_sequence;
8528 #endif
8529 key = read_char (NILP (prompt), nmaps,
8530 (Lisp_Object *) submaps, last_nonmenu_event,
8531 &used_mouse_menu);
8534 /* read_char returns t when it shows a menu and the user rejects it.
8535 Just return -1. */
8536 if (EQ (key, Qt))
8538 unbind_to (count, Qnil);
8539 UNGCPRO;
8540 return -1;
8543 /* read_char returns -1 at the end of a macro.
8544 Emacs 18 handles this by returning immediately with a
8545 zero, so that's what we'll do. */
8546 if (INTEGERP (key) && XINT (key) == -1)
8548 t = 0;
8549 /* The Microsoft C compiler can't handle the goto that
8550 would go here. */
8551 dummyflag = 1;
8552 break;
8555 /* If the current buffer has been changed from under us, the
8556 keymap may have changed, so replay the sequence. */
8557 if (BUFFERP (key))
8559 EMACS_TIME initial_idleness_start_time;
8560 EMACS_SET_SECS_USECS (initial_idleness_start_time,
8561 EMACS_SECS (timer_last_idleness_start_time),
8562 EMACS_USECS (timer_last_idleness_start_time));
8564 /* Resume idle state, using the same start-time as before. */
8565 timer_start_idle ();
8566 timer_idleness_start_time = initial_idleness_start_time;
8568 mock_input = t;
8569 /* Reset the current buffer from the selected window
8570 in case something changed the former and not the latter.
8571 This is to be more consistent with the behavior
8572 of the command_loop_1. */
8573 if (fix_current_buffer)
8575 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8576 Fkill_emacs (Qnil);
8577 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
8578 Fset_buffer (XWINDOW (selected_window)->buffer);
8581 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8582 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8583 goto replay_sequence;
8586 /* If we have a quit that was typed in another frame, and
8587 quit_throw_to_read_char switched buffers,
8588 replay to get the right keymap. */
8589 if (INTEGERP (key)
8590 && XINT (key) == quit_char
8591 && current_buffer != starting_buffer)
8593 GROW_RAW_KEYBUF;
8594 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8595 keybuf[t++] = key;
8596 mock_input = t;
8597 Vquit_flag = Qnil;
8598 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8599 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8600 goto replay_sequence;
8603 Vquit_flag = Qnil;
8605 if (EVENT_HAS_PARAMETERS (key)
8606 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), Qswitch_frame))
8608 /* If we're at the beginning of a key sequence, and the caller
8609 says it's okay, go ahead and return this event. If we're
8610 in the midst of a key sequence, delay it until the end. */
8611 if (t > 0 || !can_return_switch_frame)
8613 delayed_switch_frame = key;
8614 goto replay_key;
8618 GROW_RAW_KEYBUF;
8619 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8622 /* Clicks in non-text areas get prefixed by the symbol
8623 in their CHAR-ADDRESS field. For example, a click on
8624 the mode line is prefixed by the symbol `mode-line'.
8626 Furthermore, key sequences beginning with mouse clicks
8627 are read using the keymaps of the buffer clicked on, not
8628 the current buffer. So we may have to switch the buffer
8629 here.
8631 When we turn one event into two events, we must make sure
8632 that neither of the two looks like the original--so that,
8633 if we replay the events, they won't be expanded again.
8634 If not for this, such reexpansion could happen either here
8635 or when user programs play with this-command-keys. */
8636 if (EVENT_HAS_PARAMETERS (key))
8638 Lisp_Object kind;
8640 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
8641 if (EQ (kind, Qmouse_click))
8643 Lisp_Object window, posn;
8645 window = POSN_WINDOW (EVENT_START (key));
8646 posn = POSN_BUFFER_POSN (EVENT_START (key));
8648 if (CONSP (posn)
8649 || (!NILP (fake_prefixed_keys)
8650 && !NILP (Fmemq (key, fake_prefixed_keys))))
8652 /* We're looking a second time at an event for which
8653 we generated a fake prefix key. Set
8654 last_real_key_start appropriately. */
8655 if (t > 0)
8656 last_real_key_start = t - 1;
8659 /* Key sequences beginning with mouse clicks are
8660 read using the keymaps in the buffer clicked on,
8661 not the current buffer. If we're at the
8662 beginning of a key sequence, switch buffers. */
8663 if (last_real_key_start == 0
8664 && WINDOWP (window)
8665 && BUFFERP (XWINDOW (window)->buffer)
8666 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
8668 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8669 keybuf[t] = key;
8670 mock_input = t + 1;
8672 /* Arrange to go back to the original buffer once we're
8673 done reading the key sequence. Note that we can't
8674 use save_excursion_{save,restore} here, because they
8675 save point as well as the current buffer; we don't
8676 want to save point, because redisplay may change it,
8677 to accommodate a Fset_window_start or something. We
8678 don't want to do this at the top of the function,
8679 because we may get input from a subprocess which
8680 wants to change the selected window and stuff (say,
8681 emacsclient). */
8682 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
8684 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8685 Fkill_emacs (Qnil);
8686 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
8687 orig_local_map = get_local_map (PT, current_buffer,
8688 Qlocal_map);
8689 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8690 goto replay_sequence;
8693 /* For a mouse click, get the local text-property keymap
8694 of the place clicked on, rather than point. */
8695 if (last_real_key_start == 0
8696 && CONSP (XCDR (key))
8697 && ! localized_local_map)
8699 Lisp_Object map_here, start, pos;
8701 localized_local_map = 1;
8702 start = EVENT_START (key);
8704 if (CONSP (start) && CONSP (XCDR (start)))
8706 pos = POSN_BUFFER_POSN (start);
8707 if (INTEGERP (pos)
8708 && XINT (pos) >= BEG && XINT (pos) <= Z)
8710 map_here = get_local_map (XINT (pos),
8711 current_buffer, Qlocal_map);
8712 if (!EQ (map_here, orig_local_map))
8714 orig_local_map = map_here;
8715 keybuf[t] = key;
8716 mock_input = t + 1;
8718 goto replay_sequence;
8720 map_here = get_local_map (XINT (pos),
8721 current_buffer, Qkeymap);
8722 if (!EQ (map_here, orig_keymap))
8724 orig_keymap = map_here;
8725 keybuf[t] = key;
8726 mock_input = t + 1;
8728 goto replay_sequence;
8734 /* Expand mode-line and scroll-bar events into two events:
8735 use posn as a fake prefix key. */
8736 if (SYMBOLP (posn)
8737 && (NILP (fake_prefixed_keys)
8738 || NILP (Fmemq (key, fake_prefixed_keys))))
8740 if (t + 1 >= bufsize)
8741 error ("Key sequence too long");
8743 keybuf[t] = posn;
8744 keybuf[t + 1] = key;
8745 mock_input = t + 2;
8747 /* Record that a fake prefix key has been generated
8748 for KEY. Don't modify the event; this would
8749 prevent proper action when the event is pushed
8750 back into unread-command-events. */
8751 fake_prefixed_keys = Fcons (key, fake_prefixed_keys);
8753 /* If on a mode line string with a local keymap,
8754 reconsider the key sequence with that keymap. */
8755 if (CONSP (POSN_STRING (EVENT_START (key))))
8757 Lisp_Object string, pos, map, map2;
8759 string = POSN_STRING (EVENT_START (key));
8760 pos = XCDR (string);
8761 string = XCAR (string);
8762 if (XINT (pos) >= 0
8763 && XINT (pos) < SCHARS (string))
8765 map = Fget_text_property (pos, Qlocal_map, string);
8766 if (!NILP (map))
8767 orig_local_map = map;
8768 map2 = Fget_text_property (pos, Qkeymap, string);
8769 if (!NILP (map2))
8770 orig_keymap = map2;
8771 if (!NILP (map) || !NILP (map2))
8772 goto replay_sequence;
8776 goto replay_key;
8778 else if (CONSP (POSN_STRING (EVENT_START (key)))
8779 && NILP (from_string))
8781 /* For a click on a string, i.e. overlay string or a
8782 string displayed via the `display' property,
8783 consider `local-map' and `keymap' properties of
8784 that string. */
8785 Lisp_Object string, pos, map, map2;
8787 string = POSN_STRING (EVENT_START (key));
8788 pos = XCDR (string);
8789 string = XCAR (string);
8790 if (XINT (pos) >= 0
8791 && XINT (pos) < SCHARS (string))
8793 map = Fget_text_property (pos, Qlocal_map, string);
8794 if (!NILP (map))
8795 orig_local_map = map;
8796 map2 = Fget_text_property (pos, Qkeymap, string);
8797 if (!NILP (map2))
8798 orig_keymap = map2;
8800 if (!NILP (map) || !NILP (map2))
8802 from_string = string;
8803 goto replay_sequence;
8808 else if (CONSP (XCDR (key))
8809 && CONSP (EVENT_START (key))
8810 && CONSP (XCDR (EVENT_START (key))))
8812 Lisp_Object posn;
8814 posn = POSN_BUFFER_POSN (EVENT_START (key));
8815 /* Handle menu-bar events:
8816 insert the dummy prefix event `menu-bar'. */
8817 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
8819 if (t + 1 >= bufsize)
8820 error ("Key sequence too long");
8821 keybuf[t] = posn;
8822 keybuf[t+1] = key;
8824 /* Zap the position in key, so we know that we've
8825 expanded it, and don't try to do so again. */
8826 POSN_BUFFER_SET_POSN (EVENT_START (key),
8827 Fcons (posn, Qnil));
8829 mock_input = t + 2;
8830 goto replay_sequence;
8832 else if (CONSP (posn))
8834 /* We're looking at the second event of a
8835 sequence which we expanded before. Set
8836 last_real_key_start appropriately. */
8837 if (last_real_key_start == t && t > 0)
8838 last_real_key_start = t - 1;
8843 /* We have finally decided that KEY is something we might want
8844 to look up. */
8845 first_binding = (follow_key (key,
8846 nmaps - first_binding,
8847 submaps + first_binding,
8848 defs + first_binding,
8849 submaps + first_binding)
8850 + first_binding);
8852 /* If KEY wasn't bound, we'll try some fallbacks. */
8853 if (first_binding < nmaps)
8854 /* This is needed for the following scenario:
8855 event 0: a down-event that gets dropped by calling replay_key.
8856 event 1: some normal prefix like C-h.
8857 After event 0, first_unbound is 0, after event 1 fkey.start
8858 and keytran.start are both 1, so when we see that C-h is bound,
8859 we need to update first_unbound. */
8860 first_unbound = max (t + 1, first_unbound);
8861 else
8863 Lisp_Object head;
8865 /* Remember the position to put an upper bound on fkey.start. */
8866 first_unbound = min (t, first_unbound);
8868 head = EVENT_HEAD (key);
8869 if (help_char_p (head) && t > 0)
8871 read_key_sequence_cmd = Vprefix_help_command;
8872 keybuf[t++] = key;
8873 last_nonmenu_event = key;
8874 /* The Microsoft C compiler can't handle the goto that
8875 would go here. */
8876 dummyflag = 1;
8877 break;
8880 if (SYMBOLP (head))
8882 Lisp_Object breakdown;
8883 int modifiers;
8885 breakdown = parse_modifiers (head);
8886 modifiers = XINT (XCAR (XCDR (breakdown)));
8887 /* Attempt to reduce an unbound mouse event to a simpler
8888 event that is bound:
8889 Drags reduce to clicks.
8890 Double-clicks reduce to clicks.
8891 Triple-clicks reduce to double-clicks, then to clicks.
8892 Down-clicks are eliminated.
8893 Double-downs reduce to downs, then are eliminated.
8894 Triple-downs reduce to double-downs, then to downs,
8895 then are eliminated. */
8896 if (modifiers & (down_modifier | drag_modifier
8897 | double_modifier | triple_modifier))
8899 while (modifiers & (down_modifier | drag_modifier
8900 | double_modifier | triple_modifier))
8902 Lisp_Object new_head, new_click;
8903 if (modifiers & triple_modifier)
8904 modifiers ^= (double_modifier | triple_modifier);
8905 else if (modifiers & double_modifier)
8906 modifiers &= ~double_modifier;
8907 else if (modifiers & drag_modifier)
8908 modifiers &= ~drag_modifier;
8909 else
8911 /* Dispose of this `down' event by simply jumping
8912 back to replay_key, to get another event.
8914 Note that if this event came from mock input,
8915 then just jumping back to replay_key will just
8916 hand it to us again. So we have to wipe out any
8917 mock input.
8919 We could delete keybuf[t] and shift everything
8920 after that to the left by one spot, but we'd also
8921 have to fix up any variable that points into
8922 keybuf, and shifting isn't really necessary
8923 anyway.
8925 Adding prefixes for non-textual mouse clicks
8926 creates two characters of mock input, and both
8927 must be thrown away. If we're only looking at
8928 the prefix now, we can just jump back to
8929 replay_key. On the other hand, if we've already
8930 processed the prefix, and now the actual click
8931 itself is giving us trouble, then we've lost the
8932 state of the keymaps we want to backtrack to, and
8933 we need to replay the whole sequence to rebuild
8936 Beyond that, only function key expansion could
8937 create more than two keys, but that should never
8938 generate mouse events, so it's okay to zero
8939 mock_input in that case too.
8941 FIXME: The above paragraph seems just plain
8942 wrong, if you consider things like
8943 xterm-mouse-mode. -stef
8945 Isn't this just the most wonderful code ever? */
8946 if (t == last_real_key_start)
8948 mock_input = 0;
8949 goto replay_key;
8951 else
8953 mock_input = last_real_key_start;
8954 goto replay_sequence;
8958 new_head
8959 = apply_modifiers (modifiers, XCAR (breakdown));
8960 new_click
8961 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
8963 /* Look for a binding for this new key. follow_key
8964 promises that it didn't munge submaps the
8965 last time we called it, since key was unbound. */
8966 first_binding
8967 = (follow_key (new_click,
8968 nmaps - local_first_binding,
8969 submaps + local_first_binding,
8970 defs + local_first_binding,
8971 submaps + local_first_binding)
8972 + local_first_binding);
8974 /* If that click is bound, go for it. */
8975 if (first_binding < nmaps)
8977 key = new_click;
8978 break;
8980 /* Otherwise, we'll leave key set to the drag event. */
8986 keybuf[t++] = key;
8987 /* Normally, last_nonmenu_event gets the previous key we read.
8988 But when a mouse popup menu is being used,
8989 we don't update last_nonmenu_event; it continues to hold the mouse
8990 event that preceded the first level of menu. */
8991 if (!used_mouse_menu)
8992 last_nonmenu_event = key;
8994 /* Record what part of this_command_keys is the current key sequence. */
8995 this_single_command_key_start = this_command_key_count - t;
8997 if (first_binding < nmaps && NILP (submaps[first_binding]))
8998 /* There is a binding and it's not a prefix.
8999 There is thus no function-key in this sequence.
9000 Moving fkey.start is important in this case to allow keytran.start
9001 to go over the sequence before we return (since we keep the
9002 invariant that keytran.end <= fkey.start). */
9004 if (fkey.start < t)
9005 (fkey.start = fkey.end = t, fkey.map = Vfunction_key_map);
9007 else
9008 /* If the sequence is unbound, see if we can hang a function key
9009 off the end of it. */
9011 Lisp_Object next;
9013 /* Continue scan from fkey.end until we find a bound suffix.
9014 If we fail, increment fkey.start and start over from there. */
9015 while (fkey.end < t)
9017 Lisp_Object key;
9019 key = keybuf[fkey.end++];
9020 next = access_keymap (fkey.map, key, 1, 0, 1);
9022 /* Handle symbol with autoload definition. */
9023 if (SYMBOLP (next) && ! NILP (Ffboundp (next))
9024 && CONSP (XSYMBOL (next)->function)
9025 && EQ (XCAR (XSYMBOL (next)->function), Qautoload))
9026 do_autoload (XSYMBOL (next)->function, next);
9028 /* Handle a symbol whose function definition is a keymap
9029 or an array. */
9030 if (SYMBOLP (next) && ! NILP (Ffboundp (next))
9031 && (!NILP (Farrayp (XSYMBOL (next)->function))
9032 || KEYMAPP (XSYMBOL (next)->function)))
9033 next = XSYMBOL (next)->function;
9035 #if 0 /* I didn't turn this on, because it might cause trouble
9036 for the mapping of return into C-m and tab into C-i. */
9037 /* Optionally don't map function keys into other things.
9038 This enables the user to redefine kp- keys easily. */
9039 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
9040 next = Qnil;
9041 #endif
9043 /* If the function key map gives a function, not an
9044 array, then call the function with no args and use
9045 its value instead. */
9046 if (SYMBOLP (next) && ! NILP (Ffboundp (next))
9047 /* If there's a binding (i.e. first_binding >= nmaps)
9048 we don't want to apply this function-key-mapping. */
9049 && fkey.end == t && first_binding >= nmaps)
9051 struct gcpro gcpro1, gcpro2, gcpro3;
9052 Lisp_Object tem;
9053 tem = next;
9055 GCPRO3 (fkey.map, keytran.map, delayed_switch_frame);
9056 next = call1 (next, prompt);
9057 UNGCPRO;
9058 /* If the function returned something invalid,
9059 barf--don't ignore it.
9060 (To ignore it safely, we would need to gcpro a bunch of
9061 other variables.) */
9062 if (! (VECTORP (next) || STRINGP (next)))
9063 error ("Function in key-translation-map returns invalid key sequence");
9066 /* If keybuf[fkey.start..fkey.end] is bound in the
9067 function key map and it's a suffix of the current
9068 sequence (i.e. fkey.end == t), replace it with
9069 the binding and restart with fkey.start at the end. */
9070 if ((VECTORP (next) || STRINGP (next))
9071 /* If there's a binding (i.e. first_binding >= nmaps)
9072 we don't want to apply this function-key-mapping. */
9073 && fkey.end == t && first_binding >= nmaps)
9075 int len = XFASTINT (Flength (next));
9077 t = fkey.start + len;
9078 if (t >= bufsize)
9079 error ("Key sequence too long");
9081 if (VECTORP (next))
9082 bcopy (XVECTOR (next)->contents,
9083 keybuf + fkey.start,
9084 (t - fkey.start) * sizeof (keybuf[0]));
9085 else if (STRINGP (next))
9087 int i;
9089 for (i = 0; i < len; i++)
9090 XSETFASTINT (keybuf[fkey.start + i], SREF (next, i));
9093 mock_input = t;
9094 fkey.start = fkey.end = t;
9095 fkey.map = Vfunction_key_map;
9097 /* Do pass the results through key-translation-map.
9098 But don't retranslate what key-translation-map
9099 has already translated. */
9100 keytran.end = keytran.start;
9101 keytran.map = Vkey_translation_map;
9103 goto replay_sequence;
9106 fkey.map = get_keymap (next, 0, 1);
9108 /* If we no longer have a bound suffix, try a new positions for
9109 fkey.start. */
9110 if (!CONSP (fkey.map))
9112 fkey.end = ++fkey.start;
9113 fkey.map = Vfunction_key_map;
9118 /* Look for this sequence in key-translation-map. */
9120 Lisp_Object next;
9122 /* Scan from keytran.end until we find a bound suffix. */
9123 while (keytran.end < fkey.start)
9125 Lisp_Object key;
9127 key = keybuf[keytran.end++];
9128 next = access_keymap (keytran.map, key, 1, 0, 1);
9130 /* Handle symbol with autoload definition. */
9131 if (SYMBOLP (next) && ! NILP (Ffboundp (next))
9132 && CONSP (XSYMBOL (next)->function)
9133 && EQ (XCAR (XSYMBOL (next)->function), Qautoload))
9134 do_autoload (XSYMBOL (next)->function, next);
9136 /* Handle a symbol whose function definition is a keymap
9137 or an array. */
9138 if (SYMBOLP (next) && ! NILP (Ffboundp (next))
9139 && (!NILP (Farrayp (XSYMBOL (next)->function))
9140 || KEYMAPP (XSYMBOL (next)->function)))
9141 next = XSYMBOL (next)->function;
9143 /* If the key translation map gives a function, not an
9144 array, then call the function with one arg and use
9145 its value instead. */
9146 if (SYMBOLP (next) && ! NILP (Ffboundp (next)))
9148 struct gcpro gcpro1, gcpro2, gcpro3;
9149 Lisp_Object tem;
9150 tem = next;
9152 GCPRO3 (fkey.map, keytran.map, delayed_switch_frame);
9153 next = call1 (next, prompt);
9154 UNGCPRO;
9155 /* If the function returned something invalid,
9156 barf--don't ignore it.
9157 (To ignore it safely, we would need to gcpro a bunch of
9158 other variables.) */
9159 if (! (VECTORP (next) || STRINGP (next)))
9160 error ("Function in key-translation-map returns invalid key sequence");
9163 /* If keybuf[keytran.start..keytran.end] is bound in the
9164 key translation map and it's a suffix of the current
9165 sequence (i.e. keytran.end == t), replace it with
9166 the binding and restart with keytran.start at the end. */
9167 if ((VECTORP (next) || STRINGP (next)))
9169 int len = XFASTINT (Flength (next));
9170 int i, diff = len - (keytran.end - keytran.start);
9172 mock_input = max (t, mock_input);
9173 if (mock_input + diff >= bufsize)
9174 error ("Key sequence too long");
9176 /* Shift the keys that are after keytran.end. */
9177 if (diff < 0)
9178 for (i = keytran.end; i < mock_input; i++)
9179 keybuf[i + diff] = keybuf[i];
9180 else if (diff > 0)
9181 for (i = mock_input - 1; i >= keytran.end; i--)
9182 keybuf[i + diff] = keybuf[i];
9183 /* Replace the keys between keytran.start and keytran.end
9184 with those from next. */
9185 for (i = 0; i < len; i++)
9186 keybuf[keytran.start + i]
9187 = Faref (next, make_number (i));
9189 mock_input += diff;
9190 keytran.start = keytran.end += diff;
9191 keytran.map = Vkey_translation_map;
9193 /* Adjust the function-key-map counters. */
9194 fkey.start += diff;
9195 fkey.end += diff;
9197 goto replay_sequence;
9200 keytran.map = get_keymap (next, 0, 1);
9202 /* If we no longer have a bound suffix, try a new positions for
9203 keytran.start. */
9204 if (!CONSP (keytran.map))
9206 keytran.end = ++keytran.start;
9207 keytran.map = Vkey_translation_map;
9212 /* If KEY is not defined in any of the keymaps,
9213 and cannot be part of a function key or translation,
9214 and is an upper case letter
9215 use the corresponding lower-case letter instead. */
9216 if (first_binding >= nmaps
9217 && fkey.start >= t && keytran.start >= t
9218 && INTEGERP (key)
9219 && ((((XINT (key) & 0x3ffff)
9220 < XCHAR_TABLE (current_buffer->downcase_table)->size)
9221 && UPPERCASEP (XINT (key) & 0x3ffff))
9222 || (XINT (key) & shift_modifier)))
9224 Lisp_Object new_key;
9226 original_uppercase = key;
9227 original_uppercase_position = t - 1;
9229 if (XINT (key) & shift_modifier)
9230 XSETINT (new_key, XINT (key) & ~shift_modifier);
9231 else
9232 XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
9233 | (XINT (key) & ~0x3ffff)));
9235 /* We have to do this unconditionally, regardless of whether
9236 the lower-case char is defined in the keymaps, because they
9237 might get translated through function-key-map. */
9238 keybuf[t - 1] = new_key;
9239 mock_input = max (t, mock_input);
9241 goto replay_sequence;
9243 /* If KEY is not defined in any of the keymaps,
9244 and cannot be part of a function key or translation,
9245 and is a shifted function key,
9246 use the corresponding unshifted function key instead. */
9247 if (first_binding >= nmaps
9248 && fkey.start >= t && keytran.start >= t
9249 && SYMBOLP (key))
9251 Lisp_Object breakdown;
9252 int modifiers;
9254 breakdown = parse_modifiers (key);
9255 modifiers = XINT (XCAR (XCDR (breakdown)));
9256 if (modifiers & shift_modifier)
9258 Lisp_Object new_key;
9260 original_uppercase = key;
9261 original_uppercase_position = t - 1;
9263 modifiers &= ~shift_modifier;
9264 new_key = apply_modifiers (modifiers,
9265 XCAR (breakdown));
9267 keybuf[t - 1] = new_key;
9268 mock_input = max (t, mock_input);
9270 goto replay_sequence;
9275 if (!dummyflag)
9276 read_key_sequence_cmd = (first_binding < nmaps
9277 ? defs[first_binding]
9278 : Qnil);
9280 unread_switch_frame = delayed_switch_frame;
9281 unbind_to (count, Qnil);
9283 /* Don't downcase the last character if the caller says don't.
9284 Don't downcase it if the result is undefined, either. */
9285 if ((dont_downcase_last || first_binding >= nmaps)
9286 && t - 1 == original_uppercase_position)
9287 keybuf[t - 1] = original_uppercase;
9289 /* Occasionally we fabricate events, perhaps by expanding something
9290 according to function-key-map, or by adding a prefix symbol to a
9291 mouse click in the scroll bar or modeline. In this cases, return
9292 the entire generated key sequence, even if we hit an unbound
9293 prefix or a definition before the end. This means that you will
9294 be able to push back the event properly, and also means that
9295 read-key-sequence will always return a logical unit.
9297 Better ideas? */
9298 for (; t < mock_input; t++)
9300 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
9301 && NILP (Fzerop (Vecho_keystrokes)))
9302 echo_char (keybuf[t]);
9303 add_command_key (keybuf[t]);
9308 UNGCPRO;
9309 return t;
9312 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 5, 0,
9313 doc: /* Read a sequence of keystrokes and return as a string or vector.
9314 The sequence is sufficient to specify a non-prefix command in the
9315 current local and global maps.
9317 First arg PROMPT is a prompt string. If nil, do not prompt specially.
9318 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos
9319 as a continuation of the previous key.
9321 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not
9322 convert the last event to lower case. (Normally any upper case event
9323 is converted to lower case if the original event is undefined and the lower
9324 case equivalent is defined.) A non-nil value is appropriate for reading
9325 a key sequence to be defined.
9327 A C-g typed while in this function is treated like any other character,
9328 and `quit-flag' is not set.
9330 If the key sequence starts with a mouse click, then the sequence is read
9331 using the keymaps of the buffer of the window clicked in, not the buffer
9332 of the selected window as normal.
9334 `read-key-sequence' drops unbound button-down events, since you normally
9335 only care about the click or drag events which follow them. If a drag
9336 or multi-click event is unbound, but the corresponding click event would
9337 be bound, `read-key-sequence' turns the event into a click event at the
9338 drag's starting position. This means that you don't have to distinguish
9339 between click and drag, double, or triple events unless you want to.
9341 `read-key-sequence' prefixes mouse events on mode lines, the vertical
9342 lines separating windows, and scroll bars with imaginary keys
9343 `mode-line', `vertical-line', and `vertical-scroll-bar'.
9345 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this
9346 function will process a switch-frame event if the user switches frames
9347 before typing anything. If the user switches frames in the middle of a
9348 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME
9349 is nil, then the event will be put off until after the current key sequence.
9351 `read-key-sequence' checks `function-key-map' for function key
9352 sequences, where they wouldn't conflict with ordinary bindings. See
9353 `function-key-map' for more details.
9355 The optional fifth argument COMMAND-LOOP, if non-nil, means
9356 that this key sequence is being read by something that will
9357 read commands one after another. It should be nil if the caller
9358 will read just one key sequence. */)
9359 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
9360 command_loop)
9361 Lisp_Object prompt, continue_echo, dont_downcase_last;
9362 Lisp_Object can_return_switch_frame, command_loop;
9364 Lisp_Object keybuf[30];
9365 register int i;
9366 struct gcpro gcpro1;
9367 int count = SPECPDL_INDEX ();
9369 if (!NILP (prompt))
9370 CHECK_STRING (prompt);
9371 QUIT;
9373 specbind (Qinput_method_exit_on_first_char,
9374 (NILP (command_loop) ? Qt : Qnil));
9375 specbind (Qinput_method_use_echo_area,
9376 (NILP (command_loop) ? Qt : Qnil));
9378 bzero (keybuf, sizeof keybuf);
9379 GCPRO1 (keybuf[0]);
9380 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
9382 if (NILP (continue_echo))
9384 this_command_key_count = 0;
9385 this_single_command_key_start = 0;
9388 #ifdef HAVE_X_WINDOWS
9389 if (display_hourglass_p)
9390 cancel_hourglass ();
9391 #endif
9393 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
9394 prompt, ! NILP (dont_downcase_last),
9395 ! NILP (can_return_switch_frame), 0);
9397 #if 0 /* The following is fine for code reading a key sequence and
9398 then proceeding with a lenghty computation, but it's not good
9399 for code reading keys in a loop, like an input method. */
9400 #ifdef HAVE_X_WINDOWS
9401 if (display_hourglass_p)
9402 start_hourglass ();
9403 #endif
9404 #endif
9406 if (i == -1)
9408 Vquit_flag = Qt;
9409 QUIT;
9411 UNGCPRO;
9412 return unbind_to (count, make_event_array (i, keybuf));
9415 DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
9416 Sread_key_sequence_vector, 1, 5, 0,
9417 doc: /* Like `read-key-sequence' but always return a vector. */)
9418 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
9419 command_loop)
9420 Lisp_Object prompt, continue_echo, dont_downcase_last;
9421 Lisp_Object can_return_switch_frame, command_loop;
9423 Lisp_Object keybuf[30];
9424 register int i;
9425 struct gcpro gcpro1;
9426 int count = SPECPDL_INDEX ();
9428 if (!NILP (prompt))
9429 CHECK_STRING (prompt);
9430 QUIT;
9432 specbind (Qinput_method_exit_on_first_char,
9433 (NILP (command_loop) ? Qt : Qnil));
9434 specbind (Qinput_method_use_echo_area,
9435 (NILP (command_loop) ? Qt : Qnil));
9437 bzero (keybuf, sizeof keybuf);
9438 GCPRO1 (keybuf[0]);
9439 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
9441 if (NILP (continue_echo))
9443 this_command_key_count = 0;
9444 this_single_command_key_start = 0;
9447 #ifdef HAVE_X_WINDOWS
9448 if (display_hourglass_p)
9449 cancel_hourglass ();
9450 #endif
9452 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
9453 prompt, ! NILP (dont_downcase_last),
9454 ! NILP (can_return_switch_frame), 0);
9456 #ifdef HAVE_X_WINDOWS
9457 if (display_hourglass_p)
9458 start_hourglass ();
9459 #endif
9461 if (i == -1)
9463 Vquit_flag = Qt;
9464 QUIT;
9466 UNGCPRO;
9467 return unbind_to (count, Fvector (i, keybuf));
9470 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
9471 doc: /* Execute CMD as an editor command.
9472 CMD must be a symbol that satisfies the `commandp' predicate.
9473 Optional second arg RECORD-FLAG non-nil
9474 means unconditionally put this command in `command-history'.
9475 Otherwise, that is done only if an arg is read using the minibuffer.
9476 The argument KEYS specifies the value to use instead of (this-command-keys)
9477 when reading the arguments; if it is nil, (this-command-keys) is used.
9478 The argument SPECIAL, if non-nil, means that this command is executing
9479 a special event, so ignore the prefix argument and don't clear it. */)
9480 (cmd, record_flag, keys, special)
9481 Lisp_Object cmd, record_flag, keys, special;
9483 register Lisp_Object final;
9484 register Lisp_Object tem;
9485 Lisp_Object prefixarg;
9486 struct backtrace backtrace;
9487 extern int debug_on_next_call;
9489 debug_on_next_call = 0;
9491 if (NILP (special))
9493 prefixarg = current_kboard->Vprefix_arg;
9494 Vcurrent_prefix_arg = prefixarg;
9495 current_kboard->Vprefix_arg = Qnil;
9497 else
9498 prefixarg = Qnil;
9500 if (SYMBOLP (cmd))
9502 tem = Fget (cmd, Qdisabled);
9503 if (!NILP (tem) && !NILP (Vrun_hooks))
9505 tem = Fsymbol_value (Qdisabled_command_hook);
9506 if (!NILP (tem))
9507 return call1 (Vrun_hooks, Qdisabled_command_hook);
9511 while (1)
9513 final = Findirect_function (cmd);
9515 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
9517 struct gcpro gcpro1, gcpro2;
9519 GCPRO2 (cmd, prefixarg);
9520 do_autoload (final, cmd);
9521 UNGCPRO;
9523 else
9524 break;
9527 if (STRINGP (final) || VECTORP (final))
9529 /* If requested, place the macro in the command history. For
9530 other sorts of commands, call-interactively takes care of
9531 this. */
9532 if (!NILP (record_flag))
9534 Vcommand_history
9535 = Fcons (Fcons (Qexecute_kbd_macro,
9536 Fcons (final, Fcons (prefixarg, Qnil))),
9537 Vcommand_history);
9539 /* Don't keep command history around forever. */
9540 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
9542 tem = Fnthcdr (Vhistory_length, Vcommand_history);
9543 if (CONSP (tem))
9544 XSETCDR (tem, Qnil);
9548 return Fexecute_kbd_macro (final, prefixarg, Qnil);
9551 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
9553 backtrace.next = backtrace_list;
9554 backtrace_list = &backtrace;
9555 backtrace.function = &Qcall_interactively;
9556 backtrace.args = &cmd;
9557 backtrace.nargs = 1;
9558 backtrace.evalargs = 0;
9560 tem = Fcall_interactively (cmd, record_flag, keys);
9562 backtrace_list = backtrace.next;
9563 return tem;
9565 return Qnil;
9570 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
9571 1, 1, "P",
9572 doc: /* Read function name, then read its arguments and call it. */)
9573 (prefixarg)
9574 Lisp_Object prefixarg;
9576 Lisp_Object function;
9577 char buf[40];
9578 int saved_last_point_position;
9579 Lisp_Object saved_keys, saved_last_point_position_buffer;
9580 Lisp_Object bindings, value;
9581 struct gcpro gcpro1, gcpro2, gcpro3;
9583 saved_keys = Fvector (this_command_key_count,
9584 XVECTOR (this_command_keys)->contents);
9585 saved_last_point_position_buffer = last_point_position_buffer;
9586 saved_last_point_position = last_point_position;
9587 buf[0] = 0;
9588 GCPRO3 (saved_keys, prefixarg, saved_last_point_position_buffer);
9590 if (EQ (prefixarg, Qminus))
9591 strcpy (buf, "- ");
9592 else if (CONSP (prefixarg) && XINT (XCAR (prefixarg)) == 4)
9593 strcpy (buf, "C-u ");
9594 else if (CONSP (prefixarg) && INTEGERP (XCAR (prefixarg)))
9596 if (sizeof (int) == sizeof (EMACS_INT))
9597 sprintf (buf, "%d ", XINT (XCAR (prefixarg)));
9598 else if (sizeof (long) == sizeof (EMACS_INT))
9599 sprintf (buf, "%ld ", (long) XINT (XCAR (prefixarg)));
9600 else
9601 abort ();
9603 else if (INTEGERP (prefixarg))
9605 if (sizeof (int) == sizeof (EMACS_INT))
9606 sprintf (buf, "%d ", XINT (prefixarg));
9607 else if (sizeof (long) == sizeof (EMACS_INT))
9608 sprintf (buf, "%ld ", (long) XINT (prefixarg));
9609 else
9610 abort ();
9613 /* This isn't strictly correct if execute-extended-command
9614 is bound to anything else. Perhaps it should use
9615 this_command_keys? */
9616 strcat (buf, "M-x ");
9618 /* Prompt with buf, and then read a string, completing from and
9619 restricting to the set of all defined commands. Don't provide
9620 any initial input. Save the command read on the extended-command
9621 history list. */
9622 function = Fcompleting_read (build_string (buf),
9623 Vobarray, Qcommandp,
9624 Qt, Qnil, Qextended_command_history, Qnil,
9625 Qnil);
9627 if (STRINGP (function) && SCHARS (function) == 0)
9628 error ("No command name given");
9630 /* Set this_command_keys to the concatenation of saved_keys and
9631 function, followed by a RET. */
9633 Lisp_Object *keys;
9634 int i;
9636 this_command_key_count = 0;
9637 this_single_command_key_start = 0;
9639 keys = XVECTOR (saved_keys)->contents;
9640 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
9641 add_command_key (keys[i]);
9643 for (i = 0; i < SCHARS (function); i++)
9644 add_command_key (Faref (function, make_number (i)));
9646 add_command_key (make_number ('\015'));
9649 last_point_position = saved_last_point_position;
9650 last_point_position_buffer = saved_last_point_position_buffer;
9652 UNGCPRO;
9654 function = Fintern (function, Qnil);
9655 current_kboard->Vprefix_arg = prefixarg;
9656 Vthis_command = function;
9657 real_this_command = function;
9659 /* If enabled, show which key runs this command. */
9660 if (!NILP (Vsuggest_key_bindings)
9661 && NILP (Vexecuting_macro)
9662 && SYMBOLP (function))
9663 bindings = Fwhere_is_internal (function, Voverriding_local_map,
9664 Qt, Qnil, Qnil);
9665 else
9666 bindings = Qnil;
9668 value = Qnil;
9669 GCPRO2 (bindings, value);
9670 value = Fcommand_execute (function, Qt, Qnil, Qnil);
9672 /* If the command has a key binding, print it now. */
9673 if (!NILP (bindings)
9674 && ! (VECTORP (bindings) && EQ (Faref (bindings, make_number (0)),
9675 Qmouse_movement)))
9677 /* But first wait, and skip the message if there is input. */
9678 int delay_time;
9679 if (!NILP (echo_area_buffer[0]))
9680 /* This command displayed something in the echo area;
9681 so wait a few seconds, then display our suggestion message. */
9682 delay_time = (NUMBERP (Vsuggest_key_bindings)
9683 ? XINT (Vsuggest_key_bindings) : 2);
9684 else
9685 /* This command left the echo area empty,
9686 so display our message immediately. */
9687 delay_time = 0;
9689 if (!NILP (Fsit_for (make_number (delay_time), Qnil, Qnil))
9690 && ! CONSP (Vunread_command_events))
9692 Lisp_Object binding;
9693 char *newmessage;
9694 int message_p = push_message ();
9695 int count = SPECPDL_INDEX ();
9697 record_unwind_protect (pop_message_unwind, Qnil);
9698 binding = Fkey_description (bindings);
9700 newmessage
9701 = (char *) alloca (SCHARS (SYMBOL_NAME (function))
9702 + SBYTES (binding)
9703 + 100);
9704 sprintf (newmessage, "You can run the command `%s' with %s",
9705 SDATA (SYMBOL_NAME (function)),
9706 SDATA (binding));
9707 message2_nolog (newmessage,
9708 strlen (newmessage),
9709 STRING_MULTIBYTE (binding));
9710 if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
9711 ? Vsuggest_key_bindings : make_number (2)),
9712 Qnil, Qnil))
9713 && message_p)
9714 restore_message ();
9716 unbind_to (count, Qnil);
9720 RETURN_UNGCPRO (value);
9724 /* Return nonzero if input events are pending. */
9727 detect_input_pending ()
9729 if (!input_pending)
9730 get_input_pending (&input_pending, 0);
9732 return input_pending;
9735 /* Return nonzero if input events are pending, and run any pending timers. */
9738 detect_input_pending_run_timers (do_display)
9739 int do_display;
9741 int old_timers_run = timers_run;
9743 if (!input_pending)
9744 get_input_pending (&input_pending, 1);
9746 if (old_timers_run != timers_run && do_display)
9748 redisplay_preserve_echo_area (8);
9749 /* The following fixes a bug when using lazy-lock with
9750 lazy-lock-defer-on-the-fly set to t, i.e. when fontifying
9751 from an idle timer function. The symptom of the bug is that
9752 the cursor sometimes doesn't become visible until the next X
9753 event is processed. --gerd. */
9754 if (rif)
9755 rif->flush_display (NULL);
9758 return input_pending;
9761 /* This is called in some cases before a possible quit.
9762 It cases the next call to detect_input_pending to recompute input_pending.
9763 So calling this function unnecessarily can't do any harm. */
9765 void
9766 clear_input_pending ()
9768 input_pending = 0;
9771 /* Return nonzero if there are pending requeued events.
9772 This isn't used yet. The hope is to make wait_reading_process_input
9773 call it, and return if it runs Lisp code that unreads something.
9774 The problem is, kbd_buffer_get_event needs to be fixed to know what
9775 to do in that case. It isn't trivial. */
9778 requeued_events_pending_p ()
9780 return (!NILP (Vunread_command_events) || unread_command_char != -1);
9784 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
9785 doc: /* Return t if command input is currently available with no wait.
9786 Actually, the value is nil only if we can be sure that no input is available;
9787 if there is a doubt, the value is t. */)
9790 if (!NILP (Vunread_command_events) || unread_command_char != -1)
9791 return (Qt);
9793 get_filtered_input_pending (&input_pending, 1, 1);
9794 return input_pending > 0 ? Qt : Qnil;
9797 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
9798 doc: /* Return vector of last 100 events, not counting those from keyboard macros. */)
9801 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
9802 Lisp_Object val;
9804 if (total_keys < NUM_RECENT_KEYS)
9805 return Fvector (total_keys, keys);
9806 else
9808 val = Fvector (NUM_RECENT_KEYS, keys);
9809 bcopy (keys + recent_keys_index,
9810 XVECTOR (val)->contents,
9811 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
9812 bcopy (keys,
9813 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
9814 recent_keys_index * sizeof (Lisp_Object));
9815 return val;
9819 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
9820 doc: /* Return the key sequence that invoked this command.
9821 However, if the command has called `read-key-sequence', it returns
9822 the last key sequence that has been read.
9823 The value is a string or a vector. */)
9826 return make_event_array (this_command_key_count,
9827 XVECTOR (this_command_keys)->contents);
9830 DEFUN ("this-command-keys-vector", Fthis_command_keys_vector, Sthis_command_keys_vector, 0, 0, 0,
9831 doc: /* Return the key sequence that invoked this command, as a vector.
9832 However, if the command has called `read-key-sequence', it returns
9833 the last key sequence that has been read. */)
9836 return Fvector (this_command_key_count,
9837 XVECTOR (this_command_keys)->contents);
9840 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
9841 Sthis_single_command_keys, 0, 0, 0,
9842 doc: /* Return the key sequence that invoked this command.
9843 More generally, it returns the last key sequence read, either by
9844 the command loop or by `read-key-sequence'.
9845 Unlike `this-command-keys', this function's value
9846 does not include prefix arguments.
9847 The value is always a vector. */)
9850 return Fvector (this_command_key_count
9851 - this_single_command_key_start,
9852 (XVECTOR (this_command_keys)->contents
9853 + this_single_command_key_start));
9856 DEFUN ("this-single-command-raw-keys", Fthis_single_command_raw_keys,
9857 Sthis_single_command_raw_keys, 0, 0, 0,
9858 doc: /* Return the raw events that were read for this command.
9859 More generally, it returns the last key sequence read, either by
9860 the command loop or by `read-key-sequence'.
9861 Unlike `this-single-command-keys', this function's value
9862 shows the events before all translations (except for input methods).
9863 The value is always a vector. */)
9866 return Fvector (raw_keybuf_count,
9867 (XVECTOR (raw_keybuf)->contents));
9870 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
9871 Sreset_this_command_lengths, 0, 0, 0,
9872 doc: /* Used for complicated reasons in `universal-argument-other-key'.
9874 `universal-argument-other-key' rereads the event just typed.
9875 It then gets translated through `function-key-map'.
9876 The translated event gets included in the echo area and in
9877 the value of `this-command-keys' in addition to the raw original event.
9878 That is not right.
9880 Calling this function directs the translated event to replace
9881 the original event, so that only one version of the event actually
9882 appears in the echo area and in the value of `this-command-keys'. */)
9885 before_command_restore_flag = 1;
9886 before_command_key_count_1 = before_command_key_count;
9887 before_command_echo_length_1 = before_command_echo_length;
9888 return Qnil;
9891 DEFUN ("clear-this-command-keys", Fclear_this_command_keys,
9892 Sclear_this_command_keys, 0, 1, 0,
9893 doc: /* Clear out the vector that `this-command-keys' returns.
9894 Also clear the record of the last 100 events, unless optional arg
9895 KEEP-RECORD is non-nil. */)
9896 (keep_record)
9897 Lisp_Object keep_record;
9899 int i;
9901 this_command_key_count = 0;
9903 if (NILP (keep_record))
9905 for (i = 0; i < XVECTOR (recent_keys)->size; ++i)
9906 XVECTOR (recent_keys)->contents[i] = Qnil;
9907 total_keys = 0;
9908 recent_keys_index = 0;
9910 return Qnil;
9913 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
9914 doc: /* Return the current depth in recursive edits. */)
9917 Lisp_Object temp;
9918 XSETFASTINT (temp, command_loop_level + minibuf_level);
9919 return temp;
9922 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
9923 "FOpen dribble file: ",
9924 doc: /* Start writing all keyboard characters to a dribble file called FILE.
9925 If FILE is nil, close any open dribble file. */)
9926 (file)
9927 Lisp_Object file;
9929 if (dribble)
9931 fclose (dribble);
9932 dribble = 0;
9934 if (!NILP (file))
9936 file = Fexpand_file_name (file, Qnil);
9937 dribble = fopen (SDATA (file), "w");
9938 if (dribble == 0)
9939 report_file_error ("Opening dribble", Fcons (file, Qnil));
9941 return Qnil;
9944 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
9945 doc: /* Discard the contents of the terminal input buffer.
9946 Also end any kbd macro being defined. */)
9949 if (!NILP (current_kboard->defining_kbd_macro))
9951 /* Discard the last command from the macro. */
9952 Fcancel_kbd_macro_events ();
9953 end_kbd_macro ();
9956 update_mode_lines++;
9958 Vunread_command_events = Qnil;
9959 unread_command_char = -1;
9961 discard_tty_input ();
9963 kbd_fetch_ptr = kbd_store_ptr;
9964 Ffillarray (kbd_buffer_gcpro, Qnil);
9965 input_pending = 0;
9967 return Qnil;
9970 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
9971 doc: /* Stop Emacs and return to superior process. You can resume later.
9972 If `cannot-suspend' is non-nil, or if the system doesn't support job
9973 control, run a subshell instead.
9975 If optional arg STUFFSTRING is non-nil, its characters are stuffed
9976 to be read as terminal input by Emacs's parent, after suspension.
9978 Before suspending, run the normal hook `suspend-hook'.
9979 After resumption run the normal hook `suspend-resume-hook'.
9981 Some operating systems cannot stop the Emacs process and resume it later.
9982 On such systems, Emacs starts a subshell instead of suspending. */)
9983 (stuffstring)
9984 Lisp_Object stuffstring;
9986 int count = SPECPDL_INDEX ();
9987 int old_height, old_width;
9988 int width, height;
9989 struct gcpro gcpro1;
9991 if (!NILP (stuffstring))
9992 CHECK_STRING (stuffstring);
9994 /* Run the functions in suspend-hook. */
9995 if (!NILP (Vrun_hooks))
9996 call1 (Vrun_hooks, intern ("suspend-hook"));
9998 GCPRO1 (stuffstring);
9999 get_frame_size (&old_width, &old_height);
10000 reset_sys_modes ();
10001 /* sys_suspend can get an error if it tries to fork a subshell
10002 and the system resources aren't available for that. */
10003 record_unwind_protect ((Lisp_Object (*) P_ ((Lisp_Object))) init_sys_modes,
10004 Qnil);
10005 stuff_buffered_input (stuffstring);
10006 if (cannot_suspend)
10007 sys_subshell ();
10008 else
10009 sys_suspend ();
10010 unbind_to (count, Qnil);
10012 /* Check if terminal/window size has changed.
10013 Note that this is not useful when we are running directly
10014 with a window system; but suspend should be disabled in that case. */
10015 get_frame_size (&width, &height);
10016 if (width != old_width || height != old_height)
10017 change_frame_size (SELECTED_FRAME (), height, width, 0, 0, 0);
10019 /* Run suspend-resume-hook. */
10020 if (!NILP (Vrun_hooks))
10021 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
10023 UNGCPRO;
10024 return Qnil;
10027 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
10028 Then in any case stuff anything Emacs has read ahead and not used. */
10030 void
10031 stuff_buffered_input (stuffstring)
10032 Lisp_Object stuffstring;
10034 /* stuff_char works only in BSD, versions 4.2 and up. */
10035 #ifdef BSD_SYSTEM
10036 #ifndef BSD4_1
10037 register unsigned char *p;
10039 if (STRINGP (stuffstring))
10041 register int count;
10043 p = SDATA (stuffstring);
10044 count = SBYTES (stuffstring);
10045 while (count-- > 0)
10046 stuff_char (*p++);
10047 stuff_char ('\n');
10050 /* Anything we have read ahead, put back for the shell to read. */
10051 /* ?? What should this do when we have multiple keyboards??
10052 Should we ignore anything that was typed in at the "wrong" kboard? */
10053 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
10055 int idx;
10057 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
10058 kbd_fetch_ptr = kbd_buffer;
10059 if (kbd_fetch_ptr->kind == ASCII_KEYSTROKE_EVENT)
10060 stuff_char (kbd_fetch_ptr->code);
10062 kbd_fetch_ptr->kind = NO_EVENT;
10063 idx = 2 * (kbd_fetch_ptr - kbd_buffer);
10064 ASET (kbd_buffer_gcpro, idx, Qnil);
10065 ASET (kbd_buffer_gcpro, idx + 1, Qnil);
10068 input_pending = 0;
10069 #endif
10070 #endif /* BSD_SYSTEM and not BSD4_1 */
10073 void
10074 set_waiting_for_input (time_to_clear)
10075 EMACS_TIME *time_to_clear;
10077 input_available_clear_time = time_to_clear;
10079 /* Tell interrupt_signal to throw back to read_char, */
10080 waiting_for_input = 1;
10082 /* If interrupt_signal was called before and buffered a C-g,
10083 make it run again now, to avoid timing error. */
10084 if (!NILP (Vquit_flag))
10085 quit_throw_to_read_char ();
10088 void
10089 clear_waiting_for_input ()
10091 /* Tell interrupt_signal not to throw back to read_char, */
10092 waiting_for_input = 0;
10093 input_available_clear_time = 0;
10096 /* This routine is called at interrupt level in response to C-g.
10098 If interrupt_input, this is the handler for SIGINT. Otherwise, it
10099 is called from kbd_buffer_store_event, in handling SIGIO or
10100 SIGTINT.
10102 If `waiting_for_input' is non zero, then unless `echoing' is
10103 nonzero, immediately throw back to read_char.
10105 Otherwise it sets the Lisp variable quit-flag not-nil. This causes
10106 eval to throw, when it gets a chance. If quit-flag is already
10107 non-nil, it stops the job right away. */
10109 static SIGTYPE
10110 interrupt_signal (signalnum) /* If we don't have an argument, */
10111 int signalnum; /* some compilers complain in signal calls. */
10113 char c;
10114 /* Must preserve main program's value of errno. */
10115 int old_errno = errno;
10116 struct frame *sf = SELECTED_FRAME ();
10118 #if defined (USG) && !defined (POSIX_SIGNALS)
10119 if (!read_socket_hook && NILP (Vwindow_system))
10121 /* USG systems forget handlers when they are used;
10122 must reestablish each time */
10123 signal (SIGINT, interrupt_signal);
10124 signal (SIGQUIT, interrupt_signal);
10126 #endif /* USG */
10128 cancel_echoing ();
10130 if (!NILP (Vquit_flag)
10131 && (FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf)))
10133 /* If SIGINT isn't blocked, don't let us be interrupted by
10134 another SIGINT, it might be harmful due to non-reentrancy
10135 in I/O functions. */
10136 sigblock (sigmask (SIGINT));
10138 fflush (stdout);
10139 reset_sys_modes ();
10141 #ifdef SIGTSTP /* Support possible in later USG versions */
10143 * On systems which can suspend the current process and return to the original
10144 * shell, this command causes the user to end up back at the shell.
10145 * The "Auto-save" and "Abort" questions are not asked until
10146 * the user elects to return to emacs, at which point he can save the current
10147 * job and either dump core or continue.
10149 sys_suspend ();
10150 #else
10151 #ifdef VMS
10152 if (sys_suspend () == -1)
10154 printf ("Not running as a subprocess;\n");
10155 printf ("you can continue or abort.\n");
10157 #else /* not VMS */
10158 /* Perhaps should really fork an inferior shell?
10159 But that would not provide any way to get back
10160 to the original shell, ever. */
10161 printf ("No support for stopping a process on this operating system;\n");
10162 printf ("you can continue or abort.\n");
10163 #endif /* not VMS */
10164 #endif /* not SIGTSTP */
10165 #ifdef MSDOS
10166 /* We must remain inside the screen area when the internal terminal
10167 is used. Note that [Enter] is not echoed by dos. */
10168 cursor_to (0, 0);
10169 #endif
10170 /* It doesn't work to autosave while GC is in progress;
10171 the code used for auto-saving doesn't cope with the mark bit. */
10172 if (!gc_in_progress)
10174 printf ("Auto-save? (y or n) ");
10175 fflush (stdout);
10176 if (((c = getchar ()) & ~040) == 'Y')
10178 Fdo_auto_save (Qt, Qnil);
10179 #ifdef MSDOS
10180 printf ("\r\nAuto-save done");
10181 #else /* not MSDOS */
10182 printf ("Auto-save done\n");
10183 #endif /* not MSDOS */
10185 while (c != '\n') c = getchar ();
10187 else
10189 /* During GC, it must be safe to reenable quitting again. */
10190 Vinhibit_quit = Qnil;
10191 #ifdef MSDOS
10192 printf ("\r\n");
10193 #endif /* not MSDOS */
10194 printf ("Garbage collection in progress; cannot auto-save now\r\n");
10195 printf ("but will instead do a real quit after garbage collection ends\r\n");
10196 fflush (stdout);
10199 #ifdef MSDOS
10200 printf ("\r\nAbort? (y or n) ");
10201 #else /* not MSDOS */
10202 #ifdef VMS
10203 printf ("Abort (and enter debugger)? (y or n) ");
10204 #else /* not VMS */
10205 printf ("Abort (and dump core)? (y or n) ");
10206 #endif /* not VMS */
10207 #endif /* not MSDOS */
10208 fflush (stdout);
10209 if (((c = getchar ()) & ~040) == 'Y')
10210 abort ();
10211 while (c != '\n') c = getchar ();
10212 #ifdef MSDOS
10213 printf ("\r\nContinuing...\r\n");
10214 #else /* not MSDOS */
10215 printf ("Continuing...\n");
10216 #endif /* not MSDOS */
10217 fflush (stdout);
10218 init_sys_modes ();
10219 sigfree ();
10221 else
10223 /* If executing a function that wants to be interrupted out of
10224 and the user has not deferred quitting by binding `inhibit-quit'
10225 then quit right away. */
10226 if (immediate_quit && NILP (Vinhibit_quit))
10228 struct gl_state_s saved;
10229 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
10231 immediate_quit = 0;
10232 sigfree ();
10233 saved = gl_state;
10234 GCPRO4 (saved.object, saved.global_code,
10235 saved.current_syntax_table, saved.old_prop);
10236 Fsignal (Qquit, Qnil);
10237 gl_state = saved;
10238 UNGCPRO;
10240 else
10241 /* Else request quit when it's safe */
10242 Vquit_flag = Qt;
10245 if (waiting_for_input && !echoing)
10246 quit_throw_to_read_char ();
10248 errno = old_errno;
10251 /* Handle a C-g by making read_char return C-g. */
10253 void
10254 quit_throw_to_read_char ()
10256 sigfree ();
10257 /* Prevent another signal from doing this before we finish. */
10258 clear_waiting_for_input ();
10259 input_pending = 0;
10261 Vunread_command_events = Qnil;
10262 unread_command_char = -1;
10264 #if 0 /* Currently, sit_for is called from read_char without turning
10265 off polling. And that can call set_waiting_for_input.
10266 It seems to be harmless. */
10267 #ifdef POLL_FOR_INPUT
10268 /* May be > 1 if in recursive minibuffer. */
10269 if (poll_suppress_count == 0)
10270 abort ();
10271 #endif
10272 #endif
10273 if (FRAMEP (internal_last_event_frame)
10274 && !EQ (internal_last_event_frame, selected_frame))
10275 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
10276 0, 0);
10278 _longjmp (getcjmp, 1);
10281 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
10282 doc: /* Set mode of reading keyboard input.
10283 First arg INTERRUPT non-nil means use input interrupts;
10284 nil means use CBREAK mode.
10285 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal
10286 (no effect except in CBREAK mode).
10287 Third arg META t means accept 8-bit input (for a Meta key).
10288 META nil means ignore the top bit, on the assumption it is parity.
10289 Otherwise, accept 8-bit input and don't use the top bit for Meta.
10290 Optional fourth arg QUIT if non-nil specifies character to use for quitting.
10291 See also `current-input-mode'. */)
10292 (interrupt, flow, meta, quit)
10293 Lisp_Object interrupt, flow, meta, quit;
10295 if (!NILP (quit)
10296 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
10297 error ("set-input-mode: QUIT must be an ASCII character");
10299 #ifdef POLL_FOR_INPUT
10300 stop_polling ();
10301 #endif
10303 #ifndef DOS_NT
10304 /* this causes startup screen to be restored and messes with the mouse */
10305 reset_sys_modes ();
10306 #endif
10308 #ifdef SIGIO
10309 /* Note SIGIO has been undef'd if FIONREAD is missing. */
10310 if (read_socket_hook)
10312 /* When using X, don't give the user a real choice,
10313 because we haven't implemented the mechanisms to support it. */
10314 #ifdef NO_SOCK_SIGIO
10315 interrupt_input = 0;
10316 #else /* not NO_SOCK_SIGIO */
10317 interrupt_input = 1;
10318 #endif /* NO_SOCK_SIGIO */
10320 else
10321 interrupt_input = !NILP (interrupt);
10322 #else /* not SIGIO */
10323 interrupt_input = 0;
10324 #endif /* not SIGIO */
10326 /* Our VMS input only works by interrupts, as of now. */
10327 #ifdef VMS
10328 interrupt_input = 1;
10329 #endif
10331 flow_control = !NILP (flow);
10332 if (NILP (meta))
10333 meta_key = 0;
10334 else if (EQ (meta, Qt))
10335 meta_key = 1;
10336 else
10337 meta_key = 2;
10338 if (!NILP (quit))
10339 /* Don't let this value be out of range. */
10340 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
10342 #ifndef DOS_NT
10343 init_sys_modes ();
10344 #endif
10346 #ifdef POLL_FOR_INPUT
10347 poll_suppress_count = 1;
10348 start_polling ();
10349 #endif
10350 return Qnil;
10353 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
10354 doc: /* Return information about the way Emacs currently reads keyboard input.
10355 The value is a list of the form (INTERRUPT FLOW META QUIT), where
10356 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if
10357 nil, Emacs is using CBREAK mode.
10358 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the
10359 terminal; this does not apply if Emacs uses interrupt-driven input.
10360 META is t if accepting 8-bit input with 8th bit as Meta flag.
10361 META nil means ignoring the top bit, on the assumption it is parity.
10362 META is neither t nor nil if accepting 8-bit input and using
10363 all 8 bits as the character code.
10364 QUIT is the character Emacs currently uses to quit.
10365 The elements of this list correspond to the arguments of
10366 `set-input-mode'. */)
10369 Lisp_Object val[4];
10371 val[0] = interrupt_input ? Qt : Qnil;
10372 val[1] = flow_control ? Qt : Qnil;
10373 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
10374 XSETFASTINT (val[3], quit_char);
10376 return Flist (sizeof (val) / sizeof (val[0]), val);
10381 * Set up a new kboard object with reasonable initial values.
10383 void
10384 init_kboard (kb)
10385 KBOARD *kb;
10387 kb->Voverriding_terminal_local_map = Qnil;
10388 kb->Vlast_command = Qnil;
10389 kb->Vreal_last_command = Qnil;
10390 kb->Vprefix_arg = Qnil;
10391 kb->Vlast_prefix_arg = Qnil;
10392 kb->kbd_queue = Qnil;
10393 kb->kbd_queue_has_data = 0;
10394 kb->immediate_echo = 0;
10395 kb->echo_string = Qnil;
10396 kb->echo_after_prompt = -1;
10397 kb->kbd_macro_buffer = 0;
10398 kb->kbd_macro_bufsize = 0;
10399 kb->defining_kbd_macro = Qnil;
10400 kb->Vlast_kbd_macro = Qnil;
10401 kb->reference_count = 0;
10402 kb->Vsystem_key_alist = Qnil;
10403 kb->system_key_syms = Qnil;
10404 kb->Vdefault_minibuffer_frame = Qnil;
10408 * Destroy the contents of a kboard object, but not the object itself.
10409 * We use this just before deleting it, or if we're going to initialize
10410 * it a second time.
10412 static void
10413 wipe_kboard (kb)
10414 KBOARD *kb;
10416 if (kb->kbd_macro_buffer)
10417 xfree (kb->kbd_macro_buffer);
10420 #ifdef MULTI_KBOARD
10422 /* Free KB and memory referenced from it. */
10424 void
10425 delete_kboard (kb)
10426 KBOARD *kb;
10428 KBOARD **kbp;
10430 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
10431 if (*kbp == NULL)
10432 abort ();
10433 *kbp = kb->next_kboard;
10435 /* Prevent a dangling reference to KB. */
10436 if (kb == current_kboard
10437 && FRAMEP (selected_frame)
10438 && FRAME_LIVE_P (XFRAME (selected_frame)))
10440 current_kboard = XFRAME (selected_frame)->kboard;
10441 if (current_kboard == kb)
10442 abort ();
10445 wipe_kboard (kb);
10446 xfree (kb);
10449 #endif /* MULTI_KBOARD */
10451 void
10452 init_keyboard ()
10454 /* This is correct before outermost invocation of the editor loop */
10455 command_loop_level = -1;
10456 immediate_quit = 0;
10457 quit_char = Ctl ('g');
10458 Vunread_command_events = Qnil;
10459 unread_command_char = -1;
10460 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
10461 total_keys = 0;
10462 recent_keys_index = 0;
10463 kbd_fetch_ptr = kbd_buffer;
10464 kbd_store_ptr = kbd_buffer;
10465 kbd_buffer_gcpro = Fmake_vector (make_number (2 * KBD_BUFFER_SIZE), Qnil);
10466 #ifdef HAVE_MOUSE
10467 do_mouse_tracking = Qnil;
10468 #endif
10469 input_pending = 0;
10471 /* This means that command_loop_1 won't try to select anything the first
10472 time through. */
10473 internal_last_event_frame = Qnil;
10474 Vlast_event_frame = internal_last_event_frame;
10476 #ifdef MULTI_KBOARD
10477 current_kboard = initial_kboard;
10478 #endif
10479 wipe_kboard (current_kboard);
10480 init_kboard (current_kboard);
10482 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
10484 signal (SIGINT, interrupt_signal);
10485 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
10486 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
10487 SIGQUIT and we can't tell which one it will give us. */
10488 signal (SIGQUIT, interrupt_signal);
10489 #endif /* HAVE_TERMIO */
10491 /* Note SIGIO has been undef'd if FIONREAD is missing. */
10492 #ifdef SIGIO
10493 if (!noninteractive)
10494 signal (SIGIO, input_available_signal);
10495 #endif /* SIGIO */
10497 /* Use interrupt input by default, if it works and noninterrupt input
10498 has deficiencies. */
10500 #ifdef INTERRUPT_INPUT
10501 interrupt_input = 1;
10502 #else
10503 interrupt_input = 0;
10504 #endif
10506 /* Our VMS input only works by interrupts, as of now. */
10507 #ifdef VMS
10508 interrupt_input = 1;
10509 #endif
10511 sigfree ();
10512 dribble = 0;
10514 if (keyboard_init_hook)
10515 (*keyboard_init_hook) ();
10517 #ifdef POLL_FOR_INPUT
10518 poll_suppress_count = 1;
10519 start_polling ();
10520 #endif
10523 /* This type's only use is in syms_of_keyboard, to initialize the
10524 event header symbols and put properties on them. */
10525 struct event_head {
10526 Lisp_Object *var;
10527 char *name;
10528 Lisp_Object *kind;
10531 struct event_head head_table[] = {
10532 {&Qmouse_movement, "mouse-movement", &Qmouse_movement},
10533 {&Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement},
10534 {&Qswitch_frame, "switch-frame", &Qswitch_frame},
10535 {&Qdelete_frame, "delete-frame", &Qdelete_frame},
10536 {&Qiconify_frame, "iconify-frame", &Qiconify_frame},
10537 {&Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible},
10538 {&Qselect_window, "select-window", &Qselect_window}
10541 void
10542 syms_of_keyboard ()
10544 Vpre_help_message = Qnil;
10545 staticpro (&Vpre_help_message);
10547 Vlispy_mouse_stem = build_string ("mouse");
10548 staticpro (&Vlispy_mouse_stem);
10550 /* Tool-bars. */
10551 QCimage = intern (":image");
10552 staticpro (&QCimage);
10554 staticpro (&Qhelp_echo);
10555 Qhelp_echo = intern ("help-echo");
10557 staticpro (&item_properties);
10558 item_properties = Qnil;
10560 staticpro (&tool_bar_item_properties);
10561 tool_bar_item_properties = Qnil;
10562 staticpro (&tool_bar_items_vector);
10563 tool_bar_items_vector = Qnil;
10565 staticpro (&real_this_command);
10566 real_this_command = Qnil;
10568 Qtimer_event_handler = intern ("timer-event-handler");
10569 staticpro (&Qtimer_event_handler);
10571 Qdisabled_command_hook = intern ("disabled-command-hook");
10572 staticpro (&Qdisabled_command_hook);
10574 Qself_insert_command = intern ("self-insert-command");
10575 staticpro (&Qself_insert_command);
10577 Qforward_char = intern ("forward-char");
10578 staticpro (&Qforward_char);
10580 Qbackward_char = intern ("backward-char");
10581 staticpro (&Qbackward_char);
10583 Qdisabled = intern ("disabled");
10584 staticpro (&Qdisabled);
10586 Qundefined = intern ("undefined");
10587 staticpro (&Qundefined);
10589 Qpre_command_hook = intern ("pre-command-hook");
10590 staticpro (&Qpre_command_hook);
10592 Qpost_command_hook = intern ("post-command-hook");
10593 staticpro (&Qpost_command_hook);
10595 Qpost_command_idle_hook = intern ("post-command-idle-hook");
10596 staticpro (&Qpost_command_idle_hook);
10598 Qdeferred_action_function = intern ("deferred-action-function");
10599 staticpro (&Qdeferred_action_function);
10601 Qcommand_hook_internal = intern ("command-hook-internal");
10602 staticpro (&Qcommand_hook_internal);
10604 Qfunction_key = intern ("function-key");
10605 staticpro (&Qfunction_key);
10606 Qmouse_click = intern ("mouse-click");
10607 staticpro (&Qmouse_click);
10608 #if defined(WINDOWSNT) || defined(MAC_OSX)
10609 Qmouse_wheel = intern ("mouse-wheel");
10610 staticpro (&Qmouse_wheel);
10611 #endif
10612 #ifdef WINDOWSNT
10613 Qlanguage_change = intern ("language-change");
10614 staticpro (&Qlanguage_change);
10615 #endif
10616 Qdrag_n_drop = intern ("drag-n-drop");
10617 staticpro (&Qdrag_n_drop);
10619 Qsave_session = intern ("save-session");
10620 staticpro(&Qsave_session);
10622 Qusr1_signal = intern ("usr1-signal");
10623 staticpro (&Qusr1_signal);
10624 Qusr2_signal = intern ("usr2-signal");
10625 staticpro (&Qusr2_signal);
10627 Qmenu_enable = intern ("menu-enable");
10628 staticpro (&Qmenu_enable);
10629 Qmenu_alias = intern ("menu-alias");
10630 staticpro (&Qmenu_alias);
10631 QCenable = intern (":enable");
10632 staticpro (&QCenable);
10633 QCvisible = intern (":visible");
10634 staticpro (&QCvisible);
10635 QChelp = intern (":help");
10636 staticpro (&QChelp);
10637 QCfilter = intern (":filter");
10638 staticpro (&QCfilter);
10639 QCbutton = intern (":button");
10640 staticpro (&QCbutton);
10641 QCkeys = intern (":keys");
10642 staticpro (&QCkeys);
10643 QCkey_sequence = intern (":key-sequence");
10644 staticpro (&QCkey_sequence);
10645 QCtoggle = intern (":toggle");
10646 staticpro (&QCtoggle);
10647 QCradio = intern (":radio");
10648 staticpro (&QCradio);
10650 Qmode_line = intern ("mode-line");
10651 staticpro (&Qmode_line);
10652 Qvertical_line = intern ("vertical-line");
10653 staticpro (&Qvertical_line);
10654 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
10655 staticpro (&Qvertical_scroll_bar);
10656 Qmenu_bar = intern ("menu-bar");
10657 staticpro (&Qmenu_bar);
10659 Qabove_handle = intern ("above-handle");
10660 staticpro (&Qabove_handle);
10661 Qhandle = intern ("handle");
10662 staticpro (&Qhandle);
10663 Qbelow_handle = intern ("below-handle");
10664 staticpro (&Qbelow_handle);
10665 Qup = intern ("up");
10666 staticpro (&Qup);
10667 Qdown = intern ("down");
10668 staticpro (&Qdown);
10669 Qtop = intern ("top");
10670 staticpro (&Qtop);
10671 Qbottom = intern ("bottom");
10672 staticpro (&Qbottom);
10673 Qend_scroll = intern ("end-scroll");
10674 staticpro (&Qend_scroll);
10675 Qratio = intern ("ratio");
10676 staticpro (&Qratio);
10678 Qevent_kind = intern ("event-kind");
10679 staticpro (&Qevent_kind);
10680 Qevent_symbol_elements = intern ("event-symbol-elements");
10681 staticpro (&Qevent_symbol_elements);
10682 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
10683 staticpro (&Qevent_symbol_element_mask);
10684 Qmodifier_cache = intern ("modifier-cache");
10685 staticpro (&Qmodifier_cache);
10687 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
10688 staticpro (&Qrecompute_lucid_menubar);
10689 Qactivate_menubar_hook = intern ("activate-menubar-hook");
10690 staticpro (&Qactivate_menubar_hook);
10692 Qpolling_period = intern ("polling-period");
10693 staticpro (&Qpolling_period);
10695 Qinput_method_function = intern ("input-method-function");
10696 staticpro (&Qinput_method_function);
10698 Qinput_method_exit_on_first_char = intern ("input-method-exit-on-first-char");
10699 staticpro (&Qinput_method_exit_on_first_char);
10700 Qinput_method_use_echo_area = intern ("input-method-use-echo-area");
10701 staticpro (&Qinput_method_use_echo_area);
10703 Fset (Qinput_method_exit_on_first_char, Qnil);
10704 Fset (Qinput_method_use_echo_area, Qnil);
10706 last_point_position_buffer = Qnil;
10709 struct event_head *p;
10711 for (p = head_table;
10712 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
10713 p++)
10715 *p->var = intern (p->name);
10716 staticpro (p->var);
10717 Fput (*p->var, Qevent_kind, *p->kind);
10718 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
10722 button_down_location = Fmake_vector (make_number (1), Qnil);
10723 staticpro (&button_down_location);
10724 mouse_syms = Fmake_vector (make_number (1), Qnil);
10725 staticpro (&mouse_syms);
10728 int i;
10729 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
10731 modifier_symbols = Fmake_vector (make_number (len), Qnil);
10732 for (i = 0; i < len; i++)
10733 if (modifier_names[i])
10734 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
10735 staticpro (&modifier_symbols);
10738 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
10739 staticpro (&recent_keys);
10741 this_command_keys = Fmake_vector (make_number (40), Qnil);
10742 staticpro (&this_command_keys);
10744 raw_keybuf = Fmake_vector (make_number (30), Qnil);
10745 staticpro (&raw_keybuf);
10747 Qextended_command_history = intern ("extended-command-history");
10748 Fset (Qextended_command_history, Qnil);
10749 staticpro (&Qextended_command_history);
10751 kbd_buffer_gcpro = Fmake_vector (make_number (2 * KBD_BUFFER_SIZE), Qnil);
10752 staticpro (&kbd_buffer_gcpro);
10754 accent_key_syms = Qnil;
10755 staticpro (&accent_key_syms);
10757 func_key_syms = Qnil;
10758 staticpro (&func_key_syms);
10760 #if defined(WINDOWSNT) || defined(MAC_OSX)
10761 mouse_wheel_syms = Qnil;
10762 staticpro (&mouse_wheel_syms);
10763 drag_n_drop_syms = Qnil;
10764 staticpro (&drag_n_drop_syms);
10765 #endif
10767 unread_switch_frame = Qnil;
10768 staticpro (&unread_switch_frame);
10770 internal_last_event_frame = Qnil;
10771 staticpro (&internal_last_event_frame);
10773 read_key_sequence_cmd = Qnil;
10774 staticpro (&read_key_sequence_cmd);
10776 menu_bar_one_keymap_changed_items = Qnil;
10777 staticpro (&menu_bar_one_keymap_changed_items);
10779 defsubr (&Sevent_convert_list);
10780 defsubr (&Sread_key_sequence);
10781 defsubr (&Sread_key_sequence_vector);
10782 defsubr (&Srecursive_edit);
10783 #ifdef HAVE_MOUSE
10784 defsubr (&Strack_mouse);
10785 #endif
10786 defsubr (&Sinput_pending_p);
10787 defsubr (&Scommand_execute);
10788 defsubr (&Srecent_keys);
10789 defsubr (&Sthis_command_keys);
10790 defsubr (&Sthis_command_keys_vector);
10791 defsubr (&Sthis_single_command_keys);
10792 defsubr (&Sthis_single_command_raw_keys);
10793 defsubr (&Sreset_this_command_lengths);
10794 defsubr (&Sclear_this_command_keys);
10795 defsubr (&Ssuspend_emacs);
10796 defsubr (&Sabort_recursive_edit);
10797 defsubr (&Sexit_recursive_edit);
10798 defsubr (&Srecursion_depth);
10799 defsubr (&Stop_level);
10800 defsubr (&Sdiscard_input);
10801 defsubr (&Sopen_dribble_file);
10802 defsubr (&Sset_input_mode);
10803 defsubr (&Scurrent_input_mode);
10804 defsubr (&Sexecute_extended_command);
10806 DEFVAR_LISP ("last-command-char", &last_command_char,
10807 doc: /* Last input event that was part of a command. */);
10809 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
10810 doc: /* Last input event that was part of a command. */);
10812 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
10813 doc: /* Last input event in a command, except for mouse menu events.
10814 Mouse menus give back keys that don't look like mouse events;
10815 this variable holds the actual mouse event that led to the menu,
10816 so that you can determine whether the command was run by mouse or not. */);
10818 DEFVAR_LISP ("last-input-char", &last_input_char,
10819 doc: /* Last input event. */);
10821 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
10822 doc: /* Last input event. */);
10824 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
10825 doc: /* List of events to be read as the command input.
10826 These events are processed first, before actual keyboard input. */);
10827 Vunread_command_events = Qnil;
10829 DEFVAR_INT ("unread-command-char", &unread_command_char,
10830 doc: /* If not -1, an object to be read as next command input event. */);
10832 DEFVAR_LISP ("unread-post-input-method-events", &Vunread_post_input_method_events,
10833 doc: /* List of events to be processed as input by input methods.
10834 These events are processed after `unread-command-events', but
10835 before actual keyboard input. */);
10836 Vunread_post_input_method_events = Qnil;
10838 DEFVAR_LISP ("unread-input-method-events", &Vunread_input_method_events,
10839 doc: /* List of events to be processed as input by input methods.
10840 These events are processed after `unread-command-events', but
10841 before actual keyboard input. */);
10842 Vunread_input_method_events = Qnil;
10844 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
10845 doc: /* Meta-prefix character code.
10846 Meta-foo as command input turns into this character followed by foo. */);
10847 XSETINT (meta_prefix_char, 033);
10849 DEFVAR_KBOARD ("last-command", Vlast_command,
10850 doc: /* The last command executed.
10851 Normally a symbol with a function definition, but can be whatever was found
10852 in the keymap, or whatever the variable `this-command' was set to by that
10853 command.
10855 The value `mode-exit' is special; it means that the previous command
10856 read an event that told it to exit, and it did so and unread that event.
10857 In other words, the present command is the event that made the previous
10858 command exit.
10860 The value `kill-region' is special; it means that the previous command
10861 was a kill command. */);
10863 DEFVAR_KBOARD ("real-last-command", Vreal_last_command,
10864 doc: /* Same as `last-command', but never altered by Lisp code. */);
10866 DEFVAR_LISP ("this-command", &Vthis_command,
10867 doc: /* The command now being executed.
10868 The command can set this variable; whatever is put here
10869 will be in `last-command' during the following command. */);
10870 Vthis_command = Qnil;
10872 DEFVAR_LISP ("this-original-command", &Vthis_original_command,
10873 doc: /* If non-nil, the original command bound to the current key sequence.
10874 The value of `this-command' is the result of looking up the original
10875 command in the active keymaps. */);
10876 Vthis_original_command = Qnil;
10878 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
10879 doc: /* *Number of input events between auto-saves.
10880 Zero means disable autosaving due to number of characters typed. */);
10881 auto_save_interval = 300;
10883 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
10884 doc: /* *Number of seconds idle time before auto-save.
10885 Zero or nil means disable auto-saving due to idleness.
10886 After auto-saving due to this many seconds of idle time,
10887 Emacs also does a garbage collection if that seems to be warranted. */);
10888 XSETFASTINT (Vauto_save_timeout, 30);
10890 DEFVAR_LISP ("echo-keystrokes", &Vecho_keystrokes,
10891 doc: /* *Nonzero means echo unfinished commands after this many seconds of pause.
10892 The value may be integer or floating point. */);
10893 Vecho_keystrokes = make_number (1);
10895 DEFVAR_INT ("polling-period", &polling_period,
10896 doc: /* *Interval between polling for input during Lisp execution.
10897 The reason for polling is to make C-g work to stop a running program.
10898 Polling is needed only when using X windows and SIGIO does not work.
10899 Polling is automatically disabled in all other cases. */);
10900 polling_period = 2;
10902 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
10903 doc: /* *Maximum time between mouse clicks to make a double-click.
10904 Measured in milliseconds. nil means disable double-click recognition;
10905 t means double-clicks have no time limit and are detected
10906 by position only. */);
10907 Vdouble_click_time = make_number (500);
10909 DEFVAR_INT ("double-click-fuzz", &double_click_fuzz,
10910 doc: /* *Maximum mouse movement between clicks to make a double-click.
10911 On window-system frames, value is the number of pixels the mouse may have
10912 moved horizontally or vertically between two clicks to make a double-click.
10913 On non window-system frames, value is interpreted in units of 1/8 characters
10914 instead of pixels.
10916 This variable is also the threshold for motion of the mouse
10917 to count as a drag. */);
10918 double_click_fuzz = 3;
10920 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
10921 doc: /* *Non-nil means inhibit local map menu bar menus. */);
10922 inhibit_local_menu_bar_menus = 0;
10924 DEFVAR_INT ("num-input-keys", &num_input_keys,
10925 doc: /* Number of complete key sequences read as input so far.
10926 This includes key sequences read from keyboard macros.
10927 The number is effectively the number of interactive command invocations. */);
10928 num_input_keys = 0;
10930 DEFVAR_INT ("num-nonmacro-input-events", &num_nonmacro_input_events,
10931 doc: /* Number of input events read from the keyboard so far.
10932 This does not include events generated by keyboard macros. */);
10933 num_nonmacro_input_events = 0;
10935 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
10936 doc: /* The frame in which the most recently read event occurred.
10937 If the last event came from a keyboard macro, this is set to `macro'. */);
10938 Vlast_event_frame = Qnil;
10940 /* This variable is set up in sysdep.c. */
10941 DEFVAR_LISP ("tty-erase-char", &Vtty_erase_char,
10942 doc: /* The ERASE character as set by the user with stty. */);
10944 DEFVAR_LISP ("help-char", &Vhelp_char,
10945 doc: /* Character to recognize as meaning Help.
10946 When it is read, do `(eval help-form)', and display result if it's a string.
10947 If the value of `help-form' is nil, this char can be read normally. */);
10948 XSETINT (Vhelp_char, Ctl ('H'));
10950 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
10951 doc: /* List of input events to recognize as meaning Help.
10952 These work just like the value of `help-char' (see that). */);
10953 Vhelp_event_list = Qnil;
10955 DEFVAR_LISP ("help-form", &Vhelp_form,
10956 doc: /* Form to execute when character `help-char' is read.
10957 If the form returns a string, that string is displayed.
10958 If `help-form' is nil, the help char is not recognized. */);
10959 Vhelp_form = Qnil;
10961 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
10962 doc: /* Command to run when `help-char' character follows a prefix key.
10963 This command is used only when there is no actual binding
10964 for that character after that prefix key. */);
10965 Vprefix_help_command = Qnil;
10967 DEFVAR_LISP ("top-level", &Vtop_level,
10968 doc: /* Form to evaluate when Emacs starts up.
10969 Useful to set before you dump a modified Emacs. */);
10970 Vtop_level = Qnil;
10972 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
10973 doc: /* Translate table for keyboard input, or nil.
10974 Each character is looked up in this string and the contents used instead.
10975 The value may be a string, a vector, or a char-table.
10976 If it is a string or vector of length N,
10977 character codes N and up are untranslated.
10978 In a vector or a char-table, an element which is nil means "no translation".
10980 This is applied to the characters supplied to input methods, not their
10981 output. See also `translation-table-for-input'. */);
10982 Vkeyboard_translate_table = Qnil;
10984 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
10985 doc: /* Non-nil means to always spawn a subshell instead of suspending.
10986 \(Even if the operating system has support for stopping a process.\) */);
10987 cannot_suspend = 0;
10989 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
10990 doc: /* Non-nil means prompt with menus when appropriate.
10991 This is done when reading from a keymap that has a prompt string,
10992 for elements that have prompt strings.
10993 The menu is displayed on the screen
10994 if X menus were enabled at configuration
10995 time and the previous event was a mouse click prefix key.
10996 Otherwise, menu prompting uses the echo area. */);
10997 menu_prompting = 1;
10999 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
11000 doc: /* Character to see next line of menu prompt.
11001 Type this character while in a menu prompt to rotate around the lines of it. */);
11002 XSETINT (menu_prompt_more_char, ' ');
11004 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
11005 doc: /* A mask of additional modifier keys to use with every keyboard character.
11006 Emacs applies the modifiers of the character stored here to each keyboard
11007 character it reads. For example, after evaluating the expression
11008 (setq extra-keyboard-modifiers ?\\C-x)
11009 all input characters will have the control modifier applied to them.
11011 Note that the character ?\\C-@, equivalent to the integer zero, does
11012 not count as a control character; rather, it counts as a character
11013 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero
11014 cancels any modification. */);
11015 extra_keyboard_modifiers = 0;
11017 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
11018 doc: /* If an editing command sets this to t, deactivate the mark afterward.
11019 The command loop sets this to nil before each command,
11020 and tests the value when the command returns.
11021 Buffer modification stores t in this variable. */);
11022 Vdeactivate_mark = Qnil;
11024 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
11025 doc: /* Temporary storage of pre-command-hook or post-command-hook. */);
11026 Vcommand_hook_internal = Qnil;
11028 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
11029 doc: /* Normal hook run before each command is executed.
11030 If an unhandled error happens in running this hook,
11031 the hook value is set to nil, since otherwise the error
11032 might happen repeatedly and make Emacs nonfunctional. */);
11033 Vpre_command_hook = Qnil;
11035 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
11036 doc: /* Normal hook run after each command is executed.
11037 If an unhandled error happens in running this hook,
11038 the hook value is set to nil, since otherwise the error
11039 might happen repeatedly and make Emacs nonfunctional. */);
11040 Vpost_command_hook = Qnil;
11042 DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
11043 doc: /* Normal hook run after each command is executed, if idle.
11044 Errors running the hook are caught and ignored. */);
11045 Vpost_command_idle_hook = Qnil;
11047 DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
11048 doc: /* Delay time before running `post-command-idle-hook'.
11049 This is measured in microseconds. */);
11050 post_command_idle_delay = 100000;
11052 #if 0
11053 DEFVAR_LISP ("echo-area-clear-hook", ...,
11054 doc: /* Normal hook run when clearing the echo area. */);
11055 #endif
11056 Qecho_area_clear_hook = intern ("echo-area-clear-hook");
11057 SET_SYMBOL_VALUE (Qecho_area_clear_hook, Qnil);
11059 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
11060 doc: /* Non-nil means menu bar, specified Lucid style, needs to be recomputed. */);
11061 Vlucid_menu_bar_dirty_flag = Qnil;
11063 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
11064 doc: /* List of menu bar items to move to the end of the menu bar.
11065 The elements of the list are event types that may have menu bar bindings. */);
11066 Vmenu_bar_final_items = Qnil;
11068 DEFVAR_KBOARD ("overriding-terminal-local-map",
11069 Voverriding_terminal_local_map,
11070 doc: /* Per-terminal keymap that overrides all other local keymaps.
11071 If this variable is non-nil, it is used as a keymap instead of the
11072 buffer's local map, and the minor mode keymaps and text property keymaps.
11073 This variable is intended to let commands such as `universal-argument'
11074 set up a different keymap for reading the next command. */);
11076 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
11077 doc: /* Keymap that overrides all other local keymaps.
11078 If this variable is non-nil, it is used as a keymap instead of the
11079 buffer's local map, and the minor mode keymaps and text property keymaps. */);
11080 Voverriding_local_map = Qnil;
11082 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
11083 doc: /* Non-nil means `overriding-local-map' applies to the menu bar.
11084 Otherwise, the menu bar continues to reflect the buffer's local map
11085 and the minor mode maps regardless of `overriding-local-map'. */);
11086 Voverriding_local_map_menu_flag = Qnil;
11088 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
11089 doc: /* Keymap defining bindings for special events to execute at low level. */);
11090 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
11092 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
11093 doc: /* *Non-nil means generate motion events for mouse motion. */);
11095 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
11096 doc: /* Alist of system-specific X windows key symbols.
11097 Each element should have the form (N . SYMBOL) where N is the
11098 numeric keysym code (sans the \"system-specific\" bit 1<<28)
11099 and SYMBOL is its name. */);
11101 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
11102 doc: /* List of deferred actions to be performed at a later time.
11103 The precise format isn't relevant here; we just check whether it is nil. */);
11104 Vdeferred_action_list = Qnil;
11106 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
11107 doc: /* Function to call to handle deferred actions, after each command.
11108 This function is called with no arguments after each command
11109 whenever `deferred-action-list' is non-nil. */);
11110 Vdeferred_action_function = Qnil;
11112 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
11113 doc: /* *Non-nil means show the equivalent key-binding when M-x command has one.
11114 The value can be a length of time to show the message for.
11115 If the value is non-nil and not a number, we wait 2 seconds. */);
11116 Vsuggest_key_bindings = Qt;
11118 DEFVAR_LISP ("timer-list", &Vtimer_list,
11119 doc: /* List of active absolute time timers in order of increasing time. */);
11120 Vtimer_list = Qnil;
11122 DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
11123 doc: /* List of active idle-time timers in order of increasing time. */);
11124 Vtimer_idle_list = Qnil;
11126 DEFVAR_LISP ("input-method-function", &Vinput_method_function,
11127 doc: /* If non-nil, the function that implements the current input method.
11128 It's called with one argument, a printing character that was just read.
11129 \(That means a character with code 040...0176.)
11130 Typically this function uses `read-event' to read additional events.
11131 When it does so, it should first bind `input-method-function' to nil
11132 so it will not be called recursively.
11134 The function should return a list of zero or more events
11135 to be used as input. If it wants to put back some events
11136 to be reconsidered, separately, by the input method,
11137 it can add them to the beginning of `unread-command-events'.
11139 The input method function can find in `input-method-previous-method'
11140 the previous echo area message.
11142 The input method function should refer to the variables
11143 `input-method-use-echo-area' and `input-method-exit-on-first-char'
11144 for guidance on what to do. */);
11145 Vinput_method_function = Qnil;
11147 DEFVAR_LISP ("input-method-previous-message",
11148 &Vinput_method_previous_message,
11149 doc: /* When `input-method-function' is called, hold the previous echo area message.
11150 This variable exists because `read-event' clears the echo area
11151 before running the input method. It is nil if there was no message. */);
11152 Vinput_method_previous_message = Qnil;
11154 DEFVAR_LISP ("show-help-function", &Vshow_help_function,
11155 doc: /* If non-nil, the function that implements the display of help.
11156 It's called with one argument, the help string to display. */);
11157 Vshow_help_function = Qnil;
11159 DEFVAR_LISP ("disable-point-adjustment", &Vdisable_point_adjustment,
11160 doc: /* If non-nil, suppress point adjustment after executing a command.
11162 After a command is executed, if point is moved into a region that has
11163 special properties (e.g. composition, display), we adjust point to
11164 the boundary of the region. But, several special commands sets this
11165 variable to non-nil, then we suppress the point adjustment.
11167 This variable is set to nil before reading a command, and is checked
11168 just after executing the command. */);
11169 Vdisable_point_adjustment = Qnil;
11171 DEFVAR_LISP ("global-disable-point-adjustment",
11172 &Vglobal_disable_point_adjustment,
11173 doc: /* *If non-nil, always suppress point adjustment.
11175 The default value is nil, in which case, point adjustment are
11176 suppressed only after special commands that set
11177 `disable-point-adjustment' (which see) to non-nil. */);
11178 Vglobal_disable_point_adjustment = Qnil;
11180 DEFVAR_BOOL ("update-menu-bindings", &update_menu_bindings,
11181 doc: /* Non-nil means updating menu bindings is allowed.
11182 A value of nil means menu bindings should not be updated.
11183 Used during Emacs' startup. */);
11184 update_menu_bindings = 1;
11186 DEFVAR_LISP ("minibuffer-message-timeout", &Vminibuffer_message_timeout,
11187 doc: /* *How long to display an echo-area message when the minibuffer is active.
11188 If the value is not a number, such messages don't time out. */);
11189 Vminibuffer_message_timeout = make_number (2);
11192 void
11193 keys_of_keyboard ()
11195 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
11196 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
11197 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
11198 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
11199 initial_define_key (meta_map, 'x', "execute-extended-command");
11201 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
11202 "handle-delete-frame");
11203 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
11204 "ignore-event");
11205 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
11206 "ignore-event");
11207 initial_define_lispy_key (Vspecial_event_map, "select-window",
11208 "handle-select-window");
11209 initial_define_lispy_key (Vspecial_event_map, "save-session",
11210 "handle-save-session");