Use format_get_command() and some spacing tweaks.
[tmux-openbsd.git] / grid.c
blobd06e71542b2bf42d9662bef77dbc0e3665985379
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "tmux.h"
27 * Grid data. This is the basic data structure that represents what is shown on
28 * screen.
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.
38 /* Default grid cell data. */
39 const struct grid_cell grid_default_cell = { 0, 0, 8, 8, (1 << 4) | 1, " " };
40 const struct grid_cell grid_marker_cell = { 0, 0, 8, 8, (1 << 4) | 1, "_" };
42 #define grid_put_cell(gd, px, py, gc) do { \
43 memcpy(&gd->linedata[py].celldata[px], \
44 gc, sizeof gd->linedata[py].celldata[px]); \
45 } while (0)
46 #define grid_put_utf8(gd, px, py, gc) do { \
47 memcpy(&gd->linedata[py].utf8data[px], \
48 gc, sizeof gd->linedata[py].utf8data[px]); \
49 } while (0)
51 int grid_check_y(struct grid *, u_int);
53 #ifdef DEBUG
54 int
55 grid_check_y(struct grid *gd, u_int py)
57 if ((py) >= (gd)->hsize + (gd)->sy)
58 log_fatalx("y out of range: %u", py);
59 return (0);
61 #else
62 int
63 grid_check_y(struct grid *gd, u_int py)
65 if ((py) >= (gd)->hsize + (gd)->sy) {
66 log_debug("y out of range: %u", py);
67 return (-1);
69 return (0);
71 #endif
73 void grid_reflow_join(struct grid *, u_int *, struct grid_line *, u_int);
74 void grid_reflow_split(struct grid *, u_int *, struct grid_line *, u_int,
75 u_int);
76 void grid_reflow_move(struct grid *, u_int *, struct grid_line *);
77 size_t grid_string_cells_fg(const struct grid_cell *, int *);
78 size_t grid_string_cells_bg(const struct grid_cell *, int *);
79 void grid_string_cells_code(const struct grid_cell *,
80 const struct grid_cell *, char *, size_t, int);
82 /* Create a new grid. */
83 struct grid *
84 grid_create(u_int sx, u_int sy, u_int hlimit)
86 struct grid *gd;
88 gd = xmalloc(sizeof *gd);
89 gd->sx = sx;
90 gd->sy = sy;
92 gd->flags = GRID_HISTORY;
94 gd->hsize = 0;
95 gd->hlimit = hlimit;
97 gd->linedata = xcalloc(gd->sy, sizeof *gd->linedata);
99 return (gd);
102 /* Destroy grid. */
103 void
104 grid_destroy(struct grid *gd)
106 struct grid_line *gl;
107 u_int yy;
109 for (yy = 0; yy < gd->hsize + gd->sy; yy++) {
110 gl = &gd->linedata[yy];
111 free(gl->celldata);
114 free(gd->linedata);
116 free(gd);
119 /* Compare grids. */
121 grid_compare(struct grid *ga, struct grid *gb)
123 struct grid_line *gla, *glb;
124 struct grid_cell *gca, *gcb;
125 u_int xx, yy;
127 if (ga->sx != gb->sx || ga->sy != ga->sy)
128 return (1);
130 for (yy = 0; yy < ga->sy; yy++) {
131 gla = &ga->linedata[yy];
132 glb = &gb->linedata[yy];
133 if (gla->cellsize != glb->cellsize)
134 return (1);
135 for (xx = 0; xx < ga->sx; xx++) {
136 gca = &gla->celldata[xx];
137 gcb = &glb->celldata[xx];
138 if (memcmp(gca, gcb, sizeof (struct grid_cell)) != 0)
139 return (1);
143 return (0);
147 * Collect lines from the history if at the limit. Free the top (oldest) 10%
148 * and shift up.
150 void
151 grid_collect_history(struct grid *gd)
153 u_int yy;
155 GRID_DEBUG(gd, "");
157 if (gd->hsize < gd->hlimit)
158 return;
160 yy = gd->hlimit / 10;
161 if (yy < 1)
162 yy = 1;
164 grid_move_lines(gd, 0, yy, gd->hsize + gd->sy - yy);
165 gd->hsize -= yy;
169 * Scroll the entire visible screen, moving one line into the history. Just
170 * allocate a new line at the bottom and move the history size indicator.
172 void
173 grid_scroll_history(struct grid *gd)
175 u_int yy;
177 GRID_DEBUG(gd, "");
179 yy = gd->hsize + gd->sy;
180 gd->linedata = xrealloc(gd->linedata, yy + 1, sizeof *gd->linedata);
181 memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
183 gd->hsize++;
186 /* Scroll a region up, moving the top line into the history. */
187 void
188 grid_scroll_history_region(struct grid *gd, u_int upper, u_int lower)
190 struct grid_line *gl_history, *gl_upper, *gl_lower;
191 u_int yy;
193 GRID_DEBUG(gd, "upper=%u, lower=%u", upper, lower);
195 /* Create a space for a new line. */
196 yy = gd->hsize + gd->sy;
197 gd->linedata = xrealloc(gd->linedata, yy + 1, sizeof *gd->linedata);
199 /* Move the entire screen down to free a space for this line. */
200 gl_history = &gd->linedata[gd->hsize];
201 memmove(gl_history + 1, gl_history, gd->sy * sizeof *gl_history);
203 /* Adjust the region and find its start and end. */
204 upper++;
205 gl_upper = &gd->linedata[upper];
206 lower++;
207 gl_lower = &gd->linedata[lower];
209 /* Move the line into the history. */
210 memcpy(gl_history, gl_upper, sizeof *gl_history);
212 /* Then move the region up and clear the bottom line. */
213 memmove(gl_upper, gl_upper + 1, (lower - upper) * sizeof *gl_upper);
214 memset(gl_lower, 0, sizeof *gl_lower);
216 /* Move the history offset down over the line. */
217 gd->hsize++;
220 /* Expand line to fit to cell. */
221 void
222 grid_expand_line(struct grid *gd, u_int py, u_int sx)
224 struct grid_line *gl;
225 u_int xx;
227 gl = &gd->linedata[py];
228 if (sx <= gl->cellsize)
229 return;
231 gl->celldata = xrealloc(gl->celldata, sx, sizeof *gl->celldata);
232 for (xx = gl->cellsize; xx < sx; xx++)
233 grid_put_cell(gd, xx, py, &grid_default_cell);
234 gl->cellsize = sx;
237 /* Peek at grid line. */
238 const struct grid_line *
239 grid_peek_line(struct grid *gd, u_int py)
241 if (grid_check_y(gd, py) != 0)
242 return (NULL);
243 return (&gd->linedata[py]);
246 /* Get cell for reading. */
247 const struct grid_cell *
248 grid_peek_cell(struct grid *gd, u_int px, u_int py)
250 if (grid_check_y(gd, py) != 0)
251 return (&grid_default_cell);
253 if (px >= gd->linedata[py].cellsize)
254 return (&grid_default_cell);
255 return (&gd->linedata[py].celldata[px]);
258 /* Get cell at relative position (for writing). */
259 struct grid_cell *
260 grid_get_cell(struct grid *gd, u_int px, u_int py)
262 if (grid_check_y(gd, py) != 0)
263 return (NULL);
265 grid_expand_line(gd, py, px + 1);
266 return (&gd->linedata[py].celldata[px]);
269 /* Set cell at relative position. */
270 void
271 grid_set_cell(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
273 if (grid_check_y(gd, py) != 0)
274 return;
276 grid_expand_line(gd, py, px + 1);
277 grid_put_cell(gd, px, py, gc);
280 /* Clear area. */
281 void
282 grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny)
284 u_int xx, yy;
286 GRID_DEBUG(gd, "px=%u, py=%u, nx=%u, ny=%u", px, py, nx, ny);
288 if (nx == 0 || ny == 0)
289 return;
291 if (px == 0 && nx == gd->sx) {
292 grid_clear_lines(gd, py, ny);
293 return;
296 if (grid_check_y(gd, py) != 0)
297 return;
298 if (grid_check_y(gd, py + ny - 1) != 0)
299 return;
301 for (yy = py; yy < py + ny; yy++) {
302 if (px >= gd->linedata[yy].cellsize)
303 continue;
304 if (px + nx >= gd->linedata[yy].cellsize) {
305 gd->linedata[yy].cellsize = px;
306 continue;
308 for (xx = px; xx < px + nx; xx++) {
309 if (xx >= gd->linedata[yy].cellsize)
310 break;
311 grid_put_cell(gd, xx, yy, &grid_default_cell);
316 /* Clear lines. This just frees and truncates the lines. */
317 void
318 grid_clear_lines(struct grid *gd, u_int py, u_int ny)
320 struct grid_line *gl;
321 u_int yy;
323 GRID_DEBUG(gd, "py=%u, ny=%u", py, ny);
325 if (ny == 0)
326 return;
328 if (grid_check_y(gd, py) != 0)
329 return;
330 if (grid_check_y(gd, py + ny - 1) != 0)
331 return;
333 for (yy = py; yy < py + ny; yy++) {
334 gl = &gd->linedata[yy];
335 free(gl->celldata);
336 memset(gl, 0, sizeof *gl);
340 /* Move a group of lines. */
341 void
342 grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny)
344 u_int yy;
346 GRID_DEBUG(gd, "dy=%u, py=%u, ny=%u", dy, py, ny);
348 if (ny == 0 || py == dy)
349 return;
351 if (grid_check_y(gd, py) != 0)
352 return;
353 if (grid_check_y(gd, py + ny - 1) != 0)
354 return;
355 if (grid_check_y(gd, dy) != 0)
356 return;
357 if (grid_check_y(gd, dy + ny - 1) != 0)
358 return;
360 /* Free any lines which are being replaced. */
361 for (yy = dy; yy < dy + ny; yy++) {
362 if (yy >= py && yy < py + ny)
363 continue;
364 grid_clear_lines(gd, yy, 1);
367 memmove(
368 &gd->linedata[dy], &gd->linedata[py], ny * (sizeof *gd->linedata));
370 /* Wipe any lines that have been moved (without freeing them). */
371 for (yy = py; yy < py + ny; yy++) {
372 if (yy >= dy && yy < dy + ny)
373 continue;
374 memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
378 /* Move a group of cells. */
379 void
380 grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx)
382 struct grid_line *gl;
383 u_int xx;
385 GRID_DEBUG(gd, "dx=%u, px=%u, py=%u, nx=%u", dx, px, py, nx);
387 if (nx == 0 || px == dx)
388 return;
390 if (grid_check_y(gd, py) != 0)
391 return;
392 gl = &gd->linedata[py];
394 grid_expand_line(gd, py, px + nx);
395 grid_expand_line(gd, py, dx + nx);
396 memmove(
397 &gl->celldata[dx], &gl->celldata[px], nx * sizeof *gl->celldata);
399 /* Wipe any cells that have been moved. */
400 for (xx = px; xx < px + nx; xx++) {
401 if (xx >= dx && xx < dx + nx)
402 continue;
403 grid_put_cell(gd, xx, py, &grid_default_cell);
407 /* Get ANSI foreground sequence. */
408 size_t
409 grid_string_cells_fg(const struct grid_cell *gc, int *values)
411 size_t n;
413 n = 0;
414 if (gc->flags & GRID_FLAG_FG256) {
415 values[n++] = 38;
416 values[n++] = 5;
417 values[n++] = gc->fg;
418 } else {
419 switch (gc->fg) {
420 case 0:
421 case 1:
422 case 2:
423 case 3:
424 case 4:
425 case 5:
426 case 6:
427 case 7:
428 values[n++] = gc->fg + 30;
429 break;
430 case 8:
431 values[n++] = 39;
432 break;
433 case 90:
434 case 91:
435 case 92:
436 case 93:
437 case 94:
438 case 95:
439 case 96:
440 case 97:
441 values[n++] = gc->fg;
442 break;
445 return (n);
448 /* Get ANSI background sequence. */
449 size_t
450 grid_string_cells_bg(const struct grid_cell *gc, int *values)
452 size_t n;
454 n = 0;
455 if (gc->flags & GRID_FLAG_BG256) {
456 values[n++] = 48;
457 values[n++] = 5;
458 values[n++] = gc->bg;
459 } else {
460 switch (gc->bg) {
461 case 0:
462 case 1:
463 case 2:
464 case 3:
465 case 4:
466 case 5:
467 case 6:
468 case 7:
469 values[n++] = gc->bg + 40;
470 break;
471 case 8:
472 values[n++] = 49;
473 break;
474 case 100:
475 case 101:
476 case 102:
477 case 103:
478 case 104:
479 case 105:
480 case 106:
481 case 107:
482 values[n++] = gc->bg - 10;
483 break;
486 return (n);
490 * Returns ANSI code to set particular attributes (colour, bold and so on)
491 * given a current state. The output buffer must be able to hold at least 57
492 * bytes.
494 void
495 grid_string_cells_code(const struct grid_cell *lastgc,
496 const struct grid_cell *gc, char *buf, size_t len, int escape_c0)
498 int oldc[16], newc[16], s[32];
499 size_t noldc, nnewc, n, i;
500 u_int attr = gc->attr;
501 u_int lastattr = lastgc->attr;
502 char tmp[64];
504 struct {
505 u_int mask;
506 u_int code;
507 } attrs[] = {
508 { GRID_ATTR_BRIGHT, 1 },
509 { GRID_ATTR_DIM, 2 },
510 { GRID_ATTR_ITALICS, 3 },
511 { GRID_ATTR_UNDERSCORE, 4 },
512 { GRID_ATTR_BLINK, 5 },
513 { GRID_ATTR_REVERSE, 7 },
514 { GRID_ATTR_HIDDEN, 8 }
516 n = 0;
518 /* If any attribute is removed, begin with 0. */
519 for (i = 0; i < nitems(attrs); i++) {
520 if (!(attr & attrs[i].mask) && (lastattr & attrs[i].mask)) {
521 s[n++] = 0;
522 lastattr &= GRID_ATTR_CHARSET;
523 break;
526 /* For each attribute that is newly set, add its code. */
527 for (i = 0; i < nitems(attrs); i++) {
528 if ((attr & attrs[i].mask) && !(lastattr & attrs[i].mask))
529 s[n++] = attrs[i].code;
532 /* If the foreground c changed, append its parameters. */
533 nnewc = grid_string_cells_fg(gc, newc);
534 noldc = grid_string_cells_fg(lastgc, oldc);
535 if (nnewc != noldc ||
536 memcmp(newc,oldc, nnewc * sizeof newc[0]) != 0) {
537 for (i = 0; i < nnewc; i++)
538 s[n++] = newc[i];
541 /* If the background c changed, append its parameters. */
542 nnewc = grid_string_cells_bg(gc, newc);
543 noldc = grid_string_cells_bg(lastgc, oldc);
544 if (nnewc != noldc ||
545 memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0) {
546 for (i = 0; i < nnewc; i++)
547 s[n++] = newc[i];
550 /* If there are any parameters, append an SGR code. */
551 *buf = '\0';
552 if (n > 0) {
553 if (escape_c0)
554 strlcat(buf, "\\033[", len);
555 else
556 strlcat(buf, "\033[", len);
557 for (i = 0; i < n; i++) {
558 if (i + 1 < n)
559 xsnprintf(tmp, sizeof tmp, "%d;", s[i]);
560 else
561 xsnprintf(tmp, sizeof tmp, "%d", s[i]);
562 strlcat(buf, tmp, len);
564 strlcat(buf, "m", len);
567 /* Append shift in/shift out if needed. */
568 if ((attr & GRID_ATTR_CHARSET) && !(lastattr & GRID_ATTR_CHARSET)) {
569 if (escape_c0)
570 strlcat(buf, "\\016", len); /* SO */
571 else
572 strlcat(buf, "\016", len); /* SO */
574 if (!(attr & GRID_ATTR_CHARSET) && (lastattr & GRID_ATTR_CHARSET)) {
575 if (escape_c0)
576 strlcat(buf, "\\017", len); /* SI */
577 else
578 strlcat(buf, "\017", len); /* SI */
582 /* Convert cells into a string. */
583 char *
584 grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx,
585 struct grid_cell **lastgc, int with_codes, int escape_c0, int trim)
587 const struct grid_cell *gc;
588 static struct grid_cell lastgc1;
589 struct utf8_data ud;
590 const char* data;
591 char *buf, code[128];
592 size_t len, off, size, codelen;
593 u_int xx;
594 const struct grid_line *gl;
596 GRID_DEBUG(gd, "px=%u, py=%u, nx=%u", px, py, nx);
598 if (lastgc != NULL && *lastgc == NULL) {
599 memcpy(&lastgc1, &grid_default_cell, sizeof lastgc1);
600 *lastgc = &lastgc1;
603 len = 128;
604 buf = xmalloc(len);
605 off = 0;
607 gl = grid_peek_line(gd, py);
608 for (xx = px; xx < px + nx; xx++) {
609 if (gl == NULL || xx >= gl->cellsize)
610 break;
611 gc = &gl->celldata[xx];
612 if (gc->flags & GRID_FLAG_PADDING)
613 continue;
614 grid_cell_get(gc, &ud);
616 if (with_codes) {
617 grid_string_cells_code(*lastgc, gc, code, sizeof code,
618 escape_c0);
619 codelen = strlen(code);
620 memcpy(*lastgc, gc, sizeof *gc);
621 } else
622 codelen = 0;
624 data = ud.data;
625 size = ud.size;
626 if (escape_c0 && size == 1 && *data == '\\') {
627 data = "\\\\";
628 size = 2;
631 while (len < off + size + codelen + 1) {
632 buf = xrealloc(buf, 2, len);
633 len *= 2;
636 if (codelen != 0) {
637 memcpy(buf + off, code, codelen);
638 off += codelen;
640 memcpy(buf + off, data, size);
641 off += size;
644 if (trim) {
645 while (off > 0 && buf[off - 1] == ' ')
646 off--;
648 buf[off] = '\0';
650 return (buf);
654 * Duplicate a set of lines between two grids. If there aren't enough lines in
655 * either source or destination, the number of lines is limited to the number
656 * available.
658 void
659 grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy,
660 u_int ny)
662 struct grid_line *dstl, *srcl;
663 u_int yy;
665 GRID_DEBUG(src, "dy=%u, sy=%u, ny=%u", dy, sy, ny);
667 if (dy + ny > dst->hsize + dst->sy)
668 ny = dst->hsize + dst->sy - dy;
669 if (sy + ny > src->hsize + src->sy)
670 ny = src->hsize + src->sy - sy;
671 grid_clear_lines(dst, dy, ny);
673 for (yy = 0; yy < ny; yy++) {
674 srcl = &src->linedata[sy];
675 dstl = &dst->linedata[dy];
677 memcpy(dstl, srcl, sizeof *dstl);
678 if (srcl->cellsize != 0) {
679 dstl->celldata = xcalloc(
680 srcl->cellsize, sizeof *dstl->celldata);
681 memcpy(dstl->celldata, srcl->celldata,
682 srcl->cellsize * sizeof *dstl->celldata);
685 sy++;
686 dy++;
690 /* Join line data. */
691 void
692 grid_reflow_join(struct grid *dst, u_int *py, struct grid_line *src_gl,
693 u_int new_x)
695 struct grid_line *dst_gl = &dst->linedata[(*py) - 1];
696 u_int left, to_copy, ox, nx;
698 /* How much is left on the old line? */
699 left = new_x - dst_gl->cellsize;
701 /* Work out how much to append. */
702 to_copy = src_gl->cellsize;
703 if (to_copy > left)
704 to_copy = left;
705 ox = dst_gl->cellsize;
706 nx = ox + to_copy;
708 /* Resize the destination line. */
709 dst_gl->celldata = xrealloc(dst_gl->celldata, nx,
710 sizeof *dst_gl->celldata);
711 dst_gl->cellsize = nx;
713 /* Append as much as possible. */
714 memcpy(&dst_gl->celldata[ox], &src_gl->celldata[0],
715 to_copy * sizeof src_gl->celldata[0]);
717 /* If there is any left in the source, split it. */
718 if (src_gl->cellsize > to_copy) {
719 dst_gl->flags |= GRID_LINE_WRAPPED;
721 src_gl->cellsize -= to_copy;
722 grid_reflow_split(dst, py, src_gl, new_x, to_copy);
726 /* Split line data. */
727 void
728 grid_reflow_split(struct grid *dst, u_int *py, struct grid_line *src_gl,
729 u_int new_x, u_int offset)
731 struct grid_line *dst_gl = NULL;
732 u_int to_copy;
734 /* Loop and copy sections of the source line. */
735 while (src_gl->cellsize > 0) {
736 /* Create new line. */
737 if (*py >= dst->hsize + dst->sy)
738 grid_scroll_history(dst);
739 dst_gl = &dst->linedata[*py];
740 (*py)++;
742 /* How much should we copy? */
743 to_copy = new_x;
744 if (to_copy > src_gl->cellsize)
745 to_copy = src_gl->cellsize;
747 /* Expand destination line. */
748 dst_gl->celldata = xmalloc(to_copy * sizeof *dst_gl->celldata);
749 dst_gl->cellsize = to_copy;
750 dst_gl->flags |= GRID_LINE_WRAPPED;
752 /* Copy the data. */
753 memcpy (&dst_gl->celldata[0], &src_gl->celldata[offset],
754 to_copy * sizeof dst_gl->celldata[0]);
756 /* Move offset and reduce old line size. */
757 offset += to_copy;
758 src_gl->cellsize -= to_copy;
761 /* Last line is not wrapped. */
762 if (dst_gl != NULL)
763 dst_gl->flags &= ~GRID_LINE_WRAPPED;
766 /* Move line data. */
767 void
768 grid_reflow_move(struct grid *dst, u_int *py, struct grid_line *src_gl)
770 struct grid_line *dst_gl;
772 /* Create new line. */
773 if (*py >= dst->hsize + dst->sy)
774 grid_scroll_history(dst);
775 dst_gl = &dst->linedata[*py];
776 (*py)++;
778 /* Copy the old line. */
779 memcpy(dst_gl, src_gl, sizeof *dst_gl);
780 dst_gl->flags &= ~GRID_LINE_WRAPPED;
782 /* Clear old line. */
783 src_gl->celldata = NULL;
787 * Reflow lines from src grid into dst grid of width new_x. Returns number of
788 * lines fewer in the visible area. The source grid is destroyed.
790 u_int
791 grid_reflow(struct grid *dst, struct grid *src, u_int new_x)
793 u_int py, sy, line;
794 int previous_wrapped;
795 struct grid_line *src_gl;
797 py = 0;
798 sy = src->sy;
800 previous_wrapped = 0;
801 for (line = 0; line < sy + src->hsize; line++) {
802 src_gl = src->linedata + line;
803 if (!previous_wrapped) {
804 /* Wasn't wrapped. If smaller, move to destination. */
805 if (src_gl->cellsize <= new_x)
806 grid_reflow_move(dst, &py, src_gl);
807 else
808 grid_reflow_split(dst, &py, src_gl, new_x, 0);
809 } else {
810 /* Previous was wrapped. Try to join. */
811 grid_reflow_join(dst, &py, src_gl, new_x);
813 previous_wrapped = src_gl->flags & GRID_LINE_WRAPPED;
816 grid_destroy(src);
818 if (py > sy)
819 return (0);
820 return (sy - py);