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

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