2013-10-27 18:40:10 +01:00
|
|
|
# Fish-like autosuggestions for zsh based on the code for 'predict-on'
|
2013-10-26 20:05:12 +02:00
|
|
|
#
|
|
|
|
# ```zsh
|
2013-10-26 19:11:53 +02:00
|
|
|
# zle-line-init() {
|
|
|
|
# enable-autosuggestions
|
|
|
|
# }
|
|
|
|
# zle -N zle-line-init
|
2013-10-26 20:05:12 +02:00
|
|
|
# ```
|
2013-10-27 18:40:10 +01:00
|
|
|
# zmodload zsh/zpty
|
|
|
|
#
|
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
|
|
|
|
2013-10-26 19:11:53 +02:00
|
|
|
pause-autosuggestions() {
|
2013-10-27 18:40:10 +01:00
|
|
|
[[ -n $ZLE_AUTOSUGGESTING_PAUSED ]] && return
|
|
|
|
local widget
|
2013-10-26 18:05:17 +02:00
|
|
|
# When autosuggestions are disabled, kill the unmaterialized part
|
|
|
|
RBUFFER=''
|
|
|
|
unset ZLE_AUTOSUGGESTING
|
2013-10-26 19:11:53 +02:00
|
|
|
ZLE_AUTOSUGGESTING_PAUSED=1
|
2013-10-27 10:53:52 +01:00
|
|
|
zle -A self-insert paused-autosuggest-self-insert
|
2013-10-27 14:49:21 +01:00
|
|
|
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
|
2013-10-26 18:05:17 +02:00
|
|
|
highlight-suggested-text
|
|
|
|
}
|
|
|
|
|
|
|
|
enable-autosuggestions() {
|
2013-10-27 18:40:10 +01:00
|
|
|
[[ -n $ZLE_AUTOSUGGESTING ]] && return
|
|
|
|
local widget
|
2013-10-26 19:11:53 +02:00
|
|
|
unset ZLE_AUTOSUGGESTING_PAUSED
|
2013-10-26 18:05:17 +02:00
|
|
|
ZLE_AUTOSUGGESTING=1
|
|
|
|
# Replace prediction widgets by versions that will also highlight RBUFFER
|
2013-10-27 14:49:21 +01:00
|
|
|
zle -N self-insert autosuggest-self-insert
|
2013-10-27 18:40:10 +01:00
|
|
|
zle -N magic-space autosuggest-self-insert
|
2013-10-27 14:49:21 +01:00
|
|
|
zle -N backward-delete-char autosuggest-delete
|
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-pause $widget"
|
|
|
|
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
|
2013-10-27 18:40:10 +01:00
|
|
|
show-suggestion
|
2013-10-26 18:05:17 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-10-26 19:11:53 +02:00
|
|
|
disable-autosuggestions() {
|
|
|
|
if [[ -z $ZLE_AUTOSUGGESTING_PAUSED ]]; then
|
|
|
|
pause-autosuggestions
|
|
|
|
fi
|
|
|
|
unset ZLE_AUTOSUGGESTING_PAUSED
|
|
|
|
zle -A .self-insert self-insert
|
|
|
|
}
|
|
|
|
|
2013-10-26 18:05:17 +02:00
|
|
|
# Toggles autosuggestions on/off
|
|
|
|
toggle-autosuggestions() {
|
2013-10-26 19:11:53 +02:00
|
|
|
if [[ -n $ZLE_AUTOSUGGESTING || -n $ZLE_AUTOSUGGESTING_PAUSED ]]; then
|
2013-10-26 18:05:17 +02:00
|
|
|
disable-autosuggestions
|
|
|
|
else
|
|
|
|
enable-autosuggestions
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
highlight-suggested-text() {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-10-27 18:40:10 +01:00
|
|
|
autosuggest-self-insert() {
|
|
|
|
setopt localoptions noshwordsplit noksharrays
|
|
|
|
if [[ ${RBUFFER[1]} == ${KEYS[-1]} ]]; then
|
|
|
|
# Same as what's typed, just move on
|
|
|
|
((++CURSOR))
|
|
|
|
highlight-suggested-text
|
|
|
|
else
|
|
|
|
LBUFFER="$LBUFFER$KEYS"
|
|
|
|
show-suggestion
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Taken from predict-on
|
|
|
|
autosuggest-delete() {
|
|
|
|
if (( $#LBUFFER > 1 )); then
|
|
|
|
setopt localoptions noshwordsplit noksharrays
|
|
|
|
# When editing a multiline buffer, it's unlikely prediction is wanted;
|
|
|
|
# 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.
|
|
|
|
if [[ $LASTWIDGET != (self-insert|magic-space|backward-delete-char) ]]; then
|
|
|
|
LBUFFER="$LBUFFER[1,-2]"
|
|
|
|
else
|
|
|
|
((--CURSOR))
|
|
|
|
zle .history-beginning-search-forward || RBUFFER=""
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
zle .kill-whole-line
|
|
|
|
fi
|
|
|
|
highlight-suggested-text
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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-27 10:53:52 +01:00
|
|
|
paused-autosuggest-self-insert() {
|
|
|
|
if [[ $RBUFFER == '' ]]; then
|
|
|
|
# Resume autosuggestions when inserting at the end of the line
|
|
|
|
enable-autosuggestions
|
2013-10-27 14:49:21 +01:00
|
|
|
autosuggest-self-insert
|
2013-10-27 10:53:52 +01:00
|
|
|
else
|
|
|
|
zle .self-insert
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-10-27 18:40:10 +01:00
|
|
|
autosuggest-first-completion() {
|
|
|
|
zle .complete-word || return 1
|
2013-10-26 18:05:17 +02:00
|
|
|
}
|
|
|
|
|
2013-10-27 18:40:10 +01:00
|
|
|
show-suggestion() {
|
|
|
|
[[ $LBUFFER == '' ]] && return
|
|
|
|
# TODO need a way to reset HISTNO so .history-beginning-search-backward
|
|
|
|
# will always retrieve the last matching history entry
|
|
|
|
# unset HISTNO
|
|
|
|
zle .history-beginning-search-backward || autosuggest-first-completion\
|
|
|
|
|| RBUFFER=''
|
|
|
|
highlight-suggested-text
|
2013-10-27 14:49:21 +01:00
|
|
|
}
|
|
|
|
|
2013-10-27 18:40:10 +01:00
|
|
|
autosuggest-pause() {
|
|
|
|
pause-autosuggestions
|
|
|
|
zle autosuggest-${WIDGET}-orig "$@"
|
2013-10-27 14:49:21 +01:00
|
|
|
}
|
|
|
|
|
2013-10-27 18:40:10 +01:00
|
|
|
autosuggest-tab() {
|
2013-10-27 14:49:21 +01:00
|
|
|
RBUFFER=''
|
2013-10-27 18:40:10 +01:00
|
|
|
zle autosuggest-${WIDGET}-orig "$@"
|
2013-10-27 14:49:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
accept-suggested-small-word() {
|
|
|
|
zle .vi-forward-word
|
2013-10-26 18:05:17 +02:00
|
|
|
highlight-suggested-text
|
|
|
|
}
|
|
|
|
|
2013-10-27 14:49:21 +01:00
|
|
|
accept-suggested-word() {
|
|
|
|
zle .forward-word
|
2013-10-26 18:05:17 +02:00
|
|
|
highlight-suggested-text
|
|
|
|
}
|
|
|
|
|
|
|
|
zle -N toggle-autosuggestions
|
2013-10-27 14:49:21 +01:00
|
|
|
zle -N accept-suggested-small-word
|
|
|
|
zle -N accept-suggested-word
|
2013-10-27 18:40:10 +01:00
|
|
|
zle -N autosuggest-pause
|
|
|
|
zle -N autosuggest-tab
|