2016-07-20 05:04:18 +02:00
|
|
|
|
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Async #
|
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
|
2018-06-13 07:45:29 +02:00
|
|
|
zmodload zsh/system
|
|
|
|
|
2018-06-11 10:06:18 +02:00
|
|
|
_zsh_autosuggest_async_request() {
|
2018-06-13 07:45:29 +02:00
|
|
|
typeset -g _ZSH_AUTOSUGGEST_ASYNC_FD _ZSH_AUTOSUGGEST_CHILD_PID
|
2016-07-20 05:04:18 +02:00
|
|
|
|
2018-06-13 07:45:29 +02:00
|
|
|
# If we've got a pending request, cancel it
|
2018-06-11 10:06:18 +02:00
|
|
|
if [[ -n "$_ZSH_AUTOSUGGEST_ASYNC_FD" ]] && { true <&$_ZSH_AUTOSUGGEST_ASYNC_FD } 2>/dev/null; then
|
2018-07-13 19:25:59 +02:00
|
|
|
# Close the file descriptor and remove the handler
|
2018-06-11 10:06:18 +02:00
|
|
|
exec {_ZSH_AUTOSUGGEST_ASYNC_FD}<&-
|
2018-07-13 19:25:59 +02:00
|
|
|
zle -F $_ZSH_AUTOSUGGEST_ASYNC_FD
|
2018-06-13 07:45:29 +02:00
|
|
|
|
|
|
|
# Assume the child process created a new process group and send
|
|
|
|
# TERM to the group to attempt to kill all descendent processes
|
|
|
|
kill -TERM -$_ZSH_AUTOSUGGEST_CHILD_PID 2>/dev/null
|
2018-06-11 10:06:18 +02:00
|
|
|
fi
|
2017-01-25 08:00:53 +01:00
|
|
|
|
2018-06-11 10:06:18 +02:00
|
|
|
# Fork a process to fetch a suggestion and open a pipe to read from it
|
|
|
|
exec {_ZSH_AUTOSUGGEST_ASYNC_FD}< <(
|
2018-06-13 07:45:29 +02:00
|
|
|
# Tell parent process our pid
|
|
|
|
echo $sysparams[pid]
|
|
|
|
|
|
|
|
# Fetch and print the suggestion
|
2018-06-11 10:06:18 +02:00
|
|
|
local suggestion
|
|
|
|
_zsh_autosuggest_fetch_suggestion "$1"
|
|
|
|
echo -nE "$suggestion"
|
|
|
|
)
|
2016-07-20 05:04:18 +02:00
|
|
|
|
2018-06-13 07:45:29 +02:00
|
|
|
# Read the pid from the child process
|
|
|
|
read _ZSH_AUTOSUGGEST_CHILD_PID <&$_ZSH_AUTOSUGGEST_ASYNC_FD
|
|
|
|
|
2018-06-11 10:06:18 +02:00
|
|
|
# When the fd is readable, call the response handler
|
|
|
|
zle -F "$_ZSH_AUTOSUGGEST_ASYNC_FD" _zsh_autosuggest_async_response
|
2017-01-27 23:18:26 +01:00
|
|
|
}
|
|
|
|
|
2018-06-11 10:06:18 +02:00
|
|
|
# Called when new data is ready to be read from the pipe
|
2017-01-25 03:53:26 +01:00
|
|
|
# First arg will be fd ready for reading
|
|
|
|
# Second arg will be passed in case of error
|
2017-01-27 23:18:26 +01:00
|
|
|
_zsh_autosuggest_async_response() {
|
2018-07-13 19:26:57 +02:00
|
|
|
if [[ -z "$2" || "$2" == "hup" ]]; then
|
|
|
|
# Read everything from the fd and give it as a suggestion
|
|
|
|
zle autosuggest-suggest -- "$(cat <&$1)"
|
2017-01-27 23:18:26 +01:00
|
|
|
|
2018-07-13 19:26:57 +02:00
|
|
|
# Close the fd
|
|
|
|
exec {1}<&-
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Always remove the handler
|
2018-06-11 10:06:18 +02:00
|
|
|
zle -F "$1"
|
2016-07-20 05:04:18 +02:00
|
|
|
}
|