Discussion:
[PATCH] Window reference printable flag/format option
(too old to reply)
Thomas Adam
2015-05-02 21:57:20 UTC
Permalink
This adds support for indicating in the status line whether a given window
has been referenced more than once. In the case of grouped sessions this is
likely always going to be the case, but this also helps in showing which
windows in a session have been included via linkw, for example. Currently,
the window printable flag for this is '+'.

Also, add the corresponding #{window_references} flag for returning the
number of references on the window.
---
format.c | 2 ++
session.c | 15 +++++++++++++++
tmux.1 | 2 ++
tmux.h | 1 +
window.c | 2 ++
5 files changed, 22 insertions(+)

diff --git a/format.c b/format.c
index 9e9b6c8..a388a9a 100644
--- a/format.c
+++ b/format.c
@@ -589,6 +589,8 @@ format_defaults_window(struct format_tree *ft, struct window *w)
format_add(ft, "window_height", "%u", w->sy);
format_add(ft, "window_layout", "%s", layout);
format_add(ft, "window_panes", "%u", window_count_panes(w));
+ format_add(ft, "window_references", "%u",
+ session_count_window_refs(ft->s, ft->s->curw));
format_add(ft, "window_zoomed_flag", "%d",
!!(w->flags & WINDOW_ZOOMED));

diff --git a/session.c b/session.c
index 7d86f68..4076053 100644
--- a/session.c
+++ b/session.c
@@ -659,3 +659,18 @@ session_renumber_windows(struct session *s)
RB_FOREACH_SAFE(wl, winlinks, &old_wins, wl1)
winlink_remove(&old_wins, wl);
}
+
+/*
+ * Returns the number of refences on a window, which may be across a session
+ * group.
+ */
+u_int
+session_count_window_refs(struct session *s, struct winlink *wl)
+{
+ struct session_group *sg;
+
+ if ((sg = session_group_find(s)) != NULL)
+ return (session_group_count(sg));
+ else
+ return (wl->window->references);
+}
diff --git a/tmux.1 b/tmux.1
index b97fd8a..3e186d2 100644
--- a/tmux.1
+++ b/tmux.1
@@ -3352,6 +3352,7 @@ The following variables are available, where appropriate:
.It Li "window_layout" Ta "" Ta "Window layout description"
.It Li "window_name" Ta "#W" Ta "Name of window"
.It Li "window_panes" Ta "" Ta "Number of panes in window"
+.It Li "window_references" Ta "" Ta "Number of references a window has"
.It Li "window_silence_flag" Ta "" Ta "1 if window has silence alert"
.It Li "window_width" Ta "" Ta "Width of window"
.It Li "window_zoomed_flag" Ta "" Ta "1 if window is zoomed"
@@ -3508,6 +3509,7 @@ The flag is one of the following symbols appended to the window name:
.It Li "!" Ta "A bell has occurred in the window."
.It Li "~" Ta "The window has been silent for the monitor-silence interval."
.It Li "Z" Ta "The window's active pane is zoomed."
+.It Li "+" Ta "The window has been linked from another session."
.El
.Pp
The # symbol relates to the
diff --git a/tmux.h b/tmux.h
index dddd4f8..012f7f8 100644
--- a/tmux.h
+++ b/tmux.h
@@ -2340,6 +2340,7 @@ void session_group_synchronize_to(struct session *);
void session_group_synchronize_from(struct session *);
void session_group_synchronize1(struct session *, struct session *);
void session_renumber_windows(struct session *);
+u_int session_count_window_refs(struct session *, struct winlink *);

/* utf8.c */
void utf8_build(void);
diff --git a/window.c b/window.c
index 57e8ae4..57db62a 100644
--- a/window.c
+++ b/window.c
@@ -662,6 +662,8 @@ window_printable_flags(struct session *s, struct winlink *wl)
flags[pos++] = '-';
if (wl->window->flags & WINDOW_ZOOMED)
flags[pos++] = 'Z';
+ if (session_count_window_refs(s, wl) > 1)
+ flags[pos++] = '+';
if (pos == 0)
flags[pos++] = ' ';
flags[pos] = '\0';
--
2.1.4
Nicholas Marriott
2015-05-03 08:08:51 UTC
Permalink
Hi

format_defaults_window can't rely on ft->s being set. Perhaps you want
to put it in format_defaults_winlink, although check if that always has
the session.

