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

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