From f7195bec8290a12a29a96a49b1966446c41a5bad Mon Sep 17 00:00:00 2001 From: nicm Date: Wed, 7 Oct 2026 13:13:49 +0000 Subject: [PATCH] Add sort order flags to switch-mode, GitHub issue 5696 from harikp2002 at gmail dot com. --- cmd-choose-tree.c | 7 ++++--- tmux.1 | 14 ++++++++++++-- window-switch.c | 21 ++++++++++++++++++--- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/cmd-choose-tree.c b/cmd-choose-tree.c index 2dc304ff2..3ca85aa14 100644 --- a/cmd-choose-tree.c +++ b/cmd-choose-tree.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-choose-tree.c,v 1.58 2026/07/14 17:17:17 nicm Exp $ */ +/* $OpenBSD: cmd-choose-tree.c,v 1.59 2026/10/07 13:13:49 nicm Exp $ */ /* * Copyright (c) 2012 Thomas Adam @@ -88,8 +88,9 @@ const struct cmd_entry cmd_switch_mode_entry = { .name = "switch-mode", .alias = NULL, - .args = { "F:kst:wZ", 0, 1, cmd_choose_tree_args_parse }, - .usage = "[-kswZ] [-F format] " CMD_TARGET_PANE_USAGE " [command]", + .args = { "F:kO:rst:wZ", 0, 1, cmd_choose_tree_args_parse }, + .usage = "[-krsZw] [-F format] [-O sort-order] " CMD_TARGET_PANE_USAGE + " [command]", .target = { 't', CMD_FIND_PANE, 0 }, diff --git a/tmux.1 b/tmux.1 index 9d4672210..8893e075c 100644 --- a/tmux.1 +++ b/tmux.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: tmux.1,v 1.1179 2026/10/07 12:59:50 nicm Exp $ +.\" $OpenBSD: tmux.1,v 1.1180 2026/10/07 13:13:49 nicm Exp $ .\" .\" Copyright (c) 2007 Nicholas Marriott .\" @@ -3131,8 +3131,9 @@ The command works only if at least one client is attached. .It Xo .Ic switch\-mode -.Op Fl kswZ +.Op Fl krsZw .Op Fl F Ar format +.Op Fl O Ar sort\-order .Op Fl t Ar target\-pane .Op Ar command .Xc @@ -3145,6 +3146,15 @@ matching and only matching items are shown, sorted by how well they match. lists sessions (the default) and .Fl w lists windows. +.Fl O +specifies the initial sort order within matches of equal quality: one of +.Ql index , +.Ql name , +or +.Ql activity +(time). +.Fl r +reverses the sort order. .Fl Z zooms the pane. The following keys may be used in switch mode: diff --git a/window-switch.c b/window-switch.c index 1ff1007bb..24b3851ac 100644 --- a/window-switch.c +++ b/window-switch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: window-switch.c,v 1.4 2026/09/01 21:03:55 nicm Exp $ */ +/* $OpenBSD: window-switch.c,v 1.5 2026/10/07 13:13:49 nicm Exp $ */ /* * Copyright (c) 2026 Nicholas Marriott @@ -88,6 +88,8 @@ struct window_switch_modedata { char *command; enum window_switch_type type; + enum sort_order order; + int reversed; char *filter; struct prompt *prompt; u_int prompt_cx; @@ -191,8 +193,9 @@ window_switch_build(struct window_switch_modedata *data) struct winlink **wl; struct sort_criteria sort_crit; - sort_crit.order = SORT_NAME; - sort_crit.reversed = 0; + sort_crit.order = data->order; + sort_crit.reversed = data->reversed; + sort_crit.order_seq = NULL; for (i = 0; i < data->item_size; i++) window_switch_free_item(data->item_list[i]); @@ -346,6 +349,18 @@ window_switch_init(struct window_mode_entry *wme, wme->data = data = xcalloc(1, sizeof *data); data->wp = wp; + data->order = SORT_NAME; + data->reversed = 0; + if (args != NULL) { + if (args_has(args, 'O')) { + data->order = sort_order_from_string(args_get(args, 'O')); + if (data->order == SORT_END) + data->order = SORT_NAME; + } + if (args_has(args, 'r')) + data->reversed = 1; + } + if (args_has(args, 'w')) data->type = WINDOW_SWITCH_TYPE_WINDOW; else