zsh-autosuggestions/autosuggestions.zsh

229 lines
5.9 KiB
Bash
Raw Normal View History

# Fish-like autosuggestions for zsh. Some of the code was based on the code
# for 'predict-on'
2013-10-26 20:05:12 +02:00
#
# ```zsh
# zle-line-init() {
# autosuggest-enable
# }
# zle -N zle-line-init
2013-10-26 20:05:12 +02:00
# ```
zmodload zsh/net/socket
source "${0:a:h}/completion-client.zsh"
function {
[[ -n $ZLE_DISABLE_AUTOSUGGEST ]] && return
autosuggest-ensure-server
}
2013-10-26 18:05:17 +02:00
2013-10-27 18:40:10 +01: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-26 18:05:17 +02:00
autosuggest-pause() {
[[ -z $ZLE_AUTOSUGGESTING ]] && return
unset ZLE_AUTOSUGGESTING
2013-10-27 18:40:10 +01:00
local widget
2013-10-26 18:05:17 +02:00
# When autosuggestions are disabled, kill the unmaterialized part
RBUFFER=''
zle -A self-insert autosuggest-paused-self-insert
zle -A .magic-space magic-space
zle -A .backward-delete-char backward-delete-char
2013-10-26 18:05:17 +02:00
zle -A .accept-line accept-line
2013-10-27 18:40:10 +01: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
autosuggest-highlight-suggested-text
zle -F $ZLE_AUTOSUGGEST_CONNECTION
2013-10-26 18:05:17 +02:00
}
autosuggest-resume() {
2013-10-27 18:40:10 +01:00
[[ -n $ZLE_AUTOSUGGESTING ]] && return
2013-10-26 18:05:17 +02:00
ZLE_AUTOSUGGESTING=1
local widget
2013-10-26 18:05:17 +02:00
# Replace prediction widgets by versions that will also highlight RBUFFER
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-27 18:40:10 +01:00
zle -N accept-line autosuggest-accept-line
# Hook into some default widgets that should pause autosuggestion
2013-10-26 18:05:17 +02:00
# automatically
2013-10-27 18:40:10 +01:00
for widget in $ZLE_AUTOSUGGEST_PAUSE_WIDGETS; do
eval "zle -A $widget autosuggest-${widget}-orig; \
zle -A autosuggest-suspend $widget"
2013-10-27 18:40:10 +01: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-26 18:05:17 +02:00
if [[ $BUFFER != '' ]]; then
autosuggest-request-suggestion
2013-10-26 18:05:17 +02:00
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
}
2013-10-26 18:05:17 +02:00
# Toggles autosuggestions on/off
autosuggest-toggle() {
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
autosuggest-pause
2013-10-26 18:05:17 +02:00
else
autosuggest-resume
2013-10-26 18:05:17 +02:00
fi
}
autosuggest-highlight-suggested-text() {
2013-10-26 18:05:17 +02:00
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
2013-10-26 20:05:12 +02:00
local color='fg=8'
[[ -n $AUTOSUGGESTION_HIGHLIGHT_COLOR ]] &&\
2013-10-27 18:40:10 +01:00
color=$AUTOSUGGESTION_HIGHLIGHT_COLOR
2013-10-26 20:05:12 +02:00
region_highlight=("$(( $CURSOR + 1 )) $(( $CURSOR + $#RBUFFER )) $color")
2013-10-26 18:05:17 +02:00
else
region_highlight=()
fi
}
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-27 18:40:10 +01:00
((++CURSOR))
autosuggest-highlight-suggested-text
2013-10-27 18:40:10 +01:00
else
LBUFFER="$LBUFFER$KEYS"
autosuggest-request-suggestion
2013-10-27 18:40:10 +01:00
fi
}
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-27 18:40:10 +01:00
else
((--CURSOR))
zle .history-beginning-search-forward || RBUFFER=''
2013-10-27 18:40:10 +01: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
}
autosuggest-paused-self-insert() {
if [[ $RBUFFER == '' ]]; then
# Resume autosuggestions when inserting at the end of the line
autosuggest-enable
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=''
return
fi
[[ -n $ZLE_DISABLE_AUTOSUGGEST || $LBUFFER == '' ]] && return
2013-10-29 12:17:03 +01:00
zle .history-beginning-search-backward ||\
autosuggest-first-completion ${LBUFFER}
autosuggest-highlight-suggested-text
}
autosuggest-suspend() {
autosuggest-pause
2013-10-27 18:40:10 +01:00
zle autosuggest-${WIDGET}-orig "$@"
}
2013-10-27 18:40:10 +01:00
autosuggest-tab() {
RBUFFER=''
2013-10-27 18:40:10 +01:00
zle autosuggest-${WIDGET}-orig "$@"
autosuggest-highlight-suggested-text
}
autosuggest-accept-suggested-small-word() {
zle .vi-forward-word
autosuggest-highlight-suggested-text
2013-10-26 18:05:17 +02:00
}
autosuggest-accept-suggested-word() {
zle .forward-word
autosuggest-highlight-suggested-text
2013-10-26 18:05:17 +02: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-27 18:40:10 +01:00
zle -N autosuggest-tab