Annotation of src/usr.bin/mg/line.c, Revision 1.33
1.33 ! kjell 1: /* $OpenBSD: line.c,v 1.32 2005/11/22 04:38:57 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
11: * necessary updating gets done. There are routines in this file that
1.4 millert 12: * handle the kill buffer too. It isn't here for any good reason.
1.1 deraadt 13: *
1.8 mickey 14: * Note that this code only updates the dot and mark values in the window
15: * list. Since all the code acts on the current window, the buffer that
16: * we are editing must be displayed, which means that "b_nwnd" is non-zero,
17: * which means that the dot and mark values in the buffer headers are
1.1 deraadt 18: * nonsense.
19: */
20:
1.4 millert 21: #include "def.h"
22:
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) {
123: wp->w_flag |= flag;
1.3 millert 124: if (wp != curwp)
125: wp->w_flag |= WFHARD;
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;
145: int doto;
146:
147: if (curbp->b_flag & BFREADONLY) {
148: ewprintf("Buffer is read only");
1.22 db 149: return (FALSE);
1.20 vincent 150: }
151:
152: if (!n)
153: return (TRUE);
154:
155: lchange(WFHARD);
156:
157: /* current line */
158: lp1 = curwp->w_dotp;
159:
160: /* special case for the end */
161: if (lp1 == curbp->b_linep) {
1.28 deraadt 162: struct line *lp2, *lp3;
1.20 vincent 163:
164: /* now should only happen in empty buffer */
165: if (curwp->w_doto != 0)
166: panic("bug: linsert_str");
167: /* allocate a new line */
168: if ((lp2 = lalloc(n)) == NULL)
1.22 db 169: return (FALSE);
1.20 vincent 170: /* previous line */
171: lp3 = lp1->l_bp;
172: /* link in */
173: lp3->l_fp = lp2;
174: lp2->l_fp = lp1;
175: lp1->l_bp = lp2;
176: lp2->l_bp = lp3;
177: for (i = 0; i < n; ++i)
178: lp2->l_text[i] = s[i];
179: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
180: if (wp->w_linep == lp1)
181: wp->w_linep = lp2;
182: if (wp->w_dotp == lp1)
183: wp->w_dotp = lp2;
184: if (wp->w_markp == lp1)
185: wp->w_markp = lp2;
186: }
187: undo_add_insert(lp2, 0, n);
188: curwp->w_doto = n;
1.22 db 189: return (TRUE);
1.20 vincent 190: }
191: /* save for later */
192: doto = curwp->w_doto;
193:
194: if ((lp1->l_used + n) > lp1->l_size) {
195: if (lrealloc(lp1, lp1->l_used + n) == FALSE)
1.22 db 196: return (FALSE);
1.20 vincent 197: }
198: lp1->l_used += n;
199: if (lp1->l_used != n)
200: memmove(&lp1->l_text[doto + n], &lp1->l_text[doto],
201: lp1->l_used - n - doto);
202:
203: /* Add the characters */
204: for (i = 0; i < n; ++i)
205: lp1->l_text[doto + i] = s[i];
206: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
207: if (wp->w_dotp == lp1) {
208: if (wp == curwp || wp->w_doto > doto)
209: wp->w_doto += n;
210: }
211: if (wp->w_markp == lp1) {
212: if (wp->w_marko > doto)
213: wp->w_marko += n;
214: }
215: }
216: undo_add_insert(curwp->w_dotp, doto, n);
1.22 db 217: return (TRUE);
1.20 vincent 218: }
219:
220: /*
1.8 mickey 221: * Insert "n" copies of the character "c" at the current location of dot.
222: * In the easy case all that happens is the text is stored in the line.
223: * In the hard case, the line has to be reallocated. When the window list
224: * is updated, take special care; I screwed it up once. You always update
1.4 millert 225: * dot in the current window. You update mark and a dot in another window
226: * if it is greater than the place where you did the insert. Return TRUE
1.1 deraadt 227: * if all is well, and FALSE on errors.
228: */
1.4 millert 229: int
1.15 vincent 230: linsert(int n, int c)
1.1 deraadt 231: {
1.28 deraadt 232: struct line *lp1;
233: struct mgwin *wp;
1.4 millert 234: RSIZE i;
235: int doto;
1.1 deraadt 236:
1.20 vincent 237: if (!n)
238: return (TRUE);
239:
1.15 vincent 240: if (curbp->b_flag & BFREADONLY) {
241: ewprintf("Buffer is read only");
1.22 db 242: return (FALSE);
1.15 vincent 243: }
244:
1.1 deraadt 245: lchange(WFEDIT);
1.4 millert 246:
247: /* current line */
248: lp1 = curwp->w_dotp;
1.15 vincent 249:
1.4 millert 250: /* special case for the end */
251: if (lp1 == curbp->b_linep) {
1.28 deraadt 252: struct line *lp2, *lp3;
1.11 deraadt 253:
1.4 millert 254: /* now should only happen in empty buffer */
1.1 deraadt 255: if (curwp->w_doto != 0) {
256: ewprintf("bug: linsert");
1.22 db 257: return (FALSE);
1.1 deraadt 258: }
1.4 millert 259: /* allocate a new line */
1.9 vincent 260: if ((lp2 = lalloc(n)) == NULL)
1.22 db 261: return (FALSE);
1.4 millert 262: /* previous line */
263: lp3 = lp1->l_bp;
264: /* link in */
265: lp3->l_fp = lp2;
1.1 deraadt 266: lp2->l_fp = lp1;
267: lp1->l_bp = lp2;
268: lp2->l_bp = lp3;
1.3 millert 269: for (i = 0; i < n; ++i)
1.1 deraadt 270: lp2->l_text[i] = c;
1.3 millert 271: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 272: if (wp->w_linep == lp1)
273: wp->w_linep = lp2;
274: if (wp->w_dotp == lp1)
275: wp->w_dotp = lp2;
276: if (wp->w_markp == lp1)
277: wp->w_markp = lp2;
278: }
1.16 vincent 279: undo_add_insert(lp2, 0, n);
1.1 deraadt 280: curwp->w_doto = n;
1.22 db 281: return (TRUE);
1.1 deraadt 282: }
1.4 millert 283: /* save for later */
284: doto = curwp->w_doto;
1.9 vincent 285:
286: if ((lp1->l_used + n) > lp1->l_size) {
287: if (lrealloc(lp1, lp1->l_used + n) == FALSE)
1.22 db 288: return (FALSE);
1.11 deraadt 289: }
1.9 vincent 290: lp1->l_used += n;
1.11 deraadt 291: if (lp1->l_used != n)
1.9 vincent 292: memmove(&lp1->l_text[doto + n], &lp1->l_text[doto],
293: lp1->l_used - n - doto);
294:
1.4 millert 295: /* Add the characters */
296: for (i = 0; i < n; ++i)
1.9 vincent 297: lp1->l_text[doto + i] = c;
1.3 millert 298: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 299: if (wp->w_dotp == lp1) {
1.3 millert 300: if (wp == curwp || wp->w_doto > doto)
1.1 deraadt 301: wp->w_doto += n;
302: }
303: if (wp->w_markp == lp1) {
304: if (wp->w_marko > doto)
305: wp->w_marko += n;
306: }
307: }
1.16 vincent 308: undo_add_insert(curwp->w_dotp, doto, n);
1.22 db 309: return (TRUE);
1.1 deraadt 310: }
311:
1.4 millert 312: int
1.28 deraadt 313: lnewline_at(struct line *lp1, int doto)
1.1 deraadt 314: {
1.28 deraadt 315: struct line *lp2;
1.21 vincent 316: int nlen;
1.28 deraadt 317: struct mgwin *wp;
1.24 kjell 318: int retval = TRUE;
1.1 deraadt 319:
320: lchange(WFHARD);
1.14 vincent 321:
1.4 millert 322: /* avoid unnecessary copying */
323: if (doto == 0) {
324: /* new first part */
1.24 kjell 325: if ((lp2 = lalloc(0)) == NULL) {
326: retval = FALSE;
327: goto lnl_done;
328: }
1.1 deraadt 329: lp2->l_bp = lp1->l_bp;
330: lp1->l_bp->l_fp = lp2;
331: lp2->l_fp = lp1;
332: lp1->l_bp = lp2;
1.3 millert 333: for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
334: if (wp->w_linep == lp1)
335: wp->w_linep = lp2;
1.24 kjell 336: goto lnl_done;
1.1 deraadt 337: }
1.4 millert 338:
339: /* length of new part */
340: nlen = llength(lp1) - doto;
341:
342: /* new second half line */
1.24 kjell 343: if ((lp2 = lalloc(nlen)) == NULL) {
344: retval = FALSE;
345: goto lnl_done;
346: }
1.3 millert 347: if (nlen != 0)
348: bcopy(&lp1->l_text[doto], &lp2->l_text[0], nlen);
1.1 deraadt 349: lp1->l_used = doto;
350: lp2->l_bp = lp1;
351: lp2->l_fp = lp1->l_fp;
352: lp1->l_fp = lp2;
353: lp2->l_fp->l_bp = lp2;
1.4 millert 354: /* Windows */
355: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 356: if (wp->w_dotp == lp1 && wp->w_doto >= doto) {
357: wp->w_dotp = lp2;
358: wp->w_doto -= doto;
359: }
360: if (wp->w_markp == lp1 && wp->w_marko >= doto) {
361: wp->w_markp = lp2;
362: wp->w_marko -= doto;
363: }
364: }
1.24 kjell 365: lnl_done:
366: undo_add_boundary();
367: undo_add_insert(lp1, llength(lp1), 1);
368: undo_add_boundary();
369: return (retval);
1.1 deraadt 370: }
371:
372: /*
1.21 vincent 373: * Insert a newline into the buffer at the current location of dot in the
374: * current window.
375: */
376: int
377: lnewline(void)
378: {
379: if (curbp->b_flag & BFREADONLY) {
380: ewprintf("Buffer is read only");
1.22 db 381: return (FALSE);
1.21 vincent 382: }
1.22 db 383: return (lnewline_at(curwp->w_dotp, curwp->w_doto));
1.21 vincent 384: }
385:
386: /*
1.8 mickey 387: * This function deletes "n" bytes, starting at dot. It understands how to
388: * deal with end of lines, etc. It returns TRUE if all of the characters
389: * were deleted, and FALSE if they were not (because dot ran into the end
390: * of the buffer. The "kflag" indicates either no insertion, or direction
1.4 millert 391: * of insertion into the kill buffer.
1.1 deraadt 392: */
1.4 millert 393: int
1.15 vincent 394: ldelete(RSIZE n, int kflag)
1.3 millert 395: {
1.28 deraadt 396: struct line *dotp;
1.4 millert 397: RSIZE chunk;
1.28 deraadt 398: struct mgwin *wp;
1.4 millert 399: int doto;
400: char *cp1, *cp2;
1.32 kjell 401: size_t len;
402: char *sv;
403: int end;
1.1 deraadt 404:
1.15 vincent 405: if (curbp->b_flag & BFREADONLY) {
406: ewprintf("Buffer is read only");
1.22 db 407: return (FALSE);
1.15 vincent 408: }
1.32 kjell 409: len = n;
410: if ((sv = calloc(1, len + 1)) == NULL)
411: return (FALSE);
412: end = 0;
1.15 vincent 413:
1.16 vincent 414: undo_add_delete(curwp->w_dotp, curwp->w_doto, n);
1.15 vincent 415:
1.1 deraadt 416: while (n != 0) {
417: dotp = curwp->w_dotp;
418: doto = curwp->w_doto;
1.4 millert 419: /* Hit the end of the buffer */
420: if (dotp == curbp->b_linep)
1.22 db 421: return (FALSE);
1.4 millert 422: /* Size of the chunk */
423: chunk = dotp->l_used - doto;
1.20 vincent 424:
1.1 deraadt 425: if (chunk > n)
426: chunk = n;
1.4 millert 427: /* End of line, merge */
428: if (chunk == 0) {
1.3 millert 429: if (dotp == lback(curbp->b_linep))
1.4 millert 430: /* End of buffer */
1.22 db 431: return (FALSE);
1.1 deraadt 432: lchange(WFHARD);
1.32 kjell 433: if (ldelnewline() == FALSE)
1.22 db 434: return (FALSE);
1.32 kjell 435: end = strlcat(sv, "\n", len + 1);
1.1 deraadt 436: --n;
437: continue;
438: }
439: lchange(WFEDIT);
1.4 millert 440: /* Scrunch text */
441: cp1 = &dotp->l_text[doto];
1.32 kjell 442: memcpy(&sv[end], cp1, chunk);
443: end += chunk;
444: sv[end] = '\0';
1.20 vincent 445: for (cp2 = cp1 + chunk; cp2 < &dotp->l_text[dotp->l_used];
446: cp2++)
447: *cp1++ = *cp2;
1.4 millert 448: dotp->l_used -= (int)chunk;
1.3 millert 449: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
450: if (wp->w_dotp == dotp && wp->w_doto >= doto) {
451: /* NOSTRICT */
1.1 deraadt 452: wp->w_doto -= chunk;
453: if (wp->w_doto < doto)
454: wp->w_doto = doto;
455: }
1.3 millert 456: if (wp->w_markp == dotp && wp->w_marko >= doto) {
457: /* NOSTRICT */
1.1 deraadt 458: wp->w_marko -= chunk;
459: if (wp->w_marko < doto)
460: wp->w_marko = doto;
461: }
462: }
463: n -= chunk;
464: }
1.32 kjell 465: if (kchunk(sv, len, kflag) != TRUE)
466: return (FALSE);
467: free(sv);
1.22 db 468: return (TRUE);
1.1 deraadt 469: }
470:
471: /*
1.8 mickey 472: * Delete a newline and join the current line with the next line. If the next
1.4 millert 473: * line is the magic header line always return TRUE; merging the last line
1.8 mickey 474: * with the header line can be thought of as always being a successful
475: * operation. Even if nothing is done, this makes the kill buffer work
476: * "right". Easy cases can be done by shuffling data around. Hard cases
477: * require that lines be moved about in memory. Return FALSE on error and
1.4 millert 478: * TRUE if all looks ok.
1.1 deraadt 479: */
1.4 millert 480: int
1.15 vincent 481: ldelnewline(void)
1.3 millert 482: {
1.28 deraadt 483: struct line *lp1, *lp2, *lp3;
484: struct mgwin *wp;
1.1 deraadt 485:
1.15 vincent 486: if (curbp->b_flag & BFREADONLY) {
487: ewprintf("Buffer is read only");
1.22 db 488: return (FALSE);
1.15 vincent 489: }
490:
1.1 deraadt 491: lp1 = curwp->w_dotp;
492: lp2 = lp1->l_fp;
1.4 millert 493: /* at the end of the buffer */
494: if (lp2 == curbp->b_linep)
1.22 db 495: return (TRUE);
1.1 deraadt 496: if (lp2->l_used <= lp1->l_size - lp1->l_used) {
497: bcopy(&lp2->l_text[0], &lp1->l_text[lp1->l_used], lp2->l_used);
1.3 millert 498: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 499: if (wp->w_linep == lp2)
500: wp->w_linep = lp1;
501: if (wp->w_dotp == lp2) {
1.3 millert 502: wp->w_dotp = lp1;
1.1 deraadt 503: wp->w_doto += lp1->l_used;
504: }
505: if (wp->w_markp == lp2) {
1.3 millert 506: wp->w_markp = lp1;
1.1 deraadt 507: wp->w_marko += lp1->l_used;
508: }
509: }
510: lp1->l_used += lp2->l_used;
511: lp1->l_fp = lp2->l_fp;
512: lp2->l_fp->l_bp = lp1;
1.4 millert 513: free((char *)lp2);
1.22 db 514: return (TRUE);
1.1 deraadt 515: }
1.3 millert 516: if ((lp3 = lalloc(lp1->l_used + lp2->l_used)) == NULL)
1.22 db 517: return (FALSE);
1.1 deraadt 518: bcopy(&lp1->l_text[0], &lp3->l_text[0], lp1->l_used);
519: bcopy(&lp2->l_text[0], &lp3->l_text[lp1->l_used], lp2->l_used);
520: lp1->l_bp->l_fp = lp3;
521: lp3->l_fp = lp2->l_fp;
522: lp2->l_fp->l_bp = lp3;
523: lp3->l_bp = lp1->l_bp;
1.3 millert 524: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
525: if (wp->w_linep == lp1 || wp->w_linep == lp2)
1.1 deraadt 526: wp->w_linep = lp3;
527: if (wp->w_dotp == lp1)
1.3 millert 528: wp->w_dotp = lp3;
1.1 deraadt 529: else if (wp->w_dotp == lp2) {
1.3 millert 530: wp->w_dotp = lp3;
1.1 deraadt 531: wp->w_doto += lp1->l_used;
532: }
533: if (wp->w_markp == lp1)
1.3 millert 534: wp->w_markp = lp3;
1.1 deraadt 535: else if (wp->w_markp == lp2) {
1.3 millert 536: wp->w_markp = lp3;
1.1 deraadt 537: wp->w_marko += lp1->l_used;
538: }
539: }
1.4 millert 540: free((char *)lp1);
541: free((char *)lp2);
1.22 db 542: return (TRUE);
1.1 deraadt 543: }
1.9 vincent 544:
1.1 deraadt 545: /*
1.8 mickey 546: * Replace plen characters before dot with argument string. Control-J
547: * characters in st are interpreted as newlines. There is a casehack
548: * disable flag (normally it likes to match case of replacement to what
1.4 millert 549: * was there).
1.1 deraadt 550: */
1.4 millert 551: int
1.27 kjell 552: lreplace(RSIZE plen, char *st)
1.4 millert 553: {
554: RSIZE rlen; /* replacement length */
1.15 vincent 555:
556: if (curbp->b_flag & BFREADONLY) {
557: ewprintf("Buffer is read only");
1.22 db 558: return (FALSE);
1.15 vincent 559: }
1.24 kjell 560: undo_add_boundary();
561: undo_no_boundary(TRUE);
1.25 deraadt 562:
1.6 art 563: (void)backchar(FFARG | FFRAND, (int)plen);
1.24 kjell 564: (void)ldelete(plen, KNONE);
1.15 vincent 565:
1.1 deraadt 566: rlen = strlen(st);
1.24 kjell 567: region_put_data(st, rlen);
568: lchange(WFHARD);
1.1 deraadt 569:
1.24 kjell 570: undo_no_boundary(FALSE);
571: undo_add_boundary();
1.29 kjell 572: return (TRUE);
1.1 deraadt 573: }