[BACK]Return to grid.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/grid.c, Revision 1.85

1.85    ! nicm        1: /* $OpenBSD: grid.c,v 1.84 2018/07/04 09:44:07 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.50      nicm        4:  * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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:
1.20      nicm       21: #include <stdlib.h>
1.1       nicm       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. */
1.48      nicm       39: const struct grid_cell grid_default_cell = {
1.53      nicm       40:        0, 0, 8, 8, { { ' ' }, 0, 1, 1 }
1.48      nicm       41: };
1.66      nicm       42: static const struct grid_cell_entry grid_default_entry = {
1.48      nicm       43:        0, { .data = { 0, 8, 8, ' ' } }
                     44: };
1.1       nicm       45:
1.58      nicm       46: static void    grid_empty_line(struct grid *, u_int, u_int);
1.57      nicm       47:
1.63      nicm       48: /* Store cell in entry. */
                     49: static void
                     50: grid_store_cell(struct grid_cell_entry *gce, const struct grid_cell *gc,
                     51:     u_char c)
                     52: {
                     53:        gce->flags = gc->flags;
                     54:
                     55:        gce->data.fg = gc->fg & 0xff;
                     56:        if (gc->fg & COLOUR_FLAG_256)
                     57:                gce->flags |= GRID_FLAG_FG256;
                     58:
                     59:        gce->data.bg = gc->bg & 0xff;
                     60:        if (gc->bg & COLOUR_FLAG_256)
                     61:                gce->flags |= GRID_FLAG_BG256;
                     62:
                     63:        gce->data.attr = gc->attr;
                     64:        gce->data.data = c;
                     65: }
                     66:
1.65      nicm       67: /* Check if a cell should be extended. */
                     68: static int
                     69: grid_need_extended_cell(const struct grid_cell_entry *gce,
                     70:     const struct grid_cell *gc)
                     71: {
                     72:        if (gce->flags & GRID_FLAG_EXTENDED)
                     73:                return (1);
1.68      nicm       74:        if (gc->attr > 0xff)
                     75:                return (1);
1.65      nicm       76:        if (gc->data.size != 1 || gc->data.width != 1)
                     77:                return (1);
1.69      nicm       78:        if ((gc->fg & COLOUR_FLAG_RGB) || (gc->bg & COLOUR_FLAG_RGB))
1.65      nicm       79:                return (1);
                     80:        return (0);
                     81: }
                     82:
1.76      nicm       83: /* Free up unused extended cells. */
                     84: static void
                     85: grid_compact_line(struct grid_line *gl)
                     86: {
                     87:        int                      new_extdsize = 0;
                     88:        struct grid_cell        *new_extddata;
                     89:        struct grid_cell_entry  *gce;
                     90:        struct grid_cell        *gc;
                     91:        u_int                    px, idx;
                     92:
                     93:        if (gl->extdsize == 0)
                     94:                return;
                     95:
                     96:        for (px = 0; px < gl->cellsize; px++) {
                     97:                gce = &gl->celldata[px];
                     98:                if (gce->flags & GRID_FLAG_EXTENDED)
                     99:                        new_extdsize++;
                    100:        }
                    101:
                    102:        if (new_extdsize == 0) {
                    103:                free(gl->extddata);
                    104:                gl->extddata = NULL;
                    105:                gl->extdsize = 0;
                    106:                return;
                    107:        }
                    108:        new_extddata = xreallocarray(NULL, new_extdsize, sizeof *gl->extddata);
                    109:
                    110:        idx = 0;
                    111:        for (px = 0; px < gl->cellsize; px++) {
                    112:                gce = &gl->celldata[px];
                    113:                if (gce->flags & GRID_FLAG_EXTENDED) {
                    114:                        gc = &gl->extddata[gce->offset];
                    115:                        memcpy(&new_extddata[idx], gc, sizeof *gc);
                    116:                        gce->offset = idx++;
                    117:                }
                    118:        }
                    119:
                    120:        free(gl->extddata);
                    121:        gl->extddata = new_extddata;
                    122:        gl->extdsize = new_extdsize;
                    123: }
                    124:
1.59      nicm      125: /* Set cell as extended. */
                    126: static struct grid_cell *
                    127: grid_extended_cell(struct grid_line *gl, struct grid_cell_entry *gce,
                    128:     const struct grid_cell *gc)
                    129: {
                    130:        struct grid_cell        *gcp;
                    131:
                    132:        gl->flags |= GRID_LINE_EXTENDED;
                    133:
                    134:        if (~gce->flags & GRID_FLAG_EXTENDED) {
                    135:                gl->extddata = xreallocarray(gl->extddata, gl->extdsize + 1,
                    136:                    sizeof *gl->extddata);
                    137:                gce->offset = gl->extdsize++;
                    138:                gce->flags = gc->flags | GRID_FLAG_EXTENDED;
                    139:        }
                    140:        if (gce->offset >= gl->extdsize)
                    141:                fatalx("offset too big");
                    142:
                    143:        gcp = &gl->extddata[gce->offset];
                    144:        memcpy(gcp, gc, sizeof *gcp);
                    145:        return (gcp);
1.84      nicm      146: }
                    147:
1.85    ! nicm      148: /* Get line data. */
1.84      nicm      149: struct grid_line *
                    150: grid_get_line(struct grid *gd, u_int line)
                    151: {
                    152:        return (&gd->linedata[line]);
                    153: }
                    154:
1.85    ! nicm      155: /* Adjust number of lines. */
1.84      nicm      156: void
                    157: grid_adjust_lines(struct grid *gd, u_int lines)
                    158: {
                    159:        gd->linedata = xreallocarray(gd->linedata, lines, sizeof *gd->linedata);
1.59      nicm      160: }
                    161:
1.48      nicm      162: /* Copy default into a cell. */
                    163: static void
1.58      nicm      164: grid_clear_cell(struct grid *gd, u_int px, u_int py, u_int bg)
1.48      nicm      165: {
1.59      nicm      166:        struct grid_line        *gl = &gd->linedata[py];
                    167:        struct grid_cell_entry  *gce = &gl->celldata[px];
                    168:        struct grid_cell        *gc;
                    169:
1.60      nicm      170:        memcpy(gce, &grid_default_entry, sizeof *gce);
1.59      nicm      171:        if (bg & COLOUR_FLAG_RGB) {
                    172:                gc = grid_extended_cell(gl, gce, &grid_default_cell);
                    173:                gc->bg = bg;
                    174:        } else {
                    175:                if (bg & COLOUR_FLAG_256)
                    176:                        gce->flags |= GRID_FLAG_BG256;
                    177:                gce->data.bg = bg;
                    178:        }
1.48      nicm      179: }
                    180:
1.43      nicm      181: /* Check grid y position. */
1.54      nicm      182: static int
1.81      nicm      183: grid_check_y(struct grid *gd, const char* from, u_int py)
1.1       nicm      184: {
1.78      nicm      185:        if (py >= gd->hsize + gd->sy) {
1.81      nicm      186:                log_debug("%s: y out of range: %u", from, py);
1.1       nicm      187:                return (-1);
                    188:        }
                    189:        return (0);
                    190: }
