From 767d55eba0355d9ef9270df3ca257a936cf0eac5 Mon Sep 17 00:00:00 2001
From: Eric Freese <ericdfreese@gmail.com>
Date: Sun, 26 Jan 2020 21:46:58 -0700
Subject: [PATCH] Allow loading the plugin in the background

If zle is already active (plugin has been loaded in the background),
just start the widgets and set up the automatic re-binding (if manual
rebind has not been specified).

See GitHub issue #481
---
 src/start.zsh           | 10 ++++++++--
 zsh-autosuggestions.zsh | 10 ++++++++--
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/start.zsh b/src/start.zsh
index 221e932..ca9f966 100644
--- a/src/start.zsh
+++ b/src/start.zsh
@@ -21,5 +21,11 @@ _zsh_autosuggest_start() {
 # Mark for auto-loading the functions that we use
 autoload -Uz add-zsh-hook is-at-least
 
-# Start the autosuggestion widgets on the next precmd
-add-zsh-hook precmd _zsh_autosuggest_start
+# If zle is already running, go ahead and start the autosuggestion widgets.
+# Otherwise, wait until the next precmd.
+if zle; then
+	_zsh_autosuggest_start
+	(( ! ${+ZSH_AUTOSUGGEST_MANUAL_REBIND} )) && add-zsh-hook precmd _zsh_autosuggest_start
+else
+	add-zsh-hook precmd _zsh_autosuggest_start
+fi
diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh
index 3277dbb..9f5db95 100644
--- a/zsh-autosuggestions.zsh
+++ b/zsh-autosuggestions.zsh
@@ -854,5 +854,11 @@ _zsh_autosuggest_start() {
 # Mark for auto-loading the functions that we use
 autoload -Uz add-zsh-hook is-at-least
 
-# Start the autosuggestion widgets on the next precmd
-add-zsh-hook precmd _zsh_autosuggest_start
+# If zle is already running, go ahead and start the autosuggestion widgets.
+# Otherwise, wait until the next precmd.
+if zle; then
+	_zsh_autosuggest_start
+	(( ! ${+ZSH_AUTOSUGGEST_MANUAL_REBIND} )) && add-zsh-hook precmd _zsh_autosuggest_start
+else
+	add-zsh-hook precmd _zsh_autosuggest_start
+fi