home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / emacs / src / keyboard.c < prev    next >
C/C++ Source or Header  |  1992-05-17  |  59KB  |  2,289 lines

  1. /* Keyboard input; editor command loop.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990 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 1, 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. /*** For version 19, can simplify this by making interrupt_input 1 on VMS.  */
  21.  
  22. /* This must precede sys/signal.h on certain machines.  */
  23. #include <sys/types.h>
  24. /* Allow config.h to undefine symbols found here.  */
  25. #include <signal.h>
  26.  
  27. #include "config.h"
  28. #include <stdio.h>
  29. #undef NULL
  30. #include "termchar.h"
  31. #include "termopts.h"
  32. #include "termhooks.h"
  33. #include "lisp.h"
  34. #include "macros.h"
  35. #include "window.h"
  36. #include "commands.h"
  37. #include "buffer.h"
  38. #include <setjmp.h>
  39. #include <errno.h>
  40.  
  41. extern int errno;
  42.  
  43. /* Get FIONREAD, if it is available.  */
  44. #ifdef USG
  45. #include <termio.h>
  46. #include <fcntl.h>
  47. #else /* not USG */
  48. #ifndef VMS
  49. #include <sys/ioctl.h>
  50. #endif /* not VMS */
  51. #endif /* not USG */
  52.  
  53. #include "emacssignal.h"
  54.  
  55. /* Allow m- file to inhibit use of FIONREAD.  */
  56. #ifdef BROKEN_FIONREAD
  57. #undef FIONREAD
  58. #endif
  59.  
  60. /* Make all keyboard buffers much bigger when using X windows.  */
  61. #ifdef HAVE_X_WINDOWS
  62. #define BUFFER_SIZE_FACTOR 16
  63. #else
  64. #define BUFFER_SIZE_FACTOR 1
  65. #endif
  66.  
  67. /* Following definition copied from eval.c */
  68.  
  69. struct backtrace
  70.   {
  71.     struct backtrace *next;
  72.     Lisp_Object *function;
  73.     Lisp_Object *args;    /* Points to vector of args. */
  74.     int nargs;        /* length of vector */
  75.            /* if nargs is UNEVALLED, args points to slot holding list of unevalled args */
  76.     char evalargs;
  77.   };
  78.  
  79. /* Non-nil disable property on a command means
  80.  do not execute it; call disabled-command-hook's value instead. */
  81. Lisp_Object Qdisabled, Vdisabled_command_hook;
  82.  
  83. int recent_keys_index;    /* Index for storing next element into recent_keys */
  84. int total_keys;        /* Total number of elements stored into recent_keys */
  85. char recent_keys[100];    /* Holds last 100 keystrokes */
  86.  
  87. /* Buffer holding the key that invoked the current command.  */
  88. unsigned char *this_command_keys;
  89. int this_command_key_count;    /* Size in use.  */
  90. int this_command_keys_size;    /* Size allocated.  */
  91.  
  92. extern struct backtrace *backtrace_list;
  93.  
  94. static jmp_buf getcjmp;    /* for longjmp to where kbd input is being done. */
  95.  
  96. int waiting_for_input;    /* True while doing kbd input */
  97.  
  98. /* True while displaying for echoing.   Delays C-g throwing. */
  99. static int echoing;
  100.  
  101. int immediate_quit;    /* Nonzero means C-G should cause immediate error-signal. */
  102.  
  103. int help_char;        /* Character to recognize as the help char.  */
  104.  
  105. Lisp_Object Vhelp_form;    /* Form to execute when help char is typed.  */
  106.  
  107. /* Character that causes a quit.  Normally C-g.  */
  108.  
  109. int quit_char;
  110.  
  111. extern Lisp_Object global_map;
  112.  
  113. /* Current depth in recursive edits.  */
  114.  
  115. int command_loop_level;
  116.  
  117. /* Last input character read as a command.  */
  118.  
  119. int last_command_char;
  120.  
  121. /* Last input character read for any purpose.  */
  122.  
  123. int last_input_char;
  124.  
  125. /* If not -1, a character to be read as the next command input */
  126.  
  127. int unread_command_char;
  128.  
  129. /* Char to use as prefix when a meta character is typed in.
  130.  This is bound on entry to minibuffer in case Esc is changed there.  */
  131.  
  132. int meta_prefix_char;
  133.  
  134. /* Total number of times read_command_char has returned.  */
  135.  
  136. int num_input_chars;
  137.  
  138. /* Auto-save automatically when this many characters have been typed
  139.    since the last time.  */
  140.  
  141. static int auto_save_interval;
  142.  
  143. /* Value of num_input_chars as of last auto save.  */
  144.  
  145. int last_auto_save;
  146.  
  147. /* Last command executed by the editor command loop, not counting
  148.    commands that set the prefix argument.  */
  149.  
  150. Lisp_Object last_command;
  151.  
  152. /* The command being executed by the command loop.
  153.    Commands may set this, and the value set will be copied into last_command
  154.    instead of the actual command.  */
  155. Lisp_Object this_command;
  156.  
  157. Lisp_Object Qself_insert_command;
  158. Lisp_Object Qforward_char;
  159. Lisp_Object Qbackward_char;
  160.  
  161. /* read_key_sequence stores here the command definition of the
  162.    key sequence that it reads.  */
  163. Lisp_Object read_key_sequence_cmd;
  164.  
  165. /* Form to evaluate (if non-nil) when Emacs is started */
  166. Lisp_Object Vtop_level;
  167.  
  168. /* User-supplied string to translate input characters through */
  169. Lisp_Object Vkeyboard_translate_table;
  170.  
  171. FILE *dribble;            /* File in which we write all commands we read */
  172.  
  173. /* Nonzero if input is available */
  174. int input_pending;
  175.  
  176. /* Nonzero if should obey 0200 bit in input chars as "Meta" */
  177. int meta_key;
  178.  
  179. extern char *pending_malloc_warning;
  180.  
  181. /* Buffer for pre-read keyboard input */
  182. unsigned char kbd_buffer [256 * BUFFER_SIZE_FACTOR];
  183.  
  184. /* Number of characters available in kbd_buffer.  */
  185. int kbd_count;
  186.  
  187. /* Pointer to next available character in kbd_buffer.  */
  188. unsigned char *kbd_ptr;
  189.  
  190. /* Address (if not 0) of word to zero out
  191.    if a SIGIO interrupt happens */
  192. long *input_available_clear_word;
  193.  
  194. /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
  195.    Default is 1 if INTERRUPT_INPUT is defined.  */
  196.  
  197. int interrupt_input;
  198.  
  199. /* Nonzero while interrupts are temporarily deferred during redisplay.  */
  200.  
  201. int interrupts_deferred;
  202.  
  203. /* nonzero means use ^S/^Q for flow control.  */
  204.  
  205. int flow_control;
  206.  
  207. #ifndef BSD4_1
  208. #define sigfree() sigsetmask (SIGEMPTYMASK)
  209. #define sigholdx(sig) sigsetmask (sigmask (sig))
  210. #define sigblockx(sig) sigblock (sigmask (sig))
  211. #define sigunblockx(sig) sigblock (SIGEMPTYMASK)
  212. #define sigpausex(sig) sigpause (0)
  213. #endif /* not BSD4_1 */
  214.  
  215. #ifdef BSD4_1
  216. #define SIGIO SIGTINT
  217. /* sigfree and sigholdx are in sysdep.c */
  218. #define sigblockx(sig) sighold (sig)
  219. #define sigunblockx(sig) sigrelse (sig)
  220. #define sigpausex(sig) sigpause (sig)
  221. #endif /* BSD4_1 */
  222.  
  223. /* We are unable to use interrupts if FIONREAD is not available,
  224.    so flush SIGIO so we won't try. */
  225. #ifndef FIONREAD
  226. #ifdef SIGIO
  227. #undef SIGIO
  228. #endif
  229. #endif
  230.  
  231. /* If we support X Windows, and won't get an interrupt when input
  232.    arrives from the server, poll periodically so we can detect C-g.  */
  233. #ifdef HAVE_X_WINDOWS
  234. #ifndef SIGIO
  235. #define POLL_FOR_INPUT
  236. #endif
  237. #endif
  238.  
  239. /* Function for init_keyboard to call with no args (if nonzero).  */
  240. void (*keyboard_init_hook) ();
  241.  
  242. static void read_avail_input ();
  243. static void get_input_pending ();
  244.  
  245. /* Non-zero tells input_available_signal to call read_socket_hook
  246.    even if FIONREAD returns zero.  */
  247. static int force_input;
  248.  
  249. static int echo_keystrokes;    /* > 0 if we are to echo keystrokes */
  250.  
  251. /* Nonzero means echo each character as typed.  */
  252. static int immediate_echo;
  253.  
  254. #define    min(a,b)    ((a)<(b)?(a):(b))
  255. #define    max(a,b)    ((a)>(b)?(a):(b))
  256.  
  257. static char echobuf[100];
  258. static char *echoptr;
  259.  
  260. /* Install the string STR as the beginning of the string of echoing,
  261.    so that it serves as a prompt for the next character.
  262.    Also start echoing.  */
  263.  
  264. echo_prompt (str)
  265.      char *str;
  266. {
  267.   int len = strlen (str);
  268.   if (len > sizeof echobuf - 4)
  269.     len = sizeof echobuf - 4;
  270.   bcopy (str, echobuf, len + 1);
  271.   echoptr = echobuf + len;
  272.  
  273.   echo ();
  274. }
  275.  
  276. /* Add the character C to the echo string,
  277.    if echoing is going on.  */
  278.  
  279. echo_char (c)
  280.      int c;
  281. {
  282.   extern char *push_key_description ();
  283.  
  284.   if (immediate_echo)
  285.     {
  286.       char *ptr = echoptr;
  287.  
  288.       if (ptr - echobuf > sizeof echobuf - 6)
  289.     return;
  290.  
  291.       if (echoptr != echobuf)
  292.     *ptr++ = ' ';
  293.  
  294.       ptr = push_key_description (c, ptr);
  295.       if (echoptr == echobuf && c == help_char)
  296.     {
  297.       strcpy (ptr, " (Type ? for further options)");
  298.       ptr += strlen (ptr);
  299.     }
  300.  
  301.       *ptr = 0;
  302.       echoptr = ptr;
  303.  
  304.       echo ();
  305.     }
  306. }
  307.  
  308. /* Temporarily add a dash to the end of the echo string,
  309.    so that it serves as a mini-prompt for the very next character.  */
  310.  
  311. echo_dash ()
  312. {
  313.   if (!immediate_echo && echoptr == echobuf)
  314.     return;
  315.  
  316.   /* Put a dash at the end of the buffer temporarily,
  317.      but make it go away when the next character is added.  */
  318.   echoptr[0] = '-';
  319.   echoptr[1] = 0;
  320.  
  321.   echo ();
  322. }
  323.  
  324. /* Display the current echo string, and begin echoing if not already
  325.    doing so.  */
  326.  
  327. echo ()
  328. {
  329.   if (!immediate_echo)
  330.     {
  331.       int i;
  332.       immediate_echo = 1;
  333.  
  334.       for (i = 0; i < this_command_key_count; i++)
  335.     echo_char (this_command_keys[i]);
  336.       echo_dash ();
  337.     }
  338.  
  339.   echoing = 1;
  340.   message1 (echobuf);
  341.   echoing = 0;
  342.  
  343.   if (waiting_for_input && !NULL (Vquit_flag))
  344.     quit_throw_to_read_command_char ();
  345. }
  346.  
  347. /* Turn off echoing, for the start of a new command.  */
  348.  
  349. cancel_echoing ()
  350. {
  351.   immediate_echo = 0;
  352.   echoptr = echobuf;
  353. }
  354.  
  355. /* When an auto-save happens, record the "time", and don't do again soon.  */
  356. record_auto_save ()
  357. {
  358.   last_auto_save = num_input_chars;
  359. }
  360.  
  361. Lisp_Object recursive_edit_unwind (), command_loop ();
  362.  
  363. DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
  364.   "Invoke the editor command loop recursively.\n\
  365. Do (throw 'exit nil) within the command loop to make this function return,\n\
  366. or (throw 'exit t) to make this function signal an error.\n\
  367. This function is called by the editor initialization\n\
  368. to begin editing.")
  369.   ()
  370. {
  371.   int count = specpdl_ptr - specpdl;
  372.  
  373.   command_loop_level++;
  374.   update_mode_lines = 1;
  375.  
  376.   record_unwind_protect (recursive_edit_unwind,
  377.              (current_buffer != XBUFFER (XWINDOW (selected_window)->buffer)
  378.               ? Fcurrent_buffer ()
  379.               : Qnil));
  380.  
  381.   recursive_edit_1 ();
  382.  
  383.   unbind_to (count);
  384.   return Qnil;
  385. }
  386.  
  387. Lisp_Object
  388. recursive_edit_1 ()
  389. {
  390.   int count = specpdl_ptr - specpdl;
  391.   Lisp_Object val;
  392.  
  393.   if (command_loop_level > 0)
  394.     {
  395.       specbind (Qstandard_output, Qt);
  396.       specbind (Qstandard_input, Qt);
  397.     }
  398.  
  399.   val = command_loop ();
  400.   if (EQ (val, Qt))
  401.     Fsignal (Qquit, Qnil);
  402.  
  403.   unbind_to (count);
  404.   return Qnil;
  405. }
  406.  
  407. Lisp_Object
  408. recursive_edit_unwind (buffer)
  409.      Lisp_Object buffer;
  410. {
  411.   if (!NULL (buffer))
  412.     Fset_buffer (buffer);
  413.   command_loop_level--;
  414.   update_mode_lines = 1;
  415.   return Qnil;
  416. }
  417.  
  418. Lisp_Object
  419. cmd_error (data)
  420.      Lisp_Object data;
  421. {
  422.   Lisp_Object errmsg, tail, errname, file_error;
  423.   struct gcpro gcpro1;
  424.   int i;
  425.  
  426.   Vquit_flag = Qnil;
  427.   Vinhibit_quit = Qt;
  428.   Vstandard_output = Qt;
  429.   Vstandard_input = Qt;
  430.   Vexecuting_macro = Qnil;
  431.   echo_area_contents = 0;
  432.  
  433.   Fdiscard_input ();
  434.   bell ();
  435.  
  436.   errname = Fcar (data);
  437.  
  438.   if (EQ (errname, Qerror))
  439.     {
  440.       data = Fcdr (data);
  441.       if (!CONSP (data)) data = Qnil;
  442.       errmsg = Fcar (data);
  443.       file_error = Qnil;
  444.     }
  445.   else
  446.     {
  447.       errmsg = Fget (errname, Qerror_message);
  448.       file_error = Fmemq (Qfile_error,
  449.               Fget (errname, Qerror_conditions));
  450.     }
  451.  
  452.   /* Print an error message including the data items.
  453.      This is done by printing it into a scratch buffer
  454.      and then making a copy of the text in the buffer. */
  455.   
  456.   if (!CONSP (data)) data = Qnil;
  457.   tail = Fcdr (data);
  458.   GCPRO1 (tail);
  459.  
  460.   /* For file-error, make error message by concatenating
  461.      all the data items.  They are all strings.  */
  462.   if (!NULL (file_error))
  463.     errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
  464.  
  465.   if (XTYPE (errmsg) == Lisp_String)
  466.     Fprinc (errmsg, Qt);
  467.   else
  468.     write_string_1 ("peculiar error", -1, Qt);
  469.  
  470.   for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
  471.     {
  472.       write_string_1 (i ? ", " : ": ", 2, Qt);
  473.       if (!NULL (file_error))
  474.     Fprinc (Fcar (tail), Qt);
  475.       else
  476.     Fprin1 (Fcar (tail), Qt);
  477.     }
  478.   UNGCPRO;
  479.  
  480.   /* In -batch mode, force out the error message and newlines after it
  481.      and then die.  */
  482.   if (noninteractive)
  483.     {
  484.       message ("");
  485.       Fkill_emacs (make_number (-1));
  486.     }
  487.  
  488.   Vquit_flag = Qnil;
  489.  
  490.   Vinhibit_quit = Qnil;
  491.   return make_number (0);
  492. }
  493.  
  494. Lisp_Object command_loop_1 ();
  495. Lisp_Object command_loop_2 ();
  496. Lisp_Object cmd_error ();
  497. Lisp_Object top_level_1 ();
  498.  
  499. /* Entry to editor-command-loop.
  500.    This level has the catches for exiting/returning to editor command loop.
  501.    It returns nil to exit recursive edit, t to abort it.  */
  502.  
  503. Lisp_Object
  504. command_loop ()
  505. {
  506.   if (command_loop_level > 0 || minibuf_level > 0)
  507.     {
  508.       return internal_catch (Qexit, command_loop_2, Qnil);
  509.     }
  510.   else
  511.     while (1)
  512.       {
  513.     internal_catch (Qtop_level, top_level_1, Qnil);
  514.     internal_catch (Qtop_level, command_loop_2, Qnil);
  515.     /* End of file in -batch run causes exit here.  */
  516.     if (noninteractive)
  517.       Fkill_emacs (Qt);
  518.       }
  519. }
  520.  
  521. /* Here we catch errors in execution of commands within the
  522.    editing loop, and reenter the editing loop.
  523.    When there is an error, cmd_error runs and returns a non-nil
  524.    value to us.  A value of nil means that cmd_loop_1 itself
  525.    returned due to end of file (or end of kbd macro).  */
  526.  
  527. Lisp_Object
  528. command_loop_2 ()
  529. {
  530.   register Lisp_Object val;
  531.   do
  532.     val = internal_condition_case (command_loop_1, Qerror, cmd_error);
  533.   while (!NULL (val));
  534.   return Qnil;
  535. }
  536.  
  537. Lisp_Object
  538. top_level_2 ()
  539. {
  540.   return Feval (Vtop_level);
  541. }
  542.  
  543. Lisp_Object
  544. top_level_1 ()
  545. {
  546.   /* On entry to the outer level, run the startup file */
  547.   if (!NULL (Vtop_level))
  548.     internal_condition_case (top_level_2, Qerror, cmd_error);
  549.   else if (!NULL (Vpurify_flag))
  550.     message ("Bare impure Emacs (standard Lisp code not loaded)");
  551.   else
  552.     message ("Bare Emacs (standard Lisp code not loaded)");
  553.   return Qnil;
  554. }
  555.  
  556. DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
  557.   "Exit all recursive editing levels.")
  558.   ()
  559. {
  560.   Fthrow (Qtop_level, Qnil);
  561. }
  562.  
  563. DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
  564.   "Exit from the innermost recursive edit or minibuffer.")
  565.   ()
  566. {
  567.   if (command_loop_level > 0 || minibuf_level > 0)
  568.     Fthrow (Qexit, Qnil);
  569.   error ("No recursive edit is in progress");
  570. }
  571.  
  572. DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
  573.   "Abort the command that requested this recursive edit or minibuffer input.")
  574.   ()
  575. {
  576.   if (command_loop_level > 0 || minibuf_level > 0)
  577.     Fthrow (Qexit, Qt);
  578.   error ("No recursive edit is in progress");
  579. }
  580.  
  581. /* This is the actual command reading loop,
  582.  sans error-handling encapsulation */
  583.  
  584. Lisp_Object Fcommand_execute ();
  585.  
  586. Lisp_Object
  587. command_loop_1 ()
  588. {
  589.   Lisp_Object cmd;
  590.   int lose;
  591.   int nonundocount;
  592.   char keybuf[30];
  593.   int i;
  594.   int no_redisplay;
  595.   int no_direct;
  596.  
  597.   Vprefix_arg = Qnil;
  598.   waiting_for_input = 0;
  599.   cancel_echoing ();
  600.  
  601.   /* Don't clear out last_command at the beginning of a macro.  */
  602.   if (NULL (Vexecuting_macro)
  603.       || XTYPE (Vexecuting_macro) != Lisp_String)
  604.     last_command = Qt;
  605.   nonundocount = 0;
  606.   no_redisplay = 0;
  607.   this_command_key_count = 0;
  608.   
  609.   while (1)
  610.     {
  611.       /* Install chars successfully executed in kbd macro */
  612.       if (defining_kbd_macro && NULL (Vprefix_arg))
  613.     finalize_kbd_macro_chars ();
  614.  
  615.       /* Make sure current window's buffer is selected.  */
  616.  
  617.       if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
  618.     set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
  619.  
  620.       /* Display any malloc warning that just came out.
  621.      Use while because displaying one warning can cause another.  */
  622.       while (pending_malloc_warning)
  623.     display_malloc_warning ();
  624.  
  625.       no_direct = 0;
  626.  
  627.       /* If minibuffer on and echo area in use,
  628.      wait 2 sec and redraw minibufer.  */
  629.  
  630.       if (minibuf_level && echo_area_contents)
  631.     {
  632.       int count = specpdl_ptr - specpdl;
  633.       specbind (Qinhibit_quit, Qt);
  634.       Fsit_for (make_number (2), Qnil);
  635.       unbind_to (count);
  636.  
  637.       echo_area_contents = 0;
  638.       no_direct = 1;
  639.       if (!NULL (Vquit_flag))
  640.         {
  641.           Vquit_flag = Qnil;
  642.           unread_command_char = quit_char;
  643.         }
  644.     }
  645.  
  646.       i = 0;
  647. #if 0
  648.       /* If prev. command was directly displayed, we don't need
  649.      redisplay.  Try shortcut for reading single-char key sequence.  */
  650.       if (no_redisplay)
  651.     i = fast_read_one_key (keybuf);
  652. #endif /* 0 */
  653.       /* Shortcut not applicable or found a prefix key.
  654.      Take full precautions and read key sequence the hard way.  */
  655.       if (i == 0)
  656.     {
  657. #ifdef C_ALLOCA
  658.       alloca (0);        /* Cause a garbage collection now */
  659.                 /* Since we can free the most stuff here.  */
  660. #endif /* C_ALLOCA */
  661.  
  662.       /* Read next key sequence; i gets its length.  */
  663.  
  664.       i = read_key_sequence (keybuf, sizeof keybuf, 0,
  665.                  no_redisplay && buffer_shared <= 1);
  666.     }
  667.  
  668.       /* Now we have read a key sequence of length I,
  669.      or else I is 0 and we found end of file.  */
  670.  
  671.       if (i == 0)        /* End of file -- happens only in */
  672.     return Qnil;        /* a kbd macro, at the end */
  673.  
  674.       last_command_char = keybuf[i - 1];
  675.       
  676.       cmd = read_key_sequence_cmd;
  677.       if (!NULL (Vexecuting_macro))
  678.     {
  679.       if (!NULL (Vquit_flag))
  680.         {
  681.           Vexecuting_macro = Qt;
  682.           QUIT;        /* Make some noise. */
  683.                 /* Will return since macro now empty. */
  684.         }
  685.     }
  686.  
  687.       /* Do redisplay processing after this command except in special
  688.      cases identified below that set no_redisplay to 1.  */
  689.       no_redisplay = 0;
  690.  
  691.       /* Execute the command.  */
  692.  
  693.       if (NULL (cmd))
  694.     {
  695.       /* nil means key is undefined.  */
  696.       bell ();
  697.       defining_kbd_macro = 0;
  698.       update_mode_lines++;
  699.       Vprefix_arg = Qnil;
  700.     }
  701.       else
  702.     {
  703.       this_command = cmd;
  704.       if (NULL (Vprefix_arg) && ! no_direct)
  705.         {
  706.           if (EQ (cmd, Qforward_char) && point < ZV)
  707.         {
  708.           lose = FETCH_CHAR (point);
  709.           SET_PT (point + 1);
  710.           if (lose >= ' ' && lose < 0177
  711.               && (XFASTINT (XWINDOW (selected_window)->last_modified)
  712.               >= MODIFF)
  713.               && (XFASTINT (XWINDOW (selected_window)->last_point)
  714.               == point)
  715.               && !windows_or_buffers_changed
  716.               && EQ (current_buffer->selective_display, Qnil)
  717.               && !detect_input_pending ()
  718.               && NULL (Vexecuting_macro))
  719.             no_redisplay = direct_output_forward_char (1);
  720.           goto directly_done;
  721.         }
  722.           else if (EQ (cmd, Qbackward_char) && point > BEGV)
  723.         {
  724.           SET_PT (point - 1);
  725.           lose = FETCH_CHAR (point);
  726.           if (lose >= ' ' && lose < 0177
  727.               && (XFASTINT (XWINDOW (selected_window)->last_modified)
  728.               >= MODIFF)
  729.               && (XFASTINT (XWINDOW (selected_window)->last_point)
  730.               == point)
  731.               && !windows_or_buffers_changed
  732.               && EQ (current_buffer->selective_display, Qnil)
  733.               && !detect_input_pending ()
  734.               && NULL (Vexecuting_macro))
  735.             no_redisplay = direct_output_forward_char (-1);
  736.           goto directly_done;
  737.         }
  738.           else if (EQ (cmd, Qself_insert_command))
  739.         {
  740.           if (NULL (Vexecuting_macro) &&
  741.               !EQ (minibuf_window, selected_window))
  742.             {
  743.               if (!nonundocount || nonundocount >= 20)
  744.             {
  745.               Fundo_boundary ();
  746.               nonundocount = 0;
  747.             }
  748.               nonundocount++;
  749.             }
  750.           lose = (XFASTINT (XWINDOW (selected_window)->last_modified)
  751.               < MODIFF)
  752.             || (XFASTINT (XWINDOW (selected_window)->last_point)
  753.               != point)
  754.             || MODIFF <= current_buffer->save_modified
  755.             || windows_or_buffers_changed
  756.             || !EQ (current_buffer->selective_display, Qnil)
  757.             || detect_input_pending ()
  758.             || !NULL (Vexecuting_macro);
  759.           if (self_insert_internal (last_command_char, 0))
  760.             {
  761.               lose = 1;
  762.               nonundocount = 0;
  763.             }
  764.           if (!lose
  765.               && (point == ZV || FETCH_CHAR (point) == '\n')
  766.               && last_command_char >= ' '
  767.               && last_command_char < 0177)
  768.             no_redisplay
  769.               = direct_output_for_insert (last_command_char);
  770.           goto directly_done;
  771.         }
  772.         }
  773.  
  774.       /* Here for a command that isn't executed directly */
  775.  
  776.       nonundocount = 0;
  777.       if (NULL (Vprefix_arg))
  778.         Fundo_boundary ();
  779.       Fcommand_execute (cmd, Qnil);
  780.     }
  781.       /* This label logically belongs inside the above group,
  782.      but moving it is said to avoid a compiler bug on SCO V.3.2v2.  */
  783.     directly_done: ;
  784.  
  785.       if (NULL (Vprefix_arg))
  786.     {
  787.       last_command = this_command;
  788.       this_command_key_count = 0;
  789.       cancel_echoing ();
  790.     }
  791.     }
  792. }
  793.  
  794. /* Input of single characters from keyboard */
  795.  
  796. Lisp_Object print_help ();
  797.  
  798. int echo_flag;
  799. int echo_now;
  800.  
  801. /* Alarm interrupt calls this and requests echoing at earliest safe time. */
  802. request_echo ()
  803. {
  804.   int old_errno = errno;
  805.  
  806.   /* Note: no need to reestablish handler on USG systems
  807.      because it is established, if approriate, each time an alarm is requested.  */
  808. #ifdef subprocesses
  809. #ifdef BSD4_1
  810.   extern int select_alarmed;
  811.   if (select_alarmed == 0)
  812.     {
  813.       select_alarmed = 1;
  814.       sigrelse (SIGALRM);
  815.       return;
  816.     }
  817. #endif
  818. #endif
  819.  
  820. #ifdef BSD4_1
  821.   sigisheld (SIGALRM);
  822. #endif
  823.  
  824.   if (echo_now)
  825.     echo ();
  826.   else
  827.     echo_flag = 1;
  828.  
  829. #ifdef BSD4_1
  830.   sigunhold (SIGALRM);
  831. #endif
  832.  
  833.   errno = old_errno;
  834. }
  835.  
  836. /* Nonzero means polling for input is temporarily suppresed.  */
  837. int poll_suppress_count;
  838.  
  839. /* Number of seconds between polling for input.  */
  840. int polling_period;
  841.  
  842. #ifdef POLL_FOR_INPUT
  843. int polling_for_input;
  844.  
  845. /* Handle an alarm once each second and read pending input
  846.    so as to handle a C-g if it comces in.  */
  847.  
  848. input_poll_signal ()
  849. {
  850.   int junk;
  851.  
  852.   if (!waiting_for_input)
  853.     read_avail_input (&junk);
  854.   signal (SIGALRM, input_poll_signal);
  855.   alarm (polling_period);
  856. }
  857.  
  858. #endif
  859.  
  860. /* Begin signals to poll for input, if they are appropriate.
  861.    This function is called unconditionally from various places.  */
  862.  
  863. start_polling ()
  864. {
  865. #ifdef POLL_FOR_INPUT
  866.   if (read_socket_hook)
  867.     {
  868.       poll_suppress_count--;
  869.       if (poll_suppress_count == 0)
  870.     {
  871.       signal (SIGALRM, input_poll_signal);
  872.       polling_for_input = 1;
  873.       alarm (polling_period);
  874.     }
  875.     }
  876. #endif
  877. }
  878.  
  879. /* Turn off polling.  */
  880.  
  881. stop_polling ()
  882. {
  883. #ifdef POLL_FOR_INPUT
  884.   if (read_socket_hook)
  885.     {
  886.       if (poll_suppress_count == 0)
  887.     {
  888.       polling_for_input = 0;
  889.       alarm (0);
  890.     }
  891.       poll_suppress_count++;
  892.     }
  893. #endif
  894. }
  895.  
  896. /* read a character from the keyboard; call the redisplay if needed */
  897. /* commandflag 0 means do not do auto-saving, but do do redisplay.
  898.    -1 means do not do redisplay, but do do autosaving.
  899.    1 means do both.  */
  900.  
  901. read_command_char (commandflag)
  902.      int commandflag;
  903. {
  904.   register int c;
  905.   int alarmtime;
  906.   int count;
  907.   Lisp_Object tem;
  908.   jmp_buf save_jump;
  909.   extern request_echo ();
  910.  
  911.   if (unread_command_char >= 0)
  912.     {
  913.       c = unread_command_char;
  914.       unread_command_char = -1;
  915.       if (this_command_key_count == 0)
  916.     goto reread_first;
  917.       goto reread;
  918.     }
  919.  
  920.   if (!NULL (Vexecuting_macro))
  921.     {
  922.       if (XTYPE (Vexecuting_macro) != Lisp_String
  923.       || XSTRING (Vexecuting_macro)->size <= executing_macro_index)
  924.     return -1;
  925.       QUIT;
  926.       c = XSTRING (Vexecuting_macro)->data[executing_macro_index++];
  927.       goto from_macro;
  928.     }
  929.  
  930.   /* Save outer setjmp data, in case called recursively.  */
  931.   bcopy (getcjmp, save_jump, sizeof getcjmp);
  932.  
  933.   stop_polling ();
  934.  
  935.   if (commandflag >= 0 && !detect_input_pending ())
  936.     redisplay ();
  937.  
  938.   if (commandflag != 0
  939.       && auto_save_interval > 0
  940.       && num_input_chars - last_auto_save > max (auto_save_interval, 20)
  941.       && !detect_input_pending ())
  942.     Fdo_auto_save (Qnil);
  943.  
  944.   if (_setjmp (getcjmp))
  945.     {
  946.       c = quit_char;
  947.       waiting_for_input = 0;
  948.       input_available_clear_word = 0;
  949.  
  950.       goto non_reread;
  951.     }
  952.  
  953.   /* Message turns off echoing unless more keystrokes turn it on again. */
  954.   if (echo_area_contents && *echo_area_contents && echo_area_contents != echobuf)
  955.     cancel_echoing ();
  956.   else
  957.     /* If already echoing, continue, and prompt.  */
  958.     echo_dash ();
  959.  
  960.   /* If in middle of key sequence and minibuffer not active,
  961.      start echoing if enough time elapses.  */
  962.   if (minibuf_level == 0 && !immediate_echo && this_command_key_count > 0
  963.       && echo_keystrokes > 0
  964.       && (echo_area_contents == 0 || *echo_area_contents == 0))
  965.     {
  966.       /* Else start echoing if user waits more than `alarmtime' seconds. */
  967.       /* This interrupt either calls echo right away
  968.      or sets echo_flag, which causes echo to be called
  969.      by set_waiting_for_input's next invocation.  */
  970.       signal (SIGALRM, request_echo);
  971.       echo_flag = 0;
  972.       echo_now = 0;
  973.       alarmtime = echo_keystrokes;
  974.       alarm ((unsigned) alarmtime);
  975.     }
  976.  
  977.   c = kbd_buffer_read_command_char ();
  978.  
  979.   /* Terminate Emacs in batch mode if at eof.  */
  980.   if (noninteractive && c < 0)
  981.     Fkill_emacs (make_number (1));
  982.  
  983.  non_reread:
  984.  
  985.   bcopy (save_jump, getcjmp, sizeof getcjmp);
  986.  
  987.   /* Cancel alarm if it was set and has not already gone off. */
  988.   if (alarmtime > 0) alarm (0);
  989.  
  990.   echo_area_contents = 0;
  991.  
  992.   if (c < 0) return -1;
  993.  
  994.   c &= meta_key ? 0377 : 0177;
  995.  
  996.   if (XTYPE (Vkeyboard_translate_table) == Lisp_String
  997.       && XSTRING (Vkeyboard_translate_table)->size > c)
  998.     c = XSTRING (Vkeyboard_translate_table)->data[c];
  999.  
  1000.   total_keys++;
  1001.   recent_keys[recent_keys_index] = c;
  1002.   recent_keys_index = (recent_keys_index + 1) % sizeof recent_keys;
  1003.  
  1004.   if (dribble)
  1005.     {
  1006.       putc (c, dribble);
  1007.       fflush (dribble);
  1008.     }
  1009.  
  1010.   store_kbd_macro_char (c);
  1011.  
  1012.   start_polling ();
  1013.  
  1014.  from_macro:
  1015.  reread_first:  /* Rereading a char and it is the first in a command.  */
  1016.  
  1017.   echo_char (c);
  1018.  
  1019.   /* Record this character as part of the current key.  */
  1020.   if (this_command_key_count == this_command_keys_size)
  1021.     {
  1022.       this_command_keys_size *= 2;
  1023.       this_command_keys
  1024.     = (unsigned char *) xrealloc (this_command_keys,
  1025.                       this_command_keys_size);
  1026.     }
  1027.   this_command_keys[this_command_key_count++] = c;
  1028.  
  1029.   /* Rereading in the middle of a command.  */
  1030.  reread:
  1031.  
  1032.   last_input_char = c;
  1033.  
  1034.   num_input_chars++;
  1035.  
  1036.   /* Process the help character specially if enabled */
  1037.   if (c == help_char && !NULL (Vhelp_form))
  1038.     {
  1039.       count = specpdl_ptr - specpdl;
  1040.  
  1041.       record_unwind_protect (Fset_window_configuration,
  1042.                  Fcurrent_window_configuration ());
  1043.  
  1044.       tem = Feval (Vhelp_form);
  1045.       if (XTYPE (tem) == Lisp_String)
  1046.     internal_with_output_to_temp_buffer ("*Help*", print_help, tem);
  1047.  
  1048.       cancel_echoing ();
  1049.       c = read_command_char (0);
  1050.       /* Remove the help from the screen */
  1051.       unbind_to (count);
  1052.       redisplay ();
  1053.       if (c == 040)
  1054.     {
  1055.       cancel_echoing ();
  1056.       c = read_command_char (0);
  1057.     }
  1058.     }
  1059.  
  1060.   return c;
  1061. }
  1062.  
  1063. Lisp_Object
  1064. print_help (object)
  1065.      Lisp_Object object;
  1066. {
  1067.   Fprinc (object, Qnil);
  1068.   return Qnil;
  1069. }
  1070.  
  1071. /* Low level keyboard input.
  1072.  Read characters into kbd_buffer
  1073.  from which they are obtained by kbd_buffer_read_command_char.  */
  1074.  
  1075. /* Set this for debugging, to have a way to get out */
  1076. int stop_character;
  1077.  
  1078. /* Store a character obtained at interrupt level into kbd_buffer, fifo */
  1079. kbd_buffer_store_char (c)
  1080.      register int c;
  1081. {
  1082.   c &= 0377;
  1083.  
  1084.   if (c == quit_char
  1085.       || ((c == (0200 | quit_char)) && !meta_key))
  1086.     {
  1087.       interrupt_signal ();
  1088.       return;
  1089.     }
  1090.  
  1091.   if (c && c == stop_character)
  1092.     {
  1093.       sys_suspend ();
  1094.       return;
  1095.     }
  1096.  
  1097.   if (kbd_ptr != kbd_buffer)
  1098.     {
  1099.       bcopy (kbd_ptr, kbd_buffer, kbd_count);
  1100.       kbd_ptr = kbd_buffer;
  1101.     }
  1102.  
  1103.   if (kbd_count < sizeof kbd_buffer)
  1104.     {
  1105.       kbd_buffer[kbd_count++] = c;
  1106.     }
  1107. }
  1108.  
  1109. kbd_buffer_read_command_char ()
  1110. {
  1111.   register int c;
  1112.  
  1113.   if (noninteractive)
  1114.     {
  1115.       c = getchar ();
  1116.       return c;
  1117.     }
  1118.  
  1119.   /* Either ordinary input buffer or C-g buffered means we can return.  */
  1120.   while (!kbd_count)
  1121.     {
  1122.       if (!NULL (Vquit_flag))
  1123.     quit_throw_to_read_command_char ();
  1124.  
  1125.       /* One way or another, wait until input is available; then, if
  1126.      interrupt handlers have not read it, read it now.  */
  1127.  
  1128. #ifdef VMS
  1129.       wait_for_kbd_input ();
  1130. #else
  1131. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  1132. #ifdef SIGIO
  1133.       gobble_input ();
  1134. #endif /* SIGIO */
  1135.       if (!kbd_count)
  1136.     {
  1137. #ifdef subprocesses
  1138.       wait_reading_process_input (0, -1, 1);
  1139. #else
  1140. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  1141. #ifdef SIGIO
  1142.       if (interrupt_input)
  1143.         {
  1144.           sigblockx (SIGIO);
  1145.           set_waiting_for_input (0);
  1146.           while (!kbd_count)
  1147.         sigpausex (SIGIO);
  1148.           clear_waiting_for_input ();
  1149.           sigunblockx (SIGIO);
  1150.         }
  1151. #else
  1152.       interrupt_input = 0;
  1153. #endif /* not SIGIO */
  1154. #endif /* subprocesses */
  1155.  
  1156.       if (!interrupt_input && !kbd_count)
  1157.         {
  1158.           read_avail_input (0);
  1159.         }
  1160.     }
  1161. #endif /* not VMS */
  1162.     }
  1163.  
  1164.   input_pending = --kbd_count > 0;
  1165.   c = *kbd_ptr;            /* *kbd_ptr++ would have a timing error. */
  1166.   kbd_ptr++;            /* See kbd_buffer_store_char. */
  1167.   return (c & (meta_key ? 0377 : 0177)); /* Clean up if sign was extended. */
  1168. }
  1169.  
  1170. /* Force an attempt to read input regardless of what FIONREAD says.  */
  1171.  
  1172. force_input_read ()
  1173. {
  1174.   force_input = 1;
  1175.   detect_input_pending ();
  1176.   force_input = 0;
  1177. }
  1178.  
  1179. /* Store into *addr the number of terminal input chars available.
  1180.    Equivalent to ioctl (0, FIONREAD, addr) but works
  1181.    even if FIONREAD does not exist.  */
  1182.  
  1183. static void
  1184. get_input_pending (addr)
  1185.      int *addr;
  1186. {
  1187. #ifdef VMS
  1188.   /* On VMS, we always have something in the buffer
  1189.      if any input is available.  */
  1190.   /*** It might be simpler to make interrupt_input 1 on VMS ***/
  1191.   *addr = kbd_count | !NULL (Vquit_flag);
  1192. #else
  1193.   /* First of all, have we already counted some input?  */
  1194.   *addr = kbd_count | !NULL (Vquit_flag);
  1195.   /* If input is being read as it arrives, and we have none, there is none.  */
  1196.   if (*addr > 0 || (interrupt_input && ! interrupts_deferred && ! force_input))
  1197.     return;
  1198. #ifdef FIONREAD
  1199.   if (! force_input)
  1200.     {
  1201.       /* If we can count the input without reading it, do so.  */
  1202.       if (ioctl (0, FIONREAD, addr) < 0)
  1203.     *addr = 0;
  1204.       if (*addr == 0 || read_socket_hook == 0)
  1205.     return;
  1206.       /* If the input consists of window-events, not all of them
  1207.      are necessarily kbd chars.  So process all the input
  1208.      and see how many kbd chars we got.  */
  1209.     }
  1210. #endif
  1211. #ifdef SIGIO
  1212.   {
  1213.     /* It seems there is a timing error such that a SIGIO can be handled here
  1214.        and cause kbd_count to become nonzero even though raising of SIGIO
  1215.        has already been turned off.  */
  1216.     SIGMASKTYPE mask = sigblock (sigmask (SIGIO));
  1217.     if (kbd_count == 0)
  1218.       read_avail_input (*addr);
  1219.     sigsetmask (mask);
  1220.   }
  1221. #else
  1222.   /* If we can't count the input, read it (if any) and see what we got.  */
  1223.   read_avail_input (*addr);
  1224. #endif
  1225.   *addr = kbd_count | !NULL (Vquit_flag);
  1226. #endif
  1227. }
  1228.  
  1229. /* Read pending any input out of the system and into Emacs.  */
  1230.  
  1231. /* This function is temporary in Emacs 18.  It is used only
  1232.    with X windows.  X windows always turns on interrupt input
  1233.    if possible, so this function has nothing to do except
  1234.    on systems that don't have SIGIO.  And they also don't have FIONREAD.  */
  1235. void
  1236. consume_available_input ()
  1237. {
  1238. #ifdef SIGIO
  1239.   if (!interrupt_input || interrupts_deferred)
  1240. #endif
  1241.     read_avail_input (0);
  1242. }
  1243.  
  1244. /* Read any terminal input already buffered up by the system
  1245.    into the kbd_buffer, assuming the buffer is currently empty.
  1246.    Never waits.
  1247.  
  1248.    If NREAD is nonzero, assume it contains # chars of raw data waiting.
  1249.    If it is zero, we determine that datum.
  1250.  
  1251.    Input gets into the kbd_buffer either through this function
  1252.    (at main program level) or at interrupt level if input
  1253.    is interrupt-driven.  */
  1254.  
  1255. static void
  1256. read_avail_input (nread)
  1257.      int nread;
  1258. {
  1259.   /* This function is not used on VMS.  */
  1260. #ifndef VMS
  1261.   char buf[256 * BUFFER_SIZE_FACTOR];
  1262.   register int i;
  1263.  
  1264. #ifdef FIONREAD
  1265.   if (! force_input)
  1266.     {
  1267.       if (nread == 0)
  1268.     get_input_pending (&nread);
  1269.       if (nread == 0)
  1270.     return;
  1271.     }
  1272.   if (nread > sizeof buf)
  1273.     nread = sizeof buf;
  1274.  
  1275.   /* Read what is waiting.  */
  1276.   if (read_socket_hook)
  1277.     nread = (*read_socket_hook) (0, buf, nread);
  1278.   else
  1279.     nread = read (0, buf, nread);
  1280.  
  1281. #else /* no FIONREAD */
  1282. #ifdef USG
  1283. #ifdef SYSV_STREAMS
  1284.   /* When talking to Xwindows using streams, something gets screwed up
  1285.      if Emacs alters this flag in the descriptor.  */
  1286.   if (!read_socket_hook)
  1287. #endif
  1288.     fcntl (fileno (stdin), F_SETFL, O_NDELAY);
  1289.   if (read_socket_hook)
  1290.     {
  1291.       nread = (*read_socket_hook) (0, buf, sizeof buf);
  1292.     }
  1293.   else
  1294.     {
  1295.       nread = read (fileno (stdin), buf, sizeof buf);
  1296.     }
  1297. #ifdef AIX
  1298.   /* The kernel sometimes fails to deliver SIGHUP for ptys.
  1299.      This looks incorrect, but it isn't, because _BSD causes
  1300.      O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
  1301.      and that causes a value other than 0 when there is no input.  */
  1302.   if (nread == 0)
  1303.     kill (SIGHUP, 0);
  1304. #endif
  1305. #ifdef EBADSLT
  1306.   if (nread == -1 && (errno == EAGAIN || errno == EBADSLT))
  1307. #else
  1308.   if (nread == -1 && errno == EAGAIN)
  1309. #endif
  1310.     nread = 0;
  1311. #ifdef SYSV_STREAMS
  1312.   if (!read_socket_hook)
  1313. #endif
  1314.     fcntl (fileno (stdin), F_SETFL, 0);
  1315. #else /* not USG */
  1316.   you lose
  1317. #endif /* not USG */
  1318. #endif /* no FIONREAD */
  1319.  
  1320.   /* Scan the chars for C-g and store them in kbd_buffer.  */
  1321.   if (kbd_count == 0)
  1322.     kbd_ptr = kbd_buffer;
  1323.  
  1324.   for (i = 0; i < nread; i++)
  1325.     {
  1326.       kbd_buffer_store_char (buf[i]);
  1327.       /* Don't look at input that follows a C-g too closely.
  1328.      This reduces lossage due to autorepeat on C-g.  */
  1329.       if (buf[i] == quit_char)
  1330.     break;
  1331.     }
  1332. #endif /* not VMS */
  1333. }
  1334.  
  1335. #ifdef SIGIO   /* for entire page */
  1336. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  1337.  
  1338. /* If using interrupt input and some input chars snuck into the
  1339.    buffer before we enabled interrupts, fake an interrupt for them.  */
  1340.  
  1341. gobble_input ()
  1342. {
  1343.   int nread;
  1344.   if (interrupt_input)
  1345.     {
  1346.       if (ioctl (0, FIONREAD, &nread) < 0)
  1347.     nread = 0;
  1348.       if (nread)
  1349.     {
  1350.       sigholdx (SIGIO);
  1351.       input_available_signal (SIGIO);
  1352.       sigfree ();
  1353.     }
  1354.     }
  1355. }
  1356.  
  1357. input_available_signal (signo)
  1358.      int signo;
  1359. {
  1360.   unsigned char buf[256 * BUFFER_SIZE_FACTOR];
  1361.   int nread;
  1362.   register int i;
  1363.   /* Must preserve main program's value of errno.  */
  1364.   int old_errno = errno;
  1365. #ifdef BSD4_1
  1366.   extern int select_alarmed;
  1367. #endif
  1368.  
  1369. #ifdef USG
  1370.   /* USG systems forget handlers when they are used;
  1371.      must reestablish each time */
  1372.   signal (signo, input_available_signal);
  1373. #endif /* USG */
  1374.  
  1375. #ifdef BSD4_1
  1376.   sigisheld (SIGIO);
  1377. #endif
  1378.  
  1379.   if (input_available_clear_word)
  1380.     *input_available_clear_word = 0;
  1381.  
  1382.   while (1)
  1383.     {
  1384.       if (ioctl (0, FIONREAD, &nread) < 0)
  1385.     /* Formerly simply exited the loop, but that sometimes led to
  1386.        a failure of Emacs to terminate.
  1387.        SIGHUP seems appropriate if we can't reach the terminal.  */
  1388.     kill (getpid (), SIGHUP);
  1389.       if (nread <= 0)
  1390.     break;
  1391. #ifdef BSD4_1
  1392.       select_alarmed = 1;  /* Force the select emulator back to life */
  1393. #endif
  1394.       if (read_socket_hook)
  1395.     {
  1396.       nread = (*read_socket_hook) (0, buf, sizeof buf);
  1397.       if (!nread)
  1398.         continue;
  1399.     }
  1400.       else
  1401.     {
  1402.       if (nread > sizeof buf)
  1403.         nread = sizeof buf;
  1404.       nread = read (0, buf, nread);
  1405.     }
  1406.  
  1407.       for (i = 0; i < nread; i++)
  1408.     {
  1409.       kbd_buffer_store_char (buf[i]);
  1410.       /* Don't look at input that follows a C-g too closely.
  1411.          This reduces lossage due to autorepeat on C-g.  */
  1412.       if (buf[i] == quit_char)
  1413.         break;
  1414.     }
  1415.     }
  1416. #ifdef BSD4_1
  1417.   sigfree ();
  1418. #endif
  1419.   errno = old_errno;
  1420. }
  1421. #endif /* SIGIO */
  1422.  
  1423. #if 0
  1424. /* This is turned off because it didn't produce much speedup.  */
  1425.  
  1426. /* Read a single-char key sequence.  Do not redisplay.
  1427.    Return 1 if successful, or 0 if what follows is not
  1428.    a single-char key.  (In that case, a char has been unread.)
  1429.    This is used instead of read_key_sequence as an optimization
  1430.    just after a direct-updating command is done, since at such
  1431.    times we know that no redisplay is required.  */
  1432.  
  1433. int
  1434. fast_read_one_key (keybuf)
  1435.      char *keybuf;
  1436. {
  1437.   register Lisp_Object map;
  1438.   register int c;
  1439.   register Lisp_Object tem;
  1440.  
  1441.   keys_prompt = 0;
  1442.   /* Read a character, and do not redisplay.  */
  1443.   c = read_command_char (-1);
  1444.   Vquit_flag = Qnil;
  1445.  
  1446.   /* Assume until further notice that we are unlucky
  1447.      and will return zero, so this char will be
  1448.      reread by read_key_sequence.  */
  1449.  
  1450.   unread_command_char = c;
  1451.  
  1452.   if (c < 0 || c >= 0200)
  1453.     return 0;
  1454.  
  1455.   map = current_buffer->keymap;
  1456.   if (!EQ (map, Qnil))
  1457.     {
  1458.       tem = get_keyelt (access_keymap (map, c));
  1459.       if (!EQ (tem, Qnil))
  1460.     return 0;
  1461.     }
  1462.  
  1463.   XSET (map, Lisp_Vector, global_map);
  1464.   tem = !NULL (map)
  1465.     ? get_keyelt (access_keymap (map, c))
  1466.       : Qnil;
  1467.  
  1468.   read_key_sequence_cmd = tem;
  1469.  
  1470.   /* trace symbols to their function definitions */
  1471.  
  1472.   while (XTYPE (tem) == Lisp_Symbol && !NULL (tem)
  1473.      && !EQ (tem, Qunbound))
  1474.     tem = XSYMBOL (tem)->function;
  1475.  
  1476.   /* Is the definition a prefix character?  */
  1477.  
  1478.   if (XTYPE (tem) == Lisp_Vector ||
  1479.       (CONSP (tem) && EQ (XCONS (tem)->car, Qkeymap)))
  1480.     return 0;
  1481.  
  1482.   unread_command_char = -1;
  1483.   keybuf[0] = c;
  1484.   return 1;
  1485. }
  1486.  
  1487. #endif /* 0 */
  1488.  
  1489. /* Read a sequence of keys that ends with a non prefix character,
  1490.  and store them in KEYBUF, a buffer of size BUFSIZE.
  1491.  Prompt with PROMPT.  Echo starting immediately unless `prompt' is 0.
  1492.  Return the length of the key sequence stored.
  1493.  NODISPLAY nonzero means don't do redisplay before the first character
  1494.  (just for speedup).  */
  1495.  
  1496. int
  1497. read_key_sequence (keybuf, bufsize, prompt, nodisplay)
  1498.      char *keybuf;
  1499.      int bufsize;
  1500.      unsigned char *prompt;
  1501.      int nodisplay;
  1502. {
  1503.   register int i;
  1504.   Lisp_Object nextlocal, nextglobal;
  1505.   register int c, nextc;
  1506.   Lisp_Object local, global;
  1507.  
  1508.   if (FROM_KBD)
  1509.     {
  1510.       if (prompt)
  1511.     echo_prompt (prompt);
  1512.       else if (cursor_in_echo_area)
  1513.     echo_dash ();
  1514.     }
  1515.  
  1516.   nextc = read_command_char (nodisplay ? -1 : !prompt);
  1517.   nextlocal = current_buffer->keymap;
  1518.   XSET (nextglobal, Lisp_Vector, global_map);
  1519.  
  1520.   i = 0;
  1521.   while (!NULL (nextlocal) || !NULL (nextglobal))
  1522.     {
  1523.       if (i == bufsize)
  1524.     error ("key sequence too long");
  1525.  
  1526.       if (nextc >= 0)
  1527.     {
  1528.       c = nextc;
  1529.       nextc = -1;
  1530.     }
  1531.       else
  1532.     c = read_command_char (!prompt);
  1533.       Vquit_flag = Qnil;
  1534.       nodisplay = 0;
  1535.  
  1536.       if (c < 0)
  1537.     return 0;
  1538.       if (c >= 0200)
  1539.     {
  1540.       nextc = c & 0177;
  1541.       c = meta_prefix_char;
  1542.     }
  1543.  
  1544.       keybuf[i] = c;
  1545.  
  1546.       global = !NULL (nextglobal)
  1547.     ? get_keyelt (access_keymap (nextglobal, c))
  1548.       : Qnil;
  1549.  
  1550.       local = !NULL (nextlocal)
  1551.     ? get_keyelt (access_keymap (nextlocal, c))
  1552.       : Qnil;
  1553.  
  1554.       /* If C is not defined in either keymap
  1555.      and it is an uppercase letter, try corresponding lowercase.  */
  1556.  
  1557.       if (NULL (global) && NULL (local) && UPPERCASEP (c))
  1558.     {
  1559.       global = !NULL (nextglobal)
  1560.         ? get_keyelt (access_keymap (nextglobal, DOWNCASE (c)))
  1561.           : Qnil;
  1562.  
  1563.       local = !NULL (nextlocal)
  1564.         ? get_keyelt (access_keymap (nextlocal, DOWNCASE (c)))
  1565.           : Qnil;
  1566.  
  1567.       /* If that has worked better that the original char,
  1568.          downcase it permanently.  */
  1569.  
  1570.       if (!NULL (global) || !NULL (local))
  1571.         {
  1572.           keybuf[i] = c = DOWNCASE (c);
  1573.         }
  1574.     }
  1575.  
  1576.       i++;
  1577.  
  1578.       nextlocal = Qnil;
  1579.       nextglobal = Qnil;
  1580.  
  1581.       read_key_sequence_cmd = !NULL (local) ? local : global;
  1582.  
  1583.       /* trace symbols to their function definitions */
  1584.  
  1585.       while (XTYPE (global) == Lisp_Symbol && !NULL (global)
  1586.          && !EQ (global, Qunbound))
  1587.     global = XSYMBOL (global)->function;
  1588.       while (XTYPE (local) == Lisp_Symbol && !NULL (local)
  1589.          && !EQ (local, Qunbound))
  1590.     local = XSYMBOL (local)->function;
  1591.  
  1592.       /* Are the definitions prefix characters? */
  1593.  
  1594.       if (XTYPE (local) == Lisp_Vector ||
  1595.       (CONSP (local) && EQ (XCONS (local)->car, Qkeymap))
  1596.       ||
  1597.       /* If nextc is set, we are processing a prefix char
  1598.          that represents a meta-bit.
  1599.          Let a global prefix definition override a local non-prefix.
  1600.          This is for minibuffers that redefine Escape for completion.
  1601.          A real Escape gets completion, but Meta bits get ESC-prefix.  */
  1602.       ((NULL (local) || nextc >= 0)
  1603.        && (XTYPE (global) == Lisp_Vector ||
  1604.            (CONSP (global) && EQ (XCONS (global)->car, Qkeymap)))))
  1605.     {
  1606.       if (XTYPE (local) == Lisp_Vector ||
  1607.           (CONSP (local) && EQ (XCONS (local)->car, Qkeymap)))
  1608.         nextlocal = local;
  1609.       else
  1610.         nextlocal = Qnil;
  1611.  
  1612.       if (XTYPE (global) == Lisp_Vector ||
  1613.           (CONSP (global) && EQ (XCONS (global)->car, Qkeymap)))
  1614.         nextglobal = global;
  1615.       else
  1616.         nextglobal = Qnil;
  1617.     }
  1618.     }
  1619.  
  1620.   return i;
  1621. }
  1622.  
  1623. DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 1, 0,
  1624.   "Read a sequence of keystrokes and return as a string.\n\
  1625. The sequence is sufficient to specify a non-prefix command\n\
  1626. starting from the current local and global keymaps.\n\
  1627. A C-g typed while in this function is treated like\n\
  1628. any other character, and quit-flag is not set.\n\
  1629. One arg, PROMPT, a prompt string or  nil, meaning do not prompt specially.")
  1630.   (prompt)
  1631.      Lisp_Object prompt;
  1632. {
  1633.   char keybuf[30];
  1634.   register int i;
  1635.  
  1636.   if (!NULL (prompt))
  1637.     CHECK_STRING (prompt, 0);
  1638.   QUIT;
  1639.  
  1640.   this_command_key_count = 0;
  1641.   i = read_key_sequence (keybuf, sizeof keybuf,
  1642.              (NULL (prompt)) ? 0 : XSTRING (prompt)->data,
  1643.              0);
  1644.   return make_string (keybuf, i);
  1645. }
  1646.  
  1647. DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
  1648.  "Execute CMD as an editor command.\n\
  1649. CMD must be a symbol that satisfies the `commandp' predicate.\n\
  1650. Optional second arg RECORD-FLAG non-nil\n\
  1651. means unconditionally put this command in the command-history.\n\
  1652. Otherwise, this is done only if an arg is read using the minibuffer.")
  1653.      (cmd, record)
  1654.      Lisp_Object cmd, record;
  1655. {
  1656.   register Lisp_Object final;
  1657.   register Lisp_Object tem;
  1658.   Lisp_Object prefixarg;
  1659.   struct backtrace backtrace;
  1660.   extern int debug_on_next_call;
  1661.  
  1662.   prefixarg = Vprefix_arg, Vprefix_arg = Qnil;
  1663.   Vcurrent_prefix_arg = prefixarg;
  1664.   debug_on_next_call = 0;
  1665.  
  1666.   if (XTYPE (cmd) == Lisp_Symbol)
  1667.     {
  1668.       tem = Fget (cmd, Qdisabled);
  1669.       if (!NULL (tem))
  1670.     return call0 (Vdisabled_command_hook);
  1671.     }
  1672.  
  1673.   while (1)
  1674.     {
  1675.       final = cmd;
  1676.       while (XTYPE (final) == Lisp_Symbol)
  1677.     {
  1678.       if (EQ (Qunbound, XSYMBOL (final)->function))
  1679.         Fsymbol_function (final);    /* Get an error! */
  1680.       final = XSYMBOL (final)->function;
  1681.     }
  1682.  
  1683.       if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
  1684.     do_autoload (final, cmd);
  1685.       else
  1686.     break;
  1687.     }
  1688.  
  1689.   if (CONSP (final) || XTYPE (final) == Lisp_Subr)
  1690.     {
  1691.       backtrace.next = backtrace_list;
  1692.       backtrace_list = &backtrace;
  1693.       backtrace.function = &Qcall_interactively;
  1694.       backtrace.args = &cmd;
  1695.       backtrace.nargs = 1;
  1696.       backtrace.evalargs = 0;
  1697.  
  1698.       tem = Fcall_interactively (cmd, record);
  1699.  
  1700.       backtrace_list = backtrace.next;
  1701.       return tem;
  1702.     }
  1703.   if (XTYPE (final) == Lisp_String)
  1704.     {
  1705.       return Fexecute_kbd_macro (final, prefixarg);
  1706.     }
  1707.   return Qnil;
  1708. }
  1709.  
  1710. DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
  1711.   1, 1, "P",
  1712.   "Read function name, then read its arguments and call it.")
  1713.   (prefixarg)
  1714.      Lisp_Object prefixarg;
  1715. {
  1716.   Lisp_Object function;
  1717.   char buf[40];
  1718.   Lisp_Object saved_keys;
  1719.   struct gcpro gcpro1;
  1720.  
  1721.   saved_keys = Fthis_command_keys ();
  1722.   GCPRO1 (saved_keys);
  1723.  
  1724.   buf[0] = 0;
  1725.  
  1726.   if (EQ (prefixarg, Qminus))
  1727.     strcpy (buf, "- ");
  1728.   else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
  1729.     strcpy (buf, "C-u ");
  1730.   else if (CONSP (prefixarg) && XTYPE (XCONS (prefixarg)->car) == Lisp_Int)
  1731.     sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
  1732.   else if (XTYPE (prefixarg) == Lisp_Int)
  1733.     sprintf (buf, "%d ", XINT (prefixarg));
  1734.  
  1735.   /* This isn't strictly correct if execute-extended-command
  1736.      is bound to anything else */
  1737.   strcat (buf, "M-x ");
  1738.  
  1739.   function = Fcompleting_read (build_string (buf), Vobarray, Qcommandp, Qt, Qnil);
  1740.  
  1741.   saved_keys = concat2 (saved_keys, function);
  1742.   if (this_command_keys_size < XSTRING (saved_keys)->size)
  1743.     {
  1744.       /* This makes the buffer bigger than necessary, but that's okay.  */
  1745.       this_command_keys_size += XSTRING (saved_keys)->size;
  1746.       this_command_keys = (unsigned char *) xrealloc (this_command_keys,
  1747.                               this_command_keys_size);
  1748.     }
  1749.   bcopy (XSTRING (saved_keys)->data, this_command_keys,
  1750.      XSTRING (saved_keys)->size + 1);
  1751.   this_command_key_count = XSTRING (saved_keys)->size;
  1752.  
  1753.   UNGCPRO;
  1754.  
  1755.   function = Fintern (function, Vobarray);
  1756.   Vprefix_arg = prefixarg;
  1757.   this_command = function;
  1758.  
  1759.   return Fcommand_execute (function, Qt);
  1760. }
  1761.  
  1762. detect_input_pending ()
  1763. {
  1764.   if (!input_pending)
  1765.     get_input_pending (&input_pending);
  1766.  
  1767.   return input_pending;
  1768. }
  1769.  
  1770. /* This is called in some cases before a possible quit.
  1771.    It cases the next call to detect_input_pending to recompute input_pending.
  1772.    So calling this function unnecessarily can't do any harm.  */
  1773. clear_input_pending ()
  1774. {
  1775.   input_pending = 0;
  1776. }
  1777.  
  1778. DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
  1779.   "T if command input is currently available with no waiting.\n\
  1780. Actually, the value is NIL only if we can be sure that no input is available.")
  1781.   ()
  1782. {
  1783.   if (unread_command_char >= 0) return Qt;
  1784.  
  1785.   return detect_input_pending () ? Qt : Qnil;
  1786. }
  1787.  
  1788. DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
  1789.   "Return string of last 100 chars read from terminal.")
  1790.   ()
  1791. {
  1792.   Lisp_Object val;
  1793.   if (total_keys < sizeof recent_keys)
  1794.     return make_string (recent_keys, total_keys);
  1795.  
  1796.   val = make_string (recent_keys, sizeof recent_keys);
  1797.   bcopy (recent_keys + recent_keys_index,
  1798.      XSTRING (val)->data,
  1799.      sizeof recent_keys - recent_keys_index);
  1800.   bcopy (recent_keys,
  1801.      XSTRING (val)->data + sizeof recent_keys - recent_keys_index,
  1802.      recent_keys_index);
  1803.   return val;
  1804. }
  1805.  
  1806. DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
  1807.   "Return string of the keystrokes that invoked this command.")
  1808.   ()
  1809. {
  1810.   return make_string (this_command_keys, this_command_key_count);
  1811. }
  1812.  
  1813. DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
  1814.   "Return the current depth in recursive edits.")
  1815.   ()
  1816. {
  1817.   Lisp_Object temp;
  1818.   XFASTINT (temp) = command_loop_level + minibuf_level;
  1819.   return temp;
  1820. }
  1821.  
  1822. DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
  1823.   "FOpen dribble file: ",
  1824.   "Start writing all keyboard characters to FILE.\n\
  1825. Use nil as an argument to close the dribble file.")
  1826.   (file)
  1827.      Lisp_Object file;
  1828. {
  1829.   if (dribble != 0)
  1830.     fclose (dribble);
  1831.   dribble = 0;
  1832.   if (!NULL (file))
  1833.     {
  1834.       file = Fexpand_file_name (file, Qnil);
  1835.       dribble = fopen (XSTRING (file)->data, "w");
  1836.     }
  1837.   return Qnil;
  1838. }
  1839.  
  1840. DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
  1841.   "Discard the contents of the terminal input buffer.\n\
  1842. Also flush any kbd macro definition in progress.")
  1843.   ()
  1844. {
  1845.   defining_kbd_macro = 0;
  1846.   update_mode_lines++;
  1847.  
  1848.   unread_command_char = -1;
  1849.   Vquit_flag = Qnil;
  1850.   discard_tty_input ();
  1851.  
  1852.   kbd_count = 0;
  1853.   input_pending = 0;
  1854.  
  1855.   return Qnil;
  1856. }
  1857.  
  1858. DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
  1859.   "Stop Emacs and return to superior process.  You can resume.\n\
  1860. If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
  1861. to be read as terminal input by Emacs's superior shell.\n\
  1862. Before suspending, if `suspend-hook' is bound and value is non-nil\n\
  1863. call the value as a function of no args.  Don't suspend if it returns non-nil.\n\
  1864. Otherwise, suspend normally and after resumption call\n\
  1865. `suspend-resume-hook' if that is bound and non-nil.")
  1866.   (stuffstring)
  1867.      Lisp_Object stuffstring;
  1868. {
  1869.   register Lisp_Object tem;
  1870.   int count = specpdl_ptr - specpdl;
  1871.   int old_height, old_width;
  1872.   int width, height;
  1873.   struct gcpro gcpro1;
  1874.   extern init_sys_modes ();
  1875.  
  1876.   if (!NULL (stuffstring))
  1877.     CHECK_STRING (stuffstring, 0);
  1878.   GCPRO1 (stuffstring);
  1879.  
  1880.   /* Call value of suspend-hook
  1881.      if it is bound and value is non-nil.  */
  1882.   tem = intern ("suspend-hook");
  1883.   tem = XSYMBOL (tem)->value;
  1884.   if (! EQ (tem, Qunbound) && ! EQ (tem, Qnil))
  1885.     {
  1886.       tem = call0 (tem);
  1887.       if (!EQ (tem, Qnil)) return Qnil;
  1888.     }
  1889.  
  1890.   get_screen_size (&old_width, &old_height);
  1891.   reset_sys_modes ();
  1892.   /* sys_suspend can get an error if it tries to fork a subshell
  1893.      and the system resources aren't available for that.  */
  1894.   record_unwind_protect (init_sys_modes, 0);
  1895.   stuff_buffered_input (stuffstring);
  1896.   sys_suspend ();
  1897.   unbind_to (count);
  1898.  
  1899.   /* Check if terminal/window size has changed.
  1900.      Note that this is not useful when we are running directly
  1901.      with a window system; but suspend should be disabled in that case.  */
  1902.   get_screen_size (&width, &height);
  1903.   if (width != old_width || height != old_height)
  1904.     change_screen_size (height, width, 0, 0, 0);
  1905.  
  1906.   /* Call value of suspend-resume-hook
  1907.      if it is bound and value is non-nil.  */
  1908.   tem = intern ("suspend-resume-hook");
  1909.   tem = XSYMBOL (tem)->value;
  1910.   if (! EQ (tem, Qunbound) && ! EQ (tem, Qnil))
  1911.     call0 (tem);
  1912.   UNGCPRO;
  1913.   return Qnil;
  1914. }
  1915.  
  1916. /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
  1917.    Then in any case stuff anthing Emacs has read ahead and not used.  */
  1918.  
  1919. stuff_buffered_input (stuffstring)
  1920.      Lisp_Object stuffstring;
  1921. {
  1922.   register unsigned char *p;
  1923.  
  1924. /* stuff_char works only in BSD, versions 4.2 and up.  */
  1925. #ifdef BSD
  1926. #ifndef BSD4_1
  1927.   if (XTYPE (stuffstring) == Lisp_String)
  1928.     {
  1929.       register int count;
  1930.  
  1931.       p = XSTRING (stuffstring)->data;
  1932.       count = XSTRING (stuffstring)->size;
  1933.       while (count-- > 0)
  1934.     stuff_char (*p++);
  1935.       stuff_char ('\n');
  1936.     }
  1937.   /* Anything we have read ahead, put back for the shell to read.  */
  1938.   while (kbd_count)
  1939.     {
  1940.       stuff_char (*kbd_ptr++);
  1941.       kbd_count--;
  1942.     }
  1943.   input_pending = 0;
  1944. #endif
  1945. #endif /* BSD and not BSD4_1 */
  1946. }
  1947.  
  1948. set_waiting_for_input (word_to_clear)
  1949.      long *word_to_clear;
  1950. {
  1951.   input_available_clear_word = word_to_clear;
  1952.  
  1953.   /* Tell interrupt_signal to throw back to read_command_char,  */
  1954.   waiting_for_input = 1;
  1955.  
  1956.   /* If interrupt_signal was called before and buffered a C-g,
  1957.      make it run again now, to avoid timing error.  */
  1958.   if (!NULL (Vquit_flag))
  1959.     quit_throw_to_read_command_char ();
  1960.  
  1961.   /* Tell alarm signal to echo right away */
  1962.   echo_now = 1;
  1963.  
  1964.   /* If alarm has gone off already, echo now.  */
  1965.   if (echo_flag)
  1966.     {
  1967.       echo ();
  1968.       echo_flag = 0;
  1969.     }
  1970. }
  1971.  
  1972. clear_waiting_for_input ()
  1973. {
  1974.   /* Tell interrupt_signal not to throw back to read_command_char,  */
  1975.   waiting_for_input = 0;
  1976.   echo_now = 0;
  1977.   input_available_clear_word = 0;
  1978. }
  1979.  
  1980. /* This routine is called at interrupt level in response to C-G.
  1981.  If interrupt_input, this is the handler for SIGINT.
  1982.  Otherwise, it is called from kbd_buffer_store_char,
  1983.  in handling SIGIO or SIGTINT.
  1984.  
  1985.  If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
  1986.  immediately throw back to read_command_char.
  1987.  
  1988.  Otherwise it sets the Lisp variable  quit-flag  not-nil.
  1989.  This causes  eval  to throw, when it gets a chance.
  1990.  If  quit-flag  is already non-nil, it stops the job right away.  */
  1991.  
  1992. interrupt_signal ()
  1993. {
  1994.   char c;
  1995.   /* Must preserve main program's value of errno.  */
  1996.   int old_errno = errno;
  1997.   extern Lisp_Object Vwindow_system;
  1998.  
  1999. #ifdef USG
  2000.   /* USG systems forget handlers when they are used;
  2001.      must reestablish each time */
  2002.   signal (SIGINT, interrupt_signal);
  2003.   signal (SIGQUIT, interrupt_signal);
  2004. #endif /* USG */
  2005.  
  2006.   cancel_echoing ();
  2007.  
  2008.   if (!NULL (Vquit_flag) && NULL (Vwindow_system))
  2009.     {
  2010.       fflush (stdout);
  2011.       reset_sys_modes ();
  2012.       sigfree ();
  2013. #ifdef SIGTSTP            /* Support possible in later USG versions */
  2014. /*
  2015.  * On systems which can suspend the current process and return to the original
  2016.  * shell, this command causes the user to end up back at the shell.
  2017.  * The "Auto-save" and "Abort" questions are not asked until
  2018.  * the user elects to return to emacs, at which point he can save the current
  2019.  * job and either dump core or continue.
  2020.  */
  2021.       sys_suspend ();
  2022. #else
  2023. #ifdef VMS
  2024.       if (sys_suspend () == -1)
  2025.     {
  2026.       printf ("Not running as a subprocess;\n");
  2027.       printf ("you can continue or abort.\n");
  2028.     }
  2029. #else /* not VMS */
  2030.       /* Perhaps should really fork an inferior shell?
  2031.      But that would not provide any way to get back
  2032.      to the original shell, ever.  */
  2033.       printf ("No support for stopping a process on this operating system;\n");
  2034.       printf ("you can continue or abort.\n");
  2035. #endif /* not VMS */
  2036. #endif /* not SIGTSTP */
  2037.       printf ("Auto-save? (y or n) ");
  2038.       fflush (stdout);
  2039.       if (((c = getchar ()) & ~040) == 'Y')
  2040.     Fdo_auto_save (Qnil);
  2041.       while (c != '\n') c = getchar ();
  2042. #ifdef VMS
  2043.       printf ("Abort (and enter debugger)? (y or n) ");
  2044. #else /* not VMS */
  2045.       printf ("Abort (and dump core)? (y or n) ");
  2046. #endif /* not VMS */
  2047.       fflush (stdout);
  2048.       if (((c = getchar ()) & ~040) == 'Y')
  2049.     abort ();
  2050.       while (c != '\n') c = getchar ();
  2051.       printf ("Continuing...\n");
  2052.       fflush (stdout);
  2053.       init_sys_modes ();
  2054.     }
  2055.   else
  2056.     {
  2057.       /* If executing a function that wants to be interrupted out of
  2058.          and the user has not deferred quitting by binding `inhibit-quit'
  2059.          then quit right away.  */
  2060.       if (immediate_quit && NULL (Vinhibit_quit))
  2061.     {
  2062.       immediate_quit = 0;
  2063.           sigfree ();
  2064.       Fsignal (Qquit, Qnil);
  2065.     }
  2066.       else
  2067.     /* Else request quit when it's safe */
  2068.     Vquit_flag = Qt;
  2069.     }
  2070.  
  2071.   if (waiting_for_input && !echoing)
  2072.     quit_throw_to_read_command_char ();
  2073.  
  2074.   errno = old_errno;
  2075. }
  2076.  
  2077. /* Handle a C-g by making read_command_char return C-g.  */
  2078.  
  2079. quit_throw_to_read_command_char ()
  2080. {
  2081.   quit_error_check ();
  2082.   sigfree ();
  2083.   /* Prevent another signal from doing this before we finish.  */
  2084.   waiting_for_input = 0;
  2085.   input_pending = 0;
  2086.   unread_command_char = -1;
  2087. #ifdef POLL_FOR_INPUT
  2088.   if (poll_suppress_count != 1)
  2089.     abort ();
  2090. #endif
  2091.   _longjmp (getcjmp, 1);
  2092. }
  2093.  
  2094. DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 2, 3, 0,
  2095.   "Set mode of reading keyboard input.\n\
  2096. First arg non-nil means use input interrupts; nil means use CBREAK mode.\n\
  2097. Second arg non-nil means use ^S/^Q flow control for output to terminal\n\
  2098.  (no effect except in CBREAK mode).\n\
  2099. Optional third arg non-nil specifies character to use for quitting.\n\n\
  2100. Note that the arguments will change incompatibly in version 19.")
  2101.   (interrupt, flow, quit)
  2102.      Lisp_Object interrupt, flow, quit;
  2103. {
  2104.   reset_sys_modes ();
  2105. #ifdef SIGIO
  2106. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  2107. #ifdef NO_SOCK_SIGIO
  2108.   if (read_socket_hook)
  2109.     interrupt_input = 0;    /* No interrupts if reading from a socket.  */
  2110.   else
  2111. #endif /* NO_SOCK_SIGIO */
  2112.     interrupt_input = !NULL (interrupt);
  2113. #else /* not SIGIO */
  2114.   interrupt_input = 0;
  2115. #endif /* not SIGIO */
  2116.   flow_control = !NULL (flow);
  2117.   if (!NULL (quit))
  2118.     {
  2119.       CHECK_NUMBER (quit, 2);
  2120.       quit_char = XINT (quit);
  2121.       /* Don't let this value be out of range.  */
  2122.       quit_char &= (meta_key ? 0377 : 0177);
  2123.     }
  2124.   init_sys_modes ();
  2125.   return Qnil;
  2126. }
  2127.  
  2128. init_keyboard ()
  2129. {
  2130.   this_command_keys_size = 40;
  2131.   this_command_keys = (unsigned char *) xmalloc (40);
  2132.  
  2133.   command_loop_level = -1;    /* Correct, before outermost invocation.  */
  2134.   quit_char = Ctl ('G');
  2135.   immediate_quit = 0;
  2136.   unread_command_char = -1;
  2137.   recent_keys_index = 0;
  2138.   total_keys = 0;
  2139.   kbd_count = 0;
  2140.   kbd_ptr = kbd_buffer;
  2141.   input_pending = 0;
  2142.   force_input = 0;
  2143.   if (!noninteractive)
  2144.     {
  2145.       signal (SIGINT, interrupt_signal);
  2146. #ifdef HAVE_TERMIO
  2147.       /* On  systems with TERMIO, C-g is set up for both SIGINT and SIGQUIT
  2148.      and we can't tell which one it will give us.  */
  2149.       signal (SIGQUIT, interrupt_signal);
  2150. #endif /* HAVE_TERMIO */
  2151. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  2152. #ifdef SIGIO
  2153.       signal (SIGIO, input_available_signal);
  2154. #endif /* SIGIO */
  2155.     }
  2156.  
  2157. /* Use interrupt input by default, if it works and noninterrupt input
  2158.    has deficiencies.  */
  2159.  
  2160. #ifdef INTERRUPT_INPUT
  2161.   interrupt_input = 1;
  2162. #else
  2163.   interrupt_input = 0;
  2164. #endif
  2165.  
  2166.   sigfree ();
  2167.   dribble = 0;
  2168.  
  2169.   if (keyboard_init_hook)
  2170.     (*keyboard_init_hook) ();
  2171.  
  2172.   poll_suppress_count = 1;
  2173. #ifdef POLL_FOR_INPUT
  2174.   start_polling ();
  2175. #endif
  2176. }
  2177.  
  2178. syms_of_keyboard ()
  2179. {
  2180.   Qself_insert_command = intern ("self-insert-command");
  2181.   staticpro (&Qself_insert_command);
  2182.  
  2183.   Qforward_char = intern ("forward-char");
  2184.   staticpro (&Qforward_char);
  2185.  
  2186.   Qbackward_char = intern ("backward-char");
  2187.   staticpro (&Qbackward_char);
  2188.  
  2189.   Qdisabled = intern ("disabled");
  2190.   staticpro (&Qdisabled);
  2191.  
  2192.   defsubr (&Sread_key_sequence);
  2193.   defsubr (&Srecursive_edit);
  2194.   defsubr (&Sinput_pending_p);
  2195.   defsubr (&Scommand_execute);
  2196.   defsubr (&Srecent_keys);
  2197.   defsubr (&Sthis_command_keys);
  2198.   defsubr (&Ssuspend_emacs);
  2199.   defsubr (&Sabort_recursive_edit);
  2200.   defsubr (&Sexit_recursive_edit);
  2201.   defsubr (&Srecursion_depth);
  2202.   defsubr (&Stop_level);
  2203.   defsubr (&Sdiscard_input);
  2204.   defsubr (&Sopen_dribble_file);
  2205.   defsubr (&Sset_input_mode);
  2206.   defsubr (&Sexecute_extended_command);
  2207.  
  2208.   DEFVAR_LISP ("disabled-command-hook", &Vdisabled_command_hook,
  2209.     "Value is called instead of any command that is disabled\n\
  2210. \(has a non-nil  disabled  property).");
  2211.  
  2212.   DEFVAR_BOOL ("meta-flag", &meta_key,
  2213.     "*Non-nil means treat 0200 bit in terminal input as Meta bit.");
  2214.  
  2215.   DEFVAR_INT ("last-command-char", &last_command_char,
  2216.     "Last terminal input character that was part of a command, as an integer.");
  2217.  
  2218.   DEFVAR_INT ("last-input-char", &last_input_char,
  2219.     "Last terminal input character, as an integer.");
  2220.  
  2221.   DEFVAR_INT ("unread-command-char", &unread_command_char,
  2222.     "Character to be read as next input from command input stream, or -1 if none.");
  2223.  
  2224.   DEFVAR_INT ("meta-prefix-char", &meta_prefix_char,
  2225.     "Meta-prefix character code.  Meta-foo as command input\n\
  2226. turns into this character followed by foo.");
  2227.   meta_prefix_char = 033;
  2228.  
  2229.   DEFVAR_LISP ("last-command", &last_command,
  2230.     "The last command executed.  Normally a symbol with a function definition,\n\
  2231. but can be whatever was found in the keymap, or whatever the variable\n\
  2232. `this-command' was set to by that command.");
  2233.   last_command = Qnil;
  2234.  
  2235.   DEFVAR_LISP ("this-command", &this_command,
  2236.     "The command now being executed.\n\
  2237. The command can set this variable; whatever is put here\n\
  2238. will be in  last-command  during the following command.");
  2239.   this_command = Qnil;
  2240.  
  2241.   DEFVAR_INT ("auto-save-interval", &auto_save_interval,
  2242.     "*Number of keyboard input characters between auto-saves.\n\
  2243. Zero means disable autosaving.");
  2244.   auto_save_interval = 300;
  2245.  
  2246.   DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
  2247.     "*Nonzero means echo unfinished commands after this many seconds of pause.");
  2248.   echo_keystrokes = 1;
  2249.  
  2250.   DEFVAR_INT ("polling-period", &polling_period,
  2251.     "*Interval between polling for input during Lisp execution.\n\
  2252. The reason for polling is to make C-g work to stop a running program.\n\
  2253. Polling is needed only when using X windows and SIGIO does not work.\n\
  2254. Polling is automatically disabled in all other cases.");
  2255.   polling_period = 2;
  2256.  
  2257.   DEFVAR_INT ("help-char", &help_char,
  2258.     "Character to recognize as meaning Help.\n\
  2259. When it is read, do (eval help-form), and display result if it's a string.\n\
  2260. If help-form's value is nil, this char can be read normally.");
  2261.   help_char = Ctl ('H');
  2262.  
  2263.   DEFVAR_LISP ("help-form", &Vhelp_form,
  2264.     "Form to execute when character help-char is read.\n\
  2265. If the form returns a string, that string is displayed.\n\
  2266. If help-form is nil, the help char is not recognized.");
  2267.   Vhelp_form = Qnil;
  2268.   
  2269.   DEFVAR_LISP ("top-level", &Vtop_level,
  2270.     "Form to evaluate when Emacs starts up.\n\
  2271. Useful to set before you dump a modified Emacs.");
  2272.   Vtop_level = Qnil;
  2273.  
  2274.   DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
  2275.     "String used as translate table for keyboard input, or nil.\n\
  2276. Each character is looked up in this string and the contents used instead.\n\
  2277. If string is of length N, character codes N and up are untranslated."); 
  2278.   Vkeyboard_translate_table = Qnil;
  2279. }
  2280.  
  2281. keys_of_keyboard ()
  2282. {
  2283.   ndefkey (Vglobal_map, Ctl ('Z'), "suspend-emacs");
  2284.   ndefkey (Vctl_x_map, Ctl ('Z'), "suspend-emacs");
  2285.   ndefkey (Vesc_map, Ctl ('C'), "exit-recursive-edit");
  2286.   ndefkey (Vglobal_map, Ctl (']'), "abort-recursive-edit");
  2287.   ndefkey (Vesc_map, 'x', "execute-extended-command");
  2288. }
  2289.