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