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-11-07 21:42:17 +00:00
|
|
|
if [[ ! -d $server_dir || ! -r $pid_file ]] || ! kill -0 $(<$pid_file) &> /dev/null; then
|
2013-10-29 13:46:39 +00:00
|
|
|
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 15:01:18 +00:00
|
|
|
autosuggest-server-connect
|
|
|
|
}
|
|
|
|
|
|
|
|
autosuggest-server-connect() {
|
|
|
|
unset ZLE_AUTOSUGGEST_CONNECTION
|
|
|
|
|
2013-10-29 08:42:03 +00:00
|
|
|
integer remaining_tries=10
|
2013-10-29 15:01:18 +00:00
|
|
|
while (( --remaining_tries )) && ! zsocket $socket_path &>/dev/null; do
|
2013-10-29 08:42:03 +00:00
|
|
|
sleep 0.3
|
|
|
|
done
|
2013-10-29 15:01:18 +00:00
|
|
|
|
|
|
|
[[ -z $REPLY ]] && return 1
|
|
|
|
|
|
|
|
ZLE_AUTOSUGGEST_CONNECTION=$REPLY
|
2013-10-29 08:42:03 +00:00
|
|
|
}
|
2013-10-28 22:51:04 +00:00
|
|
|
|
2013-11-07 21:42:17 +00:00
|
|
|
autosuggest-send-request() {
|
2013-10-29 17:27:42 +00:00
|
|
|
[[ -z $ZLE_AUTOSUGGEST_CONNECTION ]] && return 1
|
2013-10-29 15:01:18 +00:00
|
|
|
setopt local_options noglob
|
|
|
|
print -u $ZLE_AUTOSUGGEST_CONNECTION - $1 &> /dev/null || return 1
|
2013-10-29 08:42:03 +00:00
|
|
|
}
|