*** empty log message ***
[emacs.git] / src / keyboard.c
blobb132075cf6b867d26e8662141ec8b0eb109ff8c9
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 "frame.h"
31 #include "window.h"
32 #include "commands.h"
33 #include "buffer.h"
34 #include "charset.h"
35 #include "disptab.h"
36 #include "dispextern.h"
37 #include "keyboard.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 extern int errno;
78 /* Variables for blockinput.h: */
80 /* Non-zero if interrupt input is blocked right now. */
81 int interrupt_input_blocked;
83 /* Nonzero means an input interrupt has arrived
84 during the current critical section. */
85 int interrupt_input_pending;
88 /* File descriptor to use for input. */
89 extern int input_fd;
91 #ifdef HAVE_WINDOW_SYSTEM
92 /* Make all keyboard buffers much bigger when using X windows. */
93 #ifdef macintosh
94 /* But not too big (local data > 32K error) if on macintosh */
95 #define KBD_BUFFER_SIZE 512
96 #else
97 #define KBD_BUFFER_SIZE 4096
98 #endif
99 #else /* No X-windows, character input */
100 #define KBD_BUFFER_SIZE 256
101 #endif /* No X-windows */
103 /* Following definition copied from eval.c */
105 struct backtrace
107 struct backtrace *next;
108 Lisp_Object *function;
109 Lisp_Object *args; /* Points to vector of args. */
110 int nargs; /* length of vector. If nargs is UNEVALLED,
111 args points to slot holding list of
112 unevalled args */
113 char evalargs;
116 #ifdef MULTI_KBOARD
117 KBOARD *initial_kboard;
118 KBOARD *current_kboard;
119 KBOARD *all_kboards;
120 int single_kboard;
121 #else
122 KBOARD the_only_kboard;
123 #endif
125 /* Non-nil disable property on a command means
126 do not execute it; call disabled-command-hook's value instead. */
127 Lisp_Object Qdisabled, Qdisabled_command_hook;
129 #define NUM_RECENT_KEYS (100)
130 int recent_keys_index; /* Index for storing next element into recent_keys */
131 int total_keys; /* Total number of elements stored into recent_keys */
132 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
134 /* Vector holding the key sequence that invoked the current command.
135 It is reused for each command, and it may be longer than the current
136 sequence; this_command_key_count indicates how many elements
137 actually mean something.
138 It's easier to staticpro a single Lisp_Object than an array. */
139 Lisp_Object this_command_keys;
140 int this_command_key_count;
142 /* This vector is used as a buffer to record the events that were actually read
143 by read_key_sequence. */
144 Lisp_Object raw_keybuf;
145 int raw_keybuf_count;
147 #define GROW_RAW_KEYBUF \
148 if (raw_keybuf_count == XVECTOR (raw_keybuf)->size) \
150 int newsize = 2 * XVECTOR (raw_keybuf)->size; \
151 Lisp_Object new; \
152 new = Fmake_vector (make_number (newsize), Qnil); \
153 bcopy (XVECTOR (raw_keybuf)->contents, XVECTOR (new)->contents, \
154 raw_keybuf_count * sizeof (Lisp_Object)); \
155 raw_keybuf = new; \
158 /* Number of elements of this_command_keys
159 that precede this key sequence. */
160 int this_single_command_key_start;
162 /* Record values of this_command_key_count and echo_length ()
163 before this command was read. */
164 static int before_command_key_count;
165 static int before_command_echo_length;
166 /* Values of before_command_key_count and before_command_echo_length
167 saved by reset-this-command-lengths. */
168 static int before_command_key_count_1;
169 static int before_command_echo_length_1;
170 /* Flag set by reset-this-command-lengths,
171 saying to reset the lengths when add_command_key is called. */
172 static int before_command_restore_flag;
174 extern int minbuf_level;
176 extern int message_enable_multibyte;
178 extern struct backtrace *backtrace_list;
180 /* If non-nil, the function that implements the display of help.
181 It's called with one argument, the help string to display. */
183 Lisp_Object Vshow_help_function;
185 /* Nonzero means do menu prompting. */
186 static int menu_prompting;
188 /* Character to see next line of menu prompt. */
189 static Lisp_Object menu_prompt_more_char;
191 /* For longjmp to where kbd input is being done. */
192 static jmp_buf getcjmp;
194 /* True while doing kbd input. */
195 int waiting_for_input;
197 /* True while displaying for echoing. Delays C-g throwing. */
199 static int echoing;
201 /* Non-null means we can start echoing at the next input pause even
202 though there is something in the echo area. */
204 static struct kboard *ok_to_echo_at_next_pause;
206 /* The kboard last echoing, or null for none. Reset to 0 in
207 cancel_echoing. If non-null, and a current echo area message
208 exists, and echo_message_buffer is eq to the current message
209 buffer, we know that the message comes from echo_kboard. */
211 static struct kboard *echo_kboard;
213 /* The buffer used for echoing. Set in echo_now, reset in
214 cancel_echoing. */
216 static Lisp_Object echo_message_buffer;
218 /* Nonzero means disregard local maps for the menu bar. */
219 static int inhibit_local_menu_bar_menus;
221 /* Nonzero means C-g should cause immediate error-signal. */
222 int immediate_quit;
224 /* The user's ERASE setting. */
225 Lisp_Object Vtty_erase_char;
227 /* Character to recognize as the help char. */
228 Lisp_Object Vhelp_char;
230 /* List of other event types to recognize as meaning "help". */
231 Lisp_Object Vhelp_event_list;
233 /* Form to execute when help char is typed. */
234 Lisp_Object Vhelp_form;
236 /* Command to run when the help character follows a prefix key. */
237 Lisp_Object Vprefix_help_command;
239 /* List of items that should move to the end of the menu bar. */
240 Lisp_Object Vmenu_bar_final_items;
242 /* Non-nil means show the equivalent key-binding for
243 any M-x command that has one.
244 The value can be a length of time to show the message for.
245 If the value is non-nil and not a number, we wait 2 seconds. */
246 Lisp_Object Vsuggest_key_bindings;
248 /* Character that causes a quit. Normally C-g.
250 If we are running on an ordinary terminal, this must be an ordinary
251 ASCII char, since we want to make it our interrupt character.
253 If we are not running on an ordinary terminal, it still needs to be
254 an ordinary ASCII char. This character needs to be recognized in
255 the input interrupt handler. At this point, the keystroke is
256 represented as a struct input_event, while the desired quit
257 character is specified as a lispy event. The mapping from struct
258 input_events to lispy events cannot run in an interrupt handler,
259 and the reverse mapping is difficult for anything but ASCII
260 keystrokes.
262 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
263 ASCII character. */
264 int quit_char;
266 extern Lisp_Object current_global_map;
267 extern int minibuf_level;
269 /* If non-nil, this is a map that overrides all other local maps. */
270 Lisp_Object Voverriding_local_map;
272 /* If non-nil, Voverriding_local_map applies to the menu bar. */
273 Lisp_Object Voverriding_local_map_menu_flag;
275 /* Keymap that defines special misc events that should
276 be processed immediately at a low level. */
277 Lisp_Object Vspecial_event_map;
279 /* Current depth in recursive edits. */
280 int command_loop_level;
282 /* Total number of times command_loop has read a key sequence. */
283 int num_input_keys;
285 /* Last input character read as a command. */
286 Lisp_Object last_command_char;
288 /* Last input character read as a command, not counting menus
289 reached by the mouse. */
290 Lisp_Object last_nonmenu_event;
292 /* Last input character read for any purpose. */
293 Lisp_Object last_input_char;
295 /* If not Qnil, a list of objects to be read as subsequent command input. */
296 Lisp_Object Vunread_command_events;
298 /* If not Qnil, a list of objects to be read as subsequent command input
299 including input method processing. */
300 Lisp_Object Vunread_input_method_events;
302 /* If not Qnil, a list of objects to be read as subsequent command input
303 but NOT including input method processing. */
304 Lisp_Object Vunread_post_input_method_events;
306 /* If not -1, an event to be read as subsequent command input. */
307 int unread_command_char;
309 /* If not Qnil, this is a switch-frame event which we decided to put
310 off until the end of a key sequence. This should be read as the
311 next command input, after any unread_command_events.
313 read_key_sequence uses this to delay switch-frame events until the
314 end of the key sequence; Fread_char uses it to put off switch-frame
315 events until a non-ASCII event is acceptable as input. */
316 Lisp_Object unread_switch_frame;
318 /* A mask of extra modifier bits to put into every keyboard char. */
319 int extra_keyboard_modifiers;
321 /* Char to use as prefix when a meta character is typed in.
322 This is bound on entry to minibuffer in case ESC is changed there. */
324 Lisp_Object meta_prefix_char;
326 /* Last size recorded for a current buffer which is not a minibuffer. */
327 static int last_non_minibuf_size;
329 /* Number of idle seconds before an auto-save and garbage collection. */
330 static Lisp_Object Vauto_save_timeout;
332 /* Total number of times read_char has returned. */
333 int num_input_events;
335 /* Total number of times read_char has returned, outside of macros. */
336 int num_nonmacro_input_events;
338 /* Auto-save automatically when this many characters have been typed
339 since the last time. */
341 static int auto_save_interval;
343 /* Value of num_nonmacro_input_events as of last auto save. */
345 int last_auto_save;
347 /* The command being executed by the command loop.
348 Commands may set this, and the value set will be copied into
349 current_kboard->Vlast_command instead of the actual command. */
350 Lisp_Object Vthis_command;
352 /* This is like Vthis_command, except that commands never set it. */
353 Lisp_Object real_this_command;
355 /* The value of point when the last command was executed. */
356 int last_point_position;
358 /* The buffer that was current when the last command was started. */
359 Lisp_Object last_point_position_buffer;
361 /* The frame in which the last input event occurred, or Qmacro if the
362 last event came from a macro. We use this to determine when to
363 generate switch-frame events. This may be cleared by functions
364 like Fselect_frame, to make sure that a switch-frame event is
365 generated by the next character. */
366 Lisp_Object internal_last_event_frame;
368 /* A user-visible version of the above, intended to allow users to
369 figure out where the last event came from, if the event doesn't
370 carry that information itself (i.e. if it was a character). */
371 Lisp_Object Vlast_event_frame;
373 /* The timestamp of the last input event we received from the X server.
374 X Windows wants this for selection ownership. */
375 unsigned long last_event_timestamp;
377 Lisp_Object Qself_insert_command;
378 Lisp_Object Qforward_char;
379 Lisp_Object Qbackward_char;
380 Lisp_Object Qundefined;
381 Lisp_Object Qtimer_event_handler;
383 /* read_key_sequence stores here the command definition of the
384 key sequence that it reads. */
385 Lisp_Object read_key_sequence_cmd;
387 /* Echo unfinished commands after this many seconds of pause. */
388 Lisp_Object Vecho_keystrokes;
390 /* Form to evaluate (if non-nil) when Emacs is started. */
391 Lisp_Object Vtop_level;
393 /* User-supplied string to translate input characters through. */
394 Lisp_Object Vkeyboard_translate_table;
396 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
397 extern Lisp_Object Vfunction_key_map;
399 /* Another keymap that maps key sequences into key sequences.
400 This one takes precedence over ordinary definitions. */
401 extern Lisp_Object Vkey_translation_map;
403 /* If non-nil, this implements the current input method. */
404 Lisp_Object Vinput_method_function;
405 Lisp_Object Qinput_method_function;
407 /* When we call Vinput_method_function,
408 this holds the echo area message that was just erased. */
409 Lisp_Object Vinput_method_previous_message;
411 /* Non-nil means deactivate the mark at end of this command. */
412 Lisp_Object Vdeactivate_mark;
414 /* Menu bar specified in Lucid Emacs fashion. */
416 Lisp_Object Vlucid_menu_bar_dirty_flag;
417 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
419 Lisp_Object Qecho_area_clear_hook;
421 /* Hooks to run before and after each command. */
422 Lisp_Object Qpre_command_hook, Vpre_command_hook;
423 Lisp_Object Qpost_command_hook, Vpost_command_hook;
424 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
425 /* Hook run after a command if there's no more input soon. */
426 Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook;
428 /* Delay time in microseconds before running post-command-idle-hook. */
429 int post_command_idle_delay;
431 /* List of deferred actions to be performed at a later time.
432 The precise format isn't relevant here; we just check whether it is nil. */
433 Lisp_Object Vdeferred_action_list;
435 /* Function to call to handle deferred actions, when there are any. */
436 Lisp_Object Vdeferred_action_function;
437 Lisp_Object Qdeferred_action_function;
439 Lisp_Object Qinput_method_exit_on_first_char;
440 Lisp_Object Qinput_method_use_echo_area;
442 /* File in which we write all commands we read. */
443 FILE *dribble;
445 /* Nonzero if input is available. */
446 int input_pending;
448 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
449 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
451 int meta_key;
453 extern char *pending_malloc_warning;
455 /* Circular buffer for pre-read keyboard input. */
457 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
459 /* Vector to GCPRO the Lisp objects referenced from kbd_buffer.
461 The interrupt-level event handlers will never enqueue an event on a
462 frame which is not in Vframe_list, and once an event is dequeued,
463 internal_last_event_frame or the event itself points to the frame.
464 So that's all fine.
466 But while the event is sitting in the queue, it's completely
467 unprotected. Suppose the user types one command which will run for
468 a while and then delete a frame, and then types another event at
469 the frame that will be deleted, before the command gets around to
470 it. Suppose there are no references to this frame elsewhere in
471 Emacs, and a GC occurs before the second event is dequeued. Now we
472 have an event referring to a freed frame, which will crash Emacs
473 when it is dequeued.
475 Similar things happen when an event on a scroll bar is enqueued; the
476 window may be deleted while the event is in the queue.
478 So, we use this vector to protect the Lisp_Objects in the event
479 queue. That way, they'll be dequeued as dead frames or windows,
480 but still valid Lisp objects.
482 If kbd_buffer[i].kind != no_event, then
484 AREF (kbd_buffer_gcpro, 2 * i) == kbd_buffer[i].frame_or_window.
485 AREF (kbd_buffer_gcpro, 2 * i + 1) == kbd_buffer[i].arg. */
487 static Lisp_Object kbd_buffer_gcpro;
489 /* Pointer to next available character in kbd_buffer.
490 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
491 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
492 next available char is in kbd_buffer[0]. */
493 static struct input_event *kbd_fetch_ptr;
495 /* Pointer to next place to store character in kbd_buffer. This
496 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
497 character should go in kbd_buffer[0]. */
498 static struct input_event * volatile kbd_store_ptr;
500 /* The above pair of variables forms a "queue empty" flag. When we
501 enqueue a non-hook event, we increment kbd_store_ptr. When we
502 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
503 there is input available iff the two pointers are not equal.
505 Why not just have a flag set and cleared by the enqueuing and
506 dequeuing functions? Such a flag could be screwed up by interrupts
507 at inopportune times. */
509 /* If this flag is non-nil, we check mouse_moved to see when the
510 mouse moves, and motion events will appear in the input stream.
511 Otherwise, mouse motion is ignored. */
512 static Lisp_Object do_mouse_tracking;
514 /* Symbols to head events. */
515 Lisp_Object Qmouse_movement;
516 Lisp_Object Qscroll_bar_movement;
517 Lisp_Object Qswitch_frame;
518 Lisp_Object Qdelete_frame;
519 Lisp_Object Qiconify_frame;
520 Lisp_Object Qmake_frame_visible;
521 Lisp_Object Qhelp_echo;
523 /* Symbols to denote kinds of events. */
524 Lisp_Object Qfunction_key;
525 Lisp_Object Qmouse_click;
526 #ifdef WINDOWSNT
527 Lisp_Object Qmouse_wheel;
528 Lisp_Object Qlanguage_change;
529 #endif
530 Lisp_Object Qdrag_n_drop;
531 /* Lisp_Object Qmouse_movement; - also an event header */
533 /* Properties of event headers. */
534 Lisp_Object Qevent_kind;
535 Lisp_Object Qevent_symbol_elements;
537 /* menu item parts */
538 Lisp_Object Qmenu_alias;
539 Lisp_Object Qmenu_enable;
540 Lisp_Object QCenable, QCvisible, QChelp, QCfilter, QCkeys, QCkey_sequence;
541 Lisp_Object QCbutton, QCtoggle, QCradio;
542 extern Lisp_Object Vdefine_key_rebound_commands;
543 extern Lisp_Object Qmenu_item;
545 /* An event header symbol HEAD may have a property named
546 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
547 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
548 mask of modifiers applied to it. If present, this is used to help
549 speed up parse_modifiers. */
550 Lisp_Object Qevent_symbol_element_mask;
552 /* An unmodified event header BASE may have a property named
553 Qmodifier_cache, which is an alist mapping modifier masks onto
554 modified versions of BASE. If present, this helps speed up
555 apply_modifiers. */
556 Lisp_Object Qmodifier_cache;
558 /* Symbols to use for parts of windows. */
559 Lisp_Object Qmode_line;
560 Lisp_Object Qvertical_line;
561 Lisp_Object Qvertical_scroll_bar;
562 Lisp_Object Qmenu_bar;
564 Lisp_Object recursive_edit_unwind (), command_loop ();
565 Lisp_Object Fthis_command_keys ();
566 Lisp_Object Qextended_command_history;
567 EMACS_TIME timer_check ();
569 extern Lisp_Object Vhistory_length;
571 extern char *x_get_keysym_name ();
573 static void record_menu_key ();
575 Lisp_Object Qpolling_period;
577 /* List of absolute timers. Appears in order of next scheduled event. */
578 Lisp_Object Vtimer_list;
580 /* List of idle time timers. Appears in order of next scheduled event. */
581 Lisp_Object Vtimer_idle_list;
583 /* Incremented whenever a timer is run. */
584 int timers_run;
586 extern Lisp_Object Vprint_level, Vprint_length;
588 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
589 happens. */
590 EMACS_TIME *input_available_clear_time;
592 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
593 Default is 1 if INTERRUPT_INPUT is defined. */
594 int interrupt_input;
596 /* Nonzero while interrupts are temporarily deferred during redisplay. */
597 int interrupts_deferred;
599 /* Nonzero means use ^S/^Q for flow control. */
600 int flow_control;
602 /* Allow m- file to inhibit use of FIONREAD. */
603 #ifdef BROKEN_FIONREAD
604 #undef FIONREAD
605 #endif
607 /* We are unable to use interrupts if FIONREAD is not available,
608 so flush SIGIO so we won't try. */
609 #ifndef FIONREAD
610 #ifdef SIGIO
611 #undef SIGIO
612 #endif
613 #endif
615 /* If we support a window system, turn on the code to poll periodically
616 to detect C-g. It isn't actually used when doing interrupt input. */
617 #ifdef HAVE_WINDOW_SYSTEM
618 #define POLL_FOR_INPUT
619 #endif
621 /* After a command is executed, if point is moved into a region that
622 has specific properties (e.g. composition, display), we adjust
623 point to the boundary of the region. But, if a command sets this
624 valiable to non-nil, we suppress this point adjustment. This
625 variable is set to nil before reading a command. */
627 Lisp_Object Vdisable_point_adjustment;
629 /* If non-nil, always disable point adjustment. */
631 Lisp_Object Vglobal_disable_point_adjustment;
634 /* Global variable declarations. */
636 /* Function for init_keyboard to call with no args (if nonzero). */
637 void (*keyboard_init_hook) ();
639 static int read_avail_input ();
640 static void get_input_pending ();
641 static int readable_events ();
642 static Lisp_Object read_char_x_menu_prompt ();
643 static Lisp_Object read_char_minibuf_menu_prompt ();
644 static Lisp_Object make_lispy_event ();
645 #ifdef HAVE_MOUSE
646 static Lisp_Object make_lispy_movement ();
647 #endif
648 static Lisp_Object modify_event_symbol ();
649 static Lisp_Object make_lispy_switch_frame ();
650 static int parse_solitary_modifier ();
651 static void save_getcjmp ();
652 static void restore_getcjmp ();
653 static Lisp_Object apply_modifiers P_ ((int, Lisp_Object));
655 /* Nonzero means don't try to suspend even if the operating system seems
656 to support it. */
657 static int cannot_suspend;
659 #define min(a,b) ((a)<(b)?(a):(b))
660 #define max(a,b) ((a)>(b)?(a):(b))
662 /* Install the string STR as the beginning of the string of echoing,
663 so that it serves as a prompt for the next character.
664 Also start echoing. */
666 void
667 echo_prompt (str)
668 char *str;
670 int len = strlen (str);
672 if (len > ECHOBUFSIZE - 4)
673 len = ECHOBUFSIZE - 4;
674 bcopy (str, current_kboard->echobuf, len);
675 current_kboard->echoptr = current_kboard->echobuf + len;
676 *current_kboard->echoptr = '\0';
678 current_kboard->echo_after_prompt = len;
680 echo_now ();
683 /* Add C to the echo string, if echoing is going on.
684 C can be a character, which is printed prettily ("M-C-x" and all that
685 jazz), or a symbol, whose name is printed. */
687 void
688 echo_char (c)
689 Lisp_Object c;
691 extern char *push_key_description ();
693 if (current_kboard->immediate_echo)
695 char *ptr = current_kboard->echoptr;
697 if (ptr != current_kboard->echobuf)
698 *ptr++ = ' ';
700 /* If someone has passed us a composite event, use its head symbol. */
701 c = EVENT_HEAD (c);
703 if (INTEGERP (c))
705 if (ptr - current_kboard->echobuf
706 > ECHOBUFSIZE - KEY_DESCRIPTION_SIZE)
707 return;
709 ptr = push_key_description (XINT (c), ptr);
711 else if (SYMBOLP (c))
713 struct Lisp_String *name = XSYMBOL (c)->name;
714 if ((ptr - current_kboard->echobuf) + STRING_BYTES (name) + 4
715 > ECHOBUFSIZE)
716 return;
717 bcopy (name->data, ptr, STRING_BYTES (name));
718 ptr += STRING_BYTES (name);
721 if (current_kboard->echoptr == current_kboard->echobuf
722 && help_char_p (c))
724 strcpy (ptr, " (Type ? for further options)");
725 ptr += strlen (ptr);
728 *ptr = 0;
729 current_kboard->echoptr = ptr;
731 echo_now ();
735 /* Temporarily add a dash to the end of the echo string if it's not
736 empty, so that it serves as a mini-prompt for the very next character. */
738 void
739 echo_dash ()
741 if (!current_kboard->immediate_echo
742 && current_kboard->echoptr == current_kboard->echobuf)
743 return;
744 /* Do nothing if we just printed a prompt. */
745 if (current_kboard->echo_after_prompt
746 == current_kboard->echoptr - current_kboard->echobuf)
747 return;
748 /* Do nothing if not echoing at all. */
749 if (current_kboard->echoptr == 0)
750 return;
752 /* Put a dash at the end of the buffer temporarily,
753 but make it go away when the next character is added. */
754 current_kboard->echoptr[0] = '-';
755 current_kboard->echoptr[1] = 0;
757 echo_now ();
760 /* Display the current echo string, and begin echoing if not already
761 doing so. */
763 void
764 echo_now ()
766 if (!current_kboard->immediate_echo)
768 int i;
769 current_kboard->immediate_echo = 1;
771 for (i = 0; i < this_command_key_count; i++)
773 Lisp_Object c;
774 c = XVECTOR (this_command_keys)->contents[i];
775 if (! (EVENT_HAS_PARAMETERS (c)
776 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
777 echo_char (c);
779 echo_dash ();
782 echoing = 1;
783 message2_nolog (current_kboard->echobuf, strlen (current_kboard->echobuf),
784 ! NILP (current_buffer->enable_multibyte_characters));
785 echoing = 0;
787 /* Record in what buffer we echoed, and from which kboard. */
788 echo_message_buffer = echo_area_buffer[0];
789 echo_kboard = current_kboard;
791 if (waiting_for_input && !NILP (Vquit_flag))
792 quit_throw_to_read_char ();
795 /* Turn off echoing, for the start of a new command. */
797 void
798 cancel_echoing ()
800 current_kboard->immediate_echo = 0;
801 current_kboard->echoptr = current_kboard->echobuf;
802 current_kboard->echo_after_prompt = -1;
803 ok_to_echo_at_next_pause = NULL;
804 echo_kboard = NULL;
805 echo_message_buffer = Qnil;
808 /* Return the length of the current echo string. */
810 static int
811 echo_length ()
813 return current_kboard->echoptr - current_kboard->echobuf;
816 /* Truncate the current echo message to its first LEN chars.
817 This and echo_char get used by read_key_sequence when the user
818 switches frames while entering a key sequence. */
820 static void
821 echo_truncate (len)
822 int len;
824 current_kboard->echobuf[len] = '\0';
825 current_kboard->echoptr = current_kboard->echobuf + len;
826 truncate_echo_area (len);
830 /* Functions for manipulating this_command_keys. */
831 static void
832 add_command_key (key)
833 Lisp_Object key;
835 int size = XVECTOR (this_command_keys)->size;
837 /* If reset-this-command-length was called recently, obey it now.
838 See the doc string of that function for an explanation of why. */
839 if (before_command_restore_flag)
841 this_command_key_count = before_command_key_count_1;
842 if (this_command_key_count < this_single_command_key_start)
843 this_single_command_key_start = this_command_key_count;
844 echo_truncate (before_command_echo_length_1);
845 before_command_restore_flag = 0;
848 if (this_command_key_count >= size)
850 Lisp_Object new_keys;
852 new_keys = Fmake_vector (make_number (size * 2), Qnil);
853 bcopy (XVECTOR (this_command_keys)->contents,
854 XVECTOR (new_keys)->contents,
855 size * sizeof (Lisp_Object));
857 this_command_keys = new_keys;
860 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
863 Lisp_Object
864 recursive_edit_1 ()
866 int count = specpdl_ptr - specpdl;
867 Lisp_Object val;
869 if (command_loop_level > 0)
871 specbind (Qstandard_output, Qt);
872 specbind (Qstandard_input, Qt);
875 #ifdef HAVE_X_WINDOWS
876 /* The command loop has started a busy-cursor timer, so we have to
877 cancel it here, otherwise it will fire because the recursive edit
878 can take some time. */
879 if (display_busy_cursor_p)
880 cancel_busy_cursor ();
881 #endif
883 val = command_loop ();
884 if (EQ (val, Qt))
885 Fsignal (Qquit, Qnil);
886 /* Handle throw from read_minibuf when using minibuffer
887 while it's active but we're in another window. */
888 if (STRINGP (val))
889 Fsignal (Qerror, Fcons (val, Qnil));
891 return unbind_to (count, Qnil);
894 /* When an auto-save happens, record the "time", and don't do again soon. */
896 void
897 record_auto_save ()
899 last_auto_save = num_nonmacro_input_events;
902 /* Make an auto save happen as soon as possible at command level. */
904 void
905 force_auto_save_soon ()
907 last_auto_save = - auto_save_interval - 1;
909 record_asynch_buffer_change ();
912 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
913 "Invoke the editor command loop recursively.\n\
914 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
915 that tells this function to return.\n\
916 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
917 This function is called by the editor initialization to begin editing.")
920 int count = specpdl_ptr - specpdl;
922 command_loop_level++;
923 update_mode_lines = 1;
925 record_unwind_protect (recursive_edit_unwind,
926 (command_loop_level
927 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
928 ? Fcurrent_buffer ()
929 : Qnil);
930 recursive_edit_1 ();
931 return unbind_to (count, Qnil);
934 Lisp_Object
935 recursive_edit_unwind (buffer)
936 Lisp_Object buffer;
938 if (!NILP (buffer))
939 Fset_buffer (buffer);
941 command_loop_level--;
942 update_mode_lines = 1;
943 return Qnil;
946 static void
947 any_kboard_state ()
949 #ifdef MULTI_KBOARD
950 #if 0 /* Theory: if there's anything in Vunread_command_events,
951 it will right away be read by read_key_sequence,
952 and then if we do switch KBOARDS, it will go into the side
953 queue then. So we don't need to do anything special here -- rms. */
954 if (CONSP (Vunread_command_events))
956 current_kboard->kbd_queue
957 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
958 current_kboard->kbd_queue_has_data = 1;
960 Vunread_command_events = Qnil;
961 #endif
962 single_kboard = 0;
963 #endif
966 /* Switch to the single-kboard state, making current_kboard
967 the only KBOARD from which further input is accepted. */
969 void
970 single_kboard_state ()
972 #ifdef MULTI_KBOARD
973 single_kboard = 1;
974 #endif
977 /* Maintain a stack of kboards, so other parts of Emacs
978 can switch temporarily to the kboard of a given frame
979 and then revert to the previous status. */
981 struct kboard_stack
983 KBOARD *kboard;
984 struct kboard_stack *next;
987 static struct kboard_stack *kboard_stack;
989 void
990 push_frame_kboard (f)
991 FRAME_PTR f;
993 #ifdef MULTI_KBOARD
994 struct kboard_stack *p
995 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
997 p->next = kboard_stack;
998 p->kboard = current_kboard;
999 kboard_stack = p;
1001 current_kboard = FRAME_KBOARD (f);
1002 #endif
1005 void
1006 pop_frame_kboard ()
1008 #ifdef MULTI_KBOARD
1009 struct kboard_stack *p = kboard_stack;
1010 current_kboard = p->kboard;
1011 kboard_stack = p->next;
1012 xfree (p);
1013 #endif
1016 /* Handle errors that are not handled at inner levels
1017 by printing an error message and returning to the editor command loop. */
1019 Lisp_Object
1020 cmd_error (data)
1021 Lisp_Object data;
1023 Lisp_Object old_level, old_length;
1024 char macroerror[50];
1026 if (!NILP (executing_macro))
1028 if (executing_macro_iterations == 1)
1029 sprintf (macroerror, "After 1 kbd macro iteration: ");
1030 else
1031 sprintf (macroerror, "After %d kbd macro iterations: ",
1032 executing_macro_iterations);
1034 else
1035 *macroerror = 0;
1037 Vstandard_output = Qt;
1038 Vstandard_input = Qt;
1039 Vexecuting_macro = Qnil;
1040 executing_macro = Qnil;
1041 current_kboard->Vprefix_arg = Qnil;
1042 current_kboard->Vlast_prefix_arg = Qnil;
1043 cancel_echoing ();
1045 /* Avoid unquittable loop if data contains a circular list. */
1046 old_level = Vprint_level;
1047 old_length = Vprint_length;
1048 XSETFASTINT (Vprint_level, 10);
1049 XSETFASTINT (Vprint_length, 10);
1050 cmd_error_internal (data, macroerror);
1051 Vprint_level = old_level;
1052 Vprint_length = old_length;
1054 Vquit_flag = Qnil;
1056 Vinhibit_quit = Qnil;
1057 #ifdef MULTI_KBOARD
1058 any_kboard_state ();
1059 #endif
1061 return make_number (0);
1064 /* Take actions on handling an error. DATA is the data that describes
1065 the error.
1067 CONTEXT is a C-string containing ASCII characters only which
1068 describes the context in which the error happened. If we need to
1069 generalize CONTEXT to allow multibyte characters, make it a Lisp
1070 string. */
1072 void
1073 cmd_error_internal (data, context)
1074 Lisp_Object data;
1075 char *context;
1077 Lisp_Object stream;
1078 int kill_emacs_p = 0;
1079 struct frame *sf = SELECTED_FRAME ();
1081 Vquit_flag = Qnil;
1082 Vinhibit_quit = Qt;
1083 clear_message (1, 0);
1085 /* If the window system or terminal frame hasn't been initialized
1086 yet, or we're not interactive, it's best to dump this message out
1087 to stderr and exit. */
1088 if (!sf->glyphs_initialized_p
1089 /* This is the case of the frame dumped with Emacs, when we're
1090 running under a window system. */
1091 || (!NILP (Vwindow_system)
1092 && !inhibit_window_system
1093 && FRAME_TERMCAP_P (sf))
1094 || noninteractive)
1096 stream = Qexternal_debugging_output;
1097 kill_emacs_p = 1;
1099 else
1101 Fdiscard_input ();
1102 bitch_at_user ();
1103 stream = Qt;
1106 if (context != 0)
1107 write_string_1 (context, -1, stream);
1109 print_error_message (data, stream);
1111 /* If the window system or terminal frame hasn't been initialized
1112 yet, or we're in -batch mode, this error should cause Emacs to exit. */
1113 if (kill_emacs_p)
1115 Fterpri (stream);
1116 Fkill_emacs (make_number (-1));
1120 Lisp_Object command_loop_1 ();
1121 Lisp_Object command_loop_2 ();
1122 Lisp_Object top_level_1 ();
1124 /* Entry to editor-command-loop.
1125 This level has the catches for exiting/returning to editor command loop.
1126 It returns nil to exit recursive edit, t to abort it. */
1128 Lisp_Object
1129 command_loop ()
1131 if (command_loop_level > 0 || minibuf_level > 0)
1133 Lisp_Object val;
1134 val = internal_catch (Qexit, command_loop_2, Qnil);
1135 executing_macro = Qnil;
1136 return val;
1138 else
1139 while (1)
1141 internal_catch (Qtop_level, top_level_1, Qnil);
1142 internal_catch (Qtop_level, command_loop_2, Qnil);
1143 executing_macro = Qnil;
1145 /* End of file in -batch run causes exit here. */
1146 if (noninteractive)
1147 Fkill_emacs (Qt);
1151 /* Here we catch errors in execution of commands within the
1152 editing loop, and reenter the editing loop.
1153 When there is an error, cmd_error runs and returns a non-nil
1154 value to us. A value of nil means that cmd_loop_1 itself
1155 returned due to end of file (or end of kbd macro). */
1157 Lisp_Object
1158 command_loop_2 ()
1160 register Lisp_Object val;
1163 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
1164 while (!NILP (val));
1166 return Qnil;
1169 Lisp_Object
1170 top_level_2 ()
1172 return Feval (Vtop_level);
1175 Lisp_Object
1176 top_level_1 ()
1178 /* On entry to the outer level, run the startup file */
1179 if (!NILP (Vtop_level))
1180 internal_condition_case (top_level_2, Qerror, cmd_error);
1181 else if (!NILP (Vpurify_flag))
1182 message ("Bare impure Emacs (standard Lisp code not loaded)");
1183 else
1184 message ("Bare Emacs (standard Lisp code not loaded)");
1185 return Qnil;
1188 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1189 "Exit all recursive editing levels.")
1192 #ifdef HAVE_X_WINDOWS
1193 if (display_busy_cursor_p)
1194 cancel_busy_cursor ();
1195 #endif
1196 Fthrow (Qtop_level, Qnil);
1199 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1200 "Exit from the innermost recursive edit or minibuffer.")
1203 if (command_loop_level > 0 || minibuf_level > 0)
1204 Fthrow (Qexit, Qnil);
1206 error ("No recursive edit is in progress");
1209 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1210 "Abort the command that requested this recursive edit or minibuffer input.")
1213 if (command_loop_level > 0 || minibuf_level > 0)
1214 Fthrow (Qexit, Qt);
1216 error ("No recursive edit is in progress");
1219 /* This is the actual command reading loop,
1220 sans error-handling encapsulation. */
1222 Lisp_Object Fcommand_execute ();
1223 static int read_key_sequence ();
1224 void safe_run_hooks ();
1225 static void adjust_point_for_property ();
1227 Lisp_Object
1228 command_loop_1 ()
1230 Lisp_Object cmd;
1231 int lose;
1232 int nonundocount;
1233 Lisp_Object keybuf[30];
1234 int i;
1235 int no_direct;
1236 int prev_modiff;
1237 struct buffer *prev_buffer;
1238 #ifdef MULTI_KBOARD
1239 int was_locked = single_kboard;
1240 #endif
1242 current_kboard->Vprefix_arg = Qnil;
1243 current_kboard->Vlast_prefix_arg = Qnil;
1244 Vdeactivate_mark = Qnil;
1245 waiting_for_input = 0;
1246 cancel_echoing ();
1248 nonundocount = 0;
1249 this_command_key_count = 0;
1250 this_single_command_key_start = 0;
1252 /* Make sure this hook runs after commands that get errors and
1253 throw to top level. */
1254 /* Note that the value cell will never directly contain nil
1255 if the symbol is a local variable. */
1256 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1257 safe_run_hooks (Qpost_command_hook);
1259 /* If displaying a message, resize the echo area window to fit
1260 that message's size exactly. */
1261 if (!NILP (echo_area_buffer[0]))
1262 resize_echo_area_axactly ();
1264 if (!NILP (Vdeferred_action_list))
1265 call0 (Vdeferred_action_function);
1267 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1269 if (NILP (Vunread_command_events)
1270 && NILP (Vunread_input_method_events)
1271 && NILP (Vunread_post_input_method_events)
1272 && NILP (Vexecuting_macro)
1273 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1274 safe_run_hooks (Qpost_command_idle_hook);
1277 /* Do this after running Vpost_command_hook, for consistency. */
1278 current_kboard->Vlast_command = Vthis_command;
1279 current_kboard->Vreal_last_command = real_this_command;
1281 while (1)
1283 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1284 Fkill_emacs (Qnil);
1286 /* Make sure the current window's buffer is selected. */
1287 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1288 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1290 /* Display any malloc warning that just came out. Use while because
1291 displaying one warning can cause another. */
1293 while (pending_malloc_warning)
1294 display_malloc_warning ();
1296 no_direct = 0;
1298 Vdeactivate_mark = Qnil;
1300 /* If minibuffer on and echo area in use,
1301 wait 2 sec and redraw minibuffer. */
1303 if (minibuf_level
1304 && !NILP (echo_area_buffer[0])
1305 && EQ (minibuf_window, echo_area_window))
1307 /* Bind inhibit-quit to t so that C-g gets read in
1308 rather than quitting back to the minibuffer. */
1309 int count = specpdl_ptr - specpdl;
1310 specbind (Qinhibit_quit, Qt);
1312 Fsit_for (make_number (2), Qnil, Qnil);
1313 /* Clear the echo area. */
1314 message2 (0, 0, 0);
1315 safe_run_hooks (Qecho_area_clear_hook);
1317 unbind_to (count, Qnil);
1319 /* If a C-g came in before, treat it as input now. */
1320 if (!NILP (Vquit_flag))
1322 Vquit_flag = Qnil;
1323 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1327 #ifdef C_ALLOCA
1328 alloca (0); /* Cause a garbage collection now */
1329 /* Since we can free the most stuff here. */
1330 #endif /* C_ALLOCA */
1332 #if 0
1333 /* Select the frame that the last event came from. Usually,
1334 switch-frame events will take care of this, but if some lisp
1335 code swallows a switch-frame event, we'll fix things up here.
1336 Is this a good idea? */
1337 if (FRAMEP (internal_last_event_frame)
1338 && !EQ (internal_last_event_frame, selected_frame))
1339 Fselect_frame (internal_last_event_frame, Qnil);
1340 #endif
1341 /* If it has changed current-menubar from previous value,
1342 really recompute the menubar from the value. */
1343 if (! NILP (Vlucid_menu_bar_dirty_flag)
1344 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1345 call0 (Qrecompute_lucid_menubar);
1347 before_command_key_count = this_command_key_count;
1348 before_command_echo_length = echo_length ();
1350 Vthis_command = Qnil;
1351 real_this_command = Qnil;
1353 /* Read next key sequence; i gets its length. */
1354 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1355 Qnil, 0, 1, 1);
1357 /* A filter may have run while we were reading the input. */
1358 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1359 Fkill_emacs (Qnil);
1360 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1361 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1363 ++num_input_keys;
1365 /* Now we have read a key sequence of length I,
1366 or else I is 0 and we found end of file. */
1368 if (i == 0) /* End of file -- happens only in */
1369 return Qnil; /* a kbd macro, at the end. */
1370 /* -1 means read_key_sequence got a menu that was rejected.
1371 Just loop around and read another command. */
1372 if (i == -1)
1374 cancel_echoing ();
1375 this_command_key_count = 0;
1376 this_single_command_key_start = 0;
1377 goto finalize;
1380 last_command_char = keybuf[i - 1];
1382 /* If the previous command tried to force a specific window-start,
1383 forget about that, in case this command moves point far away
1384 from that position. But also throw away beg_unchanged and
1385 end_unchanged information in that case, so that redisplay will
1386 update the whole window properly. */
1387 if (!NILP (XWINDOW (selected_window)->force_start))
1389 struct buffer *b;
1390 XWINDOW (selected_window)->force_start = Qnil;
1391 b = XBUFFER (XWINDOW (selected_window)->buffer);
1392 BUF_BEG_UNCHANGED (b) = BUF_END_UNCHANGED (b) = 0;
1395 cmd = read_key_sequence_cmd;
1396 if (!NILP (Vexecuting_macro))
1398 if (!NILP (Vquit_flag))
1400 Vexecuting_macro = Qt;
1401 QUIT; /* Make some noise. */
1402 /* Will return since macro now empty. */
1406 /* Do redisplay processing after this command except in special
1407 cases identified below. */
1408 prev_buffer = current_buffer;
1409 prev_modiff = MODIFF;
1410 last_point_position = PT;
1411 XSETBUFFER (last_point_position_buffer, prev_buffer);
1413 /* By default, we adjust point to a boundary of a region that
1414 has such a property that should be treated intangible
1415 (e.g. composition, display). But, some commands will set
1416 this variable differently. */
1417 Vdisable_point_adjustment = Qnil;
1419 /* Execute the command. */
1421 Vthis_command = cmd;
1422 real_this_command = cmd;
1423 /* Note that the value cell will never directly contain nil
1424 if the symbol is a local variable. */
1425 if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
1426 safe_run_hooks (Qpre_command_hook);
1428 if (NILP (Vthis_command))
1430 /* nil means key is undefined. */
1431 bitch_at_user ();
1432 current_kboard->defining_kbd_macro = Qnil;
1433 update_mode_lines = 1;
1434 current_kboard->Vprefix_arg = Qnil;
1436 else
1438 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1440 /* In case we jump to directly_done. */
1441 Vcurrent_prefix_arg = current_kboard->Vprefix_arg;
1443 /* Recognize some common commands in common situations and
1444 do them directly. */
1445 if (EQ (Vthis_command, Qforward_char) && PT < ZV)
1447 struct Lisp_Char_Table *dp
1448 = window_display_table (XWINDOW (selected_window));
1449 lose = FETCH_CHAR (PT_BYTE);
1450 SET_PT (PT + 1);
1451 if ((dp
1452 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1453 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1454 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1455 && (lose >= 0x20 && lose < 0x7f)))
1456 : (lose >= 0x20 && lose < 0x7f))
1457 /* To extract the case of continuation on
1458 wide-column characters. */
1459 && (WIDTH_BY_CHAR_HEAD (FETCH_BYTE (PT_BYTE)) == 1)
1460 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1461 >= MODIFF)
1462 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1463 >= OVERLAY_MODIFF)
1464 && (XFASTINT (XWINDOW (selected_window)->last_point)
1465 == PT - 1)
1466 && !windows_or_buffers_changed
1467 && EQ (current_buffer->selective_display, Qnil)
1468 && !detect_input_pending ()
1469 && NILP (XWINDOW (selected_window)->column_number_displayed)
1470 && NILP (Vexecuting_macro))
1471 direct_output_forward_char (1);
1472 goto directly_done;
1474 else if (EQ (Vthis_command, Qbackward_char) && PT > BEGV)
1476 struct Lisp_Char_Table *dp
1477 = window_display_table (XWINDOW (selected_window));
1478 SET_PT (PT - 1);
1479 lose = FETCH_CHAR (PT_BYTE);
1480 if ((dp
1481 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1482 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1483 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1484 && (lose >= 0x20 && lose < 0x7f)))
1485 : (lose >= 0x20 && lose < 0x7f))
1486 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1487 >= MODIFF)
1488 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1489 >= OVERLAY_MODIFF)
1490 && (XFASTINT (XWINDOW (selected_window)->last_point)
1491 == PT + 1)
1492 && !windows_or_buffers_changed
1493 && EQ (current_buffer->selective_display, Qnil)
1494 && !detect_input_pending ()
1495 && NILP (XWINDOW (selected_window)->column_number_displayed)
1496 && NILP (Vexecuting_macro))
1497 direct_output_forward_char (-1);
1498 goto directly_done;
1500 else if (EQ (Vthis_command, Qself_insert_command)
1501 /* Try this optimization only on ascii keystrokes. */
1502 && INTEGERP (last_command_char))
1504 unsigned int c = XINT (last_command_char);
1505 int value;
1506 if (NILP (Vexecuting_macro)
1507 && !EQ (minibuf_window, selected_window))
1509 if (!nonundocount || nonundocount >= 20)
1511 Fundo_boundary ();
1512 nonundocount = 0;
1514 nonundocount++;
1517 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1518 < MODIFF)
1519 || (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1520 < OVERLAY_MODIFF)
1521 || (XFASTINT (XWINDOW (selected_window)->last_point)
1522 != PT)
1523 || MODIFF <= SAVE_MODIFF
1524 || windows_or_buffers_changed
1525 || !EQ (current_buffer->selective_display, Qnil)
1526 || detect_input_pending ()
1527 || !NILP (XWINDOW (selected_window)->column_number_displayed)
1528 || !NILP (Vexecuting_macro));
1530 value = internal_self_insert (c, 0);
1532 if (value == 2)
1533 nonundocount = 0;
1535 /* VALUE == 1 when AFTER-CHANGE functions are
1536 installed which is the case most of the time
1537 because FONT-LOCK installs one. */
1538 if (!lose && !value)
1539 direct_output_for_insert (c);
1540 goto directly_done;
1544 /* Here for a command that isn't executed directly */
1546 #ifdef HAVE_X_WINDOWS
1547 if (display_busy_cursor_p)
1548 start_busy_cursor ();
1549 #endif
1551 nonundocount = 0;
1552 if (NILP (current_kboard->Vprefix_arg))
1553 Fundo_boundary ();
1554 Fcommand_execute (Vthis_command, Qnil, Qnil, Qnil);
1556 #ifdef HAVE_X_WINDOWS
1557 if (display_busy_cursor_p)
1558 cancel_busy_cursor ();
1559 #endif
1561 directly_done: ;
1562 current_kboard->Vlast_prefix_arg = Vcurrent_prefix_arg;
1564 /* Note that the value cell will never directly contain nil
1565 if the symbol is a local variable. */
1566 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1567 safe_run_hooks (Qpost_command_hook);
1569 /* If displaying a message, resize the echo area window to fit
1570 that message's size exactly. */
1571 if (!NILP (echo_area_buffer[0]))
1572 resize_echo_area_axactly ();
1574 if (!NILP (Vdeferred_action_list))
1575 safe_run_hooks (Qdeferred_action_function);
1577 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1579 if (NILP (Vunread_command_events)
1580 && NILP (Vunread_input_method_events)
1581 && NILP (Vunread_post_input_method_events)
1582 && NILP (Vexecuting_macro)
1583 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1584 safe_run_hooks (Qpost_command_idle_hook);
1587 /* If there is a prefix argument,
1588 1) We don't want Vlast_command to be ``universal-argument''
1589 (that would be dumb), so don't set Vlast_command,
1590 2) we want to leave echoing on so that the prefix will be
1591 echoed as part of this key sequence, so don't call
1592 cancel_echoing, and
1593 3) we want to leave this_command_key_count non-zero, so that
1594 read_char will realize that it is re-reading a character, and
1595 not echo it a second time.
1597 If the command didn't actually create a prefix arg,
1598 but is merely a frame event that is transparent to prefix args,
1599 then the above doesn't apply. */
1600 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1602 current_kboard->Vlast_command = Vthis_command;
1603 current_kboard->Vreal_last_command = real_this_command;
1604 cancel_echoing ();
1605 this_command_key_count = 0;
1606 this_single_command_key_start = 0;
1609 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1611 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1613 current_buffer->mark_active = Qnil;
1614 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1616 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1617 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1620 finalize:
1622 if (current_buffer == prev_buffer
1623 && last_point_position != PT
1624 && NILP (Vdisable_point_adjustment)
1625 && NILP (Vglobal_disable_point_adjustment))
1626 adjust_point_for_property (last_point_position);
1628 /* Install chars successfully executed in kbd macro. */
1630 if (!NILP (current_kboard->defining_kbd_macro)
1631 && NILP (current_kboard->Vprefix_arg))
1632 finalize_kbd_macro_chars ();
1634 #ifdef MULTI_KBOARD
1635 if (!was_locked)
1636 any_kboard_state ();
1637 #endif
1641 extern Lisp_Object Qcomposition, Qdisplay;
1643 /* Adjust point to a boundary of a region that has such a property
1644 that should be treated intangible. For the moment, we check
1645 `composition' and `display' property. LAST_PT is the last position
1646 of point. */
1648 static void
1649 adjust_point_for_property (last_pt)
1650 int last_pt;
1652 int start, end;
1653 Lisp_Object val;
1654 int check_composition = 1, check_display = 1;
1656 while (check_composition || check_display)
1658 if (check_composition
1659 && PT > BEGV && PT < ZV
1660 && get_property_and_range (PT, Qcomposition, &val, &start, &end, Qnil)
1661 && COMPOSITION_VALID_P (start, end, val)
1662 && start < PT && end > PT
1663 && (last_pt <= start || last_pt >= end))
1665 if (PT < last_pt)
1666 SET_PT (start);
1667 else
1668 SET_PT (end);
1669 check_display = 1;
1671 check_composition = 0;
1672 if (check_display
1673 && PT > BEGV && PT < ZV
1674 && get_property_and_range (PT, Qdisplay, &val, &start, &end, Qnil)
1675 && display_prop_intangible_p (val)
1676 && start < PT && end > PT
1677 && (last_pt <= start || last_pt >= end))
1679 if (PT < last_pt)
1680 SET_PT (start);
1681 else
1682 SET_PT (end);
1683 check_composition = 1;
1685 check_display = 0;
1689 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1691 static Lisp_Object
1692 safe_run_hooks_1 (hook)
1693 Lisp_Object hook;
1695 return call1 (Vrun_hooks, Vinhibit_quit);
1698 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1700 static Lisp_Object
1701 safe_run_hooks_error (data)
1702 Lisp_Object data;
1704 return Fset (Vinhibit_quit, Qnil);
1707 /* If we get an error while running the hook, cause the hook variable
1708 to be nil. Also inhibit quits, so that C-g won't cause the hook
1709 to mysteriously evaporate. */
1711 void
1712 safe_run_hooks (hook)
1713 Lisp_Object hook;
1715 int count = specpdl_ptr - specpdl;
1716 specbind (Qinhibit_quit, hook);
1718 internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
1720 unbind_to (count, Qnil);
1724 /* Number of seconds between polling for input. This is a Lisp
1725 variable that can be bound. */
1727 int polling_period;
1729 /* Nonzero means polling for input is temporarily suppressed. */
1731 int poll_suppress_count;
1733 /* Asynchronous timer for polling. */
1735 struct atimer *poll_timer;
1738 #ifdef POLL_FOR_INPUT
1740 /* Poll for input, so what we catch a C-g if it comes in. This
1741 function is called from x_make_frame_visible, see comment
1742 there. */
1744 void
1745 poll_for_input_1 ()
1747 if (interrupt_input_blocked == 0
1748 && !waiting_for_input)
1749 read_avail_input (0);
1752 /* Timer callback function for poll_timer. TIMER is equal to
1753 poll_timer. */
1755 void
1756 poll_for_input (timer)
1757 struct atimer *timer;
1759 if (poll_suppress_count == 0)
1760 poll_for_input_1 ();
1763 #endif /* POLL_FOR_INPUT */
1765 /* Begin signals to poll for input, if they are appropriate.
1766 This function is called unconditionally from various places. */
1768 void
1769 start_polling ()
1771 #ifdef POLL_FOR_INPUT
1772 if (read_socket_hook && !interrupt_input)
1774 /* Turn alarm handling on unconditionally. It might have
1775 been turned off in process.c. */
1776 turn_on_atimers (1);
1778 /* If poll timer doesn't exist, are we need one with
1779 a different interval, start a new one. */
1780 if (poll_timer == NULL
1781 || EMACS_SECS (poll_timer->interval) != polling_period)
1783 EMACS_TIME interval;
1785 if (poll_timer)
1786 cancel_atimer (poll_timer);
1788 EMACS_SET_SECS_USECS (interval, polling_period, 0);
1789 poll_timer = start_atimer (ATIMER_CONTINUOUS, interval,
1790 poll_for_input, NULL);
1793 /* Let the timer's callback function poll for input
1794 if this becomes zero. */
1795 --poll_suppress_count;
1797 #endif
1800 /* Nonzero if we are using polling to handle input asynchronously. */
1803 input_polling_used ()
1805 #ifdef POLL_FOR_INPUT
1806 return read_socket_hook && !interrupt_input;
1807 #else
1808 return 0;
1809 #endif
1812 /* Turn off polling. */
1814 void
1815 stop_polling ()
1817 #ifdef POLL_FOR_INPUT
1818 if (read_socket_hook && !interrupt_input)
1819 ++poll_suppress_count;
1820 #endif
1823 /* Set the value of poll_suppress_count to COUNT
1824 and start or stop polling accordingly. */
1826 void
1827 set_poll_suppress_count (count)
1828 int count;
1830 #ifdef POLL_FOR_INPUT
1831 if (count == 0 && poll_suppress_count != 0)
1833 poll_suppress_count = 1;
1834 start_polling ();
1836 else if (count != 0 && poll_suppress_count == 0)
1838 stop_polling ();
1840 poll_suppress_count = count;
1841 #endif
1844 /* Bind polling_period to a value at least N.
1845 But don't decrease it. */
1847 void
1848 bind_polling_period (n)
1849 int n;
1851 #ifdef POLL_FOR_INPUT
1852 int new = polling_period;
1854 if (n > new)
1855 new = n;
1857 stop_other_atimers (poll_timer);
1858 stop_polling ();
1859 specbind (Qpolling_period, make_number (new));
1860 /* Start a new alarm with the new period. */
1861 start_polling ();
1862 #endif
1865 /* Apply the control modifier to CHARACTER. */
1868 make_ctrl_char (c)
1869 int c;
1871 /* Save the upper bits here. */
1872 int upper = c & ~0177;
1874 c &= 0177;
1876 /* Everything in the columns containing the upper-case letters
1877 denotes a control character. */
1878 if (c >= 0100 && c < 0140)
1880 int oc = c;
1881 c &= ~0140;
1882 /* Set the shift modifier for a control char
1883 made from a shifted letter. But only for letters! */
1884 if (oc >= 'A' && oc <= 'Z')
1885 c |= shift_modifier;
1888 /* The lower-case letters denote control characters too. */
1889 else if (c >= 'a' && c <= 'z')
1890 c &= ~0140;
1892 /* Include the bits for control and shift
1893 only if the basic ASCII code can't indicate them. */
1894 else if (c >= ' ')
1895 c |= ctrl_modifier;
1897 /* Replace the high bits. */
1898 c |= (upper & ~ctrl_modifier);
1900 return c;
1903 /* Display a help message in the echo area. */
1904 void
1905 show_help_echo (msg, ok_to_overwrite_keystroke_echo)
1906 Lisp_Object msg;
1907 int ok_to_overwrite_keystroke_echo;
1909 int count = specpdl_ptr - specpdl;
1911 specbind (Qmessage_truncate_lines, Qt);
1912 if (CONSP (msg))
1913 msg = Feval (msg);
1915 if (!NILP (Vshow_help_function))
1916 call1 (Vshow_help_function, msg);
1917 else if (/* Don't overwrite minibuffer contents. */
1918 !MINI_WINDOW_P (XWINDOW (selected_window))
1919 /* Don't overwrite a keystroke echo. */
1920 && (NILP (echo_message_buffer) || ok_to_overwrite_keystroke_echo)
1921 /* Don't overwrite a prompt. */
1922 && !cursor_in_echo_area)
1924 if (STRINGP (msg))
1925 message3_nolog (msg, XSTRING (msg)->size, STRING_MULTIBYTE (msg));
1926 else
1927 message (0);
1930 unbind_to (count, Qnil);
1935 /* Input of single characters from keyboard */
1937 Lisp_Object print_help ();
1938 static Lisp_Object kbd_buffer_get_event ();
1939 static void record_char ();
1941 #ifdef MULTI_KBOARD
1942 static jmp_buf wrong_kboard_jmpbuf;
1943 #endif
1945 /* read a character from the keyboard; call the redisplay if needed */
1946 /* commandflag 0 means do not do auto-saving, but do do redisplay.
1947 -1 means do not do redisplay, but do do autosaving.
1948 1 means do both. */
1950 /* The arguments MAPS and NMAPS are for menu prompting.
1951 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1953 PREV_EVENT is the previous input event, or nil if we are reading
1954 the first event of a key sequence (or not reading a key sequence).
1955 If PREV_EVENT is t, that is a "magic" value that says
1956 not to run input methods, but in other respects to act as if
1957 not reading a key sequence.
1959 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
1960 if we used a mouse menu to read the input, or zero otherwise. If
1961 USED_MOUSE_MENU is null, we don't dereference it.
1963 Value is t if we showed a menu and the user rejected it. */
1965 Lisp_Object
1966 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
1967 int commandflag;
1968 int nmaps;
1969 Lisp_Object *maps;
1970 Lisp_Object prev_event;
1971 int *used_mouse_menu;
1973 Lisp_Object c;
1974 int count;
1975 jmp_buf local_getcjmp;
1976 jmp_buf save_jump;
1977 int key_already_recorded = 0;
1978 Lisp_Object tem, save;
1979 Lisp_Object previous_echo_area_message;
1980 Lisp_Object also_record;
1981 int reread;
1982 struct gcpro gcpro1, gcpro2;
1984 also_record = Qnil;
1986 before_command_key_count = this_command_key_count;
1987 before_command_echo_length = echo_length ();
1988 c = Qnil;
1989 previous_echo_area_message = Qnil;
1991 GCPRO2 (c, previous_echo_area_message);
1993 retry:
1995 reread = 0;
1996 if (CONSP (Vunread_post_input_method_events))
1998 c = XCAR (Vunread_post_input_method_events);
1999 Vunread_post_input_method_events
2000 = XCDR (Vunread_post_input_method_events);
2002 /* Undo what read_char_x_menu_prompt did when it unread
2003 additional keys returned by Fx_popup_menu. */
2004 if (CONSP (c)
2005 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2006 && NILP (XCDR (c)))
2007 c = XCAR (c);
2009 reread = 1;
2010 goto reread_first;
2013 if (unread_command_char != -1)
2015 XSETINT (c, unread_command_char);
2016 unread_command_char = -1;
2018 reread = 1;
2019 goto reread_first;
2022 if (CONSP (Vunread_command_events))
2024 c = XCAR (Vunread_command_events);
2025 Vunread_command_events = XCDR (Vunread_command_events);
2027 /* Undo what read_char_x_menu_prompt did when it unread
2028 additional keys returned by Fx_popup_menu. */
2029 if (CONSP (c)
2030 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2031 && NILP (XCDR (c)))
2032 c = XCAR (c);
2034 reread = 1;
2035 goto reread_for_input_method;
2038 if (CONSP (Vunread_input_method_events))
2040 c = XCAR (Vunread_input_method_events);
2041 Vunread_input_method_events = XCDR (Vunread_input_method_events);
2043 /* Undo what read_char_x_menu_prompt did when it unread
2044 additional keys returned by Fx_popup_menu. */
2045 if (CONSP (c)
2046 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2047 && NILP (XCDR (c)))
2048 c = XCAR (c);
2049 reread = 1;
2050 goto reread_for_input_method;
2053 /* If there is no function key translated before
2054 reset-this-command-lengths takes effect, forget about it. */
2055 before_command_restore_flag = 0;
2057 if (!NILP (Vexecuting_macro))
2059 /* We set this to Qmacro; since that's not a frame, nobody will
2060 try to switch frames on us, and the selected window will
2061 remain unchanged.
2063 Since this event came from a macro, it would be misleading to
2064 leave internal_last_event_frame set to wherever the last
2065 real event came from. Normally, a switch-frame event selects
2066 internal_last_event_frame after each command is read, but
2067 events read from a macro should never cause a new frame to be
2068 selected. */
2069 Vlast_event_frame = internal_last_event_frame = Qmacro;
2071 /* Exit the macro if we are at the end.
2072 Also, some things replace the macro with t
2073 to force an early exit. */
2074 if (EQ (Vexecuting_macro, Qt)
2075 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
2077 XSETINT (c, -1);
2078 RETURN_UNGCPRO (c);
2081 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
2082 if (STRINGP (Vexecuting_macro)
2083 && (XINT (c) & 0x80))
2084 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
2086 executing_macro_index++;
2088 goto from_macro;
2091 if (!NILP (unread_switch_frame))
2093 c = unread_switch_frame;
2094 unread_switch_frame = Qnil;
2096 /* This event should make it into this_command_keys, and get echoed
2097 again, so we do not set `reread'. */
2098 goto reread_first;
2101 /* if redisplay was requested */
2102 if (commandflag >= 0)
2104 /* If there is pending input, process any events which are not
2105 user-visible, such as X selection_request events. */
2106 if (input_pending
2107 || detect_input_pending_run_timers (0))
2108 swallow_events (0); /* may clear input_pending */
2110 /* Redisplay if no pending input. */
2111 while (!input_pending)
2113 redisplay ();
2115 if (!input_pending)
2116 /* Normal case: no input arrived during redisplay. */
2117 break;
2119 /* Input arrived and pre-empted redisplay.
2120 Process any events which are not user-visible. */
2121 swallow_events (0);
2122 /* If that cleared input_pending, try again to redisplay. */
2126 /* Message turns off echoing unless more keystrokes turn it on again.
2128 The code in 20.x for the condition was
2130 1. echo_area_glyphs && *echo_area_glyphs
2131 2. && echo_area_glyphs != current_kboard->echobuf
2132 3. && ok_to_echo_at_next_pause != echo_area_glyphs
2134 (1) means there's a current message displayed
2136 (2) means it's not the message from echoing from the current
2137 kboard.
2139 (3) There's only one place in 20.x where ok_to_echo_at_next_pause
2140 is set to a non-null value. This is done in read_char and it is
2141 set to echo_area_glyphs after a call to echo_char. That means
2142 ok_to_echo_at_next_pause is either null or
2143 current_kboard->echobuf with the appropriate current_kboard at
2144 that time.
2146 So, condition (3) means in clear text ok_to_echo_at_next_pause
2147 must be either null, or the current message isn't from echoing at
2148 all, or it's from echoing from a different kboard than the
2149 current one. */
2151 if (/* There currently something in the echo area */
2152 !NILP (echo_area_buffer[0])
2153 && (/* And it's either not from echoing. */
2154 !EQ (echo_area_buffer[0], echo_message_buffer)
2155 /* Or it's an echo from a different kboard. */
2156 || echo_kboard != current_kboard
2157 /* Or we explicitly allow overwriting whatever there is. */
2158 || ok_to_echo_at_next_pause == NULL))
2159 cancel_echoing ();
2160 else
2161 echo_dash ();
2163 /* Try reading a character via menu prompting in the minibuf.
2164 Try this before the sit-for, because the sit-for
2165 would do the wrong thing if we are supposed to do
2166 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
2167 after a mouse event so don't try a minibuf menu. */
2168 c = Qnil;
2169 if (nmaps > 0 && INTERACTIVE
2170 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
2171 /* Don't bring up a menu if we already have another event. */
2172 && NILP (Vunread_command_events)
2173 && unread_command_char < 0
2174 && !detect_input_pending_run_timers (0))
2176 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
2177 if (! NILP (c))
2179 key_already_recorded = 1;
2180 goto non_reread_1;
2184 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
2185 We will do that below, temporarily for short sections of code,
2186 when appropriate. local_getcjmp must be in effect
2187 around any call to sit_for or kbd_buffer_get_event;
2188 it *must not* be in effect when we call redisplay. */
2190 if (_setjmp (local_getcjmp))
2192 XSETINT (c, quit_char);
2193 internal_last_event_frame = selected_frame;
2194 Vlast_event_frame = internal_last_event_frame;
2195 /* If we report the quit char as an event,
2196 don't do so more than once. */
2197 if (!NILP (Vinhibit_quit))
2198 Vquit_flag = Qnil;
2200 #ifdef MULTI_KBOARD
2202 KBOARD *kb = FRAME_KBOARD (XFRAME (selected_frame));
2203 if (kb != current_kboard)
2205 Lisp_Object *tailp = &kb->kbd_queue;
2206 /* We shouldn't get here if we were in single-kboard mode! */
2207 if (single_kboard)
2208 abort ();
2209 while (CONSP (*tailp))
2210 tailp = &XCDR (*tailp);
2211 if (!NILP (*tailp))
2212 abort ();
2213 *tailp = Fcons (c, Qnil);
2214 kb->kbd_queue_has_data = 1;
2215 current_kboard = kb;
2216 /* This is going to exit from read_char
2217 so we had better get rid of this frame's stuff. */
2218 UNGCPRO;
2219 longjmp (wrong_kboard_jmpbuf, 1);
2222 #endif
2223 goto non_reread;
2226 timer_start_idle ();
2228 /* If in middle of key sequence and minibuffer not active,
2229 start echoing if enough time elapses. */
2231 if (minibuf_level == 0
2232 && !current_kboard->immediate_echo
2233 && this_command_key_count > 0
2234 && ! noninteractive
2235 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2236 && NILP (Fzerop (Vecho_keystrokes))
2237 && (/* No message. */
2238 NILP (echo_area_buffer[0])
2239 /* Or empty message. */
2240 || (BUF_BEG (XBUFFER (echo_area_buffer[0]))
2241 == BUF_Z (XBUFFER (echo_area_buffer[0])))
2242 /* Or already echoing from same kboard. */
2243 || (echo_kboard && ok_to_echo_at_next_pause == echo_kboard)
2244 /* Or not echoing before and echoing allowed. */
2245 || (!echo_kboard && ok_to_echo_at_next_pause)))
2247 Lisp_Object tem0;
2249 /* After a mouse event, start echoing right away.
2250 This is because we are probably about to display a menu,
2251 and we don't want to delay before doing so. */
2252 if (EVENT_HAS_PARAMETERS (prev_event))
2253 echo_now ();
2254 else
2256 int sec, usec;
2257 double duration = extract_float (Vecho_keystrokes);
2258 sec = (int) duration;
2259 usec = (duration - sec) * 1000000;
2260 save_getcjmp (save_jump);
2261 restore_getcjmp (local_getcjmp);
2262 tem0 = sit_for (sec, usec, 1, 1, 0);
2263 restore_getcjmp (save_jump);
2264 if (EQ (tem0, Qt)
2265 && ! CONSP (Vunread_command_events))
2266 echo_now ();
2270 /* Maybe auto save due to number of keystrokes. */
2272 if (commandflag != 0
2273 && auto_save_interval > 0
2274 && num_nonmacro_input_events - last_auto_save > max (auto_save_interval, 20)
2275 && !detect_input_pending_run_timers (0))
2277 Fdo_auto_save (Qnil, Qnil);
2278 /* Hooks can actually change some buffers in auto save. */
2279 redisplay ();
2282 /* Try reading using an X menu.
2283 This is never confused with reading using the minibuf
2284 because the recursive call of read_char in read_char_minibuf_menu_prompt
2285 does not pass on any keymaps. */
2287 if (nmaps > 0 && INTERACTIVE
2288 && !NILP (prev_event)
2289 && EVENT_HAS_PARAMETERS (prev_event)
2290 && !EQ (XCAR (prev_event), Qmenu_bar)
2291 && !EQ (XCAR (prev_event), Qtool_bar)
2292 /* Don't bring up a menu if we already have another event. */
2293 && NILP (Vunread_command_events)
2294 && unread_command_char < 0)
2296 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
2298 /* Now that we have read an event, Emacs is not idle. */
2299 timer_stop_idle ();
2301 RETURN_UNGCPRO (c);
2304 /* Maybe autosave and/or garbage collect due to idleness. */
2306 if (INTERACTIVE && NILP (c))
2308 int delay_level, buffer_size;
2310 /* Slow down auto saves logarithmically in size of current buffer,
2311 and garbage collect while we're at it. */
2312 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
2313 last_non_minibuf_size = Z - BEG;
2314 buffer_size = (last_non_minibuf_size >> 8) + 1;
2315 delay_level = 0;
2316 while (buffer_size > 64)
2317 delay_level++, buffer_size -= buffer_size >> 2;
2318 if (delay_level < 4) delay_level = 4;
2319 /* delay_level is 4 for files under around 50k, 7 at 100k,
2320 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
2322 /* Auto save if enough time goes by without input. */
2323 if (commandflag != 0
2324 && num_nonmacro_input_events > last_auto_save
2325 && INTEGERP (Vauto_save_timeout)
2326 && XINT (Vauto_save_timeout) > 0)
2328 Lisp_Object tem0;
2330 save_getcjmp (save_jump);
2331 restore_getcjmp (local_getcjmp);
2332 tem0 = sit_for (delay_level * XFASTINT (Vauto_save_timeout) / 4,
2333 0, 1, 1, 0);
2334 restore_getcjmp (save_jump);
2336 if (EQ (tem0, Qt)
2337 && ! CONSP (Vunread_command_events))
2339 Fdo_auto_save (Qnil, Qnil);
2341 /* If we have auto-saved and there is still no input
2342 available, garbage collect if there has been enough
2343 consing going on to make it worthwhile. */
2344 if (!detect_input_pending_run_timers (0)
2345 && consing_since_gc > gc_cons_threshold / 2)
2346 Fgarbage_collect ();
2348 redisplay ();
2353 /* If this has become non-nil here, it has been set by a timer
2354 or sentinel or filter. */
2355 if (CONSP (Vunread_command_events))
2357 c = XCAR (Vunread_command_events);
2358 Vunread_command_events = XCDR (Vunread_command_events);
2361 /* Read something from current KBOARD's side queue, if possible. */
2363 if (NILP (c))
2365 if (current_kboard->kbd_queue_has_data)
2367 if (!CONSP (current_kboard->kbd_queue))
2368 abort ();
2369 c = XCAR (current_kboard->kbd_queue);
2370 current_kboard->kbd_queue
2371 = XCDR (current_kboard->kbd_queue);
2372 if (NILP (current_kboard->kbd_queue))
2373 current_kboard->kbd_queue_has_data = 0;
2374 input_pending = readable_events (0);
2375 if (EVENT_HAS_PARAMETERS (c)
2376 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
2377 internal_last_event_frame = XCAR (XCDR (c));
2378 Vlast_event_frame = internal_last_event_frame;
2382 #ifdef MULTI_KBOARD
2383 /* If current_kboard's side queue is empty check the other kboards.
2384 If one of them has data that we have not yet seen here,
2385 switch to it and process the data waiting for it.
2387 Note: if the events queued up for another kboard
2388 have already been seen here, and therefore are not a complete command,
2389 the kbd_queue_has_data field is 0, so we skip that kboard here.
2390 That's to avoid an infinite loop switching between kboards here. */
2391 if (NILP (c) && !single_kboard)
2393 KBOARD *kb;
2394 for (kb = all_kboards; kb; kb = kb->next_kboard)
2395 if (kb->kbd_queue_has_data)
2397 current_kboard = kb;
2398 /* This is going to exit from read_char
2399 so we had better get rid of this frame's stuff. */
2400 UNGCPRO;
2401 longjmp (wrong_kboard_jmpbuf, 1);
2404 #endif
2406 wrong_kboard:
2408 stop_polling ();
2410 /* Finally, we read from the main queue,
2411 and if that gives us something we can't use yet, we put it on the
2412 appropriate side queue and try again. */
2414 if (NILP (c))
2416 KBOARD *kb;
2418 /* Actually read a character, waiting if necessary. */
2419 save_getcjmp (save_jump);
2420 restore_getcjmp (local_getcjmp);
2421 c = kbd_buffer_get_event (&kb, used_mouse_menu);
2422 restore_getcjmp (save_jump);
2424 #ifdef MULTI_KBOARD
2425 if (! NILP (c) && (kb != current_kboard))
2427 Lisp_Object *tailp = &kb->kbd_queue;
2428 while (CONSP (*tailp))
2429 tailp = &XCDR (*tailp);
2430 if (!NILP (*tailp))
2431 abort ();
2432 *tailp = Fcons (c, Qnil);
2433 kb->kbd_queue_has_data = 1;
2434 c = Qnil;
2435 if (single_kboard)
2436 goto wrong_kboard;
2437 current_kboard = kb;
2438 /* This is going to exit from read_char
2439 so we had better get rid of this frame's stuff. */
2440 UNGCPRO;
2441 longjmp (wrong_kboard_jmpbuf, 1);
2443 #endif
2446 /* Terminate Emacs in batch mode if at eof. */
2447 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
2448 Fkill_emacs (make_number (1));
2450 if (INTEGERP (c))
2452 /* Add in any extra modifiers, where appropriate. */
2453 if ((extra_keyboard_modifiers & CHAR_CTL)
2454 || ((extra_keyboard_modifiers & 0177) < ' '
2455 && (extra_keyboard_modifiers & 0177) != 0))
2456 XSETINT (c, make_ctrl_char (XINT (c)));
2458 /* Transfer any other modifier bits directly from
2459 extra_keyboard_modifiers to c. Ignore the actual character code
2460 in the low 16 bits of extra_keyboard_modifiers. */
2461 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
2464 non_reread:
2466 timer_stop_idle ();
2468 start_polling ();
2470 if (NILP (c))
2472 if (commandflag >= 0
2473 && !input_pending && !detect_input_pending_run_timers (0))
2474 redisplay ();
2476 goto wrong_kboard;
2479 non_reread_1:
2481 /* Buffer switch events are only for internal wakeups
2482 so don't show them to the user.
2483 Also, don't record a key if we already did. */
2484 if (BUFFERP (c) || key_already_recorded)
2485 RETURN_UNGCPRO (c);
2487 /* Process special events within read_char
2488 and loop around to read another event. */
2489 save = Vquit_flag;
2490 Vquit_flag = Qnil;
2491 tem = get_keyelt (access_keymap (get_keymap_1 (Vspecial_event_map, 0, 0),
2492 c, 0, 0), 1);
2493 Vquit_flag = save;
2495 if (!NILP (tem))
2497 int was_locked = single_kboard;
2499 last_input_char = c;
2500 Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt);
2502 /* Resume allowing input from any kboard, if that was true before. */
2503 if (!was_locked)
2504 any_kboard_state ();
2506 goto retry;
2509 /* Handle things that only apply to characters. */
2510 if (INTEGERP (c))
2512 /* If kbd_buffer_get_event gave us an EOF, return that. */
2513 if (XINT (c) == -1)
2514 RETURN_UNGCPRO (c);
2516 if ((STRINGP (Vkeyboard_translate_table)
2517 && XSTRING (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2518 || (VECTORP (Vkeyboard_translate_table)
2519 && XVECTOR (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2520 || (CHAR_TABLE_P (Vkeyboard_translate_table)
2521 && CHAR_TABLE_ORDINARY_SLOTS > (unsigned) XFASTINT (c)))
2523 Lisp_Object d;
2524 d = Faref (Vkeyboard_translate_table, c);
2525 /* nil in keyboard-translate-table means no translation. */
2526 if (!NILP (d))
2527 c = d;
2531 /* If this event is a mouse click in the menu bar,
2532 return just menu-bar for now. Modify the mouse click event
2533 so we won't do this twice, then queue it up. */
2534 if (EVENT_HAS_PARAMETERS (c)
2535 && CONSP (XCDR (c))
2536 && CONSP (EVENT_START (c))
2537 && CONSP (XCDR (EVENT_START (c))))
2539 Lisp_Object posn;
2541 posn = POSN_BUFFER_POSN (EVENT_START (c));
2542 /* Handle menu-bar events:
2543 insert the dummy prefix event `menu-bar'. */
2544 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
2546 /* Change menu-bar to (menu-bar) as the event "position". */
2547 POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
2549 also_record = c;
2550 Vunread_command_events = Fcons (c, Vunread_command_events);
2551 c = posn;
2555 /* Store these characters into recent_keys, the dribble file if any,
2556 and the keyboard macro being defined, if any. */
2557 record_char (c);
2558 if (! NILP (also_record))
2559 record_char (also_record);
2561 /* Wipe the echo area.
2562 But first, if we are about to use an input method,
2563 save the echo area contents for it to refer to. */
2564 if (INTEGERP (c)
2565 && ! NILP (Vinput_method_function)
2566 && (unsigned) XINT (c) >= ' '
2567 && (unsigned) XINT (c) != 127
2568 && (unsigned) XINT (c) < 256)
2570 previous_echo_area_message = Fcurrent_message ();
2571 Vinput_method_previous_message = previous_echo_area_message;
2574 /* Now wipe the echo area, except for help events which do their
2575 own stuff with the echo area. */
2576 if (!CONSP (c) || !(EQ (Qhelp_echo, XCAR (c))))
2578 if (!NILP (echo_area_buffer[0]))
2579 safe_run_hooks (Qecho_area_clear_hook);
2580 clear_message (1, 0);
2583 reread_for_input_method:
2584 from_macro:
2585 /* Pass this to the input method, if appropriate. */
2586 if (INTEGERP (c)
2587 && ! NILP (Vinput_method_function)
2588 /* Don't run the input method within a key sequence,
2589 after the first event of the key sequence. */
2590 && NILP (prev_event)
2591 && (unsigned) XINT (c) >= ' '
2592 && (unsigned) XINT (c) != 127
2593 && (unsigned) XINT (c) < 256)
2595 Lisp_Object keys;
2596 int key_count;
2597 struct gcpro gcpro1;
2598 int count = specpdl_ptr - specpdl;
2600 /* Save the echo status. */
2601 int saved_immediate_echo = current_kboard->immediate_echo;
2602 struct kboard *saved_ok_to_echo = ok_to_echo_at_next_pause;
2603 int saved_echo_after_prompt = current_kboard->echo_after_prompt;
2605 if (before_command_restore_flag)
2607 this_command_key_count = before_command_key_count_1;
2608 if (this_command_key_count < this_single_command_key_start)
2609 this_single_command_key_start = this_command_key_count;
2610 echo_truncate (before_command_echo_length_1);
2611 before_command_restore_flag = 0;
2614 /* Save the this_command_keys status. */
2615 key_count = this_command_key_count;
2617 if (key_count > 0)
2618 keys = Fcopy_sequence (this_command_keys);
2619 else
2620 keys = Qnil;
2621 GCPRO1 (keys);
2623 /* Clear out this_command_keys. */
2624 this_command_key_count = 0;
2626 /* Now wipe the echo area. */
2627 if (!NILP (echo_area_buffer[0]))
2628 safe_run_hooks (Qecho_area_clear_hook);
2629 clear_message (1, 0);
2630 echo_truncate (0);
2632 /* If we are not reading a key sequence,
2633 never use the echo area. */
2634 if (maps == 0)
2636 specbind (Qinput_method_use_echo_area, Qt);
2639 /* Call the input method. */
2640 tem = call1 (Vinput_method_function, c);
2642 tem = unbind_to (count, tem);
2644 /* Restore the saved echoing state
2645 and this_command_keys state. */
2646 this_command_key_count = key_count;
2647 if (key_count > 0)
2648 this_command_keys = keys;
2650 cancel_echoing ();
2651 ok_to_echo_at_next_pause = saved_ok_to_echo;
2652 current_kboard->echo_after_prompt = saved_echo_after_prompt;
2653 if (saved_immediate_echo)
2654 echo_now ();
2656 UNGCPRO;
2658 /* The input method can return no events. */
2659 if (! CONSP (tem))
2661 /* Bring back the previous message, if any. */
2662 if (! NILP (previous_echo_area_message))
2663 message_with_string ("%s", previous_echo_area_message, 0);
2664 goto retry;
2666 /* It returned one event or more. */
2667 c = XCAR (tem);
2668 Vunread_post_input_method_events
2669 = nconc2 (XCDR (tem), Vunread_post_input_method_events);
2672 reread_first:
2674 /* Display help if not echoing. */
2675 if (CONSP (c) && EQ (XCAR (c), Qhelp_echo))
2677 show_help_echo (XCDR (XCDR (c)), 0);
2678 goto retry;
2681 if (this_command_key_count == 0 || ! reread)
2683 before_command_key_count = this_command_key_count;
2684 before_command_echo_length = echo_length ();
2686 /* Don't echo mouse motion events. */
2687 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2688 && NILP (Fzerop (Vecho_keystrokes))
2689 && ! (EVENT_HAS_PARAMETERS (c)
2690 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
2692 echo_char (c);
2693 if (! NILP (also_record))
2694 echo_char (also_record);
2695 /* Once we reread a character, echoing can happen
2696 the next time we pause to read a new one. */
2697 ok_to_echo_at_next_pause = current_kboard;
2700 /* Record this character as part of the current key. */
2701 add_command_key (c);
2702 if (! NILP (also_record))
2703 add_command_key (also_record);
2706 last_input_char = c;
2707 num_input_events++;
2709 /* Process the help character specially if enabled */
2710 if (!NILP (Vhelp_form) && help_char_p (c))
2712 Lisp_Object tem0;
2713 count = specpdl_ptr - specpdl;
2715 record_unwind_protect (Fset_window_configuration,
2716 Fcurrent_window_configuration (Qnil));
2718 tem0 = Feval (Vhelp_form);
2719 if (STRINGP (tem0))
2720 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
2722 cancel_echoing ();
2724 c = read_char (0, 0, 0, Qnil, 0);
2725 while (BUFFERP (c));
2726 /* Remove the help from the frame */
2727 unbind_to (count, Qnil);
2729 redisplay ();
2730 if (EQ (c, make_number (040)))
2732 cancel_echoing ();
2734 c = read_char (0, 0, 0, Qnil, 0);
2735 while (BUFFERP (c));
2739 RETURN_UNGCPRO (c);
2742 /* Record a key that came from a mouse menu.
2743 Record it for echoing, for this-command-keys, and so on. */
2745 static void
2746 record_menu_key (c)
2747 Lisp_Object c;
2749 /* Wipe the echo area. */
2750 clear_message (1, 0);
2752 record_char (c);
2754 before_command_key_count = this_command_key_count;
2755 before_command_echo_length = echo_length ();
2757 /* Don't echo mouse motion events. */
2758 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2759 && NILP (Fzerop (Vecho_keystrokes)))
2761 echo_char (c);
2763 /* Once we reread a character, echoing can happen
2764 the next time we pause to read a new one. */
2765 ok_to_echo_at_next_pause = 0;
2768 /* Record this character as part of the current key. */
2769 add_command_key (c);
2771 /* Re-reading in the middle of a command */
2772 last_input_char = c;
2773 num_input_events++;
2776 /* Return 1 if should recognize C as "the help character". */
2779 help_char_p (c)
2780 Lisp_Object c;
2782 Lisp_Object tail;
2784 if (EQ (c, Vhelp_char))
2785 return 1;
2786 for (tail = Vhelp_event_list; CONSP (tail); tail = XCDR (tail))
2787 if (EQ (c, XCAR (tail)))
2788 return 1;
2789 return 0;
2792 /* Record the input event C in various ways. */
2794 static void
2795 record_char (c)
2796 Lisp_Object c;
2798 total_keys++;
2799 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
2800 if (++recent_keys_index >= NUM_RECENT_KEYS)
2801 recent_keys_index = 0;
2803 /* Write c to the dribble file. If c is a lispy event, write
2804 the event's symbol to the dribble file, in <brackets>. Bleaugh.
2805 If you, dear reader, have a better idea, you've got the source. :-) */
2806 if (dribble)
2808 if (INTEGERP (c))
2810 if (XUINT (c) < 0x100)
2811 putc (XINT (c), dribble);
2812 else
2813 fprintf (dribble, " 0x%x", (int) XUINT (c));
2815 else
2817 Lisp_Object dribblee;
2819 /* If it's a structured event, take the event header. */
2820 dribblee = EVENT_HEAD (c);
2822 if (SYMBOLP (dribblee))
2824 putc ('<', dribble);
2825 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
2826 STRING_BYTES (XSYMBOL (dribblee)->name),
2827 dribble);
2828 putc ('>', dribble);
2832 fflush (dribble);
2835 store_kbd_macro_char (c);
2837 num_nonmacro_input_events++;
2840 Lisp_Object
2841 print_help (object)
2842 Lisp_Object object;
2844 struct buffer *old = current_buffer;
2845 Fprinc (object, Qnil);
2846 set_buffer_internal (XBUFFER (Vstandard_output));
2847 call0 (intern ("help-mode"));
2848 set_buffer_internal (old);
2849 return Qnil;
2852 /* Copy out or in the info on where C-g should throw to.
2853 This is used when running Lisp code from within get_char,
2854 in case get_char is called recursively.
2855 See read_process_output. */
2857 static void
2858 save_getcjmp (temp)
2859 jmp_buf temp;
2861 bcopy (getcjmp, temp, sizeof getcjmp);
2864 static void
2865 restore_getcjmp (temp)
2866 jmp_buf temp;
2868 bcopy (temp, getcjmp, sizeof getcjmp);
2871 #ifdef HAVE_MOUSE
2873 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
2874 of this function. */
2876 static Lisp_Object
2877 tracking_off (old_value)
2878 Lisp_Object old_value;
2880 do_mouse_tracking = old_value;
2881 if (NILP (old_value))
2883 /* Redisplay may have been preempted because there was input
2884 available, and it assumes it will be called again after the
2885 input has been processed. If the only input available was
2886 the sort that we have just disabled, then we need to call
2887 redisplay. */
2888 if (!readable_events (1))
2890 redisplay_preserve_echo_area ();
2891 get_input_pending (&input_pending, 1);
2894 return Qnil;
2897 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
2898 "Evaluate BODY with mouse movement events enabled.\n\
2899 Within a `track-mouse' form, mouse motion generates input events that\n\
2900 you can read with `read-event'.\n\
2901 Normally, mouse motion is ignored.")
2902 (args)
2903 Lisp_Object args;
2905 int count = specpdl_ptr - specpdl;
2906 Lisp_Object val;
2908 record_unwind_protect (tracking_off, do_mouse_tracking);
2910 do_mouse_tracking = Qt;
2912 val = Fprogn (args);
2913 return unbind_to (count, val);
2916 /* If mouse has moved on some frame, return one of those frames.
2917 Return 0 otherwise. */
2919 static FRAME_PTR
2920 some_mouse_moved ()
2922 Lisp_Object tail, frame;
2924 FOR_EACH_FRAME (tail, frame)
2926 if (XFRAME (frame)->mouse_moved)
2927 return XFRAME (frame);
2930 return 0;
2933 #endif /* HAVE_MOUSE */
2935 /* Low level keyboard/mouse input.
2936 kbd_buffer_store_event places events in kbd_buffer, and
2937 kbd_buffer_get_event retrieves them. */
2939 /* Return true iff there are any events in the queue that read-char
2940 would return. If this returns false, a read-char would block. */
2941 static int
2942 readable_events (do_timers_now)
2943 int do_timers_now;
2945 if (do_timers_now)
2946 timer_check (do_timers_now);
2948 if (kbd_fetch_ptr != kbd_store_ptr)
2949 return 1;
2950 #ifdef HAVE_MOUSE
2951 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2952 return 1;
2953 #endif
2954 if (single_kboard)
2956 if (current_kboard->kbd_queue_has_data)
2957 return 1;
2959 else
2961 KBOARD *kb;
2962 for (kb = all_kboards; kb; kb = kb->next_kboard)
2963 if (kb->kbd_queue_has_data)
2964 return 1;
2966 return 0;
2969 /* Set this for debugging, to have a way to get out */
2970 int stop_character;
2972 #ifdef MULTI_KBOARD
2973 static KBOARD *
2974 event_to_kboard (event)
2975 struct input_event *event;
2977 Lisp_Object frame;
2978 frame = event->frame_or_window;
2979 if (CONSP (frame))
2980 frame = XCAR (frame);
2981 else if (WINDOWP (frame))
2982 frame = WINDOW_FRAME (XWINDOW (frame));
2984 /* There are still some events that don't set this field.
2985 For now, just ignore the problem.
2986 Also ignore dead frames here. */
2987 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
2988 return 0;
2989 else
2990 return FRAME_KBOARD (XFRAME (frame));
2992 #endif
2994 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
2996 void
2997 kbd_buffer_store_event (event)
2998 register struct input_event *event;
3000 if (event->kind == no_event)
3001 abort ();
3003 if (event->kind == ascii_keystroke)
3005 register int c = event->code & 0377;
3007 if (event->modifiers & ctrl_modifier)
3008 c = make_ctrl_char (c);
3010 c |= (event->modifiers
3011 & (meta_modifier | alt_modifier
3012 | hyper_modifier | super_modifier));
3014 if (c == quit_char)
3016 extern SIGTYPE interrupt_signal ();
3017 #ifdef MULTI_KBOARD
3018 KBOARD *kb;
3019 struct input_event *sp;
3021 if (single_kboard
3022 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
3023 kb != current_kboard))
3025 kb->kbd_queue
3026 = Fcons (make_lispy_switch_frame (event->frame_or_window),
3027 Fcons (make_number (c), Qnil));
3028 kb->kbd_queue_has_data = 1;
3029 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3031 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3032 sp = kbd_buffer;
3034 if (event_to_kboard (sp) == kb)
3036 sp->kind = no_event;
3037 sp->frame_or_window = Qnil;
3038 sp->arg = Qnil;
3041 return;
3043 #endif
3045 /* If this results in a quit_char being returned to Emacs as
3046 input, set Vlast_event_frame properly. If this doesn't
3047 get returned to Emacs as an event, the next event read
3048 will set Vlast_event_frame again, so this is safe to do. */
3050 Lisp_Object focus;
3052 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
3053 if (NILP (focus))
3054 focus = event->frame_or_window;
3055 internal_last_event_frame = focus;
3056 Vlast_event_frame = focus;
3059 last_event_timestamp = event->timestamp;
3060 interrupt_signal ();
3061 return;
3064 if (c && c == stop_character)
3066 sys_suspend ();
3067 return;
3070 /* Don't insert two buffer_switch_event's in a row.
3071 Just ignore the second one. */
3072 else if (event->kind == buffer_switch_event
3073 && kbd_fetch_ptr != kbd_store_ptr
3074 && kbd_store_ptr->kind == buffer_switch_event)
3075 return;
3077 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
3078 kbd_store_ptr = kbd_buffer;
3080 /* Don't let the very last slot in the buffer become full,
3081 since that would make the two pointers equal,
3082 and that is indistinguishable from an empty buffer.
3083 Discard the event if it would fill the last slot. */
3084 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
3086 int idx;
3088 #if 0 /* The selection_request_event case looks bogus, and it's error
3089 prone to assign individual members for other events, in case
3090 the input_event structure is changed. --2000-07-13, gerd. */
3091 struct input_event *sp = kbd_store_ptr;
3092 sp->kind = event->kind;
3093 if (event->kind == selection_request_event)
3095 /* We must not use the ordinary copying code for this case,
3096 since `part' is an enum and copying it might not copy enough
3097 in this case. */
3098 bcopy (event, (char *) sp, sizeof (*event));
3100 else
3103 sp->code = event->code;
3104 sp->part = event->part;
3105 sp->frame_or_window = event->frame_or_window;
3106 sp->arg = event->arg;
3107 sp->modifiers = event->modifiers;
3108 sp->x = event->x;
3109 sp->y = event->y;
3110 sp->timestamp = event->timestamp;
3112 #else
3113 *kbd_store_ptr = *event;
3114 #endif
3116 idx = 2 * (kbd_store_ptr - kbd_buffer);
3117 ASET (kbd_buffer_gcpro, idx, event->frame_or_window);
3118 ASET (kbd_buffer_gcpro, idx + 1, event->arg);
3119 ++kbd_store_ptr;
3123 /* Discard any mouse events in the event buffer by setting them to
3124 no_event. */
3125 void
3126 discard_mouse_events ()
3128 struct input_event *sp;
3129 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3131 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3132 sp = kbd_buffer;
3134 if (sp->kind == mouse_click
3135 #ifdef WINDOWSNT
3136 || sp->kind == w32_scroll_bar_click
3137 #endif
3138 || sp->kind == scroll_bar_click)
3140 sp->kind = no_event;
3145 /* Read one event from the event buffer, waiting if necessary.
3146 The value is a Lisp object representing the event.
3147 The value is nil for an event that should be ignored,
3148 or that was handled here.
3149 We always read and discard one event. */
3151 static Lisp_Object
3152 kbd_buffer_get_event (kbp, used_mouse_menu)
3153 KBOARD **kbp;
3154 int *used_mouse_menu;
3156 register int c;
3157 Lisp_Object obj;
3159 if (noninteractive)
3161 c = getchar ();
3162 XSETINT (obj, c);
3163 *kbp = current_kboard;
3164 return obj;
3167 /* Wait until there is input available. */
3168 for (;;)
3170 if (kbd_fetch_ptr != kbd_store_ptr)
3171 break;
3172 #ifdef HAVE_MOUSE
3173 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3174 break;
3175 #endif
3177 /* If the quit flag is set, then read_char will return
3178 quit_char, so that counts as "available input." */
3179 if (!NILP (Vquit_flag))
3180 quit_throw_to_read_char ();
3182 /* One way or another, wait until input is available; then, if
3183 interrupt handlers have not read it, read it now. */
3185 #ifdef OLDVMS
3186 wait_for_kbd_input ();
3187 #else
3188 /* Note SIGIO has been undef'd if FIONREAD is missing. */
3189 #ifdef SIGIO
3190 gobble_input (0);
3191 #endif /* SIGIO */
3192 if (kbd_fetch_ptr != kbd_store_ptr)
3193 break;
3194 #ifdef HAVE_MOUSE
3195 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3196 break;
3197 #endif
3199 Lisp_Object minus_one;
3201 XSETINT (minus_one, -1);
3202 wait_reading_process_input (0, 0, minus_one, 1);
3204 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
3205 /* Pass 1 for EXPECT since we just waited to have input. */
3206 read_avail_input (1);
3208 #endif /* not VMS */
3211 if (CONSP (Vunread_command_events))
3213 Lisp_Object first;
3214 first = XCAR (Vunread_command_events);
3215 Vunread_command_events = XCDR (Vunread_command_events);
3216 *kbp = current_kboard;
3217 return first;
3220 /* At this point, we know that there is a readable event available
3221 somewhere. If the event queue is empty, then there must be a
3222 mouse movement enabled and available. */
3223 if (kbd_fetch_ptr != kbd_store_ptr)
3225 struct input_event *event;
3227 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3228 ? kbd_fetch_ptr
3229 : kbd_buffer);
3231 last_event_timestamp = event->timestamp;
3233 #ifdef MULTI_KBOARD
3234 *kbp = event_to_kboard (event);
3235 if (*kbp == 0)
3236 *kbp = current_kboard; /* Better than returning null ptr? */
3237 #else
3238 *kbp = &the_only_kboard;
3239 #endif
3241 obj = Qnil;
3243 /* These two kinds of events get special handling
3244 and don't actually appear to the command loop.
3245 We return nil for them. */
3246 if (event->kind == selection_request_event)
3248 #ifdef HAVE_X11
3249 struct input_event copy;
3251 /* Remove it from the buffer before processing it,
3252 since otherwise swallow_events will see it
3253 and process it again. */
3254 copy = *event;
3255 kbd_fetch_ptr = event + 1;
3256 input_pending = readable_events (0);
3257 x_handle_selection_request (&copy);
3258 #else
3259 /* We're getting selection request events, but we don't have
3260 a window system. */
3261 abort ();
3262 #endif
3265 else if (event->kind == selection_clear_event)
3267 #ifdef HAVE_X11
3268 struct input_event copy;
3270 /* Remove it from the buffer before processing it. */
3271 copy = *event;
3272 kbd_fetch_ptr = event + 1;
3273 input_pending = readable_events (0);
3274 x_handle_selection_clear (&copy);
3275 #else
3276 /* We're getting selection request events, but we don't have
3277 a window system. */
3278 abort ();
3279 #endif
3281 #if defined (HAVE_X11) || defined (HAVE_NTGUI)
3282 else if (event->kind == delete_window_event)
3284 /* Make an event (delete-frame (FRAME)). */
3285 obj = Fcons (event->frame_or_window, Qnil);
3286 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
3287 kbd_fetch_ptr = event + 1;
3289 else if (event->kind == iconify_event)
3291 /* Make an event (iconify-frame (FRAME)). */
3292 obj = Fcons (event->frame_or_window, Qnil);
3293 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
3294 kbd_fetch_ptr = event + 1;
3296 else if (event->kind == deiconify_event)
3298 /* Make an event (make-frame-visible (FRAME)). */
3299 obj = Fcons (event->frame_or_window, Qnil);
3300 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
3301 kbd_fetch_ptr = event + 1;
3303 #endif
3304 else if (event->kind == buffer_switch_event)
3306 /* The value doesn't matter here; only the type is tested. */
3307 XSETBUFFER (obj, current_buffer);
3308 kbd_fetch_ptr = event + 1;
3310 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
3311 else if (event->kind == menu_bar_activate_event)
3313 kbd_fetch_ptr = event + 1;
3314 input_pending = readable_events (0);
3315 if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
3316 x_activate_menubar (XFRAME (event->frame_or_window));
3318 #endif
3319 #ifdef WINDOWSNT
3320 else if (event->kind == language_change_event)
3322 /* Make an event (language-change (FRAME CHARSET LCID)). */
3323 obj = Fcons (event->modifiers, Qnil);
3324 obj = Fcons (event->code, Qnil);
3325 obj = Fcons (event->frame_or_window, obj);
3326 obj = Fcons (Qlanguage_change, Fcons (obj, Qnil));
3327 kbd_fetch_ptr = event + 1;
3329 #endif
3330 /* Just discard these, by returning nil.
3331 With MULTI_KBOARD, these events are used as placeholders
3332 when we need to randomly delete events from the queue.
3333 (They shouldn't otherwise be found in the buffer,
3334 but on some machines it appears they do show up
3335 even without MULTI_KBOARD.) */
3336 /* On Windows NT/9X, no_event is used to delete extraneous
3337 mouse events during a popup-menu call. */
3338 else if (event->kind == no_event)
3339 kbd_fetch_ptr = event + 1;
3340 else if (event->kind == HELP_EVENT)
3342 /* Event->frame_or_window is a frame, event->arg is the
3343 help to display. */
3344 obj = Fcons (Qhelp_echo,
3345 Fcons (event->frame_or_window,
3346 event->arg));
3347 kbd_fetch_ptr = event + 1;
3349 else if (event->kind == FOCUS_IN_EVENT)
3351 /* Notification of a FocusIn event. The frame receiving the
3352 focus is in event->frame_or_window. Generate a
3353 switch-frame event if necessary. */
3354 Lisp_Object frame, focus;
3356 frame = event->frame_or_window;
3357 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
3358 if (FRAMEP (focus))
3359 frame = focus;
3361 if (!EQ (frame, internal_last_event_frame)
3362 && !EQ (frame, selected_frame))
3363 obj = make_lispy_switch_frame (frame);
3364 internal_last_event_frame = frame;
3365 kbd_fetch_ptr = event + 1;
3367 else
3369 /* If this event is on a different frame, return a switch-frame this
3370 time, and leave the event in the queue for next time. */
3371 Lisp_Object frame;
3372 Lisp_Object focus;
3374 frame = event->frame_or_window;
3375 if (CONSP (frame))
3376 frame = XCAR (frame);
3377 else if (WINDOWP (frame))
3378 frame = WINDOW_FRAME (XWINDOW (frame));
3380 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
3381 if (! NILP (focus))
3382 frame = focus;
3384 if (! EQ (frame, internal_last_event_frame)
3385 && !EQ (frame, selected_frame))
3386 obj = make_lispy_switch_frame (frame);
3387 internal_last_event_frame = frame;
3389 /* If we didn't decide to make a switch-frame event, go ahead
3390 and build a real event from the queue entry. */
3392 if (NILP (obj))
3394 int idx;
3396 obj = make_lispy_event (event);
3398 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
3399 /* If this was a menu selection, then set the flag to inhibit
3400 writing to last_nonmenu_event. Don't do this if the event
3401 we're returning is (menu-bar), though; that indicates the
3402 beginning of the menu sequence, and we might as well leave
3403 that as the `event with parameters' for this selection. */
3404 if (used_mouse_menu
3405 && !EQ (event->frame_or_window, event->arg)
3406 && (event->kind == MENU_BAR_EVENT
3407 || event->kind == TOOL_BAR_EVENT))
3408 *used_mouse_menu = 1;
3409 #endif
3411 /* Wipe out this event, to catch bugs. */
3412 event->kind = no_event;
3413 idx = 2 * (event - kbd_buffer);
3414 ASET (kbd_buffer_gcpro, idx, Qnil);
3415 ASET (kbd_buffer_gcpro, idx + 1, Qnil);
3417 kbd_fetch_ptr = event + 1;
3421 #ifdef HAVE_MOUSE
3422 /* Try generating a mouse motion event. */
3423 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3425 FRAME_PTR f = some_mouse_moved ();
3426 Lisp_Object bar_window;
3427 enum scroll_bar_part part;
3428 Lisp_Object x, y;
3429 unsigned long time;
3431 *kbp = current_kboard;
3432 /* Note that this uses F to determine which display to look at.
3433 If there is no valid info, it does not store anything
3434 so x remains nil. */
3435 x = Qnil;
3436 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
3438 obj = Qnil;
3440 /* Decide if we should generate a switch-frame event. Don't
3441 generate switch-frame events for motion outside of all Emacs
3442 frames. */
3443 if (!NILP (x) && f)
3445 Lisp_Object frame;
3447 frame = FRAME_FOCUS_FRAME (f);
3448 if (NILP (frame))
3449 XSETFRAME (frame, f);
3451 if (! EQ (frame, internal_last_event_frame)
3452 && !EQ (frame, selected_frame))
3453 obj = make_lispy_switch_frame (frame);
3454 internal_last_event_frame = frame;
3457 /* If we didn't decide to make a switch-frame event, go ahead and
3458 return a mouse-motion event. */
3459 if (!NILP (x) && NILP (obj))
3460 obj = make_lispy_movement (f, bar_window, part, x, y, time);
3462 #endif /* HAVE_MOUSE */
3463 else
3464 /* We were promised by the above while loop that there was
3465 something for us to read! */
3466 abort ();
3468 input_pending = readable_events (0);
3470 Vlast_event_frame = internal_last_event_frame;
3472 return (obj);
3475 /* Process any events that are not user-visible,
3476 then return, without reading any user-visible events. */
3478 void
3479 swallow_events (do_display)
3480 int do_display;
3482 int old_timers_run;
3484 while (kbd_fetch_ptr != kbd_store_ptr)
3486 struct input_event *event;
3488 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3489 ? kbd_fetch_ptr
3490 : kbd_buffer);
3492 last_event_timestamp = event->timestamp;
3494 /* These two kinds of events get special handling
3495 and don't actually appear to the command loop. */
3496 if (event->kind == selection_request_event)
3498 #ifdef HAVE_X11
3499 struct input_event copy;
3501 /* Remove it from the buffer before processing it,
3502 since otherwise swallow_events called recursively could see it
3503 and process it again. */
3504 copy = *event;
3505 kbd_fetch_ptr = event + 1;
3506 input_pending = readable_events (0);
3507 x_handle_selection_request (&copy);
3508 #else
3509 /* We're getting selection request events, but we don't have
3510 a window system. */
3511 abort ();
3512 #endif
3515 else if (event->kind == selection_clear_event)
3517 #ifdef HAVE_X11
3518 struct input_event copy;
3520 /* Remove it from the buffer before processing it, */
3521 copy = *event;
3523 kbd_fetch_ptr = event + 1;
3524 input_pending = readable_events (0);
3525 x_handle_selection_clear (&copy);
3526 #else
3527 /* We're getting selection request events, but we don't have
3528 a window system. */
3529 abort ();
3530 #endif
3532 else
3533 break;
3536 old_timers_run = timers_run;
3537 get_input_pending (&input_pending, 1);
3539 if (timers_run != old_timers_run && do_display)
3540 redisplay_preserve_echo_area ();
3543 static EMACS_TIME timer_idleness_start_time;
3545 /* Record the start of when Emacs is idle,
3546 for the sake of running idle-time timers. */
3548 void
3549 timer_start_idle ()
3551 Lisp_Object timers;
3553 /* If we are already in the idle state, do nothing. */
3554 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3555 return;
3557 EMACS_GET_TIME (timer_idleness_start_time);
3559 /* Mark all idle-time timers as once again candidates for running. */
3560 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCDR (timers))
3562 Lisp_Object timer;
3564 timer = XCAR (timers);
3566 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3567 continue;
3568 XVECTOR (timer)->contents[0] = Qnil;
3572 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
3574 void
3575 timer_stop_idle ()
3577 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
3580 /* This is only for debugging. */
3581 struct input_event last_timer_event;
3583 /* Check whether a timer has fired. To prevent larger problems we simply
3584 disregard elements that are not proper timers. Do not make a circular
3585 timer list for the time being.
3587 Returns the number of seconds to wait until the next timer fires. If a
3588 timer is triggering now, return zero seconds.
3589 If no timer is active, return -1 seconds.
3591 If a timer is ripe, we run it, with quitting turned off.
3593 DO_IT_NOW is now ignored. It used to mean that we should
3594 run the timer directly instead of queueing a timer-event.
3595 Now we always run timers directly. */
3597 EMACS_TIME
3598 timer_check (do_it_now)
3599 int do_it_now;
3601 EMACS_TIME nexttime;
3602 EMACS_TIME now, idleness_now;
3603 Lisp_Object timers, idle_timers, chosen_timer;
3604 struct gcpro gcpro1, gcpro2, gcpro3;
3606 EMACS_SET_SECS (nexttime, -1);
3607 EMACS_SET_USECS (nexttime, -1);
3609 /* Always consider the ordinary timers. */
3610 timers = Vtimer_list;
3611 /* Consider the idle timers only if Emacs is idle. */
3612 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3613 idle_timers = Vtimer_idle_list;
3614 else
3615 idle_timers = Qnil;
3616 chosen_timer = Qnil;
3617 GCPRO3 (timers, idle_timers, chosen_timer);
3619 if (CONSP (timers) || CONSP (idle_timers))
3621 EMACS_GET_TIME (now);
3622 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3623 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
3626 while (CONSP (timers) || CONSP (idle_timers))
3628 Lisp_Object *vector;
3629 Lisp_Object timer, idle_timer;
3630 EMACS_TIME timer_time, idle_timer_time;
3631 EMACS_TIME difference, timer_difference, idle_timer_difference;
3633 /* Skip past invalid timers and timers already handled. */
3634 if (!NILP (timers))
3636 timer = XCAR (timers);
3637 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3639 timers = XCDR (timers);
3640 continue;
3642 vector = XVECTOR (timer)->contents;
3644 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3645 || !INTEGERP (vector[3])
3646 || ! NILP (vector[0]))
3648 timers = XCDR (timers);
3649 continue;
3652 if (!NILP (idle_timers))
3654 timer = XCAR (idle_timers);
3655 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3657 idle_timers = XCDR (idle_timers);
3658 continue;
3660 vector = XVECTOR (timer)->contents;
3662 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3663 || !INTEGERP (vector[3])
3664 || ! NILP (vector[0]))
3666 idle_timers = XCDR (idle_timers);
3667 continue;
3671 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
3672 based on the next ordinary timer.
3673 TIMER_DIFFERENCE is the distance in time from NOW to when
3674 this timer becomes ripe (negative if it's already ripe). */
3675 if (!NILP (timers))
3677 timer = XCAR (timers);
3678 vector = XVECTOR (timer)->contents;
3679 EMACS_SET_SECS (timer_time,
3680 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3681 EMACS_SET_USECS (timer_time, XINT (vector[3]));
3682 EMACS_SUB_TIME (timer_difference, timer_time, now);
3685 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
3686 based on the next idle timer. */
3687 if (!NILP (idle_timers))
3689 idle_timer = XCAR (idle_timers);
3690 vector = XVECTOR (idle_timer)->contents;
3691 EMACS_SET_SECS (idle_timer_time,
3692 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3693 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
3694 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
3697 /* Decide which timer is the next timer,
3698 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
3699 Also step down the list where we found that timer. */
3701 if (! NILP (timers) && ! NILP (idle_timers))
3703 EMACS_TIME temp;
3704 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
3705 if (EMACS_TIME_NEG_P (temp))
3707 chosen_timer = timer;
3708 timers = XCDR (timers);
3709 difference = timer_difference;
3711 else
3713 chosen_timer = idle_timer;
3714 idle_timers = XCDR (idle_timers);
3715 difference = idle_timer_difference;
3718 else if (! NILP (timers))
3720 chosen_timer = timer;
3721 timers = XCDR (timers);
3722 difference = timer_difference;
3724 else
3726 chosen_timer = idle_timer;
3727 idle_timers = XCDR (idle_timers);
3728 difference = idle_timer_difference;
3730 vector = XVECTOR (chosen_timer)->contents;
3732 /* If timer is ripe, run it if it hasn't been run. */
3733 if (EMACS_TIME_NEG_P (difference)
3734 || (EMACS_SECS (difference) == 0
3735 && EMACS_USECS (difference) == 0))
3737 if (NILP (vector[0]))
3739 int was_locked = single_kboard;
3740 int count = specpdl_ptr - specpdl;
3742 /* Mark the timer as triggered to prevent problems if the lisp
3743 code fails to reschedule it right. */
3744 vector[0] = Qt;
3746 specbind (Qinhibit_quit, Qt);
3748 call1 (Qtimer_event_handler, chosen_timer);
3749 timers_run++;
3751 unbind_to (count, Qnil);
3753 /* Resume allowing input from any kboard, if that was true before. */
3754 if (!was_locked)
3755 any_kboard_state ();
3757 /* Since we have handled the event,
3758 we don't need to tell the caller to wake up and do it. */
3761 else
3762 /* When we encounter a timer that is still waiting,
3763 return the amount of time to wait before it is ripe. */
3765 UNGCPRO;
3766 return difference;
3770 /* No timers are pending in the future. */
3771 /* Return 0 if we generated an event, and -1 if not. */
3772 UNGCPRO;
3773 return nexttime;
3776 /* Caches for modify_event_symbol. */
3777 static Lisp_Object accent_key_syms;
3778 static Lisp_Object func_key_syms;
3779 static Lisp_Object mouse_syms;
3780 #ifdef WINDOWSNT
3781 static Lisp_Object mouse_wheel_syms;
3782 #endif
3783 static Lisp_Object drag_n_drop_syms;
3785 /* This is a list of keysym codes for special "accent" characters.
3786 It parallels lispy_accent_keys. */
3788 static int lispy_accent_codes[] =
3790 #ifdef XK_dead_circumflex
3791 XK_dead_circumflex,
3792 #else
3794 #endif
3795 #ifdef XK_dead_grave
3796 XK_dead_grave,
3797 #else
3799 #endif
3800 #ifdef XK_dead_tilde
3801 XK_dead_tilde,
3802 #else
3804 #endif
3805 #ifdef XK_dead_diaeresis
3806 XK_dead_diaeresis,
3807 #else
3809 #endif
3810 #ifdef XK_dead_macron
3811 XK_dead_macron,
3812 #else
3814 #endif
3815 #ifdef XK_dead_degree
3816 XK_dead_degree,
3817 #else
3819 #endif
3820 #ifdef XK_dead_acute
3821 XK_dead_acute,
3822 #else
3824 #endif
3825 #ifdef XK_dead_cedilla
3826 XK_dead_cedilla,
3827 #else
3829 #endif
3830 #ifdef XK_dead_breve
3831 XK_dead_breve,
3832 #else
3834 #endif
3835 #ifdef XK_dead_ogonek
3836 XK_dead_ogonek,
3837 #else
3839 #endif
3840 #ifdef XK_dead_caron
3841 XK_dead_caron,
3842 #else
3844 #endif
3845 #ifdef XK_dead_doubleacute
3846 XK_dead_doubleacute,
3847 #else
3849 #endif
3850 #ifdef XK_dead_abovedot
3851 XK_dead_abovedot,
3852 #else
3854 #endif
3857 /* This is a list of Lisp names for special "accent" characters.
3858 It parallels lispy_accent_codes. */
3860 static char *lispy_accent_keys[] =
3862 "dead-circumflex",
3863 "dead-grave",
3864 "dead-tilde",
3865 "dead-diaeresis",
3866 "dead-macron",
3867 "dead-degree",
3868 "dead-acute",
3869 "dead-cedilla",
3870 "dead-breve",
3871 "dead-ogonek",
3872 "dead-caron",
3873 "dead-doubleacute",
3874 "dead-abovedot",
3877 #ifdef HAVE_NTGUI
3878 #define FUNCTION_KEY_OFFSET 0x0
3880 char *lispy_function_keys[] =
3882 0, /* 0 */
3884 0, /* VK_LBUTTON 0x01 */
3885 0, /* VK_RBUTTON 0x02 */
3886 "cancel", /* VK_CANCEL 0x03 */
3887 0, /* VK_MBUTTON 0x04 */
3889 0, 0, 0, /* 0x05 .. 0x07 */
3891 "backspace", /* VK_BACK 0x08 */
3892 "tab", /* VK_TAB 0x09 */
3894 0, 0, /* 0x0A .. 0x0B */
3896 "clear", /* VK_CLEAR 0x0C */
3897 "return", /* VK_RETURN 0x0D */
3899 0, 0, /* 0x0E .. 0x0F */
3901 0, /* VK_SHIFT 0x10 */
3902 0, /* VK_CONTROL 0x11 */
3903 0, /* VK_MENU 0x12 */
3904 "pause", /* VK_PAUSE 0x13 */
3905 "capslock", /* VK_CAPITAL 0x14 */
3907 0, 0, 0, 0, 0, 0, /* 0x15 .. 0x1A */
3909 "escape", /* VK_ESCAPE 0x1B */
3911 0, 0, 0, 0, /* 0x1C .. 0x1F */
3913 0, /* VK_SPACE 0x20 */
3914 "prior", /* VK_PRIOR 0x21 */
3915 "next", /* VK_NEXT 0x22 */
3916 "end", /* VK_END 0x23 */
3917 "home", /* VK_HOME 0x24 */
3918 "left", /* VK_LEFT 0x25 */
3919 "up", /* VK_UP 0x26 */
3920 "right", /* VK_RIGHT 0x27 */
3921 "down", /* VK_DOWN 0x28 */
3922 "select", /* VK_SELECT 0x29 */
3923 "print", /* VK_PRINT 0x2A */
3924 "execute", /* VK_EXECUTE 0x2B */
3925 "snapshot", /* VK_SNAPSHOT 0x2C */
3926 "insert", /* VK_INSERT 0x2D */
3927 "delete", /* VK_DELETE 0x2E */
3928 "help", /* VK_HELP 0x2F */
3930 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
3932 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3934 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
3936 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
3938 0, 0, 0, 0, 0, 0, 0, 0, 0,
3939 0, 0, 0, 0, 0, 0, 0, 0, 0,
3940 0, 0, 0, 0, 0, 0, 0, 0,
3942 "lwindow", /* VK_LWIN 0x5B */
3943 "rwindow", /* VK_RWIN 0x5C */
3944 "apps", /* VK_APPS 0x5D */
3946 0, 0, /* 0x5E .. 0x5F */
3948 "kp-0", /* VK_NUMPAD0 0x60 */
3949 "kp-1", /* VK_NUMPAD1 0x61 */
3950 "kp-2", /* VK_NUMPAD2 0x62 */
3951 "kp-3", /* VK_NUMPAD3 0x63 */
3952 "kp-4", /* VK_NUMPAD4 0x64 */
3953 "kp-5", /* VK_NUMPAD5 0x65 */
3954 "kp-6", /* VK_NUMPAD6 0x66 */
3955 "kp-7", /* VK_NUMPAD7 0x67 */
3956 "kp-8", /* VK_NUMPAD8 0x68 */
3957 "kp-9", /* VK_NUMPAD9 0x69 */
3958 "kp-multiply", /* VK_MULTIPLY 0x6A */
3959 "kp-add", /* VK_ADD 0x6B */
3960 "kp-separator", /* VK_SEPARATOR 0x6C */
3961 "kp-subtract", /* VK_SUBTRACT 0x6D */
3962 "kp-decimal", /* VK_DECIMAL 0x6E */
3963 "kp-divide", /* VK_DIVIDE 0x6F */
3964 "f1", /* VK_F1 0x70 */
3965 "f2", /* VK_F2 0x71 */
3966 "f3", /* VK_F3 0x72 */
3967 "f4", /* VK_F4 0x73 */
3968 "f5", /* VK_F5 0x74 */
3969 "f6", /* VK_F6 0x75 */
3970 "f7", /* VK_F7 0x76 */
3971 "f8", /* VK_F8 0x77 */
3972 "f9", /* VK_F9 0x78 */
3973 "f10", /* VK_F10 0x79 */
3974 "f11", /* VK_F11 0x7A */
3975 "f12", /* VK_F12 0x7B */
3976 "f13", /* VK_F13 0x7C */
3977 "f14", /* VK_F14 0x7D */
3978 "f15", /* VK_F15 0x7E */
3979 "f16", /* VK_F16 0x7F */
3980 "f17", /* VK_F17 0x80 */
3981 "f18", /* VK_F18 0x81 */
3982 "f19", /* VK_F19 0x82 */
3983 "f20", /* VK_F20 0x83 */
3984 "f21", /* VK_F21 0x84 */
3985 "f22", /* VK_F22 0x85 */
3986 "f23", /* VK_F23 0x86 */
3987 "f24", /* VK_F24 0x87 */
3989 0, 0, 0, 0, /* 0x88 .. 0x8B */
3990 0, 0, 0, 0, /* 0x8C .. 0x8F */
3992 "kp-numlock", /* VK_NUMLOCK 0x90 */
3993 "scroll", /* VK_SCROLL 0x91 */
3995 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
3996 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
3997 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
3998 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
3999 "kp-end", /* VK_NUMPAD_END 0x96 */
4000 "kp-home", /* VK_NUMPAD_HOME 0x97 */
4001 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
4002 "kp-up", /* VK_NUMPAD_UP 0x99 */
4003 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
4004 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
4005 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
4006 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
4008 0, 0, /* 0x9E .. 0x9F */
4011 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
4012 * Used only as parameters to GetAsyncKeyState and GetKeyState.
4013 * No other API or message will distinguish left and right keys this way.
4015 /* 0xA0 .. 0xEF */
4017 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4018 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4019 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4020 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4021 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4023 /* 0xF0 .. 0xF5 */
4025 0, 0, 0, 0, 0, 0,
4027 "attn", /* VK_ATTN 0xF6 */
4028 "crsel", /* VK_CRSEL 0xF7 */
4029 "exsel", /* VK_EXSEL 0xF8 */
4030 "ereof", /* VK_EREOF 0xF9 */
4031 "play", /* VK_PLAY 0xFA */
4032 "zoom", /* VK_ZOOM 0xFB */
4033 "noname", /* VK_NONAME 0xFC */
4034 "pa1", /* VK_PA1 0xFD */
4035 "oem_clear", /* VK_OEM_CLEAR 0xFE */
4036 0 /* 0xFF */
4039 #else /* not HAVE_NTGUI */
4041 #ifdef XK_kana_A
4042 static char *lispy_kana_keys[] =
4044 /* X Keysym value */
4045 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x400 .. 0x40f */
4046 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x410 .. 0x41f */
4047 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x420 .. 0x42f */
4048 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x430 .. 0x43f */
4049 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x440 .. 0x44f */
4050 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x450 .. 0x45f */
4051 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x460 .. 0x46f */
4052 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0,
4053 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x480 .. 0x48f */
4054 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x490 .. 0x49f */
4055 0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket",
4056 "kana-comma", "kana-conjunctive", "kana-WO", "kana-a",
4057 "kana-i", "kana-u", "kana-e", "kana-o",
4058 "kana-ya", "kana-yu", "kana-yo", "kana-tsu",
4059 "prolongedsound", "kana-A", "kana-I", "kana-U",
4060 "kana-E", "kana-O", "kana-KA", "kana-KI",
4061 "kana-KU", "kana-KE", "kana-KO", "kana-SA",
4062 "kana-SHI", "kana-SU", "kana-SE", "kana-SO",
4063 "kana-TA", "kana-CHI", "kana-TSU", "kana-TE",
4064 "kana-TO", "kana-NA", "kana-NI", "kana-NU",
4065 "kana-NE", "kana-NO", "kana-HA", "kana-HI",
4066 "kana-FU", "kana-HE", "kana-HO", "kana-MA",
4067 "kana-MI", "kana-MU", "kana-ME", "kana-MO",
4068 "kana-YA", "kana-YU", "kana-YO", "kana-RA",
4069 "kana-RI", "kana-RU", "kana-RE", "kana-RO",
4070 "kana-WA", "kana-N", "voicedsound", "semivoicedsound",
4071 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4e0 .. 0x4ef */
4072 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f0 .. 0x4ff */
4074 #endif /* XK_kana_A */
4076 #define FUNCTION_KEY_OFFSET 0xff00
4078 /* You'll notice that this table is arranged to be conveniently
4079 indexed by X Windows keysym values. */
4080 static char *lispy_function_keys[] =
4082 /* X Keysym value */
4084 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00...0f */
4085 "backspace", "tab", "linefeed", "clear",
4086 0, "return", 0, 0,
4087 0, 0, 0, "pause", /* 0xff10...1f */
4088 0, 0, 0, 0, 0, 0, 0, "escape",
4089 0, 0, 0, 0,
4090 0, "kanji", "muhenkan", "henkan", /* 0xff20...2f */
4091 "romaji", "hiragana", "katakana", "hiragana-katakana",
4092 "zenkaku", "hankaku", "zenkaku-hankaku", "touroku",
4093 "massyo", "kana-lock", "kana-shift", "eisu-shift",
4094 "eisu-toggle", /* 0xff30...3f */
4095 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4096 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
4098 "home", "left", "up", "right", /* 0xff50 */ /* IsCursorKey */
4099 "down", "prior", "next", "end",
4100 "begin", 0, 0, 0, 0, 0, 0, 0,
4101 "select", /* 0xff60 */ /* IsMiscFunctionKey */
4102 "print",
4103 "execute",
4104 "insert",
4105 0, /* 0xff64 */
4106 "undo",
4107 "redo",
4108 "menu",
4109 "find",
4110 "cancel",
4111 "help",
4112 "break", /* 0xff6b */
4114 0, 0, 0, 0,
4115 0, 0, 0, 0, "backtab", 0, 0, 0, /* 0xff70... */
4116 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff78... */
4117 "kp-space", /* 0xff80 */ /* IsKeypadKey */
4118 0, 0, 0, 0, 0, 0, 0, 0,
4119 "kp-tab", /* 0xff89 */
4120 0, 0, 0,
4121 "kp-enter", /* 0xff8d */
4122 0, 0, 0,
4123 "kp-f1", /* 0xff91 */
4124 "kp-f2",
4125 "kp-f3",
4126 "kp-f4",
4127 "kp-home", /* 0xff95 */
4128 "kp-left",
4129 "kp-up",
4130 "kp-right",
4131 "kp-down",
4132 "kp-prior", /* kp-page-up */
4133 "kp-next", /* kp-page-down */
4134 "kp-end",
4135 "kp-begin",
4136 "kp-insert",
4137 "kp-delete",
4138 0, /* 0xffa0 */
4139 0, 0, 0, 0, 0, 0, 0, 0, 0,
4140 "kp-multiply", /* 0xffaa */
4141 "kp-add",
4142 "kp-separator",
4143 "kp-subtract",
4144 "kp-decimal",
4145 "kp-divide", /* 0xffaf */
4146 "kp-0", /* 0xffb0 */
4147 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
4148 0, /* 0xffba */
4149 0, 0,
4150 "kp-equal", /* 0xffbd */
4151 "f1", /* 0xffbe */ /* IsFunctionKey */
4152 "f2",
4153 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
4154 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
4155 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
4156 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
4157 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
4158 0, 0, 0, 0, 0, 0, 0, 0,
4159 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
4160 0, 0, 0, 0, 0, 0, 0, "delete"
4163 /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE. */
4164 #define ISO_FUNCTION_KEY_OFFSET 0xfe00
4166 static char *iso_lispy_function_keys[] =
4168 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe00 */
4169 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe08 */
4170 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe10 */
4171 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe18 */
4172 "iso-lefttab", /* 0xfe20 */
4173 "iso-move-line-up", "iso-move-line-down",
4174 "iso-partial-line-up", "iso-partial-line-down",
4175 "iso-partial-space-left", "iso-partial-space-right",
4176 "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */
4177 "iso-release-margin-left", "iso-release-margin-right",
4178 "iso-release-both-margins",
4179 "iso-fast-cursor-left", "iso-fast-cursor-right",
4180 "iso-fast-cursor-up", "iso-fast-cursor-down",
4181 "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */
4182 "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */
4185 #endif /* not HAVE_NTGUI */
4187 Lisp_Object Vlispy_mouse_stem;
4189 #ifdef WINDOWSNT
4190 /* mouse-wheel events are generated by the wheel on devices such as
4191 the MS Intellimouse. The wheel sits in between the left and right
4192 mouse buttons, and is typically used to scroll or zoom the window
4193 underneath the pointer. mouse-wheel events specify the object on
4194 which they operate, and a delta corresponding to the amount and
4195 direction that the wheel is rotated. Clicking the mouse-wheel
4196 generates a mouse-2 event. */
4197 static char *lispy_mouse_wheel_names[] =
4199 "mouse-wheel"
4202 #endif /* WINDOWSNT */
4204 /* drag-n-drop events are generated when a set of selected files are
4205 dragged from another application and dropped onto an Emacs window. */
4206 static char *lispy_drag_n_drop_names[] =
4208 "drag-n-drop"
4211 /* Scroll bar parts. */
4212 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
4213 Lisp_Object Qup, Qdown, Qbottom, Qend_scroll;
4214 Lisp_Object Qtop, Qratio;
4216 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
4217 Lisp_Object *scroll_bar_parts[] = {
4218 &Qabove_handle, &Qhandle, &Qbelow_handle,
4219 &Qup, &Qdown, &Qtop, &Qbottom, &Qend_scroll, &Qratio
4222 /* User signal events. */
4223 Lisp_Object Qusr1_signal, Qusr2_signal;
4225 Lisp_Object *lispy_user_signals[] =
4227 &Qusr1_signal, &Qusr2_signal
4231 /* A vector, indexed by button number, giving the down-going location
4232 of currently depressed buttons, both scroll bar and non-scroll bar.
4234 The elements have the form
4235 (BUTTON-NUMBER MODIFIER-MASK . REST)
4236 where REST is the cdr of a position as it would be reported in the event.
4238 The make_lispy_event function stores positions here to tell the
4239 difference between click and drag events, and to store the starting
4240 location to be included in drag events. */
4242 static Lisp_Object button_down_location;
4244 /* Information about the most recent up-going button event: Which
4245 button, what location, and what time. */
4247 static int last_mouse_button;
4248 static int last_mouse_x;
4249 static int last_mouse_y;
4250 static unsigned long button_down_time;
4252 /* The maximum time between clicks to make a double-click,
4253 or Qnil to disable double-click detection,
4254 or Qt for no time limit. */
4255 Lisp_Object Vdouble_click_time;
4257 /* The number of clicks in this multiple-click. */
4259 int double_click_count;
4261 /* Given a struct input_event, build the lisp event which represents
4262 it. If EVENT is 0, build a mouse movement event from the mouse
4263 movement buffer, which should have a movement event in it.
4265 Note that events must be passed to this function in the order they
4266 are received; this function stores the location of button presses
4267 in order to build drag events when the button is released. */
4269 static Lisp_Object
4270 make_lispy_event (event)
4271 struct input_event *event;
4273 int i;
4275 switch (SWITCH_ENUM_CAST (event->kind))
4277 /* A simple keystroke. */
4278 case ascii_keystroke:
4280 Lisp_Object lispy_c;
4281 int c = event->code & 0377;
4282 /* Turn ASCII characters into control characters
4283 when proper. */
4284 if (event->modifiers & ctrl_modifier)
4285 c = make_ctrl_char (c);
4287 /* Add in the other modifier bits. We took care of ctrl_modifier
4288 just above, and the shift key was taken care of by the X code,
4289 and applied to control characters by make_ctrl_char. */
4290 c |= (event->modifiers
4291 & (meta_modifier | alt_modifier
4292 | hyper_modifier | super_modifier));
4293 /* Distinguish Shift-SPC from SPC. */
4294 if ((event->code & 0377) == 040
4295 && event->modifiers & shift_modifier)
4296 c |= shift_modifier;
4297 button_down_time = 0;
4298 XSETFASTINT (lispy_c, c);
4299 return lispy_c;
4302 /* A function key. The symbol may need to have modifier prefixes
4303 tacked onto it. */
4304 case non_ascii_keystroke:
4305 button_down_time = 0;
4307 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
4308 if (event->code == lispy_accent_codes[i])
4309 return modify_event_symbol (i,
4310 event->modifiers,
4311 Qfunction_key, Qnil,
4312 lispy_accent_keys, &accent_key_syms,
4313 (sizeof (lispy_accent_keys)
4314 / sizeof (lispy_accent_keys[0])));
4316 /* Handle system-specific keysyms. */
4317 if (event->code & (1 << 28))
4319 /* We need to use an alist rather than a vector as the cache
4320 since we can't make a vector long enuf. */
4321 if (NILP (current_kboard->system_key_syms))
4322 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
4323 return modify_event_symbol (event->code,
4324 event->modifiers,
4325 Qfunction_key,
4326 current_kboard->Vsystem_key_alist,
4327 0, &current_kboard->system_key_syms,
4328 (unsigned)-1);
4331 #ifdef XK_kana_A
4332 if (event->code >= 0x400 && event->code < 0x500)
4333 return modify_event_symbol (event->code - 0x400,
4334 event->modifiers & ~shift_modifier,
4335 Qfunction_key, Qnil,
4336 lispy_kana_keys, &func_key_syms,
4337 (sizeof (lispy_kana_keys)
4338 / sizeof (lispy_kana_keys[0])));
4339 #endif /* XK_kana_A */
4341 #ifdef ISO_FUNCTION_KEY_OFFSET
4342 if (event->code < FUNCTION_KEY_OFFSET
4343 && event->code >= ISO_FUNCTION_KEY_OFFSET)
4344 return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET,
4345 event->modifiers,
4346 Qfunction_key, Qnil,
4347 iso_lispy_function_keys, &func_key_syms,
4348 (sizeof (iso_lispy_function_keys)
4349 / sizeof (iso_lispy_function_keys[0])));
4350 else
4351 #endif
4352 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
4353 event->modifiers,
4354 Qfunction_key, Qnil,
4355 lispy_function_keys, &func_key_syms,
4356 (sizeof (lispy_function_keys)
4357 / sizeof (lispy_function_keys[0])));
4359 #ifdef HAVE_MOUSE
4360 /* A mouse click. Figure out where it is, decide whether it's
4361 a press, click or drag, and build the appropriate structure. */
4362 case mouse_click:
4363 #ifndef USE_TOOLKIT_SCROLL_BARS
4364 case scroll_bar_click:
4365 #endif
4367 int button = event->code;
4368 int is_double;
4369 Lisp_Object position;
4370 Lisp_Object *start_pos_ptr;
4371 Lisp_Object start_pos;
4373 /* Build the position as appropriate for this mouse click. */
4374 if (event->kind == mouse_click)
4376 int part;
4377 FRAME_PTR f = XFRAME (event->frame_or_window);
4378 Lisp_Object window;
4379 Lisp_Object posn;
4380 Lisp_Object string_info = Qnil;
4381 int row, column;
4383 /* Ignore mouse events that were made on frame that
4384 have been deleted. */
4385 if (! FRAME_LIVE_P (f))
4386 return Qnil;
4388 /* EVENT->x and EVENT->y are frame-relative pixel
4389 coordinates at this place. Under old redisplay, COLUMN
4390 and ROW are set to frame relative glyph coordinates
4391 which are then used to determine whether this click is
4392 in a menu (non-toolkit version). */
4393 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4394 &column, &row, NULL, 1);
4396 #ifndef USE_X_TOOLKIT
4397 /* In the non-toolkit version, clicks on the menu bar
4398 are ordinary button events in the event buffer.
4399 Distinguish them, and invoke the menu.
4401 (In the toolkit version, the toolkit handles the menu bar
4402 and Emacs doesn't know about it until after the user
4403 makes a selection.) */
4404 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
4405 && (event->modifiers & down_modifier))
4407 Lisp_Object items, item;
4408 int hpos;
4409 int i;
4411 #if 0
4412 /* Activate the menu bar on the down event. If the
4413 up event comes in before the menu code can deal with it,
4414 just ignore it. */
4415 if (! (event->modifiers & down_modifier))
4416 return Qnil;
4417 #endif
4419 /* Find the menu bar item under `column'. */
4420 item = Qnil;
4421 items = FRAME_MENU_BAR_ITEMS (f);
4422 for (i = 0; i < XVECTOR (items)->size; i += 4)
4424 Lisp_Object pos, string;
4425 string = XVECTOR (items)->contents[i + 1];
4426 pos = XVECTOR (items)->contents[i + 3];
4427 if (NILP (string))
4428 break;
4429 if (column >= XINT (pos)
4430 && column < XINT (pos) + XSTRING (string)->size)
4432 item = XVECTOR (items)->contents[i];
4433 break;
4437 /* ELisp manual 2.4b says (x y) are window relative but
4438 code says they are frame-relative. */
4439 position
4440 = Fcons (event->frame_or_window,
4441 Fcons (Qmenu_bar,
4442 Fcons (Fcons (event->x, event->y),
4443 Fcons (make_number (event->timestamp),
4444 Qnil))));
4446 return Fcons (item, Fcons (position, Qnil));
4448 #endif /* not USE_X_TOOLKIT */
4450 /* Set `window' to the window under frame pixel coordinates
4451 event->x/event->y. */
4452 window = window_from_coordinates (f, XINT (event->x),
4453 XINT (event->y), &part, 0);
4455 if (!WINDOWP (window))
4457 window = event->frame_or_window;
4458 posn = Qnil;
4460 else
4462 /* It's a click in window window at frame coordinates
4463 event->x/ event->y. */
4464 struct window *w = XWINDOW (window);
4466 /* Get window relative coordinates. Original code
4467 `rounded' this to glyph boundaries. */
4468 int wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (event->x));
4469 int wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (event->y));
4471 /* Set event coordinates to window-relative coordinates
4472 for constructing the Lisp event below. */
4473 XSETINT (event->x, wx);
4474 XSETINT (event->y, wy);
4476 if (part == 1 || part == 3)
4478 /* Mode line or top line. Look for a string under
4479 the mouse that may have a `local-map' property. */
4480 Lisp_Object string;
4481 int charpos;
4483 posn = part == 1 ? Qmode_line : Qheader_line;
4484 string = mode_line_string (w, wx, wy, part == 1, &charpos);
4485 if (STRINGP (string))
4486 string_info = Fcons (string, make_number (charpos));
4488 else if (part == 2)
4489 posn = Qvertical_line;
4490 else
4491 XSETINT (posn, buffer_posn_from_coords (w, &wx, &wy));
4494 position
4495 = Fcons (window,
4496 Fcons (posn,
4497 Fcons (Fcons (event->x, event->y),
4498 Fcons (make_number (event->timestamp),
4499 (NILP (string_info)
4500 ? Qnil
4501 : Fcons (string_info, Qnil))))));
4503 #ifndef USE_TOOLKIT_SCROLL_BARS
4504 else
4506 /* It's a scrollbar click. */
4507 Lisp_Object window;
4508 Lisp_Object portion_whole;
4509 Lisp_Object part;
4511 window = event->frame_or_window;
4512 portion_whole = Fcons (event->x, event->y);
4513 part = *scroll_bar_parts[(int) event->part];
4515 position
4516 = Fcons (window,
4517 Fcons (Qvertical_scroll_bar,
4518 Fcons (portion_whole,
4519 Fcons (make_number (event->timestamp),
4520 Fcons (part, Qnil)))));
4522 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4524 if (button >= XVECTOR (button_down_location)->size)
4526 button_down_location = larger_vector (button_down_location,
4527 button + 1, Qnil);
4528 mouse_syms = larger_vector (mouse_syms, button + 1, Qnil);
4531 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
4533 start_pos = *start_pos_ptr;
4534 *start_pos_ptr = Qnil;
4536 is_double = (button == last_mouse_button
4537 && XINT (event->x) == last_mouse_x
4538 && XINT (event->y) == last_mouse_y
4539 && button_down_time != 0
4540 && (EQ (Vdouble_click_time, Qt)
4541 || (INTEGERP (Vdouble_click_time)
4542 && ((int)(event->timestamp - button_down_time)
4543 < XINT (Vdouble_click_time)))));
4544 last_mouse_button = button;
4545 last_mouse_x = XINT (event->x);
4546 last_mouse_y = XINT (event->y);
4548 /* If this is a button press, squirrel away the location, so
4549 we can decide later whether it was a click or a drag. */
4550 if (event->modifiers & down_modifier)
4552 if (is_double)
4554 double_click_count++;
4555 event->modifiers |= ((double_click_count > 2)
4556 ? triple_modifier
4557 : double_modifier);
4559 else
4560 double_click_count = 1;
4561 button_down_time = event->timestamp;
4562 *start_pos_ptr = Fcopy_alist (position);
4565 /* Now we're releasing a button - check the co-ordinates to
4566 see if this was a click or a drag. */
4567 else if (event->modifiers & up_modifier)
4569 /* If we did not see a down before this up,
4570 ignore the up. Probably this happened because
4571 the down event chose a menu item.
4572 It would be an annoyance to treat the release
4573 of the button that chose the menu item
4574 as a separate event. */
4576 if (!CONSP (start_pos))
4577 return Qnil;
4579 event->modifiers &= ~up_modifier;
4580 #if 0 /* Formerly we treated an up with no down as a click event. */
4581 if (!CONSP (start_pos))
4582 event->modifiers |= click_modifier;
4583 else
4584 #endif
4586 /* The third element of every position should be the (x,y)
4587 pair. */
4588 Lisp_Object down;
4590 down = Fnth (make_number (2), start_pos);
4591 if (EQ (event->x, XCAR (down))
4592 && EQ (event->y, XCDR (down)))
4594 event->modifiers |= click_modifier;
4596 else
4598 button_down_time = 0;
4599 event->modifiers |= drag_modifier;
4601 /* Don't check is_double; treat this as multiple
4602 if the down-event was multiple. */
4603 if (double_click_count > 1)
4604 event->modifiers |= ((double_click_count > 2)
4605 ? triple_modifier
4606 : double_modifier);
4609 else
4610 /* Every mouse event should either have the down_modifier or
4611 the up_modifier set. */
4612 abort ();
4615 /* Get the symbol we should use for the mouse click. */
4616 Lisp_Object head;
4618 head = modify_event_symbol (button,
4619 event->modifiers,
4620 Qmouse_click, Vlispy_mouse_stem,
4621 NULL,
4622 &mouse_syms,
4623 XVECTOR (mouse_syms)->size);
4624 if (event->modifiers & drag_modifier)
4625 return Fcons (head,
4626 Fcons (start_pos,
4627 Fcons (position,
4628 Qnil)));
4629 else if (event->modifiers & (double_modifier | triple_modifier))
4630 return Fcons (head,
4631 Fcons (position,
4632 Fcons (make_number (double_click_count),
4633 Qnil)));
4634 else
4635 return Fcons (head,
4636 Fcons (position,
4637 Qnil));
4641 #if USE_TOOLKIT_SCROLL_BARS
4643 /* We don't have down and up events if using toolkit scroll bars,
4644 so make this always a click event. Store in the `part' of
4645 the Lisp event a symbol which maps to the following actions:
4647 `above_handle' page up
4648 `below_handle' page down
4649 `up' line up
4650 `down' line down
4651 `top' top of buffer
4652 `bottom' bottom of buffer
4653 `handle' thumb has been dragged.
4654 `end-scroll' end of interaction with scroll bar
4656 The incoming input_event contains in its `part' member an
4657 index of type `enum scroll_bar_part' which we can use as an
4658 index in scroll_bar_parts to get the appropriate symbol. */
4660 case scroll_bar_click:
4662 Lisp_Object position, head, window, portion_whole, part;
4664 window = event->frame_or_window;
4665 portion_whole = Fcons (event->x, event->y);
4666 part = *scroll_bar_parts[(int) event->part];
4668 position
4669 = Fcons (window,
4670 Fcons (Qvertical_scroll_bar,
4671 Fcons (portion_whole,
4672 Fcons (make_number (event->timestamp),
4673 Fcons (part, Qnil)))));
4675 /* Always treat scroll bar events as clicks. */
4676 event->modifiers |= click_modifier;
4678 /* Get the symbol we should use for the mouse click. */
4679 head = modify_event_symbol (event->code,
4680 event->modifiers,
4681 Qmouse_click,
4682 Vlispy_mouse_stem,
4683 NULL, &mouse_syms,
4684 XVECTOR (mouse_syms)->size);
4685 return Fcons (head, Fcons (position, Qnil));
4688 #endif /* USE_TOOLKIT_SCROLL_BARS */
4690 #ifdef WINDOWSNT
4691 case w32_scroll_bar_click:
4693 int button = event->code;
4694 int is_double;
4695 Lisp_Object position;
4696 Lisp_Object *start_pos_ptr;
4697 Lisp_Object start_pos;
4700 Lisp_Object window;
4701 Lisp_Object portion_whole;
4702 Lisp_Object part;
4704 window = event->frame_or_window;
4705 portion_whole = Fcons (event->x, event->y);
4706 part = *scroll_bar_parts[(int) event->part];
4708 position
4709 = Fcons (window,
4710 Fcons (Qvertical_scroll_bar,
4711 Fcons (portion_whole,
4712 Fcons (make_number (event->timestamp),
4713 Fcons (part, Qnil)))));
4716 /* Always treat W32 scroll bar events as clicks. */
4717 event->modifiers |= click_modifier;
4720 /* Get the symbol we should use for the mouse click. */
4721 Lisp_Object head;
4723 head = modify_event_symbol (button,
4724 event->modifiers,
4725 Qmouse_click,
4726 Vlispy_mouse_stem,
4727 NULL, &mouse_syms,
4728 XVECTOR (mouse_syms)->size);
4729 return Fcons (head,
4730 Fcons (position,
4731 Qnil));
4734 case mouse_wheel:
4736 int part;
4737 FRAME_PTR f = XFRAME (event->frame_or_window);
4738 Lisp_Object window;
4739 Lisp_Object posn;
4740 Lisp_Object head, position;
4741 int row, column;
4743 /* Ignore mouse events that were made on frame that
4744 have been deleted. */
4745 if (! FRAME_LIVE_P (f))
4746 return Qnil;
4747 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4748 &column, &row, NULL, 1);
4749 window = window_from_coordinates (f, column, row, &part, 0);
4751 if (!WINDOWP (window))
4753 window = event->frame_or_window;
4754 posn = Qnil;
4756 else
4758 int pixcolumn, pixrow;
4759 column -= XINT (XWINDOW (window)->left);
4760 row -= XINT (XWINDOW (window)->top);
4761 glyph_to_pixel_coords (XWINDOW(window), column, row,
4762 &pixcolumn, &pixrow);
4763 XSETINT (event->x, pixcolumn);
4764 XSETINT (event->y, pixrow);
4766 if (part == 1)
4767 posn = Qmode_line;
4768 else if (part == 2)
4769 posn = Qvertical_line;
4770 else if (part == 3)
4771 posn = Qheader_line;
4772 else
4773 XSETINT (posn,
4774 buffer_posn_from_coords (XWINDOW (window),
4775 &column, &row));
4779 Lisp_Object head, position;
4781 position
4782 = Fcons (window,
4783 Fcons (posn,
4784 Fcons (Fcons (event->x, event->y),
4785 Fcons (make_number (event->timestamp),
4786 Qnil))));
4788 head = modify_event_symbol (0, event->modifiers,
4789 Qmouse_wheel, Qnil,
4790 lispy_mouse_wheel_names,
4791 &mouse_wheel_syms, 1);
4792 return Fcons (head,
4793 Fcons (position,
4794 Fcons (make_number (event->code),
4795 Qnil)));
4798 #endif /* WINDOWSNT */
4800 case drag_n_drop:
4802 int part;
4803 FRAME_PTR f;
4804 Lisp_Object window;
4805 Lisp_Object posn;
4806 Lisp_Object files;
4807 int row, column;
4809 /* The frame_or_window field should be a cons of the frame in
4810 which the event occurred and a list of the filenames
4811 dropped. */
4812 if (! CONSP (event->frame_or_window))
4813 abort ();
4815 f = XFRAME (XCAR (event->frame_or_window));
4816 files = XCDR (event->frame_or_window);
4818 /* Ignore mouse events that were made on frames that
4819 have been deleted. */
4820 if (! FRAME_LIVE_P (f))
4821 return Qnil;
4822 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4823 &column, &row, NULL, 1);
4824 window = window_from_coordinates (f, column, row, &part, 0);
4826 if (!WINDOWP (window))
4828 window = XCAR (event->frame_or_window);
4829 posn = Qnil;
4831 else
4833 /* It's an event in window `window' at frame coordinates
4834 event->x/ event->y. */
4835 struct window *w = XWINDOW (window);
4837 /* Get window relative coordinates. */
4838 int wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (event->x));
4839 int wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (event->y));
4841 /* Set event coordinates to window-relative coordinates
4842 for constructing the Lisp event below. */
4843 XSETINT (event->x, wx);
4844 XSETINT (event->y, wy);
4846 if (part == 1)
4847 posn = Qmode_line;
4848 else if (part == 2)
4849 posn = Qvertical_line;
4850 else if (part == 3)
4851 posn = Qheader_line;
4852 else
4853 XSETINT (posn, buffer_posn_from_coords (w, &wx, &wy));
4857 Lisp_Object head, position;
4859 position
4860 = Fcons (window,
4861 Fcons (posn,
4862 Fcons (Fcons (event->x, event->y),
4863 Fcons (make_number (event->timestamp),
4864 Qnil))));
4866 head = modify_event_symbol (0, event->modifiers,
4867 Qdrag_n_drop, Qnil,
4868 lispy_drag_n_drop_names,
4869 &drag_n_drop_syms, 1);
4870 return Fcons (head,
4871 Fcons (position,
4872 Fcons (files,
4873 Qnil)));
4876 #endif /* HAVE_MOUSE */
4878 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
4879 case MENU_BAR_EVENT:
4880 if (EQ (event->arg, event->frame_or_window))
4881 /* This is the prefix key. We translate this to
4882 `(menu_bar)' because the code in keyboard.c for menu
4883 events, which we use, relies on this. */
4884 return Fcons (Qmenu_bar, Qnil);
4885 return event->arg;
4886 #endif
4888 case TOOL_BAR_EVENT:
4889 if (EQ (event->arg, event->frame_or_window))
4890 /* This is the prefix key. We translate this to
4891 `(tool_bar)' because the code in keyboard.c for menu
4892 events, which we use, relies on this. */
4893 return Fcons (Qtool_bar, Qnil);
4894 else if (SYMBOLP (event->arg))
4895 return apply_modifiers (event->modifiers, event->arg);
4896 return event->arg;
4898 case USER_SIGNAL_EVENT:
4899 /* A user signal. */
4900 return *lispy_user_signals[event->code];
4902 /* The 'kind' field of the event is something we don't recognize. */
4903 default:
4904 abort ();
4908 #ifdef HAVE_MOUSE
4910 static Lisp_Object
4911 make_lispy_movement (frame, bar_window, part, x, y, time)
4912 FRAME_PTR frame;
4913 Lisp_Object bar_window;
4914 enum scroll_bar_part part;
4915 Lisp_Object x, y;
4916 unsigned long time;
4918 /* Is it a scroll bar movement? */
4919 if (frame && ! NILP (bar_window))
4921 Lisp_Object part_sym;
4923 part_sym = *scroll_bar_parts[(int) part];
4924 return Fcons (Qscroll_bar_movement,
4925 (Fcons (Fcons (bar_window,
4926 Fcons (Qvertical_scroll_bar,
4927 Fcons (Fcons (x, y),
4928 Fcons (make_number (time),
4929 Fcons (part_sym,
4930 Qnil))))),
4931 Qnil)));
4934 /* Or is it an ordinary mouse movement? */
4935 else
4937 int area;
4938 Lisp_Object window;
4939 Lisp_Object posn;
4941 if (frame)
4942 /* It's in a frame; which window on that frame? */
4943 window = window_from_coordinates (frame, XINT (x), XINT (y), &area, 0);
4944 else
4945 window = Qnil;
4947 if (WINDOWP (window))
4949 struct window *w = XWINDOW (window);
4950 int wx, wy;
4952 /* Get window relative coordinates. */
4953 wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (x));
4954 wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (y));
4955 XSETINT (x, wx);
4956 XSETINT (y, wy);
4958 if (area == 1)
4959 posn = Qmode_line;
4960 else if (area == 2)
4961 posn = Qvertical_line;
4962 else if (area == 3)
4963 posn = Qheader_line;
4964 else
4965 XSETINT (posn, buffer_posn_from_coords (w, &wx, &wy));
4967 else if (frame != 0)
4969 XSETFRAME (window, frame);
4970 posn = Qnil;
4972 else
4974 window = Qnil;
4975 posn = Qnil;
4976 XSETFASTINT (x, 0);
4977 XSETFASTINT (y, 0);
4980 return Fcons (Qmouse_movement,
4981 Fcons (Fcons (window,
4982 Fcons (posn,
4983 Fcons (Fcons (x, y),
4984 Fcons (make_number (time),
4985 Qnil)))),
4986 Qnil));
4990 #endif /* HAVE_MOUSE */
4992 /* Construct a switch frame event. */
4993 static Lisp_Object
4994 make_lispy_switch_frame (frame)
4995 Lisp_Object frame;
4997 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
5000 /* Manipulating modifiers. */
5002 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
5004 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
5005 SYMBOL's name of the end of the modifiers; the string from this
5006 position is the unmodified symbol name.
5008 This doesn't use any caches. */
5010 static int
5011 parse_modifiers_uncached (symbol, modifier_end)
5012 Lisp_Object symbol;
5013 int *modifier_end;
5015 struct Lisp_String *name;
5016 int i;
5017 int modifiers;
5019 CHECK_SYMBOL (symbol, 1);
5021 modifiers = 0;
5022 name = XSYMBOL (symbol)->name;
5024 for (i = 0; i+2 <= STRING_BYTES (name); )
5026 int this_mod_end = 0;
5027 int this_mod = 0;
5029 /* See if the name continues with a modifier word.
5030 Check that the word appears, but don't check what follows it.
5031 Set this_mod and this_mod_end to record what we find. */
5033 switch (name->data[i])
5035 #define SINGLE_LETTER_MOD(BIT) \
5036 (this_mod_end = i + 1, this_mod = BIT)
5038 case 'A':
5039 SINGLE_LETTER_MOD (alt_modifier);
5040 break;
5042 case 'C':
5043 SINGLE_LETTER_MOD (ctrl_modifier);
5044 break;
5046 case 'H':
5047 SINGLE_LETTER_MOD (hyper_modifier);
5048 break;
5050 case 'M':
5051 SINGLE_LETTER_MOD (meta_modifier);
5052 break;
5054 case 'S':
5055 SINGLE_LETTER_MOD (shift_modifier);
5056 break;
5058 case 's':
5059 SINGLE_LETTER_MOD (super_modifier);
5060 break;
5062 #undef SINGLE_LETTER_MOD
5065 /* If we found no modifier, stop looking for them. */
5066 if (this_mod_end == 0)
5067 break;
5069 /* Check there is a dash after the modifier, so that it
5070 really is a modifier. */
5071 if (this_mod_end >= STRING_BYTES (name)
5072 || name->data[this_mod_end] != '-')
5073 break;
5075 /* This modifier is real; look for another. */
5076 modifiers |= this_mod;
5077 i = this_mod_end + 1;
5080 /* Should we include the `click' modifier? */
5081 if (! (modifiers & (down_modifier | drag_modifier
5082 | double_modifier | triple_modifier))
5083 && i + 7 == STRING_BYTES (name)
5084 && strncmp (name->data + i, "mouse-", 6) == 0
5085 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
5086 modifiers |= click_modifier;
5088 if (modifier_end)
5089 *modifier_end = i;
5091 return modifiers;
5094 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
5095 prepended to the string BASE[0..BASE_LEN-1].
5096 This doesn't use any caches. */
5097 static Lisp_Object
5098 apply_modifiers_uncached (modifiers, base, base_len, base_len_byte)
5099 int modifiers;
5100 char *base;
5101 int base_len, base_len_byte;
5103 /* Since BASE could contain nulls, we can't use intern here; we have
5104 to use Fintern, which expects a genuine Lisp_String, and keeps a
5105 reference to it. */
5106 char *new_mods
5107 = (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
5108 int mod_len;
5111 char *p = new_mods;
5113 /* Only the event queue may use the `up' modifier; it should always
5114 be turned into a click or drag event before presented to lisp code. */
5115 if (modifiers & up_modifier)
5116 abort ();
5118 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
5119 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
5120 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
5121 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
5122 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
5123 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
5124 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
5125 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
5126 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
5127 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
5128 /* The click modifier is denoted by the absence of other modifiers. */
5130 *p = '\0';
5132 mod_len = p - new_mods;
5136 Lisp_Object new_name;
5138 new_name = make_uninit_multibyte_string (mod_len + base_len,
5139 mod_len + base_len_byte);
5140 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
5141 bcopy (base, XSTRING (new_name)->data + mod_len, base_len_byte);
5143 return Fintern (new_name, Qnil);
5148 static char *modifier_names[] =
5150 "up", "down", "drag", "click", "double", "triple", 0, 0,
5151 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5152 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
5154 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
5156 static Lisp_Object modifier_symbols;
5158 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
5159 static Lisp_Object
5160 lispy_modifier_list (modifiers)
5161 int modifiers;
5163 Lisp_Object modifier_list;
5164 int i;
5166 modifier_list = Qnil;
5167 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
5168 if (modifiers & (1<<i))
5169 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
5170 modifier_list);
5172 return modifier_list;
5176 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
5177 where UNMODIFIED is the unmodified form of SYMBOL,
5178 MASK is the set of modifiers present in SYMBOL's name.
5179 This is similar to parse_modifiers_uncached, but uses the cache in
5180 SYMBOL's Qevent_symbol_element_mask property, and maintains the
5181 Qevent_symbol_elements property. */
5183 Lisp_Object
5184 parse_modifiers (symbol)
5185 Lisp_Object symbol;
5187 Lisp_Object elements;
5189 elements = Fget (symbol, Qevent_symbol_element_mask);
5190 if (CONSP (elements))
5191 return elements;
5192 else
5194 int end;
5195 int modifiers = parse_modifiers_uncached (symbol, &end);
5196 Lisp_Object unmodified;
5197 Lisp_Object mask;
5199 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
5200 STRING_BYTES (XSYMBOL (symbol)->name) - end),
5201 Qnil);
5203 if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1))
5204 abort ();
5205 XSETFASTINT (mask, modifiers);
5206 elements = Fcons (unmodified, Fcons (mask, Qnil));
5208 /* Cache the parsing results on SYMBOL. */
5209 Fput (symbol, Qevent_symbol_element_mask,
5210 elements);
5211 Fput (symbol, Qevent_symbol_elements,
5212 Fcons (unmodified, lispy_modifier_list (modifiers)));
5214 /* Since we know that SYMBOL is modifiers applied to unmodified,
5215 it would be nice to put that in unmodified's cache.
5216 But we can't, since we're not sure that parse_modifiers is
5217 canonical. */
5219 return elements;
5223 /* Apply the modifiers MODIFIERS to the symbol BASE.
5224 BASE must be unmodified.
5226 This is like apply_modifiers_uncached, but uses BASE's
5227 Qmodifier_cache property, if present. It also builds
5228 Qevent_symbol_elements properties, since it has that info anyway.
5230 apply_modifiers copies the value of BASE's Qevent_kind property to
5231 the modified symbol. */
5232 static Lisp_Object
5233 apply_modifiers (modifiers, base)
5234 int modifiers;
5235 Lisp_Object base;
5237 Lisp_Object cache, index, entry, new_symbol;
5239 /* Mask out upper bits. We don't know where this value's been. */
5240 modifiers &= ((EMACS_INT)1 << VALBITS) - 1;
5242 /* The click modifier never figures into cache indices. */
5243 cache = Fget (base, Qmodifier_cache);
5244 XSETFASTINT (index, (modifiers & ~click_modifier));
5245 entry = assq_no_quit (index, cache);
5247 if (CONSP (entry))
5248 new_symbol = XCDR (entry);
5249 else
5251 /* We have to create the symbol ourselves. */
5252 new_symbol = apply_modifiers_uncached (modifiers,
5253 XSYMBOL (base)->name->data,
5254 XSYMBOL (base)->name->size,
5255 STRING_BYTES (XSYMBOL (base)->name));
5257 /* Add the new symbol to the base's cache. */
5258 entry = Fcons (index, new_symbol);
5259 Fput (base, Qmodifier_cache, Fcons (entry, cache));
5261 /* We have the parsing info now for free, so add it to the caches. */
5262 XSETFASTINT (index, modifiers);
5263 Fput (new_symbol, Qevent_symbol_element_mask,
5264 Fcons (base, Fcons (index, Qnil)));
5265 Fput (new_symbol, Qevent_symbol_elements,
5266 Fcons (base, lispy_modifier_list (modifiers)));
5269 /* Make sure this symbol is of the same kind as BASE.
5271 You'd think we could just set this once and for all when we
5272 intern the symbol above, but reorder_modifiers may call us when
5273 BASE's property isn't set right; we can't assume that just
5274 because it has a Qmodifier_cache property it must have its
5275 Qevent_kind set right as well. */
5276 if (NILP (Fget (new_symbol, Qevent_kind)))
5278 Lisp_Object kind;
5280 kind = Fget (base, Qevent_kind);
5281 if (! NILP (kind))
5282 Fput (new_symbol, Qevent_kind, kind);
5285 return new_symbol;
5289 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
5290 return a symbol with the modifiers placed in the canonical order.
5291 Canonical order is alphabetical, except for down and drag, which
5292 always come last. The 'click' modifier is never written out.
5294 Fdefine_key calls this to make sure that (for example) C-M-foo
5295 and M-C-foo end up being equivalent in the keymap. */
5297 Lisp_Object
5298 reorder_modifiers (symbol)
5299 Lisp_Object symbol;
5301 /* It's hopefully okay to write the code this way, since everything
5302 will soon be in caches, and no consing will be done at all. */
5303 Lisp_Object parsed;
5305 parsed = parse_modifiers (symbol);
5306 return apply_modifiers ((int) XINT (XCAR (XCDR (parsed))),
5307 XCAR (parsed));
5311 /* For handling events, we often want to produce a symbol whose name
5312 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
5313 to some base, like the name of a function key or mouse button.
5314 modify_event_symbol produces symbols of this sort.
5316 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
5317 is the name of the i'th symbol. TABLE_SIZE is the number of elements
5318 in the table.
5320 Alternatively, NAME_ALIST_OR_STEM is either an alist mapping codes
5321 into symbol names, or a string specifying a name stem used to
5322 contruct a symbol name or the form `STEM-N', where N is the decimal
5323 representation of SYMBOL_NUM. NAME_ALIST_OR_STEM is used if it is
5324 non-nil; otherwise NAME_TABLE is used.
5326 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
5327 persist between calls to modify_event_symbol that it can use to
5328 store a cache of the symbols it's generated for this NAME_TABLE
5329 before. The object stored there may be a vector or an alist.
5331 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
5333 MODIFIERS is a set of modifier bits (as given in struct input_events)
5334 whose prefixes should be applied to the symbol name.
5336 SYMBOL_KIND is the value to be placed in the event_kind property of
5337 the returned symbol.
5339 The symbols we create are supposed to have an
5340 `event-symbol-elements' property, which lists the modifiers present
5341 in the symbol's name. */
5343 static Lisp_Object
5344 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist_or_stem,
5345 name_table, symbol_table, table_size)
5346 int symbol_num;
5347 unsigned modifiers;
5348 Lisp_Object symbol_kind;
5349 Lisp_Object name_alist_or_stem;
5350 char **name_table;
5351 Lisp_Object *symbol_table;
5352 unsigned int table_size;
5354 Lisp_Object value;
5355 Lisp_Object symbol_int;
5357 /* Get rid of the "vendor-specific" bit here. */
5358 XSETINT (symbol_int, symbol_num & 0xffffff);
5360 /* Is this a request for a valid symbol? */
5361 if (symbol_num < 0 || symbol_num >= table_size)
5362 return Qnil;
5364 if (CONSP (*symbol_table))
5365 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
5367 /* If *symbol_table doesn't seem to be initialized properly, fix that.
5368 *symbol_table should be a lisp vector TABLE_SIZE elements long,
5369 where the Nth element is the symbol for NAME_TABLE[N], or nil if
5370 we've never used that symbol before. */
5371 else
5373 if (! VECTORP (*symbol_table)
5374 || XVECTOR (*symbol_table)->size != table_size)
5376 Lisp_Object size;
5378 XSETFASTINT (size, table_size);
5379 *symbol_table = Fmake_vector (size, Qnil);
5382 value = XVECTOR (*symbol_table)->contents[symbol_num];
5385 /* Have we already used this symbol before? */
5386 if (NILP (value))
5388 /* No; let's create it. */
5389 if (CONSP (name_alist_or_stem))
5390 value = Fcdr_safe (Fassq (symbol_int, name_alist_or_stem));
5391 else if (STRINGP (name_alist_or_stem))
5393 int len = STRING_BYTES (XSTRING (name_alist_or_stem));
5394 char *buf = (char *) alloca (len + 50);
5395 sprintf (buf, "%s-%d", XSTRING (name_alist_or_stem)->data,
5396 XINT (symbol_int) + 1);
5397 value = intern (buf);
5399 else if (name_table != 0 && name_table[symbol_num])
5400 value = intern (name_table[symbol_num]);
5402 #ifdef HAVE_WINDOW_SYSTEM
5403 if (NILP (value))
5405 char *name = x_get_keysym_name (symbol_num);
5406 if (name)
5407 value = intern (name);
5409 #endif
5411 if (NILP (value))
5413 char buf[20];
5414 sprintf (buf, "key-%d", symbol_num);
5415 value = intern (buf);
5418 if (CONSP (*symbol_table))
5419 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
5420 else
5421 XVECTOR (*symbol_table)->contents[symbol_num] = value;
5423 /* Fill in the cache entries for this symbol; this also
5424 builds the Qevent_symbol_elements property, which the user
5425 cares about. */
5426 apply_modifiers (modifiers & click_modifier, value);
5427 Fput (value, Qevent_kind, symbol_kind);
5430 /* Apply modifiers to that symbol. */
5431 return apply_modifiers (modifiers, value);
5434 /* Convert a list that represents an event type,
5435 such as (ctrl meta backspace), into the usual representation of that
5436 event type as a number or a symbol. */
5438 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
5439 "Convert the event description list EVENT-DESC to an event type.\n\
5440 EVENT-DESC should contain one base event type (a character or symbol)\n\
5441 and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\
5442 drag, down, double or triple). The base must be last.\n\
5443 The return value is an event type (a character or symbol) which\n\
5444 has the same base event type and all the specified modifiers.")
5445 (event_desc)
5446 Lisp_Object event_desc;
5448 Lisp_Object base;
5449 int modifiers = 0;
5450 Lisp_Object rest;
5452 base = Qnil;
5453 rest = event_desc;
5454 while (CONSP (rest))
5456 Lisp_Object elt;
5457 int this = 0;
5459 elt = XCAR (rest);
5460 rest = XCDR (rest);
5462 /* Given a symbol, see if it is a modifier name. */
5463 if (SYMBOLP (elt) && CONSP (rest))
5464 this = parse_solitary_modifier (elt);
5466 if (this != 0)
5467 modifiers |= this;
5468 else if (!NILP (base))
5469 error ("Two bases given in one event");
5470 else
5471 base = elt;
5475 /* Let the symbol A refer to the character A. */
5476 if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1)
5477 XSETINT (base, XSYMBOL (base)->name->data[0]);
5479 if (INTEGERP (base))
5481 /* Turn (shift a) into A. */
5482 if ((modifiers & shift_modifier) != 0
5483 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
5485 XSETINT (base, XINT (base) - ('a' - 'A'));
5486 modifiers &= ~shift_modifier;
5489 /* Turn (control a) into C-a. */
5490 if (modifiers & ctrl_modifier)
5491 return make_number ((modifiers & ~ctrl_modifier)
5492 | make_ctrl_char (XINT (base)));
5493 else
5494 return make_number (modifiers | XINT (base));
5496 else if (SYMBOLP (base))
5497 return apply_modifiers (modifiers, base);
5498 else
5499 error ("Invalid base event");
5502 /* Try to recognize SYMBOL as a modifier name.
5503 Return the modifier flag bit, or 0 if not recognized. */
5505 static int
5506 parse_solitary_modifier (symbol)
5507 Lisp_Object symbol;
5509 struct Lisp_String *name = XSYMBOL (symbol)->name;
5511 switch (name->data[0])
5513 #define SINGLE_LETTER_MOD(BIT) \
5514 if (STRING_BYTES (name) == 1) \
5515 return BIT;
5517 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
5518 if (LEN == STRING_BYTES (name) \
5519 && ! strncmp (name->data, NAME, LEN)) \
5520 return BIT;
5522 case 'A':
5523 SINGLE_LETTER_MOD (alt_modifier);
5524 break;
5526 case 'a':
5527 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
5528 break;
5530 case 'C':
5531 SINGLE_LETTER_MOD (ctrl_modifier);
5532 break;
5534 case 'c':
5535 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
5536 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
5537 break;
5539 case 'H':
5540 SINGLE_LETTER_MOD (hyper_modifier);
5541 break;
5543 case 'h':
5544 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
5545 break;
5547 case 'M':
5548 SINGLE_LETTER_MOD (meta_modifier);
5549 break;
5551 case 'm':
5552 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
5553 break;
5555 case 'S':
5556 SINGLE_LETTER_MOD (shift_modifier);
5557 break;
5559 case 's':
5560 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
5561 MULTI_LETTER_MOD (super_modifier, "super", 5);
5562 SINGLE_LETTER_MOD (super_modifier);
5563 break;
5565 case 'd':
5566 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
5567 MULTI_LETTER_MOD (down_modifier, "down", 4);
5568 MULTI_LETTER_MOD (double_modifier, "double", 6);
5569 break;
5571 case 't':
5572 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
5573 break;
5575 #undef SINGLE_LETTER_MOD
5576 #undef MULTI_LETTER_MOD
5579 return 0;
5582 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
5583 Such a list is not valid as an event,
5584 but it can be a Lucid-style event type list. */
5587 lucid_event_type_list_p (object)
5588 Lisp_Object object;
5590 Lisp_Object tail;
5592 if (! CONSP (object))
5593 return 0;
5595 for (tail = object; CONSP (tail); tail = XCDR (tail))
5597 Lisp_Object elt;
5598 elt = XCAR (tail);
5599 if (! (INTEGERP (elt) || SYMBOLP (elt)))
5600 return 0;
5603 return NILP (tail);
5606 /* Store into *addr a value nonzero if terminal input chars are available.
5607 Serves the purpose of ioctl (0, FIONREAD, addr)
5608 but works even if FIONREAD does not exist.
5609 (In fact, this may actually read some input.)
5611 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe. */
5613 static void
5614 get_input_pending (addr, do_timers_now)
5615 int *addr;
5616 int do_timers_now;
5618 /* First of all, have we already counted some input? */
5619 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
5621 /* If input is being read as it arrives, and we have none, there is none. */
5622 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
5623 return;
5625 /* Try to read some input and see how much we get. */
5626 gobble_input (0);
5627 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
5630 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
5632 void
5633 gobble_input (expected)
5634 int expected;
5636 #ifndef VMS
5637 #ifdef SIGIO
5638 if (interrupt_input)
5640 SIGMASKTYPE mask;
5641 mask = sigblock (sigmask (SIGIO));
5642 read_avail_input (expected);
5643 sigsetmask (mask);
5645 else
5646 #ifdef POLL_FOR_INPUT
5647 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
5649 SIGMASKTYPE mask;
5650 mask = sigblock (sigmask (SIGALRM));
5651 read_avail_input (expected);
5652 sigsetmask (mask);
5654 else
5655 #endif
5656 #endif
5657 read_avail_input (expected);
5658 #endif
5661 /* Put a buffer_switch_event in the buffer
5662 so that read_key_sequence will notice the new current buffer. */
5664 void
5665 record_asynch_buffer_change ()
5667 struct input_event event;
5668 Lisp_Object tem;
5670 event.kind = buffer_switch_event;
5671 event.frame_or_window = Qnil;
5672 event.arg = Qnil;
5674 #ifdef subprocesses
5675 /* We don't need a buffer-switch event unless Emacs is waiting for input.
5676 The purpose of the event is to make read_key_sequence look up the
5677 keymaps again. If we aren't in read_key_sequence, we don't need one,
5678 and the event could cause trouble by messing up (input-pending-p). */
5679 tem = Fwaiting_for_user_input_p ();
5680 if (NILP (tem))
5681 return;
5682 #else
5683 /* We never need these events if we have no asynchronous subprocesses. */
5684 return;
5685 #endif
5687 /* Make sure no interrupt happens while storing the event. */
5688 #ifdef SIGIO
5689 if (interrupt_input)
5691 SIGMASKTYPE mask;
5692 mask = sigblock (sigmask (SIGIO));
5693 kbd_buffer_store_event (&event);
5694 sigsetmask (mask);
5696 else
5697 #endif
5699 stop_polling ();
5700 kbd_buffer_store_event (&event);
5701 start_polling ();
5705 #ifndef VMS
5707 /* Read any terminal input already buffered up by the system
5708 into the kbd_buffer, but do not wait.
5710 EXPECTED should be nonzero if the caller knows there is some input.
5712 Except on VMS, all input is read by this function.
5713 If interrupt_input is nonzero, this function MUST be called
5714 only when SIGIO is blocked.
5716 Returns the number of keyboard chars read, or -1 meaning
5717 this is a bad time to try to read input. */
5719 static int
5720 read_avail_input (expected)
5721 int expected;
5723 struct input_event buf[KBD_BUFFER_SIZE];
5724 register int i;
5725 int nread;
5727 if (read_socket_hook)
5728 /* No need for FIONREAD or fcntl; just say don't wait. */
5729 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE, expected);
5730 else
5732 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
5733 the kbd_buffer can really hold. That may prevent loss
5734 of characters on some systems when input is stuffed at us. */
5735 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
5736 int n_to_read;
5738 /* Determine how many characters we should *try* to read. */
5739 #ifdef WINDOWSNT
5740 return 0;
5741 #else /* not WINDOWSNT */
5742 #ifdef MSDOS
5743 n_to_read = dos_keysns ();
5744 if (n_to_read == 0)
5745 return 0;
5746 #else /* not MSDOS */
5747 #ifdef FIONREAD
5748 /* Find out how much input is available. */
5749 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
5750 /* Formerly simply reported no input, but that sometimes led to
5751 a failure of Emacs to terminate.
5752 SIGHUP seems appropriate if we can't reach the terminal. */
5753 /* ??? Is it really right to send the signal just to this process
5754 rather than to the whole process group?
5755 Perhaps on systems with FIONREAD Emacs is alone in its group. */
5756 kill (getpid (), SIGHUP);
5757 if (n_to_read == 0)
5758 return 0;
5759 if (n_to_read > sizeof cbuf)
5760 n_to_read = sizeof cbuf;
5761 #else /* no FIONREAD */
5762 #if defined (USG) || defined (DGUX)
5763 /* Read some input if available, but don't wait. */
5764 n_to_read = sizeof cbuf;
5765 fcntl (input_fd, F_SETFL, O_NDELAY);
5766 #else
5767 you lose;
5768 #endif
5769 #endif
5770 #endif /* not MSDOS */
5771 #endif /* not WINDOWSNT */
5773 /* Now read; for one reason or another, this will not block.
5774 NREAD is set to the number of chars read. */
5777 #ifdef MSDOS
5778 cbuf[0] = dos_keyread ();
5779 nread = 1;
5780 #else
5781 nread = emacs_read (input_fd, cbuf, n_to_read);
5782 #endif
5783 /* POSIX infers that processes which are not in the session leader's
5784 process group won't get SIGHUP's at logout time. BSDI adheres to
5785 this part standard and returns -1 from read (0) with errno==EIO
5786 when the control tty is taken away.
5787 Jeffrey Honig <jch@bsdi.com> says this is generally safe. */
5788 if (nread == -1 && errno == EIO)
5789 kill (0, SIGHUP);
5790 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
5791 /* The kernel sometimes fails to deliver SIGHUP for ptys.
5792 This looks incorrect, but it isn't, because _BSD causes
5793 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
5794 and that causes a value other than 0 when there is no input. */
5795 if (nread == 0)
5796 kill (0, SIGHUP);
5797 #endif
5799 while (
5800 /* We used to retry the read if it was interrupted.
5801 But this does the wrong thing when O_NDELAY causes
5802 an EAGAIN error. Does anybody know of a situation
5803 where a retry is actually needed? */
5804 #if 0
5805 nread < 0 && (errno == EAGAIN
5806 #ifdef EFAULT
5807 || errno == EFAULT
5808 #endif
5809 #ifdef EBADSLT
5810 || errno == EBADSLT
5811 #endif
5813 #else
5815 #endif
5818 #ifndef FIONREAD
5819 #if defined (USG) || defined (DGUX)
5820 fcntl (input_fd, F_SETFL, 0);
5821 #endif /* USG or DGUX */
5822 #endif /* no FIONREAD */
5823 for (i = 0; i < nread; i++)
5825 buf[i].kind = ascii_keystroke;
5826 buf[i].modifiers = 0;
5827 if (meta_key == 1 && (cbuf[i] & 0x80))
5828 buf[i].modifiers = meta_modifier;
5829 if (meta_key != 2)
5830 cbuf[i] &= ~0x80;
5832 buf[i].code = cbuf[i];
5833 buf[i].frame_or_window = selected_frame;
5834 buf[i].arg = Qnil;
5838 /* Scan the chars for C-g and store them in kbd_buffer. */
5839 for (i = 0; i < nread; i++)
5841 kbd_buffer_store_event (&buf[i]);
5842 /* Don't look at input that follows a C-g too closely.
5843 This reduces lossage due to autorepeat on C-g. */
5844 if (buf[i].kind == ascii_keystroke
5845 && buf[i].code == quit_char)
5846 break;
5849 return nread;
5851 #endif /* not VMS */
5853 #ifdef SIGIO /* for entire page */
5854 /* Note SIGIO has been undef'd if FIONREAD is missing. */
5856 SIGTYPE
5857 input_available_signal (signo)
5858 int signo;
5860 /* Must preserve main program's value of errno. */
5861 int old_errno = errno;
5862 #ifdef BSD4_1
5863 extern int select_alarmed;
5864 #endif
5866 #if defined (USG) && !defined (POSIX_SIGNALS)
5867 /* USG systems forget handlers when they are used;
5868 must reestablish each time */
5869 signal (signo, input_available_signal);
5870 #endif /* USG */
5872 #ifdef BSD4_1
5873 sigisheld (SIGIO);
5874 #endif
5876 if (input_available_clear_time)
5877 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
5879 while (1)
5881 int nread;
5882 nread = read_avail_input (1);
5883 /* -1 means it's not ok to read the input now.
5884 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
5885 0 means there was no keyboard input available. */
5886 if (nread <= 0)
5887 break;
5889 #ifdef BSD4_1
5890 select_alarmed = 1; /* Force the select emulator back to life */
5891 #endif
5894 #ifdef BSD4_1
5895 sigfree ();
5896 #endif
5897 errno = old_errno;
5899 #endif /* SIGIO */
5901 /* Send ourselves a SIGIO.
5903 This function exists so that the UNBLOCK_INPUT macro in
5904 blockinput.h can have some way to take care of input we put off
5905 dealing with, without assuming that every file which uses
5906 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
5907 void
5908 reinvoke_input_signal ()
5910 #ifdef SIGIO
5911 kill (getpid (), SIGIO);
5912 #endif
5917 /* Return the prompt-string of a sparse keymap.
5918 This is the first element which is a string.
5919 Return nil if there is none. */
5921 Lisp_Object
5922 map_prompt (map)
5923 Lisp_Object map;
5925 while (CONSP (map))
5927 register Lisp_Object tem;
5928 tem = Fcar (map);
5929 if (STRINGP (tem))
5930 return tem;
5931 map = Fcdr (map);
5933 return Qnil;
5936 static void menu_bar_item ();
5937 static void menu_bar_one_keymap ();
5939 /* These variables hold the vector under construction within
5940 menu_bar_items and its subroutines, and the current index
5941 for storing into that vector. */
5942 static Lisp_Object menu_bar_items_vector;
5943 static int menu_bar_items_index;
5945 /* Return a vector of menu items for a menu bar, appropriate
5946 to the current buffer. Each item has three elements in the vector:
5947 KEY STRING MAPLIST.
5949 OLD is an old vector we can optionally reuse, or nil. */
5951 Lisp_Object
5952 menu_bar_items (old)
5953 Lisp_Object old;
5955 /* The number of keymaps we're scanning right now, and the number of
5956 keymaps we have allocated space for. */
5957 int nmaps;
5959 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
5960 in the current keymaps, or nil where it is not a prefix. */
5961 Lisp_Object *maps;
5963 Lisp_Object def, tem, tail;
5965 Lisp_Object result;
5967 int mapno;
5968 Lisp_Object oquit;
5970 int i;
5972 struct gcpro gcpro1;
5974 /* In order to build the menus, we need to call the keymap
5975 accessors. They all call QUIT. But this function is called
5976 during redisplay, during which a quit is fatal. So inhibit
5977 quitting while building the menus.
5978 We do this instead of specbind because (1) errors will clear it anyway
5979 and (2) this avoids risk of specpdl overflow. */
5980 oquit = Vinhibit_quit;
5981 Vinhibit_quit = Qt;
5983 if (!NILP (old))
5984 menu_bar_items_vector = old;
5985 else
5986 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
5987 menu_bar_items_index = 0;
5989 GCPRO1 (menu_bar_items_vector);
5991 /* Build our list of keymaps.
5992 If we recognize a function key and replace its escape sequence in
5993 keybuf with its symbol, or if the sequence starts with a mouse
5994 click and we need to switch buffers, we jump back here to rebuild
5995 the initial keymaps from the current buffer. */
5997 Lisp_Object *tmaps;
5999 /* Should overriding-terminal-local-map and overriding-local-map apply? */
6000 if (!NILP (Voverriding_local_map_menu_flag))
6002 /* Yes, use them (if non-nil) as well as the global map. */
6003 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
6004 nmaps = 0;
6005 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6006 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6007 if (!NILP (Voverriding_local_map))
6008 maps[nmaps++] = Voverriding_local_map;
6010 else
6012 /* No, so use major and minor mode keymaps and keymap property. */
6013 int extra_maps = 2;
6014 Lisp_Object map = get_local_map (PT, current_buffer, keymap);
6015 if (!NILP (map))
6016 extra_maps = 3;
6017 nmaps = current_minor_maps (NULL, &tmaps);
6018 maps = (Lisp_Object *) alloca ((nmaps + extra_maps)
6019 * sizeof (maps[0]));
6020 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
6021 if (!NILP (map))
6022 maps[nmaps++] = get_local_map (PT, current_buffer, keymap);
6023 maps[nmaps++] = get_local_map (PT, current_buffer, local_map);
6025 maps[nmaps++] = current_global_map;
6028 /* Look up in each map the dummy prefix key `menu-bar'. */
6030 result = Qnil;
6032 for (mapno = nmaps - 1; mapno >= 0; mapno--)
6034 if (! NILP (maps[mapno]))
6035 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0), 0);
6036 else
6037 def = Qnil;
6039 tem = Fkeymapp (def);
6040 if (!NILP (tem))
6041 menu_bar_one_keymap (def);
6044 /* Move to the end those items that should be at the end. */
6046 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCDR (tail))
6048 int i;
6049 int end = menu_bar_items_index;
6051 for (i = 0; i < end; i += 4)
6052 if (EQ (XCAR (tail), XVECTOR (menu_bar_items_vector)->contents[i]))
6054 Lisp_Object tem0, tem1, tem2, tem3;
6055 /* Move the item at index I to the end,
6056 shifting all the others forward. */
6057 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
6058 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
6059 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
6060 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
6061 if (end > i + 4)
6062 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
6063 &XVECTOR (menu_bar_items_vector)->contents[i],
6064 (end - i - 4) * sizeof (Lisp_Object));
6065 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
6066 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
6067 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
6068 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
6069 break;
6073 /* Add nil, nil, nil, nil at the end. */
6074 i = menu_bar_items_index;
6075 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
6077 Lisp_Object tem;
6078 tem = Fmake_vector (make_number (2 * i), Qnil);
6079 bcopy (XVECTOR (menu_bar_items_vector)->contents,
6080 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
6081 menu_bar_items_vector = tem;
6083 /* Add this item. */
6084 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6085 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6086 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6087 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6088 menu_bar_items_index = i;
6090 Vinhibit_quit = oquit;
6091 UNGCPRO;
6092 return menu_bar_items_vector;
6095 /* Scan one map KEYMAP, accumulating any menu items it defines
6096 in menu_bar_items_vector. */
6098 static Lisp_Object menu_bar_one_keymap_changed_items;
6100 static void
6101 menu_bar_one_keymap (keymap)
6102 Lisp_Object keymap;
6104 Lisp_Object tail, item;
6106 menu_bar_one_keymap_changed_items = Qnil;
6108 /* Loop over all keymap entries that have menu strings. */
6109 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
6111 item = XCAR (tail);
6112 if (CONSP (item))
6113 menu_bar_item (XCAR (item), XCDR (item));
6114 else if (VECTORP (item))
6116 /* Loop over the char values represented in the vector. */
6117 int len = XVECTOR (item)->size;
6118 int c;
6119 for (c = 0; c < len; c++)
6121 Lisp_Object character;
6122 XSETFASTINT (character, c);
6123 menu_bar_item (character, XVECTOR (item)->contents[c]);
6129 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
6130 If there's already an item for KEY, add this DEF to it. */
6132 Lisp_Object item_properties;
6134 static void
6135 menu_bar_item (key, item)
6136 Lisp_Object key, item;
6138 struct gcpro gcpro1;
6139 int i;
6140 Lisp_Object tem;
6142 if (EQ (item, Qundefined))
6144 /* If a map has an explicit `undefined' as definition,
6145 discard any previously made menu bar item. */
6147 for (i = 0; i < menu_bar_items_index; i += 4)
6148 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
6150 if (menu_bar_items_index > i + 4)
6151 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
6152 &XVECTOR (menu_bar_items_vector)->contents[i],
6153 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
6154 menu_bar_items_index -= 4;
6155 return;
6158 /* If there's no definition for this key yet,
6159 just ignore `undefined'. */
6160 return;
6163 GCPRO1 (key); /* Is this necessary? */
6164 i = parse_menu_item (item, 0, 1);
6165 UNGCPRO;
6166 if (!i)
6167 return;
6169 /* If this keymap has already contributed to this KEY,
6170 don't contribute to it a second time. */
6171 tem = Fmemq (key, menu_bar_one_keymap_changed_items);
6172 if (!NILP (tem))
6173 return;
6175 menu_bar_one_keymap_changed_items
6176 = Fcons (key, menu_bar_one_keymap_changed_items);
6178 item = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6180 /* Find any existing item for this KEY. */
6181 for (i = 0; i < menu_bar_items_index; i += 4)
6182 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
6183 break;
6185 /* If we did not find this KEY, add it at the end. */
6186 if (i == menu_bar_items_index)
6188 /* If vector is too small, get a bigger one. */
6189 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
6191 Lisp_Object tem;
6192 tem = Fmake_vector (make_number (2 * i), Qnil);
6193 bcopy (XVECTOR (menu_bar_items_vector)->contents,
6194 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
6195 menu_bar_items_vector = tem;
6198 /* Add this item. */
6199 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
6200 XVECTOR (menu_bar_items_vector)->contents[i++]
6201 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
6202 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (item, Qnil);
6203 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
6204 menu_bar_items_index = i;
6206 /* We did find an item for this KEY. Add ITEM to its list of maps. */
6207 else
6209 Lisp_Object old;
6210 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
6211 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (item, old);
6215 /* This is used as the handler when calling menu_item_eval_property. */
6216 static Lisp_Object
6217 menu_item_eval_property_1 (arg)
6218 Lisp_Object arg;
6220 /* If we got a quit from within the menu computation,
6221 quit all the way out of it. This takes care of C-] in the debugger. */
6222 if (CONSP (arg) && EQ (XCAR (arg), Qquit))
6223 Fsignal (Qquit, Qnil);
6225 return Qnil;
6228 /* Evaluate an expression and return the result (or nil if something
6229 went wrong). Used to evaluate dynamic parts of menu items. */
6230 Lisp_Object
6231 menu_item_eval_property (sexpr)
6232 Lisp_Object sexpr;
6234 int count = specpdl_ptr - specpdl;
6235 Lisp_Object val;
6236 specbind (Qinhibit_redisplay, Qt);
6237 val = internal_condition_case_1 (Feval, sexpr, Qerror,
6238 menu_item_eval_property_1);
6239 return unbind_to (count, val);
6242 /* This function parses a menu item and leaves the result in the
6243 vector item_properties.
6244 ITEM is a key binding, a possible menu item.
6245 If NOTREAL is nonzero, only check for equivalent key bindings, don't
6246 evaluate dynamic expressions in the menu item.
6247 INMENUBAR is > 0 when this is considered for an entry in a menu bar
6248 top level.
6249 INMENUBAR is < 0 when this is considered for an entry in a keyboard menu.
6250 parse_menu_item returns true if the item is a menu item and false
6251 otherwise. */
6254 parse_menu_item (item, notreal, inmenubar)
6255 Lisp_Object item;
6256 int notreal, inmenubar;
6258 Lisp_Object def, tem, item_string, start;
6259 Lisp_Object cachelist;
6260 Lisp_Object filter;
6261 Lisp_Object keyhint;
6262 int i;
6263 int newcache = 0;
6265 cachelist = Qnil;
6266 filter = Qnil;
6267 keyhint = Qnil;
6269 if (!CONSP (item))
6270 return 0;
6272 /* Create item_properties vector if necessary. */
6273 if (NILP (item_properties))
6274 item_properties
6275 = Fmake_vector (make_number (ITEM_PROPERTY_ENABLE + 1), Qnil);
6277 /* Initialize optional entries. */
6278 for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++)
6279 XVECTOR (item_properties)->contents[i] = Qnil;
6280 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = Qt;
6282 /* Save the item here to protect it from GC. */
6283 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ITEM] = item;
6285 item_string = XCAR (item);
6287 start = item;
6288 item = XCDR (item);
6289 if (STRINGP (item_string))
6291 /* Old format menu item. */
6292 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME] = item_string;
6294 /* Maybe help string. */
6295 if (CONSP (item) && STRINGP (XCAR (item)))
6297 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]
6298 = XCAR (item);
6299 start = item;
6300 item = XCDR (item);
6303 /* Maybe key binding cache. */
6304 if (CONSP (item) && CONSP (XCAR (item))
6305 && (NILP (XCAR (XCAR (item)))
6306 || VECTORP (XCAR (XCAR (item)))))
6308 cachelist = XCAR (item);
6309 item = XCDR (item);
6312 /* This is the real definition--the function to run. */
6313 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = item;
6315 /* Get enable property, if any. */
6316 if (SYMBOLP (item))
6318 tem = Fget (item, Qmenu_enable);
6319 if (!NILP (tem))
6320 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = tem;
6323 else if (EQ (item_string, Qmenu_item) && CONSP (item))
6325 /* New format menu item. */
6326 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME]
6327 = XCAR (item);
6328 start = XCDR (item);
6329 if (CONSP (start))
6331 /* We have a real binding. */
6332 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF]
6333 = XCAR (start);
6335 item = XCDR (start);
6336 /* Is there a cache list with key equivalences. */
6337 if (CONSP (item) && CONSP (XCAR (item)))
6339 cachelist = XCAR (item);
6340 item = XCDR (item);
6343 /* Parse properties. */
6344 while (CONSP (item) && CONSP (XCDR (item)))
6346 tem = XCAR (item);
6347 item = XCDR (item);
6349 if (EQ (tem, QCenable))
6350 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE]
6351 = XCAR (item);
6352 else if (EQ (tem, QCvisible) && !notreal)
6354 /* If got a visible property and that evaluates to nil
6355 then ignore this item. */
6356 tem = menu_item_eval_property (XCAR (item));
6357 if (NILP (tem))
6358 return 0;
6360 else if (EQ (tem, QChelp))
6361 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]
6362 = XCAR (item);
6363 else if (EQ (tem, QCfilter))
6364 filter = item;
6365 else if (EQ (tem, QCkey_sequence))
6367 tem = XCAR (item);
6368 if (NILP (cachelist)
6369 && (SYMBOLP (tem) || STRINGP (tem) || VECTORP (tem)))
6370 /* Be GC protected. Set keyhint to item instead of tem. */
6371 keyhint = item;
6373 else if (EQ (tem, QCkeys))
6375 tem = XCAR (item);
6376 if (CONSP (tem) || (STRINGP (tem) && NILP (cachelist)))
6377 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ]
6378 = tem;
6380 else if (EQ (tem, QCbutton) && CONSP (XCAR (item)))
6382 Lisp_Object type;
6383 tem = XCAR (item);
6384 type = XCAR (tem);
6385 if (EQ (type, QCtoggle) || EQ (type, QCradio))
6387 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]
6388 = XCDR (tem);
6389 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE]
6390 = type;
6393 item = XCDR (item);
6396 else if (inmenubar || !NILP (start))
6397 return 0;
6399 else
6400 return 0; /* not a menu item */
6402 /* If item string is not a string, evaluate it to get string.
6403 If we don't get a string, skip this item. */
6404 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
6405 if (!(STRINGP (item_string) || notreal))
6407 item_string = menu_item_eval_property (item_string);
6408 if (!STRINGP (item_string))
6409 return 0;
6410 XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME] = item_string;
6413 /* If got a filter apply it on definition. */
6414 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6415 if (!NILP (filter))
6417 def = menu_item_eval_property (list2 (XCAR (filter),
6418 list2 (Qquote, def)));
6420 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = def;
6423 /* If we got no definition, this item is just unselectable text which
6424 is OK in a submenu but not in the menubar. */
6425 if (NILP (def))
6426 return (inmenubar ? 0 : 1);
6428 /* Enable or disable selection of item. */
6429 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
6430 if (!EQ (tem, Qt))
6432 if (notreal)
6433 tem = Qt;
6434 else
6435 tem = menu_item_eval_property (tem);
6436 if (inmenubar && NILP (tem))
6437 return 0; /* Ignore disabled items in menu bar. */
6438 XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = tem;
6441 /* See if this is a separate pane or a submenu. */
6442 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6443 tem = get_keymap_1 (def, 0, 1);
6444 /* For a subkeymap, just record its details and exit. */
6445 if (!NILP (tem))
6447 XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP] = tem;
6448 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = tem;
6449 return 1;
6451 /* At the top level in the menu bar, do likewise for commands also.
6452 The menu bar does not display equivalent key bindings anyway.
6453 ITEM_PROPERTY_DEF is already set up properly. */
6454 if (inmenubar > 0)
6455 return 1;
6457 /* This is a command. See if there is an equivalent key binding. */
6458 if (NILP (cachelist))
6460 /* We have to create a cachelist. */
6461 CHECK_IMPURE (start);
6462 XCDR (start) = Fcons (Fcons (Qnil, Qnil), XCDR (start));
6463 cachelist = XCAR (XCDR (start));
6464 newcache = 1;
6465 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
6466 if (!NILP (keyhint))
6468 XCAR (cachelist) = XCAR (keyhint);
6469 newcache = 0;
6471 else if (STRINGP (tem))
6473 XCDR (cachelist) = Fsubstitute_command_keys (tem);
6474 XCAR (cachelist) = Qt;
6477 tem = XCAR (cachelist);
6478 if (!EQ (tem, Qt))
6480 int chkcache = 0;
6481 Lisp_Object prefix;
6483 if (!NILP (tem))
6484 tem = Fkey_binding (tem, Qnil);
6486 prefix = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
6487 if (CONSP (prefix))
6489 def = XCAR (prefix);
6490 prefix = XCDR (prefix);
6492 else
6493 def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6495 if (NILP (XCAR (cachelist))) /* Have no saved key. */
6497 if (newcache /* Always check first time. */
6498 /* Should we check everything when precomputing key
6499 bindings? */
6500 /* || notreal */
6501 /* If something had no key binding before, don't recheck it
6502 because that is too slow--except if we have a list of
6503 rebound commands in Vdefine_key_rebound_commands, do
6504 recheck any command that appears in that list. */
6505 || (CONSP (Vdefine_key_rebound_commands)
6506 && !NILP (Fmemq (def, Vdefine_key_rebound_commands))))
6507 chkcache = 1;
6509 /* We had a saved key. Is it still bound to the command? */
6510 else if (NILP (tem)
6511 || (!EQ (tem, def)
6512 /* If the command is an alias for another
6513 (such as lmenu.el set it up), check if the
6514 original command matches the cached command. */
6515 && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function))))
6516 chkcache = 1; /* Need to recompute key binding. */
6518 if (chkcache)
6520 /* Recompute equivalent key binding. If the command is an alias
6521 for another (such as lmenu.el set it up), see if the original
6522 command name has equivalent keys. Otherwise look up the
6523 specified command itself. We don't try both, because that
6524 makes lmenu menus slow. */
6525 if (SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
6526 && ! NILP (Fget (def, Qmenu_alias)))
6527 def = XSYMBOL (def)->function;
6528 tem = Fwhere_is_internal (def, Qnil, Qt, Qnil);
6529 XCAR (cachelist) = tem;
6530 if (NILP (tem))
6532 XCDR (cachelist) = Qnil;
6533 chkcache = 0;
6536 else if (!NILP (keyhint) && !NILP (XCAR (cachelist)))
6538 tem = XCAR (cachelist);
6539 chkcache = 1;
6542 newcache = chkcache;
6543 if (chkcache)
6545 tem = Fkey_description (tem);
6546 if (CONSP (prefix))
6548 if (STRINGP (XCAR (prefix)))
6549 tem = concat2 (XCAR (prefix), tem);
6550 if (STRINGP (XCDR (prefix)))
6551 tem = concat2 (tem, XCDR (prefix));
6553 XCDR (cachelist) = tem;
6557 tem = XCDR (cachelist);
6558 if (newcache && !NILP (tem))
6560 tem = concat3 (build_string (" ("), tem, build_string (")"));
6561 XCDR (cachelist) = tem;
6564 /* If we only want to precompute equivalent key bindings, stop here. */
6565 if (notreal)
6566 return 1;
6568 /* If we have an equivalent key binding, use that. */
6569 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ] = tem;
6571 /* Include this when menu help is implemented.
6572 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP];
6573 if (!(NILP (tem) || STRINGP (tem)))
6575 tem = menu_item_eval_property (tem);
6576 if (!STRINGP (tem))
6577 tem = Qnil;
6578 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP] = tem;
6582 /* Handle radio buttons or toggle boxes. */
6583 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
6584 if (!NILP (tem))
6585 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]
6586 = menu_item_eval_property (tem);
6588 return 1;
6593 /***********************************************************************
6594 Tool-bars
6595 ***********************************************************************/
6597 /* A vector holding tool bar items while they are parsed in function
6598 tool_bar_items runs Each item occupies TOOL_BAR_ITEM_NSCLOTS elements
6599 in the vector. */
6601 static Lisp_Object tool_bar_items_vector;
6603 /* A vector holding the result of parse_tool_bar_item. Layout is like
6604 the one for a single item in tool_bar_items_vector. */
6606 static Lisp_Object tool_bar_item_properties;
6608 /* Next free index in tool_bar_items_vector. */
6610 static int ntool_bar_items;
6612 /* The symbols `tool-bar', and `:image'. */
6614 extern Lisp_Object Qtool_bar;
6615 Lisp_Object QCimage;
6617 /* Function prototypes. */
6619 static void init_tool_bar_items P_ ((Lisp_Object));
6620 static void process_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
6621 static int parse_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
6622 static void append_tool_bar_item P_ ((void));
6625 /* Return a vector of tool bar items for keymaps currently in effect.
6626 Reuse vector REUSE if non-nil. Return in *NITEMS the number of
6627 tool bar items found. */
6629 Lisp_Object
6630 tool_bar_items (reuse, nitems)
6631 Lisp_Object reuse;
6632 int *nitems;
6634 Lisp_Object *maps;
6635 int nmaps, i;
6636 Lisp_Object oquit;
6637 Lisp_Object *tmaps;
6638 extern Lisp_Object Voverriding_local_map_menu_flag;
6639 extern Lisp_Object Voverriding_local_map;
6641 *nitems = 0;
6643 /* In order to build the menus, we need to call the keymap
6644 accessors. They all call QUIT. But this function is called
6645 during redisplay, during which a quit is fatal. So inhibit
6646 quitting while building the menus. We do this instead of
6647 specbind because (1) errors will clear it anyway and (2) this
6648 avoids risk of specpdl overflow. */
6649 oquit = Vinhibit_quit;
6650 Vinhibit_quit = Qt;
6652 /* Initialize tool_bar_items_vector and protect it from GC. */
6653 init_tool_bar_items (reuse);
6655 /* Build list of keymaps in maps. Set nmaps to the number of maps
6656 to process. */
6658 /* Should overriding-terminal-local-map and overriding-local-map apply? */
6659 if (!NILP (Voverriding_local_map_menu_flag))
6661 /* Yes, use them (if non-nil) as well as the global map. */
6662 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
6663 nmaps = 0;
6664 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6665 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6666 if (!NILP (Voverriding_local_map))
6667 maps[nmaps++] = Voverriding_local_map;
6669 else
6671 /* No, so use major and minor mode keymaps and keymap property. */
6672 int extra_maps = 2;
6673 Lisp_Object map = get_local_map (PT, current_buffer, keymap);
6674 if (!NILP (map))
6675 extra_maps = 3;
6676 nmaps = current_minor_maps (NULL, &tmaps);
6677 maps = (Lisp_Object *) alloca ((nmaps + extra_maps)
6678 * sizeof (maps[0]));
6679 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
6680 if (!NILP (map))
6681 maps[nmaps++] = get_local_map (PT, current_buffer, keymap);
6682 maps[nmaps++] = get_local_map (PT, current_buffer, local_map);
6685 /* Add global keymap at the end. */
6686 maps[nmaps++] = current_global_map;
6688 /* Process maps in reverse order and look up in each map the prefix
6689 key `tool-bar'. */
6690 for (i = nmaps - 1; i >= 0; --i)
6691 if (!NILP (maps[i]))
6693 Lisp_Object keymap;
6695 keymap = get_keyelt (access_keymap (maps[i], Qtool_bar, 1, 1), 0);
6696 if (!NILP (Fkeymapp (keymap)))
6698 Lisp_Object tail;
6700 /* KEYMAP is a list `(keymap (KEY . BINDING) ...)'. */
6701 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
6703 Lisp_Object keydef = XCAR (tail);
6704 if (CONSP (keydef))
6705 process_tool_bar_item (XCAR (keydef), XCDR (keydef));
6710 Vinhibit_quit = oquit;
6711 *nitems = ntool_bar_items / TOOL_BAR_ITEM_NSLOTS;
6712 return tool_bar_items_vector;
6716 /* Process the definition of KEY which is DEF. */
6718 static void
6719 process_tool_bar_item (key, def)
6720 Lisp_Object key, def;
6722 int i;
6723 extern Lisp_Object Qundefined;
6724 struct gcpro gcpro1, gcpro2;
6726 /* Protect KEY and DEF from GC because parse_tool_bar_item may call
6727 eval. */
6728 GCPRO2 (key, def);
6730 if (EQ (def, Qundefined))
6732 /* If a map has an explicit `undefined' as definition,
6733 discard any previously made item. */
6734 for (i = 0; i < ntool_bar_items; i += TOOL_BAR_ITEM_NSLOTS)
6736 Lisp_Object *v = XVECTOR (tool_bar_items_vector)->contents + i;
6738 if (EQ (key, v[TOOL_BAR_ITEM_KEY]))
6740 if (ntool_bar_items > i + TOOL_BAR_ITEM_NSLOTS)
6741 bcopy (v + TOOL_BAR_ITEM_NSLOTS, v,
6742 ((ntool_bar_items - i - TOOL_BAR_ITEM_NSLOTS)
6743 * sizeof (Lisp_Object)));
6744 ntool_bar_items -= TOOL_BAR_ITEM_NSLOTS;
6745 break;
6749 else if (parse_tool_bar_item (key, def))
6750 /* Append a new tool bar item to tool_bar_items_vector. Accept
6751 more than one definition for the same key. */
6752 append_tool_bar_item ();
6754 UNGCPRO;
6758 /* Parse a tool bar item specification ITEM for key KEY and return the
6759 result in tool_bar_item_properties. Value is zero if ITEM is
6760 invalid.
6762 ITEM is a list `(menu-item CAPTION BINDING PROPS...)'.
6764 CAPTION is the caption of the item, If it's not a string, it is
6765 evaluated to get a string.
6767 BINDING is the tool bar item's binding. Tool-bar items with keymaps
6768 as binding are currently ignored.
6770 The following properties are recognized:
6772 - `:enable FORM'.
6774 FORM is evaluated and specifies whether the tool bar item is
6775 enabled or disabled.
6777 - `:visible FORM'
6779 FORM is evaluated and specifies whether the tool bar item is visible.
6781 - `:filter FUNCTION'
6783 FUNCTION is invoked with one parameter `(quote BINDING)'. Its
6784 result is stored as the new binding.
6786 - `:button (TYPE SELECTED)'
6788 TYPE must be one of `:radio' or `:toggle'. SELECTED is evaluated
6789 and specifies whether the button is selected (pressed) or not.
6791 - `:image IMAGES'
6793 IMAGES is either a single image specification or a vector of four
6794 image specifications. See enum tool_bar_item_images.
6796 - `:help HELP-STRING'.
6798 Gives a help string to display for the tool bar item. */
6800 static int
6801 parse_tool_bar_item (key, item)
6802 Lisp_Object key, item;
6804 /* Access slot with index IDX of vector tool_bar_item_properties. */
6805 #define PROP(IDX) XVECTOR (tool_bar_item_properties)->contents[IDX]
6807 Lisp_Object filter = Qnil;
6808 Lisp_Object caption;
6809 extern Lisp_Object QCenable, QCvisible, QChelp, QCfilter;
6810 extern Lisp_Object QCbutton, QCtoggle, QCradio;
6811 int i;
6813 /* Defininition looks like `(tool-bar-item CAPTION BINDING
6814 PROPS...)'. Rule out items that aren't lists, don't start with
6815 `tool-bar-item' or whose rest following `tool-bar-item' is not a
6816 list. */
6817 if (!CONSP (item)
6818 || !EQ (XCAR (item), Qmenu_item)
6819 || (item = XCDR (item),
6820 !CONSP (item)))
6821 return 0;
6823 /* Create tool_bar_item_properties vector if necessary. Reset it to
6824 defaults. */
6825 if (VECTORP (tool_bar_item_properties))
6827 for (i = 0; i < TOOL_BAR_ITEM_NSLOTS; ++i)
6828 PROP (i) = Qnil;
6830 else
6831 tool_bar_item_properties
6832 = Fmake_vector (make_number (TOOL_BAR_ITEM_NSLOTS), Qnil);
6834 /* Set defaults. */
6835 PROP (TOOL_BAR_ITEM_KEY) = key;
6836 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
6838 /* Get the caption of the item. If the caption is not a string,
6839 evaluate it to get a string. If we don't get a string, skip this
6840 item. */
6841 caption = XCAR (item);
6842 if (!STRINGP (caption))
6844 caption = menu_item_eval_property (caption);
6845 if (!STRINGP (caption))
6846 return 0;
6848 PROP (TOOL_BAR_ITEM_CAPTION) = caption;
6850 /* Give up if rest following the caption is not a list. */
6851 item = XCDR (item);
6852 if (!CONSP (item))
6853 return 0;
6855 /* Store the binding. */
6856 PROP (TOOL_BAR_ITEM_BINDING) = XCAR (item);
6857 item = XCDR (item);
6859 /* Process the rest of the properties. */
6860 for (; CONSP (item) && CONSP (XCDR (item)); item = XCDR (XCDR (item)))
6862 Lisp_Object key, value;
6864 key = XCAR (item);
6865 value = XCAR (XCDR (item));
6867 if (EQ (key, QCenable))
6868 /* `:enable FORM'. */
6869 PROP (TOOL_BAR_ITEM_ENABLED_P) = value;
6870 else if (EQ (key, QCvisible))
6872 /* `:visible FORM'. If got a visible property and that
6873 evaluates to nil then ignore this item. */
6874 if (NILP (menu_item_eval_property (value)))
6875 return 0;
6877 else if (EQ (key, QChelp))
6878 /* `:help HELP-STRING'. */
6879 PROP (TOOL_BAR_ITEM_HELP) = value;
6880 else if (EQ (key, QCfilter))
6881 /* ':filter FORM'. */
6882 filter = value;
6883 else if (EQ (key, QCbutton) && CONSP (value))
6885 /* `:button (TYPE . SELECTED)'. */
6886 Lisp_Object type, selected;
6888 type = XCAR (value);
6889 selected = XCDR (value);
6890 if (EQ (type, QCtoggle) || EQ (type, QCradio))
6892 PROP (TOOL_BAR_ITEM_SELECTED_P) = selected;
6893 PROP (TOOL_BAR_ITEM_TYPE) = type;
6896 else if (EQ (key, QCimage)
6897 && (CONSP (value)
6898 || (VECTORP (value) && XVECTOR (value)->size == 4)))
6899 /* Value is either a single image specification or a vector
6900 of 4 such specifications for the different buttion states. */
6901 PROP (TOOL_BAR_ITEM_IMAGES) = value;
6904 /* If got a filter apply it on binding. */
6905 if (!NILP (filter))
6906 PROP (TOOL_BAR_ITEM_BINDING)
6907 = menu_item_eval_property (list2 (filter,
6908 list2 (Qquote,
6909 PROP (TOOL_BAR_ITEM_BINDING))));
6911 /* See if the binding is a keymap. Give up if it is. */
6912 if (!NILP (get_keymap_1 (PROP (TOOL_BAR_ITEM_BINDING), 0, 1)))
6913 return 0;
6915 /* Enable or disable selection of item. */
6916 if (!EQ (PROP (TOOL_BAR_ITEM_ENABLED_P), Qt))
6917 PROP (TOOL_BAR_ITEM_ENABLED_P)
6918 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_ENABLED_P));
6920 /* Handle radio buttons or toggle boxes. */
6921 if (!NILP (PROP (TOOL_BAR_ITEM_SELECTED_P)))
6922 PROP (TOOL_BAR_ITEM_SELECTED_P)
6923 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_SELECTED_P));
6925 return 1;
6927 #undef PROP
6931 /* Initialize tool_bar_items_vector. REUSE, if non-nil, is a vector
6932 that can be reused. */
6934 static void
6935 init_tool_bar_items (reuse)
6936 Lisp_Object reuse;
6938 if (VECTORP (reuse))
6939 tool_bar_items_vector = reuse;
6940 else
6941 tool_bar_items_vector = Fmake_vector (make_number (64), Qnil);
6942 ntool_bar_items = 0;
6946 /* Append parsed tool bar item properties from
6947 tool_bar_item_properties */
6949 static void
6950 append_tool_bar_item ()
6952 Lisp_Object *to, *from;
6954 /* Enlarge tool_bar_items_vector if necessary. */
6955 if (ntool_bar_items + TOOL_BAR_ITEM_NSLOTS
6956 >= XVECTOR (tool_bar_items_vector)->size)
6958 Lisp_Object new_vector;
6959 int old_size = XVECTOR (tool_bar_items_vector)->size;
6961 new_vector = Fmake_vector (make_number (2 * old_size), Qnil);
6962 bcopy (XVECTOR (tool_bar_items_vector)->contents,
6963 XVECTOR (new_vector)->contents,
6964 old_size * sizeof (Lisp_Object));
6965 tool_bar_items_vector = new_vector;
6968 /* Append entries from tool_bar_item_properties to the end of
6969 tool_bar_items_vector. */
6970 to = XVECTOR (tool_bar_items_vector)->contents + ntool_bar_items;
6971 from = XVECTOR (tool_bar_item_properties)->contents;
6972 bcopy (from, to, TOOL_BAR_ITEM_NSLOTS * sizeof *to);
6973 ntool_bar_items += TOOL_BAR_ITEM_NSLOTS;
6980 /* Read a character using menus based on maps in the array MAPS.
6981 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
6982 Return t if we displayed a menu but the user rejected it.
6984 PREV_EVENT is the previous input event, or nil if we are reading
6985 the first event of a key sequence.
6987 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
6988 if we used a mouse menu to read the input, or zero otherwise. If
6989 USED_MOUSE_MENU is null, we don't dereference it.
6991 The prompting is done based on the prompt-string of the map
6992 and the strings associated with various map elements.
6994 This can be done with X menus or with menus put in the minibuf.
6995 These are done in different ways, depending on how the input will be read.
6996 Menus using X are done after auto-saving in read-char, getting the input
6997 event from Fx_popup_menu; menus using the minibuf use read_char recursively
6998 and do auto-saving in the inner call of read_char. */
7000 static Lisp_Object
7001 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
7002 int nmaps;
7003 Lisp_Object *maps;
7004 Lisp_Object prev_event;
7005 int *used_mouse_menu;
7007 int mapno;
7008 register Lisp_Object name;
7010 if (used_mouse_menu)
7011 *used_mouse_menu = 0;
7013 /* Use local over global Menu maps */
7015 if (! menu_prompting)
7016 return Qnil;
7018 /* Optionally disregard all but the global map. */
7019 if (inhibit_local_menu_bar_menus)
7021 maps += (nmaps - 1);
7022 nmaps = 1;
7025 /* Get the menu name from the first map that has one (a prompt string). */
7026 for (mapno = 0; mapno < nmaps; mapno++)
7028 name = map_prompt (maps[mapno]);
7029 if (!NILP (name))
7030 break;
7033 /* If we don't have any menus, just read a character normally. */
7034 if (mapno >= nmaps)
7035 return Qnil;
7037 #ifdef HAVE_MENUS
7038 /* If we got to this point via a mouse click,
7039 use a real menu for mouse selection. */
7040 if (EVENT_HAS_PARAMETERS (prev_event)
7041 && !EQ (XCAR (prev_event), Qmenu_bar)
7042 && !EQ (XCAR (prev_event), Qtool_bar))
7044 /* Display the menu and get the selection. */
7045 Lisp_Object *realmaps
7046 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
7047 Lisp_Object value;
7048 int nmaps1 = 0;
7050 /* Use the maps that are not nil. */
7051 for (mapno = 0; mapno < nmaps; mapno++)
7052 if (!NILP (maps[mapno]))
7053 realmaps[nmaps1++] = maps[mapno];
7055 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
7056 if (CONSP (value))
7058 Lisp_Object tem;
7060 record_menu_key (XCAR (value));
7062 /* If we got multiple events, unread all but
7063 the first.
7064 There is no way to prevent those unread events
7065 from showing up later in last_nonmenu_event.
7066 So turn symbol and integer events into lists,
7067 to indicate that they came from a mouse menu,
7068 so that when present in last_nonmenu_event
7069 they won't confuse things. */
7070 for (tem = XCDR (value); !NILP (tem);
7071 tem = XCDR (tem))
7073 record_menu_key (XCAR (tem));
7074 if (SYMBOLP (XCAR (tem))
7075 || INTEGERP (XCAR (tem)))
7076 XCAR (tem)
7077 = Fcons (XCAR (tem), Qnil);
7080 /* If we got more than one event, put all but the first
7081 onto this list to be read later.
7082 Return just the first event now. */
7083 Vunread_command_events
7084 = nconc2 (XCDR (value), Vunread_command_events);
7085 value = XCAR (value);
7087 else if (NILP (value))
7088 value = Qt;
7089 if (used_mouse_menu)
7090 *used_mouse_menu = 1;
7091 return value;
7093 #endif /* HAVE_MENUS */
7094 return Qnil ;
7097 /* Buffer in use so far for the minibuf prompts for menu keymaps.
7098 We make this bigger when necessary, and never free it. */
7099 static char *read_char_minibuf_menu_text;
7100 /* Size of that buffer. */
7101 static int read_char_minibuf_menu_width;
7103 static Lisp_Object
7104 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
7105 int commandflag ;
7106 int nmaps;
7107 Lisp_Object *maps;
7109 int mapno;
7110 register Lisp_Object name;
7111 int nlength;
7112 int width = FRAME_WIDTH (SELECTED_FRAME ()) - 4;
7113 int idx = -1;
7114 int nobindings = 1;
7115 Lisp_Object rest, vector;
7116 char *menu;
7118 if (! menu_prompting)
7119 return Qnil;
7121 /* Make sure we have a big enough buffer for the menu text. */
7122 if (read_char_minibuf_menu_text == 0)
7124 read_char_minibuf_menu_width = width + 4;
7125 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
7127 else if (width + 4 > read_char_minibuf_menu_width)
7129 read_char_minibuf_menu_width = width + 4;
7130 read_char_minibuf_menu_text
7131 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
7133 menu = read_char_minibuf_menu_text;
7135 /* Get the menu name from the first map that has one (a prompt string). */
7136 for (mapno = 0; mapno < nmaps; mapno++)
7138 name = map_prompt (maps[mapno]);
7139 if (!NILP (name))
7140 break;
7143 /* If we don't have any menus, just read a character normally. */
7144 if (mapno >= nmaps)
7145 return Qnil;
7147 /* Prompt string always starts with map's prompt, and a space. */
7148 strcpy (menu, XSTRING (name)->data);
7149 nlength = STRING_BYTES (XSTRING (name));
7150 menu[nlength++] = ':';
7151 menu[nlength++] = ' ';
7152 menu[nlength] = 0;
7154 /* Start prompting at start of first map. */
7155 mapno = 0;
7156 rest = maps[mapno];
7158 /* Present the documented bindings, a line at a time. */
7159 while (1)
7161 int notfirst = 0;
7162 int i = nlength;
7163 Lisp_Object obj;
7164 int ch;
7165 Lisp_Object orig_defn_macro;
7167 /* Loop over elements of map. */
7168 while (i < width)
7170 Lisp_Object elt;
7172 /* If reached end of map, start at beginning of next map. */
7173 if (NILP (rest))
7175 mapno++;
7176 /* At end of last map, wrap around to first map if just starting,
7177 or end this line if already have something on it. */
7178 if (mapno == nmaps)
7180 mapno = 0;
7181 if (notfirst || nobindings) break;
7183 rest = maps[mapno];
7186 /* Look at the next element of the map. */
7187 if (idx >= 0)
7188 elt = XVECTOR (vector)->contents[idx];
7189 else
7190 elt = Fcar_safe (rest);
7192 if (idx < 0 && VECTORP (elt))
7194 /* If we found a dense table in the keymap,
7195 advanced past it, but start scanning its contents. */
7196 rest = Fcdr_safe (rest);
7197 vector = elt;
7198 idx = 0;
7200 else
7202 /* An ordinary element. */
7203 Lisp_Object event, tem;
7205 if (idx < 0)
7207 event = Fcar_safe (elt); /* alist */
7208 elt = Fcdr_safe (elt);
7210 else
7212 XSETINT (event, idx); /* vector */
7215 /* Ignore the element if it has no prompt string. */
7216 if (INTEGERP (event) && parse_menu_item (elt, 0, -1))
7218 /* 1 if the char to type matches the string. */
7219 int char_matches;
7220 Lisp_Object upcased_event, downcased_event;
7221 Lisp_Object desc;
7222 Lisp_Object s
7223 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
7225 upcased_event = Fupcase (event);
7226 downcased_event = Fdowncase (event);
7227 char_matches = (XINT (upcased_event) == XSTRING (s)->data[0]
7228 || XINT (downcased_event) == XSTRING (s)->data[0]);
7229 if (! char_matches)
7230 desc = Fsingle_key_description (event);
7233 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
7234 if (!NILP (tem))
7235 /* Insert equivalent keybinding. */
7236 s = concat2 (s, tem);
7239 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
7240 if (EQ (tem, QCradio) || EQ (tem, QCtoggle))
7242 /* Insert button prefix. */
7243 Lisp_Object selected
7244 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
7245 if (EQ (tem, QCradio))
7246 tem = build_string (NILP (selected) ? "(*) " : "( ) ");
7247 else
7248 tem = build_string (NILP (selected) ? "[X] " : "[ ] ");
7249 s = concat2 (tem, s);
7253 /* If we have room for the prompt string, add it to this line.
7254 If this is the first on the line, always add it. */
7255 if ((XSTRING (s)->size + i + 2
7256 + (char_matches ? 0 : XSTRING (desc)->size + 3))
7257 < width
7258 || !notfirst)
7260 int thiswidth;
7262 /* Punctuate between strings. */
7263 if (notfirst)
7265 strcpy (menu + i, ", ");
7266 i += 2;
7268 notfirst = 1;
7269 nobindings = 0 ;
7271 /* If the char to type doesn't match the string's
7272 first char, explicitly show what char to type. */
7273 if (! char_matches)
7275 /* Add as much of string as fits. */
7276 thiswidth = XSTRING (desc)->size;
7277 if (thiswidth + i > width)
7278 thiswidth = width - i;
7279 bcopy (XSTRING (desc)->data, menu + i, thiswidth);
7280 i += thiswidth;
7281 strcpy (menu + i, " = ");
7282 i += 3;
7285 /* Add as much of string as fits. */
7286 thiswidth = XSTRING (s)->size;
7287 if (thiswidth + i > width)
7288 thiswidth = width - i;
7289 bcopy (XSTRING (s)->data, menu + i, thiswidth);
7290 i += thiswidth;
7291 menu[i] = 0;
7293 else
7295 /* If this element does not fit, end the line now,
7296 and save the element for the next line. */
7297 strcpy (menu + i, "...");
7298 break;
7302 /* Move past this element. */
7303 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
7304 /* Handle reaching end of dense table. */
7305 idx = -1;
7306 if (idx >= 0)
7307 idx++;
7308 else
7309 rest = Fcdr_safe (rest);
7313 /* Prompt with that and read response. */
7314 message2_nolog (menu, strlen (menu),
7315 ! NILP (current_buffer->enable_multibyte_characters));
7317 /* Make believe its not a keyboard macro in case the help char
7318 is pressed. Help characters are not recorded because menu prompting
7319 is not used on replay.
7321 orig_defn_macro = current_kboard->defining_kbd_macro;
7322 current_kboard->defining_kbd_macro = Qnil;
7324 obj = read_char (commandflag, 0, 0, Qnil, 0);
7325 while (BUFFERP (obj));
7326 current_kboard->defining_kbd_macro = orig_defn_macro;
7328 if (!INTEGERP (obj))
7329 return obj;
7330 else
7331 ch = XINT (obj);
7333 if (! EQ (obj, menu_prompt_more_char)
7334 && (!INTEGERP (menu_prompt_more_char)
7335 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
7337 if (!NILP (current_kboard->defining_kbd_macro))
7338 store_kbd_macro_char (obj);
7339 return obj;
7341 /* Help char - go round again */
7345 /* Reading key sequences. */
7347 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
7348 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
7349 keymap, or nil otherwise. Return the index of the first keymap in
7350 which KEY has any binding, or NMAPS if no map has a binding.
7352 If KEY is a meta ASCII character, treat it like meta-prefix-char
7353 followed by the corresponding non-meta character. Keymaps in
7354 CURRENT with non-prefix bindings for meta-prefix-char become nil in
7355 NEXT.
7357 If KEY has no bindings in any of the CURRENT maps, NEXT is left
7358 unmodified.
7360 NEXT may be the same array as CURRENT. */
7362 static int
7363 follow_key (key, nmaps, current, defs, next)
7364 Lisp_Object key;
7365 Lisp_Object *current, *defs, *next;
7366 int nmaps;
7368 int i, first_binding;
7369 int did_meta = 0;
7371 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
7372 followed by the corresponding non-meta character.
7373 Put the results into DEFS, since we are going to alter that anyway.
7374 Do not alter CURRENT or NEXT. */
7375 if (INTEGERP (key) && (XUINT (key) & CHAR_META))
7377 for (i = 0; i < nmaps; i++)
7378 if (! NILP (current[i]))
7380 Lisp_Object def;
7381 def = get_keyelt (access_keymap (current[i],
7382 meta_prefix_char, 1, 0), 0);
7384 /* Note that since we pass the resulting bindings through
7385 get_keymap_1, non-prefix bindings for meta-prefix-char
7386 disappear. */
7387 defs[i] = get_keymap_1 (def, 0, 1);
7389 else
7390 defs[i] = Qnil;
7392 did_meta = 1;
7393 XSETINT (key, XFASTINT (key) & ~CHAR_META);
7396 first_binding = nmaps;
7397 for (i = nmaps - 1; i >= 0; i--)
7399 if (! NILP (current[i]))
7401 Lisp_Object map;
7402 if (did_meta)
7403 map = defs[i];
7404 else
7405 map = current[i];
7407 defs[i] = get_keyelt (access_keymap (map, key, 1, 0), 1);
7408 if (! NILP (defs[i]))
7409 first_binding = i;
7411 else
7412 defs[i] = Qnil;
7415 /* Given the set of bindings we've found, produce the next set of maps. */
7416 if (first_binding < nmaps)
7417 for (i = 0; i < nmaps; i++)
7418 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
7420 return first_binding;
7423 /* Read a sequence of keys that ends with a non prefix character,
7424 storing it in KEYBUF, a buffer of size BUFSIZE.
7425 Prompt with PROMPT.
7426 Return the length of the key sequence stored.
7427 Return -1 if the user rejected a command menu.
7429 Echo starting immediately unless `prompt' is 0.
7431 Where a key sequence ends depends on the currently active keymaps.
7432 These include any minor mode keymaps active in the current buffer,
7433 the current buffer's local map, and the global map.
7435 If a key sequence has no other bindings, we check Vfunction_key_map
7436 to see if some trailing subsequence might be the beginning of a
7437 function key's sequence. If so, we try to read the whole function
7438 key, and substitute its symbolic name into the key sequence.
7440 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
7441 `double-' events into similar click events, if that would make them
7442 bound. We try to turn `triple-' events first into `double-' events,
7443 then into clicks.
7445 If we get a mouse click in a mode line, vertical divider, or other
7446 non-text area, we treat the click as if it were prefixed by the
7447 symbol denoting that area - `mode-line', `vertical-line', or
7448 whatever.
7450 If the sequence starts with a mouse click, we read the key sequence
7451 with respect to the buffer clicked on, not the current buffer.
7453 If the user switches frames in the midst of a key sequence, we put
7454 off the switch-frame event until later; the next call to
7455 read_char will return it.
7457 If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
7458 from the selected window's buffer. */
7460 static int
7461 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
7462 can_return_switch_frame, fix_current_buffer)
7463 Lisp_Object *keybuf;
7464 int bufsize;
7465 Lisp_Object prompt;
7466 int dont_downcase_last;
7467 int can_return_switch_frame;
7468 int fix_current_buffer;
7470 int count = specpdl_ptr - specpdl;
7472 /* How many keys there are in the current key sequence. */
7473 int t;
7475 /* The length of the echo buffer when we started reading, and
7476 the length of this_command_keys when we started reading. */
7477 int echo_start;
7478 int keys_start;
7480 /* The number of keymaps we're scanning right now, and the number of
7481 keymaps we have allocated space for. */
7482 int nmaps;
7483 int nmaps_allocated = 0;
7485 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
7486 the current keymaps. */
7487 Lisp_Object *defs;
7489 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
7490 in the current keymaps, or nil where it is not a prefix. */
7491 Lisp_Object *submaps;
7493 /* The local map to start out with at start of key sequence. */
7494 Lisp_Object orig_local_map;
7496 /* The map from the `keymap' property to start out with at start of
7497 key sequence. */
7498 Lisp_Object orig_keymap;
7500 /* 1 if we have already considered switching to the local-map property
7501 of the place where a mouse click occurred. */
7502 int localized_local_map = 0;
7504 /* The index in defs[] of the first keymap that has a binding for
7505 this key sequence. In other words, the lowest i such that
7506 defs[i] is non-nil. */
7507 int first_binding;
7509 /* If t < mock_input, then KEYBUF[t] should be read as the next
7510 input key.
7512 We use this to recover after recognizing a function key. Once we
7513 realize that a suffix of the current key sequence is actually a
7514 function key's escape sequence, we replace the suffix with the
7515 function key's binding from Vfunction_key_map. Now keybuf
7516 contains a new and different key sequence, so the echo area,
7517 this_command_keys, and the submaps and defs arrays are wrong. In
7518 this situation, we set mock_input to t, set t to 0, and jump to
7519 restart_sequence; the loop will read keys from keybuf up until
7520 mock_input, thus rebuilding the state; and then it will resume
7521 reading characters from the keyboard. */
7522 int mock_input = 0;
7524 /* If the sequence is unbound in submaps[], then
7525 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
7526 and fkey_map is its binding.
7528 These might be > t, indicating that all function key scanning
7529 should hold off until t reaches them. We do this when we've just
7530 recognized a function key, to avoid searching for the function
7531 key's again in Vfunction_key_map. */
7532 int fkey_start = 0, fkey_end = 0;
7533 Lisp_Object fkey_map;
7535 /* Likewise, for key_translation_map. */
7536 int keytran_start = 0, keytran_end = 0;
7537 Lisp_Object keytran_map;
7539 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
7540 we put it off for later. While we're reading, we keep the event here. */
7541 Lisp_Object delayed_switch_frame;
7543 /* See the comment below... */
7544 #if defined (GOBBLE_FIRST_EVENT)
7545 Lisp_Object first_event;
7546 #endif
7548 Lisp_Object original_uppercase;
7549 int original_uppercase_position = -1;
7551 /* Gets around Microsoft compiler limitations. */
7552 int dummyflag = 0;
7554 struct buffer *starting_buffer;
7556 /* Nonzero if we seem to have got the beginning of a binding
7557 in function_key_map. */
7558 int function_key_possible = 0;
7559 int key_translation_possible = 0;
7561 /* Save the status of key translation before each step,
7562 so that we can restore this after downcasing. */
7563 Lisp_Object prev_fkey_map;
7564 int prev_fkey_start;
7565 int prev_fkey_end;
7567 Lisp_Object prev_keytran_map;
7568 int prev_keytran_start;
7569 int prev_keytran_end;
7571 #if defined (GOBBLE_FIRST_EVENT)
7572 int junk;
7573 #endif
7575 raw_keybuf_count = 0;
7577 last_nonmenu_event = Qnil;
7579 delayed_switch_frame = Qnil;
7580 fkey_map = Vfunction_key_map;
7581 keytran_map = Vkey_translation_map;
7583 /* If there is no function-key-map, turn off function key scanning. */
7584 if (NILP (Fkeymapp (Vfunction_key_map)))
7585 fkey_start = fkey_end = bufsize + 1;
7587 /* If there is no key-translation-map, turn off scanning. */
7588 if (NILP (Fkeymapp (Vkey_translation_map)))
7589 keytran_start = keytran_end = bufsize + 1;
7591 if (INTERACTIVE)
7593 if (!NILP (prompt))
7594 echo_prompt (XSTRING (prompt)->data);
7595 else if (cursor_in_echo_area
7596 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
7597 && NILP (Fzerop (Vecho_keystrokes)))
7598 /* This doesn't put in a dash if the echo buffer is empty, so
7599 you don't always see a dash hanging out in the minibuffer. */
7600 echo_dash ();
7603 /* Record the initial state of the echo area and this_command_keys;
7604 we will need to restore them if we replay a key sequence. */
7605 if (INTERACTIVE)
7606 echo_start = echo_length ();
7607 keys_start = this_command_key_count;
7608 this_single_command_key_start = keys_start;
7610 #if defined (GOBBLE_FIRST_EVENT)
7611 /* This doesn't quite work, because some of the things that read_char
7612 does cannot safely be bypassed. It seems too risky to try to make
7613 this work right. */
7615 /* Read the first char of the sequence specially, before setting
7616 up any keymaps, in case a filter runs and switches buffers on us. */
7617 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
7618 &junk);
7619 #endif /* GOBBLE_FIRST_EVENT */
7621 orig_local_map = get_local_map (PT, current_buffer, local_map);
7622 orig_keymap = get_local_map (PT, current_buffer, keymap);
7624 /* We jump here when the key sequence has been thoroughly changed, and
7625 we need to rescan it starting from the beginning. When we jump here,
7626 keybuf[0..mock_input] holds the sequence we should reread. */
7627 replay_sequence:
7629 starting_buffer = current_buffer;
7630 function_key_possible = 0;
7631 key_translation_possible = 0;
7633 /* Build our list of keymaps.
7634 If we recognize a function key and replace its escape sequence in
7635 keybuf with its symbol, or if the sequence starts with a mouse
7636 click and we need to switch buffers, we jump back here to rebuild
7637 the initial keymaps from the current buffer. */
7639 Lisp_Object *maps;
7641 if (!NILP (current_kboard->Voverriding_terminal_local_map)
7642 || !NILP (Voverriding_local_map))
7644 if (3 > nmaps_allocated)
7646 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
7647 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
7648 nmaps_allocated = 3;
7650 nmaps = 0;
7651 if (!NILP (current_kboard->Voverriding_terminal_local_map))
7652 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
7653 if (!NILP (Voverriding_local_map))
7654 submaps[nmaps++] = Voverriding_local_map;
7656 else
7658 int extra_maps = 2;
7659 nmaps = current_minor_maps (0, &maps);
7660 if (!NILP (orig_keymap))
7661 extra_maps = 3;
7662 if (nmaps + extra_maps > nmaps_allocated)
7664 submaps = (Lisp_Object *) alloca ((nmaps+extra_maps)
7665 * sizeof (submaps[0]));
7666 defs = (Lisp_Object *) alloca ((nmaps+extra_maps)
7667 * sizeof (defs[0]));
7668 nmaps_allocated = nmaps + extra_maps;
7670 bcopy (maps, submaps, nmaps * sizeof (submaps[0]));
7671 if (!NILP (orig_keymap))
7672 submaps[nmaps++] = orig_keymap;
7673 submaps[nmaps++] = orig_local_map;
7675 submaps[nmaps++] = current_global_map;
7678 /* Find an accurate initial value for first_binding. */
7679 for (first_binding = 0; first_binding < nmaps; first_binding++)
7680 if (! NILP (submaps[first_binding]))
7681 break;
7683 /* Start from the beginning in keybuf. */
7684 t = 0;
7686 /* These are no-ops the first time through, but if we restart, they
7687 revert the echo area and this_command_keys to their original state. */
7688 this_command_key_count = keys_start;
7689 if (INTERACTIVE && t < mock_input)
7690 echo_truncate (echo_start);
7692 /* If the best binding for the current key sequence is a keymap, or
7693 we may be looking at a function key's escape sequence, keep on
7694 reading. */
7695 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
7696 || (first_binding >= nmaps
7697 && fkey_start < t
7698 /* mock input is never part of a function key's sequence. */
7699 && mock_input <= fkey_start)
7700 || (first_binding >= nmaps
7701 && keytran_start < t && key_translation_possible)
7702 /* Don't return in the middle of a possible function key sequence,
7703 if the only bindings we found were via case conversion.
7704 Thus, if ESC O a has a function-key-map translation
7705 and ESC o has a binding, don't return after ESC O,
7706 so that we can translate ESC O plus the next character. */
7709 Lisp_Object key;
7710 int used_mouse_menu = 0;
7712 /* Where the last real key started. If we need to throw away a
7713 key that has expanded into more than one element of keybuf
7714 (say, a mouse click on the mode line which is being treated
7715 as [mode-line (mouse-...)], then we backtrack to this point
7716 of keybuf. */
7717 int last_real_key_start;
7719 /* These variables are analogous to echo_start and keys_start;
7720 while those allow us to restart the entire key sequence,
7721 echo_local_start and keys_local_start allow us to throw away
7722 just one key. */
7723 int echo_local_start, keys_local_start, local_first_binding;
7725 if (t >= bufsize)
7726 error ("Key sequence too long");
7728 if (INTERACTIVE)
7729 echo_local_start = echo_length ();
7730 keys_local_start = this_command_key_count;
7731 local_first_binding = first_binding;
7733 replay_key:
7734 /* These are no-ops, unless we throw away a keystroke below and
7735 jumped back up to replay_key; in that case, these restore the
7736 variables to their original state, allowing us to replay the
7737 loop. */
7738 if (INTERACTIVE && t < mock_input)
7739 echo_truncate (echo_local_start);
7740 this_command_key_count = keys_local_start;
7741 first_binding = local_first_binding;
7743 /* By default, assume each event is "real". */
7744 last_real_key_start = t;
7746 /* Does mock_input indicate that we are re-reading a key sequence? */
7747 if (t < mock_input)
7749 key = keybuf[t];
7750 add_command_key (key);
7751 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
7752 && NILP (Fzerop (Vecho_keystrokes)))
7753 echo_char (key);
7756 /* If not, we should actually read a character. */
7757 else
7760 #ifdef MULTI_KBOARD
7761 KBOARD *interrupted_kboard = current_kboard;
7762 struct frame *interrupted_frame = SELECTED_FRAME ();
7763 if (setjmp (wrong_kboard_jmpbuf))
7765 if (!NILP (delayed_switch_frame))
7767 interrupted_kboard->kbd_queue
7768 = Fcons (delayed_switch_frame,
7769 interrupted_kboard->kbd_queue);
7770 delayed_switch_frame = Qnil;
7772 while (t > 0)
7773 interrupted_kboard->kbd_queue
7774 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
7776 /* If the side queue is non-empty, ensure it begins with a
7777 switch-frame, so we'll replay it in the right context. */
7778 if (CONSP (interrupted_kboard->kbd_queue)
7779 && (key = XCAR (interrupted_kboard->kbd_queue),
7780 !(EVENT_HAS_PARAMETERS (key)
7781 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
7782 Qswitch_frame))))
7784 Lisp_Object frame;
7785 XSETFRAME (frame, interrupted_frame);
7786 interrupted_kboard->kbd_queue
7787 = Fcons (make_lispy_switch_frame (frame),
7788 interrupted_kboard->kbd_queue);
7790 mock_input = 0;
7791 orig_local_map = get_local_map (PT, current_buffer, local_map);
7792 orig_keymap = get_local_map (PT, current_buffer, keymap);
7793 goto replay_sequence;
7795 #endif
7796 key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
7797 &used_mouse_menu);
7800 /* read_char returns t when it shows a menu and the user rejects it.
7801 Just return -1. */
7802 if (EQ (key, Qt))
7804 unbind_to (count, Qnil);
7805 return -1;
7808 /* read_char returns -1 at the end of a macro.
7809 Emacs 18 handles this by returning immediately with a
7810 zero, so that's what we'll do. */
7811 if (INTEGERP (key) && XINT (key) == -1)
7813 t = 0;
7814 /* The Microsoft C compiler can't handle the goto that
7815 would go here. */
7816 dummyflag = 1;
7817 break;
7820 /* If the current buffer has been changed from under us, the
7821 keymap may have changed, so replay the sequence. */
7822 if (BUFFERP (key))
7824 mock_input = t;
7825 /* Reset the current buffer from the selected window
7826 in case something changed the former and not the latter.
7827 This is to be more consistent with the behavior
7828 of the command_loop_1. */
7829 if (fix_current_buffer)
7831 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7832 Fkill_emacs (Qnil);
7833 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
7834 Fset_buffer (XWINDOW (selected_window)->buffer);
7837 orig_local_map = get_local_map (PT, current_buffer, local_map);
7838 orig_keymap = get_local_map (PT, current_buffer, keymap);
7839 goto replay_sequence;
7842 /* If we have a quit that was typed in another frame, and
7843 quit_throw_to_read_char switched buffers,
7844 replay to get the right keymap. */
7845 if (XINT (key) == quit_char && current_buffer != starting_buffer)
7847 GROW_RAW_KEYBUF;
7848 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
7849 keybuf[t++] = key;
7850 mock_input = t;
7851 Vquit_flag = Qnil;
7852 orig_local_map = get_local_map (PT, current_buffer, local_map);
7853 orig_keymap = get_local_map (PT, current_buffer, keymap);
7854 goto replay_sequence;
7857 Vquit_flag = Qnil;
7859 if (EVENT_HAS_PARAMETERS (key)
7860 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), Qswitch_frame))
7862 /* If we're at the beginning of a key sequence, and the caller
7863 says it's okay, go ahead and return this event. If we're
7864 in the midst of a key sequence, delay it until the end. */
7865 if (t > 0 || !can_return_switch_frame)
7867 delayed_switch_frame = key;
7868 goto replay_key;
7872 GROW_RAW_KEYBUF;
7873 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
7876 /* Clicks in non-text areas get prefixed by the symbol
7877 in their CHAR-ADDRESS field. For example, a click on
7878 the mode line is prefixed by the symbol `mode-line'.
7880 Furthermore, key sequences beginning with mouse clicks
7881 are read using the keymaps of the buffer clicked on, not
7882 the current buffer. So we may have to switch the buffer
7883 here.
7885 When we turn one event into two events, we must make sure
7886 that neither of the two looks like the original--so that,
7887 if we replay the events, they won't be expanded again.
7888 If not for this, such reexpansion could happen either here
7889 or when user programs play with this-command-keys. */
7890 if (EVENT_HAS_PARAMETERS (key))
7892 Lisp_Object kind;
7894 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
7895 if (EQ (kind, Qmouse_click))
7897 Lisp_Object window, posn;
7899 window = POSN_WINDOW (EVENT_START (key));
7900 posn = POSN_BUFFER_POSN (EVENT_START (key));
7902 if (CONSP (posn))
7904 /* We're looking at the second event of a
7905 sequence which we expanded before. Set
7906 last_real_key_start appropriately. */
7907 if (t > 0)
7908 last_real_key_start = t - 1;
7911 /* Key sequences beginning with mouse clicks are
7912 read using the keymaps in the buffer clicked on,
7913 not the current buffer. If we're at the
7914 beginning of a key sequence, switch buffers. */
7915 if (last_real_key_start == 0
7916 && WINDOWP (window)
7917 && BUFFERP (XWINDOW (window)->buffer)
7918 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
7920 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
7921 keybuf[t] = key;
7922 mock_input = t + 1;
7924 /* Arrange to go back to the original buffer once we're
7925 done reading the key sequence. Note that we can't
7926 use save_excursion_{save,restore} here, because they
7927 save point as well as the current buffer; we don't
7928 want to save point, because redisplay may change it,
7929 to accommodate a Fset_window_start or something. We
7930 don't want to do this at the top of the function,
7931 because we may get input from a subprocess which
7932 wants to change the selected window and stuff (say,
7933 emacsclient). */
7934 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
7936 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7937 Fkill_emacs (Qnil);
7938 set_buffer_internal (XBUFFER (XWINDOW
7939 (window)->buffer)
7941 orig_local_map = get_local_map (PT, current_buffer,
7942 local_map);
7943 orig_keymap = get_local_map (PT, current_buffer, keymap);
7944 goto replay_sequence;
7947 /* For a mouse click, get the local text-property keymap
7948 of the place clicked on, rather than point. */
7949 if (last_real_key_start == 0
7950 && CONSP (XCDR (key))
7951 && ! localized_local_map)
7953 Lisp_Object map_here, start, pos;
7955 localized_local_map = 1;
7956 start = EVENT_START (key);
7958 if (CONSP (start) && CONSP (XCDR (start)))
7960 pos = POSN_BUFFER_POSN (start);
7961 if (INTEGERP (pos)
7962 && XINT (pos) >= BEG && XINT (pos) <= Z)
7964 map_here = get_local_map (XINT (pos),
7965 current_buffer, local_map);
7966 if (!EQ (map_here, orig_local_map))
7968 orig_local_map = map_here;
7969 keybuf[t] = key;
7970 mock_input = t + 1;
7972 goto replay_sequence;
7974 map_here = get_local_map (XINT (pos),
7975 current_buffer, keymap);
7976 if (!EQ (map_here, orig_keymap))
7978 orig_keymap = map_here;
7979 keybuf[t] = key;
7980 mock_input = t + 1;
7982 goto replay_sequence;
7988 /* Expand mode-line and scroll-bar events into two events:
7989 use posn as a fake prefix key. */
7990 if (SYMBOLP (posn))
7992 if (t + 1 >= bufsize)
7993 error ("Key sequence too long");
7994 keybuf[t] = posn;
7995 keybuf[t+1] = key;
7996 mock_input = t + 2;
7998 /* Zap the position in key, so we know that we've
7999 expanded it, and don't try to do so again. */
8000 POSN_BUFFER_POSN (EVENT_START (key))
8001 = Fcons (posn, Qnil);
8003 /* If on a mode line string with a local keymap,
8004 reconsider the key sequence with that keymap. */
8005 if (CONSP (POSN_STRING (EVENT_START (key))))
8007 Lisp_Object string, pos, map, map2;
8009 string = POSN_STRING (EVENT_START (key));
8010 pos = XCDR (string);
8011 string = XCAR (string);
8012 if (XINT (pos) >= 0
8013 && XINT (pos) < XSTRING (string)->size)
8015 map = Fget_text_property (pos, Qlocal_map, string);
8016 if (!NILP (map))
8017 orig_local_map = map;
8018 map2 = Fget_text_property (pos, Qkeymap, string);
8019 if (!NILP (map2))
8020 orig_keymap = map2;
8021 if (!NILP (map) || !NILP (map2))
8022 goto replay_sequence;
8026 goto replay_key;
8029 else if (CONSP (XCDR (key))
8030 && CONSP (EVENT_START (key))
8031 && CONSP (XCDR (EVENT_START (key))))
8033 Lisp_Object posn;
8035 posn = POSN_BUFFER_POSN (EVENT_START (key));
8036 /* Handle menu-bar events:
8037 insert the dummy prefix event `menu-bar'. */
8038 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
8040 if (t + 1 >= bufsize)
8041 error ("Key sequence too long");
8042 keybuf[t] = posn;
8043 keybuf[t+1] = key;
8045 /* Zap the position in key, so we know that we've
8046 expanded it, and don't try to do so again. */
8047 POSN_BUFFER_POSN (EVENT_START (key))
8048 = Fcons (posn, Qnil);
8050 mock_input = t + 2;
8051 goto replay_sequence;
8053 else if (CONSP (posn))
8055 /* We're looking at the second event of a
8056 sequence which we expanded before. Set
8057 last_real_key_start appropriately. */
8058 if (last_real_key_start == t && t > 0)
8059 last_real_key_start = t - 1;
8064 /* We have finally decided that KEY is something we might want
8065 to look up. */
8066 first_binding = (follow_key (key,
8067 nmaps - first_binding,
8068 submaps + first_binding,
8069 defs + first_binding,
8070 submaps + first_binding)
8071 + first_binding);
8073 /* If KEY wasn't bound, we'll try some fallbacks. */
8074 if (first_binding >= nmaps)
8076 Lisp_Object head;
8078 head = EVENT_HEAD (key);
8079 if (help_char_p (head) && t > 0)
8081 read_key_sequence_cmd = Vprefix_help_command;
8082 keybuf[t++] = key;
8083 last_nonmenu_event = key;
8084 /* The Microsoft C compiler can't handle the goto that
8085 would go here. */
8086 dummyflag = 1;
8087 break;
8090 if (SYMBOLP (head))
8092 Lisp_Object breakdown;
8093 int modifiers;
8095 breakdown = parse_modifiers (head);
8096 modifiers = XINT (XCAR (XCDR (breakdown)));
8097 /* Attempt to reduce an unbound mouse event to a simpler
8098 event that is bound:
8099 Drags reduce to clicks.
8100 Double-clicks reduce to clicks.
8101 Triple-clicks reduce to double-clicks, then to clicks.
8102 Down-clicks are eliminated.
8103 Double-downs reduce to downs, then are eliminated.
8104 Triple-downs reduce to double-downs, then to downs,
8105 then are eliminated. */
8106 if (modifiers & (down_modifier | drag_modifier
8107 | double_modifier | triple_modifier))
8109 while (modifiers & (down_modifier | drag_modifier
8110 | double_modifier | triple_modifier))
8112 Lisp_Object new_head, new_click;
8113 if (modifiers & triple_modifier)
8114 modifiers ^= (double_modifier | triple_modifier);
8115 else if (modifiers & double_modifier)
8116 modifiers &= ~double_modifier;
8117 else if (modifiers & drag_modifier)
8118 modifiers &= ~drag_modifier;
8119 else
8121 /* Dispose of this `down' event by simply jumping
8122 back to replay_key, to get another event.
8124 Note that if this event came from mock input,
8125 then just jumping back to replay_key will just
8126 hand it to us again. So we have to wipe out any
8127 mock input.
8129 We could delete keybuf[t] and shift everything
8130 after that to the left by one spot, but we'd also
8131 have to fix up any variable that points into
8132 keybuf, and shifting isn't really necessary
8133 anyway.
8135 Adding prefixes for non-textual mouse clicks
8136 creates two characters of mock input, and both
8137 must be thrown away. If we're only looking at
8138 the prefix now, we can just jump back to
8139 replay_key. On the other hand, if we've already
8140 processed the prefix, and now the actual click
8141 itself is giving us trouble, then we've lost the
8142 state of the keymaps we want to backtrack to, and
8143 we need to replay the whole sequence to rebuild
8146 Beyond that, only function key expansion could
8147 create more than two keys, but that should never
8148 generate mouse events, so it's okay to zero
8149 mock_input in that case too.
8151 Isn't this just the most wonderful code ever? */
8152 if (t == last_real_key_start)
8154 mock_input = 0;
8155 goto replay_key;
8157 else
8159 mock_input = last_real_key_start;
8160 goto replay_sequence;
8164 new_head
8165 = apply_modifiers (modifiers, XCAR (breakdown));
8166 new_click
8167 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
8169 /* Look for a binding for this new key. follow_key
8170 promises that it didn't munge submaps the
8171 last time we called it, since key was unbound. */
8172 first_binding
8173 = (follow_key (new_click,
8174 nmaps - local_first_binding,
8175 submaps + local_first_binding,
8176 defs + local_first_binding,
8177 submaps + local_first_binding)
8178 + local_first_binding);
8180 /* If that click is bound, go for it. */
8181 if (first_binding < nmaps)
8183 key = new_click;
8184 break;
8186 /* Otherwise, we'll leave key set to the drag event. */
8192 keybuf[t++] = key;
8193 /* Normally, last_nonmenu_event gets the previous key we read.
8194 But when a mouse popup menu is being used,
8195 we don't update last_nonmenu_event; it continues to hold the mouse
8196 event that preceded the first level of menu. */
8197 if (!used_mouse_menu)
8198 last_nonmenu_event = key;
8200 /* Record what part of this_command_keys is the current key sequence. */
8201 this_single_command_key_start = this_command_key_count - t;
8203 prev_fkey_map = fkey_map;
8204 prev_fkey_start = fkey_start;
8205 prev_fkey_end = fkey_end;
8207 prev_keytran_map = keytran_map;
8208 prev_keytran_start = keytran_start;
8209 prev_keytran_end = keytran_end;
8211 /* If the sequence is unbound, see if we can hang a function key
8212 off the end of it. We only want to scan real keyboard input
8213 for function key sequences, so if mock_input says that we're
8214 re-reading old events, don't examine it. */
8215 if (first_binding >= nmaps
8216 && t >= mock_input)
8218 Lisp_Object fkey_next;
8220 /* Continue scan from fkey_end until we find a bound suffix.
8221 If we fail, increment fkey_start
8222 and start fkey_end from there. */
8223 while (fkey_end < t)
8225 Lisp_Object key;
8227 key = keybuf[fkey_end++];
8228 /* Look up meta-characters by prefixing them
8229 with meta_prefix_char. I hate this. */
8230 if (INTEGERP (key) && XUINT (key) & meta_modifier)
8232 fkey_next
8233 = get_keymap_1
8234 (get_keyelt
8235 (access_keymap (fkey_map, meta_prefix_char, 1, 0), 0),
8236 0, 1);
8237 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
8239 else
8240 fkey_next = fkey_map;
8242 fkey_next
8243 = get_keyelt (access_keymap (fkey_next, key, 1, 0), 1);
8245 /* Handle symbol with autoload definition. */
8246 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8247 && CONSP (XSYMBOL (fkey_next)->function)
8248 && EQ (XCAR (XSYMBOL (fkey_next)->function), Qautoload))
8249 do_autoload (XSYMBOL (fkey_next)->function,
8250 fkey_next);
8252 /* Handle a symbol whose function definition is a keymap
8253 or an array. */
8254 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8255 && (!NILP (Farrayp (XSYMBOL (fkey_next)->function))
8256 || !NILP (Fkeymapp (XSYMBOL (fkey_next)->function))))
8257 fkey_next = XSYMBOL (fkey_next)->function;
8259 #if 0 /* I didn't turn this on, because it might cause trouble
8260 for the mapping of return into C-m and tab into C-i. */
8261 /* Optionally don't map function keys into other things.
8262 This enables the user to redefine kp- keys easily. */
8263 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
8264 fkey_next = Qnil;
8265 #endif
8267 /* If the function key map gives a function, not an
8268 array, then call the function with no args and use
8269 its value instead. */
8270 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8271 && fkey_end == t)
8273 struct gcpro gcpro1, gcpro2, gcpro3;
8274 Lisp_Object tem;
8275 tem = fkey_next;
8277 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
8278 fkey_next = call1 (fkey_next, prompt);
8279 UNGCPRO;
8280 /* If the function returned something invalid,
8281 barf--don't ignore it.
8282 (To ignore it safely, we would need to gcpro a bunch of
8283 other variables.) */
8284 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
8285 error ("Function in key-translation-map returns invalid key sequence");
8288 function_key_possible = ! NILP (fkey_next);
8290 /* If keybuf[fkey_start..fkey_end] is bound in the
8291 function key map and it's a suffix of the current
8292 sequence (i.e. fkey_end == t), replace it with
8293 the binding and restart with fkey_start at the end. */
8294 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
8295 && fkey_end == t)
8297 int len = XFASTINT (Flength (fkey_next));
8299 t = fkey_start + len;
8300 if (t >= bufsize)
8301 error ("Key sequence too long");
8303 if (VECTORP (fkey_next))
8304 bcopy (XVECTOR (fkey_next)->contents,
8305 keybuf + fkey_start,
8306 (t - fkey_start) * sizeof (keybuf[0]));
8307 else if (STRINGP (fkey_next))
8309 int i;
8311 for (i = 0; i < len; i++)
8312 XSETFASTINT (keybuf[fkey_start + i],
8313 XSTRING (fkey_next)->data[i]);
8316 mock_input = t;
8317 fkey_start = fkey_end = t;
8318 fkey_map = Vfunction_key_map;
8320 /* Do pass the results through key-translation-map.
8321 But don't retranslate what key-translation-map
8322 has already translated. */
8323 keytran_end = keytran_start;
8324 keytran_map = Vkey_translation_map;
8326 goto replay_sequence;
8329 fkey_map = get_keymap_1 (fkey_next, 0, 1);
8331 /* If we no longer have a bound suffix, try a new positions for
8332 fkey_start. */
8333 if (NILP (fkey_map))
8335 fkey_end = ++fkey_start;
8336 fkey_map = Vfunction_key_map;
8337 function_key_possible = 0;
8342 /* Look for this sequence in key-translation-map. */
8344 Lisp_Object keytran_next;
8346 /* Scan from keytran_end until we find a bound suffix. */
8347 while (keytran_end < t)
8349 Lisp_Object key;
8351 key = keybuf[keytran_end++];
8352 /* Look up meta-characters by prefixing them
8353 with meta_prefix_char. I hate this. */
8354 if (INTEGERP (key) && XUINT (key) & meta_modifier)
8356 keytran_next
8357 = get_keymap_1
8358 (get_keyelt
8359 (access_keymap (keytran_map, meta_prefix_char, 1, 0), 0),
8360 0, 1);
8361 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
8363 else
8364 keytran_next = keytran_map;
8366 keytran_next
8367 = get_keyelt (access_keymap (keytran_next, key, 1, 0), 1);
8369 /* Handle symbol with autoload definition. */
8370 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8371 && CONSP (XSYMBOL (keytran_next)->function)
8372 && EQ (XCAR (XSYMBOL (keytran_next)->function), Qautoload))
8373 do_autoload (XSYMBOL (keytran_next)->function,
8374 keytran_next);
8376 /* Handle a symbol whose function definition is a keymap
8377 or an array. */
8378 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8379 && (!NILP (Farrayp (XSYMBOL (keytran_next)->function))
8380 || !NILP (Fkeymapp (XSYMBOL (keytran_next)->function))))
8381 keytran_next = XSYMBOL (keytran_next)->function;
8383 /* If the key translation map gives a function, not an
8384 array, then call the function with one arg and use
8385 its value instead. */
8386 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8387 && keytran_end == t)
8389 struct gcpro gcpro1, gcpro2, gcpro3;
8390 Lisp_Object tem;
8391 tem = keytran_next;
8393 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
8394 keytran_next = call1 (keytran_next, prompt);
8395 UNGCPRO;
8396 /* If the function returned something invalid,
8397 barf--don't ignore it.
8398 (To ignore it safely, we would need to gcpro a bunch of
8399 other variables.) */
8400 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
8401 error ("Function in key-translation-map returns invalid key sequence");
8404 key_translation_possible = ! NILP (keytran_next);
8406 /* If keybuf[keytran_start..keytran_end] is bound in the
8407 key translation map and it's a suffix of the current
8408 sequence (i.e. keytran_end == t), replace it with
8409 the binding and restart with keytran_start at the end. */
8410 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
8411 && keytran_end == t)
8413 int len = XFASTINT (Flength (keytran_next));
8415 t = keytran_start + len;
8416 if (t >= bufsize)
8417 error ("Key sequence too long");
8419 if (VECTORP (keytran_next))
8420 bcopy (XVECTOR (keytran_next)->contents,
8421 keybuf + keytran_start,
8422 (t - keytran_start) * sizeof (keybuf[0]));
8423 else if (STRINGP (keytran_next))
8425 int i;
8427 for (i = 0; i < len; i++)
8428 XSETFASTINT (keybuf[keytran_start + i],
8429 XSTRING (keytran_next)->data[i]);
8432 mock_input = t;
8433 keytran_start = keytran_end = t;
8434 keytran_map = Vkey_translation_map;
8436 /* Don't pass the results of key-translation-map
8437 through function-key-map. */
8438 fkey_start = fkey_end = t;
8439 fkey_map = Vfunction_key_map;
8441 goto replay_sequence;
8444 keytran_map = get_keymap_1 (keytran_next, 0, 1);
8446 /* If we no longer have a bound suffix, try a new positions for
8447 keytran_start. */
8448 if (NILP (keytran_map))
8450 keytran_end = ++keytran_start;
8451 keytran_map = Vkey_translation_map;
8452 key_translation_possible = 0;
8457 /* If KEY is not defined in any of the keymaps,
8458 and cannot be part of a function key or translation,
8459 and is an upper case letter
8460 use the corresponding lower-case letter instead. */
8461 if (first_binding == nmaps && ! function_key_possible
8462 && ! key_translation_possible
8463 && INTEGERP (key)
8464 && ((((XINT (key) & 0x3ffff)
8465 < XCHAR_TABLE (current_buffer->downcase_table)->size)
8466 && UPPERCASEP (XINT (key) & 0x3ffff))
8467 || (XINT (key) & shift_modifier)))
8469 Lisp_Object new_key;
8471 original_uppercase = key;
8472 original_uppercase_position = t - 1;
8474 if (XINT (key) & shift_modifier)
8475 XSETINT (new_key, XINT (key) & ~shift_modifier);
8476 else
8477 XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
8478 | (XINT (key) & ~0x3ffff)));
8480 /* We have to do this unconditionally, regardless of whether
8481 the lower-case char is defined in the keymaps, because they
8482 might get translated through function-key-map. */
8483 keybuf[t - 1] = new_key;
8484 mock_input = t;
8486 fkey_map = prev_fkey_map;
8487 fkey_start = prev_fkey_start;
8488 fkey_end = prev_fkey_end;
8490 keytran_map = prev_keytran_map;
8491 keytran_start = prev_keytran_start;
8492 keytran_end = prev_keytran_end;
8494 goto replay_sequence;
8496 /* If KEY is not defined in any of the keymaps,
8497 and cannot be part of a function key or translation,
8498 and is a shifted function key,
8499 use the corresponding unshifted function key instead. */
8500 if (first_binding == nmaps && ! function_key_possible
8501 && ! key_translation_possible
8502 && SYMBOLP (key))
8504 Lisp_Object breakdown;
8505 int modifiers;
8507 breakdown = parse_modifiers (key);
8508 modifiers = XINT (XCAR (XCDR (breakdown)));
8509 if (modifiers & shift_modifier)
8511 Lisp_Object new_key;
8513 original_uppercase = key;
8514 original_uppercase_position = t - 1;
8516 modifiers &= ~shift_modifier;
8517 new_key = apply_modifiers (modifiers,
8518 XCAR (breakdown));
8520 keybuf[t - 1] = new_key;
8521 mock_input = t;
8523 fkey_map = prev_fkey_map;
8524 fkey_start = prev_fkey_start;
8525 fkey_end = prev_fkey_end;
8527 keytran_map = prev_keytran_map;
8528 keytran_start = prev_keytran_start;
8529 keytran_end = prev_keytran_end;
8531 goto replay_sequence;
8536 if (!dummyflag)
8537 read_key_sequence_cmd = (first_binding < nmaps
8538 ? defs[first_binding]
8539 : Qnil);
8541 unread_switch_frame = delayed_switch_frame;
8542 unbind_to (count, Qnil);
8544 /* Don't downcase the last character if the caller says don't.
8545 Don't downcase it if the result is undefined, either. */
8546 if ((dont_downcase_last || first_binding >= nmaps)
8547 && t - 1 == original_uppercase_position)
8548 keybuf[t - 1] = original_uppercase;
8550 /* Occasionally we fabricate events, perhaps by expanding something
8551 according to function-key-map, or by adding a prefix symbol to a
8552 mouse click in the scroll bar or modeline. In this cases, return
8553 the entire generated key sequence, even if we hit an unbound
8554 prefix or a definition before the end. This means that you will
8555 be able to push back the event properly, and also means that
8556 read-key-sequence will always return a logical unit.
8558 Better ideas? */
8559 for (; t < mock_input; t++)
8561 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
8562 && NILP (Fzerop (Vecho_keystrokes)))
8563 echo_char (keybuf[t]);
8564 add_command_key (keybuf[t]);
8569 return t;
8572 #if 0 /* This doc string is too long for some compilers.
8573 This commented-out definition serves for DOC. */
8574 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
8575 "Read a sequence of keystrokes and return as a string or vector.\n\
8576 The sequence is sufficient to specify a non-prefix command in the\n\
8577 current local and global maps.\n\
8579 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
8580 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
8581 as a continuation of the previous key.\n\
8583 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
8584 convert the last event to lower case. (Normally any upper case event\n\
8585 is converted to lower case if the original event is undefined and the lower\n\
8586 case equivalent is defined.) A non-nil value is appropriate for reading\n\
8587 a key sequence to be defined.\n\
8589 A C-g typed while in this function is treated like any other character,\n\
8590 and `quit-flag' is not set.\n\
8592 If the key sequence starts with a mouse click, then the sequence is read\n\
8593 using the keymaps of the buffer of the window clicked in, not the buffer\n\
8594 of the selected window as normal.\n\
8595 ""\n\
8596 `read-key-sequence' drops unbound button-down events, since you normally\n\
8597 only care about the click or drag events which follow them. If a drag\n\
8598 or multi-click event is unbound, but the corresponding click event would\n\
8599 be bound, `read-key-sequence' turns the event into a click event at the\n\
8600 drag's starting position. This means that you don't have to distinguish\n\
8601 between click and drag, double, or triple events unless you want to.\n\
8603 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
8604 lines separating windows, and scroll bars with imaginary keys\n\
8605 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
8607 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
8608 function will process a switch-frame event if the user switches frames\n\
8609 before typing anything. If the user switches frames in the middle of a\n\
8610 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
8611 is nil, then the event will be put off until after the current key sequence.\n\
8613 `read-key-sequence' checks `function-key-map' for function key\n\
8614 sequences, where they wouldn't conflict with ordinary bindings. See\n\
8615 `function-key-map' for more details.\n\
8617 The optional fifth argument COMMAND-LOOP, if non-nil, means\n\
8618 that this key sequence is being read by something that will\n\
8619 read commands one after another. It should be nil if the caller\n\
8620 will read just one key sequence.")
8621 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame, command-loop)
8622 #endif
8624 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 5, 0,
8626 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
8627 command_loop)
8628 Lisp_Object prompt, continue_echo, dont_downcase_last;
8629 Lisp_Object can_return_switch_frame, command_loop;
8631 Lisp_Object keybuf[30];
8632 register int i;
8633 struct gcpro gcpro1;
8634 int count = specpdl_ptr - specpdl;
8636 if (!NILP (prompt))
8637 CHECK_STRING (prompt, 0);
8638 QUIT;
8640 specbind (Qinput_method_exit_on_first_char,
8641 (NILP (command_loop) ? Qt : Qnil));
8642 specbind (Qinput_method_use_echo_area,
8643 (NILP (command_loop) ? Qt : Qnil));
8645 bzero (keybuf, sizeof keybuf);
8646 GCPRO1 (keybuf[0]);
8647 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
8649 if (NILP (continue_echo))
8651 this_command_key_count = 0;
8652 this_single_command_key_start = 0;
8655 #ifdef HAVE_X_WINDOWS
8656 if (display_busy_cursor_p)
8657 cancel_busy_cursor ();
8658 #endif
8660 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
8661 prompt, ! NILP (dont_downcase_last),
8662 ! NILP (can_return_switch_frame), 0);
8664 #ifdef HAVE_X_WINDOWS
8665 if (display_busy_cursor_p)
8666 start_busy_cursor ();
8667 #endif
8669 if (i == -1)
8671 Vquit_flag = Qt;
8672 QUIT;
8674 UNGCPRO;
8675 return unbind_to (count, make_event_array (i, keybuf));
8678 DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
8679 Sread_key_sequence_vector, 1, 5, 0,
8680 "Like `read-key-sequence' but always return a vector.")
8681 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
8682 command_loop)
8683 Lisp_Object prompt, continue_echo, dont_downcase_last;
8684 Lisp_Object can_return_switch_frame, command_loop;
8686 Lisp_Object keybuf[30];
8687 register int i;
8688 struct gcpro gcpro1;
8689 int count = specpdl_ptr - specpdl;
8691 if (!NILP (prompt))
8692 CHECK_STRING (prompt, 0);
8693 QUIT;
8695 specbind (Qinput_method_exit_on_first_char,
8696 (NILP (command_loop) ? Qt : Qnil));
8697 specbind (Qinput_method_use_echo_area,
8698 (NILP (command_loop) ? Qt : Qnil));
8700 bzero (keybuf, sizeof keybuf);
8701 GCPRO1 (keybuf[0]);
8702 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
8704 if (NILP (continue_echo))
8706 this_command_key_count = 0;
8707 this_single_command_key_start = 0;
8710 #ifdef HAVE_X_WINDOWS
8711 if (display_busy_cursor_p)
8712 cancel_busy_cursor ();
8713 #endif
8715 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
8716 prompt, ! NILP (dont_downcase_last),
8717 ! NILP (can_return_switch_frame), 0);
8719 #ifdef HAVE_X_WINDOWS
8720 if (display_busy_cursor_p)
8721 start_busy_cursor ();
8722 #endif
8724 if (i == -1)
8726 Vquit_flag = Qt;
8727 QUIT;
8729 UNGCPRO;
8730 return unbind_to (count, Fvector (i, keybuf));
8733 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
8734 "Execute CMD as an editor command.\n\
8735 CMD must be a symbol that satisfies the `commandp' predicate.\n\
8736 Optional second arg RECORD-FLAG non-nil\n\
8737 means unconditionally put this command in `command-history'.\n\
8738 Otherwise, that is done only if an arg is read using the minibuffer.\n\
8739 The argument KEYS specifies the value to use instead of (this-command-keys)\n\
8740 when reading the arguments; if it is nil, (this-command-keys) is used.\n\
8741 The argument SPECIAL, if non-nil, means that this command is executing\n\
8742 a special event, so ignore the prefix argument and don't clear it.")
8743 (cmd, record_flag, keys, special)
8744 Lisp_Object cmd, record_flag, keys, special;
8746 register Lisp_Object final;
8747 register Lisp_Object tem;
8748 Lisp_Object prefixarg;
8749 struct backtrace backtrace;
8750 extern int debug_on_next_call;
8752 debug_on_next_call = 0;
8754 if (NILP (special))
8756 prefixarg = current_kboard->Vprefix_arg;
8757 Vcurrent_prefix_arg = prefixarg;
8758 current_kboard->Vprefix_arg = Qnil;
8760 else
8761 prefixarg = Qnil;
8763 if (SYMBOLP (cmd))
8765 tem = Fget (cmd, Qdisabled);
8766 if (!NILP (tem) && !NILP (Vrun_hooks))
8768 tem = Fsymbol_value (Qdisabled_command_hook);
8769 if (!NILP (tem))
8770 return call1 (Vrun_hooks, Qdisabled_command_hook);
8774 while (1)
8776 final = Findirect_function (cmd);
8778 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
8780 struct gcpro gcpro1, gcpro2;
8782 GCPRO2 (cmd, prefixarg);
8783 do_autoload (final, cmd);
8784 UNGCPRO;
8786 else
8787 break;
8790 if (STRINGP (final) || VECTORP (final))
8792 /* If requested, place the macro in the command history. For
8793 other sorts of commands, call-interactively takes care of
8794 this. */
8795 if (!NILP (record_flag))
8797 Vcommand_history
8798 = Fcons (Fcons (Qexecute_kbd_macro,
8799 Fcons (final, Fcons (prefixarg, Qnil))),
8800 Vcommand_history);
8802 /* Don't keep command history around forever. */
8803 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
8805 tem = Fnthcdr (Vhistory_length, Vcommand_history);
8806 if (CONSP (tem))
8807 XCDR (tem) = Qnil;
8811 return Fexecute_kbd_macro (final, prefixarg);
8814 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
8816 backtrace.next = backtrace_list;
8817 backtrace_list = &backtrace;
8818 backtrace.function = &Qcall_interactively;
8819 backtrace.args = &cmd;
8820 backtrace.nargs = 1;
8821 backtrace.evalargs = 0;
8823 tem = Fcall_interactively (cmd, record_flag, keys);
8825 backtrace_list = backtrace.next;
8826 return tem;
8828 return Qnil;
8831 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
8832 1, 1, "P",
8833 "Read function name, then read its arguments and call it.")
8834 (prefixarg)
8835 Lisp_Object prefixarg;
8837 Lisp_Object function;
8838 char buf[40];
8839 Lisp_Object saved_keys;
8840 Lisp_Object bindings, value;
8841 struct gcpro gcpro1, gcpro2;
8843 saved_keys = Fvector (this_command_key_count,
8844 XVECTOR (this_command_keys)->contents);
8845 buf[0] = 0;
8846 GCPRO2 (saved_keys, prefixarg);
8848 if (EQ (prefixarg, Qminus))
8849 strcpy (buf, "- ");
8850 else if (CONSP (prefixarg) && XINT (XCAR (prefixarg)) == 4)
8851 strcpy (buf, "C-u ");
8852 else if (CONSP (prefixarg) && INTEGERP (XCAR (prefixarg)))
8854 if (sizeof (int) == sizeof (EMACS_INT))
8855 sprintf (buf, "%d ", XINT (XCAR (prefixarg)));
8856 else if (sizeof (long) == sizeof (EMACS_INT))
8857 sprintf (buf, "%ld ", (long) XINT (XCAR (prefixarg)));
8858 else
8859 abort ();
8861 else if (INTEGERP (prefixarg))
8863 if (sizeof (int) == sizeof (EMACS_INT))
8864 sprintf (buf, "%d ", XINT (prefixarg));
8865 else if (sizeof (long) == sizeof (EMACS_INT))
8866 sprintf (buf, "%ld ", (long) XINT (prefixarg));
8867 else
8868 abort ();
8871 /* This isn't strictly correct if execute-extended-command
8872 is bound to anything else. Perhaps it should use
8873 this_command_keys? */
8874 strcat (buf, "M-x ");
8876 /* Prompt with buf, and then read a string, completing from and
8877 restricting to the set of all defined commands. Don't provide
8878 any initial input. Save the command read on the extended-command
8879 history list. */
8880 function = Fcompleting_read (build_string (buf),
8881 Vobarray, Qcommandp,
8882 Qt, Qnil, Qextended_command_history, Qnil,
8883 Qnil);
8885 if (STRINGP (function) && XSTRING (function)->size == 0)
8886 error ("No command name given");
8888 /* Set this_command_keys to the concatenation of saved_keys and
8889 function, followed by a RET. */
8891 struct Lisp_String *str;
8892 Lisp_Object *keys;
8893 int i;
8895 this_command_key_count = 0;
8896 this_single_command_key_start = 0;
8898 keys = XVECTOR (saved_keys)->contents;
8899 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
8900 add_command_key (keys[i]);
8902 str = XSTRING (function);
8903 for (i = 0; i < str->size; i++)
8904 add_command_key (Faref (function, make_number (i)));
8906 add_command_key (make_number ('\015'));
8909 UNGCPRO;
8911 function = Fintern (function, Qnil);
8912 current_kboard->Vprefix_arg = prefixarg;
8913 Vthis_command = function;
8914 real_this_command = function;
8916 /* If enabled, show which key runs this command. */
8917 if (!NILP (Vsuggest_key_bindings)
8918 && NILP (Vexecuting_macro)
8919 && SYMBOLP (function))
8920 bindings = Fwhere_is_internal (function, Voverriding_local_map,
8921 Qt, Qnil);
8922 else
8923 bindings = Qnil;
8925 value = Qnil;
8926 GCPRO2 (bindings, value);
8927 value = Fcommand_execute (function, Qt, Qnil, Qnil);
8929 /* If the command has a key binding, print it now. */
8930 if (!NILP (bindings)
8931 && ! (VECTORP (bindings) && EQ (Faref (bindings, make_number (0)),
8932 Qmouse_movement)))
8934 /* But first wait, and skip the message if there is input. */
8935 int delay_time;
8936 if (!NILP (echo_area_buffer[0]))
8937 /* This command displayed something in the echo area;
8938 so wait a few seconds, then display our suggestion message. */
8939 delay_time = (NUMBERP (Vsuggest_key_bindings)
8940 ? XINT (Vsuggest_key_bindings) : 2);
8941 else
8942 /* This command left the echo area empty,
8943 so display our message immediately. */
8944 delay_time = 0;
8946 if (!NILP (Fsit_for (make_number (delay_time), Qnil, Qnil))
8947 && ! CONSP (Vunread_command_events))
8949 Lisp_Object binding;
8950 char *newmessage;
8951 int message_p = push_message ();
8953 binding = Fkey_description (bindings);
8955 newmessage
8956 = (char *) alloca (XSYMBOL (function)->name->size
8957 + STRING_BYTES (XSTRING (binding))
8958 + 100);
8959 sprintf (newmessage, "You can run the command `%s' with %s",
8960 XSYMBOL (function)->name->data,
8961 XSTRING (binding)->data);
8962 message2_nolog (newmessage,
8963 strlen (newmessage),
8964 STRING_MULTIBYTE (binding));
8965 if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
8966 ? Vsuggest_key_bindings : make_number (2)),
8967 Qnil, Qnil))
8968 && message_p)
8969 restore_message ();
8971 pop_message ();
8975 RETURN_UNGCPRO (value);
8978 /* Find the set of keymaps now active.
8979 Store into *MAPS_P a vector holding the various maps
8980 and return the number of them. The vector was malloc'd
8981 and the caller should free it. */
8984 current_active_maps (maps_p)
8985 Lisp_Object **maps_p;
8987 Lisp_Object *tmaps, *maps;
8988 int nmaps;
8990 /* Should overriding-terminal-local-map and overriding-local-map apply? */
8991 if (!NILP (Voverriding_local_map_menu_flag))
8993 /* Yes, use them (if non-nil) as well as the global map. */
8994 maps = (Lisp_Object *) xmalloc (3 * sizeof (maps[0]));
8995 nmaps = 0;
8996 if (!NILP (current_kboard->Voverriding_terminal_local_map))
8997 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
8998 if (!NILP (Voverriding_local_map))
8999 maps[nmaps++] = Voverriding_local_map;
9001 else
9003 /* No, so use major and minor mode keymaps and keymap property. */
9004 int extra_maps = 2;
9005 Lisp_Object map = get_local_map (PT, current_buffer, keymap);
9006 if (!NILP (map))
9007 extra_maps = 3;
9008 nmaps = current_minor_maps (NULL, &tmaps);
9009 maps = (Lisp_Object *) alloca ((nmaps + extra_maps)
9010 * sizeof (maps[0]));
9011 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
9012 if (!NILP (map))
9013 maps[nmaps++] = get_local_map (PT, current_buffer, keymap);
9014 maps[nmaps++] = get_local_map (PT, current_buffer, local_map);
9016 maps[nmaps++] = current_global_map;
9018 *maps_p = maps;
9019 return nmaps;
9022 /* Return nonzero if input events are pending. */
9025 detect_input_pending ()
9027 if (!input_pending)
9028 get_input_pending (&input_pending, 0);
9030 return input_pending;
9033 /* Return nonzero if input events are pending, and run any pending timers. */
9036 detect_input_pending_run_timers (do_display)
9037 int do_display;
9039 int old_timers_run = timers_run;
9041 if (!input_pending)
9042 get_input_pending (&input_pending, 1);
9044 if (old_timers_run != timers_run && do_display)
9046 redisplay_preserve_echo_area ();
9047 /* The following fixes a bug when using lazy-lock with
9048 lazy-lock-defer-on-the-fly set to t, i.e. when fontifying
9049 from an idle timer function. The symptom of the bug is that
9050 the cursor sometimes doesn't become visible until the next X
9051 event is processed. --gerd. */
9052 if (rif)
9053 rif->flush_display (NULL);
9056 return input_pending;
9059 /* This is called in some cases before a possible quit.
9060 It cases the next call to detect_input_pending to recompute input_pending.
9061 So calling this function unnecessarily can't do any harm. */
9063 void
9064 clear_input_pending ()
9066 input_pending = 0;
9069 /* Return nonzero if there are pending requeued events.
9070 This isn't used yet. The hope is to make wait_reading_process_input
9071 call it, and return return if it runs Lisp code that unreads something.
9072 The problem is, kbd_buffer_get_event needs to be fixed to know what
9073 to do in that case. It isn't trivial. */
9076 requeued_events_pending_p ()
9078 return (!NILP (Vunread_command_events) || unread_command_char != -1);
9082 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
9083 "T if command input is currently available with no waiting.\n\
9084 Actually, the value is nil only if we can be sure that no input is available.")
9087 if (!NILP (Vunread_command_events) || unread_command_char != -1)
9088 return (Qt);
9090 get_input_pending (&input_pending, 1);
9091 return input_pending > 0 ? Qt : Qnil;
9094 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
9095 "Return vector of last 100 events, not counting those from keyboard macros.")
9098 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
9099 Lisp_Object val;
9101 if (total_keys < NUM_RECENT_KEYS)
9102 return Fvector (total_keys, keys);
9103 else
9105 val = Fvector (NUM_RECENT_KEYS, keys);
9106 bcopy (keys + recent_keys_index,
9107 XVECTOR (val)->contents,
9108 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
9109 bcopy (keys,
9110 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
9111 recent_keys_index * sizeof (Lisp_Object));
9112 return val;
9116 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
9117 "Return the key sequence that invoked this command.\n\
9118 The value is a string or a vector.")
9121 return make_event_array (this_command_key_count,
9122 XVECTOR (this_command_keys)->contents);
9125 DEFUN ("this-command-keys-vector", Fthis_command_keys_vector, Sthis_command_keys_vector, 0, 0, 0,
9126 "Return the key sequence that invoked this command, as a vector.")
9129 return Fvector (this_command_key_count,
9130 XVECTOR (this_command_keys)->contents);
9133 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
9134 Sthis_single_command_keys, 0, 0, 0,
9135 "Return the key sequence that invoked this command.\n\
9136 Unlike `this-command-keys', this function's value\n\
9137 does not include prefix arguments.\n\
9138 The value is always a vector.")
9141 return Fvector (this_command_key_count
9142 - this_single_command_key_start,
9143 (XVECTOR (this_command_keys)->contents
9144 + this_single_command_key_start));
9147 DEFUN ("this-single-command-raw-keys", Fthis_single_command_raw_keys,
9148 Sthis_single_command_raw_keys, 0, 0, 0,
9149 "Return the raw events that were read for this command.\n\
9150 Unlike `this-single-command-keys', this function's value\n\
9151 shows the events before all translations (except for input methods).\n\
9152 The value is always a vector.")
9155 return Fvector (raw_keybuf_count,
9156 (XVECTOR (raw_keybuf)->contents));
9159 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
9160 Sreset_this_command_lengths, 0, 0, 0,
9161 "Used for complicated reasons in `universal-argument-other-key'.\n\
9163 `universal-argument-other-key' rereads the event just typed.\n\
9164 It then gets translated through `function-key-map'.\n\
9165 The translated event gets included in the echo area and in\n\
9166 the value of `this-command-keys' in addition to the raw original event.\n\
9167 That is not right.\n\
9169 Calling this function directs the translated event to replace\n\
9170 the original event, so that only one version of the event actually\n\
9171 appears in the echo area and in the value of `this-command-keys.'.")
9174 before_command_restore_flag = 1;
9175 before_command_key_count_1 = before_command_key_count;
9176 before_command_echo_length_1 = before_command_echo_length;
9177 return Qnil;
9180 DEFUN ("clear-this-command-keys", Fclear_this_command_keys,
9181 Sclear_this_command_keys, 0, 0, 0,
9182 "Clear out the vector that `this-command-keys' returns.\n\
9183 Clear vector containing last 100 events.")
9186 int i;
9188 this_command_key_count = 0;
9190 for (i = 0; i < XVECTOR (recent_keys)->size; ++i)
9191 XVECTOR (recent_keys)->contents[i] = Qnil;
9192 total_keys = 0;
9193 recent_keys_index = 0;
9194 return Qnil;
9197 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
9198 "Return the current depth in recursive edits.")
9201 Lisp_Object temp;
9202 XSETFASTINT (temp, command_loop_level + minibuf_level);
9203 return temp;
9206 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
9207 "FOpen dribble file: ",
9208 "Start writing all keyboard characters to a dribble file called FILE.\n\
9209 If FILE is nil, close any open dribble file.")
9210 (file)
9211 Lisp_Object file;
9213 if (dribble)
9215 fclose (dribble);
9216 dribble = 0;
9218 if (!NILP (file))
9220 file = Fexpand_file_name (file, Qnil);
9221 dribble = fopen (XSTRING (file)->data, "w");
9222 if (dribble == 0)
9223 report_file_error ("Opening dribble", Fcons (file, Qnil));
9225 return Qnil;
9228 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
9229 "Discard the contents of the terminal input buffer.\n\
9230 Also cancel any kbd macro being defined.")
9233 current_kboard->defining_kbd_macro = Qnil;
9234 update_mode_lines++;
9236 Vunread_command_events = Qnil;
9237 unread_command_char = -1;
9239 discard_tty_input ();
9241 kbd_fetch_ptr = kbd_store_ptr;
9242 Ffillarray (kbd_buffer_gcpro, Qnil);
9243 input_pending = 0;
9245 return Qnil;
9248 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
9249 "Stop Emacs and return to superior process. You can resume later.\n\
9250 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
9251 control, run a subshell instead.\n\n\
9252 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
9253 to be read as terminal input by Emacs's parent, after suspension.\n\
9255 Before suspending, run the normal hook `suspend-hook'.\n\
9256 After resumption run the normal hook `suspend-resume-hook'.\n\
9258 Some operating systems cannot stop the Emacs process and resume it later.\n\
9259 On such systems, Emacs starts a subshell instead of suspending.")
9260 (stuffstring)
9261 Lisp_Object stuffstring;
9263 int count = specpdl_ptr - specpdl;
9264 int old_height, old_width;
9265 int width, height;
9266 struct gcpro gcpro1;
9268 if (!NILP (stuffstring))
9269 CHECK_STRING (stuffstring, 0);
9271 /* Run the functions in suspend-hook. */
9272 if (!NILP (Vrun_hooks))
9273 call1 (Vrun_hooks, intern ("suspend-hook"));
9275 GCPRO1 (stuffstring);
9276 get_frame_size (&old_width, &old_height);
9277 reset_sys_modes ();
9278 /* sys_suspend can get an error if it tries to fork a subshell
9279 and the system resources aren't available for that. */
9280 record_unwind_protect ((Lisp_Object (*) P_ ((Lisp_Object))) init_sys_modes,
9281 Qnil);
9282 stuff_buffered_input (stuffstring);
9283 if (cannot_suspend)
9284 sys_subshell ();
9285 else
9286 sys_suspend ();
9287 unbind_to (count, Qnil);
9289 /* Check if terminal/window size has changed.
9290 Note that this is not useful when we are running directly
9291 with a window system; but suspend should be disabled in that case. */
9292 get_frame_size (&width, &height);
9293 if (width != old_width || height != old_height)
9294 change_frame_size (SELECTED_FRAME (), height, width, 0, 0, 0);
9296 /* Run suspend-resume-hook. */
9297 if (!NILP (Vrun_hooks))
9298 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
9300 UNGCPRO;
9301 return Qnil;
9304 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
9305 Then in any case stuff anything Emacs has read ahead and not used. */
9307 void
9308 stuff_buffered_input (stuffstring)
9309 Lisp_Object stuffstring;
9311 /* stuff_char works only in BSD, versions 4.2 and up. */
9312 #ifdef BSD_SYSTEM
9313 #ifndef BSD4_1
9314 register unsigned char *p;
9316 if (STRINGP (stuffstring))
9318 register int count;
9320 p = XSTRING (stuffstring)->data;
9321 count = STRING_BYTES (XSTRING (stuffstring));
9322 while (count-- > 0)
9323 stuff_char (*p++);
9324 stuff_char ('\n');
9327 /* Anything we have read ahead, put back for the shell to read. */
9328 /* ?? What should this do when we have multiple keyboards??
9329 Should we ignore anything that was typed in at the "wrong" kboard? */
9330 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
9332 int idx;
9334 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
9335 kbd_fetch_ptr = kbd_buffer;
9336 if (kbd_fetch_ptr->kind == ascii_keystroke)
9337 stuff_char (kbd_fetch_ptr->code);
9339 kbd_fetch_ptr->kind = no_event;
9340 idx = 2 * (kbd_fetch_ptr - kbd_buffer);
9341 ASET (kbd_buffer_gcpro, idx, Qnil);
9342 ASET (kbd_buffer_gcpro, idx + 1, Qnil);
9345 input_pending = 0;
9346 #endif
9347 #endif /* BSD_SYSTEM and not BSD4_1 */
9350 void
9351 set_waiting_for_input (time_to_clear)
9352 EMACS_TIME *time_to_clear;
9354 input_available_clear_time = time_to_clear;
9356 /* Tell interrupt_signal to throw back to read_char, */
9357 waiting_for_input = 1;
9359 /* If interrupt_signal was called before and buffered a C-g,
9360 make it run again now, to avoid timing error. */
9361 if (!NILP (Vquit_flag))
9362 quit_throw_to_read_char ();
9365 void
9366 clear_waiting_for_input ()
9368 /* Tell interrupt_signal not to throw back to read_char, */
9369 waiting_for_input = 0;
9370 input_available_clear_time = 0;
9373 /* This routine is called at interrupt level in response to C-G.
9374 If interrupt_input, this is the handler for SIGINT.
9375 Otherwise, it is called from kbd_buffer_store_event,
9376 in handling SIGIO or SIGTINT.
9378 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
9379 immediately throw back to read_char.
9381 Otherwise it sets the Lisp variable quit-flag not-nil.
9382 This causes eval to throw, when it gets a chance.
9383 If quit-flag is already non-nil, it stops the job right away. */
9385 SIGTYPE
9386 interrupt_signal (signalnum) /* If we don't have an argument, */
9387 int signalnum; /* some compilers complain in signal calls. */
9389 char c;
9390 /* Must preserve main program's value of errno. */
9391 int old_errno = errno;
9392 struct frame *sf = SELECTED_FRAME ();
9394 #if defined (USG) && !defined (POSIX_SIGNALS)
9395 if (!read_socket_hook && NILP (Vwindow_system))
9397 /* USG systems forget handlers when they are used;
9398 must reestablish each time */
9399 signal (SIGINT, interrupt_signal);
9400 signal (SIGQUIT, interrupt_signal);
9402 #endif /* USG */
9404 cancel_echoing ();
9406 if (!NILP (Vquit_flag)
9407 && (FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf)))
9409 /* If SIGINT isn't blocked, don't let us be interrupted by
9410 another SIGINT, it might be harmful due to non-reentrancy
9411 in I/O functions. */
9412 sigblock (sigmask (SIGINT));
9414 fflush (stdout);
9415 reset_sys_modes ();
9417 #ifdef SIGTSTP /* Support possible in later USG versions */
9419 * On systems which can suspend the current process and return to the original
9420 * shell, this command causes the user to end up back at the shell.
9421 * The "Auto-save" and "Abort" questions are not asked until
9422 * the user elects to return to emacs, at which point he can save the current
9423 * job and either dump core or continue.
9425 sys_suspend ();
9426 #else
9427 #ifdef VMS
9428 if (sys_suspend () == -1)
9430 printf ("Not running as a subprocess;\n");
9431 printf ("you can continue or abort.\n");
9433 #else /* not VMS */
9434 /* Perhaps should really fork an inferior shell?
9435 But that would not provide any way to get back
9436 to the original shell, ever. */
9437 printf ("No support for stopping a process on this operating system;\n");
9438 printf ("you can continue or abort.\n");
9439 #endif /* not VMS */
9440 #endif /* not SIGTSTP */
9441 #ifdef MSDOS
9442 /* We must remain inside the screen area when the internal terminal
9443 is used. Note that [Enter] is not echoed by dos. */
9444 cursor_to (0, 0);
9445 #endif
9446 /* It doesn't work to autosave while GC is in progress;
9447 the code used for auto-saving doesn't cope with the mark bit. */
9448 if (!gc_in_progress)
9450 printf ("Auto-save? (y or n) ");
9451 fflush (stdout);
9452 if (((c = getchar ()) & ~040) == 'Y')
9454 Fdo_auto_save (Qt, Qnil);
9455 #ifdef MSDOS
9456 printf ("\r\nAuto-save done");
9457 #else /* not MSDOS */
9458 printf ("Auto-save done\n");
9459 #endif /* not MSDOS */
9461 while (c != '\n') c = getchar ();
9463 else
9465 /* During GC, it must be safe to reenable quitting again. */
9466 Vinhibit_quit = Qnil;
9467 #ifdef MSDOS
9468 printf ("\r\n");
9469 #endif /* not MSDOS */
9470 printf ("Garbage collection in progress; cannot auto-save now\r\n");
9471 printf ("but will instead do a real quit after garbage collection ends\r\n");
9472 fflush (stdout);
9475 #ifdef MSDOS
9476 printf ("\r\nAbort? (y or n) ");
9477 #else /* not MSDOS */
9478 #ifdef VMS
9479 printf ("Abort (and enter debugger)? (y or n) ");
9480 #else /* not VMS */
9481 printf ("Abort (and dump core)? (y or n) ");
9482 #endif /* not VMS */
9483 #endif /* not MSDOS */
9484 fflush (stdout);
9485 if (((c = getchar ()) & ~040) == 'Y')
9486 abort ();
9487 while (c != '\n') c = getchar ();
9488 #ifdef MSDOS
9489 printf ("\r\nContinuing...\r\n");
9490 #else /* not MSDOS */
9491 printf ("Continuing...\n");
9492 #endif /* not MSDOS */
9493 fflush (stdout);
9494 init_sys_modes ();
9495 sigfree ();
9497 else
9499 /* If executing a function that wants to be interrupted out of
9500 and the user has not deferred quitting by binding `inhibit-quit'
9501 then quit right away. */
9502 if (immediate_quit && NILP (Vinhibit_quit))
9504 struct gl_state_s saved;
9505 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9507 immediate_quit = 0;
9508 sigfree ();
9509 saved = gl_state;
9510 GCPRO4 (saved.object, saved.global_code,
9511 saved.current_syntax_table, saved.old_prop);
9512 Fsignal (Qquit, Qnil);
9513 gl_state = saved;
9514 UNGCPRO;
9516 else
9517 /* Else request quit when it's safe */
9518 Vquit_flag = Qt;
9521 if (waiting_for_input && !echoing)
9522 quit_throw_to_read_char ();
9524 errno = old_errno;
9527 /* Handle a C-g by making read_char return C-g. */
9529 void
9530 quit_throw_to_read_char ()
9532 sigfree ();
9533 /* Prevent another signal from doing this before we finish. */
9534 clear_waiting_for_input ();
9535 input_pending = 0;
9537 Vunread_command_events = Qnil;
9538 unread_command_char = -1;
9540 #if 0 /* Currently, sit_for is called from read_char without turning
9541 off polling. And that can call set_waiting_for_input.
9542 It seems to be harmless. */
9543 #ifdef POLL_FOR_INPUT
9544 /* May be > 1 if in recursive minibuffer. */
9545 if (poll_suppress_count == 0)
9546 abort ();
9547 #endif
9548 #endif
9549 if (FRAMEP (internal_last_event_frame)
9550 && !EQ (internal_last_event_frame, selected_frame))
9551 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
9552 Qnil, 0);
9554 _longjmp (getcjmp, 1);
9557 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
9558 "Set mode of reading keyboard input.\n\
9559 First arg INTERRUPT non-nil means use input interrupts;\n\
9560 nil means use CBREAK mode.\n\
9561 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
9562 (no effect except in CBREAK mode).\n\
9563 Third arg META t means accept 8-bit input (for a Meta key).\n\
9564 META nil means ignore the top bit, on the assumption it is parity.\n\
9565 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
9566 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
9567 See also `current-input-mode'.")
9568 (interrupt, flow, meta, quit)
9569 Lisp_Object interrupt, flow, meta, quit;
9571 if (!NILP (quit)
9572 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
9573 error ("set-input-mode: QUIT must be an ASCII character");
9575 #ifdef POLL_FOR_INPUT
9576 stop_polling ();
9577 #endif
9579 #ifndef DOS_NT
9580 /* this causes startup screen to be restored and messes with the mouse */
9581 reset_sys_modes ();
9582 #endif
9584 #ifdef SIGIO
9585 /* Note SIGIO has been undef'd if FIONREAD is missing. */
9586 if (read_socket_hook)
9588 /* When using X, don't give the user a real choice,
9589 because we haven't implemented the mechanisms to support it. */
9590 #ifdef NO_SOCK_SIGIO
9591 interrupt_input = 0;
9592 #else /* not NO_SOCK_SIGIO */
9593 interrupt_input = 1;
9594 #endif /* NO_SOCK_SIGIO */
9596 else
9597 interrupt_input = !NILP (interrupt);
9598 #else /* not SIGIO */
9599 interrupt_input = 0;
9600 #endif /* not SIGIO */
9602 /* Our VMS input only works by interrupts, as of now. */
9603 #ifdef VMS
9604 interrupt_input = 1;
9605 #endif
9607 flow_control = !NILP (flow);
9608 if (NILP (meta))
9609 meta_key = 0;
9610 else if (EQ (meta, Qt))
9611 meta_key = 1;
9612 else
9613 meta_key = 2;
9614 if (!NILP (quit))
9615 /* Don't let this value be out of range. */
9616 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
9618 #ifndef DOS_NT
9619 init_sys_modes ();
9620 #endif
9622 #ifdef POLL_FOR_INPUT
9623 poll_suppress_count = 1;
9624 start_polling ();
9625 #endif
9626 return Qnil;
9629 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
9630 "Return information about the way Emacs currently reads keyboard input.\n\
9631 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
9632 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
9633 nil, Emacs is using CBREAK mode.\n\
9634 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
9635 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
9636 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
9637 META nil means ignoring the top bit, on the assumption it is parity.\n\
9638 META is neither t nor nil if accepting 8-bit input and using\n\
9639 all 8 bits as the character code.\n\
9640 QUIT is the character Emacs currently uses to quit.\n\
9641 The elements of this list correspond to the arguments of\n\
9642 `set-input-mode'.")
9645 Lisp_Object val[4];
9647 val[0] = interrupt_input ? Qt : Qnil;
9648 val[1] = flow_control ? Qt : Qnil;
9649 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
9650 XSETFASTINT (val[3], quit_char);
9652 return Flist (sizeof (val) / sizeof (val[0]), val);
9657 * Set up a new kboard object with reasonable initial values.
9659 void
9660 init_kboard (kb)
9661 KBOARD *kb;
9663 kb->Voverriding_terminal_local_map = Qnil;
9664 kb->Vlast_command = Qnil;
9665 kb->Vreal_last_command = Qnil;
9666 kb->Vprefix_arg = Qnil;
9667 kb->Vlast_prefix_arg = Qnil;
9668 kb->kbd_queue = Qnil;
9669 kb->kbd_queue_has_data = 0;
9670 kb->immediate_echo = 0;
9671 kb->echoptr = kb->echobuf;
9672 kb->echo_after_prompt = -1;
9673 kb->kbd_macro_buffer = 0;
9674 kb->kbd_macro_bufsize = 0;
9675 kb->defining_kbd_macro = Qnil;
9676 kb->Vlast_kbd_macro = Qnil;
9677 kb->reference_count = 0;
9678 kb->Vsystem_key_alist = Qnil;
9679 kb->system_key_syms = Qnil;
9680 kb->Vdefault_minibuffer_frame = Qnil;
9684 * Destroy the contents of a kboard object, but not the object itself.
9685 * We use this just before deleting it, or if we're going to initialize
9686 * it a second time.
9688 static void
9689 wipe_kboard (kb)
9690 KBOARD *kb;
9692 if (kb->kbd_macro_buffer)
9693 xfree (kb->kbd_macro_buffer);
9696 #ifdef MULTI_KBOARD
9697 void
9698 delete_kboard (kb)
9699 KBOARD *kb;
9701 KBOARD **kbp;
9702 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
9703 if (*kbp == NULL)
9704 abort ();
9705 *kbp = kb->next_kboard;
9706 wipe_kboard (kb);
9707 xfree (kb);
9709 #endif
9711 void
9712 init_keyboard ()
9714 /* This is correct before outermost invocation of the editor loop */
9715 command_loop_level = -1;
9716 immediate_quit = 0;
9717 quit_char = Ctl ('g');
9718 Vunread_command_events = Qnil;
9719 unread_command_char = -1;
9720 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
9721 total_keys = 0;
9722 recent_keys_index = 0;
9723 kbd_fetch_ptr = kbd_buffer;
9724 kbd_store_ptr = kbd_buffer;
9725 kbd_buffer_gcpro = Fmake_vector (make_number (2 * KBD_BUFFER_SIZE), Qnil);
9726 #ifdef HAVE_MOUSE
9727 do_mouse_tracking = Qnil;
9728 #endif
9729 input_pending = 0;
9731 /* This means that command_loop_1 won't try to select anything the first
9732 time through. */
9733 internal_last_event_frame = Qnil;
9734 Vlast_event_frame = internal_last_event_frame;
9736 #ifdef MULTI_KBOARD
9737 current_kboard = initial_kboard;
9738 #endif
9739 wipe_kboard (current_kboard);
9740 init_kboard (current_kboard);
9742 if (initialized)
9743 Ffillarray (kbd_buffer_gcpro, Qnil);
9745 kbd_buffer_gcpro = Fmake_vector (make_number (2 * KBD_BUFFER_SIZE), Qnil);
9746 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
9748 signal (SIGINT, interrupt_signal);
9749 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
9750 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
9751 SIGQUIT and we can't tell which one it will give us. */
9752 signal (SIGQUIT, interrupt_signal);
9753 #endif /* HAVE_TERMIO */
9755 /* Note SIGIO has been undef'd if FIONREAD is missing. */
9756 #ifdef SIGIO
9757 if (!noninteractive)
9758 signal (SIGIO, input_available_signal);
9759 #endif /* SIGIO */
9761 /* Use interrupt input by default, if it works and noninterrupt input
9762 has deficiencies. */
9764 #ifdef INTERRUPT_INPUT
9765 interrupt_input = 1;
9766 #else
9767 interrupt_input = 0;
9768 #endif
9770 /* Our VMS input only works by interrupts, as of now. */
9771 #ifdef VMS
9772 interrupt_input = 1;
9773 #endif
9775 sigfree ();
9776 dribble = 0;
9778 if (keyboard_init_hook)
9779 (*keyboard_init_hook) ();
9781 #ifdef POLL_FOR_INPUT
9782 poll_suppress_count = 1;
9783 start_polling ();
9784 #endif
9787 /* This type's only use is in syms_of_keyboard, to initialize the
9788 event header symbols and put properties on them. */
9789 struct event_head {
9790 Lisp_Object *var;
9791 char *name;
9792 Lisp_Object *kind;
9795 struct event_head head_table[] = {
9796 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
9797 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
9798 &Qswitch_frame, "switch-frame", &Qswitch_frame,
9799 &Qdelete_frame, "delete-frame", &Qdelete_frame,
9800 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
9801 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
9804 void
9805 syms_of_keyboard ()
9807 Vlispy_mouse_stem = build_string ("mouse");
9808 staticpro (&Vlispy_mouse_stem);
9810 /* Tool-bars. */
9811 QCimage = intern (":image");
9812 staticpro (&QCimage);
9814 staticpro (&Qhelp_echo);
9815 Qhelp_echo = intern ("help-echo");
9817 staticpro (&item_properties);
9818 item_properties = Qnil;
9820 staticpro (&tool_bar_item_properties);
9821 tool_bar_item_properties = Qnil;
9822 staticpro (&tool_bar_items_vector);
9823 tool_bar_items_vector = Qnil;
9825 staticpro (&real_this_command);
9826 real_this_command = Qnil;
9828 Qtimer_event_handler = intern ("timer-event-handler");
9829 staticpro (&Qtimer_event_handler);
9831 Qdisabled_command_hook = intern ("disabled-command-hook");
9832 staticpro (&Qdisabled_command_hook);
9834 Qself_insert_command = intern ("self-insert-command");
9835 staticpro (&Qself_insert_command);
9837 Qforward_char = intern ("forward-char");
9838 staticpro (&Qforward_char);
9840 Qbackward_char = intern ("backward-char");
9841 staticpro (&Qbackward_char);
9843 Qdisabled = intern ("disabled");
9844 staticpro (&Qdisabled);
9846 Qundefined = intern ("undefined");
9847 staticpro (&Qundefined);
9849 Qpre_command_hook = intern ("pre-command-hook");
9850 staticpro (&Qpre_command_hook);
9852 Qpost_command_hook = intern ("post-command-hook");
9853 staticpro (&Qpost_command_hook);
9855 Qpost_command_idle_hook = intern ("post-command-idle-hook");
9856 staticpro (&Qpost_command_idle_hook);
9858 Qdeferred_action_function = intern ("deferred-action-function");
9859 staticpro (&Qdeferred_action_function);
9861 Qcommand_hook_internal = intern ("command-hook-internal");
9862 staticpro (&Qcommand_hook_internal);
9864 Qfunction_key = intern ("function-key");
9865 staticpro (&Qfunction_key);
9866 Qmouse_click = intern ("mouse-click");
9867 staticpro (&Qmouse_click);
9868 #ifdef WINDOWSNT
9869 Qmouse_wheel = intern ("mouse-wheel");
9870 staticpro (&Qmouse_wheel);
9871 Qlanguage_change = intern ("language-change");
9872 staticpro (&Qlanguage_change);
9873 #endif
9874 Qdrag_n_drop = intern ("drag-n-drop");
9875 staticpro (&Qdrag_n_drop);
9877 Qusr1_signal = intern ("usr1-signal");
9878 staticpro (&Qusr1_signal);
9879 Qusr2_signal = intern ("usr2-signal");
9880 staticpro (&Qusr2_signal);
9882 Qmenu_enable = intern ("menu-enable");
9883 staticpro (&Qmenu_enable);
9884 Qmenu_alias = intern ("menu-alias");
9885 staticpro (&Qmenu_alias);
9886 QCenable = intern (":enable");
9887 staticpro (&QCenable);
9888 QCvisible = intern (":visible");
9889 staticpro (&QCvisible);
9890 QChelp = intern (":help");
9891 staticpro (&QChelp);
9892 QCfilter = intern (":filter");
9893 staticpro (&QCfilter);
9894 QCbutton = intern (":button");
9895 staticpro (&QCbutton);
9896 QCkeys = intern (":keys");
9897 staticpro (&QCkeys);
9898 QCkey_sequence = intern (":key-sequence");
9899 staticpro (&QCkey_sequence);
9900 QCtoggle = intern (":toggle");
9901 staticpro (&QCtoggle);
9902 QCradio = intern (":radio");
9903 staticpro (&QCradio);
9905 Qmode_line = intern ("mode-line");
9906 staticpro (&Qmode_line);
9907 Qvertical_line = intern ("vertical-line");
9908 staticpro (&Qvertical_line);
9909 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
9910 staticpro (&Qvertical_scroll_bar);
9911 Qmenu_bar = intern ("menu-bar");
9912 staticpro (&Qmenu_bar);
9914 Qabove_handle = intern ("above-handle");
9915 staticpro (&Qabove_handle);
9916 Qhandle = intern ("handle");
9917 staticpro (&Qhandle);
9918 Qbelow_handle = intern ("below-handle");
9919 staticpro (&Qbelow_handle);
9920 Qup = intern ("up");
9921 staticpro (&Qup);
9922 Qdown = intern ("down");
9923 staticpro (&Qdown);
9924 Qtop = intern ("top");
9925 staticpro (&Qtop);
9926 Qbottom = intern ("bottom");
9927 staticpro (&Qbottom);
9928 Qend_scroll = intern ("end-scroll");
9929 staticpro (&Qend_scroll);
9930 Qratio = intern ("ratio");
9931 staticpro (&Qratio);
9933 Qevent_kind = intern ("event-kind");
9934 staticpro (&Qevent_kind);
9935 Qevent_symbol_elements = intern ("event-symbol-elements");
9936 staticpro (&Qevent_symbol_elements);
9937 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
9938 staticpro (&Qevent_symbol_element_mask);
9939 Qmodifier_cache = intern ("modifier-cache");
9940 staticpro (&Qmodifier_cache);
9942 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
9943 staticpro (&Qrecompute_lucid_menubar);
9944 Qactivate_menubar_hook = intern ("activate-menubar-hook");
9945 staticpro (&Qactivate_menubar_hook);
9947 Qpolling_period = intern ("polling-period");
9948 staticpro (&Qpolling_period);
9950 Qinput_method_function = intern ("input-method-function");
9951 staticpro (&Qinput_method_function);
9953 Qinput_method_exit_on_first_char = intern ("input-method-exit-on-first-char");
9954 staticpro (&Qinput_method_exit_on_first_char);
9955 Qinput_method_use_echo_area = intern ("input-method-use-echo-area");
9956 staticpro (&Qinput_method_use_echo_area);
9958 Fset (Qinput_method_exit_on_first_char, Qnil);
9959 Fset (Qinput_method_use_echo_area, Qnil);
9962 struct event_head *p;
9964 for (p = head_table;
9965 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
9966 p++)
9968 *p->var = intern (p->name);
9969 staticpro (p->var);
9970 Fput (*p->var, Qevent_kind, *p->kind);
9971 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
9975 button_down_location = Fmake_vector (make_number (1), Qnil);
9976 staticpro (&button_down_location);
9977 mouse_syms = Fmake_vector (make_number (1), Qnil);
9978 staticpro (&mouse_syms);
9981 int i;
9982 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
9984 modifier_symbols = Fmake_vector (make_number (len), Qnil);
9985 for (i = 0; i < len; i++)
9986 if (modifier_names[i])
9987 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
9988 staticpro (&modifier_symbols);
9991 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
9992 staticpro (&recent_keys);
9994 this_command_keys = Fmake_vector (make_number (40), Qnil);
9995 staticpro (&this_command_keys);
9997 raw_keybuf = Fmake_vector (make_number (30), Qnil);
9998 staticpro (&raw_keybuf);
10000 Qextended_command_history = intern ("extended-command-history");
10001 Fset (Qextended_command_history, Qnil);
10002 staticpro (&Qextended_command_history);
10004 kbd_buffer_gcpro = Fmake_vector (make_number (2 * KBD_BUFFER_SIZE), Qnil);
10005 staticpro (&kbd_buffer_gcpro);
10007 accent_key_syms = Qnil;
10008 staticpro (&accent_key_syms);
10010 func_key_syms = Qnil;
10011 staticpro (&func_key_syms);
10013 #ifdef WINDOWSNT
10014 mouse_wheel_syms = Qnil;
10015 staticpro (&mouse_wheel_syms);
10017 drag_n_drop_syms = Qnil;
10018 staticpro (&drag_n_drop_syms);
10019 #endif
10021 unread_switch_frame = Qnil;
10022 staticpro (&unread_switch_frame);
10024 internal_last_event_frame = Qnil;
10025 staticpro (&internal_last_event_frame);
10027 read_key_sequence_cmd = Qnil;
10028 staticpro (&read_key_sequence_cmd);
10030 menu_bar_one_keymap_changed_items = Qnil;
10031 staticpro (&menu_bar_one_keymap_changed_items);
10033 defsubr (&Sevent_convert_list);
10034 defsubr (&Sread_key_sequence);
10035 defsubr (&Sread_key_sequence_vector);
10036 defsubr (&Srecursive_edit);
10037 #ifdef HAVE_MOUSE
10038 defsubr (&Strack_mouse);
10039 #endif
10040 defsubr (&Sinput_pending_p);
10041 defsubr (&Scommand_execute);
10042 defsubr (&Srecent_keys);
10043 defsubr (&Sthis_command_keys);
10044 defsubr (&Sthis_command_keys_vector);
10045 defsubr (&Sthis_single_command_keys);
10046 defsubr (&Sthis_single_command_raw_keys);
10047 defsubr (&Sreset_this_command_lengths);
10048 defsubr (&Sclear_this_command_keys);
10049 defsubr (&Ssuspend_emacs);
10050 defsubr (&Sabort_recursive_edit);
10051 defsubr (&Sexit_recursive_edit);
10052 defsubr (&Srecursion_depth);
10053 defsubr (&Stop_level);
10054 defsubr (&Sdiscard_input);
10055 defsubr (&Sopen_dribble_file);
10056 defsubr (&Sset_input_mode);
10057 defsubr (&Scurrent_input_mode);
10058 defsubr (&Sexecute_extended_command);
10060 DEFVAR_LISP ("last-command-char", &last_command_char,
10061 "Last input event that was part of a command.");
10063 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
10064 "Last input event that was part of a command.");
10066 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
10067 "Last input event in a command, except for mouse menu events.\n\
10068 Mouse menus give back keys that don't look like mouse events;\n\
10069 this variable holds the actual mouse event that led to the menu,\n\
10070 so that you can determine whether the command was run by mouse or not.");
10072 DEFVAR_LISP ("last-input-char", &last_input_char,
10073 "Last input event.");
10075 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
10076 "Last input event.");
10078 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
10079 "List of events to be read as the command input.\n\
10080 These events are processed first, before actual keyboard input.");
10081 Vunread_command_events = Qnil;
10083 DEFVAR_INT ("unread-command-char", &unread_command_char,
10084 "If not -1, an object to be read as next command input event.");
10086 DEFVAR_LISP ("unread-post-input-method-events", &Vunread_post_input_method_events,
10087 "List of events to be processed as input by input methods.\n\
10088 These events are processed after `unread-command-events', but\n\
10089 before actual keyboard input.");
10090 Vunread_post_input_method_events = Qnil;
10092 DEFVAR_LISP ("unread-input-method-events", &Vunread_input_method_events,
10093 "List of events to be processed as input by input methods.\n\
10094 These events are processed after `unread-command-events', but\n\
10095 before actual keyboard input.");
10096 Vunread_input_method_events = Qnil;
10098 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
10099 "Meta-prefix character code.\n\
10100 Meta-foo as command input turns into this character followed by foo.");
10101 XSETINT (meta_prefix_char, 033);
10103 DEFVAR_KBOARD ("last-command", Vlast_command,
10104 "The last command executed.\n\
10105 Normally a symbol with a function definition, but can be whatever was found\n\
10106 in the keymap, or whatever the variable `this-command' was set to by that\n\
10107 command.\n\
10109 The value `mode-exit' is special; it means that the previous command\n\
10110 read an event that told it to exit, and it did so and unread that event.\n\
10111 In other words, the present command is the event that made the previous\n\
10112 command exit.\n\
10114 The value `kill-region' is special; it means that the previous command\n\
10115 was a kill command.");
10117 DEFVAR_KBOARD ("real-last-command", Vreal_last_command,
10118 "Same as `last-command', but never altered by Lisp code.");
10120 DEFVAR_LISP ("this-command", &Vthis_command,
10121 "The command now being executed.\n\
10122 The command can set this variable; whatever is put here\n\
10123 will be in `last-command' during the following command.");
10124 Vthis_command = Qnil;
10126 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
10127 "*Number of input events between auto-saves.\n\
10128 Zero means disable autosaving due to number of characters typed.");
10129 auto_save_interval = 300;
10131 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
10132 "*Number of seconds idle time before auto-save.\n\
10133 Zero or nil means disable auto-saving due to idleness.\n\
10134 After auto-saving due to this many seconds of idle time,\n\
10135 Emacs also does a garbage collection if that seems to be warranted.");
10136 XSETFASTINT (Vauto_save_timeout, 30);
10138 DEFVAR_LISP ("echo-keystrokes", &Vecho_keystrokes,
10139 "*Nonzero means echo unfinished commands after this many seconds of pause.\n\
10140 The value may be integer or floating point.");
10141 Vecho_keystrokes = make_number (1);
10143 DEFVAR_INT ("polling-period", &polling_period,
10144 "*Interval between polling for input during Lisp execution.\n\
10145 The reason for polling is to make C-g work to stop a running program.\n\
10146 Polling is needed only when using X windows and SIGIO does not work.\n\
10147 Polling is automatically disabled in all other cases.");
10148 polling_period = 2;
10150 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
10151 "*Maximum time between mouse clicks to make a double-click.\n\
10152 Measured in milliseconds. nil means disable double-click recognition;\n\
10153 t means double-clicks have no time limit and are detected\n\
10154 by position only.");
10155 Vdouble_click_time = make_number (500);
10157 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
10158 "*Non-nil means inhibit local map menu bar menus.");
10159 inhibit_local_menu_bar_menus = 0;
10161 DEFVAR_INT ("num-input-keys", &num_input_keys,
10162 "Number of complete key sequences read as input so far.\n\
10163 This includes key sequences read from keyboard macros.\n\
10164 The number is effectively the number of interactive command invocations.");
10165 num_input_keys = 0;
10167 DEFVAR_INT ("num-nonmacro-input-events", &num_nonmacro_input_events,
10168 "Number of input events read from the keyboard so far.\n\
10169 This does not include events generated by keyboard macros.");
10170 num_nonmacro_input_events = 0;
10172 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
10173 "The frame in which the most recently read event occurred.\n\
10174 If the last event came from a keyboard macro, this is set to `macro'.");
10175 Vlast_event_frame = Qnil;
10177 /* This variable is set up in sysdep.c. */
10178 DEFVAR_LISP ("tty-erase-char", &Vtty_erase_char,
10179 "The ERASE character as set by the user with stty.");
10181 DEFVAR_LISP ("help-char", &Vhelp_char,
10182 "Character to recognize as meaning Help.\n\
10183 When it is read, do `(eval help-form)', and display result if it's a string.\n\
10184 If the value of `help-form' is nil, this char can be read normally.");
10185 XSETINT (Vhelp_char, Ctl ('H'));
10187 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
10188 "List of input events to recognize as meaning Help.\n\
10189 These work just like the value of `help-char' (see that).");
10190 Vhelp_event_list = Qnil;
10192 DEFVAR_LISP ("help-form", &Vhelp_form,
10193 "Form to execute when character `help-char' is read.\n\
10194 If the form returns a string, that string is displayed.\n\
10195 If `help-form' is nil, the help char is not recognized.");
10196 Vhelp_form = Qnil;
10198 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
10199 "Command to run when `help-char' character follows a prefix key.\n\
10200 This command is used only when there is no actual binding\n\
10201 for that character after that prefix key.");
10202 Vprefix_help_command = Qnil;
10204 DEFVAR_LISP ("top-level", &Vtop_level,
10205 "Form to evaluate when Emacs starts up.\n\
10206 Useful to set before you dump a modified Emacs.");
10207 Vtop_level = Qnil;
10209 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
10210 "Translate table for keyboard input, or nil.\n\
10211 Each character is looked up in this string and the contents used instead.\n\
10212 The value may be a string, a vector, or a char-table.\n\
10213 If it is a string or vector of length N,\n\
10214 character codes N and up are untranslated.\n\
10215 In a vector or a char-table, an element which is nil means \"no translation\".");
10216 Vkeyboard_translate_table = Qnil;
10218 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
10219 "Non-nil means to always spawn a subshell instead of suspending.\n\
10220 \(Even if the operating system has support for stopping a process.\)");
10221 cannot_suspend = 0;
10223 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
10224 "Non-nil means prompt with menus when appropriate.\n\
10225 This is done when reading from a keymap that has a prompt string,\n\
10226 for elements that have prompt strings.\n\
10227 The menu is displayed on the screen\n\
10228 if X menus were enabled at configuration\n\
10229 time and the previous event was a mouse click prefix key.\n\
10230 Otherwise, menu prompting uses the echo area.");
10231 menu_prompting = 1;
10233 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
10234 "Character to see next line of menu prompt.\n\
10235 Type this character while in a menu prompt to rotate around the lines of it.");
10236 XSETINT (menu_prompt_more_char, ' ');
10238 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
10239 "A mask of additional modifier keys to use with every keyboard character.\n\
10240 Emacs applies the modifiers of the character stored here to each keyboard\n\
10241 character it reads. For example, after evaluating the expression\n\
10242 (setq extra-keyboard-modifiers ?\\C-x)\n\
10243 all input characters will have the control modifier applied to them.\n\
10245 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
10246 not count as a control character; rather, it counts as a character\n\
10247 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
10248 cancels any modification.");
10249 extra_keyboard_modifiers = 0;
10251 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
10252 "If an editing command sets this to t, deactivate the mark afterward.\n\
10253 The command loop sets this to nil before each command,\n\
10254 and tests the value when the command returns.\n\
10255 Buffer modification stores t in this variable.");
10256 Vdeactivate_mark = Qnil;
10258 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
10259 "Temporary storage of pre-command-hook or post-command-hook.");
10260 Vcommand_hook_internal = Qnil;
10262 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
10263 "Normal hook run before each command is executed.\n\
10264 If an unhandled error happens in running this hook,\n\
10265 the hook value is set to nil, since otherwise the error\n\
10266 might happen repeatedly and make Emacs nonfunctional.");
10267 Vpre_command_hook = Qnil;
10269 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
10270 "Normal hook run after each command is executed.\n\
10271 If an unhandled error happens in running this hook,\n\
10272 the hook value is set to nil, since otherwise the error\n\
10273 might happen repeatedly and make Emacs nonfunctional.");
10274 Vpost_command_hook = Qnil;
10276 DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
10277 "Normal hook run after each command is executed, if idle.\n\
10278 Errors running the hook are caught and ignored.\n\
10279 This feature is obsolete; use idle timers instead. See `etc/NEWS'.");
10280 Vpost_command_idle_hook = Qnil;
10282 DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
10283 "Delay time before running `post-command-idle-hook'.\n\
10284 This is measured in microseconds.");
10285 post_command_idle_delay = 100000;
10287 #if 0
10288 DEFVAR_LISP ("echo-area-clear-hook", ...,
10289 "Normal hook run when clearing the echo area.");
10290 #endif
10291 Qecho_area_clear_hook = intern ("echo-area-clear-hook");
10292 XSYMBOL (Qecho_area_clear_hook)->value = Qnil;
10294 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
10295 "t means menu bar, specified Lucid style, needs to be recomputed.");
10296 Vlucid_menu_bar_dirty_flag = Qnil;
10298 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
10299 "List of menu bar items to move to the end of the menu bar.\n\
10300 The elements of the list are event types that may have menu bar bindings.");
10301 Vmenu_bar_final_items = Qnil;
10303 DEFVAR_KBOARD ("overriding-terminal-local-map",
10304 Voverriding_terminal_local_map,
10305 "Per-terminal keymap that overrides all other local keymaps.\n\
10306 If this variable is non-nil, it is used as a keymap instead of the\n\
10307 buffer's local map, and the minor mode keymaps and text property keymaps.\n\
10308 This variable is intended to let commands such as `universal-argumemnt'\n\
10309 set up a different keymap for reading the next command.");
10311 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
10312 "Keymap that overrides all other local keymaps.\n\
10313 If this variable is non-nil, it is used as a keymap instead of the\n\
10314 buffer's local map, and the minor mode keymaps and text property keymaps.");
10315 Voverriding_local_map = Qnil;
10317 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
10318 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
10319 Otherwise, the menu bar continues to reflect the buffer's local map\n\
10320 and the minor mode maps regardless of `overriding-local-map'.");
10321 Voverriding_local_map_menu_flag = Qnil;
10323 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
10324 "Keymap defining bindings for special events to execute at low level.");
10325 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
10327 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
10328 "*Non-nil means generate motion events for mouse motion.");
10330 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
10331 "Alist of system-specific X windows key symbols.\n\
10332 Each element should have the form (N . SYMBOL) where N is the\n\
10333 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
10334 and SYMBOL is its name.");
10336 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
10337 "List of deferred actions to be performed at a later time.\n\
10338 The precise format isn't relevant here; we just check whether it is nil.");
10339 Vdeferred_action_list = Qnil;
10341 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
10342 "Function to call to handle deferred actions, after each command.\n\
10343 This function is called with no arguments after each command\n\
10344 whenever `deferred-action-list' is non-nil.");
10345 Vdeferred_action_function = Qnil;
10347 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
10348 "*Non-nil means show the equivalent key-binding when M-x command has one.\n\
10349 The value can be a length of time to show the message for.\n\
10350 If the value is non-nil and not a number, we wait 2 seconds.");
10351 Vsuggest_key_bindings = Qt;
10353 DEFVAR_LISP ("timer-list", &Vtimer_list,
10354 "List of active absolute time timers in order of increasing time");
10355 Vtimer_list = Qnil;
10357 DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
10358 "List of active idle-time timers in order of increasing time");
10359 Vtimer_idle_list = Qnil;
10361 DEFVAR_LISP ("input-method-function", &Vinput_method_function,
10362 "If non-nil, the function that implements the current input method.\n\
10363 It's called with one argument, a printing character that was just read.\n\
10364 \(That means a character with code 040...0176.)\n\
10365 Typically this function uses `read-event' to read additional events.\n\
10366 When it does so, it should first bind `input-method-function' to nil\n\
10367 so it will not be called recursively.\n\
10369 The function should return a list of zero or more events\n\
10370 to be used as input. If it wants to put back some events\n\
10371 to be reconsidered, separately, by the input method,\n\
10372 it can add them to the beginning of `unread-command-events'.\n\
10374 The input method function can find in `input-method-previous-method'\n\
10375 the previous echo area message.\n\
10377 The input method function should refer to the variables\n\
10378 `input-method-use-echo-area' and `input-method-exit-on-first-char'\n\
10379 for guidance on what to do.");
10380 Vinput_method_function = Qnil;
10382 DEFVAR_LISP ("input-method-previous-message",
10383 &Vinput_method_previous_message,
10384 "When `input-method-function' is called, hold the previous echo area message.\n\
10385 This variable exists because `read-event' clears the echo area\n\
10386 before running the input method. It is nil if there was no message.");
10387 Vinput_method_previous_message = Qnil;
10389 DEFVAR_LISP ("show-help-function", &Vshow_help_function,
10390 "If non-nil, the function that implements the display of help.\n\
10391 It's called with one argument, the help string to display.");
10392 Vshow_help_function = Qnil;
10394 DEFVAR_LISP ("disable-point-adjustment", &Vdisable_point_adjustment,
10395 "If non-nil, suppress point adjustment after executing a command.\n\
10397 After a command is executed, if point is moved into a region that has\n\
10398 special properties (e.g. composition, display), we adjust point to\n\
10399 the boundary of the region. But, several special commands sets this\n\
10400 variable to non-nil, then we suppress the point adjustment.\n\
10402 This variable is set to nil before reading a command, and is checked\n\
10403 just after executing the command");
10404 Vdisable_point_adjustment = Qnil;
10406 DEFVAR_LISP ("global-disable-point-adjustment",
10407 &Vglobal_disable_point_adjustment,
10408 "*If non-nil, always suppress point adjustment.\n\
10410 The default value is nil, in which case, point adjustment are\n\
10411 suppressed only after special commands that set\n\
10412 `disable-point-adjustment' (which see) to non-nil.");
10413 Vglobal_disable_point_adjustment = Qnil;
10416 void
10417 keys_of_keyboard ()
10419 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
10420 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
10421 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
10422 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
10423 initial_define_key (meta_map, 'x', "execute-extended-command");
10425 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
10426 "handle-delete-frame");
10427 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
10428 "ignore-event");
10429 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
10430 "ignore-event");