Removed author and Adapted-By lines. They're wrong, and they're not
[emacs.git] / src / keyboard.c
blob2f8689ef13ef4690e8fafd576844d29b2f88acdb
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 /* If the previous command tried to force a specific window-start,
961 forget about that, in case this command moves point far away
962 from that position. */
963 XWINDOW (selected_window)->force_start = Qnil;
965 cmd = read_key_sequence_cmd;
966 if (!NILP (Vexecuting_macro))
968 if (!NILP (Vquit_flag))
970 Vexecuting_macro = Qt;
971 QUIT; /* Make some noise. */
972 /* Will return since macro now empty. */
976 /* Do redisplay processing after this command except in special
977 cases identified below that set no_redisplay to 1. */
978 no_redisplay = 0;
980 prev_buffer = current_buffer;
981 prev_modiff = MODIFF;
983 /* Execute the command. */
985 this_command = cmd;
986 if (!NILP (Vpre_command_hook))
987 call1 (Vrun_hooks, Qpre_command_hook);
989 if (NILP (cmd))
991 /* nil means key is undefined. */
992 bitch_at_user ();
993 defining_kbd_macro = 0;
994 update_mode_lines = 1;
995 Vprefix_arg = Qnil;
998 else
1000 if (NILP (Vprefix_arg) && ! no_direct)
1002 /* Recognize some common commands in common situations and
1003 do them directly. */
1004 if (EQ (cmd, Qforward_char) && point < ZV)
1006 struct Lisp_Vector *dp
1007 = window_display_table (XWINDOW (selected_window));
1008 lose = FETCH_CHAR (point);
1009 SET_PT (point + 1);
1010 if ((dp
1011 ? (XTYPE (DISP_CHAR_VECTOR (dp, lose)) != Lisp_Vector
1012 && XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1)
1013 : (lose >= 0x20 && lose < 0x7f))
1014 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1015 >= MODIFF)
1016 && (XFASTINT (XWINDOW (selected_window)->last_point)
1017 == point - 1)
1018 && !windows_or_buffers_changed
1019 && EQ (current_buffer->selective_display, Qnil)
1020 && !detect_input_pending ()
1021 && NILP (Vexecuting_macro))
1022 no_redisplay = direct_output_forward_char (1);
1023 goto directly_done;
1025 else if (EQ (cmd, Qbackward_char) && point > BEGV)
1027 struct Lisp_Vector *dp
1028 = window_display_table (XWINDOW (selected_window));
1029 SET_PT (point - 1);
1030 lose = FETCH_CHAR (point);
1031 if ((dp
1032 ? (XTYPE (DISP_CHAR_VECTOR (dp, lose)) != Lisp_Vector
1033 && XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1)
1034 : (lose >= 0x20 && lose < 0x7f))
1035 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1036 >= MODIFF)
1037 && (XFASTINT (XWINDOW (selected_window)->last_point)
1038 == point + 1)
1039 && !windows_or_buffers_changed
1040 && EQ (current_buffer->selective_display, Qnil)
1041 && !detect_input_pending ()
1042 && NILP (Vexecuting_macro))
1043 no_redisplay = direct_output_forward_char (-1);
1044 goto directly_done;
1046 else if (EQ (cmd, Qself_insert_command)
1047 /* Try this optimization only on ascii keystrokes. */
1048 && XTYPE (last_command_char) == Lisp_Int)
1050 unsigned char c = XINT (last_command_char);
1052 if (NILP (Vexecuting_macro) &&
1053 !EQ (minibuf_window, selected_window))
1055 if (!nonundocount || nonundocount >= 20)
1057 Fundo_boundary ();
1058 nonundocount = 0;
1060 nonundocount++;
1062 lose = (XFASTINT (XWINDOW (selected_window)->last_modified)
1063 < MODIFF)
1064 || (XFASTINT (XWINDOW (selected_window)->last_point)
1065 != point)
1066 || MODIFF <= current_buffer->save_modified
1067 || windows_or_buffers_changed
1068 || !EQ (current_buffer->selective_display, Qnil)
1069 || detect_input_pending ()
1070 || !NILP (Vexecuting_macro);
1071 if (internal_self_insert (XINT (c), 0))
1073 lose = 1;
1074 nonundocount = 0;
1076 if (!lose &&
1077 (point == ZV || FETCH_CHAR (point) == '\n'))
1079 struct Lisp_Vector *dp
1080 = window_display_table (XWINDOW (selected_window));
1081 int lose = XINT (c);
1083 if (dp)
1085 Lisp_Object obj = DISP_CHAR_VECTOR (dp, lose);
1087 if (XTYPE (obj) == Lisp_Vector
1088 && XVECTOR (obj)->size == 1
1089 && (XTYPE (obj = XVECTOR (obj)->contents[0])
1090 == Lisp_Int))
1091 no_redisplay =
1092 direct_output_for_insert (XINT (obj));
1094 else
1096 if (lose >= 0x20 && lose <= 0x7e)
1097 no_redisplay = direct_output_for_insert (lose);
1100 goto directly_done;
1104 /* Here for a command that isn't executed directly */
1106 nonundocount = 0;
1107 if (NILP (Vprefix_arg))
1108 Fundo_boundary ();
1109 Fcommand_execute (cmd, Qnil);
1112 directly_done: ;
1114 if (!NILP (Vpost_command_hook))
1115 call1 (Vrun_hooks, Qpost_command_hook);
1117 /* If there is a prefix argument,
1118 1) We don't want last_command to be ``universal-argument''
1119 (that would be dumb), so don't set last_command,
1120 2) we want to leave echoing on so that the prefix will be
1121 echoed as part of this key sequence, so don't call
1122 cancel_echoing, and
1123 3) we want to leave this_command_key_count non-zero, so that
1124 read_char will realize that it is re-reading a character, and
1125 not echo it a second time. */
1126 if (NILP (Vprefix_arg))
1128 last_command = this_command;
1129 cancel_echoing ();
1130 this_command_key_count = 0;
1133 if (!NILP (current_buffer->mark_active))
1135 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1137 current_buffer->mark_active = Qnil;
1138 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1140 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1141 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1146 /* Number of seconds between polling for input. */
1147 int polling_period;
1149 /* Nonzero means polling for input is temporarily suppresed. */
1150 int poll_suppress_count;
1152 #ifdef POLL_FOR_INPUT
1153 int polling_for_input;
1155 /* Handle an alarm once each second and read pending input
1156 so as to handle a C-g if it comces in. */
1158 SIGTYPE
1159 input_poll_signal ()
1161 #ifdef HAVE_X_WINDOWS
1162 extern int x_input_blocked;
1163 if (x_input_blocked == 0)
1164 #endif
1165 if (!waiting_for_input)
1166 read_avail_input (0);
1167 signal (SIGALRM, input_poll_signal);
1168 alarm (polling_period);
1171 #endif
1173 /* Begin signals to poll for input, if they are appropriate.
1174 This function is called unconditionally from various places. */
1176 start_polling ()
1178 #ifdef POLL_FOR_INPUT
1179 if (read_socket_hook)
1181 poll_suppress_count--;
1182 if (poll_suppress_count == 0)
1184 signal (SIGALRM, input_poll_signal);
1185 polling_for_input = 1;
1186 alarm (polling_period);
1189 #endif
1192 /* Turn off polling. */
1194 stop_polling ()
1196 #ifdef POLL_FOR_INPUT
1197 if (read_socket_hook)
1199 if (poll_suppress_count == 0)
1201 polling_for_input = 0;
1202 alarm (0);
1204 poll_suppress_count++;
1206 #endif
1209 /* Input of single characters from keyboard */
1211 Lisp_Object print_help ();
1212 static Lisp_Object kbd_buffer_get_event ();
1214 /* read a character from the keyboard; call the redisplay if needed */
1215 /* commandflag 0 means do not do auto-saving, but do do redisplay.
1216 -1 means do not do redisplay, but do do autosaving.
1217 1 means do both. */
1219 /* The arguments MAPS and NMAPS are for menu prompting.
1220 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1222 PREV_EVENT is the previous input event, or nil if we are reading
1223 the first event of a key sequence.
1225 If USED_MOUSE_MENU is non-zero, then we set *USED_MOUSE_MENU to 1
1226 if we used a mouse menu to read the input, or zero otherwise. If
1227 USED_MOUSE_MENU is zero, *USED_MOUSE_MENU is left alone. */
1229 Lisp_Object
1230 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
1231 int commandflag;
1232 int nmaps;
1233 Lisp_Object *maps;
1234 Lisp_Object prev_event;
1235 int *used_mouse_menu;
1237 register Lisp_Object c;
1238 int count;
1239 jmp_buf save_jump;
1241 if (CONSP (unread_command_events))
1243 c = XCONS (unread_command_events)->car;
1244 unread_command_events = XCONS (unread_command_events)->cdr;
1246 if (this_command_key_count == 0)
1247 goto reread_first;
1248 else
1249 goto reread;
1252 if (unread_command_char != -1)
1254 XSET (c, Lisp_Int, unread_command_char);
1255 unread_command_char = -1;
1257 if (this_command_key_count == 0)
1258 goto reread_first;
1259 else
1260 goto reread;
1263 if (!NILP (Vexecuting_macro))
1265 #ifdef MULTI_FRAME
1266 /* We set this to Qmacro; since that's not a frame, nobody will
1267 try to switch frames on us, and the selected window will
1268 remain unchanged.
1270 Since this event came from a macro, it would be misleading to
1271 leave internal_last_event_frame set to whereever the last
1272 real event came from. Normally, a switch-frame event selects
1273 internal_last_event_frame after each command is read, but
1274 events read from a macro should never cause a new frame to be
1275 selected. */
1276 Vlast_event_frame = internal_last_event_frame = Qmacro;
1277 #endif
1279 if (executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
1281 XSET (c, Lisp_Int, -1);
1282 return c;
1285 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
1286 if (XTYPE (Vexecuting_macro) == Lisp_String
1287 && (XINT (c) & 0x80))
1288 XFASTINT (c) = CHAR_META | (XINT (c) & ~0x80);
1290 executing_macro_index++;
1292 goto from_macro;
1295 if (!NILP (unread_switch_frame))
1297 c = unread_switch_frame;
1298 unread_switch_frame = Qnil;
1300 /* This event should make it into this_command_keys, and get echoed
1301 again, so we go to reread_first, rather than reread. */
1302 goto reread_first;
1305 /* Save outer setjmp data, in case called recursively. */
1306 save_getcjmp (save_jump);
1308 stop_polling ();
1310 if (commandflag >= 0 && !input_pending && !detect_input_pending ())
1311 redisplay ();
1313 if (_setjmp (getcjmp))
1315 XSET (c, Lisp_Int, quit_char);
1316 #ifdef MULTI_FRAME
1317 XSET (internal_last_event_frame, Lisp_Frame, selected_frame);
1318 Vlast_event_frame = internal_last_event_frame;
1319 #endif
1321 goto non_reread;
1324 /* Message turns off echoing unless more keystrokes turn it on again. */
1325 if (echo_area_glyphs && *echo_area_glyphs && echo_area_glyphs != echobuf)
1326 cancel_echoing ();
1327 else
1328 /* If already echoing, continue. */
1329 echo_dash ();
1331 /* If in middle of key sequence and minibuffer not active,
1332 start echoing if enough time elapses. */
1333 if (minibuf_level == 0 && !immediate_echo && this_command_key_count > 0
1334 && echo_keystrokes > 0
1335 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0))
1337 Lisp_Object tem0;
1339 /* After a mouse event, start echoing right away.
1340 This is because we are probably about to display a menu,
1341 and we don't want to delay before doing so. */
1342 if (EVENT_HAS_PARAMETERS (prev_event))
1343 echo ();
1344 else
1346 tem0 = sit_for (echo_keystrokes, 0, 1, 1);
1347 if (EQ (tem0, Qt))
1348 echo ();
1352 /* Maybe auto save due to number of keystrokes or idle time. */
1354 if (commandflag != 0
1355 && auto_save_interval > 0
1356 && num_nonmacro_input_chars - last_auto_save > max (auto_save_interval, 20)
1357 && !detect_input_pending ())
1359 jmp_buf temp;
1360 save_getcjmp (temp);
1361 Fdo_auto_save (Qnil, Qnil);
1362 restore_getcjmp (temp);
1365 /* Try reading a character via menu prompting.
1366 Try this before the sit-for, because the sit-for
1367 would do the wrong thing if we are supposed to do
1368 menu prompting. */
1369 c = Qnil;
1370 if (INTERACTIVE && !NILP (prev_event))
1371 c = read_char_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
1373 /* Slow down auto saves logarithmically in size of current buffer,
1374 and garbage collect while we're at it. */
1375 if (NILP (c))
1377 int delay_level, buffer_size;
1379 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
1380 last_non_minibuf_size = Z - BEG;
1381 buffer_size = (last_non_minibuf_size >> 8) + 1;
1382 delay_level = 0;
1383 while (buffer_size > 64)
1384 delay_level++, buffer_size -= buffer_size >> 2;
1385 if (delay_level < 4) delay_level = 4;
1386 /* delay_level is 4 for files under around 50k, 7 at 100k,
1387 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
1389 /* Auto save if enough time goes by without input. */
1390 if (commandflag != 0
1391 && num_nonmacro_input_chars > last_auto_save
1392 && XTYPE (Vauto_save_timeout) == Lisp_Int
1393 && XINT (Vauto_save_timeout) > 0)
1395 Lisp_Object tem0;
1396 int delay = delay_level * XFASTINT (Vauto_save_timeout) / 4;
1397 tem0 = sit_for (delay, 0, 1, 1);
1398 if (EQ (tem0, Qt))
1400 jmp_buf temp;
1401 save_getcjmp (temp);
1402 Fdo_auto_save (Qnil, Qnil);
1403 restore_getcjmp (temp);
1405 /* If we have auto-saved and there is still no input
1406 available, garbage collect if there has been enough
1407 consing going on to make it worthwhile. */
1408 if (!detect_input_pending ()
1409 && consing_since_gc > gc_cons_threshold / 2)
1410 Fgarbage_collect ();
1415 /* Actually read a character, waiting if necessary. */
1416 if (NILP (c))
1417 c = kbd_buffer_get_event ();
1419 if (NILP (c))
1420 abort (); /* Don't think this can happen. */
1422 /* Terminate Emacs in batch mode if at eof. */
1423 if (noninteractive && XTYPE (c) == Lisp_Int && XINT (c) < 0)
1424 Fkill_emacs (make_number (1));
1426 /* Test for ControlMask and Mod1Mask. */
1427 if (extra_keyboard_modifiers & 4)
1428 c &= ~0140;
1429 if (extra_keyboard_modifiers & 8)
1430 c |= 0200;
1432 non_reread:
1434 restore_getcjmp (save_jump);
1436 start_polling ();
1438 echo_area_glyphs = 0;
1440 /* Handle things that only apply to characters. */
1441 if (XTYPE (c) == Lisp_Int)
1443 /* If kbd_buffer_get_event gave us an EOF, return that. */
1444 if (XINT (c) == -1)
1445 return c;
1447 if (XTYPE (Vkeyboard_translate_table) == Lisp_String
1448 && XSTRING (Vkeyboard_translate_table)->size > XFASTINT (c))
1449 XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[XFASTINT (c)]);
1452 total_keys++;
1453 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
1454 if (++recent_keys_index >= NUM_RECENT_KEYS)
1455 recent_keys_index = 0;
1457 /* Write c to the dribble file. If c is a lispy event, write
1458 the event's symbol to the dribble file, in <brackets>. Bleaugh.
1459 If you, dear reader, have a better idea, you've got the source. :-) */
1460 if (dribble)
1462 if (XTYPE (c) == Lisp_Int)
1463 putc (XINT (c), dribble);
1464 else
1466 Lisp_Object dribblee = c;
1468 /* If it's a structured event, take the event header. */
1469 dribblee = EVENT_HEAD (dribblee);
1471 if (XTYPE (dribblee) == Lisp_Symbol)
1473 putc ('<', dribble);
1474 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
1475 XSYMBOL (dribblee)->name->size,
1476 dribble);
1477 putc ('>', dribble);
1481 fflush (dribble);
1484 store_kbd_macro_char (c);
1486 num_nonmacro_input_chars++;
1488 from_macro:
1489 reread_first:
1491 /* Record this character as part of the current key.
1492 Don't record mouse motion; it should never matter. */
1493 if (! (EVENT_HAS_PARAMETERS (c)
1494 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
1496 echo_char (c);
1497 add_command_key (c);
1500 /* Re-reading in the middle of a command */
1501 reread:
1502 last_input_char = c;
1503 num_input_chars++;
1505 /* Process the help character specially if enabled */
1506 if (EQ (c, help_char) && !NILP (Vhelp_form))
1508 Lisp_Object tem0;
1509 count = specpdl_ptr - specpdl;
1511 record_unwind_protect (Fset_window_configuration,
1512 Fcurrent_window_configuration (Qnil));
1514 tem0 = Feval (Vhelp_form);
1515 if (XTYPE (tem0) == Lisp_String)
1516 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
1518 cancel_echoing ();
1519 c = read_char (0, 0, 0, Qnil, 0);
1520 /* Remove the help from the frame */
1521 unbind_to (count, Qnil);
1522 redisplay ();
1523 if (EQ (c, make_number (040)))
1525 cancel_echoing ();
1526 c = read_char (0, 0, 0, Qnil, 0);
1530 return c;
1533 Lisp_Object
1534 print_help (object)
1535 Lisp_Object object;
1537 Fprinc (object, Qnil);
1538 return Qnil;
1541 /* Copy out or in the info on where C-g should throw to.
1542 This is used when running Lisp code from within get_char,
1543 in case get_char is called recursively.
1544 See read_process_output. */
1546 save_getcjmp (temp)
1547 jmp_buf temp;
1549 bcopy (getcjmp, temp, sizeof getcjmp);
1552 restore_getcjmp (temp)
1553 jmp_buf temp;
1555 bcopy (temp, getcjmp, sizeof getcjmp);
1559 /* Low level keyboard/mouse input.
1560 kbd_buffer_store_event places events in kbd_buffer, and
1561 kbd_buffer_get_event retrieves them.
1562 mouse_moved indicates when the mouse has moved again, and
1563 *mouse_position_hook provides the mouse position. */
1565 /* Set this for debugging, to have a way to get out */
1566 int stop_character;
1568 extern int frame_garbaged;
1570 /* Return true iff there are any events in the queue that read-char
1571 would return. If this returns false, a read-char would block. */
1572 static int
1573 readable_events ()
1575 return ! EVENT_QUEUES_EMPTY;
1579 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
1580 of this function. */
1581 static Lisp_Object
1582 tracking_off (old_value)
1583 Lisp_Object old_value;
1585 if (! XFASTINT (old_value))
1587 do_mouse_tracking = 0;
1589 /* Redisplay may have been preempted because there was input
1590 available, and it assumes it will be called again after the
1591 input has been processed. If the only input available was
1592 the sort that we have just disabled, then we need to call
1593 redisplay. */
1594 if (!readable_events ())
1596 redisplay_preserve_echo_area ();
1597 get_input_pending (&input_pending);
1602 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
1603 "Evaluate BODY with mouse movement events enabled.\n\
1604 Within a `track-mouse' form, mouse motion generates input events that\n\
1605 you can read with `read-event'.\n\
1606 Normally, mouse motion is ignored.")
1607 (args)
1608 Lisp_Object args;
1610 int count = specpdl_ptr - specpdl;
1611 Lisp_Object val;
1613 XSET (val, Lisp_Int, do_mouse_tracking);
1614 record_unwind_protect (tracking_off, val);
1616 do_mouse_tracking = 1;
1618 val = Fprogn (args);
1619 return unbind_to (count, val);
1622 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
1624 void
1625 kbd_buffer_store_event (event)
1626 register struct input_event *event;
1628 if (event->kind == no_event)
1629 abort ();
1631 if (event->kind == ascii_keystroke)
1633 register int c = XFASTINT (event->code) & 0377;
1635 if (c == quit_char)
1637 extern SIGTYPE interrupt_signal ();
1639 #ifdef MULTI_FRAME
1640 /* If this results in a quit_char being returned to Emacs as
1641 input, set Vlast_event_frame properly. If this doesn't
1642 get returned to Emacs as an event, the next event read
1643 will set Vlast_event_frame again, so this is safe to do. */
1645 Lisp_Object focus =
1646 FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
1648 if (NILP (focus))
1649 internal_last_event_frame = event->frame_or_window;
1650 else
1651 internal_last_event_frame = focus;
1652 Vlast_event_frame = internal_last_event_frame;
1654 #endif
1656 last_event_timestamp = event->timestamp;
1657 interrupt_signal ();
1658 return;
1661 if (c && c == stop_character)
1663 sys_suspend ();
1664 return;
1667 XSET (event->code, Lisp_Int, c);
1670 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
1671 kbd_store_ptr = kbd_buffer;
1673 /* Don't let the very last slot in the buffer become full,
1674 since that would make the two pointers equal,
1675 and that is indistinguishable from an empty buffer.
1676 Discard the event if it would fill the last slot. */
1677 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
1679 kbd_store_ptr->kind = event->kind;
1680 kbd_store_ptr->code = event->code;
1681 kbd_store_ptr->part = event->part;
1682 kbd_store_ptr->frame_or_window = event->frame_or_window;
1683 kbd_store_ptr->modifiers = event->modifiers;
1684 kbd_store_ptr->x = event->x;
1685 kbd_store_ptr->y = event->y;
1686 kbd_store_ptr->timestamp = event->timestamp;
1687 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
1688 - kbd_buffer]
1689 = event->frame_or_window);
1691 kbd_store_ptr++;
1695 static Lisp_Object make_lispy_event ();
1696 static Lisp_Object make_lispy_movement ();
1697 static Lisp_Object modify_event_symbol ();
1698 static Lisp_Object make_lispy_switch_frame ();
1700 static Lisp_Object
1701 kbd_buffer_get_event ()
1703 register int c;
1704 Lisp_Object obj;
1706 if (noninteractive)
1708 c = getchar ();
1709 XSET (obj, Lisp_Int, c);
1710 return obj;
1713 retry:
1714 /* Wait until there is input available. */
1715 for (;;)
1717 if (!EVENT_QUEUES_EMPTY)
1718 break;
1720 /* If the quit flag is set, then read_char will return
1721 quit_char, so that counts as "available input." */
1722 if (!NILP (Vquit_flag))
1723 quit_throw_to_read_char ();
1725 /* One way or another, wait until input is available; then, if
1726 interrupt handlers have not read it, read it now. */
1728 #ifdef OLDVMS
1729 wait_for_kbd_input ();
1730 #else
1731 /* Note SIGIO has been undef'd if FIONREAD is missing. */
1732 #ifdef SIGIO
1733 gobble_input (0);
1734 #endif /* SIGIO */
1735 if (EVENT_QUEUES_EMPTY)
1737 Lisp_Object minus_one;
1739 XSET (minus_one, Lisp_Int, -1);
1740 wait_reading_process_input (0, 0, minus_one, 1);
1742 if (!interrupt_input && EVENT_QUEUES_EMPTY)
1744 read_avail_input (0);
1747 #endif /* not VMS */
1750 /* At this point, we know that there is a readable event available
1751 somewhere. If the event queue is empty, then there must be a
1752 mouse movement enabled and available. */
1753 if (kbd_fetch_ptr != kbd_store_ptr)
1755 struct input_event *event;
1757 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
1758 ? kbd_fetch_ptr
1759 : kbd_buffer);
1761 last_event_timestamp = event->timestamp;
1763 obj = Qnil;
1765 /* These two kinds of events get special handling
1766 and don't actually appear to the command loop. */
1767 if (event->kind == selection_request_event)
1769 x_handle_selection_request (event);
1770 kbd_fetch_ptr = event + 1;
1771 goto retry;
1774 if (event->kind == selection_clear_event)
1776 x_handle_selection_clear (event);
1777 kbd_fetch_ptr = event + 1;
1778 goto retry;
1781 #ifdef MULTI_FRAME
1782 /* If this event is on a different frame, return a switch-frame this
1783 time, and leave the event in the queue for next time. */
1785 Lisp_Object frame = event->frame_or_window;
1786 Lisp_Object focus;
1788 if (XTYPE (frame) == Lisp_Window)
1789 frame = WINDOW_FRAME (XWINDOW (frame));
1791 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
1792 if (! NILP (focus))
1793 frame = focus;
1795 if (! EQ (frame, internal_last_event_frame))
1797 internal_last_event_frame = frame;
1798 obj = make_lispy_switch_frame (frame);
1801 #endif
1803 /* If we didn't decide to make a switch-frame event, go ahead
1804 and build a real event from the queue entry. */
1805 if (NILP (obj))
1807 obj = make_lispy_event (event);
1809 /* Wipe out this event, to catch bugs. */
1810 event->kind = no_event;
1811 (XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer]
1812 = Qnil);
1814 kbd_fetch_ptr = event + 1;
1817 else if (do_mouse_tracking && mouse_moved)
1819 FRAME_PTR f;
1820 Lisp_Object bar_window;
1821 enum scroll_bar_part part;
1822 Lisp_Object x, y;
1823 unsigned long time;
1825 (*mouse_position_hook) (&f, &bar_window, &part, &x, &y, &time);
1827 obj = Qnil;
1829 #ifdef MULTI_FRAME
1830 /* Decide if we should generate a switch-frame event. Don't
1831 generate switch-frame events for motion outside of all Emacs
1832 frames. */
1833 if (f)
1835 Lisp_Object frame = FRAME_FOCUS_FRAME (f);
1837 if (NILP (frame))
1838 XSET (frame, Lisp_Frame, f);
1840 if (! EQ (frame, internal_last_event_frame))
1842 XSET (internal_last_event_frame, Lisp_Frame, frame);
1843 obj = make_lispy_switch_frame (internal_last_event_frame);
1846 #endif
1848 /* If we didn't decide to make a switch-frame event, go ahead and
1849 return a mouse-motion event. */
1850 if (NILP (obj))
1851 obj = make_lispy_movement (f, bar_window, part, x, y, time);
1853 else
1854 /* We were promised by the above while loop that there was
1855 something for us to read! */
1856 abort ();
1858 /* If something gave back nil as the Lispy event,
1859 it means the event was discarded, so try again. */
1860 if (NILP (obj))
1861 goto retry;
1863 input_pending = readable_events ();
1865 #ifdef MULTI_FRAME
1866 Vlast_event_frame = internal_last_event_frame;
1867 #endif
1869 return (obj);
1873 /* Caches for modify_event_symbol. */
1874 static Lisp_Object func_key_syms;
1875 static Lisp_Object mouse_syms;
1877 /* You'll notice that this table is arranged to be conveniently
1878 indexed by X Windows keysym values. */
1879 static char *lispy_function_keys[] =
1881 /* X Keysym value */
1883 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00 */
1884 "backspace",
1885 "tab",
1886 "linefeed",
1887 "clear",
1889 "return",
1890 0, 0,
1891 0, 0, 0, /* 0xff10 */
1892 "pause",
1893 0, 0, 0, 0, 0, 0, 0,
1894 "escape",
1895 0, 0, 0, 0,
1896 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff20...2f */
1897 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff30...3f */
1898 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
1900 "home", /* 0xff50 */ /* IsCursorKey */
1901 "left",
1902 "up",
1903 "right",
1904 "down",
1905 "prior",
1906 "next",
1907 "end",
1908 "begin",
1909 0, /* 0xff59 */
1910 0, 0, 0, 0, 0, 0,
1911 "select", /* 0xff60 */ /* IsMiscFunctionKey */
1912 "print",
1913 "execute",
1914 "insert",
1915 0, /* 0xff64 */
1916 "undo",
1917 "redo",
1918 "menu",
1919 "find",
1920 "cancel",
1921 "help",
1922 "break", /* 0xff6b */
1924 /* Here are some keys found mostly on HP keyboards. The X event
1925 handling code will strip bit 29, which flags vendor-specific
1926 keysyms. */
1927 "reset", /* 0x1000ff6c */
1928 "system",
1929 "user",
1930 "clearline",
1931 "insertline",
1932 "deleteline",
1933 "insertchar",
1934 "deletechar",
1935 "backtab",
1936 "kp_backtab", /* 0x1000ff75 */
1937 0, /* 0xff76 */
1938 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff7f */
1939 "kp-space", /* 0xff80 */ /* IsKeypadKey */
1940 0, 0, 0, 0, 0, 0, 0, 0,
1941 "kp-tab", /* 0xff89 */
1942 0, 0, 0,
1943 "kp-enter", /* 0xff8d */
1944 0, 0, 0,
1945 "kp-f1", /* 0xff91 */
1946 "kp-f2",
1947 "kp-f3",
1948 "kp-f4",
1949 0, /* 0xff95 */
1950 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1951 "kp-multiply", /* 0xffaa */
1952 "kp-add",
1953 "kp-separator",
1954 "kp-subtract",
1955 "kp-decimal",
1956 "kp-divide", /* 0xffaf */
1957 "kp-0", /* 0xffb0 */
1958 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
1959 0, /* 0xffba */
1960 0, 0,
1961 "kp-equal", /* 0xffbd */
1962 "f1", /* 0xffbe */ /* IsFunctionKey */
1963 "f2",
1964 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
1965 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
1966 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
1967 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
1968 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
1969 0, 0, 0, 0, 0, 0, 0, 0,
1970 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
1971 0, 0, 0, 0, 0, 0, 0, "delete"
1974 static char *lispy_mouse_names[] =
1976 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
1979 /* Scroll bar parts. */
1980 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
1982 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
1983 Lisp_Object *scroll_bar_parts[] = {
1984 &Qabove_handle, &Qhandle, &Qbelow_handle
1988 /* A vector, indexed by button number, giving the down-going location
1989 of currently depressed buttons, both scroll bar and non-scroll bar.
1991 The elements have the form
1992 (BUTTON-NUMBER MODIFIER-MASK . REST)
1993 where REST is the cdr of a position as it would be reported in the event.
1995 The make_lispy_event function stores positions here to tell the
1996 difference between click and drag events, and to store the starting
1997 location to be included in drag events. */
1999 static Lisp_Object button_down_location;
2001 /* Given a struct input_event, build the lisp event which represents
2002 it. If EVENT is 0, build a mouse movement event from the mouse
2003 movement buffer, which should have a movement event in it.
2005 Note that events must be passed to this function in the order they
2006 are received; this function stores the location of button presses
2007 in order to build drag events when the button is released. */
2009 static Lisp_Object
2010 make_lispy_event (event)
2011 struct input_event *event;
2013 #ifdef SWITCH_ENUM_BUG
2014 switch ((int) event->kind)
2015 #else
2016 switch (event->kind)
2017 #endif
2019 /* A simple keystroke. */
2020 case ascii_keystroke:
2022 int c = XFASTINT (event->code);
2023 /* Include the bits for control and shift
2024 only if the basic ASCII code can't indicate them. */
2025 if ((event->modifiers & ctrl_modifier)
2026 && c >= 040)
2027 c |= ctrl_modifier;
2028 if (XFASTINT (event->code) < 040
2029 && (event->modifiers & shift_modifier))
2030 c |= shift_modifier;
2031 c |= (event->modifiers
2032 & (meta_modifier | alt_modifier
2033 | hyper_modifier | super_modifier));
2034 return c;
2037 /* A function key. The symbol may need to have modifier prefixes
2038 tacked onto it. */
2039 case non_ascii_keystroke:
2040 return modify_event_symbol (XFASTINT (event->code), event->modifiers,
2041 Qfunction_key,
2042 lispy_function_keys, &func_key_syms,
2043 (sizeof (lispy_function_keys)
2044 / sizeof (lispy_function_keys[0])));
2045 break;
2047 /* A mouse click. Figure out where it is, decide whether it's
2048 a press, click or drag, and build the appropriate structure. */
2049 case mouse_click:
2050 case scroll_bar_click:
2052 int button = XFASTINT (event->code);
2053 Lisp_Object position;
2054 Lisp_Object *start_pos_ptr;
2055 Lisp_Object start_pos;
2057 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
2058 abort ();
2060 /* Build the position as appropriate for this mouse click. */
2061 if (event->kind == mouse_click)
2063 int part;
2064 struct frame *f = XFRAME (event->frame_or_window);
2065 Lisp_Object window
2066 = window_from_coordinates (f, XINT (event->x), XINT (event->y),
2067 &part);
2068 Lisp_Object posn;
2070 if (XINT (event->y) < FRAME_MENU_BAR_LINES (f))
2072 int hpos;
2073 Lisp_Object items;
2074 items = FRAME_MENU_BAR_ITEMS (f);
2075 for (; CONSP (items); items = XCONS (items)->cdr)
2077 Lisp_Object pos, string;
2078 pos = Fcdr (Fcdr (Fcar (items)));
2079 string = Fcar (Fcdr (Fcar (items)));
2080 if (XINT (event->x) > XINT (pos)
2081 && XINT (event->x) <= XINT (pos) + XSTRING (string)->size)
2082 break;
2084 position
2085 = Fcons (event->frame_or_window,
2086 Fcons (Qmenu_bar,
2087 Fcons (Fcons (event->x, event->y),
2088 Fcons (make_number (event->timestamp),
2089 Qnil))));
2091 if (CONSP (items))
2092 return Fcons (Fcar (Fcar (items)),
2093 Fcons (position, Qnil));
2094 else
2095 return Fcons (Qnil, Fcons (position, Qnil));
2097 else if (XTYPE (window) != Lisp_Window)
2098 posn = Qnil;
2099 else
2101 XSETINT (event->x,
2102 (XINT (event->x) - XINT (XWINDOW (window)->left)));
2103 XSETINT (event->y,
2104 (XINT (event->y) - XINT (XWINDOW (window)->top)));
2106 if (part == 1)
2107 posn = Qmode_line;
2108 else if (part == 2)
2109 posn = Qvertical_line;
2110 else
2111 XSET (posn, Lisp_Int,
2112 buffer_posn_from_coords (XWINDOW (window),
2113 XINT (event->x),
2114 XINT (event->y)));
2117 position
2118 = Fcons (window,
2119 Fcons (posn,
2120 Fcons (Fcons (event->x, event->y),
2121 Fcons (make_number (event->timestamp),
2122 Qnil))));
2124 else
2126 Lisp_Object window = event->frame_or_window;
2127 Lisp_Object portion_whole = Fcons (event->x, event->y);
2128 Lisp_Object part = *scroll_bar_parts[(int) event->part];
2130 position =
2131 Fcons (window,
2132 Fcons (Qvertical_scroll_bar,
2133 Fcons (portion_whole,
2134 Fcons (make_number (event->timestamp),
2135 Fcons (part,
2136 Qnil)))));
2139 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
2141 start_pos = *start_pos_ptr;
2142 *start_pos_ptr = Qnil;
2144 /* If this is a button press, squirrel away the location, so
2145 we can decide later whether it was a click or a drag. */
2146 if (event->modifiers & down_modifier)
2147 *start_pos_ptr = Fcopy_alist (position);
2149 /* Now we're releasing a button - check the co-ordinates to
2150 see if this was a click or a drag. */
2151 else if (event->modifiers & up_modifier)
2153 /* If we did not see a down before this up,
2154 ignore the up. Probably this happened because
2155 the down event chose a menu item.
2156 It would be an annoyance to treat the release
2157 of the button that chose the menu item
2158 as a separate event. */
2160 if (XTYPE (start_pos) != Lisp_Cons)
2161 return Qnil;
2163 event->modifiers &= ~up_modifier;
2164 #if 0 /* Formerly we treated an up with no down as a click event. */
2165 if (XTYPE (start_pos) != Lisp_Cons)
2166 event->modifiers |= click_modifier;
2167 else
2168 #endif
2170 /* The third element of every position should be the (x,y)
2171 pair. */
2172 Lisp_Object down = Fnth (make_number (2), start_pos);
2174 event->modifiers |= ((EQ (event->x, XCONS (down)->car)
2175 && EQ (event->y, XCONS (down)->cdr))
2176 ? click_modifier
2177 : drag_modifier);
2180 else
2181 /* Every mouse event should either have the down_modifier or
2182 the up_modifier set. */
2183 abort ();
2186 /* Get the symbol we should use for the mouse click. */
2187 Lisp_Object head
2188 = modify_event_symbol (button,
2189 event->modifiers,
2190 Qmouse_click,
2191 lispy_mouse_names, &mouse_syms,
2192 (sizeof (lispy_mouse_names)
2193 / sizeof (lispy_mouse_names[0])));
2195 if (event->modifiers & drag_modifier)
2196 return Fcons (head,
2197 Fcons (start_pos,
2198 Fcons (position,
2199 Qnil)));
2200 else
2201 return Fcons (head,
2202 Fcons (position,
2203 Qnil));
2207 /* The 'kind' field of the event is something we don't recognize. */
2208 default:
2209 abort ();
2213 static Lisp_Object
2214 make_lispy_movement (frame, bar_window, part, x, y, time)
2215 FRAME_PTR frame;
2216 Lisp_Object bar_window;
2217 enum scroll_bar_part part;
2218 Lisp_Object x, y;
2219 unsigned long time;
2221 /* Is it a scroll bar movement? */
2222 if (frame && ! NILP (bar_window))
2224 Lisp_Object part_sym = *scroll_bar_parts[(int) part];
2226 return Fcons (Qscroll_bar_movement,
2227 (Fcons (Fcons (bar_window,
2228 Fcons (Qvertical_scroll_bar,
2229 Fcons (Fcons (x, y),
2230 Fcons (make_number (time),
2231 Fcons (part_sym,
2232 Qnil))))),
2233 Qnil)));
2236 /* Or is it an ordinary mouse movement? */
2237 else
2239 int area;
2240 Lisp_Object window =
2241 (frame
2242 ? window_from_coordinates (frame, XINT (x), XINT (y), &area)
2243 : Qnil);
2244 Lisp_Object posn;
2246 if (XTYPE (window) == Lisp_Window)
2248 XSETINT (x, XINT (x) - XINT (XWINDOW (window)->left));
2249 XSETINT (y, XINT (y) - XINT (XWINDOW (window)->top));
2251 if (area == 1)
2252 posn = Qmode_line;
2253 else if (area == 2)
2254 posn = Qvertical_line;
2255 else
2256 XSET (posn, Lisp_Int,
2257 buffer_posn_from_coords (XWINDOW (window),
2258 XINT (x), XINT (y)));
2260 else
2262 window = Qnil;
2263 posn = Qnil;
2266 return Fcons (Qmouse_movement,
2267 Fcons (Fcons (window,
2268 Fcons (posn,
2269 Fcons (Fcons (x, y),
2270 Fcons (make_number (time),
2271 Qnil)))),
2272 Qnil));
2276 /* Construct a switch frame event. */
2277 static Lisp_Object
2278 make_lispy_switch_frame (frame)
2279 Lisp_Object frame;
2281 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
2284 /* Manipulating modifiers. */
2286 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
2288 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
2289 SYMBOL's name of the end of the modifiers; the string from this
2290 position is the unmodified symbol name.
2292 This doesn't use any caches. */
2293 static int
2294 parse_modifiers_uncached (symbol, modifier_end)
2295 Lisp_Object symbol;
2296 int *modifier_end;
2298 struct Lisp_String *name;
2299 int i;
2300 int modifiers;
2302 CHECK_SYMBOL (symbol, 1);
2304 modifiers = 0;
2305 name = XSYMBOL (symbol)->name;
2308 for (i = 0; i+2 <= name->size; )
2309 switch (name->data[i])
2311 #define SINGLE_LETTER_MOD(bit) \
2312 if (name->data[i+1] != '-') \
2313 goto no_more_modifiers; \
2314 modifiers |= bit; \
2315 i += 2;
2317 case 'A':
2318 SINGLE_LETTER_MOD (alt_modifier);
2319 break;
2321 case 'C':
2322 SINGLE_LETTER_MOD (ctrl_modifier);
2323 break;
2325 case 'H':
2326 SINGLE_LETTER_MOD (hyper_modifier);
2327 break;
2329 case 'M':
2330 SINGLE_LETTER_MOD (meta_modifier);
2331 break;
2333 case 'S':
2334 SINGLE_LETTER_MOD (shift_modifier);
2335 break;
2337 case 's':
2338 SINGLE_LETTER_MOD (super_modifier);
2339 break;
2341 case 'd':
2342 if (i + 5 > name->size)
2343 goto no_more_modifiers;
2344 if (! strncmp (name->data + i, "drag-", 5))
2346 modifiers |= drag_modifier;
2347 i += 5;
2349 else if (! strncmp (name->data + i, "down-", 5))
2351 modifiers |= down_modifier;
2352 i += 5;
2354 else
2355 goto no_more_modifiers;
2356 break;
2358 default:
2359 goto no_more_modifiers;
2361 #undef SINGLE_LETTER_MOD
2363 no_more_modifiers:
2365 /* Should we include the `click' modifier? */
2366 if (! (modifiers & (down_modifier | drag_modifier))
2367 && i + 7 == name->size
2368 && strncmp (name->data + i, "mouse-", 6) == 0
2369 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
2370 modifiers |= click_modifier;
2372 if (modifier_end)
2373 *modifier_end = i;
2375 return modifiers;
2379 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
2380 prepended to the string BASE[0..BASE_LEN-1].
2381 This doesn't use any caches. */
2382 static Lisp_Object
2383 apply_modifiers_uncached (modifiers, base, base_len)
2384 int modifiers;
2385 char *base;
2386 int base_len;
2388 /* Since BASE could contain nulls, we can't use intern here; we have
2389 to use Fintern, which expects a genuine Lisp_String, and keeps a
2390 reference to it. */
2391 char *new_mods =
2392 (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-"));
2393 int mod_len;
2396 char *p = new_mods;
2398 /* Only the event queue may use the `up' modifier; it should always
2399 be turned into a click or drag event before presented to lisp code. */
2400 if (modifiers & up_modifier)
2401 abort ();
2403 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
2404 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
2405 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
2406 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
2407 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
2408 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
2409 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
2410 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
2411 /* The click modifier is denoted by the absence of other modifiers. */
2413 *p = '\0';
2415 mod_len = p - new_mods;
2419 Lisp_Object new_name = make_uninit_string (mod_len + base_len);
2421 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
2422 bcopy (base, XSTRING (new_name)->data + mod_len, base_len);
2424 return Fintern (new_name, Qnil);
2429 static char *modifier_names[] =
2431 "up", 0, 0, 0, 0, 0, 0, "down",
2432 "drag", "click", 0, 0, 0, 0, 0, 0,
2433 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
2436 static Lisp_Object modifier_symbols;
2438 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
2439 static Lisp_Object
2440 lispy_modifier_list (modifiers)
2441 int modifiers;
2443 Lisp_Object modifier_list;
2444 int i;
2446 modifier_list = Qnil;
2447 for (i = 0; (1<<i) <= modifiers; i++)
2448 if (modifiers & (1<<i))
2450 if (i >= XVECTOR (modifier_symbols)->size)
2451 abort ();
2452 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
2453 modifier_list);
2456 return modifier_list;
2460 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
2461 where UNMODIFIED is the unmodified form of SYMBOL,
2462 MASK is the set of modifiers present in SYMBOL's name.
2463 This is similar to parse_modifiers_uncached, but uses the cache in
2464 SYMBOL's Qevent_symbol_element_mask property, and maintains the
2465 Qevent_symbol_elements property. */
2466 static Lisp_Object
2467 parse_modifiers (symbol)
2468 Lisp_Object symbol;
2470 Lisp_Object elements = Fget (symbol, Qevent_symbol_element_mask);
2472 if (CONSP (elements))
2473 return elements;
2474 else
2476 int end;
2477 int modifiers = parse_modifiers_uncached (symbol, &end);
2478 Lisp_Object unmodified
2479 = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
2480 XSYMBOL (symbol)->name->size - end),
2481 Qnil);
2482 Lisp_Object mask;
2484 XFASTINT (mask) = modifiers;
2485 elements = Fcons (unmodified, Fcons (mask, Qnil));
2487 /* Cache the parsing results on SYMBOL. */
2488 Fput (symbol, Qevent_symbol_element_mask,
2489 elements);
2490 Fput (symbol, Qevent_symbol_elements,
2491 Fcons (unmodified, lispy_modifier_list (modifiers)));
2493 /* Since we know that SYMBOL is modifiers applied to unmodified,
2494 it would be nice to put that in unmodified's cache.
2495 But we can't, since we're not sure that parse_modifiers is
2496 canonical. */
2498 return elements;
2502 /* Apply the modifiers MODIFIERS to the symbol BASE.
2503 BASE must be unmodified.
2505 This is like apply_modifiers_uncached, but uses BASE's
2506 Qmodifier_cache property, if present. It also builds
2507 Qevent_symbol_elements properties, since it has that info anyway.
2509 apply_modifiers copies the value of BASE's Qevent_kind property to
2510 the modified symbol. */
2511 static Lisp_Object
2512 apply_modifiers (modifiers, base)
2513 int modifiers;
2514 Lisp_Object base;
2516 Lisp_Object cache, index, entry, new_symbol;
2518 /* The click modifier never figures into cache indices. */
2519 cache = Fget (base, Qmodifier_cache);
2520 XFASTINT (index) = (modifiers & ~click_modifier);
2521 entry = Fassq (index, cache);
2523 if (CONSP (entry))
2524 new_symbol = XCONS (entry)->cdr;
2525 else
2527 /* We have to create the symbol ourselves. */
2528 new_symbol = apply_modifiers_uncached (modifiers,
2529 XSYMBOL (base)->name->data,
2530 XSYMBOL (base)->name->size);
2532 /* Add the new symbol to the base's cache. */
2533 entry = Fcons (index, new_symbol);
2534 Fput (base, Qmodifier_cache, Fcons (entry, cache));
2536 /* We have the parsing info now for free, so add it to the caches. */
2537 XFASTINT (index) = modifiers;
2538 Fput (new_symbol, Qevent_symbol_element_mask,
2539 Fcons (base, Fcons (index, Qnil)));
2540 Fput (new_symbol, Qevent_symbol_elements,
2541 Fcons (base, lispy_modifier_list (modifiers)));
2544 /* Make sure this symbol is of the same kind as BASE.
2546 You'd think we could just set this once and for all when we
2547 intern the symbol above, but reorder_modifiers may call us when
2548 BASE's property isn't set right; we can't assume that just
2549 because we found something in the cache it must have its kind set
2550 right. */
2551 if (NILP (Fget (new_symbol, Qevent_kind)))
2553 Lisp_Object kind = Fget (base, Qevent_kind);
2555 if (! NILP (kind))
2556 Fput (new_symbol, Qevent_kind, kind);
2559 return new_symbol;
2563 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
2564 return a symbol with the modifiers placed in the canonical order.
2565 Canonical order is alphabetical, except for down and drag, which
2566 always come last. The 'click' modifier is never written out.
2568 Fdefine_key calls this to make sure that (for example) C-M-foo
2569 and M-C-foo end up being equivalent in the keymap. */
2571 Lisp_Object
2572 reorder_modifiers (symbol)
2573 Lisp_Object symbol;
2575 /* It's hopefully okay to write the code this way, since everything
2576 will soon be in caches, and no consing will be done at all. */
2577 Lisp_Object parsed = parse_modifiers (symbol);
2579 return apply_modifiers (XCONS (XCONS (parsed)->cdr)->car,
2580 XCONS (parsed)->car);
2584 /* For handling events, we often want to produce a symbol whose name
2585 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
2586 to some base, like the name of a function key or mouse button.
2587 modify_event_symbol produces symbols of this sort.
2589 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
2590 is the name of the i'th symbol. TABLE_SIZE is the number of elements
2591 in the table.
2593 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
2594 persist between calls to modify_event_symbol that it can use to
2595 store a cache of the symbols it's generated for this NAME_TABLE
2596 before.
2598 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
2600 MODIFIERS is a set of modifier bits (as given in struct input_events)
2601 whose prefixes should be applied to the symbol name.
2603 SYMBOL_KIND is the value to be placed in the event_kind property of
2604 the returned symbol.
2606 The symbols we create are supposed to have an
2607 `event-symbol-elements' propery, which lists the modifiers present
2608 in the symbol's name. */
2610 static Lisp_Object
2611 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_table,
2612 symbol_table, table_size)
2613 int symbol_num;
2614 unsigned modifiers;
2615 Lisp_Object symbol_kind;
2616 char **name_table;
2617 Lisp_Object *symbol_table;
2618 int table_size;
2620 Lisp_Object *slot;
2622 /* Is this a request for a valid symbol? */
2623 if (symbol_num < 0 || symbol_num >= table_size)
2624 abort ();
2626 /* If *symbol_table doesn't seem to be initialized properly, fix that.
2627 *symbol_table should be a lisp vector TABLE_SIZE elements long,
2628 where the Nth element is the symbol for NAME_TABLE[N], or nil if
2629 we've never used that symbol before. */
2630 if (XTYPE (*symbol_table) != Lisp_Vector
2631 || XVECTOR (*symbol_table)->size != table_size)
2633 Lisp_Object size;
2635 XFASTINT (size) = table_size;
2636 *symbol_table = Fmake_vector (size, Qnil);
2639 slot = & XVECTOR (*symbol_table)->contents[symbol_num];
2641 /* Have we already used this symbol before? */
2642 if (NILP (*slot))
2644 /* No; let's create it. */
2645 *slot = intern (name_table[symbol_num]);
2647 /* Fill in the cache entries for this symbol; this also
2648 builds the Qevent_symbol_elements property, which the user
2649 cares about. */
2650 apply_modifiers (modifiers & click_modifier, *slot);
2651 Fput (*slot, Qevent_kind, symbol_kind);
2654 /* Apply modifiers to that symbol. */
2655 return apply_modifiers (modifiers, *slot);
2659 /* Store into *addr a value nonzero if terminal input chars are available.
2660 Serves the purpose of ioctl (0, FIONREAD, addr)
2661 but works even if FIONREAD does not exist.
2662 (In fact, this may actually read some input.) */
2664 static void
2665 get_input_pending (addr)
2666 int *addr;
2668 /* First of all, have we already counted some input? */
2669 *addr = !NILP (Vquit_flag) || readable_events ();
2671 /* If input is being read as it arrives, and we have none, there is none. */
2672 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
2673 return;
2675 /* Try to read some input and see how much we get. */
2676 gobble_input (0);
2677 *addr = !NILP (Vquit_flag) || readable_events ();
2680 /* Interface to read_avail_input, blocking SIGIO if necessary. */
2683 gobble_input (expected)
2684 int expected;
2686 #ifndef VMS
2687 #ifdef SIGIO
2688 if (interrupt_input)
2690 SIGMASKTYPE mask;
2691 mask = sigblockx (SIGIO);
2692 read_avail_input (expected);
2693 sigsetmask (mask);
2695 else
2696 #endif
2697 read_avail_input (expected);
2698 #endif
2701 #ifndef VMS
2703 /* Read any terminal input already buffered up by the system
2704 into the kbd_buffer, but do not wait.
2706 EXPECTED should be nonzero if the caller knows there is some input.
2708 Except on VMS, all input is read by this function.
2709 If interrupt_input is nonzero, this function MUST be called
2710 only when SIGIO is blocked.
2712 Returns the number of keyboard chars read, or -1 meaning
2713 this is a bad time to try to read input. */
2715 static int
2716 read_avail_input (expected)
2717 int expected;
2719 struct input_event buf[KBD_BUFFER_SIZE];
2720 register int i;
2721 int nread;
2723 if (read_socket_hook)
2724 /* No need for FIONREAD or fcntl; just say don't wait. */
2725 nread = (*read_socket_hook) (0, buf, KBD_BUFFER_SIZE, expected, expected);
2726 else
2728 unsigned char cbuf[KBD_BUFFER_SIZE];
2730 #ifdef FIONREAD
2731 /* Find out how much input is available. */
2732 if (ioctl (0, FIONREAD, &nread) < 0)
2733 /* Formerly simply reported no input, but that sometimes led to
2734 a failure of Emacs to terminate.
2735 SIGHUP seems appropriate if we can't reach the terminal. */
2736 /* ??? Is it really right to send the signal just to this process
2737 rather than to the whole process group?
2738 Perhaps on systems with FIONREAD Emacs is alone in its group. */
2739 kill (getpid (), SIGHUP);
2740 if (nread == 0)
2741 return 0;
2742 if (nread > sizeof cbuf)
2743 nread = sizeof cbuf;
2744 #else /* no FIONREAD */
2745 #ifdef USG
2746 /* Read some input if available, but don't wait. */
2747 nread = sizeof cbuf;
2748 fcntl (fileno (stdin), F_SETFL, O_NDELAY);
2749 #else
2750 you lose;
2751 #endif
2752 #endif
2754 /* Now read; for one reason or another, this will not block. */
2755 while (1)
2757 nread = read (fileno (stdin), cbuf, nread);
2758 #ifdef AIX
2759 /* The kernel sometimes fails to deliver SIGHUP for ptys.
2760 This looks incorrect, but it isn't, because _BSD causes
2761 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
2762 and that causes a value other than 0 when there is no input. */
2763 if (nread == 0)
2764 kill (SIGHUP, 0);
2765 #endif
2766 /* Retry the read if it is interrupted. */
2767 if (nread >= 0
2768 || ! (errno == EAGAIN || errno == EFAULT
2769 #ifdef EBADSLT
2770 || errno == EBADSLT
2771 #endif
2773 break;
2776 #ifndef FIONREAD
2777 #ifdef USG
2778 fcntl (fileno (stdin), F_SETFL, 0);
2779 #endif /* USG */
2780 #endif /* no FIONREAD */
2781 for (i = 0; i < nread; i++)
2783 buf[i].kind = ascii_keystroke;
2784 buf[i].modifiers = 0;
2785 if (meta_key && (cbuf[i] & 0x80))
2786 buf[i].modifiers = meta_modifier;
2787 cbuf[i] &= ~0x80;
2789 XSET (buf[i].code, Lisp_Int, cbuf[i]);
2790 #ifdef MULTI_FRAME
2791 XSET (buf[i].frame_or_window, Lisp_Frame, selected_frame);
2792 #else
2793 buf[i].frame_or_window = Qnil;
2794 #endif
2798 /* Scan the chars for C-g and store them in kbd_buffer. */
2799 for (i = 0; i < nread; i++)
2801 kbd_buffer_store_event (&buf[i]);
2802 /* Don't look at input that follows a C-g too closely.
2803 This reduces lossage due to autorepeat on C-g. */
2804 if (buf[i].kind == ascii_keystroke
2805 && XINT(buf[i].code) == quit_char)
2806 break;
2809 return nread;
2811 #endif /* not VMS */
2813 #ifdef SIGIO /* for entire page */
2814 /* Note SIGIO has been undef'd if FIONREAD is missing. */
2816 SIGTYPE
2817 input_available_signal (signo)
2818 int signo;
2820 /* Must preserve main program's value of errno. */
2821 int old_errno = errno;
2822 #ifdef BSD4_1
2823 extern int select_alarmed;
2824 #endif
2826 #ifdef USG
2827 /* USG systems forget handlers when they are used;
2828 must reestablish each time */
2829 signal (signo, input_available_signal);
2830 #endif /* USG */
2832 #ifdef BSD4_1
2833 sigisheld (SIGIO);
2834 #endif
2836 if (input_available_clear_time)
2837 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
2839 while (1)
2841 int nread;
2842 nread = read_avail_input (1);
2843 /* -1 means it's not ok to read the input now.
2844 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
2845 0 means there was no keyboard input available. */
2846 if (nread <= 0)
2847 break;
2849 #ifdef BSD4_1
2850 select_alarmed = 1; /* Force the select emulator back to life */
2851 #endif
2854 #ifdef BSD4_1
2855 sigfree ();
2856 #endif
2857 errno = old_errno;
2859 #endif /* SIGIO */
2861 /* Return the prompt-string of a sparse keymap.
2862 This is the first element which is a string.
2863 Return nil if there is none. */
2865 Lisp_Object
2866 map_prompt (map)
2867 Lisp_Object map;
2869 while (CONSP (map))
2871 register Lisp_Object tem;
2872 tem = Fcar (map);
2873 if (XTYPE (tem) == Lisp_String)
2874 return tem;
2875 map = Fcdr (map);
2877 return Qnil;
2880 static Lisp_Object menu_bar_item ();
2881 static Lisp_Object menu_bar_one_keymap ();
2883 /* Return a list of menu items for a menu bar, appropriate
2884 to the current buffer.
2885 The elements have the form (KEY STRING . nil). */
2887 Lisp_Object
2888 menu_bar_items ()
2890 /* The number of keymaps we're scanning right now, and the number of
2891 keymaps we have allocated space for. */
2892 int nmaps;
2894 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
2895 in the current keymaps, or nil where it is not a prefix. */
2896 Lisp_Object *maps;
2898 Lisp_Object def, tem;
2900 Lisp_Object result;
2902 int mapno;
2904 /* Build our list of keymaps.
2905 If we recognize a function key and replace its escape sequence in
2906 keybuf with its symbol, or if the sequence starts with a mouse
2907 click and we need to switch buffers, we jump back here to rebuild
2908 the initial keymaps from the current buffer. */
2910 Lisp_Object *tmaps;
2912 nmaps = current_minor_maps (0, &tmaps) + 2;
2913 maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
2914 bcopy (tmaps, maps, (nmaps - 2) * sizeof (maps[0]));
2915 #ifdef USE_TEXT_PROPERTIES
2916 maps[nmaps-2] = get_local_map (PT, current_buffer);
2917 #else
2918 maps[nmaps-2] = current_buffer->local_map;
2919 #endif
2920 maps[nmaps-1] = global_map;
2923 /* Look up in each map the dummy prefix key `menu-bar'. */
2925 result = Qnil;
2927 for (mapno = 0; mapno < nmaps; mapno++)
2929 if (! NILP (maps[mapno]))
2930 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1));
2931 else
2932 def = Qnil;
2934 tem = Fkeymapp (def);
2935 if (!NILP (tem))
2936 result = menu_bar_one_keymap (def, result);
2939 return Fnreverse (result);
2942 /* Scan one map KEYMAP, accumulating any menu items it defines
2943 that have not yet been seen in RESULT. Return the updated RESULT. */
2945 static Lisp_Object
2946 menu_bar_one_keymap (keymap, result)
2947 Lisp_Object keymap, result;
2949 Lisp_Object tail, item, key, binding, item_string, table;
2951 /* Loop over all keymap entries that have menu strings. */
2952 for (tail = keymap; XTYPE (tail) == Lisp_Cons; tail = XCONS (tail)->cdr)
2954 item = XCONS (tail)->car;
2955 if (XTYPE (item) == Lisp_Cons)
2957 key = XCONS (item)->car;
2958 binding = XCONS (item)->cdr;
2959 if (XTYPE (binding) == Lisp_Cons)
2961 item_string = XCONS (binding)->car;
2962 if (XTYPE (item_string) == Lisp_String)
2963 result = menu_bar_item (key, item_string,
2964 Fcdr (binding), result);
2967 else if (XTYPE (item) == Lisp_Vector)
2969 /* Loop over the char values represented in the vector. */
2970 int len = XVECTOR (item)->size;
2971 int c;
2972 for (c = 0; c < len; c++)
2974 Lisp_Object character;
2975 XFASTINT (character) = c;
2976 binding = XVECTOR (item)->contents[c];
2977 if (XTYPE (binding) == Lisp_Cons)
2979 item_string = XCONS (binding)->car;
2980 if (XTYPE (item_string) == Lisp_String)
2981 result = menu_bar_item (key, item_string,
2982 Fcdr (binding), result);
2988 return result;
2991 static Lisp_Object
2992 menu_bar_item (key, item_string, def, result)
2993 Lisp_Object key, item_string, def, result;
2995 Lisp_Object tem, elt;
2996 Lisp_Object enabled;
2998 /* See if this entry is enabled. */
2999 enabled = Qt;
3001 if (XTYPE (def) == Lisp_Symbol)
3003 /* No property, or nil, means enable.
3004 Otherwise, enable if value is not nil. */
3005 tem = Fget (def, Qmenu_enable);
3006 if (!NILP (tem))
3007 enabled = Feval (tem);
3010 /* Add an entry for this key and string
3011 if there is none yet. */
3012 elt = Fassq (key, result);
3013 if (!NILP (enabled) && NILP (elt))
3014 result = Fcons (Fcons (key, Fcons (item_string, Qnil)), result);
3016 return result;
3019 static int echo_flag;
3020 static int echo_now;
3022 /* Read a character like read_char but optionally prompt based on maps
3023 in the array MAPS. NMAPS is the length of MAPS. Return nil if we
3024 decided not to read a character, because there are no menu items in
3025 MAPS.
3027 PREV_EVENT is the previous input event, or nil if we are reading
3028 the first event of a key sequence.
3030 If USED_MOUSE_MENU is non-zero, then we set *USED_MOUSE_MENU to 1
3031 if we used a mouse menu to read the input, or zero otherwise. If
3032 USED_MOUSE_MENU is zero, *USED_MOUSE_MENU is left alone.
3034 The prompting is done based on the prompt-string of the map
3035 and the strings associated with various map elements. */
3037 Lisp_Object
3038 read_char_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
3039 int nmaps;
3040 Lisp_Object *maps;
3041 Lisp_Object prev_event;
3042 int *used_mouse_menu;
3044 int mapno;
3045 register Lisp_Object name;
3046 int nlength;
3047 int width = FRAME_WIDTH (selected_frame) - 4;
3048 char *menu = (char *) alloca (width + 4);
3049 int idx = -1;
3050 Lisp_Object rest, vector;
3052 if (used_mouse_menu)
3053 *used_mouse_menu = 0;
3055 /* Use local over global Menu maps */
3057 if (! menu_prompting)
3058 return Qnil;
3060 /* Get the menu name from the first map that has one (a prompt string). */
3061 for (mapno = 0; mapno < nmaps; mapno++)
3063 name = map_prompt (maps[mapno]);
3064 if (!NILP (name))
3065 break;
3068 /* If we don't have any menus, just read a character normally. */
3069 if (mapno >= nmaps)
3070 return Qnil;
3072 #ifdef HAVE_X_WINDOWS
3073 #ifdef HAVE_X_MENU
3074 /* If we got to this point via a mouse click,
3075 use a real menu for mouse selection. */
3076 if (EVENT_HAS_PARAMETERS (prev_event))
3078 /* Display the menu and get the selection. */
3079 Lisp_Object *realmaps
3080 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
3081 Lisp_Object value;
3082 int nmaps1 = 0;
3084 /* Use the maps that are not nil. */
3085 for (mapno = 0; mapno < nmaps; mapno++)
3086 if (!NILP (maps[mapno]))
3087 realmaps[nmaps1++] = maps[mapno];
3089 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
3090 if (NILP (value))
3091 XSET (value, Lisp_Int, quit_char);
3092 if (used_mouse_menu)
3093 *used_mouse_menu = 1;
3094 return value;
3096 #endif /* HAVE_X_MENU */
3097 #endif /* HAVE_X_WINDOWS */
3099 /* Prompt string always starts with map's prompt, and a space. */
3100 strcpy (menu, XSTRING (name)->data);
3101 nlength = XSTRING (name)->size;
3102 menu[nlength++] = ':';
3103 menu[nlength++] = ' ';
3104 menu[nlength] = 0;
3106 /* Start prompting at start of first map. */
3107 mapno = 0;
3108 rest = maps[mapno];
3110 /* Present the documented bindings, a line at a time. */
3111 while (1)
3113 int notfirst = 0;
3114 int i = nlength;
3115 Lisp_Object obj;
3116 int ch;
3118 /* Loop over elements of map. */
3119 while (i < width)
3121 Lisp_Object s, elt;
3123 /* If reached end of map, start at beginning of next map. */
3124 if (NILP (rest))
3126 mapno++;
3127 /* At end of last map, wrap around to first map if just starting,
3128 or end this line if already have something on it. */
3129 if (mapno == nmaps)
3131 if (notfirst)
3132 break;
3133 else
3134 mapno = 0;
3136 rest = maps[mapno];
3139 /* Look at the next element of the map. */
3140 if (idx >= 0)
3141 elt = XVECTOR (vector)->contents[idx];
3142 else
3143 elt = Fcar_safe (rest);
3145 if (idx < 0 && XTYPE (elt) == Lisp_Vector)
3147 /* If we found a dense table in the keymap,
3148 advanced past it, but start scanning its contents. */
3149 rest = Fcdr_safe (rest);
3150 vector = elt;
3151 idx = 0;
3153 else
3155 /* An ordinary element. */
3156 s = Fcar_safe (Fcdr_safe (elt));
3157 if (XTYPE (s) != Lisp_String)
3158 /* Ignore the element if it has no prompt string. */
3160 /* If we have room for the prompt string, add it to this line.
3161 If this is the first on the line, always add it. */
3162 else if (XSTRING (s)->size + i < width
3163 || !notfirst)
3165 int thiswidth;
3167 /* Punctuate between strings. */
3168 if (notfirst)
3170 strcpy (menu + i, ", ");
3171 i += 2;
3173 notfirst = 1;
3175 /* Add as much of string as fits. */
3176 thiswidth = XSTRING (s)->size;
3177 if (thiswidth + i > width)
3178 thiswidth = width - i;
3179 bcopy (XSTRING (s)->data, menu + i, thiswidth);
3180 i += thiswidth;
3182 else
3184 /* If this element does not fit, end the line now,
3185 and save the element for the next line. */
3186 strcpy (menu + i, "...");
3187 break;
3190 /* Move past this element. */
3191 if (idx >= 0 && idx + 1 >= XVECTOR (rest)->size)
3192 /* Handle reaching end of dense table. */
3193 idx = -1;
3194 if (idx >= 0)
3195 idx++;
3196 else
3197 rest = Fcdr_safe (rest);
3201 /* Prompt with that and read response. */
3202 message1 (menu);
3203 obj = read_char (1, 0, 0, Qnil, 0);
3205 if (XTYPE (obj) != Lisp_Int)
3206 return obj;
3207 else
3208 ch = XINT (obj);
3210 if (! EQ (obj, menu_prompt_more_char)
3211 && (XTYPE (menu_prompt_more_char) != Lisp_Int
3212 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
3213 return obj;
3217 /* Reading key sequences. */
3219 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
3220 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
3221 keymap, or nil otherwise. Return the index of the first keymap in
3222 which KEY has any binding, or NMAPS if no map has a binding.
3224 If KEY is a meta ASCII character, treat it like meta-prefix-char
3225 followed by the corresponding non-meta character. Keymaps in
3226 CURRENT with non-prefix bindings for meta-prefix-char become nil in
3227 NEXT.
3229 When KEY is not defined in any of the keymaps, if it is an upper
3230 case letter and there are bindings for the corresponding lower-case
3231 letter, return the bindings for the lower-case letter.
3233 If KEY has no bindings in any of the CURRENT maps, NEXT is left
3234 unmodified.
3236 NEXT may == CURRENT. */
3238 static int
3239 follow_key (key, nmaps, current, defs, next)
3240 Lisp_Object key;
3241 Lisp_Object *current, *defs, *next;
3242 int nmaps;
3244 int i, first_binding;
3246 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
3247 followed by the corresponding non-meta character. */
3248 if (XTYPE (key) == Lisp_Int && (XINT (key) & CHAR_META))
3250 for (i = 0; i < nmaps; i++)
3251 if (! NILP (current[i]))
3253 next[i] =
3254 get_keyelt (access_keymap (current[i], meta_prefix_char, 1));
3256 /* Note that since we pass the resulting bindings through
3257 get_keymap_1, non-prefix bindings for meta-prefix-char
3258 disappear. */
3259 next[i] = get_keymap_1 (next[i], 0, 1);
3261 else
3262 next[i] = Qnil;
3264 current = next;
3265 XSET (key, Lisp_Int, XFASTINT (key) & ~CHAR_META);
3268 first_binding = nmaps;
3269 for (i = nmaps - 1; i >= 0; i--)
3271 if (! NILP (current[i]))
3273 defs[i] = get_keyelt (access_keymap (current[i], key, 1));
3274 if (! NILP (defs[i]))
3275 first_binding = i;
3277 else
3278 defs[i] = Qnil;
3281 /* When KEY is not defined in any of the keymaps, if it is an upper
3282 case letter and there are bindings for the corresponding
3283 lower-case letter, return the bindings for the lower-case letter. */
3284 if (first_binding == nmaps
3285 && XTYPE (key) == Lisp_Int
3286 && (UPPERCASEP (XINT (key) & 0x3ffff)
3287 || (XINT (key) & shift_modifier)))
3289 if (XINT (key) & shift_modifier)
3290 XSETINT (key, XINT (key) & ~shift_modifier);
3291 else
3292 XSETINT (key, DOWNCASE (XINT (key)));
3294 first_binding = nmaps;
3295 for (i = nmaps - 1; i >= 0; i--)
3297 if (! NILP (current[i]))
3299 defs[i] = get_keyelt (access_keymap (current[i], key, 1));
3300 if (! NILP (defs[i]))
3301 first_binding = i;
3303 else
3304 defs[i] = Qnil;
3308 /* Given the set of bindings we've found, produce the next set of maps. */
3309 if (first_binding < nmaps)
3310 for (i = 0; i < nmaps; i++)
3311 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
3313 return first_binding;
3316 /* Read a sequence of keys that ends with a non prefix character,
3317 storing it in KEYBUF, a buffer of size BUFSIZE.
3318 Prompt with PROMPT.
3319 Return the length of the key sequence stored.
3321 Echo starting immediately unless `prompt' is 0.
3323 Where a key sequence ends depends on the currently active keymaps.
3324 These include any minor mode keymaps active in the current buffer,
3325 the current buffer's local map, and the global map.
3327 If a key sequence has no other bindings, we check Vfunction_key_map
3328 to see if some trailing subsequence might be the beginning of a
3329 function key's sequence. If so, we try to read the whole function
3330 key, and substitute its symbolic name into the key sequence.
3332 We ignore unbound `down-' mouse clicks. We turn unbound `drag-'
3333 events into similar click events, if that would make them bound.
3335 If we get a mouse click in a mode line, vertical divider, or other
3336 non-text area, we treat the click as if it were prefixed by the
3337 symbol denoting that area - `mode-line', `vertical-line', or
3338 whatever.
3340 If the sequence starts with a mouse click, we read the key sequence
3341 with respect to the buffer clicked on, not the current buffer.
3343 If the user switches frames in the midst of a key sequence, we put
3344 off the switch-frame event until later; the next call to
3345 read_char will return it. */
3347 static int
3348 read_key_sequence (keybuf, bufsize, prompt)
3349 Lisp_Object *keybuf;
3350 int bufsize;
3351 char *prompt;
3353 int count = specpdl_ptr - specpdl;
3355 /* How many keys there are in the current key sequence. */
3356 int t;
3358 /* The length of the echo buffer when we started reading, and
3359 the length of this_command_keys when we started reading. */
3360 int echo_start;
3361 int keys_start;
3363 /* The number of keymaps we're scanning right now, and the number of
3364 keymaps we have allocated space for. */
3365 int nmaps;
3366 int nmaps_allocated = 0;
3368 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
3369 the current keymaps. */
3370 Lisp_Object *defs;
3372 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
3373 in the current keymaps, or nil where it is not a prefix. */
3374 Lisp_Object *submaps;
3376 /* The index in defs[] of the first keymap that has a binding for
3377 this key sequence. In other words, the lowest i such that
3378 defs[i] is non-nil. */
3379 int first_binding;
3381 /* If t < mock_input, then KEYBUF[t] should be read as the next
3382 input key.
3384 We use this to recover after recognizing a function key. Once we
3385 realize that a suffix of the current key sequence is actually a
3386 function key's escape sequence, we replace the suffix with the
3387 function key's binding from Vfunction_key_map. Now keybuf
3388 contains a new and different key sequence, so the echo area,
3389 this_command_keys, and the submaps and defs arrays are wrong. In
3390 this situation, we set mock_input to t, set t to 0, and jump to
3391 restart_sequence; the loop will read keys from keybuf up until
3392 mock_input, thus rebuilding the state; and then it will resume
3393 reading characters from the keyboard. */
3394 int mock_input = 0;
3396 /* If the sequence is unbound in submaps[], then
3397 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
3398 and fkey_map is its binding.
3400 These might be > t, indicating that all function key scanning
3401 should hold off until t reaches them. We do this when we've just
3402 recognized a function key, to avoid searching for the function
3403 key's again in Vfunction_key_map. */
3404 int fkey_start = 0, fkey_end = 0;
3405 Lisp_Object fkey_map = Vfunction_key_map;
3407 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
3408 we put it off for later. While we're reading, we keep the event here. */
3409 Lisp_Object delayed_switch_frame = Qnil;
3412 /* If there is no function key map, turn off function key scanning. */
3413 if (NILP (Fkeymapp (Vfunction_key_map)))
3414 fkey_start = fkey_end = bufsize + 1;
3416 /* We need to save the current buffer in case we switch buffers to
3417 find the right binding for a mouse click. Note that we can't use
3418 save_excursion_{save,restore} here, because they save point as
3419 well as the current buffer; we don't want to save point, because
3420 redisplay may change it, to accomodate a Fset_window_start or
3421 something. */
3422 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3424 last_nonmenu_event = Qnil;
3426 if (INTERACTIVE)
3428 if (prompt)
3429 echo_prompt (prompt);
3430 else if (cursor_in_echo_area)
3431 /* This doesn't put in a dash if the echo buffer is empty, so
3432 you don't always see a dash hanging out in the minibuffer. */
3433 echo_dash ();
3436 /* Record the initial state of the echo area and this_command_keys;
3437 we will need to restore them if we replay a key sequence. */
3438 if (INTERACTIVE)
3439 echo_start = echo_length ();
3440 keys_start = this_command_key_count;
3442 /* We jump here when the key sequence has been thoroughly changed, and
3443 we need to rescan it starting from the beginning. When we jump here,
3444 keybuf[0..mock_input] holds the sequence we should reread. */
3445 replay_sequence:
3447 /* Build our list of keymaps.
3448 If we recognize a function key and replace its escape sequence in
3449 keybuf with its symbol, or if the sequence starts with a mouse
3450 click and we need to switch buffers, we jump back here to rebuild
3451 the initial keymaps from the current buffer. */
3453 Lisp_Object *maps;
3455 nmaps = current_minor_maps (0, &maps) + 2;
3456 if (nmaps > nmaps_allocated)
3458 submaps = (Lisp_Object *) alloca (nmaps * sizeof (submaps[0]));
3459 defs = (Lisp_Object *) alloca (nmaps * sizeof (defs[0]));
3460 nmaps_allocated = nmaps;
3462 bcopy (maps, submaps, (nmaps - 2) * sizeof (submaps[0]));
3463 #ifdef USE_TEXT_PROPERTIES
3464 submaps[nmaps-2] = get_local_map (PT, current_buffer);
3465 #else
3466 submaps[nmaps-2] = current_buffer->local_map;
3467 #endif
3468 submaps[nmaps-1] = global_map;
3471 /* Find an accurate initial value for first_binding. */
3472 for (first_binding = 0; first_binding < nmaps; first_binding++)
3473 if (! NILP (submaps[first_binding]))
3474 break;
3476 /* We jump here when a function key substitution has forced us to
3477 reprocess the current key sequence. keybuf[0..mock_input] is the
3478 sequence we want to reread. */
3479 t = 0;
3481 /* These are no-ops the first time through, but if we restart, they
3482 revert the echo area and this_command_keys to their original state. */
3483 this_command_key_count = keys_start;
3484 if (INTERACTIVE)
3485 echo_truncate (echo_start);
3487 /* If the best binding for the current key sequence is a keymap,
3488 or we may be looking at a function key's escape sequence, keep
3489 on reading. */
3490 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
3491 || (first_binding >= nmaps && fkey_start < t))
3493 Lisp_Object key;
3494 int used_mouse_menu = 0;
3496 /* Where the last real key started. If we need to throw away a
3497 key that has expanded into more than one element of keybuf
3498 (say, a mouse click on the mode line which is being treated
3499 as [mode-line (mouse-...)], then we backtrack to this point
3500 of keybuf. */
3501 int last_real_key_start;
3503 /* These variables are analogous to echo_start and keys_start;
3504 while those allow us to restart the entire key sequence,
3505 echo_local_start and keys_local_start allow us to throw away
3506 just one key. */
3507 int echo_local_start, keys_local_start, local_first_binding;
3509 if (t >= bufsize)
3510 error ("key sequence too long");
3512 if (INTERACTIVE)
3513 echo_local_start = echo_length ();
3514 keys_local_start = this_command_key_count;
3515 local_first_binding = first_binding;
3517 replay_key:
3518 /* These are no-ops, unless we throw away a keystroke below and
3519 jumped back up to replay_key; in that case, these restore the
3520 variables to their original state, allowing us to replay the
3521 loop. */
3522 if (INTERACTIVE)
3523 echo_truncate (echo_local_start);
3524 this_command_key_count = keys_local_start;
3525 first_binding = local_first_binding;
3527 /* Does mock_input indicate that we are re-reading a key sequence? */
3528 if (t < mock_input)
3530 key = keybuf[t];
3531 add_command_key (key);
3532 echo_char (key);
3535 /* If not, we should actually read a character. */
3536 else
3538 last_real_key_start = t;
3540 key = read_char (!prompt, nmaps, submaps, last_nonmenu_event,
3541 &used_mouse_menu);
3543 /* read_char returns -1 at the end of a macro.
3544 Emacs 18 handles this by returning immediately with a
3545 zero, so that's what we'll do. */
3546 if (XTYPE (key) == Lisp_Int && XINT (key) == -1)
3548 t = 0;
3549 goto done;
3552 Vquit_flag = Qnil;
3554 /* Clicks in non-text areas get prefixed by the symbol
3555 in their CHAR-ADDRESS field. For example, a click on
3556 the mode line is prefixed by the symbol `mode-line'.
3558 Furthermore, key sequences beginning with mouse clicks
3559 are read using the keymaps of the buffer clicked on, not
3560 the current buffer. So we may have to switch the buffer
3561 here. */
3562 if (EVENT_HAS_PARAMETERS (key))
3564 Lisp_Object kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
3566 if (EQ (kind, Qmouse_click))
3568 Lisp_Object window = POSN_WINDOW (EVENT_START (key));
3569 Lisp_Object posn = POSN_BUFFER_POSN (EVENT_START (key));
3571 /* Key sequences beginning with mouse clicks are
3572 read using the keymaps in the buffer clicked on,
3573 not the current buffer. If we're at the
3574 beginning of a key sequence, switch buffers. */
3575 if (t == 0
3576 && XTYPE (window) == Lisp_Window
3577 && XTYPE (XWINDOW (window)->buffer) == Lisp_Buffer
3578 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
3580 if (XTYPE (posn) == Lisp_Symbol)
3582 if (t + 1 >= bufsize)
3583 error ("key sequence too long");
3584 keybuf[t] = posn;
3585 keybuf[t+1] = key;
3586 mock_input = t + 2;
3588 else
3590 keybuf[t] = key;
3591 mock_input = t + 1;
3594 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
3595 goto replay_sequence;
3597 else if (XTYPE (posn) == Lisp_Symbol)
3599 if (t + 1 >= bufsize)
3600 error ("key sequence too long");
3601 keybuf[t] = posn;
3602 keybuf[t+1] = key;
3603 mock_input = t + 2;
3605 goto replay_key;
3608 else if (EQ (kind, Qswitch_frame))
3610 /* If we're at the beginning of a key sequence, go
3611 ahead and return this event. If we're in the
3612 midst of a key sequence, delay it until the end. */
3613 if (t > 0)
3615 delayed_switch_frame = key;
3616 goto replay_key;
3619 else
3621 Lisp_Object posn = POSN_BUFFER_POSN (EVENT_START (key));
3623 /* Handle menu-bar events:
3624 insert the dummy prefix char `menu-bar'. */
3625 if (EQ (posn, Qmenu_bar))
3627 if (t + 1 >= bufsize)
3628 error ("key sequence too long");
3629 /* Run the Lucid hook. */
3630 call1 (Vrun_hooks, Qactivate_menubar_hook);
3631 /* If it has changed current-menubar from previous value,
3632 really recompute the menubar from the value. */
3633 if (! NILP (Vlucid_menu_bar_dirty_flag))
3634 call0 (Qrecompute_lucid_menubar);
3635 keybuf[t] = posn;
3636 keybuf[t+1] = key;
3637 mock_input = t + 2;
3638 goto replay_sequence;
3644 /* We have finally decided that KEY is something we might want
3645 to look up. */
3646 first_binding = (follow_key (key,
3647 nmaps - first_binding,
3648 submaps + first_binding,
3649 defs + first_binding,
3650 submaps + first_binding)
3651 + first_binding);
3653 /* If KEY wasn't bound, we'll try some fallbacks. */
3654 if (first_binding >= nmaps)
3656 Lisp_Object head = EVENT_HEAD (key);
3658 if (XTYPE (head) == Lisp_Symbol)
3660 Lisp_Object breakdown = parse_modifiers (head);
3661 Lisp_Object modifiers =
3662 XINT (XCONS (XCONS (breakdown)->cdr)->car);
3664 /* We drop unbound `down-' events altogether. */
3665 if (modifiers & down_modifier)
3667 /* Dispose of this event by simply jumping back to
3668 replay_key, to get another event.
3670 Note that if this event came from mock input,
3671 then just jumping back to replay_key will just
3672 hand it to us again. So we have to wipe out any
3673 mock input.
3675 We could delete keybuf[t] and shift everything
3676 after that to the left by one spot, but we'd also
3677 have to fix up any variable that points into
3678 keybuf, and shifting isn't really necessary
3679 anyway.
3681 Adding prefixes for non-textual mouse clicks
3682 creates two characters of mock input, and both
3683 must be thrown away. If we're only looking at
3684 the prefix now, we can just jump back to
3685 replay_key. On the other hand, if we've already
3686 processed the prefix, and now the actual click
3687 itself is giving us trouble, then we've lost the
3688 state of the keymaps we want to backtrack to, and
3689 we need to replay the whole sequence to rebuild
3692 Beyond that, only function key expansion could
3693 create more than two keys, but that should never
3694 generate mouse events, so it's okay to zero
3695 mock_input in that case too.
3697 Isn't this just the most wonderful code ever? */
3698 if (t == last_real_key_start)
3700 mock_input = 0;
3701 goto replay_key;
3703 else
3705 mock_input = last_real_key_start;
3706 goto replay_sequence;
3710 /* We turn unbound `drag-' events into `click-'
3711 events, if the click would be bound. */
3712 else if (modifiers & drag_modifier)
3714 Lisp_Object new_head =
3715 apply_modifiers (modifiers & ~drag_modifier,
3716 XCONS (breakdown)->car);
3717 Lisp_Object new_click =
3718 Fcons (new_head, Fcons (EVENT_START (key), Qnil));
3720 /* Look for a binding for this new key. follow_key
3721 promises that it didn't munge submaps the
3722 last time we called it, since key was unbound. */
3723 first_binding =
3724 (follow_key (new_click,
3725 nmaps - local_first_binding,
3726 submaps + local_first_binding,
3727 defs + local_first_binding,
3728 submaps + local_first_binding)
3729 + local_first_binding);
3731 /* If that click is bound, go for it. */
3732 if (first_binding < nmaps)
3733 key = new_click;
3734 /* Otherwise, we'll leave key set to the drag event. */
3739 keybuf[t++] = key;
3740 /* Normally, last_nonmenu_event gets the previous key we read.
3741 But when a mouse popup menu is being used,
3742 we don't update last_nonmenu_event; it continues to hold the mouse
3743 event that preceded the first level of menu. */
3744 if (!used_mouse_menu)
3745 last_nonmenu_event = key;
3747 /* If the sequence is unbound, see if we can hang a function key
3748 off the end of it. We only want to scan real keyboard input
3749 for function key sequences, so if mock_input says that we're
3750 re-reading old events, don't examine it. */
3751 if (first_binding >= nmaps
3752 && t >= mock_input)
3754 Lisp_Object fkey_next;
3756 /* Scan from fkey_end until we find a bound suffix. */
3757 while (fkey_end < t)
3759 Lisp_Object key;
3761 key = keybuf[fkey_end++];
3762 /* Look up meta-characters by prefixing them
3763 with meta_prefix_char. I hate this. */
3764 if (XTYPE (key) == Lisp_Int && XINT (key) & meta_modifier)
3766 fkey_next =
3767 get_keymap_1
3768 (get_keyelt
3769 (access_keymap
3770 (fkey_map, meta_prefix_char, 1)),
3771 0, 1);
3772 XFASTINT (key) = XFASTINT (key) & ~meta_modifier;
3774 else
3775 fkey_next = fkey_map;
3777 fkey_next =
3778 get_keyelt (access_keymap (fkey_next, key, 1));
3780 /* If keybuf[fkey_start..fkey_end] is bound in the
3781 function key map and it's a suffix of the current
3782 sequence (i.e. fkey_end == t), replace it with
3783 the binding and restart with fkey_start at the end. */
3784 if (XTYPE (fkey_next) == Lisp_Vector
3785 && fkey_end == t)
3787 t = fkey_start + XVECTOR (fkey_next)->size;
3788 if (t >= bufsize)
3789 error ("key sequence too long");
3791 bcopy (XVECTOR (fkey_next)->contents,
3792 keybuf + fkey_start,
3793 (t - fkey_start) * sizeof (keybuf[0]));
3795 mock_input = t;
3796 fkey_start = fkey_end = t;
3798 goto replay_sequence;
3801 fkey_map = get_keymap_1 (fkey_next, 0, 1);
3803 /* If we no longer have a bound suffix, try a new positions for
3804 fkey_start. */
3805 if (NILP (fkey_map))
3807 fkey_end = ++fkey_start;
3808 fkey_map = Vfunction_key_map;
3814 read_key_sequence_cmd = (first_binding < nmaps
3815 ? defs[first_binding]
3816 : Qnil);
3818 done:
3819 unread_switch_frame = delayed_switch_frame;
3820 unbind_to (count, Qnil);
3821 return t;
3824 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 2, 0,
3825 "Read a sequence of keystrokes and return as a string or vector.\n\
3826 The sequence is sufficient to specify a non-prefix command in the\n\
3827 current local and global maps.\n\
3829 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
3830 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
3831 as a continuation of the previous key.\n\
3833 A C-g typed while in this function is treated like any other character,\n\
3834 and `quit-flag' is not set.\n\
3836 If the key sequence starts with a mouse click, then the sequence is read\n\
3837 using the keymaps of the buffer of the window clicked in, not the buffer\n\
3838 of the selected window as normal.\n\
3840 `read-key-sequence' drops unbound button-down events, since you normally\n\
3841 only care about the click or drag events which follow them. If a drag\n\
3842 event is unbound, but the corresponding click event would be bound,\n\
3843 `read-key-sequence' turns the drag event into a click event at the\n\
3844 drag's starting position. This means that you don't have to distinguish\n\
3845 between click and drag events unless you want to.\n\
3847 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
3848 lines separating windows, and scroll bars with imaginary keys\n\
3849 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
3851 If the user switches frames in the middle of a key sequence, the\n\
3852 frame-switch event is put off until after the current key sequence.\n\
3854 `read-key-sequence' checks `function-key-map' for function key\n\
3855 sequences, where they wouldn't conflict with ordinary bindings. See\n\
3856 `function-key-map' for more details.")
3857 (prompt, continue_echo)
3858 Lisp_Object prompt, continue_echo;
3860 Lisp_Object keybuf[30];
3861 register int i;
3862 struct gcpro gcpro1, gcpro2;
3864 if (!NILP (prompt))
3865 CHECK_STRING (prompt, 0);
3866 QUIT;
3868 bzero (keybuf, sizeof keybuf);
3869 GCPRO1 (keybuf[0]);
3870 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
3872 if (NILP (continue_echo))
3873 this_command_key_count = 0;
3875 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
3876 NILP (prompt) ? 0 : XSTRING (prompt)->data);
3878 UNGCPRO;
3879 return make_event_array (i, keybuf);
3882 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
3883 "Execute CMD as an editor command.\n\
3884 CMD must be a symbol that satisfies the `commandp' predicate.\n\
3885 Optional second arg RECORD-FLAG non-nil\n\
3886 means unconditionally put this command in `command-history'.\n\
3887 Otherwise, that is done only if an arg is read using the minibuffer.")
3888 (cmd, record)
3889 Lisp_Object cmd, record;
3891 register Lisp_Object final;
3892 register Lisp_Object tem;
3893 Lisp_Object prefixarg;
3894 struct backtrace backtrace;
3895 extern int debug_on_next_call;
3897 prefixarg = Vprefix_arg, Vprefix_arg = Qnil;
3898 Vcurrent_prefix_arg = prefixarg;
3899 debug_on_next_call = 0;
3901 if (XTYPE (cmd) == Lisp_Symbol)
3903 tem = Fget (cmd, Qdisabled);
3904 if (!NILP (tem))
3905 return call1 (Vrun_hooks, Vdisabled_command_hook);
3908 while (1)
3910 final = Findirect_function (cmd);
3912 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
3913 do_autoload (final, cmd);
3914 else
3915 break;
3918 if (XTYPE (final) == Lisp_String
3919 || XTYPE (final) == Lisp_Vector)
3921 /* If requested, place the macro in the command history. For
3922 other sorts of commands, call-interactively takes care of
3923 this. */
3924 if (!NILP (record))
3925 Vcommand_history
3926 = Fcons (Fcons (Qexecute_kbd_macro,
3927 Fcons (final, Fcons (prefixarg, Qnil))),
3928 Vcommand_history);
3930 return Fexecute_kbd_macro (final, prefixarg);
3932 if (CONSP (final) || XTYPE (final) == Lisp_Subr
3933 || XTYPE (final) == Lisp_Compiled)
3935 backtrace.next = backtrace_list;
3936 backtrace_list = &backtrace;
3937 backtrace.function = &Qcall_interactively;
3938 backtrace.args = &cmd;
3939 backtrace.nargs = 1;
3940 backtrace.evalargs = 0;
3942 tem = Fcall_interactively (cmd, record);
3944 backtrace_list = backtrace.next;
3945 return tem;
3947 return Qnil;
3950 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
3951 1, 1, "P",
3952 "Read function name, then read its arguments and call it.")
3953 (prefixarg)
3954 Lisp_Object prefixarg;
3956 Lisp_Object function;
3957 char buf[40];
3958 Lisp_Object saved_keys;
3959 struct gcpro gcpro1;
3961 saved_keys = Fthis_command_keys ();
3962 buf[0] = 0;
3963 GCPRO1 (saved_keys);
3965 if (EQ (prefixarg, Qminus))
3966 strcpy (buf, "- ");
3967 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
3968 strcpy (buf, "C-u ");
3969 else if (CONSP (prefixarg) && XTYPE (XCONS (prefixarg)->car) == Lisp_Int)
3970 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
3971 else if (XTYPE (prefixarg) == Lisp_Int)
3972 sprintf (buf, "%d ", XINT (prefixarg));
3974 /* This isn't strictly correct if execute-extended-command
3975 is bound to anything else. Perhaps it should use
3976 this_command_keys? */
3977 strcat (buf, "M-x ");
3979 /* Prompt with buf, and then read a string, completing from and
3980 restricting to the set of all defined commands. Don't provide
3981 any initial input. The last Qnil says not to perform a
3982 peculiar hack on the initial input. */
3983 function = Fcompleting_read (build_string (buf),
3984 Vobarray, Qcommandp,
3985 Qt, Qnil, Qnil);
3987 /* Set this_command_keys to the concatenation of saved_keys and
3988 function, followed by a RET. */
3990 struct Lisp_String *str;
3991 int i;
3992 Lisp_Object tem;
3994 this_command_key_count = 0;
3996 str = XSTRING (saved_keys);
3997 for (i = 0; i < str->size; i++)
3999 XFASTINT (tem) = str->data[i];
4000 add_command_key (tem);
4003 str = XSTRING (function);
4004 for (i = 0; i < str->size; i++)
4006 XFASTINT (tem) = str->data[i];
4007 add_command_key (tem);
4010 XFASTINT (tem) = '\015';
4011 add_command_key (tem);
4014 UNGCPRO;
4016 function = Fintern (function, Qnil);
4017 Vprefix_arg = prefixarg;
4018 this_command = function;
4020 return Fcommand_execute (function, Qt);
4024 detect_input_pending ()
4026 if (!input_pending)
4027 get_input_pending (&input_pending);
4029 return input_pending;
4032 /* This is called in some cases before a possible quit.
4033 It cases the next call to detect_input_pending to recompute input_pending.
4034 So calling this function unnecessarily can't do any harm. */
4035 clear_input_pending ()
4037 input_pending = 0;
4040 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
4041 "T if command input is currently available with no waiting.\n\
4042 Actually, the value is nil only if we can be sure that no input is available.")
4045 if (!NILP (unread_command_events) || unread_command_char != -1)
4046 return (Qt);
4048 return detect_input_pending () ? Qt : Qnil;
4051 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
4052 "Return vector of last 100 events, not counting those from keyboard macros.")
4055 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
4056 Lisp_Object val;
4058 if (total_keys < NUM_RECENT_KEYS)
4059 return Fvector (total_keys, keys);
4060 else
4062 val = Fvector (NUM_RECENT_KEYS, keys);
4063 bcopy (keys + recent_keys_index,
4064 XVECTOR (val)->contents,
4065 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
4066 bcopy (keys,
4067 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
4068 recent_keys_index * sizeof (Lisp_Object));
4069 return val;
4073 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
4074 "Return string of the keystrokes that invoked this command.")
4077 return make_event_array (this_command_key_count,
4078 XVECTOR (this_command_keys)->contents);
4081 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
4082 "Return the current depth in recursive edits.")
4085 Lisp_Object temp;
4086 XFASTINT (temp) = command_loop_level + minibuf_level;
4087 return temp;
4090 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
4091 "FOpen dribble file: ",
4092 "Start writing all keyboard characters to a dribble file called FILE.\n\
4093 If FILE is nil, close any open dribble file.")
4094 (file)
4095 Lisp_Object file;
4097 if (NILP (file))
4099 fclose (dribble);
4100 dribble = 0;
4102 else
4104 file = Fexpand_file_name (file, Qnil);
4105 dribble = fopen (XSTRING (file)->data, "w");
4107 return Qnil;
4110 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
4111 "Discard the contents of the terminal input buffer.\n\
4112 Also cancel any kbd macro being defined.")
4115 defining_kbd_macro = 0;
4116 update_mode_lines++;
4118 unread_command_events = Qnil;
4119 unread_command_char = -1;
4121 discard_tty_input ();
4123 /* Without the cast, GCC complains that this assignment loses the
4124 volatile qualifier of kbd_store_ptr. Is there anything wrong
4125 with that? */
4126 kbd_fetch_ptr = (struct input_event *) kbd_store_ptr;
4127 Ffillarray (kbd_buffer_frame_or_window, Qnil);
4128 input_pending = 0;
4130 return Qnil;
4133 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
4134 "Stop Emacs and return to superior process. You can resume later.\n\
4135 On systems that don't have job control, run a subshell instead.\n\n\
4136 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
4137 to be read as terminal input by Emacs's parent, after suspension.\n\
4139 Before suspending, call the functions in `suspend-hooks' with no args.\n\
4140 If any of them returns nil, don't call the rest and don't suspend.\n\
4141 Otherwise, suspend normally and after resumption run the normal hook\n\
4142 `suspend-resume-hook' if that is bound and non-nil.\n\
4144 Some operating systems cannot stop the Emacs process and resume it later.\n\
4145 On such systems, Emacs starts a subshell instead of suspending.")
4146 (stuffstring)
4147 Lisp_Object stuffstring;
4149 Lisp_Object tem;
4150 int count = specpdl_ptr - specpdl;
4151 int old_height, old_width;
4152 int width, height;
4153 struct gcpro gcpro1, gcpro2;
4154 extern init_sys_modes ();
4156 if (!NILP (stuffstring))
4157 CHECK_STRING (stuffstring, 0);
4159 /* Run the functions in suspend-hook. */
4160 if (!NILP (Vrun_hooks))
4161 call1 (Vrun_hooks, intern ("suspend-hook"));
4163 GCPRO1 (stuffstring);
4164 get_frame_size (&old_width, &old_height);
4165 reset_sys_modes ();
4166 /* sys_suspend can get an error if it tries to fork a subshell
4167 and the system resources aren't available for that. */
4168 record_unwind_protect (init_sys_modes, 0);
4169 stuff_buffered_input (stuffstring);
4170 sys_suspend ();
4171 unbind_to (count, Qnil);
4173 /* Check if terminal/window size has changed.
4174 Note that this is not useful when we are running directly
4175 with a window system; but suspend should be disabled in that case. */
4176 get_frame_size (&width, &height);
4177 if (width != old_width || height != old_height)
4178 change_frame_size (0, height, width, 0, 0);
4180 /* Run suspend-resume-hook. */
4181 if (!NILP (Vrun_hooks))
4182 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
4184 UNGCPRO;
4185 return Qnil;
4188 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
4189 Then in any case stuff anthing Emacs has read ahead and not used. */
4191 stuff_buffered_input (stuffstring)
4192 Lisp_Object stuffstring;
4194 register unsigned char *p;
4196 /* stuff_char works only in BSD, versions 4.2 and up. */
4197 #ifdef BSD
4198 #ifndef BSD4_1
4199 if (XTYPE (stuffstring) == Lisp_String)
4201 register int count;
4203 p = XSTRING (stuffstring)->data;
4204 count = XSTRING (stuffstring)->size;
4205 while (count-- > 0)
4206 stuff_char (*p++);
4207 stuff_char ('\n');
4209 /* Anything we have read ahead, put back for the shell to read. */
4210 while (kbd_fetch_ptr != kbd_store_ptr)
4212 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
4213 kbd_fetch_ptr = kbd_buffer;
4214 if (kbd_fetch_ptr->kind == ascii_keystroke)
4215 stuff_char (XINT (kbd_fetch_ptr->code));
4216 kbd_fetch_ptr->kind = no_event;
4217 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
4218 - kbd_buffer]
4219 = Qnil);
4220 kbd_fetch_ptr++;
4222 input_pending = 0;
4223 #endif
4224 #endif /* BSD and not BSD4_1 */
4227 set_waiting_for_input (time_to_clear)
4228 EMACS_TIME *time_to_clear;
4230 input_available_clear_time = time_to_clear;
4232 /* Tell interrupt_signal to throw back to read_char, */
4233 waiting_for_input = 1;
4235 /* If interrupt_signal was called before and buffered a C-g,
4236 make it run again now, to avoid timing error. */
4237 if (!NILP (Vquit_flag))
4238 quit_throw_to_read_char ();
4240 /* If alarm has gone off already, echo now. */
4241 if (echo_flag)
4243 echo ();
4244 echo_flag = 0;
4248 clear_waiting_for_input ()
4250 /* Tell interrupt_signal not to throw back to read_char, */
4251 waiting_for_input = 0;
4252 input_available_clear_time = 0;
4255 /* This routine is called at interrupt level in response to C-G.
4256 If interrupt_input, this is the handler for SIGINT.
4257 Otherwise, it is called from kbd_buffer_store_event,
4258 in handling SIGIO or SIGTINT.
4260 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
4261 immediately throw back to read_char.
4263 Otherwise it sets the Lisp variable quit-flag not-nil.
4264 This causes eval to throw, when it gets a chance.
4265 If quit-flag is already non-nil, it stops the job right away. */
4267 SIGTYPE
4268 interrupt_signal ()
4270 char c;
4271 /* Must preserve main program's value of errno. */
4272 int old_errno = errno;
4274 #ifdef USG
4275 /* USG systems forget handlers when they are used;
4276 must reestablish each time */
4277 signal (SIGINT, interrupt_signal);
4278 signal (SIGQUIT, interrupt_signal);
4279 #endif /* USG */
4281 cancel_echoing ();
4283 if (!NILP (Vquit_flag) && FRAME_TERMCAP_P (selected_frame))
4285 fflush (stdout);
4286 reset_sys_modes ();
4287 sigfree ();
4288 #ifdef SIGTSTP /* Support possible in later USG versions */
4290 * On systems which can suspend the current process and return to the original
4291 * shell, this command causes the user to end up back at the shell.
4292 * The "Auto-save" and "Abort" questions are not asked until
4293 * the user elects to return to emacs, at which point he can save the current
4294 * job and either dump core or continue.
4296 sys_suspend ();
4297 #else
4298 #ifdef VMS
4299 if (sys_suspend () == -1)
4301 printf ("Not running as a subprocess;\n");
4302 printf ("you can continue or abort.\n");
4304 #else /* not VMS */
4305 /* Perhaps should really fork an inferior shell?
4306 But that would not provide any way to get back
4307 to the original shell, ever. */
4308 printf ("No support for stopping a process on this operating system;\n");
4309 printf ("you can continue or abort.\n");
4310 #endif /* not VMS */
4311 #endif /* not SIGTSTP */
4312 printf ("Auto-save? (y or n) ");
4313 fflush (stdout);
4314 if (((c = getchar ()) & ~040) == 'Y')
4315 Fdo_auto_save (Qnil, Qnil);
4316 while (c != '\n') c = getchar ();
4317 #ifdef VMS
4318 printf ("Abort (and enter debugger)? (y or n) ");
4319 #else /* not VMS */
4320 printf ("Abort (and dump core)? (y or n) ");
4321 #endif /* not VMS */
4322 fflush (stdout);
4323 if (((c = getchar ()) & ~040) == 'Y')
4324 abort ();
4325 while (c != '\n') c = getchar ();
4326 printf ("Continuing...\n");
4327 fflush (stdout);
4328 init_sys_modes ();
4330 else
4332 /* If executing a function that wants to be interrupted out of
4333 and the user has not deferred quitting by binding `inhibit-quit'
4334 then quit right away. */
4335 if (immediate_quit && NILP (Vinhibit_quit))
4337 immediate_quit = 0;
4338 sigfree ();
4339 Fsignal (Qquit, Qnil);
4341 else
4342 /* Else request quit when it's safe */
4343 Vquit_flag = Qt;
4346 if (waiting_for_input && !echoing)
4347 quit_throw_to_read_char ();
4349 errno = old_errno;
4352 /* Handle a C-g by making read_char return C-g. */
4354 quit_throw_to_read_char ()
4356 quit_error_check ();
4357 sigfree ();
4358 /* Prevent another signal from doing this before we finish. */
4359 clear_waiting_for_input ();
4360 input_pending = 0;
4362 unread_command_events = Qnil;
4363 unread_command_char = -1;
4365 #ifdef POLL_FOR_INPUT
4366 /* May be > 1 if in recursive minibuffer. */
4367 if (poll_suppress_count == 0)
4368 abort ();
4369 #endif
4371 _longjmp (getcjmp, 1);
4374 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
4375 "Set mode of reading keyboard input.\n\
4376 First arg INTERRUPT non-nil means use input interrupts;\n\
4377 nil means use CBREAK mode.\n\
4378 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
4379 (no effect except in CBREAK mode).\n\
4380 Third arg META non-nil means accept 8-bit input (for a Meta key).\n\
4381 Otherwise, the top bit is ignored, on the assumption it is parity.\n\
4382 Optional fourth arg QUIT if non-nil specifies character to use for quitting.")
4383 (interrupt, flow, meta, quit)
4384 Lisp_Object interrupt, flow, meta, quit;
4386 if (!NILP (quit)
4387 && (XTYPE (quit) != Lisp_Int
4388 || XINT (quit) < 0 || XINT (quit) > 0400))
4389 error ("set-input-mode: QUIT must be an ASCII character.");
4391 reset_sys_modes ();
4392 #ifdef SIGIO
4393 /* Note SIGIO has been undef'd if FIONREAD is missing. */
4394 #ifdef NO_SOCK_SIGIO
4395 if (read_socket_hook)
4396 interrupt_input = 0; /* No interrupts if reading from a socket. */
4397 else
4398 #endif /* NO_SOCK_SIGIO */
4399 interrupt_input = !NILP (interrupt);
4400 #else /* not SIGIO */
4401 interrupt_input = 0;
4402 #endif /* not SIGIO */
4403 /* Our VMS input only works by interrupts, as of now. */
4404 #ifdef VMS
4405 interrupt_input = 1;
4406 #endif
4407 flow_control = !NILP (flow);
4408 meta_key = !NILP (meta);
4409 if (!NILP (quit))
4410 /* Don't let this value be out of range. */
4411 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
4413 init_sys_modes ();
4414 return Qnil;
4417 init_keyboard ()
4419 /* This is correct before outermost invocation of the editor loop */
4420 command_loop_level = -1;
4421 immediate_quit = 0;
4422 quit_char = Ctl ('g');
4423 unread_command_events = Qnil;
4424 unread_command_char = -1;
4425 total_keys = 0;
4426 recent_keys_index = 0;
4427 kbd_fetch_ptr = kbd_buffer;
4428 kbd_store_ptr = kbd_buffer;
4429 do_mouse_tracking = 0;
4430 input_pending = 0;
4432 #ifdef MULTI_FRAME
4433 /* This means that command_loop_1 won't try to select anything the first
4434 time through. */
4435 internal_last_event_frame = Qnil;
4436 Vlast_event_frame = internal_last_event_frame;
4437 #endif
4439 /* If we're running a dumped Emacs, we need to clear out
4440 kbd_buffer_frame_or_window, in case some events got into it
4441 before we dumped.
4443 If we're running an undumped Emacs, it hasn't been initialized by
4444 syms_of_keyboard yet. */
4445 if (initialized)
4446 Ffillarray (kbd_buffer_frame_or_window, Qnil);
4448 if (!noninteractive)
4450 signal (SIGINT, interrupt_signal);
4451 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
4452 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
4453 SIGQUIT and we can't tell which one it will give us. */
4454 signal (SIGQUIT, interrupt_signal);
4455 #endif /* HAVE_TERMIO */
4456 /* Note SIGIO has been undef'd if FIONREAD is missing. */
4457 #ifdef SIGIO
4458 signal (SIGIO, input_available_signal);
4459 #endif /* SIGIO */
4462 /* Use interrupt input by default, if it works and noninterrupt input
4463 has deficiencies. */
4465 #ifdef INTERRUPT_INPUT
4466 interrupt_input = 1;
4467 #else
4468 interrupt_input = 0;
4469 #endif
4471 /* Our VMS input only works by interrupts, as of now. */
4472 #ifdef VMS
4473 interrupt_input = 1;
4474 #endif
4476 sigfree ();
4477 dribble = 0;
4479 if (keyboard_init_hook)
4480 (*keyboard_init_hook) ();
4482 #ifdef POLL_FOR_INPUT
4483 poll_suppress_count = 1;
4484 start_polling ();
4485 #endif
4488 /* This type's only use is in syms_of_keyboard, to initialize the
4489 event header symbols and put properties on them. */
4490 struct event_head {
4491 Lisp_Object *var;
4492 char *name;
4493 Lisp_Object *kind;
4496 struct event_head head_table[] = {
4497 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
4498 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
4499 &Qswitch_frame, "switch-frame", &Qswitch_frame,
4502 syms_of_keyboard ()
4504 Qself_insert_command = intern ("self-insert-command");
4505 staticpro (&Qself_insert_command);
4507 Qforward_char = intern ("forward-char");
4508 staticpro (&Qforward_char);
4510 Qbackward_char = intern ("backward-char");
4511 staticpro (&Qbackward_char);
4513 Qdisabled = intern ("disabled");
4514 staticpro (&Qdisabled);
4516 Qpre_command_hook = intern ("pre-command-hook");
4517 staticpro (&Qpre_command_hook);
4519 Qpost_command_hook = intern ("post-command-hook");
4520 staticpro (&Qpost_command_hook);
4522 Qfunction_key = intern ("function-key");
4523 staticpro (&Qfunction_key);
4524 Qmouse_click = intern ("mouse-click");
4525 staticpro (&Qmouse_click);
4527 Qmode_line = intern ("mode-line");
4528 staticpro (&Qmode_line);
4529 Qvertical_line = intern ("vertical-line");
4530 staticpro (&Qvertical_line);
4531 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
4532 staticpro (&Qvertical_scroll_bar);
4533 Qmenu_bar = intern ("menu-bar");
4534 staticpro (&Qmenu_bar);
4536 Qabove_handle = intern ("above-handle");
4537 staticpro (&Qabove_handle);
4538 Qhandle = intern ("handle");
4539 staticpro (&Qhandle);
4540 Qbelow_handle = intern ("below-handle");
4541 staticpro (&Qbelow_handle);
4543 Qevent_kind = intern ("event-kind");
4544 staticpro (&Qevent_kind);
4545 Qevent_symbol_elements = intern ("event-symbol-elements");
4546 staticpro (&Qevent_symbol_elements);
4547 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
4548 staticpro (&Qevent_symbol_element_mask);
4549 Qmodifier_cache = intern ("modifier-cache");
4550 staticpro (&Qmodifier_cache);
4552 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
4553 staticpro (&Qrecompute_lucid_menubar);
4554 Qactivate_menubar_hook = intern ("activate-menubar-hook");
4555 staticpro (&Qactivate_menubar_hook);
4558 struct event_head *p;
4560 for (p = head_table;
4561 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
4562 p++)
4564 *p->var = intern (p->name);
4565 staticpro (p->var);
4566 Fput (*p->var, Qevent_kind, *p->kind);
4567 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
4571 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
4572 staticpro (&button_down_location);
4575 int i;
4576 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
4578 modifier_symbols = Fmake_vector (make_number (len), Qnil);
4579 for (i = 0; i < len; i++)
4580 if (modifier_names[i])
4581 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
4582 staticpro (&modifier_symbols);
4585 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
4586 staticpro (&recent_keys);
4588 this_command_keys = Fmake_vector (make_number (40), Qnil);
4589 staticpro (&this_command_keys);
4591 kbd_buffer_frame_or_window
4592 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
4593 staticpro (&kbd_buffer_frame_or_window);
4595 func_key_syms = Qnil;
4596 staticpro (&func_key_syms);
4598 mouse_syms = Qnil;
4599 staticpro (&mouse_syms);
4601 unread_switch_frame = Qnil;
4602 staticpro (&unread_switch_frame);
4604 defsubr (&Sread_key_sequence);
4605 defsubr (&Srecursive_edit);
4606 defsubr (&Strack_mouse);
4607 defsubr (&Sinput_pending_p);
4608 defsubr (&Scommand_execute);
4609 defsubr (&Srecent_keys);
4610 defsubr (&Sthis_command_keys);
4611 defsubr (&Ssuspend_emacs);
4612 defsubr (&Sabort_recursive_edit);
4613 defsubr (&Sexit_recursive_edit);
4614 defsubr (&Srecursion_depth);
4615 defsubr (&Stop_level);
4616 defsubr (&Sdiscard_input);
4617 defsubr (&Sopen_dribble_file);
4618 defsubr (&Sset_input_mode);
4619 defsubr (&Sexecute_extended_command);
4621 DEFVAR_LISP ("disabled-command-hook", &Vdisabled_command_hook,
4622 "Value is called instead of any command that is disabled\n\
4623 \(has a non-nil `disabled' property).");
4625 DEFVAR_LISP ("last-command-char", &last_command_char,
4626 "Last input event that was part of a command.");
4628 DEFVAR_LISP ("last-command-event", &last_command_char,
4629 "Last input event that was part of a command.");
4631 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
4632 "Last input event in a command, except for mouse menu events.\n\
4633 Mouse menus give back keys that don't look like mouse events;\n\
4634 this variable holds the actual mouse event that led to the menu,\n\
4635 so that you can determine whether the command was run by mouse or not.");
4637 DEFVAR_LISP ("last-input-char", &last_input_char,
4638 "Last input event.");
4640 DEFVAR_LISP ("last-input-event", &last_input_char,
4641 "Last input event.");
4643 DEFVAR_LISP ("unread-command-events", &unread_command_events,
4644 "List of objects to be read as next command input events.");
4646 DEFVAR_INT ("unread-command-char", &unread_command_char,
4647 "If not -1, an object to be read as next command input event.");
4649 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
4650 "Meta-prefix character code. Meta-foo as command input\n\
4651 turns into this character followed by foo.");
4652 XSET (meta_prefix_char, Lisp_Int, 033);
4654 DEFVAR_LISP ("last-command", &last_command,
4655 "The last command executed. Normally a symbol with a function definition,\n\
4656 but can be whatever was found in the keymap, or whatever the variable\n\
4657 `this-command' was set to by that command.");
4658 last_command = Qnil;
4660 DEFVAR_LISP ("this-command", &this_command,
4661 "The command now being executed.\n\
4662 The command can set this variable; whatever is put here\n\
4663 will be in `last-command' during the following command.");
4664 this_command = Qnil;
4666 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
4667 "*Number of keyboard input characters between auto-saves.\n\
4668 Zero means disable autosaving due to number of characters typed.");
4669 auto_save_interval = 300;
4671 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
4672 "*Number of seconds idle time before auto-save.\n\
4673 Zero or nil means disable auto-saving due to idleness.\n\
4674 After auto-saving due to this many seconds of idle time,\n\
4675 Emacs also does a garbage collection if that seems to be warranted.");
4676 XFASTINT (Vauto_save_timeout) = 30;
4678 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
4679 "*Nonzero means echo unfinished commands after this many seconds of pause.");
4680 echo_keystrokes = 1;
4682 DEFVAR_INT ("polling-period", &polling_period,
4683 "*Interval between polling for input during Lisp execution.\n\
4684 The reason for polling is to make C-g work to stop a running program.\n\
4685 Polling is needed only when using X windows and SIGIO does not work.\n\
4686 Polling is automatically disabled in all other cases.");
4687 polling_period = 2;
4689 DEFVAR_INT ("num-input-keys", &num_input_keys,
4690 "*Number of complete keys read from the keyboard so far.");
4691 num_input_keys = 0;
4693 #ifdef MULTI_FRAME
4694 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
4695 "*The frame in which the most recently read event occurred.\n\
4696 If the last event came from a keyboard macro, this is set to `macro'.");
4697 Vlast_event_frame = Qnil;
4698 #endif
4700 DEFVAR_LISP ("help-char", &help_char,
4701 "Character to recognize as meaning Help.\n\
4702 When it is read, do `(eval help-form)', and display result if it's a string.\n\
4703 If the value of `help-form' is nil, this char can be read normally.");
4704 XSET (help_char, Lisp_Int, Ctl ('H'));
4706 DEFVAR_LISP ("help-form", &Vhelp_form,
4707 "Form to execute when character help-char is read.\n\
4708 If the form returns a string, that string is displayed.\n\
4709 If `help-form' is nil, the help char is not recognized.");
4710 Vhelp_form = Qnil;
4712 DEFVAR_LISP ("top-level", &Vtop_level,
4713 "Form to evaluate when Emacs starts up.\n\
4714 Useful to set before you dump a modified Emacs.");
4715 Vtop_level = Qnil;
4717 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
4718 "String used as translate table for keyboard input, or nil.\n\
4719 Each character is looked up in this string and the contents used instead.\n\
4720 If string is of length N, character codes N and up are untranslated.");
4721 Vkeyboard_translate_table = Qnil;
4723 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
4724 "Non-nil means prompt with menus when appropriate.\n\
4725 This is done when reading from a keymap that has a prompt string,\n\
4726 for elements that have prompt strings.\n\
4727 The menu is displayed on the screen\n\
4728 if X menus were enabled at configuration\n\
4729 time and the previous event was a mouse click prefix key.\n\
4730 Otherwise, menu prompting uses the echo area.");
4731 menu_prompting = 1;
4733 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
4734 "Character to see next line of menu prompt.\n\
4735 Type this character while in a menu prompt to rotate around the lines of it.");
4736 XSET (menu_prompt_more_char, Lisp_Int, ' ');
4738 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
4739 "A mask of additional modifier keys to use with every keyboard character.\n\
4740 These bits follow the convention for X windows,\n\
4741 but the control and meta bits work even when you are not using X:\n\
4742 1 -- shift bit 2 -- lock bit\n\
4743 4 -- control bit 8 -- meta bit.");
4744 extra_keyboard_modifiers = 0;
4746 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
4747 "If an editing command sets this to t, deactivate the mark afterward.\n\
4748 The command loop sets this to nil before each command,\n\
4749 and tests the value when the command returns.\n\
4750 Buffer modification stores t in this variable.");
4751 Vdeactivate_mark = Qnil;
4753 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
4754 "Normal hook run before each command is executed.");
4755 Vpre_command_hook = Qnil;
4757 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
4758 "Normal hook run before each command is executed.");
4759 Vpost_command_hook = Qnil;
4761 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
4762 "t means menu bar, specified Lucid style, needs to be recomputed.");
4763 Vlucid_menu_bar_dirty_flag = Qnil;
4766 keys_of_keyboard ()
4768 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
4769 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
4770 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
4771 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
4772 initial_define_key (meta_map, 'x', "execute-extended-command");