(auto-mode-alist): added pairs for .ms, .man, .mk, [Mm]akefile, .lex.
[emacs.git] / src / keyboard.c
blob468d9703fe08032903e51f0581e7e2b086624af4
1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993 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 #undef NULL
26 #include "termchar.h"
27 #include "termopts.h"
28 #include "lisp.h"
29 #include "termhooks.h"
30 #include "macros.h"
31 #include "frame.h"
32 #include "window.h"
33 #include "commands.h"
34 #include "buffer.h"
35 #include "disptab.h"
36 #include "dispextern.h"
37 #include "keyboard.h"
38 #include "intervals.h"
39 #include <setjmp.h>
40 #include <errno.h>
42 #ifndef VMS
43 #include <sys/ioctl.h>
44 #endif
46 #include "syssignal.h"
47 #include "systty.h"
48 #include "systime.h"
50 extern int errno;
52 #ifdef HAVE_X_WINDOWS
53 extern Lisp_Object Vmouse_grabbed;
55 /* Make all keyboard buffers much bigger when using X windows. */
56 #define KBD_BUFFER_SIZE 4096
57 #else /* No X-windows, character input */
58 #define KBD_BUFFER_SIZE 256
59 #endif /* No X-windows */
61 /* Following definition copied from eval.c */
63 struct backtrace
65 struct backtrace *next;
66 Lisp_Object *function;
67 Lisp_Object *args; /* Points to vector of args. */
68 int nargs; /* length of vector. If nargs is UNEVALLED,
69 args points to slot holding list of
70 unevalled args */
71 char evalargs;
74 /* Non-nil disable property on a command means
75 do not execute it; call disabled-command-hook's value instead. */
76 Lisp_Object Qdisabled, Vdisabled_command_hook;
78 #define NUM_RECENT_KEYS (100)
79 int recent_keys_index; /* Index for storing next element into recent_keys */
80 int total_keys; /* Total number of elements stored into recent_keys */
81 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
83 /* Vector holding the key sequence that invoked the current command.
84 It is reused for each command, and it may be longer than the current
85 sequence; this_command_key_count indicates how many elements
86 actually mean something.
87 It's easier to staticpro a single Lisp_Object than an array. */
88 Lisp_Object this_command_keys;
89 int this_command_key_count;
91 extern int minbuf_level;
93 extern struct backtrace *backtrace_list;
95 /* Nonzero means do menu prompting. */
96 static int menu_prompting;
98 /* Character to see next line of menu prompt. */
99 static Lisp_Object menu_prompt_more_char;
101 /* For longjmp to where kbd input is being done. */
102 static jmp_buf getcjmp;
104 /* True while doing kbd input. */
105 int waiting_for_input;
107 /* True while displaying for echoing. Delays C-g throwing. */
108 static int echoing;
110 /* Nonzero means C-G should cause immediate error-signal. */
111 int immediate_quit;
113 /* Character to recognize as the help char. */
114 Lisp_Object help_char;
116 /* Form to execute when help char is typed. */
117 Lisp_Object Vhelp_form;
119 /* Character that causes a quit. Normally C-g.
121 If we are running on an ordinary terminal, this must be an ordinary
122 ASCII char, since we want to make it our interrupt character.
124 If we are not running on an ordinary terminal, it still needs to be
125 an ordinary ASCII char. This character needs to be recognized in
126 the input interrupt handler. At this point, the keystroke is
127 represented as a struct input_event, while the desired quit
128 character is specified as a lispy event. The mapping from struct
129 input_events to lispy events cannot run in an interrupt handler,
130 and the reverse mapping is difficult for anything but ASCII
131 keystrokes.
133 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
134 ASCII character. */
135 int quit_char;
137 extern Lisp_Object current_global_map;
138 extern int minibuf_level;
140 /* Current depth in recursive edits. */
141 int command_loop_level;
143 /* Total number of times command_loop has read a key sequence. */
144 int num_input_keys;
146 /* Last input character read as a command. */
147 Lisp_Object last_command_char;
149 /* Last input character read as a command, not counting menus
150 reached by the mouse. */
151 Lisp_Object last_nonmenu_event;
153 /* Last input character read for any purpose. */
154 Lisp_Object last_input_char;
156 /* If not Qnil, a list of objects to be read as subsequent command input. */
157 Lisp_Object unread_command_events;
159 /* If not -1, an event to be read as subsequent command input. */
160 int unread_command_char;
162 /* If not Qnil, this is a switch-frame event which we decided to put
163 off until the end of a key sequence. This should be read as the
164 next command input, after any unread_command_events.
166 read_key_sequence uses this to delay switch-frame events until the
167 end of the key sequence; Fread_char uses it to put off switch-frame
168 events until a non-ASCII event is acceptable as input. */
169 Lisp_Object unread_switch_frame;
171 /* A mask of extra modifier bits to put into every keyboard char. */
172 int extra_keyboard_modifiers;
174 /* Char to use as prefix when a meta character is typed in.
175 This is bound on entry to minibuffer in case ESC is changed there. */
177 Lisp_Object meta_prefix_char;
179 /* Last size recorded for a current buffer which is not a minibuffer. */
180 static int last_non_minibuf_size;
182 /* Number of idle seconds before an auto-save and garbage collection. */
183 static Lisp_Object Vauto_save_timeout;
185 /* Total number of times read_char has returned. */
186 int num_input_chars;
188 /* Total number of times read_char has returned, outside of macros. */
189 int num_nonmacro_input_chars;
191 /* Auto-save automatically when this many characters have been typed
192 since the last time. */
194 static int auto_save_interval;
196 /* Value of num_nonmacro_input_chars as of last auto save. */
198 int last_auto_save;
200 /* Last command executed by the editor command loop, not counting
201 commands that set the prefix argument. */
203 Lisp_Object last_command;
205 /* The command being executed by the command loop.
206 Commands may set this, and the value set will be copied into last_command
207 instead of the actual command. */
208 Lisp_Object this_command;
210 #ifdef MULTI_FRAME
211 /* The frame in which the last input event occurred, or Qmacro if the
212 last event came from a macro. We use this to determine when to
213 generate switch-frame events. This may be cleared by functions
214 like Fselect_frame, to make sure that a switch-frame event is
215 generated by the next character. */
216 Lisp_Object internal_last_event_frame;
218 /* A user-visible version of the above, intended to allow users to
219 figure out where the last event came from, if the event doesn't
220 carry that information itself (i.e. if it was a character). */
221 Lisp_Object Vlast_event_frame;
222 #endif
224 /* The timestamp of the last input event we received from the X server.
225 X Windows wants this for selection ownership. */
226 unsigned long last_event_timestamp;
228 Lisp_Object Qself_insert_command;
229 Lisp_Object Qforward_char;
230 Lisp_Object Qbackward_char;
232 /* read_key_sequence stores here the command definition of the
233 key sequence that it reads. */
234 Lisp_Object read_key_sequence_cmd;
236 /* Form to evaluate (if non-nil) when Emacs is started. */
237 Lisp_Object Vtop_level;
239 /* User-supplied string to translate input characters through. */
240 Lisp_Object Vkeyboard_translate_table;
242 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
243 extern Lisp_Object Vfunction_key_map;
245 /* Non-nil means deactivate the mark at end of this command. */
246 Lisp_Object Vdeactivate_mark;
248 /* Menu bar specified in Lucid Emacs fashion. */
250 Lisp_Object Vlucid_menu_bar_dirty_flag;
251 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
253 /* Hooks to run before and after each command. */
254 Lisp_Object Qpre_command_hook, Qpost_command_hook;
255 Lisp_Object Vpre_command_hook, Vpost_command_hook;
257 /* File in which we write all commands we read. */
258 FILE *dribble;
260 /* Nonzero if input is available. */
261 int input_pending;
263 /* Nonzero if should obey 0200 bit in input chars as "Meta". */
264 int meta_key;
266 extern char *pending_malloc_warning;
268 /* Circular buffer for pre-read keyboard input. */
269 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
271 /* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
273 The interrupt-level event handlers will never enqueue an event on a
274 frame which is not in Vframe_list, and once an event is dequeued,
275 internal_last_event_frame or the event itself points to the frame.
276 So that's all fine.
278 But while the event is sitting in the queue, it's completely
279 unprotected. Suppose the user types one command which will run for
280 a while and then delete a frame, and then types another event at
281 the frame that will be deleted, before the command gets around to
282 it. Suppose there are no references to this frame elsewhere in
283 Emacs, and a GC occurs before the second event is dequeued. Now we
284 have an event referring to a freed frame, which will crash Emacs
285 when it is dequeued.
287 Similar things happen when an event on a scroll bar is enqueued; the
288 window may be deleted while the event is in the queue.
290 So, we use this vector to protect the frame_or_window field in the
291 event queue. That way, they'll be dequeued as dead frames or
292 windows, but still valid lisp objects.
294 If kbd_buffer[i].kind != no_event, then
295 (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
296 == kbd_buffer[i].frame_or_window. */
297 static Lisp_Object kbd_buffer_frame_or_window;
299 /* Pointer to next available character in kbd_buffer.
300 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
301 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
302 next available char is in kbd_buffer[0]. */
303 static struct input_event *kbd_fetch_ptr;
305 /* Pointer to next place to store character in kbd_buffer. This
306 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
307 character should go in kbd_buffer[0]. */
308 #ifdef __STDC__
309 volatile
310 #endif
311 static struct input_event *kbd_store_ptr;
313 /* The above pair of variables forms a "queue empty" flag. When we
314 enqueue a non-hook event, we increment kbd_write_count. When we
315 dequeue a non-hook event, we increment kbd_read_count. We say that
316 there is input available iff the two counters are not equal.
318 Why not just have a flag set and cleared by the enqueuing and
319 dequeuing functions? Such a flag could be screwed up by interrupts
320 at inopportune times. */
322 /* If this flag is non-zero, we will check mouse_moved to see when the
323 mouse moves, and motion events will appear in the input stream. If
324 it is zero, mouse motion will be ignored. */
325 int do_mouse_tracking;
327 /* The window system handling code should set this if the mouse has
328 moved since the last call to the mouse_position_hook. Calling that
329 hook should clear this. Code assumes that if this is set, it can
330 call mouse_position_hook to get the promised position, so don't set
331 it unless you're prepared to substantiate the claim! */
332 int mouse_moved;
334 /* True iff there is an event in kbd_buffer, or if mouse tracking is
335 enabled and there is a new mouse position in the mouse movement
336 buffer. Note that if this is false, that doesn't mean that there
337 is readable input; all the events in the queue might be button-up
338 events, and do_mouse_tracking might be off. */
339 #define EVENT_QUEUES_EMPTY \
340 ((kbd_fetch_ptr == kbd_store_ptr) && (!do_mouse_tracking || !mouse_moved))
343 /* Symbols to head events. */
344 Lisp_Object Qmouse_movement;
345 Lisp_Object Qscroll_bar_movement;
347 Lisp_Object Qswitch_frame;
349 /* Symbols to denote kinds of events. */
350 Lisp_Object Qfunction_key;
351 Lisp_Object Qmouse_click;
352 /* Lisp_Object Qmouse_movement; - also an event header */
354 /* Properties of event headers. */
355 Lisp_Object Qevent_kind;
356 Lisp_Object Qevent_symbol_elements;
358 /* An event header symbol HEAD may have a property named
359 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
360 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
361 mask of modifiers applied to it. If present, this is used to help
362 speed up parse_modifiers. */
363 Lisp_Object Qevent_symbol_element_mask;
365 /* An unmodified event header BASE may have a property named
366 Qmodifier_cache, which is an alist mapping modifier masks onto
367 modified versions of BASE. If present, this helps speed up
368 apply_modifiers. */
369 Lisp_Object Qmodifier_cache;
371 /* Symbols to use for parts of windows. */
372 Lisp_Object Qmode_line;
373 Lisp_Object Qvertical_line;
374 Lisp_Object Qvertical_scroll_bar;
375 Lisp_Object Qmenu_bar;
377 extern Lisp_Object Qmenu_enable;
379 Lisp_Object recursive_edit_unwind (), command_loop ();
380 Lisp_Object Fthis_command_keys ();
382 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
383 happens. */
384 EMACS_TIME *input_available_clear_time;
386 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
387 Default is 1 if INTERRUPT_INPUT is defined. */
388 int interrupt_input;
390 /* Nonzero while interrupts are temporarily deferred during redisplay. */
391 int interrupts_deferred;
393 /* nonzero means use ^S/^Q for flow control. */
394 int flow_control;
396 /* Allow m- file to inhibit use of FIONREAD. */
397 #ifdef BROKEN_FIONREAD
398 #undef FIONREAD
399 #endif
401 /* We are unable to use interrupts if FIONREAD is not available,
402 so flush SIGIO so we won't try. */
403 #ifndef FIONREAD
404 #ifdef SIGIO
405 #undef SIGIO
406 #endif
407 #endif
409 /* If we support X Windows, and won't get an interrupt when input
410 arrives from the server, poll periodically so we can detect C-g. */
411 #ifdef HAVE_X_WINDOWS
412 #ifndef SIGIO
413 #define POLL_FOR_INPUT
414 #endif
415 #endif
417 /* Global variable declarations. */
419 /* Function for init_keyboard to call with no args (if nonzero). */
420 void (*keyboard_init_hook) ();
422 static int read_avail_input ();
423 static void get_input_pending ();
425 /* > 0 if we are to echo keystrokes. */
426 static int echo_keystrokes;
428 /* Nonzero means echo each character as typed. */
429 static int immediate_echo;
431 /* The text we're echoing in the modeline - partial key sequences,
432 usually. '\0'-terminated. This really shouldn't have a fixed size. */
433 static char echobuf[300];
435 /* Where to append more text to echobuf if we want to. */
436 static char *echoptr;
438 #define min(a,b) ((a)<(b)?(a):(b))
439 #define max(a,b) ((a)>(b)?(a):(b))
441 /* Install the string STR as the beginning of the string of echoing,
442 so that it serves as a prompt for the next character.
443 Also start echoing. */
445 echo_prompt (str)
446 char *str;
448 int len = strlen (str);
449 if (len > sizeof echobuf - 4)
450 len = sizeof echobuf - 4;
451 bcopy (str, echobuf, len);
452 echoptr = echobuf + len;
453 *echoptr = '\0';
455 echo ();
458 /* Add C to the echo string, if echoing is going on.
459 C can be a character, which is printed prettily ("M-C-x" and all that
460 jazz), or a symbol, whose name is printed. */
462 echo_char (c)
463 Lisp_Object c;
465 extern char *push_key_description ();
467 if (immediate_echo)
469 char *ptr = echoptr;
471 if (ptr != echobuf)
472 *ptr++ = ' ';
474 /* If someone has passed us a composite event, use its head symbol. */
475 c = EVENT_HEAD (c);
477 if (XTYPE (c) == Lisp_Int)
479 if (ptr - echobuf > sizeof echobuf - 6)
480 return;
482 ptr = push_key_description (XINT (c), ptr);
484 else if (XTYPE (c) == Lisp_Symbol)
486 struct Lisp_String *name = XSYMBOL (c)->name;
487 if (((ptr - echobuf) + name->size + 4) > sizeof echobuf)
488 return;
489 bcopy (name->data, ptr, name->size);
490 ptr += name->size;
493 if (echoptr == echobuf && EQ (c, help_char))
495 strcpy (ptr, " (Type ? for further options)");
496 ptr += strlen (ptr);
499 *ptr = 0;
500 echoptr = ptr;
502 echo ();
506 /* Temporarily add a dash to the end of the echo string if it's not
507 empty, so that it serves as a mini-prompt for the very next character. */
509 echo_dash ()
511 if (!immediate_echo && echoptr == echobuf)
512 return;
514 /* Put a dash at the end of the buffer temporarily,
515 but make it go away when the next character is added. */
516 echoptr[0] = '-';
517 echoptr[1] = 0;
519 echo ();
522 /* Display the current echo string, and begin echoing if not already
523 doing so. */
525 echo ()
527 if (!immediate_echo)
529 int i;
530 immediate_echo = 1;
532 for (i = 0; i < this_command_key_count; i++)
533 echo_char (XVECTOR (this_command_keys)->contents[i]);
534 echo_dash ();
537 echoing = 1;
538 message1 (echobuf);
539 echoing = 0;
541 if (waiting_for_input && !NILP (Vquit_flag))
542 quit_throw_to_read_char ();
545 /* Turn off echoing, for the start of a new command. */
547 cancel_echoing ()
549 immediate_echo = 0;
550 echoptr = echobuf;
553 /* Return the length of the current echo string. */
555 static int
556 echo_length ()
558 return echoptr - echobuf;
561 /* Truncate the current echo message to its first LEN chars.
562 This and echo_char get used by read_key_sequence when the user
563 switches frames while entering a key sequence. */
565 static void
566 echo_truncate (len)
567 int len;
569 echobuf[len] = '\0';
570 echoptr = echobuf + len;
574 /* Functions for manipulating this_command_keys. */
575 static void
576 add_command_key (key)
577 Lisp_Object key;
579 int size = XVECTOR (this_command_keys)->size;
581 if (this_command_key_count >= size)
583 Lisp_Object new_keys = Fmake_vector (make_number (size * 2), Qnil);
585 bcopy (XVECTOR (this_command_keys)->contents,
586 XVECTOR (new_keys)->contents,
587 size * sizeof (Lisp_Object));
589 this_command_keys = new_keys;
592 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
595 Lisp_Object
596 recursive_edit_1 ()
598 int count = specpdl_ptr - specpdl;
599 Lisp_Object val;
601 if (command_loop_level > 0)
603 specbind (Qstandard_output, Qt);
604 specbind (Qstandard_input, Qt);
607 val = command_loop ();
608 if (EQ (val, Qt))
609 Fsignal (Qquit, Qnil);
611 return unbind_to (count, Qnil);
614 /* When an auto-save happens, record the "time", and don't do again soon. */
615 record_auto_save ()
617 last_auto_save = num_nonmacro_input_chars;
620 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
621 "Invoke the editor command loop recursively.\n\
622 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
623 that tells this function to return.\n\
624 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
625 This function is called by the editor initialization to begin editing.")
628 int count = specpdl_ptr - specpdl;
629 Lisp_Object val;
631 command_loop_level++;
632 update_mode_lines = 1;
634 record_unwind_protect (recursive_edit_unwind,
635 (command_loop_level
636 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
637 ? Fcurrent_buffer ()
638 : Qnil);
639 recursive_edit_1 ();
640 return unbind_to (count, Qnil);
643 Lisp_Object
644 recursive_edit_unwind (buffer)
645 Lisp_Object buffer;
647 if (!NILP (buffer))
648 Fset_buffer (buffer);
650 command_loop_level--;
651 update_mode_lines = 1;
652 return Qnil;
655 Lisp_Object
656 cmd_error (data)
657 Lisp_Object data;
659 Lisp_Object errmsg, tail, errname, file_error;
660 Lisp_Object stream;
661 struct gcpro gcpro1;
662 int i;
664 Vquit_flag = Qnil;
665 Vinhibit_quit = Qt;
666 Vstandard_output = Qt;
667 Vstandard_input = Qt;
668 Vexecuting_macro = Qnil;
669 echo_area_glyphs = 0;
671 /* If the window system or terminal frame hasn't been initialized
672 yet, or we're not interactive, it's best to dump this message out
673 to stderr and exit. */
674 if (! FRAME_MESSAGE_BUF (selected_frame)
675 || noninteractive)
676 stream = Qexternal_debugging_output;
677 else
679 Fdiscard_input ();
680 bitch_at_user ();
681 stream = Qt;
684 errname = Fcar (data);
686 if (EQ (errname, Qerror))
688 data = Fcdr (data);
689 if (!CONSP (data)) data = Qnil;
690 errmsg = Fcar (data);
691 file_error = Qnil;
693 else
695 errmsg = Fget (errname, Qerror_message);
696 file_error = Fmemq (Qfile_error,
697 Fget (errname, Qerror_conditions));
700 /* Print an error message including the data items.
701 This is done by printing it into a scratch buffer
702 and then making a copy of the text in the buffer. */
704 if (!CONSP (data)) data = Qnil;
705 tail = Fcdr (data);
706 GCPRO1 (tail);
708 /* For file-error, make error message by concatenating
709 all the data items. They are all strings. */
710 if (!NILP (file_error) && !NILP (tail))
711 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
713 if (XTYPE (errmsg) == Lisp_String)
714 Fprinc (errmsg, stream);
715 else
716 write_string_1 ("peculiar error", -1, stream);
718 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
720 write_string_1 (i ? ", " : ": ", 2, stream);
721 if (!NILP (file_error))
722 Fprinc (Fcar (tail), stream);
723 else
724 Fprin1 (Fcar (tail), stream);
726 UNGCPRO;
728 /* If the window system or terminal frame hasn't been initialized
729 yet, or we're in -batch mode, this error should cause Emacs to exit. */
730 if (! FRAME_MESSAGE_BUF (selected_frame)
731 || noninteractive)
733 Fterpri (stream);
734 Fkill_emacs (make_number (-1));
737 Vquit_flag = Qnil;
739 Vinhibit_quit = Qnil;
740 return make_number (0);
743 Lisp_Object command_loop_1 ();
744 Lisp_Object command_loop_2 ();
745 Lisp_Object top_level_1 ();
747 /* Entry to editor-command-loop.
748 This level has the catches for exiting/returning to editor command loop.
749 It returns nil to exit recursive edit, t to abort it. */
751 Lisp_Object
752 command_loop ()
754 if (command_loop_level > 0 || minibuf_level > 0)
756 return internal_catch (Qexit, command_loop_2, Qnil);
758 else
759 while (1)
761 internal_catch (Qtop_level, top_level_1, Qnil);
762 internal_catch (Qtop_level, command_loop_2, Qnil);
764 /* End of file in -batch run causes exit here. */
765 if (noninteractive)
766 Fkill_emacs (Qt);
770 /* Here we catch errors in execution of commands within the
771 editing loop, and reenter the editing loop.
772 When there is an error, cmd_error runs and returns a non-nil
773 value to us. A value of nil means that cmd_loop_1 itself
774 returned due to end of file (or end of kbd macro). */
776 Lisp_Object
777 command_loop_2 ()
779 register Lisp_Object val;
782 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
783 while (!NILP (val));
785 return Qnil;
788 Lisp_Object
789 top_level_2 ()
791 return Feval (Vtop_level);
794 Lisp_Object
795 top_level_1 ()
797 /* On entry to the outer level, run the startup file */
798 if (!NILP (Vtop_level))
799 internal_condition_case (top_level_2, Qerror, cmd_error);
800 else if (!NILP (Vpurify_flag))
801 message ("Bare impure Emacs (standard Lisp code not loaded)");
802 else
803 message ("Bare Emacs (standard Lisp code not loaded)");
804 return Qnil;
807 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
808 "Exit all recursive editing levels.")
811 Fthrow (Qtop_level, Qnil);
814 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
815 "Exit from the innermost recursive edit or minibuffer.")
818 if (command_loop_level > 0 || minibuf_level > 0)
819 Fthrow (Qexit, Qnil);
821 error ("No recursive edit is in progress");
824 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
825 "Abort the command that requested this recursive edit or minibuffer input.")
828 if (command_loop_level > 0 || minibuf_level > 0)
829 Fthrow (Qexit, Qt);
831 error ("No recursive edit is in progress");
834 /* This is the actual command reading loop,
835 sans error-handling encapsulation. */
837 Lisp_Object Fcommand_execute ();
838 static int read_key_sequence ();
840 Lisp_Object
841 command_loop_1 ()
843 Lisp_Object cmd, tem;
844 int lose;
845 int nonundocount;
846 Lisp_Object keybuf[30];
847 int i;
848 int no_redisplay;
849 int no_direct;
850 int prev_modiff;
851 struct buffer *prev_buffer;
853 Vprefix_arg = Qnil;
854 Vdeactivate_mark = Qnil;
855 waiting_for_input = 0;
856 cancel_echoing ();
858 /* Don't clear out last_command at the beginning of a macro. */
859 if (XTYPE (Vexecuting_macro) != Lisp_String)
860 last_command = Qt;
862 nonundocount = 0;
863 no_redisplay = 0;
864 this_command_key_count = 0;
866 while (1)
868 /* Install chars successfully executed in kbd macro. */
870 if (defining_kbd_macro && NILP (Vprefix_arg))
871 finalize_kbd_macro_chars ();
873 /* Make sure the current window's buffer is selected. */
874 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
875 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
877 /* Display any malloc warning that just came out. Use while because
878 displaying one warning can cause another. */
880 while (pending_malloc_warning)
881 display_malloc_warning ();
883 no_direct = 0;
885 Vdeactivate_mark = Qnil;
887 /* If minibuffer on and echo area in use,
888 wait 2 sec and redraw minibufer. */
890 if (minibuf_level && echo_area_glyphs)
892 /* Bind inhibit-quit to t so that C-g gets read in
893 rather than quitting back to the minibuffer. */
894 int count = specpdl_ptr - specpdl;
895 specbind (Qinhibit_quit, Qt);
896 Fsit_for (make_number (2), Qnil, Qnil);
897 unbind_to (count, Qnil);
899 echo_area_glyphs = 0;
900 no_direct = 1;
901 if (!NILP (Vquit_flag))
903 Vquit_flag = Qnil;
904 unread_command_events = Fcons (make_number (quit_char), Qnil);
908 #ifdef C_ALLOCA
909 alloca (0); /* Cause a garbage collection now */
910 /* Since we can free the most stuff here. */
911 #endif /* C_ALLOCA */
913 #if 0
914 #ifdef MULTI_FRAME
915 /* Select the frame that the last event came from. Usually,
916 switch-frame events will take care of this, but if some lisp
917 code swallows a switch-frame event, we'll fix things up here.
918 Is this a good idea? */
919 if (XTYPE (internal_last_event_frame) == Lisp_Frame
920 && XFRAME (internal_last_event_frame) != selected_frame)
921 Fselect_frame (internal_last_event_frame, Qnil);
922 #endif
923 #endif
924 /* If it has changed current-menubar from previous value,
925 really recompute the menubar from the value. */
926 if (! NILP (Vlucid_menu_bar_dirty_flag))
927 call0 (Qrecompute_lucid_menubar);
929 #ifdef MULTI_FRAME
930 for (tem = Vframe_list; CONSP (tem); tem = XCONS (tem)->cdr)
932 struct frame *f = XFRAME (XCONS (tem)->car);
933 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
934 if (windows_or_buffers_changed
935 || (XFASTINT (w->last_modified) < MODIFF
936 && (XFASTINT (w->last_modified)
937 <= XBUFFER (w->buffer)->save_modified)))
939 struct buffer *prev = current_buffer;
940 current_buffer = XBUFFER (w->buffer);
941 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items ();
942 current_buffer = prev;
945 #endif /* MULTI_FRAME */
947 /* Read next key sequence; i gets its length. */
948 i = read_key_sequence (keybuf, (sizeof keybuf / sizeof (keybuf[0])), 0);
950 ++num_input_keys;
952 /* Now we have read a key sequence of length I,
953 or else I is 0 and we found end of file. */
955 if (i == 0) /* End of file -- happens only in */
956 return Qnil; /* a kbd macro, at the end. */
958 last_command_char = keybuf[i - 1];
960 cmd = read_key_sequence_cmd;
961 if (!NILP (Vexecuting_macro))
963 if (!NILP (Vquit_flag))
965 Vexecuting_macro = Qt;
966 QUIT; /* Make some noise. */
967 /* Will return since macro now empty. */
971 /* Do redisplay processing after this command except in special
972 cases identified below that set no_redisplay to 1. */
973 no_redisplay = 0;
975 prev_buffer = current_buffer;
976 prev_modiff = MODIFF;
978 /* Execute the command. */
980 this_command = cmd;
981 if (!NILP (Vpre_command_hook))
982 call1 (Vrun_hooks, Qpre_command_hook);
984 if (NILP (cmd))
986 /* nil means key is undefined. */
987 bitch_at_user ();
988 defining_kbd_macro = 0;
989 update_mode_lines = 1;
990 Vprefix_arg = Qnil;
993 else
995 if (NILP (Vprefix_arg) && ! no_direct)
997 /* Recognize some common commands in common situations and
998 do them directly. */
999 if (EQ (cmd, Qforward_char) && point < ZV)
1001 struct Lisp_Vector *dp
1002 = window_display_table (XWINDOW (selected_window));
1003 lose = FETCH_CHAR (point);
1004 SET_PT (point + 1);
1005 if ((dp
1006 ? (XTYPE (DISP_CHAR_VECTOR (dp, lose)) != Lisp_Vector
1007 && XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1)
1008 : (lose >= 0x20 && lose < 0x7f))
1009 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1010 >= MODIFF)
1011 && (XFASTINT (XWINDOW (selected_window)->last_point)
1012 == point - 1)
1013 && !windows_or_buffers_changed
1014 && EQ (current_buffer->selective_display, Qnil)
1015 && !detect_input_pending ()
1016 && NILP (Vexecuting_macro))
1017 no_redisplay = direct_output_forward_char (1);
1018 goto directly_done;
1020 else if (EQ (cmd, Qbackward_char) && point > BEGV)
1022 struct Lisp_Vector *dp
1023 = window_display_table (XWINDOW (selected_window));
1024 SET_PT (point - 1);
1025 lose = FETCH_CHAR (point);
1026 if ((dp
1027 ? (XTYPE (DISP_CHAR_VECTOR (dp, lose)) != Lisp_Vector
1028 && XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1)
1029 : (lose >= 0x20 && lose < 0x7f))
1030 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1031 >= MODIFF)
1032 && (XFASTINT (XWINDOW (selected_window)->last_point)
1033 == point + 1)
1034 && !windows_or_buffers_changed
1035 && EQ (current_buffer->selective_display, Qnil)
1036 && !detect_input_pending ()
1037 && NILP (Vexecuting_macro))
1038 no_redisplay = direct_output_forward_char (-1);
1039 goto directly_done;
1041 else if (EQ (cmd, Qself_insert_command)
1042 /* Try this optimization only on ascii keystrokes. */
1043 && XTYPE (last_command_char) == Lisp_Int)
1045 unsigned char c = XINT (last_command_char);
1047 if (NILP (Vexecuting_macro) &&
1048 !EQ (minibuf_window, selected_window))
1050 if (!nonundocount || nonundocount >= 20)
1052 Fundo_boundary ();
1053 nonundocount = 0;
1055 nonundocount++;
1057 lose = (XFASTINT (XWINDOW (selected_window)->last_modified)
1058 < MODIFF)
1059 || (XFASTINT (XWINDOW (selected_window)->last_point)
1060 != point)
1061 || MODIFF <= current_buffer->save_modified
1062 || windows_or_buffers_changed
1063 || !EQ (current_buffer->selective_display, Qnil)
1064 || detect_input_pending ()
1065 || !NILP (Vexecuting_macro);
1066 if (internal_self_insert (XINT (c), 0))
1068 lose = 1;
1069 nonundocount = 0;
1071 if (!lose &&
1072 (point == ZV || FETCH_CHAR (point) == '\n'))
1074 struct Lisp_Vector *dp
1075 = window_display_table (XWINDOW (selected_window));
1076 int lose = XINT (c);
1078 if (dp)
1080 Lisp_Object obj = DISP_CHAR_VECTOR (dp, lose);
1082 if (XTYPE (obj) == Lisp_Vector
1083 && XVECTOR (obj)->size == 1
1084 && (XTYPE (obj = XVECTOR (obj)->contents[0])
1085 == Lisp_Int))
1086 no_redisplay =
1087 direct_output_for_insert (XINT (obj));
1089 else
1091 if (lose >= 0x20 && lose <= 0x7e)
1092 no_redisplay = direct_output_for_insert (lose);
1095 goto directly_done;
1099 /* Here for a command that isn't executed directly */
1101 nonundocount = 0;
1102 if (NILP (Vprefix_arg))
1103 Fundo_boundary ();
1104 Fcommand_execute (cmd, Qnil);
1107 directly_done: ;
1109 if (!NILP (Vpost_command_hook))
1110 call1 (Vrun_hooks, Qpost_command_hook);
1112 /* If there is a prefix argument,
1113 1) We don't want last_command to be ``universal-argument''
1114 (that would be dumb), so don't set last_command,
1115 2) we want to leave echoing on so that the prefix will be
1116 echoed as part of this key sequence, so don't call
1117 cancel_echoing, and
1118 3) we want to leave this_command_key_count non-zero, so that
1119 read_char will realize that it is re-reading a character, and
1120 not echo it a second time. */
1121 if (NILP (Vprefix_arg))
1123 last_command = this_command;
1124 cancel_echoing ();
1125 this_command_key_count = 0;
1128 if (!NILP (current_buffer->mark_active))
1130 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1132 current_buffer->mark_active = Qnil;
1133 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1135 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1136 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1141 /* Number of seconds between polling for input. */
1142 int polling_period;
1144 /* Nonzero means polling for input is temporarily suppresed. */
1145 int poll_suppress_count;
1147 #ifdef POLL_FOR_INPUT
1148 int polling_for_input;
1150 /* Handle an alarm once each second and read pending input
1151 so as to handle a C-g if it comces in. */
1153 SIGTYPE
1154 input_poll_signal ()
1156 #ifdef HAVE_X_WINDOWS
1157 extern int x_input_blocked;
1158 if (x_input_blocked == 0)
1159 #endif
1160 if (!waiting_for_input)
1161 read_avail_input (0);
1162 signal (SIGALRM, input_poll_signal);
1163 alarm (polling_period);
1166 #endif
1168 /* Begin signals to poll for input, if they are appropriate.
1169 This function is called unconditionally from various places. */
1171 start_polling ()
1173 #ifdef POLL_FOR_INPUT
1174 if (read_socket_hook)
1176 poll_suppress_count--;
1177 if (poll_suppress_count == 0)
1179 signal (SIGALRM, input_poll_signal);
1180 polling_for_input = 1;
1181 alarm (polling_period);
1184 #endif
1187 /* Turn off polling. */
1189 stop_polling ()
1191 #ifdef POLL_FOR_INPUT
1192 if (read_socket_hook)
1194 if (poll_suppress_count == 0)
1196 polling_for_input = 0;
1197 alarm (0);
1199 poll_suppress_count++;
1201 #endif
1204 /* Input of single characters from keyboard */
1206 Lisp_Object print_help ();
1207 static Lisp_Object kbd_buffer_get_event ();
1209 /* read a character from the keyboard; call the redisplay if needed */
1210 /* commandflag 0 means do not do auto-saving, but do do redisplay.
1211 -1 means do not do redisplay, but do do autosaving.
1212 1 means do both. */
1214 /* The arguments MAPS and NMAPS are for menu prompting.
1215 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1217 PREV_EVENT is the previous input event, or nil if we are reading
1218 the first event of a key sequence.
1220 If USED_MOUSE_MENU is non-zero, then we set *USED_MOUSE_MENU to 1
1221 if we used a mouse menu to read the input, or zero otherwise. If
1222 USED_MOUSE_MENU is zero, *USED_MOUSE_MENU is left alone. */
1224 Lisp_Object
1225 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
1226 int commandflag;
1227 int nmaps;
1228 Lisp_Object *maps;
1229 Lisp_Object prev_event;
1230 int *used_mouse_menu;
1232 register Lisp_Object c;
1233 int count;
1234 jmp_buf save_jump;
1236 if (CONSP (unread_command_events))
1238 c = XCONS (unread_command_events)->car;
1239 unread_command_events = XCONS (unread_command_events)->cdr;
1241 if (this_command_key_count == 0)
1242 goto reread_first;
1243 else
1244 goto reread;
1247 if (unread_command_char != -1)
1249 XSET (c, Lisp_Int, unread_command_char);
1250 unread_command_char = -1;
1252 if (this_command_key_count == 0)
1253 goto reread_first;
1254 else
1255 goto reread;
1258 if (!NILP (Vexecuting_macro))
1260 #ifdef MULTI_FRAME
1261 /* We set this to Qmacro; since that's not a frame, nobody will
1262 try to switch frames on us, and the selected window will
1263 remain unchanged.
1265 Since this event came from a macro, it would be misleading to
1266 leave internal_last_event_frame set to whereever the last
1267 real event came from. Normally, a switch-frame event selects
1268 internal_last_event_frame after each command is read, but
1269 events read from a macro should never cause a new frame to be
1270 selected. */
1271 Vlast_event_frame = internal_last_event_frame = Qmacro;
1272 #endif
1274 if (executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
1276 XSET (c, Lisp_Int, -1);
1277 return c;
1280 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
1281 if (XTYPE (Vexecuting_macro) == Lisp_String
1282 && (XINT (c) & 0x80))
1283 XFASTINT (c) = CHAR_META | (XINT (c) & ~0x80);
1285 executing_macro_index++;
1287 goto from_macro;
1290 if (!NILP (unread_switch_frame))
1292 c = unread_switch_frame;
1293 unread_switch_frame = Qnil;
1295 /* This event should make it into this_command_keys, and get echoed
1296 again, so we go to reread_first, rather than reread. */
1297 goto reread_first;
1300 /* Save outer setjmp data, in case called recursively. */
1301 save_getcjmp (save_jump);
1303 stop_polling ();
1305 if (commandflag >= 0 && !input_pending && !detect_input_pending ())
1306 redisplay ();
1308 if (_setjmp (getcjmp))
1310 XSET (c, Lisp_Int, quit_char);
1311 #ifdef MULTI_FRAME
1312 XSET (internal_last_event_frame, Lisp_Frame, selected_frame);
1313 Vlast_event_frame = internal_last_event_frame;
1314 #endif
1316 goto non_reread;
1319 /* Message turns off echoing unless more keystrokes turn it on again. */
1320 if (echo_area_glyphs && *echo_area_glyphs && echo_area_glyphs != echobuf)
1321 cancel_echoing ();
1322 else
1323 /* If already echoing, continue. */
1324 echo_dash ();
1326 /* If in middle of key sequence and minibuffer not active,
1327 start echoing if enough time elapses. */
1328 if (minibuf_level == 0 && !immediate_echo && this_command_key_count > 0
1329 && echo_keystrokes > 0
1330 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0))
1332 Lisp_Object tem0;
1334 /* After a mouse event, start echoing right away.
1335 This is because we are probably about to display a menu,
1336 and we don't want to delay before doing so. */
1337 if (EVENT_HAS_PARAMETERS (prev_event))
1338 echo ();
1339 else
1341 tem0 = sit_for (echo_keystrokes, 0, 1, 1);
1342 if (EQ (tem0, Qt))
1343 echo ();
1347 /* Maybe auto save due to number of keystrokes or idle time. */
1349 if (commandflag != 0
1350 && auto_save_interval > 0
1351 && num_nonmacro_input_chars - last_auto_save > max (auto_save_interval, 20)
1352 && !detect_input_pending ())
1354 jmp_buf temp;
1355 save_getcjmp (temp);
1356 Fdo_auto_save (Qnil, Qnil);
1357 restore_getcjmp (temp);
1360 /* Try reading a character via menu prompting.
1361 Try this before the sit-for, because the sit-for
1362 would do the wrong thing if we are supposed to do
1363 menu prompting. */
1364 c = Qnil;
1365 if (INTERACTIVE && !NILP (prev_event))
1366 c = read_char_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
1368 /* Slow down auto saves logarithmically in size of current buffer,
1369 and garbage collect while we're at it. */
1370 if (NILP (c))
1372 int delay_level, buffer_size;
1374 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
1375 last_non_minibuf_size = Z - BEG;
1376 buffer_size = (last_non_minibuf_size >> 8) + 1;
1377 delay_level = 0;
1378 while (buffer_size > 64)
1379 delay_level++, buffer_size -= buffer_size >> 2;
1380 if (delay_level < 4) delay_level = 4;
1381 /* delay_level is 4 for files under around 50k, 7 at 100k,
1382 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
1384 /* Auto save if enough time goes by without input. */
1385 if (commandflag != 0
1386 && num_nonmacro_input_chars > last_auto_save
1387 && XTYPE (Vauto_save_timeout) == Lisp_Int
1388 && XINT (Vauto_save_timeout) > 0)
1390 Lisp_Object tem0;
1391 int delay = delay_level * XFASTINT (Vauto_save_timeout) / 4;
1392 tem0 = sit_for (delay, 0, 1, 1);
1393 if (EQ (tem0, Qt))
1395 jmp_buf temp;
1396 save_getcjmp (temp);
1397 Fdo_auto_save (Qnil, Qnil);
1398 restore_getcjmp (temp);
1400 /* If we have auto-saved and there is still no input
1401 available, garbage collect if there has been enough
1402 consing going on to make it worthwhile. */
1403 if (!detect_input_pending ()
1404 && consing_since_gc > gc_cons_threshold / 2)
1405 Fgarbage_collect ();
1410 /* Actually read a character, waiting if necessary. */
1411 if (NILP (c))
1412 c = kbd_buffer_get_event ();
1414 if (NILP (c))
1415 abort (); /* Don't think this can happen. */
1417 /* Terminate Emacs in batch mode if at eof. */
1418 if (noninteractive && XTYPE (c) == Lisp_Int && XINT (c) < 0)
1419 Fkill_emacs (make_number (1));
1421 /* Test for ControlMask and Mod1Mask. */
1422 if (extra_keyboard_modifiers & 4)
1423 c &= ~0140;
1424 if (extra_keyboard_modifiers & 8)
1425 c |= 0200;
1427 non_reread:
1429 restore_getcjmp (save_jump);
1431 start_polling ();
1433 echo_area_glyphs = 0;
1435 /* Handle things that only apply to characters. */
1436 if (XTYPE (c) == Lisp_Int)
1438 /* If kbd_buffer_get_event gave us an EOF, return that. */
1439 if (XINT (c) == -1)
1440 return c;
1442 if (XTYPE (Vkeyboard_translate_table) == Lisp_String
1443 && XSTRING (Vkeyboard_translate_table)->size > XFASTINT (c))
1444 XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[XFASTINT (c)]);
1447 total_keys++;
1448 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
1449 if (++recent_keys_index >= NUM_RECENT_KEYS)
1450 recent_keys_index = 0;
1452 /* Write c to the dribble file. If c is a lispy event, write
1453 the event's symbol to the dribble file, in <brackets>. Bleaugh.
1454 If you, dear reader, have a better idea, you've got the source. :-) */
1455 if (dribble)
1457 if (XTYPE (c) == Lisp_Int)
1458 putc (XINT (c), dribble);
1459 else
1461 Lisp_Object dribblee = c;
1463 /* If it's a structured event, take the event header. */
1464 dribblee = EVENT_HEAD (dribblee);
1466 if (XTYPE (dribblee) == Lisp_Symbol)
1468 putc ('<', dribble);
1469 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
1470 XSYMBOL (dribblee)->name->size,
1471 dribble);
1472 putc ('>', dribble);
1476 fflush (dribble);
1479 store_kbd_macro_char (c);
1481 num_nonmacro_input_chars++;
1483 from_macro:
1484 reread_first:
1486 /* Record this character as part of the current key.
1487 Don't record mouse motion; it should never matter. */
1488 if (! (EVENT_HAS_PARAMETERS (c)
1489 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
1491 echo_char (c);
1492 add_command_key (c);
1495 /* Re-reading in the middle of a command */
1496 reread:
1497 last_input_char = c;
1498 num_input_chars++;
1500 /* Process the help character specially if enabled */
1501 if (EQ (c, help_char) && !NILP (Vhelp_form))
1503 Lisp_Object tem0;
1504 count = specpdl_ptr - specpdl;
1506 record_unwind_protect (Fset_window_configuration,
1507 Fcurrent_window_configuration (Qnil));
1509 tem0 = Feval (Vhelp_form);
1510 if (XTYPE (tem0) == Lisp_String)
1511 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
1513 cancel_echoing ();
1514 c = read_char (0, 0, 0, Qnil, 0);
1515 /* Remove the help from the frame */
1516 unbind_to (count, Qnil);
1517 redisplay ();
1518 if (EQ (c, make_number (040)))
1520 cancel_echoing ();
1521 c = read_char (0, 0, 0, Qnil, 0);
1525 return c;
1528 Lisp_Object
1529 print_help (object)
1530 Lisp_Object object;
1532 Fprinc (object, Qnil);
1533 return Qnil;
1536 /* Copy out or in the info on where C-g should throw to.
1537 This is used when running Lisp code from within get_char,
1538 in case get_char is called recursively.
1539 See read_process_output. */
1541 save_getcjmp (temp)
1542 jmp_buf temp;
1544 bcopy (getcjmp, temp, sizeof getcjmp);
1547 restore_getcjmp (temp)
1548 jmp_buf temp;
1550 bcopy (temp, getcjmp, sizeof getcjmp);
1554 /* Low level keyboard/mouse input.
1555 kbd_buffer_store_event places events in kbd_buffer, and
1556 kbd_buffer_get_event retrieves them.
1557 mouse_moved indicates when the mouse has moved again, and
1558 *mouse_position_hook provides the mouse position. */
1560 /* Set this for debugging, to have a way to get out */
1561 int stop_character;
1563 extern int frame_garbaged;
1565 /* Return true iff there are any events in the queue that read-char
1566 would return. If this returns false, a read-char would block. */
1567 static int
1568 readable_events ()
1570 return ! EVENT_QUEUES_EMPTY;
1574 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
1575 of this function. */
1576 static Lisp_Object
1577 tracking_off (old_value)
1578 Lisp_Object old_value;
1580 if (! XFASTINT (old_value))
1582 do_mouse_tracking = 0;
1584 /* Redisplay may have been preempted because there was input
1585 available, and it assumes it will be called again after the
1586 input has been processed. If the only input available was
1587 the sort that we have just disabled, then we need to call
1588 redisplay. */
1589 if (!readable_events ())
1591 redisplay_preserve_echo_area ();
1592 get_input_pending (&input_pending);
1597 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
1598 "Evaluate BODY with mouse movement events enabled.\n\
1599 Within a `track-mouse' form, mouse motion generates input events that\n\
1600 you can read with `read-event'.\n\
1601 Normally, mouse motion is ignored.")
1602 (args)
1603 Lisp_Object args;
1605 int count = specpdl_ptr - specpdl;
1606 Lisp_Object val;
1608 XSET (val, Lisp_Int, do_mouse_tracking);
1609 record_unwind_protect (tracking_off, val);
1611 do_mouse_tracking = 1;
1613 val = Fprogn (args);
1614 return unbind_to (count, val);
1617 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
1619 void
1620 kbd_buffer_store_event (event)
1621 register struct input_event *event;
1623 if (event->kind == no_event)
1624 abort ();
1626 if (event->kind == ascii_keystroke)
1628 register int c = XFASTINT (event->code) & 0377;
1630 if (c == quit_char)
1632 extern SIGTYPE interrupt_signal ();
1634 #ifdef MULTI_FRAME
1635 /* If this results in a quit_char being returned to Emacs as
1636 input, set Vlast_event_frame properly. If this doesn't
1637 get returned to Emacs as an event, the next event read
1638 will set Vlast_event_frame again, so this is safe to do. */
1640 Lisp_Object focus =
1641 FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
1643 if (NILP (focus))
1644 internal_last_event_frame = event->frame_or_window;
1645 else
1646 internal_last_event_frame = focus;
1647 Vlast_event_frame = internal_last_event_frame;
1649 #endif
1651 last_event_timestamp = event->timestamp;
1652 interrupt_signal ();
1653 return;
1656 if (c && c == stop_character)
1658 sys_suspend ();
1659 return;
1662 XSET (event->code, Lisp_Int, c);
1665 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
1666 kbd_store_ptr = kbd_buffer;
1668 /* Don't let the very last slot in the buffer become full,
1669 since that would make the two pointers equal,
1670 and that is indistinguishable from an empty buffer.
1671 Discard the event if it would fill the last slot. */
1672 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
1674 kbd_store_ptr->kind = event->kind;
1675 kbd_store_ptr->code = event->code;
1676 kbd_store_ptr->part = event->part;
1677 kbd_store_ptr->frame_or_window = event->frame_or_window;
1678 kbd_store_ptr->modifiers = event->modifiers;
1679 kbd_store_ptr->x = event->x;
1680 kbd_store_ptr->y = event->y;
1681 kbd_store_ptr->timestamp = event->timestamp;
1682 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
1683 - kbd_buffer]
1684 = event->frame_or_window);
1686 kbd_store_ptr++;
1690 static Lisp_Object make_lispy_event ();
1691 static Lisp_Object make_lispy_movement ();
1692 static Lisp_Object modify_event_symbol ();
1693 static Lisp_Object make_lispy_switch_frame ();
1695 static Lisp_Object
1696 kbd_buffer_get_event ()
1698 register int c;
1699 Lisp_Object obj;
1701 if (noninteractive)
1703 c = getchar ();
1704 XSET (obj, Lisp_Int, c);
1705 return obj;
1708 retry:
1709 /* Wait until there is input available. */
1710 for (;;)
1712 if (!EVENT_QUEUES_EMPTY)
1713 break;
1715 /* If the quit flag is set, then read_char will return
1716 quit_char, so that counts as "available input." */
1717 if (!NILP (Vquit_flag))
1718 quit_throw_to_read_char ();
1720 /* One way or another, wait until input is available; then, if
1721 interrupt handlers have not read it, read it now. */
1723 #ifdef OLDVMS
1724 wait_for_kbd_input ();
1725 #else
1726 /* Note SIGIO has been undef'd if FIONREAD is missing. */
1727 #ifdef SIGIO
1728 gobble_input (0);
1729 #endif /* SIGIO */
1730 if (EVENT_QUEUES_EMPTY)
1732 Lisp_Object minus_one;
1734 XSET (minus_one, Lisp_Int, -1);
1735 wait_reading_process_input (0, 0, minus_one, 1);
1737 if (!interrupt_input && EVENT_QUEUES_EMPTY)
1739 read_avail_input (0);
1742 #endif /* not VMS */
1745 /* At this point, we know that there is a readable event available
1746 somewhere. If the event queue is empty, then there must be a
1747 mouse movement enabled and available. */
1748 if (kbd_fetch_ptr != kbd_store_ptr)
1750 struct input_event *event;
1752 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
1753 ? kbd_fetch_ptr
1754 : kbd_buffer);
1756 last_event_timestamp = event->timestamp;
1758 obj = Qnil;
1760 /* These two kinds of events get special handling
1761 and don't actually appear to the command loop. */
1762 if (event->kind == selection_request_event)
1764 x_handle_selection_request (event);
1765 kbd_fetch_ptr = event + 1;
1766 goto retry;
1769 if (event->kind == selection_clear_event)
1771 x_handle_selection_clear (event);
1772 kbd_fetch_ptr = event + 1;
1773 goto retry;
1776 #ifdef MULTI_FRAME
1777 /* If this event is on a different frame, return a switch-frame this
1778 time, and leave the event in the queue for next time. */
1780 Lisp_Object frame = event->frame_or_window;
1781 Lisp_Object focus;
1783 if (XTYPE (frame) == Lisp_Window)
1784 frame = WINDOW_FRAME (XWINDOW (frame));
1786 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
1787 if (! NILP (focus))
1788 frame = focus;
1790 if (! EQ (frame, internal_last_event_frame))
1792 internal_last_event_frame = frame;
1793 obj = make_lispy_switch_frame (frame);
1796 #endif
1798 /* If we didn't decide to make a switch-frame event, go ahead
1799 and build a real event from the queue entry. */
1800 if (NILP (obj))
1802 obj = make_lispy_event (event);
1804 /* Wipe out this event, to catch bugs. */
1805 event->kind = no_event;
1806 (XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer]
1807 = Qnil);
1809 kbd_fetch_ptr = event + 1;
1812 else if (do_mouse_tracking && mouse_moved)
1814 FRAME_PTR f;
1815 Lisp_Object bar_window;
1816 enum scroll_bar_part part;
1817 Lisp_Object x, y;
1818 unsigned long time;
1820 (*mouse_position_hook) (&f, &bar_window, &part, &x, &y, &time);
1822 obj = Qnil;
1824 #ifdef MULTI_FRAME
1825 /* Decide if we should generate a switch-frame event. Don't
1826 generate switch-frame events for motion outside of all Emacs
1827 frames. */
1828 if (f)
1830 Lisp_Object frame = FRAME_FOCUS_FRAME (f);
1832 if (NILP (frame))
1833 XSET (frame, Lisp_Frame, f);
1835 if (! EQ (frame, internal_last_event_frame))
1837 XSET (internal_last_event_frame, Lisp_Frame, frame);
1838 obj = make_lispy_switch_frame (internal_last_event_frame);
1841 #endif
1843 /* If we didn't decide to make a switch-frame event, go ahead and
1844 return a mouse-motion event. */
1845 if (NILP (obj))
1846 obj = make_lispy_movement (f, bar_window, part, x, y, time);
1848 else
1849 /* We were promised by the above while loop that there was
1850 something for us to read! */
1851 abort ();
1853 /* If something gave back nil as the Lispy event,
1854 it means the event was discarded, so try again. */
1855 if (NILP (obj))
1856 goto retry;
1858 input_pending = readable_events ();
1860 #ifdef MULTI_FRAME
1861 Vlast_event_frame = internal_last_event_frame;
1862 #endif
1864 return (obj);
1868 /* Caches for modify_event_symbol. */
1869 static Lisp_Object func_key_syms;
1870 static Lisp_Object mouse_syms;
1872 /* You'll notice that this table is arranged to be conveniently
1873 indexed by X Windows keysym values. */
1874 static char *lispy_function_keys[] =
1876 /* X Keysym value */
1878 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00 */
1879 "backspace",
1880 "tab",
1881 "linefeed",
1882 "clear",
1884 "return",
1885 0, 0,
1886 0, 0, 0, /* 0xff10 */
1887 "pause",
1888 0, 0, 0, 0, 0, 0, 0,
1889 "escape",
1890 0, 0, 0, 0,
1891 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff20...2f */
1892 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff30...3f */
1893 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
1895 "home", /* 0xff50 */ /* IsCursorKey */
1896 "left",
1897 "up",
1898 "right",
1899 "down",
1900 "prior",
1901 "next",
1902 "end",
1903 "begin",
1904 0, /* 0xff59 */
1905 0, 0, 0, 0, 0, 0,
1906 "select", /* 0xff60 */ /* IsMiscFunctionKey */
1907 "print",
1908 "execute",
1909 "insert",
1910 0, /* 0xff64 */
1911 "undo",
1912 "redo",
1913 "menu",
1914 "find",
1915 "cancel",
1916 "help",
1917 "break", /* 0xff6b */
1919 /* Here are some keys found mostly on HP keyboards. The X event
1920 handling code will strip bit 29, which flags vendor-specific
1921 keysyms. */
1922 "reset", /* 0x1000ff6c */
1923 "system",
1924 "user",
1925 "clearline",
1926 "insertline",
1927 "deleteline",
1928 "insertchar",
1929 "deletechar",
1930 "backtab",
1931 "kp_backtab", /* 0x1000ff75 */
1932 0, /* 0xff76 */
1933 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff7f */
1934 "kp-space", /* 0xff80 */ /* IsKeypadKey */
1935 0, 0, 0, 0, 0, 0, 0, 0,
1936 "kp-tab", /* 0xff89 */
1937 0, 0, 0,
1938 "kp-enter", /* 0xff8d */
1939 0, 0, 0,
1940 "kp-f1", /* 0xff91 */
1941 "kp-f2",
1942 "kp-f3",
1943 "kp-f4",
1944 0, /* 0xff95 */
1945 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1946 "kp-multiply", /* 0xffaa */
1947 "kp-add",
1948 "kp-separator",
1949 "kp-subtract",
1950 "kp-decimal",
1951 "kp-divide", /* 0xffaf */
1952 "kp-0", /* 0xffb0 */
1953 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
1954 0, /* 0xffba */
1955 0, 0,
1956 "kp-equal", /* 0xffbd */
1957 "f1", /* 0xffbe */ /* IsFunctionKey */
1958 "f2",
1959 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
1960 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
1961 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
1962 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
1963 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
1964 0, 0, 0, 0, 0, 0, 0, 0,
1965 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
1966 0, 0, 0, 0, 0, 0, 0, "delete"
1969 static char *lispy_mouse_names[] =
1971 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
1974 /* Scroll bar parts. */
1975 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
1977 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
1978 Lisp_Object *scroll_bar_parts[] = {
1979 &Qabove_handle, &Qhandle, &Qbelow_handle
1983 /* A vector, indexed by button number, giving the down-going location
1984 of currently depressed buttons, both scroll bar and non-scroll bar.
1986 The elements have the form
1987 (BUTTON-NUMBER MODIFIER-MASK . REST)
1988 where REST is the cdr of a position as it would be reported in the event.
1990 The make_lispy_event function stores positions here to tell the
1991 difference between click and drag events, and to store the starting
1992 location to be included in drag events. */
1994 static Lisp_Object button_down_location;
1996 /* Given a struct input_event, build the lisp event which represents
1997 it. If EVENT is 0, build a mouse movement event from the mouse
1998 movement buffer, which should have a movement event in it.
2000 Note that events must be passed to this function in the order they
2001 are received; this function stores the location of button presses
2002 in order to build drag events when the button is released. */
2004 static Lisp_Object
2005 make_lispy_event (event)
2006 struct input_event *event;
2008 #ifdef SWITCH_ENUM_BUG
2009 switch ((int) event->kind)
2010 #else
2011 switch (event->kind)
2012 #endif
2014 /* A simple keystroke. */
2015 case ascii_keystroke:
2017 int c = XFASTINT (event->code);
2018 /* Include the bits for control and shift
2019 only if the basic ASCII code can't indicate them. */
2020 if ((event->modifiers & ctrl_modifier)
2021 && c >= 040)
2022 c |= ctrl_modifier;
2023 if (XFASTINT (event->code) < 040
2024 && (event->modifiers & shift_modifier))
2025 c |= shift_modifier;
2026 c |= (event->modifiers
2027 & (meta_modifier | alt_modifier
2028 | hyper_modifier | super_modifier));
2029 return c;
2032 /* A function key. The symbol may need to have modifier prefixes
2033 tacked onto it. */
2034 case non_ascii_keystroke:
2035 return modify_event_symbol (XFASTINT (event->code), event->modifiers,
2036 Qfunction_key,
2037 lispy_function_keys, &func_key_syms,
2038 (sizeof (lispy_function_keys)
2039 / sizeof (lispy_function_keys[0])));
2040 break;
2042 /* A mouse click. Figure out where it is, decide whether it's
2043 a press, click or drag, and build the appropriate structure. */
2044 case mouse_click:
2045 case scroll_bar_click:
2047 int button = XFASTINT (event->code);
2048 Lisp_Object position;
2049 Lisp_Object *start_pos_ptr;
2050 Lisp_Object start_pos;
2052 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
2053 abort ();
2055 /* Build the position as appropriate for this mouse click. */
2056 if (event->kind == mouse_click)
2058 int part;
2059 struct frame *f = XFRAME (event->frame_or_window);
2060 Lisp_Object window
2061 = window_from_coordinates (f, XINT (event->x), XINT (event->y),
2062 &part);
2063 Lisp_Object posn;
2065 if (XINT (event->y) < FRAME_MENU_BAR_LINES (f))
2067 int hpos;
2068 Lisp_Object items;
2069 items = FRAME_MENU_BAR_ITEMS (f);
2070 for (; CONSP (items); items = XCONS (items)->cdr)
2072 Lisp_Object pos, string;
2073 pos = Fcdr (Fcdr (Fcar (items)));
2074 string = Fcar (Fcdr (Fcar (items)));
2075 if (XINT (event->x) > XINT (pos)
2076 && XINT (event->x) <= XINT (pos) + XSTRING (string)->size)
2077 break;
2079 position
2080 = Fcons (event->frame_or_window,
2081 Fcons (Qmenu_bar,
2082 Fcons (Fcons (event->x, event->y),
2083 Fcons (make_number (event->timestamp),
2084 Qnil))));
2086 if (CONSP (items))
2087 return Fcons (Fcar (Fcar (items)),
2088 Fcons (position, Qnil));
2089 else
2090 return Fcons (Qnil, Fcons (position, Qnil));
2092 else if (XTYPE (window) != Lisp_Window)
2093 posn = Qnil;
2094 else
2096 XSETINT (event->x,
2097 (XINT (event->x) - XINT (XWINDOW (window)->left)));
2098 XSETINT (event->y,
2099 (XINT (event->y) - XINT (XWINDOW (window)->top)));
2101 if (part == 1)
2102 posn = Qmode_line;
2103 else if (part == 2)
2104 posn = Qvertical_line;
2105 else
2106 XSET (posn, Lisp_Int,
2107 buffer_posn_from_coords (XWINDOW (window),
2108 XINT (event->x),
2109 XINT (event->y)));
2112 position
2113 = Fcons (window,
2114 Fcons (posn,
2115 Fcons (Fcons (event->x, event->y),
2116 Fcons (make_number (event->timestamp),
2117 Qnil))));
2119 else
2121 Lisp_Object window = event->frame_or_window;
2122 Lisp_Object portion_whole = Fcons (event->x, event->y);
2123 Lisp_Object part = *scroll_bar_parts[(int) event->part];
2125 position =
2126 Fcons (window,
2127 Fcons (Qvertical_scroll_bar,
2128 Fcons (portion_whole,
2129 Fcons (make_number (event->timestamp),
2130 Fcons (part,
2131 Qnil)))));
2134 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
2136 start_pos = *start_pos_ptr;
2137 *start_pos_ptr = Qnil;
2139 /* If this is a button press, squirrel away the location, so
2140 we can decide later whether it was a click or a drag. */
2141 if (event->modifiers & down_modifier)
2142 *start_pos_ptr = Fcopy_alist (position);
2144 /* Now we're releasing a button - check the co-ordinates to
2145 see if this was a click or a drag. */
2146 else if (event->modifiers & up_modifier)
2148 /* If we did not see a down before this up,
2149 ignore the up. Probably this happened because
2150 the down event chose a menu item.
2151 It would be an annoyance to treat the release
2152 of the button that chose the menu item
2153 as a separate event. */
2155 if (XTYPE (start_pos) != Lisp_Cons)
2156 return Qnil;
2158 event->modifiers &= ~up_modifier;
2159 #if 0 /* Formerly we treated an up with no down as a click event. */
2160 if (XTYPE (start_pos) != Lisp_Cons)
2161 event->modifiers |= click_modifier;
2162 else
2163 #endif
2165 /* The third element of every position should be the (x,y)
2166 pair. */
2167 Lisp_Object down = Fnth (make_number (2), start_pos);
2169 event->modifiers |= ((EQ (event->x, XCONS (down)->car)
2170 && EQ (event->y, XCONS (down)->cdr))
2171 ? click_modifier
2172 : drag_modifier);
2175 else
2176 /* Every mouse event should either have the down_modifier or
2177 the up_modifier set. */
2178 abort ();
2181 /* Get the symbol we should use for the mouse click. */
2182 Lisp_Object head
2183 = modify_event_symbol (button,
2184 event->modifiers,
2185 Qmouse_click,
2186 lispy_mouse_names, &mouse_syms,
2187 (sizeof (lispy_mouse_names)
2188 / sizeof (lispy_mouse_names[0])));
2190 if (event->modifiers & drag_modifier)
2191 return Fcons (head,
2192 Fcons (start_pos,
2193 Fcons (position,
2194 Qnil)));
2195 else
2196 return Fcons (head,
2197 Fcons (position,
2198 Qnil));
2202 /* The 'kind' field of the event is something we don't recognize. */
2203 default:
2204 abort ();
2208 static Lisp_Object
2209 make_lispy_movement (frame, bar_window, part, x, y, time)
2210 FRAME_PTR frame;
2211 Lisp_Object bar_window;
2212 enum scroll_bar_part part;
2213 Lisp_Object x, y;
2214 unsigned long time;
2216 /* Is it a scroll bar movement? */
2217 if (frame && ! NILP (bar_window))
2219 Lisp_Object part_sym = *scroll_bar_parts[(int) part];
2221 return Fcons (Qscroll_bar_movement,
2222 (Fcons (Fcons (bar_window,
2223 Fcons (Qvertical_scroll_bar,
2224 Fcons (Fcons (x, y),
2225 Fcons (make_number (time),
2226 Fcons (part_sym,
2227 Qnil))))),
2228 Qnil)));
2231 /* Or is it an ordinary mouse movement? */
2232 else
2234 int area;
2235 Lisp_Object window =
2236 (frame
2237 ? window_from_coordinates (frame, XINT (x), XINT (y), &area)
2238 : Qnil);
2239 Lisp_Object posn;
2241 if (XTYPE (window) == Lisp_Window)
2243 XSETINT (x, XINT (x) - XINT (XWINDOW (window)->left));
2244 XSETINT (y, XINT (y) - XINT (XWINDOW (window)->top));
2246 if (area == 1)
2247 posn = Qmode_line;
2248 else if (area == 2)
2249 posn = Qvertical_line;
2250 else
2251 XSET (posn, Lisp_Int,
2252 buffer_posn_from_coords (XWINDOW (window),
2253 XINT (x), XINT (y)));
2255 else
2257 window = Qnil;
2258 posn = Qnil;
2261 return Fcons (Qmouse_movement,
2262 Fcons (Fcons (window,
2263 Fcons (posn,
2264 Fcons (Fcons (x, y),
2265 Fcons (make_number (time),
2266 Qnil)))),
2267 Qnil));
2271 /* Construct a switch frame event. */
2272 static Lisp_Object
2273 make_lispy_switch_frame (frame)
2274 Lisp_Object frame;
2276 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
2279 /* Manipulating modifiers. */
2281 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
2283 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
2284 SYMBOL's name of the end of the modifiers; the string from this
2285 position is the unmodified symbol name.
2287 This doesn't use any caches. */
2288 static int
2289 parse_modifiers_uncached (symbol, modifier_end)
2290 Lisp_Object symbol;
2291 int *modifier_end;
2293 struct Lisp_String *name;
2294 int i;
2295 int modifiers;
2297 CHECK_SYMBOL (symbol, 1);
2299 modifiers = 0;
2300 name = XSYMBOL (symbol)->name;
2303 for (i = 0; i+2 <= name->size; )
2304 switch (name->data[i])
2306 #define SINGLE_LETTER_MOD(bit) \
2307 if (name->data[i+1] != '-') \
2308 goto no_more_modifiers; \
2309 modifiers |= bit; \
2310 i += 2;
2312 case 'A':
2313 SINGLE_LETTER_MOD (alt_modifier);
2314 break;
2316 case 'C':
2317 SINGLE_LETTER_MOD (ctrl_modifier);
2318 break;
2320 case 'H':
2321 SINGLE_LETTER_MOD (hyper_modifier);
2322 break;
2324 case 'M':
2325 SINGLE_LETTER_MOD (meta_modifier);
2326 break;
2328 case 'S':
2329 SINGLE_LETTER_MOD (shift_modifier);
2330 break;
2332 case 's':
2333 SINGLE_LETTER_MOD (super_modifier);
2334 break;
2336 case 'd':
2337 if (i + 5 > name->size)
2338 goto no_more_modifiers;
2339 if (! strncmp (name->data + i, "drag-", 5))
2341 modifiers |= drag_modifier;
2342 i += 5;
2344 else if (! strncmp (name->data + i, "down-", 5))
2346 modifiers |= down_modifier;
2347 i += 5;
2349 else
2350 goto no_more_modifiers;
2351 break;
2353 default:
2354 goto no_more_modifiers;
2356 #undef SINGLE_LETTER_MOD
2358 no_more_modifiers:
2360 /* Should we include the `click' modifier? */
2361 if (! (modifiers & (down_modifier | drag_modifier))
2362 && i + 7 == name->size
2363 && strncmp (name->data + i, "mouse-", 6) == 0
2364 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
2365 modifiers |= click_modifier;
2367 if (modifier_end)
2368 *modifier_end = i;
2370 return modifiers;
2374 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
2375 prepended to the string BASE[0..BASE_LEN-1].
2376 This doesn't use any caches. */
2377 static Lisp_Object
2378 apply_modifiers_uncached (modifiers, base, base_len)
2379 int modifiers;
2380 char *base;
2381 int base_len;
2383 /* Since BASE could contain nulls, we can't use intern here; we have
2384 to use Fintern, which expects a genuine Lisp_String, and keeps a
2385 reference to it. */
2386 char *new_mods =
2387 (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-"));
2388 int mod_len;
2391 char *p = new_mods;
2393 /* Only the event queue may use the `up' modifier; it should always
2394 be turned into a click or drag event before presented to lisp code. */
2395 if (modifiers & up_modifier)
2396 abort ();
2398 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
2399 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
2400 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
2401 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
2402 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
2403 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
2404 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
2405 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
2406 /* The click modifier is denoted by the absence of other modifiers. */
2408 *p = '\0';
2410 mod_len = p - new_mods;
2414 Lisp_Object new_name = make_uninit_string (mod_len + base_len);
2416 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
2417 bcopy (base, XSTRING (new_name)->data + mod_len, base_len);
2419 return Fintern (new_name, Qnil);
2424 static char *modifier_names[] =
2426 "up", 0, 0, 0, 0, 0, 0, "down",
2427 "drag", "click", 0, 0, 0, 0, 0, 0,
2428 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
2431 static Lisp_Object modifier_symbols;
2433 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
2434 static Lisp_Object
2435 lispy_modifier_list (modifiers)
2436 int modifiers;
2438 Lisp_Object modifier_list;
2439 int i;
2441 modifier_list = Qnil;
2442 for (i = 0; (1<<i) <= modifiers; i++)
2443 if (modifiers & (1<<i))
2445 if (i >= XVECTOR (modifier_symbols)->size)
2446 abort ();
2447 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
2448 modifier_list);
2451 return modifier_list;
2455 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
2456 where UNMODIFIED is the unmodified form of SYMBOL,
2457 MASK is the set of modifiers present in SYMBOL's name.
2458 This is similar to parse_modifiers_uncached, but uses the cache in
2459 SYMBOL's Qevent_symbol_element_mask property, and maintains the
2460 Qevent_symbol_elements property. */
2461 static Lisp_Object
2462 parse_modifiers (symbol)
2463 Lisp_Object symbol;
2465 Lisp_Object elements = Fget (symbol, Qevent_symbol_element_mask);
2467 if (CONSP (elements))
2468 return elements;
2469 else
2471 int end;
2472 int modifiers = parse_modifiers_uncached (symbol, &end);
2473 Lisp_Object unmodified
2474 = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
2475 XSYMBOL (symbol)->name->size - end),
2476 Qnil);
2477 Lisp_Object mask;
2479 XFASTINT (mask) = modifiers;
2480 elements = Fcons (unmodified, Fcons (mask, Qnil));
2482 /* Cache the parsing results on SYMBOL. */
2483 Fput (symbol, Qevent_symbol_element_mask,
2484 elements);
2485 Fput (symbol, Qevent_symbol_elements,
2486 Fcons (unmodified, lispy_modifier_list (modifiers)));
2488 /* Since we know that SYMBOL is modifiers applied to unmodified,
2489 it would be nice to put that in unmodified's cache.
2490 But we can't, since we're not sure that parse_modifiers is
2491 canonical. */
2493 return elements;
2497 /* Apply the modifiers MODIFIERS to the symbol BASE.
2498 BASE must be unmodified.
2500 This is like apply_modifiers_uncached, but uses BASE's
2501 Qmodifier_cache property, if present. It also builds
2502 Qevent_symbol_elements properties, since it has that info anyway.
2504 apply_modifiers copies the value of BASE's Qevent_kind property to
2505 the modified symbol. */
2506 static Lisp_Object
2507 apply_modifiers (modifiers, base)
2508 int modifiers;
2509 Lisp_Object base;
2511 Lisp_Object cache, index, entry, new_symbol;
2513 /* The click modifier never figures into cache indices. */
2514 cache = Fget (base, Qmodifier_cache);
2515 XFASTINT (index) = (modifiers & ~click_modifier);
2516 entry = Fassq (index, cache);
2518 if (CONSP (entry))
2519 new_symbol = XCONS (entry)->cdr;
2520 else
2522 /* We have to create the symbol ourselves. */
2523 new_symbol = apply_modifiers_uncached (modifiers,
2524 XSYMBOL (base)->name->data,
2525 XSYMBOL (base)->name->size);
2527 /* Add the new symbol to the base's cache. */
2528 entry = Fcons (index, new_symbol);
2529 Fput (base, Qmodifier_cache, Fcons (entry, cache));
2531 /* We have the parsing info now for free, so add it to the caches. */
2532 XFASTINT (index) = modifiers;
2533 Fput (new_symbol, Qevent_symbol_element_mask,
2534 Fcons (base, Fcons (index, Qnil)));
2535 Fput (new_symbol, Qevent_symbol_elements,
2536 Fcons (base, lispy_modifier_list (modifiers)));
2539 /* Make sure this symbol is of the same kind as BASE.
2541 You'd think we could just set this once and for all when we
2542 intern the symbol above, but reorder_modifiers may call us when
2543 BASE's property isn't set right; we can't assume that just
2544 because we found something in the cache it must have its kind set
2545 right. */
2546 if (NILP (Fget (new_symbol, Qevent_kind)))
2548 Lisp_Object kind = Fget (base, Qevent_kind);
2550 if (! NILP (kind))
2551 Fput (new_symbol, Qevent_kind, kind);
2554 return new_symbol;
2558 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
2559 return a symbol with the modifiers placed in the canonical order.
2560 Canonical order is alphabetical, except for down and drag, which
2561 always come last. The 'click' modifier is never written out.
2563 Fdefine_key calls this to make sure that (for example) C-M-foo
2564 and M-C-foo end up being equivalent in the keymap. */
2566 Lisp_Object
2567 reorder_modifiers (symbol)
2568 Lisp_Object symbol;
2570 /* It's hopefully okay to write the code this way, since everything
2571 will soon be in caches, and no consing will be done at all. */
2572 Lisp_Object parsed = parse_modifiers (symbol);
2574 return apply_modifiers (XCONS (XCONS (parsed)->cdr)->car,
2575 XCONS (parsed)->car);
2579 /* For handling events, we often want to produce a symbol whose name
2580 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
2581 to some base, like the name of a function key or mouse button.
2582 modify_event_symbol produces symbols of this sort.
2584 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
2585 is the name of the i'th symbol. TABLE_SIZE is the number of elements
2586 in the table.
2588 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
2589 persist between calls to modify_event_symbol that it can use to
2590 store a cache of the symbols it's generated for this NAME_TABLE
2591 before.
2593 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
2595 MODIFIERS is a set of modifier bits (as given in struct input_events)
2596 whose prefixes should be applied to the symbol name.
2598 SYMBOL_KIND is the value to be placed in the event_kind property of
2599 the returned symbol.
2601 The symbols we create are supposed to have an
2602 `event-symbol-elements' propery, which lists the modifiers present
2603 in the symbol's name. */
2605 static Lisp_Object
2606 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_table,
2607 symbol_table, table_size)
2608 int symbol_num;
2609 unsigned modifiers;
2610 Lisp_Object symbol_kind;
2611 char **name_table;
2612 Lisp_Object *symbol_table;
2613 int table_size;
2615 Lisp_Object *slot;
2617 /* Is this a request for a valid symbol? */
2618 if (symbol_num < 0 || symbol_num >= table_size)
2619 abort ();
2621 /* If *symbol_table doesn't seem to be initialized properly, fix that.
2622 *symbol_table should be a lisp vector TABLE_SIZE elements long,
2623 where the Nth element is the symbol for NAME_TABLE[N], or nil if
2624 we've never used that symbol before. */
2625 if (XTYPE (*symbol_table) != Lisp_Vector
2626 || XVECTOR (*symbol_table)->size != table_size)
2628 Lisp_Object size;
2630 XFASTINT (size) = table_size;
2631 *symbol_table = Fmake_vector (size, Qnil);
2634 slot = & XVECTOR (*symbol_table)->contents[symbol_num];
2636 /* Have we already used this symbol before? */
2637 if (NILP (*slot))
2639 /* No; let's create it. */
2640 *slot = intern (name_table[symbol_num]);
2642 /* Fill in the cache entries for this symbol; this also
2643 builds the Qevent_symbol_elements property, which the user
2644 cares about. */
2645 apply_modifiers (modifiers & click_modifier, *slot);
2646 Fput (*slot, Qevent_kind, symbol_kind);
2649 /* Apply modifiers to that symbol. */
2650 return apply_modifiers (modifiers, *slot);
2654 /* Store into *addr a value nonzero if terminal input chars are available.
2655 Serves the purpose of ioctl (0, FIONREAD, addr)
2656 but works even if FIONREAD does not exist.
2657 (In fact, this may actually read some input.) */
2659 static void
2660 get_input_pending (addr)
2661 int *addr;
2663 /* First of all, have we already counted some input? */
2664 *addr = !NILP (Vquit_flag) || readable_events ();
2666 /* If input is being read as it arrives, and we have none, there is none. */
2667 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
2668 return;
2670 /* Try to read some input and see how much we get. */
2671 gobble_input (0);
2672 *addr = !NILP (Vquit_flag) || readable_events ();
2675 /* Interface to read_avail_input, blocking SIGIO if necessary. */
2678 gobble_input (expected)
2679 int expected;
2681 #ifndef VMS
2682 #ifdef SIGIO
2683 if (interrupt_input)
2685 SIGMASKTYPE mask;
2686 mask = sigblockx (SIGIO);
2687 read_avail_input (expected);
2688 sigsetmask (mask);
2690 else
2691 #endif
2692 read_avail_input (expected);
2693 #endif
2696 #ifndef VMS
2698 /* Read any terminal input already buffered up by the system
2699 into the kbd_buffer, but do not wait.
2701 EXPECTED should be nonzero if the caller knows there is some input.
2703 Except on VMS, all input is read by this function.
2704 If interrupt_input is nonzero, this function MUST be called
2705 only when SIGIO is blocked.
2707 Returns the number of keyboard chars read, or -1 meaning
2708 this is a bad time to try to read input. */
2710 static int
2711 read_avail_input (expected)
2712 int expected;
2714 struct input_event buf[KBD_BUFFER_SIZE];
2715 register int i;
2716 int nread;
2718 if (read_socket_hook)
2719 /* No need for FIONREAD or fcntl; just say don't wait. */
2720 nread = (*read_socket_hook) (0, buf, KBD_BUFFER_SIZE, expected, expected);
2721 else
2723 unsigned char cbuf[KBD_BUFFER_SIZE];
2725 #ifdef FIONREAD
2726 /* Find out how much input is available. */
2727 if (ioctl (0, FIONREAD, &nread) < 0)
2728 /* Formerly simply reported no input, but that sometimes led to
2729 a failure of Emacs to terminate.
2730 SIGHUP seems appropriate if we can't reach the terminal. */
2731 /* ??? Is it really right to send the signal just to this process
2732 rather than to the whole process group?
2733 Perhaps on systems with FIONREAD Emacs is alone in its group. */
2734 kill (getpid (), SIGHUP);
2735 if (nread == 0)
2736 return 0;
2737 if (nread > sizeof cbuf)
2738 nread = sizeof cbuf;
2739 #else /* no FIONREAD */
2740 #ifdef USG
2741 /* Read some input if available, but don't wait. */
2742 nread = sizeof cbuf;
2743 fcntl (fileno (stdin), F_SETFL, O_NDELAY);
2744 #else
2745 you lose;
2746 #endif
2747 #endif
2749 /* Now read; for one reason or another, this will not block. */
2750 while (1)
2752 nread = read (fileno (stdin), cbuf, nread);
2753 #ifdef AIX
2754 /* The kernel sometimes fails to deliver SIGHUP for ptys.
2755 This looks incorrect, but it isn't, because _BSD causes
2756 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
2757 and that causes a value other than 0 when there is no input. */
2758 if (nread == 0)
2759 kill (SIGHUP, 0);
2760 #endif
2761 /* Retry the read if it is interrupted. */
2762 if (nread >= 0
2763 || ! (errno == EAGAIN || errno == EFAULT
2764 #ifdef EBADSLT
2765 || errno == EBADSLT
2766 #endif
2768 break;
2771 #ifndef FIONREAD
2772 #ifdef USG
2773 fcntl (fileno (stdin), F_SETFL, 0);
2774 #endif /* USG */
2775 #endif /* no FIONREAD */
2776 for (i = 0; i < nread; i++)
2778 buf[i].kind = ascii_keystroke;
2779 buf[i].modifiers = 0;
2780 if (meta_key && (cbuf[i] & 0x80))
2781 buf[i].modifiers = meta_modifier;
2782 cbuf[i] &= ~0x80;
2784 XSET (buf[i].code, Lisp_Int, cbuf[i]);
2785 #ifdef MULTI_FRAME
2786 XSET (buf[i].frame_or_window, Lisp_Frame, selected_frame);
2787 #else
2788 buf[i].frame_or_window = Qnil;
2789 #endif
2793 /* Scan the chars for C-g and store them in kbd_buffer. */
2794 for (i = 0; i < nread; i++)
2796 kbd_buffer_store_event (&buf[i]);
2797 /* Don't look at input that follows a C-g too closely.
2798 This reduces lossage due to autorepeat on C-g. */
2799 if (buf[i].kind == ascii_keystroke
2800 && XINT(buf[i].code) == quit_char)
2801 break;
2804 return nread;
2806 #endif /* not VMS */
2808 #ifdef SIGIO /* for entire page */
2809 /* Note SIGIO has been undef'd if FIONREAD is missing. */
2811 SIGTYPE
2812 input_available_signal (signo)
2813 int signo;
2815 /* Must preserve main program's value of errno. */
2816 int old_errno = errno;
2817 #ifdef BSD4_1
2818 extern int select_alarmed;
2819 #endif
2821 #ifdef USG
2822 /* USG systems forget handlers when they are used;
2823 must reestablish each time */
2824 signal (signo, input_available_signal);
2825 #endif /* USG */
2827 #ifdef BSD4_1
2828 sigisheld (SIGIO);
2829 #endif
2831 if (input_available_clear_time)
2832 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
2834 while (1)
2836 int nread;
2837 nread = read_avail_input (1);
2838 /* -1 means it's not ok to read the input now.
2839 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
2840 0 means there was no keyboard input available. */
2841 if (nread <= 0)
2842 break;
2844 #ifdef BSD4_1
2845 select_alarmed = 1; /* Force the select emulator back to life */
2846 #endif
2849 #ifdef BSD4_1
2850 sigfree ();
2851 #endif
2852 errno = old_errno;
2854 #endif /* SIGIO */
2856 /* Return the prompt-string of a sparse keymap.
2857 This is the first element which is a string.
2858 Return nil if there is none. */
2860 Lisp_Object
2861 map_prompt (map)
2862 Lisp_Object map;
2864 while (CONSP (map))
2866 register Lisp_Object tem;
2867 tem = Fcar (map);
2868 if (XTYPE (tem) == Lisp_String)
2869 return tem;
2870 map = Fcdr (map);
2872 return Qnil;
2875 static Lisp_Object menu_bar_item ();
2876 static Lisp_Object menu_bar_one_keymap ();
2878 /* Return a list of menu items for a menu bar, appropriate
2879 to the current buffer.
2880 The elements have the form (KEY STRING . nil). */
2882 Lisp_Object
2883 menu_bar_items ()
2885 /* The number of keymaps we're scanning right now, and the number of
2886 keymaps we have allocated space for. */
2887 int nmaps;
2889 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
2890 in the current keymaps, or nil where it is not a prefix. */
2891 Lisp_Object *maps;
2893 Lisp_Object def, tem;
2895 Lisp_Object result;
2897 int mapno;
2899 /* Build our list of keymaps.
2900 If we recognize a function key and replace its escape sequence in
2901 keybuf with its symbol, or if the sequence starts with a mouse
2902 click and we need to switch buffers, we jump back here to rebuild
2903 the initial keymaps from the current buffer. */
2905 Lisp_Object *tmaps;
2907 nmaps = current_minor_maps (0, &tmaps) + 2;
2908 maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
2909 bcopy (tmaps, maps, (nmaps - 2) * sizeof (maps[0]));
2910 #ifdef USE_TEXT_PROPERTIES
2911 maps[nmaps-2] = get_local_map (PT, current_buffer);
2912 #else
2913 maps[nmaps-2] = current_buffer->local_map;
2914 #endif
2915 maps[nmaps-1] = global_map;
2918 /* Look up in each map the dummy prefix key `menu-bar'. */
2920 result = Qnil;
2922 for (mapno = 0; mapno < nmaps; mapno++)
2924 if (! NILP (maps[mapno]))
2925 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1));
2926 else
2927 def = Qnil;
2929 tem = Fkeymapp (def);
2930 if (!NILP (tem))
2931 result = menu_bar_one_keymap (def, result);
2934 return Fnreverse (result);
2937 /* Scan one map KEYMAP, accumulating any menu items it defines
2938 that have not yet been seen in RESULT. Return the updated RESULT. */
2940 static Lisp_Object
2941 menu_bar_one_keymap (keymap, result)
2942 Lisp_Object keymap, result;
2944 Lisp_Object tail, item, key, binding, item_string, table;
2946 /* Loop over all keymap entries that have menu strings. */
2947 for (tail = keymap; XTYPE (tail) == Lisp_Cons; tail = XCONS (tail)->cdr)
2949 item = XCONS (tail)->car;
2950 if (XTYPE (item) == Lisp_Cons)
2952 key = XCONS (item)->car;
2953 binding = XCONS (item)->cdr;
2954 if (XTYPE (binding) == Lisp_Cons)
2956 item_string = XCONS (binding)->car;
2957 if (XTYPE (item_string) == Lisp_String)
2958 result = menu_bar_item (key, item_string,
2959 Fcdr (binding), result);
2962 else if (XTYPE (item) == Lisp_Vector)
2964 /* Loop over the char values represented in the vector. */
2965 int len = XVECTOR (item)->size;
2966 int c;
2967 for (c = 0; c < len; c++)
2969 Lisp_Object character;
2970 XFASTINT (character) = c;
2971 binding = XVECTOR (item)->contents[c];
2972 if (XTYPE (binding) == Lisp_Cons)
2974 item_string = XCONS (binding)->car;
2975 if (XTYPE (item_string) == Lisp_String)
2976 result = menu_bar_item (key, item_string,
2977 Fcdr (binding), result);
2983 return result;
2986 static Lisp_Object
2987 menu_bar_item (key, item_string, def, result)
2988 Lisp_Object key, item_string, def, result;
2990 Lisp_Object tem, elt;
2991 Lisp_Object enabled;
2993 /* See if this entry is enabled. */
2994 enabled = Qt;
2996 if (XTYPE (def) == Lisp_Symbol)
2998 /* No property, or nil, means enable.
2999 Otherwise, enable if value is not nil. */
3000 tem = Fget (def, Qmenu_enable);
3001 if (!NILP (tem))
3002 enabled = Feval (tem);
3005 /* Add an entry for this key and string
3006 if there is none yet. */
3007 elt = Fassq (key, result);
3008 if (!NILP (enabled) && NILP (elt))
3009 result = Fcons (Fcons (key, Fcons (item_string, Qnil)), result);
3011 return result;
3014 static int echo_flag;
3015 static int echo_now;
3017 /* Read a character like read_char but optionally prompt based on maps
3018 in the array MAPS. NMAPS is the length of MAPS. Return nil if we
3019 decided not to read a character, because there are no menu items in
3020 MAPS.
3022 PREV_EVENT is the previous input event, or nil if we are reading
3023 the first event of a key sequence.
3025 If USED_MOUSE_MENU is non-zero, then we set *USED_MOUSE_MENU to 1
3026 if we used a mouse menu to read the input, or zero otherwise. If
3027 USED_MOUSE_MENU is zero, *USED_MOUSE_MENU is left alone.
3029 The prompting is done based on the prompt-string of the map
3030 and the strings associated with various map elements. */
3032 Lisp_Object
3033 read_char_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
3034 int nmaps;
3035 Lisp_Object *maps;
3036 Lisp_Object prev_event;
3037 int *used_mouse_menu;
3039 int mapno;
3040 register Lisp_Object name;
3041 int nlength;
3042 int width = FRAME_WIDTH (selected_frame) - 4;
3043 char *menu = (char *) alloca (width + 4);
3044 int idx = -1;
3045 Lisp_Object rest, vector;
3047 if (used_mouse_menu)
3048 *used_mouse_menu = 0;
3050 /* Use local over global Menu maps */
3052 if (! menu_prompting)
3053 return Qnil;
3055 /* Get the menu name from the first map that has one (a prompt string). */
3056 for (mapno = 0; mapno < nmaps; mapno++)
3058 name = map_prompt (maps[mapno]);
3059 if (!NILP (name))
3060 break;
3063 /* If we don't have any menus, just read a character normally. */
3064 if (mapno >= nmaps)
3065 return Qnil;
3067 #ifdef HAVE_X_WINDOWS
3068 #ifdef HAVE_X_MENU
3069 /* If we got to this point via a mouse click,
3070 use a real menu for mouse selection. */
3071 if (EVENT_HAS_PARAMETERS (prev_event))
3073 /* Display the menu and get the selection. */
3074 Lisp_Object *realmaps
3075 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
3076 Lisp_Object value;
3077 int nmaps1 = 0;
3079 /* Use the maps that are not nil. */
3080 for (mapno = 0; mapno < nmaps; mapno++)
3081 if (!NILP (maps[mapno]))
3082 realmaps[nmaps1++] = maps[mapno];
3084 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
3085 if (NILP (value))
3086 XSET (value, Lisp_Int, quit_char);
3087 if (used_mouse_menu)
3088 *used_mouse_menu = 1;
3089 return value;
3091 #endif /* HAVE_X_MENU */
3092 #endif /* HAVE_X_WINDOWS */
3094 /* Prompt string always starts with map's prompt, and a space. */
3095 strcpy (menu, XSTRING (name)->data);
3096 nlength = XSTRING (name)->size;
3097 menu[nlength++] = ':';
3098 menu[nlength++] = ' ';
3099 menu[nlength] = 0;
3101 /* Start prompting at start of first map. */
3102 mapno = 0;
3103 rest = maps[mapno];
3105 /* Present the documented bindings, a line at a time. */
3106 while (1)
3108 int notfirst = 0;
3109 int i = nlength;
3110 Lisp_Object obj;
3111 int ch;
3113 /* Loop over elements of map. */
3114 while (i < width)
3116 Lisp_Object s, elt;
3118 /* If reached end of map, start at beginning of next map. */
3119 if (NILP (rest))
3121 mapno++;
3122 /* At end of last map, wrap around to first map if just starting,
3123 or end this line if already have something on it. */
3124 if (mapno == nmaps)
3126 if (notfirst)
3127 break;
3128 else
3129 mapno = 0;
3131 rest = maps[mapno];
3134 /* Look at the next element of the map. */
3135 if (idx >= 0)
3136 elt = XVECTOR (vector)->contents[idx];
3137 else
3138 elt = Fcar_safe (rest);
3140 if (idx < 0 && XTYPE (elt) == Lisp_Vector)
3142 /* If we found a dense table in the keymap,
3143 advanced past it, but start scanning its contents. */
3144 rest = Fcdr_safe (rest);
3145 vector = elt;
3146 idx = 0;
3148 else
3150 /* An ordinary element. */
3151 s = Fcar_safe (Fcdr_safe (elt));
3152 if (XTYPE (s) != Lisp_String)
3153 /* Ignore the element if it has no prompt string. */
3155 /* If we have room for the prompt string, add it to this line.
3156 If this is the first on the line, always add it. */
3157 else if (XSTRING (s)->size + i < width
3158 || !notfirst)
3160 int thiswidth;
3162 /* Punctuate between strings. */
3163 if (notfirst)
3165 strcpy (menu + i, ", ");
3166 i += 2;
3168 notfirst = 1;
3170 /* Add as much of string as fits. */
3171 thiswidth = XSTRING (s)->size;
3172 if (thiswidth + i > width)
3173 thiswidth = width - i;
3174 bcopy (XSTRING (s)->data, menu + i, thiswidth);
3175 i += thiswidth;
3177 else
3179 /* If this element does not fit, end the line now,
3180 and save the element for the next line. */
3181 strcpy (menu + i, "...");
3182 break;
3185 /* Move past this element. */
3186 if (idx >= 0 && idx + 1 >= XVECTOR (rest)->size)
3187 /* Handle reaching end of dense table. */
3188 idx = -1;
3189 if (idx >= 0)
3190 idx++;
3191 else
3192 rest = Fcdr_safe (rest);
3196 /* Prompt with that and read response. */
3197 message1 (menu);
3198 obj = read_char (1, 0, 0, Qnil, 0);
3200 if (XTYPE (obj) != Lisp_Int)
3201 return obj;
3202 else
3203 ch = XINT (obj);
3205 if (! EQ (obj, menu_prompt_more_char)
3206 && (XTYPE (menu_prompt_more_char) != Lisp_Int
3207 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
3208 return obj;
3212 /* Reading key sequences. */
3214 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
3215 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
3216 keymap, or nil otherwise. Return the index of the first keymap in
3217 which KEY has any binding, or NMAPS if no map has a binding.
3219 If KEY is a meta ASCII character, treat it like meta-prefix-char
3220 followed by the corresponding non-meta character. Keymaps in
3221 CURRENT with non-prefix bindings for meta-prefix-char become nil in
3222 NEXT.
3224 When KEY is not defined in any of the keymaps, if it is an upper
3225 case letter and there are bindings for the corresponding lower-case
3226 letter, return the bindings for the lower-case letter.
3228 If KEY has no bindings in any of the CURRENT maps, NEXT is left
3229 unmodified.
3231 NEXT may == CURRENT. */
3233 static int
3234 follow_key (key, nmaps, current, defs, next)
3235 Lisp_Object key;
3236 Lisp_Object *current, *defs, *next;
3237 int nmaps;
3239 int i, first_binding;
3241 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
3242 followed by the corresponding non-meta character. */
3243 if (XTYPE (key) == Lisp_Int && (XINT (key) & CHAR_META))
3245 for (i = 0; i < nmaps; i++)
3246 if (! NILP (current[i]))
3248 next[i] =
3249 get_keyelt (access_keymap (current[i], meta_prefix_char, 1));
3251 /* Note that since we pass the resulting bindings through
3252 get_keymap_1, non-prefix bindings for meta-prefix-char
3253 disappear. */
3254 next[i] = get_keymap_1 (next[i], 0, 1);
3256 else
3257 next[i] = Qnil;
3259 current = next;
3260 XSET (key, Lisp_Int, XFASTINT (key) & ~CHAR_META);
3263 first_binding = nmaps;
3264 for (i = nmaps - 1; i >= 0; i--)
3266 if (! NILP (current[i]))
3268 defs[i] = get_keyelt (access_keymap (current[i], key, 1));
3269 if (! NILP (defs[i]))
3270 first_binding = i;
3272 else
3273 defs[i] = Qnil;
3276 /* When KEY is not defined in any of the keymaps, if it is an upper
3277 case letter and there are bindings for the corresponding
3278 lower-case letter, return the bindings for the lower-case letter. */
3279 if (first_binding == nmaps
3280 && XTYPE (key) == Lisp_Int
3281 && (UPPERCASEP (XINT (key) & 0x3ffff)
3282 || (XINT (key) & shift_modifier)))
3284 if (XINT (key) & shift_modifier)
3285 XSETINT (key, XINT (key) & ~shift_modifier);
3286 else
3287 XSETINT (key, DOWNCASE (XINT (key)));
3289 first_binding = nmaps;
3290 for (i = nmaps - 1; i >= 0; i--)
3292 if (! NILP (current[i]))
3294 defs[i] = get_keyelt (access_keymap (current[i], key, 1));
3295 if (! NILP (defs[i]))
3296 first_binding = i;
3298 else
3299 defs[i] = Qnil;
3303 /* Given the set of bindings we've found, produce the next set of maps. */
3304 if (first_binding < nmaps)
3305 for (i = 0; i < nmaps; i++)
3306 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
3308 return first_binding;
3311 /* Read a sequence of keys that ends with a non prefix character,
3312 storing it in KEYBUF, a buffer of size BUFSIZE.
3313 Prompt with PROMPT.
3314 Return the length of the key sequence stored.
3316 Echo starting immediately unless `prompt' is 0.
3318 Where a key sequence ends depends on the currently active keymaps.
3319 These include any minor mode keymaps active in the current buffer,
3320 the current buffer's local map, and the global map.
3322 If a key sequence has no other bindings, we check Vfunction_key_map
3323 to see if some trailing subsequence might be the beginning of a
3324 function key's sequence. If so, we try to read the whole function
3325 key, and substitute its symbolic name into the key sequence.
3327 We ignore unbound `down-' mouse clicks. We turn unbound `drag-'
3328 events into similar click events, if that would make them bound.
3330 If we get a mouse click in a mode line, vertical divider, or other
3331 non-text area, we treat the click as if it were prefixed by the
3332 symbol denoting that area - `mode-line', `vertical-line', or
3333 whatever.
3335 If the sequence starts with a mouse click, we read the key sequence
3336 with respect to the buffer clicked on, not the current buffer.
3338 If the user switches frames in the midst of a key sequence, we put
3339 off the switch-frame event until later; the next call to
3340 read_char will return it. */
3342 static int
3343 read_key_sequence (keybuf, bufsize, prompt)
3344 Lisp_Object *keybuf;
3345 int bufsize;
3346 char *prompt;
3348 int count = specpdl_ptr - specpdl;
3350 /* How many keys there are in the current key sequence. */
3351 int t;
3353 /* The length of the echo buffer when we started reading, and
3354 the length of this_command_keys when we started reading. */
3355 int echo_start;
3356 int keys_start;
3358 /* The number of keymaps we're scanning right now, and the number of
3359 keymaps we have allocated space for. */
3360 int nmaps;
3361 int nmaps_allocated = 0;
3363 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
3364 the current keymaps. */
3365 Lisp_Object *defs;
3367 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
3368 in the current keymaps, or nil where it is not a prefix. */
3369 Lisp_Object *submaps;
3371 /* The index in defs[] of the first keymap that has a binding for
3372 this key sequence. In other words, the lowest i such that
3373 defs[i] is non-nil. */
3374 int first_binding;
3376 /* If t < mock_input, then KEYBUF[t] should be read as the next
3377 input key.
3379 We use this to recover after recognizing a function key. Once we
3380 realize that a suffix of the current key sequence is actually a
3381 function key's escape sequence, we replace the suffix with the
3382 function key's binding from Vfunction_key_map. Now keybuf
3383 contains a new and different key sequence, so the echo area,
3384 this_command_keys, and the submaps and defs arrays are wrong. In
3385 this situation, we set mock_input to t, set t to 0, and jump to
3386 restart_sequence; the loop will read keys from keybuf up until
3387 mock_input, thus rebuilding the state; and then it will resume
3388 reading characters from the keyboard. */
3389 int mock_input = 0;
3391 /* If the sequence is unbound in submaps[], then
3392 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
3393 and fkey_map is its binding.
3395 These might be > t, indicating that all function key scanning
3396 should hold off until t reaches them. We do this when we've just
3397 recognized a function key, to avoid searching for the function
3398 key's again in Vfunction_key_map. */
3399 int fkey_start = 0, fkey_end = 0;
3400 Lisp_Object fkey_map = Vfunction_key_map;
3402 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
3403 we put it off for later. While we're reading, we keep the event here. */
3404 Lisp_Object delayed_switch_frame = Qnil;
3407 /* If there is no function key map, turn off function key scanning. */
3408 if (NILP (Fkeymapp (Vfunction_key_map)))
3409 fkey_start = fkey_end = bufsize + 1;
3411 /* We need to save the current buffer in case we switch buffers to
3412 find the right binding for a mouse click. Note that we can't use
3413 save_excursion_{save,restore} here, because they save point as
3414 well as the current buffer; we don't want to save point, because
3415 redisplay may change it, to accomodate a Fset_window_start or
3416 something. */
3417 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3419 last_nonmenu_event = Qnil;
3421 if (INTERACTIVE)
3423 if (prompt)
3424 echo_prompt (prompt);
3425 else if (cursor_in_echo_area)
3426 /* This doesn't put in a dash if the echo buffer is empty, so
3427 you don't always see a dash hanging out in the minibuffer. */
3428 echo_dash ();
3431 /* Record the initial state of the echo area and this_command_keys;
3432 we will need to restore them if we replay a key sequence. */
3433 if (INTERACTIVE)
3434 echo_start = echo_length ();
3435 keys_start = this_command_key_count;
3437 /* We jump here when the key sequence has been thoroughly changed, and
3438 we need to rescan it starting from the beginning. When we jump here,
3439 keybuf[0..mock_input] holds the sequence we should reread. */
3440 replay_sequence:
3442 /* Build our list of keymaps.
3443 If we recognize a function key and replace its escape sequence in
3444 keybuf with its symbol, or if the sequence starts with a mouse
3445 click and we need to switch buffers, we jump back here to rebuild
3446 the initial keymaps from the current buffer. */
3448 Lisp_Object *maps;
3450 nmaps = current_minor_maps (0, &maps) + 2;
3451 if (nmaps > nmaps_allocated)
3453 submaps = (Lisp_Object *) alloca (nmaps * sizeof (submaps[0]));
3454 defs = (Lisp_Object *) alloca (nmaps * sizeof (defs[0]));
3455 nmaps_allocated = nmaps;
3457 bcopy (maps, submaps, (nmaps - 2) * sizeof (submaps[0]));
3458 #ifdef USE_TEXT_PROPERTIES
3459 submaps[nmaps-2] = get_local_map (PT, current_buffer);
3460 #else
3461 submaps[nmaps-2] = current_buffer->local_map;
3462 #endif
3463 submaps[nmaps-1] = global_map;
3466 /* Find an accurate initial value for first_binding. */
3467 for (first_binding = 0; first_binding < nmaps; first_binding++)
3468 if (! NILP (submaps[first_binding]))
3469 break;
3471 /* We jump here when a function key substitution has forced us to
3472 reprocess the current key sequence. keybuf[0..mock_input] is the
3473 sequence we want to reread. */
3474 t = 0;
3476 /* These are no-ops the first time through, but if we restart, they
3477 revert the echo area and this_command_keys to their original state. */
3478 this_command_key_count = keys_start;
3479 if (INTERACTIVE)
3480 echo_truncate (echo_start);
3482 /* If the best binding for the current key sequence is a keymap,
3483 or we may be looking at a function key's escape sequence, keep
3484 on reading. */
3485 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
3486 || (first_binding >= nmaps && fkey_start < t))
3488 Lisp_Object key;
3489 int used_mouse_menu = 0;
3491 /* Where the last real key started. If we need to throw away a
3492 key that has expanded into more than one element of keybuf
3493 (say, a mouse click on the mode line which is being treated
3494 as [mode-line (mouse-...)], then we backtrack to this point
3495 of keybuf. */
3496 int last_real_key_start;
3498 /* These variables are analogous to echo_start and keys_start;
3499 while those allow us to restart the entire key sequence,
3500 echo_local_start and keys_local_start allow us to throw away
3501 just one key. */
3502 int echo_local_start, keys_local_start, local_first_binding;
3504 if (t >= bufsize)
3505 error ("key sequence too long");
3507 if (INTERACTIVE)
3508 echo_local_start = echo_length ();
3509 keys_local_start = this_command_key_count;
3510 local_first_binding = first_binding;
3512 replay_key:
3513 /* These are no-ops, unless we throw away a keystroke below and
3514 jumped back up to replay_key; in that case, these restore the
3515 variables to their original state, allowing us to replay the
3516 loop. */
3517 if (INTERACTIVE)
3518 echo_truncate (echo_local_start);
3519 this_command_key_count = keys_local_start;
3520 first_binding = local_first_binding;
3522 /* Does mock_input indicate that we are re-reading a key sequence? */
3523 if (t < mock_input)
3525 key = keybuf[t];
3526 add_command_key (key);
3527 echo_char (key);
3530 /* If not, we should actually read a character. */
3531 else
3533 last_real_key_start = t;
3535 key = read_char (!prompt, nmaps, submaps, last_nonmenu_event,
3536 &used_mouse_menu);
3538 /* read_char returns -1 at the end of a macro.
3539 Emacs 18 handles this by returning immediately with a
3540 zero, so that's what we'll do. */
3541 if (XTYPE (key) == Lisp_Int && XINT (key) == -1)
3543 t = 0;
3544 goto done;
3547 Vquit_flag = Qnil;
3549 /* Clicks in non-text areas get prefixed by the symbol
3550 in their CHAR-ADDRESS field. For example, a click on
3551 the mode line is prefixed by the symbol `mode-line'.
3553 Furthermore, key sequences beginning with mouse clicks
3554 are read using the keymaps of the buffer clicked on, not
3555 the current buffer. So we may have to switch the buffer
3556 here. */
3557 if (EVENT_HAS_PARAMETERS (key))
3559 Lisp_Object kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
3561 if (EQ (kind, Qmouse_click))
3563 Lisp_Object window = POSN_WINDOW (EVENT_START (key));
3564 Lisp_Object posn = POSN_BUFFER_POSN (EVENT_START (key));
3566 /* Key sequences beginning with mouse clicks are
3567 read using the keymaps in the buffer clicked on,
3568 not the current buffer. If we're at the
3569 beginning of a key sequence, switch buffers. */
3570 if (t == 0
3571 && XTYPE (window) == Lisp_Window
3572 && XTYPE (XWINDOW (window)->buffer) == Lisp_Buffer
3573 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
3575 if (XTYPE (posn) == Lisp_Symbol)
3577 if (t + 1 >= bufsize)
3578 error ("key sequence too long");
3579 keybuf[t] = posn;
3580 keybuf[t+1] = key;
3581 mock_input = t + 2;
3583 else
3585 keybuf[t] = key;
3586 mock_input = t + 1;
3589 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
3590 goto replay_sequence;
3592 else if (XTYPE (posn) == Lisp_Symbol)
3594 if (t + 1 >= bufsize)
3595 error ("key sequence too long");
3596 keybuf[t] = posn;
3597 keybuf[t+1] = key;
3598 mock_input = t + 2;
3600 goto replay_key;
3603 else if (EQ (kind, Qswitch_frame))
3605 /* If we're at the beginning of a key sequence, go
3606 ahead and return this event. If we're in the
3607 midst of a key sequence, delay it until the end. */
3608 if (t > 0)
3610 delayed_switch_frame = key;
3611 goto replay_key;
3614 else
3616 Lisp_Object posn = POSN_BUFFER_POSN (EVENT_START (key));
3618 /* Handle menu-bar events:
3619 insert the dummy prefix char `menu-bar'. */
3620 if (EQ (posn, Qmenu_bar))
3622 if (t + 1 >= bufsize)
3623 error ("key sequence too long");
3624 /* Run the Lucid hook. */
3625 call1 (Vrun_hooks, Qactivate_menubar_hook);
3626 /* If it has changed current-menubar from previous value,
3627 really recompute the menubar from the value. */
3628 if (! NILP (Vlucid_menu_bar_dirty_flag))
3629 call0 (Qrecompute_lucid_menubar);
3630 keybuf[t] = posn;
3631 keybuf[t+1] = key;
3632 mock_input = t + 2;
3633 goto replay_sequence;
3639 /* We have finally decided that KEY is something we might want
3640 to look up. */
3641 first_binding = (follow_key (key,
3642 nmaps - first_binding,
3643 submaps + first_binding,
3644 defs + first_binding,
3645 submaps + first_binding)
3646 + first_binding);
3648 /* If KEY wasn't bound, we'll try some fallbacks. */
3649 if (first_binding >= nmaps)
3651 Lisp_Object head = EVENT_HEAD (key);
3653 if (XTYPE (head) == Lisp_Symbol)
3655 Lisp_Object breakdown = parse_modifiers (head);
3656 Lisp_Object modifiers =
3657 XINT (XCONS (XCONS (breakdown)->cdr)->car);
3659 /* We drop unbound `down-' events altogether. */
3660 if (modifiers & down_modifier)
3662 /* Dispose of this event by simply jumping back to
3663 replay_key, to get another event.
3665 Note that if this event came from mock input,
3666 then just jumping back to replay_key will just
3667 hand it to us again. So we have to wipe out any
3668 mock input.
3670 We could delete keybuf[t] and shift everything
3671 after that to the left by one spot, but we'd also
3672 have to fix up any variable that points into
3673 keybuf, and shifting isn't really necessary
3674 anyway.
3676 Adding prefixes for non-textual mouse clicks
3677 creates two characters of mock input, and both
3678 must be thrown away. If we're only looking at
3679 the prefix now, we can just jump back to
3680 replay_key. On the other hand, if we've already
3681 processed the prefix, and now the actual click
3682 itself is giving us trouble, then we've lost the
3683 state of the keymaps we want to backtrack to, and
3684 we need to replay the whole sequence to rebuild
3687 Beyond that, only function key expansion could
3688 create more than two keys, but that should never
3689 generate mouse events, so it's okay to zero
3690 mock_input in that case too.
3692 Isn't this just the most wonderful code ever? */
3693 if (t == last_real_key_start)
3695 mock_input = 0;
3696 goto replay_key;
3698 else
3700 mock_input = last_real_key_start;
3701 goto replay_sequence;
3705 /* We turn unbound `drag-' events into `click-'
3706 events, if the click would be bound. */
3707 else if (modifiers & drag_modifier)
3709 Lisp_Object new_head =
3710 apply_modifiers (modifiers & ~drag_modifier,
3711 XCONS (breakdown)->car);
3712 Lisp_Object new_click =
3713 Fcons (new_head, Fcons (EVENT_START (key), Qnil));
3715 /* Look for a binding for this new key. follow_key
3716 promises that it didn't munge submaps the
3717 last time we called it, since key was unbound. */
3718 first_binding =
3719 (follow_key (new_click,
3720 nmaps - local_first_binding,
3721 submaps + local_first_binding,
3722 defs + local_first_binding,
3723 submaps + local_first_binding)
3724 + local_first_binding);
3726 /* If that click is bound, go for it. */
3727 if (first_binding < nmaps)
3728 key = new_click;
3729 /* Otherwise, we'll leave key set to the drag event. */
3734 keybuf[t++] = key;
3735 /* Normally, last_nonmenu_event gets the previous key we read.
3736 But when a mouse popup menu is being used,
3737 we don't update last_nonmenu_event; it continues to hold the mouse
3738 event that preceded the first level of menu. */
3739 if (!used_mouse_menu)
3740 last_nonmenu_event = key;
3742 /* If the sequence is unbound, see if we can hang a function key
3743 off the end of it. We only want to scan real keyboard input
3744 for function key sequences, so if mock_input says that we're
3745 re-reading old events, don't examine it. */
3746 if (first_binding >= nmaps
3747 && t >= mock_input)
3749 Lisp_Object fkey_next;
3751 /* Scan from fkey_end until we find a bound suffix. */
3752 while (fkey_end < t)
3754 Lisp_Object key;
3756 key = keybuf[fkey_end++];
3757 /* Look up meta-characters by prefixing them
3758 with meta_prefix_char. I hate this. */
3759 if (XTYPE (key) == Lisp_Int && XINT (key) & meta_modifier)
3761 fkey_next =
3762 get_keymap_1
3763 (get_keyelt
3764 (access_keymap
3765 (fkey_map, meta_prefix_char, 1)),
3766 0, 1);
3767 XFASTINT (key) = XFASTINT (key) & ~meta_modifier;
3769 else
3770 fkey_next = fkey_map;
3772 fkey_next =
3773 get_keyelt (access_keymap (fkey_next, key, 1));
3775 /* If keybuf[fkey_start..fkey_end] is bound in the
3776 function key map and it's a suffix of the current
3777 sequence (i.e. fkey_end == t), replace it with
3778 the binding and restart with fkey_start at the end. */
3779 if (XTYPE (fkey_next) == Lisp_Vector
3780 && fkey_end == t)
3782 t = fkey_start + XVECTOR (fkey_next)->size;
3783 if (t >= bufsize)
3784 error ("key sequence too long");
3786 bcopy (XVECTOR (fkey_next)->contents,
3787 keybuf + fkey_start,
3788 (t - fkey_start) * sizeof (keybuf[0]));
3790 mock_input = t;
3791 fkey_start = fkey_end = t;
3793 goto replay_sequence;
3796 fkey_map = get_keymap_1 (fkey_next, 0, 1);
3798 /* If we no longer have a bound suffix, try a new positions for
3799 fkey_start. */
3800 if (NILP (fkey_map))
3802 fkey_end = ++fkey_start;
3803 fkey_map = Vfunction_key_map;
3809 read_key_sequence_cmd = (first_binding < nmaps
3810 ? defs[first_binding]
3811 : Qnil);
3813 done:
3814 unread_switch_frame = delayed_switch_frame;
3815 unbind_to (count, Qnil);
3816 return t;
3819 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 2, 0,
3820 "Read a sequence of keystrokes and return as a string or vector.\n\
3821 The sequence is sufficient to specify a non-prefix command in the\n\
3822 current local and global maps.\n\
3824 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
3825 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
3826 as a continuation of the previous key.\n\
3828 A C-g typed while in this function is treated like any other character,\n\
3829 and `quit-flag' is not set.\n\
3831 If the key sequence starts with a mouse click, then the sequence is read\n\
3832 using the keymaps of the buffer of the window clicked in, not the buffer\n\
3833 of the selected window as normal.\n\
3835 `read-key-sequence' drops unbound button-down events, since you normally\n\
3836 only care about the click or drag events which follow them. If a drag\n\
3837 event is unbound, but the corresponding click event would be bound,\n\
3838 `read-key-sequence' turns the drag event into a click event at the\n\
3839 drag's starting position. This means that you don't have to distinguish\n\
3840 between click and drag events unless you want to.\n\
3842 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
3843 lines separating windows, and scroll bars with imaginary keys\n\
3844 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
3846 If the user switches frames in the middle of a key sequence, the\n\
3847 frame-switch event is put off until after the current key sequence.\n\
3849 `read-key-sequence' checks `function-key-map' for function key\n\
3850 sequences, where they wouldn't conflict with ordinary bindings. See\n\
3851 `function-key-map' for more details.")
3852 (prompt, continue_echo)
3853 Lisp_Object prompt, continue_echo;
3855 Lisp_Object keybuf[30];
3856 register int i;
3857 struct gcpro gcpro1, gcpro2;
3859 if (!NILP (prompt))
3860 CHECK_STRING (prompt, 0);
3861 QUIT;
3863 bzero (keybuf, sizeof keybuf);
3864 GCPRO1 (keybuf[0]);
3865 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
3867 if (NILP (continue_echo))
3868 this_command_key_count = 0;
3870 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
3871 NILP (prompt) ? 0 : XSTRING (prompt)->data);
3873 UNGCPRO;
3874 return make_event_array (i, keybuf);
3877 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
3878 "Execute CMD as an editor command.\n\
3879 CMD must be a symbol that satisfies the `commandp' predicate.\n\
3880 Optional second arg RECORD-FLAG non-nil\n\
3881 means unconditionally put this command in `command-history'.\n\
3882 Otherwise, that is done only if an arg is read using the minibuffer.")
3883 (cmd, record)
3884 Lisp_Object cmd, record;
3886 register Lisp_Object final;
3887 register Lisp_Object tem;
3888 Lisp_Object prefixarg;
3889 struct backtrace backtrace;
3890 extern int debug_on_next_call;
3892 prefixarg = Vprefix_arg, Vprefix_arg = Qnil;
3893 Vcurrent_prefix_arg = prefixarg;
3894 debug_on_next_call = 0;
3896 if (XTYPE (cmd) == Lisp_Symbol)
3898 tem = Fget (cmd, Qdisabled);
3899 if (!NILP (tem))
3900 return call1 (Vrun_hooks, Vdisabled_command_hook);
3903 while (1)
3905 final = Findirect_function (cmd);
3907 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
3908 do_autoload (final, cmd);
3909 else
3910 break;
3913 if (XTYPE (final) == Lisp_String
3914 || XTYPE (final) == Lisp_Vector)
3916 /* If requested, place the macro in the command history. For
3917 other sorts of commands, call-interactively takes care of
3918 this. */
3919 if (!NILP (record))
3920 Vcommand_history
3921 = Fcons (Fcons (Qexecute_kbd_macro,
3922 Fcons (final, Fcons (prefixarg, Qnil))),
3923 Vcommand_history);
3925 return Fexecute_kbd_macro (final, prefixarg);
3927 if (CONSP (final) || XTYPE (final) == Lisp_Subr
3928 || XTYPE (final) == Lisp_Compiled)
3930 backtrace.next = backtrace_list;
3931 backtrace_list = &backtrace;
3932 backtrace.function = &Qcall_interactively;
3933 backtrace.args = &cmd;
3934 backtrace.nargs = 1;
3935 backtrace.evalargs = 0;
3937 tem = Fcall_interactively (cmd, record);
3939 backtrace_list = backtrace.next;
3940 return tem;
3942 return Qnil;
3945 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
3946 1, 1, "P",
3947 "Read function name, then read its arguments and call it.")
3948 (prefixarg)
3949 Lisp_Object prefixarg;
3951 Lisp_Object function;
3952 char buf[40];
3953 Lisp_Object saved_keys;
3954 struct gcpro gcpro1;
3956 saved_keys = Fthis_command_keys ();
3957 buf[0] = 0;
3958 GCPRO1 (saved_keys);
3960 if (EQ (prefixarg, Qminus))
3961 strcpy (buf, "- ");
3962 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
3963 strcpy (buf, "C-u ");
3964 else if (CONSP (prefixarg) && XTYPE (XCONS (prefixarg)->car) == Lisp_Int)
3965 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
3966 else if (XTYPE (prefixarg) == Lisp_Int)
3967 sprintf (buf, "%d ", XINT (prefixarg));
3969 /* This isn't strictly correct if execute-extended-command
3970 is bound to anything else. Perhaps it should use
3971 this_command_keys? */
3972 strcat (buf, "M-x ");
3974 /* Prompt with buf, and then read a string, completing from and
3975 restricting to the set of all defined commands. Don't provide
3976 any initial input. The last Qnil says not to perform a
3977 peculiar hack on the initial input. */
3978 function = Fcompleting_read (build_string (buf),
3979 Vobarray, Qcommandp,
3980 Qt, Qnil, Qnil);
3982 /* Set this_command_keys to the concatenation of saved_keys and
3983 function, followed by a RET. */
3985 struct Lisp_String *str;
3986 int i;
3987 Lisp_Object tem;
3989 this_command_key_count = 0;
3991 str = XSTRING (saved_keys);
3992 for (i = 0; i < str->size; i++)
3994 XFASTINT (tem) = str->data[i];
3995 add_command_key (tem);
3998 str = XSTRING (function);
3999 for (i = 0; i < str->size; i++)
4001 XFASTINT (tem) = str->data[i];
4002 add_command_key (tem);
4005 XFASTINT (tem) = '\015';
4006 add_command_key (tem);
4009 UNGCPRO;
4011 function = Fintern (function, Qnil);
4012 Vprefix_arg = prefixarg;
4013 this_command = function;
4015 return Fcommand_execute (function, Qt);
4019 detect_input_pending ()
4021 if (!input_pending)
4022 get_input_pending (&input_pending);
4024 return input_pending;
4027 /* This is called in some cases before a possible quit.
4028 It cases the next call to detect_input_pending to recompute input_pending.
4029 So calling this function unnecessarily can't do any harm. */
4030 clear_input_pending ()
4032 input_pending = 0;
4035 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
4036 "T if command input is currently available with no waiting.\n\
4037 Actually, the value is nil only if we can be sure that no input is available.")
4040 if (!NILP (unread_command_events) || unread_command_char != -1)
4041 return (Qt);
4043 return detect_input_pending () ? Qt : Qnil;
4046 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
4047 "Return vector of last 100 events, not counting those from keyboard macros.")
4050 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
4051 Lisp_Object val;
4053 if (total_keys < NUM_RECENT_KEYS)
4054 return Fvector (total_keys, keys);
4055 else
4057 val = Fvector (NUM_RECENT_KEYS, keys);
4058 bcopy (keys + recent_keys_index,
4059 XVECTOR (val)->contents,
4060 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
4061 bcopy (keys,
4062 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
4063 recent_keys_index * sizeof (Lisp_Object));
4064 return val;
4068 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
4069 "Return string of the keystrokes that invoked this command.")
4072 return make_event_array (this_command_key_count,
4073 XVECTOR (this_command_keys)->contents);
4076 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
4077 "Return the current depth in recursive edits.")
4080 Lisp_Object temp;
4081 XFASTINT (temp) = command_loop_level + minibuf_level;
4082 return temp;
4085 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
4086 "FOpen dribble file: ",
4087 "Start writing all keyboard characters to a dribble file called FILE.\n\
4088 If FILE is nil, close any open dribble file.")
4089 (file)
4090 Lisp_Object file;
4092 if (NILP (file))
4094 fclose (dribble);
4095 dribble = 0;
4097 else
4099 file = Fexpand_file_name (file, Qnil);
4100 dribble = fopen (XSTRING (file)->data, "w");
4102 return Qnil;
4105 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
4106 "Discard the contents of the terminal input buffer.\n\
4107 Also cancel any kbd macro being defined.")
4110 defining_kbd_macro = 0;
4111 update_mode_lines++;
4113 unread_command_events = Qnil;
4114 unread_command_char = -1;
4116 discard_tty_input ();
4118 /* Without the cast, GCC complains that this assignment loses the
4119 volatile qualifier of kbd_store_ptr. Is there anything wrong
4120 with that? */
4121 kbd_fetch_ptr = (struct input_event *) kbd_store_ptr;
4122 Ffillarray (kbd_buffer_frame_or_window, Qnil);
4123 input_pending = 0;
4125 return Qnil;
4128 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
4129 "Stop Emacs and return to superior process. You can resume later.\n\
4130 On systems that don't have job control, run a subshell instead.\n\n\
4131 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
4132 to be read as terminal input by Emacs's parent, after suspension.\n\
4134 Before suspending, call the functions in `suspend-hooks' with no args.\n\
4135 If any of them returns nil, don't call the rest and don't suspend.\n\
4136 Otherwise, suspend normally and after resumption run the normal hook\n\
4137 `suspend-resume-hook' if that is bound and non-nil.\n\
4139 Some operating systems cannot stop the Emacs process and resume it later.\n\
4140 On such systems, Emacs starts a subshell instead of suspending.")
4141 (stuffstring)
4142 Lisp_Object stuffstring;
4144 Lisp_Object tem;
4145 int count = specpdl_ptr - specpdl;
4146 int old_height, old_width;
4147 int width, height;
4148 struct gcpro gcpro1, gcpro2;
4149 extern init_sys_modes ();
4151 if (!NILP (stuffstring))
4152 CHECK_STRING (stuffstring, 0);
4154 /* Run the functions in suspend-hook. */
4155 if (!NILP (Vrun_hooks))
4156 call1 (Vrun_hooks, intern ("suspend-hook"));
4158 GCPRO1 (stuffstring);
4159 get_frame_size (&old_width, &old_height);
4160 reset_sys_modes ();
4161 /* sys_suspend can get an error if it tries to fork a subshell
4162 and the system resources aren't available for that. */
4163 record_unwind_protect (init_sys_modes, 0);
4164 stuff_buffered_input (stuffstring);
4165 sys_suspend ();
4166 unbind_to (count, Qnil);
4168 /* Check if terminal/window size has changed.
4169 Note that this is not useful when we are running directly
4170 with a window system; but suspend should be disabled in that case. */
4171 get_frame_size (&width, &height);
4172 if (width != old_width || height != old_height)
4173 change_frame_size (0, height, width, 0, 0);
4175 /* Run suspend-resume-hook. */
4176 if (!NILP (Vrun_hooks))
4177 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
4179 UNGCPRO;
4180 return Qnil;
4183 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
4184 Then in any case stuff anthing Emacs has read ahead and not used. */
4186 stuff_buffered_input (stuffstring)
4187 Lisp_Object stuffstring;
4189 register unsigned char *p;
4191 /* stuff_char works only in BSD, versions 4.2 and up. */
4192 #ifdef BSD
4193 #ifndef BSD4_1
4194 if (XTYPE (stuffstring) == Lisp_String)
4196 register int count;
4198 p = XSTRING (stuffstring)->data;
4199 count = XSTRING (stuffstring)->size;
4200 while (count-- > 0)
4201 stuff_char (*p++);
4202 stuff_char ('\n');
4204 /* Anything we have read ahead, put back for the shell to read. */
4205 while (kbd_fetch_ptr != kbd_store_ptr)
4207 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
4208 kbd_fetch_ptr = kbd_buffer;
4209 if (kbd_fetch_ptr->kind == ascii_keystroke)
4210 stuff_char (XINT (kbd_fetch_ptr->code));
4211 kbd_fetch_ptr->kind = no_event;
4212 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
4213 - kbd_buffer]
4214 = Qnil);
4215 kbd_fetch_ptr++;
4217 input_pending = 0;
4218 #endif
4219 #endif /* BSD and not BSD4_1 */
4222 set_waiting_for_input (time_to_clear)
4223 EMACS_TIME *time_to_clear;
4225 input_available_clear_time = time_to_clear;
4227 /* Tell interrupt_signal to throw back to read_char, */
4228 waiting_for_input = 1;
4230 /* If interrupt_signal was called before and buffered a C-g,
4231 make it run again now, to avoid timing error. */
4232 if (!NILP (Vquit_flag))
4233 quit_throw_to_read_char ();
4235 /* If alarm has gone off already, echo now. */
4236 if (echo_flag)
4238 echo ();
4239 echo_flag = 0;
4243 clear_waiting_for_input ()
4245 /* Tell interrupt_signal not to throw back to read_char, */
4246 waiting_for_input = 0;
4247 input_available_clear_time = 0;
4250 /* This routine is called at interrupt level in response to C-G.
4251 If interrupt_input, this is the handler for SIGINT.
4252 Otherwise, it is called from kbd_buffer_store_event,
4253 in handling SIGIO or SIGTINT.
4255 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
4256 immediately throw back to read_char.
4258 Otherwise it sets the Lisp variable quit-flag not-nil.
4259 This causes eval to throw, when it gets a chance.
4260 If quit-flag is already non-nil, it stops the job right away. */
4262 SIGTYPE
4263 interrupt_signal ()
4265 char c;
4266 /* Must preserve main program's value of errno. */
4267 int old_errno = errno;
4269 #ifdef USG
4270 /* USG systems forget handlers when they are used;
4271 must reestablish each time */
4272 signal (SIGINT, interrupt_signal);
4273 signal (SIGQUIT, interrupt_signal);
4274 #endif /* USG */
4276 cancel_echoing ();
4278 if (!NILP (Vquit_flag) && FRAME_TERMCAP_P (selected_frame))
4280 fflush (stdout);
4281 reset_sys_modes ();
4282 sigfree ();
4283 #ifdef SIGTSTP /* Support possible in later USG versions */
4285 * On systems which can suspend the current process and return to the original
4286 * shell, this command causes the user to end up back at the shell.
4287 * The "Auto-save" and "Abort" questions are not asked until
4288 * the user elects to return to emacs, at which point he can save the current
4289 * job and either dump core or continue.
4291 sys_suspend ();
4292 #else
4293 #ifdef VMS
4294 if (sys_suspend () == -1)
4296 printf ("Not running as a subprocess;\n");
4297 printf ("you can continue or abort.\n");
4299 #else /* not VMS */
4300 /* Perhaps should really fork an inferior shell?
4301 But that would not provide any way to get back
4302 to the original shell, ever. */
4303 printf ("No support for stopping a process on this operating system;\n");
4304 printf ("you can continue or abort.\n");
4305 #endif /* not VMS */
4306 #endif /* not SIGTSTP */
4307 printf ("Auto-save? (y or n) ");
4308 fflush (stdout);
4309 if (((c = getchar ()) & ~040) == 'Y')
4310 Fdo_auto_save (Qnil, Qnil);
4311 while (c != '\n') c = getchar ();
4312 #ifdef VMS
4313 printf ("Abort (and enter debugger)? (y or n) ");
4314 #else /* not VMS */
4315 printf ("Abort (and dump core)? (y or n) ");
4316 #endif /* not VMS */
4317 fflush (stdout);
4318 if (((c = getchar ()) & ~040) == 'Y')
4319 abort ();
4320 while (c != '\n') c = getchar ();
4321 printf ("Continuing...\n");
4322 fflush (stdout);
4323 init_sys_modes ();
4325 else
4327 /* If executing a function that wants to be interrupted out of
4328 and the user has not deferred quitting by binding `inhibit-quit'
4329 then quit right away. */
4330 if (immediate_quit && NILP (Vinhibit_quit))
4332 immediate_quit = 0;
4333 sigfree ();
4334 Fsignal (Qquit, Qnil);
4336 else
4337 /* Else request quit when it's safe */
4338 Vquit_flag = Qt;
4341 if (waiting_for_input && !echoing)
4342 quit_throw_to_read_char ();
4344 errno = old_errno;
4347 /* Handle a C-g by making read_char return C-g. */
4349 quit_throw_to_read_char ()
4351 quit_error_check ();
4352 sigfree ();
4353 /* Prevent another signal from doing this before we finish. */
4354 clear_waiting_for_input ();
4355 input_pending = 0;
4357 unread_command_events = Qnil;
4358 unread_command_char = -1;
4360 #ifdef POLL_FOR_INPUT
4361 /* May be > 1 if in recursive minibuffer. */
4362 if (poll_suppress_count == 0)
4363 abort ();
4364 #endif
4366 _longjmp (getcjmp, 1);
4369 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
4370 "Set mode of reading keyboard input.\n\
4371 First arg INTERRUPT non-nil means use input interrupts;\n\
4372 nil means use CBREAK mode.\n\
4373 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
4374 (no effect except in CBREAK mode).\n\
4375 Third arg META non-nil means accept 8-bit input (for a Meta key).\n\
4376 Otherwise, the top bit is ignored, on the assumption it is parity.\n\
4377 Optional fourth arg QUIT if non-nil specifies character to use for quitting.")
4378 (interrupt, flow, meta, quit)
4379 Lisp_Object interrupt, flow, meta, quit;
4381 if (!NILP (quit)
4382 && (XTYPE (quit) != Lisp_Int
4383 || XINT (quit) < 0 || XINT (quit) > 0400))
4384 error ("set-input-mode: QUIT must be an ASCII character.");
4386 reset_sys_modes ();
4387 #ifdef SIGIO
4388 /* Note SIGIO has been undef'd if FIONREAD is missing. */
4389 #ifdef NO_SOCK_SIGIO
4390 if (read_socket_hook)
4391 interrupt_input = 0; /* No interrupts if reading from a socket. */
4392 else
4393 #endif /* NO_SOCK_SIGIO */
4394 interrupt_input = !NILP (interrupt);
4395 #else /* not SIGIO */
4396 interrupt_input = 0;
4397 #endif /* not SIGIO */
4398 /* Our VMS input only works by interrupts, as of now. */
4399 #ifdef VMS
4400 interrupt_input = 1;
4401 #endif
4402 flow_control = !NILP (flow);
4403 meta_key = !NILP (meta);
4404 if (!NILP (quit))
4405 /* Don't let this value be out of range. */
4406 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
4408 init_sys_modes ();
4409 return Qnil;
4412 init_keyboard ()
4414 /* This is correct before outermost invocation of the editor loop */
4415 command_loop_level = -1;
4416 immediate_quit = 0;
4417 quit_char = Ctl ('g');
4418 unread_command_events = Qnil;
4419 unread_command_char = -1;
4420 total_keys = 0;
4421 recent_keys_index = 0;
4422 kbd_fetch_ptr = kbd_buffer;
4423 kbd_store_ptr = kbd_buffer;
4424 do_mouse_tracking = 0;
4425 input_pending = 0;
4427 #ifdef MULTI_FRAME
4428 /* This means that command_loop_1 won't try to select anything the first
4429 time through. */
4430 internal_last_event_frame = Qnil;
4431 Vlast_event_frame = internal_last_event_frame;
4432 #endif
4434 /* If we're running a dumped Emacs, we need to clear out
4435 kbd_buffer_frame_or_window, in case some events got into it
4436 before we dumped.
4438 If we're running an undumped Emacs, it hasn't been initialized by
4439 syms_of_keyboard yet. */
4440 if (initialized)
4441 Ffillarray (kbd_buffer_frame_or_window, Qnil);
4443 if (!noninteractive)
4445 signal (SIGINT, interrupt_signal);
4446 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
4447 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
4448 SIGQUIT and we can't tell which one it will give us. */
4449 signal (SIGQUIT, interrupt_signal);
4450 #endif /* HAVE_TERMIO */
4451 /* Note SIGIO has been undef'd if FIONREAD is missing. */
4452 #ifdef SIGIO
4453 signal (SIGIO, input_available_signal);
4454 #endif /* SIGIO */
4457 /* Use interrupt input by default, if it works and noninterrupt input
4458 has deficiencies. */
4460 #ifdef INTERRUPT_INPUT
4461 interrupt_input = 1;
4462 #else
4463 interrupt_input = 0;
4464 #endif
4466 /* Our VMS input only works by interrupts, as of now. */
4467 #ifdef VMS
4468 interrupt_input = 1;
4469 #endif
4471 sigfree ();
4472 dribble = 0;
4474 if (keyboard_init_hook)
4475 (*keyboard_init_hook) ();
4477 #ifdef POLL_FOR_INPUT
4478 poll_suppress_count = 1;
4479 start_polling ();
4480 #endif
4483 /* This type's only use is in syms_of_keyboard, to initialize the
4484 event header symbols and put properties on them. */
4485 struct event_head {
4486 Lisp_Object *var;
4487 char *name;
4488 Lisp_Object *kind;
4491 struct event_head head_table[] = {
4492 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
4493 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
4494 &Qswitch_frame, "switch-frame", &Qswitch_frame,
4497 syms_of_keyboard ()
4499 Qself_insert_command = intern ("self-insert-command");
4500 staticpro (&Qself_insert_command);
4502 Qforward_char = intern ("forward-char");
4503 staticpro (&Qforward_char);
4505 Qbackward_char = intern ("backward-char");
4506 staticpro (&Qbackward_char);
4508 Qdisabled = intern ("disabled");
4509 staticpro (&Qdisabled);
4511 Qpre_command_hook = intern ("pre-command-hook");
4512 staticpro (&Qpre_command_hook);
4514 Qpost_command_hook = intern ("post-command-hook");
4515 staticpro (&Qpost_command_hook);
4517 Qfunction_key = intern ("function-key");
4518 staticpro (&Qfunction_key);
4519 Qmouse_click = intern ("mouse-click");
4520 staticpro (&Qmouse_click);
4522 Qmode_line = intern ("mode-line");
4523 staticpro (&Qmode_line);
4524 Qvertical_line = intern ("vertical-line");
4525 staticpro (&Qvertical_line);
4526 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
4527 staticpro (&Qvertical_scroll_bar);
4528 Qmenu_bar = intern ("menu-bar");
4529 staticpro (&Qmenu_bar);
4531 Qabove_handle = intern ("above-handle");
4532 staticpro (&Qabove_handle);
4533 Qhandle = intern ("handle");
4534 staticpro (&Qhandle);
4535 Qbelow_handle = intern ("below-handle");
4536 staticpro (&Qbelow_handle);
4538 Qevent_kind = intern ("event-kind");
4539 staticpro (&Qevent_kind);
4540 Qevent_symbol_elements = intern ("event-symbol-elements");
4541 staticpro (&Qevent_symbol_elements);
4542 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
4543 staticpro (&Qevent_symbol_element_mask);
4544 Qmodifier_cache = intern ("modifier-cache");
4545 staticpro (&Qmodifier_cache);
4547 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
4548 staticpro (&Qrecompute_lucid_menubar);
4549 Qactivate_menubar_hook = intern ("activate-menubar-hook");
4550 staticpro (&Qactivate_menubar_hook);
4553 struct event_head *p;
4555 for (p = head_table;
4556 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
4557 p++)
4559 *p->var = intern (p->name);
4560 staticpro (p->var);
4561 Fput (*p->var, Qevent_kind, *p->kind);
4562 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
4566 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
4567 staticpro (&button_down_location);
4570 int i;
4571 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
4573 modifier_symbols = Fmake_vector (make_number (len), Qnil);
4574 for (i = 0; i < len; i++)
4575 if (modifier_names[i])
4576 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
4577 staticpro (&modifier_symbols);
4580 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
4581 staticpro (&recent_keys);
4583 this_command_keys = Fmake_vector (make_number (40), Qnil);
4584 staticpro (&this_command_keys);
4586 kbd_buffer_frame_or_window
4587 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
4588 staticpro (&kbd_buffer_frame_or_window);
4590 func_key_syms = Qnil;
4591 staticpro (&func_key_syms);
4593 mouse_syms = Qnil;
4594 staticpro (&mouse_syms);
4596 unread_switch_frame = Qnil;
4597 staticpro (&unread_switch_frame);
4599 defsubr (&Sread_key_sequence);
4600 defsubr (&Srecursive_edit);
4601 defsubr (&Strack_mouse);
4602 defsubr (&Sinput_pending_p);
4603 defsubr (&Scommand_execute);
4604 defsubr (&Srecent_keys);
4605 defsubr (&Sthis_command_keys);
4606 defsubr (&Ssuspend_emacs);
4607 defsubr (&Sabort_recursive_edit);
4608 defsubr (&Sexit_recursive_edit);
4609 defsubr (&Srecursion_depth);
4610 defsubr (&Stop_level);
4611 defsubr (&Sdiscard_input);
4612 defsubr (&Sopen_dribble_file);
4613 defsubr (&Sset_input_mode);
4614 defsubr (&Sexecute_extended_command);
4616 DEFVAR_LISP ("disabled-command-hook", &Vdisabled_command_hook,
4617 "Value is called instead of any command that is disabled\n\
4618 \(has a non-nil `disabled' property).");
4620 DEFVAR_LISP ("last-command-char", &last_command_char,
4621 "Last input event that was part of a command.");
4623 DEFVAR_LISP ("last-command-event", &last_command_char,
4624 "Last input event that was part of a command.");
4626 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
4627 "Last input event in a command, except for mouse menu events.\n\
4628 Mouse menus give back keys that don't look like mouse events;\n\
4629 this variable holds the actual mouse event that led to the menu,\n\
4630 so that you can determine whether the command was run by mouse or not.");
4632 DEFVAR_LISP ("last-input-char", &last_input_char,
4633 "Last input event.");
4635 DEFVAR_LISP ("last-input-event", &last_input_char,
4636 "Last input event.");
4638 DEFVAR_LISP ("unread-command-events", &unread_command_events,
4639 "List of objects to be read as next command input events.");
4641 DEFVAR_INT ("unread-command-char", &unread_command_char,
4642 "If not -1, an object to be read as next command input event.");
4644 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
4645 "Meta-prefix character code. Meta-foo as command input\n\
4646 turns into this character followed by foo.");
4647 XSET (meta_prefix_char, Lisp_Int, 033);
4649 DEFVAR_LISP ("last-command", &last_command,
4650 "The last command executed. Normally a symbol with a function definition,\n\
4651 but can be whatever was found in the keymap, or whatever the variable\n\
4652 `this-command' was set to by that command.");
4653 last_command = Qnil;
4655 DEFVAR_LISP ("this-command", &this_command,
4656 "The command now being executed.\n\
4657 The command can set this variable; whatever is put here\n\
4658 will be in `last-command' during the following command.");
4659 this_command = Qnil;
4661 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
4662 "*Number of keyboard input characters between auto-saves.\n\
4663 Zero means disable autosaving due to number of characters typed.");
4664 auto_save_interval = 300;
4666 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
4667 "*Number of seconds idle time before auto-save.\n\
4668 Zero or nil means disable auto-saving due to idleness.\n\
4669 After auto-saving due to this many seconds of idle time,\n\
4670 Emacs also does a garbage collection if that seems to be warranted.");
4671 XFASTINT (Vauto_save_timeout) = 30;
4673 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
4674 "*Nonzero means echo unfinished commands after this many seconds of pause.");
4675 echo_keystrokes = 1;
4677 DEFVAR_INT ("polling-period", &polling_period,
4678 "*Interval between polling for input during Lisp execution.\n\
4679 The reason for polling is to make C-g work to stop a running program.\n\
4680 Polling is needed only when using X windows and SIGIO does not work.\n\
4681 Polling is automatically disabled in all other cases.");
4682 polling_period = 2;
4684 DEFVAR_INT ("num-input-keys", &num_input_keys,
4685 "*Number of complete keys read from the keyboard so far.");
4686 num_input_keys = 0;
4688 #ifdef MULTI_FRAME
4689 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
4690 "*The frame in which the most recently read event occurred.\n\
4691 If the last event came from a keyboard macro, this is set to `macro'.");
4692 Vlast_event_frame = Qnil;
4693 #endif
4695 DEFVAR_LISP ("help-char", &help_char,
4696 "Character to recognize as meaning Help.\n\
4697 When it is read, do `(eval help-form)', and display result if it's a string.\n\
4698 If the value of `help-form' is nil, this char can be read normally.");
4699 XSET (help_char, Lisp_Int, Ctl ('H'));
4701 DEFVAR_LISP ("help-form", &Vhelp_form,
4702 "Form to execute when character help-char is read.\n\
4703 If the form returns a string, that string is displayed.\n\
4704 If `help-form' is nil, the help char is not recognized.");
4705 Vhelp_form = Qnil;
4707 DEFVAR_LISP ("top-level", &Vtop_level,
4708 "Form to evaluate when Emacs starts up.\n\
4709 Useful to set before you dump a modified Emacs.");
4710 Vtop_level = Qnil;
4712 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
4713 "String used as translate table for keyboard input, or nil.\n\
4714 Each character is looked up in this string and the contents used instead.\n\
4715 If string is of length N, character codes N and up are untranslated.");
4716 Vkeyboard_translate_table = Qnil;
4718 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
4719 "Non-nil means prompt with menus when appropriate.\n\
4720 This is done when reading from a keymap that has a prompt string,\n\
4721 for elements that have prompt strings.\n\
4722 The menu is displayed on the screen\n\
4723 if X menus were enabled at configuration\n\
4724 time and the previous event was a mouse click prefix key.\n\
4725 Otherwise, menu prompting uses the echo area.");
4726 menu_prompting = 1;
4728 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
4729 "Character to see next line of menu prompt.\n\
4730 Type this character while in a menu prompt to rotate around the lines of it.");
4731 XSET (menu_prompt_more_char, Lisp_Int, ' ');
4733 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
4734 "A mask of additional modifier keys to use with every keyboard character.\n\
4735 These bits follow the convention for X windows,\n\
4736 but the control and meta bits work even when you are not using X:\n\
4737 1 -- shift bit 2 -- lock bit\n\
4738 4 -- control bit 8 -- meta bit.");
4739 extra_keyboard_modifiers = 0;
4741 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
4742 "If an editing command sets this to t, deactivate the mark afterward.\n\
4743 The command loop sets this to nil before each command,\n\
4744 and tests the value when the command returns.\n\
4745 Buffer modification stores t in this variable.");
4746 Vdeactivate_mark = Qnil;
4748 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
4749 "Normal hook run before each command is executed.");
4750 Vpre_command_hook = Qnil;
4752 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
4753 "Normal hook run before each command is executed.");
4754 Vpost_command_hook = Qnil;
4756 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
4757 "t means menu bar, specified Lucid style, needs to be recomputed.");
4758 Vlucid_menu_bar_dirty_flag = Qnil;
4761 keys_of_keyboard ()
4763 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
4764 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
4765 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
4766 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
4767 initial_define_key (meta_map, 'x', "execute-extended-command");