home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / src / baseline / jove-4.14.6.lha / jove-4.14.6 / wind.c < prev    next >
C/C++ Source or Header  |  1992-01-10  |  10KB  |  544 lines

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