You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

140 lines
3.2 KiB

  1. # GNU Guix --- Functional package management for GNU
  2. # Copyright © 2015 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. _guix_complete_available_package ()
  20. {
  21. local prefix="$1"
  22. local packages="$(${COMP_WORDS[0]} package -A "^$prefix" | cut -f1)"
  23. COMPREPLY=($(compgen -W "$packages" -- "$prefix"))
  24. }
  25. _guix_complete_installed_package ()
  26. {
  27. local prefix="$1"
  28. local packages="$(${COMP_WORDS[0]} package -I "^$prefix" | cut -f1)"
  29. COMPREPLY=($(compgen -W "$packages" -- "$prefix"))
  30. }
  31. _guix_complete_option ()
  32. {
  33. local options="$(${COMP_WORDS[0]} ${COMP_WORDS[1]} --help \
  34. | grep '^ -' \
  35. | sed -e's/^.*--\([a-zA-Z0-9_-]\+\)\(=\?\).*/--\1\2/g' )"
  36. compopt -o nospace
  37. COMPREPLY=($(compgen -W "$options" -- "${COMP_WORDS[$word_count - 1]}"))
  38. }
  39. _guix_is_command ()
  40. {
  41. local word
  42. local result="false"
  43. for word in ${COMP_WORDS[*]}
  44. do
  45. if [ "$word" = "$1" ]
  46. then
  47. result=true
  48. break
  49. fi
  50. done
  51. $result
  52. }
  53. _guix_is_removing ()
  54. {
  55. local word
  56. local result="false"
  57. for word in ${COMP_WORDS[*]}
  58. do
  59. case "$word" in
  60. --remove|--remove=*|-r)
  61. result=true
  62. break
  63. ;;
  64. esac
  65. done
  66. $result
  67. }
  68. _guix_is_dash_L ()
  69. {
  70. [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-L" ] \
  71. || { case "${COMP_WORDS[$COMP_CWORD]}" in
  72. --load-path=*) true;;
  73. *) false;;
  74. esac }
  75. }
  76. _guix_complete_file ()
  77. {
  78. # Let Readline complete file names.
  79. compopt -o default
  80. COMPREPLY=()
  81. }
  82. _guix_complete ()
  83. {
  84. local word_count=${#COMP_WORDS[*]}
  85. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  86. if [ "$COMP_CWORD" -gt 1 ]
  87. then
  88. case "$word_at_point" in
  89. -*)
  90. _guix_complete_option "$word_at_point"
  91. return
  92. ;;
  93. esac
  94. fi
  95. case $COMP_CWORD in
  96. 1)
  97. local subcommands="$(guix --help | grep '^ ' | cut -c 2-)"
  98. COMPREPLY=($(compgen -W "$subcommands" -- "$word_at_point"))
  99. ;;
  100. *)
  101. if _guix_is_command "package"
  102. then
  103. if _guix_is_dash_L
  104. then
  105. _guix_complete_file
  106. elif _guix_is_removing
  107. then
  108. _guix_complete_installed_package "$word_at_point"
  109. else
  110. _guix_complete_available_package "$word_at_point"
  111. fi
  112. elif _guix_is_command "system"
  113. then
  114. _guix_complete_file # TODO: complete sub-commands
  115. elif _guix_is_command "hash"
  116. then
  117. _guix_complete_file
  118. elif _guix_is_command "import" # TODO: complete sub-commands
  119. then
  120. _guix_complete_file
  121. else
  122. _guix_complete_available_package "$word_at_point"
  123. fi
  124. ;;
  125. esac
  126. }
  127. complete -F _guix_complete guix