Make pane/window wrapping more logical (so with 10 windows, +10 from
[tmux-openbsd.git] / window.c
blob705962843cfb030733c2abe5ac5f4cb9611e904f
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 w->active = wp;
329 while (!window_pane_visible(w->active)) {
330 w->active = TAILQ_PREV(w->active, window_panes, entry);
331 if (w->active == NULL)
332 w->active = TAILQ_LAST(&w->panes, window_panes);
333 if (w->active == wp)
334 return;
338 void
339 window_set_active_at(struct window *w, u_int x, u_int y)
341 struct window_pane *wp;
343 TAILQ_FOREACH(wp, &w->panes, entry) {
344 if (!window_pane_visible(wp))
345 continue;
346 if (x < wp->xoff || x >= wp->xoff + wp->sx)
347 continue;
348 if (y < wp->yoff || y >= wp->yoff + wp->sy)
349 continue;
350 window_set_active_pane(w, wp);
351 break;
355 struct window_pane *
356 window_add_pane(struct window *w, u_int hlimit)
358 struct window_pane *wp;
360 wp = window_pane_create(w, w->sx, w->sy, hlimit);
361 if (TAILQ_EMPTY(&w->panes))
362 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
363 else
364 TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
365 return (wp);
368 void
369 window_remove_pane(struct window *w, struct window_pane *wp)
371 w->active = TAILQ_PREV(wp, window_panes, entry);
372 if (w->active == NULL)
373 w->active = TAILQ_NEXT(wp, entry);
375 TAILQ_REMOVE(&w->panes, wp, entry);
376 window_pane_destroy(wp);
379 struct window_pane *
380 window_pane_at_index(struct window *w, u_int idx)
382 struct window_pane *wp;
383 u_int n;
385 n = 0;
386 TAILQ_FOREACH(wp, &w->panes, entry) {
387 if (n == idx)
388 return (wp);
389 n++;
391 return (NULL);
394 struct window_pane *
395 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
397 for (; n > 0; n--) {
398 if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
399 wp = TAILQ_FIRST(&w->panes);
402 return (wp);
405 struct window_pane *
406 window_pane_previous_by_number(struct window *w, struct window_pane *wp,
407 u_int n)
409 for (; n > 0; n--) {
410 if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
411 wp = TAILQ_LAST(&w->panes, window_panes);
414 return (wp);
417 u_int
418 window_pane_index(struct window *w, struct window_pane *wp)
420 struct window_pane *wq;
421 u_int n;
423 n = 0;
424 TAILQ_FOREACH(wq, &w->panes, entry) {
425 if (wp == wq)
426 break;
427 n++;
429 return (n);
432 u_int
433 window_count_panes(struct window *w)
435 struct window_pane *wp;
436 u_int n;
438 n = 0;
439 TAILQ_FOREACH(wp, &w->panes, entry)
440 n++;
441 return (n);
444 void
445 window_destroy_panes(struct window *w)
447 struct window_pane *wp;
449 while (!TAILQ_EMPTY(&w->panes)) {
450 wp = TAILQ_FIRST(&w->panes);
451 TAILQ_REMOVE(&w->panes, wp, entry);
452 window_pane_destroy(wp);
456 struct window_pane *
457 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
459 struct window_pane *wp;
461 wp = xcalloc(1, sizeof *wp);
462 wp->window = w;
464 wp->cmd = NULL;
465 wp->shell = NULL;
466 wp->cwd = NULL;
468 wp->fd = -1;
469 wp->event = NULL;
471 wp->mode = NULL;
473 wp->layout_cell = NULL;
475 wp->xoff = 0;
476 wp->yoff = 0;
478 wp->sx = sx;
479 wp->sy = sy;
481 wp->pipe_fd = -1;
482 wp->pipe_off = 0;
483 wp->pipe_event = NULL;
485 wp->saved_grid = NULL;
487 screen_init(&wp->base, sx, sy, hlimit);
488 wp->screen = &wp->base;
490 input_init(wp);
492 return (wp);
495 void
496 window_pane_destroy(struct window_pane *wp)
498 if (wp->fd != -1) {
499 close(wp->fd);
500 bufferevent_free(wp->event);
503 input_free(wp);
505 window_pane_reset_mode(wp);
506 screen_free(&wp->base);
507 if (wp->saved_grid != NULL)
508 grid_destroy(wp->saved_grid);
510 if (wp->pipe_fd != -1) {
511 close(wp->pipe_fd);
512 bufferevent_free(wp->pipe_event);
515 if (wp->cwd != NULL)
516 xfree(wp->cwd);
517 if (wp->shell != NULL)
518 xfree(wp->shell);
519 if (wp->cmd != NULL)
520 xfree(wp->cmd);
521 xfree(wp);
525 window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
526 const char *cwd, struct environ *env, struct termios *tio, char **cause)
528 struct winsize ws;
529 int mode;
530 char *argv0;
531 const char *ptr;
532 struct termios tio2;
534 if (wp->fd != -1) {
535 close(wp->fd);
536 bufferevent_free(wp->event);
538 if (cmd != NULL) {
539 if (wp->cmd != NULL)
540 xfree(wp->cmd);
541 wp->cmd = xstrdup(cmd);
543 if (shell != NULL) {
544 if (wp->shell != NULL)
545 xfree(wp->shell);
546 wp->shell = xstrdup(shell);
548 if (cwd != NULL) {
549 if (wp->cwd != NULL)
550 xfree(wp->cwd);
551 wp->cwd = xstrdup(cwd);
554 memset(&ws, 0, sizeof ws);
555 ws.ws_col = screen_size_x(&wp->base);
556 ws.ws_row = screen_size_y(&wp->base);
558 switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
559 case -1:
560 wp->fd = -1;
561 xasprintf(cause, "%s: %s", cmd, strerror(errno));
562 return (-1);
563 case 0:
564 if (chdir(wp->cwd) != 0)
565 chdir("/");
567 if (tcgetattr(STDIN_FILENO, &tio2) != 0)
568 fatal("tcgetattr failed");
569 if (tio != NULL)
570 memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
571 tio2.c_cc[VERASE] = '\177';
572 if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
573 fatal("tcgetattr failed");
575 environ_push(env);
577 clear_signals();
578 log_close();
580 if (*wp->cmd != '\0') {
581 /* Set SHELL but only if it is currently not useful. */
582 shell = getenv("SHELL");
583 if (shell == NULL || *shell == '\0' || areshell(shell))
584 setenv("SHELL", wp->shell, 1);
586 execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
587 fatal("execl failed");
590 /* No command; fork a login shell. */
591 ptr = strrchr(wp->shell, '/');
592 if (ptr != NULL && *(ptr + 1) != '\0')
593 xasprintf(&argv0, "-%s", ptr + 1);
594 else
595 xasprintf(&argv0, "-%s", wp->shell);
596 setenv("SHELL", wp->shell, 1);
597 execl(wp->shell, argv0, (char *) NULL);
598 fatal("execl failed");
601 if ((mode = fcntl(wp->fd, F_GETFL)) == -1)
602 fatal("fcntl failed");
603 if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1)
604 fatal("fcntl failed");
605 if (fcntl(wp->fd, F_SETFD, FD_CLOEXEC) == -1)
606 fatal("fcntl failed");
607 wp->event = bufferevent_new(wp->fd,
608 window_pane_read_callback, NULL, window_pane_error_callback, wp);
609 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
611 return (0);
614 /* ARGSUSED */
615 void
616 window_pane_read_callback(unused struct bufferevent *bufev, void *data)
618 struct window_pane *wp = data;
619 char *new_data;
620 size_t new_size;
622 new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
623 if (wp->pipe_fd != -1 && new_size > 0) {
624 new_data = EVBUFFER_DATA(wp->event->input);
625 bufferevent_write(wp->pipe_event, new_data, new_size);
628 input_parse(wp);
630 wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
633 /* ARGSUSED */
634 void
635 window_pane_error_callback(
636 unused struct bufferevent *bufev, unused short what, void *data)
638 struct window_pane *wp = data;
640 server_destroy_pane(wp);
643 void
644 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
646 struct winsize ws;
648 if (sx == wp->sx && sy == wp->sy)
649 return;
650 wp->sx = sx;
651 wp->sy = sy;
653 memset(&ws, 0, sizeof ws);
654 ws.ws_col = sx;
655 ws.ws_row = sy;
657 screen_resize(&wp->base, sx, sy);
658 if (wp->mode != NULL)
659 wp->mode->resize(wp, sx, sy);
661 if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
662 fatal("ioctl failed");
666 * Enter alternative screen mode. A copy of the visible screen is saved and the
667 * history is not updated
669 void
670 window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc)
672 struct screen *s = &wp->base;
673 u_int sx, sy;
675 if (wp->saved_grid != NULL)
676 return;
677 if (!options_get_number(&wp->window->options, "alternate-screen"))
678 return;
679 sx = screen_size_x(s);
680 sy = screen_size_y(s);
682 wp->saved_grid = grid_create(sx, sy, 0);
683 grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
684 wp->saved_cx = s->cx;
685 wp->saved_cy = s->cy;
686 memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
688 grid_view_clear(s->grid, 0, 0, sx, sy);
690 wp->base.grid->flags &= ~GRID_HISTORY;
692 wp->flags |= PANE_REDRAW;
695 /* Exit alternate screen mode and restore the copied grid. */
696 void
697 window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc)
699 struct screen *s = &wp->base;
700 u_int sx, sy;
702 if (wp->saved_grid == NULL)
703 return;
704 if (!options_get_number(&wp->window->options, "alternate-screen"))
705 return;
706 sx = screen_size_x(s);
707 sy = screen_size_y(s);
710 * If the current size is bigger, temporarily resize to the old size
711 * before copying back.
713 if (sy > wp->saved_grid->sy)
714 screen_resize(s, sx, wp->saved_grid->sy);
716 /* Restore the grid, cursor position and cell. */
717 grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
718 s->cx = wp->saved_cx;
719 if (s->cx > screen_size_x(s) - 1)
720 s->cx = screen_size_x(s) - 1;
721 s->cy = wp->saved_cy;
722 if (s->cy > screen_size_y(s) - 1)
723 s->cy = screen_size_y(s) - 1;
724 memcpy(gc, &wp->saved_cell, sizeof *gc);
727 * Turn history back on (so resize can use it) and then resize back to
728 * the current size.
730 wp->base.grid->flags |= GRID_HISTORY;
731 if (sy > wp->saved_grid->sy)
732 screen_resize(s, sx, sy);
734 grid_destroy(wp->saved_grid);
735 wp->saved_grid = NULL;
737 wp->flags |= PANE_REDRAW;
741 window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
743 struct screen *s;
745 if (wp->mode != NULL)
746 return (1);
747 wp->mode = mode;
749 if ((s = wp->mode->init(wp)) != NULL)
750 wp->screen = s;
751 wp->flags |= PANE_REDRAW;
752 return (0);
755 void
756 window_pane_reset_mode(struct window_pane *wp)
758 if (wp->mode == NULL)
759 return;
761 wp->mode->free(wp);
762 wp->mode = NULL;
764 wp->screen = &wp->base;
765 wp->flags |= PANE_REDRAW;
768 void
769 window_pane_key(struct window_pane *wp, struct session *sess, int key)
771 struct window_pane *wp2;
773 if (!window_pane_visible(wp))
774 return;
776 if (wp->mode != NULL) {
777 if (wp->mode->key != NULL)
778 wp->mode->key(wp, sess, key);
779 return;
782 if (wp->fd == -1)
783 return;
784 input_key(wp, key);
785 if (options_get_number(&wp->window->options, "synchronize-panes")) {
786 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
787 if (wp2 == wp || wp2->mode != NULL)
788 continue;
789 if (wp2->fd != -1 && window_pane_visible(wp2))
790 input_key(wp2, key);
795 void
796 window_pane_mouse(
797 struct window_pane *wp, struct session *sess, struct mouse_event *m)
799 if (!window_pane_visible(wp))
800 return;
802 if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
803 return;
804 if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
805 return;
806 m->x -= wp->xoff;
807 m->y -= wp->yoff;
809 if (wp->mode != NULL) {
810 if (wp->mode->mouse != NULL)
811 wp->mode->mouse(wp, sess, m);
812 } else if (wp->fd != -1)
813 input_mouse(wp, m);
817 window_pane_visible(struct window_pane *wp)
819 struct window *w = wp->window;
821 if (wp->xoff >= w->sx || wp->yoff >= w->sy)
822 return (0);
823 if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
824 return (0);
825 return (1);
828 char *
829 window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
831 struct screen *s = &wp->base;
832 char *newsearchstr, *line, *msg;
833 u_int i;
835 msg = NULL;
836 xasprintf(&newsearchstr, "*%s*", searchstr);
838 for (i = 0; i < screen_size_y(s); i++) {
839 line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
840 if (fnmatch(newsearchstr, line, 0) == 0) {
841 msg = line;
842 if (lineno != NULL)
843 *lineno = i;
844 break;
846 xfree(line);
849 xfree(newsearchstr);
850 return (msg);
853 /* Find the pane directly above another. */
854 struct window_pane *
855 window_pane_find_up(struct window_pane *wp)
857 struct window_pane *wp2;
858 u_int left, top;
860 if (wp == NULL || !window_pane_visible(wp))
861 return (NULL);
863 top = wp->yoff;
864 if (top == 0)
865 top = wp->window->sy + 1;
866 left = wp->xoff;
868 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
869 if (!window_pane_visible(wp2))
870 continue;
871 if (wp2->yoff + wp2->sy + 1 != top)
872 continue;
873 if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
874 return (wp2);
876 return (NULL);
879 /* Find the pane directly below another. */
880 struct window_pane *
881 window_pane_find_down(struct window_pane *wp)
883 struct window_pane *wp2;
884 u_int left, bottom;
886 if (wp == NULL || !window_pane_visible(wp))
887 return (NULL);
889 bottom = wp->yoff + wp->sy + 1;
890 if (bottom >= wp->window->sy)
891 bottom = 0;
892 left = wp->xoff;
894 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
895 if (!window_pane_visible(wp2))
896 continue;
897 if (wp2->yoff != bottom)
898 continue;
899 if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
900 return (wp2);
902 return (NULL);
906 * Find the pane directly to the left of another, adjacent to the left side and
907 * containing the top edge.
909 struct window_pane *
910 window_pane_find_left(struct window_pane *wp)
912 struct window_pane *wp2;
913 u_int left, top;
915 if (wp == NULL || !window_pane_visible(wp))
916 return (NULL);
918 left = wp->xoff;
919 if (left == 0)
920 left = wp->window->sx + 1;
921 top = wp->yoff;
923 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
924 if (!window_pane_visible(wp2))
925 continue;
926 if (wp2->xoff + wp2->sx + 1 != left)
927 continue;
928 if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
929 return (wp2);
931 return (NULL);
935 * Find the pane directly to the right of another, that is adjacent to the
936 * right edge and including the top edge.
938 struct window_pane *
939 window_pane_find_right(struct window_pane *wp)
941 struct window_pane *wp2;
942 u_int right, top;
944 if (wp == NULL || !window_pane_visible(wp))
945 return (NULL);
947 right = wp->xoff + wp->sx + 1;
948 if (right >= wp->window->sx)
949 right = 0;
950 top = wp->yoff;
952 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
953 if (!window_pane_visible(wp2))
954 continue;
955 if (wp2->xoff != right)
956 continue;
957 if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
958 return (wp2);
960 return (NULL);