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

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