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

1.80    ! nicm        1: /* $OpenBSD: grid.c,v 1.79 2017/11/15 19:21:24 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.1       nicm      169: grid_check_y(struct grid *gd, u_int py)
                    170: {
1.78      nicm      171:        if (py >= gd->hsize + gd->sy) {
1.1       nicm      172:                log_debug("y out of range: %u", py);
                    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: {
                    410:        if (grid_check_y(gd, py) != 0)
                    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: {
                    444:        if (grid_check_y(gd, py) != 0 || px >= gd->linedata[py].cellsize) {
                    445:                memcpy(gc, &grid_default_cell, sizeof *gc);
                    446:                return;
                    447:        }
                    448:        return (grid_get_cell1(&gd->linedata[py], px, gc));
                    449: }
                    450:
1.1       nicm      451: /* Set cell at relative position. */
                    452: void
1.31      nicm      453: grid_set_cell(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
1.1       nicm      454: {
1.48      nicm      455:        struct grid_line        *gl;
                    456:        struct grid_cell_entry  *gce;
                    457:
1.1       nicm      458:        if (grid_check_y(gd, py) != 0)
                    459:                return;
                    460:
1.58      nicm      461:        grid_expand_line(gd, py, px + 1, 8);
1.48      nicm      462:
                    463:        gl = &gd->linedata[py];
1.58      nicm      464:        if (px + 1 > gl->cellused)
                    465:                gl->cellused = px + 1;
                    466:
1.63      nicm      467:        gce = &gl->celldata[px];
1.65      nicm      468:        if (grid_need_extended_cell(gce, gc))
1.59      nicm      469:                grid_extended_cell(gl, gce, gc);
1.63      nicm      470:        else
                    471:                grid_store_cell(gce, gc, gc->data.data[0]);
1.64      nicm      472: }
                    473:
                    474: /* Set cells at relative position. */
                    475: void
                    476: grid_set_cells(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc,
                    477:     const char *s, size_t slen)
                    478: {
                    479:        struct grid_line        *gl;
                    480:        struct grid_cell_entry  *gce;
                    481:        struct grid_cell        *gcp;
                    482:        u_int                    i;
                    483:
                    484:        if (grid_check_y(gd, py) != 0)
                    485:                return;
                    486:
                    487:        grid_expand_line(gd, py, px + slen, 8);
                    488:
                    489:        gl = &gd->linedata[py];
                    490:        if (px + slen > gl->cellused)
                    491:                gl->cellused = px + slen;
                    492:
                    493:        for (i = 0; i < slen; i++) {
                    494:                gce = &gl->celldata[px + i];
1.65      nicm      495:                if (grid_need_extended_cell(gce, gc)) {
                    496:                        gcp = grid_extended_cell(gl, gce, gc);
1.64      nicm      497:                        utf8_set(&gcp->data, s[i]);
                    498:                } else
                    499:                        grid_store_cell(gce, gc, s[i]);
                    500:        }
1.1       nicm      501: }
                    502:
1.14      nicm      503: /* Clear area. */
1.1       nicm      504: void
1.58      nicm      505: grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny, u_int bg)
1.1       nicm      506: {
                    507:        u_int   xx, yy;
                    508:
                    509:        if (nx == 0 || ny == 0)
                    510:                return;
                    511:
                    512:        if (px == 0 && nx == gd->sx) {
1.58      nicm      513:                grid_clear_lines(gd, py, ny, bg);
1.1       nicm      514:                return;
                    515:        }
                    516:
                    517:        if (grid_check_y(gd, py) != 0)
                    518:                return;
                    519:        if (grid_check_y(gd, py + ny - 1) != 0)
                    520:                return;
                    521:
                    522:        for (yy = py; yy < py + ny; yy++) {
1.58      nicm      523:                if (px + nx >= gd->sx && px < gd->linedata[yy].cellused)
                    524:                        gd->linedata[yy].cellused = px;
                    525:                if (px > gd->linedata[yy].cellsize && bg == 8)
1.14      nicm      526:                        continue;
1.58      nicm      527:                if (px + nx >= gd->linedata[yy].cellsize && bg == 8) {
1.14      nicm      528:                        gd->linedata[yy].cellsize = px;
                    529:                        continue;
                    530:                }
1.72      nicm      531:                grid_expand_line(gd, yy, px + nx, 8); /* default bg first */
1.58      nicm      532:                for (xx = px; xx < px + nx; xx++)
                    533:                        grid_clear_cell(gd, xx, yy, bg);
1.1       nicm      534:        }
                    535: }
                    536:
                    537: /* Clear lines. This just frees and truncates the lines. */
                    538: void
