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

1.98    ! nicm        1: /* $OpenBSD: screen-write.c,v 1.97 2016/10/12 13:24:07 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 *);
                     28: static void    screen_write_save_last(struct screen_write_ctx *,
                     29:                    struct tty_ctx *);
1.92      nicm       30: static void    screen_write_flush(struct screen_write_ctx *);
1.87      nicm       31:
1.89      nicm       32: static int     screen_write_overwrite(struct screen_write_ctx *,
                     33:                    struct grid_cell *, u_int);
1.87      nicm       34: static int     screen_write_combine(struct screen_write_ctx *,
                     35:                    const struct utf8_data *);
1.1       nicm       36:
1.88      nicm       37: static const struct grid_cell screen_write_pad_cell = {
1.91      nicm       38:        GRID_FLAG_PADDING, 0, 8, 8, { { 0 }, 0, 0, 0 }
1.88      nicm       39: };
                     40:
1.92      nicm       41: #define screen_dirty_bit(s, x, y) (((y) * screen_size_x(s)) + (x))
                     42: #define screen_dirty_clear(s, sx, sy, ex, ey)                  \
                     43:        do {                                                    \
                     44:                if (s->dirty != NULL) {                         \
                     45:                        bit_nclear(s->dirty,                    \
                     46:                            screen_dirty_bit(s, sx, sy),        \
                     47:                            screen_dirty_bit(s, ex, ey));       \
                     48:                }                                               \
                     49:        } while (0)
                     50:
                     51: /* Initialize writing with a window. */
1.1       nicm       52: void
1.71      nicm       53: screen_write_start(struct screen_write_ctx *ctx, struct window_pane *wp,
                     54:     struct screen *s)
1.1       nicm       55: {
1.92      nicm       56:        u_int            size;
                     57:        char             tmp[16];
                     58:        const char      *cp = tmp;
                     59:
1.1       nicm       60:        ctx->wp = wp;
                     61:        if (wp != NULL && s == NULL)
                     62:                ctx->s = wp->screen;
                     63:        else
                     64:                ctx->s = s;
1.92      nicm       65:
                     66:        size = screen_size_x(ctx->s) * screen_size_y(ctx->s);
                     67:        if (ctx->s->dirtysize != size) {
                     68:                free(ctx->s->dirty);
                     69:                ctx->s->dirty = NULL;
                     70:                ctx->s->dirtysize = size;
                     71:        }
                     72:        ctx->dirty = 0;
                     73:
                     74:        ctx->cells = ctx->written = ctx->skipped = 0;
                     75:
                     76:        if (wp == NULL)
                     77:                cp = "no pane";
                     78:        else
                     79:                snprintf(tmp, sizeof tmp, "pane %%%u", wp->id);
                     80:        log_debug("%s: size %ux%u, %s", __func__, screen_size_x(ctx->s),
                     81:            screen_size_y(ctx->s), cp);
1.1       nicm       82: }
                     83:
                     84: /* Finish writing. */
                     85: void
1.92      nicm       86: screen_write_stop(struct screen_write_ctx *ctx)
                     87: {
                     88:        screen_write_flush(ctx);
                     89:
                     90:        log_debug("%s: %u of %u written (dirty %u, skipped %u)", __func__,
                     91:            ctx->written, ctx->cells, ctx->cells - ctx->written, ctx->skipped);
                     92: }
                     93:
                     94: /* Flush outstanding cell writes. */
                     95: static void
                     96: screen_write_flush(struct screen_write_ctx *ctx)
1.1       nicm       97: {
1.92      nicm       98:        struct screen   *s = ctx->s;
                     99:        struct tty_ctx   ttyctx;
                    100:        u_int            x, y, offset, cx, cy, dirty;
                    101:        struct grid_cell gc;
                    102:
                    103:        if (ctx->dirty == 0)
                    104:                return;
                    105:        dirty = 0;
1.98    ! nicm      106:        log_debug("%s: dirty %u", __func__, ctx->dirty);
1.92      nicm      107:
                    108:        cx = s->cx;
                    109:        cy = s->cy;
                    110:
                    111:        offset = 0;
                    112:        for (y = 0; y < screen_size_y(s); y++) {
                    113:                for (x = 0; x < screen_size_x(s); x++) {
                    114:                        offset++;
                    115:                        if (!bit_test(s->dirty, offset - 1))
                    116:                                continue;
                    117:                        bit_clear(s->dirty, offset - 1);
                    118:
                    119:                        screen_write_cursormove(ctx, x, y);
                    120:                        grid_view_get_cell(s->grid, x, y, &gc);
                    121:
                    122:                        screen_write_initctx(ctx, &ttyctx);
                    123:                        ttyctx.cell = &gc;
                    124:                        tty_write(tty_cmd_cell, &ttyctx);
                    125:                        ctx->written++;
                    126:
                    127:                        if (++dirty == ctx->dirty)
                    128:                                break;
                    129:                }
                    130:                if (dirty == ctx->dirty)
                    131:                        break;
                    132:        }
                    133:
                    134:        s->cx = cx;
                    135:        s->cy = cy;
1.52      nicm      136: }
                    137:
                    138: /* Reset screen state. */
                    139: void
                    140: screen_write_reset(struct screen_write_ctx *ctx)
                    141: {
1.61      nicm      142:        struct screen   *s = ctx->s;
1.52      nicm      143:
1.61      nicm      144:        screen_reset_tabs(s);
                    145:        screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
1.63      nicm      146:
1.67      nicm      147:        s->mode &= ~(MODE_INSERT|MODE_KCURSOR|MODE_KKEYPAD|MODE_FOCUSON);
1.82      nicm      148:        s->mode &= ~(ALL_MOUSE_MODES|MODE_MOUSE_UTF8|MODE_MOUSE_SGR);
1.52      nicm      149:
                    150:        screen_write_clearscreen(ctx);
                    151:        screen_write_cursormove(ctx, 0, 0);
1.1       nicm      152: }
                    153:
                    154: /* Write character. */
                    155: void
1.86      nicm      156: screen_write_putc(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
1.69      nicm      157:     u_char ch)
1.1       nicm      158: {
1.86      nicm      159:        struct grid_cell        gc;
                    160:
                    161:        memcpy(&gc, gcp, sizeof gc);
                    162:
                    163:        utf8_set(&gc.data, ch);
                    164:        screen_write_cell(ctx, &gc);
1.1       nicm      165: }
                    166:
