2013-10-28 23:51:04 +01:00
|
|
|
#!/usr/bin/env zsh
|
|
|
|
# Based on:
|
|
|
|
# https://github.com/Valodim/zsh-capture-completion/blob/master/capture.zsh
|
|
|
|
|
2013-10-29 16:01:18 +01:00
|
|
|
# read everything until a line containing the byte 0 is found
|
|
|
|
read-to-null() {
|
|
|
|
while zpty -r z chunk; do
|
|
|
|
[[ $chunk == *$'\0'* ]] && break
|
|
|
|
[[ $chunk != $'\1'* ]] && continue # ignore what doesnt start with '1'
|
|
|
|
print -n - ${chunk:1}
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
accept-connection() {
|
|
|
|
zsocket -a $server
|
|
|
|
fds[$REPLY]=1
|
2013-10-30 12:36:53 +01:00
|
|
|
print "connection accepted, fd: $REPLY" >&2
|
2013-10-29 16:01:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
handle-request() {
|
|
|
|
local connection=$1 current line
|
|
|
|
integer read_something=0
|
2013-10-29 23:01:31 +01:00
|
|
|
print "request received from fd $connection"
|
2013-10-29 16:01:18 +01:00
|
|
|
while read -u $connection prefix &> /dev/null; do
|
|
|
|
read_something=1
|
|
|
|
# send the prefix to be completed followed by a TAB to force
|
|
|
|
# completion
|
|
|
|
zpty -w -n z $prefix$'\t'
|
|
|
|
zpty -r z chunk &> /dev/null # read empty line before completions
|
|
|
|
current=''
|
|
|
|
# read completions one by one, storing the longest match
|
|
|
|
read-to-null | while IFS= read -r line; do
|
|
|
|
(( $#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 18:27:42 +01:00
|
|
|
print -u $connection - $prefix$'\2'${current:0:-1}
|
|
|
|
else
|
|
|
|
print -u $connection ''
|
2013-10-29 16:01:18 +01:00
|
|
|
fi
|
|
|
|
# clear input buffer
|
|
|
|
zpty -w z $'\n'
|
|
|
|
break # handle more requests/return to zselect
|
|
|
|
done
|
|
|
|
if ! (( read_something )); then
|
2013-10-30 12:36:53 +01:00
|
|
|
print "connection with fd $connection closed" >&2
|
2013-10-29 16:01:18 +01:00
|
|
|
unset fds[$connection]
|
|
|
|
exec {connection}>&- # free the file descriptor
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-29 14:46:39 +01:00
|
|
|
if [[ -n $ZLE_AUTOSUGGEST_SERVER_LOG ]]; then
|
2013-10-29 16:01:18 +01:00
|
|
|
exec >> "$HOME/.autosuggest-server.log"
|
|
|
|
else
|
|
|
|
exec > /dev/null
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -n $ZLE_AUTOSUGGEST_SERVER_LOG_ERRORS ]]; then
|
|
|
|
exec 2>> "$HOME/.autosuggest-server-errors.log"
|
2013-10-29 14:46:39 +01:00
|
|
|
else
|
2013-10-29 16:01:18 +01:00
|
|
|
exec 2> /dev/null
|
2013-10-29 14:46:39 +01:00
|
|
|
fi
|
2013-10-29 16:01:18 +01:00
|
|
|
|
2013-10-29 09:42:03 +01:00
|
|
|
exec < /dev/null
|
2013-10-28 23:51:04 +01:00
|
|
|
|
|
|
|
zmodload zsh/zpty
|
2013-10-29 16:01:18 +01:00
|
|
|
zmodload zsh/zselect
|
|
|
|
zmodload zsh/net/socket
|
2013-10-28 23:51:04 +01:00
|
|
|
setopt noglob
|
2013-10-30 12:36:53 +01:00
|
|
|
print "autosuggestion server started, pid: $$" >&2
|
2013-10-28 23:51:04 +01:00
|
|
|
|
|
|
|
# Start an interactive zsh connected to a zpty
|
|
|
|
zpty z ZLE_DISABLE_AUTOSUGGEST=1 zsh -i
|
2013-10-29 14:46:39 +01:00
|
|
|
print 'interactive shell started'
|
2013-10-28 23:51:04 +01:00
|
|
|
# Source the init script
|
2014-03-09 18:45:31 +01:00
|
|
|
zpty -w z "source '${0:a:h}/completion-server-init.zsh'"
|
2013-10-28 23:51:04 +01:00
|
|
|
|
|
|
|
# wait for ok from shell
|
2013-10-29 09:42:03 +01:00
|
|
|
read-to-null &> /dev/null
|
2013-10-29 14:46:39 +01:00
|
|
|
print 'interactive shell ready'
|
2013-10-28 23:51:04 +01:00
|
|
|
|
2013-10-29 09:42:03 +01:00
|
|
|
# listen on a socket for completion requests
|
2013-10-28 23:51:04 +01:00
|
|
|
server_dir=$1
|
|
|
|
pid_file=$2
|
|
|
|
socket_path=$3
|
|
|
|
|
|
|
|
|
|
|
|
cleanup() {
|
2013-10-29 14:46:39 +01:00
|
|
|
print 'removing socket and pid file...'
|
2013-10-29 09:42:03 +01:00
|
|
|
rm -f $socket_path $pid_file
|
2013-10-29 14:46:39 +01:00
|
|
|
print "autosuggestion server stopped, pid: $$"
|
2013-10-29 16:01:18 +01:00
|
|
|
exit
|
2013-10-28 23:51:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
trap cleanup TERM INT HUP EXIT
|
|
|
|
|
2013-10-30 12:36:53 +01:00
|
|
|
mkdir -m 700 $server_dir
|
2013-10-28 23:51:04 +01:00
|
|
|
|
|
|
|
while ! zsocket -l $socket_path; do
|
2013-10-30 12:36:53 +01:00
|
|
|
if [[ ! -r $pid_file ]] || ! kill -0 $(<$pid_file); then
|
2013-10-28 23:51:04 +01:00
|
|
|
rm -f $socket_path
|
|
|
|
else
|
|
|
|
exit 1
|
|
|
|
fi
|
2013-10-29 14:46:39 +01:00
|
|
|
print "will retry listening on '$socket_path'"
|
2013-10-28 23:51:04 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
server=$REPLY
|
|
|
|
|
2013-10-29 14:46:39 +01:00
|
|
|
print "server listening on '$socket_path'"
|
|
|
|
|
|
|
|
print $$ > $pid_file
|
|
|
|
|
2013-10-29 16:01:18 +01:00
|
|
|
typeset -A fds ready
|
|
|
|
fds[$server]=1
|
|
|
|
|
|
|
|
while zselect -A ready ${(k)fds}; do
|
|
|
|
queue=(${(k)ready})
|
|
|
|
for fd in $queue; do
|
|
|
|
if (( fd == server )); then
|
|
|
|
accept-connection
|
2013-10-29 09:42:03 +01:00
|
|
|
else
|
2013-10-29 16:01:18 +01:00
|
|
|
handle-request $fd
|
2013-10-29 09:42:03 +01:00
|
|
|
fi
|
2013-10-28 23:51:04 +01:00
|
|
|
done
|
|
|
|
done
|