[BACK]Return to screen-write.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/screen-write.c, Revision 1.136

1.136   ! nicm        1: /* $OpenBSD: screen-write.c,v 1.135 2017/11/15 19:21:24 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.84      nicm        4:  * Copyright (c) 2007 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.56      nicm       21: #include <stdlib.h>
1.1       nicm       22: #include <string.h>
                     23:
                     24: #include "tmux.h"
                     25:
1.87      nicm       26: static void    screen_write_initctx(struct screen_write_ctx *,
                     27:                    struct tty_ctx *);
1.109     nicm       28: static void    screen_write_collect_clear(struct screen_write_ctx *, u_int,
                     29:                    u_int);
                     30: static void    screen_write_collect_scroll(struct screen_write_ctx *);
1.111     nicm       31: static void    screen_write_collect_flush(struct screen_write_ctx *, int);
1.87      nicm       32:
1.89      nicm       33: static int     screen_write_overwrite(struct screen_write_ctx *,
                     34:                    struct grid_cell *, u_int);
1.104     nicm       35: static const struct grid_cell *screen_write_combine(struct screen_write_ctx *,
                     36:                    const struct utf8_data *, u_int *);
1.1       nicm       37:
1.88      nicm       38: static const struct grid_cell screen_write_pad_cell = {
1.91      nicm       39:        GRID_FLAG_PADDING, 0, 8, 8, { { 0 }, 0, 0, 0 }
1.88      nicm       40: };
                     41:
1.109     nicm       42: struct screen_write_collect_item {
                     43:        u_int                    x;
1.118     nicm       44:        int                      wrapped;
1.109     nicm       45:
                     46:        u_int                    used;
                     47:        char                     data[256];
                     48:
                     49:        struct grid_cell         gc;
                     50:
1.126     nicm       51:        TAILQ_ENTRY(screen_write_collect_item) entry;
1.109     nicm       52: };
                     53: struct screen_write_collect_line {
                     54:        TAILQ_HEAD(, screen_write_collect_item) items;
                     55: };
1.92      nicm       56:
                     57: /* Initialize writing with a window. */
1.1       nicm       58: void
1.71      nicm       59: screen_write_start(struct screen_write_ctx *ctx, struct window_pane *wp,
                     60:     struct screen *s)
1.1       nicm       61: {
1.108     nicm       62:        char    tmp[16];
1.109     nicm       63:        u_int   y;
                     64:
                     65:        memset(ctx, 0, sizeof *ctx);
1.92      nicm       66:
1.1       nicm       67:        ctx->wp = wp;
                     68:        if (wp != NULL && s == NULL)
                     69:                ctx->s = wp->screen;
                     70:        else
                     71:                ctx->s = s;
1.92      nicm       72:
1.109     nicm       73:        ctx->list = xcalloc(screen_size_y(ctx->s), sizeof *ctx->list);
                     74:        for (y = 0; y < screen_size_y(ctx->s); y++)
                     75:                TAILQ_INIT(&ctx->list[y].items);
                     76:        ctx->item = xcalloc(1, sizeof *ctx->item);
1.92      nicm       77:
1.122     nicm       78:        ctx->scrolled = 0;
                     79:        ctx->bg = 8;
                     80:
1.108     nicm       81:        if (wp != NULL)
1.92      nicm       82:                snprintf(tmp, sizeof tmp, "pane %%%u", wp->id);
                     83:        log_debug("%s: size %ux%u, %s", __func__, screen_size_x(ctx->s),
1.108     nicm       84:            screen_size_y(ctx->s), wp == NULL ? "no pane" : tmp);
1.1       nicm       85: }
                     86:
                     87: /* Finish writing. */
                     88: void
1.92      nicm       89: screen_write_stop(struct screen_write_ctx *ctx)
                     90: {
1.109     nicm       91:        screen_write_collect_end(ctx);
1.111     nicm       92:        screen_write_collect_flush(ctx, 0);
1.92      nicm       93:
1.109     nicm       94:        log_debug("%s: %u cells (%u written, %u skipped)", __func__,
                     95:            ctx->cells, ctx->written, ctx->skipped);
1.92      nicm       96:
1.109     nicm       97:        free(ctx->item);
                     98:        free(ctx->list); /* flush will have emptied */
1.52      nicm       99: }
                    100:
                    101: /* Reset screen state. */
                    102: void
                    103: screen_write_reset(struct screen_write_ctx *ctx)
                    104: {
1.61      nicm      105:        struct screen   *s = ctx->s;
1.52      nicm      106:
1.61      nicm      107:        screen_reset_tabs(s);
                    108:        screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
1.63      nicm      109:
1.67      nicm      110:        s->mode &= ~(MODE_INSERT|MODE_KCURSOR|MODE_KKEYPAD|MODE_FOCUSON);
1.82      nicm      111:        s->mode &= ~(ALL_MOUSE_MODES|MODE_MOUSE_UTF8|MODE_MOUSE_SGR);
1.52      nicm      112:
1.99      nicm      113:        screen_write_clearscreen(ctx, 8);
1.52      nicm      114:        screen_write_cursormove(ctx, 0, 0);
1.1       nicm      115: }
                    116:
                    117: /* Write character. */
                    118: void
1.86      nicm      119: screen_write_putc(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
1.69      nicm      120:     u_char ch)
1.1       nicm      121: {
1.86      nicm      122:        struct grid_cell        gc;
                    123:
                    124:        memcpy(&gc, gcp, sizeof gc);
                    125:
                    126:        utf8_set(&gc.data, ch);
                    127:        screen_write_cell(ctx, &gc);
1.1       nicm      128: }
                    129:
1.24      nicm      130: /* Calculate string length, with embedded formatting. */
1.71      nicm      131: size_t
1.75      nicm      132: screen_write_cstrlen(const char *fmt, ...)
1.24      nicm      133: {
                    134:        va_list ap;
                    135:        char   *msg, *msg2, *ptr, *ptr2;
                    136:        size_t  size;
                    137:
                    138:        va_start(ap, fmt);
                    139:        xvasprintf(&msg, fmt, ap);
                    140:        va_end(ap);
                    141:        msg2 = xmalloc(strlen(msg) + 1);
                    142:
                    143:        ptr = msg;
                    144:        ptr2 = msg2;
                    145:        while (*ptr != '\0') {
                    146:                if (ptr[0] == '#' && ptr[1] == '[') {
                    147:                        while (*ptr != ']' && *ptr != '\0')
                    148:                                ptr++;
                    149:                        if (*ptr == ']')
                    150:                                ptr++;
                    151:                        continue;
                    152:                }
                    153:                *ptr2++ = *ptr++;
                    154:        }
                    155:        *ptr2 = '\0';
                    156:
1.75      nicm      157:        size = screen_write_strlen("%s", msg2);
1.24      nicm      158:
1.56      nicm      159:        free(msg);
                    160:        free(msg2);
1.24      nicm      161:
                    162:        return (size);
                    163: }
                    164:
1.2       nicm      165: /* Calculate string length. */
1.71      nicm      166: size_t
1.75      nicm      167: screen_write_strlen(const char *fmt, ...)
1.2       nicm      168: {
1.36      nicm      169:        va_list                 ap;
                    170:        char                   *msg;
1.76      nicm      171:        struct utf8_data        ud;
1.36      nicm      172:        u_char                 *ptr;
                    173:        size_t                  left, size = 0;
1.79      nicm      174:        enum utf8_state         more;
1.2       nicm      175:
                    176:        va_start(ap, fmt);
                    177:        xvasprintf(&msg, fmt, ap);
                    178:        va_end(ap);
                    179:
                    180:        ptr = msg;
                    181:        while (*ptr != '\0') {
1.79      nicm      182:                if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) {
1.36      nicm      183:                        ptr++;
1.2       nicm      184:
                    185:                        left = strlen(ptr);
1.77      nicm      186:                        if (left < (size_t)ud.size - 1)
1.36      nicm      187:                                break;
1.79      nicm      188:                        while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE)
1.2       nicm      189:                                ptr++;
1.36      nicm      190:                        ptr++;
                    191:
1.79      nicm      192:                        if (more == UTF8_DONE)
1.78      nicm      193:                                size += ud.width;
1.2       nicm      194:                } else {
1.75      nicm      195:                        if (*ptr > 0x1f && *ptr < 0x7f)
                    196:                                size++;
1.2       nicm      197:                        ptr++;
                    198:                }
1.7       ray       199:        }
1.2       nicm      200:
1.56      nicm      201:        free(msg);
1.2       nicm      202:        return (size);
                    203: }
                    204:
1.3       nicm      205: /* Write simple string (no UTF-8 or maximum length). */
1.71      nicm      206: void
1.86      nicm      207: screen_write_puts(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
1.71      nicm      208:     const char *fmt, ...)
1.1       nicm      209: {
                    210:        va_list ap;
                    211:
                    212:        va_start(ap, fmt);
1.86      nicm      213:        screen_write_vnputs(ctx, -1, gcp, fmt, ap);
1.2       nicm      214:        va_end(ap);
                    215: }
                    216:
                    217: /* Write string with length limit (-1 for unlimited). */
1.71      nicm      218: void
                    219: screen_write_nputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86      nicm      220:     const struct grid_cell *gcp, const char *fmt, ...)
1.2       nicm      221: {
                    222:        va_list ap;
                    223:
                    224:        va_start(ap, fmt);
1.86      nicm      225:        screen_write_vnputs(ctx, maxlen, gcp, fmt, ap);
1.2       nicm      226:        va_end(ap);
                    227: }
                    228:
                    229: void