1.24      nicm      167: /* Calculate string length, with embedded formatting. */
1.71      nicm      168: size_t
1.75      nicm      169: screen_write_cstrlen(const char *fmt, ...)
1.24      nicm      170: {
                    171:        va_list ap;
                    172:        char   *msg, *msg2, *ptr, *ptr2;
                    173:        size_t  size;
                    174:
                    175:        va_start(ap, fmt);
                    176:        xvasprintf(&msg, fmt, ap);
                    177:        va_end(ap);
                    178:        msg2 = xmalloc(strlen(msg) + 1);
                    179:
                    180:        ptr = msg;
                    181:        ptr2 = msg2;
                    182:        while (*ptr != '\0') {
                    183:                if (ptr[0] == '#' && ptr[1] == '[') {
                    184:                        while (*ptr != ']' && *ptr != '\0')
                    185:                                ptr++;
                    186:                        if (*ptr == ']')
                    187:                                ptr++;
                    188:                        continue;
                    189:                }
                    190:                *ptr2++ = *ptr++;
                    191:        }
                    192:        *ptr2 = '\0';
                    193:
1.75      nicm      194:        size = screen_write_strlen("%s", msg2);
1.24      nicm      195:
1.56      nicm      196:        free(msg);
                    197:        free(msg2);
1.24      nicm      198:
                    199:        return (size);
                    200: }
                    201:
1.2       nicm      202: /* Calculate string length. */
1.71      nicm      203: size_t
1.75      nicm      204: screen_write_strlen(const char *fmt, ...)
1.2       nicm      205: {
1.36      nicm      206:        va_list                 ap;
                    207:        char                   *msg;
1.76      nicm      208:        struct utf8_data        ud;
1.36      nicm      209:        u_char                 *ptr;
                    210:        size_t                  left, size = 0;
1.79      nicm      211:        enum utf8_state         more;
1.2       nicm      212:
                    213:        va_start(ap, fmt);
                    214:        xvasprintf(&msg, fmt, ap);
                    215:        va_end(ap);
                    216:
                    217:        ptr = msg;
                    218:        while (*ptr != '\0') {
1.79      nicm      219:                if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) {
1.36      nicm      220:                        ptr++;
1.2       nicm      221:
                    222:                        left = strlen(ptr);
1.77      nicm      223:                        if (left < (size_t)ud.size - 1)
1.36      nicm      224:                                break;
1.79      nicm      225:                        while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE)
1.2       nicm      226:                                ptr++;
1.36      nicm      227:                        ptr++;
                    228:
1.79      nicm      229:                        if (more == UTF8_DONE)
1.78      nicm      230:                                size += ud.width;
1.2       nicm      231:                } else {
1.75      nicm      232:                        if (*ptr > 0x1f && *ptr < 0x7f)
                    233:                                size++;
1.2       nicm      234:                        ptr++;
                    235:                }
1.7       ray       236:        }
1.2       nicm      237:
1.56      nicm      238:        free(msg);
1.2       nicm      239:        return (size);
                    240: }
                    241:
1.3       nicm      242: /* Write simple string (no UTF-8 or maximum length). */
1.71      nicm      243: void
1.86      nicm      244: screen_write_puts(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
1.71      nicm      245:     const char *fmt, ...)
1.1       nicm      246: {
                    247:        va_list ap;
                    248:
                    249:        va_start(ap, fmt);
1.86      nicm      250:        screen_write_vnputs(ctx, -1, gcp, fmt, ap);
1.2       nicm      251:        va_end(ap);
                    252: }
                    253:
                    254: /* Write string with length limit (-1 for unlimited). */
1.71      nicm      255: void
                    256: screen_write_nputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86      nicm      257:     const struct grid_cell *gcp, const char *fmt, ...)
1.2       nicm      258: {
                    259:        va_list ap;
                    260:
                    261:        va_start(ap, fmt);
1.86      nicm      262:        screen_write_vnputs(ctx, maxlen, gcp, fmt, ap);
1.2       nicm      263:        va_end(ap);
                    264: }
                    265:
                    266: void
1.3       nicm      267: screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86      nicm      268:     const struct grid_cell *gcp, const char *fmt, va_list ap)
1.2       nicm      269: {
1.86      nicm      270:        struct grid_cell        gc;
                    271:        struct utf8_data       *ud = &gc.data;
1.36      nicm      272:        char                   *msg;
                    273:        u_char                 *ptr;
                    274:        size_t                  left, size = 0;
1.79      nicm      275:        enum utf8_state         more;
1.2       nicm      276:
1.86      nicm      277:        memcpy(&gc, gcp, sizeof gc);
1.1       nicm      278:        xvasprintf(&msg, fmt, ap);
                    279:
1.2       nicm      280:        ptr = msg;
                    281:        while (*ptr != '\0') {
1.86      nicm      282:                if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
1.36      nicm      283:                        ptr++;
1.2       nicm      284:
                    285:                        left = strlen(ptr);
1.86      nicm      286:                        if (left < (size_t)ud->size - 1)
1.36      nicm      287:                                break;
1.86      nicm      288:                        while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
1.2       nicm      289:                                ptr++;
1.36      nicm      290:                        ptr++;
1.7       ray       291:
1.86      nicm      292:                        if (more != UTF8_DONE)
                    293:                                continue;
                    294:                        if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
                    295:                                while (size < (size_t)maxlen) {
                    296:                                        screen_write_putc(ctx, &gc, ' ');
                    297:                                        size++;
1.2       nicm      298:                                }
1.86      nicm      299:                                break;
1.2       nicm      300:                        }
1.86      nicm      301:                        size += ud->width;
                    302:                        screen_write_cell(ctx, &gc);
1.2       nicm      303:                } else {
1.86      nicm      304:                        if (maxlen > 0 && size + 1 > (size_t)maxlen)
1.2       nicm      305:                                break;
                    306:
1.57      nicm      307:                        if (*ptr == '\001')
1.86      nicm      308:                                gc.attr ^= GRID_ATTR_CHARSET;
1.75      nicm      309:                        else if (*ptr > 0x1f && *ptr < 0x7f) {
1.57      nicm      310:                                size++;
1.86      nicm      311:                                screen_write_putc(ctx, &gc, *ptr);
1.57      nicm      312:                        }
1.2       nicm      313:                        ptr++;
                    314:                }
                    315:        }
1.1       nicm      316:
1.56      nicm      317:        free(msg);
1.24      nicm      318: }
                    319:
                    320: /* Write string, similar to nputs, but with embedded formatting (#[]). */
