Skip to content

Commit 1d78ee1

Browse files
committed
Complete options
Add completion of options. If there are aliases in bookmarks and none of the arguments is an alias, try to complete an alias. If there are no aliases in bookmarks or alias is one of the arguments, do the default completion.
1 parent e131439 commit 1d78ee1

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

completion/yafc

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,38 @@
22

33
_yafc()
44
{
5-
local cur sed_flags
5+
local cur prev sed_flags opts aliases len_wo_first_and_last word _alias
66

77
COMPREPLY=()
88
cur=`_get_cword`
99
sed_flags=-nre
10+
opts="-a --anon -d --debug -D --dump-rc -m --mechanism= -n --norc -p \
11+
--noproxy -q --quiet -r --rcfile= -t --trace= -u --noauto -U \
12+
--noalias -v --verbose -w --wait= -W --workdir= -V --version -h --help"
13+
14+
if [[ ${cur} == -* ]]; then
15+
COMPREPLY=( $( compgen -W "$opts" -- "$cur" ) )
16+
[[ $COMPREPLY == *= ]] && compopt -o nospace
17+
return 0
18+
fi
1019

1120
if [ "$( uname )" = "Darwin" ]; then
1221
sed_flags=-nEe
1322
fi
1423

15-
if [ $COMP_CWORD -eq 1 ] && [ -f ~/.yafc/bookmarks ]; then
16-
COMPREPLY=( $( compgen -W '$( sed $sed_flags "/machine/ s/.* alias '\''([^'\'']*)'\''/\1/ p" \
17-
~/.yafc/bookmarks )' -- "$cur" ) )
24+
if [ -f ~/.yafc/bookmarks ]; then
25+
aliases=( $( sed $sed_flags \
26+
'/machine/ s/.* alias '\''([^'\'']*)'\''/\1/ p' \
27+
~/.yafc/bookmarks ) )
28+
len_wo_first_and_last=$(( ${#COMP_WORDS[@]} - 2 ))
29+
# If alias is already one of the arguments, don't complete the current
30+
# word to one.
31+
for word in "${COMP_WORDS[@]:1:$len_wo_first_and_last}"; do
32+
for _alias in "${aliases[@]}"; do
33+
[[ $_alias == $word ]] && return 0
34+
done
35+
done
36+
COMPREPLY=( $( compgen -W "${aliases[*]}" -- "$cur" ) )
1837
fi
1938

2039
return 0

0 commit comments

Comments
 (0)