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

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