2013-10-30 01:27:42 +08:00
|
|
|
# Fish-like autosuggestions for zsh. Some of the code was based on the code
|
2013-12-04 03:25:52 +08:00
|
|
|
# for 'predict-on'
|
2013-10-27 02:05:12 +08:00
|
|
|
#
|
|
|
|
# ```zsh
|
2013-10-27 01:11:53 +08:00
|
|
|
# zle-line-init() {
|
2013-10-30 01:27:42 +08:00
|
|
|
# autosuggest-enable
|
2013-10-27 01:11:53 +08:00
|
|
|
# }
|
|
|
|
# zle -N zle-line-init
|
2013-10-27 02:05:12 +08:00
|
|
|
# ```
|
2013-10-29 06:51:04 +08:00
|
|
|
zmodload zsh/net/socket
|
|
|
|
|
2013-10-29 16:42:03 +08:00
|
|
|
source "${0:a:h}/completion-client.zsh"
|
2013-10-29 06:51:04 +08:00
|
|
|
|
2013-12-04 03:45:56 +08:00
|
|
|
# configuration variables
|
|
|
|
AUTOSUGGESTION_HIGHLIGHT_COLOR='fg=8'
|
|
|
|
AUTOSUGGESTION_HIGHLIGHT_CURSOR=1
|
|
|
|
|
2013-10-29 16:42:03 +08:00
|
|
|
function {
|
2013-11-08 07:12:33 +08:00
|
|
|
if [[ -n $ZLE_DISABLE_AUTOSUGGEST ]]; then
|
|
|
|
ZSH_HIGHLIGHT_HIGHLIGHTERS=()
|
|
|
|
return
|
|
|
|
fi
|
2013-11-08 05:42:17 +08:00
|
|
|
autoload -U is-at-least
|
|
|
|
|
2014-09-22 21:08:24 +08:00
|
|
|
# if is-at-least 5.0.3; then
|
|
|
|
# autosuggest-ensure-server
|
|
|
|
# fi
|
2013-10-29 16:42:03 +08:00
|
|
|
}
|
2013-10-27 00:05:17 +08:00
|
|
|
|
2013-11-08 05:42:17 +08:00
|
|
|
ZLE_AUTOSUGGEST_SUSPEND_WIDGETS=(
|
2013-10-28 01:40:10 +08:00
|
|
|
vi-cmd-mode vi-backward-char backward-char backward-word beginning-of-line
|
|
|
|
history-search-forward history-search-backward up-line-or-history
|
2013-12-05 03:07:24 +08:00
|
|
|
history-beginning-search-forward history-beginning-search-backward
|
2014-01-31 22:58:28 +08:00
|
|
|
down-line-or-history history-substring-search-up history-substring-search-down
|
2014-02-24 04:54:31 +08:00
|
|
|
backward-kill-word
|
2013-10-28 01:40:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
ZLE_AUTOSUGGEST_COMPLETION_WIDGETS=(
|
|
|
|
complete-word expand-or-complete expand-or-complete-prefix list-choices
|
|
|
|
menu-complete reverse-menu-complete menu-expand-or-complete menu-select
|
|
|
|
accept-and-menu-complete
|
|
|
|
)
|
2013-10-27 00:05:17 +08:00
|
|
|
|
2013-11-08 19:20:57 +08:00
|
|
|
ZLE_AUTOSUGGEST_ACCEPT_WIDGETS=(
|
|
|
|
vi-forward-char forward-char vi-forward-word forward-word vi-add-eol
|
2014-01-31 22:58:28 +08:00
|
|
|
vi-add-next vi-forward-blank-word end-of-line
|
2013-11-08 19:20:57 +08:00
|
|
|
)
|
|
|
|
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-pause() {
|
|
|
|
[[ -z $ZLE_AUTOSUGGESTING ]] && return
|
|
|
|
unset ZLE_AUTOSUGGESTING
|
2013-10-28 01:40:10 +08:00
|
|
|
local widget
|
2013-10-27 00:05:17 +08:00
|
|
|
# When autosuggestions are disabled, kill the unmaterialized part
|
|
|
|
RBUFFER=''
|
2013-11-08 05:42:17 +08:00
|
|
|
zle -A autosuggest-paused-self-insert self-insert
|
|
|
|
zle -A autosuggest-magic-space-orig magic-space
|
|
|
|
zle -A autosuggest-backward-delete-char-orig backward-delete-char
|
|
|
|
zle -A autosuggest-accept-line-orig accept-line
|
2013-11-08 19:20:57 +08:00
|
|
|
for widget in $ZLE_AUTOSUGGEST_ACCEPT_WIDGETS $ZLE_AUTOSUGGEST_SUSPEND_WIDGETS $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
|
2013-11-08 22:49:33 +08:00
|
|
|
[[ -z $widgets[$widget] || -z $widgets[autosuggest-${widget}-orig] ]] &&\
|
|
|
|
continue
|
2013-11-08 05:42:17 +08:00
|
|
|
eval "zle -A autosuggest-${widget}-orig ${widget}"
|
2013-10-28 01:40:10 +08:00
|
|
|
done
|
2013-10-29 21:46:39 +08:00
|
|
|
autosuggest-highlight-suggested-text
|
2013-10-30 01:27:42 +08:00
|
|
|
|
2013-11-08 05:42:17 +08:00
|
|
|
if [[ -n $ZLE_AUTOSUGGEST_CONNECTION ]]; then
|
|
|
|
zle -F $ZLE_AUTOSUGGEST_CONNECTION
|
|
|
|
fi
|
2013-10-27 00:05:17 +08:00
|
|
|
}
|
|
|
|
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-resume() {
|
2013-10-28 01:40:10 +08:00
|
|
|
[[ -n $ZLE_AUTOSUGGESTING ]] && return
|
2013-10-27 00:05:17 +08:00
|
|
|
ZLE_AUTOSUGGESTING=1
|
2013-10-30 01:27:42 +08:00
|
|
|
local widget
|
2013-10-27 00:05:17 +08:00
|
|
|
# Replace prediction widgets by versions that will also highlight RBUFFER
|
2013-11-08 05:42:17 +08:00
|
|
|
zle -A autosuggest-insert-or-space self-insert
|
2013-12-04 03:25:52 +08:00
|
|
|
zle -A autosuggest-insert-or-space magic-space
|
|
|
|
zle -A autosuggest-backward-delete-char backward-delete-char
|
|
|
|
zle -A autosuggest-accept-line accept-line
|
2013-11-08 05:42:17 +08:00
|
|
|
# Hook into some default widgets that should suspend autosuggestion
|
2013-12-04 03:25:52 +08:00
|
|
|
# automatically
|
2013-11-08 19:20:57 +08:00
|
|
|
for widget in $ZLE_AUTOSUGGEST_ACCEPT_WIDGETS; do
|
|
|
|
[[ -z $widgets[$widget] ]] && continue
|
|
|
|
eval "zle -A autosuggest-accept-suggestion $widget"
|
|
|
|
done
|
2013-11-08 05:42:17 +08:00
|
|
|
for widget in $ZLE_AUTOSUGGEST_SUSPEND_WIDGETS; do
|
|
|
|
[[ -z $widgets[$widget] ]] && continue
|
|
|
|
eval "zle -A autosuggest-suspend $widget"
|
2013-10-28 01:40:10 +08:00
|
|
|
done
|
2013-11-08 05:42:17 +08:00
|
|
|
# Hook into completion widgets to trim RBUFFER before completion
|
2013-10-28 01:40:10 +08:00
|
|
|
for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
|
2013-11-08 05:42:17 +08:00
|
|
|
[[ -z $widgets[$widget] ]] && continue
|
|
|
|
eval "zle -A autosuggest-tab $widget"
|
2013-10-28 01:40:10 +08:00
|
|
|
done
|
2013-10-30 01:27:42 +08:00
|
|
|
if [[ -n $ZLE_AUTOSUGGEST_CONNECTION ]]; then
|
|
|
|
# install listen for suggestions asynchronously
|
2013-11-08 05:42:17 +08:00
|
|
|
zle -Fw $ZLE_AUTOSUGGEST_CONNECTION autosuggest-pop-suggestion
|
2013-10-27 01:11:53 +08:00
|
|
|
fi
|
2013-10-30 01:27:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
autosuggest-start() {
|
2013-11-08 07:12:33 +08:00
|
|
|
if [[ -z $ZLE_DISABLE_AUTOSUGGEST && -n $functions[_zsh_highlight] ]]; then
|
2014-08-11 14:25:21 +08:00
|
|
|
if [[ ${ZSH_HIGHLIGHT_HIGHLIGHTERS[(i)autosuggest]} -gt ${#ZSH_HIGHLIGHT_HIGHLIGHTERS} ]];then
|
|
|
|
ZSH_HIGHLIGHT_HIGHLIGHTERS+=(autosuggest)
|
|
|
|
fi
|
2013-11-08 07:12:33 +08:00
|
|
|
fi
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-resume
|
2013-10-27 01:11:53 +08:00
|
|
|
}
|
|
|
|
|
2013-10-27 00:05:17 +08:00
|
|
|
# Toggles autosuggestions on/off
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-toggle() {
|
|
|
|
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
|
|
|
|
autosuggest-pause
|
2013-11-08 05:42:17 +08:00
|
|
|
zle -A autosuggest-self-insert-orig self-insert
|
2013-10-27 00:05:17 +08:00
|
|
|
else
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-resume
|
2013-10-27 00:05:17 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:46:39 +08:00
|
|
|
autosuggest-highlight-suggested-text() {
|
2013-11-08 07:12:33 +08:00
|
|
|
if (( $+functions[_zsh_highlight_buffer_modified] > 0 )); then
|
|
|
|
_zsh_highlight
|
2013-10-27 00:05:17 +08:00
|
|
|
else
|
|
|
|
region_highlight=()
|
2013-11-08 07:12:33 +08:00
|
|
|
_zsh_highlight_autosuggest_highlighter
|
2013-10-27 00:05:17 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-11-08 07:12:33 +08:00
|
|
|
_zsh_highlight_autosuggest_highlighter_predicate() {
|
|
|
|
[[ -n $ZLE_AUTOSUGGESTING ]] && (( $#RBUFFER > 0 ))
|
|
|
|
}
|
|
|
|
|
|
|
|
_zsh_highlight_autosuggest_highlighter() {
|
2013-12-04 03:45:56 +08:00
|
|
|
region_highlight+=("$(( $CURSOR + $AUTOSUGGESTION_HIGHLIGHT_CURSOR )) $(( $CURSOR + $#RBUFFER )) $AUTOSUGGESTION_HIGHLIGHT_COLOR")
|
2013-11-08 07:12:33 +08:00
|
|
|
}
|
|
|
|
|
2013-10-29 21:46:39 +08:00
|
|
|
autosuggest-insert-or-space() {
|
2013-11-08 05:42:17 +08:00
|
|
|
setopt localoptions noshwordsplit noksharrays
|
2013-10-29 21:46:39 +08:00
|
|
|
if [[ $LBUFFER == *$'\012'* ]] || (( PENDING )); then
|
2013-11-08 05:42:17 +08:00
|
|
|
# Editing multiline buffer or pasting a chunk of text, pause
|
|
|
|
autosuggest-suspend
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ${RBUFFER[1]} == ${KEYS[-1]} ]]; then
|
|
|
|
# Same as what's typed, just move on
|
2013-10-28 01:40:10 +08:00
|
|
|
((++CURSOR))
|
2013-11-08 07:12:33 +08:00
|
|
|
autosuggest-invalidate-highlight-cache
|
2013-10-28 01:40:10 +08:00
|
|
|
else
|
2013-11-08 05:42:17 +08:00
|
|
|
LBUFFER="$LBUFFER$KEYS"
|
|
|
|
if [[ $LASTWIDGET == (self-insert|magic-space|backward-delete-char) || $LASTWIDGET == (complete-word|accept-*|zle-line-init) ]]; then
|
|
|
|
if ! zle .history-beginning-search-backward; then
|
|
|
|
RBUFFER=''
|
|
|
|
if [[ ${KEYS[-1]} != ' ' ]]; then
|
|
|
|
autosuggest-send-request ${LBUFFER}
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2013-10-28 01:40:10 +08:00
|
|
|
fi
|
2013-11-08 05:42:17 +08:00
|
|
|
autosuggest-highlight-suggested-text
|
2013-10-28 01:40:10 +08:00
|
|
|
}
|
|
|
|
|
2013-10-29 21:46:39 +08:00
|
|
|
autosuggest-backward-delete-char() {
|
2013-11-08 05:42:17 +08:00
|
|
|
if (( $#LBUFFER > 1 )); then
|
|
|
|
setopt localoptions noshwordsplit noksharrays
|
|
|
|
if [[ $LBUFFER = *$'\012'* || $LASTWIDGET != (self-insert|magic-space|backward-delete-char) ]]; then
|
|
|
|
LBUFFER="$LBUFFER[1,-2]"
|
|
|
|
else
|
|
|
|
((--CURSOR))
|
2013-11-08 07:12:33 +08:00
|
|
|
autosuggest-invalidate-highlight-cache
|
2013-11-08 05:42:17 +08:00
|
|
|
zle .history-beginning-search-forward || RBUFFER=''
|
|
|
|
fi
|
|
|
|
autosuggest-highlight-suggested-text
|
2013-10-28 01:40:10 +08:00
|
|
|
else
|
2013-11-08 05:42:17 +08:00
|
|
|
zle .kill-whole-line
|
2013-10-28 01:40:10 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# When autosuggesting, ignore RBUFFER which corresponds to the 'unmaterialized'
|
|
|
|
# section when the user accepts the line
|
|
|
|
autosuggest-accept-line() {
|
|
|
|
RBUFFER=''
|
2013-11-08 19:20:57 +08:00
|
|
|
if ! (( $+functions[_zsh_highlight_buffer_modified] )); then
|
2013-11-08 07:12:33 +08:00
|
|
|
# Only clear the colors if the user doesn't have zsh-highlight installed
|
|
|
|
region_highlight=()
|
|
|
|
fi
|
2013-10-28 01:40:10 +08:00
|
|
|
zle .accept-line
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:46:39 +08:00
|
|
|
autosuggest-paused-self-insert() {
|
2013-10-27 17:53:52 +08:00
|
|
|
if [[ $RBUFFER == '' ]]; then
|
|
|
|
# Resume autosuggestions when inserting at the end of the line
|
2013-11-08 05:42:17 +08:00
|
|
|
autosuggest-resume
|
|
|
|
zle self-insert
|
2013-10-27 17:53:52 +08:00
|
|
|
else
|
2013-11-08 05:42:17 +08:00
|
|
|
zle autosuggest-self-insert-orig
|
2013-10-27 17:53:52 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-10-30 01:27:42 +08:00
|
|
|
autosuggest-pop-suggestion() {
|
|
|
|
local words last_word suggestion
|
|
|
|
if ! IFS= read -r -u $ZLE_AUTOSUGGEST_CONNECTION suggestion; then
|
|
|
|
# server closed the connection, stop listenting
|
|
|
|
zle -F $ZLE_AUTOSUGGEST_CONNECTION
|
|
|
|
unset ZLE_AUTOSUGGEST_CONNECTION
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
if [[ -n $suggestion ]]; then
|
|
|
|
local prefix=${suggestion%$'\2'*}
|
|
|
|
suggestion=${suggestion#*$'\2'}
|
|
|
|
# only use the suggestion if the prefix is still compatible with
|
|
|
|
# the suggestion(prefix should be contained in LBUFFER)
|
|
|
|
if [[ ${LBUFFER#$prefix*} != ${LBUFFER} ]]; then
|
|
|
|
words=(${(z)LBUFFER})
|
|
|
|
last_word=${words[-1]}
|
|
|
|
suggestion=${suggestion:$#last_word}
|
|
|
|
RBUFFER="$suggestion"
|
|
|
|
autosuggest-highlight-suggested-text
|
|
|
|
else
|
|
|
|
RBUFFER=''
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
RBUFFER=''
|
|
|
|
fi
|
|
|
|
zle -Rc
|
|
|
|
}
|
|
|
|
|
|
|
|
autosuggest-suspend() {
|
|
|
|
autosuggest-pause
|
2013-10-28 01:40:10 +08:00
|
|
|
zle autosuggest-${WIDGET}-orig "$@"
|
2013-10-27 21:49:21 +08:00
|
|
|
}
|
|
|
|
|
2013-10-28 01:40:10 +08:00
|
|
|
autosuggest-tab() {
|
2013-10-27 21:49:21 +08:00
|
|
|
RBUFFER=''
|
2013-10-28 01:40:10 +08:00
|
|
|
zle autosuggest-${WIDGET}-orig "$@"
|
2013-10-29 21:46:39 +08:00
|
|
|
autosuggest-highlight-suggested-text
|
2013-10-27 21:49:21 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 19:20:57 +08:00
|
|
|
autosuggest-accept-suggestion() {
|
2014-02-24 04:54:31 +08:00
|
|
|
if [[ AUTOSUGGESTION_ACCEPT_RIGHT_ARROW -eq 1 && "$WIDGET" == 'forward-char' ]]; then
|
|
|
|
zle autosuggest-end-of-line-orig "$@"
|
|
|
|
else
|
|
|
|
zle autosuggest-${WIDGET}-orig "$@"
|
|
|
|
fi
|
2013-11-08 07:12:33 +08:00
|
|
|
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
|
|
|
|
autosuggest-invalidate-highlight-cache
|
|
|
|
autosuggest-highlight-suggested-text
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
autosuggest-invalidate-highlight-cache() {
|
|
|
|
# invalidate the buffer for zsh-syntax-highlighting
|
|
|
|
_ZSH_HIGHLIGHT_PRIOR_BUFFER=''
|
2013-10-27 00:05:17 +08:00
|
|
|
}
|
|
|
|
|
2013-10-30 01:27:42 +08:00
|
|
|
zle -N autosuggest-toggle
|
|
|
|
zle -N autosuggest-start
|
|
|
|
zle -N autosuggest-accept-suggested-small-word
|
|
|
|
zle -N autosuggest-accept-suggested-word
|
2013-11-08 05:42:17 +08:00
|
|
|
|
|
|
|
zle -N autosuggest-paused-self-insert
|
|
|
|
zle -N autosuggest-insert-or-space
|
|
|
|
zle -N autosuggest-backward-delete-char
|
|
|
|
zle -N autosuggest-accept-line
|
|
|
|
|
2013-10-28 01:40:10 +08:00
|
|
|
zle -N autosuggest-tab
|
2013-11-08 05:42:17 +08:00
|
|
|
zle -N autosuggest-suspend
|
2013-11-08 19:20:57 +08:00
|
|
|
zle -N autosuggest-accept-suggestion
|
2013-11-08 05:42:17 +08:00
|
|
|
|
|
|
|
# Save all widgets
|
|
|
|
zle -A self-insert autosuggest-self-insert-orig
|
|
|
|
zle -A magic-space autosuggest-magic-space-orig
|
|
|
|
zle -A backward-delete-char autosuggest-backward-delete-char-orig
|
|
|
|
zle -A accept-line autosuggest-accept-line-orig
|
2013-11-08 19:20:57 +08:00
|
|
|
|
|
|
|
for widget in ${ZLE_AUTOSUGGEST_ACCEPT_WIDGETS} ${ZLE_AUTOSUGGEST_SUSPEND_WIDGETS} ${ZLE_AUTOSUGGEST_COMPLETION_WIDGETS}; do
|
2013-11-08 05:42:17 +08:00
|
|
|
[[ -z $widgets[$widget] ]] && continue
|
|
|
|
eval "zle -A $widget autosuggest-${widget}-orig"
|
|
|
|
done
|