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

Annotation of src/usr.bin/mg/buffer.c, Revision 1.8

1.8     ! art         1: /*     $OpenBSD: buffer.c,v 1.7 2001/05/04 22:00:35 art Exp $  */
1.5       niklas      2:
1.1       deraadt     3: /*
                      4:  *             Buffer handling.
                      5:  */
1.4       millert     6:
                      7: #include "def.h"
                      8: #include "kbd.h"               /* needed for modes */
1.8     ! art         9: #include <stdarg.h>
1.1       deraadt    10:
1.3       millert    11: static RSIZE    itor           __P((char *, int, RSIZE));
                     12: static BUFFER  *makelist       __P((void));
1.1       deraadt    13:
                     14: /*
                     15:  * Attach a buffer to a window. The values of dot and mark come
                     16:  * from the buffer if the use count is 0. Otherwise, they come
                     17:  * from some other window.  *scratch* is the default alternate
                     18:  * buffer.
                     19:  */
1.3       millert    20: /* ARGSUSED */
                     21: int
1.1       deraadt    22: usebuffer(f, n)
1.3       millert    23:        int     f, n;
1.1       deraadt    24: {
1.3       millert    25:        BUFFER *bp;
                     26:        int     s;
                     27:        char    bufn[NBUFN];
1.1       deraadt    28:
                     29:        /* Get buffer to use from user */
                     30:        if ((curbp->b_altb == NULL)
                     31:            && ((curbp->b_altb = bfind("*scratch*", TRUE)) == NULL))
1.3       millert    32:                s = eread("Switch to buffer: ", bufn, NBUFN, EFNEW | EFBUF);
1.1       deraadt    33:        else
1.3       millert    34:                s = eread("Switch to buffer: (default %s) ", bufn, NBUFN,
                     35:                          EFNEW | EFBUF, curbp->b_altb->b_bname);
1.1       deraadt    36:
1.3       millert    37:        if (s == ABORT)
                     38:                return s;
                     39:        if (s == FALSE && curbp->b_altb != NULL)
                     40:                bp = curbp->b_altb;
                     41:        else if ((bp = bfind(bufn, TRUE)) == NULL)
                     42:                return FALSE;
1.1       deraadt    43:
                     44:        /* and put it in current window */
                     45:        curbp = bp;
1.3       millert    46:        return showbuffer(bp, curwp, WFFORCE | WFHARD);
1.1       deraadt    47: }
                     48:
                     49: /*
                     50:  * pop to buffer asked for by the user.
                     51:  */
1.3       millert    52: /* ARGSUSED */
                     53: int
1.1       deraadt    54: poptobuffer(f, n)
1.3       millert    55:        int     f, n;
1.1       deraadt    56: {
1.3       millert    57:        BUFFER *bp;
                     58:        MGWIN  *wp;
                     59:        int     s;
                     60:        char    bufn[NBUFN];
1.1       deraadt    61:
                     62:        /* Get buffer to use from user */
                     63:        if ((curbp->b_altb == NULL)
                     64:            && ((curbp->b_altb = bfind("*scratch*", TRUE)) == NULL))
1.3       millert    65:                s = eread("Switch to buffer in other window: ", bufn, NBUFN,
                     66:                          EFNEW | EFBUF);
1.1       deraadt    67:        else
1.3       millert    68:                s = eread("Switch to buffer in other window: (default %s) ",
                     69:                        bufn, NBUFN, EFNEW | EFBUF, curbp->b_altb->b_bname);
                     70:        if (s == ABORT)
                     71:                return s;
                     72:        if (s == FALSE && curbp->b_altb != NULL)
                     73:                bp = curbp->b_altb;
                     74:        else if ((bp = bfind(bufn, TRUE)) == NULL)
                     75:                return FALSE;
1.1       deraadt    76:
                     77:        /* and put it in a new window */
1.3       millert    78:        if ((wp = popbuf(bp)) == NULL)
                     79:                return FALSE;
1.1       deraadt    80:        curbp = bp;
                     81:        curwp = wp;
                     82:        return TRUE;
                     83: }
                     84:
                     85: /*
                     86:  * Dispose of a buffer, by name.
                     87:  * Ask for the name. Look it up (don't get too
                     88:  * upset if it isn't there at all!). Clear the buffer (ask
                     89:  * if the buffer has been changed). Then free the header
                     90:  * line and the buffer header. Bound to "C-X K".
                     91:  */
