2013-10-29 06:51:04 +08:00
|
|
|
#!/usr/bin/env zsh
|
|
|
|
zmodload zsh/net/socket
|
2013-10-29 16:42:03 +08:00
|
|
|
|
2013-10-29 06:51:04 +08:00
|
|
|
AUTOSUGGEST_SERVER_SCRIPT="${0:a:h}/completion-server.zsh"
|
|
|
|
|
2013-10-29 16:42:03 +08: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-08 05:42:17 +08:00
|
|
|
if [[ ! -d $server_dir || ! -r $pid_file ]] || ! kill -0 $(<$pid_file) &> /dev/null; then
|
2013-10-29 21:46:39 +08: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-29 06:51:04 +08:00
|
|
|
|
2013-10-29 23:01:18 +08:00
|
|
|
autosuggest-server-connect
|
|
|
|
}
|
|
|
|
|
|
|
|
autosuggest-server-connect() {
|
|
|
|
unset ZLE_AUTOSUGGEST_CONNECTION
|
|
|
|
|
2013-10-29 16:42:03 +08:00
|
|
|
integer remaining_tries=10
|
2013-10-29 23:01:18 +08:00
|
|
|
while (( --remaining_tries )) && ! zsocket $socket_path &>/dev/null; do
|
2013-10-29 16:42:03 +08:00
|
|
|
sleep 0.3
|
|
|
|
done
|
2013-10-29 23:01:18 +08:00
|
|
|
|
|
|
|
[[ -z $REPLY ]] && return 1
|
|
|
|
|
|
|
|
ZLE_AUTOSUGGEST_CONNECTION=$REPLY
|
2013-10-29 16:42:03 +08:00
|
|
|
}
|
2013-10-29 06:51:04 +08:00
|
|
|
|
2013-11-08 05:42:17 +08:00
|
|
|
autosuggest-send-request() {
|
2013-10-30 01:27:42 +08:00
|
|
|
[[ -z $ZLE_AUTOSUGGEST_CONNECTION ]] && return 1
|
2013-10-29 23:01:18 +08:00
|
|
|
setopt local_options noglob
|
|
|
|
print -u $ZLE_AUTOSUGGEST_CONNECTION - $1 &> /dev/null || return 1
|
2013-10-29 16:42:03 +08:00
|
|
|
}
|