2013-10-29 06:51:04 +08:00
|
|
|
#!/usr/bin/env zsh
|
|
|
|
# Based on:
|
|
|
|
# https://github.com/Valodim/zsh-capture-completion/blob/master/capture.zsh
|
|
|
|
|
2013-10-29 21:46:39 +08:00
|
|
|
if [[ -n $ZLE_AUTOSUGGEST_SERVER_LOG ]]; then
|
|
|
|
exec &>> "$HOME/.autosuggest-server.log"
|
|
|
|
else
|
|
|
|
exec &> /dev/null
|
|
|
|
fi
|
2013-10-29 16:42:03 +08:00
|
|
|
exec < /dev/null
|
2013-10-29 06:51:04 +08:00
|
|
|
|
|
|
|
zmodload zsh/zpty
|
|
|
|
zmodload zsh/net/socket
|
|
|
|
setopt noglob
|
2013-10-29 21:46:39 +08:00
|
|
|
print "autosuggestion server started, pid: $$"
|
2013-10-29 06:51:04 +08:00
|
|
|
|
|
|
|
# Start an interactive zsh connected to a zpty
|
|
|
|
zpty z ZLE_DISABLE_AUTOSUGGEST=1 zsh -i
|
2013-10-29 21:46:39 +08:00
|
|
|
print 'interactive shell started'
|
2013-10-29 06:51:04 +08:00
|
|
|
# Source the init script
|
|
|
|
zpty -w z "source '${0:a:h}/completion-server-init.zsh'"
|
|
|
|
|
2013-10-29 21:46:39 +08:00
|
|
|
# read everything until a line containing the byte 0 is found
|
2013-10-29 06:51:04 +08:00
|
|
|
read-to-null() {
|
|
|
|
while zpty -r z chunk; do
|
|
|
|
[[ $chunk == *$'\0'* ]] && break
|
2013-10-29 19:26:59 +08:00
|
|
|
[[ $chunk != $'\1'* ]] && continue # ignore what doesnt start with '1'
|
|
|
|
print -n - ${chunk:1}
|
2013-10-29 06:51:04 +08:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# wait for ok from shell
|
2013-10-29 16:42:03 +08:00
|
|
|
read-to-null &> /dev/null
|
2013-10-29 21:46:39 +08:00
|
|
|
print 'interactive shell ready'
|
2013-10-29 06:51:04 +08:00
|
|
|
|
2013-10-29 16:42:03 +08:00
|
|
|
# listen on a socket for completion requests
|
2013-10-29 06:51:04 +08:00
|
|
|
server_dir=$1
|
|
|
|
pid_file=$2
|
|
|
|
socket_path=$3
|
|
|
|
|
|
|
|
|
|
|
|
cleanup() {
|
2013-10-29 21:46:39 +08:00
|
|
|
print 'removing socket and pid file...'
|
2013-10-29 16:42:03 +08:00
|
|
|
rm -f $socket_path $pid_file
|
2013-10-29 21:46:39 +08:00
|
|
|
print "autosuggestion server stopped, pid: $$"
|
2013-10-29 06:51:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
trap cleanup TERM INT HUP EXIT
|
|
|
|
|
2013-10-29 16:42:03 +08:00
|
|
|
mkdir -m 700 $server_dir &> /dev/null
|
2013-10-29 06:51:04 +08:00
|
|
|
|
|
|
|
while ! zsocket -l $socket_path; do
|
|
|
|
if [[ ! -r $pid_file ]] || ! kill -0 $(<$pid_file) &> /dev/null; then
|
|
|
|
rm -f $socket_path
|
|
|
|
else
|
|
|
|
exit 1
|
|
|
|
fi
|
2013-10-29 21:46:39 +08:00
|
|
|
print "will retry listening on '$socket_path'"
|
2013-10-29 06:51:04 +08:00
|
|
|
done
|
|
|
|
|
|
|
|
server=$REPLY
|
|
|
|
|
2013-10-29 21:46:39 +08:00
|
|
|
print "server listening on '$socket_path'"
|
|
|
|
|
|
|
|
print $$ > $pid_file
|
|
|
|
|
2013-10-29 06:51:04 +08:00
|
|
|
while zsocket -a $server &> /dev/null; do
|
|
|
|
connection=$REPLY
|
2013-10-29 21:46:39 +08:00
|
|
|
print "connection accepted, fd: $connection"
|
2013-10-29 06:51:04 +08:00
|
|
|
# connection accepted, read the request and send response
|
|
|
|
while read -u $connection prefix &> /dev/null; do
|
2013-10-29 16:42:03 +08:00
|
|
|
# send the prefix to be completed followed by a TAB to force
|
|
|
|
# completion
|
2013-10-29 06:51:04 +08:00
|
|
|
zpty -w -n z $prefix$'\t'
|
|
|
|
zpty -r z chunk &> /dev/null # read empty line before completions
|
2013-10-29 16:42:03 +08:00
|
|
|
local current=''
|
|
|
|
# read completions one by one, storing the longest match
|
2013-10-29 21:46:39 +08:00
|
|
|
read-to-null | while IFS= read -r line; do
|
2013-10-29 16:42:03 +08:00
|
|
|
(( $#line > $#current )) && current=$line
|
|
|
|
done
|
|
|
|
# send the longest completion back to the client, strip the last
|
|
|
|
# non-printable character
|
|
|
|
if (( $#current )); then
|
2013-10-29 19:17:03 +08:00
|
|
|
print -u $connection - ${current:0:-1}
|
2013-10-29 16:42:03 +08:00
|
|
|
else
|
|
|
|
print -u $connection ''
|
|
|
|
fi
|
|
|
|
# close fd
|
2013-10-29 06:51:04 +08:00
|
|
|
exec {connection}>&-
|
2013-10-29 21:46:39 +08:00
|
|
|
print "connection closed, fd: $connection"
|
2013-10-29 16:42:03 +08:00
|
|
|
# clear input buffer
|
2013-10-29 06:51:04 +08:00
|
|
|
zpty -w z $'\n'
|
|
|
|
done
|
|
|
|
done
|