The original implementation of working out if a window was linked used
the window's reference count.  This is fragile.  Instead, use a flag on
the winlink to indicate if a winlink is linked.  This also avoids the
need for counting winlinks (i.e., looping).

No functional change.
---
 cmd-kill-window.c |  4 ++--
 format.c          |  2 +-
 server-fn.c       |  5 +++++
 session.c         | 14 --------------
 tmux.h            |  2 +-
 5 files changed, 9 insertions(+), 18 deletions(-)

diff --git a/cmd-kill-window.c b/cmd-kill-window.c
index 50df83ee..90359b3e 100644
--- a/cmd-kill-window.c
+++ b/cmd-kill-window.c
@@ -57,11 +57,11 @@ cmd_kill_window_exec(struct cmd *self, struct cmdq_item 
*item)
 {
        struct args             *args = self->args;
        struct winlink          *wl = item->target.wl, *wl2, *wl3;
-       struct window           *w = wl->window;
        struct session          *s = item->target.s;
 
        if (self->entry == &cmd_unlink_window_entry) {
-               if (!args_has(self->args, 'k') && !session_is_linked(s, w)) {
+               if (!args_has(self->args, 'k') &&
+                   !(wl->flags & WINLINK_LINKED)) {
                        cmdq_error(item, "window only linked to one session");
                        return (CMD_RETURN_ERROR);
                }
diff --git a/format.c b/format.c
index 6d7e5bfe..cd57e938 100644
--- a/format.c
+++ b/format.c
@@ -1380,7 +1380,7 @@ format_defaults_winlink(struct format_tree *ft, struct 
winlink *wl)
            !!(wl->flags & WINLINK_SILENCE));
        format_add(ft, "window_last_flag", "%d",
            !!(wl == TAILQ_FIRST(&s->lastw)));
-       format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
+       format_add(ft, "window_linked", "%d", !!(wl->flags & WINLINK_LINKED));
 }
 
 /* Set default format keys for a window pane. */
diff --git a/server-fn.c b/server-fn.c
index bf349469..879a8732 100644
--- a/server-fn.c
+++ b/server-fn.c
@@ -260,12 +260,17 @@ server_link_window(struct session *src, struct winlink 
*srcwl,
                session_select(dst, dstwl->idx);
        server_redraw_session_group(dst);
 
+       srcwl->flags |= WINLINK_LINKED;
+       dstwl->flags |= WINLINK_LINKED;
+
        return (0);
 }
 
 void
 server_unlink_window(struct session *s, struct winlink *wl)
 {
+       wl->flags &= ~WINLINK_LINKED;
+
        if (session_detach(s, wl))
                server_destroy_session_group(s);
        else
diff --git a/session.c b/session.c
index 6143807c..011d2bdb 100644
--- a/session.c
+++ b/session.c
@@ -437,20 +437,6 @@ session_has(struct session *s, struct window *w)
        return (0);
 }
 
-/*
- * Return 1 if a window is linked outside this session (not including session
- * groups). The window must be in this session!
- */
-int
-session_is_linked(struct session *s, struct window *w)
-{
-       struct session_group    *sg;
-
-       if ((sg = session_group_contains(s)) != NULL)
-               return (w->references != session_group_count(sg));
-       return (w->references != 1);
-}
-
 static struct winlink *
 session_next_alert(struct winlink *wl)
 {
diff --git a/tmux.h b/tmux.h
index c18958d9..610ac515 100644
--- a/tmux.h
+++ b/tmux.h
@@ -888,6 +888,7 @@ struct winlink {
 #define WINLINK_ACTIVITY 0x2
 #define WINLINK_SILENCE 0x4
 #define WINLINK_ALERTFLAGS (WINLINK_BELL|WINLINK_ACTIVITY|WINLINK_SILENCE)
+#define WINLINK_LINKED 0x8
 
        RB_ENTRY(winlink) entry;
        TAILQ_ENTRY(winlink) wentry;
@@ -2310,7 +2311,6 @@ struct winlink    *session_attach(struct session *, 
struct window *, int,
                     char **);
 int             session_detach(struct session *, struct winlink *);
 int             session_has(struct session *, struct window *);
-int             session_is_linked(struct session *, struct window *);
 int             session_next(struct session *, int);
 int             session_previous(struct session *, int);
 int             session_select(struct session *, int);
-- 
2.13.2

-- 
You received this message because you are subscribed to the Google Groups 
"tmux-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to