2017-05-24 02:39:08 +08:00
|
|
|
# Please add "source /path/to/bash-autocomplete.sh" to your .bashrc to use this.
|
2017-07-02 02:32:55 +08:00
|
|
|
|
|
|
|
_clang_filedir()
|
|
|
|
{
|
|
|
|
# _filedir function provided by recent versions of bash-completion package is
|
|
|
|
# better than "compgen -f" because the former honors spaces in pathnames while
|
|
|
|
# the latter doesn't. So we use compgen only when _filedir is not provided.
|
|
|
|
_filedir 2> /dev/null || COMPREPLY=( $( compgen -f ) )
|
|
|
|
}
|
|
|
|
|
2017-05-24 02:39:08 +08:00
|
|
|
_clang()
|
|
|
|
{
|
2017-07-02 02:32:55 +08:00
|
|
|
local cur prev words cword arg flags w1 w2
|
|
|
|
# If latest bash-completion is not supported just initialize COMPREPLY and
|
|
|
|
# initialize variables by setting manualy.
|
|
|
|
_init_completion -n 2> /dev/null
|
|
|
|
if [[ "$?" != 0 ]]; then
|
|
|
|
COMPREPLY=()
|
|
|
|
cword=$COMP_CWORD
|
|
|
|
cur="${COMP_WORDS[$cword]}"
|
|
|
|
fi
|
2017-05-24 02:39:08 +08:00
|
|
|
|
2017-07-02 02:32:55 +08:00
|
|
|
w1="${COMP_WORDS[$cword - 1]}"
|
|
|
|
if [[ $cword > 1 ]]; then
|
|
|
|
w2="${COMP_WORDS[$cword - 2]}"
|
2017-07-15 17:09:51 +08:00
|
|
|
fi
|
|
|
|
|
2017-07-09 01:48:59 +08:00
|
|
|
# Clang want to know if -cc1 or -Xclang option is specified or not, because we don't want to show
|
|
|
|
# cc1 options otherwise.
|
|
|
|
if [[ "${COMP_WORDS[1]}" == "-cc1" || "$w1" == "-Xclang" ]]; then
|
|
|
|
arg="#"
|
2017-07-02 02:32:55 +08:00
|
|
|
fi
|
2017-07-15 17:09:51 +08:00
|
|
|
|
|
|
|
# bash always separates '=' as a token even if there's no space before/after '='.
|
|
|
|
# On the other hand, '=' is just a regular character for clang options that
|
|
|
|
# contain '='. For example, "-stdlib=" is defined as is, instead of "-stdlib" and "=".
|
|
|
|
# So, we need to partially undo bash tokenization here for integrity.
|
2017-06-21 00:31:31 +08:00
|
|
|
if [[ "$cur" == -* ]]; then
|
|
|
|
# -foo<tab>
|
2017-07-09 01:48:59 +08:00
|
|
|
arg="$arg$cur"
|
2017-06-21 00:31:31 +08:00
|
|
|
elif [[ "$w1" == -* && "$cur" == '=' ]]; then
|
|
|
|
# -foo=<tab>
|
2017-07-09 01:48:59 +08:00
|
|
|
arg="$arg$w1=,"
|
2017-07-09 01:34:02 +08:00
|
|
|
elif [[ "$cur" == -*= ]]; then
|
|
|
|
# -foo=<tab>
|
2017-07-09 01:48:59 +08:00
|
|
|
arg="$arg$cur,"
|
2017-06-21 00:31:31 +08:00
|
|
|
elif [[ "$w1" == -* ]]; then
|
|
|
|
# -foo <tab> or -foo bar<tab>
|
2017-07-09 01:48:59 +08:00
|
|
|
arg="$arg$w1,$cur"
|
2017-06-21 00:31:31 +08:00
|
|
|
elif [[ "$w2" == -* && "$w1" == '=' ]]; then
|
|
|
|
# -foo=bar<tab>
|
2017-07-09 01:48:59 +08:00
|
|
|
arg="$arg$w2=,$cur"
|
2017-07-09 01:34:02 +08:00
|
|
|
elif [[ ${cur: -1} != '=' && ${cur/=} != $cur ]]; then
|
|
|
|
# -foo=bar<tab>
|
2017-07-09 01:48:59 +08:00
|
|
|
arg="$arg${cur%=*}=,${cur#*=}"
|
2017-06-21 00:31:31 +08:00
|
|
|
fi
|
|
|
|
|
2017-07-02 00:30:02 +08:00
|
|
|
# expand ~ to $HOME
|
|
|
|
eval local path=${COMP_WORDS[0]}
|
2017-07-26 21:36:58 +08:00
|
|
|
flags=$( "$path" --autocomplete="$arg" 2>/dev/null | sed -e 's/\t.*//' )
|
2017-06-28 23:59:55 +08:00
|
|
|
# If clang is old that it does not support --autocomplete,
|
|
|
|
# fall back to the filename completion.
|
|
|
|
if [[ "$?" != 0 ]]; then
|
2017-07-02 02:32:55 +08:00
|
|
|
_clang_filedir
|
2017-06-28 23:59:55 +08:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2017-07-26 21:30:36 +08:00
|
|
|
# When clang does not emit any possible autocompletion, or user pushed tab after " ",
|
|
|
|
# just autocomplete files.
|
|
|
|
if [[ "$flags" == "$(echo -e '\n')" || "$arg" == "" ]]; then
|
|
|
|
# If -foo=<tab> and there was no possible values, autocomplete files.
|
|
|
|
[[ "$cur" == '=' || "$cur" == -*= ]] && cur=""
|
2017-07-02 02:32:55 +08:00
|
|
|
_clang_filedir
|
2017-07-26 21:30:36 +08:00
|
|
|
elif [[ "$cur" == '=' ]]; then
|
|
|
|
COMPREPLY=( $( compgen -W "$flags" -- "") )
|
2017-05-24 02:39:08 +08:00
|
|
|
else
|
2017-06-26 08:35:36 +08:00
|
|
|
# Bash automatically appends a space after '=' by default.
|
|
|
|
# Disable it so that it works nicely for options in the form of -foo=bar.
|
2017-07-02 02:32:55 +08:00
|
|
|
[[ "${flags: -1}" == '=' ]] && compopt -o nospace 2> /dev/null
|
2017-05-24 02:39:08 +08:00
|
|
|
COMPREPLY=( $( compgen -W "$flags" -- "$cur" ) )
|
|
|
|
fi
|
2017-05-24 02:52:27 +08:00
|
|
|
}
|
2017-05-24 02:39:08 +08:00
|
|
|
complete -F _clang clang
|