1.71      nicm      321: void
1.75      nicm      322: screen_write_cnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86      nicm      323:     const struct grid_cell *gcp, const char *fmt, ...)
1.24      nicm      324: {
1.86      nicm      325:        struct grid_cell         gc;
                    326:        struct utf8_data        *ud = &gc.data;
1.24      nicm      327:        va_list                  ap;
                    328:        char                    *msg;
1.36      nicm      329:        u_char                  *ptr, *last;
1.24      nicm      330:        size_t                   left, size = 0;
1.79      nicm      331:        enum utf8_state          more;
1.24      nicm      332:
1.86      nicm      333:        memcpy(&gc, gcp, sizeof gc);
                    334:
1.24      nicm      335:        va_start(ap, fmt);
                    336:        xvasprintf(&msg, fmt, ap);
                    337:        va_end(ap);
                    338:
                    339:        ptr = msg;
                    340:        while (*ptr != '\0') {
                    341:                if (ptr[0] == '#' && ptr[1] == '[') {
                    342:                        ptr += 2;
                    343:                        last = ptr + strcspn(ptr, "]");
                    344:                        if (*last == '\0') {
                    345:                                /* No ]. Not much point in doing anything. */
                    346:                                break;
                    347:                        }
                    348:                        *last = '\0';
                    349:
1.86      nicm      350:                        style_parse(gcp, &gc, ptr);
1.24      nicm      351:                        ptr = last + 1;
                    352:                        continue;
                    353:                }
                    354:
1.86      nicm      355:                if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
1.36      nicm      356:                        ptr++;
1.24      nicm      357:
                    358:                        left = strlen(ptr);
1.86      nicm      359:                        if (left < (size_t)ud->size - 1)
1.36      nicm      360:                                break;
1.86      nicm      361:                        while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
1.24      nicm      362:                                ptr++;
1.36      nicm      363:                        ptr++;
1.24      nicm      364:
1.86      nicm      365:                        if (more != UTF8_DONE)
                    366:                                continue;
                    367:                        if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
                    368:                                while (size < (size_t)maxlen) {
                    369:                                        screen_write_putc(ctx, &gc, ' ');
                    370:                                        size++;
1.24      nicm      371:                                }
1.86      nicm      372:                                break;
1.24      nicm      373:                        }
1.86      nicm      374:                        size += ud->width;
                    375:                        screen_write_cell(ctx, &gc);
1.24      nicm      376:                } else {
1.86      nicm      377:                        if (maxlen > 0 && size + 1 > (size_t)maxlen)
1.24      nicm      378:                                break;
                    379:
1.75      nicm      380:                        if (*ptr > 0x1f && *ptr < 0x7f) {
                    381:                                size++;
1.86      nicm      382:                                screen_write_putc(ctx, &gc, *ptr);
1.75      nicm      383:                        }
1.24      nicm      384:                        ptr++;
                    385:                }
                    386:        }
                    387:
1.56      nicm      388:        free(msg);
1.1       nicm      389: }
                    390:
                    391: /* Copy from another screen. */
                    392: void
1.95      nicm      393: screen_write_copy(struct screen_write_ctx *ctx, struct screen *src, u_int px,
                    394:     u_int py, u_int nx, u_int ny)
1.1       nicm      395: {
                    396:        struct screen           *s = ctx->s;
                    397:        struct grid             *gd = src->grid;
1.77      nicm      398:        struct grid_cell         gc;
1.96      nicm      399:        u_int                    xx, yy, cx, cy;
1.1       nicm      400:
                    401:        cx = s->cx;
                    402:        cy = s->cy;
1.96      nicm      403:
1.1       nicm      404:        for (yy = py; yy < py + ny; yy++) {
1.96      nicm      405:                for (xx = px; xx < px + nx; xx++) {
                    406:                        grid_get_cell(gd, xx, yy, &gc);
                    407:                        screen_write_cell(ctx, &gc);
                    408:                }
                    409:
1.1       nicm      410:                cy++;
                    411:                screen_write_cursormove(ctx, cx, cy);
                    412:        }
                    413: }
                    414:
1.17      nicm      415: /* Set up context for TTY command. */
1.87      nicm      416: static void
                    417: screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx)
1.1       nicm      418: {
1.87      nicm      419:        struct screen   *s = ctx->s;
1.1       nicm      420:
1.17      nicm      421:        ttyctx->wp = ctx->wp;
1.1       nicm      422:
1.17      nicm      423:        ttyctx->ocx = s->cx;
                    424:        ttyctx->ocy = s->cy;
                    425:
                    426:        ttyctx->orlower = s->rlower;
                    427:        ttyctx->orupper = s->rupper;
1.87      nicm      428: }
1.31      nicm      429:
1.87      nicm      430: /* Save last cell on screen. */
                    431: static void
                    432: screen_write_save_last(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx)
                    433: {
                    434:        struct screen           *s = ctx->s;
                    435:        struct grid             *gd = s->grid;
                    436:        struct grid_cell         gc;
                    437:        u_int                    xx;
1.31      nicm      438:
1.77      nicm      439:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.38      nicm      440:        for (xx = 1; xx <= screen_size_x(s); xx++) {
1.77      nicm      441:                grid_view_get_cell(gd, screen_size_x(s) - xx, s->cy, &gc);
                    442:                if (~gc.flags & GRID_FLAG_PADDING)
1.31      nicm      443:                        break;
                    444:        }
1.77      nicm      445:        memcpy(&ttyctx->last_cell, &gc, sizeof ttyctx->last_cell);
1.1       nicm      446: }
                    447:
1.61      nicm      448: /* Set a mode. */
                    449: void
                    450: screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
                    451: {
                    452:        struct screen   *s = ctx->s;
                    453:
                    454:        s->mode |= mode;
                    455: }
                    456:
                    457: /* Clear a mode. */
                    458: void
                    459: screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
                    460: {
                    461:        struct screen   *s = ctx->s;
                    462:
                    463:        s->mode &= ~mode;
                    464: }
                    465:
1.1       nicm      466: /* Cursor up by ny. */
                    467: void
                    468: screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
                    469: {
                    470:        struct screen   *s = ctx->s;
                    471:
                    472:        if (ny == 0)
                    473:                ny = 1;
                    474:
1.12      nicm      475:        if (s->cy < s->rupper) {
                    476:                /* Above region. */
                    477:                if (ny > s->cy)
                    478:                        ny = s->cy;
                    479:        } else {
                    480:                /* Below region. */
                    481:                if (ny > s->cy - s->rupper)
                    482:                        ny = s->cy - s->rupper;
                    483:        }
1.66      nicm      484:        if (s->cx == screen_size_x(s))
                    485:            s->cx--;
1.1       nicm      486:        if (ny == 0)
                    487:                return;
                    488:
                    489:        s->cy -= ny;
                    490: }
                    491:
                    492: /* Cursor down by ny. */
                    493: void
                    494: screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
                    495: {
                    496:        struct screen   *s = ctx->s;
                    497:
                    498:        if (ny == 0)
                    499:                ny = 1;
                    500:
1.12      nicm      501:        if (s->cy > s->rlower) {
                    502:                /* Below region. */
                    503:                if (ny > screen_size_y(s) - 1 - s->cy)
                    504:                        ny = screen_size_y(s) - 1 - s->cy;
                    505:        } else {
                    506:                /* Above region. */
                    507:                if (ny > s->rlower - s->cy)
                    508:                        ny = s->rlower - s->cy;
                    509:        }
1.66      nicm      510:        if (s->cx == screen_size_x(s))
                    511:            s->cx--;
1.1       nicm      512:        if (ny == 0)
                    513:                return;
                    514:
                    515:        s->cy += ny;
                    516: }
                    517:
                    518: /* Cursor right by nx.  */
                    519: void
                    520: screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
                    521: {
                    522:        struct screen   *s = ctx->s;
                    523:
                    524:        if (nx == 0)
                    525:                nx = 1;
                    526:
                    527:        if (nx > screen_size_x(s) - 1 - s->cx)
                    528:                nx = screen_size_x(s) - 1 - s->cx;
                    529:        if (nx == 0)
                    530:                return;
                    531:
                    532:        s->cx += nx;
                    533: }
                    534:
                    535: /* Cursor left by nx. */
                    536: void
                    537: screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
                    538: {
                    539:        struct screen   *s = ctx->s;
                    540:
                    541:        if (nx == 0)
                    542:                nx = 1;
                    543:
                    544:        if (nx > s->cx)
                    545:                nx = s->cx;
                    546:        if (nx == 0)
                    547:                return;
                    548:
                    549:        s->cx -= nx;
1.5       nicm      550: }
                    551:
1.29      nicm      552: /* Backspace; cursor left unless at start of wrapped line when can move up. */
                    553: void
                    554: screen_write_backspace(struct screen_write_ctx *ctx)
                    555: {
                    556:        struct screen           *s = ctx->s;
                    557:        struct grid_line        *gl;
                    558:
                    559:        if (s->cx == 0) {
                    560:                if (s->cy == 0)
                    561:                        return;
                    562:                gl = &s->grid->linedata[s->grid->hsize + s->cy - 1];
                    563:                if (gl->flags & GRID_LINE_WRAPPED) {
                    564:                        s->cy--;
                    565:                        s->cx = screen_size_x(s) - 1;
                    566:                }
                    567:        } else
                    568:                s->cx--;
                    569: }
                    570:
1.5       nicm      571: /* VT100 alignment test. */
                    572: void
                    573: screen_write_alignmenttest(struct screen_write_ctx *ctx)
                    574: {
                    575:        struct screen           *s = ctx->s;
1.17      nicm      576:        struct tty_ctx           ttyctx;
1.5       nicm      577:        struct grid_cell         gc;
                    578:        u_int                    xx, yy;
1.92      nicm      579:        u_int                    sx = screen_size_x(s), sy = screen_size_y(s);
1.5       nicm      580:
1.87      nicm      581:        screen_write_initctx(ctx, &ttyctx);
1.17      nicm      582:
1.92      nicm      583:        screen_dirty_clear(s, 0, 0, sx - 1, sy  - 1);
                    584:
1.5       nicm      585:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.77      nicm      586:        utf8_set(&gc.data, 'E');
1.7       ray       587:
1.5       nicm      588:        for (yy = 0; yy < screen_size_y(s); yy++) {
                    589:                for (xx = 0; xx < screen_size_x(s); xx++)
                    590:                        grid_view_set_cell(s->grid, xx, yy, &gc);
                    591:        }
1.7       ray       592:
1.5       nicm      593:        s->cx = 0;
                    594:        s->cy = 0;
                    595:
                    596:        s->rupper = 0;
                    597:        s->rlower = screen_size_y(s) - 1;
                    598:
1.17      nicm      599:        tty_write(tty_cmd_alignmenttest, &ttyctx);
1.1       nicm      600: }
                    601:
                    602: /* Insert nx characters. */
                    603: void
                    604: screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx)
                    605: {
                    606:        struct screen   *s = ctx->s;
1.17      nicm      607:        struct tty_ctx   ttyctx;
1.1       nicm      608:
                    609:        if (nx == 0)
                    610:                nx = 1;
                    611:
1.9       nicm      612:        if (nx > screen_size_x(s) - s->cx)
                    613:                nx = screen_size_x(s) - s->cx;
1.1       nicm      614:        if (nx == 0)
                    615:                return;
                    616:
1.92      nicm      617:        screen_write_flush(ctx);
1.87      nicm      618:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      619:
                    620:        if (s->cx <= screen_size_x(s) - 1)
                    621:                grid_view_insert_cells(s->grid, s->cx, s->cy, nx);
                    622:
1.17      nicm      623:        ttyctx.num = nx;
                    624:        tty_write(tty_cmd_insertcharacter, &ttyctx);
1.1       nicm      625: }
                    626:
                    627: /* Delete nx characters. */
                    628: void
                    629: screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx)
                    630: {
                    631:        struct screen   *s = ctx->s;
1.17      nicm      632:        struct tty_ctx   ttyctx;
1.1       nicm      633:
                    634:        if (nx == 0)
                    635:                nx = 1;
                    636:
1.9       nicm      637:        if (nx > screen_size_x(s) - s->cx)
                    638:                nx = screen_size_x(s) - s->cx;
1.1       nicm      639:        if (nx == 0)
                    640:                return;
                    641:
1.92      nicm      642:        screen_write_flush(ctx);
1.87      nicm      643:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      644:
                    645:        if (s->cx <= screen_size_x(s) - 1)
                    646:                grid_view_delete_cells(s->grid, s->cx, s->cy, nx);
                    647:
1.17      nicm      648:        ttyctx.num = nx;
                    649:        tty_write(tty_cmd_deletecharacter, &ttyctx);
1.59      nicm      650: }
                    651:
                    652: /* Clear nx characters. */
                    653: void
                    654: screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx)
                    655: {
                    656:        struct screen   *s = ctx->s;
                    657:        struct tty_ctx   ttyctx;
                    658:
                    659:        if (nx == 0)
                    660:                nx = 1;
                    661:
                    662:        if (nx > screen_size_x(s) - s->cx)
                    663:                nx = screen_size_x(s) - s->cx;
                    664:        if (nx == 0)
                    665:                return;
                    666:
1.87      nicm      667:        screen_write_initctx(ctx, &ttyctx);
1.59      nicm      668:
1.92      nicm      669:        if (s->cx <= screen_size_x(s) - 1) {
                    670:                screen_dirty_clear(s, s->cx, s->cy, s->cx + nx - 1, s->cy);
1.59      nicm      671:                grid_view_clear(s->grid, s->cx, s->cy, nx, 1);
1.92      nicm      672:        } else
                    673:                return;
1.59      nicm      674:
                    675:        ttyctx.num = nx;
                    676:        tty_write(tty_cmd_clearcharacter, &ttyctx);
1.1       nicm      677: }
                    678:
                    679: /* Insert ny lines. */
                    680: void
                    681: screen_write_insertline(struct screen_write_ctx *ctx, u_int ny)
                    682: {
                    683:        struct screen   *s = ctx->s;
1.17      nicm      684:        struct tty_ctx   ttyctx;
1.1       nicm      685:
                    686:        if (ny == 0)
                    687:                ny = 1;
                    688:
1.11      nicm      689:        if (s->cy < s->rupper || s->cy > s->rlower) {
                    690:                if (ny > screen_size_y(s) - s->cy)
                    691:                        ny = screen_size_y(s) - s->cy;
                    692:                if (ny == 0)
                    693:                        return;
                    694:
1.92      nicm      695:                screen_write_flush(ctx);
1.87      nicm      696:                screen_write_initctx(ctx, &ttyctx);
1.11      nicm      697:
                    698:                grid_view_insert_lines(s->grid, s->cy, ny);
                    699:
1.17      nicm      700:                ttyctx.num = ny;
                    701:                tty_write(tty_cmd_insertline, &ttyctx);
1.11      nicm      702:                return;
                    703:        }
                    704:
                    705:        if (ny > s->rlower + 1 - s->cy)
                    706:                ny = s->rlower + 1 - s->cy;
1.1       nicm      707:        if (ny == 0)
                    708:                return;
1.41      nicm      709:
1.92      nicm      710:        screen_write_flush(ctx);
1.87      nicm      711:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      712:
                    713:        if (s->cy < s->rupper || s->cy > s->rlower)
                    714:                grid_view_insert_lines(s->grid, s->cy, ny);
1.10      nicm      715:        else
                    716:                grid_view_insert_lines_region(s->grid, s->rlower, s->cy, ny);
1.1       nicm      717:
1.17      nicm      718:        ttyctx.num = ny;
                    719:        tty_write(tty_cmd_insertline, &ttyctx);
1.1       nicm      720: }
                    721:
                    722: /* Delete ny lines. */
                    723: void
                    724: screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny)
                    725: {
                    726:        struct screen   *s = ctx->s;
1.17      nicm      727:        struct tty_ctx   ttyctx;
1.1       nicm      728:
                    729:        if (ny == 0)
                    730:                ny = 1;
                    731:
1.11      nicm      732:        if (s->cy < s->rupper || s->cy > s->rlower) {
                    733:                if (ny > screen_size_y(s) - s->cy)
                    734:                        ny = screen_size_y(s) - s->cy;
                    735:                if (ny == 0)
                    736:                        return;
                    737:
1.92      nicm      738:                screen_write_flush(ctx);
1.87      nicm      739:                screen_write_initctx(ctx, &ttyctx);
1.11      nicm      740:
                    741:                grid_view_delete_lines(s->grid, s->cy, ny);
                    742:
1.17      nicm      743:                ttyctx.num = ny;
                    744:                tty_write(tty_cmd_deleteline, &ttyctx);
1.11      nicm      745:                return;
                    746:        }
1.41      nicm      747:
1.11      nicm      748:        if (ny > s->rlower + 1 - s->cy)
                    749:                ny = s->rlower + 1 - s->cy;
1.1       nicm      750:        if (ny == 0)
                    751:                return;
                    752:
1.92      nicm      753:        screen_write_flush(ctx);
1.87      nicm      754:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      755:
                    756:        if (s->cy < s->rupper || s->cy > s->rlower)
                    757:                grid_view_delete_lines(s->grid, s->cy, ny);
1.10      nicm      758:        else
                    759:                grid_view_delete_lines_region(s->grid, s->rlower, s->cy, ny);
1.1       nicm      760:
1.17      nicm      761:        ttyctx.num = ny;
                    762:        tty_write(tty_cmd_deleteline, &ttyctx);
1.1       nicm      763: }
                    764:
                    765: /* Clear line at cursor. */
                    766: void
                    767: screen_write_clearline(struct screen_write_ctx *ctx)
                    768: {
1.92      nicm      769:        struct screen           *s = ctx->s;
                    770:        struct grid_line        *gl;
                    771:        struct tty_ctx           ttyctx;
                    772:        u_int                    sx = screen_size_x(s);
1.1       nicm      773:
1.87      nicm      774:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      775:
1.92      nicm      776:        gl = &s->grid->linedata[s->grid->hsize + s->cy];
                    777:        if (gl->cellsize != 0) {
                    778:                screen_dirty_clear(s, 0, s->cy, sx - 1, s->cy);
                    779:                grid_view_clear(s->grid, 0, s->cy, sx, 1);
                    780:        } else
                    781:                return;
1.1       nicm      782:
1.17      nicm      783:        tty_write(tty_cmd_clearline, &ttyctx);
1.1       nicm      784: }
                    785:
                    786: /* Clear to end of line from cursor. */
                    787: void
                    788: screen_write_clearendofline(struct screen_write_ctx *ctx)
                    789: {
1.92      nicm      790:        struct screen           *s = ctx->s;
                    791:        struct grid_line        *gl;
                    792:        struct tty_ctx           ttyctx;
                    793:        u_int                    sx = screen_size_x(s);
1.1       nicm      794:
1.87      nicm      795:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      796:
1.92      nicm      797:        gl = &s->grid->linedata[s->grid->hsize + s->cy];
                    798:        if (s->cx <= sx - 1 && s->cx < gl->cellsize) {
                    799:                screen_dirty_clear(s, s->cx, s->cy, sx - 1, s->cy);
1.1       nicm      800:                grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1);
1.92      nicm      801:        } else
                    802:                return;
