home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / event-stream.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-13  |  57.0 KB  |  1,900 lines

  1. /* The portable interface to event_streams.
  2.    Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  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.
  10.  
  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.
  15.  
  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.  */
  19.  
  20. /*
  21.  *    DANGER!!
  22.  *
  23.  *    If you ever change ANYTHING in this file, you MUST run the
  24.  *    testcases at the end to make sure that you haven't changed
  25.  *    the semantics of recent-keys, last-input-char, or keyboard
  26.  *    macros.  You'd be surprised how easy it is to break this.
  27.  *
  28.  */
  29.  
  30. #include "config.h"
  31. #include "lisp.h"
  32. #include "buffer.h"
  33. #include "window.h"
  34. #include "process.h"
  35. #include "events.h"
  36.  
  37. /* The number of keystrokes between auto-saves. */
  38. static int auto_save_interval;
  39.  
  40. extern Lisp_Object Qself_insert_command, Qprocessp;
  41.  
  42. extern int interrupt_char, help_char, meta_prefix_char;
  43.  
  44. Lisp_Object Qundefined; /* The symbol undefined; good a place as any... */
  45.  
  46. extern Lisp_Object Vmouse_motion_handler;
  47.  
  48. extern Lisp_Object Qpre_command_hook, Qpost_command_hook;
  49.  
  50. static void echo_char_event (), echo_prompt (), maybe_echo_keys ();
  51. void cancel_echoing ();
  52.  
  53.  
  54. /* The callback routines for the window system or terminal driver */
  55. struct event_stream *event_stream;
  56.  
  57. /* This structure is what we use to excapsulate the state of a command sequence
  58.    being composed; key events are executed by adding themselves to the command
  59.    builder; if the command builder is then complete (does not still represent
  60.    a prefix key sequence) it executes the corresponding command.
  61.  */
  62. static struct command_builder {
  63.   Lisp_Object events;        /* Vector of the events being accumulated */
  64.   int event_count;        /* How many elts of the vector are in use */
  65.   Lisp_Object leaf;        /* Terminal node of what we've got so far */
  66.   int echo_keys;        /* Whether minibuffer echoing is active */
  67.   char *echobuf, *echoptr;    /* Minibuffer string, and fill pointer */
  68. } *command_builder;
  69.  
  70. /* This structure is basically a typeahead queue: things like wait-reading-
  71.    process-output will delay the execution of keyboard and mouse events by
  72.    pushing them here.  I'd like this to be private to event-stream.c, but
  73.    alloc.c needs to know about it to mark the events on it during GC.
  74.  */
  75. struct command_event_queue *command_event_queue;
  76.  
  77.  
  78. extern Lisp_Object Vkeyboard_translate_table;
  79. extern Lisp_Object Vthis_command, Vlast_command;
  80. extern Lisp_Object minibuf_window, selected_window;
  81.  
  82. /* The number of keystrokes since the last auto-save. */
  83. static int keystrokes;
  84.  
  85.  
  86.  
  87. #if 0
  88. static void
  89. maybe_kbd_translate (event)
  90.      Lisp_Object event;
  91. {
  92.   int c;
  93.   if (!STRINGP (Vkeyboard_translate_table)) return;
  94.   c = event_to_character (XEVENT (event), 0);
  95.   if (c == -1) return;
  96.   if (XSTRING (Vkeyboard_translate_table)->size <= c) return;
  97.   c = XSTRING (Vkeyboard_translate_table)->data[c];
  98.   Fcharacter_to_event (make_number (c), event);
  99. }
  100. #endif
  101.  
  102. #define    max(a,b) ((a)>(b)?(a):(b))
  103.  
  104. static void
  105. maybe_do_auto_save ()
  106. {
  107.   keystrokes++;
  108.   if (auto_save_interval > 0 &&
  109.       keystrokes > max (auto_save_interval, 20) &&
  110.       !detect_input_pending ())
  111.     {
  112.       keystrokes = 0;
  113.       Fdo_auto_save (Qnil);
  114.     }
  115. }
  116.  
  117.  
  118. static Lisp_Object
  119. print_help (object)
  120.      Lisp_Object object;
  121. {
  122.   Fprinc (object, Qnil);
  123.   return Qnil;
  124. }
  125.  
  126.  
  127. static void
  128. execute_help_form (event)
  129.      Lisp_Object event;
  130. {
  131.   Lisp_Object tem0;
  132.   int count = specpdl_ptr - specpdl;
  133.   record_unwind_protect (Fset_window_configuration,
  134.              Fcurrent_window_configuration ());
  135.   tem0 = Feval (Vhelp_form);
  136.   if (STRINGP (tem0))
  137.     internal_with_output_to_temp_buffer ("*Help*", print_help, tem0, Qnil);
  138.   cancel_echoing ();
  139.   Fnext_command_event (event);
  140.   /* Remove the help from the screen */
  141.   unbind_to (count, Qnil);
  142.   redisplay ();
  143.   if (event_to_character (XEVENT (event), 0) == ' ')
  144.     {
  145.       cancel_echoing ();
  146.       Fnext_command_event (event);
  147.     }
  148. }
  149.  
  150. extern Lisp_Object Vunread_command_event;
  151. extern Lisp_Object Vlast_command_event, Vlast_input_event;
  152. /* These two for compatibility; they are V... because they can be nil. */
  153. extern Lisp_Object Vlast_command_char, Vlast_input_char;
  154. extern Lisp_Object Vlast_input_time;
  155.  
  156. extern Lisp_Object Vexecuting_macro;
  157. extern int defining_kbd_macro;
  158. extern void store_kbd_macro_event ();
  159. extern void pop_kbd_macro_event ();
  160.  
  161. int
  162. detect_input_pending ()
  163. {
  164.   /* Always call the event_pending_p hook even if there's an unread
  165.      character, because that might do some needed ^G detection (on
  166.      systems without SIGIO, for example).
  167.    */
  168.   if (event_stream && event_stream->event_pending_p (1)) return 1;
  169.   if (!NILP (Vunread_command_event)) return 1;
  170.   if (command_event_queue->head)
  171.     {
  172.       struct Lisp_Event *e;
  173.       for (e = command_event_queue->head; e; e = e->next)
  174.     if (e->event_type != eval_event)
  175.       return 1;
  176.     }
  177.   return 0;
  178. }
  179.  
  180. DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
  181.   "T if command input is currently available with no waiting.\n\
  182. Actually, the value is nil only if we can be sure that no input is available.")
  183.   ()
  184. {
  185.   return (detect_input_pending() ? Qt : Qnil);
  186. }
  187.  
  188. /* Add an event to the back of the queue: it will be the next event read after
  189.    all pending events.   This only works on keyboard, mouse-click, menu, and
  190.    eval events.
  191.  */
  192. void
  193. enqueue_command_event (event)
  194.      Lisp_Object event;
  195. {
  196.   if (command_event_queue->tail &&
  197.       command_event_queue->tail == XEVENT (event))
  198.     abort();
  199.  
  200.   if (command_event_queue->tail)
  201.     command_event_queue->tail->next = XEVENT (event);
  202.   else
  203.     command_event_queue->head = XEVENT (event);
  204.   command_event_queue->tail = XEVENT (event);
  205.   
  206.   if (XEVENT (event) == XEVENT (event)->next)
  207.     abort ();
  208. }
  209.  
  210. DEFUN ("enqueue-eval-event", Fenqueue_command_event, Senqueue_command_event,
  211.        2, 2, 0, 
  212.        "Add an eval event to the back of the queue.\n\
  213. (enqueue-eval-event <function> <object>)\n\
  214. It will be the next event read after all pending events.")
  215.   (function, object)
  216. Lisp_Object function, object;
  217. {
  218.   Lisp_Object event;
  219.  
  220.   event = Fallocate_event ();
  221.  
  222.   XEVENT (event)->event_type = eval_event;
  223.   XEVENT (event)->event.eval.function = function;
  224.   XEVENT (event)->event.eval.object = object;
  225.   enqueue_command_event (event);
  226.  
  227.   return event;
  228. }
  229.  
  230. /* the number of keyboard characters read.  callint.c wants this. 
  231.  */
  232. int num_input_chars;
  233.  
  234. static void store_recent_key ();
  235.  
  236. static void
  237. next_event_internal (target_event, allow_queued)
  238.      Lisp_Object target_event;
  239.      int allow_queued;
  240. {
  241.   Lisp_Object event;
  242.   struct Lisp_Event *e;
  243.  
  244.   if (XEVENT (target_event)->next)
  245.     abort ();
  246.  
  247.   if (allow_queued && (e = command_event_queue->head))
  248.     {
  249.       XSET (event, Lisp_Event, e);
  250.       command_event_queue->head = e->next;
  251.       if (! e->next)
  252.     command_event_queue->tail = 0;
  253.       e->next = 0;
  254.     }
  255.   else
  256.     {
  257.       /* #### Temporary hack to make emacs exit "gracefully" when it tries
  258.      to run in tty-mode for some reason.  This is the latest time we
  259.      can make this check, because we're about to deref event_stream.
  260.        */
  261.       if (! event_stream)
  262.     {
  263.       extern Display *x_current_display;
  264.       Fsend_string_to_terminal (build_string ("\n\n\
  265. This version of emacs only runs under X Windows (for now).\n\
  266. Check that your $DISPLAY environment variable is properly set.\n"));
  267.       if (!x_current_display) Vwindow_system = Qnil;
  268.       Fkill_emacs (make_number (69));
  269.     }
  270.  
  271.       /* The command_event_queue was empty.  Wait for an event.
  272.        */
  273.       event = Fallocate_event ();
  274.       e = XEVENT (event);
  275.       event_stream->next_event_cb (e);
  276.       if (e->event_type == key_press_event &&
  277.       event_to_character (e, 0) == interrupt_char)
  278.     interrupt_signal (0);
  279.     }
  280.  
  281.   Fcopy_event (event, target_event);
  282.   Fdeallocate_event (event);
  283.   return;
  284. }
  285.  
  286.  
  287. void maybe_status_notify (void);
  288. void update_status (struct Lisp_Process *);
  289. void finalize_kbd_macro_chars (void);
  290. void deactivate_process (Lisp_Object);
  291. int read_process_output (Lisp_Object, int);
  292.  
  293.  
  294. DEFUN ("next-event", Fnext_event, Snext_event, 1, 1, 0,
  295.   "Given an event structure, fills it in with the next event available\n\
  296. from the window system or terminal driver.  Pass this object to\n\
  297. dispatch-event to handle it.  See also the function next-command-event,\n\
  298. which is often more appropriate.")
  299.      (event)
  300.      Lisp_Object event;
  301. {
  302.   int store_this_key = 0;
  303.  
  304.   CHECK_EVENT (event, 0);
  305.   if (XEVENT (event)->event_type == dead_event)
  306.     error ("next-event called with a deallocated event!");
  307.  
  308.   if (! detect_input_pending () && NILP (Vexecuting_macro))
  309.     redisplay ();
  310.  
  311.   /* If there is an unread-command-event, simply return it.
  312.      But do some error checking to make sure the user hasn't put something
  313.      in the unread-command-event that they shouldn't have.
  314.      This does not update this-command-keys and recent-keys.
  315.    */
  316.   if (!NILP (Vunread_command_event))
  317.     {
  318.       if (!EVENTP (Vunread_command_event) ||
  319.       (XEVENT (Vunread_command_event)->event_type != key_press_event &&
  320.        XEVENT (Vunread_command_event)->event_type != button_press_event &&
  321.        XEVENT (Vunread_command_event)->event_type != button_release_event&&
  322.        XEVENT (Vunread_command_event)->event_type != menu_event))
  323.     {
  324.       Lisp_Object bogus = Vunread_command_event;
  325.       Vunread_command_event = Qnil;
  326.       while (1)
  327.         Fsignal (Qwrong_type_argument,
  328.              Fcons (Qeventp,
  329.                 Fcons (bogus,
  330.                    Fcons (intern ("unread-command-event"),
  331.                       Qnil))));
  332.     }
  333.       if (!EQ (Vunread_command_event, event))
  334.     Fcopy_event (Vunread_command_event, event);
  335.       Vunread_command_event = Qnil;
  336.     }
  337.  
  338.   /* If we're executing a keyboard macro, take the next event from that,
  339.      and update this-command-keys and recent-keys.
  340.      Note that the unread-command-event takes prescedence over kbd macros.
  341.    */
  342.   else if (!NILP (Vexecuting_macro))
  343.     {
  344.       pop_kbd_macro_event (event);  /* This throws past us at end-of-macro. */
  345.       store_this_key = 1;
  346.     }
  347.   /* Otherwise, read a real event, possibly from the command_event_queue,
  348.      and update this-command-keys and recent-keys.
  349.    */
  350.   else
  351.     {
  352.       next_event_internal (event, 1);
  353.       store_this_key = 1;
  354.     }
  355.  
  356.   maybe_status_notify ();    /* Notice process change */
  357.  
  358.   switch (XEVENT (event)->event_type)
  359.     {
  360.     case key_press_event:    /* any key input can trigger autosave */
  361.       maybe_do_auto_save ();
  362.       num_input_chars++;
  363.       /* fall through */
  364.     case button_press_event:    /* key or mouse input can trigger prompting */
  365.       if (store_this_key)
  366.     echo_char_event (XEVENT (event));
  367.       /* fall through */
  368.     case button_release_event:
  369.     case menu_event:
  370.  
  371.       /* Store the last-input-event.  The semantics of this is that it is
  372.      the thing most recently returned by next-command-event.  It need
  373.      not have come from the keyboard or a keyboard macro, it may have
  374.      come from unread-command-event.  It's always a command-event, (a
  375.      key, click, or menu selection) never a motion or process event.
  376.        */
  377.       if (!EVENTP (Vlast_input_event))
  378.     Vlast_input_event = Fallocate_event ();
  379.       if (XEVENT (Vlast_input_event)->event_type == dead_event)
  380.     {
  381.       Vlast_input_event = Fallocate_event ();
  382.       error ("Someone deallocated the last-input-event!");
  383.     }
  384.       if (! EQ (event, Vlast_input_event))
  385.     Fcopy_event (event, Vlast_input_event);
  386.       
  387.       /* last-input-char and last-input-time are derived from last-input-event.
  388.        */
  389.       Vlast_input_char = Fevent_to_character (Vlast_input_event, Qnil);
  390.       Vlast_input_time = Fcurrent_time_seconds (Vlast_input_time);
  391.  
  392.       /* If this key came from the keyboard or from a keyboard macro, then
  393.      it goes into the recent-keys and this-command-keys vectors.
  394.      If this key came from the keyboard, and we're defining a keyboard
  395.      macro, then it goes into the macro.
  396.        */
  397.       if (store_this_key)
  398.     {
  399.       store_recent_key (event);
  400.       if (defining_kbd_macro && NILP (Vexecuting_macro))
  401.         {
  402.           if (command_builder->event_count == 0)
  403.         finalize_kbd_macro_chars ();
  404.           store_kbd_macro_event (event);
  405.         }
  406.     }
  407.     default:
  408.       ;
  409.     }
  410.  
  411.   /* If this is the help char and there is a help form, then execute the
  412.      help form and swallow this character.  This is the only place where
  413.      calling Fnext_event() can cause arbitrary lisp code to run.  Note
  414.      that execute_help_form() calls Fnext_command_event(), which calls
  415.      this function, as well as Fdispatch_event.
  416.    */
  417.   if (!NILP (Vhelp_form) &&
  418.       XEVENT (event)->event_type == key_press_event &&
  419.       help_char == event_to_character (XEVENT (event), 0))
  420.     execute_help_form (event);
  421.  
  422.   return event;
  423. }
  424.  
  425.  
  426. DEFUN ("next-command-event", Fnext_command_event, Snext_command_event, 1, 1, 0,
  427.   "Given an event structure, fills it in with the next keyboard, mouse\n\
  428. press, or mouse release event available from the user.  If there are\n\
  429. non-command events available (mouse motion, sub-process output, etc) then\n\
  430. these will be executed (with `dispatch-event') and discarded.  This \n\
  431. function is provided as a convenience; it is equivalent to the elisp code\n\
  432. \n\
  433.     (while (progn\n\
  434.         (next-event event)\n\
  435.             (not (or (key-press-event-p event)\n\
  436.                      (button-press-event-p event)\n\
  437.                      (button-release-event-p event)\n\
  438.                      (menu-event-p event))))\n\
  439.       (dispatch-event event))\n")
  440.      (event)
  441.     Lisp_Object event;
  442. {
  443.   maybe_echo_keys ();  /* ## This sucks bigtime */
  444.   while (1) {
  445.     Fnext_event (event);
  446.     switch (XEVENT (event)->event_type) {
  447.     case key_press_event:
  448.     case button_press_event:
  449.     case button_release_event:
  450.     case menu_event:
  451.       return event;
  452.     default:
  453.       Fdispatch_event (event);
  454.     }
  455.   }
  456. }
  457.  
  458.  
  459. DEFUN ("read-char", Fread_char, Sread_char, 0, 0, 0,
  460.   "Read a character from the command input (keyboard or macro).\n\
  461. If a mouse click is detected, an error is signalled.  The character typed\n\
  462. is returned as an ASCII value.  This is most likely the wrong thing for you\n\
  463. to be using: consider using the `next-command-event' function instead.")
  464.   ()
  465. {
  466.   register int val = -1;
  467.   Lisp_Object event = Fallocate_event ();
  468.   struct gcpro gcpro1;
  469.   GCPRO1 (event);
  470.  
  471.   Fnext_command_event (event);
  472.   switch (XEVENT (event)->event_type) {
  473.   case key_press_event:
  474.   case button_press_event:
  475.   case button_release_event:
  476.   case menu_event:
  477.     if (XEVENT (event)->event_type == key_press_event)
  478.       val = event_to_character (XEVENT (event), 0);
  479.     if (val == -1)
  480.       Fsignal (Qerror,
  481.            Fcons(build_string ("key read has no ASCII equivalent"),
  482.              Fcons (event, Qnil)));
  483.     Fdeallocate_event (event);
  484.     UNGCPRO;
  485.     return (make_number (val));
  486.   default:
  487.     abort ();
  488.   }
  489. }
  490.  
  491.  
  492. DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
  493.   "Discard the contents of the terminal input buffer.\n\
  494. Also cancel any kbd macro being defined.")
  495.   ()
  496. {
  497.   /* This throws away user-input on the queue, but doesn't process any
  498.      events.  Calling Fdispatch_event() here leads to a race condition.
  499.    */
  500.   Lisp_Object event, event2;
  501.   struct Lisp_Event *e, *e2, *head, *tail;
  502.   Lisp_Object oiq = Vinhibit_quit;
  503.  
  504.   defining_kbd_macro = 0;
  505.   command_builder->event_count = 0;
  506.   command_builder->leaf = Qnil;
  507.   Vinhibit_quit = Qt;
  508.  
  509.   event = Fallocate_event ();
  510.   e = XEVENT (event);
  511.   tail = 0;
  512.  
  513.   while (command_event_queue->head ||
  514.      (event_stream && event_stream->event_pending_p (1)))
  515.     {
  516.       /* This will take stuff off the command_event_queue, or read it
  517.      from the event_stream, but it will not block.
  518.        */
  519.       next_event_internal (event, 1);
  520.  
  521.       /* If the event is a user event, ignore it.
  522.        */
  523.       if (e->event_type == key_press_event ||
  524.       e->event_type == button_press_event ||
  525.       e->event_type == button_release_event ||
  526.       e->event_type == menu_event)
  527.     continue;
  528.  
  529.       /* Otherwise, chain the event onto our list of events not to ignore,
  530.      and keep reading until the queue is empty.  This does not mean
  531.      that if a subprocess is generating an infinite amount of output,
  532.      we will never terminate, because this loop ends as soon as there
  533.      are no more user events on the command_event_queue or event_stream.
  534.        */
  535.       event2 = Fcopy_event (event, Qnil);
  536.       e2 = XEVENT (event2);
  537.       if (tail)
  538.     tail->next = e2;
  539.       else
  540.     head = e2;
  541.       tail = e2;
  542.     }
  543.  
  544.   if (command_event_queue->head || command_event_queue->tail)
  545.     abort ();
  546.  
  547.   /* Now tack our chain of events back on to the front of the queue.
  548.      Actually, since the queue is now drained, we can just replace it.
  549.      The effect of this will be that we have deleted all user events
  550.      from the input stream without changing the relative ordering of
  551.      any other events.  (Some events may have been taken from the
  552.      event_stream and added to the command_event_queue, however.)
  553.  
  554.      At this time, the command_event_queue will contain only eval_events.
  555.    */
  556.   if (tail)
  557.     {
  558.       command_event_queue->head = head;
  559.       command_event_queue->tail = tail;
  560.     }
  561.  
  562.   Fdeallocate_event (event);
  563.  
  564.   Vinhibit_quit = oiq;
  565.   return Qnil;
  566. }
  567.  
  568.  
  569. DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output,
  570.   0, 1, 0,
  571.   "Allow any pending output from subprocesses to be read by Emacs.\n\
  572. It is read into the process' buffers or given to their filter functions.\n\
  573. Non-nil arg PROCESS means do not return until some output has been received\n\
  574. from PROCESS.")
  575.   (proc)
  576.      register Lisp_Object proc;
  577. {
  578.   Lisp_Object event = 0;
  579.   struct gcpro gcpro1;
  580.   GCPRO1 (event);
  581.  
  582.   if (!NILP (proc)) CHECK_PROCESS (proc, 0);
  583.   event = Fallocate_event ();
  584.   while (!NILP (proc) ||
  585.      /* Calling detect_input_pending() is the wrong thing here, because
  586.         that considers the Vunread_command_event and command_event_queue.
  587.         We don't need to look at the command_event_queue because we are
  588.         only interested in process events, which don't go on that.  In
  589.         fact, we can't read from it anyway, because we put stuff on it.
  590.  
  591.         Note that event_stream->event_pending_p must be called in such
  592.         a way that it says whether any events *of any kind* are ready,
  593.         not just user events, or (accept-process-output nil) will fail
  594.         to dispatch any process events that may be on the queue.  It is
  595.         not clear to me that this is important, because the top-level
  596.         loop will process it, and I don't think that there is ever a
  597.         time when one calls accept-process-output with a nil argument
  598.         and really need the processes to be handled.
  599.       */
  600.      event_stream->event_pending_p (0))
  601.     {
  602.       QUIT;
  603.       next_event_internal (event, 0);
  604.       switch (XEVENT (event)->event_type)
  605.     {
  606.     case process_event:
  607.       {
  608.         if (EQ (XEVENT (event)->event.process.process, proc))
  609.           proc = Qnil;
  610.         Fdispatch_event (event);
  611.  
  612.         /* We must call status_notify here to allow the
  613.            event_stream->unselect_process_cb to be run if appropriate.
  614.            Otherwise, dead fds may be selected for, and we will get a
  615.            continuous stream of process events for them.  Since we don't
  616.            return until all process events have been flushed, we would
  617.            get stuck here, processing events on a process whose status
  618.            was 'exit.  Call this after dispatch-event, or the fds will
  619.            have been closed before we read the last data from them.
  620.            It's safe for the filter to signal an error because
  621.            status_notify() will be called on return to top-level.
  622.          */
  623.         maybe_status_notify ();
  624.         break;
  625.       }
  626.     case timeout_event:
  627.     case pointer_motion_event:
  628.     case magic_event:
  629.       Fdispatch_event (event);
  630.       break;
  631.     default:
  632.       enqueue_command_event (Fcopy_event (event, Qnil));
  633.     }
  634.     }
  635.   Fdeallocate_event (event);
  636.   UNGCPRO;
  637.   return Qnil;
  638. }
  639.  
  640.  
  641. /* sit-for, sleep-for, and timeouts.
  642.  */
  643.  
  644. static unsigned long
  645. lisp_number_to_milliseconds (secs, allow_0)
  646.      Lisp_Object secs;
  647.      int allow_0;
  648. {
  649.   unsigned long msecs;
  650. #ifdef LISP_FLOAT_TYPE
  651.   double fsecs;
  652.   CHECK_NUMBER (secs, 0);
  653.   fsecs = XFLOATINT (secs);
  654. #else
  655.   long fsecs;
  656.   CHECK_NUMBER (secs, 0);
  657.   fsecs = XINT (secs);
  658. #endif
  659.   msecs = 1000 * fsecs;
  660.   if (fsecs < 0)
  661.     return Fsignal (Qerror, Fcons (build_string ("timeout is negative"),
  662.                    Fcons (secs, Qnil)));
  663.   if (!allow_0 && fsecs == 0)
  664.     return Fsignal (Qerror, Fcons (build_string ("timeout is non-positive"),
  665.                    Fcons (secs, Qnil)));
  666.   if (fsecs >= (((unsigned int) 0xFFFFFFFF) / 1000))
  667.     return Fsignal (Qerror, Fcons (build_string (
  668.              "timeout would exceed 32 bits when represented in milliseconds"),
  669.                 Fcons (secs, Qnil)));
  670.   return msecs;
  671. }
  672.  
  673.  
  674. DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 1, 0,
  675.   "Pause, without updating display, for ARG seconds.\n\
  676. ARG may be a float, meaning pause for some fractional part of a second.")
  677.   (n)
  678.      Lisp_Object n;
  679. {
  680.   unsigned int msecs = lisp_number_to_milliseconds (n, 1);
  681.   int id;
  682.   Lisp_Object event = 0;
  683.   struct gcpro gcpro1;
  684.  
  685.   if (!event_stream)
  686.     return Qnil;
  687.  
  688.   GCPRO1 (event);
  689.  
  690.   id = event_stream->generate_wakeup_cb (msecs, 0, Qnil, Qnil);
  691.   event = Fallocate_event ();
  692.   while (1)
  693.     {
  694.       QUIT;
  695.       /* We're a generator of the command_event_queue, so we can't be a
  696.      consumer as well.  We don't care about command and eval-events
  697.      anyway.
  698.        */
  699.       next_event_internal (event, 0);    /* blocks */
  700.       switch (XEVENT (event)->event_type)
  701.     {
  702.     case timeout_event:
  703.       if (XEVENT (event)->event.timeout.id_number == id)
  704.         goto DONE;
  705.       /* else fall through */
  706.     case pointer_motion_event:
  707.     case process_event:
  708.     case magic_event:
  709.       Fdispatch_event (event);
  710.       break;
  711.     default:
  712.       enqueue_command_event (Fcopy_event (event, Qnil));
  713.     }
  714.     }
  715.  DONE:
  716.   Fdeallocate_event (event);
  717.   UNGCPRO;
  718.   return Qnil;
  719. }
  720.  
  721.  
  722. DEFUN ("sit-for", Fsit_for, Ssit_for, 1, 2, 0,
  723.   "Perform redisplay, then wait for ARG seconds or until user input is\n\
  724. available.  ARG may be a float, meaning a fractional part of a second.\n\
  725. Optional second arg non-nil means don't redisplay, just wait for input.\n\
  726. Redisplay is preempted as always if user input arrives, and does not\n\
  727. happen if input is available before it starts.\n\
  728. Value is t if waited the full time with no input arriving.")
  729.   (n, nodisp)
  730.      Lisp_Object n, nodisp;
  731. {
  732.   unsigned long msecs = lisp_number_to_milliseconds (n, 1);
  733.   Lisp_Object event, result;
  734.   struct Lisp_Event *e;
  735.   struct gcpro gcpro1;
  736.   int id;
  737.  
  738.   /* The unread-command-event counts as pending input */
  739.   if (!NILP (Vunread_command_event))
  740.     return Qnil;
  741.  
  742.   /* If the command-builder already has user-input on it (not eval events)
  743.      then that means we're done too.
  744.    */
  745.   for (e = command_event_queue->head; e; e = e->next)
  746.     if (e->event_type == key_press_event ||
  747.     e->event_type == button_press_event ||
  748.     e->event_type == button_release_event ||
  749.     e->event_type == menu_event)
  750.       return Qnil;
  751.  
  752.   /* If we're in a macro, or noninteractive, or early in temacs, then
  753.      don't wait. */
  754.   if (noninteractive || !NILP (Vexecuting_macro) || !event_stream)
  755.     return Qt;
  756.  
  757.   /* Otherwise, start reading events from the event_stream.
  758.      Do this loop at least once even if (sit-for 0) so that we
  759.      redisplay when no input pending.
  760.    */
  761.   event = Fallocate_event ();
  762.   GCPRO1 (event);
  763.  
  764.   if (msecs <= 0)
  765.     id = 0;
  766.   else
  767.     id = event_stream->generate_wakeup_cb (msecs, 0, Qnil, Qnil);
  768.  
  769.   while (1)
  770.     {
  771.       /* If there is no user input pending, then redisplay.
  772.        */
  773.       if (!event_stream->event_pending_p (1) && NILP (nodisp))
  774.     redisplay_preserving_echo_area ();
  775.  
  776.       /* If we're no longer waiting for a timeout, bug out. */
  777.       if (! id)
  778.     {
  779.       result = Qt;
  780.       goto DONE;
  781.     }
  782.  
  783.       QUIT;
  784.       /* We're a generator of the command_event_queue, so we can't be a
  785.      consumer as well.  In fact, we know there's nothing on the
  786.      command_event_queue that we didn't just put there.
  787.        */
  788.       next_event_internal (event, 0);    /* blocks */
  789.  
  790.       switch (XEVENT (event)->event_type)
  791.     {
  792.     case key_press_event:
  793.     case button_press_event:
  794.     case button_release_event:
  795.     case menu_event:
  796.       result = Qnil;
  797.       goto DONE;
  798.  
  799.     case eval_event:
  800.       /* eval-events get delayed until later. */
  801.       enqueue_command_event (Fcopy_event (event, Qnil));
  802.       break;
  803.  
  804.     case timeout_event:
  805.       if (XEVENT (event)->event.timeout.id_number == id)
  806.         {
  807.           id = 0;    /* assert that we are no longer waiting for it. */
  808.           result = Qt;
  809.           goto DONE;
  810.         }
  811.       else        /* a timeout that wasn't the one we're waiting for */
  812.         Fdispatch_event (event);
  813.       break;
  814.       
  815.     case process_event:
  816.     case pointer_motion_event:
  817.     case magic_event:
  818.       Fdispatch_event (event);
  819.       break;
  820.  
  821.     default:
  822.       abort ();
  823.     }
  824.     }
  825.  
  826.  DONE:
  827.   /* If our timeout has not been signalled yet, disable it. */
  828.   if (id)
  829.     event_stream->disable_wakeup_cb (id);
  830.  
  831.   /* Put back the event (if any) that made Fsit_for() exit before the
  832.      timeout.  Note that it is being added to the back of the queue, which
  833.      would be inappropriate if there were any user events on the queue
  834.      already: we would be misordering them.  But we know that there are
  835.      no user-events on the queue, or else we would not have reached this
  836.      point at all.
  837.    */
  838.   if (NILP (result))
  839.     enqueue_command_event (event);
  840.   else
  841.     Fdeallocate_event (event);
  842.   UNGCPRO;
  843.   return result;
  844. }
  845.  
  846.  
  847. #ifdef LISP_FLOAT_TYPE
  848. DEFUN ("sleep-for-millisecs", Fsleep_for_millisecs, Ssleep_for_millisecs,
  849.   1, 1, 0,
  850.   "Pause, without updating display, for ARG milliseconds.\n\
  851. This function is obsolete; call `sleep-for' with a float instead.")
  852.   (n)
  853.      Lisp_Object n;
  854. {
  855.   Lisp_Object args[2];
  856.   args[0] = Ffloat (n);
  857.   args[1] = make_number (1000);
  858.   return Fsleep_for (Fquo (2, args));
  859. }
  860. #endif
  861.  
  862.  
  863. DEFUN ("add-timeout", Fadd_timeout, Sadd_timeout, 3, 4, 0,
  864.  "SECS is a number of seconds, expressed as an integer or a float.\n\
  865. FUNCTION will be called after that many seconds have elapsed, with one\n\
  866. argument, the given OBJECT.  If the optional RESIGNAL argument is provided,\n\
  867. then after this timeout expires, `add-timeout' will automatically be called\n\
  868. again with RESIGNAL as the first argument.\n\
  869. \n\
  870. This function returns an object which is the `id' of this particular timeout.\n\
  871. You can pass that object to `disable-timeout' to turn off the timeout before\n\
  872. it has been signalled.\n\
  873. \n\
  874. The number of seconds may be expressed as a floating-point number, in which\n\
  875. case some fractional part of a second will be used.  Caveat: the usable\n\
  876. timeout granularity will vary from system to system.\n\
  877. \n\
  878. Adding a timeout causes a timeout event to be returned by `next-event', and\n\
  879. the function will be invoked by `dispatch-event,' so if emacs is in a tight\n\
  880. loop, the function will not be invoked until the next call to sit-for or\n\
  881. until the return to top-level (the same is true of process filters.)\n\
  882. \n\
  883. WARNING: if you are thinking of calling add-timeout from inside of a\n\
  884. callback function as a way of resignalling a timeout, think again.  There\n\
  885. is a race condition.  That's why the RESIGNAL argument exists.")
  886.      (secs, function, object, resignal)
  887.      Lisp_Object secs, function, object, resignal;
  888. {
  889.   unsigned long msecs = lisp_number_to_milliseconds (secs, 0);
  890.   unsigned long msecs2 = (NILP (resignal) ? 0 :
  891.               lisp_number_to_milliseconds (resignal, 0));
  892.   int id;
  893.   Lisp_Object lid;
  894.   if (noninteractive) error ("can't add timeouts in batch mode");
  895.   if (! event_stream) abort ();
  896.   id = event_stream->generate_wakeup_cb (msecs, msecs2, function, object);
  897.   lid = make_number (id);
  898.   if (id != XINT (lid)) abort ();
  899.   return lid;
  900. }
  901.  
  902.  
  903. DEFUN ("disable-timeout", Fdisable_timeout, Sdisable_timeout, 1, 1, 0,
  904.  "Given a timeout id number as returned by `add-timeout', this function\n\
  905. will cause that timeout to not be signalled if it hasn't been already.")
  906.      (id)
  907.      Lisp_Object id;
  908. {
  909.   CHECK_FIXNUM (id, 0);
  910.   if (noninteractive) error ("can't use timeouts in batch mode");
  911.   if (! event_stream) abort ();
  912.   event_stream->disable_wakeup_cb (XINT (id));
  913.   return Qnil;
  914. }
  915.  
  916.  
  917. /* This handy little function is used by xselect.c and editorside.c to
  918.    wait for replies from processes that aren't really processes (that is,
  919.    the X server and the Energize server.)
  920.  */
  921. void
  922. wait_delaying_user_input (int (*predicate) (void *arg), void *predicate_arg)
  923. {
  924.   Lisp_Object event = Fallocate_event ();
  925.   struct gcpro gcpro1;
  926.   GCPRO1 (event);
  927.  
  928.   while (!(*predicate) (predicate_arg))
  929.     {
  930.       QUIT;
  931.       /* We're a generator of the command_event_queue, so we can't be a
  932.      consumer as well.  Also, we have no reason to consult the
  933.      command_event_queue; there are only user and eval-events there,
  934.      and we'd just have to put them back anyway.
  935.        */
  936.       next_event_internal (event, 0);
  937.       switch (XEVENT (event)->event_type)
  938.     {
  939.     case key_press_event:
  940.     case button_press_event:
  941.     case button_release_event:
  942.     case menu_event:
  943.     case eval_event:
  944.       enqueue_command_event (Fcopy_event (event, Qnil));
  945.       break;
  946.     default:
  947.       Fdispatch_event (event);
  948.     }
  949.     }
  950.   UNGCPRO;
  951. }
  952.  
  953.  
  954. /* ## This macro copied from process.c; should be in process.h */
  955. #ifdef HAVE_SOCKETS
  956. # define NETCONN_P(p) (XGCTYPE (XPROCESS (p)->childp) == Lisp_String)
  957. #else
  958. # define NETCONN_P(p) 0
  959. #endif
  960.  
  961. extern Lisp_Object Qrun, Qexit;
  962. extern int process_tick;
  963.  
  964. static void
  965. deactivate_netconn (p)
  966.      Lisp_Object p;
  967. {
  968.   XSETINT (XPROCESS (p)->tick, ++process_tick);
  969.   if (!NILP (XPROCESS (p)->raw_status_low))
  970.     update_status (XPROCESS (p));
  971.   if (EQ (XPROCESS (p)->status, Qrun))
  972.     XPROCESS (p)->status = Fcons (Qexit, Fcons (make_number (256), Qnil));
  973.   deactivate_process (p);
  974. }
  975.  
  976. static void dispatch_menu_event ();
  977. static Lisp_Object compose_command ();
  978.  
  979.  
  980. /* If execute_p is true, this does what you expect: execute the event.
  981.    But if events are keyboard or mouse events, they are executed by passing 
  982.    them to the command builder, which doesn't actually run a command until
  983.    a complete key sequence has been read.  So, if the execute_p arg is
  984.    false, this returns the complete command sequence without executing it,
  985.    once it has read one, and returns nil before then.
  986.    This function is the guts of Fread_key_sequence and Fdispatch_event.
  987.  */
  988. static Lisp_Object 
  989. dispatch_event_internal (event, execute_p)
  990.      Lisp_Object event;
  991.      int execute_p;
  992. {
  993.   struct Lisp_Event *e;
  994.   Lisp_Object result = Qnil;
  995.  
  996.   e = XEVENT (event);
  997.  
  998.   switch (e->event_type) {
  999.  
  1000.   case key_press_event:
  1001.   case button_press_event:
  1002.   case button_release_event:
  1003.     result = compose_command (e, execute_p);
  1004.     break;
  1005.  
  1006.   case menu_event:
  1007.     if (execute_p)
  1008.       dispatch_menu_event (e);
  1009.     else
  1010.       {
  1011.     /* If a menu item is selected while in the midst of read-key-sequence,
  1012.        return that event, so that describe-key works for menu items
  1013.        as well.
  1014.      */
  1015.     command_builder->event_count = 0;
  1016.     command_builder->leaf = Qnil;
  1017.     result = compose_command (e, 0);
  1018.       }
  1019.     break;
  1020.     
  1021.   case eval_event:
  1022.     dispatch_menu_event (e);
  1023.     break;
  1024.  
  1025.   case pointer_motion_event:
  1026.     if (!NILP (Vmouse_motion_handler))
  1027.       call1 (Vmouse_motion_handler, event);
  1028.     break;
  1029.  
  1030.   case process_event:
  1031.     {
  1032.       Lisp_Object p = e->event.process.process;
  1033.       int fd = XFASTINT (XPROCESS (p)->infd);
  1034.       int value;
  1035.       if (fd)
  1036.     {
  1037.       if (NETCONN_P (p) && ! (value = read_process_output (p, fd)))
  1038.         deactivate_netconn (p);
  1039.       while ((value = read_process_output (p, fd)) > 0);
  1040.     }
  1041.       break;
  1042.     }
  1043.   case timeout_event:
  1044.     if (!NILP (e->event.timeout.function))
  1045.       call1 (e->event.timeout.function, e->event.timeout.object);
  1046.     break;
  1047.  
  1048.   case magic_event:
  1049.     event_stream->handle_magic_event_cb (e);
  1050.     break;
  1051.  
  1052.   default:
  1053.     abort();
  1054.   }
  1055.  
  1056.   return result;
  1057. }
  1058.  
  1059.  
  1060. /*
  1061.  * character prompting
  1062.  */
  1063.  
  1064. static void
  1065. echo_char_event (event)
  1066.      struct Lisp_Event *event;
  1067. {
  1068.   int len = command_builder->echoptr - command_builder->echobuf;
  1069.   if (len > 200) return;
  1070.   format_event_object (command_builder->echoptr, event, 1);
  1071.   command_builder->echoptr += strlen (command_builder->echoptr);
  1072.   if (len == 0 && help_char == event_to_character (event, 0))
  1073.     {
  1074.       strcpy (command_builder->echoptr, " (Type ? for further options)");
  1075.       command_builder->echoptr += strlen (command_builder->echoptr);
  1076.     }
  1077.   command_builder->echoptr [0] = ' ';
  1078.   command_builder->echoptr [1] = '-';
  1079.   command_builder->echoptr [2] = ' ';
  1080.   command_builder->echoptr [3] = 0;
  1081.   command_builder->echoptr++;
  1082. }
  1083.  
  1084. static void
  1085. echo_prompt (str)
  1086.      char *str;
  1087. {
  1088.   int len = strlen (str);
  1089.   if (len > 200) return;
  1090.   memcpy (command_builder->echobuf, str, len+1);
  1091.   command_builder->echoptr = command_builder->echobuf + len;
  1092.   command_builder->echo_keys = 1;
  1093.   maybe_echo_keys ();
  1094. }
  1095.  
  1096. extern int minibuf_level;
  1097. static int echo_keystrokes;
  1098.  
  1099. static void
  1100. maybe_echo_keys ()
  1101. {
  1102.   /* Message turns off echoing unless more keystrokes turn it on again. */
  1103.   if (echo_area_glyphs && *echo_area_glyphs &&
  1104.       echo_area_glyphs != command_builder->echobuf) {
  1105.     command_builder->echo_keys = 0;
  1106.     return;
  1107.   }
  1108.  
  1109.   if (command_builder->echo_keys ||
  1110.       (minibuf_level == 0 &&
  1111.        echo_keystrokes > 0 &&
  1112.        ! NILP (Fsit_for (make_number (echo_keystrokes), Qt))))
  1113.     {
  1114.       command_builder->echo_keys = 1;
  1115.       /* This is like calling message(), but trickier */
  1116.       echo_area_glyphs = command_builder->echobuf;
  1117.     }
  1118. }
  1119.  
  1120. extern char* echo_area_glyphs;
  1121.  
  1122. void
  1123. cancel_echoing ()
  1124. {
  1125.   command_builder->echoptr = command_builder->echobuf;
  1126.   if (command_builder->echo_keys) {
  1127.     command_builder->echo_keys = 0;
  1128.     echo_area_glyphs = 0;
  1129.   }
  1130. }
  1131.  
  1132.  
  1133. /* Compare the current state of the command builder against the local and
  1134.    global keymaps; if there is no match, try again, case-insensitively.
  1135.    The binding found (if any) is stored in the CB's `leaf' slot.
  1136.    It may be a command or a keymap if we're not done yet.
  1137.  */
  1138. static Lisp_Object find_leaf_unwind ();
  1139.  
  1140. static void
  1141. command_builder_find_leaf (allow_menu_events_p)
  1142.      int allow_menu_events_p;
  1143. {
  1144.   struct gcpro gcpro1;
  1145.   int size = XVECTOR (command_builder->events)->size;
  1146.   int fill_pointer = command_builder->event_count;
  1147.   Lisp_Object event1;
  1148.   int count = specpdl_ptr - specpdl;
  1149.  
  1150.   /* This is an utterly sleazy way of simulating fill pointers...
  1151.    */
  1152.   GCPRO1 (XVECTOR (command_builder->events)->contents [fill_pointer]);
  1153.   gcpro1.nvars = size - fill_pointer;
  1154.   record_unwind_protect (find_leaf_unwind, size);
  1155.   XVECTOR (command_builder->events)->size = fill_pointer;
  1156.   event1 = XVECTOR (command_builder->events)->contents [0];
  1157.   if (allow_menu_events_p &&
  1158.       fill_pointer == 1 &&
  1159.       XEVENT (event1)->event_type == menu_event)
  1160.     {
  1161.       Lisp_Object fn = XEVENT (event1)->event.eval.function;
  1162.       Lisp_Object arg = XEVENT (event1)->event.eval.object;
  1163.       command_builder->leaf = Fcons (fn, Fcons (arg, Qnil));
  1164.     }
  1165.   else
  1166.     command_builder->leaf = Fkey_binding (command_builder->events);
  1167.   unbind_to (count, Qnil);
  1168.   UNGCPRO;
  1169.  
  1170.   /* If we didn't find a binding, and the last event in the sequence is
  1171.      a shifted character, then try again with the lowercase version.
  1172.    */
  1173.   if (NILP (command_builder->leaf))
  1174.     {
  1175.       Lisp_Object terminal = 
  1176.     XVECTOR (command_builder->events)->contents [fill_pointer - 1];
  1177.       if (XEVENT (terminal)->event_type == key_press_event &&
  1178.       ((XEVENT (terminal)->event.key.key >= 'A' &&
  1179.         XEVENT (terminal)->event.key.key <= 'Z') ||
  1180.        XEVENT (terminal)->event.key.modifiers & MOD_SHIFT))
  1181.     {
  1182.       Lisp_Object e2 = Fallocate_event ();
  1183.       GCPRO1 (e2);
  1184.       Fcopy_event (terminal, e2);
  1185.       if (XEVENT (e2)->event.key.modifiers & MOD_SHIFT)
  1186.         XEVENT (e2)->event.key.modifiers &= (~ MOD_SHIFT);
  1187.       else
  1188.         XEVENT (e2)->event.key.key += ('a'-'A');
  1189.       XVECTOR (command_builder->events)->contents [fill_pointer - 1] = e2;
  1190.       command_builder_find_leaf (allow_menu_events_p);
  1191.       /* If there was no match with the lower-case version either, then
  1192.          put back the upper-case event for the error message.
  1193.        */
  1194.       if (NILP (command_builder->leaf))
  1195.         {
  1196.           XVECTOR (command_builder->events)->contents [fill_pointer - 1]
  1197.         = terminal;
  1198.           Fdeallocate_event (e2);
  1199.         }
  1200.       UNGCPRO;
  1201.     }
  1202.     }
  1203.       
  1204. }
  1205.  
  1206. static Lisp_Object
  1207. find_leaf_unwind (size)
  1208.      int size;
  1209. {
  1210.   XVECTOR (command_builder->events)->size = size;
  1211.   return Qnil;
  1212. }
  1213.  
  1214.  
  1215. /* Every time a command-event (a key, button, or menu selection) is read by
  1216.    Fnext_event(), it is stored in the recent_keys_ring, in Vlast_input_event,
  1217.    and in Vthis_command_keys.  (Eval-events are not stored there.)
  1218.  
  1219.    Every time a command is invoked, Vlast_command_event is set to the last
  1220.    event in the sequence.
  1221.  
  1222.    This means that Vthis_command_keys is really about "input read since the
  1223.    last command was executed" rather than about "what keys invoked this
  1224.    command."  This is a little counterintuitive, but that's the way it 
  1225.    has always worked.
  1226.  
  1227.    As an extra kink, the function read-key-sequence resets/updates the
  1228.    last-command-event and this-command-keys.  It doesn't append to the
  1229.    command-keys as read-char does.  Such are the pitfalls of having to
  1230.    maintain compatibility with a program for which the only specification
  1231.    is the code itself.
  1232.  
  1233.    (We could implement recent_keys_ring and Vthis_command_keys as the same
  1234.    data structure.)
  1235.  */
  1236.  
  1237. #define RECENT_KEYS_SIZE 100
  1238. Lisp_Object recent_keys_ring;
  1239. int recent_keys_ring_index;
  1240.  
  1241. Lisp_Object Vthis_command_keys;
  1242. int this_command_keys_count;
  1243. int reset_this_command_keys;
  1244.  
  1245. /* reset_this_command_keys is a flag which means that the next time
  1246.    push_command_keys_vector() is called, it should flush the current
  1247.    value of Vthis_command_keys and start over.  The times at which the
  1248.    the command-keys are reset (instead of merely being augumented)
  1249.    are pretty conterintuitive.
  1250.  */
  1251.  
  1252. static Lisp_Object
  1253. reset_this_command_keys_fn () /* need a function for unwind-protect */
  1254. {
  1255.   reset_this_command_keys = 1;
  1256.   return Qnil;
  1257. }
  1258.  
  1259. static void
  1260. push_command_keys_vector (event)
  1261.      Lisp_Object event;
  1262. {
  1263.   int i;
  1264.   int old_size = XVECTOR (Vthis_command_keys)->size;
  1265.   int old_fp, new_fp;
  1266.   int meta_hack = 0;
  1267.  
  1268.   if (reset_this_command_keys)
  1269.     {
  1270.       reset_this_command_keys = 0;
  1271.       this_command_keys_count = 0;
  1272.     }
  1273.  
  1274.   old_fp = this_command_keys_count;
  1275.   new_fp = old_fp + 1;
  1276.  
  1277.   if (old_size <= new_fp) {
  1278.     Lisp_Object new = Fmake_vector (new_fp + 10, Qnil);
  1279.     for (i = 0; i < old_size; i++)
  1280.       XVECTOR (new)->contents [i] = XVECTOR (Vthis_command_keys)->contents [i];
  1281.     Vthis_command_keys = new;
  1282.   }
  1283.   for (i = 0; i < new_fp; i++) {
  1284.     Lisp_Object event;
  1285.     if (NILP (event = XVECTOR (Vthis_command_keys)->contents [i]))
  1286.       XVECTOR (Vthis_command_keys)->contents [i] = event = Fallocate_event ();
  1287.   }
  1288.   Fcopy_event (event, XVECTOR (Vthis_command_keys)->contents [new_fp-1]);
  1289.  
  1290.   if (meta_hack)
  1291.     XEVENT (XVECTOR (Vthis_command_keys)->contents [new_fp-1])
  1292.       ->event.key.modifiers |= MOD_META;
  1293.  
  1294.   this_command_keys_count = new_fp;
  1295. }
  1296.  
  1297.  
  1298. static void
  1299. push_recent_keys_vector (event)
  1300.      Lisp_Object event;
  1301. {
  1302.   if (NILP (XVECTOR (recent_keys_ring)->contents [recent_keys_ring_index]))
  1303.     XVECTOR (recent_keys_ring)->contents [recent_keys_ring_index] =
  1304.       Fallocate_event ();
  1305.   Fcopy_event (event,
  1306.            XVECTOR (recent_keys_ring)->contents [recent_keys_ring_index]);
  1307.   if (++recent_keys_ring_index == RECENT_KEYS_SIZE)
  1308.     recent_keys_ring_index = 0;
  1309. }
  1310.  
  1311.  
  1312. static void
  1313. store_recent_key (event)
  1314.      Lisp_Object event;
  1315. {
  1316.   push_command_keys_vector (event);
  1317.   push_recent_keys_vector (event);
  1318. }
  1319.  
  1320.  
  1321. static void command_builder_push_meta_hack ();
  1322.  
  1323. /* Add the given event to the command builder, enlarging the vector 
  1324.    first if necessary.
  1325.  
  1326.    Extra hack: this also updates the recent_keys_ring and Vthis_command_keys
  1327.    vectors to translate "ESC x" to "M-x" (for any "x" of course.)
  1328.  */
  1329. static void
  1330. command_builder_push (e)
  1331.      struct Lisp_Event *e;
  1332. {
  1333.   Lisp_Object event, event2;
  1334.   struct Lisp_Vector *vec = XVECTOR (command_builder->events);
  1335.   Lisp_Object prev = Qnil;
  1336.  
  1337.   if (command_builder->event_count > 0)
  1338.     prev = vec->contents [command_builder->event_count - 1];
  1339.       
  1340.   if (!NILP (prev) && meta_prefix_char != -1 &&
  1341.       event_to_character (XEVENT (prev), 0) == meta_prefix_char)
  1342.     {
  1343.       command_builder_push_meta_hack (e);
  1344.       return;
  1345.     }
  1346.  
  1347.   /* Update the command-builder vector
  1348.    */
  1349.   if (command_builder->event_count >= vec->size - 1) {
  1350.     int i;
  1351.     int old_size = command_builder->event_count;
  1352.     int new_size = old_size + 50;
  1353.     Lisp_Object old_vector = command_builder->events;
  1354.     Lisp_Object new_vector = Fmake_vector (new_size, Qnil);
  1355.     struct gcpro gcpro1;
  1356.     GCPRO1 (new_vector);
  1357.     for (i = 0; i < old_size; i++)
  1358.       XVECTOR (new_vector)->contents [i] = XVECTOR (old_vector)->contents [i];
  1359.     for (; i < new_size; i++)
  1360.       XVECTOR (new_vector)->contents [i] = Fallocate_event ();
  1361.     command_builder->events = new_vector;
  1362.     vec = XVECTOR (new_vector);
  1363.     UNGCPRO;
  1364.   }
  1365.   XSET (event, Lisp_Event, e);
  1366.   event2 = vec->contents [command_builder->event_count++];
  1367.   Fcopy_event (event, event2);
  1368.   XEVENT (event2)->next = (struct Lisp_Event *) -1;  /* safety hack */
  1369. }
  1370.  
  1371.  
  1372. /* When we see a sequence like "ESC x", pretend we really saw "M-x".
  1373.    DoubleThink the recent-keys and this-command-keys as well.
  1374.  */
  1375. static void
  1376. command_builder_push_meta_hack (e)
  1377.      struct Lisp_Event *e;
  1378. {
  1379.   Lisp_Object event;
  1380.   struct Lisp_Vector *vec = XVECTOR (command_builder->events);
  1381.   Lisp_Object target_event = vec->contents [command_builder->event_count - 1];
  1382.  
  1383.   /* Modify the previous most-recently-pushed event on the command
  1384.      builder to be a copy of this one with the meta-bit set instead of
  1385.      pushing a new event.
  1386.    */
  1387.   XSET (event, Lisp_Event, e);
  1388.   Fcopy_event (event, target_event);
  1389.   if (XEVENT (target_event)->event_type == key_press_event)
  1390.     XEVENT (target_event)->event.key.modifiers |= MOD_META;
  1391.   else if (XEVENT (target_event)->event_type == button_press_event ||
  1392.        XEVENT (target_event)->event_type == button_release_event)
  1393.     XEVENT (target_event)->event.button.modifiers |= MOD_META;
  1394.   else abort ();
  1395.  
  1396.   /* regenerate the echo-glyphs */
  1397.   {
  1398.     int i;
  1399.     command_builder->echoptr = command_builder->echobuf;
  1400.     for (i = 0; i < command_builder->event_count; i++)
  1401.       echo_char_event (XEVENT (XVECTOR (command_builder->events)
  1402.                    ->contents [i]));
  1403.   }
  1404. }
  1405.  
  1406.  
  1407. /* Self-insert-command is magic in that it doesn't always push an undo-
  1408.    boundary: up to 20 consecutive self-inserts can happen before an undo-
  1409.    boundary is pushed.  This variable is that counter.
  1410.  */
  1411. static int self_insert_countdown;
  1412.  
  1413. static void dispatch_command_event_internal ();
  1414.  
  1415. extern Lisp_Object Vcurrent_mouse_event;
  1416. extern int zmacs_region_active_p, zmacs_region_stays;
  1417. extern void zmacs_update_region (void);
  1418.  
  1419. extern int kbd_macro_end, pre_command_kbd_macro_end;
  1420.  
  1421. static void pre_command_hook ();
  1422. static void post_command_hook ();
  1423.  
  1424. /* Add the given event to the command builder, and if that results in 
  1425.    a complete key sequence, either execute the corresponding command, or
  1426.    return the key sequence.
  1427.  */
  1428. static Lisp_Object
  1429. compose_command (event, execute_p)
  1430.      struct Lisp_Event *event;
  1431.      int execute_p;
  1432. {
  1433.   Lisp_Object leaf;
  1434.   int count;
  1435.  
  1436.   /* for button-release events, kludges abound */
  1437.   int button_up_p = (event->event_type == button_release_event);
  1438.  
  1439.   command_builder_push (event);
  1440.   command_builder_find_leaf (! execute_p);
  1441.  
  1442.   leaf = command_builder->leaf;
  1443.   count = command_builder->event_count;
  1444.  
  1445.   {
  1446.     Lisp_Object leaf2 = leaf;
  1447.  
  1448.     if (! button_up_p)
  1449.       echo_area_glyphs = 0;
  1450.  
  1451.     while (SYMBOLP (leaf2) && !NILP (Ffboundp (leaf2)))
  1452.       leaf2 = Fsymbol_function (leaf2);
  1453.     /* If the leaf is a keymap, we're not done yet.  Return nil.
  1454.        We do all this junk with leaf2 instead of leaf so that
  1455.        command-execute is called on the actual contents of the
  1456.        keymap instead of its function cell.
  1457.      */
  1458.     if (KEYMAPP (leaf2)) {
  1459.       maybe_echo_keys ();
  1460.       return Qnil;
  1461.     }
  1462.   }
  1463.  
  1464.   /* The suppress-keymap function binds keys to 'undefined - special-case
  1465.      that here, so that being bound to that has the same error-behavior as
  1466.      not being defined at all.
  1467.    */
  1468.   if (EQ (leaf, Qundefined)) leaf = Qnil;
  1469.  
  1470.   /* Now we've got a complete key sequence.  Execute it or return a vector
  1471.      of the events, depending on the value of execute_p.
  1472.    */
  1473.   if (execute_p)
  1474.     {
  1475.       if (NILP (leaf))
  1476.     {
  1477.      /* At this point, we know that the sequence is not bound to a
  1478.         command.  Normally, we beep and print a message informing the
  1479.         user of this.  But we do not beep or print a message when:
  1480.  
  1481.         o  the last event in this sequence is a mouse-up event; or
  1482.         o  the last event in this sequence is a mouse-down event and
  1483.            there is a binding for the mouse-up version.
  1484.  
  1485.         That is, if the sequence ``C-x button1'' is typed, and is not
  1486.         bound to a command, but the sequence ``C-x button1up'' is bound
  1487.         to a command, we do not complain about the ``C-x button1''
  1488.         sequence.  If neither ``C-x button1'' nor ``C-x button1up'' is
  1489.         bound to a command, then we complain about the ``C-x button1''
  1490.         sequence, but later will *not* complain about the ``C-x button1up''
  1491.         sequence, which would be redundant.
  1492.  
  1493.         This is pretty hairy, but I think it's the most intuitive behavior.
  1494.       */
  1495.       struct Lisp_Event *terminal_event =
  1496.         /* `terminal-event' is equal but not eq to `event' */
  1497.         XEVENT (XVECTOR (command_builder->events)->contents [count-1]);
  1498.  
  1499.       if (terminal_event->event_type == button_press_event)
  1500.         {
  1501.           int suppress_bitching;
  1502.           /* Temporarily pretend the last event was an "up" instead of a
  1503.          "down", and look up its binding. */
  1504.           terminal_event->event_type = button_release_event;
  1505.           command_builder_find_leaf (0);
  1506.           /* If the "up" version is bound, don't complain. */
  1507.           suppress_bitching = !NILP (command_builder->leaf);
  1508.           /* Undo the temporary changes we just made. */
  1509.           terminal_event->event_type = button_press_event;
  1510.           command_builder->leaf = leaf;
  1511.           if (suppress_bitching)
  1512.         {
  1513.           /* Pretend this press was not seen (treat it as a prefix) */
  1514.           command_builder->event_count--;
  1515.           maybe_echo_keys ();
  1516.           return Qnil;
  1517.         }
  1518.         }
  1519.       
  1520.       /* Complain that the typed sequence is not defined, if this is the
  1521.          kind of sequence that warrants a complaint.
  1522.        */
  1523.       command_builder->echo_keys = 1;
  1524.       if (! button_up_p)
  1525.         {
  1526.           echo_area_glyphs = 0;
  1527.           strcpy (command_builder->echoptr,
  1528.               "not defined."); /* doo dah, doo dah */
  1529.           maybe_echo_keys ();
  1530.           command_builder->echo_keys = 0;
  1531.           /* Run the pre-command-hook before barfing about an
  1532.          undefined key. */
  1533.           Vthis_command = Qnil;
  1534.           pre_command_hook ();
  1535.           /* Beep (but don't signal).  The post-command-hook doesn't run,
  1536.          just as it wouldn't run if we had executed a real command
  1537.          which signalled an error.  But maybe it should in this case.
  1538.            */
  1539.           bitch_at_user (intern ("undefined-key"));
  1540.  
  1541.           cancel_echoing ();
  1542.         }
  1543.       /* Reset the command builder to read the next sequence.
  1544.        */
  1545.       command_builder->event_count = 0;
  1546.       command_builder->leaf = Qnil;
  1547.       defining_kbd_macro = 0;
  1548.       Vprefix_arg = Qnil;
  1549.       return Qnil;
  1550.     }
  1551.       dispatch_command_event_internal (event, leaf);
  1552.       return Qnil;
  1553.     }
  1554.   else
  1555.     {
  1556.       /* Copy the vector and the events in it.
  1557.        */
  1558.       Lisp_Object vector = Fmake_vector (make_number (count), Qnil);
  1559.       Lisp_Object *vec = XVECTOR (vector)->contents;
  1560.       Lisp_Object *events = XVECTOR (command_builder->events)->contents;
  1561.       int i;
  1562.       for (i = 0; i < count; i++)
  1563.     vec [i] = Fcopy_event (events [i], Qnil);
  1564.       command_builder->event_count = 0;
  1565.       command_builder->leaf = Qnil;
  1566.       return vector;
  1567.     }
  1568. }
  1569.  
  1570.  
  1571. static void store_last_command_event ();
  1572. static void
  1573. dispatch_command_event_internal (event, leaf)
  1574.      struct Lisp_Event *event;
  1575.      Lisp_Object leaf;
  1576. {
  1577.   Vthis_command = leaf;
  1578.  
  1579.   /* Don't push an undo boundary if the command set the prefix arg, or if we
  1580.      are executing a keyboard macro, or if in the minibuffer.  If the command
  1581.      we are about to execute is self-insert, it's tricky: up to 20 consecutive
  1582.      self-inserts may be done without an undo boundary.  This counter is reset
  1583.      as soon as a command other than self-insert-command is executed.
  1584.    */
  1585.   if (! EQ (leaf, Qself_insert_command))
  1586.     self_insert_countdown = 0;
  1587.   if (NILP (Vprefix_arg) &&
  1588.       NILP (Vexecuting_macro) &&
  1589.       !EQ (minibuf_window, selected_window) &&
  1590.       self_insert_countdown == 0)
  1591.     Fundo_boundary ();
  1592.  
  1593.   if (EQ (leaf, Qself_insert_command))
  1594.     if (--self_insert_countdown < 0)
  1595.       self_insert_countdown = 20;
  1596.  
  1597.   /* If we had been echoing keys, echo the last one (without the trailing
  1598.      dash) and redisplay before executing the command.
  1599.    */
  1600.   if (command_builder->echo_keys && event->event_type != eval_event)
  1601.     {
  1602.       command_builder->echoptr[0] = 0;
  1603.       maybe_echo_keys ();
  1604.       Fsit_for (0, Qt);
  1605.     }
  1606.  
  1607.   if (event->event_type == key_press_event)
  1608.     Vcurrent_mouse_event = Qnil;
  1609.   else XSET (Vcurrent_mouse_event, Lisp_Event, event);
  1610.  
  1611.   if (event->event_type != eval_event)
  1612.     store_last_command_event (event);
  1613.  
  1614.   {
  1615.     char *old_eag = echo_area_glyphs; /* before command executed */
  1616.     int old_rap = zmacs_region_active_p;
  1617.     zmacs_region_stays = 0;
  1618.  
  1619.     if (defining_kbd_macro)
  1620.       pre_command_kbd_macro_end = kbd_macro_end;
  1621.  
  1622.     /* Now we actually execute the command.
  1623.        If the command completes abnormally (signals an error, or does
  1624.        a throw past us) then we want reset_this_command_keys to get set
  1625.        to 1.  Otherwise, we want it to be 0.  We do this via the kind
  1626.        of hairy unwind-protect here...
  1627.      */
  1628.     {
  1629.       int count = specpdl_ptr - specpdl;
  1630.       record_unwind_protect (reset_this_command_keys_fn, 0);
  1631.       
  1632.       if (event->event_type != eval_event)
  1633.     pre_command_hook ();
  1634.  
  1635.       if (event->event_type == menu_event ||
  1636.       event->event_type == eval_event)
  1637.     call1 (event->event.eval.function, event->event.eval.object);
  1638.       else
  1639.     Fcommand_execute (Vthis_command, Qnil);
  1640.       
  1641.       /* the following two lines set it to 1 and then set it back;
  1642.      but remember that the unbinding will happen from elsewhere
  1643.      if an error was actually signalled, so the setting back
  1644.      will only happen if it completed normally.
  1645.        */
  1646.       unbind_to (count, Qnil);
  1647.       reset_this_command_keys = 0;
  1648.     }
  1649.  
  1650.     if (event->event_type == eval_event)
  1651.       return;
  1652.  
  1653.     /* If we're recording a keyboard macro, and the last command
  1654.        executed set a prefix argument, then decrement the pointer to
  1655.        the "last character really in the macro" to be just before this
  1656.        command.  This is so that the ^U in "^U ^X )" doesn't go onto
  1657.        the end of macro.
  1658.      */
  1659.     if (defining_kbd_macro && !NILP (Vprefix_arg))
  1660.       kbd_macro_end = pre_command_kbd_macro_end;
  1661.  
  1662.     post_command_hook (old_rap);
  1663.  
  1664.     /* Commands that set the prefix arg don't update last-command, don't
  1665.        reset the echoing state, and don't go into keyboard macros unless
  1666.        followed by another command.
  1667.      */
  1668.     if (NILP (Vprefix_arg))
  1669.       {
  1670.     char *new_eag = echo_area_glyphs;  /* after command executed */
  1671.     Vlast_command = Vthis_command;
  1672.     cancel_echoing ();  /* clear the char-echoing... */
  1673.     reset_this_command_keys = 1;  /* restart this-command-keys */
  1674.     /* ...but if the command printed a message, don't lose it. */
  1675.     if (old_eag != new_eag) echo_area_glyphs = new_eag;
  1676.       }
  1677.     else
  1678.       {
  1679.     maybe_echo_keys ();
  1680.       }
  1681.   }
  1682. }
  1683.  
  1684.  
  1685. static void
  1686. dispatch_menu_event (e)
  1687.      struct Lisp_Event *e;
  1688. {     
  1689.   dispatch_command_event_internal (e, Qnil);
  1690. }
  1691.  
  1692.  
  1693. static void
  1694. store_last_command_event (event)
  1695.      struct Lisp_event *event;
  1696. {
  1697.   Lisp_Object e;
  1698.  
  1699.   command_builder->event_count = 0;
  1700.   command_builder->leaf = Qnil;
  1701.  
  1702.   /* Store the last-command-event.  The semantics of this is that it is
  1703.      the last event most recently involved in command-lookup.
  1704.      */
  1705.   if (!EVENTP (Vlast_command_event))
  1706.     Vlast_command_event = Fallocate_event ();
  1707.   if (XEVENT (Vlast_command_event)->event_type == dead_event)
  1708.     {
  1709.       Vlast_command_event = Fallocate_event ();
  1710.       error ("Someone deallocated the last-command-event!");
  1711.     }
  1712.  
  1713.   XSET (e, Lisp_Event, event);
  1714.   if (! EQ (e, Vlast_command_event))
  1715.     Fcopy_event (e, Vlast_command_event);
  1716.   Vlast_command_char = Fevent_to_character (Vlast_command_event, Qnil);
  1717. }
  1718.  
  1719. static void
  1720. pre_command_hook ()
  1721. {
  1722.   if (!NILP (Vrun_hooks))
  1723.     call1 (Vrun_hooks, Qpre_command_hook);
  1724. }
  1725.  
  1726. static void
  1727. post_command_hook (old_rap)
  1728.      int old_rap;
  1729. {
  1730.   /* Turn off region hilighting unless this command requested that
  1731.      it be left on, or we're in the minibuffer.  We don't turn it off
  1732.      when we're in the minibuffer so that things like M-x write-region
  1733.      still work!
  1734.  
  1735.      This could be done via a function on the post-command-hook, but
  1736.      we don't want the user to accidentally remove it.
  1737.    */
  1738.   if (! zmacs_region_stays &&
  1739.       !EQ (minibuf_window, selected_window) &&
  1740.       (! zmacs_region_active_p ||
  1741.        ! (zmacs_region_active_p > old_rap)))
  1742.     Fzmacs_deactivate_region ();
  1743.   else
  1744.     zmacs_update_region ();
  1745.  
  1746.   if (!NILP (Vrun_hooks))
  1747.     call1 (Vrun_hooks, Qpost_command_hook);
  1748. }
  1749.  
  1750.  
  1751.  
  1752. static Lisp_Object dispatch_event_internal ();
  1753. static Lisp_Object compose_command ();
  1754.  
  1755. DEFUN ("dispatch-event", Fdispatch_event, Sdispatch_event, 1, 1, 0,
  1756.   "Given an event object returned by next-event, execute it.")
  1757.      (event)
  1758.      Lisp_Object event;
  1759. {
  1760.   CHECK_EVENT (event, 0);
  1761.   if (XEVENT (event)->event_type == dead_event)
  1762.     error ("dispatch-event called on a deallocated event!");
  1763.   dispatch_event_internal (event, 1);
  1764.   return Qnil;
  1765. }
  1766.  
  1767. DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 1, 0,
  1768.   "Read a sequence of keystrokes or mouse clicks and return a vector of the\n\
  1769. event objects read.  The vector is newly created, but the event objects are\n\
  1770. reused: if you want to hold a pointer to them beyond the next call to this\n\
  1771. function, you must copy them first.\n\
  1772. \n\
  1773. The sequence read is sufficient to specify a non-prefix command starting\n\
  1774. from the current local and global keymaps.  A C-g typed while in this\n\
  1775. function is treated like any other character, and `quit-flag' is not set.\n\
  1776. One arg, PROMPT, is a prompt string, or nil meaning do not prompt specially.\n\
  1777. \n\
  1778. If the user selects a menu item while we are prompting for a key-sequence,\n\
  1779. the returned value will be a vector of a single menu-selection event.\n\
  1780. An error will be signalled if you pass this value to `lookup-key' or a\n\
  1781. related function.")
  1782.      (prompt)
  1783.      Lisp_Object prompt;
  1784. {
  1785.   Lisp_Object result = Qnil;
  1786.   Lisp_Object event = Fallocate_event ();
  1787.   int count = specpdl_ptr - specpdl;
  1788.   struct gcpro gcpro1;
  1789.   GCPRO1 (event);
  1790.  
  1791.   QUIT;
  1792.   CHECK_STRING (prompt, 0);
  1793.   echo_prompt ((char *) XSTRING (prompt)->data);
  1794.  
  1795.   specbind (Qinhibit_quit, Qt);
  1796.   reset_this_command_keys = 1; /* this is stupid, but that's how v18 works */
  1797.   while (NILP (result))
  1798.     result = dispatch_event_internal (Fnext_event (event), 0);
  1799.   Vquit_flag = Qnil;  /* In case we read a ^G */
  1800.   Fdeallocate_event (event);
  1801.   unbind_to (count, Qnil);
  1802.   UNGCPRO;
  1803.   return result;
  1804. }
  1805.  
  1806.  
  1807. void
  1808. syms_of_event_stream ()
  1809. {
  1810.   command_builder = (struct command_builder *)
  1811.     xmalloc (sizeof (struct command_builder));
  1812.   command_builder->event_count = 0;
  1813.   command_builder->leaf = Qnil;
  1814.   command_builder->echobuf = (char *) xmalloc (300);
  1815.   command_builder->echoptr = command_builder->echobuf;
  1816.   command_builder->echoptr[0] = 0;
  1817.   command_builder->echo_keys = 0;
  1818.   command_builder->events = Fmake_vector (0, Qnil);
  1819.   staticpro (&command_builder->events);
  1820.  
  1821.   command_event_queue = (struct command_event_queue *)
  1822.     xmalloc (sizeof (struct command_event_queue));
  1823.   command_event_queue->head = 0;
  1824.   command_event_queue->tail = 0;
  1825.  
  1826.   recent_keys_ring_index = 0;
  1827.   recent_keys_ring = Fmake_vector (RECENT_KEYS_SIZE, Qnil);
  1828.   staticpro (&recent_keys_ring);
  1829.  
  1830.   this_command_keys_count = 0;
  1831.   Vthis_command_keys = Fmake_vector (40, Qnil);
  1832.   staticpro (&Vthis_command_keys);
  1833.  
  1834.   num_input_chars = 0;
  1835.   self_insert_countdown = 0;
  1836.  
  1837.   Qundefined = intern ("undefined");
  1838.   staticpro (&Qundefined);
  1839.  
  1840.   defsubr (&Sinput_pending_p);
  1841.   defsubr (&Senqueue_command_event);
  1842.   defsubr (&Snext_event);
  1843.   defsubr (&Snext_command_event);
  1844.   defsubr (&Sread_char);
  1845.   defsubr (&Sdiscard_input);
  1846.   defsubr (&Ssit_for);
  1847.   defsubr (&Ssleep_for);
  1848. #ifdef LISP_FLOAT_TYPE
  1849.   defsubr (&Ssleep_for_millisecs);
  1850. #endif
  1851.   defsubr (&Saccept_process_output);
  1852.   defsubr (&Sadd_timeout);
  1853.   defsubr (&Sdisable_timeout);
  1854.   defsubr (&Sdispatch_event);
  1855.   defsubr (&Sread_key_sequence);
  1856.  
  1857.   DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
  1858.  "*Nonzero means echo unfinished commands after this many seconds of pause.");
  1859.   echo_keystrokes = 1;
  1860.  
  1861.   DEFVAR_INT ("auto-save-interval", &auto_save_interval,
  1862.     "*Number of keyboard input characters between auto-saves.\n\
  1863. Zero means disable autosaving due to number of characters typed.\n\
  1864. See also the variable `auto-save-timeout'.");
  1865.   auto_save_interval = 300;
  1866. }
  1867.  
  1868.  
  1869. /*
  1870. useful testcases for v18/v19 compatibility:
  1871.  
  1872. (defun foo ()
  1873.  (interactive)
  1874.  (setq unread-command-event (character-to-event ?A (allocate-event)))
  1875.  (setq x (list (read-char)
  1876. ;      (read-key-sequence "") ; try it with and without this
  1877.       last-command-char last-input-char
  1878.       (recent-keys) (this-command-keys))))
  1879. (global-set-key "\^Q" 'foo)
  1880. ;use ^Q and ^U^U^Q and ^U^U^U^G^Q
  1881. ;the evi-mode command "4dlj.j.j.j.j.j." is also a good testcase (gag)
  1882.  
  1883. ;(setq x (list (read-char) quit-flag))^J^G
  1884. ;x should get set to (7 t), but no result should be printed.
  1885.  
  1886. ;also do this: make two screens, one viewing "*scratch*", the other "foo".
  1887. ;in *scratch*, type (sit-for 20)^J
  1888. ;wait a couple of seconds, move cursor to foo, type "a"
  1889. ;a should be inserted in foo.  Cursor highlighting should not change in
  1890. ;the meantime.
  1891.  
  1892. ;do it with sleep-for.  move cursor into foo, then back into *scratch*
  1893. ;before typing.
  1894.  
  1895. ;make sure ^G aborts both sit-for and sleep-for.
  1896.  
  1897. ;do it all in both v18 and v19 and make sure all results are the same.
  1898. ;all of these cases matter a lot, but some in quite subtle ways.
  1899. */
  1900.