I'm genuinely not delighted about more OS-dependent code. Is there no
portable solution to this? Even if it isn't quite as good.

Otherwise, what's the use case here? I love that you sent code but how
inconvenient is it to have more OS-dependent code verses the actual
issue? And how inconvenient the solution in the FAQ is?

If this is to be supported it is going to need to work on at least *BSD
without procfs, which is a more unportable code...


On Wed, Nov 30, 2011 at 09:48:31PM +0100, Romain Francoise wrote:
> Hi,
> 
> This patch adds a new session option 'inherit-default-path' and the
> associated machinery to create new windows/panes in the same working
> directory as the active window instead of using the value of default-path.
> It affects only windows created from keys or the command prompt.
> 
> Note that only Linux is supported for now, Solaris and FreeBSD (with
> linprocfs) may be added later. Not sure if the others have the required
> interface.
> 
> Comments welcome,
> 
> diff --git a/cmd-new-window.c b/cmd-new-window.c
> index 065e2b1..30ddd64 100644
> --- a/cmd-new-window.c
> +++ b/cmd-new-window.c
> @@ -98,13 +98,7 @@ cmd_new_window_exec(struct cmd *self, struct cmd_ctx *ctx)
>               cmd = options_get_string(&s->options, "default-command");
>       else
>               cmd = args->argv[0];
> -     cwd = options_get_string(&s->options, "default-path");
> -     if (*cwd == '\0') {
> -             if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
> -                     cwd = ctx->cmdclient->cwd;
> -             else
> -                     cwd = s->cwd;
> -     }
> +     cwd = cmd_get_default_path(ctx);
>  
>       if (idx == -1)
>               idx = -1 - options_get_number(&s->options, "base-index");
> diff --git a/cmd-split-window.c b/cmd-split-window.c
> index 258c632..7089193 100644
> --- a/cmd-split-window.c
> +++ b/cmd-split-window.c
> @@ -77,13 +77,7 @@ cmd_split_window_exec(struct cmd *self, struct cmd_ctx 
> *ctx)
>               cmd = options_get_string(&s->options, "default-command");
>       else
>               cmd = args->argv[0];
> -     cwd = options_get_string(&s->options, "default-path");
> -     if (*cwd == '\0') {
> -             if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
> -                     cwd = ctx->cmdclient->cwd;
> -             else
> -                     cwd = s->cwd;
> -     }
> +     cwd = cmd_get_default_path(ctx);
>  
>       type = LAYOUT_TOPBOTTOM;
>       if (args_has(args, 'h'))
> diff --git a/cmd.c b/cmd.c
> index 2027f75..c67dbed 100644
> --- a/cmd.c
> +++ b/cmd.c
> @@ -1212,3 +1212,35 @@ cmd_template_replace(char *template, const char *s, 
> int idx)
>  
>       return (buf);
>  }
> +
> +/*
> + * Return a default path for new windows appropriate for this command
> + * context.
> + */
> +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);
> +     wp = s->curw->window->active;
> +
> +     if (ctx->curclient != NULL
> +         && options_get_number(&s->options, "inherit-default-path")) {
> +             if ((cwd = osdep_get_pid_cwd(wp->pid)) != NULL)
> +                     return (cwd);
> +     }
> +
> +     cwd = options_get_string(&s->options, "default-path");
> +     if (*cwd == '\0') {
> +             if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
> +                     cwd = ctx->cmdclient->cwd;
> +             else
> +                     cwd = s->cwd;
> +     }
> +
> +     return (cwd);
> +}
> diff --git a/options-table.c b/options-table.c
> index 2700536..4bc2805 100644
> --- a/options-table.c
> +++ b/options-table.c
> @@ -166,6 +166,11 @@ const struct options_table_entry session_options_table[] 
> = {
>         .default_num = 2000
>       },
>  
> +     { .name = "inherit-default-path",
> +       .type = OPTIONS_TABLE_FLAG,
> +       .default_num = 0
> +     },
> +
>       { .name = "lock-after-time",
>         .type = OPTIONS_TABLE_NUMBER,
>         .minimum = 0,
> diff --git a/osdep-aix.c b/osdep-aix.c
> index 0dc07d6..02a650d 100644
> --- a/osdep-aix.c
> +++ b/osdep-aix.c
> @@ -28,6 +28,12 @@ osdep_get_name(unused int fd, unused char *tty)
>       return (NULL);
>  }
>  
> +char *
> +osdep_get_pid_cwd(pid_t pid)
> +{
> +     return (NULL);
> +}
> +
>  struct event_base *
>  osdep_event_init(void)
>  {
> diff --git a/osdep-darwin.c b/osdep-darwin.c
> index 229a493..092adde 100644
> --- a/osdep-darwin.c
> +++ b/osdep-darwin.c
> @@ -48,6 +48,12 @@ osdep_get_name(int fd, unused char *tty)
>       return (strdup(kp.kp_proc.p_comm));
>  }
>  
> +char *
> +osdep_get_pid_cwd(pid_t pid)
> +{
> +     return (NULL);
> +}
> +
>  struct event_base *
>  osdep_event_init(void)
>  {
> diff --git a/osdep-dragonfly.c b/osdep-dragonfly.c
> index b15abf9..6cb656f 100644
> --- a/osdep-dragonfly.c
> +++ b/osdep-dragonfly.c
> @@ -119,6 +119,12 @@ error:
>       return (NULL);
>  }
>  
> +char *
> +osdep_get_pid_cwd(pid_t pid)
> +{
> +     return (NULL);
> +}
> +
>  struct event_base *
>  osdep_event_init(void)
>  {
> diff --git a/osdep-freebsd.c b/osdep-freebsd.c
> index 6b0c888..779b027 100644
> --- a/osdep-freebsd.c
> +++ b/osdep-freebsd.c
> @@ -130,6 +130,12 @@ error:
>       return (NULL);
>  }
>  
> +char *
> +osdep_get_pid_cwd(pid_t pid)
> +{
> +     return (NULL);
> +}
> +
>  struct event_base *
>  osdep_event_init(void)
>  {
> diff --git a/osdep-hpux.c b/osdep-hpux.c
> index 3bec98e..e7ec565 100644
> --- a/osdep-hpux.c
> +++ b/osdep-hpux.c
> @@ -28,6 +28,12 @@ osdep_get_name(unused int fd, unused char *tty)
>       return (NULL);
>  }
>  
> +char *
> +osdep_get_pid_cwd(pid_t pid)
> +{
> +     return (NULL);
> +}
> +
>  struct event_base *
>  osdep_event_init(void)
>  {
> diff --git a/osdep-linux.c b/osdep-linux.c
> index b4a1a9f..3d3cf1f 100644
> --- a/osdep-linux.c
> +++ b/osdep-linux.c
> @@ -60,6 +60,23 @@ osdep_get_name(int fd, unused char *tty)
>       return (buf);
>  }
>  
> +char *
> +osdep_get_pid_cwd(pid_t pid)
> +{
> +     static char      target[MAXPATHLEN + 1];
> +     char            *path;
> +     ssize_t          n;
> +
> +     xasprintf(&path, "/proc/%d/cwd", pid);
> +     n = readlink(path, target, MAXPATHLEN);
> +     xfree(path);
> +     if (n > 0) {
> +             target[n] = '\0';
> +             return (target);
> +     }
> +     return (NULL);
> +}
> +
>  struct event_base *
>  osdep_event_init(void)
>  {
> diff --git a/osdep-netbsd.c b/osdep-netbsd.c
> index 64fcb3e..8a2b13f 100644
> --- a/osdep-netbsd.c
> +++ b/osdep-netbsd.c
> @@ -123,6 +123,12 @@ error:
>       return (NULL);
>  }
>  
> +char *
> +osdep_get_pid_cwd(pid_t pid)
> +{
> +     return (NULL);
> +}
> +
>  struct event_base *
>  osdep_event_init(void)
>  {
> diff --git a/osdep-openbsd.c b/osdep-openbsd.c
> index 9eefc44..74059c2 100644
> --- a/osdep-openbsd.c
> +++ b/osdep-openbsd.c
> @@ -133,6 +133,12 @@ error:
>       return (NULL);
>  }
>  
> +char *
> +osdep_get_pid_cwd(pid_t pid)
> +{
> +     return (NULL);
> +}
> +
>  struct event_base *
>  osdep_event_init(void)
>  {
> diff --git a/osdep-sunos.c b/osdep-sunos.c
> index 5d1e582..565bbc3 100644
> --- a/osdep-sunos.c
> +++ b/osdep-sunos.c
> @@ -64,6 +64,12 @@ osdep_get_name(int fd, char *tty)
>       return (xstrdup(p.pr_fname));
>  }
>  
> +char *
> +osdep_get_pid_cwd(pid_t pid)
> +{
> +     return (NULL);
> +}
> +
>  struct event_base *
>  osdep_event_init(void)
>  {
> diff --git a/osdep-unknown.c b/osdep-unknown.c
> index 3bec98e..e7ec565 100644
> --- a/osdep-unknown.c
> +++ b/osdep-unknown.c
> @@ -28,6 +28,12 @@ osdep_get_name(unused int fd, unused char *tty)
>       return (NULL);
>  }
>  
> +char *
> +osdep_get_pid_cwd(pid_t pid)
> +{
> +     return (NULL);
> +}
> +
>  struct event_base *
>  osdep_event_init(void)
>  {
> diff --git a/tmux.1 b/tmux.1
> index cbe8062..cecc07e 100644
> --- a/tmux.1
> +++ b/tmux.1
> @@ -1847,6 +1847,9 @@ Set the default working directory for processes created 
> from keys, or
>  interactively from the prompt.
>  The default is empty, which means to use the working directory of the shell
>  from which the server was started if it is available or the user's home if 
> not.
> +See also the
> +.Ic inherit-default-path
> +option.
>  .It Ic default-shell Ar path
>  Specify the default shell.
>  This is used as the login shell for new windows when the
> @@ -1908,6 +1911,13 @@ is in milliseconds.
>  Set the maximum number of lines held in window history.
>  This setting applies only to new windows - existing window histories are not
>  resized and retain the limit at the point they were created.
> +.It Xo Ic inherit-default-path
> +.Op Ic on | off
> +.Xc
> +If on, new windows are created in the same working directory as the
> +currently active window instead of using the value of the
> +.Ic default-path
> +option. This is not supported on all platforms.
>  .It Ic lock-after-time Ar number
>  Lock the session (like the
>  .Ic lock-session
> diff --git a/tmux.h b/tmux.h
> index 8c9f9e7..226c3d6 100644
> --- a/tmux.h
> +++ b/tmux.h
> @@ -1556,6 +1556,7 @@ int              cmd_find_index(
>  struct winlink       *cmd_find_pane(struct cmd_ctx *,
>                    const char *, struct session **, struct window_pane **);
>  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_attach_session_entry;
>  extern const struct cmd_entry cmd_bind_key_entry;
> @@ -2073,6 +2074,7 @@ u_int   utf8_split2(u_int, u_char *);
>  
>  /* osdep-*.c */
>  char         *osdep_get_name(int, char *);
> +char         *osdep_get_pid_cwd(pid_t);
>  struct event_base *osdep_event_init(void);
>  
>  /* log.c */

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure 
contains a definitive record of customers, application performance, 
security threats, fraudulent activity, and more. Splunk takes this 
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
tmux-users mailing list
tmux-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tmux-users

Reply via email to