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

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