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

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