1.3       millert    92: /* ARGSUSED */
                     93: int
1.1       deraadt    94: killbuffer(f, n)
1.3       millert    95:        int     f, n;
1.1       deraadt    96: {
1.3       millert    97:        BUFFER *bp;
                     98:        BUFFER *bp1;
                     99:        BUFFER *bp2;
                    100:        MGWIN  *wp;
                    101:        int     s;
                    102:        char    bufn[NBUFN];
                    103:
                    104:        if ((s = eread("Kill buffer: (default %s) ", bufn, NBUFN, EFNEW | EFBUF,
                    105:                       curbp->b_bname)) == ABORT)
                    106:                return (s);
                    107:        else if (s == FALSE)
                    108:                bp = curbp;
                    109:        else if ((bp = bfind(bufn, FALSE)) == NULL)
                    110:                return FALSE;
                    111:
                    112:        /*
                    113:         * Find some other buffer to display. try the alternate buffer,
                    114:         * then the first different buffer in the buffer list.  If there's
                    115:         * only one buffer, create buffer *scratch* and make it the alternate
                    116:         * buffer.  Return if *scratch* is only buffer...
1.1       deraadt   117:         */
                    118:        if ((bp1 = bp->b_altb) == NULL) {
                    119:                bp1 = (bp == bheadp) ? bp->b_bufp : bheadp;
                    120:                if (bp1 == NULL) {
                    121:                        /* only one buffer. see if it's *scratch* */
1.3       millert   122:                        if (bp == bfind("*scratch*", FALSE))
1.1       deraadt   123:                                return FALSE;
                    124:                        /* create *scratch* for alternate buffer */
1.3       millert   125:                        if ((bp1 = bfind("*scratch*", TRUE)) == NULL)
1.1       deraadt   126:                                return FALSE;
                    127:                }
                    128:        }
1.3       millert   129:        if (bclear(bp) != TRUE)
                    130:                return TRUE;
1.1       deraadt   131:        for (wp = wheadp; bp->b_nwnd > 0; wp = wp->w_wndp) {
1.3       millert   132:                if (wp->w_bufp == bp) {
                    133:                        bp2 = bp1->b_altb;      /* save alternate buffer */
                    134:                        if (showbuffer(bp1, wp, WFMODE | WFFORCE | WFHARD))
                    135:                                bp1->b_altb = bp2;
                    136:                        else
                    137:                                bp1 = bp2;
                    138:                }
                    139:        }
                    140:        if (bp == curbp)
                    141:                curbp = bp1;
                    142:        free(bp->b_linep);                      /* Release header line.  */
                    143:        bp2 = NULL;                             /* Find the header.      */
1.1       deraadt   144:        bp1 = bheadp;
                    145:        while (bp1 != bp) {
                    146:                if (bp1->b_altb == bp)
                    147:                        bp1->b_altb = (bp->b_altb == bp1) ? NULL : bp->b_altb;
                    148:                bp2 = bp1;
                    149:                bp1 = bp1->b_bufp;
                    150:        }
1.3       millert   151:        bp1 = bp1->b_bufp;                      /* Next one in chain.    */
                    152:        if (bp2 == NULL)                        /* Unlink it.            */
1.1       deraadt   153:                bheadp = bp1;
                    154:        else
                    155:                bp2->b_bufp = bp1;
1.3       millert   156:        while (bp1 != NULL) {                   /* Finish with altb's    */
1.1       deraadt   157:                if (bp1->b_altb == bp)
                    158:                        bp1->b_altb = (bp->b_altb == bp1) ? NULL : bp->b_altb;
                    159:                bp1 = bp1->b_bufp;
                    160:        }
1.3       millert   161:        free(bp->b_bname);                      /* Release name block    */
                    162:        free(bp);                               /* Release buffer block */
1.1       deraadt   163:        return TRUE;
                    164: }
                    165:
                    166: /*
                    167:  * Save some buffers - just call anycb with the arg flag.
                    168:  */
1.3       millert   169: /* ARGSUSED */
                    170: int
1.1       deraadt   171: savebuffers(f, n)
1.3       millert   172:        int f, n;
1.1       deraadt   173: {
1.3       millert   174:        if (anycb(f) == ABORT)
                    175:                return ABORT;
1.1       deraadt   176:        return TRUE;
                    177: }
                    178:
                    179: /*
                    180:  * Display the buffer list. This is done
                    181:  * in two parts. The "makelist" routine figures out
                    182:  * the text, and puts it in a buffer. "popbuf"
                    183:  * then pops the data onto the screen. Bound to
                    184:  * "C-X C-B".
                    185:  */
