Allow attach-session -t to accept a window and pane to select them on
[tmux-openbsd.git] / screen-write.c
blob7fcfc5eebed09649fce949e4855ccacbeff1bd9b
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "tmux.h"
26 void screen_write_initctx(struct screen_write_ctx *, struct tty_ctx *, int);
27 void screen_write_overwrite(struct screen_write_ctx *, u_int);
28 int screen_write_combine(
29 struct screen_write_ctx *, const struct utf8_data *);
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)
36 ctx->wp = wp;
37 if (wp != NULL && s == NULL)
38 ctx->s = wp->screen;
39 else
40 ctx->s = s;
43 /* Finish writing. */
44 void
45 screen_write_stop(unused struct screen_write_ctx *ctx)
50 /* Reset screen state. */
51 void
52 screen_write_reset(struct screen_write_ctx *ctx)
54 struct screen *s = ctx->s;
56 screen_reset_tabs(s);
57 screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
59 s->mode &= ~(MODE_INSERT|MODE_KCURSOR|MODE_KKEYPAD|MODE_FOCUSON);
60 s->mode &= ~(ALL_MOUSE_MODES|MODE_MOUSE_UTF8|MODE_MOUSE_SGR);
62 screen_write_clearscreen(ctx);
63 screen_write_cursormove(ctx, 0, 0);
66 /* Write character. */
67 void
68 screen_write_putc(struct screen_write_ctx *ctx, struct grid_cell *gc, u_char ch)
70 grid_cell_one(gc, ch);
71 screen_write_cell(ctx, gc);
74 /* Calculate string length, with embedded formatting. */
75 size_t printflike2
76 screen_write_cstrlen(int utf8flag, const char *fmt, ...)
78 va_list ap;
79 char *msg, *msg2, *ptr, *ptr2;
80 size_t size;
82 va_start(ap, fmt);
83 xvasprintf(&msg, fmt, ap);
84 va_end(ap);
85 msg2 = xmalloc(strlen(msg) + 1);
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;
97 *ptr2++ = *ptr++;
99 *ptr2 = '\0';
101 size = screen_write_strlen(utf8flag, "%s", msg2);
103 free(msg);
104 free(msg2);
106 return (size);
109 /* Calculate string length. */
110 size_t printflike2
111 screen_write_strlen(int utf8flag, const char *fmt, ...)
113 va_list ap;
114 char *msg;
115 struct utf8_data utf8data;
116 u_char *ptr;
117 size_t left, size = 0;
119 va_start(ap, fmt);
120 xvasprintf(&msg, fmt, ap);
121 va_end(ap);
123 ptr = msg;
124 while (*ptr != '\0') {
125 if (utf8flag && *ptr > 0x7f && utf8_open(&utf8data, *ptr)) {
126 ptr++;
128 left = strlen(ptr);
129 if (left < utf8data.size - 1)
130 break;
131 while (utf8_append(&utf8data, *ptr))
132 ptr++;
133 ptr++;
135 size += utf8data.width;
136 } else {
137 size++;
138 ptr++;
142 free(msg);
143 return (size);
146 /* Write simple string (no UTF-8 or maximum length). */
147 void printflike3
148 screen_write_puts(
149 struct screen_write_ctx *ctx, struct grid_cell *gc, const char *fmt, ...)
151 va_list ap;
153 va_start(ap, fmt);
154 screen_write_vnputs(ctx, -1, gc, 0, fmt, ap);
155 va_end(ap);
158 /* Write string with length limit (-1 for unlimited). */
159 void printflike5
160 screen_write_nputs(struct screen_write_ctx *ctx,
161 ssize_t maxlen, struct grid_cell *gc, int utf8flag, const char *fmt, ...)
163 va_list ap;
165 va_start(ap, fmt);
166 screen_write_vnputs(ctx, maxlen, gc, utf8flag, fmt, ap);
167 va_end(ap);
170 void
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)
174 char *msg;
175 struct utf8_data utf8data;
176 u_char *ptr;
177 size_t left, size = 0;
179 xvasprintf(&msg, fmt, ap);
181 ptr = msg;
182 while (*ptr != '\0') {
183 if (utf8flag && *ptr > 0x7f && utf8_open(&utf8data, *ptr)) {
184 ptr++;
186 left = strlen(ptr);
187 if (left < utf8data.size - 1)
188 break;
189 while (utf8_append(&utf8data, *ptr))
190 ptr++;
191 ptr++;
193 if (maxlen > 0 &&
194 size + utf8data.width > (size_t) maxlen) {
195 while (size < (size_t) maxlen) {
196 screen_write_putc(ctx, gc, ' ');
197 size++;
199 break;
201 size += utf8data.width;
203 grid_cell_set(gc, &utf8data);
204 screen_write_cell(ctx, gc);
205 } else {
206 if (maxlen > 0 && size + 1 > (size_t) maxlen)
207 break;
209 if (*ptr == '\001')
210 gc->attr ^= GRID_ATTR_CHARSET;
211 else {
212 size++;
213 screen_write_putc(ctx, gc, *ptr);
215 ptr++;
219 free(msg);
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, ...)
227 struct grid_cell lgc;
228 struct utf8_data utf8data;
229 va_list ap;
230 char *msg;
231 u_char *ptr, *last;
232 size_t left, size = 0;
234 va_start(ap, fmt);
235 xvasprintf(&msg, fmt, ap);
236 va_end(ap);
238 memcpy(&lgc, gc, sizeof lgc);
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;
249 *last = '\0';
251 screen_write_parsestyle(gc, &lgc, ptr);
252 ptr = last + 1;
253 continue;
256 if (utf8flag && *ptr > 0x7f && utf8_open(&utf8data, *ptr)) {
257 ptr++;
259 left = strlen(ptr);
260 if (left < utf8data.size - 1)
261 break;
262 while (utf8_append(&utf8data, *ptr))
263 ptr++;
264 ptr++;
266 if (maxlen > 0 &&
267 size + utf8data.width > (size_t) maxlen) {
268 while (size < (size_t) maxlen) {
269 screen_write_putc(ctx, gc, ' ');
270 size++;
272 break;
274 size += utf8data.width;
276 grid_cell_set(&lgc, &utf8data);
277 screen_write_cell(ctx, &lgc);
278 } else {
279 if (maxlen > 0 && size + 1 > (size_t) maxlen)
280 break;
282 size++;
283 screen_write_putc(ctx, &lgc, *ptr);
284 ptr++;
288 free(msg);
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)
296 const char delimiters[] = " ,";
297 char tmp[32];
298 int val;
299 size_t end;
300 u_char fg, bg, attr, flags;
302 if (*in == '\0')
303 return;
304 if (strchr(delimiters, in[strlen(in) - 1]) != NULL)
305 return;
307 fg = gc->fg;
308 bg = gc->bg;
309 attr = gc->attr;
310 flags = gc->flags;
311 do {
312 end = strcspn(in, delimiters);
313 if (end > (sizeof tmp) - 1)
314 return;
315 memcpy(tmp, in, end);
316 tmp[end] = '\0';
318 if (strcasecmp(tmp, "default") == 0) {
319 fg = defgc->fg;
320 bg = defgc->bg;
321 attr = defgc->attr;
322 flags &= ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
323 flags |=
324 defgc->flags & (GRID_FLAG_FG256|GRID_FLAG_BG256);
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') {
329 if (val != 8) {
330 if (val & 0x100) {
331 flags |= GRID_FLAG_FG256;
332 val &= ~0x100;
333 } else
334 flags &= ~GRID_FLAG_FG256;
335 fg = val;
336 } else {
337 fg = defgc->fg;
338 flags &= ~GRID_FLAG_FG256;
339 flags |= defgc->flags & GRID_FLAG_FG256;
341 } else if (*in == 'b' || *in == 'B') {
342 if (val != 8) {
343 if (val & 0x100) {
344 flags |= GRID_FLAG_BG256;
345 val &= ~0x100;
346 } else
347 flags &= ~GRID_FLAG_BG256;
348 bg = val;
349 } else {
350 bg = defgc->bg;
351 flags &= ~GRID_FLAG_BG256;
352 flags |= defgc->flags & GRID_FLAG_BG256;
354 } else
355 return;
356 } else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
357 if ((val = attributes_fromstring(tmp + 2)) == -1)
358 return;
359 attr &= ~val;
360 } else {
361 if ((val = attributes_fromstring(tmp)) == -1)
362 return;
363 attr |= val;
366 in += end + strspn(in + end, delimiters);
367 } while (*in != '\0');
368 gc->fg = fg;
369 gc->bg = bg;
370 gc->attr = attr;
371 gc->flags = flags;
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)
379 struct screen *s = ctx->s;
380 struct grid *gd = src->grid;
381 struct grid_line *gl;
382 const struct grid_cell *gc;
383 struct utf8_data ud;
384 u_int xx, yy, cx, cy, ax, bx;
386 cx = s->cx;
387 cy = s->cy;
388 for (yy = py; yy < py + ny; yy++) {
389 gl = &gd->linedata[yy];
390 if (yy < gd->hsize + gd->sy) {
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.
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;
406 for (xx = ax; xx < bx; xx++) {
407 if (xx >= gl->cellsize)
408 gc = &grid_default_cell;
409 else
410 gc = &gl->celldata[xx];
411 grid_cell_get(gc, &ud);
412 screen_write_cell(ctx, gc);
414 if (px + nx == gd->sx && px + nx > gl->cellsize)
415 screen_write_clearendofline(ctx);
416 } else
417 screen_write_clearline(ctx);
418 cy++;
419 screen_write_cursormove(ctx, cx, cy);
423 /* Set up context for TTY command. */
424 void
425 screen_write_initctx(
426 struct screen_write_ctx *ctx, struct tty_ctx *ttyctx, int save_last)
428 struct screen *s = ctx->s;
429 struct grid *gd = s->grid;
430 const struct grid_cell *gc;
431 u_int xx;
433 ttyctx->wp = ctx->wp;
435 ttyctx->ocx = s->cx;
436 ttyctx->ocy = s->cy;
438 ttyctx->orlower = s->rlower;
439 ttyctx->orupper = s->rupper;
441 if (!save_last)
442 return;
444 /* Save the last cell on the screen. */
445 gc = &grid_default_cell;
446 for (xx = 1; xx <= screen_size_x(s); xx++) {
447 gc = grid_view_peek_cell(gd, screen_size_x(s) - xx, s->cy);
448 if (!(gc->flags & GRID_FLAG_PADDING))
449 break;
451 ttyctx->last_width = xx;
452 memcpy(&ttyctx->last_cell, gc, sizeof ttyctx->last_cell);
455 /* Set a mode. */
456 void
457 screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
459 struct screen *s = ctx->s;
461 s->mode |= mode;
464 /* Clear a mode. */
465 void
466 screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
468 struct screen *s = ctx->s;
470 s->mode &= ~mode;
473 /* Cursor up by ny. */
474 void
475 screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
477 struct screen *s = ctx->s;
479 if (ny == 0)
480 ny = 1;
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;
491 if (s->cx == screen_size_x(s))
492 s->cx--;
493 if (ny == 0)
494 return;
496 s->cy -= ny;
499 /* Cursor down by ny. */
500 void
501 screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
503 struct screen *s = ctx->s;
505 if (ny == 0)
506 ny = 1;
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;
517 if (s->cx == screen_size_x(s))
518 s->cx--;
519 if (ny == 0)
520 return;
522 s->cy += ny;
525 /* Cursor right by nx. */
526 void
527 screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
529 struct screen *s = ctx->s;
531 if (nx == 0)
532 nx = 1;
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;
539 s->cx += nx;
542 /* Cursor left by nx. */
543 void
544 screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
546 struct screen *s = ctx->s;
548 if (nx == 0)
549 nx = 1;
551 if (nx > s->cx)
552 nx = s->cx;
553 if (nx == 0)
554 return;
556 s->cx -= nx;
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)
563 struct screen *s = ctx->s;
564 struct grid_line *gl;
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;
574 } else
575 s->cx--;
578 /* VT100 alignment test. */
579 void
580 screen_write_alignmenttest(struct screen_write_ctx *ctx)
582 struct screen *s = ctx->s;
583 struct tty_ctx ttyctx;
584 struct grid_cell gc;
585 u_int xx, yy;
587 screen_write_initctx(ctx, &ttyctx, 0);
589 memcpy(&gc, &grid_default_cell, sizeof gc);
590 grid_cell_one(&gc, 'E');
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);
597 s->cx = 0;
598 s->cy = 0;
600 s->rupper = 0;
602 s->rlower = screen_size_y(s) - 1;
604 tty_write(tty_cmd_alignmenttest, &ttyctx);
607 /* Insert nx characters. */
608 void
609 screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx)
611 struct screen *s = ctx->s;
612 struct tty_ctx ttyctx;
614 if (nx == 0)
615 nx = 1;
617 if (nx > screen_size_x(s) - s->cx)
618 nx = screen_size_x(s) - s->cx;
619 if (nx == 0)
620 return;
622 screen_write_initctx(ctx, &ttyctx, 0);
624 if (s->cx <= screen_size_x(s) - 1)
625 grid_view_insert_cells(s->grid, s->cx, s->cy, nx);
627 ttyctx.num = nx;
628 tty_write(tty_cmd_insertcharacter, &ttyctx);
631 /* Delete nx characters. */
632 void
633 screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx)
635 struct screen *s = ctx->s;
636 struct tty_ctx ttyctx;
638 if (nx == 0)
639 nx = 1;
641 if (nx > screen_size_x(s) - s->cx)
642 nx = screen_size_x(s) - s->cx;
643 if (nx == 0)
644 return;
646 screen_write_initctx(ctx, &ttyctx, 0);
648 if (s->cx <= screen_size_x(s) - 1)
649 grid_view_delete_cells(s->grid, s->cx, s->cy, nx);
651 ttyctx.num = nx;
652 tty_write(tty_cmd_deletecharacter, &ttyctx);
655 /* Clear nx characters. */
656 void
657 screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx)
659 struct screen *s = ctx->s;
660 struct tty_ctx ttyctx;
662 if (nx == 0)
663 nx = 1;
665 if (nx > screen_size_x(s) - s->cx)
666 nx = screen_size_x(s) - s->cx;
667 if (nx == 0)
668 return;
670 screen_write_initctx(ctx, &ttyctx, 0);
672 if (s->cx <= screen_size_x(s) - 1)
673 grid_view_clear(s->grid, s->cx, s->cy, nx, 1);
675 ttyctx.num = nx;
676 tty_write(tty_cmd_clearcharacter, &ttyctx);
679 /* Insert ny lines. */
680 void
681 screen_write_insertline(struct screen_write_ctx *ctx, u_int ny)
683 struct screen *s = ctx->s;
684 struct tty_ctx ttyctx;
686 if (ny == 0)
687 ny = 1;
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;
695 screen_write_initctx(ctx, &ttyctx, 0);
697 grid_view_insert_lines(s->grid, s->cy, ny);
699 ttyctx.num = ny;
700 tty_write(tty_cmd_insertline, &ttyctx);
701 return;
704 if (ny > s->rlower + 1 - s->cy)
705 ny = s->rlower + 1 - s->cy;
706 if (ny == 0)
707 return;
709 screen_write_initctx(ctx, &ttyctx, 0);
711 if (s->cy < s->rupper || s->cy > s->rlower)
712 grid_view_insert_lines(s->grid, s->cy, ny);
713 else
714 grid_view_insert_lines_region(s->grid, s->rlower, s->cy, ny);
716 ttyctx.num = ny;
717 tty_write(tty_cmd_insertline, &ttyctx);
720 /* Delete ny lines. */
721 void
722 screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny)
724 struct screen *s = ctx->s;
725 struct tty_ctx ttyctx;
727 if (ny == 0)
728 ny = 1;
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;
736 screen_write_initctx(ctx, &ttyctx, 0);
738 grid_view_delete_lines(s->grid, s->cy, ny);
740 ttyctx.num = ny;
741 tty_write(tty_cmd_deleteline, &ttyctx);
742 return;
745 if (ny > s->rlower + 1 - s->cy)
746 ny = s->rlower + 1 - s->cy;
747 if (ny == 0)
748 return;
750 screen_write_initctx(ctx, &ttyctx, 0);
752 if (s->cy < s->rupper || s->cy > s->rlower)
753 grid_view_delete_lines(s->grid, s->cy, ny);
754 else
755 grid_view_delete_lines_region(s->grid, s->rlower, s->cy, ny);
757 ttyctx.num = ny;
758 tty_write(tty_cmd_deleteline, &ttyctx);
761 /* Clear line at cursor. */
762 void
763 screen_write_clearline(struct screen_write_ctx *ctx)
765 struct screen *s = ctx->s;
766 struct tty_ctx ttyctx;
768 screen_write_initctx(ctx, &ttyctx, 0);
770 grid_view_clear(s->grid, 0, s->cy, screen_size_x(s), 1);
772 tty_write(tty_cmd_clearline, &ttyctx);
775 /* Clear to end of line from cursor. */
776 void
777 screen_write_clearendofline(struct screen_write_ctx *ctx)
779 struct screen *s = ctx->s;
780 struct tty_ctx ttyctx;
781 u_int sx;
783 screen_write_initctx(ctx, &ttyctx, 0);
785 sx = screen_size_x(s);
787 if (s->cx <= sx - 1)
788 grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1);
790 tty_write(tty_cmd_clearendofline, &ttyctx);
793 /* Clear to start of line from cursor. */
794 void
795 screen_write_clearstartofline(struct screen_write_ctx *ctx)
797 struct screen *s = ctx->s;
798 struct tty_ctx ttyctx;
799 u_int sx;
801 screen_write_initctx(ctx, &ttyctx, 0);
803 sx = screen_size_x(s);
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);
810 tty_write(tty_cmd_clearstartofline, &ttyctx);
813 /* Move cursor to px,py. */
814 void
815 screen_write_cursormove(struct screen_write_ctx *ctx, u_int px, u_int py)
817 struct screen *s = ctx->s;
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;
824 s->cx = px;
825 s->cy = py;
828 /* Reverse index (up with scroll). */
829 void
830 screen_write_reverseindex(struct screen_write_ctx *ctx)
832 struct screen *s = ctx->s;
833 struct tty_ctx ttyctx;
835 screen_write_initctx(ctx, &ttyctx, 0);
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--;
842 tty_write(tty_cmd_reverseindex, &ttyctx);
845 /* Set scroll region. */
846 void
847 screen_write_scrollregion(
848 struct screen_write_ctx *ctx, u_int rupper, u_int rlower)
850 struct screen *s = ctx->s;
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;
856 if (rupper >= rlower) /* cannot be one line */
857 return;
859 /* Cursor moves to top-left. */
860 s->cx = 0;
861 s->cy = 0;
863 s->rupper = rupper;
864 s->rlower = rlower;
867 /* Line feed. */
868 void
869 screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped)
871 struct screen *s = ctx->s;
872 struct grid_line *gl;
873 struct tty_ctx ttyctx;
875 screen_write_initctx(ctx, &ttyctx, 0);
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;
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++;
888 ttyctx.num = wrapped;
889 tty_write(tty_cmd_linefeed, &ttyctx);
892 /* Carriage return (cursor to start of line). */
893 void
894 screen_write_carriagereturn(struct screen_write_ctx *ctx)
896 struct screen *s = ctx->s;
898 s->cx = 0;
901 /* Clear to end of screen from cursor. */
902 void
903 screen_write_clearendofscreen(struct screen_write_ctx *ctx)
905 struct screen *s = ctx->s;
906 struct tty_ctx ttyctx;
907 u_int sx, sy;
909 screen_write_initctx(ctx, &ttyctx, 0);
911 sx = screen_size_x(s);
912 sy = screen_size_y(s);
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));
923 tty_write(tty_cmd_clearendofscreen, &ttyctx);
926 /* Clear to start of screen. */
927 void
928 screen_write_clearstartofscreen(struct screen_write_ctx *ctx)
930 struct screen *s = ctx->s;
931 struct tty_ctx ttyctx;
932 u_int sx;
934 screen_write_initctx(ctx, &ttyctx, 0);
936 sx = screen_size_x(s);
938 if (s->cy > 0)
939 grid_view_clear(s->grid, 0, 0, sx, s->cy);
940 if (s->cx > sx - 1)
941 grid_view_clear(s->grid, 0, s->cy, sx, 1);
942 else
943 grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1);
945 tty_write(tty_cmd_clearstartofscreen, &ttyctx);
948 /* Clear entire screen. */
949 void
950 screen_write_clearscreen(struct screen_write_ctx *ctx)
952 struct screen *s = ctx->s;
953 struct tty_ctx ttyctx;
955 screen_write_initctx(ctx, &ttyctx, 0);
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));
965 tty_write(tty_cmd_clearscreen, &ttyctx);
968 /* Clear entire history. */
969 void
970 screen_write_clearhistory(struct screen_write_ctx *ctx)
972 struct screen *s = ctx->s;
973 struct grid *gd = s->grid;
975 grid_move_lines(gd, 0, gd->hsize, gd->sy);
976 gd->hsize = 0;
979 /* Write cell data. */
980 void
981 screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
983 struct screen *s = ctx->s;
984 struct grid *gd = s->grid;
985 struct tty_ctx ttyctx;
986 u_int width, xx, last;
987 struct grid_cell tmp_gc, *tmp_gcp;
988 struct utf8_data ud;
989 int insert;
991 /* Ignore padding. */
992 if (gc->flags & GRID_FLAG_PADDING)
993 return;
994 width = grid_cell_width(gc);
997 * If this is a wide character and there is no room on the screen, for
998 * the entire character, don't print it.
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))))
1004 return;
1007 * If the width is zero, combine onto the previous character, if
1008 * there is space.
1010 if (width == 0) {
1011 grid_cell_get(gc, &ud);
1012 if (screen_write_combine(ctx, &ud) == 0) {
1013 screen_write_initctx(ctx, &ttyctx, 0);
1014 tty_write(tty_cmd_utf8character, &ttyctx);
1016 return;
1019 /* Initialise the redraw context, saving the last cell. */
1020 screen_write_initctx(ctx, &ttyctx, 1);
1022 /* If in insert mode, make space for the cells. */
1023 if ((s->mode & MODE_INSERT) && s->cx <= screen_size_x(s) - width) {
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;
1027 } else
1028 insert = 0;
1030 /* Check this will fit on the current line and wrap if not. */
1031 if ((s->mode & MODE_WRAP) && s->cx > screen_size_x(s) - width) {
1032 screen_write_linefeed(ctx, 1);
1033 s->cx = 0; /* carriage return */
1036 /* Sanity check cursor position. */
1037 if (s->cx > screen_size_x(s) - width || s->cy > screen_size_y(s) - 1)
1038 return;
1040 /* Handle overwriting of UTF-8 characters. */
1041 screen_write_overwrite(ctx, width);
1044 * If the new character is UTF-8 wide, fill in padding cells. Have
1045 * already ensured there is enough room.
1047 for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1048 tmp_gcp = grid_view_get_cell(gd, xx, s->cy);
1049 if (tmp_gcp != NULL)
1050 tmp_gcp->flags |= GRID_FLAG_PADDING;
1053 /* Set the cell. */
1054 grid_view_set_cell(gd, s->cx, s->cy, gc);
1057 * Move the cursor. If not wrapping, stick at the last character and
1058 * replace it.
1060 last = !(s->mode & MODE_WRAP);
1061 if (s->cx <= screen_size_x(s) - last - width)
1062 s->cx += width;
1063 else
1064 s->cx = screen_size_x(s) - last;
1066 /* Draw to the screen if necessary. */
1067 if (insert) {
1068 ttyctx.num = width;
1069 tty_write(tty_cmd_insertcharacter, &ttyctx);
1071 if (screen_check_selection(s, s->cx - width, s->cy)) {
1072 memcpy(&tmp_gc, &s->sel.cell, sizeof tmp_gc);
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);
1076 tmp_gc.flags |= s->sel.cell.flags &
1077 (GRID_FLAG_FG256|GRID_FLAG_BG256);
1078 ttyctx.cell = &tmp_gc;
1079 tty_write(tty_cmd_cell, &ttyctx);
1080 } else {
1081 ttyctx.cell = gc;
1082 tty_write(tty_cmd_cell, &ttyctx);
1086 /* Combine a UTF-8 zero-width character onto the previous. */
1088 screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud)
1090 struct screen *s = ctx->s;
1091 struct grid *gd = s->grid;
1092 struct grid_cell *gc;
1093 struct utf8_data ud1;
1095 /* Can't combine if at 0. */
1096 if (s->cx == 0)
1097 return (-1);
1099 /* Empty data is out. */
1100 if (ud->size == 0)
1101 fatalx("UTF-8 data empty");
1103 /* Retrieve the previous cell. */
1104 gc = grid_view_get_cell(gd, s->cx - 1, s->cy);
1105 grid_cell_get(gc, &ud1);
1107 /* Check there is enough space. */
1108 if (ud1.size + ud->size > sizeof ud1.data)
1109 return (-1);
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);
1116 return (0);
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.
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.
1128 void
1129 screen_write_overwrite(struct screen_write_ctx *ctx, u_int width)
1131 struct screen *s = ctx->s;
1132 struct grid *gd = s->grid;
1133 const struct grid_cell *gc;
1134 u_int xx;
1136 gc = grid_view_peek_cell(gd, s->cx, s->cy);
1137 if (gc->flags & GRID_FLAG_PADDING) {
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.
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);
1151 /* Overwrite the character at the start of this padding. */
1152 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1156 * Overwrite any padding cells that belong to a UTF-8 character
1157 * we'll be overwriting with the current character.
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);
1168 void
1169 screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len)
1171 struct tty_ctx ttyctx;
1173 screen_write_initctx(ctx, &ttyctx, 0);
1174 ttyctx.ptr = str;
1175 ttyctx.num = len;
1177 tty_write(tty_cmd_setselection, &ttyctx);
1180 void
1181 screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len)
1183 struct tty_ctx ttyctx;
1185 screen_write_initctx(ctx, &ttyctx, 0);
1186 ttyctx.ptr = str;
1187 ttyctx.num = len;
1189 tty_write(tty_cmd_rawstring, &ttyctx);