1.3       nicm      230: screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86      nicm      231:     const struct grid_cell *gcp, const char *fmt, va_list ap)
1.2       nicm      232: {
1.86      nicm      233:        struct grid_cell        gc;
                    234:        struct utf8_data       *ud = &gc.data;
1.36      nicm      235:        char                   *msg;
                    236:        u_char                 *ptr;
                    237:        size_t                  left, size = 0;
1.79      nicm      238:        enum utf8_state         more;
1.2       nicm      239:
1.86      nicm      240:        memcpy(&gc, gcp, sizeof gc);
1.1       nicm      241:        xvasprintf(&msg, fmt, ap);
                    242:
1.2       nicm      243:        ptr = msg;
                    244:        while (*ptr != '\0') {
1.86      nicm      245:                if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
1.36      nicm      246:                        ptr++;
1.2       nicm      247:
                    248:                        left = strlen(ptr);
1.86      nicm      249:                        if (left < (size_t)ud->size - 1)
1.36      nicm      250:                                break;
1.86      nicm      251:                        while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
1.2       nicm      252:                                ptr++;
1.36      nicm      253:                        ptr++;
1.7       ray       254:
1.86      nicm      255:                        if (more != UTF8_DONE)
                    256:                                continue;
                    257:                        if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
                    258:                                while (size < (size_t)maxlen) {
                    259:                                        screen_write_putc(ctx, &gc, ' ');
                    260:                                        size++;
1.2       nicm      261:                                }
1.86      nicm      262:                                break;
1.2       nicm      263:                        }
1.86      nicm      264:                        size += ud->width;
                    265:                        screen_write_cell(ctx, &gc);
1.2       nicm      266:                } else {
1.86      nicm      267:                        if (maxlen > 0 && size + 1 > (size_t)maxlen)
1.2       nicm      268:                                break;
                    269:
1.57      nicm      270:                        if (*ptr == '\001')
1.86      nicm      271:                                gc.attr ^= GRID_ATTR_CHARSET;
1.75      nicm      272:                        else if (*ptr > 0x1f && *ptr < 0x7f) {
1.57      nicm      273:                                size++;
1.86      nicm      274:                                screen_write_putc(ctx, &gc, *ptr);
1.57      nicm      275:                        }
1.2       nicm      276:                        ptr++;
                    277:                }
                    278:        }
1.1       nicm      279:
1.56      nicm      280:        free(msg);
1.24      nicm      281: }
                    282:
                    283: /* Write string, similar to nputs, but with embedded formatting (#[]). */
1.71      nicm      284: void
1.75      nicm      285: screen_write_cnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86      nicm      286:     const struct grid_cell *gcp, const char *fmt, ...)
1.24      nicm      287: {
1.86      nicm      288:        struct grid_cell         gc;
                    289:        struct utf8_data        *ud = &gc.data;
1.24      nicm      290:        va_list                  ap;
                    291:        char                    *msg;
1.36      nicm      292:        u_char                  *ptr, *last;
1.24      nicm      293:        size_t                   left, size = 0;
1.79      nicm      294:        enum utf8_state          more;
1.24      nicm      295:
1.86      nicm      296:        memcpy(&gc, gcp, sizeof gc);
                    297:
1.24      nicm      298:        va_start(ap, fmt);
                    299:        xvasprintf(&msg, fmt, ap);
                    300:        va_end(ap);
                    301:
                    302:        ptr = msg;
                    303:        while (*ptr != '\0') {
                    304:                if (ptr[0] == '#' && ptr[1] == '[') {
                    305:                        ptr += 2;
                    306:                        last = ptr + strcspn(ptr, "]");
                    307:                        if (*last == '\0') {
                    308:                                /* No ]. Not much point in doing anything. */
                    309:                                break;
                    310:                        }
                    311:                        *last = '\0';
                    312:
1.86      nicm      313:                        style_parse(gcp, &gc, ptr);
1.24      nicm      314:                        ptr = last + 1;
                    315:                        continue;
                    316:                }
                    317:
1.86      nicm      318:                if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
1.36      nicm      319:                        ptr++;
1.24      nicm      320:
                    321:                        left = strlen(ptr);
1.86      nicm      322:                        if (left < (size_t)ud->size - 1)
1.36      nicm      323:                                break;
1.86      nicm      324:                        while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
1.24      nicm      325:                                ptr++;
1.36      nicm      326:                        ptr++;
1.24      nicm      327:
1.86      nicm      328:                        if (more != UTF8_DONE)
                    329:                                continue;
                    330:                        if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
                    331:                                while (size < (size_t)maxlen) {
                    332:                                        screen_write_putc(ctx, &gc, ' ');
                    333:                                        size++;
1.24      nicm      334:                                }
1.86      nicm      335:                                break;
1.24      nicm      336:                        }
1.86      nicm      337:                        size += ud->width;
                    338:                        screen_write_cell(ctx, &gc);
1.24      nicm      339:                } else {
1.86      nicm      340:                        if (maxlen > 0 && size + 1 > (size_t)maxlen)
1.24      nicm      341:                                break;
                    342:
1.75      nicm      343:                        if (*ptr > 0x1f && *ptr < 0x7f) {
                    344:                                size++;
1.86      nicm      345:                                screen_write_putc(ctx, &gc, *ptr);
1.75      nicm      346:                        }
1.24      nicm      347:                        ptr++;
                    348:                }
                    349:        }
                    350:
1.56      nicm      351:        free(msg);
1.1       nicm      352: }
                    353:
1.132     nicm      354: /* Copy from another screen. Assumes target region is big enough. */
1.1       nicm      355: void
1.95      nicm      356: screen_write_copy(struct screen_write_ctx *ctx, struct screen *src, u_int px,
1.132     nicm      357:     u_int py, u_int nx, u_int ny, bitstr_t *mbs, const struct grid_cell *mgc)
1.1       nicm      358: {
                    359:        struct screen           *s = ctx->s;
                    360:        struct grid             *gd = src->grid;
1.77      nicm      361:        struct grid_cell         gc;
1.102     nicm      362:        u_int                    xx, yy, cx, cy, b;
1.1       nicm      363:
1.125     nicm      364:        if (nx == 0 || ny == 0)
                    365:                return;
                    366:
1.1       nicm      367:        cx = s->cx;
                    368:        cy = s->cy;
1.96      nicm      369:
1.1       nicm      370:        for (yy = py; yy < py + ny; yy++) {
1.96      nicm      371:                for (xx = px; xx < px + nx; xx++) {
                    372:                        grid_get_cell(gd, xx, yy, &gc);
1.132     nicm      373:                        if (mbs != NULL) {
1.102     nicm      374:                                b = (yy * screen_size_x(src)) + xx;
1.132     nicm      375:                                if (bit_test(mbs, b)) {
                    376:                                        gc.attr = mgc->attr;
                    377:                                        gc.fg = mgc->fg;
                    378:                                        gc.bg = mgc->bg;
1.102     nicm      379:                                }
                    380:                        }
1.135     nicm      381:                        if (xx + gc.data.width <= px + nx)
                    382:                                screen_write_cell(ctx, &gc);
1.96      nicm      383:                }
1.132     nicm      384:                cy++;
                    385:                screen_write_cursormove(ctx, cx, cy);
                    386:        }
                    387: }
                    388:
                    389: /*
                    390:  * Copy from another screen but without the selection stuff. Also assumes the
                    391:  * target region is already big enough and already cleared.
                    392:  */
                    393: void
                    394: screen_write_fast_copy(struct screen_write_ctx *ctx, struct screen *src,
                    395:     u_int px, u_int py, u_int nx, u_int ny)
                    396: {
                    397:        struct screen           *s = ctx->s;
                    398:        struct grid             *gd = src->grid;
                    399:        struct grid_cell         gc;
                    400:        u_int                    xx, yy, cx, cy;
1.96      nicm      401:
1.132     nicm      402:        if (nx == 0 || ny == 0)
                    403:                return;
                    404:
                    405:        cy = s->cy;
                    406:        for (yy = py; yy < py + ny; yy++) {
1.134     nicm      407:                if (yy >= gd->hsize + gd->sy)
                    408:                        break;
1.132     nicm      409:                cx = s->cx;
                    410:                for (xx = px; xx < px + nx; xx++) {
                    411:                        if (xx >= gd->linedata[yy].cellsize)
                    412:                                break;
                    413:                        grid_get_cell(gd, xx, yy, &gc);
1.135     nicm      414:                        if (xx + gc.data.width > px + nx)
                    415:                                break;
1.132     nicm      416:                        if (!grid_cells_equal(&gc, &grid_default_cell))
                    417:                                grid_view_set_cell(ctx->s->grid, cx, cy, &gc);
                    418:                        cx++;
                    419:                }
1.1       nicm      420:                cy++;
1.125     nicm      421:        }
                    422: }
                    423:
1.129     nicm      424: /* Draw a horizontal line on screen. */
1.125     nicm      425: void
1.129     nicm      426: screen_write_hline(struct screen_write_ctx *ctx, u_int nx, int left, int right)
1.125     nicm      427: {
                    428:        struct screen           *s = ctx->s;
                    429:        struct grid_cell         gc;
                    430:        u_int                    cx, cy, i;
                    431:
                    432:        cx = s->cx;
                    433:        cy = s->cy;
                    434:
                    435:        memcpy(&gc, &grid_default_cell, sizeof gc);
                    436:        gc.attr |= GRID_ATTR_CHARSET;
                    437:
                    438:        screen_write_putc(ctx, &gc, left ? 't' : 'q');
                    439:        for (i = 1; i < nx - 1; i++)
                    440:                screen_write_putc(ctx, &gc, 'q');
                    441:        screen_write_putc(ctx, &gc, right ? 'u' : 'q');
1.129     nicm      442:
                    443:        screen_write_cursormove(ctx, cx, cy);
                    444: }
                    445:
                    446: /* Draw a horizontal line on screen. */
                    447: void
1.130     nicm      448: screen_write_vline(struct screen_write_ctx *ctx, u_int ny, int top, int bottom)
1.129     nicm      449: {
                    450:        struct screen           *s = ctx->s;
                    451:        struct grid_cell         gc;
                    452:        u_int                    cx, cy, i;
                    453:
                    454:        cx = s->cx;
                    455:        cy = s->cy;
                    456:
                    457:        memcpy(&gc, &grid_default_cell, sizeof gc);
                    458:        gc.attr |= GRID_ATTR_CHARSET;
                    459:
                    460:        screen_write_putc(ctx, &gc, top ? 'w' : 'x');
                    461:        for (i = 1; i < ny - 1; i++) {
                    462:                screen_write_cursormove(ctx, cx, cy + i);
                    463:                screen_write_putc(ctx, &gc, 'x');
                    464:        }
1.133     nicm      465:        screen_write_cursormove(ctx, cx, cy + ny - 1);
1.129     nicm      466:        screen_write_putc(ctx, &gc, bottom ? 'v' : 'x');
1.125     nicm      467:
                    468:        screen_write_cursormove(ctx, cx, cy);
                    469: }
                    470:
                    471: /* Draw a box on screen. */
                    472: void
                    473: screen_write_box(struct screen_write_ctx *ctx, u_int nx, u_int ny)
                    474: {
                    475:        struct screen           *s = ctx->s;
                    476:        struct grid_cell         gc;
                    477:        u_int                    cx, cy, i;
                    478:
                    479:        cx = s->cx;
                    480:        cy = s->cy;
                    481:
                    482:        memcpy(&gc, &grid_default_cell, sizeof gc);
                    483:        gc.attr |= GRID_ATTR_CHARSET;
                    484:
                    485:        screen_write_putc(ctx, &gc, 'l');
                    486:        for (i = 1; i < nx - 1; i++)
                    487:                screen_write_putc(ctx, &gc, 'q');
                    488:        screen_write_putc(ctx, &gc, 'k');
                    489:
                    490:        screen_write_cursormove(ctx, cx, cy + ny - 1);
                    491:        screen_write_putc(ctx, &gc, 'm');
                    492:        for (i = 1; i < nx - 1; i++)
                    493:                screen_write_putc(ctx, &gc, 'q');
                    494:        screen_write_putc(ctx, &gc, 'j');
                    495:
                    496:        for (i = 1; i < ny - 1; i++) {
                    497:                screen_write_cursormove(ctx, cx, cy + i);
                    498:                screen_write_putc(ctx, &gc, 'x');
                    499:        }
                    500:        for (i = 1; i < ny - 1; i++) {
                    501:                screen_write_cursormove(ctx, cx + nx - 1, cy + i);
                    502:                screen_write_putc(ctx, &gc, 'x');
                    503:        }
                    504:
                    505:        screen_write_cursormove(ctx, cx, cy);
                    506: }
                    507:
1.132     nicm      508: /*
                    509:  * Write a preview version of a window. Assumes target area is big enough and
                    510:  * already cleared.
                    511:  */
1.125     nicm      512: void
                    513: screen_write_preview(struct screen_write_ctx *ctx, struct screen *src, u_int nx,
                    514:     u_int ny)
                    515: {
                    516:        struct screen           *s = ctx->s;
                    517:        struct grid_cell         gc;
                    518:        u_int                    cx, cy, px, py;
                    519:
                    520:        cx = s->cx;
                    521:        cy = s->cy;
                    522:
                    523:        /*
                    524:         * If the cursor is on, pick the area around the cursor, otherwise use
                    525:         * the top left.
                    526:         */
                    527:        if (src->mode & MODE_CURSOR) {
                    528:                px = src->cx;
                    529:                if (px < nx / 3)
                    530:                        px = 0;
                    531:                else
                    532:                        px = px - nx / 3;
                    533:                if (px + nx > screen_size_x(src)) {
                    534:                        if (nx > screen_size_x(src))
                    535:                                px = 0;
                    536:                        else
                    537:                                px = screen_size_x(src) - nx;
                    538:                }
                    539:                py = src->cy;
                    540:                if (py < ny / 3)
                    541:                        py = 0;
                    542:                else
                    543:                        py = py - ny / 3;
                    544:                if (py + ny > screen_size_y(src)) {
                    545:                        if (ny > screen_size_y(src))
                    546:                                py = 0;
                    547:                        else
                    548:                                py = screen_size_y(src) - ny;
                    549:                }
                    550:        } else {
                    551:                px = 0;
                    552:                py = 0;
                    553:        }
                    554:
1.132     nicm      555:        screen_write_fast_copy(ctx, src, px, src->grid->hsize + py, nx, ny);
1.125     nicm      556:
                    557:        if (src->mode & MODE_CURSOR) {
                    558:                grid_view_get_cell(src->grid, src->cx, src->cy, &gc);
                    559:                gc.attr |= GRID_ATTR_REVERSE;
                    560:                screen_write_cursormove(ctx, cx + (src->cx - px),
                    561:                    cy + (src->cy - py));
                    562:                screen_write_cell(ctx, &gc);
1.1       nicm      563:        }
                    564: }
                    565:
1.17      nicm      566: /* Set up context for TTY command. */
1.87      nicm      567: static void
                    568: screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx)
1.1       nicm      569: {
1.87      nicm      570:        struct screen   *s = ctx->s;
1.123     nicm      571:
                    572:        memset(ttyctx, 0, sizeof *ttyctx);
1.1       nicm      573:
1.17      nicm      574:        ttyctx->wp = ctx->wp;
1.1       nicm      575:
1.17      nicm      576:        ttyctx->ocx = s->cx;
                    577:        ttyctx->ocy = s->cy;
                    578:
                    579:        ttyctx->orlower = s->rlower;
                    580:        ttyctx->orupper = s->rupper;
1.87      nicm      581: }
1.31      nicm      582:
1.61      nicm      583: /* Set a mode. */
                    584: void
                    585: screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
                    586: {
                    587:        struct screen   *s = ctx->s;
                    588:
                    589:        s->mode |= mode;
                    590: }
                    591:
                    592: /* Clear a mode. */
                    593: void
                    594: screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
                    595: {
                    596:        struct screen   *s = ctx->s;
                    597:
                    598:        s->mode &= ~mode;
                    599: }
                    600:
1.1       nicm      601: /* Cursor up by ny. */
                    602: void
                    603: screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
                    604: {
                    605:        struct screen   *s = ctx->s;
                    606:
                    607:        if (ny == 0)
                    608:                ny = 1;
                    609:
1.12      nicm      610:        if (s->cy < s->rupper) {
                    611:                /* Above region. */
                    612:                if (ny > s->cy)
                    613:                        ny = s->cy;
                    614:        } else {
                    615:                /* Below region. */
                    616:                if (ny > s->cy - s->rupper)
                    617:                        ny = s->cy - s->rupper;
                    618:        }
1.66      nicm      619:        if (s->cx == screen_size_x(s))
                    620:            s->cx--;
1.1       nicm      621:        if (ny == 0)
                    622:                return;
                    623:
                    624:        s->cy -= ny;
                    625: }
                    626:
                    627: /* Cursor down by ny. */
                    628: void
                    629: screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
                    630: {
                    631:        struct screen   *s = ctx->s;
                    632:
                    633:        if (ny == 0)
                    634:                ny = 1;
                    635:
1.12      nicm      636:        if (s->cy > s->rlower) {
                    637:                /* Below region. */
                    638:                if (ny > screen_size_y(s) - 1 - s->cy)
                    639:                        ny = screen_size_y(s) - 1 - s->cy;
                    640:        } else {
                    641:                /* Above region. */
                    642:                if (ny > s->rlower - s->cy)
                    643:                        ny = s->rlower - s->cy;
                    644:        }
1.66      nicm      645:        if (s->cx == screen_size_x(s))
                    646:            s->cx--;
1.1       nicm      647:        if (ny == 0)
                    648:                return;
                    649:
                    650:        s->cy += ny;
                    651: }
                    652:
1.101     nicm      653: /* Cursor right by nx. */
1.1       nicm      654: void
                    655: screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
                    656: {
                    657:        struct screen   *s = ctx->s;
                    658:
                    659:        if (nx == 0)
                    660:                nx = 1;
                    661:
                    662:        if (nx > screen_size_x(s) - 1 - s->cx)
                    663:                nx = screen_size_x(s) - 1 - s->cx;
                    664:        if (nx == 0)
                    665:                return;
                    666:
                    667:        s->cx += nx;
                    668: }
                    669:
                    670: /* Cursor left by nx. */
                    671: void
                    672: screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
                    673: {
                    674:        struct screen   *s = ctx->s;
                    675:
                    676:        if (nx == 0)
                    677:                nx = 1;
                    678:
                    679:        if (nx > s->cx)
                    680:                nx = s->cx;
                    681:        if (nx == 0)
                    682:                return;
                    683:
                    684:        s->cx -= nx;
1.5       nicm      685: }
                    686:
