Skip to content

Commit 0d2c9de

Browse files
authored
Merge pull request #488 from zsh-users/fixes/run-orig-before-move-cursor
Call original widget before moving cursor when accepting suggestion
2 parents 8f3f595 + 6ec9537 commit 0d2c9de

File tree

2 files changed

+48
-28
lines changed

2 files changed

+48
-28
lines changed

src/widgets.zsh

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ _zsh_autosuggest_modify() {
5656
emulate -L zsh
5757

5858
# Don't fetch a new suggestion if there's more input to be read immediately
59-
if (( $PENDING > 0 )) || (( $KEYS_QUEUED_COUNT > 0 )); then
59+
if (( $PENDING > 0 || $KEYS_QUEUED_COUNT > 0 )); then
6060
POSTDISPLAY="$orig_postdisplay"
6161
return $retval
6262
fi
@@ -119,31 +119,41 @@ _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 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
133+
_zsh_autosuggest_invoke_original_widget $@
134+
return
135+
fi
136+
130137
# Only accept if the cursor is at the end of the buffer
131-
if [[ $CURSOR = $max_cursor_pos ]]; then
132-
# Add the suggestion to the buffer
133-
BUFFER="$BUFFER$POSTDISPLAY"
138+
# Add the suggestion to the buffer
139+
BUFFER="$BUFFER$POSTDISPLAY"
134140

135-
# Remove the suggestion
136-
unset POSTDISPLAY
141+
# Remove the suggestion
142+
unset POSTDISPLAY
137143

138-
# Move the cursor to the end of the buffer
139-
if [[ "$KEYMAP" = "vicmd" ]]; then
140-
CURSOR=$(($#BUFFER - 1))
141-
else
142-
CURSOR=$#BUFFER
143-
fi
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+
149+
# Move the cursor to the end of the buffer
150+
if [[ "$KEYMAP" = "vicmd" ]]; then
151+
CURSOR=$(($#BUFFER - 1))
152+
else
153+
CURSOR=$#BUFFER
144154
fi
145155

146-
_zsh_autosuggest_invoke_original_widget $@
156+
return $retval
147157
}
148158

149159
# Accept the entire suggestion and execute it

zsh-autosuggestions.zsh

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ _zsh_autosuggest_modify() {
318318
emulate -L zsh
319319

320320
# Don't fetch a new suggestion if there's more input to be read immediately
321-
if (( $PENDING > 0 )) || (( $KEYS_QUEUED_COUNT > 0 )); then
321+
if (( $PENDING > 0 || $KEYS_QUEUED_COUNT > 0 )); then
322322
POSTDISPLAY="$orig_postdisplay"
323323
return $retval
324324
fi
@@ -381,31 +381,41 @@ _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 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
395+
_zsh_autosuggest_invoke_original_widget $@
396+
return
397+
fi
398+
392399
# Only accept if the cursor is at the end of the buffer
393-
if [[ $CURSOR = $max_cursor_pos ]]; then
394-
# Add the suggestion to the buffer
395-
BUFFER="$BUFFER$POSTDISPLAY"
400+
# Add the suggestion to the buffer
401+
BUFFER="$BUFFER$POSTDISPLAY"
396402

397-
# Remove the suggestion
398-
unset POSTDISPLAY
403+
# Remove the suggestion
404+
unset POSTDISPLAY
399405

400-
# Move the cursor to the end of the buffer
401-
if [[ "$KEYMAP" = "vicmd" ]]; then
402-
CURSOR=$(($#BUFFER - 1))
403-
else
404-
CURSOR=$#BUFFER
405-
fi
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+
411+
# Move the cursor to the end of the buffer
412+
if [[ "$KEYMAP" = "vicmd" ]]; then
413+
CURSOR=$(($#BUFFER - 1))
414+
else
415+
CURSOR=$#BUFFER
406416
fi
407417

408-
_zsh_autosuggest_invoke_original_widget $@
418+
return $retval
409419
}
410420

411421
# Accept the entire suggestion and execute it

0 commit comments

Comments
 (0)