Skip to content

Commit 6ec9537

Browse files
committed
Call original widget before moving cursor when accepting suggestion
The check on length of `$POSTDISPLAY` is in support of the test for `vi-delete` on the last char of the buffer with `dl`. Fixes issue #482.
1 parent 54d7a9a commit 6ec9537

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/widgets.zsh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,17 @@ _zsh_autosuggest_suggest() {
119119

120120
# Accept the entire suggestion
121121
_zsh_autosuggest_accept() {
122-
local -i max_cursor_pos=$#BUFFER
122+
local -i retval max_cursor_pos=$#BUFFER
123123

124124
# When vicmd keymap is active, the cursor can't move all the way
125125
# to the end of the buffer
126126
if [[ "$KEYMAP" = "vicmd" ]]; then
127127
max_cursor_pos=$((max_cursor_pos - 1))
128128
fi
129129

130-
if (( $CURSOR != $max_cursor_pos )); then
130+
# If we're not in a valid state to accept a suggestion, just run the
131+
# original widget and bail out
132+
if (( $CURSOR != $max_cursor_pos || !$#POSTDISPLAY )); then
131133
_zsh_autosuggest_invoke_original_widget $@
132134
return
133135
fi
@@ -139,14 +141,19 @@ _zsh_autosuggest_accept() {
139141
# Remove the suggestion
140142
unset POSTDISPLAY
141143

144+
# Run the original widget before manually moving the cursor so that the
145+
# cursor movement doesn't make the widget do something unexpected
146+
_zsh_autosuggest_invoke_original_widget $@
147+
retval=$?
148+
142149
# Move the cursor to the end of the buffer
143150
if [[ "$KEYMAP" = "vicmd" ]]; then
144151
CURSOR=$(($#BUFFER - 1))
145152
else
146153
CURSOR=$#BUFFER
147154
fi
148155

149-
_zsh_autosuggest_invoke_original_widget $@
156+
return $retval
150157
}
151158

152159
# Accept the entire suggestion and execute it

zsh-autosuggestions.zsh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,15 +381,17 @@ _zsh_autosuggest_suggest() {
381381

382382
# Accept the entire suggestion
383383
_zsh_autosuggest_accept() {
384-
local -i max_cursor_pos=$#BUFFER
384+
local -i retval max_cursor_pos=$#BUFFER
385385

386386
# When vicmd keymap is active, the cursor can't move all the way
387387
# to the end of the buffer
388388
if [[ "$KEYMAP" = "vicmd" ]]; then
389389
max_cursor_pos=$((max_cursor_pos - 1))
390390
fi
391391

392-
if (( $CURSOR != $max_cursor_pos )); then
392+
# If we're not in a valid state to accept a suggestion, just run the
393+
# original widget and bail out
394+
if (( $CURSOR != $max_cursor_pos || !$#POSTDISPLAY )); then
393395
_zsh_autosuggest_invoke_original_widget $@
394396
return
395397
fi
@@ -401,14 +403,19 @@ _zsh_autosuggest_accept() {
401403
# Remove the suggestion
402404
unset POSTDISPLAY
403405

406+
# Run the original widget before manually moving the cursor so that the
407+
# cursor movement doesn't make the widget do something unexpected
408+
_zsh_autosuggest_invoke_original_widget $@
409+
retval=$?
410+
404411
# Move the cursor to the end of the buffer
405412
if [[ "$KEYMAP" = "vicmd" ]]; then
406413
CURSOR=$(($#BUFFER - 1))
407414
else
408415
CURSOR=$#BUFFER
409416
fi
410417

411-
_zsh_autosuggest_invoke_original_widget $@
418+
return $retval
412419
}
413420

414421
# Accept the entire suggestion and execute it

0 commit comments

Comments
 (0)