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