[BACK]Return to window.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/window.c, Revision 1.164

1.164   ! nicm        1: /* $OpenBSD: window.c,v 1.163 2016/06/16 10:55:47 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.156     nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        5:  *
                      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.
                      9:  *
                     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.
                     17:  */
                     18:
                     19: #include <sys/types.h>
1.162     nicm       20: #include <sys/ioctl.h>
1.1       nicm       21:
                     22: #include <errno.h>
                     23: #include <fcntl.h>
1.5       nicm       24: #include <fnmatch.h>
1.1       nicm       25: #include <stdint.h>
                     26: #include <stdlib.h>
                     27: #include <string.h>
                     28: #include <termios.h>
1.163     nicm       29: #include <time.h>
1.1       nicm       30: #include <unistd.h>
                     31: #include <util.h>
                     32:
                     33: #include "tmux.h"
                     34:
                     35: /*
1.14      nicm       36:  * Each window is attached to a number of panes, each of which is a pty. This
1.1       nicm       37:  * file contains code to handle them.
                     38:  *
                     39:  * A pane has two buffers attached, these are filled and emptied by the main
                     40:  * server poll loop. Output data is received from pty's in screen format,
                     41:  * translated and returned as a series of escape sequences and strings via
                     42:  * input_parse (in input.c). Input data is received as key codes and written
                     43:  * directly via input_key.
                     44:  *
                     45:  * Each pane also has a "virtual" screen (screen.c) which contains the current
                     46:  * state and is redisplayed when the window is reattached to a client.
                     47:  *
                     48:  * Windows are stored directly on a global array and wrapped in any number of
                     49:  * winlink structs to be linked onto local session RB trees. A reference count
                     50:  * is maintained and a window removed from the global list and destroyed when
                     51:  * it reaches zero.
                     52:  */
1.124     nicm       53:
1.1       nicm       54: /* Global window list. */
                     55: struct windows windows;
                     56:
1.64      nicm       57: /* Global panes tree. */
                     58: struct window_pane_tree all_window_panes;
1.71      nicm       59: u_int  next_window_pane_id;
                     60: u_int  next_window_id;
1.109     nicm       61: u_int  next_active_point;
1.100     nicm       62:
1.131     nicm       63: void   window_pane_timer_callback(int, short, void *);
1.37      nicm       64: void   window_pane_read_callback(struct bufferevent *, void *);
                     65: void   window_pane_error_callback(struct bufferevent *, short, void *);
                     66:
1.126     nicm       67: struct window_pane *window_pane_choose_best(struct window_pane **, u_int);
1.109     nicm       68:
1.122     nicm       69: RB_GENERATE(windows, window, entry, window_cmp);
                     70:
                     71: int
                     72: window_cmp(struct window *w1, struct window *w2)
                     73: {
                     74:        return (w1->id - w2->id);
                     75: }
                     76:
1.1       nicm       77: RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
                     78:
                     79: int
                     80: winlink_cmp(struct winlink *wl1, struct winlink *wl2)
                     81: {
                     82:        return (wl1->idx - wl2->idx);
1.12      nicm       83: }
                     84:
1.64      nicm       85: RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
                     86:
                     87: int
                     88: window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
                     89: {
                     90:        return (wp1->id - wp2->id);
                     91: }
                     92:
1.12      nicm       93: struct winlink *
                     94: winlink_find_by_window(struct winlinks *wwl, struct window *w)
                     95: {
                     96:        struct winlink  *wl;
                     97:
                     98:        RB_FOREACH(wl, winlinks, wwl) {
                     99:                if (wl->window == w)
                    100:                        return (wl);
                    101:        }
                    102:
                    103:        return (NULL);
1.1       nicm      104: }
                    105:
                    106: struct winlink *
                    107: winlink_find_by_index(struct winlinks *wwl, int idx)
                    108: {
                    109:        struct winlink  wl;
                    110:
                    111:        if (idx < 0)
                    112:                fatalx("bad index");
                    113:
                    114:        wl.idx = idx;
                    115:        return (RB_FIND(winlinks, wwl, &wl));
                    116: }
                    117:
1.71      nicm      118: struct winlink *
                    119: winlink_find_by_window_id(struct winlinks *wwl, u_int id)
                    120: {
                    121:        struct winlink *wl;
                    122:
                    123:        RB_FOREACH(wl, winlinks, wwl) {
                    124:                if (wl->window->id == id)
                    125:                        return (wl);
                    126:        }
1.78      nicm      127:        return (NULL);
1.71      nicm      128: }
                    129:
1.1       nicm      130: int
1.22      nicm      131: winlink_next_index(struct winlinks *wwl, int idx)
1.1       nicm      132: {
1.22      nicm      133:        int     i;
1.1       nicm      134:
1.22      nicm      135:        i = idx;
                    136:        do {
1.1       nicm      137:                if (winlink_find_by_index(wwl, i) == NULL)
                    138:                        return (i);
1.22      nicm      139:                if (i == INT_MAX)
                    140:                        i = 0;
                    141:                else
                    142:                        i++;
                    143:        } while (i != idx);
                    144:        return (-1);
1.1       nicm      145: }
                    146:
                    147: u_int
                    148: winlink_count(struct winlinks *wwl)
                    149: {
                    150:        struct winlink  *wl;
                    151:        u_int            n;
                    152:
                    153:        n = 0;
                    154:        RB_FOREACH(wl, winlinks, wwl)
                    155:                n++;
                    156:
                    157:        return (n);
                    158: }
                    159:
                    160: struct winlink *
1.63      nicm      161: winlink_add(struct winlinks *wwl, int idx)
1.1       nicm      162: {
                    163:        struct winlink  *wl;
                    164:
1.22      nicm      165:        if (idx < 0) {
                    166:                if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
                    167:                        return (NULL);
                    168:        } else if (winlink_find_by_index(wwl, idx) != NULL)
1.1       nicm      169:                return (NULL);
                    170:
                    171:        wl = xcalloc(1, sizeof *wl);
                    172:        wl->idx = idx;
                    173:        RB_INSERT(winlinks, wwl, wl);
                    174:
1.63      nicm      175:        return (wl);
                    176: }
                    177:
                    178: void
                    179: winlink_set_window(struct winlink *wl, struct window *w)
                    180: {
                    181:        wl->window = w;
1.1       nicm      182:        w->references++;
                    183: }
                    184:
                    185: void
                    186: winlink_remove(struct winlinks *wwl, struct winlink *wl)
                    187: {
                    188:        struct window   *w = wl->window;
                    189:
                    190:        RB_REMOVE(winlinks, wwl, wl);
1.82      nicm      191:        free(wl->status_text);
                    192:        free(wl);
1.1       nicm      193:
1.84      nicm      194:        if (w != NULL)
                    195:                window_remove_ref(w);
1.1       nicm      196: }
                    197:
                    198: struct winlink *
1.41      nicm      199: winlink_next(struct winlink *wl)
1.1       nicm      200: {
                    201:        return (RB_NEXT(winlinks, wwl, wl));
                    202: }
                    203:
                    204: struct winlink *
1.41      nicm      205: winlink_previous(struct winlink *wl)
1.1       nicm      206: {
                    207:        return (RB_PREV(winlinks, wwl, wl));
1.52      nicm      208: }
                    209:
                    210: struct winlink *
1.53      nicm      211: winlink_next_by_number(struct winlink *wl, struct session *s, int n)
1.52      nicm      212: {
                    213:        for (; n > 0; n--) {
                    214:                if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
1.53      nicm      215:                        wl = RB_MIN(winlinks, &s->windows);
1.52      nicm      216:        }
                    217:
                    218:        return (wl);
                    219: }
                    220:
                    221: struct winlink *
1.53      nicm      222: winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
1.52      nicm      223: {
                    224:        for (; n > 0; n--) {
                    225:                if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
1.53      nicm      226:                        wl = RB_MAX(winlinks, &s->windows);
1.52      nicm      227:        }
                    228:
                    229:        return (wl);
1.1       nicm      230: }
                    231:
                    232: void
                    233: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
                    234: {
                    235:        if (wl == NULL)
                    236:                return;
                    237:
                    238:        winlink_stack_remove(stack, wl);
1.28      nicm      239:        TAILQ_INSERT_HEAD(stack, wl, sentry);
1.1       nicm      240: }
                    241:
                    242: void
                    243: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
                    244: {
                    245:        struct winlink  *wl2;
                    246:
                    247:        if (wl == NULL)
                    248:                return;
1.42      nicm      249:
1.28      nicm      250:        TAILQ_FOREACH(wl2, stack, sentry) {
1.1       nicm      251:                if (wl2 == wl) {
1.28      nicm      252:                        TAILQ_REMOVE(stack, wl, sentry);
1.1       nicm      253:                        return;
                    254:                }
                    255:        }
                    256: }
                    257:
                    258: struct window *
1.125     nicm      259: window_find_by_id_str(const char *s)
1.123     nicm      260: {
                    261:        const char      *errstr;
                    262:        u_int            id;
                    263:
                    264:        if (*s != '@')
                    265:                return (NULL);
                    266:
                    267:        id = strtonum(s + 1, 0, UINT_MAX, &errstr);
                    268:        if (errstr != NULL)
                    269:                return (NULL);
                    270:        return (window_find_by_id(id));
                    271: }
                    272:
                    273: struct window *
1.71      nicm      274: window_find_by_id(u_int id)
                    275: {
1.122     nicm      276:        struct window   w;
1.71      nicm      277:
1.122     nicm      278:        w.id = id;
                    279:        return (RB_FIND(windows, &windows, &w));
1.71      nicm      280: }
                    281:
1.143     nicm      282: void
                    283: window_update_activity(struct window *w)
                    284: {
                    285:        gettimeofday(&w->activity_time, NULL);
                    286:        alerts_queue(w, WINDOW_ACTIVITY);
                    287: }
                    288:
1.71      nicm      289: struct window *
1.1       nicm      290: window_create1(u_int sx, u_int sy)
                    291: {
                    292:        struct window   *w;
                    293:
1.38      nicm      294:        w = xcalloc(1, sizeof *w);
1.1       nicm      295:        w->name = NULL;
1.160     nicm      296:        w->flags = WINDOW_STYLECHANGED;
1.1       nicm      297:
                    298:        TAILQ_INIT(&w->panes);
                    299:        w->active = NULL;
1.14      nicm      300:
1.17      nicm      301:        w->lastlayout = -1;
1.14      nicm      302:        w->layout_root = NULL;
1.42      nicm      303:
1.1       nicm      304:        w->sx = sx;
                    305:        w->sy = sy;
1.133     nicm      306:
1.146     nicm      307:        w->options = options_create(global_w_options);
1.1       nicm      308:
                    309:        w->references = 0;
                    310:
1.122     nicm      311:        w->id = next_window_id++;
1.129     nicm      312:        RB_INSERT(windows, &windows, w);
1.122     nicm      313:
1.143     nicm      314:        window_update_activity(w);
                    315:
1.1       nicm      316:        return (w);
                    317: }
                    318:
                    319: struct window *
1.110     nicm      320: window_create(const char *name, int argc, char **argv, const char *path,
1.147     nicm      321:     const char *shell, const char *cwd, struct environ *env,
                    322:     struct termios *tio, u_int sx, u_int sy, u_int hlimit, char **cause)
1.1       nicm      323: {
1.14      nicm      324:        struct window           *w;
                    325:        struct window_pane      *wp;
1.1       nicm      326:
                    327:        w = window_create1(sx, sy);
1.161     nicm      328:        wp = window_add_pane(w, NULL, hlimit);
1.93      nicm      329:        layout_init(w, wp);
1.91      nicm      330:
1.110     nicm      331:        if (window_pane_spawn(wp, argc, argv, path, shell, cwd, env, tio,
1.107     nicm      332:            cause) != 0) {
1.1       nicm      333:                window_destroy(w);
                    334:                return (NULL);
                    335:        }
1.91      nicm      336:
1.1       nicm      337:        w->active = TAILQ_FIRST(&w->panes);
                    338:        if (name != NULL) {
                    339:                w->name = xstrdup(name);
1.146     nicm      340:                options_set_number(w->options, "automatic-rename", 0);
1.1       nicm      341:        } else
                    342:                w->name = default_window_name(w);
1.91      nicm      343:
1.1       nicm      344:        return (w);
                    345: }
                    346:
                    347: void
                    348: window_destroy(struct window *w)
                    349: {
1.122     nicm      350:        RB_REMOVE(windows, &windows, w);
1.1       nicm      351:
1.14      nicm      352:        if (w->layout_root != NULL)
1.135     nicm      353:                layout_free_cell(w->layout_root);
                    354:        if (w->saved_layout_root != NULL)
                    355:                layout_free_cell(w->saved_layout_root);
1.127     nicm      356:        free(w->old_layout);
1.139     nicm      357:
1.141     nicm      358:        if (event_initialized(&w->name_event))
                    359:                evtimer_del(&w->name_event);
1.38      nicm      360:
1.143     nicm      361:        if (event_initialized(&w->alerts_timer))
                    362:                evtimer_del(&w->alerts_timer);
                    363:
1.146     nicm      364:        options_free(w->options);
1.1       nicm      365:
                    366:        window_destroy_panes(w);
                    367:
1.82      nicm      368:        free(w->name);
                    369:        free(w);
1.84      nicm      370: }
                    371:
                    372: void
                    373: window_remove_ref(struct window *w)
                    374: {
                    375:        if (w->references == 0)
                    376:                fatal("bad reference count");
                    377:        w->references--;
                    378:        if (w->references == 0)
                    379:                window_destroy(w);
1.72      nicm      380: }
                    381:
                    382: void
                    383: window_set_name(struct window *w, const char *new_name)
                    384: {
1.82      nicm      385:        free(w->name);
1.72      nicm      386:        w->name = xstrdup(new_name);
1.74      nicm      387:        notify_window_renamed(w);
1.1       nicm      388: }
                    389:
1.15      nicm      390: void
1.1       nicm      391: window_resize(struct window *w, u_int sx, u_int sy)
                    392: {
                    393:        w->sx = sx;
                    394:        w->sy = sy;
                    395: }
                    396:
1.114     nicm      397: int
1.118     nicm      398: window_has_pane(struct window *w, struct window_pane *wp)
                    399: {
                    400:        struct window_pane      *wp1;
                    401:
                    402:        TAILQ_FOREACH(wp1, &w->panes, entry) {
                    403:                if (wp1 == wp)
                    404:                        return (1);
                    405:        }
                    406:        return (0);
                    407: }
                    408:
                    409: int
1.1       nicm      410: window_set_active_pane(struct window *w, struct window_pane *wp)
                    411: {
1.59      nicm      412:        if (wp == w->active)
1.114     nicm      413:                return (0);
1.58      nicm      414:        w->last = w->active;
1.1       nicm      415:        w->active = wp;
1.10      nicm      416:        while (!window_pane_visible(w->active)) {
1.1       nicm      417:                w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10      nicm      418:                if (w->active == NULL)
                    419:                        w->active = TAILQ_LAST(&w->panes, window_panes);
                    420:                if (w->active == wp)
1.114     nicm      421:                        return (1);
1.29      nicm      422:        }
1.109     nicm      423:        w->active->active_point = next_active_point++;
1.136     nicm      424:        w->active->flags |= PANE_CHANGED;
1.114     nicm      425:        return (1);
1.145     nicm      426: }
                    427:
                    428: void
                    429: window_redraw_active_switch(struct window *w, struct window_pane *wp)
                    430: {
                    431:        const struct grid_cell  *agc, *wgc;
                    432:
                    433:        if (wp == w->active)
                    434:                return;
                    435:
                    436:        /*
                    437:         * If window-style and window-active-style are the same, we don't need
                    438:         * to redraw panes when switching active panes. Otherwise, if the
                    439:         * active or inactive pane do not have a custom style, they will need
                    440:         * to be redrawn.
                    441:         */
1.146     nicm      442:        agc = options_get_style(w->options, "window-active-style");
                    443:        wgc = options_get_style(w->options, "window-style");
1.145     nicm      444:        if (style_equal(agc, wgc))
                    445:                return;
                    446:        if (style_equal(&grid_default_cell, &w->active->colgc))
                    447:                w->active->flags |= PANE_REDRAW;
                    448:        if (style_equal(&grid_default_cell, &wp->colgc))
                    449:                wp->flags |= PANE_REDRAW;
1.29      nicm      450: }
                    451:
1.66      nicm      452: struct window_pane *
                    453: window_get_active_at(struct window *w, u_int x, u_int y)
1.29      nicm      454: {
                    455:        struct window_pane      *wp;
                    456:
                    457:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.66      nicm      458:                if (!window_pane_visible(wp))
1.29      nicm      459:                        continue;
1.66      nicm      460:                if (x < wp->xoff || x > wp->xoff + wp->sx)
1.29      nicm      461:                        continue;
1.66      nicm      462:                if (y < wp->yoff || y > wp->yoff + wp->sy)
1.29      nicm      463:                        continue;
1.66      nicm      464:                return (wp);
                    465:        }
                    466:        return (NULL);
                    467: }
                    468:
                    469: struct window_pane *
                    470: window_find_string(struct window *w, const char *s)
                    471: {
                    472:        u_int   x, y;
                    473:
                    474:        x = w->sx / 2;
                    475:        y = w->sy / 2;
                    476:
                    477:        if (strcasecmp(s, "top") == 0)
                    478:                y = 0;
                    479:        else if (strcasecmp(s, "bottom") == 0)
                    480:                y = w->sy - 1;
                    481:        else if (strcasecmp(s, "left") == 0)
                    482:                x = 0;
                    483:        else if (strcasecmp(s, "right") == 0)
                    484:                x = w->sx - 1;
                    485:        else if (strcasecmp(s, "top-left") == 0) {
                    486:                x = 0;
                    487:                y = 0;
                    488:        } else if (strcasecmp(s, "top-right") == 0) {
                    489:                x = w->sx - 1;
                    490:                y = 0;
                    491:        } else if (strcasecmp(s, "bottom-left") == 0) {
                    492:                x = 0;
                    493:                y = w->sy - 1;
                    494:        } else if (strcasecmp(s, "bottom-right") == 0) {
                    495:                x = w->sx - 1;
                    496:                y = w->sy - 1;
                    497:        } else
                    498:                return (NULL);
                    499:
                    500:        return (window_get_active_at(w, x, y));
1.1       nicm      501: }
                    502:
1.93      nicm      503: int
                    504: window_zoom(struct window_pane *wp)
                    505: {
                    506:        struct window           *w = wp->window;
                    507:        struct window_pane      *wp1;
                    508:
                    509:        if (w->flags & WINDOW_ZOOMED)
                    510:                return (-1);
                    511:
                    512:        if (!window_pane_visible(wp))
                    513:                return (-1);
1.94      nicm      514:
                    515:        if (window_count_panes(w) == 1)
                    516:                return (-1);
                    517:
1.93      nicm      518:        if (w->active != wp)
                    519:                window_set_active_pane(w, wp);
                    520:
                    521:        TAILQ_FOREACH(wp1, &w->panes, entry) {
                    522:                wp1->saved_layout_cell = wp1->layout_cell;
                    523:                wp1->layout_cell = NULL;
                    524:        }
                    525:
                    526:        w->saved_layout_root = w->layout_root;
                    527:        layout_init(w, wp);
                    528:        w->flags |= WINDOW_ZOOMED;
1.115     nicm      529:        notify_window_layout_changed(w);
1.93      nicm      530:
                    531:        return (0);
                    532: }
                    533:
                    534: int
                    535: window_unzoom(struct window *w)
                    536: {
1.97      nicm      537:        struct window_pane      *wp;
1.93      nicm      538:
                    539:        if (!(w->flags & WINDOW_ZOOMED))
                    540:                return (-1);
                    541:
                    542:        w->flags &= ~WINDOW_ZOOMED;
                    543:        layout_free(w);
                    544:        w->layout_root = w->saved_layout_root;
1.120     nicm      545:        w->saved_layout_root = NULL;
1.93      nicm      546:
1.97      nicm      547:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    548:                wp->layout_cell = wp->saved_layout_cell;
                    549:                wp->saved_layout_cell = NULL;
1.93      nicm      550:        }
                    551:        layout_fix_panes(w, w->sx, w->sy);
1.115     nicm      552:        notify_window_layout_changed(w);
1.93      nicm      553:
                    554:        return (0);
                    555: }
                    556:
1.1       nicm      557: struct window_pane *
1.161     nicm      558: window_add_pane(struct window *w, struct window_pane *after, u_int hlimit)
1.1       nicm      559: {
                    560:        struct window_pane      *wp;
                    561:
1.14      nicm      562:        wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1       nicm      563:        if (TAILQ_EMPTY(&w->panes))
                    564:                TAILQ_INSERT_HEAD(&w->panes, wp, entry);
1.161     nicm      565:        else {
                    566:                if (after == NULL)
                    567:                        TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
                    568:                else
                    569:                        TAILQ_INSERT_AFTER(&w->panes, after, wp, entry);
                    570:        }
1.1       nicm      571:        return (wp);
                    572: }
                    573:
                    574: void
1.105     nicm      575: window_lost_pane(struct window *w, struct window_pane *wp)
1.1       nicm      576: {
1.152     nicm      577:        if (wp == marked_pane.wp)
1.132     nicm      578:                server_clear_marked();
                    579:
1.57      nicm      580:        if (wp == w->active) {
1.58      nicm      581:                w->active = w->last;
                    582:                w->last = NULL;
                    583:                if (w->active == NULL) {
                    584:                        w->active = TAILQ_PREV(wp, window_panes, entry);
                    585:                        if (w->active == NULL)
                    586:                                w->active = TAILQ_NEXT(wp, entry);
                    587:                }
1.151     nicm      588:                if (w->active != NULL)
                    589:                        w->active->flags |= PANE_CHANGED;
1.58      nicm      590:        } else if (wp == w->last)
                    591:                w->last = NULL;
1.105     nicm      592: }
                    593:
                    594: void
                    595: window_remove_pane(struct window *w, struct window_pane *wp)
                    596: {
                    597:        window_lost_pane(w, wp);
1.1       nicm      598:
                    599:        TAILQ_REMOVE(&w->panes, wp, entry);
                    600:        window_pane_destroy(wp);
                    601: }
                    602:
                    603: struct window_pane *
                    604: window_pane_at_index(struct window *w, u_int idx)
                    605: {
                    606:        struct window_pane      *wp;
                    607:        u_int                    n;
                    608:
1.146     nicm      609:        n = options_get_number(w->options, "pane-base-index");
1.1       nicm      610:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    611:                if (n == idx)
                    612:                        return (wp);
                    613:                n++;
                    614:        }
                    615:        return (NULL);
1.53      nicm      616: }
                    617:
                    618: struct window_pane *
                    619: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
                    620: {
                    621:        for (; n > 0; n--) {
                    622:                if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
                    623:                        wp = TAILQ_FIRST(&w->panes);
                    624:        }
                    625:
                    626:        return (wp);
                    627: }
                    628:
                    629: struct window_pane *
                    630: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
                    631:     u_int n)
                    632: {
                    633:        for (; n > 0; n--) {
                    634:                if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
                    635:                        wp = TAILQ_LAST(&w->panes, window_panes);
                    636:        }
                    637:
                    638:        return (wp);
1.13      nicm      639: }
                    640:
1.69      nicm      641: int
                    642: window_pane_index(struct window_pane *wp, u_int *i)
1.13      nicm      643: {
                    644:        struct window_pane      *wq;
1.69      nicm      645:        struct window           *w = wp->window;
1.13      nicm      646:
1.146     nicm      647:        *i = options_get_number(w->options, "pane-base-index");
1.13      nicm      648:        TAILQ_FOREACH(wq, &w->panes, entry) {
1.69      nicm      649:                if (wp == wq) {
                    650:                        return (0);
                    651:                }
                    652:                (*i)++;
1.13      nicm      653:        }
1.69      nicm      654:
                    655:        return (-1);
1.1       nicm      656: }
                    657:
                    658: u_int
                    659: window_count_panes(struct window *w)
                    660: {
                    661:        struct window_pane      *wp;
                    662:        u_int                    n;
                    663:
                    664:        n = 0;
                    665:        TAILQ_FOREACH(wp, &w->panes, entry)
                    666:                n++;
                    667:        return (n);
                    668: }
                    669:
                    670: void
                    671: window_destroy_panes(struct window *w)
                    672: {
                    673:        struct window_pane      *wp;
                    674:
                    675:        while (!TAILQ_EMPTY(&w->panes)) {
                    676:                wp = TAILQ_FIRST(&w->panes);
                    677:                TAILQ_REMOVE(&w->panes, wp, entry);
                    678:                window_pane_destroy(wp);
                    679:        }
1.61      nicm      680: }
                    681:
1.128     nicm      682: /* Retuns the printable flags on a window, empty string if no flags set. */
1.61      nicm      683: char *
                    684: window_printable_flags(struct session *s, struct winlink *wl)
                    685: {
1.119     nicm      686:        char    flags[32];
1.61      nicm      687:        int     pos;
                    688:
                    689:        pos = 0;
                    690:        if (wl->flags & WINLINK_ACTIVITY)
                    691:                flags[pos++] = '#';
                    692:        if (wl->flags & WINLINK_BELL)
                    693:                flags[pos++] = '!';
                    694:        if (wl->flags & WINLINK_SILENCE)
                    695:                flags[pos++] = '~';
                    696:        if (wl == s->curw)
                    697:                flags[pos++] = '*';
                    698:        if (wl == TAILQ_FIRST(&s->lastw))
                    699:                flags[pos++] = '-';
1.152     nicm      700:        if (server_check_marked() && wl == marked_pane.wl)
1.132     nicm      701:                flags[pos++] = 'M';
1.93      nicm      702:        if (wl->window->flags & WINDOW_ZOOMED)
                    703:                flags[pos++] = 'Z';
1.61      nicm      704:        flags[pos] = '\0';
                    705:        return (xstrdup(flags));
1.1       nicm      706: }
                    707:
1.123     nicm      708: struct window_pane *
                    709: window_pane_find_by_id_str(const char *s)
                    710: {
                    711:        const char      *errstr;
                    712:        u_int            id;
                    713:
                    714:        if (*s != '%')
                    715:                return (NULL);
                    716:
                    717:        id = strtonum(s + 1, 0, UINT_MAX, &errstr);
                    718:        if (errstr != NULL)
                    719:                return (NULL);
                    720:        return (window_pane_find_by_id(id));
                    721: }
                    722:
1.64      nicm      723: struct window_pane *
                    724: window_pane_find_by_id(u_int id)
                    725: {
                    726:        struct window_pane      wp;
                    727:
                    728:        wp.id = id;
                    729:        return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
                    730: }
                    731:
1.1       nicm      732: struct window_pane *
                    733: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
                    734: {
                    735:        struct window_pane      *wp;
1.140     nicm      736:        char                     host[HOST_NAME_MAX + 1];
1.1       nicm      737:
                    738:        wp = xcalloc(1, sizeof *wp);
                    739:        wp->window = w;
                    740:
1.71      nicm      741:        wp->id = next_window_pane_id++;
1.64      nicm      742:        RB_INSERT(window_pane_tree, &all_window_panes, wp);
                    743:
1.110     nicm      744:        wp->argc = 0;
                    745:        wp->argv = NULL;
1.23      nicm      746:        wp->shell = NULL;
1.147     nicm      747:        wp->cwd = NULL;
1.1       nicm      748:
                    749:        wp->fd = -1;
1.37      nicm      750:        wp->event = NULL;
1.1       nicm      751:
                    752:        wp->mode = NULL;
1.14      nicm      753:
                    754:        wp->layout_cell = NULL;
1.1       nicm      755:
                    756:        wp->xoff = 0;
1.42      nicm      757:        wp->yoff = 0;
1.1       nicm      758:
                    759:        wp->sx = sx;
                    760:        wp->sy = sy;
                    761:
1.32      nicm      762:        wp->pipe_fd = -1;
                    763:        wp->pipe_off = 0;
1.36      nicm      764:        wp->pipe_event = NULL;
1.32      nicm      765:
1.9       nicm      766:        wp->saved_grid = NULL;
1.117     nicm      767:
                    768:        memcpy(&wp->colgc, &grid_default_cell, sizeof wp->colgc);
1.9       nicm      769:
1.1       nicm      770:        screen_init(&wp->base, sx, sy, hlimit);
                    771:        wp->screen = &wp->base;
1.159     nicm      772:
                    773:        screen_init(&wp->status_screen, 1, 1, 0);
1.140     nicm      774:
                    775:        if (gethostname(host, sizeof host) == 0)
                    776:                screen_set_title(&wp->base, host);
1.1       nicm      777:
                    778:        input_init(wp);
                    779:
                    780:        return (wp);
                    781: }
                    782:
                    783: void
                    784: window_pane_destroy(struct window_pane *wp)
                    785: {
1.55      nicm      786:        window_pane_reset_mode(wp);
                    787:
1.37      nicm      788:        if (wp->fd != -1) {
1.70      nicm      789:                bufferevent_free(wp->event);
1.1       nicm      790:                close(wp->fd);
1.37      nicm      791:        }
1.1       nicm      792:
                    793:        input_free(wp);
                    794:
                    795:        screen_free(&wp->base);
1.9       nicm      796:        if (wp->saved_grid != NULL)
                    797:                grid_destroy(wp->saved_grid);
1.1       nicm      798:
1.32      nicm      799:        if (wp->pipe_fd != -1) {
1.70      nicm      800:                bufferevent_free(wp->pipe_event);
1.32      nicm      801:                close(wp->pipe_fd);
                    802:        }
                    803:
1.64      nicm      804:        RB_REMOVE(window_pane_tree, &all_window_panes, wp);
                    805:
1.147     nicm      806:        free((void *)wp->cwd);
1.82      nicm      807:        free(wp->shell);
1.110     nicm      808:        cmd_free_argv(wp->argc, wp->argv);
1.82      nicm      809:        free(wp);
1.1       nicm      810: }
                    811:
                    812: int
