2016-02-05 23:14:08 +01:00
|
|
|
|
2016-02-14 08:29:43 +01:00
|
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Autosuggest Widget Implementations #
|
|
|
|
#--------------------------------------------------------------------#
|
2016-02-05 23:14:08 +01:00
|
|
|
|
|
|
|
# Clear the suggestion
|
|
|
|
_zsh_autosuggest_clear() {
|
|
|
|
# Remove the suggestion
|
|
|
|
unset POSTDISPLAY
|
|
|
|
|
2016-02-14 07:46:34 +01:00
|
|
|
_zsh_autosuggest_invoke_original_widget $@
|
2016-02-05 23:14:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Modify the buffer and get a new suggestion
|
|
|
|
_zsh_autosuggest_modify() {
|
|
|
|
# Original widget modifies the buffer
|
2016-02-14 07:46:34 +01:00
|
|
|
_zsh_autosuggest_invoke_original_widget $@
|
2016-02-05 23:14:08 +01:00
|
|
|
|
|
|
|
# Get a new suggestion if the buffer is not empty after modification
|
|
|
|
local suggestion
|
|
|
|
if [ $#BUFFER -gt 0 ]; then
|
2016-02-16 15:57:44 +01:00
|
|
|
suggestion=$(_zsh_autosuggest_suggestion "$BUFFER")
|
2016-02-05 23:14:08 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Add the suggestion to the POSTDISPLAY
|
|
|
|
if [ -n "$suggestion" ]; then
|
|
|
|
POSTDISPLAY=${suggestion#$BUFFER}
|
|
|
|
else
|
|
|
|
unset POSTDISPLAY
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Accept the entire suggestion
|
|
|
|
_zsh_autosuggest_accept() {
|
|
|
|
# Only accept if the cursor is at the end of the buffer
|
|
|
|
if [ $CURSOR -eq $#BUFFER ]; then
|
|
|
|
# Add the suggestion to the buffer
|
|
|
|
BUFFER="$BUFFER$POSTDISPLAY"
|
|
|
|
|
|
|
|
# Remove the suggestion
|
|
|
|
unset POSTDISPLAY
|
|
|
|
|
|
|
|
# Move the cursor to the end of the buffer
|
|
|
|
CURSOR=${#BUFFER}
|
|
|
|
fi
|
|
|
|
|
2016-02-14 07:46:34 +01:00
|
|
|
_zsh_autosuggest_invoke_original_widget $@
|
2016-02-05 23:14:08 +01:00
|
|
|
}
|
|
|
|
|
2016-02-20 14:52:21 +01:00
|
|
|
# Accept the entire suggestion and execute it
|
|
|
|
_zsh_autosuggest_execute() {
|
|
|
|
# Add the suggestion to the buffer
|
|
|
|
BUFFER="$BUFFER$POSTDISPLAY"
|
|
|
|
|
|
|
|
# Remove the suggestion
|
|
|
|
unset POSTDISPLAY
|
|
|
|
|
|
|
|
zle .accept-line
|
|
|
|
}
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Partially accept the suggestion
|
|
|
|
_zsh_autosuggest_partial_accept() {
|
|
|
|
# Save the contents of the buffer so we can restore later if needed
|
|
|
|
local original_buffer=$BUFFER
|
|
|
|
|
|
|
|
# Temporarily accept the suggestion.
|
|
|
|
BUFFER="$BUFFER$POSTDISPLAY"
|
|
|
|
|
|
|
|
# Original widget moves the cursor
|
2016-02-14 07:46:34 +01:00
|
|
|
_zsh_autosuggest_invoke_original_widget $@
|
2016-02-05 23:14:08 +01:00
|
|
|
|
|
|
|
# If we've moved past the end of the original buffer
|
|
|
|
if [ $CURSOR -gt $#original_buffer ]; then
|
|
|
|
# Set POSTDISPLAY to text right of the cursor
|
|
|
|
POSTDISPLAY=$RBUFFER
|
|
|
|
|
|
|
|
# Clip the buffer at the cursor
|
|
|
|
BUFFER=$LBUFFER
|
|
|
|
else
|
|
|
|
# Restore the original buffer
|
|
|
|
BUFFER=$original_buffer
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-02-20 14:52:21 +01:00
|
|
|
for action in clear modify accept partial_accept execute; do
|
2016-02-14 16:54:34 +01:00
|
|
|
eval "_zsh_autosuggest_widget_$action() {
|
|
|
|
_zsh_autosuggest_highlight_reset
|
|
|
|
_zsh_autosuggest_$action \$@
|
|
|
|
_zsh_autosuggest_highlight_apply
|
|
|
|
}"
|
|
|
|
done
|
2016-02-05 23:14:08 +01:00
|
|
|
|
|
|
|
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
|
2016-02-07 16:58:09 +01:00
|
|
|
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
|
2016-02-20 14:52:21 +01:00
|
|
|
zle -N autosuggest-execute _zsh_autosuggest_widget_execute
|