Use flags for input callback instead of a single int done so the

callback can be told about cursor movement in an incremental prompt.
This commit is contained in:
nicm
2026-06-23 20:07:58 +00:00
parent de086f9848
commit c0a5e10313
7 changed files with 53 additions and 39 deletions

View File

@@ -35,7 +35,7 @@ static enum cmd_retval cmd_command_prompt_exec(struct cmd *,
struct cmdq_item *);
static int cmd_command_prompt_callback(struct client *, void *,
const char *, int);
const char *, key_code, int);
static void cmd_command_prompt_free(void *);
const struct cmd_entry cmd_command_prompt_entry = {
@@ -178,7 +178,7 @@ cmd_command_prompt_exec(struct cmd *self, struct cmdq_item *item)
static int
cmd_command_prompt_callback(struct client *c, void *data, const char *s,
int done)
__unused key_code key, int flags)
{
struct cmd_command_prompt_cdata *cdata = data;
char *error;
@@ -188,10 +188,10 @@ cmd_command_prompt_callback(struct client *c, void *data, const char *s,
int argc = 0;
char **argv = NULL;
if (s == NULL)
if (s == NULL || (flags & PROMPT_INPUT_MOVE))
goto out;
if (done) {
if (flags & PROMPT_INPUT_DONE) {
if (cdata->flags & PROMPT_INCREMENTAL)
goto out;
cmd_append_argv(&cdata->argc, &cdata->argv, s);
@@ -204,10 +204,9 @@ cmd_command_prompt_callback(struct client *c, void *data, const char *s,
argc = cdata->argc;
argv = cmd_copy_argv(cdata->argc, cdata->argv);
if (!done)
if (~flags & PROMPT_INPUT_DONE)
cmd_append_argv(&argc, &argv, s);
if (done) {
else {
cmd_free_argv(cdata->argc, cdata->argv);
cdata->argc = argc;
cdata->argv = cmd_copy_argv(argc, argv);

View File

@@ -34,7 +34,7 @@ static enum cmd_retval cmd_confirm_before_exec(struct cmd *,
struct cmdq_item *);
static int cmd_confirm_before_callback(struct client *, void *,
const char *, int);
const char *, key_code, int);
static void cmd_confirm_before_free(void *);
const struct cmd_entry cmd_confirm_before_entry = {
@@ -120,7 +120,7 @@ cmd_confirm_before_exec(struct cmd *self, struct cmdq_item *item)
static int
cmd_confirm_before_callback(struct client *c, void *data, const char *s,
__unused int done)
__unused key_code key, __unused int flags)
{
struct cmd_confirm_before_data *cdata = data;
struct cmdq_item *item = cdata->item, *new_item;

View File

@@ -1059,7 +1059,7 @@ mode_tree_search_set(struct mode_tree_data *mtd)
static int
mode_tree_search_callback(__unused struct client *c, void *data, const char *s,
__unused int done)
__unused key_code key, __unused int flags)
{
struct mode_tree_data *mtd = data;
@@ -1086,7 +1086,7 @@ mode_tree_search_free(void *data)
static int
mode_tree_filter_callback(__unused struct client *c, void *data, const char *s,
__unused int done)
__unused key_code key, __unused int flags)
{
struct mode_tree_data *mtd = data;

View File

@@ -670,9 +670,10 @@ static enum cmd_retval
status_prompt_accept(__unused struct cmdq_item *item, void *data)
{
struct client *c = data;
void *pd = c->prompt_data;
if (c->prompt_string != NULL) {
c->prompt_inputcb(c, c->prompt_data, "y", 1);
c->prompt_inputcb(c, pd, "y", KEYC_NONE, PROMPT_INPUT_DONE);
status_prompt_clear(c);
}
return (CMD_RETURN_NORMAL);
@@ -735,7 +736,7 @@ status_prompt_set(struct client *c, struct cmd_find_state *fs,
c->flags |= CLIENT_REDRAWSTATUS;
if (flags & PROMPT_INCREMENTAL)
c->prompt_inputcb(c, c->prompt_data, "=", 0);
c->prompt_inputcb(c, data, "=", KEYC_NONE, 0);
if ((flags & PROMPT_SINGLE) && (flags & PROMPT_ACCEPT))
cmdq_append(c, cmdq_get_callback(status_prompt_accept, c));
@@ -1440,20 +1441,34 @@ status_prompt_incremental_exit_key(key_code key)
return (0);
}
/* Fire input callback when done. */
static int
status_prompt_done(struct client *c, const char *s)
{
struct prompt_data *pd = c->prompt_data;
if (c->prompt_inputcb(c, pd, s, KEYC_NONE, PROMPT_INPUT_DONE) == 0) {
status_prompt_clear(c);
return (0);
}
return (1);
}
/* Handle keys in prompt. */
int
status_prompt_key(struct client *c, key_code key)
{
struct prompt_data *pd = c->prompt_data;
struct options *oo = c->session->options;
char *s, *cp, prefix = '=';
const char *histstr, *separators = NULL, *keystring;
const char *histstr, *separators = NULL, *ks;
size_t size, idx;
struct utf8_data tmp;
int keys, word_is_separators;
if (c->prompt_flags & PROMPT_KEY) {
keystring = key_string_lookup_key(key, 0);
c->prompt_inputcb(c, c->prompt_data, keystring, 1);
ks = key_string_lookup_key(key, 0);
c->prompt_inputcb(c, pd, ks, KEYC_NONE, PROMPT_INPUT_DONE);
status_prompt_clear(c);
return (0);
}
@@ -1466,7 +1481,7 @@ status_prompt_key(struct client *c, key_code key)
if (key >= '0' && key <= '9')
goto append_key;
s = utf8_tocstr(c->prompt_buffer);
c->prompt_inputcb(c, c->prompt_data, s, 1);
c->prompt_inputcb(c, pd, s, KEYC_NONE, PROMPT_INPUT_DONE);
status_prompt_clear(c);
free(s);
return (1);
@@ -1501,10 +1516,11 @@ process_key:
if ((c->prompt_flags & PROMPT_INCREMENTAL) &&
status_prompt_incremental_exit_key(key)) {
s = utf8_tocstr(c->prompt_buffer);
c->prompt_inputcb(c, c->prompt_data, s, 1);
status_prompt_clear(c);
free(s);
return (1);
if (status_prompt_done(c, s) == 0) {
free(s);
return (1);
}
return (0);
}
switch (key) {
@@ -1543,8 +1559,7 @@ process_key:
case KEYC_BSPACE:
case 'h'|KEYC_CTRL:
if (c->prompt_flags & PROMPT_BSPACE_EXIT && size == 0) {
if (c->prompt_inputcb(c, c->prompt_data, NULL, 1) == 0)
status_prompt_clear(c);
status_prompt_done(c, NULL);
break;
}
if (c->prompt_index != 0) {
@@ -1689,16 +1704,14 @@ process_key:
s = utf8_tocstr(c->prompt_buffer);
if (*s != '\0')
status_prompt_add_history(s, c->prompt_type);
if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
status_prompt_clear(c);
status_prompt_done(c, s);
free(s);
break;
case '\033': /* Escape */
case '['|KEYC_CTRL:
case 'c'|KEYC_CTRL:
case 'g'|KEYC_CTRL:
if (c->prompt_inputcb(c, c->prompt_data, NULL, 1) == 0)
status_prompt_clear(c);
status_prompt_done(c, NULL);
break;
case 'r'|KEYC_CTRL:
if (~c->prompt_flags & PROMPT_INCREMENTAL)
@@ -1763,8 +1776,7 @@ append_key:
status_prompt_clear(c);
else {
s = utf8_tocstr(c->prompt_buffer);
if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
status_prompt_clear(c);
status_prompt_done(c, s);
free(s);
}
}
@@ -1774,7 +1786,7 @@ changed:
if (c->prompt_flags & PROMPT_INCREMENTAL) {
s = utf8_tocstr(c->prompt_buffer);
xasprintf(&cp, "%c%s", prefix, s);
c->prompt_inputcb(c, c->prompt_data, cp, 0);
c->prompt_inputcb(c, pd, cp, KEYC_NONE, 0);
free(cp);
free(s);
}

5
tmux.h
View File

@@ -1991,7 +1991,10 @@ RB_HEAD(client_windows, client_window);
#define CLIENT_PASTE_TIME_LIMIT 5
/* Client connection. */
typedef int (*prompt_input_cb)(struct client *, void *, const char *, int);
#define PROMPT_INPUT_DONE 0x1
#define PROMPT_INPUT_MOVE 0x2
typedef int (*prompt_input_cb)(struct client *, void *, const char *, key_code,
int);
typedef void (*prompt_free_cb)(void *);
typedef struct visible_ranges *(*overlay_check_cb)(struct client *, void *,
u_int, u_int, u_int);

View File

@@ -981,7 +981,7 @@ window_customize_free_item_callback(void *itemdata)
static int
window_customize_set_option_callback(struct client *c, void *itemdata,
const char *s, __unused int done)
const char *s, __unused key_code key, __unused int flags)
{
struct window_customize_itemdata *item = itemdata;
struct window_customize_modedata *data = item->data;
@@ -1195,7 +1195,7 @@ window_customize_reset_option(struct window_customize_modedata *data,
static int
window_customize_set_command_callback(struct client *c, void *itemdata,
const char *s, __unused int done)
const char *s, __unused key_code key, __unused int flags)
{
struct window_customize_itemdata *item = itemdata;
struct window_customize_modedata *data = item->data;
@@ -1234,7 +1234,7 @@ fail:
static int
window_customize_set_note_callback(__unused struct client *c, void *itemdata,
const char *s, __unused int done)
const char *s, __unused key_code key, __unused int flags)
{
struct window_customize_itemdata *item = itemdata;
struct window_customize_modedata *data = item->data;
@@ -1372,7 +1372,7 @@ window_customize_change_each(void *modedata, void *itemdata,
static int
window_customize_change_current_callback(__unused struct client *c,
void *modedata, const char *s, __unused int done)
void *modedata, const char *s, __unused key_code key, __unused int flags)
{
struct window_customize_modedata *data = modedata;
struct window_customize_itemdata *item;
@@ -1408,7 +1408,7 @@ window_customize_change_current_callback(__unused struct client *c,
static int
window_customize_change_tagged_callback(struct client *c, void *modedata,
const char *s, __unused int done)
const char *s, __unused key_code key, __unused int flags)
{
struct window_customize_modedata *data = modedata;

View File

@@ -1082,7 +1082,7 @@ window_tree_command_done(__unused struct cmdq_item *item, void *modedata)
static int
window_tree_command_callback(struct client *c, void *modedata, const char *s,
__unused int done)
__unused key_code key, __unused int flags)
{
struct window_tree_modedata *data = modedata;
@@ -1141,7 +1141,7 @@ window_tree_kill_each(__unused void *modedata, void *itemdata,
static int
window_tree_kill_current_callback(struct client *c, void *modedata,
const char *s, __unused int done)
const char *s, __unused key_code key, __unused int flags)
{
struct window_tree_modedata *data = modedata;
struct mode_tree_data *mtd = data->data;
@@ -1162,7 +1162,7 @@ window_tree_kill_current_callback(struct client *c, void *modedata,
static int
window_tree_kill_tagged_callback(struct client *c, void *modedata,
const char *s, __unused int done)
const char *s, __unused key_code key, __unused int flags)
{
struct window_tree_modedata *data = modedata;
struct mode_tree_data *mtd = data->data;