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

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