completion: introduce __gitcomp_nl_append ()

There are situations where multiple classes of completions possible. For example branch.<TAB> should try to complete branch.master. branch.autosetupmerge branch.autosetuprebase The first candidate has the suffix ".", and the second/ third candidates have the suffix " ". To facilitate completions of this kind, create a variation of __gitcomp_nl () that appends to the existing list of completion candidates, COMPREPLY. Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ramkumar Ramachandra committed Jan 5, 2014 at 15:48 UTC f33c2c0f9e2258a80e6e406af39e9fa3fd6e430e
2 files changed +26 -4
contrib/completion/git-completion.bash
+18 -4
@@ -178,9 +178,9 @@ _get_comp_words_by_ref ()
178 }
179 fi
180
181 -__gitcompadd ()
181 +__gitcompappend ()
182 {
183 - local i=0
183 + local i=${#COMPREPLY[@]}
184 for x in $1; do
185 if [[ "$x" == "$3"* ]]; then
186 COMPREPLY[i++]="$2$x$4"
@@ -188,6 +188,12 @@ __gitcompadd ()
188 done
189 }
190
191 +__gitcompadd ()
192 +{
193 + COMPREPLY=()
194 + __gitcompappend "$@"
195 +}
196 +
197 # Generates completion reply, appending a space to possible completion words,
198 # if necessary.
199 # It accepts 1 to 4 arguments:
@@ -218,6 +224,14 @@ __gitcomp ()
224 esac
225 }
226
227 +# Variation of __gitcomp_nl () that appends to the existing list of
228 +# completion candidates, COMPREPLY.
229 +__gitcomp_nl_append ()
230 +{
231 + local IFS=$'\n'
232 + __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
233 +}
234 +
235 # Generates completion reply from newline-separated possible completion words
236 # by appending a space to all of them.
237 # It accepts 1 to 4 arguments:
@@ -229,8 +243,8 @@ __gitcomp ()
243 # appended.
244 __gitcomp_nl ()
245 {
232 - local IFS=$'\n'
233 - __gitcompadd "$1" "${2-}" "${3-$cur}" "${4- }"
246 + COMPREPLY=()
247 + __gitcomp_nl_append "$@"
248 }
249
250 # Generates completion reply with compgen from newline-separated possible
contrib/completion/git-completion.zsh
+8
@@ -76,6 +76,14 @@ __gitcomp_nl ()
76 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
77 }
78
79 +__gitcomp_nl_append ()
80 +{
81 + emulate -L zsh
82 +
83 + local IFS=$'\n'
84 + compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
85 +}
86 +
87 __gitcomp_file ()
88 {
89 emulate -L zsh