Skip to content

Commit c23f915

Browse files
committed
move tab completion initialization; expand support for tab completion in zsh (fixes #97)
1 parent f91d717 commit c23f915

File tree

2 files changed

+48
-34
lines changed

2 files changed

+48
-34
lines changed

docs/en/history.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
Release History
33
===============
44

5+
2.7.2
6+
7+
- Move setup code for tab completion later in the startup code so
8+
all of the needed variables are configured. (:bbissue:`97`)
9+
- Expand tab completion for zsh to work for all commands.
10+
511
2.7.1
612

713
- When testing for WORKON_HOME during startup, dereference any

virtualenvwrapper.sh

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,44 @@ virtualenvwrapper_run_hook () {
149149
return $result
150150
}
151151

152+
# Set up tab completion. (Adapted from Arthur Koziel's version at
153+
# http://arthurkoziel.com/2008/10/11/virtualenvwrapper-bash-completion/)
154+
virtualenvwrapper_setup_tab_completion () {
155+
if [ -n "$BASH" ] ; then
156+
_virtualenvs () {
157+
local cur="${COMP_WORDS[COMP_CWORD]}"
158+
COMPREPLY=( $(compgen -W "`virtualenvwrapper_show_workon_options`" -- ${cur}) )
159+
}
160+
_cdvirtualenv_complete () {
161+
local cur="$2"
162+
COMPREPLY=( $(cdvirtualenv && compgen -d -- "${cur}" ) )
163+
}
164+
_cdsitepackages_complete () {
165+
local cur="$2"
166+
COMPREPLY=( $(cdsitepackages && compgen -d -- "${cur}" ) )
167+
}
168+
complete -o nospace -F _cdvirtualenv_complete -S/ cdvirtualenv
169+
complete -o nospace -F _cdsitepackages_complete -S/ cdsitepackages
170+
complete -o default -o nospace -F _virtualenvs workon
171+
complete -o default -o nospace -F _virtualenvs rmvirtualenv
172+
complete -o default -o nospace -F _virtualenvs cpvirtualenv
173+
complete -o default -o nospace -F _virtualenvs showvirtualenv
174+
elif [ -n "$ZSH_VERSION" ] ; then
175+
_virtualenvs () {
176+
reply=( $(virtualenvwrapper_show_workon_options) )
177+
}
178+
_cdvirtualenv_complete () {
179+
reply=( $(cdvirtualenv && ls -d ${1}*) )
180+
}
181+
_cdsitepackages_complete () {
182+
reply=( $(cdsitepackages && ls -d ${1}*) )
183+
}
184+
compctl -K _virtualenvs workon rmvirtualenv cpvirtualenv showvirtualenv
185+
compctl -K _cdvirtualenv_complete cdvirtualenv
186+
compctl -K _cdsitepackages_complete cdsitepackages
187+
fi
188+
}
189+
152190
# Set up virtualenvwrapper properly
153191
virtualenvwrapper_initialize () {
154192
export WORKON_HOME="$(virtualenvwrapper_derive_workon_home)"
@@ -173,8 +211,12 @@ virtualenvwrapper_initialize () {
173211
echo "virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenv has been installed for VIRTUALENVWRAPPER_PYTHON=$VIRTUALENVWRAPPER_PYTHON and that PATH is set properly." 1>&2
174212
return 1
175213
fi
214+
215+
virtualenvwrapper_setup_tab_completion
216+
176217
}
177218

219+
178220
# Verify that virtualenv is installed and visible
179221
virtualenvwrapper_verify_virtualenv () {
180222
typeset venv=$(\which "$VIRTUALENVWRAPPER_VIRTUALENV" | (unset GREP_OPTIONS; \grep -v "not found"))
@@ -413,40 +455,6 @@ workon () {
413455
}
414456

415457

416-
#
417-
# Set up tab completion. (Adapted from Arthur Koziel's version at
418-
# http://arthurkoziel.com/2008/10/11/virtualenvwrapper-bash-completion/)
419-
#
420-
421-
if [ -n "$BASH" ] ; then
422-
_virtualenvs ()
423-
{
424-
local cur="${COMP_WORDS[COMP_CWORD]}"
425-
COMPREPLY=( $(compgen -W "`virtualenvwrapper_show_workon_options`" -- ${cur}) )
426-
}
427-
428-
429-
_cdvirtualenv_complete ()
430-
{
431-
local cur="$2"
432-
# COMPREPLY=( $(compgen -d -- "${VIRTUAL_ENV}/${cur}" | sed -e "s@${VIRTUAL_ENV}/@@" ) )
433-
COMPREPLY=( $(cdvirtualenv && compgen -d -- "${cur}" ) )
434-
}
435-
_cdsitepackages_complete ()
436-
{
437-
local cur="$2"
438-
COMPREPLY=( $(cdsitepackages && compgen -d -- "${cur}" ) )
439-
}
440-
complete -o nospace -F _cdvirtualenv_complete -S/ cdvirtualenv
441-
complete -o nospace -F _cdsitepackages_complete -S/ cdsitepackages
442-
complete -o default -o nospace -F _virtualenvs workon
443-
complete -o default -o nospace -F _virtualenvs rmvirtualenv
444-
complete -o default -o nospace -F _virtualenvs cpvirtualenv
445-
complete -o default -o nospace -F _virtualenvs showvirtualenv
446-
elif [ -n "$ZSH_VERSION" ] ; then
447-
compctl -g "`virtualenvwrapper_show_workon_options`" workon rmvirtualenv cpvirtualenv showvirtualenv
448-
fi
449-
450458
# Prints the Python version string for the current interpreter.
451459
virtualenvwrapper_get_python_version () {
452460
# Uses the Python from the virtualenv because we're trying to

0 commit comments

Comments
 (0)