1.1       nicm      803:
1.41      nicm      804:        tty_write(tty_cmd_clearendofline, &ttyctx);
1.1       nicm      805: }
                    806:
                    807: /* Clear to start of line from cursor. */
                    808: void
                    809: screen_write_clearstartofline(struct screen_write_ctx *ctx)
                    810: {
                    811:        struct screen   *s = ctx->s;
1.17      nicm      812:        struct tty_ctx   ttyctx;
1.92      nicm      813:        u_int            sx = screen_size_x(s);
1.1       nicm      814:
1.87      nicm      815:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      816:
1.92      nicm      817:        if (s->cx > sx - 1) {
                    818:                screen_dirty_clear(s, 0, s->cy, sx - 1, s->cy);
1.1       nicm      819:                grid_view_clear(s->grid, 0, s->cy, sx, 1);
1.92      nicm      820:        } else {
                    821:                screen_dirty_clear(s, 0, s->cy, s->cx, s->cy);
1.1       nicm      822:                grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1);
1.92      nicm      823:        }
1.1       nicm      824:
1.17      nicm      825:        tty_write(tty_cmd_clearstartofline, &ttyctx);
1.1       nicm      826: }
                    827:
                    828: /* Move cursor to px,py.  */
                    829: void
                    830: screen_write_cursormove(struct screen_write_ctx *ctx, u_int px, u_int py)
                    831: {
                    832:        struct screen   *s = ctx->s;
                    833:
                    834:        if (px > screen_size_x(s) - 1)
                    835:                px = screen_size_x(s) - 1;
                    836:        if (py > screen_size_y(s) - 1)
                    837:                py = screen_size_y(s) - 1;
                    838:
                    839:        s->cx = px;
                    840:        s->cy = py;
                    841: }
                    842:
                    843: /* Reverse index (up with scroll).  */
                    844: void
                    845: screen_write_reverseindex(struct screen_write_ctx *ctx)
                    846: {
                    847:        struct screen   *s = ctx->s;
1.17      nicm      848:        struct tty_ctx   ttyctx;
1.1       nicm      849:
1.87      nicm      850:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      851:
1.92      nicm      852:        if (s->cy == s->rupper) {
                    853:                screen_write_flush(ctx);
1.1       nicm      854:                grid_view_scroll_region_down(s->grid, s->rupper, s->rlower);
1.92      nicm      855:        } else if (s->cy > 0)
1.1       nicm      856:                s->cy--;
                    857:
1.17      nicm      858:        tty_write(tty_cmd_reverseindex, &ttyctx);
1.1       nicm      859: }
                    860:
                    861: /* Set scroll region. */
                    862: void
1.83      nicm      863: screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper,
                    864:     u_int rlower)
1.1       nicm      865: {
                    866:        struct screen   *s = ctx->s;
                    867:
                    868:        if (rupper > screen_size_y(s) - 1)
                    869:                rupper = screen_size_y(s) - 1;
                    870:        if (rlower > screen_size_y(s) - 1)
                    871:                rlower = screen_size_y(s) - 1;
1.13      nicm      872:        if (rupper >= rlower)   /* cannot be one line */
1.1       nicm      873:                return;
                    874:
                    875:        /* Cursor moves to top-left. */
                    876:        s->cx = 0;
                    877:        s->cy = 0;
                    878:
                    879:        s->rupper = rupper;
                    880:        s->rlower = rlower;
                    881: }
                    882:
1.34      nicm      883: /* Line feed. */
1.1       nicm      884: void
1.34      nicm      885: screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped)
1.1       nicm      886: {
1.20      nicm      887:        struct screen           *s = ctx->s;
                    888:        struct grid_line        *gl;
1.34      nicm      889:        struct tty_ctx           ttyctx;
1.92      nicm      890:        u_int                    sx = screen_size_x(s), sy = screen_size_y(s);
1.34      nicm      891:
1.87      nicm      892:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      893:
1.20      nicm      894:        gl = &s->grid->linedata[s->grid->hsize + s->cy];
                    895:        if (wrapped)
                    896:                gl->flags |= GRID_LINE_WRAPPED;
1.73      nicm      897:        else
                    898:                gl->flags &= ~GRID_LINE_WRAPPED;
1.20      nicm      899:
1.92      nicm      900:        if (s->cy == s->rlower) {
                    901:                screen_dirty_clear(s, 0, s->rupper, sx - 1, s->rupper);
                    902:                screen_write_flush(ctx);
1.1       nicm      903:                grid_view_scroll_region_up(s->grid, s->rupper, s->rlower);
1.92      nicm      904:        } else if (s->cy < sy - 1)
1.1       nicm      905:                s->cy++;
                    906:
1.34      nicm      907:        ttyctx.num = wrapped;
1.41      nicm      908:        tty_write(tty_cmd_linefeed, &ttyctx);
1.1       nicm      909: }
                    910:
                    911: /* Carriage return (cursor to start of line). */
                    912: void
                    913: screen_write_carriagereturn(struct screen_write_ctx *ctx)
                    914: {
                    915:        struct screen   *s = ctx->s;
                    916:
                    917:        s->cx = 0;
                    918: }
                    919:
                    920: /* Clear to end of screen from cursor. */
                    921: void
                    922: screen_write_clearendofscreen(struct screen_write_ctx *ctx)
                    923: {
                    924:        struct screen   *s = ctx->s;
1.17      nicm      925:        struct tty_ctx   ttyctx;
1.92      nicm      926:        u_int            sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm      927:
1.87      nicm      928:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      929:
1.46      nicm      930:        /* Scroll into history if it is enabled and clearing entire screen. */
1.92      nicm      931:        if (s->cy == 0 && s->grid->flags & GRID_HISTORY) {
                    932:                screen_dirty_clear(s, 0, 0, sx - 1, sy  - 1);
1.46      nicm      933:                grid_view_clear_history(s->grid);
1.92      nicm      934:        } else {
                    935:                if (s->cx <= sx - 1) {
                    936:                        screen_dirty_clear(s, s->cx, s->cy, sx - 1, s->cy);
1.46      nicm      937:                        grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1);
1.92      nicm      938:                }
                    939:                screen_dirty_clear(s, 0, s->cy + 1, sx - 1, sy - 1);
1.46      nicm      940:                grid_view_clear(s->grid, 0, s->cy + 1, sx, sy - (s->cy + 1));
                    941:        }
1.1       nicm      942:
1.17      nicm      943:        tty_write(tty_cmd_clearendofscreen, &ttyctx);
1.1       nicm      944: }
                    945:
                    946: /* Clear to start of screen. */
                    947: void
                    948: screen_write_clearstartofscreen(struct screen_write_ctx *ctx)
                    949: {
                    950:        struct screen   *s = ctx->s;
1.17      nicm      951:        struct tty_ctx   ttyctx;
1.92      nicm      952:        u_int            sx = screen_size_x(s);
1.1       nicm      953:
1.87      nicm      954:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      955:
1.92      nicm      956:        if (s->cy > 0) {
                    957:                screen_dirty_clear(s, 0, 0, sx - 1, s->cy);
1.4       nicm      958:                grid_view_clear(s->grid, 0, 0, sx, s->cy);
1.92      nicm      959:        }
                    960:        if (s->cx > sx - 1) {
                    961:                screen_dirty_clear(s, 0, s->cy, sx - 1, s->cy);
1.1       nicm      962:                grid_view_clear(s->grid, 0, s->cy, sx, 1);
1.92      nicm      963:        } else {
                    964:                screen_dirty_clear(s, 0, s->cy, s->cx, s->cy);
1.4       nicm      965:                grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1);
1.92      nicm      966:        }
1.1       nicm      967:
1.17      nicm      968:        tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1.1       nicm      969: }
                    970:
                    971: /* Clear entire screen. */
                    972: void
                    973: screen_write_clearscreen(struct screen_write_ctx *ctx)
                    974: {
                    975:        struct screen   *s = ctx->s;
1.17      nicm      976:        struct tty_ctx   ttyctx;
1.92      nicm      977:        u_int            sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm      978:
1.87      nicm      979:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      980:
1.92      nicm      981:        screen_dirty_clear(s, 0, 0, sx - 1, sy  - 1);
                    982:
1.46      nicm      983:        /* Scroll into history if it is enabled. */
                    984:        if (s->grid->flags & GRID_HISTORY)
                    985:                grid_view_clear_history(s->grid);
1.83      nicm      986:        else
                    987:                grid_view_clear(s->grid, 0, 0, sx, sy);
1.1       nicm      988:
1.17      nicm      989:        tty_write(tty_cmd_clearscreen, &ttyctx);
1.51      nicm      990: }
                    991:
                    992: /* Clear entire history. */
                    993: void
                    994: screen_write_clearhistory(struct screen_write_ctx *ctx)
                    995: {
                    996:        struct screen   *s = ctx->s;
                    997:        struct grid     *gd = s->grid;
                    998:
                    999:        grid_move_lines(gd, 0, gd->hsize, gd->sy);
1.93      nicm     1000:        gd->hscrolled = gd->hsize = 0;
1.1       nicm     1001: }
                   1002:
                   1003: /* Write cell data. */
                   1004: void
