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

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