[BACK]Return to line.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Annotation of src/usr.bin/mg/line.c, Revision 1.47

1.47    ! kjell       1: /*     $OpenBSD: line.c,v 1.46 2008/09/15 16:13:35 kjell Exp $ */
1.23      kjell       2:
                      3: /* This file is in the public domain. */
1.5       niklas      4:
1.1       deraadt     5: /*
                      6:  *             Text line handling.
1.8       mickey      7:  *
1.4       millert     8:  * The functions in this file are a general set of line management
1.8       mickey      9:  * utilities. They are the only routines that touch the text. They
                     10:  * also touch the buffer and window structures to make sure that the
                     11:  * necessary updating gets done.  There are routines in this file that
1.4       millert    12:  * handle the kill buffer too.  It isn't here for any good reason.
1.1       deraadt    13:  *
1.8       mickey     14:  * Note that this code only updates the dot and mark values in the window
                     15:  * list.  Since all the code acts on the current window, the buffer that
                     16:  * we are editing must be displayed, which means that "b_nwnd" is non-zero,
                     17:  * which means that the dot and mark values in the buffer headers are
1.1       deraadt    18:  * nonsense.
                     19:  */
                     20:
1.4       millert    21: #include "def.h"
                     22:
1.32      kjell      23: #include <stdlib.h>
                     24: #include <string.h>
                     25:
1.4       millert    26: /*
1.9       vincent    27:  * Allocate a new line of size `used'.  lrealloc() can be called if the line
                     28:  * ever needs to grow beyond that.
1.1       deraadt    29:  */
1.28      deraadt    30: struct line *
1.9       vincent    31: lalloc(int used)
1.3       millert    32: {
1.28      deraadt    33:        struct line *lp;
1.1       deraadt    34:
1.22      db         35:        if ((lp = malloc(sizeof(*lp))) == NULL)
                     36:                return (NULL);
1.9       vincent    37:        lp->l_text = NULL;
                     38:        lp->l_size = 0;
                     39:        lp->l_used = used;      /* XXX */
                     40:        if (lrealloc(lp, used) == FALSE) {
                     41:                free(lp);
1.22      db         42:                return (NULL);
1.1       deraadt    43:        }
1.22      db         44:        return (lp);
1.1       deraadt    45: }
                     46:
1.9       vincent    47: int
1.28      deraadt    48: lrealloc(struct line *lp, int newsize)
1.1       deraadt    49: {
1.9       vincent    50:        char *tmp;
1.1       deraadt    51:
1.18      millert    52:        if (lp->l_size < newsize) {
                     53:                if ((tmp = realloc(lp->l_text, newsize)) == NULL)
1.22      db         54:                        return (FALSE);
1.18      millert    55:                lp->l_text = tmp;
                     56:                lp->l_size = newsize;
                     57:        }
1.22      db         58:        return (TRUE);
1.1       deraadt    59: }
                     60:
                     61: /*
1.4       millert    62:  * Delete line "lp".  Fix all of the links that might point to it (they are
1.8       mickey     63:  * moved to offset 0 of the next line.  Unlink the line from whatever buffer
                     64:  * it might be in, and release the memory.  The buffers are updated too; the
1.4       millert    65:  * magic conditions described in the above comments don't hold here.
1.1       deraadt    66:  */
1.6       art        67: void
1.28      deraadt    68: lfree(struct line *lp)
1.3       millert    69: {
1.28      deraadt    70:        struct buffer   *bp;
                     71:        struct mgwin    *wp;
1.1       deraadt    72:
1.3       millert    73:        for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1       deraadt    74:                if (wp->w_linep == lp)
                     75:                        wp->w_linep = lp->l_fp;
1.3       millert    76:                if (wp->w_dotp == lp) {
                     77:                        wp->w_dotp = lp->l_fp;
                     78:                        wp->w_doto = 0;
1.1       deraadt    79:                }
                     80:                if (wp->w_markp == lp) {
                     81:                        wp->w_markp = lp->l_fp;
                     82:                        wp->w_marko = 0;
                     83:                }
                     84:        }
