Hi Nicholas,

That sounds perfect, thanks.  I was unable to 'patch' it in to my 2.4 source to 
test it for some reason, but I'm happy to wait for the next release to make use 
of that feature - meanwhile the solution with session_stack is working fine for 
me.

Thanks again, keep up the great work!  Regards,

Scott

-----Original Message-----
From: Nicholas Marriott [mailto:[email protected]] 
Sent: Wednesday, 3 May 2017 16:11
To: Scott ROCHFORD <[email protected]>
Cc: tmux-users <[email protected]>
Subject: Re: Way to access tmux internal window stack?

Oh yeah, good idea, we could add a format for the position in the stack
too, like this

But there is no way to specify sort order in choose mode and we are not
adding it right now because choose mode is going to change completely at
some point

You could use it with tmux lsw and sort -n though

Index: format.c
===================================================================
RCS file: /cvs/src/usr.bin/tmux/format.c,v
retrieving revision 1.132
diff -u -p -r1.132 format.c
--- format.c    3 May 2017 05:53:34 -0000       1.132
+++ format.c    3 May 2017 06:10:02 -0000
@@ -42,25 +42,6 @@ typedef void (*format_cb)(struct format_
 static char    *format_job_get(struct format_tree *, const char *);
 static void     format_job_timer(int, short, void *);
 
-static void     format_cb_host(struct format_tree *, struct format_entry *);
-static void     format_cb_host_short(struct format_tree *,
-                    struct format_entry *);
-static void     format_cb_pid(struct format_tree *, struct format_entry *);
-static void     format_cb_session_alerts(struct format_tree *,
-                    struct format_entry *);
-static void     format_cb_window_layout(struct format_tree *,
-                    struct format_entry *);
-static void     format_cb_window_visible_layout(struct format_tree *,
-                    struct format_entry *);
-static void     format_cb_start_command(struct format_tree *,
-                    struct format_entry *);
-static void     format_cb_current_command(struct format_tree *,
-                    struct format_entry *);
-static void     format_cb_history_bytes(struct format_tree *,
-                    struct format_entry *);
-static void     format_cb_pane_tabs(struct format_tree *,
-                    struct format_entry *);
-
 static char    *format_find(struct format_tree *, const char *, int);
 static void     format_add_cb(struct format_tree *, const char *, format_cb);
 static void     format_add_tv(struct format_tree *, const char *,
@@ -126,6 +107,7 @@ struct format_entry {
 /* Format entry tree. */
 struct format_tree {
        struct window           *w;
+       struct winlink          *wl;
        struct session          *s;
        struct window_pane      *wp;
 
@@ -416,7 +398,7 @@ format_cb_session_alerts(struct format_t
 {
        struct session  *s = ft->s;
        struct winlink  *wl;
-       char             alerts[256], tmp[16];
+       char             alerts[1024], tmp[16];
 
        if (s == NULL)
                return;
@@ -440,6 +422,48 @@ format_cb_session_alerts(struct format_t
        fe->value = xstrdup(alerts);
 }
 
+/* Callback for session_stack. */
+static void
+format_cb_session_stack(struct format_tree *ft, struct format_entry *fe)
+{
+       struct session  *s = ft->s;
+       struct winlink  *wl;
+       char             result[1024], tmp[16];
+
+       if (s == NULL)
+               return;
+
+       xsnprintf(result, sizeof result, "%u", s->curw->idx);
+       TAILQ_FOREACH(wl, &s->lastw, sentry) {
+               xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
+
+               if (*result != '\0')
+                       strlcat(result, ",", sizeof result);
+               strlcat(result, tmp, sizeof result);
+       }
+       fe->value = xstrdup(result);
+}
+
+/* Callback for window_stack_index. */
+static void
+format_cb_window_stack_index(struct format_tree *ft, struct format_entry *fe)
+{
+       struct session  *s = ft->wl->session;
+       struct winlink  *wl;
+       u_int            idx;
+
+       idx = 0;
+       TAILQ_FOREACH(wl, &s->lastw, sentry) {
+               idx++;
+               if (wl == ft->wl)
+                       break;
+       }
+       if (wl != NULL)
+               xasprintf(&fe->value, "%u", idx);
+       else
+               fe->value = xstrdup("0");
+}
+
 /* Callback for window_layout. */
 static void
 format_cb_window_layout(struct format_tree *ft, struct format_entry *fe)
@@ -1200,6 +1224,7 @@ format_defaults_session(struct format_tr
        format_add(ft, "session_many_attached", "%d", s->attached > 1);
 
        format_add_cb(ft, "session_alerts", format_cb_session_alerts);
+       format_add_cb(ft, "session_stack", format_cb_session_stack);
 }
 
 /* Set default format keys for a client. */
@@ -1286,10 +1311,12 @@ format_defaults_winlink(struct format_tr
 
        if (ft->w == NULL)
                ft->w = wl->window;
+       ft->wl = wl;
 
        format_defaults_window(ft, w);
 
        format_add(ft, "window_index", "%d", wl->idx);
+       format_add_cb(ft, "window_stack_index", format_cb_window_stack_index);
        format_add(ft, "window_flags", "%s", window_printable_flags(wl));
        format_add(ft, "window_active", "%d", wl == s->curw);
 
Index: tmux.1
===================================================================
RCS file: /cvs/src/usr.bin/tmux/tmux.1,v
retrieving revision 1.546
diff -u -p -r1.546 tmux.1
--- tmux.1      3 May 2017 05:53:34 -0000       1.546
+++ tmux.1      3 May 2017 06:10:03 -0000
@@ -3586,6 +3586,7 @@ The following variables are available, w
 .It Li "session_id" Ta "" Ta "Unique session ID"
 .It Li "session_many_attached" Ta "" Ta "1 if multiple clients attached"
 .It Li "session_name" Ta "#S" Ta "Name of session"
+.It Li "session_stack" Ta "" Ta "Window indexes in most recent order"
 .It Li "session_width" Ta "" Ta "Width of session"
 .It Li "session_windows" Ta "" Ta "Number of windows in session"
 .It Li "socket_path" Ta "" Ta "Server socket path"
@@ -3605,6 +3606,7 @@ The following variables are available, w
 .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"
+.It Li "window_stack_index" Ta "" Ta "Index in session most recent stack"
 .It Li "window_visible_layout" Ta "" Ta "Window layout description, respecting 
zoomed window panes"
 .It Li "window_width" Ta "" Ta "Width of window"
 .It Li "window_zoomed_flag" Ta "" Ta "1 if window is zoomed"




On Wed, May 03, 2017 at 04:02:58AM +0000, Scott ROCHFORD wrote:
> Hi Nicholas,
> 
> Your existing solution does already work for me with a binding such as this:
> 
> # switch to last-but-one window (like prefix-l but last, last)
> bind k run-shell "tmux select-window -t $(tmux list-windows -F 
> '#{session_stack}' | awk -F, '1{print $3;exit}')"
> 
> The 'choose-window' solution would just be useful for accessing windows even 
> further down the stack, otherwise I can't think of convenient key bindings to 
> use that are easy to remember.
> 
> Thanks again!  Regards,
> 
> Scott
> 
> -----Original Message-----
> From: Scott ROCHFORD 
> Sent: Wednesday, 3 May 2017 13:38
> To: 'Nicholas Marriott' <[email protected]>
> Cc: tmux-users <[email protected]>
> Subject: RE: Way to access tmux internal window stack?
> 
> Hi Nicholas,
> 
> Thanks, that seems to work with both list-windows and list-sessions - 
> although I'm not sure how best to access or use it.  Is there some less 
> clunky way for me to access such a session variable directly for "this" 
> session only rather than 'list-windows | uniq' or similar?
> 
> Another approach to use it with the 'choose-window' command in a way that it 
> would affect the sort order, if that's even possible.  Perhaps a 
> "window_stack_position" format would be more appropriate in that case.
> 
> Regards,
> 
> Scott
> 
> -----Original Message-----
> From: Nicholas Marriott [mailto:[email protected]] 
> Sent: Wednesday, 26 April 2017 17:43
> To: Scott ROCHFORD <[email protected]>
> Cc: tmux-users <[email protected]>
> Subject: Re: Way to access tmux internal window stack?
> 
> We could add a session_stack format, something like this:
> 
> Index: format.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/tmux/format.c,v
> retrieving revision 1.130
> diff -u -p -r1.130 format.c
> --- format.c  21 Apr 2017 14:01:19 -0000      1.130
> +++ format.c  26 Apr 2017 07:42:05 -0000
> @@ -48,6 +48,8 @@ static void  format_cb_host_short(struct
>  static void   format_cb_pid(struct format_tree *, struct format_entry *);
>  static void   format_cb_session_alerts(struct format_tree *,
>                    struct format_entry *);
> +static void   format_cb_session_stack(struct format_tree *,
> +                  struct format_entry *);
>  static void   format_cb_window_layout(struct format_tree *,
>                    struct format_entry *);
>  static void   format_cb_window_visible_layout(struct format_tree *,
> @@ -382,7 +384,7 @@ format_cb_session_alerts(struct format_t
>  {
>       struct session  *s = ft->s;
>       struct winlink  *wl;
> -     char             alerts[256], tmp[16];
> +     char             alerts[1024], tmp[16];
>  
>       if (s == NULL)
>               return;
> @@ -406,6 +408,28 @@ format_cb_session_alerts(struct format_t
>       fe->value = xstrdup(alerts);
>  }
>  
> +/* Callback for session_stack. */
> +static void
> +format_cb_session_stack(struct format_tree *ft, struct format_entry *fe)
> +{
> +     struct session  *s = ft->s;
> +     struct winlink  *wl;
> +     char             result[1024], tmp[16];
> +
> +     if (s == NULL)
> +             return;
> +
> +     xsnprintf(result, sizeof result, "%u", s->curw->idx);
> +     TAILQ_FOREACH(wl, &s->lastw, sentry) {
> +             xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
> +
> +             if (*result != '\0')
> +                     strlcat(result, ",", sizeof result);
> +             strlcat(result, tmp, sizeof result);
> +     }
> +     fe->value = xstrdup(result);
> +}
> +
>  /* Callback for window_layout. */
>  static void
>  format_cb_window_layout(struct format_tree *ft, struct format_entry *fe)
> @@ -1156,6 +1180,7 @@ format_defaults_session(struct format_tr
>       format_add(ft, "session_many_attached", "%d", s->attached > 1);
>  
>       format_add_cb(ft, "session_alerts", format_cb_session_alerts);
> +     format_add_cb(ft, "session_stack", format_cb_session_stack);
>  }
>  
>  /* Set default format keys for a client. */
> 
> 
> 
> 
> On Wed, Apr 26, 2017 at 07:33:57AM +0000, Scott ROCHFORD wrote:
> >    I have a LOT of windows open.  Often I want to toggle quickly between two
> >    of them... but sometimes three, or more.  So I thought I'd write my own
> >    key bindings to go to the last but one, or the last but two... (a l`a
> >    alt-tab,tab,tab in Windows) but without a way to access the MRU window
> >    list I don't know how to do so.  Can you think of something tricky?
> > 
> >     
> > 
> >    Regards,
> > 
> >     
> > 
> >    Scott
> > 
> >     
> > 
> >    P.S. apologies for duplicate emails Nicholas, forgot to CC the list.
> > 
> >     
> > 
> >    From: Nicholas Marriott [mailto:[email protected]]
> >    Sent: Wednesday, 26 April 2017 16:37
> >    To: Scott ROCHFORD <[email protected]>
> >    Cc: tmux-users <[email protected]>
> >    Subject: Re: Way to access tmux internal window stack?
> > 
> >     
> > 
> >    Hi
> > 
> >    There is no way to access it at the moment. What do you need to access it
> >    for?
> > 
> >     
> > 
> >     
> > 
> >     
> > 
> >     
> > 
> >     
> > 
> >    On 26 Apr 2017 7:19 am, "Scott ROCHFORD" <[email protected]>
> >    wrote:
> > 
> >      Hi,
> > 
> >       
> > 
> >      tmux appears to maintain an internal window stack (similar Windows MRU
> >      alt-tab stack) in most-recently-used order.
> > 
> >       
> > 
> >      You can toggle between the top two windows on the stack using the
> >      'last-window' command (default key binding prefix-l), however I can't
> >      find a way to access anything lower in the stack until you close those
> >      top windows.  Is there some way you can list the windows in internal
> >      stack order?  Do I need to make a feature request for a stack-order
> >      option to the 'list-windows' and 'select-window' commands perhaps?
> > 
> >       
> > 
> >      Regards,
> > 
> >      Scott
> > 
> >      --
> >      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 email to [email protected].
> >      For more options, visit https://groups.google.com/d/optout.
> > 
> >     

-- 
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