Add a loop_index variable and use it to show last used 5 sessions on the

session menu instead of next/previous.
This commit is contained in:
nicm
2026-06-19 16:25:43 +00:00
parent f06d930445
commit 624db256c6
3 changed files with 21 additions and 33 deletions

View File

@@ -1842,15 +1842,6 @@ format_cb_keypad_flag(struct format_tree *ft)
return (NULL);
}
/* Callback for loop_last_flag. */
static void *
format_cb_loop_last_flag(struct format_tree *ft)
{
if (ft->flags & FORMAT_LAST)
return (xstrdup("1"));
return (xstrdup("0"));
}
/* Callback for mouse_all_flag. */
static void *
format_cb_mouse_all_flag(struct format_tree *ft)
@@ -3331,9 +3322,6 @@ static const struct format_table_entry format_table[] = {
{ "last_window_index", FORMAT_TABLE_STRING,
format_cb_last_window_index
},
{ "loop_last_flag", FORMAT_TABLE_STRING,
format_cb_loop_last_flag
},
{ "mouse_all_flag", FORMAT_TABLE_STRING,
format_cb_mouse_all_flag
},
@@ -4668,7 +4656,7 @@ format_loop_sessions(struct format_expand_state *es, const char *fmt)
struct evbuffer *buffer;
size_t size;
struct session *s, **l;
int i, n, last = 0;
int i, n;
if (format_choose(es, fmt, &all, &active, 0) != 0) {
all = xstrdup(fmt);
@@ -4689,9 +4677,9 @@ format_loop_sessions(struct format_expand_state *es, const char *fmt)
use = active;
else
use = all;
if (i == n - 1)
last = FORMAT_LAST;
nft = format_create(c, item, FORMAT_NONE, ft->flags|last);
nft = format_create(c, item, FORMAT_NONE, ft->flags);
format_add(nft, "loop_index", "%d", i);
format_add(nft, "loop_last_flag", "%d", i == n - 1);
format_defaults(nft, ft->c, s, NULL, NULL);
format_copy_state(&next, es, 0);
next.ft = nft;
@@ -4783,7 +4771,7 @@ format_loop_windows(struct format_expand_state *es, const char *fmt)
size_t size;
struct winlink *wl, **l;
struct window *w;
int i, n, last = 0;
int i, n;
if (ft->s == NULL) {
format_log(es, "window loop but no session");
@@ -4808,10 +4796,10 @@ format_loop_windows(struct format_expand_state *es, const char *fmt)
use = active;
else
use = all;
if (i == n - 1)
last = FORMAT_LAST;
nft = format_create(c, item, FORMAT_WINDOW|w->id,
ft->flags|last);
ft->flags);
format_add(nft, "loop_index", "%d", i);
format_add(nft, "loop_last_flag", "%d", i == n - 1);
format_defaults(nft, ft->c, ft->s, wl, NULL);
/* Add neighbor window data to the format tree. */
@@ -4858,7 +4846,7 @@ format_loop_panes(struct format_expand_state *es, const char *fmt)
struct evbuffer *buffer;
size_t size;
struct window_pane *wp, **l;
int i, n, last = 0;
int i, n;
if (ft->w == NULL) {
format_log(es, "pane loop but no window");
@@ -4882,10 +4870,10 @@ format_loop_panes(struct format_expand_state *es, const char *fmt)
use = active;
else
use = all;
if (i == n - 1)
last = FORMAT_LAST;
nft = format_create(c, item, FORMAT_PANE|wp->id,
ft->flags|last);
ft->flags);
format_add(nft, "loop_index", "%d", i);
format_add(nft, "loop_last_flag", "%d", i == n - 1);
format_defaults(nft, ft->c, ft->s, ft->wl, wp);
format_copy_state(&next, es, 0);
next.ft = nft;
@@ -4920,7 +4908,7 @@ format_loop_clients(struct format_expand_state *es, const char *fmt)
char *expanded, *value;
struct evbuffer *buffer;
size_t size;
int i, n, last = 0;
int i, n;
buffer = evbuffer_new();
if (buffer == NULL)
@@ -4930,9 +4918,9 @@ format_loop_clients(struct format_expand_state *es, const char *fmt)
for (i = 0; i < n; i++) {
c = l[i];
format_log(es, "client loop: %s", c->name);
if (i == n - 1)
last = FORMAT_LAST;
nft = format_create(c, item, 0, ft->flags|last);
nft = format_create(c, item, 0, ft->flags);
format_add(nft, "loop_index", "%d", i);
format_add(nft, "loop_last_flag", "%d", i == n - 1);
format_defaults(nft, c, ft->s, ft->wl, ft->wp);
format_copy_state(&next, es, 0);
next.ft = nft;

View File

@@ -25,11 +25,10 @@
#include "tmux.h"
#define DEFAULT_SESSION_MENU \
" 'Next' 'n' {switch-client -n}" \
" 'Previous' 'p' {switch-client -p}" \
" #{S/t:#{?#{&&:#{<:#{loop_index},6},#{!:#{session_active}}},'Switch To #[underscore]#{session_name}' '' {switch-client -t=#{session_id}#} ,}}" \
" ''" \
" 'Renumber' 'N' {move-window -r}" \
" 'Rename' 'r' {command-prompt -I \"#S\" {rename-session -- '%%'}}" \
" 'Rename' 'r' {command-prompt -I '#S' {rename-session -- '%%'}}" \
" 'Detach' 'd' {detach-client}" \
" ''" \
" 'New Session' 's' {new-session}" \
@@ -486,8 +485,8 @@ key_bindings_init(void)
"bind -n WheelUpStatus { previous-window }",
/* Mouse button 3 down on status left. */
"bind -n MouseDown3StatusLeft { display-menu -t= -xM -yW -T '#[align=centre]#{session_name}' " DEFAULT_SESSION_MENU " }",
"bind -n M-MouseDown3StatusLeft { display-menu -t= -xM -yW -T '#[align=centre]#{session_name}' " DEFAULT_SESSION_MENU " }",
"bind -n MouseDown3StatusLeft { run -C \"display-menu -t= -xM -yW -T '#[align=centre]#{session_name}' " DEFAULT_SESSION_MENU "\" }",
"bind -n M-MouseDown3StatusLeft { run -C \"display-menu -t= -xM -yW -T '#[align=centre]#{session_name}' " DEFAULT_SESSION_MENU "\" }",
/* Mouse button 3 down on status line. */
"bind -n MouseDown3Status { display-menu -t= -xW -yW -T '#[align=centre]#{window_index}:#{window_name}' " DEFAULT_WINDOW_MENU "}",

1
tmux.1
View File

@@ -6774,6 +6774,7 @@ The following variables are available, where appropriate:
.It Li "keypad_flag" Ta "" Ta "Pane keypad flag"
.It Li "last_window_index" Ta "" Ta "Index of last window in session"
.It Li "line" Ta "" Ta "Line number in the list"
.It Li "loop_index" Ta "" Ta "Index of item in the W:, P:, S:, or L: loop"
.It Li "loop_last_flag" Ta "" Ta "1 if last window, pane, session, client in the W:, P:, S:, or L: loop"
.It Li "mouse_all_flag" Ta "" Ta "Pane mouse all flag"
.It Li "mouse_any_flag" Ta "" Ta "Pane mouse any flag"