xref: /openbsd-src/usr.bin/tmux/screen-redraw.c (revision 9f11ffb7133c203312a01e4b986886bc88c7d74b)
1 /* $OpenBSD: screen-redraw.c,v 1.55 2018/10/18 08:38:01 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 struct screen_redraw_ctx {
27 	struct client	*c;
28 
29 	u_int		 lines;
30 	int		 top;
31 
32 	int		 pane_status;
33 
34 	u_int		 sx;
35 	u_int		 sy;
36 	u_int		 ox;
37 	u_int		 oy;
38 };
39 
40 static void	screen_redraw_draw_borders(struct screen_redraw_ctx *);
41 static void	screen_redraw_draw_panes(struct screen_redraw_ctx *);
42 static void	screen_redraw_draw_status(struct screen_redraw_ctx *);
43 static void	screen_redraw_draw_pane(struct screen_redraw_ctx *,
44 		    struct window_pane *);
45 static void	screen_redraw_draw_number(struct screen_redraw_ctx *,
46 		    struct window_pane *);
47 
48 #define CELL_INSIDE 0
49 #define CELL_LEFTRIGHT 1
50 #define CELL_TOPBOTTOM 2
51 #define CELL_TOPLEFT 3
52 #define CELL_TOPRIGHT 4
53 #define CELL_BOTTOMLEFT 5
54 #define CELL_BOTTOMRIGHT 6
55 #define CELL_TOPJOIN 7
56 #define CELL_BOTTOMJOIN 8
57 #define CELL_LEFTJOIN 9
58 #define CELL_RIGHTJOIN 10
59 #define CELL_JOIN 11
60 #define CELL_OUTSIDE 12
61 
62 #define CELL_BORDERS " xqlkmjwvtun~"
63 
64 #define CELL_STATUS_OFF 0
65 #define CELL_STATUS_TOP 1
66 #define CELL_STATUS_BOTTOM 2
67 
68 /* Check if cell is on the border of a particular pane. */
69 static int
70 screen_redraw_cell_border1(struct window_pane *wp, u_int px, u_int py)
71 {
72 	/* Inside pane. */
73 	if (px >= wp->xoff && px < wp->xoff + wp->sx &&
74 	    py >= wp->yoff && py < wp->yoff + wp->sy)
75 		return (0);
76 
77 	/* Left/right borders. */
78 	if ((wp->yoff == 0 || py >= wp->yoff - 1) && py <= wp->yoff + wp->sy) {
79 		if (wp->xoff != 0 && px == wp->xoff - 1)
80 			return (1);
81 		if (px == wp->xoff + wp->sx)
82 			return (2);
83 	}
84 
85 	/* Top/bottom borders. */
86 	if ((wp->xoff == 0 || px >= wp->xoff - 1) && px <= wp->xoff + wp->sx) {
87 		if (wp->yoff != 0 && py == wp->yoff - 1)
88 			return (3);
89 		if (py == wp->yoff + wp->sy)
90 			return (4);
91 	}
92 
93 	/* Outside pane. */
94 	return (-1);
95 }
96 
97 /* Check if a cell is on the pane border. */
98 static int
99 screen_redraw_cell_border(struct client *c, u_int px, u_int py)
100 {
101 	struct window		*w = c->session->curw->window;
102 	struct window_pane	*wp;
103 	int			 retval;
104 
105 	/* Check all the panes. */
106 	TAILQ_FOREACH(wp, &w->panes, entry) {
107 		if (!window_pane_visible(wp))
108 			continue;
109 		if ((retval = screen_redraw_cell_border1(wp, px, py)) != -1)
110 			return (!!retval);
111 	}
112 
113 	return (0);
114 }
115 
116 /* Check if cell inside a pane. */
117 static int
118 screen_redraw_check_cell(struct client *c, u_int px, u_int py, int pane_status,
119     struct window_pane **wpp)
120 {
121 	struct window		*w = c->session->curw->window;
122 	struct window_pane	*wp;
123 	int			 borders;
124 	u_int			 right, line;
125 
126 	*wpp = NULL;
127 
128 	if (px > w->sx || py > w->sy)
129 		return (CELL_OUTSIDE);
130 
131 	if (pane_status != CELL_STATUS_OFF) {
132 		TAILQ_FOREACH(wp, &w->panes, entry) {
133 			if (!window_pane_visible(wp))
134 				continue;
135 
136 			if (pane_status == CELL_STATUS_TOP)
137 				line = wp->yoff - 1;
138 			else
139 				line = wp->yoff + wp->sy;
140 			right = wp->xoff + 2 + wp->status_size - 1;
141 
142 			if (py == line && px >= wp->xoff + 2 && px <= right)
143 				return (CELL_INSIDE);
144 		}
145 	}
146 
147 	TAILQ_FOREACH(wp, &w->panes, entry) {
148 		if (!window_pane_visible(wp))
149 			continue;
150 		*wpp = wp;
151 
152 		/* If outside the pane and its border, skip it. */
153 		if ((wp->xoff != 0 && px < wp->xoff - 1) ||
154 		    px > wp->xoff + wp->sx ||
155 		    (wp->yoff != 0 && py < wp->yoff - 1) ||
156 		    py > wp->yoff + wp->sy)
157 			continue;
158 
159 		/* If definitely inside, return so. */
160 		if (!screen_redraw_cell_border(c, px, py))
161 			return (CELL_INSIDE);
162 
163 		/*
164 		 * Construct a bitmask of whether the cells to the left (bit
165 		 * 4), right, top, and bottom (bit 1) of this cell are borders.
166 		 */
167 		borders = 0;
168 		if (px == 0 || screen_redraw_cell_border(c, px - 1, py))
169 			borders |= 8;
170 		if (px <= w->sx && screen_redraw_cell_border(c, px + 1, py))
171 			borders |= 4;
172 		if (pane_status == CELL_STATUS_TOP) {
173 			if (py != 0 && screen_redraw_cell_border(c, px, py - 1))
174 				borders |= 2;
175 		} else {
176 			if (py == 0 || screen_redraw_cell_border(c, px, py - 1))
177 				borders |= 2;
178 		}
179 		if (py <= w->sy && screen_redraw_cell_border(c, px, py + 1))
180 			borders |= 1;
181 
182 		/*
183 		 * Figure out what kind of border this cell is. Only one bit
184 		 * set doesn't make sense (can't have a border cell with no
185 		 * others connected).
186 		 */
187 		switch (borders) {
188 		case 15:	/* 1111, left right top bottom */
189 			return (CELL_JOIN);
190 		case 14:	/* 1110, left right top */
191 			return (CELL_BOTTOMJOIN);
192 		case 13:	/* 1101, left right bottom */
193 			return (CELL_TOPJOIN);
194 		case 12:	/* 1100, left right */
195 			return (CELL_TOPBOTTOM);
196 		case 11:	/* 1011, left top bottom */
197 			return (CELL_RIGHTJOIN);
198 		case 10:	/* 1010, left top */
199 			return (CELL_BOTTOMRIGHT);
200 		case 9:		/* 1001, left bottom */
201 			return (CELL_TOPRIGHT);
202 		case 7:		/* 0111, right top bottom */
203 			return (CELL_LEFTJOIN);
204 		case 6:		/* 0110, right top */
205 			return (CELL_BOTTOMLEFT);
206 		case 5:		/* 0101, right bottom */
207 			return (CELL_TOPLEFT);
208 		case 3:		/* 0011, top bottom */
209 			return (CELL_LEFTRIGHT);
210 		}
211 	}
212 
213 	return (CELL_OUTSIDE);
214 }
215 
216 /* Check if the border of a particular pane. */
217 static int
218 screen_redraw_check_is(u_int px, u_int py, int type, int pane_status,
219     struct window *w, struct window_pane *wantwp, struct window_pane *wp)
220 {
221 	int	border;
222 
223 	/* Is this off the active pane border? */
224 	border = screen_redraw_cell_border1(wantwp, px, py);
225 	if (border == 0 || border == -1)
226 		return (0);
227 	if (pane_status == CELL_STATUS_TOP && border == 4)
228 		return (0);
229 	if (pane_status == CELL_STATUS_BOTTOM && border == 3)
230 		return (0);
231 
232 	/* If there are more than two panes, that's enough. */
233 	if (window_count_panes(w) != 2)
234 		return (1);
235 
236 	/* Else if the cell is not a border cell, forget it. */
237 	if (wp == NULL || (type == CELL_OUTSIDE || type == CELL_INSIDE))
238 		return (1);
239 
240 	/* With status lines mark the entire line. */
241 	if (pane_status != CELL_STATUS_OFF)
242 		return (1);
243 
244 	/* Check if the pane covers the whole width. */
245 	if (wp->xoff == 0 && wp->sx == w->sx) {
246 		/* This can either be the top pane or the bottom pane. */
247 		if (wp->yoff == 0) { /* top pane */
248 			if (wp == wantwp)
249 				return (px <= wp->sx / 2);
250 			return (px > wp->sx / 2);
251 		}
252 		return (0);
253 	}
254 
255 	/* Check if the pane covers the whole height. */
256 	if (wp->yoff == 0 && wp->sy == w->sy) {
257 		/* This can either be the left pane or the right pane. */
258 		if (wp->xoff == 0) { /* left pane */
259 			if (wp == wantwp)
260 				return (py <= wp->sy / 2);
261 			return (py > wp->sy / 2);
262 		}
263 		return (0);
264 	}
265 
266 	return (1);
267 }
268 
269 /* Update pane status. */
270 static int
271 screen_redraw_make_pane_status(struct client *c, struct window *w,
272     struct window_pane *wp)
273 {
274 	struct grid_cell	 gc;
275 	const char		*fmt;
276 	struct format_tree	*ft;
277 	char			*out;
278 	size_t			 outlen;
279 	struct screen_write_ctx	 ctx;
280 	struct screen		 old;
281 
282 	if (wp == w->active)
283 		style_apply(&gc, w->options, "pane-active-border-style");
284 	else
285 		style_apply(&gc, w->options, "pane-border-style");
286 
287 	fmt = options_get_string(w->options, "pane-border-format");
288 
289 	ft = format_create(c, NULL, FORMAT_PANE|wp->id, 0);
290 	format_defaults(ft, c, NULL, NULL, wp);
291 
292 	memcpy(&old, &wp->status_screen, sizeof old);
293 	screen_init(&wp->status_screen, wp->sx, 1, 0);
294 	wp->status_screen.mode = 0;
295 
296 	out = format_expand(ft, fmt);
297 	outlen = screen_write_cstrlen("%s", out);
298 	if (outlen > wp->sx - 4)
299 		outlen = wp->sx - 4;
300 	screen_resize(&wp->status_screen, outlen, 1, 0);
301 
302 	screen_write_start(&ctx, NULL, &wp->status_screen);
303 	screen_write_cursormove(&ctx, 0, 0);
304 	screen_write_clearline(&ctx, 8);
305 	screen_write_cnputs(&ctx, outlen, &gc, "%s", out);
306 	screen_write_stop(&ctx);
307 
308 	free(out);
309 	format_free(ft);
310 
311 	wp->status_size = outlen;
312 
313 	if (grid_compare(wp->status_screen.grid, old.grid) == 0) {
314 		screen_free(&old);
315 		return (0);
316 	}
317 	screen_free(&old);
318 	return (1);
319 }
320 
321 /* Draw pane status. */
322 static void
323 screen_redraw_draw_pane_status(struct screen_redraw_ctx *ctx)
324 {
325 	struct client		*c = ctx->c;
326 	struct window		*w = c->session->curw->window;
327 	struct tty		*tty = &c->tty;
328 	struct window_pane	*wp;
329 	struct screen		*s;
330 	u_int			 i, x, width, xoff, yoff, size;
331 
332 	log_debug("%s: %s @%u", __func__, c->name, w->id);
333 
334 	TAILQ_FOREACH(wp, &w->panes, entry) {
335 		if (!window_pane_visible(wp))
336 			continue;
337 		s = &wp->status_screen;
338 
339 		size = wp->status_size;
340 		if (ctx->pane_status == CELL_STATUS_TOP)
341 			yoff = wp->yoff - 1;
342 		else
343 			yoff = wp->yoff + wp->sy;
344 		xoff = wp->xoff + 2;
345 
346 		if (xoff + size <= ctx->ox ||
347 		    xoff >= ctx->ox + ctx->sx ||
348 		    yoff < ctx->oy ||
349 		    yoff >= ctx->oy + ctx->sy)
350 			continue;
351 
352 		if (xoff >= ctx->ox && xoff + size <= ctx->ox + ctx->sx) {
353 			/* All visible. */
354 			i = 0;
355 			x = xoff - ctx->ox;
356 			width = size;
357 		} else if (xoff < ctx->ox && xoff + size > ctx->ox + ctx->sx) {
358 			/* Both left and right not visible. */
359 			i = ctx->ox;
360 			x = 0;
361 			width = ctx->sx;
362 		} else if (xoff < ctx->ox) {
363 			/* Left not visible. */
364 			i = ctx->ox - xoff;
365 			x = 0;
366 			width = size - i;
367 		} else {
368 			/* Right not visible. */
369 			i = 0;
370 			x = xoff - ctx->ox;
371 			width = size - x;
372 		}
373 
374 		if (ctx->top)
375 			yoff += ctx->lines;
376 		tty_draw_line(tty, NULL, s, i, 0, width, x, yoff - ctx->oy);
377 	}
378 	tty_cursor(tty, 0, 0);
379 }
380 
381 /* Update status line and change flags if unchanged. */
382 static int
383 screen_redraw_update(struct client *c, int flags)
384 {
385 	struct window		*w = c->session->curw->window;
386 	struct window_pane	*wp;
387 	struct options		*wo = w->options;
388 	int			 redraw;
389 
390 	if (c->message_string != NULL)
391 		redraw = status_message_redraw(c);
392 	else if (c->prompt_string != NULL)
393 		redraw = status_prompt_redraw(c);
394 	else
395 		redraw = status_redraw(c);
396 	if (!redraw && (~flags & CLIENT_REDRAWSTATUSALWAYS))
397 		flags &= ~CLIENT_REDRAWSTATUS;
398 
399 	if (options_get_number(wo, "pane-border-status") != CELL_STATUS_OFF) {
400 		redraw = 0;
401 		TAILQ_FOREACH(wp, &w->panes, entry) {
402 			if (screen_redraw_make_pane_status(c, w, wp))
403 				redraw = 1;
404 		}
405 		if (redraw)
406 			flags |= CLIENT_REDRAWBORDERS;
407 	}
408 	return (flags);
409 }
410 
411 /* Set up redraw context. */
412 static void
413 screen_redraw_set_context(struct client *c, struct screen_redraw_ctx *ctx)
414 {
415 	struct session	*s = c->session;
416 	struct options	*oo = s->options;
417 	struct window	*w = s->curw->window;
418 	struct options	*wo = w->options;
419 
420 	memset(ctx, 0, sizeof *ctx);
421 	ctx->c = c;
422 
423 	ctx->lines = status_line_size(c);
424 	if (c->message_string != NULL || c->prompt_string != NULL)
425 		ctx->lines = (ctx->lines == 0) ? 1 : ctx->lines;
426 	if (ctx->lines != 0 && options_get_number(oo, "status-position") == 0)
427 		ctx->top = 1;
428 	ctx->pane_status = options_get_number(wo, "pane-border-status");
429 
430 	tty_window_offset(&c->tty, &ctx->ox, &ctx->oy, &ctx->sx, &ctx->sy);
431 
432 	log_debug("%s: %s @%u ox=%u oy=%u sx=%u sy=%u %u/%d", __func__, c->name,
433 	    w->id, ctx->ox, ctx->oy, ctx->sx, ctx->sy, ctx->lines, ctx->top);
434 }
435 
436 /* Redraw entire screen. */
437 void
438 screen_redraw_screen(struct client *c)
439 {
440 	struct screen_redraw_ctx	ctx;
441 	int				flags;
442 
443 	if (c->flags & CLIENT_SUSPENDED)
444 		return;
445 
446 	flags = screen_redraw_update(c, c->flags);
447 	screen_redraw_set_context(c, &ctx);
448 
449 	if (flags & (CLIENT_REDRAWWINDOW|CLIENT_REDRAWBORDERS)) {
450 		if (ctx.pane_status != CELL_STATUS_OFF)
451 			screen_redraw_draw_pane_status(&ctx);
452 		screen_redraw_draw_borders(&ctx);
453 	}
454 	if (flags & CLIENT_REDRAWWINDOW)
455 		screen_redraw_draw_panes(&ctx);
456 	if (ctx.lines != 0 &&
457 	    (flags & (CLIENT_REDRAWSTATUS|CLIENT_REDRAWSTATUSALWAYS)))
458 		screen_redraw_draw_status(&ctx);
459 	tty_reset(&c->tty);
460 }
461 
462 /* Redraw a single pane. */
463 void
464 screen_redraw_pane(struct client *c, struct window_pane *wp)
465 {
466 	struct screen_redraw_ctx	 ctx;
467 
468 	if (!window_pane_visible(wp))
469 		return;
470 
471 	screen_redraw_set_context(c, &ctx);
472 
473 	screen_redraw_draw_pane(&ctx, wp);
474 	tty_reset(&c->tty);
475 }
476 
477 /* Draw a border cell. */
478 static void
479 screen_redraw_draw_borders_cell(struct screen_redraw_ctx *ctx, u_int i, u_int j,
480     struct grid_cell *m_active_gc, struct grid_cell *active_gc,
481     struct grid_cell *m_other_gc, struct grid_cell *other_gc)
482 {
483 	struct client		*c = ctx->c;
484 	struct session		*s = c->session;
485 	struct window		*w = s->curw->window;
486 	struct tty		*tty = &c->tty;
487 	struct window_pane	*wp;
488 	struct window_pane	*active = w->active;
489 	struct window_pane	*marked = marked_pane.wp;
490 	u_int			 type, x = ctx->ox + i, y = ctx->oy + j;
491 	int			 flag, pane_status = ctx->pane_status;
492 
493 	type = screen_redraw_check_cell(c, x, y, pane_status, &wp);
494 	if (type == CELL_INSIDE)
495 		return;
496 	flag = screen_redraw_check_is(x, y, type, pane_status, w, active, wp);
497 
498 	if (server_is_marked(s, s->curw, marked_pane.wp) &&
499 	    screen_redraw_check_is(x, y, type, pane_status, w, marked, wp)) {
500 		if (flag)
501 			tty_attributes(tty, m_active_gc, NULL);
502 		else
503 			tty_attributes(tty, m_other_gc, NULL);
504 	} else if (flag)
505 		tty_attributes(tty, active_gc, NULL);
506 	else
507 		tty_attributes(tty, other_gc, NULL);
508 	if (ctx->top)
509 		tty_cursor(tty, i, ctx->lines + j);
510 	else
511 		tty_cursor(tty, i, j);
512 	tty_putc(tty, CELL_BORDERS[type]);
513 }
514 
515 /* Draw the borders. */
516 static void
517 screen_redraw_draw_borders(struct screen_redraw_ctx *ctx)
518 {
519 	struct client		*c = ctx->c;
520 	struct session		*s = c->session;
521 	struct window		*w = s->curw->window;
522 	struct tty		*tty = &c->tty;
523 	struct options		*oo = w->options;
524 	struct grid_cell	 m_active_gc, active_gc, m_other_gc, other_gc;
525 	u_int		 	 i, j;
526 
527 	log_debug("%s: %s @%u", __func__, c->name, w->id);
528 
529 	style_apply(&other_gc, oo, "pane-border-style");
530 	style_apply(&active_gc, oo, "pane-active-border-style");
531 	active_gc.attr = other_gc.attr = GRID_ATTR_CHARSET;
532 
533 	memcpy(&m_other_gc, &other_gc, sizeof m_other_gc);
534 	m_other_gc.attr ^= GRID_ATTR_REVERSE;
535 	memcpy(&m_active_gc, &active_gc, sizeof m_active_gc);
536 	m_active_gc.attr ^= GRID_ATTR_REVERSE;
537 
538 	for (j = 0; j < tty->sy - ctx->lines; j++) {
539 		for (i = 0; i < tty->sx; i++) {
540 			screen_redraw_draw_borders_cell(ctx, i, j,
541 			    &m_active_gc, &active_gc, &m_other_gc, &other_gc);
542 		}
543 	}
544 }
545 
546 /* Draw the panes. */
547 static void
548 screen_redraw_draw_panes(struct screen_redraw_ctx *ctx)
549 {
550 	struct client		*c = ctx->c;
551 	struct window		*w = c->session->curw->window;
552 	struct window_pane	*wp;
553 
554 	log_debug("%s: %s @%u", __func__, c->name, w->id);
555 
556 	TAILQ_FOREACH(wp, &w->panes, entry) {
557 		if (!window_pane_visible(wp))
558 			continue;
559 		screen_redraw_draw_pane(ctx, wp);
560 		if (c->flags & CLIENT_IDENTIFY)
561 			screen_redraw_draw_number(ctx, wp);
562 	}
563 }
564 
565 /* Draw the status line. */
566 static void
567 screen_redraw_draw_status(struct screen_redraw_ctx *ctx)
568 {
569 	struct client	*c = ctx->c;
570 	struct window	*w = c->session->curw->window;
571 	struct tty	*tty = &c->tty;
572 	struct screen	*s = &c->status.status;
573 	u_int		 i, y;
574 
575 	log_debug("%s: %s @%u", __func__, c->name, w->id);
576 
577 	if (ctx->top)
578 		y = 0;
579 	else
580 		y = c->tty.sy - ctx->lines;
581 	for (i = 0; i < ctx->lines; i++)
582 		tty_draw_line(tty, NULL, s, 0, i, UINT_MAX, 0, y + i);
583 }
584 
585 /* Draw one pane. */
586 static void
587 screen_redraw_draw_pane(struct screen_redraw_ctx *ctx, struct window_pane *wp)
588 {
589 	struct client	*c = ctx->c;
590 	struct window	*w = c->session->curw->window;
591 	struct tty	*tty = &c->tty;
592 	struct screen	*s;
593 	u_int		 i, j, top, x, y, width;
594 
595 	log_debug("%s: %s @%u %%%u", __func__, c->name, w->id, wp->id);
596 
597 	if (wp->xoff + wp->sx <= ctx->ox || wp->xoff >= ctx->ox + ctx->sx)
598 		return;
599 	if (ctx->top)
600 		top = ctx->lines;
601 	else
602 		top = 0;
603 
604 	s = wp->screen;
605 	for (j = 0; j < wp->sy; j++) {
606 		if (wp->yoff + j < ctx->oy || wp->yoff + j >= ctx->oy + ctx->sy)
607 			continue;
608 		y = top + wp->yoff + j - ctx->oy;
609 
610 		if (wp->xoff >= ctx->ox &&
611 		    wp->xoff + wp->sx <= ctx->ox + ctx->sx) {
612 			/* All visible. */
613 			i = 0;
614 			x = wp->xoff - ctx->ox;
615 			width = wp->sx;
616 		} else if (wp->xoff < ctx->ox &&
617 		    wp->xoff + wp->sx > ctx->ox + ctx->sx) {
618 			/* Both left and right not visible. */
619 			i = ctx->ox;
620 			x = 0;
621 			width = ctx->sx;
622 		} else if (wp->xoff < ctx->ox) {
623 			/* Left not visible. */
624 			i = ctx->ox - wp->xoff;
625 			x = 0;
626 			width = wp->sx - i;
627 		} else {
628 			/* Right not visible. */
629 			i = 0;
630 			x = wp->xoff - ctx->ox;
631 			width = ctx->sx - x;
632 		}
633 		log_debug("%s: %s %%%u line %u,%u at %u,%u, width %u",
634 		    __func__, c->name, wp->id, i, j, x, y, width);
635 
636 		tty_draw_line(tty, wp, s, i, j, width, x, y);
637 	}
638 }
639 
640 /* Draw number on a pane. */
641 static void
642 screen_redraw_draw_number(struct screen_redraw_ctx *ctx, struct window_pane *wp)
643 {
644 	struct client		*c = ctx->c;
645 	struct tty		*tty = &c->tty;
646 	struct session		*s = c->session;
647 	struct options		*oo = s->options;
648 	struct window		*w = wp->window;
649 	struct grid_cell	 gc;
650 	u_int			 idx, px, py, i, j, xoff, yoff, sx, sy;
651 	int			 colour, active_colour;
652 	char			 buf[16], *ptr;
653 	size_t			 len;
654 
655 	if (wp->xoff + wp->sx <= ctx->ox ||
656 	    wp->xoff >= ctx->ox + ctx->sx ||
657 	    wp->yoff + wp->sy <= ctx->oy ||
658 	    wp->yoff >= ctx->oy + ctx->sy)
659 		return;
660 
661 	if (wp->xoff >= ctx->ox && wp->xoff + wp->sx <= ctx->ox + ctx->sx) {
662 		/* All visible. */
663 		xoff = wp->xoff - ctx->ox;
664 		sx = wp->sx;
665 	} else if (wp->xoff < ctx->ox &&
666 	    wp->xoff + wp->sx > ctx->ox + ctx->sx) {
667 		/* Both left and right not visible. */
668 		xoff = 0;
669 		sx = ctx->sx;
670 	} else if (wp->xoff < ctx->ox) {
671 		/* Left not visible. */
672 		xoff = 0;
673 		sx = wp->sx - (ctx->ox - wp->xoff);
674 	} else {
675 		/* Right not visible. */
676 		xoff = wp->xoff - ctx->ox;
677 		sx = wp->sx - xoff;
678 	}
679 	if (wp->yoff >= ctx->oy && wp->yoff + wp->sy <= ctx->oy + ctx->sy) {
680 		/* All visible. */
681 		yoff = wp->yoff - ctx->oy;
682 		sy = wp->sy;
683 	} else if (wp->yoff < ctx->oy &&
684 	    wp->yoff + wp->sy > ctx->oy + ctx->sy) {
685 		/* Both top and bottom not visible. */
686 		yoff = 0;
687 		sy = ctx->sy;
688 	} else if (wp->yoff < ctx->oy) {
689 		/* Top not visible. */
690 		yoff = 0;
691 		sy = wp->sy - (ctx->oy - wp->yoff);
692 	} else {
693 		/* Bottom not visible. */
694 		yoff = wp->yoff - ctx->oy;
695 		sy = wp->sy - yoff;
696 	}
697 
698 	if (ctx->top)
699 		yoff += ctx->lines;
700 	px = sx / 2;
701 	py = sy / 2;
702 
703 	if (window_pane_index(wp, &idx) != 0)
704 		fatalx("index not found");
705 	len = xsnprintf(buf, sizeof buf, "%u", idx);
706 
707 	if (sx < len)
708 		return;
709 	colour = options_get_number(oo, "display-panes-colour");
710 	active_colour = options_get_number(oo, "display-panes-active-colour");
711 
712 	if (sx < len * 6 || sy < 5) {
713 		tty_cursor(tty, xoff + px - len / 2, yoff + py);
714 		goto draw_text;
715 	}
716 
717 	px -= len * 3;
718 	py -= 2;
719 
720 	memcpy(&gc, &grid_default_cell, sizeof gc);
721 	if (w->active == wp)
722 		gc.bg = active_colour;
723 	else
724 		gc.bg = colour;
725 	gc.flags |= GRID_FLAG_NOPALETTE;
726 
727 	tty_attributes(tty, &gc, wp);
728 	for (ptr = buf; *ptr != '\0'; ptr++) {
729 		if (*ptr < '0' || *ptr > '9')
730 			continue;
731 		idx = *ptr - '0';
732 
733 		for (j = 0; j < 5; j++) {
734 			for (i = px; i < px + 5; i++) {
735 				tty_cursor(tty, xoff + i, yoff + py + j);
736 				if (window_clock_table[idx][j][i - px])
737 					tty_putc(tty, ' ');
738 			}
739 		}
740 		px += 6;
741 	}
742 
743 	len = xsnprintf(buf, sizeof buf, "%ux%u", wp->sx, wp->sy);
744 	if (sx < len || sy < 6)
745 		return;
746 	tty_cursor(tty, xoff + sx - len, yoff);
747 
748 draw_text:
749 	memcpy(&gc, &grid_default_cell, sizeof gc);
750 	if (w->active == wp)
751 		gc.fg = active_colour;
752 	else
753 		gc.fg = colour;
754 	gc.flags |= GRID_FLAG_NOPALETTE;
755 
756 	tty_attributes(tty, &gc, wp);
757 	tty_puts(tty, buf);
758 
759 	tty_cursor(tty, 0, 0);
760 }
761