Support -c for new-session, based on code from J Raynor.

pull/1/head
Nicholas Marriott 2013-10-01 23:48:03 +01:00
parent d62121e7bb
commit 9389cfbec9
7 changed files with 66 additions and 47 deletions

View File

@ -34,9 +34,10 @@ enum cmd_retval cmd_new_session_exec(struct cmd *, struct cmd_q *);
const struct cmd_entry cmd_new_session_entry = { const struct cmd_entry cmd_new_session_entry = {
"new-session", "new", "new-session", "new",
"AdDF:n:Ps:t:x:y:", 0, 1, "Ac:dDF:n:Ps:t:x:y:", 0, 1,
"[-AdDP] [-F format] [-n window-name] [-s session-name] " "[-AdDP] [-c start-directory] [-F format] [-n window-name] "
CMD_TARGET_SESSION_USAGE " [-x width] [-y height] [command]", "[-s session-name] " CMD_TARGET_SESSION_USAGE " [-x width] [-y height] "
"[command]",
CMD_STARTSERVER|CMD_CANTNEST|CMD_SENDENVIRON, CMD_STARTSERVER|CMD_CANTNEST|CMD_SENDENVIRON,
NULL, NULL,
cmd_new_session_exec cmd_new_session_exec
@ -52,8 +53,8 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
struct environ env; struct environ env;
struct termios tio, *tiop; struct termios tio, *tiop;
struct passwd *pw; struct passwd *pw;
const char *newname, *target, *update, *cwd, *errstr; const char *newname, *target, *update, *base, *cwd;
const char *template; const char *errstr, *template;
char *cmd, *cause, *cp; char *cmd, *cause, *cp;
int detached, idx; int detached, idx;
u_int sx, sy; u_int sx, sy;
@ -126,14 +127,19 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
/* Get the new session working directory. */ /* Get the new session working directory. */
if (c != NULL && c->cwd != NULL) if (c != NULL && c->cwd != NULL)
cwd = c->cwd; base = c->cwd;
else { else {
pw = getpwuid(getuid()); pw = getpwuid(getuid());
if (pw->pw_dir != NULL && *pw->pw_dir != '\0') if (pw->pw_dir != NULL && *pw->pw_dir != '\0')
cwd = pw->pw_dir; base = pw->pw_dir;
else else
cwd = "/"; base = "/";
} }
if (args_has(args, 'c'))
cwd = args_get(args, 'c');
else
cwd = options_get_string(&global_s_options, "default-path");
cwd = cmd_default_path(base, base, cwd);
/* Find new session size. */ /* Find new session size. */
if (c != NULL) { if (c != NULL) {

View File

@ -102,7 +102,7 @@ cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq)
cmd = options_get_string(&s->options, "default-command"); cmd = options_get_string(&s->options, "default-command");
else else
cmd = args->argv[0]; cmd = args->argv[0];
cwd = cmd_get_default_path(cmdq, args_get(args, 'c')); cwd = cmdq_default_path(cmdq, args_get(args, 'c'));
if (idx == -1) if (idx == -1)
idx = -1 - options_get_number(&s->options, "base-index"); idx = -1 - options_get_number(&s->options, "base-index");

View File

@ -283,3 +283,27 @@ cmdq_flush(struct cmd_q *cmdq)
} }
cmdq->item = NULL; cmdq->item = NULL;
} }
/* Get default path using command queue. */
const char *
cmdq_default_path(struct cmd_q *cmdq, const char *cwd)
{
struct client *c = cmdq->client;
struct session *s;
const char *current;
if ((s = cmd_current_session(cmdq, 0)) == NULL)
return (NULL);
if (cwd == NULL)
cwd = options_get_string(&s->options, "default-path");
if (c != NULL && c->session == NULL && c->cwd != NULL)
current = c->cwd;
else if (s->curw != NULL)
current = osdep_get_cwd(s->curw->window->active->fd);
else
current = NULL;
return (cmd_default_path(s->cwd, current, cwd));
}

View File

@ -82,7 +82,7 @@ cmd_split_window_exec(struct cmd *self, struct cmd_q *cmdq)
cmd = options_get_string(&s->options, "default-command"); cmd = options_get_string(&s->options, "default-command");
else else
cmd = args->argv[0]; cmd = args->argv[0];
cwd = cmd_get_default_path(cmdq, args_get(args, 'c')); cwd = cmdq_default_path(cmdq, args_get(args, 'c'));
type = LAYOUT_TOPBOTTOM; type = LAYOUT_TOPBOTTOM;
if (args_has(args, 'h')) if (args_has(args, 'h'))

59
cmd.c
View File