I don't think it's good that every window in a grouped session should
suddenly grow a +.

Possibly you should just add the format and then add
#{?window_references,+,,} to window-status-format yourself rather than
making it a new fixed flag.

Also I think the count will be incorrect if the window is linked into
multiple non-grouped sessions and also in grouped sessions, if you don't
want to make it exact a 0 or 1 would be better.

I think I'd call it window_sessions if it's an exact count, or
window_linked if it is a flag.
Post by Thomas Adam
This adds support for indicating in the status line whether a given window
has been referenced more than once. In the case of grouped sessions this is
likely always going to be the case, but this also helps in showing which
windows in a session have been included via linkw, for example. Currently,
the window printable flag for this is '+'.
Also, add the corresponding #{window_references} flag for returning the
number of references on the window.
---
format.c | 2 ++
session.c | 15 +++++++++++++++
tmux.1 | 2 ++
tmux.h | 1 +
window.c | 2 ++
5 files changed, 22 insertions(+)
diff --git a/format.c b/format.c
index 9e9b6c8..a388a9a 100644
--- a/format.c
+++ b/format.c
@@ -589,6 +589,8 @@ format_defaults_window(struct format_tree *ft, struct window *w)
format_add(ft, "window_height", "%u", w->sy);
format_add(ft, "window_layout", "%s", layout);
format_add(ft, "window_panes", "%u", window_count_panes(w));
+ format_add(ft, "window_references", "%u",
+ session_count_window_refs(ft->s, ft->s->curw));
format_add(ft, "window_zoomed_flag", "%d",
!!(w->flags & WINDOW_ZOOMED));
diff --git a/session.c b/session.c
index 7d86f68..4076053 100644
--- a/session.c
+++ b/session.c
@@ -659,3 +659,18 @@ session_renumber_windows(struct session *s)
RB_FOREACH_SAFE(wl, winlinks, &old_wins, wl1)
winlink_remove(&old_wins, wl);
}
+
+/*
+ * Returns the number of refences on a window, which may be across a session
+ * group.
+ */
+u_int
+session_count_window_refs(struct session *s, struct winlink *wl)
+{
+ struct session_group *sg;
+
+ if ((sg = session_group_find(s)) != NULL)
+ return (session_group_count(sg));
+ else
+ return (wl->window->references);
+}
diff --git a/tmux.1 b/tmux.1
index b97fd8a..3e186d2 100644
--- a/tmux.1
+++ b/tmux.1
.It Li "window_layout" Ta "" Ta "Window layout description"
.It Li "window_name" Ta "#W" Ta "Name of window"
.It Li "window_panes" Ta "" Ta "Number of panes in window"
+.It Li "window_references" Ta "" Ta "Number of references a window has"
.It Li "window_silence_flag" Ta "" Ta "1 if window has silence alert"
.It Li "window_width" Ta "" Ta "Width of window"
.It Li "window_zoomed_flag" Ta "" Ta "1 if window is zoomed"
.It Li "!" Ta "A bell has occurred in the window."
.It Li "~" Ta "The window has been silent for the monitor-silence interval."
.It Li "Z" Ta "The window's active pane is zoomed."
+.It Li "+" Ta "The window has been linked from another session."
.El
.Pp
The # symbol relates to the
diff --git a/tmux.h b/tmux.h
index dddd4f8..012f7f8 100644
--- a/tmux.h
+++ b/tmux.h
@@ -2340,6 +2340,7 @@ void session_group_synchronize_to(struct session *);
void session_group_synchronize_from(struct session *);
void session_group_synchronize1(struct session *, struct session *);
void session_renumber_windows(struct session *);
+u_int session_count_window_refs(struct session *, struct winlink *);
/* utf8.c */
void utf8_build(void);
diff --git a/window.c b/window.c
index 57e8ae4..57db62a 100644
--- a/window.c
+++ b/window.c
@@ -662,6 +662,8 @@ window_printable_flags(struct session *s, struct winlink *wl)
flags[pos++] = '-';
if (wl->window->flags & WINDOW_ZOOMED)
flags[pos++] = 'Z';
+ if (session_count_window_refs(s, wl) > 1)
+ flags[pos++] = '+';
if (pos == 0)
flags[pos++] = ' ';
flags[pos] = '\0';
--
2.1.4
------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
tmux-users mailing list
https://lists.sourceforge.net/lists/listinfo/tmux-users
Thomas Adam
2015-05-03 12:33:55 UTC
Permalink
Post by Nicholas Marriott
Possibly you should just add the format and then add
#{?window_references,+,,} to window-status-format yourself rather than
making it a new fixed flag.
Yeah, fair enough. I've done this, and hence this leaves an extra space
in most cases due to how window_printable_flags() is calculated. See
the patch attached which shoves this out into its own flag as well. The
commit message explains the rationale.
Post by Nicholas Marriott
Also I think the count will be incorrect if the window is linked into
multiple non-grouped sessions and also in grouped sessions, if you don't
want to make it exact a 0 or 1 would be better.
Done. Now, the flag window_linked considered only unique windows
linked across sessions (via linkw)---even in the case of a grouped
session.

