diff --git a/plugins/python/python.plugin.zsh b/plugins/python/python.plugin.zsh index a10c06fd..5d4da8df 100644 --- a/plugins/python/python.plugin.zsh +++ b/plugins/python/python.plugin.zsh @@ -9,6 +9,47 @@ function pyclean() { find ${ZSH_PYCLEAN_PLACES} -type d -name "__pycache__" -delete } +function pydoc-serve() { +# Serves offline python documentation +# Args: +# python_doc_version: 2 or 3 (will fall back to other if necessary) + which serve-doc >/dev/null + if [ $? -ne 0 ]; then + echo "This function (pydoc-serve) depends on the oh-my-zsh serve plugin." >&2 + return + fi + + local version=$1 + + # python2 prints --version to stderr, python3 to stdout + local iv="$(python --version 2>&1 | cut -d' ' -f2 | tr '.' ' ')" + [ $version -ne ${iv[1]} ] \ + && iv="$(python$version --version 2>&1 | cut -d' ' -f2 | tr '.' ' ')" + # Try to make this local … bash/zsh are a bad joke. + installed_version=(`echo $iv`) + local v1=${installed_version[1]} + local v2=${installed_version[2]} + local v3=${installed_version[3]} + + local done=-1 + for suffix in "" "docs"; do + if [ $done -ne 0 ]; then + serve-doc "python$v1.$v2.$v3" || \ + serve-doc "python$v1.$v2" || \ + serve-doc "python$v1" || \ + serve-doc "python" + done=$? + fi + done +} + +function pydoc2-serve() { + pydoc-serve 2 +} +function pydoc3-serve() { + pydoc-serve 3 +} + # Grep among .py files alias pygrep='grep --include="*.py"'