1.3       millert    85:        for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
1.1       deraadt    86:                if (bp->b_nwnd == 0) {
1.3       millert    87:                        if (bp->b_dotp == lp) {
1.1       deraadt    88:                                bp->b_dotp = lp->l_fp;
                     89:                                bp->b_doto = 0;
                     90:                        }
                     91:                        if (bp->b_markp == lp) {
                     92:                                bp->b_markp = lp->l_fp;
                     93:                                bp->b_marko = 0;
                     94:                        }
                     95:                }
                     96:        }
                     97:        lp->l_bp->l_fp = lp->l_fp;
                     98:        lp->l_fp->l_bp = lp->l_bp;
1.9       vincent    99:        if (lp->l_text != NULL)
                    100:                free(lp->l_text);
                    101:        free(lp);
1.1       deraadt   102: }
                    103:
                    104: /*
1.8       mickey    105:  * This routine is called when a character changes in place in the current
                    106:  * buffer. It updates all of the required flags in the buffer and window
                    107:  * system. The flag used is passed as an argument; if the buffer is being
                    108:  * displayed in more than 1 window we change EDIT to HARD. Set MODE if the
1.4       millert   109:  * mode line needs to be updated (the "*" has to be set).
1.1       deraadt   110:  */
1.6       art       111: void
1.15      vincent   112: lchange(int flag)
1.3       millert   113: {
1.28      deraadt   114:        struct mgwin    *wp;
1.1       deraadt   115:
1.4       millert   116:        /* update mode lines if this is the first change. */
                    117:        if ((curbp->b_flag & BFCHG) == 0) {
                    118:                flag |= WFMODE;
1.1       deraadt   119:                curbp->b_flag |= BFCHG;
                    120:        }
1.3       millert   121:        for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1       deraadt   122:                if (wp->w_bufp == curbp) {
1.47    ! kjell     123:                        wp->w_rflag |= flag;
1.3       millert   124:                        if (wp != curwp)
1.47    ! kjell     125:                                wp->w_rflag |= WFFULL;
1.1       deraadt   126:                }
                    127:        }
                    128: }
                    129:
                    130: /*
1.20      vincent   131:  * Insert "n" bytes from "s" at the current location of dot.
                    132:  * In the easy case all that happens is the text is stored in the line.
                    133:  * In the hard case, the line has to be reallocated.  When the window list
                    134:  * is updated, take special care; I screwed it up once.  You always update
                    135:  * dot in the current window.  You update mark and a dot in another window
                    136:  * if it is greater than the place where you did the insert. Return TRUE
                    137:  * if all is well, and FALSE on errors.
                    138:  */
                    139: int
                    140: linsert_str(const char *s, int n)
                    141: {
1.28      deraadt   142:        struct line     *lp1;
                    143:        struct mgwin    *wp;
1.20      vincent   144:        RSIZE    i;
1.45      kjell     145:        int      doto, k;
                    146:
                    147:        if ((k = checkdirty(curbp)) != TRUE)
                    148:                return (k);
1.20      vincent   149:
                    150:        if (curbp->b_flag & BFREADONLY) {
                    151:                ewprintf("Buffer is read only");
1.22      db        152:                return (FALSE);
1.20      vincent   153:        }
                    154:
                    155:        if (!n)
                    156:                return (TRUE);
                    157:
1.39      kjell     158:        lchange(WFFULL);
1.20      vincent   159:
                    160:        /* current line */
                    161:        lp1 = curwp->w_dotp;
                    162:
                    163:        /* special case for the end */
1.41      kjell     164:        if (lp1 == curbp->b_headp) {
1.28      deraadt   165:                struct line *lp2, *lp3;
1.20      vincent   166:
                    167:                /* now should only happen in empty buffer */
                    168:                if (curwp->w_doto != 0)
                    169:                        panic("bug: linsert_str");
                    170:                /* allocate a new line */
                    171:                if ((lp2 = lalloc(n)) == NULL)
1.22      db        172:                        return (FALSE);
1.20      vincent   173:                /* previous line */
                    174:                lp3 = lp1->l_bp;
                    175:                /* link in */
                    176:                lp3->l_fp = lp2;
                    177:                lp2->l_fp = lp1;
                    178:                lp1->l_bp = lp2;
                    179:                lp2->l_bp = lp3;
                    180:                for (i = 0; i < n; ++i)
                    181:                        lp2->l_text[i] = s[i];
                    182:                for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
                    183:                        if (wp->w_linep == lp1)
                    184:                                wp->w_linep = lp2;
                    185:                        if (wp->w_dotp == lp1)
                    186:                                wp->w_dotp = lp2;
                    187:                        if (wp->w_markp == lp1)
                    188:                                wp->w_markp = lp2;
                    189:                }
                    190:                undo_add_insert(lp2, 0, n);
                    191:                curwp->w_doto = n;
1.22      db        192:                return (TRUE);
1.20      vincent   193:        }
                    194:        /* save for later */
                    195:        doto = curwp->w_doto;
                    196:
                    197:        if ((lp1->l_used + n) > lp1->l_size) {
                    198:                if (lrealloc(lp1, lp1->l_used + n) == FALSE)
1.22      db        199:                        return (FALSE);
1.20      vincent   200:        }
                    201:        lp1->l_used += n;
                    202:        if (lp1->l_used != n)
                    203:                memmove(&lp1->l_text[doto + n], &lp1->l_text[doto],
                    204:                    lp1->l_used - n - doto);
                    205:
                    206:        /* Add the characters */
                    207:        for (i = 0; i < n; ++i)
                    208:                lp1->l_text[doto + i] = s[i];
                    209:        for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
                    210:                if (wp->w_dotp == lp1) {
                    211:                        if (wp == curwp || wp->w_doto > doto)
                    212:                                wp->w_doto += n;
                    213:                }
                    214:                if (wp->w_markp == lp1) {
                    215:                        if (wp->w_marko > doto)
                    216:                                wp->w_marko += n;
                    217:                }
                    218:        }
                    219:        undo_add_insert(curwp->w_dotp, doto, n);
