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

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