Debian Codegraph @

init/bash_completion.in

raw
1 # shellcheck shell=bash
2 #
3 # Bash commandline completion
4 #
5 _module_comgen_words() {
6 local -a words
7 local val
8 # split candidate list on IFS without triggering shell expansion (command
9 # substitution, arithmetic, ...): candidate words may come from module
10 # names read off disk, which must never be treated as executable code
11 IFS=$' \t\n' read -r -d '' -a words <<<"$1"
12 for val in "${words[@]}"; do
13 case "$val" in
14 "$2"*) COMPREPLY[${#COMPREPLY[@]}]="$val" ;;
15 esac
16 done
17 }
18
19 _module_comgen_words_and_files() {
20 local start=${#COMPREPLY[@]} i
21 _module_comgen_words "$1" "$2"
22 # do not append space to word completed if it is a directory (ends with /)
23 for ((i = start; i < ${#COMPREPLY[@]}; i++)); do
24 if [ "${COMPREPLY[i]: -1:1}" = '/' ]; then
25 # Bash >=4.0 is required for compopt
26 type compopt &>/dev/null && compopt -o nospace
27 break
28 fi
29 done
30 }
31
32 _module_avail() {
33 local cur="${1:-}"
34 # skip avail call if word currently being completed is an option keyword
35 if [ -z "$cur" ] || [ "${cur:0:1}" != '-' ]; then
36 module avail --color=never -s -t -S --no-indepth -o 'alias:indesym' "$cur" 2>&1
37 fi
38 }
39
40 _module_spider() {
41 local cur="${1:-}"
42 # skip spider call if word currently being completed is an option keyword
43 if [ -z "$cur" ] || [ "${cur:0:1}" != '-' ]; then
44 module spider --color=never -s -t -S --no-indepth -o 'alias:indesym' "$cur" 2>&1
45 fi
46 }
47
48 _module_savelist() {
49 module savelist --color=never -s -t 2>&1 | sed '
50 /No named collection\.$/d;
51 /Named collection list$/d;
52 /:$/d;'
53 }
54
55 _module_stashlist() {
56 module stashlist --color=never -s -t 2>&1 | sed '
57 /No stash collection\.$/d;
58 /Stash collection list$/d;
59 /:$/d;'
60 }
61
62 _module_not_yet_loaded() {
63 _module_avail "${1:-}" | sort | @SED_ERE@ "\%^(${LOADEDMODULES//:/|})$%d"
64 }
65
66 _module_long_arg_list() {
67 local cur="$1" i
68
69 if [[ ${COMP_WORDS[COMP_CWORD-2]} == sw* ]]
70 then
71 _module_comgen_words_and_files "$(_module_not_yet_loaded "$cur")" "$cur"
72 return
73 fi
74 for ((i = COMP_CWORD - 1; i > 0; i--))
75 do case ${COMP_WORDS[$i]} in
76 add|load)
77 _module_comgen_words_and_files "$(_module_not_yet_loaded "$cur")" "$cur"
78 break;;
79 rm|delete|remove|unload|switch|swap)
80 _module_comgen_words "${LOADEDMODULES//:/ }" "$cur"
81 break;;
82 esac
83 done
84 }
85
86 _module() {
87 local cur="$2" prev="$3"
88
89 COMPREPLY=()
90
91 case "$prev" in
92 add|add-any|load|load-any|try-add|try-load)
93 _module_comgen_words_and_files "@comp_load_opts@ $(_module_not_yet_loaded "$cur")" "$cur";;
94 avail) _module_comgen_words_and_files "@comp_avail_opts@ $(_module_avail "$cur")" "$cur";;
95 spider) _module_comgen_words_and_files "@comp_avail_opts@ $(_module_spider "$cur")" "$cur";;
96 edit) _module_comgen_words_and_files "$(_module_avail "$cur")" "$cur";;
97 aliases) COMPREPLY=( $(compgen -W "@comp_aliases_opts@" -- "$cur") );;
98 list) COMPREPLY=( $(compgen -W "@comp_list_opts@" -- "$cur") );;
99 savelist) COMPREPLY=( $(compgen -W "@comp_savelist_opts@" -- "$cur") );;
100 stashlist) COMPREPLY=( $(compgen -W "@comp_stashlist_opts@" -- "$cur") );;
101 stashpop) _module_comgen_words "@comp_stashpop_opts@ $(_module_stashlist)" "$cur";;
102 stashshow|stashrm)
103 _module_comgen_words "$(_module_stashlist)" "$cur";;
104 clear) COMPREPLY=( $(compgen -W "@comp_clear_opts@" -- "$cur") );;
105 restore) _module_comgen_words "@comp_restore_opts@ $(_module_savelist)" "$cur";;
106 save|saveshow|describe|saverm|disable|is-saved)
107 _module_comgen_words "$(_module_savelist)" "$cur";;
108 rm|delete|remove|unload)
109 _module_comgen_words "@comp_unload_opts@ ${LOADEDMODULES//:/ }" "$cur";;
110 switch|swap) _module_comgen_words "@comp_load_opts@ ${LOADEDMODULES//:/ }" "$cur";;
111 unuse|is-used) _module_comgen_words "${MODULEPATH//:/ }" "$cur";;
112 use) case "$cur" in
113 -*) _module_comgen_words "@comp_use_opts@" "$cur";;
114 *) ;; # let readline handle the completion
115 esac;;
116 -a|--append|cachebuild) ;; # let readline handle the completion
117 display|help|show|test|path|paths|is-loaded|info-loaded)
118 _module_comgen_words_and_files "@comp_mfile_opts@ $(_module_avail "$cur")" "$cur";;
119 is-avail)
120 _module_comgen_words_and_files "@comp_isavail_opts@ $(_module_avail "$cur")" "$cur";;
121 lint)
122 _module_comgen_words_and_files "@comp_lint_opts@ $(_module_avail "$cur")" "$cur";;
123 mod-to-sh)
124 _module_comgen_words_and_files "@comp_modtosh_opts@ $(_module_not_yet_loaded "$cur")" "$cur";;
125 whatis)
126 _module_comgen_words_and_files "@comp_whatis_opts@ $(_module_avail "$cur")" "$cur";;
127 apropos|keyword|search)
128 COMPREPLY=( $(compgen -W "@comp_search_opts@" -- "$cur") );;
129 config|--reset) COMPREPLY=( $(compgen -W "@comp_config_opts@" -- "$cur") );;
130 purge) COMPREPLY=( $(compgen -W "@comp_purge_opts@" -- "$cur") );;
131 reload|update) COMPREPLY=( $(compgen -W "@comp_reload_opts@" -- "$cur") );;
132 reset) COMPREPLY=( $(compgen -W "@comp_reset_opts@" -- "$cur") );;
133 stash) COMPREPLY=( $(compgen -W "@comp_stash_opts@" -- "$cur") );;
134 source) case "$cur" in
135 -*) COMPREPLY=( $(compgen -W "@comp_source_opts@" -- "$cur") );;
136 *) ;; # let readline handle the completion
137 esac;;
138 -h|--help|-V|--version|--dumpname|refresh|sh-to-mod|state|stashclear|cacheclear)
139 ;;
140 append-path|prepend-path)
141 COMPREPLY=( $(compgen -W "@comp_path_opts@" -- "$cur") );;
142 remove-path)
143 COMPREPLY=( $(compgen -W "@comp_rm_path_opts@" -- "$cur") );;
144 *) if test "$COMP_CWORD" -gt 2
145 then
146 _module_long_arg_list "$cur"
147 else
148 case "$cur" in
149 # The mappings below are optional abbreviations for convenience
150 ls) COMPREPLY=( "list" );; # map ls -> list
151 sw*) COMPREPLY=( "switch" );;
152
153 -*) COMPREPLY=( $(compgen -W "@comp_opts@" -- "$cur") );;
154 *) COMPREPLY=( $(compgen -W "@comp_opts@ @comp_cmds@" -- "$cur") );;
155 esac
156 fi;;
157 esac
158 }
159
160 # define completion for ml command if command exists
161 if type -t ml >/dev/null; then
162 _ml() {
163 local cur="$2" prev="$3"
164
165 COMPREPLY=()
166
167 case "$prev" in
168 add|add-any|load|load-any|try-add|try-load)
169 _module_comgen_words_and_files "@comp_load_opts@ $(_module_not_yet_loaded "$cur")" "$cur";;
170 avail) _module_comgen_words_and_files "@comp_avail_opts@ $(_module_avail "$cur")" "$cur";;
171 spider) _module_comgen_words_and_files "@comp_avail_opts@ $(_module_spider "$cur")" "$cur";;
172 edit) _module_comgen_words_and_files "$(_module_avail "$cur")" "$cur";;
173 aliases) COMPREPLY=( $(compgen -W "@comp_aliases_opts@" -- "$cur") );;
174 list) COMPREPLY=( $(compgen -W "@comp_list_opts@" -- "$cur") );;
175 savelist) COMPREPLY=( $(compgen -W "@comp_savelist_opts@" -- "$cur") );;
176 stashlist) COMPREPLY=( $(compgen -W "@comp_stashlist_opts@" -- "$cur") );;
177 stashpop) _module_comgen_words "@comp_stashpop_opts@ $(_module_stashlist)" "$cur";;
178 stashshow|stashrm)
179 _module_comgen_words "$(_module_stashlist)" "$cur";;
180 clear) COMPREPLY=( $(compgen -W "@comp_clear_opts@" -- "$cur") );;
181 restore) _module_comgen_words "@comp_restore_opts@ $(_module_savelist)" "$cur";;
182 save|saveshow|describe|saverm|disable|is-saved)
183 _module_comgen_words "$(_module_savelist)" "$cur";;
184 rm|delete|remove|unload)
185 _module_comgen_words "@comp_unload_opts@ ${LOADEDMODULES//:/ }" "$cur";;
186 switch|swap) _module_comgen_words "@comp_load_opts@ ${LOADEDMODULES//:/ }" "$cur";;
187 unuse|is-used) _module_comgen_words "${MODULEPATH//:/ }" "$cur";;
188 use) case "$cur" in
189 -*) _module_comgen_words "@comp_use_opts@" "$cur";;
190 *) ;; # let readline handle the completion
191 esac;;
192 -a|--append|cachebuild) ;; # let readline handle the completion
193 display|help|show|test|path|paths|is-loaded|info-loaded)
194 _module_comgen_words_and_files "@comp_mfile_opts@ $(_module_avail "$cur")" "$cur";;
195 is-avail)
196 _module_comgen_words_and_files "@comp_isavail_opts@ $(_module_avail "$cur")" "$cur";;
197 lint)
198 _module_comgen_words_and_files "@comp_lint_opts@ $(_module_avail "$cur")" "$cur";;
199 mod-to-sh)
200 _module_comgen_words_and_files "@comp_modtosh_opts@ $(_module_not_yet_loaded "$cur")" "$cur";;
201 whatis)
202 _module_comgen_words_and_files "@comp_whatis_opts@ $(_module_avail "$cur")" "$cur";;
203 apropos|keyword|search)
204 COMPREPLY=( $(compgen -W "@comp_search_opts@" -- "$cur") );;
205 config|--reset) COMPREPLY=( $(compgen -W "@comp_config_opts@" -- "$cur") );;
206 purge) COMPREPLY=( $(compgen -W "@comp_purge_opts@" -- "$cur") );;
207 reload|update) COMPREPLY=( $(compgen -W "@comp_reload_opts@" -- "$cur") );;
208 reset) COMPREPLY=( $(compgen -W "@comp_reset_opts@" -- "$cur") );;
209 stash) COMPREPLY=( $(compgen -W "@comp_stash_opts@" -- "$cur") );;
210 source) case "$cur" in
211 -*) COMPREPLY=( $(compgen -W "@comp_source_opts@" -- "$cur") );;
212 *) ;; # let readline handle the completion
213 esac;;
214 -h|--help|-V|--version|--dumpname|refresh|sh-to-mod|state|stashclear|cacheclear)
215 ;;
216 append-path|prepend-path)
217 COMPREPLY=( $(compgen -W "@comp_path_opts@" -- "$cur") );;
218 remove-path)
219 COMPREPLY=( $(compgen -W "@comp_rm_path_opts@" -- "$cur") );;
220 *) if test "$COMP_CWORD" -gt 2
221 then
222 _module_long_arg_list "$cur"
223 else
224 case "$cur" in
225 # The mappings below are optional abbreviations for convenience
226 ls) COMPREPLY=( "list" );; # map ls -> list
227 sw*) COMPREPLY=( "switch" );;
228
229 -*) COMPREPLY=( $(compgen -W "@comp_opts@" -- "$cur") )
230 loaded_modules=""
231 for i in ${LOADEDMODULES//:/ }; do
232 loaded_modules+="-${i} "
233 done
234 _module_comgen_words "@comp_load_opts@ $loaded_modules" "$cur";;
235 *) _module_comgen_words_and_files "@comp_load_opts@ $(_module_not_yet_loaded "$cur")" "$cur"
236 COMPREPLY=( "${COMPREPLY[@]}" $(compgen -W "@comp_opts@ @comp_cmds@" -- "$cur") )
237 loaded_modules=""
238 for i in ${LOADEDMODULES//:/ }; do
239 loaded_modules+="-${i} "
240 done
241 _module_comgen_words "@comp_load_opts@ $loaded_modules" "$cur";;
242 esac
243 fi;;
244 esac
245 }
246 complete -o default -F _ml ml
247 fi
248 complete -o default -F _module module