(vip-add-hook,vip-remove-hook): new functions.
[emacs.git] / src / keyboard.c
blobf1181188103a2f22c24ae3c5c9af624248be83c1
1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985,86,87,88,89,93,94,95 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Allow config.h to undefine symbols found here. */
21 #include <signal.h>
23 #include <config.h>
24 #include <stdio.h>
25 #include "termchar.h"
26 #include "termopts.h"
27 #include "lisp.h"
28 #include "termhooks.h"
29 #include "macros.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "commands.h"
33 #include "buffer.h"
34 #include "disptab.h"
35 #include "dispextern.h"
36 #include "keyboard.h"
37 #include "intervals.h"
38 #include "blockinput.h"
39 #include <setjmp.h>
40 #include <errno.h>
42 #ifdef MSDOS
43 #include "msdos.h"
44 #include <time.h>
45 #else /* not MSDOS */
46 #ifndef VMS
47 #include <sys/ioctl.h>
48 #endif
49 #endif /* not MSDOS */
51 #include "syssignal.h"
52 #include "systty.h"
54 /* This is to get the definitions of the XK_ symbols. */
55 #ifdef HAVE_X_WINDOWS
56 #include "xterm.h"
57 #endif
59 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
60 #include "systime.h"
62 extern int errno;
64 /* Variables for blockinput.h: */
66 /* Non-zero if interrupt input is blocked right now. */
67 int interrupt_input_blocked;
69 /* Nonzero means an input interrupt has arrived
70 during the current critical section. */
71 int interrupt_input_pending;
74 /* File descriptor to use for input. */
75 extern int input_fd;
77 #ifdef HAVE_X_WINDOWS
78 /* Make all keyboard buffers much bigger when using X windows. */
79 #define KBD_BUFFER_SIZE 4096
80 #else /* No X-windows, character input */
81 #define KBD_BUFFER_SIZE 256
82 #endif /* No X-windows */
84 /* Following definition copied from eval.c */
86 struct backtrace
88 struct backtrace *next;
89 Lisp_Object *function;
90 Lisp_Object *args; /* Points to vector of args. */
91 int nargs; /* length of vector. If nargs is UNEVALLED,
92 args points to slot holding list of
93 unevalled args */
94 char evalargs;
97 #ifdef MULTI_KBOARD
98 KBOARD *initial_kboard;
99 KBOARD *current_kboard;
100 KBOARD *all_kboards;
101 int single_kboard;
102 #else
103 KBOARD the_only_kboard;
104 #endif
106 /* Non-nil disable property on a command means
107 do not execute it; call disabled-command-hook's value instead. */
108 Lisp_Object Qdisabled, Qdisabled_command_hook;
110 #define NUM_RECENT_KEYS (100)
111 int recent_keys_index; /* Index for storing next element into recent_keys */
112 int total_keys; /* Total number of elements stored into recent_keys */
113 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
115 /* Vector holding the key sequence that invoked the current command.
116 It is reused for each command, and it may be longer than the current
117 sequence; this_command_key_count indicates how many elements
118 actually mean something.
119 It's easier to staticpro a single Lisp_Object than an array. */
120 Lisp_Object this_command_keys;
121 int this_command_key_count;
123 /* Record values of this_command_key_count and echo_length ()
124 before this command was read. */
125 static int before_command_key_count;
126 static int before_command_echo_length;
127 /* Values of before_command_key_count and before_command_echo_length
128 saved by reset-this-command-lengths. */
129 static int before_command_key_count_1;
130 static int before_command_echo_length_1;
131 /* Flag set by reset-this-command-lengths,
132 saying to reset the lengths when add_command_key is called. */
133 static int before_command_restore_flag;
135 extern int minbuf_level;
137 extern struct backtrace *backtrace_list;
139 /* Nonzero means do menu prompting. */
140 static int menu_prompting;
142 /* Character to see next line of menu prompt. */
143 static Lisp_Object menu_prompt_more_char;
145 /* For longjmp to where kbd input is being done. */
146 static jmp_buf getcjmp;
148 /* True while doing kbd input. */
149 int waiting_for_input;
151 /* True while displaying for echoing. Delays C-g throwing. */
152 static int echoing;
154 /* Nonzero means disregard local maps for the menu bar. */
155 static int inhibit_local_menu_bar_menus;
157 /* Nonzero means C-g should cause immediate error-signal. */
158 int immediate_quit;
160 /* Character to recognize as the help char. */
161 Lisp_Object Vhelp_char;
163 /* List of other event types to recognize as meaning "help". */
164 Lisp_Object Vhelp_event_list;
166 /* Form to execute when help char is typed. */
167 Lisp_Object Vhelp_form;
169 /* Command to run when the help character follows a prefix key. */
170 Lisp_Object Vprefix_help_command;
172 /* List of items that should move to the end of the menu bar. */
173 Lisp_Object Vmenu_bar_final_items;
175 /* Character that causes a quit. Normally C-g.
177 If we are running on an ordinary terminal, this must be an ordinary
178 ASCII char, since we want to make it our interrupt character.
180 If we are not running on an ordinary terminal, it still needs to be
181 an ordinary ASCII char. This character needs to be recognized in
182 the input interrupt handler. At this point, the keystroke is
183 represented as a struct input_event, while the desired quit
184 character is specified as a lispy event. The mapping from struct
185 input_events to lispy events cannot run in an interrupt handler,
186 and the reverse mapping is difficult for anything but ASCII
187 keystrokes.
189 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
190 ASCII character. */
191 int quit_char;
193 extern Lisp_Object current_global_map;
194 extern int minibuf_level;
196 /* If non-nil, this is a map that overrides all other local maps. */
197 Lisp_Object Voverriding_local_map;
199 /* If non-nil, Voverriding_local_map applies to the menu bar. */
200 Lisp_Object Voverriding_local_map_menu_flag;
202 /* Keymap that defines special misc events that should
203 be processed immediately at a low level. */
204 Lisp_Object Vspecial_event_map;
206 /* Current depth in recursive edits. */
207 int command_loop_level;
209 /* Total number of times command_loop has read a key sequence. */
210 int num_input_keys;
212 /* Last input character read as a command. */
213 Lisp_Object last_command_char;
215 /* Last input character read as a command, not counting menus
216 reached by the mouse. */
217 Lisp_Object last_nonmenu_event;
219 /* Last input character read for any purpose. */
220 Lisp_Object last_input_char;
222 /* If not Qnil, a list of objects to be read as subsequent command input. */
223 Lisp_Object Vunread_command_events;
225 /* If not -1, an event to be read as subsequent command input. */
226 int unread_command_char;
228 /* If not Qnil, this is a switch-frame event which we decided to put
229 off until the end of a key sequence. This should be read as the
230 next command input, after any unread_command_events.
232 read_key_sequence uses this to delay switch-frame events until the
233 end of the key sequence; Fread_char uses it to put off switch-frame
234 events until a non-ASCII event is acceptable as input. */
235 Lisp_Object unread_switch_frame;
237 /* A mask of extra modifier bits to put into every keyboard char. */
238 int extra_keyboard_modifiers;
240 /* Char to use as prefix when a meta character is typed in.
241 This is bound on entry to minibuffer in case ESC is changed there. */
243 Lisp_Object meta_prefix_char;
245 /* Last size recorded for a current buffer which is not a minibuffer. */
246 static int last_non_minibuf_size;
248 /* Number of idle seconds before an auto-save and garbage collection. */
249 static Lisp_Object Vauto_save_timeout;
251 /* Total number of times read_char has returned. */
252 int num_input_chars;
254 /* Total number of times read_char has returned, outside of macros. */
255 int num_nonmacro_input_chars;
257 /* Auto-save automatically when this many characters have been typed
258 since the last time. */
260 static int auto_save_interval;
262 /* Value of num_nonmacro_input_chars as of last auto save. */
264 int last_auto_save;
266 /* The command being executed by the command loop.
267 Commands may set this, and the value set will be copied into
268 current_kboard->Vlast_command instead of the actual command. */
269 Lisp_Object this_command;
271 /* The value of point when the last command was executed. */
272 int last_point_position;
274 /* The buffer that was current when the last command was started. */
275 Lisp_Object last_point_position_buffer;
277 #ifdef MULTI_FRAME
278 /* The frame in which the last input event occurred, or Qmacro if the
279 last event came from a macro. We use this to determine when to
280 generate switch-frame events. This may be cleared by functions
281 like Fselect_frame, to make sure that a switch-frame event is
282 generated by the next character. */
283 Lisp_Object internal_last_event_frame;
284 #endif
286 /* A user-visible version of the above, intended to allow users to
287 figure out where the last event came from, if the event doesn't
288 carry that information itself (i.e. if it was a character). */
289 Lisp_Object Vlast_event_frame;
291 /* The timestamp of the last input event we received from the X server.
292 X Windows wants this for selection ownership. */
293 unsigned long last_event_timestamp;
295 Lisp_Object Qself_insert_command;
296 Lisp_Object Qforward_char;
297 Lisp_Object Qbackward_char;
298 Lisp_Object Qundefined;
300 /* read_key_sequence stores here the command definition of the
301 key sequence that it reads. */
302 Lisp_Object read_key_sequence_cmd;
304 /* Form to evaluate (if non-nil) when Emacs is started. */
305 Lisp_Object Vtop_level;
307 /* User-supplied string to translate input characters through. */
308 Lisp_Object Vkeyboard_translate_table;
310 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
311 extern Lisp_Object Vfunction_key_map;
313 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
314 Lisp_Object Vkey_translation_map;
316 /* Non-nil means deactivate the mark at end of this command. */
317 Lisp_Object Vdeactivate_mark;
319 /* Menu bar specified in Lucid Emacs fashion. */
321 Lisp_Object Vlucid_menu_bar_dirty_flag;
322 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
324 /* Hooks to run before and after each command. */
325 Lisp_Object Qpre_command_hook, Qpost_command_hook;
326 Lisp_Object Vpre_command_hook, Vpost_command_hook;
327 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
329 /* List of deferred actions to be performed at a later time.
330 The precise format isn't relevant here; we just check whether it is nil. */
331 Lisp_Object Vdeferred_action_list;
333 /* Function to call to handle deferred actions, when there are any. */
334 Lisp_Object Vdeferred_action_function;
335 Lisp_Object Qdeferred_action_function;
337 /* File in which we write all commands we read. */
338 FILE *dribble;
340 /* Nonzero if input is available. */
341 int input_pending;
343 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
344 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
346 int meta_key;
348 extern char *pending_malloc_warning;
350 /* Circular buffer for pre-read keyboard input. */
351 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
353 /* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
355 The interrupt-level event handlers will never enqueue an event on a
356 frame which is not in Vframe_list, and once an event is dequeued,
357 internal_last_event_frame or the event itself points to the frame.
358 So that's all fine.
360 But while the event is sitting in the queue, it's completely
361 unprotected. Suppose the user types one command which will run for
362 a while and then delete a frame, and then types another event at
363 the frame that will be deleted, before the command gets around to
364 it. Suppose there are no references to this frame elsewhere in
365 Emacs, and a GC occurs before the second event is dequeued. Now we
366 have an event referring to a freed frame, which will crash Emacs
367 when it is dequeued.
369 Similar things happen when an event on a scroll bar is enqueued; the
370 window may be deleted while the event is in the queue.
372 So, we use this vector to protect the frame_or_window field in the
373 event queue. That way, they'll be dequeued as dead frames or
374 windows, but still valid lisp objects.
376 If kbd_buffer[i].kind != no_event, then
377 (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
378 == kbd_buffer[i].frame_or_window. */
379 static Lisp_Object kbd_buffer_frame_or_window;
381 /* Pointer to next available character in kbd_buffer.
382 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
383 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
384 next available char is in kbd_buffer[0]. */
385 static struct input_event *kbd_fetch_ptr;
387 /* Pointer to next place to store character in kbd_buffer. This
388 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
389 character should go in kbd_buffer[0]. */
390 static volatile struct input_event *kbd_store_ptr;
392 /* The above pair of variables forms a "queue empty" flag. When we
393 enqueue a non-hook event, we increment kbd_store_ptr. When we
394 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
395 there is input available iff the two pointers are not equal.
397 Why not just have a flag set and cleared by the enqueuing and
398 dequeuing functions? Such a flag could be screwed up by interrupts
399 at inopportune times. */
401 /* If this flag is non-nil, we check mouse_moved to see when the
402 mouse moves, and motion events will appear in the input stream.
403 Otherwise, mouse motion is ignored. */
404 static Lisp_Object do_mouse_tracking;
406 /* Symbols to head events. */
407 Lisp_Object Qmouse_movement;
408 Lisp_Object Qscroll_bar_movement;
409 Lisp_Object Qswitch_frame;
410 Lisp_Object Qdelete_frame;
411 Lisp_Object Qiconify_frame;
412 Lisp_Object Qmake_frame_visible;
414 /* Symbols to denote kinds of events. */
415 Lisp_Object Qfunction_key;
416 Lisp_Object Qmouse_click;
417 /* Lisp_Object Qmouse_movement; - also an event header */
419 /* Properties of event headers. */
420 Lisp_Object Qevent_kind;
421 Lisp_Object Qevent_symbol_elements;
423 Lisp_Object Qmenu_enable;
425 /* An event header symbol HEAD may have a property named
426 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
427 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
428 mask of modifiers applied to it. If present, this is used to help
429 speed up parse_modifiers. */
430 Lisp_Object Qevent_symbol_element_mask;
432 /* An unmodified event header BASE may have a property named
433 Qmodifier_cache, which is an alist mapping modifier masks onto
434 modified versions of BASE. If present, this helps speed up
435 apply_modifiers. */
436 Lisp_Object Qmodifier_cache;
438 /* Symbols to use for parts of windows. */
439 Lisp_Object Qmode_line;
440 Lisp_Object Qvertical_line;
441 Lisp_Object Qvertical_scroll_bar;
442 Lisp_Object Qmenu_bar;
444 extern Lisp_Object Qmenu_enable;
446 Lisp_Object recursive_edit_unwind (), command_loop ();
447 Lisp_Object Fthis_command_keys ();
448 Lisp_Object Qextended_command_history;
450 extern char *x_get_keysym_name ();
452 Lisp_Object Qpolling_period;
454 extern Lisp_Object Vprint_level, Vprint_length;
456 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
457 happens. */
458 EMACS_TIME *input_available_clear_time;
460 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
461 Default is 1 if INTERRUPT_INPUT is defined. */
462 int interrupt_input;
464 /* Nonzero while interrupts are temporarily deferred during redisplay. */
465 int interrupts_deferred;
467 /* nonzero means use ^S/^Q for flow control. */
468 int flow_control;
470 /* Allow m- file to inhibit use of FIONREAD. */
471 #ifdef BROKEN_FIONREAD
472 #undef FIONREAD
473 #endif
475 /* We are unable to use interrupts if FIONREAD is not available,
476 so flush SIGIO so we won't try. */
477 #ifndef FIONREAD
478 #ifdef SIGIO
479 #undef SIGIO
480 #endif
481 #endif
483 /* If we support X Windows, turn on the code to poll periodically
484 to detect C-g. It isn't actually used when doing interrupt input. */
485 #ifdef HAVE_X_WINDOWS
486 #define POLL_FOR_INPUT
487 #endif
489 /* Global variable declarations. */
491 /* Function for init_keyboard to call with no args (if nonzero). */
492 void (*keyboard_init_hook) ();
494 static int read_avail_input ();
495 static void get_input_pending ();
496 static int readable_events ();
497 static Lisp_Object read_char_x_menu_prompt ();
498 static Lisp_Object read_char_minibuf_menu_prompt ();
499 static Lisp_Object make_lispy_event ();
500 #ifdef HAVE_MOUSE
501 static Lisp_Object make_lispy_movement ();
502 #endif
503 static Lisp_Object modify_event_symbol ();
504 static Lisp_Object make_lispy_switch_frame ();
505 static int parse_solitary_modifier ();
507 /* > 0 if we are to echo keystrokes. */
508 static int echo_keystrokes;
510 /* Nonzero means don't try to suspend even if the operating system seems
511 to support it. */
512 static int cannot_suspend;
514 #define min(a,b) ((a)<(b)?(a):(b))
515 #define max(a,b) ((a)>(b)?(a):(b))
517 /* Install the string STR as the beginning of the string of echoing,
518 so that it serves as a prompt for the next character.
519 Also start echoing. */
521 echo_prompt (str)
522 char *str;
524 int len = strlen (str);
526 if (len > ECHOBUFSIZE - 4)
527 len = ECHOBUFSIZE - 4;
528 bcopy (str, current_kboard->echobuf, len);
529 current_kboard->echoptr = current_kboard->echobuf + len;
530 *current_kboard->echoptr = '\0';
532 current_kboard->echo_after_prompt = len;
534 echo ();
537 /* Add C to the echo string, if echoing is going on.
538 C can be a character, which is printed prettily ("M-C-x" and all that
539 jazz), or a symbol, whose name is printed. */
541 echo_char (c)
542 Lisp_Object c;
544 extern char *push_key_description ();
546 if (current_kboard->immediate_echo)
548 char *ptr = current_kboard->echoptr;
550 if (ptr != current_kboard->echobuf)
551 *ptr++ = ' ';
553 /* If someone has passed us a composite event, use its head symbol. */
554 c = EVENT_HEAD (c);
556 if (INTEGERP (c))
558 if (ptr - current_kboard->echobuf > ECHOBUFSIZE - 6)
559 return;
561 ptr = push_key_description (XINT (c), ptr);
563 else if (SYMBOLP (c))
565 struct Lisp_String *name = XSYMBOL (c)->name;
566 if ((ptr - current_kboard->echobuf) + name->size + 4 > ECHOBUFSIZE)
567 return;
568 bcopy (name->data, ptr, name->size);
569 ptr += name->size;
572 if (current_kboard->echoptr == current_kboard->echobuf
573 && help_char_p (c))
575 strcpy (ptr, " (Type ? for further options)");
576 ptr += strlen (ptr);
579 *ptr = 0;
580 current_kboard->echoptr = ptr;
582 echo ();
586 /* Temporarily add a dash to the end of the echo string if it's not
587 empty, so that it serves as a mini-prompt for the very next character. */
589 echo_dash ()
591 if (!current_kboard->immediate_echo
592 && current_kboard->echoptr == current_kboard->echobuf)
593 return;
594 /* Do nothing if we just printed a prompt. */
595 if (current_kboard->echo_after_prompt
596 == current_kboard->echoptr - current_kboard->echobuf)
597 return;
598 /* Do nothing if not echoing at all. */
599 if (current_kboard->echoptr == 0)
600 return;
602 /* Put a dash at the end of the buffer temporarily,
603 but make it go away when the next character is added. */
604 current_kboard->echoptr[0] = '-';
605 current_kboard->echoptr[1] = 0;
607 echo ();
610 /* Display the current echo string, and begin echoing if not already
611 doing so. */
613 echo ()
615 if (!current_kboard->immediate_echo)
617 int i;
618 current_kboard->immediate_echo = 1;
620 for (i = 0; i < this_command_key_count; i++)
622 Lisp_Object c;
623 c = XVECTOR (this_command_keys)->contents[i];
624 if (! (EVENT_HAS_PARAMETERS (c)
625 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
626 echo_char (c);
628 echo_dash ();
631 echoing = 1;
632 message1_nolog (current_kboard->echobuf);
633 echoing = 0;
635 if (waiting_for_input && !NILP (Vquit_flag))
636 quit_throw_to_read_char ();
639 /* Turn off echoing, for the start of a new command. */
641 cancel_echoing ()
643 current_kboard->immediate_echo = 0;
644 current_kboard->echoptr = current_kboard->echobuf;
645 current_kboard->echo_after_prompt = -1;
648 /* Return the length of the current echo string. */
650 static int
651 echo_length ()
653 return current_kboard->echoptr - current_kboard->echobuf;
656 /* Truncate the current echo message to its first LEN chars.
657 This and echo_char get used by read_key_sequence when the user
658 switches frames while entering a key sequence. */
660 static void
661 echo_truncate (len)
662 int len;
664 current_kboard->echobuf[len] = '\0';
665 current_kboard->echoptr = current_kboard->echobuf + len;
666 truncate_echo_area (len);
670 /* Functions for manipulating this_command_keys. */
671 static void
672 add_command_key (key)
673 Lisp_Object key;
675 int size = XVECTOR (this_command_keys)->size;
677 /* If reset-this-command-length was called recently, obey it now.
678 See the doc string of that function for an explanation of why. */
679 if (before_command_restore_flag)
681 this_command_key_count = before_command_key_count_1;
682 echo_truncate (before_command_echo_length_1);
683 before_command_restore_flag = 0;
686 if (this_command_key_count >= size)
688 Lisp_Object new_keys;
690 new_keys = Fmake_vector (make_number (size * 2), Qnil);
691 bcopy (XVECTOR (this_command_keys)->contents,
692 XVECTOR (new_keys)->contents,
693 size * sizeof (Lisp_Object));
695 this_command_keys = new_keys;
698 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
701 Lisp_Object
702 recursive_edit_1 ()
704 int count = specpdl_ptr - specpdl;
705 Lisp_Object val;
707 if (command_loop_level > 0)
709 specbind (Qstandard_output, Qt);
710 specbind (Qstandard_input, Qt);
713 val = command_loop ();
714 if (EQ (val, Qt))
715 Fsignal (Qquit, Qnil);
717 return unbind_to (count, Qnil);
720 /* When an auto-save happens, record the "time", and don't do again soon. */
722 record_auto_save ()
724 last_auto_save = num_nonmacro_input_chars;
727 /* Make an auto save happen as soon as possible at command level. */
729 force_auto_save_soon ()
731 last_auto_save = - auto_save_interval - 1;
733 record_asynch_buffer_change ();
736 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
737 "Invoke the editor command loop recursively.\n\
738 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
739 that tells this function to return.\n\
740 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
741 This function is called by the editor initialization to begin editing.")
744 int count = specpdl_ptr - specpdl;
745 Lisp_Object val;
747 command_loop_level++;
748 update_mode_lines = 1;
750 record_unwind_protect (recursive_edit_unwind,
751 (command_loop_level
752 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
753 ? Fcurrent_buffer ()
754 : Qnil);
755 recursive_edit_1 ();
756 return unbind_to (count, Qnil);
759 Lisp_Object
760 recursive_edit_unwind (buffer)
761 Lisp_Object buffer;
763 if (!NILP (buffer))
764 Fset_buffer (buffer);
766 command_loop_level--;
767 update_mode_lines = 1;
768 return Qnil;
771 static void
772 any_kboard_state ()
774 #ifdef MULTI_KBOARD
775 #if 0 /* Theory: if there's anything in Vunread_command_events,
776 it will right away be read by read_key_sequence,
777 and then if we do switch KBOARDS, it will go into the side
778 queue then. So we don't need to do anything special here -- rms. */
779 if (CONSP (Vunread_command_events))
781 current_kboard->kbd_queue
782 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
783 current_kboard->kbd_queue_has_data = 1;
785 Vunread_command_events = Qnil;
786 #endif
787 single_kboard = 0;
788 #endif
791 /* Switch to the single-kboard state, making current_kboard
792 the only KBOARD from which further input is accepted. */
794 void
795 single_kboard_state ()
797 #ifdef MULTI_KBOARD
798 single_kboard = 1;
799 #endif
802 /* Maintain a stack of kboards, so other parts of Emacs
803 can switch temporarily to the kboard of a given frame
804 and then revert to the previous status. */
806 struct kboard_stack
808 KBOARD *kboard;
809 struct kboard_stack *next;
812 static struct kboard_stack *kboard_stack;
814 void
815 push_frame_kboard (f)
816 FRAME_PTR f;
818 #ifdef MULTI_KBOARD
819 struct kboard_stack *p
820 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
822 p->next = kboard_stack;
823 p->kboard = current_kboard;
824 kboard_stack = p;
826 current_kboard = FRAME_KBOARD (f);
827 #endif
830 void
831 pop_frame_kboard ()
833 #ifdef MULTI_KBOARD
834 struct kboard_stack *p = kboard_stack;
835 current_kboard = p->kboard;
836 kboard_stack = p->next;
837 xfree (p);
838 #endif
841 /* Handle errors that are not handled at inner levels
842 by printing an error message and returning to the editor command loop. */
844 Lisp_Object
845 cmd_error (data)
846 Lisp_Object data;
848 Lisp_Object old_level, old_length;
850 Vstandard_output = Qt;
851 Vstandard_input = Qt;
852 Vexecuting_macro = Qnil;
853 current_kboard->Vprefix_arg = Qnil;
854 cancel_echoing ();
856 /* Avoid unquittable loop if data contains a circular list. */
857 old_level = Vprint_level;
858 old_length = Vprint_length;
859 XSETFASTINT(Vprint_level, 10);
860 XSETFASTINT(Vprint_length, 10);
861 cmd_error_internal (data, NULL);
862 Vprint_level = old_level;
863 Vprint_length = old_length;
865 Vquit_flag = Qnil;
867 Vinhibit_quit = Qnil;
868 #ifdef MULTI_KBOARD
869 any_kboard_state ();
870 #endif
872 return make_number (0);
875 cmd_error_internal (data, context)
876 Lisp_Object data;
877 char *context;
879 Lisp_Object errmsg, tail, errname, file_error;
880 Lisp_Object stream;
881 struct gcpro gcpro1;
882 int i;
884 Vquit_flag = Qnil;
885 Vinhibit_quit = Qt;
886 echo_area_glyphs = 0;
888 /* If the window system or terminal frame hasn't been initialized
889 yet, or we're not interactive, it's best to dump this message out
890 to stderr and exit. */
891 if (! FRAME_MESSAGE_BUF (selected_frame)
892 || noninteractive)
893 stream = Qexternal_debugging_output;
894 else
896 Fdiscard_input ();
897 bitch_at_user ();
898 stream = Qt;
901 if (context != 0)
902 write_string_1 (context, -1, stream);
904 errname = Fcar (data);
906 if (EQ (errname, Qerror))
908 data = Fcdr (data);
909 if (!CONSP (data)) data = Qnil;
910 errmsg = Fcar (data);
911 file_error = Qnil;
913 else
915 errmsg = Fget (errname, Qerror_message);
916 file_error = Fmemq (Qfile_error,
917 Fget (errname, Qerror_conditions));
920 /* Print an error message including the data items.
921 This is done by printing it into a scratch buffer
922 and then making a copy of the text in the buffer. */
924 if (!CONSP (data)) data = Qnil;
925 tail = Fcdr (data);
926 GCPRO1 (tail);
928 /* For file-error, make error message by concatenating
929 all the data items. They are all strings. */
930 if (!NILP (file_error) && !NILP (tail))
931 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
933 if (STRINGP (errmsg))
934 Fprinc (errmsg, stream);
935 else
936 write_string_1 ("peculiar error", -1, stream);
938 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
940 write_string_1 (i ? ", " : ": ", 2, stream);
941 if (!NILP (file_error))
942 Fprinc (Fcar (tail), stream);
943 else
944 Fprin1 (Fcar (tail), stream);
946 UNGCPRO;
948 /* If the window system or terminal frame hasn't been initialized
949 yet, or we're in -batch mode, this error should cause Emacs to exit. */
950 if (! FRAME_MESSAGE_BUF (selected_frame)
951 || noninteractive)
953 Fterpri (stream);
954 Fkill_emacs (make_number (-1));
958 Lisp_Object command_loop_1 ();
959 Lisp_Object command_loop_2 ();
960 Lisp_Object top_level_1 ();
962 /* Entry to editor-command-loop.
963 This level has the catches for exiting/returning to editor command loop.
964 It returns nil to exit recursive edit, t to abort it. */
966 Lisp_Object
967 command_loop ()
969 if (command_loop_level > 0 || minibuf_level > 0)
971 return internal_catch (Qexit, command_loop_2, Qnil);
973 else
974 while (1)
976 internal_catch (Qtop_level, top_level_1, Qnil);
977 internal_catch (Qtop_level, command_loop_2, Qnil);
979 /* End of file in -batch run causes exit here. */
980 if (noninteractive)
981 Fkill_emacs (Qt);
985 /* Here we catch errors in execution of commands within the
986 editing loop, and reenter the editing loop.
987 When there is an error, cmd_error runs and returns a non-nil
988 value to us. A value of nil means that cmd_loop_1 itself
989 returned due to end of file (or end of kbd macro). */
991 Lisp_Object
992 command_loop_2 ()
994 register Lisp_Object val;
997 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
998 while (!NILP (val));
1000 return Qnil;
1003 Lisp_Object
1004 top_level_2 ()
1006 return Feval (Vtop_level);
1009 Lisp_Object
1010 top_level_1 ()
1012 /* On entry to the outer level, run the startup file */
1013 if (!NILP (Vtop_level))
1014 internal_condition_case (top_level_2, Qerror, cmd_error);
1015 else if (!NILP (Vpurify_flag))
1016 message ("Bare impure Emacs (standard Lisp code not loaded)");
1017 else
1018 message ("Bare Emacs (standard Lisp code not loaded)");
1019 return Qnil;
1022 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1023 "Exit all recursive editing levels.")
1026 Fthrow (Qtop_level, Qnil);
1029 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1030 "Exit from the innermost recursive edit or minibuffer.")
1033 if (command_loop_level > 0 || minibuf_level > 0)
1034 Fthrow (Qexit, Qnil);
1036 error ("No recursive edit is in progress");
1039 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1040 "Abort the command that requested this recursive edit or minibuffer input.")
1043 if (command_loop_level > 0 || minibuf_level > 0)
1044 Fthrow (Qexit, Qt);
1046 error ("No recursive edit is in progress");
1049 /* This is the actual command reading loop,
1050 sans error-handling encapsulation. */
1052 Lisp_Object Fcommand_execute ();
1053 static int read_key_sequence ();
1054 static void safe_run_hooks ();
1056 Lisp_Object
1057 command_loop_1 ()
1059 Lisp_Object cmd, tem;
1060 int lose;
1061 int nonundocount;
1062 Lisp_Object keybuf[30];
1063 int i;
1064 int no_redisplay;
1065 int no_direct;
1066 int prev_modiff;
1067 struct buffer *prev_buffer;
1068 #ifdef MULTI_KBOARD
1069 int was_locked = single_kboard;
1070 #endif
1072 current_kboard->Vprefix_arg = Qnil;
1073 Vdeactivate_mark = Qnil;
1074 waiting_for_input = 0;
1075 cancel_echoing ();
1077 nonundocount = 0;
1078 no_redisplay = 0;
1079 this_command_key_count = 0;
1081 /* Make sure this hook runs after commands that get errors and
1082 throw to top level. */
1083 /* Note that the value cell will never directly contain nil
1084 if the symbol is a local variable. */
1085 if (!NILP (XSYMBOL (Qpost_command_hook)->value) && !NILP (Vrun_hooks))
1086 safe_run_hooks (Qpost_command_hook);
1088 if (!NILP (Vdeferred_action_list))
1089 call0 (Vdeferred_action_function);
1091 /* Do this after running Vpost_command_hook, for consistency. */
1092 current_kboard->Vlast_command = this_command;
1094 while (1)
1096 /* Make sure the current window's buffer is selected. */
1097 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1098 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1100 /* Display any malloc warning that just came out. Use while because
1101 displaying one warning can cause another. */
1103 while (pending_malloc_warning)
1104 display_malloc_warning ();
1106 no_direct = 0;
1108 Vdeactivate_mark = Qnil;
1110 /* If minibuffer on and echo area in use,
1111 wait 2 sec and redraw minibuffer. */
1113 if (minibuf_level && echo_area_glyphs
1114 && EQ (minibuf_window, echo_area_window))
1116 /* Bind inhibit-quit to t so that C-g gets read in
1117 rather than quitting back to the minibuffer. */
1118 int count = specpdl_ptr - specpdl;
1119 specbind (Qinhibit_quit, Qt);
1120 Fsit_for (make_number (2), Qnil, Qnil);
1121 unbind_to (count, Qnil);
1123 echo_area_glyphs = 0;
1124 no_direct = 1;
1125 if (!NILP (Vquit_flag))
1127 Vquit_flag = Qnil;
1128 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1132 #ifdef C_ALLOCA
1133 alloca (0); /* Cause a garbage collection now */
1134 /* Since we can free the most stuff here. */
1135 #endif /* C_ALLOCA */
1137 #if 0
1138 #ifdef MULTI_FRAME
1139 /* Select the frame that the last event came from. Usually,
1140 switch-frame events will take care of this, but if some lisp
1141 code swallows a switch-frame event, we'll fix things up here.
1142 Is this a good idea? */
1143 if (FRAMEP (internal_last_event_frame)
1144 && XFRAME (internal_last_event_frame) != selected_frame)
1145 Fselect_frame (internal_last_event_frame, Qnil);
1146 #endif
1147 #endif
1148 /* If it has changed current-menubar from previous value,
1149 really recompute the menubar from the value. */
1150 if (! NILP (Vlucid_menu_bar_dirty_flag)
1151 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1152 call0 (Qrecompute_lucid_menubar);
1154 before_command_key_count = this_command_key_count;
1155 before_command_echo_length = echo_length ();
1157 /* Read next key sequence; i gets its length. */
1158 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1159 Qnil, 0, 1);
1161 ++num_input_keys;
1163 /* Now we have read a key sequence of length I,
1164 or else I is 0 and we found end of file. */
1166 if (i == 0) /* End of file -- happens only in */
1167 return Qnil; /* a kbd macro, at the end. */
1168 /* -1 means read_key_sequence got a menu that was rejected.
1169 Just loop around and read another command. */
1170 if (i == -1)
1172 cancel_echoing ();
1173 this_command_key_count = 0;
1174 goto finalize;
1177 last_command_char = keybuf[i - 1];
1179 /* If the previous command tried to force a specific window-start,
1180 forget about that, in case this command moves point far away
1181 from that position. But also throw away beg_unchanged and
1182 end_unchanged information in that case, so that redisplay will
1183 update the whole window properly. */
1184 if (!NILP (XWINDOW (selected_window)->force_start))
1186 XWINDOW (selected_window)->force_start = Qnil;
1187 beg_unchanged = end_unchanged = 0;
1190 cmd = read_key_sequence_cmd;
1191 if (!NILP (Vexecuting_macro))
1193 if (!NILP (Vquit_flag))
1195 Vexecuting_macro = Qt;
1196 QUIT; /* Make some noise. */
1197 /* Will return since macro now empty. */
1201 /* Do redisplay processing after this command except in special
1202 cases identified below that set no_redisplay to 1.
1203 (actually, there's currently no way to prevent the redisplay,
1204 and no_redisplay is ignored.
1205 Perhaps someday we will really implement it.) */
1206 no_redisplay = 0;
1208 prev_buffer = current_buffer;
1209 prev_modiff = MODIFF;
1210 last_point_position = PT;
1211 XSETBUFFER (last_point_position_buffer, prev_buffer);
1213 /* Execute the command. */
1215 this_command = cmd;
1216 /* Note that the value cell will never directly contain nil
1217 if the symbol is a local variable. */
1218 if (!NILP (XSYMBOL (Qpre_command_hook)->value) && !NILP (Vrun_hooks))
1219 safe_run_hooks (Qpre_command_hook);
1221 if (NILP (this_command))
1223 /* nil means key is undefined. */
1224 bitch_at_user ();
1225 current_kboard->defining_kbd_macro = Qnil;
1226 update_mode_lines = 1;
1227 current_kboard->Vprefix_arg = Qnil;
1229 else
1231 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1233 /* Recognize some common commands in common situations and
1234 do them directly. */
1235 if (EQ (this_command, Qforward_char) && PT < ZV)
1237 struct Lisp_Vector *dp
1238 = window_display_table (XWINDOW (selected_window));
1239 lose = FETCH_CHAR (PT);
1240 SET_PT (PT + 1);
1241 if ((dp
1242 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1243 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1244 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1245 && (lose >= 0x20 && lose < 0x7f)))
1246 : (lose >= 0x20 && lose < 0x7f))
1247 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1248 >= MODIFF)
1249 && (XFASTINT (XWINDOW (selected_window)->last_point)
1250 == PT - 1)
1251 && !windows_or_buffers_changed
1252 && EQ (current_buffer->selective_display, Qnil)
1253 && !detect_input_pending ()
1254 && NILP (Vexecuting_macro))
1255 no_redisplay = direct_output_forward_char (1);
1256 goto directly_done;
1258 else if (EQ (this_command, Qbackward_char) && PT > BEGV)
1260 struct Lisp_Vector *dp
1261 = window_display_table (XWINDOW (selected_window));
1262 SET_PT (PT - 1);
1263 lose = FETCH_CHAR (PT);
1264 if ((dp
1265 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1266 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1267 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1268 && (lose >= 0x20 && lose < 0x7f)))
1269 : (lose >= 0x20 && lose < 0x7f))
1270 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1271 >= MODIFF)
1272 && (XFASTINT (XWINDOW (selected_window)->last_point)
1273 == PT + 1)
1274 && !windows_or_buffers_changed
1275 && EQ (current_buffer->selective_display, Qnil)
1276 && !detect_input_pending ()
1277 && NILP (Vexecuting_macro))
1278 no_redisplay = direct_output_forward_char (-1);
1279 goto directly_done;
1281 else if (EQ (this_command, Qself_insert_command)
1282 /* Try this optimization only on ascii keystrokes. */
1283 && INTEGERP (last_command_char))
1285 unsigned char c = XINT (last_command_char);
1286 int value;
1288 if (NILP (Vexecuting_macro)
1289 && !EQ (minibuf_window, selected_window))
1291 if (!nonundocount || nonundocount >= 20)
1293 Fundo_boundary ();
1294 nonundocount = 0;
1296 nonundocount++;
1298 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1299 < MODIFF)
1300 || (XFASTINT (XWINDOW (selected_window)->last_point)
1301 != PT)
1302 || MODIFF <= SAVE_MODIFF
1303 || windows_or_buffers_changed
1304 || !EQ (current_buffer->selective_display, Qnil)
1305 || detect_input_pending ()
1306 || !NILP (Vexecuting_macro));
1307 value = internal_self_insert (c, 0);
1308 if (value)
1309 lose = 1;
1310 if (value == 2)
1311 nonundocount = 0;
1313 if (!lose
1314 && (PT == ZV || FETCH_CHAR (PT) == '\n'))
1316 struct Lisp_Vector *dp
1317 = window_display_table (XWINDOW (selected_window));
1318 int lose = c;
1320 if (dp)
1322 Lisp_Object obj;
1324 obj = DISP_CHAR_VECTOR (dp, lose);
1325 if (NILP (obj))
1327 /* Do it only for char codes
1328 that by default display as themselves. */
1329 if (lose >= 0x20 && lose <= 0x7e)
1330 no_redisplay = direct_output_for_insert (lose);
1332 else if (VECTORP (obj)
1333 && XVECTOR (obj)->size == 1
1334 && (obj = XVECTOR (obj)->contents[0],
1335 INTEGERP (obj))
1336 /* Insist face not specified in glyph. */
1337 && (XINT (obj) & ((-1) << 8)) == 0)
1338 no_redisplay
1339 = direct_output_for_insert (XINT (obj));
1341 else
1343 if (lose >= 0x20 && lose <= 0x7e)
1344 no_redisplay = direct_output_for_insert (lose);
1347 goto directly_done;
1351 /* Here for a command that isn't executed directly */
1353 nonundocount = 0;
1354 if (NILP (current_kboard->Vprefix_arg))
1355 Fundo_boundary ();
1356 Fcommand_execute (this_command, Qnil);
1359 directly_done: ;
1361 /* Note that the value cell will never directly contain nil
1362 if the symbol is a local variable. */
1363 if (!NILP (XSYMBOL (Qpost_command_hook)->value) && !NILP (Vrun_hooks))
1364 safe_run_hooks (Qpost_command_hook);
1366 if (!NILP (Vdeferred_action_list))
1367 safe_run_hooks (Qdeferred_action_function);
1369 /* If there is a prefix argument,
1370 1) We don't want Vlast_command to be ``universal-argument''
1371 (that would be dumb), so don't set Vlast_command,
1372 2) we want to leave echoing on so that the prefix will be
1373 echoed as part of this key sequence, so don't call
1374 cancel_echoing, and
1375 3) we want to leave this_command_key_count non-zero, so that
1376 read_char will realize that it is re-reading a character, and
1377 not echo it a second time.
1379 If the command didn't actually create a prefix arg,
1380 but is merely a frame event that is transparent to prefix args,
1381 then the above doesn't apply. */
1382 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1384 current_kboard->Vlast_command = this_command;
1385 cancel_echoing ();
1386 this_command_key_count = 0;
1389 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1391 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1393 current_buffer->mark_active = Qnil;
1394 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1396 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1397 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1400 finalize:
1401 /* Install chars successfully executed in kbd macro. */
1403 if (!NILP (current_kboard->defining_kbd_macro)
1404 && NILP (current_kboard->Vprefix_arg))
1405 finalize_kbd_macro_chars ();
1407 #ifdef MULTI_KBOARD
1408 if (!was_locked)
1409 any_kboard_state ();
1410 #endif
1414 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1416 static Lisp_Object
1417 safe_run_hooks_1 (hook)
1418 Lisp_Object hook;
1420 return call1 (Vrun_hooks, Vinhibit_quit);
1423 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1425 static Lisp_Object
1426 safe_run_hooks_error (data)
1427 Lisp_Object data;
1429 Fset (Vinhibit_quit, Qnil);
1432 /* If we get an error while running the hook, cause the hook variable
1433 to be nil. Also inhibit quits, so that C-g won't cause the hook
1434 to mysteriously evaporate. */
1436 static void
1437 safe_run_hooks (hook)
1438 Lisp_Object hook;
1440 Lisp_Object value;
1441 int count = specpdl_ptr - specpdl;
1442 specbind (Qinhibit_quit, hook);
1444 internal_condition_case (safe_run_hooks_1, Qerror, safe_run_hooks_error);
1446 unbind_to (count, Qnil);
1449 /* Number of seconds between polling for input. */
1450 int polling_period;
1452 /* Nonzero means polling for input is temporarily suppressed. */
1453 int poll_suppress_count;
1455 /* Nonzero if polling_for_input is actually being used. */
1456 int polling_for_input;
1458 #ifdef POLL_FOR_INPUT
1460 /* Handle an alarm once each second and read pending input
1461 so as to handle a C-g if it comces in. */
1463 SIGTYPE
1464 input_poll_signal (signalnum) /* If we don't have an argument, */
1465 int signalnum; /* some compilers complain in signal calls. */
1467 if (interrupt_input_blocked == 0
1468 && !waiting_for_input)
1469 read_avail_input (0);
1470 signal (SIGALRM, input_poll_signal);
1471 alarm (polling_period);
1474 #endif
1476 /* Begin signals to poll for input, if they are appropriate.
1477 This function is called unconditionally from various places. */
1479 start_polling ()
1481 #ifdef POLL_FOR_INPUT
1482 if (read_socket_hook && !interrupt_input)
1484 poll_suppress_count--;
1485 if (poll_suppress_count == 0)
1487 signal (SIGALRM, input_poll_signal);
1488 polling_for_input = 1;
1489 alarm (polling_period);
1492 #endif
1495 /* Nonzero if we are using polling to handle input asynchronously. */
1498 input_polling_used ()
1500 #ifdef POLL_FOR_INPUT
1501 return read_socket_hook && !interrupt_input;
1502 #else
1503 return 0;
1504 #endif
1507 /* Turn off polling. */
1509 stop_polling ()
1511 #ifdef POLL_FOR_INPUT
1512 if (read_socket_hook && !interrupt_input)
1514 if (poll_suppress_count == 0)
1516 polling_for_input = 0;
1517 alarm (0);
1519 poll_suppress_count++;
1521 #endif
1524 /* Set the value of poll_suppress_count to COUNT
1525 and start or stop polling accordingly. */
1527 void
1528 set_poll_suppress_count (count)
1529 int count;
1531 #ifdef POLL_FOR_INPUT
1532 if (count == 0 && poll_suppress_count != 0)
1534 poll_suppress_count = 1;
1535 start_polling ();
1537 else if (count != 0 && poll_suppress_count == 0)
1539 stop_polling ();
1541 poll_suppress_count = count;
1542 #endif
1545 /* Bind polling_period to a value at least N.
1546 But don't decrease it. */
1548 bind_polling_period (n)
1549 int n;
1551 #ifdef POLL_FOR_INPUT
1552 int new = polling_period;
1554 if (n > new)
1555 new = n;
1557 stop_polling ();
1558 specbind (Qpolling_period, make_number (new));
1559 /* Start a new alarm with the new period. */
1560 start_polling ();
1561 #endif
1564 /* Apply the control modifier to CHARACTER. */
1567 make_ctrl_char (c)
1568 int c;
1570 /* Save the upper bits here. */
1571 int upper = c & ~0177;
1573 c &= 0177;
1575 /* Everything in the columns containing the upper-case letters
1576 denotes a control character. */
1577 if (c >= 0100 && c < 0140)
1579 int oc = c;
1580 c &= ~0140;
1581 /* Set the shift modifier for a control char
1582 made from a shifted letter. But only for letters! */
1583 if (oc >= 'A' && oc <= 'Z')
1584 c |= shift_modifier;
1587 /* The lower-case letters denote control characters too. */
1588 else if (c >= 'a' && c <= 'z')
1589 c &= ~0140;
1591 /* Include the bits for control and shift
1592 only if the basic ASCII code can't indicate them. */
1593 else if (c >= ' ')
1594 c |= ctrl_modifier;
1596 /* Replace the high bits. */
1597 c |= (upper & ~ctrl_modifier);
1599 return c;
1604 /* Input of single characters from keyboard */
1606 Lisp_Object print_help ();
1607 static Lisp_Object kbd_buffer_get_event ();
1608 static void record_char ();
1610 #ifdef MULTI_KBOARD
1611 static jmp_buf wrong_kboard_jmpbuf;
1612 #endif
1614 /* read a character from the keyboard; call the redisplay if needed */
1615 /* commandflag 0 means do not do auto-saving, but do do redisplay.
1616 -1 means do not do redisplay, but do do autosaving.
1617 1 means do both. */
1619 /* The arguments MAPS and NMAPS are for menu prompting.
1620 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1622 PREV_EVENT is the previous input event, or nil if we are reading
1623 the first event of a key sequence.
1625 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
1626 if we used a mouse menu to read the input, or zero otherwise. If
1627 USED_MOUSE_MENU is null, we don't dereference it.
1629 Value is t if we showed a menu and the user rejected it. */
1631 Lisp_Object
1632 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
1633 int commandflag;
1634 int nmaps;
1635 Lisp_Object *maps;
1636 Lisp_Object prev_event;
1637 int *used_mouse_menu;
1639 register Lisp_Object c;
1640 int count;
1641 jmp_buf local_getcjmp;
1642 jmp_buf save_jump;
1643 int key_already_recorded = 0;
1644 Lisp_Object tem, save;
1645 Lisp_Object also_record;
1646 also_record = Qnil;
1648 before_command_key_count = this_command_key_count;
1649 before_command_echo_length = echo_length ();
1651 retry:
1653 if (CONSP (Vunread_command_events))
1655 c = XCONS (Vunread_command_events)->car;
1656 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
1658 if (this_command_key_count == 0)
1659 goto reread_first;
1660 else
1661 goto reread;
1664 if (unread_command_char != -1)
1666 XSETINT (c, unread_command_char);
1667 unread_command_char = -1;
1669 if (this_command_key_count == 0)
1670 goto reread_first;
1671 else
1672 goto reread;
1675 /* If there is no function key translated before
1676 reset-this-command-lengths takes effect, forget about it. */
1677 before_command_restore_flag = 0;
1679 if (!NILP (Vexecuting_macro))
1681 #ifdef MULTI_FRAME
1682 /* We set this to Qmacro; since that's not a frame, nobody will
1683 try to switch frames on us, and the selected window will
1684 remain unchanged.
1686 Since this event came from a macro, it would be misleading to
1687 leave internal_last_event_frame set to wherever the last
1688 real event came from. Normally, a switch-frame event selects
1689 internal_last_event_frame after each command is read, but
1690 events read from a macro should never cause a new frame to be
1691 selected. */
1692 Vlast_event_frame = internal_last_event_frame = Qmacro;
1693 #endif
1695 /* Exit the macro if we are at the end.
1696 Also, some things replace the macro with t
1697 to force an early exit. */
1698 if (EQ (Vexecuting_macro, Qt)
1699 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
1701 XSETINT (c, -1);
1702 return c;
1705 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
1706 if (STRINGP (Vexecuting_macro)
1707 && (XINT (c) & 0x80))
1708 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
1710 executing_macro_index++;
1712 goto from_macro;
1715 if (!NILP (unread_switch_frame))
1717 c = unread_switch_frame;
1718 unread_switch_frame = Qnil;
1720 /* This event should make it into this_command_keys, and get echoed
1721 again, so we go to reread_first, rather than reread. */
1722 goto reread_first;
1725 if (commandflag >= 0 && !input_pending && !detect_input_pending ())
1726 redisplay ();
1728 /* Message turns off echoing unless more keystrokes turn it on again. */
1729 if (echo_area_glyphs && *echo_area_glyphs
1730 && echo_area_glyphs != current_kboard->echobuf)
1731 cancel_echoing ();
1732 else
1733 /* If already echoing, continue. */
1734 echo_dash ();
1736 /* Try reading a character via menu prompting in the minibuf.
1737 Try this before the sit-for, because the sit-for
1738 would do the wrong thing if we are supposed to do
1739 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
1740 after a mouse event so don't try a minibuf menu. */
1741 c = Qnil;
1742 if (nmaps > 0 && INTERACTIVE
1743 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
1744 /* Don't bring up a menu if we already have another event. */
1745 && NILP (Vunread_command_events)
1746 && unread_command_char < 0
1747 && !detect_input_pending ())
1749 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
1750 if (! NILP (c))
1752 key_already_recorded = 1;
1753 goto non_reread_1;
1757 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
1758 We will do that below, temporarily for short sections of code,
1759 when appropriate. local_getcjmp must be in effect
1760 around any call to sit_for or kbd_buffer_get_event;
1761 it *must not* be in effect when we call redisplay. */
1763 if (_setjmp (local_getcjmp))
1765 XSETINT (c, quit_char);
1766 #ifdef MULTI_FRAME
1767 XSETFRAME (internal_last_event_frame, selected_frame);
1768 Vlast_event_frame = internal_last_event_frame;
1769 #endif
1770 /* If we report the quit char as an event,
1771 don't do so more than once. */
1772 if (!NILP (Vinhibit_quit))
1773 Vquit_flag = Qnil;
1775 #ifdef MULTI_KBOARD
1777 KBOARD *kb = FRAME_KBOARD (selected_frame);
1778 if (kb != current_kboard)
1780 Lisp_Object *tailp = &kb->kbd_queue;
1781 /* We shouldn't get here if we were in single-kboard mode! */
1782 if (single_kboard)
1783 abort ();
1784 while (CONSP (*tailp))
1785 tailp = &XCONS (*tailp)->cdr;
1786 if (!NILP (*tailp))
1787 abort ();
1788 *tailp = Fcons (c, Qnil);
1789 kb->kbd_queue_has_data = 1;
1790 current_kboard = kb;
1791 longjmp (wrong_kboard_jmpbuf, 1);
1794 #endif
1795 goto non_reread;
1798 /* If in middle of key sequence and minibuffer not active,
1799 start echoing if enough time elapses. */
1801 if (minibuf_level == 0 && !current_kboard->immediate_echo
1802 && this_command_key_count > 0
1803 && ! noninteractive
1804 && echo_keystrokes > 0
1805 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0))
1807 Lisp_Object tem0;
1809 /* After a mouse event, start echoing right away.
1810 This is because we are probably about to display a menu,
1811 and we don't want to delay before doing so. */
1812 if (EVENT_HAS_PARAMETERS (prev_event))
1813 echo ();
1814 else
1816 save_getcjmp (save_jump);
1817 restore_getcjmp (local_getcjmp);
1818 tem0 = sit_for (echo_keystrokes, 0, 1, 1);
1819 restore_getcjmp (save_jump);
1820 if (EQ (tem0, Qt))
1821 echo ();
1825 /* Maybe auto save due to number of keystrokes. */
1827 if (commandflag != 0
1828 && auto_save_interval > 0
1829 && num_nonmacro_input_chars - last_auto_save > max (auto_save_interval, 20)
1830 && !detect_input_pending ())
1832 Fdo_auto_save (Qnil, Qnil);
1833 /* Hooks can actually change some buffers in auto save. */
1834 redisplay ();
1837 /* Try reading using an X menu.
1838 This is never confused with reading using the minibuf
1839 because the recursive call of read_char in read_char_minibuf_menu_prompt
1840 does not pass on any keymaps. */
1842 if (nmaps > 0 && INTERACTIVE
1843 && !NILP (prev_event)
1844 && EVENT_HAS_PARAMETERS (prev_event)
1845 && !EQ (XCONS (prev_event)->car, Qmenu_bar)
1846 /* Don't bring up a menu if we already have another event. */
1847 && NILP (Vunread_command_events)
1848 && unread_command_char < 0)
1849 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
1851 /* Maybe autosave and/or garbage collect due to idleness. */
1853 if (INTERACTIVE && NILP (c))
1855 int delay_level, buffer_size;
1857 /* Slow down auto saves logarithmically in size of current buffer,
1858 and garbage collect while we're at it. */
1859 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
1860 last_non_minibuf_size = Z - BEG;
1861 buffer_size = (last_non_minibuf_size >> 8) + 1;
1862 delay_level = 0;
1863 while (buffer_size > 64)
1864 delay_level++, buffer_size -= buffer_size >> 2;
1865 if (delay_level < 4) delay_level = 4;
1866 /* delay_level is 4 for files under around 50k, 7 at 100k,
1867 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
1869 /* Auto save if enough time goes by without input. */
1870 if (commandflag != 0
1871 && num_nonmacro_input_chars > last_auto_save
1872 && INTEGERP (Vauto_save_timeout)
1873 && XINT (Vauto_save_timeout) > 0)
1875 Lisp_Object tem0;
1876 int delay = delay_level * XFASTINT (Vauto_save_timeout) / 4;
1878 save_getcjmp (save_jump);
1879 restore_getcjmp (local_getcjmp);
1880 tem0 = sit_for (delay, 0, 1, 1);
1881 restore_getcjmp (save_jump);
1883 if (EQ (tem0, Qt))
1885 Fdo_auto_save (Qnil, Qnil);
1887 /* If we have auto-saved and there is still no input
1888 available, garbage collect if there has been enough
1889 consing going on to make it worthwhile. */
1890 if (!detect_input_pending ()
1891 && consing_since_gc > gc_cons_threshold / 2)
1892 Fgarbage_collect ();
1894 redisplay ();
1899 /* Read something from current KBOARD's side queue, if possible. */
1901 if (NILP (c))
1903 if (current_kboard->kbd_queue_has_data)
1905 if (!CONSP (current_kboard->kbd_queue))
1906 abort ();
1907 c = XCONS (current_kboard->kbd_queue)->car;
1908 current_kboard->kbd_queue
1909 = XCONS (current_kboard->kbd_queue)->cdr;
1910 if (NILP (current_kboard->kbd_queue))
1911 current_kboard->kbd_queue_has_data = 0;
1912 input_pending = readable_events ();
1913 #ifdef MULTI_FRAME
1914 if (EVENT_HAS_PARAMETERS (c)
1915 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
1916 internal_last_event_frame = XCONS (XCONS (c)->cdr)->car;
1917 Vlast_event_frame = internal_last_event_frame;
1918 #endif
1922 #ifdef MULTI_KBOARD
1923 /* If current_kboard's side queue is empty check the other kboards.
1924 If one of them has data that we have not yet seen here,
1925 switch to it and process the data waiting for it.
1927 Note: if the events queued up for another kboard
1928 have already been seen here, and therefore are not a complete command,
1929 the kbd_queue_has_data field is 0, so we skip that kboard here.
1930 That's to avoid an infinite loop switching between kboards here. */
1931 if (NILP (c) && !single_kboard)
1933 KBOARD *kb;
1934 for (kb = all_kboards; kb; kb = kb->next_kboard)
1935 if (kb->kbd_queue_has_data)
1937 current_kboard = kb;
1938 longjmp (wrong_kboard_jmpbuf, 1);
1941 #endif
1943 wrong_kboard:
1945 stop_polling ();
1947 /* Finally, we read from the main queue,
1948 and if that gives us something we can't use yet, we put it on the
1949 appropriate side queue and try again. */
1951 if (NILP (c))
1953 KBOARD *kb;
1955 /* Actually read a character, waiting if necessary. */
1956 save_getcjmp (save_jump);
1957 restore_getcjmp (local_getcjmp);
1958 c = kbd_buffer_get_event (&kb, used_mouse_menu);
1959 restore_getcjmp (save_jump);
1961 #ifdef MULTI_KBOARD
1962 if (! NILP (c) && (kb != current_kboard))
1964 Lisp_Object *tailp = &kb->kbd_queue;
1965 while (CONSP (*tailp))
1966 tailp = &XCONS (*tailp)->cdr;
1967 if (!NILP (*tailp))
1968 abort ();
1969 *tailp = Fcons (c, Qnil);
1970 kb->kbd_queue_has_data = 1;
1971 c = Qnil;
1972 if (single_kboard)
1973 goto wrong_kboard;
1974 current_kboard = kb;
1975 longjmp (wrong_kboard_jmpbuf, 1);
1977 #endif
1980 /* Terminate Emacs in batch mode if at eof. */
1981 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
1982 Fkill_emacs (make_number (1));
1984 if (INTEGERP (c))
1986 /* Add in any extra modifiers, where appropriate. */
1987 if ((extra_keyboard_modifiers & CHAR_CTL)
1988 || ((extra_keyboard_modifiers & 0177) < ' '
1989 && (extra_keyboard_modifiers & 0177) != 0))
1990 XSETINT (c, make_ctrl_char (XINT (c)));
1992 /* Transfer any other modifier bits directly from
1993 extra_keyboard_modifiers to c. Ignore the actual character code
1994 in the low 16 bits of extra_keyboard_modifiers. */
1995 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
1998 non_reread:
2000 start_polling ();
2002 if (NILP (c))
2004 if (commandflag >= 0
2005 && !input_pending && !detect_input_pending ())
2006 redisplay ();
2008 goto wrong_kboard;
2011 non_reread_1:
2013 /* Buffer switch events are only for internal wakeups
2014 so don't show them to the user. */
2015 if (BUFFERP (c))
2016 return c;
2018 if (key_already_recorded)
2019 return c;
2021 /* Process special events within read_char
2022 and loop around to read another event. */
2023 save = Vquit_flag;
2024 Vquit_flag = Qnil;
2025 tem = get_keyelt (access_keymap (get_keymap_1 (Vspecial_event_map, 0, 0),
2026 c, 0, 0), 1);
2027 Vquit_flag = save;
2029 if (!NILP (tem))
2031 last_input_char = c;
2032 Fcommand_execute (tem, Qnil);
2033 goto retry;
2036 /* Wipe the echo area. */
2037 echo_area_glyphs = 0;
2039 /* Handle things that only apply to characters. */
2040 if (INTEGERP (c))
2042 /* If kbd_buffer_get_event gave us an EOF, return that. */
2043 if (XINT (c) == -1)
2044 return c;
2046 if (STRINGP (Vkeyboard_translate_table)
2047 && XSTRING (Vkeyboard_translate_table)->size > XFASTINT (c))
2048 XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[XFASTINT (c)]);
2051 /* If this event is a mouse click in the menu bar,
2052 return just menu-bar for now. Modify the mouse click event
2053 so we won't do this twice, then queue it up. */
2054 if (EVENT_HAS_PARAMETERS (c)
2055 && CONSP (XCONS (c)->cdr)
2056 && CONSP (EVENT_START (c))
2057 && CONSP (XCONS (EVENT_START (c))->cdr))
2059 Lisp_Object posn;
2061 posn = POSN_BUFFER_POSN (EVENT_START (c));
2062 /* Handle menu-bar events:
2063 insert the dummy prefix event `menu-bar'. */
2064 if (EQ (posn, Qmenu_bar))
2066 /* Change menu-bar to (menu-bar) as the event "position". */
2067 POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
2069 also_record = c;
2070 Vunread_command_events = Fcons (c, Vunread_command_events);
2071 c = posn;
2075 record_char (c);
2076 if (! NILP (also_record))
2077 record_char (also_record);
2079 from_macro:
2080 reread_first:
2081 before_command_key_count = this_command_key_count;
2082 before_command_echo_length = echo_length ();
2084 /* Don't echo mouse motion events. */
2085 if (echo_keystrokes
2086 && ! (EVENT_HAS_PARAMETERS (c)
2087 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
2089 echo_char (c);
2090 if (! NILP (also_record))
2091 echo_char (also_record);
2094 /* Record this character as part of the current key. */
2095 add_command_key (c);
2096 if (! NILP (also_record))
2097 add_command_key (also_record);
2099 /* Re-reading in the middle of a command */
2100 reread:
2101 last_input_char = c;
2102 num_input_chars++;
2104 /* Process the help character specially if enabled */
2105 if (!NILP (Vhelp_form) && help_char_p (c))
2107 Lisp_Object tem0;
2108 count = specpdl_ptr - specpdl;
2110 record_unwind_protect (Fset_window_configuration,
2111 Fcurrent_window_configuration (Qnil));
2113 tem0 = Feval (Vhelp_form);
2114 if (STRINGP (tem0))
2115 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
2117 cancel_echoing ();
2119 c = read_char (0, 0, 0, Qnil, 0);
2120 while (BUFFERP (c));
2121 /* Remove the help from the frame */
2122 unbind_to (count, Qnil);
2124 redisplay ();
2125 if (EQ (c, make_number (040)))
2127 cancel_echoing ();
2129 c = read_char (0, 0, 0, Qnil, 0);
2130 while (BUFFERP (c));
2134 return c;
2137 /* Return 1 if should recognize C as "the help character". */
2140 help_char_p (c)
2141 Lisp_Object c;
2143 Lisp_Object tail;
2145 if (EQ (c, Vhelp_char))
2146 return 1;
2147 for (tail = Vhelp_event_list; CONSP (tail); tail = XCONS (tail)->cdr)
2148 if (EQ (c, XCONS (tail)->car))
2149 return 1;
2150 return 0;
2153 /* Record the input event C in various ways. */
2155 static void
2156 record_char (c)
2157 Lisp_Object c;
2159 total_keys++;
2160 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
2161 if (++recent_keys_index >= NUM_RECENT_KEYS)
2162 recent_keys_index = 0;
2164 /* Write c to the dribble file. If c is a lispy event, write
2165 the event's symbol to the dribble file, in <brackets>. Bleaugh.
2166 If you, dear reader, have a better idea, you've got the source. :-) */
2167 if (dribble)
2169 if (INTEGERP (c))
2171 if (XUINT (c) < 0x100)
2172 putc (XINT (c), dribble);
2173 else
2174 fprintf (dribble, " 0x%x", (int) XUINT (c));
2176 else
2178 Lisp_Object dribblee;
2180 /* If it's a structured event, take the event header. */
2181 dribblee = EVENT_HEAD (c);
2183 if (SYMBOLP (dribblee))
2185 putc ('<', dribble);
2186 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
2187 XSYMBOL (dribblee)->name->size,
2188 dribble);
2189 putc ('>', dribble);
2193 fflush (dribble);
2196 store_kbd_macro_char (c);
2198 num_nonmacro_input_chars++;
2201 Lisp_Object
2202 print_help (object)
2203 Lisp_Object object;
2205 struct buffer *old = current_buffer;
2206 Fprinc (object, Qnil);
2207 set_buffer_internal (XBUFFER (Vstandard_output));
2208 call0 (intern ("help-mode"));
2209 set_buffer_internal (old);
2210 return Qnil;
2213 /* Copy out or in the info on where C-g should throw to.
2214 This is used when running Lisp code from within get_char,
2215 in case get_char is called recursively.
2216 See read_process_output. */
2218 save_getcjmp (temp)
2219 jmp_buf temp;
2221 bcopy (getcjmp, temp, sizeof getcjmp);
2224 restore_getcjmp (temp)
2225 jmp_buf temp;
2227 bcopy (temp, getcjmp, sizeof getcjmp);
2230 #ifdef HAVE_MOUSE
2232 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
2233 of this function. */
2235 static Lisp_Object
2236 tracking_off (old_value)
2237 Lisp_Object old_value;
2239 do_mouse_tracking = old_value;
2240 if (NILP (old_value))
2242 /* Redisplay may have been preempted because there was input
2243 available, and it assumes it will be called again after the
2244 input has been processed. If the only input available was
2245 the sort that we have just disabled, then we need to call
2246 redisplay. */
2247 if (!readable_events ())
2249 redisplay_preserve_echo_area ();
2250 get_input_pending (&input_pending);
2255 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
2256 "Evaluate BODY with mouse movement events enabled.\n\
2257 Within a `track-mouse' form, mouse motion generates input events that\n\
2258 you can read with `read-event'.\n\
2259 Normally, mouse motion is ignored.")
2260 (args)
2261 Lisp_Object args;
2263 int count = specpdl_ptr - specpdl;
2264 Lisp_Object val;
2266 record_unwind_protect (tracking_off, do_mouse_tracking);
2268 do_mouse_tracking = Qt;
2270 val = Fprogn (args);
2271 return unbind_to (count, val);
2274 /* If mouse has moved on some frame, return one of those frames.
2275 Return 0 otherwise. */
2277 static FRAME_PTR
2278 some_mouse_moved ()
2280 Lisp_Object tail, frame;
2282 FOR_EACH_FRAME (tail, frame)
2284 if (XFRAME (frame)->mouse_moved)
2285 return XFRAME (frame);
2288 return 0;
2291 #endif /* HAVE_MOUSE */
2293 /* Low level keyboard/mouse input.
2294 kbd_buffer_store_event places events in kbd_buffer, and
2295 kbd_buffer_get_event retrieves them.
2296 mouse_moved indicates when the mouse has moved again, and
2297 *mouse_position_hook provides the mouse position. */
2299 /* Return true iff there are any events in the queue that read-char
2300 would return. If this returns false, a read-char would block. */
2301 static int
2302 readable_events ()
2304 if (kbd_fetch_ptr != kbd_store_ptr)
2305 return 1;
2306 #ifdef HAVE_MOUSE
2307 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2308 return 1;
2309 #endif
2310 if (single_kboard)
2312 if (current_kboard->kbd_queue_has_data)
2313 return 1;
2315 else
2317 KBOARD *kb;
2318 for (kb = all_kboards; kb; kb = kb->next_kboard)
2319 if (kb->kbd_queue_has_data)
2320 return 1;
2322 return 0;
2325 /* Set this for debugging, to have a way to get out */
2326 int stop_character;
2328 #ifdef MULTI_KBOARD
2329 static KBOARD *
2330 event_to_kboard (event)
2331 struct input_event *event;
2333 Lisp_Object frame;
2334 frame = event->frame_or_window;
2335 if (CONSP (frame))
2336 frame = XCONS (frame)->car;
2337 else if (WINDOWP (frame))
2338 frame = WINDOW_FRAME (XWINDOW (frame));
2340 /* There are still some events that don't set this field.
2341 For now, just ignore the problem.
2342 Also ignore dead frames here. */
2343 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
2344 return 0;
2345 else
2346 return FRAME_KBOARD (XFRAME (frame));
2348 #endif
2350 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
2352 void
2353 kbd_buffer_store_event (event)
2354 register struct input_event *event;
2356 if (event->kind == no_event)
2357 abort ();
2359 if (event->kind == ascii_keystroke)
2361 register int c = event->code & 0377;
2363 if (event->modifiers & ctrl_modifier)
2364 c = make_ctrl_char (c);
2366 c |= (event->modifiers
2367 & (meta_modifier | alt_modifier
2368 | hyper_modifier | super_modifier));
2370 if (c == quit_char)
2372 extern SIGTYPE interrupt_signal ();
2373 #ifdef MULTI_KBOARD
2374 KBOARD *kb;
2375 struct input_event *sp;
2377 if (single_kboard
2378 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
2379 kb != current_kboard))
2381 kb->kbd_queue
2382 = Fcons (make_lispy_switch_frame (event->frame_or_window),
2383 Fcons (make_number (c), Qnil));
2384 kb->kbd_queue_has_data = 1;
2385 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
2387 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
2388 sp = kbd_buffer;
2390 if (event_to_kboard (sp) == kb)
2392 sp->kind = no_event;
2393 sp->frame_or_window = Qnil;
2396 return;
2398 #endif
2400 #ifdef MULTI_FRAME
2401 /* If this results in a quit_char being returned to Emacs as
2402 input, set Vlast_event_frame properly. If this doesn't
2403 get returned to Emacs as an event, the next event read
2404 will set Vlast_event_frame again, so this is safe to do. */
2406 Lisp_Object focus;
2408 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
2409 if (NILP (focus))
2410 focus = event->frame_or_window;
2411 internal_last_event_frame = focus;
2412 Vlast_event_frame = focus;
2414 #endif
2416 last_event_timestamp = event->timestamp;
2417 interrupt_signal ();
2418 return;
2421 if (c && c == stop_character)
2423 sys_suspend ();
2424 return;
2428 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
2429 kbd_store_ptr = kbd_buffer;
2431 /* Don't let the very last slot in the buffer become full,
2432 since that would make the two pointers equal,
2433 and that is indistinguishable from an empty buffer.
2434 Discard the event if it would fill the last slot. */
2435 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
2437 volatile struct input_event *sp = kbd_store_ptr;
2438 sp->kind = event->kind;
2439 if (event->kind == selection_request_event)
2441 /* We must not use the ordinary copying code for this case,
2442 since `part' is an enum and copying it might not copy enough
2443 in this case. */
2444 bcopy (event, (char *) sp, sizeof (*event));
2446 else
2448 sp->code = event->code;
2449 sp->part = event->part;
2450 sp->frame_or_window = event->frame_or_window;
2451 sp->modifiers = event->modifiers;
2452 sp->x = event->x;
2453 sp->y = event->y;
2454 sp->timestamp = event->timestamp;
2456 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
2457 - kbd_buffer]
2458 = event->frame_or_window);
2460 kbd_store_ptr++;
2464 /* Read one event from the event buffer, waiting if necessary.
2465 The value is a Lisp object representing the event.
2466 The value is nil for an event that should be ignored,
2467 or that was handled here.
2468 We always read and discard one event. */
2470 static Lisp_Object
2471 kbd_buffer_get_event (kbp, used_mouse_menu)
2472 KBOARD **kbp;
2473 int *used_mouse_menu;
2475 register int c;
2476 Lisp_Object obj;
2478 if (noninteractive)
2480 c = getchar ();
2481 XSETINT (obj, c);
2482 *kbp = current_kboard;
2483 return obj;
2486 /* Wait until there is input available. */
2487 for (;;)
2489 if (kbd_fetch_ptr != kbd_store_ptr)
2490 break;
2491 #ifdef HAVE_MOUSE
2492 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2493 break;
2494 #endif
2496 /* If the quit flag is set, then read_char will return
2497 quit_char, so that counts as "available input." */
2498 if (!NILP (Vquit_flag))
2499 quit_throw_to_read_char ();
2501 /* One way or another, wait until input is available; then, if
2502 interrupt handlers have not read it, read it now. */
2504 #ifdef OLDVMS
2505 wait_for_kbd_input ();
2506 #else
2507 /* Note SIGIO has been undef'd if FIONREAD is missing. */
2508 #ifdef SIGIO
2509 gobble_input (0);
2510 #endif /* SIGIO */
2511 if (kbd_fetch_ptr != kbd_store_ptr)
2512 break;
2513 #ifdef HAVE_MOUSE
2514 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2515 break;
2516 #endif
2518 Lisp_Object minus_one;
2520 XSETINT (minus_one, -1);
2521 wait_reading_process_input (0, 0, minus_one, 1);
2523 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
2524 /* Pass 1 for EXPECT since we just waited to have input. */
2525 read_avail_input (1);
2527 #endif /* not VMS */
2530 /* At this point, we know that there is a readable event available
2531 somewhere. If the event queue is empty, then there must be a
2532 mouse movement enabled and available. */
2533 if (kbd_fetch_ptr != kbd_store_ptr)
2535 struct input_event *event;
2537 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2538 ? kbd_fetch_ptr
2539 : kbd_buffer);
2541 last_event_timestamp = event->timestamp;
2543 #ifdef MULTI_KBOARD
2544 *kbp = event_to_kboard (event);
2545 if (*kbp == 0)
2546 *kbp = current_kboard; /* Better than returning null ptr? */
2547 #else
2548 *kbp = &the_only_kboard;
2549 #endif
2551 obj = Qnil;
2553 /* These two kinds of events get special handling
2554 and don't actually appear to the command loop.
2555 We return nil for them. */
2556 if (event->kind == selection_request_event)
2558 #ifdef HAVE_X11
2559 struct input_event copy;
2561 /* Remove it from the buffer before processing it,
2562 since otherwise swallow_events will see it
2563 and process it again. */
2564 copy = *event;
2565 kbd_fetch_ptr = event + 1;
2566 x_handle_selection_request (&copy);
2567 #else
2568 /* We're getting selection request events, but we don't have
2569 a window system. */
2570 abort ();
2571 #endif
2574 else if (event->kind == selection_clear_event)
2576 #ifdef HAVE_X11
2577 kbd_fetch_ptr = event + 1;
2578 x_handle_selection_clear (event);
2579 #else
2580 /* We're getting selection request events, but we don't have
2581 a window system. */
2582 abort ();
2583 #endif
2585 #ifdef HAVE_X11
2586 else if (event->kind == delete_window_event)
2588 /* Make an event (delete-frame (FRAME)). */
2589 obj = Fcons (event->frame_or_window, Qnil);
2590 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
2591 kbd_fetch_ptr = event + 1;
2593 else if (event->kind == iconify_event)
2595 /* Make an event (iconify-frame (FRAME)). */
2596 obj = Fcons (event->frame_or_window, Qnil);
2597 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
2598 kbd_fetch_ptr = event + 1;
2600 else if (event->kind == deiconify_event)
2602 /* Make an event (make-frame-visible (FRAME)). */
2603 obj = Fcons (event->frame_or_window, Qnil);
2604 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
2605 kbd_fetch_ptr = event + 1;
2607 #endif
2608 else if (event->kind == buffer_switch_event)
2610 /* The value doesn't matter here; only the type is tested. */
2611 XSETBUFFER (obj, current_buffer);
2612 kbd_fetch_ptr = event + 1;
2614 #ifdef USE_X_TOOLKIT
2615 else if (event->kind == menu_bar_activate_event)
2617 kbd_fetch_ptr = event + 1;
2618 x_activate_menubar (XFRAME (event->frame_or_window));
2620 #endif
2621 /* Just discard these, by returning nil.
2622 With MULTI_KBOARD, these events are used as placeholders
2623 when we need to randomly delete events from the queue.
2624 (They shouldn't otherwise be found in the buffer,
2625 but on some machines it appears they do show up
2626 even without MULTI_KBOARD.) */
2627 else if (event->kind == no_event)
2628 kbd_fetch_ptr = event + 1;
2630 /* If this event is on a different frame, return a switch-frame this
2631 time, and leave the event in the queue for next time. */
2632 else
2634 #ifdef MULTI_FRAME
2635 Lisp_Object frame;
2636 Lisp_Object focus;
2638 frame = event->frame_or_window;
2639 if (CONSP (frame))
2640 frame = XCONS (frame)->car;
2641 else if (WINDOWP (frame))
2642 frame = WINDOW_FRAME (XWINDOW (frame));
2644 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
2645 if (! NILP (focus))
2646 frame = focus;
2648 if (! EQ (frame, internal_last_event_frame)
2649 && XFRAME (frame) != selected_frame)
2650 obj = make_lispy_switch_frame (frame);
2651 internal_last_event_frame = frame;
2652 #endif /* MULTI_FRAME */
2654 /* If we didn't decide to make a switch-frame event, go ahead
2655 and build a real event from the queue entry. */
2657 if (NILP (obj))
2659 obj = make_lispy_event (event);
2660 #ifdef USE_X_TOOLKIT
2661 /* If this was a menu selection, then set the flag to inhibit
2662 writing to last_nonmenu_event. Don't do this if the event
2663 we're returning is (menu-bar), though; that indicates the
2664 beginning of the menu sequence, and we might as well leave
2665 that as the `event with parameters' for this selection. */
2666 if (event->kind == menu_bar_event
2667 && !(CONSP (obj) && EQ (XCONS (obj)->car, Qmenu_bar))
2668 && used_mouse_menu)
2669 *used_mouse_menu = 1;
2670 #endif
2672 /* Wipe out this event, to catch bugs. */
2673 event->kind = no_event;
2674 XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer] = Qnil;
2676 kbd_fetch_ptr = event + 1;
2680 #ifdef HAVE_MOUSE
2681 /* Try generating a mouse motion event. */
2682 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
2684 FRAME_PTR f = some_mouse_moved ();
2685 Lisp_Object bar_window;
2686 enum scroll_bar_part part;
2687 Lisp_Object x, y;
2688 unsigned long time;
2690 *kbp = current_kboard;
2691 /* Note that this uses F to determine which display to look at.
2692 If there is no valid info, it does not store anything
2693 so x remains nil. */
2694 x = Qnil;
2695 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
2697 obj = Qnil;
2699 #ifdef MULTI_FRAME
2700 /* Decide if we should generate a switch-frame event. Don't
2701 generate switch-frame events for motion outside of all Emacs
2702 frames. */
2703 if (!NILP (x) && f)
2705 Lisp_Object frame;
2707 frame = FRAME_FOCUS_FRAME (f);
2708 if (NILP (frame))
2709 XSETFRAME (frame, f);
2711 if (! EQ (frame, internal_last_event_frame)
2712 && XFRAME (frame) != selected_frame)
2713 obj = make_lispy_switch_frame (frame);
2714 internal_last_event_frame = frame;
2716 #endif
2718 /* If we didn't decide to make a switch-frame event, go ahead and
2719 return a mouse-motion event. */
2720 if (!NILP (x) && NILP (obj))
2721 obj = make_lispy_movement (f, bar_window, part, x, y, time);
2723 #endif /* HAVE_MOUSE */
2724 else
2725 /* We were promised by the above while loop that there was
2726 something for us to read! */
2727 abort ();
2729 input_pending = readable_events ();
2731 #ifdef MULTI_FRAME
2732 Vlast_event_frame = internal_last_event_frame;
2733 #endif
2735 return (obj);
2738 /* Process any events that are not user-visible,
2739 then return, without reading any user-visible events. */
2741 void
2742 swallow_events ()
2744 while (kbd_fetch_ptr != kbd_store_ptr)
2746 struct input_event *event;
2748 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2749 ? kbd_fetch_ptr
2750 : kbd_buffer);
2752 last_event_timestamp = event->timestamp;
2754 /* These two kinds of events get special handling
2755 and don't actually appear to the command loop. */
2756 if (event->kind == selection_request_event)
2758 #ifdef HAVE_X11
2759 struct input_event copy;
2760 copy = *event;
2761 kbd_fetch_ptr = event + 1;
2762 x_handle_selection_request (&copy);
2763 #else
2764 /* We're getting selection request events, but we don't have
2765 a window system. */
2766 abort ();
2767 #endif
2770 else if (event->kind == selection_clear_event)
2772 #ifdef HAVE_X11
2773 x_handle_selection_clear (event);
2774 kbd_fetch_ptr = event + 1;
2775 #else
2776 /* We're getting selection request events, but we don't have
2777 a window system. */
2778 abort ();
2779 #endif
2781 else
2782 break;
2785 get_input_pending (&input_pending);
2788 /* Caches for modify_event_symbol. */
2789 static Lisp_Object accent_key_syms;
2790 static Lisp_Object func_key_syms;
2791 static Lisp_Object mouse_syms;
2793 /* This is a list of keysym codes for special "accent" characters.
2794 It parallels lispy_accent_keys. */
2796 static int lispy_accent_codes[] =
2798 #ifdef XK_dead_circumflex
2799 XK_dead_circumflex,
2800 #else
2802 #endif
2803 #ifdef XK_dead_grave
2804 XK_dead_grave,
2805 #else
2807 #endif
2808 #ifdef XK_dead_tilde
2809 XK_dead_tilde,
2810 #else
2812 #endif
2813 #ifdef XK_dead_diaeresis
2814 XK_dead_diaeresis,
2815 #else
2817 #endif
2818 #ifdef XK_dead_macron
2819 XK_dead_macron,
2820 #else
2822 #endif
2823 #ifdef XK_dead_degree
2824 XK_dead_degree,
2825 #else
2827 #endif
2828 #ifdef XK_dead_acute
2829 XK_dead_acute,
2830 #else
2832 #endif
2833 #ifdef XK_dead_cedilla
2834 XK_dead_cedilla,
2835 #else
2837 #endif
2838 #ifdef XK_dead_breve
2839 XK_dead_breve,
2840 #else
2842 #endif
2843 #ifdef XK_dead_ogonek
2844 XK_dead_ogonek,
2845 #else
2847 #endif
2848 #ifdef XK_dead_caron
2849 XK_dead_caron,
2850 #else
2852 #endif
2853 #ifdef XK_dead_doubleacute
2854 XK_dead_doubleacute,
2855 #else
2857 #endif
2858 #ifdef XK_dead_abovedot
2859 XK_dead_abovedot,
2860 #else
2862 #endif
2865 /* This is a list of Lisp names for special "accent" characters.
2866 It parallels lispy_accent_codes. */
2868 static char *lispy_accent_keys[] =
2870 "dead-circumflex",
2871 "dead-grave",
2872 "dead-tilde",
2873 "dead-diaeresis",
2874 "dead-macron",
2875 "dead-degree",
2876 "dead-acute",
2877 "dead-cedilla",
2878 "dead-breve",
2879 "dead-ogonek",
2880 "dead-caron",
2881 "dead-doubleacute",
2882 "dead-abovedot",
2885 /* You'll notice that this table is arranged to be conveniently
2886 indexed by X Windows keysym values. */
2887 static char *lispy_function_keys[] =
2889 /* X Keysym value */
2891 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00 */
2892 "backspace",
2893 "tab",
2894 "linefeed",
2895 "clear",
2897 "return",
2898 0, 0,
2899 0, 0, 0, /* 0xff10 */
2900 "pause",
2901 0, 0, 0, 0, 0, 0, 0,
2902 "escape",
2903 0, 0, 0, 0,
2904 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff20...2f */
2905 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff30...3f */
2906 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
2908 "home", /* 0xff50 */ /* IsCursorKey */
2909 "left",
2910 "up",
2911 "right",
2912 "down",
2913 "prior",
2914 "next",
2915 "end",
2916 "begin",
2917 0, /* 0xff59 */
2918 0, 0, 0, 0, 0, 0,
2919 "select", /* 0xff60 */ /* IsMiscFunctionKey */
2920 "print",
2921 "execute",
2922 "insert",
2923 0, /* 0xff64 */
2924 "undo",
2925 "redo",
2926 "menu",
2927 "find",
2928 "cancel",
2929 "help",
2930 "break", /* 0xff6b */
2932 0, 0, 0, 0, 0, 0, 0, 0, "backtab", 0,
2933 0, /* 0xff76 */
2934 0, 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff7f */
2935 "kp-space", /* 0xff80 */ /* IsKeypadKey */
2936 0, 0, 0, 0, 0, 0, 0, 0,
2937 "kp-tab", /* 0xff89 */
2938 0, 0, 0,
2939 "kp-enter", /* 0xff8d */
2940 0, 0, 0,
2941 "kp-f1", /* 0xff91 */
2942 "kp-f2",
2943 "kp-f3",
2944 "kp-f4",
2945 "kp-home", /* 0xff95 */
2946 "kp-left",
2947 "kp-up",
2948 "kp-right",
2949 "kp-down",
2950 "kp-prior", /* kp-page-up */
2951 "kp-next", /* kp-page-down */
2952 "kp-end",
2953 "kp-begin",
2954 "kp-insert",
2955 "kp-delete",
2956 0, /* 0xffa0 */
2957 0, 0, 0, 0, 0, 0, 0, 0, 0,
2958 "kp-multiply", /* 0xffaa */
2959 "kp-add",
2960 "kp-separator",
2961 "kp-subtract",
2962 "kp-decimal",
2963 "kp-divide", /* 0xffaf */
2964 "kp-0", /* 0xffb0 */
2965 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
2966 0, /* 0xffba */
2967 0, 0,
2968 "kp-equal", /* 0xffbd */
2969 "f1", /* 0xffbe */ /* IsFunctionKey */
2970 "f2",
2971 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
2972 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
2973 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
2974 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
2975 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
2976 0, 0, 0, 0, 0, 0, 0, 0,
2977 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
2978 0, 0, 0, 0, 0, 0, 0, "delete"
2981 static char *lispy_mouse_names[] =
2983 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
2986 /* Scroll bar parts. */
2987 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
2989 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
2990 Lisp_Object *scroll_bar_parts[] = {
2991 &Qabove_handle, &Qhandle, &Qbelow_handle
2995 /* A vector, indexed by button number, giving the down-going location
2996 of currently depressed buttons, both scroll bar and non-scroll bar.
2998 The elements have the form
2999 (BUTTON-NUMBER MODIFIER-MASK . REST)
3000 where REST is the cdr of a position as it would be reported in the event.
3002 The make_lispy_event function stores positions here to tell the
3003 difference between click and drag events, and to store the starting
3004 location to be included in drag events. */
3006 static Lisp_Object button_down_location;
3008 /* Information about the most recent up-going button event: Which
3009 button, what location, and what time. */
3011 static int last_mouse_button;
3012 static int last_mouse_x;
3013 static int last_mouse_y;
3014 static unsigned long button_down_time;
3016 /* The maximum time between clicks to make a double-click,
3017 or Qnil to disable double-click detection,
3018 or Qt for no time limit. */
3019 Lisp_Object Vdouble_click_time;
3021 /* The number of clicks in this multiple-click. */
3023 int double_click_count;
3025 /* Given a struct input_event, build the lisp event which represents
3026 it. If EVENT is 0, build a mouse movement event from the mouse
3027 movement buffer, which should have a movement event in it.
3029 Note that events must be passed to this function in the order they
3030 are received; this function stores the location of button presses
3031 in order to build drag events when the button is released. */
3033 static Lisp_Object
3034 make_lispy_event (event)
3035 struct input_event *event;
3037 int i;
3039 switch (SWITCH_ENUM_CAST (event->kind))
3041 /* A simple keystroke. */
3042 case ascii_keystroke:
3044 Lisp_Object lispy_c;
3045 int c = event->code & 0377;
3046 /* Turn ASCII characters into control characters
3047 when proper. */
3048 if (event->modifiers & ctrl_modifier)
3049 c = make_ctrl_char (c);
3051 /* Add in the other modifier bits. We took care of ctrl_modifier
3052 just above, and the shift key was taken care of by the X code,
3053 and applied to control characters by make_ctrl_char. */
3054 c |= (event->modifiers
3055 & (meta_modifier | alt_modifier
3056 | hyper_modifier | super_modifier));
3057 button_down_time = 0;
3058 XSETFASTINT (lispy_c, c);
3059 return lispy_c;
3062 /* A function key. The symbol may need to have modifier prefixes
3063 tacked onto it. */
3064 case non_ascii_keystroke:
3065 button_down_time = 0;
3067 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
3068 if (event->code == lispy_accent_codes[i])
3069 return modify_event_symbol (i,
3070 event->modifiers,
3071 Qfunction_key, Qnil,
3072 lispy_accent_keys, &accent_key_syms,
3073 (sizeof (lispy_accent_keys)
3074 / sizeof (lispy_accent_keys[0])));
3076 /* Handle system-specific keysyms. */
3077 if (event->code & (1 << 28))
3079 /* We need to use an alist rather than a vector as the cache
3080 since we can't make a vector long enuf. */
3081 if (NILP (current_kboard->system_key_syms))
3082 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
3083 return modify_event_symbol (event->code,
3084 event->modifiers,
3085 Qfunction_key,
3086 current_kboard->Vsystem_key_alist,
3087 0, &current_kboard->system_key_syms,
3088 (unsigned)-1);
3091 return modify_event_symbol (event->code - 0xff00,
3092 event->modifiers,
3093 Qfunction_key, Qnil,
3094 lispy_function_keys, &func_key_syms,
3095 (sizeof (lispy_function_keys)
3096 / sizeof (lispy_function_keys[0])));
3097 break;
3099 #ifdef HAVE_MOUSE
3100 /* A mouse click. Figure out where it is, decide whether it's
3101 a press, click or drag, and build the appropriate structure. */
3102 case mouse_click:
3103 case scroll_bar_click:
3105 int button = event->code;
3106 int is_double;
3107 Lisp_Object position;
3108 Lisp_Object *start_pos_ptr;
3109 Lisp_Object start_pos;
3111 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
3112 abort ();
3114 /* Build the position as appropriate for this mouse click. */
3115 if (event->kind == mouse_click)
3117 int part;
3118 FRAME_PTR f = XFRAME (event->frame_or_window);
3119 Lisp_Object window;
3120 Lisp_Object posn;
3121 int row, column;
3123 /* Ignore mouse events that were made on frame that
3124 have been deleted. */
3125 if (! FRAME_LIVE_P (f))
3126 return Qnil;
3128 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
3129 &column, &row, NULL, 1);
3131 #ifndef USE_X_TOOLKIT
3132 /* In the non-toolkit version, clicks on the menu bar
3133 are ordinary button events in the event buffer.
3134 Distinguish them, and invoke the menu.
3136 (In the toolkit version, the toolkit handles the menu bar
3137 and Emacs doesn't know about it until after the user
3138 makes a selection.) */
3139 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f))
3141 Lisp_Object items, item;
3142 int hpos;
3143 int i;
3145 /* Activate the menu bar on the down event. If the
3146 up event comes in before the menu code can deal with it,
3147 just ignore it. */
3148 if (! (event->modifiers & down_modifier))
3149 return Qnil;
3151 item = Qnil;
3152 items = FRAME_MENU_BAR_ITEMS (f);
3153 for (i = 0; i < XVECTOR (items)->size; i += 3)
3155 Lisp_Object pos, string;
3156 string = XVECTOR (items)->contents[i + 1];
3157 pos = XVECTOR (items)->contents[i + 2];
3158 if (NILP (string))
3159 break;
3160 if (column >= XINT (pos)
3161 && column < XINT (pos) + XSTRING (string)->size)
3163 item = XVECTOR (items)->contents[i];
3164 break;
3168 position
3169 = Fcons (event->frame_or_window,
3170 Fcons (Qmenu_bar,
3171 Fcons (Fcons (event->x, event->y),
3172 Fcons (make_number (event->timestamp),
3173 Qnil))));
3175 return Fcons (item, Fcons (position, Qnil));
3177 #endif /* not USE_X_TOOLKIT */
3179 window = window_from_coordinates (f, column, row, &part);
3181 if (!WINDOWP (window))
3183 window = event->frame_or_window;
3184 posn = Qnil;
3186 else
3188 int pixcolumn, pixrow;
3189 column -= XINT (XWINDOW (window)->left);
3190 row -= XINT (XWINDOW (window)->top);
3191 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
3192 XSETINT (event->x, pixcolumn);
3193 XSETINT (event->y, pixrow);
3195 if (part == 1)
3196 posn = Qmode_line;
3197 else if (part == 2)
3198 posn = Qvertical_line;
3199 else
3200 XSETINT (posn,
3201 buffer_posn_from_coords (XWINDOW (window),
3202 column, row));
3205 position
3206 = Fcons (window,
3207 Fcons (posn,
3208 Fcons (Fcons (event->x, event->y),
3209 Fcons (make_number (event->timestamp),
3210 Qnil))));
3212 else
3214 Lisp_Object window;
3215 Lisp_Object portion_whole;
3216 Lisp_Object part;
3218 window = event->frame_or_window;
3219 portion_whole = Fcons (event->x, event->y);
3220 part = *scroll_bar_parts[(int) event->part];
3222 position =
3223 Fcons (window,
3224 Fcons (Qvertical_scroll_bar,
3225 Fcons (portion_whole,
3226 Fcons (make_number (event->timestamp),
3227 Fcons (part, Qnil)))));
3230 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
3232 start_pos = *start_pos_ptr;
3233 *start_pos_ptr = Qnil;
3235 is_double = (button == last_mouse_button
3236 && XINT (event->x) == last_mouse_x
3237 && XINT (event->y) == last_mouse_y
3238 && button_down_time != 0
3239 && (EQ (Vdouble_click_time, Qt)
3240 || (INTEGERP (Vdouble_click_time)
3241 && ((int)(event->timestamp - button_down_time)
3242 < XINT (Vdouble_click_time)))));
3243 last_mouse_button = button;
3244 last_mouse_x = XINT (event->x);
3245 last_mouse_y = XINT (event->y);
3247 /* If this is a button press, squirrel away the location, so
3248 we can decide later whether it was a click or a drag. */
3249 if (event->modifiers & down_modifier)
3251 if (is_double)
3253 double_click_count++;
3254 event->modifiers |= ((double_click_count > 2)
3255 ? triple_modifier
3256 : double_modifier);
3258 else
3259 double_click_count = 1;
3260 button_down_time = event->timestamp;
3261 *start_pos_ptr = Fcopy_alist (position);
3264 /* Now we're releasing a button - check the co-ordinates to
3265 see if this was a click or a drag. */
3266 else if (event->modifiers & up_modifier)
3268 /* If we did not see a down before this up,
3269 ignore the up. Probably this happened because
3270 the down event chose a menu item.
3271 It would be an annoyance to treat the release
3272 of the button that chose the menu item
3273 as a separate event. */
3275 if (!CONSP (start_pos))
3276 return Qnil;
3278 event->modifiers &= ~up_modifier;
3279 #if 0 /* Formerly we treated an up with no down as a click event. */
3280 if (!CONSP (start_pos))
3281 event->modifiers |= click_modifier;
3282 else
3283 #endif
3285 /* The third element of every position should be the (x,y)
3286 pair. */
3287 Lisp_Object down;
3289 down = Fnth (make_number (2), start_pos);
3290 if (EQ (event->x, XCONS (down)->car)
3291 && EQ (event->y, XCONS (down)->cdr))
3293 event->modifiers |= click_modifier;
3295 else
3297 button_down_time = 0;
3298 event->modifiers |= drag_modifier;
3300 /* Don't check is_double; treat this as multiple
3301 if the down-event was multiple. */
3302 if (double_click_count > 1)
3303 event->modifiers |= ((double_click_count > 2)
3304 ? triple_modifier
3305 : double_modifier);
3308 else
3309 /* Every mouse event should either have the down_modifier or
3310 the up_modifier set. */
3311 abort ();
3314 /* Get the symbol we should use for the mouse click. */
3315 Lisp_Object head;
3317 head = modify_event_symbol (button,
3318 event->modifiers,
3319 Qmouse_click, Qnil,
3320 lispy_mouse_names, &mouse_syms,
3321 (sizeof (lispy_mouse_names)
3322 / sizeof (lispy_mouse_names[0])));
3323 if (event->modifiers & drag_modifier)
3324 return Fcons (head,
3325 Fcons (start_pos,
3326 Fcons (position,
3327 Qnil)));
3328 else if (event->modifiers & (double_modifier | triple_modifier))
3329 return Fcons (head,
3330 Fcons (position,
3331 Fcons (make_number (double_click_count),
3332 Qnil)));
3333 else
3334 return Fcons (head,
3335 Fcons (position,
3336 Qnil));
3339 #endif /* HAVE_MOUSE */
3341 #ifdef USE_X_TOOLKIT
3342 case menu_bar_event:
3343 /* The event value is in the cdr of the frame_or_window slot. */
3344 if (!CONSP (event->frame_or_window))
3345 abort ();
3346 return XCONS (event->frame_or_window)->cdr;
3347 #endif
3349 /* The 'kind' field of the event is something we don't recognize. */
3350 default:
3351 abort ();
3355 #ifdef HAVE_MOUSE
3357 static Lisp_Object
3358 make_lispy_movement (frame, bar_window, part, x, y, time)
3359 FRAME_PTR frame;
3360 Lisp_Object bar_window;
3361 enum scroll_bar_part part;
3362 Lisp_Object x, y;
3363 unsigned long time;
3365 #ifdef MULTI_FRAME
3366 /* Is it a scroll bar movement? */
3367 if (frame && ! NILP (bar_window))
3369 Lisp_Object part_sym;
3371 part_sym = *scroll_bar_parts[(int) part];
3372 return Fcons (Qscroll_bar_movement,
3373 (Fcons (Fcons (bar_window,
3374 Fcons (Qvertical_scroll_bar,
3375 Fcons (Fcons (x, y),
3376 Fcons (make_number (time),
3377 Fcons (part_sym,
3378 Qnil))))),
3379 Qnil)));
3382 /* Or is it an ordinary mouse movement? */
3383 else
3384 #endif /* MULTI_FRAME */
3386 int area;
3387 Lisp_Object window;
3388 Lisp_Object posn;
3389 int column, row;
3391 #ifdef MULTI_FRAME
3392 if (frame)
3393 #else
3394 if (1)
3395 #endif
3397 /* It's in a frame; which window on that frame? */
3398 pixel_to_glyph_coords (frame, XINT (x), XINT (y), &column, &row,
3399 NULL, 1);
3400 window = window_from_coordinates (frame, column, row, &area);
3402 else
3403 window = Qnil;
3405 if (WINDOWP (window))
3407 int pixcolumn, pixrow;
3408 column -= XINT (XWINDOW (window)->left);
3409 row -= XINT (XWINDOW (window)->top);
3410 glyph_to_pixel_coords (frame, column, row, &pixcolumn, &pixrow);
3411 XSETINT (x, pixcolumn);
3412 XSETINT (y, pixrow);
3414 if (area == 1)
3415 posn = Qmode_line;
3416 else if (area == 2)
3417 posn = Qvertical_line;
3418 else
3419 XSETINT (posn,
3420 buffer_posn_from_coords (XWINDOW (window), column, row));
3422 #ifdef MULTI_FRAME
3423 else if (frame != 0)
3425 XSETFRAME (window, frame);
3426 posn = Qnil;
3428 #endif
3429 else
3431 window = Qnil;
3432 posn = Qnil;
3433 XSETFASTINT (x, 0);
3434 XSETFASTINT (y, 0);
3437 return Fcons (Qmouse_movement,
3438 Fcons (Fcons (window,
3439 Fcons (posn,
3440 Fcons (Fcons (x, y),
3441 Fcons (make_number (time),
3442 Qnil)))),
3443 Qnil));
3447 #endif /* HAVE_MOUSE */
3449 /* Construct a switch frame event. */
3450 static Lisp_Object
3451 make_lispy_switch_frame (frame)
3452 Lisp_Object frame;
3454 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
3457 /* Manipulating modifiers. */
3459 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
3461 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
3462 SYMBOL's name of the end of the modifiers; the string from this
3463 position is the unmodified symbol name.
3465 This doesn't use any caches. */
3467 static int
3468 parse_modifiers_uncached (symbol, modifier_end)
3469 Lisp_Object symbol;
3470 int *modifier_end;
3472 struct Lisp_String *name;
3473 int i;
3474 int modifiers;
3476 CHECK_SYMBOL (symbol, 1);
3478 modifiers = 0;
3479 name = XSYMBOL (symbol)->name;
3481 for (i = 0; i+2 <= name->size; )
3483 int this_mod_end = 0;
3484 int this_mod = 0;
3486 /* See if the name continues with a modifier word.
3487 Check that the word appears, but don't check what follows it.
3488 Set this_mod and this_mod_end to record what we find. */
3490 switch (name->data[i])
3492 #define SINGLE_LETTER_MOD(BIT) \
3493 (this_mod_end = i + 1, this_mod = BIT)
3495 case 'A':
3496 SINGLE_LETTER_MOD (alt_modifier);
3497 break;
3499 case 'C':
3500 SINGLE_LETTER_MOD (ctrl_modifier);
3501 break;
3503 case 'H':
3504 SINGLE_LETTER_MOD (hyper_modifier);
3505 break;
3507 case 'M':
3508 SINGLE_LETTER_MOD (meta_modifier);
3509 break;
3511 case 'S':
3512 SINGLE_LETTER_MOD (shift_modifier);
3513 break;
3515 case 's':
3516 SINGLE_LETTER_MOD (super_modifier);
3517 break;
3519 #undef SINGLE_LETTER_MOD
3522 /* If we found no modifier, stop looking for them. */
3523 if (this_mod_end == 0)
3524 break;
3526 /* Check there is a dash after the modifier, so that it
3527 really is a modifier. */
3528 if (this_mod_end >= name->size || name->data[this_mod_end] != '-')
3529 break;
3531 /* This modifier is real; look for another. */
3532 modifiers |= this_mod;
3533 i = this_mod_end + 1;
3536 /* Should we include the `click' modifier? */
3537 if (! (modifiers & (down_modifier | drag_modifier
3538 | double_modifier | triple_modifier))
3539 && i + 7 == name->size
3540 && strncmp (name->data + i, "mouse-", 6) == 0
3541 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
3542 modifiers |= click_modifier;
3544 if (modifier_end)
3545 *modifier_end = i;
3547 return modifiers;
3550 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
3551 prepended to the string BASE[0..BASE_LEN-1].
3552 This doesn't use any caches. */
3553 static Lisp_Object
3554 apply_modifiers_uncached (modifiers, base, base_len)
3555 int modifiers;
3556 char *base;
3557 int base_len;
3559 /* Since BASE could contain nulls, we can't use intern here; we have
3560 to use Fintern, which expects a genuine Lisp_String, and keeps a
3561 reference to it. */
3562 char *new_mods =
3563 (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
3564 int mod_len;
3567 char *p = new_mods;
3569 /* Only the event queue may use the `up' modifier; it should always
3570 be turned into a click or drag event before presented to lisp code. */
3571 if (modifiers & up_modifier)
3572 abort ();
3574 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
3575 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
3576 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
3577 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
3578 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
3579 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
3580 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
3581 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
3582 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
3583 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
3584 /* The click modifier is denoted by the absence of other modifiers. */
3586 *p = '\0';
3588 mod_len = p - new_mods;
3592 Lisp_Object new_name;
3594 new_name = make_uninit_string (mod_len + base_len);
3595 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
3596 bcopy (base, XSTRING (new_name)->data + mod_len, base_len);
3598 return Fintern (new_name, Qnil);
3603 static char *modifier_names[] =
3605 "up", "down", "drag", "click", "double", "triple", 0, 0,
3606 0, 0, 0, 0, 0, 0, 0, 0,
3607 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
3609 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
3611 static Lisp_Object modifier_symbols;
3613 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
3614 static Lisp_Object
3615 lispy_modifier_list (modifiers)
3616 int modifiers;
3618 Lisp_Object modifier_list;
3619 int i;
3621 modifier_list = Qnil;
3622 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
3623 if (modifiers & (1<<i))
3624 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
3625 modifier_list);
3627 return modifier_list;
3631 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
3632 where UNMODIFIED is the unmodified form of SYMBOL,
3633 MASK is the set of modifiers present in SYMBOL's name.
3634 This is similar to parse_modifiers_uncached, but uses the cache in
3635 SYMBOL's Qevent_symbol_element_mask property, and maintains the
3636 Qevent_symbol_elements property. */
3638 static Lisp_Object
3639 parse_modifiers (symbol)
3640 Lisp_Object symbol;
3642 Lisp_Object elements;
3644 elements = Fget (symbol, Qevent_symbol_element_mask);
3645 if (CONSP (elements))
3646 return elements;
3647 else
3649 int end;
3650 int modifiers = parse_modifiers_uncached (symbol, &end);
3651 Lisp_Object unmodified;
3652 Lisp_Object mask;
3654 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
3655 XSYMBOL (symbol)->name->size - end),
3656 Qnil);
3658 if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1))
3659 abort ();
3660 XSETFASTINT (mask, modifiers);
3661 elements = Fcons (unmodified, Fcons (mask, Qnil));
3663 /* Cache the parsing results on SYMBOL. */
3664 Fput (symbol, Qevent_symbol_element_mask,
3665 elements);
3666 Fput (symbol, Qevent_symbol_elements,
3667 Fcons (unmodified, lispy_modifier_list (modifiers)));
3669 /* Since we know that SYMBOL is modifiers applied to unmodified,
3670 it would be nice to put that in unmodified's cache.
3671 But we can't, since we're not sure that parse_modifiers is
3672 canonical. */
3674 return elements;
3678 /* Apply the modifiers MODIFIERS to the symbol BASE.
3679 BASE must be unmodified.
3681 This is like apply_modifiers_uncached, but uses BASE's
3682 Qmodifier_cache property, if present. It also builds
3683 Qevent_symbol_elements properties, since it has that info anyway.
3685 apply_modifiers copies the value of BASE's Qevent_kind property to
3686 the modified symbol. */
3687 static Lisp_Object
3688 apply_modifiers (modifiers, base)
3689 int modifiers;
3690 Lisp_Object base;
3692 Lisp_Object cache, index, entry, new_symbol;
3694 /* Mask out upper bits. We don't know where this value's been. */
3695 modifiers &= ((EMACS_INT)1 << VALBITS) - 1;
3697 /* The click modifier never figures into cache indices. */
3698 cache = Fget (base, Qmodifier_cache);
3699 XSETFASTINT (index, (modifiers & ~click_modifier));
3700 entry = assq_no_quit (index, cache);
3702 if (CONSP (entry))
3703 new_symbol = XCONS (entry)->cdr;
3704 else
3706 /* We have to create the symbol ourselves. */
3707 new_symbol = apply_modifiers_uncached (modifiers,
3708 XSYMBOL (base)->name->data,
3709 XSYMBOL (base)->name->size);
3711 /* Add the new symbol to the base's cache. */
3712 entry = Fcons (index, new_symbol);
3713 Fput (base, Qmodifier_cache, Fcons (entry, cache));
3715 /* We have the parsing info now for free, so add it to the caches. */
3716 XSETFASTINT (index, modifiers);
3717 Fput (new_symbol, Qevent_symbol_element_mask,
3718 Fcons (base, Fcons (index, Qnil)));
3719 Fput (new_symbol, Qevent_symbol_elements,
3720 Fcons (base, lispy_modifier_list (modifiers)));
3723 /* Make sure this symbol is of the same kind as BASE.
3725 You'd think we could just set this once and for all when we
3726 intern the symbol above, but reorder_modifiers may call us when
3727 BASE's property isn't set right; we can't assume that just
3728 because it has a Qmodifier_cache property it must have its
3729 Qevent_kind set right as well. */
3730 if (NILP (Fget (new_symbol, Qevent_kind)))
3732 Lisp_Object kind;
3734 kind = Fget (base, Qevent_kind);
3735 if (! NILP (kind))
3736 Fput (new_symbol, Qevent_kind, kind);
3739 return new_symbol;
3743 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
3744 return a symbol with the modifiers placed in the canonical order.
3745 Canonical order is alphabetical, except for down and drag, which
3746 always come last. The 'click' modifier is never written out.
3748 Fdefine_key calls this to make sure that (for example) C-M-foo
3749 and M-C-foo end up being equivalent in the keymap. */
3751 Lisp_Object
3752 reorder_modifiers (symbol)
3753 Lisp_Object symbol;
3755 /* It's hopefully okay to write the code this way, since everything
3756 will soon be in caches, and no consing will be done at all. */
3757 Lisp_Object parsed;
3759 parsed = parse_modifiers (symbol);
3760 return apply_modifiers ((int) XINT (XCONS (XCONS (parsed)->cdr)->car),
3761 XCONS (parsed)->car);
3765 /* For handling events, we often want to produce a symbol whose name
3766 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
3767 to some base, like the name of a function key or mouse button.
3768 modify_event_symbol produces symbols of this sort.
3770 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
3771 is the name of the i'th symbol. TABLE_SIZE is the number of elements
3772 in the table.
3774 Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
3775 NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
3777 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
3778 persist between calls to modify_event_symbol that it can use to
3779 store a cache of the symbols it's generated for this NAME_TABLE
3780 before. The object stored there may be a vector or an alist.
3782 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
3784 MODIFIERS is a set of modifier bits (as given in struct input_events)
3785 whose prefixes should be applied to the symbol name.
3787 SYMBOL_KIND is the value to be placed in the event_kind property of
3788 the returned symbol.
3790 The symbols we create are supposed to have an
3791 `event-symbol-elements' property, which lists the modifiers present
3792 in the symbol's name. */
3794 static Lisp_Object
3795 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
3796 name_table, symbol_table, table_size)
3797 int symbol_num;
3798 unsigned modifiers;
3799 Lisp_Object symbol_kind;
3800 Lisp_Object name_alist;
3801 char **name_table;
3802 Lisp_Object *symbol_table;
3803 unsigned int table_size;
3805 Lisp_Object value;
3806 Lisp_Object symbol_int;
3808 /* Get rid of the "vendor-specific" bit here. */
3809 XSETINT (symbol_int, symbol_num & 0xffffff);
3811 /* Is this a request for a valid symbol? */
3812 if (symbol_num < 0 || symbol_num >= table_size)
3813 return Qnil;
3815 if (CONSP (*symbol_table))
3816 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
3818 /* If *symbol_table doesn't seem to be initialized properly, fix that.
3819 *symbol_table should be a lisp vector TABLE_SIZE elements long,
3820 where the Nth element is the symbol for NAME_TABLE[N], or nil if
3821 we've never used that symbol before. */
3822 else
3824 if (! VECTORP (*symbol_table)
3825 || XVECTOR (*symbol_table)->size != table_size)
3827 Lisp_Object size;
3829 XSETFASTINT (size, table_size);
3830 *symbol_table = Fmake_vector (size, Qnil);
3833 value = XVECTOR (*symbol_table)->contents[symbol_num];
3836 /* Have we already used this symbol before? */
3837 if (NILP (value))
3839 /* No; let's create it. */
3840 if (!NILP (name_alist))
3841 value = Fcdr_safe (Fassq (symbol_int, name_alist));
3842 else if (name_table[symbol_num])
3843 value = intern (name_table[symbol_num]);
3845 #ifdef HAVE_X_WINDOWS
3846 if (NILP (value))
3848 char *name = x_get_keysym_name (symbol_num);
3849 if (name)
3850 value = intern (name);
3852 #endif
3854 if (NILP (value))
3856 char buf[20];
3857 sprintf (buf, "key-%d", symbol_num);
3858 value = intern (buf);
3861 if (CONSP (*symbol_table))
3862 *symbol_table = Fcons (value, *symbol_table);
3863 else
3864 XVECTOR (*symbol_table)->contents[symbol_num] = value;
3866 /* Fill in the cache entries for this symbol; this also
3867 builds the Qevent_symbol_elements property, which the user
3868 cares about. */
3869 apply_modifiers (modifiers & click_modifier, value);
3870 Fput (value, Qevent_kind, symbol_kind);
3873 /* Apply modifiers to that symbol. */
3874 return apply_modifiers (modifiers, value);
3877 /* Convert a list that represents an event type,
3878 such as (ctrl meta backspace), into the usual representation of that
3879 event type as a number or a symbol. */
3881 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
3882 "Convert the event description LIST to an event type.\n\
3883 LIST should contain one base event type (a character or symbol)\n\
3884 and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\
3885 drag, down, double or triple).\n\
3886 The return value is an event type (a character or symbol) which\n\
3887 has the same base event type and all the specified modifiers.")
3888 (event)
3889 Lisp_Object event;
3891 Lisp_Object base;
3892 int modifiers = 0;
3893 Lisp_Object rest;
3895 base = Qnil;
3896 rest = event;
3897 while (CONSP (rest))
3899 Lisp_Object elt;
3900 int this = 0;
3902 elt = XCONS (rest)->car;
3904 /* Given a symbol, see if it is a modifier name. */
3905 if (SYMBOLP (elt))
3906 this = parse_solitary_modifier (elt);
3908 if (this != 0)
3909 modifiers |= this;
3910 else if (!NILP (base))
3911 error ("Two bases given in one event");
3912 else
3913 base = elt;
3915 rest = XCONS (rest)->cdr;
3918 /* Let the symbol A refer to the character A. */
3919 if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1)
3920 XSETINT (base, XSYMBOL (base)->name->data[0]);
3922 if (INTEGERP (base))
3924 /* Turn (shift a) into A. */
3925 if ((modifiers & shift_modifier) != 0
3926 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
3928 XSETINT (base, XINT (base) - ('a' - 'A'));
3929 modifiers &= ~shift_modifier;
3932 /* Turn (control a) into C-a. */
3933 if (modifiers & ctrl_modifier)
3934 return make_number ((modifiers & ~ctrl_modifier)
3935 | make_ctrl_char (XINT (base)));
3936 else
3937 return make_number (modifiers | XINT (base));
3939 else if (SYMBOLP (base))
3940 return apply_modifiers (modifiers, base);
3941 else
3942 error ("Invalid base event");
3945 /* Try to recognize SYMBOL as a modifier name.
3946 Return the modifier flag bit, or 0 if not recognized. */
3948 static int
3949 parse_solitary_modifier (symbol)
3950 Lisp_Object symbol;
3952 struct Lisp_String *name = XSYMBOL (symbol)->name;
3954 switch (name->data[0])
3956 #define SINGLE_LETTER_MOD(BIT) \
3957 if (name->size == 1) \
3958 return BIT;
3960 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
3961 if (LEN == name->size \
3962 && ! strncmp (name->data, NAME, LEN)) \
3963 return BIT;
3965 case 'A':
3966 SINGLE_LETTER_MOD (alt_modifier);
3967 break;
3969 case 'a':
3970 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
3971 break;
3973 case 'C':
3974 SINGLE_LETTER_MOD (ctrl_modifier);
3975 break;
3977 case 'c':
3978 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
3979 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
3980 break;
3982 case 'H':
3983 SINGLE_LETTER_MOD (hyper_modifier);
3984 break;
3986 case 'h':
3987 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
3988 break;
3990 case 'M':
3991 SINGLE_LETTER_MOD (meta_modifier);
3992 break;
3994 case 'm':
3995 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
3996 break;
3998 case 'S':
3999 SINGLE_LETTER_MOD (shift_modifier);
4000 break;
4002 case 's':
4003 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
4004 MULTI_LETTER_MOD (super_modifier, "super", 5);
4005 SINGLE_LETTER_MOD (super_modifier);
4006 break;
4008 case 'd':
4009 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
4010 MULTI_LETTER_MOD (down_modifier, "down", 4);
4011 MULTI_LETTER_MOD (double_modifier, "double", 6);
4012 break;
4014 case 't':
4015 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
4016 break;
4018 #undef SINGLE_LETTER_MOD
4019 #undef MULTI_LETTER_MOD
4022 return 0;
4025 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
4026 Such a list is not valid as an event,
4027 but it can be a Lucid-style event type list. */
4030 lucid_event_type_list_p (object)
4031 Lisp_Object object;
4033 Lisp_Object tail;
4035 if (! CONSP (object))
4036 return 0;
4038 for (tail = object; CONSP (tail); tail = XCONS (tail)->cdr)
4040 Lisp_Object elt;
4041 elt = XCONS (tail)->car;
4042 if (! (INTEGERP (elt) || SYMBOLP (elt)))
4043 return 0;
4046 return NILP (tail);
4049 /* Store into *addr a value nonzero if terminal input chars are available.
4050 Serves the purpose of ioctl (0, FIONREAD, addr)
4051 but works even if FIONREAD does not exist.
4052 (In fact, this may actually read some input.) */
4054 static void
4055 get_input_pending (addr)
4056 int *addr;
4058 /* First of all, have we already counted some input? */
4059 *addr = !NILP (Vquit_flag) || readable_events ();
4061 /* If input is being read as it arrives, and we have none, there is none. */
4062 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
4063 return;
4065 /* Try to read some input and see how much we get. */
4066 gobble_input (0);
4067 *addr = !NILP (Vquit_flag) || readable_events ();
4070 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
4073 gobble_input (expected)
4074 int expected;
4076 #ifndef VMS
4077 #ifdef SIGIO
4078 if (interrupt_input)
4080 SIGMASKTYPE mask;
4081 mask = sigblockx (SIGIO);
4082 read_avail_input (expected);
4083 sigsetmask (mask);
4085 else
4086 #ifdef POLL_FOR_INPUT
4087 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
4089 SIGMASKTYPE mask;
4090 mask = sigblockx (SIGALRM);
4091 read_avail_input (expected);
4092 sigsetmask (mask);
4094 else
4095 #endif
4096 #endif
4097 read_avail_input (expected);
4098 #endif
4101 /* Put a buffer_switch_event in the buffer
4102 so that read_key_sequence will notice the new current buffer. */
4104 record_asynch_buffer_change ()
4106 struct input_event event;
4107 Lisp_Object tem;
4109 event.kind = buffer_switch_event;
4110 event.frame_or_window = Qnil;
4112 #ifdef subprocesses
4113 /* We don't need a buffer-switch event unless Emacs is waiting for input.
4114 The purpose of the event is to make read_key_sequence look up the
4115 keymaps again. If we aren't in read_key_sequence, we don't need one,
4116 and the event could cause trouble by messing up (input-pending-p). */
4117 tem = Fwaiting_for_user_input_p ();
4118 if (NILP (tem))
4119 return;
4120 #else
4121 /* We never need these events if we have no asynchronous subprocesses. */
4122 return;
4123 #endif
4125 /* Make sure no interrupt happens while storing the event. */
4126 #ifdef SIGIO
4127 if (interrupt_input)
4129 SIGMASKTYPE mask;
4130 mask = sigblockx (SIGIO);
4131 kbd_buffer_store_event (&event);
4132 sigsetmask (mask);
4134 else
4135 #endif
4137 stop_polling ();
4138 kbd_buffer_store_event (&event);
4139 start_polling ();
4143 #ifndef VMS
4145 /* Read any terminal input already buffered up by the system
4146 into the kbd_buffer, but do not wait.
4148 EXPECTED should be nonzero if the caller knows there is some input.
4150 Except on VMS, all input is read by this function.
4151 If interrupt_input is nonzero, this function MUST be called
4152 only when SIGIO is blocked.
4154 Returns the number of keyboard chars read, or -1 meaning
4155 this is a bad time to try to read input. */
4157 static int
4158 read_avail_input (expected)
4159 int expected;
4161 struct input_event buf[KBD_BUFFER_SIZE];
4162 register int i;
4163 int nread;
4165 if (read_socket_hook)
4166 /* No need for FIONREAD or fcntl; just say don't wait. */
4167 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE,
4168 expected, expected);
4169 else
4171 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
4172 the kbd_buffer can really hold. That may prevent loss
4173 of characters on some systems when input is stuffed at us. */
4174 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
4175 int n_to_read;
4177 /* Determine how many characters we should *try* to read. */
4178 #ifdef WINDOWSNT
4179 return 0;
4180 #else /* not WINDOWSNT */
4181 #ifdef MSDOS
4182 n_to_read = dos_keysns ();
4183 if (n_to_read == 0)
4184 return 0;
4185 #else /* not MSDOS */
4186 #ifdef FIONREAD
4187 /* Find out how much input is available. */
4188 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
4189 /* Formerly simply reported no input, but that sometimes led to
4190 a failure of Emacs to terminate.
4191 SIGHUP seems appropriate if we can't reach the terminal. */
4192 /* ??? Is it really right to send the signal just to this process
4193 rather than to the whole process group?
4194 Perhaps on systems with FIONREAD Emacs is alone in its group. */
4195 kill (getpid (), SIGHUP);
4196 if (n_to_read == 0)
4197 return 0;
4198 if (n_to_read > sizeof cbuf)
4199 n_to_read = sizeof cbuf;
4200 #else /* no FIONREAD */
4201 #if defined(USG) || defined(DGUX)
4202 /* Read some input if available, but don't wait. */
4203 n_to_read = sizeof cbuf;
4204 fcntl (input_fd, F_SETFL, O_NDELAY);
4205 #else
4206 you lose;
4207 #endif
4208 #endif
4209 #endif /* not MSDOS */
4210 #endif /* not WINDOWSNT */
4212 /* Now read; for one reason or another, this will not block.
4213 NREAD is set to the number of chars read. */
4216 #ifdef MSDOS
4217 cbuf[0] = dos_keyread();
4218 nread = 1;
4219 #else
4220 nread = read (input_fd, cbuf, n_to_read);
4221 #endif
4222 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
4223 /* The kernel sometimes fails to deliver SIGHUP for ptys.
4224 This looks incorrect, but it isn't, because _BSD causes
4225 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
4226 and that causes a value other than 0 when there is no input. */
4227 if (nread == 0)
4228 kill (0, SIGHUP);
4229 #endif
4231 while (
4232 /* We used to retry the read if it was interrupted.
4233 But this does the wrong thing when O_NDELAY causes
4234 an EAGAIN error. Does anybody know of a situation
4235 where a retry is actually needed? */
4236 #if 0
4237 nread < 0 && (errno == EAGAIN
4238 #ifdef EFAULT
4239 || errno == EFAULT
4240 #endif
4241 #ifdef EBADSLT
4242 || errno == EBADSLT
4243 #endif
4245 #else
4247 #endif
4250 #ifndef FIONREAD
4251 #if defined (USG) || defined (DGUX)
4252 fcntl (input_fd, F_SETFL, 0);
4253 #endif /* USG or DGUX */
4254 #endif /* no FIONREAD */
4255 for (i = 0; i < nread; i++)
4257 buf[i].kind = ascii_keystroke;
4258 buf[i].modifiers = 0;
4259 if (meta_key == 1 && (cbuf[i] & 0x80))
4260 buf[i].modifiers = meta_modifier;
4261 if (meta_key != 2)
4262 cbuf[i] &= ~0x80;
4264 buf[i].code = cbuf[i];
4265 #ifdef MULTI_FRAME
4266 XSETFRAME (buf[i].frame_or_window, selected_frame);
4267 #else
4268 buf[i].frame_or_window = Qnil;
4269 #endif
4273 /* Scan the chars for C-g and store them in kbd_buffer. */
4274 for (i = 0; i < nread; i++)
4276 kbd_buffer_store_event (&buf[i]);
4277 /* Don't look at input that follows a C-g too closely.
4278 This reduces lossage due to autorepeat on C-g. */
4279 if (buf[i].kind == ascii_keystroke
4280 && buf[i].code == quit_char)
4281 break;
4284 return nread;
4286 #endif /* not VMS */
4288 #ifdef SIGIO /* for entire page */
4289 /* Note SIGIO has been undef'd if FIONREAD is missing. */
4291 SIGTYPE
4292 input_available_signal (signo)
4293 int signo;
4295 /* Must preserve main program's value of errno. */
4296 int old_errno = errno;
4297 #ifdef BSD4_1
4298 extern int select_alarmed;
4299 #endif
4301 #ifdef USG
4302 /* USG systems forget handlers when they are used;
4303 must reestablish each time */
4304 signal (signo, input_available_signal);
4305 #endif /* USG */
4307 #ifdef BSD4_1
4308 sigisheld (SIGIO);
4309 #endif
4311 if (input_available_clear_time)
4312 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
4314 while (1)
4316 int nread;
4317 nread = read_avail_input (1);
4318 /* -1 means it's not ok to read the input now.
4319 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
4320 0 means there was no keyboard input available. */
4321 if (nread <= 0)
4322 break;
4324 #ifdef BSD4_1
4325 select_alarmed = 1; /* Force the select emulator back to life */
4326 #endif
4329 #ifdef BSD4_1
4330 sigfree ();
4331 #endif
4332 errno = old_errno;
4334 #endif /* SIGIO */
4336 /* Send ourselves a SIGIO.
4338 This function exists so that the UNBLOCK_INPUT macro in
4339 blockinput.h can have some way to take care of input we put off
4340 dealing with, without assuming that every file which uses
4341 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
4342 void
4343 reinvoke_input_signal ()
4345 #ifdef SIGIO
4346 kill (0, SIGIO);
4347 #endif
4352 /* Return the prompt-string of a sparse keymap.
4353 This is the first element which is a string.
4354 Return nil if there is none. */
4356 Lisp_Object
4357 map_prompt (map)
4358 Lisp_Object map;
4360 while (CONSP (map))
4362 register Lisp_Object tem;
4363 tem = Fcar (map);
4364 if (STRINGP (tem))
4365 return tem;
4366 map = Fcdr (map);
4368 return Qnil;
4371 static void menu_bar_item ();
4372 static void menu_bar_one_keymap ();
4374 /* These variables hold the vector under construction within
4375 menu_bar_items and its subroutines, and the current index
4376 for storing into that vector. */
4377 static Lisp_Object menu_bar_items_vector;
4378 static int menu_bar_items_index;
4380 /* Return a vector of menu items for a menu bar, appropriate
4381 to the current buffer. Each item has three elements in the vector:
4382 KEY STRING MAPLIST.
4384 OLD is an old vector we can optionally reuse, or nil. */
4386 Lisp_Object
4387 menu_bar_items (old)
4388 Lisp_Object old;
4390 /* The number of keymaps we're scanning right now, and the number of
4391 keymaps we have allocated space for. */
4392 int nmaps;
4394 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
4395 in the current keymaps, or nil where it is not a prefix. */
4396 Lisp_Object *maps;
4398 Lisp_Object def, tem, tail;
4400 Lisp_Object result;
4402 int mapno;
4403 Lisp_Object oquit;
4405 int i;
4407 struct gcpro gcpro1;
4409 /* In order to build the menus, we need to call the keymap
4410 accessors. They all call QUIT. But this function is called
4411 during redisplay, during which a quit is fatal. So inhibit
4412 quitting while building the menus.
4413 We do this instead of specbind because (1) errors will clear it anyway
4414 and (2) this avoids risk of specpdl overflow. */
4415 oquit = Vinhibit_quit;
4416 Vinhibit_quit = Qt;
4418 if (!NILP (old))
4419 menu_bar_items_vector = old;
4420 else
4421 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
4422 menu_bar_items_index = 0;
4424 GCPRO1 (menu_bar_items_vector);
4426 /* Build our list of keymaps.
4427 If we recognize a function key and replace its escape sequence in
4428 keybuf with its symbol, or if the sequence starts with a mouse
4429 click and we need to switch buffers, we jump back here to rebuild
4430 the initial keymaps from the current buffer. */
4432 Lisp_Object *tmaps;
4434 /* Should overriding-terminal-local-map and overriding-local-map apply? */
4435 if (!NILP (Voverriding_local_map_menu_flag))
4437 /* Yes, use them (if non-nil) as well as the global map. */
4438 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
4439 nmaps = 0;
4440 if (!NILP (current_kboard->Voverriding_terminal_local_map))
4441 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
4442 if (!NILP (Voverriding_local_map))
4443 maps[nmaps++] = Voverriding_local_map;
4445 else
4447 /* No, so use major and minor mode keymaps. */
4448 nmaps = current_minor_maps (NULL, &tmaps);
4449 maps = (Lisp_Object *) alloca ((nmaps + 2) * sizeof (maps[0]));
4450 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
4451 #ifdef USE_TEXT_PROPERTIES
4452 maps[nmaps++] = get_local_map (PT, current_buffer);
4453 #else
4454 maps[nmaps++] = current_buffer->keymap;
4455 #endif
4457 maps[nmaps++] = current_global_map;
4460 /* Look up in each map the dummy prefix key `menu-bar'. */
4462 result = Qnil;
4464 for (mapno = nmaps - 1; mapno >= 0; mapno--)
4466 if (! NILP (maps[mapno]))
4467 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0));
4468 else
4469 def = Qnil;
4471 tem = Fkeymapp (def);
4472 if (!NILP (tem))
4473 menu_bar_one_keymap (def);
4476 /* Move to the end those items that should be at the end. */
4478 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCONS (tail)->cdr)
4480 int i;
4481 int end = menu_bar_items_index;
4483 for (i = 0; i < end; i += 3)
4484 if (EQ (XCONS (tail)->car, XVECTOR (menu_bar_items_vector)->contents[i]))
4486 Lisp_Object tem0, tem1, tem2;
4487 /* Move the item at index I to the end,
4488 shifting all the others forward. */
4489 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
4490 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
4491 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
4492 if (end > i + 3)
4493 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 3],
4494 &XVECTOR (menu_bar_items_vector)->contents[i],
4495 (end - i - 3) * sizeof (Lisp_Object));
4496 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem0;
4497 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem1;
4498 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem2;
4499 break;
4503 /* Add nil, nil, nil at the end. */
4504 i = menu_bar_items_index;
4505 if (i + 3 > XVECTOR (menu_bar_items_vector)->size)
4507 Lisp_Object tem;
4508 int newsize = 2 * i;
4509 tem = Fmake_vector (make_number (2 * i), Qnil);
4510 bcopy (XVECTOR (menu_bar_items_vector)->contents,
4511 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
4512 menu_bar_items_vector = tem;
4514 /* Add this item. */
4515 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
4516 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
4517 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
4518 menu_bar_items_index = i;
4520 Vinhibit_quit = oquit;
4521 UNGCPRO;
4522 return menu_bar_items_vector;
4525 /* Scan one map KEYMAP, accumulating any menu items it defines
4526 in menu_bar_items_vector. */
4528 static void
4529 menu_bar_one_keymap (keymap)
4530 Lisp_Object keymap;
4532 Lisp_Object tail, item, key, binding, item_string, table;
4534 /* Loop over all keymap entries that have menu strings. */
4535 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
4537 item = XCONS (tail)->car;
4538 if (CONSP (item))
4540 key = XCONS (item)->car;
4541 binding = XCONS (item)->cdr;
4542 if (CONSP (binding))
4544 item_string = XCONS (binding)->car;
4545 if (STRINGP (item_string))
4546 menu_bar_item (key, item_string, Fcdr (binding));
4548 else if (EQ (binding, Qundefined))
4549 menu_bar_item (key, Qnil, binding);
4551 else if (VECTORP (item))
4553 /* Loop over the char values represented in the vector. */
4554 int len = XVECTOR (item)->size;
4555 int c;
4556 for (c = 0; c < len; c++)
4558 Lisp_Object character;
4559 XSETFASTINT (character, c);
4560 binding = XVECTOR (item)->contents[c];
4561 if (CONSP (binding))
4563 item_string = XCONS (binding)->car;
4564 if (STRINGP (item_string))
4565 menu_bar_item (key, item_string, Fcdr (binding));
4567 else if (EQ (binding, Qundefined))
4568 menu_bar_item (key, Qnil, binding);
4574 /* This is used as the handler when calling internal_condition_case_1. */
4576 static Lisp_Object
4577 menu_bar_item_1 (arg)
4578 Lisp_Object arg;
4580 return Qnil;
4583 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
4584 If there's already an item for KEY, add this DEF to it. */
4586 static void
4587 menu_bar_item (key, item_string, def)
4588 Lisp_Object key, item_string, def;
4590 Lisp_Object tem;
4591 Lisp_Object enabled;
4592 int i;
4594 if (EQ (def, Qundefined))
4596 /* If a map has an explicit `undefined' as definition,
4597 discard any previously made menu bar item. */
4599 for (i = 0; i < menu_bar_items_index; i += 3)
4600 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
4602 if (menu_bar_items_index > i + 3)
4603 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 3],
4604 &XVECTOR (menu_bar_items_vector)->contents[i],
4605 (menu_bar_items_index - i - 3) * sizeof (Lisp_Object));
4606 menu_bar_items_index -= 3;
4607 return;
4610 /* If there's no definition for this key yet,
4611 just ignore `undefined'. */
4612 return;
4615 /* See if this entry is enabled. */
4616 enabled = Qt;
4618 if (SYMBOLP (def))
4620 /* No property, or nil, means enable.
4621 Otherwise, enable if value is not nil. */
4622 tem = Fget (def, Qmenu_enable);
4623 if (!NILP (tem))
4624 /* (condition-case nil (eval tem)
4625 (error nil)) */
4626 enabled = internal_condition_case_1 (Feval, tem, Qerror,
4627 menu_bar_item_1);
4630 /* Ignore this item if it's not enabled. */
4631 if (NILP (enabled))
4632 return;
4634 /* Find any existing item for this KEY. */
4635 for (i = 0; i < menu_bar_items_index; i += 3)
4636 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
4637 break;
4639 /* If we did not find this KEY, add it at the end. */
4640 if (i == menu_bar_items_index)
4642 /* If vector is too small, get a bigger one. */
4643 if (i + 3 > XVECTOR (menu_bar_items_vector)->size)
4645 Lisp_Object tem;
4646 int newsize = 2 * i;
4647 tem = Fmake_vector (make_number (2 * i), Qnil);
4648 bcopy (XVECTOR (menu_bar_items_vector)->contents,
4649 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
4650 menu_bar_items_vector = tem;
4652 /* Add this item. */
4653 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
4654 XVECTOR (menu_bar_items_vector)->contents[i++] = item_string;
4655 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (def, Qnil);
4656 menu_bar_items_index = i;
4658 /* We did find an item for this KEY. Add DEF to its list of maps. */
4659 else
4661 Lisp_Object old;
4662 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
4663 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (def, old);
4667 /* Read a character using menus based on maps in the array MAPS.
4668 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
4669 Return t if we displayed a menu but the user rejected it.
4671 PREV_EVENT is the previous input event, or nil if we are reading
4672 the first event of a key sequence.
4674 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
4675 if we used a mouse menu to read the input, or zero otherwise. If
4676 USED_MOUSE_MENU is null, we don't dereference it.
4678 The prompting is done based on the prompt-string of the map
4679 and the strings associated with various map elements.
4681 This can be done with X menus or with menus put in the minibuf.
4682 These are done in different ways, depending on how the input will be read.
4683 Menus using X are done after auto-saving in read-char, getting the input
4684 event from Fx_popup_menu; menus using the minibuf use read_char recursively
4685 and do auto-saving in the inner call of read_char. */
4687 static Lisp_Object
4688 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
4689 int nmaps;
4690 Lisp_Object *maps;
4691 Lisp_Object prev_event;
4692 int *used_mouse_menu;
4694 int mapno;
4695 register Lisp_Object name;
4696 Lisp_Object rest, vector;
4698 if (used_mouse_menu)
4699 *used_mouse_menu = 0;
4701 /* Use local over global Menu maps */
4703 if (! menu_prompting)
4704 return Qnil;
4706 /* Optionally disregard all but the global map. */
4707 if (inhibit_local_menu_bar_menus)
4709 maps += (nmaps - 1);
4710 nmaps = 1;
4713 /* Get the menu name from the first map that has one (a prompt string). */
4714 for (mapno = 0; mapno < nmaps; mapno++)
4716 name = map_prompt (maps[mapno]);
4717 if (!NILP (name))
4718 break;
4721 /* If we don't have any menus, just read a character normally. */
4722 if (mapno >= nmaps)
4723 return Qnil;
4725 #if (defined (HAVE_X_WINDOWS) && defined (HAVE_X_MENU)) || defined (MSDOS)
4726 /* If we got to this point via a mouse click,
4727 use a real menu for mouse selection. */
4728 if (EVENT_HAS_PARAMETERS (prev_event)
4729 && !EQ (XCONS (prev_event)->car, Qmenu_bar))
4731 /* Display the menu and get the selection. */
4732 Lisp_Object *realmaps
4733 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
4734 Lisp_Object value;
4735 int nmaps1 = 0;
4737 /* Use the maps that are not nil. */
4738 for (mapno = 0; mapno < nmaps; mapno++)
4739 if (!NILP (maps[mapno]))
4740 realmaps[nmaps1++] = maps[mapno];
4742 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
4743 if (CONSP (value))
4745 /* If we got more than one event, put all but the first
4746 onto this list to be read later.
4747 Return just the first event now. */
4748 Vunread_command_events
4749 = nconc2 (XCONS (value)->cdr, Vunread_command_events);
4750 value = XCONS (value)->car;
4752 else if (NILP (value))
4753 value = Qt;
4754 if (used_mouse_menu)
4755 *used_mouse_menu = 1;
4756 return value;
4758 #endif /* (HAVE_X_WINDOWS && HAVE_X_MENU) || MSDOS */
4759 return Qnil ;
4762 static Lisp_Object
4763 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
4764 int commandflag ;
4765 int nmaps;
4766 Lisp_Object *maps;
4768 int mapno;
4769 register Lisp_Object name;
4770 int nlength;
4771 int width = FRAME_WIDTH (selected_frame) - 4;
4772 char *menu = (char *) alloca (width + 4);
4773 int idx = -1;
4774 int nobindings = 1;
4775 Lisp_Object rest, vector;
4777 if (! menu_prompting)
4778 return Qnil;
4780 /* Get the menu name from the first map that has one (a prompt string). */
4781 for (mapno = 0; mapno < nmaps; mapno++)
4783 name = map_prompt (maps[mapno]);
4784 if (!NILP (name))
4785 break;
4788 /* If we don't have any menus, just read a character normally. */
4789 if (mapno >= nmaps)
4790 return Qnil;
4792 /* Prompt string always starts with map's prompt, and a space. */
4793 strcpy (menu, XSTRING (name)->data);
4794 nlength = XSTRING (name)->size;
4795 menu[nlength++] = ':';
4796 menu[nlength++] = ' ';
4797 menu[nlength] = 0;
4799 /* Start prompting at start of first map. */
4800 mapno = 0;
4801 rest = maps[mapno];
4803 /* Present the documented bindings, a line at a time. */
4804 while (1)
4806 int notfirst = 0;
4807 int i = nlength;
4808 Lisp_Object obj;
4809 int ch;
4810 Lisp_Object orig_defn_macro;
4812 /* Loop over elements of map. */
4813 while (i < width)
4815 Lisp_Object s, elt;
4817 /* If reached end of map, start at beginning of next map. */
4818 if (NILP (rest))
4820 mapno++;
4821 /* At end of last map, wrap around to first map if just starting,
4822 or end this line if already have something on it. */
4823 if (mapno == nmaps)
4825 mapno = 0;
4826 if (notfirst || nobindings) break;
4828 rest = maps[mapno];
4831 /* Look at the next element of the map. */
4832 if (idx >= 0)
4833 elt = XVECTOR (vector)->contents[idx];
4834 else
4835 elt = Fcar_safe (rest);
4837 if (idx < 0 && VECTORP (elt))
4839 /* If we found a dense table in the keymap,
4840 advanced past it, but start scanning its contents. */
4841 rest = Fcdr_safe (rest);
4842 vector = elt;
4843 idx = 0;
4845 else
4847 /* An ordinary element. */
4848 Lisp_Object event;
4850 if (idx < 0)
4852 s = Fcar_safe (Fcdr_safe (elt)); /* alist */
4853 event = Fcar_safe (elt);
4855 else
4857 s = Fcar_safe (elt); /* vector */
4858 XSETINT (event, idx);
4861 /* Ignore the element if it has no prompt string. */
4862 if (STRINGP (s) && INTEGERP (event))
4864 /* 1 if the char to type matches the string. */
4865 int char_matches;
4866 Lisp_Object upcased_event, downcased_event;
4867 Lisp_Object desc;
4869 upcased_event = Fupcase (event);
4870 downcased_event = Fdowncase (event);
4871 char_matches = (XINT (upcased_event) == XSTRING (s)->data[0]
4872 || XINT (downcased_event) == XSTRING (s)->data[0]);
4873 if (! char_matches)
4874 desc = Fsingle_key_description (event);
4876 /* If we have room for the prompt string, add it to this line.
4877 If this is the first on the line, always add it. */
4878 if ((XSTRING (s)->size + i + 2
4879 + (char_matches ? 0 : XSTRING (desc)->size + 3))
4880 < width
4881 || !notfirst)
4883 int thiswidth;
4885 /* Punctuate between strings. */
4886 if (notfirst)
4888 strcpy (menu + i, ", ");
4889 i += 2;
4891 notfirst = 1;
4892 nobindings = 0 ;
4894 /* If the char to type doesn't match the string's
4895 first char, explicitly show what char to type. */
4896 if (! char_matches)
4898 /* Add as much of string as fits. */
4899 thiswidth = XSTRING (desc)->size;
4900 if (thiswidth + i > width)
4901 thiswidth = width - i;
4902 bcopy (XSTRING (desc)->data, menu + i, thiswidth);
4903 i += thiswidth;
4904 strcpy (menu + i, " = ");
4905 i += 3;
4908 /* Add as much of string as fits. */
4909 thiswidth = XSTRING (s)->size;
4910 if (thiswidth + i > width)
4911 thiswidth = width - i;
4912 bcopy (XSTRING (s)->data, menu + i, thiswidth);
4913 i += thiswidth;
4914 menu[i] = 0;
4916 else
4918 /* If this element does not fit, end the line now,
4919 and save the element for the next line. */
4920 strcpy (menu + i, "...");
4921 break;
4925 /* Move past this element. */
4926 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
4927 /* Handle reaching end of dense table. */
4928 idx = -1;
4929 if (idx >= 0)
4930 idx++;
4931 else
4932 rest = Fcdr_safe (rest);
4936 /* Prompt with that and read response. */
4937 message1 (menu);
4939 /* Make believe its not a keyboard macro in case the help char
4940 is pressed. Help characters are not recorded because menu prompting
4941 is not used on replay.
4943 orig_defn_macro = current_kboard->defining_kbd_macro;
4944 current_kboard->defining_kbd_macro = Qnil;
4946 obj = read_char (commandflag, 0, 0, Qnil, 0);
4947 while (BUFFERP (obj));
4948 current_kboard->defining_kbd_macro = orig_defn_macro;
4950 if (!INTEGERP (obj))
4951 return obj;
4952 else
4953 ch = XINT (obj);
4955 if (! EQ (obj, menu_prompt_more_char)
4956 && (!INTEGERP (menu_prompt_more_char)
4957 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
4959 if (!NILP (current_kboard->defining_kbd_macro))
4960 store_kbd_macro_char (obj);
4961 return obj;
4963 /* Help char - go round again */
4967 /* Reading key sequences. */
4969 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
4970 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
4971 keymap, or nil otherwise. Return the index of the first keymap in
4972 which KEY has any binding, or NMAPS if no map has a binding.
4974 If KEY is a meta ASCII character, treat it like meta-prefix-char
4975 followed by the corresponding non-meta character. Keymaps in
4976 CURRENT with non-prefix bindings for meta-prefix-char become nil in
4977 NEXT.
4979 If KEY has no bindings in any of the CURRENT maps, NEXT is left
4980 unmodified.
4982 NEXT may == CURRENT. */
4984 static int
4985 follow_key (key, nmaps, current, defs, next)
4986 Lisp_Object key;
4987 Lisp_Object *current, *defs, *next;
4988 int nmaps;
4990 int i, first_binding;
4992 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
4993 followed by the corresponding non-meta character. */
4994 if (INTEGERP (key) && (XINT (key) & CHAR_META))
4996 for (i = 0; i < nmaps; i++)
4997 if (! NILP (current[i]))
4999 next[i] =
5000 get_keyelt (access_keymap (current[i], meta_prefix_char, 1, 0));
5002 /* Note that since we pass the resulting bindings through
5003 get_keymap_1, non-prefix bindings for meta-prefix-char
5004 disappear. */
5005 next[i] = get_keymap_1 (next[i], 0, 1);
5007 else
5008 next[i] = Qnil;
5010 current = next;
5011 XSETINT (key, XFASTINT (key) & ~CHAR_META);
5014 first_binding = nmaps;
5015 for (i = nmaps - 1; i >= 0; i--)
5017 if (! NILP (current[i]))
5019 defs[i] = get_keyelt (access_keymap (current[i], key, 1, 0));
5020 if (! NILP (defs[i]))
5021 first_binding = i;
5023 else
5024 defs[i] = Qnil;
5027 /* Given the set of bindings we've found, produce the next set of maps. */
5028 if (first_binding < nmaps)
5029 for (i = 0; i < nmaps; i++)
5030 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
5032 return first_binding;
5035 /* Read a sequence of keys that ends with a non prefix character,
5036 storing it in KEYBUF, a buffer of size BUFSIZE.
5037 Prompt with PROMPT.
5038 Return the length of the key sequence stored.
5039 Return -1 if the user rejected a command menu.
5041 Echo starting immediately unless `prompt' is 0.
5043 Where a key sequence ends depends on the currently active keymaps.
5044 These include any minor mode keymaps active in the current buffer,
5045 the current buffer's local map, and the global map.
5047 If a key sequence has no other bindings, we check Vfunction_key_map
5048 to see if some trailing subsequence might be the beginning of a
5049 function key's sequence. If so, we try to read the whole function
5050 key, and substitute its symbolic name into the key sequence.
5052 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
5053 `double-' events into similar click events, if that would make them
5054 bound. We try to turn `triple-' events first into `double-' events,
5055 then into clicks.
5057 If we get a mouse click in a mode line, vertical divider, or other
5058 non-text area, we treat the click as if it were prefixed by the
5059 symbol denoting that area - `mode-line', `vertical-line', or
5060 whatever.
5062 If the sequence starts with a mouse click, we read the key sequence
5063 with respect to the buffer clicked on, not the current buffer.
5065 If the user switches frames in the midst of a key sequence, we put
5066 off the switch-frame event until later; the next call to
5067 read_char will return it. */
5069 static int
5070 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
5071 can_return_switch_frame)
5072 Lisp_Object *keybuf;
5073 int bufsize;
5074 Lisp_Object prompt;
5075 int dont_downcase_last;
5076 int can_return_switch_frame;
5078 int count = specpdl_ptr - specpdl;
5080 /* How many keys there are in the current key sequence. */
5081 int t;
5083 /* The length of the echo buffer when we started reading, and
5084 the length of this_command_keys when we started reading. */
5085 int echo_start;
5086 int keys_start;
5088 /* The number of keymaps we're scanning right now, and the number of
5089 keymaps we have allocated space for. */
5090 int nmaps;
5091 int nmaps_allocated = 0;
5093 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
5094 the current keymaps. */
5095 Lisp_Object *defs;
5097 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
5098 in the current keymaps, or nil where it is not a prefix. */
5099 Lisp_Object *submaps;
5101 /* The local map to start out with at start of key sequence. */
5102 Lisp_Object orig_local_map;
5104 /* 1 if we have already considered switching to the local-map property
5105 of the place where a mouse click occurred. */
5106 int localized_local_map = 0;
5108 /* The index in defs[] of the first keymap that has a binding for
5109 this key sequence. In other words, the lowest i such that
5110 defs[i] is non-nil. */
5111 int first_binding;
5113 /* If t < mock_input, then KEYBUF[t] should be read as the next
5114 input key.
5116 We use this to recover after recognizing a function key. Once we
5117 realize that a suffix of the current key sequence is actually a
5118 function key's escape sequence, we replace the suffix with the
5119 function key's binding from Vfunction_key_map. Now keybuf
5120 contains a new and different key sequence, so the echo area,
5121 this_command_keys, and the submaps and defs arrays are wrong. In
5122 this situation, we set mock_input to t, set t to 0, and jump to
5123 restart_sequence; the loop will read keys from keybuf up until
5124 mock_input, thus rebuilding the state; and then it will resume
5125 reading characters from the keyboard. */
5126 int mock_input = 0;
5128 /* If the sequence is unbound in submaps[], then
5129 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
5130 and fkey_map is its binding.
5132 These might be > t, indicating that all function key scanning
5133 should hold off until t reaches them. We do this when we've just
5134 recognized a function key, to avoid searching for the function
5135 key's again in Vfunction_key_map. */
5136 int fkey_start = 0, fkey_end = 0;
5137 Lisp_Object fkey_map;
5139 /* Likewise, for key_translation_map. */
5140 int keytran_start = 0, keytran_end = 0;
5141 Lisp_Object keytran_map;
5143 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
5144 we put it off for later. While we're reading, we keep the event here. */
5145 Lisp_Object delayed_switch_frame;
5147 /* See the comment below... */
5148 #if defined (GOBBLE_FIRST_EVENT)
5149 Lisp_Object first_event;
5150 #endif
5152 Lisp_Object original_uppercase;
5153 int original_uppercase_position = -1;
5155 /* Gets around Microsoft compiler limitations. */
5156 int dummyflag = 0;
5158 struct buffer *starting_buffer;
5160 /* Nonzero if we seem to have got the beginning of a binding
5161 in function_key_map. */
5162 int function_key_possible = 0;
5163 int key_translation_possible = 0;
5165 int junk;
5167 last_nonmenu_event = Qnil;
5169 delayed_switch_frame = Qnil;
5170 fkey_map = Vfunction_key_map;
5171 keytran_map = Vkey_translation_map;
5173 /* If there is no function-key-map, turn off function key scanning. */
5174 if (NILP (Fkeymapp (Vfunction_key_map)))
5175 fkey_start = fkey_end = bufsize + 1;
5177 /* If there is no key-translation-map, turn off scanning. */
5178 if (NILP (Fkeymapp (Vkey_translation_map)))
5179 keytran_start = keytran_end = bufsize + 1;
5181 if (INTERACTIVE)
5183 if (!NILP (prompt))
5184 echo_prompt (XSTRING (prompt)->data);
5185 else if (cursor_in_echo_area && echo_keystrokes)
5186 /* This doesn't put in a dash if the echo buffer is empty, so
5187 you don't always see a dash hanging out in the minibuffer. */
5188 echo_dash ();
5191 /* Record the initial state of the echo area and this_command_keys;
5192 we will need to restore them if we replay a key sequence. */
5193 if (INTERACTIVE)
5194 echo_start = echo_length ();
5195 keys_start = this_command_key_count;
5197 #if defined (GOBBLE_FIRST_EVENT)
5198 /* This doesn't quite work, because some of the things that read_char
5199 does cannot safely be bypassed. It seems too risky to try to make
5200 this work right. */
5202 /* Read the first char of the sequence specially, before setting
5203 up any keymaps, in case a filter runs and switches buffers on us. */
5204 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
5205 &junk);
5206 #endif /* GOBBLE_FIRST_EVENT */
5208 orig_local_map = get_local_map (PT, current_buffer);
5210 /* We jump here when the key sequence has been thoroughly changed, and
5211 we need to rescan it starting from the beginning. When we jump here,
5212 keybuf[0..mock_input] holds the sequence we should reread. */
5213 replay_sequence:
5215 starting_buffer = current_buffer;
5216 function_key_possible = 0;
5217 key_translation_possible = 0;
5219 /* Build our list of keymaps.
5220 If we recognize a function key and replace its escape sequence in
5221 keybuf with its symbol, or if the sequence starts with a mouse
5222 click and we need to switch buffers, we jump back here to rebuild
5223 the initial keymaps from the current buffer. */
5225 Lisp_Object *maps;
5227 if (!NILP (current_kboard->Voverriding_terminal_local_map)
5228 || !NILP (Voverriding_local_map))
5230 if (3 > nmaps_allocated)
5232 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
5233 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
5234 nmaps_allocated = 3;
5236 nmaps = 0;
5237 if (!NILP (current_kboard->Voverriding_terminal_local_map))
5238 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
5239 if (!NILP (Voverriding_local_map))
5240 submaps[nmaps++] = Voverriding_local_map;
5242 else
5244 nmaps = current_minor_maps (0, &maps);
5245 if (nmaps + 2 > nmaps_allocated)
5247 submaps = (Lisp_Object *) alloca ((nmaps+2) * sizeof (submaps[0]));
5248 defs = (Lisp_Object *) alloca ((nmaps+2) * sizeof (defs[0]));
5249 nmaps_allocated = nmaps + 2;
5251 bcopy (maps, submaps, nmaps * sizeof (submaps[0]));
5252 #ifdef USE_TEXT_PROPERTIES
5253 submaps[nmaps++] = orig_local_map;
5254 #else
5255 submaps[nmaps++] = current_buffer->keymap;
5256 #endif
5258 submaps[nmaps++] = current_global_map;
5261 /* Find an accurate initial value for first_binding. */
5262 for (first_binding = 0; first_binding < nmaps; first_binding++)
5263 if (! NILP (submaps[first_binding]))
5264 break;
5266 /* Start from the beginning in keybuf. */
5267 t = 0;
5269 /* These are no-ops the first time through, but if we restart, they
5270 revert the echo area and this_command_keys to their original state. */
5271 this_command_key_count = keys_start;
5272 if (INTERACTIVE && t < mock_input)
5273 echo_truncate (echo_start);
5275 /* If the best binding for the current key sequence is a keymap, or
5276 we may be looking at a function key's escape sequence, keep on
5277 reading. */
5278 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
5279 || (first_binding >= nmaps
5280 && fkey_start < t
5281 /* mock input is never part of a function key's sequence. */
5282 && mock_input <= fkey_start)
5283 || (first_binding >= nmaps
5284 && keytran_start < t && key_translation_possible)
5285 /* Don't return in the middle of a possible function key sequence,
5286 if the only bindings we found were via case conversion.
5287 Thus, if ESC O a has a function-key-map translation
5288 and ESC o has a binding, don't return after ESC O,
5289 so that we can translate ESC O plus the next character. */
5292 Lisp_Object key;
5293 int used_mouse_menu = 0;
5295 /* Where the last real key started. If we need to throw away a
5296 key that has expanded into more than one element of keybuf
5297 (say, a mouse click on the mode line which is being treated
5298 as [mode-line (mouse-...)], then we backtrack to this point
5299 of keybuf. */
5300 int last_real_key_start;
5302 /* These variables are analogous to echo_start and keys_start;
5303 while those allow us to restart the entire key sequence,
5304 echo_local_start and keys_local_start allow us to throw away
5305 just one key. */
5306 int echo_local_start, keys_local_start, local_first_binding;
5308 if (t >= bufsize)
5309 error ("key sequence too long");
5311 if (INTERACTIVE)
5312 echo_local_start = echo_length ();
5313 keys_local_start = this_command_key_count;
5314 local_first_binding = first_binding;
5316 replay_key:
5317 /* These are no-ops, unless we throw away a keystroke below and
5318 jumped back up to replay_key; in that case, these restore the
5319 variables to their original state, allowing us to replay the
5320 loop. */
5321 if (INTERACTIVE && t < mock_input)
5322 echo_truncate (echo_local_start);
5323 this_command_key_count = keys_local_start;
5324 first_binding = local_first_binding;
5326 /* By default, assume each event is "real". */
5327 last_real_key_start = t;
5329 /* Does mock_input indicate that we are re-reading a key sequence? */
5330 if (t < mock_input)
5332 key = keybuf[t];
5333 add_command_key (key);
5334 if (echo_keystrokes)
5335 echo_char (key);
5338 /* If not, we should actually read a character. */
5339 else
5341 struct buffer *buf = current_buffer;
5344 #ifdef MULTI_KBOARD
5345 KBOARD *interrupted_kboard = current_kboard;
5346 struct frame *interrupted_frame = selected_frame;
5347 if (setjmp (wrong_kboard_jmpbuf))
5349 if (!NILP (delayed_switch_frame))
5351 interrupted_kboard->kbd_queue
5352 = Fcons (delayed_switch_frame,
5353 interrupted_kboard->kbd_queue);
5354 delayed_switch_frame = Qnil;
5356 while (t > 0)
5357 interrupted_kboard->kbd_queue
5358 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
5360 /* If the side queue is non-empty, ensure it begins with a
5361 switch-frame, so we'll replay it in the right context. */
5362 if (CONSP (interrupted_kboard->kbd_queue)
5363 && (key = XCONS (interrupted_kboard->kbd_queue)->car,
5364 !(EVENT_HAS_PARAMETERS (key)
5365 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
5366 Qswitch_frame))))
5368 Lisp_Object frame;
5369 XSETFRAME (frame, interrupted_frame);
5370 interrupted_kboard->kbd_queue
5371 = Fcons (make_lispy_switch_frame (frame),
5372 interrupted_kboard->kbd_queue);
5374 mock_input = 0;
5375 orig_local_map = get_local_map (PT, current_buffer);
5376 goto replay_sequence;
5378 #endif
5379 key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
5380 &used_mouse_menu);
5383 /* read_char returns t when it shows a menu and the user rejects it.
5384 Just return -1. */
5385 if (EQ (key, Qt))
5386 return -1;
5388 /* read_char returns -1 at the end of a macro.
5389 Emacs 18 handles this by returning immediately with a
5390 zero, so that's what we'll do. */
5391 if (INTEGERP (key) && XINT (key) == -1)
5393 t = 0;
5394 /* The Microsoft C compiler can't handle the goto that
5395 would go here. */
5396 dummyflag = 1;
5397 break;
5400 /* If the current buffer has been changed from under us, the
5401 keymap may have changed, so replay the sequence. */
5402 if (BUFFERP (key))
5404 mock_input = t;
5405 orig_local_map = get_local_map (PT, current_buffer);
5406 goto replay_sequence;
5409 /* If we have a quit that was typed in another frame, and
5410 quit_throw_to_read_char switched buffers,
5411 replay to get the right keymap. */
5412 if (XINT (key) == quit_char && current_buffer != starting_buffer)
5414 keybuf[t++] = key;
5415 mock_input = t;
5416 Vquit_flag = Qnil;
5417 orig_local_map = get_local_map (PT, current_buffer);
5418 goto replay_sequence;
5421 Vquit_flag = Qnil;
5424 /* Clicks in non-text areas get prefixed by the symbol
5425 in their CHAR-ADDRESS field. For example, a click on
5426 the mode line is prefixed by the symbol `mode-line'.
5428 Furthermore, key sequences beginning with mouse clicks
5429 are read using the keymaps of the buffer clicked on, not
5430 the current buffer. So we may have to switch the buffer
5431 here.
5433 When we turn one event into two events, we must make sure
5434 that neither of the two looks like the original--so that,
5435 if we replay the events, they won't be expanded again.
5436 If not for this, such reexpansion could happen either here
5437 or when user programs play with this-command-keys. */
5438 if (EVENT_HAS_PARAMETERS (key))
5440 Lisp_Object kind;
5442 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
5443 if (EQ (kind, Qmouse_click))
5445 Lisp_Object window, posn;
5447 window = POSN_WINDOW (EVENT_START (key));
5448 posn = POSN_BUFFER_POSN (EVENT_START (key));
5449 if (CONSP (posn))
5451 /* We're looking at the second event of a
5452 sequence which we expanded before. Set
5453 last_real_key_start appropriately. */
5454 if (t > 0)
5455 last_real_key_start = t - 1;
5458 /* Key sequences beginning with mouse clicks are
5459 read using the keymaps in the buffer clicked on,
5460 not the current buffer. If we're at the
5461 beginning of a key sequence, switch buffers. */
5462 if (last_real_key_start == 0
5463 && WINDOWP (window)
5464 && BUFFERP (XWINDOW (window)->buffer)
5465 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
5467 keybuf[t] = key;
5468 mock_input = t + 1;
5470 /* Arrange to go back to the original buffer once we're
5471 done reading the key sequence. Note that we can't
5472 use save_excursion_{save,restore} here, because they
5473 save point as well as the current buffer; we don't
5474 want to save point, because redisplay may change it,
5475 to accommodate a Fset_window_start or something. We
5476 don't want to do this at the top of the function,
5477 because we may get input from a subprocess which
5478 wants to change the selected window and stuff (say,
5479 emacsclient). */
5480 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
5482 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
5483 orig_local_map = get_local_map (PT, current_buffer);
5484 goto replay_sequence;
5486 /* For a mouse click, get the local text-property keymap
5487 of the place clicked on, rather than point. */
5488 if (last_real_key_start == 0 && CONSP (XCONS (key)->cdr)
5489 && ! localized_local_map)
5491 Lisp_Object map_here, start, pos;
5493 localized_local_map = 1;
5494 start = EVENT_START (key);
5495 if (CONSP (start) && CONSP (XCONS (start)->cdr))
5497 pos = POSN_BUFFER_POSN (start);
5498 if (INTEGERP (pos))
5500 map_here = get_local_map (XINT (pos), current_buffer);
5501 if (!EQ (map_here, orig_local_map))
5503 orig_local_map = map_here;
5504 keybuf[t] = key;
5505 mock_input = t + 1;
5507 goto replay_sequence;
5513 /* Expand mode-line and scroll-bar events into two events:
5514 use posn as a fake prefix key. */
5515 if (SYMBOLP (posn))
5517 if (t + 1 >= bufsize)
5518 error ("key sequence too long");
5519 keybuf[t] = posn;
5520 keybuf[t+1] = key;
5521 mock_input = t + 2;
5523 /* Zap the position in key, so we know that we've
5524 expanded it, and don't try to do so again. */
5525 POSN_BUFFER_POSN (EVENT_START (key))
5526 = Fcons (posn, Qnil);
5527 goto replay_key;
5530 else if (EQ (kind, Qswitch_frame))
5532 /* If we're at the beginning of a key sequence, and the caller
5533 says it's okay, go ahead and return this event. If we're
5534 in the midst of a key sequence, delay it until the end. */
5535 if (t > 0 || !can_return_switch_frame)
5537 delayed_switch_frame = key;
5538 goto replay_key;
5541 else if (CONSP (XCONS (key)->cdr)
5542 && CONSP (EVENT_START (key))
5543 && CONSP (XCONS (EVENT_START (key))->cdr))
5545 Lisp_Object posn;
5547 posn = POSN_BUFFER_POSN (EVENT_START (key));
5548 /* Handle menu-bar events:
5549 insert the dummy prefix event `menu-bar'. */
5550 if (EQ (posn, Qmenu_bar))
5552 if (t + 1 >= bufsize)
5553 error ("key sequence too long");
5554 keybuf[t] = posn;
5555 keybuf[t+1] = key;
5557 /* Zap the position in key, so we know that we've
5558 expanded it, and don't try to do so again. */
5559 POSN_BUFFER_POSN (EVENT_START (key))
5560 = Fcons (posn, Qnil);
5562 mock_input = t + 2;
5563 goto replay_sequence;
5565 else if (CONSP (posn))
5567 /* We're looking at the second event of a
5568 sequence which we expanded before. Set
5569 last_real_key_start appropriately. */
5570 if (last_real_key_start == t && t > 0)
5571 last_real_key_start = t - 1;
5576 /* We have finally decided that KEY is something we might want
5577 to look up. */
5578 first_binding = (follow_key (key,
5579 nmaps - first_binding,
5580 submaps + first_binding,
5581 defs + first_binding,
5582 submaps + first_binding)
5583 + first_binding);
5585 /* If KEY wasn't bound, we'll try some fallbacks. */
5586 if (first_binding >= nmaps)
5588 Lisp_Object head;
5590 head = EVENT_HEAD (key);
5591 if (help_char_p (head) && t > 0)
5593 read_key_sequence_cmd = Vprefix_help_command;
5594 keybuf[t++] = key;
5595 last_nonmenu_event = key;
5596 /* The Microsoft C compiler can't handle the goto that
5597 would go here. */
5598 dummyflag = 1;
5599 break;
5602 if (SYMBOLP (head))
5604 Lisp_Object breakdown;
5605 int modifiers;
5607 breakdown = parse_modifiers (head);
5608 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
5609 /* Attempt to reduce an unbound mouse event to a simpler
5610 event that is bound:
5611 Drags reduce to clicks.
5612 Double-clicks reduce to clicks.
5613 Triple-clicks reduce to double-clicks, then to clicks.
5614 Down-clicks are eliminated.
5615 Double-downs reduce to downs, then are eliminated.
5616 Triple-downs reduce to double-downs, then to downs,
5617 then are eliminated. */
5618 if (modifiers & (down_modifier | drag_modifier
5619 | double_modifier | triple_modifier))
5621 while (modifiers & (down_modifier | drag_modifier
5622 | double_modifier | triple_modifier))
5624 Lisp_Object new_head, new_click;
5625 if (modifiers & triple_modifier)
5626 modifiers ^= (double_modifier | triple_modifier);
5627 else if (modifiers & double_modifier)
5628 modifiers &= ~double_modifier;
5629 else if (modifiers & drag_modifier)
5630 modifiers &= ~drag_modifier;
5631 else
5633 /* Dispose of this `down' event by simply jumping
5634 back to replay_key, to get another event.
5636 Note that if this event came from mock input,
5637 then just jumping back to replay_key will just
5638 hand it to us again. So we have to wipe out any
5639 mock input.
5641 We could delete keybuf[t] and shift everything
5642 after that to the left by one spot, but we'd also
5643 have to fix up any variable that points into
5644 keybuf, and shifting isn't really necessary
5645 anyway.
5647 Adding prefixes for non-textual mouse clicks
5648 creates two characters of mock input, and both
5649 must be thrown away. If we're only looking at
5650 the prefix now, we can just jump back to
5651 replay_key. On the other hand, if we've already
5652 processed the prefix, and now the actual click
5653 itself is giving us trouble, then we've lost the
5654 state of the keymaps we want to backtrack to, and
5655 we need to replay the whole sequence to rebuild
5658 Beyond that, only function key expansion could
5659 create more than two keys, but that should never
5660 generate mouse events, so it's okay to zero
5661 mock_input in that case too.
5663 Isn't this just the most wonderful code ever? */
5664 if (t == last_real_key_start)
5666 mock_input = 0;
5667 goto replay_key;
5669 else
5671 mock_input = last_real_key_start;
5672 goto replay_sequence;
5676 new_head
5677 = apply_modifiers (modifiers, XCONS (breakdown)->car);
5678 new_click
5679 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
5681 /* Look for a binding for this new key. follow_key
5682 promises that it didn't munge submaps the
5683 last time we called it, since key was unbound. */
5684 first_binding
5685 = (follow_key (new_click,
5686 nmaps - local_first_binding,
5687 submaps + local_first_binding,
5688 defs + local_first_binding,
5689 submaps + local_first_binding)
5690 + local_first_binding);
5692 /* If that click is bound, go for it. */
5693 if (first_binding < nmaps)
5695 key = new_click;
5696 break;
5698 /* Otherwise, we'll leave key set to the drag event. */
5704 keybuf[t++] = key;
5705 /* Normally, last_nonmenu_event gets the previous key we read.
5706 But when a mouse popup menu is being used,
5707 we don't update last_nonmenu_event; it continues to hold the mouse
5708 event that preceded the first level of menu. */
5709 if (!used_mouse_menu)
5710 last_nonmenu_event = key;
5712 /* If the sequence is unbound, see if we can hang a function key
5713 off the end of it. We only want to scan real keyboard input
5714 for function key sequences, so if mock_input says that we're
5715 re-reading old events, don't examine it. */
5716 if (first_binding >= nmaps
5717 && t >= mock_input)
5719 Lisp_Object fkey_next;
5721 /* Continue scan from fkey_end until we find a bound suffix.
5722 If we fail, increment fkey_start
5723 and start fkey_end from there. */
5724 while (fkey_end < t)
5726 Lisp_Object key;
5728 key = keybuf[fkey_end++];
5729 /* Look up meta-characters by prefixing them
5730 with meta_prefix_char. I hate this. */
5731 if (INTEGERP (key) && XINT (key) & meta_modifier)
5733 fkey_next
5734 = get_keymap_1
5735 (get_keyelt
5736 (access_keymap (fkey_map, meta_prefix_char, 1, 0)),
5737 0, 1);
5738 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
5740 else
5741 fkey_next = fkey_map;
5743 fkey_next
5744 = get_keyelt (access_keymap (fkey_next, key, 1, 0));
5746 #if 0 /* I didn't turn this on, because it might cause trouble
5747 for the mapping of return into C-m and tab into C-i. */
5748 /* Optionally don't map function keys into other things.
5749 This enables the user to redefine kp- keys easily. */
5750 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
5751 fkey_next = Qnil;
5752 #endif
5754 /* If the function key map gives a function, not an
5755 array, then call the function with no args and use
5756 its value instead. */
5757 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
5758 && fkey_end == t)
5760 struct gcpro gcpro1, gcpro2, gcpro3;
5761 Lisp_Object tem;
5762 tem = fkey_next;
5764 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
5765 fkey_next = call1 (fkey_next, prompt);
5766 UNGCPRO;
5767 /* If the function returned something invalid,
5768 barf--don't ignore it.
5769 (To ignore it safely, we would need to gcpro a bunch of
5770 other variables.) */
5771 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
5772 error ("Function in function-key-map returns invalid key sequence");
5775 function_key_possible = ! NILP (fkey_next);
5777 /* If keybuf[fkey_start..fkey_end] is bound in the
5778 function key map and it's a suffix of the current
5779 sequence (i.e. fkey_end == t), replace it with
5780 the binding and restart with fkey_start at the end. */
5781 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
5782 && fkey_end == t)
5784 int len = XFASTINT (Flength (fkey_next));
5786 t = fkey_start + len;
5787 if (t >= bufsize)
5788 error ("key sequence too long");
5790 if (VECTORP (fkey_next))
5791 bcopy (XVECTOR (fkey_next)->contents,
5792 keybuf + fkey_start,
5793 (t - fkey_start) * sizeof (keybuf[0]));
5794 else if (STRINGP (fkey_next))
5796 int i;
5798 for (i = 0; i < len; i++)
5799 XSETFASTINT (keybuf[fkey_start + i],
5800 XSTRING (fkey_next)->data[i]);
5803 mock_input = t;
5804 fkey_start = fkey_end = t;
5805 fkey_map = Vfunction_key_map;
5807 /* Do pass the results through key-translation-map. */
5808 keytran_start = keytran_end = 0;
5809 keytran_map = Vkey_translation_map;
5811 goto replay_sequence;
5814 fkey_map = get_keymap_1 (fkey_next, 0, 1);
5816 /* If we no longer have a bound suffix, try a new positions for
5817 fkey_start. */
5818 if (NILP (fkey_map))
5820 fkey_end = ++fkey_start;
5821 fkey_map = Vfunction_key_map;
5822 function_key_possible = 0;
5827 /* Look for this sequence in key-translation-map. */
5829 Lisp_Object keytran_next;
5831 /* Scan from keytran_end until we find a bound suffix. */
5832 while (keytran_end < t)
5834 Lisp_Object key;
5836 key = keybuf[keytran_end++];
5837 /* Look up meta-characters by prefixing them
5838 with meta_prefix_char. I hate this. */
5839 if (INTEGERP (key) && XINT (key) & meta_modifier)
5841 keytran_next
5842 = get_keymap_1
5843 (get_keyelt
5844 (access_keymap (keytran_map, meta_prefix_char, 1, 0)),
5845 0, 1);
5846 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
5848 else
5849 keytran_next = keytran_map;
5851 keytran_next
5852 = get_keyelt (access_keymap (keytran_next, key, 1, 0));
5854 /* If the key translation map gives a function, not an
5855 array, then call the function with no args and use
5856 its value instead. */
5857 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
5858 && keytran_end == t)
5860 struct gcpro gcpro1, gcpro2, gcpro3;
5861 Lisp_Object tem;
5862 tem = keytran_next;
5864 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
5865 keytran_next = call1 (keytran_next, prompt);
5866 UNGCPRO;
5867 /* If the function returned something invalid,
5868 barf--don't ignore it.
5869 (To ignore it safely, we would need to gcpro a bunch of
5870 other variables.) */
5871 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
5872 error ("Function in key-translation-map returns invalid key sequence");
5875 key_translation_possible = ! NILP (keytran_next);
5877 /* If keybuf[keytran_start..keytran_end] is bound in the
5878 key translation map and it's a suffix of the current
5879 sequence (i.e. keytran_end == t), replace it with
5880 the binding and restart with keytran_start at the end. */
5881 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
5882 && keytran_end == t)
5884 int len = XFASTINT (Flength (keytran_next));
5886 t = keytran_start + len;
5887 if (t >= bufsize)
5888 error ("key sequence too long");
5890 if (VECTORP (keytran_next))
5891 bcopy (XVECTOR (keytran_next)->contents,
5892 keybuf + keytran_start,
5893 (t - keytran_start) * sizeof (keybuf[0]));
5894 else if (STRINGP (keytran_next))
5896 int i;
5898 for (i = 0; i < len; i++)
5899 XSETFASTINT (keybuf[keytran_start + i],
5900 XSTRING (keytran_next)->data[i]);
5903 mock_input = t;
5904 keytran_start = keytran_end = t;
5905 keytran_map = Vkey_translation_map;
5907 /* Don't pass the results of key-translation-map
5908 through function-key-map. */
5909 fkey_start = fkey_end = t;
5910 fkey_map = Vkey_translation_map;
5912 goto replay_sequence;
5915 keytran_map = get_keymap_1 (keytran_next, 0, 1);
5917 /* If we no longer have a bound suffix, try a new positions for
5918 keytran_start. */
5919 if (NILP (keytran_map))
5921 keytran_end = ++keytran_start;
5922 keytran_map = Vkey_translation_map;
5923 key_translation_possible = 0;
5928 /* If KEY is not defined in any of the keymaps,
5929 and cannot be part of a function key or translation,
5930 and is an upper case letter
5931 use the corresponding lower-case letter instead. */
5932 if (first_binding == nmaps && ! function_key_possible
5933 && ! key_translation_possible
5934 && INTEGERP (key)
5935 && ((((XINT (key) & 0x3ffff)
5936 < XSTRING (current_buffer->downcase_table)->size)
5937 && UPPERCASEP (XINT (key) & 0x3ffff))
5938 || (XINT (key) & shift_modifier)))
5940 original_uppercase = key;
5941 original_uppercase_position = t - 1;
5943 if (XINT (key) & shift_modifier)
5944 XSETINT (key, XINT (key) & ~shift_modifier);
5945 else
5946 XSETINT (key, (DOWNCASE (XINT (key) & 0x3ffff)
5947 | (XINT (key) & ~0x3ffff)));
5949 keybuf[t - 1] = key;
5950 mock_input = t;
5951 goto replay_sequence;
5953 /* If KEY is not defined in any of the keymaps,
5954 and cannot be part of a function key or translation,
5955 and is a shifted function key,
5956 use the corresponding unshifted function key instead. */
5957 if (first_binding == nmaps && ! function_key_possible
5958 && ! key_translation_possible
5959 && SYMBOLP (key))
5961 Lisp_Object breakdown;
5962 int modifiers;
5964 original_uppercase = key;
5965 original_uppercase_position = t - 1;
5967 breakdown = parse_modifiers (key);
5968 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
5969 if (modifiers & shift_modifier)
5971 modifiers &= ~shift_modifier;
5972 key = apply_modifiers (modifiers,
5973 XCONS (breakdown)->car);
5975 keybuf[t - 1] = key;
5976 mock_input = t;
5977 goto replay_sequence;
5982 if (!dummyflag)
5983 read_key_sequence_cmd = (first_binding < nmaps
5984 ? defs[first_binding]
5985 : Qnil);
5987 unread_switch_frame = delayed_switch_frame;
5988 unbind_to (count, Qnil);
5990 if (dont_downcase_last && t - 1 == original_uppercase_position)
5991 keybuf[t - 1] = original_uppercase;
5993 /* Occasionally we fabricate events, perhaps by expanding something
5994 according to function-key-map, or by adding a prefix symbol to a
5995 mouse click in the scroll bar or modeline. In this cases, return
5996 the entire generated key sequence, even if we hit an unbound
5997 prefix or a definition before the end. This means that you will
5998 be able to push back the event properly, and also means that
5999 read-key-sequence will always return a logical unit.
6001 Better ideas? */
6002 for (; t < mock_input; t++)
6004 if (echo_keystrokes)
6005 echo_char (keybuf[t]);
6006 add_command_key (keybuf[t]);
6009 return t;
6012 #if 0 /* This doc string is too long for some compilers.
6013 This commented-out definition serves for DOC. */
6014 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
6015 "Read a sequence of keystrokes and return as a string or vector.\n\
6016 The sequence is sufficient to specify a non-prefix command in the\n\
6017 current local and global maps.\n\
6019 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
6020 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
6021 as a continuation of the previous key.\n\
6023 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
6024 convert the last event to lower case. (Normally any upper case event\n\
6025 is converted to lower case if the original event is undefined and the lower\n\
6026 case equivalent is defined.) A non-nil value is appropriate for reading\n\
6027 a key sequence to be defined.\n\
6029 A C-g typed while in this function is treated like any other character,\n\
6030 and `quit-flag' is not set.\n\
6032 If the key sequence starts with a mouse click, then the sequence is read\n\
6033 using the keymaps of the buffer of the window clicked in, not the buffer\n\
6034 of the selected window as normal.\n\
6035 ""\n\
6036 `read-key-sequence' drops unbound button-down events, since you normally\n\
6037 only care about the click or drag events which follow them. If a drag\n\
6038 or multi-click event is unbound, but the corresponding click event would\n\
6039 be bound, `read-key-sequence' turns the event into a click event at the\n\
6040 drag's starting position. This means that you don't have to distinguish\n\
6041 between click and drag, double, or triple events unless you want to.\n\
6043 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
6044 lines separating windows, and scroll bars with imaginary keys\n\
6045 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
6047 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
6048 function will process a switch-frame event if the user switches frames\n\
6049 before typing anything. If the user switches frames in the middle of a\n\
6050 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
6051 is nil, then the event will be put off until after the current key sequence.\n\
6053 `read-key-sequence' checks `function-key-map' for function key\n\
6054 sequences, where they wouldn't conflict with ordinary bindings. See\n\
6055 `function-key-map' for more details.")
6056 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
6057 #endif
6059 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
6061 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
6062 Lisp_Object prompt, continue_echo, dont_downcase_last;
6063 Lisp_Object can_return_switch_frame;
6065 Lisp_Object keybuf[30];
6066 register int i;
6067 struct gcpro gcpro1, gcpro2;
6069 if (!NILP (prompt))
6070 CHECK_STRING (prompt, 0);
6071 QUIT;
6073 bzero (keybuf, sizeof keybuf);
6074 GCPRO1 (keybuf[0]);
6075 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
6077 if (NILP (continue_echo))
6078 this_command_key_count = 0;
6080 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
6081 prompt, ! NILP (dont_downcase_last),
6082 ! NILP (can_return_switch_frame));
6084 if (i == -1)
6086 Vquit_flag = Qt;
6087 QUIT;
6089 UNGCPRO;
6090 return make_event_array (i, keybuf);
6093 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
6094 "Execute CMD as an editor command.\n\
6095 CMD must be a symbol that satisfies the `commandp' predicate.\n\
6096 Optional second arg RECORD-FLAG non-nil\n\
6097 means unconditionally put this command in `command-history'.\n\
6098 Otherwise, that is done only if an arg is read using the minibuffer.")
6099 (cmd, record)
6100 Lisp_Object cmd, record;
6102 register Lisp_Object final;
6103 register Lisp_Object tem;
6104 Lisp_Object prefixarg;
6105 struct backtrace backtrace;
6106 extern int debug_on_next_call;
6108 prefixarg = current_kboard->Vprefix_arg;
6109 current_kboard->Vprefix_arg = Qnil;
6110 Vcurrent_prefix_arg = prefixarg;
6111 debug_on_next_call = 0;
6113 if (SYMBOLP (cmd))
6115 tem = Fget (cmd, Qdisabled);
6116 if (!NILP (tem) && !NILP (Vrun_hooks))
6117 return call1 (Vrun_hooks, Qdisabled_command_hook);
6120 while (1)
6122 final = Findirect_function (cmd);
6124 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
6125 do_autoload (final, cmd);
6126 else
6127 break;
6130 if (STRINGP (final) || VECTORP (final))
6132 /* If requested, place the macro in the command history. For
6133 other sorts of commands, call-interactively takes care of
6134 this. */
6135 if (!NILP (record))
6136 Vcommand_history
6137 = Fcons (Fcons (Qexecute_kbd_macro,
6138 Fcons (final, Fcons (prefixarg, Qnil))),
6139 Vcommand_history);
6141 return Fexecute_kbd_macro (final, prefixarg);
6143 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
6145 backtrace.next = backtrace_list;
6146 backtrace_list = &backtrace;
6147 backtrace.function = &Qcall_interactively;
6148 backtrace.args = &cmd;
6149 backtrace.nargs = 1;
6150 backtrace.evalargs = 0;
6152 tem = Fcall_interactively (cmd, record);
6154 backtrace_list = backtrace.next;
6155 return tem;
6157 return Qnil;
6160 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
6161 1, 1, "P",
6162 "Read function name, then read its arguments and call it.")
6163 (prefixarg)
6164 Lisp_Object prefixarg;
6166 Lisp_Object function;
6167 char buf[40];
6168 Lisp_Object saved_keys;
6169 struct gcpro gcpro1;
6171 saved_keys = Fvector (this_command_key_count,
6172 XVECTOR (this_command_keys)->contents);
6173 buf[0] = 0;
6174 GCPRO1 (saved_keys);
6176 if (EQ (prefixarg, Qminus))
6177 strcpy (buf, "- ");
6178 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
6179 strcpy (buf, "C-u ");
6180 else if (CONSP (prefixarg) && INTEGERP (XCONS (prefixarg)->car))
6182 if (sizeof (int) == sizeof (EMACS_INT))
6183 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
6184 else if (sizeof (long) == sizeof (EMACS_INT))
6185 sprintf (buf, "%ld ", XINT (XCONS (prefixarg)->car));
6186 else
6187 abort ();
6189 else if (INTEGERP (prefixarg))
6191 if (sizeof (int) == sizeof (EMACS_INT))
6192 sprintf (buf, "%d ", XINT (prefixarg));
6193 else if (sizeof (long) == sizeof (EMACS_INT))
6194 sprintf (buf, "%ld ", XINT (prefixarg));
6195 else
6196 abort ();
6199 /* This isn't strictly correct if execute-extended-command
6200 is bound to anything else. Perhaps it should use
6201 this_command_keys? */
6202 strcat (buf, "M-x ");
6204 /* Prompt with buf, and then read a string, completing from and
6205 restricting to the set of all defined commands. Don't provide
6206 any initial input. Save the command read on the extended-command
6207 history list. */
6208 function = Fcompleting_read (build_string (buf),
6209 Vobarray, Qcommandp,
6210 Qt, Qnil, Qextended_command_history);
6212 /* Set this_command_keys to the concatenation of saved_keys and
6213 function, followed by a RET. */
6215 struct Lisp_String *str;
6216 Lisp_Object *keys;
6217 int i;
6218 Lisp_Object tem;
6220 this_command_key_count = 0;
6222 keys = XVECTOR (saved_keys)->contents;
6223 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
6224 add_command_key (keys[i]);
6226 str = XSTRING (function);
6227 for (i = 0; i < str->size; i++)
6229 XSETFASTINT (tem, str->data[i]);
6230 add_command_key (tem);
6233 XSETFASTINT (tem, '\015');
6234 add_command_key (tem);
6237 UNGCPRO;
6239 function = Fintern (function, Qnil);
6240 current_kboard->Vprefix_arg = prefixarg;
6241 this_command = function;
6243 return Fcommand_execute (function, Qt);
6247 detect_input_pending ()
6249 if (!input_pending)
6250 get_input_pending (&input_pending);
6252 return input_pending;
6255 /* This is called in some cases before a possible quit.
6256 It cases the next call to detect_input_pending to recompute input_pending.
6257 So calling this function unnecessarily can't do any harm. */
6258 clear_input_pending ()
6260 input_pending = 0;
6263 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
6264 "T if command input is currently available with no waiting.\n\
6265 Actually, the value is nil only if we can be sure that no input is available.")
6268 if (!NILP (Vunread_command_events) || unread_command_char != -1)
6269 return (Qt);
6271 return detect_input_pending () ? Qt : Qnil;
6274 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
6275 "Return vector of last 100 events, not counting those from keyboard macros.")
6278 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
6279 Lisp_Object val;
6281 if (total_keys < NUM_RECENT_KEYS)
6282 return Fvector (total_keys, keys);
6283 else
6285 val = Fvector (NUM_RECENT_KEYS, keys);
6286 bcopy (keys + recent_keys_index,
6287 XVECTOR (val)->contents,
6288 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
6289 bcopy (keys,
6290 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
6291 recent_keys_index * sizeof (Lisp_Object));
6292 return val;
6296 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
6297 "Return the key sequence that invoked this command.\n\
6298 The value is a string or a vector.")
6301 return make_event_array (this_command_key_count,
6302 XVECTOR (this_command_keys)->contents);
6305 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
6306 Sreset_this_command_lengths, 0, 0, 0,
6307 "Used for complicated reasons in `universal-argument-other-key'.\n\
6309 `universal-argument-other-key' rereads the event just typed.\n\
6310 It then gets translated through `function-key-map'.\n\
6311 The translated event gets included in the echo area and in\n\
6312 the value of `this-command-keys' in addition to the raw original event.\n\
6313 That is not right.\n\
6315 Calling this function directs the translated event to replace\n\
6316 the original event, so that only one version of the event actually\n\
6317 appears in the echo area and in the value of `this-command-keys.'.")
6320 before_command_restore_flag = 1;
6321 before_command_key_count_1 = before_command_key_count;
6322 before_command_echo_length_1 = before_command_echo_length;
6325 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
6326 "Return the current depth in recursive edits.")
6329 Lisp_Object temp;
6330 XSETFASTINT (temp, command_loop_level + minibuf_level);
6331 return temp;
6334 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
6335 "FOpen dribble file: ",
6336 "Start writing all keyboard characters to a dribble file called FILE.\n\
6337 If FILE is nil, close any open dribble file.")
6338 (file)
6339 Lisp_Object file;
6341 if (NILP (file))
6343 if (dribble)
6345 fclose (dribble);
6346 dribble = 0;
6349 else
6351 file = Fexpand_file_name (file, Qnil);
6352 dribble = fopen (XSTRING (file)->data, "w");
6354 return Qnil;
6357 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
6358 "Discard the contents of the terminal input buffer.\n\
6359 Also cancel any kbd macro being defined.")
6362 current_kboard->defining_kbd_macro = Qnil;
6363 update_mode_lines++;
6365 Vunread_command_events = Qnil;
6366 unread_command_char = -1;
6368 discard_tty_input ();
6370 /* Without the cast, GCC complains that this assignment loses the
6371 volatile qualifier of kbd_store_ptr. Is there anything wrong
6372 with that? */
6373 kbd_fetch_ptr = (struct input_event *) kbd_store_ptr;
6374 Ffillarray (kbd_buffer_frame_or_window, Qnil);
6375 input_pending = 0;
6377 return Qnil;
6380 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
6381 "Stop Emacs and return to superior process. You can resume later.\n\
6382 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
6383 control, run a subshell instead.\n\n\
6384 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
6385 to be read as terminal input by Emacs's parent, after suspension.\n\
6387 Before suspending, run the normal hook `suspend-hook'.\n\
6388 After resumption run the normal hook `suspend-resume-hook'.\n\
6390 Some operating systems cannot stop the Emacs process and resume it later.\n\
6391 On such systems, Emacs starts a subshell instead of suspending.")
6392 (stuffstring)
6393 Lisp_Object stuffstring;
6395 Lisp_Object tem;
6396 int count = specpdl_ptr - specpdl;
6397 int old_height, old_width;
6398 int width, height;
6399 struct gcpro gcpro1, gcpro2;
6400 extern init_sys_modes ();
6402 if (!NILP (stuffstring))
6403 CHECK_STRING (stuffstring, 0);
6405 /* Run the functions in suspend-hook. */
6406 if (!NILP (Vrun_hooks))
6407 call1 (Vrun_hooks, intern ("suspend-hook"));
6409 GCPRO1 (stuffstring);
6410 get_frame_size (&old_width, &old_height);
6411 reset_sys_modes ();
6412 /* sys_suspend can get an error if it tries to fork a subshell
6413 and the system resources aren't available for that. */
6414 record_unwind_protect (init_sys_modes, 0);
6415 stuff_buffered_input (stuffstring);
6416 if (cannot_suspend)
6417 sys_subshell ();
6418 else
6419 sys_suspend ();
6420 unbind_to (count, Qnil);
6422 /* Check if terminal/window size has changed.
6423 Note that this is not useful when we are running directly
6424 with a window system; but suspend should be disabled in that case. */
6425 get_frame_size (&width, &height);
6426 if (width != old_width || height != old_height)
6427 change_frame_size (selected_frame, height, width, 0, 0);
6429 /* Run suspend-resume-hook. */
6430 if (!NILP (Vrun_hooks))
6431 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
6433 UNGCPRO;
6434 return Qnil;
6437 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
6438 Then in any case stuff anything Emacs has read ahead and not used. */
6440 stuff_buffered_input (stuffstring)
6441 Lisp_Object stuffstring;
6443 /* stuff_char works only in BSD, versions 4.2 and up. */
6444 #ifdef BSD
6445 #ifndef BSD4_1
6446 register unsigned char *p;
6448 if (STRINGP (stuffstring))
6450 register int count;
6452 p = XSTRING (stuffstring)->data;
6453 count = XSTRING (stuffstring)->size;
6454 while (count-- > 0)
6455 stuff_char (*p++);
6456 stuff_char ('\n');
6458 /* Anything we have read ahead, put back for the shell to read. */
6459 /* ?? What should this do when we have multiple keyboards??
6460 Should we ignore anything that was typed in at the "wrong" kboard? */
6461 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
6463 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
6464 kbd_fetch_ptr = kbd_buffer;
6465 if (kbd_fetch_ptr->kind == ascii_keystroke)
6466 stuff_char (kbd_fetch_ptr->code);
6467 kbd_fetch_ptr->kind = no_event;
6468 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
6469 - kbd_buffer]
6470 = Qnil);
6472 input_pending = 0;
6473 #endif
6474 #endif /* BSD and not BSD4_1 */
6477 set_waiting_for_input (time_to_clear)
6478 EMACS_TIME *time_to_clear;
6480 input_available_clear_time = time_to_clear;
6482 /* Tell interrupt_signal to throw back to read_char, */
6483 waiting_for_input = 1;
6485 /* If interrupt_signal was called before and buffered a C-g,
6486 make it run again now, to avoid timing error. */
6487 if (!NILP (Vquit_flag))
6488 quit_throw_to_read_char ();
6491 clear_waiting_for_input ()
6493 /* Tell interrupt_signal not to throw back to read_char, */
6494 waiting_for_input = 0;
6495 input_available_clear_time = 0;
6498 /* This routine is called at interrupt level in response to C-G.
6499 If interrupt_input, this is the handler for SIGINT.
6500 Otherwise, it is called from kbd_buffer_store_event,
6501 in handling SIGIO or SIGTINT.
6503 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
6504 immediately throw back to read_char.
6506 Otherwise it sets the Lisp variable quit-flag not-nil.
6507 This causes eval to throw, when it gets a chance.
6508 If quit-flag is already non-nil, it stops the job right away. */
6510 SIGTYPE
6511 interrupt_signal (signalnum) /* If we don't have an argument, */
6512 int signalnum; /* some compilers complain in signal calls. */
6514 char c;
6515 /* Must preserve main program's value of errno. */
6516 int old_errno = errno;
6518 #ifdef USG
6519 if (!read_socket_hook && NILP (Vwindow_system))
6521 /* USG systems forget handlers when they are used;
6522 must reestablish each time */
6523 signal (SIGINT, interrupt_signal);
6524 signal (SIGQUIT, interrupt_signal);
6526 #endif /* USG */
6528 cancel_echoing ();
6530 if (!NILP (Vquit_flag) && FRAME_TERMCAP_P (selected_frame))
6532 fflush (stdout);
6533 reset_sys_modes ();
6534 sigfree ();
6535 #ifdef SIGTSTP /* Support possible in later USG versions */
6537 * On systems which can suspend the current process and return to the original
6538 * shell, this command causes the user to end up back at the shell.
6539 * The "Auto-save" and "Abort" questions are not asked until
6540 * the user elects to return to emacs, at which point he can save the current
6541 * job and either dump core or continue.
6543 sys_suspend ();
6544 #else
6545 #ifdef VMS
6546 if (sys_suspend () == -1)
6548 printf ("Not running as a subprocess;\n");
6549 printf ("you can continue or abort.\n");
6551 #else /* not VMS */
6552 /* Perhaps should really fork an inferior shell?
6553 But that would not provide any way to get back
6554 to the original shell, ever. */
6555 printf ("No support for stopping a process on this operating system;\n");
6556 printf ("you can continue or abort.\n");
6557 #endif /* not VMS */
6558 #endif /* not SIGTSTP */
6559 #ifdef MSDOS
6560 /* We must remain inside the screen area when the internal terminal
6561 is used. Note that [Enter] is not echoed by dos. */
6562 cursor_to (0, 0);
6563 #endif
6564 printf ("Auto-save? (y or n) ");
6565 fflush (stdout);
6566 if (((c = getchar ()) & ~040) == 'Y')
6568 Fdo_auto_save (Qt, Qnil);
6569 #ifdef MSDOS
6570 printf ("\r\nAuto-save done");
6571 #else /* not MSDOS */
6572 printf ("Auto-save done\n");
6573 #endif /* not MSDOS */
6575 while (c != '\n') c = getchar ();
6576 #ifdef MSDOS
6577 printf ("\r\nAbort? (y or n) ");
6578 #else /* not MSDOS */
6579 #ifdef VMS
6580 printf ("Abort (and enter debugger)? (y or n) ");
6581 #else /* not VMS */
6582 printf ("Abort (and dump core)? (y or n) ");
6583 #endif /* not VMS */
6584 #endif /* not MSDOS */
6585 fflush (stdout);
6586 if (((c = getchar ()) & ~040) == 'Y')
6587 abort ();
6588 while (c != '\n') c = getchar ();
6589 #ifdef MSDOS
6590 printf ("\r\nContinuing...\r\n");
6591 #else /* not MSDOS */
6592 printf ("Continuing...\n");
6593 #endif /* not MSDOS */
6594 fflush (stdout);
6595 init_sys_modes ();
6597 else
6599 /* If executing a function that wants to be interrupted out of
6600 and the user has not deferred quitting by binding `inhibit-quit'
6601 then quit right away. */
6602 if (immediate_quit && NILP (Vinhibit_quit))
6604 immediate_quit = 0;
6605 sigfree ();
6606 Fsignal (Qquit, Qnil);
6608 else
6609 /* Else request quit when it's safe */
6610 Vquit_flag = Qt;
6613 if (waiting_for_input && !echoing)
6614 quit_throw_to_read_char ();
6616 errno = old_errno;
6619 /* Handle a C-g by making read_char return C-g. */
6621 quit_throw_to_read_char ()
6623 quit_error_check ();
6624 sigfree ();
6625 /* Prevent another signal from doing this before we finish. */
6626 clear_waiting_for_input ();
6627 input_pending = 0;
6629 Vunread_command_events = Qnil;
6630 unread_command_char = -1;
6632 #if 0 /* Currently, sit_for is called from read_char without turning
6633 off polling. And that can call set_waiting_for_input.
6634 It seems to be harmless. */
6635 #ifdef POLL_FOR_INPUT
6636 /* May be > 1 if in recursive minibuffer. */
6637 if (poll_suppress_count == 0)
6638 abort ();
6639 #endif
6640 #endif
6641 #ifdef MULTI_FRAME
6642 if (FRAMEP (internal_last_event_frame)
6643 && XFRAME (internal_last_event_frame) != selected_frame)
6644 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
6645 Qnil, 0);
6646 #endif
6648 _longjmp (getcjmp, 1);
6651 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
6652 "Set mode of reading keyboard input.\n\
6653 First arg INTERRUPT non-nil means use input interrupts;\n\
6654 nil means use CBREAK mode.\n\
6655 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
6656 (no effect except in CBREAK mode).\n\
6657 Third arg META t means accept 8-bit input (for a Meta key).\n\
6658 META nil means ignore the top bit, on the assumption it is parity.\n\
6659 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
6660 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
6661 See also `current-input-mode'.")
6662 (interrupt, flow, meta, quit)
6663 Lisp_Object interrupt, flow, meta, quit;
6665 if (!NILP (quit)
6666 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
6667 error ("set-input-mode: QUIT must be an ASCII character");
6669 #ifdef POLL_FOR_INPUT
6670 stop_polling ();
6671 #endif
6673 reset_sys_modes ();
6674 #ifdef SIGIO
6675 /* Note SIGIO has been undef'd if FIONREAD is missing. */
6676 if (read_socket_hook)
6678 /* When using X, don't give the user a real choice,
6679 because we haven't implemented the mechanisms to support it. */
6680 #ifdef NO_SOCK_SIGIO
6681 interrupt_input = 0;
6682 #else /* not NO_SOCK_SIGIO */
6683 interrupt_input = 1;
6684 #endif /* NO_SOCK_SIGIO */
6686 else
6687 interrupt_input = !NILP (interrupt);
6688 #else /* not SIGIO */
6689 interrupt_input = 0;
6690 #endif /* not SIGIO */
6692 /* Our VMS input only works by interrupts, as of now. */
6693 #ifdef VMS
6694 interrupt_input = 1;
6695 #endif
6697 flow_control = !NILP (flow);
6698 if (NILP (meta))
6699 meta_key = 0;
6700 else if (EQ (meta, Qt))
6701 meta_key = 1;
6702 else
6703 meta_key = 2;
6704 if (!NILP (quit))
6705 /* Don't let this value be out of range. */
6706 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
6708 init_sys_modes ();
6710 #ifdef POLL_FOR_INPUT
6711 poll_suppress_count = 1;
6712 start_polling ();
6713 #endif
6714 return Qnil;
6717 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
6718 "Return information about the way Emacs currently reads keyboard input.\n\
6719 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
6720 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
6721 nil, Emacs is using CBREAK mode.\n\
6722 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
6723 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
6724 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
6725 META nil means ignoring the top bit, on the assumption it is parity.\n\
6726 META is neither t nor nil if accepting 8-bit input and using\n\
6727 all 8 bits as the character code.\n\
6728 QUIT is the character Emacs currently uses to quit.\n\
6729 The elements of this list correspond to the arguments of\n\
6730 `set-input-mode'.")
6733 Lisp_Object val[4];
6735 val[0] = interrupt_input ? Qt : Qnil;
6736 val[1] = flow_control ? Qt : Qnil;
6737 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
6738 XSETFASTINT (val[3], quit_char);
6740 return Flist (sizeof (val) / sizeof (val[0]), val);
6745 * Set up a new kboard object with reasonable initial values.
6747 void
6748 init_kboard (kb)
6749 KBOARD *kb;
6751 kb->Voverriding_terminal_local_map = Qnil;
6752 kb->Vlast_command = Qnil;
6753 kb->Vprefix_arg = Qnil;
6754 kb->kbd_queue = Qnil;
6755 kb->kbd_queue_has_data = 0;
6756 kb->immediate_echo = 0;
6757 kb->echoptr = kb->echobuf;
6758 kb->echo_after_prompt = -1;
6759 kb->kbd_macro_buffer = 0;
6760 kb->kbd_macro_bufsize = 0;
6761 kb->defining_kbd_macro = Qnil;
6762 kb->Vlast_kbd_macro = Qnil;
6763 kb->reference_count = 0;
6764 kb->Vsystem_key_alist = Qnil;
6765 kb->system_key_syms = Qnil;
6766 kb->Vdefault_minibuffer_frame = Qnil;
6770 * Destroy the contents of a kboard object, but not the object itself.
6771 * We use this just before deleteing it, or if we're going to initialize
6772 * it a second time.
6774 static void
6775 wipe_kboard (kb)
6776 KBOARD *kb;
6778 if (kb->kbd_macro_buffer)
6779 xfree (kb->kbd_macro_buffer);
6782 #ifdef MULTI_KBOARD
6783 void
6784 delete_kboard (kb)
6785 KBOARD *kb;
6787 KBOARD **kbp;
6788 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
6789 if (*kbp == NULL)
6790 abort ();
6791 *kbp = kb->next_kboard;
6792 wipe_kboard (kb);
6793 xfree (kb);
6795 #endif
6797 init_keyboard ()
6799 /* This is correct before outermost invocation of the editor loop */
6800 command_loop_level = -1;
6801 immediate_quit = 0;
6802 quit_char = Ctl ('g');
6803 Vunread_command_events = Qnil;
6804 unread_command_char = -1;
6805 total_keys = 0;
6806 recent_keys_index = 0;
6807 kbd_fetch_ptr = kbd_buffer;
6808 kbd_store_ptr = kbd_buffer;
6809 kbd_buffer_frame_or_window
6810 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
6811 #ifdef HAVE_MOUSE
6812 do_mouse_tracking = Qnil;
6813 #endif
6814 input_pending = 0;
6816 #ifdef MULTI_FRAME
6817 /* This means that command_loop_1 won't try to select anything the first
6818 time through. */
6819 internal_last_event_frame = Qnil;
6820 Vlast_event_frame = internal_last_event_frame;
6821 #endif
6823 #ifdef MULTI_KBOARD
6824 current_kboard = initial_kboard;
6825 #endif
6826 wipe_kboard (current_kboard);
6827 init_kboard (current_kboard);
6829 if (initialized)
6830 Ffillarray (kbd_buffer_frame_or_window, Qnil);
6832 kbd_buffer_frame_or_window
6833 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
6834 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
6836 signal (SIGINT, interrupt_signal);
6837 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
6838 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
6839 SIGQUIT and we can't tell which one it will give us. */
6840 signal (SIGQUIT, interrupt_signal);
6841 #endif /* HAVE_TERMIO */
6843 /* Note SIGIO has been undef'd if FIONREAD is missing. */
6844 #ifdef SIGIO
6845 if (!noninteractive)
6846 signal (SIGIO, input_available_signal);
6847 #endif /* SIGIO */
6849 /* Use interrupt input by default, if it works and noninterrupt input
6850 has deficiencies. */
6852 #ifdef INTERRUPT_INPUT
6853 interrupt_input = 1;
6854 #else
6855 interrupt_input = 0;
6856 #endif
6858 /* Our VMS input only works by interrupts, as of now. */
6859 #ifdef VMS
6860 interrupt_input = 1;
6861 #endif
6863 sigfree ();
6864 dribble = 0;
6866 if (keyboard_init_hook)
6867 (*keyboard_init_hook) ();
6869 #ifdef POLL_FOR_INPUT
6870 poll_suppress_count = 1;
6871 start_polling ();
6872 #endif
6875 /* This type's only use is in syms_of_keyboard, to initialize the
6876 event header symbols and put properties on them. */
6877 struct event_head {
6878 Lisp_Object *var;
6879 char *name;
6880 Lisp_Object *kind;
6883 struct event_head head_table[] = {
6884 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
6885 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
6886 &Qswitch_frame, "switch-frame", &Qswitch_frame,
6887 &Qdelete_frame, "delete-frame", &Qdelete_frame,
6888 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
6889 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
6892 syms_of_keyboard ()
6894 Qdisabled_command_hook = intern ("disabled-command-hook");
6895 staticpro (&Qdisabled_command_hook);
6897 Qself_insert_command = intern ("self-insert-command");
6898 staticpro (&Qself_insert_command);
6900 Qforward_char = intern ("forward-char");
6901 staticpro (&Qforward_char);
6903 Qbackward_char = intern ("backward-char");
6904 staticpro (&Qbackward_char);
6906 Qdisabled = intern ("disabled");
6907 staticpro (&Qdisabled);
6909 Qundefined = intern ("undefined");
6910 staticpro (&Qundefined);
6912 Qpre_command_hook = intern ("pre-command-hook");
6913 staticpro (&Qpre_command_hook);
6915 Qpost_command_hook = intern ("post-command-hook");
6916 staticpro (&Qpost_command_hook);
6918 Qdeferred_action_function = intern ("deferred-action-function");
6919 staticpro (&Qdeferred_action_function);
6921 Qcommand_hook_internal = intern ("command-hook-internal");
6922 staticpro (&Qcommand_hook_internal);
6924 Qfunction_key = intern ("function-key");
6925 staticpro (&Qfunction_key);
6926 Qmouse_click = intern ("mouse-click");
6927 staticpro (&Qmouse_click);
6929 Qmenu_enable = intern ("menu-enable");
6930 staticpro (&Qmenu_enable);
6932 Qmode_line = intern ("mode-line");
6933 staticpro (&Qmode_line);
6934 Qvertical_line = intern ("vertical-line");
6935 staticpro (&Qvertical_line);
6936 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
6937 staticpro (&Qvertical_scroll_bar);
6938 Qmenu_bar = intern ("menu-bar");
6939 staticpro (&Qmenu_bar);
6941 Qabove_handle = intern ("above-handle");
6942 staticpro (&Qabove_handle);
6943 Qhandle = intern ("handle");
6944 staticpro (&Qhandle);
6945 Qbelow_handle = intern ("below-handle");
6946 staticpro (&Qbelow_handle);
6948 Qevent_kind = intern ("event-kind");
6949 staticpro (&Qevent_kind);
6950 Qevent_symbol_elements = intern ("event-symbol-elements");
6951 staticpro (&Qevent_symbol_elements);
6952 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
6953 staticpro (&Qevent_symbol_element_mask);
6954 Qmodifier_cache = intern ("modifier-cache");
6955 staticpro (&Qmodifier_cache);
6957 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
6958 staticpro (&Qrecompute_lucid_menubar);
6959 Qactivate_menubar_hook = intern ("activate-menubar-hook");
6960 staticpro (&Qactivate_menubar_hook);
6962 Qpolling_period = intern ("polling-period");
6963 staticpro (&Qpolling_period);
6966 struct event_head *p;
6968 for (p = head_table;
6969 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
6970 p++)
6972 *p->var = intern (p->name);
6973 staticpro (p->var);
6974 Fput (*p->var, Qevent_kind, *p->kind);
6975 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
6979 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
6980 staticpro (&button_down_location);
6983 int i;
6984 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
6986 modifier_symbols = Fmake_vector (make_number (len), Qnil);
6987 for (i = 0; i < len; i++)
6988 if (modifier_names[i])
6989 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
6990 staticpro (&modifier_symbols);
6993 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
6994 staticpro (&recent_keys);
6996 this_command_keys = Fmake_vector (make_number (40), Qnil);
6997 staticpro (&this_command_keys);
6999 Qextended_command_history = intern ("extended-command-history");
7000 Fset (Qextended_command_history, Qnil);
7001 staticpro (&Qextended_command_history);
7003 kbd_buffer_frame_or_window
7004 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
7005 staticpro (&kbd_buffer_frame_or_window);
7007 accent_key_syms = Qnil;
7008 staticpro (&accent_key_syms);
7010 func_key_syms = Qnil;
7011 staticpro (&func_key_syms);
7013 mouse_syms = Qnil;
7014 staticpro (&mouse_syms);
7016 unread_switch_frame = Qnil;
7017 staticpro (&unread_switch_frame);
7019 defsubr (&Sevent_convert_list);
7020 defsubr (&Sread_key_sequence);
7021 defsubr (&Srecursive_edit);
7022 #ifdef HAVE_MOUSE
7023 defsubr (&Strack_mouse);
7024 #endif
7025 defsubr (&Sinput_pending_p);
7026 defsubr (&Scommand_execute);
7027 defsubr (&Srecent_keys);
7028 defsubr (&Sthis_command_keys);
7029 defsubr (&Sreset_this_command_lengths);
7030 defsubr (&Ssuspend_emacs);
7031 defsubr (&Sabort_recursive_edit);
7032 defsubr (&Sexit_recursive_edit);
7033 defsubr (&Srecursion_depth);
7034 defsubr (&Stop_level);
7035 defsubr (&Sdiscard_input);
7036 defsubr (&Sopen_dribble_file);
7037 defsubr (&Sset_input_mode);
7038 defsubr (&Scurrent_input_mode);
7039 defsubr (&Sexecute_extended_command);
7041 DEFVAR_LISP ("last-command-char", &last_command_char,
7042 "Last input event that was part of a command.");
7044 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
7045 "Last input event that was part of a command.");
7047 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
7048 "Last input event in a command, except for mouse menu events.\n\
7049 Mouse menus give back keys that don't look like mouse events;\n\
7050 this variable holds the actual mouse event that led to the menu,\n\
7051 so that you can determine whether the command was run by mouse or not.");
7053 DEFVAR_LISP ("last-input-char", &last_input_char,
7054 "Last input event.");
7056 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
7057 "Last input event.");
7059 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
7060 "List of objects to be read as next command input events.");
7062 DEFVAR_INT ("unread-command-char", &unread_command_char,
7063 "If not -1, an object to be read as next command input event.");
7065 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
7066 "Meta-prefix character code. Meta-foo as command input\n\
7067 turns into this character followed by foo.");
7068 XSETINT (meta_prefix_char, 033);
7070 DEFVAR_KBOARD ("last-command", Vlast_command,
7071 "The last command executed. Normally a symbol with a function definition,\n\
7072 but can be whatever was found in the keymap, or whatever the variable\n\
7073 `this-command' was set to by that command.\n\
7075 The value `mode-exit' is special; it means that the previous command\n\
7076 read an event that told it to exit, and it did so and unread that event.\n\
7077 In other words, the present command is the event that made the previous\n\
7078 command exit.\n\
7080 The value `kill-region' is special; it means that the previous command\n\
7081 was a kill command.");
7083 DEFVAR_LISP ("this-command", &this_command,
7084 "The command now being executed.\n\
7085 The command can set this variable; whatever is put here\n\
7086 will be in `last-command' during the following command.");
7087 this_command = Qnil;
7089 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
7090 "*Number of keyboard input characters between auto-saves.\n\
7091 Zero means disable autosaving due to number of characters typed.");
7092 auto_save_interval = 300;
7094 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
7095 "*Number of seconds idle time before auto-save.\n\
7096 Zero or nil means disable auto-saving due to idleness.\n\
7097 After auto-saving due to this many seconds of idle time,\n\
7098 Emacs also does a garbage collection if that seems to be warranted.");
7099 XSETFASTINT (Vauto_save_timeout, 30);
7101 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
7102 "*Nonzero means echo unfinished commands after this many seconds of pause.");
7103 echo_keystrokes = 1;
7105 DEFVAR_INT ("polling-period", &polling_period,
7106 "*Interval between polling for input during Lisp execution.\n\
7107 The reason for polling is to make C-g work to stop a running program.\n\
7108 Polling is needed only when using X windows and SIGIO does not work.\n\
7109 Polling is automatically disabled in all other cases.");
7110 polling_period = 2;
7112 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
7113 "*Maximum time between mouse clicks to make a double-click.\n\
7114 Measured in milliseconds. nil means disable double-click recognition;\n\
7115 t means double-clicks have no time limit and are detected\n\
7116 by position only.");
7117 Vdouble_click_time = make_number (500);
7119 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
7120 "*Non-nil means inhibit local map menu bar menus.");
7121 inhibit_local_menu_bar_menus = 0;
7123 DEFVAR_INT ("num-input-keys", &num_input_keys,
7124 "Number of complete keys read from the keyboard so far.");
7125 num_input_keys = 0;
7127 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
7128 "The frame in which the most recently read event occurred.\n\
7129 If the last event came from a keyboard macro, this is set to `macro'.");
7130 Vlast_event_frame = Qnil;
7132 DEFVAR_LISP ("help-char", &Vhelp_char,
7133 "Character to recognize as meaning Help.\n\
7134 When it is read, do `(eval help-form)', and display result if it's a string.\n\
7135 If the value of `help-form' is nil, this char can be read normally.");
7136 XSETINT (Vhelp_char, Ctl ('H'));
7138 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
7139 "List of input events to recognize as meaning Help.\n\
7140 These work just like the value of `help-char' (see that).");
7141 Vhelp_event_list = Qnil;
7143 DEFVAR_LISP ("help-form", &Vhelp_form,
7144 "Form to execute when character `help-char' is read.\n\
7145 If the form returns a string, that string is displayed.\n\
7146 If `help-form' is nil, the help char is not recognized.");
7147 Vhelp_form = Qnil;
7149 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
7150 "Command to run when `help-char' character follows a prefix key.\n\
7151 This command is used only when there is no actual binding\n\
7152 for that character after that prefix key.");
7153 Vprefix_help_command = Qnil;
7155 DEFVAR_LISP ("top-level", &Vtop_level,
7156 "Form to evaluate when Emacs starts up.\n\
7157 Useful to set before you dump a modified Emacs.");
7158 Vtop_level = Qnil;
7160 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
7161 "String used as translate table for keyboard input, or nil.\n\
7162 Each character is looked up in this string and the contents used instead.\n\
7163 If string is of length N, character codes N and up are untranslated.");
7164 Vkeyboard_translate_table = Qnil;
7166 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
7167 "Keymap of key translations that can override keymaps.\n\
7168 This keymap works like `function-key-map', but comes after that,\n\
7169 and applies even for keys that have ordinary bindings.");
7170 Vkey_translation_map = Qnil;
7172 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
7173 "Non-nil means to always spawn a subshell instead of suspending,\n\
7174 even if the operating system has support for stopping a process.");
7175 cannot_suspend = 0;
7177 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
7178 "Non-nil means prompt with menus when appropriate.\n\
7179 This is done when reading from a keymap that has a prompt string,\n\
7180 for elements that have prompt strings.\n\
7181 The menu is displayed on the screen\n\
7182 if X menus were enabled at configuration\n\
7183 time and the previous event was a mouse click prefix key.\n\
7184 Otherwise, menu prompting uses the echo area.");
7185 menu_prompting = 1;
7187 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
7188 "Character to see next line of menu prompt.\n\
7189 Type this character while in a menu prompt to rotate around the lines of it.");
7190 XSETINT (menu_prompt_more_char, ' ');
7192 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
7193 "A mask of additional modifier keys to use with every keyboard character.\n\
7194 Emacs applies the modifiers of the character stored here to each keyboard\n\
7195 character it reads. For example, after evaluating the expression\n\
7196 (setq extra-keyboard-modifiers ?\\C-x)\n\
7197 all input characters will have the control modifier applied to them.\n\
7199 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
7200 not count as a control character; rather, it counts as a character\n\
7201 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
7202 cancels any modification.");
7203 extra_keyboard_modifiers = 0;
7205 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
7206 "If an editing command sets this to t, deactivate the mark afterward.\n\
7207 The command loop sets this to nil before each command,\n\
7208 and tests the value when the command returns.\n\
7209 Buffer modification stores t in this variable.");
7210 Vdeactivate_mark = Qnil;
7212 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
7213 "Temporary storage of pre-command-hook or post-command-hook.");
7214 Vcommand_hook_internal = Qnil;
7216 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
7217 "Normal hook run before each command is executed.\n\
7218 While the hook is run, its value is temporarily set to nil\n\
7219 to avoid an unbreakable infinite loop if a hook function gets an error.\n\
7220 As a result, a hook function cannot straightforwardly alter the value of\n\
7221 `pre-command-hook'. See the Emacs Lisp manual for a way of\n\
7222 implementing hook functions that alter the set of hook functions.");
7223 Vpre_command_hook = Qnil;
7225 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
7226 "Normal hook run after each command is executed.\n\
7227 While the hook is run, its value is temporarily set to nil\n\
7228 to avoid an unbreakable infinite loop if a hook function gets an error.\n\
7229 As a result, a hook function cannot straightforwardly alter the value of\n\
7230 `post-command-hook'. See the Emacs Lisp manual for a way of\n\
7231 implementing hook functions that alter the set of hook functions.");
7232 Vpost_command_hook = Qnil;
7234 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
7235 "t means menu bar, specified Lucid style, needs to be recomputed.");
7236 Vlucid_menu_bar_dirty_flag = Qnil;
7238 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
7239 "List of menu bar items to move to the end of the menu bar.\n\
7240 The elements of the list are event types that may have menu bar bindings.");
7241 Vmenu_bar_final_items = Qnil;
7243 DEFVAR_KBOARD ("overriding-terminal-local-map",
7244 Voverriding_terminal_local_map,
7245 "Keymap that overrides all other local keymaps.\n\
7246 If this variable is non-nil, it is used as a keymap instead of the\n\
7247 buffer's local map, and the minor mode keymaps and text property keymaps.");
7249 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
7250 "Keymap that overrides all other local keymaps.\n\
7251 If this variable is non-nil, it is used as a keymap instead of the\n\
7252 buffer's local map, and the minor mode keymaps and text property keymaps.");
7253 Voverriding_local_map = Qnil;
7255 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
7256 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
7257 Otherwise, the menu bar continues to reflect the buffer's local map\n\
7258 and the minor mode maps regardless of `overriding-local-map'.");
7259 Voverriding_local_map_menu_flag = Qnil;
7261 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
7262 "Keymap defining bindings for special events to execute at low level.");
7263 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
7265 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
7266 "*Non-nil means generate motion events for mouse motion.");
7268 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
7269 "Alist of system-specific X windows key symbols.\n\
7270 Each element should have the form (N . SYMBOL) where N is the\n\
7271 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
7272 and SYMBOL is its name.");
7274 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
7275 "List of deferred actions to be performed at a later time.\n\
7276 The precise format isn't relevant here; we just check whether it is nil.");
7277 Vdeferred_action_list = Qnil;
7279 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
7280 "Function to call to handle deferred actions, after each command.\n\
7281 This function is called with no arguments after each command\n\
7282 whenever `deferred-action-list' is non-nil.");
7283 Vdeferred_action_function = Qnil;
7286 keys_of_keyboard ()
7288 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
7289 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
7290 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
7291 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
7292 initial_define_key (meta_map, 'x', "execute-extended-command");
7294 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
7295 "handle-delete-frame");
7296 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
7297 "ignore-event");
7298 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
7299 "ignore-event");