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

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