r306962 - [Bash-autocompletion] Add support for older bash version.

Yuka Takahashi via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 1 11:32:55 PDT 2017


Author: yamaguchi
Date: Sat Jul  1 11:32:55 2017
New Revision: 306962

URL: http://llvm.org/viewvc/llvm-project?rev=306962&view=rev
Log:
[Bash-autocompletion] Add support for older bash version.

Summary:
OS X seems to use older bash version which doesn't suport
_init_completion and compopt, so add support for this.

Reviewers: ruiu, v.g.vassilev, teemperor

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D34924

Modified:
    cfe/trunk/utils/bash-autocomplete.sh

Modified: cfe/trunk/utils/bash-autocomplete.sh
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/utils/bash-autocomplete.sh?rev=306962&r1=306961&r2=306962&view=diff
==============================================================================
--- cfe/trunk/utils/bash-autocomplete.sh (original)
+++ cfe/trunk/utils/bash-autocomplete.sh Sat Jul  1 11:32:55 2017
@@ -1,15 +1,33 @@
 # Please add "source /path/to/bash-autocomplete.sh" to your .bashrc to use this.
+
+_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 ) )
+}
+
 _clang()
 {
-  local cur prev words cword arg flags
-  _init_completion -n : || return
+  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
 
   # 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.
-  local w1="${COMP_WORDS[$cword - 1]}"
-  local w2="${COMP_WORDS[$cword - 2]}"
+  w1="${COMP_WORDS[$cword - 1]}"
+  if [[ $cword > 1 ]]; then
+    w2="${COMP_WORDS[$cword - 2]}"
+  fi
   if [[ "$cur" == -* ]]; then
     # -foo<tab>
     arg="$cur"
@@ -30,18 +48,18 @@ _clang()
   # If clang is old that it does not support --autocomplete,
   # fall back to the filename completion.
   if [[ "$?" != 0 ]]; then
-    _filedir
+    _clang_filedir
     return
   fi
 
   if [[ "$cur" == '=' ]]; then
     COMPREPLY=( $( compgen -W "$flags" -- "") )
   elif [[ "$flags" == "" || "$arg" == "" ]]; then
-    _filedir
+    _clang_filedir
   else
     # Bash automatically appends a space after '=' by default.
     # Disable it so that it works nicely for options in the form of -foo=bar.
-    [[ "${flags: -1}" == '=' ]] && compopt -o nospace
+    [[ "${flags: -1}" == '=' ]] && compopt -o nospace 2> /dev/null
     COMPREPLY=( $( compgen -W "$flags" -- "$cur" ) )
   fi
 }




More information about the cfe-commits mailing list