Fixed accept-line and motion widgets integration

with zsh-syntax-highlighting
This commit is contained in:
Thiago de Arruda 2013-11-08 08:20:57 -03:00
parent a0d6493f34
commit 7d737e3cc8
2 changed files with 34 additions and 31 deletions

View file

@ -17,15 +17,23 @@ zle-line-init() {
} }
zle -N zle-line-init zle -N zle-line-init
# use ctrl+t to toggle autosuggestions(hopefully this wont be needed) # use ctrl+t to toggle autosuggestions(hopefully this wont be needed as
autosuggestions is designed to be unobtrusive)
bindkey '^T' autosuggest-toggle bindkey '^T' autosuggest-toggle
# use ctrl+f to accept suggestions
bindkey '^F' autosuggest-accept-suggested-small-word
# or
bindkey '^F' autosuggest-accept-suggested-word
EOF
``` ```
I recommend binding ctrl+f to autosuggest-accept-suggested-small-word, as the
[zsh-history-substring-search](https://github.com/zsh-users/zsh-history-substring-search) Any widget that moves the cursor to the right(forward-word, forward-char...)
plugin will already provide a key to accept the entire suggestion will accept parts of the suggested text. For example, vi-mode users can do
this:
```sh
# Accept suggestions without leaving insert mode
bindkey '^f' vi-forward-word
# or
bindkey '^f' vi-forward-blank-word
```
Emacs-mode users can simply use alt+f which is bound to forward-word
The [zsh-history-substring-search](https://github.com/zsh-users/zsh-history-substring-search)
plugin is also recommended.

View file

@ -35,6 +35,11 @@ menu-complete reverse-menu-complete menu-expand-or-complete menu-select
accept-and-menu-complete accept-and-menu-complete
) )
ZLE_AUTOSUGGEST_ACCEPT_WIDGETS=(
vi-forward-char forward-char vi-forward-word forward-word vi-add-eol
vi-add-next vi-forward-blank-word
)
autosuggest-pause() { autosuggest-pause() {
[[ -z $ZLE_AUTOSUGGESTING ]] && return [[ -z $ZLE_AUTOSUGGESTING ]] && return
unset ZLE_AUTOSUGGESTING unset ZLE_AUTOSUGGESTING
@ -45,11 +50,7 @@ autosuggest-pause() {
zle -A autosuggest-magic-space-orig magic-space zle -A autosuggest-magic-space-orig magic-space
zle -A autosuggest-backward-delete-char-orig backward-delete-char zle -A autosuggest-backward-delete-char-orig backward-delete-char
zle -A autosuggest-accept-line-orig accept-line zle -A autosuggest-accept-line-orig accept-line
for widget in $ZLE_AUTOSUGGEST_SUSPEND_WIDGETS; do for widget in $ZLE_AUTOSUGGEST_ACCEPT_WIDGETS $ZLE_AUTOSUGGEST_SUSPEND_WIDGETS $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
[[ -z $widgets[$widget] ]] && continue
eval "zle -A autosuggest-${widget}-orig ${widget}"
done
for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
[[ -z $widgets[$widget] ]] && continue [[ -z $widgets[$widget] ]] && continue
eval "zle -A autosuggest-${widget}-orig ${widget}" eval "zle -A autosuggest-${widget}-orig ${widget}"
done done
@ -71,6 +72,10 @@ autosuggest-resume() {
zle -A autosuggest-accept-line accept-line zle -A autosuggest-accept-line accept-line
# Hook into some default widgets that should suspend autosuggestion # Hook into some default widgets that should suspend autosuggestion
# automatically # automatically
for widget in $ZLE_AUTOSUGGEST_ACCEPT_WIDGETS; do
[[ -z $widgets[$widget] ]] && continue
eval "zle -A autosuggest-accept-suggestion $widget"
done
for widget in $ZLE_AUTOSUGGEST_SUSPEND_WIDGETS; do for widget in $ZLE_AUTOSUGGEST_SUSPEND_WIDGETS; do
[[ -z $widgets[$widget] ]] && continue [[ -z $widgets[$widget] ]] && continue
eval "zle -A autosuggest-suspend $widget" eval "zle -A autosuggest-suspend $widget"
@ -169,7 +174,7 @@ autosuggest-backward-delete-char() {
# section when the user accepts the line # section when the user accepts the line
autosuggest-accept-line() { autosuggest-accept-line() {
RBUFFER='' RBUFFER=''
if (( $+functions[_zsh_highlight_buffer_modified] > 0 )); then if ! (( $+functions[_zsh_highlight_buffer_modified] )); then
# Only clear the colors if the user doesn't have zsh-highlight installed # Only clear the colors if the user doesn't have zsh-highlight installed
region_highlight=() region_highlight=()
fi fi
@ -225,17 +230,9 @@ autosuggest-tab() {
autosuggest-highlight-suggested-text autosuggest-highlight-suggested-text
} }
autosuggest-accept-suggested-small-word() { autosuggest-accept-suggestion() {
zle autosuggest-${WIDGET}-orig "$@"
if [[ -n $ZLE_AUTOSUGGESTING ]]; then if [[ -n $ZLE_AUTOSUGGESTING ]]; then
zle .vi-forward-word
autosuggest-invalidate-highlight-cache
autosuggest-highlight-suggested-text
fi
}
autosuggest-accept-suggested-word() {
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
zle .forward-word
autosuggest-invalidate-highlight-cache autosuggest-invalidate-highlight-cache
autosuggest-highlight-suggested-text autosuggest-highlight-suggested-text
fi fi
@ -258,17 +255,15 @@ zle -N autosuggest-accept-line
zle -N autosuggest-tab zle -N autosuggest-tab
zle -N autosuggest-suspend zle -N autosuggest-suspend
zle -N autosuggest-accept-suggestion
# Save all widgets # Save all widgets
zle -A self-insert autosuggest-self-insert-orig zle -A self-insert autosuggest-self-insert-orig
zle -A magic-space autosuggest-magic-space-orig zle -A magic-space autosuggest-magic-space-orig
zle -A backward-delete-char autosuggest-backward-delete-char-orig zle -A backward-delete-char autosuggest-backward-delete-char-orig
zle -A accept-line autosuggest-accept-line-orig zle -A accept-line autosuggest-accept-line-orig
for widget in $ZLE_AUTOSUGGEST_SUSPEND_WIDGETS; do
[[ -z $widgets[$widget] ]] && continue for widget in ${ZLE_AUTOSUGGEST_ACCEPT_WIDGETS} ${ZLE_AUTOSUGGEST_SUSPEND_WIDGETS} ${ZLE_AUTOSUGGEST_COMPLETION_WIDGETS}; do
eval "zle -A $widget autosuggest-${widget}-orig"
done
for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
[[ -z $widgets[$widget] ]] && continue [[ -z $widgets[$widget] ]] && continue
eval "zle -A $widget autosuggest-${widget}-orig" eval "zle -A $widget autosuggest-${widget}-orig"
done done