[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.87

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