guix 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. # GNU Guix --- Functional package management for GNU
  2. # Copyright © 2015-2022 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="$(${COMP_WORDS[0]} --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 [ $1 -eq 0 ]
  68. then
  69. command=""
  70. subcommand=""
  71. elif _guix_is_option "$subcommand"
  72. then
  73. subcommand=""
  74. fi
  75. local options="$(${COMP_WORDS[0]} $command $subcommand --help 2> /dev/null \
  76. | grep '^ \+-' \
  77. | sed -e's/^.*--\([a-zA-Z0-9_-]\+\)\(=\?\).*/--\1\2/g')"
  78. compopt -o nospace
  79. COMPREPLY+=($(compgen -W "$options" -- "$2"))
  80. }
  81. _guix_is_option ()
  82. {
  83. case "$1" in
  84. -*)
  85. true
  86. ;;
  87. *)
  88. false
  89. ;;
  90. esac
  91. }
  92. _guix_is_removing ()
  93. {
  94. local word
  95. local result="false"
  96. for word in ${COMP_WORDS[*]}
  97. do
  98. case "$word" in
  99. --remove|--remove=*|-r)
  100. result=true
  101. break
  102. ;;
  103. esac
  104. done
  105. $result
  106. }
  107. _guix_is_dash_f ()
  108. {
  109. [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-f" ] \
  110. || { case "${COMP_WORDS[$COMP_CWORD]}" in
  111. --file=*|--install-from-file=*) true;;
  112. *) false;;
  113. esac }
  114. }
  115. _guix_is_dash_l ()
  116. {
  117. [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-l" ] \
  118. || { case "${COMP_WORDS[$COMP_CWORD]}" in
  119. --load=*) true;;
  120. *) false;;
  121. esac }
  122. }
  123. _guix_is_dash_L ()
  124. {
  125. [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-L" ] \
  126. || { case "${COMP_WORDS[$COMP_CWORD]}" in
  127. --load-path=*) true;;
  128. *) false;;
  129. esac }
  130. }
  131. _guix_is_dash_m ()
  132. {
  133. [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-m" ] \
  134. || { case "${COMP_WORDS[$COMP_CWORD]}" in
  135. --manifest=*) true;;
  136. *) false;;
  137. esac }
  138. }
  139. _guix_is_dash_C ()
  140. {
  141. [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-C" ] \
  142. || { case "${COMP_WORDS[$COMP_CWORD]}" in
  143. --channels=*) true;;
  144. *) false;;
  145. esac }
  146. }
  147. _guix_is_dash_p ()
  148. {
  149. [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-p" ] \
  150. || { case "${COMP_WORDS[$COMP_CWORD]}" in
  151. --profile=*) true;;
  152. *) false;;
  153. esac }
  154. }
  155. _guix_complete_file ()
  156. {
  157. # Let Readline complete file names.
  158. compopt -o default
  159. COMPREPLY=()
  160. }
  161. _guix_complete_available_package_or_store_file ()
  162. {
  163. _guix_complete_available_package "$@"
  164. # The current _guix_complete_file implementation doesn't compose (append to
  165. # COMPREPLY), so we suggest file names only if no package names matched.
  166. if [[ -z "$COMPREPLY" ]]
  167. then
  168. _guix_complete_file # TODO: restrict to store files
  169. fi
  170. }
  171. _guix_complete_pid ()
  172. {
  173. local pids="$(cd /proc; echo [0-9]*)"
  174. COMPREPLY+=($(compgen -W "$pids" -- "$1"))
  175. }
  176. _guix_complete ()
  177. {
  178. local word_count=${#COMP_WORDS[*]}
  179. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  180. # Find the innermost command at point, e.g. "build" in the case of
  181. # "guix time-machine OPTIONS -- build<Tab>" -- but "time-machine" if
  182. # point is moved before "build".
  183. local command_index=0
  184. local command
  185. local word_index=0
  186. local word
  187. local expect_command="true"
  188. while [[ $((++word_index)) -le COMP_CWORD ]]
  189. do
  190. word="${COMP_WORDS[$word_index]}"
  191. if $expect_command
  192. then
  193. command_index=$word_index
  194. command="$word"
  195. expect_command="false"
  196. continue
  197. fi
  198. if [[ "$word" = "--" ]]
  199. then
  200. case "$command" in
  201. environment|shell)
  202. break
  203. ;;
  204. time-machine)
  205. expect_command="true"
  206. ;;
  207. esac
  208. fi
  209. done
  210. case $COMP_CWORD in
  211. $command_index)
  212. _guix_complete_command
  213. _guix_complete_option 0 "$word_at_point"
  214. ;;
  215. *)
  216. if [[ "$command" = "package" ]]
  217. then
  218. if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p || _guix_is_dash_f
  219. then
  220. _guix_complete_file
  221. elif _guix_is_removing
  222. then
  223. _guix_complete_installed_package "$word_at_point"
  224. else
  225. _guix_complete_available_package "$word_at_point"
  226. fi
  227. elif [[ "$command" = "install" ]]
  228. then
  229. if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p
  230. then
  231. _guix_complete_file
  232. else
  233. _guix_complete_available_package "$word_at_point"
  234. fi
  235. elif [[ "$command" = "upgrade" || "$command" = "remove" ]]
  236. then
  237. if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p
  238. then
  239. _guix_complete_file
  240. else
  241. _guix_complete_installed_package "$word_at_point"
  242. fi
  243. elif [[ "$command" = "build" ]]
  244. then
  245. if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_f
  246. then
  247. _guix_complete_file
  248. else
  249. _guix_complete_available_package_or_store_file "$word_at_point"
  250. fi
  251. elif [[ "$command" = "environment" || "$command" = "shell" ]]
  252. then
  253. if _guix_is_dash_f && [[ "$command" = "shell" ]]
  254. then
  255. # The otherwise identical ‘guix environment’ lacks the ‘-f’ option.
  256. _guix_complete_file
  257. elif _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p || _guix_is_dash_l
  258. then
  259. _guix_complete_file
  260. elif _guix_is_option "$word_at_point"
  261. then
  262. _guix_complete_option "$command_index" "$word_at_point"
  263. else
  264. _guix_complete_available_package "$word_at_point"
  265. fi
  266. elif [[ "$command" = "download" || "$command" = "gc" || "$command" = "hash" ]]
  267. then
  268. _guix_complete_file
  269. elif [[ "$command" = "size" ]]
  270. then
  271. _guix_complete_available_package_or_store_file "$word_at_point"
  272. elif [[ "$command" = "system" || "$command" = "home" ]]
  273. then
  274. case $((COMP_CWORD - command_index)) in
  275. 1) _guix_complete_subcommand;;
  276. *) _guix_complete_file;; # TODO: restrict to *.scm
  277. esac
  278. elif [[ "$command" = "pull" ]]
  279. then
  280. if _guix_is_dash_C || _guix_is_dash_p
  281. then
  282. _guix_complete_file
  283. fi
  284. elif [[ "$command" = "time-machine" ]]
  285. then
  286. if _guix_is_dash_C
  287. then
  288. _guix_complete_file
  289. else
  290. _guix_complete_option "$command_index" "$word_at_point"
  291. fi
  292. elif [[ "$command" = "container" ]]
  293. then
  294. case $((COMP_CWORD - command_index)) in
  295. 1) _guix_complete_subcommand;;
  296. 2) _guix_complete_pid "$word_at_point";;
  297. *) _guix_complete_file;;
  298. esac
  299. elif [[ "$command" = "import" ]]
  300. then
  301. _guix_complete_subcommand
  302. elif [[ "$command" = "weather" ]]
  303. then
  304. if _guix_is_dash_m
  305. then
  306. _guix_complete_file
  307. else
  308. _guix_complete_available_package "$word_at_point"
  309. fi
  310. else
  311. _guix_complete_available_package "$word_at_point"
  312. fi
  313. ;;
  314. esac
  315. if [[ -z "$COMPREPLY" && COMP_CWORD -gt command_index ]] &&
  316. _guix_is_option "$word_at_point"
  317. then
  318. _guix_complete_option "$command_index" "$word_at_point"
  319. fi
  320. }
  321. complete -F _guix_complete guix