diff --git a/bash_completion b/bash_completion index f6481133..4878145b 100644 --- a/bash_completion +++ b/bash_completion @@ -339,7 +339,33 @@ __get_cword3() # _get_cword, main routine # __get_cword3, bash-3 variant # -[ ${BASH_VERSINFO[0]} -ge 4 ] && +[ ${BASH_VERSINFO[0]} -ge 4 ] && { +# return index of first occuring break character in $1; return 0 if none +__break_index() { + if [[ $1 == *[$WORDBREAKS]* ]]; then + local w="${1%[$WORDBREAKS]*}" + echo $((${#w}+1)) + else + echo 0 + fi +} # __break_index() + +# return the index of the start of the last word in $@ +__word_start() { + local buf="$@" + local start="$(__break_index "$buf")" + while [[ $start -ge 2 ]]; do + # Get character before $start + local char="${cur:$(( start - 2 )):1}" + # If the WORDBREAK character isn't escaped, exit loop + [[ $char != \\ ]] && break + # The WORDBREAK character is escaped; recalculate $start + buf="${COMP_LINE:0:$(( start - 2 ))}" + start=$(__break_index "$buf") + done + echo $start +} # __word_start() + __get_cword4() { local exclude="$1" n_idx="${2:-0}" @@ -363,41 +389,16 @@ __get_cword4() local cur="${COMP_LINE:0:$COMP_POINT}" local tmp="$cur" - local break_index word_start - # return index of first occuring break character in $1; return 0 if none - break_index() { - if [[ $1 == *[$WORDBREAKS]* ]]; then - local w="${1%[$WORDBREAKS]*}" - echo $((${#w}+1)) - else - echo 0 - fi - } - # return the index of the start of the last word in $@ - word_start() { - local buf="$@" - local start="$(break_index "$buf")" - while [[ $start -ge 2 ]]; do - # Get character before $start - local char="${cur:$(( start - 2 )):1}" - # If the WORDBREAK character isn't escaped, exit loop - [[ $char != \\ ]] && break - # The WORDBREAK character is escaped; recalculate $start - buf="${COMP_LINE:0:$(( start - 2 ))}" - start=$(break_index "$buf") - done - echo $start - } - # calculate current word, negatively offset by n_idx - cur="${tmp:$(word_start "$tmp")}" + cur="${tmp:$(__word_start "$tmp")}" while [[ $n_idx -gt 0 ]]; do local tmp="${tmp%[$WORDBREAKS]$cur}" # truncate passed string - local cur="${tmp:$(word_start "$tmp")}" # then recalculate + local cur="${tmp:$(__word_start "$tmp")}" # then recalculate ((--n_idx)) done - echo -n "$cur" + printf "%s" "$cur" } # __get_cword4() +} # [ ${BASH_VERSINFO[0]} -ge 4 ] # This function performs file and directory completion. It's better than