guix 8.8 KB

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