[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.23

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