2011-01-07 16:04:51 +01:00
|
|
|
/* $Id: cmd-command-prompt.c,v 1.30 2011-01-07 15:04:51 tcunha Exp $ */
|
2008-06-19 22:45:21 +02:00
|
|
|
|
|
|
|
/*
|
2008-06-25 22:43:14 +02:00
|
|
|
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
|
2008-06-19 22:45:21 +02:00
|
|
|
*
|
|
|
|
* 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>
|
2009-01-13 02:08:40 +01:00
|
|
|
#include <string.h>
|
2008-06-19 22:45:21 +02:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prompt for command in client.
|
|
|
|
*/
|
|
|
|
|
2011-01-07 15:45:34 +01: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 *);
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2011-01-07 15:45:34 +01:00
|
|
|
int cmd_command_prompt_callback(void *, const char *);
|
|
|
|
void cmd_command_prompt_free(void *);
|
2008-06-19 22:45:21 +02:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_command_prompt_entry = {
|
|
|
|
"command-prompt", NULL,
|
2011-01-07 16:04:51 +01:00
|
|
|
"p:t:", 0, 1,
|
2009-08-20 13:51:20 +02:00
|
|
|
CMD_TARGET_CLIENT_USAGE " [-p prompts] [template]",
|
2011-01-07 15:45:34 +01:00
|
|
|
0,
|
|
|
|
cmd_command_prompt_key_binding,
|
|
|
|
NULL,
|
|
|
|
cmd_command_prompt_exec
|
2009-08-20 13:51:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct cmd_command_prompt_cdata {
|
2009-01-13 02:08:40 +01:00
|
|
|
struct client *c;
|
2009-08-20 13:51:20 +02:00
|
|
|
char *next_prompt;
|
|
|
|
char *prompts;
|
2009-01-13 02:08:40 +01:00
|
|
|
char *template;
|
2009-08-20 13:51:20 +02:00
|
|
|
int idx;
|
2009-01-13 02:08:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2011-01-07 15:45:34 +01:00
|
|
|
cmd_command_prompt_key_binding(struct cmd *self, int key)
|
2009-01-13 02:08:40 +01:00
|
|
|
{
|
|
|
|
switch (key) {
|
|
|
|
case ',':
|
2011-01-07 15:45:34 +01:00
|
|
|
self->args = args_create(1, "rename-window '%%'");
|
2009-01-13 02:08:40 +01:00
|
|
|
break;
|
2009-01-30 22:10:10 +01:00
|
|
|
case '.':
|
2011-01-07 15:45:34 +01:00
|
|
|
self->args = args_create(1, "move-window -t '%%'");
|
2009-01-30 22:10:10 +01:00
|
|
|
break;
|
2009-01-18 18:20:52 +01:00
|
|
|
case 'f':
|
2011-01-07 15:45:34 +01:00
|
|
|
self->args = args_create(1, "find-window '%%'");
|
2009-01-18 18:20:52 +01:00
|
|
|
break;
|
2010-05-14 16:33:39 +02:00
|
|
|
case '\'':
|
2011-01-07 15:45:34 +01:00
|
|
|
self->args = args_create(1, "select-window -t ':%%'");
|
|
|
|
args_set(self->args, 'p', "index");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
self->args = args_create(0);
|
2010-05-14 16:33:39 +02:00
|
|
|
break;
|
2009-01-13 02:08:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-19 19:23:40 +01:00
|
|
|
int
|
2008-06-19 22:45:21 +02:00
|
|
|
cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
|
|
|
|
{
|
2011-01-07 15:45:34 +01:00
|
|
|
struct args *args = self->args;
|
|
|
|
const char *prompts;
|
2009-08-20 13:51:20 +02:00
|
|
|
struct cmd_command_prompt_cdata *cdata;
|
2009-01-13 02:08:40 +01:00
|
|
|
struct client *c;
|
2009-08-20 13:51:20 +02:00
|
|
|
char *prompt, *ptr;
|
|
|
|
size_t n;
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2011-01-07 15:45:34 +01:00
|
|
|
if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
|
2009-01-19 19:23:40 +01:00
|
|
|
return (-1);
|
2008-06-19 22:45:21 +02:00
|
|
|
|
|
|
|
if (c->prompt_string != NULL)
|
2009-01-19 19:23:40 +01:00
|
|
|
return (0);
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2009-01-13 02:08:40 +01:00
|
|
|
cdata = xmalloc(sizeof *cdata);
|
|
|
|
cdata->c = c;
|
2009-08-20 13:51:20 +02:00
|
|
|
cdata->idx = 1;
|
|
|
|
cdata->next_prompt = NULL;
|
|
|
|
cdata->prompts = NULL;
|
|
|
|
cdata->template = NULL;
|
|
|
|
|
2011-01-07 15:45:34 +01:00
|
|
|
if (args->argc != 0)
|
|
|
|
cdata->template = xstrdup(args->argv[0]);
|
2009-08-20 13:51:20 +02:00
|
|
|
else
|
|
|
|
cdata->template = xstrdup("%1");
|
2011-01-07 15:45:34 +01: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-20 13:51:20 +02:00
|
|
|
} else
|
|
|
|
cdata->prompts = xstrdup(":");
|
|
|
|
|
|
|
|
cdata->next_prompt = cdata->prompts;
|
|
|
|
ptr = strsep(&cdata->next_prompt, ",");
|
2011-01-07 15:45:34 +01:00
|
|
|
if (prompts == NULL)
|
2009-08-20 13:51:20 +02:00
|
|
|
prompt = xstrdup(ptr);
|
|
|
|
else
|
|
|
|
xasprintf(&prompt, "%s ", ptr);
|
|
|
|
status_prompt_set(c, prompt, cmd_command_prompt_callback,
|
2011-01-07 15:45:34 +01:00
|
|
|
cmd_command_prompt_free, cdata, 0);
|
2009-08-20 13:51:20 +02:00
|
|
|
xfree(prompt);
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2009-01-19 19:23:40 +01:00
|
|
|
return (0);
|
2008-06-19 22:45:21 +02:00
|
|
|
}
|
|
|
|
|
2009-01-11 01:48:42 +01:00
|
|
|
int
|
|
|
|
cmd_command_prompt_callback(void *data, const char *s)
|
2008-06-19 22:45:21 +02:00
|
|
|
{
|
2009-08-20 13:51:20 +02:00
|
|
|
struct cmd_command_prompt_cdata *cdata = data;
|
2009-01-13 02:08:40 +01:00
|
|
|
struct client *c = cdata->c;
|
2009-01-18 15:40:48 +01:00
|
|
|
struct cmd_list *cmdlist;
|
2009-08-20 13:51:20 +02:00
|
|
|
struct cmd_ctx ctx;
|
|
|
|
char *cause, *newtempl, *prompt, *ptr;
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2009-08-20 13:51:20 +02:00
|
|
|
if (s == NULL)
|
2009-01-11 01:48:42 +01:00
|
|
|
return (0);
|
2009-01-13 02:08:40 +01:00
|
|
|
|
2009-08-25 15:53:39 +02:00
|
|
|
newtempl = cmd_template_replace(cdata->template, s, cdata->idx);
|
2009-08-20 13:51:20 +02:00
|
|
|
xfree(cdata->template);
|
|
|
|
cdata->template = newtempl;
|
2009-01-13 02:08:40 +01:00
|
|
|
|
2009-08-20 13:51:20 +02:00
|
|
|
if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
|
|
|
|
xasprintf(&prompt, "%s ", ptr);
|
|
|
|
status_prompt_update(c, prompt);
|
|
|
|
xfree(prompt);
|
|
|
|
cdata->idx++;
|
|
|
|
return (1);
|
2009-01-13 02:08:40 +01:00
|
|
|
}
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2009-08-20 13:51:20 +02: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-01-11 01:48:42 +01:00
|
|
|
return (0);
|
2009-08-20 13:51:20 +02:00
|
|
|
}
|
2008-09-26 08:45:28 +02:00
|
|
|
|
2008-06-19 22:45:21 +02: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;
|
|
|
|
|
2009-01-18 15:40:48 +01:00
|
|
|
cmd_list_exec(cmdlist, &ctx);
|
2009-02-16 19:58:14 +01:00
|
|
|
cmd_list_free(cmdlist);
|
2009-01-11 01:48:42 +01:00
|
|
|
|
2009-07-17 11:26:21 +02:00
|
|
|
if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback)
|
2009-01-11 01:48:42 +01:00
|
|
|
return (1);
|
|
|
|
return (0);
|
2008-06-19 22:45:21 +02:00
|
|
|
}
|
2009-07-17 11:26:21 +02:00
|
|
|
|
|
|
|
void
|
2011-01-07 15:45:34 +01:00
|
|
|
cmd_command_prompt_free(void *data)
|
2009-07-17 11:26:21 +02:00
|
|
|
{
|
2009-08-20 13:51:20 +02:00
|
|
|
struct cmd_command_prompt_cdata *cdata = data;
|
2009-07-17 11:26:21 +02:00
|
|
|
|
2009-08-20 13:51:20 +02:00
|
|
|
if (cdata->prompts != NULL)
|
|
|
|
xfree(cdata->prompts);
|
2009-07-17 11:26:21 +02:00
|
|
|
if (cdata->template != NULL)
|
|
|
|
xfree(cdata->template);
|
|
|
|
xfree(cdata);
|
|
|
|
}
|