1.22      db        220:        return (TRUE);
1.20      vincent   221: }
                    222:
                    223: /*
1.8       mickey    224:  * Insert "n" copies of the character "c" at the current location of dot.
                    225:  * In the easy case all that happens is the text is stored in the line.
                    226:  * In the hard case, the line has to be reallocated.  When the window list
                    227:  * is updated, take special care; I screwed it up once.  You always update
1.4       millert   228:  * dot in the current window.  You update mark and a dot in another window
                    229:  * if it is greater than the place where you did the insert. Return TRUE
1.1       deraadt   230:  * if all is well, and FALSE on errors.
                    231:  */
1.4       millert   232: int
1.15      vincent   233: linsert(int n, int c)
1.1       deraadt   234: {
1.28      deraadt   235:        struct line     *lp1;
                    236:        struct mgwin    *wp;
1.4       millert   237:        RSIZE    i;
                    238:        int      doto;
1.45      kjell     239:        int s;
1.1       deraadt   240:
1.20      vincent   241:        if (!n)
                    242:                return (TRUE);
                    243:
1.45      kjell     244:        if ((s = checkdirty(curbp)) != TRUE)
                    245:                return (s);
                    246:
1.15      vincent   247:        if (curbp->b_flag & BFREADONLY) {
                    248:                ewprintf("Buffer is read only");
1.22      db        249:                return (FALSE);
1.15      vincent   250:        }
                    251:
1.1       deraadt   252:        lchange(WFEDIT);
1.4       millert   253:
                    254:        /* current line */
                    255:        lp1 = curwp->w_dotp;
1.15      vincent   256:
1.4       millert   257:        /* special case for the end */
1.41      kjell     258:        if (lp1 == curbp->b_headp) {
1.28      deraadt   259:                struct line *lp2, *lp3;
1.11      deraadt   260:
1.4       millert   261:                /* now should only happen in empty buffer */
1.1       deraadt   262:                if (curwp->w_doto != 0) {
                    263:                        ewprintf("bug: linsert");
1.22      db        264:                        return (FALSE);
1.1       deraadt   265:                }
1.4       millert   266:                /* allocate a new line */
1.9       vincent   267:                if ((lp2 = lalloc(n)) == NULL)
1.22      db        268:                        return (FALSE);
1.4       millert   269:                /* previous line */
                    270:                lp3 = lp1->l_bp;
                    271:                /* link in */
                    272:                lp3->l_fp = lp2;
1.1       deraadt   273:                lp2->l_fp = lp1;
                    274:                lp1->l_bp = lp2;
                    275:                lp2->l_bp = lp3;
1.3       millert   276:                for (i = 0; i < n; ++i)
1.1       deraadt   277:                        lp2->l_text[i] = c;
1.3       millert   278:                for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1       deraadt   279:                        if (wp->w_linep == lp1)
                    280:                                wp->w_linep = lp2;
                    281:                        if (wp->w_dotp == lp1)
                    282:                                wp->w_dotp = lp2;
                    283:                        if (wp->w_markp == lp1)
                    284:                                wp->w_markp = lp2;
                    285:                }
1.16      vincent   286:                undo_add_insert(lp2, 0, n);
1.1       deraadt   287:                curwp->w_doto = n;
1.22      db        288:                return (TRUE);
1.1       deraadt   289:        }
1.4       millert   290:        /* save for later */
                    291:        doto = curwp->w_doto;
