home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / jove414s.zip / wind.c < prev   
C/C++ Source or Header  |  1991-07-06  |  11KB  |  547 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. #include <stdio.h>
  15.  
  16. private char    onlyone[] = "You only have one window!",
  17.         toosmall[] = "Resulting window would be too small.";
  18.  
  19. Window    *curwind,
  20.     *fwind = 0;
  21.  
  22. /* First line in a Window */
  23.  
  24. int
  25. FLine(w)
  26. /* register */ Window    *w;
  27. {
  28.     /* register */ Window    *wp = fwind;
  29.     /* register */ int    lineno = -1;
  30.  
  31.     do {
  32.         if (wp == w)
  33.             return lineno + 1;
  34.         lineno += wp->w_height;
  35.         wp = wp->w_next;
  36.     } while (wp != fwind);
  37.     complain("window?");
  38.     /* NOTREACHED */
  39. }
  40.  
  41. /* Delete `wp' from the screen.  If it is the only window left
  42.    on the screen, then complain.  It gives its body
  43.    to the next window if there is one, otherwise the previous
  44.    window gets the body.  */
  45.  
  46. void
  47. del_wind(wp)
  48. /* register */ Window    *wp;
  49. {
  50.     /* register */ Window    *prev = wp->w_prev;
  51.  
  52.     if (one_windp())
  53.         complain(onlyone);
  54.  
  55.     prev->w_next = wp->w_next;
  56.     wp->w_next->w_prev = prev;
  57.  
  58.     if (fwind == wp) {
  59.         fwind = wp->w_next;
  60.         fwind->w_height += wp->w_height;
  61.         /* Here try to do something intelligent for redisplay() */
  62.         SetTop(fwind, prev_line(fwind->w_top, wp->w_height));
  63.         if (curwind == wp)
  64.             SetWind(fwind);
  65.     } else {
  66.         prev->w_height += wp->w_height;
  67.         if (curwind == wp)
  68.             SetWind(prev);
  69.     }
  70. #ifdef MAC
  71.     RemoveScrollBar(wp);
  72.     Windchange++;
  73. #endif
  74.     free((char *) wp);
  75. }
  76.  
  77. /* Divide the window WP N times, or at least once.  Complains if WP is too
  78.    small to be split into that many pieces.  It returns the new window. */
  79.  
  80. Window *
  81. div_wind(wp, n)
  82. /* register */ Window    *wp;
  83. int    n;
  84. {
  85.     /* register */ Window    *new;
  86.     int    amt;
  87.  
  88.     if (n < 1)
  89.         n = 1;
  90.     amt = wp->w_height / (n + 1);
  91.     if (amt < 2)
  92.         complain(toosmall);
  93.  
  94.     while (--n >= 0) {
  95.         new = (Window *) emalloc(sizeof (Window));
  96.         new->w_flags = 0;
  97.         new->w_LRscroll = 0;
  98.  
  99.         new->w_height = amt;
  100.         wp->w_height -= amt;
  101.  
  102.         /* set the lines such that w_line is the center in
  103.            each Window */
  104.         new->w_line = wp->w_line;
  105.         new->w_char = wp->w_char;
  106.         new->w_bufp = wp->w_bufp;
  107.         new->w_top = prev_line(new->w_line, HALF(new));
  108.  
  109.         /* Link the new window into the list */
  110.         new->w_prev = wp;
  111.         new->w_next = wp->w_next;
  112.         new->w_next->w_prev = new;
  113.         wp->w_next = new;
  114. #ifdef MAC
  115.         new->w_control = 0;
  116. #endif
  117.     }
  118. #ifdef MAC
  119.     Windchange++;
  120. #endif
  121.     return new;
  122. }
  123.  
  124. /* Initialze the first window setting the bounds to the size of the
  125.    screen.  There is no buffer with this window.  See parse for the
  126.    setting of this window. */
  127.  
  128. void
  129. winit()
  130. {
  131.     /* register */ Window    *w;
  132.  
  133.     w = curwind = fwind = (Window *) emalloc(sizeof (Window));
  134.     w->w_line = w->w_top = 0;
  135.     w->w_LRscroll = 0;
  136.     w->w_flags = 0;
  137.     w->w_char = 0;
  138.     w->w_next = w->w_prev = fwind;
  139.     w->w_height = ILI;
  140. #ifdef MAC
  141.     w->w_control = 0;
  142.     Windchange++;
  143. #endif
  144. }
  145.  
  146. /* Change to previous window. */
  147.  
  148. void
  149. PrevWindow()
  150. {
  151.     /* register */ Window    *new = curwind->w_prev;
  152.  
  153.     if (Asking)
  154.         complain((char *) 0);
  155.     if (one_windp())
  156.         complain(onlyone);
  157.     SetWind(new);
  158. }
  159.  
  160. /* Make NEW the current Window */
  161.  
  162. void
  163. SetWind(new)
  164. /* register */ Window    *new;
  165. {
  166.     if (!Asking && curbuf!=NULL) {        /* can you say kludge? */
  167.         curwind->w_line = curline;
  168.         curwind->w_char = curchar;
  169.         curwind->w_bufp = curbuf;
  170.     }
  171.     if (new == curwind)
  172.         return;
  173.     SetBuf(new->w_bufp);
  174.     if (!inlist(new->w_bufp->b_first, new->w_line)) {
  175.         new->w_line = curline;
  176.         new->w_char = curchar;
  177.     }
  178.     DotTo(new->w_line, new->w_char);
  179.     if (curchar > (int)strlen(linebuf))
  180.         new->w_char = curchar = strlen(linebuf);
  181.     curwind = new;
  182. }
  183.  
  184. /* delete the current window if it isn't the only one left */
  185.  
  186. void
  187. DelCurWindow()
  188. {
  189.     SetABuf(curwind->w_bufp);
  190.     del_wind(curwind);
  191. }
  192.  
  193. /* put the current line of `w' in the middle of the window */
  194.  
  195. void
  196. CentWind(w)
  197. /* register */ Window    *w;
  198. {
  199.     SetTop(w, prev_line(w->w_line, HALF(w)));
  200. }
  201.  
  202. int    ScrollStep = 0;        /* full scrolling */
  203.  
  204. /* Calculate the new topline of the window.  If ScrollStep == 0
  205.    it means we should center the current line in the window. */
  206.  
  207. void
  208. CalcWind(w)
  209. /* register */ Window    *w;
  210. {
  211.     /* register */ int    up;
  212.     int    scr_step;
  213.     Line    *newtop;
  214.  
  215.     if (ScrollStep == 0)    /* Means just center it */
  216.         CentWind(w);
  217.     else {
  218.         up = inorder(w->w_line, 0, w->w_top, 0);
  219.         if (up == -1) {
  220.             CentWind(w);
  221.             return;
  222.         }
  223.         scr_step = (ScrollStep < 0) ? SIZE(w) + ScrollStep :
  224.                ScrollStep - 1;
  225.         if (up)        /* point is above the screen */
  226.             newtop = prev_line(w->w_line, scr_step);
  227.         else
  228.             newtop = prev_line(w->w_line, (SIZE(w) - 1 - scr_step));
  229.         if (LineDist(newtop, w->w_top) >= SIZE(w) - 1)
  230.             CentWind(w);
  231.         else
  232.             SetTop(w, newtop);
  233.     }
  234. }
  235.  
  236. /* This is bound to C-X 4 [BTF].  To make the screen stay the
  237.    same we have to remember various things, like the current
  238.    top line in the current window.  It's sorta gross, but it's
  239.    necessary because of the way this is implemented (i.e., in
  240.    terms of do_find(), do_select() which manipulate the windows. */
  241.  
  242. void
  243. WindFind()
  244. {
  245.     /* register */ Buffer    *obuf = curbuf,
  246.                 *nbuf;
  247.     Line    *ltop = curwind->w_top;
  248.     Bufpos    odot,
  249.         ndot;
  250.  
  251.     DOTsave(&odot);
  252.  
  253.     switch (waitchar((int *) 0)) {
  254.     case 't':
  255.     case 'T':
  256.         ExecCmd((data_obj *) FindCmd(FindTag));
  257.         break;
  258.  
  259.     case CTL('T'):
  260.         ExecCmd((data_obj *) FindCmd(FDotTag));
  261.         break;
  262.  
  263.     case 'b':
  264.     case 'B':
  265.         ExecCmd((data_obj *) FindCmd(BufSelect));
  266.         break;
  267.  
  268.     case 'f':
  269.     case 'F':
  270.         ExecCmd((data_obj *) FindCmd(FindFile));
  271.         break;
  272.  
  273.     default:
  274.         complain("T: find-tag, ^T: find-tag-at-point, F: find-file, B: select-buffer.");
  275.     }
  276.  
  277.     nbuf = curbuf;
  278.     DOTsave(&ndot);
  279.     SetBuf(obuf);
  280.     SetDot(&odot);
  281.     SetTop(curwind, ltop);    /* there! it's as if we did nothing */
  282.  
  283.     if (one_windp())
  284.         (void) div_wind(curwind, 1);
  285.  
  286.     tiewind(curwind->w_next, nbuf);
  287.     SetWind(curwind->w_next);
  288.     SetDot(&ndot);
  289. }
  290.  
  291. /* Go into one window mode by deleting all the other windows */
  292.  
  293. void
  294. OneWindow()
  295. {
  296.     while (curwind->w_next != curwind)
  297.         del_wind(curwind->w_next);
  298. }
  299.  
  300. Window *
  301. windbp(bp)
  302. /* register */ Buffer    *bp;
  303. {
  304.  
  305.     /* register */ Window    *wp = fwind;
  306.  
  307.     if (bp == 0)
  308.         return 0;
  309.     do {
  310.         if (wp->w_bufp == bp)
  311.             return wp;
  312.         wp = wp->w_next;
  313.     } while (wp != fwind);
  314.     return 0;
  315. }
  316.  
  317. /* Change window into the next window.  Curwind becomes the new window. */
  318.  
  319. void
  320. NextWindow()
  321. {
  322.     /* register */ Window    *new = curwind->w_next;
  323.  
  324.     if (Asking)
  325.         complain((char *) 0);
  326.     if (one_windp())
  327.         complain(onlyone);
  328.     SetWind(new);
  329. }
  330.  
  331. /* Scroll the next Window */
  332.  
  333. void
  334. PageNWind()
  335. {
  336.     if (one_windp())
  337.         complain(onlyone);
  338.     NextWindow();
  339.     NextPage();
  340.     PrevWindow();
  341. }
  342.  
  343. Window *
  344. w_nam_typ(name, type)
  345. /* register */ char    *name;
  346. int    type;
  347. {
  348.     /* register */ Window *w;
  349.     /* register */ Buffer    *b;
  350.  
  351.     b = buf_exists(name);
  352.     w = fwind;
  353.     if (b) do {
  354.         if (w->w_bufp == b)
  355.             return w;
  356.     } while ((w = w->w_next) != fwind);
  357.  
  358.     w = fwind;
  359.     do {
  360.         if (w->w_bufp->b_type == type)
  361.             return w;
  362.     } while ((w = w->w_next) != fwind);
  363.  
  364.     return 0;
  365. }
  366.  
  367. /* Put a window with the buffer `name' in it.  Erase the buffer if
  368.    `clobber' is non-zero. */
  369.  
  370. void
  371. pop_wind(name, clobber, btype)
  372. /* register */ char    *name;
  373. int    clobber;
  374. int    btype;
  375. {
  376.     /* register */ Window    *wp;
  377.     /* register */ Buffer    *newb;
  378.  
  379.     if ((newb = buf_exists(name)) != NIL)
  380.         btype = -1;    /* if the buffer exists, don't change
  381.                    it's type */
  382.     if ((wp = w_nam_typ(name, btype)) == 0) {
  383.         if (one_windp())
  384.             SetWind(div_wind(curwind, 1));
  385.         else
  386.             PrevWindow();
  387.     } else
  388.         SetWind(wp);
  389.  
  390.     newb = do_select((Window *) 0, name);
  391.     if (clobber) {
  392.         initlist(newb);
  393.         newb->b_modified = NO;
  394.     }
  395.     tiewind(curwind, newb);
  396.     if (btype != -1)
  397.         newb->b_type = btype;
  398.     SetBuf(newb);
  399. }
  400.  
  401. void
  402. GrowWindow()
  403. {
  404.     WindSize(curwind, abs(arg_value()));
  405. }
  406.  
  407. void
  408. ShrWindow()
  409. {
  410.     WindSize(curwind, -abs(arg_value()));
  411. }
  412.  
  413. /* Change the size of the window by inc.  First arg is the window,
  414.    second is the increment. */
  415.  
  416. void
  417. WindSize(w, inc)
  418. /* register */ Window    *w;
  419. /* register */ int    inc;
  420. {
  421.     if (one_windp())
  422.         complain(onlyone);
  423.  
  424.     if (inc == 0)
  425.         return;
  426.     else if (inc < 0) {    /* Shrinking this Window. */
  427.         if (w->w_height + inc < 2)
  428.             complain(toosmall);
  429.         w->w_height += inc;
  430.         w->w_prev->w_height -= inc;
  431.     } else {        /* Growing the window. */
  432.         /* Change made from original code so that growing a window
  433.            exactly offsets effect of shrinking a window, i.e.
  434.            doing either followed by the other restores original
  435.            sizes of all affected windows. */
  436.         if (w->w_prev->w_height - inc < 2)
  437.             complain(toosmall);
  438.         w->w_height += inc;
  439.         w->w_prev->w_height -= inc;
  440.     }
  441. #ifdef MAC
  442.     Windchange++;
  443. #endif
  444. }
  445.  
  446. /* Set the topline of the window, calculating its number in the buffer.
  447.    This is for numbering the lines only. */
  448.  
  449. void
  450. SetTop(w, line)
  451. Window    *w;
  452. /* register */ Line    *line;
  453. {
  454.     /* register */ Line    *lp = w->w_bufp->b_first;
  455.     /* register */ int    num = 0;
  456.  
  457.     w->w_top = line;
  458.     if (w->w_flags & W_NUMLINES) {
  459.         while (lp) {
  460.             num += 1;
  461.             if (line == lp)
  462.                 break;
  463.             lp = lp->l_next;
  464.         }
  465.         w->w_topnum = num;
  466.     }
  467. }
  468.  
  469. void
  470. WNumLines()
  471. {
  472.     curwind->w_flags ^= W_NUMLINES;
  473.     SetTop(curwind, curwind->w_top);
  474. }
  475.  
  476. void
  477. WVisSpace()
  478. {
  479.     curwind->w_flags ^= W_VISSPACE;
  480.     ClAndRedraw();
  481. }
  482.  
  483. /* Return the line number that `line' occupies in `windes' */
  484.  
  485. int
  486. in_window(windes, line)
  487. /* register */ Window    *windes;
  488. /* register */ Line    *line;
  489. {
  490.     /* register */ int    i;
  491.     /* register */ Line    *top = windes->w_top;
  492.  
  493.     for (i = 0; top && i < windes->w_height - 1; i++, top = top->l_next)
  494.         if (top == line)
  495.             return FLine(windes) + i;
  496.     return -1;
  497. }
  498.  
  499. void
  500. SplitWind()
  501. {
  502.     SetWind(div_wind(curwind, is_an_arg() ? (arg_value() - 1) : 1));
  503. }
  504.  
  505. /* Goto the window with the named buffer.  If no such window
  506.    exists, pop one and attach the buffer to it. */
  507. void
  508. GotoWind()
  509. {
  510.     char    *bname;
  511.     Window    *w;
  512.  
  513.     bname = ask_buf(lastbuf);
  514.     w = curwind->w_next;
  515.     do {
  516.         if (w->w_bufp->b_name == bname) {
  517.             SetABuf(curbuf);
  518.             SetWind(w);
  519.             return;
  520.         }
  521.         w = w->w_next;
  522.     } while (w != curwind);
  523.     SetABuf(curbuf);
  524.     pop_wind(bname, NO, -1);
  525. }
  526.  
  527. void
  528. ScrollRight()
  529. {
  530.     int    amt = (is_an_arg() ? arg_value() : 10);
  531.  
  532.     if (curwind->w_LRscroll - amt < 0)
  533.         curwind->w_LRscroll = 0;
  534.     else
  535.         curwind->w_LRscroll -= amt;
  536.     UpdModLine = YES;
  537. }
  538.  
  539. void
  540. ScrollLeft()
  541. {
  542.     int    amt = (is_an_arg() ? arg_value() : 10);
  543.  
  544.     curwind->w_LRscroll += amt;
  545.     UpdModLine = YES;
  546. }
  547.