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