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

1.48    ! nicm        1: /* $OpenBSD: grid.c,v 1.47 2015/11/12 14:50:57 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
                      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 = {
        !            40:        0, 0, 8, 8, { { ' ' }, 0, 1, 1 }
        !            41: };
        !            42: const struct grid_cell_entry grid_default_entry = {
        !            43:        0, { .data = { 0, 8, 8, ' ' } }
        !            44: };
1.1       nicm       45:
                     46: int    grid_check_y(struct grid *, u_int);
                     47:
1.48    ! nicm       48: void   grid_reflow_copy(struct grid_line *, u_int, struct grid_line *l,
        !            49:            u_int, u_int);
1.43      nicm       50: void   grid_reflow_join(struct grid *, u_int *, struct grid_line *, u_int);
                     51: void   grid_reflow_split(struct grid *, u_int *, struct grid_line *, u_int,
                     52:            u_int);
                     53: void   grid_reflow_move(struct grid *, u_int *, struct grid_line *);
                     54: size_t grid_string_cells_fg(const struct grid_cell *, int *);
                     55: size_t grid_string_cells_bg(const struct grid_cell *, int *);
                     56: void   grid_string_cells_code(const struct grid_cell *,
                     57:            const struct grid_cell *, char *, size_t, int);
                     58:
1.48    ! nicm       59: /* Copy default into a cell. */
        !            60: static void
        !            61: grid_clear_cell(struct grid *gd, u_int px, u_int py)
        !            62: {
        !            63:        gd->linedata[py].celldata[px] = grid_default_entry;
        !            64: }
        !            65:
1.43      nicm       66: /* Check grid y position. */
1.1       nicm       67: int
                     68: grid_check_y(struct grid *gd, u_int py)
                     69: {
                     70:        if ((py) >= (gd)->hsize + (gd)->sy) {
                     71:                log_debug("y out of range: %u", py);
                     72:                return (-1);
                     73:        }
                     74:        return (0);
                     75: }
1.23      nicm       76:
1.1       nicm       77: /* Create a new grid. */
                     78: struct grid *
                     79: grid_create(u_int sx, u_int sy, u_int hlimit)
                     80: {
                     81:        struct grid     *gd;
                     82:
                     83:        gd = xmalloc(sizeof *gd);
                     84:        gd->sx = sx;
                     85:        gd->sy = sy;
                     86:
1.7       nicm       87:        gd->flags = GRID_HISTORY;
                     88:
1.1       nicm       89:        gd->hsize = 0;
                     90:        gd->hlimit = hlimit;
                     91:
1.10      nicm       92:        gd->linedata = xcalloc(gd->sy, sizeof *gd->linedata);
1.1       nicm       93:
                     94:        return (gd);
                     95: }
                     96:
                     97: /* Destroy grid. */
                     98: void
                     99: grid_destroy(struct grid *gd)
                    100: {
1.10      nicm      101:        struct grid_line        *gl;
                    102:        u_int                    yy;
1.1       nicm      103:
                    104:        for (yy = 0; yy < gd->hsize + gd->sy; yy++) {
1.10      nicm      105:                gl = &gd->linedata[yy];
1.20      nicm      106:                free(gl->celldata);
1.48    ! nicm      107:                free(gl->extddata);
1.1       nicm      108:        }
                    109:
1.20      nicm      110:        free(gd->linedata);
1.1       nicm      111:
1.20      nicm      112:        free(gd);
1.1       nicm      113: }
                    114:
                    115: /* Compare grids. */
                    116: int
                    117: grid_compare(struct grid *ga, struct grid *gb)
                    118: {
1.10      nicm      119:        struct grid_line        *gla, *glb;
1.48    ! nicm      120:        struct grid_cell         gca, gcb;
1.1       nicm      121:        u_int                    xx, yy;
                    122:
1.33      nicm      123:        if (ga->sx != gb->sx || ga->sy != gb->sy)
1.1       nicm      124:                return (1);
                    125:
                    126:        for (yy = 0; yy < ga->sy; yy++) {
1.10      nicm      127:                gla = &ga->linedata[yy];
                    128:                glb = &gb->linedata[yy];
                    129:                if (gla->cellsize != glb->cellsize)
1.1       nicm      130:                        return (1);
1.48    ! nicm      131:                for (xx = 0; xx < gla->cellsize; xx++) {
        !           132:                        grid_get_cell(ga, xx, yy, &gca);
        !           133:                        grid_get_cell(gb, xx, yy, &gcb);
        !           134:                        if (memcmp(&gca, &gcb, sizeof (struct grid_cell)) != 0)
1.1       nicm      135:                                return (1);
                    136:                }
                    137:        }
                    138:
                    139:        return (0);
                    140: }
                    141:
1.15      nicm      142: /*
                    143:  * Collect lines from the history if at the limit. Free the top (oldest) 10%
                    144:  * and shift up.
                    145:  */
1.1       nicm      146: void
1.15      nicm      147: grid_collect_history(struct grid *gd)
1.1       nicm      148: {
                    149:        u_int   yy;
                    150:
1.15      nicm      151:        if (gd->hsize < gd->hlimit)
                    152:                return;
                    153:
                    154:        yy = gd->hlimit / 10;
                    155:        if (yy < 1)
                    156:                yy = 1;
                    157:
                    158:        grid_move_lines(gd, 0, yy, gd->hsize + gd->sy - yy);
                    159:        gd->hsize -= yy;
                    160: }
                    161:
1.17      nicm      162: /*
1.15      nicm      163:  * Scroll the entire visible screen, moving one line into the history. Just
                    164:  * allocate a new line at the bottom and move the history size indicator.
                    165:  */
                    166: void
                    167: grid_scroll_history(struct grid *gd)
                    168: {
                    169:        u_int   yy;
1.1       nicm      170:
                    171:        yy = gd->hsize + gd->sy;
1.41      nicm      172:        gd->linedata = xreallocarray(gd->linedata, yy + 1,
                    173:            sizeof *gd->linedata);
1.15      nicm      174:        memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
1.17      nicm      175:
1.15      nicm      176:        gd->hsize++;
                    177: }
1.1       nicm      178:
1.46      nicm      179: /* Clear the history. */
                    180: void
                    181: grid_clear_history(struct grid *gd)
                    182: {
                    183:        grid_clear_lines(gd, 0, gd->hsize);
                    184:        grid_move_lines(gd, 0, gd->hsize, gd->sy);
                    185:
                    186:        gd->hsize = 0;
                    187:        gd->linedata = xreallocarray(gd->linedata, gd->sy,
                    188:            sizeof *gd->linedata);
                    189: }
                    190:
1.15      nicm      191: /* Scroll a region up, moving the top line into the history. */
                    192: void
                    193: grid_scroll_history_region(struct grid *gd, u_int upper, u_int lower)
                    194: {
                    195:        struct grid_line        *gl_history, *gl_upper, *gl_lower;
                    196:        u_int                    yy;
                    197:
                    198:        /* Create a space for a new line. */
                    199:        yy = gd->hsize + gd->sy;
1.41      nicm      200:        gd->linedata = xreallocarray(gd->linedata, yy + 1,
                    201:            sizeof *gd->linedata);
1.1       nicm      202:
1.15      nicm      203:        /* Move the entire screen down to free a space for this line. */
                    204:        gl_history = &gd->linedata[gd->hsize];
                    205:        memmove(gl_history + 1, gl_history, gd->sy * sizeof *gl_history);
                    206:
                    207:        /* Adjust the region and find its start and end. */
                    208:        upper++;
                    209:        gl_upper = &gd->linedata[upper];
                    210:        lower++;
                    211:        gl_lower = &gd->linedata[lower];
                    212:
                    213:        /* Move the line into the history. */
                    214:        memcpy(gl_history, gl_upper, sizeof *gl_history);
                    215:
                    216:        /* Then move the region up and clear the bottom line. */
                    217:        memmove(gl_upper, gl_upper + 1, (lower - upper) * sizeof *gl_upper);
                    218:        memset(gl_lower, 0, sizeof *gl_lower);
                    219:
                    220:        /* Move the history offset down over the line. */
1.1       nicm      221:        gd->hsize++;
                    222: }
                    223:
                    224: /* Expand line to fit to cell. */
                    225: void
1.14      nicm      226: grid_expand_line(struct grid *gd, u_int py, u_int sx)
1.1       nicm      227: {
1.10      nicm      228:        struct grid_line        *gl;
1.14      nicm      229:        u_int                    xx;
1.1       nicm      230:
1.10      nicm      231:        gl = &gd->linedata[py];
1.14      nicm      232:        if (sx <= gl->cellsize)
1.1       nicm      233:                return;
                    234:
1.41      nicm      235:        gl->celldata = xreallocarray(gl->celldata, sx, sizeof *gl->celldata);
1.10      nicm      236:        for (xx = gl->cellsize; xx < sx; xx++)
1.48    ! nicm      237:                grid_clear_cell(gd, xx, py);
1.10      nicm      238:        gl->cellsize = sx;
1.1       nicm      239: }
                    240:
1.26      nicm      241: /* Peek at grid line. */
                    242: const struct grid_line *
                    243: grid_peek_line(struct grid *gd, u_int py)
                    244: {
                    245:        if (grid_check_y(gd, py) != 0)
                    246:                return (NULL);
                    247:        return (&gd->linedata[py]);
                    248: }
                    249:
1.1       nicm      250: /* Get cell for reading. */
1.48    ! nicm      251: void
        !           252: grid_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc)
1.1       nicm      253: {
1.48    ! nicm      254:        struct grid_line        *gl;
        !           255:        struct grid_cell_entry  *gce;
        !           256:
        !           257:        if (grid_check_y(gd, py) != 0 || px >= gd->linedata[py].cellsize) {
        !           258:                memcpy(gc, &grid_default_cell, sizeof *gc);
        !           259:                return;
        !           260:        }
1.1       nicm      261:
1.48    ! nicm      262:        gl = &gd->linedata[py];
        !           263:        gce = &gl->celldata[px];
1.1       nicm      264:
1.48    ! nicm      265:        if (gce->flags & GRID_FLAG_EXTENDED) {
        !           266:                if (gce->offset >= gl->extdsize)
        !           267:                        memcpy(gc, &grid_default_cell, sizeof *gc);
        !           268:                else
        !           269:                        memcpy(gc, &gl->extddata[gce->offset], sizeof *gc);
        !           270:                return;
        !           271:        }
1.1       nicm      272:
1.48    ! nicm      273:        gc->flags = gce->flags & ~GRID_FLAG_EXTENDED;
        !           274:        gc->attr = gce->data.attr;
        !           275:        gc->fg = gce->data.fg;
        !           276:        gc->bg = gce->data.bg;
        !           277:        utf8_set(&gc->data, gce->data.data);
1.1       nicm      278: }
                    279:
                    280: /* Set cell at relative position. */
                    281: void
