ipython-completion.bash
131 lines
| 4.1 KiB
| text/x-sh
|
BashLexer
Julian Taylor
|
r8292 | # load with: . ipython-completion.bash | ||
Nathan Goldbaum
|
r10204 | if [[ -n ${ZSH_VERSION-} ]]; then | ||
autoload -Uz bashcompinit && bashcompinit | ||||
fi | ||||
Julian Taylor
|
r8292 | _ipython_get_flags() | ||
{ | ||||
local url=$1 | ||||
local var=$2 | ||||
local dash=$3 | ||||
if [[ "$url $var" == $__ipython_complete_last ]]; then | ||||
opts=$__ipython_complete_last_res | ||||
return | ||||
fi | ||||
MinRK
|
r15183 | # matplotlib and profile don't need the = and the | ||
Julian Taylor
|
r8343 | # version without simplifies the special cased completion | ||
MinRK
|
r15183 | opts=$(ipython ${url} --help-all | grep -E "^-{1,2}[^-]" | sed -e "s/<.*//" -e "s/[^=]$/& /" -e "s/^--matplotlib=$//" -e "s/^--profile=$/--profile /") | ||
Julian Taylor
|
r8292 | __ipython_complete_last="$url $var" | ||
__ipython_complete_last_res="$opts" | ||||
} | ||||
_ipython() | ||||
{ | ||||
local cur=${COMP_WORDS[COMP_CWORD]} | ||||
local prev=${COMP_WORDS[COMP_CWORD - 1]} | ||||
Paul Ivanov
|
r13602 | local subcommands="notebook qtconsole console kernel profile locate history nbconvert " | ||
Julian Taylor
|
r8292 | local opts="" | ||
if [ -z "$__ipython_complete_baseopts" ]; then | ||||
Julian Taylor
|
r8338 | _ipython_get_flags baseopts | ||
Julian Taylor
|
r8292 | __ipython_complete_baseopts="${opts}" | ||
fi | ||||
local baseopts="$__ipython_complete_baseopts" | ||||
local mode="" | ||||
for i in "${COMP_WORDS[@]}"; do | ||||
[ "$cur" = "$i" ] && break | ||||
if [[ ${subcommands} == *${i}* ]]; then | ||||
mode="$i" | ||||
Julian Taylor
|
r8337 | break | ||
elif [[ ${i} == "--"* ]]; then | ||||
mode="nosubcommand" | ||||
break | ||||
Julian Taylor
|
r8292 | fi | ||
done | ||||
Paul Ivanov
|
r13568 | |||
if [[ ${cur} == -* ]]; then | ||||
Paul Ivanov
|
r13567 | case $mode in | ||
Paul Ivanov
|
r13602 | "notebook" | "qtconsole" | "console" | "kernel" | "nbconvert") | ||
Paul Ivanov
|
r13567 | _ipython_get_flags $mode | ||
opts=$"${opts} ${baseopts}" | ||||
;; | ||||
Paul Ivanov
|
r13608 | "locate" | "profile") | ||
Paul Ivanov
|
r13567 | _ipython_get_flags $mode | ||
Paul Ivanov
|
r13608 | ;; | ||
"history") | ||||
Paul Ivanov
|
r13617 | if [[ $COMP_CWORD -ge 3 ]]; then | ||
Paul Ivanov
|
r13615 | # 'history trim' and 'history clear' covered by next line | ||
_ipython_get_flags history\ "${COMP_WORDS[2]}" | ||||
Paul Ivanov
|
r13608 | else | ||
_ipython_get_flags $mode | ||||
fi | ||||
Paul Ivanov
|
r13567 | opts=$"${opts}" | ||
;; | ||||
*) | ||||
opts=$baseopts | ||||
esac | ||||
Julian Taylor
|
r8343 | # don't drop the trailing space | ||
local IFS=$'\t\n' | ||||
Julian Taylor
|
r8292 | COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | ||
return 0 | ||||
Paul Ivanov
|
r13568 | elif [[ $mode == "profile" ]]; then | ||
Paul Ivanov
|
r13615 | opts="list create locate " | ||
local IFS=$'\t\n' | ||||
Paul Ivanov
|
r13568 | COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | ||
elif [[ $mode == "history" ]]; then | ||||
Paul Ivanov
|
r13615 | if [[ $COMP_CWORD -ge 3 ]]; then | ||
# drop into flags | ||||
opts="--" | ||||
Paul Ivanov
|
r13608 | else | ||
Paul Ivanov
|
r13615 | opts="trim clear " | ||
Paul Ivanov
|
r13608 | fi | ||
Paul Ivanov
|
r13615 | local IFS=$'\t\n' | ||
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | ||||
Paul Ivanov
|
r13568 | elif [[ $mode == "locate" ]]; then | ||
opts="profile" | ||||
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | ||||
MinRK
|
r15183 | elif [[ ${prev} == "--matplotlib"* ]] || [[ ${prev} == "--gui"* ]]; then | ||
if [ -z "$__ipython_complete_matplotlib" ]; then | ||||
__ipython_complete_matplotlib=`cat <<EOF | python - | ||||
Julian Taylor
|
r8339 | try: | ||
import IPython.core.shellapp as mod; | ||||
MinRK
|
r15183 | for k in mod.InteractiveShellApp.matplotlib.values: | ||
Julian Taylor
|
r8343 | print "%s " % k | ||
Julian Taylor
|
r8339 | except: | ||
pass | ||||
EOF | ||||
Julian Taylor
|
r8344 | ` | ||
Julian Taylor
|
r8339 | fi | ||
Julian Taylor
|
r8343 | local IFS=$'\t\n' | ||
MinRK
|
r15183 | COMPREPLY=( $(compgen -W "${__ipython_complete_matplotlib}" -- ${cur}) ) | ||
Julian Taylor
|
r8336 | elif [[ ${prev} == "--profile"* ]]; then | ||
if [ -z "$__ipython_complete_profiles" ]; then | ||||
Julian Taylor
|
r8344 | __ipython_complete_profiles=`cat <<EOF | python - | ||
Julian Taylor
|
r8336 | try: | ||
import IPython.core.profileapp | ||||
for k in IPython.core.profileapp.list_bundled_profiles(): | ||||
Julian Taylor
|
r8343 | print "%s " % k | ||
Julian Taylor
|
r8336 | p = IPython.core.profileapp.ProfileList() | ||
for k in IPython.core.profileapp.list_profiles_in(p.ipython_dir): | ||||
Julian Taylor
|
r8343 | print "%s " % k | ||
Julian Taylor
|
r8336 | except: | ||
pass | ||||
EOF | ||||
Julian Taylor
|
r8344 | ` | ||
Julian Taylor
|
r8336 | fi | ||
Julian Taylor
|
r8343 | local IFS=$'\t\n' | ||
Julian Taylor
|
r8336 | COMPREPLY=( $(compgen -W "${__ipython_complete_profiles}" -- ${cur}) ) | ||
Julian Taylor
|
r8292 | else | ||
Paul Ivanov
|
r13569 | if [ "$COMP_CWORD" == 1 ]; then | ||
Paul Ivanov
|
r13581 | local IFS=$'\t\n' | ||
local sub=$(echo $subcommands | sed -e "s/ / \t/g") | ||||
COMPREPLY=( $(compgen -W "${sub}" -- ${cur}) ) | ||||
Julian Taylor
|
r8292 | else | ||
COMPREPLY=( $(compgen -f -- ${cur}) ) | ||||
fi | ||||
fi | ||||
} | ||||
Julian Taylor
|
r8343 | complete -o default -o nospace -F _ipython ipython | ||