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