Drop the ability to have a list of keys in the prefix in favour of two
[tmux-openbsd.git] / window.c
blobf46e302f3bd0f1a62da6e41a8c5412bbfca75feb
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 /* Global panes tree. */
60 struct window_pane_tree all_window_panes;
61 u_int next_window_pane;
63 void window_pane_read_callback(struct bufferevent *, void *);
64 void window_pane_error_callback(struct bufferevent *, short, void *);
66 RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
68 int
69 winlink_cmp(struct winlink *wl1, struct winlink *wl2)
71 return (wl1->idx - wl2->idx);
74 RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
76 int
77 window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
79 return (wp1->id - wp2->id);
82 struct winlink *
83 winlink_find_by_window(struct winlinks *wwl, struct window *w)
85 struct winlink *wl;
87 RB_FOREACH(wl, winlinks, wwl) {
88 if (wl->window == w)
89 return (wl);
92 return (NULL);
95 struct winlink *
96 winlink_find_by_index(struct winlinks *wwl, int idx)
98 struct winlink wl;
100 if (idx < 0)
101 fatalx("bad index");
103 wl.idx = idx;
104 return (RB_FIND(winlinks, wwl, &wl));
108 winlink_next_index(struct winlinks *wwl, int idx)
110 int i;
112 i = idx;
113 do {
114 if (winlink_find_by_index(wwl, i) == NULL)
115 return (i);
116 if (i == INT_MAX)
117 i = 0;
118 else
119 i++;
120 } while (i != idx);
121 return (-1);
124 u_int
125 winlink_count(struct winlinks *wwl)
127 struct winlink *wl;
128 u_int n;
130 n = 0;
131 RB_FOREACH(wl, winlinks, wwl)
132 n++;
134 return (n);
137 struct winlink *
138 winlink_add(struct winlinks *wwl, int idx)
140 struct winlink *wl;
142 if (idx < 0) {
143 if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
144 return (NULL);
145 } else if (winlink_find_by_index(wwl, idx) != NULL)
146 return (NULL);
148 wl = xcalloc(1, sizeof *wl);
149 wl->idx = idx;
150 RB_INSERT(winlinks, wwl, wl);
152 return (wl);
155 void
156 winlink_set_window(struct winlink *wl, struct window *w)
158 wl->window = w;
159 w->references++;
162 void
163 winlink_remove(struct winlinks *wwl, struct winlink *wl)
165 struct window *w = wl->window;
167 RB_REMOVE(winlinks, wwl, wl);
168 if (wl->status_text != NULL)
169 xfree(wl->status_text);
170 xfree(wl);
172 if (w != NULL) {
173 if (w->references == 0)
174 fatal("bad reference count");
175 w->references--;
176 if (w->references == 0)
177 window_destroy(w);
181 struct winlink *
182 winlink_next(struct winlink *wl)
184 return (RB_NEXT(winlinks, wwl, wl));
187 struct winlink *
188 winlink_previous(struct winlink *wl)
190 return (RB_PREV(winlinks, wwl, wl));
193 struct winlink *
194 winlink_next_by_number(struct winlink *wl, struct session *s, int n)
196 for (; n > 0; n--) {
197 if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
198 wl = RB_MIN(winlinks, &s->windows);
201 return (wl);
204 struct winlink *
205 winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
207 for (; n > 0; n--) {
208 if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
209 wl = RB_MAX(winlinks, &s->windows);
212 return (wl);
215 void
216 winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
218 if (wl == NULL)
219 return;
221 winlink_stack_remove(stack, wl);
222 TAILQ_INSERT_HEAD(stack, wl, sentry);
225 void
226 winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
228 struct winlink *wl2;
230 if (wl == NULL)
231 return;
233 TAILQ_FOREACH(wl2, stack, sentry) {
234 if (wl2 == wl) {
235 TAILQ_REMOVE(stack, wl, sentry);
236 return;
242 window_index(struct window *s, u_int *i)
244 for (*i = 0; *i < ARRAY_LENGTH(&windows); (*i)++) {
245 if (s == ARRAY_ITEM(&windows, *i))
246 return (0);
248 return (-1);
251 struct window *
252 window_create1(u_int sx, u_int sy)
254 struct window *w;
255 u_int i;
257 w = xcalloc(1, sizeof *w);
258 w->name = NULL;
259 w->flags = 0;
261 TAILQ_INIT(&w->panes);
262 w->active = NULL;
264 w->lastlayout = -1;
265 w->layout_root = NULL;
267 w->sx = sx;
268 w->sy = sy;
270 queue_window_name(w);
272 options_init(&w->options, &global_w_options);
274 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
275 if (ARRAY_ITEM(&windows, i) == NULL) {
276 ARRAY_SET(&windows, i, w);
277 break;
280 if (i == ARRAY_LENGTH(&windows))
281 ARRAY_ADD(&windows, w);
282 w->references = 0;
284 return (w);
287 struct window *
288 window_create(const char *name, const char *cmd, const char *shell,
289 const char *cwd, struct environ *env, struct termios *tio,
290 u_int sx, u_int sy, u_int hlimit,char **cause)
292 struct window *w;
293 struct window_pane *wp;
295 w = window_create1(sx, sy);
296 wp = window_add_pane(w, hlimit);
297 layout_init(w);
298 if (window_pane_spawn(wp, cmd, shell, cwd, env, tio, cause) != 0) {
299 window_destroy(w);
300 return (NULL);
302 w->active = TAILQ_FIRST(&w->panes);
303 if (name != NULL) {
304 w->name = xstrdup(name);
305 options_set_number(&w->options, "automatic-rename", 0);
306 } else
307 w->name = default_window_name(w);
308 return (w);
311 void
312 window_destroy(struct window *w)
314 u_int i;
316 if (window_index(w, &i) != 0)
317 fatalx("index not found");
318 ARRAY_SET(&windows, i, NULL);
319 while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
320 ARRAY_TRUNC(&windows, 1);
322 if (w->layout_root != NULL)
323 layout_free(w);
325 evtimer_del(&w->name_timer);
327 options_free(&w->options);
329 window_destroy_panes(w);
331 if (w->name != NULL)
332 xfree(w->name);
333 xfree(w);
336 void
337 window_resize(struct window *w, u_int sx, u_int sy)
339 w->sx = sx;
340 w->sy = sy;
343 void
344 window_set_active_pane(struct window *w, struct window_pane *wp)
346 if (wp == w->active)
347 return;
348 w->last = w->active;
349 w->active = wp;
350 while (!window_pane_visible(w->active)) {
351 w->active = TAILQ_PREV(w->active, window_panes, entry);
352 if (w->active == NULL)
353 w->active = TAILQ_LAST(&w->panes, window_panes);
354 if (w->active == wp)
355 return;
359 struct window_pane *
360 window_get_active_at(struct window *w, u_int x, u_int y)
362 struct window_pane *wp;
364 TAILQ_FOREACH(wp, &w->panes, entry) {
365 if (!window_pane_visible(wp))
366 continue;
367 if (x < wp->xoff || x > wp->xoff + wp->sx)
368 continue;
369 if (y < wp->yoff || y > wp->yoff + wp->sy)
370 continue;
371 return (wp);
373 return (NULL);
376 void
377 window_set_active_at(struct window *w, u_int x, u_int y)
379 struct window_pane *wp;
381 wp = window_get_active_at(w, x, y);
382 if (wp != NULL && wp != w->active)
383 window_set_active_pane(w, wp);
386 struct window_pane *
387 window_find_string(struct window *w, const char *s)
389 u_int x, y;
391 x = w->sx / 2;
392 y = w->sy / 2;
394 if (strcasecmp(s, "top") == 0)
395 y = 0;
396 else if (strcasecmp(s, "bottom") == 0)
397 y = w->sy - 1;
398 else if (strcasecmp(s, "left") == 0)
399 x = 0;
400 else if (strcasecmp(s, "right") == 0)
401 x = w->sx - 1;
402 else if (strcasecmp(s, "top-left") == 0) {
403 x = 0;
404 y = 0;
405 } else if (strcasecmp(s, "top-right") == 0) {
406 x = w->sx - 1;
407 y = 0;
408 } else if (strcasecmp(s, "bottom-left") == 0) {
409 x = 0;
410 y = w->sy - 1;
411 } else if (strcasecmp(s, "bottom-right") == 0) {
412 x = w->sx - 1;
413 y = w->sy - 1;
414 } else
415 return (NULL);
417 return (window_get_active_at(w, x, y));
420 struct window_pane *
421 window_add_pane(struct window *w, u_int hlimit)
423 struct window_pane *wp;
425 wp = window_pane_create(w, w->sx, w->sy, hlimit);
426 if (TAILQ_EMPTY(&w->panes))
427 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
428 else
429 TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
430 return (wp);
433 void
434 window_remove_pane(struct window *w, struct window_pane *wp)
436 if (wp == w->active) {
437 w->active = w->last;
438 w->last = NULL;
439 if (w->active == NULL) {
440 w->active = TAILQ_PREV(wp, window_panes, entry);
441 if (w->active == NULL)
442 w->active = TAILQ_NEXT(wp, entry);
444 } else if (wp == w->last)
445 w->last = NULL;
447 TAILQ_REMOVE(&w->panes, wp, entry);
448 window_pane_destroy(wp);
451 struct window_pane *
452 window_pane_at_index(struct window *w, u_int idx)
454 struct window_pane *wp;
455 u_int n;
457 n = options_get_number(&w->options, "pane-base-index");
458 TAILQ_FOREACH(wp, &w->panes, entry) {
459 if (n == idx)
460 return (wp);
461 n++;
463 return (NULL);
466 struct window_pane *
467 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
469 for (; n > 0; n--) {
470 if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
471 wp = TAILQ_FIRST(&w->panes);
474 return (wp);
477 struct window_pane *
478 window_pane_previous_by_number(struct window *w, struct window_pane *wp,
479 u_int n)
481 for (; n > 0; n--) {
482 if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
483 wp = TAILQ_LAST(&w->panes, window_panes);
486 return (wp);
490 window_pane_index(struct window_pane *wp, u_int *i)
492 struct window_pane *wq;
493 struct window *w = wp->window;
495 *i = options_get_number(&w->options, "pane-base-index");
496 TAILQ_FOREACH(wq, &w->panes, entry) {
497 if (wp == wq) {
498 return (0);
500 (*i)++;
503 return (-1);
506 u_int
507 window_count_panes(struct window *w)
509 struct window_pane *wp;
510 u_int n;
512 n = 0;
513 TAILQ_FOREACH(wp, &w->panes, entry)
514 n++;
515 return (n);
518 void
519 window_destroy_panes(struct window *w)
521 struct window_pane *wp;
523 while (!TAILQ_EMPTY(&w->panes)) {
524 wp = TAILQ_FIRST(&w->panes);
525 TAILQ_REMOVE(&w->panes, wp, entry);
526 window_pane_destroy(wp);
530 /* Return list of printable window flag symbols. No flags is just a space. */
531 char *
532 window_printable_flags(struct session *s, struct winlink *wl)
534 char flags[BUFSIZ];
535 int pos;
537 pos = 0;
538 if (wl->flags & WINLINK_ACTIVITY)
539 flags[pos++] = '#';
540 if (wl->flags & WINLINK_BELL)
541 flags[pos++] = '!';
542 if (wl->flags & WINLINK_CONTENT)
543 flags[pos++] = '+';
544 if (wl->flags & WINLINK_SILENCE)
545 flags[pos++] = '~';
546 if (wl == s->curw)
547 flags[pos++] = '*';
548 if (wl == TAILQ_FIRST(&s->lastw))
549 flags[pos++] = '-';
550 if (pos == 0)
551 flags[pos++] = ' ';
552 flags[pos] = '\0';
553 return (xstrdup(flags));
556 /* Find pane in global tree by id. */
557 struct window_pane *
558 window_pane_find_by_id(u_int id)
560 struct window_pane wp;
562 wp.id = id;
563 return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
566 struct window_pane *
567 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
569 struct window_pane *wp;
571 wp = xcalloc(1, sizeof *wp);
572 wp->window = w;
574 wp->id = next_window_pane++;
575 RB_INSERT(window_pane_tree, &all_window_panes, wp);
577 wp->cmd = NULL;
578 wp->shell = NULL;
579 wp->cwd = NULL;
581 wp->fd = -1;
582 wp->event = NULL;
584 wp->mode = NULL;
586 wp->layout_cell = NULL;
588 wp->xoff = 0;
589 wp->yoff = 0;
591 wp->sx = sx;
592 wp->sy = sy;
594 wp->pipe_fd = -1;
595 wp->pipe_off = 0;
596 wp->pipe_event = NULL;
598 wp->saved_grid = NULL;
600 screen_init(&wp->base, sx, sy, hlimit);
601 wp->screen = &wp->base;
603 input_init(wp);
605 return (wp);
608 void
609 window_pane_destroy(struct window_pane *wp)
611 window_pane_reset_mode(wp);
613 if (wp->fd != -1) {
614 close(wp->fd);
615 bufferevent_free(wp->event);
618 input_free(wp);
620 screen_free(&wp->base);
621 if (wp->saved_grid != NULL)
622 grid_destroy(wp->saved_grid);
624 if (wp->pipe_fd != -1) {
625 close(wp->pipe_fd);
626 bufferevent_free(wp->pipe_event);
629 RB_REMOVE(window_pane_tree, &all_window_panes, wp);
631 if (wp->cwd != NULL)
632 xfree(wp->cwd);
633 if (wp->shell != NULL)
634 xfree(wp->shell);
635 if (wp->cmd != NULL)
636 xfree(wp->cmd);
637 xfree(wp);
641 window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
642 const char *cwd, struct environ *env, struct termios *tio, char **cause)
644 struct winsize ws;
645 char *argv0, paneid[16];
646 const char *ptr;
647 struct termios tio2;
649 if (wp->fd != -1) {
650 close(wp->fd);
651 bufferevent_free(wp->event);
653 if (cmd != NULL) {
654 if (wp->cmd != NULL)
655 xfree(wp->cmd);
656 wp->cmd = xstrdup(cmd);
658 if (shell != NULL) {
659 if (wp->shell != NULL)
660 xfree(wp->shell);
661 wp->shell = xstrdup(shell);
663 if (cwd != NULL) {
664 if (wp->cwd != NULL)
665 xfree(wp->cwd);
666 wp->cwd = xstrdup(cwd);
669 memset(&ws, 0, sizeof ws);
670 ws.ws_col = screen_size_x(&wp->base);
671 ws.ws_row = screen_size_y(&wp->base);
673 switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
674 case -1:
675 wp->fd = -1;
676 xasprintf(cause, "%s: %s", cmd, strerror(errno));
677 return (-1);
678 case 0:
679 if (chdir(wp->cwd) != 0)
680 chdir("/");
682 if (tcgetattr(STDIN_FILENO, &tio2) != 0)
683 fatal("tcgetattr failed");
684 if (tio != NULL)
685 memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
686 tio2.c_cc[VERASE] = '\177';
687 if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
688 fatal("tcgetattr failed");
690 closefrom(STDERR_FILENO + 1);
692 xsnprintf(paneid, sizeof paneid, "%%%u", wp->id);
693 environ_set(env, "TMUX_PANE", paneid);
694 environ_push(env);
696 clear_signals(1);
697 log_close();
699 if (*wp->cmd != '\0') {
700 /* Set SHELL but only if it is currently not useful. */
701 shell = getenv("SHELL");
702 if (checkshell(shell))
703 setenv("SHELL", wp->shell, 1);
705 execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
706 fatal("execl failed");
709 /* No command; fork a login shell. */
710 ptr = strrchr(wp->shell, '/');
711 if (ptr != NULL && *(ptr + 1) != '\0')
712 xasprintf(&argv0, "-%s", ptr + 1);
713 else
714 xasprintf(&argv0, "-%s", wp->shell);
715 setenv("SHELL", wp->shell, 1);
716 execl(wp->shell, argv0, (char *) NULL);
717 fatal("execl failed");
720 setblocking(wp->fd, 0);
722 wp->event = bufferevent_new(wp->fd,
723 window_pane_read_callback, NULL, window_pane_error_callback, wp);
724 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
726 return (0);
729 /* ARGSUSED */
730 void
731 window_pane_read_callback(unused struct bufferevent *bufev, void *data)
733 struct window_pane *wp = data;
734 char *new_data;
735 size_t new_size;
737 new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
738 if (wp->pipe_fd != -1 && new_size > 0) {
739 new_data = EVBUFFER_DATA(wp->event->input);
740 bufferevent_write(wp->pipe_event, new_data, new_size);
743 input_parse(wp);
745 wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
748 * If we get here, we're not outputting anymore, so set the silence
749 * flag on the window.
751 wp->window->flags |= WINDOW_SILENCE;
752 if (gettimeofday(&wp->window->silence_timer, NULL) != 0)
753 fatal("gettimeofday failed.");
756 /* ARGSUSED */
757 void
758 window_pane_error_callback(
759 unused struct bufferevent *bufev, unused short what, void *data)
761 struct window_pane *wp = data;
763 server_destroy_pane(wp);
766 void
767 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
769 struct winsize ws;
771 if (sx == wp->sx && sy == wp->sy)
772 return;
773 wp->sx = sx;
774 wp->sy = sy;
776 memset(&ws, 0, sizeof ws);
777 ws.ws_col = sx;
778 ws.ws_row = sy;
780 screen_resize(&wp->base, sx, sy);
781 if (wp->mode != NULL)
782 wp->mode->resize(wp, sx, sy);
784 if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
785 fatal("ioctl failed");
789 * Enter alternative screen mode. A copy of the visible screen is saved and the
790 * history is not updated
792 void
793 window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc)
795 struct screen *s = &wp->base;
796 u_int sx, sy;
798 if (wp->saved_grid != NULL)
799 return;
800 if (!options_get_number(&wp->window->options, "alternate-screen"))
801 return;
802 sx = screen_size_x(s);
803 sy = screen_size_y(s);
805 wp->saved_grid = grid_create(sx, sy, 0);
806 grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
807 wp->saved_cx = s->cx;
808 wp->saved_cy = s->cy;
809 memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
811 grid_view_clear(s->grid, 0, 0, sx, sy);
813 wp->base.grid->flags &= ~GRID_HISTORY;
815 wp->flags |= PANE_REDRAW;
818 /* Exit alternate screen mode and restore the copied grid. */
819 void
820 window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc)
822 struct screen *s = &wp->base;
823 u_int sx, sy;
825 if (wp->saved_grid == NULL)
826 return;
827 if (!options_get_number(&wp->window->options, "alternate-screen"))
828 return;
829 sx = screen_size_x(s);
830 sy = screen_size_y(s);
833 * If the current size is bigger, temporarily resize to the old size
834 * before copying back.
836 if (sy > wp->saved_grid->sy)
837 screen_resize(s, sx, wp->saved_grid->sy);
839 /* Restore the grid, cursor position and cell. */
840 grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
841 s->cx = wp->saved_cx;
842 if (s->cx > screen_size_x(s) - 1)
843 s->cx = screen_size_x(s) - 1;
844 s->cy = wp->saved_cy;
845 if (s->cy > screen_size_y(s) - 1)
846 s->cy = screen_size_y(s) - 1;
847 memcpy(gc, &wp->saved_cell, sizeof *gc);
850 * Turn history back on (so resize can use it) and then resize back to
851 * the current size.
853 wp->base.grid->flags |= GRID_HISTORY;
854 if (sy > wp->saved_grid->sy)
855 screen_resize(s, sx, sy);
857 grid_destroy(wp->saved_grid);
858 wp->saved_grid = NULL;
860 wp->flags |= PANE_REDRAW;
864 window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
866 struct screen *s;
868 if (wp->mode != NULL)
869 return (1);
870 wp->mode = mode;
872 if ((s = wp->mode->init(wp)) != NULL)
873 wp->screen = s;
874 wp->flags |= PANE_REDRAW;
875 return (0);
878 void
879 window_pane_reset_mode(struct window_pane *wp)
881 if (wp->mode == NULL)
882 return;
884 wp->mode->free(wp);
885 wp->mode = NULL;
887 wp->screen = &wp->base;
888 wp->flags |= PANE_REDRAW;
891 void
892 window_pane_key(struct window_pane *wp, struct session *sess, int key)
894 struct window_pane *wp2;
896 if (!window_pane_visible(wp))
897 return;
899 if (wp->mode != NULL) {
900 if (wp->mode->key != NULL)
901 wp->mode->key(wp, sess, key);
902 return;
905 if (wp->fd == -1)
906 return;
907 input_key(wp, key);
908 if (options_get_number(&wp->window->options, "synchronize-panes")) {
909 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
910 if (wp2 == wp || wp2->mode != NULL)
911 continue;
912 if (wp2->fd != -1 && window_pane_visible(wp2))
913 input_key(wp2, key);
918 void
919 window_pane_mouse(
920 struct window_pane *wp, struct session *sess, struct mouse_event *m)
922 if (!window_pane_visible(wp))
923 return;
925 if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
926 return;
927 if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
928 return;
929 m->x -= wp->xoff;
930 m->y -= wp->yoff;
932 if (wp->mode != NULL) {
933 if (wp->mode->mouse != NULL &&
934 options_get_number(&wp->window->options, "mode-mouse"))
935 wp->mode->mouse(wp, sess, m);
936 } else if (wp->fd != -1)
937 input_mouse(wp, m);
941 window_pane_visible(struct window_pane *wp)
943 struct window *w = wp->window;
945 if (wp->xoff >= w->sx || wp->yoff >= w->sy)
946 return (0);
947 if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
948 return (0);
949 return (1);
952 char *
953 window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
955 struct screen *s = &wp->base;
956 char *newsearchstr, *line, *msg;
957 u_int i;
959 msg = NULL;
960 xasprintf(&newsearchstr, "*%s*", searchstr);
962 for (i = 0; i < screen_size_y(s); i++) {
963 line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
964 if (fnmatch(newsearchstr, line, 0) == 0) {
965 msg = line;
966 if (lineno != NULL)
967 *lineno = i;
968 break;
970 xfree(line);
973 xfree(newsearchstr);
974 return (msg);
977 /* Find the pane directly above another. */
978 struct window_pane *
979 window_pane_find_up(struct window_pane *wp)
981 struct window_pane *wp2;
982 u_int left, top;
984 if (wp == NULL || !window_pane_visible(wp))
985 return (NULL);
987 top = wp->yoff;
988 if (top == 0)
989 top = wp->window->sy + 1;
990 left = wp->xoff;
992 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
993 if (!window_pane_visible(wp2))
994 continue;
995 if (wp2->yoff + wp2->sy + 1 != top)
996 continue;
997 if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
998 return (wp2);
1000 return (NULL);
1003 /* Find the pane directly below another. */
1004 struct window_pane *
1005 window_pane_find_down(struct window_pane *wp)
1007 struct window_pane *wp2;
1008 u_int left, bottom;
1010 if (wp == NULL || !window_pane_visible(wp))
1011 return (NULL);
1013 bottom = wp->yoff + wp->sy + 1;
1014 if (bottom >= wp->window->sy)
1015 bottom = 0;
1016 left = wp->xoff;
1018 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1019 if (!window_pane_visible(wp2))
1020 continue;
1021 if (wp2->yoff != bottom)
1022 continue;
1023 if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
1024 return (wp2);
1026 return (NULL);
1030 * Find the pane directly to the left of another, adjacent to the left side and
1031 * containing the top edge.
1033 struct window_pane *
1034 window_pane_find_left(struct window_pane *wp)
1036 struct window_pane *wp2;
1037 u_int left, top;
1039 if (wp == NULL || !window_pane_visible(wp))
1040 return (NULL);
1042 left = wp->xoff;
1043 if (left == 0)
1044 left = wp->window->sx + 1;
1045 top = wp->yoff;
1047 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1048 if (!window_pane_visible(wp2))
1049 continue;
1050 if (wp2->xoff + wp2->sx + 1 != left)
1051 continue;
1052 if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
1053 return (wp2);
1055 return (NULL);
1059 * Find the pane directly to the right of another, that is adjacent to the
1060 * right edge and including the top edge.
1062 struct window_pane *
1063 window_pane_find_right(struct window_pane *wp)
1065 struct window_pane *wp2;
1066 u_int right, top;
1068 if (wp == NULL || !window_pane_visible(wp))
1069 return (NULL);
1071 right = wp->xoff + wp->sx + 1;
1072 if (right >= wp->window->sx)
1073 right = 0;
1074 top = wp->yoff;
1076 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1077 if (!window_pane_visible(wp2))
1078 continue;
1079 if (wp2->xoff != right)
1080 continue;
1081 if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
1082 return (wp2);
1084 return (NULL);