-- Thomas Adam
--
"Deep in my heart I wish I was wrong. But deep in my heart I know I am
not." -- Morrissey ("Girl Least Likely To" -- off of Viva Hate.)
Thomas Adam
2015-05-04 22:40:31 UTC
Permalink
Post by Nicholas Marriott
Hi
format_defaults_window can't rely on ft->s being set. Perhaps you want
to put it in format_defaults_winlink, although check if that always has
the session.
I don't think it's good that every window in a grouped session should
suddenly grow a +.
Here's an updated version:

* Now just uses #{?window_flags,#{window_flags},} to check if there are
any flags; removing the previous #{has_window_flags} format.

* The hard-coded space in window_printable_flags() has been removed,
allowing expansion of that through formats instead.

-- Thomas Adam
--
"Deep in my heart I wish I was wrong. But deep in my heart I know I am
not." -- Morrissey ("Girl Least Likely To" -- off of Viva Hate.)
Nicholas Marriott
2015-05-06 08:38:59 UTC
Permalink
Index: cmd-kill-window.c
===================================================================
RCS file: /cvs/src/usr.bin/tmux/cmd-kill-window.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -p -r1.16 -r1.17
--- cmd-kill-window.c 22 Oct 2014 23:11:41 -0000 1.16
+++ cmd-kill-window.c 6 May 2015 08:35:39 -0000 1.17
@@ -45,24 +45,17 @@ const struct cmd_entry cmd_unlink_window
enum cmd_retval
cmd_kill_window_exec(struct cmd *self, struct cmd_q *cmdq)
{
- struct args *args = self->args;
- struct winlink *wl, *wl2, *wl3;
- struct window *w;
- struct session *s;
- struct session_group *sg;
- u_int references;
+ struct args *args = self->args;
+ struct winlink *wl, *wl2, *wl3;
+ struct window *w;
+ struct session *s;

if ((wl = cmd_find_window(cmdq, args_get(args, 't'), &s)) == NULL)
return (CMD_RETURN_ERROR);
w = wl->window;

if (self->entry == &cmd_unlink_window_entry) {
- sg = session_group_find(s);
- if (sg != NULL)
- references = session_group_count(sg);
- else
- references = 1;
- if (!args_has(self->args, 'k') && w->references == references) {
+ if (!args_has(self->args, 'k') && !session_is_linked(s, w)) {
cmdq_error(cmdq, "window only linked to one session");
return (CMD_RETURN_ERROR);
}
Index: format.c
===================================================================
RCS file: /cvs/src/usr.bin/tmux/format.c,v
retrieving revision 1.63
retrieving revision 1.64
diff -u -p -r1.63 -r1.64
--- format.c 21 Apr 2015 22:38:49 -0000 1.63
+++ format.c 6 May 2015 08:35:39 -0000 1.64
@@ -621,6 +621,7 @@ format_defaults_winlink(struct format_tr
!!(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));

free(flags);
}
Index: options-table.c
===================================================================
RCS file: /cvs/src/usr.bin/tmux/options-table.c,v
retrieving revision 1.56
retrieving revision 1.57
diff -u -p -r1.56 -r1.57
--- options-table.c 29 Apr 2015 15:59:08 -0000 1.56
+++ options-table.c 6 May 2015 08:35:39 -0000 1.57
@@ -737,7 +737,7 @@ const struct options_table_entry window_

{ .name = "window-status-current-format",
.type = OPTIONS_TABLE_STRING,
- .default_str = "#I:#W#F"
+ .default_str = "#I:#W#{?window_flags,#{window_flags}, }"
},

{ .name = "window-status-current-style",
@@ -753,7 +753,7 @@ const struct options_table_entry window_

{ .name = "window-status-format",
.type = OPTIONS_TABLE_STRING,
- .default_str = "#I:#W#F"
+ .default_str = "#I:#W#{?window_flags,#{window_flags}, }"
},

{ .name = "window-status-last-attr",
Index: session.c
===================================================================
RCS file: /cvs/src/usr.bin/tmux/session.c,v
retrieving revision 1.48
retrieving revision 1.49
diff -u -p -r1.48 -r1.49
--- session.c 25 Apr 2015 18:09:28 -0000 1.48
+++ session.c 6 May 2015 08:35:39 -0000 1.49
@@ -335,6 +335,20 @@ session_has(struct session *s, struct wi
return (1);
}
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_find(s)) != NULL)
+ return (w->references != session_group_count(sg));
+ return (w->references != 1);
}

struct winlink *
Index: tmux.h
===================================================================
RCS file: /cvs/src/usr.bin/tmux/tmux.h,v
retrieving revision 1.503
retrieving revision 1.504
diff -u -p -r1.503 -r1.504
--- tmux.h 28 Apr 2015 10:43:14 -0000 1.503
+++ tmux.h 6 May 2015 08:35:39 -0000 1.504
@@ -2328,6 +2328,7 @@ struct winlink *session_attach(struct se
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);
Index: window.c
===================================================================
RCS file: /cvs/src/usr.bin/tmux/window.c,v
retrieving revision 1.127
retrieving revision 1.128
diff -u -p -r1.127 -r1.128
--- window.c 28 Apr 2015 10:43:14 -0000 1.127
+++ window.c 6 May 2015 08:35:39 -0000 1.128
@@ -643,7 +643,7 @@ window_destroy_panes(struct window *w)
}
}

