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

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