1.23      nicm      191:
1.54      nicm      192: /* Compare grid cells. Return 1 if equal, 0 if not. */
                    193: int
                    194: grid_cells_equal(const struct grid_cell *gca, const struct grid_cell *gcb)
                    195: {
                    196:        if (gca->fg != gcb->fg || gca->bg != gcb->bg)
                    197:                return (0);
                    198:        if (gca->attr != gcb->attr || gca->flags != gcb->flags)
                    199:                return (0);
                    200:        if (gca->data.width != gcb->data.width)
                    201:                return (0);
                    202:        if (gca->data.size != gcb->data.size)
                    203:                return (0);
                    204:        return (memcmp(gca->data.data, gcb->data.data, gca->data.size) == 0);
                    205: }
                    206:
1.75      nicm      207: /* Free one line. */
                    208: static void
                    209: grid_free_line(struct grid *gd, u_int py)
                    210: {
                    211:        free(gd->linedata[py].celldata);
                    212:        gd->linedata[py].celldata = NULL;
                    213:        free(gd->linedata[py].extddata);
                    214:        gd->linedata[py].extddata = NULL;
                    215: }
                    216:
                    217: /* Free several lines. */
                    218: static void
                    219: grid_free_lines(struct grid *gd, u_int py, u_int ny)
                    220: {
                    221:        u_int   yy;
                    222:
                    223:        for (yy = py; yy < py + ny; yy++)
                    224:                grid_free_line(gd, yy);
                    225: }
                    226:
1.1       nicm      227: /* Create a new grid. */
                    228: struct grid *
                    229: grid_create(u_int sx, u_int sy, u_int hlimit)
                    230: {
                    231:        struct grid     *gd;
                    232:
                    233:        gd = xmalloc(sizeof *gd);
                    234:        gd->sx = sx;
                    235:        gd->sy = sy;
                    236:
1.7       nicm      237:        gd->flags = GRID_HISTORY;
                    238:
1.55      nicm      239:        gd->hscrolled = 0;
1.1       nicm      240:        gd->hsize = 0;
                    241:        gd->hlimit = hlimit;
                    242:
1.80      nicm      243:        if (gd->sy != 0)
                    244:                gd->linedata = xcalloc(gd->sy, sizeof *gd->linedata);
                    245:        else
                    246:                gd->linedata = NULL;
1.1       nicm      247:
                    248:        return (gd);
                    249: }
                    250:
                    251: /* Destroy grid. */
                    252: void
                    253: grid_destroy(struct grid *gd)
                    254: {
1.75      nicm      255:        grid_free_lines(gd, 0, gd->hsize + gd->sy);
1.1       nicm      256:
1.20      nicm      257:        free(gd->linedata);
1.1       nicm      258:
1.20      nicm      259:        free(gd);
1.1       nicm      260: }
                    261:
                    262: /* Compare grids. */
                    263: int
                    264: grid_compare(struct grid *ga, struct grid *gb)
                    265: {
1.10      nicm      266:        struct grid_line        *gla, *glb;
1.48      nicm      267:        struct grid_cell         gca, gcb;
1.1       nicm      268:        u_int                    xx, yy;
                    269:
1.33      nicm      270:        if (ga->sx != gb->sx || ga->sy != gb->sy)
1.1       nicm      271:                return (1);
                    272:
                    273:        for (yy = 0; yy < ga->sy; yy++) {
1.10      nicm      274:                gla = &ga->linedata[yy];
                    275:                glb = &gb->linedata[yy];
                    276:                if (gla->cellsize != glb->cellsize)
1.1       nicm      277:                        return (1);
1.48      nicm      278:                for (xx = 0; xx < gla->cellsize; xx++) {
                    279:                        grid_get_cell(ga, xx, yy, &gca);
                    280:                        grid_get_cell(gb, xx, yy, &gcb);
1.54      nicm      281:                        if (!grid_cells_equal(&gca, &gcb))
1.1       nicm      282:                                return (1);
                    283:                }
                    284:        }
                    285:
                    286:        return (0);
                    287: }
                    288:
1.15      nicm      289: /*
                    290:  * Collect lines from the history if at the limit. Free the top (oldest) 10%
                    291:  * and shift up.
                    292:  */
1.1       nicm      293: void
1.75      nicm      294: grid_collect_history(struct grid *gd)
1.1       nicm      295: {
1.75      nicm      296:        u_int   ny;
1.1       nicm      297:
1.77      nicm      298:        if (gd->hsize == 0 || gd->hsize < gd->hlimit)
1.15      nicm      299:                return;
                    300:
1.75      nicm      301:        ny = gd->hlimit / 10;
                    302:        if (ny < 1)
                    303:                ny = 1;
1.77      nicm      304:        if (ny > gd->hsize)
                    305:                ny = gd->hsize;
1.75      nicm      306:
                    307:        /*
                    308:         * Free the lines from 0 to ny then move the remaining lines over
                    309:         * them.
                    310:         */
                    311:        grid_free_lines(gd, 0, ny);
                    312:        memmove(&gd->linedata[0], &gd->linedata[ny],
                    313:            (gd->hsize + gd->sy - ny) * (sizeof *gd->linedata));
1.15      nicm      314:
1.75      nicm      315:        gd->hsize -= ny;
1.55      nicm      316:        if (gd->hscrolled > gd->hsize)
                    317:                gd->hscrolled = gd->hsize;
1.15      nicm      318: }
                    319:
1.17      nicm      320: /*
1.15      nicm      321:  * Scroll the entire visible screen, moving one line into the history. Just
                    322:  * allocate a new line at the bottom and move the history size indicator.
                    323:  */
                    324: void
1.58      nicm      325: grid_scroll_history(struct grid *gd, u_int bg)
1.15      nicm      326: {
                    327:        u_int   yy;
1.1       nicm      328:
                    329:        yy = gd->hsize + gd->sy;
1.41      nicm      330:        gd->linedata = xreallocarray(gd->linedata, yy + 1,
                    331:            sizeof *gd->linedata);
1.58      nicm      332:        grid_empty_line(gd, yy, bg);
1.17      nicm      333:
1.55      nicm      334:        gd->hscrolled++;
1.76      nicm      335:        grid_compact_line(&gd->linedata[gd->hsize]);
1.15      nicm      336:        gd->hsize++;
                    337: }
1.1       nicm      338:
1.46      nicm      339: /* Clear the history. */
                    340: void
                    341: grid_clear_history(struct grid *gd)
                    342: {
1.75      nicm      343:        grid_free_lines(gd, 0, gd->hsize);
                    344:        memmove(&gd->linedata[0], &gd->linedata[gd->hsize],
                    345:            gd->sy * (sizeof *gd->linedata));
1.46      nicm      346:
1.55      nicm      347:        gd->hscrolled = 0;
1.46      nicm      348:        gd->hsize = 0;
1.55      nicm      349:
1.46      nicm      350:        gd->linedata = xreallocarray(gd->linedata, gd->sy,
                    351:            sizeof *gd->linedata);
                    352: }
                    353:
1.15      nicm      354: /* Scroll a region up, moving the top line into the history. */
                    355: void
