From 19e375bbc81d32aed85596dc73ccfe46aae7bbd5 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Sun, 26 Jan 2020 21:15:47 -0700 Subject: [PATCH 1/2] cleanup: Consolidate `autoload`s --- src/start.zsh | 4 +++- src/strategies/completion.zsh | 2 -- zsh-autosuggestions.zsh | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/start.zsh b/src/start.zsh index 5991039..221e932 100644 --- a/src/start.zsh +++ b/src/start.zsh @@ -18,6 +18,8 @@ _zsh_autosuggest_start() { _zsh_autosuggest_bind_widgets } +# 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 -autoload -Uz add-zsh-hook add-zsh-hook precmd _zsh_autosuggest_start diff --git a/src/strategies/completion.zsh b/src/strategies/completion.zsh index 4290f0c..e2d114c 100644 --- a/src/strategies/completion.zsh +++ b/src/strategies/completion.zsh @@ -45,8 +45,6 @@ _zsh_autosuggest_capture_completion_widget() { zle -N autosuggest-capture-completion _zsh_autosuggest_capture_completion_widget _zsh_autosuggest_capture_setup() { - autoload -Uz is-at-least - # There is a bug in zpty module in older zsh versions by which a # zpty that exits will kill all zpty processes that were forked # before it. Here we set up a zsh exit hook to SIGKILL the zpty diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index a8ef6c4..3277dbb 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -541,8 +541,6 @@ _zsh_autosuggest_capture_completion_widget() { zle -N autosuggest-capture-completion _zsh_autosuggest_capture_completion_widget _zsh_autosuggest_capture_setup() { - autoload -Uz is-at-least - # There is a bug in zpty module in older zsh versions by which a # zpty that exits will kill all zpty processes that were forked # before it. Here we set up a zsh exit hook to SIGKILL the zpty @@ -853,6 +851,8 @@ _zsh_autosuggest_start() { _zsh_autosuggest_bind_widgets } +# 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 -autoload -Uz add-zsh-hook add-zsh-hook precmd _zsh_autosuggest_start From 6c634c1e35cd6093ad01bb6002f63aa5c0f6a3b6 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Sun, 26 Jan 2020 21:18:01 -0700 Subject: [PATCH 2/2] Enable async mode by default in newer versions of zsh Allow users to override the default by unsetting (or setting) the ZSH_AUTOSUGGEST_USE_ASYNC variable. See GitHub issue #498. --- README.md | 6 ++++-- spec/options/use_async_spec.rb | 7 ------- src/start.zsh | 8 ++++++++ zsh-autosuggestions.zsh | 8 ++++++++ 4 files changed, 20 insertions(+), 9 deletions(-) delete mode 100644 spec/options/use_async_spec.rb diff --git a/README.md b/README.md index 06d26e3..90ec32f 100644 --- a/README.md +++ b/README.md @@ -79,9 +79,11 @@ Widgets that modify the buffer and are not found in any of these arrays will fet 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 strings that are too long. -### Enable Asynchronous Mode +### Asynchronous Mode -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). +Suggestions are fetched asynchronously by default in zsh versions 5.0.8 and greater. To disable asynchronous suggestions and fetch them synchronously instead, `unset ZSH_AUTOSUGGEST_USE_ASYNC` after sourcing the plugin. + +Alternatively, if you are using a version of zsh older than 5.0.8 and want to enable asynchronous mode, set the `ZSH_AUTOSUGGEST_USE_ASYNC` variable after sourcing the plugin (it can be set to anything). Note that there is [a bug](https://github.com/zsh-users/zsh-autosuggestions/issues/364#issuecomment-481423232) in versions of zsh older than 5.0.8 where ctrl + c will fail to reset the prompt immediately after fetching a suggestion asynchronously. ### Disabling automatic widget re-binding diff --git a/spec/options/use_async_spec.rb b/spec/options/use_async_spec.rb deleted file mode 100644 index 420dcc3..0000000 --- a/spec/options/use_async_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -describe 'suggestion fetching' do - it 'is performed synchronously' - - context 'when ZSH_AUTOSUGGEST_USE_ASYNC is set' do - it 'is performed asynchronously' - end -end diff --git a/src/start.zsh b/src/start.zsh index 221e932..5d4ee52 100644 --- a/src/start.zsh +++ b/src/start.zsh @@ -21,5 +21,13 @@ _zsh_autosuggest_start() { # Mark for auto-loading the functions that we use autoload -Uz add-zsh-hook is-at-least +# Automatically enable asynchronous mode in newer versions of zsh. Disable for +# older versions because there is a bug when using async mode where ^C does not +# work immediately after fetching a suggestion. +# See https://github.com/zsh-users/zsh-autosuggestions/issues/364 +if is-at-least 5.0.8; then + typeset -g ZSH_AUTOSUGGEST_USE_ASYNC= +fi + # Start the autosuggestion widgets on the next precmd add-zsh-hook precmd _zsh_autosuggest_start diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 3277dbb..7f43f10 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -854,5 +854,13 @@ _zsh_autosuggest_start() { # Mark for auto-loading the functions that we use autoload -Uz add-zsh-hook is-at-least +# Automatically enable asynchronous mode in newer versions of zsh. Disable for +# older versions because there is a bug when using async mode where ^C does not +# work immediately after fetching a suggestion. +# See https://github.com/zsh-users/zsh-autosuggestions/issues/364 +if is-at-least 5.0.8; then + typeset -g ZSH_AUTOSUGGEST_USE_ASYNC= +fi + # Start the autosuggestion widgets on the next precmd add-zsh-hook precmd _zsh_autosuggest_start