Tidy command prompt callbacks and pass in the client.

pull/761/merge
nicm 2017-05-17 15:20:23 +00:00
parent 31625c2d17
commit 91d202da7e
4 changed files with 35 additions and 40 deletions

View File

@ -32,7 +32,8 @@
static enum cmd_retval cmd_command_prompt_exec(struct cmd *,
struct cmdq_item *);
static int cmd_command_prompt_callback(void *, const char *, int);
static int cmd_command_prompt_callback(struct client *, void *,
const char *, int);
static void cmd_command_prompt_free(void *);
const struct cmd_entry cmd_command_prompt_entry = {
@ -48,17 +49,16 @@ const struct cmd_entry cmd_command_prompt_entry = {
};
struct cmd_command_prompt_cdata {
struct client *c;
int flags;
int flags;
char *inputs;
char *next_input;
char *inputs;
char *next_input;
char *prompts;
char *next_prompt;
char *prompts;
char *next_prompt;
char *template;
int idx;
char *template;
int idx;
};
static enum cmd_retval
@ -78,7 +78,6 @@ cmd_command_prompt_exec(struct cmd *self, struct cmdq_item *item)
return (CMD_RETURN_NORMAL);
cdata = xcalloc(1, sizeof *cdata);
cdata->c = c;
cdata->inputs = NULL;
cdata->next_input = NULL;
@ -142,10 +141,10 @@ cmd_command_prompt_error(struct cmdq_item *item, void *data)
}
static int
cmd_command_prompt_callback(void *data, const char *s, int done)
cmd_command_prompt_callback(struct client *c, void *data, const char *s,
int done)
{
struct cmd_command_prompt_cdata *cdata = data;
struct client *c = cdata->c;
struct cmd_list *cmdlist;
struct cmdq_item *new_item;
char *cause, *new_template, *prompt, *ptr;
@ -193,7 +192,7 @@ cmd_command_prompt_callback(void *data, const char *s, int done)
if (!done)
free(new_template);
if (c->prompt_callbackfn != cmd_command_prompt_callback)
if (c->prompt_inputcb != cmd_command_prompt_callback)
return (1);
return (0);
}

View File

