##// END OF EJS Templates
bash completion: history clear & trim handle flags
Paul Ivanov -
Show More
@@ -1,126 +1,131 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
46
47 if [[ ${cur} == -* ]]; then
47 if [[ ${cur} == -* ]]; then
48 case $mode in
48 case $mode in
49 "notebook" | "qtconsole" | "console" | "kernel" | "nbconvert")
49 "notebook" | "qtconsole" | "console" | "kernel" | "nbconvert")
50 _ipython_get_flags $mode
50 _ipython_get_flags $mode
51 opts=$"${opts} ${baseopts}"
51 opts=$"${opts} ${baseopts}"
52 ;;
52 ;;
53 "locate" | "profile")
53 "locate" | "profile")
54 _ipython_get_flags $mode
54 _ipython_get_flags $mode
55 ;;
55 ;;
56 "history")
56 "history")
57 if [[ "${COMP_WORDS[2]}" == "trim" ]]; then
57 if [[ $COMP_CWORD -ge 2 ]]; then
58 _ipython_get_flags "history trim"
58 # 'history trim' and 'history clear' covered by next line
59 _ipython_get_flags history\ "${COMP_WORDS[2]}"
59 else
60 else
60 _ipython_get_flags $mode
61 _ipython_get_flags $mode
61
62
62 fi
63 fi
63 opts=$"${opts}"
64 opts=$"${opts}"
64 ;;
65 ;;
65 *)
66 *)
66 opts=$baseopts
67 opts=$baseopts
67 esac
68 esac
68 # don't drop the trailing space
69 # don't drop the trailing space
69 local IFS=$'\t\n'
70 local IFS=$'\t\n'
70 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
71 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
71 return 0
72 return 0
72 elif [[ $mode == "profile" ]]; then
73 elif [[ $mode == "profile" ]]; then
73 opts="list create locate"
74 opts="list create locate "
75 local IFS=$'\t\n'
74 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
76 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
75 elif [[ $mode == "history" ]]; then
77 elif [[ $mode == "history" ]]; then
76 if [[ "${COMP_WORDS[2]}" == "trim" ]]; then
78 if [[ $COMP_CWORD -ge 3 ]]; then
77 COMPREPLY="--"
79 # drop into flags
80 opts="--"
78 else
81 else
79 COMPREPLY="trim "
82 opts="trim clear "
80 fi
83 fi
84 local IFS=$'\t\n'
85 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
81 elif [[ $mode == "locate" ]]; then
86 elif [[ $mode == "locate" ]]; then
82 opts="profile"
87 opts="profile"
83 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
88 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
84 elif [[ ${prev} == "--pylab"* ]] || [[ ${prev} == "--gui"* ]]; then
89 elif [[ ${prev} == "--pylab"* ]] || [[ ${prev} == "--gui"* ]]; then
85 if [ -z "$__ipython_complete_pylab" ]; then
90 if [ -z "$__ipython_complete_pylab" ]; then
86 __ipython_complete_pylab=`cat <<EOF | python -
91 __ipython_complete_pylab=`cat <<EOF | python -
87 try:
92 try:
88 import IPython.core.shellapp as mod;
93 import IPython.core.shellapp as mod;
89 for k in mod.InteractiveShellApp.pylab.values:
94 for k in mod.InteractiveShellApp.pylab.values:
90 print "%s " % k
95 print "%s " % k
91 except:
96 except:
92 pass
97 pass
93 EOF
98 EOF
94 `
99 `
95 fi
100 fi
96 local IFS=$'\t\n'
101 local IFS=$'\t\n'
97 COMPREPLY=( $(compgen -W "${__ipython_complete_pylab}" -- ${cur}) )
102 COMPREPLY=( $(compgen -W "${__ipython_complete_pylab}" -- ${cur}) )
98 elif [[ ${prev} == "--profile"* ]]; then
103 elif [[ ${prev} == "--profile"* ]]; then
99 if [ -z "$__ipython_complete_profiles" ]; then
104 if [ -z "$__ipython_complete_profiles" ]; then
100 __ipython_complete_profiles=`cat <<EOF | python -
105 __ipython_complete_profiles=`cat <<EOF | python -
101 try:
106 try:
102 import IPython.core.profileapp
107 import IPython.core.profileapp
103 for k in IPython.core.profileapp.list_bundled_profiles():
108 for k in IPython.core.profileapp.list_bundled_profiles():
104 print "%s " % k
109 print "%s " % k
105 p = IPython.core.profileapp.ProfileList()
110 p = IPython.core.profileapp.ProfileList()
106 for k in IPython.core.profileapp.list_profiles_in(p.ipython_dir):
111 for k in IPython.core.profileapp.list_profiles_in(p.ipython_dir):
107 print "%s " % k
112 print "%s " % k
108 except:
113 except:
109 pass
114 pass
110 EOF
115 EOF
111 `
116 `
112 fi
117 fi
113 local IFS=$'\t\n'
118 local IFS=$'\t\n'
114 COMPREPLY=( $(compgen -W "${__ipython_complete_profiles}" -- ${cur}) )
119 COMPREPLY=( $(compgen -W "${__ipython_complete_profiles}" -- ${cur}) )
115 else
120 else
116 if [ "$COMP_CWORD" == 1 ]; then
121 if [ "$COMP_CWORD" == 1 ]; then
117 local IFS=$'\t\n'
122 local IFS=$'\t\n'
118 local sub=$(echo $subcommands | sed -e "s/ / \t/g")
123 local sub=$(echo $subcommands | sed -e "s/ / \t/g")
119 COMPREPLY=( $(compgen -W "${sub}" -- ${cur}) )
124 COMPREPLY=( $(compgen -W "${sub}" -- ${cur}) )
120 else
125 else
121 COMPREPLY=( $(compgen -f -- ${cur}) )
126 COMPREPLY=( $(compgen -f -- ${cur}) )
122 fi
127 fi
123 fi
128 fi
124
129
125 }
130 }
126 complete -o default -o nospace -F _ipython ipython
131 complete -o default -o nospace -F _ipython ipython
General Comments 0
You need to be logged in to leave comments. Login now