This commit is contained in:
Eric Freese 2016-01-26 20:51:44 -07:00
commit fc3f368acf
15 changed files with 224 additions and 708 deletions

37
lib/config.zsh Normal file
View file

@ -0,0 +1,37 @@
# Color to use when highlighting autosuggestion
# Uses format of `region_highlight`
# More info: http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Zle-Widgets
ZSH_AUTOSUGGEST_HIGHLIGHT_COLOR='fg=8'
# Widgets that clear the autosuggestion
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
history-search-forward
history-search-backward
history-beginning-search-forward
history-beginning-search-backward
history-substring-search-up
history-substring-search-down
accept-line
)
# Widgets that modify the autosuggestion
ZSH_AUTOSUGGEST_MODIFY_WIDGETS=(
complete-word
expand-or-complete
expand-or-complete-prefix
list-choices
menu-complete
reverse-menu-complete
menu-expand-or-complete
accept-and-menu-complete
self-insert
magic-space
backward-delete-char
bracketed-paste
)
# Widgets that accept the autosuggestion
ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=(
vi-forward-char
forward-char
)

11
lib/get_suggestion.zsh Normal file
View file

@ -0,0 +1,11 @@
_zsh_autosuggest_get_suggestion() {
local prefix=$1
local history_items=(${history[(R)$prefix*]})
for cmd in $history_items; do
if [ "${cmd:0:$#prefix}" = "$prefix" ]; then
echo $cmd
break
fi
done
}

35
lib/highlight.zsh Normal file
View file

@ -0,0 +1,35 @@
_zsh_autosuggest_region_highlight() {
echo "$#BUFFER $(($#BUFFER + $#POSTDISPLAY)) $ZSH_AUTOSUGGEST_HIGHLIGHT_COLOR"
}
_zsh_autosuggest_highlight() {
if _zsh_autosuggest_syntax_highlighting_enabled; then
_zsh_highlight
else
region_highlight=("$(_zsh_autosuggest_region_highlight)")
fi
}
#-------------------------------------------------------------------------------
# Support for zsh-syntax-highlighter
#
# https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
#-------------------------------------------------------------------------------
_zsh_autosuggest_syntax_highlighting_enabled() {
[ -n "$functions[_zsh_highlight]" ]
}
_zsh_autosuggest_register_highlighter() {
# Remove it from the list (if it exists) and re-add it
ZSH_HIGHLIGHT_HIGHLIGHTERS=("${(@)ZSH_HIGHLIGHT_HIGHLIGHTERS:#autosuggestion}")
ZSH_HIGHLIGHT_HIGHLIGHTERS+=(autosuggestion)
}
_zsh_highlight_autosuggestion_highlighter_predicate() {
[ "$_ZSH_AUTOSUGGESTION_ACTIVE" = true ]
}
_zsh_highlight_autosuggestion_highlighter() {
region_highlight+=("$(_zsh_autosuggest_region_highlight)")
}

53
lib/widget/hook.zsh Normal file
View file

@ -0,0 +1,53 @@
_zsh_autosuggest_is_defined_widget() {
[ -n "$widgets[$1]" ]
}
_zsh_autosuggest_is_built_in_widget() {
[ -n "$widgets[.$1]" ]
}
_zsh_autosuggest_is_original_widget_defined() {
_zsh_autosuggest_is_defined_widget $(_zsh_autosuggest_original_widget $1)
}
_zsh_autosuggest_original_widget() {
if _zsh_autosuggest_is_built_in_widget $1; then
echo ".$1"
else
echo "_autosuggest_original_$1"
fi
}
_zsh_autosuggest_hook_widget() {
local autosuggest_widget=$1
local widget=$2
# Skip if the widget does not exist
if ! _zsh_autosuggest_is_defined_widget $widget; then
continue
fi
# Alias if dot-prefixed alias is unavailable and we haven't already aliased it
if ! _zsh_autosuggest_is_original_widget_defined $widget; then
zle -A $widget $(_zsh_autosuggest_original_widget $widget)
fi
# Hook it
zle -A $autosuggest_widget $widget
}
_zsh_autosuggest_hook_widgets() {
local widget
for widget in $ZSH_AUTOSUGGEST_MODIFY_WIDGETS; do
_zsh_autosuggest_hook_widget _zsh_autosuggest_widget_modify $widget
done
for widget in $ZSH_AUTOSUGGEST_CLEAR_WIDGETS; do
_zsh_autosuggest_hook_widget _zsh_autosuggest_widget_clear $widget
done
for widget in $ZSH_AUTOSUGGEST_ACCEPT_WIDGETS; do
_zsh_autosuggest_hook_widget _zsh_autosuggest_widget_accept $widget
done
}

42
lib/widget/widgets.zsh Normal file
View file

@ -0,0 +1,42 @@
# Buffer was modified, update suggestion
_zsh_autosuggest_widget_modify() {
local suggestion
zle $(_zsh_autosuggest_original_widget $WIDGET) $@
if [ $#BUFFER -gt 0 ]; then
suggestion=$(_zsh_autosuggest_get_suggestion $BUFFER)
fi
if [ -n "$suggestion" ]; then
POSTDISPLAY=${suggestion#$BUFFER}
else
unset POSTDISPLAY
fi
_zsh_autosuggest_highlight
}
# Clear command triggered, hide the suggestion
_zsh_autosuggest_widget_clear() {
unset POSTDISPLAY
_zsh_autosuggest_highlight
zle $(_zsh_autosuggest_original_widget $WIDGET) $@
}
# Suggestion accepted, add it to the buffer
_zsh_autosuggest_widget_accept() {
if [ $CURSOR -eq $#BUFFER ]; then
BUFFER="$BUFFER$POSTDISPLAY"
unset POSTDISPLAY
CURSOR=${#BUFFER}
_zsh_autosuggest_highlight
else
zle $(_zsh_autosuggest_original_widget $WIDGET) $@
fi
}
# Create the widgets
zle -N _zsh_autosuggest_widget_modify
zle -N _zsh_autosuggest_widget_clear
zle -N _zsh_autosuggest_widget_accept