(make_lispy_event): Add string info to the event,
[emacs/old-mirror.git] / src / keyboard.c
blob3c01af43475c4c2cc4b60a5feb1c0af82610ce46
1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985,86,87,88,89,93,94,95,96,97,99, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include "termchar.h"
26 #include "termopts.h"
27 #include "lisp.h"
28 #include "termhooks.h"
29 #include "macros.h"
30 #include "keyboard.h"
31 #include "frame.h"
32 #include "window.h"
33 #include "commands.h"
34 #include "buffer.h"
35 #include "charset.h"
36 #include "disptab.h"
37 #include "dispextern.h"
38 #include "syntax.h"
39 #include "intervals.h"
40 #include "blockinput.h"
41 #include "puresize.h"
42 #include "systime.h"
43 #include "atimer.h"
44 #include <setjmp.h>
45 #include <errno.h>
47 #ifdef MSDOS
48 #include "msdos.h"
49 #include <time.h>
50 #else /* not MSDOS */
51 #ifndef VMS
52 #include <sys/ioctl.h>
53 #endif
54 #endif /* not MSDOS */
56 #include "syssignal.h"
57 #include "systty.h"
59 #include <sys/types.h>
60 #ifdef HAVE_UNISTD_H
61 #include <unistd.h>
62 #endif
64 /* This is to get the definitions of the XK_ symbols. */
65 #ifdef HAVE_X_WINDOWS
66 #include "xterm.h"
67 #endif
69 #ifdef HAVE_NTGUI
70 #include "w32term.h"
71 #endif /* HAVE_NTGUI */
73 #ifdef macintosh
74 #include "macterm.h"
75 #endif
77 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
78 #include "systime.h"
80 #ifndef USE_CRT_DLL
81 extern int errno;
82 #endif
84 /* Variables for blockinput.h: */
86 /* Non-zero if interrupt input is blocked right now. */
87 int interrupt_input_blocked;
89 /* Nonzero means an input interrupt has arrived
90 during the current critical section. */
91 int interrupt_input_pending;
94 /* File descriptor to use for input. */
95 extern int input_fd;
97 #ifdef HAVE_WINDOW_SYSTEM
98 /* Make all keyboard buffers much bigger when using X windows. */
99 #ifdef macintosh
100 /* But not too big (local data > 32K error) if on macintosh. */
101 #define KBD_BUFFER_SIZE 512
102 #else
103 #define KBD_BUFFER_SIZE 4096
104 #endif
105 #else /* No X-windows, character input */
106 #define KBD_BUFFER_SIZE 256
107 #endif /* No X-windows */
109 /* Following definition copied from eval.c */
111 struct backtrace
113 struct backtrace *next;
114 Lisp_Object *function;
115 Lisp_Object *args; /* Points to vector of args. */
116 int nargs; /* length of vector. If nargs is UNEVALLED,
117 args points to slot holding list of
118 unevalled args */
119 char evalargs;
122 #ifdef MULTI_KBOARD
123 KBOARD *initial_kboard;
124 KBOARD *current_kboard;
125 KBOARD *all_kboards;
126 int single_kboard;
127 #else
128 KBOARD the_only_kboard;
129 #endif
131 /* Non-nil disable property on a command means
132 do not execute it; call disabled-command-hook's value instead. */
133 Lisp_Object Qdisabled, Qdisabled_command_hook;
135 #define NUM_RECENT_KEYS (100)
136 int recent_keys_index; /* Index for storing next element into recent_keys */
137 int total_keys; /* Total number of elements stored into recent_keys */
138 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
140 /* Vector holding the key sequence that invoked the current command.
141 It is reused for each command, and it may be longer than the current
142 sequence; this_command_key_count indicates how many elements
143 actually mean something.
144 It's easier to staticpro a single Lisp_Object than an array. */
145 Lisp_Object this_command_keys;
146 int this_command_key_count;
148 /* This vector is used as a buffer to record the events that were actually read
149 by read_key_sequence. */
150 Lisp_Object raw_keybuf;
151 int raw_keybuf_count;
153 #define GROW_RAW_KEYBUF \
154 if (raw_keybuf_count == XVECTOR (raw_keybuf)->size) \
156 int newsize = 2 * XVECTOR (raw_keybuf)->size; \
157 Lisp_Object new; \
158 new = Fmake_vector (make_number (newsize), Qnil); \
159 bcopy (XVECTOR (raw_keybuf)->contents, XVECTOR (new)->contents, \
160 raw_keybuf_count * sizeof (Lisp_Object)); \
161 raw_keybuf = new; \
164 /* Number of elements of this_command_keys
165 that precede this key sequence. */
166 int this_single_command_key_start;
168 /* Record values of this_command_key_count and echo_length ()
169 before this command was read. */
170 static int before_command_key_count;
171 static int before_command_echo_length;
172 /* Values of before_command_key_count and before_command_echo_length
173 saved by reset-this-command-lengths. */
174 static int before_command_key_count_1;
175 static int before_command_echo_length_1;
176 /* Flag set by reset-this-command-lengths,
177 saying to reset the lengths when add_command_key is called. */
178 static int before_command_restore_flag;
180 extern int minbuf_level;
182 extern int message_enable_multibyte;
184 extern struct backtrace *backtrace_list;
186 /* If non-nil, the function that implements the display of help.
187 It's called with one argument, the help string to display. */
189 Lisp_Object Vshow_help_function;
191 /* Nonzero means do menu prompting. */
192 static int menu_prompting;
194 /* Character to see next line of menu prompt. */
195 static Lisp_Object menu_prompt_more_char;
197 /* For longjmp to where kbd input is being done. */
198 static jmp_buf getcjmp;
200 /* True while doing kbd input. */
201 int waiting_for_input;
203 /* True while displaying for echoing. Delays C-g throwing. */
205 static int echoing;
207 /* Non-null means we can start echoing at the next input pause even
208 though there is something in the echo area. */
210 static struct kboard *ok_to_echo_at_next_pause;
212 /* The kboard last echoing, or null for none. Reset to 0 in
213 cancel_echoing. If non-null, and a current echo area message
214 exists, and echo_message_buffer is eq to the current message
215 buffer, we know that the message comes from echo_kboard. */
217 static struct kboard *echo_kboard;
219 /* The buffer used for echoing. Set in echo_now, reset in
220 cancel_echoing. */
222 static Lisp_Object echo_message_buffer;
224 /* Nonzero means disregard local maps for the menu bar. */
225 static int inhibit_local_menu_bar_menus;
227 /* Nonzero means C-g should cause immediate error-signal. */
228 int immediate_quit;
230 /* The user's ERASE setting. */
231 Lisp_Object Vtty_erase_char;
233 /* Character to recognize as the help char. */
234 Lisp_Object Vhelp_char;
236 /* List of other event types to recognize as meaning "help". */
237 Lisp_Object Vhelp_event_list;
239 /* Form to execute when help char is typed. */
240 Lisp_Object Vhelp_form;
242 /* Command to run when the help character follows a prefix key. */
243 Lisp_Object Vprefix_help_command;
245 /* List of items that should move to the end of the menu bar. */
246 Lisp_Object Vmenu_bar_final_items;
248 /* Non-nil means show the equivalent key-binding for
249 any M-x command that has one.
250 The value can be a length of time to show the message for.
251 If the value is non-nil and not a number, we wait 2 seconds. */
252 Lisp_Object Vsuggest_key_bindings;
254 /* How long to display an echo-area message when the minibuffer is active.
255 If the value is not a number, such messages don't time out. */
256 Lisp_Object Vminibuffer_message_timeout;
258 /* Character that causes a quit. Normally C-g.
260 If we are running on an ordinary terminal, this must be an ordinary
261 ASCII char, since we want to make it our interrupt character.
263 If we are not running on an ordinary terminal, it still needs to be
264 an ordinary ASCII char. This character needs to be recognized in
265 the input interrupt handler. At this point, the keystroke is
266 represented as a struct input_event, while the desired quit
267 character is specified as a lispy event. The mapping from struct
268 input_events to lispy events cannot run in an interrupt handler,
269 and the reverse mapping is difficult for anything but ASCII
270 keystrokes.
272 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
273 ASCII character. */
274 int quit_char;
276 extern Lisp_Object current_global_map;
277 extern int minibuf_level;
279 /* If non-nil, this is a map that overrides all other local maps. */
280 Lisp_Object Voverriding_local_map;
282 /* If non-nil, Voverriding_local_map applies to the menu bar. */
283 Lisp_Object Voverriding_local_map_menu_flag;
285 /* Keymap that defines special misc events that should
286 be processed immediately at a low level. */
287 Lisp_Object Vspecial_event_map;
289 /* Current depth in recursive edits. */
290 int command_loop_level;
292 /* Total number of times command_loop has read a key sequence. */
293 int num_input_keys;
295 /* Last input character read as a command. */
296 Lisp_Object last_command_char;
298 /* Last input character read as a command, not counting menus
299 reached by the mouse. */
300 Lisp_Object last_nonmenu_event;
302 /* Last input character read for any purpose. */
303 Lisp_Object last_input_char;
305 /* If not Qnil, a list of objects to be read as subsequent command input. */
306 Lisp_Object Vunread_command_events;
308 /* If not Qnil, a list of objects to be read as subsequent command input
309 including input method processing. */
310 Lisp_Object Vunread_input_method_events;
312 /* If not Qnil, a list of objects to be read as subsequent command input
313 but NOT including input method processing. */
314 Lisp_Object Vunread_post_input_method_events;
316 /* If not -1, an event to be read as subsequent command input. */
317 int unread_command_char;
319 /* If not Qnil, this is a switch-frame event which we decided to put
320 off until the end of a key sequence. This should be read as the
321 next command input, after any unread_command_events.
323 read_key_sequence uses this to delay switch-frame events until the
324 end of the key sequence; Fread_char uses it to put off switch-frame
325 events until a non-ASCII event is acceptable as input. */
326 Lisp_Object unread_switch_frame;
328 /* A mask of extra modifier bits to put into every keyboard char. */
329 int extra_keyboard_modifiers;
331 /* Char to use as prefix when a meta character is typed in.
332 This is bound on entry to minibuffer in case ESC is changed there. */
334 Lisp_Object meta_prefix_char;
336 /* Last size recorded for a current buffer which is not a minibuffer. */
337 static int last_non_minibuf_size;
339 /* Number of idle seconds before an auto-save and garbage collection. */
340 static Lisp_Object Vauto_save_timeout;
342 /* Total number of times read_char has returned. */
343 int num_input_events;
345 /* Total number of times read_char has returned, outside of macros. */
346 int num_nonmacro_input_events;
348 /* Auto-save automatically when this many characters have been typed
349 since the last time. */
351 static int auto_save_interval;
353 /* Value of num_nonmacro_input_events as of last auto save. */
355 int last_auto_save;
357 /* The command being executed by the command loop.
358 Commands may set this, and the value set will be copied into
359 current_kboard->Vlast_command instead of the actual command. */
360 Lisp_Object Vthis_command;
362 /* This is like Vthis_command, except that commands never set it. */
363 Lisp_Object real_this_command;
365 /* The value of point when the last command was executed. */
366 int last_point_position;
368 /* The buffer that was current when the last command was started. */
369 Lisp_Object last_point_position_buffer;
371 /* The frame in which the last input event occurred, or Qmacro if the
372 last event came from a macro. We use this to determine when to
373 generate switch-frame events. This may be cleared by functions
374 like Fselect_frame, to make sure that a switch-frame event is
375 generated by the next character. */
376 Lisp_Object internal_last_event_frame;
378 /* A user-visible version of the above, intended to allow users to
379 figure out where the last event came from, if the event doesn't
380 carry that information itself (i.e. if it was a character). */
381 Lisp_Object Vlast_event_frame;
383 /* The timestamp of the last input event we received from the X server.
384 X Windows wants this for selection ownership. */
385 unsigned long last_event_timestamp;
387 Lisp_Object Qself_insert_command;
388 Lisp_Object Qforward_char;
389 Lisp_Object Qbackward_char;
390 Lisp_Object Qundefined;
391 Lisp_Object Qtimer_event_handler;
393 /* read_key_sequence stores here the command definition of the
394 key sequence that it reads. */
395 Lisp_Object read_key_sequence_cmd;
397 /* Echo unfinished commands after this many seconds of pause. */
398 Lisp_Object Vecho_keystrokes;
400 /* Form to evaluate (if non-nil) when Emacs is started. */
401 Lisp_Object Vtop_level;
403 /* User-supplied string to translate input characters through. */
404 Lisp_Object Vkeyboard_translate_table;
406 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
407 extern Lisp_Object Vfunction_key_map;
409 /* Another keymap that maps key sequences into key sequences.
410 This one takes precedence over ordinary definitions. */
411 extern Lisp_Object Vkey_translation_map;
413 /* If non-nil, this implements the current input method. */
414 Lisp_Object Vinput_method_function;
415 Lisp_Object Qinput_method_function;
417 /* When we call Vinput_method_function,
418 this holds the echo area message that was just erased. */
419 Lisp_Object Vinput_method_previous_message;
421 /* Non-nil means deactivate the mark at end of this command. */
422 Lisp_Object Vdeactivate_mark;
424 /* Menu bar specified in Lucid Emacs fashion. */
426 Lisp_Object Vlucid_menu_bar_dirty_flag;
427 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
429 Lisp_Object Qecho_area_clear_hook;
431 /* Hooks to run before and after each command. */
432 Lisp_Object Qpre_command_hook, Vpre_command_hook;
433 Lisp_Object Qpost_command_hook, Vpost_command_hook;
434 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
435 /* Hook run after a command if there's no more input soon. */
436 Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook;
438 /* Delay time in microseconds before running post-command-idle-hook. */
439 int post_command_idle_delay;
441 /* List of deferred actions to be performed at a later time.
442 The precise format isn't relevant here; we just check whether it is nil. */
443 Lisp_Object Vdeferred_action_list;
445 /* Function to call to handle deferred actions, when there are any. */
446 Lisp_Object Vdeferred_action_function;
447 Lisp_Object Qdeferred_action_function;
449 Lisp_Object Qinput_method_exit_on_first_char;
450 Lisp_Object Qinput_method_use_echo_area;
452 /* File in which we write all commands we read. */
453 FILE *dribble;
455 /* Nonzero if input is available. */
456 int input_pending;
458 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
459 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
461 int meta_key;
463 /* Non-zero means force key bindings update in parse_menu_item. */
465 int update_menu_bindings;
467 extern char *pending_malloc_warning;
469 /* Circular buffer for pre-read keyboard input. */
471 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
473 /* Vector to GCPRO the Lisp objects referenced from kbd_buffer.
475 The interrupt-level event handlers will never enqueue an event on a
476 frame which is not in Vframe_list, and once an event is dequeued,
477 internal_last_event_frame or the event itself points to the frame.
478 So that's all fine.
480 But while the event is sitting in the queue, it's completely
481 unprotected. Suppose the user types one command which will run for
482 a while and then delete a frame, and then types another event at
483 the frame that will be deleted, before the command gets around to
484 it. Suppose there are no references to this frame elsewhere in
485 Emacs, and a GC occurs before the second event is dequeued. Now we
486 have an event referring to a freed frame, which will crash Emacs
487 when it is dequeued.
489 Similar things happen when an event on a scroll bar is enqueued; the
490 window may be deleted while the event is in the queue.
492 So, we use this vector to protect the Lisp_Objects in the event
493 queue. That way, they'll be dequeued as dead frames or windows,
494 but still valid Lisp objects.
496 If kbd_buffer[i].kind != no_event, then
498 AREF (kbd_buffer_gcpro, 2 * i) == kbd_buffer[i].frame_or_window.
499 AREF (kbd_buffer_gcpro, 2 * i + 1) == kbd_buffer[i].arg. */
501 static Lisp_Object kbd_buffer_gcpro;
503 /* Pointer to next available character in kbd_buffer.
504 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
505 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
506 next available char is in kbd_buffer[0]. */
507 static struct input_event *kbd_fetch_ptr;
509 /* Pointer to next place to store character in kbd_buffer. This
510 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
511 character should go in kbd_buffer[0]. */
512 static struct input_event * volatile kbd_store_ptr;
514 /* The above pair of variables forms a "queue empty" flag. When we
515 enqueue a non-hook event, we increment kbd_store_ptr. When we
516 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
517 there is input available iff the two pointers are not equal.
519 Why not just have a flag set and cleared by the enqueuing and
520 dequeuing functions? Such a flag could be screwed up by interrupts
521 at inopportune times. */
523 /* If this flag is non-nil, we check mouse_moved to see when the
524 mouse moves, and motion events will appear in the input stream.
525 Otherwise, mouse motion is ignored. */
526 Lisp_Object do_mouse_tracking;
528 /* Symbols to head events. */
529 Lisp_Object Qmouse_movement;
530 Lisp_Object Qscroll_bar_movement;
531 Lisp_Object Qswitch_frame;
532 Lisp_Object Qdelete_frame;
533 Lisp_Object Qiconify_frame;
534 Lisp_Object Qmake_frame_visible;
535 Lisp_Object Qhelp_echo;
537 /* Symbols to denote kinds of events. */
538 Lisp_Object Qfunction_key;
539 Lisp_Object Qmouse_click;
540 #ifdef WINDOWSNT
541 Lisp_Object Qmouse_wheel;
542 Lisp_Object Qlanguage_change;
543 #endif
544 Lisp_Object Qdrag_n_drop;
545 /* Lisp_Object Qmouse_movement; - also an event header */
547 /* Properties of event headers. */
548 Lisp_Object Qevent_kind;
549 Lisp_Object Qevent_symbol_elements;
551 /* menu item parts */
552 Lisp_Object Qmenu_alias;
553 Lisp_Object Qmenu_enable;
554 Lisp_Object QCenable, QCvisible, QChelp, QCfilter, QCkeys, QCkey_sequence;
555 Lisp_Object QCbutton, QCtoggle, QCradio;
556 extern Lisp_Object Vdefine_key_rebound_commands;
557 extern Lisp_Object Qmenu_item;
559 /* An event header symbol HEAD may have a property named
560 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
561 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
562 mask of modifiers applied to it. If present, this is used to help
563 speed up parse_modifiers. */
564 Lisp_Object Qevent_symbol_element_mask;
566 /* An unmodified event header BASE may have a property named
567 Qmodifier_cache, which is an alist mapping modifier masks onto
568 modified versions of BASE. If present, this helps speed up
569 apply_modifiers. */
570 Lisp_Object Qmodifier_cache;
572 /* Symbols to use for parts of windows. */
573 Lisp_Object Qmode_line;
574 Lisp_Object Qvertical_line;
575 Lisp_Object Qvertical_scroll_bar;
576 Lisp_Object Qmenu_bar;
578 Lisp_Object recursive_edit_unwind (), command_loop ();
579 Lisp_Object Fthis_command_keys ();
580 Lisp_Object Qextended_command_history;
581 EMACS_TIME timer_check ();
583 extern Lisp_Object Vhistory_length;
585 extern char *x_get_keysym_name ();
587 static void record_menu_key ();
589 Lisp_Object Qpolling_period;
591 /* List of absolute timers. Appears in order of next scheduled event. */
592 Lisp_Object Vtimer_list;
594 /* List of idle time timers. Appears in order of next scheduled event. */
595 Lisp_Object Vtimer_idle_list;
597 /* Incremented whenever a timer is run. */
598 int timers_run;
600 extern Lisp_Object Vprint_level, Vprint_length;
602 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
603 happens. */
604 EMACS_TIME *input_available_clear_time;
606 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
607 Default is 1 if INTERRUPT_INPUT is defined. */
608 int interrupt_input;
610 /* Nonzero while interrupts are temporarily deferred during redisplay. */
611 int interrupts_deferred;
613 /* Nonzero means use ^S/^Q for flow control. */
614 int flow_control;
616 /* Allow m- file to inhibit use of FIONREAD. */
617 #ifdef BROKEN_FIONREAD
618 #undef FIONREAD
619 #endif
621 /* We are unable to use interrupts if FIONREAD is not available,
622 so flush SIGIO so we won't try. */
623 #ifndef FIONREAD
624 #ifdef SIGIO
625 #undef SIGIO
626 #endif
627 #endif
629 /* If we support a window system, turn on the code to poll periodically
630 to detect C-g. It isn't actually used when doing interrupt input. */
631 #ifdef HAVE_WINDOW_SYSTEM
632 #define POLL_FOR_INPUT
633 #endif
635 /* After a command is executed, if point is moved into a region that
636 has specific properties (e.g. composition, display), we adjust
637 point to the boundary of the region. But, if a command sets this
638 valiable to non-nil, we suppress this point adjustment. This
639 variable is set to nil before reading a command. */
641 Lisp_Object Vdisable_point_adjustment;
643 /* If non-nil, always disable point adjustment. */
645 Lisp_Object Vglobal_disable_point_adjustment;
648 /* Global variable declarations. */
650 /* Function for init_keyboard to call with no args (if nonzero). */
651 void (*keyboard_init_hook) ();
653 static int read_avail_input P_ ((int));
654 static void get_input_pending P_ ((int *, int));
655 static int readable_events P_ ((int));
656 static Lisp_Object read_char_x_menu_prompt P_ ((int, Lisp_Object *,
657 Lisp_Object, int *));
658 static Lisp_Object read_char_x_menu_prompt ();
659 static Lisp_Object read_char_minibuf_menu_prompt P_ ((int, int,
660 Lisp_Object *));
661 static Lisp_Object make_lispy_event P_ ((struct input_event *));
662 #ifdef HAVE_MOUSE
663 static Lisp_Object make_lispy_movement P_ ((struct frame *, Lisp_Object,
664 enum scroll_bar_part,
665 Lisp_Object, Lisp_Object,
666 unsigned long));
667 #endif
668 static Lisp_Object modify_event_symbol P_ ((int, unsigned, Lisp_Object,
669 Lisp_Object, char **,
670 Lisp_Object *, unsigned));
671 static Lisp_Object make_lispy_switch_frame P_ ((Lisp_Object));
672 static int parse_solitary_modifier P_ ((Lisp_Object));
673 static int parse_solitary_modifier ();
674 static void save_getcjmp P_ ((jmp_buf));
675 static void save_getcjmp ();
676 static void restore_getcjmp P_ ((jmp_buf));
677 static Lisp_Object apply_modifiers P_ ((int, Lisp_Object));
678 static void clear_event P_ ((struct input_event *));
680 /* Nonzero means don't try to suspend even if the operating system seems
681 to support it. */
682 static int cannot_suspend;
684 #define min(a,b) ((a)<(b)?(a):(b))
685 #define max(a,b) ((a)>(b)?(a):(b))
687 /* Install the string STR as the beginning of the string of echoing,
688 so that it serves as a prompt for the next character.
689 Also start echoing. */
691 void
692 echo_prompt (str)
693 Lisp_Object str;
695 int nbytes = STRING_BYTES (XSTRING (str));
696 int multibyte_p = STRING_MULTIBYTE (str);
698 if (nbytes > ECHOBUFSIZE - 4)
700 if (multibyte_p)
702 /* Have to find the last character that fit's into the
703 echo buffer. */
704 unsigned char *p = XSTRING (str)->data;
705 unsigned char *pend = p + ECHOBUFSIZE - 4;
706 int char_len;
710 PARSE_MULTIBYTE_SEQ (p, pend - p, char_len);
711 p += char_len;
713 while (p < pend);
715 nbytes = p - XSTRING (str)->data - char_len;
717 else
718 nbytes = ECHOBUFSIZE - 4;
721 nbytes = copy_text (XSTRING (str)->data, current_kboard->echobuf, nbytes,
722 STRING_MULTIBYTE (str), 1);
723 current_kboard->echoptr = current_kboard->echobuf + nbytes;
724 *current_kboard->echoptr = '\0';
725 current_kboard->echo_after_prompt = nbytes;
727 echo_now ();
730 /* Add C to the echo string, if echoing is going on.
731 C can be a character, which is printed prettily ("M-C-x" and all that
732 jazz), or a symbol, whose name is printed. */
734 void
735 echo_char (c)
736 Lisp_Object c;
738 if (current_kboard->immediate_echo)
740 char *ptr = current_kboard->echoptr;
742 if (ptr != current_kboard->echobuf)
743 *ptr++ = ' ';
745 /* If someone has passed us a composite event, use its head symbol. */
746 c = EVENT_HEAD (c);
748 if (INTEGERP (c))
750 int ch = XINT (c);
752 if (ptr - current_kboard->echobuf
753 > ECHOBUFSIZE - KEY_DESCRIPTION_SIZE)
754 return;
756 ptr = push_key_description (ch, ptr, 1);
758 else if (SYMBOLP (c))
760 struct Lisp_String *name = XSYMBOL (c)->name;
761 if ((ptr - current_kboard->echobuf) + STRING_BYTES (name) + 4
762 > ECHOBUFSIZE)
763 return;
764 ptr += copy_text (name->data, ptr, STRING_BYTES (name),
765 name->size_byte >= 0, 1);
768 if (current_kboard->echoptr == current_kboard->echobuf
769 && help_char_p (c))
771 strcpy (ptr, " (Type ? for further options)");
772 ptr += strlen (ptr);
775 *ptr = 0;
776 current_kboard->echoptr = ptr;
778 echo_now ();
782 /* Temporarily add a dash to the end of the echo string if it's not
783 empty, so that it serves as a mini-prompt for the very next character. */
785 void
786 echo_dash ()
788 if (!current_kboard->immediate_echo
789 && current_kboard->echoptr == current_kboard->echobuf)
790 return;
791 /* Do nothing if we just printed a prompt. */
792 if (current_kboard->echo_after_prompt
793 == current_kboard->echoptr - current_kboard->echobuf)
794 return;
795 /* Do nothing if not echoing at all. */
796 if (current_kboard->echoptr == 0)
797 return;
799 /* Put a dash at the end of the buffer temporarily,
800 but make it go away when the next character is added. */
801 current_kboard->echoptr[0] = '-';
802 current_kboard->echoptr[1] = 0;
804 echo_now ();
807 /* Display the current echo string, and begin echoing if not already
808 doing so. */
810 void
811 echo_now ()
813 if (!current_kboard->immediate_echo)
815 int i;
816 current_kboard->immediate_echo = 1;
818 for (i = 0; i < this_command_key_count; i++)
820 Lisp_Object c;
821 c = XVECTOR (this_command_keys)->contents[i];
822 if (! (EVENT_HAS_PARAMETERS (c)
823 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
824 echo_char (c);
826 echo_dash ();
829 echoing = 1;
830 message2_nolog (current_kboard->echobuf, strlen (current_kboard->echobuf),
832 echoing = 0;
834 /* Record in what buffer we echoed, and from which kboard. */
835 echo_message_buffer = echo_area_buffer[0];
836 echo_kboard = current_kboard;
838 if (waiting_for_input && !NILP (Vquit_flag))
839 quit_throw_to_read_char ();
842 /* Turn off echoing, for the start of a new command. */
844 void
845 cancel_echoing ()
847 current_kboard->immediate_echo = 0;
848 current_kboard->echoptr = current_kboard->echobuf;
849 current_kboard->echo_after_prompt = -1;
850 ok_to_echo_at_next_pause = NULL;
851 echo_kboard = NULL;
852 echo_message_buffer = Qnil;
855 /* Return the length of the current echo string. */
857 static int
858 echo_length ()
860 return current_kboard->echoptr - current_kboard->echobuf;
863 /* Truncate the current echo message to its first LEN chars.
864 This and echo_char get used by read_key_sequence when the user
865 switches frames while entering a key sequence. */
867 static void
868 echo_truncate (len)
869 int len;
871 current_kboard->echobuf[len] = '\0';
872 current_kboard->echoptr = current_kboard->echobuf + len;
873 truncate_echo_area (len);
877 /* Functions for manipulating this_command_keys. */
878 static void
879 add_command_key (key)
880 Lisp_Object key;
882 int size = XVECTOR (this_command_keys)->size;
884 /* If reset-this-command-length was called recently, obey it now.
885 See the doc string of that function for an explanation of why. */
886 if (before_command_restore_flag)
888 this_command_key_count = before_command_key_count_1;
889 if (this_command_key_count < this_single_command_key_start)
890 this_single_command_key_start = this_command_key_count;
891 echo_truncate (before_command_echo_length_1);
892 before_command_restore_flag = 0;
895 if (this_command_key_count >= size)
897 Lisp_Object new_keys;
899 new_keys = Fmake_vector (make_number (size * 2), Qnil);
900 bcopy (XVECTOR (this_command_keys)->contents,
901 XVECTOR (new_keys)->contents,
902 size * sizeof (Lisp_Object));
904 this_command_keys = new_keys;
907 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
910 Lisp_Object
911 recursive_edit_1 ()
913 int count = specpdl_ptr - specpdl;
914 Lisp_Object val;
916 if (command_loop_level > 0)
918 specbind (Qstandard_output, Qt);
919 specbind (Qstandard_input, Qt);
922 #ifdef HAVE_X_WINDOWS
923 /* The command loop has started an hourglass timer, so we have to
924 cancel it here, otherwise it will fire because the recursive edit
925 can take some time. */
926 if (display_hourglass_p)
927 cancel_hourglass ();
928 #endif
930 /* This function may have been called from a debugger called from
931 within redisplay, for instance by Edebugging a function called
932 from fontification-functions. We want to allow redisplay in
933 the debugging session.
935 The recursive edit is left with a `(throw exit ...)'. The `exit'
936 tag is not caught anywhere in redisplay, i.e. when we leave the
937 recursive edit, the original redisplay leading to the recursive
938 edit will be unwound. The outcome should therefore be safe. */
939 specbind (Qinhibit_redisplay, Qnil);
940 redisplaying_p = 0;
942 val = command_loop ();
943 if (EQ (val, Qt))
944 Fsignal (Qquit, Qnil);
945 /* Handle throw from read_minibuf when using minibuffer
946 while it's active but we're in another window. */
947 if (STRINGP (val))
948 Fsignal (Qerror, Fcons (val, Qnil));
950 return unbind_to (count, Qnil);
953 /* When an auto-save happens, record the "time", and don't do again soon. */
955 void
956 record_auto_save ()
958 last_auto_save = num_nonmacro_input_events;
961 /* Make an auto save happen as soon as possible at command level. */
963 void
964 force_auto_save_soon ()
966 last_auto_save = - auto_save_interval - 1;
968 record_asynch_buffer_change ();
971 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
972 "Invoke the editor command loop recursively.\n\
973 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
974 that tells this function to return.\n\
975 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
976 This function is called by the editor initialization to begin editing.")
979 int count = specpdl_ptr - specpdl;
981 command_loop_level++;
982 update_mode_lines = 1;
984 record_unwind_protect (recursive_edit_unwind,
985 (command_loop_level
986 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
987 ? Fcurrent_buffer ()
988 : Qnil);
989 recursive_edit_1 ();
990 return unbind_to (count, Qnil);
993 Lisp_Object
994 recursive_edit_unwind (buffer)
995 Lisp_Object buffer;
997 if (!NILP (buffer))
998 Fset_buffer (buffer);
1000 command_loop_level--;
1001 update_mode_lines = 1;
1002 return Qnil;
1005 static void
1006 any_kboard_state ()
1008 #ifdef MULTI_KBOARD
1009 #if 0 /* Theory: if there's anything in Vunread_command_events,
1010 it will right away be read by read_key_sequence,
1011 and then if we do switch KBOARDS, it will go into the side
1012 queue then. So we don't need to do anything special here -- rms. */
1013 if (CONSP (Vunread_command_events))
1015 current_kboard->kbd_queue
1016 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
1017 current_kboard->kbd_queue_has_data = 1;
1019 Vunread_command_events = Qnil;
1020 #endif
1021 single_kboard = 0;
1022 #endif
1025 /* Switch to the single-kboard state, making current_kboard
1026 the only KBOARD from which further input is accepted. */
1028 void
1029 single_kboard_state ()
1031 #ifdef MULTI_KBOARD
1032 single_kboard = 1;
1033 #endif
1036 /* Maintain a stack of kboards, so other parts of Emacs
1037 can switch temporarily to the kboard of a given frame
1038 and then revert to the previous status. */
1040 struct kboard_stack
1042 KBOARD *kboard;
1043 struct kboard_stack *next;
1046 static struct kboard_stack *kboard_stack;
1048 void
1049 push_frame_kboard (f)
1050 FRAME_PTR f;
1052 #ifdef MULTI_KBOARD
1053 struct kboard_stack *p
1054 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
1056 p->next = kboard_stack;
1057 p->kboard = current_kboard;
1058 kboard_stack = p;
1060 current_kboard = FRAME_KBOARD (f);
1061 #endif
1064 void
1065 pop_frame_kboard ()
1067 #ifdef MULTI_KBOARD
1068 struct kboard_stack *p = kboard_stack;
1069 current_kboard = p->kboard;
1070 kboard_stack = p->next;
1071 xfree (p);
1072 #endif
1075 /* Handle errors that are not handled at inner levels
1076 by printing an error message and returning to the editor command loop. */
1078 Lisp_Object
1079 cmd_error (data)
1080 Lisp_Object data;
1082 Lisp_Object old_level, old_length;
1083 char macroerror[50];
1085 if (!NILP (executing_macro))
1087 if (executing_macro_iterations == 1)
1088 sprintf (macroerror, "After 1 kbd macro iteration: ");
1089 else
1090 sprintf (macroerror, "After %d kbd macro iterations: ",
1091 executing_macro_iterations);
1093 else
1094 *macroerror = 0;
1096 Vstandard_output = Qt;
1097 Vstandard_input = Qt;
1098 Vexecuting_macro = Qnil;
1099 executing_macro = Qnil;
1100 current_kboard->Vprefix_arg = Qnil;
1101 current_kboard->Vlast_prefix_arg = Qnil;
1102 cancel_echoing ();
1104 /* Avoid unquittable loop if data contains a circular list. */
1105 old_level = Vprint_level;
1106 old_length = Vprint_length;
1107 XSETFASTINT (Vprint_level, 10);
1108 XSETFASTINT (Vprint_length, 10);
1109 cmd_error_internal (data, macroerror);
1110 Vprint_level = old_level;
1111 Vprint_length = old_length;
1113 Vquit_flag = Qnil;
1115 Vinhibit_quit = Qnil;
1116 #ifdef MULTI_KBOARD
1117 any_kboard_state ();
1118 #endif
1120 return make_number (0);
1123 /* Take actions on handling an error. DATA is the data that describes
1124 the error.
1126 CONTEXT is a C-string containing ASCII characters only which
1127 describes the context in which the error happened. If we need to
1128 generalize CONTEXT to allow multibyte characters, make it a Lisp
1129 string. */
1131 void
1132 cmd_error_internal (data, context)
1133 Lisp_Object data;
1134 char *context;
1136 Lisp_Object stream;
1137 int kill_emacs_p = 0;
1138 struct frame *sf = SELECTED_FRAME ();
1140 Vquit_flag = Qnil;
1141 Vinhibit_quit = Qt;
1142 clear_message (1, 0);
1144 /* If the window system or terminal frame hasn't been initialized
1145 yet, or we're not interactive, it's best to dump this message out
1146 to stderr and exit. */
1147 if (!sf->glyphs_initialized_p
1148 /* This is the case of the frame dumped with Emacs, when we're
1149 running under a window system. */
1150 || (!NILP (Vwindow_system)
1151 && !inhibit_window_system
1152 && FRAME_TERMCAP_P (sf))
1153 || noninteractive)
1155 stream = Qexternal_debugging_output;
1156 kill_emacs_p = 1;
1158 else
1160 Fdiscard_input ();
1161 bitch_at_user ();
1162 stream = Qt;
1165 if (context != 0)
1166 write_string_1 (context, -1, stream);
1168 print_error_message (data, stream);
1170 /* If the window system or terminal frame hasn't been initialized
1171 yet, or we're in -batch mode, this error should cause Emacs to exit. */
1172 if (kill_emacs_p)
1174 Fterpri (stream);
1175 Fkill_emacs (make_number (-1));
1179 Lisp_Object command_loop_1 ();
1180 Lisp_Object command_loop_2 ();
1181 Lisp_Object top_level_1 ();
1183 /* Entry to editor-command-loop.
1184 This level has the catches for exiting/returning to editor command loop.
1185 It returns nil to exit recursive edit, t to abort it. */
1187 Lisp_Object
1188 command_loop ()
1190 if (command_loop_level > 0 || minibuf_level > 0)
1192 Lisp_Object val;
1193 val = internal_catch (Qexit, command_loop_2, Qnil);
1194 executing_macro = Qnil;
1195 return val;
1197 else
1198 while (1)
1200 internal_catch (Qtop_level, top_level_1, Qnil);
1201 internal_catch (Qtop_level, command_loop_2, Qnil);
1202 executing_macro = Qnil;
1204 /* End of file in -batch run causes exit here. */
1205 if (noninteractive)
1206 Fkill_emacs (Qt);
1210 /* Here we catch errors in execution of commands within the
1211 editing loop, and reenter the editing loop.
1212 When there is an error, cmd_error runs and returns a non-nil
1213 value to us. A value of nil means that cmd_loop_1 itself
1214 returned due to end of file (or end of kbd macro). */
1216 Lisp_Object
1217 command_loop_2 ()
1219 register Lisp_Object val;
1222 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
1223 while (!NILP (val));
1225 return Qnil;
1228 Lisp_Object
1229 top_level_2 ()
1231 return Feval (Vtop_level);
1234 Lisp_Object
1235 top_level_1 ()
1237 /* On entry to the outer level, run the startup file */
1238 if (!NILP (Vtop_level))
1239 internal_condition_case (top_level_2, Qerror, cmd_error);
1240 else if (!NILP (Vpurify_flag))
1241 message ("Bare impure Emacs (standard Lisp code not loaded)");
1242 else
1243 message ("Bare Emacs (standard Lisp code not loaded)");
1244 return Qnil;
1247 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1248 "Exit all recursive editing levels.")
1251 #ifdef HAVE_X_WINDOWS
1252 if (display_hourglass_p)
1253 cancel_hourglass ();
1254 #endif
1255 return Fthrow (Qtop_level, Qnil);
1258 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1259 "Exit from the innermost recursive edit or minibuffer.")
1262 if (command_loop_level > 0 || minibuf_level > 0)
1263 Fthrow (Qexit, Qnil);
1265 error ("No recursive edit is in progress");
1266 return Qnil;
1269 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1270 "Abort the command that requested this recursive edit or minibuffer input.")
1273 if (command_loop_level > 0 || minibuf_level > 0)
1274 Fthrow (Qexit, Qt);
1276 error ("No recursive edit is in progress");
1277 return Qnil;
1280 /* This is the actual command reading loop,
1281 sans error-handling encapsulation. */
1283 Lisp_Object Fcommand_execute ();
1284 static int read_key_sequence ();
1285 void safe_run_hooks ();
1286 static void adjust_point_for_property ();
1288 Lisp_Object
1289 command_loop_1 ()
1291 Lisp_Object cmd;
1292 int lose;
1293 int nonundocount;
1294 Lisp_Object keybuf[30];
1295 int i;
1296 int no_direct;
1297 int prev_modiff;
1298 struct buffer *prev_buffer = NULL;
1299 #ifdef MULTI_KBOARD
1300 int was_locked = single_kboard;
1301 #endif
1303 current_kboard->Vprefix_arg = Qnil;
1304 current_kboard->Vlast_prefix_arg = Qnil;
1305 Vdeactivate_mark = Qnil;
1306 waiting_for_input = 0;
1307 cancel_echoing ();
1309 nonundocount = 0;
1310 this_command_key_count = 0;
1311 this_single_command_key_start = 0;
1313 /* Make sure this hook runs after commands that get errors and
1314 throw to top level. */
1315 /* Note that the value cell will never directly contain nil
1316 if the symbol is a local variable. */
1317 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1318 safe_run_hooks (Qpost_command_hook);
1320 /* If displaying a message, resize the echo area window to fit
1321 that message's size exactly. */
1322 if (!NILP (echo_area_buffer[0]))
1323 resize_echo_area_axactly ();
1325 if (!NILP (Vdeferred_action_list))
1326 call0 (Vdeferred_action_function);
1328 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1330 if (NILP (Vunread_command_events)
1331 && NILP (Vunread_input_method_events)
1332 && NILP (Vunread_post_input_method_events)
1333 && NILP (Vexecuting_macro)
1334 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1335 safe_run_hooks (Qpost_command_idle_hook);
1338 /* Do this after running Vpost_command_hook, for consistency. */
1339 current_kboard->Vlast_command = Vthis_command;
1340 current_kboard->Vreal_last_command = real_this_command;
1342 while (1)
1344 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1345 Fkill_emacs (Qnil);
1347 /* Make sure the current window's buffer is selected. */
1348 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1349 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1351 /* Display any malloc warning that just came out. Use while because
1352 displaying one warning can cause another. */
1354 while (pending_malloc_warning)
1355 display_malloc_warning ();
1357 no_direct = 0;
1359 Vdeactivate_mark = Qnil;
1361 /* If minibuffer on and echo area in use,
1362 wait a short time and redraw minibuffer. */
1364 if (minibuf_level
1365 && !NILP (echo_area_buffer[0])
1366 && EQ (minibuf_window, echo_area_window)
1367 && NUMBERP (Vminibuffer_message_timeout))
1369 /* Bind inhibit-quit to t so that C-g gets read in
1370 rather than quitting back to the minibuffer. */
1371 int count = specpdl_ptr - specpdl;
1372 specbind (Qinhibit_quit, Qt);
1374 Fsit_for (Vminibuffer_message_timeout, Qnil, Qnil);
1375 /* Clear the echo area. */
1376 message2 (0, 0, 0);
1377 safe_run_hooks (Qecho_area_clear_hook);
1379 unbind_to (count, Qnil);
1381 /* If a C-g came in before, treat it as input now. */
1382 if (!NILP (Vquit_flag))
1384 Vquit_flag = Qnil;
1385 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1389 #ifdef C_ALLOCA
1390 alloca (0); /* Cause a garbage collection now */
1391 /* Since we can free the most stuff here. */
1392 #endif /* C_ALLOCA */
1394 #if 0
1395 /* Select the frame that the last event came from. Usually,
1396 switch-frame events will take care of this, but if some lisp
1397 code swallows a switch-frame event, we'll fix things up here.
1398 Is this a good idea? */
1399 if (FRAMEP (internal_last_event_frame)
1400 && !EQ (internal_last_event_frame, selected_frame))
1401 Fselect_frame (internal_last_event_frame, Qnil);
1402 #endif
1403 /* If it has changed current-menubar from previous value,
1404 really recompute the menubar from the value. */
1405 if (! NILP (Vlucid_menu_bar_dirty_flag)
1406 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1407 call0 (Qrecompute_lucid_menubar);
1409 before_command_key_count = this_command_key_count;
1410 before_command_echo_length = echo_length ();
1412 Vthis_command = Qnil;
1413 real_this_command = Qnil;
1415 /* Read next key sequence; i gets its length. */
1416 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1417 Qnil, 0, 1, 1);
1419 /* A filter may have run while we were reading the input. */
1420 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1421 Fkill_emacs (Qnil);
1422 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1423 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1425 ++num_input_keys;
1427 /* Now we have read a key sequence of length I,
1428 or else I is 0 and we found end of file. */
1430 if (i == 0) /* End of file -- happens only in */
1431 return Qnil; /* a kbd macro, at the end. */
1432 /* -1 means read_key_sequence got a menu that was rejected.
1433 Just loop around and read another command. */
1434 if (i == -1)
1436 cancel_echoing ();
1437 this_command_key_count = 0;
1438 this_single_command_key_start = 0;
1439 goto finalize;
1442 last_command_char = keybuf[i - 1];
1444 /* If the previous command tried to force a specific window-start,
1445 forget about that, in case this command moves point far away
1446 from that position. But also throw away beg_unchanged and
1447 end_unchanged information in that case, so that redisplay will
1448 update the whole window properly. */
1449 if (!NILP (XWINDOW (selected_window)->force_start))
1451 struct buffer *b;
1452 XWINDOW (selected_window)->force_start = Qnil;
1453 b = XBUFFER (XWINDOW (selected_window)->buffer);
1454 BUF_BEG_UNCHANGED (b) = BUF_END_UNCHANGED (b) = 0;
1457 cmd = read_key_sequence_cmd;
1458 if (!NILP (Vexecuting_macro))
1460 if (!NILP (Vquit_flag))
1462 Vexecuting_macro = Qt;
1463 QUIT; /* Make some noise. */
1464 /* Will return since macro now empty. */
1468 /* Do redisplay processing after this command except in special
1469 cases identified below. */
1470 prev_buffer = current_buffer;
1471 prev_modiff = MODIFF;
1472 last_point_position = PT;
1473 XSETBUFFER (last_point_position_buffer, prev_buffer);
1475 /* By default, we adjust point to a boundary of a region that
1476 has such a property that should be treated intangible
1477 (e.g. composition, display). But, some commands will set
1478 this variable differently. */
1479 Vdisable_point_adjustment = Qnil;
1481 /* Execute the command. */
1483 Vthis_command = cmd;
1484 real_this_command = cmd;
1485 /* Note that the value cell will never directly contain nil
1486 if the symbol is a local variable. */
1487 if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
1488 safe_run_hooks (Qpre_command_hook);
1490 if (NILP (Vthis_command))
1492 /* nil means key is undefined. */
1493 bitch_at_user ();
1494 current_kboard->defining_kbd_macro = Qnil;
1495 update_mode_lines = 1;
1496 current_kboard->Vprefix_arg = Qnil;
1498 else
1500 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1502 /* In case we jump to directly_done. */
1503 Vcurrent_prefix_arg = current_kboard->Vprefix_arg;
1505 /* Recognize some common commands in common situations and
1506 do them directly. */
1507 if (EQ (Vthis_command, Qforward_char) && PT < ZV)
1509 struct Lisp_Char_Table *dp
1510 = window_display_table (XWINDOW (selected_window));
1511 lose = FETCH_CHAR (PT_BYTE);
1512 SET_PT (PT + 1);
1513 if ((dp
1514 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1515 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1516 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1517 && (lose >= 0x20 && lose < 0x7f)))
1518 : (lose >= 0x20 && lose < 0x7f))
1519 /* To extract the case of continuation on
1520 wide-column characters. */
1521 && (WIDTH_BY_CHAR_HEAD (FETCH_BYTE (PT_BYTE)) == 1)
1522 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1523 >= MODIFF)
1524 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1525 >= OVERLAY_MODIFF)
1526 && (XFASTINT (XWINDOW (selected_window)->last_point)
1527 == PT - 1)
1528 && !windows_or_buffers_changed
1529 && EQ (current_buffer->selective_display, Qnil)
1530 && !detect_input_pending ()
1531 && NILP (XWINDOW (selected_window)->column_number_displayed)
1532 && NILP (Vexecuting_macro))
1533 direct_output_forward_char (1);
1534 goto directly_done;
1536 else if (EQ (Vthis_command, Qbackward_char) && PT > BEGV)
1538 struct Lisp_Char_Table *dp
1539 = window_display_table (XWINDOW (selected_window));
1540 SET_PT (PT - 1);
1541 lose = FETCH_CHAR (PT_BYTE);
1542 if ((dp
1543 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1544 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1545 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1546 && (lose >= 0x20 && lose < 0x7f)))
1547 : (lose >= 0x20 && lose < 0x7f))
1548 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1549 >= MODIFF)
1550 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1551 >= OVERLAY_MODIFF)
1552 && (XFASTINT (XWINDOW (selected_window)->last_point)
1553 == PT + 1)
1554 && !windows_or_buffers_changed
1555 && EQ (current_buffer->selective_display, Qnil)
1556 && !detect_input_pending ()
1557 && NILP (XWINDOW (selected_window)->column_number_displayed)
1558 && NILP (Vexecuting_macro))
1559 direct_output_forward_char (-1);
1560 goto directly_done;
1562 else if (EQ (Vthis_command, Qself_insert_command)
1563 /* Try this optimization only on ascii keystrokes. */
1564 && INTEGERP (last_command_char))
1566 unsigned int c = XINT (last_command_char);
1567 int value;
1568 if (NILP (Vexecuting_macro)
1569 && !EQ (minibuf_window, selected_window))
1571 if (!nonundocount || nonundocount >= 20)
1573 Fundo_boundary ();
1574 nonundocount = 0;
1576 nonundocount++;
1579 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1580 < MODIFF)
1581 || (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1582 < OVERLAY_MODIFF)
1583 || (XFASTINT (XWINDOW (selected_window)->last_point)
1584 != PT)
1585 || MODIFF <= SAVE_MODIFF
1586 || windows_or_buffers_changed
1587 || !EQ (current_buffer->selective_display, Qnil)
1588 || detect_input_pending ()
1589 || !NILP (XWINDOW (selected_window)->column_number_displayed)
1590 || !NILP (Vexecuting_macro));
1592 value = internal_self_insert (c, 0);
1594 if (value == 2)
1595 nonundocount = 0;
1597 /* VALUE == 1 when AFTER-CHANGE functions are
1598 installed which is the case most of the time
1599 because FONT-LOCK installs one. */
1600 if (!lose && !value)
1601 direct_output_for_insert (c);
1602 goto directly_done;
1606 /* Here for a command that isn't executed directly */
1608 #ifdef HAVE_X_WINDOWS
1609 if (display_hourglass_p)
1610 start_hourglass ();
1611 #endif
1613 nonundocount = 0;
1614 if (NILP (current_kboard->Vprefix_arg))
1615 Fundo_boundary ();
1616 Fcommand_execute (Vthis_command, Qnil, Qnil, Qnil);
1618 #ifdef HAVE_X_WINDOWS
1619 if (display_hourglass_p)
1620 cancel_hourglass ();
1621 #endif
1623 directly_done: ;
1624 current_kboard->Vlast_prefix_arg = Vcurrent_prefix_arg;
1626 /* Note that the value cell will never directly contain nil
1627 if the symbol is a local variable. */
1628 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1629 safe_run_hooks (Qpost_command_hook);
1631 /* If displaying a message, resize the echo area window to fit
1632 that message's size exactly. */
1633 if (!NILP (echo_area_buffer[0]))
1634 resize_echo_area_axactly ();
1636 if (!NILP (Vdeferred_action_list))
1637 safe_run_hooks (Qdeferred_action_function);
1639 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1641 if (NILP (Vunread_command_events)
1642 && NILP (Vunread_input_method_events)
1643 && NILP (Vunread_post_input_method_events)
1644 && NILP (Vexecuting_macro)
1645 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1646 safe_run_hooks (Qpost_command_idle_hook);
1649 /* If there is a prefix argument,
1650 1) We don't want Vlast_command to be ``universal-argument''
1651 (that would be dumb), so don't set Vlast_command,
1652 2) we want to leave echoing on so that the prefix will be
1653 echoed as part of this key sequence, so don't call
1654 cancel_echoing, and
1655 3) we want to leave this_command_key_count non-zero, so that
1656 read_char will realize that it is re-reading a character, and
1657 not echo it a second time.
1659 If the command didn't actually create a prefix arg,
1660 but is merely a frame event that is transparent to prefix args,
1661 then the above doesn't apply. */
1662 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1664 current_kboard->Vlast_command = Vthis_command;
1665 current_kboard->Vreal_last_command = real_this_command;
1666 cancel_echoing ();
1667 this_command_key_count = 0;
1668 this_single_command_key_start = 0;
1671 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1673 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1675 current_buffer->mark_active = Qnil;
1676 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1678 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1679 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1682 finalize:
1684 if (current_buffer == prev_buffer
1685 && last_point_position != PT
1686 && NILP (Vdisable_point_adjustment)
1687 && NILP (Vglobal_disable_point_adjustment))
1688 adjust_point_for_property (last_point_position);
1690 /* Install chars successfully executed in kbd macro. */
1692 if (!NILP (current_kboard->defining_kbd_macro)
1693 && NILP (current_kboard->Vprefix_arg))
1694 finalize_kbd_macro_chars ();
1696 #ifdef MULTI_KBOARD
1697 if (!was_locked)
1698 any_kboard_state ();
1699 #endif
1703 extern Lisp_Object Qcomposition, Qdisplay;
1705 /* Adjust point to a boundary of a region that has such a property
1706 that should be treated intangible. For the moment, we check
1707 `composition' and `display' property. LAST_PT is the last position
1708 of point. */
1710 static void
1711 adjust_point_for_property (last_pt)
1712 int last_pt;
1714 int start, end;
1715 Lisp_Object val;
1716 int check_composition = 1, check_display = 1;
1718 while (check_composition || check_display)
1720 if (check_composition
1721 && PT > BEGV && PT < ZV
1722 && get_property_and_range (PT, Qcomposition, &val, &start, &end, Qnil)
1723 && COMPOSITION_VALID_P (start, end, val)
1724 && start < PT && end > PT
1725 && (last_pt <= start || last_pt >= end))
1727 if (PT < last_pt)
1728 SET_PT (start);
1729 else
1730 SET_PT (end);
1731 check_display = 1;
1733 check_composition = 0;
1734 if (check_display
1735 && PT > BEGV && PT < ZV
1736 && get_property_and_range (PT, Qdisplay, &val, &start, &end, Qnil)
1737 && display_prop_intangible_p (val)
1738 && start < PT && end > PT
1739 && (last_pt <= start || last_pt >= end))
1741 if (PT < last_pt)
1742 SET_PT (start);
1743 else
1744 SET_PT (end);
1745 check_composition = 1;
1747 check_display = 0;
1751 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1753 static Lisp_Object
1754 safe_run_hooks_1 (hook)
1755 Lisp_Object hook;
1757 return call1 (Vrun_hooks, Vinhibit_quit);
1760 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1762 static Lisp_Object
1763 safe_run_hooks_error (data)
1764 Lisp_Object data;
1766 return Fset (Vinhibit_quit, Qnil);
1769 /* If we get an error while running the hook, cause the hook variable
1770 to be nil. Also inhibit quits, so that C-g won't cause the hook
1771 to mysteriously evaporate. */
1773 void
1774 safe_run_hooks (hook)
1775 Lisp_Object hook;
1777 int count = specpdl_ptr - specpdl;
1778 specbind (Qinhibit_quit, hook);
1780 internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
1782 unbind_to (count, Qnil);
1786 /* Number of seconds between polling for input. This is a Lisp
1787 variable that can be bound. */
1789 int polling_period;
1791 /* Nonzero means polling for input is temporarily suppressed. */
1793 int poll_suppress_count;
1795 /* Asynchronous timer for polling. */
1797 struct atimer *poll_timer;
1800 #ifdef POLL_FOR_INPUT
1802 /* Poll for input, so what we catch a C-g if it comes in. This
1803 function is called from x_make_frame_visible, see comment
1804 there. */
1806 void
1807 poll_for_input_1 ()
1809 if (interrupt_input_blocked == 0
1810 && !waiting_for_input)
1811 read_avail_input (0);
1814 /* Timer callback function for poll_timer. TIMER is equal to
1815 poll_timer. */
1817 void
1818 poll_for_input (timer)
1819 struct atimer *timer;
1821 if (poll_suppress_count == 0)
1822 poll_for_input_1 ();
1825 #endif /* POLL_FOR_INPUT */
1827 /* Begin signals to poll for input, if they are appropriate.
1828 This function is called unconditionally from various places. */
1830 void
1831 start_polling ()
1833 #ifdef POLL_FOR_INPUT
1834 if (read_socket_hook && !interrupt_input)
1836 /* Turn alarm handling on unconditionally. It might have
1837 been turned off in process.c. */
1838 turn_on_atimers (1);
1840 /* If poll timer doesn't exist, are we need one with
1841 a different interval, start a new one. */
1842 if (poll_timer == NULL
1843 || EMACS_SECS (poll_timer->interval) != polling_period)
1845 EMACS_TIME interval;
1847 if (poll_timer)
1848 cancel_atimer (poll_timer);
1850 EMACS_SET_SECS_USECS (interval, polling_period, 0);
1851 poll_timer = start_atimer (ATIMER_CONTINUOUS, interval,
1852 poll_for_input, NULL);
1855 /* Let the timer's callback function poll for input
1856 if this becomes zero. */
1857 --poll_suppress_count;
1859 #endif
1862 /* Nonzero if we are using polling to handle input asynchronously. */
1865 input_polling_used ()
1867 #ifdef POLL_FOR_INPUT
1868 return read_socket_hook && !interrupt_input;
1869 #else
1870 return 0;
1871 #endif
1874 /* Turn off polling. */
1876 void
1877 stop_polling ()
1879 #ifdef POLL_FOR_INPUT
1880 if (read_socket_hook && !interrupt_input)
1881 ++poll_suppress_count;
1882 #endif
1885 /* Set the value of poll_suppress_count to COUNT
1886 and start or stop polling accordingly. */
1888 void
1889 set_poll_suppress_count (count)
1890 int count;
1892 #ifdef POLL_FOR_INPUT
1893 if (count == 0 && poll_suppress_count != 0)
1895 poll_suppress_count = 1;
1896 start_polling ();
1898 else if (count != 0 && poll_suppress_count == 0)
1900 stop_polling ();
1902 poll_suppress_count = count;
1903 #endif
1906 /* Bind polling_period to a value at least N.
1907 But don't decrease it. */
1909 void
1910 bind_polling_period (n)
1911 int n;
1913 #ifdef POLL_FOR_INPUT
1914 int new = polling_period;
1916 if (n > new)
1917 new = n;
1919 stop_other_atimers (poll_timer);
1920 stop_polling ();
1921 specbind (Qpolling_period, make_number (new));
1922 /* Start a new alarm with the new period. */
1923 start_polling ();
1924 #endif
1927 /* Apply the control modifier to CHARACTER. */
1930 make_ctrl_char (c)
1931 int c;
1933 /* Save the upper bits here. */
1934 int upper = c & ~0177;
1936 c &= 0177;
1938 /* Everything in the columns containing the upper-case letters
1939 denotes a control character. */
1940 if (c >= 0100 && c < 0140)
1942 int oc = c;
1943 c &= ~0140;
1944 /* Set the shift modifier for a control char
1945 made from a shifted letter. But only for letters! */
1946 if (oc >= 'A' && oc <= 'Z')
1947 c |= shift_modifier;
1950 /* The lower-case letters denote control characters too. */
1951 else if (c >= 'a' && c <= 'z')
1952 c &= ~0140;
1954 /* Include the bits for control and shift
1955 only if the basic ASCII code can't indicate them. */
1956 else if (c >= ' ')
1957 c |= ctrl_modifier;
1959 /* Replace the high bits. */
1960 c |= (upper & ~ctrl_modifier);
1962 return c;
1965 /* Display help echo in the echo area.
1967 HELP a string means display that string, HELP nil means clear the
1968 help echo. If HELP is a function, call it with OBJECT and POS as
1969 arguments; the function should return a help string or nil for
1970 none. For all other types of HELP evaluate it to obtain a string.
1972 WINDOW is the window in which the help was generated, if any.
1973 It is nil if not in a window.
1975 If OBJECT is a buffer, POS is the position in the buffer where the
1976 `help-echo' text property was found.
1978 If OBJECT is an overlay, that overlay has a `help-echo' property,
1979 and POS is the position in the overlay's buffer under the mouse.
1981 If OBJECT is a string (an overlay string or a string displayed with
1982 the `display' property). POS is the position in that string under
1983 the mouse.
1985 OK_TO_IVERWRITE_KEYSTROKE_ECHO non-zero means it's okay if the help
1986 echo overwrites a keystroke echo currently displayed in the echo
1987 area.
1989 Note: this function may only be called with HELP nil or a string
1990 from X code running asynchronously. */
1992 void
1993 show_help_echo (help, window, object, pos, ok_to_overwrite_keystroke_echo)
1994 Lisp_Object help, window, object, pos;
1995 int ok_to_overwrite_keystroke_echo;
1997 if (!NILP (help) && !STRINGP (help))
1999 if (FUNCTIONP (help))
2001 Lisp_Object args[4];
2002 args[0] = help;
2003 args[1] = window;
2004 args[2] = object;
2005 args[3] = pos;
2006 help = safe_call (4, args);
2008 else
2009 help = safe_eval (help);
2011 if (!STRINGP (help))
2012 return;
2015 if (STRINGP (help) || NILP (help))
2017 if (!NILP (Vshow_help_function))
2018 call1 (Vshow_help_function, help);
2019 else if (/* Don't overwrite minibuffer contents. */
2020 !MINI_WINDOW_P (XWINDOW (selected_window))
2021 /* Don't overwrite a keystroke echo. */
2022 && (NILP (echo_message_buffer)
2023 || ok_to_overwrite_keystroke_echo)
2024 /* Don't overwrite a prompt. */
2025 && !cursor_in_echo_area)
2027 if (STRINGP (help))
2029 int count = specpdl_ptr - specpdl;
2030 specbind (Qmessage_truncate_lines, Qt);
2031 message3_nolog (help, STRING_BYTES (XSTRING (help)),
2032 STRING_MULTIBYTE (help));
2033 unbind_to (count, Qnil);
2035 else
2036 message (0);
2039 help_echo_showing_p = STRINGP (help);
2045 /* Input of single characters from keyboard */
2047 Lisp_Object print_help ();
2048 static Lisp_Object kbd_buffer_get_event ();
2049 static void record_char ();
2051 #ifdef MULTI_KBOARD
2052 static jmp_buf wrong_kboard_jmpbuf;
2053 #endif
2055 /* read a character from the keyboard; call the redisplay if needed */
2056 /* commandflag 0 means do not do auto-saving, but do do redisplay.
2057 -1 means do not do redisplay, but do do autosaving.
2058 1 means do both. */
2060 /* The arguments MAPS and NMAPS are for menu prompting.
2061 MAPS is an array of keymaps; NMAPS is the length of MAPS.
2063 PREV_EVENT is the previous input event, or nil if we are reading
2064 the first event of a key sequence (or not reading a key sequence).
2065 If PREV_EVENT is t, that is a "magic" value that says
2066 not to run input methods, but in other respects to act as if
2067 not reading a key sequence.
2069 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
2070 if we used a mouse menu to read the input, or zero otherwise. If
2071 USED_MOUSE_MENU is null, we don't dereference it.
2073 Value is t if we showed a menu and the user rejected it. */
2075 Lisp_Object
2076 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
2077 int commandflag;
2078 int nmaps;
2079 Lisp_Object *maps;
2080 Lisp_Object prev_event;
2081 int *used_mouse_menu;
2083 volatile Lisp_Object c;
2084 int count;
2085 jmp_buf local_getcjmp;
2086 jmp_buf save_jump;
2087 volatile int key_already_recorded = 0;
2088 Lisp_Object tem, save;
2089 volatile Lisp_Object previous_echo_area_message;
2090 volatile Lisp_Object also_record;
2091 volatile int reread;
2092 struct gcpro gcpro1, gcpro2;
2094 also_record = Qnil;
2096 before_command_key_count = this_command_key_count;
2097 before_command_echo_length = echo_length ();
2098 c = Qnil;
2099 previous_echo_area_message = Qnil;
2101 GCPRO2 (c, previous_echo_area_message);
2103 retry:
2105 reread = 0;
2106 if (CONSP (Vunread_post_input_method_events))
2108 c = XCAR (Vunread_post_input_method_events);
2109 Vunread_post_input_method_events
2110 = XCDR (Vunread_post_input_method_events);
2112 /* Undo what read_char_x_menu_prompt did when it unread
2113 additional keys returned by Fx_popup_menu. */
2114 if (CONSP (c)
2115 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2116 && NILP (XCDR (c)))
2117 c = XCAR (c);
2119 reread = 1;
2120 goto reread_first;
2123 if (unread_command_char != -1)
2125 XSETINT (c, unread_command_char);
2126 unread_command_char = -1;
2128 reread = 1;
2129 goto reread_first;
2132 if (CONSP (Vunread_command_events))
2134 c = XCAR (Vunread_command_events);
2135 Vunread_command_events = XCDR (Vunread_command_events);
2137 /* Undo what read_char_x_menu_prompt did when it unread
2138 additional keys returned by Fx_popup_menu. */
2139 if (CONSP (c)
2140 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2141 && NILP (XCDR (c)))
2142 c = XCAR (c);
2144 /* If the queued event is something that used the mouse,
2145 set used_mouse_menu accordingly. */
2146 if (used_mouse_menu
2147 && (EQ (c, Qtool_bar) || EQ (c, Qmenu_bar)))
2148 *used_mouse_menu = 1;
2150 reread = 1;
2151 goto reread_for_input_method;
2154 if (CONSP (Vunread_input_method_events))
2156 c = XCAR (Vunread_input_method_events);
2157 Vunread_input_method_events = XCDR (Vunread_input_method_events);
2159 /* Undo what read_char_x_menu_prompt did when it unread
2160 additional keys returned by Fx_popup_menu. */
2161 if (CONSP (c)
2162 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2163 && NILP (XCDR (c)))
2164 c = XCAR (c);
2165 reread = 1;
2166 goto reread_for_input_method;
2169 /* If there is no function key translated before
2170 reset-this-command-lengths takes effect, forget about it. */
2171 before_command_restore_flag = 0;
2173 if (!NILP (Vexecuting_macro))
2175 /* We set this to Qmacro; since that's not a frame, nobody will
2176 try to switch frames on us, and the selected window will
2177 remain unchanged.
2179 Since this event came from a macro, it would be misleading to
2180 leave internal_last_event_frame set to wherever the last
2181 real event came from. Normally, a switch-frame event selects
2182 internal_last_event_frame after each command is read, but
2183 events read from a macro should never cause a new frame to be
2184 selected. */
2185 Vlast_event_frame = internal_last_event_frame = Qmacro;
2187 /* Exit the macro if we are at the end.
2188 Also, some things replace the macro with t
2189 to force an early exit. */
2190 if (EQ (Vexecuting_macro, Qt)
2191 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
2193 XSETINT (c, -1);
2194 RETURN_UNGCPRO (c);
2197 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
2198 if (STRINGP (Vexecuting_macro)
2199 && (XINT (c) & 0x80))
2200 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
2202 executing_macro_index++;
2204 goto from_macro;
2207 if (!NILP (unread_switch_frame))
2209 c = unread_switch_frame;
2210 unread_switch_frame = Qnil;
2212 /* This event should make it into this_command_keys, and get echoed
2213 again, so we do not set `reread'. */
2214 goto reread_first;
2217 /* if redisplay was requested */
2218 if (commandflag >= 0)
2220 /* If there is pending input, process any events which are not
2221 user-visible, such as X selection_request events. */
2222 if (input_pending
2223 || detect_input_pending_run_timers (0))
2224 swallow_events (0); /* may clear input_pending */
2226 /* Redisplay if no pending input. */
2227 while (!input_pending)
2229 if (help_echo_showing_p && !EQ (selected_window, minibuf_window))
2230 redisplay_preserve_echo_area (5);
2231 else
2232 redisplay ();
2234 if (!input_pending)
2235 /* Normal case: no input arrived during redisplay. */
2236 break;
2238 /* Input arrived and pre-empted redisplay.
2239 Process any events which are not user-visible. */
2240 swallow_events (0);
2241 /* If that cleared input_pending, try again to redisplay. */
2245 /* Message turns off echoing unless more keystrokes turn it on again.
2247 The code in 20.x for the condition was
2249 1. echo_area_glyphs && *echo_area_glyphs
2250 2. && echo_area_glyphs != current_kboard->echobuf
2251 3. && ok_to_echo_at_next_pause != echo_area_glyphs
2253 (1) means there's a current message displayed
2255 (2) means it's not the message from echoing from the current
2256 kboard.
2258 (3) There's only one place in 20.x where ok_to_echo_at_next_pause
2259 is set to a non-null value. This is done in read_char and it is
2260 set to echo_area_glyphs after a call to echo_char. That means
2261 ok_to_echo_at_next_pause is either null or
2262 current_kboard->echobuf with the appropriate current_kboard at
2263 that time.
2265 So, condition (3) means in clear text ok_to_echo_at_next_pause
2266 must be either null, or the current message isn't from echoing at
2267 all, or it's from echoing from a different kboard than the
2268 current one. */
2270 if (/* There currently something in the echo area */
2271 !NILP (echo_area_buffer[0])
2272 && (/* And it's either not from echoing. */
2273 !EQ (echo_area_buffer[0], echo_message_buffer)
2274 /* Or it's an echo from a different kboard. */
2275 || echo_kboard != current_kboard
2276 /* Or we explicitly allow overwriting whatever there is. */
2277 || ok_to_echo_at_next_pause == NULL))
2278 cancel_echoing ();
2279 else
2280 echo_dash ();
2282 /* Try reading a character via menu prompting in the minibuf.
2283 Try this before the sit-for, because the sit-for
2284 would do the wrong thing if we are supposed to do
2285 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
2286 after a mouse event so don't try a minibuf menu. */
2287 c = Qnil;
2288 if (nmaps > 0 && INTERACTIVE
2289 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
2290 /* Don't bring up a menu if we already have another event. */
2291 && NILP (Vunread_command_events)
2292 && unread_command_char < 0
2293 && !detect_input_pending_run_timers (0))
2295 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
2296 if (! NILP (c))
2298 key_already_recorded = 1;
2299 goto non_reread_1;
2303 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
2304 We will do that below, temporarily for short sections of code,
2305 when appropriate. local_getcjmp must be in effect
2306 around any call to sit_for or kbd_buffer_get_event;
2307 it *must not* be in effect when we call redisplay. */
2309 if (_setjmp (local_getcjmp))
2311 XSETINT (c, quit_char);
2312 internal_last_event_frame = selected_frame;
2313 Vlast_event_frame = internal_last_event_frame;
2314 /* If we report the quit char as an event,
2315 don't do so more than once. */
2316 if (!NILP (Vinhibit_quit))
2317 Vquit_flag = Qnil;
2319 #ifdef MULTI_KBOARD
2321 KBOARD *kb = FRAME_KBOARD (XFRAME (selected_frame));
2322 if (kb != current_kboard)
2324 Lisp_Object *tailp = &kb->kbd_queue;
2325 /* We shouldn't get here if we were in single-kboard mode! */
2326 if (single_kboard)
2327 abort ();
2328 while (CONSP (*tailp))
2329 tailp = &XCDR (*tailp);
2330 if (!NILP (*tailp))
2331 abort ();
2332 *tailp = Fcons (c, Qnil);
2333 kb->kbd_queue_has_data = 1;
2334 current_kboard = kb;
2335 /* This is going to exit from read_char
2336 so we had better get rid of this frame's stuff. */
2337 UNGCPRO;
2338 longjmp (wrong_kboard_jmpbuf, 1);
2341 #endif
2342 goto non_reread;
2345 timer_start_idle ();
2347 /* If in middle of key sequence and minibuffer not active,
2348 start echoing if enough time elapses. */
2350 if (minibuf_level == 0
2351 && !current_kboard->immediate_echo
2352 && this_command_key_count > 0
2353 && ! noninteractive
2354 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2355 && NILP (Fzerop (Vecho_keystrokes))
2356 && (/* No message. */
2357 NILP (echo_area_buffer[0])
2358 /* Or empty message. */
2359 || (BUF_BEG (XBUFFER (echo_area_buffer[0]))
2360 == BUF_Z (XBUFFER (echo_area_buffer[0])))
2361 /* Or already echoing from same kboard. */
2362 || (echo_kboard && ok_to_echo_at_next_pause == echo_kboard)
2363 /* Or not echoing before and echoing allowed. */
2364 || (!echo_kboard && ok_to_echo_at_next_pause)))
2366 Lisp_Object tem0;
2368 /* After a mouse event, start echoing right away.
2369 This is because we are probably about to display a menu,
2370 and we don't want to delay before doing so. */
2371 if (EVENT_HAS_PARAMETERS (prev_event))
2372 echo_now ();
2373 else
2375 int sec, usec;
2376 double duration = extract_float (Vecho_keystrokes);
2377 sec = (int) duration;
2378 usec = (duration - sec) * 1000000;
2379 save_getcjmp (save_jump);
2380 restore_getcjmp (local_getcjmp);
2381 tem0 = sit_for (sec, usec, 1, 1, 0);
2382 restore_getcjmp (save_jump);
2383 if (EQ (tem0, Qt)
2384 && ! CONSP (Vunread_command_events))
2385 echo_now ();
2389 /* Maybe auto save due to number of keystrokes. */
2391 if (commandflag != 0
2392 && auto_save_interval > 0
2393 && num_nonmacro_input_events - last_auto_save > max (auto_save_interval, 20)
2394 && !detect_input_pending_run_timers (0))
2396 Fdo_auto_save (Qnil, Qnil);
2397 /* Hooks can actually change some buffers in auto save. */
2398 redisplay ();
2401 /* Try reading using an X menu.
2402 This is never confused with reading using the minibuf
2403 because the recursive call of read_char in read_char_minibuf_menu_prompt
2404 does not pass on any keymaps. */
2406 if (nmaps > 0 && INTERACTIVE
2407 && !NILP (prev_event)
2408 && EVENT_HAS_PARAMETERS (prev_event)
2409 && !EQ (XCAR (prev_event), Qmenu_bar)
2410 && !EQ (XCAR (prev_event), Qtool_bar)
2411 /* Don't bring up a menu if we already have another event. */
2412 && NILP (Vunread_command_events)
2413 && unread_command_char < 0)
2415 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
2417 /* Now that we have read an event, Emacs is not idle. */
2418 timer_stop_idle ();
2420 RETURN_UNGCPRO (c);
2423 /* Maybe autosave and/or garbage collect due to idleness. */
2425 if (INTERACTIVE && NILP (c))
2427 int delay_level, buffer_size;
2429 /* Slow down auto saves logarithmically in size of current buffer,
2430 and garbage collect while we're at it. */
2431 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
2432 last_non_minibuf_size = Z - BEG;
2433 buffer_size = (last_non_minibuf_size >> 8) + 1;
2434 delay_level = 0;
2435 while (buffer_size > 64)
2436 delay_level++, buffer_size -= buffer_size >> 2;
2437 if (delay_level < 4) delay_level = 4;
2438 /* delay_level is 4 for files under around 50k, 7 at 100k,
2439 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
2441 /* Auto save if enough time goes by without input. */
2442 if (commandflag != 0
2443 && num_nonmacro_input_events > last_auto_save
2444 && INTEGERP (Vauto_save_timeout)
2445 && XINT (Vauto_save_timeout) > 0)
2447 Lisp_Object tem0;
2449 save_getcjmp (save_jump);
2450 restore_getcjmp (local_getcjmp);
2451 tem0 = sit_for (delay_level * XFASTINT (Vauto_save_timeout) / 4,
2452 0, 1, 1, 0);
2453 restore_getcjmp (save_jump);
2455 if (EQ (tem0, Qt)
2456 && ! CONSP (Vunread_command_events))
2458 Fdo_auto_save (Qnil, Qnil);
2460 /* If we have auto-saved and there is still no input
2461 available, garbage collect if there has been enough
2462 consing going on to make it worthwhile. */
2463 if (!detect_input_pending_run_timers (0)
2464 && consing_since_gc > gc_cons_threshold / 2)
2465 Fgarbage_collect ();
2467 redisplay ();
2472 /* If this has become non-nil here, it has been set by a timer
2473 or sentinel or filter. */
2474 if (CONSP (Vunread_command_events))
2476 c = XCAR (Vunread_command_events);
2477 Vunread_command_events = XCDR (Vunread_command_events);
2480 /* Read something from current KBOARD's side queue, if possible. */
2482 if (NILP (c))
2484 if (current_kboard->kbd_queue_has_data)
2486 if (!CONSP (current_kboard->kbd_queue))
2487 abort ();
2488 c = XCAR (current_kboard->kbd_queue);
2489 current_kboard->kbd_queue
2490 = XCDR (current_kboard->kbd_queue);
2491 if (NILP (current_kboard->kbd_queue))
2492 current_kboard->kbd_queue_has_data = 0;
2493 input_pending = readable_events (0);
2494 if (EVENT_HAS_PARAMETERS (c)
2495 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
2496 internal_last_event_frame = XCAR (XCDR (c));
2497 Vlast_event_frame = internal_last_event_frame;
2501 #ifdef MULTI_KBOARD
2502 /* If current_kboard's side queue is empty check the other kboards.
2503 If one of them has data that we have not yet seen here,
2504 switch to it and process the data waiting for it.
2506 Note: if the events queued up for another kboard
2507 have already been seen here, and therefore are not a complete command,
2508 the kbd_queue_has_data field is 0, so we skip that kboard here.
2509 That's to avoid an infinite loop switching between kboards here. */
2510 if (NILP (c) && !single_kboard)
2512 KBOARD *kb;
2513 for (kb = all_kboards; kb; kb = kb->next_kboard)
2514 if (kb->kbd_queue_has_data)
2516 current_kboard = kb;
2517 /* This is going to exit from read_char
2518 so we had better get rid of this frame's stuff. */
2519 UNGCPRO;
2520 longjmp (wrong_kboard_jmpbuf, 1);
2523 #endif
2525 wrong_kboard:
2527 stop_polling ();
2529 /* Finally, we read from the main queue,
2530 and if that gives us something we can't use yet, we put it on the
2531 appropriate side queue and try again. */
2533 if (NILP (c))
2535 KBOARD *kb;
2537 /* Actually read a character, waiting if necessary. */
2538 save_getcjmp (save_jump);
2539 restore_getcjmp (local_getcjmp);
2540 timer_start_idle ();
2541 c = kbd_buffer_get_event (&kb, used_mouse_menu);
2542 restore_getcjmp (save_jump);
2544 #ifdef MULTI_KBOARD
2545 if (! NILP (c) && (kb != current_kboard))
2547 Lisp_Object *tailp = &kb->kbd_queue;
2548 while (CONSP (*tailp))
2549 tailp = &XCDR (*tailp);
2550 if (!NILP (*tailp))
2551 abort ();
2552 *tailp = Fcons (c, Qnil);
2553 kb->kbd_queue_has_data = 1;
2554 c = Qnil;
2555 if (single_kboard)
2556 goto wrong_kboard;
2557 current_kboard = kb;
2558 /* This is going to exit from read_char
2559 so we had better get rid of this frame's stuff. */
2560 UNGCPRO;
2561 longjmp (wrong_kboard_jmpbuf, 1);
2563 #endif
2566 /* Terminate Emacs in batch mode if at eof. */
2567 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
2568 Fkill_emacs (make_number (1));
2570 if (INTEGERP (c))
2572 /* Add in any extra modifiers, where appropriate. */
2573 if ((extra_keyboard_modifiers & CHAR_CTL)
2574 || ((extra_keyboard_modifiers & 0177) < ' '
2575 && (extra_keyboard_modifiers & 0177) != 0))
2576 XSETINT (c, make_ctrl_char (XINT (c)));
2578 /* Transfer any other modifier bits directly from
2579 extra_keyboard_modifiers to c. Ignore the actual character code
2580 in the low 16 bits of extra_keyboard_modifiers. */
2581 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
2584 non_reread:
2586 timer_stop_idle ();
2587 start_polling ();
2589 if (NILP (c))
2591 if (commandflag >= 0
2592 && !input_pending && !detect_input_pending_run_timers (0))
2593 redisplay ();
2595 goto wrong_kboard;
2598 non_reread_1:
2600 /* Buffer switch events are only for internal wakeups
2601 so don't show them to the user.
2602 Also, don't record a key if we already did. */
2603 if (BUFFERP (c) || key_already_recorded)
2604 RETURN_UNGCPRO (c);
2606 /* Process special events within read_char
2607 and loop around to read another event. */
2608 save = Vquit_flag;
2609 Vquit_flag = Qnil;
2610 tem = access_keymap (get_keymap (Vspecial_event_map, 0, 1), c, 0, 0, 1);
2611 Vquit_flag = save;
2613 if (!NILP (tem))
2615 int was_locked = single_kboard;
2617 last_input_char = c;
2618 Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt);
2620 /* Resume allowing input from any kboard, if that was true before. */
2621 if (!was_locked)
2622 any_kboard_state ();
2624 goto retry;
2627 /* Handle things that only apply to characters. */
2628 if (INTEGERP (c))
2630 /* If kbd_buffer_get_event gave us an EOF, return that. */
2631 if (XINT (c) == -1)
2632 RETURN_UNGCPRO (c);
2634 if ((STRINGP (Vkeyboard_translate_table)
2635 && XSTRING (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2636 || (VECTORP (Vkeyboard_translate_table)
2637 && XVECTOR (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2638 || (CHAR_TABLE_P (Vkeyboard_translate_table)
2639 && CHAR_TABLE_ORDINARY_SLOTS > (unsigned) XFASTINT (c)))
2641 Lisp_Object d;
2642 d = Faref (Vkeyboard_translate_table, c);
2643 /* nil in keyboard-translate-table means no translation. */
2644 if (!NILP (d))
2645 c = d;
2649 /* If this event is a mouse click in the menu bar,
2650 return just menu-bar for now. Modify the mouse click event
2651 so we won't do this twice, then queue it up. */
2652 if (EVENT_HAS_PARAMETERS (c)
2653 && CONSP (XCDR (c))
2654 && CONSP (EVENT_START (c))
2655 && CONSP (XCDR (EVENT_START (c))))
2657 Lisp_Object posn;
2659 posn = POSN_BUFFER_POSN (EVENT_START (c));
2660 /* Handle menu-bar events:
2661 insert the dummy prefix event `menu-bar'. */
2662 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
2664 /* Change menu-bar to (menu-bar) as the event "position". */
2665 POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
2667 also_record = c;
2668 Vunread_command_events = Fcons (c, Vunread_command_events);
2669 c = posn;
2673 /* Store these characters into recent_keys, the dribble file if any,
2674 and the keyboard macro being defined, if any. */
2675 record_char (c);
2676 if (! NILP (also_record))
2677 record_char (also_record);
2679 /* Wipe the echo area.
2680 But first, if we are about to use an input method,
2681 save the echo area contents for it to refer to. */
2682 if (INTEGERP (c)
2683 && ! NILP (Vinput_method_function)
2684 && (unsigned) XINT (c) >= ' '
2685 && (unsigned) XINT (c) != 127
2686 && (unsigned) XINT (c) < 256)
2688 previous_echo_area_message = Fcurrent_message ();
2689 Vinput_method_previous_message = previous_echo_area_message;
2692 /* Now wipe the echo area, except for help events which do their
2693 own stuff with the echo area. */
2694 if (!CONSP (c) || !(EQ (Qhelp_echo, XCAR (c))))
2696 if (!NILP (echo_area_buffer[0]))
2697 safe_run_hooks (Qecho_area_clear_hook);
2698 clear_message (1, 0);
2701 reread_for_input_method:
2702 from_macro:
2703 /* Pass this to the input method, if appropriate. */
2704 if (INTEGERP (c)
2705 && ! NILP (Vinput_method_function)
2706 /* Don't run the input method within a key sequence,
2707 after the first event of the key sequence. */
2708 && NILP (prev_event)
2709 && (unsigned) XINT (c) >= ' '
2710 && (unsigned) XINT (c) != 127
2711 && (unsigned) XINT (c) < 256)
2713 Lisp_Object keys;
2714 int key_count;
2715 struct gcpro gcpro1;
2716 int count = specpdl_ptr - specpdl;
2718 /* Save the echo status. */
2719 int saved_immediate_echo = current_kboard->immediate_echo;
2720 struct kboard *saved_ok_to_echo = ok_to_echo_at_next_pause;
2721 int saved_echo_after_prompt = current_kboard->echo_after_prompt;
2723 if (before_command_restore_flag)
2725 this_command_key_count = before_command_key_count_1;
2726 if (this_command_key_count < this_single_command_key_start)
2727 this_single_command_key_start = this_command_key_count;
2728 echo_truncate (before_command_echo_length_1);
2729 before_command_restore_flag = 0;
2732 /* Save the this_command_keys status. */
2733 key_count = this_command_key_count;
2735 if (key_count > 0)
2736 keys = Fcopy_sequence (this_command_keys);
2737 else
2738 keys = Qnil;
2739 GCPRO1 (keys);
2741 /* Clear out this_command_keys. */
2742 this_command_key_count = 0;
2744 /* Now wipe the echo area. */
2745 if (!NILP (echo_area_buffer[0]))
2746 safe_run_hooks (Qecho_area_clear_hook);
2747 clear_message (1, 0);
2748 echo_truncate (0);
2750 /* If we are not reading a key sequence,
2751 never use the echo area. */
2752 if (maps == 0)
2754 specbind (Qinput_method_use_echo_area, Qt);
2757 /* Call the input method. */
2758 tem = call1 (Vinput_method_function, c);
2760 tem = unbind_to (count, tem);
2762 /* Restore the saved echoing state
2763 and this_command_keys state. */
2764 this_command_key_count = key_count;
2765 if (key_count > 0)
2766 this_command_keys = keys;
2768 cancel_echoing ();
2769 ok_to_echo_at_next_pause = saved_ok_to_echo;
2770 current_kboard->echo_after_prompt = saved_echo_after_prompt;
2771 if (saved_immediate_echo)
2772 echo_now ();
2774 UNGCPRO;
2776 /* The input method can return no events. */
2777 if (! CONSP (tem))
2779 /* Bring back the previous message, if any. */
2780 if (! NILP (previous_echo_area_message))
2781 message_with_string ("%s", previous_echo_area_message, 0);
2782 goto retry;
2784 /* It returned one event or more. */
2785 c = XCAR (tem);
2786 Vunread_post_input_method_events
2787 = nconc2 (XCDR (tem), Vunread_post_input_method_events);
2790 reread_first:
2792 /* Display help if not echoing. */
2793 if (CONSP (c) && EQ (XCAR (c), Qhelp_echo))
2795 /* (help-echo FRAME HELP WINDOW OBJECT POS). */
2796 Lisp_Object help, object, position, window;
2797 help = Fnth (make_number (2), c);
2798 window = Fnth (make_number (3), c);
2799 object = Fnth (make_number (4), c);
2800 position = Fnth (make_number (5), c);
2801 show_help_echo (help, window, object, position, 0);
2802 goto retry;
2805 if (this_command_key_count == 0 || ! reread)
2807 before_command_key_count = this_command_key_count;
2808 before_command_echo_length = echo_length ();
2810 /* Don't echo mouse motion events. */
2811 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2812 && NILP (Fzerop (Vecho_keystrokes))
2813 && ! (EVENT_HAS_PARAMETERS (c)
2814 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
2816 echo_char (c);
2817 if (! NILP (also_record))
2818 echo_char (also_record);
2819 /* Once we reread a character, echoing can happen
2820 the next time we pause to read a new one. */
2821 ok_to_echo_at_next_pause = current_kboard;
2824 /* Record this character as part of the current key. */
2825 add_command_key (c);
2826 if (! NILP (also_record))
2827 add_command_key (also_record);
2830 last_input_char = c;
2831 num_input_events++;
2833 /* Process the help character specially if enabled */
2834 if (!NILP (Vhelp_form) && help_char_p (c))
2836 Lisp_Object tem0;
2837 count = specpdl_ptr - specpdl;
2839 record_unwind_protect (Fset_window_configuration,
2840 Fcurrent_window_configuration (Qnil));
2842 tem0 = Feval (Vhelp_form);
2843 if (STRINGP (tem0))
2844 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
2846 cancel_echoing ();
2848 c = read_char (0, 0, 0, Qnil, 0);
2849 while (BUFFERP (c));
2850 /* Remove the help from the frame */
2851 unbind_to (count, Qnil);
2853 redisplay ();
2854 if (EQ (c, make_number (040)))
2856 cancel_echoing ();
2858 c = read_char (0, 0, 0, Qnil, 0);
2859 while (BUFFERP (c));
2863 RETURN_UNGCPRO (c);
2866 /* Record a key that came from a mouse menu.
2867 Record it for echoing, for this-command-keys, and so on. */
2869 static void
2870 record_menu_key (c)
2871 Lisp_Object c;
2873 /* Wipe the echo area. */
2874 clear_message (1, 0);
2876 record_char (c);
2878 before_command_key_count = this_command_key_count;
2879 before_command_echo_length = echo_length ();
2881 /* Don't echo mouse motion events. */
2882 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2883 && NILP (Fzerop (Vecho_keystrokes)))
2885 echo_char (c);
2887 /* Once we reread a character, echoing can happen
2888 the next time we pause to read a new one. */
2889 ok_to_echo_at_next_pause = 0;
2892 /* Record this character as part of the current key. */
2893 add_command_key (c);
2895 /* Re-reading in the middle of a command */
2896 last_input_char = c;
2897 num_input_events++;
2900 /* Return 1 if should recognize C as "the help character". */
2903 help_char_p (c)
2904 Lisp_Object c;
2906 Lisp_Object tail;
2908 if (EQ (c, Vhelp_char))
2909 return 1;
2910 for (tail = Vhelp_event_list; CONSP (tail); tail = XCDR (tail))
2911 if (EQ (c, XCAR (tail)))
2912 return 1;
2913 return 0;
2916 /* Record the input event C in various ways. */
2918 static void
2919 record_char (c)
2920 Lisp_Object c;
2922 /* Don't record `help-echo' in recent_keys unless it shows some help
2923 message, and a different help than the previoiusly recorded
2924 event. */
2925 if (CONSP (c) && EQ (XCAR (c), Qhelp_echo))
2927 Lisp_Object help;
2929 help = Fnth (make_number (2), c);
2930 if (STRINGP (help))
2932 int last_idx;
2933 Lisp_Object last_c, last_help;
2935 last_idx = recent_keys_index - 1;
2936 if (last_idx < 0)
2937 last_idx = NUM_RECENT_KEYS - 1;
2938 last_c = AREF (recent_keys, last_idx);
2940 if (!CONSP (last_c)
2941 || !EQ (XCAR (last_c), Qhelp_echo)
2942 || (last_help = Fnth (make_number (2), last_c),
2943 !EQ (last_help, help)))
2945 total_keys++;
2946 ASET (recent_keys, recent_keys_index, c);
2947 if (++recent_keys_index >= NUM_RECENT_KEYS)
2948 recent_keys_index = 0;
2952 else
2954 total_keys++;
2955 ASET (recent_keys, recent_keys_index, c);
2956 if (++recent_keys_index >= NUM_RECENT_KEYS)
2957 recent_keys_index = 0;
2960 /* Write c to the dribble file. If c is a lispy event, write
2961 the event's symbol to the dribble file, in <brackets>. Bleaugh.
2962 If you, dear reader, have a better idea, you've got the source. :-) */
2963 if (dribble)
2965 if (INTEGERP (c))
2967 if (XUINT (c) < 0x100)
2968 putc (XINT (c), dribble);
2969 else
2970 fprintf (dribble, " 0x%x", (int) XUINT (c));
2972 else
2974 Lisp_Object dribblee;
2976 /* If it's a structured event, take the event header. */
2977 dribblee = EVENT_HEAD (c);
2979 if (SYMBOLP (dribblee))
2981 putc ('<', dribble);
2982 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
2983 STRING_BYTES (XSYMBOL (dribblee)->name),
2984 dribble);
2985 putc ('>', dribble);
2989 fflush (dribble);
2992 if (!CONSP (c) || !EQ (Qhelp_echo, XCAR (c)))
2993 store_kbd_macro_char (c);
2995 num_nonmacro_input_events++;
2998 Lisp_Object
2999 print_help (object)
3000 Lisp_Object object;
3002 struct buffer *old = current_buffer;
3003 Fprinc (object, Qnil);
3004 set_buffer_internal (XBUFFER (Vstandard_output));
3005 call0 (intern ("help-mode"));
3006 set_buffer_internal (old);
3007 return Qnil;
3010 /* Copy out or in the info on where C-g should throw to.
3011 This is used when running Lisp code from within get_char,
3012 in case get_char is called recursively.
3013 See read_process_output. */
3015 static void
3016 save_getcjmp (temp)
3017 jmp_buf temp;
3019 bcopy (getcjmp, temp, sizeof getcjmp);
3022 static void
3023 restore_getcjmp (temp)
3024 jmp_buf temp;
3026 bcopy (temp, getcjmp, sizeof getcjmp);
3029 #ifdef HAVE_MOUSE
3031 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
3032 of this function. */
3034 static Lisp_Object
3035 tracking_off (old_value)
3036 Lisp_Object old_value;
3038 do_mouse_tracking = old_value;
3039 if (NILP (old_value))
3041 /* Redisplay may have been preempted because there was input
3042 available, and it assumes it will be called again after the
3043 input has been processed. If the only input available was
3044 the sort that we have just disabled, then we need to call
3045 redisplay. */
3046 if (!readable_events (1))
3048 redisplay_preserve_echo_area (6);
3049 get_input_pending (&input_pending, 1);
3052 return Qnil;
3055 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
3056 "Evaluate BODY with mouse movement events enabled.\n\
3057 Within a `track-mouse' form, mouse motion generates input events that\n\
3058 you can read with `read-event'.\n\
3059 Normally, mouse motion is ignored.")
3060 (args)
3061 Lisp_Object args;
3063 int count = specpdl_ptr - specpdl;
3064 Lisp_Object val;
3066 record_unwind_protect (tracking_off, do_mouse_tracking);
3068 do_mouse_tracking = Qt;
3070 val = Fprogn (args);
3071 return unbind_to (count, val);
3074 /* If mouse has moved on some frame, return one of those frames.
3075 Return 0 otherwise. */
3077 static FRAME_PTR
3078 some_mouse_moved ()
3080 Lisp_Object tail, frame;
3082 FOR_EACH_FRAME (tail, frame)
3084 if (XFRAME (frame)->mouse_moved)
3085 return XFRAME (frame);
3088 return 0;
3091 #endif /* HAVE_MOUSE */
3093 /* Low level keyboard/mouse input.
3094 kbd_buffer_store_event places events in kbd_buffer, and
3095 kbd_buffer_get_event retrieves them. */
3097 /* Return true iff there are any events in the queue that read-char
3098 would return. If this returns false, a read-char would block. */
3099 static int
3100 readable_events (do_timers_now)
3101 int do_timers_now;
3103 if (do_timers_now)
3104 timer_check (do_timers_now);
3106 if (kbd_fetch_ptr != kbd_store_ptr)
3107 return 1;
3108 #ifdef HAVE_MOUSE
3109 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3110 return 1;
3111 #endif
3112 if (single_kboard)
3114 if (current_kboard->kbd_queue_has_data)
3115 return 1;
3117 else
3119 KBOARD *kb;
3120 for (kb = all_kboards; kb; kb = kb->next_kboard)
3121 if (kb->kbd_queue_has_data)
3122 return 1;
3124 return 0;
3127 /* Set this for debugging, to have a way to get out */
3128 int stop_character;
3130 #ifdef MULTI_KBOARD
3131 static KBOARD *
3132 event_to_kboard (event)
3133 struct input_event *event;
3135 Lisp_Object frame;
3136 frame = event->frame_or_window;
3137 if (CONSP (frame))
3138 frame = XCAR (frame);
3139 else if (WINDOWP (frame))
3140 frame = WINDOW_FRAME (XWINDOW (frame));
3142 /* There are still some events that don't set this field.
3143 For now, just ignore the problem.
3144 Also ignore dead frames here. */
3145 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
3146 return 0;
3147 else
3148 return FRAME_KBOARD (XFRAME (frame));
3150 #endif
3152 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
3154 void
3155 kbd_buffer_store_event (event)
3156 register struct input_event *event;
3158 if (event->kind == no_event)
3159 abort ();
3161 if (event->kind == ascii_keystroke)
3163 register int c = event->code & 0377;
3165 if (event->modifiers & ctrl_modifier)
3166 c = make_ctrl_char (c);
3168 c |= (event->modifiers
3169 & (meta_modifier | alt_modifier
3170 | hyper_modifier | super_modifier));
3172 if (c == quit_char)
3174 extern SIGTYPE interrupt_signal ();
3175 #ifdef MULTI_KBOARD
3176 KBOARD *kb;
3177 struct input_event *sp;
3179 if (single_kboard
3180 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
3181 kb != current_kboard))
3183 kb->kbd_queue
3184 = Fcons (make_lispy_switch_frame (event->frame_or_window),
3185 Fcons (make_number (c), Qnil));
3186 kb->kbd_queue_has_data = 1;
3187 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3189 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3190 sp = kbd_buffer;
3192 if (event_to_kboard (sp) == kb)
3194 sp->kind = no_event;
3195 sp->frame_or_window = Qnil;
3196 sp->arg = Qnil;
3199 return;
3201 #endif
3203 /* If this results in a quit_char being returned to Emacs as
3204 input, set Vlast_event_frame properly. If this doesn't
3205 get returned to Emacs as an event, the next event read
3206 will set Vlast_event_frame again, so this is safe to do. */
3208 Lisp_Object focus;
3210 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
3211 if (NILP (focus))
3212 focus = event->frame_or_window;
3213 internal_last_event_frame = focus;
3214 Vlast_event_frame = focus;
3217 last_event_timestamp = event->timestamp;
3218 interrupt_signal ();
3219 return;
3222 if (c && c == stop_character)
3224 sys_suspend ();
3225 return;
3228 /* Don't insert two buffer_switch_event's in a row.
3229 Just ignore the second one. */
3230 else if (event->kind == buffer_switch_event
3231 && kbd_fetch_ptr != kbd_store_ptr
3232 && kbd_store_ptr->kind == buffer_switch_event)
3233 return;
3235 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
3236 kbd_store_ptr = kbd_buffer;
3238 /* Don't let the very last slot in the buffer become full,
3239 since that would make the two pointers equal,
3240 and that is indistinguishable from an empty buffer.
3241 Discard the event if it would fill the last slot. */
3242 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
3244 int idx;
3246 #if 0 /* The selection_request_event case looks bogus, and it's error
3247 prone to assign individual members for other events, in case
3248 the input_event structure is changed. --2000-07-13, gerd. */
3249 struct input_event *sp = kbd_store_ptr;
3250 sp->kind = event->kind;
3251 if (event->kind == selection_request_event)
3253 /* We must not use the ordinary copying code for this case,
3254 since `part' is an enum and copying it might not copy enough
3255 in this case. */
3256 bcopy (event, (char *) sp, sizeof (*event));
3258 else
3261 sp->code = event->code;
3262 sp->part = event->part;
3263 sp->frame_or_window = event->frame_or_window;
3264 sp->arg = event->arg;
3265 sp->modifiers = event->modifiers;
3266 sp->x = event->x;
3267 sp->y = event->y;
3268 sp->timestamp = event->timestamp;
3270 #else
3271 *kbd_store_ptr = *event;
3272 #endif
3274 idx = 2 * (kbd_store_ptr - kbd_buffer);
3275 ASET (kbd_buffer_gcpro, idx, event->frame_or_window);
3276 ASET (kbd_buffer_gcpro, idx + 1, event->arg);
3277 ++kbd_store_ptr;
3282 /* Generate HELP_EVENT input_events in BUFP which has room for
3283 SIZE events. If there's not enough room in BUFP, ignore this
3284 event.
3286 HELP is the help form.
3288 FRAME is the frame on which the help is generated. OBJECT is the
3289 Lisp object where the help was found (a buffer, a string, an
3290 overlay, or nil if neither from a string nor from a buffer. POS is
3291 the position within OBJECT where the help was found.
3293 Value is the number of input_events generated. */
3296 gen_help_event (bufp, size, help, frame, window, object, pos)
3297 struct input_event *bufp;
3298 int size;
3299 Lisp_Object help, frame, object, window;
3300 int pos;
3302 int nevents_stored = 0;
3304 if (size >= 2)
3306 bufp->kind = HELP_EVENT;
3307 bufp->frame_or_window = frame;
3308 bufp->arg = object;
3309 bufp->x = make_number (pos);
3310 bufp->code = 0;
3312 ++bufp;
3313 bufp->kind = HELP_EVENT;
3314 bufp->frame_or_window = WINDOWP (window) ? window : frame;
3315 bufp->arg = help;
3316 bufp->code = 1;
3317 nevents_stored = 2;
3320 return nevents_stored;
3324 /* Store HELP_EVENTs for HELP on FRAME in the input queue. */
3326 void
3327 kbd_buffer_store_help_event (frame, help)
3328 Lisp_Object frame, help;
3330 struct input_event event;
3332 event.kind = HELP_EVENT;
3333 event.frame_or_window = frame;
3334 event.arg = Qnil;
3335 event.x = make_number (0);
3336 event.code = 0;
3337 kbd_buffer_store_event (&event);
3339 event.kind = HELP_EVENT;
3340 event.frame_or_window = frame;
3341 event.arg = help;
3342 event.x = make_number (0);
3343 event.code = 1;
3344 kbd_buffer_store_event (&event);
3348 /* Discard any mouse events in the event buffer by setting them to
3349 no_event. */
3350 void
3351 discard_mouse_events ()
3353 struct input_event *sp;
3354 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3356 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3357 sp = kbd_buffer;
3359 if (sp->kind == mouse_click
3360 #ifdef WINDOWSNT
3361 || sp->kind == w32_scroll_bar_click
3362 #endif
3363 || sp->kind == scroll_bar_click)
3365 sp->kind = no_event;
3371 /* Return non-zero if there are any real events waiting in the event
3372 buffer, not counting `no_event's.
3374 If DISCARD is non-zero, discard no_event events at the front of
3375 the input queue, possibly leaving the input queue empty if there
3376 are no real input events. */
3379 kbd_buffer_events_waiting (discard)
3380 int discard;
3382 struct input_event *sp;
3384 for (sp = kbd_fetch_ptr;
3385 sp != kbd_store_ptr && sp->kind == no_event;
3386 ++sp)
3388 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3389 sp = kbd_buffer;
3392 if (discard)
3393 kbd_fetch_ptr = sp;
3395 return sp != kbd_store_ptr && sp->kind != no_event;
3399 /* Clear input event EVENT. */
3401 static INLINE void
3402 clear_event (event)
3403 struct input_event *event;
3405 int idx = 2 * (event - kbd_buffer);
3406 ASET (kbd_buffer_gcpro, idx, Qnil);
3407 ASET (kbd_buffer_gcpro, idx + 1, Qnil);
3408 event->kind = no_event;
3412 /* Read one event from the event buffer, waiting if necessary.
3413 The value is a Lisp object representing the event.
3414 The value is nil for an event that should be ignored,
3415 or that was handled here.
3416 We always read and discard one event. */
3418 static Lisp_Object
3419 kbd_buffer_get_event (kbp, used_mouse_menu)
3420 KBOARD **kbp;
3421 int *used_mouse_menu;
3423 register int c;
3424 Lisp_Object obj;
3426 if (noninteractive)
3428 c = getchar ();
3429 XSETINT (obj, c);
3430 *kbp = current_kboard;
3431 return obj;
3434 /* Wait until there is input available. */
3435 for (;;)
3437 if (kbd_fetch_ptr != kbd_store_ptr)
3438 break;
3439 #ifdef HAVE_MOUSE
3440 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3441 break;
3442 #endif
3444 /* If the quit flag is set, then read_char will return
3445 quit_char, so that counts as "available input." */
3446 if (!NILP (Vquit_flag))
3447 quit_throw_to_read_char ();
3449 /* One way or another, wait until input is available; then, if
3450 interrupt handlers have not read it, read it now. */
3452 #ifdef OLDVMS
3453 wait_for_kbd_input ();
3454 #else
3455 /* Note SIGIO has been undef'd if FIONREAD is missing. */
3456 #ifdef SIGIO
3457 gobble_input (0);
3458 #endif /* SIGIO */
3459 if (kbd_fetch_ptr != kbd_store_ptr)
3460 break;
3461 #ifdef HAVE_MOUSE
3462 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3463 break;
3464 #endif
3466 Lisp_Object minus_one;
3468 XSETINT (minus_one, -1);
3469 wait_reading_process_input (0, 0, minus_one, 1);
3471 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
3472 /* Pass 1 for EXPECT since we just waited to have input. */
3473 read_avail_input (1);
3475 #endif /* not VMS */
3478 if (CONSP (Vunread_command_events))
3480 Lisp_Object first;
3481 first = XCAR (Vunread_command_events);
3482 Vunread_command_events = XCDR (Vunread_command_events);
3483 *kbp = current_kboard;
3484 return first;
3487 /* At this point, we know that there is a readable event available
3488 somewhere. If the event queue is empty, then there must be a
3489 mouse movement enabled and available. */
3490 if (kbd_fetch_ptr != kbd_store_ptr)
3492 struct input_event *event;
3494 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3495 ? kbd_fetch_ptr
3496 : kbd_buffer);
3498 last_event_timestamp = event->timestamp;
3500 #ifdef MULTI_KBOARD
3501 *kbp = event_to_kboard (event);
3502 if (*kbp == 0)
3503 *kbp = current_kboard; /* Better than returning null ptr? */
3504 #else
3505 *kbp = &the_only_kboard;
3506 #endif
3508 obj = Qnil;
3510 /* These two kinds of events get special handling
3511 and don't actually appear to the command loop.
3512 We return nil for them. */
3513 if (event->kind == selection_request_event)
3515 #ifdef HAVE_X11
3516 struct input_event copy;
3518 /* Remove it from the buffer before processing it,
3519 since otherwise swallow_events will see it
3520 and process it again. */
3521 copy = *event;
3522 kbd_fetch_ptr = event + 1;
3523 input_pending = readable_events (0);
3524 x_handle_selection_request (&copy);
3525 #else
3526 /* We're getting selection request events, but we don't have
3527 a window system. */
3528 abort ();
3529 #endif
3532 else if (event->kind == selection_clear_event)
3534 #ifdef HAVE_X11
3535 struct input_event copy;
3537 /* Remove it from the buffer before processing it. */
3538 copy = *event;
3539 kbd_fetch_ptr = event + 1;
3540 input_pending = readable_events (0);
3541 x_handle_selection_clear (&copy);
3542 #else
3543 /* We're getting selection request events, but we don't have
3544 a window system. */
3545 abort ();
3546 #endif
3548 #if defined (HAVE_X11) || defined (HAVE_NTGUI) || defined (macintosh)
3549 else if (event->kind == delete_window_event)
3551 /* Make an event (delete-frame (FRAME)). */
3552 obj = Fcons (event->frame_or_window, Qnil);
3553 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
3554 kbd_fetch_ptr = event + 1;
3556 #endif
3557 #if defined (HAVE_X11) || defined (HAVE_NTGUI)
3558 else if (event->kind == iconify_event)
3560 /* Make an event (iconify-frame (FRAME)). */
3561 obj = Fcons (event->frame_or_window, Qnil);
3562 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
3563 kbd_fetch_ptr = event + 1;
3565 else if (event->kind == deiconify_event)
3567 /* Make an event (make-frame-visible (FRAME)). */
3568 obj = Fcons (event->frame_or_window, Qnil);
3569 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
3570 kbd_fetch_ptr = event + 1;
3572 #endif
3573 else if (event->kind == buffer_switch_event)
3575 /* The value doesn't matter here; only the type is tested. */
3576 XSETBUFFER (obj, current_buffer);
3577 kbd_fetch_ptr = event + 1;
3579 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
3580 else if (event->kind == menu_bar_activate_event)
3582 kbd_fetch_ptr = event + 1;
3583 input_pending = readable_events (0);
3584 if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
3585 x_activate_menubar (XFRAME (event->frame_or_window));
3587 #endif
3588 #ifdef WINDOWSNT
3589 else if (event->kind == language_change_event)
3591 /* Make an event (language-change (FRAME CHARSET LCID)). */
3592 obj = Fcons (event->modifiers, Qnil);
3593 obj = Fcons (event->code, Qnil);
3594 obj = Fcons (event->frame_or_window, obj);
3595 obj = Fcons (Qlanguage_change, Fcons (obj, Qnil));
3596 kbd_fetch_ptr = event + 1;
3598 #endif
3599 /* Just discard these, by returning nil.
3600 With MULTI_KBOARD, these events are used as placeholders
3601 when we need to randomly delete events from the queue.
3602 (They shouldn't otherwise be found in the buffer,
3603 but on some machines it appears they do show up
3604 even without MULTI_KBOARD.) */
3605 /* On Windows NT/9X, no_event is used to delete extraneous
3606 mouse events during a popup-menu call. */
3607 else if (event->kind == no_event)
3608 kbd_fetch_ptr = event + 1;
3609 else if (event->kind == HELP_EVENT)
3611 /* There are always two HELP_EVENTs in the input queue. */
3612 Lisp_Object object, position, help, frame, window;
3614 xassert (event->code == 0);
3615 frame = event->frame_or_window;
3616 object = event->arg;
3617 position = event->x;
3618 clear_event (event);
3620 kbd_fetch_ptr = event + 1;
3621 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3622 ? kbd_fetch_ptr
3623 : kbd_buffer);
3624 xassert (event->code == 1);
3625 help = event->arg;
3626 window = event->frame_or_window;
3627 if (!WINDOWP (window))
3628 window = Qnil;
3629 obj = Fcons (Qhelp_echo,
3630 list5 (frame, help, window, object, position));
3631 clear_event (event);
3632 kbd_fetch_ptr = event + 1;
3634 else if (event->kind == FOCUS_IN_EVENT)
3636 /* Notification of a FocusIn event. The frame receiving the
3637 focus is in event->frame_or_window. Generate a
3638 switch-frame event if necessary. */
3639 Lisp_Object frame, focus;
3641 frame = event->frame_or_window;
3642 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
3643 if (FRAMEP (focus))
3644 frame = focus;
3646 if (!EQ (frame, internal_last_event_frame)
3647 && !EQ (frame, selected_frame))
3648 obj = make_lispy_switch_frame (frame);
3649 internal_last_event_frame = frame;
3650 kbd_fetch_ptr = event + 1;
3652 else
3654 /* If this event is on a different frame, return a switch-frame this
3655 time, and leave the event in the queue for next time. */
3656 Lisp_Object frame;
3657 Lisp_Object focus;
3659 frame = event->frame_or_window;
3660 if (CONSP (frame))
3661 frame = XCAR (frame);
3662 else if (WINDOWP (frame))
3663 frame = WINDOW_FRAME (XWINDOW (frame));
3665 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
3666 if (! NILP (focus))
3667 frame = focus;
3669 if (! EQ (frame, internal_last_event_frame)
3670 && !EQ (frame, selected_frame))
3671 obj = make_lispy_switch_frame (frame);
3672 internal_last_event_frame = frame;
3674 /* If we didn't decide to make a switch-frame event, go ahead
3675 and build a real event from the queue entry. */
3677 if (NILP (obj))
3679 obj = make_lispy_event (event);
3681 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
3682 /* If this was a menu selection, then set the flag to inhibit
3683 writing to last_nonmenu_event. Don't do this if the event
3684 we're returning is (menu-bar), though; that indicates the
3685 beginning of the menu sequence, and we might as well leave
3686 that as the `event with parameters' for this selection. */
3687 if (used_mouse_menu
3688 && !EQ (event->frame_or_window, event->arg)
3689 && (event->kind == MENU_BAR_EVENT
3690 || event->kind == TOOL_BAR_EVENT))
3691 *used_mouse_menu = 1;
3692 #endif
3694 /* Wipe out this event, to catch bugs. */
3695 clear_event (event);
3696 kbd_fetch_ptr = event + 1;
3700 #ifdef HAVE_MOUSE
3701 /* Try generating a mouse motion event. */
3702 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3704 FRAME_PTR f = some_mouse_moved ();
3705 Lisp_Object bar_window;
3706 enum scroll_bar_part part;
3707 Lisp_Object x, y;
3708 unsigned long time;
3710 *kbp = current_kboard;
3711 /* Note that this uses F to determine which display to look at.
3712 If there is no valid info, it does not store anything
3713 so x remains nil. */
3714 x = Qnil;
3715 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
3717 obj = Qnil;
3719 /* Decide if we should generate a switch-frame event. Don't
3720 generate switch-frame events for motion outside of all Emacs
3721 frames. */
3722 if (!NILP (x) && f)
3724 Lisp_Object frame;
3726 frame = FRAME_FOCUS_FRAME (f);
3727 if (NILP (frame))
3728 XSETFRAME (frame, f);
3730 if (! EQ (frame, internal_last_event_frame)
3731 && !EQ (frame, selected_frame))
3732 obj = make_lispy_switch_frame (frame);
3733 internal_last_event_frame = frame;
3736 /* If we didn't decide to make a switch-frame event, go ahead and
3737 return a mouse-motion event. */
3738 if (!NILP (x) && NILP (obj))
3739 obj = make_lispy_movement (f, bar_window, part, x, y, time);
3741 #endif /* HAVE_MOUSE */
3742 else
3743 /* We were promised by the above while loop that there was
3744 something for us to read! */
3745 abort ();
3747 input_pending = readable_events (0);
3749 Vlast_event_frame = internal_last_event_frame;
3751 return (obj);
3754 /* Process any events that are not user-visible,
3755 then return, without reading any user-visible events. */
3757 void
3758 swallow_events (do_display)
3759 int do_display;
3761 int old_timers_run;
3763 while (kbd_fetch_ptr != kbd_store_ptr)
3765 struct input_event *event;
3767 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3768 ? kbd_fetch_ptr
3769 : kbd_buffer);
3771 last_event_timestamp = event->timestamp;
3773 /* These two kinds of events get special handling
3774 and don't actually appear to the command loop. */
3775 if (event->kind == selection_request_event)
3777 #ifdef HAVE_X11
3778 struct input_event copy;
3780 /* Remove it from the buffer before processing it,
3781 since otherwise swallow_events called recursively could see it
3782 and process it again. */
3783 copy = *event;
3784 kbd_fetch_ptr = event + 1;
3785 input_pending = readable_events (0);
3786 x_handle_selection_request (&copy);
3787 #else
3788 /* We're getting selection request events, but we don't have
3789 a window system. */
3790 abort ();
3791 #endif
3794 else if (event->kind == selection_clear_event)
3796 #ifdef HAVE_X11
3797 struct input_event copy;
3799 /* Remove it from the buffer before processing it, */
3800 copy = *event;
3802 kbd_fetch_ptr = event + 1;
3803 input_pending = readable_events (0);
3804 x_handle_selection_clear (&copy);
3805 #else
3806 /* We're getting selection request events, but we don't have
3807 a window system. */
3808 abort ();
3809 #endif
3811 else
3812 break;
3815 old_timers_run = timers_run;
3816 get_input_pending (&input_pending, 1);
3818 if (timers_run != old_timers_run && do_display)
3819 redisplay_preserve_echo_area (7);
3822 static EMACS_TIME timer_idleness_start_time;
3824 /* Record the start of when Emacs is idle,
3825 for the sake of running idle-time timers. */
3827 void
3828 timer_start_idle ()
3830 Lisp_Object timers;
3832 /* If we are already in the idle state, do nothing. */
3833 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3834 return;
3836 EMACS_GET_TIME (timer_idleness_start_time);
3838 /* Mark all idle-time timers as once again candidates for running. */
3839 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCDR (timers))
3841 Lisp_Object timer;
3843 timer = XCAR (timers);
3845 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3846 continue;
3847 XVECTOR (timer)->contents[0] = Qnil;
3851 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
3853 void
3854 timer_stop_idle ()
3856 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
3859 /* This is only for debugging. */
3860 struct input_event last_timer_event;
3862 /* Check whether a timer has fired. To prevent larger problems we simply
3863 disregard elements that are not proper timers. Do not make a circular
3864 timer list for the time being.
3866 Returns the number of seconds to wait until the next timer fires. If a
3867 timer is triggering now, return zero seconds.
3868 If no timer is active, return -1 seconds.
3870 If a timer is ripe, we run it, with quitting turned off.
3872 DO_IT_NOW is now ignored. It used to mean that we should
3873 run the timer directly instead of queueing a timer-event.
3874 Now we always run timers directly. */
3876 EMACS_TIME
3877 timer_check (do_it_now)
3878 int do_it_now;
3880 EMACS_TIME nexttime;
3881 EMACS_TIME now, idleness_now;
3882 Lisp_Object timers, idle_timers, chosen_timer;
3883 struct gcpro gcpro1, gcpro2, gcpro3;
3885 EMACS_SET_SECS (nexttime, -1);
3886 EMACS_SET_USECS (nexttime, -1);
3888 /* Always consider the ordinary timers. */
3889 timers = Vtimer_list;
3890 /* Consider the idle timers only if Emacs is idle. */
3891 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3892 idle_timers = Vtimer_idle_list;
3893 else
3894 idle_timers = Qnil;
3895 chosen_timer = Qnil;
3896 GCPRO3 (timers, idle_timers, chosen_timer);
3898 if (CONSP (timers) || CONSP (idle_timers))
3900 EMACS_GET_TIME (now);
3901 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3902 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
3905 while (CONSP (timers) || CONSP (idle_timers))
3907 Lisp_Object *vector;
3908 Lisp_Object timer = Qnil, idle_timer = Qnil;
3909 EMACS_TIME timer_time, idle_timer_time;
3910 EMACS_TIME difference, timer_difference, idle_timer_difference;
3912 /* Skip past invalid timers and timers already handled. */
3913 if (!NILP (timers))
3915 timer = XCAR (timers);
3916 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3918 timers = XCDR (timers);
3919 continue;
3921 vector = XVECTOR (timer)->contents;
3923 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3924 || !INTEGERP (vector[3])
3925 || ! NILP (vector[0]))
3927 timers = XCDR (timers);
3928 continue;
3931 if (!NILP (idle_timers))
3933 timer = XCAR (idle_timers);
3934 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3936 idle_timers = XCDR (idle_timers);
3937 continue;
3939 vector = XVECTOR (timer)->contents;
3941 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3942 || !INTEGERP (vector[3])
3943 || ! NILP (vector[0]))
3945 idle_timers = XCDR (idle_timers);
3946 continue;
3950 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
3951 based on the next ordinary timer.
3952 TIMER_DIFFERENCE is the distance in time from NOW to when
3953 this timer becomes ripe (negative if it's already ripe). */
3954 if (!NILP (timers))
3956 timer = XCAR (timers);
3957 vector = XVECTOR (timer)->contents;
3958 EMACS_SET_SECS (timer_time,
3959 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3960 EMACS_SET_USECS (timer_time, XINT (vector[3]));
3961 EMACS_SUB_TIME (timer_difference, timer_time, now);
3964 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
3965 based on the next idle timer. */
3966 if (!NILP (idle_timers))
3968 idle_timer = XCAR (idle_timers);
3969 vector = XVECTOR (idle_timer)->contents;
3970 EMACS_SET_SECS (idle_timer_time,
3971 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3972 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
3973 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
3976 /* Decide which timer is the next timer,
3977 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
3978 Also step down the list where we found that timer. */
3980 if (! NILP (timers) && ! NILP (idle_timers))
3982 EMACS_TIME temp;
3983 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
3984 if (EMACS_TIME_NEG_P (temp))
3986 chosen_timer = timer;
3987 timers = XCDR (timers);
3988 difference = timer_difference;
3990 else
3992 chosen_timer = idle_timer;
3993 idle_timers = XCDR (idle_timers);
3994 difference = idle_timer_difference;
3997 else if (! NILP (timers))
3999 chosen_timer = timer;
4000 timers = XCDR (timers);
4001 difference = timer_difference;
4003 else
4005 chosen_timer = idle_timer;
4006 idle_timers = XCDR (idle_timers);
4007 difference = idle_timer_difference;
4009 vector = XVECTOR (chosen_timer)->contents;
4011 /* If timer is ripe, run it if it hasn't been run. */
4012 if (EMACS_TIME_NEG_P (difference)
4013 || (EMACS_SECS (difference) == 0
4014 && EMACS_USECS (difference) == 0))
4016 if (NILP (vector[0]))
4018 int was_locked = single_kboard;
4019 int count = specpdl_ptr - specpdl;
4021 /* Mark the timer as triggered to prevent problems if the lisp
4022 code fails to reschedule it right. */
4023 vector[0] = Qt;
4025 specbind (Qinhibit_quit, Qt);
4027 call1 (Qtimer_event_handler, chosen_timer);
4028 timers_run++;
4030 unbind_to (count, Qnil);
4032 /* Resume allowing input from any kboard, if that was true before. */
4033 if (!was_locked)
4034 any_kboard_state ();
4036 /* Since we have handled the event,
4037 we don't need to tell the caller to wake up and do it. */
4040 else
4041 /* When we encounter a timer that is still waiting,
4042 return the amount of time to wait before it is ripe. */
4044 UNGCPRO;
4045 return difference;
4049 /* No timers are pending in the future. */
4050 /* Return 0 if we generated an event, and -1 if not. */
4051 UNGCPRO;
4052 return nexttime;
4055 /* Caches for modify_event_symbol. */
4056 static Lisp_Object accent_key_syms;
4057 static Lisp_Object func_key_syms;
4058 static Lisp_Object mouse_syms;
4059 #ifdef WINDOWSNT
4060 static Lisp_Object mouse_wheel_syms;
4061 #endif
4062 static Lisp_Object drag_n_drop_syms;
4064 /* This is a list of keysym codes for special "accent" characters.
4065 It parallels lispy_accent_keys. */
4067 static int lispy_accent_codes[] =
4069 #ifdef XK_dead_circumflex
4070 XK_dead_circumflex,
4071 #else
4073 #endif
4074 #ifdef XK_dead_grave
4075 XK_dead_grave,
4076 #else
4078 #endif
4079 #ifdef XK_dead_tilde
4080 XK_dead_tilde,
4081 #else
4083 #endif
4084 #ifdef XK_dead_diaeresis
4085 XK_dead_diaeresis,
4086 #else
4088 #endif
4089 #ifdef XK_dead_macron
4090 XK_dead_macron,
4091 #else
4093 #endif
4094 #ifdef XK_dead_degree
4095 XK_dead_degree,
4096 #else
4098 #endif
4099 #ifdef XK_dead_acute
4100 XK_dead_acute,
4101 #else
4103 #endif
4104 #ifdef XK_dead_cedilla
4105 XK_dead_cedilla,
4106 #else
4108 #endif
4109 #ifdef XK_dead_breve
4110 XK_dead_breve,
4111 #else
4113 #endif
4114 #ifdef XK_dead_ogonek
4115 XK_dead_ogonek,
4116 #else
4118 #endif
4119 #ifdef XK_dead_caron
4120 XK_dead_caron,
4121 #else
4123 #endif
4124 #ifdef XK_dead_doubleacute
4125 XK_dead_doubleacute,
4126 #else
4128 #endif
4129 #ifdef XK_dead_abovedot
4130 XK_dead_abovedot,
4131 #else
4133 #endif
4136 /* This is a list of Lisp names for special "accent" characters.
4137 It parallels lispy_accent_codes. */
4139 static char *lispy_accent_keys[] =
4141 "dead-circumflex",
4142 "dead-grave",
4143 "dead-tilde",
4144 "dead-diaeresis",
4145 "dead-macron",
4146 "dead-degree",
4147 "dead-acute",
4148 "dead-cedilla",
4149 "dead-breve",
4150 "dead-ogonek",
4151 "dead-caron",
4152 "dead-doubleacute",
4153 "dead-abovedot",
4156 #ifdef HAVE_NTGUI
4157 #define FUNCTION_KEY_OFFSET 0x0
4159 char *lispy_function_keys[] =
4161 0, /* 0 */
4163 0, /* VK_LBUTTON 0x01 */
4164 0, /* VK_RBUTTON 0x02 */
4165 "cancel", /* VK_CANCEL 0x03 */
4166 0, /* VK_MBUTTON 0x04 */
4168 0, 0, 0, /* 0x05 .. 0x07 */
4170 "backspace", /* VK_BACK 0x08 */
4171 "tab", /* VK_TAB 0x09 */
4173 0, 0, /* 0x0A .. 0x0B */
4175 "clear", /* VK_CLEAR 0x0C */
4176 "return", /* VK_RETURN 0x0D */
4178 0, 0, /* 0x0E .. 0x0F */
4180 0, /* VK_SHIFT 0x10 */
4181 0, /* VK_CONTROL 0x11 */
4182 0, /* VK_MENU 0x12 */
4183 "pause", /* VK_PAUSE 0x13 */
4184 "capslock", /* VK_CAPITAL 0x14 */
4186 0, 0, 0, 0, 0, 0, /* 0x15 .. 0x1A */
4188 "escape", /* VK_ESCAPE 0x1B */
4190 0, 0, 0, 0, /* 0x1C .. 0x1F */
4192 0, /* VK_SPACE 0x20 */
4193 "prior", /* VK_PRIOR 0x21 */
4194 "next", /* VK_NEXT 0x22 */
4195 "end", /* VK_END 0x23 */
4196 "home", /* VK_HOME 0x24 */
4197 "left", /* VK_LEFT 0x25 */
4198 "up", /* VK_UP 0x26 */
4199 "right", /* VK_RIGHT 0x27 */
4200 "down", /* VK_DOWN 0x28 */
4201 "select", /* VK_SELECT 0x29 */
4202 "print", /* VK_PRINT 0x2A */
4203 "execute", /* VK_EXECUTE 0x2B */
4204 "snapshot", /* VK_SNAPSHOT 0x2C */
4205 "insert", /* VK_INSERT 0x2D */
4206 "delete", /* VK_DELETE 0x2E */
4207 "help", /* VK_HELP 0x2F */
4209 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
4211 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4213 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
4215 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
4217 0, 0, 0, 0, 0, 0, 0, 0, 0,
4218 0, 0, 0, 0, 0, 0, 0, 0, 0,
4219 0, 0, 0, 0, 0, 0, 0, 0,
4221 "lwindow", /* VK_LWIN 0x5B */
4222 "rwindow", /* VK_RWIN 0x5C */
4223 "apps", /* VK_APPS 0x5D */
4225 0, 0, /* 0x5E .. 0x5F */
4227 "kp-0", /* VK_NUMPAD0 0x60 */
4228 "kp-1", /* VK_NUMPAD1 0x61 */
4229 "kp-2", /* VK_NUMPAD2 0x62 */
4230 "kp-3", /* VK_NUMPAD3 0x63 */
4231 "kp-4", /* VK_NUMPAD4 0x64 */
4232 "kp-5", /* VK_NUMPAD5 0x65 */
4233 "kp-6", /* VK_NUMPAD6 0x66 */
4234 "kp-7", /* VK_NUMPAD7 0x67 */
4235 "kp-8", /* VK_NUMPAD8 0x68 */
4236 "kp-9", /* VK_NUMPAD9 0x69 */
4237 "kp-multiply", /* VK_MULTIPLY 0x6A */
4238 "kp-add", /* VK_ADD 0x6B */
4239 "kp-separator", /* VK_SEPARATOR 0x6C */
4240 "kp-subtract", /* VK_SUBTRACT 0x6D */
4241 "kp-decimal", /* VK_DECIMAL 0x6E */
4242 "kp-divide", /* VK_DIVIDE 0x6F */
4243 "f1", /* VK_F1 0x70 */
4244 "f2", /* VK_F2 0x71 */
4245 "f3", /* VK_F3 0x72 */
4246 "f4", /* VK_F4 0x73 */
4247 "f5", /* VK_F5 0x74 */
4248 "f6", /* VK_F6 0x75 */
4249 "f7", /* VK_F7 0x76 */
4250 "f8", /* VK_F8 0x77 */
4251 "f9", /* VK_F9 0x78 */
4252 "f10", /* VK_F10 0x79 */
4253 "f11", /* VK_F11 0x7A */
4254 "f12", /* VK_F12 0x7B */
4255 "f13", /* VK_F13 0x7C */
4256 "f14", /* VK_F14 0x7D */
4257 "f15", /* VK_F15 0x7E */
4258 "f16", /* VK_F16 0x7F */
4259 "f17", /* VK_F17 0x80 */
4260 "f18", /* VK_F18 0x81 */
4261 "f19", /* VK_F19 0x82 */
4262 "f20", /* VK_F20 0x83 */
4263 "f21", /* VK_F21 0x84 */
4264 "f22", /* VK_F22 0x85 */
4265 "f23", /* VK_F23 0x86 */
4266 "f24", /* VK_F24 0x87 */
4268 0, 0, 0, 0, /* 0x88 .. 0x8B */
4269 0, 0, 0, 0, /* 0x8C .. 0x8F */
4271 "kp-numlock", /* VK_NUMLOCK 0x90 */
4272 "scroll", /* VK_SCROLL 0x91 */
4274 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
4275 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
4276 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
4277 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
4278 "kp-end", /* VK_NUMPAD_END 0x96 */
4279 "kp-home", /* VK_NUMPAD_HOME 0x97 */
4280 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
4281 "kp-up", /* VK_NUMPAD_UP 0x99 */
4282 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
4283 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
4284 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
4285 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
4287 0, 0, /* 0x9E .. 0x9F */
4290 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
4291 * Used only as parameters to GetAsyncKeyState and GetKeyState.
4292 * No other API or message will distinguish left and right keys this way.
4294 /* 0xA0 .. 0xEF */
4296 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4297 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4298 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4299 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4300 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4302 /* 0xF0 .. 0xF5 */
4304 0, 0, 0, 0, 0, 0,
4306 "attn", /* VK_ATTN 0xF6 */
4307 "crsel", /* VK_CRSEL 0xF7 */
4308 "exsel", /* VK_EXSEL 0xF8 */
4309 "ereof", /* VK_EREOF 0xF9 */
4310 "play", /* VK_PLAY 0xFA */
4311 "zoom", /* VK_ZOOM 0xFB */
4312 "noname", /* VK_NONAME 0xFC */
4313 "pa1", /* VK_PA1 0xFD */
4314 "oem_clear", /* VK_OEM_CLEAR 0xFE */
4315 0 /* 0xFF */
4318 #else /* not HAVE_NTGUI */
4320 #ifdef XK_kana_A
4321 static char *lispy_kana_keys[] =
4323 /* X Keysym value */
4324 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x400 .. 0x40f */
4325 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x410 .. 0x41f */
4326 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x420 .. 0x42f */
4327 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x430 .. 0x43f */
4328 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x440 .. 0x44f */
4329 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x450 .. 0x45f */
4330 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x460 .. 0x46f */
4331 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0,
4332 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x480 .. 0x48f */
4333 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x490 .. 0x49f */
4334 0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket",
4335 "kana-comma", "kana-conjunctive", "kana-WO", "kana-a",
4336 "kana-i", "kana-u", "kana-e", "kana-o",
4337 "kana-ya", "kana-yu", "kana-yo", "kana-tsu",
4338 "prolongedsound", "kana-A", "kana-I", "kana-U",
4339 "kana-E", "kana-O", "kana-KA", "kana-KI",
4340 "kana-KU", "kana-KE", "kana-KO", "kana-SA",
4341 "kana-SHI", "kana-SU", "kana-SE", "kana-SO",
4342 "kana-TA", "kana-CHI", "kana-TSU", "kana-TE",
4343 "kana-TO", "kana-NA", "kana-NI", "kana-NU",
4344 "kana-NE", "kana-NO", "kana-HA", "kana-HI",
4345 "kana-FU", "kana-HE", "kana-HO", "kana-MA",
4346 "kana-MI", "kana-MU", "kana-ME", "kana-MO",
4347 "kana-YA", "kana-YU", "kana-YO", "kana-RA",
4348 "kana-RI", "kana-RU", "kana-RE", "kana-RO",
4349 "kana-WA", "kana-N", "voicedsound", "semivoicedsound",
4350 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4e0 .. 0x4ef */
4351 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f0 .. 0x4ff */
4353 #endif /* XK_kana_A */
4355 #define FUNCTION_KEY_OFFSET 0xff00
4357 /* You'll notice that this table is arranged to be conveniently
4358 indexed by X Windows keysym values. */
4359 static char *lispy_function_keys[] =
4361 /* X Keysym value */
4363 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00...0f */
4364 "backspace", "tab", "linefeed", "clear",
4365 0, "return", 0, 0,
4366 0, 0, 0, "pause", /* 0xff10...1f */
4367 0, 0, 0, 0, 0, 0, 0, "escape",
4368 0, 0, 0, 0,
4369 0, "kanji", "muhenkan", "henkan", /* 0xff20...2f */
4370 "romaji", "hiragana", "katakana", "hiragana-katakana",
4371 "zenkaku", "hankaku", "zenkaku-hankaku", "touroku",
4372 "massyo", "kana-lock", "kana-shift", "eisu-shift",
4373 "eisu-toggle", /* 0xff30...3f */
4374 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4375 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
4377 "home", "left", "up", "right", /* 0xff50 */ /* IsCursorKey */
4378 "down", "prior", "next", "end",
4379 "begin", 0, 0, 0, 0, 0, 0, 0,
4380 "select", /* 0xff60 */ /* IsMiscFunctionKey */
4381 "print",
4382 "execute",
4383 "insert",
4384 0, /* 0xff64 */
4385 "undo",
4386 "redo",
4387 "menu",
4388 "find",
4389 "cancel",
4390 "help",
4391 "break", /* 0xff6b */
4393 0, 0, 0, 0,
4394 0, 0, 0, 0, "backtab", 0, 0, 0, /* 0xff70... */
4395 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff78... */
4396 "kp-space", /* 0xff80 */ /* IsKeypadKey */
4397 0, 0, 0, 0, 0, 0, 0, 0,
4398 "kp-tab", /* 0xff89 */
4399 0, 0, 0,
4400 "kp-enter", /* 0xff8d */
4401 0, 0, 0,
4402 "kp-f1", /* 0xff91 */
4403 "kp-f2",
4404 "kp-f3",
4405 "kp-f4",
4406 "kp-home", /* 0xff95 */
4407 "kp-left",
4408 "kp-up",
4409 "kp-right",
4410 "kp-down",
4411 "kp-prior", /* kp-page-up */
4412 "kp-next", /* kp-page-down */
4413 "kp-end",
4414 "kp-begin",
4415 "kp-insert",
4416 "kp-delete",
4417 0, /* 0xffa0 */
4418 0, 0, 0, 0, 0, 0, 0, 0, 0,
4419 "kp-multiply", /* 0xffaa */
4420 "kp-add",
4421 "kp-separator",
4422 "kp-subtract",
4423 "kp-decimal",
4424 "kp-divide", /* 0xffaf */
4425 "kp-0", /* 0xffb0 */
4426 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
4427 0, /* 0xffba */
4428 0, 0,
4429 "kp-equal", /* 0xffbd */
4430 "f1", /* 0xffbe */ /* IsFunctionKey */
4431 "f2",
4432 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
4433 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
4434 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
4435 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
4436 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
4437 0, 0, 0, 0, 0, 0, 0, 0,
4438 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
4439 0, 0, 0, 0, 0, 0, 0, "delete"
4442 /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE. */
4443 #define ISO_FUNCTION_KEY_OFFSET 0xfe00
4445 static char *iso_lispy_function_keys[] =
4447 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe00 */
4448 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe08 */
4449 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe10 */
4450 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe18 */
4451 "iso-lefttab", /* 0xfe20 */
4452 "iso-move-line-up", "iso-move-line-down",
4453 "iso-partial-line-up", "iso-partial-line-down",
4454 "iso-partial-space-left", "iso-partial-space-right",
4455 "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */
4456 "iso-release-margin-left", "iso-release-margin-right",
4457 "iso-release-both-margins",
4458 "iso-fast-cursor-left", "iso-fast-cursor-right",
4459 "iso-fast-cursor-up", "iso-fast-cursor-down",
4460 "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */
4461 "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */
4464 #endif /* not HAVE_NTGUI */
4466 Lisp_Object Vlispy_mouse_stem;
4468 #ifdef WINDOWSNT
4469 /* mouse-wheel events are generated by the wheel on devices such as
4470 the MS Intellimouse. The wheel sits in between the left and right
4471 mouse buttons, and is typically used to scroll or zoom the window
4472 underneath the pointer. mouse-wheel events specify the object on
4473 which they operate, and a delta corresponding to the amount and
4474 direction that the wheel is rotated. Clicking the mouse-wheel
4475 generates a mouse-2 event. */
4476 static char *lispy_mouse_wheel_names[] =
4478 "mouse-wheel"
4481 #endif /* WINDOWSNT */
4483 /* drag-n-drop events are generated when a set of selected files are
4484 dragged from another application and dropped onto an Emacs window. */
4485 static char *lispy_drag_n_drop_names[] =
4487 "drag-n-drop"
4490 /* Scroll bar parts. */
4491 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
4492 Lisp_Object Qup, Qdown, Qbottom, Qend_scroll;
4493 Lisp_Object Qtop, Qratio;
4495 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
4496 Lisp_Object *scroll_bar_parts[] = {
4497 &Qabove_handle, &Qhandle, &Qbelow_handle,
4498 &Qup, &Qdown, &Qtop, &Qbottom, &Qend_scroll, &Qratio
4501 /* User signal events. */
4502 Lisp_Object Qusr1_signal, Qusr2_signal;
4504 Lisp_Object *lispy_user_signals[] =
4506 &Qusr1_signal, &Qusr2_signal
4510 /* A vector, indexed by button number, giving the down-going location
4511 of currently depressed buttons, both scroll bar and non-scroll bar.
4513 The elements have the form
4514 (BUTTON-NUMBER MODIFIER-MASK . REST)
4515 where REST is the cdr of a position as it would be reported in the event.
4517 The make_lispy_event function stores positions here to tell the
4518 difference between click and drag events, and to store the starting
4519 location to be included in drag events. */
4521 static Lisp_Object button_down_location;
4523 /* Information about the most recent up-going button event: Which
4524 button, what location, and what time. */
4526 static int last_mouse_button;
4527 static int last_mouse_x;
4528 static int last_mouse_y;
4529 static unsigned long button_down_time;
4531 /* The maximum time between clicks to make a double-click,
4532 or Qnil to disable double-click detection,
4533 or Qt for no time limit. */
4534 Lisp_Object Vdouble_click_time;
4536 /* The number of clicks in this multiple-click. */
4538 int double_click_count;
4540 /* Given a struct input_event, build the lisp event which represents
4541 it. If EVENT is 0, build a mouse movement event from the mouse
4542 movement buffer, which should have a movement event in it.
4544 Note that events must be passed to this function in the order they
4545 are received; this function stores the location of button presses
4546 in order to build drag events when the button is released. */
4548 static Lisp_Object
4549 make_lispy_event (event)
4550 struct input_event *event;
4552 int i;
4554 switch (SWITCH_ENUM_CAST (event->kind))
4556 /* A simple keystroke. */
4557 case ascii_keystroke:
4559 Lisp_Object lispy_c;
4560 int c = event->code & 0377;
4561 /* Turn ASCII characters into control characters
4562 when proper. */
4563 if (event->modifiers & ctrl_modifier)
4564 c = make_ctrl_char (c);
4566 /* Add in the other modifier bits. We took care of ctrl_modifier
4567 just above, and the shift key was taken care of by the X code,
4568 and applied to control characters by make_ctrl_char. */
4569 c |= (event->modifiers
4570 & (meta_modifier | alt_modifier
4571 | hyper_modifier | super_modifier));
4572 /* Distinguish Shift-SPC from SPC. */
4573 if ((event->code & 0377) == 040
4574 && event->modifiers & shift_modifier)
4575 c |= shift_modifier;
4576 button_down_time = 0;
4577 XSETFASTINT (lispy_c, c);
4578 return lispy_c;
4581 case multibyte_char_keystroke:
4583 Lisp_Object lispy_c;
4585 XSETFASTINT (lispy_c, event->code);
4586 return lispy_c;
4589 /* A function key. The symbol may need to have modifier prefixes
4590 tacked onto it. */
4591 case non_ascii_keystroke:
4592 button_down_time = 0;
4594 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
4595 if (event->code == lispy_accent_codes[i])
4596 return modify_event_symbol (i,
4597 event->modifiers,
4598 Qfunction_key, Qnil,
4599 lispy_accent_keys, &accent_key_syms,
4600 (sizeof (lispy_accent_keys)
4601 / sizeof (lispy_accent_keys[0])));
4603 /* Handle system-specific keysyms. */
4604 if (event->code & (1 << 28))
4606 /* We need to use an alist rather than a vector as the cache
4607 since we can't make a vector long enuf. */
4608 if (NILP (current_kboard->system_key_syms))
4609 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
4610 return modify_event_symbol (event->code,
4611 event->modifiers,
4612 Qfunction_key,
4613 current_kboard->Vsystem_key_alist,
4614 0, &current_kboard->system_key_syms,
4615 (unsigned)-1);
4618 #ifdef XK_kana_A
4619 if (event->code >= 0x400 && event->code < 0x500)
4620 return modify_event_symbol (event->code - 0x400,
4621 event->modifiers & ~shift_modifier,
4622 Qfunction_key, Qnil,
4623 lispy_kana_keys, &func_key_syms,
4624 (sizeof (lispy_kana_keys)
4625 / sizeof (lispy_kana_keys[0])));
4626 #endif /* XK_kana_A */
4628 #ifdef ISO_FUNCTION_KEY_OFFSET
4629 if (event->code < FUNCTION_KEY_OFFSET
4630 && event->code >= ISO_FUNCTION_KEY_OFFSET)
4631 return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET,
4632 event->modifiers,
4633 Qfunction_key, Qnil,
4634 iso_lispy_function_keys, &func_key_syms,
4635 (sizeof (iso_lispy_function_keys)
4636 / sizeof (iso_lispy_function_keys[0])));
4637 else
4638 #endif
4639 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
4640 event->modifiers,
4641 Qfunction_key, Qnil,
4642 lispy_function_keys, &func_key_syms,
4643 (sizeof (lispy_function_keys)
4644 / sizeof (lispy_function_keys[0])));
4646 #ifdef HAVE_MOUSE
4647 /* A mouse click. Figure out where it is, decide whether it's
4648 a press, click or drag, and build the appropriate structure. */
4649 case mouse_click:
4650 #ifndef USE_TOOLKIT_SCROLL_BARS
4651 case scroll_bar_click:
4652 #endif
4654 int button = event->code;
4655 int is_double;
4656 Lisp_Object position;
4657 Lisp_Object *start_pos_ptr;
4658 Lisp_Object start_pos;
4660 position = Qnil;
4662 /* Build the position as appropriate for this mouse click. */
4663 if (event->kind == mouse_click)
4665 int part;
4666 FRAME_PTR f = XFRAME (event->frame_or_window);
4667 Lisp_Object window;
4668 Lisp_Object posn;
4669 Lisp_Object string_info = Qnil;
4670 int row, column;
4672 /* Ignore mouse events that were made on frame that
4673 have been deleted. */
4674 if (! FRAME_LIVE_P (f))
4675 return Qnil;
4677 /* EVENT->x and EVENT->y are frame-relative pixel
4678 coordinates at this place. Under old redisplay, COLUMN
4679 and ROW are set to frame relative glyph coordinates
4680 which are then used to determine whether this click is
4681 in a menu (non-toolkit version). */
4682 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4683 &column, &row, NULL, 1);
4685 #ifndef USE_X_TOOLKIT
4686 /* In the non-toolkit version, clicks on the menu bar
4687 are ordinary button events in the event buffer.
4688 Distinguish them, and invoke the menu.
4690 (In the toolkit version, the toolkit handles the menu bar
4691 and Emacs doesn't know about it until after the user
4692 makes a selection.) */
4693 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
4694 && (event->modifiers & down_modifier))
4696 Lisp_Object items, item;
4697 int hpos;
4698 int i;
4700 #if 0
4701 /* Activate the menu bar on the down event. If the
4702 up event comes in before the menu code can deal with it,
4703 just ignore it. */
4704 if (! (event->modifiers & down_modifier))
4705 return Qnil;
4706 #endif
4708 /* Find the menu bar item under `column'. */
4709 item = Qnil;
4710 items = FRAME_MENU_BAR_ITEMS (f);
4711 for (i = 0; i < XVECTOR (items)->size; i += 4)
4713 Lisp_Object pos, string;
4714 string = AREF (items, i + 1);
4715 pos = AREF (items, i + 3);
4716 if (NILP (string))
4717 break;
4718 if (column >= XINT (pos)
4719 && column < XINT (pos) + XSTRING (string)->size)
4721 item = AREF (items, i);
4722 break;
4726 /* ELisp manual 2.4b says (x y) are window relative but
4727 code says they are frame-relative. */
4728 position
4729 = Fcons (event->frame_or_window,
4730 Fcons (Qmenu_bar,
4731 Fcons (Fcons (event->x, event->y),
4732 Fcons (make_number (event->timestamp),
4733 Qnil))));
4735 return Fcons (item, Fcons (position, Qnil));
4737 #endif /* not USE_X_TOOLKIT */
4739 /* Set `window' to the window under frame pixel coordinates
4740 event->x/event->y. */
4741 window = window_from_coordinates (f, XINT (event->x),
4742 XINT (event->y), &part, 0);
4744 if (!WINDOWP (window))
4746 window = event->frame_or_window;
4747 posn = Qnil;
4749 else
4751 /* It's a click in window window at frame coordinates
4752 event->x/ event->y. */
4753 struct window *w = XWINDOW (window);
4755 /* Get window relative coordinates. Original code
4756 `rounded' this to glyph boundaries. */
4757 int wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (event->x));
4758 int wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (event->y));
4760 /* Set event coordinates to window-relative coordinates
4761 for constructing the Lisp event below. */
4762 XSETINT (event->x, wx);
4763 XSETINT (event->y, wy);
4765 if (part == 1 || part == 3)
4767 /* Mode line or header line. Look for a string under
4768 the mouse that may have a `local-map' property. */
4769 Lisp_Object string;
4770 int charpos;
4772 posn = part == 1 ? Qmode_line : Qheader_line;
4773 string = mode_line_string (w, wx, wy, part == 1, &charpos);
4774 if (STRINGP (string))
4775 string_info = Fcons (string, make_number (charpos));
4777 else if (part == 2)
4778 posn = Qvertical_line;
4779 else
4781 Lisp_Object object;
4782 struct display_pos p;
4783 buffer_posn_from_coords (w, &wx, &wy, &object, &p);
4784 posn = make_number (CHARPOS (p.pos));
4785 if (STRINGP (object))
4786 string_info
4787 = Fcons (object,
4788 make_number (CHARPOS (p.string_pos)));
4792 position
4793 = Fcons (window,
4794 Fcons (posn,
4795 Fcons (Fcons (event->x, event->y),
4796 Fcons (make_number (event->timestamp),
4797 (NILP (string_info)
4798 ? Qnil
4799 : Fcons (string_info, Qnil))))));
4801 #ifndef USE_TOOLKIT_SCROLL_BARS
4802 else
4804 /* It's a scrollbar click. */
4805 Lisp_Object window;
4806 Lisp_Object portion_whole;
4807 Lisp_Object part;
4809 window = event->frame_or_window;
4810 portion_whole = Fcons (event->x, event->y);
4811 part = *scroll_bar_parts[(int) event->part];
4813 position
4814 = Fcons (window,
4815 Fcons (Qvertical_scroll_bar,
4816 Fcons (portion_whole,
4817 Fcons (make_number (event->timestamp),
4818 Fcons (part, Qnil)))));
4820 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4822 if (button >= ASIZE (button_down_location))
4824 button_down_location = larger_vector (button_down_location,
4825 button + 1, Qnil);
4826 mouse_syms = larger_vector (mouse_syms, button + 1, Qnil);
4829 start_pos_ptr = &AREF (button_down_location, button);
4830 start_pos = *start_pos_ptr;
4831 *start_pos_ptr = Qnil;
4833 is_double = (button == last_mouse_button
4834 && XINT (event->x) == last_mouse_x
4835 && XINT (event->y) == last_mouse_y
4836 && button_down_time != 0
4837 && (EQ (Vdouble_click_time, Qt)
4838 || (INTEGERP (Vdouble_click_time)
4839 && ((int)(event->timestamp - button_down_time)
4840 < XINT (Vdouble_click_time)))));
4841 last_mouse_button = button;
4842 last_mouse_x = XINT (event->x);
4843 last_mouse_y = XINT (event->y);
4845 /* If this is a button press, squirrel away the location, so
4846 we can decide later whether it was a click or a drag. */
4847 if (event->modifiers & down_modifier)
4849 if (is_double)
4851 double_click_count++;
4852 event->modifiers |= ((double_click_count > 2)
4853 ? triple_modifier
4854 : double_modifier);
4856 else
4857 double_click_count = 1;
4858 button_down_time = event->timestamp;
4859 *start_pos_ptr = Fcopy_alist (position);
4862 /* Now we're releasing a button - check the co-ordinates to
4863 see if this was a click or a drag. */
4864 else if (event->modifiers & up_modifier)
4866 /* If we did not see a down before this up, ignore the up.
4867 Probably this happened because the down event chose a
4868 menu item. It would be an annoyance to treat the
4869 release of the button that chose the menu item as a
4870 separate event. */
4872 if (!CONSP (start_pos))
4873 return Qnil;
4875 event->modifiers &= ~up_modifier;
4876 #if 0 /* Formerly we treated an up with no down as a click event. */
4877 if (!CONSP (start_pos))
4878 event->modifiers |= click_modifier;
4879 else
4880 #endif
4882 /* The third element of every position should be the (x,y)
4883 pair. */
4884 Lisp_Object down;
4886 down = Fnth (make_number (2), start_pos);
4887 if (EQ (event->x, XCAR (down)) && EQ (event->y, XCDR (down)))
4888 /* Mouse hasn't moved. */
4889 event->modifiers |= click_modifier;
4890 else
4892 Lisp_Object window1, window2, posn1, posn2;
4894 /* Avoid generating a drag event if the mouse
4895 hasn't actually moved off the buffer position. */
4896 window1 = Fnth (make_number (0), position);
4897 posn1 = Fnth (make_number (1), position);
4898 window2 = Fnth (make_number (0), start_pos);
4899 posn2 = Fnth (make_number (1), start_pos);
4901 if (EQ (window1, window2) && EQ (posn1, posn2))
4902 event->modifiers |= click_modifier;
4903 else
4905 button_down_time = 0;
4906 event->modifiers |= drag_modifier;
4910 /* Don't check is_double; treat this as multiple
4911 if the down-event was multiple. */
4912 if (double_click_count > 1)
4913 event->modifiers |= ((double_click_count > 2)
4914 ? triple_modifier
4915 : double_modifier);
4918 else
4919 /* Every mouse event should either have the down_modifier or
4920 the up_modifier set. */
4921 abort ();
4924 /* Get the symbol we should use for the mouse click. */
4925 Lisp_Object head;
4927 head = modify_event_symbol (button,
4928 event->modifiers,
4929 Qmouse_click, Vlispy_mouse_stem,
4930 NULL,
4931 &mouse_syms,
4932 XVECTOR (mouse_syms)->size);
4933 if (event->modifiers & drag_modifier)
4934 return Fcons (head,
4935 Fcons (start_pos,
4936 Fcons (position,
4937 Qnil)));
4938 else if (event->modifiers & (double_modifier | triple_modifier))
4939 return Fcons (head,
4940 Fcons (position,
4941 Fcons (make_number (double_click_count),
4942 Qnil)));
4943 else
4944 return Fcons (head,
4945 Fcons (position,
4946 Qnil));
4950 #if USE_TOOLKIT_SCROLL_BARS
4952 /* We don't have down and up events if using toolkit scroll bars,
4953 so make this always a click event. Store in the `part' of
4954 the Lisp event a symbol which maps to the following actions:
4956 `above_handle' page up
4957 `below_handle' page down
4958 `up' line up
4959 `down' line down
4960 `top' top of buffer
4961 `bottom' bottom of buffer
4962 `handle' thumb has been dragged.
4963 `end-scroll' end of interaction with scroll bar
4965 The incoming input_event contains in its `part' member an
4966 index of type `enum scroll_bar_part' which we can use as an
4967 index in scroll_bar_parts to get the appropriate symbol. */
4969 case scroll_bar_click:
4971 Lisp_Object position, head, window, portion_whole, part;
4973 window = event->frame_or_window;
4974 portion_whole = Fcons (event->x, event->y);
4975 part = *scroll_bar_parts[(int) event->part];
4977 position
4978 = Fcons (window,
4979 Fcons (Qvertical_scroll_bar,
4980 Fcons (portion_whole,
4981 Fcons (make_number (event->timestamp),
4982 Fcons (part, Qnil)))));
4984 /* Always treat scroll bar events as clicks. */
4985 event->modifiers |= click_modifier;
4987 /* Get the symbol we should use for the mouse click. */
4988 head = modify_event_symbol (event->code,
4989 event->modifiers,
4990 Qmouse_click,
4991 Vlispy_mouse_stem,
4992 NULL, &mouse_syms,
4993 XVECTOR (mouse_syms)->size);
4994 return Fcons (head, Fcons (position, Qnil));
4997 #endif /* USE_TOOLKIT_SCROLL_BARS */
4999 #ifdef WINDOWSNT
5000 case w32_scroll_bar_click:
5002 int button = event->code;
5003 int is_double;
5004 Lisp_Object position;
5005 Lisp_Object *start_pos_ptr;
5006 Lisp_Object start_pos;
5009 Lisp_Object window;
5010 Lisp_Object portion_whole;
5011 Lisp_Object part;
5013 window = event->frame_or_window;
5014 portion_whole = Fcons (event->x, event->y);
5015 part = *scroll_bar_parts[(int) event->part];
5017 position
5018 = Fcons (window,
5019 Fcons (Qvertical_scroll_bar,
5020 Fcons (portion_whole,
5021 Fcons (make_number (event->timestamp),
5022 Fcons (part, Qnil)))));
5025 /* Always treat W32 scroll bar events as clicks. */
5026 event->modifiers |= click_modifier;
5029 /* Get the symbol we should use for the mouse click. */
5030 Lisp_Object head;
5032 head = modify_event_symbol (button,
5033 event->modifiers,
5034 Qmouse_click,
5035 Vlispy_mouse_stem,
5036 NULL, &mouse_syms,
5037 XVECTOR (mouse_syms)->size);
5038 return Fcons (head,
5039 Fcons (position,
5040 Qnil));
5043 case mouse_wheel:
5045 int part;
5046 FRAME_PTR f = XFRAME (event->frame_or_window);
5047 Lisp_Object window;
5048 Lisp_Object posn;
5049 Lisp_Object head, position;
5050 int row, column;
5052 /* Ignore mouse events that were made on frame that
5053 have been deleted. */
5054 if (! FRAME_LIVE_P (f))
5055 return Qnil;
5056 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
5057 &column, &row, NULL, 1);
5058 window = window_from_coordinates (f, XINT (event->x),
5059 XINT (event->y), &part, 0);
5061 if (!WINDOWP (window))
5063 window = event->frame_or_window;
5064 posn = Qnil;
5066 else
5068 int pixcolumn, pixrow;
5069 column -= XINT (XWINDOW (window)->left);
5070 row -= XINT (XWINDOW (window)->top);
5071 glyph_to_pixel_coords (XWINDOW(window), column, row,
5072 &pixcolumn, &pixrow);
5073 XSETINT (event->x, pixcolumn);
5074 XSETINT (event->y, pixrow);
5076 if (part == 1)
5077 posn = Qmode_line;
5078 else if (part == 2)
5079 posn = Qvertical_line;
5080 else if (part == 3)
5081 posn = Qheader_line;
5082 else
5084 Lisp_Object object;
5085 struct display_pos p;
5086 buffer_posn_from_coords (XWINDOW (window), &column, &row,
5087 &object, &p);
5088 posn = make_number (CHARPOS (p.pos));
5093 Lisp_Object head, position;
5095 position
5096 = Fcons (window,
5097 Fcons (posn,
5098 Fcons (Fcons (event->x, event->y),
5099 Fcons (make_number (event->timestamp),
5100 Qnil))));
5102 head = modify_event_symbol (0, event->modifiers,
5103 Qmouse_wheel, Qnil,
5104 lispy_mouse_wheel_names,
5105 &mouse_wheel_syms, 1);
5106 return Fcons (head,
5107 Fcons (position,
5108 Fcons (make_number (event->code),
5109 Qnil)));
5112 #endif /* WINDOWSNT */
5114 case drag_n_drop:
5116 int part;
5117 FRAME_PTR f;
5118 Lisp_Object window;
5119 Lisp_Object posn;
5120 Lisp_Object files;
5122 /* The frame_or_window field should be a cons of the frame in
5123 which the event occurred and a list of the filenames
5124 dropped. */
5125 if (! CONSP (event->frame_or_window))
5126 abort ();
5128 f = XFRAME (XCAR (event->frame_or_window));
5129 files = XCDR (event->frame_or_window);
5131 /* Ignore mouse events that were made on frames that
5132 have been deleted. */
5133 if (! FRAME_LIVE_P (f))
5134 return Qnil;
5136 window = window_from_coordinates (f, XINT (event->x),
5137 XINT (event->y), &part, 0);
5139 if (!WINDOWP (window))
5141 window = XCAR (event->frame_or_window);
5142 posn = Qnil;
5144 else
5146 /* It's an event in window `window' at frame coordinates
5147 event->x/ event->y. */
5148 struct window *w = XWINDOW (window);
5150 /* Get window relative coordinates. */
5151 int wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (event->x));
5152 int wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (event->y));
5154 /* Set event coordinates to window-relative coordinates
5155 for constructing the Lisp event below. */
5156 XSETINT (event->x, wx);
5157 XSETINT (event->y, wy);
5159 if (part == 1)
5160 posn = Qmode_line;
5161 else if (part == 2)
5162 posn = Qvertical_line;
5163 else if (part == 3)
5164 posn = Qheader_line;
5165 else
5167 Lisp_Object object;
5168 struct display_pos p;
5169 buffer_posn_from_coords (w, &wx, &wy, &object, &p);
5170 posn = make_number (CHARPOS (p.pos));
5175 Lisp_Object head, position;
5177 position
5178 = Fcons (window,
5179 Fcons (posn,
5180 Fcons (Fcons (event->x, event->y),
5181 Fcons (make_number (event->timestamp),
5182 Qnil))));
5184 head = modify_event_symbol (0, event->modifiers,
5185 Qdrag_n_drop, Qnil,
5186 lispy_drag_n_drop_names,
5187 &drag_n_drop_syms, 1);
5188 return Fcons (head,
5189 Fcons (position,
5190 Fcons (files,
5191 Qnil)));
5194 #endif /* HAVE_MOUSE */
5196 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
5197 case MENU_BAR_EVENT:
5198 if (EQ (event->arg, event->frame_or_window))
5199 /* This is the prefix key. We translate this to
5200 `(menu_bar)' because the code in keyboard.c for menu
5201 events, which we use, relies on this. */
5202 return Fcons (Qmenu_bar, Qnil);
5203 return event->arg;
5204 #endif
5206 case TOOL_BAR_EVENT:
5207 if (EQ (event->arg, event->frame_or_window))
5208 /* This is the prefix key. We translate this to
5209 `(tool_bar)' because the code in keyboard.c for menu
5210 events, which we use, relies on this. */
5211 return Fcons (Qtool_bar, Qnil);
5212 else if (SYMBOLP (event->arg))
5213 return apply_modifiers (event->modifiers, event->arg);
5214 return event->arg;
5216 case USER_SIGNAL_EVENT:
5217 /* A user signal. */
5218 return *lispy_user_signals[event->code];
5220 /* The 'kind' field of the event is something we don't recognize. */
5221 default:
5222 abort ();
5226 #ifdef HAVE_MOUSE
5228 static Lisp_Object
5229 make_lispy_movement (frame, bar_window, part, x, y, time)
5230 FRAME_PTR frame;
5231 Lisp_Object bar_window;
5232 enum scroll_bar_part part;
5233 Lisp_Object x, y;
5234 unsigned long time;
5236 /* Is it a scroll bar movement? */
5237 if (frame && ! NILP (bar_window))
5239 Lisp_Object part_sym;
5241 part_sym = *scroll_bar_parts[(int) part];
5242 return Fcons (Qscroll_bar_movement,
5243 (Fcons (Fcons (bar_window,
5244 Fcons (Qvertical_scroll_bar,
5245 Fcons (Fcons (x, y),
5246 Fcons (make_number (time),
5247 Fcons (part_sym,
5248 Qnil))))),
5249 Qnil)));
5252 /* Or is it an ordinary mouse movement? */
5253 else
5255 int area;
5256 Lisp_Object window;
5257 Lisp_Object posn;
5259 if (frame)
5260 /* It's in a frame; which window on that frame? */
5261 window = window_from_coordinates (frame, XINT (x), XINT (y), &area, 0);
5262 else
5263 window = Qnil;
5265 if (WINDOWP (window))
5267 struct window *w = XWINDOW (window);
5268 int wx, wy;
5270 /* Get window relative coordinates. */
5271 wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (x));
5272 wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (y));
5273 XSETINT (x, wx);
5274 XSETINT (y, wy);
5276 if (area == 1)
5277 posn = Qmode_line;
5278 else if (area == 2)
5279 posn = Qvertical_line;
5280 else if (area == 3)
5281 posn = Qheader_line;
5282 else
5284 Lisp_Object object;
5285 struct display_pos p;
5286 buffer_posn_from_coords (w, &wx, &wy, &object, &p);
5287 posn = make_number (CHARPOS (p.pos));
5290 else if (frame != 0)
5292 XSETFRAME (window, frame);
5293 posn = Qnil;
5295 else
5297 window = Qnil;
5298 posn = Qnil;
5299 XSETFASTINT (x, 0);
5300 XSETFASTINT (y, 0);
5303 return Fcons (Qmouse_movement,
5304 Fcons (Fcons (window,
5305 Fcons (posn,
5306 Fcons (Fcons (x, y),
5307 Fcons (make_number (time),
5308 Qnil)))),
5309 Qnil));
5313 #endif /* HAVE_MOUSE */
5315 /* Construct a switch frame event. */
5316 static Lisp_Object
5317 make_lispy_switch_frame (frame)
5318 Lisp_Object frame;
5320 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
5323 /* Manipulating modifiers. */
5325 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
5327 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
5328 SYMBOL's name of the end of the modifiers; the string from this
5329 position is the unmodified symbol name.
5331 This doesn't use any caches. */
5333 static int
5334 parse_modifiers_uncached (symbol, modifier_end)
5335 Lisp_Object symbol;
5336 int *modifier_end;
5338 struct Lisp_String *name;
5339 int i;
5340 int modifiers;
5342 CHECK_SYMBOL (symbol, 1);
5344 modifiers = 0;
5345 name = XSYMBOL (symbol)->name;
5347 for (i = 0; i+2 <= STRING_BYTES (name); )
5349 int this_mod_end = 0;
5350 int this_mod = 0;
5352 /* See if the name continues with a modifier word.
5353 Check that the word appears, but don't check what follows it.
5354 Set this_mod and this_mod_end to record what we find. */
5356 switch (name->data[i])
5358 #define SINGLE_LETTER_MOD(BIT) \
5359 (this_mod_end = i + 1, this_mod = BIT)
5361 case 'A':
5362 SINGLE_LETTER_MOD (alt_modifier);
5363 break;
5365 case 'C':
5366 SINGLE_LETTER_MOD (ctrl_modifier);
5367 break;
5369 case 'H':
5370 SINGLE_LETTER_MOD (hyper_modifier);
5371 break;
5373 case 'M':
5374 SINGLE_LETTER_MOD (meta_modifier);
5375 break;
5377 case 'S':
5378 SINGLE_LETTER_MOD (shift_modifier);
5379 break;
5381 case 's':
5382 SINGLE_LETTER_MOD (super_modifier);
5383 break;
5385 #undef SINGLE_LETTER_MOD
5388 /* If we found no modifier, stop looking for them. */
5389 if (this_mod_end == 0)
5390 break;
5392 /* Check there is a dash after the modifier, so that it
5393 really is a modifier. */
5394 if (this_mod_end >= STRING_BYTES (name)
5395 || name->data[this_mod_end] != '-')
5396 break;
5398 /* This modifier is real; look for another. */
5399 modifiers |= this_mod;
5400 i = this_mod_end + 1;
5403 /* Should we include the `click' modifier? */
5404 if (! (modifiers & (down_modifier | drag_modifier
5405 | double_modifier | triple_modifier))
5406 && i + 7 == STRING_BYTES (name)
5407 && strncmp (name->data + i, "mouse-", 6) == 0
5408 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
5409 modifiers |= click_modifier;
5411 if (modifier_end)
5412 *modifier_end = i;
5414 return modifiers;
5417 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
5418 prepended to the string BASE[0..BASE_LEN-1].
5419 This doesn't use any caches. */
5420 static Lisp_Object
5421 apply_modifiers_uncached (modifiers, base, base_len, base_len_byte)
5422 int modifiers;
5423 char *base;
5424 int base_len, base_len_byte;
5426 /* Since BASE could contain nulls, we can't use intern here; we have
5427 to use Fintern, which expects a genuine Lisp_String, and keeps a
5428 reference to it. */
5429 char *new_mods
5430 = (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
5431 int mod_len;
5434 char *p = new_mods;
5436 /* Only the event queue may use the `up' modifier; it should always
5437 be turned into a click or drag event before presented to lisp code. */
5438 if (modifiers & up_modifier)
5439 abort ();
5441 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
5442 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
5443 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
5444 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
5445 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
5446 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
5447 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
5448 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
5449 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
5450 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
5451 /* The click modifier is denoted by the absence of other modifiers. */
5453 *p = '\0';
5455 mod_len = p - new_mods;
5459 Lisp_Object new_name;
5461 new_name = make_uninit_multibyte_string (mod_len + base_len,
5462 mod_len + base_len_byte);
5463 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
5464 bcopy (base, XSTRING (new_name)->data + mod_len, base_len_byte);
5466 return Fintern (new_name, Qnil);
5471 static char *modifier_names[] =
5473 "up", "down", "drag", "click", "double", "triple", 0, 0,
5474 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5475 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
5477 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
5479 static Lisp_Object modifier_symbols;
5481 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
5482 static Lisp_Object
5483 lispy_modifier_list (modifiers)
5484 int modifiers;
5486 Lisp_Object modifier_list;
5487 int i;
5489 modifier_list = Qnil;
5490 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
5491 if (modifiers & (1<<i))
5492 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
5493 modifier_list);
5495 return modifier_list;
5499 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
5500 where UNMODIFIED is the unmodified form of SYMBOL,
5501 MASK is the set of modifiers present in SYMBOL's name.
5502 This is similar to parse_modifiers_uncached, but uses the cache in
5503 SYMBOL's Qevent_symbol_element_mask property, and maintains the
5504 Qevent_symbol_elements property. */
5506 Lisp_Object
5507 parse_modifiers (symbol)
5508 Lisp_Object symbol;
5510 Lisp_Object elements;
5512 elements = Fget (symbol, Qevent_symbol_element_mask);
5513 if (CONSP (elements))
5514 return elements;
5515 else
5517 int end;
5518 int modifiers = parse_modifiers_uncached (symbol, &end);
5519 Lisp_Object unmodified;
5520 Lisp_Object mask;
5522 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
5523 STRING_BYTES (XSYMBOL (symbol)->name) - end),
5524 Qnil);
5526 if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1))
5527 abort ();
5528 XSETFASTINT (mask, modifiers);
5529 elements = Fcons (unmodified, Fcons (mask, Qnil));
5531 /* Cache the parsing results on SYMBOL. */
5532 Fput (symbol, Qevent_symbol_element_mask,
5533 elements);
5534 Fput (symbol, Qevent_symbol_elements,
5535 Fcons (unmodified, lispy_modifier_list (modifiers)));
5537 /* Since we know that SYMBOL is modifiers applied to unmodified,
5538 it would be nice to put that in unmodified's cache.
5539 But we can't, since we're not sure that parse_modifiers is
5540 canonical. */
5542 return elements;
5546 /* Apply the modifiers MODIFIERS to the symbol BASE.
5547 BASE must be unmodified.
5549 This is like apply_modifiers_uncached, but uses BASE's
5550 Qmodifier_cache property, if present. It also builds
5551 Qevent_symbol_elements properties, since it has that info anyway.
5553 apply_modifiers copies the value of BASE's Qevent_kind property to
5554 the modified symbol. */
5555 static Lisp_Object
5556 apply_modifiers (modifiers, base)
5557 int modifiers;
5558 Lisp_Object base;
5560 Lisp_Object cache, index, entry, new_symbol;
5562 /* Mask out upper bits. We don't know where this value's been. */
5563 modifiers &= ((EMACS_INT)1 << VALBITS) - 1;
5565 /* The click modifier never figures into cache indices. */
5566 cache = Fget (base, Qmodifier_cache);
5567 XSETFASTINT (index, (modifiers & ~click_modifier));
5568 entry = assq_no_quit (index, cache);
5570 if (CONSP (entry))
5571 new_symbol = XCDR (entry);
5572 else
5574 /* We have to create the symbol ourselves. */
5575 new_symbol = apply_modifiers_uncached (modifiers,
5576 XSYMBOL (base)->name->data,
5577 XSYMBOL (base)->name->size,
5578 STRING_BYTES (XSYMBOL (base)->name));
5580 /* Add the new symbol to the base's cache. */
5581 entry = Fcons (index, new_symbol);
5582 Fput (base, Qmodifier_cache, Fcons (entry, cache));
5584 /* We have the parsing info now for free, so add it to the caches. */
5585 XSETFASTINT (index, modifiers);
5586 Fput (new_symbol, Qevent_symbol_element_mask,
5587 Fcons (base, Fcons (index, Qnil)));
5588 Fput (new_symbol, Qevent_symbol_elements,
5589 Fcons (base, lispy_modifier_list (modifiers)));
5592 /* Make sure this symbol is of the same kind as BASE.
5594 You'd think we could just set this once and for all when we
5595 intern the symbol above, but reorder_modifiers may call us when
5596 BASE's property isn't set right; we can't assume that just
5597 because it has a Qmodifier_cache property it must have its
5598 Qevent_kind set right as well. */
5599 if (NILP (Fget (new_symbol, Qevent_kind)))
5601 Lisp_Object kind;
5603 kind = Fget (base, Qevent_kind);
5604 if (! NILP (kind))
5605 Fput (new_symbol, Qevent_kind, kind);
5608 return new_symbol;
5612 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
5613 return a symbol with the modifiers placed in the canonical order.
5614 Canonical order is alphabetical, except for down and drag, which
5615 always come last. The 'click' modifier is never written out.
5617 Fdefine_key calls this to make sure that (for example) C-M-foo
5618 and M-C-foo end up being equivalent in the keymap. */
5620 Lisp_Object
5621 reorder_modifiers (symbol)
5622 Lisp_Object symbol;
5624 /* It's hopefully okay to write the code this way, since everything
5625 will soon be in caches, and no consing will be done at all. */
5626 Lisp_Object parsed;
5628 parsed = parse_modifiers (symbol);
5629 return apply_modifiers ((int) XINT (XCAR (XCDR (parsed))),
5630 XCAR (parsed));
5634 /* For handling events, we often want to produce a symbol whose name
5635 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
5636 to some base, like the name of a function key or mouse button.
5637 modify_event_symbol produces symbols of this sort.
5639 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
5640 is the name of the i'th symbol. TABLE_SIZE is the number of elements
5641 in the table.
5643 Alternatively, NAME_ALIST_OR_STEM is either an alist mapping codes
5644 into symbol names, or a string specifying a name stem used to
5645 construct a symbol name or the form `STEM-N', where N is the decimal
5646 representation of SYMBOL_NUM. NAME_ALIST_OR_STEM is used if it is
5647 non-nil; otherwise NAME_TABLE is used.
5649 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
5650 persist between calls to modify_event_symbol that it can use to
5651 store a cache of the symbols it's generated for this NAME_TABLE
5652 before. The object stored there may be a vector or an alist.
5654 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
5656 MODIFIERS is a set of modifier bits (as given in struct input_events)
5657 whose prefixes should be applied to the symbol name.
5659 SYMBOL_KIND is the value to be placed in the event_kind property of
5660 the returned symbol.
5662 The symbols we create are supposed to have an
5663 `event-symbol-elements' property, which lists the modifiers present
5664 in the symbol's name. */
5666 static Lisp_Object
5667 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist_or_stem,
5668 name_table, symbol_table, table_size)
5669 int symbol_num;
5670 unsigned modifiers;
5671 Lisp_Object symbol_kind;
5672 Lisp_Object name_alist_or_stem;
5673 char **name_table;
5674 Lisp_Object *symbol_table;
5675 unsigned int table_size;
5677 Lisp_Object value;
5678 Lisp_Object symbol_int;
5680 /* Get rid of the "vendor-specific" bit here. */
5681 XSETINT (symbol_int, symbol_num & 0xffffff);
5683 /* Is this a request for a valid symbol? */
5684 if (symbol_num < 0 || symbol_num >= table_size)
5685 return Qnil;
5687 if (CONSP (*symbol_table))
5688 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
5690 /* If *symbol_table doesn't seem to be initialized properly, fix that.
5691 *symbol_table should be a lisp vector TABLE_SIZE elements long,
5692 where the Nth element is the symbol for NAME_TABLE[N], or nil if
5693 we've never used that symbol before. */
5694 else
5696 if (! VECTORP (*symbol_table)
5697 || XVECTOR (*symbol_table)->size != table_size)
5699 Lisp_Object size;
5701 XSETFASTINT (size, table_size);
5702 *symbol_table = Fmake_vector (size, Qnil);
5705 value = XVECTOR (*symbol_table)->contents[symbol_num];
5708 /* Have we already used this symbol before? */
5709 if (NILP (value))
5711 /* No; let's create it. */
5712 if (CONSP (name_alist_or_stem))
5713 value = Fcdr_safe (Fassq (symbol_int, name_alist_or_stem));
5714 else if (STRINGP (name_alist_or_stem))
5716 int len = STRING_BYTES (XSTRING (name_alist_or_stem));
5717 char *buf = (char *) alloca (len + 50);
5718 sprintf (buf, "%s-%d", XSTRING (name_alist_or_stem)->data,
5719 XINT (symbol_int) + 1);
5720 value = intern (buf);
5722 else if (name_table != 0 && name_table[symbol_num])
5723 value = intern (name_table[symbol_num]);
5725 #ifdef HAVE_WINDOW_SYSTEM
5726 if (NILP (value))
5728 char *name = x_get_keysym_name (symbol_num);
5729 if (name)
5730 value = intern (name);
5732 #endif
5734 if (NILP (value))
5736 char buf[20];
5737 sprintf (buf, "key-%d", symbol_num);
5738 value = intern (buf);
5741 if (CONSP (*symbol_table))
5742 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
5743 else
5744 XVECTOR (*symbol_table)->contents[symbol_num] = value;
5746 /* Fill in the cache entries for this symbol; this also
5747 builds the Qevent_symbol_elements property, which the user
5748 cares about. */
5749 apply_modifiers (modifiers & click_modifier, value);
5750 Fput (value, Qevent_kind, symbol_kind);
5753 /* Apply modifiers to that symbol. */
5754 return apply_modifiers (modifiers, value);
5757 /* Convert a list that represents an event type,
5758 such as (ctrl meta backspace), into the usual representation of that
5759 event type as a number or a symbol. */
5761 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
5762 "Convert the event description list EVENT-DESC to an event type.\n\
5763 EVENT-DESC should contain one base event type (a character or symbol)\n\
5764 and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\
5765 drag, down, double or triple). The base must be last.\n\
5766 The return value is an event type (a character or symbol) which\n\
5767 has the same base event type and all the specified modifiers.")
5768 (event_desc)
5769 Lisp_Object event_desc;
5771 Lisp_Object base;
5772 int modifiers = 0;
5773 Lisp_Object rest;
5775 base = Qnil;
5776 rest = event_desc;
5777 while (CONSP (rest))
5779 Lisp_Object elt;
5780 int this = 0;
5782 elt = XCAR (rest);
5783 rest = XCDR (rest);
5785 /* Given a symbol, see if it is a modifier name. */
5786 if (SYMBOLP (elt) && CONSP (rest))
5787 this = parse_solitary_modifier (elt);
5789 if (this != 0)
5790 modifiers |= this;
5791 else if (!NILP (base))
5792 error ("Two bases given in one event");
5793 else
5794 base = elt;
5798 /* Let the symbol A refer to the character A. */
5799 if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1)
5800 XSETINT (base, XSYMBOL (base)->name->data[0]);
5802 if (INTEGERP (base))
5804 /* Turn (shift a) into A. */
5805 if ((modifiers & shift_modifier) != 0
5806 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
5808 XSETINT (base, XINT (base) - ('a' - 'A'));
5809 modifiers &= ~shift_modifier;
5812 /* Turn (control a) into C-a. */
5813 if (modifiers & ctrl_modifier)
5814 return make_number ((modifiers & ~ctrl_modifier)
5815 | make_ctrl_char (XINT (base)));
5816 else
5817 return make_number (modifiers | XINT (base));
5819 else if (SYMBOLP (base))
5820 return apply_modifiers (modifiers, base);
5821 else
5823 error ("Invalid base event");
5824 return Qnil;
5828 /* Try to recognize SYMBOL as a modifier name.
5829 Return the modifier flag bit, or 0 if not recognized. */
5831 static int
5832 parse_solitary_modifier (symbol)
5833 Lisp_Object symbol;
5835 struct Lisp_String *name = XSYMBOL (symbol)->name;
5837 switch (name->data[0])
5839 #define SINGLE_LETTER_MOD(BIT) \
5840 if (STRING_BYTES (name) == 1) \
5841 return BIT;
5843 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
5844 if (LEN == STRING_BYTES (name) \
5845 && ! strncmp (name->data, NAME, LEN)) \
5846 return BIT;
5848 case 'A':
5849 SINGLE_LETTER_MOD (alt_modifier);
5850 break;
5852 case 'a':
5853 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
5854 break;
5856 case 'C':
5857 SINGLE_LETTER_MOD (ctrl_modifier);
5858 break;
5860 case 'c':
5861 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
5862 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
5863 break;
5865 case 'H':
5866 SINGLE_LETTER_MOD (hyper_modifier);
5867 break;
5869 case 'h':
5870 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
5871 break;
5873 case 'M':
5874 SINGLE_LETTER_MOD (meta_modifier);
5875 break;
5877 case 'm':
5878 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
5879 break;
5881 case 'S':
5882 SINGLE_LETTER_MOD (shift_modifier);
5883 break;
5885 case 's':
5886 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
5887 MULTI_LETTER_MOD (super_modifier, "super", 5);
5888 SINGLE_LETTER_MOD (super_modifier);
5889 break;
5891 case 'd':
5892 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
5893 MULTI_LETTER_MOD (down_modifier, "down", 4);
5894 MULTI_LETTER_MOD (double_modifier, "double", 6);
5895 break;
5897 case 't':
5898 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
5899 break;
5901 #undef SINGLE_LETTER_MOD
5902 #undef MULTI_LETTER_MOD
5905 return 0;
5908 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
5909 Such a list is not valid as an event,
5910 but it can be a Lucid-style event type list. */
5913 lucid_event_type_list_p (object)
5914 Lisp_Object object;
5916 Lisp_Object tail;
5918 if (! CONSP (object))
5919 return 0;
5921 if (EQ (XCAR (object), Qhelp_echo)
5922 || EQ (XCAR (object), Qvertical_line)
5923 || EQ (XCAR (object), Qmode_line)
5924 || EQ (XCAR (object), Qheader_line))
5925 return 0;
5927 for (tail = object; CONSP (tail); tail = XCDR (tail))
5929 Lisp_Object elt;
5930 elt = XCAR (tail);
5931 if (! (INTEGERP (elt) || SYMBOLP (elt)))
5932 return 0;
5935 return NILP (tail);
5938 /* Store into *addr a value nonzero if terminal input chars are available.
5939 Serves the purpose of ioctl (0, FIONREAD, addr)
5940 but works even if FIONREAD does not exist.
5941 (In fact, this may actually read some input.)
5943 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe. */
5945 static void
5946 get_input_pending (addr, do_timers_now)
5947 int *addr;
5948 int do_timers_now;
5950 /* First of all, have we already counted some input? */
5951 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
5953 /* If input is being read as it arrives, and we have none, there is none. */
5954 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
5955 return;
5957 /* Try to read some input and see how much we get. */
5958 gobble_input (0);
5959 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
5962 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
5964 void
5965 gobble_input (expected)
5966 int expected;
5968 #ifndef VMS
5969 #ifdef SIGIO
5970 if (interrupt_input)
5972 SIGMASKTYPE mask;
5973 mask = sigblock (sigmask (SIGIO));
5974 read_avail_input (expected);
5975 sigsetmask (mask);
5977 else
5978 #ifdef POLL_FOR_INPUT
5979 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
5981 SIGMASKTYPE mask;
5982 mask = sigblock (sigmask (SIGALRM));
5983 read_avail_input (expected);
5984 sigsetmask (mask);
5986 else
5987 #endif
5988 #endif
5989 read_avail_input (expected);
5990 #endif
5993 /* Put a buffer_switch_event in the buffer
5994 so that read_key_sequence will notice the new current buffer. */
5996 void
5997 record_asynch_buffer_change ()
5999 struct input_event event;
6000 Lisp_Object tem;
6002 event.kind = buffer_switch_event;
6003 event.frame_or_window = Qnil;
6004 event.arg = Qnil;
6006 #ifdef subprocesses
6007 /* We don't need a buffer-switch event unless Emacs is waiting for input.
6008 The purpose of the event is to make read_key_sequence look up the
6009 keymaps again. If we aren't in read_key_sequence, we don't need one,
6010 and the event could cause trouble by messing up (input-pending-p). */
6011 tem = Fwaiting_for_user_input_p ();
6012 if (NILP (tem))
6013 return;
6014 #else
6015 /* We never need these events if we have no asynchronous subprocesses. */
6016 return;
6017 #endif
6019 /* Make sure no interrupt happens while storing the event. */
6020 #ifdef SIGIO
6021 if (interrupt_input)
6023 SIGMASKTYPE mask;
6024 mask = sigblock (sigmask (SIGIO));
6025 kbd_buffer_store_event (&event);
6026 sigsetmask (mask);
6028 else
6029 #endif
6031 stop_polling ();
6032 kbd_buffer_store_event (&event);
6033 start_polling ();
6037 #ifndef VMS
6039 /* Read any terminal input already buffered up by the system
6040 into the kbd_buffer, but do not wait.
6042 EXPECTED should be nonzero if the caller knows there is some input.
6044 Except on VMS, all input is read by this function.
6045 If interrupt_input is nonzero, this function MUST be called
6046 only when SIGIO is blocked.
6048 Returns the number of keyboard chars read, or -1 meaning
6049 this is a bad time to try to read input. */
6051 static int
6052 read_avail_input (expected)
6053 int expected;
6055 struct input_event buf[KBD_BUFFER_SIZE];
6056 register int i;
6057 int nread;
6059 if (read_socket_hook)
6060 /* No need for FIONREAD or fcntl; just say don't wait. */
6061 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE, expected);
6062 else
6064 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
6065 the kbd_buffer can really hold. That may prevent loss
6066 of characters on some systems when input is stuffed at us. */
6067 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
6068 int n_to_read;
6070 /* Determine how many characters we should *try* to read. */
6071 #ifdef WINDOWSNT
6072 return 0;
6073 #else /* not WINDOWSNT */
6074 #ifdef MSDOS
6075 n_to_read = dos_keysns ();
6076 if (n_to_read == 0)
6077 return 0;
6078 #else /* not MSDOS */
6079 #ifdef FIONREAD
6080 /* Find out how much input is available. */
6081 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
6082 /* Formerly simply reported no input, but that sometimes led to
6083 a failure of Emacs to terminate.
6084 SIGHUP seems appropriate if we can't reach the terminal. */
6085 /* ??? Is it really right to send the signal just to this process
6086 rather than to the whole process group?
6087 Perhaps on systems with FIONREAD Emacs is alone in its group. */
6088 kill (getpid (), SIGHUP);
6089 if (n_to_read == 0)
6090 return 0;
6091 if (n_to_read > sizeof cbuf)
6092 n_to_read = sizeof cbuf;
6093 #else /* no FIONREAD */
6094 #if defined (USG) || defined (DGUX)
6095 /* Read some input if available, but don't wait. */
6096 n_to_read = sizeof cbuf;
6097 fcntl (input_fd, F_SETFL, O_NDELAY);
6098 #else
6099 you lose;
6100 #endif
6101 #endif
6102 #endif /* not MSDOS */
6103 #endif /* not WINDOWSNT */
6105 /* Now read; for one reason or another, this will not block.
6106 NREAD is set to the number of chars read. */
6109 #ifdef MSDOS
6110 cbuf[0] = dos_keyread ();
6111 nread = 1;
6112 #else
6113 nread = emacs_read (input_fd, cbuf, n_to_read);
6114 #endif
6115 /* POSIX infers that processes which are not in the session leader's
6116 process group won't get SIGHUP's at logout time. BSDI adheres to
6117 this part standard and returns -1 from read (0) with errno==EIO
6118 when the control tty is taken away.
6119 Jeffrey Honig <jch@bsdi.com> says this is generally safe. */
6120 if (nread == -1 && errno == EIO)
6121 kill (0, SIGHUP);
6122 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
6123 /* The kernel sometimes fails to deliver SIGHUP for ptys.
6124 This looks incorrect, but it isn't, because _BSD causes
6125 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
6126 and that causes a value other than 0 when there is no input. */
6127 if (nread == 0)
6128 kill (0, SIGHUP);
6129 #endif
6131 while (
6132 /* We used to retry the read if it was interrupted.
6133 But this does the wrong thing when O_NDELAY causes
6134 an EAGAIN error. Does anybody know of a situation
6135 where a retry is actually needed? */
6136 #if 0
6137 nread < 0 && (errno == EAGAIN
6138 #ifdef EFAULT
6139 || errno == EFAULT
6140 #endif
6141 #ifdef EBADSLT
6142 || errno == EBADSLT
6143 #endif
6145 #else
6147 #endif
6150 #ifndef FIONREAD
6151 #if defined (USG) || defined (DGUX)
6152 fcntl (input_fd, F_SETFL, 0);
6153 #endif /* USG or DGUX */
6154 #endif /* no FIONREAD */
6155 for (i = 0; i < nread; i++)
6157 buf[i].kind = ascii_keystroke;
6158 buf[i].modifiers = 0;
6159 if (meta_key == 1 && (cbuf[i] & 0x80))
6160 buf[i].modifiers = meta_modifier;
6161 if (meta_key != 2)
6162 cbuf[i] &= ~0x80;
6164 buf[i].code = cbuf[i];
6165 buf[i].frame_or_window = selected_frame;
6166 buf[i].arg = Qnil;
6170 /* Scan the chars for C-g and store them in kbd_buffer. */
6171 for (i = 0; i < nread; i++)
6173 kbd_buffer_store_event (&buf[i]);
6174 /* Don't look at input that follows a C-g too closely.
6175 This reduces lossage due to autorepeat on C-g. */
6176 if (buf[i].kind == ascii_keystroke
6177 && buf[i].code == quit_char)
6178 break;
6181 return nread;
6183 #endif /* not VMS */
6185 #ifdef SIGIO /* for entire page */
6186 /* Note SIGIO has been undef'd if FIONREAD is missing. */
6188 SIGTYPE
6189 input_available_signal (signo)
6190 int signo;
6192 /* Must preserve main program's value of errno. */
6193 int old_errno = errno;
6194 #ifdef BSD4_1
6195 extern int select_alarmed;
6196 #endif
6198 #if defined (USG) && !defined (POSIX_SIGNALS)
6199 /* USG systems forget handlers when they are used;
6200 must reestablish each time */
6201 signal (signo, input_available_signal);
6202 #endif /* USG */
6204 #ifdef BSD4_1
6205 sigisheld (SIGIO);
6206 #endif
6208 if (input_available_clear_time)
6209 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
6211 while (1)
6213 int nread;
6214 nread = read_avail_input (1);
6215 /* -1 means it's not ok to read the input now.
6216 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
6217 0 means there was no keyboard input available. */
6218 if (nread <= 0)
6219 break;
6221 #ifdef BSD4_1
6222 select_alarmed = 1; /* Force the select emulator back to life */
6223 #endif
6226 #ifdef BSD4_1
6227 sigfree ();
6228 #endif
6229 errno = old_errno;
6231 #endif /* SIGIO */
6233 /* Send ourselves a SIGIO.
6235 This function exists so that the UNBLOCK_INPUT macro in
6236 blockinput.h can have some way to take care of input we put off
6237 dealing with, without assuming that every file which uses
6238 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
6239 void
6240 reinvoke_input_signal ()
6242 #ifdef SIGIO
6243 kill (getpid (), SIGIO);
6244 #endif
6249 /* Return the prompt-string of a sparse keymap.
6250 This is the first element which is a string.
6251 Return nil if there is none. */
6253 Lisp_Object
6254 map_prompt (map)
6255 Lisp_Object map;
6257 while (CONSP (map))
6259 register Lisp_Object tem;
6260 tem = Fcar (map);
6261 if (STRINGP (tem))
6262 return tem;
6263 map = Fcdr (map);
6265 return Qnil;
6268 static void menu_bar_item P_ ((Lisp_Object, Lisp_Object));
6269 static void menu_bar_one_keymap P_ ((Lisp_Object));
6271 /* These variables hold the vector under construction within
6272 menu_bar_items and its subroutines, and the current index
6273 for storing into that vector. */
6274 static Lisp_Object menu_bar_items_vector;
6275 static int menu_bar_items_index;
6277 /* Return a vector of menu items for a menu bar, appropriate
6278 to the current buffer. Each item has three elements in the vector:
6279 KEY STRING MAPLIST.
6281 OLD is an old vector we can optionally reuse, or nil. */
6283 Lisp_Object
6284 menu_bar_items (old)
6285 Lisp_Object old;
6287 /* The number of keymaps we're scanning right now, and the number of
6288 keymaps we have allocated space for. */
6289 int nmaps;
6291 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
6292 in the current keymaps, or nil where it is not a prefix. */
6293 Lisp_Object *maps;
6295 Lisp_Object def, tail;
6297 Lisp_Object result;
6299 int mapno;
6300 Lisp_Object oquit;
6302 int i;
6304 struct gcpro gcpro1;
6306 /* In order to build the menus, we need to call the keymap
6307 accessors. They all call QUIT. But this function is called
6308 during redisplay, during which a quit is fatal. So inhibit
6309 quitting while building the menus.
6310 We do this instead of specbind because (1) errors will clear it anyway
6311 and (2) this avoids risk of specpdl overflow. */
6312 oquit = Vinhibit_quit;
6313 Vinhibit_quit = Qt;
6315 if (!NILP (old))
6316 menu_bar_items_vector = old;
6317 else
6318 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
6319 menu_bar_items_index = 0;
6321 GCPRO1 (menu_bar_items_vector);
6323 /* Build our list of keymaps.
6324 If we recognize a function key and replace its escape sequence in
6325 keybuf with its symbol, or if the sequence starts with a mouse
6326 click and we need to switch buffers, we jump back here to rebuild
6327 the initial keymaps from the current buffer. */
6329 Lisp_Object *tmaps;
6331 /* Should overriding-terminal-local-map and overriding-local-map apply? */
6332 if (!NILP (Voverriding_local_map_menu_flag))
6334 /* Yes, use them (if non-nil) as well as the global map. */
6335 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
6336 nmaps = 0;
6337 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6338 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6339 if (!NILP (Voverriding_local_map))
6340 maps[nmaps++] = Voverriding_local_map;
6342 else
6344 /* No, so use major and minor mode keymaps and keymap property. */
6345 int extra_maps = 2;
6346 Lisp_Object map = get_local_map (PT, current_buffer, Qkeymap);
6347 if (!NILP (map))
6348 extra_maps = 3;
6349 nmaps = current_minor_maps (NULL, &tmaps);
6350 maps = (Lisp_Object *) alloca ((nmaps + extra_maps)
6351 * sizeof (maps[0]));
6352 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
6353 if (!NILP (map))
6354 maps[nmaps++] = map;
6355 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
6357 maps[nmaps++] = current_global_map;
6360 /* Look up in each map the dummy prefix key `menu-bar'. */
6362 result = Qnil;
6364 for (mapno = nmaps - 1; mapno >= 0; mapno--)
6365 if (!NILP (maps[mapno]))
6367 def = get_keymap (access_keymap (maps[mapno], Qmenu_bar, 1, 0, 1),
6368 0, 1);
6369 if (CONSP (def))
6370 menu_bar_one_keymap (def);
6373 /* Move to the end those items that should be at the end. */
6375 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCDR (tail))
6377 int i;
6378 int end = menu_bar_items_index;
6380 for (i = 0; i < end; i += 4)
6381 if (EQ (XCAR (tail), XVECTOR (menu_bar_items_vector)->contents[i]))
6383 Lisp_Object tem0, tem1, tem2, tem3;
6384 /* Move the item at index I to the end,
6385 shifting all the others forward. */
6386 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
6387 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
6388 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
6389 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
6390 if (end > i + 4)
6391 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
6392 &XVECTOR (menu_bar_items_vector)->contents[i],
6393 (end - i - 4) * sizeof (Lisp_Object));
6394 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
6395 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
6396 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
6397 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
6398 break;
6402 /* Add nil, nil, nil, nil at the end. */
6403 i = menu_bar_items_index;
6404 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
6406 Lisp_Object tem;
6407 tem = Fmake_vector (make_number (2 * i), Qnil);
6408 bcopy (XVECTOR (menu_bar_items_vector)->contents,
6409 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
6410 menu_bar_items_vector = tem;
6412 /* Add this item. */
6413 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6414 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6415 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6416 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6417 menu_bar_items_index = i;
6419 Vinhibit_quit = oquit;
6420 UNGCPRO;
6421 return menu_bar_items_vector;
6424 /* Scan one map KEYMAP, accumulating any menu items it defines
6425 in menu_bar_items_vector. */
6427 static Lisp_Object menu_bar_one_keymap_changed_items;
6429 static void
6430 menu_bar_one_keymap (keymap)
6431 Lisp_Object keymap;
6433 Lisp_Object tail, item;
6435 menu_bar_one_keymap_changed_items = Qnil;
6437 /* Loop over all keymap entries that have menu strings. */
6438 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
6440 item = XCAR (tail);
6441 if (CONSP (item))
6442 menu_bar_item (XCAR (item), XCDR (item));
6443 else if (VECTORP (item))
6445 /* Loop over the char values represented in the vector. */
6446 int len = XVECTOR (item)->size;
6447 int c;
6448 for (c = 0; c < len; c++)
6450 Lisp_Object character;
6451 XSETFASTINT (character, c);
6452 menu_bar_item (character, XVECTOR (item)->contents[c]);
6458 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
6459 If there's already an item for KEY, add this DEF to it. */
6461 Lisp_Object item_properties;
6463 static void
6464 menu_bar_item (key, item)
6465 Lisp_Object key, item;
6467 struct gcpro gcpro1;
6468 int i;
6469 Lisp_Object tem;
6471 if (EQ (item, Qundefined))
6473 /* If a map has an explicit `undefined' as definition,
6474 discard any previously made menu bar item. */
6476 for (i = 0; i < menu_bar_items_index; i += 4)
6477 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
6479 if (menu_bar_items_index > i + 4)
6480 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
6481 &XVECTOR (menu_bar_items_vector)->contents[i],
6482 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
6483 menu_bar_items_index -= 4;
6487 /* If this keymap has already contributed to this KEY,
6488 don't contribute to it a second time. */
6489 tem = Fmemq (key, menu_bar_one_keymap_changed_items);
6490 if (!NILP (tem) || NILP (item))
6491 return;
6493 menu_bar_one_keymap_changed_items
6494 = Fcons (key, menu_bar_one_keymap_changed_items);
6496 /* We add to menu_bar_one_keymap_changed_items before doing the
6497 parse_menu_item, so that if it turns out it wasn't a menu item,
6498 it still correctly hides any further menu item. */
6499 GCPRO1 (key);
6500 i = parse_menu_item (item, 0, 1);
6501 UNGCPRO;
6502 if (!i)
6503 return;
6505 item = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6507 /* Find any existing item for this KEY. */
6508 for (i = 0; i < menu_bar_items_index; i += 4)
6509 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
6510 break;
6512 /* If we did not find this KEY, add it at the end. */
6513 if (i == menu_bar_items_index)
6515 /* If vector is too small, get a bigger one. */
6516 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
6518 Lisp_Object tem;
6519 tem = Fmake_vector (make_number (2 * i), Qnil);
6520 bcopy (XVECTOR (menu_bar_items_vector)->contents,
6521 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
6522 menu_bar_items_vector = tem;
6525 /* Add this item. */
6526 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
6527 XVECTOR (menu_bar_items_vector)->contents[i++]
6528 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
6529 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (item, Qnil);
6530 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
6531 menu_bar_items_index = i;
6533 /* We did find an item for this KEY. Add ITEM to its list of maps. */
6534 else
6536 Lisp_Object old;
6537 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
6538 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (item, old);
6542 /* This is used as the handler when calling menu_item_eval_property. */
6543 static Lisp_Object
6544 menu_item_eval_property_1 (arg)
6545 Lisp_Object arg;
6547 /* If we got a quit from within the menu computation,
6548 quit all the way out of it. This takes care of C-] in the debugger. */
6549 if (CONSP (arg) && EQ (XCAR (arg), Qquit))
6550 Fsignal (Qquit, Qnil);
6552 return Qnil;
6555 /* Evaluate an expression and return the result (or nil if something
6556 went wrong). Used to evaluate dynamic parts of menu items. */
6557 Lisp_Object
6558 menu_item_eval_property (sexpr)
6559 Lisp_Object sexpr;
6561 int count = specpdl_ptr - specpdl;
6562 Lisp_Object val;
6563 specbind (Qinhibit_redisplay, Qt);
6564 val = internal_condition_case_1 (Feval, sexpr, Qerror,
6565 menu_item_eval_property_1);
6566 return unbind_to (count, val);
6569 /* This function parses a menu item and leaves the result in the
6570 vector item_properties.
6571 ITEM is a key binding, a possible menu item.
6572 If NOTREAL is nonzero, only check for equivalent key bindings, don't
6573 evaluate dynamic expressions in the menu item.
6574 INMENUBAR is > 0 when this is considered for an entry in a menu bar
6575 top level.
6576 INMENUBAR is < 0 when this is considered for an entry in a keyboard menu.
6577 parse_menu_item returns true if the item is a menu item and false
6578 otherwise. */
6581 parse_menu_item (item, notreal, inmenubar)
6582 Lisp_Object item;
6583 int notreal, inmenubar;
6585 Lisp_Object def, tem, item_string, start;
6586 Lisp_Object cachelist;
6587 Lisp_Object filter;
6588 Lisp_Object keyhint;
6589 int i;
6590 int newcache = 0;
6592 cachelist = Qnil;
6593 filter = Qnil;
6594 keyhint = Qnil;
6596 if (!CONSP (item))
6597 return 0;
6599 /* Create item_properties vector if necessary. */
6600 if (NILP (item_properties))
6601 item_properties
6602 = Fmake_vector (make_number (ITEM_PROPERTY_ENABLE + 1), Qnil);
6604 /* Initialize optional entries. */
6605 for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++)
6606 AREF (item_properties, i) = Qnil;
6607 AREF (item_properties, ITEM_PROPERTY_ENABLE) = Qt;
6609 /* Save the item here to protect it from GC. */
6610 AREF (item_properties, ITEM_PROPERTY_ITEM) = item;
6612 item_string = XCAR (item);
6614 start = item;
6615 item = XCDR (item);
6616 if (STRINGP (item_string))
6618 /* Old format menu item. */
6619 AREF (item_properties, ITEM_PROPERTY_NAME) = item_string;
6621 /* Maybe help string. */
6622 if (CONSP (item) && STRINGP (XCAR (item)))
6624 AREF (item_properties, ITEM_PROPERTY_HELP) = XCAR (item);
6625 start = item;
6626 item = XCDR (item);
6629 /* Maybe key binding cache. */
6630 if (CONSP (item) && CONSP (XCAR (item))
6631 && (NILP (XCAR (XCAR (item)))
6632 || VECTORP (XCAR (XCAR (item)))))
6634 cachelist = XCAR (item);
6635 item = XCDR (item);
6638 /* This is the real definition--the function to run. */
6639 AREF (item_properties, ITEM_PROPERTY_DEF) = item;
6641 /* Get enable property, if any. */
6642 if (SYMBOLP (item))
6644 tem = Fget (item, Qmenu_enable);
6645 if (!NILP (tem))
6646 AREF (item_properties, ITEM_PROPERTY_ENABLE) = tem;
6649 else if (EQ (item_string, Qmenu_item) && CONSP (item))
6651 /* New format menu item. */
6652 AREF (item_properties, ITEM_PROPERTY_NAME) = XCAR (item);
6653 start = XCDR (item);
6654 if (CONSP (start))
6656 /* We have a real binding. */
6657 AREF (item_properties, ITEM_PROPERTY_DEF) = XCAR (start);
6659 item = XCDR (start);
6660 /* Is there a cache list with key equivalences. */
6661 if (CONSP (item) && CONSP (XCAR (item)))
6663 cachelist = XCAR (item);
6664 item = XCDR (item);
6667 /* Parse properties. */
6668 while (CONSP (item) && CONSP (XCDR (item)))
6670 tem = XCAR (item);
6671 item = XCDR (item);
6673 if (EQ (tem, QCenable))
6674 AREF (item_properties, ITEM_PROPERTY_ENABLE) = XCAR (item);
6675 else if (EQ (tem, QCvisible) && !notreal)
6677 /* If got a visible property and that evaluates to nil
6678 then ignore this item. */
6679 tem = menu_item_eval_property (XCAR (item));
6680 if (NILP (tem))
6681 return 0;
6683 else if (EQ (tem, QChelp))
6684 AREF (item_properties, ITEM_PROPERTY_HELP) = XCAR (item);
6685 else if (EQ (tem, QCfilter))
6686 filter = item;
6687 else if (EQ (tem, QCkey_sequence))
6689 tem = XCAR (item);
6690 if (NILP (cachelist)
6691 && (SYMBOLP (tem) || STRINGP (tem) || VECTORP (tem)))
6692 /* Be GC protected. Set keyhint to item instead of tem. */
6693 keyhint = item;
6695 else if (EQ (tem, QCkeys))
6697 tem = XCAR (item);
6698 if (CONSP (tem) || (STRINGP (tem) && NILP (cachelist)))
6699 AREF (item_properties, ITEM_PROPERTY_KEYEQ) = tem;
6701 else if (EQ (tem, QCbutton) && CONSP (XCAR (item)))
6703 Lisp_Object type;
6704 tem = XCAR (item);
6705 type = XCAR (tem);
6706 if (EQ (type, QCtoggle) || EQ (type, QCradio))
6708 AREF (item_properties, ITEM_PROPERTY_SELECTED)
6709 = XCDR (tem);
6710 AREF (item_properties, ITEM_PROPERTY_TYPE)
6711 = type;
6714 item = XCDR (item);
6717 else if (inmenubar || !NILP (start))
6718 return 0;
6720 else
6721 return 0; /* not a menu item */
6723 /* If item string is not a string, evaluate it to get string.
6724 If we don't get a string, skip this item. */
6725 item_string = AREF (item_properties, ITEM_PROPERTY_NAME);
6726 if (!(STRINGP (item_string) || notreal))
6728 item_string = menu_item_eval_property (item_string);
6729 if (!STRINGP (item_string))
6730 return 0;
6731 AREF (item_properties, ITEM_PROPERTY_NAME) = item_string;
6734 /* If got a filter apply it on definition. */
6735 def = AREF (item_properties, ITEM_PROPERTY_DEF);
6736 if (!NILP (filter))
6738 def = menu_item_eval_property (list2 (XCAR (filter),
6739 list2 (Qquote, def)));
6741 AREF (item_properties, ITEM_PROPERTY_DEF) = def;
6744 /* If we got no definition, this item is just unselectable text which
6745 is OK in a submenu but not in the menubar. */
6746 if (NILP (def))
6747 return (inmenubar ? 0 : 1);
6749 /* Enable or disable selection of item. */
6750 tem = AREF (item_properties, ITEM_PROPERTY_ENABLE);
6751 if (!EQ (tem, Qt))
6753 if (notreal)
6754 tem = Qt;
6755 else
6756 tem = menu_item_eval_property (tem);
6757 if (inmenubar && NILP (tem))
6758 return 0; /* Ignore disabled items in menu bar. */
6759 AREF (item_properties, ITEM_PROPERTY_ENABLE) = tem;
6762 /* See if this is a separate pane or a submenu. */
6763 def = AREF (item_properties, ITEM_PROPERTY_DEF);
6764 tem = get_keymap (def, 0, 1);
6765 /* For a subkeymap, just record its details and exit. */
6766 if (CONSP (tem))
6768 AREF (item_properties, ITEM_PROPERTY_MAP) = tem;
6769 AREF (item_properties, ITEM_PROPERTY_DEF) = tem;
6770 return 1;
6773 /* At the top level in the menu bar, do likewise for commands also.
6774 The menu bar does not display equivalent key bindings anyway.
6775 ITEM_PROPERTY_DEF is already set up properly. */
6776 if (inmenubar > 0)
6777 return 1;
6779 /* This is a command. See if there is an equivalent key binding. */
6780 if (NILP (cachelist))
6782 /* We have to create a cachelist. */
6783 CHECK_IMPURE (start);
6784 XCDR (start) = Fcons (Fcons (Qnil, Qnil), XCDR (start));
6785 cachelist = XCAR (XCDR (start));
6786 newcache = 1;
6787 tem = AREF (item_properties, ITEM_PROPERTY_KEYEQ);
6788 if (!NILP (keyhint))
6790 XCAR (cachelist) = XCAR (keyhint);
6791 newcache = 0;
6793 else if (STRINGP (tem))
6795 XCDR (cachelist) = Fsubstitute_command_keys (tem);
6796 XCAR (cachelist) = Qt;
6800 tem = XCAR (cachelist);
6801 if (!EQ (tem, Qt))
6803 int chkcache = 0;
6804 Lisp_Object prefix;
6806 if (!NILP (tem))
6807 tem = Fkey_binding (tem, Qnil);
6809 prefix = AREF (item_properties, ITEM_PROPERTY_KEYEQ);
6810 if (CONSP (prefix))
6812 def = XCAR (prefix);
6813 prefix = XCDR (prefix);
6815 else
6816 def = AREF (item_properties, ITEM_PROPERTY_DEF);
6818 if (!update_menu_bindings)
6819 chkcache = 0;
6820 else if (NILP (XCAR (cachelist))) /* Have no saved key. */
6822 if (newcache /* Always check first time. */
6823 /* Should we check everything when precomputing key
6824 bindings? */
6825 /* If something had no key binding before, don't recheck it
6826 because that is too slow--except if we have a list of
6827 rebound commands in Vdefine_key_rebound_commands, do
6828 recheck any command that appears in that list. */
6829 || (CONSP (Vdefine_key_rebound_commands)
6830 && !NILP (Fmemq (def, Vdefine_key_rebound_commands))))
6831 chkcache = 1;
6833 /* We had a saved key. Is it still bound to the command? */
6834 else if (NILP (tem)
6835 || (!EQ (tem, def)
6836 /* If the command is an alias for another
6837 (such as lmenu.el set it up), check if the
6838 original command matches the cached command. */
6839 && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function))))
6840 chkcache = 1; /* Need to recompute key binding. */
6842 if (chkcache)
6844 /* Recompute equivalent key binding. If the command is an alias
6845 for another (such as lmenu.el set it up), see if the original
6846 command name has equivalent keys. Otherwise look up the
6847 specified command itself. We don't try both, because that
6848 makes lmenu menus slow. */
6849 if (SYMBOLP (def)
6850 && SYMBOLP (XSYMBOL (def)->function)
6851 && ! NILP (Fget (def, Qmenu_alias)))
6852 def = XSYMBOL (def)->function;
6853 tem = Fwhere_is_internal (def, Qnil, Qt, Qnil);
6854 XCAR (cachelist) = tem;
6855 if (NILP (tem))
6857 XCDR (cachelist) = Qnil;
6858 chkcache = 0;
6861 else if (!NILP (keyhint) && !NILP (XCAR (cachelist)))
6863 tem = XCAR (cachelist);
6864 chkcache = 1;
6867 newcache = chkcache;
6868 if (chkcache)
6870 tem = Fkey_description (tem);
6871 if (CONSP (prefix))
6873 if (STRINGP (XCAR (prefix)))
6874 tem = concat2 (XCAR (prefix), tem);
6875 if (STRINGP (XCDR (prefix)))
6876 tem = concat2 (tem, XCDR (prefix));
6878 XCDR (cachelist) = tem;
6882 tem = XCDR (cachelist);
6883 if (newcache && !NILP (tem))
6885 tem = concat3 (build_string (" ("), tem, build_string (")"));
6886 XCDR (cachelist) = tem;
6889 /* If we only want to precompute equivalent key bindings, stop here. */
6890 if (notreal)
6891 return 1;
6893 /* If we have an equivalent key binding, use that. */
6894 AREF (item_properties, ITEM_PROPERTY_KEYEQ) = tem;
6896 /* Include this when menu help is implemented.
6897 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP];
6898 if (!(NILP (tem) || STRINGP (tem)))
6900 tem = menu_item_eval_property (tem);
6901 if (!STRINGP (tem))
6902 tem = Qnil;
6903 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP] = tem;
6907 /* Handle radio buttons or toggle boxes. */
6908 tem = AREF (item_properties, ITEM_PROPERTY_SELECTED);
6909 if (!NILP (tem))
6910 AREF (item_properties, ITEM_PROPERTY_SELECTED)
6911 = menu_item_eval_property (tem);
6913 return 1;
6918 /***********************************************************************
6919 Tool-bars
6920 ***********************************************************************/
6922 /* A vector holding tool bar items while they are parsed in function
6923 tool_bar_items runs Each item occupies TOOL_BAR_ITEM_NSCLOTS elements
6924 in the vector. */
6926 static Lisp_Object tool_bar_items_vector;
6928 /* A vector holding the result of parse_tool_bar_item. Layout is like
6929 the one for a single item in tool_bar_items_vector. */
6931 static Lisp_Object tool_bar_item_properties;
6933 /* Next free index in tool_bar_items_vector. */
6935 static int ntool_bar_items;
6937 /* The symbols `tool-bar', and `:image'. */
6939 extern Lisp_Object Qtool_bar;
6940 Lisp_Object QCimage;
6942 /* Function prototypes. */
6944 static void init_tool_bar_items P_ ((Lisp_Object));
6945 static void process_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
6946 static int parse_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
6947 static void append_tool_bar_item P_ ((void));
6950 /* Return a vector of tool bar items for keymaps currently in effect.
6951 Reuse vector REUSE if non-nil. Return in *NITEMS the number of
6952 tool bar items found. */
6954 Lisp_Object
6955 tool_bar_items (reuse, nitems)
6956 Lisp_Object reuse;
6957 int *nitems;
6959 Lisp_Object *maps;
6960 int nmaps, i;
6961 Lisp_Object oquit;
6962 Lisp_Object *tmaps;
6963 extern Lisp_Object Voverriding_local_map_menu_flag;
6964 extern Lisp_Object Voverriding_local_map;
6966 *nitems = 0;
6968 /* In order to build the menus, we need to call the keymap
6969 accessors. They all call QUIT. But this function is called
6970 during redisplay, during which a quit is fatal. So inhibit
6971 quitting while building the menus. We do this instead of
6972 specbind because (1) errors will clear it anyway and (2) this
6973 avoids risk of specpdl overflow. */
6974 oquit = Vinhibit_quit;
6975 Vinhibit_quit = Qt;
6977 /* Initialize tool_bar_items_vector and protect it from GC. */
6978 init_tool_bar_items (reuse);
6980 /* Build list of keymaps in maps. Set nmaps to the number of maps
6981 to process. */
6983 /* Should overriding-terminal-local-map and overriding-local-map apply? */
6984 if (!NILP (Voverriding_local_map_menu_flag))
6986 /* Yes, use them (if non-nil) as well as the global map. */
6987 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
6988 nmaps = 0;
6989 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6990 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6991 if (!NILP (Voverriding_local_map))
6992 maps[nmaps++] = Voverriding_local_map;
6994 else
6996 /* No, so use major and minor mode keymaps and keymap property. */
6997 int extra_maps = 2;
6998 Lisp_Object map = get_local_map (PT, current_buffer, Qkeymap);
6999 if (!NILP (map))
7000 extra_maps = 3;
7001 nmaps = current_minor_maps (NULL, &tmaps);
7002 maps = (Lisp_Object *) alloca ((nmaps + extra_maps)
7003 * sizeof (maps[0]));
7004 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
7005 if (!NILP (map))
7006 maps[nmaps++] = map;
7007 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
7010 /* Add global keymap at the end. */
7011 maps[nmaps++] = current_global_map;
7013 /* Process maps in reverse order and look up in each map the prefix
7014 key `tool-bar'. */
7015 for (i = nmaps - 1; i >= 0; --i)
7016 if (!NILP (maps[i]))
7018 Lisp_Object keymap;
7020 keymap = get_keymap (access_keymap (maps[i], Qtool_bar, 1, 0, 1), 0, 1);
7021 if (CONSP (keymap))
7023 Lisp_Object tail;
7025 /* KEYMAP is a list `(keymap (KEY . BINDING) ...)'. */
7026 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
7028 Lisp_Object keydef = XCAR (tail);
7029 if (CONSP (keydef))
7030 process_tool_bar_item (XCAR (keydef), XCDR (keydef));
7035 Vinhibit_quit = oquit;
7036 *nitems = ntool_bar_items / TOOL_BAR_ITEM_NSLOTS;
7037 return tool_bar_items_vector;
7041 /* Process the definition of KEY which is DEF. */
7043 static void
7044 process_tool_bar_item (key, def)
7045 Lisp_Object key, def;
7047 int i;
7048 extern Lisp_Object Qundefined;
7049 struct gcpro gcpro1, gcpro2;
7051 /* Protect KEY and DEF from GC because parse_tool_bar_item may call
7052 eval. */
7053 GCPRO2 (key, def);
7055 if (EQ (def, Qundefined))
7057 /* If a map has an explicit `undefined' as definition,
7058 discard any previously made item. */
7059 for (i = 0; i < ntool_bar_items; i += TOOL_BAR_ITEM_NSLOTS)
7061 Lisp_Object *v = XVECTOR (tool_bar_items_vector)->contents + i;
7063 if (EQ (key, v[TOOL_BAR_ITEM_KEY]))
7065 if (ntool_bar_items > i + TOOL_BAR_ITEM_NSLOTS)
7066 bcopy (v + TOOL_BAR_ITEM_NSLOTS, v,
7067 ((ntool_bar_items - i - TOOL_BAR_ITEM_NSLOTS)
7068 * sizeof (Lisp_Object)));
7069 ntool_bar_items -= TOOL_BAR_ITEM_NSLOTS;
7070 break;
7074 else if (parse_tool_bar_item (key, def))
7075 /* Append a new tool bar item to tool_bar_items_vector. Accept
7076 more than one definition for the same key. */
7077 append_tool_bar_item ();
7079 UNGCPRO;
7083 /* Parse a tool bar item specification ITEM for key KEY and return the
7084 result in tool_bar_item_properties. Value is zero if ITEM is
7085 invalid.
7087 ITEM is a list `(menu-item CAPTION BINDING PROPS...)'.
7089 CAPTION is the caption of the item, If it's not a string, it is
7090 evaluated to get a string.
7092 BINDING is the tool bar item's binding. Tool-bar items with keymaps
7093 as binding are currently ignored.
7095 The following properties are recognized:
7097 - `:enable FORM'.
7099 FORM is evaluated and specifies whether the tool bar item is
7100 enabled or disabled.
7102 - `:visible FORM'
7104 FORM is evaluated and specifies whether the tool bar item is visible.
7106 - `:filter FUNCTION'
7108 FUNCTION is invoked with one parameter `(quote BINDING)'. Its
7109 result is stored as the new binding.
7111 - `:button (TYPE SELECTED)'
7113 TYPE must be one of `:radio' or `:toggle'. SELECTED is evaluated
7114 and specifies whether the button is selected (pressed) or not.
7116 - `:image IMAGES'
7118 IMAGES is either a single image specification or a vector of four
7119 image specifications. See enum tool_bar_item_images.
7121 - `:help HELP-STRING'.
7123 Gives a help string to display for the tool bar item. */
7125 static int
7126 parse_tool_bar_item (key, item)
7127 Lisp_Object key, item;
7129 /* Access slot with index IDX of vector tool_bar_item_properties. */
7130 #define PROP(IDX) XVECTOR (tool_bar_item_properties)->contents[IDX]
7132 Lisp_Object filter = Qnil;
7133 Lisp_Object caption;
7134 extern Lisp_Object QCenable, QCvisible, QChelp, QCfilter;
7135 extern Lisp_Object QCbutton, QCtoggle, QCradio;
7136 int i;
7138 /* Defininition looks like `(menu-item CAPTION BINDING PROPS...)'.
7139 Rule out items that aren't lists, don't start with
7140 `menu-item' or whose rest following `tool-bar-item' is not a
7141 list. */
7142 if (!CONSP (item)
7143 || !EQ (XCAR (item), Qmenu_item)
7144 || (item = XCDR (item),
7145 !CONSP (item)))
7146 return 0;
7148 /* Create tool_bar_item_properties vector if necessary. Reset it to
7149 defaults. */
7150 if (VECTORP (tool_bar_item_properties))
7152 for (i = 0; i < TOOL_BAR_ITEM_NSLOTS; ++i)
7153 PROP (i) = Qnil;
7155 else
7156 tool_bar_item_properties
7157 = Fmake_vector (make_number (TOOL_BAR_ITEM_NSLOTS), Qnil);
7159 /* Set defaults. */
7160 PROP (TOOL_BAR_ITEM_KEY) = key;
7161 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
7163 /* Get the caption of the item. If the caption is not a string,
7164 evaluate it to get a string. If we don't get a string, skip this
7165 item. */
7166 caption = XCAR (item);
7167 if (!STRINGP (caption))
7169 caption = menu_item_eval_property (caption);
7170 if (!STRINGP (caption))
7171 return 0;
7173 PROP (TOOL_BAR_ITEM_CAPTION) = caption;
7175 /* Give up if rest following the caption is not a list. */
7176 item = XCDR (item);
7177 if (!CONSP (item))
7178 return 0;
7180 /* Store the binding. */
7181 PROP (TOOL_BAR_ITEM_BINDING) = XCAR (item);
7182 item = XCDR (item);
7184 /* Ignore cached key binding, if any. */
7185 if (CONSP (item) && CONSP (XCAR (item)))
7186 item = XCDR (item);
7188 /* Process the rest of the properties. */
7189 for (; CONSP (item) && CONSP (XCDR (item)); item = XCDR (XCDR (item)))
7191 Lisp_Object key, value;
7193 key = XCAR (item);
7194 value = XCAR (XCDR (item));
7196 if (EQ (key, QCenable))
7197 /* `:enable FORM'. */
7198 PROP (TOOL_BAR_ITEM_ENABLED_P) = value;
7199 else if (EQ (key, QCvisible))
7201 /* `:visible FORM'. If got a visible property and that
7202 evaluates to nil then ignore this item. */
7203 if (NILP (menu_item_eval_property (value)))
7204 return 0;
7206 else if (EQ (key, QChelp))
7207 /* `:help HELP-STRING'. */
7208 PROP (TOOL_BAR_ITEM_HELP) = value;
7209 else if (EQ (key, QCfilter))
7210 /* ':filter FORM'. */
7211 filter = value;
7212 else if (EQ (key, QCbutton) && CONSP (value))
7214 /* `:button (TYPE . SELECTED)'. */
7215 Lisp_Object type, selected;
7217 type = XCAR (value);
7218 selected = XCDR (value);
7219 if (EQ (type, QCtoggle) || EQ (type, QCradio))
7221 PROP (TOOL_BAR_ITEM_SELECTED_P) = selected;
7222 PROP (TOOL_BAR_ITEM_TYPE) = type;
7225 else if (EQ (key, QCimage)
7226 && (CONSP (value)
7227 || (VECTORP (value) && XVECTOR (value)->size == 4)))
7228 /* Value is either a single image specification or a vector
7229 of 4 such specifications for the different buttion states. */
7230 PROP (TOOL_BAR_ITEM_IMAGES) = value;
7233 /* If got a filter apply it on binding. */
7234 if (!NILP (filter))
7235 PROP (TOOL_BAR_ITEM_BINDING)
7236 = menu_item_eval_property (list2 (filter,
7237 list2 (Qquote,
7238 PROP (TOOL_BAR_ITEM_BINDING))));
7240 /* See if the binding is a keymap. Give up if it is. */
7241 if (CONSP (get_keymap (PROP (TOOL_BAR_ITEM_BINDING), 0, 1)))
7242 return 0;
7244 /* Enable or disable selection of item. */
7245 if (!EQ (PROP (TOOL_BAR_ITEM_ENABLED_P), Qt))
7246 PROP (TOOL_BAR_ITEM_ENABLED_P)
7247 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_ENABLED_P));
7249 /* Handle radio buttons or toggle boxes. */
7250 if (!NILP (PROP (TOOL_BAR_ITEM_SELECTED_P)))
7251 PROP (TOOL_BAR_ITEM_SELECTED_P)
7252 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_SELECTED_P));
7254 return 1;
7256 #undef PROP
7260 /* Initialize tool_bar_items_vector. REUSE, if non-nil, is a vector
7261 that can be reused. */
7263 static void
7264 init_tool_bar_items (reuse)
7265 Lisp_Object reuse;
7267 if (VECTORP (reuse))
7268 tool_bar_items_vector = reuse;
7269 else
7270 tool_bar_items_vector = Fmake_vector (make_number (64), Qnil);
7271 ntool_bar_items = 0;
7275 /* Append parsed tool bar item properties from
7276 tool_bar_item_properties */
7278 static void
7279 append_tool_bar_item ()
7281 Lisp_Object *to, *from;
7283 /* Enlarge tool_bar_items_vector if necessary. */
7284 if (ntool_bar_items + TOOL_BAR_ITEM_NSLOTS
7285 >= XVECTOR (tool_bar_items_vector)->size)
7287 Lisp_Object new_vector;
7288 int old_size = XVECTOR (tool_bar_items_vector)->size;
7290 new_vector = Fmake_vector (make_number (2 * old_size), Qnil);
7291 bcopy (XVECTOR (tool_bar_items_vector)->contents,
7292 XVECTOR (new_vector)->contents,
7293 old_size * sizeof (Lisp_Object));
7294 tool_bar_items_vector = new_vector;
7297 /* Append entries from tool_bar_item_properties to the end of
7298 tool_bar_items_vector. */
7299 to = XVECTOR (tool_bar_items_vector)->contents + ntool_bar_items;
7300 from = XVECTOR (tool_bar_item_properties)->contents;
7301 bcopy (from, to, TOOL_BAR_ITEM_NSLOTS * sizeof *to);
7302 ntool_bar_items += TOOL_BAR_ITEM_NSLOTS;
7309 /* Read a character using menus based on maps in the array MAPS.
7310 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
7311 Return t if we displayed a menu but the user rejected it.
7313 PREV_EVENT is the previous input event, or nil if we are reading
7314 the first event of a key sequence.
7316 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
7317 if we used a mouse menu to read the input, or zero otherwise. If
7318 USED_MOUSE_MENU is null, we don't dereference it.
7320 The prompting is done based on the prompt-string of the map
7321 and the strings associated with various map elements.
7323 This can be done with X menus or with menus put in the minibuf.
7324 These are done in different ways, depending on how the input will be read.
7325 Menus using X are done after auto-saving in read-char, getting the input
7326 event from Fx_popup_menu; menus using the minibuf use read_char recursively
7327 and do auto-saving in the inner call of read_char. */
7329 static Lisp_Object
7330 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
7331 int nmaps;
7332 Lisp_Object *maps;
7333 Lisp_Object prev_event;
7334 int *used_mouse_menu;
7336 int mapno;
7337 register Lisp_Object name;
7339 if (used_mouse_menu)
7340 *used_mouse_menu = 0;
7342 /* Use local over global Menu maps */
7344 if (! menu_prompting)
7345 return Qnil;
7347 /* Optionally disregard all but the global map. */
7348 if (inhibit_local_menu_bar_menus)
7350 maps += (nmaps - 1);
7351 nmaps = 1;
7354 /* Get the menu name from the first map that has one (a prompt string). */
7355 for (mapno = 0; mapno < nmaps; mapno++)
7357 name = map_prompt (maps[mapno]);
7358 if (!NILP (name))
7359 break;
7362 /* If we don't have any menus, just read a character normally. */
7363 if (mapno >= nmaps)
7364 return Qnil;
7366 #ifdef HAVE_MENUS
7367 /* If we got to this point via a mouse click,
7368 use a real menu for mouse selection. */
7369 if (EVENT_HAS_PARAMETERS (prev_event)
7370 && !EQ (XCAR (prev_event), Qmenu_bar)
7371 && !EQ (XCAR (prev_event), Qtool_bar))
7373 /* Display the menu and get the selection. */
7374 Lisp_Object *realmaps
7375 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
7376 Lisp_Object value;
7377 int nmaps1 = 0;
7379 /* Use the maps that are not nil. */
7380 for (mapno = 0; mapno < nmaps; mapno++)
7381 if (!NILP (maps[mapno]))
7382 realmaps[nmaps1++] = maps[mapno];
7384 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
7385 if (CONSP (value))
7387 Lisp_Object tem;
7389 record_menu_key (XCAR (value));
7391 /* If we got multiple events, unread all but
7392 the first.
7393 There is no way to prevent those unread events
7394 from showing up later in last_nonmenu_event.
7395 So turn symbol and integer events into lists,
7396 to indicate that they came from a mouse menu,
7397 so that when present in last_nonmenu_event
7398 they won't confuse things. */
7399 for (tem = XCDR (value); !NILP (tem);
7400 tem = XCDR (tem))
7402 record_menu_key (XCAR (tem));
7403 if (SYMBOLP (XCAR (tem))
7404 || INTEGERP (XCAR (tem)))
7405 XCAR (tem)
7406 = Fcons (XCAR (tem), Qnil);
7409 /* If we got more than one event, put all but the first
7410 onto this list to be read later.
7411 Return just the first event now. */
7412 Vunread_command_events
7413 = nconc2 (XCDR (value), Vunread_command_events);
7414 value = XCAR (value);
7416 else if (NILP (value))
7417 value = Qt;
7418 if (used_mouse_menu)
7419 *used_mouse_menu = 1;
7420 return value;
7422 #endif /* HAVE_MENUS */
7423 return Qnil ;
7426 /* Buffer in use so far for the minibuf prompts for menu keymaps.
7427 We make this bigger when necessary, and never free it. */
7428 static char *read_char_minibuf_menu_text;
7429 /* Size of that buffer. */
7430 static int read_char_minibuf_menu_width;
7432 static Lisp_Object
7433 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
7434 int commandflag ;
7435 int nmaps;
7436 Lisp_Object *maps;
7438 int mapno;
7439 register Lisp_Object name;
7440 int nlength;
7441 int width = FRAME_WIDTH (SELECTED_FRAME ()) - 4;
7442 int idx = -1;
7443 int nobindings = 1;
7444 Lisp_Object rest, vector;
7445 char *menu;
7447 vector = Qnil;
7449 if (! menu_prompting)
7450 return Qnil;
7452 /* Make sure we have a big enough buffer for the menu text. */
7453 if (read_char_minibuf_menu_text == 0)
7455 read_char_minibuf_menu_width = width + 4;
7456 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
7458 else if (width + 4 > read_char_minibuf_menu_width)
7460 read_char_minibuf_menu_width = width + 4;
7461 read_char_minibuf_menu_text
7462 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
7464 menu = read_char_minibuf_menu_text;
7466 /* Get the menu name from the first map that has one (a prompt string). */
7467 for (mapno = 0; mapno < nmaps; mapno++)
7469 name = map_prompt (maps[mapno]);
7470 if (!NILP (name))
7471 break;
7474 /* If we don't have any menus, just read a character normally. */
7475 if (mapno >= nmaps)
7476 return Qnil;
7478 /* Prompt string always starts with map's prompt, and a space. */
7479 strcpy (menu, XSTRING (name)->data);
7480 nlength = STRING_BYTES (XSTRING (name));
7481 menu[nlength++] = ':';
7482 menu[nlength++] = ' ';
7483 menu[nlength] = 0;
7485 /* Start prompting at start of first map. */
7486 mapno = 0;
7487 rest = maps[mapno];
7489 /* Present the documented bindings, a line at a time. */
7490 while (1)
7492 int notfirst = 0;
7493 int i = nlength;
7494 Lisp_Object obj;
7495 int ch;
7496 Lisp_Object orig_defn_macro;
7498 /* Loop over elements of map. */
7499 while (i < width)
7501 Lisp_Object elt;
7503 /* If reached end of map, start at beginning of next map. */
7504 if (NILP (rest))
7506 mapno++;
7507 /* At end of last map, wrap around to first map if just starting,
7508 or end this line if already have something on it. */
7509 if (mapno == nmaps)
7511 mapno = 0;
7512 if (notfirst || nobindings) break;
7514 rest = maps[mapno];
7517 /* Look at the next element of the map. */
7518 if (idx >= 0)
7519 elt = XVECTOR (vector)->contents[idx];
7520 else
7521 elt = Fcar_safe (rest);
7523 if (idx < 0 && VECTORP (elt))
7525 /* If we found a dense table in the keymap,
7526 advanced past it, but start scanning its contents. */
7527 rest = Fcdr_safe (rest);
7528 vector = elt;
7529 idx = 0;
7531 else
7533 /* An ordinary element. */
7534 Lisp_Object event, tem;
7536 if (idx < 0)
7538 event = Fcar_safe (elt); /* alist */
7539 elt = Fcdr_safe (elt);
7541 else
7543 XSETINT (event, idx); /* vector */
7546 /* Ignore the element if it has no prompt string. */
7547 if (INTEGERP (event) && parse_menu_item (elt, 0, -1))
7549 /* 1 if the char to type matches the string. */
7550 int char_matches;
7551 Lisp_Object upcased_event, downcased_event;
7552 Lisp_Object desc = Qnil;
7553 Lisp_Object s
7554 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
7556 upcased_event = Fupcase (event);
7557 downcased_event = Fdowncase (event);
7558 char_matches = (XINT (upcased_event) == XSTRING (s)->data[0]
7559 || XINT (downcased_event) == XSTRING (s)->data[0]);
7560 if (! char_matches)
7561 desc = Fsingle_key_description (event, Qnil);
7564 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
7565 if (!NILP (tem))
7566 /* Insert equivalent keybinding. */
7567 s = concat2 (s, tem);
7570 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
7571 if (EQ (tem, QCradio) || EQ (tem, QCtoggle))
7573 /* Insert button prefix. */
7574 Lisp_Object selected
7575 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
7576 if (EQ (tem, QCradio))
7577 tem = build_string (NILP (selected) ? "(*) " : "( ) ");
7578 else
7579 tem = build_string (NILP (selected) ? "[X] " : "[ ] ");
7580 s = concat2 (tem, s);
7584 /* If we have room for the prompt string, add it to this line.
7585 If this is the first on the line, always add it. */
7586 if ((XSTRING (s)->size + i + 2
7587 + (char_matches ? 0 : XSTRING (desc)->size + 3))
7588 < width
7589 || !notfirst)
7591 int thiswidth;
7593 /* Punctuate between strings. */
7594 if (notfirst)
7596 strcpy (menu + i, ", ");
7597 i += 2;
7599 notfirst = 1;
7600 nobindings = 0 ;
7602 /* If the char to type doesn't match the string's
7603 first char, explicitly show what char to type. */
7604 if (! char_matches)
7606 /* Add as much of string as fits. */
7607 thiswidth = XSTRING (desc)->size;
7608 if (thiswidth + i > width)
7609 thiswidth = width - i;
7610 bcopy (XSTRING (desc)->data, menu + i, thiswidth);
7611 i += thiswidth;
7612 strcpy (menu + i, " = ");
7613 i += 3;
7616 /* Add as much of string as fits. */
7617 thiswidth = XSTRING (s)->size;
7618 if (thiswidth + i > width)
7619 thiswidth = width - i;
7620 bcopy (XSTRING (s)->data, menu + i, thiswidth);
7621 i += thiswidth;
7622 menu[i] = 0;
7624 else
7626 /* If this element does not fit, end the line now,
7627 and save the element for the next line. */
7628 strcpy (menu + i, "...");
7629 break;
7633 /* Move past this element. */
7634 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
7635 /* Handle reaching end of dense table. */
7636 idx = -1;
7637 if (idx >= 0)
7638 idx++;
7639 else
7640 rest = Fcdr_safe (rest);
7644 /* Prompt with that and read response. */
7645 message2_nolog (menu, strlen (menu),
7646 ! NILP (current_buffer->enable_multibyte_characters));
7648 /* Make believe its not a keyboard macro in case the help char
7649 is pressed. Help characters are not recorded because menu prompting
7650 is not used on replay.
7652 orig_defn_macro = current_kboard->defining_kbd_macro;
7653 current_kboard->defining_kbd_macro = Qnil;
7655 obj = read_char (commandflag, 0, 0, Qt, 0);
7656 while (BUFFERP (obj));
7657 current_kboard->defining_kbd_macro = orig_defn_macro;
7659 if (!INTEGERP (obj))
7660 return obj;
7661 else
7662 ch = XINT (obj);
7664 if (! EQ (obj, menu_prompt_more_char)
7665 && (!INTEGERP (menu_prompt_more_char)
7666 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
7668 if (!NILP (current_kboard->defining_kbd_macro))
7669 store_kbd_macro_char (obj);
7670 return obj;
7672 /* Help char - go round again */
7676 /* Reading key sequences. */
7678 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
7679 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
7680 keymap, or nil otherwise. Return the index of the first keymap in
7681 which KEY has any binding, or NMAPS if no map has a binding.
7683 If KEY is a meta ASCII character, treat it like meta-prefix-char
7684 followed by the corresponding non-meta character. Keymaps in
7685 CURRENT with non-prefix bindings for meta-prefix-char become nil in
7686 NEXT.
7688 If KEY has no bindings in any of the CURRENT maps, NEXT is left
7689 unmodified.
7691 NEXT may be the same array as CURRENT. */
7693 static int
7694 follow_key (key, nmaps, current, defs, next)
7695 Lisp_Object key;
7696 Lisp_Object *current, *defs, *next;
7697 int nmaps;
7699 int i, first_binding;
7700 int did_meta = 0;
7702 first_binding = nmaps;
7703 for (i = nmaps - 1; i >= 0; i--)
7705 if (! NILP (current[i]))
7707 Lisp_Object map;
7708 if (did_meta)
7709 map = defs[i];
7710 else
7711 map = current[i];
7713 defs[i] = access_keymap (map, key, 1, 0, 1);
7714 if (! NILP (defs[i]))
7715 first_binding = i;
7717 else
7718 defs[i] = Qnil;
7721 /* Given the set of bindings we've found, produce the next set of maps. */
7722 if (first_binding < nmaps)
7723 for (i = 0; i < nmaps; i++)
7724 next[i] = NILP (defs[i]) ? Qnil : get_keymap (defs[i], 0, 1);
7726 return first_binding;
7729 /* Read a sequence of keys that ends with a non prefix character,
7730 storing it in KEYBUF, a buffer of size BUFSIZE.
7731 Prompt with PROMPT.
7732 Return the length of the key sequence stored.
7733 Return -1 if the user rejected a command menu.
7735 Echo starting immediately unless `prompt' is 0.
7737 Where a key sequence ends depends on the currently active keymaps.
7738 These include any minor mode keymaps active in the current buffer,
7739 the current buffer's local map, and the global map.
7741 If a key sequence has no other bindings, we check Vfunction_key_map
7742 to see if some trailing subsequence might be the beginning of a
7743 function key's sequence. If so, we try to read the whole function
7744 key, and substitute its symbolic name into the key sequence.
7746 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
7747 `double-' events into similar click events, if that would make them
7748 bound. We try to turn `triple-' events first into `double-' events,
7749 then into clicks.
7751 If we get a mouse click in a mode line, vertical divider, or other
7752 non-text area, we treat the click as if it were prefixed by the
7753 symbol denoting that area - `mode-line', `vertical-line', or
7754 whatever.
7756 If the sequence starts with a mouse click, we read the key sequence
7757 with respect to the buffer clicked on, not the current buffer.
7759 If the user switches frames in the midst of a key sequence, we put
7760 off the switch-frame event until later; the next call to
7761 read_char will return it.
7763 If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
7764 from the selected window's buffer. */
7766 static int
7767 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
7768 can_return_switch_frame, fix_current_buffer)
7769 Lisp_Object *keybuf;
7770 int bufsize;
7771 Lisp_Object prompt;
7772 int dont_downcase_last;
7773 int can_return_switch_frame;
7774 int fix_current_buffer;
7776 volatile Lisp_Object from_string;
7777 volatile int count = specpdl_ptr - specpdl;
7779 /* How many keys there are in the current key sequence. */
7780 volatile int t;
7782 /* The length of the echo buffer when we started reading, and
7783 the length of this_command_keys when we started reading. */
7784 volatile int echo_start;
7785 volatile int keys_start;
7787 /* The number of keymaps we're scanning right now, and the number of
7788 keymaps we have allocated space for. */
7789 volatile int nmaps;
7790 volatile int nmaps_allocated = 0;
7792 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
7793 the current keymaps. */
7794 Lisp_Object *volatile defs = NULL;
7796 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
7797 in the current keymaps, or nil where it is not a prefix. */
7798 Lisp_Object *volatile submaps = NULL;
7800 /* The local map to start out with at start of key sequence. */
7801 volatile Lisp_Object orig_local_map;
7803 /* The map from the `keymap' property to start out with at start of
7804 key sequence. */
7805 volatile Lisp_Object orig_keymap;
7807 /* 1 if we have already considered switching to the local-map property
7808 of the place where a mouse click occurred. */
7809 volatile int localized_local_map = 0;
7811 /* The index in defs[] of the first keymap that has a binding for
7812 this key sequence. In other words, the lowest i such that
7813 defs[i] is non-nil. */
7814 volatile int first_binding;
7816 /* If t < mock_input, then KEYBUF[t] should be read as the next
7817 input key.
7819 We use this to recover after recognizing a function key. Once we
7820 realize that a suffix of the current key sequence is actually a
7821 function key's escape sequence, we replace the suffix with the
7822 function key's binding from Vfunction_key_map. Now keybuf
7823 contains a new and different key sequence, so the echo area,
7824 this_command_keys, and the submaps and defs arrays are wrong. In
7825 this situation, we set mock_input to t, set t to 0, and jump to
7826 restart_sequence; the loop will read keys from keybuf up until
7827 mock_input, thus rebuilding the state; and then it will resume
7828 reading characters from the keyboard. */
7829 volatile int mock_input = 0;
7831 /* If the sequence is unbound in submaps[], then
7832 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
7833 and fkey_map is its binding.
7835 These might be > t, indicating that all function key scanning
7836 should hold off until t reaches them. We do this when we've just
7837 recognized a function key, to avoid searching for the function
7838 key's again in Vfunction_key_map. */
7839 volatile int fkey_start = 0, fkey_end = 0;
7840 volatile Lisp_Object fkey_map;
7842 /* Likewise, for key_translation_map. */
7843 volatile int keytran_start = 0, keytran_end = 0;
7844 volatile Lisp_Object keytran_map;
7846 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
7847 we put it off for later. While we're reading, we keep the event here. */
7848 volatile Lisp_Object delayed_switch_frame;
7850 /* See the comment below... */
7851 #if defined (GOBBLE_FIRST_EVENT)
7852 Lisp_Object first_event;
7853 #endif
7855 volatile Lisp_Object original_uppercase;
7856 volatile int original_uppercase_position = -1;
7858 /* Gets around Microsoft compiler limitations. */
7859 int dummyflag = 0;
7861 struct buffer *starting_buffer;
7863 /* Nonzero if we seem to have got the beginning of a binding
7864 in function_key_map. */
7865 volatile int function_key_possible = 0;
7866 volatile int key_translation_possible = 0;
7868 /* Save the status of key translation before each step,
7869 so that we can restore this after downcasing. */
7870 Lisp_Object prev_fkey_map;
7871 int prev_fkey_start;
7872 int prev_fkey_end;
7874 Lisp_Object prev_keytran_map;
7875 int prev_keytran_start;
7876 int prev_keytran_end;
7878 #if defined (GOBBLE_FIRST_EVENT)
7879 int junk;
7880 #endif
7882 raw_keybuf_count = 0;
7884 last_nonmenu_event = Qnil;
7886 delayed_switch_frame = Qnil;
7887 fkey_map = Vfunction_key_map;
7888 keytran_map = Vkey_translation_map;
7890 /* If there is no function-key-map, turn off function key scanning. */
7891 if (!KEYMAPP (Vfunction_key_map))
7892 fkey_start = fkey_end = bufsize + 1;
7894 /* If there is no key-translation-map, turn off scanning. */
7895 if (!KEYMAPP (Vkey_translation_map))
7896 keytran_start = keytran_end = bufsize + 1;
7898 if (INTERACTIVE)
7900 if (!NILP (prompt))
7901 echo_prompt (prompt);
7902 else if (cursor_in_echo_area
7903 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
7904 && NILP (Fzerop (Vecho_keystrokes)))
7905 /* This doesn't put in a dash if the echo buffer is empty, so
7906 you don't always see a dash hanging out in the minibuffer. */
7907 echo_dash ();
7910 /* Record the initial state of the echo area and this_command_keys;
7911 we will need to restore them if we replay a key sequence. */
7912 if (INTERACTIVE)
7913 echo_start = echo_length ();
7914 keys_start = this_command_key_count;
7915 this_single_command_key_start = keys_start;
7917 #if defined (GOBBLE_FIRST_EVENT)
7918 /* This doesn't quite work, because some of the things that read_char
7919 does cannot safely be bypassed. It seems too risky to try to make
7920 this work right. */
7922 /* Read the first char of the sequence specially, before setting
7923 up any keymaps, in case a filter runs and switches buffers on us. */
7924 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
7925 &junk);
7926 #endif /* GOBBLE_FIRST_EVENT */
7928 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
7929 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
7930 from_string = Qnil;
7932 /* We jump here when the key sequence has been thoroughly changed, and
7933 we need to rescan it starting from the beginning. When we jump here,
7934 keybuf[0..mock_input] holds the sequence we should reread. */
7935 replay_sequence:
7937 starting_buffer = current_buffer;
7938 function_key_possible = 0;
7939 key_translation_possible = 0;
7941 /* Build our list of keymaps.
7942 If we recognize a function key and replace its escape sequence in
7943 keybuf with its symbol, or if the sequence starts with a mouse
7944 click and we need to switch buffers, we jump back here to rebuild
7945 the initial keymaps from the current buffer. */
7947 Lisp_Object *maps;
7949 if (!NILP (current_kboard->Voverriding_terminal_local_map)
7950 || !NILP (Voverriding_local_map))
7952 if (3 > nmaps_allocated)
7954 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
7955 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
7956 nmaps_allocated = 3;
7958 nmaps = 0;
7959 if (!NILP (current_kboard->Voverriding_terminal_local_map))
7960 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
7961 if (!NILP (Voverriding_local_map))
7962 submaps[nmaps++] = Voverriding_local_map;
7964 else
7966 int extra_maps = 2;
7967 nmaps = current_minor_maps (0, &maps);
7968 if (!NILP (orig_keymap))
7969 extra_maps = 3;
7970 if (nmaps + extra_maps > nmaps_allocated)
7972 submaps = (Lisp_Object *) alloca ((nmaps+extra_maps)
7973 * sizeof (submaps[0]));
7974 defs = (Lisp_Object *) alloca ((nmaps+extra_maps)
7975 * sizeof (defs[0]));
7976 nmaps_allocated = nmaps + extra_maps;
7978 bcopy (maps, (void *) submaps, nmaps * sizeof (submaps[0]));
7979 if (!NILP (orig_keymap))
7980 submaps[nmaps++] = orig_keymap;
7981 submaps[nmaps++] = orig_local_map;
7983 submaps[nmaps++] = current_global_map;
7986 /* Find an accurate initial value for first_binding. */
7987 for (first_binding = 0; first_binding < nmaps; first_binding++)
7988 if (! NILP (submaps[first_binding]))
7989 break;
7991 /* Start from the beginning in keybuf. */
7992 t = 0;
7994 /* These are no-ops the first time through, but if we restart, they
7995 revert the echo area and this_command_keys to their original state. */
7996 this_command_key_count = keys_start;
7997 if (INTERACTIVE && t < mock_input)
7998 echo_truncate (echo_start);
8000 /* If the best binding for the current key sequence is a keymap, or
8001 we may be looking at a function key's escape sequence, keep on
8002 reading. */
8003 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
8004 || (first_binding >= nmaps
8005 && fkey_start < t
8006 /* mock input is never part of a function key's sequence. */
8007 && mock_input <= fkey_start)
8008 || (first_binding >= nmaps
8009 && keytran_start < t && key_translation_possible)
8010 /* Don't return in the middle of a possible function key sequence,
8011 if the only bindings we found were via case conversion.
8012 Thus, if ESC O a has a function-key-map translation
8013 and ESC o has a binding, don't return after ESC O,
8014 so that we can translate ESC O plus the next character. */
8017 Lisp_Object key;
8018 int used_mouse_menu = 0;
8020 /* Where the last real key started. If we need to throw away a
8021 key that has expanded into more than one element of keybuf
8022 (say, a mouse click on the mode line which is being treated
8023 as [mode-line (mouse-...)], then we backtrack to this point
8024 of keybuf. */
8025 volatile int last_real_key_start;
8027 /* These variables are analogous to echo_start and keys_start;
8028 while those allow us to restart the entire key sequence,
8029 echo_local_start and keys_local_start allow us to throw away
8030 just one key. */
8031 volatile int echo_local_start, keys_local_start, local_first_binding;
8033 if (t >= bufsize)
8034 error ("Key sequence too long");
8036 if (INTERACTIVE)
8037 echo_local_start = echo_length ();
8038 keys_local_start = this_command_key_count;
8039 local_first_binding = first_binding;
8041 replay_key:
8042 /* These are no-ops, unless we throw away a keystroke below and
8043 jumped back up to replay_key; in that case, these restore the
8044 variables to their original state, allowing us to replay the
8045 loop. */
8046 if (INTERACTIVE && t < mock_input)
8047 echo_truncate (echo_local_start);
8048 this_command_key_count = keys_local_start;
8049 first_binding = local_first_binding;
8051 /* By default, assume each event is "real". */
8052 last_real_key_start = t;
8054 /* Does mock_input indicate that we are re-reading a key sequence? */
8055 if (t < mock_input)
8057 key = keybuf[t];
8058 add_command_key (key);
8059 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
8060 && NILP (Fzerop (Vecho_keystrokes)))
8061 echo_char (key);
8064 /* If not, we should actually read a character. */
8065 else
8068 #ifdef MULTI_KBOARD
8069 KBOARD *interrupted_kboard = current_kboard;
8070 struct frame *interrupted_frame = SELECTED_FRAME ();
8071 if (setjmp (wrong_kboard_jmpbuf))
8073 if (!NILP (delayed_switch_frame))
8075 interrupted_kboard->kbd_queue
8076 = Fcons (delayed_switch_frame,
8077 interrupted_kboard->kbd_queue);
8078 delayed_switch_frame = Qnil;
8080 while (t > 0)
8081 interrupted_kboard->kbd_queue
8082 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
8084 /* If the side queue is non-empty, ensure it begins with a
8085 switch-frame, so we'll replay it in the right context. */
8086 if (CONSP (interrupted_kboard->kbd_queue)
8087 && (key = XCAR (interrupted_kboard->kbd_queue),
8088 !(EVENT_HAS_PARAMETERS (key)
8089 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
8090 Qswitch_frame))))
8092 Lisp_Object frame;
8093 XSETFRAME (frame, interrupted_frame);
8094 interrupted_kboard->kbd_queue
8095 = Fcons (make_lispy_switch_frame (frame),
8096 interrupted_kboard->kbd_queue);
8098 mock_input = 0;
8099 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8100 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8101 goto replay_sequence;
8103 #endif
8104 key = read_char (NILP (prompt), nmaps,
8105 (Lisp_Object *) submaps, last_nonmenu_event,
8106 &used_mouse_menu);
8109 /* read_char returns t when it shows a menu and the user rejects it.
8110 Just return -1. */
8111 if (EQ (key, Qt))
8113 unbind_to (count, Qnil);
8114 return -1;
8117 /* read_char returns -1 at the end of a macro.
8118 Emacs 18 handles this by returning immediately with a
8119 zero, so that's what we'll do. */
8120 if (INTEGERP (key) && XINT (key) == -1)
8122 t = 0;
8123 /* The Microsoft C compiler can't handle the goto that
8124 would go here. */
8125 dummyflag = 1;
8126 break;
8129 /* If the current buffer has been changed from under us, the
8130 keymap may have changed, so replay the sequence. */
8131 if (BUFFERP (key))
8133 mock_input = t;
8134 /* Reset the current buffer from the selected window
8135 in case something changed the former and not the latter.
8136 This is to be more consistent with the behavior
8137 of the command_loop_1. */
8138 if (fix_current_buffer)
8140 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8141 Fkill_emacs (Qnil);
8142 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
8143 Fset_buffer (XWINDOW (selected_window)->buffer);
8146 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8147 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8148 goto replay_sequence;
8151 /* If we have a quit that was typed in another frame, and
8152 quit_throw_to_read_char switched buffers,
8153 replay to get the right keymap. */
8154 if (XINT (key) == quit_char && current_buffer != starting_buffer)
8156 GROW_RAW_KEYBUF;
8157 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8158 keybuf[t++] = key;
8159 mock_input = t;
8160 Vquit_flag = Qnil;
8161 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8162 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8163 goto replay_sequence;
8166 Vquit_flag = Qnil;
8168 if (EVENT_HAS_PARAMETERS (key)
8169 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), Qswitch_frame))
8171 /* If we're at the beginning of a key sequence, and the caller
8172 says it's okay, go ahead and return this event. If we're
8173 in the midst of a key sequence, delay it until the end. */
8174 if (t > 0 || !can_return_switch_frame)
8176 delayed_switch_frame = key;
8177 goto replay_key;
8181 GROW_RAW_KEYBUF;
8182 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8185 /* Clicks in non-text areas get prefixed by the symbol
8186 in their CHAR-ADDRESS field. For example, a click on
8187 the mode line is prefixed by the symbol `mode-line'.
8189 Furthermore, key sequences beginning with mouse clicks
8190 are read using the keymaps of the buffer clicked on, not
8191 the current buffer. So we may have to switch the buffer
8192 here.
8194 When we turn one event into two events, we must make sure
8195 that neither of the two looks like the original--so that,
8196 if we replay the events, they won't be expanded again.
8197 If not for this, such reexpansion could happen either here
8198 or when user programs play with this-command-keys. */
8199 if (EVENT_HAS_PARAMETERS (key))
8201 Lisp_Object kind;
8203 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
8204 if (EQ (kind, Qmouse_click))
8206 Lisp_Object window, posn;
8208 window = POSN_WINDOW (EVENT_START (key));
8209 posn = POSN_BUFFER_POSN (EVENT_START (key));
8211 if (CONSP (posn))
8213 /* We're looking at the second event of a
8214 sequence which we expanded before. Set
8215 last_real_key_start appropriately. */
8216 if (t > 0)
8217 last_real_key_start = t - 1;
8220 /* Key sequences beginning with mouse clicks are
8221 read using the keymaps in the buffer clicked on,
8222 not the current buffer. If we're at the
8223 beginning of a key sequence, switch buffers. */
8224 if (last_real_key_start == 0
8225 && WINDOWP (window)
8226 && BUFFERP (XWINDOW (window)->buffer)
8227 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
8229 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8230 keybuf[t] = key;
8231 mock_input = t + 1;
8233 /* Arrange to go back to the original buffer once we're
8234 done reading the key sequence. Note that we can't
8235 use save_excursion_{save,restore} here, because they
8236 save point as well as the current buffer; we don't
8237 want to save point, because redisplay may change it,
8238 to accommodate a Fset_window_start or something. We
8239 don't want to do this at the top of the function,
8240 because we may get input from a subprocess which
8241 wants to change the selected window and stuff (say,
8242 emacsclient). */
8243 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
8245 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8246 Fkill_emacs (Qnil);
8247 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
8248 orig_local_map = get_local_map (PT, current_buffer,
8249 Qlocal_map);
8250 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8251 goto replay_sequence;
8254 /* For a mouse click, get the local text-property keymap
8255 of the place clicked on, rather than point. */
8256 if (last_real_key_start == 0
8257 && CONSP (XCDR (key))
8258 && ! localized_local_map)
8260 Lisp_Object map_here, start, pos;
8262 localized_local_map = 1;
8263 start = EVENT_START (key);
8265 if (CONSP (start) && CONSP (XCDR (start)))
8267 pos = POSN_BUFFER_POSN (start);
8268 if (INTEGERP (pos)
8269 && XINT (pos) >= BEG && XINT (pos) <= Z)
8271 map_here = get_local_map (XINT (pos),
8272 current_buffer, Qlocal_map);
8273 if (!EQ (map_here, orig_local_map))
8275 orig_local_map = map_here;
8276 keybuf[t] = key;
8277 mock_input = t + 1;
8279 goto replay_sequence;
8281 map_here = get_local_map (XINT (pos),
8282 current_buffer, Qkeymap);
8283 if (!EQ (map_here, orig_keymap))
8285 orig_keymap = map_here;
8286 keybuf[t] = key;
8287 mock_input = t + 1;
8289 goto replay_sequence;
8295 /* Expand mode-line and scroll-bar events into two events:
8296 use posn as a fake prefix key. */
8297 if (SYMBOLP (posn))
8299 if (t + 1 >= bufsize)
8300 error ("Key sequence too long");
8301 keybuf[t] = posn;
8302 keybuf[t+1] = key;
8303 mock_input = t + 2;
8305 /* Zap the position in key, so we know that we've
8306 expanded it, and don't try to do so again. */
8307 POSN_BUFFER_POSN (EVENT_START (key))
8308 = Fcons (posn, Qnil);
8310 /* If on a mode line string with a local keymap,
8311 reconsider the key sequence with that keymap. */
8312 if (CONSP (POSN_STRING (EVENT_START (key))))
8314 Lisp_Object string, pos, map, map2;
8316 string = POSN_STRING (EVENT_START (key));
8317 pos = XCDR (string);
8318 string = XCAR (string);
8319 if (XINT (pos) >= 0
8320 && XINT (pos) < XSTRING (string)->size)
8322 map = Fget_text_property (pos, Qlocal_map, string);
8323 if (!NILP (map))
8324 orig_local_map = map;
8325 map2 = Fget_text_property (pos, Qkeymap, string);
8326 if (!NILP (map2))
8327 orig_keymap = map2;
8328 if (!NILP (map) || !NILP (map2))
8329 goto replay_sequence;
8333 goto replay_key;
8335 else if (CONSP (POSN_STRING (EVENT_START (key)))
8336 && NILP (from_string))
8338 /* For a click on a string, i.e. overlay string or a
8339 string displayed via the `display' property,
8340 consider `local-map' and `keymap' properties of
8341 that string. */
8342 Lisp_Object string, pos, map, map2;
8344 string = POSN_STRING (EVENT_START (key));
8345 pos = XCDR (string);
8346 string = XCAR (string);
8347 if (XINT (pos) >= 0
8348 && XINT (pos) < XSTRING (string)->size)
8350 map = Fget_text_property (pos, Qlocal_map, string);
8351 if (!NILP (map))
8352 orig_local_map = map;
8353 map2 = Fget_text_property (pos, Qkeymap, string);
8354 if (!NILP (map2))
8355 orig_keymap = map2;
8357 if (!NILP (map) || !NILP (map2))
8359 from_string = string;
8360 goto replay_sequence;
8365 else if (CONSP (XCDR (key))
8366 && CONSP (EVENT_START (key))
8367 && CONSP (XCDR (EVENT_START (key))))
8369 Lisp_Object posn;
8371 posn = POSN_BUFFER_POSN (EVENT_START (key));
8372 /* Handle menu-bar events:
8373 insert the dummy prefix event `menu-bar'. */
8374 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
8376 if (t + 1 >= bufsize)
8377 error ("Key sequence too long");
8378 keybuf[t] = posn;
8379 keybuf[t+1] = key;
8381 /* Zap the position in key, so we know that we've
8382 expanded it, and don't try to do so again. */
8383 POSN_BUFFER_POSN (EVENT_START (key))
8384 = Fcons (posn, Qnil);
8386 mock_input = t + 2;
8387 goto replay_sequence;
8389 else if (CONSP (posn))
8391 /* We're looking at the second event of a
8392 sequence which we expanded before. Set
8393 last_real_key_start appropriately. */
8394 if (last_real_key_start == t && t > 0)
8395 last_real_key_start = t - 1;
8400 /* We have finally decided that KEY is something we might want
8401 to look up. */
8402 first_binding = (follow_key (key,
8403 nmaps - first_binding,
8404 submaps + first_binding,
8405 defs + first_binding,
8406 submaps + first_binding)
8407 + first_binding);
8409 /* If KEY wasn't bound, we'll try some fallbacks. */
8410 if (first_binding >= nmaps)
8412 Lisp_Object head;
8414 head = EVENT_HEAD (key);
8415 if (help_char_p (head) && t > 0)
8417 read_key_sequence_cmd = Vprefix_help_command;
8418 keybuf[t++] = key;
8419 last_nonmenu_event = key;
8420 /* The Microsoft C compiler can't handle the goto that
8421 would go here. */
8422 dummyflag = 1;
8423 break;
8426 if (SYMBOLP (head))
8428 Lisp_Object breakdown;
8429 int modifiers;
8431 breakdown = parse_modifiers (head);
8432 modifiers = XINT (XCAR (XCDR (breakdown)));
8433 /* Attempt to reduce an unbound mouse event to a simpler
8434 event that is bound:
8435 Drags reduce to clicks.
8436 Double-clicks reduce to clicks.
8437 Triple-clicks reduce to double-clicks, then to clicks.
8438 Down-clicks are eliminated.
8439 Double-downs reduce to downs, then are eliminated.
8440 Triple-downs reduce to double-downs, then to downs,
8441 then are eliminated. */
8442 if (modifiers & (down_modifier | drag_modifier
8443 | double_modifier | triple_modifier))
8445 while (modifiers & (down_modifier | drag_modifier
8446 | double_modifier | triple_modifier))
8448 Lisp_Object new_head, new_click;
8449 if (modifiers & triple_modifier)
8450 modifiers ^= (double_modifier | triple_modifier);
8451 else if (modifiers & double_modifier)
8452 modifiers &= ~double_modifier;
8453 else if (modifiers & drag_modifier)
8454 modifiers &= ~drag_modifier;
8455 else
8457 /* Dispose of this `down' event by simply jumping
8458 back to replay_key, to get another event.
8460 Note that if this event came from mock input,
8461 then just jumping back to replay_key will just
8462 hand it to us again. So we have to wipe out any
8463 mock input.
8465 We could delete keybuf[t] and shift everything
8466 after that to the left by one spot, but we'd also
8467 have to fix up any variable that points into
8468 keybuf, and shifting isn't really necessary
8469 anyway.
8471 Adding prefixes for non-textual mouse clicks
8472 creates two characters of mock input, and both
8473 must be thrown away. If we're only looking at
8474 the prefix now, we can just jump back to
8475 replay_key. On the other hand, if we've already
8476 processed the prefix, and now the actual click
8477 itself is giving us trouble, then we've lost the
8478 state of the keymaps we want to backtrack to, and
8479 we need to replay the whole sequence to rebuild
8482 Beyond that, only function key expansion could
8483 create more than two keys, but that should never
8484 generate mouse events, so it's okay to zero
8485 mock_input in that case too.
8487 Isn't this just the most wonderful code ever? */
8488 if (t == last_real_key_start)
8490 mock_input = 0;
8491 goto replay_key;
8493 else
8495 mock_input = last_real_key_start;
8496 goto replay_sequence;
8500 new_head
8501 = apply_modifiers (modifiers, XCAR (breakdown));
8502 new_click
8503 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
8505 /* Look for a binding for this new key. follow_key
8506 promises that it didn't munge submaps the
8507 last time we called it, since key was unbound. */
8508 first_binding
8509 = (follow_key (new_click,
8510 nmaps - local_first_binding,
8511 submaps + local_first_binding,
8512 defs + local_first_binding,
8513 submaps + local_first_binding)
8514 + local_first_binding);
8516 /* If that click is bound, go for it. */
8517 if (first_binding < nmaps)
8519 key = new_click;
8520 break;
8522 /* Otherwise, we'll leave key set to the drag event. */
8528 keybuf[t++] = key;
8529 /* Normally, last_nonmenu_event gets the previous key we read.
8530 But when a mouse popup menu is being used,
8531 we don't update last_nonmenu_event; it continues to hold the mouse
8532 event that preceded the first level of menu. */
8533 if (!used_mouse_menu)
8534 last_nonmenu_event = key;
8536 /* Record what part of this_command_keys is the current key sequence. */
8537 this_single_command_key_start = this_command_key_count - t;
8539 prev_fkey_map = fkey_map;
8540 prev_fkey_start = fkey_start;
8541 prev_fkey_end = fkey_end;
8543 prev_keytran_map = keytran_map;
8544 prev_keytran_start = keytran_start;
8545 prev_keytran_end = keytran_end;
8547 /* If the sequence is unbound, see if we can hang a function key
8548 off the end of it. We only want to scan real keyboard input
8549 for function key sequences, so if mock_input says that we're
8550 re-reading old events, don't examine it. */
8551 if (first_binding >= nmaps
8552 && t >= mock_input)
8554 Lisp_Object fkey_next;
8556 /* Continue scan from fkey_end until we find a bound suffix.
8557 If we fail, increment fkey_start
8558 and start fkey_end from there. */
8559 while (fkey_end < t)
8561 Lisp_Object key;
8563 key = keybuf[fkey_end++];
8564 fkey_next
8565 = access_keymap (fkey_map, key, 1, 0, 1);
8567 /* Handle symbol with autoload definition. */
8568 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8569 && CONSP (XSYMBOL (fkey_next)->function)
8570 && EQ (XCAR (XSYMBOL (fkey_next)->function), Qautoload))
8571 do_autoload (XSYMBOL (fkey_next)->function,
8572 fkey_next);
8574 /* Handle a symbol whose function definition is a keymap
8575 or an array. */
8576 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8577 && (!NILP (Farrayp (XSYMBOL (fkey_next)->function))
8578 || KEYMAPP (XSYMBOL (fkey_next)->function)))
8579 fkey_next = XSYMBOL (fkey_next)->function;
8581 #if 0 /* I didn't turn this on, because it might cause trouble
8582 for the mapping of return into C-m and tab into C-i. */
8583 /* Optionally don't map function keys into other things.
8584 This enables the user to redefine kp- keys easily. */
8585 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
8586 fkey_next = Qnil;
8587 #endif
8589 /* If the function key map gives a function, not an
8590 array, then call the function with no args and use
8591 its value instead. */
8592 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8593 && fkey_end == t)
8595 struct gcpro gcpro1, gcpro2, gcpro3;
8596 Lisp_Object tem;
8597 tem = fkey_next;
8599 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
8600 fkey_next = call1 (fkey_next, prompt);
8601 UNGCPRO;
8602 /* If the function returned something invalid,
8603 barf--don't ignore it.
8604 (To ignore it safely, we would need to gcpro a bunch of
8605 other variables.) */
8606 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
8607 error ("Function in key-translation-map returns invalid key sequence");
8610 function_key_possible = ! NILP (fkey_next);
8612 /* If keybuf[fkey_start..fkey_end] is bound in the
8613 function key map and it's a suffix of the current
8614 sequence (i.e. fkey_end == t), replace it with
8615 the binding and restart with fkey_start at the end. */
8616 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
8617 && fkey_end == t)
8619 int len = XFASTINT (Flength (fkey_next));
8621 t = fkey_start + len;
8622 if (t >= bufsize)
8623 error ("Key sequence too long");
8625 if (VECTORP (fkey_next))
8626 bcopy (XVECTOR (fkey_next)->contents,
8627 keybuf + fkey_start,
8628 (t - fkey_start) * sizeof (keybuf[0]));
8629 else if (STRINGP (fkey_next))
8631 int i;
8633 for (i = 0; i < len; i++)
8634 XSETFASTINT (keybuf[fkey_start + i],
8635 XSTRING (fkey_next)->data[i]);
8638 mock_input = t;
8639 fkey_start = fkey_end = t;
8640 fkey_map = Vfunction_key_map;
8642 /* Do pass the results through key-translation-map.
8643 But don't retranslate what key-translation-map
8644 has already translated. */
8645 keytran_end = keytran_start;
8646 keytran_map = Vkey_translation_map;
8648 goto replay_sequence;
8651 fkey_map = get_keymap (fkey_next, 0, 1);
8653 /* If we no longer have a bound suffix, try a new positions for
8654 fkey_start. */
8655 if (!CONSP (fkey_map))
8657 fkey_end = ++fkey_start;
8658 fkey_map = Vfunction_key_map;
8659 function_key_possible = 0;
8664 /* Look for this sequence in key-translation-map. */
8666 Lisp_Object keytran_next;
8668 /* Scan from keytran_end until we find a bound suffix. */
8669 while (keytran_end < t)
8671 Lisp_Object key;
8673 key = keybuf[keytran_end++];
8674 keytran_next
8675 = access_keymap (keytran_map, key, 1, 0, 1);
8677 /* Handle symbol with autoload definition. */
8678 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8679 && CONSP (XSYMBOL (keytran_next)->function)
8680 && EQ (XCAR (XSYMBOL (keytran_next)->function), Qautoload))
8681 do_autoload (XSYMBOL (keytran_next)->function,
8682 keytran_next);
8684 /* Handle a symbol whose function definition is a keymap
8685 or an array. */
8686 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8687 && (!NILP (Farrayp (XSYMBOL (keytran_next)->function))
8688 || KEYMAPP (XSYMBOL (keytran_next)->function)))
8689 keytran_next = XSYMBOL (keytran_next)->function;
8691 /* If the key translation map gives a function, not an
8692 array, then call the function with one arg and use
8693 its value instead. */
8694 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8695 && keytran_end == t)
8697 struct gcpro gcpro1, gcpro2, gcpro3;
8698 Lisp_Object tem;
8699 tem = keytran_next;
8701 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
8702 keytran_next = call1 (keytran_next, prompt);
8703 UNGCPRO;
8704 /* If the function returned something invalid,
8705 barf--don't ignore it.
8706 (To ignore it safely, we would need to gcpro a bunch of
8707 other variables.) */
8708 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
8709 error ("Function in key-translation-map returns invalid key sequence");
8712 key_translation_possible = ! NILP (keytran_next);
8714 /* If keybuf[keytran_start..keytran_end] is bound in the
8715 key translation map and it's a suffix of the current
8716 sequence (i.e. keytran_end == t), replace it with
8717 the binding and restart with keytran_start at the end. */
8718 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
8719 && keytran_end == t)
8721 int len = XFASTINT (Flength (keytran_next));
8723 t = keytran_start + len;
8724 if (t >= bufsize)
8725 error ("Key sequence too long");
8727 if (VECTORP (keytran_next))
8728 bcopy (XVECTOR (keytran_next)->contents,
8729 keybuf + keytran_start,
8730 (t - keytran_start) * sizeof (keybuf[0]));
8731 else if (STRINGP (keytran_next))
8733 int i;
8735 for (i = 0; i < len; i++)
8736 XSETFASTINT (keybuf[keytran_start + i],
8737 XSTRING (keytran_next)->data[i]);
8740 mock_input = t;
8741 keytran_start = keytran_end = t;
8742 keytran_map = Vkey_translation_map;
8744 /* Don't pass the results of key-translation-map
8745 through function-key-map. */
8746 fkey_start = fkey_end = t;
8747 fkey_map = Vfunction_key_map;
8749 goto replay_sequence;
8752 keytran_map = get_keymap (keytran_next, 0, 1);
8754 /* If we no longer have a bound suffix, try a new positions for
8755 keytran_start. */
8756 if (!CONSP (keytran_map))
8758 keytran_end = ++keytran_start;
8759 keytran_map = Vkey_translation_map;
8760 key_translation_possible = 0;
8765 /* If KEY is not defined in any of the keymaps,
8766 and cannot be part of a function key or translation,
8767 and is an upper case letter
8768 use the corresponding lower-case letter instead. */
8769 if (first_binding == nmaps && ! function_key_possible
8770 && ! key_translation_possible
8771 && INTEGERP (key)
8772 && ((((XINT (key) & 0x3ffff)
8773 < XCHAR_TABLE (current_buffer->downcase_table)->size)
8774 && UPPERCASEP (XINT (key) & 0x3ffff))
8775 || (XINT (key) & shift_modifier)))
8777 Lisp_Object new_key;
8779 original_uppercase = key;
8780 original_uppercase_position = t - 1;
8782 if (XINT (key) & shift_modifier)
8783 XSETINT (new_key, XINT (key) & ~shift_modifier);
8784 else
8785 XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
8786 | (XINT (key) & ~0x3ffff)));
8788 /* We have to do this unconditionally, regardless of whether
8789 the lower-case char is defined in the keymaps, because they
8790 might get translated through function-key-map. */
8791 keybuf[t - 1] = new_key;
8792 mock_input = t;
8794 fkey_map = prev_fkey_map;
8795 fkey_start = prev_fkey_start;
8796 fkey_end = prev_fkey_end;
8798 keytran_map = prev_keytran_map;
8799 keytran_start = prev_keytran_start;
8800 keytran_end = prev_keytran_end;
8802 goto replay_sequence;
8804 /* If KEY is not defined in any of the keymaps,
8805 and cannot be part of a function key or translation,
8806 and is a shifted function key,
8807 use the corresponding unshifted function key instead. */
8808 if (first_binding == nmaps && ! function_key_possible
8809 && ! key_translation_possible
8810 && SYMBOLP (key))
8812 Lisp_Object breakdown;
8813 int modifiers;
8815 breakdown = parse_modifiers (key);
8816 modifiers = XINT (XCAR (XCDR (breakdown)));
8817 if (modifiers & shift_modifier)
8819 Lisp_Object new_key;
8821 original_uppercase = key;
8822 original_uppercase_position = t - 1;
8824 modifiers &= ~shift_modifier;
8825 new_key = apply_modifiers (modifiers,
8826 XCAR (breakdown));
8828 keybuf[t - 1] = new_key;
8829 mock_input = t;
8831 fkey_map = prev_fkey_map;
8832 fkey_start = prev_fkey_start;
8833 fkey_end = prev_fkey_end;
8835 keytran_map = prev_keytran_map;
8836 keytran_start = prev_keytran_start;
8837 keytran_end = prev_keytran_end;
8839 goto replay_sequence;
8844 if (!dummyflag)
8845 read_key_sequence_cmd = (first_binding < nmaps
8846 ? defs[first_binding]
8847 : Qnil);
8849 unread_switch_frame = delayed_switch_frame;
8850 unbind_to (count, Qnil);
8852 /* Don't downcase the last character if the caller says don't.
8853 Don't downcase it if the result is undefined, either. */
8854 if ((dont_downcase_last || first_binding >= nmaps)
8855 && t - 1 == original_uppercase_position)
8856 keybuf[t - 1] = original_uppercase;
8858 /* Occasionally we fabricate events, perhaps by expanding something
8859 according to function-key-map, or by adding a prefix symbol to a
8860 mouse click in the scroll bar or modeline. In this cases, return
8861 the entire generated key sequence, even if we hit an unbound
8862 prefix or a definition before the end. This means that you will
8863 be able to push back the event properly, and also means that
8864 read-key-sequence will always return a logical unit.
8866 Better ideas? */
8867 for (; t < mock_input; t++)
8869 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
8870 && NILP (Fzerop (Vecho_keystrokes)))
8871 echo_char (keybuf[t]);
8872 add_command_key (keybuf[t]);
8877 return t;
8880 #if 0 /* This doc string is too long for some compilers.
8881 This commented-out definition serves for DOC. */
8882 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
8883 "Read a sequence of keystrokes and return as a string or vector.\n\
8884 The sequence is sufficient to specify a non-prefix command in the\n\
8885 current local and global maps.\n\
8887 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
8888 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
8889 as a continuation of the previous key.\n\
8891 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
8892 convert the last event to lower case. (Normally any upper case event\n\
8893 is converted to lower case if the original event is undefined and the lower\n\
8894 case equivalent is defined.) A non-nil value is appropriate for reading\n\
8895 a key sequence to be defined.\n\
8897 A C-g typed while in this function is treated like any other character,\n\
8898 and `quit-flag' is not set.\n\
8900 If the key sequence starts with a mouse click, then the sequence is read\n\
8901 using the keymaps of the buffer of the window clicked in, not the buffer\n\
8902 of the selected window as normal.\n\
8903 ""\n\
8904 `read-key-sequence' drops unbound button-down events, since you normally\n\
8905 only care about the click or drag events which follow them. If a drag\n\
8906 or multi-click event is unbound, but the corresponding click event would\n\
8907 be bound, `read-key-sequence' turns the event into a click event at the\n\
8908 drag's starting position. This means that you don't have to distinguish\n\
8909 between click and drag, double, or triple events unless you want to.\n\
8911 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
8912 lines separating windows, and scroll bars with imaginary keys\n\
8913 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
8915 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
8916 function will process a switch-frame event if the user switches frames\n\
8917 before typing anything. If the user switches frames in the middle of a\n\
8918 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
8919 is nil, then the event will be put off until after the current key sequence.\n\
8921 `read-key-sequence' checks `function-key-map' for function key\n\
8922 sequences, where they wouldn't conflict with ordinary bindings. See\n\
8923 `function-key-map' for more details.\n\
8925 The optional fifth argument COMMAND-LOOP, if non-nil, means\n\
8926 that this key sequence is being read by something that will\n\
8927 read commands one after another. It should be nil if the caller\n\
8928 will read just one key sequence.")
8929 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame, command-loop)
8930 #endif
8932 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 5, 0,
8934 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
8935 command_loop)
8936 Lisp_Object prompt, continue_echo, dont_downcase_last;
8937 Lisp_Object can_return_switch_frame, command_loop;
8939 Lisp_Object keybuf[30];
8940 register int i;
8941 struct gcpro gcpro1;
8942 int count = specpdl_ptr - specpdl;
8944 if (!NILP (prompt))
8945 CHECK_STRING (prompt, 0);
8946 QUIT;
8948 specbind (Qinput_method_exit_on_first_char,
8949 (NILP (command_loop) ? Qt : Qnil));
8950 specbind (Qinput_method_use_echo_area,
8951 (NILP (command_loop) ? Qt : Qnil));
8953 bzero (keybuf, sizeof keybuf);
8954 GCPRO1 (keybuf[0]);
8955 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
8957 if (NILP (continue_echo))
8959 this_command_key_count = 0;
8960 this_single_command_key_start = 0;
8963 #ifdef HAVE_X_WINDOWS
8964 if (display_hourglass_p)
8965 cancel_hourglass ();
8966 #endif
8968 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
8969 prompt, ! NILP (dont_downcase_last),
8970 ! NILP (can_return_switch_frame), 0);
8972 #if 0 /* The following is fine for code reading a key sequence and
8973 then proceeding with a lenghty compuation, but it's not good
8974 for code reading keys in a loop, like an input method. */
8975 #ifdef HAVE_X_WINDOWS
8976 if (display_hourglass_p)
8977 start_hourglass ();
8978 #endif
8979 #endif
8981 if (i == -1)
8983 Vquit_flag = Qt;
8984 QUIT;
8986 UNGCPRO;
8987 return unbind_to (count, make_event_array (i, keybuf));
8990 DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
8991 Sread_key_sequence_vector, 1, 5, 0,
8992 "Like `read-key-sequence' but always return a vector.")
8993 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
8994 command_loop)
8995 Lisp_Object prompt, continue_echo, dont_downcase_last;
8996 Lisp_Object can_return_switch_frame, command_loop;
8998 Lisp_Object keybuf[30];
8999 register int i;
9000 struct gcpro gcpro1;
9001 int count = specpdl_ptr - specpdl;
9003 if (!NILP (prompt))
9004 CHECK_STRING (prompt, 0);
9005 QUIT;
9007 specbind (Qinput_method_exit_on_first_char,
9008 (NILP (command_loop) ? Qt : Qnil));
9009 specbind (Qinput_method_use_echo_area,
9010 (NILP (command_loop) ? Qt : Qnil));
9012 bzero (keybuf, sizeof keybuf);
9013 GCPRO1 (keybuf[0]);
9014 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
9016 if (NILP (continue_echo))
9018 this_command_key_count = 0;
9019 this_single_command_key_start = 0;
9022 #ifdef HAVE_X_WINDOWS
9023 if (display_hourglass_p)
9024 cancel_hourglass ();
9025 #endif
9027 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
9028 prompt, ! NILP (dont_downcase_last),
9029 ! NILP (can_return_switch_frame), 0);
9031 #ifdef HAVE_X_WINDOWS
9032 if (display_hourglass_p)
9033 start_hourglass ();
9034 #endif
9036 if (i == -1)
9038 Vquit_flag = Qt;
9039 QUIT;
9041 UNGCPRO;
9042 return unbind_to (count, Fvector (i, keybuf));
9045 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
9046 "Execute CMD as an editor command.\n\
9047 CMD must be a symbol that satisfies the `commandp' predicate.\n\
9048 Optional second arg RECORD-FLAG non-nil\n\
9049 means unconditionally put this command in `command-history'.\n\
9050 Otherwise, that is done only if an arg is read using the minibuffer.\n\
9051 The argument KEYS specifies the value to use instead of (this-command-keys)\n\
9052 when reading the arguments; if it is nil, (this-command-keys) is used.\n\
9053 The argument SPECIAL, if non-nil, means that this command is executing\n\
9054 a special event, so ignore the prefix argument and don't clear it.")
9055 (cmd, record_flag, keys, special)
9056 Lisp_Object cmd, record_flag, keys, special;
9058 register Lisp_Object final;
9059 register Lisp_Object tem;
9060 Lisp_Object prefixarg;
9061 struct backtrace backtrace;
9062 extern int debug_on_next_call;
9064 debug_on_next_call = 0;
9066 if (NILP (special))
9068 prefixarg = current_kboard->Vprefix_arg;
9069 Vcurrent_prefix_arg = prefixarg;
9070 current_kboard->Vprefix_arg = Qnil;
9072 else
9073 prefixarg = Qnil;
9075 if (SYMBOLP (cmd))
9077 tem = Fget (cmd, Qdisabled);
9078 if (!NILP (tem) && !NILP (Vrun_hooks))
9080 tem = Fsymbol_value (Qdisabled_command_hook);
9081 if (!NILP (tem))
9082 return call1 (Vrun_hooks, Qdisabled_command_hook);
9086 while (1)
9088 final = Findirect_function (cmd);
9090 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
9092 struct gcpro gcpro1, gcpro2;
9094 GCPRO2 (cmd, prefixarg);
9095 do_autoload (final, cmd);
9096 UNGCPRO;
9098 else
9099 break;
9102 if (STRINGP (final) || VECTORP (final))
9104 /* If requested, place the macro in the command history. For
9105 other sorts of commands, call-interactively takes care of
9106 this. */
9107 if (!NILP (record_flag))
9109 Vcommand_history
9110 = Fcons (Fcons (Qexecute_kbd_macro,
9111 Fcons (final, Fcons (prefixarg, Qnil))),
9112 Vcommand_history);
9114 /* Don't keep command history around forever. */
9115 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
9117 tem = Fnthcdr (Vhistory_length, Vcommand_history);
9118 if (CONSP (tem))
9119 XCDR (tem) = Qnil;
9123 return Fexecute_kbd_macro (final, prefixarg);
9126 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
9128 backtrace.next = backtrace_list;
9129 backtrace_list = &backtrace;
9130 backtrace.function = &Qcall_interactively;
9131 backtrace.args = &cmd;
9132 backtrace.nargs = 1;
9133 backtrace.evalargs = 0;
9135 tem = Fcall_interactively (cmd, record_flag, keys);
9137 backtrace_list = backtrace.next;
9138 return tem;
9140 return Qnil;
9145 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
9146 1, 1, "P",
9147 "Read function name, then read its arguments and call it.")
9148 (prefixarg)
9149 Lisp_Object prefixarg;
9151 Lisp_Object function;
9152 char buf[40];
9153 Lisp_Object saved_keys;
9154 Lisp_Object bindings, value;
9155 struct gcpro gcpro1, gcpro2;
9157 saved_keys = Fvector (this_command_key_count,
9158 XVECTOR (this_command_keys)->contents);
9159 buf[0] = 0;
9160 GCPRO2 (saved_keys, prefixarg);
9162 if (EQ (prefixarg, Qminus))
9163 strcpy (buf, "- ");
9164 else if (CONSP (prefixarg) && XINT (XCAR (prefixarg)) == 4)
9165 strcpy (buf, "C-u ");
9166 else if (CONSP (prefixarg) && INTEGERP (XCAR (prefixarg)))
9168 if (sizeof (int) == sizeof (EMACS_INT))
9169 sprintf (buf, "%d ", XINT (XCAR (prefixarg)));
9170 else if (sizeof (long) == sizeof (EMACS_INT))
9171 sprintf (buf, "%ld ", (long) XINT (XCAR (prefixarg)));
9172 else
9173 abort ();
9175 else if (INTEGERP (prefixarg))
9177 if (sizeof (int) == sizeof (EMACS_INT))
9178 sprintf (buf, "%d ", XINT (prefixarg));
9179 else if (sizeof (long) == sizeof (EMACS_INT))
9180 sprintf (buf, "%ld ", (long) XINT (prefixarg));
9181 else
9182 abort ();
9185 /* This isn't strictly correct if execute-extended-command
9186 is bound to anything else. Perhaps it should use
9187 this_command_keys? */
9188 strcat (buf, "M-x ");
9190 /* Prompt with buf, and then read a string, completing from and
9191 restricting to the set of all defined commands. Don't provide
9192 any initial input. Save the command read on the extended-command
9193 history list. */
9194 function = Fcompleting_read (build_string (buf),
9195 Vobarray, Qcommandp,
9196 Qt, Qnil, Qextended_command_history, Qnil,
9197 Qnil);
9199 if (STRINGP (function) && XSTRING (function)->size == 0)
9200 error ("No command name given");
9202 /* Set this_command_keys to the concatenation of saved_keys and
9203 function, followed by a RET. */
9205 struct Lisp_String *str;
9206 Lisp_Object *keys;
9207 int i;
9209 this_command_key_count = 0;
9210 this_single_command_key_start = 0;
9212 keys = XVECTOR (saved_keys)->contents;
9213 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
9214 add_command_key (keys[i]);
9216 str = XSTRING (function);
9217 for (i = 0; i < str->size; i++)
9218 add_command_key (Faref (function, make_number (i)));
9220 add_command_key (make_number ('\015'));
9223 UNGCPRO;
9225 function = Fintern (function, Qnil);
9226 current_kboard->Vprefix_arg = prefixarg;
9227 Vthis_command = function;
9228 real_this_command = function;
9230 /* If enabled, show which key runs this command. */
9231 if (!NILP (Vsuggest_key_bindings)
9232 && NILP (Vexecuting_macro)
9233 && SYMBOLP (function))
9234 bindings = Fwhere_is_internal (function, Voverriding_local_map,
9235 Qt, Qnil);
9236 else
9237 bindings = Qnil;
9239 value = Qnil;
9240 GCPRO2 (bindings, value);
9241 value = Fcommand_execute (function, Qt, Qnil, Qnil);
9243 /* If the command has a key binding, print it now. */
9244 if (!NILP (bindings)
9245 && ! (VECTORP (bindings) && EQ (Faref (bindings, make_number (0)),
9246 Qmouse_movement)))
9248 /* But first wait, and skip the message if there is input. */
9249 int delay_time;
9250 if (!NILP (echo_area_buffer[0]))
9251 /* This command displayed something in the echo area;
9252 so wait a few seconds, then display our suggestion message. */
9253 delay_time = (NUMBERP (Vsuggest_key_bindings)
9254 ? XINT (Vsuggest_key_bindings) : 2);
9255 else
9256 /* This command left the echo area empty,
9257 so display our message immediately. */
9258 delay_time = 0;
9260 if (!NILP (Fsit_for (make_number (delay_time), Qnil, Qnil))
9261 && ! CONSP (Vunread_command_events))
9263 Lisp_Object binding;
9264 char *newmessage;
9265 int message_p = push_message ();
9266 int count = BINDING_STACK_SIZE ();
9268 record_unwind_protect (push_message_unwind, Qnil);
9269 binding = Fkey_description (bindings);
9271 newmessage
9272 = (char *) alloca (XSYMBOL (function)->name->size
9273 + STRING_BYTES (XSTRING (binding))
9274 + 100);
9275 sprintf (newmessage, "You can run the command `%s' with %s",
9276 XSYMBOL (function)->name->data,
9277 XSTRING (binding)->data);
9278 message2_nolog (newmessage,
9279 strlen (newmessage),
9280 STRING_MULTIBYTE (binding));
9281 if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
9282 ? Vsuggest_key_bindings : make_number (2)),
9283 Qnil, Qnil))
9284 && message_p)
9285 restore_message ();
9287 unbind_to (count, Qnil);
9291 RETURN_UNGCPRO (value);
9294 /* Find the set of keymaps now active.
9295 Store into *MAPS_P a vector holding the various maps
9296 and return the number of them. The vector was malloc'd
9297 and the caller should free it. */
9300 current_active_maps (maps_p)
9301 Lisp_Object **maps_p;
9303 Lisp_Object *tmaps, *maps;
9304 int nmaps;
9306 /* Should overriding-terminal-local-map and overriding-local-map apply? */
9307 if (!NILP (Voverriding_local_map_menu_flag))
9309 /* Yes, use them (if non-nil) as well as the global map. */
9310 maps = (Lisp_Object *) xmalloc (3 * sizeof (maps[0]));
9311 nmaps = 0;
9312 if (!NILP (current_kboard->Voverriding_terminal_local_map))
9313 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
9314 if (!NILP (Voverriding_local_map))
9315 maps[nmaps++] = Voverriding_local_map;
9317 else
9319 /* No, so use major and minor mode keymaps and keymap property. */
9320 int extra_maps = 2;
9321 Lisp_Object map = get_local_map (PT, current_buffer, Qkeymap);
9322 if (!NILP (map))
9323 extra_maps = 3;
9324 nmaps = current_minor_maps (NULL, &tmaps);
9325 maps = (Lisp_Object *) alloca ((nmaps + extra_maps)
9326 * sizeof (maps[0]));
9327 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
9328 if (!NILP (map))
9329 maps[nmaps++] = map;
9330 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
9332 maps[nmaps++] = current_global_map;
9334 *maps_p = maps;
9335 return nmaps;
9338 /* Return nonzero if input events are pending. */
9341 detect_input_pending ()
9343 if (!input_pending)
9344 get_input_pending (&input_pending, 0);
9346 return input_pending;
9349 /* Return nonzero if input events are pending, and run any pending timers. */
9352 detect_input_pending_run_timers (do_display)
9353 int do_display;
9355 int old_timers_run = timers_run;
9357 if (!input_pending)
9358 get_input_pending (&input_pending, 1);
9360 if (old_timers_run != timers_run && do_display)
9362 redisplay_preserve_echo_area (8);
9363 /* The following fixes a bug when using lazy-lock with
9364 lazy-lock-defer-on-the-fly set to t, i.e. when fontifying
9365 from an idle timer function. The symptom of the bug is that
9366 the cursor sometimes doesn't become visible until the next X
9367 event is processed. --gerd. */
9368 if (rif)
9369 rif->flush_display (NULL);
9372 return input_pending;
9375 /* This is called in some cases before a possible quit.
9376 It cases the next call to detect_input_pending to recompute input_pending.
9377 So calling this function unnecessarily can't do any harm. */
9379 void
9380 clear_input_pending ()
9382 input_pending = 0;
9385 /* Return nonzero if there are pending requeued events.
9386 This isn't used yet. The hope is to make wait_reading_process_input
9387 call it, and return return if it runs Lisp code that unreads something.
9388 The problem is, kbd_buffer_get_event needs to be fixed to know what
9389 to do in that case. It isn't trivial. */
9392 requeued_events_pending_p ()
9394 return (!NILP (Vunread_command_events) || unread_command_char != -1);
9398 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
9399 "T if command input is currently available with no waiting.\n\
9400 Actually, the value is nil only if we can be sure that no input is available.")
9403 if (!NILP (Vunread_command_events) || unread_command_char != -1)
9404 return (Qt);
9406 get_input_pending (&input_pending, 1);
9407 return input_pending > 0 ? Qt : Qnil;
9410 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
9411 "Return vector of last 100 events, not counting those from keyboard macros.")
9414 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
9415 Lisp_Object val;
9417 if (total_keys < NUM_RECENT_KEYS)
9418 return Fvector (total_keys, keys);
9419 else
9421 val = Fvector (NUM_RECENT_KEYS, keys);
9422 bcopy (keys + recent_keys_index,
9423 XVECTOR (val)->contents,
9424 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
9425 bcopy (keys,
9426 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
9427 recent_keys_index * sizeof (Lisp_Object));
9428 return val;
9432 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
9433 "Return the key sequence that invoked this command.\n\
9434 The value is a string or a vector.")
9437 return make_event_array (this_command_key_count,
9438 XVECTOR (this_command_keys)->contents);
9441 DEFUN ("this-command-keys-vector", Fthis_command_keys_vector, Sthis_command_keys_vector, 0, 0, 0,
9442 "Return the key sequence that invoked this command, as a vector.")
9445 return Fvector (this_command_key_count,
9446 XVECTOR (this_command_keys)->contents);
9449 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
9450 Sthis_single_command_keys, 0, 0, 0,
9451 "Return the key sequence that invoked this command.\n\
9452 Unlike `this-command-keys', this function's value\n\
9453 does not include prefix arguments.\n\
9454 The value is always a vector.")
9457 return Fvector (this_command_key_count
9458 - this_single_command_key_start,
9459 (XVECTOR (this_command_keys)->contents
9460 + this_single_command_key_start));
9463 DEFUN ("this-single-command-raw-keys", Fthis_single_command_raw_keys,
9464 Sthis_single_command_raw_keys, 0, 0, 0,
9465 "Return the raw events that were read for this command.\n\
9466 Unlike `this-single-command-keys', this function's value\n\
9467 shows the events before all translations (except for input methods).\n\
9468 The value is always a vector.")
9471 return Fvector (raw_keybuf_count,
9472 (XVECTOR (raw_keybuf)->contents));
9475 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
9476 Sreset_this_command_lengths, 0, 0, 0,
9477 "Used for complicated reasons in `universal-argument-other-key'.\n\
9479 `universal-argument-other-key' rereads the event just typed.\n\
9480 It then gets translated through `function-key-map'.\n\
9481 The translated event gets included in the echo area and in\n\
9482 the value of `this-command-keys' in addition to the raw original event.\n\
9483 That is not right.\n\
9485 Calling this function directs the translated event to replace\n\
9486 the original event, so that only one version of the event actually\n\
9487 appears in the echo area and in the value of `this-command-keys.'.")
9490 before_command_restore_flag = 1;
9491 before_command_key_count_1 = before_command_key_count;
9492 before_command_echo_length_1 = before_command_echo_length;
9493 return Qnil;
9496 DEFUN ("clear-this-command-keys", Fclear_this_command_keys,
9497 Sclear_this_command_keys, 0, 0, 0,
9498 "Clear out the vector that `this-command-keys' returns.\n\
9499 Clear vector containing last 100 events.")
9502 int i;
9504 this_command_key_count = 0;
9506 for (i = 0; i < XVECTOR (recent_keys)->size; ++i)
9507 XVECTOR (recent_keys)->contents[i] = Qnil;
9508 total_keys = 0;
9509 recent_keys_index = 0;
9510 return Qnil;
9513 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
9514 "Return the current depth in recursive edits.")
9517 Lisp_Object temp;
9518 XSETFASTINT (temp, command_loop_level + minibuf_level);
9519 return temp;
9522 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
9523 "FOpen dribble file: ",
9524 "Start writing all keyboard characters to a dribble file called FILE.\n\
9525 If FILE is nil, close any open dribble file.")
9526 (file)
9527 Lisp_Object file;
9529 if (dribble)
9531 fclose (dribble);
9532 dribble = 0;
9534 if (!NILP (file))
9536 file = Fexpand_file_name (file, Qnil);
9537 dribble = fopen (XSTRING (file)->data, "w");
9538 if (dribble == 0)
9539 report_file_error ("Opening dribble", Fcons (file, Qnil));
9541 return Qnil;
9544 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
9545 "Discard the contents of the terminal input buffer.\n\
9546 Also cancel any kbd macro being defined.")
9549 current_kboard->defining_kbd_macro = Qnil;
9550 update_mode_lines++;
9552 Vunread_command_events = Qnil;
9553 unread_command_char = -1;
9555 discard_tty_input ();
9557 kbd_fetch_ptr = kbd_store_ptr;
9558 Ffillarray (kbd_buffer_gcpro, Qnil);
9559 input_pending = 0;
9561 return Qnil;
9564 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
9565 "Stop Emacs and return to superior process. You can resume later.\n\
9566 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
9567 control, run a subshell instead.\n\n\
9568 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
9569 to be read as terminal input by Emacs's parent, after suspension.\n\
9571 Before suspending, run the normal hook `suspend-hook'.\n\
9572 After resumption run the normal hook `suspend-resume-hook'.\n\
9574 Some operating systems cannot stop the Emacs process and resume it later.\n\
9575 On such systems, Emacs starts a subshell instead of suspending.")
9576 (stuffstring)
9577 Lisp_Object stuffstring;
9579 int count = specpdl_ptr - specpdl;
9580 int old_height, old_width;
9581 int width, height;
9582 struct gcpro gcpro1;
9584 if (!NILP (stuffstring))
9585 CHECK_STRING (stuffstring, 0);
9587 /* Run the functions in suspend-hook. */
9588 if (!NILP (Vrun_hooks))
9589 call1 (Vrun_hooks, intern ("suspend-hook"));
9591 GCPRO1 (stuffstring);
9592 get_frame_size (&old_width, &old_height);
9593 reset_sys_modes ();
9594 /* sys_suspend can get an error if it tries to fork a subshell
9595 and the system resources aren't available for that. */
9596 record_unwind_protect ((Lisp_Object (*) P_ ((Lisp_Object))) init_sys_modes,
9597 Qnil);
9598 stuff_buffered_input (stuffstring);
9599 if (cannot_suspend)
9600 sys_subshell ();
9601 else
9602 sys_suspend ();
9603 unbind_to (count, Qnil);
9605 /* Check if terminal/window size has changed.
9606 Note that this is not useful when we are running directly
9607 with a window system; but suspend should be disabled in that case. */
9608 get_frame_size (&width, &height);
9609 if (width != old_width || height != old_height)
9610 change_frame_size (SELECTED_FRAME (), height, width, 0, 0, 0);
9612 /* Run suspend-resume-hook. */
9613 if (!NILP (Vrun_hooks))
9614 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
9616 UNGCPRO;
9617 return Qnil;
9620 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
9621 Then in any case stuff anything Emacs has read ahead and not used. */
9623 void
9624 stuff_buffered_input (stuffstring)
9625 Lisp_Object stuffstring;
9627 /* stuff_char works only in BSD, versions 4.2 and up. */
9628 #ifdef BSD_SYSTEM
9629 #ifndef BSD4_1
9630 register unsigned char *p;
9632 if (STRINGP (stuffstring))
9634 register int count;
9636 p = XSTRING (stuffstring)->data;
9637 count = STRING_BYTES (XSTRING (stuffstring));
9638 while (count-- > 0)
9639 stuff_char (*p++);
9640 stuff_char ('\n');
9643 /* Anything we have read ahead, put back for the shell to read. */
9644 /* ?? What should this do when we have multiple keyboards??
9645 Should we ignore anything that was typed in at the "wrong" kboard? */
9646 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
9648 int idx;
9650 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
9651 kbd_fetch_ptr = kbd_buffer;
9652 if (kbd_fetch_ptr->kind == ascii_keystroke)
9653 stuff_char (kbd_fetch_ptr->code);
9655 kbd_fetch_ptr->kind = no_event;
9656 idx = 2 * (kbd_fetch_ptr - kbd_buffer);
9657 ASET (kbd_buffer_gcpro, idx, Qnil);
9658 ASET (kbd_buffer_gcpro, idx + 1, Qnil);
9661 input_pending = 0;
9662 #endif
9663 #endif /* BSD_SYSTEM and not BSD4_1 */
9666 void
9667 set_waiting_for_input (time_to_clear)
9668 EMACS_TIME *time_to_clear;
9670 input_available_clear_time = time_to_clear;
9672 /* Tell interrupt_signal to throw back to read_char, */
9673 waiting_for_input = 1;
9675 /* If interrupt_signal was called before and buffered a C-g,
9676 make it run again now, to avoid timing error. */
9677 if (!NILP (Vquit_flag))
9678 quit_throw_to_read_char ();
9681 void
9682 clear_waiting_for_input ()
9684 /* Tell interrupt_signal not to throw back to read_char, */
9685 waiting_for_input = 0;
9686 input_available_clear_time = 0;
9689 /* This routine is called at interrupt level in response to C-G.
9691 If interrupt_input, this is the handler for SIGINT. Otherwise, it
9692 is called from kbd_buffer_store_event, in handling SIGIO or
9693 SIGTINT.
9695 If `waiting_for_input' is non zero, then unless `echoing' is
9696 nonzero, immediately throw back to read_char.
9698 Otherwise it sets the Lisp variable quit-flag not-nil. This causes
9699 eval to throw, when it gets a chance. If quit-flag is already
9700 non-nil, it stops the job right away. */
9702 SIGTYPE
9703 interrupt_signal (signalnum) /* If we don't have an argument, */
9704 int signalnum; /* some compilers complain in signal calls. */
9706 char c;
9707 /* Must preserve main program's value of errno. */
9708 int old_errno = errno;
9709 struct frame *sf = SELECTED_FRAME ();
9711 #if defined (USG) && !defined (POSIX_SIGNALS)
9712 if (!read_socket_hook && NILP (Vwindow_system))
9714 /* USG systems forget handlers when they are used;
9715 must reestablish each time */
9716 signal (SIGINT, interrupt_signal);
9717 signal (SIGQUIT, interrupt_signal);
9719 #endif /* USG */
9721 cancel_echoing ();
9723 if (!NILP (Vquit_flag)
9724 && (FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf)))
9726 /* If SIGINT isn't blocked, don't let us be interrupted by
9727 another SIGINT, it might be harmful due to non-reentrancy
9728 in I/O functions. */
9729 sigblock (sigmask (SIGINT));
9731 fflush (stdout);
9732 reset_sys_modes ();
9734 #ifdef SIGTSTP /* Support possible in later USG versions */
9736 * On systems which can suspend the current process and return to the original
9737 * shell, this command causes the user to end up back at the shell.
9738 * The "Auto-save" and "Abort" questions are not asked until
9739 * the user elects to return to emacs, at which point he can save the current
9740 * job and either dump core or continue.
9742 sys_suspend ();
9743 #else
9744 #ifdef VMS
9745 if (sys_suspend () == -1)
9747 printf ("Not running as a subprocess;\n");
9748 printf ("you can continue or abort.\n");
9750 #else /* not VMS */
9751 /* Perhaps should really fork an inferior shell?
9752 But that would not provide any way to get back
9753 to the original shell, ever. */
9754 printf ("No support for stopping a process on this operating system;\n");
9755 printf ("you can continue or abort.\n");
9756 #endif /* not VMS */
9757 #endif /* not SIGTSTP */
9758 #ifdef MSDOS
9759 /* We must remain inside the screen area when the internal terminal
9760 is used. Note that [Enter] is not echoed by dos. */
9761 cursor_to (0, 0);
9762 #endif
9763 /* It doesn't work to autosave while GC is in progress;
9764 the code used for auto-saving doesn't cope with the mark bit. */
9765 if (!gc_in_progress)
9767 printf ("Auto-save? (y or n) ");
9768 fflush (stdout);
9769 if (((c = getchar ()) & ~040) == 'Y')
9771 Fdo_auto_save (Qt, Qnil);
9772 #ifdef MSDOS
9773 printf ("\r\nAuto-save done");
9774 #else /* not MSDOS */
9775 printf ("Auto-save done\n");
9776 #endif /* not MSDOS */
9778 while (c != '\n') c = getchar ();
9780 else
9782 /* During GC, it must be safe to reenable quitting again. */
9783 Vinhibit_quit = Qnil;
9784 #ifdef MSDOS
9785 printf ("\r\n");
9786 #endif /* not MSDOS */
9787 printf ("Garbage collection in progress; cannot auto-save now\r\n");
9788 printf ("but will instead do a real quit after garbage collection ends\r\n");
9789 fflush (stdout);
9792 #ifdef MSDOS
9793 printf ("\r\nAbort? (y or n) ");
9794 #else /* not MSDOS */
9795 #ifdef VMS
9796 printf ("Abort (and enter debugger)? (y or n) ");
9797 #else /* not VMS */
9798 printf ("Abort (and dump core)? (y or n) ");
9799 #endif /* not VMS */
9800 #endif /* not MSDOS */
9801 fflush (stdout);
9802 if (((c = getchar ()) & ~040) == 'Y')
9803 abort ();
9804 while (c != '\n') c = getchar ();
9805 #ifdef MSDOS
9806 printf ("\r\nContinuing...\r\n");
9807 #else /* not MSDOS */
9808 printf ("Continuing...\n");
9809 #endif /* not MSDOS */
9810 fflush (stdout);
9811 init_sys_modes ();
9812 sigfree ();
9814 else
9816 /* If executing a function that wants to be interrupted out of
9817 and the user has not deferred quitting by binding `inhibit-quit'
9818 then quit right away. */
9819 if (immediate_quit && NILP (Vinhibit_quit))
9821 struct gl_state_s saved;
9822 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9824 immediate_quit = 0;
9825 sigfree ();
9826 saved = gl_state;
9827 GCPRO4 (saved.object, saved.global_code,
9828 saved.current_syntax_table, saved.old_prop);
9829 Fsignal (Qquit, Qnil);
9830 gl_state = saved;
9831 UNGCPRO;
9833 else
9834 /* Else request quit when it's safe */
9835 Vquit_flag = Qt;
9838 if (waiting_for_input && !echoing)
9839 quit_throw_to_read_char ();
9841 errno = old_errno;
9844 /* Handle a C-g by making read_char return C-g. */
9846 void
9847 quit_throw_to_read_char ()
9849 sigfree ();
9850 /* Prevent another signal from doing this before we finish. */
9851 clear_waiting_for_input ();
9852 input_pending = 0;
9854 Vunread_command_events = Qnil;
9855 unread_command_char = -1;
9857 #if 0 /* Currently, sit_for is called from read_char without turning
9858 off polling. And that can call set_waiting_for_input.
9859 It seems to be harmless. */
9860 #ifdef POLL_FOR_INPUT
9861 /* May be > 1 if in recursive minibuffer. */
9862 if (poll_suppress_count == 0)
9863 abort ();
9864 #endif
9865 #endif
9866 if (FRAMEP (internal_last_event_frame)
9867 && !EQ (internal_last_event_frame, selected_frame))
9868 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
9869 Qnil, 0);
9871 _longjmp (getcjmp, 1);
9874 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
9875 "Set mode of reading keyboard input.\n\
9876 First arg INTERRUPT non-nil means use input interrupts;\n\
9877 nil means use CBREAK mode.\n\
9878 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
9879 (no effect except in CBREAK mode).\n\
9880 Third arg META t means accept 8-bit input (for a Meta key).\n\
9881 META nil means ignore the top bit, on the assumption it is parity.\n\
9882 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
9883 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
9884 See also `current-input-mode'.")
9885 (interrupt, flow, meta, quit)
9886 Lisp_Object interrupt, flow, meta, quit;
9888 if (!NILP (quit)
9889 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
9890 error ("set-input-mode: QUIT must be an ASCII character");
9892 #ifdef POLL_FOR_INPUT
9893 stop_polling ();
9894 #endif
9896 #ifndef DOS_NT
9897 /* this causes startup screen to be restored and messes with the mouse */
9898 reset_sys_modes ();
9899 #endif
9901 #ifdef SIGIO
9902 /* Note SIGIO has been undef'd if FIONREAD is missing. */
9903 if (read_socket_hook)
9905 /* When using X, don't give the user a real choice,
9906 because we haven't implemented the mechanisms to support it. */
9907 #ifdef NO_SOCK_SIGIO
9908 interrupt_input = 0;
9909 #else /* not NO_SOCK_SIGIO */
9910 interrupt_input = 1;
9911 #endif /* NO_SOCK_SIGIO */
9913 else
9914 interrupt_input = !NILP (interrupt);
9915 #else /* not SIGIO */
9916 interrupt_input = 0;
9917 #endif /* not SIGIO */
9919 /* Our VMS input only works by interrupts, as of now. */
9920 #ifdef VMS
9921 interrupt_input = 1;
9922 #endif
9924 flow_control = !NILP (flow);
9925 if (NILP (meta))
9926 meta_key = 0;
9927 else if (EQ (meta, Qt))
9928 meta_key = 1;
9929 else
9930 meta_key = 2;
9931 if (!NILP (quit))
9932 /* Don't let this value be out of range. */
9933 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
9935 #ifndef DOS_NT
9936 init_sys_modes ();
9937 #endif
9939 #ifdef POLL_FOR_INPUT
9940 poll_suppress_count = 1;
9941 start_polling ();
9942 #endif
9943 return Qnil;
9946 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
9947 "Return information about the way Emacs currently reads keyboard input.\n\
9948 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
9949 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
9950 nil, Emacs is using CBREAK mode.\n\
9951 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
9952 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
9953 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
9954 META nil means ignoring the top bit, on the assumption it is parity.\n\
9955 META is neither t nor nil if accepting 8-bit input and using\n\
9956 all 8 bits as the character code.\n\
9957 QUIT is the character Emacs currently uses to quit.\n\
9958 The elements of this list correspond to the arguments of\n\
9959 `set-input-mode'.")
9962 Lisp_Object val[4];
9964 val[0] = interrupt_input ? Qt : Qnil;
9965 val[1] = flow_control ? Qt : Qnil;
9966 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
9967 XSETFASTINT (val[3], quit_char);
9969 return Flist (sizeof (val) / sizeof (val[0]), val);
9974 * Set up a new kboard object with reasonable initial values.
9976 void
9977 init_kboard (kb)
9978 KBOARD *kb;
9980 kb->Voverriding_terminal_local_map = Qnil;
9981 kb->Vlast_command = Qnil;
9982 kb->Vreal_last_command = Qnil;
9983 kb->Vprefix_arg = Qnil;
9984 kb->Vlast_prefix_arg = Qnil;
9985 kb->kbd_queue = Qnil;
9986 kb->kbd_queue_has_data = 0;
9987 kb->immediate_echo = 0;
9988 kb->echoptr = kb->echobuf;
9989 kb->echo_after_prompt = -1;
9990 kb->kbd_macro_buffer = 0;
9991 kb->kbd_macro_bufsize = 0;
9992 kb->defining_kbd_macro = Qnil;
9993 kb->Vlast_kbd_macro = Qnil;
9994 kb->reference_count = 0;
9995 kb->Vsystem_key_alist = Qnil;
9996 kb->system_key_syms = Qnil;
9997 kb->Vdefault_minibuffer_frame = Qnil;
10001 * Destroy the contents of a kboard object, but not the object itself.
10002 * We use this just before deleting it, or if we're going to initialize
10003 * it a second time.
10005 static void
10006 wipe_kboard (kb)
10007 KBOARD *kb;
10009 if (kb->kbd_macro_buffer)
10010 xfree (kb->kbd_macro_buffer);
10013 #ifdef MULTI_KBOARD
10014 void
10015 delete_kboard (kb)
10016 KBOARD *kb;
10018 KBOARD **kbp;
10019 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
10020 if (*kbp == NULL)
10021 abort ();
10022 *kbp = kb->next_kboard;
10023 wipe_kboard (kb);
10024 xfree (kb);
10026 #endif
10028 void
10029 init_keyboard ()
10031 /* This is correct before outermost invocation of the editor loop */
10032 command_loop_level = -1;
10033 immediate_quit = 0;
10034 quit_char = Ctl ('g');
10035 Vunread_command_events = Qnil;
10036 unread_command_char = -1;
10037 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
10038 total_keys = 0;
10039 recent_keys_index = 0;
10040 kbd_fetch_ptr = kbd_buffer;
10041 kbd_store_ptr = kbd_buffer;
10042 kbd_buffer_gcpro = Fmake_vector (make_number (2 * KBD_BUFFER_SIZE), Qnil);
10043 #ifdef HAVE_MOUSE
10044 do_mouse_tracking = Qnil;
10045 #endif
10046 input_pending = 0;
10048 /* This means that command_loop_1 won't try to select anything the first
10049 time through. */
10050 internal_last_event_frame = Qnil;
10051 Vlast_event_frame = internal_last_event_frame;
10053 #ifdef MULTI_KBOARD
10054 current_kboard = initial_kboard;
10055 #endif
10056 wipe_kboard (current_kboard);
10057 init_kboard (current_kboard);
10059 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
10061 signal (SIGINT, interrupt_signal);
10062 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
10063 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
10064 SIGQUIT and we can't tell which one it will give us. */
10065 signal (SIGQUIT, interrupt_signal);
10066 #endif /* HAVE_TERMIO */
10068 /* Note SIGIO has been undef'd if FIONREAD is missing. */
10069 #ifdef SIGIO
10070 if (!noninteractive)
10071 signal (SIGIO, input_available_signal);
10072 #endif /* SIGIO */
10074 /* Use interrupt input by default, if it works and noninterrupt input
10075 has deficiencies. */
10077 #ifdef INTERRUPT_INPUT
10078 interrupt_input = 1;
10079 #else
10080 interrupt_input = 0;
10081 #endif
10083 /* Our VMS input only works by interrupts, as of now. */
10084 #ifdef VMS
10085 interrupt_input = 1;
10086 #endif
10088 sigfree ();
10089 dribble = 0;
10091 if (keyboard_init_hook)
10092 (*keyboard_init_hook) ();
10094 #ifdef POLL_FOR_INPUT
10095 poll_suppress_count = 1;
10096 start_polling ();
10097 #endif
10100 /* This type's only use is in syms_of_keyboard, to initialize the
10101 event header symbols and put properties on them. */
10102 struct event_head {
10103 Lisp_Object *var;
10104 char *name;
10105 Lisp_Object *kind;
10108 struct event_head head_table[] = {
10109 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
10110 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
10111 &Qswitch_frame, "switch-frame", &Qswitch_frame,
10112 &Qdelete_frame, "delete-frame", &Qdelete_frame,
10113 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
10114 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
10117 void
10118 syms_of_keyboard ()
10120 Vlispy_mouse_stem = build_string ("mouse");
10121 staticpro (&Vlispy_mouse_stem);
10123 /* Tool-bars. */
10124 QCimage = intern (":image");
10125 staticpro (&QCimage);
10127 staticpro (&Qhelp_echo);
10128 Qhelp_echo = intern ("help-echo");
10130 staticpro (&item_properties);
10131 item_properties = Qnil;
10133 staticpro (&tool_bar_item_properties);
10134 tool_bar_item_properties = Qnil;
10135 staticpro (&tool_bar_items_vector);
10136 tool_bar_items_vector = Qnil;
10138 staticpro (&real_this_command);
10139 real_this_command = Qnil;
10141 Qtimer_event_handler = intern ("timer-event-handler");
10142 staticpro (&Qtimer_event_handler);
10144 Qdisabled_command_hook = intern ("disabled-command-hook");
10145 staticpro (&Qdisabled_command_hook);
10147 Qself_insert_command = intern ("self-insert-command");
10148 staticpro (&Qself_insert_command);
10150 Qforward_char = intern ("forward-char");
10151 staticpro (&Qforward_char);
10153 Qbackward_char = intern ("backward-char");
10154 staticpro (&Qbackward_char);
10156 Qdisabled = intern ("disabled");
10157 staticpro (&Qdisabled);
10159 Qundefined = intern ("undefined");
10160 staticpro (&Qundefined);
10162 Qpre_command_hook = intern ("pre-command-hook");
10163 staticpro (&Qpre_command_hook);
10165 Qpost_command_hook = intern ("post-command-hook");
10166 staticpro (&Qpost_command_hook);
10168 Qpost_command_idle_hook = intern ("post-command-idle-hook");
10169 staticpro (&Qpost_command_idle_hook);
10171 Qdeferred_action_function = intern ("deferred-action-function");
10172 staticpro (&Qdeferred_action_function);
10174 Qcommand_hook_internal = intern ("command-hook-internal");
10175 staticpro (&Qcommand_hook_internal);
10177 Qfunction_key = intern ("function-key");
10178 staticpro (&Qfunction_key);
10179 Qmouse_click = intern ("mouse-click");
10180 staticpro (&Qmouse_click);
10181 #ifdef WINDOWSNT
10182 Qmouse_wheel = intern ("mouse-wheel");
10183 staticpro (&Qmouse_wheel);
10184 Qlanguage_change = intern ("language-change");
10185 staticpro (&Qlanguage_change);
10186 #endif
10187 Qdrag_n_drop = intern ("drag-n-drop");
10188 staticpro (&Qdrag_n_drop);
10190 Qusr1_signal = intern ("usr1-signal");
10191 staticpro (&Qusr1_signal);
10192 Qusr2_signal = intern ("usr2-signal");
10193 staticpro (&Qusr2_signal);
10195 Qmenu_enable = intern ("menu-enable");
10196 staticpro (&Qmenu_enable);
10197 Qmenu_alias = intern ("menu-alias");
10198 staticpro (&Qmenu_alias);
10199 QCenable = intern (":enable");
10200 staticpro (&QCenable);
10201 QCvisible = intern (":visible");
10202 staticpro (&QCvisible);
10203 QChelp = intern (":help");
10204 staticpro (&QChelp);
10205 QCfilter = intern (":filter");
10206 staticpro (&QCfilter);
10207 QCbutton = intern (":button");
10208 staticpro (&QCbutton);
10209 QCkeys = intern (":keys");
10210 staticpro (&QCkeys);
10211 QCkey_sequence = intern (":key-sequence");
10212 staticpro (&QCkey_sequence);
10213 QCtoggle = intern (":toggle");
10214 staticpro (&QCtoggle);
10215 QCradio = intern (":radio");
10216 staticpro (&QCradio);
10218 Qmode_line = intern ("mode-line");
10219 staticpro (&Qmode_line);
10220 Qvertical_line = intern ("vertical-line");
10221 staticpro (&Qvertical_line);
10222 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
10223 staticpro (&Qvertical_scroll_bar);
10224 Qmenu_bar = intern ("menu-bar");
10225 staticpro (&Qmenu_bar);
10227 Qabove_handle = intern ("above-handle");
10228 staticpro (&Qabove_handle);
10229 Qhandle = intern ("handle");
10230 staticpro (&Qhandle);
10231 Qbelow_handle = intern ("below-handle");
10232 staticpro (&Qbelow_handle);
10233 Qup = intern ("up");
10234 staticpro (&Qup);
10235 Qdown = intern ("down");
10236 staticpro (&Qdown);
10237 Qtop = intern ("top");
10238 staticpro (&Qtop);
10239 Qbottom = intern ("bottom");
10240 staticpro (&Qbottom);
10241 Qend_scroll = intern ("end-scroll");
10242 staticpro (&Qend_scroll);
10243 Qratio = intern ("ratio");
10244 staticpro (&Qratio);
10246 Qevent_kind = intern ("event-kind");
10247 staticpro (&Qevent_kind);
10248 Qevent_symbol_elements = intern ("event-symbol-elements");
10249 staticpro (&Qevent_symbol_elements);
10250 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
10251 staticpro (&Qevent_symbol_element_mask);
10252 Qmodifier_cache = intern ("modifier-cache");
10253 staticpro (&Qmodifier_cache);
10255 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
10256 staticpro (&Qrecompute_lucid_menubar);
10257 Qactivate_menubar_hook = intern ("activate-menubar-hook");
10258 staticpro (&Qactivate_menubar_hook);
10260 Qpolling_period = intern ("polling-period");
10261 staticpro (&Qpolling_period);
10263 Qinput_method_function = intern ("input-method-function");
10264 staticpro (&Qinput_method_function);
10266 Qinput_method_exit_on_first_char = intern ("input-method-exit-on-first-char");
10267 staticpro (&Qinput_method_exit_on_first_char);
10268 Qinput_method_use_echo_area = intern ("input-method-use-echo-area");
10269 staticpro (&Qinput_method_use_echo_area);
10271 Fset (Qinput_method_exit_on_first_char, Qnil);
10272 Fset (Qinput_method_use_echo_area, Qnil);
10274 last_point_position_buffer = Qnil;
10277 struct event_head *p;
10279 for (p = head_table;
10280 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
10281 p++)
10283 *p->var = intern (p->name);
10284 staticpro (p->var);
10285 Fput (*p->var, Qevent_kind, *p->kind);
10286 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
10290 button_down_location = Fmake_vector (make_number (1), Qnil);
10291 staticpro (&button_down_location);
10292 mouse_syms = Fmake_vector (make_number (1), Qnil);
10293 staticpro (&mouse_syms);
10296 int i;
10297 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
10299 modifier_symbols = Fmake_vector (make_number (len), Qnil);
10300 for (i = 0; i < len; i++)
10301 if (modifier_names[i])
10302 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
10303 staticpro (&modifier_symbols);
10306 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
10307 staticpro (&recent_keys);
10309 this_command_keys = Fmake_vector (make_number (40), Qnil);
10310 staticpro (&this_command_keys);
10312 raw_keybuf = Fmake_vector (make_number (30), Qnil);
10313 staticpro (&raw_keybuf);
10315 Qextended_command_history = intern ("extended-command-history");
10316 Fset (Qextended_command_history, Qnil);
10317 staticpro (&Qextended_command_history);
10319 kbd_buffer_gcpro = Fmake_vector (make_number (2 * KBD_BUFFER_SIZE), Qnil);
10320 staticpro (&kbd_buffer_gcpro);
10322 accent_key_syms = Qnil;
10323 staticpro (&accent_key_syms);
10325 func_key_syms = Qnil;
10326 staticpro (&func_key_syms);
10328 #ifdef WINDOWSNT
10329 mouse_wheel_syms = Qnil;
10330 staticpro (&mouse_wheel_syms);
10332 drag_n_drop_syms = Qnil;
10333 staticpro (&drag_n_drop_syms);
10334 #endif
10336 unread_switch_frame = Qnil;
10337 staticpro (&unread_switch_frame);
10339 internal_last_event_frame = Qnil;
10340 staticpro (&internal_last_event_frame);
10342 read_key_sequence_cmd = Qnil;
10343 staticpro (&read_key_sequence_cmd);
10345 menu_bar_one_keymap_changed_items = Qnil;
10346 staticpro (&menu_bar_one_keymap_changed_items);
10348 defsubr (&Sevent_convert_list);
10349 defsubr (&Sread_key_sequence);
10350 defsubr (&Sread_key_sequence_vector);
10351 defsubr (&Srecursive_edit);
10352 #ifdef HAVE_MOUSE
10353 defsubr (&Strack_mouse);
10354 #endif
10355 defsubr (&Sinput_pending_p);
10356 defsubr (&Scommand_execute);
10357 defsubr (&Srecent_keys);
10358 defsubr (&Sthis_command_keys);
10359 defsubr (&Sthis_command_keys_vector);
10360 defsubr (&Sthis_single_command_keys);
10361 defsubr (&Sthis_single_command_raw_keys);
10362 defsubr (&Sreset_this_command_lengths);
10363 defsubr (&Sclear_this_command_keys);
10364 defsubr (&Ssuspend_emacs);
10365 defsubr (&Sabort_recursive_edit);
10366 defsubr (&Sexit_recursive_edit);
10367 defsubr (&Srecursion_depth);
10368 defsubr (&Stop_level);
10369 defsubr (&Sdiscard_input);
10370 defsubr (&Sopen_dribble_file);
10371 defsubr (&Sset_input_mode);
10372 defsubr (&Scurrent_input_mode);
10373 defsubr (&Sexecute_extended_command);
10375 DEFVAR_LISP ("last-command-char", &last_command_char,
10376 "Last input event that was part of a command.");
10378 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
10379 "Last input event that was part of a command.");
10381 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
10382 "Last input event in a command, except for mouse menu events.\n\
10383 Mouse menus give back keys that don't look like mouse events;\n\
10384 this variable holds the actual mouse event that led to the menu,\n\
10385 so that you can determine whether the command was run by mouse or not.");
10387 DEFVAR_LISP ("last-input-char", &last_input_char,
10388 "Last input event.");
10390 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
10391 "Last input event.");
10393 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
10394 "List of events to be read as the command input.\n\
10395 These events are processed first, before actual keyboard input.");
10396 Vunread_command_events = Qnil;
10398 DEFVAR_INT ("unread-command-char", &unread_command_char,
10399 "If not -1, an object to be read as next command input event.");
10401 DEFVAR_LISP ("unread-post-input-method-events", &Vunread_post_input_method_events,
10402 "List of events to be processed as input by input methods.\n\
10403 These events are processed after `unread-command-events', but\n\
10404 before actual keyboard input.");
10405 Vunread_post_input_method_events = Qnil;
10407 DEFVAR_LISP ("unread-input-method-events", &Vunread_input_method_events,
10408 "List of events to be processed as input by input methods.\n\
10409 These events are processed after `unread-command-events', but\n\
10410 before actual keyboard input.");
10411 Vunread_input_method_events = Qnil;
10413 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
10414 "Meta-prefix character code.\n\
10415 Meta-foo as command input turns into this character followed by foo.");
10416 XSETINT (meta_prefix_char, 033);
10418 DEFVAR_KBOARD ("last-command", Vlast_command,
10419 "The last command executed.\n\
10420 Normally a symbol with a function definition, but can be whatever was found\n\
10421 in the keymap, or whatever the variable `this-command' was set to by that\n\
10422 command.\n\
10424 The value `mode-exit' is special; it means that the previous command\n\
10425 read an event that told it to exit, and it did so and unread that event.\n\
10426 In other words, the present command is the event that made the previous\n\
10427 command exit.\n\
10429 The value `kill-region' is special; it means that the previous command\n\
10430 was a kill command.");
10432 DEFVAR_KBOARD ("real-last-command", Vreal_last_command,
10433 "Same as `last-command', but never altered by Lisp code.");
10435 DEFVAR_LISP ("this-command", &Vthis_command,
10436 "The command now being executed.\n\
10437 The command can set this variable; whatever is put here\n\
10438 will be in `last-command' during the following command.");
10439 Vthis_command = Qnil;
10441 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
10442 "*Number of input events between auto-saves.\n\
10443 Zero means disable autosaving due to number of characters typed.");
10444 auto_save_interval = 300;
10446 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
10447 "*Number of seconds idle time before auto-save.\n\
10448 Zero or nil means disable auto-saving due to idleness.\n\
10449 After auto-saving due to this many seconds of idle time,\n\
10450 Emacs also does a garbage collection if that seems to be warranted.");
10451 XSETFASTINT (Vauto_save_timeout, 30);
10453 DEFVAR_LISP ("echo-keystrokes", &Vecho_keystrokes,
10454 "*Nonzero means echo unfinished commands after this many seconds of pause.\n\
10455 The value may be integer or floating point.");
10456 Vecho_keystrokes = make_number (1);
10458 DEFVAR_INT ("polling-period", &polling_period,
10459 "*Interval between polling for input during Lisp execution.\n\
10460 The reason for polling is to make C-g work to stop a running program.\n\
10461 Polling is needed only when using X windows and SIGIO does not work.\n\
10462 Polling is automatically disabled in all other cases.");
10463 polling_period = 2;
10465 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
10466 "*Maximum time between mouse clicks to make a double-click.\n\
10467 Measured in milliseconds. nil means disable double-click recognition;\n\
10468 t means double-clicks have no time limit and are detected\n\
10469 by position only.");
10470 Vdouble_click_time = make_number (500);
10472 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
10473 "*Non-nil means inhibit local map menu bar menus.");
10474 inhibit_local_menu_bar_menus = 0;
10476 DEFVAR_INT ("num-input-keys", &num_input_keys,
10477 "Number of complete key sequences read as input so far.\n\
10478 This includes key sequences read from keyboard macros.\n\
10479 The number is effectively the number of interactive command invocations.");
10480 num_input_keys = 0;
10482 DEFVAR_INT ("num-nonmacro-input-events", &num_nonmacro_input_events,
10483 "Number of input events read from the keyboard so far.\n\
10484 This does not include events generated by keyboard macros.");
10485 num_nonmacro_input_events = 0;
10487 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
10488 "The frame in which the most recently read event occurred.\n\
10489 If the last event came from a keyboard macro, this is set to `macro'.");
10490 Vlast_event_frame = Qnil;
10492 /* This variable is set up in sysdep.c. */
10493 DEFVAR_LISP ("tty-erase-char", &Vtty_erase_char,
10494 "The ERASE character as set by the user with stty.");
10496 DEFVAR_LISP ("help-char", &Vhelp_char,
10497 "Character to recognize as meaning Help.\n\
10498 When it is read, do `(eval help-form)', and display result if it's a string.\n\
10499 If the value of `help-form' is nil, this char can be read normally.");
10500 XSETINT (Vhelp_char, Ctl ('H'));
10502 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
10503 "List of input events to recognize as meaning Help.\n\
10504 These work just like the value of `help-char' (see that).");
10505 Vhelp_event_list = Qnil;
10507 DEFVAR_LISP ("help-form", &Vhelp_form,
10508 "Form to execute when character `help-char' is read.\n\
10509 If the form returns a string, that string is displayed.\n\
10510 If `help-form' is nil, the help char is not recognized.");
10511 Vhelp_form = Qnil;
10513 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
10514 "Command to run when `help-char' character follows a prefix key.\n\
10515 This command is used only when there is no actual binding\n\
10516 for that character after that prefix key.");
10517 Vprefix_help_command = Qnil;
10519 DEFVAR_LISP ("top-level", &Vtop_level,
10520 "Form to evaluate when Emacs starts up.\n\
10521 Useful to set before you dump a modified Emacs.");
10522 Vtop_level = Qnil;
10524 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
10525 "Translate table for keyboard input, or nil.\n\
10526 Each character is looked up in this string and the contents used instead.\n\
10527 The value may be a string, a vector, or a char-table.\n\
10528 If it is a string or vector of length N,\n\
10529 character codes N and up are untranslated.\n\
10530 In a vector or a char-table, an element which is nil means \"no translation\".");
10531 Vkeyboard_translate_table = Qnil;
10533 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
10534 "Non-nil means to always spawn a subshell instead of suspending.\n\
10535 \(Even if the operating system has support for stopping a process.\)");
10536 cannot_suspend = 0;
10538 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
10539 "Non-nil means prompt with menus when appropriate.\n\
10540 This is done when reading from a keymap that has a prompt string,\n\
10541 for elements that have prompt strings.\n\
10542 The menu is displayed on the screen\n\
10543 if X menus were enabled at configuration\n\
10544 time and the previous event was a mouse click prefix key.\n\
10545 Otherwise, menu prompting uses the echo area.");
10546 menu_prompting = 1;
10548 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
10549 "Character to see next line of menu prompt.\n\
10550 Type this character while in a menu prompt to rotate around the lines of it.");
10551 XSETINT (menu_prompt_more_char, ' ');
10553 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
10554 "A mask of additional modifier keys to use with every keyboard character.\n\
10555 Emacs applies the modifiers of the character stored here to each keyboard\n\
10556 character it reads. For example, after evaluating the expression\n\
10557 (setq extra-keyboard-modifiers ?\\C-x)\n\
10558 all input characters will have the control modifier applied to them.\n\
10560 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
10561 not count as a control character; rather, it counts as a character\n\
10562 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
10563 cancels any modification.");
10564 extra_keyboard_modifiers = 0;
10566 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
10567 "If an editing command sets this to t, deactivate the mark afterward.\n\
10568 The command loop sets this to nil before each command,\n\
10569 and tests the value when the command returns.\n\
10570 Buffer modification stores t in this variable.");
10571 Vdeactivate_mark = Qnil;
10573 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
10574 "Temporary storage of pre-command-hook or post-command-hook.");
10575 Vcommand_hook_internal = Qnil;
10577 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
10578 "Normal hook run before each command is executed.\n\
10579 If an unhandled error happens in running this hook,\n\
10580 the hook value is set to nil, since otherwise the error\n\
10581 might happen repeatedly and make Emacs nonfunctional.");
10582 Vpre_command_hook = Qnil;
10584 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
10585 "Normal hook run after each command is executed.\n\
10586 If an unhandled error happens in running this hook,\n\
10587 the hook value is set to nil, since otherwise the error\n\
10588 might happen repeatedly and make Emacs nonfunctional.");
10589 Vpost_command_hook = Qnil;
10591 DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
10592 "Normal hook run after each command is executed, if idle.\n\
10593 Errors running the hook are caught and ignored.\n\
10594 This feature is obsolete; use idle timers instead. See `etc/NEWS'.");
10595 Vpost_command_idle_hook = Qnil;
10597 DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
10598 "Delay time before running `post-command-idle-hook'.\n\
10599 This is measured in microseconds.");
10600 post_command_idle_delay = 100000;
10602 #if 0
10603 DEFVAR_LISP ("echo-area-clear-hook", ...,
10604 "Normal hook run when clearing the echo area.");
10605 #endif
10606 Qecho_area_clear_hook = intern ("echo-area-clear-hook");
10607 XSYMBOL (Qecho_area_clear_hook)->value = Qnil;
10609 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
10610 "t means menu bar, specified Lucid style, needs to be recomputed.");
10611 Vlucid_menu_bar_dirty_flag = Qnil;
10613 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
10614 "List of menu bar items to move to the end of the menu bar.\n\
10615 The elements of the list are event types that may have menu bar bindings.");
10616 Vmenu_bar_final_items = Qnil;
10618 DEFVAR_KBOARD ("overriding-terminal-local-map",
10619 Voverriding_terminal_local_map,
10620 "Per-terminal keymap that overrides all other local keymaps.\n\
10621 If this variable is non-nil, it is used as a keymap instead of the\n\
10622 buffer's local map, and the minor mode keymaps and text property keymaps.\n\
10623 This variable is intended to let commands such as `universal-argumemnt'\n\
10624 set up a different keymap for reading the next command.");
10626 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
10627 "Keymap that overrides all other local keymaps.\n\
10628 If this variable is non-nil, it is used as a keymap instead of the\n\
10629 buffer's local map, and the minor mode keymaps and text property keymaps.");
10630 Voverriding_local_map = Qnil;
10632 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
10633 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
10634 Otherwise, the menu bar continues to reflect the buffer's local map\n\
10635 and the minor mode maps regardless of `overriding-local-map'.");
10636 Voverriding_local_map_menu_flag = Qnil;
10638 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
10639 "Keymap defining bindings for special events to execute at low level.");
10640 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
10642 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
10643 "*Non-nil means generate motion events for mouse motion.");
10645 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
10646 "Alist of system-specific X windows key symbols.\n\
10647 Each element should have the form (N . SYMBOL) where N is the\n\
10648 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
10649 and SYMBOL is its name.");
10651 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
10652 "List of deferred actions to be performed at a later time.\n\
10653 The precise format isn't relevant here; we just check whether it is nil.");
10654 Vdeferred_action_list = Qnil;
10656 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
10657 "Function to call to handle deferred actions, after each command.\n\
10658 This function is called with no arguments after each command\n\
10659 whenever `deferred-action-list' is non-nil.");
10660 Vdeferred_action_function = Qnil;
10662 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
10663 "*Non-nil means show the equivalent key-binding when M-x command has one.\n\
10664 The value can be a length of time to show the message for.\n\
10665 If the value is non-nil and not a number, we wait 2 seconds.");
10666 Vsuggest_key_bindings = Qt;
10668 DEFVAR_LISP ("timer-list", &Vtimer_list,
10669 "List of active absolute time timers in order of increasing time");
10670 Vtimer_list = Qnil;
10672 DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
10673 "List of active idle-time timers in order of increasing time");
10674 Vtimer_idle_list = Qnil;
10676 DEFVAR_LISP ("input-method-function", &Vinput_method_function,
10677 "If non-nil, the function that implements the current input method.\n\
10678 It's called with one argument, a printing character that was just read.\n\
10679 \(That means a character with code 040...0176.)\n\
10680 Typically this function uses `read-event' to read additional events.\n\
10681 When it does so, it should first bind `input-method-function' to nil\n\
10682 so it will not be called recursively.\n\
10684 The function should return a list of zero or more events\n\
10685 to be used as input. If it wants to put back some events\n\
10686 to be reconsidered, separately, by the input method,\n\
10687 it can add them to the beginning of `unread-command-events'.\n\
10689 The input method function can find in `input-method-previous-method'\n\
10690 the previous echo area message.\n\
10692 The input method function should refer to the variables\n\
10693 `input-method-use-echo-area' and `input-method-exit-on-first-char'\n\
10694 for guidance on what to do.");
10695 Vinput_method_function = Qnil;
10697 DEFVAR_LISP ("input-method-previous-message",
10698 &Vinput_method_previous_message,
10699 "When `input-method-function' is called, hold the previous echo area message.\n\
10700 This variable exists because `read-event' clears the echo area\n\
10701 before running the input method. It is nil if there was no message.");
10702 Vinput_method_previous_message = Qnil;
10704 DEFVAR_LISP ("show-help-function", &Vshow_help_function,
10705 "If non-nil, the function that implements the display of help.\n\
10706 It's called with one argument, the help string to display.");
10707 Vshow_help_function = Qnil;
10709 DEFVAR_LISP ("disable-point-adjustment", &Vdisable_point_adjustment,
10710 "If non-nil, suppress point adjustment after executing a command.\n\
10712 After a command is executed, if point is moved into a region that has\n\
10713 special properties (e.g. composition, display), we adjust point to\n\
10714 the boundary of the region. But, several special commands sets this\n\
10715 variable to non-nil, then we suppress the point adjustment.\n\
10717 This variable is set to nil before reading a command, and is checked\n\
10718 just after executing the command");
10719 Vdisable_point_adjustment = Qnil;
10721 DEFVAR_LISP ("global-disable-point-adjustment",
10722 &Vglobal_disable_point_adjustment,
10723 "*If non-nil, always suppress point adjustment.\n\
10725 The default value is nil, in which case, point adjustment are\n\
10726 suppressed only after special commands that set\n\
10727 `disable-point-adjustment' (which see) to non-nil.");
10728 Vglobal_disable_point_adjustment = Qnil;
10730 DEFVAR_BOOL ("update-menu-bindings", &update_menu_bindings,
10731 "Non-nil means updating menu bindings is allowed.\n\
10732 A value of nil means menu bindings should not be updated.\n\
10733 Used during Emacs' startup.");
10734 update_menu_bindings = 1;
10736 DEFVAR_LISP ("minibuffer-message-timeout", &Vminibuffer_message_timeout,
10737 "*How long to display an echo-area message when the minibuffer is active.\n\
10738 If the value is not a number, such messages don't time out.");
10739 Vminibuffer_message_timeout = make_number (2);
10742 void
10743 keys_of_keyboard ()
10745 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
10746 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
10747 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
10748 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
10749 initial_define_key (meta_map, 'x', "execute-extended-command");
10751 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
10752 "handle-delete-frame");
10753 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
10754 "ignore-event");
10755 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
10756 "ignore-event");