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

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