From 16666da4884a334ccb4c457c63be3f9d0fa00fec Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Thu, 26 Jan 2017 16:50:19 -0700 Subject: [PATCH] Handle versions of zsh where zpty does not set REPLY to fd of opened pty Based on https://github.com/mafredri/zsh-async/blob/e702ec4697ab6469b8948d599768227304d146b1/async.zsh#L400-L406 --- src/async.zsh | 16 ++++++++++++++-- zsh-autosuggestions.zsh | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/async.zsh b/src/async.zsh index 81b3268..eadd9c7 100644 --- a/src/async.zsh +++ b/src/async.zsh @@ -52,14 +52,26 @@ _zsh_autosuggest_async_recreate_pty() { zpty -d $ZSH_AUTOSUGGEST_PTY_NAME &>/dev/null fi - # REPLY stores the fd to read from + # With newer versions of zsh, REPLY stores the fd to read from typeset -h REPLY + # If we won't get a fd back from zpty, try to guess it + if [ $_ZSH_AUTOSUGGEST_ZPTY_RETURNS_FD -eq 0 ]; then + integer -l zptyfd + exec {zptyfd}>&1 # Open a new file descriptor (above 10). + exec {zptyfd}>&- # Close it so it's free to be used by zpty. + fi + # Start a new pty running the server function zpty -b $ZSH_AUTOSUGGEST_PTY_NAME "_zsh_autosuggest_async_suggestion_server _zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY" # Store the fd so we can destroy this pty later - _ZSH_AUTOSUGGEST_PTY_FD=$REPLY + if (( REPLY )); then + _ZSH_AUTOSUGGEST_PTY_FD=$REPLY + else + _ZSH_AUTOSUGGEST_PTY_FD=$zptyfd + fi + # Set up input handler from the pty zle -F $_ZSH_AUTOSUGGEST_PTY_FD _zsh_autosuggest_async_suggestion_ready diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index efdf6d2..64bfd34 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -566,14 +566,26 @@ _zsh_autosuggest_async_recreate_pty() { zpty -d $ZSH_AUTOSUGGEST_PTY_NAME &>/dev/null fi - # REPLY stores the fd to read from + # With newer versions of zsh, REPLY stores the fd to read from typeset -h REPLY + # If we won't get a fd back from zpty, try to guess it + if [ $_ZSH_AUTOSUGGEST_ZPTY_RETURNS_FD -eq 0 ]; then + integer -l zptyfd + exec {zptyfd}>&1 # Open a new file descriptor (above 10). + exec {zptyfd}>&- # Close it so it's free to be used by zpty. + fi + # Start a new pty running the server function zpty -b $ZSH_AUTOSUGGEST_PTY_NAME "_zsh_autosuggest_async_suggestion_server _zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY" # Store the fd so we can destroy this pty later - _ZSH_AUTOSUGGEST_PTY_FD=$REPLY + if (( REPLY )); then + _ZSH_AUTOSUGGEST_PTY_FD=$REPLY + else + _ZSH_AUTOSUGGEST_PTY_FD=$zptyfd + fi + # Set up input handler from the pty zle -F $_ZSH_AUTOSUGGEST_PTY_FD _zsh_autosuggest_async_suggestion_ready