1.110     nicm      813: window_pane_spawn(struct window_pane *wp, int argc, char **argv,
1.147     nicm      814:     const char *path, const char *shell, const char *cwd, struct environ *env,
1.110     nicm      815:     struct termios *tio, char **cause)
1.1       nicm      816: {
1.47      nicm      817:        struct winsize   ws;
1.150     nicm      818:        char            *argv0, *cmd, **argvp;
1.147     nicm      819:        const char      *ptr, *first, *home;
1.47      nicm      820:        struct termios   tio2;
1.110     nicm      821:        int              i;
1.1       nicm      822:
1.37      nicm      823:        if (wp->fd != -1) {
1.70      nicm      824:                bufferevent_free(wp->event);
1.1       nicm      825:                close(wp->fd);
1.37      nicm      826:        }
1.110     nicm      827:        if (argc > 0) {
                    828:                cmd_free_argv(wp->argc, wp->argv);
                    829:                wp->argc = argc;
                    830:                wp->argv = cmd_copy_argv(argc, argv);
1.1       nicm      831:        }
1.23      nicm      832:        if (shell != NULL) {
1.82      nicm      833:                free(wp->shell);
1.23      nicm      834:                wp->shell = xstrdup(shell);
                    835:        }
1.147     nicm      836:        if (cwd != NULL) {
                    837:                free((void *)wp->cwd);
                    838:                wp->cwd = xstrdup(cwd);
1.1       nicm      839:        }
1.91      nicm      840:
1.110     nicm      841:        cmd = cmd_stringify_argv(wp->argc, wp->argv);
                    842:        log_debug("spawn: %s -- %s", wp->shell, cmd);
                    843:        for (i = 0; i < wp->argc; i++)
                    844:                log_debug("spawn: argv[%d] = %s", i, wp->argv[i]);
1.164   ! nicm      845:        environ_log(env);
1.1       nicm      846:
                    847:        memset(&ws, 0, sizeof ws);
                    848:        ws.ws_col = screen_size_x(&wp->base);
                    849:        ws.ws_row = screen_size_y(&wp->base);
                    850:
1.42      nicm      851:        switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1       nicm      852:        case -1:
                    853:                wp->fd = -1;
                    854:                xasprintf(cause, "%s: %s", cmd, strerror(errno));
1.110     nicm      855:                free(cmd);
1.1       nicm      856:                return (-1);
                    857:        case 0:
1.147     nicm      858:                if (chdir(wp->cwd) != 0) {
                    859:                        if ((home = find_home()) == NULL || chdir(home) != 0)
                    860:                                chdir("/");
                    861:                }
1.25      nicm      862:
                    863:                if (tcgetattr(STDIN_FILENO, &tio2) != 0)
                    864:                        fatal("tcgetattr failed");
                    865:                if (tio != NULL)
                    866:                        memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
                    867:                tio2.c_cc[VERASE] = '\177';
                    868:                if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
                    869:                        fatal("tcgetattr failed");
1.18      nicm      870:
1.56      nicm      871:                closefrom(STDERR_FILENO + 1);
                    872:
1.107     nicm      873:                if (path != NULL)
1.150     nicm      874:                        environ_set(env, "PATH", "%s", path);
                    875:                environ_set(env, "TMUX_PANE", "%%%u", wp->id);
1.47      nicm      876:                environ_push(env);
1.18      nicm      877:
1.54      nicm      878:                clear_signals(1);
1.1       nicm      879:                log_close();
                    880:
1.80      nicm      881:                setenv("SHELL", wp->shell, 1);
                    882:                ptr = strrchr(wp->shell, '/');
                    883:
1.110     nicm      884:                /*
                    885:                 * If given one argument, assume it should be passed to sh -c;
                    886:                 * with more than one argument, use execvp(). If there is no
                    887:                 * arguments, create a login shell.
                    888:                 */
                    889:                if (wp->argc > 0) {
                    890:                        if (wp->argc != 1) {
                    891:                                /* Copy to ensure argv ends in NULL. */
                    892:                                argvp = cmd_copy_argv(wp->argc, wp->argv);
                    893:                                execvp(argvp[0], argvp);
                    894:                                fatal("execvp failed");
                    895:                        }
                    896:                        first = wp->argv[0];
                    897:
1.80      nicm      898:                        if (ptr != NULL && *(ptr + 1) != '\0')
                    899:                                xasprintf(&argv0, "%s", ptr + 1);
                    900:                        else
                    901:                                xasprintf(&argv0, "%s", wp->shell);
1.110     nicm      902:                        execl(wp->shell, argv0, "-c", first, (char *)NULL);
1.8       nicm      903:                        fatal("execl failed");
                    904:                }
1.23      nicm      905:                if (ptr != NULL && *(ptr + 1) != '\0')
1.8       nicm      906:                        xasprintf(&argv0, "-%s", ptr + 1);
                    907:                else
1.23      nicm      908:                        xasprintf(&argv0, "-%s", wp->shell);
1.110     nicm      909:                execl(wp->shell, argv0, (char *)NULL);
1.1       nicm      910:                fatal("execl failed");
                    911:        }
                    912:
1.62      nicm      913:        setblocking(wp->fd, 0);
                    914:
1.110     nicm      915:        wp->event = bufferevent_new(wp->fd, window_pane_read_callback, NULL,
                    916:            window_pane_error_callback, wp);
1.131     nicm      917:
                    918:        bufferevent_setwatermark(wp->event, EV_READ, 0, READ_SIZE);
1.37      nicm      919:        bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1       nicm      920:
1.110     nicm      921:        free(cmd);
1.1       nicm      922:        return (0);
                    923: }
                    924:
1.15      nicm      925: void
1.149     nicm      926: window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
1.37      nicm      927: {
1.131     nicm      928:        struct window_pane      *wp = data;
                    929:        struct evbuffer         *evb = wp->event->input;
                    930:        char                    *new_data;
1.158     nicm      931:        size_t                   new_size;
1.131     nicm      932:
1.158     nicm      933:        log_debug("%%%u has %zu bytes (of %zu)", wp->id, EVBUFFER_LENGTH(evb),
                    934:            (size_t)READ_SIZE);
1.131     nicm      935:
                    936:        new_size = EVBUFFER_LENGTH(evb) - wp->pipe_off;
1.46      nicm      937:        if (wp->pipe_fd != -1 && new_size > 0) {
1.155     nicm      938:                new_data = EVBUFFER_DATA(evb) + wp->pipe_off;
1.46      nicm      939:                bufferevent_write(wp->pipe_event, new_data, new_size);
                    940:        }
                    941:
                    942:        input_parse(wp);
1.37      nicm      943:
1.131     nicm      944:        wp->pipe_off = EVBUFFER_LENGTH(evb);
1.37      nicm      945: }
                    946:
                    947: void
1.149     nicm      948: window_pane_error_callback(__unused struct bufferevent *bufev,
                    949:     __unused short what, void *data)
1.37      nicm      950: {
                    951:        struct window_pane *wp = data;
                    952:
1.153     nicm      953:        server_destroy_pane(wp, 1);
1.37      nicm      954: }
                    955:
                    956: void
1.1       nicm      957: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
                    958: {
                    959:        if (sx == wp->sx && sy == wp->sy)
1.15      nicm      960:                return;
1.1       nicm      961:        wp->sx = sx;
                    962:        wp->sy = sy;
                    963:
1.89      nicm      964:        screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
1.1       nicm      965:        if (wp->mode != NULL)
                    966:                wp->mode->resize(wp, sx, sy);
1.95      nicm      967:
                    968:        wp->flags |= PANE_RESIZE;
1.44      nicm      969: }
                    970:
                    971: /*
                    972:  * Enter alternative screen mode. A copy of the visible screen is saved and the
                    973:  * history is not updated
                    974:  */
                    975: void
1.87      nicm      976: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
                    977:     int cursor)
1.44      nicm      978: {
                    979:        struct screen   *s = &wp->base;
                    980:        u_int            sx, sy;
                    981:
                    982:        if (wp->saved_grid != NULL)
                    983:                return;
1.146     nicm      984:        if (!options_get_number(wp->window->options, "alternate-screen"))
1.44      nicm      985:                return;
                    986:        sx = screen_size_x(s);
                    987:        sy = screen_size_y(s);
                    988:
                    989:        wp->saved_grid = grid_create(sx, sy, 0);
                    990:        grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1.87      nicm      991:        if (cursor) {
                    992:                wp->saved_cx = s->cx;
                    993:                wp->saved_cy = s->cy;
                    994:        }
1.44      nicm      995:        memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
                    996:
                    997:        grid_view_clear(s->grid, 0, 0, sx, sy);
                    998:
                    999:        wp->base.grid->flags &= ~GRID_HISTORY;
                   1000:
                   1001:        wp->flags |= PANE_REDRAW;
                   1002: }
                   1003:
                   1004: /* Exit alternate screen mode and restore the copied grid. */
                   1005: void
1.87      nicm     1006: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
                   1007:     int cursor)
1.44      nicm     1008: {
                   1009:        struct screen   *s = &wp->base;
                   1010:        u_int            sx, sy;
                   1011:
                   1012:        if (wp->saved_grid == NULL)
                   1013:                return;
1.146     nicm     1014:        if (!options_get_number(wp->window->options, "alternate-screen"))
1.44      nicm     1015:                return;
                   1016:        sx = screen_size_x(s);
                   1017:        sy = screen_size_y(s);
                   1018:
                   1019:        /*
                   1020:         * If the current size is bigger, temporarily resize to the old size
                   1021:         * before copying back.
                   1022:         */
                   1023:        if (sy > wp->saved_grid->sy)
1.89      nicm     1024:                screen_resize(s, sx, wp->saved_grid->sy, 1);
1.44      nicm     1025:
                   1026:        /* Restore the grid, cursor position and cell. */
                   1027:        grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
1.87      nicm     1028:        if (cursor)
                   1029:                s->cx = wp->saved_cx;
1.44      nicm     1030:        if (s->cx > screen_size_x(s) - 1)
                   1031:                s->cx = screen_size_x(s) - 1;
1.87      nicm     1032:        if (cursor)
                   1033:                s->cy = wp->saved_cy;
1.44      nicm     1034:        if (s->cy > screen_size_y(s) - 1)
                   1035:                s->cy = screen_size_y(s) - 1;
                   1036:        memcpy(gc, &wp->saved_cell, sizeof *gc);
                   1037:
                   1038:        /*
                   1039:         * Turn history back on (so resize can use it) and then resize back to
                   1040:         * the current size.
                   1041:         */
                   1042:        wp->base.grid->flags |= GRID_HISTORY;
1.89      nicm     1043:        if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
                   1044:                screen_resize(s, sx, sy, 1);
1.44      nicm     1045:
                   1046:        grid_destroy(wp->saved_grid);
                   1047:        wp->saved_grid = NULL;
                   1048:
                   1049:        wp->flags |= PANE_REDRAW;
1.1       nicm     1050: }
                   1051:
1.162     nicm     1052: static void
                   1053: window_pane_mode_timer(__unused int fd, __unused short events, void *arg)
                   1054: {
                   1055:        struct window_pane      *wp = arg;
                   1056:        struct timeval           tv = { .tv_sec = 10 };
                   1057:        int                      n = 0;
                   1058:
                   1059:        evtimer_del(&wp->modetimer);
                   1060:        evtimer_add(&wp->modetimer, &tv);
                   1061:
                   1062:        log_debug("%%%u in mode: last=%ld", wp->id, (long)wp->modelast);
                   1063:
                   1064:        if (wp->modelast < time(NULL) - WINDOW_MODE_TIMEOUT) {
                   1065:                if (ioctl(wp->fd, FIONREAD, &n) == -1 || n > 0)
                   1066:                        window_pane_reset_mode(wp);
                   1067:        }
                   1068: }
                   1069:
1.1       nicm     1070: int
                   1071: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
                   1072: {
                   1073:        struct screen   *s;
1.162     nicm     1074:        struct timeval   tv = { .tv_sec = 10 };
1.1       nicm     1075:
1.15      nicm     1076:        if (wp->mode != NULL)
1.1       nicm     1077:                return (1);
                   1078:        wp->mode = mode;
                   1079:
1.162     nicm     1080:        wp->modelast = time(NULL);
                   1081:        evtimer_set(&wp->modetimer, window_pane_mode_timer, wp);
                   1082:        evtimer_add(&wp->modetimer, &tv);
                   1083:
1.1       nicm     1084:        if ((s = wp->mode->init(wp)) != NULL)
                   1085:                wp->screen = s;
1.142     nicm     1086:        wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157     nicm     1087:
                   1088:        server_status_window(wp->window);
1.1       nicm     1089:        return (0);
                   1090: }
                   1091:
                   1092: void
                   1093: window_pane_reset_mode(struct window_pane *wp)
                   1094: {
                   1095:        if (wp->mode == NULL)
                   1096:                return;
                   1097:
1.162     nicm     1098:        evtimer_del(&wp->modetimer);
                   1099:
1.1       nicm     1100:        wp->mode->free(wp);
                   1101:        wp->mode = NULL;
                   1102:
                   1103:        wp->screen = &wp->base;
1.142     nicm     1104:        wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157     nicm     1105:
                   1106:        server_status_window(wp->window);
1.1       nicm     1107: }
                   1108:
                   1109: void
