Update man page for update-environment.
[tmux-openbsd.git] / window.c
bloba7a5c40ec3ccd06e0d695d55fc2d4290ec514627
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <fnmatch.h>
25 #include <paths.h>
26 #include <pwd.h>
27 #include <signal.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <termios.h>
32 #include <unistd.h>
33 #include <util.h>
35 #include "tmux.h"
38 * Each window is attached to a number of panes, each of which is a pty. This
39 * file contains code to handle them.
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.
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.
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.
56 /* Global window list. */
57 struct windows windows;
59 void window_pane_read_callback(struct bufferevent *, void *);
60 void window_pane_error_callback(struct bufferevent *, short, void *);
62 RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
64 int
65 winlink_cmp(struct winlink *wl1, struct winlink *wl2)
67 return (wl1->idx - wl2->idx);
70 struct winlink *
71 winlink_find_by_window(struct winlinks *wwl, struct window *w)
73 struct winlink *wl;
75 RB_FOREACH(wl, winlinks, wwl) {
76 if (wl->window == w)
77 return (wl);
80 return (NULL);
83 struct winlink *
84 winlink_find_by_index(struct winlinks *wwl, int idx)
86 struct winlink wl;
88 if (idx < 0)
89 fatalx("bad index");
91 wl.idx = idx;
92 return (RB_FIND(winlinks, wwl, &wl));
95 int
96 winlink_next_index(struct winlinks *wwl, int idx)
98 int i;
100 i = idx;
101 do {
102 if (winlink_find_by_index(wwl, i) == NULL)
103 return (i);
104 if (i == INT_MAX)
105 i = 0;
106 else
107 i++;
108 } while (i != idx);
109 return (-1);
112 u_int
113 winlink_count(struct winlinks *wwl)
115 struct winlink *wl;
116 u_int n;
118 n = 0;
119 RB_FOREACH(wl, winlinks, wwl)
120 n++;
122 return (n);
125 struct winlink *
126 winlink_add(struct winlinks *wwl, struct window *w, int idx)
128 struct winlink *wl;
130 if (idx < 0) {
131 if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
132 return (NULL);
133 } else if (winlink_find_by_index(wwl, idx) != NULL)
134 return (NULL);
136 wl = xcalloc(1, sizeof *wl);
137 wl->idx = idx;
138 wl->window = w;
139 RB_INSERT(winlinks, wwl, wl);
141 w->references++;
143 return (wl);
146 void
147 winlink_remove(struct winlinks *wwl, struct winlink *wl)
149 struct window *w = wl->window;
151 RB_REMOVE(winlinks, wwl, wl);
152 if (wl->status_text != NULL)
153 xfree(wl->status_text);
154 xfree(wl);
156 if (w->references == 0)
157 fatal("bad reference count");
158 w->references--;
159 if (w->references == 0)
160 window_destroy(w);
163 struct winlink *
164 winlink_next(struct winlink *wl)
166 return (RB_NEXT(winlinks, wwl, wl));
169 struct winlink *
170 winlink_previous(struct winlink *wl)
172 return (RB_PREV(winlinks, wwl, wl));
175 struct winlink *
176 winlink_next_by_number(struct winlink *wl, struct session *s, int n)
178 for (; n > 0; n--) {
179 if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
180 wl = RB_MIN(winlinks, &s->windows);
183 return (wl);
186 struct winlink *
187 winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
189 for (; n > 0; n--) {
190 if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
191 wl = RB_MAX(winlinks, &s->windows);
194 return (wl);
197 void
198 winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
200 if (wl == NULL)
201 return;
203 winlink_stack_remove(stack, wl);
204 TAILQ_INSERT_HEAD(stack, wl, sentry);
207 void
208 winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
210 struct winlink *wl2;
212 if (wl == NULL)
213 return;
215 TAILQ_FOREACH(wl2, stack, sentry) {
216 if (wl2 == wl) {
217 TAILQ_REMOVE(stack, wl, sentry);
218 return;
224 window_index(struct window *s, u_int *i)
226 for (*i = 0; *i < ARRAY_LENGTH(&windows); (*i)++) {
227 if (s == ARRAY_ITEM(&windows, *i))
228 return (0);
230 return (-1);
233 struct window *
234 window_create1(u_int sx, u_int sy)
236 struct window *w;
237 u_int i;
239 w = xcalloc(1, sizeof *w);
240 w->name = NULL;
241 w->flags = 0;
243 TAILQ_INIT(&w->panes);
244 w->active = NULL;
246 w->lastlayout = -1;
247 w->layout_root = NULL;
249 w->sx = sx;
250 w->sy = sy;
252 queue_window_name(w);
254 options_init(&w->options, &global_w_options);
256 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
257 if (ARRAY_ITEM(&windows, i) == NULL) {
258 ARRAY_SET(&windows, i, w);
259 break;
262 if (i == ARRAY_LENGTH(&windows))
263 ARRAY_ADD(&windows, w);
264 w->references = 0;
266 return (w);
269 struct window *
270 window_create(const char *name, const char *cmd, const char *shell,
271 const char *cwd, struct environ *env, struct termios *tio,
272 u_int sx, u_int sy, u_int hlimit,char **cause)
274 struct window *w;
275 struct window_pane *wp;
277 w = window_create1(sx, sy);
278 wp = window_add_pane(w, hlimit);
279 layout_init(w);
280 if (window_pane_spawn(wp, cmd, shell, cwd, env, tio, cause) != 0) {
281 window_destroy(w);
282 return (NULL);
284 w->active = TAILQ_FIRST(&w->panes);
285 if (name != NULL) {
286 w->name = xstrdup(name);
287 options_set_number(&w->options, "automatic-rename", 0);
288 } else
289 w->name = default_window_name(w);
290 return (w);
293 void
294 window_destroy(struct window *w)
296 u_int i;
298 if (window_index(w, &i) != 0)
299 fatalx("index not found");
300 ARRAY_SET(&windows, i, NULL);
301 while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
302 ARRAY_TRUNC(&windows, 1);
304 if (w->layout_root != NULL)
305 layout_free(w);
307 evtimer_del(&w->name_timer);
309 options_free(&w->options);
311 window_destroy_panes(w);
313 if (w->name != NULL)
314 xfree(w->name);
315 xfree(w);
318 void
319 window_resize(struct window *w, u_int sx, u_int sy)
321 w->sx = sx;
322 w->sy = sy;
325 void
326 window_set_active_pane(struct window *w, struct window_pane *wp)
328 if (wp == w->active)
329 return;
330 w->last = w->active;
331 w->active = wp;
332 while (!window_pane_visible(w->active)) {
333 w->active = TAILQ_PREV(w->active, window_panes, entry);
334 if (w->active == NULL)
335 w->active = TAILQ_LAST(&w->panes, window_panes);
336 if (w->active == wp)
337 return;
341 void
342 window_set_active_at(struct window *w, u_int x, u_int y)
344 struct window_pane *wp;
346 TAILQ_FOREACH(wp, &w->panes, entry) {
347 if (wp == w->active || !window_pane_visible(wp))
348 continue;
349 if (x < wp->xoff || x >= wp->xoff + wp->sx)
350 continue;
351 if (y < wp->yoff || y >= wp->yoff + wp->sy)
352 continue;
353 window_set_active_pane(w, wp);
354 break;
358 struct window_pane *
359 window_add_pane(struct window *w, u_int hlimit)
361 struct window_pane *wp;
363 wp = window_pane_create(w, w->sx, w->sy, hlimit);
364 if (TAILQ_EMPTY(&w->panes))
365 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
366 else
367 TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
368 return (wp);
371 void
372 window_remove_pane(struct window *w, struct window_pane *wp)
374 if (wp == w->active) {
375 w->active = w->last;
376 w->last = NULL;
377 if (w->active == NULL) {
378 w->active = TAILQ_PREV(wp, window_panes, entry);
379 if (w->active == NULL)
380 w->active = TAILQ_NEXT(wp, entry);
382 } else if (wp == w->last)
383 w->last = NULL;
385 TAILQ_REMOVE(&w->panes, wp, entry);
386 window_pane_destroy(wp);
389 struct window_pane *
390 window_pane_at_index(struct window *w, u_int idx)
392 struct window_pane *wp;
393 u_int n;
395 n = 0;
396 TAILQ_FOREACH(wp, &w->panes, entry) {
397 if (n == idx)
398 return (wp);
399 n++;
401 return (NULL);
404 struct window_pane *
405 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
407 for (; n > 0; n--) {
408 if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
409 wp = TAILQ_FIRST(&w->panes);
412 return (wp);
415 struct window_pane *
416 window_pane_previous_by_number(struct window *w, struct window_pane *wp,
417 u_int n)
419 for (; n > 0; n--) {
420 if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
421 wp = TAILQ_LAST(&w->panes, window_panes);
424 return (wp);
427 u_int
428 window_pane_index(struct window *w, struct window_pane *wp)
430 struct window_pane *wq;
431 u_int n;
433 n = 0;
434 TAILQ_FOREACH(wq, &w->panes, entry) {
435 if (wp == wq)
436 break;
437 n++;
439 return (n);
442 u_int
443 window_count_panes(struct window *w)
445 struct window_pane *wp;
446 u_int n;
448 n = 0;
449 TAILQ_FOREACH(wp, &w->panes, entry)
450 n++;
451 return (n);
454 void
455 window_destroy_panes(struct window *w)
457 struct window_pane *wp;
459 while (!TAILQ_EMPTY(&w->panes)) {
460 wp = TAILQ_FIRST(&w->panes);
461 TAILQ_REMOVE(&w->panes, wp, entry);
462 window_pane_destroy(wp);
466 struct window_pane *
467 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
469 struct window_pane *wp;
471 wp = xcalloc(1, sizeof *wp);
472 wp->window = w;
474 wp->cmd = NULL;
475 wp->shell = NULL;
476 wp->cwd = NULL;
478 wp->fd = -1;
479 wp->event = NULL;
481 wp->mode = NULL;
483 wp->layout_cell = NULL;
485 wp->xoff = 0;
486 wp->yoff = 0;
488 wp->sx = sx;
489 wp->sy = sy;
491 wp->pipe_fd = -1;
492 wp->pipe_off = 0;
493 wp->pipe_event = NULL;
495 wp->saved_grid = NULL;
497 screen_init(&wp->base, sx, sy, hlimit);
498 wp->screen = &wp->base;
500 input_init(wp);
502 return (wp);
505 void
506 window_pane_destroy(struct window_pane *wp)
508 window_pane_reset_mode(wp);
510 if (wp->fd != -1) {
511 close(wp->fd);
512 bufferevent_free(wp->event);
515 input_free(wp);
517 screen_free(&wp->base);
518 if (wp->saved_grid != NULL)
519 grid_destroy(wp->saved_grid);
521 if (wp->pipe_fd != -1) {
522 close(wp->pipe_fd);
523 bufferevent_free(wp->pipe_event);
526 if (wp->cwd != NULL)
527 xfree(wp->cwd);
528 if (wp->shell != NULL)
529 xfree(wp->shell);
530 if (wp->cmd != NULL)
531 xfree(wp->cmd);
532 xfree(wp);
536 window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
537 const char *cwd, struct environ *env, struct termios *tio, char **cause)
539 struct winsize ws;
540 int mode;
541 char *argv0;
542 const char *ptr;
543 struct termios tio2;
545 if (wp->fd != -1) {
546 close(wp->fd);
547 bufferevent_free(wp->event);
549 if (cmd != NULL) {
550 if (wp->cmd != NULL)
551 xfree(wp->cmd);
552 wp->cmd = xstrdup(cmd);
554 if (shell != NULL) {
555 if (wp->shell != NULL)
556 xfree(wp->shell);
557 wp->shell = xstrdup(shell);
559 if (cwd != NULL) {
560 if (wp->cwd != NULL)
561 xfree(wp->cwd);
562 wp->cwd = xstrdup(cwd);
565 memset(&ws, 0, sizeof ws);
566 ws.ws_col = screen_size_x(&wp->base);
567 ws.ws_row = screen_size_y(&wp->base);
569 switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
570 case -1:
571 wp->fd = -1;
572 xasprintf(cause, "%s: %s", cmd, strerror(errno));
573 return (-1);
574 case 0:
575 if (chdir(wp->cwd) != 0)
576 chdir("/");
578 if (tcgetattr(STDIN_FILENO, &tio2) != 0)
579 fatal("tcgetattr failed");
580 if (tio != NULL)
581 memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
582 tio2.c_cc[VERASE] = '\177';
583 if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
584 fatal("tcgetattr failed");
586 closefrom(STDERR_FILENO + 1);
588 environ_push(env);
590 clear_signals(1);
591 log_close();
593 if (*wp->cmd != '\0') {
594 /* Set SHELL but only if it is currently not useful. */
595 shell = getenv("SHELL");
596 if (shell == NULL || *shell == '\0' || areshell(shell))
597 setenv("SHELL", wp->shell, 1);
599 execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
600 fatal("execl failed");
603 /* No command; fork a login shell. */
604 ptr = strrchr(wp->shell, '/');
605 if (ptr != NULL && *(ptr + 1) != '\0')
606 xasprintf(&argv0, "-%s", ptr + 1);
607 else
608 xasprintf(&argv0, "-%s", wp->shell);
609 setenv("SHELL", wp->shell, 1);
610 execl(wp->shell, argv0, (char *) NULL);
611 fatal("execl failed");
614 if ((mode = fcntl(wp->fd, F_GETFL)) == -1)
615 fatal("fcntl failed");
616 if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1)
617 fatal("fcntl failed");
618 wp->event = bufferevent_new(wp->fd,
619 window_pane_read_callback, NULL, window_pane_error_callback, wp);
620 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
622 return (0);
625 /* ARGSUSED */
626 void
627 window_pane_read_callback(unused struct bufferevent *bufev, void *data)
629 struct window_pane *wp = data;
630 char *new_data;
631 size_t new_size;
633 new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
634 if (wp->pipe_fd != -1 && new_size > 0) {
635 new_data = EVBUFFER_DATA(wp->event->input);
636 bufferevent_write(wp->pipe_event, new_data, new_size);
639 input_parse(wp);
641 wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
644 /* ARGSUSED */
645 void
646 window_pane_error_callback(
647 unused struct bufferevent *bufev, unused short what, void *data)
649 struct window_pane *wp = data;
651 server_destroy_pane(wp);
654 void
655 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
657 struct winsize ws;
659 if (sx == wp->sx && sy == wp->sy)
660 return;
661 wp->sx = sx;
662 wp->sy = sy;
664 memset(&ws, 0, sizeof ws);
665 ws.ws_col = sx;
666 ws.ws_row = sy;
668 screen_resize(&wp->base, sx, sy);
669 if (wp->mode != NULL)
670 wp->mode->resize(wp, sx, sy);
672 if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
673 fatal("ioctl failed");
677 * Enter alternative screen mode. A copy of the visible screen is saved and the
678 * history is not updated
680 void
681 window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc)
683 struct screen *s = &wp->base;
684 u_int sx, sy;
686 if (wp->saved_grid != NULL)
687 return;
688 if (!options_get_number(&wp->window->options, "alternate-screen"))
689 return;
690 sx = screen_size_x(s);
691 sy = screen_size_y(s);
693 wp->saved_grid = grid_create(sx, sy, 0);
694 grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
695 wp->saved_cx = s->cx;
696 wp->saved_cy = s->cy;
697 memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
699 grid_view_clear(s->grid, 0, 0, sx, sy);
701 wp->base.grid->flags &= ~GRID_HISTORY;
703 wp->flags |= PANE_REDRAW;
706 /* Exit alternate screen mode and restore the copied grid. */
707 void
708 window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc)
710 struct screen *s = &wp->base;
711 u_int sx, sy;
713 if (wp->saved_grid == NULL)
714 return;
715 if (!options_get_number(&wp->window->options, "alternate-screen"))
716 return;
717 sx = screen_size_x(s);
718 sy = screen_size_y(s);
721 * If the current size is bigger, temporarily resize to the old size
722 * before copying back.
724 if (sy > wp->saved_grid->sy)
725 screen_resize(s, sx, wp->saved_grid->sy);
727 /* Restore the grid, cursor position and cell. */
728 grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
729 s->cx = wp->saved_cx;
730 if (s->cx > screen_size_x(s) - 1)
731 s->cx = screen_size_x(s) - 1;
732 s->cy = wp->saved_cy;
733 if (s->cy > screen_size_y(s) - 1)
734 s->cy = screen_size_y(s) - 1;
735 memcpy(gc, &wp->saved_cell, sizeof *gc);
738 * Turn history back on (so resize can use it) and then resize back to
739 * the current size.
741 wp->base.grid->flags |= GRID_HISTORY;
742 if (sy > wp->saved_grid->sy)
743 screen_resize(s, sx, sy);
745 grid_destroy(wp->saved_grid);
746 wp->saved_grid = NULL;
748 wp->flags |= PANE_REDRAW;
752 window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
754 struct screen *s;
756 if (wp->mode != NULL)
757 return (1);
758 wp->mode = mode;
760 if ((s = wp->mode->init(wp)) != NULL)
761 wp->screen = s;
762 wp->flags |= PANE_REDRAW;
763 return (0);
766 void
767 window_pane_reset_mode(struct window_pane *wp)
769 if (wp->mode == NULL)
770 return;
772 wp->mode->free(wp);
773 wp->mode = NULL;
775 wp->screen = &wp->base;
776 wp->flags |= PANE_REDRAW;
779 void
780 window_pane_key(struct window_pane *wp, struct session *sess, int key)
782 struct window_pane *wp2;
784 if (!window_pane_visible(wp))
785 return;
787 if (wp->mode != NULL) {
788 if (wp->mode->key != NULL)
789 wp->mode->key(wp, sess, key);
790 return;
793 if (wp->fd == -1)
794 return;
795 input_key(wp, key);
796 if (options_get_number(&wp->window->options, "synchronize-panes")) {
797 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
798 if (wp2 == wp || wp2->mode != NULL)
799 continue;
800 if (wp2->fd != -1 && window_pane_visible(wp2))
801 input_key(wp2, key);
806 void
807 window_pane_mouse(
808 struct window_pane *wp, struct session *sess, struct mouse_event *m)
810 if (!window_pane_visible(wp))
811 return;
813 if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
814 return;
815 if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
816 return;
817 m->x -= wp->xoff;
818 m->y -= wp->yoff;
820 if (wp->mode != NULL) {
821 if (wp->mode->mouse != NULL)
822 wp->mode->mouse(wp, sess, m);
823 } else if (wp->fd != -1)
824 input_mouse(wp, m);
828 window_pane_visible(struct window_pane *wp)
830 struct window *w = wp->window;
832 if (wp->xoff >= w->sx || wp->yoff >= w->sy)
833 return (0);
834 if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
835 return (0);
836 return (1);
839 char *
840 window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
842 struct screen *s = &wp->base;
843 char *newsearchstr, *line, *msg;
844 u_int i;
846 msg = NULL;
847 xasprintf(&newsearchstr, "*%s*", searchstr);
849 for (i = 0; i < screen_size_y(s); i++) {
850 line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
851 if (fnmatch(newsearchstr, line, 0) == 0) {
852 msg = line;
853 if (lineno != NULL)
854 *lineno = i;
855 break;
857 xfree(line);
860 xfree(newsearchstr);
861 return (msg);
864 /* Find the pane directly above another. */
865 struct window_pane *
866 window_pane_find_up(struct window_pane *wp)
868 struct window_pane *wp2;
869 u_int left, top;
871 if (wp == NULL || !window_pane_visible(wp))
872 return (NULL);
874 top = wp->yoff;
875 if (top == 0)
876 top = wp->window->sy + 1;
877 left = wp->xoff;
879 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
880 if (!window_pane_visible(wp2))
881 continue;
882 if (wp2->yoff + wp2->sy + 1 != top)
883 continue;
884 if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
885 return (wp2);
887 return (NULL);
890 /* Find the pane directly below another. */
891 struct window_pane *
892 window_pane_find_down(struct window_pane *wp)
894 struct window_pane *wp2;
895 u_int left, bottom;
897 if (wp == NULL || !window_pane_visible(wp))
898 return (NULL);
900 bottom = wp->yoff + wp->sy + 1;
901 if (bottom >= wp->window->sy)
902 bottom = 0;
903 left = wp->xoff;
905 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
906 if (!window_pane_visible(wp2))
907 continue;
908 if (wp2->yoff != bottom)
909 continue;
910 if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
911 return (wp2);
913 return (NULL);
917 * Find the pane directly to the left of another, adjacent to the left side and
918 * containing the top edge.
920 struct window_pane *
921 window_pane_find_left(struct window_pane *wp)
923 struct window_pane *wp2;
924 u_int left, top;
926 if (wp == NULL || !window_pane_visible(wp))
927 return (NULL);
929 left = wp->xoff;
930 if (left == 0)
931 left = wp->window->sx + 1;
932 top = wp->yoff;
934 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
935 if (!window_pane_visible(wp2))
936 continue;
937 if (wp2->xoff + wp2->sx + 1 != left)
938 continue;
939 if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
940 return (wp2);
942 return (NULL);
946 * Find the pane directly to the right of another, that is adjacent to the
947 * right edge and including the top edge.
949 struct window_pane *
950 window_pane_find_right(struct window_pane *wp)
952 struct window_pane *wp2;
953 u_int right, top;
955 if (wp == NULL || !window_pane_visible(wp))
956 return (NULL);
958 right = wp->xoff + wp->sx + 1;
959 if (right >= wp->window->sx)
960 right = 0;
961 top = wp->yoff;
963 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
964 if (!window_pane_visible(wp2))
965 continue;
966 if (wp2->xoff != right)
967 continue;
968 if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
969 return (wp2);
971 return (NULL);