From fe1fa7a5f1c9bfa1ed283eca424edd7ed1ebccee Mon Sep 17 00:00:00 2001 From: nicm Date: Wed, 17 Jun 2015 16:50:28 +0000 Subject: [PATCH] Move the shuffle code from new-window -a into a function and add a -a flag for move-window too. From Thomas Adam. --- usr.bin/tmux/cmd-move-window.c | 15 ++++++++++++--- usr.bin/tmux/cmd-new-window.c | 20 +++----------------- usr.bin/tmux/tmux.1 | 12 ++++++++---- usr.bin/tmux/tmux.h | 3 ++- usr.bin/tmux/window.c | 27 ++++++++++++++++++++++++++- 5 files changed, 51 insertions(+), 26 deletions(-) diff --git a/usr.bin/tmux/cmd-move-window.c b/usr.bin/tmux/cmd-move-window.c index dd80a64838e..ecb08574ba0 100644 --- a/usr.bin/tmux/cmd-move-window.c +++ b/usr.bin/tmux/cmd-move-window.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-move-window.c,v 1.19 2015/04/21 21:24:49 nicm Exp $ */ +/* $OpenBSD: cmd-move-window.c,v 1.20 2015/06/17 16:50:28 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -30,7 +30,7 @@ enum cmd_retval cmd_move_window_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_move_window_entry = { "move-window", "movew", - "dkrs:t:", 0, 0, + "adkrs:t:", 0, 0, "[-dkr] " CMD_SRCDST_WINDOW_USAGE, 0, cmd_move_window_exec @@ -38,7 +38,7 @@ const struct cmd_entry cmd_move_window_entry = { const struct cmd_entry cmd_link_window_entry = { "link-window", "linkw", - "dks:t:", 0, 0, + "adks:t:", 0, 0, "[-dk] " CMD_SRCDST_WINDOW_USAGE, 0, cmd_move_window_exec @@ -72,6 +72,15 @@ cmd_move_window_exec(struct cmd *self, struct cmd_q *cmdq) kflag = args_has(self->args, 'k'); dflag = args_has(self->args, 'd'); sflag = args_has(self->args, 's'); + + if (args_has(self->args, 'a')) { + s = cmd_find_session(cmdq, args_get(args, 't'), 0); + if (s == NULL) + return (CMD_RETURN_ERROR); + if ((idx = winlink_shuffle_up(s, s->curw)) == -1) + return (CMD_RETURN_ERROR); + } + if (server_link_window(src, wl, dst, idx, kflag, !dflag, &cause) != 0) { cmdq_error(cmdq, "can't link window: %s", cause); diff --git a/usr.bin/tmux/cmd-new-window.c b/usr.bin/tmux/cmd-new-window.c index fc6cb36da22..154ef57d37c 100644 --- a/usr.bin/tmux/cmd-new-window.c +++ b/usr.bin/tmux/cmd-new-window.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-new-window.c,v 1.45 2015/02/05 10:32:39 nicm Exp $ */ +/* $OpenBSD: cmd-new-window.c,v 1.46 2015/06/17 16:50:28 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -51,7 +51,7 @@ cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq) struct winlink *wl; const char *cmd, *path, *template; char **argv, *cause, *cp; - int argc, idx, last, detached, cwd, fd = -1; + int argc, idx, detached, cwd, fd = -1; struct format_tree *ft; struct environ_entry *envent; @@ -59,24 +59,10 @@ cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq) wl = cmd_find_window(cmdq, args_get(args, 't'), &s); if (wl == NULL) return (CMD_RETURN_ERROR); - idx = wl->idx + 1; - - /* Find the next free index. */ - for (last = idx; last < INT_MAX; last++) { - if (winlink_find_by_index(&s->windows, last) == NULL) - break; - } - if (last == INT_MAX) { + if ((idx = winlink_shuffle_up(s, wl)) == -1) { cmdq_error(cmdq, "no free window indexes"); return (CMD_RETURN_ERROR); } - - /* 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); - } } else { idx = cmd_find_index(cmdq, args_get(args, 't'), &s); if (idx == -2) diff --git a/usr.bin/tmux/tmux.1 b/usr.bin/tmux/tmux.1 index 003d3db810e..9e5e0b484b8 100644 --- a/usr.bin/tmux/tmux.1 +++ b/usr.bin/tmux/tmux.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: tmux.1,v 1.438 2015/06/15 10:58:01 nicm Exp $ +.\" $OpenBSD: tmux.1,v 1.439 2015/06/17 16:50:28 nicm Exp $ .\" .\" Copyright (c) 2007 Nicholas Marriott .\" @@ -14,7 +14,7 @@ .\" IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING .\" OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: June 15 2015 $ +.Dd $Mdocdate: June 17 2015 $ .Dt TMUX 1 .Os .Sh NAME @@ -1544,7 +1544,7 @@ If no .Ar target-session is specified, select the last window of the current session. .It Xo Ic link-window -.Op Fl dk +.Op Fl adk .Op Fl s Ar src-window .Op Fl t Ar dst-window .Xc @@ -1558,6 +1558,10 @@ If 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). If .Fl k is given and @@ -1623,7 +1627,7 @@ and .Ar dst-pane may belong to the same window. .It Xo Ic move-window -.Op Fl rdk +.Op Fl ardk .Op Fl s Ar src-window .Op Fl t Ar dst-window .Xc diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 36a9648dfc1..91f72bcbf15 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.525 2015/06/17 16:44:49 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.526 2015/06/17 16:50:28 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -2166,6 +2166,7 @@ struct window_pane *window_pane_find_right(struct window_pane *); void window_set_name(struct window *, const char *); void window_remove_ref(struct window *); void winlink_clear_flags(struct winlink *); +int winlink_shuffle_up(struct session *, struct winlink *); /* layout.c */ u_int layout_count_cells(struct layout_cell *); diff --git a/usr.bin/tmux/window.c b/usr.bin/tmux/window.c index 14f14fea6a9..bd25c6f827c 100644 --- a/usr.bin/tmux/window.c +++ b/usr.bin/tmux/window.c @@ -1,4 +1,4 @@ -/* $OpenBSD: window.c,v 1.133 2015/06/15 10:58:01 nicm Exp $ */ +/* $OpenBSD: window.c,v 1.134 2015/06/17 16:50:28 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -1378,3 +1378,28 @@ winlink_clear_flags(struct winlink *wl) } } } + +int +winlink_shuffle_up(struct session *s, struct winlink *wl) +{ + int idx, last; + + idx = wl->idx + 1; + + /* Find the next free index. */ + for (last = idx; last < INT_MAX; last++) { + if (winlink_find_by_index(&s->windows, last) == NULL) + break; + } + if (last == INT_MAX) + return (-1); + + /* 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); + } + + return (idx); +} -- 2.20.1