1.9       vincent   292:
                    293:        if ((lp1->l_used + n) > lp1->l_size) {
                    294:                if (lrealloc(lp1, lp1->l_used + n) == FALSE)
1.22      db        295:                        return (FALSE);
1.11      deraadt   296:        }
1.9       vincent   297:        lp1->l_used += n;
1.11      deraadt   298:        if (lp1->l_used != n)
1.9       vincent   299:                memmove(&lp1->l_text[doto + n], &lp1->l_text[doto],
                    300:                    lp1->l_used - n - doto);
                    301:
1.4       millert   302:        /* Add the characters */
                    303:        for (i = 0; i < n; ++i)
1.9       vincent   304:                lp1->l_text[doto + i] = c;
1.3       millert   305:        for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1       deraadt   306:                if (wp->w_dotp == lp1) {
1.3       millert   307:                        if (wp == curwp || wp->w_doto > doto)
1.1       deraadt   308:                                wp->w_doto += n;
                    309:                }
                    310:                if (wp->w_markp == lp1) {
                    311:                        if (wp->w_marko > doto)
                    312:                                wp->w_marko += n;
                    313:                }
                    314:        }
1.16      vincent   315:        undo_add_insert(curwp->w_dotp, doto, n);
1.22      db        316:        return (TRUE);
1.1       deraadt   317: }
                    318:
1.44      kjell     319: /*
                    320:  * Do the work of inserting a newline at the given line/offset.
                    321:  * If mark is on the current line, we may have to move the markline
                    322:  * to keep line numbers in sync.
                    323:  * lnewline_at assumes the current buffer is writable. Checking for
                    324:  * this fact should be done by the caller.
                    325:  */
1.4       millert   326: int
1.28      deraadt   327: lnewline_at(struct line *lp1, int doto)
1.1       deraadt   328: {
1.28      deraadt   329:        struct line     *lp2;
1.21      vincent   330:        int      nlen;
1.28      deraadt   331:        struct mgwin    *wp;
1.1       deraadt   332:
1.39      kjell     333:        lchange(WFFULL);
1.14      vincent   334:
1.44      kjell     335:        curwp->w_bufp->b_lines++;
                    336:        /* Check if mark is past dot (even on current line) */
                    337:        if (curwp->w_markline > curwp->w_dotline  ||
                    338:           (curwp->w_dotline == curwp->w_markline &&
                    339:            curwp->w_marko >= doto))
                    340:                curwp->w_markline++;
                    341:        curwp->w_dotline++;
                    342:
1.35      kjell     343:        /* If start of line, allocate a new line instead of copying */
1.4       millert   344:        if (doto == 0) {
                    345:                /* new first part */
1.35      kjell     346:                if ((lp2 = lalloc(0)) == NULL)
                    347:                        return (FALSE);
1.1       deraadt   348:                lp2->l_bp = lp1->l_bp;
                    349:                lp1->l_bp->l_fp = lp2;
                    350:                lp2->l_fp = lp1;
                    351:                lp1->l_bp = lp2;
1.3       millert   352:                for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
                    353:                        if (wp->w_linep == lp1)
                    354:                                wp->w_linep = lp2;
1.46      kjell     355:                undo_add_boundary(FFRAND, 1);
1.35      kjell     356:                undo_add_insert(lp2, 0, 1);
1.46      kjell     357:                undo_add_boundary(FFRAND, 1);
1.35      kjell     358:                return (TRUE);
1.1       deraadt   359:        }
1.4       millert   360:
                    361:        /* length of new part */
                    362:        nlen = llength(lp1) - doto;
                    363:
                    364:        /* new second half line */
1.35      kjell     365:        if ((lp2 = lalloc(nlen)) == NULL)
                    366:                return (FALSE);
1.3       millert   367:        if (nlen != 0)
                    368:                bcopy(&lp1->l_text[doto], &lp2->l_text[0], nlen);
1.1       deraadt   369:        lp1->l_used = doto;
                    370:        lp2->l_bp = lp1;
                    371:        lp2->l_fp = lp1->l_fp;
                    372:        lp1->l_fp = lp2;
                    373:        lp2->l_fp->l_bp = lp2;
1.4       millert   374:        /* Windows */
                    375:        for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1       deraadt   376:                if (wp->w_dotp == lp1 && wp->w_doto >= doto) {
                    377:                        wp->w_dotp = lp2;
                    378:                        wp->w_doto -= doto;
                    379:                }
                    380:                if (wp->w_markp == lp1 && wp->w_marko >= doto) {
                    381:                        wp->w_markp = lp2;
                    382:                        wp->w_marko -= doto;
                    383:                }
                    384:        }
