Annotation of src/usr.bin/tmux/cmd-command-prompt.c, Revision 1.16
1.16 ! nicm 1: /* $OpenBSD: cmd-command-prompt.c,v 1.15 2011/01/05 22:38:28 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.16 ! nicm 85: char *prompt, *prompt_replaced, *ptr;
1.8 nicm 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);
1.16 ! nicm 119: else {
! 120: prompt_replaced = status_replace(c, NULL, NULL, NULL, ptr,
! 121: time(NULL), 0);
! 122: xasprintf(&prompt, "%s ", prompt_replaced);
! 123: xfree(prompt_replaced);
! 124: }
1.8 nicm 125: status_prompt_set(c, prompt, cmd_command_prompt_callback,
1.14 nicm 126: cmd_command_prompt_free, cdata, 0);
1.8 nicm 127: xfree(prompt);
1.1 nicm 128:
129: return (0);
130: }
131:
132: int
133: cmd_command_prompt_callback(void *data, const char *s)
134: {
1.8 nicm 135: struct cmd_command_prompt_cdata *cdata = data;
1.1 nicm 136: struct client *c = cdata->c;
137: struct cmd_list *cmdlist;
1.8 nicm 138: struct cmd_ctx ctx;
139: char *cause, *newtempl, *prompt, *ptr;
1.16 ! nicm 140: char *prompt_replaced;
1.1 nicm 141:
1.8 nicm 142: if (s == NULL)
1.1 nicm 143: return (0);
144:
1.10 nicm 145: newtempl = cmd_template_replace(cdata->template, s, cdata->idx);
1.8 nicm 146: xfree(cdata->template);
147: cdata->template = newtempl;
148:
149: if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
1.16 ! nicm 150: prompt_replaced = status_replace(c, NULL, NULL, NULL, ptr,
! 151: time(NULL), 0);
! 152: xasprintf(&prompt, "%s ", prompt_replaced);
1.8 nicm 153: status_prompt_update(c, prompt);
1.16 ! nicm 154:
! 155: xfree(prompt_replaced);
1.8 nicm 156: xfree(prompt);
157: cdata->idx++;
158: return (1);
159: }
1.1 nicm 160:
1.8 nicm 161: if (cmd_string_parse(newtempl, &cmdlist, &cause) != 0) {
162: if (cause != NULL) {
163: *cause = toupper((u_char) *cause);
164: status_message_set(c, "%s", cause);
165: xfree(cause);
1.1 nicm 166: }
1.8 nicm 167: return (0);
1.1 nicm 168: }
169:
170: ctx.msgdata = NULL;
171: ctx.curclient = c;
172:
173: ctx.error = key_bindings_error;
174: ctx.print = key_bindings_print;
175: ctx.info = key_bindings_info;
176:
177: ctx.cmdclient = NULL;
178:
179: cmd_list_exec(cmdlist, &ctx);
180: cmd_list_free(cmdlist);
181:
1.4 nicm 182: if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback)
1.1 nicm 183: return (1);
184: return (0);
1.4 nicm 185: }
186:
187: void
1.14 nicm 188: cmd_command_prompt_free(void *data)
1.4 nicm 189: {
1.8 nicm 190: struct cmd_command_prompt_cdata *cdata = data;
1.4 nicm 191:
1.8 nicm 192: if (cdata->prompts != NULL)
193: xfree(cdata->prompts);
1.4 nicm 194: if (cdata->template != NULL)
195: xfree(cdata->template);
196: xfree(cdata);
1.1 nicm 197: }