Repository: xonsh/xonsh

Issue:
Enable Subprocess Completion for Subsequent Tokens
I have used the following to set my bash completion:  

``` bash
$BASH_COMPLETIONS=`/usr/local/etc/bash_completion.d/*`
```

There's a `git-completion.bash` in that folder, so one would expect to be able to type `git clo<TAB>` and get `git clone`.  I read in the docs that completion is enabled for the first token in Subprocess mode.  Is there a chance to enable it for the rest of the tokens as well?



Discussion:
Right now subsequent tokens are treated as Python, so that if I type `git cl<TAB>` I get `git class`.

Hi @blink1073 - where are you setting the `$BASH_COMPLETIONS`? This needs to be done in the `~/.xonshrc` file.  Also, since you are using the backtick globbing, you probably meant to do `.*` at the end of your regex.  That is:

``` console
$BASH_COMPLETIONS = `/usr/local/etc/bash_completion.d/.*`
```

Do those fixes help?

Works on my Linux box, I'll try it on my Mac when I get home, thanks @scopatz.

Yep, it works now, but I get a _lot_ of errors when I start up.  Here's a sampling:

``` python
/usr/local/etc/bash_completion.d/update-alternatives: line 39: syntax error in conditional expression: unexpected token `('
/usr/local/etc/bash_completion.d/update-alternatives: line 39: syntax error near `--@(i'
/usr/local/etc/bash_completion.d/update-alternatives: line 39: `        if [[ "${COMP_WORDS[i]}" == --@(install|remove|auto|display|config|remove-all|set) ]]; then'
/usr/local/etc/bash_completion.d/util-linux: line 5: have: command not found
```

Can you post the full output please?

https://gist.github.com/blink1073/cdfa267f222f83d1066d

You need to add the file "/usr/local/etc/bash_completion" to the front of the list, if it exists.  If not, you need to find the bash completion file that contains the `have()` function and then put that file at the beginning. 

`have()` is indeed defined in "/usr/local/etc/bash_completion", and I added as 

```
$BASH_COMPLETIONS=["/usr/local/etc/bash_completion"] + `/usr/local/etc/bash_completion.d/.*`
```

The error persists.  I verified that the desired file was first in `$BASH_COMPLETIONS`.

What if you leave off the regex globbing?  Here is what mine looks like:

``` console
scopatz@athenaie ~ $ $BASH_COMPLETIONS
['/etc/bash_completion', '/usr/share/bash-completion/completions/git']
```

We have a winner!

Final version: `$BASH_COMPLETIONS=["/usr/local/etc/bash_completion"]`  

Great!  Please feel free to close.

I'm pondering whether the docs should be more explicit on this.  Right now `$BASH_COMPLETIONS` is mentioned in the docs, but not the fact that it _must_ be in the `.xonshrc` file (I was setting it via the command line at first).  

It also mentions that the defaults are platform dependent, but sane.  When using brew, it ends up in `$(brew --prefix)/etc/bash_completion`.  When using Macports its in `/opt/local/etc/profile.d/bash_completion.sh`.  Perhaps these locations should be checked in `environ.py` and added if available?

http://blog.jeffterrace.com/2012/09/bash-completion-for-mac-os-x.html
https://trac.macports.org/wiki/howto/bash-completion

(or just assume `/usr/local/` for `(brew --prefix)` since that is the default).

Hi @blink1073 - I completely agree.  The docs should be more explicit.  Also, I am all for adding checks for common locations in environ.  Adding extra files should be safe, since the completer will check for the file's existence before trying to source it.  

Pull requests are really very welcome here for both of these issues. I clearly did a bad job on the docs the first time and I can't test this with macports or brew easily :)


Resolving patch:
diff --git a/xonsh/environ.py b/xonsh/environ.py
--- a/xonsh/environ.py
+++ b/xonsh/environ.py
@@ -14,7 +14,7 @@
 
 def current_branch(cwd=None):
     """Gets the branch for a current working directory. Returns None
-    if the cwd is not a repository.  This currently only works for git, 
+    if the cwd is not a repository.  This currently only works for git,
     bust should be extended in the future.
     """
     branch = None
@@ -50,7 +50,7 @@ def current_branch(cwd=None):
         try:
             s = subprocess.check_output(['git', 'rev-parse','--abbrev-ref', 'HEAD'],
                     stderr=subprocess.PIPE, cwd=cwd,
-                    universal_newlines=True) 
+                    universal_newlines=True)
             s = s.strip()
             if len(s) > 0:
                 branch = s
@@ -135,9 +135,10 @@ def multiline_prompt():
     }
 
 if platform.system() == 'Darwin':
-    BASE_ENV['BASH_COMPLETIONS'] = []
+    BASE_ENV['BASH_COMPLETIONS'] = ['/usr/local/etc/bash_completion',
+                                    '/opt/local/etc/profile.d/bash_completion.sh']
 else:
-    BASE_ENV['BASH_COMPLETIONS'] = ['/etc/bash_completion', 
+    BASE_ENV['BASH_COMPLETIONS'] = ['/etc/bash_completion',
                                     '/usr/share/bash-completion/completions/git']
 
 def bash_env():
@@ -146,7 +147,7 @@ def bash_env():
     if hasattr(builtins, '__xonsh_env__'):
         currenv = builtins.__xonsh_env__.detype()
     try:
-        s = subprocess.check_output(['bash', '-i'], input='env', env=currenv, 
+        s = subprocess.check_output(['bash', '-i'], input='env', env=currenv,
                                     stderr=subprocess.PIPE,
                                     universal_newlines=True)
     except subprocess.CalledProcessError:


Test patch: