From 6b0aee3c8d337f71dc5ceb2ffe9514848d31e9e2 Mon Sep 17 00:00:00 2001 From: Gil Forsyth Date: Tue, 3 Nov 2015 12:31:28 -0500 Subject: [PATCH 1/4] add ensurer `AUTO_CD` bool and default to False This allows the user to set `$AUTO_CD=True` if they want the auto cd behavior to trigger, as proposed in https://github.com/scopatz/xonsh/issues/439 --- xonsh/environ.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xonsh/environ.py b/xonsh/environ.py index 6c9c4b530..1fcf24fff 100644 --- a/xonsh/environ.py +++ b/xonsh/environ.py @@ -47,6 +47,7 @@ represent environment variable validation, conversion, detyping. """ DEFAULT_ENSURERS = { + 'AUTO_CD': (is_bool, to_bool, bool_to_str), 'AUTO_SUGGEST': (is_bool, to_bool, bool_to_str), 'BASH_COMPLETIONS': (is_env_path, str_to_env_path, env_path_to_str), 'CASE_SENSITIVE_COMPLETIONS': (is_bool, to_bool, bool_to_str), @@ -112,6 +113,7 @@ def xonshconfig(env): # to set them they have to do a copy and write them to the environment. # try to keep this sorted. DEFAULT_VALUES = { + 'AUTO_CD': False, 'AUTO_PUSHD': False, 'AUTO_SUGGEST': True, 'BASH_COMPLETIONS': (('/usr/local/etc/bash_completion', From 6ed178446e64699f3c02811347411ef3652ac858 Mon Sep 17 00:00:00 2001 From: Gil Forsyth Date: Tue, 3 Nov 2015 12:38:15 -0500 Subject: [PATCH 2/4] add `AUTO_CD` to envvars docs --- docs/envvars.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/envvars.rst b/docs/envvars.rst index dec705d43..d1f2299bc 100644 --- a/docs/envvars.rst +++ b/docs/envvars.rst @@ -16,9 +16,12 @@ applicable. * - ANSICON - No default set - This is used on Windows to set the title, if available. + * - AUTO_CD + - ``False`` + - Flag to enable changing to a directory by entering the dirname or full path only (without the `cd` command) * - AUTO_PUSHD - ``False`` - - Flag for automatically pushing directorties onto the directory stack. + - Flag for automatically pushing directories onto the directory stack. * - AUTO_SUGGEST - ``True`` - Enable automatic command suggestions based on history (like in fish shell). From e858b009cfffe4a2648b82a905c327226233a2b2 Mon Sep 17 00:00:00 2001 From: Gil Forsyth Date: Tue, 3 Nov 2015 16:03:00 -0500 Subject: [PATCH 3/4] add autocd functionality to run_subproc relocated from base_shell. Checks the following: $AUTO_CD=True cmd is a directory cmd is not in $PATH no other arguments If so, it prepends 'cd' and reruns --- xonsh/built_ins.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/xonsh/built_ins.py b/xonsh/built_ins.py index 36d5707d8..d7bc00742 100644 --- a/xonsh/built_ins.py +++ b/xonsh/built_ins.py @@ -21,7 +21,7 @@ from collections import Sequence, MutableMapping, Iterable, namedtuple, \ from xonsh.tools import suggest_commands, XonshError, ON_POSIX, ON_WINDOWS, \ string_types from xonsh.inspectors import Inspector -from xonsh.environ import Env, default_env +from xonsh.environ import Env, default_env, locate_binary from xonsh.aliases import DEFAULT_ALIASES from xonsh.jobs import add_job, wait_for_active_job from xonsh.proc import ProcProxy, SimpleProcProxy, TeePTYProc @@ -523,6 +523,13 @@ def run_subproc(cmds, captured=True): stderr = streams['stderr'] uninew = (ix == last_cmd) and (not captured) alias = builtins.aliases.get(cmd[0], None) + if (alias is None + and builtins.__xonsh_env__.get('AUTO_CD') + and os.path.isdir(cmd[0]) + and locate_binary(cmd[0], cwd=None) is None + and len(cmds)==1): + cmd.insert(0, 'cd') + return run_subproc(cmds, captured=False) if callable(alias): aliased_cmd = alias else: From e3323169e30a5d6f81cb33ae8c7b2ba5cda070cc Mon Sep 17 00:00:00 2001 From: Gil Forsyth Date: Tue, 3 Nov 2015 16:39:47 -0500 Subject: [PATCH 4/4] replace recursive subproc call with alias assignment --- xonsh/built_ins.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xonsh/built_ins.py b/xonsh/built_ins.py index d7bc00742..8e08f556a 100644 --- a/xonsh/built_ins.py +++ b/xonsh/built_ins.py @@ -529,7 +529,8 @@ def run_subproc(cmds, captured=True): and locate_binary(cmd[0], cwd=None) is None and len(cmds)==1): cmd.insert(0, 'cd') - return run_subproc(cmds, captured=False) + alias = builtins.aliases.get('cd', None) + if callable(alias): aliased_cmd = alias else: