home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / jove / part08 / wind.c < prev   
Encoding:
C/C++ Source or Header  |  1987-02-03  |  8.4 KB  |  439 lines

  1. /************************************************************************
  2.  * This program is Copyright (C) 1986 by Jonathan Payne.  JOVE is       *
  3.  * provided to you without charge, and with no warranty.  You may give  *
  4.  * away copies of JOVE, including sources, provided that this notice is *
  5.  * included in all the files.                                           *
  6.  ************************************************************************/
  7.  
  8. /* This creates/deletes/divides/grows/shrinks windows.  */
  9.  
  10. #include "jove.h"
  11. #include "termcap.h"
  12.  
  13. static char    onlyone[] = "You only have one window!",
  14.         toosmall[] = "Resulting window would be too small.";
  15.  
  16. Window    *curwind,
  17.     *fwind = 0;
  18.  
  19. /* First line in a Window */
  20.  
  21. FLine(w)
  22. register Window    *w;
  23. {
  24.     register Window    *wp = fwind;
  25.     register int    lineno = -1;
  26.  
  27.     do {
  28.         if (wp == w)
  29.             return lineno + 1;
  30.         lineno += wp->w_height;
  31.         wp = wp->w_next;
  32.     } while (wp != fwind);
  33.     complain("window?");
  34.     /* NOTREACHED */
  35. }
  36.  
  37. /* Delete `wp' from the screen.  If it is the only window left
  38.    on the screen, then complain.  It gives its body
  39.    to the next window if there is one, otherwise the previous
  40.    window gets the body.  */
  41.  
  42. del_wind(wp)
  43. register Window    *wp;
  44. {
  45.     register Window    *prev = wp->w_prev;
  46.  
  47.     if (one_windp())
  48.         complain(onlyone);
  49.  
  50.     wp->w_prev->w_next = wp->w_next;
  51.     wp->w_next->w_prev = wp->w_prev;
  52.     
  53.     if (fwind == wp) {
  54.         fwind = wp->w_next;
  55.         fwind->w_height += wp->w_height;
  56.         /* Here try to do something intelligent for redisplay() */
  57.         SetTop(fwind, prev_line(fwind->w_top, wp->w_height));
  58.         if (curwind == wp)
  59.             SetWind(fwind);
  60.     } else {
  61.         prev->w_height += wp->w_height;
  62.         if (curwind == wp)
  63.             SetWind(prev);
  64.     }
  65.     free((char *) wp);
  66. }
  67.  
  68. /* Divide the window WP N times, or at least once.  Complains if WP is too
  69.    small to be split into that many pieces.  It returns the new window. */
  70.  
  71. Window *
  72. div_wind(wp, n)
  73. register Window    *wp;
  74. {
  75.     register Window    *new;
  76.     int    amt;
  77.  
  78.     if (n < 1)
  79.         n = 1;
  80.     amt = wp->w_height / (n + 1);
  81.     if (amt < 2)
  82.         complain(toosmall);
  83.  
  84.     while (--n >= 0) {
  85.         new = (Window *) emalloc(sizeof (Window));
  86.         new->w_flags = 0;
  87.  
  88.         new->w_height = amt;
  89.         wp->w_height -= amt;
  90.  
  91.         /* set the lines such that w_line is the center in
  92.            each Window */
  93.         new->w_line = wp->w_line;
  94.         new->w_char = wp->w_char;
  95.         new->w_bufp = wp->w_bufp;
  96.         new->w_top = prev_line(new->w_line, HALF(new));
  97.  
  98.         /* Link the new window into the list */
  99.         new->w_prev = wp;
  100.         new->w_next = wp->w_next;
  101.         new->w_next->w_prev = new;
  102.         wp->w_next = new;
  103.     }
  104.     return new;
  105. }
  106.  
  107. /* Initialze the first window setting the bounds to the size of the
  108.    screen.  There is no buffer with this window.  See parse for the
  109.    setting of this window. */
  110.  
  111. winit()
  112. {
  113.     register Window    *w;
  114.  
  115.     w = curwind = fwind = (Window *) emalloc(sizeof (Window));
  116.     w->w_line = w->w_top = 0;
  117.     w->w_flags = 0;
  118.     w->w_char = 0;
  119.     w->w_next = w->w_prev = fwind;
  120.     w->w_height = ILI;
  121. }
  122.  
  123. /* Change to previous window. */
  124.  
  125. PrevWindow()
  126. {
  127.     register Window    *new = curwind->w_prev;
  128.  
  129.     if (one_windp())
  130.         complain(onlyone);
  131.     SetWind(new);
  132. }
  133.  
  134. /* Make NEW the current Window */
  135.  
  136. SetWind(new)
  137. register Window    *new;
  138. {
  139.     if (!Asking){        /* can you say kludge? */
  140.         curwind->w_line = curline;
  141.         curwind->w_char = curchar;
  142.         curwind->w_bufp = curbuf;
  143.     }
  144.     if (new == curwind)
  145.         return;
  146.     SetBuf(new->w_bufp);
  147.     if (!inlist(new->w_bufp->b_first, new->w_line)) {
  148.         new->w_line = curline;
  149.         new->w_char = curchar;
  150.     }
  151.     DotTo(new->w_line, new->w_char);
  152.     if (curchar > strlen(linebuf))
  153.         new->w_char = curchar = strlen(linebuf);
  154.     curwind = new;
  155. }
  156.  
  157. /* delete the current window if it isn't the only one left */
  158.  
  159. DelCurWindow()
  160. {
  161.     SetABuf(curwind->w_bufp);
  162.     del_wind(curwind);
  163. }
  164.  
  165. /* put the current line of `w' in the middle of the window */
  166.  
  167. CentWind(w)
  168. register Window    *w;
  169. {
  170.     SetTop(w, prev_line(w->w_line, HALF(w)));
  171. }
  172.  
  173. int    ScrollStep = 0;        /* full scrolling */
  174.  
  175. /* Calculate the new topline of the window.  If ScrollStep == 0
  176.    it means we should center the current line in the window. */
  177.  
  178. CalcWind(w)
  179. register Window    *w;
  180. {
  181.     register int    up;
  182.     Line    *newtop;
  183.  
  184.     if (ScrollStep == 0)    /* Means just center it */
  185.         CentWind(w);
  186.     else {
  187.         up = inorder(w->w_line, 0, w->w_top, 0);
  188.         if (up == -1) {
  189.             CentWind(w);
  190.             return;
  191.         }
  192.         if (up)        /* Dot is above the screen */
  193.             newtop = prev_line(w->w_line, min(ScrollStep - 1, HALF(w)));
  194.         else
  195.             newtop = prev_line(w->w_line, (SIZE(w) - 1) -
  196.                         min(ScrollStep - 1, HALF(w)));
  197.         if (LineDist(newtop, w->w_top) >= SIZE(w) - 1)
  198.             CentWind(w);
  199.         else
  200.             SetTop(w, newtop);
  201.     }
  202. }
  203.  
  204. /* This is bound to C-X 4 [BTF].  To make the screen stay the
  205.    same we have to remember various things, like the current
  206.    top line in the current window.  It's sorta gross, but it's
  207.    necessary because of the way this is implemented (i.e., in
  208.    terms of do_find(), do_select() which manipulate the windows. */
  209. WindFind()
  210. {
  211.     register Buffer    *obuf = curbuf,
  212.             *nbuf;
  213.     Line    *ltop = curwind->w_top;
  214.     Bufpos    savedot;
  215.     extern int
  216.         FindTag(),
  217.         BufSelect(),
  218.         FindFile();
  219.  
  220.     DOTsave(&savedot);
  221.  
  222.     switch (waitchar()) {
  223.     case 't':
  224.     case 'T':
  225.         ExecCmd((data_obj *) FindCmd(FindTag));
  226.         break;
  227.  
  228.     case 'b':
  229.     case 'B':
  230.         ExecCmd((data_obj *) FindCmd(BufSelect));
  231.         break;
  232.  
  233.     case 'f':
  234.     case 'F':
  235.         ExecCmd((data_obj *) FindCmd(FindFile));
  236.         break;
  237.  
  238.     default:
  239.         complain("T: find-tag, F: find-file, B: select-buffer.");
  240.     }
  241.  
  242.     nbuf = curbuf;
  243.     SetBuf(obuf);
  244.     SetDot(&savedot);
  245.     SetTop(curwind, ltop);    /* there! it's as if we did nothing */
  246.  
  247.     if (one_windp())
  248.         (void) div_wind(curwind, 1);
  249.  
  250.     tiewind(curwind->w_next, nbuf);
  251.     SetWind(curwind->w_next);
  252. }
  253.  
  254. /* Go into one window mode by deleting all the other windows */
  255.  
  256. OneWindow()
  257. {
  258.     while (curwind->w_next != curwind)
  259.         del_wind(curwind->w_next);
  260. }
  261.  
  262. Window *
  263. windbp(bp)
  264. register Buffer    *bp;
  265. {
  266.  
  267.     register Window    *wp = fwind;
  268.  
  269.     if (bp == 0)
  270.         return 0;
  271.     do {
  272.         if (wp->w_bufp == bp)
  273.             return wp;
  274.         wp = wp->w_next;
  275.     } while (wp != fwind);
  276.     return 0;
  277. }
  278.  
  279. /* Change window into the next window.  Curwind becomes the new window. */
  280.  
  281. NextWindow()
  282. {
  283.     register Window    *new = curwind->w_next;
  284.  
  285.     if (one_windp())
  286.         complain(onlyone);
  287.     SetWind(new);
  288. }
  289.  
  290. /* Scroll the next Window */
  291.  
  292. PageNWind()
  293. {
  294.     if (one_windp())
  295.         complain(onlyone);
  296.     NextWindow();
  297.     NextPage();
  298.     PrevWindow();
  299. }
  300.  
  301. Window *
  302. w_nam_typ(name, type)
  303. register char    *name;
  304. {
  305.     register Window *w;
  306.     register Buffer    *b;
  307.  
  308.     b = buf_exists(name);
  309.     w = fwind;
  310.     if (b) do {
  311.         if (w->w_bufp == b)
  312.             return w;
  313.     } while ((w = w->w_next) != fwind);
  314.  
  315.     w = fwind;
  316.     do {
  317.         if (w->w_bufp->b_type == type)
  318.             return w;
  319.     } while ((w = w->w_next) != fwind);
  320.  
  321.     return 0;
  322. }
  323.  
  324. /* Put a window with the buffer `name' in it.  Erase the buffer if
  325.    `clobber' is non-zero. */
  326.  
  327. pop_wind(name, clobber, btype)
  328. register char    *name;
  329. {
  330.     register Window    *wp;
  331.     register Buffer    *newb;
  332.  
  333.     if (newb = buf_exists(name))
  334.         btype = -1;    /* if the buffer exists, don't change
  335.                    it's type */
  336.     if ((wp = w_nam_typ(name, btype)) == 0) {
  337.         if (one_windp())
  338.             SetWind(div_wind(curwind, 1));
  339.         else
  340.             PrevWindow();
  341.     } else
  342.         SetWind(wp);
  343.  
  344.     newb = do_select((Window *) 0, name);
  345.     if (clobber) {
  346.         initlist(newb);
  347.         newb->b_modified = NO;
  348.     }
  349.     tiewind(curwind, newb);
  350.     if (btype != -1)
  351.         newb->b_type = btype;
  352.     SetBuf(newb);
  353. }
  354.  
  355. GrowWindow()
  356. {
  357.     WindSize(curwind, abs(exp));
  358. }
  359.  
  360. ShrWindow()
  361. {
  362.     WindSize(curwind, -abs(exp));
  363. }
  364.  
  365. /* Change the size of the window by inc.  First arg is the window,
  366.    second is the increment. */
  367.  
  368. WindSize(w, inc)
  369. register Window    *w;
  370. register int    inc;
  371. {
  372.     if (one_windp())
  373.         complain(onlyone);
  374.  
  375.     if (inc == 0)
  376.         return;
  377.     else if (inc < 0) {    /* Shrinking this Window. */
  378.         if (w->w_height + inc < 2)
  379.             complain(toosmall);
  380.         w->w_height += inc;
  381.         w->w_prev->w_height -= inc;
  382.     } else            /* Growing the window. */
  383.         WindSize(w->w_next, -inc);
  384. }
  385.  
  386. /* Set the topline of the window, calculating its number in the buffer.
  387.    This is for numbering the lines only. */
  388.  
  389. SetTop(w, line)
  390. Window    *w;
  391. register Line    *line;
  392. {
  393.     register Line    *lp = w->w_bufp->b_first;
  394.     register int    num = 0;
  395.  
  396.     w->w_top = line;
  397.     if (w->w_flags & W_NUMLINES) {
  398.         while (lp) {
  399.             num++;
  400.             if (line == lp)
  401.                 break;
  402.             lp = lp->l_next;
  403.         }
  404.         w->w_topnum = num;
  405.     }
  406. }
  407.  
  408. WNumLines()
  409. {
  410.     curwind->w_flags ^= W_NUMLINES; 
  411.     SetTop(curwind, curwind->w_top);
  412. }
  413.  
  414. WVisSpace()
  415. {
  416.     curwind->w_flags ^= W_VISSPACE;
  417.     ClAndRedraw();
  418. }
  419.  
  420. /* Return the line number that `line' occupies in `windes' */
  421.  
  422. in_window(windes, line)
  423. register Window    *windes;
  424. register Line    *line;
  425. {
  426.     register int    i;
  427.     register Line    *top = windes->w_top;
  428.  
  429.     for (i = 0; top && i < windes->w_height - 1; i++, top = top->l_next)
  430.         if (top == line)
  431.             return FLine(windes) + i;
  432.     return -1;
  433. }
  434.  
  435. SplitWind()
  436. {
  437.     SetWind(div_wind(curwind, exp_p ? (exp - 1) : 1));
  438. }
  439.