Allow the initial context on prompts to be set with the new -I option to

command-prompt. From Tiago Cunha.
This commit is contained in:
Nicholas Marriott 2011-07-02 21:05:44 +00:00
parent 1202284f37
commit ad60a2c952
5 changed files with 73 additions and 24 deletions

View File

@ -20,6 +20,7 @@
#include <ctype.h>
#include <string.h>
#include <time.h>
#include "tmux.h"
@ -36,8 +37,8 @@ void cmd_command_prompt_free(void *);
const struct cmd_entry cmd_command_prompt_entry = {
"command-prompt", NULL,
"p:t:", 0, 1,
CMD_TARGET_CLIENT_USAGE " [-p prompts] [template]",
"I:p:t:", 0, 1,
"[-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " [template]",
0,
cmd_command_prompt_key_binding,
NULL,
@ -46,6 +47,8 @@ const struct cmd_entry cmd_command_prompt_entry = {
struct cmd_command_prompt_cdata {
struct client *c;
char *inputs;
char *next_input;
char *next_prompt;
char *prompts;
char *template;
@ -79,9 +82,10 @@ int
cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
{
struct args *args = self->args;
const char *prompts;
const char *inputs, *prompts;
struct cmd_command_prompt_cdata *cdata;
struct client *c;
char *input = NULL;
char *prompt, *prompt_replaced, *ptr;
size_t n;
@ -94,6 +98,8 @@ cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
cdata = xmalloc(sizeof *cdata);
cdata->c = c;
cdata->idx = 1;
cdata->inputs = NULL;
cdata->next_input = NULL;
cdata->next_prompt = NULL;
cdata->prompts = NULL;
cdata->template = NULL;
@ -103,8 +109,7 @@ cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
else
cdata->template = xstrdup("%1");
prompts = args_get(args, 'p');
if (prompts != NULL)
if ((prompts = args_get(args, 'p')) != NULL)
cdata->prompts = xstrdup(prompts);
else if (args->argc != 0) {
n = strcspn(cdata->template, " ,");
@ -112,6 +117,7 @@ cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
} else
cdata->prompts = xstrdup(":");
/* Get first prompt. */
cdata->next_prompt = cdata->prompts;
ptr = strsep(&cdata->next_prompt, ",");
if (prompts == NULL)
@ -122,8 +128,22 @@ cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
xasprintf(&prompt, "%s ", prompt_replaced);
xfree(prompt_replaced);
}
status_prompt_set(c, prompt, cmd_command_prompt_callback,
/* Get initial prompt input. */
if ((inputs = args_get(args, 'I')) != NULL) {
cdata->inputs = xstrdup(inputs);
cdata->next_input = cdata->inputs;
ptr = 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,
cmd_command_prompt_free, cdata, 0);
if (input != NULL)
xfree(input);
xfree(prompt);
return (0);
@ -136,29 +156,43 @@ cmd_command_prompt_callback(void *data, const char *s)
struct client *c = cdata->c;
struct cmd_list *cmdlist;
struct cmd_ctx ctx;
char *cause, *newtempl, *prompt, *ptr;
char *prompt_replaced;
char *cause, *new_template, *prompt;
char *prompt_replaced, *ptr, *input = NULL;
if (s == NULL)
return (0);
newtempl = cmd_template_replace(cdata->template, s, cdata->idx);
new_template = cmd_template_replace(cdata->template, s, cdata->idx);
xfree(cdata->template);
cdata->template = newtempl;
cdata->template = new_template;
/*
* Check if there are more prompts; if so, get its respective input
* and update the prompt data.
*/
if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
prompt_replaced = status_replace(c, NULL, NULL, NULL, ptr,
time(NULL), 0);
xasprintf(&prompt, "%s ", prompt_replaced);
status_prompt_update(c, prompt);
/* 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);
if (input != NULL)
xfree(input);
xfree(prompt_replaced);
xfree(prompt);
cdata->idx++;
return (1);
}
if (cmd_string_parse(newtempl, &cmdlist, &cause) != 0) {
if (cmd_string_parse(new_template, &cmdlist, &cause) != 0) {
if (cause != NULL) {
*cause = toupper((u_char) *cause);
status_message_set(c, "%s", cause);
@ -189,6 +223,8 @@ cmd_command_prompt_free(void *data)
{
struct cmd_command_prompt_cdata *cdata = data;
if (cdata->inputs != NULL)
xfree(cdata->inputs);
if (cdata->prompts != NULL)
xfree(cdata->prompts);
if (cdata->template != NULL)

View File

@ -87,9 +87,8 @@ cmd_confirm_before_exec(struct cmd *self, struct cmd_ctx *ctx)
cdata = xmalloc(sizeof *cdata);
cdata->cmd = xstrdup(args->argv[0]);
cdata->c = c;
status_prompt_set(cdata->c, buf,
cmd_confirm_before_callback, cmd_confirm_before_free, cdata,
PROMPT_SINGLE);
status_prompt_set(cdata->c, buf, NULL, cmd_confirm_before_callback,
cmd_confirm_before_free, cdata, PROMPT_SINGLE);
xfree(buf);
return (1);

View File

@ -815,7 +815,7 @@ status_message_redraw(struct client *c)
/* Enable status line prompt. */
void
status_prompt_set(struct client *c, const char *msg,
status_prompt_set(struct client *c, const char *msg, const char *input,
int (*callbackfn)(void *, const char *), void (*freefn)(void *),
void *data, int flags)
{
@ -826,8 +826,11 @@ status_prompt_set(struct client *c, const char *msg,
c->prompt_string = xstrdup(msg);
c->prompt_buffer = xstrdup("");
c->prompt_index = 0;
if (input != NULL)
c->prompt_buffer = xstrdup(input);
else
c->prompt_buffer = xstrdup("");
c->prompt_index = strlen(c->prompt_buffer);
c->prompt_callbackfn = callbackfn;
c->prompt_freefn = freefn;
@ -871,13 +874,17 @@ status_prompt_clear(struct client *c)
/* Update status line prompt with a new prompt string. */
void
status_prompt_update(struct client *c, const char *msg)
status_prompt_update(struct client *c, const char *msg, const char *input)
{
xfree(c->prompt_string);
c->prompt_string = xstrdup(msg);
*c->prompt_buffer = '\0';
c->prompt_index = 0;
xfree(c->prompt_buffer);
if (input != NULL)
c->prompt_buffer = xstrdup(input);
else
c->prompt_buffer = xstrdup("");
c->prompt_index = strlen(c->prompt_buffer);
c->prompt_hindex = 0;

9
tmux.1
View File

@ -2631,6 +2631,7 @@ session option.
Commands related to the status line are as follows:
.Bl -tag -width Ds
.It Xo Ic command-prompt
.Op Fl I Ar inputs
.Op Fl p Ar prompts
.Op Fl t Ar target-client
.Op Ar template
@ -2643,6 +2644,9 @@ to execute commands interactively.
If
.Ar template
is specified, it is used as the command.
If present,
.Fl I
is a comma-separated list of the initial text for each prompt.
If
.Fl p
is given,
@ -2653,7 +2657,10 @@ a single prompt is displayed, constructed from
if it is present, or
.Ql \&:
if not.
The
.Pp
Both
.Ar inputs
and
.Ar prompts
may contain the special character sequences supported by the
.Ic status-left

4
tmux.h
View File

@ -1704,12 +1704,12 @@ char *status_replace(struct client *, struct session *,
void printflike2 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 *,
void status_prompt_set(struct client *, const char *, const char *,
int (*)(void *, const char *), void (*)(void *), void *, int);
void status_prompt_clear(struct client *);
int status_prompt_redraw(struct client *);
void status_prompt_key(struct client *, int);
void status_prompt_update(struct client *, const char *);
void status_prompt_update(struct client *, const char *, const char *);
/* resize.c */
void recalculate_sizes(void);