From f543ba08c33453c30d36195ce6fc91b6514369c9 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Wed, 17 Apr 2019 23:30:48 -0600 Subject: [PATCH] Fix cr/lf handling in completion strategy --- spec/strategies/completion_spec.rb | 12 ++++++++++++ src/strategies/completion.zsh | 10 ++++++++++ zsh-autosuggestions.zsh | 10 ++++++++++ 3 files changed, 32 insertions(+) diff --git a/spec/strategies/completion_spec.rb b/spec/strategies/completion_spec.rb index 1b4002b..e8cc8ce 100644 --- a/spec/strategies/completion_spec.rb +++ b/spec/strategies/completion_spec.rb @@ -14,6 +14,12 @@ describe 'the `completion` suggestion strategy' do wait_for { session.content }.to eq('baz bar') end + it 'does not add extra carriage returns when prefix has a line feed' do + skip '`stty` does not work inside zpty below zsh version 5.0.3' if session.zsh_version < Gem::Version.new('5.0.3') + session.send_string('baz \\').send_keys('C-v', 'C-j') + wait_for { session.content }.to eq("baz \\\nbar") + end + context 'when async mode is enabled' do let(:options) { ['ZSH_AUTOSUGGEST_USE_ASYNC=true', 'ZSH_AUTOSUGGEST_STRATEGY=completion'] } @@ -21,6 +27,12 @@ describe 'the `completion` suggestion strategy' do session.send_string('baz ') wait_for { session.content }.to eq('baz bar') end + + it 'does not add extra carriage returns when prefix has a line feed' do + skip '`stty` does not work inside zpty below zsh version 5.0.3' if session.zsh_version < Gem::Version.new('5.0.3') + session.send_string('baz \\').send_keys('C-v', 'C-j') + wait_for { session.content }.to eq("baz \\\nbar") + end end end diff --git a/src/strategies/completion.zsh b/src/strategies/completion.zsh index 06edc20..4c60e90 100644 --- a/src/strategies/completion.zsh +++ b/src/strategies/completion.zsh @@ -28,6 +28,16 @@ _zsh_autosuggest_capture_completion_widget() { # after autosuggestions is initialized. zle -- ${(k)widgets[(r)completion:.complete-word:_main_complete]} + if is-at-least 5.0.3; then + # Don't do any cr/lf transformations. We need to do this immediately before + # output because if we do it in setup, onlcr will be re-enabled when we enter + # vared in the async code path. There is a bug in zpty module in older versions + # where the tty is not properly attached to the pty slave, resulting in stty + # getting stopped with a SIGTTOU. See zsh-workers thread 31660 and upstream + # commit f75904a38 + stty -onlcr -ocrnl -F /dev/tty + fi + # The completion has been added, print the buffer as the suggestion echo -nE - $'\0'$BUFFER$'\0' } diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 11ff7bd..5f2401d 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -510,6 +510,16 @@ _zsh_autosuggest_capture_completion_widget() { # after autosuggestions is initialized. zle -- ${(k)widgets[(r)completion:.complete-word:_main_complete]} + if is-at-least 5.0.3; then + # Don't do any cr/lf transformations. We need to do this immediately before + # output because if we do it in setup, onlcr will be re-enabled when we enter + # vared in the async code path. There is a bug in zpty module in older versions + # where the tty is not properly attached to the pty slave, resulting in stty + # getting stopped with a SIGTTOU. See zsh-workers thread 31660 and upstream + # commit f75904a38 + stty -onlcr -ocrnl -F /dev/tty + fi + # The completion has been added, print the buffer as the suggestion echo -nE - $'\0'$BUFFER$'\0' }