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