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

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