diff --git a/cmd-break-pane.c b/cmd-break-pane.c index 57622119..b8d489de 100644 --- a/cmd-break-pane.c +++ b/cmd-break-pane.c @@ -48,6 +48,7 @@ cmd_break_pane_exec(struct cmd *self, struct cmd_ctx *ctx) struct window_pane *wp; struct window *w; char *cause; + int base_idx; if ((wl = cmd_find_pane(ctx, data->target, &s, &wp)) == NULL) return (-1); @@ -71,7 +72,8 @@ cmd_break_pane_exec(struct cmd *self, struct cmd_ctx *ctx) w->name = default_window_name(w); layout_init(w); - wl = session_attach(s, w, -1, &cause); /* can't fail */ + base_idx = options_get_number(&s->options, "base-index"); + wl = session_attach(s, w, -1 - base_idx, &cause); /* can't fail */ if (!(data->chflags & CMD_CHFLAG('d'))) session_select(s, wl->idx); diff --git a/cmd-link-window.c b/cmd-link-window.c index 742fb243..c13dbc90 100644 --- a/cmd-link-window.c +++ b/cmd-link-window.c @@ -77,6 +77,8 @@ cmd_link_window_exec(struct cmd *self, struct cmd_ctx *ctx) } } + if (idx == -1) + idx = -1 - options_get_number(&dst->options, "base-index"); wl_dst = session_attach(dst, wl_src->window, idx, &cause); if (wl_dst == NULL) { ctx->error(ctx, "create session failed: %s", cause); diff --git a/cmd-move-window.c b/cmd-move-window.c index f714c2cb..1cf72f94 100644 --- a/cmd-move-window.c +++ b/cmd-move-window.c @@ -79,6 +79,8 @@ cmd_move_window_exec(struct cmd *self, struct cmd_ctx *ctx) } } + if (idx == -1) + idx = -1 - options_get_number(&dst->options, "base-index"); wl_dst = session_attach(dst, wl_src->window, idx, &cause); if (wl_dst == NULL) { ctx->error(ctx, "attach window failed: %s", cause); diff --git a/cmd-new-session.c b/cmd-new-session.c index 64337e6d..ff90e3be 100644 --- a/cmd-new-session.c +++ b/cmd-new-session.c @@ -118,7 +118,7 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx) struct termios tio; const char *update; char *overrides, *cmd, *cwd, *cause; - int detached; + int detached, idx; u_int sx, sy; if (data->newname != NULL && session_find(data->newname) != NULL) { @@ -216,7 +216,9 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx) tio.c_ospeed = TTYDEF_SPEED; /* Create the new session. */ - s = session_create(data->newname, cmd, cwd, &env, &tio, sx, sy, &cause); + idx = -1 - options_get_number(&global_s_options, "base-index"); + s = session_create( + data->newname, cmd, cwd, &env, &tio, idx, sx, sy, &cause); if (s == NULL) { ctx->error(ctx, "create session failed: %s", cause); xfree(cause); diff --git a/cmd-new-window.c b/cmd-new-window.c index 2af6abf3..b3ec4379 100644 --- a/cmd-new-window.c +++ b/cmd-new-window.c @@ -154,6 +154,8 @@ cmd_new_window_exec(struct cmd *self, struct cmd_ctx *ctx) else cwd = ctx->cmdclient->cwd; + if (idx == -1) + idx = -1 - options_get_number(&s->options, "base-index"); wl = session_new(s, data->name, cmd, cwd, idx, &cause); if (wl == NULL) { ctx->error(ctx, "create window failed: %s", cause); diff --git a/cmd-set-option.c b/cmd-set-option.c index 87b0b5a3..e764f4c5 100644 --- a/cmd-set-option.c +++ b/cmd-set-option.c @@ -50,6 +50,7 @@ const char *set_option_bell_action_list[] = { "none", "any", "current", NULL }; const struct set_option_entry set_option_table[] = { + { "base-index", SET_OPTION_NUMBER, 0, INT_MAX, NULL }, { "bell-action", SET_OPTION_CHOICE, 0, 0, set_option_bell_action_list }, { "buffer-limit", SET_OPTION_NUMBER, 1, INT_MAX, NULL }, { "default-command", SET_OPTION_STRING, 0, 0, NULL }, diff --git a/session.c b/session.c index 9b7beeb5..2e1aae3d 100644 --- a/session.c +++ b/session.c @@ -113,7 +113,8 @@ session_find(const char *name) /* Create a new session. */ struct session * session_create(const char *name, const char *cmd, const char *cwd, - struct environ *env, struct termios *tio, u_int sx, u_int sy, char **cause) + struct environ *env, struct termios *tio, int idx, u_int sx, u_int sy, + char **cause) { struct session *s; u_int i; @@ -149,11 +150,11 @@ session_create(const char *name, const char *cmd, const char *cwd, s->name = xstrdup(name); else xasprintf(&s->name, "%u", i); - if (session_new(s, NULL, cmd, cwd, -1, cause) == NULL) { + if (session_new(s, NULL, cmd, cwd, idx, cause) == NULL) { session_destroy(s); return (NULL); } - session_select(s, 0); + session_select(s, RB_ROOT(&s->windows)->idx); log_debug("session %s created", s->name); diff --git a/tmux.1 b/tmux.1 index faa44ad6..138366e2 100644 --- a/tmux.1 +++ b/tmux.1 @@ -1064,6 +1064,10 @@ options - it is not possible to unset a global option. .Pp Available session options are: .Bl -tag -width Ds +.It Ic base-index Ar index +Set the base index from which an unused index should be searched when a new +window is created. +The default is zero. .It Xo Ic bell-action .Op Ic any | none | current .Xc diff --git a/tmux.c b/tmux.c index 65772f92..9ad48863 100644 --- a/tmux.c +++ b/tmux.c @@ -342,6 +342,7 @@ main(int argc, char **argv) } options_init(&global_s_options, NULL); + options_set_number(&global_s_options, "base-index", 0); options_set_number(&global_s_options, "bell-action", BELL_ANY); options_set_number(&global_s_options, "buffer-limit", 9); options_set_string(&global_s_options, "default-command", "%s", ""); diff --git a/tmux.h b/tmux.h index b15d02bf..2e182660 100644 --- a/tmux.h +++ b/tmux.h @@ -1565,7 +1565,7 @@ RB_PROTOTYPE(windows, window, entry, window_cmp); RB_PROTOTYPE(winlinks, winlink, entry, winlink_cmp); struct winlink *winlink_find_by_index(struct winlinks *, int); struct winlink *winlink_find_by_window(struct winlinks *, struct window *); -int winlink_next_index(struct winlinks *); +int winlink_next_index(struct winlinks *, int); u_int winlink_count(struct winlinks *); struct winlink *winlink_add(struct winlinks *, struct window *, int); void winlink_remove(struct winlinks *, struct winlink *); @@ -1670,7 +1670,8 @@ int session_alert_has(struct session *, struct winlink *, int); int session_alert_has_window(struct session *, struct window *, int); struct session *session_find(const char *); struct session *session_create(const char *, const char *, const char *, - struct environ *, struct termios *, u_int, u_int, char **); + struct environ *, struct termios *, int, u_int, u_int, + char **); void session_destroy(struct session *); int session_index(struct session *, u_int *); struct winlink *session_new(struct session *, diff --git a/window.c b/window.c index eccaa4e5..630533ee 100644 --- a/window.c +++ b/window.c @@ -122,16 +122,20 @@ winlink_find_by_index(struct winlinks *wwl, int idx) } int -winlink_next_index(struct winlinks *wwl) +winlink_next_index(struct winlinks *wwl, int idx) { - u_int i; + int i; - for (i = 0; i < INT_MAX; i++) { + i = idx; + do { if (winlink_find_by_index(wwl, i) == NULL) return (i); - } - - fatalx("no free indexes"); + if (i == INT_MAX) + i = 0; + else + i++; + } while (i != idx); + return (-1); } u_int @@ -152,14 +156,12 @@ winlink_add(struct winlinks *wwl, struct window *w, int idx) { struct winlink *wl; - if (idx == -1) - idx = winlink_next_index(wwl); - else if (winlink_find_by_index(wwl, idx) != NULL) + if (idx < 0) { + if ((idx = winlink_next_index(wwl, -idx - 1)) == -1) + return (NULL); + } else if (winlink_find_by_index(wwl, idx) != NULL) return (NULL); - if (idx < 0) - fatalx("bad index"); - wl = xcalloc(1, sizeof *wl); wl->idx = idx; wl->window = w;