1*e5dd7070Spatrick# Please add "source /path/to/bash-autocomplete.sh" to your .bashrc to use this. 2*e5dd7070Spatrick 3*e5dd7070Spatrick_clang_filedir() 4*e5dd7070Spatrick{ 5*e5dd7070Spatrick # _filedir function provided by recent versions of bash-completion package is 6*e5dd7070Spatrick # better than "compgen -f" because the former honors spaces in pathnames while 7*e5dd7070Spatrick # the latter doesn't. So we use compgen only when _filedir is not provided. 8*e5dd7070Spatrick _filedir 2> /dev/null || COMPREPLY=( $( compgen -f ) ) 9*e5dd7070Spatrick} 10*e5dd7070Spatrick 11*e5dd7070Spatrick_clang() 12*e5dd7070Spatrick{ 13*e5dd7070Spatrick local cur prev words cword arg flags w1 w2 14*e5dd7070Spatrick # If latest bash-completion is not supported just initialize COMPREPLY and 15*e5dd7070Spatrick # initialize variables by setting manually. 16*e5dd7070Spatrick _init_completion -n 2> /dev/null 17*e5dd7070Spatrick if [[ "$?" != 0 ]]; then 18*e5dd7070Spatrick COMPREPLY=() 19*e5dd7070Spatrick cword=$COMP_CWORD 20*e5dd7070Spatrick cur="${COMP_WORDS[$cword]}" 21*e5dd7070Spatrick fi 22*e5dd7070Spatrick 23*e5dd7070Spatrick w1="${COMP_WORDS[$cword - 1]}" 24*e5dd7070Spatrick if [[ $cword > 1 ]]; then 25*e5dd7070Spatrick w2="${COMP_WORDS[$cword - 2]}" 26*e5dd7070Spatrick fi 27*e5dd7070Spatrick 28*e5dd7070Spatrick # Pass all the current command-line flags to clang, so that clang can handle 29*e5dd7070Spatrick # these internally. 30*e5dd7070Spatrick # '=' is separated differently by bash, so we have to concat them without ',' 31*e5dd7070Spatrick for i in `seq 1 $cword`; do 32*e5dd7070Spatrick if [[ $i == $cword || "${COMP_WORDS[$(($i+1))]}" == '=' ]]; then 33*e5dd7070Spatrick arg="$arg${COMP_WORDS[$i]}" 34*e5dd7070Spatrick else 35*e5dd7070Spatrick arg="$arg${COMP_WORDS[$i]}," 36*e5dd7070Spatrick fi 37*e5dd7070Spatrick done 38*e5dd7070Spatrick 39*e5dd7070Spatrick # expand ~ to $HOME 40*e5dd7070Spatrick eval local path=${COMP_WORDS[0]} 41*e5dd7070Spatrick # Use $'\t' so that bash expands the \t for older versions of sed. 42*e5dd7070Spatrick flags=$( "$path" --autocomplete="$arg" 2>/dev/null | sed -e $'s/\t.*//' ) 43*e5dd7070Spatrick # If clang is old that it does not support --autocomplete, 44*e5dd7070Spatrick # fall back to the filename completion. 45*e5dd7070Spatrick if [[ "$?" != 0 ]]; then 46*e5dd7070Spatrick _clang_filedir 47*e5dd7070Spatrick return 48*e5dd7070Spatrick fi 49*e5dd7070Spatrick 50*e5dd7070Spatrick # When clang does not emit any possible autocompletion, or user pushed tab after " ", 51*e5dd7070Spatrick # just autocomplete files. 52*e5dd7070Spatrick if [[ "$flags" == "$(echo -e '\n')" ]]; then 53*e5dd7070Spatrick # If -foo=<tab> and there was no possible values, autocomplete files. 54*e5dd7070Spatrick [[ "$cur" == '=' || "$cur" == -*= ]] && cur="" 55*e5dd7070Spatrick _clang_filedir 56*e5dd7070Spatrick elif [[ "$cur" == '=' ]]; then 57*e5dd7070Spatrick COMPREPLY=( $( compgen -W "$flags" -- "") ) 58*e5dd7070Spatrick else 59*e5dd7070Spatrick # Bash automatically appends a space after '=' by default. 60*e5dd7070Spatrick # Disable it so that it works nicely for options in the form of -foo=bar. 61*e5dd7070Spatrick [[ "${flags: -1}" == '=' ]] && compopt -o nospace 2> /dev/null 62*e5dd7070Spatrick COMPREPLY=( $( compgen -W "$flags" -- "$cur" ) ) 63*e5dd7070Spatrick fi 64*e5dd7070Spatrick} 65*e5dd7070Spatrickcomplete -F _clang clang 66