home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / utilities / cli / gnuinfo / Source / c / echo_area < prev    next >
Encoding:
Text File  |  1994-10-01  |  39.0 KB  |  1,502 lines

  1. #include "defines.h"
  2. /* echo_area.c -- How to read a line in the echo area. */
  3.  
  4. /* This file is part of GNU Info, a program for reading online documentation
  5.    stored in Info format.
  6.  
  7.    Copyright (C) 1993 Free Software Foundation, Inc.
  8.  
  9.    This program is free software; you can redistribute it and/or modify
  10.    it under the terms of the GNU General Public License as published by
  11.    the Free Software Foundation; either version 2, or (at your option)
  12.    any later version.
  13.  
  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.    You should have received a copy of the GNU General Public License
  20.    along with this program; if not, write to the Free Software
  21.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23.    Written by Brian Fox (bfox@ai.mit.edu). */
  24.  
  25. #include "info.h"
  26.  
  27. /* Non-zero means that C-g was used to quit reading input. */
  28. int info_aborted_echo_area = 0;
  29.  
  30. /* Non-zero means that the echo area is being used to read input. */
  31. int echo_area_is_active = 0;
  32.  
  33. /* The address of the last command executed in the echo area. */
  34. VFunction *ea_last_executed_command = (VFunction *)NULL;
  35.  
  36. /* Non-zero means that the last command executed while reading input
  37.    killed some text. */
  38. int echo_area_last_command_was_kill = 0;
  39.  
  40. /* Variables which hold on to the current state of the input line. */
  41. static char input_line[1 + EA_MAX_INPUT];
  42. static char *input_line_prompt;
  43. static int input_line_point;
  44. static int input_line_beg;
  45. static int input_line_end;
  46. static NODE input_line_node = {
  47.   (char *)NULL, (char *)NULL, (char *)NULL, input_line, EA_MAX_INPUT, 0
  48. };
  49.  
  50. static void echo_area_initialize_node ();
  51. static void push_echo_area (), pop_echo_area ();
  52. static int echo_area_stack_depth (), echo_area_stack_contains_completions_p ();
  53.  
  54. static void ea_kill_text ();
  55.  
  56. /* Non-zero means we force the user to complete. */
  57. static int echo_area_must_complete_p = 0;
  58. static int completions_window_p ();
  59.  
  60. /* If non-null, this is a window which was specifically created to display
  61.    possible completions output.  We remember it so we can delete it when
  62.    appropriate. */
  63. static WINDOW *echo_area_completions_window = (WINDOW *)NULL;
  64.  
  65. /* Variables which keep track of the window which was active prior to
  66.    entering the echo area. */
  67. static WINDOW *calling_window = (WINDOW *)NULL;
  68. static NODE *calling_window_node = (NODE *)NULL;
  69. static long calling_window_point = 0;
  70. static long calling_window_pagetop = 0;
  71.  
  72. /* Remember the node and pertinent variables of the calling window. */
  73. static void
  74. remember_calling_window (window)
  75.      WINDOW *window;
  76. {
  77.   /* Only do this if the calling window is not the completions window, or,
  78.      if it is the completions window and there is no other window. */
  79.   if (!completions_window_p (window) ||
  80.       ((window == windows) && !(window->next)))
  81.     {
  82.       calling_window = window;
  83.       calling_window_node = window->node;
  84.       calling_window_point = window->point;
  85.       calling_window_pagetop = window->pagetop;
  86.     }
  87. }
  88.  
  89. /* Restore the caller's window so that it shows the node that it was showing
  90.    on entry to info_read_xxx_echo_area (). */
  91. static void
  92. restore_calling_window ()
  93. {
  94.   register WINDOW *win, *compwin = (WINDOW *)NULL;
  95.  
  96.   /* If the calling window is still visible, and it is the window that
  97.      we used for completions output, then restore the calling window. */
  98.   for (win = windows; win; win = win->next)
  99.     {
  100.       if (completions_window_p (win))
  101.     compwin = win;
  102.  
  103.       if (win == calling_window && win == compwin)
  104.     {
  105.       window_set_node_of_window (calling_window, calling_window_node);
  106.       calling_window->point = calling_window_point;
  107.       calling_window->pagetop = calling_window_pagetop;
  108.       compwin = (WINDOW *)NULL;
  109.       break;
  110.     }
  111.     }
  112.  
  113.   /* Delete the completions window if it is still present, it isn't the
  114.      last window on the screen, and there aren't any prior echo area reads
  115.      pending which created a completions window. */
  116.   if (compwin)
  117.     {
  118.       if ((compwin != windows || windows->next) &&
  119.       !echo_area_stack_contains_completions_p ())
  120.     {
  121.       WINDOW *next;
  122.       int pagetop, start, end, amount;
  123.  
  124.       next = compwin->next;
  125.       if (next)
  126.         {
  127.           start = next->first_row;
  128.           end = start + next->height;
  129.           amount = - (compwin->height + 1);
  130.           pagetop = next->pagetop;
  131.         }
  132.  
  133.       info_delete_window_internal (compwin);
  134.  
  135.       /* This is not necessary because info_delete_window_internal ()
  136.          calls echo_area_inform_of_deleted_window (), which does the
  137.          right thing. */
  138. #if defined (UNNECESSARY)
  139.       echo_area_completions_window = (WINDOW *)NULL;
  140. #endif /* UNNECESSARY */
  141.  
  142.       if (next)
  143.         {
  144.           display_scroll_display (start, end, amount);
  145.           next->pagetop = pagetop;
  146.           display_update_display (windows);
  147.         }
  148.     }
  149.     }
  150. }
  151.  
  152. /* Set up a new input line with PROMPT. */
  153. static void
  154. initialize_input_line (prompt)
  155.      char *prompt;
  156. {
  157.   input_line_prompt = prompt;
  158.   if (prompt)
  159.     strcpy (input_line, prompt);
  160.   else
  161.     input_line[0] = '\0';
  162.  
  163.   input_line_beg = input_line_end = input_line_point = strlen (prompt);
  164. }
  165.  
  166. static char *
  167. echo_area_after_read ()
  168. {
  169.   char *return_value;
  170.  
  171.   if (info_aborted_echo_area)
  172.     {
  173.       info_aborted_echo_area = 0;
  174.       return_value = (char *)NULL;
  175.     }
  176.   else
  177.     {
  178.       if (input_line_beg == input_line_end)
  179.     return_value = savestring ("");
  180.       else
  181.     {
  182.       int line_len = input_line_end - input_line_beg;
  183.       return_value = (char *) xmalloc (1 + line_len);
  184.       strncpy (return_value, &input_line[input_line_beg], line_len);
  185.       return_value[line_len] = '\0';
  186.     }
  187.     }
  188.   return (return_value);
  189. }
  190.  
  191. /* Read a line of text in the echo area.  Return a malloc ()'ed string,
  192.    or NULL if the user aborted out of this read.  WINDOW is the currently
  193.    active window, so that we can restore it when we need to.  PROMPT, if
  194.    non-null, is a prompt to print before reading the line. */
  195. char *
  196. info_read_in_echo_area (window, prompt)
  197.      WINDOW *window;
  198.      char *prompt;
  199. {
  200.   char *line;
  201.  
  202.   /* If the echo area is already active, remember the current state. */
  203.   if (echo_area_is_active)
  204.     push_echo_area ();
  205.  
  206.   /* Initialize our local variables. */
  207.   initialize_input_line (prompt);
  208.  
  209.   /* Initialize the echo area for the first (but maybe not the last) time. */
  210.   echo_area_initialize_node ();
  211.  
  212.   /* Save away the original node of this window, and the window itself,
  213.      so echo area commands can temporarily use this window. */
  214.   remember_calling_window (window);
  215.  
  216.   /* Let the rest of Info know that the echo area is active. */
  217.   echo_area_is_active++;
  218.   active_window = the_echo_area;
  219.  
  220.   /* Read characters in the echo area. */
  221.   info_read_and_dispatch ();
  222.  
  223.   echo_area_is_active--;
  224.  
  225.   /* Restore the original active window and show point in it. */
  226.   active_window = calling_window;
  227.   restore_calling_window ();
  228.   display_cursor_at_point (active_window);
  229.   fflush (stdout);
  230.  
  231.   /* Get the value of the line. */
  232.   line = echo_area_after_read ();
  233.  
  234.   /* If there is a previous loop waiting for us, restore it now. */
  235.   if (echo_area_is_active)
  236.     pop_echo_area ();
  237.  
  238.   /* Return the results to the caller. */
  239.   return (line);
  240. }
  241.  
  242. /* (re) Initialize the echo area node. */
  243. static void
  244. echo_area_initialize_node ()
  245. {
  246.   register int i;
  247.  
  248.   for (i = input_line_end; i < sizeof (input_line); i++)
  249.     input_line[i] = ' ';
  250.  
  251.   input_line[i - 1] = '\n';
  252.   window_set_node_of_window (the_echo_area, &input_line_node);
  253.   input_line[input_line_end] = '\n';
  254. }
  255.  
  256. /* Prepare to read characters in the echo area.  This can initialize the
  257.    echo area node, but its primary purpose is to side effect the input
  258.    line buffer contents. */
  259. void
  260. echo_area_prep_read ()
  261. {
  262.   if (the_echo_area->node != &input_line_node)
  263.     echo_area_initialize_node ();
  264.  
  265.   the_echo_area->point = input_line_point;
  266.   input_line[input_line_end] = '\n';
  267.   display_update_one_window (the_echo_area);
  268.   display_cursor_at_point (active_window);
  269. }
  270.  
  271.  
  272. /* **************************************************************** */
  273. /*                                    */
  274. /*             Echo Area Movement Commands            */
  275. /*                                    */
  276. /* **************************************************************** */
  277.  
  278. DECLARE_INFO_COMMAND (ea_forward, "Move forward a character")
  279. {
  280.   if (count < 0)
  281.     ea_backward (window, -count, key);
  282.   else
  283.     {
  284.       input_line_point += count;
  285.       if (input_line_point > input_line_end)
  286.     input_line_point = input_line_end;
  287.     }
  288. }
  289.  
  290. DECLARE_INFO_COMMAND (ea_backward, "Move backward a character")
  291. {
  292.   if (count < 0)
  293.     ea_forward (window, -count, key);
  294.   else
  295.     {
  296.       input_line_point -= count;
  297.       if (input_line_point < input_line_beg)
  298.     input_line_point = input_line_beg;
  299.     }
  300. }
  301.  
  302. DECLARE_INFO_COMMAND (ea_beg_of_line, "Move to the start of this line")
  303. {
  304.   input_line_point = input_line_beg;
  305. }
  306.  
  307. DECLARE_INFO_COMMAND (ea_end_of_line, "Move to the end of this line")
  308. {
  309.   input_line_point = input_line_end;
  310. }
  311.  
  312. #define alphabetic(c) (islower (c) || isupper (c) || isdigit (c))
  313.  
  314. /* Move forward a word in the input line. */
  315. DECLARE_INFO_COMMAND (ea_forward_word, "Move forward a word")
  316. {
  317.   int c;
  318.  
  319.   if (count < 0)
  320.     ea_backward_word (window, -count, key);
  321.   else
  322.     {
  323.       while (count--)
  324.     {
  325.       if (input_line_point == input_line_end)
  326.         return;
  327.  
  328.       /* If we are not in a word, move forward until we are in one.
  329.          Then, move forward until we hit a non-alphabetic character. */
  330.       c = input_line[input_line_point];
  331.  
  332.       if (!alphabetic (c))
  333.         {
  334.           while (++input_line_point < input_line_end)
  335.         {
  336.           c = input_line[input_line_point];
  337.           if (alphabetic (c))
  338.             break;
  339.         }
  340.         }
  341.  
  342.       if (input_line_point == input_line_end)
  343.         return;
  344.  
  345.       while (++input_line_point < input_line_end)
  346.         {
  347.           c = input_line[input_line_point];
  348.           if (!alphabetic (c))
  349.         break;
  350.         }
  351.     }
  352.     }
  353. }
  354.  
  355. DECLARE_INFO_COMMAND (ea_backward_word, "Move backward a word")
  356. {
  357.   int c;
  358.  
  359.   if (count < 0)
  360.     ea_forward_word (window, -count, key);
  361.   else
  362.     {
  363.       while (count--)
  364.     {
  365.       if (input_line_point == input_line_beg)
  366.         return;
  367.  
  368.       /* Like ea_forward_word (), except that we look at the
  369.          characters just before point. */
  370.  
  371.       c = input_line[input_line_point - 1];
  372.  
  373.       if (!alphabetic (c))
  374.         {
  375.           while (--input_line_point)
  376.         {
  377.           c = input_line[input_line_point - 1];
  378.           if (alphabetic (c))
  379.             break;
  380.         }
  381.         }
  382.  
  383.       while (input_line_point != input_line_beg)
  384.         {
  385.           c = input_line[input_line_point - 1];
  386.           if (!alphabetic (c))
  387.         break;
  388.           else
  389.         --input_line_point;
  390.         }
  391.     }
  392.     }
  393. }
  394.  
  395. DECLARE_INFO_COMMAND (ea_delete, "Delete the character under the cursor")
  396. {
  397.   register int i;
  398.  
  399.   if (count < 0)
  400.     ea_rubout (window, -count, key);
  401.   else
  402.     {
  403.       if (input_line_point == input_line_end)
  404.     return;
  405.  
  406.       if (info_explicit_arg || count > 1)
  407.     {
  408.       int orig_point;
  409.  
  410.       orig_point = input_line_point;
  411.       ea_forward (window, count, key);
  412.       ea_kill_text (orig_point, input_line_point);
  413.       input_line_point = orig_point;
  414.     }
  415.       else
  416.     {
  417.       for (i = input_line_point; i < input_line_end; i++)
  418.         input_line[i] = input_line[i + 1];
  419.  
  420.       input_line_end--;
  421.     }
  422.     }
  423. }
  424.  
  425. DECLARE_INFO_COMMAND (ea_rubout, "Delete the character behind the cursor")
  426. {
  427.   if (count < 0)
  428.     ea_delete (window, -count, key);
  429.   else
  430.     {
  431.       int start;
  432.  
  433.       if (input_line_point == input_line_beg)
  434.     return;
  435.  
  436.       start = input_line_point;
  437.       ea_backward (window, count, key);
  438.  
  439.       if (info_explicit_arg || count > 1)
  440.     ea_kill_text (start, input_line_point);
  441.       else
  442.     ea_delete (window, count, key);
  443.     }
  444. }
  445.  
  446. DECLARE_INFO_COMMAND (ea_abort, "Cancel or quit operation")
  447. {
  448.   /* If any text, just discard it, and restore the calling window's node.
  449.      If no text, quit. */
  450.   if (input_line_end != input_line_beg)
  451.     {
  452.       terminal_ring_bell ();
  453.       input_line_end = input_line_point = input_line_beg;
  454.       if (calling_window->node != calling_window_node)
  455.     restore_calling_window ();
  456.     }
  457.   else
  458.     info_aborted_echo_area = 1;
  459. }
  460.  
  461. DECLARE_INFO_COMMAND (ea_newline, "Accept (or force completion of) this line")
  462. {
  463.   /* Stub does nothing.  Simply here to see if it has been executed. */
  464. }
  465.  
  466. DECLARE_INFO_COMMAND (ea_quoted_insert, "Insert next character verbatim")
  467. {
  468.   unsigned char character;
  469.  
  470.   character = info_get_another_input_char ();
  471.   ea_insert (window, count, character);
  472. }
  473.  
  474. DECLARE_INFO_COMMAND (ea_insert, "Insert this character")
  475. {
  476.   register int i;
  477.  
  478.   if ((input_line_end + 1) == EA_MAX_INPUT)
  479.     {
  480.       terminal_ring_bell ();
  481.       return;
  482.     }
  483.  
  484.   for (i = input_line_end + 1; i != input_line_point; i--)
  485.     input_line[i] = input_line[i - 1];
  486.  
  487.   input_line[input_line_point] = key;
  488.   input_line_point++;
  489.   input_line_end++;
  490. }
  491.  
  492. DECLARE_INFO_COMMAND (ea_tab_insert, "Insert a TAB character")
  493. {
  494.   ea_insert (window, count, '\t');
  495. }
  496.  
  497. /* Transpose the characters at point.  If point is at the end of the line,
  498.    then transpose the characters before point. */
  499. DECLARE_INFO_COMMAND (ea_transpose_chars, "Transpose characters at point")
  500. {
  501.   /* Handle conditions that would make it impossible to transpose
  502.      characters. */
  503.   if (!count || !input_line_point || (input_line_end - input_line_beg) < 2)
  504.     return;
  505.  
  506.   while (count)
  507.     {
  508.       int t;
  509.       if (input_line_point == input_line_end)
  510.     {
  511.       t = input_line[input_line_point - 1];
  512.  
  513.       input_line[input_line_point - 1] = input_line[input_line_point - 2];
  514.       input_line[input_line_point - 2] = t;
  515.     }
  516.       else
  517.     {
  518.       t = input_line[input_line_point];
  519.  
  520.       input_line[input_line_point] = input_line[input_line_point - 1];
  521.       input_line[input_line_point - 1] = t;
  522.  
  523.       if (count < 0 && input_line_point != input_line_beg)
  524.         input_line_point--;
  525.       else
  526.         input_line_point++;
  527.     }
  528.  
  529.       if (count < 0)
  530.     count++;
  531.       else
  532.     count--;
  533.     }
  534. }
  535.  
  536. /* **************************************************************** */
  537. /*                                    */
  538. /*             Echo Area Killing and Yanking            */
  539. /*                                    */
  540. /* **************************************************************** */
  541.  
  542. static char **kill_ring = (char **)NULL;
  543. static int kill_ring_index = 0;    /* Number of kills appearing in KILL_RING. */
  544. static int kill_ring_slots = 0;    /* Number of slots allocated to KILL_RING. */
  545. static int kill_ring_loc = 0;    /* Location of current yank pointer. */
  546.  
  547. /* The largest number of kills that we remember at one time. */
  548. static int max_retained_kills = 15;
  549.  
  550. DECLARE_INFO_COMMAND (ea_yank, "Yank back the contents of the last kill")
  551. {
  552.   register int i;
  553.   register char *text;
  554.  
  555.   if (!kill_ring_index)
  556.     {
  557.       inform_in_echo_area ("Kill ring is empty");
  558.       return;
  559.     }
  560.  
  561.   text = kill_ring[kill_ring_loc];
  562.  
  563.   for (i = 0; text[i]; i++)
  564.     ea_insert (window, 1, text[i]);
  565. }
  566.  
  567. /* If the last command was yank, or yank_pop, and the text just before
  568.    point is identical to the current kill item, then delete that text
  569.    from the line, rotate the index down, and yank back some other text. */
  570. DECLARE_INFO_COMMAND (ea_yank_pop, "Yank back a previous kill")
  571. {
  572.   register int len;
  573.  
  574.   if (((ea_last_executed_command != ea_yank) &&
  575.        (ea_last_executed_command != ea_yank_pop)) ||
  576.       (kill_ring_index == 0))
  577.     return;
  578.  
  579.   len = strlen (kill_ring[kill_ring_loc]);
  580.  
  581.   /* Delete the last yanked item from the line. */
  582.   {
  583.     register int i, counter;
  584.  
  585.     counter = input_line_end - input_line_point;
  586.     
  587.     for (i = input_line_point - len; counter; i++, counter--)
  588.       input_line[i] = input_line[i + len];
  589.  
  590.     input_line_end -= len;
  591.     input_line_point -= len;
  592.   }
  593.  
  594.   /* Get a previous kill, and yank that. */
  595.   kill_ring_loc--;
  596.   if (kill_ring_loc < 0)
  597.     kill_ring_loc = kill_ring_index - 1;
  598.  
  599.   ea_yank (window, count, key);
  600. }
  601.  
  602. /* Delete the text from point to end of line. */
  603. DECLARE_INFO_COMMAND (ea_kill_line, "Kill to the end of the line")
  604. {
  605.   if (count < 0)
  606.     {
  607.       ea_kill_text (input_line_point, input_line_beg);
  608.       input_line_point = input_line_beg;
  609.     }
  610.   else
  611.     ea_kill_text (input_line_point, input_line_end);
  612. }
  613.  
  614. /* Delete the text from point to beg of line. */
  615. DECLARE_INFO_COMMAND (ea_backward_kill_line,
  616.               "Kill to the beginning of the line")
  617. {
  618.   if (count < 0)
  619.     ea_kill_text (input_line_point, input_line_end);
  620.   else
  621.     {
  622.       ea_kill_text (input_line_point, input_line_beg);
  623.       input_line_point = input_line_beg;
  624.     }
  625. }
  626.  
  627. /* Delete from point to the end of the current word. */
  628. DECLARE_INFO_COMMAND (ea_kill_word, "Kill the word following the cursor")
  629. {
  630.   int orig_point = input_line_point;
  631.  
  632.   if (count < 0)
  633.     ea_backward_kill_word (window, -count, key);
  634.   else
  635.     {
  636.       ea_forward_word (window, count, key);
  637.  
  638.       if (input_line_point != orig_point)
  639.     ea_kill_text (orig_point, input_line_point);
  640.  
  641.       input_line_point = orig_point;
  642.     }
  643. }
  644.  
  645. /* Delete from point to the start of the current word. */
  646. DECLARE_INFO_COMMAND (ea_backward_kill_word,
  647.               "Kill the word preceding the cursor")
  648. {
  649.   int orig_point = input_line_point;
  650.  
  651.   if (count < 0)
  652.     ea_kill_word (window, -count, key);
  653.   else
  654.     {
  655.       ea_backward_word (window, count, key);
  656.  
  657.       if (input_line_point != orig_point)
  658.     ea_kill_text (orig_point, input_line_point);
  659.     }
  660. }
  661.  
  662. /* The way to kill something.  This appends or prepends to the last
  663.    kill, if the last command was a kill command.  If FROM is less
  664.    than TO, then the killed text is appended to the most recent kill,
  665.    otherwise it is prepended.  If the last command was not a kill command,
  666.    then a new slot is made for this kill. */
  667. static void
  668. ea_kill_text (from, to)
  669.      int from, to;
  670. {
  671.   register int i, counter, distance;
  672.   int killing_backwards, slot;
  673.   char *killed_text;
  674.  
  675.   killing_backwards = (from > to);
  676.  
  677.   /* If killing backwards, reverse the values of FROM and TO. */
  678.   if (killing_backwards)
  679.     {
  680.       int temp = from;
  681.       from = to;
  682.       to = temp;
  683.     }
  684.  
  685.   /* Remember the text that we are about to delete. */
  686.   distance = to - from;
  687.   killed_text = (char *)xmalloc (1 + distance);
  688.   strncpy (killed_text, &input_line[from], distance);
  689.   killed_text[distance] = '\0';
  690.  
  691.   /* Actually delete the text from the line. */
  692.   counter = input_line_end - to;
  693.  
  694.   for (i = from; counter; i++, counter--)
  695.     input_line[i] = input_line[i + distance];
  696.  
  697.   input_line_end -= distance;
  698.  
  699.   /* If the last command was a kill, append or prepend the killed text to
  700.      the last command's killed text. */
  701.   if (echo_area_last_command_was_kill)
  702.     {
  703.       char *old, *new;
  704.  
  705.       slot = kill_ring_loc;
  706.       old = kill_ring[slot];
  707.       new = (char *)xmalloc (1 + strlen (old) + strlen (killed_text));
  708.  
  709.       if (killing_backwards)
  710.     {
  711.       /* Prepend TEXT to current kill. */
  712.       strcpy (new, killed_text);
  713.       strcat (new, old);
  714.     }
  715.       else
  716.     {
  717.       /* Append TEXT to current kill. */
  718.       strcpy (new, old);
  719.       strcat (new, killed_text);
  720.     }
  721.  
  722.       free (old);
  723.       free (killed_text);
  724.       kill_ring[slot] = new;
  725.     }
  726.   else
  727.     {
  728.       /* Try to store the kill in a new slot, unless that would cause there
  729.      to be too many remembered kills. */
  730.       slot = kill_ring_index;
  731.  
  732.       if (slot == max_retained_kills)
  733.     slot = 0;
  734.  
  735.       if (slot + 1 > kill_ring_slots)
  736.     kill_ring = (char **) xrealloc
  737.       (kill_ring,
  738.        (kill_ring_slots += max_retained_kills) * sizeof (char *));
  739.  
  740.       if (slot != kill_ring_index)
  741.     free (kill_ring[slot]);
  742.       else
  743.     kill_ring_index++;
  744.  
  745.       kill_ring[slot] = killed_text;
  746.  
  747.       kill_ring_loc = slot;
  748.     }
  749.  
  750.   /* Notice that the last command was a kill. */
  751.   echo_area_last_command_was_kill++;
  752. }
  753.  
  754. /* **************************************************************** */
  755. /*                                    */
  756. /*            Echo Area Completion                */
  757. /*                                    */
  758. /* **************************************************************** */
  759.  
  760. /* Pointer to an array of REFERENCE to complete over. */
  761. static REFERENCE **echo_area_completion_items = (REFERENCE **)NULL;
  762.  
  763. /* Sorted array of REFERENCE * which is the possible completions found in
  764.    the variable echo_area_completion_items.  If there is only one element,
  765.    it is the only possible completion. */
  766. static REFERENCE **completions_found = (REFERENCE **)NULL;
  767. static int completions_found_index = 0;
  768. static int completions_found_slots = 0;
  769.  
  770. /* The lowest common denominator found while completing. */
  771. static REFERENCE *LCD_completion;
  772.  
  773. /* Internal functions used by the user calls. */
  774. static void build_completions (), completions_must_be_rebuilt ();
  775.  
  776. /* Variable which holds the output of completions. */
  777. static NODE *possible_completions_output_node = (NODE *)NULL;
  778.  
  779. static char *compwin_name = "*Completions*";
  780.  
  781. /* Return non-zero if WINDOW is a window used for completions output. */
  782. static int
  783. completions_window_p (window)
  784.      WINDOW *window;
  785. {
  786.   int result = 0;
  787.  
  788.   if (internal_info_node_p (window->node) &&
  789.       (strcmp (window->node->nodename, compwin_name) == 0))
  790.     result = 1;
  791.  
  792.   return (result);
  793. }
  794.  
  795. /* Workhorse for completion readers.  If FORCE is non-zero, the user cannot
  796.    exit unless the line read completes, or is empty. */
  797. char *
  798. info_read_completing_internal (window, prompt, completions, force)
  799.      WINDOW *window;
  800.      char *prompt;
  801.      REFERENCE **completions;
  802.      int force;
  803. {
  804.   char *line;
  805.  
  806.   /* If the echo area is already active, remember the current state. */
  807.   if (echo_area_is_active)
  808.     push_echo_area ();
  809.  
  810.   echo_area_must_complete_p = force;
  811.  
  812.   /* Initialize our local variables. */
  813.   initialize_input_line (prompt);
  814.  
  815.   /* Initialize the echo area for the first (but maybe not the last) time. */
  816.   echo_area_initialize_node ();
  817.  
  818.   /* Save away the original node of this window, and the window itself,
  819.      so echo area commands can temporarily use this window. */
  820.   remember_calling_window (window);
  821.  
  822.   /* Save away the list of items to complete over. */
  823.   echo_area_completion_items = completions;
  824.   completions_must_be_rebuilt ();
  825.  
  826.   active_window = the_echo_area;
  827.   echo_area_is_active++;
  828.  
  829.   /* Read characters in the echo area. */
  830.   while (1)
  831.     {
  832.       info_read_and_dispatch ();
  833.  
  834.       line = echo_area_after_read ();
  835.  
  836.       /* Force the completion to take place if the user hasn't accepted
  837.      a default or aborted, and if FORCE is active. */
  838.       if (force && line && *line && completions)
  839.     {
  840.       register int i;
  841.  
  842.       build_completions ();
  843.  
  844.       /* If there is only one completion, then make the line be that
  845.          completion. */
  846.       if (completions_found_index == 1)
  847.         {
  848.           free (line);
  849.           line = savestring (completions_found[0]->label);
  850.           break;
  851.         }
  852.  
  853.       /* If one of the completions matches exactly, then that is okay, so
  854.          return the current line. */
  855.       for (i = 0; i < completions_found_index; i++)
  856.         if (stricmp (completions_found[i]->label, line) == 0)
  857.           {
  858.         free (line);
  859.         line = savestring (completions_found[i]->label);
  860.         break;
  861.           }
  862.  
  863.       /* If no match, go back and try again. */
  864.       if (i == completions_found_index)
  865.         {
  866.           inform_in_echo_area ("Not complete");
  867.           continue;
  868.         }
  869.     }
  870.       break;
  871.     }
  872.   echo_area_is_active--;
  873.  
  874.   /* Restore the original active window and show point in it. */
  875.   active_window = calling_window;
  876.   restore_calling_window ();
  877.   display_cursor_at_point (active_window);
  878.   fflush (stdout);
  879.  
  880.   echo_area_completion_items = (REFERENCE **)NULL;
  881.   completions_must_be_rebuilt ();
  882.  
  883.   /* If there is a previous loop waiting for us, restore it now. */
  884.   if (echo_area_is_active)
  885.     pop_echo_area ();
  886.  
  887.   return (line);
  888. }
  889.   
  890. /* Read a line in the echo area with completion over COMPLETIONS. */
  891. char *
  892. info_read_completing_in_echo_area (window, prompt, completions)
  893.      WINDOW *window;
  894.      char *prompt;
  895.      REFERENCE **completions;
  896. {
  897.   return (info_read_completing_internal (window, prompt, completions, 1));
  898. }
  899.  
  900. /* Read a line in the echo area allowing completion over COMPLETIONS, but
  901.    not requiring it. */
  902. char *
  903. info_read_maybe_completing (window, prompt, completions)
  904.      WINDOW *window;
  905.      char *prompt;
  906.      REFERENCE **completions;
  907. {
  908.   return (info_read_completing_internal (window, prompt, completions, 0));
  909. }
  910.  
  911. DECLARE_INFO_COMMAND (ea_possible_completions, "List possible completions")
  912. {
  913.   if (!echo_area_completion_items)
  914.     {
  915.       ea_insert (window, count, key);
  916.       return;
  917.     }
  918.  
  919.   build_completions ();
  920.  
  921.   if (!completions_found_index)
  922.     {
  923.       terminal_ring_bell ();
  924.       inform_in_echo_area ("No completions");
  925.     }
  926.   else if ((completions_found_index == 1) && (key != '?'))
  927.     {
  928.       inform_in_echo_area ("Sole completion");
  929.     }
  930.   else
  931.     {
  932.       register int i, l;
  933.       int limit, count, max_label = 0;
  934.  
  935.       initialize_message_buffer ();
  936.       printf_to_message_buffer
  937.     ("There %s %d ", completions_found_index == 1 ? "is" : "are",
  938.      completions_found_index);
  939.       printf_to_message_buffer
  940.     ("completion%s:\n", completions_found_index == 1 ? "" : "s");
  941.  
  942.       /* Find the maximum length of a label. */
  943.       for (i = 0; i < completions_found_index; i++)
  944.     {
  945.       int len = strlen (completions_found[i]->label);
  946.       if (len > max_label)
  947.         max_label = len;
  948.     }
  949.  
  950.       max_label += 4;
  951.  
  952.       /* Find out how many columns we should print in. */
  953.       limit = calling_window->width / max_label;
  954.       if (limit != 1 && (limit * max_label == calling_window->width))
  955.     limit--;
  956.  
  957.       /* Avoid a possible floating exception.  If max_label > width then
  958.      the limit will be 0 and a divide-by-zero fault will result. */
  959.       if (limit == 0)
  960.     limit = 1;
  961.  
  962.       /* How many iterations of the printing loop? */
  963.       count = (completions_found_index + (limit - 1)) / limit;
  964.  
  965.       /* Watch out for special case.  If the number of completions is less
  966.      than LIMIT, then just do the inner printing loop. */
  967.       if (completions_found_index < limit)
  968.     count = 1;
  969.  
  970.       /* Print the sorted items, up-and-down alphabetically. */
  971.       for (i = 0; i < count; i++)
  972.     {
  973.       register int j;
  974.  
  975.       for (j = 0, l = i; j < limit; j++)
  976.         {
  977.           if (l >= completions_found_index)
  978.         break;
  979.           else
  980.         {
  981.           char *label;
  982.           int printed_length, k;
  983.  
  984.           label = completions_found[l]->label;
  985.           printed_length = strlen (label);
  986.           printf_to_message_buffer ("%s", label);
  987.  
  988.           if (j + 1 < limit)
  989.             {
  990.               for (k = 0; k < max_label - printed_length; k++)
  991.             printf_to_message_buffer (" ");
  992.             }
  993.         }
  994.           l += count;
  995.         }
  996.       printf_to_message_buffer ("\n");
  997.     }
  998.  
  999.       /* Make a new node to hold onto possible completions.  Don't destroy
  1000.      dangling pointers. */
  1001.       {
  1002.     NODE *temp;
  1003.  
  1004.     temp = message_buffer_to_node ();
  1005.     add_gcable_pointer (temp->contents);
  1006.     name_internal_node (temp, compwin_name);
  1007.     possible_completions_output_node = temp;
  1008.       }
  1009.  
  1010.       /* Find a suitable window for displaying the completions output.
  1011.      First choice is an existing window showing completions output.
  1012.      If there is only one window, and it is large, make another
  1013.      (smaller) window, and use that one.  Otherwise, use the caller's
  1014.      window. */
  1015.       {
  1016.     WINDOW *compwin;
  1017.  
  1018.     compwin = get_internal_info_window (compwin_name);
  1019.  
  1020.     if (!compwin)
  1021.       {
  1022.         /* If we can split the window to display most of the completion
  1023.            items, then do so. */
  1024.         if (calling_window->height > (count * 2))
  1025.           {
  1026.         int start, end, pagetop;
  1027.  
  1028.         active_window = calling_window;
  1029.  
  1030.         /* Perhaps we can scroll this window on redisplay. */
  1031.         start = calling_window->first_row;
  1032.         pagetop = calling_window->pagetop;
  1033.  
  1034.         compwin =
  1035.           window_make_window (possible_completions_output_node);
  1036.         active_window = the_echo_area;
  1037.         window_change_window_height
  1038.           (compwin, -(compwin->height - (count + 2)));
  1039.  
  1040.         window_adjust_pagetop (calling_window);
  1041.         remember_calling_window (calling_window);
  1042.  
  1043. #if defined (SPLIT_BEFORE_ACTIVE)
  1044.         /* If the pagetop hasn't changed, scrolling the calling
  1045.            window is a reasonable thing to do. */
  1046.         if (pagetop == calling_window->pagetop)
  1047.           {
  1048.             end = start + calling_window->height;
  1049.             display_scroll_display
  1050.               (start, end, calling_window->prev->height + 1);
  1051.           }
  1052. #else /* !SPLIT_BEFORE_ACTIVE */
  1053.         /* If the pagetop has changed, set the new pagetop here. */
  1054.         if (pagetop != calling_window->pagetop)
  1055.           {
  1056.             int newtop = calling_window->pagetop;
  1057.             calling_window->pagetop = pagetop;
  1058.             set_window_pagetop (calling_window, newtop);
  1059.           }
  1060. #endif /* !SPLIT_BEFORE_ACTIVE */
  1061.  
  1062.         echo_area_completions_window = compwin;
  1063.         remember_window_and_node (compwin, compwin->node);
  1064.           }
  1065.         else
  1066.           compwin = calling_window;
  1067.       }
  1068.  
  1069.     if (compwin->node != possible_completions_output_node)
  1070.       {
  1071.         window_set_node_of_window
  1072.           (compwin, possible_completions_output_node);
  1073.         remember_window_and_node (compwin, compwin->node);
  1074.       }
  1075.  
  1076.     display_update_display (windows);
  1077.       }
  1078.     }
  1079. }
  1080.  
  1081. DECLARE_INFO_COMMAND (ea_complete, "Insert completion")
  1082. {
  1083.   if (!echo_area_completion_items)
  1084.     {
  1085.       ea_insert (window, count, key);
  1086.       return;
  1087.     }
  1088.  
  1089.   /* If KEY is SPC, and we are not forcing completion to take place, simply
  1090.      insert the key. */
  1091.   if (!echo_area_must_complete_p && key == SPC)
  1092.     {
  1093.       ea_insert (window, count, key);
  1094.       return;
  1095.     }
  1096.  
  1097.   if (ea_last_executed_command == ea_complete)
  1098.     {
  1099.       /* If the keypress is a SPC character, and we have already tried
  1100.      completing once, and there are several completions, then check
  1101.      the batch of completions to see if any continue with a space.
  1102.      If there are some, insert the space character and continue. */
  1103.       if (key == SPC && completions_found_index > 1)
  1104.     {
  1105.       register int i, offset;
  1106.  
  1107.       offset = input_line_end - input_line_beg;
  1108.  
  1109.       for (i = 0; i < completions_found_index; i++)
  1110.         if (completions_found[i]->label[offset] == ' ')
  1111.           break;
  1112.  
  1113.       if (completions_found[i])
  1114.         ea_insert (window, 1, ' ');
  1115.       else
  1116.         {
  1117.           ea_possible_completions (window, count, key);
  1118.           return;
  1119.         }
  1120.     }
  1121.       else
  1122.     {
  1123.       ea_possible_completions (window, count, key);
  1124.       return;
  1125.     }
  1126.     }
  1127.  
  1128.   input_line_point = input_line_end;
  1129.   build_completions ();
  1130.  
  1131.   if (!completions_found_index)
  1132.     terminal_ring_bell ();
  1133.   else if (LCD_completion->label[0] == '\0')
  1134.     ea_possible_completions (window, count, key);
  1135.   else
  1136.     {
  1137.       register int i;
  1138.       input_line_point = input_line_end = input_line_beg;
  1139.       for (i = 0; LCD_completion->label[i]; i++)
  1140.     ea_insert (window, 1, LCD_completion->label[i]);
  1141.     }
  1142. }
  1143.  
  1144. /* Utility REFERENCE used to store possible LCD. */
  1145. static REFERENCE LCD_reference = { (char *)NULL, (char *)NULL, (char *)NULL };
  1146.  
  1147. static void remove_completion_duplicates ();
  1148.  
  1149. /* Variables which remember the state of the most recent call
  1150.    to build_completions (). */
  1151. static char *last_completion_request = (char *)NULL;
  1152. static REFERENCE **last_completion_items = (REFERENCE **)NULL;
  1153.  
  1154. /* How to tell the completion builder to reset internal state. */
  1155. static void
  1156. completions_must_be_rebuilt ()
  1157. {
  1158.   maybe_free (last_completion_request);
  1159.   last_completion_request = (char *)NULL;
  1160.   last_completion_items = (REFERENCE **)NULL;
  1161. }
  1162.  
  1163. /* Build a list of possible completions from echo_area_completion_items,
  1164.    and the contents of input_line. */
  1165. static void
  1166. build_completions ()
  1167. {
  1168.   register int i, len;
  1169.   register REFERENCE *entry;
  1170.   char *request;
  1171.   int informed_of_lengthy_job = 0;
  1172.  
  1173.   /* If there are no items to complete over, exit immediately. */
  1174.   if (!echo_area_completion_items)
  1175.     {
  1176.       completions_found_index = 0;
  1177.       LCD_completion = (REFERENCE *)NULL;
  1178.       return;
  1179.     }
  1180.  
  1181.   /* Check to see if this call to build completions is the same as the last
  1182.      call to build completions. */
  1183.   len = input_line_end - input_line_beg;
  1184.   request = (char *)xmalloc (1 + len);
  1185.   strncpy (request, &input_line[input_line_beg], len);
  1186.   request[len] = '\0';
  1187.  
  1188.   if (last_completion_request && last_completion_items &&
  1189.       last_completion_items == echo_area_completion_items &&
  1190.       (strcmp (last_completion_request, request) == 0))
  1191.     {
  1192.       free (request);
  1193.       return;
  1194.     }
  1195.  
  1196.   maybe_free (last_completion_request);
  1197.   last_completion_request = request;
  1198.   last_completion_items = echo_area_completion_items;
  1199.  
  1200.   /* Always start at the beginning of the list. */
  1201.   completions_found_index = 0;
  1202.   LCD_completion = (REFERENCE *)NULL;
  1203.  
  1204.   for (i = 0; entry = echo_area_completion_items[i]; i++)
  1205.     {
  1206.       if (strnicmp (request, entry->label, len) == 0)
  1207.     add_pointer_to_array (entry, completions_found_index,
  1208.                   completions_found, completions_found_slots,
  1209.                   20, REFERENCE *);
  1210.  
  1211.       if (!informed_of_lengthy_job && completions_found_index > 100)
  1212.     {
  1213.       informed_of_lengthy_job = 1;
  1214.       window_message_in_echo_area ("Building completions...");
  1215.     }
  1216.     }
  1217.  
  1218.   if (!completions_found_index)
  1219.     return;
  1220.  
  1221.   /* Sort and prune duplicate entries from the completions array. */
  1222.   remove_completion_duplicates ();
  1223.  
  1224.   /* If there is only one completion, just return that. */
  1225.   if (completions_found_index == 1)
  1226.     {
  1227.       LCD_completion = completions_found[0];
  1228.       return;
  1229.     }
  1230.  
  1231.   /* Find the least common denominator. */
  1232.   {
  1233.     long shortest = 100000;
  1234.  
  1235.     for (i = 1; i < completions_found_index; i++)
  1236.       {
  1237.     register int j;
  1238.     int c1, c2;
  1239.  
  1240.     for (j = 0;
  1241.          (c1 = info_tolower (completions_found[i - 1]->label[j])) &&
  1242.          (c2 = info_tolower (completions_found[i]->label[j]));
  1243.          j++)
  1244.       if (c1 != c2)
  1245.         break;
  1246.  
  1247.     if (shortest > j)
  1248.       shortest = j;
  1249.       }
  1250.  
  1251.     maybe_free (LCD_reference.label);
  1252.     LCD_reference.label = (char *)xmalloc (1 + shortest);
  1253.     strncpy (LCD_reference.label, completions_found[0]->label, shortest);
  1254.     LCD_reference.label[shortest] = '\0';
  1255.     LCD_completion = &LCD_reference;
  1256.   }
  1257.  
  1258.   if (informed_of_lengthy_job)
  1259.     echo_area_initialize_node ();
  1260. }
  1261.  
  1262. /* Function called by qsort. */
  1263. static int
  1264. compare_references (entry1, entry2)
  1265.      REFERENCE **entry1, **entry2;
  1266. {
  1267.   return (stricmp ((*entry1)->label, (*entry2)->label));
  1268. }
  1269.  
  1270. /* Prune duplicate entries from COMPLETIONS_FOUND. */
  1271. static void
  1272. remove_completion_duplicates ()
  1273. {
  1274.   register int i, j;
  1275.   REFERENCE **temp;
  1276.   int newlen;
  1277.  
  1278.   if (!completions_found_index)
  1279.     return;
  1280.  
  1281.   /* Sort the items. */
  1282.   qsort (completions_found, completions_found_index, sizeof (REFERENCE *),
  1283.      compare_references);
  1284.  
  1285.   for (i = 0, newlen = 1; i < completions_found_index - 1; i++)
  1286.     {
  1287.       if (strcmp (completions_found[i]->label,
  1288.           completions_found[i + 1]->label) == 0)
  1289.     completions_found[i] = (REFERENCE *)NULL;
  1290.       else
  1291.     newlen++;
  1292.     }
  1293.  
  1294.   /* We have marked all the dead slots.  It is faster to copy the live slots
  1295.      twice than to prune the dead slots one by one. */
  1296.   temp = (REFERENCE **)xmalloc ((1 + newlen) * sizeof (REFERENCE *));
  1297.   for (i = 0, j = 0; i < completions_found_index; i++)
  1298.     if (completions_found[i])
  1299.       temp[j++] = completions_found[i];
  1300.  
  1301.   for (i = 0; i < newlen; i++)
  1302.     completions_found[i] = temp[i];
  1303.  
  1304.   completions_found[i] = (REFERENCE *)NULL;
  1305.   completions_found_index = newlen;
  1306.   free (temp);
  1307. }
  1308.  
  1309. /* Scroll the "other" window.  If there is a window showing completions, scroll
  1310.    that one, otherwise scroll the window which was active on entering the read
  1311.    function. */
  1312. DECLARE_INFO_COMMAND (ea_scroll_completions_window, "Scroll the completions window")
  1313. {
  1314.   WINDOW *compwin;
  1315.   int old_pagetop;
  1316.  
  1317.   compwin = get_internal_info_window (compwin_name);
  1318.  
  1319.   if (!compwin)
  1320.     compwin = calling_window;
  1321.  
  1322.   old_pagetop = compwin->pagetop;
  1323.  
  1324.   /* Let info_scroll_forward () do the work, and print any messages that
  1325.      need to be displayed. */
  1326.   info_scroll_forward (compwin, count, key);
  1327. }
  1328.  
  1329. /* Function which gets called when an Info window is deleted while the
  1330.    echo area is active.  WINDOW is the window which has just been deleted. */
  1331. void
  1332. echo_area_inform_of_deleted_window (window)
  1333.      WINDOW *window;
  1334. {
  1335.   /* If this is the calling_window, forget what we remembered about it. */
  1336.   if (window == calling_window)
  1337.     {
  1338.       if (active_window != the_echo_area)
  1339.     remember_calling_window (active_window);
  1340.       else
  1341.     remember_calling_window (windows);
  1342.     }
  1343.  
  1344.   /* If this window was the echo_area_completions_window, then notice that
  1345.      the window has been deleted. */
  1346.   if (window == echo_area_completions_window)
  1347.     echo_area_completions_window = (WINDOW *)NULL;
  1348. }
  1349.  
  1350. /* **************************************************************** */
  1351. /*                                    */
  1352. /*           Pushing and Popping the Echo Area            */
  1353. /*                                    */
  1354. /* **************************************************************** */
  1355.  
  1356. /* Push and Pop the echo area. */
  1357. typedef struct {
  1358.   char *line;
  1359.   char *prompt;
  1360.   REFERENCE **comp_items;
  1361.   int point, beg, end;
  1362.   int must_complete;
  1363.   NODE node;
  1364.   WINDOW *compwin;
  1365. } PUSHED_EA;
  1366.  
  1367. static PUSHED_EA **pushed_echo_areas = (PUSHED_EA **)NULL;
  1368. static int pushed_echo_areas_index = 0;
  1369. static int pushed_echo_areas_slots = 0;
  1370.  
  1371. /* Pushing the echo_area has a side effect of zeroing the completion_items. */
  1372. static void
  1373. push_echo_area ()
  1374. {
  1375.   PUSHED_EA *pushed;
  1376.  
  1377.   pushed = (PUSHED_EA *)xmalloc (sizeof (PUSHED_EA));
  1378.   pushed->line = savestring (input_line);
  1379.   pushed->prompt = input_line_prompt;
  1380.   pushed->point = input_line_point;
  1381.   pushed->beg = input_line_beg;
  1382.   pushed->end = input_line_end;
  1383.   pushed->node = input_line_node;
  1384.   pushed->comp_items = echo_area_completion_items;
  1385.   pushed->must_complete = echo_area_must_complete_p;
  1386.   pushed->compwin = echo_area_completions_window;
  1387.  
  1388.   add_pointer_to_array (pushed, pushed_echo_areas_index, pushed_echo_areas,
  1389.             pushed_echo_areas_slots, 4, PUSHED_EA *);
  1390.  
  1391.   echo_area_completion_items = (REFERENCE **)NULL;
  1392. }
  1393.  
  1394. static void
  1395. pop_echo_area ()
  1396. {
  1397.   PUSHED_EA *popped;
  1398.  
  1399.   popped = pushed_echo_areas[--pushed_echo_areas_index];
  1400.  
  1401.   strcpy (input_line, popped->line);
  1402.   free (popped->line);
  1403.   input_line_prompt = popped->prompt;
  1404.   input_line_point = popped->point;
  1405.   input_line_beg = popped->beg;
  1406.   input_line_end = popped->end;
  1407.   input_line_node = popped->node;
  1408.   echo_area_completion_items = popped->comp_items;
  1409.   echo_area_must_complete_p = popped->must_complete;
  1410.   echo_area_completions_window = popped->compwin;
  1411.   completions_must_be_rebuilt ();
  1412.  
  1413.   /* If the completion window no longer exists, forget about it. */
  1414.   if (echo_area_completions_window)
  1415.     {
  1416.       register WINDOW *win;
  1417.  
  1418.       for (win = windows; win; win = win->next)
  1419.     if (echo_area_completions_window == win)
  1420.       break;
  1421.  
  1422.       /* If the window wasn't found, then it has already been deleted. */
  1423.       if (!win)
  1424.     echo_area_completions_window = (WINDOW *)NULL;
  1425.     }
  1426.  
  1427.   free (popped);
  1428. }
  1429.  
  1430. static int
  1431. echo_area_stack_depth ()
  1432. {
  1433.   return (pushed_echo_areas_index);
  1434. }
  1435.  
  1436. /* Returns non-zero if any of the prior stacked calls to read in the echo
  1437.    area produced a completions window. */
  1438. static int
  1439. echo_area_stack_contains_completions_p ()
  1440. {
  1441.   register int i;
  1442.  
  1443.   for (i = 0; i < pushed_echo_areas_index; i++)
  1444.     if (pushed_echo_areas[i]->compwin)
  1445.       return (1);
  1446.  
  1447.   return (0);
  1448. }
  1449.  
  1450. /* **************************************************************** */
  1451. /*                                    */
  1452. /*           Error Messages While Reading in Echo Area        */
  1453. /*                                    */
  1454. /* **************************************************************** */
  1455.  
  1456. #if defined (HAVE_SYS_TIME_H)
  1457. #  include <sys/time.h>
  1458. #  define HAVE_STRUCT_TIMEVAL
  1459. #endif /* HAVE_SYS_TIME_H */
  1460.  
  1461. static void
  1462. pause_or_input ()
  1463. {
  1464. #if defined (FD_SETX)
  1465.   struct timeval timer;
  1466.   fd_set readfds;
  1467.   int ready;
  1468.  
  1469.   FD_ZERO (&readfds);
  1470.   FD_SET (fileno (stdin), &readfds);
  1471.   timer.tv_sec = 2;
  1472.   timer.tv_usec = 750;
  1473.   ready = select (1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &timer);
  1474. #endif /* FD_SET */
  1475. }
  1476.  
  1477. /* Print MESSAGE right after the end of the current line, and wait
  1478.    for input or 2.75 seconds, whichever comes first.  Then flush the
  1479.    informational message that was printed. */
  1480. void
  1481. inform_in_echo_area (message)
  1482.      char *message;
  1483. {
  1484.   register int i;
  1485.   char *text;
  1486.  
  1487.   text = savestring (message);
  1488.   for (i = 0; text[i] && text[i] != '\n'; i++);
  1489.   text[i] = '\0';
  1490.  
  1491.   echo_area_initialize_node ();
  1492.   sprintf (&input_line[input_line_end], "%s[%s]\n",
  1493.        echo_area_is_active ? " ": "", text);
  1494.   free (text);
  1495.   the_echo_area->point = input_line_point;
  1496.   display_update_one_window (the_echo_area);
  1497.   display_cursor_at_point (active_window);
  1498.   fflush (stdout);
  1499.   pause_or_input ();
  1500.   echo_area_initialize_node ();
  1501. }
  1502.