From 3136700ccf1fd5fb0a8d66627eed5edd892881dc Mon Sep 17 00:00:00 2001
From: Eric Freese <ericdfreese@gmail.com>
Date: Fri, 23 Mar 2018 15:04:21 -0600
Subject: [PATCH] Don't fetch suggestions after
 [up,down]-line-or-beginning-search

These widgets rely on `$LASTWIDGET` being set to restore the cursor
position. When asynchronous suggestions are enabled, and the widget
triggers a suggestion to be fetched, `autosuggest-suggest` will be
called and $LASTWIDGET will be set to it.
---
 .../async_line_or_beginning_spec.rb           | 27 +++++++++++++++++++
 src/config.zsh                                |  2 ++
 zsh-autosuggestions.zsh                       |  2 ++
 3 files changed, 31 insertions(+)
 create mode 100644 spec/integrations/async_line_or_beginning_spec.rb

diff --git a/spec/integrations/async_line_or_beginning_spec.rb b/spec/integrations/async_line_or_beginning_spec.rb
new file mode 100644
index 0000000..41935fd
--- /dev/null
+++ b/spec/integrations/async_line_or_beginning_spec.rb
@@ -0,0 +1,27 @@
+describe 'using up-line-or-beginning-search when async is enabled' do
+  let(:options) { ["ZSH_AUTOSUGGEST_USE_ASYNC="] }
+  let(:before_sourcing) do
+    -> do
+      session.
+        run_command('autoload -U up-line-or-beginning-search').
+        run_command('zle -N up-line-or-beginning-search').
+        send_string('bindkey "').
+        send_keys('C-v').send_keys('up').
+        send_string('" up-line-or-beginning-search').
+        send_keys('enter')
+    end
+  end
+
+  it 'should show previous history entries' do
+    with_history(
+      'echo foo',
+      'echo bar',
+      'echo baz'
+    ) do
+      session.clear_screen
+      3.times { session.send_keys('up') }
+      wait_for { session.content }.to eq("echo foo")
+    end
+  end
+end
+
diff --git a/src/config.zsh b/src/config.zsh
index ba694c0..5c44af5 100644
--- a/src/config.zsh
+++ b/src/config.zsh
@@ -21,6 +21,8 @@ ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
 	history-beginning-search-backward
 	history-substring-search-up
 	history-substring-search-down
+	up-line-or-beginning-search
+	down-line-or-beginning-search
 	up-line-or-history
 	down-line-or-history
 	accept-line
diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh
index a9330ea..3e0405d 100644
--- a/zsh-autosuggestions.zsh
+++ b/zsh-autosuggestions.zsh
@@ -57,6 +57,8 @@ ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
 	history-beginning-search-backward
 	history-substring-search-up
 	history-substring-search-down
+	up-line-or-beginning-search
+	down-line-or-beginning-search
 	up-line-or-history
 	down-line-or-history
 	accept-line