1.3       millert   186: /* ARGSUSED */
                    187: int
1.1       deraadt   188: listbuffers(f, n)
1.4       millert   189:        int f, n;
1.1       deraadt   190: {
1.3       millert   191:        BUFFER *bp;
                    192:        MGWIN  *wp;
1.1       deraadt   193:
1.3       millert   194:        if ((bp = makelist()) == NULL || (wp = popbuf(bp)) == NULL)
1.1       deraadt   195:                return FALSE;
1.3       millert   196:        wp->w_dotp = bp->b_dotp;/* fix up if window already on screen */
1.1       deraadt   197:        wp->w_doto = bp->b_doto;
                    198:        return TRUE;
                    199: }
                    200:
                    201: /*
                    202:  * This routine rebuilds the text for the
                    203:  * list buffers command. Return TRUE if
                    204:  * everything works. Return FALSE if there
                    205:  * is an error (if there is no memory).
                    206:  */
1.3       millert   207: static BUFFER *
                    208: makelist()
                    209: {
                    210:        char   *cp1;
                    211:        char   *cp2;
                    212:        int     c;
                    213:        BUFFER *bp;
                    214:        LINE   *lp;
                    215:        RSIZE   nbytes;
                    216:        BUFFER *blp;
                    217:        char    b[6 + 1];
1.6       art       218:        char    line[NBUFN+128];
1.3       millert   219:
                    220:        if ((blp = bfind("*Buffer List*", TRUE)) == NULL)
                    221:                return NULL;
                    222:        if (bclear(blp) != TRUE)
                    223:                return NULL;
                    224:        blp->b_flag &= ~BFCHG;          /* Blow away old.        */
1.1       deraadt   225:
                    226:        (VOID) strcpy(line, " MR Buffer");
                    227:        cp1 = line + 10;
1.3       millert   228:        while (cp1 < line + 4 + NBUFN + 1)
                    229:                *cp1++ = ' ';
1.1       deraadt   230:        (VOID) strcpy(cp1, "Size   File");
1.3       millert   231:        if (addline(blp, line) == FALSE)
                    232:                return NULL;
1.1       deraadt   233:        (VOID) strcpy(line, " -- ------");
                    234:        cp1 = line + 10;
1.3       millert   235:        while (cp1 < line + 4 + NBUFN + 1)
                    236:                *cp1++ = ' ';
1.1       deraadt   237:        (VOID) strcpy(cp1, "----   ----");
1.3       millert   238:        if (addline(blp, line) == FALSE)
                    239:                return NULL;
                    240:        bp = bheadp;                            /* For all buffers       */
1.1       deraadt   241:        while (bp != NULL) {
1.3       millert   242:                cp1 = &line[0];                 /* Start at left edge    */
1.1       deraadt   243:                *cp1++ = (bp == curbp) ? '.' : ' ';
1.3       millert   244:                *cp1++ = ((bp->b_flag & BFCHG) != 0) ? '*' : ' ';
                    245:                *cp1++ = ' ';                   /* Gap.                  */
1.1       deraadt   246:                *cp1++ = ' ';
1.3       millert   247:                cp2 = &bp->b_bname[0];          /* Buffer name           */
1.1       deraadt   248:                while ((c = *cp2++) != 0)
                    249:                        *cp1++ = c;
1.3       millert   250:                while (cp1 < &line[4 + NBUFN + 1])
1.1       deraadt   251:                        *cp1++ = ' ';
1.3       millert   252:                nbytes = 0;                     /* Count bytes in buf.   */
1.1       deraadt   253:                if (bp != blp) {
                    254:                        lp = lforw(bp->b_linep);
                    255:                        while (lp != bp->b_linep) {
1.3       millert   256:                                nbytes += llength(lp) + 1;
1.1       deraadt   257:                                lp = lforw(lp);
                    258:                        }
1.3       millert   259:                        if (nbytes)
                    260:                                nbytes--;       /* no bonus newline      */
1.1       deraadt   261:                }
                    262:                (VOID) itor(b, 6, nbytes);      /* 6 digit buffer size. */
                    263:                cp2 = &b[0];
                    264:                while ((c = *cp2++) != 0)
                    265:                        *cp1++ = c;
1.3       millert   266:                *cp1++ = ' ';                   /* Gap..                 */
                    267:                cp2 = &bp->b_fname[0];          /* File name             */
1.1       deraadt   268:                if (*cp2 != 0) {
                    269:                        while ((c = *cp2++) != 0) {
1.3       millert   270:                                if (cp1 < &line[128 - 1])
1.1       deraadt   271:                                        *cp1++ = c;
                    272:                        }
                    273:                }
1.3       millert   274:                *cp1 = 0;       /* Add to the buffer.    */
1.1       deraadt   275:                if (addline(blp, line) == FALSE)
                    276:                        return NULL;
                    277:                bp = bp->b_bufp;
                    278:        }
1.3       millert   279:        blp->b_dotp = lforw(blp->b_linep);      /* put dot at beginning of
                    280:                                                 * buffer */
1.1       deraadt   281:        blp->b_doto = 0;
1.3       millert   282:        return blp;                             /* All done              */
1.1       deraadt   283: }
                    284:
                    285: /*
                    286:  * Used above.
                    287:  */
