Make confirm-before prompt customizable with -p option like

command-prompt. Also move responsibility for calling status_replace into
status_prompt_{set,update} and add #W and #P to the default kill-window
and kill-pane prompts. By Tiago Cunha.
This commit is contained in:
Nicholas Marriott 2011-07-08 06:37:57 +00:00
parent b4b3d9c936
commit 2de9b1e005
4 changed files with 49 additions and 52 deletions

View File

@ -90,8 +90,7 @@ cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
const char *inputs, *prompts; const char *inputs, *prompts;
struct cmd_command_prompt_cdata *cdata; struct cmd_command_prompt_cdata *cdata;
struct client *c; struct client *c;
char *input = NULL; char *prompt, *ptr, *input = NULL;
char *prompt, *prompt_replaced, *ptr;
size_t n; size_t n;
if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL) if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
@ -127,28 +126,18 @@ cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
ptr = strsep(&cdata->next_prompt, ","); ptr = strsep(&cdata->next_prompt, ",");
if (prompts == NULL) if (prompts == NULL)
prompt = xstrdup(ptr); prompt = xstrdup(ptr);
else { else
prompt_replaced = status_replace(c, NULL, NULL, NULL, ptr, xasprintf(&prompt, "%s ", ptr);
time(NULL), 0);
xasprintf(&prompt, "%s ", prompt_replaced);
xfree(prompt_replaced);
}
/* Get initial prompt input. */ /* Get initial prompt input. */
if ((inputs = args_get(args, 'I')) != NULL) { if ((inputs = args_get(args, 'I')) != NULL) {
cdata->inputs = xstrdup(inputs); cdata->inputs = xstrdup(inputs);
cdata->next_input = cdata->inputs; cdata->next_input = cdata->inputs;
ptr = strsep(&cdata->next_input, ","); input = strsep(&cdata->next_input, ",");
input = status_replace(c, NULL, NULL, NULL, ptr, time(NULL),
0);
} }
status_prompt_set(c, prompt, input, cmd_command_prompt_callback, status_prompt_set(c, prompt, input, cmd_command_prompt_callback,
cmd_command_prompt_free, cdata, 0); cmd_command_prompt_free, cdata, 0);
if (input != NULL)
xfree(input);
xfree(prompt); xfree(prompt);
return (0); return (0);
@ -161,8 +150,8 @@ cmd_command_prompt_callback(void *data, const char *s)
struct client *c = cdata->c; struct client *c = cdata->c;
struct cmd_list *cmdlist; struct cmd_list *cmdlist;
struct cmd_ctx ctx; struct cmd_ctx ctx;
char *cause, *new_template, *prompt; char *cause, *new_template, *prompt, *ptr;
char *prompt_replaced, *ptr, *input = NULL; char *input = NULL;
if (s == NULL) if (s == NULL)
return (0); return (0);
@ -176,23 +165,11 @@ cmd_command_prompt_callback(void *data, const char *s)
* and update the prompt data. * and update the prompt data.
*/ */
if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) { if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
prompt_replaced = status_replace(c, NULL, NULL, NULL, ptr, xasprintf(&prompt, "%s ", ptr);
time(NULL), 0); input = strsep(&cdata->next_input, ",");
xasprintf(&prompt, "%s ", prompt_replaced);
/* Find next input and expand special sequences. */
if ((ptr = strsep(&cdata->next_input, ",")) != NULL) {
input = status_replace(c, NULL, NULL, NULL, ptr,
time(NULL), 0);
}
status_prompt_update(c, prompt, input); status_prompt_update(c, prompt, input);
if (input != NULL)
xfree(input);
xfree(prompt_replaced);
xfree(prompt); xfree(prompt);
cdata->idx++; cdata->idx++;
return (1); return (1);
} }

View File

