2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prompt for command in client.
|
|
|
|
*/
|
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
void cmd_command_prompt_key_binding(struct cmd *, int);
|
|
|
|
int cmd_command_prompt_check(struct args *);
|
|
|
|
int cmd_command_prompt_exec(struct cmd *, struct cmd_ctx *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
int cmd_command_prompt_callback(void *, const char *);
|
|
|
|
void cmd_command_prompt_free(void *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_command_prompt_entry = {
|
|
|
|
"command-prompt", NULL,
|
2011-01-05 22:38:28 +00:00
|
|
|
"p:t:", 0, 1,
|
2009-08-19 10:39:50 +00:00
|
|
|
CMD_TARGET_CLIENT_USAGE " [-p prompts] [template]",
|
2011-01-04 00:42:46 +00:00
|
|
|
0,
|
|
|
|
cmd_command_prompt_key_binding,
|
|
|
|
NULL,
|
|
|
|
cmd_command_prompt_exec
|
2009-08-19 10:39:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct cmd_command_prompt_cdata {
|
2009-06-01 22:58:49 +00:00
|
|
|
struct client *c;
|
2009-08-19 10:39:50 +00:00
|
|
|
char *next_prompt;
|
|
|
|
char *prompts;
|
2009-06-01 22:58:49 +00:00
|
|
|
char *template;
|
2009-08-19 10:39:50 +00:00
|
|
|
int idx;
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2011-01-04 00:42:46 +00:00
|
|
|
cmd_command_prompt_key_binding(struct cmd *self, int key)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
|
|
|
switch (key) {
|
|
|
|
case ',':
|
2011-01-04 00:42:46 +00:00
|
|
|
self->args = args_create(1, "rename-window '%%'");
|
2009-06-01 22:58:49 +00:00
|
|
|
break;
|
|
|
|
case '.':
|
2011-01-04 00:42:46 +00:00
|
|
|
self->args = args_create(1, "move-window -t '%%'");
|
2009-06-01 22:58:49 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
2011-01-04 00:42:46 +00:00
|
|
|
self->args = args_create(1, "find-window '%%'");
|
2009-06-01 22:58:49 +00:00
|
|
|
break;
|
2010-05-05 23:24:23 +00:00
|
|
|
case '\'':
|
2011-01-04 00:42:46 +00:00
|
|
|
self->args = args_create(1, "select-window -t ':%%'");
|
|
|
|
args_set(self->args, 'p', "index");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
self->args = args_create(0);
|
2010-05-05 23:24:23 +00:00
|
|
|
break;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
|
|
|
|
{
|
2011-01-04 00:42:46 +00:00
|
|
|
struct args *args = self->args;
|
|
|
|
const char *prompts;
|
2009-08-19 10:39:50 +00:00
|
|
|
struct cmd_command_prompt_cdata *cdata;
|
2009-06-01 22:58:49 +00:00
|
|
|
struct client *c;
|
2009-08-19 10:39:50 +00:00
|
|
|
char *prompt, *ptr;
|
|
|
|
size_t n;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
|
2009-06-01 22:58:49 +00:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (c->prompt_string != NULL)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
cdata = xmalloc(sizeof *cdata);
|
|
|
|
cdata->c = c;
|
2009-08-19 10:39:50 +00:00
|
|
|
cdata->idx = 1;
|
|
|
|
cdata->next_prompt = NULL;
|
|
|
|
cdata->prompts = NULL;
|
|
|
|
cdata->template = NULL;
|
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
if (args->argc != 0)
|
|
|
|
cdata->template = xstrdup(args->argv[0]);
|
2009-08-19 10:39:50 +00:00
|
|
|
else
|
|
|
|
cdata->template = xstrdup("%1");
|
2011-01-04 00:42:46 +00:00
|
|
|
|
|
|
|
prompts = args_get(args, 'p');
|
|
|
|
if (prompts != NULL)
|
|
|
|
cdata->prompts = xstrdup(prompts);
|
|
|
|
else if (args->argc != 0) {
|
|
|
|
n = strcspn(cdata->template, " ,");
|
|
|
|
xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template);
|
2009-08-19 10:39:50 +00:00
|
|
|
} else
|
|
|
|
cdata->prompts = xstrdup(":");
|
|
|
|
|
|
|
|
cdata->next_prompt = cdata->prompts;
|
|
|
|
ptr = strsep(&cdata->next_prompt, ",");
|
2011-01-04 00:42:46 +00:00
|
|
|
if (prompts == NULL)
|
2009-08-19 10:39:50 +00:00
|
|
|
prompt = xstrdup(ptr);
|
|
|
|
else
|
|
|
|
xasprintf(&prompt, "%s ", ptr);
|
|
|
|
status_prompt_set(c, prompt, cmd_command_prompt_callback,
|
2011-01-04 00:42:46 +00:00
|
|
|
cmd_command_prompt_free, cdata, 0);
|
2009-08-19 10:39:50 +00:00
|
|
|
xfree(prompt);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cmd_command_prompt_callback(void *data, const char *s)
|
|
|
|
{
|
2009-08-19 10:39:50 +00:00
|
|
|
struct cmd_command_prompt_cdata *cdata = data;
|
2009-06-01 22:58:49 +00:00
|
|
|
struct client *c = cdata->c;
|
|
|
|
struct cmd_list *cmdlist;
|
2009-08-19 10:39:50 +00:00
|
|
|
struct cmd_ctx ctx;
|
|
|
|
char *cause, *newtempl, *prompt, *ptr;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-08-19 10:39:50 +00:00
|
|
|
if (s == NULL)
|
2009-06-01 22:58:49 +00:00
|
|
|
return (0);
|
|
|
|
|
2009-08-25 12:18:51 +00:00
|
|
|
newtempl = cmd_template_replace(cdata->template, s, cdata->idx);
|
2009-08-19 10:39:50 +00:00
|
|
|
xfree(cdata->template);
|
|
|
|
cdata->template = newtempl;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-08-19 10:39:50 +00:00
|
|
|
if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
|
|
|
|
xasprintf(&prompt, "%s ", ptr);
|
|
|
|
status_prompt_update(c, prompt);
|
|
|
|
xfree(prompt);
|
|
|
|
cdata->idx++;
|
|
|
|
return (1);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-08-19 10:39:50 +00:00
|
|
|
if (cmd_string_parse(newtempl, &cmdlist, &cause) != 0) {
|
|
|
|
if (cause != NULL) {
|
|
|
|
*cause = toupper((u_char) *cause);
|
|
|
|
status_message_set(c, "%s", cause);
|
|
|
|
xfree(cause);
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
return (0);
|
2009-08-19 10:39:50 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
ctx.msgdata = NULL;
|
|
|
|
ctx.curclient = c;
|
|
|
|
|
|
|
|
ctx.error = key_bindings_error;
|
|
|
|
ctx.print = key_bindings_print;
|
|
|
|
ctx.info = key_bindings_info;
|
|
|
|
|
|
|
|
ctx.cmdclient = NULL;
|
|
|
|
|
|
|
|
cmd_list_exec(cmdlist, &ctx);
|
|
|
|
cmd_list_free(cmdlist);
|
|
|
|
|
2009-07-17 06:13:27 +00:00
|
|
|
if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback)
|
2009-06-01 22:58:49 +00:00
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|
2009-07-17 06:13:27 +00:00
|
|
|
|
|
|
|
void
|
2011-01-04 00:42:46 +00:00
|
|
|
cmd_command_prompt_free(void *data)
|
2009-07-17 06:13:27 +00:00
|
|
|
{
|
2009-08-19 10:39:50 +00:00
|
|
|
struct cmd_command_prompt_cdata *cdata = data;
|
2009-07-17 06:13:27 +00:00
|
|
|
|
2009-08-19 10:39:50 +00:00
|
|
|
if (cdata->prompts != NULL)
|
|
|
|
xfree(cdata->prompts);
|
2009-07-17 06:13:27 +00:00
|
|
|
if (cdata->template != NULL)
|
|
|
|
xfree(cdata->template);
|
|
|
|
xfree(cdata);
|
|
|
|
}
|