1.3       millert   288: static RSIZE
                    289: itor(buf, width, num)
                    290:        char  *buf;
                    291:        int    width;
                    292:        RSIZE  num;
                    293: {
                    294:        RSIZE  r;
1.1       deraadt   295:
                    296:        if (num / 10 == 0) {
                    297:                buf[0] = (num % 10) + '0';
1.3       millert   298:                for (r = 1; r < width; buf[r++] = ' ');
1.1       deraadt   299:                buf[width] = '\0';
                    300:                return 1;
                    301:        } else {
1.3       millert   302:                buf[r = itor(buf, width, num / (RSIZE) 10)] =
                    303:                        (num % (RSIZE) 10) + '0';
1.1       deraadt   304:                return r + 1;
                    305:        }
1.3       millert   306:        /* NOTREACHED */
1.1       deraadt   307: }
                    308:
                    309: /*
1.8     ! art       310:  * The argument "text" points to a format string.  Append this line to the
1.3       millert   311:  * buffer. Handcraft the EOL on the end.  Return TRUE if it worked and
1.1       deraadt   312:  * FALSE if you ran out of room.
                    313:  */
1.3       millert   314: int
1.8     ! art       315: addlinef(BUFFER *bp, char *fmt, ...)
1.3       millert   316: {
1.8     ! art       317:        va_list ap;
1.3       millert   318:        LINE  *lp;
                    319:        int    i;
                    320:        int    ntext;
1.8     ! art       321:        char   dummy[1];
1.1       deraadt   322:
1.8     ! art       323:        va_start(ap, fmt);
        !           324:        ntext = vsnprintf(dummy, 1, fmt, ap) + 1;
        !           325:        if ((lp = lalloc(ntext)) == NULL) {
        !           326:                va_end(ap);
1.1       deraadt   327:                return FALSE;
1.8     ! art       328:        }
        !           329:        vsnprintf(lp->l_text, ntext, fmt, ap);
        !           330:        va_end(ap);
        !           331:
1.3       millert   332:        bp->b_linep->l_bp->l_fp = lp;           /* Hook onto the end     */
1.1       deraadt   333:        lp->l_bp = bp->b_linep->l_bp;
                    334:        bp->b_linep->l_bp = lp;
                    335:        lp->l_fp = bp->b_linep;
1.8     ! art       336:
1.1       deraadt   337:        return TRUE;
1.8     ! art       338: }
        !           339:
        !           340: /*
        !           341:  * Wrapper for addlinef with just one string. Should go away.
        !           342:  */
        !           343: int
        !           344: addline(BUFFER *bp, char *text)
        !           345: {
        !           346:        return addlinef(bp, "%s", text);
1.1       deraadt   347: }
                    348:
                    349: /*
1.3       millert   350:  * Look through the list of buffers, giving the user a chance to save them.
                    351:  * Return TRUE if there are any changed buffers afterwards.  Buffers that
                    352:  * don't have an associated file don't count.  Return FALSE if there are
                    353:  * no changed buffers.
                    354:  */
                    355: int
                    356: anycb(f)
                    357:        int     f;
                    358: {
                    359:        BUFFER *bp;
                    360:        int     s = FALSE, save = FALSE;
                    361:        char    prompt[NFILEN + 11];
1.1       deraadt   362:
                    363:        for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
                    364:                if (*(bp->b_fname) != '\0'
1.3       millert   365:                    && (bp->b_flag & BFCHG) != 0) {
1.1       deraadt   366:                        (VOID) strcpy(prompt, "Save file ");
                    367:                        (VOID) strcpy(prompt + 10, bp->b_fname);
                    368:                        if ((f == TRUE || (save = eyorn(prompt)) == TRUE)
1.3       millert   369:                            && buffsave(bp) == TRUE) {
1.1       deraadt   370:                                bp->b_flag &= ~BFCHG;
                    371:                                upmodes(bp);
1.3       millert   372:                        } else
                    373:                                s = TRUE;
                    374:                        if (save == ABORT)
                    375:                                return (save);
1.1       deraadt   376:                        save = TRUE;
                    377:                }
                    378:        }
                    379:        if (save == FALSE /* && kbdmop == NULL */ )     /* experimental */
                    380:                ewprintf("(No files need saving)");
                    381:        return s;
                    382: }
                    383:
                    384: /*
                    385:  * Search for a buffer, by name.
                    386:  * If not found, and the "cflag" is TRUE,
                    387:  * create a buffer and put it in the list of
                    388:  * all buffers. Return pointer to the BUFFER
                    389:  * block for the buffer.
                    390:  */
