guix 8.5 KB

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