@ -33,8 +33,8 @@ void cmd_confirm_before_free(void *);
const struct cmd_entry cmd_confirm_before_entry = { const struct cmd_entry cmd_confirm_before_entry = {
"confirm-before", "confirm", "confirm-before", "confirm",
"t:", 1, 1, "p:t:", 1, 1,
CMD_TARGET_CLIENT_USAGE " command", "[-p prompt] " CMD_TARGET_CLIENT_USAGE " command",
0, 0,
cmd_confirm_before_key_binding, cmd_confirm_before_key_binding,
NULL, NULL,
@ -52,9 +52,11 @@ cmd_confirm_before_key_binding(struct cmd *self, int key)
switch (key) { switch (key) {
case '&': case '&':
self->args = args_create(1, "kill-window"); self->args = args_create(1, "kill-window");
args_set(self->args, 'p', "kill-window #W? (y/n)");
break; break;
case 'x': case 'x':
self->args = args_create(1, "kill-pane"); self->args = args_create(1, "kill-pane");
args_set(self->args, 'p', "kill-pane #P? (y/n)");
break; break;
default: default:
self->args = args_create(0); self->args = args_create(0);
@ -68,7 +70,8 @@ cmd_confirm_before_exec(struct cmd *self, struct cmd_ctx *ctx)
struct args *args = self->args; struct args *args = self->args;
struct cmd_confirm_before_data *cdata; struct cmd_confirm_before_data *cdata;
struct client *c; struct client *c;
char *buf, *cmd, *ptr; char *cmd, *copy, *new_prompt, *ptr;
const char *prompt;
if (ctx->curclient == NULL) { if (ctx->curclient == NULL) {
ctx->error(ctx, "must be run interactively"); ctx->error(ctx, "must be run interactively");
@ -78,19 +81,23 @@ cmd_confirm_before_exec(struct cmd *self, struct cmd_ctx *ctx)
if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL) if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
return (-1); return (-1);
ptr = xstrdup(args->argv[0]); if ((prompt = args_get(args, 'p')) != NULL)
if ((cmd = strtok(ptr, " \t")) == NULL) xasprintf(&new_prompt, "%s ", prompt);
cmd = ptr; else {
xasprintf(&buf, "Confirm '%s'? (y/n) ", cmd); ptr = copy = xstrdup(args->argv[0]);
xfree(ptr); cmd = strsep(&ptr, " \t");
xasprintf(&new_prompt, "Confirm '%s'? (y/n) ", cmd);
xfree(copy);
}
cdata = xmalloc(sizeof *cdata); cdata = xmalloc(sizeof *cdata);
cdata->cmd = xstrdup(args->argv[0]); cdata->cmd = xstrdup(args->argv[0]);
cdata->c = c; cdata->c = c;
status_prompt_set(cdata->c, buf, NULL, cmd_confirm_before_callback, status_prompt_set(cdata->c, new_prompt, NULL,
cmd_confirm_before_free, cdata, PROMPT_SINGLE); cmd_confirm_before_callback, cmd_confirm_before_free, cdata,
PROMPT_SINGLE);
xfree(buf); xfree(new_prompt);
return (1); return (1);
} }

View File

@ -824,12 +824,13 @@ status_prompt_set(struct client *c, const char *msg, const char *input,
status_message_clear(c); status_message_clear(c);
status_prompt_clear(c); status_prompt_clear(c);
c->prompt_string = xstrdup(msg); c->prompt_string = status_replace(c, NULL, NULL, NULL, msg,
time(NULL), 0);
if (input != NULL) if (input == NULL)
c->prompt_buffer = xstrdup(input); input = "";
else c->prompt_buffer = status_replace(c, NULL, NULL, NULL, input,
c->prompt_buffer = xstrdup(""); time(NULL), 0);
c->prompt_index = strlen(c->prompt_buffer); c->prompt_index = strlen(c->prompt_buffer);
c->prompt_callbackfn = callbackfn; c->prompt_callbackfn = callbackfn;
@ -877,13 +878,14 @@ void
status_prompt_update(struct client *c, const char *msg, const char *input) status_prompt_update(struct client *c, const char *msg, const char *input)
{ {
xfree(c->prompt_string); xfree(c->prompt_string);
c->prompt_string = xstrdup(msg); c->prompt_string = status_replace(c, NULL, NULL, NULL, msg,
time(NULL), 0);
xfree(c->prompt_buffer); xfree(c->prompt_buffer);
if (input != NULL) if (input == NULL)
c->prompt_buffer = xstrdup(input); input = "";
else c->prompt_buffer = status_replace(c, NULL, NULL, NULL, input,
c->prompt_buffer = xstrdup(""); time(NULL), 0);
c->prompt_index = strlen(c->prompt_buffer); c->prompt_index = strlen(c->prompt_buffer);
c->prompt_hindex = 0; c->prompt_hindex = 0;

11
tmux.1
View File

@ -2690,12 +2690,23 @@ to
.Ql %9 .Ql %9
.Pc . .Pc .
.It Xo Ic confirm-before .It Xo Ic confirm-before
.Op Fl p Ar prompt
.Op Fl t Ar target-client .Op Fl t Ar target-client
.Ar command .Ar command
.Xc .Xc
.D1 (alias: Ic confirm ) .D1 (alias: Ic confirm )
Ask for confirmation before executing Ask for confirmation before executing
.Ar command . .Ar command .
If
.Fl p
is given,
.Ar prompt
is the prompt to display; otherwise a prompt is constructed from
.Ar command .
It may contain the special character sequences supported by the
.Ic status-left
option.
.Pp
This command works only from inside This command works only from inside
.Nm . .Nm .
.It Xo Ic display-message .It Xo Ic display-message