1.118     nicm     1110: window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1.148     nicm     1111:     key_code key, struct mouse_event *m)
1.1       nicm     1112: {
1.27      nicm     1113:        struct window_pane      *wp2;
1.3       nicm     1114:
1.118     nicm     1115:        if (KEYC_IS_MOUSE(key) && m == NULL)
                   1116:                return;
                   1117:
1.1       nicm     1118:        if (wp->mode != NULL) {
1.162     nicm     1119:                wp->modelast = time(NULL);
1.1       nicm     1120:                if (wp->mode->key != NULL)
1.118     nicm     1121:                        wp->mode->key(wp, c, s, key, m);
1.27      nicm     1122:                return;
1.30      nicm     1123:        }
1.27      nicm     1124:
1.113     nicm     1125:        if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1.30      nicm     1126:                return;
1.113     nicm     1127:
1.118     nicm     1128:        input_key(wp, key, m);
                   1129:
                   1130:        if (KEYC_IS_MOUSE(key))
                   1131:                return;
1.146     nicm     1132:        if (options_get_number(wp->window->options, "synchronize-panes")) {
1.27      nicm     1133:                TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                   1134:                        if (wp2 == wp || wp2->mode != NULL)
                   1135:                                continue;
1.154     nicm     1136:                        if (wp2->fd == -1 || wp2->flags & PANE_INPUTOFF)
                   1137:                                continue;
                   1138:                        if (window_pane_visible(wp2))
1.118     nicm     1139:                                input_key(wp2, key, NULL);
1.27      nicm     1140:                }
                   1141:        }
1.10      nicm     1142: }
                   1143:
                   1144: int
                   1145: window_pane_visible(struct window_pane *wp)
                   1146: {
                   1147:        struct window   *w = wp->window;
                   1148:
1.93      nicm     1149:        if (wp->layout_cell == NULL)
                   1150:                return (0);
1.10      nicm     1151:        if (wp->xoff >= w->sx || wp->yoff >= w->sy)
                   1152:                return (0);
                   1153:        if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
                   1154:                return (0);
                   1155:        return (1);
1.1       nicm     1156: }
                   1157:
                   1158: char *
1.108     nicm     1159: window_pane_search(struct window_pane *wp, const char *searchstr,
                   1160:     u_int *lineno)
1.1       nicm     1161: {
1.4       nicm     1162:        struct screen   *s = &wp->base;
1.5       nicm     1163:        char            *newsearchstr, *line, *msg;
1.4       nicm     1164:        u_int            i;
                   1165:
1.5       nicm     1166:        msg = NULL;
                   1167:        xasprintf(&newsearchstr, "*%s*", searchstr);
                   1168:
1.4       nicm     1169:        for (i = 0; i < screen_size_y(s); i++) {
                   1170:                line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5       nicm     1171:                if (fnmatch(newsearchstr, line, 0) == 0) {
                   1172:                        msg = line;
                   1173:                        if (lineno != NULL)
                   1174:                                *lineno = i;
                   1175:                        break;
                   1176:                }
1.82      nicm     1177:                free(line);
1.4       nicm     1178:        }
1.5       nicm     1179:
1.82      nicm     1180:        free(newsearchstr);
1.5       nicm     1181:        return (msg);
1.45      nicm     1182: }
                   1183:
1.109     nicm     1184: /* Get MRU pane from a list. */
                   1185: struct window_pane *
1.126     nicm     1186: window_pane_choose_best(struct window_pane **list, u_int size)
1.109     nicm     1187: {
                   1188:        struct window_pane      *next, *best;
                   1189:        u_int                    i;
                   1190:
1.126     nicm     1191:        if (size == 0)
1.109     nicm     1192:                return (NULL);
                   1193:
1.126     nicm     1194:        best = list[0];
                   1195:        for (i = 1; i < size; i++) {
                   1196:                next = list[i];
1.109     nicm     1197:                if (next->active_point > best->active_point)
                   1198:                        best = next;
                   1199:        }
                   1200:        return (best);
                   1201: }
                   1202:
                   1203: /*
                   1204:  * Find the pane directly above another. We build a list of those adjacent to
                   1205:  * top edge and then choose the best.
                   1206:  */
1.45      nicm     1207: struct window_pane *
                   1208: window_pane_find_up(struct window_pane *wp)
                   1209: {
1.126     nicm     1210:        struct window_pane      *next, *best, **list;
                   1211:        u_int                    edge, left, right, end, size;
1.109     nicm     1212:        int                      found;
1.45      nicm     1213:
                   1214:        if (wp == NULL || !window_pane_visible(wp))
                   1215:                return (NULL);
1.126     nicm     1216:
                   1217:        list = NULL;
                   1218:        size = 0;
1.109     nicm     1219:
                   1220:        edge = wp->yoff;
                   1221:        if (edge == 0)
                   1222:                edge = wp->window->sy + 1;
1.45      nicm     1223:
                   1224:        left = wp->xoff;
1.109     nicm     1225:        right = wp->xoff + wp->sx;
1.45      nicm     1226:
1.109     nicm     1227:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1228:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1229:                        continue;
1.109     nicm     1230:                if (next->yoff + next->sy + 1 != edge)
1.45      nicm     1231:                        continue;
1.109     nicm     1232:                end = next->xoff + next->sx - 1;
                   1233:
                   1234:                found = 0;
                   1235:                if (next->xoff < left && end > right)
                   1236:                        found = 1;
                   1237:                else if (next->xoff >= left && next->xoff <= right)
                   1238:                        found = 1;
                   1239:                else if (end >= left && end <= right)
                   1240:                        found = 1;
1.126     nicm     1241:                if (!found)
                   1242:                        continue;
                   1243:                list = xreallocarray(list, size + 1, sizeof *list);
                   1244:                list[size++] = next;
1.109     nicm     1245:        }
                   1246:
1.126     nicm     1247:        best = window_pane_choose_best(list, size);
                   1248:        free(list);