1.71      nicm      356: grid_scroll_history_region(struct grid *gd, u_int upper, u_int lower, u_int bg)
1.15      nicm      357: {
1.73      nicm      358:        struct grid_line        *gl_history, *gl_upper;
1.15      nicm      359:        u_int                    yy;
                    360:
                    361:        /* Create a space for a new line. */
                    362:        yy = gd->hsize + gd->sy;
1.41      nicm      363:        gd->linedata = xreallocarray(gd->linedata, yy + 1,
                    364:            sizeof *gd->linedata);
1.1       nicm      365:
1.15      nicm      366:        /* Move the entire screen down to free a space for this line. */
                    367:        gl_history = &gd->linedata[gd->hsize];
                    368:        memmove(gl_history + 1, gl_history, gd->sy * sizeof *gl_history);
                    369:
                    370:        /* Adjust the region and find its start and end. */
                    371:        upper++;
                    372:        gl_upper = &gd->linedata[upper];
                    373:        lower++;
                    374:
                    375:        /* Move the line into the history. */
                    376:        memcpy(gl_history, gl_upper, sizeof *gl_history);
                    377:
                    378:        /* Then move the region up and clear the bottom line. */
                    379:        memmove(gl_upper, gl_upper + 1, (lower - upper) * sizeof *gl_upper);
1.71      nicm      380:        grid_empty_line(gd, lower, bg);
1.15      nicm      381:
                    382:        /* Move the history offset down over the line. */
1.55      nicm      383:        gd->hscrolled++;
1.1       nicm      384:        gd->hsize++;
                    385: }
                    386:
                    387: /* Expand line to fit to cell. */
1.57      nicm      388: static void
1.58      nicm      389: grid_expand_line(struct grid *gd, u_int py, u_int sx, u_int bg)
1.1       nicm      390: {
1.10      nicm      391:        struct grid_line        *gl;
1.14      nicm      392:        u_int                    xx;
1.1       nicm      393:
1.10      nicm      394:        gl = &gd->linedata[py];
1.14      nicm      395:        if (sx <= gl->cellsize)
1.1       nicm      396:                return;
1.61      nicm      397:
1.62      nicm      398:        if (sx < gd->sx / 4)
                    399:                sx = gd->sx / 4;
                    400:        else if (sx < gd->sx / 2)
                    401:                sx = gd->sx / 2;
                    402:        else
                    403:                sx = gd->sx;
1.1       nicm      404:
1.41      nicm      405:        gl->celldata = xreallocarray(gl->celldata, sx, sizeof *gl->celldata);
1.10      nicm      406:        for (xx = gl->cellsize; xx < sx; xx++)
1.58      nicm      407:                grid_clear_cell(gd, xx, py, bg);
1.10      nicm      408:        gl->cellsize = sx;
1.1       nicm      409: }
                    410:
1.58      nicm      411: /* Empty a line and set background colour if needed. */
                    412: static void
                    413: grid_empty_line(struct grid *gd, u_int py, u_int bg)
                    414: {
                    415:        memset(&gd->linedata[py], 0, sizeof gd->linedata[py]);
                    416:        if (bg != 8)
                    417:                grid_expand_line(gd, py, gd->sx, bg);
                    418: }
                    419:
1.26      nicm      420: /* Peek at grid line. */
                    421: const struct grid_line *
                    422: grid_peek_line(struct grid *gd, u_int py)
                    423: {
1.81      nicm      424:        if (grid_check_y(gd, __func__, py) != 0)
1.26      nicm      425:                return (NULL);
                    426:        return (&gd->linedata[py]);
                    427: }
                    428:
1.79      nicm      429: /* Get cell from line. */
                    430: static void
                    431: grid_get_cell1(struct grid_line *gl, u_int px, struct grid_cell *gc)
1.1       nicm      432: {
1.79      nicm      433:        struct grid_cell_entry  *gce = &gl->celldata[px];
1.1       nicm      434:
1.48      nicm      435:        if (gce->flags & GRID_FLAG_EXTENDED) {
                    436:                if (gce->offset >= gl->extdsize)
                    437:                        memcpy(gc, &grid_default_cell, sizeof *gc);
                    438:                else
                    439:                        memcpy(gc, &gl->extddata[gce->offset], sizeof *gc);
                    440:                return;
                    441:        }
1.1       nicm      442:
1.53      nicm      443:        gc->flags = gce->flags & ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
1.48      nicm      444:        gc->attr = gce->data.attr;
                    445:        gc->fg = gce->data.fg;
1.53      nicm      446:        if (gce->flags & GRID_FLAG_FG256)
                    447:                gc->fg |= COLOUR_FLAG_256;
1.48      nicm      448:        gc->bg = gce->data.bg;
1.53      nicm      449:        if (gce->flags & GRID_FLAG_BG256)
                    450:                gc->bg |= COLOUR_FLAG_256;
1.48      nicm      451:        utf8_set(&gc->data, gce->data.data);
1.1       nicm      452: }
                    453:
1.79      nicm      454: /* Get cell for reading. */
                    455: void
                    456: grid_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc)
                    457: {
1.81      nicm      458:        if (grid_check_y(gd, __func__, py) != 0 ||
                    459:            px >= gd->linedata[py].cellsize) {
1.79      nicm      460:                memcpy(gc, &grid_default_cell, sizeof *gc);
                    461:                return;
                    462:        }
                    463:        return (grid_get_cell1(&gd->linedata[py], px, gc));
                    464: }
                    465:
1.1       nicm      466: /* Set cell at relative position. */
                    467: void
