guix 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. # GNU Guix --- Functional package management for GNU
  2. # Copyright © 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. #
  4. # This file is part of GNU Guix.
  5. #
  6. # GNU Guix is free software; you can redistribute it and/or modify it
  7. # under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or (at
  9. # your option) any later version.
  10. #
  11. # GNU Guix is distributed in the hope that it will be useful, but
  12. # WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. # Bash completion for Guix commands.
  19. declare _guix_available_packages
  20. declare _guix_commands
  21. _guix_complete_command ()
  22. {
  23. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  24. if [ -z "$_guix_commands" ]
  25. then
  26. # Cache the list of commands to speed things up.
  27. _guix_commands="$(guix --help 2> /dev/null \
  28. | grep '^ ' \
  29. | sed '-es/^ *\([a-z-]\+\).*$/\1/g')"
  30. fi
  31. COMPREPLY=($(compgen -W "$_guix_commands" -- "$word_at_point"))
  32. }
  33. _guix_complete_subcommand ()
  34. {
  35. local command="${COMP_WORDS[1]}"
  36. local subcommands="$(${COMP_WORDS[0]} $command --help 2> /dev/null \
  37. | grep '^ [a-z]' \
  38. | sed -e's/^ \+\([a-z-]\+\).*$/\1/g')"
  39. COMPREPLY=($(compgen -W "$subcommands" -- "${COMP_WORDS[$COMP_CWORD]}"))
  40. }
  41. _guix_complete_available_package ()
  42. {
  43. local prefix="$1"
  44. if [ -z "$_guix_available_packages" ]
  45. then
  46. # Cache the complete list because it rarely changes and makes
  47. # completion much faster.
  48. _guix_available_packages="$(${COMP_WORDS[0]} package -A 2> /dev/null \
  49. | cut -f1)"
  50. fi
  51. COMPREPLY=($(compgen -W "$_guix_available_packages" -- "$prefix"))
  52. }
  53. _guix_complete_installed_package ()
  54. {
  55. # Here we do not cache the list of installed packages because that
  56. # may change over time and the list is relatively small anyway.
  57. local prefix="$1"
  58. local packages="$(${COMP_WORDS[0]} package -I "^$prefix" 2> /dev/null \
  59. | cut -f1)"
  60. COMPREPLY=($(compgen -W "$packages" -- "$prefix"))
  61. }
  62. _guix_complete_option ()
  63. {
  64. local subcommand
  65. case "${COMP_WORDS[2]}" in
  66. -*) subcommand="";;
  67. [a-z]*) subcommand="${COMP_WORDS[2]}";;
  68. esac
  69. local options="$(${COMP_WORDS[0]} ${COMP_WORDS[1]} $subcommand --help 2> /dev/null \
  70. | grep '^ \+-' \
  71. | sed -e's/^.*--\([a-zA-Z0-9_-]\+\)\(=\?\).*/--\1\2/g')"
  72. compopt -o nospace
  73. COMPREPLY=($(compgen -W "$options" -- "${COMP_WORDS[${#COMP_WORDS[*]} - 1]}"))
  74. }
  75. _guix_is_command ()
  76. {
  77. local word
  78. local result="false"
  79. for word in ${COMP_WORDS[*]}
  80. do
  81. if [ "$word" = "$1" ]
  82. then
  83. result=true
  84. break
  85. fi
  86. done
  87. $result
  88. }
  89. _guix_is_removing ()
  90. {
  91. local word
  92. local result="false"
  93. for word in ${COMP_WORDS[*]}
  94. do
  95. case "$word" in
  96. --remove|--remove=*|-r)
  97. result=true
  98. break
  99. ;;
  100. esac
  101. done
  102. $result
  103. }
  104. _guix_is_dash_f ()
  105. {
  106. [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-f" ] \
  107. || { case "${COMP_WORDS[$COMP_CWORD]}" in
  108. --file=*|--install-from-file=*) true;;
  109. *) false;;
  110. esac }
  111. }
  112. _guix_is_dash_l ()
  113. {
  114. [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-l" ] \
  115. || { case "${COMP_WORDS[$COMP_CWORD]}" in
  116. --load=*) true;;
  117. *) false;;
  118. esac }
  119. }
  120. _guix_is_dash_L ()
  121. {
  122. [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-L" ] \
  123. || { case "${COMP_WORDS[$COMP_CWORD]}" in
  124. --load-path=*) true;;
  125. *) false;;
  126. esac }
  127. }
  128. _guix_is_dash_m ()
  129. {
  130. [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-m" ] \
  131. || { case "${COMP_WORDS[$COMP_CWORD]}" in
  132. --manifest=*) true;;
  133. *) false;;
  134. esac }
  135. }
  136. _guix_is_dash_C ()
  137. {
  138. [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-C" ] \
  139. || { case "${COMP_WORDS[$COMP_CWORD]}" in
  140. --channels=*) true;;
  141. *) false;;
  142. esac }
  143. }
  144. _guix_is_dash_p ()
  145. {
  146. [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-p" ] \
  147. || { case "${COMP_WORDS[$COMP_CWORD]}" in
  148. --profile=*) true;;
  149. *) false;;
  150. esac }
  151. }
  152. _guix_complete_file ()
  153. {
  154. # Let Readline complete file names.
  155. compopt -o default
  156. COMPREPLY=()
  157. }
  158. _guix_complete_pid ()
  159. {
  160. local pids="$(cd /proc; echo [0-9]*)"
  161. COMPREPLY=($(compgen -W "$pids" -- "$1"))
  162. }
  163. _guix_complete ()
  164. {
  165. local word_count=${#COMP_WORDS[*]}
  166. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  167. if [ "$COMP_CWORD" -gt 1 ]
  168. then
  169. case "$word_at_point" in
  170. -*)
  171. _guix_complete_option "$word_at_point"
  172. return
  173. ;;
  174. esac
  175. fi
  176. case $COMP_CWORD in
  177. 1)
  178. _guix_complete_command
  179. ;;
  180. *)
  181. if _guix_is_command "package"
  182. then
  183. if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p || _guix_is_dash_f
  184. then
  185. _guix_complete_file
  186. elif _guix_is_removing
  187. then
  188. _guix_complete_installed_package "$word_at_point"
  189. else
  190. _guix_complete_available_package "$word_at_point"
  191. fi
  192. elif _guix_is_command "install"
  193. then
  194. if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p
  195. then
  196. _guix_complete_file
  197. else
  198. _guix_complete_available_package "$word_at_point"
  199. fi
  200. elif _guix_is_command "remove"
  201. then
  202. if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p
  203. then
  204. _guix_complete_file
  205. else
  206. _guix_complete_installed_package "$word_at_point"
  207. fi
  208. elif _guix_is_command "upgrade"
  209. then
  210. if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p
  211. then
  212. _guix_complete_file
  213. else
  214. _guix_complete_installed_package "$word_at_point"
  215. fi
  216. elif _guix_is_command "build"
  217. then
  218. if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_f
  219. then
  220. _guix_complete_file
  221. else
  222. _guix_complete_available_package "$word_at_point"
  223. fi
  224. elif _guix_is_command "environment"
  225. then
  226. if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p || _guix_is_dash_l
  227. then
  228. _guix_complete_file
  229. else
  230. _guix_complete_available_package "$word_at_point"
  231. fi
  232. elif _guix_is_command "download"
  233. then
  234. _guix_complete_file
  235. elif _guix_is_command "system"
  236. then
  237. case $COMP_CWORD in
  238. 2) _guix_complete_subcommand;;
  239. *) _guix_complete_file;; # TODO: restrict to *.scm
  240. esac
  241. elif _guix_is_command "pull"
  242. then
  243. if _guix_is_dash_C || _guix_is_dash_p
  244. then
  245. _guix_complete_file
  246. fi
  247. elif _guix_is_command "time-machine"
  248. then
  249. if _guix_is_dash_C
  250. then
  251. _guix_complete_file
  252. else
  253. _guix_complete_command
  254. fi
  255. elif _guix_is_command "container"
  256. then
  257. case $COMP_CWORD in
  258. 2) _guix_complete_subcommand;;
  259. 3) _guix_complete_pid "$word_at_point";;
  260. *) _guix_complete_file;;
  261. esac
  262. elif _guix_is_command "import"
  263. then
  264. _guix_complete_subcommand
  265. elif _guix_is_command "hash" || _guix_is_command "gc"
  266. then
  267. _guix_complete_file
  268. elif _guix_is_command "weather"
  269. then
  270. if _guix_is_dash_m
  271. then
  272. _guix_complete_file
  273. fi
  274. else
  275. _guix_complete_available_package "$word_at_point"
  276. fi
  277. ;;
  278. esac
  279. }
  280. complete -F _guix_complete guix