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

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