mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2025-12-06 15:20:40 +01:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20c0ea841b |
40
README.mkd
40
README.mkd
@@ -1,32 +1,20 @@
|
|||||||
# zsh-autosuggestions
|
# zsh-autosuggestions
|
||||||
|
|
||||||
> [Fish](http://fishshell.com/)-like fast/unobtrusive autosuggestions for zsh.
|
> [Fish](http://fishshell.com/)-like fast/unobtrusive autosuggestions for zsh.
|
||||||
|
> [Shelr demo](http://shelr.tv/records/527007a99660803c5d000048).
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
```zsh
|
||||||
|
git clone git://github.com/tarruda/zsh-autosuggestions ~/.zsh-autosuggestions
|
||||||
|
cat >> ~/.zshrc << "EOF"
|
||||||
|
source ~/.zsh-autosuggestions/autosuggestions.zsh
|
||||||
|
# Enable autosuggestions automatically
|
||||||
|
zle-line-init() {
|
||||||
|
zle autosuggest-start
|
||||||
|
}
|
||||||
|
zle -N zle-line-init
|
||||||
|
# use ctrl+f to accept a suggested word
|
||||||
|
bindkey '^F' autosuggest-accept-suggested-word
|
||||||
|
EOF
|
||||||
```
|
```
|
||||||
$ git clone git://github.com/tarruda/zsh-autosuggestions ~/.zsh-autosuggestions
|
|
||||||
sh .zsh-autosuggestions/install
|
|
||||||
```
|
|
||||||
|
|
||||||
Any widget that moves the cursor to the right(forward-word, forward-char...)
|
|
||||||
will accept parts of the suggested text. For example, vi-mode users can do
|
|
||||||
this:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
# Accept suggestions without leaving insert mode
|
|
||||||
bindkey '^f' vi-forward-word
|
|
||||||
# or
|
|
||||||
bindkey '^f' vi-forward-blank-word
|
|
||||||
```
|
|
||||||
|
|
||||||
Emacs-mode users can simply use alt+f which is bound to forward-word
|
|
||||||
|
|
||||||
The [zsh-history-substring-search](https://github.com/zsh-users/zsh-history-substring-search)
|
|
||||||
plugin is also recommended.
|
|
||||||
|
|
||||||
## Configuration
|
|
||||||
|
|
||||||
You may override default global config variables after plugin load.
|
|
||||||
|
|
||||||
- `AUTOSUGGESTION_HIGHLIGHT_COLOR`: suggestion highlight color, default is `'fg=8'`.
|
|
||||||
- `AUTOSUGGESTION_HIGHLIGHT_CURSOR`: highlight word after cursor or not. Must be integer value `1` or `0`, default is `1`.
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
autosuggestions.zsh
|
|
||||||
@@ -1,38 +1,17 @@
|
|||||||
# Fish-like autosuggestions for zsh. Some of the code was based on the code
|
# Fish-like autosuggestions for zsh. Some of the code was based on the code
|
||||||
# for 'predict-on'
|
# for 'predict-on'
|
||||||
#
|
#
|
||||||
# ```zsh
|
# ```zsh
|
||||||
# zle-line-init() {
|
# zle-line-init() {
|
||||||
# autosuggest-enable
|
# autosuggest-start
|
||||||
# }
|
# }
|
||||||
# zle -N zle-line-init
|
# zle -N zle-line-init
|
||||||
# ```
|
# ```
|
||||||
zmodload zsh/net/socket
|
|
||||||
|
|
||||||
source "${0:a:h}/completion-client.zsh"
|
ZLE_AUTOSUGGEST_PAUSE_WIDGETS=(
|
||||||
|
|
||||||
# configuration variables
|
|
||||||
AUTOSUGGESTION_HIGHLIGHT_COLOR='fg=8'
|
|
||||||
AUTOSUGGESTION_HIGHLIGHT_CURSOR=1
|
|
||||||
|
|
||||||
function {
|
|
||||||
if [[ -n $ZLE_DISABLE_AUTOSUGGEST ]]; then
|
|
||||||
ZSH_HIGHLIGHT_HIGHLIGHTERS=()
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
autoload -U is-at-least
|
|
||||||
|
|
||||||
# if is-at-least 5.0.3; then
|
|
||||||
# autosuggest-ensure-server
|
|
||||||
# fi
|
|
||||||
}
|
|
||||||
|
|
||||||
ZLE_AUTOSUGGEST_SUSPEND_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
|
||||||
history-search-forward history-search-backward up-line-or-history
|
history-search-forward history-search-backward up-line-or-history
|
||||||
history-beginning-search-forward history-beginning-search-backward
|
down-line-or-history
|
||||||
down-line-or-history history-substring-search-up history-substring-search-down
|
|
||||||
backward-kill-word
|
|
||||||
)
|
)
|
||||||
|
|
||||||
ZLE_AUTOSUGGEST_COMPLETION_WIDGETS=(
|
ZLE_AUTOSUGGEST_COMPLETION_WIDGETS=(
|
||||||
@@ -41,31 +20,23 @@ menu-complete reverse-menu-complete menu-expand-or-complete menu-select
|
|||||||
accept-and-menu-complete
|
accept-and-menu-complete
|
||||||
)
|
)
|
||||||
|
|
||||||
ZLE_AUTOSUGGEST_ACCEPT_WIDGETS=(
|
|
||||||
vi-forward-char forward-char vi-forward-word forward-word vi-add-eol
|
|
||||||
vi-add-next vi-forward-blank-word end-of-line
|
|
||||||
)
|
|
||||||
|
|
||||||
autosuggest-pause() {
|
autosuggest-pause() {
|
||||||
[[ -z $ZLE_AUTOSUGGESTING ]] && return
|
[[ -z $ZLE_AUTOSUGGESTING ]] && return
|
||||||
unset ZLE_AUTOSUGGESTING
|
unset ZLE_AUTOSUGGESTING
|
||||||
local widget
|
local widget
|
||||||
# When autosuggestions are disabled, kill the unmaterialized part
|
# When autosuggestions are disabled, kill the unmaterialized part
|
||||||
RBUFFER=''
|
RBUFFER=''
|
||||||
zle -A autosuggest-paused-self-insert self-insert
|
zle -A self-insert autosuggest-paused-self-insert
|
||||||
zle -A autosuggest-magic-space-orig magic-space
|
zle -A .magic-space magic-space
|
||||||
zle -A autosuggest-backward-delete-char-orig backward-delete-char
|
zle -A .backward-delete-char backward-delete-char
|
||||||
zle -A autosuggest-accept-line-orig accept-line
|
zle -A .accept-line accept-line
|
||||||
for widget in $ZLE_AUTOSUGGEST_ACCEPT_WIDGETS $ZLE_AUTOSUGGEST_SUSPEND_WIDGETS $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
|
for widget in $ZLE_AUTOSUGGEST_PAUSE_WIDGETS; do
|
||||||
[[ -z $widgets[$widget] || -z $widgets[autosuggest-${widget}-orig] ]] &&\
|
|
||||||
continue
|
|
||||||
eval "zle -A autosuggest-${widget}-orig ${widget}"
|
eval "zle -A autosuggest-${widget}-orig ${widget}"
|
||||||
done
|
done
|
||||||
|
for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
|
||||||
|
eval "zle -A autosuggest-${widget}-orig $widget"
|
||||||
|
done
|
||||||
autosuggest-highlight-suggested-text
|
autosuggest-highlight-suggested-text
|
||||||
|
|
||||||
if [[ -n $ZLE_AUTOSUGGEST_CONNECTION ]]; then
|
|
||||||
zle -F $ZLE_AUTOSUGGEST_CONNECTION
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
autosuggest-resume() {
|
autosuggest-resume() {
|
||||||
@@ -73,106 +44,70 @@ autosuggest-resume() {
|
|||||||
ZLE_AUTOSUGGESTING=1
|
ZLE_AUTOSUGGESTING=1
|
||||||
local widget
|
local widget
|
||||||
# Replace prediction widgets by versions that will also highlight RBUFFER
|
# Replace prediction widgets by versions that will also highlight RBUFFER
|
||||||
zle -A autosuggest-insert-or-space self-insert
|
zle -N self-insert autosuggest-insert-or-space
|
||||||
zle -A autosuggest-insert-or-space magic-space
|
zle -N magic-space autosuggest-insert-or-space
|
||||||
zle -A autosuggest-backward-delete-char backward-delete-char
|
zle -N backward-delete-char autosuggest-backward-delete-char
|
||||||
zle -A autosuggest-accept-line accept-line
|
zle -N accept-line autosuggest-accept-line
|
||||||
# Hook into some default widgets that should suspend autosuggestion
|
# Hook into some default widgets that should pause autosuggestion
|
||||||
# automatically
|
# automatically
|
||||||
for widget in $ZLE_AUTOSUGGEST_ACCEPT_WIDGETS; do
|
for widget in $ZLE_AUTOSUGGEST_PAUSE_WIDGETS; do
|
||||||
[[ -z $widgets[$widget] ]] && continue
|
eval "zle -A $widget autosuggest-${widget}-orig; \
|
||||||
eval "zle -A autosuggest-accept-suggestion $widget"
|
zle -A autosuggest-suspend $widget"
|
||||||
done
|
done
|
||||||
for widget in $ZLE_AUTOSUGGEST_SUSPEND_WIDGETS; do
|
# Hook into completion widgets to handle suggestions after completions
|
||||||
[[ -z $widgets[$widget] ]] && continue
|
|
||||||
eval "zle -A autosuggest-suspend $widget"
|
|
||||||
done
|
|
||||||
# Hook into completion widgets to trim RBUFFER before completion
|
|
||||||
for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
|
for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
|
||||||
[[ -z $widgets[$widget] ]] && continue
|
eval "zle -A $widget autosuggest-${widget}-orig; \
|
||||||
eval "zle -A autosuggest-tab $widget"
|
zle -A autosuggest-tab $widget"
|
||||||
done
|
done
|
||||||
if [[ -n $ZLE_AUTOSUGGEST_CONNECTION ]]; then
|
if [[ $BUFFER != '' ]]; then
|
||||||
# install listen for suggestions asynchronously
|
autosuggest-request-suggestion
|
||||||
zle -Fw $ZLE_AUTOSUGGEST_CONNECTION autosuggest-pop-suggestion
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
autosuggest-start() {
|
autosuggest-start() {
|
||||||
if [[ -z $ZLE_DISABLE_AUTOSUGGEST && -n $functions[_zsh_highlight] ]]; then
|
|
||||||
if [[ ${ZSH_HIGHLIGHT_HIGHLIGHTERS[(i)autosuggest]} -gt ${#ZSH_HIGHLIGHT_HIGHLIGHTERS} ]];then
|
|
||||||
ZSH_HIGHLIGHT_HIGHLIGHTERS+=(autosuggest)
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
autosuggest-resume
|
autosuggest-resume
|
||||||
}
|
}
|
||||||
|
|
||||||
# Toggles autosuggestions on/off
|
|
||||||
autosuggest-toggle() {
|
|
||||||
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
|
|
||||||
autosuggest-pause
|
|
||||||
zle -A autosuggest-self-insert-orig self-insert
|
|
||||||
else
|
|
||||||
autosuggest-resume
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-highlight-suggested-text() {
|
autosuggest-highlight-suggested-text() {
|
||||||
if (( $+functions[_zsh_highlight_buffer_modified] > 0 )); then
|
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
|
||||||
_zsh_highlight
|
local color='fg=8'
|
||||||
|
[[ -n $AUTOSUGGESTION_HIGHLIGHT_COLOR ]] &&\
|
||||||
|
color=$AUTOSUGGESTION_HIGHLIGHT_COLOR
|
||||||
|
region_highlight=("$(( $CURSOR + 1 )) $(( $CURSOR + $#RBUFFER )) $color")
|
||||||
else
|
else
|
||||||
region_highlight=()
|
region_highlight=()
|
||||||
_zsh_highlight_autosuggest_highlighter
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_zsh_highlight_autosuggest_highlighter_predicate() {
|
|
||||||
[[ -n $ZLE_AUTOSUGGESTING ]] && (( $#RBUFFER > 0 ))
|
|
||||||
}
|
|
||||||
|
|
||||||
_zsh_highlight_autosuggest_highlighter() {
|
|
||||||
region_highlight+=("$(( $CURSOR + $AUTOSUGGESTION_HIGHLIGHT_CURSOR )) $(( $CURSOR + $#RBUFFER )) $AUTOSUGGESTION_HIGHLIGHT_COLOR")
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-insert-or-space() {
|
autosuggest-insert-or-space() {
|
||||||
setopt localoptions noshwordsplit noksharrays
|
|
||||||
if [[ $LBUFFER == *$'\012'* ]] || (( PENDING )); then
|
if [[ $LBUFFER == *$'\012'* ]] || (( PENDING )); then
|
||||||
# Editing multiline buffer or pasting a chunk of text, pause
|
# Editing a multiline buffer or pasting in a chunk of text, dont
|
||||||
autosuggest-suspend
|
# autosuggest
|
||||||
return
|
zle .$WIDGET "$@"
|
||||||
fi
|
elif [[ ${RBUFFER[1]} == ${KEYS[-1]} ]]; then
|
||||||
|
# Same as what's typed, just move on
|
||||||
if [[ ${RBUFFER[1]} == ${KEYS[-1]} ]]; then
|
|
||||||
# Same as what's typed, just move on
|
|
||||||
((++CURSOR))
|
((++CURSOR))
|
||||||
autosuggest-invalidate-highlight-cache
|
autosuggest-highlight-suggested-text
|
||||||
else
|
else
|
||||||
LBUFFER="$LBUFFER$KEYS"
|
LBUFFER="$LBUFFER$KEYS"
|
||||||
if [[ $LASTWIDGET == (self-insert|magic-space|backward-delete-char) || $LASTWIDGET == (complete-word|accept-*|zle-line-init) ]]; then
|
autosuggest-request-suggestion
|
||||||
if ! zle .history-beginning-search-backward; then
|
|
||||||
RBUFFER=''
|
|
||||||
if [[ ${KEYS[-1]} != ' ' ]]; then
|
|
||||||
autosuggest-send-request ${LBUFFER}
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
autosuggest-highlight-suggested-text
|
|
||||||
}
|
}
|
||||||
|
|
||||||
autosuggest-backward-delete-char() {
|
autosuggest-backward-delete-char() {
|
||||||
if (( $#LBUFFER > 1 )); then
|
if ! (( CURSOR )); then
|
||||||
setopt localoptions noshwordsplit noksharrays
|
zle .kill-whole-line
|
||||||
if [[ $LBUFFER = *$'\012'* || $LASTWIDGET != (self-insert|magic-space|backward-delete-char) ]]; then
|
return
|
||||||
LBUFFER="$LBUFFER[1,-2]"
|
fi
|
||||||
else
|
|
||||||
((--CURSOR))
|
if [[ $LBUFFER == *$'\012'* || $LASTWIDGET != (self-insert|magic-space|backward-delete-char) ]]; then
|
||||||
autosuggest-invalidate-highlight-cache
|
# When editing a multiline buffer or if the last widget was e.g. a motion,
|
||||||
zle .history-beginning-search-forward || RBUFFER=''
|
# then probably the intent is to actually edit the line, not change the
|
||||||
fi
|
# search prefix.
|
||||||
autosuggest-highlight-suggested-text
|
LBUFFER="$LBUFFER[1,-2]"
|
||||||
else
|
else
|
||||||
zle .kill-whole-line
|
((--CURSOR))
|
||||||
|
zle .history-beginning-search-forward || RBUFFER=''
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,10 +115,7 @@ autosuggest-backward-delete-char() {
|
|||||||
# section when the user accepts the line
|
# section when the user accepts the line
|
||||||
autosuggest-accept-line() {
|
autosuggest-accept-line() {
|
||||||
RBUFFER=''
|
RBUFFER=''
|
||||||
if ! (( $+functions[_zsh_highlight_buffer_modified] )); then
|
region_highlight=()
|
||||||
# Only clear the colors if the user doesn't have zsh-highlight installed
|
|
||||||
region_highlight=()
|
|
||||||
fi
|
|
||||||
zle .accept-line
|
zle .accept-line
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,38 +123,21 @@ autosuggest-paused-self-insert() {
|
|||||||
if [[ $RBUFFER == '' ]]; then
|
if [[ $RBUFFER == '' ]]; then
|
||||||
# Resume autosuggestions when inserting at the end of the line
|
# Resume autosuggestions when inserting at the end of the line
|
||||||
autosuggest-resume
|
autosuggest-resume
|
||||||
zle self-insert
|
zle autosuggest-modify
|
||||||
else
|
else
|
||||||
zle autosuggest-self-insert-orig
|
zle .self-insert
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
autosuggest-pop-suggestion() {
|
autosuggest-request-suggestion() {
|
||||||
local words last_word suggestion
|
if (( $CURSOR == 0 )) || [[ ${LBUFFER[-1]} == ' ' ]]; then
|
||||||
if ! IFS= read -r -u $ZLE_AUTOSUGGEST_CONNECTION suggestion; then
|
RBUFFER=''
|
||||||
# server closed the connection, stop listenting
|
|
||||||
zle -F $ZLE_AUTOSUGGEST_CONNECTION
|
|
||||||
unset ZLE_AUTOSUGGEST_CONNECTION
|
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
if [[ -n $suggestion ]]; then
|
|
||||||
local prefix=${suggestion%$'\2'*}
|
[[ -n $ZLE_DISABLE_AUTOSUGGEST || $LBUFFER == '' ]] && return
|
||||||
suggestion=${suggestion#*$'\2'}
|
zle .history-beginning-search-backward || RBUFFER=''
|
||||||
# only use the suggestion if the prefix is still compatible with
|
autosuggest-highlight-suggested-text
|
||||||
# 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-suspend() {
|
autosuggest-suspend() {
|
||||||
@@ -236,44 +151,18 @@ autosuggest-tab() {
|
|||||||
autosuggest-highlight-suggested-text
|
autosuggest-highlight-suggested-text
|
||||||
}
|
}
|
||||||
|
|
||||||
autosuggest-accept-suggestion() {
|
autosuggest-accept-suggested-small-word() {
|
||||||
if [[ AUTOSUGGESTION_ACCEPT_RIGHT_ARROW -eq 1 && "$WIDGET" == 'forward-char' ]]; then
|
zle .vi-forward-word
|
||||||
zle autosuggest-end-of-line-orig "$@"
|
autosuggest-highlight-suggested-text
|
||||||
else
|
|
||||||
zle autosuggest-${WIDGET}-orig "$@"
|
|
||||||
fi
|
|
||||||
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
|
|
||||||
autosuggest-invalidate-highlight-cache
|
|
||||||
autosuggest-highlight-suggested-text
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
autosuggest-invalidate-highlight-cache() {
|
autosuggest-accept-suggested-word() {
|
||||||
# invalidate the buffer for zsh-syntax-highlighting
|
zle .forward-word
|
||||||
_ZSH_HIGHLIGHT_PRIOR_BUFFER=''
|
autosuggest-highlight-suggested-text
|
||||||
}
|
}
|
||||||
|
|
||||||
zle -N autosuggest-toggle
|
|
||||||
zle -N autosuggest-start
|
zle -N autosuggest-start
|
||||||
zle -N autosuggest-accept-suggested-small-word
|
zle -N autosuggest-accept-suggested-small-word
|
||||||
zle -N autosuggest-accept-suggested-word
|
zle -N autosuggest-accept-suggested-word
|
||||||
|
|
||||||
zle -N autosuggest-paused-self-insert
|
|
||||||
zle -N autosuggest-insert-or-space
|
|
||||||
zle -N autosuggest-backward-delete-char
|
|
||||||
zle -N autosuggest-accept-line
|
|
||||||
|
|
||||||
zle -N autosuggest-tab
|
|
||||||
zle -N autosuggest-suspend
|
zle -N autosuggest-suspend
|
||||||
zle -N autosuggest-accept-suggestion
|
zle -N autosuggest-tab
|
||||||
|
|
||||||
# Save all widgets
|
|
||||||
zle -A self-insert autosuggest-self-insert-orig
|
|
||||||
zle -A magic-space autosuggest-magic-space-orig
|
|
||||||
zle -A backward-delete-char autosuggest-backward-delete-char-orig
|
|
||||||
zle -A accept-line autosuggest-accept-line-orig
|
|
||||||
|
|
||||||
for widget in ${ZLE_AUTOSUGGEST_ACCEPT_WIDGETS} ${ZLE_AUTOSUGGEST_SUSPEND_WIDGETS} ${ZLE_AUTOSUGGEST_COMPLETION_WIDGETS}; do
|
|
||||||
[[ -z $widgets[$widget] ]] && continue
|
|
||||||
eval "zle -A $widget autosuggest-${widget}-orig"
|
|
||||||
done
|
|
||||||
|
|||||||
@@ -1,40 +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 [[ ! -d $server_dir || ! -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-send-request() {
|
|
||||||
[[ -z $ZLE_AUTOSUGGEST_CONNECTION ]] && return 1
|
|
||||||
setopt local_options noglob
|
|
||||||
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" >&2
|
|
||||||
}
|
|
||||||
|
|
||||||
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" >&2
|
|
||||||
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: $$" >&2
|
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
while ! zsocket -l $socket_path; do
|
|
||||||
if [[ ! -r $pid_file ]] || ! kill -0 $(<$pid_file); 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
|
|
||||||
32
install
32
install
@@ -1,32 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
# Install script for zsh-autocomplete
|
|
||||||
|
|
||||||
#first checks if ~/.zshrc file exists and is readable
|
|
||||||
if [ ! -r ~/.zshrc ]; then
|
|
||||||
echo "\nError: ~/.zshrc file does not exist or is not readable!\n"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
DIR=$(dirname $(readlink -e $0)) ;
|
|
||||||
|
|
||||||
# appends the string to ~/.zshrc file
|
|
||||||
cat >> ~/.zshrc << EOF
|
|
||||||
|
|
||||||
# Setup zsh-autosuggestions
|
|
||||||
source $DIR/autosuggestions.zsh
|
|
||||||
|
|
||||||
# Enable autosuggestions automatically
|
|
||||||
zle-line-init() {
|
|
||||||
zle autosuggest-start
|
|
||||||
}
|
|
||||||
|
|
||||||
zle -N zle-line-init
|
|
||||||
|
|
||||||
# use ctrl+t to toggle autosuggestions(hopefully this wont be needed as
|
|
||||||
# zsh-autosuggestions is designed to be unobtrusive)
|
|
||||||
bindkey '^T' autosuggest-toggle
|
|
||||||
EOF
|
|
||||||
|
|
||||||
echo "\nSetup completed successfully!\n"
|
|
||||||
exit 0
|
|
||||||
Reference in New Issue
Block a user