1.58      nicm      539: grid_clear_lines(struct grid *gd, u_int py, u_int ny, u_int bg)
1.1       nicm      540: {
1.75      nicm      541:        u_int   yy;
1.1       nicm      542:
                    543:        if (ny == 0)
                    544:                return;
                    545:
                    546:        if (grid_check_y(gd, py) != 0)
                    547:                return;
                    548:        if (grid_check_y(gd, py + ny - 1) != 0)
                    549:                return;
                    550:
                    551:        for (yy = py; yy < py + ny; yy++) {
1.75      nicm      552:                grid_free_line(gd, yy);
1.58      nicm      553:                grid_empty_line(gd, yy, bg);
1.1       nicm      554:        }
                    555: }
                    556:
                    557: /* Move a group of lines. */
                    558: void
1.58      nicm      559: grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny, u_int bg)
1.1       nicm      560: {
                    561:        u_int   yy;
                    562:
                    563:        if (ny == 0 || py == dy)
                    564:                return;
                    565:
                    566:        if (grid_check_y(gd, py) != 0)
                    567:                return;
                    568:        if (grid_check_y(gd, py + ny - 1) != 0)
                    569:                return;
                    570:        if (grid_check_y(gd, dy) != 0)
                    571:                return;
                    572:        if (grid_check_y(gd, dy + ny - 1) != 0)
                    573:                return;
                    574:
                    575:        /* Free any lines which are being replaced. */
                    576:        for (yy = dy; yy < dy + ny; yy++) {
                    577:                if (yy >= py && yy < py + ny)
                    578:                        continue;
1.75      nicm      579:                grid_free_line(gd, yy);
1.1       nicm      580:        }
                    581:
1.46      nicm      582:        memmove(&gd->linedata[dy], &gd->linedata[py],
                    583:            ny * (sizeof *gd->linedata));
1.1       nicm      584:
1.75      nicm      585:        /*
                    586:         * Wipe any lines that have been moved (without freeing them - they are
                    587:         * still present).
                    588:         */
1.1       nicm      589:        for (yy = py; yy < py + ny; yy++) {
1.58      nicm      590:                if (yy < dy || yy >= dy + ny)
                    591:                        grid_empty_line(gd, yy, bg);
1.1       nicm      592:        }
                    593: }
                    594:
                    595: /* Move a group of cells. */
                    596: void
1.58      nicm      597: grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx,
                    598:     u_int bg)
1.1       nicm      599: {
1.10      nicm      600:        struct grid_line        *gl;
                    601:        u_int                    xx;
1.1       nicm      602:
                    603:        if (nx == 0 || px == dx)
                    604:                return;
                    605:
                    606:        if (grid_check_y(gd, py) != 0)
                    607:                return;
1.10      nicm      608:        gl = &gd->linedata[py];
1.1       nicm      609:
1.58      nicm      610:        grid_expand_line(gd, py, px + nx, 8);
                    611:        grid_expand_line(gd, py, dx + nx, 8);
1.46      nicm      612:        memmove(&gl->celldata[dx], &gl->celldata[px],
                    613:            nx * sizeof *gl->celldata);
1.67      nicm      614:        if (dx + nx > gl->cellused)
                    615:                gl->cellused = dx + nx;
1.1       nicm      616:
                    617:        /* Wipe any cells that have been moved. */
                    618:        for (xx = px; xx < px + nx; xx++) {
                    619:                if (xx >= dx && xx < dx + nx)
                    620:                        continue;
1.58      nicm      621:                grid_clear_cell(gd, xx, py, bg);
1.1       nicm      622:        }
1.3       nicm      623: }
                    624:
1.24      nicm      625: /* Get ANSI foreground sequence. */
1.56      nicm      626: static size_t
1.24      nicm      627: grid_string_cells_fg(const struct grid_cell *gc, int *values)
                    628: {
                    629:        size_t  n;
1.53      nicm      630:        u_char  r, g, b;
1.24      nicm      631:
                    632:        n = 0;
1.53      nicm      633:        if (gc->fg & COLOUR_FLAG_256) {
1.24      nicm      634:                values[n++] = 38;
                    635:                values[n++] = 5;
1.53      nicm      636:                values[n++] = gc->fg & 0xff;
                    637:        } else if (gc->fg & COLOUR_FLAG_RGB) {
1.52      nicm      638:                values[n++] = 38;
                    639:                values[n++] = 2;
1.53      nicm      640:                colour_split_rgb(gc->fg, &r, &g, &b);
                    641:                values[n++] = r;
                    642:                values[n++] = g;
                    643:                values[n++] = b;
1.24      nicm      644:        } else {
                    645:                switch (gc->fg) {
1.45      nicm      646:                case 0:
                    647:                case 1:
                    648:                case 2:
                    649:                case 3:
                    650:                case 4:
                    651:                case 5:
                    652:                case 6:
                    653:                case 7:
                    654:                        values[n++] = gc->fg + 30;
                    655:                        break;
                    656:                case 8:
                    657:                        values[n++] = 39;
                    658:                        break;
                    659:                case 90:
                    660:                case 91:
                    661:                case 92:
                    662:                case 93:
                    663:                case 94:
                    664:                case 95:
                    665:                case 96:
                    666:                case 97:
                    667:                        values[n++] = gc->fg;
                    668:                        break;
1.24      nicm      669:                }
                    670:        }
                    671:        return (n);
                    672: }
                    673:
                    674: /* Get ANSI background sequence. */
1.56      nicm      675: static size_t
1.24      nicm      676: grid_string_cells_bg(const struct grid_cell *gc, int *values)
                    677: {
                    678:        size_t  n;
1.53      nicm      679:        u_char  r, g, b;
1.24      nicm      680:
                    681:        n = 0;
1.53      nicm      682:        if (gc->bg & COLOUR_FLAG_256) {
1.24      nicm      683:                values[n++] = 48;
                    684:                values[n++] = 5;
1.53      nicm      685:                values[n++] = gc->bg & 0xff;
                    686:        } else if (gc->bg & COLOUR_FLAG_RGB) {
1.52      nicm      687:                values[n++] = 48;
                    688:                values[n++] = 2;
1.53      nicm      689:                colour_split_rgb(gc->bg, &r, &g, &b);
                    690:                values[n++] = r;
                    691:                values[n++] = g;
                    692:                values[n++] = b;
1.24      nicm      693:        } else {
                    694:                switch (gc->bg) {
                    695:                case 0:
                    696:                case 1:
                    697:                case 2:
                    698:                case 3:
                    699:                case 4:
                    700:                case 5:
                    701:                case 6:
                    702:                case 7:
                    703:                        values[n++] = gc->bg + 40;
                    704:                        break;
                    705:                case 8:
                    706:                        values[n++] = 49;
                    707:                        break;
                    708:                case 100:
                    709:                case 101:
                    710:                case 102:
                    711:                case 103:
                    712:                case 104:
1.53      nicm      713:                case 105:
1.24      nicm      714:                case 106:
                    715:                case 107:
                    716:                        values[n++] = gc->bg - 10;
                    717:                        break;
                    718:                }
                    719:        }
                    720:        return (n);
                    721: }
                    722:
                    723: /*
                    724:  * Returns ANSI code to set particular attributes (colour, bold and so on)
1.74      nicm      725:  * given a current state.
1.24      nicm      726:  */
1.56      nicm      727: static void
1.24      nicm      728: grid_string_cells_code(const struct grid_cell *lastgc,
1.26      nicm      729:     const struct grid_cell *gc, char *buf, size_t len, int escape_c0)
1.24      nicm      730: {
1.52      nicm      731:        int     oldc[64], newc[64], s[128];
1.24      nicm      732:        size_t  noldc, nnewc, n, i;
1.70      nicm      733:        u_int   attr = gc->attr, lastattr = lastgc->attr;
1.24      nicm      734:        char    tmp[64];
                    735:
                    736:        struct {
                    737:                u_int   mask;
                    738:                u_int   code;
                    739:        } attrs[] = {
                    740:                { GRID_ATTR_BRIGHT, 1 },
                    741:                { GRID_ATTR_DIM, 2 },
                    742:                { GRID_ATTR_ITALICS, 3 },
                    743:                { GRID_ATTR_UNDERSCORE, 4 },
                    744:                { GRID_ATTR_BLINK, 5 },
                    745:                { GRID_ATTR_REVERSE, 7 },
1.68      nicm      746:                { GRID_ATTR_HIDDEN, 8 },
                    747:                { GRID_ATTR_STRIKETHROUGH, 9 }
1.24      nicm      748:        };
                    749:        n = 0;
                    750:
                    751:        /* If any attribute is removed, begin with 0. */
                    752:        for (i = 0; i < nitems(attrs); i++) {
                    753:                if (!(attr & attrs[i].mask) && (lastattr & attrs[i].mask)) {
                    754:                        s[n++] = 0;
1.25      nicm      755:                        lastattr &= GRID_ATTR_CHARSET;
1.24      nicm      756:                        break;
                    757:                }
                    758:        }
                    759:        /* For each attribute that is newly set, add its code. */
                    760:        for (i = 0; i < nitems(attrs); i++) {
                    761:                if ((attr & attrs[i].mask) && !(lastattr & attrs[i].mask))
                    762:                        s[n++] = attrs[i].code;
                    763:        }
                    764:
1.70      nicm      765:        /* Write the attributes. */
                    766:        *buf = '\0';
                    767:        if (n > 0) {
                    768:                if (escape_c0)
                    769:                        strlcat(buf, "\\033[", len);
                    770:                else
                    771:                        strlcat(buf, "\033[", len);
                    772:                for (i = 0; i < n; i++) {
                    773:                        if (i + 1 < n)
                    774:                                xsnprintf(tmp, sizeof tmp, "%d;", s[i]);
                    775:                        else
                    776:                                xsnprintf(tmp, sizeof tmp, "%d", s[i]);
                    777:                        strlcat(buf, tmp, len);
                    778:                }
                    779:                strlcat(buf, "m", len);
                    780:        }
                    781:
                    782:        /* If the foreground colour changed, write its parameters. */
1.24      nicm      783:        nnewc = grid_string_cells_fg(gc, newc);
                    784:        noldc = grid_string_cells_fg(lastgc, oldc);
1.70      nicm      785:        if (nnewc != noldc ||
                    786:            memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0 ||
                    787:            (n != 0 && s[0] == 0)) {
                    788:                if (escape_c0)
                    789:                        strlcat(buf, "\\033[", len);
                    790:                else
                    791:                        strlcat(buf, "\033[", len);
                    792:                for (i = 0; i < nnewc; i++) {
                    793:                        if (i + 1 < nnewc)
                    794:                                xsnprintf(tmp, sizeof tmp, "%d;", newc[i]);
                    795:                        else
                    796:                                xsnprintf(tmp, sizeof tmp, "%d", newc[i]);
                    797:                        strlcat(buf, tmp, len);
                    798:                }
                    799:                strlcat(buf, "m", len);
1.24      nicm      800:        }
                    801:
1.39      nicm      802:        /* If the background colour changed, append its parameters. */
1.24      nicm      803:        nnewc = grid_string_cells_bg(gc, newc);
                    804:        noldc = grid_string_cells_bg(lastgc, oldc);
1.70      nicm      805:        if (nnewc != noldc ||
                    806:            memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0 ||
                    807:            (n != 0 && s[0] == 0)) {
1.26      nicm      808:                if (escape_c0)
                    809:                        strlcat(buf, "\\033[", len);
                    810:                else
                    811:                        strlcat(buf, "\033[", len);
1.70      nicm      812:                for (i = 0; i < nnewc; i++) {
                    813:                        if (i + 1 < nnewc)
                    814:                                xsnprintf(tmp, sizeof tmp, "%d;", newc[i]);
1.24      nicm      815:                        else
1.70      nicm      816:                                xsnprintf(tmp, sizeof tmp, "%d", newc[i]);
1.24      nicm      817:                        strlcat(buf, tmp, len);
                    818:                }
                    819:                strlcat(buf, "m", len);
                    820:        }
                    821:
                    822:        /* Append shift in/shift out if needed. */
1.26      nicm      823:        if ((attr & GRID_ATTR_CHARSET) && !(lastattr & GRID_ATTR_CHARSET)) {
                    824:                if (escape_c0)
1.70      nicm      825:                        strlcat(buf, "\\016", len); /* SO */
1.26      nicm      826:                else
                    827:                        strlcat(buf, "\016", len);  /* SO */
                    828:        }
                    829:        if (!(attr & GRID_ATTR_CHARSET) && (lastattr & GRID_ATTR_CHARSET)) {
                    830:                if (escape_c0)
1.70      nicm      831:                        strlcat(buf, "\\017", len); /* SI */
1.26      nicm      832:                else
                    833:                        strlcat(buf, "\017", len);  /* SI */
                    834:        }
1.24      nicm      835: }
                    836:
1.3       nicm      837: /* Convert cells into a string. */
                    838: char *
1.24      nicm      839: grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx,
1.28      nicm      840:     struct grid_cell **lastgc, int with_codes, int escape_c0, int trim)
1.3       nicm      841: {
1.48      nicm      842:        struct grid_cell         gc;
1.24      nicm      843:        static struct grid_cell  lastgc1;
1.38      nicm      844:        const char              *data;
1.24      nicm      845:        char                    *buf, code[128];
1.26      nicm      846:        size_t                   len, off, size, codelen;
1.16      nicm      847:        u_int                    xx;
1.30      nicm      848:        const struct grid_line  *gl;
1.3       nicm      849:
1.29      nicm      850:        if (lastgc != NULL && *lastgc == NULL) {
1.24      nicm      851:                memcpy(&lastgc1, &grid_default_cell, sizeof lastgc1);
                    852:                *lastgc = &lastgc1;
                    853:        }
                    854:
1.3       nicm      855:        len = 128;
                    856:        buf = xmalloc(len);
                    857:        off = 0;
                    858:
1.30      nicm      859:        gl = grid_peek_line(gd, py);
1.3       nicm      860:        for (xx = px; xx < px + nx; xx++) {
1.30      nicm      861:                if (gl == NULL || xx >= gl->cellsize)
                    862:                        break;
1.48      nicm      863:                grid_get_cell(gd, xx, py, &gc);
                    864:                if (gc.flags & GRID_FLAG_PADDING)
1.3       nicm      865:                        continue;
                    866:
1.24      nicm      867:                if (with_codes) {
1.48      nicm      868:                        grid_string_cells_code(*lastgc, &gc, code, sizeof code,
1.26      nicm      869:                            escape_c0);
1.24      nicm      870:                        codelen = strlen(code);
1.48      nicm      871:                        memcpy(*lastgc, &gc, sizeof **lastgc);
1.24      nicm      872:                } else
                    873:                        codelen = 0;
                    874:
1.48      nicm      875:                data = gc.data.data;
                    876:                size = gc.data.size;
1.26      nicm      877:                if (escape_c0 && size == 1 && *data == '\\') {
1.27      nicm      878:                        data = "\\\\";
1.26      nicm      879:                        size = 2;
                    880:                }
                    881:
                    882:                while (len < off + size + codelen + 1) {
1.41      nicm      883:                        buf = xreallocarray(buf, 2, len);
1.21      nicm      884:                        len *= 2;
                    885:                }
1.3       nicm      886:
1.24      nicm      887:                if (codelen != 0) {
                    888:                        memcpy(buf + off, code, codelen);
                    889:                        off += codelen;
                    890:                }
1.26      nicm      891:                memcpy(buf + off, data, size);
                    892:                off += size;
1.3       nicm      893:        }
1.17      nicm      894:
1.37      nicm      895:        if (trim) {
1.28      nicm      896:                while (off > 0 && buf[off - 1] == ' ')
                    897:                        off--;
1.32      nicm      898:        }
1.3       nicm      899:        buf[off] = '\0';
1.26      nicm      900:
1.3       nicm      901:        return (buf);
1.7       nicm      902: }
                    903:
