New enhanced version from <kgallagh@spd.dsccc.com>.
[emacs.git] / src / keyboard.c
blobdf2c372f44148cf7cc561c38e8aeadc6f686df2a
1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985,86,87,88,89,93,94,95 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Allow config.h to undefine symbols found here. */
21 #include <signal.h>
23 #include <config.h>
24 #include <stdio.h>
25 #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 "blockinput.h"
40 #include <setjmp.h>
41 #include <errno.h>
43 #ifdef MSDOS
44 #include "msdos.h"
45 #include <time.h>
46 #else /* not MSDOS */
47 #ifndef VMS
48 #include <sys/ioctl.h>
49 #endif
50 #endif /* not MSDOS */
52 #include "syssignal.h"
53 #include "systty.h"
55 /* This is to get the definitions of the XK_ symbols. */
56 #ifdef HAVE_X_WINDOWS
57 #include "xterm.h"
58 #endif
60 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
61 #include "systime.h"
63 extern int errno;
65 /* Variables for blockinput.h: */
67 /* Non-zero if interrupt input is blocked right now. */
68 int interrupt_input_blocked;
70 /* Nonzero means an input interrupt has arrived
71 during the current critical section. */
72 int interrupt_input_pending;
75 /* File descriptor to use for input. */
76 extern int input_fd;
78 #ifdef HAVE_X_WINDOWS
79 /* Make all keyboard buffers much bigger when using X windows. */
80 #define KBD_BUFFER_SIZE 4096
81 #else /* No X-windows, character input */
82 #define KBD_BUFFER_SIZE 256
83 #endif /* No X-windows */
85 /* Following definition copied from eval.c */
87 struct backtrace
89 struct backtrace *next;
90 Lisp_Object *function;
91 Lisp_Object *args; /* Points to vector of args. */
92 int nargs; /* length of vector. If nargs is UNEVALLED,
93 args points to slot holding list of
94 unevalled args */
95 char evalargs;
98 #ifdef MULTI_PERDISPLAY
99 PERDISPLAY *current_perdisplay;
100 PERDISPLAY *all_perdisplays;
101 #else
102 PERDISPLAY the_only_perdisplay;
103 #endif
105 /* Non-nil disable property on a command means
106 do not execute it; call disabled-command-hook's value instead. */
107 Lisp_Object Qdisabled, Qdisabled_command_hook;
109 #define NUM_RECENT_KEYS (100)
110 int recent_keys_index; /* Index for storing next element into recent_keys */
111 int total_keys; /* Total number of elements stored into recent_keys */
112 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
114 /* Vector holding the key sequence that invoked the current command.
115 It is reused for each command, and it may be longer than the current
116 sequence; this_command_key_count indicates how many elements
117 actually mean something.
118 It's easier to staticpro a single Lisp_Object than an array. */
119 Lisp_Object this_command_keys;
120 int this_command_key_count;
122 extern int minbuf_level;
124 extern struct backtrace *backtrace_list;
126 /* Nonzero means do menu prompting. */
127 static int menu_prompting;
129 /* Character to see next line of menu prompt. */
130 static Lisp_Object menu_prompt_more_char;
132 /* For longjmp to where kbd input is being done. */
133 static jmp_buf getcjmp;
135 /* True while doing kbd input. */
136 int waiting_for_input;
138 /* True while displaying for echoing. Delays C-g throwing. */
139 static int echoing;
141 /* Nonzero means disregard local maps for the menu bar. */
142 static int inhibit_local_menu_bar_menus;
144 /* Nonzero means C-g should cause immediate error-signal. */
145 int immediate_quit;
147 /* Character to recognize as the help char. */
148 Lisp_Object Vhelp_char;
150 /* Form to execute when help char is typed. */
151 Lisp_Object Vhelp_form;
153 /* Command to run when the help character follows a prefix key. */
154 Lisp_Object Vprefix_help_command;
156 /* List of items that should move to the end of the menu bar. */
157 Lisp_Object Vmenu_bar_final_items;
159 /* Character that causes a quit. Normally C-g.
161 If we are running on an ordinary terminal, this must be an ordinary
162 ASCII char, since we want to make it our interrupt character.
164 If we are not running on an ordinary terminal, it still needs to be
165 an ordinary ASCII char. This character needs to be recognized in
166 the input interrupt handler. At this point, the keystroke is
167 represented as a struct input_event, while the desired quit
168 character is specified as a lispy event. The mapping from struct
169 input_events to lispy events cannot run in an interrupt handler,
170 and the reverse mapping is difficult for anything but ASCII
171 keystrokes.
173 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
174 ASCII character. */
175 int quit_char;
177 extern Lisp_Object current_global_map;
178 extern int minibuf_level;
180 /* If non-nil, this is a map that overrides all other local maps. */
181 Lisp_Object Voverriding_local_map;
183 /* If non-nil, Voverriding_local_map applies to the menu bar. */
184 Lisp_Object Voverriding_local_map_menu_flag;
186 /* Current depth in recursive edits. */
187 int command_loop_level;
189 /* Total number of times command_loop has read a key sequence. */
190 int num_input_keys;
192 /* Last input character read as a command. */
193 Lisp_Object last_command_char;
195 /* Last input character read as a command, not counting menus
196 reached by the mouse. */
197 Lisp_Object last_nonmenu_event;
199 /* Last input character read for any purpose. */
200 Lisp_Object last_input_char;
202 /* If not Qnil, a list of objects to be read as subsequent command input. */
203 Lisp_Object Vunread_command_events;
205 /* If not -1, an event to be read as subsequent command input. */
206 int unread_command_char;
208 /* If not Qnil, this is a switch-frame event which we decided to put
209 off until the end of a key sequence. This should be read as the
210 next command input, after any unread_command_events.
212 read_key_sequence uses this to delay switch-frame events until the
213 end of the key sequence; Fread_char uses it to put off switch-frame
214 events until a non-ASCII event is acceptable as input. */
215 Lisp_Object unread_switch_frame;
217 /* A mask of extra modifier bits to put into every keyboard char. */
218 int extra_keyboard_modifiers;
220 /* Char to use as prefix when a meta character is typed in.
221 This is bound on entry to minibuffer in case ESC is changed there. */
223 Lisp_Object meta_prefix_char;
225 /* Last size recorded for a current buffer which is not a minibuffer. */
226 static int last_non_minibuf_size;
228 /* Number of idle seconds before an auto-save and garbage collection. */
229 static Lisp_Object Vauto_save_timeout;
231 /* Total number of times read_char has returned. */
232 int num_input_chars;
234 /* Total number of times read_char has returned, outside of macros. */
235 int num_nonmacro_input_chars;
237 /* Auto-save automatically when this many characters have been typed
238 since the last time. */
240 static int auto_save_interval;
242 /* Value of num_nonmacro_input_chars as of last auto save. */
244 int last_auto_save;
246 /* Last command executed by the editor command loop, not counting
247 commands that set the prefix argument. */
249 Lisp_Object last_command;
251 /* The command being executed by the command loop.
252 Commands may set this, and the value set will be copied into last_command
253 instead of the actual command. */
254 Lisp_Object this_command;
256 /* The value of point when the last command was executed. */
257 int last_point_position;
259 /* The buffer that was current when the last command was started. */
260 Lisp_Object last_point_position_buffer;
262 /* The timestamp of the last input event we received from the X server.
263 X Windows wants this for selection ownership. */
264 unsigned long last_event_timestamp;
266 Lisp_Object Qself_insert_command;
267 Lisp_Object Qforward_char;
268 Lisp_Object Qbackward_char;
269 Lisp_Object Qundefined;
271 /* read_key_sequence stores here the command definition of the
272 key sequence that it reads. */
273 Lisp_Object read_key_sequence_cmd;
275 /* Form to evaluate (if non-nil) when Emacs is started. */
276 Lisp_Object Vtop_level;
278 /* User-supplied string to translate input characters through. */
279 Lisp_Object Vkeyboard_translate_table;
281 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
282 extern Lisp_Object Vfunction_key_map;
284 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
285 Lisp_Object Vkey_translation_map;
287 /* Non-nil means deactivate the mark at end of this command. */
288 Lisp_Object Vdeactivate_mark;
290 /* Menu bar specified in Lucid Emacs fashion. */
292 Lisp_Object Vlucid_menu_bar_dirty_flag;
293 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
295 /* Hooks to run before and after each command. */
296 Lisp_Object Qpre_command_hook, Qpost_command_hook;
297 Lisp_Object Vpre_command_hook, Vpost_command_hook;
298 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
300 /* List of deferred actions to be performed at a later time.
301 The precise format isn't relevant here; we just check whether it is nil. */
302 Lisp_Object Vdeferred_action_list;
304 /* Function to call to handle deferred actions, when there are any. */
305 Lisp_Object Vdeferred_action_function;
306 Lisp_Object Qdeferred_action_function;
308 /* File in which we write all commands we read. */
309 FILE *dribble;
311 /* Nonzero if input is available. */
312 int input_pending;
314 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
315 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
317 int meta_key;
319 extern char *pending_malloc_warning;
321 #ifdef HAVE_MOUSE
322 /* If this flag is a frame, we check mouse_moved to see when the
323 mouse moves, and motion events will appear in the input stream.
324 Otherwise, mouse motion is ignored. */
325 static Lisp_Object 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;
333 #endif /* HAVE_MOUSE. */
335 /* Symbols to head events. */
336 Lisp_Object Qmouse_movement;
337 Lisp_Object Qscroll_bar_movement;
338 Lisp_Object Qswitch_frame;
339 Lisp_Object Qdelete_frame;
340 Lisp_Object Qiconify_frame;
341 Lisp_Object Qmake_frame_visible;
343 /* Symbols to denote kinds of events. */
344 Lisp_Object Qfunction_key;
345 Lisp_Object Qmouse_click;
346 /* Lisp_Object Qmouse_movement; - also an event header */
348 /* Properties of event headers. */
349 Lisp_Object Qevent_kind;
350 Lisp_Object Qevent_symbol_elements;
352 Lisp_Object Qmenu_enable;
354 /* An event header symbol HEAD may have a property named
355 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
356 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
357 mask of modifiers applied to it. If present, this is used to help
358 speed up parse_modifiers. */
359 Lisp_Object Qevent_symbol_element_mask;
361 /* An unmodified event header BASE may have a property named
362 Qmodifier_cache, which is an alist mapping modifier masks onto
363 modified versions of BASE. If present, this helps speed up
364 apply_modifiers. */
365 Lisp_Object Qmodifier_cache;
367 /* Symbols to use for parts of windows. */
368 Lisp_Object Qmode_line;
369 Lisp_Object Qvertical_line;
370 Lisp_Object Qvertical_scroll_bar;
371 Lisp_Object Qmenu_bar;
373 extern Lisp_Object Qmenu_enable;
375 Lisp_Object recursive_edit_unwind (), command_loop ();
376 Lisp_Object Fthis_command_keys ();
377 Lisp_Object Qextended_command_history;
379 Lisp_Object Qpolling_period;
381 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
382 happens. */
383 EMACS_TIME *input_available_clear_time;
385 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
386 Default is 1 if INTERRUPT_INPUT is defined. */
387 int interrupt_input;
389 /* Nonzero while interrupts are temporarily deferred during redisplay. */
390 int interrupts_deferred;
392 /* nonzero means use ^S/^Q for flow control. */
393 int flow_control;
395 /* Allow m- file to inhibit use of FIONREAD. */
396 #ifdef BROKEN_FIONREAD
397 #undef FIONREAD
398 #endif
400 /* We are unable to use interrupts if FIONREAD is not available,
401 so flush SIGIO so we won't try. */
402 #ifndef FIONREAD
403 #ifdef SIGIO
404 #undef SIGIO
405 #endif
406 #endif
408 /* If we support X Windows, turn on the code to poll periodically
409 to detect C-g. It isn't actually used when doing interrupt input. */
410 #ifdef HAVE_X_WINDOWS
411 #define POLL_FOR_INPUT
412 #endif
414 /* Global variable declarations. */
416 /* Function for init_keyboard to call with no args (if nonzero). */
417 void (*keyboard_init_hook) ();
419 static int read_avail_input ();
420 static void get_input_pending ();
421 static int readable_events ();
422 static Lisp_Object read_char_x_menu_prompt ();
423 static Lisp_Object read_char_minibuf_menu_prompt ();
424 static Lisp_Object make_lispy_event ();
425 static Lisp_Object make_lispy_movement ();
426 static Lisp_Object modify_event_symbol ();
427 static Lisp_Object make_lispy_switch_frame ();
429 /* > 0 if we are to echo keystrokes. */
430 static int echo_keystrokes;
432 /* Nonzero means don't try to suspend even if the operating system seems
433 to support it. */
434 static int cannot_suspend;
436 #define min(a,b) ((a)<(b)?(a):(b))
437 #define max(a,b) ((a)>(b)?(a):(b))
439 /* Install the string STR as the beginning of the string of echoing,
440 so that it serves as a prompt for the next character.
441 Also start echoing. */
443 echo_prompt (str)
444 char *str;
446 int len = strlen (str);
448 if (len > ECHOBUFSIZE - 4)
449 len = ECHOBUFSIZE - 4;
450 bcopy (str, current_perdisplay->echobuf, len);
451 current_perdisplay->echoptr = current_perdisplay->echobuf + len;
452 *current_perdisplay->echoptr = '\0';
454 current_perdisplay->echo_after_prompt = len;
456 echo ();
459 /* Add C to the echo string, if echoing is going on.
460 C can be a character, which is printed prettily ("M-C-x" and all that
461 jazz), or a symbol, whose name is printed. */
463 echo_char (c)
464 Lisp_Object c;
466 extern char *push_key_description ();
468 if (current_perdisplay->immediate_echo)
470 char *ptr = current_perdisplay->echoptr;
472 if (ptr != current_perdisplay->echobuf)
473 *ptr++ = ' ';
475 /* If someone has passed us a composite event, use its head symbol. */
476 c = EVENT_HEAD (c);
478 if (INTEGERP (c))
480 if (ptr - current_perdisplay->echobuf > ECHOBUFSIZE - 6)
481 return;
483 ptr = push_key_description (XINT (c), ptr);
485 else if (SYMBOLP (c))
487 struct Lisp_String *name = XSYMBOL (c)->name;
488 if (((ptr - current_perdisplay->echobuf) + name->size + 4)
489 > ECHOBUFSIZE)
490 return;
491 bcopy (name->data, ptr, name->size);
492 ptr += name->size;
495 if (current_perdisplay->echoptr == current_perdisplay->echobuf
496 && EQ (c, Vhelp_char))
498 strcpy (ptr, " (Type ? for further options)");
499 ptr += strlen (ptr);
502 *ptr = 0;
503 current_perdisplay->echoptr = ptr;
505 echo ();
509 /* Temporarily add a dash to the end of the echo string if it's not
510 empty, so that it serves as a mini-prompt for the very next character. */
512 echo_dash ()
514 if (!current_perdisplay->immediate_echo
515 && current_perdisplay->echoptr == current_perdisplay->echobuf)
516 return;
517 /* Do nothing if we just printed a prompt. */
518 if (current_perdisplay->echo_after_prompt
519 == current_perdisplay->echoptr - current_perdisplay->echobuf)
520 return;
521 /* Do nothing if not echoing at all. */
522 if (current_perdisplay->echoptr == 0)
523 return;
525 /* Put a dash at the end of the buffer temporarily,
526 but make it go away when the next character is added. */
527 current_perdisplay->echoptr[0] = '-';
528 current_perdisplay->echoptr[1] = 0;
530 echo ();
533 /* Display the current echo string, and begin echoing if not already
534 doing so. */
536 echo ()
538 if (!current_perdisplay->immediate_echo)
540 int i;
541 current_perdisplay->immediate_echo = 1;
543 for (i = 0; i < this_command_key_count; i++)
545 Lisp_Object c;
546 c = XVECTOR (this_command_keys)->contents[i];
547 if (! (EVENT_HAS_PARAMETERS (c)
548 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
549 echo_char (c);
551 echo_dash ();
554 echoing = 1;
555 message1_nolog (current_perdisplay->echobuf);
556 echoing = 0;
558 if (waiting_for_input && !NILP (Vquit_flag))
559 quit_throw_to_read_char ();
562 /* Turn off echoing, for the start of a new command. */
564 cancel_echoing ()
566 current_perdisplay->immediate_echo = 0;
567 current_perdisplay->echoptr = current_perdisplay->echobuf;
568 current_perdisplay->echo_after_prompt = -1;
571 /* Return the length of the current echo string. */
573 static int
574 echo_length ()
576 return current_perdisplay->echoptr - current_perdisplay->echobuf;
579 /* Truncate the current echo message to its first LEN chars.
580 This and echo_char get used by read_key_sequence when the user
581 switches frames while entering a key sequence. */
583 static void
584 echo_truncate (len)
585 int len;
587 current_perdisplay->echobuf[len] = '\0';
588 current_perdisplay->echoptr = current_perdisplay->echobuf + len;
589 truncate_echo_area (len);
593 /* Functions for manipulating this_command_keys. */
594 static void
595 add_command_key (key)
596 Lisp_Object key;
598 int size = XVECTOR (this_command_keys)->size;
600 if (this_command_key_count >= size)
602 Lisp_Object new_keys;
604 new_keys = Fmake_vector (make_number (size * 2), Qnil);
605 bcopy (XVECTOR (this_command_keys)->contents,
606 XVECTOR (new_keys)->contents,
607 size * sizeof (Lisp_Object));
609 this_command_keys = new_keys;
612 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
615 Lisp_Object
616 recursive_edit_1 ()
618 int count = specpdl_ptr - specpdl;
619 Lisp_Object val;
621 if (command_loop_level > 0)
623 specbind (Qstandard_output, Qt);
624 specbind (Qstandard_input, Qt);
627 val = command_loop ();
628 if (EQ (val, Qt))
629 Fsignal (Qquit, Qnil);
631 return unbind_to (count, Qnil);
634 /* When an auto-save happens, record the "time", and don't do again soon. */
636 record_auto_save ()
638 last_auto_save = num_nonmacro_input_chars;
641 /* Make an auto save happen as soon as possible at command level. */
643 force_auto_save_soon ()
645 last_auto_save = - auto_save_interval - 1;
647 record_asynch_buffer_change ();
650 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
651 "Invoke the editor command loop recursively.\n\
652 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
653 that tells this function to return.\n\
654 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
655 This function is called by the editor initialization to begin editing.")
658 int count = specpdl_ptr - specpdl;
659 Lisp_Object val;
661 command_loop_level++;
662 update_mode_lines = 1;
664 record_unwind_protect (recursive_edit_unwind,
665 (command_loop_level
666 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
667 ? Fcurrent_buffer ()
668 : Qnil);
669 recursive_edit_1 ();
670 return unbind_to (count, Qnil);
673 Lisp_Object
674 recursive_edit_unwind (buffer)
675 Lisp_Object buffer;
677 if (!NILP (buffer))
678 Fset_buffer (buffer);
680 command_loop_level--;
681 update_mode_lines = 1;
682 return Qnil;
685 Lisp_Object
686 cmd_error (data)
687 Lisp_Object data;
689 Vstandard_output = Qt;
690 Vstandard_input = Qt;
691 Vexecuting_macro = Qnil;
692 if (!current_perdisplay)
693 abort ();
694 current_perdisplay->Vprefix_arg = Qnil;
695 cancel_echoing ();
696 cmd_error_internal (data, 0);
698 Vquit_flag = Qnil;
700 Vinhibit_quit = Qnil;
701 #ifdef MULTI_PERDISPLAY
702 current_perdisplay = 0;
703 #endif
705 return make_number (0);
708 cmd_error_internal (data, context)
709 Lisp_Object data;
710 char *context;
712 Lisp_Object errmsg, tail, errname, file_error;
713 Lisp_Object stream;
714 struct gcpro gcpro1;
715 int i;
717 Vquit_flag = Qnil;
718 Vinhibit_quit = Qt;
719 echo_area_glyphs = 0;
721 /* If the window system or terminal frame hasn't been initialized
722 yet, or we're not interactive, it's best to dump this message out
723 to stderr and exit. */
724 if (! FRAME_MESSAGE_BUF (selected_frame)
725 || noninteractive)
726 stream = Qexternal_debugging_output;
727 else
729 Fdiscard_input ();
730 bitch_at_user ();
731 stream = Qt;
734 if (context != 0)
735 write_string_1 (context, -1, stream);
737 errname = Fcar (data);
739 if (EQ (errname, Qerror))
741 data = Fcdr (data);
742 if (!CONSP (data)) data = Qnil;
743 errmsg = Fcar (data);
744 file_error = Qnil;
746 else
748 errmsg = Fget (errname, Qerror_message);
749 file_error = Fmemq (Qfile_error,
750 Fget (errname, Qerror_conditions));
753 /* Print an error message including the data items.
754 This is done by printing it into a scratch buffer
755 and then making a copy of the text in the buffer. */
757 if (!CONSP (data)) data = Qnil;
758 tail = Fcdr (data);
759 GCPRO1 (tail);
761 /* For file-error, make error message by concatenating
762 all the data items. They are all strings. */
763 if (!NILP (file_error) && !NILP (tail))
764 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
766 if (STRINGP (errmsg))
767 Fprinc (errmsg, stream);
768 else
769 write_string_1 ("peculiar error", -1, stream);
771 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
773 write_string_1 (i ? ", " : ": ", 2, stream);
774 if (!NILP (file_error))
775 Fprinc (Fcar (tail), stream);
776 else
777 Fprin1 (Fcar (tail), stream);
779 UNGCPRO;
781 /* If the window system or terminal frame hasn't been initialized
782 yet, or we're in -batch mode, this error should cause Emacs to exit. */
783 if (! FRAME_MESSAGE_BUF (selected_frame)
784 || noninteractive)
786 Fterpri (stream);
787 Fkill_emacs (make_number (-1));
791 Lisp_Object command_loop_1 ();
792 Lisp_Object command_loop_2 ();
793 Lisp_Object top_level_1 ();
795 /* Entry to editor-command-loop.
796 This level has the catches for exiting/returning to editor command loop.
797 It returns nil to exit recursive edit, t to abort it. */
799 Lisp_Object
800 command_loop ()
802 if (command_loop_level > 0 || minibuf_level > 0)
804 return internal_catch (Qexit, command_loop_2, Qnil);
806 else
807 while (1)
809 internal_catch (Qtop_level, top_level_1, Qnil);
810 internal_catch (Qtop_level, command_loop_2, Qnil);
812 /* End of file in -batch run causes exit here. */
813 if (noninteractive)
814 Fkill_emacs (Qt);
818 /* Here we catch errors in execution of commands within the
819 editing loop, and reenter the editing loop.
820 When there is an error, cmd_error runs and returns a non-nil
821 value to us. A value of nil means that cmd_loop_1 itself
822 returned due to end of file (or end of kbd macro). */
824 Lisp_Object
825 command_loop_2 ()
827 register Lisp_Object val;
830 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
831 while (!NILP (val));
833 return Qnil;
836 Lisp_Object
837 top_level_2 ()
839 return Feval (Vtop_level);
842 Lisp_Object
843 top_level_1 ()
845 /* On entry to the outer level, run the startup file */
846 if (!NILP (Vtop_level))
847 internal_condition_case (top_level_2, Qerror, cmd_error);
848 else if (!NILP (Vpurify_flag))
849 message ("Bare impure Emacs (standard Lisp code not loaded)");
850 else
851 message ("Bare Emacs (standard Lisp code not loaded)");
852 return Qnil;
855 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
856 "Exit all recursive editing levels.")
859 Fthrow (Qtop_level, Qnil);
862 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
863 "Exit from the innermost recursive edit or minibuffer.")
866 if (command_loop_level > 0 || minibuf_level > 0)
867 Fthrow (Qexit, Qnil);
869 error ("No recursive edit is in progress");
872 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
873 "Abort the command that requested this recursive edit or minibuffer input.")
876 if (command_loop_level > 0 || minibuf_level > 0)
877 Fthrow (Qexit, Qt);
879 error ("No recursive edit is in progress");
882 /* This is the actual command reading loop,
883 sans error-handling encapsulation. */
885 Lisp_Object Fcommand_execute ();
886 static int read_key_sequence ();
887 static void safe_run_hooks ();
889 Lisp_Object
890 command_loop_1 ()
892 Lisp_Object cmd, tem;
893 int lose;
894 int nonundocount;
895 Lisp_Object keybuf[30];
896 int i;
897 int no_redisplay;
898 int no_direct;
899 int prev_modiff;
900 struct buffer *prev_buffer;
901 PERDISPLAY *global_perdisplay = current_perdisplay;
903 Vdeactivate_mark = Qnil;
904 waiting_for_input = 0;
906 nonundocount = 0;
907 no_redisplay = 0;
908 this_command_key_count = 0;
910 /* Make sure this hook runs after commands that get errors and
911 throw to top level. */
912 /* Note that the value cell will never directly contain nil
913 if the symbol is a local variable. */
914 if (!NILP (XSYMBOL (Qpost_command_hook)->value) && !NILP (Vrun_hooks))
915 safe_run_hooks (Qpost_command_hook);
917 if (!NILP (Vdeferred_action_list))
918 call0 (Vdeferred_action_function);
920 /* Do this after running Vpost_command_hook, for consistency. */
921 last_command = this_command;
923 while (1)
925 /* Make sure the current window's buffer is selected. */
926 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
927 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
929 /* Display any malloc warning that just came out. Use while because
930 displaying one warning can cause another. */
932 while (pending_malloc_warning)
933 display_malloc_warning ();
935 no_direct = 0;
937 Vdeactivate_mark = Qnil;
939 /* If minibuffer on and echo area in use,
940 wait 2 sec and redraw minibuffer. */
942 if (minibuf_level && echo_area_glyphs)
944 /* Bind inhibit-quit to t so that C-g gets read in
945 rather than quitting back to the minibuffer. */
946 int count = specpdl_ptr - specpdl;
947 specbind (Qinhibit_quit, Qt);
948 Fsit_for (make_number (2), Qnil, Qnil);
949 unbind_to (count, Qnil);
951 echo_area_glyphs = 0;
952 no_direct = 1;
953 if (!NILP (Vquit_flag))
955 Vquit_flag = Qnil;
956 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
960 #ifdef C_ALLOCA
961 alloca (0); /* Cause a garbage collection now */
962 /* Since we can free the most stuff here. */
963 #endif /* C_ALLOCA */
965 #if 0
966 #ifdef MULTI_FRAME
967 /* Select the frame that the last event came from. Usually,
968 switch-frame events will take care of this, but if some lisp
969 code swallows a switch-frame event, we'll fix things up here.
970 Is this a good idea? */
971 if (FRAMEP (internal_last_event_frame)
972 && XFRAME (internal_last_event_frame) != selected_frame)
973 Fselect_frame (internal_last_event_frame, Qnil);
974 #endif
975 #endif
976 /* If it has changed current-menubar from previous value,
977 really recompute the menubar from the value. */
978 if (! NILP (Vlucid_menu_bar_dirty_flag)
979 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
980 call0 (Qrecompute_lucid_menubar);
982 /* Read next key sequence; i gets its length. */
983 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0], Qnil, 0);
985 ++num_input_keys;
987 /* Now we have read a key sequence of length I,
988 or else I is 0 and we found end of file. */
990 if (i == 0) /* End of file -- happens only in */
991 return Qnil; /* a kbd macro, at the end. */
992 /* -1 means read_key_sequence got a menu that was rejected.
993 Just loop around and read another command. */
994 if (i == -1)
996 cancel_echoing ();
997 this_command_key_count = 0;
998 goto finalize;
1001 last_command_char = keybuf[i - 1];
1003 /* If the previous command tried to force a specific window-start,
1004 forget about that, in case this command moves point far away
1005 from that position. */
1006 XWINDOW (selected_window)->force_start = Qnil;
1008 cmd = read_key_sequence_cmd;
1009 if (!NILP (Vexecuting_macro))
1011 if (!NILP (Vquit_flag))
1013 Vexecuting_macro = Qt;
1014 QUIT; /* Make some noise. */
1015 /* Will return since macro now empty. */
1019 /* Do redisplay processing after this command except in special
1020 cases identified below that set no_redisplay to 1.
1021 (actually, there's currently no way to prevent the redisplay,
1022 and no_redisplay is ignored.
1023 Perhaps someday we will really implement it. */
1024 no_redisplay = 0;
1026 prev_buffer = current_buffer;
1027 prev_modiff = MODIFF;
1028 last_point_position = PT;
1029 XSETBUFFER (last_point_position_buffer, prev_buffer);
1031 /* Execute the command. */
1033 this_command = cmd;
1034 /* Note that the value cell will never directly contain nil
1035 if the symbol is a local variable. */
1036 if (!NILP (XSYMBOL (Qpre_command_hook)->value) && !NILP (Vrun_hooks))
1037 safe_run_hooks (Qpre_command_hook);
1039 if (NILP (this_command))
1041 /* nil means key is undefined. */
1042 bitch_at_user ();
1043 defining_kbd_macro = 0;
1044 update_mode_lines = 1;
1045 current_perdisplay->Vprefix_arg = Qnil;
1048 else
1050 if (NILP (current_perdisplay->Vprefix_arg) && ! no_direct)
1052 /* Recognize some common commands in common situations and
1053 do them directly. */
1054 if (EQ (this_command, Qforward_char) && PT < ZV)
1056 struct Lisp_Vector *dp
1057 = window_display_table (XWINDOW (selected_window));
1058 lose = FETCH_CHAR (PT);
1059 SET_PT (PT + 1);
1060 if ((dp
1061 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1062 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1063 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1064 && (lose >= 0x20 && lose < 0x7f)))
1065 : (lose >= 0x20 && lose < 0x7f))
1066 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1067 >= MODIFF)
1068 && (XFASTINT (XWINDOW (selected_window)->last_point)
1069 == PT - 1)
1070 && !windows_or_buffers_changed
1071 && EQ (current_buffer->selective_display, Qnil)
1072 && !detect_input_pending ()
1073 && NILP (Vexecuting_macro))
1074 no_redisplay = direct_output_forward_char (1);
1075 goto directly_done;
1077 else if (EQ (this_command, Qbackward_char) && PT > BEGV)
1079 struct Lisp_Vector *dp
1080 = window_display_table (XWINDOW (selected_window));
1081 SET_PT (PT - 1);
1082 lose = FETCH_CHAR (PT);
1083 if ((dp
1084 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1085 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1086 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1087 && (lose >= 0x20 && lose < 0x7f)))
1088 : (lose >= 0x20 && lose < 0x7f))
1089 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1090 >= MODIFF)
1091 && (XFASTINT (XWINDOW (selected_window)->last_point)
1092 == PT + 1)
1093 && !windows_or_buffers_changed
1094 && EQ (current_buffer->selective_display, Qnil)
1095 && !detect_input_pending ()
1096 && NILP (Vexecuting_macro))
1097 no_redisplay = direct_output_forward_char (-1);
1098 goto directly_done;
1100 else if (EQ (this_command, Qself_insert_command)
1101 /* Try this optimization only on ascii keystrokes. */
1102 && INTEGERP (last_command_char))
1104 unsigned char c = XINT (last_command_char);
1105 int value;
1107 if (NILP (Vexecuting_macro)
1108 && !EQ (minibuf_window, selected_window))
1110 if (!nonundocount || nonundocount >= 20)
1112 Fundo_boundary ();
1113 nonundocount = 0;
1115 nonundocount++;
1117 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1118 < MODIFF)
1119 || (XFASTINT (XWINDOW (selected_window)->last_point)
1120 != PT)
1121 || MODIFF <= SAVE_MODIFF
1122 || windows_or_buffers_changed
1123 || !EQ (current_buffer->selective_display, Qnil)
1124 || detect_input_pending ()
1125 || !NILP (Vexecuting_macro));
1126 value = internal_self_insert (c, 0);
1127 if (value)
1128 lose = 1;
1129 if (value == 2)
1130 nonundocount = 0;
1132 if (!lose
1133 && (PT == ZV || FETCH_CHAR (PT) == '\n'))
1135 struct Lisp_Vector *dp
1136 = window_display_table (XWINDOW (selected_window));
1137 int lose = c;
1139 if (dp)
1141 Lisp_Object obj;
1143 obj = DISP_CHAR_VECTOR (dp, lose);
1144 if (NILP (obj))
1146 /* Do it only for char codes
1147 that by default display as themselves. */
1148 if (lose >= 0x20 && lose <= 0x7e)
1149 no_redisplay = direct_output_for_insert (lose);
1151 else if (VECTORP (obj)
1152 && XVECTOR (obj)->size == 1
1153 && (obj = XVECTOR (obj)->contents[0],
1154 INTEGERP (obj))
1155 /* Insist face not specified in glyph. */
1156 && (XINT (obj) & ((-1) << 8)) == 0)
1157 no_redisplay
1158 = direct_output_for_insert (XINT (obj));
1160 else
1162 if (lose >= 0x20 && lose <= 0x7e)
1163 no_redisplay = direct_output_for_insert (lose);
1166 goto directly_done;
1170 /* Here for a command that isn't executed directly */
1172 nonundocount = 0;
1173 if (NILP (current_perdisplay->Vprefix_arg))
1174 Fundo_boundary ();
1175 Fcommand_execute (this_command, Qnil);
1178 directly_done: ;
1180 /* Note that the value cell will never directly contain nil
1181 if the symbol is a local variable. */
1182 if (!NILP (XSYMBOL (Qpost_command_hook)->value) && !NILP (Vrun_hooks))
1183 safe_run_hooks (Qpost_command_hook);
1185 if (!NILP (Vdeferred_action_list))
1186 safe_run_hooks (Qdeferred_action_function);
1188 /* If there is a prefix argument,
1189 1) We don't want last_command to be ``universal-argument''
1190 (that would be dumb), so don't set last_command,
1191 2) we want to leave echoing on so that the prefix will be
1192 echoed as part of this key sequence, so don't call
1193 cancel_echoing, and
1194 3) we want to leave this_command_key_count non-zero, so that
1195 read_char will realize that it is re-reading a character, and
1196 not echo it a second time. */
1197 if (NILP (current_perdisplay->Vprefix_arg))
1199 last_command = this_command;
1200 cancel_echoing ();
1201 this_command_key_count = 0;
1204 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1206 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1208 current_buffer->mark_active = Qnil;
1209 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1211 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1212 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1215 finalize:
1216 /* Install chars successfully executed in kbd macro. */
1218 if (defining_kbd_macro && NILP (current_perdisplay->Vprefix_arg))
1219 finalize_kbd_macro_chars ();
1221 #ifdef MULTI_PERDISPLAY
1222 current_perdisplay = global_perdisplay;
1223 #endif
1227 /* If we get an error while running the hook, cause the hook variable
1228 to be nil. Also inhibit quits, so that C-g won't cause the hook
1229 to mysteriously evaporate. */
1230 static void
1231 safe_run_hooks (hook)
1232 Lisp_Object hook;
1234 Lisp_Object value;
1235 int count = specpdl_ptr - specpdl;
1236 specbind (Qinhibit_quit, Qt);
1238 /* We read and set the variable with functions,
1239 in case it's buffer-local. */
1240 value = Vcommand_hook_internal = Fsymbol_value (hook);
1241 Fset (hook, Qnil);
1242 call1 (Vrun_hooks, Qcommand_hook_internal);
1243 Fset (hook, value);
1245 unbind_to (count, Qnil);
1248 /* Number of seconds between polling for input. */
1249 int polling_period;
1251 /* Nonzero means polling for input is temporarily suppressed. */
1252 int poll_suppress_count;
1254 /* Nonzero if polling_for_input is actually being used. */
1255 int polling_for_input;
1257 #ifdef POLL_FOR_INPUT
1259 /* Handle an alarm once each second and read pending input
1260 so as to handle a C-g if it comces in. */
1262 SIGTYPE
1263 input_poll_signal ()
1265 if (interrupt_input_blocked == 0
1266 && !waiting_for_input)
1267 read_avail_input (0);
1268 signal (SIGALRM, input_poll_signal);
1269 alarm (polling_period);
1272 #endif
1274 /* Begin signals to poll for input, if they are appropriate.
1275 This function is called unconditionally from various places. */
1277 start_polling ()
1279 #ifdef POLL_FOR_INPUT
1280 if (read_socket_hook && !interrupt_input)
1282 poll_suppress_count--;
1283 if (poll_suppress_count == 0)
1285 signal (SIGALRM, input_poll_signal);
1286 polling_for_input = 1;
1287 alarm (polling_period);
1290 #endif
1293 /* Nonzero if we are using polling to handle input asynchronously. */
1296 input_polling_used ()
1298 #ifdef POLL_FOR_INPUT
1299 return read_socket_hook && !interrupt_input;
1300 #else
1301 return 0;
1302 #endif
1305 /* Turn off polling. */
1307 stop_polling ()
1309 #ifdef POLL_FOR_INPUT
1310 if (read_socket_hook && !interrupt_input)
1312 if (poll_suppress_count == 0)
1314 polling_for_input = 0;
1315 alarm (0);
1317 poll_suppress_count++;
1319 #endif
1322 /* Set the value of poll_suppress_count to COUNT
1323 and start or stop polling accordingly. */
1325 void
1326 set_poll_suppress_count (count)
1327 int count;
1329 #ifdef POLL_FOR_INPUT
1330 if (count == 0 && poll_suppress_count != 0)
1332 poll_suppress_count = 1;
1333 start_polling ();
1335 else if (count != 0 && poll_suppress_count == 0)
1337 stop_polling ();
1339 poll_suppress_count = count;
1340 #endif
1343 /* Bind polling_period to a value at least N.
1344 But don't decrease it. */
1346 bind_polling_period (n)
1347 int n;
1349 #ifdef POLL_FOR_INPUT
1350 int new = polling_period;
1352 if (n > new)
1353 new = n;
1355 stop_polling ();
1356 specbind (Qpolling_period, make_number (new));
1357 /* Start a new alarm with the new period. */
1358 start_polling ();
1359 #endif
1362 /* Applying the control modifier to CHARACTER. */
1364 make_ctrl_char (c)
1365 int c;
1367 /* Save the upper bits here. */
1368 int upper = c & ~0177;
1370 c &= 0177;
1372 /* Everything in the columns containing the upper-case letters
1373 denotes a control character. */
1374 if (c >= 0100 && c < 0140)
1376 int oc = c;
1377 c &= ~0140;
1378 /* Set the shift modifier for a control char
1379 made from a shifted letter. But only for letters! */
1380 if (oc >= 'A' && oc <= 'Z')
1381 c |= shift_modifier;
1384 /* The lower-case letters denote control characters too. */
1385 else if (c >= 'a' && c <= 'z')
1386 c &= ~0140;
1388 /* Include the bits for control and shift
1389 only if the basic ASCII code can't indicate them. */
1390 else if (c >= ' ')
1391 c |= ctrl_modifier;
1393 /* Replace the high bits. */
1394 c |= (upper & ~ctrl_modifier);
1396 return c;
1401 /* Input of single characters from keyboard */
1403 Lisp_Object print_help ();
1404 static Lisp_Object kbd_buffer_get_event ();
1405 static void record_char ();
1407 /* read a character from the keyboard; call the redisplay if needed */
1408 /* commandflag 0 means do not do auto-saving, but do do redisplay.
1409 -1 means do not do redisplay, but do do autosaving.
1410 1 means do both. */
1412 /* The arguments MAPS and NMAPS are for menu prompting.
1413 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1415 PREV_EVENT is the previous input event, or nil if we are reading
1416 the first event of a key sequence.
1418 If USED_MOUSE_MENU is non-zero, then we set *USED_MOUSE_MENU to 1
1419 if we used a mouse menu to read the input, or zero otherwise. If
1420 USED_MOUSE_MENU is zero, *USED_MOUSE_MENU is left alone.
1422 Value is t if we showed a menu and the user rejected it. */
1424 Lisp_Object
1425 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
1426 int commandflag;
1427 int nmaps;
1428 Lisp_Object *maps;
1429 Lisp_Object prev_event;
1430 int *used_mouse_menu;
1432 register Lisp_Object c;
1433 int count;
1434 jmp_buf save_jump;
1435 int key_already_recorded = 0;
1436 Lisp_Object also_record;
1437 also_record = Qnil;
1439 if (CONSP (Vunread_command_events))
1441 c = XCONS (Vunread_command_events)->car;
1442 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
1444 if (this_command_key_count == 0)
1445 goto reread_first;
1446 else
1447 goto reread;
1450 if (unread_command_char != -1)
1452 XSETINT (c, unread_command_char);
1453 unread_command_char = -1;
1455 if (this_command_key_count == 0)
1456 goto reread_first;
1457 else
1458 goto reread;
1461 if (!NILP (Vexecuting_macro))
1463 #ifdef MULTI_FRAME
1464 /* We set this to Qmacro; since that's not a frame, nobody will
1465 try to switch frames on us, and the selected window will
1466 remain unchanged.
1468 Since this event came from a macro, it would be misleading to
1469 leave internal_last_event_frame set to wherever the last
1470 real event came from. Normally, a switch-frame event selects
1471 internal_last_event_frame after each command is read, but
1472 events read from a macro should never cause a new frame to be
1473 selected. */
1474 if (!current_perdisplay)
1475 abort ();
1476 current_perdisplay->internal_last_event_frame = Qmacro;
1477 current_perdisplay->Vlast_event_frame = Qmacro;
1478 #endif
1480 /* Exit the macro if we are at the end.
1481 Also, some things replace the macro with t
1482 to force an early exit. */
1483 if (EQ (Vexecuting_macro, Qt)
1484 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
1486 XSETINT (c, -1);
1487 return c;
1490 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
1491 if (STRINGP (Vexecuting_macro)
1492 && (XINT (c) & 0x80))
1493 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
1495 executing_macro_index++;
1497 goto from_macro;
1500 if (!NILP (unread_switch_frame))
1502 c = unread_switch_frame;
1503 unread_switch_frame = Qnil;
1505 /* This event should make it into this_command_keys, and get echoed
1506 again, so we go to reread_first, rather than reread. */
1507 goto reread_first;
1510 /* Don't bother updating menu bars while doing mouse tracking.
1511 We get events very rapidly then, and the menu bar won't be changing.
1512 We do update the menu bar once on entry to Ftrack_mouse. */
1513 if (commandflag > 0 && !input_pending && !detect_input_pending ())
1514 prepare_menu_bars ();
1516 /* Save outer setjmp data, in case called recursively. */
1517 save_getcjmp (save_jump);
1519 stop_polling ();
1521 if (commandflag >= 0 && !input_pending && !detect_input_pending ())
1522 redisplay ();
1524 if (_setjmp (getcjmp))
1526 XSETINT (c, quit_char);
1527 #ifdef MULTI_FRAME
1528 XSETFRAME (current_perdisplay->internal_last_event_frame,
1529 selected_frame);
1530 current_perdisplay->Vlast_event_frame
1531 = current_perdisplay->internal_last_event_frame;
1532 #endif
1533 /* If we report the quit char as an event,
1534 don't do so more than once. */
1535 if (!NILP (Vinhibit_quit))
1536 Vquit_flag = Qnil;
1538 goto non_reread;
1541 /* Message turns off echoing unless more keystrokes turn it on again. */
1542 if (echo_area_glyphs && *echo_area_glyphs
1543 && echo_area_glyphs != current_perdisplay->echobuf)
1544 cancel_echoing ();
1545 else
1546 /* If already echoing, continue. */
1547 echo_dash ();
1549 /* Try reading a character via menu prompting in the minibuf.
1550 Try this before the sit-for, because the sit-for
1551 would do the wrong thing if we are supposed to do
1552 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
1553 after a mouse event so don't try a minibuf menu. */
1554 c = Qnil;
1555 if (nmaps > 0 && INTERACTIVE
1556 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
1557 /* Don't bring up a menu if we already have another event. */
1558 && NILP (Vunread_command_events)
1559 && unread_command_char < 0
1560 && !detect_input_pending ())
1562 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
1563 if (! NILP (c))
1565 key_already_recorded = 1;
1566 goto non_reread;
1570 /* If in middle of key sequence and minibuffer not active,
1571 start echoing if enough time elapses. */
1572 if (minibuf_level == 0 && !current_perdisplay->immediate_echo
1573 && this_command_key_count > 0
1574 && ! noninteractive
1575 && echo_keystrokes > 0
1576 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0))
1578 Lisp_Object tem0;
1580 /* After a mouse event, start echoing right away.
1581 This is because we are probably about to display a menu,
1582 and we don't want to delay before doing so. */
1583 if (EVENT_HAS_PARAMETERS (prev_event))
1584 echo ();
1585 else
1587 tem0 = sit_for (echo_keystrokes, 0, 1, 1);
1588 if (EQ (tem0, Qt))
1589 echo ();
1593 /* Maybe auto save due to number of keystrokes or idle time. */
1595 if (commandflag != 0
1596 && auto_save_interval > 0
1597 && num_nonmacro_input_chars - last_auto_save > max (auto_save_interval, 20)
1598 && !detect_input_pending ())
1600 jmp_buf temp;
1601 save_getcjmp (temp);
1602 Fdo_auto_save (Qnil, Qnil);
1603 /* Hooks can actually change some buffers in auto save. */
1604 redisplay ();
1605 restore_getcjmp (temp);
1608 /* Try reading using an X menu.
1609 This is never confused with reading using the minibuf
1610 because the recursive call of read_char in read_char_minibuf_menu_prompt
1611 does not pass on any keymaps. */
1612 if (nmaps > 0 && INTERACTIVE
1613 && !NILP (prev_event) && EVENT_HAS_PARAMETERS (prev_event)
1614 /* Don't bring up a menu if we already have another event. */
1615 && NILP (Vunread_command_events)
1616 && unread_command_char < 0)
1617 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
1619 /* Slow down auto saves logarithmically in size of current buffer,
1620 and garbage collect while we're at it. */
1621 if (INTERACTIVE && NILP (c))
1623 int delay_level, buffer_size;
1625 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
1626 last_non_minibuf_size = Z - BEG;
1627 buffer_size = (last_non_minibuf_size >> 8) + 1;
1628 delay_level = 0;
1629 while (buffer_size > 64)
1630 delay_level++, buffer_size -= buffer_size >> 2;
1631 if (delay_level < 4) delay_level = 4;
1632 /* delay_level is 4 for files under around 50k, 7 at 100k,
1633 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
1635 /* Auto save if enough time goes by without input. */
1636 if (commandflag != 0
1637 && num_nonmacro_input_chars > last_auto_save
1638 && INTEGERP (Vauto_save_timeout)
1639 && XINT (Vauto_save_timeout) > 0)
1641 Lisp_Object tem0;
1642 int delay = delay_level * XFASTINT (Vauto_save_timeout) / 4;
1643 tem0 = sit_for (delay, 0, 1, 1);
1644 if (EQ (tem0, Qt))
1646 jmp_buf temp;
1647 save_getcjmp (temp);
1648 Fdo_auto_save (Qnil, Qnil);
1649 restore_getcjmp (temp);
1651 /* If we have auto-saved and there is still no input
1652 available, garbage collect if there has been enough
1653 consing going on to make it worthwhile. */
1654 if (!detect_input_pending ()
1655 && consing_since_gc > gc_cons_threshold / 2)
1656 Fgarbage_collect ();
1657 /* prepare_menu_bars isn't safe here, but it should
1658 also be unnecessary. */
1659 redisplay ();
1664 /* Actually read a character, waiting if necessary. */
1665 while (NILP (c))
1667 c = kbd_buffer_get_event ();
1668 if (!NILP (c))
1669 break;
1670 if (commandflag >= 0 && !input_pending && !detect_input_pending ())
1672 prepare_menu_bars ();
1673 redisplay ();
1677 /* Terminate Emacs in batch mode if at eof. */
1678 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
1679 Fkill_emacs (make_number (1));
1681 if (INTEGERP (c))
1683 /* Add in any extra modifiers, where appropriate. */
1684 if ((extra_keyboard_modifiers & CHAR_CTL)
1685 || ((extra_keyboard_modifiers & 0177) < ' '
1686 && (extra_keyboard_modifiers & 0177) != 0))
1687 XSETINT (c, make_ctrl_char (XINT (c)));
1689 /* Transfer any other modifier bits directly from
1690 extra_keyboard_modifiers to c. Ignore the actual character code
1691 in the low 16 bits of extra_keyboard_modifiers. */
1692 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
1695 non_reread:
1697 restore_getcjmp (save_jump);
1699 start_polling ();
1701 /* Buffer switch events are only for internal wakeups
1702 so don't show them to the user. */
1703 if (BUFFERP (c))
1704 return c;
1706 if (key_already_recorded)
1707 return c;
1709 /* Wipe the echo area. */
1710 echo_area_glyphs = 0;
1712 /* Handle things that only apply to characters. */
1713 if (INTEGERP (c))
1715 /* If kbd_buffer_get_event gave us an EOF, return that. */
1716 if (XINT (c) == -1)
1717 return c;
1719 if (STRINGP (Vkeyboard_translate_table)
1720 && XSTRING (Vkeyboard_translate_table)->size > XFASTINT (c))
1721 XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[XFASTINT (c)]);
1724 /* If this event is a mouse click in the menu bar,
1725 return just menu-bar for now. Modify the mouse click event
1726 so we won't do this twice, then queue it up. */
1727 if (EVENT_HAS_PARAMETERS (c)
1728 && CONSP (XCONS (c)->cdr)
1729 && CONSP (EVENT_START (c))
1730 && CONSP (XCONS (EVENT_START (c))->cdr))
1732 Lisp_Object posn;
1734 posn = POSN_BUFFER_POSN (EVENT_START (c));
1735 /* Handle menu-bar events:
1736 insert the dummy prefix event `menu-bar'. */
1737 if (EQ (posn, Qmenu_bar))
1739 /* Change menu-bar to (menu-bar) as the event "position". */
1740 POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
1742 also_record = c;
1743 Vunread_command_events = Fcons (c, Vunread_command_events);
1744 c = posn;
1748 record_char (c);
1749 if (! NILP (also_record))
1750 record_char (also_record);
1752 from_macro:
1753 reread_first:
1755 /* Don't echo mouse motion events. */
1756 if (echo_keystrokes
1757 && ! (EVENT_HAS_PARAMETERS (c)
1758 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
1760 echo_char (c);
1761 if (! NILP (also_record))
1762 echo_char (also_record);
1765 /* Record this character as part of the current key. */
1766 add_command_key (c);
1767 if (! NILP (also_record))
1768 add_command_key (also_record);
1770 /* Re-reading in the middle of a command */
1771 reread:
1772 last_input_char = c;
1773 num_input_chars++;
1775 /* Process the help character specially if enabled */
1776 if (EQ (c, Vhelp_char) && !NILP (Vhelp_form))
1778 Lisp_Object tem0;
1779 count = specpdl_ptr - specpdl;
1781 record_unwind_protect (Fset_window_configuration,
1782 Fcurrent_window_configuration (Qnil));
1784 tem0 = Feval (Vhelp_form);
1785 if (STRINGP (tem0))
1786 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
1788 cancel_echoing ();
1790 c = read_char (0, 0, 0, Qnil, 0);
1791 while (BUFFERP (c));
1792 /* Remove the help from the frame */
1793 unbind_to (count, Qnil);
1794 prepare_menu_bars ();
1795 redisplay ();
1796 if (EQ (c, make_number (040)))
1798 cancel_echoing ();
1800 c = read_char (0, 0, 0, Qnil, 0);
1801 while (BUFFERP (c));
1805 return c;
1808 /* Record the input event C in various ways. */
1810 static void
1811 record_char (c)
1812 Lisp_Object c;
1814 total_keys++;
1815 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
1816 if (++recent_keys_index >= NUM_RECENT_KEYS)
1817 recent_keys_index = 0;
1819 /* Write c to the dribble file. If c is a lispy event, write
1820 the event's symbol to the dribble file, in <brackets>. Bleaugh.
1821 If you, dear reader, have a better idea, you've got the source. :-) */
1822 if (dribble)
1824 if (INTEGERP (c))
1826 if (XUINT (c) < 0x100)
1827 putc (XINT (c), dribble);
1828 else
1829 fprintf (dribble, " 0x%x", XUINT (c));
1831 else
1833 Lisp_Object dribblee;
1835 /* If it's a structured event, take the event header. */
1836 dribblee = EVENT_HEAD (c);
1838 if (SYMBOLP (dribblee))
1840 putc ('<', dribble);
1841 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
1842 XSYMBOL (dribblee)->name->size,
1843 dribble);
1844 putc ('>', dribble);
1848 fflush (dribble);
1851 store_kbd_macro_char (c);
1853 num_nonmacro_input_chars++;
1856 Lisp_Object
1857 print_help (object)
1858 Lisp_Object object;
1860 struct buffer *old = current_buffer;
1861 Fprinc (object, Qnil);
1862 set_buffer_internal (XBUFFER (Vstandard_output));
1863 call0 (intern ("help-mode"));
1864 set_buffer_internal (old);
1865 return Qnil;
1868 /* Copy out or in the info on where C-g should throw to.
1869 This is used when running Lisp code from within get_char,
1870 in case get_char is called recursively.
1871 See read_process_output. */
1873 save_getcjmp (temp)
1874 jmp_buf temp;
1876 bcopy (getcjmp, temp, sizeof getcjmp);
1879 restore_getcjmp (temp)
1880 jmp_buf temp;
1882 bcopy (temp, getcjmp, sizeof getcjmp);
1886 #ifdef HAVE_MOUSE
1888 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
1889 of this function. */
1891 static Lisp_Object
1892 tracking_off (old_value)
1893 Lisp_Object old_value;
1895 do_mouse_tracking = old_value;
1896 if (NILP (old_value))
1898 /* Redisplay may have been preempted because there was input
1899 available, and it assumes it will be called again after the
1900 input has been processed. If the only input available was
1901 the sort that we have just disabled, then we need to call
1902 redisplay. */
1903 if (!readable_events ())
1905 prepare_menu_bars ();
1906 redisplay_preserve_echo_area ();
1907 get_input_pending (&input_pending);
1912 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
1913 "Evaluate BODY with mouse movement events enabled.\n\
1914 Within a `track-mouse' form, mouse motion generates input events that\n\
1915 you can read with `read-event'.\n\
1916 Normally, mouse motion is ignored.")
1917 (args)
1918 Lisp_Object args;
1920 int count = specpdl_ptr - specpdl;
1921 Lisp_Object val;
1923 record_unwind_protect (tracking_off, do_mouse_tracking);
1925 if (!input_pending && !detect_input_pending ())
1926 prepare_menu_bars ();
1928 XSETFRAME (do_mouse_tracking, selected_frame);
1930 val = Fprogn (args);
1931 return unbind_to (count, val);
1934 #endif /* HAVE_MOUSE */
1936 /* Low level keyboard/mouse input.
1937 kbd_buffer_store_event places events in kbd_buffer, and
1938 kbd_buffer_get_event retrieves them.
1939 mouse_moved indicates when the mouse has moved again, and
1940 *mouse_position_hook provides the mouse position. */
1942 static PERDISPLAY *
1943 find_active_event_queue (check_mouse)
1944 int check_mouse;
1946 PERDISPLAY *perd;
1948 for (perd = all_perdisplays; perd; perd = perd->next_perdisplay)
1950 if (perd->kbd_fetch_ptr != perd->kbd_store_ptr)
1951 return perd;
1952 #ifdef HAVE_MOUSE
1953 if (check_mouse && FRAMEP (do_mouse_tracking) && mouse_moved)
1954 return perd;
1955 #endif
1957 return 0;
1960 /* Return true iff there are any events in the queue that read-char
1961 would return. If this returns false, a read-char would block. */
1962 static int
1963 readable_events ()
1965 return find_active_event_queue (1) != NULL;
1968 /* Set this for debugging, to have a way to get out */
1969 int stop_character;
1971 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
1973 void
1974 kbd_buffer_store_event (event)
1975 register struct input_event *event;
1977 PERDISPLAY *perd = get_perdisplay (XFRAME (event->frame_or_window));
1979 if (event->kind == no_event)
1980 abort ();
1982 if (event->kind == ascii_keystroke)
1984 register int c = event->code & 0377;
1986 if (event->modifiers & ctrl_modifier)
1987 c = make_ctrl_char (c);
1989 c |= (event->modifiers
1990 & (meta_modifier | alt_modifier
1991 | hyper_modifier | super_modifier));
1993 if (c == quit_char)
1995 extern SIGTYPE interrupt_signal ();
1997 #ifdef MULTI_FRAME
1998 /* If this results in a quit_char being returned to Emacs as
1999 input, set Vlast_event_frame properly. If this doesn't
2000 get returned to Emacs as an event, the next event read
2001 will set Vlast_event_frame again, so this is safe to do. */
2003 Lisp_Object focus;
2005 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
2006 if (NILP (focus))
2007 perd->internal_last_event_frame = event->frame_or_window;
2008 else
2009 perd->internal_last_event_frame = focus;
2010 perd->Vlast_event_frame = perd->internal_last_event_frame;
2012 #endif
2014 last_event_timestamp = event->timestamp;
2015 interrupt_signal ();
2016 return;
2019 if (c && c == stop_character)
2021 sys_suspend ();
2022 return;
2026 if (perd->kbd_store_ptr - perd->kbd_buffer == KBD_BUFFER_SIZE)
2027 perd->kbd_store_ptr = perd->kbd_buffer;
2029 /* Don't let the very last slot in the buffer become full,
2030 since that would make the two pointers equal,
2031 and that is indistinguishable from an empty buffer.
2032 Discard the event if it would fill the last slot. */
2033 if (perd->kbd_fetch_ptr - 1 != perd->kbd_store_ptr)
2035 volatile struct input_event *sp = perd->kbd_store_ptr;
2036 sp->kind = event->kind;
2037 if (event->kind == selection_request_event)
2039 /* We must not use the ordinary copying code for this case,
2040 since `part' is an enum and copying it might not copy enough
2041 in this case. */
2042 bcopy (event, (char *) sp, sizeof (*event));
2044 else
2046 sp->code = event->code;
2047 sp->part = event->part;
2048 sp->frame_or_window = event->frame_or_window;
2049 sp->modifiers = event->modifiers;
2050 sp->x = event->x;
2051 sp->y = event->y;
2052 sp->timestamp = event->timestamp;
2054 (XVECTOR (perd->kbd_buffer_frame_or_window)->contents[perd->kbd_store_ptr
2055 - perd->kbd_buffer]
2056 = event->frame_or_window);
2058 perd->kbd_store_ptr++;
2062 /* Read one event from the event buffer, waiting if necessary.
2063 The value is a Lisp object representing the event.
2064 The value is nil for an event that should be ignored,
2065 or that was handled here.
2066 We always read and discard one event. */
2068 static Lisp_Object
2069 kbd_buffer_get_event ()
2071 PERDISPLAY *perd;
2072 register int c;
2073 Lisp_Object obj;
2075 if (noninteractive)
2077 c = getchar ();
2078 XSETINT (obj, c);
2079 return obj;
2082 /* Wait until there is input available. */
2083 for (;;)
2085 perd = find_active_event_queue (1);
2086 if (perd)
2087 break;
2089 /* If the quit flag is set, then read_char will return
2090 quit_char, so that counts as "available input." */
2091 if (!NILP (Vquit_flag))
2092 quit_throw_to_read_char ();
2094 /* One way or another, wait until input is available; then, if
2095 interrupt handlers have not read it, read it now. */
2097 #ifdef OLDVMS
2098 wait_for_kbd_input ();
2099 #else
2100 /* Note SIGIO has been undef'd if FIONREAD is missing. */
2101 #ifdef SIGIO
2102 gobble_input (0);
2103 #endif /* SIGIO */
2104 perd = find_active_event_queue (1);
2105 if (!perd)
2107 Lisp_Object minus_one;
2109 XSETINT (minus_one, -1);
2110 wait_reading_process_input (0, 0, minus_one, 1);
2112 if (!interrupt_input && find_active_event_queue (0) == NULL)
2113 /* Pass 1 for EXPECT since we just waited to have input. */
2114 read_avail_input (1);
2116 #endif /* not VMS */
2119 /* At this point, we know that there is a readable event available
2120 somewhere. If the event queue is empty, then there must be a
2121 mouse movement enabled and available. */
2122 if (perd->kbd_fetch_ptr != perd->kbd_store_ptr)
2124 struct input_event *event;
2126 event = ((perd->kbd_fetch_ptr < perd->kbd_buffer + KBD_BUFFER_SIZE)
2127 ? perd->kbd_fetch_ptr
2128 : perd->kbd_buffer);
2130 last_event_timestamp = event->timestamp;
2132 obj = Qnil;
2134 /* These two kinds of events get special handling
2135 and don't actually appear to the command loop.
2136 We return nil for them. */
2137 if (event->kind == selection_request_event)
2139 #ifdef HAVE_X11
2140 struct input_event copy = *event;
2141 /* Remove it from the buffer before processing it,
2142 since otherwise swallow_events will see it
2143 and process it again. */
2144 perd->kbd_fetch_ptr = event + 1;
2145 x_handle_selection_request (&copy);
2146 #else
2147 /* We're getting selection request events, but we don't have
2148 a window system. */
2149 abort ();
2150 #endif
2153 else if (event->kind == selection_clear_event)
2155 #ifdef HAVE_X11
2156 x_handle_selection_clear (event);
2157 perd->kbd_fetch_ptr = event + 1;
2158 #else
2159 /* We're getting selection request events, but we don't have
2160 a window system. */
2161 abort ();
2162 #endif
2164 #ifdef HAVE_X11
2165 else if (event->kind == delete_window_event)
2167 /* Make an event (delete-frame (FRAME)). */
2168 obj = Fcons (event->frame_or_window, Qnil);
2169 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
2170 perd->kbd_fetch_ptr = event + 1;
2172 else if (event->kind == iconify_event)
2174 /* Make an event (iconify-frame (FRAME)). */
2175 obj = Fcons (event->frame_or_window, Qnil);
2176 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
2177 perd->kbd_fetch_ptr = event + 1;
2179 else if (event->kind == deiconify_event)
2181 /* Make an event (make-frame-visible (FRAME)). */
2182 obj = Fcons (event->frame_or_window, Qnil);
2183 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
2184 perd->kbd_fetch_ptr = event + 1;
2186 #endif
2187 else if (event->kind == menu_bar_event)
2189 /* The event value is in the frame_or_window slot. */
2190 obj = event->frame_or_window;
2191 perd->kbd_fetch_ptr = event + 1;
2193 else if (event->kind == buffer_switch_event)
2195 /* The value doesn't matter here; only the type is tested. */
2196 XSETBUFFER (obj, current_buffer);
2197 perd->kbd_fetch_ptr = event + 1;
2199 /* Just discard these, by returning nil.
2200 (They shouldn't be found in the buffer,
2201 but on some machines it appears they do show up.) */
2202 else if (event->kind == no_event)
2203 perd->kbd_fetch_ptr = event + 1;
2205 /* If this event is on a different frame, return a switch-frame this
2206 time, and leave the event in the queue for next time. */
2207 else
2209 #ifdef MULTI_FRAME
2210 Lisp_Object frame;
2211 Lisp_Object focus;
2213 frame = event->frame_or_window;
2214 if (WINDOWP (frame))
2215 frame = WINDOW_FRAME (XWINDOW (frame));
2217 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
2218 if (! NILP (focus))
2219 frame = focus;
2221 if (! EQ (frame, perd->internal_last_event_frame)
2222 && XFRAME (frame) != selected_frame)
2223 obj = make_lispy_switch_frame (frame);
2224 perd->internal_last_event_frame = frame;
2225 #endif /* MULTI_FRAME */
2227 /* If we didn't decide to make a switch-frame event, go ahead
2228 and build a real event from the queue entry. */
2230 if (NILP (obj))
2232 obj = make_lispy_event (event);
2234 /* Wipe out this event, to catch bugs. */
2235 event->kind = no_event;
2236 XVECTOR (perd->kbd_buffer_frame_or_window)->contents[event - perd->kbd_buffer] = Qnil;
2238 perd->kbd_fetch_ptr = event + 1;
2242 #ifdef HAVE_MOUSE
2243 /* Try generating a mouse motion event. */
2244 else if (FRAMEP (do_mouse_tracking) && mouse_moved)
2246 FRAME_PTR f = XFRAME (do_mouse_tracking);
2247 Lisp_Object bar_window;
2248 enum scroll_bar_part part;
2249 Lisp_Object x, y;
2250 unsigned long time;
2252 /* Note that this uses F to determine which display to look at.
2253 If there is no valid info, it does not store anything
2254 so x remains nil. */
2255 x = Qnil;
2256 (*mouse_position_hook) (&f, &bar_window, &part, &x, &y, &time);
2258 obj = Qnil;
2260 #ifdef MULTI_FRAME
2261 /* Decide if we should generate a switch-frame event. Don't
2262 generate switch-frame events for motion outside of all Emacs
2263 frames. */
2264 if (!NILP (x) && f)
2266 Lisp_Object frame;
2268 frame = FRAME_FOCUS_FRAME (f);
2269 if (NILP (frame))
2270 XSETFRAME (frame, f);
2272 if (! EQ (frame, perd->internal_last_event_frame)
2273 && XFRAME (frame) != selected_frame)
2274 obj = make_lispy_switch_frame (frame);
2275 perd->internal_last_event_frame = frame;
2277 #endif
2279 /* If we didn't decide to make a switch-frame event, go ahead and
2280 return a mouse-motion event. */
2281 if (!NILP (x) && NILP (obj))
2282 obj = make_lispy_movement (f, bar_window, part, x, y, time);
2284 #endif /* HAVE_MOUSE */
2285 else
2286 /* We were promised by the above while loop that there was
2287 something for us to read! */
2288 abort ();
2290 input_pending = readable_events ();
2292 #ifdef MULTI_FRAME
2293 perd->Vlast_event_frame = perd->internal_last_event_frame;
2294 #endif
2296 return (obj);
2299 /* Process any events that are not user-visible,
2300 then return, without reading any user-visible events. */
2302 void
2303 swallow_events ()
2305 PERDISPLAY *perd;
2306 while ((perd = find_active_event_queue (0)) != NULL)
2308 struct input_event *event;
2310 event = ((perd->kbd_fetch_ptr < perd->kbd_buffer + KBD_BUFFER_SIZE)
2311 ? perd->kbd_fetch_ptr
2312 : perd->kbd_buffer);
2314 last_event_timestamp = event->timestamp;
2316 /* These two kinds of events get special handling
2317 and don't actually appear to the command loop. */
2318 if (event->kind == selection_request_event)
2320 #ifdef HAVE_X11
2321 struct input_event copy;
2322 copy = *event;
2323 perd->kbd_fetch_ptr = event + 1;
2324 x_handle_selection_request (&copy);
2325 #else
2326 /* We're getting selection request events, but we don't have
2327 a window system. */
2328 abort ();
2329 #endif
2332 else if (event->kind == selection_clear_event)
2334 #ifdef HAVE_X11
2335 x_handle_selection_clear (event);
2336 perd->kbd_fetch_ptr = event + 1;
2337 #else
2338 /* We're getting selection request events, but we don't have
2339 a window system. */
2340 abort ();
2341 #endif
2343 else
2344 break;
2347 get_input_pending (&input_pending);
2350 /* Caches for modify_event_symbol. */
2351 static Lisp_Object accent_key_syms;
2352 static Lisp_Object system_key_syms;
2353 static Lisp_Object func_key_syms;
2354 static Lisp_Object mouse_syms;
2356 Lisp_Object Vsystem_key_alist;
2358 /* This is a list of keysym codes for special "accent" characters.
2359 It parallels lispy_accent_keys. */
2361 static int lispy_accent_codes[] =
2363 #ifdef XK_dead_circumflex
2364 XK_dead_circumflex,
2365 #else
2367 #endif
2368 #ifdef XK_dead_grave
2369 XK_dead_grave,
2370 #else
2372 #endif
2373 #ifdef XK_dead_tilde
2374 XK_dead_tilde,
2375 #else
2377 #endif
2378 #ifdef XK_dead_diaeresis
2379 XK_dead_diaeresis,
2380 #else
2382 #endif
2383 #ifdef XK_dead_macron
2384 XK_dead_macron,
2385 #else
2387 #endif
2388 #ifdef XK_dead_degree
2389 XK_dead_degree,
2390 #else
2392 #endif
2393 #ifdef XK_dead_acute
2394 XK_dead_acute,
2395 #else
2397 #endif
2398 #ifdef XK_dead_cedilla
2399 XK_dead_cedilla,
2400 #else
2402 #endif
2403 #ifdef XK_dead_breve
2404 XK_dead_breve,
2405 #else
2407 #endif
2408 #ifdef XK_dead_ogonek
2409 XK_dead_ogonek,
2410 #else
2412 #endif
2413 #ifdef XK_dead_caron
2414 XK_dead_caron,
2415 #else
2417 #endif
2418 #ifdef XK_dead_doubleacute
2419 XK_dead_doubleacute,
2420 #else
2422 #endif
2423 #ifdef XK_dead_abovedot
2424 XK_dead_abovedot,
2425 #else
2427 #endif
2430 /* This is a list of Lisp names for special "accent" characters.
2431 It parallels lispy_accent_codes. */
2433 static char *lispy_accent_keys[] =
2435 "dead-circumflex",
2436 "dead-grave",
2437 "dead-tilde",
2438 "dead-diaeresis",
2439 "dead-macron",
2440 "dead-degree",
2441 "dead-acute",
2442 "dead-cedilla",
2443 "dead-breve",
2444 "dead-ogonek",
2445 "dead-caron",
2446 "dead-doubleacute",
2447 "dead-abovedot",
2450 /* You'll notice that this table is arranged to be conveniently
2451 indexed by X Windows keysym values. */
2452 static char *lispy_function_keys[] =
2454 /* X Keysym value */
2456 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00 */
2457 "backspace",
2458 "tab",
2459 "linefeed",
2460 "clear",
2462 "return",
2463 0, 0,
2464 0, 0, 0, /* 0xff10 */
2465 "pause",
2466 0, 0, 0, 0, 0, 0, 0,
2467 "escape",
2468 0, 0, 0, 0,
2469 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff20...2f */
2470 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff30...3f */
2471 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
2473 "home", /* 0xff50 */ /* IsCursorKey */
2474 "left",
2475 "up",
2476 "right",
2477 "down",
2478 "prior",
2479 "next",
2480 "end",
2481 "begin",
2482 0, /* 0xff59 */
2483 0, 0, 0, 0, 0, 0,
2484 "select", /* 0xff60 */ /* IsMiscFunctionKey */
2485 "print",
2486 "execute",
2487 "insert",
2488 0, /* 0xff64 */
2489 "undo",
2490 "redo",
2491 "menu",
2492 "find",
2493 "cancel",
2494 "help",
2495 "break", /* 0xff6b */
2497 0, 0, 0, 0, 0, 0, 0, 0, "backtab", 0,
2498 0, /* 0xff76 */
2499 0, 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff7f */
2500 "kp-space", /* 0xff80 */ /* IsKeypadKey */
2501 0, 0, 0, 0, 0, 0, 0, 0,
2502 "kp-tab", /* 0xff89 */
2503 0, 0, 0,
2504 "kp-enter", /* 0xff8d */
2505 0, 0, 0,
2506 "kp-f1", /* 0xff91 */
2507 "kp-f2",
2508 "kp-f3",
2509 "kp-f4",
2510 "kp-home", /* 0xff95 */
2511 "kp-left",
2512 "kp-up",
2513 "kp-right",
2514 "kp-down",
2515 "kp-prior", /* kp-page-up */
2516 "kp-next", /* kp-page-down */
2517 "kp-end",
2518 "kp-begin",
2519 "kp-insert",
2520 "kp-delete",
2521 0, /* 0xffa0 */
2522 0, 0, 0, 0, 0, 0, 0, 0, 0,
2523 "kp-multiply", /* 0xffaa */
2524 "kp-add",
2525 "kp-separator",
2526 "kp-subtract",
2527 "kp-decimal",
2528 "kp-divide", /* 0xffaf */
2529 "kp-0", /* 0xffb0 */
2530 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
2531 0, /* 0xffba */
2532 0, 0,
2533 "kp-equal", /* 0xffbd */
2534 "f1", /* 0xffbe */ /* IsFunctionKey */
2535 "f2",
2536 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
2537 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
2538 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
2539 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
2540 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
2541 0, 0, 0, 0, 0, 0, 0, 0,
2542 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
2543 0, 0, 0, 0, 0, 0, 0, "delete"
2546 static char *lispy_mouse_names[] =
2548 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
2551 /* Scroll bar parts. */
2552 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
2554 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
2555 Lisp_Object *scroll_bar_parts[] = {
2556 &Qabove_handle, &Qhandle, &Qbelow_handle
2560 /* A vector, indexed by button number, giving the down-going location
2561 of currently depressed buttons, both scroll bar and non-scroll bar.
2563 The elements have the form
2564 (BUTTON-NUMBER MODIFIER-MASK . REST)
2565 where REST is the cdr of a position as it would be reported in the event.
2567 The make_lispy_event function stores positions here to tell the
2568 difference between click and drag events, and to store the starting
2569 location to be included in drag events. */
2571 static Lisp_Object button_down_location;
2573 /* Information about the most recent up-going button event: Which
2574 button, what location, and what time. */
2576 static int last_mouse_button;
2577 static int last_mouse_x;
2578 static int last_mouse_y;
2579 static unsigned long button_down_time;
2581 /* The maximum time between clicks to make a double-click,
2582 or Qnil to disable double-click detection,
2583 or Qt for no time limit. */
2584 Lisp_Object Vdouble_click_time;
2586 /* The number of clicks in this multiple-click. */
2588 int double_click_count;
2590 #ifdef USE_X_TOOLKIT
2591 extern Lisp_Object map_event_to_object ();
2592 #endif /* USE_X_TOOLKIT */
2594 /* Given a struct input_event, build the lisp event which represents
2595 it. If EVENT is 0, build a mouse movement event from the mouse
2596 movement buffer, which should have a movement event in it.
2598 Note that events must be passed to this function in the order they
2599 are received; this function stores the location of button presses
2600 in order to build drag events when the button is released. */
2602 static Lisp_Object
2603 make_lispy_event (event)
2604 struct input_event *event;
2606 int i;
2608 switch (SWITCH_ENUM_CAST (event->kind))
2610 /* A simple keystroke. */
2611 case ascii_keystroke:
2613 Lisp_Object lispy_c;
2614 int c = event->code & 0377;
2615 /* Turn ASCII characters into control characters
2616 when proper. */
2617 if (event->modifiers & ctrl_modifier)
2618 c = make_ctrl_char (c);
2620 /* Add in the other modifier bits. We took care of ctrl_modifier
2621 just above, and the shift key was taken care of by the X code,
2622 and applied to control characters by make_ctrl_char. */
2623 c |= (event->modifiers
2624 & (meta_modifier | alt_modifier
2625 | hyper_modifier | super_modifier));
2626 button_down_time = 0;
2627 XSETFASTINT (lispy_c, c);
2628 return lispy_c;
2631 /* A function key. The symbol may need to have modifier prefixes
2632 tacked onto it. */
2633 case non_ascii_keystroke:
2634 button_down_time = 0;
2636 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
2637 if (event->code == lispy_accent_codes[i])
2638 return modify_event_symbol (i,
2639 event->modifiers,
2640 Qfunction_key, Qnil,
2641 lispy_accent_keys, &accent_key_syms,
2642 (sizeof (lispy_accent_keys)
2643 / sizeof (lispy_accent_keys[0])));
2645 /* Handle system-specific keysyms. */
2646 if (event->code & (1 << 28))
2648 /* We need to use an alist rather than a vector as the cache
2649 since we can't make a vector long enuf. */
2650 if (NILP (system_key_syms))
2651 system_key_syms = Fcons (Qnil, Qnil);
2652 return modify_event_symbol (event->code & 0xffffff,
2653 event->modifiers,
2654 Qfunction_key, Vsystem_key_alist,
2655 0, &system_key_syms, 0xffffff);
2658 return modify_event_symbol (event->code - 0xff00,
2659 event->modifiers,
2660 Qfunction_key, Qnil,
2661 lispy_function_keys, &func_key_syms,
2662 (sizeof (lispy_function_keys)
2663 / sizeof (lispy_function_keys[0])));
2664 break;
2666 #if defined (MULTI_FRAME) || defined (HAVE_MOUSE)
2667 /* A mouse click. Figure out where it is, decide whether it's
2668 a press, click or drag, and build the appropriate structure. */
2669 case mouse_click:
2670 case scroll_bar_click:
2672 int button = event->code;
2673 int is_double;
2674 Lisp_Object position;
2675 Lisp_Object *start_pos_ptr;
2676 Lisp_Object start_pos;
2678 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
2679 abort ();
2681 /* Build the position as appropriate for this mouse click. */
2682 if (event->kind == mouse_click)
2684 int part;
2685 FRAME_PTR f = XFRAME (event->frame_or_window);
2686 Lisp_Object window;
2687 Lisp_Object posn;
2688 int row, column;
2690 /* Ignore mouse events that were made on frame that
2691 have been deleted. */
2692 if (! FRAME_LIVE_P (f))
2693 return Qnil;
2695 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
2696 &column, &row, 0, 1);
2698 #ifndef USE_X_TOOLKIT
2699 /* In the non-toolkit version, clicks on the menu bar
2700 are ordinary button events in the event buffer.
2701 Distinguish them, and invoke the menu.
2703 (In the toolkit version, the toolkit handles the menu bar
2704 and Emacs doesn't know about it until after the user
2705 makes a selection.) */
2706 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f))
2708 Lisp_Object items, item;
2709 int hpos;
2710 int i;
2712 /* Activate the menu bar on the down event. If the
2713 up event comes in before the menu code can deal with it,
2714 just ignore it. */
2715 if (! (event->modifiers & down_modifier))
2716 return Qnil;
2718 item = Qnil;
2719 items = FRAME_MENU_BAR_ITEMS (f);
2720 for (i = 0; i < XVECTOR (items)->size; i += 3)
2722 Lisp_Object pos, string;
2723 string = XVECTOR (items)->contents[i + 1];
2724 pos = XVECTOR (items)->contents[i + 2];
2725 if (NILP (string))
2726 break;
2727 if (column >= XINT (pos)
2728 && column < XINT (pos) + XSTRING (string)->size)
2730 item = XVECTOR (items)->contents[i];
2731 break;
2735 position
2736 = Fcons (event->frame_or_window,
2737 Fcons (Qmenu_bar,
2738 Fcons (Fcons (event->x, event->y),
2739 Fcons (make_number (event->timestamp),
2740 Qnil))));
2742 return Fcons (item, Fcons (position, Qnil));
2744 #endif /* not USE_X_TOOLKIT */
2746 window = window_from_coordinates (f, column, row, &part);
2748 if (!WINDOWP (window))
2750 window = event->frame_or_window;
2751 posn = Qnil;
2753 else
2755 int pixcolumn, pixrow;
2756 column -= XINT (XWINDOW (window)->left);
2757 row -= XINT (XWINDOW (window)->top);
2758 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
2759 XSETINT (event->x, pixcolumn);
2760 XSETINT (event->y, pixrow);
2762 if (part == 1)
2763 posn = Qmode_line;
2764 else if (part == 2)
2765 posn = Qvertical_line;
2766 else
2767 XSETINT (posn,
2768 buffer_posn_from_coords (XWINDOW (window),
2769 column, row));
2772 position
2773 = Fcons (window,
2774 Fcons (posn,
2775 Fcons (Fcons (event->x, event->y),
2776 Fcons (make_number (event->timestamp),
2777 Qnil))));
2779 else
2781 Lisp_Object window;
2782 Lisp_Object portion_whole;
2783 Lisp_Object part;
2785 window = event->frame_or_window;
2786 portion_whole = Fcons (event->x, event->y);
2787 part = *scroll_bar_parts[(int) event->part];
2789 position =
2790 Fcons (window,
2791 Fcons (Qvertical_scroll_bar,
2792 Fcons (portion_whole,
2793 Fcons (make_number (event->timestamp),
2794 Fcons (part, Qnil)))));
2797 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
2799 start_pos = *start_pos_ptr;
2800 *start_pos_ptr = Qnil;
2802 is_double = (button == last_mouse_button
2803 && XINT (event->x) == last_mouse_x
2804 && XINT (event->y) == last_mouse_y
2805 && button_down_time != 0
2806 && (EQ (Vdouble_click_time, Qt)
2807 || (INTEGERP (Vdouble_click_time)
2808 && ((int)(event->timestamp - button_down_time)
2809 < XINT (Vdouble_click_time)))));
2810 last_mouse_button = button;
2811 last_mouse_x = XINT (event->x);
2812 last_mouse_y = XINT (event->y);
2814 /* If this is a button press, squirrel away the location, so
2815 we can decide later whether it was a click or a drag. */
2816 if (event->modifiers & down_modifier)
2818 if (is_double)
2820 double_click_count++;
2821 event->modifiers |= ((double_click_count > 2)
2822 ? triple_modifier
2823 : double_modifier);
2825 else
2826 double_click_count = 1;
2827 button_down_time = event->timestamp;
2828 *start_pos_ptr = Fcopy_alist (position);
2831 /* Now we're releasing a button - check the co-ordinates to
2832 see if this was a click or a drag. */
2833 else if (event->modifiers & up_modifier)
2835 /* If we did not see a down before this up,
2836 ignore the up. Probably this happened because
2837 the down event chose a menu item.
2838 It would be an annoyance to treat the release
2839 of the button that chose the menu item
2840 as a separate event. */
2842 if (!CONSP (start_pos))
2843 return Qnil;
2845 event->modifiers &= ~up_modifier;
2846 #if 0 /* Formerly we treated an up with no down as a click event. */
2847 if (!CONSP (start_pos))
2848 event->modifiers |= click_modifier;
2849 else
2850 #endif
2852 /* The third element of every position should be the (x,y)
2853 pair. */
2854 Lisp_Object down;
2856 down = Fnth (make_number (2), start_pos);
2857 if (EQ (event->x, XCONS (down)->car)
2858 && EQ (event->y, XCONS (down)->cdr))
2860 event->modifiers |= click_modifier;
2862 else
2864 button_down_time = 0;
2865 event->modifiers |= drag_modifier;
2867 /* Don't check is_double; treat this as multiple
2868 if the down-event was multiple. */
2869 if (double_click_count > 1)
2870 event->modifiers |= ((double_click_count > 2)
2871 ? triple_modifier
2872 : double_modifier);
2875 else
2876 /* Every mouse event should either have the down_modifier or
2877 the up_modifier set. */
2878 abort ();
2881 /* Get the symbol we should use for the mouse click. */
2882 Lisp_Object head;
2884 head = modify_event_symbol (button,
2885 event->modifiers,
2886 Qmouse_click, Qnil,
2887 lispy_mouse_names, &mouse_syms,
2888 (sizeof (lispy_mouse_names)
2889 / sizeof (lispy_mouse_names[0])));
2890 if (event->modifiers & drag_modifier)
2891 return Fcons (head,
2892 Fcons (start_pos,
2893 Fcons (position,
2894 Qnil)));
2895 else if (event->modifiers & (double_modifier | triple_modifier))
2896 return Fcons (head,
2897 Fcons (position,
2898 Fcons (make_number (double_click_count),
2899 Qnil)));
2900 else
2901 return Fcons (head,
2902 Fcons (position,
2903 Qnil));
2906 #endif /* MULTI_FRAME or HAVE_MOUSE */
2908 /* The 'kind' field of the event is something we don't recognize. */
2909 default:
2910 abort ();
2914 #if defined (MULTI_FRAME) || defined (HAVE_MOUSE)
2916 static Lisp_Object
2917 make_lispy_movement (frame, bar_window, part, x, y, time)
2918 FRAME_PTR frame;
2919 Lisp_Object bar_window;
2920 enum scroll_bar_part part;
2921 Lisp_Object x, y;
2922 unsigned long time;
2924 #ifdef MULTI_FRAME
2925 /* Is it a scroll bar movement? */
2926 if (frame && ! NILP (bar_window))
2928 Lisp_Object part_sym;
2930 part_sym = *scroll_bar_parts[(int) part];
2931 return Fcons (Qscroll_bar_movement,
2932 (Fcons (Fcons (bar_window,
2933 Fcons (Qvertical_scroll_bar,
2934 Fcons (Fcons (x, y),
2935 Fcons (make_number (time),
2936 Fcons (part_sym,
2937 Qnil))))),
2938 Qnil)));
2941 /* Or is it an ordinary mouse movement? */
2942 else
2943 #endif /* MULTI_FRAME */
2945 int area;
2946 Lisp_Object window;
2947 Lisp_Object posn;
2948 int column, row;
2950 #ifdef MULTI_FRAME
2951 if (frame)
2952 #else
2953 if (1)
2954 #endif
2956 /* It's in a frame; which window on that frame? */
2957 pixel_to_glyph_coords (frame, XINT (x), XINT (y), &column, &row, 0, 1);
2958 window = window_from_coordinates (frame, column, row, &area);
2960 else
2961 window = Qnil;
2963 if (WINDOWP (window))
2965 int pixcolumn, pixrow;
2966 column -= XINT (XWINDOW (window)->left);
2967 row -= XINT (XWINDOW (window)->top);
2968 glyph_to_pixel_coords (frame, column, row, &pixcolumn, &pixrow);
2969 XSETINT (x, pixcolumn);
2970 XSETINT (y, pixrow);
2972 if (area == 1)
2973 posn = Qmode_line;
2974 else if (area == 2)
2975 posn = Qvertical_line;
2976 else
2977 XSETINT (posn,
2978 buffer_posn_from_coords (XWINDOW (window), column, row));
2980 #ifdef MULTI_FRAME
2981 else if (frame != 0)
2983 XSETFRAME (window, frame);
2984 posn = Qnil;
2986 #endif
2987 else
2989 window = Qnil;
2990 posn = Qnil;
2991 XSETFASTINT (x, 0);
2992 XSETFASTINT (y, 0);
2995 return Fcons (Qmouse_movement,
2996 Fcons (Fcons (window,
2997 Fcons (posn,
2998 Fcons (Fcons (x, y),
2999 Fcons (make_number (time),
3000 Qnil)))),
3001 Qnil));
3005 #endif /* neither MULTI_FRAME nor HAVE_MOUSE */
3007 /* Construct a switch frame event. */
3008 static Lisp_Object
3009 make_lispy_switch_frame (frame)
3010 Lisp_Object frame;
3012 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
3015 /* Manipulating modifiers. */
3017 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
3019 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
3020 SYMBOL's name of the end of the modifiers; the string from this
3021 position is the unmodified symbol name.
3023 This doesn't use any caches. */
3024 static int
3025 parse_modifiers_uncached (symbol, modifier_end)
3026 Lisp_Object symbol;
3027 int *modifier_end;
3029 struct Lisp_String *name;
3030 int i;
3031 int modifiers;
3033 CHECK_SYMBOL (symbol, 1);
3035 modifiers = 0;
3036 name = XSYMBOL (symbol)->name;
3039 for (i = 0; i+2 <= name->size; )
3040 switch (name->data[i])
3042 #define SINGLE_LETTER_MOD(bit) \
3043 if (name->data[i+1] != '-') \
3044 goto no_more_modifiers; \
3045 modifiers |= bit; \
3046 i += 2;
3048 case 'A':
3049 SINGLE_LETTER_MOD (alt_modifier);
3050 break;
3052 case 'C':
3053 SINGLE_LETTER_MOD (ctrl_modifier);
3054 break;
3056 case 'H':
3057 SINGLE_LETTER_MOD (hyper_modifier);
3058 break;
3060 case 'M':
3061 SINGLE_LETTER_MOD (meta_modifier);
3062 break;
3064 case 'S':
3065 SINGLE_LETTER_MOD (shift_modifier);
3066 break;
3068 case 's':
3069 SINGLE_LETTER_MOD (super_modifier);
3070 break;
3072 case 'd':
3073 if (i + 5 > name->size)
3074 goto no_more_modifiers;
3075 if (! strncmp (name->data + i, "drag-", 5))
3077 modifiers |= drag_modifier;
3078 i += 5;
3080 else if (! strncmp (name->data + i, "down-", 5))
3082 modifiers |= down_modifier;
3083 i += 5;
3085 else if (i + 7 <= name->size
3086 && ! strncmp (name->data + i, "double-", 7))
3088 modifiers |= double_modifier;
3089 i += 7;
3091 else
3092 goto no_more_modifiers;
3093 break;
3095 case 't':
3096 if (i + 7 > name->size)
3097 goto no_more_modifiers;
3098 if (! strncmp (name->data + i, "triple-", 7))
3100 modifiers |= triple_modifier;
3101 i += 7;
3103 else
3104 goto no_more_modifiers;
3105 break;
3107 default:
3108 goto no_more_modifiers;
3110 #undef SINGLE_LETTER_MOD
3112 no_more_modifiers:
3114 /* Should we include the `click' modifier? */
3115 if (! (modifiers & (down_modifier | drag_modifier
3116 | double_modifier | triple_modifier))
3117 && i + 7 == name->size
3118 && strncmp (name->data + i, "mouse-", 6) == 0
3119 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
3120 modifiers |= click_modifier;
3122 if (modifier_end)
3123 *modifier_end = i;
3125 return modifiers;
3129 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
3130 prepended to the string BASE[0..BASE_LEN-1].
3131 This doesn't use any caches. */
3132 static Lisp_Object
3133 apply_modifiers_uncached (modifiers, base, base_len)
3134 int modifiers;
3135 char *base;
3136 int base_len;
3138 /* Since BASE could contain nulls, we can't use intern here; we have
3139 to use Fintern, which expects a genuine Lisp_String, and keeps a
3140 reference to it. */
3141 char *new_mods =
3142 (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
3143 int mod_len;
3146 char *p = new_mods;
3148 /* Only the event queue may use the `up' modifier; it should always
3149 be turned into a click or drag event before presented to lisp code. */
3150 if (modifiers & up_modifier)
3151 abort ();
3153 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
3154 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
3155 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
3156 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
3157 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
3158 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
3159 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
3160 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
3161 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
3162 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
3163 /* The click modifier is denoted by the absence of other modifiers. */
3165 *p = '\0';
3167 mod_len = p - new_mods;
3171 Lisp_Object new_name;
3173 new_name = make_uninit_string (mod_len + base_len);
3174 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
3175 bcopy (base, XSTRING (new_name)->data + mod_len, base_len);
3177 return Fintern (new_name, Qnil);
3182 static char *modifier_names[] =
3184 "up", "down", "drag", "click", "double", "triple", 0, 0,
3185 0, 0, 0, 0, 0, 0, 0, 0,
3186 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
3188 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
3190 static Lisp_Object modifier_symbols;
3192 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
3193 static Lisp_Object
3194 lispy_modifier_list (modifiers)
3195 int modifiers;
3197 Lisp_Object modifier_list;
3198 int i;
3200 modifier_list = Qnil;
3201 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
3202 if (modifiers & (1<<i))
3203 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
3204 modifier_list);
3206 return modifier_list;
3210 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
3211 where UNMODIFIED is the unmodified form of SYMBOL,
3212 MASK is the set of modifiers present in SYMBOL's name.
3213 This is similar to parse_modifiers_uncached, but uses the cache in
3214 SYMBOL's Qevent_symbol_element_mask property, and maintains the
3215 Qevent_symbol_elements property. */
3216 static Lisp_Object
3217 parse_modifiers (symbol)
3218 Lisp_Object symbol;
3220 Lisp_Object elements;
3222 elements = Fget (symbol, Qevent_symbol_element_mask);
3223 if (CONSP (elements))
3224 return elements;
3225 else
3227 int end;
3228 int modifiers = parse_modifiers_uncached (symbol, &end);
3229 Lisp_Object unmodified;
3230 Lisp_Object mask;
3232 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
3233 XSYMBOL (symbol)->name->size - end),
3234 Qnil);
3236 if (modifiers & ~((1<<VALBITS) - 1))
3237 abort ();
3238 XSETFASTINT (mask, modifiers);
3239 elements = Fcons (unmodified, Fcons (mask, Qnil));
3241 /* Cache the parsing results on SYMBOL. */
3242 Fput (symbol, Qevent_symbol_element_mask,
3243 elements);
3244 Fput (symbol, Qevent_symbol_elements,
3245 Fcons (unmodified, lispy_modifier_list (modifiers)));
3247 /* Since we know that SYMBOL is modifiers applied to unmodified,
3248 it would be nice to put that in unmodified's cache.
3249 But we can't, since we're not sure that parse_modifiers is
3250 canonical. */
3252 return elements;
3256 /* Apply the modifiers MODIFIERS to the symbol BASE.
3257 BASE must be unmodified.
3259 This is like apply_modifiers_uncached, but uses BASE's
3260 Qmodifier_cache property, if present. It also builds
3261 Qevent_symbol_elements properties, since it has that info anyway.
3263 apply_modifiers copies the value of BASE's Qevent_kind property to
3264 the modified symbol. */
3265 static Lisp_Object
3266 apply_modifiers (modifiers, base)
3267 int modifiers;
3268 Lisp_Object base;
3270 Lisp_Object cache, index, entry, new_symbol;
3272 /* Mask out upper bits. We don't know where this value's been. */
3273 modifiers &= (1<<VALBITS) - 1;
3275 /* The click modifier never figures into cache indices. */
3276 cache = Fget (base, Qmodifier_cache);
3277 XSETFASTINT (index, (modifiers & ~click_modifier));
3278 entry = assq_no_quit (index, cache);
3280 if (CONSP (entry))
3281 new_symbol = XCONS (entry)->cdr;
3282 else
3284 /* We have to create the symbol ourselves. */
3285 new_symbol = apply_modifiers_uncached (modifiers,
3286 XSYMBOL (base)->name->data,
3287 XSYMBOL (base)->name->size);
3289 /* Add the new symbol to the base's cache. */
3290 entry = Fcons (index, new_symbol);
3291 Fput (base, Qmodifier_cache, Fcons (entry, cache));
3293 /* We have the parsing info now for free, so add it to the caches. */
3294 XSETFASTINT (index, modifiers);
3295 Fput (new_symbol, Qevent_symbol_element_mask,
3296 Fcons (base, Fcons (index, Qnil)));
3297 Fput (new_symbol, Qevent_symbol_elements,
3298 Fcons (base, lispy_modifier_list (modifiers)));
3301 /* Make sure this symbol is of the same kind as BASE.
3303 You'd think we could just set this once and for all when we
3304 intern the symbol above, but reorder_modifiers may call us when
3305 BASE's property isn't set right; we can't assume that just
3306 because it has a Qmodifier_cache property it must have its
3307 Qevent_kind set right as well. */
3308 if (NILP (Fget (new_symbol, Qevent_kind)))
3310 Lisp_Object kind;
3312 kind = Fget (base, Qevent_kind);
3313 if (! NILP (kind))
3314 Fput (new_symbol, Qevent_kind, kind);
3317 return new_symbol;
3321 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
3322 return a symbol with the modifiers placed in the canonical order.
3323 Canonical order is alphabetical, except for down and drag, which
3324 always come last. The 'click' modifier is never written out.
3326 Fdefine_key calls this to make sure that (for example) C-M-foo
3327 and M-C-foo end up being equivalent in the keymap. */
3329 Lisp_Object
3330 reorder_modifiers (symbol)
3331 Lisp_Object symbol;
3333 /* It's hopefully okay to write the code this way, since everything
3334 will soon be in caches, and no consing will be done at all. */
3335 Lisp_Object parsed;
3337 parsed = parse_modifiers (symbol);
3338 return apply_modifiers (XCONS (XCONS (parsed)->cdr)->car,
3339 XCONS (parsed)->car);
3343 /* For handling events, we often want to produce a symbol whose name
3344 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
3345 to some base, like the name of a function key or mouse button.
3346 modify_event_symbol produces symbols of this sort.
3348 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
3349 is the name of the i'th symbol. TABLE_SIZE is the number of elements
3350 in the table.
3352 Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
3353 NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
3355 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
3356 persist between calls to modify_event_symbol that it can use to
3357 store a cache of the symbols it's generated for this NAME_TABLE
3358 before. The object stored there may be a vector or an alist.
3360 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
3362 MODIFIERS is a set of modifier bits (as given in struct input_events)
3363 whose prefixes should be applied to the symbol name.
3365 SYMBOL_KIND is the value to be placed in the event_kind property of
3366 the returned symbol.
3368 The symbols we create are supposed to have an
3369 `event-symbol-elements' property, which lists the modifiers present
3370 in the symbol's name. */
3372 static Lisp_Object
3373 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
3374 name_table, symbol_table, table_size)
3375 int symbol_num;
3376 unsigned modifiers;
3377 Lisp_Object symbol_kind;
3378 Lisp_Object name_alist;
3379 char **name_table;
3380 Lisp_Object *symbol_table;
3381 int table_size;
3383 Lisp_Object value;
3384 Lisp_Object symbol_int;
3386 XSETINT (symbol_int, symbol_num);
3388 /* Is this a request for a valid symbol? */
3389 if (symbol_num < 0 || symbol_num >= table_size)
3390 return Qnil;
3392 if (CONSP (*symbol_table))
3393 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
3395 /* If *symbol_table doesn't seem to be initialized properly, fix that.
3396 *symbol_table should be a lisp vector TABLE_SIZE elements long,
3397 where the Nth element is the symbol for NAME_TABLE[N], or nil if
3398 we've never used that symbol before. */
3399 else
3401 if (! VECTORP (*symbol_table)
3402 || XVECTOR (*symbol_table)->size != table_size)
3404 Lisp_Object size;
3406 XSETFASTINT (size, table_size);
3407 *symbol_table = Fmake_vector (size, Qnil);
3410 value = XVECTOR (*symbol_table)->contents[symbol_num];
3413 /* Have we already used this symbol before? */
3414 if (NILP (value))
3416 /* No; let's create it. */
3417 if (!NILP (name_alist))
3418 value = Fcdr_safe (Fassq (symbol_int, name_alist));
3419 else if (name_table[symbol_num])
3420 value = intern (name_table[symbol_num]);
3422 if (NILP (value))
3424 char buf[20];
3425 sprintf (buf, "key-%d", symbol_num);
3426 value = intern (buf);
3429 if (CONSP (*symbol_table))
3430 *symbol_table = Fcons (value, *symbol_table);
3431 else
3432 XVECTOR (*symbol_table)->contents[symbol_num] = value;
3434 /* Fill in the cache entries for this symbol; this also
3435 builds the Qevent_symbol_elements property, which the user
3436 cares about. */
3437 apply_modifiers (modifiers & click_modifier, value);
3438 Fput (value, Qevent_kind, symbol_kind);
3441 /* Apply modifiers to that symbol. */
3442 return apply_modifiers (modifiers, value);
3446 /* Store into *addr a value nonzero if terminal input chars are available.
3447 Serves the purpose of ioctl (0, FIONREAD, addr)
3448 but works even if FIONREAD does not exist.
3449 (In fact, this may actually read some input.) */
3451 static void
3452 get_input_pending (addr)
3453 int *addr;
3455 /* First of all, have we already counted some input? */
3456 *addr = !NILP (Vquit_flag) || readable_events ();
3458 /* If input is being read as it arrives, and we have none, there is none. */
3459 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
3460 return;
3462 /* Try to read some input and see how much we get. */
3463 gobble_input (0);
3464 *addr = !NILP (Vquit_flag) || readable_events ();
3467 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
3470 gobble_input (expected)
3471 int expected;
3473 #ifndef VMS
3474 #ifdef SIGIO
3475 if (interrupt_input)
3477 SIGMASKTYPE mask;
3478 mask = sigblockx (SIGIO);
3479 read_avail_input (expected);
3480 sigsetmask (mask);
3482 else
3483 #ifdef POLL_FOR_INPUT
3484 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
3486 SIGMASKTYPE mask;
3487 mask = sigblockx (SIGALRM);
3488 read_avail_input (expected);
3489 sigsetmask (mask);
3491 else
3492 #endif
3493 #endif
3494 read_avail_input (expected);
3495 #endif
3498 /* Put a buffer_switch_event in the buffer
3499 so that read_key_sequence will notice the new current buffer. */
3501 record_asynch_buffer_change ()
3503 struct input_event event;
3504 Lisp_Object tem;
3506 event.kind = buffer_switch_event;
3507 event.frame_or_window = Qnil;
3509 #ifdef subprocesses
3510 /* We don't need a buffer-switch event unless Emacs is waiting for input.
3511 The purpose of the event is to make read_key_sequence look up the
3512 keymaps again. If we aren't in read_key_sequence, we don't need one,
3513 and the event could cause trouble by messing up (input-pending-p). */
3514 tem = Fwaiting_for_user_input_p ();
3515 if (NILP (tem))
3516 return;
3517 #else
3518 /* We never need these events if we have no asynchronous subprocesses. */
3519 return;
3520 #endif
3522 /* Make sure no interrupt happens while storing the event. */
3523 #ifdef SIGIO
3524 if (interrupt_input)
3526 SIGMASKTYPE mask;
3527 mask = sigblockx (SIGIO);
3528 kbd_buffer_store_event (&event);
3529 sigsetmask (mask);
3531 else
3532 #endif
3534 stop_polling ();
3535 kbd_buffer_store_event (&event);
3536 start_polling ();
3540 #ifndef VMS
3542 /* Read any terminal input already buffered up by the system
3543 into the kbd_buffer, but do not wait.
3545 EXPECTED should be nonzero if the caller knows there is some input.
3547 Except on VMS, all input is read by this function.
3548 If interrupt_input is nonzero, this function MUST be called
3549 only when SIGIO is blocked.
3551 Returns the number of keyboard chars read, or -1 meaning
3552 this is a bad time to try to read input. */
3554 static int
3555 read_avail_input (expected)
3556 int expected;
3558 struct input_event buf[KBD_BUFFER_SIZE];
3559 register int i;
3560 int nread;
3562 if (read_socket_hook)
3563 /* No need for FIONREAD or fcntl; just say don't wait. */
3564 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE,
3565 expected, expected);
3566 else
3568 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
3569 the kbd_buffer can really hold. That may prevent loss
3570 of characters on some systems when input is stuffed at us. */
3571 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
3572 int n_to_read;
3574 /* Determine how many characters we should *try* to read. */
3575 #ifdef WINDOWSNT
3576 return 0;
3577 #else /* not WINDOWSNT */
3578 #ifdef MSDOS
3579 n_to_read = dos_keysns ();
3580 if (n_to_read == 0)
3581 return 0;
3582 #else /* not MSDOS */
3583 #ifdef FIONREAD
3584 /* Find out how much input is available. */
3585 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
3586 /* Formerly simply reported no input, but that sometimes led to
3587 a failure of Emacs to terminate.
3588 SIGHUP seems appropriate if we can't reach the terminal. */
3589 /* ??? Is it really right to send the signal just to this process
3590 rather than to the whole process group?
3591 Perhaps on systems with FIONREAD Emacs is alone in its group. */
3592 kill (getpid (), SIGHUP);
3593 if (n_to_read == 0)
3594 return 0;
3595 if (n_to_read > sizeof cbuf)
3596 n_to_read = sizeof cbuf;
3597 #else /* no FIONREAD */
3598 #if defined(USG) || defined(DGUX)
3599 /* Read some input if available, but don't wait. */
3600 n_to_read = sizeof cbuf;
3601 fcntl (input_fd, F_SETFL, O_NDELAY);
3602 #else
3603 you lose;
3604 #endif
3605 #endif
3606 #endif /* not MSDOS */
3607 #endif /* not WINDOWSNT */
3609 /* Now read; for one reason or another, this will not block.
3610 NREAD is set to the number of chars read. */
3613 #ifdef MSDOS
3614 cbuf[0] = dos_keyread();
3615 nread = 1;
3616 #else
3617 nread = read (input_fd, cbuf, n_to_read);
3618 #endif
3619 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
3620 /* The kernel sometimes fails to deliver SIGHUP for ptys.
3621 This looks incorrect, but it isn't, because _BSD causes
3622 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
3623 and that causes a value other than 0 when there is no input. */
3624 if (nread == 0)
3625 kill (0, SIGHUP);
3626 #endif
3628 while (
3629 /* We used to retry the read if it was interrupted.
3630 But this does the wrong thing when O_NDELAY causes
3631 an EAGAIN error. Does anybody know of a situation
3632 where a retry is actually needed? */
3633 #if 0
3634 nread < 0 && (errno == EAGAIN
3635 #ifdef EFAULT
3636 || errno == EFAULT
3637 #endif
3638 #ifdef EBADSLT
3639 || errno == EBADSLT
3640 #endif
3642 #else
3644 #endif
3647 #ifndef FIONREAD
3648 #if defined (USG) || defined (DGUX)
3649 fcntl (input_fd, F_SETFL, 0);
3650 #endif /* USG or DGUX */
3651 #endif /* no FIONREAD */
3652 for (i = 0; i < nread; i++)
3654 buf[i].kind = ascii_keystroke;
3655 buf[i].modifiers = 0;
3656 if (meta_key == 1 && (cbuf[i] & 0x80))
3657 buf[i].modifiers = meta_modifier;
3658 if (meta_key != 2)
3659 cbuf[i] &= ~0x80;
3661 buf[i].code = cbuf[i];
3662 #ifdef MULTI_FRAME
3663 XSETFRAME (buf[i].frame_or_window, selected_frame);
3664 #else
3665 buf[i].frame_or_window = Qnil;
3666 #endif
3670 /* Scan the chars for C-g and store them in kbd_buffer. */
3671 for (i = 0; i < nread; i++)
3673 kbd_buffer_store_event (&buf[i]);
3674 /* Don't look at input that follows a C-g too closely.
3675 This reduces lossage due to autorepeat on C-g. */
3676 if (buf[i].kind == ascii_keystroke
3677 && buf[i].code == quit_char)
3678 break;
3681 return nread;
3683 #endif /* not VMS */
3685 #ifdef SIGIO /* for entire page */
3686 /* Note SIGIO has been undef'd if FIONREAD is missing. */
3688 SIGTYPE
3689 input_available_signal (signo)
3690 int signo;
3692 /* Must preserve main program's value of errno. */
3693 int old_errno = errno;
3694 #ifdef BSD4_1
3695 extern int select_alarmed;
3696 #endif
3698 #ifdef USG
3699 /* USG systems forget handlers when they are used;
3700 must reestablish each time */
3701 signal (signo, input_available_signal);
3702 #endif /* USG */
3704 #ifdef BSD4_1
3705 sigisheld (SIGIO);
3706 #endif
3708 if (input_available_clear_time)
3709 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
3711 while (1)
3713 int nread;
3714 nread = read_avail_input (1);
3715 /* -1 means it's not ok to read the input now.
3716 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
3717 0 means there was no keyboard input available. */
3718 if (nread <= 0)
3719 break;
3721 #ifdef BSD4_1
3722 select_alarmed = 1; /* Force the select emulator back to life */
3723 #endif
3726 #ifdef BSD4_1
3727 sigfree ();
3728 #endif
3729 errno = old_errno;
3731 #endif /* SIGIO */
3733 /* Send ourselves a SIGIO.
3735 This function exists so that the UNBLOCK_INPUT macro in
3736 blockinput.h can have some way to take care of input we put off
3737 dealing with, without assuming that every file which uses
3738 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
3739 void
3740 reinvoke_input_signal ()
3742 #ifdef SIGIO
3743 kill (0, SIGIO);
3744 #endif
3749 /* Return the prompt-string of a sparse keymap.
3750 This is the first element which is a string.
3751 Return nil if there is none. */
3753 Lisp_Object
3754 map_prompt (map)
3755 Lisp_Object map;
3757 while (CONSP (map))
3759 register Lisp_Object tem;
3760 tem = Fcar (map);
3761 if (STRINGP (tem))
3762 return tem;
3763 map = Fcdr (map);
3765 return Qnil;
3768 static void menu_bar_item ();
3769 static void menu_bar_one_keymap ();
3771 /* These variables hold the vector under construction within
3772 menu_bar_items and its subroutines, and the current index
3773 for storing into that vector. */
3774 static Lisp_Object menu_bar_items_vector;
3775 static int menu_bar_items_index;
3777 /* Return a vector of menu items for a menu bar, appropriate
3778 to the current buffer. Each item has three elements in the vector:
3779 KEY STRING MAPLIST.
3781 OLD is an old vector we can optionally reuse, or nil. */
3783 Lisp_Object
3784 menu_bar_items (old)
3785 Lisp_Object old;
3787 /* The number of keymaps we're scanning right now, and the number of
3788 keymaps we have allocated space for. */
3789 int nmaps;
3791 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
3792 in the current keymaps, or nil where it is not a prefix. */
3793 Lisp_Object *maps;
3795 Lisp_Object def, tem, tail;
3797 Lisp_Object result;
3799 int mapno;
3800 Lisp_Object oquit;
3802 int i;
3804 struct gcpro gcpro1;
3806 /* In order to build the menus, we need to call the keymap
3807 accessors. They all call QUIT. But this function is called
3808 during redisplay, during which a quit is fatal. So inhibit
3809 quitting while building the menus.
3810 We do this instead of specbind because (1) errors will clear it anyway
3811 and (2) this avoids risk of specpdl overflow. */
3812 oquit = Vinhibit_quit;
3813 Vinhibit_quit = Qt;
3815 if (!NILP (old))
3816 menu_bar_items_vector = old;
3817 else
3818 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
3819 menu_bar_items_index = 0;
3821 GCPRO1 (menu_bar_items_vector);
3823 /* Build our list of keymaps.
3824 If we recognize a function key and replace its escape sequence in
3825 keybuf with its symbol, or if the sequence starts with a mouse
3826 click and we need to switch buffers, we jump back here to rebuild
3827 the initial keymaps from the current buffer. */
3829 Lisp_Object *tmaps;
3831 /* Should overriding-local-map apply, here? */
3832 if (!NILP (Voverriding_local_map_menu_flag))
3834 if (NILP (Voverriding_local_map))
3836 /* Yes, and it is nil. Use just global map. */
3837 nmaps = 1;
3838 maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
3840 else
3842 /* Yes, and it is non-nil. Use it and the global map. */
3843 nmaps = 2;
3844 maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
3845 maps[0] = Voverriding_local_map;
3848 else
3850 /* No, so use major and minor mode keymaps. */
3851 nmaps = current_minor_maps (0, &tmaps) + 2;
3852 maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
3853 bcopy (tmaps, maps, (nmaps - 2) * sizeof (maps[0]));
3854 #ifdef USE_TEXT_PROPERTIES
3855 maps[nmaps-2] = get_local_map (PT, current_buffer);
3856 #else
3857 maps[nmaps-2] = current_buffer->keymap;
3858 #endif
3860 maps[nmaps-1] = current_global_map;
3863 /* Look up in each map the dummy prefix key `menu-bar'. */
3865 result = Qnil;
3867 for (mapno = nmaps - 1; mapno >= 0; mapno--)
3869 if (! NILP (maps[mapno]))
3870 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0));
3871 else
3872 def = Qnil;
3874 tem = Fkeymapp (def);
3875 if (!NILP (tem))
3876 menu_bar_one_keymap (def);
3879 /* Move to the end those items that should be at the end. */
3881 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCONS (tail)->cdr)
3883 int i;
3884 int end = menu_bar_items_index;
3886 for (i = 0; i < end; i += 3)
3887 if (EQ (XCONS (tail)->car, XVECTOR (menu_bar_items_vector)->contents[i]))
3889 Lisp_Object tem0, tem1, tem2;
3890 /* Move the item at index I to the end,
3891 shifting all the others forward. */
3892 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
3893 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
3894 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
3895 if (end > i + 3)
3896 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 3],
3897 &XVECTOR (menu_bar_items_vector)->contents[i],
3898 (end - i - 3) * sizeof (Lisp_Object));
3899 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem0;
3900 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem1;
3901 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem2;
3902 break;
3906 /* Add nil, nil, nil at the end. */
3907 i = menu_bar_items_index;
3908 if (i + 3 > XVECTOR (menu_bar_items_vector)->size)
3910 Lisp_Object tem;
3911 int newsize = 2 * i;
3912 tem = Fmake_vector (make_number (2 * i), Qnil);
3913 bcopy (XVECTOR (menu_bar_items_vector)->contents,
3914 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
3915 menu_bar_items_vector = tem;
3917 /* Add this item. */
3918 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
3919 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
3920 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
3921 menu_bar_items_index = i;
3923 Vinhibit_quit = oquit;
3924 UNGCPRO;
3925 return menu_bar_items_vector;
3928 /* Scan one map KEYMAP, accumulating any menu items it defines
3929 in menu_bar_items_vector. */
3931 static void
3932 menu_bar_one_keymap (keymap)
3933 Lisp_Object keymap;
3935 Lisp_Object tail, item, key, binding, item_string, table;
3937 /* Loop over all keymap entries that have menu strings. */
3938 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
3940 item = XCONS (tail)->car;
3941 if (CONSP (item))
3943 key = XCONS (item)->car;
3944 binding = XCONS (item)->cdr;
3945 if (CONSP (binding))
3947 item_string = XCONS (binding)->car;
3948 if (STRINGP (item_string))
3949 menu_bar_item (key, item_string, Fcdr (binding));
3951 else if (EQ (binding, Qundefined))
3952 menu_bar_item (key, Qnil, binding);
3954 else if (VECTORP (item))
3956 /* Loop over the char values represented in the vector. */
3957 int len = XVECTOR (item)->size;
3958 int c;
3959 for (c = 0; c < len; c++)
3961 Lisp_Object character;
3962 XSETFASTINT (character, c);
3963 binding = XVECTOR (item)->contents[c];
3964 if (CONSP (binding))
3966 item_string = XCONS (binding)->car;
3967 if (STRINGP (item_string))
3968 menu_bar_item (key, item_string, Fcdr (binding));
3970 else if (EQ (binding, Qundefined))
3971 menu_bar_item (key, Qnil, binding);
3977 /* This is used as the handler when calling internal_condition_case_1. */
3979 static Lisp_Object
3980 menu_bar_item_1 (arg)
3981 Lisp_Object arg;
3983 return Qnil;
3986 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
3987 If there's already an item for KEY, add this DEF to it. */
3989 static void
3990 menu_bar_item (key, item_string, def)
3991 Lisp_Object key, item_string, def;
3993 Lisp_Object tem;
3994 Lisp_Object enabled;
3995 int i;
3997 if (EQ (def, Qundefined))
3999 /* If a map has an explicit `undefined' as definition,
4000 discard any previously made menu bar item. */
4002 for (i = 0; i < menu_bar_items_index; i += 3)
4003 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
4005 if (menu_bar_items_index > i + 3)
4006 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 3],
4007 &XVECTOR (menu_bar_items_vector)->contents[i],
4008 (menu_bar_items_index - i - 3) * sizeof (Lisp_Object));
4009 menu_bar_items_index -= 3;
4010 return;
4013 /* If there's no definition for this key yet,
4014 just ignore `undefined'. */
4015 return;
4018 /* See if this entry is enabled. */
4019 enabled = Qt;
4021 if (SYMBOLP (def))
4023 /* No property, or nil, means enable.
4024 Otherwise, enable if value is not nil. */
4025 tem = Fget (def, Qmenu_enable);
4026 if (!NILP (tem))
4027 /* (condition-case nil (eval tem)
4028 (error nil)) */
4029 enabled = internal_condition_case_1 (Feval, tem, Qerror,
4030 menu_bar_item_1);
4033 /* Ignore this item if it's not enabled. */
4034 if (NILP (enabled))
4035 return;
4037 /* Find any existing item for this KEY. */
4038 for (i = 0; i < menu_bar_items_index; i += 3)
4039 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
4040 break;
4042 /* If we did not find this KEY, add it at the end. */
4043 if (i == menu_bar_items_index)
4045 /* If vector is too small, get a bigger one. */
4046 if (i + 3 > XVECTOR (menu_bar_items_vector)->size)
4048 Lisp_Object tem;
4049 int newsize = 2 * i;
4050 tem = Fmake_vector (make_number (2 * i), Qnil);
4051 bcopy (XVECTOR (menu_bar_items_vector)->contents,
4052 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
4053 menu_bar_items_vector = tem;
4055 /* Add this item. */
4056 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
4057 XVECTOR (menu_bar_items_vector)->contents[i++] = item_string;
4058 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (def, Qnil);
4059 menu_bar_items_index = i;
4061 /* We did find an item for this KEY. Add DEF to its list of maps. */
4062 else
4064 Lisp_Object old;
4065 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
4066 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (def, old);
4070 /* Read a character using menus based on maps in the array MAPS.
4071 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
4072 Return t if we displayed a menu but the user rejected it.
4074 PREV_EVENT is the previous input event, or nil if we are reading
4075 the first event of a key sequence.
4077 If USED_MOUSE_MENU is non-zero, then we set *USED_MOUSE_MENU to 1
4078 if we used a mouse menu to read the input, or zero otherwise. If
4079 USED_MOUSE_MENU is zero, *USED_MOUSE_MENU is left alone.
4081 The prompting is done based on the prompt-string of the map
4082 and the strings associated with various map elements.
4084 This can be done with X menus or with menus put in the minibuf.
4085 These are done in different ways, depending on how the input will be read.
4086 Menus using X are done after auto-saving in read-char, getting the input
4087 event from Fx_popup_menu; menus using the minibuf use read_char recursively
4088 and do auto-saving in the inner call of read_char. */
4090 static Lisp_Object
4091 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
4092 int nmaps;
4093 Lisp_Object *maps;
4094 Lisp_Object prev_event;
4095 int *used_mouse_menu;
4097 int mapno;
4098 register Lisp_Object name;
4099 Lisp_Object rest, vector;
4101 if (used_mouse_menu)
4102 *used_mouse_menu = 0;
4104 /* Use local over global Menu maps */
4106 if (! menu_prompting)
4107 return Qnil;
4109 /* Optionally disregard all but the global map. */
4110 if (inhibit_local_menu_bar_menus)
4112 maps += (nmaps - 1);
4113 nmaps = 1;
4116 /* Get the menu name from the first map that has one (a prompt string). */
4117 for (mapno = 0; mapno < nmaps; mapno++)
4119 name = map_prompt (maps[mapno]);
4120 if (!NILP (name))
4121 break;
4124 /* If we don't have any menus, just read a character normally. */
4125 if (mapno >= nmaps)
4126 return Qnil;
4128 #if (defined (HAVE_X_WINDOWS) && defined (HAVE_X_MENU)) || defined (MSDOS)
4129 /* If we got to this point via a mouse click,
4130 use a real menu for mouse selection. */
4131 if (EVENT_HAS_PARAMETERS (prev_event))
4133 /* Display the menu and get the selection. */
4134 Lisp_Object *realmaps
4135 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
4136 Lisp_Object value;
4137 int nmaps1 = 0;
4139 /* Use the maps that are not nil. */
4140 for (mapno = 0; mapno < nmaps; mapno++)
4141 if (!NILP (maps[mapno]))
4142 realmaps[nmaps1++] = maps[mapno];
4144 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
4145 if (CONSP (value))
4147 /* If we got more than one event, put all but the first
4148 onto this list to be read later.
4149 Return just the first event now. */
4150 Vunread_command_events
4151 = nconc2 (XCONS (value)->cdr, Vunread_command_events);
4152 value = XCONS (value)->car;
4154 else if (NILP (value))
4155 value = Qt;
4156 if (used_mouse_menu)
4157 *used_mouse_menu = 1;
4158 return value;
4160 #endif /* (HAVE_X_WINDOWS && HAVE_X_MENU) || MSDOS */
4161 return Qnil ;
4164 static Lisp_Object
4165 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
4166 int commandflag ;
4167 int nmaps;
4168 Lisp_Object *maps;
4170 int mapno;
4171 register Lisp_Object name;
4172 int nlength;
4173 int width = FRAME_WIDTH (selected_frame) - 4;
4174 char *menu = (char *) alloca (width + 4);
4175 int idx = -1;
4176 int nobindings = 1;
4177 Lisp_Object rest, vector;
4179 if (! menu_prompting)
4180 return Qnil;
4182 /* Get the menu name from the first map that has one (a prompt string). */
4183 for (mapno = 0; mapno < nmaps; mapno++)
4185 name = map_prompt (maps[mapno]);
4186 if (!NILP (name))
4187 break;
4190 /* If we don't have any menus, just read a character normally. */
4191 if (mapno >= nmaps)
4192 return Qnil;
4194 /* Prompt string always starts with map's prompt, and a space. */
4195 strcpy (menu, XSTRING (name)->data);
4196 nlength = XSTRING (name)->size;
4197 menu[nlength++] = ':';
4198 menu[nlength++] = ' ';
4199 menu[nlength] = 0;
4201 /* Start prompting at start of first map. */
4202 mapno = 0;
4203 rest = maps[mapno];
4205 /* Present the documented bindings, a line at a time. */
4206 while (1)
4208 int notfirst = 0;
4209 int i = nlength;
4210 Lisp_Object obj;
4211 int ch;
4212 int orig_defn_macro ;
4214 /* Loop over elements of map. */
4215 while (i < width)
4217 Lisp_Object s, elt;
4219 /* If reached end of map, start at beginning of next map. */
4220 if (NILP (rest))
4222 mapno++;
4223 /* At end of last map, wrap around to first map if just starting,
4224 or end this line if already have something on it. */
4225 if (mapno == nmaps)
4227 mapno = 0;
4228 if (notfirst || nobindings) break;
4230 rest = maps[mapno];
4233 /* Look at the next element of the map. */
4234 if (idx >= 0)
4235 elt = XVECTOR (vector)->contents[idx];
4236 else
4237 elt = Fcar_safe (rest);
4239 if (idx < 0 && VECTORP (elt))
4241 /* If we found a dense table in the keymap,
4242 advanced past it, but start scanning its contents. */
4243 rest = Fcdr_safe (rest);
4244 vector = elt;
4245 idx = 0;
4247 else
4249 /* An ordinary element. */
4250 if ( idx < 0 )
4251 s = Fcar_safe (Fcdr_safe (elt)); /* alist */
4252 else
4253 s = Fcar_safe(elt); /* vector */
4254 if (!STRINGP (s))
4255 /* Ignore the element if it has no prompt string. */
4257 /* If we have room for the prompt string, add it to this line.
4258 If this is the first on the line, always add it. */
4259 else if (XSTRING (s)->size + i + 2 < width
4260 || !notfirst)
4262 int thiswidth;
4264 /* Punctuate between strings. */
4265 if (notfirst)
4267 strcpy (menu + i, ", ");
4268 i += 2;
4270 notfirst = 1;
4271 nobindings = 0 ;
4273 /* Add as much of string as fits. */
4274 thiswidth = XSTRING (s)->size;
4275 if (thiswidth + i > width)
4276 thiswidth = width - i;
4277 bcopy (XSTRING (s)->data, menu + i, thiswidth);
4278 i += thiswidth;
4279 menu[i] = 0;
4281 else
4283 /* If this element does not fit, end the line now,
4284 and save the element for the next line. */
4285 strcpy (menu + i, "...");
4286 break;
4289 /* Move past this element. */
4290 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
4291 /* Handle reaching end of dense table. */
4292 idx = -1;
4293 if (idx >= 0)
4294 idx++;
4295 else
4296 rest = Fcdr_safe (rest);
4300 /* Prompt with that and read response. */
4301 message1 (menu);
4303 /* Make believe its not a keyboard macro in case the help char
4304 is pressed. Help characters are not recorded because menu prompting
4305 is not used on replay.
4307 orig_defn_macro = defining_kbd_macro ;
4308 defining_kbd_macro = 0 ;
4310 obj = read_char (commandflag, 0, 0, Qnil, 0);
4311 while (BUFFERP (obj));
4312 defining_kbd_macro = orig_defn_macro ;
4314 if (!INTEGERP (obj))
4315 return obj;
4316 else
4317 ch = XINT (obj);
4319 if (! EQ (obj, menu_prompt_more_char)
4320 && (!INTEGERP (menu_prompt_more_char)
4321 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
4323 if ( defining_kbd_macro )
4324 store_kbd_macro_char(obj) ;
4325 return obj;
4327 /* Help char - go round again */
4331 /* Reading key sequences. */
4333 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
4334 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
4335 keymap, or nil otherwise. Return the index of the first keymap in
4336 which KEY has any binding, or NMAPS if no map has a binding.
4338 If KEY is a meta ASCII character, treat it like meta-prefix-char
4339 followed by the corresponding non-meta character. Keymaps in
4340 CURRENT with non-prefix bindings for meta-prefix-char become nil in
4341 NEXT.
4343 If KEY has no bindings in any of the CURRENT maps, NEXT is left
4344 unmodified.
4346 NEXT may == CURRENT. */
4348 static int
4349 follow_key (key, nmaps, current, defs, next)
4350 Lisp_Object key;
4351 Lisp_Object *current, *defs, *next;
4352 int nmaps;
4354 int i, first_binding;
4356 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
4357 followed by the corresponding non-meta character. */
4358 if (INTEGERP (key) && (XINT (key) & CHAR_META))
4360 for (i = 0; i < nmaps; i++)
4361 if (! NILP (current[i]))
4363 next[i] =
4364 get_keyelt (access_keymap (current[i], meta_prefix_char, 1, 0));
4366 /* Note that since we pass the resulting bindings through
4367 get_keymap_1, non-prefix bindings for meta-prefix-char
4368 disappear. */
4369 next[i] = get_keymap_1 (next[i], 0, 1);
4371 else
4372 next[i] = Qnil;
4374 current = next;
4375 XSETINT (key, XFASTINT (key) & ~CHAR_META);
4378 first_binding = nmaps;
4379 for (i = nmaps - 1; i >= 0; i--)
4381 if (! NILP (current[i]))
4383 defs[i] = get_keyelt (access_keymap (current[i], key, 1, 0));
4384 if (! NILP (defs[i]))
4385 first_binding = i;
4387 else
4388 defs[i] = Qnil;
4391 /* Given the set of bindings we've found, produce the next set of maps. */
4392 if (first_binding < nmaps)
4393 for (i = 0; i < nmaps; i++)
4394 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
4396 return first_binding;
4399 /* Read a sequence of keys that ends with a non prefix character,
4400 storing it in KEYBUF, a buffer of size BUFSIZE.
4401 Prompt with PROMPT.
4402 Return the length of the key sequence stored.
4403 Return -1 if the user rejected a command menu.
4405 Echo starting immediately unless `prompt' is 0.
4407 Where a key sequence ends depends on the currently active keymaps.
4408 These include any minor mode keymaps active in the current buffer,
4409 the current buffer's local map, and the global map.
4411 If a key sequence has no other bindings, we check Vfunction_key_map
4412 to see if some trailing subsequence might be the beginning of a
4413 function key's sequence. If so, we try to read the whole function
4414 key, and substitute its symbolic name into the key sequence.
4416 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
4417 `double-' events into similar click events, if that would make them
4418 bound. We try to turn `triple-' events first into `double-' events,
4419 then into clicks.
4421 If we get a mouse click in a mode line, vertical divider, or other
4422 non-text area, we treat the click as if it were prefixed by the
4423 symbol denoting that area - `mode-line', `vertical-line', or
4424 whatever.
4426 If the sequence starts with a mouse click, we read the key sequence
4427 with respect to the buffer clicked on, not the current buffer.
4429 If the user switches frames in the midst of a key sequence, we put
4430 off the switch-frame event until later; the next call to
4431 read_char will return it. */
4433 static int
4434 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last)
4435 Lisp_Object *keybuf;
4436 int bufsize;
4437 Lisp_Object prompt;
4438 int dont_downcase_last;
4440 int count = specpdl_ptr - specpdl;
4442 /* How many keys there are in the current key sequence. */
4443 int t;
4445 /* The length of the echo buffer when we started reading, and
4446 the length of this_command_keys when we started reading. */
4447 int echo_start;
4448 int keys_start;
4450 /* The number of keymaps we're scanning right now, and the number of
4451 keymaps we have allocated space for. */
4452 int nmaps;
4453 int nmaps_allocated = 0;
4455 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
4456 the current keymaps. */
4457 Lisp_Object *defs;
4459 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
4460 in the current keymaps, or nil where it is not a prefix. */
4461 Lisp_Object *submaps;
4463 /* The local map to start out with at start of key sequence. */
4464 Lisp_Object orig_local_map;
4466 /* 1 if we have already considered switching to the local-map property
4467 of the place where a mouse click occurred. */
4468 int localized_local_map = 0;
4470 /* The index in defs[] of the first keymap that has a binding for
4471 this key sequence. In other words, the lowest i such that
4472 defs[i] is non-nil. */
4473 int first_binding;
4475 /* If t < mock_input, then KEYBUF[t] should be read as the next
4476 input key.
4478 We use this to recover after recognizing a function key. Once we
4479 realize that a suffix of the current key sequence is actually a
4480 function key's escape sequence, we replace the suffix with the
4481 function key's binding from Vfunction_key_map. Now keybuf
4482 contains a new and different key sequence, so the echo area,
4483 this_command_keys, and the submaps and defs arrays are wrong. In
4484 this situation, we set mock_input to t, set t to 0, and jump to
4485 restart_sequence; the loop will read keys from keybuf up until
4486 mock_input, thus rebuilding the state; and then it will resume
4487 reading characters from the keyboard. */
4488 int mock_input = 0;
4490 /* If the sequence is unbound in submaps[], then
4491 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
4492 and fkey_map is its binding.
4494 These might be > t, indicating that all function key scanning
4495 should hold off until t reaches them. We do this when we've just
4496 recognized a function key, to avoid searching for the function
4497 key's again in Vfunction_key_map. */
4498 int fkey_start = 0, fkey_end = 0;
4499 Lisp_Object fkey_map;
4501 /* Likewise, for key_translation_map. */
4502 int keytran_start = 0, keytran_end = 0;
4503 Lisp_Object keytran_map;
4505 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
4506 we put it off for later. While we're reading, we keep the event here. */
4507 Lisp_Object delayed_switch_frame;
4509 /* See the comment below... */
4510 #if defined (GOBBLE_FIRST_EVENT)
4511 Lisp_Object first_event;
4512 #endif
4514 Lisp_Object original_uppercase;
4515 int original_uppercase_position = -1;
4517 /* Gets around Microsoft compiler limitations. */
4518 int dummyflag = 0;
4520 struct buffer *starting_buffer;
4522 /* Nonzero if we seem to have got the beginning of a binding
4523 in function_key_map. */
4524 int function_key_possible = 0;
4525 int key_translation_possible = 0;
4527 int junk;
4529 last_nonmenu_event = Qnil;
4531 delayed_switch_frame = Qnil;
4532 fkey_map = Vfunction_key_map;
4533 keytran_map = Vkey_translation_map;
4535 /* If there is no function-key-map, turn off function key scanning. */
4536 if (NILP (Fkeymapp (Vfunction_key_map)))
4537 fkey_start = fkey_end = bufsize + 1;
4539 /* If there is no key-translation-map, turn off scanning. */
4540 if (NILP (Fkeymapp (Vkey_translation_map)))
4541 keytran_start = keytran_end = bufsize + 1;
4543 if (INTERACTIVE)
4545 if (!NILP (prompt))
4546 echo_prompt (XSTRING (prompt)->data);
4547 else if (cursor_in_echo_area && echo_keystrokes)
4548 /* This doesn't put in a dash if the echo buffer is empty, so
4549 you don't always see a dash hanging out in the minibuffer. */
4550 echo_dash ();
4553 /* Record the initial state of the echo area and this_command_keys;
4554 we will need to restore them if we replay a key sequence. */
4555 if (INTERACTIVE)
4556 echo_start = echo_length ();
4557 keys_start = this_command_key_count;
4559 #if defined (GOBBLE_FIRST_EVENT)
4560 /* This doesn't quite work, because some of the things that read_char
4561 does cannot safely be bypassed. It seems too risky to try to make
4562 this work right. */
4564 /* Read the first char of the sequence specially, before setting
4565 up any keymaps, in case a filter runs and switches buffers on us. */
4566 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
4567 &junk);
4568 #endif /* GOBBLE_FIRST_EVENT */
4570 orig_local_map = get_local_map (PT, current_buffer);
4572 /* We jump here when the key sequence has been thoroughly changed, and
4573 we need to rescan it starting from the beginning. When we jump here,
4574 keybuf[0..mock_input] holds the sequence we should reread. */
4575 replay_sequence:
4577 starting_buffer = current_buffer;
4578 function_key_possible = 0;
4579 key_translation_possible = 0;
4581 /* Build our list of keymaps.
4582 If we recognize a function key and replace its escape sequence in
4583 keybuf with its symbol, or if the sequence starts with a mouse
4584 click and we need to switch buffers, we jump back here to rebuild
4585 the initial keymaps from the current buffer. */
4587 Lisp_Object *maps;
4589 if (!NILP (Voverriding_local_map))
4591 nmaps = 2;
4592 if (nmaps > nmaps_allocated)
4594 submaps = (Lisp_Object *) alloca (nmaps * sizeof (submaps[0]));
4595 defs = (Lisp_Object *) alloca (nmaps * sizeof (defs[0]));
4596 nmaps_allocated = nmaps;
4598 submaps[0] = Voverriding_local_map;
4600 else
4602 nmaps = current_minor_maps (0, &maps) + 2;
4603 if (nmaps > nmaps_allocated)
4605 submaps = (Lisp_Object *) alloca (nmaps * sizeof (submaps[0]));
4606 defs = (Lisp_Object *) alloca (nmaps * sizeof (defs[0]));
4607 nmaps_allocated = nmaps;
4609 bcopy (maps, submaps, (nmaps - 2) * sizeof (submaps[0]));
4610 #ifdef USE_TEXT_PROPERTIES
4611 submaps[nmaps-2] = orig_local_map;
4612 #else
4613 submaps[nmaps-2] = current_buffer->keymap;
4614 #endif
4616 submaps[nmaps-1] = current_global_map;
4619 /* Find an accurate initial value for first_binding. */
4620 for (first_binding = 0; first_binding < nmaps; first_binding++)
4621 if (! NILP (submaps[first_binding]))
4622 break;
4624 /* Start from the beginning in keybuf. */
4625 t = 0;
4627 /* These are no-ops the first time through, but if we restart, they
4628 revert the echo area and this_command_keys to their original state. */
4629 this_command_key_count = keys_start;
4630 if (INTERACTIVE && t < mock_input)
4631 echo_truncate (echo_start);
4633 /* If the best binding for the current key sequence is a keymap, or
4634 we may be looking at a function key's escape sequence, keep on
4635 reading. */
4636 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
4637 || (first_binding >= nmaps
4638 && fkey_start < t
4639 /* mock input is never part of a function key's sequence. */
4640 && mock_input <= fkey_start)
4641 || (first_binding >= nmaps
4642 && keytran_start < t && key_translation_possible)
4643 /* Don't return in the middle of a possible function key sequence,
4644 if the only bindings we found were via case conversion.
4645 Thus, if ESC O a has a function-key-map translation
4646 and ESC o has a binding, don't return after ESC O,
4647 so that we can translate ESC O plus the next character. */
4650 Lisp_Object key;
4651 int used_mouse_menu = 0;
4653 /* Where the last real key started. If we need to throw away a
4654 key that has expanded into more than one element of keybuf
4655 (say, a mouse click on the mode line which is being treated
4656 as [mode-line (mouse-...)], then we backtrack to this point
4657 of keybuf. */
4658 int last_real_key_start;
4660 /* These variables are analogous to echo_start and keys_start;
4661 while those allow us to restart the entire key sequence,
4662 echo_local_start and keys_local_start allow us to throw away
4663 just one key. */
4664 int echo_local_start, keys_local_start, local_first_binding;
4666 if (t >= bufsize)
4667 error ("key sequence too long");
4669 if (INTERACTIVE)
4670 echo_local_start = echo_length ();
4671 keys_local_start = this_command_key_count;
4672 local_first_binding = first_binding;
4674 replay_key:
4675 /* These are no-ops, unless we throw away a keystroke below and
4676 jumped back up to replay_key; in that case, these restore the
4677 variables to their original state, allowing us to replay the
4678 loop. */
4679 if (INTERACTIVE && t < mock_input)
4680 echo_truncate (echo_local_start);
4681 this_command_key_count = keys_local_start;
4682 first_binding = local_first_binding;
4684 /* By default, assume each event is "real". */
4685 last_real_key_start = t;
4687 /* Does mock_input indicate that we are re-reading a key sequence? */
4688 if (t < mock_input)
4690 key = keybuf[t];
4691 add_command_key (key);
4692 if (echo_keystrokes)
4693 echo_char (key);
4696 /* If not, we should actually read a character. */
4697 else
4699 struct buffer *buf = current_buffer;
4701 key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
4702 &used_mouse_menu);
4704 /* read_char returns t when it shows a menu and the user rejects it.
4705 Just return -1. */
4706 if (EQ (key, Qt))
4707 return -1;
4709 /* read_char returns -1 at the end of a macro.
4710 Emacs 18 handles this by returning immediately with a
4711 zero, so that's what we'll do. */
4712 if (INTEGERP (key) && XINT (key) == -1)
4714 t = 0;
4715 /* The Microsoft C compiler can't handle the goto that
4716 would go here. */
4717 dummyflag = 1;
4718 break;
4721 /* If the current buffer has been changed from under us, the
4722 keymap may have changed, so replay the sequence. */
4723 if (BUFFERP (key))
4725 mock_input = t;
4726 goto replay_sequence;
4729 /* If we have a quit that was typed in another frame, and
4730 quit_throw_to_read_char switched buffers,
4731 replay to get the right keymap. */
4732 if (XINT (key) == quit_char && current_buffer != starting_buffer)
4734 keybuf[t++] = key;
4735 mock_input = t;
4736 Vquit_flag = Qnil;
4737 goto replay_sequence;
4740 Vquit_flag = Qnil;
4743 /* Clicks in non-text areas get prefixed by the symbol
4744 in their CHAR-ADDRESS field. For example, a click on
4745 the mode line is prefixed by the symbol `mode-line'.
4747 Furthermore, key sequences beginning with mouse clicks
4748 are read using the keymaps of the buffer clicked on, not
4749 the current buffer. So we may have to switch the buffer
4750 here.
4752 When we turn one event into two events, we must make sure
4753 that neither of the two looks like the original--so that,
4754 if we replay the events, they won't be expanded again.
4755 If not for this, such reexpansion could happen either here
4756 or when user programs play with this-command-keys. */
4757 if (EVENT_HAS_PARAMETERS (key))
4759 Lisp_Object kind;
4761 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
4762 if (EQ (kind, Qmouse_click))
4764 Lisp_Object window, posn;
4766 window = POSN_WINDOW (EVENT_START (key));
4767 posn = POSN_BUFFER_POSN (EVENT_START (key));
4768 if (CONSP (posn))
4770 /* We're looking at the second event of a
4771 sequence which we expanded before. Set
4772 last_real_key_start appropriately. */
4773 if (t > 0)
4774 last_real_key_start = t - 1;
4777 /* Key sequences beginning with mouse clicks are
4778 read using the keymaps in the buffer clicked on,
4779 not the current buffer. If we're at the
4780 beginning of a key sequence, switch buffers. */
4781 if (last_real_key_start == 0
4782 && WINDOWP (window)
4783 && BUFFERP (XWINDOW (window)->buffer)
4784 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
4786 keybuf[t] = key;
4787 mock_input = t + 1;
4789 /* Arrange to go back to the original buffer once we're
4790 done reading the key sequence. Note that we can't
4791 use save_excursion_{save,restore} here, because they
4792 save point as well as the current buffer; we don't
4793 want to save point, because redisplay may change it,
4794 to accommodate a Fset_window_start or something. We
4795 don't want to do this at the top of the function,
4796 because we may get input from a subprocess which
4797 wants to change the selected window and stuff (say,
4798 emacsclient). */
4799 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4801 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
4802 orig_local_map = get_local_map (PT, current_buffer);
4803 goto replay_sequence;
4805 /* For a mouse click, get the local text-property keymap
4806 of the place clicked on, rather than point. */
4807 if (last_real_key_start == 0 && CONSP (XCONS (key)->cdr)
4808 && ! localized_local_map)
4810 Lisp_Object map_here, start, pos;
4812 localized_local_map = 1;
4813 start = EVENT_START (key);
4814 if (CONSP (start) && CONSP (XCONS (start)->cdr))
4816 pos = POSN_BUFFER_POSN (start);
4817 if (INTEGERP (pos))
4819 map_here = get_local_map (XINT (pos), current_buffer);
4820 if (!EQ (map_here, orig_local_map))
4822 orig_local_map = map_here;
4823 keybuf[t] = key;
4824 mock_input = t + 1;
4826 goto replay_sequence;
4832 /* Expand mode-line and scroll-bar events into two events:
4833 use posn as a fake prefix key. */
4834 if (SYMBOLP (posn))
4836 if (t + 1 >= bufsize)
4837 error ("key sequence too long");
4838 keybuf[t] = posn;
4839 keybuf[t+1] = key;
4840 mock_input = t + 2;
4842 /* Zap the position in key, so we know that we've
4843 expanded it, and don't try to do so again. */
4844 POSN_BUFFER_POSN (EVENT_START (key))
4845 = Fcons (posn, Qnil);
4846 goto replay_key;
4849 else if (EQ (kind, Qswitch_frame))
4851 /* If we're at the beginning of a key sequence, go
4852 ahead and return this event. If we're in the
4853 midst of a key sequence, delay it until the end. */
4854 if (t > 0)
4856 delayed_switch_frame = key;
4857 goto replay_key;
4860 else if (CONSP (XCONS (key)->cdr)
4861 && CONSP (EVENT_START (key))
4862 && CONSP (XCONS (EVENT_START (key))->cdr))
4864 Lisp_Object posn;
4866 posn = POSN_BUFFER_POSN (EVENT_START (key));
4867 /* Handle menu-bar events:
4868 insert the dummy prefix event `menu-bar'. */
4869 if (EQ (posn, Qmenu_bar))
4871 if (t + 1 >= bufsize)
4872 error ("key sequence too long");
4873 /* Run the Lucid hook. */
4874 if (!NILP (Vrun_hooks))
4875 call1 (Vrun_hooks, Qactivate_menubar_hook);
4876 /* If it has changed current-menubar from previous value,
4877 really recompute the menubar from the value. */
4878 if (! NILP (Vlucid_menu_bar_dirty_flag))
4879 call0 (Qrecompute_lucid_menubar);
4880 keybuf[t] = posn;
4881 keybuf[t+1] = key;
4883 /* Zap the position in key, so we know that we've
4884 expanded it, and don't try to do so again. */
4885 POSN_BUFFER_POSN (EVENT_START (key))
4886 = Fcons (posn, Qnil);
4888 mock_input = t + 2;
4889 goto replay_sequence;
4891 else if (CONSP (posn))
4893 /* We're looking at the second event of a
4894 sequence which we expanded before. Set
4895 last_real_key_start appropriately. */
4896 if (last_real_key_start == t && t > 0)
4897 last_real_key_start = t - 1;
4902 /* We have finally decided that KEY is something we might want
4903 to look up. */
4904 first_binding = (follow_key (key,
4905 nmaps - first_binding,
4906 submaps + first_binding,
4907 defs + first_binding,
4908 submaps + first_binding)
4909 + first_binding);
4911 /* If KEY wasn't bound, we'll try some fallbacks. */
4912 if (first_binding >= nmaps)
4914 Lisp_Object head;
4916 head = EVENT_HEAD (key);
4917 if (EQ (head, Vhelp_char))
4919 read_key_sequence_cmd = Vprefix_help_command;
4920 keybuf[t++] = key;
4921 last_nonmenu_event = key;
4922 /* The Microsoft C compiler can't handle the goto that
4923 would go here. */
4924 dummyflag = 1;
4925 break;
4928 if (SYMBOLP (head))
4930 Lisp_Object breakdown;
4931 int modifiers;
4933 breakdown = parse_modifiers (head);
4934 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
4935 /* Attempt to reduce an unbound mouse event to a simpler
4936 event that is bound:
4937 Drags reduce to clicks.
4938 Double-clicks reduce to clicks.
4939 Triple-clicks reduce to double-clicks, then to clicks.
4940 Down-clicks are eliminated.
4941 Double-downs reduce to downs, then are eliminated.
4942 Triple-downs reduce to double-downs, then to downs,
4943 then are eliminated. */
4944 if (modifiers & (down_modifier | drag_modifier
4945 | double_modifier | triple_modifier))
4947 while (modifiers & (down_modifier | drag_modifier
4948 | double_modifier | triple_modifier))
4950 Lisp_Object new_head, new_click;
4951 if (modifiers & triple_modifier)
4952 modifiers ^= (double_modifier | triple_modifier);
4953 else if (modifiers & double_modifier)
4954 modifiers &= ~double_modifier;
4955 else if (modifiers & drag_modifier)
4956 modifiers &= ~drag_modifier;
4957 else
4959 /* Dispose of this `down' event by simply jumping
4960 back to replay_key, to get another event.
4962 Note that if this event came from mock input,
4963 then just jumping back to replay_key will just
4964 hand it to us again. So we have to wipe out any
4965 mock input.
4967 We could delete keybuf[t] and shift everything
4968 after that to the left by one spot, but we'd also
4969 have to fix up any variable that points into
4970 keybuf, and shifting isn't really necessary
4971 anyway.
4973 Adding prefixes for non-textual mouse clicks
4974 creates two characters of mock input, and both
4975 must be thrown away. If we're only looking at
4976 the prefix now, we can just jump back to
4977 replay_key. On the other hand, if we've already
4978 processed the prefix, and now the actual click
4979 itself is giving us trouble, then we've lost the
4980 state of the keymaps we want to backtrack to, and
4981 we need to replay the whole sequence to rebuild
4984 Beyond that, only function key expansion could
4985 create more than two keys, but that should never
4986 generate mouse events, so it's okay to zero
4987 mock_input in that case too.
4989 Isn't this just the most wonderful code ever? */
4990 if (t == last_real_key_start)
4992 mock_input = 0;
4993 goto replay_key;
4995 else
4997 mock_input = last_real_key_start;
4998 goto replay_sequence;
5002 new_head
5003 = apply_modifiers (modifiers, XCONS (breakdown)->car);
5004 new_click
5005 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
5007 /* Look for a binding for this new key. follow_key
5008 promises that it didn't munge submaps the
5009 last time we called it, since key was unbound. */
5010 first_binding
5011 = (follow_key (new_click,
5012 nmaps - local_first_binding,
5013 submaps + local_first_binding,
5014 defs + local_first_binding,
5015 submaps + local_first_binding)
5016 + local_first_binding);
5018 /* If that click is bound, go for it. */
5019 if (first_binding < nmaps)
5021 key = new_click;
5022 break;
5024 /* Otherwise, we'll leave key set to the drag event. */
5030 keybuf[t++] = key;
5031 /* Normally, last_nonmenu_event gets the previous key we read.
5032 But when a mouse popup menu is being used,
5033 we don't update last_nonmenu_event; it continues to hold the mouse
5034 event that preceded the first level of menu. */
5035 if (!used_mouse_menu)
5036 last_nonmenu_event = key;
5038 /* If the sequence is unbound, see if we can hang a function key
5039 off the end of it. We only want to scan real keyboard input
5040 for function key sequences, so if mock_input says that we're
5041 re-reading old events, don't examine it. */
5042 if (first_binding >= nmaps
5043 && t >= mock_input)
5045 Lisp_Object fkey_next;
5047 /* Continue scan from fkey_end until we find a bound suffix.
5048 If we fail, increment fkey_start
5049 and start fkey_end from there. */
5050 while (fkey_end < t)
5052 Lisp_Object key;
5054 key = keybuf[fkey_end++];
5055 /* Look up meta-characters by prefixing them
5056 with meta_prefix_char. I hate this. */
5057 if (INTEGERP (key) && XINT (key) & meta_modifier)
5059 fkey_next
5060 = get_keymap_1
5061 (get_keyelt
5062 (access_keymap (fkey_map, meta_prefix_char, 1, 0)),
5063 0, 1);
5064 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
5066 else
5067 fkey_next = fkey_map;
5069 fkey_next
5070 = get_keyelt (access_keymap (fkey_next, key, 1, 0));
5072 #if 0 /* I didn't turn this on, because it might cause trouble
5073 for the mapping of return into C-m and tab into C-i. */
5074 /* Optionally don't map function keys into other things.
5075 This enables the user to redefine kp- keys easily. */
5076 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
5077 fkey_next = Qnil;
5078 #endif
5080 /* If the function key map gives a function, not an
5081 array, then call the function with no args and use
5082 its value instead. */
5083 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
5084 && fkey_end == t)
5086 struct gcpro gcpro1, gcpro2, gcpro3;
5087 Lisp_Object tem;
5088 tem = fkey_next;
5090 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
5091 fkey_next = call1 (fkey_next, prompt);
5092 UNGCPRO;
5093 /* If the function returned something invalid,
5094 barf--don't ignore it.
5095 (To ignore it safely, we would need to gcpro a bunch of
5096 other variables.) */
5097 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
5098 error ("Function in function-key-map returns invalid key sequence");
5101 function_key_possible = ! NILP (fkey_next);
5103 /* If keybuf[fkey_start..fkey_end] is bound in the
5104 function key map and it's a suffix of the current
5105 sequence (i.e. fkey_end == t), replace it with
5106 the binding and restart with fkey_start at the end. */
5107 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
5108 && fkey_end == t)
5110 int len = XFASTINT (Flength (fkey_next));
5112 t = fkey_start + len;
5113 if (t >= bufsize)
5114 error ("key sequence too long");
5116 if (VECTORP (fkey_next))
5117 bcopy (XVECTOR (fkey_next)->contents,
5118 keybuf + fkey_start,
5119 (t - fkey_start) * sizeof (keybuf[0]));
5120 else if (STRINGP (fkey_next))
5122 int i;
5124 for (i = 0; i < len; i++)
5125 XSETFASTINT (keybuf[fkey_start + i],
5126 XSTRING (fkey_next)->data[i]);
5129 mock_input = t;
5130 fkey_start = fkey_end = t;
5131 fkey_map = Vfunction_key_map;
5133 /* Do pass the results through key-translation-map. */
5134 keytran_start = keytran_end = 0;
5135 keytran_map = Vkey_translation_map;
5137 goto replay_sequence;
5140 fkey_map = get_keymap_1 (fkey_next, 0, 1);
5142 /* If we no longer have a bound suffix, try a new positions for
5143 fkey_start. */
5144 if (NILP (fkey_map))
5146 fkey_end = ++fkey_start;
5147 fkey_map = Vfunction_key_map;
5148 function_key_possible = 0;
5153 /* Look for this sequence in key-translation-map. */
5155 Lisp_Object keytran_next;
5157 /* Scan from keytran_end until we find a bound suffix. */
5158 while (keytran_end < t)
5160 Lisp_Object key;
5162 key = keybuf[keytran_end++];
5163 /* Look up meta-characters by prefixing them
5164 with meta_prefix_char. I hate this. */
5165 if (INTEGERP (key) && XINT (key) & meta_modifier)
5167 keytran_next
5168 = get_keymap_1
5169 (get_keyelt
5170 (access_keymap (keytran_map, meta_prefix_char, 1, 0)),
5171 0, 1);
5172 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
5174 else
5175 keytran_next = keytran_map;
5177 keytran_next
5178 = get_keyelt (access_keymap (keytran_next, key, 1, 0));
5180 /* If the key translation map gives a function, not an
5181 array, then call the function with no args and use
5182 its value instead. */
5183 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
5184 && keytran_end == t)
5186 struct gcpro gcpro1, gcpro2, gcpro3;
5187 Lisp_Object tem;
5188 tem = keytran_next;
5190 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
5191 keytran_next = call1 (keytran_next, prompt);
5192 UNGCPRO;
5193 /* If the function returned something invalid,
5194 barf--don't ignore it.
5195 (To ignore it safely, we would need to gcpro a bunch of
5196 other variables.) */
5197 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
5198 error ("Function in key-translation-map returns invalid key sequence");
5201 key_translation_possible = ! NILP (keytran_next);
5203 /* If keybuf[keytran_start..keytran_end] is bound in the
5204 key translation map and it's a suffix of the current
5205 sequence (i.e. keytran_end == t), replace it with
5206 the binding and restart with keytran_start at the end. */
5207 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
5208 && keytran_end == t)
5210 int len = XFASTINT (Flength (keytran_next));
5212 t = keytran_start + len;
5213 if (t >= bufsize)
5214 error ("key sequence too long");
5216 if (VECTORP (keytran_next))
5217 bcopy (XVECTOR (keytran_next)->contents,
5218 keybuf + keytran_start,
5219 (t - keytran_start) * sizeof (keybuf[0]));
5220 else if (STRINGP (keytran_next))
5222 int i;
5224 for (i = 0; i < len; i++)
5225 XSETFASTINT (keybuf[keytran_start + i],
5226 XSTRING (keytran_next)->data[i]);
5229 mock_input = t;
5230 keytran_start = keytran_end = t;
5231 keytran_map = Vkey_translation_map;
5233 /* Don't pass the results of key-translation-map
5234 through function-key-map. */
5235 fkey_start = fkey_end = t;
5236 fkey_map = Vkey_translation_map;
5238 goto replay_sequence;
5241 keytran_map = get_keymap_1 (keytran_next, 0, 1);
5243 /* If we no longer have a bound suffix, try a new positions for
5244 keytran_start. */
5245 if (NILP (keytran_map))
5247 keytran_end = ++keytran_start;
5248 keytran_map = Vkey_translation_map;
5249 key_translation_possible = 0;
5254 /* If KEY is not defined in any of the keymaps,
5255 and cannot be part of a function key or translation,
5256 and is an upper case letter
5257 use the corresponding lower-case letter instead. */
5258 if (first_binding == nmaps && ! function_key_possible
5259 && ! key_translation_possible
5260 && INTEGERP (key)
5261 && ((((XINT (key) & 0x3ffff)
5262 < XSTRING (current_buffer->downcase_table)->size)
5263 && UPPERCASEP (XINT (key) & 0x3ffff))
5264 || (XINT (key) & shift_modifier)))
5266 original_uppercase = key;
5267 original_uppercase_position = t - 1;
5269 if (XINT (key) & shift_modifier)
5270 XSETINT (key, XINT (key) & ~shift_modifier);
5271 else
5272 XSETINT (key, (DOWNCASE (XINT (key) & 0x3ffff)
5273 | (XINT (key) & ~0x3ffff)));
5275 keybuf[t - 1] = key;
5276 mock_input = t;
5277 goto replay_sequence;
5279 /* If KEY is not defined in any of the keymaps,
5280 and cannot be part of a function key or translation,
5281 and is a shifted function key,
5282 use the corresponding unshifted function key instead. */
5283 if (first_binding == nmaps && ! function_key_possible
5284 && ! key_translation_possible
5285 && SYMBOLP (key))
5287 Lisp_Object breakdown;
5288 int modifiers;
5290 original_uppercase = key;
5291 original_uppercase_position = t - 1;
5293 breakdown = parse_modifiers (key);
5294 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
5295 if (modifiers & shift_modifier)
5297 modifiers &= ~shift_modifier;
5298 key = apply_modifiers (make_number (modifiers),
5299 XCONS (breakdown)->car);
5301 keybuf[t - 1] = key;
5302 mock_input = t;
5303 goto replay_sequence;
5308 if (!dummyflag)
5309 read_key_sequence_cmd = (first_binding < nmaps
5310 ? defs[first_binding]
5311 : Qnil);
5313 unread_switch_frame = delayed_switch_frame;
5314 unbind_to (count, Qnil);
5316 if (dont_downcase_last && t - 1 == original_uppercase_position)
5317 keybuf[t - 1] = original_uppercase;
5319 /* Occasionally we fabricate events, perhaps by expanding something
5320 according to function-key-map, or by adding a prefix symbol to a
5321 mouse click in the scroll bar or modeline. In this cases, return
5322 the entire generated key sequence, even if we hit an unbound
5323 prefix or a definition before the end. This means that you will
5324 be able to push back the event properly, and also means that
5325 read-key-sequence will always return a logical unit.
5327 Better ideas? */
5328 for (; t < mock_input; t++)
5330 if (echo_keystrokes)
5331 echo_char (keybuf[t]);
5332 add_command_key (keybuf[t]);
5335 return t;
5338 #if 0 /* This doc string is too long for some compilers.
5339 This commented-out definition serves for DOC. */
5340 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 2, 0,
5341 "Read a sequence of keystrokes and return as a string or vector.\n\
5342 The sequence is sufficient to specify a non-prefix command in the\n\
5343 current local and global maps.\n\
5345 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
5346 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
5347 as a continuation of the previous key.\n\
5349 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
5350 convert the last event to lower case. (Normally any upper case event\n\
5351 is converted to lower case if the original event is undefined and the lower\n\
5352 case equivalent is defined.) A non-nil value is appropriate for reading\n\
5353 a key sequence to be defined.\n\
5355 A C-g typed while in this function is treated like any other character,\n\
5356 and `quit-flag' is not set.\n\
5358 If the key sequence starts with a mouse click, then the sequence is read\n\
5359 using the keymaps of the buffer of the window clicked in, not the buffer\n\
5360 of the selected window as normal.\n\
5361 ""\n\
5362 `read-key-sequence' drops unbound button-down events, since you normally\n\
5363 only care about the click or drag events which follow them. If a drag\n\
5364 or multi-click event is unbound, but the corresponding click event would\n\
5365 be bound, `read-key-sequence' turns the event into a click event at the\n\
5366 drag's starting position. This means that you don't have to distinguish\n\
5367 between click and drag, double, or triple events unless you want to.\n\
5369 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
5370 lines separating windows, and scroll bars with imaginary keys\n\
5371 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
5373 If the user switches frames in the middle of a key sequence, the\n\
5374 frame-switch event is put off until after the current key sequence.\n\
5376 `read-key-sequence' checks `function-key-map' for function key\n\
5377 sequences, where they wouldn't conflict with ordinary bindings. See\n\
5378 `function-key-map' for more details.")
5379 (prompt, continue_echo)
5380 #endif
5382 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 3, 0,
5384 (prompt, continue_echo, dont_downcase_last)
5385 Lisp_Object prompt, continue_echo, dont_downcase_last;
5387 Lisp_Object keybuf[30];
5388 register int i;
5389 struct gcpro gcpro1, gcpro2;
5391 if (!NILP (prompt))
5392 CHECK_STRING (prompt, 0);
5393 QUIT;
5395 bzero (keybuf, sizeof keybuf);
5396 GCPRO1 (keybuf[0]);
5397 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
5399 if (NILP (continue_echo))
5400 this_command_key_count = 0;
5402 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
5403 prompt, ! NILP (dont_downcase_last));
5405 if (i == -1)
5407 Vquit_flag = Qt;
5408 QUIT;
5410 UNGCPRO;
5411 return make_event_array (i, keybuf);
5414 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
5415 "Execute CMD as an editor command.\n\
5416 CMD must be a symbol that satisfies the `commandp' predicate.\n\
5417 Optional second arg RECORD-FLAG non-nil\n\
5418 means unconditionally put this command in `command-history'.\n\
5419 Otherwise, that is done only if an arg is read using the minibuffer.")
5420 (cmd, record)
5421 Lisp_Object cmd, record;
5423 register Lisp_Object final;
5424 register Lisp_Object tem;
5425 Lisp_Object prefixarg;
5426 struct backtrace backtrace;
5427 extern int debug_on_next_call;
5429 prefixarg = current_perdisplay->Vprefix_arg;
5430 current_perdisplay->Vprefix_arg = Qnil;
5431 current_perdisplay->Vcurrent_prefix_arg = prefixarg;
5432 debug_on_next_call = 0;
5434 if (SYMBOLP (cmd))
5436 tem = Fget (cmd, Qdisabled);
5437 if (!NILP (tem) && !NILP (Vrun_hooks))
5438 return call1 (Vrun_hooks, Qdisabled_command_hook);
5441 while (1)
5443 final = Findirect_function (cmd);
5445 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
5446 do_autoload (final, cmd);
5447 else
5448 break;
5451 if (STRINGP (final) || VECTORP (final))
5453 /* If requested, place the macro in the command history. For
5454 other sorts of commands, call-interactively takes care of
5455 this. */
5456 if (!NILP (record))
5457 Vcommand_history
5458 = Fcons (Fcons (Qexecute_kbd_macro,
5459 Fcons (final, Fcons (prefixarg, Qnil))),
5460 Vcommand_history);
5462 return Fexecute_kbd_macro (final, prefixarg);
5464 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
5466 backtrace.next = backtrace_list;
5467 backtrace_list = &backtrace;
5468 backtrace.function = &Qcall_interactively;
5469 backtrace.args = &cmd;
5470 backtrace.nargs = 1;
5471 backtrace.evalargs = 0;
5473 tem = Fcall_interactively (cmd, record);
5475 backtrace_list = backtrace.next;
5476 return tem;
5478 return Qnil;
5481 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
5482 1, 1, "P",
5483 "Read function name, then read its arguments and call it.")
5484 (prefixarg)
5485 Lisp_Object prefixarg;
5487 Lisp_Object function;
5488 char buf[40];
5489 Lisp_Object saved_keys;
5490 struct gcpro gcpro1;
5492 saved_keys = Fvector (this_command_key_count,
5493 XVECTOR (this_command_keys)->contents);
5494 buf[0] = 0;
5495 GCPRO1 (saved_keys);
5497 if (EQ (prefixarg, Qminus))
5498 strcpy (buf, "- ");
5499 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
5500 strcpy (buf, "C-u ");
5501 else if (CONSP (prefixarg) && INTEGERP (XCONS (prefixarg)->car))
5502 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
5503 else if (INTEGERP (prefixarg))
5504 sprintf (buf, "%d ", XINT (prefixarg));
5506 /* This isn't strictly correct if execute-extended-command
5507 is bound to anything else. Perhaps it should use
5508 this_command_keys? */
5509 strcat (buf, "M-x ");
5511 /* Prompt with buf, and then read a string, completing from and
5512 restricting to the set of all defined commands. Don't provide
5513 any initial input. Save the command read on the extended-command
5514 history list. */
5515 function = Fcompleting_read (build_string (buf),
5516 Vobarray, Qcommandp,
5517 Qt, Qnil, Qextended_command_history);
5519 /* Set this_command_keys to the concatenation of saved_keys and
5520 function, followed by a RET. */
5522 struct Lisp_String *str;
5523 Lisp_Object *keys;
5524 int i;
5525 Lisp_Object tem;
5527 this_command_key_count = 0;
5529 keys = XVECTOR (saved_keys)->contents;
5530 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
5531 add_command_key (keys[i]);
5533 str = XSTRING (function);
5534 for (i = 0; i < str->size; i++)
5536 XSETFASTINT (tem, str->data[i]);
5537 add_command_key (tem);
5540 XSETFASTINT (tem, '\015');
5541 add_command_key (tem);
5544 UNGCPRO;
5546 function = Fintern (function, Qnil);
5547 current_perdisplay->Vprefix_arg = prefixarg;
5548 this_command = function;
5550 return Fcommand_execute (function, Qt);
5554 detect_input_pending ()
5556 if (!input_pending)
5557 get_input_pending (&input_pending);
5559 return input_pending;
5562 /* This is called in some cases before a possible quit.
5563 It cases the next call to detect_input_pending to recompute input_pending.
5564 So calling this function unnecessarily can't do any harm. */
5565 clear_input_pending ()
5567 input_pending = 0;
5570 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
5571 "T if command input is currently available with no waiting.\n\
5572 Actually, the value is nil only if we can be sure that no input is available.")
5575 if (!NILP (Vunread_command_events) || unread_command_char != -1)
5576 return (Qt);
5578 return detect_input_pending () ? Qt : Qnil;
5581 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
5582 "Return vector of last 100 events, not counting those from keyboard macros.")
5585 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
5586 Lisp_Object val;
5588 if (total_keys < NUM_RECENT_KEYS)
5589 return Fvector (total_keys, keys);
5590 else
5592 val = Fvector (NUM_RECENT_KEYS, keys);
5593 bcopy (keys + recent_keys_index,
5594 XVECTOR (val)->contents,
5595 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
5596 bcopy (keys,
5597 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
5598 recent_keys_index * sizeof (Lisp_Object));
5599 return val;
5603 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
5604 "Return the key sequence that invoked this command.\n\
5605 The value is a string or a vector.")
5608 return make_event_array (this_command_key_count,
5609 XVECTOR (this_command_keys)->contents);
5612 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
5613 "Return the current depth in recursive edits.")
5616 Lisp_Object temp;
5617 XSETFASTINT (temp, command_loop_level + minibuf_level);
5618 return temp;
5621 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
5622 "FOpen dribble file: ",
5623 "Start writing all keyboard characters to a dribble file called FILE.\n\
5624 If FILE is nil, close any open dribble file.")
5625 (file)
5626 Lisp_Object file;
5628 if (NILP (file))
5630 if (dribble)
5632 fclose (dribble);
5633 dribble = 0;
5636 else
5638 file = Fexpand_file_name (file, Qnil);
5639 dribble = fopen (XSTRING (file)->data, "w");
5641 return Qnil;
5644 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
5645 "Discard the contents of the terminal input buffer.\n\
5646 Also cancel any kbd macro being defined.")
5649 defining_kbd_macro = 0;
5650 update_mode_lines++;
5652 Vunread_command_events = Qnil;
5653 unread_command_char = -1;
5655 discard_tty_input ();
5657 /* Without the cast, GCC complains that this assignment loses the
5658 volatile qualifier of kbd_store_ptr. Is there anything wrong
5659 with that? */
5660 current_perdisplay->kbd_fetch_ptr
5661 = (struct input_event *) current_perdisplay->kbd_store_ptr;
5662 Ffillarray (current_perdisplay->kbd_buffer_frame_or_window, Qnil);
5663 input_pending = 0;
5665 return Qnil;
5668 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
5669 "Stop Emacs and return to superior process. You can resume later.\n\
5670 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
5671 control, run a subshell instead.\n\n\
5672 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
5673 to be read as terminal input by Emacs's parent, after suspension.\n\
5675 Before suspending, run the normal hook `suspend-hook'.\n\
5676 After resumption run the normal hook `suspend-resume-hook'.\n\
5678 Some operating systems cannot stop the Emacs process and resume it later.\n\
5679 On such systems, Emacs starts a subshell instead of suspending.")
5680 (stuffstring)
5681 Lisp_Object stuffstring;
5683 Lisp_Object tem;
5684 int count = specpdl_ptr - specpdl;
5685 int old_height, old_width;
5686 int width, height;
5687 struct gcpro gcpro1, gcpro2;
5688 extern init_sys_modes ();
5690 if (!NILP (stuffstring))
5691 CHECK_STRING (stuffstring, 0);
5693 /* Run the functions in suspend-hook. */
5694 if (!NILP (Vrun_hooks))
5695 call1 (Vrun_hooks, intern ("suspend-hook"));
5697 GCPRO1 (stuffstring);
5698 get_frame_size (&old_width, &old_height);
5699 reset_sys_modes ();
5700 /* sys_suspend can get an error if it tries to fork a subshell
5701 and the system resources aren't available for that. */
5702 record_unwind_protect (init_sys_modes, 0);
5703 stuff_buffered_input (stuffstring);
5704 if (cannot_suspend)
5705 sys_subshell ();
5706 else
5707 sys_suspend ();
5708 unbind_to (count, Qnil);
5710 /* Check if terminal/window size has changed.
5711 Note that this is not useful when we are running directly
5712 with a window system; but suspend should be disabled in that case. */
5713 get_frame_size (&width, &height);
5714 if (width != old_width || height != old_height)
5715 change_frame_size (selected_frame, height, width, 0, 0);
5717 /* Run suspend-resume-hook. */
5718 if (!NILP (Vrun_hooks))
5719 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
5721 UNGCPRO;
5722 return Qnil;
5725 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
5726 Then in any case stuff anything Emacs has read ahead and not used. */
5728 stuff_buffered_input (stuffstring)
5729 Lisp_Object stuffstring;
5731 /* stuff_char works only in BSD, versions 4.2 and up. */
5732 #ifdef BSD
5733 #ifndef BSD4_1
5734 register unsigned char *p;
5735 PERDISPLAY *perd;
5737 if (STRINGP (stuffstring))
5739 register int count;
5741 p = XSTRING (stuffstring)->data;
5742 count = XSTRING (stuffstring)->size;
5743 while (count-- > 0)
5744 stuff_char (*p++);
5745 stuff_char ('\n');
5747 /* Anything we have read ahead, put back for the shell to read. */
5748 #ifndef MULTI_PERDISPLAY
5749 perd = &the_only_perdisplay;
5750 #else
5751 /* ?? What should this do when we have multiple keyboards?? */
5752 perd = current_perdisplay;
5753 if (!perd)
5754 return;
5755 #endif
5756 while (perd->kbd_fetch_ptr != perd->kbd_store_ptr)
5758 if (perd->kbd_fetch_ptr == perd->kbd_buffer + KBD_BUFFER_SIZE)
5759 perd->kbd_fetch_ptr = perd->kbd_buffer;
5760 if (perd->kbd_fetch_ptr->kind == ascii_keystroke)
5761 stuff_char (perd->kbd_fetch_ptr->code);
5762 perd->kbd_fetch_ptr->kind = no_event;
5763 (XVECTOR (perd->kbd_buffer_frame_or_window)->contents[perd->kbd_fetch_ptr
5764 - perd->kbd_buffer]
5765 = Qnil);
5766 perd->kbd_fetch_ptr++;
5768 input_pending = 0;
5769 #endif
5770 #endif /* BSD and not BSD4_1 */
5773 set_waiting_for_input (time_to_clear)
5774 EMACS_TIME *time_to_clear;
5776 input_available_clear_time = time_to_clear;
5778 /* Tell interrupt_signal to throw back to read_char, */
5779 waiting_for_input = 1;
5781 /* If interrupt_signal was called before and buffered a C-g,
5782 make it run again now, to avoid timing error. */
5783 if (!NILP (Vquit_flag))
5784 quit_throw_to_read_char ();
5787 clear_waiting_for_input ()
5789 /* Tell interrupt_signal not to throw back to read_char, */
5790 waiting_for_input = 0;
5791 input_available_clear_time = 0;
5794 /* This routine is called at interrupt level in response to C-G.
5795 If interrupt_input, this is the handler for SIGINT.
5796 Otherwise, it is called from kbd_buffer_store_event,
5797 in handling SIGIO or SIGTINT.
5799 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
5800 immediately throw back to read_char.
5802 Otherwise it sets the Lisp variable quit-flag not-nil.
5803 This causes eval to throw, when it gets a chance.
5804 If quit-flag is already non-nil, it stops the job right away. */
5806 SIGTYPE
5807 interrupt_signal ()
5809 char c;
5810 /* Must preserve main program's value of errno. */
5811 int old_errno = errno;
5813 #ifdef USG
5814 if (!read_socket_hook && NILP (Vwindow_system))
5816 /* USG systems forget handlers when they are used;
5817 must reestablish each time */
5818 signal (SIGINT, interrupt_signal);
5819 signal (SIGQUIT, interrupt_signal);
5821 #endif /* USG */
5823 cancel_echoing ();
5825 if (!NILP (Vquit_flag) && FRAME_TERMCAP_P (selected_frame))
5827 fflush (stdout);
5828 reset_sys_modes ();
5829 sigfree ();
5830 #ifdef SIGTSTP /* Support possible in later USG versions */
5832 * On systems which can suspend the current process and return to the original
5833 * shell, this command causes the user to end up back at the shell.
5834 * The "Auto-save" and "Abort" questions are not asked until
5835 * the user elects to return to emacs, at which point he can save the current
5836 * job and either dump core or continue.
5838 sys_suspend ();
5839 #else
5840 #ifdef VMS
5841 if (sys_suspend () == -1)
5843 printf ("Not running as a subprocess;\n");
5844 printf ("you can continue or abort.\n");
5846 #else /* not VMS */
5847 /* Perhaps should really fork an inferior shell?
5848 But that would not provide any way to get back
5849 to the original shell, ever. */
5850 printf ("No support for stopping a process on this operating system;\n");
5851 printf ("you can continue or abort.\n");
5852 #endif /* not VMS */
5853 #endif /* not SIGTSTP */
5854 #ifdef MSDOS
5855 /* We must remain inside the screen area when the internal terminal
5856 is used. Note that [Enter] is not echoed by dos. */
5857 cursor_to (0, 0);
5858 #endif
5859 printf ("Auto-save? (y or n) ");
5860 fflush (stdout);
5861 if (((c = getchar ()) & ~040) == 'Y')
5863 Fdo_auto_save (Qt, Qnil);
5864 #ifdef MSDOS
5865 printf ("\r\nAuto-save done");
5866 #else /* not MSDOS */
5867 printf ("Auto-save done\n");
5868 #endif /* not MSDOS */
5870 while (c != '\n') c = getchar ();
5871 #ifdef MSDOS
5872 printf ("\r\nAbort? (y or n) ");
5873 #else /* not MSDOS */
5874 #ifdef VMS
5875 printf ("Abort (and enter debugger)? (y or n) ");
5876 #else /* not VMS */
5877 printf ("Abort (and dump core)? (y or n) ");
5878 #endif /* not VMS */
5879 #endif /* not MSDOS */
5880 fflush (stdout);
5881 if (((c = getchar ()) & ~040) == 'Y')
5882 abort ();
5883 while (c != '\n') c = getchar ();
5884 #ifdef MSDOS
5885 printf ("\r\nContinuing...\r\n");
5886 #else /* not MSDOS */
5887 printf ("Continuing...\n");
5888 #endif /* not MSDOS */
5889 fflush (stdout);
5890 init_sys_modes ();
5892 else
5894 /* If executing a function that wants to be interrupted out of
5895 and the user has not deferred quitting by binding `inhibit-quit'
5896 then quit right away. */
5897 if (immediate_quit && NILP (Vinhibit_quit))
5899 immediate_quit = 0;
5900 sigfree ();
5901 Fsignal (Qquit, Qnil);
5903 else
5904 /* Else request quit when it's safe */
5905 Vquit_flag = Qt;
5908 if (waiting_for_input && !echoing)
5909 quit_throw_to_read_char ();
5911 errno = old_errno;
5914 /* Handle a C-g by making read_char return C-g. */
5916 quit_throw_to_read_char ()
5918 quit_error_check ();
5919 sigfree ();
5920 /* Prevent another signal from doing this before we finish. */
5921 clear_waiting_for_input ();
5922 input_pending = 0;
5924 Vunread_command_events = Qnil;
5925 unread_command_char = -1;
5927 #ifdef POLL_FOR_INPUT
5928 /* May be > 1 if in recursive minibuffer. */
5929 if (poll_suppress_count == 0)
5930 abort ();
5931 #endif
5932 #ifdef MULTI_FRAME
5934 Lisp_Object frame;
5936 if (!current_perdisplay)
5937 abort ();
5938 frame = current_perdisplay->internal_last_event_frame;
5939 if (FRAMEP (frame) && XFRAME (frame) != selected_frame)
5940 Fhandle_switch_frame (make_lispy_switch_frame (frame));
5942 #endif
5944 _longjmp (getcjmp, 1);
5947 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
5948 "Set mode of reading keyboard input.\n\
5949 First arg INTERRUPT non-nil means use input interrupts;\n\
5950 nil means use CBREAK mode.\n\
5951 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
5952 (no effect except in CBREAK mode).\n\
5953 Third arg META t means accept 8-bit input (for a Meta key).\n\
5954 META nil means ignore the top bit, on the assumption it is parity.\n\
5955 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
5956 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
5957 See also `current-input-mode'.")
5958 (interrupt, flow, meta, quit)
5959 Lisp_Object interrupt, flow, meta, quit;
5961 if (!NILP (quit)
5962 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
5963 error ("set-input-mode: QUIT must be an ASCII character");
5965 #ifdef POLL_FOR_INPUT
5966 stop_polling ();
5967 #endif
5969 reset_sys_modes ();
5970 #ifdef SIGIO
5971 /* Note SIGIO has been undef'd if FIONREAD is missing. */
5972 #ifdef NO_SOCK_SIGIO
5973 if (read_socket_hook)
5974 interrupt_input = 0; /* No interrupts if reading from a socket. */
5975 else
5976 #endif /* NO_SOCK_SIGIO */
5977 interrupt_input = !NILP (interrupt);
5978 #else /* not SIGIO */
5979 interrupt_input = 0;
5980 #endif /* not SIGIO */
5981 /* Our VMS input only works by interrupts, as of now. */
5982 #ifdef VMS
5983 interrupt_input = 1;
5984 #endif
5985 flow_control = !NILP (flow);
5986 if (NILP (meta))
5987 meta_key = 0;
5988 else if (EQ (meta, Qt))
5989 meta_key = 1;
5990 else
5991 meta_key = 2;
5992 if (!NILP (quit))
5993 /* Don't let this value be out of range. */
5994 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
5996 init_sys_modes ();
5998 #ifdef POLL_FOR_INPUT
5999 poll_suppress_count = 1;
6000 start_polling ();
6001 #endif
6002 return Qnil;
6005 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
6006 "Return information about the way Emacs currently reads keyboard input.\n\
6007 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
6008 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
6009 nil, Emacs is using CBREAK mode.\n\
6010 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
6011 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
6012 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
6013 META nil means ignoring the top bit, on the assumption it is parity.\n\
6014 META is neither t nor nil if accepting 8-bit input and using\n\
6015 all 8 bits as the character code.\n\
6016 QUIT is the character Emacs currently uses to quit.\n\
6017 The elements of this list correspond to the arguments of\n\
6018 `set-input-mode'.")
6021 Lisp_Object val[4];
6023 val[0] = interrupt_input ? Qt : Qnil;
6024 val[1] = flow_control ? Qt : Qnil;
6025 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
6026 XSETFASTINT (val[3], quit_char);
6028 return Flist (sizeof (val) / sizeof (val[0]), val);
6033 * Set up a perdisplay object with reasonable initial values.
6035 void
6036 init_perdisplay (perd)
6037 PERDISPLAY *perd;
6039 perd->Vprefix_arg = Qnil;
6040 perd->Vcurrent_prefix_arg = Qnil;
6041 perd->kbd_buffer
6042 = (struct input_event *)xmalloc (KBD_BUFFER_SIZE
6043 * sizeof (struct input_event));
6044 perd->kbd_fetch_ptr = perd->kbd_buffer;
6045 perd->kbd_store_ptr = perd->kbd_buffer;
6046 perd->kbd_buffer_frame_or_window
6047 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
6048 #ifdef MULTI_FRAME
6049 /* This means that command_loop_1 won't try to select anything the first
6050 time through. */
6051 perd->internal_last_event_frame = Qnil;
6052 #endif
6053 perd->Vlast_event_frame = Qnil;
6057 * Destroy the contents of a perdisplay object, but not the object itself.
6058 * We use this just before deleteing it, or if we're going to initialize
6059 * it a second time.
6061 void
6062 wipe_perdisplay (perd)
6063 PERDISPLAY *perd;
6065 xfree (perd->kbd_buffer);
6068 init_keyboard ()
6070 /* This is correct before outermost invocation of the editor loop */
6071 command_loop_level = -1;
6072 immediate_quit = 0;
6073 quit_char = Ctl ('g');
6074 Vunread_command_events = Qnil;
6075 unread_command_char = -1;
6076 total_keys = 0;
6077 recent_keys_index = 0;
6078 #ifdef HAVE_MOUSE
6079 do_mouse_tracking = Qnil;
6080 #endif
6081 input_pending = 0;
6083 #ifndef MULTI_PERDISPLAY
6084 if (initialized)
6085 wipe_perdisplay (&the_only_perdisplay);
6086 init_perdisplay (&the_only_perdisplay);
6087 #endif
6089 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
6091 signal (SIGINT, interrupt_signal);
6092 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
6093 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
6094 SIGQUIT and we can't tell which one it will give us. */
6095 signal (SIGQUIT, interrupt_signal);
6096 #endif /* HAVE_TERMIO */
6098 /* Note SIGIO has been undef'd if FIONREAD is missing. */
6099 #ifdef SIGIO
6100 if (!noninteractive)
6101 signal (SIGIO, input_available_signal);
6102 #endif /* SIGIO */
6104 /* Use interrupt input by default, if it works and noninterrupt input
6105 has deficiencies. */
6107 #ifdef INTERRUPT_INPUT
6108 interrupt_input = 1;
6109 #else
6110 interrupt_input = 0;
6111 #endif
6113 /* Our VMS input only works by interrupts, as of now. */
6114 #ifdef VMS
6115 interrupt_input = 1;
6116 #endif
6118 sigfree ();
6119 dribble = 0;
6121 if (keyboard_init_hook)
6122 (*keyboard_init_hook) ();
6124 #ifdef POLL_FOR_INPUT
6125 poll_suppress_count = 1;
6126 start_polling ();
6127 #endif
6130 /* This type's only use is in syms_of_keyboard, to initialize the
6131 event header symbols and put properties on them. */
6132 struct event_head {
6133 Lisp_Object *var;
6134 char *name;
6135 Lisp_Object *kind;
6138 struct event_head head_table[] = {
6139 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
6140 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
6141 &Qswitch_frame, "switch-frame", &Qswitch_frame,
6142 &Qdelete_frame, "delete-frame", &Qdelete_frame,
6143 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
6144 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
6147 syms_of_keyboard ()
6149 Qdisabled_command_hook = intern ("disabled-command-hook");
6150 staticpro (&Qdisabled_command_hook);
6152 Qself_insert_command = intern ("self-insert-command");
6153 staticpro (&Qself_insert_command);
6155 Qforward_char = intern ("forward-char");
6156 staticpro (&Qforward_char);
6158 Qbackward_char = intern ("backward-char");
6159 staticpro (&Qbackward_char);
6161 Qdisabled = intern ("disabled");
6162 staticpro (&Qdisabled);
6164 Qundefined = intern ("undefined");
6165 staticpro (&Qundefined);
6167 Qpre_command_hook = intern ("pre-command-hook");
6168 staticpro (&Qpre_command_hook);
6170 Qpost_command_hook = intern ("post-command-hook");
6171 staticpro (&Qpost_command_hook);
6173 Qdeferred_action_function = intern ("deferred-action-function");
6174 staticpro (&Qdeferred_action_function);
6176 Qcommand_hook_internal = intern ("command-hook-internal");
6177 staticpro (&Qcommand_hook_internal);
6179 Qfunction_key = intern ("function-key");
6180 staticpro (&Qfunction_key);
6181 Qmouse_click = intern ("mouse-click");
6182 staticpro (&Qmouse_click);
6184 Qmenu_enable = intern ("menu-enable");
6185 staticpro (&Qmenu_enable);
6187 Qmode_line = intern ("mode-line");
6188 staticpro (&Qmode_line);
6189 Qvertical_line = intern ("vertical-line");
6190 staticpro (&Qvertical_line);
6191 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
6192 staticpro (&Qvertical_scroll_bar);
6193 Qmenu_bar = intern ("menu-bar");
6194 staticpro (&Qmenu_bar);
6196 Qabove_handle = intern ("above-handle");
6197 staticpro (&Qabove_handle);
6198 Qhandle = intern ("handle");
6199 staticpro (&Qhandle);
6200 Qbelow_handle = intern ("below-handle");
6201 staticpro (&Qbelow_handle);
6203 Qevent_kind = intern ("event-kind");
6204 staticpro (&Qevent_kind);
6205 Qevent_symbol_elements = intern ("event-symbol-elements");
6206 staticpro (&Qevent_symbol_elements);
6207 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
6208 staticpro (&Qevent_symbol_element_mask);
6209 Qmodifier_cache = intern ("modifier-cache");
6210 staticpro (&Qmodifier_cache);
6212 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
6213 staticpro (&Qrecompute_lucid_menubar);
6214 Qactivate_menubar_hook = intern ("activate-menubar-hook");
6215 staticpro (&Qactivate_menubar_hook);
6217 Qpolling_period = intern ("polling-period");
6218 staticpro (&Qpolling_period);
6221 struct event_head *p;
6223 for (p = head_table;
6224 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
6225 p++)
6227 *p->var = intern (p->name);
6228 staticpro (p->var);
6229 Fput (*p->var, Qevent_kind, *p->kind);
6230 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
6234 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
6235 staticpro (&button_down_location);
6238 int i;
6239 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
6241 modifier_symbols = Fmake_vector (make_number (len), Qnil);
6242 for (i = 0; i < len; i++)
6243 if (modifier_names[i])
6244 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
6245 staticpro (&modifier_symbols);
6248 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
6249 staticpro (&recent_keys);
6251 this_command_keys = Fmake_vector (make_number (40), Qnil);
6252 staticpro (&this_command_keys);
6254 Qextended_command_history = intern ("extended-command-history");
6255 Fset (Qextended_command_history, Qnil);
6256 staticpro (&Qextended_command_history);
6258 accent_key_syms = Qnil;
6259 staticpro (&accent_key_syms);
6261 func_key_syms = Qnil;
6262 staticpro (&func_key_syms);
6264 system_key_syms = Qnil;
6265 staticpro (&system_key_syms);
6267 mouse_syms = Qnil;
6268 staticpro (&mouse_syms);
6270 unread_switch_frame = Qnil;
6271 staticpro (&unread_switch_frame);
6273 defsubr (&Sread_key_sequence);
6274 defsubr (&Srecursive_edit);
6275 #ifdef HAVE_MOUSE
6276 defsubr (&Strack_mouse);
6277 #endif
6278 defsubr (&Sinput_pending_p);
6279 defsubr (&Scommand_execute);
6280 defsubr (&Srecent_keys);
6281 defsubr (&Sthis_command_keys);
6282 defsubr (&Ssuspend_emacs);
6283 defsubr (&Sabort_recursive_edit);
6284 defsubr (&Sexit_recursive_edit);
6285 defsubr (&Srecursion_depth);
6286 defsubr (&Stop_level);
6287 defsubr (&Sdiscard_input);
6288 defsubr (&Sopen_dribble_file);
6289 defsubr (&Sset_input_mode);
6290 defsubr (&Scurrent_input_mode);
6291 defsubr (&Sexecute_extended_command);
6293 DEFVAR_LISP ("last-command-char", &last_command_char,
6294 "Last input event that was part of a command.");
6296 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
6297 "Last input event that was part of a command.");
6299 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
6300 "Last input event in a command, except for mouse menu events.\n\
6301 Mouse menus give back keys that don't look like mouse events;\n\
6302 this variable holds the actual mouse event that led to the menu,\n\
6303 so that you can determine whether the command was run by mouse or not.");
6305 DEFVAR_LISP ("last-input-char", &last_input_char,
6306 "Last input event.");
6308 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
6309 "Last input event.");
6311 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
6312 "List of objects to be read as next command input events.");
6314 DEFVAR_INT ("unread-command-char", &unread_command_char,
6315 "If not -1, an object to be read as next command input event.");
6317 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
6318 "Meta-prefix character code. Meta-foo as command input\n\
6319 turns into this character followed by foo.");
6320 XSETINT (meta_prefix_char, 033);
6322 DEFVAR_LISP ("last-command", &last_command,
6323 "The last command executed. Normally a symbol with a function definition,\n\
6324 but can be whatever was found in the keymap, or whatever the variable\n\
6325 `this-command' was set to by that command.\n\
6327 The value `mode-exit' is special; it means that the previous command\n\
6328 read an event that told it to exit, and it did so and unread that event.\n\
6329 In other words, the present command is the event that made the previous\n\
6330 command exit.\n\
6332 The value `kill-region' is special; it means that the previous command\n\
6333 was a kill command.");
6334 last_command = Qnil;
6336 DEFVAR_LISP ("this-command", &this_command,
6337 "The command now being executed.\n\
6338 The command can set this variable; whatever is put here\n\
6339 will be in `last-command' during the following command.");
6340 this_command = Qnil;
6342 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
6343 "*Number of keyboard input characters between auto-saves.\n\
6344 Zero means disable autosaving due to number of characters typed.");
6345 auto_save_interval = 300;
6347 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
6348 "*Number of seconds idle time before auto-save.\n\
6349 Zero or nil means disable auto-saving due to idleness.\n\
6350 After auto-saving due to this many seconds of idle time,\n\
6351 Emacs also does a garbage collection if that seems to be warranted.");
6352 XSETFASTINT (Vauto_save_timeout, 30);
6354 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
6355 "*Nonzero means echo unfinished commands after this many seconds of pause.");
6356 echo_keystrokes = 1;
6358 DEFVAR_INT ("polling-period", &polling_period,
6359 "*Interval between polling for input during Lisp execution.\n\
6360 The reason for polling is to make C-g work to stop a running program.\n\
6361 Polling is needed only when using X windows and SIGIO does not work.\n\
6362 Polling is automatically disabled in all other cases.");
6363 polling_period = 2;
6365 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
6366 "*Maximum time between mouse clicks to make a double-click.\n\
6367 Measured in milliseconds. nil means disable double-click recognition;\n\
6368 t means double-clicks have no time limit and are detected\n\
6369 by position only.");
6370 Vdouble_click_time = make_number (500);
6372 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
6373 "*Non-nil means inhibit local map menu bar menus.");
6374 inhibit_local_menu_bar_menus = 0;
6376 DEFVAR_INT ("num-input-keys", &num_input_keys,
6377 "Number of complete keys read from the keyboard so far.");
6378 num_input_keys = 0;
6380 DEFVAR_LISP ("help-char", &Vhelp_char,
6381 "Character to recognize as meaning Help.\n\
6382 When it is read, do `(eval help-form)', and display result if it's a string.\n\
6383 If the value of `help-form' is nil, this char can be read normally.");
6384 XSETINT (Vhelp_char, Ctl ('H'));
6386 DEFVAR_LISP ("help-form", &Vhelp_form,
6387 "Form to execute when character `help-char' is read.\n\
6388 If the form returns a string, that string is displayed.\n\
6389 If `help-form' is nil, the help char is not recognized.");
6390 Vhelp_form = Qnil;
6392 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
6393 "Command to run when `help-char' character follows a prefix key.\n\
6394 This command is used only when there is no actual binding\n\
6395 for that character after that prefix key.");
6396 Vprefix_help_command = Qnil;
6398 DEFVAR_LISP ("top-level", &Vtop_level,
6399 "Form to evaluate when Emacs starts up.\n\
6400 Useful to set before you dump a modified Emacs.");
6401 Vtop_level = Qnil;
6403 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
6404 "String used as translate table for keyboard input, or nil.\n\
6405 Each character is looked up in this string and the contents used instead.\n\
6406 If string is of length N, character codes N and up are untranslated.");
6407 Vkeyboard_translate_table = Qnil;
6409 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
6410 "Keymap of key translations that can override keymaps.\n\
6411 This keymap works like `function-key-map', but comes after that,\n\
6412 and applies even for keys that have ordinary bindings.");
6413 Vkey_translation_map = Qnil;
6415 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
6416 "Non-nil means to always spawn a subshell instead of suspending,\n\
6417 even if the operating system has support for stopping a process.");
6418 cannot_suspend = 0;
6420 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
6421 "Non-nil means prompt with menus when appropriate.\n\
6422 This is done when reading from a keymap that has a prompt string,\n\
6423 for elements that have prompt strings.\n\
6424 The menu is displayed on the screen\n\
6425 if X menus were enabled at configuration\n\
6426 time and the previous event was a mouse click prefix key.\n\
6427 Otherwise, menu prompting uses the echo area.");
6428 menu_prompting = 1;
6430 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
6431 "Character to see next line of menu prompt.\n\
6432 Type this character while in a menu prompt to rotate around the lines of it.");
6433 XSETINT (menu_prompt_more_char, ' ');
6435 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
6436 "A mask of additional modifier keys to use with every keyboard character.\n\
6437 Emacs applies the modifiers of the character stored here to each keyboard\n\
6438 character it reads. For example, after evaluating the expression\n\
6439 (setq extra-keyboard-modifiers ?\\C-x)\n\
6440 all input characters will have the control modifier applied to them.\n\
6442 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
6443 not count as a control character; rather, it counts as a character\n\
6444 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
6445 cancels any modification.");
6446 extra_keyboard_modifiers = 0;
6448 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
6449 "If an editing command sets this to t, deactivate the mark afterward.\n\
6450 The command loop sets this to nil before each command,\n\
6451 and tests the value when the command returns.\n\
6452 Buffer modification stores t in this variable.");
6453 Vdeactivate_mark = Qnil;
6455 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
6456 "Temporary storage of pre-command-hook or post-command-hook.");
6457 Vcommand_hook_internal = Qnil;
6459 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
6460 "Normal hook run before each command is executed.\n\
6461 While the hook is run, its value is temporarily set to nil\n\
6462 to avoid an unbreakable infinite loop if a hook function gets an error.\n\
6463 As a result, a hook function cannot straightforwardly alter the value of\n\
6464 `pre-command-hook'. See the Emacs Lisp manual for a way of\n\
6465 implementing hook functions that alter the set of hook functions.");
6466 Vpre_command_hook = Qnil;
6468 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
6469 "Normal hook run after each command is executed.\n\
6470 While the hook is run, its value is temporarily set to nil\n\
6471 to avoid an unbreakable infinite loop if a hook function gets an error.\n\
6472 As a result, a hook function cannot straightforwardly alter the value of\n\
6473 `post-command-hook'. See the Emacs Lisp manual for a way of\n\
6474 implementing hook functions that alter the set of hook functions.");
6475 Vpost_command_hook = Qnil;
6477 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
6478 "t means menu bar, specified Lucid style, needs to be recomputed.");
6479 Vlucid_menu_bar_dirty_flag = Qnil;
6481 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
6482 "List of menu bar items to move to the end of the menu bar.\n\
6483 The elements of the list are event types that may have menu bar bindings.");
6484 Vmenu_bar_final_items = Qnil;
6486 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
6487 "Keymap that overrides all other local keymaps.\n\
6488 If this variable is non-nil, it is used as a keymap instead of the\n\
6489 buffer's local map, and the minor mode keymaps and text property keymaps.");
6490 Voverriding_local_map = Qnil;
6492 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
6493 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
6494 Otherwise, the menu bar continues to reflect the buffer's local map\n\
6495 and the minor mode maps regardless of `overriding-local-map'.");
6496 Voverriding_local_map_menu_flag = Qnil;
6498 #ifdef HAVE_MOUSE
6499 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
6500 "*Non-nil means generate motion events for mouse motion.");
6501 #endif
6503 DEFVAR_LISP ("system-key-alist", &Vsystem_key_alist,
6504 "Alist of system-specific X windows key symbols.\n\
6505 Each element should have the form (N . SYMBOL) where N is the\n\
6506 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
6507 and SYMBOL is its name.");
6508 Vsystem_key_alist = Qnil;
6510 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
6511 "List of deferred actions to be performed at a later time.\n\
6512 The precise format isn't relevant here; we just check whether it is nil.");
6513 Vdeferred_action_list = Qnil;
6515 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
6516 "Function to call to handle deferred actions, after each command.\n\
6517 This function is called with no arguments after each command\n\
6518 whenever `deferred-action-list' is non-nil.");
6519 Vdeferred_action_function = Qnil;
6521 DEFVAR_DISPLAY ("prefix-arg", Vprefix_arg,
6522 "The value of the prefix argument for the next editing command.\n\
6523 It may be a number, or the symbol `-' for just a minus sign as arg,\n\
6524 or a list whose car is a number for just one or more C-U's\n\
6525 or nil if no argument has been specified.\n\
6527 You cannot examine this variable to find the argument for this command\n\
6528 since it has been set to nil by the time you can look.\n\
6529 Instead, you should use the variable `current-prefix-arg', although\n\
6530 normally commands can get this prefix argument with (interactive \"P\").");
6532 DEFVAR_DISPLAY ("current-prefix-arg", Vcurrent_prefix_arg,
6533 "The value of the prefix argument for this editing command.\n\
6534 It may be a number, or the symbol `-' for just a minus sign as arg,\n\
6535 or a list whose car is a number for just one or more C-U's\n\
6536 or nil if no argument has been specified.\n\
6537 This is what `(interactive \"P\")' returns.");
6539 DEFVAR_DISPLAY ("last-event-frame", Vlast_event_frame,
6540 "The frame in which the most recently read event occurred.\n\
6541 If the last event came from a keyboard macro, this is set to `macro'.");
6544 keys_of_keyboard ()
6546 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
6547 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
6548 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
6549 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
6550 initial_define_key (meta_map, 'x', "execute-extended-command");