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

1.32    ! vincent     1: /*     $OpenBSD: buffer.c,v 1.31 2003/06/26 23:04:10 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
1.31      vincent    14: togglereadonly(int f, int n)
1.27      vincent    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;
1.1       deraadt   360:
1.30      vincent   361:        if ((lp = lalloc(0)) == NULL)
                    362:                return (FALSE);
1.8       art       363:        va_start(ap, fmt);
1.30      vincent   364:        if (vasprintf(&lp->l_text, fmt, ap) == -1) {
                    365:                lfree(lp);
1.16      deraadt   366:                va_end(ap);
1.30      vincent   367:                return (FALSE);
1.16      deraadt   368:        }
1.30      vincent   369:        lp->l_used = strlen(lp->l_text);
1.8       art       370:        va_end(ap);
                    371:
1.3       millert   372:        bp->b_linep->l_bp->l_fp = lp;           /* Hook onto the end     */
1.1       deraadt   373:        lp->l_bp = bp->b_linep->l_bp;
                    374:        bp->b_linep->l_bp = lp;
                    375:        lp->l_fp = bp->b_linep;
1.8       art       376:
1.1       deraadt   377:        return TRUE;
                    378: }
                    379:
                    380: /*
1.3       millert   381:  * Look through the list of buffers, giving the user a chance to save them.
                    382:  * Return TRUE if there are any changed buffers afterwards.  Buffers that
                    383:  * don't have an associated file don't count.  Return FALSE if there are
                    384:  * no changed buffers.
                    385:  */
                    386: int
1.26      vincent   387: anycb(int f)
1.3       millert   388: {
                    389:        BUFFER *bp;
                    390:        int     s = FALSE, save = FALSE;
                    391:        char    prompt[NFILEN + 11];
1.1       deraadt   392:
                    393:        for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
1.21      deraadt   394:                if (bp->b_fname != NULL && *(bp->b_fname) != '\0' &&
                    395:                    (bp->b_flag & BFCHG) != 0) {
1.17      deraadt   396:                        snprintf(prompt, sizeof prompt, "Save file %s",
                    397:                            bp->b_fname);
1.21      deraadt   398:                        if ((f == TRUE || (save = eyorn(prompt)) == TRUE) &&
                    399:                            buffsave(bp) == TRUE) {
1.1       deraadt   400:                                bp->b_flag &= ~BFCHG;
                    401:                                upmodes(bp);
1.3       millert   402:                        } else
                    403:                                s = TRUE;
                    404:                        if (save == ABORT)
                    405:                                return (save);
1.1       deraadt   406:                        save = TRUE;
                    407:                }
                    408:        }
                    409:        if (save == FALSE /* && kbdmop == NULL */ )     /* experimental */
                    410:                ewprintf("(No files need saving)");
                    411:        return s;
                    412: }
                    413:
                    414: /*
                    415:  * Search for a buffer, by name.
                    416:  * If not found, and the "cflag" is TRUE,
                    417:  * create a buffer and put it in the list of
                    418:  * all buffers. Return pointer to the BUFFER
                    419:  * block for the buffer.
                    420:  */
1.3       millert   421: BUFFER *
1.26      vincent   422: bfind(const char *bname, int cflag)
1.3       millert   423: {
1.4       millert   424:        BUFFER  *bp;
                    425:        LINE    *lp;
1.15      mickey    426:        int      i;
1.1       deraadt   427:
                    428:        bp = bheadp;
                    429:        while (bp != NULL) {
1.7       art       430:                if (strcmp(bname, bp->b_bname) == 0)
1.1       deraadt   431:                        return bp;
                    432:                bp = bp->b_bufp;
                    433:        }
1.3       millert   434:        if (cflag != TRUE)
                    435:                return NULL;
1.29      vincent   436:
1.32    ! vincent   437:        bp = calloc(1, sizeof(BUFFER));
        !           438:        if (bp == NULL) {
1.1       deraadt   439:                ewprintf("Can't get %d bytes", sizeof(BUFFER));
                    440:                return NULL;
                    441:        }
1.20      vincent   442:        if ((bp->b_bname = strdup(bname)) == NULL) {
1.3       millert   443:                ewprintf("Can't get %d bytes", strlen(bname) + 1);
1.29      vincent   444:                free(bp);
1.1       deraadt   445:                return NULL;
                    446:        }
                    447:        if ((lp = lalloc(0)) == NULL) {
1.26      vincent   448:                free((char *) bp->b_bname);
1.29      vincent   449:                free(bp);
1.1       deraadt   450:                return NULL;
                    451:        }
1.3       millert   452:        bp->b_altb = bp->b_bufp = NULL;
                    453:        bp->b_dotp = lp;
                    454:        bp->b_doto = 0;
1.1       deraadt   455:        bp->b_markp = NULL;
                    456:        bp->b_marko = 0;
1.3       millert   457:        bp->b_flag = defb_flag;
                    458:        bp->b_nwnd = 0;
1.1       deraadt   459:        bp->b_linep = lp;
                    460:        bp->b_nmodes = defb_nmodes;
1.24      vincent   461:        LIST_INIT(&bp->b_undo);
1.29      vincent   462:        bp->b_undoptr = NULL;
                    463:        memset(&bp->b_undopos, 0, sizeof bp->b_undopos);
1.1       deraadt   464:        i = 0;
                    465:        do {
1.3       millert   466:                bp->b_modes[i] = defb_modes[i];
                    467:        } while (i++ < defb_nmodes);
1.1       deraadt   468:        bp->b_fname[0] = '\0';
                    469:        bzero(&bp->b_fi, sizeof(bp->b_fi));
                    470:        lp->l_fp = lp;
                    471:        lp->l_bp = lp;
                    472:        bp->b_bufp = bheadp;
                    473:        bheadp = bp;
                    474:        return bp;
                    475: }
                    476:
                    477: /*
                    478:  * This routine blows away all of the text
                    479:  * in a buffer. If the buffer is marked as changed
                    480:  * then we ask if it is ok to blow it away; this is
                    481:  * to save the user the grief of losing text. The
                    482:  * window chain is nearly always wrong if this gets
                    483:  * called; the caller must arrange for the updates
                    484:  * that are required. Return TRUE if everything
                    485:  * looks good.
                    486:  */