1.31      nicm      468: grid_set_cell(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
1.1       nicm      469: {
1.48      nicm      470:        struct grid_line        *gl;
                    471:        struct grid_cell_entry  *gce;
                    472:
1.81      nicm      473:        if (grid_check_y(gd, __func__, py) != 0)
1.1       nicm      474:                return;
                    475:
1.58      nicm      476:        grid_expand_line(gd, py, px + 1, 8);
1.48      nicm      477:
                    478:        gl = &gd->linedata[py];
1.58      nicm      479:        if (px + 1 > gl->cellused)
                    480:                gl->cellused = px + 1;
                    481:
1.63      nicm      482:        gce = &gl->celldata[px];
1.65      nicm      483:        if (grid_need_extended_cell(gce, gc))
1.59      nicm      484:                grid_extended_cell(gl, gce, gc);
1.63      nicm      485:        else
                    486:                grid_store_cell(gce, gc, gc->data.data[0]);
1.64      nicm      487: }
                    488:
                    489: /* Set cells at relative position. */
                    490: void
                    491: grid_set_cells(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc,
                    492:     const char *s, size_t slen)
                    493: {
                    494:        struct grid_line        *gl;
                    495:        struct grid_cell_entry  *gce;
                    496:        struct grid_cell        *gcp;
                    497:        u_int                    i;
                    498:
1.81      nicm      499:        if (grid_check_y(gd, __func__, py) != 0)
1.64      nicm      500:                return;
                    501:
                    502:        grid_expand_line(gd, py, px + slen, 8);
                    503:
                    504:        gl = &gd->linedata[py];
                    505:        if (px + slen > gl->cellused)
                    506:                gl->cellused = px + slen;
                    507:
                    508:        for (i = 0; i < slen; i++) {
                    509:                gce = &gl->celldata[px + i];
1.65      nicm      510:                if (grid_need_extended_cell(gce, gc)) {
                    511:                        gcp = grid_extended_cell(gl, gce, gc);
1.64      nicm      512:                        utf8_set(&gcp->data, s[i]);
                    513:                } else
                    514:                        grid_store_cell(gce, gc, s[i]);
                    515:        }
1.1       nicm      516: }
                    517:
1.14      nicm      518: /* Clear area. */
1.1       nicm      519: void
1.58      nicm      520: grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny, u_int bg)
1.1       nicm      521: {
                    522:        u_int   xx, yy;
                    523:
                    524:        if (nx == 0 || ny == 0)
                    525:                return;
                    526:
                    527:        if (px == 0 && nx == gd->sx) {
1.58      nicm      528:                grid_clear_lines(gd, py, ny, bg);
1.1       nicm      529:                return;
                    530:        }
                    531:
1.81      nicm      532:        if (grid_check_y(gd, __func__, py) != 0)
1.1       nicm      533:                return;
1.81      nicm      534:        if (grid_check_y(gd, __func__, py + ny - 1) != 0)
1.1       nicm      535:                return;
                    536:
                    537:        for (yy = py; yy < py + ny; yy++) {
1.58      nicm      538:                if (px + nx >= gd->sx && px < gd->linedata[yy].cellused)
                    539:                        gd->linedata[yy].cellused = px;
                    540:                if (px > gd->linedata[yy].cellsize && bg == 8)
1.14      nicm      541:                        continue;
1.58      nicm      542:                if (px + nx >= gd->linedata[yy].cellsize && bg == 8) {
1.14      nicm      543:                        gd->linedata[yy].cellsize = px;
                    544:                        continue;
                    545:                }
1.72      nicm      546:                grid_expand_line(gd, yy, px + nx, 8); /* default bg first */
1.58      nicm      547:                for (xx = px; xx < px + nx; xx++)
                    548:                        grid_clear_cell(gd, xx, yy, bg);
1.1       nicm      549:        }
                    550: }
                    551:
                    552: /* Clear lines. This just frees and truncates the lines. */
                    553: void
1.58      nicm      554: grid_clear_lines(struct grid *gd, u_int py, u_int ny, u_int bg)
1.1       nicm      555: {
1.75      nicm      556:        u_int   yy;
1.1       nicm      557:
                    558:        if (ny == 0)
                    559:                return;
                    560:
1.81      nicm      561:        if (grid_check_y(gd, __func__, py) != 0)
1.1       nicm      562:                return;
1.81      nicm      563:        if (grid_check_y(gd, __func__, py + ny - 1) != 0)
1.1       nicm      564:                return;
                    565:
                    566:        for (yy = py; yy < py + ny; yy++) {
1.75      nicm      567:                grid_free_line(gd, yy);
1.58      nicm      568:                grid_empty_line(gd, yy, bg);
1.1       nicm      569:        }
                    570: }
                    571:
                    572: /* Move a group of lines. */
                    573: void
1.58      nicm      574: grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny, u_int bg)
1.1       nicm      575: {
                    576:        u_int   yy;
                    577:
                    578:        if (ny == 0 || py == dy)
                    579:                return;
                    580:
1.81      nicm      581:        if (grid_check_y(gd, __func__, py) != 0)
1.1       nicm      582:                return;
1.81      nicm      583:        if (grid_check_y(gd, __func__, py + ny - 1) != 0)
1.1       nicm      584:                return;
1.81      nicm      585:        if (grid_check_y(gd, __func__, dy) != 0)
1.1       nicm      586:                return;
1.81      nicm      587:        if (grid_check_y(gd, __func__, dy + ny - 1) != 0)
1.1       nicm      588:                return;
                    589:
                    590:        /* Free any lines which are being replaced. */
                    591:        for (yy = dy; yy < dy + ny; yy++) {
                    592:                if (yy >= py && yy < py + ny)
                    593:                        continue;
1.75      nicm      594:                grid_free_line(gd, yy);
1.1       nicm      595:        }
                    596:
1.46      nicm      597:        memmove(&gd->linedata[dy], &gd->linedata[py],
                    598:            ny * (sizeof *gd->linedata));
1.1       nicm      599:
1.75      nicm      600:        /*
                    601:         * Wipe any lines that have been moved (without freeing them - they are
                    602:         * still present).
                    603:         */
1.1       nicm      604:        for (yy = py; yy < py + ny; yy++) {
1.58      nicm      605:                if (yy < dy || yy >= dy + ny)
                    606:                        grid_empty_line(gd, yy, bg);
1.1       nicm      607:        }
                    608: }
                    609:
                    610: /* Move a group of cells. */
                    611: void
1.58      nicm      612: grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx,
                    613:     u_int bg)
1.1       nicm      614: {
1.10      nicm      615:        struct grid_line        *gl;
                    616:        u_int                    xx;
1.1       nicm      617:
                    618:        if (nx == 0 || px == dx)
                    619:                return;
                    620:
1.81      nicm      621:        if (grid_check_y(gd, __func__, py) != 0)
1.1       nicm      622:                return;
1.10      nicm      623:        gl = &gd->linedata[py];
1.1       nicm      624:
1.58      nicm      625:        grid_expand_line(gd, py, px + nx, 8);
                    626:        grid_expand_line(gd, py, dx + nx, 8);
1.46      nicm      627:        memmove(&gl->celldata[dx], &gl->celldata[px],
                    628:            nx * sizeof *gl->celldata);
1.67      nicm      629:        if (dx + nx > gl->cellused)
                    630:                gl->cellused = dx + nx;
1.1       nicm      631:
                    632:        /* Wipe any cells that have been moved. */
                    633:        for (xx = px; xx < px + nx; xx++) {
                    634:                if (xx >= dx && xx < dx + nx)
                    635:                        continue;
1.58      nicm      636:                grid_clear_cell(gd, xx, py, bg);
1.1       nicm      637:        }
1.3       nicm      638: }
                    639:
1.24      nicm      640: /* Get ANSI foreground sequence. */
1.56      nicm      641: static size_t
1.24      nicm      642: grid_string_cells_fg(const struct grid_cell *gc, int *values)
                    643: {
                    644:        size_t  n;
1.53      nicm      645:        u_char  r, g, b;
1.24      nicm      646:
                    647:        n = 0;
1.53      nicm      648:        if (gc->fg & COLOUR_FLAG_256) {
1.24      nicm      649:                values[n++] = 38;
                    650:                values[n++] = 5;
1.53      nicm      651:                values[n++] = gc->fg & 0xff;
                    652:        } else if (gc->fg & COLOUR_FLAG_RGB) {
1.52      nicm      653:                values[n++] = 38;
                    654:                values[n++] = 2;
1.53      nicm      655:                colour_split_rgb(gc->fg, &r, &g, &b);
                    656:                values[n++] = r;
                    657:                values[n++] = g;
                    658:                values[n++] = b;
1.24      nicm      659:        } else {
                    660:                switch (gc->fg) {
1.45      nicm      661:                case 0:
                    662:                case 1:
                    663:                case 2:
                    664:                case 3:
                    665:                case 4:
                    666:                case 5:
                    667:                case 6:
                    668:                case 7:
                    669:                        values[n++] = gc->fg + 30;
                    670:                        break;
                    671:                case 8:
                    672:                        values[n++] = 39;
                    673:                        break;
                    674:                case 90:
                    675:                case 91:
                    676:                case 92:
                    677:                case 93:
                    678:                case 94:
                    679:                case 95:
                    680:                case 96:
                    681:                case 97:
                    682:                        values[n++] = gc->fg;
                    683:                        break;
1.24      nicm      684:                }
                    685:        }
                    686:        return (n);
                    687: }
                    688:
                    689: /* Get ANSI background sequence. */