1.3       millert   391: BUFFER *
                    392: bfind(bname, cflag)
1.4       millert   393:        char *bname;
                    394:        int   cflag;
1.3       millert   395: {
1.4       millert   396:        BUFFER  *bp;
                    397:        LINE    *lp;
                    398:        int      i;
1.1       deraadt   399:
                    400:        bp = bheadp;
                    401:        while (bp != NULL) {
1.7       art       402:                if (strcmp(bname, bp->b_bname) == 0)
1.1       deraadt   403:                        return bp;
                    404:                bp = bp->b_bufp;
                    405:        }
1.3       millert   406:        if (cflag != TRUE)
                    407:                return NULL;
                    408:        /* NOSTRICT */
                    409:        if ((bp = (BUFFER *) malloc(sizeof(BUFFER))) == NULL) {
1.1       deraadt   410:                ewprintf("Can't get %d bytes", sizeof(BUFFER));
                    411:                return NULL;
                    412:        }
1.3       millert   413:        if ((bp->b_bname = malloc((strlen(bname) + 1))) == NULL) {
                    414:                ewprintf("Can't get %d bytes", strlen(bname) + 1);
1.1       deraadt   415:                free((char *) bp);
                    416:                return NULL;
                    417:        }
                    418:        if ((lp = lalloc(0)) == NULL) {
                    419:                free(bp->b_bname);
                    420:                free((char *) bp);
                    421:                return NULL;
                    422:        }
1.3       millert   423:        bp->b_altb = bp->b_bufp = NULL;
                    424:        bp->b_dotp = lp;
                    425:        bp->b_doto = 0;
1.1       deraadt   426:        bp->b_markp = NULL;
                    427:        bp->b_marko = 0;
1.3       millert   428:        bp->b_flag = defb_flag;
                    429:        bp->b_nwnd = 0;
1.1       deraadt   430:        bp->b_linep = lp;
                    431:        bp->b_nmodes = defb_nmodes;
                    432:        i = 0;
                    433:        do {
1.3       millert   434:                bp->b_modes[i] = defb_modes[i];
                    435:        } while (i++ < defb_nmodes);
1.1       deraadt   436:        bp->b_fname[0] = '\0';
                    437:        bzero(&bp->b_fi, sizeof(bp->b_fi));
                    438:        (VOID) strcpy(bp->b_bname, bname);
                    439:        lp->l_fp = lp;
                    440:        lp->l_bp = lp;
                    441:        bp->b_bufp = bheadp;
                    442:        bheadp = bp;
                    443:        return bp;
                    444: }
                    445:
                    446: /*
                    447:  * This routine blows away all of the text
                    448:  * in a buffer. If the buffer is marked as changed
                    449:  * then we ask if it is ok to blow it away; this is
                    450:  * to save the user the grief of losing text. The
                    451:  * window chain is nearly always wrong if this gets
                    452:  * called; the caller must arrange for the updates
                    453:  * that are required. Return TRUE if everything
                    454:  * looks good.
                    455:  */