1.3       millert   487: int
1.26      vincent   488: bclear(BUFFER *bp)
1.3       millert   489: {
                    490:        LINE  *lp;
                    491:        int    s;
1.1       deraadt   492:
1.21      deraadt   493:        if ((bp->b_flag & BFCHG) != 0 &&        /* Changed.              */
                    494:            (s = eyesno("Buffer modified; kill anyway")) != TRUE)
1.1       deraadt   495:                return (s);
1.3       millert   496:        bp->b_flag &= ~BFCHG;   /* Not changed           */
                    497:        while ((lp = lforw(bp->b_linep)) != bp->b_linep)
1.1       deraadt   498:                lfree(lp);
1.32    ! vincent   499:        bp->b_dotp = bp->b_linep;       /* Fix dot */
1.3       millert   500:        bp->b_doto = 0;
                    501:        bp->b_markp = NULL;     /* Invalidate "mark"     */
1.1       deraadt   502:        bp->b_marko = 0;
                    503:        return TRUE;
                    504: }
                    505:
                    506: /*
                    507:  * Display the given buffer in the given window. Flags indicated
                    508:  * action on redisplay.
                    509:  */
1.3       millert   510: int
1.26      vincent   511: showbuffer(BUFFER *bp, MGWIN *wp, int flags)
1.3       millert   512: {
                    513:        BUFFER *obp;
                    514:        MGWIN  *owp;
1.1       deraadt   515:
1.32    ! vincent   516:        if (wp->w_bufp == bp) { /* Easy case! */
1.1       deraadt   517:                wp->w_flag |= flags;
1.32    ! vincent   518:                wp->w_dotp = bp->b_dotp;
        !           519:                wp->w_doto = bp->b_doto;
1.1       deraadt   520:                return TRUE;
                    521:        }
                    522:        /* First, dettach the old buffer from the window */
                    523:        if ((bp->b_altb = obp = wp->w_bufp) != NULL) {
                    524:                if (--obp->b_nwnd == 0) {
1.3       millert   525:                        obp->b_dotp = wp->w_dotp;
                    526:                        obp->b_doto = wp->w_doto;
1.1       deraadt   527:                        obp->b_markp = wp->w_markp;
                    528:                        obp->b_marko = wp->w_marko;
                    529:                }
                    530:        }
                    531:        /* Now, attach the new buffer to the window */
                    532:        wp->w_bufp = bp;
                    533:
1.3       millert   534:        if (bp->b_nwnd++ == 0) {        /* First use.            */
                    535:                wp->w_dotp = bp->b_dotp;
                    536:                wp->w_doto = bp->b_doto;
1.1       deraadt   537:                wp->w_markp = bp->b_markp;
                    538:                wp->w_marko = bp->b_marko;
                    539:        } else
1.3       millert   540:                /* already on screen, steal values from other window */
1.1       deraadt   541:                for (owp = wheadp; owp != NULL; owp = wp->w_wndp)
                    542:                        if (wp->w_bufp == bp && owp != wp) {
1.3       millert   543:                                wp->w_dotp = owp->w_dotp;
                    544:                                wp->w_doto = owp->w_doto;
1.1       deraadt   545:                                wp->w_markp = owp->w_markp;
                    546:                                wp->w_marko = owp->w_marko;
                    547:                                break;
                    548:                        }
1.3       millert   549:        wp->w_flag |= WFMODE | flags;
1.1       deraadt   550:        return TRUE;
                    551: }
                    552:
                    553: /*
                    554:  * Pop the buffer we got passed onto the screen.
                    555:  * Returns a status.
                    556:  */
