/collects/meta/contrib/completion/racket-completion.bash

http://github.com/gmarceau/PLT · BASH · 212 lines · 166 code · 23 blank · 23 comment · 19 complexity · 1769062984bf0e54e5db601a6d9bb6a8 MD5 · raw file

  1. # -*- mode: shell-script; sh-basic-offset: 2; indent-tabs-mode: nil -*-
  2. # ex: ts=2 sw=2 noet filetype=sh
  3. # to enable this, add the following line to ~/.bash_completion you
  4. # will need to make sure that you've enable bash completion more
  5. # generally, usually via '. /etc/bash_completion'
  6. #
  7. # source $PLTHOME/collects/meta/contrib/completion/racket-completion.bash
  8. #
  9. # Change $PLTHOME to whatever references your Racket installation
  10. # this completes only *.{rkt,ss,scm,scrbl} files unless there are
  11. # none, in which case it completes other things
  12. _smart_filedir()
  13. {
  14. COMPREPLY=()
  15. _filedir '@(rkt|rktl|ss|scm|scrbl)'
  16. if [[ ${#COMPREPLY[@]} -eq 0 ]]; then
  17. _filedir
  18. fi
  19. return 0
  20. }
  21. _find_exe()
  22. {
  23. local exename=$1
  24. local path=`dirname "${COMP_WORDS[0]}"`
  25. local old_exe=`basename "${COMP_WORDS[0]}"`
  26. if [ "${path}" = "." ]
  27. then
  28. if [ "${COMP_WORDS[0]}" = "${path}/${old_exe}" ]
  29. then
  30. echo "${path}/${exename}"
  31. else
  32. echo ${exename}
  33. fi
  34. else
  35. echo "${path}/${exename}"
  36. fi
  37. return 0
  38. }
  39. _racket()
  40. {
  41. local cur prev singleopts doubleopts
  42. COMPREPLY=()
  43. cur=`_get_cword`
  44. prev="${COMP_WORDS[COMP_CWORD-1]}"
  45. doubleopts="--help --version --eval --load --require --lib --script --require-script\
  46. --main --repl --no-lib --version --warn --syslog --collects --search --addon --no-compiled --no-init-file"
  47. singleopts="-h -e -f -t -l -p -r -u -k -m -i -n -v -W -L -X -S -A -I -U -N -j -d -b -c -q"
  48. warnlevels="none fatal error warning info debug"
  49. # if '--' is already given, complete all kind of files, but no options
  50. for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
  51. if [[ ${COMP_WORDS[i]} == -- ]]; then
  52. _smart_filedir
  53. return 0
  54. fi
  55. done
  56. # -k takes *two* integer arguments
  57. if [[ 2 < ${#COMP_WORDS[@]} ]]; then
  58. if [[ ${COMP_WORDS[COMP_CWORD-2]} == -k ]]; then
  59. return 0
  60. fi
  61. fi
  62. case "${cur}" in
  63. --*)
  64. COMPREPLY=( $(compgen -W "${doubleopts}" -- ${cur}) )
  65. ;;
  66. -*)
  67. COMPREPLY=( $(compgen -W "${singleopts}" -- ${cur}) )
  68. ;;
  69. *)
  70. case "${prev}" in
  71. # these do not take anything completable as arguments
  72. --help|-h|-e|--eval|-p|-k)
  73. ;;
  74. # these take dirs (not files) as arguments
  75. -X|-S|-A|--collects|--search|--addon)
  76. _filedir '-d'
  77. ;;
  78. # these take warnlevels as arguments
  79. -W|--warn|-L|--syslog)
  80. COMPREPLY=( $(compgen -W "${warnlevels}" -- ${cur}) )
  81. ;;
  82. # otherwise, just a file
  83. *)
  84. _smart_filedir
  85. ;;
  86. esac
  87. ;;
  88. esac
  89. return 0
  90. }
  91. complete -F _racket $filenames racket
  92. complete -F _racket $filenames gracket
  93. complete -F _racket $filenames gracket-text
  94. _raco_cmd=$(_find_exe "raco" )
  95. _raco_planet()
  96. {
  97. local cur="${COMP_WORDS[COMP_CWORD]}"
  98. local planetcmds=$( echo '' '--help' ; for x in `${_raco_cmd} planet --help 2>&1 | sed -n -e 's/^ raco planet \(.[^ ]*\).*/\1/p'` ; do echo ${x} ; done )
  99. COMPREPLY=( $(compgen -W "${planetcmds}" -- ${cur}) )
  100. }
  101. _raco_cmds=$()
  102. _racket_cmd=$(_find_exe "racket" )
  103. _raco_help()
  104. {
  105. local cur="${COMP_WORDS[COMP_CWORD]}"
  106. if [ ${#_raco_cmds[@]} -eq 0 ]; then
  107. # removing the empty string on the next line breaks things. such as my brain.
  108. _raco_cmds=$( echo '' 'help' ; for x in `${_racket_cmd} -e '(begin (require raco/all-tools) (for ([(k v) (all-tools)]) (printf "~a\n" k)))'` ; do echo ${x} ; done )
  109. fi
  110. COMPREPLY=( $(compgen -W "${_raco_cmds}" -- ${cur}) )
  111. }
  112. _racket_collects_dirs=()
  113. _complete_collects()
  114. {
  115. local cur=$1
  116. if [ ${#_racket_collects_dirs[@]} -eq 0 ]; then
  117. _racket_collects_dirs=( $( for x in `${_racket_cmd} -e '(for-each displayln (map path->string (current-library-collection-paths)))'`; do echo "${x}/" ; done ) )
  118. fi
  119. local wordlist=""
  120. for dir in ${_racket_collects_dirs[@]}; do
  121. wordlist="$wordlist "$( for x in $(compgen -d "${dir}"); do basename "${x}"; done )
  122. done
  123. COMPREPLY=( $(compgen -W "${wordlist}" -- ${cur}) )
  124. }
  125. _raco_setup()
  126. {
  127. local cur="${COMP_WORDS[COMP_CWORD]}"
  128. local prev="${COMP_WORDS[COMP_CWORD-1]}"
  129. if [ $COMP_CWORD -eq 2 ]
  130. then
  131. _complete_collects ${cur}
  132. else
  133. case "${prev}" in
  134. -l) # specifying a particular collection
  135. _complete_collects ${cur}
  136. ;;
  137. *)
  138. _filedir
  139. ;;
  140. esac
  141. fi
  142. }
  143. _raco()
  144. {
  145. COMPREPLY=()
  146. local cur="${COMP_WORDS[COMP_CWORD]}"
  147. #
  148. # Complete the arguments to some of the basic commands.
  149. #
  150. local makeopts="--disable-inline --no-deps -p --prefix --no-prim -v -vv --help -h"
  151. if [ $COMP_CWORD -eq 1 ]; then
  152. # removing the empty string on the next line breaks things. such as my brain.
  153. _raco_cmds=$( echo '' 'help' ; for x in `${_racket_cmd} -e '(begin (require raco/all-tools) (for ([(k v) (all-tools)]) (printf "~a\n" k)))'` ; do echo ${x} ; done )
  154. COMPREPLY=($(compgen -W "${_raco_cmds}" -- ${cur}))
  155. elif [ $COMP_CWORD -ge 2 ]; then
  156. # Here we'll handle the main raco commands
  157. local prev="${COMP_WORDS[1]}"
  158. case "${prev}" in
  159. make)
  160. case "${cur}" in
  161. -*)
  162. COMPREPLY=( $(compgen -W "${makeopts}" -- ${cur}) )
  163. ;;
  164. *)
  165. _filedir
  166. ;;
  167. esac
  168. ;;
  169. planet)
  170. _raco_planet
  171. ;;
  172. help)
  173. _raco_help
  174. ;;
  175. setup)
  176. _raco_setup
  177. ;;
  178. *)
  179. _filedir
  180. ;;
  181. esac
  182. else
  183. _filedir
  184. fi
  185. return 0
  186. }
  187. complete -F _raco rico
  188. complete -F _raco racket-tool
  189. complete -F _raco raco