1.3       millert   456: int
                    457: bclear(bp)
                    458:        BUFFER *bp;
                    459: {
                    460:        LINE  *lp;
                    461:        int    s;
1.1       deraadt   462:
1.3       millert   463:        if ((bp->b_flag & BFCHG) != 0   /* Changed.              */
                    464:            && (s = eyesno("Buffer modified; kill anyway")) != TRUE)
1.1       deraadt   465:                return (s);
1.3       millert   466:        bp->b_flag &= ~BFCHG;   /* Not changed           */
                    467:        while ((lp = lforw(bp->b_linep)) != bp->b_linep)
1.1       deraadt   468:                lfree(lp);
1.3       millert   469:        bp->b_dotp = bp->b_linep;       /* Fix "."               */
                    470:        bp->b_doto = 0;
                    471:        bp->b_markp = NULL;     /* Invalidate "mark"     */
1.1       deraadt   472:        bp->b_marko = 0;
                    473:        return TRUE;
                    474: }
                    475:
                    476: /*
                    477:  * Display the given buffer in the given window. Flags indicated
                    478:  * action on redisplay.
                    479:  */
1.3       millert   480: int
                    481: showbuffer(bp, wp, flags)
                    482:        BUFFER *bp;
                    483:        MGWIN  *wp;
                    484:        int     flags;
                    485: {
                    486:        BUFFER *obp;
                    487:        MGWIN  *owp;
1.1       deraadt   488:
1.3       millert   489:        if (wp->w_bufp == bp) { /* Easy case!    */
1.1       deraadt   490:                wp->w_flag |= flags;
                    491:                return TRUE;
                    492:        }
                    493:        /* First, dettach the old buffer from the window */
                    494:        if ((bp->b_altb = obp = wp->w_bufp) != NULL) {
                    495:                if (--obp->b_nwnd == 0) {
1.3       millert   496:                        obp->b_dotp = wp->w_dotp;
                    497:                        obp->b_doto = wp->w_doto;
1.1       deraadt   498:                        obp->b_markp = wp->w_markp;
                    499:                        obp->b_marko = wp->w_marko;
                    500:                }
                    501:        }
                    502:        /* Now, attach the new buffer to the window */
                    503:        wp->w_bufp = bp;
                    504:
1.3       millert   505:        if (bp->b_nwnd++ == 0) {        /* First use.            */
                    506:                wp->w_dotp = bp->b_dotp;
                    507:                wp->w_doto = bp->b_doto;
1.1       deraadt   508:                wp->w_markp = bp->b_markp;
                    509:                wp->w_marko = bp->b_marko;
                    510:        } else
1.3       millert   511:                /* already on screen, steal values from other window */
1.1       deraadt   512:                for (owp = wheadp; owp != NULL; owp = wp->w_wndp)
                    513:                        if (wp->w_bufp == bp && owp != wp) {
1.3       millert   514:                                wp->w_dotp = owp->w_dotp;
                    515:                                wp->w_doto = owp->w_doto;
1.1       deraadt   516:                                wp->w_markp = owp->w_markp;
                    517:                                wp->w_marko = owp->w_marko;
                    518:                                break;
                    519:                        }
1.3       millert   520:        wp->w_flag |= WFMODE | flags;
1.1       deraadt   521:        return TRUE;
                    522: }
                    523:
                    524: /*
                    525:  * Pop the buffer we got passed onto the screen.
                    526:  * Returns a status.
                    527:  */
1.2       millert   528: MGWIN *
1.3       millert   529: popbuf(bp)
                    530:        BUFFER *bp;
                    531: {
                    532:        MGWIN  *wp;
1.1       deraadt   533:
1.3       millert   534:        if (bp->b_nwnd == 0) {  /* Not on screen yet.    */
                    535:                if ((wp = wpopup()) == NULL)
                    536:                        return NULL;
1.1       deraadt   537:        } else
                    538:                for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
                    539:                        if (wp->w_bufp == bp) {
1.3       millert   540:                                wp->w_flag |= WFHARD | WFFORCE;
                    541:                                return wp;
1.1       deraadt   542:                        }
1.3       millert   543:        if (showbuffer(bp, wp, WFHARD) != TRUE)
                    544:                return NULL;
1.1       deraadt   545:        return wp;
                    546: }
                    547:
                    548: /*
                    549:  * Insert another buffer at dot.  Very useful.
                    550:  */
1.3       millert   551: /* ARGSUSED */
                    552: int
