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

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