1.17      nicm      904: /*
1.75      nicm      905:  * Duplicate a set of lines between two grids. Both source and destination
                    906:  * should be big enough.
1.7       nicm      907:  */
                    908: void
1.31      nicm      909: grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy,
                    910:     u_int ny)
1.7       nicm      911: {
1.10      nicm      912:        struct grid_line        *dstl, *srcl;
                    913:        u_int                    yy;
1.7       nicm      914:
                    915:        if (dy + ny > dst->hsize + dst->sy)
                    916:                ny = dst->hsize + dst->sy - dy;
                    917:        if (sy + ny > src->hsize + src->sy)
                    918:                ny = src->hsize + src->sy - sy;
1.75      nicm      919:        grid_free_lines(dst, dy, ny);
1.7       nicm      920:
                    921:        for (yy = 0; yy < ny; yy++) {
1.11      nicm      922:                srcl = &src->linedata[sy];
                    923:                dstl = &dst->linedata[dy];
1.10      nicm      924:
                    925:                memcpy(dstl, srcl, sizeof *dstl);
                    926:                if (srcl->cellsize != 0) {
1.42      deraadt   927:                        dstl->celldata = xreallocarray(NULL,
1.10      nicm      928:                            srcl->cellsize, sizeof *dstl->celldata);
                    929:                        memcpy(dstl->celldata, srcl->celldata,
                    930:                            srcl->cellsize * sizeof *dstl->celldata);
1.44      nicm      931:                } else
                    932:                        dstl->celldata = NULL;
1.7       nicm      933:
1.48      nicm      934:                if (srcl->extdsize != 0) {
                    935:                        dstl->extdsize = srcl->extdsize;
                    936:                        dstl->extddata = xreallocarray(NULL, dstl->extdsize,
                    937:                            sizeof *dstl->extddata);
                    938:                        memcpy(dstl->extddata, srcl->extddata, dstl->extdsize *
                    939:                            sizeof *dstl->extddata);
                    940:                }
                    941:
1.10      nicm      942:                sy++;
                    943:                dy++;
1.7       nicm      944:        }
1.22      nicm      945: }
                    946:
1.80    ! nicm      947: /* Mark line as dead. */
        !           948: static void
        !           949: grid_reflow_dead(struct grid_line *gl)
        !           950: {
        !           951:        memset(gl, 0, sizeof *gl);
        !           952:        gl->flags = GRID_LINE_DEAD;
        !           953: }
        !           954:
        !           955: /* Add lines, return the first new one. */
        !           956: static struct grid_line *
        !           957: grid_reflow_add(struct grid *gd, u_int n)
        !           958: {
        !           959:        struct grid_line        *gl;
        !           960:        u_int                    sy = gd->sy + n;
        !           961:
        !           962:        gd->linedata = xreallocarray(gd->linedata, sy, sizeof *gd->linedata);
        !           963:        gl = &gd->linedata[gd->sy];
        !           964:        memset(gl, 0, n * (sizeof *gl));
        !           965:        gd->sy = sy;
        !           966:        return (gl);
        !           967: }
        !           968:
        !           969: /* Move a line across. */
        !           970: static struct grid_line *
        !           971: grid_reflow_move(struct grid *gd, struct grid_line *from)
        !           972: {
        !           973:        struct grid_line        *to;
        !           974:
        !           975:        to = grid_reflow_add(gd, 1);
        !           976:        memcpy(to, from, sizeof *to);
        !           977:        grid_reflow_dead(from);
        !           978:        return (to);
        !           979: }
        !           980:
1.79      nicm      981: /* Join line below onto this one. */
1.56      nicm      982: static void
1.80    ! nicm      983: grid_reflow_join(struct grid *target, struct grid *gd, u_int sx, u_int yy,
        !           984:     u_int width, u_int *cy, int already)
1.48      nicm      985: {
1.80    ! nicm      986:        struct grid_line        *gl, *from;
1.79      nicm      987:        struct grid_cell         gc;
1.80    ! nicm      988:        u_int                    lines, want, left, i, to, line;
        !           989:        u_int                    at;
1.79      nicm      990:        int                      wrapped = 1;
1.48      nicm      991:
1.80    ! nicm      992:        /*
        !           993:         * Add a new target line.
        !           994:         */
        !           995:        if (!already) {
        !           996:                to = target->sy;
        !           997:                gl = grid_reflow_move(target, &gd->linedata[yy]);
        !           998:        } else {
        !           999:                to = target->sy - 1;
        !          1000:                gl = &target->linedata[to];
        !          1001:        }
        !          1002:        at = gl->cellused;
        !          1003:
        !          1004:        /*
        !          1005:         * Loop until no more to consume or the target line is full.
        !          1006:         */
1.79      nicm     1007:        lines = 0;
                   1008:        for (;;) {
                   1009:                /*
                   1010:                 * If this is now the last line, there is nothing more to be
                   1011:                 * done.
                   1012:                 */
                   1013:                if (yy + lines == gd->hsize + gd->sy)
                   1014:                        break;
                   1015:                line = yy + 1 + lines;
1.48      nicm     1016:
1.79      nicm     1017:                /* If the next line is empty, skip it. */
                   1018:                if (~gd->linedata[line].flags & GRID_LINE_WRAPPED)
                   1019:                        wrapped = 0;
                   1020:                if (gd->linedata[line].cellused == 0) {
                   1021:                        if (!wrapped)
                   1022:                                break;
1.48      nicm     1023:                        continue;
1.79      nicm     1024:                }
                   1025:
                   1026:                /*
                   1027:                 * Is the destination line now full? Copy the first character
                   1028:                 * separately because we need to leave "from" set to the last
                   1029:                 * line if this line is full.
                   1030:                 */
                   1031:                grid_get_cell1(&gd->linedata[line], 0, &gc);
                   1032:                if (width + gc.data.width > sx)
                   1033:                        break;
                   1034:                width += gc.data.width;
1.80    ! nicm     1035:                grid_set_cell(target, at, to, &gc);
1.79      nicm     1036:                at++;
                   1037:
                   1038:                /* Join as much more as possible onto the current line. */
                   1039:                from = &gd->linedata[line];
                   1040:                for (want = 1; want < from->cellused; want++) {
                   1041:                        grid_get_cell1(from, want, &gc);
                   1042:                        if (width + gc.data.width > sx)
                   1043:                                break;
                   1044:                        width += gc.data.width;
                   1045:
1.80    ! nicm     1046:                        grid_set_cell(target, at, to, &gc);
1.79      nicm     1047:                        at++;
                   1048:                }
                   1049:                lines++;
                   1050:
                   1051:                /*
                   1052:                 * If this line wasn't wrapped or we didn't consume the entire
                   1053:                 * line, don't try to join any further lines.
                   1054:                 */
                   1055:                if (!wrapped || want != from->cellused || width == sx)
                   1056:                        break;
                   1057:        }
                   1058:        if (lines == 0)
                   1059:                return;
1.48      nicm     1060:
1.79      nicm     1061:        /*
                   1062:         * If we didn't consume the entire final line, then remove what we did
                   1063:         * consume. If we consumed the entire line and it wasn't wrapped,
                   1064:         * remove the wrap flag from this line.
                   1065:         */
                   1066:        left = from->cellused - want;
                   1067:        if (left != 0) {
                   1068:                grid_move_cells(gd, 0, want, yy + lines, left, 8);
                   1069:                from->cellsize = from->cellused = left;
                   1070:                lines--;
                   1071:        } else if (!wrapped)
                   1072:                gl->flags &= ~GRID_LINE_WRAPPED;
                   1073:
                   1074:        /* Remove the lines that were completely consumed. */
1.80    ! nicm     1075:        for (i = yy + 1; i < yy + 1 + lines; i++) {
        !          1076:                free(gd->linedata[i].celldata);
        !          1077:                free(gd->linedata[i].extddata);
        !          1078:                grid_reflow_dead(&gd->linedata[i]);
1.48      nicm     1079:        }
1.79      nicm     1080:
                   1081:        /* Adjust cursor and scroll positions. */
1.80    ! nicm     1082:        if (*cy > to + lines)
1.79      nicm     1083:                *cy -= lines;
1.80    ! nicm     1084:        else if (*cy > to)
        !          1085:                *cy = to;
        !          1086:        if (gd->hscrolled > to + lines)
1.79      nicm     1087:                gd->hscrolled -= lines;
1.80    ! nicm     1088:        else if (gd->hscrolled > to)
        !          1089:                gd->hscrolled = to;
1.48      nicm     1090: }
                   1091:
1.79      nicm     1092: /* Split this line into several new ones */
1.56      nicm     1093: static void
1.80    ! nicm     1094: grid_reflow_split(struct grid *target, struct grid *gd, u_int sx, u_int yy,
        !          1095:     u_int at, u_int *cy)
1.23      nicm     1096: {
1.80    ! nicm     1097:        struct grid_line        *gl = &gd->linedata[yy], *first;
1.79      nicm     1098:        struct grid_cell         gc;
1.80    ! nicm     1099:        u_int                    line, lines, width, i, xx;
        !          1100:        u_int                    used = gl->cellused;
1.79      nicm     1101:        int                      flags = gl->flags;
1.23      nicm     1102:
1.80    ! nicm     1103:        /* How many lines do we need to insert? We know we need at least two. */
1.79      nicm     1104:        if (~gl->flags & GRID_LINE_EXTENDED)
1.80    ! nicm     1105:                lines = 1 + (gl->cellused - 1) / sx;
1.79      nicm     1106:        else {
1.80    ! nicm     1107:                lines = 2;
1.79      nicm     1108:                width = 0;
                   1109:                for (i = at; i < used; i++) {
                   1110:                        grid_get_cell1(gl, i, &gc);
                   1111:                        if (width + gc.data.width > sx) {
                   1112:                                lines++;
                   1113:                                width = 0;
                   1114:                        }
                   1115:                        width += gc.data.width;
                   1116:                }
                   1117:        }
1.23      nicm     1118:
1.79      nicm     1119:        /* Insert new lines. */
1.80    ! nicm     1120:        line = target->sy + 1;
        !          1121:        first = grid_reflow_add(target, lines);
1.79      nicm     1122:
                   1123:        /* Copy sections from the original line. */
                   1124:        width = 0;
                   1125:        xx = 0;
                   1126:        for (i = at; i < used; i++) {
                   1127:                grid_get_cell1(gl, i, &gc);
                   1128:                if (width + gc.data.width > sx) {
1.80    ! nicm     1129:                        target->linedata[line].flags |= GRID_LINE_WRAPPED;
1.79      nicm     1130:
                   1131:                        line++;
                   1132:                        width = 0;
                   1133:                        xx = 0;
                   1134:                }
                   1135:                width += gc.data.width;
1.80    ! nicm     1136:                grid_set_cell(target, xx, line, &gc);
1.79      nicm     1137:                xx++;
                   1138:        }
                   1139:        if (flags & GRID_LINE_WRAPPED)
1.80    ! nicm     1140:                target->linedata[line].flags |= GRID_LINE_WRAPPED;
        !          1141:
        !          1142:        /* Move the remainder of the original line. */
        !          1143:        gl->cellsize = gl->cellused = at;
        !          1144:        gl->flags |= GRID_LINE_WRAPPED;
        !          1145:        memcpy(first, gl, sizeof *first);
        !          1146:        grid_reflow_dead(gl);
1.79      nicm     1147:
                   1148:        /* Adjust the cursor and scroll positions. */
                   1149:        if (yy <= *cy)
1.80    ! nicm     1150:                (*cy) += lines - 1;
1.79      nicm     1151:        if (yy <= gd->hscrolled)
1.80    ! nicm     1152:                gd->hscrolled += lines - 1;
1.23      nicm     1153:
1.79      nicm     1154:        /*
                   1155:         * If the original line had the wrapped flag and there is still space
                   1156:         * in the last new line, try to join with the next lines.
                   1157:         */
                   1158:        if (width < sx && (flags & GRID_LINE_WRAPPED))
1.80    ! nicm     1159:                grid_reflow_join(target, gd, sx, yy, width, cy, 1);
1.23      nicm     1160: }
                   1161:
1.79      nicm     1162: /* Reflow lines on grid to new width. */
                   1163: void
                   1164: grid_reflow(struct grid *gd, u_int sx, u_int *cursor)
