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