Annotation of src/usr.bin/tmux/cmd-command-prompt.c, Revision 1.15
1.15 ! nicm 1: /* $OpenBSD: cmd-command-prompt.c,v 1.14 2011/01/04 00:42:46 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
5: *
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.
9: *
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.
17: */
18:
19: #include <sys/types.h>
20:
21: #include <ctype.h>
22: #include <string.h>
23:
24: #include "tmux.h"
25:
26: /*
27: * Prompt for command in client.
28: */
29:
1.14 nicm 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 *);
1.8 nicm 33:
1.14 nicm 34: int cmd_command_prompt_callback(void *, const char *);
35: void cmd_command_prompt_free(void *);
1.1 nicm 36:
37: const struct cmd_entry cmd_command_prompt_entry = {
38: "command-prompt", NULL,
1.15 ! nicm 39: "p:t:", 0, 1,
1.8 nicm 40: CMD_TARGET_CLIENT_USAGE " [-p prompts] [template]",
1.14 nicm 41: 0,
42: cmd_command_prompt_key_binding,
43: NULL,
44: cmd_command_prompt_exec
1.8 nicm 45: };
46:
47: struct cmd_command_prompt_cdata {
1.1 nicm 48: struct client *c;
1.8 nicm 49: char *next_prompt;
50: char *prompts;
1.1 nicm 51: char *template;
1.8 nicm 52: int idx;
1.1 nicm 53: };
54:
55: void
1.14 nicm 56: cmd_command_prompt_key_binding(struct cmd *self, int key)
1.1 nicm 57: {
58: switch (key) {
59: case ',':
1.14 nicm 60: self->args = args_create(1, "rename-window '%%'");
1.1 nicm 61: break;
62: case '.':
1.14 nicm 63: self->args = args_create(1, "move-window -t '%%'");
1.1 nicm 64: break;
65: case 'f':
1.14 nicm 66: self->args = args_create(1, "find-window '%%'");
1.13 nicm 67: break;
68: case '\'':
1.14 nicm 69: self->args = args_create(1, "select-window -t ':%%'");
70: args_set(self->args, 'p', "index");
71: break;
72: default:
73: self->args = args_create(0);
1.1 nicm 74: break;
75: }
76: }
77:
78: int
79: cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
80: {
1.14 nicm 81: struct args *args = self->args;
82: const char *prompts;
1.8 nicm 83: struct cmd_command_prompt_cdata *cdata;
1.1 nicm 84: struct client *c;
1.8 nicm 85: char *prompt, *ptr;
86: size_t n;
1.1 nicm 87:
1.14 nicm 88: if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
1.1 nicm 89: return (-1);
90:
91: if (c->prompt_string != NULL)
92: return (0);
93:
94: cdata = xmalloc(sizeof *cdata);
95: cdata->c = c;
1.8 nicm 96: cdata->idx = 1;
97: cdata->next_prompt = NULL;
98: cdata->prompts = NULL;
99: cdata->template = NULL;
100:
1.14 nicm 101: if (args->argc != 0)
102: cdata->template = xstrdup(args->argv[0]);
1.8 nicm 103: else
104: cdata->template = xstrdup("%1");
1.14 nicm 105:
106: prompts = args_get(args, 'p');
107: if (prompts != NULL)
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);
1.8 nicm 112: } else
113: cdata->prompts = xstrdup(":");
114:
115: cdata->next_prompt = cdata->prompts;
116: ptr = strsep(&cdata->next_prompt, ",");
1.14 nicm 117: if (prompts == NULL)
1.8 nicm 118: prompt = xstrdup(ptr);
119: else
120: xasprintf(&prompt, "%s ", ptr);
121: status_prompt_set(c, prompt, cmd_command_prompt_callback,
1.14 nicm 122: cmd_command_prompt_free, cdata, 0);
1.8 nicm 123: xfree(prompt);
1.1 nicm 124:
125: return (0);
126: }
127:
128: int
129: cmd_command_prompt_callback(void *data, const char *s)
130: {
1.8 nicm 131: struct cmd_command_prompt_cdata *cdata = data;
1.1 nicm 132: struct client *c = cdata->c;
133: struct cmd_list *cmdlist;
1.8 nicm 134: struct cmd_ctx ctx;
135: char *cause, *newtempl, *prompt, *ptr;
1.1 nicm 136:
1.8 nicm 137: if (s == NULL)
1.1 nicm 138: return (0);
139:
1.10 nicm 140: newtempl = cmd_template_replace(cdata->template, s, cdata->idx);
1.8 nicm 141: xfree(cdata->template);
142: cdata->template = newtempl;
143:
144: if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
145: xasprintf(&prompt, "%s ", ptr);
146: status_prompt_update(c, prompt);
147: xfree(prompt);
148: cdata->idx++;
149: return (1);
150: }
1.1 nicm 151:
1.8 nicm 152: if (cmd_string_parse(newtempl, &cmdlist, &cause) != 0) {
153: if (cause != NULL) {
154: *cause = toupper((u_char) *cause);
155: status_message_set(c, "%s", cause);
156: xfree(cause);
1.1 nicm 157: }
1.8 nicm 158: return (0);
1.1 nicm 159: }
160:
161: ctx.msgdata = NULL;
162: ctx.curclient = c;
163:
164: ctx.error = key_bindings_error;
165: ctx.print = key_bindings_print;
166: ctx.info = key_bindings_info;
167:
168: ctx.cmdclient = NULL;
169:
170: cmd_list_exec(cmdlist, &ctx);
171: cmd_list_free(cmdlist);
172:
1.4 nicm 173: if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback)
1.1 nicm 174: return (1);
175: return (0);
1.4 nicm 176: }
177:
178: void
1.14 nicm 179: cmd_command_prompt_free(void *data)
1.4 nicm 180: {
1.8 nicm 181: struct cmd_command_prompt_cdata *cdata = data;
1.4 nicm 182:
1.8 nicm 183: if (cdata->prompts != NULL)
184: xfree(cdata->prompts);
1.4 nicm 185: if (cdata->template != NULL)
186: xfree(cdata->template);
187: xfree(cdata);
1.1 nicm 188: }