From b9fee8a324eaad01570fdae62bb816cb63531020 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Wed, 10 Apr 2019 11:41:20 -0600 Subject: [PATCH] Allow disabling of automatic widget re-binding Addresses github #411 --- README.md | 4 ++++ spec/options/widget_lists_spec.rb | 23 +++++++++++++++++++++++ src/start.zsh | 16 +++++++++------- zsh-autosuggestions.zsh | 16 +++++++++------- 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index f6907dd..f5a57e0 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,10 @@ This can be useful when pasting large amount of text in the terminal, to avoid t As of `v0.4.0`, suggestions can be fetched asynchronously. To enable this behavior, set the `ZSH_AUTOSUGGEST_USE_ASYNC` variable (it can be set to anything). +### Disabling automatic widget re-binding + +Set `ZSH_AUTOSUGGEST_MANUAL_REBIND` (it can be set to anything) to disable automatic widget re-binding on each precmd. This can be a big boost to performance, but you'll need to handle re-binding yourself if any of the widget lists change or if you or another plugin wrap any of the autosuggest widgets. To re-bind widgets, run `_zsh_autosuggest_bind_widgets`. + ### Key Bindings diff --git a/spec/options/widget_lists_spec.rb b/spec/options/widget_lists_spec.rb index c207c0c..db3612f 100644 --- a/spec/options/widget_lists_spec.rb +++ b/spec/options/widget_lists_spec.rb @@ -94,4 +94,27 @@ describe 'a modification to the widget lists' do wait_for { session.content(esc_seqs: true) }.to eq('echo hello') end end + + context 'when manual rebind is enabled' do + let(:options) { ["ZSH_AUTOSUGGEST_MANUAL_REBIND=true"] } + + it 'does not take effect until bind command is re-run' do + with_history('echo hello') do + session.send_string('e') + wait_for { session.content }.to eq('echo hello') + session.send_keys('C-b') + sleep 1 + expect(session.content(esc_seqs: true)).not_to eq('echo hello') + + session.send_keys('C-c') + session.run_command('_zsh_autosuggest_bind_widgets').clear_screen + wait_for { session.content }.to eq('') + + session.send_string('e') + wait_for { session.content }.to eq('echo hello') + session.send_keys('C-b') + wait_for { session.content(esc_seqs: true) }.to eq('echo hello') + end + end + end end diff --git a/src/start.zsh b/src/start.zsh index a73ee3b..0125ab8 100644 --- a/src/start.zsh +++ b/src/start.zsh @@ -5,15 +5,17 @@ # Start the autosuggestion widgets _zsh_autosuggest_start() { - add-zsh-hook -d precmd _zsh_autosuggest_start + # By default we re-bind widgets on every precmd to ensure we wrap other + # wrappers. Specifically, highlighting breaks if our widgets are wrapped by + # zsh-syntax-highlighting widgets. This also allows modifications to the + # widget list variables to take effect on the next precmd. However this has + # a decent performance hit, so users can set ZSH_AUTOSUGGEST_MANUAL_REBIND + # to disable the automatic re-binding. + if (( ${+ZSH_AUTOSUGGEST_MANUAL_REBIND} )); then + add-zsh-hook -d precmd _zsh_autosuggest_start + fi _zsh_autosuggest_bind_widgets - - # Re-bind widgets on every precmd to ensure we wrap other wrappers. - # Specifically, highlighting breaks if our widgets are wrapped by - # zsh-syntax-highlighting widgets. This also allows modifications - # to the widget list variables to take effect on the next precmd. - add-zsh-hook precmd _zsh_autosuggest_bind_widgets } # Start the autosuggestion widgets on the next precmd diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 265a4b5..7be3415 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -669,15 +669,17 @@ _zsh_autosuggest_async_response() { # Start the autosuggestion widgets _zsh_autosuggest_start() { - add-zsh-hook -d precmd _zsh_autosuggest_start + # By default we re-bind widgets on every precmd to ensure we wrap other + # wrappers. Specifically, highlighting breaks if our widgets are wrapped by + # zsh-syntax-highlighting widgets. This also allows modifications to the + # widget list variables to take effect on the next precmd. However this has + # a decent performance hit, so users can set ZSH_AUTOSUGGEST_MANUAL_REBIND + # to disable the automatic re-binding. + if (( ${+ZSH_AUTOSUGGEST_MANUAL_REBIND} )); then + add-zsh-hook -d precmd _zsh_autosuggest_start + fi _zsh_autosuggest_bind_widgets - - # Re-bind widgets on every precmd to ensure we wrap other wrappers. - # Specifically, highlighting breaks if our widgets are wrapped by - # zsh-syntax-highlighting widgets. This also allows modifications - # to the widget list variables to take effect on the next precmd. - add-zsh-hook precmd _zsh_autosuggest_bind_widgets } # Start the autosuggestion widgets on the next precmd