#--------------------------------------------------------------------## Autosuggest Widget Implementations ##--------------------------------------------------------------------## Disable suggestions_zsh_autosuggest_disable(){typeset -g _ZSH_AUTOSUGGEST_DISABLED
_zsh_autosuggest_clear
}# Enable suggestions_zsh_autosuggest_enable(){unset _ZSH_AUTOSUGGEST_DISABLED
if(($#BUFFER )); then _zsh_autosuggest_fetch
fi}# Toggle suggestions (enable/disable)_zsh_autosuggest_toggle(){if((${+_ZSH_AUTOSUGGEST_DISABLED})); then _zsh_autosuggest_enable
else _zsh_autosuggest_disable
fi}# Clear the suggestion_zsh_autosuggest_clear(){# Remove the suggestionPOSTDISPLAY= _zsh_autosuggest_invoke_original_widget $@}# Modify the buffer and get a new suggestion_zsh_autosuggest_modify(){local -i retval
# Only available in zsh >= 5.4local -i KEYS_QUEUED_COUNT
# Save the contents of the buffer/postdisplaylocalorig_buffer="$BUFFER"localorig_postdisplay="$POSTDISPLAY"# Clear suggestion while waiting for next onePOSTDISPLAY=# Original widget may modify the buffer _zsh_autosuggest_invoke_original_widget $@retval=$? emulate -L zsh
# Don't fetch a new suggestion if there's more input to be read immediatelyif(($PENDING > 0||$KEYS_QUEUED_COUNT > 0)); thenPOSTDISPLAY="$orig_postdisplay"return$retvalfi# Optimize if manually typing in the suggestion or if buffer hasn't changedif[["$BUFFER"="$orig_buffer"* &&"$orig_postdisplay"="${BUFFER:$#orig_buffer}"* ]]; thenPOSTDISPLAY="${orig_postdisplay:$(($#BUFFER-$#orig_buffer))}"return$retvalfi# Bail out if suggestions are disabledif((${+_ZSH_AUTOSUGGEST_DISABLED})); thenreturn$?fi# Get a new suggestion if the buffer is not empty after modificationif(($#BUFFER > 0)); thenif[[ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE"]]||(($#BUFFER <=$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE)); then _zsh_autosuggest_fetch
fifireturn$retval}# Fetch a new suggestion based on what's currently in the buffer_zsh_autosuggest_fetch(){if((${+ZSH_AUTOSUGGEST_USE_ASYNC})); then _zsh_autosuggest_async_request "$BUFFER"elselocal suggestion
_zsh_autosuggest_fetch_suggestion "$BUFFER" _zsh_autosuggest_suggest "$suggestion"fi}# Offer a suggestion_zsh_autosuggest_suggest(){ emulate -L zsh
localsuggestion="$1"if[[ -n "$suggestion"]]&&(($#BUFFER )); thenPOSTDISPLAY="${suggestion#$BUFFER}"elsePOSTDISPLAY=fi}# Accept the entire suggestion_zsh_autosuggest_accept(){local -i retval max_cursor_pos=$#BUFFER
# When vicmd keymap is active, the cursor can't move all the way# to the end of the bufferif[["$KEYMAP"="vicmd"]]; thenmax_cursor_pos=$((max_cursor_pos-1))fi# If we're not in a valid state to accept a suggestion, just run the# original widget and bail outif(($CURSOR !=$max_cursor_pos|| !$#POSTDISPLAY )); then _zsh_autosuggest_invoke_original_widget $@returnfi# Only accept if the cursor is at the end of the buffer# Add the suggestion to the bufferBUFFER="$BUFFER$POSTDISPLAY"# Remove the suggestionPOSTDISPLAY=# Run the original widget before manually moving the cursor so that the# cursor movement doesn't make the widget do something unexpected _zsh_autosuggest_invoke_original_widget $@retval=$?# Move the cursor to the end of the bufferif[["$KEYMAP"="vicmd"]]; thenCURSOR=$(($#BUFFER-1))elseCURSOR=$#BUFFER
fireturn$retval}# Accept the entire suggestion and execute it_zsh_autosuggest_execute(){# Add the suggestion to the bufferBUFFER="$BUFFER$POSTDISPLAY"# Remove the suggestionPOSTDISPLAY=# Call the original `accept-line` to handle syntax highlighting or# other potential custom behavior _zsh_autosuggest_invoke_original_widget "accept-line"}# Partially accept the suggestion_zsh_autosuggest_partial_accept(){local -i retval cursor_loc
# Save the contents of the buffer so we can restore later if neededlocaloriginal_buffer="$BUFFER"# Temporarily accept the suggestion.BUFFER="$BUFFER$POSTDISPLAY"# Original widget moves the cursor _zsh_autosuggest_invoke_original_widget $@retval=$?# Normalize cursor location across vi/emacs modescursor_loc=$CURSORif[["$KEYMAP"="vicmd"]]; thencursor_loc=$((cursor_loc+1))fi# If we've moved past the end of the original bufferif(($cursor_loc > $#original_buffer )); then# Set POSTDISPLAY to text right of the cursorPOSTDISPLAY="${BUFFER[$(($cursor_loc+1)),$#BUFFER]}"# Clip the buffer at the cursorBUFFER="${BUFFER[1,$cursor_loc]}"else# Restore the original bufferBUFFER="$original_buffer"fireturn$retval}(){typeset -ga _ZSH_AUTOSUGGEST_BUILTIN_ACTIONS
_ZSH_AUTOSUGGEST_BUILTIN_ACTIONS=( clear
fetch
suggest
accept
execute
enable disable
toggle
)local action
for action in$_ZSH_AUTOSUGGEST_BUILTIN_ACTIONS modify partial_accept; doeval"_zsh_autosuggest_widget_$action() { local -i retval _zsh_autosuggest_highlight_reset _zsh_autosuggest_$action \$@ retval=\$? _zsh_autosuggest_highlight_apply zle -R return \$retval }"donefor action in$_ZSH_AUTOSUGGEST_BUILTIN_ACTIONS; do zle -N autosuggest-$action _zsh_autosuggest_widget_$actiondone}