##// END OF EJS Templates
bash completion: generalize subcmd completer logic
Paul Ivanov -
Show More
@@ -1,110 +1,106 b''
1 # load with: . ipython-completion.bash
1 # load with: . ipython-completion.bash
2
2
3 if [[ -n ${ZSH_VERSION-} ]]; then
3 if [[ -n ${ZSH_VERSION-} ]]; then
4 autoload -Uz bashcompinit && bashcompinit
4 autoload -Uz bashcompinit && bashcompinit
5 fi
5 fi
6
6
7 _ipython_get_flags()
7 _ipython_get_flags()
8 {
8 {
9 local url=$1
9 local url=$1
10 local var=$2
10 local var=$2
11 local dash=$3
11 local dash=$3
12 if [[ "$url $var" == $__ipython_complete_last ]]; then
12 if [[ "$url $var" == $__ipython_complete_last ]]; then
13 opts=$__ipython_complete_last_res
13 opts=$__ipython_complete_last_res
14 return
14 return
15 fi
15 fi
16 # pylab and profile don't need the = and the
16 # pylab and profile don't need the = and the
17 # version without simplifies the special cased completion
17 # version without simplifies the special cased completion
18 opts=$(ipython ${url} --help-all | grep -E "^-{1,2}[^-]" | sed -e "s/<.*//" -e "s/[^=]$/& /" -e "s/^--pylab=$//" -e "s/^--profile=$/--profile /")
18 opts=$(ipython ${url} --help-all | grep -E "^-{1,2}[^-]" | sed -e "s/<.*//" -e "s/[^=]$/& /" -e "s/^--pylab=$//" -e "s/^--profile=$/--profile /")
19 __ipython_complete_last="$url $var"
19 __ipython_complete_last="$url $var"
20 __ipython_complete_last_res="$opts"
20 __ipython_complete_last_res="$opts"
21 }
21 }
22
22
23 _ipython()
23 _ipython()
24 {
24 {
25 local cur=${COMP_WORDS[COMP_CWORD]}
25 local cur=${COMP_WORDS[COMP_CWORD]}
26 local prev=${COMP_WORDS[COMP_CWORD - 1]}
26 local prev=${COMP_WORDS[COMP_CWORD - 1]}
27 local subcommands="notebook qtconsole console kernel profile locate history nbconvert"
27 local subcommands="notebook qtconsole console kernel profile locate history nbconvert"
28 local opts=""
28 local opts=""
29 if [ -z "$__ipython_complete_baseopts" ]; then
29 if [ -z "$__ipython_complete_baseopts" ]; then
30 _ipython_get_flags baseopts
30 _ipython_get_flags baseopts
31 __ipython_complete_baseopts="${opts}"
31 __ipython_complete_baseopts="${opts}"
32 fi
32 fi
33 local baseopts="$__ipython_complete_baseopts"
33 local baseopts="$__ipython_complete_baseopts"
34 local mode=""
34 local mode=""
35 for i in "${COMP_WORDS[@]}"; do
35 for i in "${COMP_WORDS[@]}"; do
36 [ "$cur" = "$i" ] && break
36 [ "$cur" = "$i" ] && break
37 if [[ ${subcommands} == *${i}* ]]; then
37 if [[ ${subcommands} == *${i}* ]]; then
38 mode="$i"
38 mode="$i"
39 break
39 break
40 elif [[ ${i} == "--"* ]]; then
40 elif [[ ${i} == "--"* ]]; then
41 mode="nosubcommand"
41 mode="nosubcommand"
42 break
42 break
43 fi
43 fi
44 done
44 done
45
45
46 if [[ $mode == "profile" ]]; then
46 if [[ $mode == "profile" ]]; then
47 opts="list create"
47 opts="list create"
48 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
48 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
49 elif [[ ${cur} == -* ]]; then
49 elif [[ ${cur} == -* ]]; then
50 if [[ $mode == "notebook" ]]; then
50 case $mode in
51 _ipython_get_flags notebook
51 "notebook" | "qtconsole" | "console" | "kernel")
52 opts=$"${opts} ${baseopts}"
52 _ipython_get_flags $mode
53 elif [[ $mode == "qtconsole" ]]; then
53 opts=$"${opts} ${baseopts}"
54 _ipython_get_flags qtconsole
54 ;;
55 opts="${opts} ${baseopts}"
55 "locate" | "history" | "profile")
56 elif [[ $mode == "console" ]]; then
56 _ipython_get_flags $mode
57 _ipython_get_flags console
57 opts=$"${opts}"
58 elif [[ $mode == "kernel" ]]; then
58 ;;
59 _ipython_get_flags kernel
59 *)
60 opts="${opts} ${baseopts}"
60 opts=$baseopts
61 elif [[ $mode == "locate" ]]; then
61 esac
62 opts=""
63 else
64 opts=$baseopts
65 fi
66 # don't drop the trailing space
62 # don't drop the trailing space
67 local IFS=$'\t\n'
63 local IFS=$'\t\n'
68 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
64 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
69 return 0
65 return 0
70 elif [[ ${prev} == "--pylab"* ]] || [[ ${prev} == "--gui"* ]]; then
66 elif [[ ${prev} == "--pylab"* ]] || [[ ${prev} == "--gui"* ]]; then
71 if [ -z "$__ipython_complete_pylab" ]; then
67 if [ -z "$__ipython_complete_pylab" ]; then
72 __ipython_complete_pylab=`cat <<EOF | python -
68 __ipython_complete_pylab=`cat <<EOF | python -
73 try:
69 try:
74 import IPython.core.shellapp as mod;
70 import IPython.core.shellapp as mod;
75 for k in mod.InteractiveShellApp.pylab.values:
71 for k in mod.InteractiveShellApp.pylab.values:
76 print "%s " % k
72 print "%s " % k
77 except:
73 except:
78 pass
74 pass
79 EOF
75 EOF
80 `
76 `
81 fi
77 fi
82 local IFS=$'\t\n'
78 local IFS=$'\t\n'
83 COMPREPLY=( $(compgen -W "${__ipython_complete_pylab}" -- ${cur}) )
79 COMPREPLY=( $(compgen -W "${__ipython_complete_pylab}" -- ${cur}) )
84 elif [[ ${prev} == "--profile"* ]]; then
80 elif [[ ${prev} == "--profile"* ]]; then
85 if [ -z "$__ipython_complete_profiles" ]; then
81 if [ -z "$__ipython_complete_profiles" ]; then
86 __ipython_complete_profiles=`cat <<EOF | python -
82 __ipython_complete_profiles=`cat <<EOF | python -
87 try:
83 try:
88 import IPython.core.profileapp
84 import IPython.core.profileapp
89 for k in IPython.core.profileapp.list_bundled_profiles():
85 for k in IPython.core.profileapp.list_bundled_profiles():
90 print "%s " % k
86 print "%s " % k
91 p = IPython.core.profileapp.ProfileList()
87 p = IPython.core.profileapp.ProfileList()
92 for k in IPython.core.profileapp.list_profiles_in(p.ipython_dir):
88 for k in IPython.core.profileapp.list_profiles_in(p.ipython_dir):
93 print "%s " % k
89 print "%s " % k
94 except:
90 except:
95 pass
91 pass
96 EOF
92 EOF
97 `
93 `
98 fi
94 fi
99 local IFS=$'\t\n'
95 local IFS=$'\t\n'
100 COMPREPLY=( $(compgen -W "${__ipython_complete_profiles}" -- ${cur}) )
96 COMPREPLY=( $(compgen -W "${__ipython_complete_profiles}" -- ${cur}) )
101 else
97 else
102 if [ -z "$mode" ]; then
98 if [ -z "$mode" ]; then
103 COMPREPLY=( $(compgen -f -W "${subcommands}" -- ${cur}) )
99 COMPREPLY=( $(compgen -f -W "${subcommands}" -- ${cur}) )
104 else
100 else
105 COMPREPLY=( $(compgen -f -- ${cur}) )
101 COMPREPLY=( $(compgen -f -- ${cur}) )
106 fi
102 fi
107 fi
103 fi
108
104
109 }
105 }
110 complete -o default -o nospace -F _ipython ipython
106 complete -o default -o nospace -F _ipython ipython
General Comments 0
You need to be logged in to leave comments. Login now