Merge branch 'obsd-master'
[tmux.git] / window.c
blob1f5cdc758c9386b632d3a6913f291572757f2e8a
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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 <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>
34 #include "tmux.h"
37 * Each window is attached to a number of panes, each of which is a pty. This
38 * file contains code to handle them.
40 * A pane has two buffers attached, these are filled and emptied by the main
41 * server poll loop. Output data is received from pty's in screen format,
42 * translated and returned as a series of escape sequences and strings via
43 * input_parse (in input.c). Input data is received as key codes and written
44 * directly via input_key.
46 * Each pane also has a "virtual" screen (screen.c) which contains the current
47 * state and is redisplayed when the window is reattached to a client.
49 * Windows are stored directly on a global array and wrapped in any number of
50 * winlink structs to be linked onto local session RB trees. A reference count
51 * is maintained and a window removed from the global list and destroyed when
52 * it reaches zero.
55 /* Global window list. */
56 struct windows windows;
58 /* Global panes tree. */
59 struct window_pane_tree all_window_panes;
60 static u_int next_window_pane_id;
61 static u_int next_window_id;
62 static u_int next_active_point;
64 struct window_pane_input_data {
65 struct cmdq_item *item;
66 u_int wp;
67 struct client_file *file;
70 static struct window_pane *window_pane_create(struct window *, u_int, u_int,
71 u_int);
72 static void window_pane_destroy(struct window_pane *);
74 RB_GENERATE(windows, window, entry, window_cmp);
75 RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
76 RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
78 int
79 window_cmp(struct window *w1, struct window *w2)
81 return (w1->id - w2->id);
84 int
85 winlink_cmp(struct winlink *wl1, struct winlink *wl2)
87 return (wl1->idx - wl2->idx);
90 int
91 window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
93 return (wp1->id - wp2->id);
96 struct winlink *
97 winlink_find_by_window(struct winlinks *wwl, struct window *w)
99 struct winlink *wl;
101 RB_FOREACH(wl, winlinks, wwl) {
102 if (wl->window == w)
103 return (wl);
106 return (NULL);
109 struct winlink *
110 winlink_find_by_index(struct winlinks *wwl, int idx)
112 struct winlink wl;
114 if (idx < 0)
115 fatalx("bad index");
117 wl.idx = idx;
118 return (RB_FIND(winlinks, wwl, &wl));
121 struct winlink *
122 winlink_find_by_window_id(struct winlinks *wwl, u_int id)
124 struct winlink *wl;
126 RB_FOREACH(wl, winlinks, wwl) {
127 if (wl->window->id == id)
128 return (wl);
130 return (NULL);
133 static int
134 winlink_next_index(struct winlinks *wwl, int idx)
136 int i;
138 i = idx;
139 do {
140 if (winlink_find_by_index(wwl, i) == NULL)
141 return (i);
142 if (i == INT_MAX)
143 i = 0;
144 else
145 i++;
146 } while (i != idx);
147 return (-1);
150 u_int
151 winlink_count(struct winlinks *wwl)
153 struct winlink *wl;
154 u_int n;
156 n = 0;
157 RB_FOREACH(wl, winlinks, wwl)
158 n++;
160 return (n);
163 struct winlink *
164 winlink_add(struct winlinks *wwl, int idx)
166 struct winlink *wl;
168 if (idx < 0) {
169 if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
170 return (NULL);
171 } else if (winlink_find_by_index(wwl, idx) != NULL)
172 return (NULL);
174 wl = xcalloc(1, sizeof *wl);
175 wl->idx = idx;
176 RB_INSERT(winlinks, wwl, wl);
178 return (wl);
181 void
182 winlink_set_window(struct winlink *wl, struct window *w)
184 if (wl->window != NULL) {
185 TAILQ_REMOVE(&wl->window->winlinks, wl, wentry);
186 window_remove_ref(wl->window, __func__);
188 TAILQ_INSERT_TAIL(&w->winlinks, wl, wentry);
189 wl->window = w;
190 window_add_ref(w, __func__);
193 void
194 winlink_remove(struct winlinks *wwl, struct winlink *wl)
196 struct window *w = wl->window;
198 if (w != NULL) {
199 TAILQ_REMOVE(&w->winlinks, wl, wentry);
200 window_remove_ref(w, __func__);
203 RB_REMOVE(winlinks, wwl, wl);
204 free(wl);
207 struct winlink *
208 winlink_next(struct winlink *wl)
210 return (RB_NEXT(winlinks, wwl, wl));
213 struct winlink *
214 winlink_previous(struct winlink *wl)
216 return (RB_PREV(winlinks, wwl, wl));
219 struct winlink *
220 winlink_next_by_number(struct winlink *wl, struct session *s, int n)
222 for (; n > 0; n--) {
223 if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
224 wl = RB_MIN(winlinks, &s->windows);
227 return (wl);
230 struct winlink *
231 winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
233 for (; n > 0; n--) {
234 if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
235 wl = RB_MAX(winlinks, &s->windows);
238 return (wl);
241 void
242 winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
244 if (wl == NULL)
245 return;
247 winlink_stack_remove(stack, wl);
248 TAILQ_INSERT_HEAD(stack, wl, sentry);
249 wl->flags |= WINLINK_VISITED;
252 void
253 winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
255 if (wl != NULL && (wl->flags & WINLINK_VISITED)) {
256 TAILQ_REMOVE(stack, wl, sentry);
257 wl->flags &= ~WINLINK_VISITED;
261 struct window *
262 window_find_by_id_str(const char *s)
264 const char *errstr;
265 u_int id;
267 if (*s != '@')
268 return (NULL);
270 id = strtonum(s + 1, 0, UINT_MAX, &errstr);
271 if (errstr != NULL)
272 return (NULL);
273 return (window_find_by_id(id));
276 struct window *
277 window_find_by_id(u_int id)
279 struct window w;
281 w.id = id;
282 return (RB_FIND(windows, &windows, &w));
285 void
286 window_update_activity(struct window *w)
288 gettimeofday(&w->activity_time, NULL);
289 alerts_queue(w, WINDOW_ACTIVITY);
292 struct window *
293 window_create(u_int sx, u_int sy, u_int xpixel, u_int ypixel)
295 struct window *w;
297 if (xpixel == 0)
298 xpixel = DEFAULT_XPIXEL;
299 if (ypixel == 0)
300 ypixel = DEFAULT_YPIXEL;
302 w = xcalloc(1, sizeof *w);
303 w->name = xstrdup("");
304 w->flags = 0;
306 TAILQ_INIT(&w->panes);
307 TAILQ_INIT(&w->last_panes);
308 w->active = NULL;
310 w->lastlayout = -1;
311 w->layout_root = NULL;
313 w->sx = sx;
314 w->sy = sy;
315 w->manual_sx = sx;
316 w->manual_sy = sy;
317 w->xpixel = xpixel;
318 w->ypixel = ypixel;
320 w->options = options_create(global_w_options);
322 w->references = 0;
323 TAILQ_INIT(&w->winlinks);
325 w->id = next_window_id++;
326 RB_INSERT(windows, &windows, w);
328 window_set_fill_character(w);
329 window_update_activity(w);
331 log_debug("%s: @%u create %ux%u (%ux%u)", __func__, w->id, sx, sy,
332 w->xpixel, w->ypixel);
333 return (w);
336 static void
337 window_destroy(struct window *w)
339 log_debug("window @%u destroyed (%d references)", w->id, w->references);
341 window_unzoom(w, 0);
342 RB_REMOVE(windows, &windows, w);
344 if (w->layout_root != NULL)
345 layout_free_cell(w->layout_root);
346 if (w->saved_layout_root != NULL)
347 layout_free_cell(w->saved_layout_root);
348 free(w->old_layout);
350 window_destroy_panes(w);
352 if (event_initialized(&w->name_event))
353 evtimer_del(&w->name_event);
355 if (event_initialized(&w->alerts_timer))
356 evtimer_del(&w->alerts_timer);
357 if (event_initialized(&w->offset_timer))
358 event_del(&w->offset_timer);
360 options_free(w->options);
361 free(w->fill_character);
363 free(w->name);
364 free(w);
368 window_pane_destroy_ready(struct window_pane *wp)
370 int n;
372 if (wp->pipe_fd != -1) {
373 if (EVBUFFER_LENGTH(wp->pipe_event->output) != 0)
374 return (0);
375 if (ioctl(wp->fd, FIONREAD, &n) != -1 && n > 0)
376 return (0);
379 if (~wp->flags & PANE_EXITED)
380 return (0);
381 return (1);
384 void
385 window_add_ref(struct window *w, const char *from)
387 w->references++;
388 log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
391 void
392 window_remove_ref(struct window *w, const char *from)
394 w->references--;
395 log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
397 if (w->references == 0)
398 window_destroy(w);
401 void
402 window_set_name(struct window *w, const char *new_name)
404 free(w->name);
405 utf8_stravis(&w->name, new_name, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
406 notify_window("window-renamed", w);
409 void
410 window_resize(struct window *w, u_int sx, u_int sy, int xpixel, int ypixel)
412 if (xpixel == 0)
413 xpixel = DEFAULT_XPIXEL;
414 if (ypixel == 0)
415 ypixel = DEFAULT_YPIXEL;
417 log_debug("%s: @%u resize %ux%u (%ux%u)", __func__, w->id, sx, sy,
418 xpixel == -1 ? w->xpixel : (u_int)xpixel,
419 ypixel == -1 ? w->ypixel : (u_int)ypixel);
420 w->sx = sx;
421 w->sy = sy;
422 if (xpixel != -1)
423 w->xpixel = xpixel;
424 if (ypixel != -1)
425 w->ypixel = ypixel;
428 void
429 window_pane_send_resize(struct window_pane *wp, u_int sx, u_int sy)
431 struct window *w = wp->window;
432 struct winsize ws;
434 if (wp->fd == -1)
435 return;
437 log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, sx, sy);
439 memset(&ws, 0, sizeof ws);
440 ws.ws_col = sx;
441 ws.ws_row = sy;
442 ws.ws_xpixel = w->xpixel * ws.ws_col;
443 ws.ws_ypixel = w->ypixel * ws.ws_row;
444 if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
445 #ifdef __sun
447 * Some versions of Solaris apparently can return an error when
448 * resizing; don't know why this happens, can't reproduce on
449 * other platforms and ignoring it doesn't seem to cause any
450 * issues.
452 if (errno != EINVAL && errno != ENXIO)
453 #endif
454 fatal("ioctl failed");
458 window_has_pane(struct window *w, struct window_pane *wp)
460 struct window_pane *wp1;
462 TAILQ_FOREACH(wp1, &w->panes, entry) {
463 if (wp1 == wp)
464 return (1);
466 return (0);
469 void
470 window_update_focus(struct window *w)
472 if (w != NULL) {
473 log_debug("%s: @%u", __func__, w->id);
474 window_pane_update_focus(w->active);
478 void
479 window_pane_update_focus(struct window_pane *wp)
481 struct client *c;
482 int focused = 0;
484 if (wp != NULL && (~wp->flags & PANE_EXITED)) {
485 if (wp != wp->window->active)
486 focused = 0;
487 else {
488 TAILQ_FOREACH(c, &clients, entry) {
489 if (c->session != NULL &&
490 c->session->attached != 0 &&
491 (c->flags & CLIENT_FOCUSED) &&
492 c->session->curw->window == wp->window &&
493 c->overlay_draw == NULL) {
494 focused = 1;
495 break;
499 if (!focused && (wp->flags & PANE_FOCUSED)) {
500 log_debug("%s: %%%u focus out", __func__, wp->id);
501 if (wp->base.mode & MODE_FOCUSON)
502 bufferevent_write(wp->event, "\033[O", 3);
503 notify_pane("pane-focus-out", wp);
504 wp->flags &= ~PANE_FOCUSED;
505 } else if (focused && (~wp->flags & PANE_FOCUSED)) {
506 log_debug("%s: %%%u focus in", __func__, wp->id);
507 if (wp->base.mode & MODE_FOCUSON)
508 bufferevent_write(wp->event, "\033[I", 3);
509 notify_pane("pane-focus-in", wp);
510 wp->flags |= PANE_FOCUSED;
511 } else
512 log_debug("%s: %%%u focus unchanged", __func__, wp->id);
517 window_set_active_pane(struct window *w, struct window_pane *wp, int notify)
519 struct window_pane *lastwp;
521 log_debug("%s: pane %%%u", __func__, wp->id);
523 if (wp == w->active)
524 return (0);
525 lastwp = w->active;
527 window_pane_stack_remove(&w->last_panes, wp);
528 window_pane_stack_push(&w->last_panes, lastwp);
530 w->active = wp;
531 w->active->active_point = next_active_point++;
532 w->active->flags |= PANE_CHANGED;
534 if (options_get_number(global_options, "focus-events")) {
535 window_pane_update_focus(lastwp);
536 window_pane_update_focus(w->active);
539 tty_update_window_offset(w);
541 if (notify)
542 notify_window("window-pane-changed", w);
543 return (1);
546 static int
547 window_pane_get_palette(struct window_pane *wp, int c)
549 if (wp == NULL)
550 return (-1);
551 return (colour_palette_get(&wp->palette, c));
554 void
555 window_redraw_active_switch(struct window *w, struct window_pane *wp)
557 struct grid_cell *gc1, *gc2;
558 int c1, c2;
560 if (wp == w->active)
561 return;
563 for (;;) {
565 * If the active and inactive styles or palettes are different,
566 * need to redraw the panes.
568 gc1 = &wp->cached_gc;
569 gc2 = &wp->cached_active_gc;
570 if (!grid_cells_look_equal(gc1, gc2))
571 wp->flags |= PANE_REDRAW;
572 else {
573 c1 = window_pane_get_palette(wp, gc1->fg);
574 c2 = window_pane_get_palette(wp, gc2->fg);
575 if (c1 != c2)
576 wp->flags |= PANE_REDRAW;
577 else {
578 c1 = window_pane_get_palette(wp, gc1->bg);
579 c2 = window_pane_get_palette(wp, gc2->bg);
580 if (c1 != c2)
581 wp->flags |= PANE_REDRAW;
584 if (wp == w->active)
585 break;
586 wp = w->active;
590 struct window_pane *
591 window_get_active_at(struct window *w, u_int x, u_int y)
593 struct window_pane *wp;
594 int pane_scrollbars;
595 u_int sb_pos, sb_w, xoff, sx;
597 pane_scrollbars = options_get_number(w->options, "pane-scrollbars");
598 sb_pos = options_get_number(w->options, "pane-scrollbars-position");
600 TAILQ_FOREACH(wp, &w->panes, entry) {
601 if (!window_pane_visible(wp))
602 continue;
604 if (pane_scrollbars == PANE_SCROLLBARS_ALWAYS ||
605 (pane_scrollbars == PANE_SCROLLBARS_MODAL &&
606 window_pane_mode(wp) != WINDOW_PANE_NO_MODE)) {
607 sb_w = wp->scrollbar_style.width +
608 wp->scrollbar_style.pad;
609 } else
610 sb_w = 0;
612 if (sb_pos == PANE_SCROLLBARS_LEFT) {
613 xoff = wp->xoff - sb_w;
614 sx = wp->sx + sb_w;
615 } else { /* sb_pos == PANE_SCROLLBARS_RIGHT */
616 xoff = wp->xoff;
617 sx = wp->sx + sb_w;
619 if (x < xoff || x > xoff + sx)
620 continue;
621 if (y < wp->yoff || y > wp->yoff + wp->sy)
622 continue;
623 return (wp);
625 return (NULL);
628 struct window_pane *
629 window_find_string(struct window *w, const char *s)
631 u_int x, y, top = 0, bottom = w->sy - 1;
632 int status;
634 x = w->sx / 2;
635 y = w->sy / 2;
637 status = options_get_number(w->options, "pane-border-status");
638 if (status == PANE_STATUS_TOP)
639 top++;
640 else if (status == PANE_STATUS_BOTTOM)
641 bottom--;
643 if (strcasecmp(s, "top") == 0)
644 y = top;
645 else if (strcasecmp(s, "bottom") == 0)
646 y = bottom;
647 else if (strcasecmp(s, "left") == 0)
648 x = 0;
649 else if (strcasecmp(s, "right") == 0)
650 x = w->sx - 1;
651 else if (strcasecmp(s, "top-left") == 0) {
652 x = 0;
653 y = top;
654 } else if (strcasecmp(s, "top-right") == 0) {
655 x = w->sx - 1;
656 y = top;
657 } else if (strcasecmp(s, "bottom-left") == 0) {
658 x = 0;
659 y = bottom;
660 } else if (strcasecmp(s, "bottom-right") == 0) {
661 x = w->sx - 1;
662 y = bottom;
663 } else
664 return (NULL);
666 return (window_get_active_at(w, x, y));
670 window_zoom(struct window_pane *wp)
672 struct window *w = wp->window;
673 struct window_pane *wp1;
675 if (w->flags & WINDOW_ZOOMED)
676 return (-1);
678 if (window_count_panes(w) == 1)
679 return (-1);
681 if (w->active != wp)
682 window_set_active_pane(w, wp, 1);
684 TAILQ_FOREACH(wp1, &w->panes, entry) {
685 wp1->saved_layout_cell = wp1->layout_cell;
686 wp1->layout_cell = NULL;
689 w->saved_layout_root = w->layout_root;
690 layout_init(w, wp);
691 w->flags |= WINDOW_ZOOMED;
692 notify_window("window-layout-changed", w);
694 return (0);
698 window_unzoom(struct window *w, int notify)
700 struct window_pane *wp;
702 if (!(w->flags & WINDOW_ZOOMED))
703 return (-1);
705 w->flags &= ~WINDOW_ZOOMED;
706 layout_free(w);
707 w->layout_root = w->saved_layout_root;
708 w->saved_layout_root = NULL;
710 TAILQ_FOREACH(wp, &w->panes, entry) {
711 wp->layout_cell = wp->saved_layout_cell;
712 wp->saved_layout_cell = NULL;
714 layout_fix_panes(w, NULL);
716 if (notify)
717 notify_window("window-layout-changed", w);
719 return (0);
723 window_push_zoom(struct window *w, int always, int flag)
725 log_debug("%s: @%u %d", __func__, w->id,
726 flag && (w->flags & WINDOW_ZOOMED));
727 if (flag && (always || (w->flags & WINDOW_ZOOMED)))
728 w->flags |= WINDOW_WASZOOMED;
729 else
730 w->flags &= ~WINDOW_WASZOOMED;
731 return (window_unzoom(w, 1) == 0);
735 window_pop_zoom(struct window *w)
737 log_debug("%s: @%u %d", __func__, w->id,
738 !!(w->flags & WINDOW_WASZOOMED));
739 if (w->flags & WINDOW_WASZOOMED)
740 return (window_zoom(w->active) == 0);
741 return (0);
744 struct window_pane *
745 window_add_pane(struct window *w, struct window_pane *other, u_int hlimit,
746 int flags)
748 struct window_pane *wp;
750 if (other == NULL)
751 other = w->active;
753 wp = window_pane_create(w, w->sx, w->sy, hlimit);
754 if (TAILQ_EMPTY(&w->panes)) {
755 log_debug("%s: @%u at start", __func__, w->id);
756 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
757 } else if (flags & SPAWN_BEFORE) {
758 log_debug("%s: @%u before %%%u", __func__, w->id, wp->id);
759 if (flags & SPAWN_FULLSIZE)
760 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
761 else
762 TAILQ_INSERT_BEFORE(other, wp, entry);
763 } else {
764 log_debug("%s: @%u after %%%u", __func__, w->id, wp->id);
765 if (flags & SPAWN_FULLSIZE)
766 TAILQ_INSERT_TAIL(&w->panes, wp, entry);
767 else
768 TAILQ_INSERT_AFTER(&w->panes, other, wp, entry);
770 return (wp);
773 void
774 window_lost_pane(struct window *w, struct window_pane *wp)
776 log_debug("%s: @%u pane %%%u", __func__, w->id, wp->id);
778 if (wp == marked_pane.wp)
779 server_clear_marked();
781 window_pane_stack_remove(&w->last_panes, wp);
782 if (wp == w->active) {
783 w->active = TAILQ_FIRST(&w->last_panes);
784 if (w->active == NULL) {
785 w->active = TAILQ_PREV(wp, window_panes, entry);
786 if (w->active == NULL)
787 w->active = TAILQ_NEXT(wp, entry);
789 if (w->active != NULL) {
790 window_pane_stack_remove(&w->last_panes, w->active);
791 w->active->flags |= PANE_CHANGED;
792 notify_window("window-pane-changed", w);
793 window_update_focus(w);
798 void
799 window_remove_pane(struct window *w, struct window_pane *wp)
801 window_lost_pane(w, wp);
803 TAILQ_REMOVE(&w->panes, wp, entry);
804 window_pane_destroy(wp);
807 struct window_pane *
808 window_pane_at_index(struct window *w, u_int idx)
810 struct window_pane *wp;
811 u_int n;
813 n = options_get_number(w->options, "pane-base-index");
814 TAILQ_FOREACH(wp, &w->panes, entry) {
815 if (n == idx)
816 return (wp);
817 n++;
819 return (NULL);
822 struct window_pane *
823 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
825 for (; n > 0; n--) {
826 if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
827 wp = TAILQ_FIRST(&w->panes);
830 return (wp);
833 struct window_pane *
834 window_pane_previous_by_number(struct window *w, struct window_pane *wp,
835 u_int n)
837 for (; n > 0; n--) {
838 if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
839 wp = TAILQ_LAST(&w->panes, window_panes);
842 return (wp);
846 window_pane_index(struct window_pane *wp, u_int *i)
848 struct window_pane *wq;
849 struct window *w = wp->window;
851 *i = options_get_number(w->options, "pane-base-index");
852 TAILQ_FOREACH(wq, &w->panes, entry) {
853 if (wp == wq) {
854 return (0);
856 (*i)++;
859 return (-1);
862 u_int
863 window_count_panes(struct window *w)
865 struct window_pane *wp;
866 u_int n;
868 n = 0;
869 TAILQ_FOREACH(wp, &w->panes, entry)
870 n++;
871 return (n);
874 void
875 window_destroy_panes(struct window *w)
877 struct window_pane *wp;
879 while (!TAILQ_EMPTY(&w->last_panes)) {
880 wp = TAILQ_FIRST(&w->last_panes);
881 window_pane_stack_remove(&w->last_panes, wp);
884 while (!TAILQ_EMPTY(&w->panes)) {
885 wp = TAILQ_FIRST(&w->panes);
886 TAILQ_REMOVE(&w->panes, wp, entry);
887 window_pane_destroy(wp);
891 const char *
892 window_printable_flags(struct winlink *wl, int escape)
894 struct session *s = wl->session;
895 static char flags[32];
896 int pos;
898 pos = 0;
899 if (wl->flags & WINLINK_ACTIVITY) {
900 flags[pos++] = '#';
901 if (escape)
902 flags[pos++] = '#';
904 if (wl->flags & WINLINK_BELL)
905 flags[pos++] = '!';
906 if (wl->flags & WINLINK_SILENCE)
907 flags[pos++] = '~';
908 if (wl == s->curw)
909 flags[pos++] = '*';
910 if (wl == TAILQ_FIRST(&s->lastw))
911 flags[pos++] = '-';
912 if (server_check_marked() && wl == marked_pane.wl)
913 flags[pos++] = 'M';
914 if (wl->window->flags & WINDOW_ZOOMED)
915 flags[pos++] = 'Z';
916 flags[pos] = '\0';
917 return (flags);
920 struct window_pane *
921 window_pane_find_by_id_str(const char *s)
923 const char *errstr;
924 u_int id;
926 if (*s != '%')
927 return (NULL);
929 id = strtonum(s + 1, 0, UINT_MAX, &errstr);
930 if (errstr != NULL)
931 return (NULL);
932 return (window_pane_find_by_id(id));
935 struct window_pane *
936 window_pane_find_by_id(u_int id)
938 struct window_pane wp;
940 wp.id = id;
941 return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
944 static struct window_pane *
945 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
947 struct window_pane *wp;
948 char host[HOST_NAME_MAX + 1];
950 wp = xcalloc(1, sizeof *wp);
951 wp->window = w;
952 wp->options = options_create(w->options);
953 wp->flags = PANE_STYLECHANGED;
955 wp->id = next_window_pane_id++;
956 RB_INSERT(window_pane_tree, &all_window_panes, wp);
958 wp->fd = -1;
960 TAILQ_INIT(&wp->modes);
962 TAILQ_INIT (&wp->resize_queue);
964 wp->sx = sx;
965 wp->sy = sy;
967 wp->pipe_fd = -1;
969 wp->control_bg = -1;
970 wp->control_fg = -1;
972 style_set_scrollbar_style_from_option(&wp->scrollbar_style,
973 wp->options);
975 colour_palette_init(&wp->palette);
976 colour_palette_from_option(&wp->palette, wp->options);
978 screen_init(&wp->base, sx, sy, hlimit);
979 wp->screen = &wp->base;
980 window_pane_default_cursor(wp);
982 screen_init(&wp->status_screen, 1, 1, 0);
984 if (gethostname(host, sizeof host) == 0)
985 screen_set_title(&wp->base, host);
987 return (wp);
990 static void
991 window_pane_destroy(struct window_pane *wp)
993 struct window_pane_resize *r;
994 struct window_pane_resize *r1;
996 window_pane_reset_mode_all(wp);
997 free(wp->searchstr);
999 if (wp->fd != -1) {
1000 #ifdef HAVE_UTEMPTER
1001 utempter_remove_record(wp->fd);
1002 #endif
1003 bufferevent_free(wp->event);
1004 close(wp->fd);
1006 if (wp->ictx != NULL)
1007 input_free(wp->ictx);
1009 screen_free(&wp->status_screen);
1011 screen_free(&wp->base);
1013 if (wp->pipe_fd != -1) {
1014 bufferevent_free(wp->pipe_event);
1015 close(wp->pipe_fd);
1018 if (event_initialized(&wp->resize_timer))
1019 event_del(&wp->resize_timer);
1020 TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) {
1021 TAILQ_REMOVE(&wp->resize_queue, r, entry);
1022 free(r);
1025 RB_REMOVE(window_pane_tree, &all_window_panes, wp);
1027 options_free(wp->options);
1028 free((void *)wp->cwd);
1029 free(wp->shell);
1030 cmd_free_argv(wp->argc, wp->argv);
1031 colour_palette_free(&wp->palette);
1032 free(wp);
1035 static void
1036 window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
1038 struct window_pane *wp = data;
1039 struct evbuffer *evb = wp->event->input;
1040 struct window_pane_offset *wpo = &wp->pipe_offset;
1041 size_t size = EVBUFFER_LENGTH(evb);
1042 char *new_data;
1043 size_t new_size;
1044 struct client *c;
1046 if (wp->pipe_fd != -1) {
1047 new_data = window_pane_get_new_data(wp, wpo, &new_size);
1048 if (new_size > 0) {
1049 bufferevent_write(wp->pipe_event, new_data, new_size);
1050 window_pane_update_used_data(wp, wpo, new_size);
1054 log_debug("%%%u has %zu bytes", wp->id, size);
1055 TAILQ_FOREACH(c, &clients, entry) {
1056 if (c->session != NULL && (c->flags & CLIENT_CONTROL))
1057 control_write_output(c, wp);
1059 input_parse_pane(wp);
1060 bufferevent_disable(wp->event, EV_READ);
1063 static void
1064 window_pane_error_callback(__unused struct bufferevent *bufev,
1065 __unused short what, void *data)
1067 struct window_pane *wp = data;
1069 log_debug("%%%u error", wp->id);
1070 wp->flags |= PANE_EXITED;
1072 if (window_pane_destroy_ready(wp))
1073 server_destroy_pane(wp, 1);
1076 void
1077 window_pane_set_event(struct window_pane *wp)
1079 setblocking(wp->fd, 0);
1081 wp->event = bufferevent_new(wp->fd, window_pane_read_callback,
1082 NULL, window_pane_error_callback, wp);
1083 if (wp->event == NULL)
1084 fatalx("out of memory");
1085 wp->ictx = input_init(wp, wp->event, &wp->palette);
1087 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1090 void
1091 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
1093 struct window_mode_entry *wme;
1094 struct window_pane_resize *r;
1096 if (sx == wp->sx && sy == wp->sy)
1097 return;
1099 r = xmalloc(sizeof *r);
1100 r->sx = sx;
1101 r->sy = sy;
1102 r->osx = wp->sx;
1103 r->osy = wp->sy;
1104 TAILQ_INSERT_TAIL (&wp->resize_queue, r, entry);
1106 wp->sx = sx;
1107 wp->sy = sy;
1109 log_debug("%s: %%%u resize %ux%u", __func__, wp->id, sx, sy);
1110 screen_resize(&wp->base, sx, sy, wp->base.saved_grid == NULL);
1112 wme = TAILQ_FIRST(&wp->modes);
1113 if (wme != NULL && wme->mode->resize != NULL)
1114 wme->mode->resize(wme, sx, sy);
1118 window_pane_set_mode(struct window_pane *wp, struct window_pane *swp,
1119 const struct window_mode *mode, struct cmd_find_state *fs,
1120 struct args *args)
1122 struct window_mode_entry *wme;
1123 struct window *w = wp->window;
1125 if (!TAILQ_EMPTY(&wp->modes) && TAILQ_FIRST(&wp->modes)->mode == mode)
1126 return (1);
1128 TAILQ_FOREACH(wme, &wp->modes, entry) {
1129 if (wme->mode == mode)
1130 break;
1132 if (wme != NULL) {
1133 TAILQ_REMOVE(&wp->modes, wme, entry);
1134 TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1135 } else {
1136 wme = xcalloc(1, sizeof *wme);
1137 wme->wp = wp;
1138 wme->swp = swp;
1139 wme->mode = mode;
1140 wme->prefix = 1;
1141 TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1142 wme->screen = wme->mode->init(wme, fs, args);
1144 wp->screen = wme->screen;
1146 wp->flags |= (PANE_REDRAW|PANE_REDRAWSCROLLBAR|PANE_CHANGED);
1147 layout_fix_panes(w, NULL);
1149 server_redraw_window_borders(wp->window);
1150 server_status_window(wp->window);
1151 notify_pane("pane-mode-changed", wp);
1153 return (0);
1156 void
1157 window_pane_reset_mode(struct window_pane *wp)
1159 struct window_mode_entry *wme, *next;
1160 struct window *w = wp->window;
1162 if (TAILQ_EMPTY(&wp->modes))
1163 return;
1165 wme = TAILQ_FIRST(&wp->modes);
1166 TAILQ_REMOVE(&wp->modes, wme, entry);
1167 wme->mode->free(wme);
1168 free(wme);
1170 next = TAILQ_FIRST(&wp->modes);
1171 if (next == NULL) {
1172 wp->flags &= ~PANE_UNSEENCHANGES;
1173 log_debug("%s: no next mode", __func__);
1174 wp->screen = &wp->base;
1175 } else {
1176 log_debug("%s: next mode is %s", __func__, next->mode->name);
1177 wp->screen = next->screen;
1178 if (next->mode->resize != NULL)
1179 next->mode->resize(next, wp->sx, wp->sy);
1182 wp->flags |= (PANE_REDRAW|PANE_REDRAWSCROLLBAR|PANE_CHANGED);
1183 layout_fix_panes(w, NULL);
1185 server_redraw_window_borders(wp->window);
1186 server_status_window(wp->window);
1187 notify_pane("pane-mode-changed", wp);
1190 void
1191 window_pane_reset_mode_all(struct window_pane *wp)
1193 while (!TAILQ_EMPTY(&wp->modes))
1194 window_pane_reset_mode(wp);
1197 static void
1198 window_pane_copy_paste(struct window_pane *wp, char *buf, size_t len)
1200 struct window_pane *loop;
1202 TAILQ_FOREACH(loop, &wp->window->panes, entry) {
1203 if (loop != wp &&
1204 TAILQ_EMPTY(&loop->modes) &&
1205 loop->fd != -1 &&
1206 (~loop->flags & PANE_INPUTOFF) &&
1207 window_pane_visible(loop) &&
1208 options_get_number(loop->options, "synchronize-panes")) {
1209 log_debug("%s: %.*s", __func__, (int)len, buf);
1210 bufferevent_write(loop->event, buf, len);
1215 static void
1216 window_pane_copy_key(struct window_pane *wp, key_code key)
1218 struct window_pane *loop;
1220 TAILQ_FOREACH(loop, &wp->window->panes, entry) {
1221 if (loop != wp &&
1222 TAILQ_EMPTY(&loop->modes) &&
1223 loop->fd != -1 &&
1224 (~loop->flags & PANE_INPUTOFF) &&
1225 window_pane_visible(loop) &&
1226 options_get_number(loop->options, "synchronize-panes"))
1227 input_key_pane(loop, key, NULL);
1231 void
1232 window_pane_paste(struct window_pane *wp, char *buf, size_t len)
1234 if (!TAILQ_EMPTY(&wp->modes))
1235 return;
1237 if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1238 return;
1240 log_debug("%s: %.*s", __func__, (int)len, buf);
1241 bufferevent_write(wp->event, buf, len);
1243 if (options_get_number(wp->options, "synchronize-panes"))
1244 window_pane_copy_paste(wp, buf, len);
1248 window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1249 struct winlink *wl, key_code key, struct mouse_event *m)
1251 struct window_mode_entry *wme;
1253 if (KEYC_IS_MOUSE(key) && m == NULL)
1254 return (-1);
1256 wme = TAILQ_FIRST(&wp->modes);
1257 if (wme != NULL) {
1258 if (wme->mode->key != NULL && c != NULL) {
1259 key &= ~KEYC_MASK_FLAGS;
1260 wme->mode->key(wme, c, s, wl, key, m);
1262 return (0);
1265 if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1266 return (0);
1268 if (input_key_pane(wp, key, m) != 0)
1269 return (-1);
1271 if (KEYC_IS_MOUSE(key))
1272 return (0);
1273 if (options_get_number(wp->options, "synchronize-panes"))
1274 window_pane_copy_key(wp, key);
1275 return (0);
1279 window_pane_visible(struct window_pane *wp)
1281 if (~wp->window->flags & WINDOW_ZOOMED)
1282 return (1);
1283 return (wp == wp->window->active);
1287 window_pane_exited(struct window_pane *wp)
1289 return (wp->fd == -1 || (wp->flags & PANE_EXITED));
1292 u_int
1293 window_pane_search(struct window_pane *wp, const char *term, int regex,
1294 int ignore)
1296 struct screen *s = &wp->base;
1297 regex_t r;
1298 char *new = NULL, *line;
1299 u_int i;
1300 int flags = 0, found;
1301 size_t n;
1303 if (!regex) {
1304 if (ignore)
1305 flags |= FNM_CASEFOLD;
1306 xasprintf(&new, "*%s*", term);
1307 } else {
1308 if (ignore)
1309 flags |= REG_ICASE;
1310 if (regcomp(&r, term, flags|REG_EXTENDED) != 0)
1311 return (0);
1314 for (i = 0; i < screen_size_y(s); i++) {
1315 line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1316 for (n = strlen(line); n > 0; n--) {
1317 if (!isspace((u_char)line[n - 1]))
1318 break;
1319 line[n - 1] = '\0';
1321 log_debug("%s: %s", __func__, line);
1322 if (!regex)
1323 found = (fnmatch(new, line, flags) == 0);
1324 else
1325 found = (regexec(&r, line, 0, NULL, 0) == 0);
1326 free(line);
1327 if (found)
1328 break;
1330 if (!regex)
1331 free(new);
1332 else
1333 regfree(&r);
1335 if (i == screen_size_y(s))
1336 return (0);
1337 return (i + 1);
1340 /* Get MRU pane from a list. */
1341 static struct window_pane *
1342 window_pane_choose_best(struct window_pane **list, u_int size)
1344 struct window_pane *next, *best;
1345 u_int i;
1347 if (size == 0)
1348 return (NULL);
1350 best = list[0];
1351 for (i = 1; i < size; i++) {
1352 next = list[i];
1353 if (next->active_point > best->active_point)
1354 best = next;
1356 return (best);
1360 * Find the pane directly above another. We build a list of those adjacent to
1361 * top edge and then choose the best.
1363 struct window_pane *
1364 window_pane_find_up(struct window_pane *wp)
1366 struct window *w;
1367 struct window_pane *next, *best, **list;
1368 u_int edge, left, right, end, size;
1369 int status, found;
1371 if (wp == NULL)
1372 return (NULL);
1373 w = wp->window;
1374 status = options_get_number(w->options, "pane-border-status");
1376 list = NULL;
1377 size = 0;
1379 edge = wp->yoff;
1380 if (status == PANE_STATUS_TOP) {
1381 if (edge == 1)
1382 edge = w->sy + 1;
1383 } else if (status == PANE_STATUS_BOTTOM) {
1384 if (edge == 0)
1385 edge = w->sy;
1386 } else {
1387 if (edge == 0)
1388 edge = w->sy + 1;
1391 left = wp->xoff;
1392 right = wp->xoff + wp->sx;
1394 TAILQ_FOREACH(next, &w->panes, entry) {
1395 if (next == wp)
1396 continue;
1397 if (next->yoff + next->sy + 1 != edge)
1398 continue;
1399 end = next->xoff + next->sx - 1;
1401 found = 0;
1402 if (next->xoff < left && end > right)
1403 found = 1;
1404 else if (next->xoff >= left && next->xoff <= right)
1405 found = 1;
1406 else if (end >= left && end <= right)
1407 found = 1;
1408 if (!found)
1409 continue;
1410 list = xreallocarray(list, size + 1, sizeof *list);
1411 list[size++] = next;
1414 best = window_pane_choose_best(list, size);
1415 free(list);
1416 return (best);
1419 /* Find the pane directly below another. */
1420 struct window_pane *
1421 window_pane_find_down(struct window_pane *wp)
1423 struct window *w;
1424 struct window_pane *next, *best, **list;
1425 u_int edge, left, right, end, size;
1426 int status, found;
1428 if (wp == NULL)
1429 return (NULL);
1430 w = wp->window;
1431 status = options_get_number(w->options, "pane-border-status");
1433 list = NULL;
1434 size = 0;
1436 edge = wp->yoff + wp->sy + 1;
1437 if (status == PANE_STATUS_TOP) {
1438 if (edge >= w->sy)
1439 edge = 1;
1440 } else if (status == PANE_STATUS_BOTTOM) {
1441 if (edge >= w->sy - 1)
1442 edge = 0;
1443 } else {
1444 if (edge >= w->sy)
1445 edge = 0;
1448 left = wp->xoff;
1449 right = wp->xoff + wp->sx;
1451 TAILQ_FOREACH(next, &w->panes, entry) {
1452 if (next == wp)
1453 continue;
1454 if (next->yoff != edge)
1455 continue;
1456 end = next->xoff + next->sx - 1;
1458 found = 0;
1459 if (next->xoff < left && end > right)
1460 found = 1;
1461 else if (next->xoff >= left && next->xoff <= right)
1462 found = 1;
1463 else if (end >= left && end <= right)
1464 found = 1;
1465 if (!found)
1466 continue;
1467 list = xreallocarray(list, size + 1, sizeof *list);
1468 list[size++] = next;
1471 best = window_pane_choose_best(list, size);
1472 free(list);
1473 return (best);
1476 /* Find the pane directly to the left of another. */
1477 struct window_pane *
1478 window_pane_find_left(struct window_pane *wp)
1480 struct window *w;
1481 struct window_pane *next, *best, **list;
1482 u_int edge, top, bottom, end, size;
1483 int found;
1485 if (wp == NULL)
1486 return (NULL);
1487 w = wp->window;
1489 list = NULL;
1490 size = 0;
1492 edge = wp->xoff;
1493 if (edge == 0)
1494 edge = w->sx + 1;
1496 top = wp->yoff;
1497 bottom = wp->yoff + wp->sy;
1499 TAILQ_FOREACH(next, &w->panes, entry) {
1500 if (next == wp)
1501 continue;
1502 if (next->xoff + next->sx + 1 != edge)
1503 continue;
1504 end = next->yoff + next->sy - 1;
1506 found = 0;
1507 if (next->yoff < top && end > bottom)
1508 found = 1;
1509 else if (next->yoff >= top && next->yoff <= bottom)
1510 found = 1;
1511 else if (end >= top && end <= bottom)
1512 found = 1;
1513 if (!found)
1514 continue;
1515 list = xreallocarray(list, size + 1, sizeof *list);
1516 list[size++] = next;
1519 best = window_pane_choose_best(list, size);
1520 free(list);
1521 return (best);
1524 /* Find the pane directly to the right of another. */
1525 struct window_pane *
1526 window_pane_find_right(struct window_pane *wp)
1528 struct window *w;
1529 struct window_pane *next, *best, **list;
1530 u_int edge, top, bottom, end, size;
1531 int found;
1533 if (wp == NULL)
1534 return (NULL);
1535 w = wp->window;
1537 list = NULL;
1538 size = 0;
1540 edge = wp->xoff + wp->sx + 1;
1541 if (edge >= w->sx)
1542 edge = 0;
1544 top = wp->yoff;
1545 bottom = wp->yoff + wp->sy;
1547 TAILQ_FOREACH(next, &w->panes, entry) {
1548 if (next == wp)
1549 continue;
1550 if (next->xoff != edge)
1551 continue;
1552 end = next->yoff + next->sy - 1;
1554 found = 0;
1555 if (next->yoff < top && end > bottom)
1556 found = 1;
1557 else if (next->yoff >= top && next->yoff <= bottom)
1558 found = 1;
1559 else if (end >= top && end <= bottom)
1560 found = 1;
1561 if (!found)
1562 continue;
1563 list = xreallocarray(list, size + 1, sizeof *list);
1564 list[size++] = next;
1567 best = window_pane_choose_best(list, size);
1568 free(list);
1569 return (best);
1572 void
1573 window_pane_stack_push(struct window_panes *stack, struct window_pane *wp)
1575 if (wp != NULL) {
1576 window_pane_stack_remove(stack, wp);
1577 TAILQ_INSERT_HEAD(stack, wp, sentry);
1578 wp->flags |= PANE_VISITED;
1582 void
1583 window_pane_stack_remove(struct window_panes *stack, struct window_pane *wp)
1585 if (wp != NULL && (wp->flags & PANE_VISITED)) {
1586 TAILQ_REMOVE(stack, wp, sentry);
1587 wp->flags &= ~PANE_VISITED;
1591 /* Clear alert flags for a winlink */
1592 void
1593 winlink_clear_flags(struct winlink *wl)
1595 struct winlink *loop;
1597 wl->window->flags &= ~WINDOW_ALERTFLAGS;
1598 TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
1599 if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
1600 loop->flags &= ~WINLINK_ALERTFLAGS;
1601 server_status_session(loop->session);
1606 /* Shuffle window indexes up. */
1608 winlink_shuffle_up(struct session *s, struct winlink *wl, int before)
1610 int idx, last;
1612 if (wl == NULL)
1613 return (-1);
1614 if (before)
1615 idx = wl->idx;
1616 else
1617 idx = wl->idx + 1;
1619 /* Find the next free index. */
1620 for (last = idx; last < INT_MAX; last++) {
1621 if (winlink_find_by_index(&s->windows, last) == NULL)
1622 break;
1624 if (last == INT_MAX)
1625 return (-1);
1627 /* Move everything from last - 1 to idx up a bit. */
1628 for (; last > idx; last--) {
1629 wl = winlink_find_by_index(&s->windows, last - 1);
1630 RB_REMOVE(winlinks, &s->windows, wl);
1631 wl->idx++;
1632 RB_INSERT(winlinks, &s->windows, wl);
1635 return (idx);
1638 static void
1639 window_pane_input_callback(struct client *c, __unused const char *path,
1640 int error, int closed, struct evbuffer *buffer, void *data)
1642 struct window_pane_input_data *cdata = data;
1643 struct window_pane *wp;
1644 u_char *buf = EVBUFFER_DATA(buffer);
1645 size_t len = EVBUFFER_LENGTH(buffer);
1647 wp = window_pane_find_by_id(cdata->wp);
1648 if (cdata->file != NULL && (wp == NULL || c->flags & CLIENT_DEAD)) {
1649 if (wp == NULL) {
1650 c->retval = 1;
1651 c->flags |= CLIENT_EXIT;
1653 file_cancel(cdata->file);
1654 } else if (cdata->file == NULL || closed || error != 0) {
1655 cmdq_continue(cdata->item);
1656 server_client_unref(c);
1657 free(cdata);
1658 } else
1659 input_parse_buffer(wp, buf, len);
1660 evbuffer_drain(buffer, len);
1664 window_pane_start_input(struct window_pane *wp, struct cmdq_item *item,
1665 char **cause)
1667 struct client *c = cmdq_get_client(item);
1668 struct window_pane_input_data *cdata;
1670 if (~wp->flags & PANE_EMPTY) {
1671 *cause = xstrdup("pane is not empty");
1672 return (-1);
1674 if (c->flags & (CLIENT_DEAD|CLIENT_EXITED))
1675 return (1);
1676 if (c->session != NULL)
1677 return (1);
1679 cdata = xmalloc(sizeof *cdata);
1680 cdata->item = item;
1681 cdata->wp = wp->id;
1682 cdata->file = file_read(c, "-", window_pane_input_callback, cdata);
1683 c->references++;
1685 return (0);
1688 void *
1689 window_pane_get_new_data(struct window_pane *wp,
1690 struct window_pane_offset *wpo, size_t *size)
1692 size_t used = wpo->used - wp->base_offset;
1694 *size = EVBUFFER_LENGTH(wp->event->input) - used;
1695 return (EVBUFFER_DATA(wp->event->input) + used);
1698 void
1699 window_pane_update_used_data(struct window_pane *wp,
1700 struct window_pane_offset *wpo, size_t size)
1702 size_t used = wpo->used - wp->base_offset;
1704 if (size > EVBUFFER_LENGTH(wp->event->input) - used)
1705 size = EVBUFFER_LENGTH(wp->event->input) - used;
1706 wpo->used += size;
1709 void
1710 window_set_fill_character(struct window *w)
1712 const char *value;
1713 struct utf8_data *ud;
1715 free(w->fill_character);
1716 w->fill_character = NULL;
1718 value = options_get_string(w->options, "fill-character");
1719 if (*value != '\0' && utf8_isvalid(value)) {
1720 ud = utf8_fromcstr(value);
1721 if (ud != NULL && ud[0].width == 1)
1722 w->fill_character = ud;
1726 void
1727 window_pane_default_cursor(struct window_pane *wp)
1729 screen_set_default_cursor(wp->screen, wp->options);
1733 window_pane_mode(struct window_pane *wp)
1735 if (TAILQ_FIRST(&wp->modes) != NULL) {
1736 if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode)
1737 return (WINDOW_PANE_COPY_MODE);
1738 if (TAILQ_FIRST(&wp->modes)->mode == &window_view_mode)
1739 return (WINDOW_PANE_VIEW_MODE);
1741 return (WINDOW_PANE_NO_MODE);
1744 /* Return 1 if scrollbar is or should be displayed. */
1746 window_pane_show_scrollbar(struct window_pane *wp, int sb_option)
1748 if (SCREEN_IS_ALTERNATE(wp->screen))
1749 return (0);
1750 if (sb_option == PANE_SCROLLBARS_ALWAYS ||
1751 (sb_option == PANE_SCROLLBARS_MODAL &&
1752 window_pane_mode(wp) != WINDOW_PANE_NO_MODE))
1753 return (1);
1754 return (0);