parent
3dbe2c8c9b
commit
20c0ea841b
@ -1,41 +0,0 @@
|
||||
#!/usr/bin/env zsh
|
||||
zmodload zsh/net/socket
|
||||
|
||||
AUTOSUGGEST_SERVER_SCRIPT="${0:a:h}/completion-server.zsh"
|
||||
|
||||
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"
|
||||
|
||||
if [[ ! -S $socket_path || ! -r $pid_file ]] || ! kill -0 $(<$pid_file) &> /dev/null; then
|
||||
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
|
||||
|
||||
autosuggest-server-connect
|
||||
}
|
||||
|
||||
autosuggest-server-connect() {
|
||||
unset ZLE_AUTOSUGGEST_CONNECTION
|
||||
|
||||
integer remaining_tries=10
|
||||
while (( --remaining_tries )) && ! zsocket $socket_path &>/dev/null; do
|
||||
sleep 0.3
|
||||
done
|
||||
|
||||
[[ -z $REPLY ]] && return 1
|
||||
|
||||
ZLE_AUTOSUGGEST_CONNECTION=$REPLY
|
||||
}
|
||||
|
||||
autosuggest-first-completion() {
|
||||
[[ -z $ZLE_AUTOSUGGEST_CONNECTION ]] && return 1
|
||||
setopt local_options noglob
|
||||
local response
|
||||
print -u $ZLE_AUTOSUGGEST_CONNECTION - $1 &> /dev/null || return 1
|
||||
}
|
@ -1,121 +0,0 @@
|
||||
# Based on:
|
||||
# https://github.com/Valodim/zsh-capture-completion/blob/master/.zshrc
|
||||
|
||||
ZLE_DISABLE_AUTOSUGGEST=1
|
||||
# no prompt!
|
||||
PROMPT=
|
||||
|
||||
# load completion system
|
||||
autoload compinit
|
||||
compinit
|
||||
|
||||
# never run a command
|
||||
bindkey '\C-m' .kill-buffer
|
||||
bindkey '\C-j' .kill-buffer
|
||||
bindkey '\C-i' complete-word
|
||||
|
||||
# send an emtpy line before completions are output
|
||||
empty-line() {
|
||||
print
|
||||
# handler needs to reinsert itself after being called
|
||||
compprefuncs+=empty-line
|
||||
}
|
||||
compprefuncs+=empty-line
|
||||
|
||||
# send a line with null-byte after completions are output
|
||||
null-line() {
|
||||
print $'\0'
|
||||
# handler needs to reinsert itself after being called
|
||||
comppostfuncs+=null-line
|
||||
}
|
||||
comppostfuncs+=null-line
|
||||
|
||||
zstyle ':completion:*' completer _complete
|
||||
# never group stuff!
|
||||
zstyle ':completion:*' list-grouped false
|
||||
# don't insert tab when attempting completion on empty line
|
||||
zstyle ':completion:*' insert-tab false
|
||||
# no list separator, this saves some stripping later on
|
||||
zstyle ':completion:*' list-separator ''
|
||||
# dont use matchers
|
||||
zstyle -d ':completion:*' matcher-list
|
||||
# dont format
|
||||
zstyle -d ':completion:*' format
|
||||
# no color formatting
|
||||
zstyle -d ':completion:*' list-colors
|
||||
|
||||
# we use zparseopts
|
||||
zmodload zsh/zutil
|
||||
|
||||
# override compadd (this our hook)
|
||||
compadd () {
|
||||
|
||||
# check if any of -O, -A or -D are given
|
||||
if [[ ${@[1,(i)(-|--)]} == *-(O|A|D)\ * ]]; then
|
||||
# if that is the case, just delegate and leave
|
||||
builtin compadd "$@"
|
||||
return $?
|
||||
fi
|
||||
|
||||
# be careful with namespacing here, we don't want to mess with stuff that
|
||||
# should be passed to compadd!
|
||||
typeset -a __hits __dscr __tmp
|
||||
|
||||
# do we have a description parameter?
|
||||
# note we don't use zparseopts here because of combined option parameters
|
||||
# with arguments like -default- confuse it.
|
||||
if (( $@[(I)-d] )); then # kind of a hack, $+@[(r)-d] doesn't work because of line noise overload
|
||||
# next param after -d
|
||||
__tmp=${@[$[${@[(i)-d]}+1]]}
|
||||
# description can be given as an array parameter name, or inline () array
|
||||
if [[ $__tmp == \(* ]]; then
|
||||
eval "__dscr=$__tmp"
|
||||
else
|
||||
__dscr=( "${(@P)__tmp}" )
|
||||
fi
|
||||
fi
|
||||
|
||||
# capture completions by injecting -A parameter into the compadd call.
|
||||
# this takes care of matching for us.
|
||||
builtin compadd -A __hits -D __dscr "$@"
|
||||
|
||||
# JESUS CHRIST IT TOOK ME FOREVER TO FIGURE OUT THIS OPTION WAS SET AND WAS MESSING WITH MY SHIT HERE
|
||||
setopt localoptions norcexpandparam extendedglob
|
||||
|
||||
# extract prefixes and suffixes from compadd call. we can't do zsh's cool
|
||||
# -r remove-func magic, but it's better than nothing.
|
||||
typeset -A apre hpre hsuf asuf
|
||||
zparseopts -E P:=apre p:=hpre S:=asuf s:=hsuf
|
||||
|
||||
# append / to directories? we are only emulating -f in a half-assed way
|
||||
# here, but it's better than nothing.
|
||||
integer dirsuf=0
|
||||
# don't be fooled by -default- >.>
|
||||
if [[ -z $hsuf && "${${@//-default-/}% -# *}" == *-[[:alnum:]]#f* ]]; then
|
||||
dirsuf=1
|
||||
fi
|
||||
|
||||
# just drop
|
||||
[[ -n $__hits ]] || return
|
||||
|
||||
# this is the point where we have all matches in $__hits and all
|
||||
# descriptions in $__dscr!
|
||||
|
||||
# display all matches
|
||||
local dsuf dscr
|
||||
for i in {1..$#__hits}; do
|
||||
|
||||
# add a dir suffix?
|
||||
(( dirsuf )) && [[ -d $__hits[$i] ]] && dsuf=/ || dsuf=
|
||||
# description to be displayed afterwards
|
||||
# (( $#__dscr >= $i )) && dscr=" -- ${${__dscr[$i]}##$__hits[$i] #}" || dscr=
|
||||
|
||||
print - $'\1'$IPREFIX$apre$hpre$__hits[$i]$dsuf$hsuf$asuf$dscr
|
||||
|
||||
done
|
||||
|
||||
unset __hits __dscr __tmp
|
||||
}
|
||||
|
||||
# signal the daemon we are ready for input
|
||||
print $'\0'
|
@ -1,128 +0,0 @@
|
||||
#!/usr/bin/env zsh
|
||||
# Based on:
|
||||
# https://github.com/Valodim/zsh-capture-completion/blob/master/capture.zsh
|
||||
|
||||
# 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
|
||||
print "connection accepted, fd: $REPLY"
|
||||
}
|
||||
|
||||
handle-request() {
|
||||
local connection=$1 current line
|
||||
integer read_something=0
|
||||
print "request received from fd $connection"
|
||||
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
|
||||
print -u $connection - $prefix$'\2'${current:0:-1}
|
||||
else
|
||||
print -u $connection ''
|
||||
fi
|
||||
# clear input buffer
|
||||
zpty -w z $'\n'
|
||||
break # handle more requests/return to zselect
|
||||
done
|
||||
if ! (( read_something )); then
|
||||
print "connection with fd $connection closed"
|
||||
unset fds[$connection]
|
||||
exec {connection}>&- # free the file descriptor
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
if [[ -n $ZLE_AUTOSUGGEST_SERVER_LOG ]]; then
|
||||
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"
|
||||
else
|
||||
exec 2> /dev/null
|
||||
fi
|
||||
|
||||
exec < /dev/null
|
||||
|
||||
zmodload zsh/zpty
|
||||
zmodload zsh/zselect
|
||||
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'"
|
||||
|
||||
# wait for ok from shell
|
||||
read-to-null &> /dev/null
|
||||
print 'interactive shell ready'
|
||||
|
||||
# listen on a socket for completion requests
|
||||
server_dir=$1
|
||||
pid_file=$2
|
||||
socket_path=$3
|
||||
|
||||
|
||||
cleanup() {
|
||||
print 'removing socket and pid file...'
|
||||
rm -f $socket_path $pid_file
|
||||
print "autosuggestion server stopped, pid: $$"
|
||||
exit
|
||||
}
|
||||
|
||||
trap cleanup TERM INT HUP EXIT
|
||||
|
||||
mkdir -m 700 $server_dir &> /dev/null
|
||||
|
||||
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
|
||||
print "will retry listening on '$socket_path'"
|
||||
done
|
||||
|
||||
server=$REPLY
|
||||
|
||||
print "server listening on '$socket_path'"
|
||||
|
||||
print $$ > $pid_file
|
||||
|
||||
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
|
||||
else
|
||||
handle-request $fd
|
||||
fi
|
||||
done
|
||||
done
|
Loading…
Reference in New Issue