mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2024-11-18 09:51:06 +01:00
Remove user completion obstructions
This commit is contained in:
parent
cdb15d4d98
commit
edc762de64
1 changed files with 96 additions and 89 deletions
|
@ -1,13 +1,4 @@
|
||||||
# Fish-like autosuggestions for zsh. This is implemented on top of zsh's
|
# Fish-like autosuggestions for zsh based on the code for 'predict-on'
|
||||||
# builtin prediction feature by treating whatever is after the cursor
|
|
||||||
# as 'unmaterialized'. The unmaterialized part is highlighted and ignored
|
|
||||||
# when the user accepts the line. To materialize autosuggestions 'TAB' must
|
|
||||||
# be pressed.
|
|
||||||
#
|
|
||||||
# Since predict-on doesn't work well on the middle of the line, many actions
|
|
||||||
# that move the cursor to the left will pause autosuggestions, so it should be
|
|
||||||
# safe enough to leave autosuggest enabled by default by adding the followingto
|
|
||||||
# zshrc:
|
|
||||||
#
|
#
|
||||||
# ```zsh
|
# ```zsh
|
||||||
# zle-line-init() {
|
# zle-line-init() {
|
||||||
|
@ -15,9 +6,24 @@
|
||||||
# }
|
# }
|
||||||
# zle -N zle-line-init
|
# zle -N zle-line-init
|
||||||
# ```
|
# ```
|
||||||
|
# zmodload zsh/zpty
|
||||||
|
#
|
||||||
|
|
||||||
|
ZLE_AUTOSUGGEST_PAUSE_WIDGETS=(
|
||||||
|
vi-cmd-mode vi-backward-char backward-char backward-word beginning-of-line
|
||||||
|
history-search-forward history-search-backward up-line-or-history
|
||||||
|
down-line-or-history
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
)
|
||||||
|
|
||||||
pause-autosuggestions() {
|
pause-autosuggestions() {
|
||||||
|
[[ -n $ZLE_AUTOSUGGESTING_PAUSED ]] && return
|
||||||
|
local widget
|
||||||
# When autosuggestions are disabled, kill the unmaterialized part
|
# When autosuggestions are disabled, kill the unmaterialized part
|
||||||
RBUFFER=''
|
RBUFFER=''
|
||||||
unset ZLE_AUTOSUGGESTING
|
unset ZLE_AUTOSUGGESTING
|
||||||
|
@ -25,50 +31,40 @@ pause-autosuggestions() {
|
||||||
zle -A self-insert paused-autosuggest-self-insert
|
zle -A self-insert paused-autosuggest-self-insert
|
||||||
zle -A .magic-space magic-space
|
zle -A .magic-space magic-space
|
||||||
zle -A .backward-delete-char backward-delete-char
|
zle -A .backward-delete-char backward-delete-char
|
||||||
zle -A .delete-char-or-list delete-char-or-list
|
|
||||||
zle -A .accept-line accept-line
|
zle -A .accept-line accept-line
|
||||||
zle -A .vi-cmd-mode vi-cmd-mode
|
for widget in $ZLE_AUTOSUGGEST_PAUSE_WIDGETS; do
|
||||||
zle -A .vi-backward-char vi-backward-char
|
eval "zle -A autosuggest-${widget}-orig ${widget}"
|
||||||
zle -A .backward-char backward-char
|
done
|
||||||
zle -A .backward-word backward-word
|
for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
|
||||||
zle -A .beginning-of-line beginning-of-line
|
eval "zle -A autosuggest-${widget}-orig $widget"
|
||||||
zle -A .history-search-forward history-search-forward
|
done
|
||||||
zle -A .history-search-backward history-search-backward
|
|
||||||
zle -A .up-line-or-history up-line-or-history
|
|
||||||
zle -A .down-line-or-history down-line-or-history
|
|
||||||
zle -A .complete-word complete-word
|
|
||||||
zle -A .expand-or-complete expand-or-complete
|
|
||||||
highlight-suggested-text
|
highlight-suggested-text
|
||||||
}
|
}
|
||||||
|
|
||||||
enable-autosuggestions() {
|
enable-autosuggestions() {
|
||||||
|
[[ -n $ZLE_AUTOSUGGESTING ]] && return
|
||||||
|
local widget
|
||||||
unset ZLE_AUTOSUGGESTING_PAUSED
|
unset ZLE_AUTOSUGGESTING_PAUSED
|
||||||
ZLE_AUTOSUGGESTING=1
|
ZLE_AUTOSUGGESTING=1
|
||||||
# Replace prediction widgets by versions that will also highlight RBUFFER
|
# Replace prediction widgets by versions that will also highlight RBUFFER
|
||||||
zle -N self-insert autosuggest-self-insert
|
zle -N self-insert autosuggest-self-insert
|
||||||
zle -N self-insert autosuggest-self-insert
|
zle -N magic-space autosuggest-self-insert
|
||||||
zle -N backward-delete-char autosuggest-delete
|
zle -N backward-delete-char autosuggest-delete
|
||||||
zle -N delete-char-or-list autosuggest-delete
|
zle -N accept-line autosuggest-accept-line
|
||||||
# Replace some default widgets that should disable autosuggestion
|
# Hook into some default widgets that should pause autosuggestion
|
||||||
# automatically
|
# automatically
|
||||||
zle -N accept-line execute-widget-and-pause
|
for widget in $ZLE_AUTOSUGGEST_PAUSE_WIDGETS; do
|
||||||
zle -N vi-cmd-mode execute-widget-and-pause
|
eval "zle -A $widget autosuggest-${widget}-orig; \
|
||||||
zle -N vi-backward-char execute-widget-and-pause
|
zle -A autosuggest-pause $widget"
|
||||||
zle -N backward-char execute-widget-and-pause
|
done
|
||||||
zle -N backward-word execute-widget-and-pause
|
# Hook into completion widgets to handle suggestions after completions
|
||||||
zle -N beginning-of-line execute-widget-and-pause
|
for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
|
||||||
zle -N history-search-forward execute-widget-and-pause
|
eval "zle -A $widget autosuggest-${widget}-orig; \
|
||||||
zle -N history-search-backward execute-widget-and-pause
|
zle -A autosuggest-tab $widget"
|
||||||
zle -N up-line-or-history execute-widget-and-pause
|
done
|
||||||
zle -N down-line-or-history execute-widget-and-pause
|
|
||||||
zle -N complete-word autosuggest-expand-or-complete
|
|
||||||
zle -N expand-or-complete autosuggest-expand-or-complete
|
|
||||||
if [[ $BUFFER != '' ]]; then
|
if [[ $BUFFER != '' ]]; then
|
||||||
local cursor=$CURSOR
|
show-suggestion
|
||||||
zle .expand-or-complete
|
|
||||||
CURSOR=$cursor
|
|
||||||
fi
|
fi
|
||||||
highlight-suggested-text
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disable-autosuggestions() {
|
disable-autosuggestions() {
|
||||||
|
@ -88,57 +84,17 @@ toggle-autosuggestions() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# TODO Most of the widgets here only override default widgets to disable
|
|
||||||
# autosuggestion, find a way to do it in a loop for the sake of maintainability
|
|
||||||
|
|
||||||
# When autosuggesting, ignore RBUFFER which corresponds to the 'unmaterialized'
|
|
||||||
# section when the user accepts the line
|
|
||||||
autosuggest-accept-line() {
|
|
||||||
RBUFFER=''
|
|
||||||
region_highlight=()
|
|
||||||
zle .accept-line
|
|
||||||
}
|
|
||||||
|
|
||||||
execute-widget-and-pause() {
|
|
||||||
pause-autosuggestions
|
|
||||||
zle .$WIDGET "$@"
|
|
||||||
}
|
|
||||||
|
|
||||||
highlight-suggested-text() {
|
highlight-suggested-text() {
|
||||||
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
|
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
|
||||||
local color='fg=8'
|
local color='fg=8'
|
||||||
[[ -n $AUTOSUGGESTION_HIGHLIGHT_COLOR ]] &&\
|
[[ -n $AUTOSUGGESTION_HIGHLIGHT_COLOR ]] &&\
|
||||||
color=$AUTOSUGGESTION_HIGHLIGHT_COLOR
|
color=$AUTOSUGGESTION_HIGHLIGHT_COLOR
|
||||||
region_highlight=("$(( $CURSOR + 1 )) $(( $CURSOR + $#RBUFFER )) $color")
|
region_highlight=("$(( $CURSOR + 1 )) $(( $CURSOR + $#RBUFFER )) $color")
|
||||||
else
|
else
|
||||||
region_highlight=()
|
region_highlight=()
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
paused-autosuggest-self-insert() {
|
|
||||||
if [[ $RBUFFER == '' ]]; then
|
|
||||||
# Resume autosuggestions when inserting at the end of the line
|
|
||||||
enable-autosuggestions
|
|
||||||
autosuggest-self-insert
|
|
||||||
else
|
|
||||||
zle .self-insert
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
show-suggestion() {
|
|
||||||
local complete_word=$1
|
|
||||||
if ! zle .history-beginning-search-backward; then
|
|
||||||
RBUFFER=''
|
|
||||||
if [[ $LBUFFER[-1] != ' ' ]]; then
|
|
||||||
integer curs=$CURSOR
|
|
||||||
unsetopt automenu recexact
|
|
||||||
zle complete-word-orig
|
|
||||||
CURSOR=$curs
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
highlight-suggested-text
|
|
||||||
}
|
|
||||||
|
|
||||||
autosuggest-self-insert() {
|
autosuggest-self-insert() {
|
||||||
setopt localoptions noshwordsplit noksharrays
|
setopt localoptions noshwordsplit noksharrays
|
||||||
if [[ ${RBUFFER[1]} == ${KEYS[-1]} ]]; then
|
if [[ ${RBUFFER[1]} == ${KEYS[-1]} ]]; then
|
||||||
|
@ -151,15 +107,66 @@ autosuggest-self-insert() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Taken from predict-on
|
||||||
autosuggest-delete() {
|
autosuggest-delete() {
|
||||||
zle .$WIDGET
|
if (( $#LBUFFER > 1 )); then
|
||||||
show-suggestion
|
setopt localoptions noshwordsplit noksharrays
|
||||||
|
# When editing a multiline buffer, it's unlikely prediction is wanted;
|
||||||
|
# or if the last widget was e.g. a motion, then probably the intent is
|
||||||
|
# to actually edit the line, not change the search prefix.
|
||||||
|
if [[ $LASTWIDGET != (self-insert|magic-space|backward-delete-char) ]]; then
|
||||||
|
LBUFFER="$LBUFFER[1,-2]"
|
||||||
|
else
|
||||||
|
((--CURSOR))
|
||||||
|
zle .history-beginning-search-forward || RBUFFER=""
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
zle .kill-whole-line
|
||||||
|
fi
|
||||||
|
highlight-suggested-text
|
||||||
}
|
}
|
||||||
|
|
||||||
autosuggest-expand-or-complete() {
|
# When autosuggesting, ignore RBUFFER which corresponds to the 'unmaterialized'
|
||||||
|
# section when the user accepts the line
|
||||||
|
autosuggest-accept-line() {
|
||||||
RBUFFER=''
|
RBUFFER=''
|
||||||
zle .$WIDGET "$@"
|
region_highlight=()
|
||||||
show-suggestion
|
zle .accept-line
|
||||||
|
}
|
||||||
|
|
||||||
|
paused-autosuggest-self-insert() {
|
||||||
|
if [[ $RBUFFER == '' ]]; then
|
||||||
|
# Resume autosuggestions when inserting at the end of the line
|
||||||
|
enable-autosuggestions
|
||||||
|
autosuggest-self-insert
|
||||||
|
else
|
||||||
|
zle .self-insert
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
autosuggest-first-completion() {
|
||||||
|
zle .complete-word || return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
show-suggestion() {
|
||||||
|
[[ $LBUFFER == '' ]] && return
|
||||||
|
# TODO need a way to reset HISTNO so .history-beginning-search-backward
|
||||||
|
# will always retrieve the last matching history entry
|
||||||
|
# unset HISTNO
|
||||||
|
zle .history-beginning-search-backward || autosuggest-first-completion\
|
||||||
|
|| RBUFFER=''
|
||||||
|
highlight-suggested-text
|
||||||
|
}
|
||||||
|
|
||||||
|
autosuggest-pause() {
|
||||||
|
pause-autosuggestions
|
||||||
|
zle autosuggest-${WIDGET}-orig "$@"
|
||||||
|
}
|
||||||
|
|
||||||
|
autosuggest-tab() {
|
||||||
|
RBUFFER=''
|
||||||
|
zle autosuggest-${WIDGET}-orig "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
accept-suggested-small-word() {
|
accept-suggested-small-word() {
|
||||||
|
@ -173,7 +180,7 @@ accept-suggested-word() {
|
||||||
}
|
}
|
||||||
|
|
||||||
zle -N toggle-autosuggestions
|
zle -N toggle-autosuggestions
|
||||||
zle -N enable-autosuggestions
|
|
||||||
zle -N disable-autosuggestions
|
|
||||||
zle -N accept-suggested-small-word
|
zle -N accept-suggested-small-word
|
||||||
zle -N accept-suggested-word
|
zle -N accept-suggested-word
|
||||||
|
zle -N autosuggest-pause
|
||||||
|
zle -N autosuggest-tab
|
||||||
|
|
Loading…
Reference in a new issue