[BACK]Return to window.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Annotation of src/usr.bin/mg/window.c, Revision 1.4

1.1       deraadt     1: /*
                      2:  *             Window handling.
                      3:  */
1.4     ! millert     4:
        !             5: #include "def.h"
1.1       deraadt     6:
                      7: /*
1.4     ! millert     8:  * Reposition dot in the current window to line "n".  If the argument is
        !             9:  * positive, it is that line.  If it is negative it is that line from the
        !            10:  * bottom.  If it is 0 the window is centered (this is what the standard
        !            11:  * redisplay code does).  If GOSREC is undefined, default is 0, so it acts
        !            12:  * like GNU.  If GOSREC is defined, with no argument it defaults to 1 and
        !            13:  * works like in Gosling.
1.1       deraadt    14:  */
1.3       millert    15: /* ARGSUSED */
1.4     ! millert    16: int
1.1       deraadt    17: reposition(f, n)
1.4     ! millert    18:        int f, n;
1.1       deraadt    19: {
                     20: #ifndef GOSREC
1.3       millert    21:        curwp->w_force = (f & FFARG) ? (n >= 0 ? n + 1 : n) : 0;
1.4     ! millert    22: #else /* !GOSREC */
1.1       deraadt    23:        curwp->w_force = n;
1.4     ! millert    24: #endif /* !GOSREC */
1.1       deraadt    25:        curwp->w_flag |= WFFORCE;
                     26:        sgarbf = TRUE;
                     27:        return TRUE;
                     28: }
                     29:
                     30: /*
1.4     ! millert    31:  * Refresh the display.  A call is made to the "ttresize" entry in the
        !            32:  * terminal handler, which tries to reset "nrow" and "ncol".  They will,
        !            33:  * however, never be set outside of the NROW or NCOL range.  If the display
        !            34:  * changed size, arrange that everything is redone, then call "update" to
        !            35:  * fix the display.  We do this so the new size can be displayed.  In the
        !            36:  * normal case the call to "update" in "main.c" refreshes the screen, and
        !            37:  * all of the windows need not be recomputed.  Note that when you get to the
        !            38:  * "display unusable" message, the screen will be messed up. If you make the
        !            39:  * window bigger again, and send another command, everything will get fixed!
1.1       deraadt    40:  */
1.3       millert    41: /* ARGSUSED */
1.4     ! millert    42: int
1.1       deraadt    43: refresh(f, n)
1.4     ! millert    44:        int f, n;
1.1       deraadt    45: {
1.4     ! millert    46:        MGWIN   *wp;
        !            47:        int      oldnrow;
        !            48:        int      oldncol;
1.1       deraadt    49:
                     50:        oldnrow = nrow;
                     51:        oldncol = ncol;
                     52:        ttresize();
1.3       millert    53:        if (nrow != oldnrow || ncol != oldncol) {
1.4     ! millert    54:
        !            55:                /* find last */
        !            56:                wp = wheadp;
1.1       deraadt    57:                while (wp->w_wndp != NULL)
                     58:                        wp = wp->w_wndp;
1.4     ! millert    59:
        !            60:                /* check if too small */
        !            61:                if (nrow < wp->w_toprow + 3) {
1.1       deraadt    62:                        ewprintf("Display unusable");
                     63:                        return (FALSE);
                     64:                }
1.3       millert    65:                wp->w_ntrows = nrow - wp->w_toprow - 2;
1.1       deraadt    66:                sgarbf = TRUE;
                     67:                update();
                     68:                ewprintf("New size %d by %d", nrow, ncol);
                     69:        } else
                     70:                sgarbf = TRUE;
                     71:        return TRUE;
                     72: }
                     73:
                     74: /*
1.4     ! millert    75:  * The command to make the next window (next => down the screen) the current
        !            76:  * window. There are no real errors, although the command does nothing if
        !            77:  * there is only 1 window on the screen.
1.1       deraadt    78:  */
1.3       millert    79: /* ARGSUSED */
1.4     ! millert    80: int
1.1       deraadt    81: nextwind(f, n)
1.4     ! millert    82:        int f, n;
1.1       deraadt    83: {
1.4     ! millert    84:        MGWIN   *wp;
1.1       deraadt    85:
1.3       millert    86:        if ((wp = curwp->w_wndp) == NULL)
1.1       deraadt    87:                wp = wheadp;
                     88:        curwp = wp;
                     89:        curbp = wp->w_bufp;
                     90:        return TRUE;
                     91: }
                     92:
                     93: #ifdef GOSMACS