1.29      nicm      687: /* Backspace; cursor left unless at start of wrapped line when can move up. */
                    688: void
                    689: screen_write_backspace(struct screen_write_ctx *ctx)
                    690: {
                    691:        struct screen           *s = ctx->s;
                    692:        struct grid_line        *gl;
                    693:
                    694:        if (s->cx == 0) {
                    695:                if (s->cy == 0)
                    696:                        return;
                    697:                gl = &s->grid->linedata[s->grid->hsize + s->cy - 1];
                    698:                if (gl->flags & GRID_LINE_WRAPPED) {
                    699:                        s->cy--;
                    700:                        s->cx = screen_size_x(s) - 1;
                    701:                }
                    702:        } else
                    703:                s->cx--;
                    704: }
                    705:
1.5       nicm      706: /* VT100 alignment test. */
                    707: void
                    708: screen_write_alignmenttest(struct screen_write_ctx *ctx)
                    709: {
                    710:        struct screen           *s = ctx->s;
1.17      nicm      711:        struct tty_ctx           ttyctx;
1.5       nicm      712:        struct grid_cell         gc;
                    713:        u_int                    xx, yy;
                    714:
1.87      nicm      715:        screen_write_initctx(ctx, &ttyctx);
1.17      nicm      716:
1.5       nicm      717:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.77      nicm      718:        utf8_set(&gc.data, 'E');
1.7       ray       719:
1.5       nicm      720:        for (yy = 0; yy < screen_size_y(s); yy++) {
                    721:                for (xx = 0; xx < screen_size_x(s); xx++)
                    722:                        grid_view_set_cell(s->grid, xx, yy, &gc);
                    723:        }
1.7       ray       724:
1.5       nicm      725:        s->cx = 0;
                    726:        s->cy = 0;
                    727:
                    728:        s->rupper = 0;
                    729:        s->rlower = screen_size_y(s) - 1;
                    730:
1.109     nicm      731:        screen_write_collect_clear(ctx, 0, screen_size_y(s) - 1);
1.17      nicm      732:        tty_write(tty_cmd_alignmenttest, &ttyctx);
1.1       nicm      733: }
                    734:
                    735: /* Insert nx characters. */
                    736: void
1.99      nicm      737: screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.1       nicm      738: {
                    739:        struct screen   *s = ctx->s;
1.17      nicm      740:        struct tty_ctx   ttyctx;
1.1       nicm      741:
                    742:        if (nx == 0)
                    743:                nx = 1;
                    744:
1.9       nicm      745:        if (nx > screen_size_x(s) - s->cx)
                    746:                nx = screen_size_x(s) - s->cx;
1.1       nicm      747:        if (nx == 0)
                    748:                return;
                    749:
1.107     nicm      750:        if (s->cx > screen_size_x(s) - 1)
                    751:                return;
                    752:
1.87      nicm      753:        screen_write_initctx(ctx, &ttyctx);
1.107     nicm      754:        ttyctx.bg = bg;
1.1       nicm      755:
1.107     nicm      756:        grid_view_insert_cells(s->grid, s->cx, s->cy, nx, bg);
1.1       nicm      757:
1.111     nicm      758:        screen_write_collect_flush(ctx, 0);
1.17      nicm      759:        ttyctx.num = nx;
                    760:        tty_write(tty_cmd_insertcharacter, &ttyctx);
1.1       nicm      761: }
                    762:
                    763: /* Delete nx characters. */
                    764: void
1.99      nicm      765: screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.1       nicm      766: {
                    767:        struct screen   *s = ctx->s;
1.17      nicm      768:        struct tty_ctx   ttyctx;
1.1       nicm      769:
                    770:        if (nx == 0)
                    771:                nx = 1;
                    772:
1.9       nicm      773:        if (nx > screen_size_x(s) - s->cx)
                    774:                nx = screen_size_x(s) - s->cx;
1.1       nicm      775:        if (nx == 0)
                    776:                return;
                    777:
1.107     nicm      778:        if (s->cx > screen_size_x(s) - 1)
                    779:                return;
                    780:
1.87      nicm      781:        screen_write_initctx(ctx, &ttyctx);
1.107     nicm      782:        ttyctx.bg = bg;
1.1       nicm      783:
1.107     nicm      784:        grid_view_delete_cells(s->grid, s->cx, s->cy, nx, bg);
1.1       nicm      785:
1.111     nicm      786:        screen_write_collect_flush(ctx, 0);
1.17      nicm      787:        ttyctx.num = nx;
                    788:        tty_write(tty_cmd_deletecharacter, &ttyctx);
1.59      nicm      789: }
                    790:
                    791: /* Clear nx characters. */
                    792: void
1.121     nicm      793: screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.59      nicm      794: {
                    795:        struct screen   *s = ctx->s;
                    796:        struct tty_ctx   ttyctx;
                    797:
                    798:        if (nx == 0)
                    799:                nx = 1;
                    800:
                    801:        if (nx > screen_size_x(s) - s->cx)
                    802:                nx = screen_size_x(s) - s->cx;
                    803:        if (nx == 0)
                    804:                return;
                    805:
1.107     nicm      806:        if (s->cx > screen_size_x(s) - 1)
                    807:                return;
                    808:
1.87      nicm      809:        screen_write_initctx(ctx, &ttyctx);
1.121     nicm      810:        ttyctx.bg = bg;
1.59      nicm      811:
1.124     nicm      812:        grid_view_clear(s->grid, s->cx, s->cy, nx, 1, bg);
1.59      nicm      813:
1.111     nicm      814:        screen_write_collect_flush(ctx, 0);
1.59      nicm      815:        ttyctx.num = nx;
                    816:        tty_write(tty_cmd_clearcharacter, &ttyctx);
1.1       nicm      817: }
                    818:
                    819: /* Insert ny lines. */
                    820: void
1.99      nicm      821: screen_write_insertline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1.1       nicm      822: {
                    823:        struct screen   *s = ctx->s;
1.107     nicm      824:        struct grid     *gd = s->grid;
1.17      nicm      825:        struct tty_ctx   ttyctx;
1.1       nicm      826:
                    827:        if (ny == 0)
                    828:                ny = 1;
                    829:
1.11      nicm      830:        if (s->cy < s->rupper || s->cy > s->rlower) {
                    831:                if (ny > screen_size_y(s) - s->cy)
                    832:                        ny = screen_size_y(s) - s->cy;
                    833:                if (ny == 0)
                    834:                        return;
                    835:
1.87      nicm      836:                screen_write_initctx(ctx, &ttyctx);
1.107     nicm      837:                ttyctx.bg = bg;
1.11      nicm      838:
1.107     nicm      839:                grid_view_insert_lines(gd, s->cy, ny, bg);
1.11      nicm      840:
1.111     nicm      841:                screen_write_collect_flush(ctx, 0);
1.17      nicm      842:                ttyctx.num = ny;
                    843:                tty_write(tty_cmd_insertline, &ttyctx);
1.11      nicm      844:                return;
                    845:        }
                    846:
                    847:        if (ny > s->rlower + 1 - s->cy)
                    848:                ny = s->rlower + 1 - s->cy;
1.1       nicm      849:        if (ny == 0)
                    850:                return;
1.41      nicm      851:
1.87      nicm      852:        screen_write_initctx(ctx, &ttyctx);
1.107     nicm      853:        ttyctx.bg = bg;
1.1       nicm      854:
                    855:        if (s->cy < s->rupper || s->cy > s->rlower)
1.107     nicm      856:                grid_view_insert_lines(gd, s->cy, ny, bg);
                    857:        else
                    858:                grid_view_insert_lines_region(gd, s->rlower, s->cy, ny, bg);
1.1       nicm      859:
1.111     nicm      860:        screen_write_collect_flush(ctx, 0);
1.17      nicm      861:        ttyctx.num = ny;
                    862:        tty_write(tty_cmd_insertline, &ttyctx);
1.1       nicm      863: }
                    864:
                    865: /* Delete ny lines. */
                    866: void
1.99      nicm      867: screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1.1       nicm      868: {
                    869:        struct screen   *s = ctx->s;
1.107     nicm      870:        struct grid     *gd = s->grid;
1.17      nicm      871:        struct tty_ctx   ttyctx;
1.1       nicm      872:
                    873:        if (ny == 0)
                    874:                ny = 1;
                    875:
1.11      nicm      876:        if (s->cy < s->rupper || s->cy > s->rlower) {
                    877:                if (ny > screen_size_y(s) - s->cy)
                    878:                        ny = screen_size_y(s) - s->cy;
                    879:                if (ny == 0)
                    880:                        return;
                    881:
1.87      nicm      882:                screen_write_initctx(ctx, &ttyctx);
1.107     nicm      883:                ttyctx.bg = bg;
1.11      nicm      884:
1.107     nicm      885:                grid_view_delete_lines(gd, s->cy, ny, bg);
1.11      nicm      886:
1.111     nicm      887:                screen_write_collect_flush(ctx, 0);
1.17      nicm      888:                ttyctx.num = ny;
                    889:                tty_write(tty_cmd_deleteline, &ttyctx);
1.11      nicm      890:                return;
                    891:        }
1.41      nicm      892:
1.11      nicm      893:        if (ny > s->rlower + 1 - s->cy)
                    894:                ny = s->rlower + 1 - s->cy;
1.1       nicm      895:        if (ny == 0)
                    896:                return;
                    897:
1.87      nicm      898:        screen_write_initctx(ctx, &ttyctx);
1.107     nicm      899:        ttyctx.bg = bg;
1.1       nicm      900:
                    901:        if (s->cy < s->rupper || s->cy > s->rlower)
1.107     nicm      902:                grid_view_delete_lines(gd, s->cy, ny, bg);
                    903:        else
                    904:                grid_view_delete_lines_region(gd, s->rlower, s->cy, ny, bg);
1.1       nicm      905:
1.111     nicm      906:        screen_write_collect_flush(ctx, 0);
1.17      nicm      907:        ttyctx.num = ny;
                    908:        tty_write(tty_cmd_deleteline, &ttyctx);
1.1       nicm      909: }
                    910:
                    911: /* Clear line at cursor. */
                    912: void
