[Bash-completion-commits] [SCM] bash-completion branch, master, updated. 1.3

David Paleino dapal at debian.org
Sun Feb 6 20:13:08 UTC 2011


The following commit has been merged in the master branch:
commit f85f5347278c5f7109a5e59dd02814c479daa82d
Merge: aab38f6160dfc94b6f1afafbd02458182a0b5e84 94372b2aa6e9df90d47bedb1dabedb3a71ee1e0e
Author: David Paleino <dapal at debian.org>
Date:   Fri Jun 11 11:12:11 2010 +0200

    Merge branch 'master' into 1.x

diff --combined bash_completion
index b6476a8,7a727f9..e7a4b6e
--- a/bash_completion
+++ b/bash_completion
@@@ -23,7 -23,7 +23,7 @@@
  #
  #   http://bash-completion.alioth.debian.org/
  #
 -#   RELEASE: 2.x
 +#   RELEASE: 1.1
  
  if [[ $- == *v* ]]; then
      BASH_COMPLETION_ORIGINAL_V_VALUE="-v"
@@@ -204,6 -204,68 +204,68 @@@ dequote(
  }
  
  
+ # Assign variable one scope above the caller
+ # Usage: local "$1" && _upvar $1 "value(s)"
+ # Param: $1  Variable name to assign value to
+ # Param: $*  Value(s) to assign.  If multiple values, an array is
+ #            assigned, otherwise a single value is assigned.
+ # NOTE: For assigning multiple variables, use '_upvars'.  Do NOT
+ #       use multiple '_upvar' calls, since one '_upvar' call might
+ #       reassign a variable to be used by another '_upvar' call.
+ # See: http://fvue.nl/wiki/Bash:_Passing_variables_by_reference
+ _upvar() {
+     if unset -v "$1"; then           # Unset & validate varname
+         if (( $# == 2 )); then
+             eval $1=\"\$2\"          # Return single value
+         else
+             eval $1=\(\"\${@:2}\"\)  # Return array
+         fi
+     fi
+ }
+ 
+ 
+ # Assign variables one scope above the caller
+ # Usage: local varname [varname ...] && 
+ #        _upvars [-v varname value] | [-aN varname [value ...]] ...
+ # Available OPTIONS:
+ #     -aN  Assign next N values to varname as array
+ #     -v   Assign single value to varname
+ # Return: 1 if error occurs
+ # See: http://fvue.nl/wiki/Bash:_Passing_variables_by_reference
+ _upvars() {
+     if ! (( $# )); then
+         echo "${FUNCNAME[0]}: usage: ${FUNCNAME[0]} [-v varname"\
+             "value] | [-aN varname [value ...]] ..." 1>&2
+         return 2
+     fi
+     while (( $# )); do
+         case $1 in
+             -a*)
+                 # Error checking
+                 [[ ${1#-a} ]] || { echo "bash: ${FUNCNAME[0]}: \`$1': missing"\
+                     "number specifier" 1>&2; return 1; }
+                 printf %d "${1#-a}" &> /dev/null || { echo "bash:"\
+                     "${FUNCNAME[0]}: \`$1': invalid number specifier" 1>&2
+                     return 1; }
+                 # Assign array of -aN elements
+                 [[ "$2" ]] && unset -v "$2" && eval $2=\(\"\${@:3:${1#-a}}\"\) && 
+                 shift $((${1#-a} + 2)) || { echo "bash: ${FUNCNAME[0]}:"\
+                     "\`$1${2+ }$2': missing argument(s)" 1>&2; return 1; }
+                 ;;
+             -v)
+                 # Assign single value
+                 [[ "$2" ]] && unset -v "$2" && eval $2=\"\$3\" &&
+                 shift 3 || { echo "bash: ${FUNCNAME[0]}: $1: missing"\
+                 "argument(s)" 1>&2; return 1; }
+                 ;;
+             *)
+                 echo "bash: ${FUNCNAME[0]}: $1: invalid option" 1>&2
+                 return 1 ;;
+         esac
+     done
+ }
+ 
+ 
  # Reassemble command line words, excluding specified characters from the
  # list of word completion separators (COMP_WORDBREAKS).
  # @param $1 chars  Characters out of $COMP_WORDBREAKS which should
@@@ -267,30 -329,10 +329,10 @@@ __reassemble_comp_words_by_ref() 
  # @param $4 cur  Name of variable to return current word to complete to
  # @see ___get_cword_at_cursor_by_ref()
  __get_cword_at_cursor_by_ref() {
-     # NOTE: The call to the main function ___get_cword_at_cursor_by_ref() is
-     #       wrapped to make collisions with local variable names less likely.
-     local __words __cword __cur
-     ___get_cword_at_cursor_by_ref "$1" __words __cword __cur
- 
-     eval $2=\( \"\${__words[@]}\" \)
-     eval $3=\$__cword
-     eval $4=\$__cur
- }
- 
- 
- # @param $1 exclude
- # @param $2 words  Name of variable to return words to
- # @param $3 cword  Name of variable to return cword to
- # @param $4 cur  Name of variable to return current word to complete to
- # @note  Do not call this function directly but call 
- #     `__get_cword_at_cursor_by_ref()' instead to make variable name collisions
- #     less likely
- # @see __get_cword_at_cursor_by_ref()
- ___get_cword_at_cursor_by_ref() {
-     local cword words
+     local cword words=()
      __reassemble_comp_words_by_ref "$1" words cword
  
-     local i
+     local i cur2
      local cur="$COMP_LINE"
      local index="$COMP_POINT"
      for (( i = 0; i <= cword; ++i )); do
@@@ -318,13 -360,13 +360,13 @@@
  
      if [[ "${words[cword]:0:${#cur}}" != "$cur" ]]; then
          # We messed up. At least return the whole word so things keep working
-         eval $4=\"\${words[cword]}\"
+         cur2=${words[cword]}
      else
-         eval $4=\"\${cur:0:\$index}\"
+         cur2=${cur:0:$index}
      fi
  
-     eval $2=\( \"\${words[@]}\" \)
-     eval $3=\$cword
+     local "$2" "$3" "$4" && 
+         _upvars -a${#words[@]} $2 "${words[@]}" -v $3 "$cword" -v $4 "$cur2"
  }
  
  
@@@ -334,62 -376,66 +376,66 @@@
  # (For example, if the line is "ls foobar",
  # and the cursor is here -------->   ^
  # Also one is able to cross over possible wordbreak characters.
- # Usage: _get_comp_words_by_ref [OPTIONS] VAR1 [VAR2 [VAR3]]
+ # Usage: _get_comp_words_by_ref [OPTIONS] [VARNAMES]
+ # Available VARNAMES:
+ #     cur         Return cur via $cur
+ #     prev        Return prev via $prev
+ #     words       Return words via $words
+ #     cword       Return cword via $cword
+ #
+ # Available OPTIONS:
+ #     -n EXCLUDE  Characters out of $COMP_WORDBREAKS which should NOT be 
+ #                 considered word breaks. This is useful for things like scp
+ #                 where we want to return host:path and not only path, so we
+ #                 would pass the colon (:) as -n option in this case.  Bash-3
+ #                 doesn't do word splitting, so this ensures we get the same
+ #                 word on both bash-3 and bash-4.
+ #     -c VARNAME  Return cur via $VARNAME
+ #     -p VARNAME  Return prev via $VARNAME
+ #     -w VARNAME  Return words via $VARNAME
+ #     -i VARNAME  Return cword via $VARNAME
+ #
  # Example usage:
  #
  #    $ _get_comp_words_by_ref -n : cur prev
  #
- # Options:  -n EXCLUDE  Characters out of $COMP_WORDBREAKS which should NOT
- #     be considered word breaks. This is useful for things like scp where
- #     we want to return host:path and not only path, so we would pass the
- #     colon (:) as -n option in this case.  Bash-3 doesn't do word splitting,
- #     so this ensures we get the same word on both bash-3 and bash-4.
- # @see __get_comp_words_by_ref
- _get_comp_words_by_ref() {
-     # NOTE: The call to the main function __get_comp_words_by_ref() is wrapped
-     #       to make collisions with local variable name less likely.
-     local __words __cword __cur __var __vars 
-     __get_comp_words_by_ref __words __cword __cur __vars "$@"
-     set -- "${__vars[@]}"
-     eval $1=\$__cur
-     shift
-     for __var; do
-         ((__cword--))
-         [[ ${__words[__cword]} ]] && eval $__var=\${__words[__cword]}
-     done
- }
- 
- 
- # @param $1 words  Name of variable to return words to
- # @param $2 cword  Name of variable to return cword to
- # @param $3 cur  Name of variable to return current word to complete to
- # @param $4 varnames  Name of variable to return array of variable names to
- # @param $@  Arguments to _get_comp_words_by_ref()
- # @note  Do not call this function directly but call `_get_comp_words_by_ref()'
- #     instead to make variable name collisions less likely
- # @see _get_comp_words_by_ref()
- __get_comp_words_by_ref()
+ _get_comp_words_by_ref()
  {
-     local exclude flag i OPTIND=5  # Skip first four arguments
-     local cword words cur varnames=()
-     while getopts "n:" flag "$@"; do
+     local exclude flag i OPTIND=1
+     local cur cword words=()
+     local upargs=() upvars=() vcur vcword vprev vwords
+ 
+     while getopts "c:i:n:p:w:" flag "$@"; do
          case $flag in
+             c) vcur=$OPTARG ;;
+             i) vcword=$OPTARG ;;
              n) exclude=$OPTARG ;;
+             p) vprev=$OPTARG ;;
+             w) vwords=$OPTARG ;;
          esac
      done
-     varnames=( ${!OPTIND} )
-     let "OPTIND += 1"
      while [[ $# -ge $OPTIND ]]; do 
-         varnames+=( ${!OPTIND} )
+         case ${!OPTIND} in
+             cur)   vcur=cur ;;
+             prev)  vprev=prev ;;
+             cword) vcword=cword ;;
+             words) vwords=words ;;
+             *) echo "bash: $FUNCNAME(): \`${!OPTIND}': unknown argument" \
+                 1>&2; return 1
+         esac
          let "OPTIND += 1"
      done
  
      __get_cword_at_cursor_by_ref "$exclude" words cword cur
  
-     eval $1=\( \"\${words[@]}\" \)
-     eval $2=\$cword
-     eval $3=\$cur
-     eval $4=\( \"\${varnames[@]}\" \)
+     [[ $vcur   ]] && { upvars+=("$vcur"  ); upargs+=(-v $vcur   "$cur"  ); }
+     [[ $vcword ]] && { upvars+=("$vcword"); upargs+=(-v $vcword "$cword"); }
+     [[ $vprev  ]] && { upvars+=("$vprev" ); upargs+=(-v $vprev 
+         "${words[cword - 1]}"); }
+     [[ $vwords ]] && { upvars+=("$vwords"); upargs+=(-a${#words[@]} $vwords
+         "${words[@]}"); }
+ 
+     (( ${#upvars[@]} )) && local "${upvars[@]}" && _upvars "${upargs[@]}"
  }
  
  
@@@ -407,7 -453,8 +453,8 @@@
  #     current word (default is 0, previous is 1), respecting the exclusions
  #     given at $1.  For example, `_get_cword "=:" 1' returns the word left of
  #     the current word, respecting the exclusions "=:".
- #
+ # @deprecated  Use `_get_comp_words_by_ref cur' instead
+ # @see _get_comp_words_by_ref()
  _get_cword()
  {
      local LC_CTYPE=C
@@@ -461,7 -508,8 +508,8 @@@
  # This is a good alternative to `prev=${COMP_WORDS[COMP_CWORD-1]}' because bash4
  # will properly return the previous word with respect to any given exclusions to
  # COMP_WORDBREAKS.
- # @see _get_cword()
+ # @deprecated  Use `_get_comp_words_by_ref cur prev' instead
+ # @see _get_comp_words_by_ref()
  #
  _get_pword() 
  {
@@@ -786,7 -834,7 +834,7 @@@ __expand_tilde_by_ref() 
              #    becomes "~a".  Double quotes allow eval.
              # 2: Remove * before the first slash (/), i.e. "~a/b"
              #    becomes "b".  Single quotes prevent eval.
-             #       +-----1----+ +---2----+
+             #                          +-----1----+ +---2----+
              eval $1="${!1/%\/*}"/'${!1#*/}'
          else 
              # No, $1 doesn't contain slash
@@@ -1088,7 -1136,7 +1136,7 @@@ _user_at_host() 
      local cur
  
      COMPREPLY=()
-     cur=`_get_cword :`
+     _get_comp_words_by_ref -n : cur
  
      if [[ $cur == *@* ]]; then
          _known_hosts_real "$cur"
@@@ -1294,7 -1342,8 +1342,8 @@@ complete -F _known_hosts traceroute tra
  #
  _cd()
  {
-     local IFS=$'\t\n' cur=`_get_cword` i j k
+     local cur IFS=$'\t\n' i j k
+     _get_comp_words_by_ref cur
  
      # try to allow variable completion
      if [[ "$cur" == ?(\\)\$* ]]; then
@@@ -1397,7 -1446,7 +1446,7 @@@ _command_offset(
      COMP_CWORD=$(( $COMP_CWORD - $word_offset ))
  
      COMPREPLY=()
-     cur=`_get_cword`
+     _get_comp_words_by_ref cur
  
      if [[ $COMP_CWORD -eq 0 ]]; then
          COMPREPLY=( $( compgen -c -- "$cur" ) )
@@@ -1452,8 -1501,7 +1501,7 @@@ _longopt(
  {
      local cur prev
  
-     cur=`_get_cword`
-     prev=${COMP_WORDS[COMP_CWORD-1]}
+     _get_comp_words_by_ref cur prev
  
      if _split_longopt; then
          case "$prev" in
@@@ -1499,7 -1547,7 +1547,7 @@@ _filedir_xspec(
  
      IFS=$'\t\n'
      COMPREPLY=()
-     cur=`_get_cword`
+     _get_comp_words_by_ref cur
  
      _expand || return 0
  

-- 
bash-completion



More information about the Bash-completion-commits mailing list