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

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