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

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