1.56      nicm      690: static size_t
1.24      nicm      691: grid_string_cells_bg(const struct grid_cell *gc, int *values)
                    692: {
                    693:        size_t  n;
1.53      nicm      694:        u_char  r, g, b;
1.24      nicm      695:
                    696:        n = 0;
1.53      nicm      697:        if (gc->bg & COLOUR_FLAG_256) {
1.24      nicm      698:                values[n++] = 48;
                    699:                values[n++] = 5;
1.53      nicm      700:                values[n++] = gc->bg & 0xff;
                    701:        } else if (gc->bg & COLOUR_FLAG_RGB) {
1.52      nicm      702:                values[n++] = 48;
                    703:                values[n++] = 2;
1.53      nicm      704:                colour_split_rgb(gc->bg, &r, &g, &b);
                    705:                values[n++] = r;
                    706:                values[n++] = g;
                    707:                values[n++] = b;
1.24      nicm      708:        } else {
                    709:                switch (gc->bg) {
                    710:                case 0:
                    711:                case 1:
                    712:                case 2:
                    713:                case 3:
                    714:                case 4:
                    715:                case 5:
                    716:                case 6:
                    717:                case 7:
                    718:                        values[n++] = gc->bg + 40;
                    719:                        break;
                    720:                case 8:
                    721:                        values[n++] = 49;
                    722:                        break;
                    723:                case 100:
                    724:                case 101:
                    725:                case 102:
                    726:                case 103:
                    727:                case 104:
1.53      nicm      728:                case 105:
1.24      nicm      729:                case 106:
                    730:                case 107:
                    731:                        values[n++] = gc->bg - 10;
                    732:                        break;
                    733:                }
                    734:        }
                    735:        return (n);
                    736: }
                    737:
                    738: /*
                    739:  * Returns ANSI code to set particular attributes (colour, bold and so on)
1.74      nicm      740:  * given a current state.
1.24      nicm      741:  */
1.56      nicm      742: static void
1.24      nicm      743: grid_string_cells_code(const struct grid_cell *lastgc,
1.26      nicm      744:     const struct grid_cell *gc, char *buf, size_t len, int escape_c0)
1.24      nicm      745: {
1.52      nicm      746:        int     oldc[64], newc[64], s[128];
1.24      nicm      747:        size_t  noldc, nnewc, n, i;
1.70      nicm      748:        u_int   attr = gc->attr, lastattr = lastgc->attr;
1.24      nicm      749:        char    tmp[64];
                    750:
                    751:        struct {
                    752:                u_int   mask;
                    753:                u_int   code;
                    754:        } attrs[] = {
                    755:                { GRID_ATTR_BRIGHT, 1 },
                    756:                { GRID_ATTR_DIM, 2 },
                    757:                { GRID_ATTR_ITALICS, 3 },
                    758:                { GRID_ATTR_UNDERSCORE, 4 },
                    759:                { GRID_ATTR_BLINK, 5 },
                    760:                { GRID_ATTR_REVERSE, 7 },
1.68      nicm      761:                { GRID_ATTR_HIDDEN, 8 },
                    762:                { GRID_ATTR_STRIKETHROUGH, 9 }
1.24      nicm      763:        };
                    764:        n = 0;
                    765:
                    766:        /* If any attribute is removed, begin with 0. */
                    767:        for (i = 0; i < nitems(attrs); i++) {
                    768:                if (!(attr & attrs[i].mask) && (lastattr & attrs[i].mask)) {
                    769:                        s[n++] = 0;
1.25      nicm      770:                        lastattr &= GRID_ATTR_CHARSET;
1.24      nicm      771:                        break;
                    772:                }
                    773:        }
                    774:        /* For each attribute that is newly set, add its code. */
                    775:        for (i = 0; i < nitems(attrs); i++) {
                    776:                if ((attr & attrs[i].mask) && !(lastattr & attrs[i].mask))
                    777:                        s[n++] = attrs[i].code;
                    778:        }
                    779:
1.70      nicm      780:        /* Write the attributes. */
                    781:        *buf = '\0';
                    782:        if (n > 0) {
                    783:                if (escape_c0)
                    784:                        strlcat(buf, "\\033[", len);
                    785:                else
                    786:                        strlcat(buf, "\033[", len);
                    787:                for (i = 0; i < n; i++) {
                    788:                        if (i + 1 < n)
                    789:                                xsnprintf(tmp, sizeof tmp, "%d;", s[i]);
                    790:                        else
                    791:                                xsnprintf(tmp, sizeof tmp, "%d", s[i]);
                    792:                        strlcat(buf, tmp, len);
                    793:                }
                    794:                strlcat(buf, "m", len);
                    795:        }
                    796:
                    797:        /* If the foreground colour changed, write its parameters. */
1.24      nicm      798:        nnewc = grid_string_cells_fg(gc, newc);
                    799:        noldc = grid_string_cells_fg(lastgc, oldc);
1.70      nicm      800:        if (nnewc != noldc ||
                    801:            memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0 ||
                    802:            (n != 0 && s[0] == 0)) {
                    803:                if (escape_c0)
                    804:                        strlcat(buf, "\\033[", len);
                    805:                else
                    806:                        strlcat(buf, "\033[", len);
                    807:                for (i = 0; i < nnewc; i++) {
                    808:                        if (i + 1 < nnewc)
                    809:                                xsnprintf(tmp, sizeof tmp, "%d;", newc[i]);
                    810:                        else
                    811:                                xsnprintf(tmp, sizeof tmp, "%d", newc[i]);
                    812:                        strlcat(buf, tmp, len);
                    813:                }
                    814:                strlcat(buf, "m", len);
1.24      nicm      815:        }
                    816:
1.39      nicm      817:        /* If the background colour changed, append its parameters. */
1.24      nicm      818:        nnewc = grid_string_cells_bg(gc, newc);
                    819:        noldc = grid_string_cells_bg(lastgc, oldc);
1.70      nicm      820:        if (nnewc != noldc ||
                    821:            memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0 ||
                    822:            (n != 0 && s[0] == 0)) {
1.26      nicm      823:                if (escape_c0)
                    824:                        strlcat(buf, "\\033[", len);
                    825:                else
                    826:                        strlcat(buf, "\033[", len);
1.70      nicm      827:                for (i = 0; i < nnewc; i++) {
                    828:                        if (i + 1 < nnewc)
                    829:                                xsnprintf(tmp, sizeof tmp, "%d;", newc[i]);
1.24      nicm      830:                        else
1.70      nicm      831:                                xsnprintf(tmp, sizeof tmp, "%d", newc[i]);
1.24      nicm      832:                        strlcat(buf, tmp, len);
                    833:                }
                    834:                strlcat(buf, "m", len);
                    835:        }
                    836:
                    837:        /* Append shift in/shift out if needed. */
1.26      nicm      838:        if ((attr & GRID_ATTR_CHARSET) && !(lastattr & GRID_ATTR_CHARSET)) {
                    839:                if (escape_c0)
1.70      nicm      840:                        strlcat(buf, "\\016", len); /* SO */
1.26      nicm      841:                else
                    842:                        strlcat(buf, "\016", len);  /* SO */
                    843:        }
                    844:        if (!(attr & GRID_ATTR_CHARSET) && (lastattr & GRID_ATTR_CHARSET)) {
                    845:                if (escape_c0)
1.70      nicm      846:                        strlcat(buf, "\\017", len); /* SI */
1.26      nicm      847:                else
                    848:                        strlcat(buf, "\017", len);  /* SI */
                    849:        }
1.24      nicm      850: }
                    851:
1.3       nicm      852: /* Convert cells into a string. */
                    853: char *
1.24      nicm      854: grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx,
1.28      nicm      855:     struct grid_cell **lastgc, int with_codes, int escape_c0, int trim)
1.3       nicm      856: {
1.48      nicm      857:        struct grid_cell         gc;
1.24      nicm      858:        static struct grid_cell  lastgc1;
1.38      nicm      859:        const char              *data;
1.24      nicm      860:        char                    *buf, code[128];
1.26      nicm      861:        size_t                   len, off, size, codelen;
1.16      nicm      862:        u_int                    xx;
1.30      nicm      863:        const struct grid_line  *gl;
1.3       nicm      864:
1.29      nicm      865:        if (lastgc != NULL && *lastgc == NULL) {
1.24      nicm      866:                memcpy(&lastgc1, &grid_default_cell, sizeof lastgc1);
                    867:                *lastgc = &lastgc1;
                    868:        }
                    869:
1.3       nicm      870:        len = 128;
                    871:        buf = xmalloc(len);
                    872:        off = 0;
                    873:
1.30      nicm      874:        gl = grid_peek_line(gd, py);
1.3       nicm      875:        for (xx = px; xx < px + nx; xx++) {
1.30      nicm      876:                if (gl == NULL || xx >= gl->cellsize)
                    877:                        break;
1.48      nicm      878:                grid_get_cell(gd, xx, py, &gc);
                    879:                if (gc.flags & GRID_FLAG_PADDING)
1.3       nicm      880:                        continue;
                    881:
1.24      nicm      882:                if (with_codes) {
1.48      nicm      883:                        grid_string_cells_code(*lastgc, &gc, code, sizeof code,
1.26      nicm      884:                            escape_c0);
1.24      nicm      885:                        codelen = strlen(code);
1.48      nicm      886:                        memcpy(*lastgc, &gc, sizeof **lastgc);
1.24      nicm      887:                } else
                    888:                        codelen = 0;
                    889:
1.48      nicm      890:                data = gc.data.data;
                    891:                size = gc.data.size;
1.26      nicm      892:                if (escape_c0 && size == 1 && *data == '\\') {
1.27      nicm      893:                        data = "\\\\";
1.26      nicm      894:                        size = 2;
                    895:                }
                    896:
                    897:                while (len < off + size + codelen + 1) {
1.41      nicm      898:                        buf = xreallocarray(buf, 2, len);
1.21      nicm      899:                        len *= 2;
                    900:                }
1.3       nicm      901:
1.24      nicm      902:                if (codelen != 0) {
                    903:                        memcpy(buf + off, code, codelen);
                    904:                        off += codelen;
                    905:                }
1.26      nicm      906:                memcpy(buf + off, data, size);
                    907:                off += size;
1.3       nicm      908:        }
1.17      nicm      909:
1.37      nicm      910:        if (trim) {
1.28      nicm      911:                while (off > 0 && buf[off - 1] == ' ')
                    912:                        off--;
1.32      nicm      913:        }
1.3       nicm      914:        buf[off] = '\0';
1.26      nicm      915:
1.3       nicm      916:        return (buf);
1.7       nicm      917: }
                    918:
1.17      nicm      919: /*
1.75      nicm      920:  * Duplicate a set of lines between two grids. Both source and destination
                    921:  * should be big enough.
1.7       nicm      922:  */
                    923: void
1.31      nicm      924: grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy,
                    925:     u_int ny)
1.7       nicm      926: {
1.10      nicm      927:        struct grid_line        *dstl, *srcl;
                    928:        u_int                    yy;
1.7       nicm      929:
                    930:        if (dy + ny > dst->hsize + dst->sy)
                    931:                ny = dst->hsize + dst->sy - dy;
                    932:        if (sy + ny > src->hsize + src->sy)
                    933:                ny = src->hsize + src->sy - sy;
1.75      nicm      934:        grid_free_lines(dst, dy, ny);
1.7       nicm      935:
                    936:        for (yy = 0; yy < ny; yy++) {
1.11      nicm      937:                srcl = &src->linedata[sy];
                    938:                dstl = &dst->linedata[dy];
1.10      nicm      939:
                    940:                memcpy(dstl, srcl, sizeof *dstl);
                    941:                if (srcl->cellsize != 0) {
1.42      deraadt   942:                        dstl->celldata = xreallocarray(NULL,
1.10      nicm      943:                            srcl->cellsize, sizeof *dstl->celldata);
                    944:                        memcpy(dstl->celldata, srcl->celldata,
                    945:                            srcl->cellsize * sizeof *dstl->celldata);
1.44      nicm      946:                } else
                    947:                        dstl->celldata = NULL;
1.7       nicm      948:
1.48      nicm      949:                if (srcl->extdsize != 0) {
                    950:                        dstl->extdsize = srcl->extdsize;
                    951:                        dstl->extddata = xreallocarray(NULL, dstl->extdsize,
                    952:                            sizeof *dstl->extddata);
                    953:                        memcpy(dstl->extddata, srcl->extddata, dstl->extdsize *
                    954:                            sizeof *dstl->extddata);
                    955:                }
                    956:
1.10      nicm      957:                sy++;
                    958:                dy++;
1.7       nicm      959:        }
1.22      nicm      960: }
                    961:
1.80      nicm      962: /* Mark line as dead. */
                    963: static void
                    964: grid_reflow_dead(struct grid_line *gl)
                    965: {
                    966:        memset(gl, 0, sizeof *gl);
                    967:        gl->flags = GRID_LINE_DEAD;
                    968: }
                    969:
                    970: /* Add lines, return the first new one. */
                    971: static struct grid_line *
                    972: grid_reflow_add(struct grid *gd, u_int n)
                    973: {
                    974:        struct grid_line        *gl;
                    975:        u_int                    sy = gd->sy + n;
                    976:
                    977:        gd->linedata = xreallocarray(gd->linedata, sy, sizeof *gd->linedata);
                    978:        gl = &gd->linedata[gd->sy];
                    979:        memset(gl, 0, n * (sizeof *gl));
                    980:        gd->sy = sy;
                    981:        return (gl);
                    982: }
                    983:
                    984: /* Move a line across. */
                    985: static struct grid_line *
                    986: grid_reflow_move(struct grid *gd, struct grid_line *from)
                    987: {
                    988:        struct grid_line        *to;
                    989:
                    990:        to = grid_reflow_add(gd, 1);
                    991:        memcpy(to, from, sizeof *to);
                    992:        grid_reflow_dead(from);
                    993:        return (to);
                    994: }
                    995:
1.79      nicm      996: /* Join line below onto this one. */
1.56      nicm      997: static void
1.80      nicm      998: grid_reflow_join(struct grid *target, struct grid *gd, u_int sx, u_int yy,
                    999:     u_int width, u_int *cy, int already)
1.48      nicm     1000: {
1.83      nicm     1001:        struct grid_line        *gl, *from = NULL;
1.79      nicm     1002:        struct grid_cell         gc;
1.83      nicm     1003:        u_int                    lines, left, i, to, line, want = 0;
1.80      nicm     1004:        u_int                    at;
1.79      nicm     1005:        int                      wrapped = 1;
1.48      nicm     1006:
1.80      nicm     1007:        /*
                   1008:         * Add a new target line.
                   1009:         */
                   1010:        if (!already) {
                   1011:                to = target->sy;
                   1012:                gl = grid_reflow_move(target, &gd->linedata[yy]);
                   1013:        } else {
                   1014:                to = target->sy - 1;
                   1015:                gl = &target->linedata[to];
                   1016:        }
                   1017:        at = gl->cellused;
                   1018:
                   1019:        /*
                   1020:         * Loop until no more to consume or the target line is full.
                   1021:         */
1.79      nicm     1022:        lines = 0;
                   1023:        for (;;) {
                   1024:                /*
                   1025:                 * If this is now the last line, there is nothing more to be
                   1026:                 * done.
                   1027:                 */
1.82      nicm     1028:                if (yy + 1 + lines == gd->hsize + gd->sy)
1.79      nicm     1029:                        break;
                   1030:                line = yy + 1 + lines;
1.48      nicm     1031:
1.79      nicm     1032:                /* If the next line is empty, skip it. */
                   1033:                if (~gd->linedata[line].flags & GRID_LINE_WRAPPED)
                   1034:                        wrapped = 0;
                   1035:                if (gd->linedata[line].cellused == 0) {
                   1036:                        if (!wrapped)
                   1037:                                break;
1.82      nicm     1038:                        lines++;
1.48      nicm     1039:                        continue;
1.79      nicm     1040:                }
                   1041:
                   1042:                /*
                   1043:                 * Is the destination line now full? Copy the first character
                   1044:                 * separately because we need to leave "from" set to the last
                   1045:                 * line if this line is full.
                   1046:                 */
                   1047:                grid_get_cell1(&gd->linedata[line], 0, &gc);
                   1048:                if (width + gc.data.width > sx)
                   1049:                        break;
                   1050:                width += gc.data.width;
1.80      nicm     1051:                grid_set_cell(target, at, to, &gc);
1.79      nicm     1052:                at++;
                   1053:
                   1054:                /* Join as much more as possible onto the current line. */
                   1055:                from = &gd->linedata[line];
                   1056:                for (want = 1; want < from->cellused; want++) {
                   1057:                        grid_get_cell1(from, want, &gc);
                   1058:                        if (width + gc.data.width > sx)
                   1059:                                break;
                   1060:                        width += gc.data.width;
                   1061:
1.80      nicm     1062:                        grid_set_cell(target, at, to, &gc);
1.79      nicm     1063:                        at++;
                   1064:                }
                   1065:                lines++;
                   1066:
                   1067:                /*
                   1068:                 * If this line wasn't wrapped or we didn't consume the entire
                   1069:                 * line, don't try to join any further lines.
                   1070:                 */
                   1071:                if (!wrapped || want != from->cellused || width == sx)
                   1072:                        break;
                   1073:        }
                   1074:        if (lines == 0)
                   1075:                return;
1.48      nicm     1076:
1.79      nicm     1077:        /*
                   1078:         * If we didn't consume the entire final line, then remove what we did
                   1079:         * consume. If we consumed the entire line and it wasn't wrapped,
                   1080:         * remove the wrap flag from this line.
                   1081:         */
                   1082:        left = from->cellused - want;
                   1083:        if (left != 0) {
                   1084:                grid_move_cells(gd, 0, want, yy + lines, left, 8);
                   1085:                from->cellsize = from->cellused = left;
                   1086:                lines--;
                   1087:        } else if (!wrapped)
                   1088:                gl->flags &= ~GRID_LINE_WRAPPED;
                   1089:
                   1090:        /* Remove the lines that were completely consumed. */
1.80      nicm     1091:        for (i = yy + 1; i < yy + 1 + lines; i++) {
                   1092:                free(gd->linedata[i].celldata);
                   1093:                free(gd->linedata[i].extddata);
                   1094:                grid_reflow_dead(&gd->linedata[i]);
1.48      nicm     1095:        }
1.79      nicm     1096:
                   1097:        /* Adjust cursor and scroll positions. */
1.80      nicm     1098:        if (*cy > to + lines)
1.79      nicm     1099:                *cy -= lines;
1.80      nicm     1100:        else if (*cy > to)
                   1101:                *cy = to;
                   1102:        if (gd->hscrolled > to + lines)
1.79      nicm     1103:                gd->hscrolled -= lines;
1.80      nicm     1104:        else if (gd->hscrolled > to)
                   1105:                gd->hscrolled = to;
1.48      nicm     1106: }
                   1107:
1.79      nicm     1108: /* Split this line into several new ones */
1.56      nicm     1109: static void
1.80      nicm     1110: grid_reflow_split(struct grid *target, struct grid *gd, u_int sx, u_int yy,
                   1111:     u_int at, u_int *cy)