@ -31,7 +31,8 @@
static enum cmd_retval cmd_confirm_before_exec(struct cmd *,
struct cmdq_item *);
static int cmd_confirm_before_callback(void *, const char *, int);
static int cmd_confirm_before_callback(struct client *, void *,
const char *, int);
static void cmd_confirm_before_free(void *);
const struct cmd_entry cmd_confirm_before_entry = {
@ -46,8 +47,7 @@ const struct cmd_entry cmd_confirm_before_entry = {
};
struct cmd_confirm_before_data {
char *cmd;
struct client *client;
char *cmd;
};
static enum cmd_retval
@ -74,9 +74,6 @@ cmd_confirm_before_exec(struct cmd *self, struct cmdq_item *item)
cdata = xmalloc(sizeof *cdata);
cdata->cmd = xstrdup(args->argv[0]);
cdata->client = c;
cdata->client->references++;
status_prompt_set(c, new_prompt, NULL,
cmd_confirm_before_callback, cmd_confirm_before_free, cdata,
PROMPT_SINGLE);
@ -97,10 +94,10 @@ cmd_confirm_before_error(struct cmdq_item *item, void *data)
}
static int
cmd_confirm_before_callback(void *data, const char *s, __unused int done)
cmd_confirm_before_callback(struct client *c, void *data, const char *s,
__unused int done)
{
struct cmd_confirm_before_data *cdata = data;
struct client *c = cdata->client;
struct cmd_list *cmdlist;
struct cmdq_item *new_item;
char *cause;
@ -135,9 +132,6 @@ static void
cmd_confirm_before_free(void *data)
{
struct cmd_confirm_before_data *cdata = data;
struct client *c = cdata->client;
server_client_unref(c);
free(cdata->cmd);
free(cdata);

View File

@ -656,8 +656,7 @@ status_message_redraw(struct client *c)
/* Enable status line prompt. */
void
status_prompt_set(struct client *c, const char *msg, const char *input,
int (*callbackfn)(void *, const char *, int), void (*freefn)(void *),
void *data, int flags)
prompt_input_cb inputcb, prompt_free_cb freecb, void *data, int flags)
{
struct format_tree *ft;
time_t t;
@ -677,8 +676,8 @@ status_prompt_set(struct client *c, const char *msg, const char *input,
c->prompt_buffer = utf8_fromcstr(tmp);
c->prompt_index = utf8_strlen(c->prompt_buffer);
c->prompt_callbackfn = callbackfn;
c->prompt_freefn = freefn;
c->prompt_inputcb = inputcb;
c->prompt_freecb = freecb;
c->prompt_data = data;
c->prompt_hindex = 0;
@ -692,7 +691,7 @@ status_prompt_set(struct client *c, const char *msg, const char *input,
if ((flags & PROMPT_INCREMENTAL) && *tmp != '\0') {
xasprintf(&cp, "=%s", tmp);
c->prompt_callbackfn(c->prompt_data, cp, 0);
c->prompt_inputcb(c, c->prompt_data, cp, 0);
free(cp);
}
@ -707,8 +706,8 @@ status_prompt_clear(struct client *c)
if (c->prompt_string == NULL)
return;
if (c->prompt_freefn != NULL && c->prompt_data != NULL)
c->prompt_freefn(c->prompt_data);
if (c->prompt_freecb != NULL && c->prompt_data != NULL)
c->prompt_freecb(c->prompt_data);
free(c->prompt_string);
c->prompt_string = NULL;
@ -995,7 +994,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_callbackfn(c->prompt_data, s, 1);
c->prompt_inputcb(c, c->prompt_data, s, 1);
status_prompt_clear(c);
free(s);
return (1);
@ -1276,13 +1275,13 @@ process_key:
s = utf8_tocstr(c->prompt_buffer);
if (*s != '\0')
status_prompt_add_history(s);
if (c->prompt_callbackfn(c->prompt_data, s, 1) == 0)
if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
status_prompt_clear(c);
free(s);
break;
case '\033': /* Escape */
case '\003': /* C-c */
if (c->prompt_callbackfn(c->prompt_data, NULL, 1) == 0)
if (c->prompt_inputcb(c, c->prompt_data, NULL, 1) == 0)
status_prompt_clear(c);
break;
case '\022': /* C-r */
@ -1330,7 +1329,7 @@ append_key:
s = utf8_tocstr(c->prompt_buffer);
if (strlen(s) != 1)
status_prompt_clear(c);
else if (c->prompt_callbackfn(c->prompt_data, s, 1) == 0)
else if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
status_prompt_clear(c);
free(s);
}
@ -1340,7 +1339,7 @@ changed:
if (c->prompt_flags & PROMPT_INCREMENTAL) {
s = utf8_tocstr(c->prompt_buffer);
xasprintf(&cp, "%c%s", prefix, s);
c->prompt_callbackfn(c->prompt_data, cp, 0);
c->prompt_inputcb(c, c->prompt_data, cp, 0);
free(cp);
free(s);
}

11
tmux.h
View File

@ -1284,6 +1284,8 @@ struct cmd_entry {
};
/* Client connection. */
typedef int (*prompt_input_cb)(struct client *, void *, const char *, int);
typedef void (*prompt_free_cb)(void *);
struct client {
const char *name;
struct tmuxpeer *peer;
@ -1353,7 +1355,8 @@ struct client {
struct key_table *keytable;
struct event identify_timer;
void (*identify_callback)(struct client *, struct window_pane *);
void (*identify_callback)(struct client *,
struct window_pane *);
void *identify_callback_data;
char *message_string;
@ -1364,8 +1367,8 @@ struct client {
char *prompt_string;
struct utf8_data *prompt_buffer;
size_t prompt_index;
int (*prompt_callbackfn)(void *, const char *, int);
void (*prompt_freefn)(void *);
prompt_input_cb prompt_inputcb;
prompt_free_cb prompt_freecb;
void *prompt_data;
u_int prompt_hindex;
enum { PROMPT_ENTRY, PROMPT_COMMAND } prompt_mode;
@ -1889,7 +1892,7 @@ void printflike(2, 3) status_message_set(struct client *, const char *, ...);
void status_message_clear(struct client *);
int status_message_redraw(struct client *);
void status_prompt_set(struct client *, const char *, const char *,
int (*)(void *, const char *, int), void (*)(void *), void *, int);
prompt_input_cb, prompt_free_cb, void *, int);
void status_prompt_clear(struct client *);
int status_prompt_redraw(struct client *);
int status_prompt_key(struct client *, key_code);