From d8ba53678e27ca52e9d701cc74c6045751a8f838 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Wed, 10 Apr 2019 11:20:08 -0600 Subject: [PATCH 1/3] cleanup: Use `+` param expansion flag in arithmetic context --- src/widgets.zsh | 2 +- zsh-autosuggestions.zsh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/widgets.zsh b/src/widgets.zsh index 1d125d0..450ed3c 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -95,7 +95,7 @@ _zsh_autosuggest_modify() { # Fetch a new suggestion based on what's currently in the buffer _zsh_autosuggest_fetch() { - if [[ -n "${ZSH_AUTOSUGGEST_USE_ASYNC+x}" ]]; then + if (( ${+ZSH_AUTOSUGGEST_USE_ASYNC} )); then _zsh_autosuggest_async_request "$BUFFER" else local suggestion diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index e7e14a1..bae4ba8 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -366,7 +366,7 @@ _zsh_autosuggest_modify() { # Fetch a new suggestion based on what's currently in the buffer _zsh_autosuggest_fetch() { - if [[ -n "${ZSH_AUTOSUGGEST_USE_ASYNC+x}" ]]; then + if (( ${+ZSH_AUTOSUGGEST_USE_ASYNC} )); then _zsh_autosuggest_async_request "$BUFFER" else local suggestion From db290c518b600eedbb7af818ce19c5e7f106ceb7 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Wed, 10 Apr 2019 11:37:02 -0600 Subject: [PATCH 2/3] cleanup: Leave max size config unset by default to match other options --- README.md | 2 +- src/config.zsh | 4 ---- src/widgets.zsh | 2 +- zsh-autosuggestions.zsh | 6 +----- 4 files changed, 3 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index dcf953f..f6907dd 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ Widgets that modify the buffer and are not found in any of these arrays will fet ### Disabling suggestion for large buffers Set `ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE` to an integer value to disable autosuggestion for large buffers. The default is unset, which means that autosuggestion will be tried for any buffer size. Recommended value is 20. -This can be useful when pasting large amount of text in the terminal, to avoid triggering autosuggestion for too long strings. +This can be useful when pasting large amount of text in the terminal, to avoid triggering autosuggestion for strings that are too long. ### Enable Asynchronous Mode diff --git a/src/config.zsh b/src/config.zsh index a2f22ea..2f46aed 100644 --- a/src/config.zsh +++ b/src/config.zsh @@ -85,7 +85,3 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig- yank-pop ) } - -# Max size of buffer to trigger autosuggestion. Leave null for no upper bound. -(( ! ${+ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE} )) && -typeset -g ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE= diff --git a/src/widgets.zsh b/src/widgets.zsh index 450ed3c..7fb1183 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -85,7 +85,7 @@ _zsh_autosuggest_modify() { # Get a new suggestion if the buffer is not empty after modification if (( $#BUFFER > 0 )); then - if [[ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]] || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )); then + if (( ! ${+ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE} )) || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )); then _zsh_autosuggest_fetch fi fi diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index bae4ba8..265a4b5 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -122,10 +122,6 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig- ) } -# Max size of buffer to trigger autosuggestion. Leave null for no upper bound. -(( ! ${+ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE} )) && -typeset -g ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE= - #--------------------------------------------------------------------# # Utility Functions # #--------------------------------------------------------------------# @@ -356,7 +352,7 @@ _zsh_autosuggest_modify() { # Get a new suggestion if the buffer is not empty after modification if (( $#BUFFER > 0 )); then - if [[ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]] || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )); then + if (( ! ${+ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE} )) || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )); then _zsh_autosuggest_fetch fi fi From b9fee8a324eaad01570fdae62bb816cb63531020 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Wed, 10 Apr 2019 11:41:20 -0600 Subject: [PATCH 3/3] 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