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

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