1.99      nicm      913: screen_write_clearline(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      914: {
1.92      nicm      915:        struct screen           *s = ctx->s;
                    916:        struct grid_line        *gl;
                    917:        struct tty_ctx           ttyctx;
                    918:        u_int                    sx = screen_size_x(s);
1.1       nicm      919:
1.92      nicm      920:        gl = &s->grid->linedata[s->grid->hsize + s->cy];
1.99      nicm      921:        if (gl->cellsize == 0 && bg == 8)
1.92      nicm      922:                return;
1.1       nicm      923:
1.108     nicm      924:        screen_write_initctx(ctx, &ttyctx);
                    925:        ttyctx.bg = bg;
                    926:
1.99      nicm      927:        grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
                    928:
1.109     nicm      929:        screen_write_collect_clear(ctx, s->cy, 1);
1.114     nicm      930:        screen_write_collect_flush(ctx, 0);
1.17      nicm      931:        tty_write(tty_cmd_clearline, &ttyctx);
1.1       nicm      932: }
                    933:
                    934: /* Clear to end of line from cursor. */
                    935: void
1.99      nicm      936: screen_write_clearendofline(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      937: {
1.92      nicm      938:        struct screen           *s = ctx->s;
                    939:        struct grid_line        *gl;
                    940:        struct tty_ctx           ttyctx;
                    941:        u_int                    sx = screen_size_x(s);
1.1       nicm      942:
1.92      nicm      943:        gl = &s->grid->linedata[s->grid->hsize + s->cy];
1.99      nicm      944:        if (s->cx > sx - 1 || (s->cx >= gl->cellsize && bg == 8))
1.92      nicm      945:                return;
1.108     nicm      946:
                    947:        screen_write_initctx(ctx, &ttyctx);
                    948:        ttyctx.bg = bg;
1.1       nicm      949:
1.99      nicm      950:        grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1, bg);
                    951:
1.109     nicm      952:        if (s->cx == 0)
                    953:                screen_write_collect_clear(ctx, s->cy, 1);
1.114     nicm      954:        screen_write_collect_flush(ctx, 0);
1.41      nicm      955:        tty_write(tty_cmd_clearendofline, &ttyctx);
1.1       nicm      956: }
                    957:
                    958: /* Clear to start of line from cursor. */
                    959: void
1.99      nicm      960: screen_write_clearstartofline(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      961: {
                    962:        struct screen   *s = ctx->s;
1.17      nicm      963:        struct tty_ctx   ttyctx;
1.92      nicm      964:        u_int            sx = screen_size_x(s);
1.1       nicm      965:
1.87      nicm      966:        screen_write_initctx(ctx, &ttyctx);
1.99      nicm      967:        ttyctx.bg = bg;
1.1       nicm      968:
1.109     nicm      969:        if (s->cx > sx - 1)
1.99      nicm      970:                grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1.109     nicm      971:        else
1.99      nicm      972:                grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1.1       nicm      973:
1.109     nicm      974:        if (s->cx > sx - 1)
                    975:                screen_write_collect_clear(ctx, s->cy, 1);
1.114     nicm      976:        screen_write_collect_flush(ctx, 0);
1.17      nicm      977:        tty_write(tty_cmd_clearstartofline, &ttyctx);
1.1       nicm      978: }
                    979:
1.101     nicm      980: /* Move cursor to px,py. */
1.1       nicm      981: void
                    982: screen_write_cursormove(struct screen_write_ctx *ctx, u_int px, u_int py)
                    983: {
                    984:        struct screen   *s = ctx->s;
                    985:
                    986:        if (px > screen_size_x(s) - 1)
                    987:                px = screen_size_x(s) - 1;
                    988:        if (py > screen_size_y(s) - 1)
                    989:                py = screen_size_y(s) - 1;
                    990:
                    991:        s->cx = px;
                    992:        s->cy = py;
                    993: }
                    994:
1.101     nicm      995: /* Reverse index (up with scroll). */
1.1       nicm      996: void
1.122     nicm      997: screen_write_reverseindex(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      998: {
                    999:        struct screen   *s = ctx->s;
1.17      nicm     1000:        struct tty_ctx   ttyctx;
1.1       nicm     1001:
1.87      nicm     1002:        screen_write_initctx(ctx, &ttyctx);
1.122     nicm     1003:        ttyctx.bg = bg;
1.1       nicm     1004:
1.109     nicm     1005:        if (s->cy == s->rupper)
1.122     nicm     1006:                grid_view_scroll_region_down(s->grid, s->rupper, s->rlower, bg);
1.109     nicm     1007:        else if (s->cy > 0)
1.1       nicm     1008:                s->cy--;
                   1009:
1.111     nicm     1010:        screen_write_collect_flush(ctx, 0);
1.17      nicm     1011:        tty_write(tty_cmd_reverseindex, &ttyctx);
1.1       nicm     1012: }
                   1013:
                   1014: /* Set scroll region. */
                   1015: void
1.83      nicm     1016: screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper,
                   1017:     u_int rlower)
1.1       nicm     1018: {
                   1019:        struct screen   *s = ctx->s;
                   1020:
                   1021:        if (rupper > screen_size_y(s) - 1)
                   1022:                rupper = screen_size_y(s) - 1;
                   1023:        if (rlower > screen_size_y(s) - 1)
                   1024:                rlower = screen_size_y(s) - 1;
1.13      nicm     1025:        if (rupper >= rlower)   /* cannot be one line */
1.1       nicm     1026:                return;
                   1027:
1.111     nicm     1028:        screen_write_collect_flush(ctx, 0);
1.110     nicm     1029:
1.1       nicm     1030:        /* Cursor moves to top-left. */
                   1031:        s->cx = 0;
                   1032:        s->cy = 0;
                   1033:
                   1034:        s->rupper = rupper;
                   1035:        s->rlower = rlower;
                   1036: }
                   1037:
1.34      nicm     1038: /* Line feed. */
1.1       nicm     1039: void
1.122     nicm     1040: screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped, u_int bg)
1.1       nicm     1041: {
1.20      nicm     1042:        struct screen           *s = ctx->s;
1.109     nicm     1043:        struct grid             *gd = s->grid;
1.20      nicm     1044:        struct grid_line        *gl;
1.1       nicm     1045:
1.109     nicm     1046:        gl = &gd->linedata[gd->hsize + s->cy];
1.20      nicm     1047:        if (wrapped)
                   1048:                gl->flags |= GRID_LINE_WRAPPED;
1.73      nicm     1049:        else
                   1050:                gl->flags &= ~GRID_LINE_WRAPPED;
1.20      nicm     1051:
1.114     nicm     1052:        log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
                   1053:            s->rupper, s->rlower);
                   1054:
1.122     nicm     1055:        if (bg != ctx->bg) {
                   1056:                screen_write_collect_flush(ctx, 1);
                   1057:                ctx->bg = bg;
                   1058:        }
                   1059:
1.92      nicm     1060:        if (s->cy == s->rlower) {
1.122     nicm     1061:                grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1.109     nicm     1062:                screen_write_collect_scroll(ctx);
1.110     nicm     1063:                ctx->scrolled++;
1.109     nicm     1064:        } else if (s->cy < screen_size_y(s) - 1)
1.1       nicm     1065:                s->cy++;
                   1066: }
                   1067:
1.110     nicm     1068: /* Scroll up. */
                   1069: void
