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

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