1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
#compdef ml
# Main dispatcher
_ml()
{
local curcontext="$curcontext" state line arguments opt descript arg w optState cmd
local -A opt_args optT cmdT
arguments=(
'(-h --help)'{--help,-h}'[display usage info]'
'(- *)'{--version,-v}'[module command version]'
'(-t --terse)'{--terse,-t}'[display avail, list and spider output in short format]'
'(-d --default)'{--default,-d}'[List default modules only when used with avail]'
'(-D --debug)'{--debug,-D}'[Send program tracing information to stderr]'
'(-w --width)'{--width,-w}'[use given width for max term width]'
'(-q --quiet)'{--quiet,-q}'[disable verbose messages]'
'(-s --style)'{--style,-s}'[avail output style]'
'--mt[display the module table]'
'(-r --regexp)'{--regexp,-r}'[lua regular expression matching]'
'--ignore_cache[Treat the cache file(s) as out-of-date]'
'--Verbose[show generated module command]'
)
for arg in $arguments; do
opt=${${arg#*\)}%%\[*}
descript=${${arg#*\[}%%\]*}
optT[$opt]=$descript
done
_ml_loaded_modules_negated
_ml_available_modules
local _ml_cmds
_ml_cmds=(
"help:print the usage of each sub-command"
"unload:remove a module from the shell environment"
"rm:remove a module from the shell environment"
"swap:swap loaded a loaded module with another module"
"spider:List all possible modules"
"show:display information about a module"
"list:list loaded modules"
"load:load a module"
"try-load:load a module, no warnings if not found"
"avail:list all available modules"
"restore:load a module collection"
"use:add a directory to MODULEPATH"
"unuse:remove a directory from MODULEPATH"
"purge:unload all loaded modules"
"refresh:refresh all non-persistent components of loaded modules"
"whatis:display module information"
"keyword:search for a given keyword in modules"
"update:reload all loaded modules"
"mcc:module collection contents"
"collection:module collection contents"
"cc:module collection contents"
)
for i in $_ml_cmds; do
cmd=${i%%:*}
cmdT[$cmd]=1
done
# previous current choices pattern
# ---------------------------------------------------------
# ml -o1 <nothing> cmds 10
# A* availM
# ---------------------------------------------------------
# ml -o1 - -opts 11
# -loadM
# ---------------------------------------------------------
# ml -o1 -M1 <nothing> availM 20
# A*
# ---------------------------------------------------------
# ml -o1 -M1 - -loadM 21
# ---------------------------------------------------------
# ml -o1 M1 <nothing> availM 20
# A*
# ---------------------------------------------------------
# ml -o1 M1 - -loadM 21
# ---------------------------------------------------------
# ml -o1 cmd * cmd-specific 30
# ---------------------------------------------------------
cmd=""
pattern=10
for ((i=2; i < $#words; i++ )); do
w=$words[$i]
firstC=$w[1]
#echo "\$words[$i]: \"$w\" firstC: \"$firstC\"" >> $HOME/t/ml.log
if [ -n "$optT[$w]" ]; then
pattern=10
elif [ "$firstC" = "-" ]; then
pattern=20
break;
elif [ "$cmdT[$w]" = 1 ]; then
pattern=30
cmd=$w
break;
fi
done
w=$words[$CURRENT]
firstC=$w[1]
if [ "$firstC" = "-" ]; then
((pattern++))
fi
case $pattern in
10)
cmdsPavail=($_ml_cmds $_available_modules)
_describe -t commands 'ml commands' cmdsPavail || compadd "$@"
;;
11)
_arguments \
$arguments \
$_loaded_modules_negated
;;
20)
compadd "$@" -a -- _available_modules
;;
21)
compadd "$@" -a -- _loaded_modules_negated
;;
30|31)
case $cmd in
add|try-load) cmd="load";;
rm) cmd="unload";;
display) cmd="show";;
esac
local update_policy
curcontext="${curcontext%:*:*}:ml-${cmd}:"
zstyle -s ":completion:${curcontext}:" cache-policy update_policy
_call_function ret _ml_$cmd || _message 'no more arguments'
;;
esac
}
_ml_loaded_modules_negated()
{
_loaded_modules_negated=(${$(module -q -t list 2>&1 > /dev/null | sed ' /^ *$/d; /:$/d; s#/*$##g; s|^|-|g;')})
}
_ml_loaded_modules()
{
_loaded_modules=(${$(module -q -t list 2>&1 > /dev/null | sed ' /^ *$/d; /:$/d; s#/*$##g;')})
}
# Fills the available modules cache
_ml_available_modules()
{
if [[ -n $MODULEPATH ]]; then
_available_modules=(${$(module -q -t avail 2>&1 > /dev/null | sed ' /:$/d; s#/*$##g;')})
fi
}
# Fills the available modules cache
_ml_spider_list()
{
_spider_list=(${$(module -q -t spider 2>&1 > /dev/null)})
}
_ml_unload()
{
_ml_loaded_modules
compadd "$@" -a -- _loaded_modules
}
_ml_restore()
{
_savelist_list=(${$(module -q -t savelist 2>&1 > /dev/null)})
compadd "$@" -a -- _savelist_list
}
_ml_mcc()
{
_savelist_list=(${$(module -q -t savelist 2>&1 > /dev/null)})
compadd "$@" -a -- _savelist_list
}
_ml_collection()
{
_savelist_list=(${$(module -q -t savelist 2>&1 > /dev/null)})
compadd "$@" -a -- _savelist_list
}
_ml_cc()
{
_savelist_list=(${$(module -q -t savelist 2>&1 > /dev/null)})
compadd "$@" -a -- _savelist_list
}
_ml_help()
{
_ml_available_modules
compadd "$@" -a -- _available_modules
}
_ml_swap()
{
if [[ $words[$CURRENT-1] == "swap" ]]; then
_ml_loaded_modules
compadd "$@" -a -- _loaded_modules
else
_ml_available_modules
compadd "$@" -a -- _available_modules
fi
}
# Completion function for display|show
_ml_show()
{
_ml_available_modules
compadd "$@" -a -- _available_modules
}
# Completion function for load
_ml_load()
{
_ml_available_modules
compadd "$@" -a -- _available_modules
}
# Completion function for use
_ml_use()
{
_arguments -s \
'(-a --append)'{--append,-a}'[append the directories instead of prepending]' \
'*:directory:_files -/'
}
# Completion function for unuse
_ml_unuse()
{
compadd "$@" -- ${=MODULEPATH//:/ }
}
# Completion function for whatis
_ml_whatis()
{
_ml_available_modules
compadd "$@" -a -- _available_modules
}
_ml_spider()
{
_ml_spider_list
compadd "$@" -a -- _spider_list
}
_ml "$@"
# Local Variables:
# mode: shell-script
# indent-tabs-mode: nil
# End:
|