1.122     nicm     1070: screen_write_scrollup(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1.110     nicm     1071: {
                   1072:        struct screen   *s = ctx->s;
                   1073:        struct grid     *gd = s->grid;
                   1074:        u_int            i;
                   1075:
                   1076:        if (lines == 0)
                   1077:                lines = 1;
                   1078:        else if (lines > s->rlower - s->rupper + 1)
                   1079:                lines = s->rlower - s->rupper + 1;
                   1080:
1.122     nicm     1081:        if (bg != ctx->bg) {
                   1082:                screen_write_collect_flush(ctx, 1);
                   1083:                ctx->bg = bg;
                   1084:        }
                   1085:
1.110     nicm     1086:        for (i = 0; i < lines; i++) {
1.122     nicm     1087:                grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1.110     nicm     1088:                screen_write_collect_scroll(ctx);
                   1089:        }
                   1090:        ctx->scrolled += lines;
                   1091: }
                   1092:
1.1       nicm     1093: /* Carriage return (cursor to start of line). */
                   1094: void
                   1095: screen_write_carriagereturn(struct screen_write_ctx *ctx)
                   1096: {
                   1097:        struct screen   *s = ctx->s;
                   1098:
                   1099:        s->cx = 0;
                   1100: }
                   1101:
                   1102: /* Clear to end of screen from cursor. */
                   1103: void
1.99      nicm     1104: screen_write_clearendofscreen(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm     1105: {
                   1106:        struct screen   *s = ctx->s;
1.107     nicm     1107:        struct grid     *gd = s->grid;
1.17      nicm     1108:        struct tty_ctx   ttyctx;
1.92      nicm     1109:        u_int            sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm     1110:
1.87      nicm     1111:        screen_write_initctx(ctx, &ttyctx);
1.99      nicm     1112:        ttyctx.bg = bg;
1.1       nicm     1113:
1.46      nicm     1114:        /* Scroll into history if it is enabled and clearing entire screen. */
1.109     nicm     1115:        if (s->cx == 0 && s->cy == 0 && (gd->flags & GRID_HISTORY))
1.107     nicm     1116:                grid_view_clear_history(gd, bg);
1.109     nicm     1117:        else {
                   1118:                if (s->cx <= sx - 1)
1.107     nicm     1119:                        grid_view_clear(gd, s->cx, s->cy, sx - s->cx, 1, bg);
                   1120:                grid_view_clear(gd, 0, s->cy + 1, sx, sy - (s->cy + 1), bg);
1.46      nicm     1121:        }
1.1       nicm     1122:
1.109     nicm     1123:        screen_write_collect_clear(ctx, s->cy + 1, sy - (s->cy + 1));
1.111     nicm     1124:        screen_write_collect_flush(ctx, 0);
1.17      nicm     1125:        tty_write(tty_cmd_clearendofscreen, &ttyctx);
1.1       nicm     1126: }
                   1127:
                   1128: /* Clear to start of screen. */
                   1129: void
1.105     nicm     1130: screen_write_clearstartofscreen(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm     1131: {
                   1132:        struct screen   *s = ctx->s;
1.17      nicm     1133:        struct tty_ctx   ttyctx;
1.92      nicm     1134:        u_int            sx = screen_size_x(s);
1.1       nicm     1135:
1.87      nicm     1136:        screen_write_initctx(ctx, &ttyctx);
1.105     nicm     1137:        ttyctx.bg = bg;
1.1       nicm     1138:
1.109     nicm     1139:        if (s->cy > 0)
1.105     nicm     1140:                grid_view_clear(s->grid, 0, 0, sx, s->cy, bg);
1.109     nicm     1141:        if (s->cx > sx - 1)
1.120     nicm     1142:                grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1.109     nicm     1143:        else
1.120     nicm     1144:                grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1.1       nicm     1145:
1.109     nicm     1146:        screen_write_collect_clear(ctx, 0, s->cy);
1.111     nicm     1147:        screen_write_collect_flush(ctx, 0);
1.17      nicm     1148:        tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1.1       nicm     1149: }
                   1150:
                   1151: /* Clear entire screen. */
                   1152: void
1.99      nicm     1153: screen_write_clearscreen(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm     1154: {
                   1155:        struct screen   *s = ctx->s;
1.17      nicm     1156:        struct tty_ctx   ttyctx;
1.92      nicm     1157:        u_int            sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm     1158:
1.87      nicm     1159:        screen_write_initctx(ctx, &ttyctx);
1.99      nicm     1160:        ttyctx.bg = bg;
1.1       nicm     1161:
1.46      nicm     1162:        /* Scroll into history if it is enabled. */
                   1163:        if (s->grid->flags & GRID_HISTORY)
1.99      nicm     1164:                grid_view_clear_history(s->grid, bg);
1.83      nicm     1165:        else
1.99      nicm     1166:                grid_view_clear(s->grid, 0, 0, sx, sy, bg);
1.1       nicm     1167:
1.109     nicm     1168:        screen_write_collect_clear(ctx, 0, sy);
1.17      nicm     1169:        tty_write(tty_cmd_clearscreen, &ttyctx);
1.51      nicm     1170: }
                   1171:
                   1172: /* Clear entire history. */
                   1173: void
                   1174: screen_write_clearhistory(struct screen_write_ctx *ctx)
                   1175: {
                   1176:        struct screen   *s = ctx->s;
                   1177:        struct grid     *gd = s->grid;
                   1178:
1.99      nicm     1179:        grid_move_lines(gd, 0, gd->hsize, gd->sy, 8);
1.93      nicm     1180:        gd->hscrolled = gd->hsize = 0;
1.1       nicm     1181: }
                   1182:
1.109     nicm     1183: /* Clear a collected line. */
                   1184: static void
                   1185: screen_write_collect_clear(struct screen_write_ctx *ctx, u_int y, u_int n)
                   1186: {
                   1187:        struct screen_write_collect_item        *ci, *tmp;
                   1188:        u_int                                    i;
                   1189:        size_t                                   size;
                   1190:
                   1191:        for (i = y ; i < y + n; i++) {
                   1192:                if (TAILQ_EMPTY(&ctx->list[i].items))
                   1193:                        continue;
                   1194:                size = 0;
                   1195:                TAILQ_FOREACH_SAFE(ci, &ctx->list[i].items, entry, tmp) {
                   1196:                        size += ci->used;
                   1197:                        TAILQ_REMOVE(&ctx->list[i].items, ci, entry);
                   1198:                        free(ci);
                   1199:                }
                   1200:                ctx->skipped += size;
1.114     nicm     1201:                log_debug("%s: dropped %zu bytes (line %u)", __func__, size, i);
1.109     nicm     1202:        }
                   1203: }
                   1204:
                   1205: /* Scroll collected lines up. */
                   1206: static void
                   1207: screen_write_collect_scroll(struct screen_write_ctx *ctx)
                   1208: {
                   1209:        struct screen                           *s = ctx->s;
                   1210:        struct screen_write_collect_line        *cl;
                   1211:        u_int                                    y;
                   1212:
1.114     nicm     1213:        log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
                   1214:            s->rupper, s->rlower);
                   1215:
1.109     nicm     1216:        screen_write_collect_clear(ctx, s->rupper, 1);
                   1217:        for (y = s->rupper; y < s->rlower; y++) {
                   1218:                cl = &ctx->list[y + 1];
                   1219:                TAILQ_CONCAT(&ctx->list[y].items, &cl->items, entry);
                   1220:                TAILQ_INIT(&cl->items);
                   1221:        }
                   1222: }
                   1223:
                   1224: /* Flush collected lines. */
                   1225: static void
1.111     nicm     1226: screen_write_collect_flush(struct screen_write_ctx *ctx, int scroll_only)
1.109     nicm     1227: {
                   1228:        struct screen                           *s = ctx->s;
                   1229:        struct screen_write_collect_item        *ci, *tmp;
1.116     nicm     1230:        u_int                                    y, cx, cy, items = 0;
1.109     nicm     1231:        struct tty_ctx                           ttyctx;
1.116     nicm     1232:        size_t                                   written = 0;
1.110     nicm     1233:
                   1234:        if (ctx->scrolled != 0) {
                   1235:                log_debug("%s: scrolled %u (region %u-%u)", __func__,
                   1236:                    ctx->scrolled, s->rupper, s->rlower);
                   1237:                if (ctx->scrolled > s->rlower - s->rupper + 1)
                   1238:                        ctx->scrolled = s->rlower - s->rupper + 1;
                   1239:
                   1240:                screen_write_initctx(ctx, &ttyctx);
                   1241:                ttyctx.num = ctx->scrolled;
1.122     nicm     1242:                ttyctx.bg = ctx->bg;
1.110     nicm     1243:                tty_write(tty_cmd_scrollup, &ttyctx);
                   1244:        }
                   1245:        ctx->scrolled = 0;
1.122     nicm     1246:        ctx->bg = 8;
                   1247:
1.111     nicm     1248:        if (scroll_only)
                   1249:                return;
1.109     nicm     1250:
                   1251:        cx = s->cx; cy = s->cy;
                   1252:        for (y = 0; y < screen_size_y(s); y++) {
                   1253:                TAILQ_FOREACH_SAFE(ci, &ctx->list[y].items, entry, tmp) {
                   1254:                        screen_write_cursormove(ctx, ci->x, y);
                   1255:                        screen_write_initctx(ctx, &ttyctx);
                   1256:                        ttyctx.cell = &ci->gc;
1.118     nicm     1257:                        ttyctx.wrapped = ci->wrapped;
1.109     nicm     1258:                        ttyctx.ptr = ci->data;
                   1259:                        ttyctx.num = ci->used;
                   1260:                        tty_write(tty_cmd_cells, &ttyctx);
1.116     nicm     1261:
                   1262:                        items++;
                   1263:                        written += ci->used;
1.109     nicm     1264:
                   1265:                        TAILQ_REMOVE(&ctx->list[y].items, ci, entry);
                   1266:                        free(ci);
                   1267:                }
                   1268:        }
                   1269:        s->cx = cx; s->cy = cy;
1.116     nicm     1270:
                   1271:        log_debug("%s: flushed %u items (%zu bytes)", __func__, items, written);
                   1272:        ctx->written += written;
1.109     nicm     1273: }
                   1274:
                   1275: /* Finish and store collected cells. */
                   1276: void
                   1277: screen_write_collect_end(struct screen_write_ctx *ctx)
                   1278: {
                   1279:        struct screen                           *s = ctx->s;
                   1280:        struct screen_write_collect_item        *ci = ctx->item;
                   1281:        struct grid_cell                         gc;
1.131     nicm     1282:        u_int                                    xx;
1.109     nicm     1283:
                   1284:        if (ci->used == 0)
                   1285:                return;
                   1286:        ci->data[ci->used] = '\0';
                   1287:
                   1288:        ci->x = s->cx;
                   1289:        TAILQ_INSERT_TAIL(&ctx->list[s->cy].items, ci, entry);
                   1290:        ctx->item = xcalloc(1, sizeof *ctx->item);
                   1291:
                   1292:        log_debug("%s: %u %s (at %u,%u)", __func__, ci->used, ci->data, s->cx,
                   1293:            s->cy);
                   1294:
1.131     nicm     1295:        if (s->cx != 0) {
                   1296:                for (xx = s->cx; xx > 0; xx--) {
                   1297:                        grid_view_get_cell(s->grid, xx, s->cy, &gc);
                   1298:                        if (~gc.flags & GRID_FLAG_PADDING)
                   1299:                                break;
1.136   ! nicm     1300:                        grid_view_set_cell(s->grid, xx, s->cy,
        !          1301:                            &grid_default_cell);
1.131     nicm     1302:                }
                   1303:                if (gc.data.width > 1)
1.136   ! nicm     1304:                        grid_view_set_cell(s->grid, xx, s->cy,
        !          1305:                            &grid_default_cell);
1.131     nicm     1306:        }
                   1307:
1.109     nicm     1308:        memcpy(&gc, &ci->gc, sizeof gc);
                   1309:        grid_view_set_cells(s->grid, s->cx, s->cy, &gc, ci->data, ci->used);
                   1310:        s->cx += ci->used;
1.131     nicm     1311:
                   1312:        for (xx = s->cx; xx < screen_size_x(s); xx++) {
                   1313:                grid_view_get_cell(s->grid, xx, s->cy, &gc);
                   1314:                if (~gc.flags & GRID_FLAG_PADDING)
                   1315:                        break;
                   1316:                grid_view_set_cell(s->grid, xx, s->cy, &grid_default_cell);
                   1317:        }
1.109     nicm     1318: }
                   1319:
                   1320: /* Write cell data, collecting if necessary. */
                   1321: void
                   1322: screen_write_collect_add(struct screen_write_ctx *ctx,
                   1323:     const struct grid_cell *gc)
                   1324: {
                   1325:        struct screen                           *s = ctx->s;
                   1326:        struct screen_write_collect_item        *ci;
                   1327:        u_int                                    sx = screen_size_x(s);
                   1328:        int                                      collect;
                   1329:
                   1330:        /*
                   1331:         * Don't need to check that the attributes and whatnot are still the
1.116     nicm     1332:         * same - input_parse will end the collection when anything that isn't
                   1333:         * a plain character is encountered. Also nothing should make it here
                   1334:         * that isn't a single ASCII character.
1.109     nicm     1335:         */
                   1336:
                   1337:        collect = 1;
1.136   ! nicm     1338:        if (gc->data.width != 1 || gc->data.size != 1 || *gc->data.data >= 0x7f)
1.109     nicm     1339:                collect = 0;
                   1340:        else if (gc->attr & GRID_ATTR_CHARSET)
                   1341:                collect = 0;
                   1342:        else if (~s->mode & MODE_WRAP)
                   1343:                collect = 0;
                   1344:        else if (s->mode & MODE_INSERT)
                   1345:                collect = 0;
                   1346:        else if (s->sel.flag)
                   1347:                collect = 0;
                   1348:        if (!collect) {
                   1349:                screen_write_collect_end(ctx);
1.116     nicm     1350:                screen_write_collect_flush(ctx, 0);
1.109     nicm     1351:                screen_write_cell(ctx, gc);
                   1352:                return;
                   1353:        }
                   1354:        ctx->cells++;
                   1355:
                   1356:        if (s->cx > sx - 1 || ctx->item->used > sx - 1 - s->cx)
                   1357:                screen_write_collect_end(ctx);
1.118     nicm     1358:        ci = ctx->item; /* may have changed */
                   1359:
1.109     nicm     1360:        if (s->cx > sx - 1) {
1.114     nicm     1361:                log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1.118     nicm     1362:                ci->wrapped = 1;
1.122     nicm     1363:                screen_write_linefeed(ctx, 1, 8);
1.109     nicm     1364:                s->cx = 0;
                   1365:        }
                   1366:
                   1367:        if (ci->used == 0)
                   1368:                memcpy(&ci->gc, gc, sizeof ci->gc);
                   1369:        ci->data[ci->used++] = gc->data.data[0];
                   1370:        if (ci->used == (sizeof ci->data) - 1)
                   1371:                screen_write_collect_end(ctx);
                   1372: }
                   1373:
1.1       nicm     1374: /* Write cell data. */
                   1375: void
1.60      nicm     1376: screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1.1       nicm     1377: {
                   1378:        struct screen           *s = ctx->s;
                   1379:        struct grid             *gd = s->grid;
1.109     nicm     1380:        struct grid_line        *gl;
                   1381:        struct grid_cell_entry  *gce;
                   1382:        struct grid_cell         tmp_gc, now_gc;
1.15      nicm     1383:        struct tty_ctx           ttyctx;
1.92      nicm     1384:        u_int                    sx = screen_size_x(s), sy = screen_size_y(s);
1.115     nicm     1385:        u_int                    width = gc->data.width, xx, last, cx, cy;
1.109     nicm     1386:        int                      selected, skip = 1;
1.1       nicm     1387:
1.109     nicm     1388:        /* Ignore padding cells. */
1.1       nicm     1389:        if (gc->flags & GRID_FLAG_PADDING)
                   1390:                return;
1.109     nicm     1391:        ctx->cells++;
1.32      nicm     1392:
1.109     nicm     1393:        /* If the width is zero, combine onto the previous character. */
1.1       nicm     1394:        if (width == 0) {
1.112     nicm     1395:                screen_write_collect_flush(ctx, 0);
1.104     nicm     1396:                if ((gc = screen_write_combine(ctx, &gc->data, &xx)) != 0) {
1.115     nicm     1397:                        cx = s->cx; cy = s->cy;
1.104     nicm     1398:                        screen_write_cursormove(ctx, xx, s->cy);
1.87      nicm     1399:                        screen_write_initctx(ctx, &ttyctx);
1.104     nicm     1400:                        ttyctx.cell = gc;
                   1401:                        tty_write(tty_cmd_cell, &ttyctx);
1.115     nicm     1402:                        s->cx = cx; s->cy = cy;
1.1       nicm     1403:                }
                   1404:                return;
                   1405:        }
1.112     nicm     1406:
                   1407:        /* Flush any existing scrolling. */
                   1408:        screen_write_collect_flush(ctx, 1);
1.1       nicm     1409:
1.109     nicm     1410:        /* If this character doesn't fit, ignore it. */
                   1411:        if ((~s->mode & MODE_WRAP) &&
                   1412:            width > 1 &&
                   1413:            (width > sx || (s->cx != sx && s->cx > sx - width)))
                   1414:                return;
                   1415:
1.6       nicm     1416:        /* If in insert mode, make space for the cells. */
1.98      nicm     1417:        if (s->mode & MODE_INSERT) {
1.109     nicm     1418:                grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8);
                   1419:                skip = 0;
                   1420:        }
1.6       nicm     1421:
1.20      nicm     1422:        /* Check this will fit on the current line and wrap if not. */
1.92      nicm     1423:        if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
1.128     nicm     1424:                log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1.122     nicm     1425:                screen_write_linefeed(ctx, 1, 8);
1.109     nicm     1426:                s->cx = 0;
1.128     nicm     1427:                screen_write_collect_flush(ctx, 1);
1.1       nicm     1428:        }
                   1429:
1.64      nicm     1430:        /* Sanity check cursor position. */
1.92      nicm     1431:        if (s->cx > sx - width || s->cy > sy - 1)
1.1       nicm     1432:                return;
1.106     nicm     1433:        screen_write_initctx(ctx, &ttyctx);
                   1434:
1.1       nicm     1435:        /* Handle overwriting of UTF-8 characters. */
1.92      nicm     1436:        gl = &s->grid->linedata[s->grid->hsize + s->cy];
                   1437:        if (gl->flags & GRID_LINE_EXTENDED) {
                   1438:                grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
                   1439:                if (screen_write_overwrite(ctx, &now_gc, width))
                   1440:                        skip = 0;
                   1441:        }
1.1       nicm     1442:
                   1443:        /*
                   1444:         * If the new character is UTF-8 wide, fill in padding cells. Have
                   1445:         * already ensured there is enough room.
                   1446:         */
1.89      nicm     1447:        for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1.131     nicm     1448:                log_debug("%s: new padding at %u,%u", __func__, xx, s->cy);
1.88      nicm     1449:                grid_view_set_cell(gd, xx, s->cy, &screen_write_pad_cell);
1.89      nicm     1450:                skip = 0;
                   1451:        }
                   1452:
                   1453:        /* If no change, do not draw. */
1.92      nicm     1454:        if (skip) {
                   1455:                if (s->cx >= gl->cellsize)
                   1456:                        skip = grid_cells_equal(gc, &grid_default_cell);
                   1457:                else {
                   1458:                        gce = &gl->celldata[s->cx];
                   1459:                        if (gce->flags & GRID_FLAG_EXTENDED)
                   1460:                                skip = 0;
1.95      nicm     1461:                        else if (gc->flags != gce->flags)
1.92      nicm     1462:                                skip = 0;
                   1463:                        else if (gc->attr != gce->data.attr)
                   1464:                                skip = 0;
                   1465:                        else if (gc->fg != gce->data.fg)
                   1466:                                skip = 0;
                   1467:                        else if (gc->bg != gce->data.bg)
                   1468:                                skip = 0;
1.95      nicm     1469:                        else if (gc->data.width != 1)
                   1470:                                skip = 0;
1.109     nicm     1471:                        else if (gc->data.size != 1)
                   1472:                                skip = 0;
1.95      nicm     1473:                        else if (gce->data.data != gc->data.data[0])
1.92      nicm     1474:                                skip = 0;
                   1475:                }
                   1476:        }
1.1       nicm     1477:
1.127     nicm     1478:        /* Update the selected flag and set the cell. */
1.90      nicm     1479:        selected = screen_check_selection(s, s->cx, s->cy);
1.109     nicm     1480:        if (selected && (~gc->flags & GRID_FLAG_SELECTED)) {
1.90      nicm     1481:                memcpy(&tmp_gc, gc, sizeof tmp_gc);
                   1482:                tmp_gc.flags |= GRID_FLAG_SELECTED;
                   1483:                grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1.109     nicm     1484:        } else if (!selected && (gc->flags & GRID_FLAG_SELECTED)) {
1.90      nicm     1485:                memcpy(&tmp_gc, gc, sizeof tmp_gc);
                   1486:                tmp_gc.flags &= ~GRID_FLAG_SELECTED;
                   1487:                grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
                   1488:        } else if (!skip)
1.89      nicm     1489:                grid_view_set_cell(gd, s->cx, s->cy, gc);
1.109     nicm     1490:        if (selected)
                   1491:                skip = 0;
1.1       nicm     1492:
1.64      nicm     1493:        /*
                   1494:         * Move the cursor. If not wrapping, stick at the last character and
                   1495:         * replace it.
                   1496:         */
1.65      nicm     1497:        last = !(s->mode & MODE_WRAP);
1.92      nicm     1498:        if (s->cx <= sx - last - width)
1.64      nicm     1499:                s->cx += width;
                   1500:        else
1.92      nicm     1501:                s->cx = sx - last;
1.1       nicm     1502:
1.89      nicm     1503:        /* Create space for character in insert mode. */
1.109     nicm     1504:        if (s->mode & MODE_INSERT) {
1.113     nicm     1505:                screen_write_collect_flush(ctx, 0);
1.17      nicm     1506:                ttyctx.num = width;
                   1507:                tty_write(tty_cmd_insertcharacter, &ttyctx);
                   1508:        }
1.89      nicm     1509:
                   1510:        /* Write to the screen. */
1.109     nicm     1511:        if (!skip) {
                   1512:                if (selected) {
                   1513:                        screen_select_cell(s, &tmp_gc, gc);
                   1514:                        ttyctx.cell = &tmp_gc;
                   1515:                } else
                   1516:                        ttyctx.cell = gc;
1.16      nicm     1517:                tty_write(tty_cmd_cell, &ttyctx);
1.92      nicm     1518:                ctx->written++;
                   1519:        } else
                   1520:                ctx->skipped++;
1.35      nicm     1521: }
                   1522:
                   1523: /* Combine a UTF-8 zero-width character onto the previous. */
