2
2
3
3
# zsh completion wrapper for git
4
4
#
5
- # Copyright (c) 2012-2013 Felipe Contreras <[email protected] >
5
+ # Copyright (c) 2012-2020 Felipe Contreras <[email protected] >
6
6
#
7
- # You need git's bash completion script installed somewhere, by default it
8
- # would be the location bash-completion uses .
7
+ # The recommended way to install this script is to make a copy of it as a
8
+ # file named '_git' inside any directory in your fpath .
9
9
#
10
- # If your script is somewhere else, you can configure it on your ~/.zshrc:
10
+ # For example, create a directory '~/.zsh/', copy this file to '~/.zsh/_git',
11
+ # and then add the following to your ~/.zshrc file:
11
12
#
12
- # zstyle ':completion:*:*:git:*' script ~/.git-completion.bash
13
+ # fpath=( ~/.zsh $fpath)
13
14
#
14
- # The recommended way to install this script is to make a copy of it in
15
- # ~/.zsh/ directory as ~/.zsh/git-completion.zsh and then add the following
16
- # to your ~/.zshrc file:
15
+ # You need git's bash completion script installed. By default bash-completion's
16
+ # location will be used (e.g. pkg-config --variable=completionsdir bash-completion).
17
+ #
18
+ # If your bash completion script is somewhere else, you can specify the
19
+ # location in your ~/.zshrc:
20
+ #
21
+ # zstyle ':completion:*:*:git:*' script ~/.git-completion.bash
17
22
#
18
- # fpath=(~/.zsh $fpath)
19
- # autoload -Uz compinit && compinit
20
-
21
- complete ()
22
- {
23
- # do nothing
24
- return 0
25
- }
26
23
27
24
zstyle -T ' :completion:*:*:git:*' tag-order && \
28
25
zstyle ' :completion:*:*:git:*' tag-order ' common-commands'
29
26
30
27
zstyle -s " :completion:*:*:git:*" script script
31
28
if [ -z " $script " ]; then
32
29
local -a locations
33
- local e
30
+ local e bash_completion
31
+
32
+ bash_completion=$( pkg-config --variable=completionsdir bash-completion 2> /dev/null) ||
33
+ bash_completion=' /usr/share/bash-completion/completions/'
34
+
34
35
locations=(
35
- $( dirname ${funcsourcetrace[1]%:* } ) /git-completion.bash
36
- ' /etc/bash_completion.d/ git' # fedora, old debian
37
- ' /usr/share/bash-completion/completions/ git' # arch, ubuntu, new debian
38
- ' /usr/share/bash-completion/ git' # gentoo
36
+ " $( dirname ${funcsourcetrace[1]%:* } ) " /git-completion.bash
37
+ " $HOME /.local/share/bash-completion/completions/ git"
38
+ " $bash_completion / git"
39
+ ' /etc/bash_completion.d/ git' # old debian
39
40
)
40
41
for e in $locations ; do
41
42
test -f $e && script=" $e " && break
42
43
done
43
44
fi
45
+
46
+ local old_complete=" $functions [complete]"
47
+ functions[complete]=:
44
48
GIT_SOURCING_ZSH_COMPLETION=y . " $script "
49
+ functions[complete]=" $old_complete "
45
50
46
51
__gitcomp ()
47
52
{
@@ -52,13 +57,35 @@ __gitcomp ()
52
57
case " $cur_ " in
53
58
--* =)
54
59
;;
60
+ --no-* )
61
+ local c IFS=$' \t\n '
62
+ local -a array
63
+ for c in ${=1} ; do
64
+ if [[ $c == " --" ]]; then
65
+ continue
66
+ fi
67
+ c=" $c ${4-} "
68
+ case $c in
69
+ --* =|* .) ;;
70
+ * ) c=" $c " ;;
71
+ esac
72
+ array+=(" $c " )
73
+ done
74
+ compset -P ' *[=:]'
75
+ compadd -Q -S ' ' -p " ${2-} " -a -- array && _ret=0
76
+ ;;
55
77
* )
56
78
local c IFS=$' \t\n '
57
79
local -a array
58
80
for c in ${=1} ; do
81
+ if [[ $c == " --" ]]; then
82
+ c=" --no-...${4-} "
83
+ array+=(" $c " )
84
+ break
85
+ fi
59
86
c=" $c ${4-} "
60
87
case $c in
61
- --* =* |* .) ;;
88
+ --* =|* .) ;;
62
89
* ) c=" $c " ;;
63
90
esac
64
91
array+=(" $c " )
@@ -73,44 +100,57 @@ __gitcomp_direct ()
73
100
{
74
101
emulate -L zsh
75
102
76
- local IFS=$' \n '
77
103
compset -P ' *[=:]'
78
- compadd -Q -- ${= 1} && _ret=0
104
+ compadd -Q -S ' ' -- ${(f) 1} && _ret=0
79
105
}
80
106
81
107
__gitcomp_nl ()
82
108
{
83
109
emulate -L zsh
84
110
85
- local IFS=$' \n '
86
111
compset -P ' *[=:]'
87
- compadd -Q -S " ${4- } " -p " ${2-} " -- ${= 1} && _ret=0
112
+ compadd -Q -S " ${4- } " -p " ${2-} " -- ${(f) 1} && _ret=0
88
113
}
89
114
90
- __gitcomp_nl_append ()
115
+ __gitcomp_file ()
91
116
{
92
117
emulate -L zsh
93
118
94
- local IFS=$' \n '
95
- compadd -Q -S " ${4- } " -p " ${2-} " -- ${=1} && _ret=0
119
+ compadd -f -p " ${2-} " -- ${(f)1} && _ret=0
120
+ }
121
+
122
+ __gitcomp_direct_append ()
123
+ {
124
+ __gitcomp_direct " $@ "
125
+ }
126
+
127
+ __gitcomp_nl_append ()
128
+ {
129
+ __gitcomp_nl " $@ "
96
130
}
97
131
98
132
__gitcomp_file_direct ()
99
133
{
100
- emulate -L zsh
134
+ __gitcomp_file " $1 " " "
135
+ }
101
136
102
- local IFS= $' \n '
103
- compset -P ' *[=:] '
104
- compadd -f -- ${=1} && _ret=0
137
+ _git_zsh ()
138
+ {
139
+ __gitcomp " v1.1 "
105
140
}
106
141
107
- __gitcomp_file ()
142
+ __git_complete_command ()
108
143
{
109
144
emulate -L zsh
110
145
111
- local IFS=$' \n '
112
- compset -P ' *[=:]'
113
- compadd -p " ${2-} " -f -- ${=1} && _ret=0
146
+ local command=" $1 "
147
+ local completion_func=" _git_${command// -/ _} "
148
+ if (( $+ functions[$completion_func ] )) ; then
149
+ emulate ksh -c $completion_func
150
+ return 0
151
+ else
152
+ return 1
153
+ fi
114
154
}
115
155
116
156
__git_zsh_bash_func ()
@@ -119,14 +159,12 @@ __git_zsh_bash_func ()
119
159
120
160
local command=$1
121
161
122
- local completion_func=" _git_${command// -/ _} "
123
- declare -f $completion_func > /dev/null && $completion_func && return
162
+ __git_complete_command " $command " && return
124
163
125
164
local expansion=$( __git_aliased_command " $command " )
126
165
if [ -n " $expansion " ]; then
127
166
words[1]=$expansion
128
- completion_func=" _git_${expansion// -/ _} "
129
- declare -f $completion_func > /dev/null && $completion_func
167
+ __git_complete_command " $expansion "
130
168
fi
131
169
}
132
170
@@ -163,8 +201,9 @@ __git_zsh_cmd_common ()
163
201
__git_zsh_cmd_alias ()
164
202
{
165
203
local -a list
166
- list=(${${${(0)" $( git config -z --get-regexp ' ^alias\.' ) " } # alias.} % $' \n ' * } )
167
- _describe -t alias-commands ' aliases' list $* && _ret=0
204
+ list=(${${(0)" $( git config -z --get-regexp ' ^alias\.*' ) " } # alias.} )
205
+ list=(${(f)" $( printf " %s:alias for '%s'\n" ${(f@ )list} ) " } )
206
+ _describe -t alias-commands ' aliases' list && _ret=0
168
207
}
169
208
170
209
__git_zsh_cmd_all ()
@@ -202,10 +241,13 @@ __git_zsh_main ()
202
241
203
242
case $state in
204
243
(command)
205
- _alternative \
206
- ' alias-commands:alias:__git_zsh_cmd_alias' \
207
- ' common-commands:common:__git_zsh_cmd_common' \
208
- ' all-commands:all:__git_zsh_cmd_all' && _ret=0
244
+ _tags common-commands alias-commands all-commands
245
+ while _tags; do
246
+ _requested common-commands && __git_zsh_cmd_common
247
+ _requested alias-commands && __git_zsh_cmd_alias
248
+ _requested all-commands && __git_zsh_cmd_all
249
+ let _ret || break
250
+ done
209
251
;;
210
252
(arg)
211
253
local command=" ${words[1]} " __git_dir
@@ -236,8 +278,12 @@ _git ()
236
278
237
279
if (( $+ functions[__${service} _zsh_main] )) ; then
238
280
__${service} _zsh_main
239
- else
281
+ elif (( $ + functions[__ ${service} _main] )) ; then
240
282
emulate ksh -c __${service} _main
283
+ elif (( $+ functions[_${service} ] )) ; then
284
+ emulate ksh -c _${service}
285
+ elif (( $+ functions[_${service// -/ _} ] )) ; then
286
+ emulate ksh -c _${service// -/ _}
241
287
fi
242
288
243
289
let _ret && _default && _ret=0
0 commit comments