xref: /openbsd-src/usr.bin/tmux/screen-write.c (revision bf3e9ddbc54ec6cccf4fb99aecbd0e99706a3d76)
1 /* $OpenBSD: screen-write.c,v 1.191 2021/01/22 11:28:33 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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 <stdlib.h>
22 #include <string.h>
23 
24 #include "tmux.h"
25 
26 static void	screen_write_collect_clear(struct screen_write_ctx *, u_int,
27 		    u_int);
28 static void	screen_write_collect_clear_end(struct screen_write_ctx *, u_int,
29 		    u_int);
30 static void	screen_write_collect_clear_start(struct screen_write_ctx *,
31 		    u_int, u_int);
32 static void	screen_write_collect_scroll(struct screen_write_ctx *);
33 static void	screen_write_collect_flush(struct screen_write_ctx *, int,
34 		    const char *);
35 
36 static int	screen_write_overwrite(struct screen_write_ctx *,
37 		    struct grid_cell *, u_int);
38 static const struct grid_cell *screen_write_combine(struct screen_write_ctx *,
39 		    const struct utf8_data *, u_int *);
40 
41 struct screen_write_collect_item {
42 	u_int			 x;
43 	int			 wrapped;
44 
45 	enum { TEXT, CLEAR_END, CLEAR_START } type;
46 	u_int			 used;
47 	u_int			 bg;
48 
49 	struct grid_cell	 gc;
50 
51 	TAILQ_ENTRY(screen_write_collect_item) entry;
52 };
53 struct screen_write_collect_line {
54 	u_int					 bg;
55 	char					*data;
56 	TAILQ_HEAD(, screen_write_collect_item)  items;
57 };
58 
59 static void
60 screen_write_offset_timer(__unused int fd, __unused short events, void *data)
61 {
62 	struct window	*w = data;
63 
64 	tty_update_window_offset(w);
65 }
66 
67 /* Set cursor position. */
68 static void
69 screen_write_set_cursor(struct screen_write_ctx *ctx, int cx, int cy)
70 {
71 	struct window_pane	*wp = ctx->wp;
72 	struct window		*w;
73 	struct screen		*s = ctx->s;
74 	struct timeval		 tv = { .tv_usec = 10000 };
75 
76 	if (cx != -1 && (u_int)cx == s->cx && cy != -1 && (u_int)cy == s->cy)
77 		return;
78 
79 	if (cx != -1) {
80 		if ((u_int)cx > screen_size_x(s)) /* allow last column */
81 			cx = screen_size_x(s) - 1;
82 		s->cx = cx;
83 	}
84 	if (cy != -1) {
85 		if ((u_int)cy > screen_size_y(s) - 1)
86 			cy = screen_size_y(s) - 1;
87 		s->cy = cy;
88 	}
89 
90 	if (wp == NULL)
91 		return;
92 	w = wp->window;
93 
94 	if (!event_initialized(&w->offset_timer))
95 		evtimer_set(&w->offset_timer, screen_write_offset_timer, w);
96 	if (!evtimer_pending(&w->offset_timer, NULL))
97 		evtimer_add(&w->offset_timer, &tv);
98 }
99 
100 /* Do a full redraw. */
101 static void
102 screen_write_redraw_cb(const struct tty_ctx *ttyctx)
103 {
104 	struct window_pane	*wp = ttyctx->arg;
105 
106 	if (wp != NULL)
107 		wp->flags |= PANE_REDRAW;
108 }
109 
110 /* Update context for client. */
111 static int
112 screen_write_set_client_cb(struct tty_ctx *ttyctx, struct client *c)
113 {
114 	struct window_pane	*wp = ttyctx->arg;
115 
116 	if (c->session->curw->window != wp->window)
117 		return (0);
118 	if (wp->layout_cell == NULL)
119 		return (0);
120 
121 	if (wp->flags & (PANE_REDRAW|PANE_DROP))
122 		return (-1);
123 	if (c->flags & CLIENT_REDRAWPANES) {
124 		/*
125 		 * Redraw is already deferred to redraw another pane - redraw
126 		 * this one also when that happens.
127 		 */
128 		log_debug("adding %%%u to deferred redraw", wp->id);
129 		wp->flags |= PANE_REDRAW;
130 		return (-1);
131 	}
132 
133 	ttyctx->bigger = tty_window_offset(&c->tty, &ttyctx->wox, &ttyctx->woy,
134 	    &ttyctx->wsx, &ttyctx->wsy);
135 
136 	ttyctx->xoff = ttyctx->rxoff = wp->xoff;
137 	ttyctx->yoff = ttyctx->ryoff = wp->yoff;
138 
139 	if (status_at_line(c) == 0)
140 		ttyctx->yoff += status_line_size(c);
141 
142 	return (1);
143 }
144 
145 /* Set up context for TTY command. */
146 static void
147 screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx,
148     int sync)
149 {
150 	struct screen	*s = ctx->s;
151 
152 	memset(ttyctx, 0, sizeof *ttyctx);
153 
154 	if (ctx->wp != NULL) {
155 		tty_default_colours(&ttyctx->defaults, ctx->wp);
156 		ttyctx->palette = ctx->wp->palette;
157 	} else {
158 		memcpy(&ttyctx->defaults, &grid_default_cell,
159 		    sizeof ttyctx->defaults);
160 		ttyctx->palette = NULL;
161 	}
162 
163 	ttyctx->s = s;
164 	ttyctx->sx = screen_size_x(s);
165 	ttyctx->sy = screen_size_y(s);
166 
167 	ttyctx->ocx = s->cx;
168 	ttyctx->ocy = s->cy;
169 	ttyctx->orlower = s->rlower;
170 	ttyctx->orupper = s->rupper;
171 
172 	if (ctx->init_ctx_cb != NULL)
173 		ctx->init_ctx_cb(ctx, ttyctx);
174 	else {
175 		ttyctx->redraw_cb = screen_write_redraw_cb;
176 		if (ctx->wp == NULL)
177 			ttyctx->set_client_cb = NULL;
178 		else
179 			ttyctx->set_client_cb = screen_write_set_client_cb;
180 		ttyctx->arg = ctx->wp;
181 	}
182 
183 	if (ctx->wp != NULL &&
184 	    (~ctx->flags & SCREEN_WRITE_SYNC) &&
185 	    (sync || ctx->wp != ctx->wp->window->active)) {
186 		tty_write(tty_cmd_syncstart, ttyctx);
187 		ctx->flags |= SCREEN_WRITE_SYNC;
188 	}
189 }
190 
191 /* Make write list. */
192 void
193 screen_write_make_list(struct screen *s)
194 {
195 	u_int	y;
196 
197 	s->write_list = xcalloc(screen_size_y(s), sizeof *s->write_list);
198 	for (y = 0; y < screen_size_y(s); y++)
199 		TAILQ_INIT(&s->write_list[y].items);
200 }
201 
202 /* Free write list. */
203 void
204 screen_write_free_list(struct screen *s)
205 {
206 	u_int	y;
207 
208 	for (y = 0; y < screen_size_y(s); y++)
209 		free(s->write_list[y].data);
210 	free(s->write_list);
211 }
212 
213 /* Set up for writing. */
214 static void
215 screen_write_init(struct screen_write_ctx *ctx, struct screen *s)
216 {
217 	memset(ctx, 0, sizeof *ctx);
218 
219 	ctx->s = s;
220 
221 	if (ctx->s->write_list == NULL)
222 		screen_write_make_list(ctx->s);
223 	ctx->item = xcalloc(1, sizeof *ctx->item);
224 
225 	ctx->scrolled = 0;
226 	ctx->bg = 8;
227 }
228 
229 /* Initialize writing with a pane. */
230 void
231 screen_write_start_pane(struct screen_write_ctx *ctx, struct window_pane *wp,
232     struct screen *s)
233 {
234 	if (s == NULL)
235 		s = wp->screen;
236 	screen_write_init(ctx, s);
237 	ctx->wp = wp;
238 
239 	if (log_get_level() != 0) {
240 		log_debug("%s: size %ux%u, pane %%%u (at %u,%u)",
241 		    __func__, screen_size_x(ctx->s), screen_size_y(ctx->s),
242 		    wp->id, wp->xoff, wp->yoff);
243 	}
244 }
245 
246 /* Initialize writing with a callback. */
247 void
248 screen_write_start_callback(struct screen_write_ctx *ctx, struct screen *s,
249     screen_write_init_ctx_cb cb, void *arg)
250 {
251 	screen_write_init(ctx, s);
252 
253 	ctx->init_ctx_cb = cb;
254 	ctx->arg = arg;
255 
256 	if (log_get_level() != 0) {
257 		log_debug("%s: size %ux%u, with callback", __func__,
258 		    screen_size_x(ctx->s), screen_size_y(ctx->s));
259 	}
260 }
261 
262 /* Initialize writing. */
263 void
264 screen_write_start(struct screen_write_ctx *ctx, struct screen *s)
265 {
266 	screen_write_init(ctx, s);
267 
268 	if (log_get_level() != 0) {
269 		log_debug("%s: size %ux%u, no pane", __func__,
270 		    screen_size_x(ctx->s), screen_size_y(ctx->s));
271 	}
272 }
273 
274 /* Finish writing. */
275 void
276 screen_write_stop(struct screen_write_ctx *ctx)
277 {
278 	screen_write_collect_end(ctx);
279 	screen_write_collect_flush(ctx, 0, __func__);
280 
281 	log_debug("%s: %u cells (%u written, %u skipped)", __func__,
282 	    ctx->cells, ctx->written, ctx->skipped);
283 	if (ctx->wp != NULL) {
284 		ctx->wp->written += ctx->written;
285 		ctx->wp->skipped += ctx->skipped;
286 	}
287 
288 	free(ctx->item);
289 }
290 
291 /* Reset screen state. */
292 void
293 screen_write_reset(struct screen_write_ctx *ctx)
294 {
295 	struct screen	*s = ctx->s;
296 
297 	screen_reset_tabs(s);
298 	screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
299 
300 	s->mode = MODE_CURSOR | MODE_WRAP;
301 
302 	screen_write_clearscreen(ctx, 8);
303 	screen_write_set_cursor(ctx, 0, 0);
304 }
305 
306 /* Write character. */
307 void
308 screen_write_putc(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
309     u_char ch)
310 {
311 	struct grid_cell	gc;
312 
313 	memcpy(&gc, gcp, sizeof gc);
314 
315 	utf8_set(&gc.data, ch);
316 	screen_write_cell(ctx, &gc);
317 }
318 
319 /* Calculate string length. */
320 size_t
321 screen_write_strlen(const char *fmt, ...)
322 {
323 	va_list			ap;
324 	char   	       	       *msg;
325 	struct utf8_data	ud;
326 	u_char 	      	       *ptr;
327 	size_t			left, size = 0;
328 	enum utf8_state		more;
329 
330 	va_start(ap, fmt);
331 	xvasprintf(&msg, fmt, ap);
332 	va_end(ap);
333 
334 	ptr = msg;
335 	while (*ptr != '\0') {
336 		if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) {
337 			ptr++;
338 
339 			left = strlen(ptr);
340 			if (left < (size_t)ud.size - 1)
341 				break;
342 			while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE)
343 				ptr++;
344 			ptr++;
345 
346 			if (more == UTF8_DONE)
347 				size += ud.width;
348 		} else {
349 			if (*ptr > 0x1f && *ptr < 0x7f)
350 				size++;
351 			ptr++;
352 		}
353 	}
354 
355 	free(msg);
356 	return (size);
357 }
358 
359 /* Write string wrapped over lines. */
360 int
361 screen_write_text(struct screen_write_ctx *ctx, u_int cx, u_int width,
362     u_int lines, int more, const struct grid_cell *gcp, const char *fmt, ...)
363 {
364 	struct screen		*s = ctx->s;
365 	va_list			 ap;
366 	char			*tmp;
367 	u_int			 cy = s->cy, i, end, next, idx = 0, at, left;
368 	struct utf8_data	*text;
369 	struct grid_cell	 gc;
370 
371 	memcpy(&gc, gcp, sizeof gc);
372 
373 	va_start(ap, fmt);
374 	xvasprintf(&tmp, fmt, ap);
375 	va_end(ap);
376 
377 	text = utf8_fromcstr(tmp);
378 	free(tmp);
379 
380 	left = (cx + width) - s->cx;
381 	for (;;) {
382 		/* Find the end of what can fit on the line. */
383 		at = 0;
384 		for (end = idx; text[end].size != 0; end++) {
385 			if (text[end].size == 1 && text[end].data[0] == '\n')
386 				break;
387 			if (at + text[end].width > left)
388 				break;
389 			at += text[end].width;
390 		}
391 
392 		/*
393 		 * If we're on a space, that's the end. If not, walk back to
394 		 * try and find one.
395 		 */
396 		if (text[end].size == 0)
397 			next = end;
398 		else if (text[end].size == 1 && text[end].data[0] == '\n')
399 			next = end + 1;
400 		else if (text[end].size == 1 && text[end].data[0] == ' ')
401 			next = end + 1;
402 		else {
403 			for (i = end; i > idx; i--) {
404 				if (text[i].size == 1 && text[i].data[0] == ' ')
405 					break;
406 			}
407 			if (i != idx) {
408 				next = i + 1;
409 				end = i;
410 			} else
411 				next = end;
412 		}
413 
414 		/* Print the line. */
415 		for (i = idx; i < end; i++) {
416 			utf8_copy(&gc.data, &text[i]);
417 			screen_write_cell(ctx, &gc);
418 		}
419 
420 		/* If at the bottom, stop. */
421 		idx = next;
422 		if (s->cy == cy + lines - 1 || text[idx].size == 0)
423 			break;
424 
425 		screen_write_cursormove(ctx, cx, s->cy + 1, 0);
426 		left = width;
427 	}
428 
429 	/*
430 	 * Fail if on the last line and there is more to come or at the end, or
431 	 * if the text was not entirely consumed.
432 	 */
433 	if ((s->cy == cy + lines - 1 && (!more || s->cx == cx + width)) ||
434 	    text[idx].size != 0) {
435 		free(text);
436 		return (0);
437 	}
438 	free(text);
439 
440 	/*
441 	 * If no more to come, move to the next line. Otherwise, leave on
442 	 * the same line (except if at the end).
443 	 */
444 	if (!more || s->cx == cx + width)
445 		screen_write_cursormove(ctx, cx, s->cy + 1, 0);
446 	return (1);
447 }
448 
449 /* Write simple string (no maximum length). */
450 void
451 screen_write_puts(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
452     const char *fmt, ...)
453 {
454 	va_list	ap;
455 
456 	va_start(ap, fmt);
457 	screen_write_vnputs(ctx, -1, gcp, fmt, ap);
458 	va_end(ap);
459 }
460 
461 /* Write string with length limit (-1 for unlimited). */
462 void
463 screen_write_nputs(struct screen_write_ctx *ctx, ssize_t maxlen,
464     const struct grid_cell *gcp, const char *fmt, ...)
465 {
466 	va_list	ap;
467 
468 	va_start(ap, fmt);
469 	screen_write_vnputs(ctx, maxlen, gcp, fmt, ap);
470 	va_end(ap);
471 }
472 
473 void
474 screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
475     const struct grid_cell *gcp, const char *fmt, va_list ap)
476 {
477 	struct grid_cell	gc;
478 	struct utf8_data       *ud = &gc.data;
479 	char   		       *msg;
480 	u_char 		       *ptr;
481 	size_t		 	left, size = 0;
482 	enum utf8_state		more;
483 
484 	memcpy(&gc, gcp, sizeof gc);
485 	xvasprintf(&msg, fmt, ap);
486 
487 	ptr = msg;
488 	while (*ptr != '\0') {
489 		if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
490 			ptr++;
491 
492 			left = strlen(ptr);
493 			if (left < (size_t)ud->size - 1)
494 				break;
495 			while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
496 				ptr++;
497 			ptr++;
498 
499 			if (more != UTF8_DONE)
500 				continue;
501 			if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
502 				while (size < (size_t)maxlen) {
503 					screen_write_putc(ctx, &gc, ' ');
504 					size++;
505 				}
506 				break;
507 			}
508 			size += ud->width;
509 			screen_write_cell(ctx, &gc);
510 		} else {
511 			if (maxlen > 0 && size + 1 > (size_t)maxlen)
512 				break;
513 
514 			if (*ptr == '\001')
515 				gc.attr ^= GRID_ATTR_CHARSET;
516 			else if (*ptr == '\n') {
517 				screen_write_linefeed(ctx, 0, 8);
518 				screen_write_carriagereturn(ctx);
519 			} else if (*ptr > 0x1f && *ptr < 0x7f) {
520 				size++;
521 				screen_write_putc(ctx, &gc, *ptr);
522 			}
523 			ptr++;
524 		}
525 	}
526 
527 	free(msg);
528 }
529 
530 /*
531  * Copy from another screen but without the selection stuff. Assumes the target
532  * region is already big enough.
533  */
534 void
535 screen_write_fast_copy(struct screen_write_ctx *ctx, struct screen *src,
536     u_int px, u_int py, u_int nx, u_int ny)
537 {
538 	struct screen		*s = ctx->s;
539 	struct grid		*gd = src->grid;
540 	struct grid_cell	 gc;
541 	u_int		 	 xx, yy, cx, cy;
542 
543 	if (nx == 0 || ny == 0)
544 		return;
545 
546 	cy = s->cy;
547 	for (yy = py; yy < py + ny; yy++) {
548 		if (yy >= gd->hsize + gd->sy)
549 			break;
550 		cx = s->cx;
551 		for (xx = px; xx < px + nx; xx++) {
552 			if (xx >= grid_get_line(gd, yy)->cellsize)
553 				break;
554 			grid_get_cell(gd, xx, yy, &gc);
555 			if (xx + gc.data.width > px + nx)
556 				break;
557 			grid_view_set_cell(ctx->s->grid, cx, cy, &gc);
558 			cx++;
559 		}
560 		cy++;
561 	}
562 }
563 
564 /* Draw a horizontal line on screen. */
565 void
566 screen_write_hline(struct screen_write_ctx *ctx, u_int nx, int left, int right)
567 {
568 	struct screen		*s = ctx->s;
569 	struct grid_cell	 gc;
570 	u_int			 cx, cy, i;
571 
572 	cx = s->cx;
573 	cy = s->cy;
574 
575 	memcpy(&gc, &grid_default_cell, sizeof gc);
576 	gc.attr |= GRID_ATTR_CHARSET;
577 
578 	screen_write_putc(ctx, &gc, left ? 't' : 'q');
579 	for (i = 1; i < nx - 1; i++)
580 		screen_write_putc(ctx, &gc, 'q');
581 	screen_write_putc(ctx, &gc, right ? 'u' : 'q');
582 
583 	screen_write_set_cursor(ctx, cx, cy);
584 }
585 
586 /* Draw a horizontal line on screen. */
587 void
588 screen_write_vline(struct screen_write_ctx *ctx, u_int ny, int top, int bottom)
589 {
590 	struct screen		*s = ctx->s;
591 	struct grid_cell	 gc;
592 	u_int			 cx, cy, i;
593 
594 	cx = s->cx;
595 	cy = s->cy;
596 
597 	memcpy(&gc, &grid_default_cell, sizeof gc);
598 	gc.attr |= GRID_ATTR_CHARSET;
599 
600 	screen_write_putc(ctx, &gc, top ? 'w' : 'x');
601 	for (i = 1; i < ny - 1; i++) {
602 		screen_write_set_cursor(ctx, cx, cy + i);
603 		screen_write_putc(ctx, &gc, 'x');
604 	}
605 	screen_write_set_cursor(ctx, cx, cy + ny - 1);
606 	screen_write_putc(ctx, &gc, bottom ? 'v' : 'x');
607 
608 	screen_write_set_cursor(ctx, cx, cy);
609 }
610 
611 /* Draw a menu on screen. */
612 void
613 screen_write_menu(struct screen_write_ctx *ctx, struct menu *menu,
614     int choice, const struct grid_cell *choice_gc)
615 {
616 	struct screen		*s = ctx->s;
617 	struct grid_cell	 default_gc;
618 	const struct grid_cell	*gc = &default_gc;
619 	u_int			 cx, cy, i, j;
620 	const char		*name;
621 
622 	cx = s->cx;
623 	cy = s->cy;
624 
625 	memcpy(&default_gc, &grid_default_cell, sizeof default_gc);
626 
627 	screen_write_box(ctx, menu->width + 4, menu->count + 2);
628 	screen_write_cursormove(ctx, cx + 2, cy, 0);
629 	format_draw(ctx, &default_gc, menu->width, menu->title, NULL);
630 
631 	for (i = 0; i < menu->count; i++) {
632 		name = menu->items[i].name;
633 		if (name == NULL) {
634 			screen_write_cursormove(ctx, cx, cy + 1 + i, 0);
635 			screen_write_hline(ctx, menu->width + 4, 1, 1);
636 		} else {
637 			if (choice >= 0 && i == (u_int)choice && *name != '-')
638 				gc = choice_gc;
639 			screen_write_cursormove(ctx, cx + 2, cy + 1 + i, 0);
640 			for (j = 0; j < menu->width; j++)
641 				screen_write_putc(ctx, gc, ' ');
642 			screen_write_cursormove(ctx, cx + 2, cy + 1 + i, 0);
643 			if (*name == '-') {
644 				name++;
645 				default_gc.attr |= GRID_ATTR_DIM;
646 				format_draw(ctx, gc, menu->width, name, NULL);
647 				default_gc.attr &= ~GRID_ATTR_DIM;
648 			} else
649 				format_draw(ctx, gc, menu->width, name, NULL);
650 			gc = &default_gc;
651 		}
652 	}
653 
654 	screen_write_set_cursor(ctx, cx, cy);
655 }
656 
657 /* Draw a box on screen. */
658 void
659 screen_write_box(struct screen_write_ctx *ctx, u_int nx, u_int ny)
660 {
661 	struct screen		*s = ctx->s;
662 	struct grid_cell	 gc;
663 	u_int			 cx, cy, i;
664 
665 	cx = s->cx;
666 	cy = s->cy;
667 
668 	memcpy(&gc, &grid_default_cell, sizeof gc);
669 	gc.attr |= GRID_ATTR_CHARSET;
670 
671 	screen_write_putc(ctx, &gc, 'l');
672 	for (i = 1; i < nx - 1; i++)
673 		screen_write_putc(ctx, &gc, 'q');
674 	screen_write_putc(ctx, &gc, 'k');
675 
676 	screen_write_set_cursor(ctx, cx, cy + ny - 1);
677 	screen_write_putc(ctx, &gc, 'm');
678 	for (i = 1; i < nx - 1; i++)
679 		screen_write_putc(ctx, &gc, 'q');
680 	screen_write_putc(ctx, &gc, 'j');
681 
682 	for (i = 1; i < ny - 1; i++) {
683 		screen_write_set_cursor(ctx, cx, cy + i);
684 		screen_write_putc(ctx, &gc, 'x');
685 	}
686 	for (i = 1; i < ny - 1; i++) {
687 		screen_write_set_cursor(ctx, cx + nx - 1, cy + i);
688 		screen_write_putc(ctx, &gc, 'x');
689 	}
690 
691 	screen_write_set_cursor(ctx, cx, cy);
692 }
693 
694 /*
695  * Write a preview version of a window. Assumes target area is big enough and
696  * already cleared.
697  */
698 void
699 screen_write_preview(struct screen_write_ctx *ctx, struct screen *src, u_int nx,
700     u_int ny)
701 {
702 	struct screen		*s = ctx->s;
703 	struct grid_cell	 gc;
704 	u_int			 cx, cy, px, py;
705 
706 	cx = s->cx;
707 	cy = s->cy;
708 
709 	/*
710 	 * If the cursor is on, pick the area around the cursor, otherwise use
711 	 * the top left.
712 	 */
713 	if (src->mode & MODE_CURSOR) {
714 		px = src->cx;
715 		if (px < nx / 3)
716 			px = 0;
717 		else
718 			px = px - nx / 3;
719 		if (px + nx > screen_size_x(src)) {
720 			if (nx > screen_size_x(src))
721 				px = 0;
722 			else
723 				px = screen_size_x(src) - nx;
724 		}
725 		py = src->cy;
726 		if (py < ny / 3)
727 			py = 0;
728 		else
729 			py = py - ny / 3;
730 		if (py + ny > screen_size_y(src)) {
731 			if (ny > screen_size_y(src))
732 				py = 0;
733 			else
734 				py = screen_size_y(src) - ny;
735 		}
736 	} else {
737 		px = 0;
738 		py = 0;
739 	}
740 
741 	screen_write_fast_copy(ctx, src, px, src->grid->hsize + py, nx, ny);
742 
743 	if (src->mode & MODE_CURSOR) {
744 		grid_view_get_cell(src->grid, src->cx, src->cy, &gc);
745 		gc.attr |= GRID_ATTR_REVERSE;
746 		screen_write_set_cursor(ctx, cx + (src->cx - px),
747 		    cy + (src->cy - py));
748 		screen_write_cell(ctx, &gc);
749 	}
750 }
751 
752 /* Set a mode. */
753 void
754 screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
755 {
756 	struct screen	*s = ctx->s;
757 
758 	s->mode |= mode;
759 }
760 
761 /* Clear a mode. */
762 void
763 screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
764 {
765 	struct screen	*s = ctx->s;
766 
767 	s->mode &= ~mode;
768 }
769 
770 /* Cursor up by ny. */
771 void
772 screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
773 {
774 	struct screen	*s = ctx->s;
775 	u_int		 cx = s->cx, cy = s->cy;
776 
777 	if (ny == 0)
778 		ny = 1;
779 
780 	if (cy < s->rupper) {
781 		/* Above region. */
782 		if (ny > cy)
783 			ny = cy;
784 	} else {
785 		/* Below region. */
786 		if (ny > cy - s->rupper)
787 			ny = cy - s->rupper;
788 	}
789 	if (cx == screen_size_x(s))
790 		cx--;
791 
792 	cy -= ny;
793 
794 	screen_write_set_cursor(ctx, cx, cy);
795 }
796 
797 /* Cursor down by ny. */
798 void
799 screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
800 {
801 	struct screen	*s = ctx->s;
802 	u_int		 cx = s->cx, cy = s->cy;
803 
804 	if (ny == 0)
805 		ny = 1;
806 
807 	if (cy > s->rlower) {
808 		/* Below region. */
809 		if (ny > screen_size_y(s) - 1 - cy)
810 			ny = screen_size_y(s) - 1 - cy;
811 	} else {
812 		/* Above region. */
813 		if (ny > s->rlower - cy)
814 			ny = s->rlower - cy;
815 	}
816 	if (cx == screen_size_x(s))
817 	    cx--;
818 	else if (ny == 0)
819 		return;
820 
821 	cy += ny;
822 
823 	screen_write_set_cursor(ctx, cx, cy);
824 }
825 
826 /* Cursor right by nx. */
827 void
828 screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
829 {
830 	struct screen	*s = ctx->s;
831 	u_int		 cx = s->cx, cy = s->cy;
832 
833 	if (nx == 0)
834 		nx = 1;
835 
836 	if (nx > screen_size_x(s) - 1 - cx)
837 		nx = screen_size_x(s) - 1 - cx;
838 	if (nx == 0)
839 		return;
840 
841 	cx += nx;
842 
843 	screen_write_set_cursor(ctx, cx, cy);
844 }
845 
846 /* Cursor left by nx. */
847 void
848 screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
849 {
850 	struct screen	*s = ctx->s;
851 	u_int		 cx = s->cx, cy = s->cy;
852 
853 	if (nx == 0)
854 		nx = 1;
855 
856 	if (nx > cx)
857 		nx = cx;
858 	if (nx == 0)
859 		return;
860 
861 	cx -= nx;
862 
863 	screen_write_set_cursor(ctx, cx, cy);
864 }
865 
866 /* Backspace; cursor left unless at start of wrapped line when can move up. */
867 void
868 screen_write_backspace(struct screen_write_ctx *ctx)
869 {
870 	struct screen		*s = ctx->s;
871 	struct grid_line	*gl;
872 	u_int			 cx = s->cx, cy = s->cy;
873 
874 	if (cx == 0) {
875 		if (cy == 0)
876 			return;
877 		gl = grid_get_line(s->grid, s->grid->hsize + cy - 1);
878 		if (gl->flags & GRID_LINE_WRAPPED) {
879 			cy--;
880 			cx = screen_size_x(s) - 1;
881 		}
882 	} else
883 		cx--;
884 
885 	screen_write_set_cursor(ctx, cx, cy);
886 }
887 
888 /* VT100 alignment test. */
889 void
890 screen_write_alignmenttest(struct screen_write_ctx *ctx)
891 {
892 	struct screen		*s = ctx->s;
893 	struct tty_ctx	 	 ttyctx;
894 	struct grid_cell       	 gc;
895 	u_int			 xx, yy;
896 
897 	memcpy(&gc, &grid_default_cell, sizeof gc);
898 	utf8_set(&gc.data, 'E');
899 
900 	for (yy = 0; yy < screen_size_y(s); yy++) {
901 		for (xx = 0; xx < screen_size_x(s); xx++)
902 			grid_view_set_cell(s->grid, xx, yy, &gc);
903 	}
904 
905 	screen_write_set_cursor(ctx, 0, 0);
906 
907 	s->rupper = 0;
908 	s->rlower = screen_size_y(s) - 1;
909 
910 	screen_write_initctx(ctx, &ttyctx, 1);
911 
912 	screen_write_collect_clear(ctx, 0, screen_size_y(s) - 1);
913 	tty_write(tty_cmd_alignmenttest, &ttyctx);
914 }
915 
916 /* Insert nx characters. */
917 void
918 screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
919 {
920 	struct screen	*s = ctx->s;
921 	struct tty_ctx	 ttyctx;
922 
923 	if (nx == 0)
924 		nx = 1;
925 
926 	if (nx > screen_size_x(s) - s->cx)
927 		nx = screen_size_x(s) - s->cx;
928 	if (nx == 0)
929 		return;
930 
931 	if (s->cx > screen_size_x(s) - 1)
932 		return;
933 
934 	screen_write_initctx(ctx, &ttyctx, 0);
935 	ttyctx.bg = bg;
936 
937 	grid_view_insert_cells(s->grid, s->cx, s->cy, nx, bg);
938 
939 	screen_write_collect_flush(ctx, 0, __func__);
940 	ttyctx.num = nx;
941 	tty_write(tty_cmd_insertcharacter, &ttyctx);
942 }
943 
944 /* Delete nx characters. */
945 void
946 screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
947 {
948 	struct screen	*s = ctx->s;
949 	struct tty_ctx	 ttyctx;
950 
951 	if (nx == 0)
952 		nx = 1;
953 
954 	if (nx > screen_size_x(s) - s->cx)
955 		nx = screen_size_x(s) - s->cx;
956 	if (nx == 0)
957 		return;
958 
959 	if (s->cx > screen_size_x(s) - 1)
960 		return;
961 
962 	screen_write_initctx(ctx, &ttyctx, 0);
963 	ttyctx.bg = bg;
964 
965 	grid_view_delete_cells(s->grid, s->cx, s->cy, nx, bg);
966 
967 	screen_write_collect_flush(ctx, 0, __func__);
968 	ttyctx.num = nx;
969 	tty_write(tty_cmd_deletecharacter, &ttyctx);
970 }
971 
972 /* Clear nx characters. */
973 void
974 screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
975 {
976 	struct screen	*s = ctx->s;
977 	struct tty_ctx	 ttyctx;
978 
979 	if (nx == 0)
980 		nx = 1;
981 
982 	if (nx > screen_size_x(s) - s->cx)
983 		nx = screen_size_x(s) - s->cx;
984 	if (nx == 0)
985 		return;
986 
987 	if (s->cx > screen_size_x(s) - 1)
988 		return;
989 
990 	screen_write_initctx(ctx, &ttyctx, 0);
991 	ttyctx.bg = bg;
992 
993 	grid_view_clear(s->grid, s->cx, s->cy, nx, 1, bg);
994 
995 	screen_write_collect_flush(ctx, 0, __func__);
996 	ttyctx.num = nx;
997 	tty_write(tty_cmd_clearcharacter, &ttyctx);
998 }
999 
1000 /* Insert ny lines. */
1001 void
1002 screen_write_insertline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1003 {
1004 	struct screen	*s = ctx->s;
1005 	struct grid	*gd = s->grid;
1006 	struct tty_ctx	 ttyctx;
1007 
1008 	if (ny == 0)
1009 		ny = 1;
1010 
1011 	if (s->cy < s->rupper || s->cy > s->rlower) {
1012 		if (ny > screen_size_y(s) - s->cy)
1013 			ny = screen_size_y(s) - s->cy;
1014 		if (ny == 0)
1015 			return;
1016 
1017 		screen_write_initctx(ctx, &ttyctx, 1);
1018 		ttyctx.bg = bg;
1019 
1020 		grid_view_insert_lines(gd, s->cy, ny, bg);
1021 
1022 		screen_write_collect_flush(ctx, 0, __func__);
1023 		ttyctx.num = ny;
1024 		tty_write(tty_cmd_insertline, &ttyctx);
1025 		return;
1026 	}
1027 
1028 	if (ny > s->rlower + 1 - s->cy)
1029 		ny = s->rlower + 1 - s->cy;
1030 	if (ny == 0)
1031 		return;
1032 
1033 	screen_write_initctx(ctx, &ttyctx, 1);
1034 	ttyctx.bg = bg;
1035 
1036 	if (s->cy < s->rupper || s->cy > s->rlower)
1037 		grid_view_insert_lines(gd, s->cy, ny, bg);
1038 	else
1039 		grid_view_insert_lines_region(gd, s->rlower, s->cy, ny, bg);
1040 
1041 	screen_write_collect_flush(ctx, 0, __func__);
1042 
1043 	ttyctx.num = ny;
1044 	tty_write(tty_cmd_insertline, &ttyctx);
1045 }
1046 
1047 /* Delete ny lines. */
1048 void
1049 screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1050 {
1051 	struct screen	*s = ctx->s;
1052 	struct grid	*gd = s->grid;
1053 	struct tty_ctx	 ttyctx;
1054 
1055 	if (ny == 0)
1056 		ny = 1;
1057 
1058 	if (s->cy < s->rupper || s->cy > s->rlower) {
1059 		if (ny > screen_size_y(s) - s->cy)
1060 			ny = screen_size_y(s) - s->cy;
1061 		if (ny == 0)
1062 			return;
1063 
1064 		screen_write_initctx(ctx, &ttyctx, 1);
1065 		ttyctx.bg = bg;
1066 
1067 		grid_view_delete_lines(gd, s->cy, ny, bg);
1068 
1069 		screen_write_collect_flush(ctx, 0, __func__);
1070 		ttyctx.num = ny;
1071 		tty_write(tty_cmd_deleteline, &ttyctx);
1072 		return;
1073 	}
1074 
1075 	if (ny > s->rlower + 1 - s->cy)
1076 		ny = s->rlower + 1 - s->cy;
1077 	if (ny == 0)
1078 		return;
1079 
1080 	screen_write_initctx(ctx, &ttyctx, 1);
1081 	ttyctx.bg = bg;
1082 
1083 	if (s->cy < s->rupper || s->cy > s->rlower)
1084 		grid_view_delete_lines(gd, s->cy, ny, bg);
1085 	else
1086 		grid_view_delete_lines_region(gd, s->rlower, s->cy, ny, bg);
1087 
1088 	screen_write_collect_flush(ctx, 0, __func__);
1089 	ttyctx.num = ny;
1090 	tty_write(tty_cmd_deleteline, &ttyctx);
1091 }
1092 
1093 /* Clear line at cursor. */
1094 void
1095 screen_write_clearline(struct screen_write_ctx *ctx, u_int bg)
1096 {
1097 	struct screen		 *s = ctx->s;
1098 	struct grid_line	 *gl;
1099 	u_int			  sx = screen_size_x(s);
1100 
1101 	gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1102 	if (gl->cellsize == 0 && COLOUR_DEFAULT(bg))
1103 		return;
1104 
1105 	grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1106 
1107 	screen_write_collect_clear(ctx, s->cy, 1);
1108 	ctx->s->write_list[s->cy].bg = 1 + bg;
1109 	ctx->item->used = 0;
1110 }
1111 
1112 /* Clear to end of line from cursor. */
1113 void
1114 screen_write_clearendofline(struct screen_write_ctx *ctx, u_int bg)
1115 {
1116 	struct screen			 *s = ctx->s;
1117 	struct grid_line		 *gl;
1118 	u_int				  sx = screen_size_x(s);
1119 	struct screen_write_collect_item *ci = ctx->item;
1120 
1121 	if (s->cx == 0) {
1122 		screen_write_clearline(ctx, bg);
1123 		return;
1124 	}
1125 
1126 	gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1127 	if (s->cx > sx - 1 || (s->cx >= gl->cellsize && COLOUR_DEFAULT(bg)))
1128 		return;
1129 
1130 	grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1, bg);
1131 
1132  	screen_write_collect_clear_end(ctx, s->cy, s->cx);
1133 	ci->x = s->cx;
1134 	ci->type = CLEAR_END;
1135 	ci->bg = bg;
1136 	TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry);
1137 	ctx->item = xcalloc(1, sizeof *ctx->item);
1138 }
1139 
1140 /* Clear to start of line from cursor. */
1141 void
1142 screen_write_clearstartofline(struct screen_write_ctx *ctx, u_int bg)
1143 {
1144 	struct screen			 *s = ctx->s;
1145 	u_int				  sx = screen_size_x(s);
1146 	struct screen_write_collect_item *ci = ctx->item;
1147 
1148 	if (s->cx >= sx - 1) {
1149 		screen_write_clearline(ctx, bg);
1150 		return;
1151 	}
1152 
1153 	if (s->cx > sx - 1)
1154 		grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1155 	else
1156 		grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1157 
1158 	screen_write_collect_clear_start(ctx, s->cy, s->cx);
1159 	ci->x = s->cx;
1160 	ci->type = CLEAR_START;
1161 	ci->bg = bg;
1162 	TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry);
1163 	ctx->item = xcalloc(1, sizeof *ctx->item);
1164 }
1165 
1166 /* Move cursor to px,py. */
1167 void
1168 screen_write_cursormove(struct screen_write_ctx *ctx, int px, int py,
1169     int origin)
1170 {
1171 	struct screen	*s = ctx->s;
1172 
1173 	if (origin && py != -1 && (s->mode & MODE_ORIGIN)) {
1174 		if ((u_int)py > s->rlower - s->rupper)
1175 			py = s->rlower;
1176 		else
1177 			py += s->rupper;
1178 	}
1179 
1180 	if (px != -1 && (u_int)px > screen_size_x(s) - 1)
1181 		px = screen_size_x(s) - 1;
1182 	if (py != -1 && (u_int)py > screen_size_y(s) - 1)
1183 		py = screen_size_y(s) - 1;
1184 
1185 	screen_write_set_cursor(ctx, px, py);
1186 }
1187 
1188 /* Reverse index (up with scroll). */
1189 void
1190 screen_write_reverseindex(struct screen_write_ctx *ctx, u_int bg)
1191 {
1192 	struct screen	*s = ctx->s;
1193 	struct tty_ctx	 ttyctx;
1194 
1195 	if (s->cy == s->rupper) {
1196 		grid_view_scroll_region_down(s->grid, s->rupper, s->rlower, bg);
1197 		screen_write_collect_flush(ctx, 0, __func__);
1198 
1199 		screen_write_initctx(ctx, &ttyctx, 1);
1200 		ttyctx.bg = bg;
1201 
1202 		tty_write(tty_cmd_reverseindex, &ttyctx);
1203 	} else if (s->cy > 0)
1204 		screen_write_set_cursor(ctx, -1, s->cy - 1);
1205 
1206 }
1207 
1208 /* Set scroll region. */
1209 void
1210 screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper,
1211     u_int rlower)
1212 {
1213 	struct screen	*s = ctx->s;
1214 
1215 	if (rupper > screen_size_y(s) - 1)
1216 		rupper = screen_size_y(s) - 1;
1217 	if (rlower > screen_size_y(s) - 1)
1218 		rlower = screen_size_y(s) - 1;
1219 	if (rupper >= rlower)	/* cannot be one line */
1220 		return;
1221 
1222 	screen_write_collect_flush(ctx, 0, __func__);
1223 
1224 	/* Cursor moves to top-left. */
1225 	screen_write_set_cursor(ctx, 0, 0);
1226 
1227 	s->rupper = rupper;
1228 	s->rlower = rlower;
1229 }
1230 
1231 /* Line feed. */
1232 void
1233 screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped, u_int bg)
1234 {
1235 	struct screen		*s = ctx->s;
1236 	struct grid		*gd = s->grid;
1237 	struct grid_line	*gl;
1238 
1239 	gl = grid_get_line(gd, gd->hsize + s->cy);
1240 	if (wrapped)
1241 		gl->flags |= GRID_LINE_WRAPPED;
1242 
1243 	log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
1244 	    s->rupper, s->rlower);
1245 
1246 	if (bg != ctx->bg) {
1247 		screen_write_collect_flush(ctx, 1, __func__);
1248 		ctx->bg = bg;
1249 	}
1250 
1251 	if (s->cy == s->rlower) {
1252 		grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1253 		screen_write_collect_scroll(ctx);
1254 		ctx->scrolled++;
1255 	} else if (s->cy < screen_size_y(s) - 1)
1256 		screen_write_set_cursor(ctx, -1, s->cy + 1);
1257 }
1258 
1259 /* Scroll up. */
1260 void
1261 screen_write_scrollup(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1262 {
1263 	struct screen	*s = ctx->s;
1264 	struct grid	*gd = s->grid;
1265 	u_int		 i;
1266 
1267 	if (lines == 0)
1268 		lines = 1;
1269 	else if (lines > s->rlower - s->rupper + 1)
1270 		lines = s->rlower - s->rupper + 1;
1271 
1272 	if (bg != ctx->bg) {
1273 		screen_write_collect_flush(ctx, 1, __func__);
1274 		ctx->bg = bg;
1275 	}
1276 
1277 	for (i = 0; i < lines; i++) {
1278 		grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1279 		screen_write_collect_scroll(ctx);
1280 	}
1281 	ctx->scrolled += lines;
1282 }
1283 
1284 /* Scroll down. */
1285 void
1286 screen_write_scrolldown(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1287 {
1288 	struct screen	*s = ctx->s;
1289 	struct grid	*gd = s->grid;
1290 	struct tty_ctx	 ttyctx;
1291 	u_int		 i;
1292 
1293 	screen_write_initctx(ctx, &ttyctx, 1);
1294 	ttyctx.bg = bg;
1295 
1296 	if (lines == 0)
1297 		lines = 1;
1298 	else if (lines > s->rlower - s->rupper + 1)
1299 		lines = s->rlower - s->rupper + 1;
1300 
1301 	for (i = 0; i < lines; i++)
1302 		grid_view_scroll_region_down(gd, s->rupper, s->rlower, bg);
1303 
1304 	screen_write_collect_flush(ctx, 0, __func__);
1305 	ttyctx.num = lines;
1306 	tty_write(tty_cmd_scrolldown, &ttyctx);
1307 }
1308 
1309 /* Carriage return (cursor to start of line). */
1310 void
1311 screen_write_carriagereturn(struct screen_write_ctx *ctx)
1312 {
1313 	screen_write_set_cursor(ctx, 0, -1);
1314 }
1315 
1316 /* Clear to end of screen from cursor. */
1317 void
1318 screen_write_clearendofscreen(struct screen_write_ctx *ctx, u_int bg)
1319 {
1320 	struct screen	*s = ctx->s;
1321 	struct grid	*gd = s->grid;
1322 	struct tty_ctx	 ttyctx;
1323 	u_int		 sx = screen_size_x(s), sy = screen_size_y(s);
1324 
1325 	screen_write_initctx(ctx, &ttyctx, 1);
1326 	ttyctx.bg = bg;
1327 
1328 	/* Scroll into history if it is enabled and clearing entire screen. */
1329 	if (s->cx == 0 && s->cy == 0 && (gd->flags & GRID_HISTORY))
1330 		grid_view_clear_history(gd, bg);
1331 	else {
1332 		if (s->cx <= sx - 1)
1333 			grid_view_clear(gd, s->cx, s->cy, sx - s->cx, 1, bg);
1334 		grid_view_clear(gd, 0, s->cy + 1, sx, sy - (s->cy + 1), bg);
1335 	}
1336 
1337 	screen_write_collect_clear(ctx, s->cy + 1, sy - (s->cy + 1));
1338 	screen_write_collect_flush(ctx, 0, __func__);
1339 	tty_write(tty_cmd_clearendofscreen, &ttyctx);
1340 }
1341 
1342 /* Clear to start of screen. */
1343 void
1344 screen_write_clearstartofscreen(struct screen_write_ctx *ctx, u_int bg)
1345 {
1346 	struct screen	*s = ctx->s;
1347 	struct tty_ctx	 ttyctx;
1348 	u_int		 sx = screen_size_x(s);
1349 
1350 	screen_write_initctx(ctx, &ttyctx, 1);
1351 	ttyctx.bg = bg;
1352 
1353 	if (s->cy > 0)
1354 		grid_view_clear(s->grid, 0, 0, sx, s->cy, bg);
1355 	if (s->cx > sx - 1)
1356 		grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1357 	else
1358 		grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1359 
1360 	screen_write_collect_clear(ctx, 0, s->cy);
1361 	screen_write_collect_flush(ctx, 0, __func__);
1362 	tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1363 }
1364 
1365 /* Clear entire screen. */
1366 void
1367 screen_write_clearscreen(struct screen_write_ctx *ctx, u_int bg)
1368 {
1369 	struct screen	*s = ctx->s;
1370 	struct tty_ctx	 ttyctx;
1371 	u_int		 sx = screen_size_x(s), sy = screen_size_y(s);
1372 
1373 	screen_write_initctx(ctx, &ttyctx, 1);
1374 	ttyctx.bg = bg;
1375 
1376 	/* Scroll into history if it is enabled. */
1377 	if (s->grid->flags & GRID_HISTORY)
1378 		grid_view_clear_history(s->grid, bg);
1379 	else
1380 		grid_view_clear(s->grid, 0, 0, sx, sy, bg);
1381 
1382 	screen_write_collect_clear(ctx, 0, sy);
1383 	tty_write(tty_cmd_clearscreen, &ttyctx);
1384 }
1385 
1386 /* Clear entire history. */
1387 void
1388 screen_write_clearhistory(struct screen_write_ctx *ctx)
1389 {
1390 	grid_clear_history(ctx->s->grid);
1391 }
1392 
1393 /* Clear to start of a collected line. */
1394 static void
1395 screen_write_collect_clear_start(struct screen_write_ctx *ctx, u_int y, u_int x)
1396 {
1397 	struct screen_write_collect_item	*ci, *tmp;
1398 	size_t					 size = 0;
1399 	u_int					 items = 0;
1400 
1401 	if (TAILQ_EMPTY(&ctx->s->write_list[y].items))
1402 		return;
1403 	TAILQ_FOREACH_SAFE(ci, &ctx->s->write_list[y].items, entry, tmp) {
1404 		switch (ci->type) {
1405 		case CLEAR_START:
1406 			break;
1407 		case CLEAR_END:
1408 			if (ci->x <= x)
1409 				ci->x = x;
1410 			continue;
1411 		case TEXT:
1412 			if (ci->x > x)
1413 				continue;
1414 			break;
1415 		}
1416 		items++;
1417 		size += ci->used;
1418 		TAILQ_REMOVE(&ctx->s->write_list[y].items, ci, entry);
1419 		free(ci);
1420 	}
1421 	ctx->skipped += size;
1422 	log_debug("%s: dropped %u items (%zu bytes) (line %u)", __func__, items,
1423 	    size, y);
1424 }
1425 
1426 /* Clear to end of a collected line. */
1427 static void
1428 screen_write_collect_clear_end(struct screen_write_ctx *ctx, u_int y, u_int x)
1429 {
1430 	struct screen_write_collect_item	*ci, *tmp;
1431 	size_t					 size = 0;
1432 	u_int					 items = 0;
1433 
1434 	if (TAILQ_EMPTY(&ctx->s->write_list[y].items))
1435 		return;
1436 	TAILQ_FOREACH_SAFE(ci, &ctx->s->write_list[y].items, entry, tmp) {
1437 		switch (ci->type) {
1438 		case CLEAR_START:
1439 			if (ci->x >= x)
1440 				ci->x = x;
1441 			continue;
1442 		case CLEAR_END:
1443 			break;
1444 		case TEXT:
1445 			if (ci->x < x)
1446 				continue;
1447 			break;
1448 		}
1449 		items++;
1450 		size += ci->used;
1451 		TAILQ_REMOVE(&ctx->s->write_list[y].items, ci, entry);
1452 		free(ci);
1453 	}
1454 	ctx->skipped += size;
1455 	log_debug("%s: dropped %u items (%zu bytes) (line %u)", __func__, items,
1456 	    size, y);
1457 }
1458 
1459 /* Clear collected lines. */
1460 static void
1461 screen_write_collect_clear(struct screen_write_ctx *ctx, u_int y, u_int n)
1462 {
1463 	struct screen_write_collect_item	*ci, *tmp;
1464 	struct screen_write_collect_line	*cl;
1465 	u_int					 i, items;
1466 	size_t					 size;
1467 
1468 	for (i = y; i < y + n; i++) {
1469 		if (TAILQ_EMPTY(&ctx->s->write_list[i].items))
1470 			continue;
1471 		items = 0;
1472 		size = 0;
1473 		cl = &ctx->s->write_list[i];
1474 		TAILQ_FOREACH_SAFE(ci, &cl->items, entry, tmp) {
1475 			items++;
1476 			size += ci->used;
1477 			TAILQ_REMOVE(&cl->items, ci, entry);
1478 			free(ci);
1479 		}
1480 		ctx->skipped += size;
1481 		log_debug("%s: dropped %u items (%zu bytes) (line %u)",
1482 		    __func__, items, size, y);
1483 	}
1484 }
1485 
1486 /* Scroll collected lines up. */
1487 static void
1488 screen_write_collect_scroll(struct screen_write_ctx *ctx)
1489 {
1490 	struct screen				*s = ctx->s;
1491 	struct screen_write_collect_line	*cl;
1492 	u_int					 y;
1493 	char					*saved;
1494 
1495 	log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
1496 	    s->rupper, s->rlower);
1497 
1498 	screen_write_collect_clear(ctx, s->rupper, 1);
1499 	saved = ctx->s->write_list[s->rupper].data;
1500 	for (y = s->rupper; y < s->rlower; y++) {
1501 		cl = &ctx->s->write_list[y + 1];
1502 		TAILQ_CONCAT(&ctx->s->write_list[y].items, &cl->items, entry);
1503 		ctx->s->write_list[y].bg = cl->bg;
1504 		ctx->s->write_list[y].data = cl->data;
1505 	}
1506 	ctx->s->write_list[s->rlower].bg = 1 + 8;
1507 	ctx->s->write_list[s->rlower].data = saved;
1508 }
1509 
1510 /* Flush collected lines. */
1511 static void
1512 screen_write_collect_flush(struct screen_write_ctx *ctx, int scroll_only,
1513     const char *from)
1514 {
1515 	struct screen				*s = ctx->s;
1516 	struct screen_write_collect_item	*ci, *tmp;
1517 	struct screen_write_collect_line	*cl;
1518 	u_int					 y, cx, cy, items = 0;
1519 	struct tty_ctx				 ttyctx;
1520 	size_t					 written = 0;
1521 
1522 	if (ctx->scrolled != 0) {
1523 		log_debug("%s: scrolled %u (region %u-%u)", __func__,
1524 		    ctx->scrolled, s->rupper, s->rlower);
1525 		if (ctx->scrolled > s->rlower - s->rupper + 1)
1526 			ctx->scrolled = s->rlower - s->rupper + 1;
1527 
1528 		screen_write_initctx(ctx, &ttyctx, 1);
1529 		ttyctx.num = ctx->scrolled;
1530 		ttyctx.bg = ctx->bg;
1531 		tty_write(tty_cmd_scrollup, &ttyctx);
1532 	}
1533 	ctx->scrolled = 0;
1534 	ctx->bg = 8;
1535 
1536 	if (scroll_only)
1537 		return;
1538 
1539 	cx = s->cx; cy = s->cy;
1540 	for (y = 0; y < screen_size_y(s); y++) {
1541 		cl = &ctx->s->write_list[y];
1542 		if (cl->bg != 0) {
1543 			screen_write_set_cursor(ctx, 0, y);
1544 			screen_write_initctx(ctx, &ttyctx, 1);
1545 			ttyctx.bg = cl->bg - 1;
1546 			tty_write(tty_cmd_clearline, &ttyctx);
1547 		}
1548 		TAILQ_FOREACH_SAFE(ci, &cl->items, entry, tmp) {
1549 			screen_write_set_cursor(ctx, ci->x, y);
1550 			if (ci->type == CLEAR_END) {
1551 				screen_write_initctx(ctx, &ttyctx, 1);
1552 				ttyctx.bg = ci->bg;
1553 				tty_write(tty_cmd_clearendofline, &ttyctx);
1554 			} else if (ci->type == CLEAR_START) {
1555 				screen_write_initctx(ctx, &ttyctx, 1);
1556 				ttyctx.bg = ci->bg;
1557 				tty_write(tty_cmd_clearstartofline, &ttyctx);
1558 			} else {
1559 				screen_write_initctx(ctx, &ttyctx, 0);
1560 				ttyctx.cell = &ci->gc;
1561 				ttyctx.wrapped = ci->wrapped;
1562 				ttyctx.ptr = cl->data + ci->x;
1563 				ttyctx.num = ci->used;
1564 				tty_write(tty_cmd_cells, &ttyctx);
1565 			}
1566 
1567 			items++;
1568 			written += ci->used;
1569 
1570 			TAILQ_REMOVE(&cl->items, ci, entry);
1571 			free(ci);
1572 		}
1573 		cl->bg = 0;
1574 	}
1575 	s->cx = cx; s->cy = cy;
1576 
1577 	log_debug("%s: flushed %u items (%zu bytes) (%s)", __func__, items,
1578 	    written, from);
1579 	ctx->written += written;
1580 }
1581 
1582 /* Finish and store collected cells. */
1583 void
1584 screen_write_collect_end(struct screen_write_ctx *ctx)
1585 {
1586 	struct screen				*s = ctx->s;
1587 	struct screen_write_collect_item	*ci = ctx->item;
1588 	struct screen_write_collect_line	*cl = &s->write_list[s->cy];
1589 	struct grid_cell			 gc;
1590 	u_int					 xx;
1591 
1592 	if (ci->used == 0)
1593 		return;
1594 
1595 	ci->x = s->cx;
1596 	TAILQ_INSERT_TAIL(&cl->items, ci, entry);
1597 	ctx->item = xcalloc(1, sizeof *ctx->item);
1598 
1599 	log_debug("%s: %u %.*s (at %u,%u)", __func__, ci->used,
1600 	    (int)ci->used, cl->data + ci->x, s->cx, s->cy);
1601 
1602 	if (s->cx != 0) {
1603 		for (xx = s->cx; xx > 0; xx--) {
1604 			grid_view_get_cell(s->grid, xx, s->cy, &gc);
1605 			if (~gc.flags & GRID_FLAG_PADDING)
1606 				break;
1607 			grid_view_set_cell(s->grid, xx, s->cy,
1608 			    &grid_default_cell);
1609 		}
1610 		if (gc.data.width > 1) {
1611 			grid_view_set_cell(s->grid, xx, s->cy,
1612 			    &grid_default_cell);
1613 		}
1614 	}
1615 
1616 	grid_view_set_cells(s->grid, s->cx, s->cy, &ci->gc, cl->data + ci->x,
1617 	    ci->used);
1618 	screen_write_set_cursor(ctx, s->cx + ci->used, -1);
1619 
1620 	for (xx = s->cx; xx < screen_size_x(s); xx++) {
1621 		grid_view_get_cell(s->grid, xx, s->cy, &gc);
1622 		if (~gc.flags & GRID_FLAG_PADDING)
1623 			break;
1624 		grid_view_set_cell(s->grid, xx, s->cy, &grid_default_cell);
1625 	}
1626 }
1627 
1628 /* Write cell data, collecting if necessary. */
1629 void
1630 screen_write_collect_add(struct screen_write_ctx *ctx,
1631     const struct grid_cell *gc)
1632 {
1633 	struct screen				*s = ctx->s;
1634 	struct screen_write_collect_item	*ci;
1635 	u_int					 sx = screen_size_x(s);
1636 	int					 collect;
1637 
1638 	/*
1639 	 * Don't need to check that the attributes and whatnot are still the
1640 	 * same - input_parse will end the collection when anything that isn't
1641 	 * a plain character is encountered.
1642 	 */
1643 
1644 	collect = 1;
1645 	if (gc->data.width != 1 || gc->data.size != 1 || *gc->data.data >= 0x7f)
1646 		collect = 0;
1647 	else if (gc->attr & GRID_ATTR_CHARSET)
1648 		collect = 0;
1649 	else if (~s->mode & MODE_WRAP)
1650 		collect = 0;
1651 	else if (s->mode & MODE_INSERT)
1652 		collect = 0;
1653 	else if (s->sel != NULL)
1654 		collect = 0;
1655 	if (!collect) {
1656 		screen_write_collect_end(ctx);
1657 		screen_write_collect_flush(ctx, 0, __func__);
1658 		screen_write_cell(ctx, gc);
1659 		return;
1660 	}
1661 	ctx->cells++;
1662 
1663 	if (s->cx > sx - 1 || ctx->item->used > sx - 1 - s->cx)
1664 		screen_write_collect_end(ctx);
1665 	ci = ctx->item; /* may have changed */
1666 
1667 	if (s->cx > sx - 1) {
1668 		log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1669 		ci->wrapped = 1;
1670 		screen_write_linefeed(ctx, 1, 8);
1671 		screen_write_set_cursor(ctx, 0, -1);
1672 	}
1673 
1674 	if (ci->used == 0)
1675 		memcpy(&ci->gc, gc, sizeof ci->gc);
1676 	if (ctx->s->write_list[s->cy].data == NULL)
1677 		ctx->s->write_list[s->cy].data = xmalloc(screen_size_x(ctx->s));
1678 	ctx->s->write_list[s->cy].data[s->cx + ci->used++] = gc->data.data[0];
1679 }
1680 
1681 /* Write cell data. */
1682 void
1683 screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1684 {
1685 	struct screen		*s = ctx->s;
1686 	struct grid		*gd = s->grid;
1687 	struct grid_line	*gl;
1688 	struct grid_cell_entry	*gce;
1689 	struct grid_cell 	 tmp_gc, now_gc;
1690 	struct tty_ctx		 ttyctx;
1691 	u_int			 sx = screen_size_x(s), sy = screen_size_y(s);
1692 	u_int		 	 width = gc->data.width, xx, last, cx, cy;
1693 	int			 selected, skip = 1;
1694 
1695 	/* Ignore padding cells. */
1696 	if (gc->flags & GRID_FLAG_PADDING)
1697 		return;
1698 	ctx->cells++;
1699 
1700 	/* If the width is zero, combine onto the previous character. */
1701 	if (width == 0) {
1702 		screen_write_collect_flush(ctx, 0, __func__);
1703 		if ((gc = screen_write_combine(ctx, &gc->data, &xx)) != 0) {
1704 			cx = s->cx; cy = s->cy;
1705 			screen_write_set_cursor(ctx, xx, s->cy);
1706 			screen_write_initctx(ctx, &ttyctx, 0);
1707 			ttyctx.cell = gc;
1708 			tty_write(tty_cmd_cell, &ttyctx);
1709 			s->cx = cx; s->cy = cy;
1710 		}
1711 		return;
1712 	}
1713 
1714 	/* Flush any existing scrolling. */
1715 	screen_write_collect_flush(ctx, 1, __func__);
1716 
1717 	/* If this character doesn't fit, ignore it. */
1718 	if ((~s->mode & MODE_WRAP) &&
1719 	    width > 1 &&
1720 	    (width > sx || (s->cx != sx && s->cx > sx - width)))
1721 		return;
1722 
1723 	/* If in insert mode, make space for the cells. */
1724 	if (s->mode & MODE_INSERT) {
1725 		grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8);
1726 		skip = 0;
1727 	}
1728 
1729 	/* Check this will fit on the current line and wrap if not. */
1730 	if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
1731 		log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1732 		screen_write_linefeed(ctx, 1, 8);
1733 		screen_write_set_cursor(ctx, 0, -1);
1734 		screen_write_collect_flush(ctx, 1, __func__);
1735 	}
1736 
1737 	/* Sanity check cursor position. */
1738 	if (s->cx > sx - width || s->cy > sy - 1)
1739 		return;
1740 	screen_write_initctx(ctx, &ttyctx, 0);
1741 
1742 	/* Handle overwriting of UTF-8 characters. */
1743 	gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1744 	if (gl->flags & GRID_LINE_EXTENDED) {
1745 		grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
1746 		if (screen_write_overwrite(ctx, &now_gc, width))
1747 			skip = 0;
1748 	}
1749 
1750 	/*
1751 	 * If the new character is UTF-8 wide, fill in padding cells. Have
1752 	 * already ensured there is enough room.
1753 	 */
1754 	for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1755 		log_debug("%s: new padding at %u,%u", __func__, xx, s->cy);
1756 		grid_view_set_padding(gd, xx, s->cy);
1757 		skip = 0;
1758 	}
1759 
1760 	/* If no change, do not draw. */
1761 	if (skip) {
1762 		if (s->cx >= gl->cellsize)
1763 			skip = grid_cells_equal(gc, &grid_default_cell);
1764 		else {
1765 			gce = &gl->celldata[s->cx];
1766 			if (gce->flags & GRID_FLAG_EXTENDED)
1767 				skip = 0;
1768 			else if (gc->flags != gce->flags)
1769 				skip = 0;
1770 			else if (gc->attr != gce->data.attr)
1771 				skip = 0;
1772 			else if (gc->fg != gce->data.fg)
1773 				skip = 0;
1774 			else if (gc->bg != gce->data.bg)
1775 				skip = 0;
1776 			else if (gc->data.width != 1)
1777 				skip = 0;
1778 			else if (gc->data.size != 1)
1779 				skip = 0;
1780 			else if (gce->data.data != gc->data.data[0])
1781 				skip = 0;
1782 		}
1783 	}
1784 
1785 	/* Update the selected flag and set the cell. */
1786 	selected = screen_check_selection(s, s->cx, s->cy);
1787 	if (selected && (~gc->flags & GRID_FLAG_SELECTED)) {
1788 		memcpy(&tmp_gc, gc, sizeof tmp_gc);
1789 		tmp_gc.flags |= GRID_FLAG_SELECTED;
1790 		grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1791 	} else if (!selected && (gc->flags & GRID_FLAG_SELECTED)) {
1792 		memcpy(&tmp_gc, gc, sizeof tmp_gc);
1793 		tmp_gc.flags &= ~GRID_FLAG_SELECTED;
1794 		grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1795 	} else if (!skip)
1796 		grid_view_set_cell(gd, s->cx, s->cy, gc);
1797 	if (selected)
1798 		skip = 0;
1799 
1800 	/*
1801 	 * Move the cursor. If not wrapping, stick at the last character and
1802 	 * replace it.
1803 	 */
1804 	last = !(s->mode & MODE_WRAP);
1805 	if (s->cx <= sx - last - width)
1806 		screen_write_set_cursor(ctx, s->cx + width, -1);
1807 	else
1808 		screen_write_set_cursor(ctx,  sx - last, -1);
1809 
1810 	/* Create space for character in insert mode. */
1811 	if (s->mode & MODE_INSERT) {
1812 		screen_write_collect_flush(ctx, 0, __func__);
1813 		ttyctx.num = width;
1814 		tty_write(tty_cmd_insertcharacter, &ttyctx);
1815 	}
1816 
1817 	/* Write to the screen. */
1818 	if (!skip) {
1819 		if (selected) {
1820 			screen_select_cell(s, &tmp_gc, gc);
1821 			ttyctx.cell = &tmp_gc;
1822 		} else
1823 			ttyctx.cell = gc;
1824 		tty_write(tty_cmd_cell, &ttyctx);
1825 		ctx->written++;
1826 	} else
1827 		ctx->skipped++;
1828 }
1829 
1830 /* Combine a UTF-8 zero-width character onto the previous. */
1831 static const struct grid_cell *
1832 screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud,
1833     u_int *xx)
1834 {
1835 	struct screen		*s = ctx->s;
1836 	struct grid		*gd = s->grid;
1837 	static struct grid_cell	 gc;
1838 	u_int			 n;
1839 
1840 	/* Can't combine if at 0. */
1841 	if (s->cx == 0)
1842 		return (NULL);
1843 
1844 	/* Empty data is out. */
1845 	if (ud->size == 0)
1846 		fatalx("UTF-8 data empty");
1847 
1848 	/* Retrieve the previous cell. */
1849 	for (n = 1; n <= s->cx; n++) {
1850 		grid_view_get_cell(gd, s->cx - n, s->cy, &gc);
1851 		if (~gc.flags & GRID_FLAG_PADDING)
1852 			break;
1853 	}
1854 	if (n > s->cx)
1855 		return (NULL);
1856 	*xx = s->cx - n;
1857 
1858 	/* Check there is enough space. */
1859 	if (gc.data.size + ud->size > sizeof gc.data.data)
1860 		return (NULL);
1861 
1862 	log_debug("%s: %.*s onto %.*s at %u,%u", __func__, (int)ud->size,
1863 	    ud->data, (int)gc.data.size, gc.data.data, *xx, s->cy);
1864 
1865 	/* Append the data. */
1866 	memcpy(gc.data.data + gc.data.size, ud->data, ud->size);
1867 	gc.data.size += ud->size;
1868 
1869 	/* Set the new cell. */
1870 	grid_view_set_cell(gd, *xx, s->cy, &gc);
1871 
1872 	return (&gc);
1873 }
1874 
1875 /*
1876  * UTF-8 wide characters are a bit of an annoyance. They take up more than one
1877  * cell on the screen, so following cells must not be drawn by marking them as
1878  * padding.
1879  *
1880  * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
1881  * character, it is necessary to also overwrite any other cells which covered
1882  * by the same character.
1883  */
1884 static int
1885 screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
1886     u_int width)
1887 {
1888 	struct screen		*s = ctx->s;
1889 	struct grid		*gd = s->grid;
1890 	struct grid_cell	 tmp_gc;
1891 	u_int			 xx;
1892 	int			 done = 0;
1893 
1894 	if (gc->flags & GRID_FLAG_PADDING) {
1895 		/*
1896 		 * A padding cell, so clear any following and leading padding
1897 		 * cells back to the character. Don't overwrite the current
1898 		 * cell as that happens later anyway.
1899 		 */
1900 		xx = s->cx + 1;
1901 		while (--xx > 0) {
1902 			grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
1903 			if (~tmp_gc.flags & GRID_FLAG_PADDING)
1904 				break;
1905 			log_debug("%s: padding at %u,%u", __func__, xx, s->cy);
1906 			grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1907 		}
1908 
1909 		/* Overwrite the character at the start of this padding. */
1910 		log_debug("%s: character at %u,%u", __func__, xx, s->cy);
1911 		grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1912 		done = 1;
1913 	}
1914 
1915 	/*
1916 	 * Overwrite any padding cells that belong to any UTF-8 characters
1917 	 * we'll be overwriting with the current character.
1918 	 */
1919 	if (width != 1 ||
1920 	    gc->data.width != 1 ||
1921 	    gc->flags & GRID_FLAG_PADDING) {
1922 		xx = s->cx + width - 1;
1923 		while (++xx < screen_size_x(s)) {
1924 			grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
1925 			if (~tmp_gc.flags & GRID_FLAG_PADDING)
1926 				break;
1927 			log_debug("%s: overwrite at %u,%u", __func__, xx,
1928 			    s->cy);
1929 			grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1930 			done = 1;
1931 		}
1932 	}
1933 
1934 	return (done);
1935 }
1936 
1937 /* Set external clipboard. */
1938 void
1939 screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len)
1940 {
1941 	struct tty_ctx	ttyctx;
1942 
1943 	screen_write_initctx(ctx, &ttyctx, 0);
1944 	ttyctx.ptr = str;
1945 	ttyctx.num = len;
1946 
1947 	tty_write(tty_cmd_setselection, &ttyctx);
1948 }
1949 
1950 /* Write unmodified string. */
1951 void
1952 screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len)
1953 {
1954 	struct tty_ctx	ttyctx;
1955 
1956 	screen_write_initctx(ctx, &ttyctx, 0);
1957 	ttyctx.ptr = str;
1958 	ttyctx.num = len;
1959 
1960 	tty_write(tty_cmd_rawstring, &ttyctx);
1961 }
1962 
1963 /* Turn alternate screen on. */
1964 void
1965 screen_write_alternateon(struct screen_write_ctx *ctx, struct grid_cell *gc,
1966     int cursor)
1967 {
1968 	struct tty_ctx		 ttyctx;
1969 	struct window_pane	*wp = ctx->wp;
1970 
1971 	if (wp != NULL && !options_get_number(wp->options, "alternate-screen"))
1972 		return;
1973 	screen_alternate_on(ctx->s, gc, cursor);
1974 
1975 	screen_write_initctx(ctx, &ttyctx, 1);
1976 	ttyctx.redraw_cb(&ttyctx);
1977 }
1978 
1979 /* Turn alternate screen off. */
1980 void
1981 screen_write_alternateoff(struct screen_write_ctx *ctx, struct grid_cell *gc,
1982     int cursor)
1983 {
1984 	struct tty_ctx		 ttyctx;
1985 	struct window_pane	*wp = ctx->wp;
1986 
1987 	if (wp != NULL && !options_get_number(wp->options, "alternate-screen"))
1988 		return;
1989 	screen_alternate_off(ctx->s, gc, cursor);
1990 
1991 	screen_write_initctx(ctx, &ttyctx, 1);
1992 	ttyctx.redraw_cb(&ttyctx);
1993 }
1994