2016-07-20 05:04:18 +02:00
|
|
|
|
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Async #
|
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
|
|
|
|
_zsh_autosuggest_async_fetch_suggestion() {
|
|
|
|
local strategy_function="_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY"
|
|
|
|
local prefix="$(_zsh_autosuggest_escape_command "$1")"
|
|
|
|
|
|
|
|
# Send the suggestion command to the pty to fetch a suggestion
|
|
|
|
zpty -w -n $ZSH_AUTOSUGGEST_PTY_NAME "$strategy_function '$prefix'"$'\0'
|
|
|
|
}
|
|
|
|
|
2017-01-25 03:53:26 +01:00
|
|
|
# Pty is spawned running this function
|
2017-01-25 03:55:38 +01:00
|
|
|
_zsh_autosuggest_async_suggestion_server() {
|
2016-07-20 05:04:18 +02:00
|
|
|
while read -d $'\0' cmd; do
|
|
|
|
# Kill last bg process
|
2017-01-25 03:56:34 +01:00
|
|
|
kill -KILL %1 &>/dev/null
|
2016-07-20 05:04:18 +02:00
|
|
|
|
|
|
|
# Run suggestion search in the background
|
|
|
|
print -n -- "$(eval "$cmd")"$'\0' &
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2017-01-25 03:53:26 +01:00
|
|
|
# Called when new data is ready to be read from the pty
|
|
|
|
# First arg will be fd ready for reading
|
|
|
|
# Second arg will be passed in case of error
|
2016-07-20 05:04:18 +02:00
|
|
|
_zsh_autosuggest_async_suggestion_ready() {
|
|
|
|
# while zpty -rt $ZSH_AUTOSUGGEST_PTY_NAME suggestion 2>/dev/null; do
|
|
|
|
while read -u $_ZSH_AUTOSUGGEST_PTY_FD -d $'\0' suggestion; do
|
|
|
|
zle _autosuggest-show-suggestion "${suggestion//$'\r'$'\n'/$'\n'}"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Recreate the pty to get a fresh list of history events
|
|
|
|
_zsh_autosuggest_async_recreate_pty() {
|
|
|
|
typeset -g _ZSH_AUTOSUGGEST_PTY_FD
|
|
|
|
|
|
|
|
# Kill the old pty
|
|
|
|
if [ -n "$_ZSH_AUTOSUGGEST_PTY_FD" ]; then
|
2017-01-25 03:53:26 +01:00
|
|
|
# Remove the input handler
|
2016-07-20 05:04:18 +02:00
|
|
|
zle -F $_ZSH_AUTOSUGGEST_PTY_FD
|
2017-01-25 03:53:26 +01:00
|
|
|
|
|
|
|
# Destroy the pty
|
2016-07-20 05:04:18 +02:00
|
|
|
zpty -d $ZSH_AUTOSUGGEST_PTY_NAME &>/dev/null
|
|
|
|
fi
|
|
|
|
|
2017-01-25 03:53:26 +01:00
|
|
|
# REPLY stores the fd to read from
|
2016-07-20 05:04:18 +02:00
|
|
|
typeset -h REPLY
|
2017-01-25 03:53:26 +01:00
|
|
|
|
|
|
|
# Start a new pty running the server function
|
2017-01-25 03:55:38 +01:00
|
|
|
zpty -b $ZSH_AUTOSUGGEST_PTY_NAME _zsh_autosuggest_async_suggestion_server
|
2017-01-25 03:53:26 +01:00
|
|
|
|
|
|
|
# Store the fd so we can destroy this pty later
|
2016-07-20 05:04:18 +02:00
|
|
|
_ZSH_AUTOSUGGEST_PTY_FD=$REPLY
|
2017-01-25 03:53:26 +01:00
|
|
|
|
|
|
|
# Set up input handler from the pty
|
2016-07-20 05:04:18 +02:00
|
|
|
zle -F $_ZSH_AUTOSUGGEST_PTY_FD _zsh_autosuggest_async_suggestion_ready
|
|
|
|
}
|
|
|
|
|
|
|
|
add-zsh-hook precmd _zsh_autosuggest_async_recreate_pty
|