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