1.46      kjell     385:        undo_add_boundary(FFRAND, 1);
1.24      kjell     386:        undo_add_insert(lp1, llength(lp1), 1);
1.46      kjell     387:        undo_add_boundary(FFRAND, 1);
1.35      kjell     388:        return (TRUE);
1.1       deraadt   389: }
                    390:
                    391: /*
1.21      vincent   392:  * Insert a newline into the buffer at the current location of dot in the
                    393:  * current window.
                    394:  */
                    395: int
                    396: lnewline(void)
                    397: {
1.45      kjell     398:        int s;
                    399:
                    400:        if ((s = checkdirty(curbp)) != TRUE)
                    401:                return (s);
1.21      vincent   402:        if (curbp->b_flag & BFREADONLY) {
                    403:                ewprintf("Buffer is read only");
1.22      db        404:                return (FALSE);
1.21      vincent   405:        }
1.22      db        406:        return (lnewline_at(curwp->w_dotp, curwp->w_doto));
1.21      vincent   407: }
                    408:
                    409: /*
1.36      kjell     410:  * This function deletes "n" bytes, starting at dot. (actually, n+1, as the
                    411:  * newline is included) It understands how to deal with end of lines, etc.
                    412:  * It returns TRUE if all of the characters were deleted, and FALSE if
                    413:  * they were not (because dot ran into the end of the buffer).
                    414:  * The "kflag" indicates either no insertion, or direction  of insertion
                    415:  * into the kill buffer.
1.1       deraadt   416:  */
1.4       millert   417: int
1.15      vincent   418: ldelete(RSIZE n, int kflag)
1.3       millert   419: {
1.28      deraadt   420:        struct line     *dotp;
1.36      kjell     421:        RSIZE            chunk;
1.28      deraadt   422:        struct mgwin    *wp;
1.36      kjell     423:        int              doto;
                    424:        char            *cp1, *cp2;
                    425:        size_t           len;
                    426:        char            *sv;
                    427:        int              end;
1.45      kjell     428:        int              s;
1.1       deraadt   429:
1.45      kjell     430:        if ((s = checkdirty(curbp)) != TRUE)
                    431:                return (s);
1.15      vincent   432:        if (curbp->b_flag & BFREADONLY) {
                    433:                ewprintf("Buffer is read only");
1.22      db        434:                return (FALSE);
1.15      vincent   435:        }
1.32      kjell     436:        len = n;
                    437:        if ((sv = calloc(1, len + 1)) == NULL)
                    438:                return (FALSE);
                    439:        end = 0;
1.15      vincent   440:
1.16      vincent   441:        undo_add_delete(curwp->w_dotp, curwp->w_doto, n);
1.15      vincent   442:
1.1       deraadt   443:        while (n != 0) {
                    444:                dotp = curwp->w_dotp;
                    445:                doto = curwp->w_doto;
1.4       millert   446:                /* Hit the end of the buffer */
1.41      kjell     447:                if (dotp == curbp->b_headp)
1.22      db        448:                        return (FALSE);
1.4       millert   449:                /* Size of the chunk */
                    450:                chunk = dotp->l_used - doto;
1.20      vincent   451:
1.1       deraadt   452:                if (chunk > n)
                    453:                        chunk = n;
1.4       millert   454:                /* End of line, merge */
                    455:                if (chunk == 0) {
1.42      kjell     456:                        if (dotp == blastlp(curbp))
1.22      db        457:                                return (FALSE);
1.39      kjell     458:                        lchange(WFFULL);
1.32      kjell     459:                        if (ldelnewline() == FALSE)
1.22      db        460:                                return (FALSE);
1.32      kjell     461:                        end = strlcat(sv, "\n", len + 1);
1.1       deraadt   462:                        --n;
                    463:                        continue;
                    464:                }
                    465:                lchange(WFEDIT);
1.4       millert   466:                /* Scrunch text */
                    467:                cp1 = &dotp->l_text[doto];
1.32      kjell     468:                memcpy(&sv[end], cp1, chunk);
                    469:                end += chunk;
                    470:                sv[end] = '\0';
1.20      vincent   471:                for (cp2 = cp1 + chunk; cp2 < &dotp->l_text[dotp->l_used];
                    472:                    cp2++)
                    473:                        *cp1++ = *cp2;
1.4       millert   474:                dotp->l_used -= (int)chunk;
1.3       millert   475:                for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
                    476:                        if (wp->w_dotp == dotp && wp->w_doto >= doto) {
                    477:                                /* NOSTRICT */
1.1       deraadt   478:                                wp->w_doto -= chunk;
                    479:                                if (wp->w_doto < doto)
                    480:                                        wp->w_doto = doto;
                    481:                        }
1.3       millert   482:                        if (wp->w_markp == dotp && wp->w_marko >= doto) {
                    483:                                /* NOSTRICT */
1.1       deraadt   484:                                wp->w_marko -= chunk;
                    485:                                if (wp->w_marko < doto)
                    486:                                        wp->w_marko = doto;
                    487:                        }
                    488:                }
                    489:                n -= chunk;
                    490:        }
