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