diff options
Diffstat (limited to 'session.c')
-rw-r--r-- | session.c | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -591,3 +591,49 @@ session_group_synchronize1(struct session *target, struct session *s) winlink_remove(&old_windows, wl); } } + +/* Renumber the windows across winlinks attached to a specific session. */ +void +session_renumber_windows(struct session *s) +{ + struct winlink *wl, *wl1, *wl_new; + struct winlinks old_wins; + struct winlink_stack old_lastw; + int new_idx, new_curw_idx; + + /* Save and replace old window list. */ + memcpy(&old_wins, &s->windows, sizeof old_wins); + RB_INIT(&s->windows); + + /* Start renumbering from the base-index if it's set. */ + new_idx = options_get_number(&s->options, "base-index"); + new_curw_idx = 0; + + /* Go through the winlinks and assign new indexes. */ + RB_FOREACH(wl, winlinks, &old_wins) { + wl_new = winlink_add(&s->windows, new_idx); + winlink_set_window(wl_new, wl->window); + wl_new->flags |= wl->flags & WINLINK_ALERTFLAGS; + + if (wl == s->curw) + new_curw_idx = wl_new->idx; + + new_idx++; + } + + /* Fix the stack of last windows now. */ + memcpy(&old_lastw, &s->lastw, sizeof old_lastw); + TAILQ_INIT(&s->lastw); + TAILQ_FOREACH(wl, &old_lastw, sentry) { + wl_new = winlink_find_by_index(&s->windows, wl->idx); + if (wl_new != NULL) + TAILQ_INSERT_TAIL(&s->lastw, wl_new, sentry); + } + + /* Set the current window. */ + s->curw = winlink_find_by_index(&s->windows, new_curw_idx); + + /* Free the old winlinks (reducing window references too). */ + RB_FOREACH_SAFE(wl, winlinks, &old_wins, wl1) + winlink_remove(&old_wins, wl); +} |