home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / VILE327.ZIP / VILE327.TAR / vile3.27 / window.c < prev    next >
C/C++ Source or Header  |  1992-12-14  |  22KB  |  940 lines

  1. /*
  2.  * Window management. Some of the functions are internal, and some are
  3.  * attached to keys that the user actually types.
  4.  *
  5.  * $Log: window.c,v $
  6.  * Revision 1.16  1992/12/04  09:15:38  foxharp
  7.  * delete unused assigns
  8.  *
  9.  * Revision 1.15  1992/08/20  23:40:48  foxharp
  10.  * typo fixes -- thanks, eric
  11.  *
  12.  * Revision 1.14  1992/05/16  12:00:31  pgf
  13.  * prototypes/ansi/void-int stuff/microsoftC
  14.  *
  15.  * Revision 1.13  1992/01/05  00:06:13  pgf
  16.  * split mlwrite into mlwrite/mlprompt/mlforce to make errors visible more
  17.  * often.  also normalized message appearance somewhat.
  18.  *
  19.  * Revision 1.12  1991/11/04  14:20:18  pgf
  20.  * fixed broken mvdnwind()
  21.  *
  22.  * Revision 1.11  1991/11/01  14:38:00  pgf
  23.  * saber cleanup
  24.  *
  25.  * Revision 1.10  1991/10/22  14:08:23  pgf
  26.  * took out old ifdef BEFORE code
  27.  *
  28.  * Revision 1.9  1991/09/30  01:47:24  pgf
  29.  * keep sideways motions local to a window
  30.  *
  31.  * Revision 1.8  1991/09/26  13:09:55  pgf
  32.  * w_sideways is now one of the window values
  33.  *
  34.  * Revision 1.7  1991/08/07  12:35:07  pgf
  35.  * added RCS log messages
  36.  *
  37.  * revision 1.6
  38.  * date: 1991/08/06 15:27:21;
  39.  * new splitwind algorithm
  40.  * 
  41.  * revision 1.5
  42.  * date: 1991/08/02 10:27:07;
  43.  * added dave lemke's scroll fix to mvupwind()
  44.  * 
  45.  * revision 1.4
  46.  * date: 1991/06/25 19:53:41;
  47.  * massive data structure restructure
  48.  * 
  49.  * revision 1.3
  50.  * date: 1991/02/21 09:14:20;
  51.  * added horizontal scrolling, and made newlength and
  52.  * newwidth into commands
  53.  * 
  54.  * revision 1.2
  55.  * date: 1990/09/25 11:38:28;
  56.  * took out old ifdef BEFORE code
  57.  * 
  58.  * revision 1.1
  59.  * date: 1990/09/21 10:26:21;
  60.  * initial vile RCS revision
  61.  */
  62.  
  63. #include        <stdio.h>
  64. #include        "estruct.h"
  65. #include    "edef.h"
  66.  
  67. #if    MEGAMAX & ST520
  68. overlay    "window"
  69. #endif
  70.  
  71. /*
  72.  * Reposition dot's line to line "n" of the window. If the argument is
  73.  * positive, it is that line. If it is negative it is that line from the
  74.  * bottom. If it is 0 the window is centered around dot (this is what 
  75.  * the standard redisplay code does). Defaults to 0.
  76.  */
  77. int
  78. reposition(f, n)
  79. int f,n;
  80. {
  81.     if (f == FALSE)        /* default to 0 to center screen */
  82.         n = 0;
  83.     curwp->w_force = n;
  84.     curwp->w_flag |= WFFORCE;
  85.     return (TRUE);
  86. }
  87.  
  88. /*
  89.  * Refresh the screen. With no argument, it just does the refresh. With an
  90.  * argument it recenters "." in the current window.
  91.  */
  92. /* ARGSUSED */
  93. int
  94. refresh(f, n)
  95. int f,n;
  96. {
  97.  
  98.     if (f == FALSE) {
  99.         sgarbf = TRUE;
  100.     } else {
  101.             curwp->w_force = 0;             /* Center dot. */
  102.             curwp->w_flag |= WFFORCE;
  103.     }
  104.  
  105.     return (TRUE);
  106. }
  107.  
  108. /*
  109.  * The command make the next window (next => down the screen) the current
  110.  * window. There are no real errors, although the command does nothing if
  111.  * there is only 1 window on the screen.
  112.  *
  113.  * with an argument this command finds the <n>th window from the top
  114.  *
  115.  */
  116. int
  117. nextwind(f, n)
  118. int f, n;    /* default flag and numeric argument */
  119. {
  120.     register WINDOW *wp;
  121.     register int nwindows;        /* total number of windows */
  122.  
  123.     if (f) {
  124.  
  125.         /* first count the # of windows */
  126.         wp = wheadp;
  127.         nwindows = 1;
  128.         while (wp->w_wndp != NULL) {
  129.             nwindows++;
  130.             wp = wp->w_wndp;
  131.         }
  132.  
  133.         /* if the argument is negative, it is the nth window
  134.            from the bottom of the screen            */
  135.         if (n < 0)
  136.             n = nwindows + n + 1;
  137.  
  138.         /* if an argument, give them that window from the top */
  139.         if (n > 0 && n <= nwindows) {
  140.             wp = wheadp;
  141.             while (--n)
  142.                 wp = wp->w_wndp;
  143.         } else {
  144.             mlforce("[Window number out of range]");
  145.             return(FALSE);
  146.         }
  147.     } else {
  148.         if ((wp = curwp->w_wndp) == NULL)
  149.             wp = wheadp;
  150.     }
  151.     curwp = wp;
  152.     make_current(curwp->w_bufp);
  153.     upmode();
  154.     return (TRUE);
  155. }
  156.  
  157. int
  158. poswind(f,n)
  159. int f,n;
  160. {
  161.     register int c;
  162.     register int rows;
  163.     int s;
  164.  
  165.     c = kbd_key();
  166.     if (c == abortc)
  167.         return FALSE;
  168.  
  169.     if (c == '+' || c == '\r' || c == 'H') {
  170.         rows = 1;
  171.     } else if (c == '.' || c == 'M') {
  172.         rows = 0;
  173.     } else if (c == '-' || c == 'L') {
  174.         rows = -1;
  175.     } else {
  176.         TTbeep();
  177.         return FALSE;
  178.     }
  179.  
  180.     if (f == TRUE) {
  181.         s = gotoline(f,n);
  182.         if (s != TRUE)
  183.             return(s);
  184.     }
  185.     return(reposition(TRUE,rows));
  186. }
  187.  
  188. /*
  189.  * This command makes the previous window (previous => up the screen) the
  190.  * current window. There aren't any errors, although the command does not do a
  191.  * lot if there is 1 window.
  192.  */
  193. int
  194. prevwind(f, n)
  195. int f,n;
  196. {
  197.     register WINDOW *wp1;
  198.     register WINDOW *wp2;
  199.  
  200.     /* if we have an argument, we mean the nth window from the bottom */
  201.     if (f)
  202.         return(nextwind(f, -n));
  203.  
  204.     wp1 = wheadp;
  205.     wp2 = curwp;
  206.  
  207.     if (wp1 == wp2)
  208.         wp2 = NULL;
  209.  
  210.     while (wp1->w_wndp != wp2)
  211.         wp1 = wp1->w_wndp;
  212.  
  213.     curwp = wp1;
  214.     make_current(curwp->w_bufp);
  215.     upmode();
  216.     return (TRUE);
  217. }
  218.  
  219. /*
  220.  * This command moves the current window down by "arg" lines. Recompute the
  221.  * top line in the window. The move up and move down code is almost completely
  222.  * the same; most of the work has to do with reframing the window, and picking
  223.  * a new dot. We share the code by having "move down" just be an interface to
  224.  * "move up". Magic.
  225.  */
  226. int
  227. mvdnwind(f, n)
  228. int f,n;
  229. {
  230.     if (!f)
  231.         n = 1;
  232.     return (mvupwind(TRUE, -n));
  233. }
  234.  
  235. /*
  236.  * Move the current window up by "arg" lines. Recompute the new top line of
  237.  * the window. Look to see if "." is still on the screen. If it is, you win.
  238.  * If it isn't, then move "." to center it in the new framing of the window
  239.  * (this command does not really move "." (except as above); it moves the 
  240.  * frame).
  241.  */
  242. int
  243. mvupwind(f, n)
  244. int f,n;
  245. {
  246.     register LINE  *lp;
  247.     register int    i;
  248.     int             was_n = n;
  249.  
  250.     lp = curwp->w_line.l;
  251.  
  252.     if (!f)
  253.         n = 1;
  254.  
  255.     if (n < 0)
  256.         curwp->w_flag |= WFKILLS;
  257.     else
  258.         curwp->w_flag |= WFINS;
  259.  
  260.     if (n < 0) {
  261.         while (n++ && lforw(lp) != curbp->b_line.l)
  262.             lp = lforw(lp);
  263.     } else {
  264.         while (n-- && lback(lp) != curbp->b_line.l)
  265.             lp = lback(lp);
  266.     }
  267.  
  268.     curwp->w_line.l = lp;
  269.     curwp->w_flag |= WFHARD | WFMODE;
  270.  
  271.     /* is it still in the window */
  272.     for (i = 0; i < curwp->w_ntrows; ++i) {
  273.         if (lp == curwp->w_dot.l)
  274.             return (TRUE);
  275.         if (lforw(lp) == curbp->b_line.l)
  276.             break;
  277.         lp = lforw(lp);
  278.     }
  279.     /*
  280.      * now lp is either just past the window bottom, or it's the last
  281.      * line of the file
  282.      */
  283.  
  284.     /* preserve the current column */
  285.     if (curgoal < 0)
  286.         curgoal = getccol(FALSE);
  287.  
  288.     if (was_n < 0)
  289.         curwp->w_dot.l = curwp->w_line.l;
  290.     else
  291.         curwp->w_dot.l = lback(lp);
  292.     curwp->w_dot.o = getgoal(curwp->w_dot.l);
  293.     return (TRUE);
  294. }
  295.  
  296. int
  297. mvdnnxtwind(f, n)
  298. int f,n;
  299. {
  300.     nextwind(FALSE, 1);
  301.     mvdnwind(f, n);
  302.     prevwind(FALSE, 1);
  303.     return TRUE;
  304. }
  305.  
  306. int
  307. mvupnxtwind(f, n)
  308. int f,n;
  309. {
  310.     nextwind(FALSE, 1);
  311.     mvupwind(f, n);
  312.     prevwind(FALSE, 1);
  313.     return TRUE;
  314. }
  315.  
  316. int
  317. mvrightwind(f,n)
  318. int f,n;
  319. {
  320.     int move;
  321.  
  322.     if (f)
  323.         move = n;
  324.     else
  325.         move = term.t_ncol/3;
  326.  
  327.     if (w_val(curwp, WVAL_SIDEWAYS) + move > getccol(FALSE) - 1) {
  328.         TTbeep();
  329.         return FALSE;
  330.     }
  331.  
  332.     make_local_w_val(curwp,WVAL_SIDEWAYS);
  333.  
  334.     w_val(curwp, WVAL_SIDEWAYS) += move;
  335.  
  336.         curwp->w_flag  |= WFHARD|WFMOVE;
  337.  
  338.     return TRUE;
  339. }
  340.  
  341. int
  342. mvleftwind(f,n)
  343. int f,n;
  344. {
  345.     make_local_w_val(curwp,WVAL_SIDEWAYS);
  346.     if (f)
  347.         w_val(curwp, WVAL_SIDEWAYS) -= n;
  348.     else
  349.         w_val(curwp, WVAL_SIDEWAYS) -= term.t_ncol/3;
  350.  
  351.     if (w_val(curwp, WVAL_SIDEWAYS) < 0)
  352.         w_val(curwp, WVAL_SIDEWAYS) = 0;
  353.  
  354.         curwp->w_flag  |= WFHARD|WFMOVE;
  355.  
  356.     return TRUE;
  357. }
  358.  
  359. /*
  360.  * This command makes the current window the only window on the screen.
  361.  * Try to set the framing so that "." does not have to move on the
  362.  * display. Some care has to be taken to keep the values of dot and mark in
  363.  * the buffer structures right if the distruction of a window makes a buffer
  364.  * become undisplayed.
  365.  */
  366. /* ARGSUSED */
  367. int
  368. onlywind(f, n)
  369. int f,n;
  370. {
  371.         register WINDOW *wp;
  372.         register LINE   *lp;
  373.         register int    i;
  374.  
  375.         wp = wheadp;
  376.         while (wp != NULL) {
  377.         register WINDOW *nwp;
  378.         nwp = wp->w_wndp;
  379.             if (wp != curwp) {
  380.                     if (--wp->w_bufp->b_nwnd == 0)
  381.                             undispbuff(wp->w_bufp,wp);
  382.                     free((char *) wp);
  383.             }
  384.                 wp = nwp;
  385.         }
  386.         wheadp = curwp;
  387.         wheadp->w_wndp = NULL;
  388.         lp = curwp->w_line.l;
  389.         i  = curwp->w_toprow;
  390.         while (i!=0 && lback(lp)!=curbp->b_line.l) {
  391.                 --i;
  392.                 lp = lback(lp);
  393.         }
  394.         curwp->w_toprow = 0;
  395.         curwp->w_ntrows = term.t_nrow-1;
  396.         curwp->w_line.l  = lp;
  397.         curwp->w_flag  |= WFMODE|WFHARD;
  398.         return (TRUE);
  399. }
  400.  
  401. /*
  402.  * Delete the current window, placing its space in the window above,
  403.  * or, if it is the top window, the window below.
  404.  */
  405.  
  406. /* ARGSUSED */
  407. int
  408. delwind(f,n)
  409. int f, n;    /* arguments are ignored for this command */
  410. {
  411.     return delwp(curwp);
  412. }
  413.  
  414. int
  415. delwp(thewp)
  416. WINDOW *thewp;
  417. {
  418.     register WINDOW *wp;    /* window to receive deleted space */
  419.     register LINE *lp;    /* line pointer */
  420.     register int i;
  421.  
  422.     /* if there is only one window, don't delete it */
  423.     if (wheadp->w_wndp == NULL) {
  424.         mlforce("[Cannot delete the only window]");
  425.         return(FALSE);
  426.     }
  427.  
  428.     /* find receiving window and give up our space */
  429.     if (thewp == wheadp) { /* there's nothing before */
  430.         /* find the next window down */
  431.         wp = thewp->w_wndp;
  432.                 lp = wp->w_line.l;
  433.                 /* the prev. window (thewp) has wp->w_toprow rows in it */
  434.                 for (i = wp->w_toprow;
  435.                  i > 0 && lback(lp) != wp->w_bufp->b_line.l; --i)
  436.                         lp = lback(lp);
  437.                 wp->w_line.l  = lp;
  438.         wp->w_ntrows += wp->w_toprow;  /* add in the new rows */
  439.         wp->w_toprow = 0;    /* and we're at the top of the screen */
  440.         wheadp = wp;    /* and at the top of the list as well */
  441.     } else {
  442.         /* find window before thewp in linked list */
  443.         wp = wheadp;
  444.         while(wp->w_wndp != thewp)
  445.             wp = wp->w_wndp;
  446.         /* add thewp's rows to the next window up */
  447.         wp->w_ntrows += thewp->w_ntrows+1;
  448.         
  449.         wp->w_wndp = thewp->w_wndp; /* make their next window ours */
  450.     }
  451.  
  452.     /* get rid of the current window */
  453.     if (--thewp->w_bufp->b_nwnd == 0)
  454.         undispbuff(thewp->w_bufp,thewp);
  455.     if (thewp == curwp) {
  456.         curwp = wp;
  457.         curwp->w_flag |= WFHARD;
  458.         make_current(curwp->w_bufp);
  459.     }
  460.     free((char *)thewp);
  461.     upmode();
  462.     return(TRUE);
  463. }
  464.  
  465. /*
  466.     Split the current window.  A window smaller than 3 lines cannot be
  467.     split.  An argument of 1 forces the cursor into the upper window, an
  468.     argument of two forces the cursor to the lower window.  The only other
  469.     error that is possible is a "malloc" failure allocating the structure
  470.     for the new window.
  471.  */
  472. WINDOW *
  473. splitw(f, n)
  474. int f,n;
  475. {
  476.         register WINDOW *wp;
  477.         register LINE   *lp;
  478.         register int    ntru;
  479.         register int    ntrl;
  480.         register int    ntrd;
  481.         register WINDOW *wp1;
  482.         register WINDOW *wp2;
  483.     register int i;
  484.  
  485.         if (curwp->w_ntrows < 3) {
  486.                 mlforce("[Cannot split a %d line window]", curwp->w_ntrows);
  487.                 return NULL;
  488.         }
  489.         if ((wp = (WINDOW *) malloc(sizeof(WINDOW))) == NULL) {
  490.                 mlforce("[OUT OF MEMORY]");
  491.                 return NULL;
  492.         }
  493.     ++curwp->w_bufp->b_nwnd;           /* Displayed twice.     */
  494.         wp->w_bufp  = curwp->w_bufp;
  495.         wp->w_line = curwp->w_line;
  496.         wp->w_traits  = curwp->w_traits;
  497.         wp->w_flag  = 0;
  498.         wp->w_force = 0;
  499.         ntru = (curwp->w_ntrows-1) / 2;         /* Upper size           */
  500.         ntrl = (curwp->w_ntrows-1) - ntru;      /* Lower size           */
  501.         lp = curwp->w_line.l;
  502.         ntrd = 0;
  503.         while (lp != curwp->w_dot.l) {
  504.                 ++ntrd;
  505.                 lp = lforw(lp);
  506.         }
  507.     /* ntrd is now the row containing dot */
  508.         if (((f == FALSE) && (ntrd <= ntru)) || ((f == TRUE) && (n == 1))) {
  509.                 /* Old is upper window. */
  510.             /* Adjust the top line if necessary */
  511.                 if (ntrd == ntru) {             /* Hit mode line.       */
  512.             if (ntrl > 1) {
  513.                 ntru++;
  514.                 ntrl--;
  515.             } else {
  516.                             curwp->w_line.l = lforw(curwp->w_line.l);
  517.             }
  518.         }
  519.                 curwp->w_ntrows = ntru; /* new size */
  520.         /* insert new window after curwp in window list */
  521.                 wp->w_wndp = curwp->w_wndp;
  522.                 curwp->w_wndp = wp;
  523.         /* set new window's position and size */
  524.                 wp->w_toprow = curwp->w_toprow+ntru+1;
  525.                 wp->w_ntrows = ntrl;
  526.         /* try to keep lower from reframing */
  527.         for (i = ntru+1; i > 0 &&
  528.                  wp->w_line.l != wp->w_bufp->b_line.l; i--) {
  529.             wp->w_line.l = lforw(wp->w_line.l);
  530.         }
  531.         wp->w_dot.l = wp->w_line.l;
  532.         wp->w_dot.o = 0;
  533.         } else {
  534.         /* Old is lower window  */
  535.                 wp1 = NULL;
  536.                 wp2 = wheadp;
  537.                 while (wp2 != curwp) {
  538.                         wp1 = wp2;
  539.                         wp2 = wp2->w_wndp;
  540.                 }
  541.                 if (wp1 == NULL)
  542.                         wheadp = wp;
  543.                 else
  544.                         wp1->w_wndp = wp;
  545.                 wp->w_wndp   = curwp;
  546.                 wp->w_toprow = curwp->w_toprow;
  547.                 wp->w_ntrows = ntru;
  548.                 ++ntru;                         /* Mode line.           */
  549.                 curwp->w_toprow += ntru;
  550.                 curwp->w_ntrows  = ntrl;
  551.         wp->w_dot.l = wp->w_line.l;
  552.         /* move upper window dot to bottom line of upper */
  553.         for (i = ntru-2; 
  554.             i > 0 && wp->w_dot.l!=wp->w_bufp->b_line.l; i--)
  555.             wp->w_dot.l = lforw(wp->w_dot.l);
  556.         wp->w_dot.o = 0;
  557.         /* adjust lower window topline */
  558.                 while (ntru--)
  559.                         curwp->w_line.l = lforw(curwp->w_line.l);
  560.         }
  561.         curwp->w_flag |= WFMODE|WFHARD;
  562.         wp->w_flag |= WFMODE|WFHARD;
  563.         return wp;
  564. }
  565.  
  566. /* external callable version -- return int instead of (WINDOW *) */
  567. int
  568. splitwind(f,n)
  569. int f,n;
  570. {
  571.     return (splitw(f,n)) ? TRUE:FALSE;
  572. }
  573.  
  574. /*
  575.  * Enlarge the current window. Find the window that loses space. Make sure it
  576.  * is big enough. If so, hack the window descriptions, and ask redisplay to do
  577.  * all the hard work. You don't just set "force reframe" because dot would
  578.  * move.
  579.  */
  580. /* ARGSUSED */
  581. int
  582. enlargewind(f, n)
  583. int f,n;
  584. {
  585.         register WINDOW *adjwp;
  586.         register LINE   *lp;
  587.         register int    i;
  588.  
  589.         if (n < 0)
  590.                 return (shrinkwind(f, -n));
  591.         if (wheadp->w_wndp == NULL) {
  592.                 mlforce("[Only one window]");
  593.                 return (FALSE);
  594.         }
  595.         if ((adjwp=curwp->w_wndp) == NULL) {
  596.                 adjwp = wheadp;
  597.                 while (adjwp->w_wndp != curwp)
  598.                         adjwp = adjwp->w_wndp;
  599.         }
  600.         if (adjwp->w_ntrows <= n) {
  601.                 mlforce("[Impossible change]");
  602.                 return (FALSE);
  603.         }
  604.         if (curwp->w_wndp == adjwp) {           /* Shrink below.        */
  605.                 lp = adjwp->w_line.l;
  606.                 for (i=0; i<n && lp!=adjwp->w_bufp->b_line.l; ++i)
  607.                         lp = lforw(lp);
  608.                 adjwp->w_line.l  = lp;
  609.                 adjwp->w_toprow += n;
  610.         } else {                                /* Shrink above.        */
  611.                 lp = curwp->w_line.l;
  612.                 for (i=0; i<n && lback(lp)!=curbp->b_line.l; ++i)
  613.                         lp = lback(lp);
  614.                 curwp->w_line.l  = lp;
  615.                 curwp->w_toprow -= n;
  616.         }
  617.         curwp->w_ntrows += n;
  618.         adjwp->w_ntrows -= n;
  619.         curwp->w_flag |= WFMODE|WFHARD|WFINS;
  620.         adjwp->w_flag |= WFMODE|WFHARD|WFKILLS;
  621.         return (TRUE);
  622. }
  623.  
  624. /*
  625.  * Shrink the current window. Find the window that gains space. Hack at the
  626.  * window descriptions. Ask the redisplay to do all the hard work.
  627.  */
  628. int
  629. shrinkwind(f, n)
  630. int f,n;
  631. {
  632.         register WINDOW *adjwp;
  633.         register LINE   *lp;
  634.         register int    i;
  635.  
  636.         if (n < 0)
  637.                 return (enlargewind(f, -n));
  638.         if (wheadp->w_wndp == NULL) {
  639.                 mlforce("[Only one window]");
  640.                 return (FALSE);
  641.         }
  642.         if ((adjwp=curwp->w_wndp) == NULL) {
  643.                 adjwp = wheadp;
  644.                 while (adjwp->w_wndp != curwp)
  645.                         adjwp = adjwp->w_wndp;
  646.         }
  647.         if (curwp->w_ntrows <= n) {
  648.                 mlforce("[Impossible change]");
  649.                 return (FALSE);
  650.         }
  651.         if (curwp->w_wndp == adjwp) {           /* Grow below.          */
  652.                 lp = adjwp->w_line.l;
  653.                 for (i=0; i<n && lback(lp)!=adjwp->w_bufp->b_line.l; ++i)
  654.                         lp = lback(lp);
  655.                 adjwp->w_line.l  = lp;
  656.                 adjwp->w_toprow -= n;
  657.         } else {                                /* Grow above.          */
  658.                 lp = curwp->w_line.l;
  659.                 for (i=0; i<n && lp!=curbp->b_line.l; ++i)
  660.                         lp = lforw(lp);
  661.                 curwp->w_line.l  = lp;
  662.                 curwp->w_toprow += n;
  663.         }
  664.         curwp->w_ntrows -= n;
  665.         adjwp->w_ntrows += n;
  666.         curwp->w_flag |= WFMODE|WFHARD|WFKILLS;
  667.         adjwp->w_flag |= WFMODE|WFHARD|WFINS;
  668.         return (TRUE);
  669. }
  670.  
  671. #if !SMALLER
  672.  
  673. /*    Resize the current window to the requested size    */
  674. int
  675. resize(f, n)
  676. int f, n;    /* default flag and numeric argument */
  677. {
  678.     int clines;    /* current # of lines in window */
  679.     
  680.     /* must have a non-default argument, else ignore call */
  681.     if (f == FALSE)
  682.         return(TRUE);
  683.  
  684.     /* find out what to do */
  685.     clines = curwp->w_ntrows;
  686.  
  687.     /* already the right size? */
  688.     if (clines == n)
  689.         return(TRUE);
  690.  
  691.     return(enlargewind(TRUE, n - clines));
  692. }
  693. #endif
  694.  
  695. /*
  696.  * Pick a window for a pop-up. Split the screen if there is only one window.
  697.  * Pick the uppermost window that isn't the current window. An LRU algorithm
  698.  * might be better. Return a pointer, or NULL on error.
  699.  */
  700. WINDOW  *
  701. wpopup()
  702. {
  703.         register WINDOW *wp;
  704.         register WINDOW *owp;
  705.         register WINDOW *biggest_wp;
  706.  
  707.     owp = curwp;
  708.         wp = biggest_wp = wheadp;                /* Find window to split   */
  709.         while (wp->w_wndp != NULL) {
  710.                 wp = wp->w_wndp;
  711.         if(wp->w_ntrows > biggest_wp->w_ntrows)
  712.             biggest_wp = wp;
  713.     }
  714.     curwp = biggest_wp;
  715.     wp = splitw(FALSE,0); /* yes -- choose the unoccupied half */
  716.     curwp = owp;
  717.     if (wp == NULL ) { /* maybe biggest_wp was too small  */
  718.         wp = wheadp;        /* Find window to use    */
  719.             while (wp!=NULL && wp==curwp) /* uppermost non-current window */
  720.                     wp = wp->w_wndp;
  721.     }
  722.         return wp;
  723. }
  724.  
  725. int
  726. scrnextup(f, n)        /* scroll the next window up (back) a page */
  727. int f,n;
  728. {
  729.     nextwind(FALSE, 1);
  730.     backhpage(f, n);
  731.     prevwind(FALSE, 1);
  732.     return TRUE;
  733. }
  734.  
  735. int
  736. scrnextdw(f, n)        /* scroll the next window down (forward) a page */
  737. int f,n;
  738. {
  739.     nextwind(FALSE, 1);
  740.     forwhpage(f, n);
  741.     prevwind(FALSE, 1);
  742.     return TRUE;
  743. }
  744.  
  745. #if ! SMALLER
  746. /* ARGSUSED */
  747. int
  748. savewnd(f, n)        /* save ptr to current window */
  749. int f,n;
  750. {
  751.     swindow = curwp;
  752.     return(TRUE);
  753. }
  754.  
  755. /* ARGSUSED */
  756. int
  757. restwnd(f, n)        /* restore the saved screen */
  758. int f,n;
  759. {
  760.     register WINDOW *wp;
  761.  
  762.     /* find the window */
  763.     wp = wheadp;
  764.     while (wp != NULL) {
  765.         if (wp == swindow) {
  766.             curwp = wp;
  767.             make_current(curwp->w_bufp);
  768.             upmode();
  769.             return (TRUE);
  770.         }
  771.         wp = wp->w_wndp;
  772.     }
  773.  
  774.     mlforce("[No such window exists]");
  775.     return(FALSE);
  776. }
  777. #endif
  778.  
  779. int
  780. newlength(f,n)    /* resize the screen, re-writing the screen */
  781. int f,n;    /* numeric argument */
  782. {
  783.     WINDOW *wp;    /* current window being examined */
  784.     WINDOW *nextwp;    /* next window to scan */
  785.     WINDOW *lastwp;    /* last window scanned */
  786.     int lastline;    /* screen line of last line of current window */
  787.  
  788.     if (!f) {
  789.         mlforce("[No length given]");
  790.         return FALSE;
  791.     }
  792.  
  793.     /* make sure it's in range */
  794.     if (n < 3 || n > term.t_mrow + 1) {
  795.         mlforce("[Screen length out of range]");
  796.         return(FALSE);
  797.     }
  798.  
  799.     if (term.t_nrow == n - 1)
  800.         return(TRUE);
  801.     else if (term.t_nrow < n - 1) {
  802.  
  803.         /* go to the last window */
  804.         wp = wheadp;
  805.         while (wp->w_wndp != NULL)
  806.             wp = wp->w_wndp;
  807.  
  808.         /* and enlarge it as needed */
  809.         wp->w_ntrows = n - wp->w_toprow - 2;
  810.         wp->w_flag |= WFHARD|WFMODE;
  811.  
  812.     } else {
  813.  
  814.         /* rebuild the window structure */
  815.         nextwp = wheadp;
  816.         lastwp = NULL;
  817.         while (nextwp != NULL) {
  818.             wp = nextwp;
  819.             nextwp = wp->w_wndp;
  820.     
  821.             /* get rid of it if it is too low */
  822.             if (wp->w_toprow > n - 2) {
  823.  
  824.                 if (--wp->w_bufp->b_nwnd == 0) {
  825.                     undispbuff(wp->w_bufp,wp);
  826.                 }
  827.     
  828.                 /* update curwp and lastwp if needed */
  829.                 if (wp == curwp)
  830.                     curwp = wheadp;
  831.                     make_current(curwp->w_bufp);
  832.                 if (lastwp != NULL)
  833.                     lastwp->w_wndp = NULL;
  834.  
  835.                 /* free the structure */
  836.                 free((char *)wp);
  837.                 wp = NULL;
  838.  
  839.             } else {
  840.                 /* need to change this window size? */
  841.                 lastline = wp->w_toprow + wp->w_ntrows - 1;
  842.                 if (lastline >= n - 2) {
  843.                     wp->w_ntrows = n - wp->w_toprow - 2;
  844.                     wp->w_flag |= WFHARD|WFMODE;
  845.                 }
  846.             }
  847.     
  848.             lastwp = wp;
  849.         }
  850.     }
  851.  
  852.     /* screen is garbage */
  853.     term.t_nrow = n - 1;
  854.     sgarbf = TRUE;
  855.     return(TRUE);
  856. }
  857.  
  858. int
  859. newwidth(f,n)    /* resize the screen, re-writing the screen */
  860. int f,n;    /* numeric argument */
  861. {
  862.     register WINDOW *wp;
  863.  
  864.     if (!f) {
  865.         mlforce("[No width given]");
  866.         return FALSE;
  867.     }
  868.  
  869.     /* make sure it's in range */
  870.     if (n < 10 || n > term.t_mcol) {
  871.         mlforce("[Screen width out of range]");
  872.         return(FALSE);
  873.     }
  874.  
  875.     /* otherwise, just re-width it (no big deal) */
  876.     term.t_ncol = n;
  877.     term.t_margin = n / 10;
  878.     term.t_scrsiz = n - (term.t_margin * 2);
  879.  
  880.     /* force all windows to redraw */
  881.     wp = wheadp;
  882.     while (wp) {
  883.         wp->w_flag |= WFHARD | WFMOVE | WFMODE;
  884.         wp = wp->w_wndp;
  885.     }
  886.     sgarbf = TRUE;
  887.  
  888.     return(TRUE);
  889. }
  890.  
  891. #if ! SMALLER
  892. int
  893. getwpos()    /* get screen offset of current line in current window */
  894. {
  895.     register int sline;    /* screen line from top of window */
  896.     register LINE *lp;    /* scannile line pointer */
  897.  
  898.     /* search down the line we want */
  899.     lp = curwp->w_line.l;
  900.     sline = 1;
  901.     while (lp != curwp->w_dot.l) {
  902.         ++sline;
  903.         lp = lforw(lp);
  904.     }
  905.  
  906.     /* and return the value */
  907.     return(sline);
  908. }
  909. #endif
  910.  
  911. /*
  912.  * Initialize all of the windows.
  913.  */
  914. void
  915. winit()
  916. {
  917.         register WINDOW *wp;
  918.  
  919.         wp = (WINDOW *) malloc(sizeof(WINDOW)); /* First window         */
  920.         if (wp==NULL )
  921.                 exit(1);
  922.         wheadp = wp;
  923.         curwp  = wp;
  924.         wp->w_wndp  = NULL;                     /* Initialize window    */
  925.         wp->w_dot  = nullmark;
  926.     wp->w_line = nullmark;
  927. #if WINMARK
  928.         wp->w_mark = nullmark;
  929. #endif
  930.         wp->w_lastdot = nullmark;
  931.         wp->w_toprow = 0;
  932.         wp->w_values = global_w_values;
  933.         wp->w_ntrows = term.t_nrow-1;           /* "-1" for mode line.  */
  934.         wp->w_force = 0;
  935.         wp->w_flag  = WFMODE|WFHARD;            /* Full.                */
  936.     wp->w_bufp = NULL;
  937. }
  938.  
  939.  
  940.