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

1.50    ! nicm        1: /* $OpenBSD: grid.c,v 1.49 2015/11/22 19:42:57 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 = {
                     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.49      nicm      371:                free(gl->extddata);
1.10      nicm      372:                memset(gl, 0, sizeof *gl);
1.1       nicm      373:        }
                    374: }
                    375:
                    376: /* Move a group of lines. */
                    377: void
                    378: grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny)
                    379: {
                    380:        u_int   yy;
                    381:
                    382:        if (ny == 0 || py == dy)
                    383:                return;
                    384:
                    385:        if (grid_check_y(gd, py) != 0)
                    386:                return;
                    387:        if (grid_check_y(gd, py + ny - 1) != 0)
                    388:                return;
                    389:        if (grid_check_y(gd, dy) != 0)
                    390:                return;
                    391:        if (grid_check_y(gd, dy + ny - 1) != 0)
                    392:                return;
                    393:
                    394:        /* Free any lines which are being replaced. */
                    395:        for (yy = dy; yy < dy + ny; yy++) {
                    396:                if (yy >= py && yy < py + ny)
                    397:                        continue;
                    398:                grid_clear_lines(gd, yy, 1);
                    399:        }
                    400:
1.46      nicm      401:        memmove(&gd->linedata[dy], &gd->linedata[py],
                    402:            ny * (sizeof *gd->linedata));
1.1       nicm      403:
                    404:        /* Wipe any lines that have been moved (without freeing them). */
                    405:        for (yy = py; yy < py + ny; yy++) {
                    406:                if (yy >= dy && yy < dy + ny)
                    407:                        continue;
1.10      nicm      408:                memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
1.1       nicm      409:        }
                    410: }
                    411:
                    412: /* Move a group of cells. */
                    413: void
                    414: grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx)
                    415: {
1.10      nicm      416:        struct grid_line        *gl;
                    417:        u_int                    xx;
1.1       nicm      418:
                    419:        if (nx == 0 || px == dx)
                    420:                return;
                    421:
                    422:        if (grid_check_y(gd, py) != 0)
                    423:                return;
1.10      nicm      424:        gl = &gd->linedata[py];
1.1       nicm      425:
                    426:        grid_expand_line(gd, py, px + nx);
                    427:        grid_expand_line(gd, py, dx + nx);
1.46      nicm      428:        memmove(&gl->celldata[dx], &gl->celldata[px],
                    429:            nx * sizeof *gl->celldata);
1.1       nicm      430:
                    431:        /* Wipe any cells that have been moved. */
                    432:        for (xx = px; xx < px + nx; xx++) {
                    433:                if (xx >= dx && xx < dx + nx)
                    434:                        continue;
1.48      nicm      435:                grid_clear_cell(gd, xx, py);
1.1       nicm      436:        }
1.3       nicm      437: }
                    438:
1.24      nicm      439: /* Get ANSI foreground sequence. */
                    440: size_t
                    441: grid_string_cells_fg(const struct grid_cell *gc, int *values)
                    442: {
                    443:        size_t  n;
                    444:
                    445:        n = 0;
                    446:        if (gc->flags & GRID_FLAG_FG256) {
                    447:                values[n++] = 38;
                    448:                values[n++] = 5;
                    449:                values[n++] = gc->fg;
                    450:        } else {
                    451:                switch (gc->fg) {
1.45      nicm      452:                case 0:
                    453:                case 1:
                    454:                case 2:
                    455:                case 3:
                    456:                case 4:
                    457:                case 5:
                    458:                case 6:
                    459:                case 7:
                    460:                        values[n++] = gc->fg + 30;
                    461:                        break;
                    462:                case 8:
                    463:                        values[n++] = 39;
                    464:                        break;
                    465:                case 90:
                    466:                case 91:
                    467:                case 92:
                    468:                case 93:
                    469:                case 94:
                    470:                case 95:
                    471:                case 96:
                    472:                case 97:
                    473:                        values[n++] = gc->fg;
                    474:                        break;
1.24      nicm      475:                }
                    476:        }
                    477:        return (n);
                    478: }
                    479:
                    480: /* Get ANSI background sequence. */
                    481: size_t
                    482: grid_string_cells_bg(const struct grid_cell *gc, int *values)
                    483: {
                    484:        size_t  n;
                    485:
                    486:        n = 0;
                    487:        if (gc->flags & GRID_FLAG_BG256) {
                    488:                values[n++] = 48;
                    489:                values[n++] = 5;
                    490:                values[n++] = gc->bg;
                    491:        } else {
                    492:                switch (gc->bg) {
                    493:                case 0:
                    494:                case 1:
                    495:                case 2:
                    496:                case 3:
                    497:                case 4:
                    498:                case 5:
                    499:                case 6:
                    500:                case 7:
                    501:                        values[n++] = gc->bg + 40;
                    502:                        break;
                    503:                case 8:
                    504:                        values[n++] = 49;
                    505:                        break;
                    506:                case 100:
                    507:                case 101:
                    508:                case 102:
                    509:                case 103:
                    510:                case 104:
                    511:                        case 105:
                    512:                case 106:
                    513:                case 107:
                    514:                        values[n++] = gc->bg - 10;
                    515:                        break;
                    516:                }
                    517:        }
                    518:        return (n);
                    519: }
                    520:
                    521: /*
                    522:  * Returns ANSI code to set particular attributes (colour, bold and so on)
                    523:  * given a current state. The output buffer must be able to hold at least 57
                    524:  * bytes.
                    525:  */
                    526: void
                    527: grid_string_cells_code(const struct grid_cell *lastgc,
1.26      nicm      528:     const struct grid_cell *gc, char *buf, size_t len, int escape_c0)
1.24      nicm      529: {
                    530:        int     oldc[16], newc[16], s[32];
                    531:        size_t  noldc, nnewc, n, i;
                    532:        u_int   attr = gc->attr;
                    533:        u_int   lastattr = lastgc->attr;
                    534:        char    tmp[64];
                    535:
                    536:        struct {
                    537:                u_int   mask;
                    538:                u_int   code;
                    539:        } attrs[] = {
                    540:                { GRID_ATTR_BRIGHT, 1 },
                    541:                { GRID_ATTR_DIM, 2 },
                    542:                { GRID_ATTR_ITALICS, 3 },
                    543:                { GRID_ATTR_UNDERSCORE, 4 },
                    544:                { GRID_ATTR_BLINK, 5 },
                    545:                { GRID_ATTR_REVERSE, 7 },
                    546:                { GRID_ATTR_HIDDEN, 8 }
                    547:        };
                    548:        n = 0;
                    549:
                    550:        /* If any attribute is removed, begin with 0. */
                    551:        for (i = 0; i < nitems(attrs); i++) {
                    552:                if (!(attr & attrs[i].mask) && (lastattr & attrs[i].mask)) {
                    553:                        s[n++] = 0;
1.25      nicm      554:                        lastattr &= GRID_ATTR_CHARSET;
1.24      nicm      555:                        break;
                    556:                }
                    557:        }
                    558:        /* For each attribute that is newly set, add its code. */
                    559:        for (i = 0; i < nitems(attrs); i++) {
                    560:                if ((attr & attrs[i].mask) && !(lastattr & attrs[i].mask))
                    561:                        s[n++] = attrs[i].code;
                    562:        }
                    563:
1.39      nicm      564:        /* If the foreground colour changed, append its parameters. */
1.24      nicm      565:        nnewc = grid_string_cells_fg(gc, newc);
                    566:        noldc = grid_string_cells_fg(lastgc, oldc);
1.39      nicm      567:        if (nnewc != noldc || memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0) {
1.24      nicm      568:                for (i = 0; i < nnewc; i++)
                    569:                        s[n++] = newc[i];
                    570:        }
                    571:
1.39      nicm      572:        /* If the background colour changed, append its parameters. */
1.24      nicm      573:        nnewc = grid_string_cells_bg(gc, newc);
                    574:        noldc = grid_string_cells_bg(lastgc, oldc);
1.39      nicm      575:        if (nnewc != noldc || memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0) {
1.24      nicm      576:                for (i = 0; i < nnewc; i++)
                    577:                        s[n++] = newc[i];
                    578:        }
                    579:
                    580:        /* If there are any parameters, append an SGR code. */
                    581:        *buf = '\0';
                    582:        if (n > 0) {
1.26      nicm      583:                if (escape_c0)
                    584:                        strlcat(buf, "\\033[", len);
                    585:                else
                    586:                        strlcat(buf, "\033[", len);
1.24      nicm      587:                for (i = 0; i < n; i++) {
                    588:                        if (i + 1 < n)
                    589:                                xsnprintf(tmp, sizeof tmp, "%d;", s[i]);
                    590:                        else
                    591:                                xsnprintf(tmp, sizeof tmp, "%d", s[i]);
                    592:                        strlcat(buf, tmp, len);
                    593:                }
                    594:                strlcat(buf, "m", len);
                    595:        }
                    596:
                    597:        /* Append shift in/shift out if needed. */
1.26      nicm      598:        if ((attr & GRID_ATTR_CHARSET) && !(lastattr & GRID_ATTR_CHARSET)) {
                    599:                if (escape_c0)
                    600:                        strlcat(buf, "\\016", len);  /* SO */
                    601:                else
                    602:                        strlcat(buf, "\016", len);  /* SO */
                    603:        }
                    604:        if (!(attr & GRID_ATTR_CHARSET) && (lastattr & GRID_ATTR_CHARSET)) {
                    605:                if (escape_c0)
                    606:                        strlcat(buf, "\\017", len);  /* SI */
                    607:                else
                    608:                        strlcat(buf, "\017", len);  /* SI */
                    609:        }
1.24      nicm      610: }
                    611:
1.3       nicm      612: /* Convert cells into a string. */
                    613: char *
1.24      nicm      614: grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx,
1.28      nicm      615:     struct grid_cell **lastgc, int with_codes, int escape_c0, int trim)
1.3       nicm      616: {
1.48      nicm      617:        struct grid_cell         gc;
1.24      nicm      618:        static struct grid_cell  lastgc1;
1.38      nicm      619:        const char              *data;
1.24      nicm      620:        char                    *buf, code[128];
1.26      nicm      621:        size_t                   len, off, size, codelen;
1.16      nicm      622:        u_int                    xx;
1.30      nicm      623:        const struct grid_line  *gl;
1.3       nicm      624:
1.29      nicm      625:        if (lastgc != NULL && *lastgc == NULL) {
1.24      nicm      626:                memcpy(&lastgc1, &grid_default_cell, sizeof lastgc1);
                    627:                *lastgc = &lastgc1;
                    628:        }
                    629:
1.3       nicm      630:        len = 128;
                    631:        buf = xmalloc(len);
                    632:        off = 0;
                    633:
1.30      nicm      634:        gl = grid_peek_line(gd, py);
1.3       nicm      635:        for (xx = px; xx < px + nx; xx++) {
1.30      nicm      636:                if (gl == NULL || xx >= gl->cellsize)
                    637:                        break;
1.48      nicm      638:                grid_get_cell(gd, xx, py, &gc);
                    639:                if (gc.flags & GRID_FLAG_PADDING)
1.3       nicm      640:                        continue;
                    641:
1.24      nicm      642:                if (with_codes) {
1.48      nicm      643:                        grid_string_cells_code(*lastgc, &gc, code, sizeof code,
1.26      nicm      644:                            escape_c0);
1.24      nicm      645:                        codelen = strlen(code);
1.48      nicm      646:                        memcpy(*lastgc, &gc, sizeof **lastgc);
1.24      nicm      647:                } else
                    648:                        codelen = 0;
                    649:
1.48      nicm      650:                data = gc.data.data;
                    651:                size = gc.data.size;
1.26      nicm      652:                if (escape_c0 && size == 1 && *data == '\\') {
1.27      nicm      653:                        data = "\\\\";
1.26      nicm      654:                        size = 2;
                    655:                }
                    656:
                    657:                while (len < off + size + codelen + 1) {
1.41      nicm      658:                        buf = xreallocarray(buf, 2, len);
1.21      nicm      659:                        len *= 2;
                    660:                }
1.3       nicm      661:
1.24      nicm      662:                if (codelen != 0) {
                    663:                        memcpy(buf + off, code, codelen);
                    664:                        off += codelen;
                    665:                }
1.26      nicm      666:                memcpy(buf + off, data, size);
                    667:                off += size;
1.3       nicm      668:        }
1.17      nicm      669:
1.37      nicm      670:        if (trim) {
1.28      nicm      671:                while (off > 0 && buf[off - 1] == ' ')
                    672:                        off--;
1.32      nicm      673:        }
1.3       nicm      674:        buf[off] = '\0';
1.26      nicm      675:
1.3       nicm      676:        return (buf);
1.7       nicm      677: }
                    678:
1.17      nicm      679: /*
1.7       nicm      680:  * Duplicate a set of lines between two grids. If there aren't enough lines in
                    681:  * either source or destination, the number of lines is limited to the number
                    682:  * available.
                    683:  */
                    684: void
1.31      nicm      685: grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy,
                    686:     u_int ny)
