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

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