1.4     ! millert    94:
1.1       deraadt    95: /* not in Gnu Emacs */
                     96: /*
1.4     ! millert    97:  * This command makes the previous window (previous => up the screen) the
        !            98:  * current window. There are no errors, although the command does not do
        !            99:  * a lot if there is only 1 window.
1.1       deraadt   100:  */
1.3       millert   101: /* ARGSUSED */
1.4     ! millert   102: int
1.1       deraadt   103: prevwind(f, n)
1.4     ! millert   104:        int f, n;
1.1       deraadt   105: {
1.4     ! millert   106:        MGWIN   *wp1, *wp2;
1.1       deraadt   107:
                    108:        wp1 = wheadp;
                    109:        wp2 = curwp;
                    110:        if (wp1 == wp2)
                    111:                wp2 = NULL;
                    112:        while (wp1->w_wndp != wp2)
                    113:                wp1 = wp1->w_wndp;
                    114:        curwp = wp1;
                    115:        curbp = wp1->w_bufp;
                    116:        return TRUE;
                    117: }
1.4     ! millert   118: #endif /* GOSEMACS */
1.1       deraadt   119:
                    120: /*
1.4     ! millert   121:  * This command makes the current window the only window on the screen.  Try
        !           122:  * to set the framing so that "." does not have to move on the display.  Some
        !           123:  * care has to be taken to keep the values of dot and mark in the buffer
        !           124:  * structures right if the distruction of a window makes a buffer become
        !           125:  * undisplayed.
1.1       deraadt   126:  */
1.3       millert   127: /* ARGSUSED */
1.4     ! millert   128: int
1.1       deraadt   129: onlywind(f, n)
1.4     ! millert   130:        int f, n;
1.1       deraadt   131: {
1.4     ! millert   132:        MGWIN   *wp;
        !           133:        LINE    *lp;
        !           134:        int      i;
1.1       deraadt   135:
                    136:        while (wheadp != curwp) {
                    137:                wp = wheadp;
                    138:                wheadp = wp->w_wndp;
                    139:                if (--wp->w_bufp->b_nwnd == 0) {
1.3       millert   140:                        wp->w_bufp->b_dotp = wp->w_dotp;
                    141:                        wp->w_bufp->b_doto = wp->w_doto;
1.1       deraadt   142:                        wp->w_bufp->b_markp = wp->w_markp;
                    143:                        wp->w_bufp->b_marko = wp->w_marko;
                    144:                }
1.4     ! millert   145:                free((char *)wp);
1.1       deraadt   146:        }
                    147:        while (curwp->w_wndp != NULL) {
                    148:                wp = curwp->w_wndp;
                    149:                curwp->w_wndp = wp->w_wndp;
                    150:                if (--wp->w_bufp->b_nwnd == 0) {
1.3       millert   151:                        wp->w_bufp->b_dotp = wp->w_dotp;
                    152:                        wp->w_bufp->b_doto = wp->w_doto;
1.1       deraadt   153:                        wp->w_bufp->b_markp = wp->w_markp;
                    154:                        wp->w_bufp->b_marko = wp->w_marko;
                    155:                }
1.4     ! millert   156:                free((char *)wp);
1.1       deraadt   157:        }
                    158:        lp = curwp->w_linep;
1.3       millert   159:        i = curwp->w_toprow;
                    160:        while (i != 0 && lback(lp) != curbp->b_linep) {
1.1       deraadt   161:                --i;
                    162:                lp = lback(lp);
                    163:        }
                    164:        curwp->w_toprow = 0;
1.4     ! millert   165:
        !           166:        /* 2 = mode, echo */
        !           167:        curwp->w_ntrows = nrow - 2;
1.3       millert   168:        curwp->w_linep = lp;
                    169:        curwp->w_flag |= WFMODE | WFHARD;
1.1       deraadt   170:        return TRUE;
                    171: }
                    172:
                    173: /*
1.4     ! millert   174:  * Split the current window.  A window smaller than 3 lines cannot be split.
        !           175:  * The only other error that is possible is a "malloc" failure allocating the
        !           176:  * structure for the new window.
1.1       deraadt   177:  */
1.3       millert   178: /* ARGSUSED */
1.4     ! millert   179: int
1.1       deraadt   180: splitwind(f, n)
1.4     ! millert   181:        int f, n;
1.1       deraadt   182: {
1.4     ! millert   183:        MGWIN   *wp, *wp1, *wp2;
        !           184:        LINE    *lp;
        !           185:        int      ntru, ntrd, ntrl;
1.1       deraadt   186:
                    187:        if (curwp->w_ntrows < 3) {
                    188:                ewprintf("Cannot split a %d line window", curwp->w_ntrows);
                    189:                return (FALSE);
                    190:        }
1.4     ! millert   191:        if ((wp = (MGWIN *)malloc(sizeof(MGWIN))) == NULL) {
1.2       millert   192:                ewprintf("Can't get %d", sizeof(MGWIN));
1.1       deraadt   193:                return (FALSE);
                    194:        }
1.4     ! millert   195:
        !           196:        /* displayed twice */
        !           197:        ++curbp->b_nwnd;
1.3       millert   198:        wp->w_bufp = curbp;
                    199:        wp->w_dotp = curwp->w_dotp;
                    200:        wp->w_doto = curwp->w_doto;
1.1       deraadt   201:        wp->w_markp = curwp->w_markp;
                    202:        wp->w_marko = curwp->w_marko;
1.3       millert   203:        wp->w_flag = 0;
1.1       deraadt   204:        wp->w_force = 0;
1.3       millert   205:        ntru = (curwp->w_ntrows - 1) / 2;       /* Upper size            */
                    206:        ntrl = (curwp->w_ntrows - 1) - ntru;    /* Lower size            */
1.1       deraadt   207:        lp = curwp->w_linep;
                    208:        ntrd = 0;
                    209:        while (lp != curwp->w_dotp) {
                    210:                ++ntrd;
                    211:                lp = lforw(lp);
                    212:        }
                    213:        lp = curwp->w_linep;
1.4     ! millert   214:
        !           215:        /* old is upper window */
        !           216:        if (ntrd <= ntru) {
        !           217:                /* hit mode line */
        !           218:                if (ntrd == ntru)
1.1       deraadt   219:                        lp = lforw(lp);
                    220:                curwp->w_ntrows = ntru;
                    221:                wp->w_wndp = curwp->w_wndp;
                    222:                curwp->w_wndp = wp;
1.3       millert   223:                wp->w_toprow = curwp->w_toprow + ntru + 1;
1.1       deraadt   224:                wp->w_ntrows = ntrl;
1.4     ! millert   225:        /* old is lower window */
        !           226:        } else {
1.1       deraadt   227:                wp1 = NULL;
                    228:                wp2 = wheadp;
                    229:                while (wp2 != curwp) {
                    230:                        wp1 = wp2;
                    231:                        wp2 = wp2->w_wndp;
                    232:                }
                    233:                if (wp1 == NULL)
                    234:                        wheadp = wp;
                    235:                else
                    236:                        wp1->w_wndp = wp;
1.3       millert   237:                wp->w_wndp = curwp;
1.1       deraadt   238:                wp->w_toprow = curwp->w_toprow;
                    239:                wp->w_ntrows = ntru;
1.4     ! millert   240:
        !           241:                /* mode line */
        !           242:                ++ntru;
1.1       deraadt   243:                curwp->w_toprow += ntru;
1.3       millert   244:                curwp->w_ntrows = ntrl;
1.1       deraadt   245:                while (ntru--)
                    246:                        lp = lforw(lp);
                    247:        }
1.4     ! millert   248:
        !           249:        /* adjust the top lines if necessary */
        !           250:        curwp->w_linep = lp;
        !           251:        wp->w_linep = lp;
        !           252:
1.3       millert   253:        curwp->w_flag |= WFMODE | WFHARD;
                    254:        wp->w_flag |= WFMODE | WFHARD;
1.1       deraadt   255:        return TRUE;
                    256: }
                    257:
                    258: /*
1.4     ! millert   259:  * Enlarge the current window.  Find the window that loses space.  Make sure
        !           260:  * it is big enough.  If so, hack the window descriptions, and ask redisplay
        !           261:  * to do all the hard work.  You don't just set "force reframe" because dot
        !           262:  * would move.
1.1       deraadt   263:  */
1.3       millert   264: /* ARGSUSED */
1.4     ! millert   265: int
1.1       deraadt   266: enlargewind(f, n)
1.4     ! millert   267:        int f, n;
1.1       deraadt   268: {
1.4     ! millert   269:        MGWIN   *adjwp;
        !           270:        LINE    *lp;
        !           271:        int      i;
1.1       deraadt   272:
                    273:        if (n < 0)
                    274:                return shrinkwind(f, -n);
                    275:        if (wheadp->w_wndp == NULL) {
                    276:                ewprintf("Only one window");
                    277:                return FALSE;
                    278:        }
1.3       millert   279:        if ((adjwp = curwp->w_wndp) == NULL) {
1.1       deraadt   280:                adjwp = wheadp;
                    281:                while (adjwp->w_wndp != curwp)
                    282:                        adjwp = adjwp->w_wndp;
                    283:        }
                    284:        if (adjwp->w_ntrows <= n) {
                    285:                ewprintf("Impossible change");
                    286:                return FALSE;
                    287:        }
1.4     ! millert   288:
        !           289:        /* shrink below */
        !           290:        if (curwp->w_wndp == adjwp) {
1.1       deraadt   291:                lp = adjwp->w_linep;
1.3       millert   292:                for (i = 0; i < n && lp != adjwp->w_bufp->b_linep; ++i)
1.1       deraadt   293:                        lp = lforw(lp);
1.3       millert   294:                adjwp->w_linep = lp;
1.1       deraadt   295:                adjwp->w_toprow += n;
1.4     ! millert   296:        /* shrink above */
        !           297:        } else {
1.1       deraadt   298:                lp = curwp->w_linep;
1.3       millert   299:                for (i = 0; i < n && lback(lp) != curbp->b_linep; ++i)
1.1       deraadt   300:                        lp = lback(lp);
1.3       millert   301:                curwp->w_linep = lp;
1.1       deraadt   302:                curwp->w_toprow -= n;
                    303:        }
                    304:        curwp->w_ntrows += n;
                    305:        adjwp->w_ntrows -= n;
1.3       millert   306:        curwp->w_flag |= WFMODE | WFHARD;
                    307:        adjwp->w_flag |= WFMODE | WFHARD;
1.1       deraadt   308:        return TRUE;
                    309: }
                    310:
                    311: /*
1.4     ! millert   312:  * Shrink the current window.  Find the window that gains space.  Hack at the
        !           313:  * window descriptions. Ask the redisplay to do all the hard work.
1.1       deraadt   314:  */
1.4     ! millert   315: int
1.1       deraadt   316: shrinkwind(f, n)
1.4     ! millert   317:        int f, n;
1.1       deraadt   318: {
1.4     ! millert   319:        MGWIN   *adjwp;
        !           320:        LINE    *lp;
        !           321:        int      i;
1.1       deraadt   322:
                    323:        if (n < 0)
                    324:                return enlargewind(f, -n);
                    325:        if (wheadp->w_wndp == NULL) {
                    326:                ewprintf("Only one window");
                    327:                return FALSE;
                    328:        }
                    329:        /*
                    330:         * Bit of flakiness - KRANDOM means it was an internal call, and
                    331:         * to be trusted implicitly about sizes.
                    332:         */
1.3       millert   333:        if (!(f & FFRAND) && curwp->w_ntrows <= n) {
1.1       deraadt   334:                ewprintf("Impossible change");
                    335:                return (FALSE);
                    336:        }
1.3       millert   337:        if ((adjwp = curwp->w_wndp) == NULL) {
1.1       deraadt   338:                adjwp = wheadp;
                    339:                while (adjwp->w_wndp != curwp)
                    340:                        adjwp = adjwp->w_wndp;
                    341:        }
1.4     ! millert   342:
        !           343:        /* grow below */
        !           344:        if (curwp->w_wndp == adjwp) {
1.1       deraadt   345:                lp = adjwp->w_linep;
1.3       millert   346:                for (i = 0; i < n && lback(lp) != adjwp->w_bufp->b_linep; ++i)
1.1       deraadt   347:                        lp = lback(lp);
1.3       millert   348:                adjwp->w_linep = lp;
1.1       deraadt   349:                adjwp->w_toprow -= n;
1.4     ! millert   350:        /* grow above */
        !           351:        } else {
1.1       deraadt   352:                lp = curwp->w_linep;
1.3       millert   353:                for (i = 0; i < n && lp != curbp->b_linep; ++i)
1.1       deraadt   354:                        lp = lforw(lp);
1.3       millert   355:                curwp->w_linep = lp;
1.1       deraadt   356:                curwp->w_toprow += n;
                    357:        }
                    358:        curwp->w_ntrows -= n;
                    359:        adjwp->w_ntrows += n;
1.3       millert   360:        curwp->w_flag |= WFMODE | WFHARD;
                    361:        adjwp->w_flag |= WFMODE | WFHARD;
1.1       deraadt   362:        return (TRUE);
                    363: }
                    364:
                    365: /*
1.4     ! millert   366:  * Delete current window. Call shrink-window to do the screen updating, then
        !           367:  * throw away the window.
1.1       deraadt   368:  */
1.3       millert   369: /* ARGSUSED */
1.4     ! millert   370: int
1.1       deraadt   371: delwind(f, n)
1.4     ! millert   372:        int f, n;
1.1       deraadt   373: {
1.4     ! millert   374:        MGWIN   *wp, *nwp;
1.1       deraadt   375:
1.3       millert   376:        wp = curwp;             /* Cheap...              */
1.4     ! millert   377:
1.1       deraadt   378:        /* shrinkwind returning false means only one window... */
                    379:        if (shrinkwind(FFRAND, wp->w_ntrows + 1) == FALSE)
                    380:                return FALSE;
                    381:        if (--wp->w_bufp->b_nwnd == 0) {
1.3       millert   382:                wp->w_bufp->b_dotp = wp->w_dotp;
                    383:                wp->w_bufp->b_doto = wp->w_doto;
1.1       deraadt   384:                wp->w_bufp->b_markp = wp->w_markp;
                    385:                wp->w_bufp->b_marko = wp->w_marko;
                    386:        }
1.4     ! millert   387:
1.1       deraadt   388:        /* since shrinkwind did't crap out, we know we have a second window */
1.3       millert   389:        if (wp == wheadp)
                    390:                wheadp = curwp = wp->w_wndp;
                    391:        else if ((curwp = wp->w_wndp) == NULL)
                    392:                curwp = wheadp;
1.1       deraadt   393:        curbp = curwp->w_bufp;
                    394:        for (nwp = wheadp; nwp != NULL; nwp = nwp->w_wndp)
                    395:                if (nwp->w_wndp == wp) {
                    396:                        nwp->w_wndp = wp->w_wndp;
1.3       millert   397:                        break;
1.1       deraadt   398:                }
1.4     ! millert   399:        free((char *)wp);
1.1       deraadt   400:        return TRUE;
                    401: }
1.4     ! millert   402:
1.1       deraadt   403: /*
1.4     ! millert   404:  * Pick a window for a pop-up.  Split the screen if there is only one window.
        !           405:  * Pick the uppermost window that isn't the current window. An LRU algorithm
        !           406:  * might be better. Return a pointer, or NULL on error.
1.1       deraadt   407:  */
1.3       millert   408: MGWIN *
                    409: wpopup()
                    410: {
1.4     ! millert   411:        MGWIN   *wp;
1.1       deraadt   412:
                    413:        if (wheadp->w_wndp == NULL
1.3       millert   414:            && splitwind(FFRAND, 0) == FALSE)
1.1       deraadt   415:                return NULL;
1.4     ! millert   416:
        !           417:        /* find a window to use */
        !           418:        wp = wheadp;
        !           419:
1.3       millert   420:        while (wp != NULL && wp == curwp)
1.1       deraadt   421:                wp = wp->w_wndp;
                    422:        return wp;
                    423: }