1.34      kjell     491:        if (kchunk(sv, (RSIZE)len, kflag) != TRUE)
1.32      kjell     492:                return (FALSE);
                    493:        free(sv);
1.22      db        494:        return (TRUE);
1.1       deraadt   495: }
                    496:
                    497: /*
1.8       mickey    498:  * Delete a newline and join the current line with the next line. If the next
1.4       millert   499:  * line is the magic header line always return TRUE; merging the last line
1.8       mickey    500:  * with the header line can be thought of as always being a successful
                    501:  * operation.  Even if nothing is done, this makes the kill buffer work
1.44      kjell     502:  * "right". If the mark is past the dot (actually, markline > dotline),
                    503:  * decrease the markline accordingly to keep line numbers in sync.
                    504:  * Easy cases can be done by shuffling data around.  Hard cases
1.8       mickey    505:  * require that lines be moved about in memory.  Return FALSE on error and
1.40      kjell     506:  * TRUE if all looks ok. We do not update w_dotline here, as deletes are done
                    507:  * after moves.
1.1       deraadt   508:  */
1.4       millert   509: int
1.15      vincent   510: ldelnewline(void)
1.3       millert   511: {
1.28      deraadt   512:        struct line     *lp1, *lp2, *lp3;
                    513:        struct mgwin    *wp;
1.45      kjell     514:        int s;
1.1       deraadt   515:
1.45      kjell     516:        if ((s = checkdirty(curbp)) != TRUE)
                    517:                return (s);
1.15      vincent   518:        if (curbp->b_flag & BFREADONLY) {
                    519:                ewprintf("Buffer is read only");
1.22      db        520:                return (FALSE);
1.15      vincent   521:        }
                    522:
1.1       deraadt   523:        lp1 = curwp->w_dotp;
                    524:        lp2 = lp1->l_fp;
1.4       millert   525:        /* at the end of the buffer */
1.41      kjell     526:        if (lp2 == curbp->b_headp)
1.22      db        527:                return (TRUE);
1.44      kjell     528:        /* Keep line counts in sync */
1.40      kjell     529:        curwp->w_bufp->b_lines--;
1.44      kjell     530:        if (curwp->w_markline > curwp->w_dotline)
                    531:                curwp->w_markline--;
1.1       deraadt   532:        if (lp2->l_used <= lp1->l_size - lp1->l_used) {
                    533:                bcopy(&lp2->l_text[0], &lp1->l_text[lp1->l_used], lp2->l_used);
1.3       millert   534:                for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1       deraadt   535:                        if (wp->w_linep == lp2)
                    536:                                wp->w_linep = lp1;
                    537:                        if (wp->w_dotp == lp2) {
1.3       millert   538:                                wp->w_dotp = lp1;
1.1       deraadt   539:                                wp->w_doto += lp1->l_used;
                    540:                        }
                    541:                        if (wp->w_markp == lp2) {
1.3       millert   542:                                wp->w_markp = lp1;
1.1       deraadt   543:                                wp->w_marko += lp1->l_used;
                    544:                        }
                    545:                }
                    546:                lp1->l_used += lp2->l_used;
                    547:                lp1->l_fp = lp2->l_fp;
                    548:                lp2->l_fp->l_bp = lp1;
1.37      kjell     549:                free(lp2);
1.22      db        550:                return (TRUE);
1.1       deraadt   551:        }
1.3       millert   552:        if ((lp3 = lalloc(lp1->l_used + lp2->l_used)) == NULL)
1.22      db        553:                return (FALSE);
1.1       deraadt   554:        bcopy(&lp1->l_text[0], &lp3->l_text[0], lp1->l_used);
                    555:        bcopy(&lp2->l_text[0], &lp3->l_text[lp1->l_used], lp2->l_used);
                    556:        lp1->l_bp->l_fp = lp3;
                    557:        lp3->l_fp = lp2->l_fp;
                    558:        lp2->l_fp->l_bp = lp3;
                    559:        lp3->l_bp = lp1->l_bp;