1.31      nicm      282: grid_set_cell(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
1.1       nicm      283: {
1.48    ! nicm      284:        struct grid_line        *gl;
        !           285:        struct grid_cell_entry  *gce;
        !           286:        struct grid_cell        *gcp;
        !           287:
1.1       nicm      288:        if (grid_check_y(gd, py) != 0)
                    289:                return;
                    290:
                    291:        grid_expand_line(gd, py, px + 1);
1.48    ! nicm      292:
        !           293:        gl = &gd->linedata[py];
        !           294:        gce = &gl->celldata[px];
        !           295:
        !           296:        if ((gce->flags & GRID_FLAG_EXTENDED) || gc->data.size != 1 ||
        !           297:            gc->data.width != 1) {
        !           298:                if (~gce->flags & GRID_FLAG_EXTENDED) {
        !           299:                        gl->extddata = xreallocarray(gl->extddata,
        !           300:                            gl->extdsize + 1, sizeof *gl->extddata);
        !           301:                        gce->offset = gl->extdsize++;
        !           302:                        gce->flags = gc->flags | GRID_FLAG_EXTENDED;
        !           303:                }
        !           304:
        !           305:                if (gce->offset >= gl->extdsize)
        !           306:                        fatalx("offset too big");
        !           307:                gcp = &gl->extddata[gce->offset];
        !           308:                memcpy(gcp, gc, sizeof *gcp);
        !           309:                return;
        !           310:        }
        !           311:
        !           312:        gce->flags = gc->flags & ~GRID_FLAG_EXTENDED;
        !           313:        gce->data.attr = gc->attr;
        !           314:        gce->data.fg = gc->fg;
        !           315:        gce->data.bg = gc->bg;
        !           316:        gce->data.data = gc->data.data[0];
1.1       nicm      317: }
                    318:
1.14      nicm      319: /* Clear area. */
1.1       nicm      320: void
                    321: grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny)
                    322: {
                    323:        u_int   xx, yy;
                    324:
                    325:        if (nx == 0 || ny == 0)
                    326:                return;
                    327:
                    328:        if (px == 0 && nx == gd->sx) {
                    329:                grid_clear_lines(gd, py, ny);
                    330:                return;
                    331:        }
                    332:
                    333:        if (grid_check_y(gd, py) != 0)
                    334:                return;
                    335:        if (grid_check_y(gd, py + ny - 1) != 0)
                    336:                return;
                    337:
                    338:        for (yy = py; yy < py + ny; yy++) {
1.14      nicm      339:                if (px >= gd->linedata[yy].cellsize)
                    340:                        continue;
                    341:                if (px + nx >= gd->linedata[yy].cellsize) {
                    342:                        gd->linedata[yy].cellsize = px;
                    343:                        continue;
                    344:                }
1.1       nicm      345:                for (xx = px; xx < px + nx; xx++) {
1.10      nicm      346:                        if (xx >= gd->linedata[yy].cellsize)
1.1       nicm      347:                                break;
1.48    ! nicm      348:                        grid_clear_cell(gd, xx, yy);
1.1       nicm      349:                }
                    350:        }
                    351: }
                    352:
                    353: /* Clear lines. This just frees and truncates the lines. */
                    354: void
                    355: grid_clear_lines(struct grid *gd, u_int py, u_int ny)
                    356: {
1.10      nicm      357:        struct grid_line        *gl;
                    358:        u_int                    yy;
1.1       nicm      359:
                    360:        if (ny == 0)
                    361:                return;
                    362:
                    363:        if (grid_check_y(gd, py) != 0)
                    364:                return;
                    365:        if (grid_check_y(gd, py + ny - 1) != 0)
                    366:                return;
                    367:
                    368:        for (yy = py; yy < py + ny; yy++) {
1.10      nicm      369:                gl = &gd->linedata[yy];
1.20      nicm      370:                free(gl->celldata);
1.10      nicm      371:                memset(gl, 0, sizeof *gl);
1.48    ! nicm      372:
        !           373:                free(gl->extddata);
        !           374:                gl->extddata = NULL;
        !           375:                gl->extdsize = 0;
1.1       nicm      376:        }
                    377: }
                    378:
                    379: /* Move a group of lines. */
                    380: void
                    381: grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny)
                    382: {
                    383:        u_int   yy;
                    384:
                    385:        if (ny == 0 || py == dy)
                    386:                return;
                    387:
                    388:        if (grid_check_y(gd, py) != 0)
                    389:                return;
                    390:        if (grid_check_y(gd, py + ny - 1) != 0)
                    391:                return;
                    392:        if (grid_check_y(gd, dy) != 0)
                    393:                return;
                    394:        if (grid_check_y(gd, dy + ny - 1) != 0)
                    395:                return;
                    396:
                    397:        /* Free any lines which are being replaced. */
                    398:        for (yy = dy; yy < dy + ny; yy++) {
                    399:                if (yy >= py && yy < py + ny)
                    400:                        continue;
                    401:                grid_clear_lines(gd, yy, 1);
                    402:        }
                    403:
1.46      nicm      404:        memmove(&gd->linedata[dy], &gd->linedata[py],
                    405:            ny * (sizeof *gd->linedata));
1.1       nicm      406:
                    407:        /* Wipe any lines that have been moved (without freeing them). */
                    408:        for (yy = py; yy < py + ny; yy++) {
                    409:                if (yy >= dy && yy < dy + ny)
                    410:                        continue;
1.10      nicm      411:                memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
1.1       nicm      412:        }
                    413: }
                    414:
                    415: /* Move a group of cells. */
                    416: void
                    417: grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx)
                    418: {
1.10      nicm      419:        struct grid_line        *gl;
                    420:        u_int                    xx;
1.1       nicm      421:
                    422:        if (nx == 0 || px == dx)
                    423:                return;
                    424:
                    425:        if (grid_check_y(gd, py) != 0)
                    426:                return;
1.10      nicm      427:        gl = &gd->linedata[py];
1.1       nicm      428:
                    429:        grid_expand_line(gd, py, px + nx);
                    430:        grid_expand_line(gd, py, dx + nx);
1.46      nicm      431:        memmove(&gl->celldata[dx], &gl->celldata[px],
                    432:            nx * sizeof *gl->celldata);
1.1       nicm      433:
                    434:        /* Wipe any cells that have been moved. */
                    435:        for (xx = px; xx < px + nx; xx++) {
                    436:                if (xx >= dx && xx < dx + nx)
                    437:                        continue;
1.48    ! nicm      438:                grid_clear_cell(gd, xx, py);
1.1       nicm      439:        }
1.3       nicm      440: }
                    441:
1.24      nicm      442: /* Get ANSI foreground sequence. */
                    443: size_t
                    444: grid_string_cells_fg(const struct grid_cell *gc, int *values)
                    445: {
                    446:        size_t  n;
                    447:
                    448:        n = 0;
                    449:        if (gc->flags & GRID_FLAG_FG256) {
                    450:                values[n++] = 38;
                    451:                values[n++] = 5;
                    452:                values[n++] = gc->fg;
                    453:        } else {
                    454:                switch (gc->fg) {
1.45      nicm      455:                case 0:
                    456:                case 1:
                    457:                case 2:
                    458:                case 3:
                    459:                case 4:
                    460:                case 5:
                    461:                case 6:
                    462:                case 7:
                    463:                        values[n++] = gc->fg + 30;
                    464:                        break;
                    465:                case 8:
                    466:                        values[n++] = 39;
                    467:                        break;
                    468:                case 90:
                    469:                case 91:
                    470:                case 92:
                    471:                case 93:
                    472:                case 94:
                    473:                case 95:
                    474:                case 96:
                    475:                case 97:
                    476:                        values[n++] = gc->fg;
                    477:                        break;
1.24      nicm      478:                }
                    479:        }
                    480:        return (n);
                    481: }
                    482:
                    483: /* Get ANSI background sequence. */
                    484: size_t
                    485: grid_string_cells_bg(const struct grid_cell *gc, int *values)
                    486: {
                    487:        size_t  n;
                    488:
                    489:        n = 0;
                    490:        if (gc->flags & GRID_FLAG_BG256) {
                    491:                values[n++] = 48;
                    492:                values[n++] = 5;
                    493:                values[n++] = gc->bg;
                    494:        } else {
                    495:                switch (gc->bg) {
                    496:                case 0:
                    497:                case 1:
                    498:                case 2:
                    499:                case 3:
                    500:                case 4:
                    501:                case 5:
                    502:                case 6:
                    503:                case 7:
                    504:                        values[n++] = gc->bg + 40;
                    505:                        break;
                    506:                case 8:
                    507:                        values[n++] = 49;
                    508:                        break;
                    509:                case 100:
                    510:                case 101:
                    511:                case 102:
                    512:                case 103:
                    513:                case 104:
                    514:                        case 105:
                    515:                case 106:
                    516:                case 107:
                    517:                        values[n++] = gc->bg - 10;
                    518:                        break;
                    519:                }
                    520:        }
                    521:        return (n);
                    522: }
                    523:
                    524: /*
                    525:  * Returns ANSI code to set particular attributes (colour, bold and so on)
                    526:  * given a current state. The output buffer must be able to hold at least 57
                    527:  * bytes.
                    528:  */
                    529: void
                    530: grid_string_cells_code(const struct grid_cell *lastgc,
1.26      nicm      531:     const struct grid_cell *gc, char *buf, size_t len, int escape_c0)
1.24      nicm      532: {
                    533:        int     oldc[16], newc[16], s[32];
                    534:        size_t  noldc, nnewc, n, i;
                    535:        u_int   attr = gc->attr;
                    536:        u_int   lastattr = lastgc->attr;
                    537:        char    tmp[64];
                    538:
                    539:        struct {
                    540:                u_int   mask;
                    541:                u_int   code;
                    542:        } attrs[] = {
                    543:                { GRID_ATTR_BRIGHT, 1 },
                    544:                { GRID_ATTR_DIM, 2 },
                    545:                { GRID_ATTR_ITALICS, 3 },
                    546:                { GRID_ATTR_UNDERSCORE, 4 },
                    547:                { GRID_ATTR_BLINK, 5 },
                    548:                { GRID_ATTR_REVERSE, 7 },
                    549:                { GRID_ATTR_HIDDEN, 8 }
                    550:        };
                    551:        n = 0;
                    552:
                    553:        /* If any attribute is removed, begin with 0. */
                    554:        for (i = 0; i < nitems(attrs); i++) {
                    555:                if (!(attr & attrs[i].mask) && (lastattr & attrs[i].mask)) {
                    556:                        s[n++] = 0;
1.25      nicm      557:                        lastattr &= GRID_ATTR_CHARSET;
1.24      nicm      558:                        break;
                    559:                }
                    560:        }
                    561:        /* For each attribute that is newly set, add its code. */
                    562:        for (i = 0; i < nitems(attrs); i++) {
                    563:                if ((attr & attrs[i].mask) && !(lastattr & attrs[i].mask))
                    564:                        s[n++] = attrs[i].code;
                    565:        }
                    566:
1.39      nicm      567:        /* If the foreground colour changed, append its parameters. */
1.24      nicm      568:        nnewc = grid_string_cells_fg(gc, newc);
                    569:        noldc = grid_string_cells_fg(lastgc, oldc);
1.39      nicm      570:        if (nnewc != noldc || memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0) {
1.24      nicm      571:                for (i = 0; i < nnewc; i++)
                    572:                        s[n++] = newc[i];
                    573:        }
                    574:
1.39      nicm      575:        /* If the background colour changed, append its parameters. */
1.24      nicm      576:        nnewc = grid_string_cells_bg(gc, newc);
                    577:        noldc = grid_string_cells_bg(lastgc, oldc);
1.39      nicm      578:        if (nnewc != noldc || memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0) {
1.24      nicm      579:                for (i = 0; i < nnewc; i++)
                    580:                        s[n++] = newc[i];
                    581:        }
                    582:
                    583:        /* If there are any parameters, append an SGR code. */
                    584:        *buf = '\0';
                    585:        if (n > 0) {
1.26      nicm      586:                if (escape_c0)
                    587:                        strlcat(buf, "\\033[", len);
                    588:                else
                    589:                        strlcat(buf, "\033[", len);
1.24      nicm      590:                for (i = 0; i < n; i++) {
                    591:                        if (i + 1 < n)
                    592:                                xsnprintf(tmp, sizeof tmp, "%d;", s[i]);
                    593:                        else
                    594:                                xsnprintf(tmp, sizeof tmp, "%d", s[i]);
                    595:                        strlcat(buf, tmp, len);
                    596:                }
                    597:                strlcat(buf, "m", len);
                    598:        }
                    599:
                    600:        /* Append shift in/shift out if needed. */
1.26      nicm      601:        if ((attr & GRID_ATTR_CHARSET) && !(lastattr & GRID_ATTR_CHARSET)) {
                    602:                if (escape_c0)
                    603:                        strlcat(buf, "\\016", len);  /* SO */
                    604:                else
                    605:                        strlcat(buf, "\016", len);  /* SO */
                    606:        }
                    607:        if (!(attr & GRID_ATTR_CHARSET) && (lastattr & GRID_ATTR_CHARSET)) {
                    608:                if (escape_c0)
                    609:                        strlcat(buf, "\\017", len);  /* SI */
                    610:                else
                    611:                        strlcat(buf, "\017", len);  /* SI */
                    612:        }
1.24      nicm      613: }
                    614:
1.3       nicm      615: /* Convert cells into a string. */
                    616: char *
1.24      nicm      617: grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx,
1.28      nicm      618:     struct grid_cell **lastgc, int with_codes, int escape_c0, int trim)
1.3       nicm      619: {
1.48    ! nicm      620:        struct grid_cell         gc;
1.24      nicm      621:        static struct grid_cell  lastgc1;
1.38      nicm      622:        const char              *data;
1.24      nicm      623:        char                    *buf, code[128];
1.26      nicm      624:        size_t                   len, off, size, codelen;
1.16      nicm      625:        u_int                    xx;
1.30      nicm      626:        const struct grid_line  *gl;
1.3       nicm      627:
1.29      nicm      628:        if (lastgc != NULL && *lastgc == NULL) {
1.24      nicm      629:                memcpy(&lastgc1, &grid_default_cell, sizeof lastgc1);
                    630:                *lastgc = &lastgc1;
                    631:        }
                    632:
1.3       nicm      633:        len = 128;
                    634:        buf = xmalloc(len);
                    635:        off = 0;
                    636:
1.30      nicm      637:        gl = grid_peek_line(gd, py);
1.3       nicm      638:        for (xx = px; xx < px + nx; xx++) {
1.30      nicm      639:                if (gl == NULL || xx >= gl->cellsize)
                    640:                        break;
1.48    ! nicm      641:                grid_get_cell(gd, xx, py, &gc);
        !           642:                if (gc.flags & GRID_FLAG_PADDING)
1.3       nicm      643:                        continue;
                    644:
1.24      nicm      645:                if (with_codes) {
1.48    ! nicm      646:                        grid_string_cells_code(*lastgc, &gc, code, sizeof code,
1.26      nicm      647:                            escape_c0);
1.24      nicm      648:                        codelen = strlen(code);
1.48    ! nicm      649:                        memcpy(*lastgc, &gc, sizeof **lastgc);
1.24      nicm      650:                } else
                    651:                        codelen = 0;
                    652:
1.48    ! nicm      653:                data = gc.data.data;
        !           654:                size = gc.data.size;
1.26      nicm      655:                if (escape_c0 && size == 1 && *data == '\\') {
1.27      nicm      656:                        data = "\\\\";
1.26      nicm      657:                        size = 2;
                    658:                }
                    659:
                    660:                while (len < off + size + codelen + 1) {
1.41      nicm      661:                        buf = xreallocarray(buf, 2, len);
1.21      nicm      662:                        len *= 2;
                    663:                }
1.3       nicm      664:
1.24      nicm      665:                if (codelen != 0) {
                    666:                        memcpy(buf + off, code, codelen);
                    667:                        off += codelen;
                    668:                }
1.26      nicm      669:                memcpy(buf + off, data, size);
                    670:                off += size;
1.3       nicm      671:        }
1.17      nicm      672:
1.37      nicm      673:        if (trim) {
1.28      nicm      674:                while (off > 0 && buf[off - 1] == ' ')
                    675:                        off--;
1.32      nicm      676:        }
1.3       nicm      677:        buf[off] = '\0';
1.26      nicm      678:
1.3       nicm      679:        return (buf);
1.7       nicm      680: }
                    681:
1.17      nicm      682: /*
1.7       nicm      683:  * Duplicate a set of lines between two grids. If there aren't enough lines in
                    684:  * either source or destination, the number of lines is limited to the number
                    685:  * available.
                    686:  */
                    687: void
