# [PATCH 0/5] contrib: completion: general zsh updates

6 messages from 2019-06-14 to 2019-06-14. Participants: Felipe Contreras.
Thread: https://gitlist.dev/t/51310

## Felipe Contreras, 2019-06-14 06:50

Subject: [PATCH 0/5] contrib: completion: general zsh updates
Message-ID: <20190614065051.17774-1-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20190614065051.17774-1-felipe.contreras%40gmail.com

```
Helo,

These patches are definitely needed, and some of these have been cooking
for years in oh-my-zsh.

Felipe Contreras (5):
  completion: zsh: update installation instructions
  completion: zsh: fix for directories with spaces
  completion: remove zsh hack
  completion: zsh: improve main function selection
  completion: prompt: fix color for Zsh

 contrib/completion/git-completion.bash |  1 +
 contrib/completion/git-completion.zsh  | 33 ++++++++++++--------------
 contrib/completion/git-prompt.sh       | 10 ++++----
 3 files changed, 22 insertions(+), 22 deletions(-)

-- 
2.22.0.rc2.dirty


```

## Felipe Contreras, 2019-06-14 06:50

Subject: [PATCH 1/5] completion: zsh: update installation instructions
Message-ID: <20190614065051.17774-2-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20190614065051.17774-2-felipe.contreras%40gmail.com
In-Reply-To: <20190614065051.17774-1-felipe.contreras@gmail.com>

```
Commit 0e5ed7cca3 wrongly changed the extension of the bash script
to .zsh. The extension doesn't really matter, but it confuses people.

I've changed the text to make it clear that your zsh script goes to
~/.zsh/_git, and the bash script to ~/.git-completion.bash (or wherever
you want).

Also, update the default locations of the system bash-completion.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.zsh | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/contrib/completion/git-completion.zsh b/contrib/completion/git-completion.zsh
index 886bf95d1f..b3c4588515 100644
--- a/contrib/completion/git-completion.zsh
+++ b/contrib/completion/git-completion.zsh
@@ -4,17 +4,19 @@
 #
 # Copyright (c) 2012-2013 Felipe Contreras <felipe.contreras@gmail.com>
 #
-# You need git's bash completion script installed somewhere, by default it
-# would be the location bash-completion uses.
-#
-# If your script is somewhere else, you can configure it on your ~/.zshrc:
-#
-#  zstyle ':completion:*:*:git:*' script ~/.git-completion.zsh
-#
 # The recommended way to install this script is to copy to '~/.zsh/_git', and
 # then add the following to your ~/.zshrc file:
 #
 #  fpath=(~/.zsh $fpath)
+#
+# You need git's bash completion script installed. By default bash-completion
+# uses /usr/share/bash-completion.
+#
+# If your bash completion script is somewhere else, you can configure it on
+# your ~/.zshrc:
+#
+#  zstyle ':completion:*:*:git:*' script ~/.git-completion.bash
+#
 
 complete ()
 {
@@ -31,9 +33,8 @@ if [ -z "$script" ]; then
 	local e
 	locations=(
 		$(dirname ${funcsourcetrace[1]%:*})/git-completion.bash
-		'/etc/bash_completion.d/git' # fedora, old debian
-		'/usr/share/bash-completion/completions/git' # arch, ubuntu, new debian
-		'/usr/share/bash-completion/git' # gentoo
+		'/usr/share/bash-completion/completions/git'
+		'/etc/bash_completion.d/git' # old debian
 		)
 	for e in $locations; do
 		test -f $e && script="$e" && break
-- 
2.22.0.rc2.dirty


```

## Felipe Contreras, 2019-06-14 06:50

Subject: [PATCH 2/5] completion: zsh: fix for directories with spaces
Message-ID: <20190614065051.17774-3-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20190614065051.17774-3-felipe.contreras%40gmail.com
In-Reply-To: <20190614065051.17774-1-felipe.contreras@gmail.com>

```
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.zsh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/contrib/completion/git-completion.zsh b/contrib/completion/git-completion.zsh
index b3c4588515..067738d93f 100644
--- a/contrib/completion/git-completion.zsh
+++ b/contrib/completion/git-completion.zsh
@@ -32,7 +32,7 @@ if [ -z "$script" ]; then
 	local -a locations
 	local e
 	locations=(
-		$(dirname ${funcsourcetrace[1]%:*})/git-completion.bash
+		"$(dirname ${funcsourcetrace[1]%:*})"/git-completion.bash
 		'/usr/share/bash-completion/completions/git'
 		'/etc/bash_completion.d/git' # old debian
 		)
-- 
2.22.0.rc2.dirty


```