1.60      nicm     1005: screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1.1       nicm     1006: {
                   1007:        struct screen           *s = ctx->s;
                   1008:        struct grid             *gd = s->grid;
1.15      nicm     1009:        struct tty_ctx           ttyctx;
1.64      nicm     1010:        u_int                    width, xx, last;
1.92      nicm     1011:        u_int                    sx = screen_size_x(s), sy = screen_size_y(s);
                   1012:        struct grid_line        *gl;
1.89      nicm     1013:        struct grid_cell         tmp_gc, now_gc;
1.92      nicm     1014:        struct grid_cell_entry  *gce;
                   1015:        int                      insert, skip, selected, wrapped = 0;
                   1016:
                   1017:        ctx->cells++;
1.1       nicm     1018:
                   1019:        /* Ignore padding. */
                   1020:        if (gc->flags & GRID_FLAG_PADDING)
                   1021:                return;
1.77      nicm     1022:        width = gc->data.width;
1.1       nicm     1023:
1.32      nicm     1024:        /*
                   1025:         * If this is a wide character and there is no room on the screen, for
                   1026:         * the entire character, don't print it.
                   1027:         */
1.92      nicm     1028:        if (!(s->mode & MODE_WRAP) && (width > 1 &&
                   1029:            (width > sx || (s->cx != sx && s->cx > sx - width))))
1.32      nicm     1030:                return;
                   1031:
1.35      nicm     1032:        /*
                   1033:         * If the width is zero, combine onto the previous character, if
1.41      nicm     1034:         * there is space.
1.35      nicm     1035:         */
1.1       nicm     1036:        if (width == 0) {
1.77      nicm     1037:                if (screen_write_combine(ctx, &gc->data) == 0) {
1.87      nicm     1038:                        screen_write_initctx(ctx, &ttyctx);
1.35      nicm     1039:                        tty_write(tty_cmd_utf8character, &ttyctx);
1.1       nicm     1040:                }
                   1041:                return;
                   1042:        }
                   1043:
1.89      nicm     1044:        /* Initialise the redraw context. */
1.87      nicm     1045:        screen_write_initctx(ctx, &ttyctx);
1.55      nicm     1046:
1.6       nicm     1047:        /* If in insert mode, make space for the cells. */
1.98    ! nicm     1048:        if (s->mode & MODE_INSERT) {
        !          1049:                if (s->cx <= sx - width) {
        !          1050:                        screen_write_flush(ctx);
        !          1051:                        xx = sx - s->cx - width;
        !          1052:                        grid_view_insert_cells(s->grid, s->cx, s->cy, xx);
        !          1053:                }
1.6       nicm     1054:                insert = 1;
1.64      nicm     1055:        } else
                   1056:                insert = 0;
1.89      nicm     1057:        skip = !insert;
1.6       nicm     1058:
1.20      nicm     1059:        /* Check this will fit on the current line and wrap if not. */
1.92      nicm     1060:        if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
                   1061:                screen_write_flush(ctx);
                   1062:                screen_write_save_last(ctx, &ttyctx);
1.34      nicm     1063:                screen_write_linefeed(ctx, 1);
1.30      nicm     1064:                s->cx = 0;      /* carriage return */
1.89      nicm     1065:                skip = 0;
1.92      nicm     1066:                wrapped = 1;
1.1       nicm     1067:        }
                   1068:
1.64      nicm     1069:        /* Sanity check cursor position. */
1.92      nicm     1070:        if (s->cx > sx - width || s->cy > sy - 1)
1.1       nicm     1071:                return;
                   1072:
                   1073:        /* Handle overwriting of UTF-8 characters. */
1.92      nicm     1074:        gl = &s->grid->linedata[s->grid->hsize + s->cy];
                   1075:        if (gl->flags & GRID_LINE_EXTENDED) {
                   1076:                grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
                   1077:                if (screen_write_overwrite(ctx, &now_gc, width))
                   1078:                        skip = 0;
                   1079:        }
1.1       nicm     1080:
                   1081:        /*
                   1082:         * If the new character is UTF-8 wide, fill in padding cells. Have
                   1083:         * already ensured there is enough room.
                   1084:         */
1.89      nicm     1085:        for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1.88      nicm     1086:                grid_view_set_cell(gd, xx, s->cy, &screen_write_pad_cell);
1.89      nicm     1087:                skip = 0;
                   1088:        }
                   1089:
                   1090:        /* If no change, do not draw. */
1.92      nicm     1091:        if (skip) {
                   1092:                if (s->cx >= gl->cellsize)
                   1093:                        skip = grid_cells_equal(gc, &grid_default_cell);
                   1094:                else {
                   1095:                        gce = &gl->celldata[s->cx];
                   1096:                        if (gce->flags & GRID_FLAG_EXTENDED)
                   1097:                                skip = 0;
1.95      nicm     1098:                        else if (gc->flags != gce->flags)
1.92      nicm     1099:                                skip = 0;
                   1100:                        else if (gc->attr != gce->data.attr)
                   1101:                                skip = 0;
                   1102:                        else if (gc->fg != gce->data.fg)
                   1103:                                skip = 0;
                   1104:                        else if (gc->bg != gce->data.bg)
                   1105:                                skip = 0;
1.95      nicm     1106:                        else if (gc->data.width != 1)
                   1107:                                skip = 0;
                   1108:                        else if (gce->data.data != gc->data.data[0])
1.92      nicm     1109:                                skip = 0;
                   1110:                }
                   1111:        }
1.1       nicm     1112:
1.90      nicm     1113:        /* Update the selection the flag and set the cell. */
                   1114:        selected = screen_check_selection(s, s->cx, s->cy);
                   1115:        if (selected && ~gc->flags & GRID_FLAG_SELECTED) {
                   1116:                skip = 0;
                   1117:                memcpy(&tmp_gc, gc, sizeof tmp_gc);
                   1118:                tmp_gc.flags |= GRID_FLAG_SELECTED;
                   1119:                grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
                   1120:        } else if (!selected && gc->flags & GRID_FLAG_SELECTED) {
                   1121:                skip = 0;
                   1122:                memcpy(&tmp_gc, gc, sizeof tmp_gc);
                   1123:                tmp_gc.flags &= ~GRID_FLAG_SELECTED;
                   1124:                grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
                   1125:        } else if (!skip)
1.89      nicm     1126:                grid_view_set_cell(gd, s->cx, s->cy, gc);
1.1       nicm     1127:
1.64      nicm     1128:        /*
                   1129:         * Move the cursor. If not wrapping, stick at the last character and
                   1130:         * replace it.
                   1131:         */
1.65      nicm     1132:        last = !(s->mode & MODE_WRAP);
1.92      nicm     1133:        if (s->cx <= sx - last - width)
1.64      nicm     1134:                s->cx += width;
                   1135:        else