1.23      nicm     1165: {
1.80    ! nicm     1166:        struct grid             *target;
1.79      nicm     1167:        struct grid_line        *gl;
                   1168:        struct grid_cell         gc;
                   1169:        u_int                    yy, cy, width, i, at, first;
                   1170:        struct timeval           start, tv;
1.23      nicm     1171:
1.79      nicm     1172:        gettimeofday(&start, NULL);
1.23      nicm     1173:
1.79      nicm     1174:        log_debug("%s: %u lines, new width %u", __func__, gd->hsize + gd->sy,
                   1175:            sx);
                   1176:        cy = gd->hsize + (*cursor);
1.23      nicm     1177:
1.79      nicm     1178:        /*
1.80    ! nicm     1179:         * Create a destination grid. This is just used as a container for the
        !          1180:         * line data and may not be fully valid.
        !          1181:         */
        !          1182:        target = grid_create(gd->sx, 0, 0);
        !          1183:
        !          1184:        /*
        !          1185:         * Loop over each source line.
1.79      nicm     1186:         */
                   1187:        for (yy = 0; yy < gd->hsize + gd->sy; yy++) {
                   1188:                gl = &gd->linedata[yy];
1.80    ! nicm     1189:                if (gl->flags & GRID_LINE_DEAD)
        !          1190:                        continue;
1.23      nicm     1191:
1.80    ! nicm     1192:                /*
        !          1193:                 * Work out the width of this line. first is the width of the
        !          1194:                 * first character, at is the point at which the available
        !          1195:                 * width is hit, and width is the full line width.
        !          1196:                 */
1.79      nicm     1197:                first = at = width = 0;
                   1198:                if (~gl->flags & GRID_LINE_EXTENDED) {
                   1199:                        first = 1;
                   1200:                        width = gl->cellused;
                   1201:                        if (width > sx)
                   1202:                                at = sx;
                   1203:                        else
                   1204:                                at = width;
                   1205:                } else {
                   1206:                        for (i = 0; i < gl->cellused; i++) {
                   1207:                                grid_get_cell1(gl, i, &gc);
                   1208:                                if (i == 0)
                   1209:                                        first = gc.data.width;
                   1210:                                if (at == 0 && width + gc.data.width > sx)
                   1211:                                        at = i;
                   1212:                                width += gc.data.width;
                   1213:                        }
                   1214:                }
1.23      nicm     1215:
1.79      nicm     1216:                /*
1.80    ! nicm     1217:                 * If the line is exactly right or the first character is wider
        !          1218:                 * than the targe width, just move it across unchanged.
1.79      nicm     1219:                 */
1.80    ! nicm     1220:                if (width == sx || first > sx) {
        !          1221:                        grid_reflow_move(target, gl);
1.79      nicm     1222:                        continue;
1.80    ! nicm     1223:                }
1.23      nicm     1224:
1.79      nicm     1225:                /*
                   1226:                 * If the line is too big, it needs to be split, whether or not
                   1227:                 * it was previously wrapped.
                   1228:                 */
                   1229:                if (width > sx) {
1.80    ! nicm     1230:                        grid_reflow_split(target, gd, sx, yy, at, &cy);
1.79      nicm     1231:                        continue;
                   1232:                }
1.23      nicm     1233:
1.79      nicm     1234:                /*
                   1235:                 * If the line was previously wrapped, join as much as possible
                   1236:                 * of the next line.
                   1237:                 */
                   1238:                if (gl->flags & GRID_LINE_WRAPPED)
1.80    ! nicm     1239:                        grid_reflow_join(target, gd, sx, yy, width, &cy, 0);
        !          1240:                else
        !          1241:                        grid_reflow_move(target, gl);
        !          1242:        }
1.55      nicm     1243:
1.80    ! nicm     1244:        /*
        !          1245:         * Replace the old grid with the new.
        !          1246:         */
        !          1247:        if (target->sy < gd->sy)
        !          1248:                grid_reflow_add(target, gd->sy - target->sy);
        !          1249:        gd->hsize = target->sy - gd->sy;
        !          1250:        free(gd->linedata);
        !          1251:        gd->linedata = target->linedata;
        !          1252:        free(target);
1.22      nicm     1253:
1.80    ! nicm     1254:        /*
        !          1255:         * Update scrolled and cursor positions.
        !          1256:         */
1.79      nicm     1257:        if (gd->hscrolled > gd->hsize)
                   1258:                gd->hscrolled = gd->hsize;
                   1259:        if (cy < gd->hsize)
                   1260:                *cursor = 0;
                   1261:        else
                   1262:                *cursor = cy - gd->hsize;
1.23      nicm     1263:
1.79      nicm     1264:        gettimeofday(&tv, NULL);
                   1265:        timersub(&tv, &start, &tv);
                   1266:        log_debug("%s: now %u lines (in %llu.%06u seconds)", __func__,
                   1267:            gd->hsize + gd->sy, (unsigned long long)tv.tv_sec,
                   1268:            (u_int)tv.tv_usec);
1.1       nicm     1269: }