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