1.31      nicm      688: grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy,
                    689:     u_int ny)
1.7       nicm      690: {
1.10      nicm      691:        struct grid_line        *dstl, *srcl;
                    692:        u_int                    yy;
1.7       nicm      693:
                    694:        if (dy + ny > dst->hsize + dst->sy)
                    695:                ny = dst->hsize + dst->sy - dy;
                    696:        if (sy + ny > src->hsize + src->sy)
                    697:                ny = src->hsize + src->sy - sy;
                    698:        grid_clear_lines(dst, dy, ny);
                    699:
                    700:        for (yy = 0; yy < ny; yy++) {
1.11      nicm      701:                srcl = &src->linedata[sy];
                    702:                dstl = &dst->linedata[dy];
1.10      nicm      703:
                    704:                memcpy(dstl, srcl, sizeof *dstl);
                    705:                if (srcl->cellsize != 0) {
1.42      deraadt   706:                        dstl->celldata = xreallocarray(NULL,
1.10      nicm      707:                            srcl->cellsize, sizeof *dstl->celldata);
                    708:                        memcpy(dstl->celldata, srcl->celldata,
                    709:                            srcl->cellsize * sizeof *dstl->celldata);
1.44      nicm      710:                } else
                    711:                        dstl->celldata = NULL;
1.7       nicm      712:
1.48    ! nicm      713:                if (srcl->extdsize != 0) {
        !           714:                        dstl->extdsize = srcl->extdsize;
        !           715:                        dstl->extddata = xreallocarray(NULL, dstl->extdsize,
        !           716:                            sizeof *dstl->extddata);
        !           717:                        memcpy(dstl->extddata, srcl->extddata, dstl->extdsize *
        !           718:                            sizeof *dstl->extddata);
        !           719:                }
        !           720:
1.10      nicm      721:                sy++;
                    722:                dy++;
1.7       nicm      723:        }
1.22      nicm      724: }
                    725:
1.48    ! nicm      726: /* Copy a section of a line. */
        !           727: void
        !           728: grid_reflow_copy(struct grid_line *dst_gl, u_int to, struct grid_line *src_gl,
        !           729:     u_int from, u_int to_copy)
        !           730: {
        !           731:        struct grid_cell_entry  *gce;
        !           732:        u_int                    i, was;
        !           733:
        !           734:        memcpy(&dst_gl->celldata[to], &src_gl->celldata[from],
        !           735:            to_copy * sizeof *dst_gl->celldata);
        !           736:
        !           737:        for (i = to; i < to + to_copy; i++) {
        !           738:                gce = &dst_gl->celldata[i];
        !           739:                if (~gce->flags & GRID_FLAG_EXTENDED)
        !           740:                        continue;
        !           741:                was = gce->offset;
        !           742:
        !           743:                dst_gl->extddata = xreallocarray(dst_gl->extddata,
        !           744:                    dst_gl->extdsize + 1, sizeof *dst_gl->extddata);
        !           745:                gce->offset = dst_gl->extdsize++;
        !           746:                memcpy(&dst_gl->extddata[gce->offset], &src_gl->extddata[was],
        !           747:                    sizeof *dst_gl->extddata);
        !           748:        }
        !           749: }
        !           750:
