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() {
|
2016-04-07 01:10:34 +02:00
|
|
|
local -i retval
|
|
|
|
|
2016-08-01 04:10:22 +02:00
|
|
|
# Save the contents of the buffer/postdisplay
|
|
|
|
local orig_buffer="$BUFFER"
|
|
|
|
local orig_postdisplay="$POSTDISPLAY"
|
|
|
|
|
2016-07-20 05:04:18 +02:00
|
|
|
# Clear suggestion while waiting for next one
|
2016-04-25 22:42:09 +02:00
|
|
|
unset POSTDISPLAY
|
|
|
|
|
2016-08-01 04:10:22 +02:00
|
|
|
# Original widget may modify the buffer
|
2016-02-14 07:46:34 +01:00
|
|
|
_zsh_autosuggest_invoke_original_widget $@
|
2016-04-07 01:10:34 +02:00
|
|
|
retval=$?
|
2016-02-05 23:14:08 +01:00
|
|
|
|
2016-08-01 04:10:22 +02:00
|
|
|
# Don't fetch a new suggestion if the buffer hasn't changed
|
|
|
|
if [ "$BUFFER" = "$orig_buffer" ]; then
|
|
|
|
POSTDISPLAY="$orig_postdisplay"
|
|
|
|
return $retval
|
|
|
|
fi
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Get a new suggestion if the buffer is not empty after modification
|
|
|
|
if [ $#BUFFER -gt 0 ]; then
|
2016-07-18 11:55:19 +02:00
|
|
|
if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -lt "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then
|
2016-07-20 05:04:18 +02:00
|
|
|
_zsh_autosuggest_async_fetch_suggestion "$BUFFER"
|
2016-07-15 10:39:33 +02:00
|
|
|
fi
|
2016-02-05 23:14:08 +01:00
|
|
|
fi
|
|
|
|
|
2016-04-07 01:10:34 +02:00
|
|
|
return $retval
|
2016-02-05 23:14:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Accept the entire suggestion
|
|
|
|
_zsh_autosuggest_accept() {
|
2016-03-15 04:41:14 +01:00
|
|
|
local -i max_cursor_pos=$#BUFFER
|
|
|
|
|
|
|
|
# When vicmd keymap is active, the cursor can't move all the way
|
|
|
|
# to the end of the buffer
|
|
|
|
if [ "$KEYMAP" = "vicmd" ]; then
|
|
|
|
max_cursor_pos=$((max_cursor_pos - 1))
|
|
|
|
fi
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Only accept if the cursor is at the end of the buffer
|
2016-03-15 04:41:14 +01:00
|
|
|
if [ $CURSOR -eq $max_cursor_pos ]; then
|
2016-02-05 23:14:08 +01:00
|
|
|
# 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
|
|
|
|
|
2016-02-23 18:20:13 +01:00
|
|
|
# Call the original `accept-line` to handle syntax highlighting or
|
|
|
|
# other potential custom behavior
|
|
|
|
_zsh_autosuggest_invoke_original_widget "accept-line"
|
2016-02-20 14:52:21 +01:00
|
|
|
}
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Partially accept the suggestion
|
|
|
|
_zsh_autosuggest_partial_accept() {
|
2016-04-07 01:10:34 +02:00
|
|
|
local -i retval
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Save the contents of the buffer so we can restore later if needed
|
2016-03-06 05:03:14 +01:00
|
|
|
local original_buffer="$BUFFER"
|
2016-02-05 23:14:08 +01:00
|
|
|
|
|
|
|
# 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-04-07 01:10:34 +02:00
|
|
|
retval=$?
|
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
|
2016-03-06 05:03:14 +01:00
|
|
|
POSTDISPLAY="$RBUFFER"
|
2016-02-05 23:14:08 +01:00
|
|
|
|
|
|
|
# Clip the buffer at the cursor
|
2016-03-06 05:03:14 +01:00
|
|
|
BUFFER="$LBUFFER"
|
2016-02-05 23:14:08 +01:00
|
|
|
else
|
|
|
|
# Restore the original buffer
|
2016-03-06 05:03:14 +01:00
|
|
|
BUFFER="$original_buffer"
|
2016-02-05 23:14:08 +01:00
|
|
|
fi
|
2016-04-07 01:10:34 +02:00
|
|
|
|
|
|
|
return $retval
|
2016-02-05 23:14:08 +01:00
|
|
|
}
|
|
|
|
|
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() {
|
2016-04-07 01:10:34 +02:00
|
|
|
local -i retval
|
|
|
|
|
2016-02-14 16:54:34 +01:00
|
|
|
_zsh_autosuggest_highlight_reset
|
2016-04-07 01:10:34 +02:00
|
|
|
|
2016-02-14 16:54:34 +01:00
|
|
|
_zsh_autosuggest_$action \$@
|
2016-04-07 01:10:34 +02:00
|
|
|
retval=\$?
|
|
|
|
|
2016-02-14 16:54:34 +01:00
|
|
|
_zsh_autosuggest_highlight_apply
|
2016-04-07 01:10:34 +02:00
|
|
|
|
|
|
|
return \$retval
|
2016-02-14 16:54:34 +01:00
|
|
|
}"
|
|
|
|
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
|
2016-07-20 05:04:18 +02:00
|
|
|
|
|
|
|
_zsh_autosuggest_show_suggestion() {
|
2017-01-25 06:27:47 +01:00
|
|
|
local suggestion="$1"
|
2016-07-20 05:04:18 +02:00
|
|
|
|
|
|
|
_zsh_autosuggest_highlight_reset
|
|
|
|
|
|
|
|
if [ -n "$suggestion" ]; then
|
|
|
|
POSTDISPLAY="${suggestion#$BUFFER}"
|
|
|
|
else
|
|
|
|
unset POSTDISPLAY
|
|
|
|
fi
|
|
|
|
|
|
|
|
_zsh_autosuggest_highlight_apply
|
|
|
|
|
|
|
|
zle -R
|
|
|
|
}
|
|
|
|
|
|
|
|
zle -N _autosuggest-show-suggestion _zsh_autosuggest_show_suggestion
|