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
|
|
|
|
2017-03-03 19:59:30 +01:00
|
|
|
# Disable suggestions
|
|
|
|
_zsh_autosuggest_disable() {
|
|
|
|
typeset -g _ZSH_AUTOSUGGEST_DISABLED
|
|
|
|
_zsh_autosuggest_clear
|
|
|
|
}
|
|
|
|
|
|
|
|
# Enable suggestions
|
|
|
|
_zsh_autosuggest_enable() {
|
|
|
|
unset _ZSH_AUTOSUGGEST_DISABLED
|
2017-03-04 23:04:04 +01:00
|
|
|
|
2017-07-17 22:45:48 +02:00
|
|
|
if (( $#BUFFER )); then
|
2017-03-04 23:04:04 +01:00
|
|
|
_zsh_autosuggest_fetch
|
|
|
|
fi
|
2017-03-03 19:59:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Toggle suggestions (enable/disable)
|
|
|
|
_zsh_autosuggest_toggle() {
|
2017-07-17 22:45:48 +02:00
|
|
|
if [[ -n "${_ZSH_AUTOSUGGEST_DISABLED+x}" ]]; then
|
2017-03-03 19:59:30 +01:00
|
|
|
_zsh_autosuggest_enable
|
|
|
|
else
|
|
|
|
_zsh_autosuggest_disable
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2017-09-27 23:04:42 +02:00
|
|
|
# Only available in zsh >= 5.4
|
2017-02-28 19:18:21 +01:00
|
|
|
local -i KEYS_QUEUED_COUNT
|
|
|
|
|
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
|
|
|
|
2018-12-09 18:14:19 +01:00
|
|
|
emulate -L zsh
|
|
|
|
|
2017-02-28 19:18:21 +01:00
|
|
|
# Don't fetch a new suggestion if there's more input to be read immediately
|
2017-07-17 22:45:48 +02:00
|
|
|
if (( $PENDING > 0 )) || (( $KEYS_QUEUED_COUNT > 0 )); then
|
2018-05-15 19:32:46 +02:00
|
|
|
POSTDISPLAY="$orig_postdisplay"
|
2017-02-28 19:18:21 +01:00
|
|
|
return $retval
|
|
|
|
fi
|
|
|
|
|
2017-01-25 08:00:13 +01:00
|
|
|
# Optimize if manually typing in the suggestion
|
2017-07-17 22:45:48 +02:00
|
|
|
if (( $#BUFFER > $#orig_buffer )); then
|
2017-01-25 08:00:13 +01:00
|
|
|
local added=${BUFFER#$orig_buffer}
|
|
|
|
|
|
|
|
# If the string added matches the beginning of the postdisplay
|
2017-07-17 22:45:48 +02:00
|
|
|
if [[ "$added" = "${orig_postdisplay:0:$#added}" ]]; then
|
2017-01-25 08:00:13 +01:00
|
|
|
POSTDISPLAY="${orig_postdisplay:$#added}"
|
|
|
|
return $retval
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2016-08-01 04:10:22 +02:00
|
|
|
# Don't fetch a new suggestion if the buffer hasn't changed
|
2017-07-17 22:45:48 +02:00
|
|
|
if [[ "$BUFFER" = "$orig_buffer" ]]; then
|
2016-08-01 04:10:22 +02:00
|
|
|
POSTDISPLAY="$orig_postdisplay"
|
|
|
|
return $retval
|
|
|
|
fi
|
|
|
|
|
2017-03-03 19:59:30 +01:00
|
|
|
# Bail out if suggestions are disabled
|
2017-07-17 22:45:48 +02:00
|
|
|
if [[ -n "${_ZSH_AUTOSUGGEST_DISABLED+x}" ]]; then
|
2017-03-03 19:59:30 +01:00
|
|
|
return $?
|
|
|
|
fi
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Get a new suggestion if the buffer is not empty after modification
|
2017-07-17 22:45:48 +02:00
|
|
|
if (( $#BUFFER > 0 )); then
|
|
|
|
if [[ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]] || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )); then
|
2017-01-27 23:18:26 +01:00
|
|
|
_zsh_autosuggest_fetch
|
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
|
|
|
}
|
|
|
|
|
2017-01-27 23:18:26 +01:00
|
|
|
# Fetch a new suggestion based on what's currently in the buffer
|
|
|
|
_zsh_autosuggest_fetch() {
|
2018-06-11 10:06:18 +02:00
|
|
|
if [[ -n "${ZSH_AUTOSUGGEST_USE_ASYNC+x}" ]]; then
|
2017-01-27 23:18:26 +01:00
|
|
|
_zsh_autosuggest_async_request "$BUFFER"
|
|
|
|
else
|
|
|
|
local suggestion
|
2018-06-07 05:49:03 +02:00
|
|
|
_zsh_autosuggest_fetch_suggestion "$BUFFER"
|
2017-01-27 23:18:26 +01:00
|
|
|
_zsh_autosuggest_suggest "$suggestion"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Offer a suggestion
|
|
|
|
_zsh_autosuggest_suggest() {
|
2018-07-14 06:16:53 +02:00
|
|
|
emulate -L zsh
|
|
|
|
|
2017-01-27 23:18:26 +01:00
|
|
|
local suggestion="$1"
|
|
|
|
|
2017-07-17 22:45:48 +02:00
|
|
|
if [[ -n "$suggestion" ]] && (( $#BUFFER )); then
|
2017-01-27 23:18:26 +01:00
|
|
|
POSTDISPLAY="${suggestion#$BUFFER}"
|
|
|
|
else
|
|
|
|
unset POSTDISPLAY
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
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
|
2017-07-17 22:45:48 +02:00
|
|
|
if [[ "$KEYMAP" = "vicmd" ]]; then
|
2016-03-15 04:41:14 +01:00
|
|
|
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
|
2017-07-17 22:45:48 +02:00
|
|
|
if [[ $CURSOR = $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() {
|
2018-05-14 18:59:08 +02:00
|
|
|
local -i retval cursor_loc
|
2016-04-07 01:10:34 +02:00
|
|
|
|
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
|
|
|
|
2018-05-14 18:59:08 +02:00
|
|
|
# Normalize cursor location across vi/emacs modes
|
|
|
|
cursor_loc=$CURSOR
|
|
|
|
if [[ "$KEYMAP" = "vicmd" ]]; then
|
|
|
|
cursor_loc=$((cursor_loc + 1))
|
|
|
|
fi
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# If we've moved past the end of the original buffer
|
2018-05-14 18:59:08 +02:00
|
|
|
if (( $cursor_loc > $#original_buffer )); then
|
2016-02-05 23:14:08 +01:00
|
|
|
# Set POSTDISPLAY to text right of the cursor
|
2018-05-14 18:59:08 +02:00
|
|
|
POSTDISPLAY="${BUFFER[$(($cursor_loc + 1)),$#BUFFER]}"
|
2016-02-05 23:14:08 +01:00
|
|
|
|
|
|
|
# Clip the buffer at the cursor
|
2018-05-14 18:59:08 +02:00
|
|
|
BUFFER="${BUFFER[1,$cursor_loc]}"
|
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
|
|
|
}
|
|
|
|
|
2018-12-19 08:20:57 +01:00
|
|
|
() {
|
|
|
|
local action
|
|
|
|
for action in clear modify fetch suggest accept partial_accept execute enable disable toggle; do
|
|
|
|
eval "_zsh_autosuggest_widget_$action() {
|
|
|
|
local -i retval
|
2016-04-07 01:10:34 +02:00
|
|
|
|
2018-12-19 08:20:57 +01:00
|
|
|
_zsh_autosuggest_highlight_reset
|
2016-04-07 01:10:34 +02:00
|
|
|
|
2018-12-19 08:20:57 +01:00
|
|
|
_zsh_autosuggest_$action \$@
|
|
|
|
retval=\$?
|
2016-04-07 01:10:34 +02:00
|
|
|
|
2018-12-19 08:20:57 +01:00
|
|
|
_zsh_autosuggest_highlight_apply
|
2016-04-07 01:10:34 +02:00
|
|
|
|
2018-12-19 08:20:57 +01:00
|
|
|
zle -R
|
2017-01-27 23:18:26 +01:00
|
|
|
|
2018-12-19 08:20:57 +01:00
|
|
|
return \$retval
|
|
|
|
}"
|
|
|
|
done
|
2016-02-05 23:14:08 +01:00
|
|
|
|
2018-12-22 07:20:08 +01:00
|
|
|
zle -N autosuggest-fetch _zsh_autosuggest_widget_fetch
|
|
|
|
zle -N autosuggest-suggest _zsh_autosuggest_widget_suggest
|
|
|
|
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
|
|
|
|
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
|
|
|
|
zle -N autosuggest-execute _zsh_autosuggest_widget_execute
|
|
|
|
zle -N autosuggest-enable _zsh_autosuggest_widget_enable
|
|
|
|
zle -N autosuggest-disable _zsh_autosuggest_widget_disable
|
|
|
|
zle -N autosuggest-toggle _zsh_autosuggest_widget_toggle
|
|
|
|
}
|