Skip to content

Commit ce5889b

Browse files
committed
feat(bash_completion): add function _comp_compgen_ltrim_colon
1 parent 0a479ab commit ce5889b

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

bash_completion

+32-14
Original file line numberDiff line numberDiff line change
@@ -850,12 +850,17 @@ _comp_get_words()
850850
((${#upvars[@]})) && local "${upvars[@]}" && _comp_upvars "${upargs[@]}"
851851
}
852852

853-
# If the word-to-complete contains a colon (:), left-trim COMPREPLY items with
854-
# word-to-complete.
855-
# With a colon in COMP_WORDBREAKS, words containing
856-
# colons are always completed as entire words if the word to complete contains
857-
# a colon. This function fixes this, by removing the colon-containing-prefix
858-
# from COMPREPLY items.
853+
# Generate the specified items after left-trimming with the word-to-complete
854+
# containing a colon (:). If the word-to-complete does not contain a colon,
855+
# this generates the specified items without modifications.
856+
# @param $@ items to generate
857+
# @var[in] cur current word to complete
858+
#
859+
# @remarks In Bash, with a colon in COMP_WORDBREAKS, words containing colons
860+
# are always completed as entire words if the word to complete contains a
861+
# colon. This function fixes this behavior by removing the
862+
# colon-containing-prefix from the items.
863+
#
859864
# The preferred solution is to remove the colon (:) from COMP_WORDBREAKS in
860865
# your .bashrc:
861866
#
@@ -864,19 +869,32 @@ _comp_get_words()
864869
#
865870
# See also: Bash FAQ - E13) Why does filename completion misbehave if a colon
866871
# appears in the filename? - https://tiswww.case.edu/php/chet/bash/FAQ
872+
#
873+
# @since 2.12
874+
_comp_compgen_ltrim_colon()
875+
{
876+
(($#)) || return 0
877+
local -a tmp
878+
tmp=("$@")
879+
if [[ $cur == *:* && $COMP_WORDBREAKS == *:* ]]; then
880+
# Remove colon-word prefix from items
881+
local colon_word=${cur%"${cur##*:}"}
882+
tmp=("${tmp[@]#"$colon_word"}")
883+
fi
884+
_comp_compgen -R -- -W '"${tmp[@]}"'
885+
}
886+
887+
# If the word-to-complete contains a colon (:), left-trim COMPREPLY items with
888+
# word-to-complete.
889+
#
867890
# @param $1 current word to complete (cur)
868-
# @modifies global array $COMPREPLY
891+
# @var[in,out] COMPREPLY
869892
#
870893
# @since 2.12
871894
_comp_ltrim_colon_completions()
872895
{
873-
local i=${#COMPREPLY[*]}
874-
((i == 0)) && return 0
875-
if [[ $1 == *:* && $COMP_WORDBREAKS == *:* ]]; then
876-
# Remove colon-word prefix from COMPREPLY items
877-
local colon_word=${1%"${1##*:}"}
878-
COMPREPLY=("${COMPREPLY[@]#"$colon_word"}")
879-
fi
896+
((${#COMPREPLY[@]})) || return 0
897+
_comp_compgen -c "$1" ltrim_colon "${COMPREPLY[@]}"
880898
} # _comp_ltrim_colon_completions()
881899

882900
# This function quotes the argument in a way so that readline dequoting

0 commit comments

Comments
 (0)