1.7       nicm      687: {
1.10      nicm      688:        struct grid_line        *dstl, *srcl;
                    689:        u_int                    yy;
1.7       nicm      690:
                    691:        if (dy + ny > dst->hsize + dst->sy)
                    692:                ny = dst->hsize + dst->sy - dy;
                    693:        if (sy + ny > src->hsize + src->sy)
                    694:                ny = src->hsize + src->sy - sy;
                    695:        grid_clear_lines(dst, dy, ny);
                    696:
                    697:        for (yy = 0; yy < ny; yy++) {
1.11      nicm      698:                srcl = &src->linedata[sy];
                    699:                dstl = &dst->linedata[dy];
1.10      nicm      700:
                    701:                memcpy(dstl, srcl, sizeof *dstl);
                    702:                if (srcl->cellsize != 0) {
1.42      deraadt   703:                        dstl->celldata = xreallocarray(NULL,
1.10      nicm      704:                            srcl->cellsize, sizeof *dstl->celldata);
                    705:                        memcpy(dstl->celldata, srcl->celldata,
                    706:                            srcl->cellsize * sizeof *dstl->celldata);
1.44      nicm      707:                } else
                    708:                        dstl->celldata = NULL;
1.7       nicm      709:
1.48      nicm      710:                if (srcl->extdsize != 0) {
                    711:                        dstl->extdsize = srcl->extdsize;
                    712:                        dstl->extddata = xreallocarray(NULL, dstl->extdsize,
                    713:                            sizeof *dstl->extddata);
                    714:                        memcpy(dstl->extddata, srcl->extddata, dstl->extdsize *
                    715:                            sizeof *dstl->extddata);
                    716:                }
                    717:
1.10      nicm      718:                sy++;
                    719:                dy++;
1.7       nicm      720:        }
1.22      nicm      721: }
                    722:
1.48      nicm      723: /* Copy a section of a line. */
                    724: void
                    725: grid_reflow_copy(struct grid_line *dst_gl, u_int to, struct grid_line *src_gl,
                    726:     u_int from, u_int to_copy)
                    727: {
                    728:        struct grid_cell_entry  *gce;
                    729:        u_int                    i, was;
                    730:
                    731:        memcpy(&dst_gl->celldata[to], &src_gl->celldata[from],
                    732:            to_copy * sizeof *dst_gl->celldata);
                    733:
                    734:        for (i = to; i < to + to_copy; i++) {
                    735:                gce = &dst_gl->celldata[i];
                    736:                if (~gce->flags & GRID_FLAG_EXTENDED)
                    737:                        continue;
                    738:                was = gce->offset;
                    739:
                    740:                dst_gl->extddata = xreallocarray(dst_gl->extddata,
                    741:                    dst_gl->extdsize + 1, sizeof *dst_gl->extddata);
                    742:                gce->offset = dst_gl->extdsize++;
                    743:                memcpy(&dst_gl->extddata[gce->offset], &src_gl->extddata[was],
                    744:                    sizeof *dst_gl->extddata);
                    745:        }
                    746: }
                    747:
1.23      nicm      748: /* Join line data. */
                    749: void
                    750: grid_reflow_join(struct grid *dst, u_int *py, struct grid_line *src_gl,
                    751:     u_int new_x)
                    752: {
                    753:        struct grid_line        *dst_gl = &dst->linedata[(*py) - 1];
                    754:        u_int                    left, to_copy, ox, nx;
                    755:
                    756:        /* How much is left on the old line? */
                    757:        left = new_x - dst_gl->cellsize;
                    758:
                    759:        /* Work out how much to append. */
                    760:        to_copy = src_gl->cellsize;
                    761:        if (to_copy > left)
                    762:                to_copy = left;
                    763:        ox = dst_gl->cellsize;
                    764:        nx = ox + to_copy;
                    765:
                    766:        /* Resize the destination line. */
1.41      nicm      767:        dst_gl->celldata = xreallocarray(dst_gl->celldata, nx,
1.23      nicm      768:            sizeof *dst_gl->celldata);
                    769:        dst_gl->cellsize = nx;
                    770:
                    771:        /* Append as much as possible. */
1.48      nicm      772:        grid_reflow_copy(dst_gl, ox, src_gl, 0, to_copy);
1.23      nicm      773:
                    774:        /* If there is any left in the source, split it. */
                    775:        if (src_gl->cellsize > to_copy) {
                    776:                dst_gl->flags |= GRID_LINE_WRAPPED;
                    777:
                    778:                src_gl->cellsize -= to_copy;
                    779:                grid_reflow_split(dst, py, src_gl, new_x, to_copy);
                    780:        }
                    781: }
                    782:
                    783: /* Split line data. */
                    784: void
                    785: grid_reflow_split(struct grid *dst, u_int *py, struct grid_line *src_gl,
                    786:     u_int new_x, u_int offset)
                    787: {
                    788:        struct grid_line        *dst_gl = NULL;
                    789:        u_int                    to_copy;
                    790:
                    791:        /* Loop and copy sections of the source line. */
                    792:        while (src_gl->cellsize > 0) {
                    793:                /* Create new line. */
                    794:                if (*py >= dst->hsize + dst->sy)
                    795:                        grid_scroll_history(dst);
                    796:                dst_gl = &dst->linedata[*py];
                    797:                (*py)++;
                    798:
                    799:                /* How much should we copy? */
                    800:                to_copy = new_x;
                    801:                if (to_copy > src_gl->cellsize)
                    802:                        to_copy = src_gl->cellsize;
                    803:
                    804:                /* Expand destination line. */
1.41      nicm      805:                dst_gl->celldata = xreallocarray(NULL, to_copy,
1.40      nicm      806:                    sizeof *dst_gl->celldata);
1.23      nicm      807:                dst_gl->cellsize = to_copy;
                    808:                dst_gl->flags |= GRID_LINE_WRAPPED;
                    809:
                    810:                /* Copy the data. */
1.48      nicm      811:                grid_reflow_copy(dst_gl, 0, src_gl, offset, to_copy);
1.23      nicm      812:
                    813:                /* Move offset and reduce old line size. */
                    814:                offset += to_copy;
                    815:                src_gl->cellsize -= to_copy;
                    816:        }
                    817:
                    818:        /* Last line is not wrapped. */
                    819:        if (dst_gl != NULL)
                    820:                dst_gl->flags &= ~GRID_LINE_WRAPPED;
                    821: }
                    822:
                    823: /* Move line data. */
                    824: void
                    825: grid_reflow_move(struct grid *dst, u_int *py, struct grid_line *src_gl)
                    826: {
                    827:        struct grid_line        *dst_gl;
                    828:
                    829:        /* Create new line. */
                    830:        if (*py >= dst->hsize + dst->sy)
                    831:                grid_scroll_history(dst);
                    832:        dst_gl = &dst->linedata[*py];
                    833:        (*py)++;
                    834:
                    835:        /* Copy the old line. */
                    836:        memcpy(dst_gl, src_gl, sizeof *dst_gl);
                    837:        dst_gl->flags &= ~GRID_LINE_WRAPPED;
                    838:
                    839:        /* Clear old line. */
                    840:        src_gl->celldata = NULL;
1.48      nicm      841:        src_gl->extddata = NULL;
1.23      nicm      842: }
                    843:
1.22      nicm      844: /*
1.23      nicm      845:  * Reflow lines from src grid into dst grid of width new_x. Returns number of
                    846:  * lines fewer in the visible area. The source grid is destroyed.
1.22      nicm      847:  */
                    848: u_int
1.23      nicm      849: grid_reflow(struct grid *dst, struct grid *src, u_int new_x)
1.22      nicm      850: {
1.23      nicm      851:        u_int                    py, sy, line;
1.22      nicm      852:        int                      previous_wrapped;
1.23      nicm      853:        struct grid_line        *src_gl;
                    854:
                    855:        py = 0;
                    856:        sy = src->sy;
1.22      nicm      857:
1.23      nicm      858:        previous_wrapped = 0;
                    859:        for (line = 0; line < sy + src->hsize; line++) {
                    860:                src_gl = src->linedata + line;
1.22      nicm      861:                if (!previous_wrapped) {
1.23      nicm      862:                        /* Wasn't wrapped. If smaller, move to destination. */
                    863:                        if (src_gl->cellsize <= new_x)
                    864:                                grid_reflow_move(dst, &py, src_gl);
                    865:                        else
                    866:                                grid_reflow_split(dst, &py, src_gl, new_x, 0);
                    867:                } else {
                    868:                        /* Previous was wrapped. Try to join. */
                    869:                        grid_reflow_join(dst, &py, src_gl, new_x);
1.22      nicm      870:                }
1.48      nicm      871:                previous_wrapped = (src_gl->flags & GRID_LINE_WRAPPED);
1.22      nicm      872:        }
                    873:
1.23      nicm      874:        grid_destroy(src);
                    875:
                    876:        if (py > sy)
1.22      nicm      877:                return (0);
1.23      nicm      878:        return (sy - py);
1.1       nicm      879: }