Expand formats in window and session names.

This commit is contained in:
nicm 2018-03-01 12:53:08 +00:00
parent 508e2f0b3a
commit 182357f24b
5 changed files with 70 additions and 50 deletions

View File

@ -73,14 +73,15 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
struct environ *env;
struct termios tio, *tiop;
struct session_group *sg;
const char *newname, *errstr, *template, *group, *prefix;
const char *path, *cmd, *cwd;
char **argv, *cause, *cp, *to_free = NULL;
const char *errstr, *template, *group, *prefix;
const char *path, *cmd, *tmp;
char **argv, *cause, *cp, *newname, *cwd = NULL;
int detached, already_attached, idx, argc;
int is_control = 0;
u_int sx, sy;
struct environ_entry *envent;
struct cmd_find_state fs;
enum cmd_retval retval;
if (self->entry == &cmd_has_session_entry) {
/*
@ -95,20 +96,24 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
return (CMD_RETURN_ERROR);
}
newname = args_get(args, 's');
if (newname != NULL) {
newname = NULL;
if (args_has(args, 's')) {
newname = format_single(item, args_get(args, 's'), c, NULL,
NULL, NULL);
if (!session_check_name(newname)) {
cmdq_error(item, "bad session name: %s", newname);
return (CMD_RETURN_ERROR);
goto error;
}
if ((as = session_find(newname)) != NULL) {
if (args_has(args, 'A')) {
return (cmd_attach_session(item,
retval = cmd_attach_session(item,
newname, args_has(args, 'D'),
0, NULL, args_has(args, 'E')));
0, NULL, args_has(args, 'E'));
free(newname);
return (retval);
}
cmdq_error(item, "duplicate session: %s", newname);
return (CMD_RETURN_ERROR);
goto error;
}
}
@ -149,14 +154,12 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
already_attached = 1;
/* Get the new session working directory. */
if (args_has(args, 'c')) {
cwd = args_get(args, 'c');
to_free = format_single(item, cwd, c, NULL, NULL, NULL);
cwd = to_free;
} else if (c != NULL && c->session == NULL && c->cwd != NULL)
cwd = c->cwd;
if ((tmp = args_get(args, 'c')) != NULL)
cwd = format_single(item, tmp, c, NULL, NULL, NULL);
else if (c != NULL && c->session == NULL && c->cwd != NULL)
cwd = xstrdup(c->cwd);
else
cwd = ".";
cwd = xstrdup(".");
/*
* If this is a new client, check for nesting and save the termios
@ -261,10 +264,12 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
}
/* Set the initial window name if one given. */
if (argc >= 0 && args_has(args, 'n')) {
if (argc >= 0 && (tmp = args_get(args, 'n')) != NULL) {
cp = format_single(item, tmp, c, s, NULL, NULL);
w = s->curw->window;
window_set_name(w, args_get(args, 'n'));
window_set_name(w, cp);
options_set_number(w->options, "automatic-rename", 0);
free(cp);
}
/*
@ -331,10 +336,12 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
cmd_find_from_session(&fs, s, 0);
hooks_insert(s->hooks, item, &fs, "after-new-session");
free(to_free);
free(cwd);
free(newname);
return (CMD_RETURN_NORMAL);
error:
free(to_free);
free(cwd);
free(newname);
return (CMD_RETURN_ERROR);
}

View File

@ -57,8 +57,8 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
struct winlink *wl = item->target.wl;
struct client *c = cmd_find_client(item, NULL, 1);
int idx = item->target.idx;
const char *cmd, *path, *template, *cwd;
char **argv, *cause, *cp, *to_free = NULL;
const char *cmd, *path, *template, *tmp;
char **argv, *cause, *cp, *cwd, *name;
int argc, detached;
struct environ_entry *envent;
struct cmd_find_state fs;
@ -93,14 +93,17 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
if (envent != NULL)
path = envent->value;
if (args_has(args, 'c')) {
cwd = args_get(args, 'c');
to_free = format_single(item, cwd, c, s, NULL, NULL);
cwd = to_free;
} else if (item->client != NULL && item->client->session == NULL)
cwd = item->client->cwd;
if ((tmp = args_get(args, 'c')) != NULL)
cwd = format_single(item, tmp, c, s, NULL, NULL);
else if (item->client != NULL && item->client->session == NULL)
cwd = xstrdup(item->client->cwd);
else
cwd = s->cwd;
cwd = xstrdup(s->cwd);
if ((tmp = args_get(args, 'n')) != NULL)
name = format_single(item, tmp, c, s, NULL, NULL);
else
name = NULL;
wl = NULL;
if (idx != -1)
@ -124,7 +127,7 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
if (idx == -1)
idx = -1 - options_get_number(s->options, "base-index");
wl = session_new(s, args_get(args, 'n'), argc, argv, path, cwd, idx,
wl = session_new(s, name, argc, argv, path, cwd, idx,
&cause);
if (wl == NULL) {
cmdq_error(item, "create window failed: %s", cause);
@ -149,10 +152,12 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
cmd_find_from_winlink(&fs, wl, 0);
hooks_insert(s->hooks, item, &fs, "after-new-window");
free(to_free);
free(name);
free(cwd);
return (CMD_RETURN_NORMAL);
error:
free(to_free);
free(name);
free(cwd);
return (CMD_RETURN_ERROR);
}

View File

@ -46,26 +46,31 @@ const struct cmd_entry cmd_rename_session_entry = {
static enum cmd_retval
cmd_rename_session_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;
struct session *s = item->target.s;
const char *newname;
struct args *args = self->args;
struct client *c = cmd_find_client(item, NULL, 0);
struct session *s = item->target.s;
char *newname;
newname = args->argv[0];
if (strcmp(newname, s->name) == 0)
newname = format_single(item, args->argv[0], c, s, NULL, NULL);
if (strcmp(newname, s->name) == 0) {
free(newname);
return (CMD_RETURN_NORMAL);
}
if (!session_check_name(newname)) {
cmdq_error(item, "bad session name: %s", newname);
free(newname);
return (CMD_RETURN_ERROR);
}
if (session_find(newname) != NULL) {
cmdq_error(item, "duplicate session: %s", newname);
free(newname);
return (CMD_RETURN_ERROR);
}
RB_REMOVE(sessions, &sessions, s);
free(s->name);
s->name = xstrdup(newname);
s->name = newname;
RB_INSERT(sessions, &sessions, s);
server_status_session(s);

View File

@ -46,12 +46,17 @@ static enum cmd_retval
cmd_rename_window_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;
struct client *c = cmd_find_client(item, NULL, 0);
struct session *s = item->target.s;
struct winlink *wl = item->target.wl;
char *newname;
window_set_name(wl->window, args->argv[0]);
newname = format_single(item, args->argv[0], c, s, wl, NULL);
window_set_name(wl->window, newname);
options_set_number(wl->window->options, "automatic-rename", 0);
server_status_window(wl->window);
free(newname);
return (CMD_RETURN_NORMAL);
}

View File

@ -61,8 +61,8 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
struct window *w = wl->window;
struct window_pane *wp = item->target.wp, *new_wp = NULL;
struct environ *env;
const char *cmd, *path, *shell, *template, *cwd;
char **argv, *cause, *new_cause, *cp, *to_free = NULL;
const char *cmd, *path, *shell, *template, *tmp;
char **argv, *cause, *new_cause, *cp, *cwd;
u_int hlimit;
int argc, size, percentage;
enum layout_type type;
@ -86,14 +86,12 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
argv = args->argv;
}
if (args_has(args, 'c')) {
cwd = args_get(args, 'c');
to_free = format_single(item, cwd, c, s, NULL, NULL);
cwd = to_free;
} else if (item->client != NULL && item->client->session == NULL)
cwd = item->client->cwd;
if ((tmp = args_get(args, 'c')) != NULL)
cwd = format_single(item, tmp, c, s, NULL, NULL);
else if (item->client != NULL && item->client->session == NULL)
cwd = xstrdup(item->client->cwd);
else
cwd = s->cwd;
cwd = xstrdup(s->cwd);
type = LAYOUT_TOPBOTTOM;
if (args_has(args, 'h'))
@ -175,7 +173,7 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
cmd_find_from_winlink_pane(&fs, wl, new_wp, 0);
hooks_insert(s->hooks, item, &fs, "after-split-window");
free(to_free);
free(cwd);
return (CMD_RETURN_NORMAL);
error:
@ -186,6 +184,6 @@ error:
cmdq_error(item, "create pane failed: %s", cause);
free(cause);
free(to_free);
free(cwd);
return (CMD_RETURN_ERROR);
}