Add automatic pause/resume of autosuggestions

based on cursor position
This commit is contained in:
Thiago de Arruda 2013-10-26 14:11:53 -03:00
parent 5595bcde18
commit 9b2574dc82
2 changed files with 41 additions and 18 deletions

View file

@ -4,7 +4,7 @@
## Installation ## Installation
```sh ```zsh
git clone git://github.com/tarruda/zsh-autosuggestions ~/.zsh-autosuggestions git clone git://github.com/tarruda/zsh-autosuggestions ~/.zsh-autosuggestions
cat >> ~/.zshrc << "EOF" cat >> ~/.zshrc << "EOF"
source ~/.zsh-autosuggestions/autosuggestions.zsh source ~/.zsh-autosuggestions/autosuggestions.zsh

View file

@ -1,24 +1,28 @@
# Fish-like autosuggestions for zsh. This is implemented on top of zsh's # Fish-like autosuggestions for zsh. This is implemented on top of zsh's
# builtin prediction feature by considering whatever is after the cursor # builtin prediction feature by treating whatever is after the cursor
# to be 'unmaterialized'. The unmaterialized part is highlighted and ignored # as 'unmaterialized'. The unmaterialized part is highlighted and ignored
# when the user accepts the line. To materialize autosuggestions 'TAB' must # when the user accepts the line. To materialize autosuggestions 'TAB' must
# be pressed. # be pressed.
# #
# Since predict-on doesn't work well on the middle of the line, many actions # Since predict-on doesn't work well on the middle of the line, many actions
# that move the cursor to the left will disable autosuggestions, so it should # that move the cursor to the left will pause autosuggestions, so it should
# be less obtrusive than using predict-on directly # be safe enough to leave autosuggest enabled by default with
# zle-line-init() {
# enable-autosuggestions
# }
# zle -N zle-line-init
zstyle ':predict' verbose no zstyle ':predict' verbose no
zstyle ':completion:incremental:*' completer _complete
zstyle ':completion:predict:*' completer _complete zstyle ':completion:predict:*' completer _complete
autoload predict-on autoload predict-on
disable-autosuggestions() { pause-autosuggestions() {
# When autosuggestions are disabled, kill the unmaterialized part # When autosuggestions are disabled, kill the unmaterialized part
RBUFFER='' RBUFFER=''
unset ZLE_AUTOSUGGESTING unset ZLE_AUTOSUGGESTING
ZLE_AUTOSUGGESTING_PAUSED=1
predict-off predict-off
# Restore default widgets zle -N self-insert paused-autosuggest-self-insert
zle -A .accept-line accept-line zle -A .accept-line accept-line
zle -A .vi-cmd-mode vi-cmd-mode zle -A .vi-cmd-mode vi-cmd-mode
zle -A .vi-backward-char vi-backward-char zle -A .vi-backward-char vi-backward-char
@ -33,6 +37,7 @@ disable-autosuggestions() {
} }
enable-autosuggestions() { enable-autosuggestions() {
unset ZLE_AUTOSUGGESTING_PAUSED
ZLE_AUTOSUGGESTING=1 ZLE_AUTOSUGGESTING=1
predict-on predict-on
# Save the prediction widgets # Save the prediction widgets
@ -64,9 +69,17 @@ enable-autosuggestions() {
highlight-suggested-text highlight-suggested-text
} }
disable-autosuggestions() {
if [[ -z $ZLE_AUTOSUGGESTING_PAUSED ]]; then
pause-autosuggestions
fi
unset ZLE_AUTOSUGGESTING_PAUSED
zle -A .self-insert self-insert
}
# Toggles autosuggestions on/off # Toggles autosuggestions on/off
toggle-autosuggestions() { toggle-autosuggestions() {
if [[ -n $ZLE_AUTOSUGGESTING ]]; then if [[ -n $ZLE_AUTOSUGGESTING || -n $ZLE_AUTOSUGGESTING_PAUSED ]]; then
disable-autosuggestions disable-autosuggestions
else else
enable-autosuggestions enable-autosuggestions
@ -86,52 +99,62 @@ autosuggest-accept-line() {
# When entering vi command mode, disable autosuggestions as its possible the # When entering vi command mode, disable autosuggestions as its possible the
# user is going to edit the middle of the line # user is going to edit the middle of the line
autosuggest-vi-cmd-mode() { autosuggest-vi-cmd-mode() {
disable-autosuggestions pause-autosuggestions
zle .vi-cmd-mode zle .vi-cmd-mode
} }
# Disable autosuggestions when doing anything that moves the cursor to the left # Disable autosuggestions when doing anything that moves the cursor to the left
autosuggest-vi-backward-char() { autosuggest-vi-backward-char() {
disable-autosuggestions pause-autosuggestions
zle .vi-backward-char zle .vi-backward-char
} }
autosuggest-backward-char() { autosuggest-backward-char() {
disable-autosuggestions pause-autosuggestions
zle .backward-char zle .backward-char
} }
autosuggest-backward-word() { autosuggest-backward-word() {
disable-autosuggestions pause-autosuggestions
zle .backward-word zle .backward-word
} }
autosuggest-beginning-of-line() { autosuggest-beginning-of-line() {
disable-autosuggestions pause-autosuggestions
zle .beginning-of-line zle .beginning-of-line
} }
# Searching history or moving arrows up/down also disables autosuggestion # Searching history or moving arrows up/down also disables autosuggestion
autosuggest-history-search-forward() { autosuggest-history-search-forward() {
disable-autosuggestions pause-autosuggestions
zle .history-search-forward zle .history-search-forward
} }
autosuggest-history-search-backward() { autosuggest-history-search-backward() {
disable-autosuggestions pause-autosuggestions
zle .history-search-backward zle .history-search-backward
} }
autosuggest-up-line-or-history() { autosuggest-up-line-or-history() {
disable-autosuggestions pause-autosuggestions
zle .up-line-or-history zle .up-line-or-history
} }
autosuggest-down-line-or-history() { autosuggest-down-line-or-history() {
disable-autosuggestions pause-autosuggestions
zle .down-line-or-history zle .down-line-or-history
} }
paused-autosuggest-self-insert() {
if [[ $RBUFFER == '' ]]; then
# Resume autosuggestions when inserting at the end of the line
enable-autosuggestions
zle insert-and-autosuggest
else
zle .self-insert
fi
}
highlight-suggested-text() { highlight-suggested-text() {
if [[ -n $ZLE_AUTOSUGGESTING ]]; then if [[ -n $ZLE_AUTOSUGGESTING ]]; then
region_highlight=("$(( $CURSOR + 1 )) $(( $CURSOR + $#RBUFFER )) fg=8") region_highlight=("$(( $CURSOR + 1 )) $(( $CURSOR + $#RBUFFER )) fg=8")