xref: /openbsd-src/usr.bin/tmux/grid.c (revision 9b9d2a55a62c8e82206c25f94fcc7f4e2765250e)
1 /* $OpenBSD: grid.c,v 1.44 2015/08/24 22:49:13 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "tmux.h"
25 
26 /*
27  * Grid data. This is the basic data structure that represents what is shown on
28  * screen.
29  *
30  * A grid is a grid of cells (struct grid_cell). Lines are not allocated until
31  * cells in that line are written to. The grid is split into history and
32  * viewable data with the history starting at row (line) 0 and extending to
33  * (hsize - 1); from hsize to hsize + (sy - 1) is the viewable data. All
34  * functions in this file work on absolute coordinates, grid-view.c has
35  * functions which work on the screen data.
36  */
37 
38 /* Default grid cell data. */
39 const struct grid_cell grid_default_cell = { 0, 0, 8, 8, (1 << 4) | 1, " " };
40 
41 #define grid_put_cell(gd, px, py, gc) do {			\
42 	memcpy(&gd->linedata[py].celldata[px], 			\
43 	    gc, sizeof gd->linedata[py].celldata[px]);		\
44 } while (0)
45 #define grid_put_utf8(gd, px, py, gc) do {			\
46 	memcpy(&gd->linedata[py].utf8data[px], 			\
47 	    gc, sizeof gd->linedata[py].utf8data[px]);		\
48 } while (0)
49 
50 int	grid_check_y(struct grid *, u_int);
51 
52 void	grid_reflow_join(struct grid *, u_int *, struct grid_line *, u_int);
53 void	grid_reflow_split(struct grid *, u_int *, struct grid_line *, u_int,
54 	    u_int);
55 void	grid_reflow_move(struct grid *, u_int *, struct grid_line *);
56 size_t	grid_string_cells_fg(const struct grid_cell *, int *);
57 size_t	grid_string_cells_bg(const struct grid_cell *, int *);
58 void	grid_string_cells_code(const struct grid_cell *,
59 	    const struct grid_cell *, char *, size_t, int);
60 
61 /* Check grid y position. */
62 int
63 grid_check_y(struct grid *gd, u_int py)
64 {
65 	if ((py) >= (gd)->hsize + (gd)->sy) {
66 		log_debug("y out of range: %u", py);
67 		return (-1);
68 	}
69 	return (0);
70 }
71 
72 /* Create a new grid. */
73 struct grid *
74 grid_create(u_int sx, u_int sy, u_int hlimit)
75 {
76 	struct grid	*gd;
77 
78 	gd = xmalloc(sizeof *gd);
79 	gd->sx = sx;
80 	gd->sy = sy;
81 
82 	gd->flags = GRID_HISTORY;
83 
84 	gd->hsize = 0;
85 	gd->hlimit = hlimit;
86 
87 	gd->linedata = xcalloc(gd->sy, sizeof *gd->linedata);
88 
89 	return (gd);
90 }
91 
92 /* Destroy grid. */
93 void
94 grid_destroy(struct grid *gd)
95 {
96 	struct grid_line	*gl;
97 	u_int			 yy;
98 
99 	for (yy = 0; yy < gd->hsize + gd->sy; yy++) {
100 		gl = &gd->linedata[yy];
101 		free(gl->celldata);
102 	}
103 
104 	free(gd->linedata);
105 
106 	free(gd);
107 }
108 
109 /* Compare grids. */
110 int
111 grid_compare(struct grid *ga, struct grid *gb)
112 {
113 	struct grid_line	*gla, *glb;
114 	struct grid_cell	*gca, *gcb;
115 	u_int			 xx, yy;
116 
117 	if (ga->sx != gb->sx || ga->sy != gb->sy)
118 		return (1);
119 
120 	for (yy = 0; yy < ga->sy; yy++) {
121 		gla = &ga->linedata[yy];
122 		glb = &gb->linedata[yy];
123 		if (gla->cellsize != glb->cellsize)
124 			return (1);
125 		for (xx = 0; xx < ga->sx; xx++) {
126 			gca = &gla->celldata[xx];
127 			gcb = &glb->celldata[xx];
128 			if (memcmp(gca, gcb, sizeof (struct grid_cell)) != 0)
129 				return (1);
130 		}
131 	}
132 
133 	return (0);
134 }
135 
136 /*
137  * Collect lines from the history if at the limit. Free the top (oldest) 10%
138  * and shift up.
139  */
140 void
141 grid_collect_history(struct grid *gd)
142 {
143 	u_int	yy;
144 
145 	if (gd->hsize < gd->hlimit)
146 		return;
147 
148 	yy = gd->hlimit / 10;
149 	if (yy < 1)
150 		yy = 1;
151 
152 	grid_move_lines(gd, 0, yy, gd->hsize + gd->sy - yy);
153 	gd->hsize -= yy;
154 }
155 
156 /*
157  * Scroll the entire visible screen, moving one line into the history. Just
158  * allocate a new line at the bottom and move the history size indicator.
159  */
160 void
161 grid_scroll_history(struct grid *gd)
162 {
163 	u_int	yy;
164 
165 	yy = gd->hsize + gd->sy;
166 	gd->linedata = xreallocarray(gd->linedata, yy + 1,
167 	    sizeof *gd->linedata);
168 	memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
169 
170 	gd->hsize++;
171 }
172 
173 /* Scroll a region up, moving the top line into the history. */
174 void
175 grid_scroll_history_region(struct grid *gd, u_int upper, u_int lower)
176 {
177 	struct grid_line	*gl_history, *gl_upper, *gl_lower;
178 	u_int			 yy;
179 
180 	/* Create a space for a new line. */
181 	yy = gd->hsize + gd->sy;
182 	gd->linedata = xreallocarray(gd->linedata, yy + 1,
183 	    sizeof *gd->linedata);
184 
185 	/* Move the entire screen down to free a space for this line. */
186 	gl_history = &gd->linedata[gd->hsize];
187 	memmove(gl_history + 1, gl_history, gd->sy * sizeof *gl_history);
188 
189 	/* Adjust the region and find its start and end. */
190 	upper++;
191 	gl_upper = &gd->linedata[upper];
192 	lower++;
193 	gl_lower = &gd->linedata[lower];
194 
195 	/* Move the line into the history. */
196 	memcpy(gl_history, gl_upper, sizeof *gl_history);
197 
198 	/* Then move the region up and clear the bottom line. */
199 	memmove(gl_upper, gl_upper + 1, (lower - upper) * sizeof *gl_upper);
200 	memset(gl_lower, 0, sizeof *gl_lower);
201 
202 	/* Move the history offset down over the line. */
203 	gd->hsize++;
204 }
205 
206 /* Expand line to fit to cell. */
207 void
208 grid_expand_line(struct grid *gd, u_int py, u_int sx)
209 {
210 	struct grid_line	*gl;
211 	u_int			 xx;
212 
213 	gl = &gd->linedata[py];
214 	if (sx <= gl->cellsize)
215 		return;
216 
217 	gl->celldata = xreallocarray(gl->celldata, sx, sizeof *gl->celldata);
218 	for (xx = gl->cellsize; xx < sx; xx++)
219 		grid_put_cell(gd, xx, py, &grid_default_cell);
220 	gl->cellsize = sx;
221 }
222 
223 /* Peek at grid line. */
224 const struct grid_line *
225 grid_peek_line(struct grid *gd, u_int py)
226 {
227 	if (grid_check_y(gd, py) != 0)
228 		return (NULL);
229 	return (&gd->linedata[py]);
230 }
231 
232 /* Get cell for reading. */
233 const struct grid_cell *
234 grid_peek_cell(struct grid *gd, u_int px, u_int py)
235 {
236 	if (grid_check_y(gd, py) != 0)
237 		return (&grid_default_cell);
238 
239 	if (px >= gd->linedata[py].cellsize)
240 		return (&grid_default_cell);
241 	return (&gd->linedata[py].celldata[px]);
242 }
243 
244 /* Get cell at relative position (for writing). */
245 struct grid_cell *
246 grid_get_cell(struct grid *gd, u_int px, u_int py)
247 {
248 	if (grid_check_y(gd, py) != 0)
249 		return (NULL);
250 
251 	grid_expand_line(gd, py, px + 1);
252 	return (&gd->linedata[py].celldata[px]);
253 }
254 
255 /* Set cell at relative position. */
256 void
257 grid_set_cell(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
258 {
259 	if (grid_check_y(gd, py) != 0)
260 		return;
261 
262 	grid_expand_line(gd, py, px + 1);
263 	grid_put_cell(gd, px, py, gc);
264 }
265 
266 /* Clear area. */
267 void
268 grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny)
269 {
270 	u_int	xx, yy;
271 
272 	if (nx == 0 || ny == 0)
273 		return;
274 
275 	if (px == 0 && nx == gd->sx) {
276 		grid_clear_lines(gd, py, ny);
277 		return;
278 	}
279 
280 	if (grid_check_y(gd, py) != 0)
281 		return;
282 	if (grid_check_y(gd, py + ny - 1) != 0)
283 		return;
284 
285 	for (yy = py; yy < py + ny; yy++) {
286 		if (px >= gd->linedata[yy].cellsize)
287 			continue;
288 		if (px + nx >= gd->linedata[yy].cellsize) {
289 			gd->linedata[yy].cellsize = px;
290 			continue;
291 		}
292 		for (xx = px; xx < px + nx; xx++) {
293 			if (xx >= gd->linedata[yy].cellsize)
294 				break;
295 			grid_put_cell(gd, xx, yy, &grid_default_cell);
296 		}
297 	}
298 }
299 
300 /* Clear lines. This just frees and truncates the lines. */
301 void
302 grid_clear_lines(struct grid *gd, u_int py, u_int ny)
303 {
304 	struct grid_line	*gl;
305 	u_int			 yy;
306 
307 	if (ny == 0)
308 		return;
309 
310 	if (grid_check_y(gd, py) != 0)
311 		return;
312 	if (grid_check_y(gd, py + ny - 1) != 0)
313 		return;
314 
315 	for (yy = py; yy < py + ny; yy++) {
316 		gl = &gd->linedata[yy];
317 		free(gl->celldata);
318 		memset(gl, 0, sizeof *gl);
319 	}
320 }
321 
322 /* Move a group of lines. */
323 void
324 grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny)
325 {
326 	u_int	yy;
327 
328 	if (ny == 0 || py == dy)
329 		return;
330 
331 	if (grid_check_y(gd, py) != 0)
332 		return;
333 	if (grid_check_y(gd, py + ny - 1) != 0)
334 		return;
335 	if (grid_check_y(gd, dy) != 0)
336 		return;
337 	if (grid_check_y(gd, dy + ny - 1) != 0)
338 		return;
339 
340 	/* Free any lines which are being replaced. */
341 	for (yy = dy; yy < dy + ny; yy++) {
342 		if (yy >= py && yy < py + ny)
343 			continue;
344 		grid_clear_lines(gd, yy, 1);
345 	}
346 
347 	memmove(
348 	    &gd->linedata[dy], &gd->linedata[py], ny * (sizeof *gd->linedata));
349 
350 	/* Wipe any lines that have been moved (without freeing them). */
351 	for (yy = py; yy < py + ny; yy++) {
352 		if (yy >= dy && yy < dy + ny)
353 			continue;
354 		memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
355 	}
356 }
357 
358 /* Move a group of cells. */
359 void
360 grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx)
361 {
362 	struct grid_line	*gl;
363 	u_int			 xx;
364 
365 	if (nx == 0 || px == dx)
366 		return;
367 
368 	if (grid_check_y(gd, py) != 0)
369 		return;
370 	gl = &gd->linedata[py];
371 
372 	grid_expand_line(gd, py, px + nx);
373 	grid_expand_line(gd, py, dx + nx);
374 	memmove(
375 	    &gl->celldata[dx], &gl->celldata[px], nx * sizeof *gl->celldata);
376 
377 	/* Wipe any cells that have been moved. */
378 	for (xx = px; xx < px + nx; xx++) {
379 		if (xx >= dx && xx < dx + nx)
380 			continue;
381 		grid_put_cell(gd, xx, py, &grid_default_cell);
382 	}
383 }
384 
385 /* Get ANSI foreground sequence. */
386 size_t
387 grid_string_cells_fg(const struct grid_cell *gc, int *values)
388 {
389 	size_t	n;
390 
391 	n = 0;
392 	if (gc->flags & GRID_FLAG_FG256) {
393 		values[n++] = 38;
394 		values[n++] = 5;
395 		values[n++] = gc->fg;
396 	} else {
397 		switch (gc->fg) {
398 			case 0:
399 			case 1:
400 			case 2:
401 			case 3:
402 			case 4:
403 			case 5:
404 			case 6:
405 			case 7:
406 				values[n++] = gc->fg + 30;
407 				break;
408 			case 8:
409 				values[n++] = 39;
410 				break;
411 			case 90:
412 			case 91:
413 			case 92:
414 			case 93:
415 			case 94:
416 			case 95:
417 			case 96:
418 			case 97:
419 				values[n++] = gc->fg;
420 				break;
421 		}
422 	}
423 	return (n);
424 }
425 
426 /* Get ANSI background sequence. */
427 size_t
428 grid_string_cells_bg(const struct grid_cell *gc, int *values)
429 {
430 	size_t	n;
431 
432 	n = 0;
433 	if (gc->flags & GRID_FLAG_BG256) {
434 		values[n++] = 48;
435 		values[n++] = 5;
436 		values[n++] = gc->bg;
437 	} else {
438 		switch (gc->bg) {
439 		case 0:
440 		case 1:
441 		case 2:
442 		case 3:
443 		case 4:
444 		case 5:
445 		case 6:
446 		case 7:
447 			values[n++] = gc->bg + 40;
448 			break;
449 		case 8:
450 			values[n++] = 49;
451 			break;
452 		case 100:
453 		case 101:
454 		case 102:
455 		case 103:
456 		case 104:
457 			case 105:
458 		case 106:
459 		case 107:
460 			values[n++] = gc->bg - 10;
461 			break;
462 		}
463 	}
464 	return (n);
465 }
466 
467 /*
468  * Returns ANSI code to set particular attributes (colour, bold and so on)
469  * given a current state. The output buffer must be able to hold at least 57
470  * bytes.
471  */
472 void
473 grid_string_cells_code(const struct grid_cell *lastgc,
474     const struct grid_cell *gc, char *buf, size_t len, int escape_c0)
475 {
476 	int	oldc[16], newc[16], s[32];
477 	size_t	noldc, nnewc, n, i;
478 	u_int	attr = gc->attr;
479 	u_int	lastattr = lastgc->attr;
480 	char	tmp[64];
481 
482 	struct {
483 		u_int	mask;
484 		u_int	code;
485 	} attrs[] = {
486 		{ GRID_ATTR_BRIGHT, 1 },
487 		{ GRID_ATTR_DIM, 2 },
488 		{ GRID_ATTR_ITALICS, 3 },
489 		{ GRID_ATTR_UNDERSCORE, 4 },
490 		{ GRID_ATTR_BLINK, 5 },
491 		{ GRID_ATTR_REVERSE, 7 },
492 		{ GRID_ATTR_HIDDEN, 8 }
493 	};
494 	n = 0;
495 
496 	/* If any attribute is removed, begin with 0. */
497 	for (i = 0; i < nitems(attrs); i++) {
498 		if (!(attr & attrs[i].mask) && (lastattr & attrs[i].mask)) {
499 			s[n++] = 0;
500 			lastattr &= GRID_ATTR_CHARSET;
501 			break;
502 		}
503 	}
504 	/* For each attribute that is newly set, add its code. */
505 	for (i = 0; i < nitems(attrs); i++) {
506 		if ((attr & attrs[i].mask) && !(lastattr & attrs[i].mask))
507 			s[n++] = attrs[i].code;
508 	}
509 
510 	/* If the foreground colour changed, append its parameters. */
511 	nnewc = grid_string_cells_fg(gc, newc);
512 	noldc = grid_string_cells_fg(lastgc, oldc);
513 	if (nnewc != noldc || memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0) {
514 		for (i = 0; i < nnewc; i++)
515 			s[n++] = newc[i];
516 	}
517 
518 	/* If the background colour changed, append its parameters. */
519 	nnewc = grid_string_cells_bg(gc, newc);
520 	noldc = grid_string_cells_bg(lastgc, oldc);
521 	if (nnewc != noldc || memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0) {
522 		for (i = 0; i < nnewc; i++)
523 			s[n++] = newc[i];
524 	}
525 
526 	/* If there are any parameters, append an SGR code. */
527 	*buf = '\0';
528 	if (n > 0) {
529 		if (escape_c0)
530 			strlcat(buf, "\\033[", len);
531 		else
532 			strlcat(buf, "\033[", len);
533 		for (i = 0; i < n; i++) {
534 			if (i + 1 < n)
535 				xsnprintf(tmp, sizeof tmp, "%d;", s[i]);
536 			else
537 				xsnprintf(tmp, sizeof tmp, "%d", s[i]);
538 			strlcat(buf, tmp, len);
539 		}
540 		strlcat(buf, "m", len);
541 	}
542 
543 	/* Append shift in/shift out if needed. */
544 	if ((attr & GRID_ATTR_CHARSET) && !(lastattr & GRID_ATTR_CHARSET)) {
545 		if (escape_c0)
546 			strlcat(buf, "\\016", len);  /* SO */
547 		else
548 			strlcat(buf, "\016", len);  /* SO */
549 	}
550 	if (!(attr & GRID_ATTR_CHARSET) && (lastattr & GRID_ATTR_CHARSET)) {
551 		if (escape_c0)
552 			strlcat(buf, "\\017", len);  /* SI */
553 		else
554 			strlcat(buf, "\017", len);  /* SI */
555 	}
556 }
557 
558 /* Convert cells into a string. */
559 char *
560 grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx,
561     struct grid_cell **lastgc, int with_codes, int escape_c0, int trim)
562 {
563 	const struct grid_cell	*gc;
564 	static struct grid_cell	 lastgc1;
565 	struct utf8_data	 ud;
566 	const char		*data;
567 	char			*buf, code[128];
568 	size_t			 len, off, size, codelen;
569 	u_int			 xx;
570 	const struct grid_line	*gl;
571 
572 	if (lastgc != NULL && *lastgc == NULL) {
573 		memcpy(&lastgc1, &grid_default_cell, sizeof lastgc1);
574 		*lastgc = &lastgc1;
575 	}
576 
577 	len = 128;
578 	buf = xmalloc(len);
579 	off = 0;
580 
581 	gl = grid_peek_line(gd, py);
582 	for (xx = px; xx < px + nx; xx++) {
583 		if (gl == NULL || xx >= gl->cellsize)
584 			break;
585 		gc = &gl->celldata[xx];
586 		if (gc->flags & GRID_FLAG_PADDING)
587 			continue;
588 		grid_cell_get(gc, &ud);
589 
590 		if (with_codes) {
591 			grid_string_cells_code(*lastgc, gc, code, sizeof code,
592 			    escape_c0);
593 			codelen = strlen(code);
594 			memcpy(*lastgc, gc, sizeof *gc);
595 		} else
596 			codelen = 0;
597 
598 		data = ud.data;
599 		size = ud.size;
600 		if (escape_c0 && size == 1 && *data == '\\') {
601 			data = "\\\\";
602 			size = 2;
603 		}
604 
605 		while (len < off + size + codelen + 1) {
606 			buf = xreallocarray(buf, 2, len);
607 			len *= 2;
608 		}
609 
610 		if (codelen != 0) {
611 			memcpy(buf + off, code, codelen);
612 			off += codelen;
613 		}
614 		memcpy(buf + off, data, size);
615 		off += size;
616 	}
617 
618 	if (trim) {
619 		while (off > 0 && buf[off - 1] == ' ')
620 			off--;
621 	}
622 	buf[off] = '\0';
623 
624 	return (buf);
625 }
626 
627 /*
628  * Duplicate a set of lines between two grids. If there aren't enough lines in
629  * either source or destination, the number of lines is limited to the number
630  * available.
631  */
632 void
633 grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy,
634     u_int ny)
635 {
636 	struct grid_line	*dstl, *srcl;
637 	u_int			 yy;
638 
639 	if (dy + ny > dst->hsize + dst->sy)
640 		ny = dst->hsize + dst->sy - dy;
641 	if (sy + ny > src->hsize + src->sy)
642 		ny = src->hsize + src->sy - sy;
643 	grid_clear_lines(dst, dy, ny);
644 
645 	for (yy = 0; yy < ny; yy++) {
646 		srcl = &src->linedata[sy];
647 		dstl = &dst->linedata[dy];
648 
649 		memcpy(dstl, srcl, sizeof *dstl);
650 		if (srcl->cellsize != 0) {
651 			dstl->celldata = xreallocarray(NULL,
652 			    srcl->cellsize, sizeof *dstl->celldata);
653 			memcpy(dstl->celldata, srcl->celldata,
654 			    srcl->cellsize * sizeof *dstl->celldata);
655 		} else
656 			dstl->celldata = NULL;
657 
658 		sy++;
659 		dy++;
660 	}
661 }
662 
663 /* Join line data. */
664 void
665 grid_reflow_join(struct grid *dst, u_int *py, struct grid_line *src_gl,
666     u_int new_x)
667 {
668 	struct grid_line	*dst_gl = &dst->linedata[(*py) - 1];
669 	u_int			 left, to_copy, ox, nx;
670 
671 	/* How much is left on the old line? */
672 	left = new_x - dst_gl->cellsize;
673 
674 	/* Work out how much to append. */
675 	to_copy = src_gl->cellsize;
676 	if (to_copy > left)
677 		to_copy = left;
678 	ox = dst_gl->cellsize;
679 	nx = ox + to_copy;
680 
681 	/* Resize the destination line. */
682 	dst_gl->celldata = xreallocarray(dst_gl->celldata, nx,
683 	    sizeof *dst_gl->celldata);
684 	dst_gl->cellsize = nx;
685 
686 	/* Append as much as possible. */
687 	memcpy(&dst_gl->celldata[ox], &src_gl->celldata[0],
688 	    to_copy * sizeof src_gl->celldata[0]);
689 
690 	/* If there is any left in the source, split it. */
691 	if (src_gl->cellsize > to_copy) {
692 		dst_gl->flags |= GRID_LINE_WRAPPED;
693 
694 		src_gl->cellsize -= to_copy;
695 		grid_reflow_split(dst, py, src_gl, new_x, to_copy);
696 	}
697 }
698 
699 /* Split line data. */
700 void
701 grid_reflow_split(struct grid *dst, u_int *py, struct grid_line *src_gl,
702     u_int new_x, u_int offset)
703 {
704 	struct grid_line	*dst_gl = NULL;
705 	u_int			 to_copy;
706 
707 	/* Loop and copy sections of the source line. */
708 	while (src_gl->cellsize > 0) {
709 		/* Create new line. */
710 		if (*py >= dst->hsize + dst->sy)
711 			grid_scroll_history(dst);
712 		dst_gl = &dst->linedata[*py];
713 		(*py)++;
714 
715 		/* How much should we copy? */
716 		to_copy = new_x;
717 		if (to_copy > src_gl->cellsize)
718 			to_copy = src_gl->cellsize;
719 
720 		/* Expand destination line. */
721 		dst_gl->celldata = xreallocarray(NULL, to_copy,
722 		    sizeof *dst_gl->celldata);
723 		dst_gl->cellsize = to_copy;
724 		dst_gl->flags |= GRID_LINE_WRAPPED;
725 
726 		/* Copy the data. */
727 		memcpy(&dst_gl->celldata[0], &src_gl->celldata[offset],
728 		    to_copy * sizeof dst_gl->celldata[0]);
729 
730 		/* Move offset and reduce old line size. */
731 		offset += to_copy;
732 		src_gl->cellsize -= to_copy;
733 	}
734 
735 	/* Last line is not wrapped. */
736 	if (dst_gl != NULL)
737 		dst_gl->flags &= ~GRID_LINE_WRAPPED;
738 }
739 
740 /* Move line data. */
741 void
742 grid_reflow_move(struct grid *dst, u_int *py, struct grid_line *src_gl)
743 {
744 	struct grid_line	*dst_gl;
745 
746 	/* Create new line. */
747 	if (*py >= dst->hsize + dst->sy)
748 		grid_scroll_history(dst);
749 	dst_gl = &dst->linedata[*py];
750 	(*py)++;
751 
752 	/* Copy the old line. */
753 	memcpy(dst_gl, src_gl, sizeof *dst_gl);
754 	dst_gl->flags &= ~GRID_LINE_WRAPPED;
755 
756 	/* Clear old line. */
757 	src_gl->celldata = NULL;
758 }
759 
760 /*
761  * Reflow lines from src grid into dst grid of width new_x. Returns number of
762  * lines fewer in the visible area. The source grid is destroyed.
763  */
764 u_int
765 grid_reflow(struct grid *dst, struct grid *src, u_int new_x)
766 {
767 	u_int			 py, sy, line;
768 	int			 previous_wrapped;
769 	struct grid_line	*src_gl;
770 
771 	py = 0;
772 	sy = src->sy;
773 
774 	previous_wrapped = 0;
775 	for (line = 0; line < sy + src->hsize; line++) {
776 		src_gl = src->linedata + line;
777 		if (!previous_wrapped) {
778 			/* Wasn't wrapped. If smaller, move to destination. */
779 			if (src_gl->cellsize <= new_x)
780 				grid_reflow_move(dst, &py, src_gl);
781 			else
782 				grid_reflow_split(dst, &py, src_gl, new_x, 0);
783 		} else {
784 			/* Previous was wrapped. Try to join. */
785 			grid_reflow_join(dst, &py, src_gl, new_x);
786 		}
787 		previous_wrapped = src_gl->flags & GRID_LINE_WRAPPED;
788 	}
789 
790 	grid_destroy(src);
791 
792 	if (py > sy)
793 		return (0);
794 	return (sy - py);
795 }
796