lmms 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #!/usr/bin/env bash
  2. # lmms(1) completion -*- shell-script -*-
  3. # use shellcheck: "shellcheck -e bash <filename>"
  4. _lmms_array_contains ()
  5. {
  6. local e match="$1"
  7. shift
  8. for e; do [[ "$e" == "$match" ]] && return 0; done
  9. return 1
  10. }
  11. _lmms_long_param_of()
  12. {
  13. case "$1" in
  14. -a)
  15. echo "float"
  16. ;;
  17. -b)
  18. echo "bitrate"
  19. ;;
  20. -c)
  21. echo "config"
  22. ;;
  23. -f)
  24. echo "format"
  25. ;;
  26. -i)
  27. echo "interpolation"
  28. ;;
  29. -l)
  30. echo "loop"
  31. ;;
  32. -m)
  33. echo "mode"
  34. ;;
  35. -o)
  36. echo "output"
  37. ;;
  38. -p)
  39. echo "profile"
  40. ;;
  41. -s)
  42. echo "samplerate"
  43. ;;
  44. -x)
  45. echo "oversampling"
  46. ;;
  47. *)
  48. echo ""
  49. ;;
  50. esac
  51. }
  52. _lmms_conv_old_action ()
  53. {
  54. case "$1" in
  55. -d|--dump)
  56. echo "dump"
  57. ;;
  58. -r|--render)
  59. echo "render"
  60. ;;
  61. --rendertracks)
  62. echo "rendertracks"
  63. ;;
  64. -u|--upgrade)
  65. echo "upgrade"
  66. ;;
  67. *)
  68. echo ""
  69. ;;
  70. esac
  71. }
  72. _lmms()
  73. {
  74. local cword=$COMP_CWORD
  75. local cur="${COMP_WORDS[COMP_CWORD]}"
  76. # call routine provided by bash-completion
  77. _init_completion || return
  78. local params filemode filetypes
  79. local i # counter variable
  80. local pars_global pars_noaction pars_render actions shortargs
  81. pars_global=(--allowroot --config --help --version)
  82. pars_noaction=(--geometry --import)
  83. pars_render=(--float --bitrate --format --interpolation)
  84. pars_render+=(--loop --mode --output --profile)
  85. pars_render+=(--samplerate --oversampling)
  86. actions=(dump compress render rendertracks upgrade makebundle)
  87. actions_old=(-d --dump -r --render --rendertracks -u --upgrade)
  88. shortargs+=(-a -b -c -f -h -i -l -m -o -p -s -v -x)
  89. local prev prev2
  90. if [ "$cword" -gt 1 ]
  91. then
  92. prev=${COMP_WORDS[cword-1]}
  93. fi
  94. if [ "$cword" -gt 2 ]
  95. then
  96. prev2=${COMP_WORDS[cword-2]}
  97. fi
  98. # don't show shortargs, but complete them when entered
  99. if [[ $cur =~ ^-[^-]$ ]]
  100. then
  101. if _lmms_array_contains "$cur" "${shortargs[@]}"
  102. then
  103. COMPREPLY=( "$cur" )
  104. fi
  105. return
  106. fi
  107. #
  108. # please keep those in order like def_pars_args above
  109. #
  110. case $prev in
  111. --bitrate|-b)
  112. params="64 96 128 160 192 256 320"
  113. ;;
  114. --config|-c)
  115. filetypes='xml'
  116. filemode='existing_files'
  117. ;;
  118. --format|-f)
  119. params='wav ogg mp3'
  120. ;;
  121. --geometry)
  122. # we can not name all possibilities, but this helps the user
  123. # by showing them how the format is
  124. params='0x0+0+0'
  125. ;;
  126. --interpolation|-i)
  127. params='linear sincfastest sincmedium sincbest'
  128. ;;
  129. --import)
  130. filetypes='mid|midi|MID|MIDI|rmi|RMI|h2song|H2SONG'
  131. filemode='existing_files'
  132. ;;
  133. --mode|-m)
  134. params='s j m'
  135. ;;
  136. --output|-o)
  137. # default assumption: could be both
  138. local render=1 rendertracks=1
  139. for i in "${!COMP_WORDS[@]}"
  140. do
  141. if [[ ${COMP_WORDS[i]} =~ ^(render|-r|--render)$ ]]
  142. then
  143. rendertracks=
  144. elif [[ ${COMP_WORDS[i]} =~ ^(rendertracks|--rendertracks)$ ]]
  145. then
  146. render=
  147. fi
  148. done
  149. if [ "$rendertracks" ]
  150. then
  151. filemode='existing_directories'
  152. fi
  153. if [ "$render" ]
  154. then
  155. # filemode files is a superset of "existing directories"
  156. # so it's OK to overwrite the filemode='existing_directories'
  157. # from above
  158. filetypes='wav|ogg|mp3'
  159. filemode='files'
  160. fi
  161. ;;
  162. --profile|-p)
  163. filemode='files'
  164. ;;
  165. --samplerate|-s)
  166. # these are the ones suggested for zyn
  167. # if you think more are required,
  168. # remove this comment and write a justification
  169. params='44100 48000 96000 192000'
  170. ;;
  171. --oversampling|-x)
  172. params='1 2 4 8'
  173. ;;
  174. *)
  175. local action_found
  176. # Is an action specified?
  177. if [ "$cword" -gt 1 ]
  178. then
  179. local wrd
  180. for wrd in "${COMP_WORDS[@]}"
  181. do
  182. # action named explicitly?
  183. if _lmms_array_contains "$wrd" "${actions[@]}"
  184. then
  185. action_found=$wrd
  186. break
  187. # deprecated action name?
  188. elif _lmms_array_contains "$wrd" "${actions_old[@]}"
  189. then
  190. action_found="$(_lmms_conv_old_action "$wrd")"
  191. break
  192. # no-action params found?
  193. elif _lmms_array_contains "$wrd" "${pars_noaction[@]}"
  194. then
  195. action_found=none
  196. break
  197. fi
  198. done
  199. fi
  200. if [[ $prev =~ -e|--help|-h|-version|-v ]]
  201. then
  202. # the -e flag (from --import) and help/version
  203. # always mark the end of arguments
  204. return
  205. fi
  206. if [[ "$action_found" =~ dump|none|^$ ]] && [[ $prev =~ \.mmpz? ]]
  207. then
  208. # mmp(z) mark the end of arguments for those actions
  209. return
  210. fi
  211. local savefiletypes='mmpz|mmp'
  212. local params_array
  213. # find parameters/filetypes/dirtypes depending on actions
  214. if ! [ "$action_found" ]
  215. then
  216. params_array=( "${actions[@]}" "${pars_global[@]}" "${pars_noaction[@]}")
  217. filemode="existing_files"
  218. filetypes="$savefiletypes"
  219. elif [ "$action_found" == "none" ]
  220. then
  221. params_array=( "${pars_noaction[@]}" )
  222. filemode="existing_files"
  223. filetypes="$savefiletypes"
  224. elif [ "$action_found" == "dump" ]
  225. then
  226. filemode="existing_files"
  227. filetypes="mmpz"
  228. elif [ "$action_found" == "upgrade" ]
  229. then
  230. if [ "$prev" == "upgrade" ]
  231. then
  232. filemode="existing_files"
  233. filetypes="$savefiletypes"
  234. elif [ "$prev2" == "upgrade" ]
  235. then
  236. filemode="files"
  237. filetypes="$savefiletypes"
  238. fi
  239. elif [ "$action_found" == "makebundle" ]
  240. then
  241. if [ "$prev" == "makebundle" ]
  242. then
  243. filemode="existing_files"
  244. filetypes="$savefiletypes"
  245. elif [ "$prev2" == "makebundle" ]
  246. then
  247. filemode="files"
  248. filetypes="$savefiletypes"
  249. fi
  250. elif [[ "$action_found" =~ render(tracks)? ]]
  251. then
  252. if [[ "$prev" =~ render(tracks)? ]]
  253. then
  254. filemode="existing_files"
  255. filetypes="$savefiletypes"
  256. else
  257. params_array=( "${pars_render[@]}" )
  258. fi
  259. fi
  260. # add params_array to params, but also check the history of comp words
  261. local param
  262. for param in "${params_array[@]}"
  263. do
  264. local do_append=1
  265. for i in "${!COMP_WORDS[@]}"
  266. do
  267. if [ "$i" -ne 0 ] && [ "$i" -ne "$cword" ]
  268. then
  269. # disallow double long parameters
  270. if [ "${COMP_WORDS[$i]}" == "$param" ]
  271. then
  272. do_append=
  273. # disallow double short parameters
  274. elif [ "--$(_lmms_long_param_of "${COMP_WORDS[$i]}")" == "$param" ]
  275. then
  276. do_append=
  277. # --help or --version must be the first parameters
  278. elif [ "$cword" -gt 1 ] && [[ $param =~ --help|--version ]]
  279. then
  280. do_append=
  281. fi
  282. fi
  283. done
  284. if [ "$do_append" ]
  285. then
  286. params+="$param "
  287. fi
  288. done
  289. ;;
  290. esac
  291. case $filemode in
  292. # use completion routine provided by bash-completion
  293. # to fill $COMPREPLY
  294. existing_files)
  295. _filedir "@($filetypes)"
  296. ;;
  297. existing_directories)
  298. _filedir -d
  299. ;;
  300. files)
  301. # non existing files complete like directories...
  302. _filedir -d
  303. # ...except for non-completing files with the right file type
  304. if [ ${#COMPREPLY[@]} -eq 0 ]
  305. then
  306. if ! [[ "$cur" =~ /$ ]] && [ "$filetypes" ] && [[ "$cur" =~ \.($filetypes)$ ]]
  307. then
  308. # file ending fits, we seem to be done
  309. COMPREPLY=( "$cur" )
  310. fi
  311. fi
  312. ;;
  313. esac
  314. if [ "$params" ]
  315. then
  316. # none of our parameters contain spaces, so deactivate shellcheck's warning
  317. # shellcheck disable=SC2207
  318. COMPREPLY+=( $(compgen -W "${params}" -- "${cur}") )
  319. fi
  320. }
  321. complete -F _lmms lmms