(easy-mmode-define-navigation):
[emacs.git] / src / keyboard.c
blob501174faaca45a098e8ccdec4e6e93f0943e1552
1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985,86,87,88,89,93,94,95,96,97,99, 2000
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 "blockinput.h"
41 #include "puresize.h"
42 #include "systime.h"
43 #include "atimer.h"
44 #include <setjmp.h>
45 #include <errno.h>
47 #ifdef MSDOS
48 #include "msdos.h"
49 #include <time.h>
50 #else /* not MSDOS */
51 #ifndef VMS
52 #include <sys/ioctl.h>
53 #endif
54 #endif /* not MSDOS */
56 #include "syssignal.h"
57 #include "systty.h"
59 #include <sys/types.h>
60 #ifdef HAVE_UNISTD_H
61 #include <unistd.h>
62 #endif
64 /* This is to get the definitions of the XK_ symbols. */
65 #ifdef HAVE_X_WINDOWS
66 #include "xterm.h"
67 #endif
69 #ifdef HAVE_NTGUI
70 #include "w32term.h"
71 #endif /* HAVE_NTGUI */
73 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
74 #include "systime.h"
76 #ifndef USE_CRT_DLL
77 extern int errno;
78 #endif
80 /* Variables for blockinput.h: */
82 /* Non-zero if interrupt input is blocked right now. */
83 int interrupt_input_blocked;
85 /* Nonzero means an input interrupt has arrived
86 during the current critical section. */
87 int interrupt_input_pending;
90 /* File descriptor to use for input. */
91 extern int input_fd;
93 #ifdef HAVE_WINDOW_SYSTEM
94 /* Make all keyboard buffers much bigger when using X windows. */
95 #ifdef macintosh
96 /* But not too big (local data > 32K error) if on macintosh */
97 #define KBD_BUFFER_SIZE 512
98 #else
99 #define KBD_BUFFER_SIZE 4096
100 #endif
101 #else /* No X-windows, character input */
102 #define KBD_BUFFER_SIZE 256
103 #endif /* No X-windows */
105 /* Following definition copied from eval.c */
107 struct backtrace
109 struct backtrace *next;
110 Lisp_Object *function;
111 Lisp_Object *args; /* Points to vector of args. */
112 int nargs; /* length of vector. If nargs is UNEVALLED,
113 args points to slot holding list of
114 unevalled args */
115 char evalargs;
118 #ifdef MULTI_KBOARD
119 KBOARD *initial_kboard;
120 KBOARD *current_kboard;
121 KBOARD *all_kboards;
122 int single_kboard;
123 #else
124 KBOARD the_only_kboard;
125 #endif
127 /* Non-nil disable property on a command means
128 do not execute it; call disabled-command-hook's value instead. */
129 Lisp_Object Qdisabled, Qdisabled_command_hook;
131 #define NUM_RECENT_KEYS (100)
132 int recent_keys_index; /* Index for storing next element into recent_keys */
133 int total_keys; /* Total number of elements stored into recent_keys */
134 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
136 /* Vector holding the key sequence that invoked the current command.
137 It is reused for each command, and it may be longer than the current
138 sequence; this_command_key_count indicates how many elements
139 actually mean something.
140 It's easier to staticpro a single Lisp_Object than an array. */
141 Lisp_Object this_command_keys;
142 int this_command_key_count;
144 /* This vector is used as a buffer to record the events that were actually read
145 by read_key_sequence. */
146 Lisp_Object raw_keybuf;
147 int raw_keybuf_count;
149 #define GROW_RAW_KEYBUF \
150 if (raw_keybuf_count == XVECTOR (raw_keybuf)->size) \
152 int newsize = 2 * XVECTOR (raw_keybuf)->size; \
153 Lisp_Object new; \
154 new = Fmake_vector (make_number (newsize), Qnil); \
155 bcopy (XVECTOR (raw_keybuf)->contents, XVECTOR (new)->contents, \
156 raw_keybuf_count * sizeof (Lisp_Object)); \
157 raw_keybuf = new; \
160 /* Number of elements of this_command_keys
161 that precede this key sequence. */
162 int this_single_command_key_start;
164 /* Record values of this_command_key_count and echo_length ()
165 before this command was read. */
166 static int before_command_key_count;
167 static int before_command_echo_length;
168 /* Values of before_command_key_count and before_command_echo_length
169 saved by reset-this-command-lengths. */
170 static int before_command_key_count_1;
171 static int before_command_echo_length_1;
172 /* Flag set by reset-this-command-lengths,
173 saying to reset the lengths when add_command_key is called. */
174 static int before_command_restore_flag;
176 extern int minbuf_level;
178 extern int message_enable_multibyte;
180 extern struct backtrace *backtrace_list;
182 /* If non-nil, the function that implements the display of help.
183 It's called with one argument, the help string to display. */
185 Lisp_Object Vshow_help_function;
187 /* Nonzero means do menu prompting. */
188 static int menu_prompting;
190 /* Character to see next line of menu prompt. */
191 static Lisp_Object menu_prompt_more_char;
193 /* For longjmp to where kbd input is being done. */
194 static jmp_buf getcjmp;
196 /* True while doing kbd input. */
197 int waiting_for_input;
199 /* True while displaying for echoing. Delays C-g throwing. */
201 static int echoing;
203 /* Non-null means we can start echoing at the next input pause even
204 though there is something in the echo area. */
206 static struct kboard *ok_to_echo_at_next_pause;
208 /* The kboard last echoing, or null for none. Reset to 0 in
209 cancel_echoing. If non-null, and a current echo area message
210 exists, and echo_message_buffer is eq to the current message
211 buffer, we know that the message comes from echo_kboard. */
213 static struct kboard *echo_kboard;
215 /* The buffer used for echoing. Set in echo_now, reset in
216 cancel_echoing. */
218 static Lisp_Object echo_message_buffer;
220 /* Nonzero means disregard local maps for the menu bar. */
221 static int inhibit_local_menu_bar_menus;
223 /* Nonzero means C-g should cause immediate error-signal. */
224 int immediate_quit;
226 /* The user's ERASE setting. */
227 Lisp_Object Vtty_erase_char;
229 /* Character to recognize as the help char. */
230 Lisp_Object Vhelp_char;
232 /* List of other event types to recognize as meaning "help". */
233 Lisp_Object Vhelp_event_list;
235 /* Form to execute when help char is typed. */
236 Lisp_Object Vhelp_form;
238 /* Command to run when the help character follows a prefix key. */
239 Lisp_Object Vprefix_help_command;
241 /* List of items that should move to the end of the menu bar. */
242 Lisp_Object Vmenu_bar_final_items;
244 /* Non-nil means show the equivalent key-binding for
245 any M-x command that has one.
246 The value can be a length of time to show the message for.
247 If the value is non-nil and not a number, we wait 2 seconds. */
248 Lisp_Object Vsuggest_key_bindings;
250 /* Character that causes a quit. Normally C-g.
252 If we are running on an ordinary terminal, this must be an ordinary
253 ASCII char, since we want to make it our interrupt character.
255 If we are not running on an ordinary terminal, it still needs to be
256 an ordinary ASCII char. This character needs to be recognized in
257 the input interrupt handler. At this point, the keystroke is
258 represented as a struct input_event, while the desired quit
259 character is specified as a lispy event. The mapping from struct
260 input_events to lispy events cannot run in an interrupt handler,
261 and the reverse mapping is difficult for anything but ASCII
262 keystrokes.
264 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
265 ASCII character. */
266 int quit_char;
268 extern Lisp_Object current_global_map;
269 extern int minibuf_level;
271 /* If non-nil, this is a map that overrides all other local maps. */
272 Lisp_Object Voverriding_local_map;
274 /* If non-nil, Voverriding_local_map applies to the menu bar. */
275 Lisp_Object Voverriding_local_map_menu_flag;
277 /* Keymap that defines special misc events that should
278 be processed immediately at a low level. */
279 Lisp_Object Vspecial_event_map;
281 /* Current depth in recursive edits. */
282 int command_loop_level;
284 /* Total number of times command_loop has read a key sequence. */
285 int num_input_keys;
287 /* Last input character read as a command. */
288 Lisp_Object last_command_char;
290 /* Last input character read as a command, not counting menus
291 reached by the mouse. */
292 Lisp_Object last_nonmenu_event;
294 /* Last input character read for any purpose. */
295 Lisp_Object last_input_char;
297 /* If not Qnil, a list of objects to be read as subsequent command input. */
298 Lisp_Object Vunread_command_events;
300 /* If not Qnil, a list of objects to be read as subsequent command input
301 including input method processing. */
302 Lisp_Object Vunread_input_method_events;
304 /* If not Qnil, a list of objects to be read as subsequent command input
305 but NOT including input method processing. */
306 Lisp_Object Vunread_post_input_method_events;
308 /* If not -1, an event to be read as subsequent command input. */
309 int unread_command_char;
311 /* If not Qnil, this is a switch-frame event which we decided to put
312 off until the end of a key sequence. This should be read as the
313 next command input, after any unread_command_events.
315 read_key_sequence uses this to delay switch-frame events until the
316 end of the key sequence; Fread_char uses it to put off switch-frame
317 events until a non-ASCII event is acceptable as input. */
318 Lisp_Object unread_switch_frame;
320 /* A mask of extra modifier bits to put into every keyboard char. */
321 int extra_keyboard_modifiers;
323 /* Char to use as prefix when a meta character is typed in.
324 This is bound on entry to minibuffer in case ESC is changed there. */
326 Lisp_Object meta_prefix_char;
328 /* Last size recorded for a current buffer which is not a minibuffer. */
329 static int last_non_minibuf_size;
331 /* Number of idle seconds before an auto-save and garbage collection. */
332 static Lisp_Object Vauto_save_timeout;
334 /* Total number of times read_char has returned. */
335 int num_input_events;
337 /* Total number of times read_char has returned, outside of macros. */
338 int num_nonmacro_input_events;
340 /* Auto-save automatically when this many characters have been typed
341 since the last time. */
343 static int auto_save_interval;
345 /* Value of num_nonmacro_input_events as of last auto save. */
347 int last_auto_save;
349 /* The command being executed by the command loop.
350 Commands may set this, and the value set will be copied into
351 current_kboard->Vlast_command instead of the actual command. */
352 Lisp_Object Vthis_command;
354 /* This is like Vthis_command, except that commands never set it. */
355 Lisp_Object real_this_command;
357 /* The value of point when the last command was executed. */
358 int last_point_position;
360 /* The buffer that was current when the last command was started. */
361 Lisp_Object last_point_position_buffer;
363 /* The frame in which the last input event occurred, or Qmacro if the
364 last event came from a macro. We use this to determine when to
365 generate switch-frame events. This may be cleared by functions
366 like Fselect_frame, to make sure that a switch-frame event is
367 generated by the next character. */
368 Lisp_Object internal_last_event_frame;
370 /* A user-visible version of the above, intended to allow users to
371 figure out where the last event came from, if the event doesn't
372 carry that information itself (i.e. if it was a character). */
373 Lisp_Object Vlast_event_frame;
375 /* The timestamp of the last input event we received from the X server.
376 X Windows wants this for selection ownership. */
377 unsigned long last_event_timestamp;
379 Lisp_Object Qself_insert_command;
380 Lisp_Object Qforward_char;
381 Lisp_Object Qbackward_char;
382 Lisp_Object Qundefined;
383 Lisp_Object Qtimer_event_handler;
385 /* read_key_sequence stores here the command definition of the
386 key sequence that it reads. */
387 Lisp_Object read_key_sequence_cmd;
389 /* Echo unfinished commands after this many seconds of pause. */
390 Lisp_Object Vecho_keystrokes;
392 /* Form to evaluate (if non-nil) when Emacs is started. */
393 Lisp_Object Vtop_level;
395 /* User-supplied string to translate input characters through. */
396 Lisp_Object Vkeyboard_translate_table;
398 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
399 extern Lisp_Object Vfunction_key_map;
401 /* Another keymap that maps key sequences into key sequences.
402 This one takes precedence over ordinary definitions. */
403 extern Lisp_Object Vkey_translation_map;
405 /* If non-nil, this implements the current input method. */
406 Lisp_Object Vinput_method_function;
407 Lisp_Object Qinput_method_function;
409 /* When we call Vinput_method_function,
410 this holds the echo area message that was just erased. */
411 Lisp_Object Vinput_method_previous_message;
413 /* Non-nil means deactivate the mark at end of this command. */
414 Lisp_Object Vdeactivate_mark;
416 /* Menu bar specified in Lucid Emacs fashion. */
418 Lisp_Object Vlucid_menu_bar_dirty_flag;
419 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
421 Lisp_Object Qecho_area_clear_hook;
423 /* Hooks to run before and after each command. */
424 Lisp_Object Qpre_command_hook, Vpre_command_hook;
425 Lisp_Object Qpost_command_hook, Vpost_command_hook;
426 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
427 /* Hook run after a command if there's no more input soon. */
428 Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook;
430 /* Delay time in microseconds before running post-command-idle-hook. */
431 int post_command_idle_delay;
433 /* List of deferred actions to be performed at a later time.
434 The precise format isn't relevant here; we just check whether it is nil. */
435 Lisp_Object Vdeferred_action_list;
437 /* Function to call to handle deferred actions, when there are any. */
438 Lisp_Object Vdeferred_action_function;
439 Lisp_Object Qdeferred_action_function;
441 Lisp_Object Qinput_method_exit_on_first_char;
442 Lisp_Object Qinput_method_use_echo_area;
444 /* File in which we write all commands we read. */
445 FILE *dribble;
447 /* Nonzero if input is available. */
448 int input_pending;
450 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
451 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
453 int meta_key;
455 extern char *pending_malloc_warning;
457 /* Circular buffer for pre-read keyboard input. */
459 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
461 /* Vector to GCPRO the Lisp objects referenced from kbd_buffer.
463 The interrupt-level event handlers will never enqueue an event on a
464 frame which is not in Vframe_list, and once an event is dequeued,
465 internal_last_event_frame or the event itself points to the frame.
466 So that's all fine.
468 But while the event is sitting in the queue, it's completely
469 unprotected. Suppose the user types one command which will run for
470 a while and then delete a frame, and then types another event at
471 the frame that will be deleted, before the command gets around to
472 it. Suppose there are no references to this frame elsewhere in
473 Emacs, and a GC occurs before the second event is dequeued. Now we
474 have an event referring to a freed frame, which will crash Emacs
475 when it is dequeued.
477 Similar things happen when an event on a scroll bar is enqueued; the
478 window may be deleted while the event is in the queue.
480 So, we use this vector to protect the Lisp_Objects in the event
481 queue. That way, they'll be dequeued as dead frames or windows,
482 but still valid Lisp objects.
484 If kbd_buffer[i].kind != no_event, then
486 AREF (kbd_buffer_gcpro, 2 * i) == kbd_buffer[i].frame_or_window.
487 AREF (kbd_buffer_gcpro, 2 * i + 1) == kbd_buffer[i].arg. */
489 static Lisp_Object kbd_buffer_gcpro;
491 /* Pointer to next available character in kbd_buffer.
492 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
493 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
494 next available char is in kbd_buffer[0]. */
495 static struct input_event *kbd_fetch_ptr;
497 /* Pointer to next place to store character in kbd_buffer. This
498 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
499 character should go in kbd_buffer[0]. */
500 static struct input_event * volatile kbd_store_ptr;
502 /* The above pair of variables forms a "queue empty" flag. When we
503 enqueue a non-hook event, we increment kbd_store_ptr. When we
504 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
505 there is input available iff the two pointers are not equal.
507 Why not just have a flag set and cleared by the enqueuing and
508 dequeuing functions? Such a flag could be screwed up by interrupts
509 at inopportune times. */
511 /* If this flag is non-nil, we check mouse_moved to see when the
512 mouse moves, and motion events will appear in the input stream.
513 Otherwise, mouse motion is ignored. */
514 static Lisp_Object do_mouse_tracking;
516 /* Symbols to head events. */
517 Lisp_Object Qmouse_movement;
518 Lisp_Object Qscroll_bar_movement;
519 Lisp_Object Qswitch_frame;
520 Lisp_Object Qdelete_frame;
521 Lisp_Object Qiconify_frame;
522 Lisp_Object Qmake_frame_visible;
523 Lisp_Object Qhelp_echo;
525 /* Symbols to denote kinds of events. */
526 Lisp_Object Qfunction_key;
527 Lisp_Object Qmouse_click;
528 #ifdef WINDOWSNT
529 Lisp_Object Qmouse_wheel;
530 Lisp_Object Qlanguage_change;
531 #endif
532 Lisp_Object Qdrag_n_drop;
533 /* Lisp_Object Qmouse_movement; - also an event header */
535 /* Properties of event headers. */
536 Lisp_Object Qevent_kind;
537 Lisp_Object Qevent_symbol_elements;
539 /* menu item parts */
540 Lisp_Object Qmenu_alias;
541 Lisp_Object Qmenu_enable;
542 Lisp_Object QCenable, QCvisible, QChelp, QCfilter, QCkeys, QCkey_sequence;
543 Lisp_Object QCbutton, QCtoggle, QCradio;
544 extern Lisp_Object Vdefine_key_rebound_commands;
545 extern Lisp_Object Qmenu_item;
547 /* An event header symbol HEAD may have a property named
548 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
549 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
550 mask of modifiers applied to it. If present, this is used to help
551 speed up parse_modifiers. */
552 Lisp_Object Qevent_symbol_element_mask;
554 /* An unmodified event header BASE may have a property named
555 Qmodifier_cache, which is an alist mapping modifier masks onto
556 modified versions of BASE. If present, this helps speed up
557 apply_modifiers. */
558 Lisp_Object Qmodifier_cache;
560 /* Symbols to use for parts of windows. */
561 Lisp_Object Qmode_line;
562 Lisp_Object Qvertical_line;
563 Lisp_Object Qvertical_scroll_bar;
564 Lisp_Object Qmenu_bar;
566 Lisp_Object recursive_edit_unwind (), command_loop ();
567 Lisp_Object Fthis_command_keys ();
568 Lisp_Object Qextended_command_history;
569 EMACS_TIME timer_check ();
571 extern Lisp_Object Vhistory_length;
573 extern char *x_get_keysym_name ();
575 static void record_menu_key ();
577 Lisp_Object Qpolling_period;
579 /* List of absolute timers. Appears in order of next scheduled event. */
580 Lisp_Object Vtimer_list;
582 /* List of idle time timers. Appears in order of next scheduled event. */
583 Lisp_Object Vtimer_idle_list;
585 /* Incremented whenever a timer is run. */
586 int timers_run;
588 extern Lisp_Object Vprint_level, Vprint_length;
590 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
591 happens. */
592 EMACS_TIME *input_available_clear_time;
594 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
595 Default is 1 if INTERRUPT_INPUT is defined. */
596 int interrupt_input;
598 /* Nonzero while interrupts are temporarily deferred during redisplay. */
599 int interrupts_deferred;
601 /* Nonzero means use ^S/^Q for flow control. */
602 int flow_control;
604 /* Allow m- file to inhibit use of FIONREAD. */
605 #ifdef BROKEN_FIONREAD
606 #undef FIONREAD
607 #endif
609 /* We are unable to use interrupts if FIONREAD is not available,
610 so flush SIGIO so we won't try. */
611 #ifndef FIONREAD
612 #ifdef SIGIO
613 #undef SIGIO
614 #endif
615 #endif
617 /* If we support a window system, turn on the code to poll periodically
618 to detect C-g. It isn't actually used when doing interrupt input. */
619 #ifdef HAVE_WINDOW_SYSTEM
620 #define POLL_FOR_INPUT
621 #endif
623 /* After a command is executed, if point is moved into a region that
624 has specific properties (e.g. composition, display), we adjust
625 point to the boundary of the region. But, if a command sets this
626 valiable to non-nil, we suppress this point adjustment. This
627 variable is set to nil before reading a command. */
629 Lisp_Object Vdisable_point_adjustment;
631 /* If non-nil, always disable point adjustment. */
633 Lisp_Object Vglobal_disable_point_adjustment;
636 /* Global variable declarations. */
638 /* Function for init_keyboard to call with no args (if nonzero). */
639 void (*keyboard_init_hook) ();
641 static int read_avail_input P_ ((int));
642 static void get_input_pending P_ ((int *, int));
643 static int readable_events P_ ((int));
644 static Lisp_Object read_char_x_menu_prompt P_ ((int, Lisp_Object *,
645 Lisp_Object, int *));
646 static Lisp_Object read_char_x_menu_prompt ();
647 static Lisp_Object read_char_minibuf_menu_prompt P_ ((int, int,
648 Lisp_Object *));
649 static Lisp_Object make_lispy_event P_ ((struct input_event *));
650 #ifdef HAVE_MOUSE
651 static Lisp_Object make_lispy_movement P_ ((struct frame *, Lisp_Object,
652 enum scroll_bar_part,
653 Lisp_Object, Lisp_Object,
654 unsigned long));
655 #endif
656 static Lisp_Object modify_event_symbol P_ ((int, unsigned, Lisp_Object,
657 Lisp_Object, char **,
658 Lisp_Object *, unsigned));
659 static Lisp_Object make_lispy_switch_frame P_ ((Lisp_Object));
660 static int parse_solitary_modifier P_ ((Lisp_Object));
661 static int parse_solitary_modifier ();
662 static void save_getcjmp P_ ((jmp_buf));
663 static void save_getcjmp ();
664 static void restore_getcjmp P_ ((jmp_buf));
665 static Lisp_Object apply_modifiers P_ ((int, Lisp_Object));
666 static void clear_event P_ ((struct input_event *));
668 /* Nonzero means don't try to suspend even if the operating system seems
669 to support it. */
670 static int cannot_suspend;
672 #define min(a,b) ((a)<(b)?(a):(b))
673 #define max(a,b) ((a)>(b)?(a):(b))
675 /* Install the string STR as the beginning of the string of echoing,
676 so that it serves as a prompt for the next character.
677 Also start echoing. */
679 void
680 echo_prompt (str)
681 char *str;
683 int len = strlen (str);
685 if (len > ECHOBUFSIZE - 4)
686 len = ECHOBUFSIZE - 4;
687 bcopy (str, current_kboard->echobuf, len);
688 current_kboard->echoptr = current_kboard->echobuf + len;
689 *current_kboard->echoptr = '\0';
691 current_kboard->echo_after_prompt = len;
693 echo_now ();
696 /* Add C to the echo string, if echoing is going on.
697 C can be a character, which is printed prettily ("M-C-x" and all that
698 jazz), or a symbol, whose name is printed. */
700 void
701 echo_char (c)
702 Lisp_Object c;
704 extern char *push_key_description ();
706 if (current_kboard->immediate_echo)
708 char *ptr = current_kboard->echoptr;
710 if (ptr != current_kboard->echobuf)
711 *ptr++ = ' ';
713 /* If someone has passed us a composite event, use its head symbol. */
714 c = EVENT_HEAD (c);
716 if (INTEGERP (c))
718 if (ptr - current_kboard->echobuf
719 > ECHOBUFSIZE - KEY_DESCRIPTION_SIZE)
720 return;
722 ptr = push_key_description (XINT (c), ptr);
724 else if (SYMBOLP (c))
726 struct Lisp_String *name = XSYMBOL (c)->name;
727 if ((ptr - current_kboard->echobuf) + STRING_BYTES (name) + 4
728 > ECHOBUFSIZE)
729 return;
730 bcopy (name->data, ptr, STRING_BYTES (name));
731 ptr += STRING_BYTES (name);
734 if (current_kboard->echoptr == current_kboard->echobuf
735 && help_char_p (c))
737 strcpy (ptr, " (Type ? for further options)");
738 ptr += strlen (ptr);
741 *ptr = 0;
742 current_kboard->echoptr = ptr;
744 echo_now ();
748 /* Temporarily add a dash to the end of the echo string if it's not
749 empty, so that it serves as a mini-prompt for the very next character. */
751 void
752 echo_dash ()
754 if (!current_kboard->immediate_echo
755 && current_kboard->echoptr == current_kboard->echobuf)
756 return;
757 /* Do nothing if we just printed a prompt. */
758 if (current_kboard->echo_after_prompt
759 == current_kboard->echoptr - current_kboard->echobuf)
760 return;
761 /* Do nothing if not echoing at all. */
762 if (current_kboard->echoptr == 0)
763 return;
765 /* Put a dash at the end of the buffer temporarily,
766 but make it go away when the next character is added. */
767 current_kboard->echoptr[0] = '-';
768 current_kboard->echoptr[1] = 0;
770 echo_now ();
773 /* Display the current echo string, and begin echoing if not already
774 doing so. */
776 void
777 echo_now ()
779 if (!current_kboard->immediate_echo)
781 int i;
782 current_kboard->immediate_echo = 1;
784 for (i = 0; i < this_command_key_count; i++)
786 Lisp_Object c;
787 c = XVECTOR (this_command_keys)->contents[i];
788 if (! (EVENT_HAS_PARAMETERS (c)
789 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
790 echo_char (c);
792 echo_dash ();
795 echoing = 1;
796 message2_nolog (current_kboard->echobuf, strlen (current_kboard->echobuf),
797 ! NILP (current_buffer->enable_multibyte_characters));
798 echoing = 0;
800 /* Record in what buffer we echoed, and from which kboard. */
801 echo_message_buffer = echo_area_buffer[0];
802 echo_kboard = current_kboard;
804 if (waiting_for_input && !NILP (Vquit_flag))
805 quit_throw_to_read_char ();
808 /* Turn off echoing, for the start of a new command. */
810 void
811 cancel_echoing ()
813 current_kboard->immediate_echo = 0;
814 current_kboard->echoptr = current_kboard->echobuf;
815 current_kboard->echo_after_prompt = -1;
816 ok_to_echo_at_next_pause = NULL;
817 echo_kboard = NULL;
818 echo_message_buffer = Qnil;
821 /* Return the length of the current echo string. */
823 static int
824 echo_length ()
826 return current_kboard->echoptr - current_kboard->echobuf;
829 /* Truncate the current echo message to its first LEN chars.
830 This and echo_char get used by read_key_sequence when the user
831 switches frames while entering a key sequence. */
833 static void
834 echo_truncate (len)
835 int len;
837 current_kboard->echobuf[len] = '\0';
838 current_kboard->echoptr = current_kboard->echobuf + len;
839 truncate_echo_area (len);
843 /* Functions for manipulating this_command_keys. */
844 static void
845 add_command_key (key)
846 Lisp_Object key;
848 int size = XVECTOR (this_command_keys)->size;
850 /* If reset-this-command-length was called recently, obey it now.
851 See the doc string of that function for an explanation of why. */
852 if (before_command_restore_flag)
854 this_command_key_count = before_command_key_count_1;
855 if (this_command_key_count < this_single_command_key_start)
856 this_single_command_key_start = this_command_key_count;
857 echo_truncate (before_command_echo_length_1);
858 before_command_restore_flag = 0;
861 if (this_command_key_count >= size)
863 Lisp_Object new_keys;
865 new_keys = Fmake_vector (make_number (size * 2), Qnil);
866 bcopy (XVECTOR (this_command_keys)->contents,
867 XVECTOR (new_keys)->contents,
868 size * sizeof (Lisp_Object));
870 this_command_keys = new_keys;
873 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
876 Lisp_Object
877 recursive_edit_1 ()
879 int count = specpdl_ptr - specpdl;
880 Lisp_Object val;
882 if (command_loop_level > 0)
884 specbind (Qstandard_output, Qt);
885 specbind (Qstandard_input, Qt);
888 #ifdef HAVE_X_WINDOWS
889 /* The command loop has started a busy-cursor timer, so we have to
890 cancel it here, otherwise it will fire because the recursive edit
891 can take some time. */
892 if (display_busy_cursor_p)
893 cancel_busy_cursor ();
894 #endif
896 val = command_loop ();
897 if (EQ (val, Qt))
898 Fsignal (Qquit, Qnil);
899 /* Handle throw from read_minibuf when using minibuffer
900 while it's active but we're in another window. */
901 if (STRINGP (val))
902 Fsignal (Qerror, Fcons (val, Qnil));
904 return unbind_to (count, Qnil);
907 /* When an auto-save happens, record the "time", and don't do again soon. */
909 void
910 record_auto_save ()
912 last_auto_save = num_nonmacro_input_events;
915 /* Make an auto save happen as soon as possible at command level. */
917 void
918 force_auto_save_soon ()
920 last_auto_save = - auto_save_interval - 1;
922 record_asynch_buffer_change ();
925 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
926 "Invoke the editor command loop recursively.\n\
927 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
928 that tells this function to return.\n\
929 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
930 This function is called by the editor initialization to begin editing.")
933 int count = specpdl_ptr - specpdl;
935 command_loop_level++;
936 update_mode_lines = 1;
938 record_unwind_protect (recursive_edit_unwind,
939 (command_loop_level
940 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
941 ? Fcurrent_buffer ()
942 : Qnil);
943 recursive_edit_1 ();
944 return unbind_to (count, Qnil);
947 Lisp_Object
948 recursive_edit_unwind (buffer)
949 Lisp_Object buffer;
951 if (!NILP (buffer))
952 Fset_buffer (buffer);
954 command_loop_level--;
955 update_mode_lines = 1;
956 return Qnil;
959 static void
960 any_kboard_state ()
962 #ifdef MULTI_KBOARD
963 #if 0 /* Theory: if there's anything in Vunread_command_events,
964 it will right away be read by read_key_sequence,
965 and then if we do switch KBOARDS, it will go into the side
966 queue then. So we don't need to do anything special here -- rms. */
967 if (CONSP (Vunread_command_events))
969 current_kboard->kbd_queue
970 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
971 current_kboard->kbd_queue_has_data = 1;
973 Vunread_command_events = Qnil;
974 #endif
975 single_kboard = 0;
976 #endif
979 /* Switch to the single-kboard state, making current_kboard
980 the only KBOARD from which further input is accepted. */
982 void
983 single_kboard_state ()
985 #ifdef MULTI_KBOARD
986 single_kboard = 1;
987 #endif
990 /* Maintain a stack of kboards, so other parts of Emacs
991 can switch temporarily to the kboard of a given frame
992 and then revert to the previous status. */
994 struct kboard_stack
996 KBOARD *kboard;
997 struct kboard_stack *next;
1000 static struct kboard_stack *kboard_stack;
1002 void
1003 push_frame_kboard (f)
1004 FRAME_PTR f;
1006 #ifdef MULTI_KBOARD
1007 struct kboard_stack *p
1008 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
1010 p->next = kboard_stack;
1011 p->kboard = current_kboard;
1012 kboard_stack = p;
1014 current_kboard = FRAME_KBOARD (f);
1015 #endif
1018 void
1019 pop_frame_kboard ()
1021 #ifdef MULTI_KBOARD
1022 struct kboard_stack *p = kboard_stack;
1023 current_kboard = p->kboard;
1024 kboard_stack = p->next;
1025 xfree (p);
1026 #endif
1029 /* Handle errors that are not handled at inner levels
1030 by printing an error message and returning to the editor command loop. */
1032 Lisp_Object
1033 cmd_error (data)
1034 Lisp_Object data;
1036 Lisp_Object old_level, old_length;
1037 char macroerror[50];
1039 if (!NILP (executing_macro))
1041 if (executing_macro_iterations == 1)
1042 sprintf (macroerror, "After 1 kbd macro iteration: ");
1043 else
1044 sprintf (macroerror, "After %d kbd macro iterations: ",
1045 executing_macro_iterations);
1047 else
1048 *macroerror = 0;
1050 Vstandard_output = Qt;
1051 Vstandard_input = Qt;
1052 Vexecuting_macro = Qnil;
1053 executing_macro = Qnil;
1054 current_kboard->Vprefix_arg = Qnil;
1055 current_kboard->Vlast_prefix_arg = Qnil;
1056 cancel_echoing ();
1058 /* Avoid unquittable loop if data contains a circular list. */
1059 old_level = Vprint_level;
1060 old_length = Vprint_length;
1061 XSETFASTINT (Vprint_level, 10);
1062 XSETFASTINT (Vprint_length, 10);
1063 cmd_error_internal (data, macroerror);
1064 Vprint_level = old_level;
1065 Vprint_length = old_length;
1067 Vquit_flag = Qnil;
1069 Vinhibit_quit = Qnil;
1070 #ifdef MULTI_KBOARD
1071 any_kboard_state ();
1072 #endif
1074 return make_number (0);
1077 /* Take actions on handling an error. DATA is the data that describes
1078 the error.
1080 CONTEXT is a C-string containing ASCII characters only which
1081 describes the context in which the error happened. If we need to
1082 generalize CONTEXT to allow multibyte characters, make it a Lisp
1083 string. */
1085 void
1086 cmd_error_internal (data, context)
1087 Lisp_Object data;
1088 char *context;
1090 Lisp_Object stream;
1091 int kill_emacs_p = 0;
1092 struct frame *sf = SELECTED_FRAME ();
1094 Vquit_flag = Qnil;
1095 Vinhibit_quit = Qt;
1096 clear_message (1, 0);
1098 /* If the window system or terminal frame hasn't been initialized
1099 yet, or we're not interactive, it's best to dump this message out
1100 to stderr and exit. */
1101 if (!sf->glyphs_initialized_p
1102 /* This is the case of the frame dumped with Emacs, when we're
1103 running under a window system. */
1104 || (!NILP (Vwindow_system)
1105 && !inhibit_window_system
1106 && FRAME_TERMCAP_P (sf))
1107 || noninteractive)
1109 stream = Qexternal_debugging_output;
1110 kill_emacs_p = 1;
1112 else
1114 Fdiscard_input ();
1115 bitch_at_user ();
1116 stream = Qt;
1119 if (context != 0)
1120 write_string_1 (context, -1, stream);
1122 print_error_message (data, stream);
1124 /* If the window system or terminal frame hasn't been initialized
1125 yet, or we're in -batch mode, this error should cause Emacs to exit. */
1126 if (kill_emacs_p)
1128 Fterpri (stream);
1129 Fkill_emacs (make_number (-1));
1133 Lisp_Object command_loop_1 ();
1134 Lisp_Object command_loop_2 ();
1135 Lisp_Object top_level_1 ();
1137 /* Entry to editor-command-loop.
1138 This level has the catches for exiting/returning to editor command loop.
1139 It returns nil to exit recursive edit, t to abort it. */
1141 Lisp_Object
1142 command_loop ()
1144 if (command_loop_level > 0 || minibuf_level > 0)
1146 Lisp_Object val;
1147 val = internal_catch (Qexit, command_loop_2, Qnil);
1148 executing_macro = Qnil;
1149 return val;
1151 else
1152 while (1)
1154 internal_catch (Qtop_level, top_level_1, Qnil);
1155 internal_catch (Qtop_level, command_loop_2, Qnil);
1156 executing_macro = Qnil;
1158 /* End of file in -batch run causes exit here. */
1159 if (noninteractive)
1160 Fkill_emacs (Qt);
1164 /* Here we catch errors in execution of commands within the
1165 editing loop, and reenter the editing loop.
1166 When there is an error, cmd_error runs and returns a non-nil
1167 value to us. A value of nil means that cmd_loop_1 itself
1168 returned due to end of file (or end of kbd macro). */
1170 Lisp_Object
1171 command_loop_2 ()
1173 register Lisp_Object val;
1176 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
1177 while (!NILP (val));
1179 return Qnil;
1182 Lisp_Object
1183 top_level_2 ()
1185 return Feval (Vtop_level);
1188 Lisp_Object
1189 top_level_1 ()
1191 /* On entry to the outer level, run the startup file */
1192 if (!NILP (Vtop_level))
1193 internal_condition_case (top_level_2, Qerror, cmd_error);
1194 else if (!NILP (Vpurify_flag))
1195 message ("Bare impure Emacs (standard Lisp code not loaded)");
1196 else
1197 message ("Bare Emacs (standard Lisp code not loaded)");
1198 return Qnil;
1201 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1202 "Exit all recursive editing levels.")
1205 #ifdef HAVE_X_WINDOWS
1206 if (display_busy_cursor_p)
1207 cancel_busy_cursor ();
1208 #endif
1209 return Fthrow (Qtop_level, Qnil);
1212 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1213 "Exit from the innermost recursive edit or minibuffer.")
1216 if (command_loop_level > 0 || minibuf_level > 0)
1217 Fthrow (Qexit, Qnil);
1219 error ("No recursive edit is in progress");
1220 return Qnil;
1223 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1224 "Abort the command that requested this recursive edit or minibuffer input.")
1227 if (command_loop_level > 0 || minibuf_level > 0)
1228 Fthrow (Qexit, Qt);
1230 error ("No recursive edit is in progress");
1231 return Qnil;
1234 /* This is the actual command reading loop,
1235 sans error-handling encapsulation. */
1237 Lisp_Object Fcommand_execute ();
1238 static int read_key_sequence ();
1239 void safe_run_hooks ();
1240 static void adjust_point_for_property ();
1242 Lisp_Object
1243 command_loop_1 ()
1245 Lisp_Object cmd;
1246 int lose;
1247 int nonundocount;
1248 Lisp_Object keybuf[30];
1249 int i;
1250 int no_direct;
1251 int prev_modiff;
1252 struct buffer *prev_buffer = NULL;
1253 #ifdef MULTI_KBOARD
1254 int was_locked = single_kboard;
1255 #endif
1257 current_kboard->Vprefix_arg = Qnil;
1258 current_kboard->Vlast_prefix_arg = Qnil;
1259 Vdeactivate_mark = Qnil;
1260 waiting_for_input = 0;
1261 cancel_echoing ();
1263 nonundocount = 0;
1264 this_command_key_count = 0;
1265 this_single_command_key_start = 0;
1267 /* Make sure this hook runs after commands that get errors and
1268 throw to top level. */
1269 /* Note that the value cell will never directly contain nil
1270 if the symbol is a local variable. */
1271 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1272 safe_run_hooks (Qpost_command_hook);
1274 /* If displaying a message, resize the echo area window to fit
1275 that message's size exactly. */
1276 if (!NILP (echo_area_buffer[0]))
1277 resize_echo_area_axactly ();
1279 if (!NILP (Vdeferred_action_list))
1280 call0 (Vdeferred_action_function);
1282 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1284 if (NILP (Vunread_command_events)
1285 && NILP (Vunread_input_method_events)
1286 && NILP (Vunread_post_input_method_events)
1287 && NILP (Vexecuting_macro)
1288 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1289 safe_run_hooks (Qpost_command_idle_hook);
1292 /* Do this after running Vpost_command_hook, for consistency. */
1293 current_kboard->Vlast_command = Vthis_command;
1294 current_kboard->Vreal_last_command = real_this_command;
1296 while (1)
1298 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1299 Fkill_emacs (Qnil);
1301 /* Make sure the current window's buffer is selected. */
1302 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1303 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1305 /* Display any malloc warning that just came out. Use while because
1306 displaying one warning can cause another. */
1308 while (pending_malloc_warning)
1309 display_malloc_warning ();
1311 no_direct = 0;
1313 Vdeactivate_mark = Qnil;
1315 /* If minibuffer on and echo area in use,
1316 wait 2 sec and redraw minibuffer. */
1318 if (minibuf_level
1319 && !NILP (echo_area_buffer[0])
1320 && EQ (minibuf_window, echo_area_window))
1322 /* Bind inhibit-quit to t so that C-g gets read in
1323 rather than quitting back to the minibuffer. */
1324 int count = specpdl_ptr - specpdl;
1325 specbind (Qinhibit_quit, Qt);
1327 Fsit_for (make_number (2), Qnil, Qnil);
1328 /* Clear the echo area. */
1329 message2 (0, 0, 0);
1330 safe_run_hooks (Qecho_area_clear_hook);
1332 unbind_to (count, Qnil);
1334 /* If a C-g came in before, treat it as input now. */
1335 if (!NILP (Vquit_flag))
1337 Vquit_flag = Qnil;
1338 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1342 #ifdef C_ALLOCA
1343 alloca (0); /* Cause a garbage collection now */
1344 /* Since we can free the most stuff here. */
1345 #endif /* C_ALLOCA */
1347 #if 0
1348 /* Select the frame that the last event came from. Usually,
1349 switch-frame events will take care of this, but if some lisp
1350 code swallows a switch-frame event, we'll fix things up here.
1351 Is this a good idea? */
1352 if (FRAMEP (internal_last_event_frame)
1353 && !EQ (internal_last_event_frame, selected_frame))
1354 Fselect_frame (internal_last_event_frame, Qnil);
1355 #endif
1356 /* If it has changed current-menubar from previous value,
1357 really recompute the menubar from the value. */
1358 if (! NILP (Vlucid_menu_bar_dirty_flag)
1359 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1360 call0 (Qrecompute_lucid_menubar);
1362 before_command_key_count = this_command_key_count;
1363 before_command_echo_length = echo_length ();
1365 Vthis_command = Qnil;
1366 real_this_command = Qnil;
1368 /* Read next key sequence; i gets its length. */
1369 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1370 Qnil, 0, 1, 1);
1372 /* A filter may have run while we were reading the input. */
1373 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1374 Fkill_emacs (Qnil);
1375 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1376 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1378 ++num_input_keys;
1380 /* Now we have read a key sequence of length I,
1381 or else I is 0 and we found end of file. */
1383 if (i == 0) /* End of file -- happens only in */
1384 return Qnil; /* a kbd macro, at the end. */
1385 /* -1 means read_key_sequence got a menu that was rejected.
1386 Just loop around and read another command. */
1387 if (i == -1)
1389 cancel_echoing ();
1390 this_command_key_count = 0;
1391 this_single_command_key_start = 0;
1392 goto finalize;
1395 last_command_char = keybuf[i - 1];
1397 /* If the previous command tried to force a specific window-start,
1398 forget about that, in case this command moves point far away
1399 from that position. But also throw away beg_unchanged and
1400 end_unchanged information in that case, so that redisplay will
1401 update the whole window properly. */
1402 if (!NILP (XWINDOW (selected_window)->force_start))
1404 struct buffer *b;
1405 XWINDOW (selected_window)->force_start = Qnil;
1406 b = XBUFFER (XWINDOW (selected_window)->buffer);
1407 BUF_BEG_UNCHANGED (b) = BUF_END_UNCHANGED (b) = 0;
1410 cmd = read_key_sequence_cmd;
1411 if (!NILP (Vexecuting_macro))
1413 if (!NILP (Vquit_flag))
1415 Vexecuting_macro = Qt;
1416 QUIT; /* Make some noise. */
1417 /* Will return since macro now empty. */
1421 /* Do redisplay processing after this command except in special
1422 cases identified below. */
1423 prev_buffer = current_buffer;
1424 prev_modiff = MODIFF;
1425 last_point_position = PT;
1426 XSETBUFFER (last_point_position_buffer, prev_buffer);
1428 /* By default, we adjust point to a boundary of a region that
1429 has such a property that should be treated intangible
1430 (e.g. composition, display). But, some commands will set
1431 this variable differently. */
1432 Vdisable_point_adjustment = Qnil;
1434 /* Execute the command. */
1436 Vthis_command = cmd;
1437 real_this_command = cmd;
1438 /* Note that the value cell will never directly contain nil
1439 if the symbol is a local variable. */
1440 if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
1441 safe_run_hooks (Qpre_command_hook);
1443 if (NILP (Vthis_command))
1445 /* nil means key is undefined. */
1446 bitch_at_user ();
1447 current_kboard->defining_kbd_macro = Qnil;
1448 update_mode_lines = 1;
1449 current_kboard->Vprefix_arg = Qnil;
1451 else
1453 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1455 /* In case we jump to directly_done. */
1456 Vcurrent_prefix_arg = current_kboard->Vprefix_arg;
1458 /* Recognize some common commands in common situations and
1459 do them directly. */
1460 if (EQ (Vthis_command, Qforward_char) && PT < ZV)
1462 struct Lisp_Char_Table *dp
1463 = window_display_table (XWINDOW (selected_window));
1464 lose = FETCH_CHAR (PT_BYTE);
1465 SET_PT (PT + 1);
1466 if ((dp
1467 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1468 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1469 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1470 && (lose >= 0x20 && lose < 0x7f)))
1471 : (lose >= 0x20 && lose < 0x7f))
1472 /* To extract the case of continuation on
1473 wide-column characters. */
1474 && (WIDTH_BY_CHAR_HEAD (FETCH_BYTE (PT_BYTE)) == 1)
1475 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1476 >= MODIFF)
1477 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1478 >= OVERLAY_MODIFF)
1479 && (XFASTINT (XWINDOW (selected_window)->last_point)
1480 == PT - 1)
1481 && !windows_or_buffers_changed
1482 && EQ (current_buffer->selective_display, Qnil)
1483 && !detect_input_pending ()
1484 && NILP (XWINDOW (selected_window)->column_number_displayed)
1485 && NILP (Vexecuting_macro))
1486 direct_output_forward_char (1);
1487 goto directly_done;
1489 else if (EQ (Vthis_command, Qbackward_char) && PT > BEGV)
1491 struct Lisp_Char_Table *dp
1492 = window_display_table (XWINDOW (selected_window));
1493 SET_PT (PT - 1);
1494 lose = FETCH_CHAR (PT_BYTE);
1495 if ((dp
1496 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1497 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1498 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1499 && (lose >= 0x20 && lose < 0x7f)))
1500 : (lose >= 0x20 && lose < 0x7f))
1501 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1502 >= MODIFF)
1503 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1504 >= OVERLAY_MODIFF)
1505 && (XFASTINT (XWINDOW (selected_window)->last_point)
1506 == PT + 1)
1507 && !windows_or_buffers_changed
1508 && EQ (current_buffer->selective_display, Qnil)
1509 && !detect_input_pending ()
1510 && NILP (XWINDOW (selected_window)->column_number_displayed)
1511 && NILP (Vexecuting_macro))
1512 direct_output_forward_char (-1);
1513 goto directly_done;
1515 else if (EQ (Vthis_command, Qself_insert_command)
1516 /* Try this optimization only on ascii keystrokes. */
1517 && INTEGERP (last_command_char))
1519 unsigned int c = XINT (last_command_char);
1520 int value;
1521 if (NILP (Vexecuting_macro)
1522 && !EQ (minibuf_window, selected_window))
1524 if (!nonundocount || nonundocount >= 20)
1526 Fundo_boundary ();
1527 nonundocount = 0;
1529 nonundocount++;
1532 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1533 < MODIFF)
1534 || (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1535 < OVERLAY_MODIFF)
1536 || (XFASTINT (XWINDOW (selected_window)->last_point)
1537 != PT)
1538 || MODIFF <= SAVE_MODIFF
1539 || windows_or_buffers_changed
1540 || !EQ (current_buffer->selective_display, Qnil)
1541 || detect_input_pending ()
1542 || !NILP (XWINDOW (selected_window)->column_number_displayed)
1543 || !NILP (Vexecuting_macro));
1545 value = internal_self_insert (c, 0);
1547 if (value == 2)
1548 nonundocount = 0;
1550 /* VALUE == 1 when AFTER-CHANGE functions are
1551 installed which is the case most of the time
1552 because FONT-LOCK installs one. */
1553 if (!lose && !value)
1554 direct_output_for_insert (c);
1555 goto directly_done;
1559 /* Here for a command that isn't executed directly */
1561 #ifdef HAVE_X_WINDOWS
1562 if (display_busy_cursor_p)
1563 start_busy_cursor ();
1564 #endif
1566 nonundocount = 0;
1567 if (NILP (current_kboard->Vprefix_arg))
1568 Fundo_boundary ();
1569 Fcommand_execute (Vthis_command, Qnil, Qnil, Qnil);
1571 #ifdef HAVE_X_WINDOWS
1572 if (display_busy_cursor_p)
1573 cancel_busy_cursor ();
1574 #endif
1576 directly_done: ;
1577 current_kboard->Vlast_prefix_arg = Vcurrent_prefix_arg;
1579 /* Note that the value cell will never directly contain nil
1580 if the symbol is a local variable. */
1581 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1582 safe_run_hooks (Qpost_command_hook);
1584 /* If displaying a message, resize the echo area window to fit
1585 that message's size exactly. */
1586 if (!NILP (echo_area_buffer[0]))
1587 resize_echo_area_axactly ();
1589 if (!NILP (Vdeferred_action_list))
1590 safe_run_hooks (Qdeferred_action_function);
1592 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1594 if (NILP (Vunread_command_events)
1595 && NILP (Vunread_input_method_events)
1596 && NILP (Vunread_post_input_method_events)
1597 && NILP (Vexecuting_macro)
1598 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1599 safe_run_hooks (Qpost_command_idle_hook);
1602 /* If there is a prefix argument,
1603 1) We don't want Vlast_command to be ``universal-argument''
1604 (that would be dumb), so don't set Vlast_command,
1605 2) we want to leave echoing on so that the prefix will be
1606 echoed as part of this key sequence, so don't call
1607 cancel_echoing, and
1608 3) we want to leave this_command_key_count non-zero, so that
1609 read_char will realize that it is re-reading a character, and
1610 not echo it a second time.
1612 If the command didn't actually create a prefix arg,
1613 but is merely a frame event that is transparent to prefix args,
1614 then the above doesn't apply. */
1615 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1617 current_kboard->Vlast_command = Vthis_command;
1618 current_kboard->Vreal_last_command = real_this_command;
1619 cancel_echoing ();
1620 this_command_key_count = 0;
1621 this_single_command_key_start = 0;
1624 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1626 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1628 current_buffer->mark_active = Qnil;
1629 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1631 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1632 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1635 finalize:
1637 if (current_buffer == prev_buffer
1638 && last_point_position != PT
1639 && NILP (Vdisable_point_adjustment)
1640 && NILP (Vglobal_disable_point_adjustment))
1641 adjust_point_for_property (last_point_position);
1643 /* Install chars successfully executed in kbd macro. */
1645 if (!NILP (current_kboard->defining_kbd_macro)
1646 && NILP (current_kboard->Vprefix_arg))
1647 finalize_kbd_macro_chars ();
1649 #ifdef MULTI_KBOARD
1650 if (!was_locked)
1651 any_kboard_state ();
1652 #endif
1656 extern Lisp_Object Qcomposition, Qdisplay;
1658 /* Adjust point to a boundary of a region that has such a property
1659 that should be treated intangible. For the moment, we check
1660 `composition' and `display' property. LAST_PT is the last position
1661 of point. */
1663 static void
1664 adjust_point_for_property (last_pt)
1665 int last_pt;
1667 int start, end;
1668 Lisp_Object val;
1669 int check_composition = 1, check_display = 1;
1671 while (check_composition || check_display)
1673 if (check_composition
1674 && PT > BEGV && PT < ZV
1675 && get_property_and_range (PT, Qcomposition, &val, &start, &end, Qnil)
1676 && COMPOSITION_VALID_P (start, end, val)
1677 && start < PT && end > PT
1678 && (last_pt <= start || last_pt >= end))
1680 if (PT < last_pt)
1681 SET_PT (start);
1682 else
1683 SET_PT (end);
1684 check_display = 1;
1686 check_composition = 0;
1687 if (check_display
1688 && PT > BEGV && PT < ZV
1689 && get_property_and_range (PT, Qdisplay, &val, &start, &end, Qnil)
1690 && display_prop_intangible_p (val)
1691 && start < PT && end > PT
1692 && (last_pt <= start || last_pt >= end))
1694 if (PT < last_pt)
1695 SET_PT (start);
1696 else
1697 SET_PT (end);
1698 check_composition = 1;
1700 check_display = 0;
1704 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1706 static Lisp_Object
1707 safe_run_hooks_1 (hook)
1708 Lisp_Object hook;
1710 return call1 (Vrun_hooks, Vinhibit_quit);
1713 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1715 static Lisp_Object
1716 safe_run_hooks_error (data)
1717 Lisp_Object data;
1719 return Fset (Vinhibit_quit, Qnil);
1722 /* If we get an error while running the hook, cause the hook variable
1723 to be nil. Also inhibit quits, so that C-g won't cause the hook
1724 to mysteriously evaporate. */
1726 void
1727 safe_run_hooks (hook)
1728 Lisp_Object hook;
1730 int count = specpdl_ptr - specpdl;
1731 specbind (Qinhibit_quit, hook);
1733 internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
1735 unbind_to (count, Qnil);
1739 /* Number of seconds between polling for input. This is a Lisp
1740 variable that can be bound. */
1742 int polling_period;
1744 /* Nonzero means polling for input is temporarily suppressed. */
1746 int poll_suppress_count;
1748 /* Asynchronous timer for polling. */
1750 struct atimer *poll_timer;
1753 #ifdef POLL_FOR_INPUT
1755 /* Poll for input, so what we catch a C-g if it comes in. This
1756 function is called from x_make_frame_visible, see comment
1757 there. */
1759 void
1760 poll_for_input_1 ()
1762 if (interrupt_input_blocked == 0
1763 && !waiting_for_input)
1764 read_avail_input (0);
1767 /* Timer callback function for poll_timer. TIMER is equal to
1768 poll_timer. */
1770 void
1771 poll_for_input (timer)
1772 struct atimer *timer;
1774 if (poll_suppress_count == 0)
1775 poll_for_input_1 ();
1778 #endif /* POLL_FOR_INPUT */
1780 /* Begin signals to poll for input, if they are appropriate.
1781 This function is called unconditionally from various places. */
1783 void
1784 start_polling ()
1786 #ifdef POLL_FOR_INPUT
1787 if (read_socket_hook && !interrupt_input)
1789 /* Turn alarm handling on unconditionally. It might have
1790 been turned off in process.c. */
1791 turn_on_atimers (1);
1793 /* If poll timer doesn't exist, are we need one with
1794 a different interval, start a new one. */
1795 if (poll_timer == NULL
1796 || EMACS_SECS (poll_timer->interval) != polling_period)
1798 EMACS_TIME interval;
1800 if (poll_timer)
1801 cancel_atimer (poll_timer);
1803 EMACS_SET_SECS_USECS (interval, polling_period, 0);
1804 poll_timer = start_atimer (ATIMER_CONTINUOUS, interval,
1805 poll_for_input, NULL);
1808 /* Let the timer's callback function poll for input
1809 if this becomes zero. */
1810 --poll_suppress_count;
1812 #endif
1815 /* Nonzero if we are using polling to handle input asynchronously. */
1818 input_polling_used ()
1820 #ifdef POLL_FOR_INPUT
1821 return read_socket_hook && !interrupt_input;
1822 #else
1823 return 0;
1824 #endif
1827 /* Turn off polling. */
1829 void
1830 stop_polling ()
1832 #ifdef POLL_FOR_INPUT
1833 if (read_socket_hook && !interrupt_input)
1834 ++poll_suppress_count;
1835 #endif
1838 /* Set the value of poll_suppress_count to COUNT
1839 and start or stop polling accordingly. */
1841 void
1842 set_poll_suppress_count (count)
1843 int count;
1845 #ifdef POLL_FOR_INPUT
1846 if (count == 0 && poll_suppress_count != 0)
1848 poll_suppress_count = 1;
1849 start_polling ();
1851 else if (count != 0 && poll_suppress_count == 0)
1853 stop_polling ();
1855 poll_suppress_count = count;
1856 #endif
1859 /* Bind polling_period to a value at least N.
1860 But don't decrease it. */
1862 void
1863 bind_polling_period (n)
1864 int n;
1866 #ifdef POLL_FOR_INPUT
1867 int new = polling_period;
1869 if (n > new)
1870 new = n;
1872 stop_other_atimers (poll_timer);
1873 stop_polling ();
1874 specbind (Qpolling_period, make_number (new));
1875 /* Start a new alarm with the new period. */
1876 start_polling ();
1877 #endif
1880 /* Apply the control modifier to CHARACTER. */
1883 make_ctrl_char (c)
1884 int c;
1886 /* Save the upper bits here. */
1887 int upper = c & ~0177;
1889 c &= 0177;
1891 /* Everything in the columns containing the upper-case letters
1892 denotes a control character. */
1893 if (c >= 0100 && c < 0140)
1895 int oc = c;
1896 c &= ~0140;
1897 /* Set the shift modifier for a control char
1898 made from a shifted letter. But only for letters! */
1899 if (oc >= 'A' && oc <= 'Z')
1900 c |= shift_modifier;
1903 /* The lower-case letters denote control characters too. */
1904 else if (c >= 'a' && c <= 'z')
1905 c &= ~0140;
1907 /* Include the bits for control and shift
1908 only if the basic ASCII code can't indicate them. */
1909 else if (c >= ' ')
1910 c |= ctrl_modifier;
1912 /* Replace the high bits. */
1913 c |= (upper & ~ctrl_modifier);
1915 return c;
1918 /* Display help echo in the echo area.
1920 HELP a string means display that string, HELP nil means clear the
1921 help echo. If HELP is a function, call it with OBJECT and POS as
1922 arguments; the function should return a help string or nil for
1923 none. For all other types of HELP evaluate it to obtain a string.
1925 WINDOW is the window in which the help was generated, if any.
1926 It is nil if not in a window.
1928 If OBJECT is a buffer, POS is the position in the buffer where the
1929 `help-echo' text property was found.
1931 If OBJECT is an overlay, that overlay has a `help-echo' property,
1932 and POS is the position in the overlay's buffer under the mouse.
1934 If OBJECT is a string (an overlay string or a string displayed with
1935 the `display' property). POS is the position in that string under
1936 the mouse.
1938 OK_TO_IVERWRITE_KEYSTROKE_ECHO non-zero means it's okay if the help
1939 echo overwrites a keystroke echo currently displayed in the echo
1940 area.
1942 Note: this function may only be called with HELP nil or a string
1943 from X code running asynchronously. */
1945 void
1946 show_help_echo (help, window, object, pos, ok_to_overwrite_keystroke_echo)
1947 Lisp_Object help, window, object, pos;
1948 int ok_to_overwrite_keystroke_echo;
1950 if (!NILP (help) && !STRINGP (help))
1952 if (FUNCTIONP (help))
1954 Lisp_Object args[4];
1955 args[0] = help;
1956 args[1] = window;
1957 args[2] = object;
1958 args[3] = pos;
1959 help = call_function (4, args);
1961 else
1962 help = eval_form (help);
1964 if (!STRINGP (help))
1965 return;
1968 if (STRINGP (help) || NILP (help))
1970 if (!NILP (Vshow_help_function))
1971 call1 (Vshow_help_function, help);
1972 else if (/* Don't overwrite minibuffer contents. */
1973 !MINI_WINDOW_P (XWINDOW (selected_window))
1974 /* Don't overwrite a keystroke echo. */
1975 && (NILP (echo_message_buffer)
1976 || ok_to_overwrite_keystroke_echo)
1977 /* Don't overwrite a prompt. */
1978 && !cursor_in_echo_area)
1980 if (STRINGP (help))
1982 int count = specpdl_ptr - specpdl;
1983 specbind (Qmessage_truncate_lines, Qt);
1984 message3_nolog (help, XSTRING (help)->size,
1985 STRING_MULTIBYTE (help));
1986 unbind_to (count, Qnil);
1988 else
1989 message (0);
1992 help_echo_showing_p = STRINGP (help);
1998 /* Input of single characters from keyboard */
2000 Lisp_Object print_help ();
2001 static Lisp_Object kbd_buffer_get_event ();
2002 static void record_char ();
2004 #ifdef MULTI_KBOARD
2005 static jmp_buf wrong_kboard_jmpbuf;
2006 #endif
2008 /* read a character from the keyboard; call the redisplay if needed */
2009 /* commandflag 0 means do not do auto-saving, but do do redisplay.
2010 -1 means do not do redisplay, but do do autosaving.
2011 1 means do both. */
2013 /* The arguments MAPS and NMAPS are for menu prompting.
2014 MAPS is an array of keymaps; NMAPS is the length of MAPS.
2016 PREV_EVENT is the previous input event, or nil if we are reading
2017 the first event of a key sequence (or not reading a key sequence).
2018 If PREV_EVENT is t, that is a "magic" value that says
2019 not to run input methods, but in other respects to act as if
2020 not reading a key sequence.
2022 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
2023 if we used a mouse menu to read the input, or zero otherwise. If
2024 USED_MOUSE_MENU is null, we don't dereference it.
2026 Value is t if we showed a menu and the user rejected it. */
2028 Lisp_Object
2029 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
2030 int commandflag;
2031 int nmaps;
2032 Lisp_Object *maps;
2033 Lisp_Object prev_event;
2034 int *used_mouse_menu;
2036 volatile Lisp_Object c;
2037 int count;
2038 jmp_buf local_getcjmp;
2039 jmp_buf save_jump;
2040 volatile int key_already_recorded = 0;
2041 Lisp_Object tem, save;
2042 volatile Lisp_Object previous_echo_area_message;
2043 volatile Lisp_Object also_record;
2044 volatile int reread;
2045 struct gcpro gcpro1, gcpro2;
2047 also_record = Qnil;
2049 before_command_key_count = this_command_key_count;
2050 before_command_echo_length = echo_length ();
2051 c = Qnil;
2052 previous_echo_area_message = Qnil;
2054 GCPRO2 (c, previous_echo_area_message);
2056 retry:
2058 reread = 0;
2059 if (CONSP (Vunread_post_input_method_events))
2061 c = XCAR (Vunread_post_input_method_events);
2062 Vunread_post_input_method_events
2063 = XCDR (Vunread_post_input_method_events);
2065 /* Undo what read_char_x_menu_prompt did when it unread
2066 additional keys returned by Fx_popup_menu. */
2067 if (CONSP (c)
2068 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2069 && NILP (XCDR (c)))
2070 c = XCAR (c);
2072 reread = 1;
2073 goto reread_first;
2076 if (unread_command_char != -1)
2078 XSETINT (c, unread_command_char);
2079 unread_command_char = -1;
2081 reread = 1;
2082 goto reread_first;
2085 if (CONSP (Vunread_command_events))
2087 c = XCAR (Vunread_command_events);
2088 Vunread_command_events = XCDR (Vunread_command_events);
2090 /* Undo what read_char_x_menu_prompt did when it unread
2091 additional keys returned by Fx_popup_menu. */
2092 if (CONSP (c)
2093 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2094 && NILP (XCDR (c)))
2095 c = XCAR (c);
2097 reread = 1;
2098 goto reread_for_input_method;
2101 if (CONSP (Vunread_input_method_events))
2103 c = XCAR (Vunread_input_method_events);
2104 Vunread_input_method_events = XCDR (Vunread_input_method_events);
2106 /* Undo what read_char_x_menu_prompt did when it unread
2107 additional keys returned by Fx_popup_menu. */
2108 if (CONSP (c)
2109 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2110 && NILP (XCDR (c)))
2111 c = XCAR (c);
2112 reread = 1;
2113 goto reread_for_input_method;
2116 /* If there is no function key translated before
2117 reset-this-command-lengths takes effect, forget about it. */
2118 before_command_restore_flag = 0;
2120 if (!NILP (Vexecuting_macro))
2122 /* We set this to Qmacro; since that's not a frame, nobody will
2123 try to switch frames on us, and the selected window will
2124 remain unchanged.
2126 Since this event came from a macro, it would be misleading to
2127 leave internal_last_event_frame set to wherever the last
2128 real event came from. Normally, a switch-frame event selects
2129 internal_last_event_frame after each command is read, but
2130 events read from a macro should never cause a new frame to be
2131 selected. */
2132 Vlast_event_frame = internal_last_event_frame = Qmacro;
2134 /* Exit the macro if we are at the end.
2135 Also, some things replace the macro with t
2136 to force an early exit. */
2137 if (EQ (Vexecuting_macro, Qt)
2138 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
2140 XSETINT (c, -1);
2141 RETURN_UNGCPRO (c);
2144 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
2145 if (STRINGP (Vexecuting_macro)
2146 && (XINT (c) & 0x80))
2147 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
2149 executing_macro_index++;
2151 goto from_macro;
2154 if (!NILP (unread_switch_frame))
2156 c = unread_switch_frame;
2157 unread_switch_frame = Qnil;
2159 /* This event should make it into this_command_keys, and get echoed
2160 again, so we do not set `reread'. */
2161 goto reread_first;
2164 /* if redisplay was requested */
2165 if (commandflag >= 0)
2167 /* If there is pending input, process any events which are not
2168 user-visible, such as X selection_request events. */
2169 if (input_pending
2170 || detect_input_pending_run_timers (0))
2171 swallow_events (0); /* may clear input_pending */
2173 /* Redisplay if no pending input. */
2174 while (!input_pending)
2176 if (help_echo_showing_p && !EQ (selected_window, minibuf_window))
2177 redisplay_preserve_echo_area ();
2178 else
2179 redisplay ();
2181 if (!input_pending)
2182 /* Normal case: no input arrived during redisplay. */
2183 break;
2185 /* Input arrived and pre-empted redisplay.
2186 Process any events which are not user-visible. */
2187 swallow_events (0);
2188 /* If that cleared input_pending, try again to redisplay. */
2192 /* Message turns off echoing unless more keystrokes turn it on again.
2194 The code in 20.x for the condition was
2196 1. echo_area_glyphs && *echo_area_glyphs
2197 2. && echo_area_glyphs != current_kboard->echobuf
2198 3. && ok_to_echo_at_next_pause != echo_area_glyphs
2200 (1) means there's a current message displayed
2202 (2) means it's not the message from echoing from the current
2203 kboard.
2205 (3) There's only one place in 20.x where ok_to_echo_at_next_pause
2206 is set to a non-null value. This is done in read_char and it is
2207 set to echo_area_glyphs after a call to echo_char. That means
2208 ok_to_echo_at_next_pause is either null or
2209 current_kboard->echobuf with the appropriate current_kboard at
2210 that time.
2212 So, condition (3) means in clear text ok_to_echo_at_next_pause
2213 must be either null, or the current message isn't from echoing at
2214 all, or it's from echoing from a different kboard than the
2215 current one. */
2217 if (/* There currently something in the echo area */
2218 !NILP (echo_area_buffer[0])
2219 && (/* And it's either not from echoing. */
2220 !EQ (echo_area_buffer[0], echo_message_buffer)
2221 /* Or it's an echo from a different kboard. */
2222 || echo_kboard != current_kboard
2223 /* Or we explicitly allow overwriting whatever there is. */
2224 || ok_to_echo_at_next_pause == NULL))
2225 cancel_echoing ();
2226 else
2227 echo_dash ();
2229 /* Try reading a character via menu prompting in the minibuf.
2230 Try this before the sit-for, because the sit-for
2231 would do the wrong thing if we are supposed to do
2232 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
2233 after a mouse event so don't try a minibuf menu. */
2234 c = Qnil;
2235 if (nmaps > 0 && INTERACTIVE
2236 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
2237 /* Don't bring up a menu if we already have another event. */
2238 && NILP (Vunread_command_events)
2239 && unread_command_char < 0
2240 && !detect_input_pending_run_timers (0))
2242 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
2243 if (! NILP (c))
2245 key_already_recorded = 1;
2246 goto non_reread_1;
2250 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
2251 We will do that below, temporarily for short sections of code,
2252 when appropriate. local_getcjmp must be in effect
2253 around any call to sit_for or kbd_buffer_get_event;
2254 it *must not* be in effect when we call redisplay. */
2256 if (_setjmp (local_getcjmp))
2258 XSETINT (c, quit_char);
2259 internal_last_event_frame = selected_frame;
2260 Vlast_event_frame = internal_last_event_frame;
2261 /* If we report the quit char as an event,
2262 don't do so more than once. */
2263 if (!NILP (Vinhibit_quit))
2264 Vquit_flag = Qnil;
2266 #ifdef MULTI_KBOARD
2268 KBOARD *kb = FRAME_KBOARD (XFRAME (selected_frame));
2269 if (kb != current_kboard)
2271 Lisp_Object *tailp = &kb->kbd_queue;
2272 /* We shouldn't get here if we were in single-kboard mode! */
2273 if (single_kboard)
2274 abort ();
2275 while (CONSP (*tailp))
2276 tailp = &XCDR (*tailp);
2277 if (!NILP (*tailp))
2278 abort ();
2279 *tailp = Fcons (c, Qnil);
2280 kb->kbd_queue_has_data = 1;
2281 current_kboard = kb;
2282 /* This is going to exit from read_char
2283 so we had better get rid of this frame's stuff. */
2284 UNGCPRO;
2285 longjmp (wrong_kboard_jmpbuf, 1);
2288 #endif
2289 goto non_reread;
2292 timer_start_idle ();
2294 /* If in middle of key sequence and minibuffer not active,
2295 start echoing if enough time elapses. */
2297 if (minibuf_level == 0
2298 && !current_kboard->immediate_echo
2299 && this_command_key_count > 0
2300 && ! noninteractive
2301 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2302 && NILP (Fzerop (Vecho_keystrokes))
2303 && (/* No message. */
2304 NILP (echo_area_buffer[0])
2305 /* Or empty message. */
2306 || (BUF_BEG (XBUFFER (echo_area_buffer[0]))
2307 == BUF_Z (XBUFFER (echo_area_buffer[0])))
2308 /* Or already echoing from same kboard. */
2309 || (echo_kboard && ok_to_echo_at_next_pause == echo_kboard)
2310 /* Or not echoing before and echoing allowed. */
2311 || (!echo_kboard && ok_to_echo_at_next_pause)))
2313 Lisp_Object tem0;
2315 /* After a mouse event, start echoing right away.
2316 This is because we are probably about to display a menu,
2317 and we don't want to delay before doing so. */
2318 if (EVENT_HAS_PARAMETERS (prev_event))
2319 echo_now ();
2320 else
2322 int sec, usec;
2323 double duration = extract_float (Vecho_keystrokes);
2324 sec = (int) duration;
2325 usec = (duration - sec) * 1000000;
2326 save_getcjmp (save_jump);
2327 restore_getcjmp (local_getcjmp);
2328 tem0 = sit_for (sec, usec, 1, 1, 0);
2329 restore_getcjmp (save_jump);
2330 if (EQ (tem0, Qt)
2331 && ! CONSP (Vunread_command_events))
2332 echo_now ();
2336 /* Maybe auto save due to number of keystrokes. */
2338 if (commandflag != 0
2339 && auto_save_interval > 0
2340 && num_nonmacro_input_events - last_auto_save > max (auto_save_interval, 20)
2341 && !detect_input_pending_run_timers (0))
2343 Fdo_auto_save (Qnil, Qnil);
2344 /* Hooks can actually change some buffers in auto save. */
2345 redisplay ();
2348 /* Try reading using an X menu.
2349 This is never confused with reading using the minibuf
2350 because the recursive call of read_char in read_char_minibuf_menu_prompt
2351 does not pass on any keymaps. */
2353 if (nmaps > 0 && INTERACTIVE
2354 && !NILP (prev_event)
2355 && EVENT_HAS_PARAMETERS (prev_event)
2356 && !EQ (XCAR (prev_event), Qmenu_bar)
2357 && !EQ (XCAR (prev_event), Qtool_bar)
2358 /* Don't bring up a menu if we already have another event. */
2359 && NILP (Vunread_command_events)
2360 && unread_command_char < 0)
2362 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
2364 /* Now that we have read an event, Emacs is not idle. */
2365 timer_stop_idle ();
2367 RETURN_UNGCPRO (c);
2370 /* Maybe autosave and/or garbage collect due to idleness. */
2372 if (INTERACTIVE && NILP (c))
2374 int delay_level, buffer_size;
2376 /* Slow down auto saves logarithmically in size of current buffer,
2377 and garbage collect while we're at it. */
2378 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
2379 last_non_minibuf_size = Z - BEG;
2380 buffer_size = (last_non_minibuf_size >> 8) + 1;
2381 delay_level = 0;
2382 while (buffer_size > 64)
2383 delay_level++, buffer_size -= buffer_size >> 2;
2384 if (delay_level < 4) delay_level = 4;
2385 /* delay_level is 4 for files under around 50k, 7 at 100k,
2386 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
2388 /* Auto save if enough time goes by without input. */
2389 if (commandflag != 0
2390 && num_nonmacro_input_events > last_auto_save
2391 && INTEGERP (Vauto_save_timeout)
2392 && XINT (Vauto_save_timeout) > 0)
2394 Lisp_Object tem0;
2396 save_getcjmp (save_jump);
2397 restore_getcjmp (local_getcjmp);
2398 tem0 = sit_for (delay_level * XFASTINT (Vauto_save_timeout) / 4,
2399 0, 1, 1, 0);
2400 restore_getcjmp (save_jump);
2402 if (EQ (tem0, Qt)
2403 && ! CONSP (Vunread_command_events))
2405 Fdo_auto_save (Qnil, Qnil);
2407 /* If we have auto-saved and there is still no input
2408 available, garbage collect if there has been enough
2409 consing going on to make it worthwhile. */
2410 if (!detect_input_pending_run_timers (0)
2411 && consing_since_gc > gc_cons_threshold / 2)
2412 Fgarbage_collect ();
2414 redisplay ();
2419 /* If this has become non-nil here, it has been set by a timer
2420 or sentinel or filter. */
2421 if (CONSP (Vunread_command_events))
2423 c = XCAR (Vunread_command_events);
2424 Vunread_command_events = XCDR (Vunread_command_events);
2427 /* Read something from current KBOARD's side queue, if possible. */
2429 if (NILP (c))
2431 if (current_kboard->kbd_queue_has_data)
2433 if (!CONSP (current_kboard->kbd_queue))
2434 abort ();
2435 c = XCAR (current_kboard->kbd_queue);
2436 current_kboard->kbd_queue
2437 = XCDR (current_kboard->kbd_queue);
2438 if (NILP (current_kboard->kbd_queue))
2439 current_kboard->kbd_queue_has_data = 0;
2440 input_pending = readable_events (0);
2441 if (EVENT_HAS_PARAMETERS (c)
2442 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
2443 internal_last_event_frame = XCAR (XCDR (c));
2444 Vlast_event_frame = internal_last_event_frame;
2448 #ifdef MULTI_KBOARD
2449 /* If current_kboard's side queue is empty check the other kboards.
2450 If one of them has data that we have not yet seen here,
2451 switch to it and process the data waiting for it.
2453 Note: if the events queued up for another kboard
2454 have already been seen here, and therefore are not a complete command,
2455 the kbd_queue_has_data field is 0, so we skip that kboard here.
2456 That's to avoid an infinite loop switching between kboards here. */
2457 if (NILP (c) && !single_kboard)
2459 KBOARD *kb;
2460 for (kb = all_kboards; kb; kb = kb->next_kboard)
2461 if (kb->kbd_queue_has_data)
2463 current_kboard = kb;
2464 /* This is going to exit from read_char
2465 so we had better get rid of this frame's stuff. */
2466 UNGCPRO;
2467 longjmp (wrong_kboard_jmpbuf, 1);
2470 #endif
2472 wrong_kboard:
2474 stop_polling ();
2476 /* Finally, we read from the main queue,
2477 and if that gives us something we can't use yet, we put it on the
2478 appropriate side queue and try again. */
2480 if (NILP (c))
2482 KBOARD *kb;
2484 /* Actually read a character, waiting if necessary. */
2485 save_getcjmp (save_jump);
2486 restore_getcjmp (local_getcjmp);
2487 c = kbd_buffer_get_event (&kb, used_mouse_menu);
2488 restore_getcjmp (save_jump);
2490 #ifdef MULTI_KBOARD
2491 if (! NILP (c) && (kb != current_kboard))
2493 Lisp_Object *tailp = &kb->kbd_queue;
2494 while (CONSP (*tailp))
2495 tailp = &XCDR (*tailp);
2496 if (!NILP (*tailp))
2497 abort ();
2498 *tailp = Fcons (c, Qnil);
2499 kb->kbd_queue_has_data = 1;
2500 c = Qnil;
2501 if (single_kboard)
2502 goto wrong_kboard;
2503 current_kboard = kb;
2504 /* This is going to exit from read_char
2505 so we had better get rid of this frame's stuff. */
2506 UNGCPRO;
2507 longjmp (wrong_kboard_jmpbuf, 1);
2509 #endif
2512 /* Terminate Emacs in batch mode if at eof. */
2513 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
2514 Fkill_emacs (make_number (1));
2516 if (INTEGERP (c))
2518 /* Add in any extra modifiers, where appropriate. */
2519 if ((extra_keyboard_modifiers & CHAR_CTL)
2520 || ((extra_keyboard_modifiers & 0177) < ' '
2521 && (extra_keyboard_modifiers & 0177) != 0))
2522 XSETINT (c, make_ctrl_char (XINT (c)));
2524 /* Transfer any other modifier bits directly from
2525 extra_keyboard_modifiers to c. Ignore the actual character code
2526 in the low 16 bits of extra_keyboard_modifiers. */
2527 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
2530 non_reread:
2532 timer_stop_idle ();
2534 start_polling ();
2536 if (NILP (c))
2538 if (commandflag >= 0
2539 && !input_pending && !detect_input_pending_run_timers (0))
2540 redisplay ();
2542 goto wrong_kboard;
2545 non_reread_1:
2547 /* Buffer switch events are only for internal wakeups
2548 so don't show them to the user.
2549 Also, don't record a key if we already did. */
2550 if (BUFFERP (c) || key_already_recorded)
2551 RETURN_UNGCPRO (c);
2553 /* Process special events within read_char
2554 and loop around to read another event. */
2555 save = Vquit_flag;
2556 Vquit_flag = Qnil;
2557 tem = get_keyelt (access_keymap (get_keymap_1 (Vspecial_event_map, 0, 0),
2558 c, 0, 0), 1);
2559 Vquit_flag = save;
2561 if (!NILP (tem))
2563 int was_locked = single_kboard;
2565 last_input_char = c;
2566 Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt);
2568 /* Resume allowing input from any kboard, if that was true before. */
2569 if (!was_locked)
2570 any_kboard_state ();
2572 goto retry;
2575 /* Handle things that only apply to characters. */
2576 if (INTEGERP (c))
2578 /* If kbd_buffer_get_event gave us an EOF, return that. */
2579 if (XINT (c) == -1)
2580 RETURN_UNGCPRO (c);
2582 if ((STRINGP (Vkeyboard_translate_table)
2583 && XSTRING (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2584 || (VECTORP (Vkeyboard_translate_table)
2585 && XVECTOR (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2586 || (CHAR_TABLE_P (Vkeyboard_translate_table)
2587 && CHAR_TABLE_ORDINARY_SLOTS > (unsigned) XFASTINT (c)))
2589 Lisp_Object d;
2590 d = Faref (Vkeyboard_translate_table, c);
2591 /* nil in keyboard-translate-table means no translation. */
2592 if (!NILP (d))
2593 c = d;
2597 /* If this event is a mouse click in the menu bar,
2598 return just menu-bar for now. Modify the mouse click event
2599 so we won't do this twice, then queue it up. */
2600 if (EVENT_HAS_PARAMETERS (c)
2601 && CONSP (XCDR (c))
2602 && CONSP (EVENT_START (c))
2603 && CONSP (XCDR (EVENT_START (c))))
2605 Lisp_Object posn;
2607 posn = POSN_BUFFER_POSN (EVENT_START (c));
2608 /* Handle menu-bar events:
2609 insert the dummy prefix event `menu-bar'. */
2610 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
2612 /* Change menu-bar to (menu-bar) as the event "position". */
2613 POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
2615 also_record = c;
2616 Vunread_command_events = Fcons (c, Vunread_command_events);
2617 c = posn;
2621 /* Store these characters into recent_keys, the dribble file if any,
2622 and the keyboard macro being defined, if any. */
2623 record_char (c);
2624 if (! NILP (also_record))
2625 record_char (also_record);
2627 /* Wipe the echo area.
2628 But first, if we are about to use an input method,
2629 save the echo area contents for it to refer to. */
2630 if (INTEGERP (c)
2631 && ! NILP (Vinput_method_function)
2632 && (unsigned) XINT (c) >= ' '
2633 && (unsigned) XINT (c) != 127
2634 && (unsigned) XINT (c) < 256)
2636 previous_echo_area_message = Fcurrent_message ();
2637 Vinput_method_previous_message = previous_echo_area_message;
2640 /* Now wipe the echo area, except for help events which do their
2641 own stuff with the echo area. */
2642 if (!CONSP (c) || !(EQ (Qhelp_echo, XCAR (c))))
2644 if (!NILP (echo_area_buffer[0]))
2645 safe_run_hooks (Qecho_area_clear_hook);
2646 clear_message (1, 0);
2649 reread_for_input_method:
2650 from_macro:
2651 /* Pass this to the input method, if appropriate. */
2652 if (INTEGERP (c)
2653 && ! NILP (Vinput_method_function)
2654 /* Don't run the input method within a key sequence,
2655 after the first event of the key sequence. */
2656 && NILP (prev_event)
2657 && (unsigned) XINT (c) >= ' '
2658 && (unsigned) XINT (c) != 127
2659 && (unsigned) XINT (c) < 256)
2661 Lisp_Object keys;
2662 int key_count;
2663 struct gcpro gcpro1;
2664 int count = specpdl_ptr - specpdl;
2666 /* Save the echo status. */
2667 int saved_immediate_echo = current_kboard->immediate_echo;
2668 struct kboard *saved_ok_to_echo = ok_to_echo_at_next_pause;
2669 int saved_echo_after_prompt = current_kboard->echo_after_prompt;
2671 if (before_command_restore_flag)
2673 this_command_key_count = before_command_key_count_1;
2674 if (this_command_key_count < this_single_command_key_start)
2675 this_single_command_key_start = this_command_key_count;
2676 echo_truncate (before_command_echo_length_1);
2677 before_command_restore_flag = 0;
2680 /* Save the this_command_keys status. */
2681 key_count = this_command_key_count;
2683 if (key_count > 0)
2684 keys = Fcopy_sequence (this_command_keys);
2685 else
2686 keys = Qnil;
2687 GCPRO1 (keys);
2689 /* Clear out this_command_keys. */
2690 this_command_key_count = 0;
2692 /* Now wipe the echo area. */
2693 if (!NILP (echo_area_buffer[0]))
2694 safe_run_hooks (Qecho_area_clear_hook);
2695 clear_message (1, 0);
2696 echo_truncate (0);
2698 /* If we are not reading a key sequence,
2699 never use the echo area. */
2700 if (maps == 0)
2702 specbind (Qinput_method_use_echo_area, Qt);
2705 /* Call the input method. */
2706 tem = call1 (Vinput_method_function, c);
2708 tem = unbind_to (count, tem);
2710 /* Restore the saved echoing state
2711 and this_command_keys state. */
2712 this_command_key_count = key_count;
2713 if (key_count > 0)
2714 this_command_keys = keys;
2716 cancel_echoing ();
2717 ok_to_echo_at_next_pause = saved_ok_to_echo;
2718 current_kboard->echo_after_prompt = saved_echo_after_prompt;
2719 if (saved_immediate_echo)
2720 echo_now ();
2722 UNGCPRO;
2724 /* The input method can return no events. */
2725 if (! CONSP (tem))
2727 /* Bring back the previous message, if any. */
2728 if (! NILP (previous_echo_area_message))
2729 message_with_string ("%s", previous_echo_area_message, 0);
2730 goto retry;
2732 /* It returned one event or more. */
2733 c = XCAR (tem);
2734 Vunread_post_input_method_events
2735 = nconc2 (XCDR (tem), Vunread_post_input_method_events);
2738 reread_first:
2740 /* Display help if not echoing. */
2741 if (CONSP (c) && EQ (XCAR (c), Qhelp_echo))
2743 /* (help-echo FRAME HELP WINDOW OBJECT POS). */
2744 Lisp_Object help, object, position, window;
2745 help = Fnth (make_number (2), c);
2746 window = Fnth (make_number (3), c);
2747 object = Fnth (make_number (4), c);
2748 position = Fnth (make_number (5), c);
2749 show_help_echo (help, window, object, position, 0);
2750 goto retry;
2753 if (this_command_key_count == 0 || ! reread)
2755 before_command_key_count = this_command_key_count;
2756 before_command_echo_length = echo_length ();
2758 /* Don't echo mouse motion events. */
2759 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2760 && NILP (Fzerop (Vecho_keystrokes))
2761 && ! (EVENT_HAS_PARAMETERS (c)
2762 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
2764 echo_char (c);
2765 if (! NILP (also_record))
2766 echo_char (also_record);
2767 /* Once we reread a character, echoing can happen
2768 the next time we pause to read a new one. */
2769 ok_to_echo_at_next_pause = current_kboard;
2772 /* Record this character as part of the current key. */
2773 add_command_key (c);
2774 if (! NILP (also_record))
2775 add_command_key (also_record);
2778 last_input_char = c;
2779 num_input_events++;
2781 /* Process the help character specially if enabled */
2782 if (!NILP (Vhelp_form) && help_char_p (c))
2784 Lisp_Object tem0;
2785 count = specpdl_ptr - specpdl;
2787 record_unwind_protect (Fset_window_configuration,
2788 Fcurrent_window_configuration (Qnil));
2790 tem0 = Feval (Vhelp_form);
2791 if (STRINGP (tem0))
2792 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
2794 cancel_echoing ();
2796 c = read_char (0, 0, 0, Qnil, 0);
2797 while (BUFFERP (c));
2798 /* Remove the help from the frame */
2799 unbind_to (count, Qnil);
2801 redisplay ();
2802 if (EQ (c, make_number (040)))
2804 cancel_echoing ();
2806 c = read_char (0, 0, 0, Qnil, 0);
2807 while (BUFFERP (c));
2811 RETURN_UNGCPRO (c);
2814 /* Record a key that came from a mouse menu.
2815 Record it for echoing, for this-command-keys, and so on. */
2817 static void
2818 record_menu_key (c)
2819 Lisp_Object c;
2821 /* Wipe the echo area. */
2822 clear_message (1, 0);
2824 record_char (c);
2826 before_command_key_count = this_command_key_count;
2827 before_command_echo_length = echo_length ();
2829 /* Don't echo mouse motion events. */
2830 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2831 && NILP (Fzerop (Vecho_keystrokes)))
2833 echo_char (c);
2835 /* Once we reread a character, echoing can happen
2836 the next time we pause to read a new one. */
2837 ok_to_echo_at_next_pause = 0;
2840 /* Record this character as part of the current key. */
2841 add_command_key (c);
2843 /* Re-reading in the middle of a command */
2844 last_input_char = c;
2845 num_input_events++;
2848 /* Return 1 if should recognize C as "the help character". */
2851 help_char_p (c)
2852 Lisp_Object c;
2854 Lisp_Object tail;
2856 if (EQ (c, Vhelp_char))
2857 return 1;
2858 for (tail = Vhelp_event_list; CONSP (tail); tail = XCDR (tail))
2859 if (EQ (c, XCAR (tail)))
2860 return 1;
2861 return 0;
2864 /* Record the input event C in various ways. */
2866 static void
2867 record_char (c)
2868 Lisp_Object c;
2870 total_keys++;
2871 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
2872 if (++recent_keys_index >= NUM_RECENT_KEYS)
2873 recent_keys_index = 0;
2875 /* Write c to the dribble file. If c is a lispy event, write
2876 the event's symbol to the dribble file, in <brackets>. Bleaugh.
2877 If you, dear reader, have a better idea, you've got the source. :-) */
2878 if (dribble)
2880 if (INTEGERP (c))
2882 if (XUINT (c) < 0x100)
2883 putc (XINT (c), dribble);
2884 else
2885 fprintf (dribble, " 0x%x", (int) XUINT (c));
2887 else
2889 Lisp_Object dribblee;
2891 /* If it's a structured event, take the event header. */
2892 dribblee = EVENT_HEAD (c);
2894 if (SYMBOLP (dribblee))
2896 putc ('<', dribble);
2897 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
2898 STRING_BYTES (XSYMBOL (dribblee)->name),
2899 dribble);
2900 putc ('>', dribble);
2904 fflush (dribble);
2907 store_kbd_macro_char (c);
2909 num_nonmacro_input_events++;
2912 Lisp_Object
2913 print_help (object)
2914 Lisp_Object object;
2916 struct buffer *old = current_buffer;
2917 Fprinc (object, Qnil);
2918 set_buffer_internal (XBUFFER (Vstandard_output));
2919 call0 (intern ("help-mode"));
2920 set_buffer_internal (old);
2921 return Qnil;
2924 /* Copy out or in the info on where C-g should throw to.
2925 This is used when running Lisp code from within get_char,
2926 in case get_char is called recursively.
2927 See read_process_output. */
2929 static void
2930 save_getcjmp (temp)
2931 jmp_buf temp;
2933 bcopy (getcjmp, temp, sizeof getcjmp);
2936 static void
2937 restore_getcjmp (temp)
2938 jmp_buf temp;
2940 bcopy (temp, getcjmp, sizeof getcjmp);
2943 #ifdef HAVE_MOUSE
2945 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
2946 of this function. */
2948 static Lisp_Object
2949 tracking_off (old_value)
2950 Lisp_Object old_value;
2952 do_mouse_tracking = old_value;
2953 if (NILP (old_value))
2955 /* Redisplay may have been preempted because there was input
2956 available, and it assumes it will be called again after the
2957 input has been processed. If the only input available was
2958 the sort that we have just disabled, then we need to call
2959 redisplay. */
2960 if (!readable_events (1))
2962 redisplay_preserve_echo_area ();
2963 get_input_pending (&input_pending, 1);
2966 return Qnil;
2969 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
2970 "Evaluate BODY with mouse movement events enabled.\n\
2971 Within a `track-mouse' form, mouse motion generates input events that\n\
2972 you can read with `read-event'.\n\
2973 Normally, mouse motion is ignored.")
2974 (args)
2975 Lisp_Object args;
2977 int count = specpdl_ptr - specpdl;
2978 Lisp_Object val;
2980 record_unwind_protect (tracking_off, do_mouse_tracking);
2982 do_mouse_tracking = Qt;
2984 val = Fprogn (args);
2985 return unbind_to (count, val);
2988 /* If mouse has moved on some frame, return one of those frames.
2989 Return 0 otherwise. */
2991 static FRAME_PTR
2992 some_mouse_moved ()
2994 Lisp_Object tail, frame;
2996 FOR_EACH_FRAME (tail, frame)
2998 if (XFRAME (frame)->mouse_moved)
2999 return XFRAME (frame);
3002 return 0;
3005 #endif /* HAVE_MOUSE */
3007 /* Low level keyboard/mouse input.
3008 kbd_buffer_store_event places events in kbd_buffer, and
3009 kbd_buffer_get_event retrieves them. */
3011 /* Return true iff there are any events in the queue that read-char
3012 would return. If this returns false, a read-char would block. */
3013 static int
3014 readable_events (do_timers_now)
3015 int do_timers_now;
3017 if (do_timers_now)
3018 timer_check (do_timers_now);
3020 if (kbd_fetch_ptr != kbd_store_ptr)
3021 return 1;
3022 #ifdef HAVE_MOUSE
3023 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3024 return 1;
3025 #endif
3026 if (single_kboard)
3028 if (current_kboard->kbd_queue_has_data)
3029 return 1;
3031 else
3033 KBOARD *kb;
3034 for (kb = all_kboards; kb; kb = kb->next_kboard)
3035 if (kb->kbd_queue_has_data)
3036 return 1;
3038 return 0;
3041 /* Set this for debugging, to have a way to get out */
3042 int stop_character;
3044 #ifdef MULTI_KBOARD
3045 static KBOARD *
3046 event_to_kboard (event)
3047 struct input_event *event;
3049 Lisp_Object frame;
3050 frame = event->frame_or_window;
3051 if (CONSP (frame))
3052 frame = XCAR (frame);
3053 else if (WINDOWP (frame))
3054 frame = WINDOW_FRAME (XWINDOW (frame));
3056 /* There are still some events that don't set this field.
3057 For now, just ignore the problem.
3058 Also ignore dead frames here. */
3059 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
3060 return 0;
3061 else
3062 return FRAME_KBOARD (XFRAME (frame));
3064 #endif
3066 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
3068 void
3069 kbd_buffer_store_event (event)
3070 register struct input_event *event;
3072 if (event->kind == no_event)
3073 abort ();
3075 if (event->kind == ascii_keystroke)
3077 register int c = event->code & 0377;
3079 if (event->modifiers & ctrl_modifier)
3080 c = make_ctrl_char (c);
3082 c |= (event->modifiers
3083 & (meta_modifier | alt_modifier
3084 | hyper_modifier | super_modifier));
3086 if (c == quit_char)
3088 extern SIGTYPE interrupt_signal ();
3089 #ifdef MULTI_KBOARD
3090 KBOARD *kb;
3091 struct input_event *sp;
3093 if (single_kboard
3094 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
3095 kb != current_kboard))
3097 kb->kbd_queue
3098 = Fcons (make_lispy_switch_frame (event->frame_or_window),
3099 Fcons (make_number (c), Qnil));
3100 kb->kbd_queue_has_data = 1;
3101 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3103 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3104 sp = kbd_buffer;
3106 if (event_to_kboard (sp) == kb)
3108 sp->kind = no_event;
3109 sp->frame_or_window = Qnil;
3110 sp->arg = Qnil;
3113 return;
3115 #endif
3117 /* If this results in a quit_char being returned to Emacs as
3118 input, set Vlast_event_frame properly. If this doesn't
3119 get returned to Emacs as an event, the next event read
3120 will set Vlast_event_frame again, so this is safe to do. */
3122 Lisp_Object focus;
3124 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
3125 if (NILP (focus))
3126 focus = event->frame_or_window;
3127 internal_last_event_frame = focus;
3128 Vlast_event_frame = focus;
3131 last_event_timestamp = event->timestamp;
3132 interrupt_signal ();
3133 return;
3136 if (c && c == stop_character)
3138 sys_suspend ();
3139 return;
3142 /* Don't insert two buffer_switch_event's in a row.
3143 Just ignore the second one. */
3144 else if (event->kind == buffer_switch_event
3145 && kbd_fetch_ptr != kbd_store_ptr
3146 && kbd_store_ptr->kind == buffer_switch_event)
3147 return;
3149 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
3150 kbd_store_ptr = kbd_buffer;
3152 /* Don't let the very last slot in the buffer become full,
3153 since that would make the two pointers equal,
3154 and that is indistinguishable from an empty buffer.
3155 Discard the event if it would fill the last slot. */
3156 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
3158 int idx;
3160 #if 0 /* The selection_request_event case looks bogus, and it's error
3161 prone to assign individual members for other events, in case
3162 the input_event structure is changed. --2000-07-13, gerd. */
3163 struct input_event *sp = kbd_store_ptr;
3164 sp->kind = event->kind;
3165 if (event->kind == selection_request_event)
3167 /* We must not use the ordinary copying code for this case,
3168 since `part' is an enum and copying it might not copy enough
3169 in this case. */
3170 bcopy (event, (char *) sp, sizeof (*event));
3172 else
3175 sp->code = event->code;
3176 sp->part = event->part;
3177 sp->frame_or_window = event->frame_or_window;
3178 sp->arg = event->arg;
3179 sp->modifiers = event->modifiers;
3180 sp->x = event->x;
3181 sp->y = event->y;
3182 sp->timestamp = event->timestamp;
3184 #else
3185 *kbd_store_ptr = *event;
3186 #endif
3188 idx = 2 * (kbd_store_ptr - kbd_buffer);
3189 ASET (kbd_buffer_gcpro, idx, event->frame_or_window);
3190 ASET (kbd_buffer_gcpro, idx + 1, event->arg);
3191 ++kbd_store_ptr;
3196 /* Generate HELP_EVENT input_events in BUFP which has room for
3197 SIZE events. If there's not enough room in BUFP, ignore this
3198 event.
3200 HELP is the help form.
3202 FRAME is the frame on which the help is generated. OBJECT is the
3203 Lisp object where the help was found (a buffer, a string, an
3204 overlay, or nil if neither from a string nor from a buffer. POS is
3205 the position within OBJECT where the help was found.
3207 Value is the number of input_events generated. */
3210 gen_help_event (bufp, size, help, frame, window, object, pos)
3211 struct input_event *bufp;
3212 int size;
3213 Lisp_Object help, frame, object, window;
3214 int pos;
3216 int nevents_stored = 0;
3218 if (size >= 2)
3220 bufp->kind = HELP_EVENT;
3221 bufp->frame_or_window = frame;
3222 bufp->arg = object;
3223 bufp->x = make_number (pos);
3224 bufp->code = 0;
3226 ++bufp;
3227 bufp->kind = HELP_EVENT;
3228 bufp->frame_or_window = WINDOWP (window) ? window : frame;
3229 bufp->arg = help;
3230 bufp->code = 1;
3231 nevents_stored = 2;
3234 return nevents_stored;
3238 /* Store HELP_EVENTs for HELP on FRAME in the input queue. */
3240 void
3241 kbd_buffer_store_help_event (frame, help)
3242 Lisp_Object frame, help;
3244 struct input_event event;
3246 event.kind = HELP_EVENT;
3247 event.frame_or_window = frame;
3248 event.arg = Qnil;
3249 event.x = make_number (0);
3250 event.code = 0;
3251 kbd_buffer_store_event (&event);
3253 event.kind = HELP_EVENT;
3254 event.frame_or_window = frame;
3255 event.arg = help;
3256 event.x = make_number (0);
3257 event.code = 1;
3258 kbd_buffer_store_event (&event);
3262 /* Discard any mouse events in the event buffer by setting them to
3263 no_event. */
3264 void
3265 discard_mouse_events ()
3267 struct input_event *sp;
3268 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3270 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3271 sp = kbd_buffer;
3273 if (sp->kind == mouse_click
3274 #ifdef WINDOWSNT
3275 || sp->kind == w32_scroll_bar_click
3276 #endif
3277 || sp->kind == scroll_bar_click)
3279 sp->kind = no_event;
3285 /* Return non-zero if there are any real events waiting in the event
3286 buffer, not counting `no_event's.
3288 If DISCARD is non-zero, discard no_event events at the front of
3289 the input queue, possibly leaving the input queue empty if there
3290 are no real input events. */
3293 kbd_buffer_events_waiting (discard)
3294 int discard;
3296 struct input_event *sp;
3298 for (sp = kbd_fetch_ptr;
3299 sp != kbd_store_ptr && sp->kind == no_event;
3300 ++sp)
3302 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3303 sp = kbd_buffer;
3306 if (discard)
3307 kbd_fetch_ptr = sp;
3309 return sp != kbd_store_ptr && sp->kind != no_event;
3313 /* Clear input event EVENT. */
3315 static INLINE void
3316 clear_event (event)
3317 struct input_event *event;
3319 int idx = 2 * (event - kbd_buffer);
3320 ASET (kbd_buffer_gcpro, idx, Qnil);
3321 ASET (kbd_buffer_gcpro, idx + 1, Qnil);
3322 event->kind = no_event;
3326 /* Read one event from the event buffer, waiting if necessary.
3327 The value is a Lisp object representing the event.
3328 The value is nil for an event that should be ignored,
3329 or that was handled here.
3330 We always read and discard one event. */
3332 static Lisp_Object
3333 kbd_buffer_get_event (kbp, used_mouse_menu)
3334 KBOARD **kbp;
3335 int *used_mouse_menu;
3337 register int c;
3338 Lisp_Object obj;
3340 if (noninteractive)
3342 c = getchar ();
3343 XSETINT (obj, c);
3344 *kbp = current_kboard;
3345 return obj;
3348 /* Wait until there is input available. */
3349 for (;;)
3351 if (kbd_fetch_ptr != kbd_store_ptr)
3352 break;
3353 #ifdef HAVE_MOUSE
3354 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3355 break;
3356 #endif
3358 /* If the quit flag is set, then read_char will return
3359 quit_char, so that counts as "available input." */
3360 if (!NILP (Vquit_flag))
3361 quit_throw_to_read_char ();
3363 /* One way or another, wait until input is available; then, if
3364 interrupt handlers have not read it, read it now. */
3366 #ifdef OLDVMS
3367 wait_for_kbd_input ();
3368 #else
3369 /* Note SIGIO has been undef'd if FIONREAD is missing. */
3370 #ifdef SIGIO
3371 gobble_input (0);
3372 #endif /* SIGIO */
3373 if (kbd_fetch_ptr != kbd_store_ptr)
3374 break;
3375 #ifdef HAVE_MOUSE
3376 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3377 break;
3378 #endif
3380 Lisp_Object minus_one;
3382 XSETINT (minus_one, -1);
3383 wait_reading_process_input (0, 0, minus_one, 1);
3385 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
3386 /* Pass 1 for EXPECT since we just waited to have input. */
3387 read_avail_input (1);
3389 #endif /* not VMS */
3392 if (CONSP (Vunread_command_events))
3394 Lisp_Object first;
3395 first = XCAR (Vunread_command_events);
3396 Vunread_command_events = XCDR (Vunread_command_events);
3397 *kbp = current_kboard;
3398 return first;
3401 /* At this point, we know that there is a readable event available
3402 somewhere. If the event queue is empty, then there must be a
3403 mouse movement enabled and available. */
3404 if (kbd_fetch_ptr != kbd_store_ptr)
3406 struct input_event *event;
3408 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3409 ? kbd_fetch_ptr
3410 : kbd_buffer);
3412 last_event_timestamp = event->timestamp;
3414 #ifdef MULTI_KBOARD
3415 *kbp = event_to_kboard (event);
3416 if (*kbp == 0)
3417 *kbp = current_kboard; /* Better than returning null ptr? */
3418 #else
3419 *kbp = &the_only_kboard;
3420 #endif
3422 obj = Qnil;
3424 /* These two kinds of events get special handling
3425 and don't actually appear to the command loop.
3426 We return nil for them. */
3427 if (event->kind == selection_request_event)
3429 #ifdef HAVE_X11
3430 struct input_event copy;
3432 /* Remove it from the buffer before processing it,
3433 since otherwise swallow_events will see it
3434 and process it again. */
3435 copy = *event;
3436 kbd_fetch_ptr = event + 1;
3437 input_pending = readable_events (0);
3438 x_handle_selection_request (&copy);
3439 #else
3440 /* We're getting selection request events, but we don't have
3441 a window system. */
3442 abort ();
3443 #endif
3446 else if (event->kind == selection_clear_event)
3448 #ifdef HAVE_X11
3449 struct input_event copy;
3451 /* Remove it from the buffer before processing it. */
3452 copy = *event;
3453 kbd_fetch_ptr = event + 1;
3454 input_pending = readable_events (0);
3455 x_handle_selection_clear (&copy);
3456 #else
3457 /* We're getting selection request events, but we don't have
3458 a window system. */
3459 abort ();
3460 #endif
3462 #if defined (HAVE_X11) || defined (HAVE_NTGUI)
3463 else if (event->kind == delete_window_event)
3465 /* Make an event (delete-frame (FRAME)). */
3466 obj = Fcons (event->frame_or_window, Qnil);
3467 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
3468 kbd_fetch_ptr = event + 1;
3470 else if (event->kind == iconify_event)
3472 /* Make an event (iconify-frame (FRAME)). */
3473 obj = Fcons (event->frame_or_window, Qnil);
3474 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
3475 kbd_fetch_ptr = event + 1;
3477 else if (event->kind == deiconify_event)
3479 /* Make an event (make-frame-visible (FRAME)). */
3480 obj = Fcons (event->frame_or_window, Qnil);
3481 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
3482 kbd_fetch_ptr = event + 1;
3484 #endif
3485 else if (event->kind == buffer_switch_event)
3487 /* The value doesn't matter here; only the type is tested. */
3488 XSETBUFFER (obj, current_buffer);
3489 kbd_fetch_ptr = event + 1;
3491 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
3492 else if (event->kind == menu_bar_activate_event)
3494 kbd_fetch_ptr = event + 1;
3495 input_pending = readable_events (0);
3496 if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
3497 x_activate_menubar (XFRAME (event->frame_or_window));
3499 #endif
3500 #ifdef WINDOWSNT
3501 else if (event->kind == language_change_event)
3503 /* Make an event (language-change (FRAME CHARSET LCID)). */
3504 obj = Fcons (event->modifiers, Qnil);
3505 obj = Fcons (event->code, Qnil);
3506 obj = Fcons (event->frame_or_window, obj);
3507 obj = Fcons (Qlanguage_change, Fcons (obj, Qnil));
3508 kbd_fetch_ptr = event + 1;
3510 #endif
3511 /* Just discard these, by returning nil.
3512 With MULTI_KBOARD, these events are used as placeholders
3513 when we need to randomly delete events from the queue.
3514 (They shouldn't otherwise be found in the buffer,
3515 but on some machines it appears they do show up
3516 even without MULTI_KBOARD.) */
3517 /* On Windows NT/9X, no_event is used to delete extraneous
3518 mouse events during a popup-menu call. */
3519 else if (event->kind == no_event)
3520 kbd_fetch_ptr = event + 1;
3521 else if (event->kind == HELP_EVENT)
3523 /* There are always two HELP_EVENTs in the input queue. */
3524 Lisp_Object object, position, help, frame, window;
3526 xassert (event->code == 0);
3527 frame = event->frame_or_window;
3528 object = event->arg;
3529 position = event->x;
3530 clear_event (event);
3532 kbd_fetch_ptr = event + 1;
3533 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3534 ? kbd_fetch_ptr
3535 : kbd_buffer);
3536 xassert (event->code == 1);
3537 help = event->arg;
3538 window = event->frame_or_window;
3539 if (!WINDOWP (window))
3540 window = Qnil;
3541 obj = Fcons (Qhelp_echo,
3542 list5 (frame, help, window, object, position));
3543 clear_event (event);
3544 kbd_fetch_ptr = event + 1;
3546 else if (event->kind == FOCUS_IN_EVENT)
3548 /* Notification of a FocusIn event. The frame receiving the
3549 focus is in event->frame_or_window. Generate a
3550 switch-frame event if necessary. */
3551 Lisp_Object frame, focus;
3553 frame = event->frame_or_window;
3554 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
3555 if (FRAMEP (focus))
3556 frame = focus;
3558 if (!EQ (frame, internal_last_event_frame)
3559 && !EQ (frame, selected_frame))
3560 obj = make_lispy_switch_frame (frame);
3561 internal_last_event_frame = frame;
3562 kbd_fetch_ptr = event + 1;
3564 else
3566 /* If this event is on a different frame, return a switch-frame this
3567 time, and leave the event in the queue for next time. */
3568 Lisp_Object frame;
3569 Lisp_Object focus;
3571 frame = event->frame_or_window;
3572 if (CONSP (frame))
3573 frame = XCAR (frame);
3574 else if (WINDOWP (frame))
3575 frame = WINDOW_FRAME (XWINDOW (frame));
3577 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
3578 if (! NILP (focus))
3579 frame = focus;
3581 if (! EQ (frame, internal_last_event_frame)
3582 && !EQ (frame, selected_frame))
3583 obj = make_lispy_switch_frame (frame);
3584 internal_last_event_frame = frame;
3586 /* If we didn't decide to make a switch-frame event, go ahead
3587 and build a real event from the queue entry. */
3589 if (NILP (obj))
3591 int idx;
3593 obj = make_lispy_event (event);
3595 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
3596 /* If this was a menu selection, then set the flag to inhibit
3597 writing to last_nonmenu_event. Don't do this if the event
3598 we're returning is (menu-bar), though; that indicates the
3599 beginning of the menu sequence, and we might as well leave
3600 that as the `event with parameters' for this selection. */
3601 if (used_mouse_menu
3602 && !EQ (event->frame_or_window, event->arg)
3603 && (event->kind == MENU_BAR_EVENT
3604 || event->kind == TOOL_BAR_EVENT))
3605 *used_mouse_menu = 1;
3606 #endif
3608 /* Wipe out this event, to catch bugs. */
3609 clear_event (event);
3610 kbd_fetch_ptr = event + 1;
3614 #ifdef HAVE_MOUSE
3615 /* Try generating a mouse motion event. */
3616 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3618 FRAME_PTR f = some_mouse_moved ();
3619 Lisp_Object bar_window;
3620 enum scroll_bar_part part;
3621 Lisp_Object x, y;
3622 unsigned long time;
3624 *kbp = current_kboard;
3625 /* Note that this uses F to determine which display to look at.
3626 If there is no valid info, it does not store anything
3627 so x remains nil. */
3628 x = Qnil;
3629 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
3631 obj = Qnil;
3633 /* Decide if we should generate a switch-frame event. Don't
3634 generate switch-frame events for motion outside of all Emacs
3635 frames. */
3636 if (!NILP (x) && f)
3638 Lisp_Object frame;
3640 frame = FRAME_FOCUS_FRAME (f);
3641 if (NILP (frame))
3642 XSETFRAME (frame, f);
3644 if (! EQ (frame, internal_last_event_frame)
3645 && !EQ (frame, selected_frame))
3646 obj = make_lispy_switch_frame (frame);
3647 internal_last_event_frame = frame;
3650 /* If we didn't decide to make a switch-frame event, go ahead and
3651 return a mouse-motion event. */
3652 if (!NILP (x) && NILP (obj))
3653 obj = make_lispy_movement (f, bar_window, part, x, y, time);
3655 #endif /* HAVE_MOUSE */
3656 else
3657 /* We were promised by the above while loop that there was
3658 something for us to read! */
3659 abort ();
3661 input_pending = readable_events (0);
3663 Vlast_event_frame = internal_last_event_frame;
3665 return (obj);
3668 /* Process any events that are not user-visible,
3669 then return, without reading any user-visible events. */
3671 void
3672 swallow_events (do_display)
3673 int do_display;
3675 int old_timers_run;
3677 while (kbd_fetch_ptr != kbd_store_ptr)
3679 struct input_event *event;
3681 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3682 ? kbd_fetch_ptr
3683 : kbd_buffer);
3685 last_event_timestamp = event->timestamp;
3687 /* These two kinds of events get special handling
3688 and don't actually appear to the command loop. */
3689 if (event->kind == selection_request_event)
3691 #ifdef HAVE_X11
3692 struct input_event copy;
3694 /* Remove it from the buffer before processing it,
3695 since otherwise swallow_events called recursively could see it
3696 and process it again. */
3697 copy = *event;
3698 kbd_fetch_ptr = event + 1;
3699 input_pending = readable_events (0);
3700 x_handle_selection_request (&copy);
3701 #else
3702 /* We're getting selection request events, but we don't have
3703 a window system. */
3704 abort ();
3705 #endif
3708 else if (event->kind == selection_clear_event)
3710 #ifdef HAVE_X11
3711 struct input_event copy;
3713 /* Remove it from the buffer before processing it, */
3714 copy = *event;
3716 kbd_fetch_ptr = event + 1;
3717 input_pending = readable_events (0);
3718 x_handle_selection_clear (&copy);
3719 #else
3720 /* We're getting selection request events, but we don't have
3721 a window system. */
3722 abort ();
3723 #endif
3725 else
3726 break;
3729 old_timers_run = timers_run;
3730 get_input_pending (&input_pending, 1);
3732 if (timers_run != old_timers_run && do_display)
3733 redisplay_preserve_echo_area ();
3736 static EMACS_TIME timer_idleness_start_time;
3738 /* Record the start of when Emacs is idle,
3739 for the sake of running idle-time timers. */
3741 void
3742 timer_start_idle ()
3744 Lisp_Object timers;
3746 /* If we are already in the idle state, do nothing. */
3747 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3748 return;
3750 EMACS_GET_TIME (timer_idleness_start_time);
3752 /* Mark all idle-time timers as once again candidates for running. */
3753 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCDR (timers))
3755 Lisp_Object timer;
3757 timer = XCAR (timers);
3759 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3760 continue;
3761 XVECTOR (timer)->contents[0] = Qnil;
3765 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
3767 void
3768 timer_stop_idle ()
3770 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
3773 /* This is only for debugging. */
3774 struct input_event last_timer_event;
3776 /* Check whether a timer has fired. To prevent larger problems we simply
3777 disregard elements that are not proper timers. Do not make a circular
3778 timer list for the time being.
3780 Returns the number of seconds to wait until the next timer fires. If a
3781 timer is triggering now, return zero seconds.
3782 If no timer is active, return -1 seconds.
3784 If a timer is ripe, we run it, with quitting turned off.
3786 DO_IT_NOW is now ignored. It used to mean that we should
3787 run the timer directly instead of queueing a timer-event.
3788 Now we always run timers directly. */
3790 EMACS_TIME
3791 timer_check (do_it_now)
3792 int do_it_now;
3794 EMACS_TIME nexttime;
3795 EMACS_TIME now, idleness_now;
3796 Lisp_Object timers, idle_timers, chosen_timer;
3797 struct gcpro gcpro1, gcpro2, gcpro3;
3799 EMACS_SET_SECS (nexttime, -1);
3800 EMACS_SET_USECS (nexttime, -1);
3802 /* Always consider the ordinary timers. */
3803 timers = Vtimer_list;
3804 /* Consider the idle timers only if Emacs is idle. */
3805 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3806 idle_timers = Vtimer_idle_list;
3807 else
3808 idle_timers = Qnil;
3809 chosen_timer = Qnil;
3810 GCPRO3 (timers, idle_timers, chosen_timer);
3812 if (CONSP (timers) || CONSP (idle_timers))
3814 EMACS_GET_TIME (now);
3815 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3816 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
3819 while (CONSP (timers) || CONSP (idle_timers))
3821 Lisp_Object *vector;
3822 Lisp_Object timer = Qnil, idle_timer = Qnil;
3823 EMACS_TIME timer_time, idle_timer_time;
3824 EMACS_TIME difference, timer_difference, idle_timer_difference;
3826 /* Skip past invalid timers and timers already handled. */
3827 if (!NILP (timers))
3829 timer = XCAR (timers);
3830 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3832 timers = XCDR (timers);
3833 continue;
3835 vector = XVECTOR (timer)->contents;
3837 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3838 || !INTEGERP (vector[3])
3839 || ! NILP (vector[0]))
3841 timers = XCDR (timers);
3842 continue;
3845 if (!NILP (idle_timers))
3847 timer = XCAR (idle_timers);
3848 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3850 idle_timers = XCDR (idle_timers);
3851 continue;
3853 vector = XVECTOR (timer)->contents;
3855 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3856 || !INTEGERP (vector[3])
3857 || ! NILP (vector[0]))
3859 idle_timers = XCDR (idle_timers);
3860 continue;
3864 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
3865 based on the next ordinary timer.
3866 TIMER_DIFFERENCE is the distance in time from NOW to when
3867 this timer becomes ripe (negative if it's already ripe). */
3868 if (!NILP (timers))
3870 timer = XCAR (timers);
3871 vector = XVECTOR (timer)->contents;
3872 EMACS_SET_SECS (timer_time,
3873 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3874 EMACS_SET_USECS (timer_time, XINT (vector[3]));
3875 EMACS_SUB_TIME (timer_difference, timer_time, now);
3878 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
3879 based on the next idle timer. */
3880 if (!NILP (idle_timers))
3882 idle_timer = XCAR (idle_timers);
3883 vector = XVECTOR (idle_timer)->contents;
3884 EMACS_SET_SECS (idle_timer_time,
3885 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3886 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
3887 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
3890 /* Decide which timer is the next timer,
3891 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
3892 Also step down the list where we found that timer. */
3894 if (! NILP (timers) && ! NILP (idle_timers))
3896 EMACS_TIME temp;
3897 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
3898 if (EMACS_TIME_NEG_P (temp))
3900 chosen_timer = timer;
3901 timers = XCDR (timers);
3902 difference = timer_difference;
3904 else
3906 chosen_timer = idle_timer;
3907 idle_timers = XCDR (idle_timers);
3908 difference = idle_timer_difference;
3911 else if (! NILP (timers))
3913 chosen_timer = timer;
3914 timers = XCDR (timers);
3915 difference = timer_difference;
3917 else
3919 chosen_timer = idle_timer;
3920 idle_timers = XCDR (idle_timers);
3921 difference = idle_timer_difference;
3923 vector = XVECTOR (chosen_timer)->contents;
3925 /* If timer is ripe, run it if it hasn't been run. */
3926 if (EMACS_TIME_NEG_P (difference)
3927 || (EMACS_SECS (difference) == 0
3928 && EMACS_USECS (difference) == 0))
3930 if (NILP (vector[0]))
3932 int was_locked = single_kboard;
3933 int count = specpdl_ptr - specpdl;
3935 /* Mark the timer as triggered to prevent problems if the lisp
3936 code fails to reschedule it right. */
3937 vector[0] = Qt;
3939 specbind (Qinhibit_quit, Qt);
3941 call1 (Qtimer_event_handler, chosen_timer);
3942 timers_run++;
3944 unbind_to (count, Qnil);
3946 /* Resume allowing input from any kboard, if that was true before. */
3947 if (!was_locked)
3948 any_kboard_state ();
3950 /* Since we have handled the event,
3951 we don't need to tell the caller to wake up and do it. */
3954 else
3955 /* When we encounter a timer that is still waiting,
3956 return the amount of time to wait before it is ripe. */
3958 UNGCPRO;
3959 return difference;
3963 /* No timers are pending in the future. */
3964 /* Return 0 if we generated an event, and -1 if not. */
3965 UNGCPRO;
3966 return nexttime;
3969 /* Caches for modify_event_symbol. */
3970 static Lisp_Object accent_key_syms;
3971 static Lisp_Object func_key_syms;
3972 static Lisp_Object mouse_syms;
3973 #ifdef WINDOWSNT
3974 static Lisp_Object mouse_wheel_syms;
3975 #endif
3976 static Lisp_Object drag_n_drop_syms;
3978 /* This is a list of keysym codes for special "accent" characters.
3979 It parallels lispy_accent_keys. */
3981 static int lispy_accent_codes[] =
3983 #ifdef XK_dead_circumflex
3984 XK_dead_circumflex,
3985 #else
3987 #endif
3988 #ifdef XK_dead_grave
3989 XK_dead_grave,
3990 #else
3992 #endif
3993 #ifdef XK_dead_tilde
3994 XK_dead_tilde,
3995 #else
3997 #endif
3998 #ifdef XK_dead_diaeresis
3999 XK_dead_diaeresis,
4000 #else
4002 #endif
4003 #ifdef XK_dead_macron
4004 XK_dead_macron,
4005 #else
4007 #endif
4008 #ifdef XK_dead_degree
4009 XK_dead_degree,
4010 #else
4012 #endif
4013 #ifdef XK_dead_acute
4014 XK_dead_acute,
4015 #else
4017 #endif
4018 #ifdef XK_dead_cedilla
4019 XK_dead_cedilla,
4020 #else
4022 #endif
4023 #ifdef XK_dead_breve
4024 XK_dead_breve,
4025 #else
4027 #endif
4028 #ifdef XK_dead_ogonek
4029 XK_dead_ogonek,
4030 #else
4032 #endif
4033 #ifdef XK_dead_caron
4034 XK_dead_caron,
4035 #else
4037 #endif
4038 #ifdef XK_dead_doubleacute
4039 XK_dead_doubleacute,
4040 #else
4042 #endif
4043 #ifdef XK_dead_abovedot
4044 XK_dead_abovedot,
4045 #else
4047 #endif
4050 /* This is a list of Lisp names for special "accent" characters.
4051 It parallels lispy_accent_codes. */
4053 static char *lispy_accent_keys[] =
4055 "dead-circumflex",
4056 "dead-grave",
4057 "dead-tilde",
4058 "dead-diaeresis",
4059 "dead-macron",
4060 "dead-degree",
4061 "dead-acute",
4062 "dead-cedilla",
4063 "dead-breve",
4064 "dead-ogonek",
4065 "dead-caron",
4066 "dead-doubleacute",
4067 "dead-abovedot",
4070 #ifdef HAVE_NTGUI
4071 #define FUNCTION_KEY_OFFSET 0x0
4073 char *lispy_function_keys[] =
4075 0, /* 0 */
4077 0, /* VK_LBUTTON 0x01 */
4078 0, /* VK_RBUTTON 0x02 */
4079 "cancel", /* VK_CANCEL 0x03 */
4080 0, /* VK_MBUTTON 0x04 */
4082 0, 0, 0, /* 0x05 .. 0x07 */
4084 "backspace", /* VK_BACK 0x08 */
4085 "tab", /* VK_TAB 0x09 */
4087 0, 0, /* 0x0A .. 0x0B */
4089 "clear", /* VK_CLEAR 0x0C */
4090 "return", /* VK_RETURN 0x0D */
4092 0, 0, /* 0x0E .. 0x0F */
4094 0, /* VK_SHIFT 0x10 */
4095 0, /* VK_CONTROL 0x11 */
4096 0, /* VK_MENU 0x12 */
4097 "pause", /* VK_PAUSE 0x13 */
4098 "capslock", /* VK_CAPITAL 0x14 */
4100 0, 0, 0, 0, 0, 0, /* 0x15 .. 0x1A */
4102 "escape", /* VK_ESCAPE 0x1B */
4104 0, 0, 0, 0, /* 0x1C .. 0x1F */
4106 0, /* VK_SPACE 0x20 */
4107 "prior", /* VK_PRIOR 0x21 */
4108 "next", /* VK_NEXT 0x22 */
4109 "end", /* VK_END 0x23 */
4110 "home", /* VK_HOME 0x24 */
4111 "left", /* VK_LEFT 0x25 */
4112 "up", /* VK_UP 0x26 */
4113 "right", /* VK_RIGHT 0x27 */
4114 "down", /* VK_DOWN 0x28 */
4115 "select", /* VK_SELECT 0x29 */
4116 "print", /* VK_PRINT 0x2A */
4117 "execute", /* VK_EXECUTE 0x2B */
4118 "snapshot", /* VK_SNAPSHOT 0x2C */
4119 "insert", /* VK_INSERT 0x2D */
4120 "delete", /* VK_DELETE 0x2E */
4121 "help", /* VK_HELP 0x2F */
4123 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
4125 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4127 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
4129 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
4131 0, 0, 0, 0, 0, 0, 0, 0, 0,
4132 0, 0, 0, 0, 0, 0, 0, 0, 0,
4133 0, 0, 0, 0, 0, 0, 0, 0,
4135 "lwindow", /* VK_LWIN 0x5B */
4136 "rwindow", /* VK_RWIN 0x5C */
4137 "apps", /* VK_APPS 0x5D */
4139 0, 0, /* 0x5E .. 0x5F */
4141 "kp-0", /* VK_NUMPAD0 0x60 */
4142 "kp-1", /* VK_NUMPAD1 0x61 */
4143 "kp-2", /* VK_NUMPAD2 0x62 */
4144 "kp-3", /* VK_NUMPAD3 0x63 */
4145 "kp-4", /* VK_NUMPAD4 0x64 */
4146 "kp-5", /* VK_NUMPAD5 0x65 */
4147 "kp-6", /* VK_NUMPAD6 0x66 */
4148 "kp-7", /* VK_NUMPAD7 0x67 */
4149 "kp-8", /* VK_NUMPAD8 0x68 */
4150 "kp-9", /* VK_NUMPAD9 0x69 */
4151 "kp-multiply", /* VK_MULTIPLY 0x6A */
4152 "kp-add", /* VK_ADD 0x6B */
4153 "kp-separator", /* VK_SEPARATOR 0x6C */
4154 "kp-subtract", /* VK_SUBTRACT 0x6D */
4155 "kp-decimal", /* VK_DECIMAL 0x6E */
4156 "kp-divide", /* VK_DIVIDE 0x6F */
4157 "f1", /* VK_F1 0x70 */
4158 "f2", /* VK_F2 0x71 */
4159 "f3", /* VK_F3 0x72 */
4160 "f4", /* VK_F4 0x73 */
4161 "f5", /* VK_F5 0x74 */
4162 "f6", /* VK_F6 0x75 */
4163 "f7", /* VK_F7 0x76 */
4164 "f8", /* VK_F8 0x77 */
4165 "f9", /* VK_F9 0x78 */
4166 "f10", /* VK_F10 0x79 */
4167 "f11", /* VK_F11 0x7A */
4168 "f12", /* VK_F12 0x7B */
4169 "f13", /* VK_F13 0x7C */
4170 "f14", /* VK_F14 0x7D */
4171 "f15", /* VK_F15 0x7E */
4172 "f16", /* VK_F16 0x7F */
4173 "f17", /* VK_F17 0x80 */
4174 "f18", /* VK_F18 0x81 */
4175 "f19", /* VK_F19 0x82 */
4176 "f20", /* VK_F20 0x83 */
4177 "f21", /* VK_F21 0x84 */
4178 "f22", /* VK_F22 0x85 */
4179 "f23", /* VK_F23 0x86 */
4180 "f24", /* VK_F24 0x87 */
4182 0, 0, 0, 0, /* 0x88 .. 0x8B */
4183 0, 0, 0, 0, /* 0x8C .. 0x8F */
4185 "kp-numlock", /* VK_NUMLOCK 0x90 */
4186 "scroll", /* VK_SCROLL 0x91 */
4188 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
4189 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
4190 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
4191 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
4192 "kp-end", /* VK_NUMPAD_END 0x96 */
4193 "kp-home", /* VK_NUMPAD_HOME 0x97 */
4194 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
4195 "kp-up", /* VK_NUMPAD_UP 0x99 */
4196 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
4197 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
4198 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
4199 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
4201 0, 0, /* 0x9E .. 0x9F */
4204 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
4205 * Used only as parameters to GetAsyncKeyState and GetKeyState.
4206 * No other API or message will distinguish left and right keys this way.
4208 /* 0xA0 .. 0xEF */
4210 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4211 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4212 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4213 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4214 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4216 /* 0xF0 .. 0xF5 */
4218 0, 0, 0, 0, 0, 0,
4220 "attn", /* VK_ATTN 0xF6 */
4221 "crsel", /* VK_CRSEL 0xF7 */
4222 "exsel", /* VK_EXSEL 0xF8 */
4223 "ereof", /* VK_EREOF 0xF9 */
4224 "play", /* VK_PLAY 0xFA */
4225 "zoom", /* VK_ZOOM 0xFB */
4226 "noname", /* VK_NONAME 0xFC */
4227 "pa1", /* VK_PA1 0xFD */
4228 "oem_clear", /* VK_OEM_CLEAR 0xFE */
4229 0 /* 0xFF */
4232 #else /* not HAVE_NTGUI */
4234 #ifdef XK_kana_A
4235 static char *lispy_kana_keys[] =
4237 /* X Keysym value */
4238 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x400 .. 0x40f */
4239 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x410 .. 0x41f */
4240 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x420 .. 0x42f */
4241 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x430 .. 0x43f */
4242 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x440 .. 0x44f */
4243 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x450 .. 0x45f */
4244 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x460 .. 0x46f */
4245 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0,
4246 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x480 .. 0x48f */
4247 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x490 .. 0x49f */
4248 0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket",
4249 "kana-comma", "kana-conjunctive", "kana-WO", "kana-a",
4250 "kana-i", "kana-u", "kana-e", "kana-o",
4251 "kana-ya", "kana-yu", "kana-yo", "kana-tsu",
4252 "prolongedsound", "kana-A", "kana-I", "kana-U",
4253 "kana-E", "kana-O", "kana-KA", "kana-KI",
4254 "kana-KU", "kana-KE", "kana-KO", "kana-SA",
4255 "kana-SHI", "kana-SU", "kana-SE", "kana-SO",
4256 "kana-TA", "kana-CHI", "kana-TSU", "kana-TE",
4257 "kana-TO", "kana-NA", "kana-NI", "kana-NU",
4258 "kana-NE", "kana-NO", "kana-HA", "kana-HI",
4259 "kana-FU", "kana-HE", "kana-HO", "kana-MA",
4260 "kana-MI", "kana-MU", "kana-ME", "kana-MO",
4261 "kana-YA", "kana-YU", "kana-YO", "kana-RA",
4262 "kana-RI", "kana-RU", "kana-RE", "kana-RO",
4263 "kana-WA", "kana-N", "voicedsound", "semivoicedsound",
4264 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4e0 .. 0x4ef */
4265 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f0 .. 0x4ff */
4267 #endif /* XK_kana_A */
4269 #define FUNCTION_KEY_OFFSET 0xff00
4271 /* You'll notice that this table is arranged to be conveniently
4272 indexed by X Windows keysym values. */
4273 static char *lispy_function_keys[] =
4275 /* X Keysym value */
4277 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00...0f */
4278 "backspace", "tab", "linefeed", "clear",
4279 0, "return", 0, 0,
4280 0, 0, 0, "pause", /* 0xff10...1f */
4281 0, 0, 0, 0, 0, 0, 0, "escape",
4282 0, 0, 0, 0,
4283 0, "kanji", "muhenkan", "henkan", /* 0xff20...2f */
4284 "romaji", "hiragana", "katakana", "hiragana-katakana",
4285 "zenkaku", "hankaku", "zenkaku-hankaku", "touroku",
4286 "massyo", "kana-lock", "kana-shift", "eisu-shift",
4287 "eisu-toggle", /* 0xff30...3f */
4288 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4289 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
4291 "home", "left", "up", "right", /* 0xff50 */ /* IsCursorKey */
4292 "down", "prior", "next", "end",
4293 "begin", 0, 0, 0, 0, 0, 0, 0,
4294 "select", /* 0xff60 */ /* IsMiscFunctionKey */
4295 "print",
4296 "execute",
4297 "insert",
4298 0, /* 0xff64 */
4299 "undo",
4300 "redo",
4301 "menu",
4302 "find",
4303 "cancel",
4304 "help",
4305 "break", /* 0xff6b */
4307 0, 0, 0, 0,
4308 0, 0, 0, 0, "backtab", 0, 0, 0, /* 0xff70... */
4309 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff78... */
4310 "kp-space", /* 0xff80 */ /* IsKeypadKey */
4311 0, 0, 0, 0, 0, 0, 0, 0,
4312 "kp-tab", /* 0xff89 */
4313 0, 0, 0,
4314 "kp-enter", /* 0xff8d */
4315 0, 0, 0,
4316 "kp-f1", /* 0xff91 */
4317 "kp-f2",
4318 "kp-f3",
4319 "kp-f4",
4320 "kp-home", /* 0xff95 */
4321 "kp-left",
4322 "kp-up",
4323 "kp-right",
4324 "kp-down",
4325 "kp-prior", /* kp-page-up */
4326 "kp-next", /* kp-page-down */
4327 "kp-end",
4328 "kp-begin",
4329 "kp-insert",
4330 "kp-delete",
4331 0, /* 0xffa0 */
4332 0, 0, 0, 0, 0, 0, 0, 0, 0,
4333 "kp-multiply", /* 0xffaa */
4334 "kp-add",
4335 "kp-separator",
4336 "kp-subtract",
4337 "kp-decimal",
4338 "kp-divide", /* 0xffaf */
4339 "kp-0", /* 0xffb0 */
4340 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
4341 0, /* 0xffba */
4342 0, 0,
4343 "kp-equal", /* 0xffbd */
4344 "f1", /* 0xffbe */ /* IsFunctionKey */
4345 "f2",
4346 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
4347 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
4348 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
4349 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
4350 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
4351 0, 0, 0, 0, 0, 0, 0, 0,
4352 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
4353 0, 0, 0, 0, 0, 0, 0, "delete"
4356 /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE. */
4357 #define ISO_FUNCTION_KEY_OFFSET 0xfe00
4359 static char *iso_lispy_function_keys[] =
4361 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe00 */
4362 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe08 */
4363 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe10 */
4364 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe18 */
4365 "iso-lefttab", /* 0xfe20 */
4366 "iso-move-line-up", "iso-move-line-down",
4367 "iso-partial-line-up", "iso-partial-line-down",
4368 "iso-partial-space-left", "iso-partial-space-right",
4369 "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */
4370 "iso-release-margin-left", "iso-release-margin-right",
4371 "iso-release-both-margins",
4372 "iso-fast-cursor-left", "iso-fast-cursor-right",
4373 "iso-fast-cursor-up", "iso-fast-cursor-down",
4374 "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */
4375 "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */
4378 #endif /* not HAVE_NTGUI */
4380 Lisp_Object Vlispy_mouse_stem;
4382 #ifdef WINDOWSNT
4383 /* mouse-wheel events are generated by the wheel on devices such as
4384 the MS Intellimouse. The wheel sits in between the left and right
4385 mouse buttons, and is typically used to scroll or zoom the window
4386 underneath the pointer. mouse-wheel events specify the object on
4387 which they operate, and a delta corresponding to the amount and
4388 direction that the wheel is rotated. Clicking the mouse-wheel
4389 generates a mouse-2 event. */
4390 static char *lispy_mouse_wheel_names[] =
4392 "mouse-wheel"
4395 #endif /* WINDOWSNT */
4397 /* drag-n-drop events are generated when a set of selected files are
4398 dragged from another application and dropped onto an Emacs window. */
4399 static char *lispy_drag_n_drop_names[] =
4401 "drag-n-drop"
4404 /* Scroll bar parts. */
4405 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
4406 Lisp_Object Qup, Qdown, Qbottom, Qend_scroll;
4407 Lisp_Object Qtop, Qratio;
4409 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
4410 Lisp_Object *scroll_bar_parts[] = {
4411 &Qabove_handle, &Qhandle, &Qbelow_handle,
4412 &Qup, &Qdown, &Qtop, &Qbottom, &Qend_scroll, &Qratio
4415 /* User signal events. */
4416 Lisp_Object Qusr1_signal, Qusr2_signal;
4418 Lisp_Object *lispy_user_signals[] =
4420 &Qusr1_signal, &Qusr2_signal
4424 /* A vector, indexed by button number, giving the down-going location
4425 of currently depressed buttons, both scroll bar and non-scroll bar.
4427 The elements have the form
4428 (BUTTON-NUMBER MODIFIER-MASK . REST)
4429 where REST is the cdr of a position as it would be reported in the event.
4431 The make_lispy_event function stores positions here to tell the
4432 difference between click and drag events, and to store the starting
4433 location to be included in drag events. */
4435 static Lisp_Object button_down_location;
4437 /* Information about the most recent up-going button event: Which
4438 button, what location, and what time. */
4440 static int last_mouse_button;
4441 static int last_mouse_x;
4442 static int last_mouse_y;
4443 static unsigned long button_down_time;
4445 /* The maximum time between clicks to make a double-click,
4446 or Qnil to disable double-click detection,
4447 or Qt for no time limit. */
4448 Lisp_Object Vdouble_click_time;
4450 /* The number of clicks in this multiple-click. */
4452 int double_click_count;
4454 /* Given a struct input_event, build the lisp event which represents
4455 it. If EVENT is 0, build a mouse movement event from the mouse
4456 movement buffer, which should have a movement event in it.
4458 Note that events must be passed to this function in the order they
4459 are received; this function stores the location of button presses
4460 in order to build drag events when the button is released. */
4462 static Lisp_Object
4463 make_lispy_event (event)
4464 struct input_event *event;
4466 int i;
4468 switch (SWITCH_ENUM_CAST (event->kind))
4470 /* A simple keystroke. */
4471 case ascii_keystroke:
4473 Lisp_Object lispy_c;
4474 int c = event->code & 0377;
4475 /* Turn ASCII characters into control characters
4476 when proper. */
4477 if (event->modifiers & ctrl_modifier)
4478 c = make_ctrl_char (c);
4480 /* Add in the other modifier bits. We took care of ctrl_modifier
4481 just above, and the shift key was taken care of by the X code,
4482 and applied to control characters by make_ctrl_char. */
4483 c |= (event->modifiers
4484 & (meta_modifier | alt_modifier
4485 | hyper_modifier | super_modifier));
4486 /* Distinguish Shift-SPC from SPC. */
4487 if ((event->code & 0377) == 040
4488 && event->modifiers & shift_modifier)
4489 c |= shift_modifier;
4490 button_down_time = 0;
4491 XSETFASTINT (lispy_c, c);
4492 return lispy_c;
4495 case multibyte_char_keystroke:
4497 Lisp_Object lispy_c;
4499 XSETFASTINT (lispy_c, event->code);
4500 return lispy_c;
4503 /* A function key. The symbol may need to have modifier prefixes
4504 tacked onto it. */
4505 case non_ascii_keystroke:
4506 button_down_time = 0;
4508 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
4509 if (event->code == lispy_accent_codes[i])
4510 return modify_event_symbol (i,
4511 event->modifiers,
4512 Qfunction_key, Qnil,
4513 lispy_accent_keys, &accent_key_syms,
4514 (sizeof (lispy_accent_keys)
4515 / sizeof (lispy_accent_keys[0])));
4517 /* Handle system-specific keysyms. */
4518 if (event->code & (1 << 28))
4520 /* We need to use an alist rather than a vector as the cache
4521 since we can't make a vector long enuf. */
4522 if (NILP (current_kboard->system_key_syms))
4523 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
4524 return modify_event_symbol (event->code,
4525 event->modifiers,
4526 Qfunction_key,
4527 current_kboard->Vsystem_key_alist,
4528 0, &current_kboard->system_key_syms,
4529 (unsigned)-1);
4532 #ifdef XK_kana_A
4533 if (event->code >= 0x400 && event->code < 0x500)
4534 return modify_event_symbol (event->code - 0x400,
4535 event->modifiers & ~shift_modifier,
4536 Qfunction_key, Qnil,
4537 lispy_kana_keys, &func_key_syms,
4538 (sizeof (lispy_kana_keys)
4539 / sizeof (lispy_kana_keys[0])));
4540 #endif /* XK_kana_A */
4542 #ifdef ISO_FUNCTION_KEY_OFFSET
4543 if (event->code < FUNCTION_KEY_OFFSET
4544 && event->code >= ISO_FUNCTION_KEY_OFFSET)
4545 return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET,
4546 event->modifiers,
4547 Qfunction_key, Qnil,
4548 iso_lispy_function_keys, &func_key_syms,
4549 (sizeof (iso_lispy_function_keys)
4550 / sizeof (iso_lispy_function_keys[0])));
4551 else
4552 #endif
4553 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
4554 event->modifiers,
4555 Qfunction_key, Qnil,
4556 lispy_function_keys, &func_key_syms,
4557 (sizeof (lispy_function_keys)
4558 / sizeof (lispy_function_keys[0])));
4560 #ifdef HAVE_MOUSE
4561 /* A mouse click. Figure out where it is, decide whether it's
4562 a press, click or drag, and build the appropriate structure. */
4563 case mouse_click:
4564 #ifndef USE_TOOLKIT_SCROLL_BARS
4565 case scroll_bar_click:
4566 #endif
4568 int button = event->code;
4569 int is_double;
4570 Lisp_Object position;
4571 Lisp_Object *start_pos_ptr;
4572 Lisp_Object start_pos;
4574 position = Qnil;
4576 /* Build the position as appropriate for this mouse click. */
4577 if (event->kind == mouse_click)
4579 int part;
4580 FRAME_PTR f = XFRAME (event->frame_or_window);
4581 Lisp_Object window;
4582 Lisp_Object posn;
4583 Lisp_Object string_info = Qnil;
4584 int row, column;
4586 /* Ignore mouse events that were made on frame that
4587 have been deleted. */
4588 if (! FRAME_LIVE_P (f))
4589 return Qnil;
4591 /* EVENT->x and EVENT->y are frame-relative pixel
4592 coordinates at this place. Under old redisplay, COLUMN
4593 and ROW are set to frame relative glyph coordinates
4594 which are then used to determine whether this click is
4595 in a menu (non-toolkit version). */
4596 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4597 &column, &row, NULL, 1);
4599 #ifndef USE_X_TOOLKIT
4600 /* In the non-toolkit version, clicks on the menu bar
4601 are ordinary button events in the event buffer.
4602 Distinguish them, and invoke the menu.
4604 (In the toolkit version, the toolkit handles the menu bar
4605 and Emacs doesn't know about it until after the user
4606 makes a selection.) */
4607 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
4608 && (event->modifiers & down_modifier))
4610 Lisp_Object items, item;
4611 int hpos;
4612 int i;
4614 #if 0
4615 /* Activate the menu bar on the down event. If the
4616 up event comes in before the menu code can deal with it,
4617 just ignore it. */
4618 if (! (event->modifiers & down_modifier))
4619 return Qnil;
4620 #endif
4622 /* Find the menu bar item under `column'. */
4623 item = Qnil;
4624 items = FRAME_MENU_BAR_ITEMS (f);
4625 for (i = 0; i < XVECTOR (items)->size; i += 4)
4627 Lisp_Object pos, string;
4628 string = XVECTOR (items)->contents[i + 1];
4629 pos = XVECTOR (items)->contents[i + 3];
4630 if (NILP (string))
4631 break;
4632 if (column >= XINT (pos)
4633 && column < XINT (pos) + XSTRING (string)->size)
4635 item = XVECTOR (items)->contents[i];
4636 break;
4640 /* ELisp manual 2.4b says (x y) are window relative but
4641 code says they are frame-relative. */
4642 position
4643 = Fcons (event->frame_or_window,
4644 Fcons (Qmenu_bar,
4645 Fcons (Fcons (event->x, event->y),
4646 Fcons (make_number (event->timestamp),
4647 Qnil))));
4649 return Fcons (item, Fcons (position, Qnil));
4651 #endif /* not USE_X_TOOLKIT */
4653 /* Set `window' to the window under frame pixel coordinates
4654 event->x/event->y. */
4655 window = window_from_coordinates (f, XINT (event->x),
4656 XINT (event->y), &part, 0);
4658 if (!WINDOWP (window))
4660 window = event->frame_or_window;
4661 posn = Qnil;
4663 else
4665 /* It's a click in window window at frame coordinates
4666 event->x/ event->y. */
4667 struct window *w = XWINDOW (window);
4669 /* Get window relative coordinates. Original code
4670 `rounded' this to glyph boundaries. */
4671 int wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (event->x));
4672 int wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (event->y));
4674 /* Set event coordinates to window-relative coordinates
4675 for constructing the Lisp event below. */
4676 XSETINT (event->x, wx);
4677 XSETINT (event->y, wy);
4679 if (part == 1 || part == 3)
4681 /* Mode line or top line. Look for a string under
4682 the mouse that may have a `local-map' property. */
4683 Lisp_Object string;
4684 int charpos;
4686 posn = part == 1 ? Qmode_line : Qheader_line;
4687 string = mode_line_string (w, wx, wy, part == 1, &charpos);
4688 if (STRINGP (string))
4689 string_info = Fcons (string, make_number (charpos));
4691 else if (part == 2)
4692 posn = Qvertical_line;
4693 else
4694 XSETINT (posn, buffer_posn_from_coords (w, &wx, &wy));
4697 position
4698 = Fcons (window,
4699 Fcons (posn,
4700 Fcons (Fcons (event->x, event->y),
4701 Fcons (make_number (event->timestamp),
4702 (NILP (string_info)
4703 ? Qnil
4704 : Fcons (string_info, Qnil))))));
4706 #ifndef USE_TOOLKIT_SCROLL_BARS
4707 else
4709 /* It's a scrollbar click. */
4710 Lisp_Object window;
4711 Lisp_Object portion_whole;
4712 Lisp_Object part;
4714 window = event->frame_or_window;
4715 portion_whole = Fcons (event->x, event->y);
4716 part = *scroll_bar_parts[(int) event->part];
4718 position
4719 = Fcons (window,
4720 Fcons (Qvertical_scroll_bar,
4721 Fcons (portion_whole,
4722 Fcons (make_number (event->timestamp),
4723 Fcons (part, Qnil)))));
4725 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4727 if (button >= XVECTOR (button_down_location)->size)
4729 button_down_location = larger_vector (button_down_location,
4730 button + 1, Qnil);
4731 mouse_syms = larger_vector (mouse_syms, button + 1, Qnil);
4734 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
4736 start_pos = *start_pos_ptr;
4737 *start_pos_ptr = Qnil;
4739 is_double = (button == last_mouse_button
4740 && XINT (event->x) == last_mouse_x
4741 && XINT (event->y) == last_mouse_y
4742 && button_down_time != 0
4743 && (EQ (Vdouble_click_time, Qt)
4744 || (INTEGERP (Vdouble_click_time)
4745 && ((int)(event->timestamp - button_down_time)
4746 < XINT (Vdouble_click_time)))));
4747 last_mouse_button = button;
4748 last_mouse_x = XINT (event->x);
4749 last_mouse_y = XINT (event->y);
4751 /* If this is a button press, squirrel away the location, so
4752 we can decide later whether it was a click or a drag. */
4753 if (event->modifiers & down_modifier)
4755 if (is_double)
4757 double_click_count++;
4758 event->modifiers |= ((double_click_count > 2)
4759 ? triple_modifier
4760 : double_modifier);
4762 else
4763 double_click_count = 1;
4764 button_down_time = event->timestamp;
4765 *start_pos_ptr = Fcopy_alist (position);
4768 /* Now we're releasing a button - check the co-ordinates to
4769 see if this was a click or a drag. */
4770 else if (event->modifiers & up_modifier)
4772 /* If we did not see a down before this up,
4773 ignore the up. Probably this happened because
4774 the down event chose a menu item.
4775 It would be an annoyance to treat the release
4776 of the button that chose the menu item
4777 as a separate event. */
4779 if (!CONSP (start_pos))
4780 return Qnil;
4782 event->modifiers &= ~up_modifier;
4783 #if 0 /* Formerly we treated an up with no down as a click event. */
4784 if (!CONSP (start_pos))
4785 event->modifiers |= click_modifier;
4786 else
4787 #endif
4789 /* The third element of every position should be the (x,y)
4790 pair. */
4791 Lisp_Object down;
4793 down = Fnth (make_number (2), start_pos);
4794 if (EQ (event->x, XCAR (down))
4795 && EQ (event->y, XCDR (down)))
4797 event->modifiers |= click_modifier;
4799 else
4801 button_down_time = 0;
4802 event->modifiers |= drag_modifier;
4804 /* Don't check is_double; treat this as multiple
4805 if the down-event was multiple. */
4806 if (double_click_count > 1)
4807 event->modifiers |= ((double_click_count > 2)
4808 ? triple_modifier
4809 : double_modifier);
4812 else
4813 /* Every mouse event should either have the down_modifier or
4814 the up_modifier set. */
4815 abort ();
4818 /* Get the symbol we should use for the mouse click. */
4819 Lisp_Object head;
4821 head = modify_event_symbol (button,
4822 event->modifiers,
4823 Qmouse_click, Vlispy_mouse_stem,
4824 NULL,
4825 &mouse_syms,
4826 XVECTOR (mouse_syms)->size);
4827 if (event->modifiers & drag_modifier)
4828 return Fcons (head,
4829 Fcons (start_pos,
4830 Fcons (position,
4831 Qnil)));
4832 else if (event->modifiers & (double_modifier | triple_modifier))
4833 return Fcons (head,
4834 Fcons (position,
4835 Fcons (make_number (double_click_count),
4836 Qnil)));
4837 else
4838 return Fcons (head,
4839 Fcons (position,
4840 Qnil));
4844 #if USE_TOOLKIT_SCROLL_BARS
4846 /* We don't have down and up events if using toolkit scroll bars,
4847 so make this always a click event. Store in the `part' of
4848 the Lisp event a symbol which maps to the following actions:
4850 `above_handle' page up
4851 `below_handle' page down
4852 `up' line up
4853 `down' line down
4854 `top' top of buffer
4855 `bottom' bottom of buffer
4856 `handle' thumb has been dragged.
4857 `end-scroll' end of interaction with scroll bar
4859 The incoming input_event contains in its `part' member an
4860 index of type `enum scroll_bar_part' which we can use as an
4861 index in scroll_bar_parts to get the appropriate symbol. */
4863 case scroll_bar_click:
4865 Lisp_Object position, head, window, portion_whole, part;
4867 window = event->frame_or_window;
4868 portion_whole = Fcons (event->x, event->y);
4869 part = *scroll_bar_parts[(int) event->part];
4871 position
4872 = Fcons (window,
4873 Fcons (Qvertical_scroll_bar,
4874 Fcons (portion_whole,
4875 Fcons (make_number (event->timestamp),
4876 Fcons (part, Qnil)))));
4878 /* Always treat scroll bar events as clicks. */
4879 event->modifiers |= click_modifier;
4881 /* Get the symbol we should use for the mouse click. */
4882 head = modify_event_symbol (event->code,
4883 event->modifiers,
4884 Qmouse_click,
4885 Vlispy_mouse_stem,
4886 NULL, &mouse_syms,
4887 XVECTOR (mouse_syms)->size);
4888 return Fcons (head, Fcons (position, Qnil));
4891 #endif /* USE_TOOLKIT_SCROLL_BARS */
4893 #ifdef WINDOWSNT
4894 case w32_scroll_bar_click:
4896 int button = event->code;
4897 int is_double;
4898 Lisp_Object position;
4899 Lisp_Object *start_pos_ptr;
4900 Lisp_Object start_pos;
4903 Lisp_Object window;
4904 Lisp_Object portion_whole;
4905 Lisp_Object part;
4907 window = event->frame_or_window;
4908 portion_whole = Fcons (event->x, event->y);
4909 part = *scroll_bar_parts[(int) event->part];
4911 position
4912 = Fcons (window,
4913 Fcons (Qvertical_scroll_bar,
4914 Fcons (portion_whole,
4915 Fcons (make_number (event->timestamp),
4916 Fcons (part, Qnil)))));
4919 /* Always treat W32 scroll bar events as clicks. */
4920 event->modifiers |= click_modifier;
4923 /* Get the symbol we should use for the mouse click. */
4924 Lisp_Object head;
4926 head = modify_event_symbol (button,
4927 event->modifiers,
4928 Qmouse_click,
4929 Vlispy_mouse_stem,
4930 NULL, &mouse_syms,
4931 XVECTOR (mouse_syms)->size);
4932 return Fcons (head,
4933 Fcons (position,
4934 Qnil));
4937 case mouse_wheel:
4939 int part;
4940 FRAME_PTR f = XFRAME (event->frame_or_window);
4941 Lisp_Object window;
4942 Lisp_Object posn;
4943 Lisp_Object head, position;
4944 int row, column;
4946 /* Ignore mouse events that were made on frame that
4947 have been deleted. */
4948 if (! FRAME_LIVE_P (f))
4949 return Qnil;
4950 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4951 &column, &row, NULL, 1);
4952 window = window_from_coordinates (f, column, row, &part, 0);
4954 if (!WINDOWP (window))
4956 window = event->frame_or_window;
4957 posn = Qnil;
4959 else
4961 int pixcolumn, pixrow;
4962 column -= XINT (XWINDOW (window)->left);
4963 row -= XINT (XWINDOW (window)->top);
4964 glyph_to_pixel_coords (XWINDOW(window), column, row,
4965 &pixcolumn, &pixrow);
4966 XSETINT (event->x, pixcolumn);
4967 XSETINT (event->y, pixrow);
4969 if (part == 1)
4970 posn = Qmode_line;
4971 else if (part == 2)
4972 posn = Qvertical_line;
4973 else if (part == 3)
4974 posn = Qheader_line;
4975 else
4976 XSETINT (posn,
4977 buffer_posn_from_coords (XWINDOW (window),
4978 &column, &row));
4982 Lisp_Object head, position;
4984 position
4985 = Fcons (window,
4986 Fcons (posn,
4987 Fcons (Fcons (event->x, event->y),
4988 Fcons (make_number (event->timestamp),
4989 Qnil))));
4991 head = modify_event_symbol (0, event->modifiers,
4992 Qmouse_wheel, Qnil,
4993 lispy_mouse_wheel_names,
4994 &mouse_wheel_syms, 1);
4995 return Fcons (head,
4996 Fcons (position,
4997 Fcons (make_number (event->code),
4998 Qnil)));
5001 #endif /* WINDOWSNT */
5003 case drag_n_drop:
5005 int part;
5006 FRAME_PTR f;
5007 Lisp_Object window;
5008 Lisp_Object posn;
5009 Lisp_Object files;
5010 int row, column;
5012 /* The frame_or_window field should be a cons of the frame in
5013 which the event occurred and a list of the filenames
5014 dropped. */
5015 if (! CONSP (event->frame_or_window))
5016 abort ();
5018 f = XFRAME (XCAR (event->frame_or_window));
5019 files = XCDR (event->frame_or_window);
5021 /* Ignore mouse events that were made on frames that
5022 have been deleted. */
5023 if (! FRAME_LIVE_P (f))
5024 return Qnil;
5025 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
5026 &column, &row, NULL, 1);
5027 window = window_from_coordinates (f, column, row, &part, 0);
5029 if (!WINDOWP (window))
5031 window = XCAR (event->frame_or_window);
5032 posn = Qnil;
5034 else
5036 /* It's an event in window `window' at frame coordinates
5037 event->x/ event->y. */
5038 struct window *w = XWINDOW (window);
5040 /* Get window relative coordinates. */
5041 int wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (event->x));
5042 int wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (event->y));
5044 /* Set event coordinates to window-relative coordinates
5045 for constructing the Lisp event below. */
5046 XSETINT (event->x, wx);
5047 XSETINT (event->y, wy);
5049 if (part == 1)
5050 posn = Qmode_line;
5051 else if (part == 2)
5052 posn = Qvertical_line;
5053 else if (part == 3)
5054 posn = Qheader_line;
5055 else
5056 XSETINT (posn, buffer_posn_from_coords (w, &wx, &wy));
5060 Lisp_Object head, position;
5062 position
5063 = Fcons (window,
5064 Fcons (posn,
5065 Fcons (Fcons (event->x, event->y),
5066 Fcons (make_number (event->timestamp),
5067 Qnil))));
5069 head = modify_event_symbol (0, event->modifiers,
5070 Qdrag_n_drop, Qnil,
5071 lispy_drag_n_drop_names,
5072 &drag_n_drop_syms, 1);
5073 return Fcons (head,
5074 Fcons (position,
5075 Fcons (files,
5076 Qnil)));
5079 #endif /* HAVE_MOUSE */
5081 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
5082 case MENU_BAR_EVENT:
5083 if (EQ (event->arg, event->frame_or_window))
5084 /* This is the prefix key. We translate this to
5085 `(menu_bar)' because the code in keyboard.c for menu
5086 events, which we use, relies on this. */
5087 return Fcons (Qmenu_bar, Qnil);
5088 return event->arg;
5089 #endif
5091 case TOOL_BAR_EVENT:
5092 if (EQ (event->arg, event->frame_or_window))
5093 /* This is the prefix key. We translate this to
5094 `(tool_bar)' because the code in keyboard.c for menu
5095 events, which we use, relies on this. */
5096 return Fcons (Qtool_bar, Qnil);
5097 else if (SYMBOLP (event->arg))
5098 return apply_modifiers (event->modifiers, event->arg);
5099 return event->arg;
5101 case USER_SIGNAL_EVENT:
5102 /* A user signal. */
5103 return *lispy_user_signals[event->code];
5105 /* The 'kind' field of the event is something we don't recognize. */
5106 default:
5107 abort ();
5111 #ifdef HAVE_MOUSE
5113 static Lisp_Object
5114 make_lispy_movement (frame, bar_window, part, x, y, time)
5115 FRAME_PTR frame;
5116 Lisp_Object bar_window;
5117 enum scroll_bar_part part;
5118 Lisp_Object x, y;
5119 unsigned long time;
5121 /* Is it a scroll bar movement? */
5122 if (frame && ! NILP (bar_window))
5124 Lisp_Object part_sym;
5126 part_sym = *scroll_bar_parts[(int) part];
5127 return Fcons (Qscroll_bar_movement,
5128 (Fcons (Fcons (bar_window,
5129 Fcons (Qvertical_scroll_bar,
5130 Fcons (Fcons (x, y),
5131 Fcons (make_number (time),
5132 Fcons (part_sym,
5133 Qnil))))),
5134 Qnil)));
5137 /* Or is it an ordinary mouse movement? */
5138 else
5140 int area;
5141 Lisp_Object window;
5142 Lisp_Object posn;
5144 if (frame)
5145 /* It's in a frame; which window on that frame? */
5146 window = window_from_coordinates (frame, XINT (x), XINT (y), &area, 0);
5147 else
5148 window = Qnil;
5150 if (WINDOWP (window))
5152 struct window *w = XWINDOW (window);
5153 int wx, wy;
5155 /* Get window relative coordinates. */
5156 wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (x));
5157 wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (y));
5158 XSETINT (x, wx);
5159 XSETINT (y, wy);
5161 if (area == 1)
5162 posn = Qmode_line;
5163 else if (area == 2)
5164 posn = Qvertical_line;
5165 else if (area == 3)
5166 posn = Qheader_line;
5167 else
5168 XSETINT (posn, buffer_posn_from_coords (w, &wx, &wy));
5170 else if (frame != 0)
5172 XSETFRAME (window, frame);
5173 posn = Qnil;
5175 else
5177 window = Qnil;
5178 posn = Qnil;
5179 XSETFASTINT (x, 0);
5180 XSETFASTINT (y, 0);
5183 return Fcons (Qmouse_movement,
5184 Fcons (Fcons (window,
5185 Fcons (posn,
5186 Fcons (Fcons (x, y),
5187 Fcons (make_number (time),
5188 Qnil)))),
5189 Qnil));
5193 #endif /* HAVE_MOUSE */
5195 /* Construct a switch frame event. */
5196 static Lisp_Object
5197 make_lispy_switch_frame (frame)
5198 Lisp_Object frame;
5200 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
5203 /* Manipulating modifiers. */
5205 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
5207 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
5208 SYMBOL's name of the end of the modifiers; the string from this
5209 position is the unmodified symbol name.
5211 This doesn't use any caches. */
5213 static int
5214 parse_modifiers_uncached (symbol, modifier_end)
5215 Lisp_Object symbol;
5216 int *modifier_end;
5218 struct Lisp_String *name;
5219 int i;
5220 int modifiers;
5222 CHECK_SYMBOL (symbol, 1);
5224 modifiers = 0;
5225 name = XSYMBOL (symbol)->name;
5227 for (i = 0; i+2 <= STRING_BYTES (name); )
5229 int this_mod_end = 0;
5230 int this_mod = 0;
5232 /* See if the name continues with a modifier word.
5233 Check that the word appears, but don't check what follows it.
5234 Set this_mod and this_mod_end to record what we find. */
5236 switch (name->data[i])
5238 #define SINGLE_LETTER_MOD(BIT) \
5239 (this_mod_end = i + 1, this_mod = BIT)
5241 case 'A':
5242 SINGLE_LETTER_MOD (alt_modifier);
5243 break;
5245 case 'C':
5246 SINGLE_LETTER_MOD (ctrl_modifier);
5247 break;
5249 case 'H':
5250 SINGLE_LETTER_MOD (hyper_modifier);
5251 break;
5253 case 'M':
5254 SINGLE_LETTER_MOD (meta_modifier);
5255 break;
5257 case 'S':
5258 SINGLE_LETTER_MOD (shift_modifier);
5259 break;
5261 case 's':
5262 SINGLE_LETTER_MOD (super_modifier);
5263 break;
5265 #undef SINGLE_LETTER_MOD
5268 /* If we found no modifier, stop looking for them. */
5269 if (this_mod_end == 0)
5270 break;
5272 /* Check there is a dash after the modifier, so that it
5273 really is a modifier. */
5274 if (this_mod_end >= STRING_BYTES (name)
5275 || name->data[this_mod_end] != '-')
5276 break;
5278 /* This modifier is real; look for another. */
5279 modifiers |= this_mod;
5280 i = this_mod_end + 1;
5283 /* Should we include the `click' modifier? */
5284 if (! (modifiers & (down_modifier | drag_modifier
5285 | double_modifier | triple_modifier))
5286 && i + 7 == STRING_BYTES (name)
5287 && strncmp (name->data + i, "mouse-", 6) == 0
5288 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
5289 modifiers |= click_modifier;
5291 if (modifier_end)
5292 *modifier_end = i;
5294 return modifiers;
5297 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
5298 prepended to the string BASE[0..BASE_LEN-1].
5299 This doesn't use any caches. */
5300 static Lisp_Object
5301 apply_modifiers_uncached (modifiers, base, base_len, base_len_byte)
5302 int modifiers;
5303 char *base;
5304 int base_len, base_len_byte;
5306 /* Since BASE could contain nulls, we can't use intern here; we have
5307 to use Fintern, which expects a genuine Lisp_String, and keeps a
5308 reference to it. */
5309 char *new_mods
5310 = (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
5311 int mod_len;
5314 char *p = new_mods;
5316 /* Only the event queue may use the `up' modifier; it should always
5317 be turned into a click or drag event before presented to lisp code. */
5318 if (modifiers & up_modifier)
5319 abort ();
5321 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
5322 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
5323 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
5324 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
5325 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
5326 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
5327 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
5328 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
5329 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
5330 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
5331 /* The click modifier is denoted by the absence of other modifiers. */
5333 *p = '\0';
5335 mod_len = p - new_mods;
5339 Lisp_Object new_name;
5341 new_name = make_uninit_multibyte_string (mod_len + base_len,
5342 mod_len + base_len_byte);
5343 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
5344 bcopy (base, XSTRING (new_name)->data + mod_len, base_len_byte);
5346 return Fintern (new_name, Qnil);
5351 static char *modifier_names[] =
5353 "up", "down", "drag", "click", "double", "triple", 0, 0,
5354 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5355 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
5357 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
5359 static Lisp_Object modifier_symbols;
5361 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
5362 static Lisp_Object
5363 lispy_modifier_list (modifiers)
5364 int modifiers;
5366 Lisp_Object modifier_list;
5367 int i;
5369 modifier_list = Qnil;
5370 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
5371 if (modifiers & (1<<i))
5372 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
5373 modifier_list);
5375 return modifier_list;
5379 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
5380 where UNMODIFIED is the unmodified form of SYMBOL,
5381 MASK is the set of modifiers present in SYMBOL's name.
5382 This is similar to parse_modifiers_uncached, but uses the cache in
5383 SYMBOL's Qevent_symbol_element_mask property, and maintains the
5384 Qevent_symbol_elements property. */
5386 Lisp_Object
5387 parse_modifiers (symbol)
5388 Lisp_Object symbol;
5390 Lisp_Object elements;
5392 elements = Fget (symbol, Qevent_symbol_element_mask);
5393 if (CONSP (elements))
5394 return elements;
5395 else
5397 int end;
5398 int modifiers = parse_modifiers_uncached (symbol, &end);
5399 Lisp_Object unmodified;
5400 Lisp_Object mask;
5402 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
5403 STRING_BYTES (XSYMBOL (symbol)->name) - end),
5404 Qnil);
5406 if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1))
5407 abort ();
5408 XSETFASTINT (mask, modifiers);
5409 elements = Fcons (unmodified, Fcons (mask, Qnil));
5411 /* Cache the parsing results on SYMBOL. */
5412 Fput (symbol, Qevent_symbol_element_mask,
5413 elements);
5414 Fput (symbol, Qevent_symbol_elements,
5415 Fcons (unmodified, lispy_modifier_list (modifiers)));
5417 /* Since we know that SYMBOL is modifiers applied to unmodified,
5418 it would be nice to put that in unmodified's cache.
5419 But we can't, since we're not sure that parse_modifiers is
5420 canonical. */
5422 return elements;
5426 /* Apply the modifiers MODIFIERS to the symbol BASE.
5427 BASE must be unmodified.
5429 This is like apply_modifiers_uncached, but uses BASE's
5430 Qmodifier_cache property, if present. It also builds
5431 Qevent_symbol_elements properties, since it has that info anyway.
5433 apply_modifiers copies the value of BASE's Qevent_kind property to
5434 the modified symbol. */
5435 static Lisp_Object
5436 apply_modifiers (modifiers, base)
5437 int modifiers;
5438 Lisp_Object base;
5440 Lisp_Object cache, index, entry, new_symbol;
5442 /* Mask out upper bits. We don't know where this value's been. */
5443 modifiers &= ((EMACS_INT)1 << VALBITS) - 1;
5445 /* The click modifier never figures into cache indices. */
5446 cache = Fget (base, Qmodifier_cache);
5447 XSETFASTINT (index, (modifiers & ~click_modifier));
5448 entry = assq_no_quit (index, cache);
5450 if (CONSP (entry))
5451 new_symbol = XCDR (entry);
5452 else
5454 /* We have to create the symbol ourselves. */
5455 new_symbol = apply_modifiers_uncached (modifiers,
5456 XSYMBOL (base)->name->data,
5457 XSYMBOL (base)->name->size,
5458 STRING_BYTES (XSYMBOL (base)->name));
5460 /* Add the new symbol to the base's cache. */
5461 entry = Fcons (index, new_symbol);
5462 Fput (base, Qmodifier_cache, Fcons (entry, cache));
5464 /* We have the parsing info now for free, so add it to the caches. */
5465 XSETFASTINT (index, modifiers);
5466 Fput (new_symbol, Qevent_symbol_element_mask,
5467 Fcons (base, Fcons (index, Qnil)));
5468 Fput (new_symbol, Qevent_symbol_elements,
5469 Fcons (base, lispy_modifier_list (modifiers)));
5472 /* Make sure this symbol is of the same kind as BASE.
5474 You'd think we could just set this once and for all when we
5475 intern the symbol above, but reorder_modifiers may call us when
5476 BASE's property isn't set right; we can't assume that just
5477 because it has a Qmodifier_cache property it must have its
5478 Qevent_kind set right as well. */
5479 if (NILP (Fget (new_symbol, Qevent_kind)))
5481 Lisp_Object kind;
5483 kind = Fget (base, Qevent_kind);
5484 if (! NILP (kind))
5485 Fput (new_symbol, Qevent_kind, kind);
5488 return new_symbol;
5492 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
5493 return a symbol with the modifiers placed in the canonical order.
5494 Canonical order is alphabetical, except for down and drag, which
5495 always come last. The 'click' modifier is never written out.
5497 Fdefine_key calls this to make sure that (for example) C-M-foo
5498 and M-C-foo end up being equivalent in the keymap. */
5500 Lisp_Object
5501 reorder_modifiers (symbol)
5502 Lisp_Object symbol;
5504 /* It's hopefully okay to write the code this way, since everything
5505 will soon be in caches, and no consing will be done at all. */
5506 Lisp_Object parsed;
5508 parsed = parse_modifiers (symbol);
5509 return apply_modifiers ((int) XINT (XCAR (XCDR (parsed))),
5510 XCAR (parsed));
5514 /* For handling events, we often want to produce a symbol whose name
5515 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
5516 to some base, like the name of a function key or mouse button.
5517 modify_event_symbol produces symbols of this sort.
5519 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
5520 is the name of the i'th symbol. TABLE_SIZE is the number of elements
5521 in the table.
5523 Alternatively, NAME_ALIST_OR_STEM is either an alist mapping codes
5524 into symbol names, or a string specifying a name stem used to
5525 construct a symbol name or the form `STEM-N', where N is the decimal
5526 representation of SYMBOL_NUM. NAME_ALIST_OR_STEM is used if it is
5527 non-nil; otherwise NAME_TABLE is used.
5529 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
5530 persist between calls to modify_event_symbol that it can use to
5531 store a cache of the symbols it's generated for this NAME_TABLE
5532 before. The object stored there may be a vector or an alist.
5534 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
5536 MODIFIERS is a set of modifier bits (as given in struct input_events)
5537 whose prefixes should be applied to the symbol name.
5539 SYMBOL_KIND is the value to be placed in the event_kind property of
5540 the returned symbol.
5542 The symbols we create are supposed to have an
5543 `event-symbol-elements' property, which lists the modifiers present
5544 in the symbol's name. */
5546 static Lisp_Object
5547 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist_or_stem,
5548 name_table, symbol_table, table_size)
5549 int symbol_num;
5550 unsigned modifiers;
5551 Lisp_Object symbol_kind;
5552 Lisp_Object name_alist_or_stem;
5553 char **name_table;
5554 Lisp_Object *symbol_table;
5555 unsigned int table_size;
5557 Lisp_Object value;
5558 Lisp_Object symbol_int;
5560 /* Get rid of the "vendor-specific" bit here. */
5561 XSETINT (symbol_int, symbol_num & 0xffffff);
5563 /* Is this a request for a valid symbol? */
5564 if (symbol_num < 0 || symbol_num >= table_size)
5565 return Qnil;
5567 if (CONSP (*symbol_table))
5568 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
5570 /* If *symbol_table doesn't seem to be initialized properly, fix that.
5571 *symbol_table should be a lisp vector TABLE_SIZE elements long,
5572 where the Nth element is the symbol for NAME_TABLE[N], or nil if
5573 we've never used that symbol before. */
5574 else
5576 if (! VECTORP (*symbol_table)
5577 || XVECTOR (*symbol_table)->size != table_size)
5579 Lisp_Object size;
5581 XSETFASTINT (size, table_size);
5582 *symbol_table = Fmake_vector (size, Qnil);
5585 value = XVECTOR (*symbol_table)->contents[symbol_num];
5588 /* Have we already used this symbol before? */
5589 if (NILP (value))
5591 /* No; let's create it. */
5592 if (CONSP (name_alist_or_stem))
5593 value = Fcdr_safe (Fassq (symbol_int, name_alist_or_stem));
5594 else if (STRINGP (name_alist_or_stem))
5596 int len = STRING_BYTES (XSTRING (name_alist_or_stem));
5597 char *buf = (char *) alloca (len + 50);
5598 sprintf (buf, "%s-%d", XSTRING (name_alist_or_stem)->data,
5599 XINT (symbol_int) + 1);
5600 value = intern (buf);
5602 else if (name_table != 0 && name_table[symbol_num])
5603 value = intern (name_table[symbol_num]);
5605 #ifdef HAVE_WINDOW_SYSTEM
5606 if (NILP (value))
5608 char *name = x_get_keysym_name (symbol_num);
5609 if (name)
5610 value = intern (name);
5612 #endif
5614 if (NILP (value))
5616 char buf[20];
5617 sprintf (buf, "key-%d", symbol_num);
5618 value = intern (buf);
5621 if (CONSP (*symbol_table))
5622 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
5623 else
5624 XVECTOR (*symbol_table)->contents[symbol_num] = value;
5626 /* Fill in the cache entries for this symbol; this also
5627 builds the Qevent_symbol_elements property, which the user
5628 cares about. */
5629 apply_modifiers (modifiers & click_modifier, value);
5630 Fput (value, Qevent_kind, symbol_kind);
5633 /* Apply modifiers to that symbol. */
5634 return apply_modifiers (modifiers, value);
5637 /* Convert a list that represents an event type,
5638 such as (ctrl meta backspace), into the usual representation of that
5639 event type as a number or a symbol. */
5641 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
5642 "Convert the event description list EVENT-DESC to an event type.\n\
5643 EVENT-DESC should contain one base event type (a character or symbol)\n\
5644 and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\
5645 drag, down, double or triple). The base must be last.\n\
5646 The return value is an event type (a character or symbol) which\n\
5647 has the same base event type and all the specified modifiers.")
5648 (event_desc)
5649 Lisp_Object event_desc;
5651 Lisp_Object base;
5652 int modifiers = 0;
5653 Lisp_Object rest;
5655 base = Qnil;
5656 rest = event_desc;
5657 while (CONSP (rest))
5659 Lisp_Object elt;
5660 int this = 0;
5662 elt = XCAR (rest);
5663 rest = XCDR (rest);
5665 /* Given a symbol, see if it is a modifier name. */
5666 if (SYMBOLP (elt) && CONSP (rest))
5667 this = parse_solitary_modifier (elt);
5669 if (this != 0)
5670 modifiers |= this;
5671 else if (!NILP (base))
5672 error ("Two bases given in one event");
5673 else
5674 base = elt;
5678 /* Let the symbol A refer to the character A. */
5679 if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1)
5680 XSETINT (base, XSYMBOL (base)->name->data[0]);
5682 if (INTEGERP (base))
5684 /* Turn (shift a) into A. */
5685 if ((modifiers & shift_modifier) != 0
5686 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
5688 XSETINT (base, XINT (base) - ('a' - 'A'));
5689 modifiers &= ~shift_modifier;
5692 /* Turn (control a) into C-a. */
5693 if (modifiers & ctrl_modifier)
5694 return make_number ((modifiers & ~ctrl_modifier)
5695 | make_ctrl_char (XINT (base)));
5696 else
5697 return make_number (modifiers | XINT (base));
5699 else if (SYMBOLP (base))
5700 return apply_modifiers (modifiers, base);
5701 else
5703 error ("Invalid base event");
5704 return Qnil;
5708 /* Try to recognize SYMBOL as a modifier name.
5709 Return the modifier flag bit, or 0 if not recognized. */
5711 static int
5712 parse_solitary_modifier (symbol)
5713 Lisp_Object symbol;
5715 struct Lisp_String *name = XSYMBOL (symbol)->name;
5717 switch (name->data[0])
5719 #define SINGLE_LETTER_MOD(BIT) \
5720 if (STRING_BYTES (name) == 1) \
5721 return BIT;
5723 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
5724 if (LEN == STRING_BYTES (name) \
5725 && ! strncmp (name->data, NAME, LEN)) \
5726 return BIT;
5728 case 'A':
5729 SINGLE_LETTER_MOD (alt_modifier);
5730 break;
5732 case 'a':
5733 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
5734 break;
5736 case 'C':
5737 SINGLE_LETTER_MOD (ctrl_modifier);
5738 break;
5740 case 'c':
5741 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
5742 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
5743 break;
5745 case 'H':
5746 SINGLE_LETTER_MOD (hyper_modifier);
5747 break;
5749 case 'h':
5750 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
5751 break;
5753 case 'M':
5754 SINGLE_LETTER_MOD (meta_modifier);
5755 break;
5757 case 'm':
5758 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
5759 break;
5761 case 'S':
5762 SINGLE_LETTER_MOD (shift_modifier);
5763 break;
5765 case 's':
5766 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
5767 MULTI_LETTER_MOD (super_modifier, "super", 5);
5768 SINGLE_LETTER_MOD (super_modifier);
5769 break;
5771 case 'd':
5772 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
5773 MULTI_LETTER_MOD (down_modifier, "down", 4);
5774 MULTI_LETTER_MOD (double_modifier, "double", 6);
5775 break;
5777 case 't':
5778 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
5779 break;
5781 #undef SINGLE_LETTER_MOD
5782 #undef MULTI_LETTER_MOD
5785 return 0;
5788 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
5789 Such a list is not valid as an event,
5790 but it can be a Lucid-style event type list. */
5793 lucid_event_type_list_p (object)
5794 Lisp_Object object;
5796 Lisp_Object tail;
5798 if (! CONSP (object))
5799 return 0;
5801 for (tail = object; CONSP (tail); tail = XCDR (tail))
5803 Lisp_Object elt;
5804 elt = XCAR (tail);
5805 if (! (INTEGERP (elt) || SYMBOLP (elt)))
5806 return 0;
5809 return NILP (tail);
5812 /* Store into *addr a value nonzero if terminal input chars are available.
5813 Serves the purpose of ioctl (0, FIONREAD, addr)
5814 but works even if FIONREAD does not exist.
5815 (In fact, this may actually read some input.)
5817 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe. */
5819 static void
5820 get_input_pending (addr, do_timers_now)
5821 int *addr;
5822 int do_timers_now;
5824 /* First of all, have we already counted some input? */
5825 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
5827 /* If input is being read as it arrives, and we have none, there is none. */
5828 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
5829 return;
5831 /* Try to read some input and see how much we get. */
5832 gobble_input (0);
5833 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
5836 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
5838 void
5839 gobble_input (expected)
5840 int expected;
5842 #ifndef VMS
5843 #ifdef SIGIO
5844 if (interrupt_input)
5846 SIGMASKTYPE mask;
5847 mask = sigblock (sigmask (SIGIO));
5848 read_avail_input (expected);
5849 sigsetmask (mask);
5851 else
5852 #ifdef POLL_FOR_INPUT
5853 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
5855 SIGMASKTYPE mask;
5856 mask = sigblock (sigmask (SIGALRM));
5857 read_avail_input (expected);
5858 sigsetmask (mask);
5860 else
5861 #endif
5862 #endif
5863 read_avail_input (expected);
5864 #endif
5867 /* Put a buffer_switch_event in the buffer
5868 so that read_key_sequence will notice the new current buffer. */
5870 void
5871 record_asynch_buffer_change ()
5873 struct input_event event;
5874 Lisp_Object tem;
5876 event.kind = buffer_switch_event;
5877 event.frame_or_window = Qnil;
5878 event.arg = Qnil;
5880 #ifdef subprocesses
5881 /* We don't need a buffer-switch event unless Emacs is waiting for input.
5882 The purpose of the event is to make read_key_sequence look up the
5883 keymaps again. If we aren't in read_key_sequence, we don't need one,
5884 and the event could cause trouble by messing up (input-pending-p). */
5885 tem = Fwaiting_for_user_input_p ();
5886 if (NILP (tem))
5887 return;
5888 #else
5889 /* We never need these events if we have no asynchronous subprocesses. */
5890 return;
5891 #endif
5893 /* Make sure no interrupt happens while storing the event. */
5894 #ifdef SIGIO
5895 if (interrupt_input)
5897 SIGMASKTYPE mask;
5898 mask = sigblock (sigmask (SIGIO));
5899 kbd_buffer_store_event (&event);
5900 sigsetmask (mask);
5902 else
5903 #endif
5905 stop_polling ();
5906 kbd_buffer_store_event (&event);
5907 start_polling ();
5911 #ifndef VMS
5913 /* Read any terminal input already buffered up by the system
5914 into the kbd_buffer, but do not wait.
5916 EXPECTED should be nonzero if the caller knows there is some input.
5918 Except on VMS, all input is read by this function.
5919 If interrupt_input is nonzero, this function MUST be called
5920 only when SIGIO is blocked.
5922 Returns the number of keyboard chars read, or -1 meaning
5923 this is a bad time to try to read input. */
5925 static int
5926 read_avail_input (expected)
5927 int expected;
5929 struct input_event buf[KBD_BUFFER_SIZE];
5930 register int i;
5931 int nread;
5933 if (read_socket_hook)
5934 /* No need for FIONREAD or fcntl; just say don't wait. */
5935 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE, expected);
5936 else
5938 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
5939 the kbd_buffer can really hold. That may prevent loss
5940 of characters on some systems when input is stuffed at us. */
5941 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
5942 int n_to_read;
5944 /* Determine how many characters we should *try* to read. */
5945 #ifdef WINDOWSNT
5946 return 0;
5947 #else /* not WINDOWSNT */
5948 #ifdef MSDOS
5949 n_to_read = dos_keysns ();
5950 if (n_to_read == 0)
5951 return 0;
5952 #else /* not MSDOS */
5953 #ifdef FIONREAD
5954 /* Find out how much input is available. */
5955 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
5956 /* Formerly simply reported no input, but that sometimes led to
5957 a failure of Emacs to terminate.
5958 SIGHUP seems appropriate if we can't reach the terminal. */
5959 /* ??? Is it really right to send the signal just to this process
5960 rather than to the whole process group?
5961 Perhaps on systems with FIONREAD Emacs is alone in its group. */
5962 kill (getpid (), SIGHUP);
5963 if (n_to_read == 0)
5964 return 0;
5965 if (n_to_read > sizeof cbuf)
5966 n_to_read = sizeof cbuf;
5967 #else /* no FIONREAD */
5968 #if defined (USG) || defined (DGUX)
5969 /* Read some input if available, but don't wait. */
5970 n_to_read = sizeof cbuf;
5971 fcntl (input_fd, F_SETFL, O_NDELAY);
5972 #else
5973 you lose;
5974 #endif
5975 #endif
5976 #endif /* not MSDOS */
5977 #endif /* not WINDOWSNT */
5979 /* Now read; for one reason or another, this will not block.
5980 NREAD is set to the number of chars read. */
5983 #ifdef MSDOS
5984 cbuf[0] = dos_keyread ();
5985 nread = 1;
5986 #else
5987 nread = emacs_read (input_fd, cbuf, n_to_read);
5988 #endif
5989 /* POSIX infers that processes which are not in the session leader's
5990 process group won't get SIGHUP's at logout time. BSDI adheres to
5991 this part standard and returns -1 from read (0) with errno==EIO
5992 when the control tty is taken away.
5993 Jeffrey Honig <jch@bsdi.com> says this is generally safe. */
5994 if (nread == -1 && errno == EIO)
5995 kill (0, SIGHUP);
5996 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
5997 /* The kernel sometimes fails to deliver SIGHUP for ptys.
5998 This looks incorrect, but it isn't, because _BSD causes
5999 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
6000 and that causes a value other than 0 when there is no input. */
6001 if (nread == 0)
6002 kill (0, SIGHUP);
6003 #endif
6005 while (
6006 /* We used to retry the read if it was interrupted.
6007 But this does the wrong thing when O_NDELAY causes
6008 an EAGAIN error. Does anybody know of a situation
6009 where a retry is actually needed? */
6010 #if 0
6011 nread < 0 && (errno == EAGAIN
6012 #ifdef EFAULT
6013 || errno == EFAULT
6014 #endif
6015 #ifdef EBADSLT
6016 || errno == EBADSLT
6017 #endif
6019 #else
6021 #endif
6024 #ifndef FIONREAD
6025 #if defined (USG) || defined (DGUX)
6026 fcntl (input_fd, F_SETFL, 0);
6027 #endif /* USG or DGUX */
6028 #endif /* no FIONREAD */
6029 for (i = 0; i < nread; i++)
6031 buf[i].kind = ascii_keystroke;
6032 buf[i].modifiers = 0;
6033 if (meta_key == 1 && (cbuf[i] & 0x80))
6034 buf[i].modifiers = meta_modifier;
6035 if (meta_key != 2)
6036 cbuf[i] &= ~0x80;
6038 buf[i].code = cbuf[i];
6039 buf[i].frame_or_window = selected_frame;
6040 buf[i].arg = Qnil;
6044 /* Scan the chars for C-g and store them in kbd_buffer. */
6045 for (i = 0; i < nread; i++)
6047 kbd_buffer_store_event (&buf[i]);
6048 /* Don't look at input that follows a C-g too closely.
6049 This reduces lossage due to autorepeat on C-g. */
6050 if (buf[i].kind == ascii_keystroke
6051 && buf[i].code == quit_char)
6052 break;
6055 return nread;
6057 #endif /* not VMS */
6059 #ifdef SIGIO /* for entire page */
6060 /* Note SIGIO has been undef'd if FIONREAD is missing. */
6062 SIGTYPE
6063 input_available_signal (signo)
6064 int signo;
6066 /* Must preserve main program's value of errno. */
6067 int old_errno = errno;
6068 #ifdef BSD4_1
6069 extern int select_alarmed;
6070 #endif
6072 #if defined (USG) && !defined (POSIX_SIGNALS)
6073 /* USG systems forget handlers when they are used;
6074 must reestablish each time */
6075 signal (signo, input_available_signal);
6076 #endif /* USG */
6078 #ifdef BSD4_1
6079 sigisheld (SIGIO);
6080 #endif
6082 if (input_available_clear_time)
6083 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
6085 while (1)
6087 int nread;
6088 nread = read_avail_input (1);
6089 /* -1 means it's not ok to read the input now.
6090 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
6091 0 means there was no keyboard input available. */
6092 if (nread <= 0)
6093 break;
6095 #ifdef BSD4_1
6096 select_alarmed = 1; /* Force the select emulator back to life */
6097 #endif
6100 #ifdef BSD4_1
6101 sigfree ();
6102 #endif
6103 errno = old_errno;
6105 #endif /* SIGIO */
6107 /* Send ourselves a SIGIO.
6109 This function exists so that the UNBLOCK_INPUT macro in
6110 blockinput.h can have some way to take care of input we put off
6111 dealing with, without assuming that every file which uses
6112 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
6113 void
6114 reinvoke_input_signal ()
6116 #ifdef SIGIO
6117 kill (getpid (), SIGIO);
6118 #endif
6123 /* Return the prompt-string of a sparse keymap.
6124 This is the first element which is a string.
6125 Return nil if there is none. */
6127 Lisp_Object
6128 map_prompt (map)
6129 Lisp_Object map;
6131 while (CONSP (map))
6133 register Lisp_Object tem;
6134 tem = Fcar (map);
6135 if (STRINGP (tem))
6136 return tem;
6137 map = Fcdr (map);
6139 return Qnil;
6142 static void menu_bar_item P_ ((Lisp_Object, Lisp_Object));
6143 static void menu_bar_one_keymap P_ ((Lisp_Object));
6145 /* These variables hold the vector under construction within
6146 menu_bar_items and its subroutines, and the current index
6147 for storing into that vector. */
6148 static Lisp_Object menu_bar_items_vector;
6149 static int menu_bar_items_index;
6151 /* Return a vector of menu items for a menu bar, appropriate
6152 to the current buffer. Each item has three elements in the vector:
6153 KEY STRING MAPLIST.
6155 OLD is an old vector we can optionally reuse, or nil. */
6157 Lisp_Object
6158 menu_bar_items (old)
6159 Lisp_Object old;
6161 /* The number of keymaps we're scanning right now, and the number of
6162 keymaps we have allocated space for. */
6163 int nmaps;
6165 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
6166 in the current keymaps, or nil where it is not a prefix. */
6167 Lisp_Object *maps;
6169 Lisp_Object def, tem, tail;
6171 Lisp_Object result;
6173 int mapno;
6174 Lisp_Object oquit;
6176 int i;
6178 struct gcpro gcpro1;
6180 /* In order to build the menus, we need to call the keymap
6181 accessors. They all call QUIT. But this function is called
6182 during redisplay, during which a quit is fatal. So inhibit
6183 quitting while building the menus.
6184 We do this instead of specbind because (1) errors will clear it anyway
6185 and (2) this avoids risk of specpdl overflow. */
6186 oquit = Vinhibit_quit;
6187 Vinhibit_quit = Qt;
6189 if (!NILP (old))
6190 menu_bar_items_vector = old;
6191 else
6192 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
6193 menu_bar_items_index = 0;
6195 GCPRO1 (menu_bar_items_vector);
6197 /* Build our list of keymaps.
6198 If we recognize a function key and replace its escape sequence in
6199 keybuf with its symbol, or if the sequence starts with a mouse
6200 click and we need to switch buffers, we jump back here to rebuild
6201 the initial keymaps from the current buffer. */
6203 Lisp_Object *tmaps;
6205 /* Should overriding-terminal-local-map and overriding-local-map apply? */
6206 if (!NILP (Voverriding_local_map_menu_flag))
6208 /* Yes, use them (if non-nil) as well as the global map. */
6209 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
6210 nmaps = 0;
6211 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6212 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6213 if (!NILP (Voverriding_local_map))
6214 maps[nmaps++] = Voverriding_local_map;
6216 else
6218 /* No, so use major and minor mode keymaps and keymap property. */
6219 int extra_maps = 2;
6220 Lisp_Object map = get_local_map (PT, current_buffer, keymap);
6221 if (!NILP (map))
6222 extra_maps = 3;
6223 nmaps = current_minor_maps (NULL, &tmaps);
6224 maps = (Lisp_Object *) alloca ((nmaps + extra_maps)
6225 * sizeof (maps[0]));
6226 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
6227 if (!NILP (map))
6228 maps[nmaps++] = map;
6229 maps[nmaps++] = get_local_map (PT, current_buffer, local_map);
6231 maps[nmaps++] = current_global_map;
6234 /* Look up in each map the dummy prefix key `menu-bar'. */
6236 result = Qnil;
6238 for (mapno = nmaps - 1; mapno >= 0; mapno--)
6239 if (!NILP (maps[mapno]))
6241 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0), 0);
6242 tem = Fkeymapp (def);
6243 if (!NILP (tem))
6244 menu_bar_one_keymap (def);
6247 /* Move to the end those items that should be at the end. */
6249 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCDR (tail))
6251 int i;
6252 int end = menu_bar_items_index;
6254 for (i = 0; i < end; i += 4)
6255 if (EQ (XCAR (tail), XVECTOR (menu_bar_items_vector)->contents[i]))
6257 Lisp_Object tem0, tem1, tem2, tem3;
6258 /* Move the item at index I to the end,
6259 shifting all the others forward. */
6260 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
6261 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
6262 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
6263 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
6264 if (end > i + 4)
6265 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
6266 &XVECTOR (menu_bar_items_vector)->contents[i],
6267 (end - i - 4) * sizeof (Lisp_Object));
6268 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
6269 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
6270 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
6271 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
6272 break;
6276 /* Add nil, nil, nil, nil at the end. */
6277 i = menu_bar_items_index;
6278 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
6280 Lisp_Object tem;
6281 tem = Fmake_vector (make_number (2 * i), Qnil);
6282 bcopy (XVECTOR (menu_bar_items_vector)->contents,
6283 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
6284 menu_bar_items_vector = tem;
6286 /* Add this item. */
6287 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6288 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6289 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6290 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6291 menu_bar_items_index = i;
6293 Vinhibit_quit = oquit;
6294 UNGCPRO;
6295 return menu_bar_items_vector;
6298 /* Scan one map KEYMAP, accumulating any menu items it defines
6299 in menu_bar_items_vector. */
6301 static Lisp_Object menu_bar_one_keymap_changed_items;
6303 static void
6304 menu_bar_one_keymap (keymap)
6305 Lisp_Object keymap;
6307 Lisp_Object tail, item;
6309 /* If KEYMAP is a symbol, its function definition is the keymap
6310 to use. */
6311 if (SYMBOLP (keymap))
6312 keymap = indirect_function (keymap);
6314 menu_bar_one_keymap_changed_items = Qnil;
6316 /* Loop over all keymap entries that have menu strings. */
6317 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
6319 item = XCAR (tail);
6320 if (CONSP (item))
6321 menu_bar_item (XCAR (item), XCDR (item));
6322 else if (VECTORP (item))
6324 /* Loop over the char values represented in the vector. */
6325 int len = XVECTOR (item)->size;
6326 int c;
6327 for (c = 0; c < len; c++)
6329 Lisp_Object character;
6330 XSETFASTINT (character, c);
6331 menu_bar_item (character, XVECTOR (item)->contents[c]);
6337 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
6338 If there's already an item for KEY, add this DEF to it. */
6340 Lisp_Object item_properties;
6342 static void
6343 menu_bar_item (key, item)
6344 Lisp_Object key, item;
6346 struct gcpro gcpro1;
6347 int i;
6348 Lisp_Object tem;
6350 if (EQ (item, Qundefined))
6352 /* If a map has an explicit `undefined' as definition,
6353 discard any previously made menu bar item. */
6355 for (i = 0; i < menu_bar_items_index; i += 4)
6356 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
6358 if (menu_bar_items_index > i + 4)
6359 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
6360 &XVECTOR (menu_bar_items_vector)->contents[i],
6361 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
6362 menu_bar_items_index -= 4;
6366 /* If this keymap has already contributed to this KEY,
6367 don't contribute to it a second time. */
6368 tem = Fmemq (key, menu_bar_one_keymap_changed_items);
6369 if (!NILP (tem) || NILP (item))
6370 return;
6372 menu_bar_one_keymap_changed_items
6373 = Fcons (key, menu_bar_one_keymap_changed_items);
6375 /* We add to menu_bar_one_keymap_changed_items before doing the
6376 parse_menu_item, so that if it turns out it wasn't a menu item,
6377 it still correctly hides any further menu item. */
6378 GCPRO1 (key);
6379 i = parse_menu_item (item, 0, 1);
6380 UNGCPRO;
6381 if (!i)
6382 return;
6384 item = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6386 /* Find any existing item for this KEY. */
6387 for (i = 0; i < menu_bar_items_index; i += 4)
6388 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
6389 break;
6391 /* If we did not find this KEY, add it at the end. */
6392 if (i == menu_bar_items_index)
6394 /* If vector is too small, get a bigger one. */
6395 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
6397 Lisp_Object tem;
6398 tem = Fmake_vector (make_number (2 * i), Qnil);
6399 bcopy (XVECTOR (menu_bar_items_vector)->contents,
6400 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
6401 menu_bar_items_vector = tem;
6404 /* Add this item. */
6405 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
6406 XVECTOR (menu_bar_items_vector)->contents[i++]
6407 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
6408 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (item, Qnil);
6409 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
6410 menu_bar_items_index = i;
6412 /* We did find an item for this KEY. Add ITEM to its list of maps. */
6413 else
6415 Lisp_Object old;
6416 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
6417 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (item, old);
6421 /* This is used as the handler when calling menu_item_eval_property. */
6422 static Lisp_Object
6423 menu_item_eval_property_1 (arg)
6424 Lisp_Object arg;
6426 /* If we got a quit from within the menu computation,
6427 quit all the way out of it. This takes care of C-] in the debugger. */
6428 if (CONSP (arg) && EQ (XCAR (arg), Qquit))
6429 Fsignal (Qquit, Qnil);
6431 return Qnil;
6434 /* Evaluate an expression and return the result (or nil if something
6435 went wrong). Used to evaluate dynamic parts of menu items. */
6436 Lisp_Object
6437 menu_item_eval_property (sexpr)
6438 Lisp_Object sexpr;
6440 int count = specpdl_ptr - specpdl;
6441 Lisp_Object val;
6442 specbind (Qinhibit_redisplay, Qt);
6443 val = internal_condition_case_1 (Feval, sexpr, Qerror,
6444 menu_item_eval_property_1);
6445 return unbind_to (count, val);
6448 /* This function parses a menu item and leaves the result in the
6449 vector item_properties.
6450 ITEM is a key binding, a possible menu item.
6451 If NOTREAL is nonzero, only check for equivalent key bindings, don't
6452 evaluate dynamic expressions in the menu item.
6453 INMENUBAR is > 0 when this is considered for an entry in a menu bar
6454 top level.
6455 INMENUBAR is < 0 when this is considered for an entry in a keyboard menu.
6456 parse_menu_item returns true if the item is a menu item and false
6457 otherwise. */
6460 parse_menu_item (item, notreal, inmenubar)
6461 Lisp_Object item;
6462 int notreal, inmenubar;
6464 Lisp_Object def, tem, item_string, start;
6465 Lisp_Object cachelist;
6466 Lisp_Object filter;
6467 Lisp_Object keyhint;
6468 int i;
6469 int newcache = 0;
6471 cachelist = Qnil;
6472 filter = Qnil;
6473 keyhint = Qnil;
6475 if (!CONSP (item))
6476 return 0;
6478 /* Create item_properties vector if necessary. */
6479 if (NILP (item_properties))
6480 item_properties
6481 = Fmake_vector (make_number (ITEM_PROPERTY_ENABLE + 1), Qnil);
6483 /* Initialize optional entries. */
6484 for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++)
6485 XVECTOR (item_properties)->contents[i] = Qnil;
6486 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = Qt;
6488 /* Save the item here to protect it from GC. */
6489 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ITEM] = item;
6491 item_string = XCAR (item);
6493 start = item;
6494 item = XCDR (item);
6495 if (STRINGP (item_string))
6497 /* Old format menu item. */
6498 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME] = item_string;
6500 /* Maybe help string. */
6501 if (CONSP (item) && STRINGP (XCAR (item)))
6503 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]
6504 = XCAR (item);
6505 start = item;
6506 item = XCDR (item);
6509 /* Maybe key binding cache. */
6510 if (CONSP (item) && CONSP (XCAR (item))
6511 && (NILP (XCAR (XCAR (item)))
6512 || VECTORP (XCAR (XCAR (item)))))
6514 cachelist = XCAR (item);
6515 item = XCDR (item);
6518 /* This is the real definition--the function to run. */
6519 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = item;
6521 /* Get enable property, if any. */
6522 if (SYMBOLP (item))
6524 tem = Fget (item, Qmenu_enable);
6525 if (!NILP (tem))
6526 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = tem;
6529 else if (EQ (item_string, Qmenu_item) && CONSP (item))
6531 /* New format menu item. */
6532 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME]
6533 = XCAR (item);
6534 start = XCDR (item);
6535 if (CONSP (start))
6537 /* We have a real binding. */
6538 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF]
6539 = XCAR (start);
6541 item = XCDR (start);
6542 /* Is there a cache list with key equivalences. */
6543 if (CONSP (item) && CONSP (XCAR (item)))
6545 cachelist = XCAR (item);
6546 item = XCDR (item);
6549 /* Parse properties. */
6550 while (CONSP (item) && CONSP (XCDR (item)))
6552 tem = XCAR (item);
6553 item = XCDR (item);
6555 if (EQ (tem, QCenable))
6556 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE]
6557 = XCAR (item);
6558 else if (EQ (tem, QCvisible) && !notreal)
6560 /* If got a visible property and that evaluates to nil
6561 then ignore this item. */
6562 tem = menu_item_eval_property (XCAR (item));
6563 if (NILP (tem))
6564 return 0;
6566 else if (EQ (tem, QChelp))
6567 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]
6568 = XCAR (item);
6569 else if (EQ (tem, QCfilter))
6570 filter = item;
6571 else if (EQ (tem, QCkey_sequence))
6573 tem = XCAR (item);
6574 if (NILP (cachelist)
6575 && (SYMBOLP (tem) || STRINGP (tem) || VECTORP (tem)))
6576 /* Be GC protected. Set keyhint to item instead of tem. */
6577 keyhint = item;
6579 else if (EQ (tem, QCkeys))
6581 tem = XCAR (item);
6582 if (CONSP (tem) || (STRINGP (tem) && NILP (cachelist)))
6583 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ]
6584 = tem;
6586 else if (EQ (tem, QCbutton) && CONSP (XCAR (item)))
6588 Lisp_Object type;
6589 tem = XCAR (item);
6590 type = XCAR (tem);
6591 if (EQ (type, QCtoggle) || EQ (type, QCradio))
6593 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]
6594 = XCDR (tem);
6595 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE]
6596 = type;
6599 item = XCDR (item);
6602 else if (inmenubar || !NILP (start))
6603 return 0;
6605 else
6606 return 0; /* not a menu item */
6608 /* If item string is not a string, evaluate it to get string.
6609 If we don't get a string, skip this item. */
6610 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
6611 if (!(STRINGP (item_string) || notreal))
6613 item_string = menu_item_eval_property (item_string);
6614 if (!STRINGP (item_string))
6615 return 0;
6616 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME] = item_string;
6619 /* If got a filter apply it on definition. */
6620 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6621 if (!NILP (filter))
6623 def = menu_item_eval_property (list2 (XCAR (filter),
6624 list2 (Qquote, def)));
6626 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = def;
6629 /* If we got no definition, this item is just unselectable text which
6630 is OK in a submenu but not in the menubar. */
6631 if (NILP (def))
6632 return (inmenubar ? 0 : 1);
6634 /* Enable or disable selection of item. */
6635 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
6636 if (!EQ (tem, Qt))
6638 if (notreal)
6639 tem = Qt;
6640 else
6641 tem = menu_item_eval_property (tem);
6642 if (inmenubar && NILP (tem))
6643 return 0; /* Ignore disabled items in menu bar. */
6644 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = tem;
6647 /* See if this is a separate pane or a submenu. */
6648 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6649 tem = get_keymap_1 (def, 0, 1);
6650 /* For a subkeymap, just record its details and exit. */
6651 if (!NILP (tem))
6653 XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP] = tem;
6654 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = tem;
6655 return 1;
6657 /* At the top level in the menu bar, do likewise for commands also.
6658 The menu bar does not display equivalent key bindings anyway.
6659 ITEM_PROPERTY_DEF is already set up properly. */
6660 if (inmenubar > 0)
6661 return 1;
6663 /* This is a command. See if there is an equivalent key binding. */
6664 if (NILP (cachelist))
6666 /* We have to create a cachelist. */
6667 CHECK_IMPURE (start);
6668 XCDR (start) = Fcons (Fcons (Qnil, Qnil), XCDR (start));
6669 cachelist = XCAR (XCDR (start));
6670 newcache = 1;
6671 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
6672 if (!NILP (keyhint))
6674 XCAR (cachelist) = XCAR (keyhint);
6675 newcache = 0;
6677 else if (STRINGP (tem))
6679 XCDR (cachelist) = Fsubstitute_command_keys (tem);
6680 XCAR (cachelist) = Qt;
6683 tem = XCAR (cachelist);
6684 if (!EQ (tem, Qt))
6686 int chkcache = 0;
6687 Lisp_Object prefix;
6689 if (!NILP (tem))
6690 tem = Fkey_binding (tem, Qnil);
6692 prefix = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
6693 if (CONSP (prefix))
6695 def = XCAR (prefix);
6696 prefix = XCDR (prefix);
6698 else
6699 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6701 if (NILP (XCAR (cachelist))) /* Have no saved key. */
6703 if (newcache /* Always check first time. */
6704 /* Should we check everything when precomputing key
6705 bindings? */
6706 /* || notreal */
6707 /* If something had no key binding before, don't recheck it
6708 because that is too slow--except if we have a list of
6709 rebound commands in Vdefine_key_rebound_commands, do
6710 recheck any command that appears in that list. */
6711 || (CONSP (Vdefine_key_rebound_commands)
6712 && !NILP (Fmemq (def, Vdefine_key_rebound_commands))))
6713 chkcache = 1;
6715 /* We had a saved key. Is it still bound to the command? */
6716 else if (NILP (tem)
6717 || (!EQ (tem, def)
6718 /* If the command is an alias for another
6719 (such as lmenu.el set it up), check if the
6720 original command matches the cached command. */
6721 && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function))))
6722 chkcache = 1; /* Need to recompute key binding. */
6724 if (chkcache)
6726 /* Recompute equivalent key binding. If the command is an alias
6727 for another (such as lmenu.el set it up), see if the original
6728 command name has equivalent keys. Otherwise look up the
6729 specified command itself. We don't try both, because that
6730 makes lmenu menus slow. */
6731 if (SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
6732 && ! NILP (Fget (def, Qmenu_alias)))
6733 def = XSYMBOL (def)->function;
6734 tem = Fwhere_is_internal (def, Qnil, Qt, Qnil);
6735 XCAR (cachelist) = tem;
6736 if (NILP (tem))
6738 XCDR (cachelist) = Qnil;
6739 chkcache = 0;
6742 else if (!NILP (keyhint) && !NILP (XCAR (cachelist)))
6744 tem = XCAR (cachelist);
6745 chkcache = 1;
6748 newcache = chkcache;
6749 if (chkcache)
6751 tem = Fkey_description (tem);
6752 if (CONSP (prefix))
6754 if (STRINGP (XCAR (prefix)))
6755 tem = concat2 (XCAR (prefix), tem);
6756 if (STRINGP (XCDR (prefix)))
6757 tem = concat2 (tem, XCDR (prefix));
6759 XCDR (cachelist) = tem;
6763 tem = XCDR (cachelist);
6764 if (newcache && !NILP (tem))
6766 tem = concat3 (build_string (" ("), tem, build_string (")"));
6767 XCDR (cachelist) = tem;
6770 /* If we only want to precompute equivalent key bindings, stop here. */
6771 if (notreal)
6772 return 1;
6774 /* If we have an equivalent key binding, use that. */
6775 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ] = tem;
6777 /* Include this when menu help is implemented.
6778 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP];
6779 if (!(NILP (tem) || STRINGP (tem)))
6781 tem = menu_item_eval_property (tem);
6782 if (!STRINGP (tem))
6783 tem = Qnil;
6784 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP] = tem;
6788 /* Handle radio buttons or toggle boxes. */
6789 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
6790 if (!NILP (tem))
6791 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]
6792 = menu_item_eval_property (tem);
6794 return 1;
6799 /***********************************************************************
6800 Tool-bars
6801 ***********************************************************************/
6803 /* A vector holding tool bar items while they are parsed in function
6804 tool_bar_items runs Each item occupies TOOL_BAR_ITEM_NSCLOTS elements
6805 in the vector. */
6807 static Lisp_Object tool_bar_items_vector;
6809 /* A vector holding the result of parse_tool_bar_item. Layout is like
6810 the one for a single item in tool_bar_items_vector. */
6812 static Lisp_Object tool_bar_item_properties;
6814 /* Next free index in tool_bar_items_vector. */
6816 static int ntool_bar_items;
6818 /* The symbols `tool-bar', and `:image'. */
6820 extern Lisp_Object Qtool_bar;
6821 Lisp_Object QCimage;
6823 /* Function prototypes. */
6825 static void init_tool_bar_items P_ ((Lisp_Object));
6826 static void process_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
6827 static int parse_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
6828 static void append_tool_bar_item P_ ((void));
6831 /* Return a vector of tool bar items for keymaps currently in effect.
6832 Reuse vector REUSE if non-nil. Return in *NITEMS the number of
6833 tool bar items found. */
6835 Lisp_Object
6836 tool_bar_items (reuse, nitems)
6837 Lisp_Object reuse;
6838 int *nitems;
6840 Lisp_Object *maps;
6841 int nmaps, i;
6842 Lisp_Object oquit;
6843 Lisp_Object *tmaps;
6844 extern Lisp_Object Voverriding_local_map_menu_flag;
6845 extern Lisp_Object Voverriding_local_map;
6847 *nitems = 0;
6849 /* In order to build the menus, we need to call the keymap
6850 accessors. They all call QUIT. But this function is called
6851 during redisplay, during which a quit is fatal. So inhibit
6852 quitting while building the menus. We do this instead of
6853 specbind because (1) errors will clear it anyway and (2) this
6854 avoids risk of specpdl overflow. */
6855 oquit = Vinhibit_quit;
6856 Vinhibit_quit = Qt;
6858 /* Initialize tool_bar_items_vector and protect it from GC. */
6859 init_tool_bar_items (reuse);
6861 /* Build list of keymaps in maps. Set nmaps to the number of maps
6862 to process. */
6864 /* Should overriding-terminal-local-map and overriding-local-map apply? */
6865 if (!NILP (Voverriding_local_map_menu_flag))
6867 /* Yes, use them (if non-nil) as well as the global map. */
6868 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
6869 nmaps = 0;
6870 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6871 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6872 if (!NILP (Voverriding_local_map))
6873 maps[nmaps++] = Voverriding_local_map;
6875 else
6877 /* No, so use major and minor mode keymaps and keymap property. */
6878 int extra_maps = 2;
6879 Lisp_Object map = get_local_map (PT, current_buffer, keymap);
6880 if (!NILP (map))
6881 extra_maps = 3;
6882 nmaps = current_minor_maps (NULL, &tmaps);
6883 maps = (Lisp_Object *) alloca ((nmaps + extra_maps)
6884 * sizeof (maps[0]));
6885 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
6886 if (!NILP (map))
6887 maps[nmaps++] = map;
6888 maps[nmaps++] = get_local_map (PT, current_buffer, local_map);
6891 /* Add global keymap at the end. */
6892 maps[nmaps++] = current_global_map;
6894 /* Process maps in reverse order and look up in each map the prefix
6895 key `tool-bar'. */
6896 for (i = nmaps - 1; i >= 0; --i)
6897 if (!NILP (maps[i]))
6899 Lisp_Object keymap;
6901 keymap = get_keyelt (access_keymap (maps[i], Qtool_bar, 1, 1), 0);
6902 if (!NILP (Fkeymapp (keymap)))
6904 Lisp_Object tail;
6906 /* If KEYMAP is a symbol, its function definition is the
6907 keymap to use. */
6908 if (SYMBOLP (keymap))
6909 keymap = indirect_function (keymap);
6911 /* KEYMAP is a list `(keymap (KEY . BINDING) ...)'. */
6912 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
6914 Lisp_Object keydef = XCAR (tail);
6915 if (CONSP (keydef))
6916 process_tool_bar_item (XCAR (keydef), XCDR (keydef));
6921 Vinhibit_quit = oquit;
6922 *nitems = ntool_bar_items / TOOL_BAR_ITEM_NSLOTS;
6923 return tool_bar_items_vector;
6927 /* Process the definition of KEY which is DEF. */
6929 static void
6930 process_tool_bar_item (key, def)
6931 Lisp_Object key, def;
6933 int i;
6934 extern Lisp_Object Qundefined;
6935 struct gcpro gcpro1, gcpro2;
6937 /* Protect KEY and DEF from GC because parse_tool_bar_item may call
6938 eval. */
6939 GCPRO2 (key, def);
6941 if (EQ (def, Qundefined))
6943 /* If a map has an explicit `undefined' as definition,
6944 discard any previously made item. */
6945 for (i = 0; i < ntool_bar_items; i += TOOL_BAR_ITEM_NSLOTS)
6947 Lisp_Object *v = XVECTOR (tool_bar_items_vector)->contents + i;
6949 if (EQ (key, v[TOOL_BAR_ITEM_KEY]))
6951 if (ntool_bar_items > i + TOOL_BAR_ITEM_NSLOTS)
6952 bcopy (v + TOOL_BAR_ITEM_NSLOTS, v,
6953 ((ntool_bar_items - i - TOOL_BAR_ITEM_NSLOTS)
6954 * sizeof (Lisp_Object)));
6955 ntool_bar_items -= TOOL_BAR_ITEM_NSLOTS;
6956 break;
6960 else if (parse_tool_bar_item (key, def))
6961 /* Append a new tool bar item to tool_bar_items_vector. Accept
6962 more than one definition for the same key. */
6963 append_tool_bar_item ();
6965 UNGCPRO;
6969 /* Parse a tool bar item specification ITEM for key KEY and return the
6970 result in tool_bar_item_properties. Value is zero if ITEM is
6971 invalid.
6973 ITEM is a list `(menu-item CAPTION BINDING PROPS...)'.
6975 CAPTION is the caption of the item, If it's not a string, it is
6976 evaluated to get a string.
6978 BINDING is the tool bar item's binding. Tool-bar items with keymaps
6979 as binding are currently ignored.
6981 The following properties are recognized:
6983 - `:enable FORM'.
6985 FORM is evaluated and specifies whether the tool bar item is
6986 enabled or disabled.
6988 - `:visible FORM'
6990 FORM is evaluated and specifies whether the tool bar item is visible.
6992 - `:filter FUNCTION'
6994 FUNCTION is invoked with one parameter `(quote BINDING)'. Its
6995 result is stored as the new binding.
6997 - `:button (TYPE SELECTED)'
6999 TYPE must be one of `:radio' or `:toggle'. SELECTED is evaluated
7000 and specifies whether the button is selected (pressed) or not.
7002 - `:image IMAGES'
7004 IMAGES is either a single image specification or a vector of four
7005 image specifications. See enum tool_bar_item_images.
7007 - `:help HELP-STRING'.
7009 Gives a help string to display for the tool bar item. */
7011 static int
7012 parse_tool_bar_item (key, item)
7013 Lisp_Object key, item;
7015 /* Access slot with index IDX of vector tool_bar_item_properties. */
7016 #define PROP(IDX) XVECTOR (tool_bar_item_properties)->contents[IDX]
7018 Lisp_Object filter = Qnil;
7019 Lisp_Object caption;
7020 extern Lisp_Object QCenable, QCvisible, QChelp, QCfilter;
7021 extern Lisp_Object QCbutton, QCtoggle, QCradio;
7022 int i;
7024 /* Defininition looks like `(menu-item CAPTION BINDING PROPS...)'.
7025 Rule out items that aren't lists, don't start with
7026 `menu-item' or whose rest following `tool-bar-item' is not a
7027 list. */
7028 if (!CONSP (item)
7029 || !EQ (XCAR (item), Qmenu_item)
7030 || (item = XCDR (item),
7031 !CONSP (item)))
7032 return 0;
7034 /* Create tool_bar_item_properties vector if necessary. Reset it to
7035 defaults. */
7036 if (VECTORP (tool_bar_item_properties))
7038 for (i = 0; i < TOOL_BAR_ITEM_NSLOTS; ++i)
7039 PROP (i) = Qnil;
7041 else
7042 tool_bar_item_properties
7043 = Fmake_vector (make_number (TOOL_BAR_ITEM_NSLOTS), Qnil);
7045 /* Set defaults. */
7046 PROP (TOOL_BAR_ITEM_KEY) = key;
7047 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
7049 /* Get the caption of the item. If the caption is not a string,
7050 evaluate it to get a string. If we don't get a string, skip this
7051 item. */
7052 caption = XCAR (item);
7053 if (!STRINGP (caption))
7055 caption = menu_item_eval_property (caption);
7056 if (!STRINGP (caption))
7057 return 0;
7059 PROP (TOOL_BAR_ITEM_CAPTION) = caption;
7061 /* Give up if rest following the caption is not a list. */
7062 item = XCDR (item);
7063 if (!CONSP (item))
7064 return 0;
7066 /* Store the binding. */
7067 PROP (TOOL_BAR_ITEM_BINDING) = XCAR (item);
7068 item = XCDR (item);
7070 /* Ignore cached key binding, if any. */
7071 if (CONSP (item) && CONSP (XCAR (item)))
7072 item = XCDR (item);
7074 /* Process the rest of the properties. */
7075 for (; CONSP (item) && CONSP (XCDR (item)); item = XCDR (XCDR (item)))
7077 Lisp_Object key, value;
7079 key = XCAR (item);
7080 value = XCAR (XCDR (item));
7082 if (EQ (key, QCenable))
7083 /* `:enable FORM'. */
7084 PROP (TOOL_BAR_ITEM_ENABLED_P) = value;
7085 else if (EQ (key, QCvisible))
7087 /* `:visible FORM'. If got a visible property and that
7088 evaluates to nil then ignore this item. */
7089 if (NILP (menu_item_eval_property (value)))
7090 return 0;
7092 else if (EQ (key, QChelp))
7093 /* `:help HELP-STRING'. */
7094 PROP (TOOL_BAR_ITEM_HELP) = value;
7095 else if (EQ (key, QCfilter))
7096 /* ':filter FORM'. */
7097 filter = value;
7098 else if (EQ (key, QCbutton) && CONSP (value))
7100 /* `:button (TYPE . SELECTED)'. */
7101 Lisp_Object type, selected;
7103 type = XCAR (value);
7104 selected = XCDR (value);
7105 if (EQ (type, QCtoggle) || EQ (type, QCradio))
7107 PROP (TOOL_BAR_ITEM_SELECTED_P) = selected;
7108 PROP (TOOL_BAR_ITEM_TYPE) = type;
7111 else if (EQ (key, QCimage)
7112 && (CONSP (value)
7113 || (VECTORP (value) && XVECTOR (value)->size == 4)))
7114 /* Value is either a single image specification or a vector
7115 of 4 such specifications for the different buttion states. */
7116 PROP (TOOL_BAR_ITEM_IMAGES) = value;
7119 /* If got a filter apply it on binding. */
7120 if (!NILP (filter))
7121 PROP (TOOL_BAR_ITEM_BINDING)
7122 = menu_item_eval_property (list2 (filter,
7123 list2 (Qquote,
7124 PROP (TOOL_BAR_ITEM_BINDING))));
7126 /* See if the binding is a keymap. Give up if it is. */
7127 if (!NILP (get_keymap_1 (PROP (TOOL_BAR_ITEM_BINDING), 0, 1)))
7128 return 0;
7130 /* Enable or disable selection of item. */
7131 if (!EQ (PROP (TOOL_BAR_ITEM_ENABLED_P), Qt))
7132 PROP (TOOL_BAR_ITEM_ENABLED_P)
7133 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_ENABLED_P));
7135 /* Handle radio buttons or toggle boxes. */
7136 if (!NILP (PROP (TOOL_BAR_ITEM_SELECTED_P)))
7137 PROP (TOOL_BAR_ITEM_SELECTED_P)
7138 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_SELECTED_P));
7140 return 1;
7142 #undef PROP
7146 /* Initialize tool_bar_items_vector. REUSE, if non-nil, is a vector
7147 that can be reused. */
7149 static void
7150 init_tool_bar_items (reuse)
7151 Lisp_Object reuse;
7153 if (VECTORP (reuse))
7154 tool_bar_items_vector = reuse;
7155 else
7156 tool_bar_items_vector = Fmake_vector (make_number (64), Qnil);
7157 ntool_bar_items = 0;
7161 /* Append parsed tool bar item properties from
7162 tool_bar_item_properties */
7164 static void
7165 append_tool_bar_item ()
7167 Lisp_Object *to, *from;
7169 /* Enlarge tool_bar_items_vector if necessary. */
7170 if (ntool_bar_items + TOOL_BAR_ITEM_NSLOTS
7171 >= XVECTOR (tool_bar_items_vector)->size)
7173 Lisp_Object new_vector;
7174 int old_size = XVECTOR (tool_bar_items_vector)->size;
7176 new_vector = Fmake_vector (make_number (2 * old_size), Qnil);
7177 bcopy (XVECTOR (tool_bar_items_vector)->contents,
7178 XVECTOR (new_vector)->contents,
7179 old_size * sizeof (Lisp_Object));
7180 tool_bar_items_vector = new_vector;
7183 /* Append entries from tool_bar_item_properties to the end of
7184 tool_bar_items_vector. */
7185 to = XVECTOR (tool_bar_items_vector)->contents + ntool_bar_items;
7186 from = XVECTOR (tool_bar_item_properties)->contents;
7187 bcopy (from, to, TOOL_BAR_ITEM_NSLOTS * sizeof *to);
7188 ntool_bar_items += TOOL_BAR_ITEM_NSLOTS;
7195 /* Read a character using menus based on maps in the array MAPS.
7196 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
7197 Return t if we displayed a menu but the user rejected it.
7199 PREV_EVENT is the previous input event, or nil if we are reading
7200 the first event of a key sequence.
7202 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
7203 if we used a mouse menu to read the input, or zero otherwise. If
7204 USED_MOUSE_MENU is null, we don't dereference it.
7206 The prompting is done based on the prompt-string of the map
7207 and the strings associated with various map elements.
7209 This can be done with X menus or with menus put in the minibuf.
7210 These are done in different ways, depending on how the input will be read.
7211 Menus using X are done after auto-saving in read-char, getting the input
7212 event from Fx_popup_menu; menus using the minibuf use read_char recursively
7213 and do auto-saving in the inner call of read_char. */
7215 static Lisp_Object
7216 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
7217 int nmaps;
7218 Lisp_Object *maps;
7219 Lisp_Object prev_event;
7220 int *used_mouse_menu;
7222 int mapno;
7223 register Lisp_Object name;
7225 if (used_mouse_menu)
7226 *used_mouse_menu = 0;
7228 /* Use local over global Menu maps */
7230 if (! menu_prompting)
7231 return Qnil;
7233 /* Optionally disregard all but the global map. */
7234 if (inhibit_local_menu_bar_menus)
7236 maps += (nmaps - 1);
7237 nmaps = 1;
7240 /* Get the menu name from the first map that has one (a prompt string). */
7241 for (mapno = 0; mapno < nmaps; mapno++)
7243 name = map_prompt (maps[mapno]);
7244 if (!NILP (name))
7245 break;
7248 /* If we don't have any menus, just read a character normally. */
7249 if (mapno >= nmaps)
7250 return Qnil;
7252 #ifdef HAVE_MENUS
7253 /* If we got to this point via a mouse click,
7254 use a real menu for mouse selection. */
7255 if (EVENT_HAS_PARAMETERS (prev_event)
7256 && !EQ (XCAR (prev_event), Qmenu_bar)
7257 && !EQ (XCAR (prev_event), Qtool_bar))
7259 /* Display the menu and get the selection. */
7260 Lisp_Object *realmaps
7261 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
7262 Lisp_Object value;
7263 int nmaps1 = 0;
7265 /* Use the maps that are not nil. */
7266 for (mapno = 0; mapno < nmaps; mapno++)
7267 if (!NILP (maps[mapno]))
7268 realmaps[nmaps1++] = maps[mapno];
7270 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
7271 if (CONSP (value))
7273 Lisp_Object tem;
7275 record_menu_key (XCAR (value));
7277 /* If we got multiple events, unread all but
7278 the first.
7279 There is no way to prevent those unread events
7280 from showing up later in last_nonmenu_event.
7281 So turn symbol and integer events into lists,
7282 to indicate that they came from a mouse menu,
7283 so that when present in last_nonmenu_event
7284 they won't confuse things. */
7285 for (tem = XCDR (value); !NILP (tem);
7286 tem = XCDR (tem))
7288 record_menu_key (XCAR (tem));
7289 if (SYMBOLP (XCAR (tem))
7290 || INTEGERP (XCAR (tem)))
7291 XCAR (tem)
7292 = Fcons (XCAR (tem), Qnil);
7295 /* If we got more than one event, put all but the first
7296 onto this list to be read later.
7297 Return just the first event now. */
7298 Vunread_command_events
7299 = nconc2 (XCDR (value), Vunread_command_events);
7300 value = XCAR (value);
7302 else if (NILP (value))
7303 value = Qt;
7304 if (used_mouse_menu)
7305 *used_mouse_menu = 1;
7306 return value;
7308 #endif /* HAVE_MENUS */
7309 return Qnil ;
7312 /* Buffer in use so far for the minibuf prompts for menu keymaps.
7313 We make this bigger when necessary, and never free it. */
7314 static char *read_char_minibuf_menu_text;
7315 /* Size of that buffer. */
7316 static int read_char_minibuf_menu_width;
7318 static Lisp_Object
7319 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
7320 int commandflag ;
7321 int nmaps;
7322 Lisp_Object *maps;
7324 int mapno;
7325 register Lisp_Object name;
7326 int nlength;
7327 int width = FRAME_WIDTH (SELECTED_FRAME ()) - 4;
7328 int idx = -1;
7329 int nobindings = 1;
7330 Lisp_Object rest, vector;
7331 char *menu;
7333 vector = Qnil;
7335 if (! menu_prompting)
7336 return Qnil;
7338 /* Make sure we have a big enough buffer for the menu text. */
7339 if (read_char_minibuf_menu_text == 0)
7341 read_char_minibuf_menu_width = width + 4;
7342 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
7344 else if (width + 4 > read_char_minibuf_menu_width)
7346 read_char_minibuf_menu_width = width + 4;
7347 read_char_minibuf_menu_text
7348 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
7350 menu = read_char_minibuf_menu_text;
7352 /* Get the menu name from the first map that has one (a prompt string). */
7353 for (mapno = 0; mapno < nmaps; mapno++)
7355 name = map_prompt (maps[mapno]);
7356 if (!NILP (name))
7357 break;
7360 /* If we don't have any menus, just read a character normally. */
7361 if (mapno >= nmaps)
7362 return Qnil;
7364 /* Prompt string always starts with map's prompt, and a space. */
7365 strcpy (menu, XSTRING (name)->data);
7366 nlength = STRING_BYTES (XSTRING (name));
7367 menu[nlength++] = ':';
7368 menu[nlength++] = ' ';
7369 menu[nlength] = 0;
7371 /* Start prompting at start of first map. */
7372 mapno = 0;
7373 rest = maps[mapno];
7375 /* Present the documented bindings, a line at a time. */
7376 while (1)
7378 int notfirst = 0;
7379 int i = nlength;
7380 Lisp_Object obj;
7381 int ch;
7382 Lisp_Object orig_defn_macro;
7384 /* Loop over elements of map. */
7385 while (i < width)
7387 Lisp_Object elt;
7389 /* If reached end of map, start at beginning of next map. */
7390 if (NILP (rest))
7392 mapno++;
7393 /* At end of last map, wrap around to first map if just starting,
7394 or end this line if already have something on it. */
7395 if (mapno == nmaps)
7397 mapno = 0;
7398 if (notfirst || nobindings) break;
7400 rest = maps[mapno];
7403 /* Look at the next element of the map. */
7404 if (idx >= 0)
7405 elt = XVECTOR (vector)->contents[idx];
7406 else
7407 elt = Fcar_safe (rest);
7409 if (idx < 0 && VECTORP (elt))
7411 /* If we found a dense table in the keymap,
7412 advanced past it, but start scanning its contents. */
7413 rest = Fcdr_safe (rest);
7414 vector = elt;
7415 idx = 0;
7417 else
7419 /* An ordinary element. */
7420 Lisp_Object event, tem;
7422 if (idx < 0)
7424 event = Fcar_safe (elt); /* alist */
7425 elt = Fcdr_safe (elt);
7427 else
7429 XSETINT (event, idx); /* vector */
7432 /* Ignore the element if it has no prompt string. */
7433 if (INTEGERP (event) && parse_menu_item (elt, 0, -1))
7435 /* 1 if the char to type matches the string. */
7436 int char_matches;
7437 Lisp_Object upcased_event, downcased_event;
7438 Lisp_Object desc = Qnil;
7439 Lisp_Object s
7440 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
7442 upcased_event = Fupcase (event);
7443 downcased_event = Fdowncase (event);
7444 char_matches = (XINT (upcased_event) == XSTRING (s)->data[0]
7445 || XINT (downcased_event) == XSTRING (s)->data[0]);
7446 if (! char_matches)
7447 desc = Fsingle_key_description (event, Qnil);
7450 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
7451 if (!NILP (tem))
7452 /* Insert equivalent keybinding. */
7453 s = concat2 (s, tem);
7456 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
7457 if (EQ (tem, QCradio) || EQ (tem, QCtoggle))
7459 /* Insert button prefix. */
7460 Lisp_Object selected
7461 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
7462 if (EQ (tem, QCradio))
7463 tem = build_string (NILP (selected) ? "(*) " : "( ) ");
7464 else
7465 tem = build_string (NILP (selected) ? "[X] " : "[ ] ");
7466 s = concat2 (tem, s);
7470 /* If we have room for the prompt string, add it to this line.
7471 If this is the first on the line, always add it. */
7472 if ((XSTRING (s)->size + i + 2
7473 + (char_matches ? 0 : XSTRING (desc)->size + 3))
7474 < width
7475 || !notfirst)
7477 int thiswidth;
7479 /* Punctuate between strings. */
7480 if (notfirst)
7482 strcpy (menu + i, ", ");
7483 i += 2;
7485 notfirst = 1;
7486 nobindings = 0 ;
7488 /* If the char to type doesn't match the string's
7489 first char, explicitly show what char to type. */
7490 if (! char_matches)
7492 /* Add as much of string as fits. */
7493 thiswidth = XSTRING (desc)->size;
7494 if (thiswidth + i > width)
7495 thiswidth = width - i;
7496 bcopy (XSTRING (desc)->data, menu + i, thiswidth);
7497 i += thiswidth;
7498 strcpy (menu + i, " = ");
7499 i += 3;
7502 /* Add as much of string as fits. */
7503 thiswidth = XSTRING (s)->size;
7504 if (thiswidth + i > width)
7505 thiswidth = width - i;
7506 bcopy (XSTRING (s)->data, menu + i, thiswidth);
7507 i += thiswidth;
7508 menu[i] = 0;
7510 else
7512 /* If this element does not fit, end the line now,
7513 and save the element for the next line. */
7514 strcpy (menu + i, "...");
7515 break;
7519 /* Move past this element. */
7520 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
7521 /* Handle reaching end of dense table. */
7522 idx = -1;
7523 if (idx >= 0)
7524 idx++;
7525 else
7526 rest = Fcdr_safe (rest);
7530 /* Prompt with that and read response. */
7531 message2_nolog (menu, strlen (menu),
7532 ! NILP (current_buffer->enable_multibyte_characters));
7534 /* Make believe its not a keyboard macro in case the help char
7535 is pressed. Help characters are not recorded because menu prompting
7536 is not used on replay.
7538 orig_defn_macro = current_kboard->defining_kbd_macro;
7539 current_kboard->defining_kbd_macro = Qnil;
7541 obj = read_char (commandflag, 0, 0, Qt, 0);
7542 while (BUFFERP (obj));
7543 current_kboard->defining_kbd_macro = orig_defn_macro;
7545 if (!INTEGERP (obj))
7546 return obj;
7547 else
7548 ch = XINT (obj);
7550 if (! EQ (obj, menu_prompt_more_char)
7551 && (!INTEGERP (menu_prompt_more_char)
7552 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
7554 if (!NILP (current_kboard->defining_kbd_macro))
7555 store_kbd_macro_char (obj);
7556 return obj;
7558 /* Help char - go round again */
7562 /* Reading key sequences. */
7564 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
7565 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
7566 keymap, or nil otherwise. Return the index of the first keymap in
7567 which KEY has any binding, or NMAPS if no map has a binding.
7569 If KEY is a meta ASCII character, treat it like meta-prefix-char
7570 followed by the corresponding non-meta character. Keymaps in
7571 CURRENT with non-prefix bindings for meta-prefix-char become nil in
7572 NEXT.
7574 If KEY has no bindings in any of the CURRENT maps, NEXT is left
7575 unmodified.
7577 NEXT may be the same array as CURRENT. */
7579 static int
7580 follow_key (key, nmaps, current, defs, next)
7581 Lisp_Object key;
7582 Lisp_Object *current, *defs, *next;
7583 int nmaps;
7585 int i, first_binding;
7586 int did_meta = 0;
7588 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
7589 followed by the corresponding non-meta character.
7590 Put the results into DEFS, since we are going to alter that anyway.
7591 Do not alter CURRENT or NEXT. */
7592 if (INTEGERP (key) && (XUINT (key) & CHAR_META))
7594 for (i = 0; i < nmaps; i++)
7595 if (! NILP (current[i]))
7597 Lisp_Object def;
7598 def = get_keyelt (access_keymap (current[i],
7599 meta_prefix_char, 1, 0), 0);
7601 /* Note that since we pass the resulting bindings through
7602 get_keymap_1, non-prefix bindings for meta-prefix-char
7603 disappear. */
7604 defs[i] = get_keymap_1 (def, 0, 1);
7606 else
7607 defs[i] = Qnil;
7609 did_meta = 1;
7610 XSETINT (key, XFASTINT (key) & ~CHAR_META);
7613 first_binding = nmaps;
7614 for (i = nmaps - 1; i >= 0; i--)
7616 if (! NILP (current[i]))
7618 Lisp_Object map;
7619 if (did_meta)
7620 map = defs[i];
7621 else
7622 map = current[i];
7624 defs[i] = get_keyelt (access_keymap (map, key, 1, 0), 1);
7625 if (! NILP (defs[i]))
7626 first_binding = i;
7628 else
7629 defs[i] = Qnil;
7632 /* Given the set of bindings we've found, produce the next set of maps. */
7633 if (first_binding < nmaps)
7634 for (i = 0; i < nmaps; i++)
7635 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
7637 return first_binding;
7640 /* Read a sequence of keys that ends with a non prefix character,
7641 storing it in KEYBUF, a buffer of size BUFSIZE.
7642 Prompt with PROMPT.
7643 Return the length of the key sequence stored.
7644 Return -1 if the user rejected a command menu.
7646 Echo starting immediately unless `prompt' is 0.
7648 Where a key sequence ends depends on the currently active keymaps.
7649 These include any minor mode keymaps active in the current buffer,
7650 the current buffer's local map, and the global map.
7652 If a key sequence has no other bindings, we check Vfunction_key_map
7653 to see if some trailing subsequence might be the beginning of a
7654 function key's sequence. If so, we try to read the whole function
7655 key, and substitute its symbolic name into the key sequence.
7657 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
7658 `double-' events into similar click events, if that would make them
7659 bound. We try to turn `triple-' events first into `double-' events,
7660 then into clicks.
7662 If we get a mouse click in a mode line, vertical divider, or other
7663 non-text area, we treat the click as if it were prefixed by the
7664 symbol denoting that area - `mode-line', `vertical-line', or
7665 whatever.
7667 If the sequence starts with a mouse click, we read the key sequence
7668 with respect to the buffer clicked on, not the current buffer.
7670 If the user switches frames in the midst of a key sequence, we put
7671 off the switch-frame event until later; the next call to
7672 read_char will return it.
7674 If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
7675 from the selected window's buffer. */
7677 static int
7678 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
7679 can_return_switch_frame, fix_current_buffer)
7680 Lisp_Object *keybuf;
7681 int bufsize;
7682 Lisp_Object prompt;
7683 int dont_downcase_last;
7684 int can_return_switch_frame;
7685 int fix_current_buffer;
7687 volatile int count = specpdl_ptr - specpdl;
7689 /* How many keys there are in the current key sequence. */
7690 volatile int t;
7692 /* The length of the echo buffer when we started reading, and
7693 the length of this_command_keys when we started reading. */
7694 volatile int echo_start;
7695 volatile int keys_start;
7697 /* The number of keymaps we're scanning right now, and the number of
7698 keymaps we have allocated space for. */
7699 volatile int nmaps;
7700 volatile int nmaps_allocated = 0;
7702 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
7703 the current keymaps. */
7704 Lisp_Object *volatile defs = NULL;
7706 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
7707 in the current keymaps, or nil where it is not a prefix. */
7708 Lisp_Object *volatile submaps = NULL;
7710 /* The local map to start out with at start of key sequence. */
7711 volatile Lisp_Object orig_local_map;
7713 /* The map from the `keymap' property to start out with at start of
7714 key sequence. */
7715 volatile Lisp_Object orig_keymap;
7717 /* 1 if we have already considered switching to the local-map property
7718 of the place where a mouse click occurred. */
7719 volatile int localized_local_map = 0;
7721 /* The index in defs[] of the first keymap that has a binding for
7722 this key sequence. In other words, the lowest i such that
7723 defs[i] is non-nil. */
7724 volatile int first_binding;
7726 /* If t < mock_input, then KEYBUF[t] should be read as the next
7727 input key.
7729 We use this to recover after recognizing a function key. Once we
7730 realize that a suffix of the current key sequence is actually a
7731 function key's escape sequence, we replace the suffix with the
7732 function key's binding from Vfunction_key_map. Now keybuf
7733 contains a new and different key sequence, so the echo area,
7734 this_command_keys, and the submaps and defs arrays are wrong. In
7735 this situation, we set mock_input to t, set t to 0, and jump to
7736 restart_sequence; the loop will read keys from keybuf up until
7737 mock_input, thus rebuilding the state; and then it will resume
7738 reading characters from the keyboard. */
7739 volatile int mock_input = 0;
7741 /* If the sequence is unbound in submaps[], then
7742 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
7743 and fkey_map is its binding.
7745 These might be > t, indicating that all function key scanning
7746 should hold off until t reaches them. We do this when we've just
7747 recognized a function key, to avoid searching for the function
7748 key's again in Vfunction_key_map. */
7749 volatile int fkey_start = 0, fkey_end = 0;
7750 volatile Lisp_Object fkey_map;
7752 /* Likewise, for key_translation_map. */
7753 volatile int keytran_start = 0, keytran_end = 0;
7754 volatile Lisp_Object keytran_map;
7756 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
7757 we put it off for later. While we're reading, we keep the event here. */
7758 volatile Lisp_Object delayed_switch_frame;
7760 /* See the comment below... */
7761 #if defined (GOBBLE_FIRST_EVENT)
7762 Lisp_Object first_event;
7763 #endif
7765 volatile Lisp_Object original_uppercase;
7766 volatile int original_uppercase_position = -1;
7768 /* Gets around Microsoft compiler limitations. */
7769 int dummyflag = 0;
7771 struct buffer *starting_buffer;
7773 /* Nonzero if we seem to have got the beginning of a binding
7774 in function_key_map. */
7775 volatile int function_key_possible = 0;
7776 volatile int key_translation_possible = 0;
7778 /* Save the status of key translation before each step,
7779 so that we can restore this after downcasing. */
7780 Lisp_Object prev_fkey_map;
7781 int prev_fkey_start;
7782 int prev_fkey_end;
7784 Lisp_Object prev_keytran_map;
7785 int prev_keytran_start;
7786 int prev_keytran_end;
7788 #if defined (GOBBLE_FIRST_EVENT)
7789 int junk;
7790 #endif
7792 raw_keybuf_count = 0;
7794 last_nonmenu_event = Qnil;
7796 delayed_switch_frame = Qnil;
7797 fkey_map = Vfunction_key_map;
7798 keytran_map = Vkey_translation_map;
7800 /* If there is no function-key-map, turn off function key scanning. */
7801 if (NILP (Fkeymapp (Vfunction_key_map)))
7802 fkey_start = fkey_end = bufsize + 1;
7804 /* If there is no key-translation-map, turn off scanning. */
7805 if (NILP (Fkeymapp (Vkey_translation_map)))
7806 keytran_start = keytran_end = bufsize + 1;
7808 if (INTERACTIVE)
7810 if (!NILP (prompt))
7811 echo_prompt (XSTRING (prompt)->data);
7812 else if (cursor_in_echo_area
7813 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
7814 && NILP (Fzerop (Vecho_keystrokes)))
7815 /* This doesn't put in a dash if the echo buffer is empty, so
7816 you don't always see a dash hanging out in the minibuffer. */
7817 echo_dash ();
7820 /* Record the initial state of the echo area and this_command_keys;
7821 we will need to restore them if we replay a key sequence. */
7822 if (INTERACTIVE)
7823 echo_start = echo_length ();
7824 keys_start = this_command_key_count;
7825 this_single_command_key_start = keys_start;
7827 #if defined (GOBBLE_FIRST_EVENT)
7828 /* This doesn't quite work, because some of the things that read_char
7829 does cannot safely be bypassed. It seems too risky to try to make
7830 this work right. */
7832 /* Read the first char of the sequence specially, before setting
7833 up any keymaps, in case a filter runs and switches buffers on us. */
7834 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
7835 &junk);
7836 #endif /* GOBBLE_FIRST_EVENT */
7838 orig_local_map = get_local_map (PT, current_buffer, local_map);
7839 orig_keymap = get_local_map (PT, current_buffer, keymap);
7841 /* We jump here when the key sequence has been thoroughly changed, and
7842 we need to rescan it starting from the beginning. When we jump here,
7843 keybuf[0..mock_input] holds the sequence we should reread. */
7844 replay_sequence:
7846 starting_buffer = current_buffer;
7847 function_key_possible = 0;
7848 key_translation_possible = 0;
7850 /* Build our list of keymaps.
7851 If we recognize a function key and replace its escape sequence in
7852 keybuf with its symbol, or if the sequence starts with a mouse
7853 click and we need to switch buffers, we jump back here to rebuild
7854 the initial keymaps from the current buffer. */
7856 Lisp_Object *maps;
7858 if (!NILP (current_kboard->Voverriding_terminal_local_map)
7859 || !NILP (Voverriding_local_map))
7861 if (3 > nmaps_allocated)
7863 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
7864 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
7865 nmaps_allocated = 3;
7867 nmaps = 0;
7868 if (!NILP (current_kboard->Voverriding_terminal_local_map))
7869 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
7870 if (!NILP (Voverriding_local_map))
7871 submaps[nmaps++] = Voverriding_local_map;
7873 else
7875 int extra_maps = 2;
7876 nmaps = current_minor_maps (0, &maps);
7877 if (!NILP (orig_keymap))
7878 extra_maps = 3;
7879 if (nmaps + extra_maps > nmaps_allocated)
7881 submaps = (Lisp_Object *) alloca ((nmaps+extra_maps)
7882 * sizeof (submaps[0]));
7883 defs = (Lisp_Object *) alloca ((nmaps+extra_maps)
7884 * sizeof (defs[0]));
7885 nmaps_allocated = nmaps + extra_maps;
7887 bcopy (maps, (void *) submaps, nmaps * sizeof (submaps[0]));
7888 if (!NILP (orig_keymap))
7889 submaps[nmaps++] = orig_keymap;
7890 submaps[nmaps++] = orig_local_map;
7892 submaps[nmaps++] = current_global_map;
7895 /* Find an accurate initial value for first_binding. */
7896 for (first_binding = 0; first_binding < nmaps; first_binding++)
7897 if (! NILP (submaps[first_binding]))
7898 break;
7900 /* Start from the beginning in keybuf. */
7901 t = 0;
7903 /* These are no-ops the first time through, but if we restart, they
7904 revert the echo area and this_command_keys to their original state. */
7905 this_command_key_count = keys_start;
7906 if (INTERACTIVE && t < mock_input)
7907 echo_truncate (echo_start);
7909 /* If the best binding for the current key sequence is a keymap, or
7910 we may be looking at a function key's escape sequence, keep on
7911 reading. */
7912 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
7913 || (first_binding >= nmaps
7914 && fkey_start < t
7915 /* mock input is never part of a function key's sequence. */
7916 && mock_input <= fkey_start)
7917 || (first_binding >= nmaps
7918 && keytran_start < t && key_translation_possible)
7919 /* Don't return in the middle of a possible function key sequence,
7920 if the only bindings we found were via case conversion.
7921 Thus, if ESC O a has a function-key-map translation
7922 and ESC o has a binding, don't return after ESC O,
7923 so that we can translate ESC O plus the next character. */
7926 Lisp_Object key;
7927 int used_mouse_menu = 0;
7929 /* Where the last real key started. If we need to throw away a
7930 key that has expanded into more than one element of keybuf
7931 (say, a mouse click on the mode line which is being treated
7932 as [mode-line (mouse-...)], then we backtrack to this point
7933 of keybuf. */
7934 volatile int last_real_key_start;
7936 /* These variables are analogous to echo_start and keys_start;
7937 while those allow us to restart the entire key sequence,
7938 echo_local_start and keys_local_start allow us to throw away
7939 just one key. */
7940 volatile int echo_local_start, keys_local_start, local_first_binding;
7942 if (t >= bufsize)
7943 error ("Key sequence too long");
7945 if (INTERACTIVE)
7946 echo_local_start = echo_length ();
7947 keys_local_start = this_command_key_count;
7948 local_first_binding = first_binding;
7950 replay_key:
7951 /* These are no-ops, unless we throw away a keystroke below and
7952 jumped back up to replay_key; in that case, these restore the
7953 variables to their original state, allowing us to replay the
7954 loop. */
7955 if (INTERACTIVE && t < mock_input)
7956 echo_truncate (echo_local_start);
7957 this_command_key_count = keys_local_start;
7958 first_binding = local_first_binding;
7960 /* By default, assume each event is "real". */
7961 last_real_key_start = t;
7963 /* Does mock_input indicate that we are re-reading a key sequence? */
7964 if (t < mock_input)
7966 key = keybuf[t];
7967 add_command_key (key);
7968 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
7969 && NILP (Fzerop (Vecho_keystrokes)))
7970 echo_char (key);
7973 /* If not, we should actually read a character. */
7974 else
7977 #ifdef MULTI_KBOARD
7978 KBOARD *interrupted_kboard = current_kboard;
7979 struct frame *interrupted_frame = SELECTED_FRAME ();
7980 if (setjmp (wrong_kboard_jmpbuf))
7982 if (!NILP (delayed_switch_frame))
7984 interrupted_kboard->kbd_queue
7985 = Fcons (delayed_switch_frame,
7986 interrupted_kboard->kbd_queue);
7987 delayed_switch_frame = Qnil;
7989 while (t > 0)
7990 interrupted_kboard->kbd_queue
7991 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
7993 /* If the side queue is non-empty, ensure it begins with a
7994 switch-frame, so we'll replay it in the right context. */
7995 if (CONSP (interrupted_kboard->kbd_queue)
7996 && (key = XCAR (interrupted_kboard->kbd_queue),
7997 !(EVENT_HAS_PARAMETERS (key)
7998 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
7999 Qswitch_frame))))
8001 Lisp_Object frame;
8002 XSETFRAME (frame, interrupted_frame);
8003 interrupted_kboard->kbd_queue
8004 = Fcons (make_lispy_switch_frame (frame),
8005 interrupted_kboard->kbd_queue);
8007 mock_input = 0;
8008 orig_local_map = get_local_map (PT, current_buffer, local_map);
8009 orig_keymap = get_local_map (PT, current_buffer, keymap);
8010 goto replay_sequence;
8012 #endif
8013 key = read_char (NILP (prompt), nmaps,
8014 (Lisp_Object *) submaps, last_nonmenu_event,
8015 &used_mouse_menu);
8018 /* read_char returns t when it shows a menu and the user rejects it.
8019 Just return -1. */
8020 if (EQ (key, Qt))
8022 unbind_to (count, Qnil);
8023 return -1;
8026 /* read_char returns -1 at the end of a macro.
8027 Emacs 18 handles this by returning immediately with a
8028 zero, so that's what we'll do. */
8029 if (INTEGERP (key) && XINT (key) == -1)
8031 t = 0;
8032 /* The Microsoft C compiler can't handle the goto that
8033 would go here. */
8034 dummyflag = 1;
8035 break;
8038 /* If the current buffer has been changed from under us, the
8039 keymap may have changed, so replay the sequence. */
8040 if (BUFFERP (key))
8042 mock_input = t;
8043 /* Reset the current buffer from the selected window
8044 in case something changed the former and not the latter.
8045 This is to be more consistent with the behavior
8046 of the command_loop_1. */
8047 if (fix_current_buffer)
8049 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8050 Fkill_emacs (Qnil);
8051 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
8052 Fset_buffer (XWINDOW (selected_window)->buffer);
8055 orig_local_map = get_local_map (PT, current_buffer, local_map);
8056 orig_keymap = get_local_map (PT, current_buffer, keymap);
8057 goto replay_sequence;
8060 /* If we have a quit that was typed in another frame, and
8061 quit_throw_to_read_char switched buffers,
8062 replay to get the right keymap. */
8063 if (XINT (key) == quit_char && current_buffer != starting_buffer)
8065 GROW_RAW_KEYBUF;
8066 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8067 keybuf[t++] = key;
8068 mock_input = t;
8069 Vquit_flag = Qnil;
8070 orig_local_map = get_local_map (PT, current_buffer, local_map);
8071 orig_keymap = get_local_map (PT, current_buffer, keymap);
8072 goto replay_sequence;
8075 Vquit_flag = Qnil;
8077 if (EVENT_HAS_PARAMETERS (key)
8078 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), Qswitch_frame))
8080 /* If we're at the beginning of a key sequence, and the caller
8081 says it's okay, go ahead and return this event. If we're
8082 in the midst of a key sequence, delay it until the end. */
8083 if (t > 0 || !can_return_switch_frame)
8085 delayed_switch_frame = key;
8086 goto replay_key;
8090 GROW_RAW_KEYBUF;
8091 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8094 /* Clicks in non-text areas get prefixed by the symbol
8095 in their CHAR-ADDRESS field. For example, a click on
8096 the mode line is prefixed by the symbol `mode-line'.
8098 Furthermore, key sequences beginning with mouse clicks
8099 are read using the keymaps of the buffer clicked on, not
8100 the current buffer. So we may have to switch the buffer
8101 here.
8103 When we turn one event into two events, we must make sure
8104 that neither of the two looks like the original--so that,
8105 if we replay the events, they won't be expanded again.
8106 If not for this, such reexpansion could happen either here
8107 or when user programs play with this-command-keys. */
8108 if (EVENT_HAS_PARAMETERS (key))
8110 Lisp_Object kind;
8112 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
8113 if (EQ (kind, Qmouse_click))
8115 Lisp_Object window, posn;
8117 window = POSN_WINDOW (EVENT_START (key));
8118 posn = POSN_BUFFER_POSN (EVENT_START (key));
8120 if (CONSP (posn))
8122 /* We're looking at the second event of a
8123 sequence which we expanded before. Set
8124 last_real_key_start appropriately. */
8125 if (t > 0)
8126 last_real_key_start = t - 1;
8129 /* Key sequences beginning with mouse clicks are
8130 read using the keymaps in the buffer clicked on,
8131 not the current buffer. If we're at the
8132 beginning of a key sequence, switch buffers. */
8133 if (last_real_key_start == 0
8134 && WINDOWP (window)
8135 && BUFFERP (XWINDOW (window)->buffer)
8136 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
8138 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8139 keybuf[t] = key;
8140 mock_input = t + 1;
8142 /* Arrange to go back to the original buffer once we're
8143 done reading the key sequence. Note that we can't
8144 use save_excursion_{save,restore} here, because they
8145 save point as well as the current buffer; we don't
8146 want to save point, because redisplay may change it,
8147 to accommodate a Fset_window_start or something. We
8148 don't want to do this at the top of the function,
8149 because we may get input from a subprocess which
8150 wants to change the selected window and stuff (say,
8151 emacsclient). */
8152 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
8154 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8155 Fkill_emacs (Qnil);
8156 set_buffer_internal (XBUFFER (XWINDOW
8157 (window)->buffer)
8159 orig_local_map = get_local_map (PT, current_buffer,
8160 local_map);
8161 orig_keymap = get_local_map (PT, current_buffer, keymap);
8162 goto replay_sequence;
8165 /* For a mouse click, get the local text-property keymap
8166 of the place clicked on, rather than point. */
8167 if (last_real_key_start == 0
8168 && CONSP (XCDR (key))
8169 && ! localized_local_map)
8171 Lisp_Object map_here, start, pos;
8173 localized_local_map = 1;
8174 start = EVENT_START (key);
8176 if (CONSP (start) && CONSP (XCDR (start)))
8178 pos = POSN_BUFFER_POSN (start);
8179 if (INTEGERP (pos)
8180 && XINT (pos) >= BEG && XINT (pos) <= Z)
8182 map_here = get_local_map (XINT (pos),
8183 current_buffer, local_map);
8184 if (!EQ (map_here, orig_local_map))
8186 orig_local_map = map_here;
8187 keybuf[t] = key;
8188 mock_input = t + 1;
8190 goto replay_sequence;
8192 map_here = get_local_map (XINT (pos),
8193 current_buffer, keymap);
8194 if (!EQ (map_here, orig_keymap))
8196 orig_keymap = map_here;
8197 keybuf[t] = key;
8198 mock_input = t + 1;
8200 goto replay_sequence;
8206 /* Expand mode-line and scroll-bar events into two events:
8207 use posn as a fake prefix key. */
8208 if (SYMBOLP (posn))
8210 if (t + 1 >= bufsize)
8211 error ("Key sequence too long");
8212 keybuf[t] = posn;
8213 keybuf[t+1] = key;
8214 mock_input = t + 2;
8216 /* Zap the position in key, so we know that we've
8217 expanded it, and don't try to do so again. */
8218 POSN_BUFFER_POSN (EVENT_START (key))
8219 = Fcons (posn, Qnil);
8221 /* If on a mode line string with a local keymap,
8222 reconsider the key sequence with that keymap. */
8223 if (CONSP (POSN_STRING (EVENT_START (key))))
8225 Lisp_Object string, pos, map, map2;
8227 string = POSN_STRING (EVENT_START (key));
8228 pos = XCDR (string);
8229 string = XCAR (string);
8230 if (XINT (pos) >= 0
8231 && XINT (pos) < XSTRING (string)->size)
8233 map = Fget_text_property (pos, Qlocal_map, string);
8234 if (!NILP (map))
8235 orig_local_map = map;
8236 map2 = Fget_text_property (pos, Qkeymap, string);
8237 if (!NILP (map2))
8238 orig_keymap = map2;
8239 if (!NILP (map) || !NILP (map2))
8240 goto replay_sequence;
8244 goto replay_key;
8247 else if (CONSP (XCDR (key))
8248 && CONSP (EVENT_START (key))
8249 && CONSP (XCDR (EVENT_START (key))))
8251 Lisp_Object posn;
8253 posn = POSN_BUFFER_POSN (EVENT_START (key));
8254 /* Handle menu-bar events:
8255 insert the dummy prefix event `menu-bar'. */
8256 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
8258 if (t + 1 >= bufsize)
8259 error ("Key sequence too long");
8260 keybuf[t] = posn;
8261 keybuf[t+1] = key;
8263 /* Zap the position in key, so we know that we've
8264 expanded it, and don't try to do so again. */
8265 POSN_BUFFER_POSN (EVENT_START (key))
8266 = Fcons (posn, Qnil);
8268 mock_input = t + 2;
8269 goto replay_sequence;
8271 else if (CONSP (posn))
8273 /* We're looking at the second event of a
8274 sequence which we expanded before. Set
8275 last_real_key_start appropriately. */
8276 if (last_real_key_start == t && t > 0)
8277 last_real_key_start = t - 1;
8282 /* We have finally decided that KEY is something we might want
8283 to look up. */
8284 first_binding = (follow_key (key,
8285 nmaps - first_binding,
8286 submaps + first_binding,
8287 defs + first_binding,
8288 submaps + first_binding)
8289 + first_binding);
8291 /* If KEY wasn't bound, we'll try some fallbacks. */
8292 if (first_binding >= nmaps)
8294 Lisp_Object head;
8296 head = EVENT_HEAD (key);
8297 if (help_char_p (head) && t > 0)
8299 read_key_sequence_cmd = Vprefix_help_command;
8300 keybuf[t++] = key;
8301 last_nonmenu_event = key;
8302 /* The Microsoft C compiler can't handle the goto that
8303 would go here. */
8304 dummyflag = 1;
8305 break;
8308 if (SYMBOLP (head))
8310 Lisp_Object breakdown;
8311 int modifiers;
8313 breakdown = parse_modifiers (head);
8314 modifiers = XINT (XCAR (XCDR (breakdown)));
8315 /* Attempt to reduce an unbound mouse event to a simpler
8316 event that is bound:
8317 Drags reduce to clicks.
8318 Double-clicks reduce to clicks.
8319 Triple-clicks reduce to double-clicks, then to clicks.
8320 Down-clicks are eliminated.
8321 Double-downs reduce to downs, then are eliminated.
8322 Triple-downs reduce to double-downs, then to downs,
8323 then are eliminated. */
8324 if (modifiers & (down_modifier | drag_modifier
8325 | double_modifier | triple_modifier))
8327 while (modifiers & (down_modifier | drag_modifier
8328 | double_modifier | triple_modifier))
8330 Lisp_Object new_head, new_click;
8331 if (modifiers & triple_modifier)
8332 modifiers ^= (double_modifier | triple_modifier);
8333 else if (modifiers & double_modifier)
8334 modifiers &= ~double_modifier;
8335 else if (modifiers & drag_modifier)
8336 modifiers &= ~drag_modifier;
8337 else
8339 /* Dispose of this `down' event by simply jumping
8340 back to replay_key, to get another event.
8342 Note that if this event came from mock input,
8343 then just jumping back to replay_key will just
8344 hand it to us again. So we have to wipe out any
8345 mock input.
8347 We could delete keybuf[t] and shift everything
8348 after that to the left by one spot, but we'd also
8349 have to fix up any variable that points into
8350 keybuf, and shifting isn't really necessary
8351 anyway.
8353 Adding prefixes for non-textual mouse clicks
8354 creates two characters of mock input, and both
8355 must be thrown away. If we're only looking at
8356 the prefix now, we can just jump back to
8357 replay_key. On the other hand, if we've already
8358 processed the prefix, and now the actual click
8359 itself is giving us trouble, then we've lost the
8360 state of the keymaps we want to backtrack to, and
8361 we need to replay the whole sequence to rebuild
8364 Beyond that, only function key expansion could
8365 create more than two keys, but that should never
8366 generate mouse events, so it's okay to zero
8367 mock_input in that case too.
8369 Isn't this just the most wonderful code ever? */
8370 if (t == last_real_key_start)
8372 mock_input = 0;
8373 goto replay_key;
8375 else
8377 mock_input = last_real_key_start;
8378 goto replay_sequence;
8382 new_head
8383 = apply_modifiers (modifiers, XCAR (breakdown));
8384 new_click
8385 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
8387 /* Look for a binding for this new key. follow_key
8388 promises that it didn't munge submaps the
8389 last time we called it, since key was unbound. */
8390 first_binding
8391 = (follow_key (new_click,
8392 nmaps - local_first_binding,
8393 submaps + local_first_binding,
8394 defs + local_first_binding,
8395 submaps + local_first_binding)
8396 + local_first_binding);
8398 /* If that click is bound, go for it. */
8399 if (first_binding < nmaps)
8401 key = new_click;
8402 break;
8404 /* Otherwise, we'll leave key set to the drag event. */
8410 keybuf[t++] = key;
8411 /* Normally, last_nonmenu_event gets the previous key we read.
8412 But when a mouse popup menu is being used,
8413 we don't update last_nonmenu_event; it continues to hold the mouse
8414 event that preceded the first level of menu. */
8415 if (!used_mouse_menu)
8416 last_nonmenu_event = key;
8418 /* Record what part of this_command_keys is the current key sequence. */
8419 this_single_command_key_start = this_command_key_count - t;
8421 prev_fkey_map = fkey_map;
8422 prev_fkey_start = fkey_start;
8423 prev_fkey_end = fkey_end;
8425 prev_keytran_map = keytran_map;
8426 prev_keytran_start = keytran_start;
8427 prev_keytran_end = keytran_end;
8429 /* If the sequence is unbound, see if we can hang a function key
8430 off the end of it. We only want to scan real keyboard input
8431 for function key sequences, so if mock_input says that we're
8432 re-reading old events, don't examine it. */
8433 if (first_binding >= nmaps
8434 && t >= mock_input)
8436 Lisp_Object fkey_next;
8438 /* Continue scan from fkey_end until we find a bound suffix.
8439 If we fail, increment fkey_start
8440 and start fkey_end from there. */
8441 while (fkey_end < t)
8443 Lisp_Object key;
8445 key = keybuf[fkey_end++];
8446 /* Look up meta-characters by prefixing them
8447 with meta_prefix_char. I hate this. */
8448 if (INTEGERP (key) && XUINT (key) & meta_modifier)
8450 fkey_next
8451 = get_keymap_1
8452 (get_keyelt
8453 (access_keymap (fkey_map, meta_prefix_char, 1, 0), 0),
8454 0, 1);
8455 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
8457 else
8458 fkey_next = fkey_map;
8460 fkey_next
8461 = get_keyelt (access_keymap (fkey_next, key, 1, 0), 1);
8463 /* Handle symbol with autoload definition. */
8464 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8465 && CONSP (XSYMBOL (fkey_next)->function)
8466 && EQ (XCAR (XSYMBOL (fkey_next)->function), Qautoload))
8467 do_autoload (XSYMBOL (fkey_next)->function,
8468 fkey_next);
8470 /* Handle a symbol whose function definition is a keymap
8471 or an array. */
8472 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8473 && (!NILP (Farrayp (XSYMBOL (fkey_next)->function))
8474 || !NILP (Fkeymapp (XSYMBOL (fkey_next)->function))))
8475 fkey_next = XSYMBOL (fkey_next)->function;
8477 #if 0 /* I didn't turn this on, because it might cause trouble
8478 for the mapping of return into C-m and tab into C-i. */
8479 /* Optionally don't map function keys into other things.
8480 This enables the user to redefine kp- keys easily. */
8481 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
8482 fkey_next = Qnil;
8483 #endif
8485 /* If the function key map gives a function, not an
8486 array, then call the function with no args and use
8487 its value instead. */
8488 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8489 && fkey_end == t)
8491 struct gcpro gcpro1, gcpro2, gcpro3;
8492 Lisp_Object tem;
8493 tem = fkey_next;
8495 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
8496 fkey_next = call1 (fkey_next, prompt);
8497 UNGCPRO;
8498 /* If the function returned something invalid,
8499 barf--don't ignore it.
8500 (To ignore it safely, we would need to gcpro a bunch of
8501 other variables.) */
8502 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
8503 error ("Function in key-translation-map returns invalid key sequence");
8506 function_key_possible = ! NILP (fkey_next);
8508 /* If keybuf[fkey_start..fkey_end] is bound in the
8509 function key map and it's a suffix of the current
8510 sequence (i.e. fkey_end == t), replace it with
8511 the binding and restart with fkey_start at the end. */
8512 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
8513 && fkey_end == t)
8515 int len = XFASTINT (Flength (fkey_next));
8517 t = fkey_start + len;
8518 if (t >= bufsize)
8519 error ("Key sequence too long");
8521 if (VECTORP (fkey_next))
8522 bcopy (XVECTOR (fkey_next)->contents,
8523 keybuf + fkey_start,
8524 (t - fkey_start) * sizeof (keybuf[0]));
8525 else if (STRINGP (fkey_next))
8527 int i;
8529 for (i = 0; i < len; i++)
8530 XSETFASTINT (keybuf[fkey_start + i],
8531 XSTRING (fkey_next)->data[i]);
8534 mock_input = t;
8535 fkey_start = fkey_end = t;
8536 fkey_map = Vfunction_key_map;
8538 /* Do pass the results through key-translation-map.
8539 But don't retranslate what key-translation-map
8540 has already translated. */
8541 keytran_end = keytran_start;
8542 keytran_map = Vkey_translation_map;
8544 goto replay_sequence;
8547 fkey_map = get_keymap_1 (fkey_next, 0, 1);
8549 /* If we no longer have a bound suffix, try a new positions for
8550 fkey_start. */
8551 if (NILP (fkey_map))
8553 fkey_end = ++fkey_start;
8554 fkey_map = Vfunction_key_map;
8555 function_key_possible = 0;
8560 /* Look for this sequence in key-translation-map. */
8562 Lisp_Object keytran_next;
8564 /* Scan from keytran_end until we find a bound suffix. */
8565 while (keytran_end < t)
8567 Lisp_Object key;
8569 key = keybuf[keytran_end++];
8570 /* Look up meta-characters by prefixing them
8571 with meta_prefix_char. I hate this. */
8572 if (INTEGERP (key) && XUINT (key) & meta_modifier)
8574 keytran_next
8575 = get_keymap_1
8576 (get_keyelt
8577 (access_keymap (keytran_map, meta_prefix_char, 1, 0), 0),
8578 0, 1);
8579 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
8581 else
8582 keytran_next = keytran_map;
8584 keytran_next
8585 = get_keyelt (access_keymap (keytran_next, key, 1, 0), 1);
8587 /* Handle symbol with autoload definition. */
8588 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8589 && CONSP (XSYMBOL (keytran_next)->function)
8590 && EQ (XCAR (XSYMBOL (keytran_next)->function), Qautoload))
8591 do_autoload (XSYMBOL (keytran_next)->function,
8592 keytran_next);
8594 /* Handle a symbol whose function definition is a keymap
8595 or an array. */
8596 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8597 && (!NILP (Farrayp (XSYMBOL (keytran_next)->function))
8598 || !NILP (Fkeymapp (XSYMBOL (keytran_next)->function))))
8599 keytran_next = XSYMBOL (keytran_next)->function;
8601 /* If the key translation map gives a function, not an
8602 array, then call the function with one arg and use
8603 its value instead. */
8604 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8605 && keytran_end == t)
8607 struct gcpro gcpro1, gcpro2, gcpro3;
8608 Lisp_Object tem;
8609 tem = keytran_next;
8611 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
8612 keytran_next = call1 (keytran_next, prompt);
8613 UNGCPRO;
8614 /* If the function returned something invalid,
8615 barf--don't ignore it.
8616 (To ignore it safely, we would need to gcpro a bunch of
8617 other variables.) */
8618 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
8619 error ("Function in key-translation-map returns invalid key sequence");
8622 key_translation_possible = ! NILP (keytran_next);
8624 /* If keybuf[keytran_start..keytran_end] is bound in the
8625 key translation map and it's a suffix of the current
8626 sequence (i.e. keytran_end == t), replace it with
8627 the binding and restart with keytran_start at the end. */
8628 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
8629 && keytran_end == t)
8631 int len = XFASTINT (Flength (keytran_next));
8633 t = keytran_start + len;
8634 if (t >= bufsize)
8635 error ("Key sequence too long");
8637 if (VECTORP (keytran_next))
8638 bcopy (XVECTOR (keytran_next)->contents,
8639 keybuf + keytran_start,
8640 (t - keytran_start) * sizeof (keybuf[0]));
8641 else if (STRINGP (keytran_next))
8643 int i;
8645 for (i = 0; i < len; i++)
8646 XSETFASTINT (keybuf[keytran_start + i],
8647 XSTRING (keytran_next)->data[i]);
8650 mock_input = t;
8651 keytran_start = keytran_end = t;
8652 keytran_map = Vkey_translation_map;
8654 /* Don't pass the results of key-translation-map
8655 through function-key-map. */
8656 fkey_start = fkey_end = t;
8657 fkey_map = Vfunction_key_map;
8659 goto replay_sequence;
8662 keytran_map = get_keymap_1 (keytran_next, 0, 1);
8664 /* If we no longer have a bound suffix, try a new positions for
8665 keytran_start. */
8666 if (NILP (keytran_map))
8668 keytran_end = ++keytran_start;
8669 keytran_map = Vkey_translation_map;
8670 key_translation_possible = 0;
8675 /* If KEY is not defined in any of the keymaps,
8676 and cannot be part of a function key or translation,
8677 and is an upper case letter
8678 use the corresponding lower-case letter instead. */
8679 if (first_binding == nmaps && ! function_key_possible
8680 && ! key_translation_possible
8681 && INTEGERP (key)
8682 && ((((XINT (key) & 0x3ffff)
8683 < XCHAR_TABLE (current_buffer->downcase_table)->size)
8684 && UPPERCASEP (XINT (key) & 0x3ffff))
8685 || (XINT (key) & shift_modifier)))
8687 Lisp_Object new_key;
8689 original_uppercase = key;
8690 original_uppercase_position = t - 1;
8692 if (XINT (key) & shift_modifier)
8693 XSETINT (new_key, XINT (key) & ~shift_modifier);
8694 else
8695 XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
8696 | (XINT (key) & ~0x3ffff)));
8698 /* We have to do this unconditionally, regardless of whether
8699 the lower-case char is defined in the keymaps, because they
8700 might get translated through function-key-map. */
8701 keybuf[t - 1] = new_key;
8702 mock_input = t;
8704 fkey_map = prev_fkey_map;
8705 fkey_start = prev_fkey_start;
8706 fkey_end = prev_fkey_end;
8708 keytran_map = prev_keytran_map;
8709 keytran_start = prev_keytran_start;
8710 keytran_end = prev_keytran_end;
8712 goto replay_sequence;
8714 /* If KEY is not defined in any of the keymaps,
8715 and cannot be part of a function key or translation,
8716 and is a shifted function key,
8717 use the corresponding unshifted function key instead. */
8718 if (first_binding == nmaps && ! function_key_possible
8719 && ! key_translation_possible
8720 && SYMBOLP (key))
8722 Lisp_Object breakdown;
8723 int modifiers;
8725 breakdown = parse_modifiers (key);
8726 modifiers = XINT (XCAR (XCDR (breakdown)));
8727 if (modifiers & shift_modifier)
8729 Lisp_Object new_key;
8731 original_uppercase = key;
8732 original_uppercase_position = t - 1;
8734 modifiers &= ~shift_modifier;
8735 new_key = apply_modifiers (modifiers,
8736 XCAR (breakdown));
8738 keybuf[t - 1] = new_key;
8739 mock_input = t;
8741 fkey_map = prev_fkey_map;
8742 fkey_start = prev_fkey_start;
8743 fkey_end = prev_fkey_end;
8745 keytran_map = prev_keytran_map;
8746 keytran_start = prev_keytran_start;
8747 keytran_end = prev_keytran_end;
8749 goto replay_sequence;
8754 if (!dummyflag)
8755 read_key_sequence_cmd = (first_binding < nmaps
8756 ? defs[first_binding]
8757 : Qnil);
8759 unread_switch_frame = delayed_switch_frame;
8760 unbind_to (count, Qnil);
8762 /* Don't downcase the last character if the caller says don't.
8763 Don't downcase it if the result is undefined, either. */
8764 if ((dont_downcase_last || first_binding >= nmaps)
8765 && t - 1 == original_uppercase_position)
8766 keybuf[t - 1] = original_uppercase;
8768 /* Occasionally we fabricate events, perhaps by expanding something
8769 according to function-key-map, or by adding a prefix symbol to a
8770 mouse click in the scroll bar or modeline. In this cases, return
8771 the entire generated key sequence, even if we hit an unbound
8772 prefix or a definition before the end. This means that you will
8773 be able to push back the event properly, and also means that
8774 read-key-sequence will always return a logical unit.
8776 Better ideas? */
8777 for (; t < mock_input; t++)
8779 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
8780 && NILP (Fzerop (Vecho_keystrokes)))
8781 echo_char (keybuf[t]);
8782 add_command_key (keybuf[t]);
8787 return t;
8790 #if 0 /* This doc string is too long for some compilers.
8791 This commented-out definition serves for DOC. */
8792 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
8793 "Read a sequence of keystrokes and return as a string or vector.\n\
8794 The sequence is sufficient to specify a non-prefix command in the\n\
8795 current local and global maps.\n\
8797 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
8798 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
8799 as a continuation of the previous key.\n\
8801 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
8802 convert the last event to lower case. (Normally any upper case event\n\
8803 is converted to lower case if the original event is undefined and the lower\n\
8804 case equivalent is defined.) A non-nil value is appropriate for reading\n\
8805 a key sequence to be defined.\n\
8807 A C-g typed while in this function is treated like any other character,\n\
8808 and `quit-flag' is not set.\n\
8810 If the key sequence starts with a mouse click, then the sequence is read\n\
8811 using the keymaps of the buffer of the window clicked in, not the buffer\n\
8812 of the selected window as normal.\n\
8813 ""\n\
8814 `read-key-sequence' drops unbound button-down events, since you normally\n\
8815 only care about the click or drag events which follow them. If a drag\n\
8816 or multi-click event is unbound, but the corresponding click event would\n\
8817 be bound, `read-key-sequence' turns the event into a click event at the\n\
8818 drag's starting position. This means that you don't have to distinguish\n\
8819 between click and drag, double, or triple events unless you want to.\n\
8821 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
8822 lines separating windows, and scroll bars with imaginary keys\n\
8823 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
8825 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
8826 function will process a switch-frame event if the user switches frames\n\
8827 before typing anything. If the user switches frames in the middle of a\n\
8828 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
8829 is nil, then the event will be put off until after the current key sequence.\n\
8831 `read-key-sequence' checks `function-key-map' for function key\n\
8832 sequences, where they wouldn't conflict with ordinary bindings. See\n\
8833 `function-key-map' for more details.\n\
8835 The optional fifth argument COMMAND-LOOP, if non-nil, means\n\
8836 that this key sequence is being read by something that will\n\
8837 read commands one after another. It should be nil if the caller\n\
8838 will read just one key sequence.")
8839 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame, command-loop)
8840 #endif
8842 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 5, 0,
8844 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
8845 command_loop)
8846 Lisp_Object prompt, continue_echo, dont_downcase_last;
8847 Lisp_Object can_return_switch_frame, command_loop;
8849 Lisp_Object keybuf[30];
8850 register int i;
8851 struct gcpro gcpro1;
8852 int count = specpdl_ptr - specpdl;
8854 if (!NILP (prompt))
8855 CHECK_STRING (prompt, 0);
8856 QUIT;
8858 specbind (Qinput_method_exit_on_first_char,
8859 (NILP (command_loop) ? Qt : Qnil));
8860 specbind (Qinput_method_use_echo_area,
8861 (NILP (command_loop) ? Qt : Qnil));
8863 bzero (keybuf, sizeof keybuf);
8864 GCPRO1 (keybuf[0]);
8865 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
8867 if (NILP (continue_echo))
8869 this_command_key_count = 0;
8870 this_single_command_key_start = 0;
8873 #ifdef HAVE_X_WINDOWS
8874 if (display_busy_cursor_p)
8875 cancel_busy_cursor ();
8876 #endif
8878 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
8879 prompt, ! NILP (dont_downcase_last),
8880 ! NILP (can_return_switch_frame), 0);
8882 #ifdef HAVE_X_WINDOWS
8883 if (display_busy_cursor_p)
8884 start_busy_cursor ();
8885 #endif
8887 if (i == -1)
8889 Vquit_flag = Qt;
8890 QUIT;
8892 UNGCPRO;
8893 return unbind_to (count, make_event_array (i, keybuf));
8896 DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
8897 Sread_key_sequence_vector, 1, 5, 0,
8898 "Like `read-key-sequence' but always return a vector.")
8899 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
8900 command_loop)
8901 Lisp_Object prompt, continue_echo, dont_downcase_last;
8902 Lisp_Object can_return_switch_frame, command_loop;
8904 Lisp_Object keybuf[30];
8905 register int i;
8906 struct gcpro gcpro1;
8907 int count = specpdl_ptr - specpdl;
8909 if (!NILP (prompt))
8910 CHECK_STRING (prompt, 0);
8911 QUIT;
8913 specbind (Qinput_method_exit_on_first_char,
8914 (NILP (command_loop) ? Qt : Qnil));
8915 specbind (Qinput_method_use_echo_area,
8916 (NILP (command_loop) ? Qt : Qnil));
8918 bzero (keybuf, sizeof keybuf);
8919 GCPRO1 (keybuf[0]);
8920 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
8922 if (NILP (continue_echo))
8924 this_command_key_count = 0;
8925 this_single_command_key_start = 0;
8928 #ifdef HAVE_X_WINDOWS
8929 if (display_busy_cursor_p)
8930 cancel_busy_cursor ();
8931 #endif
8933 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
8934 prompt, ! NILP (dont_downcase_last),
8935 ! NILP (can_return_switch_frame), 0);
8937 #ifdef HAVE_X_WINDOWS
8938 if (display_busy_cursor_p)
8939 start_busy_cursor ();
8940 #endif
8942 if (i == -1)
8944 Vquit_flag = Qt;
8945 QUIT;
8947 UNGCPRO;
8948 return unbind_to (count, Fvector (i, keybuf));
8951 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
8952 "Execute CMD as an editor command.\n\
8953 CMD must be a symbol that satisfies the `commandp' predicate.\n\
8954 Optional second arg RECORD-FLAG non-nil\n\
8955 means unconditionally put this command in `command-history'.\n\
8956 Otherwise, that is done only if an arg is read using the minibuffer.\n\
8957 The argument KEYS specifies the value to use instead of (this-command-keys)\n\
8958 when reading the arguments; if it is nil, (this-command-keys) is used.\n\
8959 The argument SPECIAL, if non-nil, means that this command is executing\n\
8960 a special event, so ignore the prefix argument and don't clear it.")
8961 (cmd, record_flag, keys, special)
8962 Lisp_Object cmd, record_flag, keys, special;
8964 register Lisp_Object final;
8965 register Lisp_Object tem;
8966 Lisp_Object prefixarg;
8967 struct backtrace backtrace;
8968 extern int debug_on_next_call;
8970 debug_on_next_call = 0;
8972 if (NILP (special))
8974 prefixarg = current_kboard->Vprefix_arg;
8975 Vcurrent_prefix_arg = prefixarg;
8976 current_kboard->Vprefix_arg = Qnil;
8978 else
8979 prefixarg = Qnil;
8981 if (SYMBOLP (cmd))
8983 tem = Fget (cmd, Qdisabled);
8984 if (!NILP (tem) && !NILP (Vrun_hooks))
8986 tem = Fsymbol_value (Qdisabled_command_hook);
8987 if (!NILP (tem))
8988 return call1 (Vrun_hooks, Qdisabled_command_hook);
8992 while (1)
8994 final = Findirect_function (cmd);
8996 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
8998 struct gcpro gcpro1, gcpro2;
9000 GCPRO2 (cmd, prefixarg);
9001 do_autoload (final, cmd);
9002 UNGCPRO;
9004 else
9005 break;
9008 if (STRINGP (final) || VECTORP (final))
9010 /* If requested, place the macro in the command history. For
9011 other sorts of commands, call-interactively takes care of
9012 this. */
9013 if (!NILP (record_flag))
9015 Vcommand_history
9016 = Fcons (Fcons (Qexecute_kbd_macro,
9017 Fcons (final, Fcons (prefixarg, Qnil))),
9018 Vcommand_history);
9020 /* Don't keep command history around forever. */
9021 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
9023 tem = Fnthcdr (Vhistory_length, Vcommand_history);
9024 if (CONSP (tem))
9025 XCDR (tem) = Qnil;
9029 return Fexecute_kbd_macro (final, prefixarg);
9032 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
9034 backtrace.next = backtrace_list;
9035 backtrace_list = &backtrace;
9036 backtrace.function = &Qcall_interactively;
9037 backtrace.args = &cmd;
9038 backtrace.nargs = 1;
9039 backtrace.evalargs = 0;
9041 tem = Fcall_interactively (cmd, record_flag, keys);
9043 backtrace_list = backtrace.next;
9044 return tem;
9046 return Qnil;
9049 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
9050 1, 1, "P",
9051 "Read function name, then read its arguments and call it.")
9052 (prefixarg)
9053 Lisp_Object prefixarg;
9055 Lisp_Object function;
9056 char buf[40];
9057 Lisp_Object saved_keys;
9058 Lisp_Object bindings, value;
9059 struct gcpro gcpro1, gcpro2;
9061 saved_keys = Fvector (this_command_key_count,
9062 XVECTOR (this_command_keys)->contents);
9063 buf[0] = 0;
9064 GCPRO2 (saved_keys, prefixarg);
9066 if (EQ (prefixarg, Qminus))
9067 strcpy (buf, "- ");
9068 else if (CONSP (prefixarg) && XINT (XCAR (prefixarg)) == 4)
9069 strcpy (buf, "C-u ");
9070 else if (CONSP (prefixarg) && INTEGERP (XCAR (prefixarg)))
9072 if (sizeof (int) == sizeof (EMACS_INT))
9073 sprintf (buf, "%d ", XINT (XCAR (prefixarg)));
9074 else if (sizeof (long) == sizeof (EMACS_INT))
9075 sprintf (buf, "%ld ", (long) XINT (XCAR (prefixarg)));
9076 else
9077 abort ();
9079 else if (INTEGERP (prefixarg))
9081 if (sizeof (int) == sizeof (EMACS_INT))
9082 sprintf (buf, "%d ", XINT (prefixarg));
9083 else if (sizeof (long) == sizeof (EMACS_INT))
9084 sprintf (buf, "%ld ", (long) XINT (prefixarg));
9085 else
9086 abort ();
9089 /* This isn't strictly correct if execute-extended-command
9090 is bound to anything else. Perhaps it should use
9091 this_command_keys? */
9092 strcat (buf, "M-x ");
9094 /* Prompt with buf, and then read a string, completing from and
9095 restricting to the set of all defined commands. Don't provide
9096 any initial input. Save the command read on the extended-command
9097 history list. */
9098 function = Fcompleting_read (build_string (buf),
9099 Vobarray, Qcommandp,
9100 Qt, Qnil, Qextended_command_history, Qnil,
9101 Qnil);
9103 if (STRINGP (function) && XSTRING (function)->size == 0)
9104 error ("No command name given");
9106 /* Set this_command_keys to the concatenation of saved_keys and
9107 function, followed by a RET. */
9109 struct Lisp_String *str;
9110 Lisp_Object *keys;
9111 int i;
9113 this_command_key_count = 0;
9114 this_single_command_key_start = 0;
9116 keys = XVECTOR (saved_keys)->contents;
9117 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
9118 add_command_key (keys[i]);
9120 str = XSTRING (function);
9121 for (i = 0; i < str->size; i++)
9122 add_command_key (Faref (function, make_number (i)));
9124 add_command_key (make_number ('\015'));
9127 UNGCPRO;
9129 function = Fintern (function, Qnil);
9130 current_kboard->Vprefix_arg = prefixarg;
9131 Vthis_command = function;
9132 real_this_command = function;
9134 /* If enabled, show which key runs this command. */
9135 if (!NILP (Vsuggest_key_bindings)
9136 && NILP (Vexecuting_macro)
9137 && SYMBOLP (function))
9138 bindings = Fwhere_is_internal (function, Voverriding_local_map,
9139 Qt, Qnil);
9140 else
9141 bindings = Qnil;
9143 value = Qnil;
9144 GCPRO2 (bindings, value);
9145 value = Fcommand_execute (function, Qt, Qnil, Qnil);
9147 /* If the command has a key binding, print it now. */
9148 if (!NILP (bindings)
9149 && ! (VECTORP (bindings) && EQ (Faref (bindings, make_number (0)),
9150 Qmouse_movement)))
9152 /* But first wait, and skip the message if there is input. */
9153 int delay_time;
9154 if (!NILP (echo_area_buffer[0]))
9155 /* This command displayed something in the echo area;
9156 so wait a few seconds, then display our suggestion message. */
9157 delay_time = (NUMBERP (Vsuggest_key_bindings)
9158 ? XINT (Vsuggest_key_bindings) : 2);
9159 else
9160 /* This command left the echo area empty,
9161 so display our message immediately. */
9162 delay_time = 0;
9164 if (!NILP (Fsit_for (make_number (delay_time), Qnil, Qnil))
9165 && ! CONSP (Vunread_command_events))
9167 Lisp_Object binding;
9168 char *newmessage;
9169 int message_p = push_message ();
9171 binding = Fkey_description (bindings);
9173 newmessage
9174 = (char *) alloca (XSYMBOL (function)->name->size
9175 + STRING_BYTES (XSTRING (binding))
9176 + 100);
9177 sprintf (newmessage, "You can run the command `%s' with %s",
9178 XSYMBOL (function)->name->data,
9179 XSTRING (binding)->data);
9180 message2_nolog (newmessage,
9181 strlen (newmessage),
9182 STRING_MULTIBYTE (binding));
9183 if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
9184 ? Vsuggest_key_bindings : make_number (2)),
9185 Qnil, Qnil))
9186 && message_p)
9187 restore_message ();
9189 pop_message ();
9193 RETURN_UNGCPRO (value);
9196 /* Find the set of keymaps now active.
9197 Store into *MAPS_P a vector holding the various maps
9198 and return the number of them. The vector was malloc'd
9199 and the caller should free it. */
9202 current_active_maps (maps_p)
9203 Lisp_Object **maps_p;
9205 Lisp_Object *tmaps, *maps;
9206 int nmaps;
9208 /* Should overriding-terminal-local-map and overriding-local-map apply? */
9209 if (!NILP (Voverriding_local_map_menu_flag))
9211 /* Yes, use them (if non-nil) as well as the global map. */
9212 maps = (Lisp_Object *) xmalloc (3 * sizeof (maps[0]));
9213 nmaps = 0;
9214 if (!NILP (current_kboard->Voverriding_terminal_local_map))
9215 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
9216 if (!NILP (Voverriding_local_map))
9217 maps[nmaps++] = Voverriding_local_map;
9219 else
9221 /* No, so use major and minor mode keymaps and keymap property. */
9222 int extra_maps = 2;
9223 Lisp_Object map = get_local_map (PT, current_buffer, keymap);
9224 if (!NILP (map))
9225 extra_maps = 3;
9226 nmaps = current_minor_maps (NULL, &tmaps);
9227 maps = (Lisp_Object *) alloca ((nmaps + extra_maps)
9228 * sizeof (maps[0]));
9229 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
9230 if (!NILP (map))
9231 maps[nmaps++] = map;
9232 maps[nmaps++] = get_local_map (PT, current_buffer, local_map);
9234 maps[nmaps++] = current_global_map;
9236 *maps_p = maps;
9237 return nmaps;
9240 /* Return nonzero if input events are pending. */
9243 detect_input_pending ()
9245 if (!input_pending)
9246 get_input_pending (&input_pending, 0);
9248 return input_pending;
9251 /* Return nonzero if input events are pending, and run any pending timers. */
9254 detect_input_pending_run_timers (do_display)
9255 int do_display;
9257 int old_timers_run = timers_run;
9259 if (!input_pending)
9260 get_input_pending (&input_pending, 1);
9262 if (old_timers_run != timers_run && do_display)
9264 redisplay_preserve_echo_area ();
9265 /* The following fixes a bug when using lazy-lock with
9266 lazy-lock-defer-on-the-fly set to t, i.e. when fontifying
9267 from an idle timer function. The symptom of the bug is that
9268 the cursor sometimes doesn't become visible until the next X
9269 event is processed. --gerd. */
9270 if (rif)
9271 rif->flush_display (NULL);
9274 return input_pending;
9277 /* This is called in some cases before a possible quit.
9278 It cases the next call to detect_input_pending to recompute input_pending.
9279 So calling this function unnecessarily can't do any harm. */
9281 void
9282 clear_input_pending ()
9284 input_pending = 0;
9287 /* Return nonzero if there are pending requeued events.
9288 This isn't used yet. The hope is to make wait_reading_process_input
9289 call it, and return return if it runs Lisp code that unreads something.
9290 The problem is, kbd_buffer_get_event needs to be fixed to know what
9291 to do in that case. It isn't trivial. */
9294 requeued_events_pending_p ()
9296 return (!NILP (Vunread_command_events) || unread_command_char != -1);
9300 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
9301 "T if command input is currently available with no waiting.\n\
9302 Actually, the value is nil only if we can be sure that no input is available.")
9305 if (!NILP (Vunread_command_events) || unread_command_char != -1)
9306 return (Qt);
9308 get_input_pending (&input_pending, 1);
9309 return input_pending > 0 ? Qt : Qnil;
9312 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
9313 "Return vector of last 100 events, not counting those from keyboard macros.")
9316 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
9317 Lisp_Object val;
9319 if (total_keys < NUM_RECENT_KEYS)
9320 return Fvector (total_keys, keys);
9321 else
9323 val = Fvector (NUM_RECENT_KEYS, keys);
9324 bcopy (keys + recent_keys_index,
9325 XVECTOR (val)->contents,
9326 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
9327 bcopy (keys,
9328 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
9329 recent_keys_index * sizeof (Lisp_Object));
9330 return val;
9334 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
9335 "Return the key sequence that invoked this command.\n\
9336 The value is a string or a vector.")
9339 return make_event_array (this_command_key_count,
9340 XVECTOR (this_command_keys)->contents);
9343 DEFUN ("this-command-keys-vector", Fthis_command_keys_vector, Sthis_command_keys_vector, 0, 0, 0,
9344 "Return the key sequence that invoked this command, as a vector.")
9347 return Fvector (this_command_key_count,
9348 XVECTOR (this_command_keys)->contents);
9351 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
9352 Sthis_single_command_keys, 0, 0, 0,
9353 "Return the key sequence that invoked this command.\n\
9354 Unlike `this-command-keys', this function's value\n\
9355 does not include prefix arguments.\n\
9356 The value is always a vector.")
9359 return Fvector (this_command_key_count
9360 - this_single_command_key_start,
9361 (XVECTOR (this_command_keys)->contents
9362 + this_single_command_key_start));
9365 DEFUN ("this-single-command-raw-keys", Fthis_single_command_raw_keys,
9366 Sthis_single_command_raw_keys, 0, 0, 0,
9367 "Return the raw events that were read for this command.\n\
9368 Unlike `this-single-command-keys', this function's value\n\
9369 shows the events before all translations (except for input methods).\n\
9370 The value is always a vector.")
9373 return Fvector (raw_keybuf_count,
9374 (XVECTOR (raw_keybuf)->contents));
9377 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
9378 Sreset_this_command_lengths, 0, 0, 0,
9379 "Used for complicated reasons in `universal-argument-other-key'.\n\
9381 `universal-argument-other-key' rereads the event just typed.\n\
9382 It then gets translated through `function-key-map'.\n\
9383 The translated event gets included in the echo area and in\n\
9384 the value of `this-command-keys' in addition to the raw original event.\n\
9385 That is not right.\n\
9387 Calling this function directs the translated event to replace\n\
9388 the original event, so that only one version of the event actually\n\
9389 appears in the echo area and in the value of `this-command-keys.'.")
9392 before_command_restore_flag = 1;
9393 before_command_key_count_1 = before_command_key_count;
9394 before_command_echo_length_1 = before_command_echo_length;
9395 return Qnil;
9398 DEFUN ("clear-this-command-keys", Fclear_this_command_keys,
9399 Sclear_this_command_keys, 0, 0, 0,
9400 "Clear out the vector that `this-command-keys' returns.\n\
9401 Clear vector containing last 100 events.")
9404 int i;
9406 this_command_key_count = 0;
9408 for (i = 0; i < XVECTOR (recent_keys)->size; ++i)
9409 XVECTOR (recent_keys)->contents[i] = Qnil;
9410 total_keys = 0;
9411 recent_keys_index = 0;
9412 return Qnil;
9415 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
9416 "Return the current depth in recursive edits.")
9419 Lisp_Object temp;
9420 XSETFASTINT (temp, command_loop_level + minibuf_level);
9421 return temp;
9424 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
9425 "FOpen dribble file: ",
9426 "Start writing all keyboard characters to a dribble file called FILE.\n\
9427 If FILE is nil, close any open dribble file.")
9428 (file)
9429 Lisp_Object file;
9431 if (dribble)
9433 fclose (dribble);
9434 dribble = 0;
9436 if (!NILP (file))
9438 file = Fexpand_file_name (file, Qnil);
9439 dribble = fopen (XSTRING (file)->data, "w");
9440 if (dribble == 0)
9441 report_file_error ("Opening dribble", Fcons (file, Qnil));
9443 return Qnil;
9446 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
9447 "Discard the contents of the terminal input buffer.\n\
9448 Also cancel any kbd macro being defined.")
9451 current_kboard->defining_kbd_macro = Qnil;
9452 update_mode_lines++;
9454 Vunread_command_events = Qnil;
9455 unread_command_char = -1;
9457 discard_tty_input ();
9459 kbd_fetch_ptr = kbd_store_ptr;
9460 Ffillarray (kbd_buffer_gcpro, Qnil);
9461 input_pending = 0;
9463 return Qnil;
9466 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
9467 "Stop Emacs and return to superior process. You can resume later.\n\
9468 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
9469 control, run a subshell instead.\n\n\
9470 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
9471 to be read as terminal input by Emacs's parent, after suspension.\n\
9473 Before suspending, run the normal hook `suspend-hook'.\n\
9474 After resumption run the normal hook `suspend-resume-hook'.\n\
9476 Some operating systems cannot stop the Emacs process and resume it later.\n\
9477 On such systems, Emacs starts a subshell instead of suspending.")
9478 (stuffstring)
9479 Lisp_Object stuffstring;
9481 int count = specpdl_ptr - specpdl;
9482 int old_height, old_width;
9483 int width, height;
9484 struct gcpro gcpro1;
9486 if (!NILP (stuffstring))
9487 CHECK_STRING (stuffstring, 0);
9489 /* Run the functions in suspend-hook. */
9490 if (!NILP (Vrun_hooks))
9491 call1 (Vrun_hooks, intern ("suspend-hook"));
9493 GCPRO1 (stuffstring);
9494 get_frame_size (&old_width, &old_height);
9495 reset_sys_modes ();
9496 /* sys_suspend can get an error if it tries to fork a subshell
9497 and the system resources aren't available for that. */
9498 record_unwind_protect ((Lisp_Object (*) P_ ((Lisp_Object))) init_sys_modes,
9499 Qnil);
9500 stuff_buffered_input (stuffstring);
9501 if (cannot_suspend)
9502 sys_subshell ();
9503 else
9504 sys_suspend ();
9505 unbind_to (count, Qnil);
9507 /* Check if terminal/window size has changed.
9508 Note that this is not useful when we are running directly
9509 with a window system; but suspend should be disabled in that case. */
9510 get_frame_size (&width, &height);
9511 if (width != old_width || height != old_height)
9512 change_frame_size (SELECTED_FRAME (), height, width, 0, 0, 0);
9514 /* Run suspend-resume-hook. */
9515 if (!NILP (Vrun_hooks))
9516 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
9518 UNGCPRO;
9519 return Qnil;
9522 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
9523 Then in any case stuff anything Emacs has read ahead and not used. */
9525 void
9526 stuff_buffered_input (stuffstring)
9527 Lisp_Object stuffstring;
9529 /* stuff_char works only in BSD, versions 4.2 and up. */
9530 #ifdef BSD_SYSTEM
9531 #ifndef BSD4_1
9532 register unsigned char *p;
9534 if (STRINGP (stuffstring))
9536 register int count;
9538 p = XSTRING (stuffstring)->data;
9539 count = STRING_BYTES (XSTRING (stuffstring));
9540 while (count-- > 0)
9541 stuff_char (*p++);
9542 stuff_char ('\n');
9545 /* Anything we have read ahead, put back for the shell to read. */
9546 /* ?? What should this do when we have multiple keyboards??
9547 Should we ignore anything that was typed in at the "wrong" kboard? */
9548 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
9550 int idx;
9552 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
9553 kbd_fetch_ptr = kbd_buffer;
9554 if (kbd_fetch_ptr->kind == ascii_keystroke)
9555 stuff_char (kbd_fetch_ptr->code);
9557 kbd_fetch_ptr->kind = no_event;
9558 idx = 2 * (kbd_fetch_ptr - kbd_buffer);
9559 ASET (kbd_buffer_gcpro, idx, Qnil);
9560 ASET (kbd_buffer_gcpro, idx + 1, Qnil);
9563 input_pending = 0;
9564 #endif
9565 #endif /* BSD_SYSTEM and not BSD4_1 */
9568 void
9569 set_waiting_for_input (time_to_clear)
9570 EMACS_TIME *time_to_clear;
9572 input_available_clear_time = time_to_clear;
9574 /* Tell interrupt_signal to throw back to read_char, */
9575 waiting_for_input = 1;
9577 /* If interrupt_signal was called before and buffered a C-g,
9578 make it run again now, to avoid timing error. */
9579 if (!NILP (Vquit_flag))
9580 quit_throw_to_read_char ();
9583 void
9584 clear_waiting_for_input ()
9586 /* Tell interrupt_signal not to throw back to read_char, */
9587 waiting_for_input = 0;
9588 input_available_clear_time = 0;
9591 /* This routine is called at interrupt level in response to C-G.
9593 If interrupt_input, this is the handler for SIGINT. Otherwise, it
9594 is called from kbd_buffer_store_event, in handling SIGIO or
9595 SIGTINT.
9597 If `waiting_for_input' is non zero, then unless `echoing' is
9598 nonzero, immediately throw back to read_char.
9600 Otherwise it sets the Lisp variable quit-flag not-nil. This causes
9601 eval to throw, when it gets a chance. If quit-flag is already
9602 non-nil, it stops the job right away. */
9604 SIGTYPE
9605 interrupt_signal (signalnum) /* If we don't have an argument, */
9606 int signalnum; /* some compilers complain in signal calls. */
9608 char c;
9609 /* Must preserve main program's value of errno. */
9610 int old_errno = errno;
9611 struct frame *sf = SELECTED_FRAME ();
9613 #if defined (USG) && !defined (POSIX_SIGNALS)
9614 if (!read_socket_hook && NILP (Vwindow_system))
9616 /* USG systems forget handlers when they are used;
9617 must reestablish each time */
9618 signal (SIGINT, interrupt_signal);
9619 signal (SIGQUIT, interrupt_signal);
9621 #endif /* USG */
9623 cancel_echoing ();
9625 if (!NILP (Vquit_flag)
9626 && (FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf)))
9628 /* If SIGINT isn't blocked, don't let us be interrupted by
9629 another SIGINT, it might be harmful due to non-reentrancy
9630 in I/O functions. */
9631 sigblock (sigmask (SIGINT));
9633 fflush (stdout);
9634 reset_sys_modes ();
9636 #ifdef SIGTSTP /* Support possible in later USG versions */
9638 * On systems which can suspend the current process and return to the original
9639 * shell, this command causes the user to end up back at the shell.
9640 * The "Auto-save" and "Abort" questions are not asked until
9641 * the user elects to return to emacs, at which point he can save the current
9642 * job and either dump core or continue.
9644 sys_suspend ();
9645 #else
9646 #ifdef VMS
9647 if (sys_suspend () == -1)
9649 printf ("Not running as a subprocess;\n");
9650 printf ("you can continue or abort.\n");
9652 #else /* not VMS */
9653 /* Perhaps should really fork an inferior shell?
9654 But that would not provide any way to get back
9655 to the original shell, ever. */
9656 printf ("No support for stopping a process on this operating system;\n");
9657 printf ("you can continue or abort.\n");
9658 #endif /* not VMS */
9659 #endif /* not SIGTSTP */
9660 #ifdef MSDOS
9661 /* We must remain inside the screen area when the internal terminal
9662 is used. Note that [Enter] is not echoed by dos. */
9663 cursor_to (0, 0);
9664 #endif
9665 /* It doesn't work to autosave while GC is in progress;
9666 the code used for auto-saving doesn't cope with the mark bit. */
9667 if (!gc_in_progress)
9669 printf ("Auto-save? (y or n) ");
9670 fflush (stdout);
9671 if (((c = getchar ()) & ~040) == 'Y')
9673 Fdo_auto_save (Qt, Qnil);
9674 #ifdef MSDOS
9675 printf ("\r\nAuto-save done");
9676 #else /* not MSDOS */
9677 printf ("Auto-save done\n");
9678 #endif /* not MSDOS */
9680 while (c != '\n') c = getchar ();
9682 else
9684 /* During GC, it must be safe to reenable quitting again. */
9685 Vinhibit_quit = Qnil;
9686 #ifdef MSDOS
9687 printf ("\r\n");
9688 #endif /* not MSDOS */
9689 printf ("Garbage collection in progress; cannot auto-save now\r\n");
9690 printf ("but will instead do a real quit after garbage collection ends\r\n");
9691 fflush (stdout);
9694 #ifdef MSDOS
9695 printf ("\r\nAbort? (y or n) ");
9696 #else /* not MSDOS */
9697 #ifdef VMS
9698 printf ("Abort (and enter debugger)? (y or n) ");
9699 #else /* not VMS */
9700 printf ("Abort (and dump core)? (y or n) ");
9701 #endif /* not VMS */
9702 #endif /* not MSDOS */
9703 fflush (stdout);
9704 if (((c = getchar ()) & ~040) == 'Y')
9705 abort ();
9706 while (c != '\n') c = getchar ();
9707 #ifdef MSDOS
9708 printf ("\r\nContinuing...\r\n");
9709 #else /* not MSDOS */
9710 printf ("Continuing...\n");
9711 #endif /* not MSDOS */
9712 fflush (stdout);
9713 init_sys_modes ();
9714 sigfree ();
9716 else
9718 /* If executing a function that wants to be interrupted out of
9719 and the user has not deferred quitting by binding `inhibit-quit'
9720 then quit right away. */
9721 if (immediate_quit && NILP (Vinhibit_quit))
9723 struct gl_state_s saved;
9724 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9726 immediate_quit = 0;
9727 sigfree ();
9728 saved = gl_state;
9729 GCPRO4 (saved.object, saved.global_code,
9730 saved.current_syntax_table, saved.old_prop);
9731 Fsignal (Qquit, Qnil);
9732 gl_state = saved;
9733 UNGCPRO;
9735 else
9736 /* Else request quit when it's safe */
9737 Vquit_flag = Qt;
9740 if (waiting_for_input && !echoing)
9741 quit_throw_to_read_char ();
9743 errno = old_errno;
9746 /* Handle a C-g by making read_char return C-g. */
9748 void
9749 quit_throw_to_read_char ()
9751 sigfree ();
9752 /* Prevent another signal from doing this before we finish. */
9753 clear_waiting_for_input ();
9754 input_pending = 0;
9756 Vunread_command_events = Qnil;
9757 unread_command_char = -1;
9759 #if 0 /* Currently, sit_for is called from read_char without turning
9760 off polling. And that can call set_waiting_for_input.
9761 It seems to be harmless. */
9762 #ifdef POLL_FOR_INPUT
9763 /* May be > 1 if in recursive minibuffer. */
9764 if (poll_suppress_count == 0)
9765 abort ();
9766 #endif
9767 #endif
9768 if (FRAMEP (internal_last_event_frame)
9769 && !EQ (internal_last_event_frame, selected_frame))
9770 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
9771 Qnil, 0);
9773 _longjmp (getcjmp, 1);
9776 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
9777 "Set mode of reading keyboard input.\n\
9778 First arg INTERRUPT non-nil means use input interrupts;\n\
9779 nil means use CBREAK mode.\n\
9780 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
9781 (no effect except in CBREAK mode).\n\
9782 Third arg META t means accept 8-bit input (for a Meta key).\n\
9783 META nil means ignore the top bit, on the assumption it is parity.\n\
9784 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
9785 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
9786 See also `current-input-mode'.")
9787 (interrupt, flow, meta, quit)
9788 Lisp_Object interrupt, flow, meta, quit;
9790 if (!NILP (quit)
9791 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
9792 error ("set-input-mode: QUIT must be an ASCII character");
9794 #ifdef POLL_FOR_INPUT
9795 stop_polling ();
9796 #endif
9798 #ifndef DOS_NT
9799 /* this causes startup screen to be restored and messes with the mouse */
9800 reset_sys_modes ();
9801 #endif
9803 #ifdef SIGIO
9804 /* Note SIGIO has been undef'd if FIONREAD is missing. */
9805 if (read_socket_hook)
9807 /* When using X, don't give the user a real choice,
9808 because we haven't implemented the mechanisms to support it. */
9809 #ifdef NO_SOCK_SIGIO
9810 interrupt_input = 0;
9811 #else /* not NO_SOCK_SIGIO */
9812 interrupt_input = 1;
9813 #endif /* NO_SOCK_SIGIO */
9815 else
9816 interrupt_input = !NILP (interrupt);
9817 #else /* not SIGIO */
9818 interrupt_input = 0;
9819 #endif /* not SIGIO */
9821 /* Our VMS input only works by interrupts, as of now. */
9822 #ifdef VMS
9823 interrupt_input = 1;
9824 #endif
9826 flow_control = !NILP (flow);
9827 if (NILP (meta))
9828 meta_key = 0;
9829 else if (EQ (meta, Qt))
9830 meta_key = 1;
9831 else
9832 meta_key = 2;
9833 if (!NILP (quit))
9834 /* Don't let this value be out of range. */
9835 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
9837 #ifndef DOS_NT
9838 init_sys_modes ();
9839 #endif
9841 #ifdef POLL_FOR_INPUT
9842 poll_suppress_count = 1;
9843 start_polling ();
9844 #endif
9845 return Qnil;
9848 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
9849 "Return information about the way Emacs currently reads keyboard input.\n\
9850 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
9851 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
9852 nil, Emacs is using CBREAK mode.\n\
9853 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
9854 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
9855 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
9856 META nil means ignoring the top bit, on the assumption it is parity.\n\
9857 META is neither t nor nil if accepting 8-bit input and using\n\
9858 all 8 bits as the character code.\n\
9859 QUIT is the character Emacs currently uses to quit.\n\
9860 The elements of this list correspond to the arguments of\n\
9861 `set-input-mode'.")
9864 Lisp_Object val[4];
9866 val[0] = interrupt_input ? Qt : Qnil;
9867 val[1] = flow_control ? Qt : Qnil;
9868 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
9869 XSETFASTINT (val[3], quit_char);
9871 return Flist (sizeof (val) / sizeof (val[0]), val);
9876 * Set up a new kboard object with reasonable initial values.
9878 void
9879 init_kboard (kb)
9880 KBOARD *kb;
9882 kb->Voverriding_terminal_local_map = Qnil;
9883 kb->Vlast_command = Qnil;
9884 kb->Vreal_last_command = Qnil;
9885 kb->Vprefix_arg = Qnil;
9886 kb->Vlast_prefix_arg = Qnil;
9887 kb->kbd_queue = Qnil;
9888 kb->kbd_queue_has_data = 0;
9889 kb->immediate_echo = 0;
9890 kb->echoptr = kb->echobuf;
9891 kb->echo_after_prompt = -1;
9892 kb->kbd_macro_buffer = 0;
9893 kb->kbd_macro_bufsize = 0;
9894 kb->defining_kbd_macro = Qnil;
9895 kb->Vlast_kbd_macro = Qnil;
9896 kb->reference_count = 0;
9897 kb->Vsystem_key_alist = Qnil;
9898 kb->system_key_syms = Qnil;
9899 kb->Vdefault_minibuffer_frame = Qnil;
9903 * Destroy the contents of a kboard object, but not the object itself.
9904 * We use this just before deleting it, or if we're going to initialize
9905 * it a second time.
9907 static void
9908 wipe_kboard (kb)
9909 KBOARD *kb;
9911 if (kb->kbd_macro_buffer)
9912 xfree (kb->kbd_macro_buffer);
9915 #ifdef MULTI_KBOARD
9916 void
9917 delete_kboard (kb)
9918 KBOARD *kb;
9920 KBOARD **kbp;
9921 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
9922 if (*kbp == NULL)
9923 abort ();
9924 *kbp = kb->next_kboard;
9925 wipe_kboard (kb);
9926 xfree (kb);
9928 #endif
9930 void
9931 init_keyboard ()
9933 /* This is correct before outermost invocation of the editor loop */
9934 command_loop_level = -1;
9935 immediate_quit = 0;
9936 quit_char = Ctl ('g');
9937 Vunread_command_events = Qnil;
9938 unread_command_char = -1;
9939 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
9940 total_keys = 0;
9941 recent_keys_index = 0;
9942 kbd_fetch_ptr = kbd_buffer;
9943 kbd_store_ptr = kbd_buffer;
9944 kbd_buffer_gcpro = Fmake_vector (make_number (2 * KBD_BUFFER_SIZE), Qnil);
9945 #ifdef HAVE_MOUSE
9946 do_mouse_tracking = Qnil;
9947 #endif
9948 input_pending = 0;
9950 /* This means that command_loop_1 won't try to select anything the first
9951 time through. */
9952 internal_last_event_frame = Qnil;
9953 Vlast_event_frame = internal_last_event_frame;
9955 #ifdef MULTI_KBOARD
9956 current_kboard = initial_kboard;
9957 #endif
9958 wipe_kboard (current_kboard);
9959 init_kboard (current_kboard);
9961 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
9963 signal (SIGINT, interrupt_signal);
9964 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
9965 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
9966 SIGQUIT and we can't tell which one it will give us. */
9967 signal (SIGQUIT, interrupt_signal);
9968 #endif /* HAVE_TERMIO */
9970 /* Note SIGIO has been undef'd if FIONREAD is missing. */
9971 #ifdef SIGIO
9972 if (!noninteractive)
9973 signal (SIGIO, input_available_signal);
9974 #endif /* SIGIO */
9976 /* Use interrupt input by default, if it works and noninterrupt input
9977 has deficiencies. */
9979 #ifdef INTERRUPT_INPUT
9980 interrupt_input = 1;
9981 #else
9982 interrupt_input = 0;
9983 #endif
9985 /* Our VMS input only works by interrupts, as of now. */
9986 #ifdef VMS
9987 interrupt_input = 1;
9988 #endif
9990 sigfree ();
9991 dribble = 0;
9993 if (keyboard_init_hook)
9994 (*keyboard_init_hook) ();
9996 #ifdef POLL_FOR_INPUT
9997 poll_suppress_count = 1;
9998 start_polling ();
9999 #endif
10002 /* This type's only use is in syms_of_keyboard, to initialize the
10003 event header symbols and put properties on them. */
10004 struct event_head {
10005 Lisp_Object *var;
10006 char *name;
10007 Lisp_Object *kind;
10010 struct event_head head_table[] = {
10011 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
10012 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
10013 &Qswitch_frame, "switch-frame", &Qswitch_frame,
10014 &Qdelete_frame, "delete-frame", &Qdelete_frame,
10015 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
10016 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
10019 void
10020 syms_of_keyboard ()
10022 Vlispy_mouse_stem = build_string ("mouse");
10023 staticpro (&Vlispy_mouse_stem);
10025 /* Tool-bars. */
10026 QCimage = intern (":image");
10027 staticpro (&QCimage);
10029 staticpro (&Qhelp_echo);
10030 Qhelp_echo = intern ("help-echo");
10032 staticpro (&item_properties);
10033 item_properties = Qnil;
10035 staticpro (&tool_bar_item_properties);
10036 tool_bar_item_properties = Qnil;
10037 staticpro (&tool_bar_items_vector);
10038 tool_bar_items_vector = Qnil;
10040 staticpro (&real_this_command);
10041 real_this_command = Qnil;
10043 Qtimer_event_handler = intern ("timer-event-handler");
10044 staticpro (&Qtimer_event_handler);
10046 Qdisabled_command_hook = intern ("disabled-command-hook");
10047 staticpro (&Qdisabled_command_hook);
10049 Qself_insert_command = intern ("self-insert-command");
10050 staticpro (&Qself_insert_command);
10052 Qforward_char = intern ("forward-char");
10053 staticpro (&Qforward_char);
10055 Qbackward_char = intern ("backward-char");
10056 staticpro (&Qbackward_char);
10058 Qdisabled = intern ("disabled");
10059 staticpro (&Qdisabled);
10061 Qundefined = intern ("undefined");
10062 staticpro (&Qundefined);
10064 Qpre_command_hook = intern ("pre-command-hook");
10065 staticpro (&Qpre_command_hook);
10067 Qpost_command_hook = intern ("post-command-hook");
10068 staticpro (&Qpost_command_hook);
10070 Qpost_command_idle_hook = intern ("post-command-idle-hook");
10071 staticpro (&Qpost_command_idle_hook);
10073 Qdeferred_action_function = intern ("deferred-action-function");
10074 staticpro (&Qdeferred_action_function);
10076 Qcommand_hook_internal = intern ("command-hook-internal");
10077 staticpro (&Qcommand_hook_internal);
10079 Qfunction_key = intern ("function-key");
10080 staticpro (&Qfunction_key);
10081 Qmouse_click = intern ("mouse-click");
10082 staticpro (&Qmouse_click);
10083 #ifdef WINDOWSNT
10084 Qmouse_wheel = intern ("mouse-wheel");
10085 staticpro (&Qmouse_wheel);
10086 Qlanguage_change = intern ("language-change");
10087 staticpro (&Qlanguage_change);
10088 #endif
10089 Qdrag_n_drop = intern ("drag-n-drop");
10090 staticpro (&Qdrag_n_drop);
10092 Qusr1_signal = intern ("usr1-signal");
10093 staticpro (&Qusr1_signal);
10094 Qusr2_signal = intern ("usr2-signal");
10095 staticpro (&Qusr2_signal);
10097 Qmenu_enable = intern ("menu-enable");
10098 staticpro (&Qmenu_enable);
10099 Qmenu_alias = intern ("menu-alias");
10100 staticpro (&Qmenu_alias);
10101 QCenable = intern (":enable");
10102 staticpro (&QCenable);
10103 QCvisible = intern (":visible");
10104 staticpro (&QCvisible);
10105 QChelp = intern (":help");
10106 staticpro (&QChelp);
10107 QCfilter = intern (":filter");
10108 staticpro (&QCfilter);
10109 QCbutton = intern (":button");
10110 staticpro (&QCbutton);
10111 QCkeys = intern (":keys");
10112 staticpro (&QCkeys);
10113 QCkey_sequence = intern (":key-sequence");
10114 staticpro (&QCkey_sequence);
10115 QCtoggle = intern (":toggle");
10116 staticpro (&QCtoggle);
10117 QCradio = intern (":radio");
10118 staticpro (&QCradio);
10120 Qmode_line = intern ("mode-line");
10121 staticpro (&Qmode_line);
10122 Qvertical_line = intern ("vertical-line");
10123 staticpro (&Qvertical_line);
10124 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
10125 staticpro (&Qvertical_scroll_bar);
10126 Qmenu_bar = intern ("menu-bar");
10127 staticpro (&Qmenu_bar);
10129 Qabove_handle = intern ("above-handle");
10130 staticpro (&Qabove_handle);
10131 Qhandle = intern ("handle");
10132 staticpro (&Qhandle);
10133 Qbelow_handle = intern ("below-handle");
10134 staticpro (&Qbelow_handle);
10135 Qup = intern ("up");
10136 staticpro (&Qup);
10137 Qdown = intern ("down");
10138 staticpro (&Qdown);
10139 Qtop = intern ("top");
10140 staticpro (&Qtop);
10141 Qbottom = intern ("bottom");
10142 staticpro (&Qbottom);
10143 Qend_scroll = intern ("end-scroll");
10144 staticpro (&Qend_scroll);
10145 Qratio = intern ("ratio");
10146 staticpro (&Qratio);
10148 Qevent_kind = intern ("event-kind");
10149 staticpro (&Qevent_kind);
10150 Qevent_symbol_elements = intern ("event-symbol-elements");
10151 staticpro (&Qevent_symbol_elements);
10152 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
10153 staticpro (&Qevent_symbol_element_mask);
10154 Qmodifier_cache = intern ("modifier-cache");
10155 staticpro (&Qmodifier_cache);
10157 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
10158 staticpro (&Qrecompute_lucid_menubar);
10159 Qactivate_menubar_hook = intern ("activate-menubar-hook");
10160 staticpro (&Qactivate_menubar_hook);
10162 Qpolling_period = intern ("polling-period");
10163 staticpro (&Qpolling_period);
10165 Qinput_method_function = intern ("input-method-function");
10166 staticpro (&Qinput_method_function);
10168 Qinput_method_exit_on_first_char = intern ("input-method-exit-on-first-char");
10169 staticpro (&Qinput_method_exit_on_first_char);
10170 Qinput_method_use_echo_area = intern ("input-method-use-echo-area");
10171 staticpro (&Qinput_method_use_echo_area);
10173 Fset (Qinput_method_exit_on_first_char, Qnil);
10174 Fset (Qinput_method_use_echo_area, Qnil);
10176 last_point_position_buffer = Qnil;
10179 struct event_head *p;
10181 for (p = head_table;
10182 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
10183 p++)
10185 *p->var = intern (p->name);
10186 staticpro (p->var);
10187 Fput (*p->var, Qevent_kind, *p->kind);
10188 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
10192 button_down_location = Fmake_vector (make_number (1), Qnil);
10193 staticpro (&button_down_location);
10194 mouse_syms = Fmake_vector (make_number (1), Qnil);
10195 staticpro (&mouse_syms);
10198 int i;
10199 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
10201 modifier_symbols = Fmake_vector (make_number (len), Qnil);
10202 for (i = 0; i < len; i++)
10203 if (modifier_names[i])
10204 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
10205 staticpro (&modifier_symbols);
10208 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
10209 staticpro (&recent_keys);
10211 this_command_keys = Fmake_vector (make_number (40), Qnil);
10212 staticpro (&this_command_keys);
10214 raw_keybuf = Fmake_vector (make_number (30), Qnil);
10215 staticpro (&raw_keybuf);
10217 Qextended_command_history = intern ("extended-command-history");
10218 Fset (Qextended_command_history, Qnil);
10219 staticpro (&Qextended_command_history);
10221 kbd_buffer_gcpro = Fmake_vector (make_number (2 * KBD_BUFFER_SIZE), Qnil);
10222 staticpro (&kbd_buffer_gcpro);
10224 accent_key_syms = Qnil;
10225 staticpro (&accent_key_syms);
10227 func_key_syms = Qnil;
10228 staticpro (&func_key_syms);
10230 #ifdef WINDOWSNT
10231 mouse_wheel_syms = Qnil;
10232 staticpro (&mouse_wheel_syms);
10234 drag_n_drop_syms = Qnil;
10235 staticpro (&drag_n_drop_syms);
10236 #endif
10238 unread_switch_frame = Qnil;
10239 staticpro (&unread_switch_frame);
10241 internal_last_event_frame = Qnil;
10242 staticpro (&internal_last_event_frame);
10244 read_key_sequence_cmd = Qnil;
10245 staticpro (&read_key_sequence_cmd);
10247 menu_bar_one_keymap_changed_items = Qnil;
10248 staticpro (&menu_bar_one_keymap_changed_items);
10250 defsubr (&Sevent_convert_list);
10251 defsubr (&Sread_key_sequence);
10252 defsubr (&Sread_key_sequence_vector);
10253 defsubr (&Srecursive_edit);
10254 #ifdef HAVE_MOUSE
10255 defsubr (&Strack_mouse);
10256 #endif
10257 defsubr (&Sinput_pending_p);
10258 defsubr (&Scommand_execute);
10259 defsubr (&Srecent_keys);
10260 defsubr (&Sthis_command_keys);
10261 defsubr (&Sthis_command_keys_vector);
10262 defsubr (&Sthis_single_command_keys);
10263 defsubr (&Sthis_single_command_raw_keys);
10264 defsubr (&Sreset_this_command_lengths);
10265 defsubr (&Sclear_this_command_keys);
10266 defsubr (&Ssuspend_emacs);
10267 defsubr (&Sabort_recursive_edit);
10268 defsubr (&Sexit_recursive_edit);
10269 defsubr (&Srecursion_depth);
10270 defsubr (&Stop_level);
10271 defsubr (&Sdiscard_input);
10272 defsubr (&Sopen_dribble_file);
10273 defsubr (&Sset_input_mode);
10274 defsubr (&Scurrent_input_mode);
10275 defsubr (&Sexecute_extended_command);
10277 DEFVAR_LISP ("last-command-char", &last_command_char,
10278 "Last input event that was part of a command.");
10280 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
10281 "Last input event that was part of a command.");
10283 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
10284 "Last input event in a command, except for mouse menu events.\n\
10285 Mouse menus give back keys that don't look like mouse events;\n\
10286 this variable holds the actual mouse event that led to the menu,\n\
10287 so that you can determine whether the command was run by mouse or not.");
10289 DEFVAR_LISP ("last-input-char", &last_input_char,
10290 "Last input event.");
10292 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
10293 "Last input event.");
10295 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
10296 "List of events to be read as the command input.\n\
10297 These events are processed first, before actual keyboard input.");
10298 Vunread_command_events = Qnil;
10300 DEFVAR_INT ("unread-command-char", &unread_command_char,
10301 "If not -1, an object to be read as next command input event.");
10303 DEFVAR_LISP ("unread-post-input-method-events", &Vunread_post_input_method_events,
10304 "List of events to be processed as input by input methods.\n\
10305 These events are processed after `unread-command-events', but\n\
10306 before actual keyboard input.");
10307 Vunread_post_input_method_events = Qnil;
10309 DEFVAR_LISP ("unread-input-method-events", &Vunread_input_method_events,
10310 "List of events to be processed as input by input methods.\n\
10311 These events are processed after `unread-command-events', but\n\
10312 before actual keyboard input.");
10313 Vunread_input_method_events = Qnil;
10315 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
10316 "Meta-prefix character code.\n\
10317 Meta-foo as command input turns into this character followed by foo.");
10318 XSETINT (meta_prefix_char, 033);
10320 DEFVAR_KBOARD ("last-command", Vlast_command,
10321 "The last command executed.\n\
10322 Normally a symbol with a function definition, but can be whatever was found\n\
10323 in the keymap, or whatever the variable `this-command' was set to by that\n\
10324 command.\n\
10326 The value `mode-exit' is special; it means that the previous command\n\
10327 read an event that told it to exit, and it did so and unread that event.\n\
10328 In other words, the present command is the event that made the previous\n\
10329 command exit.\n\
10331 The value `kill-region' is special; it means that the previous command\n\
10332 was a kill command.");
10334 DEFVAR_KBOARD ("real-last-command", Vreal_last_command,
10335 "Same as `last-command', but never altered by Lisp code.");
10337 DEFVAR_LISP ("this-command", &Vthis_command,
10338 "The command now being executed.\n\
10339 The command can set this variable; whatever is put here\n\
10340 will be in `last-command' during the following command.");
10341 Vthis_command = Qnil;
10343 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
10344 "*Number of input events between auto-saves.\n\
10345 Zero means disable autosaving due to number of characters typed.");
10346 auto_save_interval = 300;
10348 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
10349 "*Number of seconds idle time before auto-save.\n\
10350 Zero or nil means disable auto-saving due to idleness.\n\
10351 After auto-saving due to this many seconds of idle time,\n\
10352 Emacs also does a garbage collection if that seems to be warranted.");
10353 XSETFASTINT (Vauto_save_timeout, 30);
10355 DEFVAR_LISP ("echo-keystrokes", &Vecho_keystrokes,
10356 "*Nonzero means echo unfinished commands after this many seconds of pause.\n\
10357 The value may be integer or floating point.");
10358 Vecho_keystrokes = make_number (1);
10360 DEFVAR_INT ("polling-period", &polling_period,
10361 "*Interval between polling for input during Lisp execution.\n\
10362 The reason for polling is to make C-g work to stop a running program.\n\
10363 Polling is needed only when using X windows and SIGIO does not work.\n\
10364 Polling is automatically disabled in all other cases.");
10365 polling_period = 2;
10367 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
10368 "*Maximum time between mouse clicks to make a double-click.\n\
10369 Measured in milliseconds. nil means disable double-click recognition;\n\
10370 t means double-clicks have no time limit and are detected\n\
10371 by position only.");
10372 Vdouble_click_time = make_number (500);
10374 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
10375 "*Non-nil means inhibit local map menu bar menus.");
10376 inhibit_local_menu_bar_menus = 0;
10378 DEFVAR_INT ("num-input-keys", &num_input_keys,
10379 "Number of complete key sequences read as input so far.\n\
10380 This includes key sequences read from keyboard macros.\n\
10381 The number is effectively the number of interactive command invocations.");
10382 num_input_keys = 0;
10384 DEFVAR_INT ("num-nonmacro-input-events", &num_nonmacro_input_events,
10385 "Number of input events read from the keyboard so far.\n\
10386 This does not include events generated by keyboard macros.");
10387 num_nonmacro_input_events = 0;
10389 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
10390 "The frame in which the most recently read event occurred.\n\
10391 If the last event came from a keyboard macro, this is set to `macro'.");
10392 Vlast_event_frame = Qnil;
10394 /* This variable is set up in sysdep.c. */
10395 DEFVAR_LISP ("tty-erase-char", &Vtty_erase_char,
10396 "The ERASE character as set by the user with stty.");
10398 DEFVAR_LISP ("help-char", &Vhelp_char,
10399 "Character to recognize as meaning Help.\n\
10400 When it is read, do `(eval help-form)', and display result if it's a string.\n\
10401 If the value of `help-form' is nil, this char can be read normally.");
10402 XSETINT (Vhelp_char, Ctl ('H'));
10404 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
10405 "List of input events to recognize as meaning Help.\n\
10406 These work just like the value of `help-char' (see that).");
10407 Vhelp_event_list = Qnil;
10409 DEFVAR_LISP ("help-form", &Vhelp_form,
10410 "Form to execute when character `help-char' is read.\n\
10411 If the form returns a string, that string is displayed.\n\
10412 If `help-form' is nil, the help char is not recognized.");
10413 Vhelp_form = Qnil;
10415 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
10416 "Command to run when `help-char' character follows a prefix key.\n\
10417 This command is used only when there is no actual binding\n\
10418 for that character after that prefix key.");
10419 Vprefix_help_command = Qnil;
10421 DEFVAR_LISP ("top-level", &Vtop_level,
10422 "Form to evaluate when Emacs starts up.\n\
10423 Useful to set before you dump a modified Emacs.");
10424 Vtop_level = Qnil;
10426 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
10427 "Translate table for keyboard input, or nil.\n\
10428 Each character is looked up in this string and the contents used instead.\n\
10429 The value may be a string, a vector, or a char-table.\n\
10430 If it is a string or vector of length N,\n\
10431 character codes N and up are untranslated.\n\
10432 In a vector or a char-table, an element which is nil means \"no translation\".");
10433 Vkeyboard_translate_table = Qnil;
10435 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
10436 "Non-nil means to always spawn a subshell instead of suspending.\n\
10437 \(Even if the operating system has support for stopping a process.\)");
10438 cannot_suspend = 0;
10440 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
10441 "Non-nil means prompt with menus when appropriate.\n\
10442 This is done when reading from a keymap that has a prompt string,\n\
10443 for elements that have prompt strings.\n\
10444 The menu is displayed on the screen\n\
10445 if X menus were enabled at configuration\n\
10446 time and the previous event was a mouse click prefix key.\n\
10447 Otherwise, menu prompting uses the echo area.");
10448 menu_prompting = 1;
10450 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
10451 "Character to see next line of menu prompt.\n\
10452 Type this character while in a menu prompt to rotate around the lines of it.");
10453 XSETINT (menu_prompt_more_char, ' ');
10455 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
10456 "A mask of additional modifier keys to use with every keyboard character.\n\
10457 Emacs applies the modifiers of the character stored here to each keyboard\n\
10458 character it reads. For example, after evaluating the expression\n\
10459 (setq extra-keyboard-modifiers ?\\C-x)\n\
10460 all input characters will have the control modifier applied to them.\n\
10462 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
10463 not count as a control character; rather, it counts as a character\n\
10464 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
10465 cancels any modification.");
10466 extra_keyboard_modifiers = 0;
10468 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
10469 "If an editing command sets this to t, deactivate the mark afterward.\n\
10470 The command loop sets this to nil before each command,\n\
10471 and tests the value when the command returns.\n\
10472 Buffer modification stores t in this variable.");
10473 Vdeactivate_mark = Qnil;
10475 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
10476 "Temporary storage of pre-command-hook or post-command-hook.");
10477 Vcommand_hook_internal = Qnil;
10479 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
10480 "Normal hook run before each command is executed.\n\
10481 If an unhandled error happens in running this hook,\n\
10482 the hook value is set to nil, since otherwise the error\n\
10483 might happen repeatedly and make Emacs nonfunctional.");
10484 Vpre_command_hook = Qnil;
10486 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
10487 "Normal hook run after each command is executed.\n\
10488 If an unhandled error happens in running this hook,\n\
10489 the hook value is set to nil, since otherwise the error\n\
10490 might happen repeatedly and make Emacs nonfunctional.");
10491 Vpost_command_hook = Qnil;
10493 DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
10494 "Normal hook run after each command is executed, if idle.\n\
10495 Errors running the hook are caught and ignored.\n\
10496 This feature is obsolete; use idle timers instead. See `etc/NEWS'.");
10497 Vpost_command_idle_hook = Qnil;
10499 DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
10500 "Delay time before running `post-command-idle-hook'.\n\
10501 This is measured in microseconds.");
10502 post_command_idle_delay = 100000;
10504 #if 0
10505 DEFVAR_LISP ("echo-area-clear-hook", ...,
10506 "Normal hook run when clearing the echo area.");
10507 #endif
10508 Qecho_area_clear_hook = intern ("echo-area-clear-hook");
10509 XSYMBOL (Qecho_area_clear_hook)->value = Qnil;
10511 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
10512 "t means menu bar, specified Lucid style, needs to be recomputed.");
10513 Vlucid_menu_bar_dirty_flag = Qnil;
10515 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
10516 "List of menu bar items to move to the end of the menu bar.\n\
10517 The elements of the list are event types that may have menu bar bindings.");
10518 Vmenu_bar_final_items = Qnil;
10520 DEFVAR_KBOARD ("overriding-terminal-local-map",
10521 Voverriding_terminal_local_map,
10522 "Per-terminal keymap that overrides all other local keymaps.\n\
10523 If this variable is non-nil, it is used as a keymap instead of the\n\
10524 buffer's local map, and the minor mode keymaps and text property keymaps.\n\
10525 This variable is intended to let commands such as `universal-argumemnt'\n\
10526 set up a different keymap for reading the next command.");
10528 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
10529 "Keymap that overrides all other local keymaps.\n\
10530 If this variable is non-nil, it is used as a keymap instead of the\n\
10531 buffer's local map, and the minor mode keymaps and text property keymaps.");
10532 Voverriding_local_map = Qnil;
10534 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
10535 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
10536 Otherwise, the menu bar continues to reflect the buffer's local map\n\
10537 and the minor mode maps regardless of `overriding-local-map'.");
10538 Voverriding_local_map_menu_flag = Qnil;
10540 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
10541 "Keymap defining bindings for special events to execute at low level.");
10542 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
10544 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
10545 "*Non-nil means generate motion events for mouse motion.");
10547 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
10548 "Alist of system-specific X windows key symbols.\n\
10549 Each element should have the form (N . SYMBOL) where N is the\n\
10550 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
10551 and SYMBOL is its name.");
10553 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
10554 "List of deferred actions to be performed at a later time.\n\
10555 The precise format isn't relevant here; we just check whether it is nil.");
10556 Vdeferred_action_list = Qnil;
10558 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
10559 "Function to call to handle deferred actions, after each command.\n\
10560 This function is called with no arguments after each command\n\
10561 whenever `deferred-action-list' is non-nil.");
10562 Vdeferred_action_function = Qnil;
10564 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
10565 "*Non-nil means show the equivalent key-binding when M-x command has one.\n\
10566 The value can be a length of time to show the message for.\n\
10567 If the value is non-nil and not a number, we wait 2 seconds.");
10568 Vsuggest_key_bindings = Qt;
10570 DEFVAR_LISP ("timer-list", &Vtimer_list,
10571 "List of active absolute time timers in order of increasing time");
10572 Vtimer_list = Qnil;
10574 DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
10575 "List of active idle-time timers in order of increasing time");
10576 Vtimer_idle_list = Qnil;
10578 DEFVAR_LISP ("input-method-function", &Vinput_method_function,
10579 "If non-nil, the function that implements the current input method.\n\
10580 It's called with one argument, a printing character that was just read.\n\
10581 \(That means a character with code 040...0176.)\n\
10582 Typically this function uses `read-event' to read additional events.\n\
10583 When it does so, it should first bind `input-method-function' to nil\n\
10584 so it will not be called recursively.\n\
10586 The function should return a list of zero or more events\n\
10587 to be used as input. If it wants to put back some events\n\
10588 to be reconsidered, separately, by the input method,\n\
10589 it can add them to the beginning of `unread-command-events'.\n\
10591 The input method function can find in `input-method-previous-method'\n\
10592 the previous echo area message.\n\
10594 The input method function should refer to the variables\n\
10595 `input-method-use-echo-area' and `input-method-exit-on-first-char'\n\
10596 for guidance on what to do.");
10597 Vinput_method_function = Qnil;
10599 DEFVAR_LISP ("input-method-previous-message",
10600 &Vinput_method_previous_message,
10601 "When `input-method-function' is called, hold the previous echo area message.\n\
10602 This variable exists because `read-event' clears the echo area\n\
10603 before running the input method. It is nil if there was no message.");
10604 Vinput_method_previous_message = Qnil;
10606 DEFVAR_LISP ("show-help-function", &Vshow_help_function,
10607 "If non-nil, the function that implements the display of help.\n\
10608 It's called with one argument, the help string to display.");
10609 Vshow_help_function = Qnil;
10611 DEFVAR_LISP ("disable-point-adjustment", &Vdisable_point_adjustment,
10612 "If non-nil, suppress point adjustment after executing a command.\n\
10614 After a command is executed, if point is moved into a region that has\n\
10615 special properties (e.g. composition, display), we adjust point to\n\
10616 the boundary of the region. But, several special commands sets this\n\
10617 variable to non-nil, then we suppress the point adjustment.\n\
10619 This variable is set to nil before reading a command, and is checked\n\
10620 just after executing the command");
10621 Vdisable_point_adjustment = Qnil;
10623 DEFVAR_LISP ("global-disable-point-adjustment",
10624 &Vglobal_disable_point_adjustment,
10625 "*If non-nil, always suppress point adjustment.\n\
10627 The default value is nil, in which case, point adjustment are\n\
10628 suppressed only after special commands that set\n\
10629 `disable-point-adjustment' (which see) to non-nil.");
10630 Vglobal_disable_point_adjustment = Qnil;
10633 void
10634 keys_of_keyboard ()
10636 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
10637 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
10638 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
10639 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
10640 initial_define_key (meta_map, 'x', "execute-extended-command");
10642 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
10643 "handle-delete-frame");
10644 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
10645 "ignore-event");
10646 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
10647 "ignore-event");