4 * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
27 * Prompt for command in client.
30 void cmd_command_prompt_key_binding(struct cmd
*, int);
31 int cmd_command_prompt_check(struct args
*);
32 int cmd_command_prompt_exec(struct cmd
*, struct cmd_ctx
*);
34 int cmd_command_prompt_callback(void *, const char *);
35 void cmd_command_prompt_free(void *);
37 const struct cmd_entry cmd_command_prompt_entry
= {
38 "command-prompt", NULL
,
40 CMD_TARGET_CLIENT_USAGE
" [-p prompts] [template]",
42 cmd_command_prompt_key_binding
,
44 cmd_command_prompt_exec
47 struct cmd_command_prompt_cdata
{
56 cmd_command_prompt_key_binding(struct cmd
*self
, int key
)
60 self
->args
= args_create(1, "rename-window '%%'");
63 self
->args
= args_create(1, "move-window -t '%%'");
66 self
->args
= args_create(1, "find-window '%%'");
69 self
->args
= args_create(1, "select-window -t ':%%'");
70 args_set(self
->args
, 'p', "index");
73 self
->args
= args_create(0);
79 cmd_command_prompt_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
81 struct args
*args
= self
->args
;
83 struct cmd_command_prompt_cdata
*cdata
;
88 if ((c
= cmd_find_client(ctx
, args_get(args
, 't'))) == NULL
)
91 if (c
->prompt_string
!= NULL
)
94 cdata
= xmalloc(sizeof *cdata
);
97 cdata
->next_prompt
= NULL
;
98 cdata
->prompts
= NULL
;
99 cdata
->template = NULL
;
102 cdata
->template = xstrdup(args
->argv
[0]);
104 cdata
->template = xstrdup("%1");
106 prompts
= args_get(args
, 'p');
108 cdata
->prompts
= xstrdup(prompts
);
109 else if (args
->argc
!= 0) {
110 n
= strcspn(cdata
->template, " ,");
111 xasprintf(&cdata
->prompts
, "(%.*s) ", (int) n
, cdata
->template);
113 cdata
->prompts
= xstrdup(":");
115 cdata
->next_prompt
= cdata
->prompts
;
116 ptr
= strsep(&cdata
->next_prompt
, ",");
118 prompt
= xstrdup(ptr
);
120 xasprintf(&prompt
, "%s ", ptr
);
121 status_prompt_set(c
, prompt
, cmd_command_prompt_callback
,
122 cmd_command_prompt_free
, cdata
, 0);
129 cmd_command_prompt_callback(void *data
, const char *s
)
131 struct cmd_command_prompt_cdata
*cdata
= data
;
132 struct client
*c
= cdata
->c
;
133 struct cmd_list
*cmdlist
;
135 char *cause
, *newtempl
, *prompt
, *ptr
;
140 newtempl
= cmd_template_replace(cdata
->template, s
, cdata
->idx
);
141 xfree(cdata
->template);
142 cdata
->template = newtempl
;
144 if ((ptr
= strsep(&cdata
->next_prompt
, ",")) != NULL
) {
145 xasprintf(&prompt
, "%s ", ptr
);
146 status_prompt_update(c
, prompt
);
152 if (cmd_string_parse(newtempl
, &cmdlist
, &cause
) != 0) {
154 *cause
= toupper((u_char
) *cause
);
155 status_message_set(c
, "%s", cause
);
164 ctx
.error
= key_bindings_error
;
165 ctx
.print
= key_bindings_print
;
166 ctx
.info
= key_bindings_info
;
168 ctx
.cmdclient
= NULL
;
170 cmd_list_exec(cmdlist
, &ctx
);
171 cmd_list_free(cmdlist
);
173 if (c
->prompt_callbackfn
!= (void *) &cmd_command_prompt_callback
)
179 cmd_command_prompt_free(void *data
)
181 struct cmd_command_prompt_cdata
*cdata
= data
;
183 if (cdata
->prompts
!= NULL
)
184 xfree(cdata
->prompts
);
185 if (cdata
->template != NULL
)
186 xfree(cdata
->template);