## Felipe Contreras, 2019-06-14 06:50

Subject: [PATCH 3/5] completion: remove zsh hack
Message-ID: <20190614065051.17774-4-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20190614065051.17774-4-felipe.contreras%40gmail.com
In-Reply-To: <20190614065051.17774-1-felipe.contreras@gmail.com>

```
We don't want to override the 'complete()' function in zsh, which can be
used by bashcomp.

Reported-by: Mark Lodato <lodato@google.com>
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.bash | 1 +
 contrib/completion/git-completion.zsh  | 6 ------
 2 files changed, 1 insertion(+), 6 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 9f71bcde96..57cced3f51 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -3066,6 +3066,7 @@ __git_func_wrap ()
 # This is NOT a public function; use at your own risk.
 __git_complete ()
 {
+	test -n "$ZSH_VERSION" && return
 	local wrapper="__git_wrap${2}"
 	eval "$wrapper () { __git_func_wrap $2 ; }"
 	complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
diff --git a/contrib/completion/git-completion.zsh b/contrib/completion/git-completion.zsh
index 067738d93f..58b3b5c27d 100644
--- a/contrib/completion/git-completion.zsh
+++ b/contrib/completion/git-completion.zsh
@@ -18,12 +18,6 @@
 #  zstyle ':completion:*:*:git:*' script ~/.git-completion.bash
 #
 
-complete ()
-{
-	# do nothing
-	return 0
-}
-
 zstyle -T ':completion:*:*:git:*' tag-order && \
 	zstyle ':completion:*:*:git:*' tag-order 'common-commands'
 
-- 
2.22.0.rc2.dirty


```

## Felipe Contreras, 2019-06-14 06:50

Subject: [PATCH 4/5] completion: zsh: improve main function selection
Message-ID: <20190614065051.17774-5-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20190614065051.17774-5-felipe.contreras%40gmail.com
In-Reply-To: <20190614065051.17774-1-felipe.contreras@gmail.com>

```
Sometimes we want to use the function directly (e.g. _git_checkout), for
example when zsh has the option 'complete_aliases', this way, we can do
something like:

  compdef _git gco=git_checkout

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.zsh | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/contrib/completion/git-completion.zsh b/contrib/completion/git-completion.zsh
index 58b3b5c27d..92b956d5de 100644
--- a/contrib/completion/git-completion.zsh
+++ b/contrib/completion/git-completion.zsh
@@ -227,8 +227,10 @@ _git ()
 
 	if (( $+functions[__${service}_zsh_main] )); then
 		__${service}_zsh_main
-	else
+	elif (( $+functions[__${service}_main] )); then
 		emulate ksh -c __${service}_main
+	elif (( $+functions[_${service}] )); then
+		emulate ksh -c _${service}
 	fi
 
 	let _ret && _default && _ret=0
-- 
2.22.0.rc2.dirty


```

## Felipe Contreras, 2019-06-14 06:50

Subject: [PATCH 5/5] completion: prompt: fix color for Zsh
Message-ID: <20190614065051.17774-6-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20190614065051.17774-6-felipe.contreras%40gmail.com
In-Reply-To: <20190614065051.17774-1-felipe.contreras@gmail.com>

```
We don't need PROMPT_COMMAND in Zsh; we are already using %F{color} %f,
which in turn use %{ and %}, which are the equivalent of Bash's
\[ and \].

We can use as many colors as we want and output directly into PS1
(or RPS1) without the risk of buffer wrapping issues.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-prompt.sh | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 983e419d2b..b57a9c96cb 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -88,7 +88,7 @@
 # If you would like a colored hint about the current dirty state, set
 # GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on
 # the colored output of "git status -sb" and are available only when
-# using __git_ps1 for PROMPT_COMMAND or precmd.
+# using __git_ps1 for PROMPT_COMMAND in Bash, but always available in Zsh.
 #
 # If you would like __git_ps1 to do nothing in the case when the current
 # directory is set up to be ignored by git, then set
@@ -506,9 +506,11 @@ __git_ps1 ()
 
 	local z="${GIT_PS1_STATESEPARATOR-" "}"
 
-	# NO color option unless in PROMPT_COMMAND mode
-	if [ $pcmode = yes ] && [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
-		__git_ps1_colorize_gitstring
+	# NO color option unless in PROMPT_COMMAND mode or it's Zsh
+	if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
+		if [ $pcmode = yes ] || [ -n "${ZSH_VERSION-}" ]; then
+			__git_ps1_colorize_gitstring
+		fi
 	fi
 
 	b=${b##refs/heads/}
-- 
2.22.0.rc2.dirty


```
