2013-10-28 22:51:04 +00:00
|
|
|
#!/usr/bin/env zsh
|
|
|
|
zmodload zsh/net/socket
|
2013-10-29 08:42:03 +00:00
|
|
|
|
2013-10-28 22:51:04 +00:00
|
|
|
AUTOSUGGEST_SERVER_SCRIPT="${0:a:h}/completion-server.zsh"
|
|
|
|
|
2013-10-29 08:42:03 +00:00
|
|
|
autosuggest-ensure-server() {
|
|
|
|
setopt local_options no_hup
|
|
|
|
local server_dir="/tmp/zsh-autosuggest-$USER"
|
|
|
|
local pid_file="$server_dir/pid"
|
|
|
|
local socket_path="$server_dir/socket"
|
|
|
|
|
2013-10-29 13:46:39 +00:00
|
|
|
if [[ ! -S $socket_path || ! -r $pid_file ]] || ! kill -0 $(<$pid_file) &> /dev/null; then
|
|
|
|
if which setsid &> /dev/null; then
|
|
|
|
setsid zsh $AUTOSUGGEST_SERVER_SCRIPT $server_dir $pid_file $socket_path &!
|
|
|
|
else
|
|
|
|
zsh $AUTOSUGGEST_SERVER_SCRIPT $server_dir $pid_file $socket_path &!
|
|
|
|
fi
|
|
|
|
fi
|
2013-10-28 22:51:04 +00:00
|
|
|
|
2013-10-29 08:42:03 +00:00
|
|
|
integer remaining_tries=10
|
|
|
|
# wait until the process is listening
|
|
|
|
while ! [[ -d $server_dir && -r $pid_file ]] ||\
|
|
|
|
! kill -0 $(<$pid_file) &> /dev/null && (( --remaining_tries )); do
|
|
|
|
sleep 0.3
|
|
|
|
done
|
|
|
|
ZLE_AUTOSUGGEST_SOCKET=$socket_path
|
|
|
|
}
|
2013-10-28 22:51:04 +00:00
|
|
|
|
|
|
|
|
2013-10-29 08:42:03 +00:00
|
|
|
autosuggest-first-completion() {
|
|
|
|
zsocket $ZLE_AUTOSUGGEST_SOCKET &>/dev/null || return 1
|
|
|
|
local connection=$REPLY
|
|
|
|
local completion
|
2013-10-29 11:17:03 +00:00
|
|
|
print -u $connection - $1
|
2013-10-29 13:46:39 +00:00
|
|
|
while IFS= read -r -u $connection completion; do
|
2013-10-29 11:17:03 +00:00
|
|
|
print - ${completion}
|
2013-10-29 08:42:03 +00:00
|
|
|
done
|
|
|
|
# close fd
|
|
|
|
exec {connection}>&-
|
|
|
|
}
|