1.92      nicm     1136:                s->cx = sx - last;
1.1       nicm     1137:
1.89      nicm     1138:        /* Create space for character in insert mode. */
1.17      nicm     1139:        if (insert) {
                   1140:                ttyctx.num = width;
                   1141:                tty_write(tty_cmd_insertcharacter, &ttyctx);
                   1142:        }
1.89      nicm     1143:
                   1144:        /* Write to the screen. */
                   1145:        if (selected) {
1.92      nicm     1146:                screen_write_flush(ctx);
1.97      nicm     1147:                screen_select_cell(s, &tmp_gc, gc);
1.33      nicm     1148:                ttyctx.cell = &tmp_gc;
1.16      nicm     1149:                tty_write(tty_cmd_cell, &ttyctx);
1.92      nicm     1150:                ctx->written++;
1.89      nicm     1151:        } else if (!skip) {
1.92      nicm     1152:                if (wrapped) {
                   1153:                        ttyctx.cell = gc;
                   1154:                        tty_write(tty_cmd_cell, &ttyctx);
                   1155:                        ctx->written++;
                   1156:                } else {
                   1157:                        /*
                   1158:                         * If wp is NULL, we are not updating the terminal and
                   1159:                         * don't care about actually writing the cells
                   1160:                         * (tty_write will just return). So don't even bother
                   1161:                         * allocating the dirty array.
                   1162:                         */
                   1163:                        if (ctx->wp != NULL && s->dirty == NULL) {
                   1164:                                log_debug("%s: allocating %u bits", __func__,
                   1165:                                    s->dirtysize);
                   1166:                                s->dirty = bit_alloc(s->dirtysize);
                   1167:                        }
                   1168:                        if (s->dirty != NULL) {
                   1169:                                bit_set(s->dirty, screen_dirty_bit(s,
                   1170:                                    ttyctx.ocx, ttyctx.ocy));
                   1171:                                ctx->dirty++;
                   1172:                        }
                   1173:                }
                   1174:        } else
                   1175:                ctx->skipped++;
1.35      nicm     1176: }
                   1177:
                   1178: /* Combine a UTF-8 zero-width character onto the previous. */
1.87      nicm     1179: static int
1.60      nicm     1180: screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud)
1.35      nicm     1181: {
                   1182:        struct screen           *s = ctx->s;
                   1183:        struct grid             *gd = s->grid;
1.77      nicm     1184:        struct grid_cell         gc;
1.35      nicm     1185:
                   1186:        /* Can't combine if at 0. */
                   1187:        if (s->cx == 0)
                   1188:                return (-1);
                   1189:
1.60      nicm     1190:        /* Empty data is out. */
                   1191:        if (ud->size == 0)
1.37      nicm     1192:                fatalx("UTF-8 data empty");
                   1193:
1.60      nicm     1194:        /* Retrieve the previous cell. */
1.77      nicm     1195:        grid_view_get_cell(gd, s->cx - 1, s->cy, &gc);
1.35      nicm     1196:
1.60      nicm     1197:        /* Check there is enough space. */
1.77      nicm     1198:        if (gc.data.size + ud->size > sizeof gc.data.data)
1.60      nicm     1199:                return (-1);
1.35      nicm     1200:
1.77      nicm     1201:        /* Append the data. */
                   1202:        memcpy(gc.data.data + gc.data.size, ud->data, ud->size);
                   1203:        gc.data.size += ud->size;
                   1204:
                   1205:        /* Set the new cell. */
                   1206:        grid_view_set_cell(gd, s->cx - 1, s->cy, &gc);
1.35      nicm     1207:
                   1208:        return (0);
1.1       nicm     1209: }
                   1210:
                   1211: /*
                   1212:  * UTF-8 wide characters are a bit of an annoyance. They take up more than one
                   1213:  * cell on the screen, so following cells must not be drawn by marking them as
                   1214:  * padding.
                   1215:  *
                   1216:  * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
                   1217:  * character, it is necessary to also overwrite any other cells which covered
                   1218:  * by the same character.
                   1219:  */
1.89      nicm     1220: static int
                   1221: screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
                   1222:     u_int width)
1.1       nicm     1223: {
                   1224:        struct screen           *s = ctx->s;
                   1225:        struct grid             *gd = s->grid;
1.89      nicm     1226:        struct grid_cell         tmp_gc;
1.1       nicm     1227:        u_int                    xx;
1.89      nicm     1228:        int                      done = 0;
1.1       nicm     1229:
1.89      nicm     1230:        if (gc->flags & GRID_FLAG_PADDING) {
1.1       nicm     1231:                /*
                   1232:                 * A padding cell, so clear any following and leading padding
                   1233:                 * cells back to the character. Don't overwrite the current
                   1234:                 * cell as that happens later anyway.
                   1235:                 */
                   1236:                xx = s->cx + 1;
                   1237:                while (--xx > 0) {
1.89      nicm     1238:                        grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
                   1239:                        if (~tmp_gc.flags & GRID_FLAG_PADDING)
1.1       nicm     1240:                                break;
                   1241:                        grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
                   1242:                }
                   1243:
                   1244:                /* Overwrite the character at the start of this padding. */
                   1245:                grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1.89      nicm     1246:                done = 1;
1.43      nicm     1247:        }
1.1       nicm     1248:
1.43      nicm     1249:        /*
1.95      nicm     1250:         * Overwrite any padding cells that belong to any UTF-8 characters
                   1251:         * we'll be overwriting with the current character.
1.43      nicm     1252:         */
1.95      nicm     1253:        if (width != 1 ||
                   1254:            gc->data.width != 1 ||
                   1255:            gc->flags & GRID_FLAG_PADDING) {
1.89      nicm     1256:                xx = s->cx + width - 1;
                   1257:                while (++xx < screen_size_x(s)) {
                   1258:                        grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
                   1259:                        if (~tmp_gc.flags & GRID_FLAG_PADDING)
                   1260:                                break;
                   1261:                        grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
                   1262:                        done = 1;
                   1263:                }
1.1       nicm     1264:        }
1.89      nicm     1265:
                   1266:        return (done);
1.50      nicm     1267: }
                   1268:
                   1269: void
                   1270: screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len)
                   1271: {
                   1272:        struct tty_ctx  ttyctx;
                   1273:
1.87      nicm     1274:        screen_write_initctx(ctx, &ttyctx);
1.50      nicm     1275:        ttyctx.ptr = str;
                   1276:        ttyctx.num = len;
                   1277:
                   1278:        tty_write(tty_cmd_setselection, &ttyctx);
1.47      nicm     1279: }
                   1280:
                   1281: void
                   1282: screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len)
                   1283: {
1.87      nicm     1284:        struct tty_ctx  ttyctx;
1.47      nicm     1285:
1.87      nicm     1286:        screen_write_initctx(ctx, &ttyctx);
1.47      nicm     1287:        ttyctx.ptr = str;
                   1288:        ttyctx.num = len;
                   1289:
                   1290:        tty_write(tty_cmd_rawstring, &ttyctx);
1.1       nicm     1291: }