From 1c78155e70a9f72ed6c191807c2b381cf114b91f Mon Sep 17 00:00:00 2001 From: nicm Date: Sat, 13 Jun 2020 09:05:53 +0000 Subject: [PATCH] Add -b flags to insert a window before (like the existing -a for after) to break-pane, move-window, new-window. GitHub issue 2261. --- cmd-break-pane.c | 16 ++++++++-------- cmd-move-window.c | 17 +++++++++-------- cmd-new-window.c | 16 ++++++++++------ tmux.1 | 33 ++++++++++++++++++++------------- tmux.h | 2 +- window.c | 12 ++++++++---- 6 files changed, 56 insertions(+), 40 deletions(-) diff --git a/cmd-break-pane.c b/cmd-break-pane.c index 6d5041e8..4c436405 100644 --- a/cmd-break-pane.c +++ b/cmd-break-pane.c @@ -34,8 +34,8 @@ const struct cmd_entry cmd_break_pane_entry = { .name = "break-pane", .alias = "breakp", - .args = { "adPF:n:s:t:", 0, 0 }, - .usage = "[-adP] [-F format] [-n window-name] [-s src-pane] " + .args = { "abdPF:n:s:t:", 0, 0 }, + .usage = "[-abdP] [-F format] [-n window-name] [-s src-pane] " "[-t dst-window]", .source = { 's', CMD_FIND_PANE, 0 }, @@ -58,16 +58,16 @@ cmd_break_pane_exec(struct cmd *self, struct cmdq_item *item) struct session *dst_s = target->s; struct window_pane *wp = source->wp; struct window *w = wl->window; - char *name, *cause; - int idx = target->idx; + char *name, *cause, *cp; + int idx = target->idx, before; const char *template; - char *cp; - if (args_has(args, 'a')) { + before = args_has(args, 'b'); + if (args_has(args, 'a') || before) { if (target->wl != NULL) - idx = winlink_shuffle_up(dst_s, target->wl); + idx = winlink_shuffle_up(dst_s, target->wl, before); else - idx = winlink_shuffle_up(dst_s, dst_s->curw); + idx = winlink_shuffle_up(dst_s, dst_s->curw, before); if (idx == -1) return (CMD_RETURN_ERROR); } diff --git a/cmd-move-window.c b/cmd-move-window.c index 94b6c950..61128771 100644 --- a/cmd-move-window.c +++ b/cmd-move-window.c @@ -32,8 +32,8 @@ const struct cmd_entry cmd_move_window_entry = { .name = "move-window", .alias = "movew", - .args = { "adkrs:t:", 0, 0 }, - .usage = "[-dkr] " CMD_SRCDST_WINDOW_USAGE, + .args = { "abdkrs:t:", 0, 0 }, + .usage = "[-abdkr] " CMD_SRCDST_WINDOW_USAGE, .source = { 's', CMD_FIND_WINDOW, 0 }, /* -t is special */ @@ -46,8 +46,8 @@ const struct cmd_entry cmd_link_window_entry = { .name = "link-window", .alias = "linkw", - .args = { "adks:t:", 0, 0 }, - .usage = "[-dk] " CMD_SRCDST_WINDOW_USAGE, + .args = { "abdks:t:", 0, 0 }, + .usage = "[-abdk] " CMD_SRCDST_WINDOW_USAGE, .source = { 's', CMD_FIND_WINDOW, 0 }, /* -t is special */ @@ -67,7 +67,7 @@ cmd_move_window_exec(struct cmd *self, struct cmdq_item *item) struct session *dst; struct winlink *wl = source->wl; char *cause; - int idx, kflag, dflag, sflag; + int idx, kflag, dflag, sflag, before; if (args_has(args, 'r')) { if (cmd_find_target(&target, item, tflag, CMD_FIND_SESSION, @@ -90,11 +90,12 @@ cmd_move_window_exec(struct cmd *self, struct cmdq_item *item) dflag = args_has(args, 'd'); sflag = args_has(args, 's'); - if (args_has(args, 'a')) { + before = args_has(args, 'b'); + if (args_has(args, 'a') || before) { if (target.wl != NULL) - idx = winlink_shuffle_up(dst, target.wl); + idx = winlink_shuffle_up(dst, target.wl, before); else - idx = winlink_shuffle_up(dst, dst->curw); + idx = winlink_shuffle_up(dst, dst->curw, before); if (idx == -1) return (CMD_RETURN_ERROR); } diff --git a/cmd-new-window.c b/cmd-new-window.c index 722f89b9..0b24474b 100644 --- a/cmd-new-window.c +++ b/cmd-new-window.c @@ -38,8 +38,8 @@ const struct cmd_entry cmd_new_window_entry = { .name = "new-window", .alias = "neww", - .args = { "ac:de:F:kn:Pt:", 0, -1 }, - .usage = "[-adkP] [-c start-directory] [-e environment] [-F format] " + .args = { "abc:de:F:kn:Pt:", 0, -1 }, + .usage = "[-abdkP] [-c start-directory] [-e environment] [-F format] " "[-n window-name] " CMD_TARGET_WINDOW_USAGE " [command]", .target = { 't', CMD_FIND_WINDOW, CMD_FIND_WINDOW_INDEX }, @@ -58,16 +58,20 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item) struct client *tc = cmdq_get_target_client(item); struct session *s = target->s; struct winlink *wl = target->wl; - int idx = target->idx; + int idx = target->idx, before; struct winlink *new_wl; char *cause = NULL, *cp; const char *template, *add; struct cmd_find_state fs; struct args_value *value; - if (args_has(args, 'a') && (idx = winlink_shuffle_up(s, wl)) == -1) { - cmdq_error(item, "couldn't get a window index"); - return (CMD_RETURN_ERROR); + before = args_has(args, 'b'); + if (args_has(args, 'a') || before) { + idx = winlink_shuffle_up(s, wl, before); + if (idx == -1) { + cmdq_error(item, "couldn't get a window index"); + return (CMD_RETURN_ERROR); + } } memset(&sc, 0, sizeof sc); diff --git a/tmux.1 b/tmux.1 index 207fa47c..d6fd1c85 100644 --- a/tmux.1 +++ b/tmux.1 @@ -1792,7 +1792,7 @@ from which the layout was originally defined. Commands related to windows and panes are as follows: .Bl -tag -width Ds .It Xo Ic break-pane -.Op Fl adP +.Op Fl abdP .Op Fl F Ar format .Op Fl n Ar window-name .Op Fl s Ar src-pane @@ -1804,9 +1804,11 @@ Break off from its containing window to make it the only pane in .Ar dst-window . With -.Fl a , -the window is moved to the next index up (following windows -are moved if necessary). +.Fl a +or +.Fl b , +the window is moved to the next index after or before (existing windows are +moved if necessary). If .Fl d is given, the new window does not become the current window. @@ -2188,7 +2190,7 @@ If no .Ar target-session is specified, select the last window of the current session. .It Xo Ic link-window -.Op Fl adk +.Op Fl abdk .Op Fl s Ar src-window .Op Fl t Ar dst-window .Xc @@ -2203,9 +2205,12 @@ is specified and no such window exists, the .Ar src-window is linked there. With -.Fl a , -the window is moved to the next index up (following windows -are moved if necessary). +.Fl a +or +.Fl b +the window is moved to the next index after or before +.Ar dst-window +(existing windows are moved if necessary). If .Fl k is given and @@ -2272,7 +2277,7 @@ section. Does the same as .Ic join-pane . .It Xo Ic move-window -.Op Fl ardk +.Op Fl abrdk .Op Fl s Ar src-window .Op Fl t Ar dst-window .Xc @@ -2290,7 +2295,7 @@ the .Ic base-index option. .It Xo Ic new-window -.Op Fl adkP +.Op Fl abdkP .Op Fl c Ar start-directory .Op Fl e Ar environment .Op Fl F Ar format @@ -2301,10 +2306,12 @@ option. .D1 (alias: Ic neww ) Create a new window. With -.Fl a , -the new window is inserted at the next index up from the specified +.Fl a +or +.Fl b , +the new window is inserted at the next index after or before the specified .Ar target-window , -moving windows up if necessary, +moving windows up if necessary; otherwise .Ar target-window is the new window location. diff --git a/tmux.h b/tmux.h index 092e0b0e..9bc89628 100644 --- a/tmux.h +++ b/tmux.h @@ -2720,7 +2720,7 @@ void window_set_name(struct window *, const char *); void window_add_ref(struct window *, const char *); void window_remove_ref(struct window *, const char *); void winlink_clear_flags(struct winlink *); -int winlink_shuffle_up(struct session *, struct winlink *); +int winlink_shuffle_up(struct session *, struct winlink *, int); int window_pane_start_input(struct window_pane *, struct cmdq_item *, char **); void *window_pane_get_new_data(struct window_pane *, diff --git a/window.c b/window.c index 523e9913..66298495 100644 --- a/window.c +++ b/window.c @@ -1490,13 +1490,16 @@ winlink_clear_flags(struct winlink *wl) /* Shuffle window indexes up. */ int -winlink_shuffle_up(struct session *s, struct winlink *wl) +winlink_shuffle_up(struct session *s, struct winlink *wl, int before) { int idx, last; if (wl == NULL) return (-1); - idx = wl->idx + 1; + if (before) + idx = wl->idx; + else + idx = wl->idx + 1; /* Find the next free index. */ for (last = idx; last < INT_MAX; last++) { @@ -1509,8 +1512,9 @@ winlink_shuffle_up(struct session *s, struct winlink *wl) /* Move everything from last - 1 to idx up a bit. */ for (; last > idx; last--) { wl = winlink_find_by_index(&s->windows, last - 1); - server_link_window(s, wl, s, last, 0, 0, NULL); - server_unlink_window(s, wl); + RB_REMOVE(winlinks, &s->windows, wl); + wl->idx++; + RB_INSERT(winlinks, &s->windows, wl); } return (idx);