home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 21 / emacsrc / window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-05-14  |  11.8 KB  |  378 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.  
  6. #include        "ed.h"
  7.  
  8. /*
  9.  * Reposition dot in the current window to line "n". If the argument is
  10.  * positive, it is that line. If it is negative it is that line from the
  11.  * bottom. If it is 0 the window is centered (this is what the standard
  12.  * redisplay code does). With no argument it defaults to 1. Bound to M-!.
  13.  * Because of the default, it works like in Gosling.
  14.  */
  15. reposition(f, n)
  16.     {
  17.     curwp->w_force = n;
  18.     curwp->w_flag |= WFFORCE;
  19.     return (TRUE);
  20.     }
  21.  
  22. /*
  23.  * Refresh the screen. With no argument, it just does the refresh. With an
  24.  * argument it recenters "." in the current window. Bound to "C-L".
  25.  */
  26. refresh(f, n)
  27.     {
  28.     if (f == FALSE)
  29.         sgarbf = TRUE;
  30.     else
  31.         {
  32.         curwp->w_force = 0;             /* Center dot. */
  33.         curwp->w_flag |= WFFORCE;
  34.         }
  35.  
  36.     return (TRUE);
  37.     }
  38.  
  39. /*
  40.  * The command make the next window (next => down the screen) the current
  41.  * window. There are no real errors, although the command does nothing if
  42.  * there is only 1 window on the screen. Bound to "C-X C-N".
  43.  */
  44. nextwind(f, n)
  45.     {
  46.     register WINDOW *wp;
  47.  
  48.     if ((wp = curwp->w_wndp) == NULL)
  49.         wp = wheadp;
  50.  
  51.     curwp = wp;
  52.     curbp = wp->w_bufp;
  53.     return (TRUE);
  54.     }
  55.  
  56. /*
  57.  * This command makes the previous window (previous => up the screen) the
  58.  * current window. There arn't any errors, although the command does not do a
  59.  * lot if there is 1 window.
  60.  */
  61. prevwind(f, n)
  62.     {
  63.     register WINDOW *wp1;
  64.     register WINDOW *wp2;
  65.  
  66.     wp1 = wheadp;
  67.     wp2 = curwp;
  68.  
  69.     if (wp1 == wp2)
  70.         wp2 = NULL;
  71.  
  72.     while (wp1->w_wndp != wp2)
  73.         wp1 = wp1->w_wndp;
  74.  
  75.     curwp = wp1;
  76.     curbp = wp1->w_bufp;
  77.     return (TRUE);
  78.     }
  79.  
  80. /*
  81.  * This command moves the current window down by "arg" lines. Recompute the
  82.  * top line in the window. The move up and move down code is almost completely
  83.  * the same; most of the work has to do with reframing the window, and picking
  84.  * a new dot. We share the code by having "move down" just be an interface to
  85.  * "move up". Magic. Bound to "C-X C-N".
  86.  */
  87. mvdnwind(f, n)
  88.     int n;
  89.     {
  90.     return (mvupwind(f, -n));
  91.     }
  92.  
  93. /*
  94.  * Move the current window up by "arg" lines. Recompute the new top line of
  95.  * the window. Look to see if "." is still on the screen. If it is, you win.
  96.  * If it isn't, then move "." to center it in the new framing of the window
  97.  * (this command does not really move "."; it moves the frame). Bound to
  98.  * "C-X C-P".
  99.  */
  100. mvupwind(f, n)
  101.     int n;
  102.     {
  103.     register LINE *lp;
  104.     register int i;
  105.  
  106.     lp = curwp->w_linep;
  107.  
  108.     if (n < 0)
  109.         {
  110.         while (n++ && lp!=curbp->b_linep)
  111.             lp = lforw(lp);
  112.         }
  113.     else
  114.         {
  115.         while (n-- && lback(lp)!=curbp->b_linep)
  116.             lp = lback(lp);
  117.         }
  118.  
  119.     curwp->w_linep = lp;
  120.     curwp->w_flag |= WFHARD;            /* Mode line is OK. */
  121.  
  122.     for (i = 0; i < curwp->w_ntrows; ++i)
  123.         {
  124.         if (lp == curwp->w_dotp)
  125.             return (TRUE);
  126.         if (lp == curbp->b_linep)
  127.             break;
  128.         lp = lforw(lp);
  129.         }
  130.  
  131.     lp = curwp->w_linep;
  132.     i  = curwp->w_ntrows/2;
  133.  
  134.     while (i-- && lp != curbp->b_linep)
  135.         lp = lforw(lp);
  136.  
  137.     curwp->w_dotp  = lp;
  138.     curwp->w_doto  = 0;
  139.     return (TRUE);
  140.     }
  141.  
  142. /*
  143.  * This command makes the current window the only window on the screen. Bound
  144.  * to "C-X 1". Try to set the framing so that "." does not have to move on the
  145.  * display. Some care has to be taken to keep the values of dot and mark in
  146.  * the buffer structures right if the distruction of a window makes a buffer
  147.  * become undisplayed.
  148.  */
  149. onlywind(f, n)
  150. {
  151.         register WINDOW *wp;
  152.         register LINE   *lp;
  153.         register int    i;
  154.  
  155.         while (wheadp != curwp) {
  156.                 wp = wheadp;
  157.                 wheadp = wp->w_wndp;
  158.                 if (--wp->w_bufp->b_nwnd == 0) {
  159.                         wp->w_bufp->b_dotp  = wp->w_dotp;
  160.                         wp->w_bufp->b_doto  = wp->w_doto;
  161.                         wp->w_bufp->b_markp = wp->w_markp;
  162.                         wp->w_bufp->b_marko = wp->w_marko;
  163.                 }
  164.                 free((char *) wp);
  165.         }
  166.         while (curwp->w_wndp != NULL) {
  167.                 wp = curwp->w_wndp;
  168.                 curwp->w_wndp = wp->w_wndp;
  169.                 if (--wp->w_bufp->b_nwnd == 0) {
  170.                         wp->w_bufp->b_dotp  = wp->w_dotp;
  171.                         wp->w_bufp->b_doto  = wp->w_doto;
  172.                         wp->w_bufp->b_markp = wp->w_markp;
  173.                         wp->w_bufp->b_marko = wp->w_marko;
  174.                 }
  175.                 free((char *) wp);
  176.         }
  177.         lp = curwp->w_linep;
  178.         i  = curwp->w_toprow;
  179.         while (i!=0 && lback(lp)!=curbp->b_linep) {
  180.                 --i;
  181.                 lp = lback(lp);
  182.         }
  183.         curwp->w_toprow = 0;
  184.         curwp->w_ntrows = term.t_nrow-1;
  185.         curwp->w_linep  = lp;
  186.         curwp->w_flag  |= WFMODE|WFHARD;
  187.         return (TRUE);
  188. }
  189.  
  190. /*
  191.  * Split the current window. A window smaller than 3 lines cannot be split.
  192.  * The only other error that is possible is a "malloc" failure allocating the
  193.  * structure for the new window. Bound to "C-X 2".
  194.  */
  195. splitwind(f, n)
  196. {
  197.         register WINDOW *wp;
  198.         register LINE   *lp;
  199.         register int    ntru;
  200.         register int    ntrl;
  201.         register int    ntrd;
  202.         register WINDOW *wp1;
  203.         register WINDOW *wp2;
  204.  
  205.         if (curwp->w_ntrows < 3) {
  206.                 mlwrite("Cannot split a %d line window", curwp->w_ntrows);
  207.                 return (FALSE);
  208.         }
  209.         if ((wp = (WINDOW *) malloc(sizeof(WINDOW))) == NULL) {
  210.                 mlwrite("Cannot allocate WINDOW block");
  211.                 return (FALSE);
  212.         }
  213.         ++curbp->b_nwnd;                        /* Displayed twice.     */
  214.         wp->w_bufp  = curbp;
  215.         wp->w_dotp  = curwp->w_dotp;
  216.         wp->w_doto  = curwp->w_doto;
  217.         wp->w_markp = curwp->w_markp;
  218.         wp->w_marko = curwp->w_marko;
  219.         wp->w_flag  = 0;
  220.         wp->w_force = 0;
  221.         ntru = (curwp->w_ntrows-1) / 2;         /* Upper size           */
  222.         ntrl = (curwp->w_ntrows-1) - ntru;      /* Lower size           */
  223.         lp = curwp->w_linep;
  224.         ntrd = 0;
  225.         while (lp != curwp->w_dotp) {
  226.                 ++ntrd;
  227.                 lp = lforw(lp);
  228.         }
  229.         lp = curwp->w_linep;
  230.         if (ntrd <= ntru) {                     /* Old is upper window. */
  231.                 if (ntrd == ntru)               /* Hit mode line.       */
  232.                         lp = lforw(lp);
  233.                 curwp->w_ntrows = ntru;
  234.                 wp->w_wndp = curwp->w_wndp;
  235.                 curwp->w_wndp = wp;
  236.                 wp->w_toprow = curwp->w_toprow+ntru+1;
  237.                 wp->w_ntrows = ntrl;
  238.         } else {                                /* Old is lower window  */
  239.                 wp1 = NULL;
  240.                 wp2 = wheadp;
  241.                 while (wp2 != curwp) {
  242.                         wp1 = wp2;
  243.                         wp2 = wp2->w_wndp;
  244.                 }
  245.                 if (wp1 == NULL)
  246.                         wheadp = wp;
  247.                 else
  248.                         wp1->w_wndp = wp;
  249.                 wp->w_wndp   = curwp;
  250.                 wp->w_toprow = curwp->w_toprow;
  251.                 wp->w_ntrows = ntru;
  252.                 ++ntru;                         /* Mode line.           */
  253.                 curwp->w_toprow += ntru;
  254.                 curwp->w_ntrows  = ntrl;
  255.                 while (ntru--)
  256.                         lp = lforw(lp);
  257.         }
  258.         curwp->w_linep = lp;                    /* Adjust the top lines */
  259.         wp->w_linep = lp;                       /* if necessary.        */
  260.         curwp->w_flag |= WFMODE|WFHARD;
  261.         wp->w_flag |= WFMODE|WFHARD;
  262.         return (TRUE);
  263. }
  264.  
  265. /*
  266.  * Enlarge the current window. Find the window that loses space. Make sure it
  267.  * is big enough. If so, hack the window descriptions, and ask redisplay to do
  268.  * all the hard work. You don't just set "force reframe" because dot would
  269.  * move. Bound to "C-X Z".
  270.  */
  271. enlargewind(f, n)
  272. {
  273.         register WINDOW *adjwp;
  274.         register LINE   *lp;
  275.         register int    i;
  276.  
  277.         if (n < 0)
  278.                 return (shrinkwind(f, -n));
  279.         if (wheadp->w_wndp == NULL) {
  280.                 mlwrite("Only one window");
  281.                 return (FALSE);
  282.         }
  283.         if ((adjwp=curwp->w_wndp) == NULL) {
  284.                 adjwp = wheadp;
  285.                 while (adjwp->w_wndp != curwp)
  286.                         adjwp = adjwp->w_wndp;
  287.         }
  288.         if (adjwp->w_ntrows <= n) {
  289.                 mlwrite("Impossible change");
  290.                 return (FALSE);
  291.         }
  292.         if (curwp->w_wndp == adjwp) {           /* Shrink below.        */
  293.                 lp = adjwp->w_linep;
  294.                 for (i=0; i<n && lp!=adjwp->w_bufp->b_linep; ++i)
  295.                         lp = lforw(lp);
  296.                 adjwp->w_linep  = lp;
  297.                 adjwp->w_toprow += n;
  298.         } else {                                /* Shrink above.        */
  299.                 lp = curwp->w_linep;
  300.                 for (i=0; i<n && lback(lp)!=curbp->b_linep; ++i)
  301.                         lp = lback(lp);
  302.                 curwp->w_linep  = lp;
  303.                 curwp->w_toprow -= n;
  304.         }
  305.         curwp->w_ntrows += n;
  306.         adjwp->w_ntrows -= n;
  307.         curwp->w_flag |= WFMODE|WFHARD;
  308.         adjwp->w_flag |= WFMODE|WFHARD;
  309.         return (TRUE);
  310. }
  311.  
  312. /*
  313.  * Shrink the current window. Find the window that gains space. Hack at the
  314.  * window descriptions. Ask the redisplay to do all the hard work. Bound to
  315.  * "C-X C-Z".
  316.  */
  317. shrinkwind(f, n)
  318. {
  319.         register WINDOW *adjwp;
  320.         register LINE   *lp;
  321.         register int    i;
  322.  
  323.         if (n < 0)
  324.                 return (enlargewind(f, -n));
  325.         if (wheadp->w_wndp == NULL) {
  326.                 mlwrite("Only one window");
  327.                 return (FALSE);
  328.         }
  329.         if ((adjwp=curwp->w_wndp) == NULL) {
  330.                 adjwp = wheadp;
  331.                 while (adjwp->w_wndp != curwp)
  332.                         adjwp = adjwp->w_wndp;
  333.         }
  334.         if (curwp->w_ntrows <= n) {
  335.                 mlwrite("Impossible change");
  336.                 return (FALSE);
  337.         }
  338.         if (curwp->w_wndp == adjwp) {           /* Grow below.          */
  339.                 lp = adjwp->w_linep;
  340.                 for (i=0; i<n && lback(lp)!=adjwp->w_bufp->b_linep; ++i)
  341.                         lp = lback(lp);
  342.                 adjwp->w_linep  = lp;
  343.                 adjwp->w_toprow -= n;
  344.         } else {                                /* Grow above.          */
  345.                 lp = curwp->w_linep;
  346.                 for (i=0; i<n && lp!=curbp->b_linep; ++i)
  347.                         lp = lforw(lp);
  348.                 curwp->w_linep  = lp;
  349.                 curwp->w_toprow += n;
  350.         }
  351.         curwp->w_ntrows -= n;
  352.         adjwp->w_ntrows += n;
  353.         curwp->w_flag |= WFMODE|WFHARD;
  354.         adjwp->w_flag |= WFMODE|WFHARD;
  355.         return (TRUE);
  356. }
  357.  
  358. /*
  359.  * Pick a window for a pop-up. Split the screen if there is only one window.
  360.  * Pick the uppermost window that isn't the current window. An LRU algorithm
  361.  * might be better. Return a pointer, or NULL on error.
  362.  */
  363. WINDOW  *
  364. wpopup()
  365. {
  366.         register WINDOW *wp;
  367.  
  368.         if (wheadp->w_wndp == NULL              /* Only 1 window        */
  369.         && splitwind(FALSE, 0) == FALSE)        /* and it won't split   */
  370.                 return (NULL);
  371.         wp = wheadp;                            /* Find window to use   */
  372.         while (wp!=NULL && wp==curwp)
  373.                 wp = wp->w_wndp;
  374.         return (wp);
  375. }
  376.  
  377. /* -eof- */
  378.