1.23      nicm     1112: {
1.80      nicm     1113:        struct grid_line        *gl = &gd->linedata[yy], *first;
1.79      nicm     1114:        struct grid_cell         gc;
1.80      nicm     1115:        u_int                    line, lines, width, i, xx;
                   1116:        u_int                    used = gl->cellused;
1.79      nicm     1117:        int                      flags = gl->flags;
1.23      nicm     1118:
1.80      nicm     1119:        /* How many lines do we need to insert? We know we need at least two. */
1.79      nicm     1120:        if (~gl->flags & GRID_LINE_EXTENDED)
1.80      nicm     1121:                lines = 1 + (gl->cellused - 1) / sx;
1.79      nicm     1122:        else {
1.80      nicm     1123:                lines = 2;
1.79      nicm     1124:                width = 0;
                   1125:                for (i = at; i < used; i++) {
                   1126:                        grid_get_cell1(gl, i, &gc);
                   1127:                        if (width + gc.data.width > sx) {
                   1128:                                lines++;
                   1129:                                width = 0;
                   1130:                        }
                   1131:                        width += gc.data.width;
                   1132:                }
                   1133:        }
1.23      nicm     1134:
1.79      nicm     1135:        /* Insert new lines. */
1.80      nicm     1136:        line = target->sy + 1;
                   1137:        first = grid_reflow_add(target, lines);
1.79      nicm     1138:
                   1139:        /* Copy sections from the original line. */
                   1140:        width = 0;
                   1141:        xx = 0;
                   1142:        for (i = at; i < used; i++) {
                   1143:                grid_get_cell1(gl, i, &gc);
                   1144:                if (width + gc.data.width > sx) {
1.80      nicm     1145:                        target->linedata[line].flags |= GRID_LINE_WRAPPED;
1.79      nicm     1146:
                   1147:                        line++;
                   1148:                        width = 0;
                   1149:                        xx = 0;
                   1150:                }
                   1151:                width += gc.data.width;
1.80      nicm     1152:                grid_set_cell(target, xx, line, &gc);
1.79      nicm     1153:                xx++;
                   1154:        }
                   1155:        if (flags & GRID_LINE_WRAPPED)
1.80      nicm     1156:                target->linedata[line].flags |= GRID_LINE_WRAPPED;
                   1157:
                   1158:        /* Move the remainder of the original line. */
                   1159:        gl->cellsize = gl->cellused = at;
                   1160:        gl->flags |= GRID_LINE_WRAPPED;
                   1161:        memcpy(first, gl, sizeof *first);
                   1162:        grid_reflow_dead(gl);
1.79      nicm     1163:
                   1164:        /* Adjust the cursor and scroll positions. */
                   1165:        if (yy <= *cy)
1.80      nicm     1166:                (*cy) += lines - 1;
1.79      nicm     1167:        if (yy <= gd->hscrolled)
1.80      nicm     1168:                gd->hscrolled += lines - 1;
1.23      nicm     1169:
1.79      nicm     1170:        /*
                   1171:         * If the original line had the wrapped flag and there is still space
                   1172:         * in the last new line, try to join with the next lines.
                   1173:         */
                   1174:        if (width < sx && (flags & GRID_LINE_WRAPPED))
1.80      nicm     1175:                grid_reflow_join(target, gd, sx, yy, width, cy, 1);
1.23      nicm     1176: }
                   1177:
1.79      nicm     1178: /* Reflow lines on grid to new width. */
                   1179: void
                   1180: grid_reflow(struct grid *gd, u_int sx, u_int *cursor)
1.23      nicm     1181: {
1.80      nicm     1182:        struct grid             *target;
1.79      nicm     1183:        struct grid_line        *gl;
                   1184:        struct grid_cell         gc;
                   1185:        u_int                    yy, cy, width, i, at, first;
                   1186:        struct timeval           start, tv;
1.23      nicm     1187:
1.79      nicm     1188:        gettimeofday(&start, NULL);
1.23      nicm     1189:
1.79      nicm     1190:        log_debug("%s: %u lines, new width %u", __func__, gd->hsize + gd->sy,
                   1191:            sx);
                   1192:        cy = gd->hsize + (*cursor);
1.23      nicm     1193:
1.79      nicm     1194:        /*
1.80      nicm     1195:         * Create a destination grid. This is just used as a container for the
                   1196:         * line data and may not be fully valid.
                   1197:         */
                   1198:        target = grid_create(gd->sx, 0, 0);
                   1199:
                   1200:        /*
                   1201:         * Loop over each source line.
1.79      nicm     1202:         */
                   1203:        for (yy = 0; yy < gd->hsize + gd->sy; yy++) {
                   1204:                gl = &gd->linedata[yy];
1.80      nicm     1205:                if (gl->flags & GRID_LINE_DEAD)
                   1206:                        continue;
1.23      nicm     1207:
1.80      nicm     1208:                /*
                   1209:                 * Work out the width of this line. first is the width of the
                   1210:                 * first character, at is the point at which the available
                   1211:                 * width is hit, and width is the full line width.
                   1212:                 */
1.79      nicm     1213:                first = at = width = 0;
                   1214:                if (~gl->flags & GRID_LINE_EXTENDED) {
                   1215:                        first = 1;
                   1216:                        width = gl->cellused;
                   1217:                        if (width > sx)
                   1218:                                at = sx;
                   1219:                        else
                   1220:                                at = width;
                   1221:                } else {
                   1222:                        for (i = 0; i < gl->cellused; i++) {
                   1223:                                grid_get_cell1(gl, i, &gc);
                   1224:                                if (i == 0)
                   1225:                                        first = gc.data.width;
                   1226:                                if (at == 0 && width + gc.data.width > sx)
                   1227:                                        at = i;
                   1228:                                width += gc.data.width;
                   1229:                        }
                   1230:                }
1.23      nicm     1231:
1.79      nicm     1232:                /*
1.80      nicm     1233:                 * If the line is exactly right or the first character is wider
                   1234:                 * than the targe width, just move it across unchanged.
1.79      nicm     1235:                 */
1.80      nicm     1236:                if (width == sx || first > sx) {
                   1237:                        grid_reflow_move(target, gl);
1.79      nicm     1238:                        continue;
1.80      nicm     1239:                }
1.23      nicm     1240:
1.79      nicm     1241:                /*
                   1242:                 * If the line is too big, it needs to be split, whether or not
                   1243:                 * it was previously wrapped.
                   1244:                 */
                   1245:                if (width > sx) {
1.80      nicm     1246:                        grid_reflow_split(target, gd, sx, yy, at, &cy);
1.79      nicm     1247:                        continue;
                   1248:                }
1.23      nicm     1249:
1.79      nicm     1250:                /*
                   1251:                 * If the line was previously wrapped, join as much as possible
                   1252:                 * of the next line.
                   1253:                 */
                   1254:                if (gl->flags & GRID_LINE_WRAPPED)
1.80      nicm     1255:                        grid_reflow_join(target, gd, sx, yy, width, &cy, 0);
                   1256:                else
                   1257:                        grid_reflow_move(target, gl);
                   1258:        }
1.55      nicm     1259:
1.80      nicm     1260:        /*
                   1261:         * Replace the old grid with the new.
                   1262:         */
                   1263:        if (target->sy < gd->sy)
                   1264:                grid_reflow_add(target, gd->sy - target->sy);
                   1265:        gd->hsize = target->sy - gd->sy;
                   1266:        free(gd->linedata);
                   1267:        gd->linedata = target->linedata;
                   1268:        free(target);
1.22      nicm     1269:
1.80      nicm     1270:        /*
                   1271:         * Update scrolled and cursor positions.
                   1272:         */
1.79      nicm     1273:        if (gd->hscrolled > gd->hsize)
                   1274:                gd->hscrolled = gd->hsize;
                   1275:        if (cy < gd->hsize)
                   1276:                *cursor = 0;
                   1277:        else
                   1278:                *cursor = cy - gd->hsize;
1.23      nicm     1279:
1.79      nicm     1280:        gettimeofday(&tv, NULL);
                   1281:        timersub(&tv, &start, &tv);
                   1282:        log_debug("%s: now %u lines (in %llu.%06u seconds)", __func__,
                   1283:            gd->hsize + gd->sy, (unsigned long long)tv.tv_sec,
                   1284:            (u_int)tv.tv_usec);
1.1       nicm     1285: }