Debian Codegraph @

init/bash_completion.in

raw ◀ ▶
1 Debian patch touch this file
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|del|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|savelist) COMPREPLY=( $(compgen -W "@comp_list_opts@" -- "$cur") );;
99 stashlist) COMPREPLY=( $(compgen -W "@comp_stashlist_opts@" -- "$cur") );;
100 stashpop|stashshow|stashrm)
•101 _module_comgen_words "$(_module_stashlist)" "$cur";;
102 clear) COMPREPLY=( $(compgen -W "@comp_clear_opts@" -- "$cur") );;
103 restore|save|saveshow|describe|saverm|disable|is-saved)
•104 _module_comgen_words "$(_module_savelist)" "$cur";;
105 rm|del|remove|unload)
•106 _module_comgen_words "@comp_unload_opts@ ${LOADEDMODULES//:/ }" "$cur";;
107 switch|swap) _module_comgen_words "@comp_load_opts@ ${LOADEDMODULES//:/ }" "$cur";;
108 unuse|is-used) _module_comgen_words "${MODULEPATH//:/ }" "$cur";;
109 use|-a|--append|cachebuild) ;; # let readline handle the completion
110 display|help|show|test|path|paths|is-loaded|info-loaded)
111 _module_comgen_words_and_files "@comp_mfile_opts@ $(_module_avail "$cur")" "$cur";;
112 is-avail)
113 _module_comgen_words_and_files "@comp_isavail_opts@ $(_module_avail "$cur")" "$cur";;
114 lint)
115 _module_comgen_words_and_files "@comp_lint_opts@ $(_module_avail "$cur")" "$cur";;
116 mod-to-sh)
117 _module_comgen_words_and_files "@comp_modtosh_opts@ $(_module_not_yet_loaded "$cur")" "$cur";;
118 whatis)
119 _module_comgen_words_and_files "@comp_whatis_opts@ $(_module_avail "$cur")" "$cur";;
120 apropos|keyword|search)
121 COMPREPLY=( $(compgen -W "@comp_search_opts@" -- "$cur") );;
122 config|--reset) COMPREPLY=( $(compgen -W "@comp_config_opts@" -- "$cur") );;
123 -h|--help|-V|--version|--dumpname|purge|refresh|reload|sh-to-mod|source|state|reset|stash|stashclear|cacheclear|update)
124 ;;
125 append-path|prepend-path)
126 COMPREPLY=( $(compgen -W "@comp_path_opts@" -- "$cur") );;
127 remove-path)
128 COMPREPLY=( $(compgen -W "@comp_rm_path_opts@" -- "$cur") );;
129 initadd|initclear|initlist|initprepend|initrm)
130 ;;
131 *) if test "$COMP_CWORD" -gt 2
132 then
133 _module_long_arg_list "$cur"
134 else
135 case "$cur" in
136 # The mappings below are optional abbreviations for convenience
137 ls) COMPREPLY=( "list" );; # map ls -> list
138 sw*) COMPREPLY=( "switch" );;
139
140 -*) COMPREPLY=( $(compgen -W "@comp_opts@" -- "$cur") );;
141 *) COMPREPLY=( $(compgen -W "@comp_opts@ @comp_cmds@" -- "$cur") );;
142 esac
143 fi;;
144 esac
145 }
146
147 # define completion for ml command if command exists
148 if type -t ml >/dev/null; then
149 _ml() {
150 local cur="$2" prev="$3"
151
152 COMPREPLY=()
153
154 case "$prev" in
155 add|add-any|load|load-any|try-add|try-load)
156 _module_comgen_words_and_files "@comp_load_opts@ $(_module_not_yet_loaded "$cur")" "$cur";;
157 avail) _module_comgen_words_and_files "@comp_avail_opts@ $(_module_avail "$cur")" "$cur";;
158 spider) _module_comgen_words_and_files "@comp_avail_opts@ $(_module_spider "$cur")" "$cur";;
159 edit) _module_comgen_words_and_files "$(_module_avail "$cur")" "$cur";;
160 aliases) COMPREPLY=( $(compgen -W "@comp_aliases_opts@" -- "$cur") );;
161 list|savelist) COMPREPLY=( $(compgen -W "@comp_list_opts@" -- "$cur") );;
162 stashlist) COMPREPLY=( $(compgen -W "@comp_stashlist_opts@" -- "$cur") );;
163 stashpop|stashshow|stashrm)
•164 _module_comgen_words "$(_module_stashlist)" "$cur";;
165 clear) COMPREPLY=( $(compgen -W "@comp_clear_opts@" -- "$cur") );;
166 restore|save|saveshow|describe|saverm|disable|is-saved)
•167 _module_comgen_words "$(_module_savelist)" "$cur";;
168 rm|del|remove|unload)
•169 _module_comgen_words "@comp_unload_opts@ ${LOADEDMODULES//:/ }" "$cur";;
170 switch|swap) _module_comgen_words "@comp_load_opts@ ${LOADEDMODULES//:/ }" "$cur";;
171 unuse|is-used) _module_comgen_words "${MODULEPATH//:/ }" "$cur";;
172 use|-a|--append|cachebuild) ;; # let readline handle the completion
173 display|help|show|test|path|paths|is-loaded|info-loaded)
174 _module_comgen_words_and_files "@comp_mfile_opts@ $(_module_avail "$cur")" "$cur";;
175 is-avail)
176 _module_comgen_words_and_files "@comp_isavail_opts@ $(_module_avail "$cur")" "$cur";;
177 lint)
178 _module_comgen_words_and_files "@comp_lint_opts@ $(_module_avail "$cur")" "$cur";;
179 mod-to-sh)
180 _module_comgen_words_and_files "@comp_modtosh_opts@ $(_module_not_yet_loaded "$cur")" "$cur";;
181 whatis)
182 _module_comgen_words_and_files "@comp_whatis_opts@ $(_module_avail "$cur")" "$cur";;
183 apropos|keyword|search)
184 COMPREPLY=( $(compgen -W "@comp_search_opts@" -- "$cur") );;
185 config|--reset) COMPREPLY=( $(compgen -W "@comp_config_opts@" -- "$cur") );;
186 -h|--help|-V|--version|--dumpname|purge|refresh|reload|sh-to-mod|source|state|reset|stash|stashclear|cacheclear|update)
187 ;;
188 append-path|prepend-path)
189 COMPREPLY=( $(compgen -W "@comp_path_opts@" -- "$cur") );;
190 remove-path)
191 COMPREPLY=( $(compgen -W "@comp_rm_path_opts@" -- "$cur") );;
192 initadd|initclear|initlist|initprepend|initrm)
193 ;;
194 *) if test "$COMP_CWORD" -gt 2
195 then
196 _module_long_arg_list "$cur"
197 else
198 case "$cur" in
199 # The mappings below are optional abbreviations for convenience
200 ls) COMPREPLY=( "list" );; # map ls -> list
201 sw*) COMPREPLY=( "switch" );;
202
203 -*) COMPREPLY=( $(compgen -W "@comp_opts@" -- "$cur") )
204 loaded_modules=""
205 for i in ${LOADEDMODULES//:/ }; do
206 loaded_modules+="-${i} "
207 done
•208 _module_comgen_words "@comp_load_opts@ $loaded_modules" "$cur";;
209 *) _module_comgen_words_and_files "@comp_load_opts@ $(_module_not_yet_loaded "$cur")" "$cur"
210 COMPREPLY=( "${COMPREPLY[@]}" $(compgen -W "@comp_opts@ @comp_cmds@" -- "$cur") )
211 loaded_modules=""
212 for i in ${LOADEDMODULES//:/ }; do
213 loaded_modules+="-${i} "
214 done
•215 _module_comgen_words "@comp_load_opts@ $loaded_modules" "$cur";;
216 esac
217 fi;;
218 esac
219 }
220 complete -o default -F _ml ml
221 fi
222 complete -o default -F _module module