mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2024-11-18 09:51:06 +01:00
Got basic completions working, need to ignore
cases like 'kill name<tab>' which should replace the word
This commit is contained in:
parent
1e6e900abf
commit
c6c97ab7bd
4 changed files with 68 additions and 71 deletions
|
@ -8,30 +8,12 @@
|
||||||
# ```
|
# ```
|
||||||
zmodload zsh/net/socket
|
zmodload zsh/net/socket
|
||||||
|
|
||||||
AUTOSUGGEST_SERVER_SCRIPT="${0:a:h}/completion-server.zsh"
|
source "${0:a:h}/completion-client.zsh"
|
||||||
|
|
||||||
# function {
|
function {
|
||||||
# [[ -n $ZLE_DISABLE_AUTOSUGGEST ]] && return
|
[[ -n $ZLE_DISABLE_AUTOSUGGEST ]] && return
|
||||||
# setopt local_options no_hup
|
autosuggest-ensure-server
|
||||||
|
}
|
||||||
# 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
|
|
||||||
# # start server
|
|
||||||
# zsh $AUTOSUGGEST_SERVER_SCRIPT $server_dir $pid_file $socket_path &!
|
|
||||||
# fi
|
|
||||||
|
|
||||||
# integer remaining_tries=10
|
|
||||||
# # wait until the process is listening
|
|
||||||
# while ! [[ -d $server_dir && -r $pid_file ]] || ! kill -0 $(<$pid_file) &> /dev/null; do
|
|
||||||
# (( --remaining_tries )) || break
|
|
||||||
# sleep 0.3
|
|
||||||
# done
|
|
||||||
|
|
||||||
# ZLE_AUTOSUGGEST_SOCKET=$socket_path
|
|
||||||
# }
|
|
||||||
|
|
||||||
ZLE_AUTOSUGGEST_PAUSE_WIDGETS=(
|
ZLE_AUTOSUGGEST_PAUSE_WIDGETS=(
|
||||||
vi-cmd-mode vi-backward-char backward-char backward-word beginning-of-line
|
vi-cmd-mode vi-backward-char backward-char backward-word beginning-of-line
|
||||||
|
@ -169,25 +151,14 @@ paused-autosuggest-self-insert() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
autosuggest-first-completion() {
|
autosuggest-get-completion() {
|
||||||
zsocket $ZLE_AUTOSUGGEST_SOCKET &>/dev/null || return 1
|
local suggestion=$(autosuggest-first-completion $LBUFFER)
|
||||||
local connection=$REPLY
|
RBUFFER="$suggestion"
|
||||||
local completion
|
|
||||||
print -u $connection $LBUFFER
|
|
||||||
while read -u $connection completion; do
|
|
||||||
RBUFFER=" $completion"
|
|
||||||
break
|
|
||||||
done
|
|
||||||
exec {connection}>&-
|
|
||||||
}
|
}
|
||||||
|
|
||||||
show-suggestion() {
|
show-suggestion() {
|
||||||
[[ -n $ZLE_DISABLE_AUTOSUGGEST || $LBUFFER == '' ]] && return
|
[[ -n $ZLE_DISABLE_AUTOSUGGEST || $LBUFFER == '' ]] && return
|
||||||
# TODO need a way to reset HISTNO so .history-beginning-search-backward
|
zle .history-beginning-search-backward || autosuggest-get-completion
|
||||||
# will always retrieve the last matching history entry
|
|
||||||
# unset HISTNO
|
|
||||||
zle .history-beginning-search-backward || autosuggest-first-completion\
|
|
||||||
|| RBUFFER=''
|
|
||||||
highlight-suggested-text
|
highlight-suggested-text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,25 +1,36 @@
|
||||||
#!/usr/bin/env zsh
|
#!/usr/bin/env zsh
|
||||||
# Helper script for debugging the completion server
|
|
||||||
zmodload zsh/net/socket
|
zmodload zsh/net/socket
|
||||||
setopt no_hup
|
|
||||||
AUTOSUGGEST_SERVER_SCRIPT="${0:a:h}/completion-server.zsh"
|
AUTOSUGGEST_SERVER_SCRIPT="${0:a:h}/completion-server.zsh"
|
||||||
|
|
||||||
server_dir="/tmp/zsh-autosuggest-$USER"
|
autosuggest-ensure-server() {
|
||||||
pid_file="$server_dir/pid"
|
setopt local_options no_hup
|
||||||
socket_path="$server_dir/socket"
|
local server_dir="/tmp/zsh-autosuggest-$USER"
|
||||||
|
local pid_file="$server_dir/pid"
|
||||||
|
local socket_path="$server_dir/socket"
|
||||||
|
|
||||||
[[ -S $socket_path && -r $pid_file ]] && kill -0 $(<$pid_file) &> /dev/null ||\
|
[[ -S $socket_path && -r $pid_file ]] && \
|
||||||
|
kill -0 $(<$pid_file) &> /dev/null || \
|
||||||
zsh $AUTOSUGGEST_SERVER_SCRIPT $server_dir $pid_file $socket_path &!
|
zsh $AUTOSUGGEST_SERVER_SCRIPT $server_dir $pid_file $socket_path &!
|
||||||
|
|
||||||
|
integer remaining_tries=10
|
||||||
# wait until the process is listening
|
# wait until the process is listening
|
||||||
while ! [[ -d $server_dir && -r $pid_file ]] || ! kill -0 $(<$pid_file) &> /dev/null; do
|
while ! [[ -d $server_dir && -r $pid_file ]] ||\
|
||||||
|
! kill -0 $(<$pid_file) &> /dev/null && (( --remaining_tries )); do
|
||||||
sleep 0.3
|
sleep 0.3
|
||||||
done
|
done
|
||||||
|
ZLE_AUTOSUGGEST_SOCKET=$socket_path
|
||||||
|
}
|
||||||
|
|
||||||
zsocket $socket_path
|
|
||||||
connection=$REPLY
|
autosuggest-first-completion() {
|
||||||
print -u $connection vi
|
zsocket $ZLE_AUTOSUGGEST_SOCKET &>/dev/null || return 1
|
||||||
|
local connection=$REPLY
|
||||||
|
local completion
|
||||||
|
print -u $connection $1
|
||||||
while read -u $connection completion; do
|
while read -u $connection completion; do
|
||||||
print $completion
|
print ${completion}
|
||||||
done
|
done
|
||||||
|
# close fd
|
||||||
exec {connection}>&-
|
exec {connection}>&-
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
# Based on:
|
# Based on:
|
||||||
# https://github.com/Valodim/zsh-capture-completion/blob/master/.zshrc
|
# https://github.com/Valodim/zsh-capture-completion/blob/master/.zshrc
|
||||||
|
|
||||||
|
ZLE_DISABLE_AUTOSUGGEST=1
|
||||||
# no prompt!
|
# no prompt!
|
||||||
PROMPT=
|
PROMPT=
|
||||||
|
|
||||||
|
@ -38,6 +39,10 @@ zstyle ':completion:*' insert-tab false
|
||||||
zstyle ':completion:*' list-separator ''
|
zstyle ':completion:*' list-separator ''
|
||||||
# dont use matchers
|
# dont use matchers
|
||||||
zstyle -d ':completion:*' matcher-list
|
zstyle -d ':completion:*' matcher-list
|
||||||
|
# dont format
|
||||||
|
zstyle -d ':completion:*' format
|
||||||
|
# no color formatting
|
||||||
|
zstyle -d ':completion:*' list-colors
|
||||||
|
|
||||||
# we use zparseopts
|
# we use zparseopts
|
||||||
zmodload zsh/zutil
|
zmodload zsh/zutil
|
||||||
|
@ -96,7 +101,6 @@ compadd () {
|
||||||
# this is the point where we have all matches in $__hits and all
|
# this is the point where we have all matches in $__hits and all
|
||||||
# descriptions in $__dscr!
|
# descriptions in $__dscr!
|
||||||
|
|
||||||
__hits=(${(O)__hits})
|
|
||||||
# display all matches
|
# display all matches
|
||||||
local dsuf dscr
|
local dsuf dscr
|
||||||
for i in {1..$#__hits}; do
|
for i in {1..$#__hits}; do
|
||||||
|
|
|
@ -2,7 +2,9 @@
|
||||||
# Based on:
|
# Based on:
|
||||||
# https://github.com/Valodim/zsh-capture-completion/blob/master/capture.zsh
|
# https://github.com/Valodim/zsh-capture-completion/blob/master/capture.zsh
|
||||||
|
|
||||||
|
# close stdio
|
||||||
exec &> /dev/null
|
exec &> /dev/null
|
||||||
|
exec < /dev/null
|
||||||
|
|
||||||
zmodload zsh/zpty
|
zmodload zsh/zpty
|
||||||
zmodload zsh/net/socket
|
zmodload zsh/net/socket
|
||||||
|
@ -13,37 +15,30 @@ zpty z ZLE_DISABLE_AUTOSUGGEST=1 zsh -i
|
||||||
# Source the init script
|
# Source the init script
|
||||||
zpty -w z "source '${0:a:h}/completion-server-init.zsh'"
|
zpty -w z "source '${0:a:h}/completion-server-init.zsh'"
|
||||||
|
|
||||||
|
# read all completions and return the longest match
|
||||||
read-to-null() {
|
read-to-null() {
|
||||||
connection=$1
|
|
||||||
integer consumed=0
|
|
||||||
while zpty -r z chunk; do
|
while zpty -r z chunk; do
|
||||||
[[ $chunk == *$'\0'* ]] && break
|
[[ $chunk == *$'\0'* ]] && break
|
||||||
(( consumed++ )) && continue
|
print -n $chunk
|
||||||
if [[ -n $connection ]]; then
|
|
||||||
print -n -u $connection $chunk
|
|
||||||
else
|
|
||||||
print -n $chunk &> /dev/null
|
|
||||||
fi
|
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# wait for ok from shell
|
# wait for ok from shell
|
||||||
read-to-null
|
read-to-null &> /dev/null
|
||||||
|
|
||||||
# listen on an unix domain socket
|
# listen on a socket for completion requests
|
||||||
server_dir=$1
|
server_dir=$1
|
||||||
pid_file=$2
|
pid_file=$2
|
||||||
socket_path=$3
|
socket_path=$3
|
||||||
|
|
||||||
|
|
||||||
cleanup() {
|
cleanup() {
|
||||||
rm -f $socket_path
|
rm -f $socket_path $pid_file
|
||||||
rm -f $pid_file
|
|
||||||
}
|
}
|
||||||
|
|
||||||
trap cleanup TERM INT HUP EXIT
|
trap cleanup TERM INT HUP EXIT
|
||||||
|
|
||||||
mkdir $server_dir &> /dev/null
|
mkdir -m 700 $server_dir &> /dev/null
|
||||||
|
|
||||||
while ! zsocket -l $socket_path; do
|
while ! zsocket -l $socket_path; do
|
||||||
if [[ ! -r $pid_file ]] || ! kill -0 $(<$pid_file) &> /dev/null; then
|
if [[ ! -r $pid_file ]] || ! kill -0 $(<$pid_file) &> /dev/null; then
|
||||||
|
@ -61,10 +56,26 @@ while zsocket -a $server &> /dev/null; do
|
||||||
connection=$REPLY
|
connection=$REPLY
|
||||||
# connection accepted, read the request and send response
|
# connection accepted, read the request and send response
|
||||||
while read -u $connection prefix &> /dev/null; do
|
while read -u $connection prefix &> /dev/null; do
|
||||||
|
# send the prefix to be completed followed by a TAB to force
|
||||||
|
# completion
|
||||||
zpty -w -n z $prefix$'\t'
|
zpty -w -n z $prefix$'\t'
|
||||||
zpty -r z chunk &> /dev/null # read empty line before completions
|
zpty -r z chunk &> /dev/null # read empty line before completions
|
||||||
read-to-null $connection
|
local current=''
|
||||||
|
# read completions one by one, storing the longest match
|
||||||
|
read-to-null | while read line; do
|
||||||
|
(( $#line > $#current )) && current=$line
|
||||||
|
done
|
||||||
|
# send the longest completion back to the client, strip the last
|
||||||
|
# non-printable character
|
||||||
|
if (( $#current )); then
|
||||||
|
local last_word=${${(z)prefix}[-1]}
|
||||||
|
print -u $connection ${current:$#last_word:-1}
|
||||||
|
else
|
||||||
|
print -u $connection ''
|
||||||
|
fi
|
||||||
|
# close fd
|
||||||
exec {connection}>&-
|
exec {connection}>&-
|
||||||
|
# clear input buffer
|
||||||
zpty -w z $'\n'
|
zpty -w z $'\n'
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
Loading…
Reference in a new issue