1.2       millert   557: MGWIN *
1.26      vincent   558: popbuf(BUFFER *bp)
1.3       millert   559: {
                    560:        MGWIN  *wp;
1.1       deraadt   561:
1.3       millert   562:        if (bp->b_nwnd == 0) {  /* Not on screen yet.    */
                    563:                if ((wp = wpopup()) == NULL)
                    564:                        return NULL;
1.1       deraadt   565:        } else
                    566:                for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
                    567:                        if (wp->w_bufp == bp) {
1.3       millert   568:                                wp->w_flag |= WFHARD | WFFORCE;
                    569:                                return wp;
1.1       deraadt   570:                        }
1.3       millert   571:        if (showbuffer(bp, wp, WFHARD) != TRUE)
                    572:                return NULL;
1.1       deraadt   573:        return wp;
                    574: }
                    575:
                    576: /*
                    577:  * Insert another buffer at dot.  Very useful.
                    578:  */
1.3       millert   579: /* ARGSUSED */
                    580: int
1.26      vincent   581: bufferinsert(int f, int n)
1.1       deraadt   582: {
1.3       millert   583:        BUFFER *bp;
                    584:        LINE   *clp;
                    585:        int     clo;
                    586:        int     nline;
                    587:        int     s;
                    588:        char    bufn[NBUFN];
1.1       deraadt   589:
                    590:        /* Get buffer to use from user */
                    591:        if (curbp->b_altb != NULL)
1.3       millert   592:                s = eread("Insert buffer: (default %s) ", bufn, NBUFN,
1.14      art       593:                          EFNEW | EFBUF, &(curbp->b_altb->b_bname), NULL);
1.1       deraadt   594:        else
1.14      art       595:                s = eread("Insert buffer: ", bufn, NBUFN, EFNEW | EFBUF, NULL);
1.3       millert   596:        if (s == ABORT)
                    597:                return (s);
                    598:        if (s == FALSE && curbp->b_altb != NULL)
                    599:                bp = curbp->b_altb;
                    600:        else if ((bp = bfind(bufn, FALSE)) == NULL)
                    601:                return FALSE;
1.1       deraadt   602:
1.3       millert   603:        if (bp == curbp) {
1.1       deraadt   604:                ewprintf("Cannot insert buffer into self");
                    605:                return FALSE;
                    606:        }
                    607:        /* insert the buffer */
                    608:        nline = 0;
                    609:        clp = lforw(bp->b_linep);
1.3       millert   610:        for (;;) {
1.1       deraadt   611:                for (clo = 0; clo < llength(clp); clo++)
                    612:                        if (linsert(1, lgetc(clp, clo)) == FALSE)
                    613:                                return FALSE;
1.3       millert   614:                if ((clp = lforw(clp)) == bp->b_linep)
                    615:                        break;
                    616:                if (newline(FFRAND, 1) == FALSE)        /* fake newline */
1.1       deraadt   617:                        return FALSE;
                    618:                nline++;
                    619:        }
1.3       millert   620:        if (nline == 1)
                    621:                ewprintf("[Inserted 1 line]");
                    622:        else
                    623:                ewprintf("[Inserted %d lines]", nline);
1.1       deraadt   624:
1.3       millert   625:        clp = curwp->w_linep;   /* cosmetic adjustment */
                    626:        if (curwp->w_dotp == clp) {     /* for offscreen insert */
                    627:                while (nline-- && lback(clp) != curbp->b_linep)
1.1       deraadt   628:                        clp = lback(clp);
1.3       millert   629:                curwp->w_linep = clp;   /* adjust framing.       */
1.1       deraadt   630:                curwp->w_flag |= WFHARD;
                    631:        }
                    632:        return (TRUE);
                    633: }
                    634:
                    635: /*
                    636:  * Turn off the dirty bit on this buffer.
                    637:  */
1.3       millert   638: /* ARGSUSED */
                    639: int
1.26      vincent   640: notmodified(int f, int n)
1.1       deraadt   641: {
1.3       millert   642:        MGWIN *wp;
1.1       deraadt   643:
                    644:        curbp->b_flag &= ~BFCHG;
1.3       millert   645:        wp = wheadp;            /* Update mode lines.    */
1.1       deraadt   646:        while (wp != NULL) {
                    647:                if (wp->w_bufp == curbp)
                    648:                        wp->w_flag |= WFMODE;
                    649:                wp = wp->w_wndp;
                    650:        }
                    651:        ewprintf("Modification-flag cleared");
                    652:        return TRUE;
                    653: }
                    654:
                    655: #ifndef NO_HELP
                    656: /*
                    657:  * Popbuf and set all windows to top of buffer.         Currently only used by
                    658:  * help functions.
                    659:  */
1.3       millert   660: int
1.26      vincent   661: popbuftop(BUFFER *bp)
1.1       deraadt   662: {
1.3       millert   663:        MGWIN *wp;
1.1       deraadt   664:
1.3       millert   665:        bp->b_dotp = lforw(bp->b_linep);
                    666:        bp->b_doto = 0;
                    667:        if (bp->b_nwnd != 0) {
                    668:                for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
                    669:                        if (wp->w_bufp == bp) {
                    670:                                wp->w_dotp = bp->b_dotp;
                    671:                                wp->w_doto = 0;
                    672:                                wp->w_flag |= WFHARD;
                    673:                        }
                    674:        }
                    675:        return popbuf(bp) != NULL;
1.1       deraadt   676: }
                    677: #endif