1.104     nicm     1524: static const struct grid_cell *
                   1525: screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud,
                   1526:     u_int *xx)
1.35      nicm     1527: {
                   1528:        struct screen           *s = ctx->s;
                   1529:        struct grid             *gd = s->grid;
1.104     nicm     1530:        static struct grid_cell  gc;
                   1531:        u_int                    n;
1.35      nicm     1532:
                   1533:        /* Can't combine if at 0. */
                   1534:        if (s->cx == 0)
1.104     nicm     1535:                return (NULL);
1.35      nicm     1536:
1.60      nicm     1537:        /* Empty data is out. */
                   1538:        if (ud->size == 0)
1.37      nicm     1539:                fatalx("UTF-8 data empty");
                   1540:
1.60      nicm     1541:        /* Retrieve the previous cell. */
1.119     nicm     1542:        for (n = 1; n <= s->cx; n++) {
1.104     nicm     1543:                grid_view_get_cell(gd, s->cx - n, s->cy, &gc);
                   1544:                if (~gc.flags & GRID_FLAG_PADDING)
                   1545:                        break;
                   1546:        }
1.119     nicm     1547:        if (n > s->cx)
1.104     nicm     1548:                return (NULL);
                   1549:        *xx = s->cx - n;
1.35      nicm     1550:
1.60      nicm     1551:        /* Check there is enough space. */
1.77      nicm     1552:        if (gc.data.size + ud->size > sizeof gc.data.data)
1.104     nicm     1553:                return (NULL);
                   1554:
                   1555:        log_debug("%s: %.*s onto %.*s at %u,%u", __func__, (int)ud->size,
                   1556:            ud->data, (int)gc.data.size, gc.data.data, *xx, s->cy);
1.35      nicm     1557:
1.77      nicm     1558:        /* Append the data. */
                   1559:        memcpy(gc.data.data + gc.data.size, ud->data, ud->size);
                   1560:        gc.data.size += ud->size;
                   1561:
                   1562:        /* Set the new cell. */
1.104     nicm     1563:        grid_view_set_cell(gd, *xx, s->cy, &gc);
1.35      nicm     1564:
1.104     nicm     1565:        return (&gc);
1.1       nicm     1566: }
                   1567:
                   1568: /*
                   1569:  * UTF-8 wide characters are a bit of an annoyance. They take up more than one
                   1570:  * cell on the screen, so following cells must not be drawn by marking them as
                   1571:  * padding.
                   1572:  *
                   1573:  * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
                   1574:  * character, it is necessary to also overwrite any other cells which covered
                   1575:  * by the same character.
                   1576:  */
