Merge branch 'obsd-master'
[tmux.git] / window.c
blob0e08444c335a3d3006e945ace216e07e6081ef72
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;
595 TAILQ_FOREACH(wp, &w->panes, entry) {
596 if (!window_pane_visible(wp))
597 continue;
598 if (x < wp->xoff || x > wp->xoff + wp->sx)
599 continue;
600 if (y < wp->yoff || y > wp->yoff + wp->sy)
601 continue;
602 return (wp);
604 return (NULL);
607 struct window_pane *
608 window_find_string(struct window *w, const char *s)
610 u_int x, y, top = 0, bottom = w->sy - 1;
611 int status;
613 x = w->sx / 2;
614 y = w->sy / 2;
616 status = options_get_number(w->options, "pane-border-status");
617 if (status == PANE_STATUS_TOP)
618 top++;
619 else if (status == PANE_STATUS_BOTTOM)
620 bottom--;
622 if (strcasecmp(s, "top") == 0)
623 y = top;
624 else if (strcasecmp(s, "bottom") == 0)
625 y = bottom;
626 else if (strcasecmp(s, "left") == 0)
627 x = 0;
628 else if (strcasecmp(s, "right") == 0)
629 x = w->sx - 1;
630 else if (strcasecmp(s, "top-left") == 0) {
631 x = 0;
632 y = top;
633 } else if (strcasecmp(s, "top-right") == 0) {
634 x = w->sx - 1;
635 y = top;
636 } else if (strcasecmp(s, "bottom-left") == 0) {
637 x = 0;
638 y = bottom;
639 } else if (strcasecmp(s, "bottom-right") == 0) {
640 x = w->sx - 1;
641 y = bottom;
642 } else
643 return (NULL);
645 return (window_get_active_at(w, x, y));
649 window_zoom(struct window_pane *wp)
651 struct window *w = wp->window;
652 struct window_pane *wp1;
654 if (w->flags & WINDOW_ZOOMED)
655 return (-1);
657 if (window_count_panes(w) == 1)
658 return (-1);
660 if (w->active != wp)
661 window_set_active_pane(w, wp, 1);
663 TAILQ_FOREACH(wp1, &w->panes, entry) {
664 wp1->saved_layout_cell = wp1->layout_cell;
665 wp1->layout_cell = NULL;
668 w->saved_layout_root = w->layout_root;
669 layout_init(w, wp);
670 w->flags |= WINDOW_ZOOMED;
671 notify_window("window-layout-changed", w);
673 return (0);
677 window_unzoom(struct window *w, int notify)
679 struct window_pane *wp;
681 if (!(w->flags & WINDOW_ZOOMED))
682 return (-1);
684 w->flags &= ~WINDOW_ZOOMED;
685 layout_free(w);
686 w->layout_root = w->saved_layout_root;
687 w->saved_layout_root = NULL;
689 TAILQ_FOREACH(wp, &w->panes, entry) {
690 wp->layout_cell = wp->saved_layout_cell;
691 wp->saved_layout_cell = NULL;
693 layout_fix_panes(w, NULL);
695 if (notify)
696 notify_window("window-layout-changed", w);
698 return (0);
702 window_push_zoom(struct window *w, int always, int flag)
704 log_debug("%s: @%u %d", __func__, w->id,
705 flag && (w->flags & WINDOW_ZOOMED));
706 if (flag && (always || (w->flags & WINDOW_ZOOMED)))
707 w->flags |= WINDOW_WASZOOMED;
708 else
709 w->flags &= ~WINDOW_WASZOOMED;
710 return (window_unzoom(w, 1) == 0);
714 window_pop_zoom(struct window *w)
716 log_debug("%s: @%u %d", __func__, w->id,
717 !!(w->flags & WINDOW_WASZOOMED));
718 if (w->flags & WINDOW_WASZOOMED)
719 return (window_zoom(w->active) == 0);
720 return (0);
723 struct window_pane *
724 window_add_pane(struct window *w, struct window_pane *other, u_int hlimit,
725 int flags)
727 struct window_pane *wp;
729 if (other == NULL)
730 other = w->active;
732 wp = window_pane_create(w, w->sx, w->sy, hlimit);
733 if (TAILQ_EMPTY(&w->panes)) {
734 log_debug("%s: @%u at start", __func__, w->id);
735 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
736 } else if (flags & SPAWN_BEFORE) {
737 log_debug("%s: @%u before %%%u", __func__, w->id, wp->id);
738 if (flags & SPAWN_FULLSIZE)
739 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
740 else
741 TAILQ_INSERT_BEFORE(other, wp, entry);
742 } else {
743 log_debug("%s: @%u after %%%u", __func__, w->id, wp->id);
744 if (flags & SPAWN_FULLSIZE)
745 TAILQ_INSERT_TAIL(&w->panes, wp, entry);
746 else
747 TAILQ_INSERT_AFTER(&w->panes, other, wp, entry);
749 return (wp);
752 void
753 window_lost_pane(struct window *w, struct window_pane *wp)
755 log_debug("%s: @%u pane %%%u", __func__, w->id, wp->id);
757 if (wp == marked_pane.wp)
758 server_clear_marked();
760 window_pane_stack_remove(&w->last_panes, wp);
761 if (wp == w->active) {
762 w->active = TAILQ_FIRST(&w->last_panes);
763 if (w->active == NULL) {
764 w->active = TAILQ_PREV(wp, window_panes, entry);
765 if (w->active == NULL)
766 w->active = TAILQ_NEXT(wp, entry);
768 if (w->active != NULL) {
769 window_pane_stack_remove(&w->last_panes, w->active);
770 w->active->flags |= PANE_CHANGED;
771 notify_window("window-pane-changed", w);
772 window_update_focus(w);
777 void
778 window_remove_pane(struct window *w, struct window_pane *wp)
780 window_lost_pane(w, wp);
782 TAILQ_REMOVE(&w->panes, wp, entry);
783 window_pane_destroy(wp);
786 struct window_pane *
787 window_pane_at_index(struct window *w, u_int idx)
789 struct window_pane *wp;
790 u_int n;
792 n = options_get_number(w->options, "pane-base-index");
793 TAILQ_FOREACH(wp, &w->panes, entry) {
794 if (n == idx)
795 return (wp);
796 n++;
798 return (NULL);
801 struct window_pane *
802 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
804 for (; n > 0; n--) {
805 if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
806 wp = TAILQ_FIRST(&w->panes);
809 return (wp);
812 struct window_pane *
813 window_pane_previous_by_number(struct window *w, struct window_pane *wp,
814 u_int n)
816 for (; n > 0; n--) {
817 if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
818 wp = TAILQ_LAST(&w->panes, window_panes);
821 return (wp);
825 window_pane_index(struct window_pane *wp, u_int *i)
827 struct window_pane *wq;
828 struct window *w = wp->window;
830 *i = options_get_number(w->options, "pane-base-index");
831 TAILQ_FOREACH(wq, &w->panes, entry) {
832 if (wp == wq) {
833 return (0);
835 (*i)++;
838 return (-1);
841 u_int
842 window_count_panes(struct window *w)
844 struct window_pane *wp;
845 u_int n;
847 n = 0;
848 TAILQ_FOREACH(wp, &w->panes, entry)
849 n++;
850 return (n);
853 void
854 window_destroy_panes(struct window *w)
856 struct window_pane *wp;
858 while (!TAILQ_EMPTY(&w->last_panes)) {
859 wp = TAILQ_FIRST(&w->last_panes);
860 window_pane_stack_remove(&w->last_panes, wp);
863 while (!TAILQ_EMPTY(&w->panes)) {
864 wp = TAILQ_FIRST(&w->panes);
865 TAILQ_REMOVE(&w->panes, wp, entry);
866 window_pane_destroy(wp);
870 const char *
871 window_printable_flags(struct winlink *wl, int escape)
873 struct session *s = wl->session;
874 static char flags[32];
875 int pos;
877 pos = 0;
878 if (wl->flags & WINLINK_ACTIVITY) {
879 flags[pos++] = '#';
880 if (escape)
881 flags[pos++] = '#';
883 if (wl->flags & WINLINK_BELL)
884 flags[pos++] = '!';
885 if (wl->flags & WINLINK_SILENCE)
886 flags[pos++] = '~';
887 if (wl == s->curw)
888 flags[pos++] = '*';
889 if (wl == TAILQ_FIRST(&s->lastw))
890 flags[pos++] = '-';
891 if (server_check_marked() && wl == marked_pane.wl)
892 flags[pos++] = 'M';
893 if (wl->window->flags & WINDOW_ZOOMED)
894 flags[pos++] = 'Z';
895 flags[pos] = '\0';
896 return (flags);
899 struct window_pane *
900 window_pane_find_by_id_str(const char *s)
902 const char *errstr;
903 u_int id;
905 if (*s != '%')
906 return (NULL);
908 id = strtonum(s + 1, 0, UINT_MAX, &errstr);
909 if (errstr != NULL)
910 return (NULL);
911 return (window_pane_find_by_id(id));
914 struct window_pane *
915 window_pane_find_by_id(u_int id)
917 struct window_pane wp;
919 wp.id = id;
920 return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
923 static struct window_pane *
924 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
926 struct window_pane *wp;
927 char host[HOST_NAME_MAX + 1];
929 wp = xcalloc(1, sizeof *wp);
930 wp->window = w;
931 wp->options = options_create(w->options);
932 wp->flags = PANE_STYLECHANGED;
934 wp->id = next_window_pane_id++;
935 RB_INSERT(window_pane_tree, &all_window_panes, wp);
937 wp->fd = -1;
939 TAILQ_INIT(&wp->modes);
941 TAILQ_INIT (&wp->resize_queue);
943 wp->sx = sx;
944 wp->sy = sy;
946 wp->pipe_fd = -1;
948 wp->control_bg = -1;
949 wp->control_fg = -1;
951 colour_palette_init(&wp->palette);
952 colour_palette_from_option(&wp->palette, wp->options);
954 screen_init(&wp->base, sx, sy, hlimit);
955 wp->screen = &wp->base;
956 window_pane_default_cursor(wp);
958 screen_init(&wp->status_screen, 1, 1, 0);
960 if (gethostname(host, sizeof host) == 0)
961 screen_set_title(&wp->base, host);
963 return (wp);
966 static void
967 window_pane_destroy(struct window_pane *wp)
969 struct window_pane_resize *r;
970 struct window_pane_resize *r1;
972 window_pane_reset_mode_all(wp);
973 free(wp->searchstr);
975 if (wp->fd != -1) {
976 #ifdef HAVE_UTEMPTER
977 utempter_remove_record(wp->fd);
978 #endif
979 bufferevent_free(wp->event);
980 close(wp->fd);
982 if (wp->ictx != NULL)
983 input_free(wp->ictx);
985 screen_free(&wp->status_screen);
987 screen_free(&wp->base);
989 if (wp->pipe_fd != -1) {
990 bufferevent_free(wp->pipe_event);
991 close(wp->pipe_fd);
994 if (event_initialized(&wp->resize_timer))
995 event_del(&wp->resize_timer);
996 TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) {
997 TAILQ_REMOVE(&wp->resize_queue, r, entry);
998 free(r);
1001 RB_REMOVE(window_pane_tree, &all_window_panes, wp);
1003 options_free(wp->options);
1004 free((void *)wp->cwd);
1005 free(wp->shell);
1006 cmd_free_argv(wp->argc, wp->argv);
1007 colour_palette_free(&wp->palette);
1008 free(wp);
1011 static void
1012 window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
1014 struct window_pane *wp = data;
1015 struct evbuffer *evb = wp->event->input;
1016 struct window_pane_offset *wpo = &wp->pipe_offset;
1017 size_t size = EVBUFFER_LENGTH(evb);
1018 char *new_data;
1019 size_t new_size;
1020 struct client *c;
1022 if (wp->pipe_fd != -1) {
1023 new_data = window_pane_get_new_data(wp, wpo, &new_size);
1024 if (new_size > 0) {
1025 bufferevent_write(wp->pipe_event, new_data, new_size);
1026 window_pane_update_used_data(wp, wpo, new_size);
1030 log_debug("%%%u has %zu bytes", wp->id, size);
1031 TAILQ_FOREACH(c, &clients, entry) {
1032 if (c->session != NULL && (c->flags & CLIENT_CONTROL))
1033 control_write_output(c, wp);
1035 input_parse_pane(wp);
1036 bufferevent_disable(wp->event, EV_READ);
1039 static void
1040 window_pane_error_callback(__unused struct bufferevent *bufev,
1041 __unused short what, void *data)
1043 struct window_pane *wp = data;
1045 log_debug("%%%u error", wp->id);
1046 wp->flags |= PANE_EXITED;
1048 if (window_pane_destroy_ready(wp))
1049 server_destroy_pane(wp, 1);
1052 void
1053 window_pane_set_event(struct window_pane *wp)
1055 setblocking(wp->fd, 0);
1057 wp->event = bufferevent_new(wp->fd, window_pane_read_callback,
1058 NULL, window_pane_error_callback, wp);
1059 if (wp->event == NULL)
1060 fatalx("out of memory");
1061 wp->ictx = input_init(wp, wp->event, &wp->palette);
1063 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1066 void
1067 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
1069 struct window_mode_entry *wme;
1070 struct window_pane_resize *r;
1072 if (sx == wp->sx && sy == wp->sy)
1073 return;
1075 r = xmalloc(sizeof *r);
1076 r->sx = sx;
1077 r->sy = sy;
1078 r->osx = wp->sx;
1079 r->osy = wp->sy;
1080 TAILQ_INSERT_TAIL (&wp->resize_queue, r, entry);
1082 wp->sx = sx;
1083 wp->sy = sy;
1085 log_debug("%s: %%%u resize %ux%u", __func__, wp->id, sx, sy);
1086 screen_resize(&wp->base, sx, sy, wp->base.saved_grid == NULL);
1088 wme = TAILQ_FIRST(&wp->modes);
1089 if (wme != NULL && wme->mode->resize != NULL)
1090 wme->mode->resize(wme, sx, sy);
1094 window_pane_set_mode(struct window_pane *wp, struct window_pane *swp,
1095 const struct window_mode *mode, struct cmd_find_state *fs,
1096 struct args *args)
1098 struct window_mode_entry *wme;
1100 if (!TAILQ_EMPTY(&wp->modes) && TAILQ_FIRST(&wp->modes)->mode == mode)
1101 return (1);
1103 TAILQ_FOREACH(wme, &wp->modes, entry) {
1104 if (wme->mode == mode)
1105 break;
1107 if (wme != NULL) {
1108 TAILQ_REMOVE(&wp->modes, wme, entry);
1109 TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1110 } else {
1111 wme = xcalloc(1, sizeof *wme);
1112 wme->wp = wp;
1113 wme->swp = swp;
1114 wme->mode = mode;
1115 wme->prefix = 1;
1116 TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1117 wme->screen = wme->mode->init(wme, fs, args);
1120 wp->screen = wme->screen;
1121 wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1123 server_redraw_window_borders(wp->window);
1124 server_status_window(wp->window);
1125 notify_pane("pane-mode-changed", wp);
1127 return (0);
1130 void
1131 window_pane_reset_mode(struct window_pane *wp)
1133 struct window_mode_entry *wme, *next;
1135 if (TAILQ_EMPTY(&wp->modes))
1136 return;
1138 wme = TAILQ_FIRST(&wp->modes);
1139 TAILQ_REMOVE(&wp->modes, wme, entry);
1140 wme->mode->free(wme);
1141 free(wme);
1143 next = TAILQ_FIRST(&wp->modes);
1144 if (next == NULL) {
1145 wp->flags &= ~PANE_UNSEENCHANGES;
1146 log_debug("%s: no next mode", __func__);
1147 wp->screen = &wp->base;
1148 } else {
1149 log_debug("%s: next mode is %s", __func__, next->mode->name);
1150 wp->screen = next->screen;
1151 if (next->mode->resize != NULL)
1152 next->mode->resize(next, wp->sx, wp->sy);
1154 wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1156 server_redraw_window_borders(wp->window);
1157 server_status_window(wp->window);
1158 notify_pane("pane-mode-changed", wp);
1161 void
1162 window_pane_reset_mode_all(struct window_pane *wp)
1164 while (!TAILQ_EMPTY(&wp->modes))
1165 window_pane_reset_mode(wp);
1168 static void
1169 window_pane_copy_paste(struct window_pane *wp, char *buf, size_t len)
1171 struct window_pane *loop;
1173 TAILQ_FOREACH(loop, &wp->window->panes, entry) {
1174 if (loop != wp &&
1175 TAILQ_EMPTY(&loop->modes) &&
1176 loop->fd != -1 &&
1177 (~loop->flags & PANE_INPUTOFF) &&
1178 window_pane_visible(loop) &&
1179 options_get_number(loop->options, "synchronize-panes")) {
1180 log_debug("%s: %.*s", __func__, (int)len, buf);
1181 bufferevent_write(loop->event, buf, len);
1186 static void
1187 window_pane_copy_key(struct window_pane *wp, key_code key)
1189 struct window_pane *loop;
1191 TAILQ_FOREACH(loop, &wp->window->panes, entry) {
1192 if (loop != wp &&
1193 TAILQ_EMPTY(&loop->modes) &&
1194 loop->fd != -1 &&
1195 (~loop->flags & PANE_INPUTOFF) &&
1196 window_pane_visible(loop) &&
1197 options_get_number(loop->options, "synchronize-panes"))
1198 input_key_pane(loop, key, NULL);
1202 void
1203 window_pane_paste(struct window_pane *wp, char *buf, size_t len)
1205 if (!TAILQ_EMPTY(&wp->modes))
1206 return;
1208 if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1209 return;
1211 log_debug("%s: %.*s", __func__, (int)len, buf);
1212 bufferevent_write(wp->event, buf, len);
1214 if (options_get_number(wp->options, "synchronize-panes"))
1215 window_pane_copy_paste(wp, buf, len);
1219 window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1220 struct winlink *wl, key_code key, struct mouse_event *m)
1222 struct window_mode_entry *wme;
1224 if (KEYC_IS_MOUSE(key) && m == NULL)
1225 return (-1);
1227 wme = TAILQ_FIRST(&wp->modes);
1228 if (wme != NULL) {
1229 if (wme->mode->key != NULL && c != NULL) {
1230 key &= ~KEYC_MASK_FLAGS;
1231 wme->mode->key(wme, c, s, wl, key, m);
1233 return (0);
1236 if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1237 return (0);
1239 if (input_key_pane(wp, key, m) != 0)
1240 return (-1);
1242 if (KEYC_IS_MOUSE(key))
1243 return (0);
1244 if (options_get_number(wp->options, "synchronize-panes"))
1245 window_pane_copy_key(wp, key);
1246 return (0);
1250 window_pane_visible(struct window_pane *wp)
1252 if (~wp->window->flags & WINDOW_ZOOMED)
1253 return (1);
1254 return (wp == wp->window->active);
1258 window_pane_exited(struct window_pane *wp)
1260 return (wp->fd == -1 || (wp->flags & PANE_EXITED));
1263 u_int
1264 window_pane_search(struct window_pane *wp, const char *term, int regex,
1265 int ignore)
1267 struct screen *s = &wp->base;
1268 regex_t r;
1269 char *new = NULL, *line;
1270 u_int i;
1271 int flags = 0, found;
1272 size_t n;
1274 if (!regex) {
1275 if (ignore)
1276 flags |= FNM_CASEFOLD;
1277 xasprintf(&new, "*%s*", term);
1278 } else {
1279 if (ignore)
1280 flags |= REG_ICASE;
1281 if (regcomp(&r, term, flags|REG_EXTENDED) != 0)
1282 return (0);
1285 for (i = 0; i < screen_size_y(s); i++) {
1286 line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1287 for (n = strlen(line); n > 0; n--) {
1288 if (!isspace((u_char)line[n - 1]))
1289 break;
1290 line[n - 1] = '\0';
1292 log_debug("%s: %s", __func__, line);
1293 if (!regex)
1294 found = (fnmatch(new, line, flags) == 0);
1295 else
1296 found = (regexec(&r, line, 0, NULL, 0) == 0);
1297 free(line);
1298 if (found)
1299 break;
1301 if (!regex)
1302 free(new);
1303 else
1304 regfree(&r);
1306 if (i == screen_size_y(s))
1307 return (0);
1308 return (i + 1);
1311 /* Get MRU pane from a list. */
1312 static struct window_pane *
1313 window_pane_choose_best(struct window_pane **list, u_int size)
1315 struct window_pane *next, *best;
1316 u_int i;
1318 if (size == 0)
1319 return (NULL);
1321 best = list[0];
1322 for (i = 1; i < size; i++) {
1323 next = list[i];
1324 if (next->active_point > best->active_point)
1325 best = next;
1327 return (best);
1331 * Find the pane directly above another. We build a list of those adjacent to
1332 * top edge and then choose the best.
1334 struct window_pane *
1335 window_pane_find_up(struct window_pane *wp)
1337 struct window *w;
1338 struct window_pane *next, *best, **list;
1339 u_int edge, left, right, end, size;
1340 int status, found;
1342 if (wp == NULL)
1343 return (NULL);
1344 w = wp->window;
1345 status = options_get_number(w->options, "pane-border-status");
1347 list = NULL;
1348 size = 0;
1350 edge = wp->yoff;
1351 if (status == PANE_STATUS_TOP) {
1352 if (edge == 1)
1353 edge = w->sy + 1;
1354 } else if (status == PANE_STATUS_BOTTOM) {
1355 if (edge == 0)
1356 edge = w->sy;
1357 } else {
1358 if (edge == 0)
1359 edge = w->sy + 1;
1362 left = wp->xoff;
1363 right = wp->xoff + wp->sx;
1365 TAILQ_FOREACH(next, &w->panes, entry) {
1366 if (next == wp)
1367 continue;
1368 if (next->yoff + next->sy + 1 != edge)
1369 continue;
1370 end = next->xoff + next->sx - 1;
1372 found = 0;
1373 if (next->xoff < left && end > right)
1374 found = 1;
1375 else if (next->xoff >= left && next->xoff <= right)
1376 found = 1;
1377 else if (end >= left && end <= right)
1378 found = 1;
1379 if (!found)
1380 continue;
1381 list = xreallocarray(list, size + 1, sizeof *list);
1382 list[size++] = next;
1385 best = window_pane_choose_best(list, size);
1386 free(list);
1387 return (best);
1390 /* Find the pane directly below another. */
1391 struct window_pane *
1392 window_pane_find_down(struct window_pane *wp)
1394 struct window *w;
1395 struct window_pane *next, *best, **list;
1396 u_int edge, left, right, end, size;
1397 int status, found;
1399 if (wp == NULL)
1400 return (NULL);
1401 w = wp->window;
1402 status = options_get_number(w->options, "pane-border-status");
1404 list = NULL;
1405 size = 0;
1407 edge = wp->yoff + wp->sy + 1;
1408 if (status == PANE_STATUS_TOP) {
1409 if (edge >= w->sy)
1410 edge = 1;
1411 } else if (status == PANE_STATUS_BOTTOM) {
1412 if (edge >= w->sy - 1)
1413 edge = 0;
1414 } else {
1415 if (edge >= w->sy)
1416 edge = 0;
1419 left = wp->xoff;
1420 right = wp->xoff + wp->sx;
1422 TAILQ_FOREACH(next, &w->panes, entry) {
1423 if (next == wp)
1424 continue;
1425 if (next->yoff != edge)
1426 continue;
1427 end = next->xoff + next->sx - 1;
1429 found = 0;
1430 if (next->xoff < left && end > right)
1431 found = 1;
1432 else if (next->xoff >= left && next->xoff <= right)
1433 found = 1;
1434 else if (end >= left && end <= right)
1435 found = 1;
1436 if (!found)
1437 continue;
1438 list = xreallocarray(list, size + 1, sizeof *list);
1439 list[size++] = next;
1442 best = window_pane_choose_best(list, size);
1443 free(list);
1444 return (best);
1447 /* Find the pane directly to the left of another. */
1448 struct window_pane *
1449 window_pane_find_left(struct window_pane *wp)
1451 struct window *w;
1452 struct window_pane *next, *best, **list;
1453 u_int edge, top, bottom, end, size;
1454 int found;
1456 if (wp == NULL)
1457 return (NULL);
1458 w = wp->window;
1460 list = NULL;
1461 size = 0;
1463 edge = wp->xoff;
1464 if (edge == 0)
1465 edge = w->sx + 1;
1467 top = wp->yoff;
1468 bottom = wp->yoff + wp->sy;
1470 TAILQ_FOREACH(next, &w->panes, entry) {
1471 if (next == wp)
1472 continue;
1473 if (next->xoff + next->sx + 1 != edge)
1474 continue;
1475 end = next->yoff + next->sy - 1;
1477 found = 0;
1478 if (next->yoff < top && end > bottom)
1479 found = 1;
1480 else if (next->yoff >= top && next->yoff <= bottom)
1481 found = 1;
1482 else if (end >= top && end <= bottom)
1483 found = 1;
1484 if (!found)
1485 continue;
1486 list = xreallocarray(list, size + 1, sizeof *list);
1487 list[size++] = next;
1490 best = window_pane_choose_best(list, size);
1491 free(list);
1492 return (best);
1495 /* Find the pane directly to the right of another. */
1496 struct window_pane *
1497 window_pane_find_right(struct window_pane *wp)
1499 struct window *w;
1500 struct window_pane *next, *best, **list;
1501 u_int edge, top, bottom, end, size;
1502 int found;
1504 if (wp == NULL)
1505 return (NULL);
1506 w = wp->window;
1508 list = NULL;
1509 size = 0;
1511 edge = wp->xoff + wp->sx + 1;
1512 if (edge >= w->sx)
1513 edge = 0;
1515 top = wp->yoff;
1516 bottom = wp->yoff + wp->sy;
1518 TAILQ_FOREACH(next, &w->panes, entry) {
1519 if (next == wp)
1520 continue;
1521 if (next->xoff != edge)
1522 continue;
1523 end = next->yoff + next->sy - 1;
1525 found = 0;
1526 if (next->yoff < top && end > bottom)
1527 found = 1;
1528 else if (next->yoff >= top && next->yoff <= bottom)
1529 found = 1;
1530 else if (end >= top && end <= bottom)
1531 found = 1;
1532 if (!found)
1533 continue;
1534 list = xreallocarray(list, size + 1, sizeof *list);
1535 list[size++] = next;
1538 best = window_pane_choose_best(list, size);
1539 free(list);
1540 return (best);
1543 void
1544 window_pane_stack_push(struct window_panes *stack, struct window_pane *wp)
1546 if (wp != NULL) {
1547 window_pane_stack_remove(stack, wp);
1548 TAILQ_INSERT_HEAD(stack, wp, sentry);
1549 wp->flags |= PANE_VISITED;
1553 void
1554 window_pane_stack_remove(struct window_panes *stack, struct window_pane *wp)
1556 if (wp != NULL && (wp->flags & PANE_VISITED)) {
1557 TAILQ_REMOVE(stack, wp, sentry);
1558 wp->flags &= ~PANE_VISITED;
1562 /* Clear alert flags for a winlink */
1563 void
1564 winlink_clear_flags(struct winlink *wl)
1566 struct winlink *loop;
1568 wl->window->flags &= ~WINDOW_ALERTFLAGS;
1569 TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
1570 if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
1571 loop->flags &= ~WINLINK_ALERTFLAGS;
1572 server_status_session(loop->session);
1577 /* Shuffle window indexes up. */
1579 winlink_shuffle_up(struct session *s, struct winlink *wl, int before)
1581 int idx, last;
1583 if (wl == NULL)
1584 return (-1);
1585 if (before)
1586 idx = wl->idx;
1587 else
1588 idx = wl->idx + 1;
1590 /* Find the next free index. */
1591 for (last = idx; last < INT_MAX; last++) {
1592 if (winlink_find_by_index(&s->windows, last) == NULL)
1593 break;
1595 if (last == INT_MAX)
1596 return (-1);
1598 /* Move everything from last - 1 to idx up a bit. */
1599 for (; last > idx; last--) {
1600 wl = winlink_find_by_index(&s->windows, last - 1);
1601 RB_REMOVE(winlinks, &s->windows, wl);
1602 wl->idx++;
1603 RB_INSERT(winlinks, &s->windows, wl);
1606 return (idx);
1609 static void
1610 window_pane_input_callback(struct client *c, __unused const char *path,
1611 int error, int closed, struct evbuffer *buffer, void *data)
1613 struct window_pane_input_data *cdata = data;
1614 struct window_pane *wp;
1615 u_char *buf = EVBUFFER_DATA(buffer);
1616 size_t len = EVBUFFER_LENGTH(buffer);
1618 wp = window_pane_find_by_id(cdata->wp);
1619 if (cdata->file != NULL && (wp == NULL || c->flags & CLIENT_DEAD)) {
1620 if (wp == NULL) {
1621 c->retval = 1;
1622 c->flags |= CLIENT_EXIT;
1624 file_cancel(cdata->file);
1625 } else if (cdata->file == NULL || closed || error != 0) {
1626 cmdq_continue(cdata->item);
1627 server_client_unref(c);
1628 free(cdata);
1629 } else
1630 input_parse_buffer(wp, buf, len);
1631 evbuffer_drain(buffer, len);
1635 window_pane_start_input(struct window_pane *wp, struct cmdq_item *item,
1636 char **cause)
1638 struct client *c = cmdq_get_client(item);
1639 struct window_pane_input_data *cdata;
1641 if (~wp->flags & PANE_EMPTY) {
1642 *cause = xstrdup("pane is not empty");
1643 return (-1);
1645 if (c->flags & (CLIENT_DEAD|CLIENT_EXITED))
1646 return (1);
1647 if (c->session != NULL)
1648 return (1);
1650 cdata = xmalloc(sizeof *cdata);
1651 cdata->item = item;
1652 cdata->wp = wp->id;
1653 cdata->file = file_read(c, "-", window_pane_input_callback, cdata);
1654 c->references++;
1656 return (0);
1659 void *
1660 window_pane_get_new_data(struct window_pane *wp,
1661 struct window_pane_offset *wpo, size_t *size)
1663 size_t used = wpo->used - wp->base_offset;
1665 *size = EVBUFFER_LENGTH(wp->event->input) - used;
1666 return (EVBUFFER_DATA(wp->event->input) + used);
1669 void
1670 window_pane_update_used_data(struct window_pane *wp,
1671 struct window_pane_offset *wpo, size_t size)
1673 size_t used = wpo->used - wp->base_offset;
1675 if (size > EVBUFFER_LENGTH(wp->event->input) - used)
1676 size = EVBUFFER_LENGTH(wp->event->input) - used;
1677 wpo->used += size;
1680 void
1681 window_set_fill_character(struct window *w)
1683 const char *value;
1684 struct utf8_data *ud;
1686 free(w->fill_character);
1687 w->fill_character = NULL;
1689 value = options_get_string(w->options, "fill-character");
1690 if (*value != '\0' && utf8_isvalid(value)) {
1691 ud = utf8_fromcstr(value);
1692 if (ud != NULL && ud[0].width == 1)
1693 w->fill_character = ud;
1697 void
1698 window_pane_default_cursor(struct window_pane *wp)
1700 screen_set_default_cursor(wp->screen, wp->options);
1704 window_pane_mode(struct window_pane *wp)
1706 if (TAILQ_FIRST(&wp->modes) != NULL) {
1707 if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode)
1708 return (WINDOW_PANE_COPY_MODE);
1709 if (TAILQ_FIRST(&wp->modes)->mode == &window_view_mode)
1710 return (WINDOW_PANE_VIEW_MODE);
1712 return (WINDOW_PANE_NO_MODE);