1.23      nicm      751: /* Join line data. */
                    752: void
                    753: grid_reflow_join(struct grid *dst, u_int *py, struct grid_line *src_gl,
                    754:     u_int new_x)
                    755: {
                    756:        struct grid_line        *dst_gl = &dst->linedata[(*py) - 1];
                    757:        u_int                    left, to_copy, ox, nx;
                    758:
                    759:        /* How much is left on the old line? */
                    760:        left = new_x - dst_gl->cellsize;
                    761:
                    762:        /* Work out how much to append. */
                    763:        to_copy = src_gl->cellsize;
                    764:        if (to_copy > left)
                    765:                to_copy = left;
                    766:        ox = dst_gl->cellsize;
                    767:        nx = ox + to_copy;
                    768:
                    769:        /* Resize the destination line. */
1.41      nicm      770:        dst_gl->celldata = xreallocarray(dst_gl->celldata, nx,
1.23      nicm      771:            sizeof *dst_gl->celldata);
                    772:        dst_gl->cellsize = nx;
                    773:
                    774:        /* Append as much as possible. */
1.48    ! nicm      775:        grid_reflow_copy(dst_gl, ox, src_gl, 0, to_copy);
1.23      nicm      776:
                    777:        /* If there is any left in the source, split it. */
                    778:        if (src_gl->cellsize > to_copy) {
                    779:                dst_gl->flags |= GRID_LINE_WRAPPED;
                    780:
                    781:                src_gl->cellsize -= to_copy;
                    782:                grid_reflow_split(dst, py, src_gl, new_x, to_copy);
                    783:        }
                    784: }
                    785:
                    786: /* Split line data. */
                    787: void
                    788: grid_reflow_split(struct grid *dst, u_int *py, struct grid_line *src_gl,
                    789:     u_int new_x, u_int offset)
                    790: {
                    791:        struct grid_line        *dst_gl = NULL;
                    792:        u_int                    to_copy;
                    793:
                    794:        /* Loop and copy sections of the source line. */
                    795:        while (src_gl->cellsize > 0) {
                    796:                /* Create new line. */
                    797:                if (*py >= dst->hsize + dst->sy)
                    798:                        grid_scroll_history(dst);
                    799:                dst_gl = &dst->linedata[*py];
                    800:                (*py)++;
                    801:
                    802:                /* How much should we copy? */
                    803:                to_copy = new_x;
                    804:                if (to_copy > src_gl->cellsize)
                    805:                        to_copy = src_gl->cellsize;
                    806:
                    807:                /* Expand destination line. */
1.41      nicm      808:                dst_gl->celldata = xreallocarray(NULL, to_copy,
1.40      nicm      809:                    sizeof *dst_gl->celldata);
1.23      nicm      810:                dst_gl->cellsize = to_copy;
                    811:                dst_gl->flags |= GRID_LINE_WRAPPED;
                    812:
                    813:                /* Copy the data. */
1.48    ! nicm      814:                grid_reflow_copy(dst_gl, 0, src_gl, offset, to_copy);
1.23      nicm      815:
                    816:                /* Move offset and reduce old line size. */
                    817:                offset += to_copy;
                    818:                src_gl->cellsize -= to_copy;
                    819:        }
                    820:
                    821:        /* Last line is not wrapped. */
                    822:        if (dst_gl != NULL)
                    823:                dst_gl->flags &= ~GRID_LINE_WRAPPED;
                    824: }
                    825:
                    826: /* Move line data. */
                    827: void
                    828: grid_reflow_move(struct grid *dst, u_int *py, struct grid_line *src_gl)
                    829: {
                    830:        struct grid_line        *dst_gl;
                    831:
                    832:        /* Create new line. */
                    833:        if (*py >= dst->hsize + dst->sy)
                    834:                grid_scroll_history(dst);
                    835:        dst_gl = &dst->linedata[*py];
                    836:        (*py)++;
                    837:
                    838:        /* Copy the old line. */
                    839:        memcpy(dst_gl, src_gl, sizeof *dst_gl);
                    840:        dst_gl->flags &= ~GRID_LINE_WRAPPED;
                    841:
                    842:        /* Clear old line. */
                    843:        src_gl->celldata = NULL;
1.48    ! nicm      844:        src_gl->extddata = NULL;
1.23      nicm      845: }
                    846:
1.22      nicm      847: /*
1.23      nicm      848:  * Reflow lines from src grid into dst grid of width new_x. Returns number of
                    849:  * lines fewer in the visible area. The source grid is destroyed.
1.22      nicm      850:  */
                    851: u_int
1.23      nicm      852: grid_reflow(struct grid *dst, struct grid *src, u_int new_x)
1.22      nicm      853: {
1.23      nicm      854:        u_int                    py, sy, line;
1.22      nicm      855:        int                      previous_wrapped;
1.23      nicm      856:        struct grid_line        *src_gl;
                    857:
                    858:        py = 0;
                    859:        sy = src->sy;
1.22      nicm      860:
1.23      nicm      861:        previous_wrapped = 0;
                    862:        for (line = 0; line < sy + src->hsize; line++) {
                    863:                src_gl = src->linedata + line;
1.22      nicm      864:                if (!previous_wrapped) {
1.23      nicm      865:                        /* Wasn't wrapped. If smaller, move to destination. */
                    866:                        if (src_gl->cellsize <= new_x)
                    867:                                grid_reflow_move(dst, &py, src_gl);
                    868:                        else
                    869:                                grid_reflow_split(dst, &py, src_gl, new_x, 0);
                    870:                } else {
                    871:                        /* Previous was wrapped. Try to join. */
                    872:                        grid_reflow_join(dst, &py, src_gl, new_x);
1.22      nicm      873:                }
1.48    ! nicm      874:                previous_wrapped = (src_gl->flags & GRID_LINE_WRAPPED);
1.22      nicm      875:        }
                    876:
1.23      nicm      877:        grid_destroy(src);
                    878:
                    879:        if (py > sy)
1.22      nicm      880:                return (0);
1.23      nicm      881:        return (sy - py);
1.1       nicm      882: }