2013-10-30 01:27:42 +08:00
|
|
|
# Fish-like autosuggestions for zsh. Some of the code was based on the code
|
|
|
|
# for 'predict-on'
|
2013-10-27 02:05:12 +08:00
|
|
|
#
|
|
|
|
# ```zsh
|
2013-10-27 01:11:53 +08:00
|
|
|
# zle-line-init() {
|
2013-10-30 01:27:42 +08:00
|
|
|
# autosuggest-enable
|
2013-10-27 01:11:53 +08:00
|
|
|
# }
|
|
|
|
# zle -N zle-line-init
|
2013-10-27 02:05:12 +08:00
|
|
|
# ```
|
2013-10-29 06:51:04 +08:00
|
|
|
zmodload zsh/net/socket
|
|
|
|
|
2013-10-29 16:42:03 +08:00
|
|
|
source "${0:a:h}/completion-client.zsh"
|
2013-10-29 06:51:04 +08:00
|
|
|
|
2013-10-29 16:42:03 +08:00
|
|
|
function {
|
|
|
|
[[ -n $ZLE_DISABLE_AUTOSUGGEST ]] && return
|
|
|
|
autosuggest-ensure-server
|
|
|
|
}
|
2013-10-27 00:05:17 +08:00
|
|
|
|
2013-10-28 01:40:10 +08:00
|
|
|
ZLE_AUTOSUGGEST_PAUSE_WIDGETS=(
|
|
|
|
vi-cmd-mode vi-backward-char backward-char backward-word beginning-of-line
|
|
|
|
history-search-forward history-search-backward up-line-or-history
|
|
|
|
down-line-or-history
|
|
|
|
)
|
|
|
|
|
|
|
|
ZLE_AUTOSUGGEST_COMPLETION_WIDGETS=(
|
|
|
|
complete-word expand-or-complete expand-or-complete-prefix list-choices
|
|
|
|
menu-complete reverse-menu-complete menu-expand-or-complete menu-select
|
|
|
|
accept-and-menu-complete
|
|
|
|
)
|
2013-10-27 00:05:17 +08:00
|
|
|
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-pause() {
|
|
|
|
[[ -z $ZLE_AUTOSUGGESTING ]] && return
|
|
|
|
unset ZLE_AUTOSUGGESTING
|
2013-10-28 01:40:10 +08:00
|
|
|
local widget
|
2013-10-27 00:05:17 +08:00
|
|
|
# When autosuggestions are disabled, kill the unmaterialized part
|
|
|
|
RBUFFER=''
|
2013-10-29 21:46:39 +08:00
|
|
|
zle -A self-insert autosuggest-paused-self-insert
|
2013-10-27 21:49:21 +08:00
|
|
|
zle -A .magic-space magic-space
|
|
|
|
zle -A .backward-delete-char backward-delete-char
|
2013-10-27 00:05:17 +08:00
|
|
|
zle -A .accept-line accept-line
|
2013-10-28 01:40:10 +08:00
|
|
|
for widget in $ZLE_AUTOSUGGEST_PAUSE_WIDGETS; do
|
|
|
|
eval "zle -A autosuggest-${widget}-orig ${widget}"
|
|
|
|
done
|
|
|
|
for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
|
|
|
|
eval "zle -A autosuggest-${widget}-orig $widget"
|
|
|
|
done
|
2013-10-29 21:46:39 +08:00
|
|
|
autosuggest-highlight-suggested-text
|
2013-10-30 01:27:42 +08:00
|
|
|
|
|
|
|
zle -F $ZLE_AUTOSUGGEST_CONNECTION
|
2013-10-27 00:05:17 +08:00
|
|
|
}
|
|
|
|
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-resume() {
|
2013-10-28 01:40:10 +08:00
|
|
|
[[ -n $ZLE_AUTOSUGGESTING ]] && return
|
2013-10-27 00:05:17 +08:00
|
|
|
ZLE_AUTOSUGGESTING=1
|
2013-10-30 01:27:42 +08:00
|
|
|
local widget
|
2013-10-27 00:05:17 +08:00
|
|
|
# Replace prediction widgets by versions that will also highlight RBUFFER
|
2013-10-29 21:46:39 +08:00
|
|
|
zle -N self-insert autosuggest-insert-or-space
|
|
|
|
zle -N magic-space autosuggest-insert-or-space
|
|
|
|
zle -N backward-delete-char autosuggest-backward-delete-char
|
2013-10-28 01:40:10 +08:00
|
|
|
zle -N accept-line autosuggest-accept-line
|
|
|
|
# Hook into some default widgets that should pause autosuggestion
|
2013-10-27 00:05:17 +08:00
|
|
|
# automatically
|
2013-10-28 01:40:10 +08:00
|
|
|
for widget in $ZLE_AUTOSUGGEST_PAUSE_WIDGETS; do
|
|
|
|
eval "zle -A $widget autosuggest-${widget}-orig; \
|
2013-10-30 01:27:42 +08:00
|
|
|
zle -A autosuggest-suspend $widget"
|
2013-10-28 01:40:10 +08:00
|
|
|
done
|
|
|
|
# Hook into completion widgets to handle suggestions after completions
|
|
|
|
for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
|
|
|
|
eval "zle -A $widget autosuggest-${widget}-orig; \
|
|
|
|
zle -A autosuggest-tab $widget"
|
|
|
|
done
|
2013-10-27 00:05:17 +08:00
|
|
|
if [[ $BUFFER != '' ]]; then
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-request-suggestion
|
2013-10-27 00:05:17 +08:00
|
|
|
fi
|
|
|
|
|
2013-10-30 01:27:42 +08:00
|
|
|
if [[ -n $ZLE_AUTOSUGGEST_CONNECTION ]]; then
|
|
|
|
# install listen for suggestions asynchronously
|
|
|
|
zle -F $ZLE_AUTOSUGGEST_CONNECTION autosuggest-pop-suggestion
|
2013-10-27 01:11:53 +08:00
|
|
|
fi
|
2013-10-30 01:27:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
autosuggest-start() {
|
|
|
|
autosuggest-resume
|
|
|
|
zle recursive-edit
|
|
|
|
integer rv=$?
|
|
|
|
autosuggest-pause
|
2013-10-27 01:11:53 +08:00
|
|
|
zle -A .self-insert self-insert
|
2013-10-30 01:27:42 +08:00
|
|
|
(( rv )) || zle accept-line
|
|
|
|
return rv
|
2013-10-27 01:11:53 +08:00
|
|
|
}
|
|
|
|
|
2013-10-27 00:05:17 +08:00
|
|
|
# Toggles autosuggestions on/off
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-toggle() {
|
|
|
|
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
|
|
|
|
autosuggest-pause
|
2013-10-27 00:05:17 +08:00
|
|
|
else
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-resume
|
2013-10-27 00:05:17 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:46:39 +08:00
|
|
|
autosuggest-highlight-suggested-text() {
|
2013-10-27 00:05:17 +08:00
|
|
|
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
|
2013-10-27 02:05:12 +08:00
|
|
|
local color='fg=8'
|
|
|
|
[[ -n $AUTOSUGGESTION_HIGHLIGHT_COLOR ]] &&\
|
2013-10-28 01:40:10 +08:00
|
|
|
color=$AUTOSUGGESTION_HIGHLIGHT_COLOR
|
2013-10-27 02:05:12 +08:00
|
|
|
region_highlight=("$(( $CURSOR + 1 )) $(( $CURSOR + $#RBUFFER )) $color")
|
2013-10-27 00:05:17 +08:00
|
|
|
else
|
|
|
|
region_highlight=()
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:46:39 +08:00
|
|
|
autosuggest-insert-or-space() {
|
|
|
|
if [[ $LBUFFER == *$'\012'* ]] || (( PENDING )); then
|
|
|
|
# Editing a multiline buffer or pasting in a chunk of text, dont
|
|
|
|
# autosuggest
|
|
|
|
zle .$WIDGET "$@"
|
|
|
|
elif [[ ${RBUFFER[1]} == ${KEYS[-1]} ]]; then
|
|
|
|
# Same as what's typed, just move on
|
2013-10-28 01:40:10 +08:00
|
|
|
((++CURSOR))
|
2013-10-29 21:46:39 +08:00
|
|
|
autosuggest-highlight-suggested-text
|
2013-10-28 01:40:10 +08:00
|
|
|
else
|
2013-10-29 21:46:39 +08:00
|
|
|
LBUFFER="$LBUFFER$KEYS"
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-request-suggestion
|
2013-10-28 01:40:10 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:46:39 +08:00
|
|
|
autosuggest-backward-delete-char() {
|
|
|
|
if ! (( $CURSOR )); then
|
|
|
|
zle .kill-whole-line
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $LBUFFER == *$'\012'* || $LASTWIDGET != (self-insert|magic-space|backward-delete-char) ]]; then
|
|
|
|
# When editing a multiline buffer or if the last widget was e.g. a motion,
|
|
|
|
# then probably the intent is to actually edit the line, not change the
|
|
|
|
# search prefix.
|
|
|
|
LBUFFER="$LBUFFER[1,-2]"
|
2013-10-28 01:40:10 +08:00
|
|
|
else
|
2013-10-29 21:46:39 +08:00
|
|
|
((--CURSOR))
|
|
|
|
zle .history-beginning-search-forward || RBUFFER=''
|
2013-10-28 01:40:10 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# When autosuggesting, ignore RBUFFER which corresponds to the 'unmaterialized'
|
|
|
|
# section when the user accepts the line
|
|
|
|
autosuggest-accept-line() {
|
|
|
|
RBUFFER=''
|
|
|
|
region_highlight=()
|
|
|
|
zle .accept-line
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:46:39 +08:00
|
|
|
autosuggest-paused-self-insert() {
|
2013-10-27 17:53:52 +08:00
|
|
|
if [[ $RBUFFER == '' ]]; then
|
|
|
|
# Resume autosuggestions when inserting at the end of the line
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-enable
|
2013-10-29 21:46:39 +08:00
|
|
|
zle autosuggest-modify
|
2013-10-27 17:53:52 +08:00
|
|
|
else
|
|
|
|
zle .self-insert
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-10-30 01:27:42 +08:00
|
|
|
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() {
|
2013-10-29 21:46:39 +08:00
|
|
|
if (( $CURSOR == 0 )) || [[ ${LBUFFER[-1]} == ' ' ]]; then
|
|
|
|
RBUFFER=''
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2013-10-29 04:05:57 +08:00
|
|
|
[[ -n $ZLE_DISABLE_AUTOSUGGEST || $LBUFFER == '' ]] && return
|
2013-10-29 19:17:03 +08:00
|
|
|
zle .history-beginning-search-backward ||\
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-first-completion ${LBUFFER}
|
2013-10-29 21:46:39 +08:00
|
|
|
autosuggest-highlight-suggested-text
|
2013-10-27 21:49:21 +08:00
|
|
|
}
|
|
|
|
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-suspend() {
|
|
|
|
autosuggest-pause
|
2013-10-28 01:40:10 +08:00
|
|
|
zle autosuggest-${WIDGET}-orig "$@"
|
2013-10-27 21:49:21 +08:00
|
|
|
}
|
|
|
|
|
2013-10-28 01:40:10 +08:00
|
|
|
autosuggest-tab() {
|
2013-10-27 21:49:21 +08:00
|
|
|
RBUFFER=''
|
2013-10-28 01:40:10 +08:00
|
|
|
zle autosuggest-${WIDGET}-orig "$@"
|
2013-10-29 21:46:39 +08:00
|
|
|
autosuggest-highlight-suggested-text
|
2013-10-27 21:49:21 +08:00
|
|
|
}
|
|
|
|
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-accept-suggested-small-word() {
|
2013-10-27 21:49:21 +08:00
|
|
|
zle .vi-forward-word
|
2013-10-29 21:46:39 +08:00
|
|
|
autosuggest-highlight-suggested-text
|
2013-10-27 00:05:17 +08:00
|
|
|
}
|
|
|
|
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-accept-suggested-word() {
|
2013-10-27 21:49:21 +08:00
|
|
|
zle .forward-word
|
2013-10-29 21:46:39 +08:00
|
|
|
autosuggest-highlight-suggested-text
|
2013-10-27 00:05:17 +08:00
|
|
|
}
|
|
|
|
|
2013-10-30 01:27:42 +08:00
|
|
|
zle -N autosuggest-toggle
|
|
|
|
zle -N autosuggest-start
|
|
|
|
zle -N autosuggest-accept-suggested-small-word
|
|
|
|
zle -N autosuggest-accept-suggested-word
|
|
|
|
zle -N autosuggest-suspend
|
2013-10-28 01:40:10 +08:00
|
|
|
zle -N autosuggest-tab
|