2016-07-20 05:04:18 +02:00
|
|
|
|
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Async #
|
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
|
2017-02-17 03:18:03 +01:00
|
|
|
# Zpty process is spawned running this function
|
2017-01-27 23:18:26 +01:00
|
|
|
_zsh_autosuggest_async_server() {
|
2017-01-25 03:58:25 +01:00
|
|
|
emulate -R zsh
|
|
|
|
|
2017-02-17 03:18:03 +01:00
|
|
|
# There is a bug in zpty module (fixed in zsh/master) 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
|
|
|
|
# process immediately, before it has a chance to kill any other
|
|
|
|
# zpty processes.
|
|
|
|
zshexit() {
|
|
|
|
kill -KILL $$
|
|
|
|
sleep 1 # Block for long enough for the signal to come through
|
|
|
|
}
|
|
|
|
|
2017-01-25 06:27:09 +01:00
|
|
|
# Output only newlines (not carriage return + newline)
|
|
|
|
stty -onlcr
|
|
|
|
|
2017-02-17 03:27:32 +01:00
|
|
|
# Silence any error messages
|
|
|
|
exec 2>/dev/null
|
|
|
|
|
2017-01-25 06:48:30 +01:00
|
|
|
local strategy=$1
|
2017-01-25 08:00:53 +01:00
|
|
|
local last_pid
|
2017-01-25 06:48:30 +01:00
|
|
|
|
2017-01-27 23:18:26 +01:00
|
|
|
while IFS='' read -r -d $'\0' query; do
|
2016-07-20 05:04:18 +02:00
|
|
|
# Kill last bg process
|
2017-01-25 08:00:53 +01:00
|
|
|
kill -KILL $last_pid &>/dev/null
|
2016-07-20 05:04:18 +02:00
|
|
|
|
|
|
|
# Run suggestion search in the background
|
2017-01-27 23:18:26 +01:00
|
|
|
(
|
|
|
|
local suggestion
|
|
|
|
_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY "$query"
|
|
|
|
echo -n -E "$suggestion"$'\0'
|
|
|
|
) &
|
2017-01-25 08:00:53 +01:00
|
|
|
|
|
|
|
last_pid=$!
|
2016-07-20 05:04:18 +02:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2017-01-27 23:18:26 +01:00
|
|
|
_zsh_autosuggest_async_request() {
|
2017-02-17 03:18:03 +01:00
|
|
|
# Write the query to the zpty process to fetch a suggestion
|
2017-01-29 18:39:07 +01:00
|
|
|
zpty -w -n $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME "${1}"$'\0'
|
2017-01-27 23:18:26 +01:00
|
|
|
}
|
|
|
|
|
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
|
2017-01-27 23:18:26 +01:00
|
|
|
_zsh_autosuggest_async_response() {
|
2017-02-17 03:18:03 +01:00
|
|
|
setopt LOCAL_OPTIONS EXTENDED_GLOB
|
|
|
|
|
2017-01-25 03:58:52 +01:00
|
|
|
local suggestion
|
|
|
|
|
2017-01-29 18:39:07 +01:00
|
|
|
zpty -rt $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME suggestion '*'$'\0' 2>/dev/null
|
2017-02-19 00:51:53 +01:00
|
|
|
zle autosuggest-suggest -- "${suggestion%%$'\0'##}"
|
2016-07-20 05:04:18 +02:00
|
|
|
}
|
|
|
|
|
2017-01-27 23:18:26 +01:00
|
|
|
_zsh_autosuggest_async_pty_create() {
|
2017-01-27 00:50:19 +01:00
|
|
|
# With newer versions of zsh, 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
|
|
|
|
2017-01-27 00:50:19 +01:00
|
|
|
# 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
|
|
|
|
|
2017-02-17 03:18:03 +01:00
|
|
|
# Fork a zpty process running the server function
|
2017-01-29 18:39:07 +01:00
|
|
|
zpty -b $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME "_zsh_autosuggest_async_server _zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY"
|
2017-01-25 03:53:26 +01:00
|
|
|
|
2017-01-27 01:00:56 +01:00
|
|
|
# Store the fd so we can remove the handler later
|
2017-01-27 00:50:19 +01:00
|
|
|
if (( REPLY )); then
|
|
|
|
_ZSH_AUTOSUGGEST_PTY_FD=$REPLY
|
|
|
|
else
|
|
|
|
_ZSH_AUTOSUGGEST_PTY_FD=$zptyfd
|
|
|
|
fi
|
|
|
|
|
2017-02-17 03:18:03 +01:00
|
|
|
# Set up input handler from the zpty
|
2017-01-27 23:18:26 +01:00
|
|
|
zle -F $_ZSH_AUTOSUGGEST_PTY_FD _zsh_autosuggest_async_response
|
|
|
|
}
|
|
|
|
|
|
|
|
_zsh_autosuggest_async_pty_destroy() {
|
2017-02-18 06:40:23 +01:00
|
|
|
if zpty -t $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME &>/dev/null; then
|
2017-01-27 23:18:26 +01:00
|
|
|
# Remove the input handler
|
2017-02-18 06:40:23 +01:00
|
|
|
zle -F $_ZSH_AUTOSUGGEST_PTY_FD &>/dev/null
|
2017-01-27 23:18:26 +01:00
|
|
|
|
2017-02-17 03:18:03 +01:00
|
|
|
# Destroy the zpty
|
2017-01-29 18:39:07 +01:00
|
|
|
zpty -d $ZSH_AUTOSUGGEST_ASYNC_PTY_NAME &>/dev/null
|
2017-01-27 23:18:26 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
_zsh_autosuggest_async_pty_recreate() {
|
|
|
|
_zsh_autosuggest_async_pty_destroy
|
|
|
|
_zsh_autosuggest_async_pty_create
|
|
|
|
}
|
|
|
|
|
|
|
|
_zsh_autosuggest_async_start() {
|
|
|
|
typeset -g _ZSH_AUTOSUGGEST_PTY_FD
|
|
|
|
|
2017-02-18 19:25:27 +01:00
|
|
|
_zsh_autosuggest_feature_detect_zpty_returns_fd
|
2017-02-18 06:40:23 +01:00
|
|
|
_zsh_autosuggest_async_pty_recreate
|
2017-01-27 23:18:26 +01:00
|
|
|
|
|
|
|
# We recreate the pty to get a fresh list of history events
|
|
|
|
add-zsh-hook precmd _zsh_autosuggest_async_pty_recreate
|
2016-07-20 05:04:18 +02:00
|
|
|
}
|