1.1       deraadt   553: bufferinsert(f, n)
                    554: {
1.3       millert   555:        BUFFER *bp;
                    556:        LINE   *clp;
                    557:        int     clo;
                    558:        int     nline;
                    559:        int     s;
                    560:        char    bufn[NBUFN];
1.1       deraadt   561:
                    562:        /* Get buffer to use from user */
                    563:        if (curbp->b_altb != NULL)
1.3       millert   564:                s = eread("Insert buffer: (default %s) ", bufn, NBUFN,
                    565:                          EFNEW | EFBUF, &(curbp->b_altb->b_bname),
                    566:                          (char *) NULL);
1.1       deraadt   567:        else
1.3       millert   568:                s = eread("Insert buffer: ", bufn, NBUFN, EFNEW | EFBUF,
                    569:                          (char *) NULL);
                    570:        if (s == ABORT)
                    571:                return (s);
                    572:        if (s == FALSE && curbp->b_altb != NULL)
                    573:                bp = curbp->b_altb;
                    574:        else if ((bp = bfind(bufn, FALSE)) == NULL)
                    575:                return FALSE;
1.1       deraadt   576:
1.3       millert   577:        if (bp == curbp) {
1.1       deraadt   578:                ewprintf("Cannot insert buffer into self");
                    579:                return FALSE;
                    580:        }
                    581:        /* insert the buffer */
                    582:        nline = 0;
                    583:        clp = lforw(bp->b_linep);
1.3       millert   584:        for (;;) {
1.1       deraadt   585:                for (clo = 0; clo < llength(clp); clo++)
                    586:                        if (linsert(1, lgetc(clp, clo)) == FALSE)
                    587:                                return FALSE;
1.3       millert   588:                if ((clp = lforw(clp)) == bp->b_linep)
                    589:                        break;
                    590:                if (newline(FFRAND, 1) == FALSE)        /* fake newline */
1.1       deraadt   591:                        return FALSE;
                    592:                nline++;
                    593:        }
1.3       millert   594:        if (nline == 1)
                    595:                ewprintf("[Inserted 1 line]");
                    596:        else
                    597:                ewprintf("[Inserted %d lines]", nline);
1.1       deraadt   598:
1.3       millert   599:        clp = curwp->w_linep;   /* cosmetic adjustment */
                    600:        if (curwp->w_dotp == clp) {     /* for offscreen insert */
                    601:                while (nline-- && lback(clp) != curbp->b_linep)
1.1       deraadt   602:                        clp = lback(clp);
1.3       millert   603:                curwp->w_linep = clp;   /* adjust framing.       */
1.1       deraadt   604:                curwp->w_flag |= WFHARD;
                    605:        }
                    606:        return (TRUE);
                    607: }
                    608:
                    609: /*
                    610:  * Turn off the dirty bit on this buffer.
                    611:  */
1.3       millert   612: /* ARGSUSED */
                    613: int
1.1       deraadt   614: notmodified(f, n)
                    615: {
1.3       millert   616:        MGWIN *wp;
1.1       deraadt   617:
                    618:        curbp->b_flag &= ~BFCHG;
1.3       millert   619:        wp = wheadp;            /* Update mode lines.    */
1.1       deraadt   620:        while (wp != NULL) {
                    621:                if (wp->w_bufp == curbp)
                    622:                        wp->w_flag |= WFMODE;
                    623:                wp = wp->w_wndp;
                    624:        }
                    625:        ewprintf("Modification-flag cleared");
                    626:        return TRUE;
                    627: }
                    628:
                    629: #ifndef NO_HELP
                    630: /*
                    631:  * Popbuf and set all windows to top of buffer.         Currently only used by
                    632:  * help functions.
                    633:  */
1.3       millert   634: int
1.1       deraadt   635: popbuftop(bp)
1.3       millert   636:        BUFFER *bp;
1.1       deraadt   637: {
1.3       millert   638:        MGWIN *wp;
1.1       deraadt   639:
1.3       millert   640:        bp->b_dotp = lforw(bp->b_linep);
                    641:        bp->b_doto = 0;
                    642:        if (bp->b_nwnd != 0) {
                    643:                for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
                    644:                        if (wp->w_bufp == bp) {
                    645:                                wp->w_dotp = bp->b_dotp;
                    646:                                wp->w_doto = 0;
                    647:                                wp->w_flag |= WFHARD;
                    648:                        }
                    649:        }
                    650:        return popbuf(bp) != NULL;
1.1       deraadt   651: }
                    652: #endif