1.109     nicm     1249:        return (best);
1.45      nicm     1250: }
                   1251:
                   1252: /* Find the pane directly below another. */
                   1253: struct window_pane *
                   1254: window_pane_find_down(struct window_pane *wp)
                   1255: {
1.126     nicm     1256:        struct window_pane      *next, *best, **list;
                   1257:        u_int                    edge, left, right, end, size;
1.109     nicm     1258:        int                      found;
1.45      nicm     1259:
                   1260:        if (wp == NULL || !window_pane_visible(wp))
                   1261:                return (NULL);
1.126     nicm     1262:
                   1263:        list = NULL;
                   1264:        size = 0;
1.109     nicm     1265:
                   1266:        edge = wp->yoff + wp->sy + 1;
                   1267:        if (edge >= wp->window->sy)
                   1268:                edge = 0;
1.45      nicm     1269:
                   1270:        left = wp->xoff;
1.109     nicm     1271:        right = wp->xoff + wp->sx;
1.45      nicm     1272:
1.109     nicm     1273:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1274:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1275:                        continue;
1.109     nicm     1276:                if (next->yoff != edge)
1.45      nicm     1277:                        continue;
1.109     nicm     1278:                end = next->xoff + next->sx - 1;
                   1279:
                   1280:                found = 0;
                   1281:                if (next->xoff < left && end > right)
                   1282:                        found = 1;
                   1283:                else if (next->xoff >= left && next->xoff <= right)
                   1284:                        found = 1;
                   1285:                else if (end >= left && end <= right)
                   1286:                        found = 1;
1.126     nicm     1287:                if (!found)
                   1288:                        continue;
                   1289:                list = xreallocarray(list, size + 1, sizeof *list);
                   1290:                list[size++] = next;
1.45      nicm     1291:        }
1.109     nicm     1292:
1.126     nicm     1293:        best = window_pane_choose_best(list, size);
                   1294:        free(list);
1.109     nicm     1295:        return (best);
1.45      nicm     1296: }
                   1297:
1.109     nicm     1298: /* Find the pane directly to the left of another. */
1.45      nicm     1299: struct window_pane *
                   1300: window_pane_find_left(struct window_pane *wp)
                   1301: {
1.126     nicm     1302:        struct window_pane      *next, *best, **list;
                   1303:        u_int                    edge, top, bottom, end, size;
1.109     nicm     1304:        int                      found;
1.45      nicm     1305:
                   1306:        if (wp == NULL || !window_pane_visible(wp))
                   1307:                return (NULL);
1.126     nicm     1308:
                   1309:        list = NULL;
                   1310:        size = 0;
1.109     nicm     1311:
                   1312:        edge = wp->xoff;
                   1313:        if (edge == 0)
                   1314:                edge = wp->window->sx + 1;
1.45      nicm     1315:
                   1316:        top = wp->yoff;
1.109     nicm     1317:        bottom = wp->yoff + wp->sy;
1.45      nicm     1318:
1.109     nicm     1319:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1320:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1321:                        continue;
1.109     nicm     1322:                if (next->xoff + next->sx + 1 != edge)
1.45      nicm     1323:                        continue;
1.109     nicm     1324:                end = next->yoff + next->sy - 1;
                   1325:
                   1326:                found = 0;
                   1327:                if (next->yoff < top && end > bottom)
                   1328:                        found = 1;
                   1329:                else if (next->yoff >= top && next->yoff <= bottom)
                   1330:                        found = 1;
                   1331:                else if (end >= top && end <= bottom)
                   1332:                        found = 1;
1.126     nicm     1333:                if (!found)
                   1334:                        continue;
                   1335:                list = xreallocarray(list, size + 1, sizeof *list);
                   1336:                list[size++] = next;
1.45      nicm     1337:        }
1.109     nicm     1338:
1.126     nicm     1339:        best = window_pane_choose_best(list, size);
                   1340:        free(list);
1.109     nicm     1341:        return (best);
1.45      nicm     1342: }
                   1343:
1.109     nicm     1344: /* Find the pane directly to the right of another. */
1.45      nicm     1345: struct window_pane *
                   1346: window_pane_find_right(struct window_pane *wp)
                   1347: {
1.126     nicm     1348:        struct window_pane      *next, *best, **list;
                   1349:        u_int                    edge, top, bottom, end, size;
1.109     nicm     1350:        int                      found;
1.45      nicm     1351:
                   1352:        if (wp == NULL || !window_pane_visible(wp))
                   1353:                return (NULL);
1.126     nicm     1354:
                   1355:        list = NULL;
                   1356:        size = 0;
1.109     nicm     1357:
                   1358:        edge = wp->xoff + wp->sx + 1;
                   1359:        if (edge >= wp->window->sx)
                   1360:                edge = 0;
1.45      nicm     1361:
                   1362:        top = wp->yoff;
1.109     nicm     1363:        bottom = wp->yoff + wp->sy;
1.45      nicm     1364:
1.109     nicm     1365:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1366:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1367:                        continue;
1.109     nicm     1368:                if (next->xoff != edge)
1.45      nicm     1369:                        continue;
1.109     nicm     1370:                end = next->yoff + next->sy - 1;
                   1371:
                   1372:                found = 0;
                   1373:                if (next->yoff < top && end > bottom)
                   1374:                        found = 1;
                   1375:                else if (next->yoff >= top && next->yoff <= bottom)
                   1376:                        found = 1;
                   1377:                else if (end >= top && end <= bottom)
                   1378:                        found = 1;
1.126     nicm     1379:                if (!found)
                   1380:                        continue;
                   1381:                list = xreallocarray(list, size + 1, sizeof *list);
                   1382:                list[size++] = next;
1.109     nicm     1383:        }
                   1384:
1.126     nicm     1385:        best = window_pane_choose_best(list, size);
                   1386:        free(list);
1.109     nicm     1387:        return (best);
1.81      nicm     1388: }
                   1389:
                   1390: /* Clear alert flags for a winlink */
                   1391: void
                   1392: winlink_clear_flags(struct winlink *wl)
                   1393: {
                   1394:        struct session  *s;
1.122     nicm     1395:        struct winlink  *wl_loop;
1.81      nicm     1396:
1.122     nicm     1397:        RB_FOREACH(s, sessions, &sessions) {
                   1398:                RB_FOREACH(wl_loop, winlinks, &s->windows) {
                   1399:                        if (wl_loop->window != wl->window)
1.81      nicm     1400:                                continue;
1.122     nicm     1401:                        if ((wl_loop->flags & WINLINK_ALERTFLAGS) == 0)
1.81      nicm     1402:                                continue;
                   1403:
1.122     nicm     1404:                        wl_loop->flags &= ~WINLINK_ALERTFLAGS;
                   1405:                        wl_loop->window->flags &= ~WINDOW_ALERTFLAGS;
1.81      nicm     1406:                        server_status_session(s);
                   1407:                }
                   1408:        }
1.134     nicm     1409: }
                   1410:
                   1411: int
                   1412: winlink_shuffle_up(struct session *s, struct winlink *wl)
                   1413: {
                   1414:        int      idx, last;
                   1415:
                   1416:        idx = wl->idx + 1;
                   1417:
                   1418:        /* Find the next free index. */
                   1419:        for (last = idx; last < INT_MAX; last++) {
                   1420:                if (winlink_find_by_index(&s->windows, last) == NULL)
                   1421:                        break;
                   1422:        }
                   1423:        if (last == INT_MAX)
                   1424:                return (-1);
                   1425:
                   1426:        /* Move everything from last - 1 to idx up a bit. */
                   1427:        for (; last > idx; last--) {
                   1428:                wl = winlink_find_by_index(&s->windows, last - 1);
                   1429:                server_link_window(s, wl, s, last, 0, 0, NULL);
                   1430:                server_unlink_window(s, wl);
                   1431:        }
                   1432:
                   1433:        return (idx);
1.1       nicm     1434: }