@ -1278,68 +1278,55 @@ cmd_template_replace(const char *template, const char *s, int idx)
} }
/* /*
* Return the default path for a new pane, using the given path or the * Return the default path for a new pane. Several special values are accepted:
* default-path option if it is NULL. Several special values are accepted: the * the empty string or relative path for the current working directory,
* empty string or relative path for the current pane's working directory, ~ * ~ for the user's home, - for the base working directory, . for the server
* for the user's home, - for the session working directory, . for the tmux * working directory.
* server's working directory. The default on failure is the session's working
* directory.
*/ */
const char * const char *
cmd_get_default_path(struct cmd_q *cmdq, const char *cwd) cmd_default_path(const char *base, const char *current, const char *in)
{ {
struct client *c = cmdq->client;
struct session *s;
struct environ_entry *envent;
const char *root; const char *root;
struct environ_entry *envent;
char tmp[MAXPATHLEN]; char tmp[MAXPATHLEN];
struct passwd *pw; struct passwd *pw;
int n; int n;
size_t skip; size_t skip;
static char path[MAXPATHLEN]; static char path[MAXPATHLEN];
if ((s = cmd_current_session(cmdq, 0)) == NULL)
return (NULL);
if (cwd == NULL)
cwd = options_get_string(&s->options, "default-path");
skip = 1; skip = 1;
if (strcmp(cwd, "$HOME") == 0 || strncmp(cwd, "$HOME/", 6) == 0) { if (strcmp(in, "$HOME") == 0 || strncmp(in, "$HOME/", 6) == 0) {
/* User's home directory - $HOME. */ /* User's home directory - $HOME. */
skip = 5; skip = 5;
goto find_home; goto find_home;
} else if (cwd[0] == '~' && (cwd[1] == '\0' || cwd[1] == '/')) { } else if (in[0] == '~' && (in[1] == '\0' || in[1] == '/')) {
/* User's home directory - ~. */ /* User's home directory - ~. */
goto find_home; goto find_home;
} else if (cwd[0] == '-' && (cwd[1] == '\0' || cwd[1] == '/')) { } else if (in[0] == '-' && (in[1] == '\0' || in[1] == '/')) {
/* Session working directory. */ /* Base working directory. */
root = s->cwd; root = base;
goto complete_path; goto complete_path;
} else if (cwd[0] == '.' && (cwd[1] == '\0' || cwd[1] == '/')) { } else if (in[0] == '.' && (in[1] == '\0' || in[1] == '/')) {
/* Server working directory. */ /* Server working directory. */
if (getcwd(tmp, sizeof tmp) != NULL) { if (getcwd(tmp, sizeof tmp) != NULL) {
root = tmp; root = tmp;
goto complete_path; goto complete_path;
} }
return (s->cwd); return ("/");
} else if (*cwd == '/') { } else if (*in == '/') {
/* Absolute path. */ /* Absolute path. */
return (cwd); return (in);
} else { } else {
/* Empty or relative path. */ /* Empty or relative path. */
if (c != NULL && c->session == NULL && c->cwd != NULL) if (current != NULL)
root = c->cwd; root = current;
else if (s->curw != NULL)
root = osdep_get_cwd(s->curw->window->active->fd);
else else
return (s->cwd); return (base);
skip = 0; skip = 0;
if (root != NULL) goto complete_path;
goto complete_path;
} }
return (s->cwd); return (base);
find_home: find_home:
envent = environ_find(&global_environ, "HOME"); envent = environ_find(&global_environ, "HOME");
@ -1348,15 +1335,15 @@ find_home:
else if ((pw = getpwuid(getuid())) != NULL) else if ((pw = getpwuid(getuid())) != NULL)
root = pw->pw_dir; root = pw->pw_dir;
else else
return (s->cwd); return (base);
complete_path: complete_path:
if (root[skip] == '\0') { if (root[skip] == '\0') {
strlcpy(path, root, sizeof path); strlcpy(path, root, sizeof path);
return (path); return (path);
} }
n = snprintf(path, sizeof path, "%s/%s", root, cwd + skip); n = snprintf(path, sizeof path, "%s/%s", root, in + skip);
if (n > 0 && (size_t)n < sizeof path) if (n > 0 && (size_t)n < sizeof path)
return (path); return (path);
return (s->cwd); return (base);
} }

1
tmux.1
View File

@ -676,6 +676,7 @@ Lock all clients attached to
.Ar target-session . .Ar target-session .
.It Xo Ic new-session .It Xo Ic new-session
.Op Fl AdDP .Op Fl AdDP
.Op Fl c Ar start-directory
.Op Fl F Ar format .Op Fl F Ar format
.Op Fl n Ar window-name .Op Fl n Ar window-name
.Op Fl s Ar session-name .Op Fl s Ar session-name

3
tmux.h
View File

@ -1767,7 +1767,7 @@ int cmd_find_index(struct cmd_q *, const char *,
struct winlink *cmd_find_pane(struct cmd_q *, const char *, struct session **, struct winlink *cmd_find_pane(struct cmd_q *, const char *, struct session **,
struct window_pane **); struct window_pane **);
char *cmd_template_replace(const char *, const char *, int); char *cmd_template_replace(const char *, const char *, int);
const char *cmd_get_default_path(struct cmd_q *, const char *); const char *cmd_default_path(const char *, const char *, const char *);
extern const struct cmd_entry *cmd_table[]; extern const struct cmd_entry *cmd_table[];
extern const struct cmd_entry cmd_attach_session_entry; extern const struct cmd_entry cmd_attach_session_entry;
extern const struct cmd_entry cmd_bind_key_entry; extern const struct cmd_entry cmd_bind_key_entry;
@ -1876,6 +1876,7 @@ void cmdq_run(struct cmd_q *, struct cmd_list *);
void cmdq_append(struct cmd_q *, struct cmd_list *); void cmdq_append(struct cmd_q *, struct cmd_list *);
int cmdq_continue(struct cmd_q *); int cmdq_continue(struct cmd_q *);
void cmdq_flush(struct cmd_q *); void cmdq_flush(struct cmd_q *);
const char *cmdq_default_path(struct cmd_q *, const char *);
/* cmd-string.c */ /* cmd-string.c */
int cmd_string_parse(const char *, struct cmd_list **, const char *, int cmd_string_parse(const char *, struct cmd_list **, const char *,