-/* Return list of printable window flag symbols. No flags is just a space. */
+/* Retuns the printable flags on a window, empty string if no flags set. */
char *
window_printable_flags(struct session *s, struct winlink *wl)
{
@@ -663,8 +663,6 @@ window_printable_flags(struct session *s
flags[pos++] = '-';
if (wl->window->flags & WINDOW_ZOOMED)
flags[pos++] = 'Z';
- if (pos == 0)
- flags[pos++] = ' ';
flags[pos] = '\0';
return (xstrdup(flags));
}
Post by Thomas Adam
Post by Nicholas Marriott
Hi
format_defaults_window can't rely on ft->s being set. Perhaps you want
to put it in format_defaults_winlink, although check if that always has
the session.
I don't think it's good that every window in a grouped session should
suddenly grow a +.
* Now just uses #{?window_flags,#{window_flags},} to check if there are
any flags; removing the previous #{has_window_flags} format.
* The hard-coded space in window_printable_flags() has been removed,
allowing expansion of that through formats instead.
-- Thomas Adam
--
"Deep in my heart I wish I was wrong. But deep in my heart I know I am
not." -- Morrissey ("Girl Least Likely To" -- off of Viva Hate.)
From 6fd7702571fcc64b26d184a9b790e4f3fafdf952 Mon Sep 17 00:00:00 2001
Date: Sat, 2 May 2015 22:51:36 +0100
Subject: [PATCH] Window flags: window_linked
This primarily adds support for a new flag, 'window_linked' to indicate
whether the given window is linked across multiple sessions. In the case of
grouped sessions, only those windows explicitly linked via lastw are
counted. This is so that the flag doesn't always return 1 for windows in
grouped sessions.
To make this flag printable as part of the window_flags without hardcoding
it, and to not leave an extra space present (because no window flags means a
single space is printed), this extra space has been removed, and the default
options changed to add the space back in, so there's no regressions.
This means it's now entirely possible to override the window printable flags
through formats alone, albeit long-winded.
---
format.c | 3 +++
options-table.c | 4 ++--
session.c | 19 +++++++++++++++++++
tmux.1 | 2 ++
tmux.h | 1 +
window.c | 4 +---
6 files changed, 28 insertions(+), 5 deletions(-)
diff --git a/format.c b/format.c
index 9e9b6c8..8733331 100644
--- a/format.c
+++ b/format.c
@@ -602,6 +602,7 @@ format_defaults_winlink(struct format_tree *ft, struct session *s,
{
struct window *w = wl->window;
char *flags;
+ int window_has_flags;
if (ft->w == NULL)
ft->w = wl->window;
@@ -622,6 +623,8 @@ format_defaults_winlink(struct format_tree *ft, struct session *s,
!!(wl->flags & WINLINK_SILENCE));
format_add(ft, "window_last_flag", "%d",
!!(wl == TAILQ_FIRST(&s->lastw)));
+ format_add(ft, "window_linked", "%d",
+ session_count_window_refs(s, wl) > 1);
free(flags);
}
diff --git a/options-table.c b/options-table.c
index a4f3ac4..0bf6558 100644
--- a/options-table.c
+++ b/options-table.c
@@ -736,7 +736,7 @@ const struct options_table_entry window_options_table[] = {
{ .name = "window-status-current-format",
.type = OPTIONS_TABLE_STRING,
- .default_str = "#I:#W#F"
+ .default_str = "#I:#W#{?window_flags,#{window_flags},}"
},
{ .name = "window-status-current-style",
@@ -752,7 +752,7 @@ const struct options_table_entry window_options_table[] = {
{ .name = "window-status-format",
.type = OPTIONS_TABLE_STRING,
- .default_str = "#I:#W#F"
+ .default_str = "#I:#W#{?window_flags,#{window_flags}, }"
},
{ .name = "window-status-last-attr",
diff --git a/session.c b/session.c
index 7d86f68..072c84b 100644
--- a/session.c
+++ b/session.c
@@ -659,3 +659,22 @@ session_renumber_windows(struct session *s)
RB_FOREACH_SAFE(wl, winlinks, &old_wins, wl1)
winlink_remove(&old_wins, wl);
}
+
+/*
+ * Returns the number of refences on a window, which may be across a session
+ * group.
+ */
+u_int
+session_count_window_refs(struct session *s, struct winlink *wl)
+{
+ struct session_group *sg;
+
+ if ((sg = session_group_find(s)) != NULL) {
+ /*
+ * We only want to consider unique links for each session in a
+ * group, and not all of them.
+ */
+ return (session_group_count(sg) - wl->window->references);
+ } else
+ return (wl->window->references);
+}
diff --git a/tmux.1 b/tmux.1
index b97fd8a..f2861b9 100644
--- a/tmux.1
+++ b/tmux.1
.It Li "window_bell_flag" Ta "" Ta "1 if window has bell"
.It Li "window_find_matches" Ta "" Ta "Matched data from the find-window"
.It Li "window_flags" Ta "#F" Ta "Window flags"
+.It Li "window_has_flags" Ta "1 if the window has flags"
.It Li "window_height" Ta "" Ta "Height of window"
.It Li "window_id" Ta "" Ta "Unique window ID"
.It Li "window_index" Ta "#I" Ta "Index of window"
.It Li "window_last_flag" Ta "" Ta "1 if window is the last used"
.It Li "window_layout" Ta "" Ta "Window layout description"
+.It Li "window_linked" Ta "" Ta "1 if window is linked across sessions"
.It Li "window_name" Ta "#W" Ta "Name of window"
.It Li "window_panes" Ta "" Ta "Number of panes in window"
.It Li "window_silence_flag" Ta "" Ta "1 if window has silence alert"
diff --git a/tmux.h b/tmux.h
index dddd4f8..012f7f8 100644
--- a/tmux.h
+++ b/tmux.h
@@ -2340,6 +2340,7 @@ void session_group_synchronize_to(struct session *);
void session_group_synchronize_from(struct session *);
void session_group_synchronize1(struct session *, struct session *);
void session_renumber_windows(struct session *);
+u_int session_count_window_refs(struct session *, struct winlink *);
/* utf8.c */
void utf8_build(void);
diff --git a/window.c b/window.c
index 57e8ae4..2e4ddd0 100644
--- a/window.c
+++ b/window.c
@@ -642,7 +642,7 @@ window_destroy_panes(struct window *w)
}
}
-/* Return list of printable window flag symbols. No flags is just a space. */
+/* Retuns the printable flags on a window, empty string if no flags set. */
char *
window_printable_flags(struct session *s, struct winlink *wl)
{
@@ -662,8 +662,6 @@ window_printable_flags(struct session *s, struct winlink *wl)
flags[pos++] = '-';
if (wl->window->flags & WINDOW_ZOOMED)
flags[pos++] = 'Z';
- if (pos == 0)
- flags[pos++] = ' ';
flags[pos] = '\0';
return (xstrdup(flags));
}
--
2.1.4
Loading...