Extend the menu drawing function to support custom characters and
[tmux-openbsd.git] / screen-write.c
blob177395a6ea2f233a1812b9be32a17a026e9d975a
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "tmux.h"
26 static struct screen_write_citem *screen_write_collect_trim(
27 struct screen_write_ctx *, u_int, u_int, u_int, int *);
28 static void screen_write_collect_clear(struct screen_write_ctx *, u_int,
29 u_int);
30 static void screen_write_collect_scroll(struct screen_write_ctx *, u_int);
31 static void screen_write_collect_flush(struct screen_write_ctx *, int,
32 const char *);
33 static int screen_write_overwrite(struct screen_write_ctx *,
34 struct grid_cell *, u_int);
35 static const struct grid_cell *screen_write_combine(struct screen_write_ctx *,
36 const struct utf8_data *, u_int *, u_int *);
38 struct screen_write_citem {
39 u_int x;
40 int wrapped;
42 enum { TEXT, CLEAR } type;
43 u_int used;
44 u_int bg;
46 struct grid_cell gc;
48 TAILQ_ENTRY(screen_write_citem) entry;
50 struct screen_write_cline {
51 char *data;
52 TAILQ_HEAD(, screen_write_citem) items;
54 TAILQ_HEAD(, screen_write_citem) screen_write_citem_freelist =
55 TAILQ_HEAD_INITIALIZER(screen_write_citem_freelist);
57 static struct screen_write_citem *
58 screen_write_get_citem(void)
60 struct screen_write_citem *ci;
62 ci = TAILQ_FIRST(&screen_write_citem_freelist);
63 if (ci != NULL) {
64 TAILQ_REMOVE(&screen_write_citem_freelist, ci, entry);
65 memset(ci, 0, sizeof *ci);
66 return (ci);
68 return (xcalloc(1, sizeof *ci));
71 static void
72 screen_write_free_citem(struct screen_write_citem *ci)
74 TAILQ_INSERT_TAIL(&screen_write_citem_freelist, ci, entry);
77 static void
78 screen_write_offset_timer(__unused int fd, __unused short events, void *data)
80 struct window *w = data;
82 tty_update_window_offset(w);
85 /* Set cursor position. */
86 static void
87 screen_write_set_cursor(struct screen_write_ctx *ctx, int cx, int cy)
89 struct window_pane *wp = ctx->wp;
90 struct window *w;
91 struct screen *s = ctx->s;
92 struct timeval tv = { .tv_usec = 10000 };
94 if (cx != -1 && (u_int)cx == s->cx && cy != -1 && (u_int)cy == s->cy)
95 return;
97 if (cx != -1) {
98 if ((u_int)cx > screen_size_x(s)) /* allow last column */
99 cx = screen_size_x(s) - 1;
100 s->cx = cx;
102 if (cy != -1) {
103 if ((u_int)cy > screen_size_y(s) - 1)
104 cy = screen_size_y(s) - 1;
105 s->cy = cy;
108 if (wp == NULL)
109 return;
110 w = wp->window;
112 if (!event_initialized(&w->offset_timer))
113 evtimer_set(&w->offset_timer, screen_write_offset_timer, w);
114 if (!evtimer_pending(&w->offset_timer, NULL))
115 evtimer_add(&w->offset_timer, &tv);
118 /* Do a full redraw. */
119 static void
120 screen_write_redraw_cb(const struct tty_ctx *ttyctx)
122 struct window_pane *wp = ttyctx->arg;
124 if (wp != NULL)
125 wp->flags |= PANE_REDRAW;
128 /* Update context for client. */
129 static int
130 screen_write_set_client_cb(struct tty_ctx *ttyctx, struct client *c)
132 struct window_pane *wp = ttyctx->arg;
134 if (ttyctx->allow_invisible_panes) {
135 if (session_has(c->session, wp->window))
136 return (1);
137 return (0);
140 if (c->session->curw->window != wp->window)
141 return (0);
142 if (wp->layout_cell == NULL)
143 return (0);
145 if (wp->flags & (PANE_REDRAW|PANE_DROP))
146 return (-1);
147 if (c->flags & CLIENT_REDRAWPANES) {
149 * Redraw is already deferred to redraw another pane - redraw
150 * this one also when that happens.
152 log_debug("%s: adding %%%u to deferred redraw", __func__,
153 wp->id);
154 wp->flags |= PANE_REDRAW;
155 return (-1);
158 ttyctx->bigger = tty_window_offset(&c->tty, &ttyctx->wox, &ttyctx->woy,
159 &ttyctx->wsx, &ttyctx->wsy);
161 ttyctx->xoff = ttyctx->rxoff = wp->xoff;
162 ttyctx->yoff = ttyctx->ryoff = wp->yoff;
164 if (status_at_line(c) == 0)
165 ttyctx->yoff += status_line_size(c);
167 return (1);
170 /* Set up context for TTY command. */
171 static void
172 screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx,
173 int sync)
175 struct screen *s = ctx->s;
177 memset(ttyctx, 0, sizeof *ttyctx);
179 ttyctx->s = s;
180 ttyctx->sx = screen_size_x(s);
181 ttyctx->sy = screen_size_y(s);
183 ttyctx->ocx = s->cx;
184 ttyctx->ocy = s->cy;
185 ttyctx->orlower = s->rlower;
186 ttyctx->orupper = s->rupper;
188 memcpy(&ttyctx->defaults, &grid_default_cell, sizeof ttyctx->defaults);
189 if (ctx->init_ctx_cb != NULL) {
190 ctx->init_ctx_cb(ctx, ttyctx);
191 if (ttyctx->palette != NULL) {
192 if (ttyctx->defaults.fg == 8)
193 ttyctx->defaults.fg = ttyctx->palette->fg;
194 if (ttyctx->defaults.bg == 8)
195 ttyctx->defaults.bg = ttyctx->palette->bg;
197 } else {
198 ttyctx->redraw_cb = screen_write_redraw_cb;
199 if (ctx->wp != NULL) {
200 tty_default_colours(&ttyctx->defaults, ctx->wp);
201 ttyctx->palette = &ctx->wp->palette;
202 ttyctx->set_client_cb = screen_write_set_client_cb;
203 ttyctx->arg = ctx->wp;
207 if (~ctx->flags & SCREEN_WRITE_SYNC) {
209 * For the active pane or for an overlay (no pane), we want to
210 * only use synchronized updates if requested (commands that
211 * move the cursor); for other panes, always use it, since the
212 * cursor will have to move.
214 if (ctx->wp != NULL) {
215 if (ctx->wp != ctx->wp->window->active)
216 ttyctx->num = 1;
217 else
218 ttyctx->num = sync;
219 } else
220 ttyctx->num = 0x10|sync;
221 tty_write(tty_cmd_syncstart, ttyctx);
222 ctx->flags |= SCREEN_WRITE_SYNC;
226 /* Make write list. */
227 void
228 screen_write_make_list(struct screen *s)
230 u_int y;
232 s->write_list = xcalloc(screen_size_y(s), sizeof *s->write_list);
233 for (y = 0; y < screen_size_y(s); y++)
234 TAILQ_INIT(&s->write_list[y].items);
237 /* Free write list. */
238 void
239 screen_write_free_list(struct screen *s)
241 u_int y;
243 for (y = 0; y < screen_size_y(s); y++)
244 free(s->write_list[y].data);
245 free(s->write_list);
248 /* Set up for writing. */
249 static void
250 screen_write_init(struct screen_write_ctx *ctx, struct screen *s)
252 memset(ctx, 0, sizeof *ctx);
254 ctx->s = s;
256 if (ctx->s->write_list == NULL)
257 screen_write_make_list(ctx->s);
258 ctx->item = screen_write_get_citem();
260 ctx->scrolled = 0;
261 ctx->bg = 8;
264 /* Initialize writing with a pane. */
265 void
266 screen_write_start_pane(struct screen_write_ctx *ctx, struct window_pane *wp,
267 struct screen *s)
269 if (s == NULL)
270 s = wp->screen;
271 screen_write_init(ctx, s);
272 ctx->wp = wp;
274 if (log_get_level() != 0) {
275 log_debug("%s: size %ux%u, pane %%%u (at %u,%u)",
276 __func__, screen_size_x(ctx->s), screen_size_y(ctx->s),
277 wp->id, wp->xoff, wp->yoff);
281 /* Initialize writing with a callback. */
282 void
283 screen_write_start_callback(struct screen_write_ctx *ctx, struct screen *s,
284 screen_write_init_ctx_cb cb, void *arg)
286 screen_write_init(ctx, s);
288 ctx->init_ctx_cb = cb;
289 ctx->arg = arg;
291 if (log_get_level() != 0) {
292 log_debug("%s: size %ux%u, with callback", __func__,
293 screen_size_x(ctx->s), screen_size_y(ctx->s));
297 /* Initialize writing. */
298 void
299 screen_write_start(struct screen_write_ctx *ctx, struct screen *s)
301 screen_write_init(ctx, s);
303 if (log_get_level() != 0) {
304 log_debug("%s: size %ux%u, no pane", __func__,
305 screen_size_x(ctx->s), screen_size_y(ctx->s));
309 /* Finish writing. */
310 void
311 screen_write_stop(struct screen_write_ctx *ctx)
313 screen_write_collect_end(ctx);
314 screen_write_collect_flush(ctx, 0, __func__);
316 screen_write_free_citem(ctx->item);
319 /* Reset screen state. */
320 void
321 screen_write_reset(struct screen_write_ctx *ctx)
323 struct screen *s = ctx->s;
325 screen_reset_tabs(s);
326 screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
328 s->mode = MODE_CURSOR|MODE_WRAP;
329 if (options_get_number(global_options, "extended-keys") == 2)
330 s->mode |= MODE_KEXTENDED;
332 screen_write_clearscreen(ctx, 8);
333 screen_write_set_cursor(ctx, 0, 0);
336 /* Write character. */
337 void
338 screen_write_putc(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
339 u_char ch)
341 struct grid_cell gc;
343 memcpy(&gc, gcp, sizeof gc);
345 utf8_set(&gc.data, ch);
346 screen_write_cell(ctx, &gc);
349 /* Calculate string length. */
350 size_t
351 screen_write_strlen(const char *fmt, ...)
353 va_list ap;
354 char *msg;
355 struct utf8_data ud;
356 u_char *ptr;
357 size_t left, size = 0;
358 enum utf8_state more;
360 va_start(ap, fmt);
361 xvasprintf(&msg, fmt, ap);
362 va_end(ap);
364 ptr = msg;
365 while (*ptr != '\0') {
366 if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) {
367 ptr++;
369 left = strlen(ptr);
370 if (left < (size_t)ud.size - 1)
371 break;
372 while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE)
373 ptr++;
374 ptr++;
376 if (more == UTF8_DONE)
377 size += ud.width;
378 } else {
379 if (*ptr > 0x1f && *ptr < 0x7f)
380 size++;
381 ptr++;
385 free(msg);
386 return (size);
389 /* Write string wrapped over lines. */
391 screen_write_text(struct screen_write_ctx *ctx, u_int cx, u_int width,
392 u_int lines, int more, const struct grid_cell *gcp, const char *fmt, ...)
394 struct screen *s = ctx->s;
395 va_list ap;
396 char *tmp;
397 u_int cy = s->cy, i, end, next, idx = 0, at, left;
398 struct utf8_data *text;
399 struct grid_cell gc;
401 memcpy(&gc, gcp, sizeof gc);
403 va_start(ap, fmt);
404 xvasprintf(&tmp, fmt, ap);
405 va_end(ap);
407 text = utf8_fromcstr(tmp);
408 free(tmp);
410 left = (cx + width) - s->cx;
411 for (;;) {
412 /* Find the end of what can fit on the line. */
413 at = 0;
414 for (end = idx; text[end].size != 0; end++) {
415 if (text[end].size == 1 && text[end].data[0] == '\n')
416 break;
417 if (at + text[end].width > left)
418 break;
419 at += text[end].width;
423 * If we're on a space, that's the end. If not, walk back to
424 * try and find one.
426 if (text[end].size == 0)
427 next = end;
428 else if (text[end].size == 1 && text[end].data[0] == '\n')
429 next = end + 1;
430 else if (text[end].size == 1 && text[end].data[0] == ' ')
431 next = end + 1;
432 else {
433 for (i = end; i > idx; i--) {
434 if (text[i].size == 1 && text[i].data[0] == ' ')
435 break;
437 if (i != idx) {
438 next = i + 1;
439 end = i;
440 } else
441 next = end;
444 /* Print the line. */
445 for (i = idx; i < end; i++) {
446 utf8_copy(&gc.data, &text[i]);
447 screen_write_cell(ctx, &gc);
450 /* If at the bottom, stop. */
451 idx = next;
452 if (s->cy == cy + lines - 1 || text[idx].size == 0)
453 break;
455 screen_write_cursormove(ctx, cx, s->cy + 1, 0);
456 left = width;
460 * Fail if on the last line and there is more to come or at the end, or
461 * if the text was not entirely consumed.
463 if ((s->cy == cy + lines - 1 && (!more || s->cx == cx + width)) ||
464 text[idx].size != 0) {
465 free(text);
466 return (0);
468 free(text);
471 * If no more to come, move to the next line. Otherwise, leave on
472 * the same line (except if at the end).
474 if (!more || s->cx == cx + width)
475 screen_write_cursormove(ctx, cx, s->cy + 1, 0);
476 return (1);
479 /* Write simple string (no maximum length). */
480 void
481 screen_write_puts(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
482 const char *fmt, ...)
484 va_list ap;
486 va_start(ap, fmt);
487 screen_write_vnputs(ctx, -1, gcp, fmt, ap);
488 va_end(ap);
491 /* Write string with length limit (-1 for unlimited). */
492 void
493 screen_write_nputs(struct screen_write_ctx *ctx, ssize_t maxlen,
494 const struct grid_cell *gcp, const char *fmt, ...)
496 va_list ap;
498 va_start(ap, fmt);
499 screen_write_vnputs(ctx, maxlen, gcp, fmt, ap);
500 va_end(ap);
503 void
504 screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
505 const struct grid_cell *gcp, const char *fmt, va_list ap)
507 struct grid_cell gc;
508 struct utf8_data *ud = &gc.data;
509 char *msg;
510 u_char *ptr;
511 size_t left, size = 0;
512 enum utf8_state more;
514 memcpy(&gc, gcp, sizeof gc);
515 xvasprintf(&msg, fmt, ap);
517 ptr = msg;
518 while (*ptr != '\0') {
519 if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
520 ptr++;
522 left = strlen(ptr);
523 if (left < (size_t)ud->size - 1)
524 break;
525 while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
526 ptr++;
527 ptr++;
529 if (more != UTF8_DONE)
530 continue;
531 if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
532 while (size < (size_t)maxlen) {
533 screen_write_putc(ctx, &gc, ' ');
534 size++;
536 break;
538 size += ud->width;
539 screen_write_cell(ctx, &gc);
540 } else {
541 if (maxlen > 0 && size + 1 > (size_t)maxlen)
542 break;
544 if (*ptr == '\001')
545 gc.attr ^= GRID_ATTR_CHARSET;
546 else if (*ptr == '\n') {
547 screen_write_linefeed(ctx, 0, 8);
548 screen_write_carriagereturn(ctx);
549 } else if (*ptr > 0x1f && *ptr < 0x7f) {
550 size++;
551 screen_write_putc(ctx, &gc, *ptr);
553 ptr++;
557 free(msg);
561 * Copy from another screen but without the selection stuff. Assumes the target
562 * region is already big enough.
564 void
565 screen_write_fast_copy(struct screen_write_ctx *ctx, struct screen *src,
566 u_int px, u_int py, u_int nx, u_int ny)
568 struct screen *s = ctx->s;
569 struct grid *gd = src->grid;
570 struct grid_cell gc;
571 u_int xx, yy, cx, cy;
573 if (nx == 0 || ny == 0)
574 return;
576 cy = s->cy;
577 for (yy = py; yy < py + ny; yy++) {
578 if (yy >= gd->hsize + gd->sy)
579 break;
580 cx = s->cx;
581 for (xx = px; xx < px + nx; xx++) {
582 if (xx >= grid_get_line(gd, yy)->cellsize)
583 break;
584 grid_get_cell(gd, xx, yy, &gc);
585 if (xx + gc.data.width > px + nx)
586 break;
587 grid_view_set_cell(ctx->s->grid, cx, cy, &gc);
588 cx++;
590 cy++;
594 /* Select character set for drawing border lines. */
595 static void
596 screen_write_box_border_set(enum box_lines lines, int cell_type,
597 struct grid_cell *gc)
599 switch (lines) {
600 case BOX_LINES_NONE:
601 break;
602 case BOX_LINES_DOUBLE:
603 gc->attr &= ~GRID_ATTR_CHARSET;
604 utf8_copy(&gc->data, tty_acs_double_borders(cell_type));
605 break;
606 case BOX_LINES_HEAVY:
607 gc->attr &= ~GRID_ATTR_CHARSET;
608 utf8_copy(&gc->data, tty_acs_heavy_borders(cell_type));
609 break;
610 case BOX_LINES_ROUNDED:
611 gc->attr &= ~GRID_ATTR_CHARSET;
612 utf8_copy(&gc->data, tty_acs_rounded_borders(cell_type));
613 break;
614 case BOX_LINES_SIMPLE:
615 gc->attr &= ~GRID_ATTR_CHARSET;
616 utf8_set(&gc->data, SIMPLE_BORDERS[cell_type]);
617 break;
618 case BOX_LINES_PADDED:
619 gc->attr &= ~GRID_ATTR_CHARSET;
620 utf8_set(&gc->data, PADDED_BORDERS[cell_type]);
621 break;
622 case BOX_LINES_SINGLE:
623 case BOX_LINES_DEFAULT:
624 gc->attr |= GRID_ATTR_CHARSET;
625 utf8_set(&gc->data, CELL_BORDERS[cell_type]);
626 break;
630 /* Draw a horizontal line on screen. */
631 void
632 screen_write_hline(struct screen_write_ctx *ctx, u_int nx, int left, int right,
633 enum box_lines lines, const struct grid_cell *border_gc)
635 struct screen *s = ctx->s;
636 struct grid_cell gc;
637 u_int cx, cy, i;
639 cx = s->cx;
640 cy = s->cy;
642 if (border_gc != NULL)
643 memcpy(&gc, border_gc, sizeof gc);
644 else
645 memcpy(&gc, &grid_default_cell, sizeof gc);
646 gc.attr |= GRID_ATTR_CHARSET;
648 if (left)
649 screen_write_box_border_set(lines, CELL_LEFTJOIN, &gc);
650 else
651 screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
652 screen_write_cell(ctx, &gc);
654 screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
655 for (i = 1; i < nx - 1; i++)
656 screen_write_cell(ctx, &gc);
658 if (right)
659 screen_write_box_border_set(lines, CELL_RIGHTJOIN, &gc);
660 else
661 screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
662 screen_write_cell(ctx, &gc);
664 screen_write_set_cursor(ctx, cx, cy);
667 /* Draw a vertical line on screen. */
668 void
669 screen_write_vline(struct screen_write_ctx *ctx, u_int ny, int top, int bottom)
671 struct screen *s = ctx->s;
672 struct grid_cell gc;
673 u_int cx, cy, i;
675 cx = s->cx;
676 cy = s->cy;
678 memcpy(&gc, &grid_default_cell, sizeof gc);
679 gc.attr |= GRID_ATTR_CHARSET;
681 screen_write_putc(ctx, &gc, top ? 'w' : 'x');
682 for (i = 1; i < ny - 1; i++) {
683 screen_write_set_cursor(ctx, cx, cy + i);
684 screen_write_putc(ctx, &gc, 'x');
686 screen_write_set_cursor(ctx, cx, cy + ny - 1);
687 screen_write_putc(ctx, &gc, bottom ? 'v' : 'x');
689 screen_write_set_cursor(ctx, cx, cy);
692 /* Draw a menu on screen. */
693 void
694 screen_write_menu(struct screen_write_ctx *ctx, struct menu *menu, int choice,
695 enum box_lines lines, const struct grid_cell *menu_gc,
696 const struct grid_cell *border_gc, const struct grid_cell *choice_gc)
698 struct screen *s = ctx->s;
699 struct grid_cell default_gc;
700 const struct grid_cell *gc = &default_gc;
701 u_int cx, cy, i, j, width = menu->width;
702 const char *name;
704 cx = s->cx;
705 cy = s->cy;
707 memcpy(&default_gc, menu_gc, sizeof default_gc);
709 screen_write_box(ctx, menu->width + 4, menu->count + 2, lines,
710 border_gc, menu->title);
712 for (i = 0; i < menu->count; i++) {
713 name = menu->items[i].name;
714 if (name == NULL) {
715 screen_write_cursormove(ctx, cx, cy + 1 + i, 0);
716 screen_write_hline(ctx, width + 4, 1, 1, lines, gc);
717 continue;
720 if (choice >= 0 && i == (u_int)choice && *name != '-')
721 gc = choice_gc;
723 screen_write_cursormove(ctx, cx + 2, cy + 1 + i, 0);
724 for (j = 0; j < width; j++)
725 screen_write_putc(ctx, gc, ' ');
727 screen_write_cursormove(ctx, cx + 2, cy + 1 + i, 0);
728 if (*name == '-') {
729 default_gc.attr |= GRID_ATTR_DIM;
730 format_draw(ctx, gc, width, name + 1, NULL, 0);
731 default_gc.attr &= ~GRID_ATTR_DIM;
732 continue;
735 format_draw(ctx, gc, width, name, NULL, gc == choice_gc);
736 gc = &default_gc;
739 screen_write_set_cursor(ctx, cx, cy);
742 /* Draw a box on screen. */
743 void
744 screen_write_box(struct screen_write_ctx *ctx, u_int nx, u_int ny,
745 enum box_lines lines, const struct grid_cell *gcp, const char *title)
747 struct screen *s = ctx->s;
748 struct grid_cell gc;
749 u_int cx, cy, i;
751 cx = s->cx;
752 cy = s->cy;
754 if (gcp != NULL)
755 memcpy(&gc, gcp, sizeof gc);
756 else
757 memcpy(&gc, &grid_default_cell, sizeof gc);
759 gc.attr |= GRID_ATTR_CHARSET;
760 gc.flags |= GRID_FLAG_NOPALETTE;
762 /* Draw top border */
763 screen_write_box_border_set(lines, CELL_TOPLEFT, &gc);
764 screen_write_cell(ctx, &gc);
765 screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
766 for (i = 1; i < nx - 1; i++)
767 screen_write_cell(ctx, &gc);
768 screen_write_box_border_set(lines, CELL_TOPRIGHT, &gc);
769 screen_write_cell(ctx, &gc);
771 /* Draw bottom border */
772 screen_write_set_cursor(ctx, cx, cy + ny - 1);
773 screen_write_box_border_set(lines, CELL_BOTTOMLEFT, &gc);
774 screen_write_cell(ctx, &gc);
775 screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
776 for (i = 1; i < nx - 1; i++)
777 screen_write_cell(ctx, &gc);
778 screen_write_box_border_set(lines, CELL_BOTTOMRIGHT, &gc);
779 screen_write_cell(ctx, &gc);
781 /* Draw sides */
782 screen_write_box_border_set(lines, CELL_TOPBOTTOM, &gc);
783 for (i = 1; i < ny - 1; i++) {
784 /* left side */
785 screen_write_set_cursor(ctx, cx, cy + i);
786 screen_write_cell(ctx, &gc);
787 /* right side */
788 screen_write_set_cursor(ctx, cx + nx - 1, cy + i);
789 screen_write_cell(ctx, &gc);
792 if (title != NULL) {
793 gc.attr &= ~GRID_ATTR_CHARSET;
794 screen_write_cursormove(ctx, cx + 2, cy, 0);
795 format_draw(ctx, &gc, nx - 4, title, NULL, 0);
798 screen_write_set_cursor(ctx, cx, cy);
802 * Write a preview version of a window. Assumes target area is big enough and
803 * already cleared.
805 void
806 screen_write_preview(struct screen_write_ctx *ctx, struct screen *src, u_int nx,
807 u_int ny)
809 struct screen *s = ctx->s;
810 struct grid_cell gc;
811 u_int cx, cy, px, py;
813 cx = s->cx;
814 cy = s->cy;
817 * If the cursor is on, pick the area around the cursor, otherwise use
818 * the top left.
820 if (src->mode & MODE_CURSOR) {
821 px = src->cx;
822 if (px < nx / 3)
823 px = 0;
824 else
825 px = px - nx / 3;
826 if (px + nx > screen_size_x(src)) {
827 if (nx > screen_size_x(src))
828 px = 0;
829 else
830 px = screen_size_x(src) - nx;
832 py = src->cy;
833 if (py < ny / 3)
834 py = 0;
835 else
836 py = py - ny / 3;
837 if (py + ny > screen_size_y(src)) {
838 if (ny > screen_size_y(src))
839 py = 0;
840 else
841 py = screen_size_y(src) - ny;
843 } else {
844 px = 0;
845 py = 0;
848 screen_write_fast_copy(ctx, src, px, src->grid->hsize + py, nx, ny);
850 if (src->mode & MODE_CURSOR) {
851 grid_view_get_cell(src->grid, src->cx, src->cy, &gc);
852 gc.attr |= GRID_ATTR_REVERSE;
853 screen_write_set_cursor(ctx, cx + (src->cx - px),
854 cy + (src->cy - py));
855 screen_write_cell(ctx, &gc);
859 /* Set a mode. */
860 void
861 screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
863 struct screen *s = ctx->s;
865 s->mode |= mode;
867 if (log_get_level() != 0)
868 log_debug("%s: %s", __func__, screen_mode_to_string(mode));
871 /* Clear a mode. */
872 void
873 screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
875 struct screen *s = ctx->s;
877 s->mode &= ~mode;
879 if (log_get_level() != 0)
880 log_debug("%s: %s", __func__, screen_mode_to_string(mode));
883 /* Cursor up by ny. */
884 void
885 screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
887 struct screen *s = ctx->s;
888 u_int cx = s->cx, cy = s->cy;
890 if (ny == 0)
891 ny = 1;
893 if (cy < s->rupper) {
894 /* Above region. */
895 if (ny > cy)
896 ny = cy;
897 } else {
898 /* Below region. */
899 if (ny > cy - s->rupper)
900 ny = cy - s->rupper;
902 if (cx == screen_size_x(s))
903 cx--;
905 cy -= ny;
907 screen_write_set_cursor(ctx, cx, cy);
910 /* Cursor down by ny. */
911 void
912 screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
914 struct screen *s = ctx->s;
915 u_int cx = s->cx, cy = s->cy;
917 if (ny == 0)
918 ny = 1;
920 if (cy > s->rlower) {
921 /* Below region. */
922 if (ny > screen_size_y(s) - 1 - cy)
923 ny = screen_size_y(s) - 1 - cy;
924 } else {
925 /* Above region. */
926 if (ny > s->rlower - cy)
927 ny = s->rlower - cy;
929 if (cx == screen_size_x(s))
930 cx--;
931 else if (ny == 0)
932 return;
934 cy += ny;
936 screen_write_set_cursor(ctx, cx, cy);
939 /* Cursor right by nx. */
940 void
941 screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
943 struct screen *s = ctx->s;
944 u_int cx = s->cx, cy = s->cy;
946 if (nx == 0)
947 nx = 1;
949 if (nx > screen_size_x(s) - 1 - cx)
950 nx = screen_size_x(s) - 1 - cx;
951 if (nx == 0)
952 return;
954 cx += nx;
956 screen_write_set_cursor(ctx, cx, cy);
959 /* Cursor left by nx. */
960 void
961 screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
963 struct screen *s = ctx->s;
964 u_int cx = s->cx, cy = s->cy;
966 if (nx == 0)
967 nx = 1;
969 if (nx > cx)
970 nx = cx;
971 if (nx == 0)
972 return;
974 cx -= nx;
976 screen_write_set_cursor(ctx, cx, cy);
979 /* Backspace; cursor left unless at start of wrapped line when can move up. */
980 void
981 screen_write_backspace(struct screen_write_ctx *ctx)
983 struct screen *s = ctx->s;
984 struct grid_line *gl;
985 u_int cx = s->cx, cy = s->cy;
987 if (cx == 0) {
988 if (cy == 0)
989 return;
990 gl = grid_get_line(s->grid, s->grid->hsize + cy - 1);
991 if (gl->flags & GRID_LINE_WRAPPED) {
992 cy--;
993 cx = screen_size_x(s) - 1;
995 } else
996 cx--;
998 screen_write_set_cursor(ctx, cx, cy);
1001 /* VT100 alignment test. */
1002 void
1003 screen_write_alignmenttest(struct screen_write_ctx *ctx)
1005 struct screen *s = ctx->s;
1006 struct tty_ctx ttyctx;
1007 struct grid_cell gc;
1008 u_int xx, yy;
1010 memcpy(&gc, &grid_default_cell, sizeof gc);
1011 utf8_set(&gc.data, 'E');
1013 for (yy = 0; yy < screen_size_y(s); yy++) {
1014 for (xx = 0; xx < screen_size_x(s); xx++)
1015 grid_view_set_cell(s->grid, xx, yy, &gc);
1018 screen_write_set_cursor(ctx, 0, 0);
1020 s->rupper = 0;
1021 s->rlower = screen_size_y(s) - 1;
1023 screen_write_initctx(ctx, &ttyctx, 1);
1025 screen_write_collect_clear(ctx, 0, screen_size_y(s) - 1);
1026 tty_write(tty_cmd_alignmenttest, &ttyctx);
1029 /* Insert nx characters. */
1030 void
1031 screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1033 struct screen *s = ctx->s;
1034 struct tty_ctx ttyctx;
1036 if (nx == 0)
1037 nx = 1;
1039 if (nx > screen_size_x(s) - s->cx)
1040 nx = screen_size_x(s) - s->cx;
1041 if (nx == 0)
1042 return;
1044 if (s->cx > screen_size_x(s) - 1)
1045 return;
1047 screen_write_initctx(ctx, &ttyctx, 0);
1048 ttyctx.bg = bg;
1050 grid_view_insert_cells(s->grid, s->cx, s->cy, nx, bg);
1052 screen_write_collect_flush(ctx, 0, __func__);
1053 ttyctx.num = nx;
1054 tty_write(tty_cmd_insertcharacter, &ttyctx);
1057 /* Delete nx characters. */
1058 void
1059 screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1061 struct screen *s = ctx->s;
1062 struct tty_ctx ttyctx;
1064 if (nx == 0)
1065 nx = 1;
1067 if (nx > screen_size_x(s) - s->cx)
1068 nx = screen_size_x(s) - s->cx;
1069 if (nx == 0)
1070 return;
1072 if (s->cx > screen_size_x(s) - 1)
1073 return;
1075 screen_write_initctx(ctx, &ttyctx, 0);
1076 ttyctx.bg = bg;
1078 grid_view_delete_cells(s->grid, s->cx, s->cy, nx, bg);
1080 screen_write_collect_flush(ctx, 0, __func__);
1081 ttyctx.num = nx;
1082 tty_write(tty_cmd_deletecharacter, &ttyctx);
1085 /* Clear nx characters. */
1086 void
1087 screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1089 struct screen *s = ctx->s;
1090 struct tty_ctx ttyctx;
1092 if (nx == 0)
1093 nx = 1;
1095 if (nx > screen_size_x(s) - s->cx)
1096 nx = screen_size_x(s) - s->cx;
1097 if (nx == 0)
1098 return;
1100 if (s->cx > screen_size_x(s) - 1)
1101 return;
1103 screen_write_initctx(ctx, &ttyctx, 0);
1104 ttyctx.bg = bg;
1106 grid_view_clear(s->grid, s->cx, s->cy, nx, 1, bg);
1108 screen_write_collect_flush(ctx, 0, __func__);
1109 ttyctx.num = nx;
1110 tty_write(tty_cmd_clearcharacter, &ttyctx);
1113 /* Insert ny lines. */
1114 void
1115 screen_write_insertline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1117 struct screen *s = ctx->s;
1118 struct grid *gd = s->grid;
1119 struct tty_ctx ttyctx;
1121 if (ny == 0)
1122 ny = 1;
1124 if (s->cy < s->rupper || s->cy > s->rlower) {
1125 if (ny > screen_size_y(s) - s->cy)
1126 ny = screen_size_y(s) - s->cy;
1127 if (ny == 0)
1128 return;
1130 screen_write_initctx(ctx, &ttyctx, 1);
1131 ttyctx.bg = bg;
1133 grid_view_insert_lines(gd, s->cy, ny, bg);
1135 screen_write_collect_flush(ctx, 0, __func__);
1136 ttyctx.num = ny;
1137 tty_write(tty_cmd_insertline, &ttyctx);
1138 return;
1141 if (ny > s->rlower + 1 - s->cy)
1142 ny = s->rlower + 1 - s->cy;
1143 if (ny == 0)
1144 return;
1146 screen_write_initctx(ctx, &ttyctx, 1);
1147 ttyctx.bg = bg;
1149 if (s->cy < s->rupper || s->cy > s->rlower)
1150 grid_view_insert_lines(gd, s->cy, ny, bg);
1151 else
1152 grid_view_insert_lines_region(gd, s->rlower, s->cy, ny, bg);
1154 screen_write_collect_flush(ctx, 0, __func__);
1156 ttyctx.num = ny;
1157 tty_write(tty_cmd_insertline, &ttyctx);
1160 /* Delete ny lines. */
1161 void
1162 screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1164 struct screen *s = ctx->s;
1165 struct grid *gd = s->grid;
1166 struct tty_ctx ttyctx;
1168 if (ny == 0)
1169 ny = 1;
1171 if (s->cy < s->rupper || s->cy > s->rlower) {
1172 if (ny > screen_size_y(s) - s->cy)
1173 ny = screen_size_y(s) - s->cy;
1174 if (ny == 0)
1175 return;
1177 screen_write_initctx(ctx, &ttyctx, 1);
1178 ttyctx.bg = bg;
1180 grid_view_delete_lines(gd, s->cy, ny, bg);
1182 screen_write_collect_flush(ctx, 0, __func__);
1183 ttyctx.num = ny;
1184 tty_write(tty_cmd_deleteline, &ttyctx);
1185 return;
1188 if (ny > s->rlower + 1 - s->cy)
1189 ny = s->rlower + 1 - s->cy;
1190 if (ny == 0)
1191 return;
1193 screen_write_initctx(ctx, &ttyctx, 1);
1194 ttyctx.bg = bg;
1196 if (s->cy < s->rupper || s->cy > s->rlower)
1197 grid_view_delete_lines(gd, s->cy, ny, bg);
1198 else
1199 grid_view_delete_lines_region(gd, s->rlower, s->cy, ny, bg);
1201 screen_write_collect_flush(ctx, 0, __func__);
1202 ttyctx.num = ny;
1203 tty_write(tty_cmd_deleteline, &ttyctx);
1206 /* Clear line at cursor. */
1207 void
1208 screen_write_clearline(struct screen_write_ctx *ctx, u_int bg)
1210 struct screen *s = ctx->s;
1211 struct grid_line *gl;
1212 u_int sx = screen_size_x(s);
1213 struct screen_write_citem *ci = ctx->item;
1215 gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1216 if (gl->cellsize == 0 && COLOUR_DEFAULT(bg))
1217 return;
1219 grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1221 screen_write_collect_clear(ctx, s->cy, 1);
1222 ci->x = 0;
1223 ci->used = sx;
1224 ci->type = CLEAR;
1225 ci->bg = bg;
1226 TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry);
1227 ctx->item = screen_write_get_citem();
1230 /* Clear to end of line from cursor. */
1231 void
1232 screen_write_clearendofline(struct screen_write_ctx *ctx, u_int bg)
1234 struct screen *s = ctx->s;
1235 struct grid_line *gl;
1236 u_int sx = screen_size_x(s);
1237 struct screen_write_citem *ci = ctx->item, *before;
1239 if (s->cx == 0) {
1240 screen_write_clearline(ctx, bg);
1241 return;
1244 gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1245 if (s->cx > sx - 1 || (s->cx >= gl->cellsize && COLOUR_DEFAULT(bg)))
1246 return;
1248 grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1, bg);
1250 before = screen_write_collect_trim(ctx, s->cy, s->cx, sx - s->cx, NULL);
1251 ci->x = s->cx;
1252 ci->used = sx - s->cx;
1253 ci->type = CLEAR;
1254 ci->bg = bg;
1255 if (before == NULL)
1256 TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry);
1257 else
1258 TAILQ_INSERT_BEFORE(before, ci, entry);
1259 ctx->item = screen_write_get_citem();
1262 /* Clear to start of line from cursor. */
1263 void
1264 screen_write_clearstartofline(struct screen_write_ctx *ctx, u_int bg)
1266 struct screen *s = ctx->s;
1267 u_int sx = screen_size_x(s);
1268 struct screen_write_citem *ci = ctx->item, *before;
1270 if (s->cx >= sx - 1) {
1271 screen_write_clearline(ctx, bg);
1272 return;
1275 if (s->cx > sx - 1)
1276 grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1277 else
1278 grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1280 before = screen_write_collect_trim(ctx, s->cy, 0, s->cx + 1, NULL);
1281 ci->x = 0;
1282 ci->used = s->cx + 1;
1283 ci->type = CLEAR;
1284 ci->bg = bg;
1285 if (before == NULL)
1286 TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry);
1287 else
1288 TAILQ_INSERT_BEFORE(before, ci, entry);
1289 ctx->item = screen_write_get_citem();
1292 /* Move cursor to px,py. */
1293 void
1294 screen_write_cursormove(struct screen_write_ctx *ctx, int px, int py,
1295 int origin)
1297 struct screen *s = ctx->s;
1299 if (origin && py != -1 && (s->mode & MODE_ORIGIN)) {
1300 if ((u_int)py > s->rlower - s->rupper)
1301 py = s->rlower;
1302 else
1303 py += s->rupper;
1306 if (px != -1 && (u_int)px > screen_size_x(s) - 1)
1307 px = screen_size_x(s) - 1;
1308 if (py != -1 && (u_int)py > screen_size_y(s) - 1)
1309 py = screen_size_y(s) - 1;
1311 log_debug("%s: from %u,%u to %u,%u", __func__, s->cx, s->cy, px, py);
1312 screen_write_set_cursor(ctx, px, py);
1315 /* Reverse index (up with scroll). */
1316 void
1317 screen_write_reverseindex(struct screen_write_ctx *ctx, u_int bg)
1319 struct screen *s = ctx->s;
1320 struct tty_ctx ttyctx;
1322 if (s->cy == s->rupper) {
1323 grid_view_scroll_region_down(s->grid, s->rupper, s->rlower, bg);
1324 screen_write_collect_flush(ctx, 0, __func__);
1326 screen_write_initctx(ctx, &ttyctx, 1);
1327 ttyctx.bg = bg;
1329 tty_write(tty_cmd_reverseindex, &ttyctx);
1330 } else if (s->cy > 0)
1331 screen_write_set_cursor(ctx, -1, s->cy - 1);
1335 /* Set scroll region. */
1336 void
1337 screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper,
1338 u_int rlower)
1340 struct screen *s = ctx->s;
1342 if (rupper > screen_size_y(s) - 1)
1343 rupper = screen_size_y(s) - 1;
1344 if (rlower > screen_size_y(s) - 1)
1345 rlower = screen_size_y(s) - 1;
1346 if (rupper >= rlower) /* cannot be one line */
1347 return;
1349 screen_write_collect_flush(ctx, 0, __func__);
1351 /* Cursor moves to top-left. */
1352 screen_write_set_cursor(ctx, 0, 0);
1354 s->rupper = rupper;
1355 s->rlower = rlower;
1358 /* Line feed. */
1359 void
1360 screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped, u_int bg)
1362 struct screen *s = ctx->s;
1363 struct grid *gd = s->grid;
1364 struct grid_line *gl;
1366 gl = grid_get_line(gd, gd->hsize + s->cy);
1367 if (wrapped)
1368 gl->flags |= GRID_LINE_WRAPPED;
1370 log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
1371 s->rupper, s->rlower);
1373 if (bg != ctx->bg) {
1374 screen_write_collect_flush(ctx, 1, __func__);
1375 ctx->bg = bg;
1378 if (s->cy == s->rlower) {
1379 grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1380 screen_write_collect_scroll(ctx, bg);
1381 ctx->scrolled++;
1382 } else if (s->cy < screen_size_y(s) - 1)
1383 screen_write_set_cursor(ctx, -1, s->cy + 1);
1386 /* Scroll up. */
1387 void
1388 screen_write_scrollup(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1390 struct screen *s = ctx->s;
1391 struct grid *gd = s->grid;
1392 u_int i;
1394 if (lines == 0)
1395 lines = 1;
1396 else if (lines > s->rlower - s->rupper + 1)
1397 lines = s->rlower - s->rupper + 1;
1399 if (bg != ctx->bg) {
1400 screen_write_collect_flush(ctx, 1, __func__);
1401 ctx->bg = bg;
1404 for (i = 0; i < lines; i++) {
1405 grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1406 screen_write_collect_scroll(ctx, bg);
1408 ctx->scrolled += lines;
1411 /* Scroll down. */
1412 void
1413 screen_write_scrolldown(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1415 struct screen *s = ctx->s;
1416 struct grid *gd = s->grid;
1417 struct tty_ctx ttyctx;
1418 u_int i;
1420 screen_write_initctx(ctx, &ttyctx, 1);
1421 ttyctx.bg = bg;
1423 if (lines == 0)
1424 lines = 1;
1425 else if (lines > s->rlower - s->rupper + 1)
1426 lines = s->rlower - s->rupper + 1;
1428 for (i = 0; i < lines; i++)
1429 grid_view_scroll_region_down(gd, s->rupper, s->rlower, bg);
1431 screen_write_collect_flush(ctx, 0, __func__);
1432 ttyctx.num = lines;
1433 tty_write(tty_cmd_scrolldown, &ttyctx);
1436 /* Carriage return (cursor to start of line). */
1437 void
1438 screen_write_carriagereturn(struct screen_write_ctx *ctx)
1440 screen_write_set_cursor(ctx, 0, -1);
1443 /* Clear to end of screen from cursor. */
1444 void
1445 screen_write_clearendofscreen(struct screen_write_ctx *ctx, u_int bg)
1447 struct screen *s = ctx->s;
1448 struct grid *gd = s->grid;
1449 struct tty_ctx ttyctx;
1450 u_int sx = screen_size_x(s), sy = screen_size_y(s);
1452 screen_write_initctx(ctx, &ttyctx, 1);
1453 ttyctx.bg = bg;
1455 /* Scroll into history if it is enabled and clearing entire screen. */
1456 if (s->cx == 0 &&
1457 s->cy == 0 &&
1458 (gd->flags & GRID_HISTORY) &&
1459 ctx->wp != NULL &&
1460 options_get_number(ctx->wp->options, "scroll-on-clear"))
1461 grid_view_clear_history(gd, bg);
1462 else {
1463 if (s->cx <= sx - 1)
1464 grid_view_clear(gd, s->cx, s->cy, sx - s->cx, 1, bg);
1465 grid_view_clear(gd, 0, s->cy + 1, sx, sy - (s->cy + 1), bg);
1468 screen_write_collect_clear(ctx, s->cy + 1, sy - (s->cy + 1));
1469 screen_write_collect_flush(ctx, 0, __func__);
1470 tty_write(tty_cmd_clearendofscreen, &ttyctx);
1473 /* Clear to start of screen. */
1474 void
1475 screen_write_clearstartofscreen(struct screen_write_ctx *ctx, u_int bg)
1477 struct screen *s = ctx->s;
1478 struct tty_ctx ttyctx;
1479 u_int sx = screen_size_x(s);
1481 screen_write_initctx(ctx, &ttyctx, 1);
1482 ttyctx.bg = bg;
1484 if (s->cy > 0)
1485 grid_view_clear(s->grid, 0, 0, sx, s->cy, bg);
1486 if (s->cx > sx - 1)
1487 grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1488 else
1489 grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1491 screen_write_collect_clear(ctx, 0, s->cy);
1492 screen_write_collect_flush(ctx, 0, __func__);
1493 tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1496 /* Clear entire screen. */
1497 void
1498 screen_write_clearscreen(struct screen_write_ctx *ctx, u_int bg)
1500 struct screen *s = ctx->s;
1501 struct tty_ctx ttyctx;
1502 u_int sx = screen_size_x(s), sy = screen_size_y(s);
1504 screen_write_initctx(ctx, &ttyctx, 1);
1505 ttyctx.bg = bg;
1507 /* Scroll into history if it is enabled. */
1508 if ((s->grid->flags & GRID_HISTORY) &&
1509 ctx->wp != NULL &&
1510 options_get_number(ctx->wp->options, "scroll-on-clear"))
1511 grid_view_clear_history(s->grid, bg);
1512 else
1513 grid_view_clear(s->grid, 0, 0, sx, sy, bg);
1515 screen_write_collect_clear(ctx, 0, sy);
1516 tty_write(tty_cmd_clearscreen, &ttyctx);
1519 /* Clear entire history. */
1520 void
1521 screen_write_clearhistory(struct screen_write_ctx *ctx)
1523 grid_clear_history(ctx->s->grid);
1526 /* Force a full redraw. */
1527 void
1528 screen_write_fullredraw(struct screen_write_ctx *ctx)
1530 struct tty_ctx ttyctx;
1532 screen_write_collect_flush(ctx, 0, __func__);
1534 screen_write_initctx(ctx, &ttyctx, 1);
1535 if (ttyctx.redraw_cb != NULL)
1536 ttyctx.redraw_cb(&ttyctx);
1539 /* Trim collected items. */
1540 static struct screen_write_citem *
1541 screen_write_collect_trim(struct screen_write_ctx *ctx, u_int y, u_int x,
1542 u_int used, int *wrapped)
1544 struct screen_write_cline *cl = &ctx->s->write_list[y];
1545 struct screen_write_citem *ci, *ci2, *tmp, *before = NULL;
1546 u_int sx = x, ex = x + used - 1;
1547 u_int csx, cex;
1549 if (TAILQ_EMPTY(&cl->items))
1550 return (NULL);
1551 TAILQ_FOREACH_SAFE(ci, &cl->items, entry, tmp) {
1552 csx = ci->x;
1553 cex = ci->x + ci->used - 1;
1555 /* Item is entirely before. */
1556 if (cex < sx) {
1557 log_debug("%s: %p %u-%u before %u-%u", __func__, ci,
1558 csx, cex, sx, ex);
1559 continue;
1562 /* Item is entirely after. */
1563 if (csx > ex) {
1564 log_debug("%s: %p %u-%u after %u-%u", __func__, ci,
1565 csx, cex, sx, ex);
1566 before = ci;
1567 break;
1570 /* Item is entirely inside. */
1571 if (csx >= sx && cex <= ex) {
1572 log_debug("%s: %p %u-%u inside %u-%u", __func__, ci,
1573 csx, cex, sx, ex);
1574 TAILQ_REMOVE(&cl->items, ci, entry);
1575 screen_write_free_citem(ci);
1576 if (csx == 0 && ci->wrapped && wrapped != NULL)
1577 *wrapped = 1;
1578 continue;
1581 /* Item under the start. */
1582 if (csx < sx && cex >= sx && cex <= ex) {
1583 log_debug("%s: %p %u-%u start %u-%u", __func__, ci,
1584 csx, cex, sx, ex);
1585 ci->used = sx - csx;
1586 log_debug("%s: %p now %u-%u", __func__, ci, ci->x,
1587 ci->x + ci->used + 1);
1588 continue;
1591 /* Item covers the end. */
1592 if (cex > ex && csx >= sx && csx <= ex) {
1593 log_debug("%s: %p %u-%u end %u-%u", __func__, ci,
1594 csx, cex, sx, ex);
1595 ci->x = ex + 1;
1596 ci->used = cex - ex;
1597 log_debug("%s: %p now %u-%u", __func__, ci, ci->x,
1598 ci->x + ci->used + 1);
1599 before = ci;
1600 break;
1603 /* Item must cover both sides. */
1604 log_debug("%s: %p %u-%u under %u-%u", __func__, ci,
1605 csx, cex, sx, ex);
1606 ci2 = screen_write_get_citem();
1607 ci2->type = ci->type;
1608 ci2->bg = ci->bg;
1609 memcpy(&ci2->gc, &ci->gc, sizeof ci2->gc);
1610 TAILQ_INSERT_AFTER(&cl->items, ci, ci2, entry);
1612 ci->used = sx - csx;
1613 ci2->x = ex + 1;
1614 ci2->used = cex - ex;
1616 log_debug("%s: %p now %u-%u (%p) and %u-%u (%p)", __func__, ci,
1617 ci->x, ci->x + ci->used - 1, ci, ci2->x,
1618 ci2->x + ci2->used - 1, ci2);
1619 before = ci2;
1620 break;
1622 return (before);
1625 /* Clear collected lines. */
1626 static void
1627 screen_write_collect_clear(struct screen_write_ctx *ctx, u_int y, u_int n)
1629 struct screen_write_cline *cl;
1630 u_int i;
1632 for (i = y; i < y + n; i++) {
1633 cl = &ctx->s->write_list[i];
1634 TAILQ_CONCAT(&screen_write_citem_freelist, &cl->items, entry);
1638 /* Scroll collected lines up. */
1639 static void
1640 screen_write_collect_scroll(struct screen_write_ctx *ctx, u_int bg)
1642 struct screen *s = ctx->s;
1643 struct screen_write_cline *cl;
1644 u_int y;
1645 char *saved;
1646 struct screen_write_citem *ci;
1648 log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
1649 s->rupper, s->rlower);
1651 screen_write_collect_clear(ctx, s->rupper, 1);
1652 saved = ctx->s->write_list[s->rupper].data;
1653 for (y = s->rupper; y < s->rlower; y++) {
1654 cl = &ctx->s->write_list[y + 1];
1655 TAILQ_CONCAT(&ctx->s->write_list[y].items, &cl->items, entry);
1656 ctx->s->write_list[y].data = cl->data;
1658 ctx->s->write_list[s->rlower].data = saved;
1660 ci = screen_write_get_citem();
1661 ci->x = 0;
1662 ci->used = screen_size_x(s);
1663 ci->type = CLEAR;
1664 ci->bg = bg;
1665 TAILQ_INSERT_TAIL(&ctx->s->write_list[s->rlower].items, ci, entry);
1668 /* Flush collected lines. */
1669 static void
1670 screen_write_collect_flush(struct screen_write_ctx *ctx, int scroll_only,
1671 const char *from)
1673 struct screen *s = ctx->s;
1674 struct screen_write_citem *ci, *tmp;
1675 struct screen_write_cline *cl;
1676 u_int y, cx, cy, last, items = 0;
1677 struct tty_ctx ttyctx;
1679 if (ctx->scrolled != 0) {
1680 log_debug("%s: scrolled %u (region %u-%u)", __func__,
1681 ctx->scrolled, s->rupper, s->rlower);
1682 if (ctx->scrolled > s->rlower - s->rupper + 1)
1683 ctx->scrolled = s->rlower - s->rupper + 1;
1685 screen_write_initctx(ctx, &ttyctx, 1);
1686 ttyctx.num = ctx->scrolled;
1687 ttyctx.bg = ctx->bg;
1688 tty_write(tty_cmd_scrollup, &ttyctx);
1690 ctx->scrolled = 0;
1691 ctx->bg = 8;
1693 if (scroll_only)
1694 return;
1696 cx = s->cx; cy = s->cy;
1697 for (y = 0; y < screen_size_y(s); y++) {
1698 cl = &ctx->s->write_list[y];
1699 last = UINT_MAX;
1700 TAILQ_FOREACH_SAFE(ci, &cl->items, entry, tmp) {
1701 if (last != UINT_MAX && ci->x <= last) {
1702 fatalx("collect list not in order: %u <= %u",
1703 ci->x, last);
1705 screen_write_set_cursor(ctx, ci->x, y);
1706 if (ci->type == CLEAR) {
1707 screen_write_initctx(ctx, &ttyctx, 1);
1708 ttyctx.bg = ci->bg;
1709 ttyctx.num = ci->used;
1710 tty_write(tty_cmd_clearcharacter, &ttyctx);
1711 } else {
1712 screen_write_initctx(ctx, &ttyctx, 0);
1713 ttyctx.cell = &ci->gc;
1714 ttyctx.wrapped = ci->wrapped;
1715 ttyctx.ptr = cl->data + ci->x;
1716 ttyctx.num = ci->used;
1717 tty_write(tty_cmd_cells, &ttyctx);
1719 items++;
1721 TAILQ_REMOVE(&cl->items, ci, entry);
1722 screen_write_free_citem(ci);
1723 last = ci->x;
1726 s->cx = cx; s->cy = cy;
1728 log_debug("%s: flushed %u items (%s)", __func__, items, from);
1731 /* Finish and store collected cells. */
1732 void
1733 screen_write_collect_end(struct screen_write_ctx *ctx)
1735 struct screen *s = ctx->s;
1736 struct screen_write_citem *ci = ctx->item, *before;
1737 struct screen_write_cline *cl = &s->write_list[s->cy];
1738 struct grid_cell gc;
1739 u_int xx;
1740 int wrapped = ci->wrapped;
1742 if (ci->used == 0)
1743 return;
1745 before = screen_write_collect_trim(ctx, s->cy, s->cx, ci->used,
1746 &wrapped);
1747 ci->x = s->cx;
1748 ci->wrapped = wrapped;
1749 if (before == NULL)
1750 TAILQ_INSERT_TAIL(&cl->items, ci, entry);
1751 else
1752 TAILQ_INSERT_BEFORE(before, ci, entry);
1753 ctx->item = screen_write_get_citem();
1755 log_debug("%s: %u %.*s (at %u,%u)", __func__, ci->used,
1756 (int)ci->used, cl->data + ci->x, s->cx, s->cy);
1758 if (s->cx != 0) {
1759 for (xx = s->cx; xx > 0; xx--) {
1760 grid_view_get_cell(s->grid, xx, s->cy, &gc);
1761 if (~gc.flags & GRID_FLAG_PADDING)
1762 break;
1763 grid_view_set_cell(s->grid, xx, s->cy,
1764 &grid_default_cell);
1766 if (gc.data.width > 1) {
1767 grid_view_set_cell(s->grid, xx, s->cy,
1768 &grid_default_cell);
1772 grid_view_set_cells(s->grid, s->cx, s->cy, &ci->gc, cl->data + ci->x,
1773 ci->used);
1774 screen_write_set_cursor(ctx, s->cx + ci->used, -1);
1776 for (xx = s->cx; xx < screen_size_x(s); xx++) {
1777 grid_view_get_cell(s->grid, xx, s->cy, &gc);
1778 if (~gc.flags & GRID_FLAG_PADDING)
1779 break;
1780 grid_view_set_cell(s->grid, xx, s->cy, &grid_default_cell);
1784 /* Write cell data, collecting if necessary. */
1785 void
1786 screen_write_collect_add(struct screen_write_ctx *ctx,
1787 const struct grid_cell *gc)
1789 struct screen *s = ctx->s;
1790 struct screen_write_citem *ci;
1791 u_int sx = screen_size_x(s);
1792 int collect;
1795 * Don't need to check that the attributes and whatnot are still the
1796 * same - input_parse will end the collection when anything that isn't
1797 * a plain character is encountered.
1800 collect = 1;
1801 if (gc->data.width != 1 || gc->data.size != 1 || *gc->data.data >= 0x7f)
1802 collect = 0;
1803 else if (gc->attr & GRID_ATTR_CHARSET)
1804 collect = 0;
1805 else if (~s->mode & MODE_WRAP)
1806 collect = 0;
1807 else if (s->mode & MODE_INSERT)
1808 collect = 0;
1809 else if (s->sel != NULL)
1810 collect = 0;
1811 if (!collect) {
1812 screen_write_collect_end(ctx);
1813 screen_write_collect_flush(ctx, 0, __func__);
1814 screen_write_cell(ctx, gc);
1815 return;
1818 if (s->cx > sx - 1 || ctx->item->used > sx - 1 - s->cx)
1819 screen_write_collect_end(ctx);
1820 ci = ctx->item; /* may have changed */
1822 if (s->cx > sx - 1) {
1823 log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1824 ci->wrapped = 1;
1825 screen_write_linefeed(ctx, 1, 8);
1826 screen_write_set_cursor(ctx, 0, -1);
1829 if (ci->used == 0)
1830 memcpy(&ci->gc, gc, sizeof ci->gc);
1831 if (ctx->s->write_list[s->cy].data == NULL)
1832 ctx->s->write_list[s->cy].data = xmalloc(screen_size_x(ctx->s));
1833 ctx->s->write_list[s->cy].data[s->cx + ci->used++] = gc->data.data[0];
1836 /* Write cell data. */
1837 void
1838 screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1840 struct screen *s = ctx->s;
1841 struct grid *gd = s->grid;
1842 const struct utf8_data *ud = &gc->data;
1843 const struct utf8_data zwj = { "\342\200\215", 0, 3, 0 };
1844 struct grid_line *gl;
1845 struct grid_cell_entry *gce;
1846 struct grid_cell tmp_gc, now_gc;
1847 struct tty_ctx ttyctx;
1848 u_int sx = screen_size_x(s), sy = screen_size_y(s);
1849 u_int width = gc->data.width, xx, last, cx, cy;
1850 int selected, skip = 1;
1852 /* Ignore padding cells. */
1853 if (gc->flags & GRID_FLAG_PADDING)
1854 return;
1857 * If this is a zero width joiner, set the flag so the next character
1858 * will be treated as zero width and appended. Note that we assume a
1859 * ZWJ will not change the width - the width of the first character is
1860 * used.
1862 if (ud->size == 3 && memcmp(ud->data, "\342\200\215", 3) == 0) {
1863 log_debug("zero width joiner at %u,%u", s->cx, s->cy);
1864 ctx->flags |= SCREEN_WRITE_ZWJ;
1865 return;
1869 * If the width is zero, combine onto the previous character. We always
1870 * combine with the cell to the left of the cursor position. In theory,
1871 * the application could have moved the cursor somewhere else, but if
1872 * they are silly enough to do that, who cares?
1874 if (ctx->flags & SCREEN_WRITE_ZWJ) {
1875 screen_write_collect_flush(ctx, 0, __func__);
1876 screen_write_combine(ctx, &zwj, &xx, &cx);
1878 if (width == 0 || (ctx->flags & SCREEN_WRITE_ZWJ)) {
1879 ctx->flags &= ~SCREEN_WRITE_ZWJ;
1880 screen_write_collect_flush(ctx, 0, __func__);
1881 if ((gc = screen_write_combine(ctx, ud, &xx, &cx)) != NULL) {
1882 cy = s->cy;
1883 screen_write_set_cursor(ctx, xx, s->cy);
1884 screen_write_initctx(ctx, &ttyctx, 0);
1885 ttyctx.cell = gc;
1886 tty_write(tty_cmd_cell, &ttyctx);
1887 s->cx = cx; s->cy = cy;
1889 return;
1892 /* Flush any existing scrolling. */
1893 screen_write_collect_flush(ctx, 1, __func__);
1895 /* If this character doesn't fit, ignore it. */
1896 if ((~s->mode & MODE_WRAP) &&
1897 width > 1 &&
1898 (width > sx || (s->cx != sx && s->cx > sx - width)))
1899 return;
1901 /* If in insert mode, make space for the cells. */
1902 if (s->mode & MODE_INSERT) {
1903 grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8);
1904 skip = 0;
1907 /* Check this will fit on the current line and wrap if not. */
1908 if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
1909 log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1910 screen_write_linefeed(ctx, 1, 8);
1911 screen_write_set_cursor(ctx, 0, -1);
1912 screen_write_collect_flush(ctx, 1, __func__);
1915 /* Sanity check cursor position. */
1916 if (s->cx > sx - width || s->cy > sy - 1)
1917 return;
1918 screen_write_initctx(ctx, &ttyctx, 0);
1920 /* Handle overwriting of UTF-8 characters. */
1921 gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1922 if (gl->flags & GRID_LINE_EXTENDED) {
1923 grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
1924 if (screen_write_overwrite(ctx, &now_gc, width))
1925 skip = 0;
1929 * If the new character is UTF-8 wide, fill in padding cells. Have
1930 * already ensured there is enough room.
1932 for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1933 log_debug("%s: new padding at %u,%u", __func__, xx, s->cy);
1934 grid_view_set_padding(gd, xx, s->cy);
1935 skip = 0;
1938 /* If no change, do not draw. */
1939 if (skip) {
1940 if (s->cx >= gl->cellsize)
1941 skip = grid_cells_equal(gc, &grid_default_cell);
1942 else {
1943 gce = &gl->celldata[s->cx];
1944 if (gce->flags & GRID_FLAG_EXTENDED)
1945 skip = 0;
1946 else if (gc->flags != gce->flags)
1947 skip = 0;
1948 else if (gc->attr != gce->data.attr)
1949 skip = 0;
1950 else if (gc->fg != gce->data.fg)
1951 skip = 0;
1952 else if (gc->bg != gce->data.bg)
1953 skip = 0;
1954 else if (gc->data.width != 1)
1955 skip = 0;
1956 else if (gc->data.size != 1)
1957 skip = 0;
1958 else if (gce->data.data != gc->data.data[0])
1959 skip = 0;
1963 /* Update the selected flag and set the cell. */
1964 selected = screen_check_selection(s, s->cx, s->cy);
1965 if (selected && (~gc->flags & GRID_FLAG_SELECTED)) {
1966 memcpy(&tmp_gc, gc, sizeof tmp_gc);
1967 tmp_gc.flags |= GRID_FLAG_SELECTED;
1968 grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1969 } else if (!selected && (gc->flags & GRID_FLAG_SELECTED)) {
1970 memcpy(&tmp_gc, gc, sizeof tmp_gc);
1971 tmp_gc.flags &= ~GRID_FLAG_SELECTED;
1972 grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1973 } else if (!skip)
1974 grid_view_set_cell(gd, s->cx, s->cy, gc);
1975 if (selected)
1976 skip = 0;
1979 * Move the cursor. If not wrapping, stick at the last character and
1980 * replace it.
1982 last = !(s->mode & MODE_WRAP);
1983 if (s->cx <= sx - last - width)
1984 screen_write_set_cursor(ctx, s->cx + width, -1);
1985 else
1986 screen_write_set_cursor(ctx, sx - last, -1);
1988 /* Create space for character in insert mode. */
1989 if (s->mode & MODE_INSERT) {
1990 screen_write_collect_flush(ctx, 0, __func__);
1991 ttyctx.num = width;
1992 tty_write(tty_cmd_insertcharacter, &ttyctx);
1995 /* Write to the screen. */
1996 if (!skip) {
1997 if (selected) {
1998 screen_select_cell(s, &tmp_gc, gc);
1999 ttyctx.cell = &tmp_gc;
2000 } else
2001 ttyctx.cell = gc;
2002 tty_write(tty_cmd_cell, &ttyctx);
2006 /* Combine a UTF-8 zero-width character onto the previous. */
2007 static const struct grid_cell *
2008 screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud,
2009 u_int *xx, u_int *cx)
2011 struct screen *s = ctx->s;
2012 struct grid *gd = s->grid;
2013 static struct grid_cell gc;
2014 u_int n, width;
2016 /* Can't combine if at 0. */
2017 if (s->cx == 0) {
2018 *xx = 0;
2019 return (NULL);
2021 *xx = s->cx;
2023 /* Empty data is out. */
2024 if (ud->size == 0)
2025 fatalx("UTF-8 data empty");
2027 /* Retrieve the previous cell. */
2028 for (n = 1; n <= s->cx; n++) {
2029 grid_view_get_cell(gd, s->cx - n, s->cy, &gc);
2030 if (~gc.flags & GRID_FLAG_PADDING)
2031 break;
2033 if (n > s->cx)
2034 return (NULL);
2036 /* Check there is enough space. */
2037 if (gc.data.size + ud->size > sizeof gc.data.data)
2038 return (NULL);
2039 (*xx) -= n;
2041 log_debug("%s: %.*s onto %.*s at %u,%u (width %u)", __func__,
2042 (int)ud->size, ud->data, (int)gc.data.size, gc.data.data, *xx,
2043 s->cy, gc.data.width);
2045 /* Append the data. */
2046 memcpy(gc.data.data + gc.data.size, ud->data, ud->size);
2047 gc.data.size += ud->size;
2048 width = gc.data.width;
2050 /* If this is U+FE0F VARIATION SELECTOR-16, force the width to 2. */
2051 if (gc.data.width == 1 &&
2052 ud->size == 3 &&
2053 memcmp(ud->data, "\357\270\217", 3) == 0) {
2054 grid_view_set_padding(gd, (*xx) + 1, s->cy);
2055 gc.data.width = 2;
2056 width += 2;
2059 /* Set the new cell. */
2060 grid_view_set_cell(gd, *xx, s->cy, &gc);
2062 *cx = (*xx) + width;
2063 log_debug("%s: character at %u; cursor at %u", __func__, *xx, *cx);
2064 return (&gc);
2068 * UTF-8 wide characters are a bit of an annoyance. They take up more than one
2069 * cell on the screen, so following cells must not be drawn by marking them as
2070 * padding.
2072 * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
2073 * character, it is necessary to also overwrite any other cells which covered
2074 * by the same character.
2076 static int
2077 screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
2078 u_int width)
2080 struct screen *s = ctx->s;
2081 struct grid *gd = s->grid;
2082 struct grid_cell tmp_gc;
2083 u_int xx;
2084 int done = 0;
2086 if (gc->flags & GRID_FLAG_PADDING) {
2088 * A padding cell, so clear any following and leading padding
2089 * cells back to the character. Don't overwrite the current
2090 * cell as that happens later anyway.
2092 xx = s->cx + 1;
2093 while (--xx > 0) {
2094 grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
2095 if (~tmp_gc.flags & GRID_FLAG_PADDING)
2096 break;
2097 log_debug("%s: padding at %u,%u", __func__, xx, s->cy);
2098 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
2101 /* Overwrite the character at the start of this padding. */
2102 log_debug("%s: character at %u,%u", __func__, xx, s->cy);
2103 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
2104 done = 1;
2108 * Overwrite any padding cells that belong to any UTF-8 characters
2109 * we'll be overwriting with the current character.
2111 if (width != 1 ||
2112 gc->data.width != 1 ||
2113 gc->flags & GRID_FLAG_PADDING) {
2114 xx = s->cx + width - 1;
2115 while (++xx < screen_size_x(s)) {
2116 grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
2117 if (~tmp_gc.flags & GRID_FLAG_PADDING)
2118 break;
2119 log_debug("%s: overwrite at %u,%u", __func__, xx,
2120 s->cy);
2121 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
2122 done = 1;
2126 return (done);
2129 /* Set external clipboard. */
2130 void
2131 screen_write_setselection(struct screen_write_ctx *ctx, const char *flags,
2132 u_char *str, u_int len)
2134 struct tty_ctx ttyctx;
2136 screen_write_initctx(ctx, &ttyctx, 0);
2137 ttyctx.ptr = str;
2138 ttyctx.ptr2 = (void *)flags;
2139 ttyctx.num = len;
2141 tty_write(tty_cmd_setselection, &ttyctx);
2144 /* Write unmodified string. */
2145 void
2146 screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len,
2147 int allow_invisible_panes)
2149 struct tty_ctx ttyctx;
2151 screen_write_initctx(ctx, &ttyctx, 0);
2152 ttyctx.ptr = str;
2153 ttyctx.num = len;
2154 ttyctx.allow_invisible_panes = allow_invisible_panes;
2156 tty_write(tty_cmd_rawstring, &ttyctx);
2159 /* Turn alternate screen on. */
2160 void
2161 screen_write_alternateon(struct screen_write_ctx *ctx, struct grid_cell *gc,
2162 int cursor)
2164 struct tty_ctx ttyctx;
2165 struct window_pane *wp = ctx->wp;
2167 if (wp != NULL && !options_get_number(wp->options, "alternate-screen"))
2168 return;
2170 screen_write_collect_flush(ctx, 0, __func__);
2171 screen_alternate_on(ctx->s, gc, cursor);
2173 screen_write_initctx(ctx, &ttyctx, 1);
2174 if (ttyctx.redraw_cb != NULL)
2175 ttyctx.redraw_cb(&ttyctx);
2178 /* Turn alternate screen off. */
2179 void
2180 screen_write_alternateoff(struct screen_write_ctx *ctx, struct grid_cell *gc,
2181 int cursor)
2183 struct tty_ctx ttyctx;
2184 struct window_pane *wp = ctx->wp;
2186 if (wp != NULL && !options_get_number(wp->options, "alternate-screen"))
2187 return;
2189 screen_write_collect_flush(ctx, 0, __func__);
2190 screen_alternate_off(ctx->s, gc, cursor);
2192 screen_write_initctx(ctx, &ttyctx, 1);
2193 if (ttyctx.redraw_cb != NULL)
2194 ttyctx.redraw_cb(&ttyctx);