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

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