From ac3b345365354252b7c264d5ff4fe6957c11798d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marc=20Cornell=C3=A0?= <marc.cornella@live.com>
Date: Tue, 2 Oct 2018 21:31:26 +0200
Subject: [PATCH] dircycle: trigger appropriate hooks after directory change
 (#7161)

This commit triggers precmd and chpwd hook functions iff we changed directory.

This has the same behavior as zsh's hook function execution, which tries to run
the functions in the order specified and silently ignores any function that
does not exist.

See http://zsh.sourceforge.net/Doc/Release/Functions.html#Hook-Functions

Also moved duplicate nopushdminus logic to the `switch-to-dir` function.
---
 plugins/dircycle/dircycle.plugin.zsh | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/plugins/dircycle/dircycle.plugin.zsh b/plugins/dircycle/dircycle.plugin.zsh
index 8c58cab4c..bb69f6b3f 100644
--- a/plugins/dircycle/dircycle.plugin.zsh
+++ b/plugins/dircycle/dircycle.plugin.zsh
@@ -9,31 +9,36 @@
 #  pushd -N: start counting from right of `dirs' output
 
 switch-to-dir () {
-	[[ ${#dirstack} -eq 0 ]] && return
+	setopt localoptions nopushdminus
+	[[ ${#dirstack} -eq 0 ]] && return 1
 
 	while ! builtin pushd -q $1 &>/dev/null; do
 		# We found a missing directory: pop it out of the dir stack
 		builtin popd -q $1
 
 		# Stop trying if there are no more directories in the dir stack
-		[[ ${#dirstack} -eq 0 ]] && break
+		[[ ${#dirstack} -eq 0 ]] && return 1
 	done
 }
 
 insert-cycledleft () {
-	emulate -L zsh
-	setopt nopushdminus
+	switch-to-dir +1 || return
 
-	switch-to-dir +1
+	local fn
+	for fn (chpwd $chpwd_functions precmd $precmd_functions); do
+		(( $+functions[$fn] )) && $fn
+	done
 	zle reset-prompt
 }
 zle -N insert-cycledleft
 
 insert-cycledright () {
-	emulate -L zsh
-	setopt nopushdminus
+	switch-to-dir -0 || return
 
-	switch-to-dir -0
+	local fn
+	for fn (chpwd $chpwd_functions precmd $precmd_functions); do
+		(( $+functions[$fn] )) && $fn
+	done
 	zle reset-prompt
 }
 zle -N insert-cycledright