Change the way the working directory for new processes is discovered. If

default-path isn't empty, it is used. Otherwise:

1) If tmux neww is run from the command line, the working directory of the
   client is used.

2) Otherwise sysctl KERN_PROC_CWD is used to retrieve the current
   working directory of the process in the active pane.

3) If that fails, the directory where the session was created is used.

Support code by Romain Francois, OpenBSD specific bits by me.

Note this requires a recent userland and kernel with KERN_PROC_CWD.
This commit is contained in:
Nicholas Marriott 2011-12-09 16:28:18 +00:00
parent e04d13f6a6
commit f308ba93aa
6 changed files with 47 additions and 19 deletions

View File

@ -98,13 +98,7 @@ cmd_new_window_exec(struct cmd *self, struct cmd_ctx *ctx)
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 = options_get_string(&s->options, "default-path"); cwd = cmd_get_default_path(ctx);
if (*cwd == '\0') {
if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
cwd = ctx->cmdclient->cwd;
else
cwd = s->cwd;
}
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

@ -78,13 +78,7 @@ cmd_split_window_exec(struct cmd *self, struct cmd_ctx *ctx)
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 = options_get_string(&s->options, "default-path"); cwd = cmd_get_default_path(ctx);
if (*cwd == '\0') {
if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
cwd = ctx->cmdclient->cwd;
else
cwd = s->cwd;
}
type = LAYOUT_TOPBOTTOM; type = LAYOUT_TOPBOTTOM;
if (args_has(args, 'h')) if (args_has(args, 'h'))

25
cmd.c
View File

@ -1213,3 +1213,28 @@ cmd_template_replace(char *template, const char *s, int idx)
return (buf); return (buf);
} }
/* Return the default path for a new pane. */
char *
cmd_get_default_path(struct cmd_ctx *ctx)
{
char *cwd;
struct session *s;
struct window_pane *wp;
if ((s = cmd_current_session(ctx, 0)) == NULL)
return (NULL);
cwd = options_get_string(&s->options, "default-path");
if (*cwd == '\0') {
if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
return (ctx->cmdclient->cwd);
if (ctx->curclient != NULL) {
wp = s->curw->window->active;
if ((cwd = get_proc_cwd(wp->pid)) != NULL)
return (cwd);
}
return (s->cwd);
}
return (cwd);
}

View File

@ -35,7 +35,8 @@
((p)->p_stat == SSTOP || (p)->p_stat == SZOMB || (p)->p_stat == SDEAD) ((p)->p_stat == SSTOP || (p)->p_stat == SZOMB || (p)->p_stat == SDEAD)
struct kinfo_proc *cmp_procs(struct kinfo_proc *, struct kinfo_proc *); struct kinfo_proc *cmp_procs(struct kinfo_proc *, struct kinfo_proc *);
char *get_proc_name(int, char *); char *get_proc_name(int, char *);
char *get_proc_cwd(pid_t);
struct kinfo_proc * struct kinfo_proc *
cmp_procs(struct kinfo_proc *p1, struct kinfo_proc *p2) cmp_procs(struct kinfo_proc *p1, struct kinfo_proc *p2)
@ -130,3 +131,15 @@ error:
free(buf); free(buf);
return (NULL); return (NULL);
} }
char*
get_proc_cwd(pid_t pid)
{
int name[] = { CTL_KERN, KERN_PROC_CWD, (int)pid };
static char path[MAXPATHLEN];
size_t pathlen = sizeof path;
if (sysctl(name, 3, path, &pathlen, NULL, 0) != 0)
return (NULL);
return (path);
}

8
tmux.1
View File

@ -1841,10 +1841,10 @@ to create a login shell using the value of the
.Ic default-shell .Ic default-shell
option. option.
.It Ic default-path Ar path .It Ic default-path Ar path
Set the default working directory for processes created from keys, or Set the default working directory for new panes.
interactively from the prompt. If empty (the default), the working directory is determined from the process
The default is empty, which means to use the working directory of the shell running in the active pane, from the command line environment or from the
from which the server was started if it is available or the user's home if not. working directory where the session was created.
.It Ic default-shell Ar path .It Ic default-shell Ar path
Specify the default shell. Specify the default shell.
This is used as the login shell for new windows when the This is used as the login shell for new windows when the

2
tmux.h
View File

@ -1562,6 +1562,7 @@ int cmd_find_index(
struct winlink *cmd_find_pane(struct cmd_ctx *, struct winlink *cmd_find_pane(struct cmd_ctx *,
const char *, struct session **, struct window_pane **); const char *, struct session **, struct window_pane **);
char *cmd_template_replace(char *, const char *, int); char *cmd_template_replace(char *, const char *, int);
char *cmd_get_default_path(struct cmd_ctx *ctx);
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;
@ -2079,6 +2080,7 @@ u_int utf8_split2(u_int, u_char *);
/* procname.c */ /* procname.c */
char *get_proc_name(int, char *); char *get_proc_name(int, char *);
char *get_proc_cwd(pid_t);
/* log.c */ /* log.c */
void log_open_tty(int); void log_open_tty(int);