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

  1. #include "defines.h"
  2. /* window.c -- Windows in Info. */
  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 <stdio.h>
  26. #include <ctype.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include "nodes.h"
  30. #include "window.h"
  31. #include "display.h"
  32. #include "info-utils.h"
  33. #include "infomap.h"
  34.  
  35. /* The window which describes the screen. */
  36. WINDOW *the_screen = (WINDOW *)NULL;
  37.  
  38. /* The window which describes the echo area. */
  39. WINDOW *the_echo_area = (WINDOW *)NULL;
  40.  
  41. /* The list of windows in Info. */
  42. WINDOW *windows = (WINDOW *)NULL;
  43.  
  44. /* Pointer to the active window in WINDOW_LIST. */
  45. WINDOW *active_window = (WINDOW *)NULL;
  46.  
  47. /* The size of the echo area in Info.  It never changes, irregardless of the
  48.    size of the screen. */
  49. #define ECHO_AREA_HEIGHT 1
  50.  
  51. /* Macro returns the amount of space that the echo area truly requires relative
  52.    to the entire screen. */
  53. #define echo_area_required (1 + the_echo_area->height)
  54.  
  55. /* Initalize the window system by creating THE_SCREEN and THE_ECHO_AREA.
  56.    Create the first window ever.
  57.    You pass the dimensions of the total screen size. */
  58. void
  59. window_initialize_windows (width, height)
  60.      int width, height;
  61. {
  62.   the_screen = (WINDOW *)xmalloc (sizeof (WINDOW));
  63.   the_echo_area = (WINDOW *)xmalloc (sizeof (WINDOW));
  64.   windows = (WINDOW *)xmalloc (sizeof (WINDOW));
  65.   active_window = windows;
  66.  
  67.   zero_mem (the_screen, sizeof (WINDOW));
  68.   zero_mem (the_echo_area, sizeof (WINDOW));
  69.   zero_mem (active_window, sizeof (WINDOW));
  70.  
  71.   /* None of these windows has a goal column yet. */
  72.   the_echo_area->goal_column = -1;
  73.   active_window->goal_column = -1;
  74.   the_screen->goal_column = -1;
  75.  
  76.   /* The active and echo_area windows are visible.
  77.      The echo_area is permanent.
  78.      The screen is permanent. */
  79.   active_window->flags = W_WindowVisible;
  80.   the_echo_area->flags = W_WindowIsPerm | W_InhibitMode | W_WindowVisible;
  81.   the_screen->flags    = W_WindowIsPerm;
  82.  
  83.   /* The height of the echo area never changes.  It is statically set right
  84.      here, and it must be at least 1 line for display.  The size of the
  85.      initial window cannot be the same size as the screen, since the screen
  86.      includes the echo area.  So, we make the height of the initial window
  87.      equal to the screen's displayable region minus the height of the echo
  88.      area. */
  89.   the_echo_area->height = ECHO_AREA_HEIGHT;
  90.   active_window->height = the_screen->height - 1 - the_echo_area->height;
  91.   window_new_screen_size (width, height, (VFunction *)NULL);
  92.  
  93.   /* The echo area uses a different keymap than normal info windows. */
  94.   the_echo_area->keymap = echo_area_keymap;
  95.   active_window->keymap = info_keymap;
  96. }
  97.  
  98. /* Given that the size of the screen has changed to WIDTH and HEIGHT
  99.    from whatever it was before (found in the_screen->height, ->width),
  100.    change the size (and possibly location) of each window in the screen.
  101.    If a window would become too small, call the function DELETER on it,
  102.    after deleting the window from our chain of windows.  If DELETER is NULL,
  103.    nothing extra is done.  The last window can never be deleted, but it can
  104.    become invisible. */
  105.  
  106. /* If non-null, a function to call with WINDOW as argument when the function
  107.    window_new_screen_size () has deleted WINDOW. */
  108. VFunction *window_deletion_notifier = (VFunction *)NULL;
  109.  
  110. void
  111. window_new_screen_size (width, height)
  112.      int width, height;
  113. {
  114.   register WINDOW *win;
  115.   int delta_height, delta_each, delta_leftover;
  116.   int numwins;
  117.  
  118.   /* If no change, do nothing. */
  119.   if (width == the_screen->width && height == the_screen->height)
  120.     return;
  121.  
  122.   /* If the new window height is too small, make it be zero. */
  123.   if (height < (WINDOW_MIN_SIZE + the_echo_area->height))
  124.     height = 0;
  125.   if (width < 0)
  126.     width = 0;
  127.  
  128.   /* Find out how many windows will change. */
  129.   for (numwins = 0, win = windows; win; win = win->next, numwins++);
  130.  
  131.   /* See if some windows will need to be deleted.  This is the case if
  132.      the screen is getting smaller, and the available space divided by
  133.      the number of windows is less than WINDOW_MIN_SIZE.  In that case,
  134.      delete some windows and try again until there is either enough
  135.      space to divy up among the windows, or until there is only one
  136.      window left. */
  137.   while ((height - echo_area_required) / numwins <= WINDOW_MIN_SIZE)
  138.     {
  139.       /* If only one window, make the size of it be zero, and return
  140.      immediately. */
  141.       if (!windows->next)
  142.     {
  143.       windows->height = 0;
  144.       maybe_free (windows->line_starts);
  145.       windows->line_starts = (char **)NULL;
  146.       windows->line_count = 0;
  147.       break;
  148.     }
  149.  
  150.       /* If we have some temporary windows, delete one of them. */
  151.       for (win = windows; win; win = win->next)
  152.     if (win->flags & W_TempWindow)
  153.       break;
  154.  
  155.       /* Otherwise, delete the first window, and try again. */
  156.       if (!win)
  157.     win = windows;
  158.  
  159.       if (window_deletion_notifier)
  160.     (*window_deletion_notifier) (win);
  161.  
  162.       window_delete_window (win);
  163.       numwins--;
  164.     }
  165.  
  166.   /* The screen has changed height and width. */
  167.   delta_height = height - the_screen->height;    /* This is how much. */
  168.   the_screen->height = height;            /* This is the new height. */
  169.   the_screen->width = width;            /* This is the new width. */
  170.  
  171.   /* Set the start of the echo area. */
  172.   the_echo_area->first_row = height - the_echo_area->height;
  173.   the_echo_area->width = width;
  174.  
  175.   /* Check to see if the screen can really be changed this way. */
  176.   if ((!windows->next) && ((windows->height == 0) && (delta_height < 0)))
  177.     return;
  178.  
  179.   /* Divide the change in height among the available windows. */
  180.   delta_each = delta_height / numwins;
  181.   delta_leftover = delta_height - (delta_each * numwins);
  182.  
  183.   /* Change the height of each window in the chain by delta_each.  Change
  184.      the height of the last window in the chain by delta_each and by the
  185.      leftover amount of change.  Change the width of each window to be
  186.      WIDTH. */
  187.   for (win = windows; win; win = win->next)
  188.     {
  189.       if ((win->width != width) && ((win->flags & W_InhibitMode) == 0))
  190.     {
  191.       win->width = width;
  192.       maybe_free (win->modeline);
  193.       win->modeline = (char *)xmalloc (1 + width);
  194.     }
  195.  
  196.       win->height += delta_each;
  197.  
  198.       /* If the previous height of this window was zero, it was the only
  199.      window, and it was not visible.  Thus we need to compensate for
  200.      the echo_area. */
  201.       if (win->height == delta_each)
  202.     win->height -= (1 + the_echo_area->height);
  203.  
  204.       /* If this is not the first window in the chain, then change the
  205.      first row of it.  We cannot just add delta_each to the first row,
  206.      since this window's first row is the sum of the collective increases
  207.      that have gone before it.  So we just add one to the location of the
  208.      previous window's modeline. */
  209.       if (win->prev)
  210.     win->first_row = (win->prev->first_row + win->prev->height) + 1;
  211.  
  212.       /* The last window in the chain gets the extra space (or shrinkage). */
  213.       if (!win->next)
  214.     win->height += delta_leftover;
  215.  
  216.       if (win->node)
  217.     recalculate_line_starts (win);
  218.  
  219.       win->flags |= W_UpdateWindow;
  220.     }
  221.  
  222.   /* If the screen got smaller, check over the windows just shrunk to
  223.      keep them within bounds.  Some of the windows may have gotten smaller
  224.      than WINDOW_MIN_HEIGHT in which case some of the other windows are
  225.      larger than the available display space in the screen.  Because of our
  226.      intial test above, we know that there is enough space for all of the
  227.      windows. */
  228.   if ((delta_each < 0) && ((windows->height != 0) && windows->next))
  229.     {
  230.       int avail;
  231.  
  232.       avail = the_screen->height - (numwins + the_echo_area->height);
  233.       win = windows;
  234.  
  235.       while (win)
  236.     {
  237.       if ((win->height < WINDOW_MIN_HEIGHT) ||
  238.           (win->height > avail))
  239.         {
  240.           WINDOW *lastwin;
  241.  
  242.           /* Split the space among the available windows. */
  243.           delta_each = avail / numwins;
  244.           delta_leftover = avail - (delta_each * numwins);
  245.  
  246.           for (win = windows; win; win = win->next)
  247.         {
  248.           lastwin = win;
  249.           if (win->prev)
  250.             win->first_row =
  251.               (win->prev->first_row + win->prev->height) + 1;
  252.           win->height = delta_each;
  253.         }
  254.  
  255.           /* Give the leftover space (if any) to the last window. */
  256.           lastwin->height += delta_leftover;
  257.           break;
  258.         }
  259.       else
  260.         win= win->next;
  261.     }
  262.     }
  263. }
  264.  
  265. /* Make a new window showing NODE, and return that window structure.
  266.    If NODE is passed as NULL, then show the node showing in the active
  267.    window.  If the window could not be made return a NULL pointer.  The
  268.    active window is not changed.*/
  269. WINDOW *
  270. window_make_window (node)
  271.      NODE *node;
  272. {
  273.   WINDOW *window;
  274.  
  275.   if (!node)
  276.     node = active_window->node;
  277.  
  278.   /* If there isn't enough room to make another window, return now. */
  279.   if ((active_window->height / 2) < WINDOW_MIN_SIZE)
  280.     return ((WINDOW *)NULL);
  281.  
  282.   /* Make and initialize the new window.
  283.      The fudging about with -1 and +1 is because the following window in the
  284.      chain cannot start at window->height, since that is where the modeline
  285.      for the previous window is displayed.  The inverse adjustment is made
  286.      in window_delete_window (). */
  287.   window = (WINDOW *)xmalloc (sizeof (WINDOW));
  288.   window->width = the_screen->width;
  289.   window->height = (active_window->height / 2) - 1;
  290. #if defined (SPLIT_BEFORE_ACTIVE)
  291.   window->first_row = active_window->first_row;
  292. #else
  293.   window->first_row = active_window->first_row +
  294.     (active_window->height - window->height);
  295. #endif
  296.   window->keymap = info_keymap;
  297.   window->goal_column = -1;
  298.   window->modeline = (char *)xmalloc (1 + window->width);
  299.   window->line_starts = (char **)NULL;
  300.   window->flags = W_UpdateWindow | W_WindowVisible;
  301.   window_set_node_of_window (window, node);
  302.  
  303.   /* Adjust the height of the old active window. */
  304.   active_window->height -= (window->height + 1);
  305. #if defined (SPLIT_BEFORE_ACTIVE)
  306.   active_window->first_row += (window->height + 1);
  307. #endif
  308.   active_window->flags |= W_UpdateWindow;
  309.  
  310.   /* Readjust the new and old windows so that their modelines and contents
  311.      will be displayed correctly. */
  312. #if defined (NOTDEF)
  313.   /* We don't have to do this for WINDOW since window_set_node_of_window ()
  314.      already did. */
  315.   window_adjust_pagetop (window);
  316.   window_make_modeline (window);
  317. #endif /* NOTDEF */
  318.  
  319.   /* We do have to readjust the existing active window. */
  320.   window_adjust_pagetop (active_window);
  321.   window_make_modeline (active_window);
  322.  
  323. #if defined (SPLIT_BEFORE_ACTIVE)
  324.   /* This window is just before the active one.  The active window gets
  325.      bumped down one.  The active window is not changed. */
  326.   window->next = active_window;
  327.  
  328.   window->prev = active_window->prev;
  329.   active_window->prev = window;
  330.  
  331.   if (window->prev)
  332.     window->prev->next = window;
  333.   else
  334.     windows = window;
  335. #else
  336.   /* This window is just after the active one.  Which window is active is
  337.      not changed. */
  338.   window->prev = active_window;
  339.   window->next = active_window->next;
  340.   active_window->next = window;
  341.   if (window->next)
  342.     window->next->prev = window;
  343. #endif /* !SPLIT_BEFORE_ACTIVE */
  344.   return (window);
  345. }
  346.  
  347. /* These useful macros make it possible to read the code in
  348.    window_change_window_height (). */
  349. #define grow_me_shrinking_next(me, next, diff) \
  350.   do { \
  351.     me->height += diff; \
  352.     next->height -= diff; \
  353.     next->first_row += diff; \
  354.     window_adjust_pagetop (next); \
  355.   } while (0)
  356.  
  357. #define grow_me_shrinking_prev(me, prev, diff) \
  358.   do { \
  359.     me->height += diff; \
  360.     prev->height -= diff; \
  361.     me->first_row -=diff; \
  362.     window_adjust_pagetop (prev); \
  363.   } while (0)
  364.  
  365. #define shrink_me_growing_next(me, next, diff) \
  366.   do { \
  367.     me->height -= diff; \
  368.     next->height += diff; \
  369.     next->first_row -= diff; \
  370.     window_adjust_pagetop (next); \
  371.   } while (0)
  372.  
  373. #define shrink_me_growing_prev(me, prev, diff) \
  374.   do { \
  375.     me->height -= diff; \
  376.     prev->height += diff; \
  377.     me->first_row += diff; \
  378.     window_adjust_pagetop (prev); \
  379.   } while (0)
  380.  
  381. /* Change the height of WINDOW by AMOUNT.  This also automagically adjusts
  382.    the previous and next windows in the chain.  If there is only one user
  383.    window, then no change takes place. */
  384. void
  385. window_change_window_height (window, amount)
  386.      WINDOW *window;
  387.      int amount;
  388. {
  389.   register WINDOW *win, *prev, *next;
  390.  
  391.   /* If there is only one window, or if the amount of change is zero,
  392.      return immediately. */
  393.   if (!windows->next || amount == 0)
  394.     return;
  395.  
  396.   /* Find this window in our chain. */
  397.   for (win = windows; win; win = win->next)
  398.     if (win == window)
  399.       break;
  400.  
  401.   /* If the window is isolated (i.e., doesn't appear in our window list,
  402.      then quit now. */
  403.   if (!win)
  404.     return;
  405.  
  406.   /* Change the height of this window by AMOUNT, if that is possible.
  407.      It can be impossible if there isn't enough available room on the
  408.      screen, or if the resultant window would be too small. */
  409.  
  410.     prev = window->prev;
  411.     next = window->next;
  412.  
  413.   /* WINDOW decreasing in size? */
  414.   if (amount < 0)
  415.     {
  416.       int abs_amount = -amount;    /* It is easier to deal with this way. */
  417.  
  418.       /* If the resultant window would be too small, stop here. */
  419.       if ((window->height - abs_amount) < WINDOW_MIN_HEIGHT)
  420.     return;
  421.  
  422.       /* If we have two neighboring windows, choose the smaller one to get
  423.      larger. */
  424.       if (next && prev)
  425.     {
  426.       if (prev->height < next->height)
  427.         shrink_me_growing_prev (window, prev, abs_amount);
  428.       else
  429.         shrink_me_growing_next (window, next, abs_amount);
  430.     }
  431.       else if (next)
  432.     shrink_me_growing_next (window, next, abs_amount);
  433.       else
  434.     shrink_me_growing_prev (window, prev, abs_amount);
  435.     }
  436.  
  437.   /* WINDOW increasing in size? */
  438.   if (amount > 0)
  439.     {
  440.       int total_avail, next_avail = 0, prev_avail = 0;
  441.  
  442.       if (next)
  443.     next_avail = next->height - WINDOW_MIN_SIZE;
  444.  
  445.       if (prev)
  446.     prev_avail = prev->height - WINDOW_MIN_SIZE;
  447.  
  448.       total_avail = next_avail + prev_avail;
  449.  
  450.       /* If there isn't enough space available to grow this window, give up. */
  451.       if (amount > total_avail)
  452.     return;
  453.  
  454.       /* If there aren't two neighboring windows, or if one of the neighbors
  455.      is larger than the other one by at least AMOUNT, grow that one. */
  456.       if ((next && !prev) || ((next_avail - amount) >= prev_avail))
  457.     grow_me_shrinking_next (window, next, amount);
  458.       else if ((prev && !next) || ((prev_avail - amount) >= next_avail))
  459.     grow_me_shrinking_prev (window, prev, amount);
  460.       else
  461.     {
  462.       int change;
  463.  
  464.       /* This window has two neighbors.  They both must be shrunk in to
  465.          make enough space for WINDOW to grow.  Make them both the same
  466.          size. */
  467.       if (prev_avail > next_avail)
  468.         {
  469.           change = prev_avail - next_avail;
  470.           grow_me_shrinking_prev (window, prev, change);
  471.           amount -= change;
  472.         }
  473.       else
  474.         {
  475.           change = next_avail - prev_avail;
  476.           grow_me_shrinking_next (window, next, change);
  477.           amount -= change;
  478.         }
  479.  
  480.       /* Both neighbors are the same size.  Split the difference in
  481.          AMOUNT between them. */
  482.       while (amount)
  483.         {
  484.           window->height++;
  485.           amount--;
  486.  
  487.           /* Odd numbers grow next, even grow prev. */
  488.           if (amount & 1)
  489.         {
  490.           prev->height--;
  491.           window->first_row--;
  492.         }
  493.           else
  494.         {
  495.           next->height--;
  496.           next->first_row++;
  497.         }
  498.         }
  499.       window_adjust_pagetop (prev);
  500.       window_adjust_pagetop (next);
  501.     }
  502.     }
  503.   if (prev)
  504.     prev->flags |= W_UpdateWindow;
  505.  
  506.   if (next)
  507.     next->flags |= W_UpdateWindow;
  508.  
  509.   window->flags |= W_UpdateWindow;
  510.   window_adjust_pagetop (window);
  511. }
  512.  
  513. /* Tile all of the windows currently displayed in the global variable
  514.    WINDOWS.  If argument STYLE is TILE_INTERNALS, tile windows displaying
  515.    internal nodes as well, otherwise do not change the height of such
  516.    windows. */
  517. void
  518. window_tile_windows (style)
  519.      int style;
  520. {
  521.   WINDOW *win, *last_adjusted;
  522.   int numwins, avail, per_win_height, leftover;
  523.   int do_internals;
  524.  
  525.   numwins = avail = 0;
  526.   do_internals = (style == TILE_INTERNALS);
  527.  
  528.   for (win = windows; win; win = win->next)
  529.     if (do_internals || !win->node ||
  530.     (win->node->flags & N_IsInternal) == 0)
  531.       {
  532.     avail += win->height;
  533.     numwins++;
  534.       }
  535.  
  536.   if (numwins <= 1 || !the_screen->height)
  537.     return;
  538.  
  539.   /* Find the size for each window.  Divide the size of the usable portion
  540.      of the screen by the number of windows. */
  541.   per_win_height = avail / numwins;
  542.   leftover = avail - (per_win_height * numwins);
  543.  
  544.   last_adjusted = (WINDOW *)NULL;
  545.   for (win = windows; win; win = win->next)
  546.     {
  547.       if (do_internals || !win->node ||
  548.       (win->node->flags & N_IsInternal) == 0)
  549.     {
  550.       last_adjusted = win;
  551.       win->height = per_win_height;
  552.     }
  553.     }
  554.  
  555.   if (last_adjusted)
  556.     last_adjusted->height += leftover;
  557.  
  558.   /* Readjust the first_row of every window in the chain. */
  559.   for (win = windows; win; win = win->next)
  560.     {
  561.       if (win->prev)
  562.     win->first_row = win->prev->first_row + win->prev->height + 1;
  563.  
  564.       window_adjust_pagetop (win);
  565.       win->flags |= W_UpdateWindow;
  566.     }
  567. }
  568.  
  569. /* Toggle the state of line wrapping in WINDOW.  This can do a bit of fancy
  570.    redisplay. */
  571. void
  572. window_toggle_wrap (window)
  573.      WINDOW *window;
  574. {
  575.   if (window->flags & W_NoWrap)
  576.     window->flags &= ~W_NoWrap;
  577.   else
  578.     window->flags |= W_NoWrap;
  579.  
  580.   if (window != the_echo_area)
  581.     {
  582.       char **old_starts;
  583.       int old_lines, old_pagetop;
  584.  
  585.       old_starts = window->line_starts;
  586.       old_lines = window->line_count;
  587.       old_pagetop = window->pagetop;
  588.  
  589.       calculate_line_starts (window);
  590.  
  591.       /* Make sure that point appears within this window. */
  592.       window_adjust_pagetop (window);
  593.  
  594.       /* If the pagetop hasn't changed maybe we can do some scrolling now
  595.      to speed up the display.  Many of the line starts will be the same,
  596.      so scrolling here is a very good optimization.*/
  597.       if (old_pagetop == window->pagetop)
  598.     display_scroll_line_starts
  599.       (window, old_pagetop, old_starts, old_lines);
  600.       maybe_free (old_starts);
  601.     }
  602.   window->flags |= W_UpdateWindow;
  603. }
  604.  
  605. /* Set WINDOW to display NODE. */
  606. void
  607. window_set_node_of_window (window, node)
  608.      WINDOW *window;
  609.      NODE *node;
  610. {
  611.   window->node = node;
  612.   window->pagetop = 0;
  613.   window->point = 0;
  614.   recalculate_line_starts (window);
  615.   window->flags |= W_UpdateWindow;
  616.   window_adjust_pagetop (window);
  617.   window_make_modeline (window);
  618. }
  619.  
  620. /* Delete WINDOW from the list of known windows.  If this window was the
  621.    active window, make the next window in the chain be the active window.
  622.    If the active window is the next or previous window, choose that window
  623.    as the recipient of the extra space.  Otherwise, prefer the next window. */
  624. void
  625. window_delete_window (window)
  626.      WINDOW *window;
  627. {
  628.   WINDOW *next, *prev, *window_to_fix;
  629.  
  630.   next = window->next;
  631.   prev = window->prev;
  632.  
  633.   /* You cannot delete the only window or a permanent window. */
  634.   if ((!next && !prev) || (window->flags & W_WindowIsPerm))
  635.     return;
  636.  
  637.   if (next)
  638.     next->prev = prev;
  639.  
  640.   if (!prev)
  641.     windows = next;
  642.   else
  643.     prev->next = next;
  644.  
  645.   if (window->line_starts)
  646.     free (window->line_starts);
  647.  
  648.   if (window->modeline)
  649.     free (window->modeline);
  650.  
  651.   if (window == active_window)
  652.     {
  653.       /* If there isn't a next window, then there must be a previous one,
  654.      since we cannot delete the last window.  If there is a next window,
  655.      prefer to use that as the active window. */
  656.       if (next)
  657.     active_window = next;
  658.       else
  659.     active_window = prev;
  660.     }
  661.  
  662.   if (next && active_window == next)
  663.     window_to_fix = next;
  664.   else if (prev && active_window == prev)
  665.     window_to_fix = prev;
  666.   else if (next)
  667.     window_to_fix = next;
  668.   else if (prev)
  669.     window_to_fix = prev;
  670.   else
  671.     window_to_fix = windows;
  672.     
  673.   if (window_to_fix->first_row > window->first_row)
  674.     {
  675.       int diff;
  676.  
  677.       /* Try to adjust the visible part of the node so that as little
  678.      text as possible has to move. */
  679.       diff = window_to_fix->first_row - window->first_row;
  680.       window_to_fix->first_row = window->first_row;
  681.  
  682.       window_to_fix->pagetop -= diff;
  683.       if (window_to_fix->pagetop < 0)
  684.     window_to_fix->pagetop = 0;
  685.     }
  686.  
  687.   /* The `+ 1' is to offset the difference between the first_row locations.
  688.      See the code in window_make_window (). */
  689.   window_to_fix->height += window->height + 1;
  690.   window_to_fix->flags |= W_UpdateWindow;
  691.  
  692.   free (window);
  693. }
  694.  
  695. /* For every window in CHAIN, set the flags member to have FLAG set. */
  696. void
  697. window_mark_chain (chain, flag)
  698.      WINDOW *chain;
  699.      int flag;
  700. {
  701.   register WINDOW *win;
  702.  
  703.   for (win = chain; win; win = win->next)
  704.     win->flags |= flag;
  705. }
  706.  
  707. /* For every window in CHAIN, clear the flags member of FLAG. */
  708. void
  709. window_unmark_chain (chain, flag)
  710.      WINDOW *chain;
  711.      int flag;
  712. {
  713.   register WINDOW *win;
  714.  
  715.   for (win = chain; win; win = win->next)
  716.     win->flags &= ~flag;
  717. }
  718.  
  719. /* Return the number of characters it takes to display CHARACTER on the
  720.    screen at HPOS. */
  721. int
  722. character_width (character, hpos)
  723.      int character, hpos;
  724. {
  725.   int printable_limit = 127;
  726.   int width = 1;
  727.  
  728.   if (ISO_Latin_p)
  729.     printable_limit = 160;
  730.  
  731.   if (character > printable_limit)
  732.     width = 3;
  733.   else if (iscntrl (character))
  734.     {
  735.       switch (character)
  736.     {
  737.     case '\r':
  738.     case '\n':
  739.       width = the_screen->width - hpos;
  740.       break;
  741.     case '\t':
  742.       width = ((hpos + 8) & 0xf8) - hpos;
  743.       break;
  744.     default:
  745.       width = 2;
  746.     }
  747.     }
  748.   else if (character == DEL)
  749.     width = 2;
  750.  
  751.   return (width);
  752. }
  753.  
  754. /* Return the number of characters it takes to display STRING on the screen
  755.    at HPOS. */
  756. int
  757. string_width (string, hpos)
  758.      char *string;
  759.      int hpos;
  760. {
  761.   register int i, width, this_char_width;
  762.  
  763.   for (width = 0, i = 0; string[i]; i++)
  764.     {
  765.       this_char_width = character_width (string[i], hpos);
  766.       width += this_char_width;
  767.       hpos += this_char_width;
  768.     }
  769.   return (width);
  770. }
  771.  
  772. /* Quickly guess the approximate number of lines to that NODE would
  773.    take to display.  This really only counts carriage returns. */
  774. int
  775. window_physical_lines (node)
  776.      NODE *node;
  777. {
  778.   register int i, lines;
  779.   char *contents;
  780.  
  781.   if (!node)
  782.     return (0);
  783.  
  784.   contents = node->contents;
  785.   for (i = 0, lines = 1; i < node->nodelen; i++)
  786.     if (contents[i] == '\n')
  787.       lines++;
  788.  
  789.   return (lines);
  790. }
  791.  
  792. /* Calculate a list of line starts for the node belonging to WINDOW.  The line
  793.    starts are pointers to the actual text within WINDOW->NODE. */
  794. void
  795. calculate_line_starts (window)
  796.      WINDOW *window;
  797. {
  798.   register int i, hpos;
  799.   char **line_starts = (char **)NULL;
  800.   int line_starts_index = 0, line_starts_slots = 0;
  801.   int bump_index;
  802.   NODE *node;
  803.  
  804.   window->line_starts = (char **)NULL;
  805.   window->line_count = 0;
  806.   node = window->node;
  807.  
  808.   if (!node)
  809.     return;
  810.  
  811.   /* Grovel the node starting at the top, and for each line calculate the
  812.      width of the characters appearing in that line.  Add each line start
  813.      to our array. */
  814.   i = 0;
  815.   hpos = 0;
  816.   bump_index = 0;
  817.  
  818.   while (i < node->nodelen)
  819.     {
  820.       char *line = node->contents + i;
  821.       unsigned int cwidth, c;
  822.  
  823.       add_pointer_to_array (line, line_starts_index, line_starts,
  824.                 line_starts_slots, 100, char *);
  825.       if (bump_index)
  826.     {
  827.       i++;
  828.       bump_index = 0;
  829.     }
  830.  
  831.       while (1)
  832.     {
  833.       c = node->contents[i];
  834.       cwidth = character_width (c, hpos);
  835.  
  836.       /* If this character fits within this line, just do the next one. */
  837.       if ((hpos + cwidth) < window->width)
  838.         {
  839.           i++;
  840.           hpos += cwidth;
  841.           continue;
  842.         }
  843.       else
  844.         {
  845.           /* If this character would position the cursor at the start of
  846.          the next printed screen line, then do the next line. */
  847.           if (c == '\n' || c == '\r' || c == '\t')
  848.         {
  849.           i++;
  850.           hpos = 0;
  851.           break;
  852.         }
  853.           else
  854.         {
  855.           /* This character passes the window width border.  Postion
  856.              the cursor after the printed character, but remember this
  857.              line start as where this character is.  A bit tricky. */
  858.  
  859.           /* If this window doesn't wrap lines, proceed to the next
  860.              physical line here. */
  861.           if (window->flags & W_NoWrap)
  862.             {
  863.               hpos = 0;
  864.               while (i < node->nodelen && node->contents[i] != '\n')
  865.             i++;
  866.  
  867.               if (node->contents[i] == '\n')
  868.             i++;
  869.             }
  870.           else
  871.             {
  872.               hpos = the_screen->width - hpos;
  873.               bump_index++;
  874.             }
  875.           break;
  876.         }
  877.         }
  878.     }
  879.     }
  880.   window->line_starts = line_starts;
  881.   window->line_count = line_starts_index;
  882. }
  883.  
  884. /* Given WINDOW, recalculate the line starts for the node it displays. */
  885. void
  886. recalculate_line_starts (window)
  887.      WINDOW *window;
  888. {
  889.   maybe_free (window->line_starts);
  890.   calculate_line_starts (window);
  891. }
  892.  
  893. /* Global variable control redisplay of scrolled windows.  If non-zero, it
  894.    is the desired number of lines to scroll the window in order to make
  895.    point visible.  A user might set this to 1 for smooth scrolling.  If
  896.    set to zero, the line containing point is centered within the window. */
  897. int window_scroll_step = 0;
  898.  
  899. /* Adjust the pagetop of WINDOW such that the cursor point will be visible. */
  900. void
  901. window_adjust_pagetop (window)
  902.      WINDOW *window;
  903. {
  904.   register int line = 0;
  905.   char *contents;
  906.  
  907.   if (!window->node)
  908.     return;
  909.  
  910.   contents = window->node->contents;
  911.  
  912.   /* Find the first printed line start which is after WINDOW->point. */
  913.   for (line = 0; line < window->line_count; line++)
  914.     {
  915.       char *line_start;
  916.  
  917.       line_start = window->line_starts[line];
  918.  
  919.       if ((line_start - contents) > window->point)
  920.     break;
  921.     }
  922.  
  923.   /* The line index preceding the line start which is past point is the
  924.      one containing point. */
  925.   line--;
  926.  
  927.   /* If this line appears in the current displayable page, do nothing.
  928.      Otherwise, adjust the top of the page to make this line visible. */
  929.   if ((line < window->pagetop) ||
  930.       (line - window->pagetop > (window->height - 1)))
  931.     {
  932.       /* The user-settable variable "scroll-step" is used to attempt
  933.      to make point visible, iff it is non-zero.  If that variable
  934.      is zero, then the line containing point is centered within
  935.      the window. */
  936.       if (window_scroll_step < window->height)
  937.     {
  938.       if ((line < window->pagetop) &&
  939.           ((window->pagetop - window_scroll_step) <= line))
  940.         window->pagetop -= window_scroll_step;
  941.       else if ((line - window->pagetop > (window->height - 1)) &&
  942.            ((line - (window->pagetop + window_scroll_step)
  943.              < window->height)))
  944.         window->pagetop += window_scroll_step;
  945.       else
  946.         window->pagetop = line - ((window->height - 1) / 2);
  947.     }
  948.       else
  949.     window->pagetop = line - ((window->height - 1) / 2);
  950.  
  951.       if (window->pagetop < 0)
  952.     window->pagetop = 0;
  953.       window->flags |= W_UpdateWindow;
  954.     }
  955. }
  956.  
  957. /* Return the index of the line containing point. */
  958. int
  959. window_line_of_point (window)
  960.      WINDOW *window;
  961. {
  962.   register int i, start = 0;
  963.  
  964.   /* Try to optimize.  Check to see if point is past the pagetop for
  965.      this window, and if so, start searching forward from there. */
  966.   if ((window->pagetop > -1 && window->pagetop < window->line_count) &&
  967.       (window->line_starts[window->pagetop] - window->node->contents)
  968.       <= window->point)
  969.     start = window->pagetop;
  970.  
  971.   for (i = start; i < window->line_count; i++)
  972.     {
  973.       if ((window->line_starts[i] - window->node->contents) > window->point)
  974.     break;
  975.     }
  976.  
  977.   return (i - 1);
  978. }
  979.  
  980. /* Get and return the goal column for this window. */
  981. int
  982. window_get_goal_column (window)
  983.      WINDOW *window;
  984. {
  985.   if (!window->node)
  986.     return (-1);
  987.  
  988.   if (window->goal_column != -1)
  989.     return (window->goal_column);
  990.  
  991.   /* Okay, do the work.  Find the printed offset of the cursor
  992.      in this window. */
  993.   return (window_get_cursor_column (window));
  994. }
  995.  
  996. /* Get and return the printed column offset of the cursor in this window. */
  997. int
  998. window_get_cursor_column (window)
  999.      WINDOW *window;
  1000. {
  1001.   int i, hpos, end;
  1002.   char *line;
  1003.  
  1004.   i = window_line_of_point (window);
  1005.  
  1006.   if (i < 0)
  1007.     return (-1);
  1008.  
  1009.   line = window->line_starts[i];
  1010.   end = window->point - (line - window->node->contents);
  1011.  
  1012.   for (hpos = 0, i = 0; i < end; i++)
  1013.     hpos += character_width (line[i], hpos);
  1014.  
  1015.   return (hpos);
  1016. }
  1017.  
  1018. /* Count the number of characters in LINE that precede the printed column
  1019.    offset of GOAL. */
  1020. int
  1021. window_chars_to_goal (line, goal)
  1022.      char *line;
  1023.      int goal;
  1024. {
  1025.   register int i, check, hpos;
  1026.  
  1027.   for (hpos = 0, i = 0; line[i] != '\n'; i++)
  1028.     {
  1029.  
  1030.       check = hpos + character_width (line[i], hpos);
  1031.  
  1032.       if (check > goal)
  1033.     break;
  1034.  
  1035.       hpos = check;
  1036.     }
  1037.   return (i);
  1038. }
  1039.  
  1040. /* Create a modeline for WINDOW, and store it in window->modeline. */
  1041. void
  1042. window_make_modeline (window)
  1043.      WINDOW *window;
  1044. {
  1045.   register int i;
  1046.   char *modeline;
  1047.   char location_indicator[4];
  1048.   int lines_remaining;
  1049.  
  1050.   /* Only make modelines for those windows which have one. */
  1051.   if (window->flags & W_InhibitMode)
  1052.     return;
  1053.  
  1054.   /* Find the number of lines actually displayed in this window. */
  1055.   lines_remaining = window->line_count - window->pagetop;
  1056.  
  1057.   if (window->pagetop == 0)
  1058.     {
  1059.       if (lines_remaining <= window->height)
  1060.     strcpy (location_indicator, "All");
  1061.       else
  1062.     strcpy (location_indicator, "Top");
  1063.     }
  1064.   else
  1065.     {
  1066.       if (lines_remaining <= window->height)
  1067.     strcpy (location_indicator, "Bot");
  1068.       else
  1069.     {
  1070.       float pt, lc;
  1071.       int percentage;
  1072.  
  1073.       pt = (float)window->pagetop;
  1074.       lc = (float)window->line_count;
  1075.  
  1076.       percentage = 100 * (pt / lc);
  1077.  
  1078.       sprintf (location_indicator, "%2d%%", percentage);
  1079.     }
  1080.     }
  1081.  
  1082.   /* Calculate the maximum size of the information to stick in MODELINE. */
  1083.   {
  1084.     int modeline_len = 0;
  1085.     char *parent = (char *)NULL, *filename = "*no file*";
  1086.     char *nodename = "*no node*";
  1087.     char *update_message = (char *)NULL;
  1088.     NODE *node = window->node;
  1089.  
  1090.     if (node)
  1091.       {
  1092.     if (node->nodename)
  1093.       nodename = node->nodename;
  1094.  
  1095.     if (node->parent)
  1096.       {
  1097.         parent = filename_non_directory (node->parent);
  1098.         modeline_len += strlen ("Subfile: ") + strlen (node->filename);
  1099.       }
  1100.  
  1101.     if (node->filename)
  1102.       filename = filename_non_directory (node->filename);
  1103.  
  1104.     if (node->flags & N_UpdateTags)
  1105.       update_message = "--*** Tags out of Date ***";
  1106.       }
  1107.  
  1108.     if (update_message)
  1109.       modeline_len += strlen (update_message);
  1110.     modeline_len += strlen (filename);
  1111.     modeline_len += strlen (nodename);
  1112.     modeline_len += 4;        /* strlen (location_indicator). */
  1113.  
  1114.     /* 10 for the decimal representation of the number of lines in this
  1115.        node, and the remainder of the text that can appear in the line. */
  1116.     modeline_len += 10 + strlen ("-----Info: (), lines ----, ");
  1117.     modeline_len += window->width;
  1118.  
  1119.     modeline = (char *)xmalloc (1 + modeline_len);
  1120.  
  1121.     /* Special internal windows have no filename. */
  1122.     if (!parent && !*filename)
  1123.       sprintf (modeline, "-%s---Info: %s, %d lines --%s--",
  1124.            (window->flags & W_NoWrap) ? "$" : "-",
  1125.            nodename, window->line_count, location_indicator);
  1126.     else
  1127.       sprintf (modeline, "-%s%s-Info: (%s)%s, %d lines --%s--",
  1128.            (window->flags & W_NoWrap) ? "$" : "-",
  1129.            (node && (node->flags & N_IsCompressed)) ? "zz" : "--",
  1130.            parent ? parent : filename,
  1131.            nodename, window->line_count, location_indicator);
  1132.  
  1133.     if (parent)
  1134.       sprintf (modeline + strlen (modeline), " Subfile: %s", filename);
  1135.  
  1136.     if (update_message)
  1137.       sprintf (modeline + strlen (modeline), "%s", update_message);
  1138.  
  1139.     i = strlen (modeline);
  1140.  
  1141.     if (i >= window->width)
  1142.       modeline[window->width] = '\0';
  1143.     else
  1144.       {
  1145.     while (i < window->width)
  1146.       modeline[i++] = '-';
  1147.     modeline[i] = '\0';
  1148.       }
  1149.  
  1150.     strcpy (window->modeline, modeline);
  1151.     free (modeline);
  1152.   }
  1153. }
  1154.  
  1155. /* Make WINDOW start displaying at PERCENT percentage of its node. */
  1156. void
  1157. window_goto_percentage (window, percent)
  1158.      WINDOW *window;
  1159.      int percent;
  1160. {
  1161.   int desired_line;
  1162.  
  1163.   if (!percent)
  1164.     desired_line = 0;
  1165.   else
  1166.     desired_line =
  1167.       (int) ((float)window->line_count * ((float)percent / 100.0));
  1168.  
  1169.   window->pagetop = desired_line;
  1170.   window->point =
  1171.     window->line_starts[window->pagetop] - window->node->contents;
  1172.   window->flags |= W_UpdateWindow;
  1173.   window_make_modeline (window);
  1174. }
  1175.  
  1176. /* Get the state of WINDOW, and save it in STATE. */
  1177. void
  1178. window_get_state (window, state)
  1179.      WINDOW *window;
  1180.      WINDOW_STATE *state;
  1181. {
  1182.   state->node = window->node;
  1183.   state->pagetop = window->pagetop;
  1184.   state->point = window->point;
  1185. }
  1186.  
  1187. /* Set the node, pagetop, and point of WINDOW. */
  1188. void
  1189. window_set_state (window, state)
  1190.      WINDOW *window;
  1191.      WINDOW_STATE *state;
  1192. {
  1193.   if (window->node != state->node)
  1194.     window_set_node_of_window (window, state->node);
  1195.   window->pagetop = state->pagetop;
  1196.   window->point = state->point;
  1197. }
  1198.  
  1199.  
  1200. /* **************************************************************** */
  1201. /*                                    */
  1202. /*           Manipulating Home-Made Nodes                */
  1203. /*                                    */
  1204. /* **************************************************************** */
  1205.  
  1206. /* A place to buffer echo area messages. */
  1207. static NODE *echo_area_node = (NODE *)NULL;
  1208.  
  1209. /* Make the node of the_echo_area be an empty one. */
  1210. static void
  1211. free_echo_area ()
  1212. {
  1213.   if (echo_area_node)
  1214.     {
  1215.       maybe_free (echo_area_node->contents);
  1216.       free (echo_area_node);
  1217.     }
  1218.  
  1219.   echo_area_node = (NODE *)NULL;
  1220.   window_set_node_of_window (the_echo_area, echo_area_node);
  1221. }
  1222.   
  1223. /* Clear the echo area, removing any message that is already present.
  1224.    The echo area is cleared immediately. */
  1225. void
  1226. window_clear_echo_area ()
  1227. {
  1228.   free_echo_area ();
  1229.   display_update_one_window (the_echo_area);
  1230. }
  1231.  
  1232. /* Make a message appear in the echo area, built from FORMAT, ARG1 and ARG2.
  1233.    The arguments are treated similar to printf () arguments, but not all of
  1234.    printf () hair is present.  The message appears immediately.  If there was
  1235.    already a message appearing in the echo area, it is removed. */
  1236. void
  1237. window_message_in_echo_area (format, arg1, arg2)
  1238.      char *format;
  1239.      void *arg1, *arg2;
  1240. {
  1241.   free_echo_area ();
  1242.   echo_area_node = build_message_node (format, arg1, arg2);
  1243.   window_set_node_of_window (the_echo_area, echo_area_node);
  1244.   display_update_one_window (the_echo_area);
  1245. }
  1246.  
  1247. /* Place a temporary message in the echo area built from FORMAT, ARG1
  1248.    and ARG2.  The message appears immediately, but does not destroy
  1249.    any existing message.  A future call to unmessage_in_echo_area ()
  1250.    restores the old contents. */
  1251. static NODE **old_echo_area_nodes = (NODE **)NULL;
  1252. static int old_echo_area_nodes_index = 0;
  1253. static int old_echo_area_nodes_slots = 0;
  1254.  
  1255. void
  1256. message_in_echo_area (format, arg1, arg2)
  1257.      char *format;
  1258.      void *arg1, *arg2;
  1259. {
  1260.   if (echo_area_node)
  1261.     {
  1262.       add_pointer_to_array (echo_area_node, old_echo_area_nodes_index,
  1263.                 old_echo_area_nodes, old_echo_area_nodes_slots,
  1264.                 4, NODE *);
  1265.     }
  1266.   echo_area_node = (NODE *)NULL;
  1267.   window_message_in_echo_area (format, arg1, arg2);
  1268. }
  1269.  
  1270. void
  1271. unmessage_in_echo_area ()
  1272. {
  1273.   free_echo_area ();
  1274.  
  1275.   if (old_echo_area_nodes_index)
  1276.     echo_area_node = old_echo_area_nodes[--old_echo_area_nodes_index];
  1277.  
  1278.   window_set_node_of_window (the_echo_area, echo_area_node);
  1279.   display_update_one_window (the_echo_area);
  1280. }
  1281.  
  1282. /* A place to build a message. */
  1283. static char *message_buffer = (char *)NULL;
  1284. static int message_buffer_index = 0;
  1285. static int message_buffer_size = 0;
  1286.  
  1287. /* Ensure that there is enough space to stuff LENGTH characters into
  1288.    MESSAGE_BUFFER. */
  1289. static void
  1290. message_buffer_resize (length)
  1291.      int length;
  1292. {
  1293.   if (!message_buffer)
  1294.     {
  1295.       message_buffer_size = length + 1;
  1296.       message_buffer = (char *)xmalloc (message_buffer_size);
  1297.       message_buffer_index = 0;
  1298.     }
  1299.  
  1300.   while (message_buffer_size <= message_buffer_index + length)
  1301.     message_buffer = (char *)
  1302.       xrealloc (message_buffer,
  1303.         message_buffer_size += 100 + (2 * length));
  1304. }
  1305.  
  1306. /* Format MESSAGE_BUFFER with the results of printing FORMAT with ARG1 and
  1307.    ARG2. */
  1308. static void
  1309. build_message_buffer (format, arg1, arg2)
  1310.      char *format;
  1311.      void *arg1, *arg2;
  1312. {
  1313.   register int i, len;
  1314.   void *args[2];
  1315.   int arg_index = 0;
  1316.  
  1317.   args[0] = arg1;
  1318.   args[1] = arg2;
  1319.  
  1320.   len = strlen (format);
  1321.  
  1322.   message_buffer_resize (len);
  1323.  
  1324.   for (i = 0; format[i]; i++)
  1325.     {
  1326.       if (format[i] != '%')
  1327.     {
  1328.       message_buffer[message_buffer_index++] = format[i];
  1329.       len--;
  1330.     }
  1331.       else
  1332.     {
  1333.       char c;
  1334.  
  1335.       c = format[++i];
  1336.  
  1337.       switch (c)
  1338.         {
  1339.         case '%':        /* Insert a percent sign. */
  1340.           message_buffer_resize (len + 1);
  1341.           message_buffer[message_buffer_index++] = '%';
  1342.           break;
  1343.  
  1344.         case 's':        /* Insert the current arg as a string. */
  1345.           {
  1346.         char *string;
  1347.         int string_len;
  1348.  
  1349.         string = (char *)args[arg_index++];
  1350.         string_len = strlen (string);
  1351.  
  1352.         message_buffer_resize (len + string_len);
  1353.         sprintf
  1354.           (message_buffer + message_buffer_index, "%s", string);
  1355.         message_buffer_index += string_len;
  1356.           }
  1357.           break;
  1358.  
  1359.         case 'd':        /* Insert the current arg as an integer. */
  1360.           {
  1361.         int integer;
  1362.  
  1363.         integer = (int)args[arg_index++];
  1364.  
  1365.         message_buffer_resize (len + 32);
  1366.         sprintf
  1367.           (message_buffer + message_buffer_index, "%d", integer);
  1368.         message_buffer_index = strlen (message_buffer);
  1369.           }
  1370.           break;
  1371.  
  1372.         case 'c':        /* Insert the current arg as a character. */
  1373.           {
  1374.         int character;
  1375.  
  1376.         character = (int)args[arg_index++];
  1377.  
  1378.         message_buffer_resize (len + 1);
  1379.         message_buffer[message_buffer_index++] = character;
  1380.           }
  1381.           break;
  1382.  
  1383.         default:
  1384.           abort ();
  1385.         }
  1386.     }
  1387.     }
  1388.   message_buffer[message_buffer_index] = '\0';
  1389. }
  1390.  
  1391. /* Build a new node which has FORMAT printed with ARG1 and ARG2 as the
  1392.    contents. */
  1393. NODE *
  1394. build_message_node (format, arg1, arg2)
  1395.      char *format;
  1396.      void *arg1, *arg2;
  1397. {
  1398.   NODE *node;
  1399.  
  1400.   message_buffer_index = 0;
  1401.   build_message_buffer (format, arg1, arg2);
  1402.  
  1403.   node = message_buffer_to_node ();
  1404.   return (node);
  1405. }
  1406.  
  1407. /* Convert the contents of the message buffer to a node. */
  1408. NODE *
  1409. message_buffer_to_node ()
  1410. {
  1411.   NODE *node;
  1412.  
  1413.   node = (NODE *)xmalloc (sizeof (NODE));
  1414.   node->filename = (char *)NULL;
  1415.   node->parent = (char *)NULL;
  1416.   node->nodename = (char *)NULL;
  1417.   node->flags = 0;
  1418.  
  1419.   /* Make sure that this buffer ends with a newline. */
  1420.   node->nodelen = 1 + strlen (message_buffer);
  1421.   node->contents = (char *)xmalloc (1 + node->nodelen);
  1422.   strcpy (node->contents, message_buffer);
  1423.   node->contents[node->nodelen - 1] = '\n';
  1424.   node->contents[node->nodelen] = '\0';
  1425.   return (node);
  1426. }
  1427.  
  1428. /* Useful functions can be called from outside of window.c. */
  1429. void
  1430. initialize_message_buffer ()
  1431. {
  1432.   message_buffer_index = 0;
  1433. }
  1434.  
  1435. /* Print FORMAT with ARG1,2 to the end of the current message buffer. */
  1436. void
  1437. printf_to_message_buffer (format, arg1, arg2)
  1438.      char *format;
  1439.      void *arg1, *arg2;
  1440. {
  1441.   build_message_buffer (format, arg1, arg2);
  1442. }
  1443.  
  1444. /* Return the current horizontal position of the "cursor" on the most
  1445.    recently output message buffer line. */
  1446. int
  1447. message_buffer_length_this_line ()
  1448. {
  1449.   register int i;
  1450.  
  1451.   if (!message_buffer_index)
  1452.     return (0);
  1453.  
  1454.   for (i = message_buffer_index; i && message_buffer[i - 1] != '\n'; i--);
  1455.  
  1456.   return (string_width (message_buffer + i, 0));
  1457. }
  1458.  
  1459. /* Pad STRING to COUNT characters by inserting blanks. */
  1460. int
  1461. pad_to (count, string)
  1462.      int count;
  1463.      char *string;
  1464. {
  1465.   register int i;
  1466.  
  1467.   i = strlen (string);
  1468.  
  1469.   if (i >= count)
  1470.     string[i++] = ' ';
  1471.   else
  1472.     {
  1473.       while (i < count)
  1474.     string[i++] = ' ';
  1475.     }
  1476.   string[i] = '\0';
  1477.  
  1478.   return (i);
  1479. }
  1480.