1.89      nicm     1577: static int
                   1578: screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
                   1579:     u_int width)
1.1       nicm     1580: {
                   1581:        struct screen           *s = ctx->s;
                   1582:        struct grid             *gd = s->grid;
1.89      nicm     1583:        struct grid_cell         tmp_gc;
1.1       nicm     1584:        u_int                    xx;
1.89      nicm     1585:        int                      done = 0;
1.1       nicm     1586:
1.89      nicm     1587:        if (gc->flags & GRID_FLAG_PADDING) {
1.1       nicm     1588:                /*
                   1589:                 * A padding cell, so clear any following and leading padding
                   1590:                 * cells back to the character. Don't overwrite the current
                   1591:                 * cell as that happens later anyway.
                   1592:                 */
                   1593:                xx = s->cx + 1;
                   1594:                while (--xx > 0) {
1.89      nicm     1595:                        grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
                   1596:                        if (~tmp_gc.flags & GRID_FLAG_PADDING)
1.1       nicm     1597:                                break;
1.131     nicm     1598:                        log_debug("%s: padding at %u,%u", __func__, xx, s->cy);
1.1       nicm     1599:                        grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
                   1600:                }
                   1601:
                   1602:                /* Overwrite the character at the start of this padding. */
1.131     nicm     1603:                log_debug("%s: character at %u,%u", __func__, xx, s->cy);
1.1       nicm     1604:                grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1.89      nicm     1605:                done = 1;
1.43      nicm     1606:        }
1.1       nicm     1607:
1.43      nicm     1608:        /*
1.95      nicm     1609:         * Overwrite any padding cells that belong to any UTF-8 characters
                   1610:         * we'll be overwriting with the current character.
1.43      nicm     1611:         */
1.95      nicm     1612:        if (width != 1 ||
                   1613:            gc->data.width != 1 ||
                   1614:            gc->flags & GRID_FLAG_PADDING) {
1.89      nicm     1615:                xx = s->cx + width - 1;
                   1616:                while (++xx < screen_size_x(s)) {
                   1617:                        grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
                   1618:                        if (~tmp_gc.flags & GRID_FLAG_PADDING)
                   1619:                                break;
1.131     nicm     1620:                        log_debug("%s: overwrite at %u,%u", __func__, xx, s->cy);
1.89      nicm     1621:                        grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
                   1622:                        done = 1;
                   1623:                }
1.1       nicm     1624:        }
1.89      nicm     1625:
                   1626:        return (done);
1.50      nicm     1627: }
                   1628:
1.107     nicm     1629: /* Set external clipboard. */
1.50      nicm     1630: void
                   1631: screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len)
                   1632: {
                   1633:        struct tty_ctx  ttyctx;
                   1634:
1.87      nicm     1635:        screen_write_initctx(ctx, &ttyctx);
1.50      nicm     1636:        ttyctx.ptr = str;
                   1637:        ttyctx.num = len;
                   1638:
                   1639:        tty_write(tty_cmd_setselection, &ttyctx);
1.47      nicm     1640: }
                   1641:
1.107     nicm     1642: /* Write unmodified string. */
1.47      nicm     1643: void
                   1644: screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len)
                   1645: {
1.87      nicm     1646:        struct tty_ctx  ttyctx;
1.47      nicm     1647:
1.87      nicm     1648:        screen_write_initctx(ctx, &ttyctx);
1.47      nicm     1649:        ttyctx.ptr = str;
                   1650:        ttyctx.num = len;
                   1651:
                   1652:        tty_write(tty_cmd_rawstring, &ttyctx);
1.1       nicm     1653: }