1.3       millert   560:        for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
                    561:                if (wp->w_linep == lp1 || wp->w_linep == lp2)
1.1       deraadt   562:                        wp->w_linep = lp3;
                    563:                if (wp->w_dotp == lp1)
1.3       millert   564:                        wp->w_dotp = lp3;
1.1       deraadt   565:                else if (wp->w_dotp == lp2) {
1.3       millert   566:                        wp->w_dotp = lp3;
1.1       deraadt   567:                        wp->w_doto += lp1->l_used;
                    568:                }
                    569:                if (wp->w_markp == lp1)
1.3       millert   570:                        wp->w_markp = lp3;
1.1       deraadt   571:                else if (wp->w_markp == lp2) {
1.3       millert   572:                        wp->w_markp = lp3;
1.1       deraadt   573:                        wp->w_marko += lp1->l_used;
                    574:                }
                    575:        }
1.37      kjell     576:        free(lp1);
                    577:        free(lp2);
1.22      db        578:        return (TRUE);
1.1       deraadt   579: }
1.9       vincent   580:
1.1       deraadt   581: /*
1.8       mickey    582:  * Replace plen characters before dot with argument string.  Control-J
                    583:  * characters in st are interpreted as newlines.  There is a casehack
                    584:  * disable flag (normally it likes to match case of replacement to what
1.4       millert   585:  * was there).
1.1       deraadt   586:  */
1.4       millert   587: int
1.27      kjell     588: lreplace(RSIZE plen, char *st)
1.4       millert   589: {
                    590:        RSIZE   rlen;   /* replacement length            */
1.45      kjell     591:        int s;
1.15      vincent   592:
1.45      kjell     593:        if ((s = checkdirty(curbp)) != TRUE)
                    594:                return (s);
1.15      vincent   595:        if (curbp->b_flag & BFREADONLY) {
                    596:                ewprintf("Buffer is read only");
1.22      db        597:                return (FALSE);
1.15      vincent   598:        }
1.46      kjell     599:        undo_boundary_enable(FFRAND, 0);
1.25      deraadt   600:
1.6       art       601:        (void)backchar(FFARG | FFRAND, (int)plen);
1.24      kjell     602:        (void)ldelete(plen, KNONE);
1.15      vincent   603:
1.1       deraadt   604:        rlen = strlen(st);
1.24      kjell     605:        region_put_data(st, rlen);
1.39      kjell     606:        lchange(WFFULL);
1.1       deraadt   607:
1.46      kjell     608:        undo_boundary_enable(FFRAND, 1);
1.29      kjell     609:        return (TRUE);
1.38      kjell     610: }
                    611:
                    612: /*
                    613:  * Allocate and return the supplied line as a C string
                    614:  */
                    615: char *
                    616: linetostr(const struct line *ln)
                    617: {
                    618:        size_t   len;
                    619:        char    *line;
                    620:
                    621:        len = llength(ln);
                    622:        if (len == SIZE_MAX)  /* (len + 1) overflow */
                    623:                return (NULL);
                    624:
                    625:        if ((line = malloc(len + 1)) == NULL)
                    626:                return (NULL);
                    627:
                    628:        (void)memcpy(line, ltext(ln), len);
                    629:        line[len] = '\0';
                    630:
                    631:        return (line);
1.1       deraadt   632: }