Add -f to switch-mode to read choices from a file, GitHub issue 5738.

This commit is contained in:
nicm
2026-10-15 08:07:17 +00:00
committed by tmux update bot
parent 1fbadf9b41
commit 73d8f81c2c
4 changed files with 163 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: cmd-choose-tree.c,v 1.60 2026/10/09 13:12:14 nicm Exp $ */
/* $OpenBSD: cmd-choose-tree.c,v 1.61 2026/10/15 08:07:17 nicm Exp $ */
/*
* Copyright (c) 2012 Thomas Adam <thomas@xteddy.org>
@@ -18,6 +18,9 @@
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include "tmux.h"
/*
@@ -28,6 +31,9 @@ static enum args_parse_type cmd_choose_tree_args_parse(struct args *args,
u_int idx, char **cause);
static enum cmd_retval cmd_choose_tree_exec(struct cmd *,
struct cmdq_item *);
static void cmd_choose_tree_read_done(struct client *,
const char *, int, int, struct evbuffer *,
void *);
const struct cmd_entry cmd_choose_tree_entry = {
.name = "choose-tree",
@@ -93,9 +99,9 @@ const struct cmd_entry cmd_switch_mode_entry = {
.description = "Switch the active pane mode.",
.alias = NULL,
.args = { "F:kO:rst:wZ", 0, 1, cmd_choose_tree_args_parse },
.usage = "[-krsZw] [-F format] [-O sort-order] " CMD_TARGET_PANE_USAGE
" [command]",
.args = { "F:f:kO:rst:wZ", 0, 1, cmd_choose_tree_args_parse },
.usage = "[-krsZw] [-F format] [-f path] [-O sort-order] "
CMD_TARGET_PANE_USAGE " [command]",
.target = { 't', CMD_FIND_PANE, 0 },
@@ -131,9 +137,11 @@ cmd_choose_tree_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = cmd_get_args(self);
struct cmd_find_state *target = cmdq_get_target(item);
struct client *c = cmdq_get_client(item);
struct window_pane *wp = target->wp;
const struct window_mode *mode;
enum sort_order order;
char *path;
order = sort_order_from_string(args_get(args, 'O'));
if (order == SORT_END && args_has(args, 'O')) {
@@ -158,6 +166,53 @@ cmd_choose_tree_exec(struct cmd *self, struct cmdq_item *item)
else
mode = &window_tree_mode;
if (mode == &window_switch_mode && args_has(args, 'f')) {
path = format_single_from_target(item, args_get(args, 'f'));
file_read(c, path, cmd_choose_tree_read_done, item);
free(path);
return (CMD_RETURN_WAIT);
}
window_pane_set_mode(wp, NULL, mode, item, target, args);
return (CMD_RETURN_NORMAL);
}
static void
cmd_choose_tree_read_done(__unused struct client *c, const char *path,
int error, int closed, struct evbuffer *buffer, void *data)
{
struct cmdq_item *item = data;
struct cmd *cmd = cmdq_get_cmd(item);
struct args *args = cmd_get_args(cmd);
struct cmd_find_state *target = cmdq_get_target(item);
struct window_pane *wp = target->wp;
struct window_mode_entry *wme;
char *buf = EVBUFFER_DATA(buffer);
size_t len = EVBUFFER_LENGTH(buffer);
if (!closed)
return;
if (error != 0) {
cmdq_error(item, "%s: %s", strerror(error), path);
goto out;
}
if (len != 0 && memchr(buf, '\0', len) != NULL) {
cmdq_error(item, "%s: contains binary data", path);
goto out;
}
if (!cmd_find_valid_state(target)) {
cmdq_error(item, "target pane no longer exists");
goto out;
}
TAILQ_FOREACH(wme, &wp->modes, entry) {
if (wme->mode == &window_switch_mode)
break;
}
if (window_pane_set_mode(wp, NULL, &window_switch_mode, item, target,
args) == 0 && wme == NULL)
window_switch_set_input(TAILQ_FIRST(&wp->modes), buffer);
out:
cmdq_continue(item);
}