mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2024-12-09 10:11:02 +01:00
Remove completion server and focus on history
suggestion
This commit is contained in:
parent
3dbe2c8c9b
commit
20c0ea841b
5 changed files with 4 additions and 356 deletions
|
@ -14,8 +14,6 @@ zle-line-init() {
|
|||
zle autosuggest-start
|
||||
}
|
||||
zle -N zle-line-init
|
||||
# use ctrl+t to toggle autosuggestions(hopefully this wont be needed)
|
||||
bindkey '^T' autosuggest-toggle
|
||||
# use ctrl+f to accept a suggested word
|
||||
bindkey '^F' autosuggest-accept-suggested-word
|
||||
EOF
|
||||
|
|
|
@ -3,18 +3,10 @@
|
|||
#
|
||||
# ```zsh
|
||||
# zle-line-init() {
|
||||
# autosuggest-enable
|
||||
# autosuggest-start
|
||||
# }
|
||||
# zle -N zle-line-init
|
||||
# ```
|
||||
zmodload zsh/net/socket
|
||||
|
||||
source "${0:a:h}/completion-client.zsh"
|
||||
|
||||
function {
|
||||
[[ -n $ZLE_DISABLE_AUTOSUGGEST ]] && return
|
||||
autosuggest-ensure-server
|
||||
}
|
||||
|
||||
ZLE_AUTOSUGGEST_PAUSE_WIDGETS=(
|
||||
vi-cmd-mode vi-backward-char backward-char backward-word beginning-of-line
|
||||
|
@ -45,8 +37,6 @@ autosuggest-pause() {
|
|||
eval "zle -A autosuggest-${widget}-orig $widget"
|
||||
done
|
||||
autosuggest-highlight-suggested-text
|
||||
|
||||
zle -F $ZLE_AUTOSUGGEST_CONNECTION
|
||||
}
|
||||
|
||||
autosuggest-resume() {
|
||||
|
@ -72,30 +62,10 @@ autosuggest-resume() {
|
|||
if [[ $BUFFER != '' ]]; then
|
||||
autosuggest-request-suggestion
|
||||
fi
|
||||
|
||||
if [[ -n $ZLE_AUTOSUGGEST_CONNECTION ]]; then
|
||||
# install listen for suggestions asynchronously
|
||||
zle -F $ZLE_AUTOSUGGEST_CONNECTION autosuggest-pop-suggestion
|
||||
fi
|
||||
}
|
||||
|
||||
autosuggest-start() {
|
||||
autosuggest-resume
|
||||
zle recursive-edit
|
||||
integer rv=$?
|
||||
autosuggest-pause
|
||||
zle -A .self-insert self-insert
|
||||
(( rv )) || zle accept-line
|
||||
return rv
|
||||
}
|
||||
|
||||
# Toggles autosuggestions on/off
|
||||
autosuggest-toggle() {
|
||||
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
|
||||
autosuggest-pause
|
||||
else
|
||||
autosuggest-resume
|
||||
fi
|
||||
}
|
||||
|
||||
autosuggest-highlight-suggested-text() {
|
||||
|
@ -125,7 +95,7 @@ autosuggest-insert-or-space() {
|
|||
}
|
||||
|
||||
autosuggest-backward-delete-char() {
|
||||
if ! (( $CURSOR )); then
|
||||
if ! (( CURSOR )); then
|
||||
zle .kill-whole-line
|
||||
return
|
||||
fi
|
||||
|
@ -152,41 +122,13 @@ autosuggest-accept-line() {
|
|||
autosuggest-paused-self-insert() {
|
||||
if [[ $RBUFFER == '' ]]; then
|
||||
# Resume autosuggestions when inserting at the end of the line
|
||||
autosuggest-enable
|
||||
autosuggest-resume
|
||||
zle autosuggest-modify
|
||||
else
|
||||
zle .self-insert
|
||||
fi
|
||||
}
|
||||
|
||||
autosuggest-pop-suggestion() {
|
||||
local words last_word suggestion
|
||||
if ! IFS= read -r -u $ZLE_AUTOSUGGEST_CONNECTION suggestion; then
|
||||
# server closed the connection, stop listenting
|
||||
zle -F $ZLE_AUTOSUGGEST_CONNECTION
|
||||
unset ZLE_AUTOSUGGEST_CONNECTION
|
||||
return
|
||||
fi
|
||||
if [[ -n $suggestion ]]; then
|
||||
local prefix=${suggestion%$'\2'*}
|
||||
suggestion=${suggestion#*$'\2'}
|
||||
# only use the suggestion if the prefix is still compatible with
|
||||
# the suggestion(prefix should be contained in LBUFFER)
|
||||
if [[ ${LBUFFER#$prefix*} != ${LBUFFER} ]]; then
|
||||
words=(${(z)LBUFFER})
|
||||
last_word=${words[-1]}
|
||||
suggestion=${suggestion:$#last_word}
|
||||
RBUFFER="$suggestion"
|
||||
autosuggest-highlight-suggested-text
|
||||
else
|
||||
RBUFFER=''
|
||||
fi
|
||||
else
|
||||
RBUFFER=''
|
||||
fi
|
||||
zle -Rc
|
||||
}
|
||||
|
||||
autosuggest-request-suggestion() {
|
||||
if (( $CURSOR == 0 )) || [[ ${LBUFFER[-1]} == ' ' ]]; then
|
||||
RBUFFER=''
|
||||
|
@ -194,8 +136,7 @@ autosuggest-request-suggestion() {
|
|||
fi
|
||||
|
||||
[[ -n $ZLE_DISABLE_AUTOSUGGEST || $LBUFFER == '' ]] && return
|
||||
zle .history-beginning-search-backward ||\
|
||||
autosuggest-first-completion ${LBUFFER}
|
||||
zle .history-beginning-search-backward || RBUFFER=''
|
||||
autosuggest-highlight-suggested-text
|
||||
}
|
||||
|
||||
|
@ -220,7 +161,6 @@ autosuggest-accept-suggested-word() {
|
|||
autosuggest-highlight-suggested-text
|
||||
}
|
||||
|
||||
zle -N autosuggest-toggle
|
||||
zle -N autosuggest-start
|
||||
zle -N autosuggest-accept-suggested-small-word
|
||||
zle -N autosuggest-accept-suggested-word
|
||||
|
|
|
@ -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 a new issue