From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29320 invoked by alias); 27 Jan 2015 16:50:39 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 34415 Received: (qmail 20349 invoked from network); 27 Jan 2015 16:50:37 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 X-Biglobe-Sender: From: "Jun T." Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: [PATCH] cache list of all python modules Message-Id: <1E3C634B-96C3-4237-AC72-4770D4A49167@kba.biglobe.ne.jp> Date: Wed, 28 Jan 2015 01:49:55 +0900 To: zsh-workers@zsh.org Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) X-Mailer: Apple Mail (2.1878.6) X-Biglobe-Spnum: 56131 A new function _python_modules will create and cache the list of all the python modules. _python and _pydoc are updated to call this function. (_pydoc is also updated for python3) #compdef lines in _python/_pydoc are modified to use -P pattern. Jun --- Completion/Unix/Command/_pydoc | 41 = ++++++++++++++++++++++----------- Completion/Unix/Command/_python | 15 +++--------- Completion/Unix/Type/_python_modules | 44 = ++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 25 deletions(-) create mode 100644 Completion/Unix/Type/_python_modules diff --git a/Completion/Unix/Command/_pydoc = b/Completion/Unix/Command/_pydoc index f85ab0c..677c96a 100644 --- a/Completion/Unix/Command/_pydoc +++ b/Completion/Unix/Command/_pydoc @@ -1,19 +1,34 @@ -#compdef pydoc +#compdef -P pydoc[0-9.]# =20 -local context state line ret=3D1 +local curcontext=3D$curcontext state state_descr line ret=3D1 typeset -A opt_args +local -a args =20 -_arguments \ - '(-)-k[search keyword]:keyword' \ - '(- *)-p[start web server on specified port]:port number' \ - '(- *)-g[start gui]' \ - '(-)-w[write out HTML]:file or dir:_files' \ - '(- *)-h[show help information]' \ - '*: :->lookup' && ret=3D0 +args=3D( + '(- *)-k[search keyword]:keyword' + '(-k -g -w *)-p[start web server on specified port]:port number' + '(-)-w[write out HTML in current directory]' + '(-)*: :->lookup' +) =20 -[[ -n $state ]] && _alternative -C $context \ - 'keywords:keyword:compadd ${=3D${${(f)"$(_call_program keywords pydoc = keywords)"}[2,-1]}}' \ - 'topics:topic:compadd ${=3D${${(f)"$(_call_program topics pydoc = topics)"}[2,-1]}}' \ - 'modules:module:' && ret=3D0 +if _pick_variant pydoc3=3D'pydoc3 -b' pydoc2 -h; then + args+=3D( '(-k -w *)-b[start server and open browser]' ) +else + args+=3D( '(- *)-g[start gui]' ) +fi + +_arguments -C : $args && return 0 + +case $state in +(lookup) + if [[ $words[CURRENT] =3D */* ]]; then + _files && ret=3D0 + else + _alternative \ + 'keywords:keyword:compadd ${=3D${${(f)"$(_call_program keywords = $words[1] keywords)"}[2,-1]}}' \ + 'topics:topic:compadd ${=3D${${(f)"$(_call_program topics = $words[1] topics)"}[2,-1]}}' \ + 'modules:module:_python_modules' && ret=3D0 + fi +esac =20 return ret diff --git a/Completion/Unix/Command/_python = b/Completion/Unix/Command/_python index 8e32bdf..dedb9ce 100644 --- a/Completion/Unix/Command/_python +++ b/Completion/Unix/Command/_python @@ -1,11 +1,10 @@ -#compdef python python2 python2.4 python2.5 python2.6 python2.7 = python3.0 python3.1 python3.2 python3.3 python3.4 +#compdef -P python[0-9.]# =20 # Python 2.7 # Python 3.4 =20 -local curcontext=3D"$curcontext" state line expl +local curcontext=3D"$curcontext" state state_descr line typeset -A opt_args - local -a args =20 if _pick_variant python3=3DPython\ 3 python2 --version; then @@ -32,7 +31,7 @@ _arguments -C -s -S "$args[@]" \ '-E[ignore PYTHON* environment variables (such as PYTHONPATH)]' \ '(1 * -)-h[display help information]' \ '-i[inspect interactively after running script]' \ - '(1 -)-m[run library module as a script (terminates option = list)]:module:->modules' \ + '(1 -)-m[run library module as a script (terminates option = list)]:module:_python_modules' \ '-O[optimize generated bytecode slightly]' \ '-OO[remove doc-strings in addition to the -O optimizations]' \ "-s[don't add user site directory to sys.path]" \ @@ -46,14 +45,6 @@ _arguments -C -s -S "$args[@]" \ '*::script argument:=3D ->normal' && return =20 case "$state" in - modules) - local -a modules - modules=3D( - ${${=3D${(f)"$(_call_program modules $words[1] -c \ - 'from\ pydoc\ import\ help\;\ = help\(\"modules\"\)')"}[2,-3]}:#\(package\)} - ) - _wanted modules expl module compadd -a modules && return - ;; normal) if [[ -z "$opt_args[(I)-(c|m)]" ]]; then shift words diff --git a/Completion/Unix/Type/_python_modules = b/Completion/Unix/Type/_python_modules new file mode 100644 index 0000000..b30848d --- /dev/null +++ b/Completion/Unix/Type/_python_modules @@ -0,0 +1,44 @@ +#autoload + +_python_module_caching_policy () { + local -a newer + # rebuild if cache does not exist or is more than a week old + newer=3D( "$1"(Nmw-1) ) + return $#newer +} + +_python_modules () { + local update_policy python expl + + case $words[1] in + (python*) python=3D$words[1] ;; + (pydoc*) python=3D${words[1]/#pydoc/python} ;; + (*) python=3D"python" ;; + esac + local cache_id=3D${${python//[^[:alnum:]]/_}#_}_modules + local array_name=3D_${cache_id} + + zstyle -s ":completion:${curcontext}:" cache-policy update_policy + [[ -z "$update_policy" ]] && \ + zstyle ":completion:${curcontext}:" \ + cache-policy _python_module_caching_policy + + if ( [[ ${(P)+array_name} -eq 0 ]] || _cache_invalid $cache_id ) && + ! _retrieve_cache $cache_id; then + + local script=3D'import sys, pydoc +def f(p,m,d): + if m.find(".") < 0: sys.stdout.write(m+"\n") +pydoc.ModuleScanner().run(f)' + + typeset -agU $array_name + set -A $array_name \ + $(_call_program modules $python -c ${(q)script} 2>/dev/null) + + _store_cache $cache_id $array_name + fi + + _wanted modules expl module compadd "$@" -a -- $array_name +} + +_python_modules "$@" --=20 1.9.3 (Apple Git-50)