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

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