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

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