xref: /openbsd-src/usr.bin/tmux/window.c (revision d59bb9942320b767f2a19aaa7690c8c6e30b724c)
1 /* $OpenBSD: window.c,v 1.185 2017/02/27 13:07: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 #include <sys/ioctl.h>
21 
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <fnmatch.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <termios.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include <util.h>
32 
33 #include "tmux.h"
34 
35 /*
36  * Each window is attached to a number of panes, each of which is a pty. This
37  * file contains code to handle them.
38  *
39  * A pane has two buffers attached, these are filled and emptied by the main
40  * server poll loop. Output data is received from pty's in screen format,
41  * translated and returned as a series of escape sequences and strings via
42  * input_parse (in input.c). Input data is received as key codes and written
43  * directly via input_key.
44  *
45  * Each pane also has a "virtual" screen (screen.c) which contains the current
46  * state and is redisplayed when the window is reattached to a client.
47  *
48  * Windows are stored directly on a global array and wrapped in any number of
49  * winlink structs to be linked onto local session RB trees. A reference count
50  * is maintained and a window removed from the global list and destroyed when
51  * it reaches zero.
52  */
53 
54 /* Global window list. */
55 struct windows windows;
56 
57 /* Global panes tree. */
58 struct window_pane_tree all_window_panes;
59 static u_int	next_window_pane_id;
60 static u_int	next_window_id;
61 static u_int	next_active_point;
62 
63 static void	window_destroy(struct window *);
64 
65 static struct window_pane *window_pane_create(struct window *, u_int, u_int,
66 		    u_int);
67 static void	window_pane_destroy(struct window_pane *);
68 
69 static void	window_pane_read_callback(struct bufferevent *, void *);
70 static void	window_pane_error_callback(struct bufferevent *, short, void *);
71 
72 static int	winlink_next_index(struct winlinks *, int);
73 
74 static struct window_pane *window_pane_choose_best(struct window_pane **,
75 		    u_int);
76 
77 RB_GENERATE(windows, window, entry, window_cmp);
78 RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
79 RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
80 
81 int
82 window_cmp(struct window *w1, struct window *w2)
83 {
84 	return (w1->id - w2->id);
85 }
86 
87 int
88 winlink_cmp(struct winlink *wl1, struct winlink *wl2)
89 {
90 	return (wl1->idx - wl2->idx);
91 }
92 
93 int
94 window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
95 {
96 	return (wp1->id - wp2->id);
97 }
98 
99 struct winlink *
100 winlink_find_by_window(struct winlinks *wwl, struct window *w)
101 {
102 	struct winlink	*wl;
103 
104 	RB_FOREACH(wl, winlinks, wwl) {
105 		if (wl->window == w)
106 			return (wl);
107 	}
108 
109 	return (NULL);
110 }
111 
112 struct winlink *
113 winlink_find_by_index(struct winlinks *wwl, int idx)
114 {
115 	struct winlink	wl;
116 
117 	if (idx < 0)
118 		fatalx("bad index");
119 
120 	wl.idx = idx;
121 	return (RB_FIND(winlinks, wwl, &wl));
122 }
123 
124 struct winlink *
125 winlink_find_by_window_id(struct winlinks *wwl, u_int id)
126 {
127 	struct winlink *wl;
128 
129 	RB_FOREACH(wl, winlinks, wwl) {
130 		if (wl->window->id == id)
131 			return (wl);
132 	}
133 	return (NULL);
134 }
135 
136 static int
137 winlink_next_index(struct winlinks *wwl, int idx)
138 {
139 	int	i;
140 
141 	i = idx;
142 	do {
143 		if (winlink_find_by_index(wwl, i) == NULL)
144 			return (i);
145 		if (i == INT_MAX)
146 			i = 0;
147 		else
148 			i++;
149 	} while (i != idx);
150 	return (-1);
151 }
152 
153 u_int
154 winlink_count(struct winlinks *wwl)
155 {
156 	struct winlink	*wl;
157 	u_int		 n;
158 
159 	n = 0;
160 	RB_FOREACH(wl, winlinks, wwl)
161 		n++;
162 
163 	return (n);
164 }
165 
166 struct winlink *
167 winlink_add(struct winlinks *wwl, int idx)
168 {
169 	struct winlink	*wl;
170 
171 	if (idx < 0) {
172 		if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
173 			return (NULL);
174 	} else if (winlink_find_by_index(wwl, idx) != NULL)
175 		return (NULL);
176 
177 	wl = xcalloc(1, sizeof *wl);
178 	wl->idx = idx;
179 	RB_INSERT(winlinks, wwl, wl);
180 
181 	return (wl);
182 }
183 
184 void
185 winlink_set_window(struct winlink *wl, struct window *w)
186 {
187 	if (wl->window != NULL) {
188 		TAILQ_REMOVE(&wl->window->winlinks, wl, wentry);
189 		window_remove_ref(w);
190 	}
191 	TAILQ_INSERT_TAIL(&w->winlinks, wl, wentry);
192 	wl->window = w;
193 	w->references++;
194 }
195 
196 void
197 winlink_remove(struct winlinks *wwl, struct winlink *wl)
198 {
199 	struct window	*w = wl->window;
200 
201 	if (w != NULL) {
202 		TAILQ_REMOVE(&w->winlinks, wl, wentry);
203 		window_remove_ref(w);
204 	}
205 
206 	RB_REMOVE(winlinks, wwl, wl);
207 	free(wl->status_text);
208 	free(wl);
209 }
210 
211 struct winlink *
212 winlink_next(struct winlink *wl)
213 {
214 	return (RB_NEXT(winlinks, wwl, wl));
215 }
216 
217 struct winlink *
218 winlink_previous(struct winlink *wl)
219 {
220 	return (RB_PREV(winlinks, wwl, wl));
221 }
222 
223 struct winlink *
224 winlink_next_by_number(struct winlink *wl, struct session *s, int n)
225 {
226 	for (; n > 0; n--) {
227 		if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
228 			wl = RB_MIN(winlinks, &s->windows);
229 	}
230 
231 	return (wl);
232 }
233 
234 struct winlink *
235 winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
236 {
237 	for (; n > 0; n--) {
238 		if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
239 			wl = RB_MAX(winlinks, &s->windows);
240 	}
241 
242 	return (wl);
243 }
244 
245 void
246 winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
247 {
248 	if (wl == NULL)
249 		return;
250 
251 	winlink_stack_remove(stack, wl);
252 	TAILQ_INSERT_HEAD(stack, wl, sentry);
253 }
254 
255 void
256 winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
257 {
258 	struct winlink	*wl2;
259 
260 	if (wl == NULL)
261 		return;
262 
263 	TAILQ_FOREACH(wl2, stack, sentry) {
264 		if (wl2 == wl) {
265 			TAILQ_REMOVE(stack, wl, sentry);
266 			return;
267 		}
268 	}
269 }
270 
271 struct window *
272 window_find_by_id_str(const char *s)
273 {
274 	const char	*errstr;
275 	u_int		 id;
276 
277 	if (*s != '@')
278 		return (NULL);
279 
280 	id = strtonum(s + 1, 0, UINT_MAX, &errstr);
281 	if (errstr != NULL)
282 		return (NULL);
283 	return (window_find_by_id(id));
284 }
285 
286 struct window *
287 window_find_by_id(u_int id)
288 {
289 	struct window	w;
290 
291 	w.id = id;
292 	return (RB_FIND(windows, &windows, &w));
293 }
294 
295 void
296 window_update_activity(struct window *w)
297 {
298 	gettimeofday(&w->activity_time, NULL);
299 	alerts_queue(w, WINDOW_ACTIVITY);
300 }
301 
302 struct window *
303 window_create(u_int sx, u_int sy)
304 {
305 	struct window	*w;
306 
307 	w = xcalloc(1, sizeof *w);
308 	w->name = NULL;
309 	w->flags = WINDOW_STYLECHANGED;
310 
311 	TAILQ_INIT(&w->panes);
312 	w->active = NULL;
313 
314 	w->lastlayout = -1;
315 	w->layout_root = NULL;
316 
317 	w->sx = sx;
318 	w->sy = sy;
319 
320 	w->options = options_create(global_w_options);
321 
322 	w->references = 0;
323 	TAILQ_INIT(&w->winlinks);
324 
325 	w->id = next_window_id++;
326 	RB_INSERT(windows, &windows, w);
327 
328 	window_update_activity(w);
329 
330 	return (w);
331 }
332 
333 struct window *
334 window_create_spawn(const char *name, int argc, char **argv, const char *path,
335     const char *shell, const char *cwd, struct environ *env,
336     struct termios *tio, u_int sx, u_int sy, u_int hlimit, char **cause)
337 {
338 	struct window		*w;
339 	struct window_pane	*wp;
340 
341 	w = window_create(sx, sy);
342 	wp = window_add_pane(w, NULL, 0, hlimit);
343 	layout_init(w, wp);
344 
345 	if (window_pane_spawn(wp, argc, argv, path, shell, cwd,
346 	    env, tio, cause) != 0) {
347 		window_destroy(w);
348 		return (NULL);
349 	}
350 
351 	w->active = TAILQ_FIRST(&w->panes);
352 	if (name != NULL) {
353 		w->name = xstrdup(name);
354 		options_set_number(w->options, "automatic-rename", 0);
355 	} else
356 		w->name = default_window_name(w);
357 
358 	return (w);
359 }
360 
361 static void
362 window_destroy(struct window *w)
363 {
364 	if (!TAILQ_EMPTY(&w->winlinks))
365 		fatalx("window destroyed with winlinks");
366 
367 	RB_REMOVE(windows, &windows, w);
368 
369 	if (w->layout_root != NULL)
370 		layout_free_cell(w->layout_root);
371 	if (w->saved_layout_root != NULL)
372 		layout_free_cell(w->saved_layout_root);
373 	free(w->old_layout);
374 
375 	if (event_initialized(&w->name_event))
376 		evtimer_del(&w->name_event);
377 
378 	if (event_initialized(&w->alerts_timer))
379 		evtimer_del(&w->alerts_timer);
380 
381 	options_free(w->options);
382 
383 	window_destroy_panes(w);
384 
385 	free(w->name);
386 	free(w);
387 }
388 
389 void
390 window_remove_ref(struct window *w)
391 {
392 	if (w->references == 0)
393 		fatal("bad reference count");
394 	w->references--;
395 	if (w->references == 0)
396 		window_destroy(w);
397 }
398 
399 void
400 window_set_name(struct window *w, const char *new_name)
401 {
402 	free(w->name);
403 	w->name = xstrdup(new_name);
404 	notify_window("window-renamed", w);
405 }
406 
407 void
408 window_resize(struct window *w, u_int sx, u_int sy)
409 {
410 	w->sx = sx;
411 	w->sy = sy;
412 }
413 
414 int
415 window_has_pane(struct window *w, struct window_pane *wp)
416 {
417 	struct window_pane	*wp1;
418 
419 	TAILQ_FOREACH(wp1, &w->panes, entry) {
420 		if (wp1 == wp)
421 			return (1);
422 	}
423 	return (0);
424 }
425 
426 int
427 window_set_active_pane(struct window *w, struct window_pane *wp)
428 {
429 	log_debug("%s: pane %%%u (was %%%u)", __func__, wp->id, w->active->id);
430 	if (wp == w->active)
431 		return (0);
432 	w->last = w->active;
433 	w->active = wp;
434 	while (!window_pane_visible(w->active)) {
435 		w->active = TAILQ_PREV(w->active, window_panes, entry);
436 		if (w->active == NULL)
437 			w->active = TAILQ_LAST(&w->panes, window_panes);
438 		if (w->active == wp)
439 			return (1);
440 	}
441 	w->active->active_point = next_active_point++;
442 	w->active->flags |= PANE_CHANGED;
443 	return (1);
444 }
445 
446 void
447 window_redraw_active_switch(struct window *w, struct window_pane *wp)
448 {
449 	const struct grid_cell	*gc;
450 
451 	if (wp == w->active)
452 		return;
453 
454 	/*
455 	 * If window-style and window-active-style are the same, we don't need
456 	 * to redraw panes when switching active panes.
457 	 */
458 	gc = options_get_style(w->options, "window-active-style");
459 	if (style_equal(gc, options_get_style(w->options, "window-style")))
460 		return;
461 
462 	/*
463 	 * If the now active or inactive pane do not have a custom style or if
464 	 * the palette is different, they need to be redrawn.
465 	 */
466 	if (window_pane_get_palette(w->active, w->active->colgc.fg) != -1 ||
467 	    window_pane_get_palette(w->active, w->active->colgc.bg) != -1 ||
468 	    style_equal(&grid_default_cell, &w->active->colgc))
469 		w->active->flags |= PANE_REDRAW;
470 	if (window_pane_get_palette(wp, wp->colgc.fg) != -1 ||
471 	    window_pane_get_palette(wp, wp->colgc.bg) != -1 ||
472 	    style_equal(&grid_default_cell, &wp->colgc))
473 		wp->flags |= PANE_REDRAW;
474 }
475 
476 struct window_pane *
477 window_get_active_at(struct window *w, u_int x, u_int y)
478 {
479 	struct window_pane	*wp;
480 
481 	TAILQ_FOREACH(wp, &w->panes, entry) {
482 		if (!window_pane_visible(wp))
483 			continue;
484 		if (x < wp->xoff || x > wp->xoff + wp->sx)
485 			continue;
486 		if (y < wp->yoff || y > wp->yoff + wp->sy)
487 			continue;
488 		return (wp);
489 	}
490 	return (NULL);
491 }
492 
493 struct window_pane *
494 window_find_string(struct window *w, const char *s)
495 {
496 	u_int	x, y;
497 
498 	x = w->sx / 2;
499 	y = w->sy / 2;
500 
501 	if (strcasecmp(s, "top") == 0)
502 		y = 0;
503 	else if (strcasecmp(s, "bottom") == 0)
504 		y = w->sy - 1;
505 	else if (strcasecmp(s, "left") == 0)
506 		x = 0;
507 	else if (strcasecmp(s, "right") == 0)
508 		x = w->sx - 1;
509 	else if (strcasecmp(s, "top-left") == 0) {
510 		x = 0;
511 		y = 0;
512 	} else if (strcasecmp(s, "top-right") == 0) {
513 		x = w->sx - 1;
514 		y = 0;
515 	} else if (strcasecmp(s, "bottom-left") == 0) {
516 		x = 0;
517 		y = w->sy - 1;
518 	} else if (strcasecmp(s, "bottom-right") == 0) {
519 		x = w->sx - 1;
520 		y = w->sy - 1;
521 	} else
522 		return (NULL);
523 
524 	return (window_get_active_at(w, x, y));
525 }
526 
527 int
528 window_zoom(struct window_pane *wp)
529 {
530 	struct window		*w = wp->window;
531 	struct window_pane	*wp1;
532 
533 	if (w->flags & WINDOW_ZOOMED)
534 		return (-1);
535 
536 	if (!window_pane_visible(wp))
537 		return (-1);
538 
539 	if (window_count_panes(w) == 1)
540 		return (-1);
541 
542 	if (w->active != wp)
543 		window_set_active_pane(w, wp);
544 
545 	TAILQ_FOREACH(wp1, &w->panes, entry) {
546 		wp1->saved_layout_cell = wp1->layout_cell;
547 		wp1->layout_cell = NULL;
548 	}
549 
550 	w->saved_layout_root = w->layout_root;
551 	layout_init(w, wp);
552 	w->flags |= WINDOW_ZOOMED;
553 	notify_window("window-layout-changed", w);
554 
555 	return (0);
556 }
557 
558 int
559 window_unzoom(struct window *w)
560 {
561 	struct window_pane	*wp;
562 
563 	if (!(w->flags & WINDOW_ZOOMED))
564 		return (-1);
565 
566 	w->flags &= ~WINDOW_ZOOMED;
567 	layout_free(w);
568 	w->layout_root = w->saved_layout_root;
569 	w->saved_layout_root = NULL;
570 
571 	TAILQ_FOREACH(wp, &w->panes, entry) {
572 		wp->layout_cell = wp->saved_layout_cell;
573 		wp->saved_layout_cell = NULL;
574 	}
575 	layout_fix_panes(w, w->sx, w->sy);
576 	notify_window("window-layout-changed", w);
577 
578 	return (0);
579 }
580 
581 struct window_pane *
582 window_add_pane(struct window *w, struct window_pane *other, int before,
583     u_int hlimit)
584 {
585 	struct window_pane	*wp;
586 
587 	if (other == NULL)
588 		other = w->active;
589 
590 	wp = window_pane_create(w, w->sx, w->sy, hlimit);
591 	if (TAILQ_EMPTY(&w->panes))
592 		TAILQ_INSERT_HEAD(&w->panes, wp, entry);
593 	else if (before)
594 		TAILQ_INSERT_BEFORE(other, wp, entry);
595 	else
596 		TAILQ_INSERT_AFTER(&w->panes, other, wp, entry);
597 	return (wp);
598 }
599 
600 void
601 window_lost_pane(struct window *w, struct window_pane *wp)
602 {
603 	if (wp == marked_pane.wp)
604 		server_clear_marked();
605 
606 	if (wp == w->active) {
607 		w->active = w->last;
608 		w->last = NULL;
609 		if (w->active == NULL) {
610 			w->active = TAILQ_PREV(wp, window_panes, entry);
611 			if (w->active == NULL)
612 				w->active = TAILQ_NEXT(wp, entry);
613 		}
614 		if (w->active != NULL)
615 			w->active->flags |= PANE_CHANGED;
616 	} else if (wp == w->last)
617 		w->last = NULL;
618 }
619 
620 void
621 window_remove_pane(struct window *w, struct window_pane *wp)
622 {
623 	window_lost_pane(w, wp);
624 
625 	TAILQ_REMOVE(&w->panes, wp, entry);
626 	window_pane_destroy(wp);
627 }
628 
629 struct window_pane *
630 window_pane_at_index(struct window *w, u_int idx)
631 {
632 	struct window_pane	*wp;
633 	u_int			 n;
634 
635 	n = options_get_number(w->options, "pane-base-index");
636 	TAILQ_FOREACH(wp, &w->panes, entry) {
637 		if (n == idx)
638 			return (wp);
639 		n++;
640 	}
641 	return (NULL);
642 }
643 
644 struct window_pane *
645 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
646 {
647 	for (; n > 0; n--) {
648 		if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
649 			wp = TAILQ_FIRST(&w->panes);
650 	}
651 
652 	return (wp);
653 }
654 
655 struct window_pane *
656 window_pane_previous_by_number(struct window *w, struct window_pane *wp,
657     u_int n)
658 {
659 	for (; n > 0; n--) {
660 		if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
661 			wp = TAILQ_LAST(&w->panes, window_panes);
662 	}
663 
664 	return (wp);
665 }
666 
667 int
668 window_pane_index(struct window_pane *wp, u_int *i)
669 {
670 	struct window_pane	*wq;
671 	struct window		*w = wp->window;
672 
673 	*i = options_get_number(w->options, "pane-base-index");
674 	TAILQ_FOREACH(wq, &w->panes, entry) {
675 		if (wp == wq) {
676 			return (0);
677 		}
678 		(*i)++;
679 	}
680 
681 	return (-1);
682 }
683 
684 u_int
685 window_count_panes(struct window *w)
686 {
687 	struct window_pane	*wp;
688 	u_int			 n;
689 
690 	n = 0;
691 	TAILQ_FOREACH(wp, &w->panes, entry)
692 		n++;
693 	return (n);
694 }
695 
696 void
697 window_destroy_panes(struct window *w)
698 {
699 	struct window_pane	*wp;
700 
701 	while (!TAILQ_EMPTY(&w->panes)) {
702 		wp = TAILQ_FIRST(&w->panes);
703 		TAILQ_REMOVE(&w->panes, wp, entry);
704 		window_pane_destroy(wp);
705 	}
706 }
707 
708 /* Retuns the printable flags on a window, empty string if no flags set. */
709 char *
710 window_printable_flags(struct session *s, struct winlink *wl)
711 {
712 	char	flags[32];
713 	int	pos;
714 
715 	pos = 0;
716 	if (wl->flags & WINLINK_ACTIVITY)
717 		flags[pos++] = '#';
718 	if (wl->flags & WINLINK_BELL)
719 		flags[pos++] = '!';
720 	if (wl->flags & WINLINK_SILENCE)
721 		flags[pos++] = '~';
722 	if (wl == s->curw)
723 		flags[pos++] = '*';
724 	if (wl == TAILQ_FIRST(&s->lastw))
725 		flags[pos++] = '-';
726 	if (server_check_marked() && wl == marked_pane.wl)
727 		flags[pos++] = 'M';
728 	if (wl->window->flags & WINDOW_ZOOMED)
729 		flags[pos++] = 'Z';
730 	flags[pos] = '\0';
731 	return (xstrdup(flags));
732 }
733 
734 struct window_pane *
735 window_pane_find_by_id_str(const char *s)
736 {
737 	const char	*errstr;
738 	u_int		 id;
739 
740 	if (*s != '%')
741 		return (NULL);
742 
743 	id = strtonum(s + 1, 0, UINT_MAX, &errstr);
744 	if (errstr != NULL)
745 		return (NULL);
746 	return (window_pane_find_by_id(id));
747 }
748 
749 struct window_pane *
750 window_pane_find_by_id(u_int id)
751 {
752 	struct window_pane	wp;
753 
754 	wp.id = id;
755 	return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
756 }
757 
758 static struct window_pane *
759 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
760 {
761 	struct window_pane	*wp;
762 	char			 host[HOST_NAME_MAX + 1];
763 
764 	wp = xcalloc(1, sizeof *wp);
765 	wp->window = w;
766 
767 	wp->id = next_window_pane_id++;
768 	RB_INSERT(window_pane_tree, &all_window_panes, wp);
769 
770 	wp->argc = 0;
771 	wp->argv = NULL;
772 	wp->shell = NULL;
773 	wp->cwd = NULL;
774 
775 	wp->fd = -1;
776 	wp->event = NULL;
777 
778 	wp->mode = NULL;
779 	wp->modeprefix = 1;
780 
781 	wp->layout_cell = NULL;
782 
783 	wp->xoff = 0;
784 	wp->yoff = 0;
785 
786 	wp->sx = sx;
787 	wp->sy = sy;
788 
789 	wp->pipe_fd = -1;
790 	wp->pipe_off = 0;
791 	wp->pipe_event = NULL;
792 
793 	wp->saved_grid = NULL;
794 
795 	memcpy(&wp->colgc, &grid_default_cell, sizeof wp->colgc);
796 
797 	screen_init(&wp->base, sx, sy, hlimit);
798 	wp->screen = &wp->base;
799 
800 	screen_init(&wp->status_screen, 1, 1, 0);
801 
802 	if (gethostname(host, sizeof host) == 0)
803 		screen_set_title(&wp->base, host);
804 
805 	input_init(wp);
806 
807 	return (wp);
808 }
809 
810 static void
811 window_pane_destroy(struct window_pane *wp)
812 {
813 	window_pane_reset_mode(wp);
814 
815 	if (wp->fd != -1) {
816 		bufferevent_free(wp->event);
817 		close(wp->fd);
818 	}
819 
820 	input_free(wp);
821 
822 	screen_free(&wp->base);
823 	if (wp->saved_grid != NULL)
824 		grid_destroy(wp->saved_grid);
825 
826 	if (wp->pipe_fd != -1) {
827 		bufferevent_free(wp->pipe_event);
828 		close(wp->pipe_fd);
829 	}
830 
831 	if (event_initialized(&wp->resize_timer))
832 		event_del(&wp->resize_timer);
833 
834 	RB_REMOVE(window_pane_tree, &all_window_panes, wp);
835 
836 	free((void *)wp->cwd);
837 	free(wp->shell);
838 	cmd_free_argv(wp->argc, wp->argv);
839 	free(wp->palette);
840 	free(wp);
841 }
842 
843 int
844 window_pane_spawn(struct window_pane *wp, int argc, char **argv,
845     const char *path, const char *shell, const char *cwd, struct environ *env,
846     struct termios *tio, char **cause)
847 {
848 	struct winsize	 ws;
849 	char		*argv0, *cmd, **argvp;
850 	const char	*ptr, *first, *home;
851 	struct termios	 tio2;
852 	int		 i;
853 
854 	if (wp->fd != -1) {
855 		bufferevent_free(wp->event);
856 		close(wp->fd);
857 	}
858 	if (argc > 0) {
859 		cmd_free_argv(wp->argc, wp->argv);
860 		wp->argc = argc;
861 		wp->argv = cmd_copy_argv(argc, argv);
862 	}
863 	if (shell != NULL) {
864 		free(wp->shell);
865 		wp->shell = xstrdup(shell);
866 	}
867 	if (cwd != NULL) {
868 		free((void *)wp->cwd);
869 		wp->cwd = xstrdup(cwd);
870 	}
871 
872 	cmd = cmd_stringify_argv(wp->argc, wp->argv);
873 	log_debug("spawn: %s -- %s", wp->shell, cmd);
874 	for (i = 0; i < wp->argc; i++)
875 		log_debug("spawn: argv[%d] = %s", i, wp->argv[i]);
876 	environ_log(env, "spawn: ");
877 
878 	memset(&ws, 0, sizeof ws);
879 	ws.ws_col = screen_size_x(&wp->base);
880 	ws.ws_row = screen_size_y(&wp->base);
881 
882 	wp->pid = pty_fork(ptm_fd, &wp->fd, wp->tty, sizeof wp->tty, &ws);
883 	switch (wp->pid) {
884 	case -1:
885 		wp->fd = -1;
886 		xasprintf(cause, "%s: %s", cmd, strerror(errno));
887 		free(cmd);
888 		return (-1);
889 	case 0:
890 		if (chdir(wp->cwd) != 0) {
891 			if ((home = find_home()) == NULL || chdir(home) != 0)
892 				chdir("/");
893 		}
894 
895 		if (tcgetattr(STDIN_FILENO, &tio2) != 0)
896 			fatal("tcgetattr failed");
897 		if (tio != NULL)
898 			memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
899 		tio2.c_cc[VERASE] = '\177';
900 		if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
901 			fatal("tcgetattr failed");
902 
903 		closefrom(STDERR_FILENO + 1);
904 
905 		if (path != NULL)
906 			environ_set(env, "PATH", "%s", path);
907 		environ_set(env, "TMUX_PANE", "%%%u", wp->id);
908 		environ_push(env);
909 
910 		clear_signals(1);
911 		log_close();
912 
913 		setenv("SHELL", wp->shell, 1);
914 		ptr = strrchr(wp->shell, '/');
915 
916 		/*
917 		 * If given one argument, assume it should be passed to sh -c;
918 		 * with more than one argument, use execvp(). If there is no
919 		 * arguments, create a login shell.
920 		 */
921 		if (wp->argc > 0) {
922 			if (wp->argc != 1) {
923 				/* Copy to ensure argv ends in NULL. */
924 				argvp = cmd_copy_argv(wp->argc, wp->argv);
925 				execvp(argvp[0], argvp);
926 				fatal("execvp failed");
927 			}
928 			first = wp->argv[0];
929 
930 			if (ptr != NULL && *(ptr + 1) != '\0')
931 				xasprintf(&argv0, "%s", ptr + 1);
932 			else
933 				xasprintf(&argv0, "%s", wp->shell);
934 			execl(wp->shell, argv0, "-c", first, (char *)NULL);
935 			fatal("execl failed");
936 		}
937 		if (ptr != NULL && *(ptr + 1) != '\0')
938 			xasprintf(&argv0, "-%s", ptr + 1);
939 		else
940 			xasprintf(&argv0, "-%s", wp->shell);
941 		execl(wp->shell, argv0, (char *)NULL);
942 		fatal("execl failed");
943 	}
944 
945 	setblocking(wp->fd, 0);
946 
947 	wp->event = bufferevent_new(wp->fd, window_pane_read_callback, NULL,
948 	    window_pane_error_callback, wp);
949 
950 	bufferevent_setwatermark(wp->event, EV_READ, 0, READ_SIZE);
951 	bufferevent_enable(wp->event, EV_READ|EV_WRITE);
952 
953 	free(cmd);
954 	return (0);
955 }
956 
957 static void
958 window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
959 {
960 	struct window_pane	*wp = data;
961 	struct evbuffer		*evb = wp->event->input;
962 	size_t			 size = EVBUFFER_LENGTH(evb);
963 	char			*new_data;
964 	size_t			 new_size;
965 
966 	new_size = size - wp->pipe_off;
967 	if (wp->pipe_fd != -1 && new_size > 0) {
968 		new_data = EVBUFFER_DATA(evb) + wp->pipe_off;
969 		bufferevent_write(wp->pipe_event, new_data, new_size);
970 	}
971 
972 	log_debug("%%%u has %zu bytes", wp->id, size);
973 	input_parse(wp);
974 
975 	wp->pipe_off = EVBUFFER_LENGTH(evb);
976 }
977 
978 static void
979 window_pane_error_callback(__unused struct bufferevent *bufev,
980     __unused short what, void *data)
981 {
982 	struct window_pane *wp = data;
983 
984 	server_destroy_pane(wp, 1);
985 }
986 
987 void
988 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
989 {
990 	if (sx == wp->sx && sy == wp->sy)
991 		return;
992 	wp->sx = sx;
993 	wp->sy = sy;
994 
995 	screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
996 	if (wp->mode != NULL)
997 		wp->mode->resize(wp, sx, sy);
998 
999 	wp->flags |= PANE_RESIZE;
1000 }
1001 
1002 /*
1003  * Enter alternative screen mode. A copy of the visible screen is saved and the
1004  * history is not updated
1005  */
1006 void
1007 window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
1008     int cursor)
1009 {
1010 	struct screen	*s = &wp->base;
1011 	u_int		 sx, sy;
1012 
1013 	if (wp->saved_grid != NULL)
1014 		return;
1015 	if (!options_get_number(wp->window->options, "alternate-screen"))
1016 		return;
1017 	sx = screen_size_x(s);
1018 	sy = screen_size_y(s);
1019 
1020 	wp->saved_grid = grid_create(sx, sy, 0);
1021 	grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1022 	if (cursor) {
1023 		wp->saved_cx = s->cx;
1024 		wp->saved_cy = s->cy;
1025 	}
1026 	memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
1027 
1028 	grid_view_clear(s->grid, 0, 0, sx, sy, 8);
1029 
1030 	wp->base.grid->flags &= ~GRID_HISTORY;
1031 
1032 	wp->flags |= PANE_REDRAW;
1033 }
1034 
1035 /* Exit alternate screen mode and restore the copied grid. */
1036 void
1037 window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
1038     int cursor)
1039 {
1040 	struct screen	*s = &wp->base;
1041 	u_int		 sx, sy;
1042 
1043 	if (wp->saved_grid == NULL)
1044 		return;
1045 	if (!options_get_number(wp->window->options, "alternate-screen"))
1046 		return;
1047 	sx = screen_size_x(s);
1048 	sy = screen_size_y(s);
1049 
1050 	/*
1051 	 * If the current size is bigger, temporarily resize to the old size
1052 	 * before copying back.
1053 	 */
1054 	if (sy > wp->saved_grid->sy)
1055 		screen_resize(s, sx, wp->saved_grid->sy, 1);
1056 
1057 	/* Restore the grid, cursor position and cell. */
1058 	grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
1059 	if (cursor)
1060 		s->cx = wp->saved_cx;
1061 	if (s->cx > screen_size_x(s) - 1)
1062 		s->cx = screen_size_x(s) - 1;
1063 	if (cursor)
1064 		s->cy = wp->saved_cy;
1065 	if (s->cy > screen_size_y(s) - 1)
1066 		s->cy = screen_size_y(s) - 1;
1067 	memcpy(gc, &wp->saved_cell, sizeof *gc);
1068 
1069 	/*
1070 	 * Turn history back on (so resize can use it) and then resize back to
1071 	 * the current size.
1072 	 */
1073 	wp->base.grid->flags |= GRID_HISTORY;
1074 	if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
1075 		screen_resize(s, sx, sy, 1);
1076 
1077 	grid_destroy(wp->saved_grid);
1078 	wp->saved_grid = NULL;
1079 
1080 	wp->flags |= PANE_REDRAW;
1081 }
1082 
1083 void
1084 window_pane_set_palette(struct window_pane *wp, u_int n, int colour)
1085 {
1086 	if (n > 0xff)
1087 		return;
1088 
1089 	if (wp->palette == NULL)
1090 		wp->palette = xcalloc(0x100, sizeof *wp->palette);
1091 
1092 	wp->palette[n] = colour;
1093 	wp->flags |= PANE_REDRAW;
1094 }
1095 
1096 void
1097 window_pane_unset_palette(struct window_pane *wp, u_int n)
1098 {
1099 	if (n > 0xff || wp->palette == NULL)
1100 		return;
1101 
1102 	wp->palette[n] = 0;
1103 	wp->flags |= PANE_REDRAW;
1104 }
1105 
1106 void
1107 window_pane_reset_palette(struct window_pane *wp)
1108 {
1109 	if (wp->palette == NULL)
1110 		return;
1111 
1112 	free(wp->palette);
1113 	wp->palette = NULL;
1114 	wp->flags |= PANE_REDRAW;
1115 }
1116 
1117 int
1118 window_pane_get_palette(const struct window_pane *wp, int c)
1119 {
1120 	int	new;
1121 
1122 	if (wp == NULL || wp->palette == NULL)
1123 		return (-1);
1124 
1125 	new = -1;
1126 	if (c < 8)
1127 		new = wp->palette[c];
1128 	else if (c >= 90 && c <= 97)
1129 		new = wp->palette[8 + c - 90];
1130 	else if (c & COLOUR_FLAG_256)
1131 		new = wp->palette[c & ~COLOUR_FLAG_256];
1132 	if (new == 0)
1133 		return (-1);
1134 	return (new);
1135 }
1136 
1137 static void
1138 window_pane_mode_timer(__unused int fd, __unused short events, void *arg)
1139 {
1140 	struct window_pane	*wp = arg;
1141 	struct timeval		 tv = { .tv_sec = 10 };
1142 	int			 n = 0;
1143 
1144 	evtimer_del(&wp->modetimer);
1145 	evtimer_add(&wp->modetimer, &tv);
1146 
1147 	log_debug("%%%u in mode: last=%ld", wp->id, (long)wp->modelast);
1148 
1149 	if (wp->modelast < time(NULL) - WINDOW_MODE_TIMEOUT) {
1150 		if (ioctl(wp->fd, FIONREAD, &n) == -1 || n > 0)
1151 			window_pane_reset_mode(wp);
1152 	}
1153 }
1154 
1155 int
1156 window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
1157 {
1158 	struct screen	*s;
1159 	struct timeval	 tv = { .tv_sec = 10 };
1160 
1161 	if (wp->mode != NULL)
1162 		return (1);
1163 	wp->mode = mode;
1164 
1165 	wp->modelast = time(NULL);
1166 	evtimer_set(&wp->modetimer, window_pane_mode_timer, wp);
1167 	evtimer_add(&wp->modetimer, &tv);
1168 
1169 	if ((s = wp->mode->init(wp)) != NULL)
1170 		wp->screen = s;
1171 	wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1172 
1173 	server_status_window(wp->window);
1174 	return (0);
1175 }
1176 
1177 void
1178 window_pane_reset_mode(struct window_pane *wp)
1179 {
1180 	if (wp->mode == NULL)
1181 		return;
1182 
1183 	evtimer_del(&wp->modetimer);
1184 
1185 	wp->mode->free(wp);
1186 	wp->mode = NULL;
1187 	wp->modeprefix = 1;
1188 
1189 	wp->screen = &wp->base;
1190 	wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1191 
1192 	server_status_window(wp->window);
1193 }
1194 
1195 void
1196 window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1197     key_code key, struct mouse_event *m)
1198 {
1199 	struct window_pane	*wp2;
1200 
1201 	if (KEYC_IS_MOUSE(key) && m == NULL)
1202 		return;
1203 
1204 	if (wp->mode != NULL) {
1205 		wp->modelast = time(NULL);
1206 		if (wp->mode->key != NULL)
1207 			wp->mode->key(wp, c, s, key, m);
1208 		return;
1209 	}
1210 
1211 	if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1212 		return;
1213 
1214 	input_key(wp, key, m);
1215 
1216 	if (KEYC_IS_MOUSE(key))
1217 		return;
1218 	if (options_get_number(wp->window->options, "synchronize-panes")) {
1219 		TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1220 			if (wp2 == wp || wp2->mode != NULL)
1221 				continue;
1222 			if (wp2->fd == -1 || wp2->flags & PANE_INPUTOFF)
1223 				continue;
1224 			if (window_pane_visible(wp2))
1225 				input_key(wp2, key, NULL);
1226 		}
1227 	}
1228 }
1229 
1230 int
1231 window_pane_outside(struct window_pane *wp)
1232 {
1233 	struct window	*w = wp->window;
1234 
1235 	if (wp->xoff >= w->sx || wp->yoff >= w->sy)
1236 		return (1);
1237 	if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
1238 		return (1);
1239 	return (0);
1240 }
1241 
1242 int
1243 window_pane_visible(struct window_pane *wp)
1244 {
1245 	if (wp->layout_cell == NULL)
1246 		return (0);
1247 	return (!window_pane_outside(wp));
1248 }
1249 
1250 char *
1251 window_pane_search(struct window_pane *wp, const char *searchstr,
1252     u_int *lineno)
1253 {
1254 	struct screen	*s = &wp->base;
1255 	char		*newsearchstr, *line, *msg;
1256 	u_int	 	 i;
1257 
1258 	msg = NULL;
1259 	xasprintf(&newsearchstr, "*%s*", searchstr);
1260 
1261 	for (i = 0; i < screen_size_y(s); i++) {
1262 		line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1263 		if (fnmatch(newsearchstr, line, 0) == 0) {
1264 			msg = line;
1265 			if (lineno != NULL)
1266 				*lineno = i;
1267 			break;
1268 		}
1269 		free(line);
1270 	}
1271 
1272 	free(newsearchstr);
1273 	return (msg);
1274 }
1275 
1276 /* Get MRU pane from a list. */
1277 static struct window_pane *
1278 window_pane_choose_best(struct window_pane **list, u_int size)
1279 {
1280 	struct window_pane	*next, *best;
1281 	u_int			 i;
1282 
1283 	if (size == 0)
1284 		return (NULL);
1285 
1286 	best = list[0];
1287 	for (i = 1; i < size; i++) {
1288 		next = list[i];
1289 		if (next->active_point > best->active_point)
1290 			best = next;
1291 	}
1292 	return (best);
1293 }
1294 
1295 /*
1296  * Find the pane directly above another. We build a list of those adjacent to
1297  * top edge and then choose the best.
1298  */
1299 struct window_pane *
1300 window_pane_find_up(struct window_pane *wp)
1301 {
1302 	struct window_pane	*next, *best, **list;
1303 	u_int			 edge, left, right, end, size;
1304 	int			 found;
1305 
1306 	if (wp == NULL || !window_pane_visible(wp))
1307 		return (NULL);
1308 
1309 	list = NULL;
1310 	size = 0;
1311 
1312 	edge = wp->yoff;
1313 	if (edge == 0)
1314 		edge = wp->window->sy + 1;
1315 
1316 	left = wp->xoff;
1317 	right = wp->xoff + wp->sx;
1318 
1319 	TAILQ_FOREACH(next, &wp->window->panes, entry) {
1320 		if (next == wp || !window_pane_visible(next))
1321 			continue;
1322 		if (next->yoff + next->sy + 1 != edge)
1323 			continue;
1324 		end = next->xoff + next->sx - 1;
1325 
1326 		found = 0;
1327 		if (next->xoff < left && end > right)
1328 			found = 1;
1329 		else if (next->xoff >= left && next->xoff <= right)
1330 			found = 1;
1331 		else if (end >= left && end <= right)
1332 			found = 1;
1333 		if (!found)
1334 			continue;
1335 		list = xreallocarray(list, size + 1, sizeof *list);
1336 		list[size++] = next;
1337 	}
1338 
1339 	best = window_pane_choose_best(list, size);
1340 	free(list);
1341 	return (best);
1342 }
1343 
1344 /* Find the pane directly below another. */
1345 struct window_pane *
1346 window_pane_find_down(struct window_pane *wp)
1347 {
1348 	struct window_pane	*next, *best, **list;
1349 	u_int			 edge, left, right, end, size;
1350 	int			 found;
1351 
1352 	if (wp == NULL || !window_pane_visible(wp))
1353 		return (NULL);
1354 
1355 	list = NULL;
1356 	size = 0;
1357 
1358 	edge = wp->yoff + wp->sy + 1;
1359 	if (edge >= wp->window->sy)
1360 		edge = 0;
1361 
1362 	left = wp->xoff;
1363 	right = wp->xoff + wp->sx;
1364 
1365 	TAILQ_FOREACH(next, &wp->window->panes, entry) {
1366 		if (next == wp || !window_pane_visible(next))
1367 			continue;
1368 		if (next->yoff != edge)
1369 			continue;
1370 		end = next->xoff + next->sx - 1;
1371 
1372 		found = 0;
1373 		if (next->xoff < left && end > right)
1374 			found = 1;
1375 		else if (next->xoff >= left && next->xoff <= right)
1376 			found = 1;
1377 		else if (end >= left && end <= right)
1378 			found = 1;
1379 		if (!found)
1380 			continue;
1381 		list = xreallocarray(list, size + 1, sizeof *list);
1382 		list[size++] = next;
1383 	}
1384 
1385 	best = window_pane_choose_best(list, size);
1386 	free(list);
1387 	return (best);
1388 }
1389 
1390 /* Find the pane directly to the left of another. */
1391 struct window_pane *
1392 window_pane_find_left(struct window_pane *wp)
1393 {
1394 	struct window_pane	*next, *best, **list;
1395 	u_int			 edge, top, bottom, end, size;
1396 	int			 found;
1397 
1398 	if (wp == NULL || !window_pane_visible(wp))
1399 		return (NULL);
1400 
1401 	list = NULL;
1402 	size = 0;
1403 
1404 	edge = wp->xoff;
1405 	if (edge == 0)
1406 		edge = wp->window->sx + 1;
1407 
1408 	top = wp->yoff;
1409 	bottom = wp->yoff + wp->sy;
1410 
1411 	TAILQ_FOREACH(next, &wp->window->panes, entry) {
1412 		if (next == wp || !window_pane_visible(next))
1413 			continue;
1414 		if (next->xoff + next->sx + 1 != edge)
1415 			continue;
1416 		end = next->yoff + next->sy - 1;
1417 
1418 		found = 0;
1419 		if (next->yoff < top && end > bottom)
1420 			found = 1;
1421 		else if (next->yoff >= top && next->yoff <= bottom)
1422 			found = 1;
1423 		else if (end >= top && end <= bottom)
1424 			found = 1;
1425 		if (!found)
1426 			continue;
1427 		list = xreallocarray(list, size + 1, sizeof *list);
1428 		list[size++] = next;
1429 	}
1430 
1431 	best = window_pane_choose_best(list, size);
1432 	free(list);
1433 	return (best);
1434 }
1435 
1436 /* Find the pane directly to the right of another. */
1437 struct window_pane *
1438 window_pane_find_right(struct window_pane *wp)
1439 {
1440 	struct window_pane	*next, *best, **list;
1441 	u_int			 edge, top, bottom, end, size;
1442 	int			 found;
1443 
1444 	if (wp == NULL || !window_pane_visible(wp))
1445 		return (NULL);
1446 
1447 	list = NULL;
1448 	size = 0;
1449 
1450 	edge = wp->xoff + wp->sx + 1;
1451 	if (edge >= wp->window->sx)
1452 		edge = 0;
1453 
1454 	top = wp->yoff;
1455 	bottom = wp->yoff + wp->sy;
1456 
1457 	TAILQ_FOREACH(next, &wp->window->panes, entry) {
1458 		if (next == wp || !window_pane_visible(next))
1459 			continue;
1460 		if (next->xoff != edge)
1461 			continue;
1462 		end = next->yoff + next->sy - 1;
1463 
1464 		found = 0;
1465 		if (next->yoff < top && end > bottom)
1466 			found = 1;
1467 		else if (next->yoff >= top && next->yoff <= bottom)
1468 			found = 1;
1469 		else if (end >= top && end <= bottom)
1470 			found = 1;
1471 		if (!found)
1472 			continue;
1473 		list = xreallocarray(list, size + 1, sizeof *list);
1474 		list[size++] = next;
1475 	}
1476 
1477 	best = window_pane_choose_best(list, size);
1478 	free(list);
1479 	return (best);
1480 }
1481 
1482 /* Clear alert flags for a winlink */
1483 void
1484 winlink_clear_flags(struct winlink *wl)
1485 {
1486 	struct winlink	*loop;
1487 
1488 	wl->window->flags &= ~WINDOW_ALERTFLAGS;
1489 	TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
1490 		if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
1491 			loop->flags &= ~WINLINK_ALERTFLAGS;
1492 			server_status_session(loop->session);
1493 		}
1494 	}
1495 }
1496 
1497 /* Shuffle window indexes up. */
1498 int
1499 winlink_shuffle_up(struct session *s, struct winlink *wl)
1500 {
1501 	int	 idx, last;
1502 
1503 	idx = wl->idx + 1;
1504 
1505 	/* Find the next free index. */
1506 	for (last = idx; last < INT_MAX; last++) {
1507 		if (winlink_find_by_index(&s->windows, last) == NULL)
1508 			break;
1509 	}
1510 	if (last == INT_MAX)
1511 		return (-1);
1512 
1513 	/* Move everything from last - 1 to idx up a bit. */
1514 	for (; last > idx; last--) {
1515 		wl = winlink_find_by_index(&s->windows, last - 1);
1516 		server_link_window(s, wl, s, last, 0, 0, NULL);
1517 		server_unlink_window(s, wl);
1518 	}
1519 
1520 	return (idx);
1521 }
1522