Improved criteria used to popup autosuggestions

This commit is contained in:
Thiago de Arruda
2013-10-29 10:46:39 -03:00
parent 4ca0501c25
commit 2d67624963
3 changed files with 74 additions and 51 deletions

View File

@@ -2,20 +2,25 @@
# Based on:
# https://github.com/Valodim/zsh-capture-completion/blob/master/capture.zsh
# close stdio
exec &> /dev/null
if [[ -n $ZLE_AUTOSUGGEST_SERVER_LOG ]]; then
exec &>> "$HOME/.autosuggest-server.log"
else
exec &> /dev/null
fi
exec < /dev/null
zmodload zsh/zpty
zmodload zsh/net/socket
setopt noglob
print "autosuggestion server started, pid: $$"
# Start an interactive zsh connected to a zpty
zpty z ZLE_DISABLE_AUTOSUGGEST=1 zsh -i
print 'interactive shell started'
# Source the init script
zpty -w z "source '${0:a:h}/completion-server-init.zsh'"
# read all completions and return the longest match
# read everything until a line containing the byte 0 is found
read-to-null() {
while zpty -r z chunk; do
[[ $chunk == *$'\0'* ]] && break
@@ -26,6 +31,7 @@ read-to-null() {
# wait for ok from shell
read-to-null &> /dev/null
print 'interactive shell ready'
# listen on a socket for completion requests
server_dir=$1
@@ -34,7 +40,9 @@ socket_path=$3
cleanup() {
print 'removing socket and pid file...'
rm -f $socket_path $pid_file
print "autosuggestion server stopped, pid: $$"
}
trap cleanup TERM INT HUP EXIT
@@ -47,14 +55,18 @@ while ! zsocket -l $socket_path; do
else
exit 1
fi
print "will retry listening on '$socket_path'"
done
print $$ > $pid_file
server=$REPLY
print "server listening on '$socket_path'"
print $$ > $pid_file
while zsocket -a $server &> /dev/null; do
connection=$REPLY
print "connection accepted, fd: $connection"
# connection accepted, read the request and send response
while read -u $connection prefix &> /dev/null; do
# send the prefix to be completed followed by a TAB to force
@@ -63,7 +75,7 @@ while zsocket -a $server &> /dev/null; do
zpty -r z chunk &> /dev/null # read empty line before completions
local current=''
# read completions one by one, storing the longest match
read-to-null | while read line; do
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
@@ -75,6 +87,7 @@ while zsocket -a $server &> /dev/null; do
fi
# close fd
exec {connection}>&-
print "connection closed, fd: $connection"
# clear input buffer
zpty -w z $'\n'
done